1 | /* |
---|
2 | * Copyright (c) 2017 embedded brains GmbH. All rights reserved. |
---|
3 | * |
---|
4 | * embedded brains GmbH |
---|
5 | * Dornierstr. 4 |
---|
6 | * 82178 Puchheim |
---|
7 | * Germany |
---|
8 | * <info@embedded-brains.de> |
---|
9 | * |
---|
10 | * The license and distribution terms for this file may be |
---|
11 | * found in the file LICENSE in this distribution or at |
---|
12 | * http://www.rtems.org/license/LICENSE. |
---|
13 | */ |
---|
14 | |
---|
15 | #include <rtems/bspIo.h> |
---|
16 | #include <rtems/console.h> |
---|
17 | #include <rtems/sysinit.h> |
---|
18 | #include <rtems/termiostypes.h> |
---|
19 | |
---|
20 | #include <bsp.h> |
---|
21 | #include <bsp/fdt.h> |
---|
22 | #include <bsp/irq.h> |
---|
23 | |
---|
24 | #include <arm/freescale/imx/imx_uartreg.h> |
---|
25 | |
---|
26 | #include <libfdt.h> |
---|
27 | |
---|
28 | typedef struct { |
---|
29 | rtems_termios_device_context base; |
---|
30 | volatile imx_uart *regs; |
---|
31 | #ifdef CONSOLE_USE_INTERRUPTS |
---|
32 | rtems_vector_number irq; |
---|
33 | bool transmitting; |
---|
34 | #endif |
---|
35 | } imx_uart_context; |
---|
36 | |
---|
37 | static imx_uart_context imx_uart_instances[7]; |
---|
38 | |
---|
39 | static imx_uart_context *imx_uart_console; |
---|
40 | |
---|
41 | static volatile imx_uart *imx_uart_get_regs(rtems_termios_device_context *base) |
---|
42 | { |
---|
43 | imx_uart_context *ctx; |
---|
44 | |
---|
45 | ctx = (imx_uart_context *) base; |
---|
46 | return ctx->regs; |
---|
47 | } |
---|
48 | |
---|
49 | static void imx_uart_write_polled(rtems_termios_device_context *base, char c) |
---|
50 | { |
---|
51 | volatile imx_uart *regs; |
---|
52 | |
---|
53 | regs = imx_uart_get_regs(base); |
---|
54 | |
---|
55 | while ((regs->usr1 & IMX_UART_USR1_TRDY) == 0) { |
---|
56 | /* Wait */ |
---|
57 | } |
---|
58 | |
---|
59 | regs->utxd = IMX_UART_UTXD_TX_DATA(c); |
---|
60 | } |
---|
61 | |
---|
62 | static void imx_output_char(char c) |
---|
63 | { |
---|
64 | imx_uart_context *ctx; |
---|
65 | |
---|
66 | ctx = imx_uart_console; |
---|
67 | |
---|
68 | if (c == '\n') { |
---|
69 | imx_uart_write_polled(&ctx->base, '\r'); |
---|
70 | } |
---|
71 | |
---|
72 | imx_uart_write_polled(&ctx->base, c); |
---|
73 | } |
---|
74 | |
---|
75 | static void imx_uart_init_context( |
---|
76 | imx_uart_context *ctx, |
---|
77 | const char *fdt, |
---|
78 | const char *serial |
---|
79 | ) |
---|
80 | { |
---|
81 | int node; |
---|
82 | int len; |
---|
83 | const uint32_t *val; |
---|
84 | |
---|
85 | rtems_termios_device_context_initialize(&ctx->base, "UART"); |
---|
86 | |
---|
87 | node = fdt_path_offset(fdt, serial); |
---|
88 | |
---|
89 | val = fdt_getprop(fdt, node, "reg", &len); |
---|
90 | if (val != NULL && len >= 4) { |
---|
91 | ctx->regs = (imx_uart *) fdt32_to_cpu(val[0]); |
---|
92 | } |
---|
93 | |
---|
94 | #ifdef CONSOLE_USE_INTERRUPTS |
---|
95 | val = fdt_getprop(fdt, node, "interrupts", &len); |
---|
96 | if (val != NULL && len >= 8) { |
---|
97 | ctx->irq = bsp_fdt_map_intr(fdt32_to_cpu(val[1])); |
---|
98 | } |
---|
99 | #endif |
---|
100 | } |
---|
101 | |
---|
102 | static void imx_uart_probe(void) |
---|
103 | { |
---|
104 | const void *fdt; |
---|
105 | int node; |
---|
106 | int offset; |
---|
107 | const char *console; |
---|
108 | size_t i; |
---|
109 | |
---|
110 | fdt = bsp_fdt_get(); |
---|
111 | node = fdt_path_offset(fdt, "/chosen"); |
---|
112 | |
---|
113 | console = fdt_getprop(fdt, node, "stdout-path", NULL); |
---|
114 | if (console == NULL) { |
---|
115 | console = ""; |
---|
116 | } |
---|
117 | |
---|
118 | node = fdt_path_offset(fdt, "/aliases"); |
---|
119 | offset = fdt_first_property_offset(fdt, node); |
---|
120 | i = 0; |
---|
121 | |
---|
122 | while (offset >= 0 && i < RTEMS_ARRAY_SIZE(imx_uart_instances)) { |
---|
123 | const struct fdt_property *prop; |
---|
124 | |
---|
125 | prop = fdt_get_property_by_offset(fdt, offset, NULL); |
---|
126 | |
---|
127 | if (prop != NULL) { |
---|
128 | const char *name; |
---|
129 | |
---|
130 | name = fdt_string(fdt, fdt32_to_cpu(prop->nameoff)); |
---|
131 | if (strstr(name, "serial") != NULL) { |
---|
132 | imx_uart_context *ctx; |
---|
133 | const char *serial; |
---|
134 | |
---|
135 | ctx = &imx_uart_instances[i]; |
---|
136 | serial = prop->data; |
---|
137 | |
---|
138 | if (strcmp(serial, console) == 0) { |
---|
139 | imx_uart_console = ctx; |
---|
140 | } |
---|
141 | |
---|
142 | imx_uart_init_context(ctx, fdt, serial); |
---|
143 | ++i; |
---|
144 | } |
---|
145 | } |
---|
146 | |
---|
147 | offset = fdt_next_property_offset(fdt, offset); |
---|
148 | } |
---|
149 | |
---|
150 | BSP_output_char = imx_output_char; |
---|
151 | } |
---|
152 | |
---|
153 | static void imx_output_char_init(char c) |
---|
154 | { |
---|
155 | imx_uart_probe(); |
---|
156 | imx_output_char(c); |
---|
157 | } |
---|
158 | |
---|
159 | BSP_output_char_function_type BSP_output_char = imx_output_char_init; |
---|
160 | |
---|
161 | BSP_polling_getchar_function_type BSP_poll_char = NULL; |
---|
162 | |
---|
163 | #ifdef CONSOLE_USE_INTERRUPTS |
---|
164 | static void imx_uart_interrupt(void *arg) |
---|
165 | { |
---|
166 | rtems_termios_tty *tty; |
---|
167 | imx_uart_context *ctx; |
---|
168 | volatile imx_uart *regs; |
---|
169 | uint32_t usr2; |
---|
170 | |
---|
171 | tty = arg; |
---|
172 | ctx = rtems_termios_get_device_context(tty); |
---|
173 | regs = ctx->regs; |
---|
174 | usr2 = regs->usr2; |
---|
175 | |
---|
176 | regs->usr1 = IMX_UART_USR1_AGTIM; |
---|
177 | |
---|
178 | while ((usr2 & IMX_UART_USR2_RDR) != 0) { |
---|
179 | char c; |
---|
180 | |
---|
181 | c = IMX_UART_URXD_RX_DATA_GET(regs->urxd); |
---|
182 | rtems_termios_enqueue_raw_characters(tty, &c, 1); |
---|
183 | usr2 = regs->usr2; |
---|
184 | } |
---|
185 | |
---|
186 | if (ctx->transmitting && (regs->usr1 & IMX_UART_USR1_TRDY) != 0) { |
---|
187 | rtems_termios_dequeue_characters(tty, 1); |
---|
188 | } |
---|
189 | } |
---|
190 | #endif |
---|
191 | |
---|
192 | static bool imx_uart_set_attributes( |
---|
193 | rtems_termios_device_context *base, |
---|
194 | const struct termios *term |
---|
195 | ) |
---|
196 | { |
---|
197 | return true; |
---|
198 | } |
---|
199 | |
---|
200 | static bool imx_uart_first_open( |
---|
201 | rtems_termios_tty *tty, |
---|
202 | rtems_termios_device_context *base, |
---|
203 | struct termios *term, |
---|
204 | rtems_libio_open_close_args_t *args |
---|
205 | ) |
---|
206 | { |
---|
207 | imx_uart_context *ctx; |
---|
208 | volatile imx_uart *regs; |
---|
209 | #ifdef CONSOLE_USE_INTERRUPTS |
---|
210 | rtems_status_code sc; |
---|
211 | uint32_t ufcr; |
---|
212 | #endif |
---|
213 | |
---|
214 | ctx = (imx_uart_context *) base; |
---|
215 | regs = imx_uart_get_regs(&ctx->base); |
---|
216 | |
---|
217 | regs->ucr1 = IMX_UART_UCR1_UARTEN; |
---|
218 | regs->ucr2 = IMX_UART_UCR2_IRTS | IMX_UART_UCR2_WS | IMX_UART_UCR2_RXEN |
---|
219 | | IMX_UART_UCR2_TXEN | IMX_UART_UCR2_SRST; |
---|
220 | |
---|
221 | rtems_termios_set_initial_baud(tty, 115200); |
---|
222 | imx_uart_set_attributes(base, term); |
---|
223 | |
---|
224 | #ifdef CONSOLE_USE_INTERRUPTS |
---|
225 | ufcr = regs->ufcr; |
---|
226 | ufcr = IMX_UART_UFCR_RXTL_SET(ufcr, 16); |
---|
227 | ufcr = IMX_UART_UFCR_TXTL_SET(ufcr, 16); |
---|
228 | regs->ufcr = ufcr; |
---|
229 | regs->ucr1 |= IMX_UART_UCR1_RRDYEN; |
---|
230 | regs->ucr2 |= IMX_UART_UCR2_ATEN; |
---|
231 | sc = rtems_interrupt_handler_install( |
---|
232 | ctx->irq, |
---|
233 | "UART", |
---|
234 | RTEMS_INTERRUPT_SHARED, |
---|
235 | imx_uart_interrupt, |
---|
236 | tty |
---|
237 | ); |
---|
238 | if (sc != RTEMS_SUCCESSFUL) { |
---|
239 | return false; |
---|
240 | } |
---|
241 | #endif |
---|
242 | |
---|
243 | return true; |
---|
244 | } |
---|
245 | |
---|
246 | static void imx_uart_last_close( |
---|
247 | rtems_termios_tty *tty, |
---|
248 | rtems_termios_device_context *base, |
---|
249 | rtems_libio_open_close_args_t *args |
---|
250 | ) |
---|
251 | { |
---|
252 | #ifdef CONSOLE_USE_INTERRUPTS |
---|
253 | imx_uart_context *ctx; |
---|
254 | |
---|
255 | ctx = (imx_uart_context *) base; |
---|
256 | rtems_interrupt_handler_remove(ctx->irq, imx_uart_interrupt, tty); |
---|
257 | #endif |
---|
258 | } |
---|
259 | |
---|
260 | static void imx_uart_write( |
---|
261 | rtems_termios_device_context *base, |
---|
262 | const char *buf, |
---|
263 | size_t len |
---|
264 | ) |
---|
265 | { |
---|
266 | #ifdef CONSOLE_USE_INTERRUPTS |
---|
267 | imx_uart_context *ctx; |
---|
268 | volatile imx_uart *regs; |
---|
269 | uint32_t ucr1; |
---|
270 | |
---|
271 | ctx = (imx_uart_context *) base; |
---|
272 | regs = imx_uart_get_regs(&ctx->base); |
---|
273 | ucr1 = regs->ucr1; |
---|
274 | |
---|
275 | if (len > 0) { |
---|
276 | ctx->transmitting = true; |
---|
277 | regs->utxd = IMX_UART_UTXD_TX_DATA(buf[0]); |
---|
278 | ucr1 |= IMX_UART_UCR1_TRDYEN; |
---|
279 | } else { |
---|
280 | ctx->transmitting = false; |
---|
281 | ucr1 &= ~IMX_UART_UCR1_TRDYEN; |
---|
282 | } |
---|
283 | |
---|
284 | regs->ucr1 = ucr1; |
---|
285 | #else |
---|
286 | size_t i; |
---|
287 | |
---|
288 | for (i = 0; i < len; ++i) { |
---|
289 | imx_uart_write_polled(base, buf[i]); |
---|
290 | } |
---|
291 | #endif |
---|
292 | } |
---|
293 | |
---|
294 | #ifndef CONSOLE_USE_INTERRUPTS |
---|
295 | static int imx_uart_read(rtems_termios_device_context *base) |
---|
296 | { |
---|
297 | volatile imx_uart *regs; |
---|
298 | |
---|
299 | regs = imx_uart_get_regs(base); |
---|
300 | |
---|
301 | if ((regs->usr2 & IMX_UART_USR2_RDR) != 0) { |
---|
302 | return IMX_UART_URXD_RX_DATA_GET(regs->urxd); |
---|
303 | } else { |
---|
304 | return -1; |
---|
305 | } |
---|
306 | } |
---|
307 | #endif |
---|
308 | |
---|
309 | static const rtems_termios_device_handler imx_uart_handler = { |
---|
310 | .first_open = imx_uart_first_open, |
---|
311 | .last_close = imx_uart_last_close, |
---|
312 | .write = imx_uart_write, |
---|
313 | .set_attributes = imx_uart_set_attributes, |
---|
314 | #ifdef CONSOLE_USE_INTERRUPTS |
---|
315 | .mode = TERMIOS_IRQ_DRIVEN |
---|
316 | #else |
---|
317 | .poll_read = imx_uart_read, |
---|
318 | .mode = TERMIOS_POLLED |
---|
319 | #endif |
---|
320 | }; |
---|
321 | |
---|
322 | rtems_status_code console_initialize( |
---|
323 | rtems_device_major_number major, |
---|
324 | rtems_device_minor_number minor, |
---|
325 | void *arg |
---|
326 | ) |
---|
327 | { |
---|
328 | char path[] = "/dev/ttyS?"; |
---|
329 | size_t i; |
---|
330 | |
---|
331 | rtems_termios_initialize(); |
---|
332 | |
---|
333 | for (i = 0; i < RTEMS_ARRAY_SIZE(imx_uart_instances); ++i) { |
---|
334 | imx_uart_context *ctx; |
---|
335 | |
---|
336 | ctx = &imx_uart_instances[i]; |
---|
337 | path[sizeof(path) - 2] = (char) ('0' + i); |
---|
338 | |
---|
339 | rtems_termios_device_install( |
---|
340 | path, |
---|
341 | &imx_uart_handler, |
---|
342 | NULL, |
---|
343 | &ctx->base |
---|
344 | ); |
---|
345 | |
---|
346 | if (ctx == imx_uart_console) { |
---|
347 | link(path, CONSOLE_DEVICE_NAME); |
---|
348 | } |
---|
349 | } |
---|
350 | |
---|
351 | return RTEMS_SUCCESSFUL; |
---|
352 | } |
---|
353 | |
---|
354 | RTEMS_SYSINIT_ITEM( |
---|
355 | imx_uart_probe, |
---|
356 | RTEMS_SYSINIT_BSP_START, |
---|
357 | RTEMS_SYSINIT_ORDER_LAST |
---|
358 | ); |
---|