source: rtems/c/src/lib/libbsp/arm/imx/console/console-config.c @ 4cfce5c

5
Last change on this file since 4cfce5c was 362e96ab, checked in by Sebastian Huber <sebastian.huber@…>, on 09/26/17 at 12:01:07

bsp/imx: Provide a default console

Update #3090.

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