a1069f92a6166440f64cf83125fa29aee56eaadd
[platform/kernel/u-boot.git] / drivers / serial / serial_ns16550.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2000
4  * Rob Taylor, Flying Pig Systems. robt@flyingpig.com.
5  */
6
7 #include <common.h>
8 #include <ns16550.h>
9 #include <serial.h>
10 #include <linux/compiler.h>
11
12 #ifndef CONFIG_NS16550_MIN_FUNCTIONS
13
14 DECLARE_GLOBAL_DATA_PTR;
15
16 #if !defined(CONFIG_CONS_INDEX)
17 #elif (CONFIG_CONS_INDEX < 1) || (CONFIG_CONS_INDEX > 6)
18 #error  "Invalid console index value."
19 #endif
20
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."
33 #endif
34
35 /* Note: The port number specified in the functions is 1 based.
36  *       the array is 0 based.
37  */
38 static NS16550_t serial_ports[6] = {
39 #ifdef CONFIG_SYS_NS16550_COM1
40         (NS16550_t)CONFIG_SYS_NS16550_COM1,
41 #else
42         NULL,
43 #endif
44 #ifdef CONFIG_SYS_NS16550_COM2
45         (NS16550_t)CONFIG_SYS_NS16550_COM2,
46 #else
47         NULL,
48 #endif
49 #ifdef CONFIG_SYS_NS16550_COM3
50         (NS16550_t)CONFIG_SYS_NS16550_COM3,
51 #else
52         NULL,
53 #endif
54 #ifdef CONFIG_SYS_NS16550_COM4
55         (NS16550_t)CONFIG_SYS_NS16550_COM4,
56 #else
57         NULL,
58 #endif
59 #ifdef CONFIG_SYS_NS16550_COM5
60         (NS16550_t)CONFIG_SYS_NS16550_COM5,
61 #else
62         NULL,
63 #endif
64 #ifdef CONFIG_SYS_NS16550_COM6
65         (NS16550_t)CONFIG_SYS_NS16550_COM6
66 #else
67         NULL
68 #endif
69 };
70
71 #define PORT    serial_ports[port-1]
72
73 /* Multi serial device functions */
74 #define DECLARE_ESERIAL_FUNCTIONS(port) \
75         static int  eserial##port##_init(void) \
76         { \
77                 int clock_divisor; \
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); \
81                 return 0 ; \
82         } \
83         static void eserial##port##_setbrg(void) \
84         { \
85                 serial_setbrg_dev(port); \
86         } \
87         static int  eserial##port##_getc(void) \
88         { \
89                 return serial_getc_dev(port); \
90         } \
91         static int  eserial##port##_tstc(void) \
92         { \
93                 return serial_tstc_dev(port); \
94         } \
95         static void eserial##port##_putc(const char c) \
96         { \
97                 serial_putc_dev(port, c); \
98         } \
99         static void eserial##port##_puts(const char *s) \
100         { \
101                 serial_puts_dev(port, s); \
102         }
103
104 /* Serial device descriptor */
105 #define INIT_ESERIAL_STRUCTURE(port, __name) {  \
106         .name   = __name,                       \
107         .start  = eserial##port##_init,         \
108         .stop   = NULL,                         \
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,         \
114 }
115
116 static void _serial_putc(const char c, const int port)
117 {
118         if (c == '\n')
119                 NS16550_putc(PORT, '\r');
120
121         NS16550_putc(PORT, c);
122 }
123
124 static void _serial_puts(const char *s, const int port)
125 {
126         while (*s) {
127                 _serial_putc(*s++, port);
128         }
129 }
130
131 static int _serial_getc(const int port)
132 {
133         return NS16550_getc(PORT);
134 }
135
136 static int _serial_tstc(const int port)
137 {
138         return NS16550_tstc(PORT);
139 }
140
141 static void _serial_setbrg(const int port)
142 {
143         int clock_divisor;
144
145         clock_divisor = ns16550_calc_divisor(PORT, CONFIG_SYS_NS16550_CLK,
146                                              gd->baudrate);
147         NS16550_reinit(PORT, clock_divisor);
148 }
149
150 static inline void
151 serial_putc_dev(unsigned int dev_index,const char c)
152 {
153         _serial_putc(c,dev_index);
154 }
155
156 static inline void
157 serial_puts_dev(unsigned int dev_index,const char *s)
158 {
159         _serial_puts(s,dev_index);
160 }
161
162 static inline int
163 serial_getc_dev(unsigned int dev_index)
164 {
165         return _serial_getc(dev_index);
166 }
167
168 static inline int
169 serial_tstc_dev(unsigned int dev_index)
170 {
171         return _serial_tstc(dev_index);
172 }
173
174 static inline void
175 serial_setbrg_dev(unsigned int dev_index)
176 {
177         _serial_setbrg(dev_index);
178 }
179
180 #if defined(CONFIG_SYS_NS16550_COM1)
181 DECLARE_ESERIAL_FUNCTIONS(1);
182 struct serial_device eserial1_device =
183         INIT_ESERIAL_STRUCTURE(1, "eserial0");
184 #endif
185 #if defined(CONFIG_SYS_NS16550_COM2)
186 DECLARE_ESERIAL_FUNCTIONS(2);
187 struct serial_device eserial2_device =
188         INIT_ESERIAL_STRUCTURE(2, "eserial1");
189 #endif
190 #if defined(CONFIG_SYS_NS16550_COM3)
191 DECLARE_ESERIAL_FUNCTIONS(3);
192 struct serial_device eserial3_device =
193         INIT_ESERIAL_STRUCTURE(3, "eserial2");
194 #endif
195 #if defined(CONFIG_SYS_NS16550_COM4)
196 DECLARE_ESERIAL_FUNCTIONS(4);
197 struct serial_device eserial4_device =
198         INIT_ESERIAL_STRUCTURE(4, "eserial3");
199 #endif
200 #if defined(CONFIG_SYS_NS16550_COM5)
201 DECLARE_ESERIAL_FUNCTIONS(5);
202 struct serial_device eserial5_device =
203         INIT_ESERIAL_STRUCTURE(5, "eserial4");
204 #endif
205 #if defined(CONFIG_SYS_NS16550_COM6)
206 DECLARE_ESERIAL_FUNCTIONS(6);
207 struct serial_device eserial6_device =
208         INIT_ESERIAL_STRUCTURE(6, "eserial5");
209 #endif
210
211 __weak struct serial_device *default_serial_console(void)
212 {
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;
225 #else
226 #error "Bad CONFIG_CONS_INDEX."
227 #endif
228 }
229
230 void ns16550_serial_initialize(void)
231 {
232 #if defined(CONFIG_SYS_NS16550_COM1)
233         serial_register(&eserial1_device);
234 #endif
235 #if defined(CONFIG_SYS_NS16550_COM2)
236         serial_register(&eserial2_device);
237 #endif
238 #if defined(CONFIG_SYS_NS16550_COM3)
239         serial_register(&eserial3_device);
240 #endif
241 #if defined(CONFIG_SYS_NS16550_COM4)
242         serial_register(&eserial4_device);
243 #endif
244 #if defined(CONFIG_SYS_NS16550_COM5)
245         serial_register(&eserial5_device);
246 #endif
247 #if defined(CONFIG_SYS_NS16550_COM6)
248         serial_register(&eserial6_device);
249 #endif
250 }
251
252 #endif /* !CONFIG_NS16550_MIN_FUNCTIONS */