1 // SPDX-License-Identifier: GPL-2.0+
4 * Rob Taylor, Flying Pig Systems. robt@flyingpig.com.
10 #include <linux/compiler.h>
12 #ifndef CONFIG_NS16550_MIN_FUNCTIONS
14 DECLARE_GLOBAL_DATA_PTR;
16 #if !defined(CONFIG_CONS_INDEX)
17 #elif (CONFIG_CONS_INDEX < 1) || (CONFIG_CONS_INDEX > 6)
18 #error "Invalid console index value."
21 #if CONFIG_CONS_INDEX == 1 && !defined(CONFIG_SYS_NS16550_COM1)
22 #error "Console port 1 defined but not configured."
23 #elif CONFIG_CONS_INDEX == 2 && !defined(CONFIG_SYS_NS16550_COM2)
24 #error "Console port 2 defined but not configured."
25 #elif CONFIG_CONS_INDEX == 3 && !defined(CONFIG_SYS_NS16550_COM3)
26 #error "Console port 3 defined but not configured."
27 #elif CONFIG_CONS_INDEX == 4 && !defined(CONFIG_SYS_NS16550_COM4)
28 #error "Console port 4 defined but not configured."
29 #elif CONFIG_CONS_INDEX == 5 && !defined(CONFIG_SYS_NS16550_COM5)
30 #error "Console port 5 defined but not configured."
31 #elif CONFIG_CONS_INDEX == 6 && !defined(CONFIG_SYS_NS16550_COM6)
32 #error "Console port 6 defined but not configured."
35 /* Note: The port number specified in the functions is 1 based.
36 * the array is 0 based.
38 static NS16550_t serial_ports[6] = {
39 #ifdef CONFIG_SYS_NS16550_COM1
40 (NS16550_t)CONFIG_SYS_NS16550_COM1,
44 #ifdef CONFIG_SYS_NS16550_COM2
45 (NS16550_t)CONFIG_SYS_NS16550_COM2,
49 #ifdef CONFIG_SYS_NS16550_COM3
50 (NS16550_t)CONFIG_SYS_NS16550_COM3,
54 #ifdef CONFIG_SYS_NS16550_COM4
55 (NS16550_t)CONFIG_SYS_NS16550_COM4,
59 #ifdef CONFIG_SYS_NS16550_COM5
60 (NS16550_t)CONFIG_SYS_NS16550_COM5,
64 #ifdef CONFIG_SYS_NS16550_COM6
65 (NS16550_t)CONFIG_SYS_NS16550_COM6
71 #define PORT serial_ports[port-1]
73 /* Multi serial device functions */
74 #define DECLARE_ESERIAL_FUNCTIONS(port) \
75 static int eserial##port##_init(void) \
78 clock_divisor = ns16550_calc_divisor(serial_ports[port-1], \
79 CONFIG_SYS_NS16550_CLK, gd->baudrate); \
80 NS16550_init(serial_ports[port-1], clock_divisor); \
83 static void eserial##port##_setbrg(void) \
85 serial_setbrg_dev(port); \
87 static int eserial##port##_getc(void) \
89 return serial_getc_dev(port); \
91 static int eserial##port##_tstc(void) \
93 return serial_tstc_dev(port); \
95 static void eserial##port##_putc(const char c) \
97 serial_putc_dev(port, c); \
99 static void eserial##port##_puts(const char *s) \
101 serial_puts_dev(port, s); \
104 /* Serial device descriptor */
105 #define INIT_ESERIAL_STRUCTURE(port, __name) { \
107 .start = eserial##port##_init, \
109 .setbrg = eserial##port##_setbrg, \
110 .getc = eserial##port##_getc, \
111 .tstc = eserial##port##_tstc, \
112 .putc = eserial##port##_putc, \
113 .puts = eserial##port##_puts, \
116 static void _serial_putc(const char c, const int port)
119 NS16550_putc(PORT, '\r');
121 NS16550_putc(PORT, c);
124 static void _serial_puts(const char *s, const int port)
127 _serial_putc(*s++, port);
131 static int _serial_getc(const int port)
133 return NS16550_getc(PORT);
136 static int _serial_tstc(const int port)
138 return NS16550_tstc(PORT);
141 static void _serial_setbrg(const int port)
145 clock_divisor = ns16550_calc_divisor(PORT, CONFIG_SYS_NS16550_CLK,
147 NS16550_reinit(PORT, clock_divisor);
151 serial_putc_dev(unsigned int dev_index,const char c)
153 _serial_putc(c,dev_index);
157 serial_puts_dev(unsigned int dev_index,const char *s)
159 _serial_puts(s,dev_index);
163 serial_getc_dev(unsigned int dev_index)
165 return _serial_getc(dev_index);
169 serial_tstc_dev(unsigned int dev_index)
171 return _serial_tstc(dev_index);
175 serial_setbrg_dev(unsigned int dev_index)
177 _serial_setbrg(dev_index);
180 #if defined(CONFIG_SYS_NS16550_COM1)
181 DECLARE_ESERIAL_FUNCTIONS(1);
182 struct serial_device eserial1_device =
183 INIT_ESERIAL_STRUCTURE(1, "eserial0");
185 #if defined(CONFIG_SYS_NS16550_COM2)
186 DECLARE_ESERIAL_FUNCTIONS(2);
187 struct serial_device eserial2_device =
188 INIT_ESERIAL_STRUCTURE(2, "eserial1");
190 #if defined(CONFIG_SYS_NS16550_COM3)
191 DECLARE_ESERIAL_FUNCTIONS(3);
192 struct serial_device eserial3_device =
193 INIT_ESERIAL_STRUCTURE(3, "eserial2");
195 #if defined(CONFIG_SYS_NS16550_COM4)
196 DECLARE_ESERIAL_FUNCTIONS(4);
197 struct serial_device eserial4_device =
198 INIT_ESERIAL_STRUCTURE(4, "eserial3");
200 #if defined(CONFIG_SYS_NS16550_COM5)
201 DECLARE_ESERIAL_FUNCTIONS(5);
202 struct serial_device eserial5_device =
203 INIT_ESERIAL_STRUCTURE(5, "eserial4");
205 #if defined(CONFIG_SYS_NS16550_COM6)
206 DECLARE_ESERIAL_FUNCTIONS(6);
207 struct serial_device eserial6_device =
208 INIT_ESERIAL_STRUCTURE(6, "eserial5");
211 __weak struct serial_device *default_serial_console(void)
213 #if CONFIG_CONS_INDEX == 1
214 return &eserial1_device;
215 #elif CONFIG_CONS_INDEX == 2
216 return &eserial2_device;
217 #elif CONFIG_CONS_INDEX == 3
218 return &eserial3_device;
219 #elif CONFIG_CONS_INDEX == 4
220 return &eserial4_device;
221 #elif CONFIG_CONS_INDEX == 5
222 return &eserial5_device;
223 #elif CONFIG_CONS_INDEX == 6
224 return &eserial6_device;
226 #error "Bad CONFIG_CONS_INDEX."
230 void ns16550_serial_initialize(void)
232 #if defined(CONFIG_SYS_NS16550_COM1)
233 serial_register(&eserial1_device);
235 #if defined(CONFIG_SYS_NS16550_COM2)
236 serial_register(&eserial2_device);
238 #if defined(CONFIG_SYS_NS16550_COM3)
239 serial_register(&eserial3_device);
241 #if defined(CONFIG_SYS_NS16550_COM4)
242 serial_register(&eserial4_device);
244 #if defined(CONFIG_SYS_NS16550_COM5)
245 serial_register(&eserial5_device);
247 #if defined(CONFIG_SYS_NS16550_COM6)
248 serial_register(&eserial6_device);
252 #endif /* !CONFIG_NS16550_MIN_FUNCTIONS */