source: rtems/bsps/arm/raspberrypi/console/console-config.c @ f53473f

Last change on this file since f53473f was f53473f, checked in by G S Niteesh <gsnb.gn@…>, on Feb 9, 2020 at 7:21:44 PM

bsp/raspberrypi: Mini UART driver

This patch adds driver for Mini UART present in Raspberry Pi 3
and above, this UART is currently used as the primary UART in
these models.
The Mini UART is similar to ns16550, this driver is built
upon libchip/ns16550.

  • Property mode set to 100644
File size: 5.3 KB
Line 
1/**
2 * @file
3 *
4 * @ingroup raspberrypi_usart
5 *
6 * @brief Console Configuration.
7 */
8
9/*
10 * Copyright (c) 2015 Yang Qiao
11 * based on work by:
12 * Copyright (c) 2013 Alan Cudmore
13 *
14 *  The license and distribution terms for this file may be
15 *  found in the file LICENSE in this distribution or at
16 *
17 *  http://www.rtems.org/license/LICENSE
18 *
19 */
20
21#include <rtems/bspIo.h>
22#include <rtems/console.h>
23#include <rtems/sysinit.h>
24
25#include <libchip/serial.h>
26#include <libfdt.h>
27#include <libchip/ns16550.h>
28
29#include <bspopts.h>
30#include <bsp/usart.h>
31#include <bsp/raspberrypi.h>
32#include <bsp/fbcons.h>
33#include <bsp.h>
34#include <bsp/arm-pl011.h>
35#include <bsp/console-termios.h>
36#include <bsp/fdt.h>
37#include <bsp/fatal.h>
38#include <bsp/gpio.h>
39#include <bsp/rpi-gpio.h>
40
41/**
42 * UART0 - PL011
43 * UART1 - Mini UART
44 */
45#define PL011     "/dev/ttyAMA0"
46#define MINIUART  "/dev/ttyS0"
47#define FBCONS    "/dev/fbcons"
48
49arm_pl011_context pl011_context;
50ns16550_context mini_uart_context;
51
52rpi_fb_context fb_context;
53
54static void output_char_pl011(char c)
55{
56  arm_pl011_write_polled(&pl011_context.base, c);
57}
58
59static void output_char_mini_uart(char c)
60{
61  ns16550_polled_putchar(&mini_uart_context.base, c);
62}
63
64void output_char_fb(char c)
65{
66  fbcons_write_polled(&fb_context.base, c);
67}
68
69static uint8_t mini_uart_get_reg(uintptr_t port, uint8_t index)
70{
71  volatile uint32_t *val = (volatile uint32_t *)port + index;
72  return (uint8_t) *val;
73}
74
75static void mini_uart_set_reg(uintptr_t port, uint8_t index, uint8_t val)
76{
77  volatile uint32_t *reg = (volatile uint32_t *)port + index;
78  *reg = val;
79}
80
81static void init_ctx_arm_pl011(
82  const void *fdt,
83  int node
84)
85{
86  arm_pl011_context *ctx = &pl011_context;
87  rtems_termios_device_context_initialize(&ctx->base, "PL011UART");
88  ctx->regs = raspberrypi_get_reg_of_node(fdt, node);
89}
90
91static uint32_t calculate_baud_divisor(
92  ns16550_context *ctx,
93  uint32_t baud
94)
95{
96  uint32_t baudDivisor = (ctx->clock / (8 * baud)) - 1;
97  return baudDivisor;
98}
99
100static void init_ctx_mini_uart(
101  const void *fdt,
102  int node
103)
104{
105  const char *status;
106  int len;
107  ns16550_context *ctx;
108
109  memset(&mini_uart_context, 0, sizeof(mini_uart_context));
110  ctx = &mini_uart_context;
111
112  rtems_termios_device_context_initialize(&ctx->base, "MiniUART");
113
114  status = fdt_getprop(fdt, node, "status", &len);
115  if ( status == NULL || strcmp(status, "disabled" ) == 0){
116    return ;
117  }
118
119  ctx->port = (uintptr_t) raspberrypi_get_reg_of_node(fdt, node);
120  ctx->initial_baud = MINI_UART_DEFAULT_BAUD;
121  ctx->clock = BCM2835_CLOCK_FREQ;
122  ctx->calculate_baud_divisor = calculate_baud_divisor;
123  ctx->get_reg = mini_uart_get_reg;
124  ctx->set_reg = mini_uart_set_reg;
125
126  rtems_gpio_bsp_select_specific_io(0, 14, RPI_ALT_FUNC_5, NULL);
127  rtems_gpio_bsp_select_specific_io(0, 15, RPI_ALT_FUNC_5, NULL);
128  rtems_gpio_bsp_set_resistor_mode(0, 14, NO_PULL_RESISTOR);
129  rtems_gpio_bsp_set_resistor_mode(0, 15, NO_PULL_RESISTOR);
130
131  BCM2835_REG(AUX_ENABLES) |= 0x1;
132  ns16550_probe(&ctx->base);
133}
134
135static void register_fb( void )
136{
137  if (fbcons_probe(&fb_context.base) == true) {
138    rtems_termios_device_install(
139      FBCONS,
140      &fbcons_fns,
141      NULL,
142      &fb_context.base);
143  }
144}
145
146static void console_select( void )
147{
148  const char *opt;
149
150  opt = rpi_cmdline_get_arg("--console=");
151
152  if ( opt ) {
153    if ( strncmp( opt, "fbcons", sizeof( "fbcons" ) - 1 ) == 0 ) {
154      if ( rpi_video_is_initialized() > 0 ) {
155        BSP_output_char = output_char_fb;
156        link(FBCONS, CONSOLE_DEVICE_NAME);
157        return ;
158      }
159    } else if ( strncmp( opt, MINIUART, sizeof(MINIUART) - 1 ) == 0) {
160      BSP_output_char = output_char_mini_uart;
161      link(MINIUART, CONSOLE_DEVICE_NAME);
162    } else if ( strncmp( opt, PL011, sizeof(PL011) - 1 ) == 0) {
163      BSP_output_char = output_char_pl011;
164      link(PL011, CONSOLE_DEVICE_NAME);
165    }
166  }else {
167    /**
168     * If no command line option was given, default to PL011.
169     */
170    BSP_output_char = output_char_pl011;
171    link(PL011, CONSOLE_DEVICE_NAME);
172  }
173}
174
175static void uart_probe(void)
176{
177  static bool initialized = false;
178  const void *fdt;
179  const char *console;
180  int len;
181  int node;
182
183  if ( initialized ) {
184    return ;
185  }
186
187  fdt = bsp_fdt_get();
188
189  node = fdt_node_offset_by_compatible(fdt, -1, "brcm,bcm2835-pl011");
190  init_ctx_arm_pl011(fdt, node);
191
192  node = fdt_node_offset_by_compatible(fdt, -1, "brcm,bcm2835-aux-uart");
193  init_ctx_mini_uart(fdt, node);
194
195  node = fdt_path_offset(fdt, "/aliases");
196  console = fdt_getprop(fdt, node, "serial0", &len);
197
198  if ( strcmp(console, "/soc/serial@7e215040" ) == 0) {
199    BSP_output_char = output_char_mini_uart;
200  }else {
201    BSP_output_char = output_char_pl011;
202  }
203
204  initialized = true;
205}
206
207static void output_char(char c)
208{
209  uart_probe();
210  (*BSP_output_char)(c);
211}
212
213rtems_status_code console_initialize(
214  rtems_device_major_number major,
215  rtems_device_minor_number minor,
216  void *arg
217)
218{
219  rtems_termios_initialize();
220
221  uart_probe();
222  rtems_termios_device_install(
223    PL011,
224    &arm_pl011_fns,
225    NULL,
226    &pl011_context.base
227  );
228
229  rtems_termios_device_install(
230    MINIUART,
231    &ns16550_handler_polled,
232    NULL,
233    &mini_uart_context.base
234  );
235
236  register_fb();
237
238  console_select();
239
240  return RTEMS_SUCCESSFUL;
241}
242
243BSP_output_char_function_type BSP_output_char = output_char;
244
245BSP_polling_getchar_function_type BSP_poll_char = NULL;
246
247RTEMS_SYSINIT_ITEM(
248  uart_probe,
249  RTEMS_SYSINIT_BSP_START,
250  RTEMS_SYSINIT_ORDER_LAST_BUT_5
251);
Note: See TracBrowser for help on using the repository browser.