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