baeaeaac8ea2fa236503358f2162776c6c96b55a
[oweals/u-boot.git] / drivers / serial / serial.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2004
4  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5  */
6
7 #include <common.h>
8 #include <env_internal.h>
9 #include <hang.h>
10 #include <serial.h>
11 #include <stdio_dev.h>
12 #include <post.h>
13 #include <linux/compiler.h>
14 #include <errno.h>
15
16 DECLARE_GLOBAL_DATA_PTR;
17
18 static struct serial_device *serial_devices;
19 static struct serial_device *serial_current;
20 /*
21  * Table with supported baudrates (defined in config_xyz.h)
22  */
23 static const unsigned long baudrate_table[] = CONFIG_SYS_BAUDRATE_TABLE;
24
25 /**
26  * serial_null() - Void registration routine of a serial driver
27  *
28  * This routine implements a void registration routine of a serial
29  * driver. The registration routine of a particular driver is aliased
30  * to this empty function in case the driver is not compiled into
31  * U-Boot.
32  */
33 static void serial_null(void)
34 {
35 }
36
37 /**
38  * on_baudrate() - Update the actual baudrate when the env var changes
39  *
40  * @name:       changed environment variable
41  * @value:      new value of the environment variable
42  * @op:         operation (create, overwrite, or delete)
43  * @flags:      attributes of environment variable change,
44  *              see flags H_* in include/search.h
45  *
46  * This will check for a valid baudrate and only apply it if valid.
47  *
48  * Return:      0 on success, 1 on error
49  */
50 static int on_baudrate(const char *name, const char *value, enum env_op op,
51         int flags)
52 {
53         int i;
54         int baudrate;
55
56         switch (op) {
57         case env_op_create:
58         case env_op_overwrite:
59                 /*
60                  * Switch to new baudrate if new baudrate is supported
61                  */
62                 baudrate = simple_strtoul(value, NULL, 10);
63
64                 /* Not actually changing */
65                 if (gd->baudrate == baudrate)
66                         return 0;
67
68                 for (i = 0; i < ARRAY_SIZE(baudrate_table); ++i) {
69                         if (baudrate == baudrate_table[i])
70                                 break;
71                 }
72                 if (i == ARRAY_SIZE(baudrate_table)) {
73                         if ((flags & H_FORCE) == 0)
74                                 printf("## Baudrate %d bps not supported\n",
75                                         baudrate);
76                         return 1;
77                 }
78                 if ((flags & H_INTERACTIVE) != 0) {
79                         printf("## Switch baudrate to %d"
80                                 " bps and press ENTER ...\n", baudrate);
81                         udelay(50000);
82                 }
83
84                 gd->baudrate = baudrate;
85
86                 serial_setbrg();
87
88                 udelay(50000);
89
90                 if ((flags & H_INTERACTIVE) != 0)
91                         while (1) {
92                                 if (getc() == '\r')
93                                         break;
94                         }
95
96                 return 0;
97         case env_op_delete:
98                 printf("## Baudrate may not be deleted\n");
99                 return 1;
100         default:
101                 return 0;
102         }
103 }
104 U_BOOT_ENV_CALLBACK(baudrate, on_baudrate);
105
106 /**
107  * serial_initfunc() - Forward declare of driver registration routine
108  * @name:       Name of the real driver registration routine.
109  *
110  * This macro expands onto forward declaration of a driver registration
111  * routine, which is then used below in serial_initialize() function.
112  * The declaration is made weak and aliases to serial_null() so in case
113  * the driver is not compiled in, the function is still declared and can
114  * be used, but aliases to serial_null() and thus is optimized away.
115  */
116 #define serial_initfunc(name)                                   \
117         void name(void)                                         \
118                 __attribute__((weak, alias("serial_null")));
119
120 serial_initfunc(atmel_serial_initialize);
121 serial_initfunc(mcf_serial_initialize);
122 serial_initfunc(mpc85xx_serial_initialize);
123 serial_initfunc(mxc_serial_initialize);
124 serial_initfunc(ns16550_serial_initialize);
125 serial_initfunc(pl01x_serial_initialize);
126 serial_initfunc(pxa_serial_initialize);
127 serial_initfunc(sh_serial_initialize);
128 serial_initfunc(mtk_serial_initialize);
129
130 /**
131  * serial_register() - Register serial driver with serial driver core
132  * @dev:        Pointer to the serial driver structure
133  *
134  * This function registers the serial driver supplied via @dev with
135  * serial driver core, thus making U-Boot aware of it and making it
136  * available for U-Boot to use. On platforms that still require manual
137  * relocation of constant variables, relocation of the supplied structure
138  * is performed.
139  */
140 void serial_register(struct serial_device *dev)
141 {
142 #ifdef CONFIG_NEEDS_MANUAL_RELOC
143         if (dev->start)
144                 dev->start += gd->reloc_off;
145         if (dev->stop)
146                 dev->stop += gd->reloc_off;
147         if (dev->setbrg)
148                 dev->setbrg += gd->reloc_off;
149         if (dev->getc)
150                 dev->getc += gd->reloc_off;
151         if (dev->tstc)
152                 dev->tstc += gd->reloc_off;
153         if (dev->putc)
154                 dev->putc += gd->reloc_off;
155         if (dev->puts)
156                 dev->puts += gd->reloc_off;
157 #endif
158
159         dev->next = serial_devices;
160         serial_devices = dev;
161 }
162
163 /**
164  * serial_initialize() - Register all compiled-in serial port drivers
165  *
166  * This function registers all serial port drivers that are compiled
167  * into the U-Boot binary with the serial core, thus making them
168  * available to U-Boot to use. Lastly, this function assigns a default
169  * serial port to the serial core. That serial port is then used as a
170  * default output.
171  */
172 void serial_initialize(void)
173 {
174         atmel_serial_initialize();
175         mcf_serial_initialize();
176         mpc85xx_serial_initialize();
177         mxc_serial_initialize();
178         ns16550_serial_initialize();
179         pl01x_serial_initialize();
180         pxa_serial_initialize();
181         sh_serial_initialize();
182         mtk_serial_initialize();
183
184         serial_assign(default_serial_console()->name);
185 }
186
187 static int serial_stub_start(struct stdio_dev *sdev)
188 {
189         struct serial_device *dev = sdev->priv;
190
191         return dev->start();
192 }
193
194 static int serial_stub_stop(struct stdio_dev *sdev)
195 {
196         struct serial_device *dev = sdev->priv;
197
198         return dev->stop();
199 }
200
201 static void serial_stub_putc(struct stdio_dev *sdev, const char ch)
202 {
203         struct serial_device *dev = sdev->priv;
204
205         dev->putc(ch);
206 }
207
208 static void serial_stub_puts(struct stdio_dev *sdev, const char *str)
209 {
210         struct serial_device *dev = sdev->priv;
211
212         dev->puts(str);
213 }
214
215 static int serial_stub_getc(struct stdio_dev *sdev)
216 {
217         struct serial_device *dev = sdev->priv;
218
219         return dev->getc();
220 }
221
222 static int serial_stub_tstc(struct stdio_dev *sdev)
223 {
224         struct serial_device *dev = sdev->priv;
225
226         return dev->tstc();
227 }
228
229 /**
230  * serial_stdio_init() - Register serial ports with STDIO core
231  *
232  * This function generates a proxy driver for each serial port driver.
233  * These proxy drivers then register with the STDIO core, making the
234  * serial drivers available as STDIO devices.
235  */
236 void serial_stdio_init(void)
237 {
238         struct stdio_dev dev;
239         struct serial_device *s = serial_devices;
240
241         while (s) {
242                 memset(&dev, 0, sizeof(dev));
243
244                 strcpy(dev.name, s->name);
245                 dev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT;
246
247                 dev.start = serial_stub_start;
248                 dev.stop = serial_stub_stop;
249                 dev.putc = serial_stub_putc;
250                 dev.puts = serial_stub_puts;
251                 dev.getc = serial_stub_getc;
252                 dev.tstc = serial_stub_tstc;
253                 dev.priv = s;
254
255                 stdio_register(&dev);
256
257                 s = s->next;
258         }
259 }
260
261 /**
262  * serial_assign() - Select the serial output device by name
263  * @name:       Name of the serial driver to be used as default output
264  *
265  * This function configures the serial output multiplexing by
266  * selecting which serial device will be used as default. In case
267  * the STDIO "serial" device is selected as stdin/stdout/stderr,
268  * the serial device previously configured by this function will be
269  * used for the particular operation.
270  *
271  * Returns 0 on success, negative on error.
272  */
273 int serial_assign(const char *name)
274 {
275         struct serial_device *s;
276
277         for (s = serial_devices; s; s = s->next) {
278                 if (strcmp(s->name, name))
279                         continue;
280                 serial_current = s;
281                 return 0;
282         }
283
284         return -EINVAL;
285 }
286
287 /**
288  * serial_reinit_all() - Reinitialize all compiled-in serial ports
289  *
290  * This function reinitializes all serial ports that are compiled
291  * into U-Boot by calling their serial_start() functions.
292  */
293 void serial_reinit_all(void)
294 {
295         struct serial_device *s;
296
297         for (s = serial_devices; s; s = s->next)
298                 s->start();
299 }
300
301 /**
302  * get_current() - Return pointer to currently selected serial port
303  *
304  * This function returns a pointer to currently selected serial port.
305  * The currently selected serial port is altered by serial_assign()
306  * function.
307  *
308  * In case this function is called before relocation or before any serial
309  * port is configured, this function calls default_serial_console() to
310  * determine the serial port. Otherwise, the configured serial port is
311  * returned.
312  *
313  * Returns pointer to the currently selected serial port on success,
314  * NULL on error.
315  */
316 static struct serial_device *get_current(void)
317 {
318         struct serial_device *dev;
319
320         if (!(gd->flags & GD_FLG_RELOC))
321                 dev = default_serial_console();
322         else if (!serial_current)
323                 dev = default_serial_console();
324         else
325                 dev = serial_current;
326
327         /* We must have a console device */
328         if (!dev) {
329 #ifdef CONFIG_SPL_BUILD
330                 puts("Cannot find console\n");
331                 hang();
332 #else
333                 panic("Cannot find console\n");
334 #endif
335         }
336
337         return dev;
338 }
339
340 /**
341  * serial_init() - Initialize currently selected serial port
342  *
343  * This function initializes the currently selected serial port. This
344  * usually involves setting up the registers of that particular port,
345  * enabling clock and such. This function uses the get_current() call
346  * to determine which port is selected.
347  *
348  * Returns 0 on success, negative on error.
349  */
350 int serial_init(void)
351 {
352         gd->flags |= GD_FLG_SERIAL_READY;
353         return get_current()->start();
354 }
355
356 /**
357  * serial_setbrg() - Configure baud-rate of currently selected serial port
358  *
359  * This function configures the baud-rate of the currently selected
360  * serial port. The baud-rate is retrieved from global data within
361  * the serial port driver. This function uses the get_current() call
362  * to determine which port is selected.
363  *
364  * Returns 0 on success, negative on error.
365  */
366 void serial_setbrg(void)
367 {
368         get_current()->setbrg();
369 }
370
371 /**
372  * serial_getc() - Read character from currently selected serial port
373  *
374  * This function retrieves a character from currently selected serial
375  * port. In case there is no character waiting on the serial port,
376  * this function will block and wait for the character to appear. This
377  * function uses the get_current() call to determine which port is
378  * selected.
379  *
380  * Returns the character on success, negative on error.
381  */
382 int serial_getc(void)
383 {
384         return get_current()->getc();
385 }
386
387 /**
388  * serial_tstc() - Test if data is available on currently selected serial port
389  *
390  * This function tests if one or more characters are available on
391  * currently selected serial port. This function never blocks. This
392  * function uses the get_current() call to determine which port is
393  * selected.
394  *
395  * Returns positive if character is available, zero otherwise.
396  */
397 int serial_tstc(void)
398 {
399         return get_current()->tstc();
400 }
401
402 /**
403  * serial_putc() - Output character via currently selected serial port
404  * @c:  Single character to be output from the serial port.
405  *
406  * This function outputs a character via currently selected serial
407  * port. This character is passed to the serial port driver responsible
408  * for controlling the hardware. The hardware may still be in process
409  * of transmitting another character, therefore this function may block
410  * for a short amount of time. This function uses the get_current()
411  * call to determine which port is selected.
412  */
413 void serial_putc(const char c)
414 {
415         get_current()->putc(c);
416 }
417
418 /**
419  * serial_puts() - Output string via currently selected serial port
420  * @s:  Zero-terminated string to be output from the serial port.
421  *
422  * This function outputs a zero-terminated string via currently
423  * selected serial port. This function behaves as an accelerator
424  * in case the hardware can queue multiple characters for transfer.
425  * The whole string that is to be output is available to the function
426  * implementing the hardware manipulation. Transmitting the whole
427  * string may take some time, thus this function may block for some
428  * amount of time. This function uses the get_current() call to
429  * determine which port is selected.
430  */
431 void serial_puts(const char *s)
432 {
433         get_current()->puts(s);
434 }
435
436 /**
437  * default_serial_puts() - Output string by calling serial_putc() in loop
438  * @s:  Zero-terminated string to be output from the serial port.
439  *
440  * This function outputs a zero-terminated string by calling serial_putc()
441  * in a loop. Most drivers do not support queueing more than one byte for
442  * transfer, thus this function precisely implements their serial_puts().
443  *
444  * To optimize the number of get_current() calls, this function only
445  * calls get_current() once and then directly accesses the putc() call
446  * of the &struct serial_device .
447  */
448 void default_serial_puts(const char *s)
449 {
450         struct serial_device *dev = get_current();
451         while (*s)
452                 dev->putc(*s++);
453 }
454
455 #if CONFIG_POST & CONFIG_SYS_POST_UART
456 static const int bauds[] = CONFIG_SYS_BAUDRATE_TABLE;
457
458 /**
459  * uart_post_test() - Test the currently selected serial port using POST
460  * @flags:      POST framework flags
461  *
462  * Do a loopback test of the currently selected serial port. This
463  * function is only useful in the context of the POST testing framwork.
464  * The serial port is first configured into loopback mode and then
465  * characters are sent through it.
466  *
467  * Returns 0 on success, value otherwise.
468  */
469 /* Mark weak until post/cpu/.../uart.c migrate over */
470 __weak
471 int uart_post_test(int flags)
472 {
473         unsigned char c;
474         int ret, saved_baud, b;
475         struct serial_device *saved_dev, *s;
476
477         /* Save current serial state */
478         ret = 0;
479         saved_dev = serial_current;
480         saved_baud = gd->baudrate;
481
482         for (s = serial_devices; s; s = s->next) {
483                 /* If this driver doesn't support loop back, skip it */
484                 if (!s->loop)
485                         continue;
486
487                 /* Test the next device */
488                 serial_current = s;
489
490                 ret = serial_init();
491                 if (ret)
492                         goto done;
493
494                 /* Consume anything that happens to be queued */
495                 while (serial_tstc())
496                         serial_getc();
497
498                 /* Enable loop back */
499                 s->loop(1);
500
501                 /* Test every available baud rate */
502                 for (b = 0; b < ARRAY_SIZE(bauds); ++b) {
503                         gd->baudrate = bauds[b];
504                         serial_setbrg();
505
506                         /*
507                          * Stick to printable chars to avoid issues:
508                          *  - terminal corruption
509                          *  - serial program reacting to sequences and sending
510                          *    back random extra data
511                          *  - most serial drivers add in extra chars (like \r\n)
512                          */
513                         for (c = 0x20; c < 0x7f; ++c) {
514                                 /* Send it out */
515                                 serial_putc(c);
516
517                                 /* Make sure it's the same one */
518                                 ret = (c != serial_getc());
519                                 if (ret) {
520                                         s->loop(0);
521                                         goto done;
522                                 }
523
524                                 /* Clean up the output in case it was sent */
525                                 serial_putc('\b');
526                                 ret = ('\b' != serial_getc());
527                                 if (ret) {
528                                         s->loop(0);
529                                         goto done;
530                                 }
531                         }
532                 }
533
534                 /* Disable loop back */
535                 s->loop(0);
536
537                 /* XXX: There is no serial_stop() !? */
538                 if (s->stop)
539                         s->stop();
540         }
541
542  done:
543         /* Restore previous serial state */
544         serial_current = saved_dev;
545         gd->baudrate = saved_baud;
546         serial_reinit_all();
547         serial_setbrg();
548
549         return ret;
550 }
551 #endif