]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - drivers/serial/serial.c
Unified codebase for TX28, TX48, TX51, TX53
[karo-tx-uboot.git] / drivers / serial / serial.c
1 /*
2  * (C) Copyright 2000
3  * Rob Taylor, Flying Pig Systems. robt@flyingpig.com.
4  *
5  * See file CREDITS for list of people who contributed to this
6  * project.
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License as
10  * published by the Free Software Foundation; either version 2 of
11  * the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21  * MA 02111-1307 USA
22  */
23
24 #include <common.h>
25 #include <linux/compiler.h>
26
27 #include <ns16550.h>
28 #ifdef CONFIG_NS87308
29 #include <ns87308.h>
30 #endif
31
32 #if defined (CONFIG_SERIAL_MULTI)
33 #include <serial.h>
34 #endif
35
36 DECLARE_GLOBAL_DATA_PTR;
37
38 #if !defined(CONFIG_CONS_INDEX)
39 #if defined (CONFIG_SERIAL_MULTI)
40 /*   with CONFIG_SERIAL_MULTI we might have no console
41  *  on these devices
42  */
43 #else
44 #error  "No console index specified."
45 #endif /* CONFIG_SERIAL_MULTI */
46 #elif (CONFIG_CONS_INDEX < 1) || (CONFIG_CONS_INDEX > 4)
47 #error  "Invalid console index value."
48 #endif
49
50 #if CONFIG_CONS_INDEX == 1 && !defined(CONFIG_SYS_NS16550_COM1)
51 #error  "Console port 1 defined but not configured."
52 #elif CONFIG_CONS_INDEX == 2 && !defined(CONFIG_SYS_NS16550_COM2)
53 #error  "Console port 2 defined but not configured."
54 #elif CONFIG_CONS_INDEX == 3 && !defined(CONFIG_SYS_NS16550_COM3)
55 #error  "Console port 3 defined but not configured."
56 #elif CONFIG_CONS_INDEX == 4 && !defined(CONFIG_SYS_NS16550_COM4)
57 #error  "Console port 4 defined but not configured."
58 #elif CONFIG_CONS_INDEX == 5 && !defined(CONFIG_SYS_NS16550_COM5)
59 #error  "Console port 5 defined but not configured."
60 #elif CONFIG_CONS_INDEX == 6 && !defined(CONFIG_SYS_NS16550_COM6)
61 #error  "Console port 6 defined but not configured."
62 #endif
63
64 /* Note: The port number specified in the functions is 1 based.
65  *       the array is 0 based.
66  */
67 static NS16550_t serial_ports[] = {
68 #ifdef CONFIG_SYS_NS16550_COM1
69         (NS16550_t)CONFIG_SYS_NS16550_COM1,
70 #else
71         NULL,
72 #endif
73 #ifdef CONFIG_SYS_NS16550_COM2
74         (NS16550_t)CONFIG_SYS_NS16550_COM2,
75 #else
76         NULL,
77 #endif
78 #ifdef CONFIG_SYS_NS16550_COM3
79         (NS16550_t)CONFIG_SYS_NS16550_COM3,
80 #else
81         NULL,
82 #endif
83 #ifdef CONFIG_SYS_NS16550_COM4
84         (NS16550_t)CONFIG_SYS_NS16550_COM4,
85 #else
86         NULL,
87 #endif
88 #ifdef CONFIG_SYS_NS16550_COM5
89         (NS16550_t)CONFIG_SYS_NS16550_COM5,
90 #else
91         NULL,
92 #endif
93 #ifdef CONFIG_SYS_NS16550_COM6
94         (NS16550_t)CONFIG_SYS_NS16550_COM6,
95 #else
96         NULL,
97 #endif
98 };
99
100 #define PORT    serial_ports[port-1]
101 #if defined(CONFIG_CONS_INDEX)
102 #define CONSOLE (serial_ports[CONFIG_CONS_INDEX-1])
103 #endif
104
105 #if defined(CONFIG_SERIAL_MULTI)
106
107 /* Multi serial device functions */
108 #define DECLARE_ESERIAL_FUNCTIONS(port) \
109     int  eserial##port##_init (void) {\
110         int clock_divisor; \
111         clock_divisor = calc_divisor(serial_ports[port-1]); \
112         NS16550_init(serial_ports[port-1], clock_divisor); \
113         return(0);}\
114     void eserial##port##_setbrg (void) {\
115         serial_setbrg_dev(port);}\
116     int  eserial##port##_getc (void) {\
117         return serial_getc_dev(port);}\
118     int  eserial##port##_tstc (void) {\
119         return serial_tstc_dev(port);}\
120     void eserial##port##_putc (const char c) {\
121         serial_putc_dev(port, c);}\
122     void eserial##port##_puts (const char *s) {\
123         serial_puts_dev(port, s);}
124
125 /* Serial device descriptor */
126 #define INIT_ESERIAL_STRUCTURE(port, name) {\
127         name,\
128         eserial##port##_init,\
129         NULL,\
130         eserial##port##_setbrg,\
131         eserial##port##_getc,\
132         eserial##port##_tstc,\
133         eserial##port##_putc,\
134         eserial##port##_puts, }
135
136 #endif /* CONFIG_SERIAL_MULTI */
137
138 static int calc_divisor (NS16550_t port)
139 {
140 #ifdef CONFIG_OMAP1510
141         /* If can't cleanly clock 115200 set div to 1 */
142         if ((CONFIG_SYS_NS16550_CLK == 12000000) && (gd->baudrate == 115200)) {
143                 port->osc_12m_sel = OSC_12M_SEL;        /* enable 6.5 * divisor */
144                 return (1);                             /* return 1 for base divisor */
145         }
146         port->osc_12m_sel = 0;                  /* clear if previsouly set */
147 #endif
148 #ifdef CONFIG_OMAP1610
149         /* If can't cleanly clock 115200 set div to 1 */
150         if ((CONFIG_SYS_NS16550_CLK == 48000000) && (gd->baudrate == 115200)) {
151                 return (26);            /* return 26 for base divisor */
152         }
153 #endif
154
155 #ifdef CONFIG_APTIX
156 #define MODE_X_DIV 13
157 #else
158 #define MODE_X_DIV 16
159 #endif
160
161         /* Compute divisor value. Normally, we should simply return:
162          *   CONFIG_SYS_NS16550_CLK) / MODE_X_DIV / gd->baudrate
163          * but we need to round that value by adding 0.5.
164          * Rounding is especially important at high baud rates.
165          */
166         return (CONFIG_SYS_NS16550_CLK + (gd->baudrate * (MODE_X_DIV / 2))) /
167                 (MODE_X_DIV * gd->baudrate);
168 }
169
170 #if !defined(CONFIG_SERIAL_MULTI)
171 int serial_init (void)
172 {
173         int clock_divisor;
174
175 #ifdef CONFIG_NS87308
176         initialise_ns87308();
177 #endif
178
179 #ifdef CONFIG_SYS_NS16550_COM1
180         clock_divisor = calc_divisor(serial_ports[0]);
181         NS16550_init(serial_ports[0], clock_divisor);
182 #endif
183 #ifdef CONFIG_SYS_NS16550_COM2
184         clock_divisor = calc_divisor(serial_ports[1]);
185         NS16550_init(serial_ports[1], clock_divisor);
186 #endif
187 #ifdef CONFIG_SYS_NS16550_COM3
188         clock_divisor = calc_divisor(serial_ports[2]);
189         NS16550_init(serial_ports[2], clock_divisor);
190 #endif
191 #ifdef CONFIG_SYS_NS16550_COM4
192         clock_divisor = calc_divisor(serial_ports[3]);
193         NS16550_init(serial_ports[3], clock_divisor);
194 #endif
195 #ifdef CONFIG_SYS_NS16550_COM5
196         clock_divisor = calc_divisor(serial_ports[4]);
197         NS16550_init(serial_ports[4], clock_divisor);
198 #endif
199 #ifdef CONFIG_SYS_NS16550_COM6
200         clock_divisor = calc_divisor(serial_ports[5]);
201         NS16550_init(serial_ports[5], clock_divisor);
202 #endif
203
204         return (0);
205 }
206 #endif
207
208 void
209 _serial_putc(const char c,const int port)
210 {
211         if (c == '\n')
212                 NS16550_putc(PORT, '\r');
213
214         NS16550_putc(PORT, c);
215 }
216
217 void
218 _serial_putc_raw(const char c,const int port)
219 {
220         NS16550_putc(PORT, c);
221 }
222
223 void
224 _serial_puts (const char *s,const int port)
225 {
226         while (*s) {
227                 _serial_putc (*s++,port);
228         }
229 }
230
231
232 int
233 _serial_getc(const int port)
234 {
235         return NS16550_getc(PORT);
236 }
237
238 int
239 _serial_tstc(const int port)
240 {
241         return NS16550_tstc(PORT);
242 }
243
244 void
245 _serial_setbrg (const int port)
246 {
247         int clock_divisor;
248
249         clock_divisor = calc_divisor(PORT);
250         NS16550_reinit(PORT, clock_divisor);
251 }
252
253 #if defined(CONFIG_SERIAL_MULTI)
254 static inline void
255 serial_putc_dev(unsigned int dev_index,const char c)
256 {
257         _serial_putc(c,dev_index);
258 }
259 #else
260 void
261 serial_putc(const char c)
262 {
263         _serial_putc(c,CONFIG_CONS_INDEX);
264 }
265 #endif
266
267 #if defined(CONFIG_SERIAL_MULTI)
268 static inline void
269 serial_putc_raw_dev(unsigned int dev_index,const char c)
270 {
271         _serial_putc_raw(c,dev_index);
272 }
273 #else
274 void
275 serial_putc_raw(const char c)
276 {
277         _serial_putc_raw(c,CONFIG_CONS_INDEX);
278 }
279 #endif
280
281 #if defined(CONFIG_SERIAL_MULTI)
282 static inline void
283 serial_puts_dev(unsigned int dev_index,const char *s)
284 {
285         _serial_puts(s,dev_index);
286 }
287 #else
288 void
289 serial_puts(const char *s)
290 {
291         _serial_puts(s,CONFIG_CONS_INDEX);
292 }
293 #endif
294
295 #if defined(CONFIG_SERIAL_MULTI)
296 static inline int
297 serial_getc_dev(unsigned int dev_index)
298 {
299         return _serial_getc(dev_index);
300 }
301 #else
302 int
303 serial_getc(void)
304 {
305         return _serial_getc(CONFIG_CONS_INDEX);
306 }
307 #endif
308
309 #if defined(CONFIG_SERIAL_MULTI)
310 static inline int
311 serial_tstc_dev(unsigned int dev_index)
312 {
313         return _serial_tstc(dev_index);
314 }
315 #else
316 int
317 serial_tstc(void)
318 {
319         return _serial_tstc(CONFIG_CONS_INDEX);
320 }
321 #endif
322
323 #if defined(CONFIG_SERIAL_MULTI)
324 static inline void
325 serial_setbrg_dev(unsigned int dev_index)
326 {
327         _serial_setbrg(dev_index);
328 }
329 #else
330 void
331 serial_setbrg(void)
332 {
333         _serial_setbrg(CONFIG_CONS_INDEX);
334 }
335 #endif
336
337 #if defined(CONFIG_SERIAL_MULTI)
338
339 DECLARE_ESERIAL_FUNCTIONS(1);
340 struct serial_device eserial1_device =
341         INIT_ESERIAL_STRUCTURE(1, "eserial0");
342 DECLARE_ESERIAL_FUNCTIONS(2);
343 struct serial_device eserial2_device =
344         INIT_ESERIAL_STRUCTURE(2, "eserial1");
345 DECLARE_ESERIAL_FUNCTIONS(3);
346 struct serial_device eserial3_device =
347         INIT_ESERIAL_STRUCTURE(3, "eserial2");
348 DECLARE_ESERIAL_FUNCTIONS(4);
349 struct serial_device eserial4_device =
350         INIT_ESERIAL_STRUCTURE(4, "eserial3");
351
352 __weak struct serial_device *default_serial_console(void)
353 {
354 #if CONFIG_CONS_INDEX == 1
355         return &eserial1_device;
356 #elif CONFIG_CONS_INDEX == 2
357         return &eserial2_device;
358 #elif CONFIG_CONS_INDEX == 3
359         return &eserial3_device;
360 #elif CONFIG_CONS_INDEX == 4
361         return &eserial4_device;
362 #else
363 #error "Bad CONFIG_CONS_INDEX."
364 #endif
365 }
366
367 #endif /* CONFIG_SERIAL_MULTI */