2004-08-02 06:48:16 +08:00
|
|
|
/*
|
|
|
|
* (C) Copyright 2004
|
|
|
|
* Wolfgang Denk, DENX Software Engineering, wd@denx.de.
|
|
|
|
*
|
|
|
|
* See file CREDITS for list of people who contributed to this
|
|
|
|
* project.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License as
|
|
|
|
* published by the Free Software Foundation; either version 2 of
|
|
|
|
* the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston,
|
|
|
|
* MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <common.h>
|
|
|
|
#include <serial.h>
|
2009-05-16 18:14:54 +08:00
|
|
|
#include <stdio_dev.h>
|
2011-05-14 14:56:15 +08:00
|
|
|
#include <post.h>
|
|
|
|
#include <linux/compiler.h>
|
2004-08-02 06:48:16 +08:00
|
|
|
|
2006-04-01 00:32:53 +08:00
|
|
|
DECLARE_GLOBAL_DATA_PTR;
|
|
|
|
|
2011-11-18 14:49:11 +08:00
|
|
|
static struct serial_device *serial_devices;
|
|
|
|
static struct serial_device *serial_current;
|
2004-08-02 06:48:16 +08:00
|
|
|
|
2011-04-30 02:03:30 +08:00
|
|
|
void serial_register(struct serial_device *dev)
|
2004-08-02 06:48:16 +08:00
|
|
|
{
|
2010-10-29 02:00:11 +08:00
|
|
|
#ifdef CONFIG_NEEDS_MANUAL_RELOC
|
2004-08-02 06:48:16 +08:00
|
|
|
dev->init += gd->reloc_off;
|
|
|
|
dev->setbrg += gd->reloc_off;
|
|
|
|
dev->getc += gd->reloc_off;
|
|
|
|
dev->tstc += gd->reloc_off;
|
|
|
|
dev->putc += gd->reloc_off;
|
|
|
|
dev->puts += gd->reloc_off;
|
2009-09-22 00:20:36 +08:00
|
|
|
#endif
|
2004-08-02 06:48:16 +08:00
|
|
|
|
|
|
|
dev->next = serial_devices;
|
|
|
|
serial_devices = dev;
|
|
|
|
}
|
|
|
|
|
2011-11-18 14:49:11 +08:00
|
|
|
void serial_initialize(void)
|
2004-08-02 06:48:16 +08:00
|
|
|
{
|
|
|
|
#if defined(CONFIG_8xx_CONS_SMC1) || defined(CONFIG_8xx_CONS_SMC2)
|
2011-11-18 14:49:11 +08:00
|
|
|
serial_register(&serial_smc_device);
|
2004-08-02 06:48:16 +08:00
|
|
|
#endif
|
2011-11-18 14:49:11 +08:00
|
|
|
#if defined(CONFIG_8xx_CONS_SCC1) || defined(CONFIG_8xx_CONS_SCC2) || \
|
|
|
|
defined(CONFIG_8xx_CONS_SCC3) || defined(CONFIG_8xx_CONS_SCC4)
|
|
|
|
serial_register(&serial_scc_device);
|
2004-08-02 06:48:16 +08:00
|
|
|
#endif
|
|
|
|
|
2008-10-16 21:01:15 +08:00
|
|
|
#if defined(CONFIG_SYS_NS16550_SERIAL)
|
|
|
|
#if defined(CONFIG_SYS_NS16550_COM1)
|
2006-08-31 05:02:10 +08:00
|
|
|
serial_register(&eserial1_device);
|
|
|
|
#endif
|
2008-10-16 21:01:15 +08:00
|
|
|
#if defined(CONFIG_SYS_NS16550_COM2)
|
2006-08-31 05:02:10 +08:00
|
|
|
serial_register(&eserial2_device);
|
|
|
|
#endif
|
2008-10-16 21:01:15 +08:00
|
|
|
#if defined(CONFIG_SYS_NS16550_COM3)
|
2006-08-31 05:02:10 +08:00
|
|
|
serial_register(&eserial3_device);
|
|
|
|
#endif
|
2008-10-16 21:01:15 +08:00
|
|
|
#if defined(CONFIG_SYS_NS16550_COM4)
|
2006-08-31 05:02:10 +08:00
|
|
|
serial_register(&eserial4_device);
|
|
|
|
#endif
|
2008-10-16 21:01:15 +08:00
|
|
|
#endif /* CONFIG_SYS_NS16550_SERIAL */
|
2011-11-18 14:49:11 +08:00
|
|
|
#if defined(CONFIG_FFUART)
|
2007-08-31 04:57:04 +08:00
|
|
|
serial_register(&serial_ffuart_device);
|
|
|
|
#endif
|
2011-11-18 14:49:11 +08:00
|
|
|
#if defined(CONFIG_BTUART)
|
2007-08-31 04:57:04 +08:00
|
|
|
serial_register(&serial_btuart_device);
|
|
|
|
#endif
|
2011-11-18 14:49:11 +08:00
|
|
|
#if defined(CONFIG_STUART)
|
2007-08-31 04:57:04 +08:00
|
|
|
serial_register(&serial_stuart_device);
|
2007-12-19 21:24:40 +08:00
|
|
|
#endif
|
|
|
|
#if defined(CONFIG_S3C2410)
|
|
|
|
serial_register(&s3c24xx_serial0_device);
|
|
|
|
serial_register(&s3c24xx_serial1_device);
|
|
|
|
serial_register(&s3c24xx_serial2_device);
|
2009-10-01 16:20:28 +08:00
|
|
|
#endif
|
2010-08-23 18:52:03 +08:00
|
|
|
#if defined(CONFIG_S5P)
|
2010-03-24 15:59:30 +08:00
|
|
|
serial_register(&s5p_serial0_device);
|
|
|
|
serial_register(&s5p_serial1_device);
|
|
|
|
serial_register(&s5p_serial2_device);
|
|
|
|
serial_register(&s5p_serial3_device);
|
2010-04-25 01:27:05 +08:00
|
|
|
#endif
|
|
|
|
#if defined(CONFIG_MPC512X)
|
|
|
|
#if defined(CONFIG_SYS_PSC1)
|
|
|
|
serial_register(&serial1_device);
|
|
|
|
#endif
|
|
|
|
#if defined(CONFIG_SYS_PSC3)
|
|
|
|
serial_register(&serial3_device);
|
|
|
|
#endif
|
|
|
|
#if defined(CONFIG_SYS_PSC4)
|
|
|
|
serial_register(&serial4_device);
|
|
|
|
#endif
|
|
|
|
#if defined(CONFIG_SYS_PSC6)
|
|
|
|
serial_register(&serial6_device);
|
|
|
|
#endif
|
2011-04-30 11:23:28 +08:00
|
|
|
#endif
|
|
|
|
#if defined(CONFIG_SYS_BFIN_UART)
|
|
|
|
serial_register_bfin_uart();
|
2007-08-31 04:57:04 +08:00
|
|
|
#endif
|
2011-09-26 05:03:08 +08:00
|
|
|
#if defined(CONFIG_XILINX_UARTLITE)
|
|
|
|
# ifdef XILINX_UARTLITE_BASEADDR
|
|
|
|
serial_register(&uartlite_serial0_device);
|
|
|
|
# endif /* XILINX_UARTLITE_BASEADDR */
|
|
|
|
# ifdef XILINX_UARTLITE_BASEADDR1
|
|
|
|
serial_register(&uartlite_serial1_device);
|
|
|
|
# endif /* XILINX_UARTLITE_BASEADDR1 */
|
|
|
|
# ifdef XILINX_UARTLITE_BASEADDR2
|
|
|
|
serial_register(&uartlite_serial2_device);
|
|
|
|
# endif /* XILINX_UARTLITE_BASEADDR2 */
|
|
|
|
# ifdef XILINX_UARTLITE_BASEADDR3
|
|
|
|
serial_register(&uartlite_serial3_device);
|
|
|
|
# endif /* XILINX_UARTLITE_BASEADDR3 */
|
|
|
|
#endif /* CONFIG_XILINX_UARTLITE */
|
2011-11-18 14:49:11 +08:00
|
|
|
serial_assign(default_serial_console()->name);
|
2004-08-02 06:48:16 +08:00
|
|
|
}
|
|
|
|
|
2011-11-18 14:49:11 +08:00
|
|
|
void serial_stdio_init(void)
|
2004-08-02 06:48:16 +08:00
|
|
|
{
|
2009-05-16 18:14:54 +08:00
|
|
|
struct stdio_dev dev;
|
2004-08-02 06:48:16 +08:00
|
|
|
struct serial_device *s = serial_devices;
|
|
|
|
|
2004-10-12 06:43:02 +08:00
|
|
|
while (s) {
|
2011-11-18 14:49:11 +08:00
|
|
|
memset(&dev, 0, sizeof(dev));
|
2004-08-02 06:48:16 +08:00
|
|
|
|
2011-11-18 14:49:11 +08:00
|
|
|
strcpy(dev.name, s->name);
|
2004-08-02 06:48:16 +08:00
|
|
|
dev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT;
|
|
|
|
|
|
|
|
dev.start = s->init;
|
2010-04-25 01:27:05 +08:00
|
|
|
dev.stop = s->uninit;
|
2004-08-02 06:48:16 +08:00
|
|
|
dev.putc = s->putc;
|
|
|
|
dev.puts = s->puts;
|
|
|
|
dev.getc = s->getc;
|
|
|
|
dev.tstc = s->tstc;
|
|
|
|
|
2011-11-18 14:49:11 +08:00
|
|
|
stdio_register(&dev);
|
2004-08-02 06:48:16 +08:00
|
|
|
|
|
|
|
s = s->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-18 14:49:11 +08:00
|
|
|
int serial_assign(char *name)
|
2004-08-02 06:48:16 +08:00
|
|
|
{
|
|
|
|
struct serial_device *s;
|
|
|
|
|
2004-10-12 06:43:02 +08:00
|
|
|
for (s = serial_devices; s; s = s->next) {
|
2011-11-18 14:49:11 +08:00
|
|
|
if (strcmp(s->name, name) == 0) {
|
2004-08-02 06:48:16 +08:00
|
|
|
serial_current = s;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2011-11-18 14:49:11 +08:00
|
|
|
void serial_reinit_all(void)
|
2004-08-02 06:48:16 +08:00
|
|
|
{
|
|
|
|
struct serial_device *s;
|
|
|
|
|
2011-11-18 14:49:11 +08:00
|
|
|
for (s = serial_devices; s; s = s->next)
|
|
|
|
s->init();
|
2004-08-02 06:48:16 +08:00
|
|
|
}
|
|
|
|
|
2011-08-19 19:09:43 +08:00
|
|
|
static struct serial_device *get_current(void)
|
2004-08-02 06:48:16 +08:00
|
|
|
{
|
2011-08-19 19:09:43 +08:00
|
|
|
struct serial_device *dev;
|
2004-10-12 06:43:02 +08:00
|
|
|
|
2011-08-19 19:09:43 +08:00
|
|
|
if (!(gd->flags & GD_FLG_RELOC) || !serial_current) {
|
|
|
|
dev = default_serial_console();
|
|
|
|
|
|
|
|
/* We must have a console device */
|
|
|
|
if (!dev)
|
|
|
|
panic("Cannot find console");
|
|
|
|
} else
|
|
|
|
dev = serial_current;
|
|
|
|
return dev;
|
|
|
|
}
|
2004-08-02 06:48:16 +08:00
|
|
|
|
2011-08-19 19:09:43 +08:00
|
|
|
int serial_init(void)
|
|
|
|
{
|
|
|
|
return get_current()->init();
|
2004-08-02 06:48:16 +08:00
|
|
|
}
|
|
|
|
|
2011-11-18 14:49:11 +08:00
|
|
|
void serial_setbrg(void)
|
2004-08-02 06:48:16 +08:00
|
|
|
{
|
2011-08-19 19:09:43 +08:00
|
|
|
get_current()->setbrg();
|
2004-08-02 06:48:16 +08:00
|
|
|
}
|
|
|
|
|
2011-11-18 14:49:11 +08:00
|
|
|
int serial_getc(void)
|
2004-08-02 06:48:16 +08:00
|
|
|
{
|
2011-08-19 19:09:43 +08:00
|
|
|
return get_current()->getc();
|
2004-08-02 06:48:16 +08:00
|
|
|
}
|
|
|
|
|
2011-11-18 14:49:11 +08:00
|
|
|
int serial_tstc(void)
|
2004-08-02 06:48:16 +08:00
|
|
|
{
|
2011-08-19 19:09:43 +08:00
|
|
|
return get_current()->tstc();
|
2004-08-02 06:48:16 +08:00
|
|
|
}
|
|
|
|
|
2011-11-18 14:49:11 +08:00
|
|
|
void serial_putc(const char c)
|
2004-08-02 06:48:16 +08:00
|
|
|
{
|
2011-08-19 19:09:43 +08:00
|
|
|
get_current()->putc(c);
|
2004-08-02 06:48:16 +08:00
|
|
|
}
|
|
|
|
|
2011-11-18 14:49:11 +08:00
|
|
|
void serial_puts(const char *s)
|
2004-08-02 06:48:16 +08:00
|
|
|
{
|
2011-08-19 19:09:43 +08:00
|
|
|
get_current()->puts(s);
|
2004-08-02 06:48:16 +08:00
|
|
|
}
|
2011-05-14 14:56:15 +08:00
|
|
|
|
|
|
|
#if CONFIG_POST & CONFIG_SYS_POST_UART
|
|
|
|
static const int bauds[] = CONFIG_SYS_BAUDRATE_TABLE;
|
|
|
|
|
|
|
|
/* Mark weak until post/cpu/.../uart.c migrate over */
|
|
|
|
__weak
|
|
|
|
int uart_post_test(int flags)
|
|
|
|
{
|
|
|
|
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_uninit() !? */
|
|
|
|
if (s->uninit)
|
|
|
|
s->uninit();
|
|
|
|
}
|
|
|
|
|
|
|
|
done:
|
|
|
|
/* Restore previous serial state */
|
|
|
|
serial_current = saved_dev;
|
|
|
|
bd->bi_baudrate = saved_baud;
|
|
|
|
serial_reinit_all();
|
|
|
|
serial_setbrg();
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif
|