1 // SPDX-License-Identifier: GPL-2.0+
4 * Rob Taylor, Flying Pig Systems. robt@flyingpig.com.
8 #include <clock_legacy.h>
11 #include <asm/global_data.h>
12 #include <linux/compiler.h>
14 #ifndef CONFIG_NS16550_MIN_FUNCTIONS
16 DECLARE_GLOBAL_DATA_PTR;
18 #if !defined(CONFIG_CONS_INDEX)
19 #elif (CONFIG_CONS_INDEX < 1) || (CONFIG_CONS_INDEX > 6)
20 #error "Invalid console index value."
23 #if CONFIG_CONS_INDEX == 1 && !defined(CONFIG_SYS_NS16550_COM1)
24 #error "Console port 1 defined but not configured."
25 #elif CONFIG_CONS_INDEX == 2 && !defined(CONFIG_SYS_NS16550_COM2)
26 #error "Console port 2 defined but not configured."
27 #elif CONFIG_CONS_INDEX == 3 && !defined(CONFIG_SYS_NS16550_COM3)
28 #error "Console port 3 defined but not configured."
29 #elif CONFIG_CONS_INDEX == 4 && !defined(CONFIG_SYS_NS16550_COM4)
30 #error "Console port 4 defined but not configured."
31 #elif CONFIG_CONS_INDEX == 5 && !defined(CONFIG_SYS_NS16550_COM5)
32 #error "Console port 5 defined but not configured."
33 #elif CONFIG_CONS_INDEX == 6 && !defined(CONFIG_SYS_NS16550_COM6)
34 #error "Console port 6 defined but not configured."
37 /* Note: The port number specified in the functions is 1 based.
38 * the array is 0 based.
40 static struct ns16550 *serial_ports[6] = {
41 #ifdef CONFIG_SYS_NS16550_COM1
42 (struct ns16550 *)CONFIG_SYS_NS16550_COM1,
46 #ifdef CONFIG_SYS_NS16550_COM2
47 (struct ns16550 *)CONFIG_SYS_NS16550_COM2,
51 #ifdef CONFIG_SYS_NS16550_COM3
52 (struct ns16550 *)CONFIG_SYS_NS16550_COM3,
56 #ifdef CONFIG_SYS_NS16550_COM4
57 (struct ns16550 *)CONFIG_SYS_NS16550_COM4,
61 #ifdef CONFIG_SYS_NS16550_COM5
62 (struct ns16550 *)CONFIG_SYS_NS16550_COM5,
66 #ifdef CONFIG_SYS_NS16550_COM6
67 (struct ns16550 *)CONFIG_SYS_NS16550_COM6
73 #define PORT serial_ports[port-1]
75 /* Multi serial device functions */
76 #define DECLARE_ESERIAL_FUNCTIONS(port) \
77 static int eserial##port##_init(void) \
80 clock_divisor = ns16550_calc_divisor(serial_ports[port-1], \
81 CONFIG_SYS_NS16550_CLK, gd->baudrate); \
82 ns16550_init(serial_ports[port - 1], clock_divisor); \
85 static void eserial##port##_setbrg(void) \
87 serial_setbrg_dev(port); \
89 static int eserial##port##_getc(void) \
91 return serial_getc_dev(port); \
93 static int eserial##port##_tstc(void) \
95 return serial_tstc_dev(port); \
97 static void eserial##port##_putc(const char c) \
99 serial_putc_dev(port, c); \
101 static void eserial##port##_puts(const char *s) \
103 serial_puts_dev(port, s); \
106 /* Serial device descriptor */
107 #define INIT_ESERIAL_STRUCTURE(port, __name) { \
109 .start = eserial##port##_init, \
111 .setbrg = eserial##port##_setbrg, \
112 .getc = eserial##port##_getc, \
113 .tstc = eserial##port##_tstc, \
114 .putc = eserial##port##_putc, \
115 .puts = eserial##port##_puts, \
118 static void _serial_putc(const char c, const int port)
121 ns16550_putc(PORT, '\r');
123 ns16550_putc(PORT, c);
126 static void _serial_puts(const char *s, const int port)
129 _serial_putc(*s++, port);
133 static int _serial_getc(const int port)
135 return ns16550_getc(PORT);
138 static int _serial_tstc(const int port)
140 return ns16550_tstc(PORT);
143 static void _serial_setbrg(const int port)
147 clock_divisor = ns16550_calc_divisor(PORT, CONFIG_SYS_NS16550_CLK,
149 ns16550_reinit(PORT, clock_divisor);
153 serial_putc_dev(unsigned int dev_index,const char c)
155 _serial_putc(c,dev_index);
159 serial_puts_dev(unsigned int dev_index,const char *s)
161 _serial_puts(s,dev_index);
165 serial_getc_dev(unsigned int dev_index)
167 return _serial_getc(dev_index);
171 serial_tstc_dev(unsigned int dev_index)
173 return _serial_tstc(dev_index);
177 serial_setbrg_dev(unsigned int dev_index)
179 _serial_setbrg(dev_index);
182 #if defined(CONFIG_SYS_NS16550_COM1)
183 DECLARE_ESERIAL_FUNCTIONS(1);
184 struct serial_device eserial1_device =
185 INIT_ESERIAL_STRUCTURE(1, "eserial0");
187 #if defined(CONFIG_SYS_NS16550_COM2)
188 DECLARE_ESERIAL_FUNCTIONS(2);
189 struct serial_device eserial2_device =
190 INIT_ESERIAL_STRUCTURE(2, "eserial1");
192 #if defined(CONFIG_SYS_NS16550_COM3)
193 DECLARE_ESERIAL_FUNCTIONS(3);
194 struct serial_device eserial3_device =
195 INIT_ESERIAL_STRUCTURE(3, "eserial2");
197 #if defined(CONFIG_SYS_NS16550_COM4)
198 DECLARE_ESERIAL_FUNCTIONS(4);
199 struct serial_device eserial4_device =
200 INIT_ESERIAL_STRUCTURE(4, "eserial3");
202 #if defined(CONFIG_SYS_NS16550_COM5)
203 DECLARE_ESERIAL_FUNCTIONS(5);
204 struct serial_device eserial5_device =
205 INIT_ESERIAL_STRUCTURE(5, "eserial4");
207 #if defined(CONFIG_SYS_NS16550_COM6)
208 DECLARE_ESERIAL_FUNCTIONS(6);
209 struct serial_device eserial6_device =
210 INIT_ESERIAL_STRUCTURE(6, "eserial5");
213 __weak struct serial_device *default_serial_console(void)
215 #if CONFIG_CONS_INDEX == 1
216 return &eserial1_device;
217 #elif CONFIG_CONS_INDEX == 2
218 return &eserial2_device;
219 #elif CONFIG_CONS_INDEX == 3
220 return &eserial3_device;
221 #elif CONFIG_CONS_INDEX == 4
222 return &eserial4_device;
223 #elif CONFIG_CONS_INDEX == 5
224 return &eserial5_device;
225 #elif CONFIG_CONS_INDEX == 6
226 return &eserial6_device;
228 #error "Bad CONFIG_CONS_INDEX."
232 void ns16550_serial_initialize(void)
234 #if defined(CONFIG_SYS_NS16550_COM1)
235 serial_register(&eserial1_device);
237 #if defined(CONFIG_SYS_NS16550_COM2)
238 serial_register(&eserial2_device);
240 #if defined(CONFIG_SYS_NS16550_COM3)
241 serial_register(&eserial3_device);
243 #if defined(CONFIG_SYS_NS16550_COM4)
244 serial_register(&eserial4_device);
246 #if defined(CONFIG_SYS_NS16550_COM5)
247 serial_register(&eserial5_device);
249 #if defined(CONFIG_SYS_NS16550_COM6)
250 serial_register(&eserial6_device);
254 #endif /* !CONFIG_NS16550_MIN_FUNCTIONS */