]> git.ipfire.org Git - people/ms/u-boot.git/blame - common/serial.c
serial: mips: Implement CONFIG_SERIAL_MULTI into au1x00 serial driver
[people/ms/u-boot.git] / common / serial.c
CommitLineData
281e00a3
WD
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>
52cb4d4f 26#include <stdio_dev.h>
7b826c2f
MF
27#include <post.h>
28#include <linux/compiler.h>
281e00a3 29
d87080b7
WD
30DECLARE_GLOBAL_DATA_PTR;
31
a6e6f7f4
GF
32static struct serial_device *serial_devices;
33static struct serial_device *serial_current;
281e00a3 34
2a333aeb
MV
35static void serial_null(void)
36{
37}
38
39#define serial_initfunc(name) \
40 void name(void) \
41 __attribute__((weak, alias("serial_null")));
42
f0eb1f61 43serial_initfunc(mpc8xx_serial_initialize);
abc0ed8d 44serial_initfunc(ns16550_serial_initialize);
1fe5c110 45serial_initfunc(pxa_serial_initialize);
28af6385 46serial_initfunc(s3c24xx_serial_initialize);
b4980515 47serial_initfunc(s5p_serial_initialize);
51d8102f 48serial_initfunc(zynq_serial_initalize);
5ae1de0d 49serial_initfunc(bfin_serial_initialize);
41651cab 50serial_initfunc(bfin_jtag_initialize);
918327c8 51serial_initfunc(mpc512x_serial_initialize);
87d69229 52serial_initfunc(uartlite_serial_initialize);
7b953c51 53serial_initfunc(au1x00_serial_initialize);
f0eb1f61 54
c52b4f79 55void serial_register(struct serial_device *dev)
281e00a3 56{
2e5167cc 57#ifdef CONFIG_NEEDS_MANUAL_RELOC
89143fb3 58 dev->start += gd->reloc_off;
281e00a3
WD
59 dev->setbrg += gd->reloc_off;
60 dev->getc += gd->reloc_off;
61 dev->tstc += gd->reloc_off;
62 dev->putc += gd->reloc_off;
63 dev->puts += gd->reloc_off;
521af04d 64#endif
281e00a3
WD
65
66 dev->next = serial_devices;
67 serial_devices = dev;
281e00a3
WD
68}
69
a6e6f7f4 70void serial_initialize(void)
281e00a3 71{
f0eb1f61 72 mpc8xx_serial_initialize();
abc0ed8d 73 ns16550_serial_initialize();
1fe5c110 74 pxa_serial_initialize();
28af6385 75 s3c24xx_serial_initialize();
b4980515 76 s5p_serial_initialize();
918327c8 77 mpc512x_serial_initialize();
5ae1de0d 78 bfin_serial_initialize();
41651cab 79 bfin_jtag_initialize();
87d69229 80 uartlite_serial_initialize();
51d8102f 81 zynq_serial_initalize();
7b953c51
MV
82 au1x00_serial_initialize();
83
a6e6f7f4 84 serial_assign(default_serial_console()->name);
281e00a3
WD
85}
86
a6e6f7f4 87void serial_stdio_init(void)
281e00a3 88{
52cb4d4f 89 struct stdio_dev dev;
281e00a3
WD
90 struct serial_device *s = serial_devices;
91
2ee66533 92 while (s) {
a6e6f7f4 93 memset(&dev, 0, sizeof(dev));
281e00a3 94
a6e6f7f4 95 strcpy(dev.name, s->name);
281e00a3
WD
96 dev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT;
97
89143fb3
MV
98 dev.start = s->start;
99 dev.stop = s->stop;
281e00a3
WD
100 dev.putc = s->putc;
101 dev.puts = s->puts;
102 dev.getc = s->getc;
103 dev.tstc = s->tstc;
104
a6e6f7f4 105 stdio_register(&dev);
281e00a3
WD
106
107 s = s->next;
108 }
109}
110
7813ca9b 111int serial_assign(const char *name)
281e00a3
WD
112{
113 struct serial_device *s;
114
2ee66533 115 for (s = serial_devices; s; s = s->next) {
a6e6f7f4 116 if (strcmp(s->name, name) == 0) {
281e00a3
WD
117 serial_current = s;
118 return 0;
119 }
120 }
121
122 return 1;
123}
124
a6e6f7f4 125void serial_reinit_all(void)
281e00a3
WD
126{
127 struct serial_device *s;
128
a6e6f7f4 129 for (s = serial_devices; s; s = s->next)
89143fb3 130 s->start();
281e00a3
WD
131}
132
857c283e 133static struct serial_device *get_current(void)
281e00a3 134{
857c283e 135 struct serial_device *dev;
2ee66533 136
857c283e
SG
137 if (!(gd->flags & GD_FLG_RELOC) || !serial_current) {
138 dev = default_serial_console();
139
140 /* We must have a console device */
141 if (!dev)
142 panic("Cannot find console");
143 } else
144 dev = serial_current;
145 return dev;
146}
281e00a3 147
857c283e
SG
148int serial_init(void)
149{
89143fb3 150 return get_current()->start();
281e00a3
WD
151}
152
a6e6f7f4 153void serial_setbrg(void)
281e00a3 154{
857c283e 155 get_current()->setbrg();
281e00a3
WD
156}
157
a6e6f7f4 158int serial_getc(void)
281e00a3 159{
857c283e 160 return get_current()->getc();
281e00a3
WD
161}
162
a6e6f7f4 163int serial_tstc(void)
281e00a3 164{
857c283e 165 return get_current()->tstc();
281e00a3
WD
166}
167
a6e6f7f4 168void serial_putc(const char c)
281e00a3 169{
857c283e 170 get_current()->putc(c);
281e00a3
WD
171}
172
a6e6f7f4 173void serial_puts(const char *s)
281e00a3 174{
857c283e 175 get_current()->puts(s);
281e00a3 176}
7b826c2f
MF
177
178#if CONFIG_POST & CONFIG_SYS_POST_UART
179static const int bauds[] = CONFIG_SYS_BAUDRATE_TABLE;
180
181/* Mark weak until post/cpu/.../uart.c migrate over */
182__weak
183int uart_post_test(int flags)
184{
185 unsigned char c;
186 int ret, saved_baud, b;
187 struct serial_device *saved_dev, *s;
188 bd_t *bd = gd->bd;
189
190 /* Save current serial state */
191 ret = 0;
192 saved_dev = serial_current;
193 saved_baud = bd->bi_baudrate;
194
195 for (s = serial_devices; s; s = s->next) {
196 /* If this driver doesn't support loop back, skip it */
197 if (!s->loop)
198 continue;
199
200 /* Test the next device */
201 serial_current = s;
202
203 ret = serial_init();
204 if (ret)
205 goto done;
206
207 /* Consume anything that happens to be queued */
208 while (serial_tstc())
209 serial_getc();
210
211 /* Enable loop back */
212 s->loop(1);
213
214 /* Test every available baud rate */
215 for (b = 0; b < ARRAY_SIZE(bauds); ++b) {
216 bd->bi_baudrate = bauds[b];
217 serial_setbrg();
218
219 /*
220 * Stick to printable chars to avoid issues:
221 * - terminal corruption
222 * - serial program reacting to sequences and sending
223 * back random extra data
224 * - most serial drivers add in extra chars (like \r\n)
225 */
226 for (c = 0x20; c < 0x7f; ++c) {
227 /* Send it out */
228 serial_putc(c);
229
230 /* Make sure it's the same one */
231 ret = (c != serial_getc());
232 if (ret) {
233 s->loop(0);
234 goto done;
235 }
236
237 /* Clean up the output in case it was sent */
238 serial_putc('\b');
239 ret = ('\b' != serial_getc());
240 if (ret) {
241 s->loop(0);
242 goto done;
243 }
244 }
245 }
246
247 /* Disable loop back */
248 s->loop(0);
249
89143fb3
MV
250 /* XXX: There is no serial_stop() !? */
251 if (s->stop)
252 s->stop();
7b826c2f
MF
253 }
254
255 done:
256 /* Restore previous serial state */
257 serial_current = saved_dev;
258 bd->bi_baudrate = saved_baud;
259 serial_reinit_all();
260 serial_setbrg();
261
262 return ret;
263}
264#endif