1 | /** |
---|
2 | * @file |
---|
3 | * |
---|
4 | * @ingroup or1ksim_uart |
---|
5 | * |
---|
6 | * @brief UART support. |
---|
7 | */ |
---|
8 | |
---|
9 | /* |
---|
10 | * COPYRIGHT (c) 2014 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/or1ksim.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 | static rtems_vector_number uart_get_irq_number(const console_tbl *ct) |
---|
33 | { |
---|
34 | return ct->ulIntVector; |
---|
35 | } |
---|
36 | |
---|
37 | static uint32_t uart_get_baud(const console_tbl *ct) |
---|
38 | { |
---|
39 | return ct->ulClock; |
---|
40 | } |
---|
41 | |
---|
42 | static void uart_set_baud(int baud) |
---|
43 | { |
---|
44 | int divisor = (OR1KSIM_BSP_CLOCK_FREQ) / (16 * baud); |
---|
45 | OR1KSIM_REG(OR1KSIM_BSP_UART_REG_LINE_CTRL) |= |
---|
46 | OR1KSIM_BSP_UART_REG_LINE_CTRL_DLAB; |
---|
47 | |
---|
48 | OR1KSIM_REG(OR1KSIM_BSP_UART_REG_DEV_LATCH_LOW) = divisor & 0xff; |
---|
49 | |
---|
50 | OR1KSIM_REG(OR1KSIM_BSP_UART_REG_DEV_LATCH_HIGH) = |
---|
51 | (divisor >> 8) & 0xff; |
---|
52 | |
---|
53 | OR1KSIM_REG(OR1KSIM_BSP_UART_REG_LINE_CTRL) &= |
---|
54 | ~(OR1KSIM_BSP_UART_REG_LINE_CTRL_DLAB); |
---|
55 | } |
---|
56 | |
---|
57 | static void uart_initialize(int minor) |
---|
58 | { |
---|
59 | /* Disable all interrupts */ |
---|
60 | OR1KSIM_REG(OR1KSIM_BSP_UART_REG_INT_ENABLE) = 0x00; |
---|
61 | |
---|
62 | /* Reset receiver and transmitter */ |
---|
63 | OR1KSIM_REG(OR1KSIM_BSP_UART_REG_FIFO_CTRL) = |
---|
64 | OR1KSIM_BSP_UART_REG_FIFO_CTRL_ENABLE_FIFO | |
---|
65 | OR1KSIM_BSP_UART_REG_FIFO_CTRL_CLEAR_RCVR | |
---|
66 | OR1KSIM_BSP_UART_REG_FIFO_CTRL_CLEAR_XMIT | |
---|
67 | OR1KSIM_BSP_UART_REG_FIFO_CTRL_TRIGGER_14; |
---|
68 | |
---|
69 | /* Set data pattern configuration */ |
---|
70 | OR1KSIM_REG(OR1KSIM_BSP_UART_REG_LINE_CTRL) = |
---|
71 | OR1KSIM_BSP_UART_REG_LINE_CTRL_WLEN8 & |
---|
72 | (OR1KSIM_BSP_UART_REG_LINE_CTRL_STOP | |
---|
73 | OR1KSIM_BSP_UART_REG_LINE_CTRL_PARITY); |
---|
74 | |
---|
75 | /* Set baud rate */ |
---|
76 | uart_set_baud(OR1KSIM_UART_DEFAULT_BAUD); |
---|
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 = OR1KSIM_REG(OR1KSIM_BSP_UART_REG_LINE_STATUS); |
---|
104 | } while ((lsr & OR1KSIM_BSP_UART_REG_LINE_STATUS_DR) |
---|
105 | != OR1KSIM_BSP_UART_REG_LINE_STATUS_DR); |
---|
106 | |
---|
107 | return OR1KSIM_REG(OR1KSIM_BSP_UART_REG_RX); |
---|
108 | } |
---|
109 | |
---|
110 | static void uart_write_polled(int minor, char c) |
---|
111 | { |
---|
112 | unsigned char lsr; |
---|
113 | const uint32_t transmit_finished = |
---|
114 | (OR1KSIM_BSP_UART_REG_LINE_STATUS_TEMT | |
---|
115 | OR1KSIM_BSP_UART_REG_LINE_STATUS_THRE); |
---|
116 | |
---|
117 | /* Wait until there is no pending data in the transmitter FIFO (empty) */ |
---|
118 | do { |
---|
119 | lsr = OR1KSIM_REG(OR1KSIM_BSP_UART_REG_LINE_STATUS); |
---|
120 | } while (!(lsr & OR1KSIM_BSP_UART_REG_LINE_STATUS_THRE)); |
---|
121 | |
---|
122 | OR1KSIM_REG(OR1KSIM_BSP_UART_REG_TX) = c; |
---|
123 | |
---|
124 | /* Wait until trasmit data is finished */ |
---|
125 | do { |
---|
126 | lsr = OR1KSIM_REG(OR1KSIM_BSP_UART_REG_LINE_STATUS); |
---|
127 | } while ( (lsr & transmit_finished) != transmit_finished ); |
---|
128 | } |
---|
129 | |
---|
130 | static ssize_t uart_write( |
---|
131 | int minor, |
---|
132 | const char *s, |
---|
133 | size_t n |
---|
134 | ) |
---|
135 | { |
---|
136 | ssize_t i = 0; |
---|
137 | |
---|
138 | for (i = 0; i < n; ++i){ |
---|
139 | uart_write_polled(minor, s [i]); |
---|
140 | } |
---|
141 | |
---|
142 | return n; |
---|
143 | } |
---|
144 | |
---|
145 | static int uart_set_attributes(int minor, const struct termios *term) |
---|
146 | { |
---|
147 | return -1; |
---|
148 | } |
---|
149 | |
---|
150 | const console_fns or1ksim_uart_fns = { |
---|
151 | .deviceProbe = libchip_serial_default_probe, |
---|
152 | .deviceFirstOpen = uart_first_open, |
---|
153 | .deviceLastClose = uart_last_close, |
---|
154 | .deviceRead = uart_read_polled, |
---|
155 | .deviceWrite = uart_write, |
---|
156 | .deviceInitialize = uart_initialize, |
---|
157 | .deviceWritePolled = uart_write_polled, |
---|
158 | .deviceSetAttributes = uart_set_attributes, |
---|
159 | .deviceOutputUsesInterrupts = false |
---|
160 | }; |
---|