source: rtems/c/src/lib/libbsp/arm/imx/console/console-config.c @ 7e195e66

5
Last change on this file since 7e195e66 was 7e195e66, checked in by Sebastian Huber <sebastian.huber@…>, on 09/28/17 at 07:29:38

bsp/imx: Add imx_get_irq_of_node()

Update #3090.

  • Property mode set to 100644
File size: 7.1 KB
Line 
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
28typedef 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
37static imx_uart_context imx_uart_instances[7];
38
39static imx_uart_context *imx_uart_console = &imx_uart_instances[0];
40
41static 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
49static 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
62static void imx_output_char(char c)
63{
64  imx_uart_write_polled(&imx_uart_console->base, c);
65}
66
67static void imx_uart_init_context(
68  imx_uart_context *ctx,
69  const char *fdt,
70  const char *serial
71)
72{
73  int node;
74  int len;
75  const uint32_t *val;
76
77  rtems_termios_device_context_initialize(&ctx->base, "UART");
78
79  node = fdt_path_offset(fdt, serial);
80
81  val = fdt_getprop(fdt, node, "reg", &len);
82  if (val != NULL && len >= 4) {
83    ctx->regs = (imx_uart *) fdt32_to_cpu(val[0]);
84  }
85
86#ifdef CONSOLE_USE_INTERRUPTS
87  ctx->irq = imx_get_irq_of_node(fdt, node, 0);
88#endif
89}
90
91static void imx_uart_probe(void)
92{
93  const void *fdt;
94  int node;
95  int offset;
96  const char *console;
97  size_t i;
98
99  fdt = bsp_fdt_get();
100  node = fdt_path_offset(fdt, "/chosen");
101
102  console = fdt_getprop(fdt, node, "stdout-path", NULL);
103  if (console == NULL) {
104    console = "";
105  }
106
107  node = fdt_path_offset(fdt, "/aliases");
108  offset = fdt_first_property_offset(fdt, node);
109  i = 0;
110
111  while (offset >= 0 && i < RTEMS_ARRAY_SIZE(imx_uart_instances)) {
112    const struct fdt_property *prop;
113
114    prop = fdt_get_property_by_offset(fdt, offset, NULL);
115
116    if (prop != NULL) {
117      const char *name;
118
119      name = fdt_string(fdt, fdt32_to_cpu(prop->nameoff));
120      if (strstr(name, "serial") != NULL) {
121        imx_uart_context *ctx;
122        const char *serial;
123
124        ctx = &imx_uart_instances[i];
125        serial = prop->data;
126
127        if (strcmp(serial, console) == 0) {
128          imx_uart_console = ctx;
129        }
130
131        imx_uart_init_context(ctx, fdt, serial);
132        ++i;
133      }
134    }
135
136    offset = fdt_next_property_offset(fdt, offset);
137  }
138
139  BSP_output_char = imx_output_char;
140}
141
142static void imx_output_char_init(char c)
143{
144  imx_uart_probe();
145  imx_output_char(c);
146}
147
148BSP_output_char_function_type BSP_output_char = imx_output_char_init;
149
150BSP_polling_getchar_function_type BSP_poll_char = NULL;
151
152#ifdef CONSOLE_USE_INTERRUPTS
153static void imx_uart_interrupt(void *arg)
154{
155  rtems_termios_tty *tty;
156  imx_uart_context *ctx;
157  volatile imx_uart *regs;
158  uint32_t usr2;
159
160  tty = arg;
161  ctx = rtems_termios_get_device_context(tty);
162  regs = ctx->regs;
163  usr2 = regs->usr2;
164
165  regs->usr1 = IMX_UART_USR1_AGTIM;
166
167  while ((usr2 & IMX_UART_USR2_RDR) != 0) {
168    char c;
169
170    c = IMX_UART_URXD_RX_DATA_GET(regs->urxd);
171    rtems_termios_enqueue_raw_characters(tty, &c, 1);
172    usr2 = regs->usr2;
173  }
174
175  if (ctx->transmitting && (regs->usr1 & IMX_UART_USR1_TRDY) != 0) {
176    rtems_termios_dequeue_characters(tty, 1);
177  }
178}
179#endif
180
181static bool imx_uart_set_attributes(
182  rtems_termios_device_context *base,
183  const struct termios *term
184)
185{
186  return true;
187}
188
189static bool imx_uart_first_open(
190  rtems_termios_tty *tty,
191  rtems_termios_device_context *base,
192  struct termios *term,
193  rtems_libio_open_close_args_t *args
194)
195{
196  imx_uart_context *ctx;
197  volatile imx_uart *regs;
198#ifdef CONSOLE_USE_INTERRUPTS
199  rtems_status_code sc;
200  uint32_t ufcr;
201#endif
202
203  ctx = (imx_uart_context *) base;
204  regs = imx_uart_get_regs(&ctx->base);
205
206  regs->ucr1 = IMX_UART_UCR1_UARTEN;
207  regs->ucr2 = IMX_UART_UCR2_IRTS | IMX_UART_UCR2_WS | IMX_UART_UCR2_RXEN
208    | IMX_UART_UCR2_TXEN | IMX_UART_UCR2_SRST;
209
210  rtems_termios_set_initial_baud(tty, 115200);
211  imx_uart_set_attributes(base, term);
212
213#ifdef CONSOLE_USE_INTERRUPTS
214  ufcr = regs->ufcr;
215  ufcr = IMX_UART_UFCR_RXTL_SET(ufcr, 16);
216  ufcr = IMX_UART_UFCR_TXTL_SET(ufcr, 16);
217  regs->ufcr = ufcr;
218  regs->ucr1 |= IMX_UART_UCR1_RRDYEN;
219  regs->ucr2 |= IMX_UART_UCR2_ATEN;
220  sc = rtems_interrupt_handler_install(
221    ctx->irq,
222    "UART",
223    RTEMS_INTERRUPT_SHARED,
224    imx_uart_interrupt,
225    tty
226  );
227  if (sc != RTEMS_SUCCESSFUL) {
228    return false;
229  }
230#endif
231
232  return true;
233}
234
235static void imx_uart_last_close(
236  rtems_termios_tty *tty,
237  rtems_termios_device_context *base,
238  rtems_libio_open_close_args_t *args
239)
240{
241#ifdef CONSOLE_USE_INTERRUPTS
242  imx_uart_context *ctx;
243
244  ctx = (imx_uart_context *) base;
245  rtems_interrupt_handler_remove(ctx->irq, imx_uart_interrupt, tty);
246#endif
247}
248
249static void imx_uart_write(
250  rtems_termios_device_context *base,
251  const char *buf,
252  size_t len
253)
254{
255#ifdef CONSOLE_USE_INTERRUPTS
256  imx_uart_context *ctx;
257  volatile imx_uart *regs;
258  uint32_t ucr1;
259
260  ctx = (imx_uart_context *) base;
261  regs = imx_uart_get_regs(&ctx->base);
262  ucr1 = regs->ucr1;
263
264  if (len > 0) {
265    ctx->transmitting = true;
266    regs->utxd = IMX_UART_UTXD_TX_DATA(buf[0]);
267    ucr1 |= IMX_UART_UCR1_TRDYEN;
268  } else {
269    ctx->transmitting = false;
270    ucr1 &= ~IMX_UART_UCR1_TRDYEN;
271  }
272
273  regs->ucr1 = ucr1;
274#else
275  size_t i;
276
277  for (i = 0; i < len; ++i) {
278    imx_uart_write_polled(base, buf[i]);
279  }
280#endif
281}
282
283#ifndef CONSOLE_USE_INTERRUPTS
284static int imx_uart_read(rtems_termios_device_context *base)
285{
286  volatile imx_uart *regs;
287
288  regs = imx_uart_get_regs(base);
289
290  if ((regs->usr2 & IMX_UART_USR2_RDR) != 0) {
291    return IMX_UART_URXD_RX_DATA_GET(regs->urxd);
292  } else {
293    return -1;
294  }
295}
296#endif
297
298static const rtems_termios_device_handler imx_uart_handler = {
299  .first_open = imx_uart_first_open,
300  .last_close = imx_uart_last_close,
301  .write = imx_uart_write,
302  .set_attributes = imx_uart_set_attributes,
303#ifdef CONSOLE_USE_INTERRUPTS
304  .mode = TERMIOS_IRQ_DRIVEN
305#else
306  .poll_read = imx_uart_read,
307  .mode = TERMIOS_POLLED
308#endif
309};
310
311rtems_status_code console_initialize(
312  rtems_device_major_number major,
313  rtems_device_minor_number minor,
314  void *arg
315)
316{
317  char path[] = "/dev/ttyS?";
318  size_t i;
319
320  rtems_termios_initialize();
321
322  for (i = 0; i < RTEMS_ARRAY_SIZE(imx_uart_instances); ++i) {
323    imx_uart_context *ctx;
324
325    ctx = &imx_uart_instances[i];
326    path[sizeof(path) - 2] = (char) ('0' + i);
327
328    rtems_termios_device_install(
329      path,
330      &imx_uart_handler,
331      NULL,
332      &ctx->base
333    );
334
335    if (ctx == imx_uart_console) {
336      link(path, CONSOLE_DEVICE_NAME);
337    }
338  }
339
340  return RTEMS_SUCCESSFUL;
341}
342
343RTEMS_SYSINIT_ITEM(
344  imx_uart_probe,
345  RTEMS_SYSINIT_BSP_START,
346  RTEMS_SYSINIT_ORDER_LAST
347);
Note: See TracBrowser for help on using the repository browser.