1 | /** |
---|
2 | * @file |
---|
3 | * |
---|
4 | * @ingroup generic_or1k_uart |
---|
5 | * |
---|
6 | * @brief UART support. |
---|
7 | */ |
---|
8 | |
---|
9 | /* |
---|
10 | * COPYRIGHT (c) 2014-2015 Hesham ALMatary <heshamelmatary@gmail.com> |
---|
11 | * |
---|
12 | * The license and distribution terms for this file may be |
---|
13 | * found in the file LICENSE in this distribution or at |
---|
14 | * http://www.rtems.org/license/LICENSE |
---|
15 | */ |
---|
16 | |
---|
17 | #include <libchip/sersupp.h> |
---|
18 | #include <bsp/generic_or1k.h> |
---|
19 | #include <bsp.h> |
---|
20 | #include <bsp/irq.h> |
---|
21 | #include <bsp/uart.h> |
---|
22 | #include <rtems/score/isr.h> |
---|
23 | |
---|
24 | static void uart_initialize(int minor); |
---|
25 | static int uart_first_open(int major, int minor, void *arg); |
---|
26 | static int uart_last_close(int major, int minor, void *arg); |
---|
27 | static int uart_read_polled(int minor); |
---|
28 | static ssize_t uart_write(int minor, const char *buf, size_t len); |
---|
29 | static void uart_write_polled(int minor, char c); |
---|
30 | static int uart_set_attributes(int minor, const struct termios *t); |
---|
31 | |
---|
32 | #if 0 |
---|
33 | /* |
---|
34 | * These will be useful when the driver supports interrupt driven IO. |
---|
35 | */ |
---|
36 | static rtems_vector_number uart_get_irq_number(const console_tbl *ct) |
---|
37 | { |
---|
38 | return ct->ulIntVector; |
---|
39 | } |
---|
40 | |
---|
41 | static uint32_t uart_get_baud(const console_tbl *ct) |
---|
42 | { |
---|
43 | return ct->ulClock; |
---|
44 | } |
---|
45 | #endif |
---|
46 | |
---|
47 | static void uart_set_baud(int baud) |
---|
48 | { |
---|
49 | uint16_t divisor = (OR1K_BSP_CLOCK_FREQ) / (16 * baud); |
---|
50 | OR1K_REG(OR1K_BSP_UART_REG_LINE_CTRL) = |
---|
51 | OR1K_BSP_UART_REG_LINE_CTRL_DLAB; |
---|
52 | |
---|
53 | OR1K_REG(OR1K_BSP_UART_REG_DEV_LATCH_LOW) = divisor & 0xff; |
---|
54 | |
---|
55 | OR1K_REG(OR1K_BSP_UART_REG_DEV_LATCH_HIGH) = |
---|
56 | (divisor >> 8); |
---|
57 | } |
---|
58 | |
---|
59 | static void uart_initialize(int minor) |
---|
60 | { |
---|
61 | /* Set baud rate */ |
---|
62 | uart_set_baud(OR1K_UART_DEFAULT_BAUD); |
---|
63 | |
---|
64 | /* Set data pattern configuration */ |
---|
65 | OR1K_REG(OR1K_BSP_UART_REG_LINE_CTRL) = |
---|
66 | OR1K_BSP_UART_REG_LINE_CTRL_WLEN8; |
---|
67 | |
---|
68 | /* Reset receiver and transmitter */ |
---|
69 | OR1K_REG(OR1K_BSP_UART_REG_FIFO_CTRL) = |
---|
70 | OR1K_BSP_UART_REG_FIFO_CTRL_ENABLE_FIFO | |
---|
71 | OR1K_BSP_UART_REG_FIFO_CTRL_CLEAR_RCVR | |
---|
72 | OR1K_BSP_UART_REG_FIFO_CTRL_TRIGGER_14; |
---|
73 | |
---|
74 | /* Disable all interrupts */ |
---|
75 | OR1K_REG(OR1K_BSP_UART_REG_INT_ENABLE) = 0x00; |
---|
76 | |
---|
77 | } |
---|
78 | |
---|
79 | static int uart_first_open(int major, int minor, void *arg) |
---|
80 | { |
---|
81 | rtems_libio_open_close_args_t *oc = (rtems_libio_open_close_args_t *) arg; |
---|
82 | struct rtems_termios_tty *tty = (struct rtems_termios_tty *) oc->iop->data1; |
---|
83 | const console_tbl *ct = Console_Port_Tbl [minor]; |
---|
84 | console_data *cd = &Console_Port_Data [minor]; |
---|
85 | |
---|
86 | cd->termios_data = tty; |
---|
87 | rtems_termios_set_initial_baud(tty, ct->ulClock); |
---|
88 | |
---|
89 | return 0; |
---|
90 | } |
---|
91 | |
---|
92 | static int uart_last_close(int major, int minor, void *arg) |
---|
93 | { |
---|
94 | return 0; |
---|
95 | } |
---|
96 | |
---|
97 | static int uart_read_polled(int minor) |
---|
98 | { |
---|
99 | unsigned char lsr; |
---|
100 | |
---|
101 | /* Get a character when avaiable */ |
---|
102 | do { |
---|
103 | lsr = OR1K_REG(OR1K_BSP_UART_REG_LINE_STATUS); |
---|
104 | } while ((lsr & OR1K_BSP_UART_REG_LINE_STATUS_DR) |
---|
105 | != OR1K_BSP_UART_REG_LINE_STATUS_DR); |
---|
106 | |
---|
107 | return OR1K_REG(OR1K_BSP_UART_REG_RX); |
---|
108 | } |
---|
109 | |
---|
110 | static void uart_write_polled(int minor, char c) |
---|
111 | { |
---|
112 | unsigned char lsr; |
---|
113 | |
---|
114 | /* Wait until there is no pending data in the transmitter FIFO (empty) */ |
---|
115 | do { |
---|
116 | lsr = OR1K_REG(OR1K_BSP_UART_REG_LINE_STATUS); |
---|
117 | } while (!(lsr & OR1K_BSP_UART_REG_LINE_STATUS_THRE)); |
---|
118 | |
---|
119 | OR1K_REG(OR1K_BSP_UART_REG_TX) = c; |
---|
120 | } |
---|
121 | |
---|
122 | static ssize_t uart_write( |
---|
123 | int minor, |
---|
124 | const char *s, |
---|
125 | size_t n |
---|
126 | ) |
---|
127 | { |
---|
128 | ssize_t i = 0; |
---|
129 | |
---|
130 | for (i = 0; i < n; ++i){ |
---|
131 | uart_write_polled(minor, s [i]); |
---|
132 | } |
---|
133 | |
---|
134 | return n; |
---|
135 | } |
---|
136 | |
---|
137 | static int uart_set_attributes(int minor, const struct termios *term) |
---|
138 | { |
---|
139 | return -1; |
---|
140 | } |
---|
141 | |
---|
142 | const console_fns generic_or1k_uart_fns = { |
---|
143 | .deviceProbe = libchip_serial_default_probe, |
---|
144 | .deviceFirstOpen = uart_first_open, |
---|
145 | .deviceLastClose = uart_last_close, |
---|
146 | .deviceRead = uart_read_polled, |
---|
147 | .deviceWrite = uart_write, |
---|
148 | .deviceInitialize = uart_initialize, |
---|
149 | .deviceWritePolled = uart_write_polled, |
---|
150 | .deviceSetAttributes = uart_set_attributes, |
---|
151 | .deviceOutputUsesInterrupts = false |
---|
152 | }; |
---|