ppc4xx: Use common NS16550 driver for PPC4xx UART
[platform/kernel/u-boot.git] / common / serial.c
1 /*
2  * (C) Copyright 2004
3  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
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 <serial.h>
26 #include <stdio_dev.h>
27
28 DECLARE_GLOBAL_DATA_PTR;
29
30 static struct serial_device *serial_devices = NULL;
31 static struct serial_device *serial_current = NULL;
32
33 #if !defined(CONFIG_LWMON) && !defined(CONFIG_PXA27X)
34 struct serial_device *__default_serial_console (void)
35 {
36 #if defined(CONFIG_8xx_CONS_SMC1) || defined(CONFIG_8xx_CONS_SMC2)
37         return &serial_smc_device;
38 #elif defined(CONFIG_8xx_CONS_SCC1) || defined(CONFIG_8xx_CONS_SCC2) \
39    || defined(CONFIG_8xx_CONS_SCC3) || defined(CONFIG_8xx_CONS_SCC4)
40         return &serial_scc_device;
41 #elif defined(CONFIG_4xx) \
42    || defined(CONFIG_MB86R0x) || defined(CONFIG_MPC5xxx) \
43    || defined(CONFIG_MPC83xx) || defined(CONFIG_MPC85xx) \
44    || defined(CONFIG_MPC86xx) || defined(CONFIG_SYS_SC520)
45 #if defined(CONFIG_CONS_INDEX) && defined(CONFIG_SYS_NS16550_SERIAL)
46 #if (CONFIG_CONS_INDEX==1)
47         return &eserial1_device;
48 #elif (CONFIG_CONS_INDEX==2)
49         return &eserial2_device;
50 #elif (CONFIG_CONS_INDEX==3)
51         return &eserial3_device;
52 #elif (CONFIG_CONS_INDEX==4)
53         return &eserial4_device;
54 #else
55 #error "Bad CONFIG_CONS_INDEX."
56 #endif
57 #elif defined(CONFIG_UART1_CONSOLE)
58                 return &serial1_device;
59 #else
60                 return &serial0_device;
61 #endif
62 #elif defined(CONFIG_MPC512X)
63 #if (CONFIG_PSC_CONSOLE == 3)
64                 return &serial3_device;
65 #elif (CONFIG_PSC_CONSOLE == 6)
66                 return &serial6_device;
67 #else
68 #error "Bad CONFIG_PSC_CONSOLE."
69 #endif
70 #elif defined(CONFIG_S3C2410)
71 #if defined(CONFIG_SERIAL1)
72         return &s3c24xx_serial0_device;
73 #elif defined(CONFIG_SERIAL2)
74         return &s3c24xx_serial1_device;
75 #elif defined(CONFIG_SERIAL3)
76         return &s3c24xx_serial2_device;
77 #else
78 #error "CONFIG_SERIAL? missing."
79 #endif
80 #elif defined(CONFIG_S5P)
81 #if defined(CONFIG_SERIAL0)
82         return &s5p_serial0_device;
83 #elif defined(CONFIG_SERIAL1)
84         return &s5p_serial1_device;
85 #elif defined(CONFIG_SERIAL2)
86         return &s5p_serial2_device;
87 #elif defined(CONFIG_SERIAL3)
88         return &s5p_serial3_device;
89 #else
90 #error "CONFIG_SERIAL? missing."
91 #endif
92 #elif defined(CONFIG_OMAP3_ZOOM2)
93                 return ZOOM2_DEFAULT_SERIAL_DEVICE;
94 #else
95 #error No default console
96 #endif
97 }
98
99 struct serial_device *default_serial_console(void) __attribute__((weak, alias("__default_serial_console")));
100 #endif
101
102 int serial_register (struct serial_device *dev)
103 {
104 #ifndef CONFIG_RELOC_FIXUP_WORKS
105         dev->init += gd->reloc_off;
106         dev->setbrg += gd->reloc_off;
107         dev->getc += gd->reloc_off;
108         dev->tstc += gd->reloc_off;
109         dev->putc += gd->reloc_off;
110         dev->puts += gd->reloc_off;
111 #endif
112
113         dev->next = serial_devices;
114         serial_devices = dev;
115
116         return 0;
117 }
118
119 void serial_initialize (void)
120 {
121 #if defined(CONFIG_8xx_CONS_SMC1) || defined(CONFIG_8xx_CONS_SMC2)
122         serial_register (&serial_smc_device);
123 #endif
124 #if defined(CONFIG_8xx_CONS_SCC1) || defined(CONFIG_8xx_CONS_SCC2) \
125  || defined(CONFIG_8xx_CONS_SCC3) || defined(CONFIG_8xx_CONS_SCC4)
126         serial_register (&serial_scc_device);
127 #endif
128
129 #if defined(CONFIG_SYS_NS16550_SERIAL)
130 #if defined(CONFIG_SYS_NS16550_COM1)
131         serial_register(&eserial1_device);
132 #endif
133 #if defined(CONFIG_SYS_NS16550_COM2)
134         serial_register(&eserial2_device);
135 #endif
136 #if defined(CONFIG_SYS_NS16550_COM3)
137         serial_register(&eserial3_device);
138 #endif
139 #if defined(CONFIG_SYS_NS16550_COM4)
140         serial_register(&eserial4_device);
141 #endif
142 #endif /* CONFIG_SYS_NS16550_SERIAL */
143 #if defined (CONFIG_FFUART)
144         serial_register(&serial_ffuart_device);
145 #endif
146 #if defined (CONFIG_BTUART)
147         serial_register(&serial_btuart_device);
148 #endif
149 #if defined (CONFIG_STUART)
150         serial_register(&serial_stuart_device);
151 #endif
152 #if defined(CONFIG_S3C2410)
153         serial_register(&s3c24xx_serial0_device);
154         serial_register(&s3c24xx_serial1_device);
155         serial_register(&s3c24xx_serial2_device);
156 #endif
157 #if defined(CONFIG_S5P)
158         serial_register(&s5p_serial0_device);
159         serial_register(&s5p_serial1_device);
160         serial_register(&s5p_serial2_device);
161         serial_register(&s5p_serial3_device);
162 #endif
163 #if defined(CONFIG_MPC512X)
164 #if defined(CONFIG_SYS_PSC1)
165         serial_register(&serial1_device);
166 #endif
167 #if defined(CONFIG_SYS_PSC3)
168         serial_register(&serial3_device);
169 #endif
170 #if defined(CONFIG_SYS_PSC4)
171         serial_register(&serial4_device);
172 #endif
173 #if defined(CONFIG_SYS_PSC6)
174         serial_register(&serial6_device);
175 #endif
176 #endif
177         serial_assign (default_serial_console ()->name);
178 }
179
180 void serial_stdio_init (void)
181 {
182         struct stdio_dev dev;
183         struct serial_device *s = serial_devices;
184
185         while (s) {
186                 memset (&dev, 0, sizeof (dev));
187
188                 strcpy (dev.name, s->name);
189                 dev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT;
190
191                 dev.start = s->init;
192                 dev.stop = s->uninit;
193                 dev.putc = s->putc;
194                 dev.puts = s->puts;
195                 dev.getc = s->getc;
196                 dev.tstc = s->tstc;
197
198                 stdio_register (&dev);
199
200                 s = s->next;
201         }
202 }
203
204 int serial_assign (char *name)
205 {
206         struct serial_device *s;
207
208         for (s = serial_devices; s; s = s->next) {
209                 if (strcmp (s->name, name) == 0) {
210                         serial_current = s;
211                         return 0;
212                 }
213         }
214
215         return 1;
216 }
217
218 void serial_reinit_all (void)
219 {
220         struct serial_device *s;
221
222         for (s = serial_devices; s; s = s->next) {
223                 s->init ();
224         }
225 }
226
227 int serial_init (void)
228 {
229         if (!(gd->flags & GD_FLG_RELOC) || !serial_current) {
230                 struct serial_device *dev = default_serial_console ();
231
232                 return dev->init ();
233         }
234
235         return serial_current->init ();
236 }
237
238 void serial_setbrg (void)
239 {
240         if (!(gd->flags & GD_FLG_RELOC) || !serial_current) {
241                 struct serial_device *dev = default_serial_console ();
242
243                 dev->setbrg ();
244                 return;
245         }
246
247         serial_current->setbrg ();
248 }
249
250 int serial_getc (void)
251 {
252         if (!(gd->flags & GD_FLG_RELOC) || !serial_current) {
253                 struct serial_device *dev = default_serial_console ();
254
255                 return dev->getc ();
256         }
257
258         return serial_current->getc ();
259 }
260
261 int serial_tstc (void)
262 {
263         if (!(gd->flags & GD_FLG_RELOC) || !serial_current) {
264                 struct serial_device *dev = default_serial_console ();
265
266                 return dev->tstc ();
267         }
268
269         return serial_current->tstc ();
270 }
271
272 void serial_putc (const char c)
273 {
274         if (!(gd->flags & GD_FLG_RELOC) || !serial_current) {
275                 struct serial_device *dev = default_serial_console ();
276
277                 dev->putc (c);
278                 return;
279         }
280
281         serial_current->putc (c);
282 }
283
284 void serial_puts (const char *s)
285 {
286         if (!(gd->flags & GD_FLG_RELOC) || !serial_current) {
287                 struct serial_device *dev = default_serial_console ();
288
289                 dev->puts (s);
290                 return;
291         }
292
293         serial_current->puts (s);
294 }