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 | |
---|
28 | #include <bspopts.h> |
---|
29 | #include <bsp/usart.h> |
---|
30 | #include <bsp/raspberrypi.h> |
---|
31 | #include <bsp/fbcons.h> |
---|
32 | #include <bsp.h> |
---|
33 | #include <bsp/arm-pl011.h> |
---|
34 | #include <bsp/console-termios.h> |
---|
35 | #include <bsp/fdt.h> |
---|
36 | #include <bsp/fatal.h> |
---|
37 | |
---|
38 | |
---|
39 | #define UART0 "/dev/ttyS0" |
---|
40 | #define FBCONS "/dev/fbcons" |
---|
41 | |
---|
42 | arm_pl011_context pl011_context; |
---|
43 | |
---|
44 | rpi_fb_context fb_context; |
---|
45 | |
---|
46 | static void output_char_serial(char c) |
---|
47 | { |
---|
48 | arm_pl011_write_polled(&pl011_context.base, c); |
---|
49 | } |
---|
50 | |
---|
51 | void output_char_fb(char c) |
---|
52 | { |
---|
53 | fbcons_write_polled(&fb_context.base, c); |
---|
54 | } |
---|
55 | |
---|
56 | static void init_ctx_arm_pl011( |
---|
57 | const void *fdt, |
---|
58 | int node |
---|
59 | ) |
---|
60 | { |
---|
61 | arm_pl011_context *ctx = &pl011_context; |
---|
62 | rtems_termios_device_context_initialize(&ctx->base, "UART"); |
---|
63 | ctx->regs = raspberrypi_get_reg_of_node(fdt, node); |
---|
64 | } |
---|
65 | |
---|
66 | static void register_fb( void ) |
---|
67 | { |
---|
68 | if (fbcons_probe(&fb_context.base) == true) { |
---|
69 | rtems_termios_device_install( |
---|
70 | FBCONS, |
---|
71 | &fbcons_fns, |
---|
72 | NULL, |
---|
73 | &fb_context.base); |
---|
74 | } |
---|
75 | } |
---|
76 | |
---|
77 | static void console_select( void ) |
---|
78 | { |
---|
79 | const char *opt; |
---|
80 | |
---|
81 | opt = rpi_cmdline_get_arg("--console="); |
---|
82 | |
---|
83 | if ( opt ) { |
---|
84 | if ( strncmp( opt, "fbcons", sizeof( "fbcons" ) - 1 ) == 0 ) { |
---|
85 | if ( rpi_video_is_initialized() > 0 ) { |
---|
86 | BSP_output_char = output_char_fb; |
---|
87 | link(FBCONS, CONSOLE_DEVICE_NAME); |
---|
88 | return ; |
---|
89 | } |
---|
90 | } |
---|
91 | } |
---|
92 | BSP_output_char = output_char_serial; |
---|
93 | link(UART0, CONSOLE_DEVICE_NAME); |
---|
94 | } |
---|
95 | |
---|
96 | static void uart_probe(void) |
---|
97 | { |
---|
98 | static bool initialized = false; |
---|
99 | const void *fdt; |
---|
100 | int node; |
---|
101 | |
---|
102 | if ( initialized ) { |
---|
103 | return ; |
---|
104 | } |
---|
105 | |
---|
106 | fdt = bsp_fdt_get(); |
---|
107 | node = fdt_node_offset_by_compatible(fdt, -1, "brcm,bcm2835-pl011"); |
---|
108 | |
---|
109 | init_ctx_arm_pl011(fdt, node); |
---|
110 | |
---|
111 | initialized = true; |
---|
112 | } |
---|
113 | |
---|
114 | static void output_char(char c) |
---|
115 | { |
---|
116 | uart_probe(); |
---|
117 | output_char_serial(c); |
---|
118 | } |
---|
119 | |
---|
120 | rtems_status_code console_initialize( |
---|
121 | rtems_device_major_number major, |
---|
122 | rtems_device_minor_number minor, |
---|
123 | void *arg |
---|
124 | ) |
---|
125 | { |
---|
126 | rtems_termios_initialize(); |
---|
127 | |
---|
128 | uart_probe(); |
---|
129 | rtems_termios_device_install( |
---|
130 | UART0, |
---|
131 | &arm_pl011_fns, |
---|
132 | NULL, |
---|
133 | &pl011_context.base |
---|
134 | ); |
---|
135 | |
---|
136 | register_fb(); |
---|
137 | |
---|
138 | console_select(); |
---|
139 | |
---|
140 | return RTEMS_SUCCESSFUL; |
---|
141 | } |
---|
142 | |
---|
143 | BSP_output_char_function_type BSP_output_char = output_char; |
---|
144 | |
---|
145 | BSP_polling_getchar_function_type BSP_poll_char = NULL; |
---|
146 | |
---|
147 | RTEMS_SYSINIT_ITEM( |
---|
148 | uart_probe, |
---|
149 | RTEMS_SYSINIT_BSP_START, |
---|
150 | RTEMS_SYSINIT_ORDER_LAST_BUT_5 |
---|
151 | ); |
---|