]> git.kernelconcepts.de Git - karo-tx-uboot.git/blobdiff - drivers/serial/serial.c
Merge branch 'u-boot-samsung/master' into 'u-boot-arm/master'
[karo-tx-uboot.git] / drivers / serial / serial.c
index 966df9ac3bf8aba14e7d519b11f49da92c1fd9a4..daa80038a71ddc507fa594a95c575e6823393764 100644 (file)
@@ -1,6 +1,6 @@
 /*
- * (C) Copyright 2000
- * Rob Taylor, Flying Pig Systems. robt@flyingpig.com.
+ * (C) Copyright 2004
+ * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  *
  * See file CREDITS for list of people who contributed to this
  * project.
  */
 
 #include <common.h>
-
-#include <ns16550.h>
-#ifdef CONFIG_NS87308
-#include <ns87308.h>
-#endif
-
-#if defined (CONFIG_SERIAL_MULTI)
+#include <environment.h>
 #include <serial.h>
-#endif
+#include <stdio_dev.h>
+#include <post.h>
+#include <linux/compiler.h>
+#include <errno.h>
 
 DECLARE_GLOBAL_DATA_PTR;
 
-#if !defined(CONFIG_CONS_INDEX)
-#if defined (CONFIG_SERIAL_MULTI)
-/*   with CONFIG_SERIAL_MULTI we might have no console
- *  on these devices
+static struct serial_device *serial_devices;
+static struct serial_device *serial_current;
+/*
+ * Table with supported baudrates (defined in config_xyz.h)
  */
-#else
-#error "No console index specified."
-#endif /* CONFIG_SERIAL_MULTI */
-#elif (CONFIG_CONS_INDEX < 1) || (CONFIG_CONS_INDEX > 4)
-#error "Invalid console index value."
-#endif
+static const unsigned long baudrate_table[] = CONFIG_SYS_BAUDRATE_TABLE;
+#define        N_BAUDRATES (sizeof(baudrate_table) / sizeof(baudrate_table[0]))
 
-#if CONFIG_CONS_INDEX == 1 && !defined(CONFIG_SYS_NS16550_COM1)
-#error "Console port 1 defined but not configured."
-#elif CONFIG_CONS_INDEX == 2 && !defined(CONFIG_SYS_NS16550_COM2)
-#error "Console port 2 defined but not configured."
-#elif CONFIG_CONS_INDEX == 3 && !defined(CONFIG_SYS_NS16550_COM3)
-#error "Console port 3 defined but not configured."
-#elif CONFIG_CONS_INDEX == 4 && !defined(CONFIG_SYS_NS16550_COM4)
-#error "Console port 4 defined but not configured."
-#endif
+/**
+ * serial_null() - Void registration routine of a serial driver
+ *
+ * This routine implements a void registration routine of a serial
+ * driver. The registration routine of a particular driver is aliased
+ * to this empty function in case the driver is not compiled into
+ * U-Boot.
+ */
+static void serial_null(void)
+{
+}
 
-/* Note: The port number specified in the functions is 1 based.
- *      the array is 0 based.
+/**
+ * on_baudrate() - Update the actual baudrate when the env var changes
+ *
+ * This will check for a valid baudrate and only apply it if valid.
  */
-static NS16550_t serial_ports[4] = {
-#ifdef CONFIG_SYS_NS16550_COM1
-       (NS16550_t)CONFIG_SYS_NS16550_COM1,
-#else
-       NULL,
-#endif
-#ifdef CONFIG_SYS_NS16550_COM2
-       (NS16550_t)CONFIG_SYS_NS16550_COM2,
-#else
-       NULL,
-#endif
-#ifdef CONFIG_SYS_NS16550_COM3
-       (NS16550_t)CONFIG_SYS_NS16550_COM3,
-#else
-       NULL,
-#endif
-#ifdef CONFIG_SYS_NS16550_COM4
-       (NS16550_t)CONFIG_SYS_NS16550_COM4
-#else
-       NULL
+static int on_baudrate(const char *name, const char *value, enum env_op op,
+       int flags)
+{
+       int i;
+       int baudrate;
+
+       switch (op) {
+       case env_op_create:
+       case env_op_overwrite:
+               /*
+                * Switch to new baudrate if new baudrate is supported
+                */
+               baudrate = simple_strtoul(value, NULL, 10);
+
+               /* Not actually changing */
+               if (gd->baudrate == baudrate)
+                       return 0;
+
+               for (i = 0; i < N_BAUDRATES; ++i) {
+                       if (baudrate == baudrate_table[i])
+                               break;
+               }
+               if (i == N_BAUDRATES) {
+                       if ((flags & H_FORCE) == 0)
+                               printf("## Baudrate %d bps not supported\n",
+                                       baudrate);
+                       return 1;
+               }
+               if ((flags & H_INTERACTIVE) != 0) {
+                       printf("## Switch baudrate to %d"
+                               " bps and press ENTER ...\n", baudrate);
+                       udelay(50000);
+               }
+
+               gd->baudrate = baudrate;
+#if defined(CONFIG_PPC) || defined(CONFIG_MCF52x2)
+               gd->bd->bi_baudrate = baudrate;
 #endif
-};
 
-#define PORT   serial_ports[port-1]
-#if defined(CONFIG_CONS_INDEX)
-#define CONSOLE        (serial_ports[CONFIG_CONS_INDEX-1])
-#endif
+               serial_setbrg();
 
-#if defined(CONFIG_SERIAL_MULTI)
-
-/* Multi serial device functions */
-#define DECLARE_ESERIAL_FUNCTIONS(port) \
-    int  eserial##port##_init (void) {\
-       int clock_divisor; \
-       clock_divisor = calc_divisor(serial_ports[port-1]); \
-       NS16550_init(serial_ports[port-1], clock_divisor); \
-       return(0);}\
-    void eserial##port##_setbrg (void) {\
-       serial_setbrg_dev(port);}\
-    int  eserial##port##_getc (void) {\
-       return serial_getc_dev(port);}\
-    int  eserial##port##_tstc (void) {\
-       return serial_tstc_dev(port);}\
-    void eserial##port##_putc (const char c) {\
-       serial_putc_dev(port, c);}\
-    void eserial##port##_puts (const char *s) {\
-       serial_puts_dev(port, s);}
-
-/* Serial device descriptor */
-#define INIT_ESERIAL_STRUCTURE(port,name,bus) {\
-       name,\
-       bus,\
-       eserial##port##_init,\
-       eserial##port##_setbrg,\
-       eserial##port##_getc,\
-       eserial##port##_tstc,\
-       eserial##port##_putc,\
-       eserial##port##_puts, }
-
-#endif /* CONFIG_SERIAL_MULTI */
-
-static int calc_divisor (NS16550_t port)
-{
-#ifdef CONFIG_OMAP1510
-       /* If can't cleanly clock 115200 set div to 1 */
-       if ((CONFIG_SYS_NS16550_CLK == 12000000) && (gd->baudrate == 115200)) {
-               port->osc_12m_sel = OSC_12M_SEL;        /* enable 6.5 * divisor */
-               return (1);                             /* return 1 for base divisor */
-       }
-       port->osc_12m_sel = 0;                  /* clear if previsouly set */
-#endif
-#ifdef CONFIG_OMAP1610
-       /* If can't cleanly clock 115200 set div to 1 */
-       if ((CONFIG_SYS_NS16550_CLK == 48000000) && (gd->baudrate == 115200)) {
-               return (26);            /* return 26 for base divisor */
-       }
-#endif
+               udelay(50000);
 
-#ifdef CONFIG_APTIX
-#define MODE_X_DIV 13
-#else
-#define MODE_X_DIV 16
-#endif
+               if ((flags & H_INTERACTIVE) != 0)
+                       while (1) {
+                               if (getc() == '\r')
+                                       break;
+                       }
 
-       /* Compute divisor value. Normally, we should simply return:
-        *   CONFIG_SYS_NS16550_CLK) / MODE_X_DIV / gd->baudrate
-        * but we need to round that value by adding 0.5.
-        * Rounding is especially important at high baud rates.
-        */
-       return (CONFIG_SYS_NS16550_CLK + (gd->baudrate * (MODE_X_DIV / 2))) /
-               (MODE_X_DIV * gd->baudrate);
+               return 0;
+       case env_op_delete:
+               printf("## Baudrate may not be deleted\n");
+               return 1;
+       default:
+               return 0;
+       }
 }
+U_BOOT_ENV_CALLBACK(baudrate, on_baudrate);
 
-#if !defined(CONFIG_SERIAL_MULTI)
-int serial_init (void)
+/**
+ * serial_initfunc() - Forward declare of driver registration routine
+ * @name:      Name of the real driver registration routine.
+ *
+ * This macro expands onto forward declaration of a driver registration
+ * routine, which is then used below in serial_initialize() function.
+ * The declaration is made weak and aliases to serial_null() so in case
+ * the driver is not compiled in, the function is still declared and can
+ * be used, but aliases to serial_null() and thus is optimized away.
+ */
+#define serial_initfunc(name)                                  \
+       void name(void)                                         \
+               __attribute__((weak, alias("serial_null")));
+
+serial_initfunc(mpc8xx_serial_initialize);
+serial_initfunc(ns16550_serial_initialize);
+serial_initfunc(pxa_serial_initialize);
+serial_initfunc(s3c24xx_serial_initialize);
+serial_initfunc(s5p_serial_initialize);
+serial_initfunc(zynq_serial_initalize);
+serial_initfunc(bfin_serial_initialize);
+serial_initfunc(bfin_jtag_initialize);
+serial_initfunc(mpc512x_serial_initialize);
+serial_initfunc(uartlite_serial_initialize);
+serial_initfunc(au1x00_serial_initialize);
+serial_initfunc(asc_serial_initialize);
+serial_initfunc(jz_serial_initialize);
+serial_initfunc(mpc5xx_serial_initialize);
+serial_initfunc(mpc8260_scc_serial_initialize);
+serial_initfunc(mpc8260_smc_serial_initialize);
+serial_initfunc(mpc85xx_serial_initialize);
+serial_initfunc(iop480_serial_initialize);
+serial_initfunc(leon2_serial_initialize);
+serial_initfunc(leon3_serial_initialize);
+serial_initfunc(marvell_serial_initialize);
+serial_initfunc(amirix_serial_initialize);
+serial_initfunc(bmw_serial_initialize);
+serial_initfunc(cogent_serial_initialize);
+serial_initfunc(cpci750_serial_initialize);
+serial_initfunc(evb64260_serial_initialize);
+serial_initfunc(ml2_serial_initialize);
+serial_initfunc(sconsole_serial_initialize);
+serial_initfunc(p3mx_serial_initialize);
+serial_initfunc(altera_jtag_serial_initialize);
+serial_initfunc(altera_serial_initialize);
+serial_initfunc(atmel_serial_initialize);
+serial_initfunc(lpc32xx_serial_initialize);
+serial_initfunc(mcf_serial_initialize);
+serial_initfunc(oc_serial_initialize);
+serial_initfunc(sandbox_serial_initialize);
+serial_initfunc(clps7111_serial_initialize);
+serial_initfunc(imx_serial_initialize);
+serial_initfunc(ixp_serial_initialize);
+serial_initfunc(ks8695_serial_initialize);
+serial_initfunc(lh7a40x_serial_initialize);
+serial_initfunc(max3100_serial_initialize);
+serial_initfunc(mxc_serial_initialize);
+serial_initfunc(pl01x_serial_initialize);
+serial_initfunc(s3c44b0_serial_initialize);
+serial_initfunc(sa1100_serial_initialize);
+serial_initfunc(sh_serial_initialize);
+
+/**
+ * serial_register() - Register serial driver with serial driver core
+ * @dev:       Pointer to the serial driver structure
+ *
+ * This function registers the serial driver supplied via @dev with
+ * serial driver core, thus making U-Boot aware of it and making it
+ * available for U-Boot to use. On platforms that still require manual
+ * relocation of constant variables, relocation of the supplied structure
+ * is performed.
+ */
+void serial_register(struct serial_device *dev)
 {
-       int clock_divisor;
-
-#ifdef CONFIG_NS87308
-       initialise_ns87308();
-#endif
-
-#ifdef CONFIG_SYS_NS16550_COM1
-       clock_divisor = calc_divisor(serial_ports[0]);
-       NS16550_init(serial_ports[0], clock_divisor);
-#endif
-#ifdef CONFIG_SYS_NS16550_COM2
-       clock_divisor = calc_divisor(serial_ports[1]);
-       NS16550_init(serial_ports[1], clock_divisor);
-#endif
-#ifdef CONFIG_SYS_NS16550_COM3
-       clock_divisor = calc_divisor(serial_ports[2]);
-       NS16550_init(serial_ports[2], clock_divisor);
-#endif
-#ifdef CONFIG_SYS_NS16550_COM4
-       clock_divisor = calc_divisor(serial_ports[3]);
-       NS16550_init(serial_ports[3], clock_divisor);
+#ifdef CONFIG_NEEDS_MANUAL_RELOC
+       if (dev->start)
+               dev->start += gd->reloc_off;
+       if (dev->stop)
+               dev->stop += gd->reloc_off;
+       if (dev->setbrg)
+               dev->setbrg += gd->reloc_off;
+       if (dev->getc)
+               dev->getc += gd->reloc_off;
+       if (dev->tstc)
+               dev->tstc += gd->reloc_off;
+       if (dev->putc)
+               dev->putc += gd->reloc_off;
+       if (dev->puts)
+               dev->puts += gd->reloc_off;
 #endif
 
-       return (0);
+       dev->next = serial_devices;
+       serial_devices = dev;
 }
-#endif
 
-void
-_serial_putc(const char c,const int port)
+/**
+ * serial_initialize() - Register all compiled-in serial port drivers
+ *
+ * This function registers all serial port drivers that are compiled
+ * into the U-Boot binary with the serial core, thus making them
+ * available to U-Boot to use. Lastly, this function assigns a default
+ * serial port to the serial core. That serial port is then used as a
+ * default output.
+ */
+void serial_initialize(void)
 {
-       if (c == '\n')
-               NS16550_putc(PORT, '\r');
-
-       NS16550_putc(PORT, c);
+       mpc8xx_serial_initialize();
+       ns16550_serial_initialize();
+       pxa_serial_initialize();
+       s3c24xx_serial_initialize();
+       s5p_serial_initialize();
+       mpc512x_serial_initialize();
+       bfin_serial_initialize();
+       bfin_jtag_initialize();
+       uartlite_serial_initialize();
+       zynq_serial_initalize();
+       au1x00_serial_initialize();
+       asc_serial_initialize();
+       jz_serial_initialize();
+       mpc5xx_serial_initialize();
+       mpc8260_scc_serial_initialize();
+       mpc8260_smc_serial_initialize();
+       mpc85xx_serial_initialize();
+       iop480_serial_initialize();
+       leon2_serial_initialize();
+       leon3_serial_initialize();
+       marvell_serial_initialize();
+       amirix_serial_initialize();
+       bmw_serial_initialize();
+       cogent_serial_initialize();
+       cpci750_serial_initialize();
+       evb64260_serial_initialize();
+       ml2_serial_initialize();
+       sconsole_serial_initialize();
+       p3mx_serial_initialize();
+       altera_jtag_serial_initialize();
+       altera_serial_initialize();
+       atmel_serial_initialize();
+       lpc32xx_serial_initialize();
+       mcf_serial_initialize();
+       oc_serial_initialize();
+       sandbox_serial_initialize();
+       clps7111_serial_initialize();
+       imx_serial_initialize();
+       ixp_serial_initialize();
+       ks8695_serial_initialize();
+       lh7a40x_serial_initialize();
+       max3100_serial_initialize();
+       mxc_serial_initialize();
+       pl01x_serial_initialize();
+       s3c44b0_serial_initialize();
+       sa1100_serial_initialize();
+       sh_serial_initialize();
+
+       serial_assign(default_serial_console()->name);
 }
 
-void
-_serial_putc_raw(const char c,const int port)
+/**
+ * serial_stdio_init() - Register serial ports with STDIO core
+ *
+ * This function generates a proxy driver for each serial port driver.
+ * These proxy drivers then register with the STDIO core, making the
+ * serial drivers available as STDIO devices.
+ */
+void serial_stdio_init(void)
 {
-       NS16550_putc(PORT, c);
-}
+       struct stdio_dev dev;
+       struct serial_device *s = serial_devices;
 
-void
-_serial_puts (const char *s,const int port)
-{
-       while (*s) {
-               _serial_putc (*s++,port);
-       }
-}
+       while (s) {
+               memset(&dev, 0, sizeof(dev));
 
+               strcpy(dev.name, s->name);
+               dev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT;
 
-int
-_serial_getc(const int port)
-{
-       return NS16550_getc(PORT);
+               dev.start = s->start;
+               dev.stop = s->stop;
+               dev.putc = s->putc;
+               dev.puts = s->puts;
+               dev.getc = s->getc;
+               dev.tstc = s->tstc;
+
+               stdio_register(&dev);
+
+               s = s->next;
+       }
 }
 
-int
-_serial_tstc(const int port)
+/**
+ * serial_assign() - Select the serial output device by name
+ * @name:      Name of the serial driver to be used as default output
+ *
+ * This function configures the serial output multiplexing by
+ * selecting which serial device will be used as default. In case
+ * the STDIO "serial" device is selected as stdin/stdout/stderr,
+ * the serial device previously configured by this function will be
+ * used for the particular operation.
+ *
+ * Returns 0 on success, negative on error.
+ */
+int serial_assign(const char *name)
 {
-       return NS16550_tstc(PORT);
+       struct serial_device *s;
+
+       for (s = serial_devices; s; s = s->next) {
+               if (strcmp(s->name, name))
+                       continue;
+               serial_current = s;
+               return 0;
+       }
+
+       return -EINVAL;
 }
 
-void
-_serial_setbrg (const int port)
+/**
+ * serial_reinit_all() - Reinitialize all compiled-in serial ports
+ *
+ * This function reinitializes all serial ports that are compiled
+ * into U-Boot by calling their serial_start() functions.
+ */
+void serial_reinit_all(void)
 {
-       int clock_divisor;
+       struct serial_device *s;
 
-       clock_divisor = calc_divisor(PORT);
-       NS16550_reinit(PORT, clock_divisor);
+       for (s = serial_devices; s; s = s->next)
+               s->start();
 }
 
-#if defined(CONFIG_SERIAL_MULTI)
-static inline void
-serial_putc_dev(unsigned int dev_index,const char c)
+/**
+ * get_current() - Return pointer to currently selected serial port
+ *
+ * This function returns a pointer to currently selected serial port.
+ * The currently selected serial port is altered by serial_assign()
+ * function.
+ *
+ * In case this function is called before relocation or before any serial
+ * port is configured, this function calls default_serial_console() to
+ * determine the serial port. Otherwise, the configured serial port is
+ * returned.
+ *
+ * Returns pointer to the currently selected serial port on success,
+ * NULL on error.
+ */
+static struct serial_device *get_current(void)
 {
-       _serial_putc(c,dev_index);
-}
+       struct serial_device *dev;
+
+       if (!(gd->flags & GD_FLG_RELOC))
+               dev = default_serial_console();
+       else if (!serial_current)
+               dev = default_serial_console();
+       else
+               dev = serial_current;
+
+       /* We must have a console device */
+       if (!dev) {
+#ifdef CONFIG_SPL_BUILD
+               puts("Cannot find console\n");
+               hang();
 #else
-void
-serial_putc(const char c)
-{
-       _serial_putc(c,CONFIG_CONS_INDEX);
-}
+               panic("Cannot find console\n");
 #endif
+       }
 
-#if defined(CONFIG_SERIAL_MULTI)
-static inline void
-serial_putc_raw_dev(unsigned int dev_index,const char c)
-{
-       _serial_putc_raw(c,dev_index);
+       return dev;
 }
-#else
-void
-serial_putc_raw(const char c)
-{
-       _serial_putc_raw(c,CONFIG_CONS_INDEX);
-}
-#endif
 
-#if defined(CONFIG_SERIAL_MULTI)
-static inline void
-serial_puts_dev(unsigned int dev_index,const char *s)
+/**
+ * serial_init() - Initialize currently selected serial port
+ *
+ * This function initializes the currently selected serial port. This
+ * usually involves setting up the registers of that particular port,
+ * enabling clock and such. This function uses the get_current() call
+ * to determine which port is selected.
+ *
+ * Returns 0 on success, negative on error.
+ */
+int serial_init(void)
 {
-       _serial_puts(s,dev_index);
+       return get_current()->start();
 }
-#else
-void
-serial_puts(const char *s)
+
+/**
+ * serial_setbrg() - Configure baud-rate of currently selected serial port
+ *
+ * This function configures the baud-rate of the currently selected
+ * serial port. The baud-rate is retrieved from global data within
+ * the serial port driver. This function uses the get_current() call
+ * to determine which port is selected.
+ *
+ * Returns 0 on success, negative on error.
+ */
+void serial_setbrg(void)
 {
-       _serial_puts(s,CONFIG_CONS_INDEX);
+       get_current()->setbrg();
 }
-#endif
 
-#if defined(CONFIG_SERIAL_MULTI)
-static inline int
-serial_getc_dev(unsigned int dev_index)
+/**
+ * serial_getc() - Read character from currently selected serial port
+ *
+ * This function retrieves a character from currently selected serial
+ * port. In case there is no character waiting on the serial port,
+ * this function will block and wait for the character to appear. This
+ * function uses the get_current() call to determine which port is
+ * selected.
+ *
+ * Returns the character on success, negative on error.
+ */
+int serial_getc(void)
 {
-       return _serial_getc(dev_index);
+       return get_current()->getc();
 }
-#else
-int
-serial_getc(void)
+
+/**
+ * serial_tstc() - Test if data is available on currently selected serial port
+ *
+ * This function tests if one or more characters are available on
+ * currently selected serial port. This function never blocks. This
+ * function uses the get_current() call to determine which port is
+ * selected.
+ *
+ * Returns positive if character is available, zero otherwise.
+ */
+int serial_tstc(void)
 {
-       return _serial_getc(CONFIG_CONS_INDEX);
+       return get_current()->tstc();
 }
-#endif
 
-#if defined(CONFIG_SERIAL_MULTI)
-static inline int
-serial_tstc_dev(unsigned int dev_index)
+/**
+ * serial_putc() - Output character via currently selected serial port
+ * @c: Single character to be output from the serial port.
+ *
+ * This function outputs a character via currently selected serial
+ * port. This character is passed to the serial port driver responsible
+ * for controlling the hardware. The hardware may still be in process
+ * of transmitting another character, therefore this function may block
+ * for a short amount of time. This function uses the get_current()
+ * call to determine which port is selected.
+ */
+void serial_putc(const char c)
 {
-       return _serial_tstc(dev_index);
+       get_current()->putc(c);
 }
-#else
-int
-serial_tstc(void)
+
+/**
+ * serial_puts() - Output string via currently selected serial port
+ * @s: Zero-terminated string to be output from the serial port.
+ *
+ * This function outputs a zero-terminated string via currently
+ * selected serial port. This function behaves as an accelerator
+ * in case the hardware can queue multiple characters for transfer.
+ * The whole string that is to be output is available to the function
+ * implementing the hardware manipulation. Transmitting the whole
+ * string may take some time, thus this function may block for some
+ * amount of time. This function uses the get_current() call to
+ * determine which port is selected.
+ */
+void serial_puts(const char *s)
 {
-       return _serial_tstc(CONFIG_CONS_INDEX);
+       get_current()->puts(s);
 }
-#endif
 
-#if defined(CONFIG_SERIAL_MULTI)
-static inline void
-serial_setbrg_dev(unsigned int dev_index)
+/**
+ * default_serial_puts() - Output string by calling serial_putc() in loop
+ * @s: Zero-terminated string to be output from the serial port.
+ *
+ * This function outputs a zero-terminated string by calling serial_putc()
+ * in a loop. Most drivers do not support queueing more than one byte for
+ * transfer, thus this function precisely implements their serial_puts().
+ *
+ * To optimize the number of get_current() calls, this function only
+ * calls get_current() once and then directly accesses the putc() call
+ * of the &struct serial_device .
+ */
+void default_serial_puts(const char *s)
 {
-       _serial_setbrg(dev_index);
+       struct serial_device *dev = get_current();
+       while (*s)
+               dev->putc(*s++);
 }
-#else
-void
-serial_setbrg(void)
+
+#if CONFIG_POST & CONFIG_SYS_POST_UART
+static const int bauds[] = CONFIG_SYS_BAUDRATE_TABLE;
+
+/**
+ * uart_post_test() - Test the currently selected serial port using POST
+ * @flags:     POST framework flags
+ *
+ * Do a loopback test of the currently selected serial port. This
+ * function is only useful in the context of the POST testing framwork.
+ * The serial port is firstly configured into loopback mode and then
+ * characters are sent through it.
+ *
+ * Returns 0 on success, value otherwise.
+ */
+/* Mark weak until post/cpu/.../uart.c migrate over */
+__weak
+int uart_post_test(int flags)
 {
-       _serial_setbrg(CONFIG_CONS_INDEX);
+       unsigned char c;
+       int ret, saved_baud, b;
+       struct serial_device *saved_dev, *s;
+       bd_t *bd = gd->bd;
+
+       /* Save current serial state */
+       ret = 0;
+       saved_dev = serial_current;
+       saved_baud = bd->bi_baudrate;
+
+       for (s = serial_devices; s; s = s->next) {
+               /* If this driver doesn't support loop back, skip it */
+               if (!s->loop)
+                       continue;
+
+               /* Test the next device */
+               serial_current = s;
+
+               ret = serial_init();
+               if (ret)
+                       goto done;
+
+               /* Consume anything that happens to be queued */
+               while (serial_tstc())
+                       serial_getc();
+
+               /* Enable loop back */
+               s->loop(1);
+
+               /* Test every available baud rate */
+               for (b = 0; b < ARRAY_SIZE(bauds); ++b) {
+                       bd->bi_baudrate = bauds[b];
+                       serial_setbrg();
+
+                       /*
+                        * Stick to printable chars to avoid issues:
+                        *  - terminal corruption
+                        *  - serial program reacting to sequences and sending
+                        *    back random extra data
+                        *  - most serial drivers add in extra chars (like \r\n)
+                        */
+                       for (c = 0x20; c < 0x7f; ++c) {
+                               /* Send it out */
+                               serial_putc(c);
+
+                               /* Make sure it's the same one */
+                               ret = (c != serial_getc());
+                               if (ret) {
+                                       s->loop(0);
+                                       goto done;
+                               }
+
+                               /* Clean up the output in case it was sent */
+                               serial_putc('\b');
+                               ret = ('\b' != serial_getc());
+                               if (ret) {
+                                       s->loop(0);
+                                       goto done;
+                               }
+                       }
+               }
+
+               /* Disable loop back */
+               s->loop(0);
+
+               /* XXX: There is no serial_stop() !? */
+               if (s->stop)
+                       s->stop();
+       }
+
+ done:
+       /* Restore previous serial state */
+       serial_current = saved_dev;
+       bd->bi_baudrate = saved_baud;
+       serial_reinit_all();
+       serial_setbrg();
+
+       return ret;
 }
 #endif
-
-#if defined(CONFIG_SERIAL_MULTI)
-
-DECLARE_ESERIAL_FUNCTIONS(1);
-struct serial_device eserial1_device =
-       INIT_ESERIAL_STRUCTURE(1,"eserial0","EUART1");
-DECLARE_ESERIAL_FUNCTIONS(2);
-struct serial_device eserial2_device =
-       INIT_ESERIAL_STRUCTURE(2,"eserial1","EUART2");
-DECLARE_ESERIAL_FUNCTIONS(3);
-struct serial_device eserial3_device =
-       INIT_ESERIAL_STRUCTURE(3,"eserial2","EUART3");
-DECLARE_ESERIAL_FUNCTIONS(4);
-struct serial_device eserial4_device =
-       INIT_ESERIAL_STRUCTURE(4,"eserial3","EUART4");
-#endif /* CONFIG_SERIAL_MULTI */