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

5
Last change on this file since ce28d601 was ce28d601, checked in by Sebastian Huber <sebastian.huber@…>, on 09/28/17 at 08:03:44

bsp/imx: Add imx_get_reg_of_node()

Update #3090.

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