1 // SPDX-License-Identifier: GPL-2.0+
4 * Rob Taylor, Flying Pig Systems. robt@flyingpig.com.
8 #include <linux/compiler.h>
17 #ifndef CONFIG_NS16550_MIN_FUNCTIONS
19 DECLARE_GLOBAL_DATA_PTR;
21 #if !defined(CONFIG_CONS_INDEX)
22 #elif (CONFIG_CONS_INDEX < 1) || (CONFIG_CONS_INDEX > 6)
23 #error "Invalid console index value."
26 #if CONFIG_CONS_INDEX == 1 && !defined(CONFIG_SYS_NS16550_COM1)
27 #error "Console port 1 defined but not configured."
28 #elif CONFIG_CONS_INDEX == 2 && !defined(CONFIG_SYS_NS16550_COM2)
29 #error "Console port 2 defined but not configured."
30 #elif CONFIG_CONS_INDEX == 3 && !defined(CONFIG_SYS_NS16550_COM3)
31 #error "Console port 3 defined but not configured."
32 #elif CONFIG_CONS_INDEX == 4 && !defined(CONFIG_SYS_NS16550_COM4)
33 #error "Console port 4 defined but not configured."
34 #elif CONFIG_CONS_INDEX == 5 && !defined(CONFIG_SYS_NS16550_COM5)
35 #error "Console port 5 defined but not configured."
36 #elif CONFIG_CONS_INDEX == 6 && !defined(CONFIG_SYS_NS16550_COM6)
37 #error "Console port 6 defined but not configured."
40 /* Note: The port number specified in the functions is 1 based.
41 * the array is 0 based.
43 static NS16550_t serial_ports[6] = {
44 #ifdef CONFIG_SYS_NS16550_COM1
45 (NS16550_t)CONFIG_SYS_NS16550_COM1,
49 #ifdef CONFIG_SYS_NS16550_COM2
50 (NS16550_t)CONFIG_SYS_NS16550_COM2,
54 #ifdef CONFIG_SYS_NS16550_COM3
55 (NS16550_t)CONFIG_SYS_NS16550_COM3,
59 #ifdef CONFIG_SYS_NS16550_COM4
60 (NS16550_t)CONFIG_SYS_NS16550_COM4,
64 #ifdef CONFIG_SYS_NS16550_COM5
65 (NS16550_t)CONFIG_SYS_NS16550_COM5,
69 #ifdef CONFIG_SYS_NS16550_COM6
70 (NS16550_t)CONFIG_SYS_NS16550_COM6
76 #define PORT serial_ports[port-1]
78 /* Multi serial device functions */
79 #define DECLARE_ESERIAL_FUNCTIONS(port) \
80 static int eserial##port##_init(void) \
83 clock_divisor = ns16550_calc_divisor(serial_ports[port-1], \
84 CONFIG_SYS_NS16550_CLK, gd->baudrate); \
85 NS16550_init(serial_ports[port-1], clock_divisor); \
88 static void eserial##port##_setbrg(void) \
90 serial_setbrg_dev(port); \
92 static int eserial##port##_getc(void) \
94 return serial_getc_dev(port); \
96 static int eserial##port##_tstc(void) \
98 return serial_tstc_dev(port); \
100 static void eserial##port##_putc(const char c) \
102 serial_putc_dev(port, c); \
104 static void eserial##port##_puts(const char *s) \
106 serial_puts_dev(port, s); \
109 /* Serial device descriptor */
110 #define INIT_ESERIAL_STRUCTURE(port, __name) { \
112 .start = eserial##port##_init, \
114 .setbrg = eserial##port##_setbrg, \
115 .getc = eserial##port##_getc, \
116 .tstc = eserial##port##_tstc, \
117 .putc = eserial##port##_putc, \
118 .puts = eserial##port##_puts, \
121 static void _serial_putc(const char c, const int port)
124 NS16550_putc(PORT, '\r');
126 NS16550_putc(PORT, c);
129 static void _serial_puts(const char *s, const int port)
132 _serial_putc(*s++, port);
136 static int _serial_getc(const int port)
138 return NS16550_getc(PORT);
141 static int _serial_tstc(const int port)
143 return NS16550_tstc(PORT);
146 static void _serial_setbrg(const int port)
150 clock_divisor = ns16550_calc_divisor(PORT, CONFIG_SYS_NS16550_CLK,
152 NS16550_reinit(PORT, clock_divisor);
156 serial_putc_dev(unsigned int dev_index,const char c)
158 _serial_putc(c,dev_index);
162 serial_puts_dev(unsigned int dev_index,const char *s)
164 _serial_puts(s,dev_index);
168 serial_getc_dev(unsigned int dev_index)
170 return _serial_getc(dev_index);
174 serial_tstc_dev(unsigned int dev_index)
176 return _serial_tstc(dev_index);
180 serial_setbrg_dev(unsigned int dev_index)
182 _serial_setbrg(dev_index);
185 #if defined(CONFIG_SYS_NS16550_COM1)
186 DECLARE_ESERIAL_FUNCTIONS(1);
187 struct serial_device eserial1_device =
188 INIT_ESERIAL_STRUCTURE(1, "eserial0");
190 #if defined(CONFIG_SYS_NS16550_COM2)
191 DECLARE_ESERIAL_FUNCTIONS(2);
192 struct serial_device eserial2_device =
193 INIT_ESERIAL_STRUCTURE(2, "eserial1");
195 #if defined(CONFIG_SYS_NS16550_COM3)
196 DECLARE_ESERIAL_FUNCTIONS(3);
197 struct serial_device eserial3_device =
198 INIT_ESERIAL_STRUCTURE(3, "eserial2");
200 #if defined(CONFIG_SYS_NS16550_COM4)
201 DECLARE_ESERIAL_FUNCTIONS(4);
202 struct serial_device eserial4_device =
203 INIT_ESERIAL_STRUCTURE(4, "eserial3");
205 #if defined(CONFIG_SYS_NS16550_COM5)
206 DECLARE_ESERIAL_FUNCTIONS(5);
207 struct serial_device eserial5_device =
208 INIT_ESERIAL_STRUCTURE(5, "eserial4");
210 #if defined(CONFIG_SYS_NS16550_COM6)
211 DECLARE_ESERIAL_FUNCTIONS(6);
212 struct serial_device eserial6_device =
213 INIT_ESERIAL_STRUCTURE(6, "eserial5");
216 __weak struct serial_device *default_serial_console(void)
218 #if CONFIG_CONS_INDEX == 1
219 return &eserial1_device;
220 #elif CONFIG_CONS_INDEX == 2
221 return &eserial2_device;
222 #elif CONFIG_CONS_INDEX == 3
223 return &eserial3_device;
224 #elif CONFIG_CONS_INDEX == 4
225 return &eserial4_device;
226 #elif CONFIG_CONS_INDEX == 5
227 return &eserial5_device;
228 #elif CONFIG_CONS_INDEX == 6
229 return &eserial6_device;
231 #error "Bad CONFIG_CONS_INDEX."
235 void ns16550_serial_initialize(void)
237 #if defined(CONFIG_SYS_NS16550_COM1)
238 serial_register(&eserial1_device);
240 #if defined(CONFIG_SYS_NS16550_COM2)
241 serial_register(&eserial2_device);
243 #if defined(CONFIG_SYS_NS16550_COM3)
244 serial_register(&eserial3_device);
246 #if defined(CONFIG_SYS_NS16550_COM4)
247 serial_register(&eserial4_device);
249 #if defined(CONFIG_SYS_NS16550_COM5)
250 serial_register(&eserial5_device);
252 #if defined(CONFIG_SYS_NS16550_COM6)
253 serial_register(&eserial6_device);
257 #endif /* !CONFIG_NS16550_MIN_FUNCTIONS */