]> Git Repo - J-u-boot.git/blame - drivers/serial/serial_ns16550.c
serial: fix circular rx buffer edge case
[J-u-boot.git] / drivers / serial / serial_ns16550.c
CommitLineData
83d290c5 1// SPDX-License-Identifier: GPL-2.0+
78f6622a
WD
2/*
3 * (C) Copyright 2000
4 * Rob Taylor, Flying Pig Systems. [email protected].
78f6622a
WD
5 */
6
03de305e 7#include <config.h>
d96c2604 8#include <clock_legacy.h>
78f6622a 9#include <ns16550.h>
0fd30252 10#include <serial.h>
401d1c4f 11#include <asm/global_data.h>
015e3348 12#include <linux/compiler.h>
0fd30252 13
57c3afbc 14#if !CONFIG_IS_ENABLED(NS16550_MIN_FUNCTIONS)
48cbc3a8 15
d87080b7
WD
16DECLARE_GLOBAL_DATA_PTR;
17
756f586a 18#if !defined(CONFIG_CONS_INDEX)
96708a06 19#elif (CONFIG_CONS_INDEX < 1) || (CONFIG_CONS_INDEX > 6)
756f586a
WD
20#error "Invalid console index value."
21#endif
22
91092132 23#if CONFIG_CONS_INDEX == 1 && !defined(CFG_SYS_NS16550_COM1)
756f586a 24#error "Console port 1 defined but not configured."
91092132 25#elif CONFIG_CONS_INDEX == 2 && !defined(CFG_SYS_NS16550_COM2)
756f586a 26#error "Console port 2 defined but not configured."
91092132 27#elif CONFIG_CONS_INDEX == 3 && !defined(CFG_SYS_NS16550_COM3)
756f586a 28#error "Console port 3 defined but not configured."
91092132 29#elif CONFIG_CONS_INDEX == 4 && !defined(CFG_SYS_NS16550_COM4)
756f586a 30#error "Console port 4 defined but not configured."
91092132 31#elif CONFIG_CONS_INDEX == 5 && !defined(CFG_SYS_NS16550_COM5)
96708a06 32#error "Console port 5 defined but not configured."
91092132 33#elif CONFIG_CONS_INDEX == 6 && !defined(CFG_SYS_NS16550_COM6)
96708a06 34#error "Console port 6 defined but not configured."
756f586a
WD
35#endif
36
37/* Note: The port number specified in the functions is 1 based.
38 * the array is 0 based.
39 */
d30c7209 40static struct ns16550 *serial_ports[6] = {
91092132
TR
41#ifdef CFG_SYS_NS16550_COM1
42 (struct ns16550 *)CFG_SYS_NS16550_COM1,
756f586a
WD
43#else
44 NULL,
45#endif
91092132
TR
46#ifdef CFG_SYS_NS16550_COM2
47 (struct ns16550 *)CFG_SYS_NS16550_COM2,
78f6622a 48#else
756f586a 49 NULL,
78f6622a 50#endif
91092132
TR
51#ifdef CFG_SYS_NS16550_COM3
52 (struct ns16550 *)CFG_SYS_NS16550_COM3,
756f586a
WD
53#else
54 NULL,
55#endif
91092132
TR
56#ifdef CFG_SYS_NS16550_COM4
57 (struct ns16550 *)CFG_SYS_NS16550_COM4,
96708a06
AB
58#else
59 NULL,
60#endif
91092132
TR
61#ifdef CFG_SYS_NS16550_COM5
62 (struct ns16550 *)CFG_SYS_NS16550_COM5,
96708a06
AB
63#else
64 NULL,
65#endif
91092132
TR
66#ifdef CFG_SYS_NS16550_COM6
67 (struct ns16550 *)CFG_SYS_NS16550_COM6
756f586a
WD
68#else
69 NULL
70#endif
71};
72
73#define PORT serial_ports[port-1]
0fd30252 74
0fd30252
WD
75/* Multi serial device functions */
76#define DECLARE_ESERIAL_FUNCTIONS(port) \
ac63f2a2
KP
77 static int eserial##port##_init(void) \
78 { \
79 int clock_divisor; \
fa54eb12 80 clock_divisor = ns16550_calc_divisor(serial_ports[port-1], \
91092132 81 CFG_SYS_NS16550_CLK, gd->baudrate); \
2d6bf754 82 ns16550_init(serial_ports[port - 1], clock_divisor); \
ac63f2a2
KP
83 return 0 ; \
84 } \
85 static void eserial##port##_setbrg(void) \
86 { \
87 serial_setbrg_dev(port); \
88 } \
89 static int eserial##port##_getc(void) \
90 { \
91 return serial_getc_dev(port); \
92 } \
93 static int eserial##port##_tstc(void) \
94 { \
95 return serial_tstc_dev(port); \
96 } \
97 static void eserial##port##_putc(const char c) \
98 { \
99 serial_putc_dev(port, c); \
100 } \
101 static void eserial##port##_puts(const char *s) \
102 { \
103 serial_puts_dev(port, s); \
104 }
0fd30252
WD
105
106/* Serial device descriptor */
90bad891
MV
107#define INIT_ESERIAL_STRUCTURE(port, __name) { \
108 .name = __name, \
109 .start = eserial##port##_init, \
110 .stop = NULL, \
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, \
116}
0fd30252 117
3fdd0bb2 118static void _serial_putc(const char c, const int port)
78f6622a
WD
119{
120 if (c == '\n')
2d6bf754 121 ns16550_putc(PORT, '\r');
78f6622a 122
2d6bf754 123 ns16550_putc(PORT, c);
78f6622a
WD
124}
125
3fdd0bb2 126static void _serial_puts(const char *s, const int port)
78f6622a
WD
127{
128 while (*s) {
3fdd0bb2 129 _serial_putc(*s++, port);
78f6622a
WD
130 }
131}
132
3fdd0bb2 133static int _serial_getc(const int port)
78f6622a 134{
2d6bf754 135 return ns16550_getc(PORT);
78f6622a
WD
136}
137
3fdd0bb2 138static int _serial_tstc(const int port)
78f6622a 139{
2d6bf754 140 return ns16550_tstc(PORT);
78f6622a
WD
141}
142
3fdd0bb2 143static void _serial_setbrg(const int port)
78f6622a 144{
2e5983d2 145 int clock_divisor;
78f6622a 146
91092132 147 clock_divisor = ns16550_calc_divisor(PORT, CFG_SYS_NS16550_CLK,
fa54eb12 148 gd->baudrate);
2d6bf754 149 ns16550_reinit(PORT, clock_divisor);
756f586a
WD
150}
151
0fd30252
WD
152static inline void
153serial_putc_dev(unsigned int dev_index,const char c)
154{
155 _serial_putc(c,dev_index);
156}
756f586a 157
0fd30252
WD
158static inline void
159serial_puts_dev(unsigned int dev_index,const char *s)
160{
161 _serial_puts(s,dev_index);
162}
756f586a 163
0fd30252
WD
164static inline int
165serial_getc_dev(unsigned int dev_index)
166{
167 return _serial_getc(dev_index);
168}
756f586a 169
0fd30252
WD
170static inline int
171serial_tstc_dev(unsigned int dev_index)
172{
173 return _serial_tstc(dev_index);
174}
756f586a 175
0fd30252
WD
176static inline void
177serial_setbrg_dev(unsigned int dev_index)
178{
179 _serial_setbrg(dev_index);
180}
0fd30252 181
91092132 182#if defined(CFG_SYS_NS16550_COM1)
0fd30252 183DECLARE_ESERIAL_FUNCTIONS(1);
511d0c72 184struct serial_device eserial1_device =
1c9a5606 185 INIT_ESERIAL_STRUCTURE(1, "eserial0");
55db9cca 186#endif
91092132 187#if defined(CFG_SYS_NS16550_COM2)
0fd30252
WD
188DECLARE_ESERIAL_FUNCTIONS(2);
189struct serial_device eserial2_device =
1c9a5606 190 INIT_ESERIAL_STRUCTURE(2, "eserial1");
55db9cca 191#endif
91092132 192#if defined(CFG_SYS_NS16550_COM3)
0fd30252
WD
193DECLARE_ESERIAL_FUNCTIONS(3);
194struct serial_device eserial3_device =
1c9a5606 195 INIT_ESERIAL_STRUCTURE(3, "eserial2");
55db9cca 196#endif
91092132 197#if defined(CFG_SYS_NS16550_COM4)
0fd30252
WD
198DECLARE_ESERIAL_FUNCTIONS(4);
199struct serial_device eserial4_device =
1c9a5606 200 INIT_ESERIAL_STRUCTURE(4, "eserial3");
55db9cca 201#endif
91092132 202#if defined(CFG_SYS_NS16550_COM5)
96708a06
AB
203DECLARE_ESERIAL_FUNCTIONS(5);
204struct serial_device eserial5_device =
205 INIT_ESERIAL_STRUCTURE(5, "eserial4");
55db9cca 206#endif
91092132 207#if defined(CFG_SYS_NS16550_COM6)
96708a06
AB
208DECLARE_ESERIAL_FUNCTIONS(6);
209struct serial_device eserial6_device =
210 INIT_ESERIAL_STRUCTURE(6, "eserial5");
55db9cca 211#endif
6c768ca7
MF
212
213__weak struct serial_device *default_serial_console(void)
214{
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;
96708a06
AB
223#elif CONFIG_CONS_INDEX == 5
224 return &eserial5_device;
225#elif CONFIG_CONS_INDEX == 6
226 return &eserial6_device;
6c768ca7
MF
227#else
228#error "Bad CONFIG_CONS_INDEX."
229#endif
230}
231
abc0ed8d
MV
232void ns16550_serial_initialize(void)
233{
91092132 234#if defined(CFG_SYS_NS16550_COM1)
abc0ed8d
MV
235 serial_register(&eserial1_device);
236#endif
91092132 237#if defined(CFG_SYS_NS16550_COM2)
abc0ed8d
MV
238 serial_register(&eserial2_device);
239#endif
91092132 240#if defined(CFG_SYS_NS16550_COM3)
abc0ed8d
MV
241 serial_register(&eserial3_device);
242#endif
91092132 243#if defined(CFG_SYS_NS16550_COM4)
abc0ed8d
MV
244 serial_register(&eserial4_device);
245#endif
91092132 246#if defined(CFG_SYS_NS16550_COM5)
96708a06
AB
247 serial_register(&eserial5_device);
248#endif
91092132 249#if defined(CFG_SYS_NS16550_COM6)
96708a06
AB
250 serial_register(&eserial6_device);
251#endif
abc0ed8d 252}
48cbc3a8 253
57c3afbc 254#endif /* !CONFIG_IS_ENABLED(NS16550_MIN_FUNCTIONS) */
This page took 0.767269 seconds and 4 git commands to generate.