]> Git Repo - J-u-boot.git/blob - common/serial.c
serial: mips: Implement CONFIG_SERIAL_MULTI into au1x00 serial driver
[J-u-boot.git] / common / serial.c
1 /*
2  * (C) Copyright 2004
3  * Wolfgang Denk, DENX Software Engineering, [email protected].
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 #include <post.h>
28 #include <linux/compiler.h>
29
30 DECLARE_GLOBAL_DATA_PTR;
31
32 static struct serial_device *serial_devices;
33 static struct serial_device *serial_current;
34
35 static void serial_null(void)
36 {
37 }
38
39 #define serial_initfunc(name)                                   \
40         void name(void)                                         \
41                 __attribute__((weak, alias("serial_null")));
42
43 serial_initfunc(mpc8xx_serial_initialize);
44 serial_initfunc(ns16550_serial_initialize);
45 serial_initfunc(pxa_serial_initialize);
46 serial_initfunc(s3c24xx_serial_initialize);
47 serial_initfunc(s5p_serial_initialize);
48 serial_initfunc(zynq_serial_initalize);
49 serial_initfunc(bfin_serial_initialize);
50 serial_initfunc(bfin_jtag_initialize);
51 serial_initfunc(mpc512x_serial_initialize);
52 serial_initfunc(uartlite_serial_initialize);
53 serial_initfunc(au1x00_serial_initialize);
54
55 void serial_register(struct serial_device *dev)
56 {
57 #ifdef CONFIG_NEEDS_MANUAL_RELOC
58         dev->start += gd->reloc_off;
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;
64 #endif
65
66         dev->next = serial_devices;
67         serial_devices = dev;
68 }
69
70 void serial_initialize(void)
71 {
72         mpc8xx_serial_initialize();
73         ns16550_serial_initialize();
74         pxa_serial_initialize();
75         s3c24xx_serial_initialize();
76         s5p_serial_initialize();
77         mpc512x_serial_initialize();
78         bfin_serial_initialize();
79         bfin_jtag_initialize();
80         uartlite_serial_initialize();
81         zynq_serial_initalize();
82         au1x00_serial_initialize();
83
84         serial_assign(default_serial_console()->name);
85 }
86
87 void serial_stdio_init(void)
88 {
89         struct stdio_dev dev;
90         struct serial_device *s = serial_devices;
91
92         while (s) {
93                 memset(&dev, 0, sizeof(dev));
94
95                 strcpy(dev.name, s->name);
96                 dev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT;
97
98                 dev.start = s->start;
99                 dev.stop = s->stop;
100                 dev.putc = s->putc;
101                 dev.puts = s->puts;
102                 dev.getc = s->getc;
103                 dev.tstc = s->tstc;
104
105                 stdio_register(&dev);
106
107                 s = s->next;
108         }
109 }
110
111 int serial_assign(const char *name)
112 {
113         struct serial_device *s;
114
115         for (s = serial_devices; s; s = s->next) {
116                 if (strcmp(s->name, name) == 0) {
117                         serial_current = s;
118                         return 0;
119                 }
120         }
121
122         return 1;
123 }
124
125 void serial_reinit_all(void)
126 {
127         struct serial_device *s;
128
129         for (s = serial_devices; s; s = s->next)
130                 s->start();
131 }
132
133 static struct serial_device *get_current(void)
134 {
135         struct serial_device *dev;
136
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 }
147
148 int serial_init(void)
149 {
150         return get_current()->start();
151 }
152
153 void serial_setbrg(void)
154 {
155         get_current()->setbrg();
156 }
157
158 int serial_getc(void)
159 {
160         return get_current()->getc();
161 }
162
163 int serial_tstc(void)
164 {
165         return get_current()->tstc();
166 }
167
168 void serial_putc(const char c)
169 {
170         get_current()->putc(c);
171 }
172
173 void serial_puts(const char *s)
174 {
175         get_current()->puts(s);
176 }
177
178 #if CONFIG_POST & CONFIG_SYS_POST_UART
179 static const int bauds[] = CONFIG_SYS_BAUDRATE_TABLE;
180
181 /* Mark weak until post/cpu/.../uart.c migrate over */
182 __weak
183 int 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
250                 /* XXX: There is no serial_stop() !? */
251                 if (s->stop)
252                         s->stop();
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
This page took 0.041202 seconds and 4 git commands to generate.