OpenCores
URL https://opencores.org/ocsvn/c0or1k/c0or1k/trunk

Subversion Repositories c0or1k

[/] [c0or1k/] [trunk/] [conts/] [libdev/] [uart/] [pl011/] [uart.c] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 drasko
/*
2
 * PL011 UART driver
3
 *
4
 * Copyright (C) 2009 B Labs Ltd.
5
 */
6
#include <libdev/uart.h>
7
#include <libdev/io.h>
8
#include "uart.h"
9
 
10
/* Error status bits in receive status register */
11
#define PL011_FE                (1 << 0)
12
#define PL011_PE                (1 << 1)
13
#define PL011_BE                (1 << 2)
14
#define PL011_OE                (1 << 3)
15
 
16
/* Status bits in flag register */
17
#define PL011_TXFE              (1 << 7)
18
#define PL011_RXFF              (1 << 6)
19
#define PL011_TXFF              (1 << 5)
20
#define PL011_RXFE              (1 << 4)
21
#define PL011_BUSY              (1 << 3)
22
#define PL011_DCD               (1 << 2)
23
#define PL011_DSR               (1 << 1)
24
#define PL011_CTS               (1 << 0)
25
 
26
void uart_tx_char(unsigned long base, char c)
27
{
28
        unsigned int val = 0;
29
 
30
        do {
31
                val = read((base + PL011_UARTFR));
32
        } while (val & PL011_TXFF);  /* TX FIFO FULL */
33
 
34
        write(c, (base + PL011_UARTDR));
35
}
36
 
37
char uart_rx_char(unsigned long base)
38
{
39
        unsigned int val = 0;
40
 
41
        do {
42
                val = read(base + PL011_UARTFR);
43
        } while (val & PL011_RXFE); /* RX FIFO Empty */
44
 
45
        return (char)read((base + PL011_UARTDR));
46
}
47
 
48
/*
49
 * Sets the baud rate in kbps. It is recommended to use
50
 * standard rates such as: 1200, 2400, 3600, 4800, 7200,
51
 * 9600, 14400, 19200, 28800, 38400, 57600 76800, 115200.
52
 */
53
void pl011_set_baudrate(unsigned long base, unsigned int baud,
54
                        unsigned int clkrate)
55
{
56
        const unsigned int uartclk = 24000000;  /* 24Mhz clock fixed on pb926 */
57
        unsigned int val = 0, ipart = 0, fpart = 0;
58
 
59
        /* Use default pb926 rate if no rate is supplied */
60
        if (clkrate == 0)
61
                clkrate = uartclk;
62
        if (baud > 115200 || baud < 1200)
63
                baud = 38400;   /* Default rate. */
64
 
65
        /* 24000000 / (38400 * 16) */
66
        ipart = 39;
67
 
68
        write(ipart, base + PL011_UARTIBRD);
69
        write(fpart, base + PL011_UARTFBRD);
70
 
71
        /*
72
         * For the IBAUD and FBAUD to update, we need to
73
         * write to UARTLCR_H because the 3 registers are
74
         * actually part of a single register in hardware
75
         * which only updates by a write to UARTLCR_H
76
         */
77
        val = read(base + PL011_UARTLCR_H);
78
        write(val, base + PL011_UARTLCR_H);
79
 
80
}
81
 
82
void uart_init(unsigned long uart_base)
83
{
84
        /* Initialise data register for 8 bit data read/writes */
85
        pl011_set_word_width(uart_base, 8);
86
 
87
        /*
88
         * Fifos are disabled because by default it is assumed the port
89
         * will be used as a user terminal, and in that case the typed
90
         * characters will only show up when fifos are flushed, rather than
91
         * when each character is typed. We avoid this by not using fifos.
92
         */
93
        pl011_disable_fifos(uart_base);
94
 
95
        /* Set default baud rate of 38400 */
96
        pl011_set_baudrate(uart_base, 38400, 24000000);
97
 
98
        /* Set default settings of 1 stop bit, no parity, no hw flow ctrl */
99
        pl011_set_stopbits(uart_base, 1);
100
        pl011_parity_disable(uart_base);
101
 
102
        /* Enable rx, tx, and uart chip */
103
        pl011_tx_enable(uart_base);
104
        pl011_rx_enable(uart_base);
105
        pl011_uart_enable(uart_base);
106
}
107
 
108
unsigned long uart_print_base;
109
 
110
void platform_init(void)
111
{
112
        uart_print_base = PL011_BASE;
113
 
114
        /*
115
         * We dont need to initialize uart here for variant-userspace,
116
         * as this is the same uart as used by kernel and hence
117
         * already initialized, we just need
118
         * a uart struct instance with proper base address.
119
         *
120
         * But in case of baremetal like loader, no one has done
121
         * initialization, so we need to do it.
122
         */
123
#if defined(VARIANT_BAREMETAL)
124
        uart_init(uart_print_base);
125
#endif
126
}
127
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.