Commit | Line | Data |
---|---|---|
83d290c5 | 1 | // SPDX-License-Identifier: GPL-2.0+ |
78f6622a WD |
2 | /* |
3 | * (C) Copyright 2000 | |
4 | * Rob Taylor, Flying Pig Systems. robt@flyingpig.com. | |
78f6622a WD |
5 | */ |
6 | ||
7 | #include <common.h> | |
6c768ca7 | 8 | #include <linux/compiler.h> |
78f6622a | 9 | |
78f6622a | 10 | #include <ns16550.h> |
55d6d2d3 | 11 | #ifdef CONFIG_NS87308 |
78f6622a WD |
12 | #include <ns87308.h> |
13 | #endif | |
14 | ||
0fd30252 | 15 | #include <serial.h> |
0fd30252 | 16 | |
48cbc3a8 SW |
17 | #ifndef CONFIG_NS16550_MIN_FUNCTIONS |
18 | ||
d87080b7 WD |
19 | DECLARE_GLOBAL_DATA_PTR; |
20 | ||
756f586a | 21 | #if !defined(CONFIG_CONS_INDEX) |
96708a06 | 22 | #elif (CONFIG_CONS_INDEX < 1) || (CONFIG_CONS_INDEX > 6) |
756f586a WD |
23 | #error "Invalid console index value." |
24 | #endif | |
25 | ||
6d0f6bcf | 26 | #if CONFIG_CONS_INDEX == 1 && !defined(CONFIG_SYS_NS16550_COM1) |
756f586a | 27 | #error "Console port 1 defined but not configured." |
6d0f6bcf | 28 | #elif CONFIG_CONS_INDEX == 2 && !defined(CONFIG_SYS_NS16550_COM2) |
756f586a | 29 | #error "Console port 2 defined but not configured." |
6d0f6bcf | 30 | #elif CONFIG_CONS_INDEX == 3 && !defined(CONFIG_SYS_NS16550_COM3) |
756f586a | 31 | #error "Console port 3 defined but not configured." |
6d0f6bcf | 32 | #elif CONFIG_CONS_INDEX == 4 && !defined(CONFIG_SYS_NS16550_COM4) |
756f586a | 33 | #error "Console port 4 defined but not configured." |
96708a06 AB |
34 | #elif CONFIG_CONS_INDEX == 5 && !defined(CONFIG_SYS_NS16550_COM5) |
35 | #error "Console port 5 defined but not configured." | |
36 | #elif CONFIG_CONS_INDEX == 6 && !defined(CONFIG_SYS_NS16550_COM6) | |
37 | #error "Console port 6 defined but not configured." | |
756f586a WD |
38 | #endif |
39 | ||
40 | /* Note: The port number specified in the functions is 1 based. | |
41 | * the array is 0 based. | |
42 | */ | |
96708a06 | 43 | static NS16550_t serial_ports[6] = { |
6d0f6bcf JCPV |
44 | #ifdef CONFIG_SYS_NS16550_COM1 |
45 | (NS16550_t)CONFIG_SYS_NS16550_COM1, | |
756f586a WD |
46 | #else |
47 | NULL, | |
48 | #endif | |
6d0f6bcf JCPV |
49 | #ifdef CONFIG_SYS_NS16550_COM2 |
50 | (NS16550_t)CONFIG_SYS_NS16550_COM2, | |
78f6622a | 51 | #else |
756f586a | 52 | NULL, |
78f6622a | 53 | #endif |
6d0f6bcf JCPV |
54 | #ifdef CONFIG_SYS_NS16550_COM3 |
55 | (NS16550_t)CONFIG_SYS_NS16550_COM3, | |
756f586a WD |
56 | #else |
57 | NULL, | |
58 | #endif | |
6d0f6bcf | 59 | #ifdef CONFIG_SYS_NS16550_COM4 |
96708a06 AB |
60 | (NS16550_t)CONFIG_SYS_NS16550_COM4, |
61 | #else | |
62 | NULL, | |
63 | #endif | |
64 | #ifdef CONFIG_SYS_NS16550_COM5 | |
65 | (NS16550_t)CONFIG_SYS_NS16550_COM5, | |
66 | #else | |
67 | NULL, | |
68 | #endif | |
69 | #ifdef CONFIG_SYS_NS16550_COM6 | |
70 | (NS16550_t)CONFIG_SYS_NS16550_COM6 | |
756f586a WD |
71 | #else |
72 | NULL | |
73 | #endif | |
74 | }; | |
75 | ||
76 | #define PORT serial_ports[port-1] | |
0fd30252 | 77 | |
0fd30252 WD |
78 | /* Multi serial device functions */ |
79 | #define DECLARE_ESERIAL_FUNCTIONS(port) \ | |
ac63f2a2 KP |
80 | static int eserial##port##_init(void) \ |
81 | { \ | |
82 | int clock_divisor; \ | |
fa54eb12 SG |
83 | clock_divisor = ns16550_calc_divisor(serial_ports[port-1], \ |
84 | CONFIG_SYS_NS16550_CLK, gd->baudrate); \ | |
ac63f2a2 KP |
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 | } | |
0fd30252 WD |
108 | |
109 | /* Serial device descriptor */ | |
90bad891 MV |
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 | } | |
0fd30252 | 120 | |
3fdd0bb2 | 121 | static void _serial_putc(const char c, const int port) |
78f6622a WD |
122 | { |
123 | if (c == '\n') | |
756f586a | 124 | NS16550_putc(PORT, '\r'); |
78f6622a | 125 | |
756f586a | 126 | NS16550_putc(PORT, c); |
78f6622a WD |
127 | } |
128 | ||
3fdd0bb2 | 129 | static void _serial_puts(const char *s, const int port) |
78f6622a WD |
130 | { |
131 | while (*s) { | |
3fdd0bb2 | 132 | _serial_putc(*s++, port); |
78f6622a WD |
133 | } |
134 | } | |
135 | ||
3fdd0bb2 | 136 | static int _serial_getc(const int port) |
78f6622a | 137 | { |
756f586a | 138 | return NS16550_getc(PORT); |
78f6622a WD |
139 | } |
140 | ||
3fdd0bb2 | 141 | static int _serial_tstc(const int port) |
78f6622a | 142 | { |
756f586a | 143 | return NS16550_tstc(PORT); |
78f6622a WD |
144 | } |
145 | ||
3fdd0bb2 | 146 | static void _serial_setbrg(const int port) |
78f6622a | 147 | { |
2e5983d2 | 148 | int clock_divisor; |
78f6622a | 149 | |
fa54eb12 SG |
150 | clock_divisor = ns16550_calc_divisor(PORT, CONFIG_SYS_NS16550_CLK, |
151 | gd->baudrate); | |
756f586a WD |
152 | NS16550_reinit(PORT, clock_divisor); |
153 | } | |
154 | ||
0fd30252 WD |
155 | static inline void |
156 | serial_putc_dev(unsigned int dev_index,const char c) | |
157 | { | |
158 | _serial_putc(c,dev_index); | |
159 | } | |
756f586a | 160 | |
0fd30252 WD |
161 | static inline void |
162 | serial_puts_dev(unsigned int dev_index,const char *s) | |
163 | { | |
164 | _serial_puts(s,dev_index); | |
165 | } | |
756f586a | 166 | |
0fd30252 WD |
167 | static inline int |
168 | serial_getc_dev(unsigned int dev_index) | |
169 | { | |
170 | return _serial_getc(dev_index); | |
171 | } | |
756f586a | 172 | |
0fd30252 WD |
173 | static inline int |
174 | serial_tstc_dev(unsigned int dev_index) | |
175 | { | |
176 | return _serial_tstc(dev_index); | |
177 | } | |
756f586a | 178 | |
0fd30252 WD |
179 | static inline void |
180 | serial_setbrg_dev(unsigned int dev_index) | |
181 | { | |
182 | _serial_setbrg(dev_index); | |
183 | } | |
0fd30252 | 184 | |
55db9cca | 185 | #if defined(CONFIG_SYS_NS16550_COM1) |
0fd30252 | 186 | DECLARE_ESERIAL_FUNCTIONS(1); |
511d0c72 | 187 | struct serial_device eserial1_device = |
1c9a5606 | 188 | INIT_ESERIAL_STRUCTURE(1, "eserial0"); |
55db9cca SW |
189 | #endif |
190 | #if defined(CONFIG_SYS_NS16550_COM2) | |
0fd30252 WD |
191 | DECLARE_ESERIAL_FUNCTIONS(2); |
192 | struct serial_device eserial2_device = | |
1c9a5606 | 193 | INIT_ESERIAL_STRUCTURE(2, "eserial1"); |
55db9cca SW |
194 | #endif |
195 | #if defined(CONFIG_SYS_NS16550_COM3) | |
0fd30252 WD |
196 | DECLARE_ESERIAL_FUNCTIONS(3); |
197 | struct serial_device eserial3_device = | |
1c9a5606 | 198 | INIT_ESERIAL_STRUCTURE(3, "eserial2"); |
55db9cca SW |
199 | #endif |
200 | #if defined(CONFIG_SYS_NS16550_COM4) | |
0fd30252 WD |
201 | DECLARE_ESERIAL_FUNCTIONS(4); |
202 | struct serial_device eserial4_device = | |
1c9a5606 | 203 | INIT_ESERIAL_STRUCTURE(4, "eserial3"); |
55db9cca SW |
204 | #endif |
205 | #if defined(CONFIG_SYS_NS16550_COM5) | |
96708a06 AB |
206 | DECLARE_ESERIAL_FUNCTIONS(5); |
207 | struct serial_device eserial5_device = | |
208 | INIT_ESERIAL_STRUCTURE(5, "eserial4"); | |
55db9cca SW |
209 | #endif |
210 | #if defined(CONFIG_SYS_NS16550_COM6) | |
96708a06 AB |
211 | DECLARE_ESERIAL_FUNCTIONS(6); |
212 | struct serial_device eserial6_device = | |
213 | INIT_ESERIAL_STRUCTURE(6, "eserial5"); | |
55db9cca | 214 | #endif |
6c768ca7 MF |
215 | |
216 | __weak struct serial_device *default_serial_console(void) | |
217 | { | |
218 | #if CONFIG_CONS_INDEX == 1 | |
219 | return &eserial1_device; | |
220 | #elif CONFIG_CONS_INDEX == 2 | |
221 | return &eserial2_device; | |
222 | #elif CONFIG_CONS_INDEX == 3 | |
223 | return &eserial3_device; | |
224 | #elif CONFIG_CONS_INDEX == 4 | |
225 | return &eserial4_device; | |
96708a06 AB |
226 | #elif CONFIG_CONS_INDEX == 5 |
227 | return &eserial5_device; | |
228 | #elif CONFIG_CONS_INDEX == 6 | |
229 | return &eserial6_device; | |
6c768ca7 MF |
230 | #else |
231 | #error "Bad CONFIG_CONS_INDEX." | |
232 | #endif | |
233 | } | |
234 | ||
abc0ed8d MV |
235 | void ns16550_serial_initialize(void) |
236 | { | |
237 | #if defined(CONFIG_SYS_NS16550_COM1) | |
238 | serial_register(&eserial1_device); | |
239 | #endif | |
240 | #if defined(CONFIG_SYS_NS16550_COM2) | |
241 | serial_register(&eserial2_device); | |
242 | #endif | |
243 | #if defined(CONFIG_SYS_NS16550_COM3) | |
244 | serial_register(&eserial3_device); | |
245 | #endif | |
246 | #if defined(CONFIG_SYS_NS16550_COM4) | |
247 | serial_register(&eserial4_device); | |
248 | #endif | |
96708a06 AB |
249 | #if defined(CONFIG_SYS_NS16550_COM5) |
250 | serial_register(&eserial5_device); | |
251 | #endif | |
252 | #if defined(CONFIG_SYS_NS16550_COM6) | |
253 | serial_register(&eserial6_device); | |
254 | #endif | |
abc0ed8d | 255 | } |
48cbc3a8 SW |
256 | |
257 | #endif /* !CONFIG_NS16550_MIN_FUNCTIONS */ |