summaryrefslogtreecommitdiffstats
path: root/bsps/arm/imx/console/console-config.c
blob: a65d7bff9f686c487173ff77b7d226e4f42b86f8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
/*
 * Copyright (c) 2017 embedded brains GmbH.  All rights reserved.
 *
 * The license and distribution terms for this file may be
 * found in the file LICENSE in this distribution or at
 * http://www.rtems.org/license/LICENSE.
 */

#include <sys/param.h>

#include <rtems/bspIo.h>
#include <rtems/console.h>
#include <rtems/sysinit.h>
#include <rtems/termiostypes.h>

#include <bsp.h>
#include <bsp/fdt.h>
#include <bsp/irq.h>

#include <arm/freescale/imx/imx_ccmvar.h>
#include <arm/freescale/imx/imx_uartreg.h>

#include <libfdt.h>

#define IMX_UART_TX_FIFO_LEVEL 16

typedef struct {
  rtems_termios_device_context base;
  volatile imx_uart *regs;
#ifdef CONSOLE_USE_INTERRUPTS
  rtems_vector_number irq;
  int tx_in_progress;
#endif
} imx_uart_context;

static imx_uart_context imx_uart_instances[7];

static imx_uart_context *imx_uart_console = &imx_uart_instances[0];

static volatile imx_uart *imx_uart_get_regs(rtems_termios_device_context *base)
{
  imx_uart_context *ctx;

  ctx = (imx_uart_context *) base;
  return ctx->regs;
}

static void imx_uart_write_polled(rtems_termios_device_context *base, char c)
{
  volatile imx_uart *regs;

  regs = imx_uart_get_regs(base);

  while ((regs->usr1 & IMX_UART_USR1_TRDY) == 0) {
    /* Wait */
  }

  regs->utxd = IMX_UART_UTXD_TX_DATA(c);
}

static int imx_uart_read_polled(rtems_termios_device_context *base)
{
  volatile imx_uart *regs;

  regs = imx_uart_get_regs(base);

  if ((regs->usr2 & IMX_UART_USR2_RDR) != 0) {
    return IMX_UART_URXD_RX_DATA_GET(regs->urxd);
  } else {
    return -1;
  }
}

void imx_uart_console_drain(void)
{
  volatile imx_uart *regs;

  regs = imx_uart_get_regs(&imx_uart_console->base);

  if (regs != NULL) {
    while ((regs->usr2 & IMX_UART_USR2_TXFE) == 0) {
      /* Wait */
    }
  }
}

static void imx_output_char(char c)
{
  imx_uart_write_polled(&imx_uart_console->base, c);
}

static int imx_poll_char(void)
{
  return imx_uart_read_polled(&imx_uart_console->base);
}

static void imx_uart_init_context(
  imx_uart_context *ctx,
  const char *fdt,
  const char *serial
)
{
  int node;

  rtems_termios_device_context_initialize(&ctx->base, "UART");
  node = fdt_path_offset(fdt, serial);
  ctx->regs = imx_get_reg_of_node(fdt, node);
#ifdef CONSOLE_USE_INTERRUPTS
  ctx->irq = imx_get_irq_of_node(fdt, node, 0);
#endif
}

static void imx_uart_probe(void)
{
  const void *fdt;
  int node;
  int offset;
  const char *console;
  size_t i;

  fdt = bsp_fdt_get();
  node = fdt_path_offset(fdt, "/chosen");

  console = fdt_getprop(fdt, node, "stdout-path", NULL);
  if (console == NULL) {
    console = "";
  }

  node = fdt_path_offset(fdt, "/aliases");
  offset = fdt_first_property_offset(fdt, node);
  i = 0;

  while (offset >= 0 && i < RTEMS_ARRAY_SIZE(imx_uart_instances)) {
    const struct fdt_property *prop;

    prop = fdt_get_property_by_offset(fdt, offset, NULL);

    if (prop != NULL) {
      const char *name;

      name = fdt_string(fdt, fdt32_to_cpu(prop->nameoff));
      if (strstr(name, "serial") != NULL) {
        imx_uart_context *ctx;
        const char *serial;

        ctx = &imx_uart_instances[i];
        serial = prop->data;

        if (strcmp(serial, console) == 0) {
          imx_uart_console = ctx;
        }

        imx_uart_init_context(ctx, fdt, serial);
        ++i;
      }
    }

    offset = fdt_next_property_offset(fdt, offset);
  }

  BSP_output_char = imx_output_char;
  BSP_poll_char = imx_poll_char;
}

static void imx_output_char_init(char c)
{
  imx_uart_probe();
  imx_output_char(c);
}

BSP_output_char_function_type BSP_output_char = imx_output_char_init;

BSP_polling_getchar_function_type BSP_poll_char = NULL;

#ifdef CONSOLE_USE_INTERRUPTS
static void imx_uart_interrupt(void *arg)
{
  rtems_termios_tty *tty;
  imx_uart_context *ctx;
  volatile imx_uart *regs;
  uint32_t usr2;

  tty = arg;
  ctx = rtems_termios_get_device_context(tty);
  regs = ctx->regs;
  usr2 = regs->usr2;

  regs->usr1 = IMX_UART_USR1_AGTIM;

  while ((usr2 & IMX_UART_USR2_RDR) != 0) {
    char c;

    c = IMX_UART_URXD_RX_DATA_GET(regs->urxd);
    rtems_termios_enqueue_raw_characters(tty, &c, 1);
    usr2 = regs->usr2;
  }

  if (ctx->tx_in_progress > 0 && (regs->usr1 & IMX_UART_USR1_TRDY) != 0) {
    rtems_termios_dequeue_characters(tty, ctx->tx_in_progress);
  }
}
#endif

static bool imx_uart_set_attributes(
  rtems_termios_device_context *base,
  const struct termios *term
)
{
  imx_uart_context *ctx;
  volatile imx_uart *regs;
  uint32_t ufcr;
  uint32_t baud;

  ctx = (imx_uart_context *) base;
  regs = imx_uart_get_regs(&ctx->base);

  baud = rtems_termios_baud_to_number(term->c_ospeed);

  if (baud != 0) {
    ufcr = regs->ufcr;
    ufcr = IMX_UART_UFCR_RFDIV_SET(ufcr, 0x5);
    regs->ufcr = ufcr;
    regs->ubir = 15;
    regs->ubmr = imx_ccm_uart_hz() / baud - 1;
  }

  return true;
}

static bool imx_uart_first_open(
  rtems_termios_tty *tty,
  rtems_termios_device_context *base,
  struct termios *term,
  rtems_libio_open_close_args_t *args
)
{
  imx_uart_context *ctx;
  volatile imx_uart *regs;
#ifdef CONSOLE_USE_INTERRUPTS
  rtems_status_code sc;
  uint32_t ufcr;
#endif

  ctx = (imx_uart_context *) base;
  regs = imx_uart_get_regs(&ctx->base);

  regs->ucr1 = IMX_UART_UCR1_UARTEN;
  regs->ucr2 = IMX_UART_UCR2_IRTS | IMX_UART_UCR2_WS | IMX_UART_UCR2_RXEN
    | IMX_UART_UCR2_TXEN | IMX_UART_UCR2_SRST;
  regs->ucr3 |= IMX_UART_UCR3_ADNIMP | IMX_UART_UCR3_RXDMUXSEL;

  rtems_termios_set_initial_baud(tty, 115200);
  imx_uart_set_attributes(base, term);

#ifdef CONSOLE_USE_INTERRUPTS
  ufcr = regs->ufcr;
  ufcr = IMX_UART_UFCR_RXTL_SET(ufcr, 16);
  ufcr = IMX_UART_UFCR_TXTL_SET(ufcr, IMX_UART_TX_FIFO_LEVEL);
  regs->ufcr = ufcr;
  regs->ucr1 |= IMX_UART_UCR1_RRDYEN;
  regs->ucr2 |= IMX_UART_UCR2_ATEN;
  sc = rtems_interrupt_handler_install(
    ctx->irq,
    "UART",
    RTEMS_INTERRUPT_SHARED,
    imx_uart_interrupt,
    tty
  );
  if (sc != RTEMS_SUCCESSFUL) {
    return false;
  }
#endif

  return true;
}

static void imx_uart_last_close(
  rtems_termios_tty *tty,
  rtems_termios_device_context *base,
  rtems_libio_open_close_args_t *args
)
{
#ifdef CONSOLE_USE_INTERRUPTS
  imx_uart_context *ctx;

  ctx = (imx_uart_context *) base;
  rtems_interrupt_handler_remove(ctx->irq, imx_uart_interrupt, tty);
#endif
}

static void imx_uart_write(
  rtems_termios_device_context *base,
  const char *buf,
  size_t len
)
{
#ifdef CONSOLE_USE_INTERRUPTS
  imx_uart_context *ctx;
  volatile imx_uart *regs;
  int n;
  uint32_t ucr1;

  ctx = (imx_uart_context *) base;
  regs = imx_uart_get_regs(&ctx->base);
  ucr1 = regs->ucr1;

  if (len > 0) {
    int i;

    n = (int) MIN(len, IMX_UART_TX_FIFO_LEVEL);
    ucr1 |= IMX_UART_UCR1_TRDYEN;

    for (i = 0; i < n; ++i) {
      regs->utxd = IMX_UART_UTXD_TX_DATA(buf[i]);
    }
  } else {
    n = 0;
    ucr1 &= ~IMX_UART_UCR1_TRDYEN;
  }

  regs->ucr1 = ucr1;
  ctx->tx_in_progress = n;
#else
  size_t i;

  for (i = 0; i < len; ++i) {
    imx_uart_write_polled(base, buf[i]);
  }
#endif
}

static const rtems_termios_device_handler imx_uart_handler = {
  .first_open = imx_uart_first_open,
  .last_close = imx_uart_last_close,
  .write = imx_uart_write,
  .set_attributes = imx_uart_set_attributes,
#ifdef CONSOLE_USE_INTERRUPTS
  .mode = TERMIOS_IRQ_DRIVEN
#else
  .poll_read = imx_uart_read_polled,
  .mode = TERMIOS_POLLED
#endif
};

rtems_status_code console_initialize(
  rtems_device_major_number major,
  rtems_device_minor_number minor,
  void *arg
)
{
  char path[] = "/dev/ttyS?";
  size_t i;

  rtems_termios_initialize();

  for (i = 0; i < RTEMS_ARRAY_SIZE(imx_uart_instances); ++i) {
    imx_uart_context *ctx;

    ctx = &imx_uart_instances[i];
    path[sizeof(path) - 2] = (char) ('0' + i);

    rtems_termios_device_install(
      path,
      &imx_uart_handler,
      NULL,
      &ctx->base
    );

    if (ctx == imx_uart_console) {
      link(path, CONSOLE_DEVICE_NAME);
    }
  }

  return RTEMS_SUCCESSFUL;
}

RTEMS_SYSINIT_ITEM(
  imx_uart_probe,
  RTEMS_SYSINIT_BSP_START,
  RTEMS_SYSINIT_ORDER_LAST_BUT_5
);