2007-05-13 11:58:00 +00:00
|
|
|
|
/*
|
|
|
|
|
* SuperH SCIF device driver.
|
2013-07-23 04:58:20 +00:00
|
|
|
|
* Copyright (C) 2013 Renesas Electronics Corporation
|
2010-10-25 18:55:15 +00:00
|
|
|
|
* Copyright (C) 2007,2008,2010 Nobuhiro Iwamatsu
|
|
|
|
|
* Copyright (C) 2002 - 2008 Paul Mundt
|
2007-12-27 00:52:50 +00:00
|
|
|
|
*
|
2013-07-08 07:37:19 +00:00
|
|
|
|
* SPDX-License-Identifier: GPL-2.0+
|
2007-05-13 11:58:00 +00:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <common.h>
|
2009-01-11 15:35:16 +00:00
|
|
|
|
#include <asm/io.h>
|
2007-05-13 11:58:00 +00:00
|
|
|
|
#include <asm/processor.h>
|
2010-10-25 18:55:15 +00:00
|
|
|
|
#include "serial_sh.h"
|
2012-09-14 20:40:08 +00:00
|
|
|
|
#include <serial.h>
|
|
|
|
|
#include <linux/compiler.h>
|
2010-12-21 01:27:51 +00:00
|
|
|
|
|
2008-08-22 08:48:51 +00:00
|
|
|
|
#if defined(CONFIG_CONS_SCIF0)
|
|
|
|
|
# define SCIF_BASE SCIF0_BASE
|
|
|
|
|
#elif defined(CONFIG_CONS_SCIF1)
|
|
|
|
|
# define SCIF_BASE SCIF1_BASE
|
|
|
|
|
#elif defined(CONFIG_CONS_SCIF2)
|
|
|
|
|
# define SCIF_BASE SCIF2_BASE
|
|
|
|
|
#elif defined(CONFIG_CONS_SCIF3)
|
|
|
|
|
# define SCIF_BASE SCIF3_BASE
|
|
|
|
|
#elif defined(CONFIG_CONS_SCIF4)
|
|
|
|
|
# define SCIF_BASE SCIF4_BASE
|
|
|
|
|
#elif defined(CONFIG_CONS_SCIF5)
|
|
|
|
|
# define SCIF_BASE SCIF5_BASE
|
2012-05-15 22:15:51 +00:00
|
|
|
|
#elif defined(CONFIG_CONS_SCIF6)
|
|
|
|
|
# define SCIF_BASE SCIF6_BASE
|
|
|
|
|
#elif defined(CONFIG_CONS_SCIF7)
|
|
|
|
|
# define SCIF_BASE SCIF7_BASE
|
2007-05-13 11:58:00 +00:00
|
|
|
|
#else
|
2008-08-22 08:48:51 +00:00
|
|
|
|
# error "Default SCIF doesn't set....."
|
2007-05-13 11:58:00 +00:00
|
|
|
|
#endif
|
|
|
|
|
|
2010-10-25 18:55:15 +00:00
|
|
|
|
#if defined(CONFIG_SCIF_A)
|
|
|
|
|
#define SCIF_BASE_PORT PORT_SCIFA
|
2008-01-09 05:30:02 +00:00
|
|
|
|
#else
|
2010-10-25 18:55:15 +00:00
|
|
|
|
#define SCIF_BASE_PORT PORT_SCIF
|
2008-01-09 05:30:02 +00:00
|
|
|
|
#endif
|
|
|
|
|
|
2010-10-25 18:55:15 +00:00
|
|
|
|
static struct uart_port sh_sci = {
|
|
|
|
|
.membase = (unsigned char*)SCIF_BASE,
|
|
|
|
|
.mapbase = SCIF_BASE,
|
|
|
|
|
.type = SCIF_BASE_PORT,
|
|
|
|
|
};
|
2007-05-13 11:58:00 +00:00
|
|
|
|
|
2012-09-14 20:40:08 +00:00
|
|
|
|
static void sh_serial_setbrg(void)
|
2007-05-13 11:58:00 +00:00
|
|
|
|
{
|
2010-10-25 18:55:15 +00:00
|
|
|
|
DECLARE_GLOBAL_DATA_PTR;
|
2014-07-28 06:07:44 +00:00
|
|
|
|
#ifdef CONFIG_SCIF_USE_EXT_CLK
|
|
|
|
|
unsigned short dl = DL_VALUE(gd->baudrate, CONFIG_SH_SCIF_CLK_FREQ);
|
|
|
|
|
sci_out(&sh_sci, DL, dl);
|
|
|
|
|
/* Need wait: Clock * 1/dl $B!_(B 1/16 */
|
|
|
|
|
udelay((1000000 * dl * 16 / CONFIG_SYS_CLK_FREQ) * 1000 + 1);
|
|
|
|
|
#else
|
2013-08-20 01:31:53 +00:00
|
|
|
|
sci_out(&sh_sci, SCBRR,
|
|
|
|
|
SCBRR_VALUE(gd->baudrate, CONFIG_SH_SCIF_CLK_FREQ));
|
2014-07-28 06:07:44 +00:00
|
|
|
|
#endif
|
2007-05-13 11:58:00 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-09-14 20:40:08 +00:00
|
|
|
|
static int sh_serial_init(void)
|
2007-05-13 11:58:00 +00:00
|
|
|
|
{
|
2010-10-25 18:55:15 +00:00
|
|
|
|
sci_out(&sh_sci, SCSCR , SCSCR_INIT(&sh_sci));
|
|
|
|
|
sci_out(&sh_sci, SCSCR , SCSCR_INIT(&sh_sci));
|
|
|
|
|
sci_out(&sh_sci, SCSMR, 0);
|
|
|
|
|
sci_out(&sh_sci, SCSMR, 0);
|
|
|
|
|
sci_out(&sh_sci, SCFCR, SCFCR_RFRST|SCFCR_TFRST);
|
|
|
|
|
sci_in(&sh_sci, SCFCR);
|
|
|
|
|
sci_out(&sh_sci, SCFCR, 0);
|
2007-05-13 11:58:00 +00:00
|
|
|
|
|
|
|
|
|
serial_setbrg();
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2010-10-25 18:55:15 +00:00
|
|
|
|
#if defined(CONFIG_CPU_SH7760) || \
|
|
|
|
|
defined(CONFIG_CPU_SH7780) || \
|
|
|
|
|
defined(CONFIG_CPU_SH7785) || \
|
|
|
|
|
defined(CONFIG_CPU_SH7786)
|
|
|
|
|
static int scif_rxfill(struct uart_port *port)
|
|
|
|
|
{
|
|
|
|
|
return sci_in(port, SCRFDR) & 0xff;
|
|
|
|
|
}
|
|
|
|
|
#elif defined(CONFIG_CPU_SH7763)
|
|
|
|
|
static int scif_rxfill(struct uart_port *port)
|
2007-05-13 11:58:00 +00:00
|
|
|
|
{
|
2010-10-25 18:55:15 +00:00
|
|
|
|
if ((port->mapbase == 0xffe00000) ||
|
|
|
|
|
(port->mapbase == 0xffe08000)) {
|
|
|
|
|
/* SCIF0/1*/
|
|
|
|
|
return sci_in(port, SCRFDR) & 0xff;
|
|
|
|
|
} else {
|
|
|
|
|
/* SCIF2 */
|
|
|
|
|
return sci_in(port, SCFDR) & SCIF2_RFDC_MASK;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#elif defined(CONFIG_ARCH_SH7372)
|
|
|
|
|
static int scif_rxfill(struct uart_port *port)
|
|
|
|
|
{
|
|
|
|
|
if (port->type == PORT_SCIFA)
|
|
|
|
|
return sci_in(port, SCFDR) & SCIF_RFDC_MASK;
|
|
|
|
|
else
|
|
|
|
|
return sci_in(port, SCRFDR);
|
|
|
|
|
}
|
2008-03-06 05:05:53 +00:00
|
|
|
|
#else
|
2010-10-25 18:55:15 +00:00
|
|
|
|
static int scif_rxfill(struct uart_port *port)
|
|
|
|
|
{
|
|
|
|
|
return sci_in(port, SCFDR) & SCIF_RFDC_MASK;
|
|
|
|
|
}
|
2008-03-06 05:05:53 +00:00
|
|
|
|
#endif
|
2010-10-25 18:55:15 +00:00
|
|
|
|
|
|
|
|
|
static int serial_rx_fifo_level(void)
|
|
|
|
|
{
|
|
|
|
|
return scif_rxfill(&sh_sci);
|
2007-05-13 11:58:00 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-11-19 21:37:38 +00:00
|
|
|
|
static void handle_error(void)
|
|
|
|
|
{
|
|
|
|
|
sci_in(&sh_sci, SCxSR);
|
|
|
|
|
sci_out(&sh_sci, SCxSR, SCxSR_ERROR_CLEAR(&sh_sci));
|
|
|
|
|
sci_in(&sh_sci, SCLSR);
|
|
|
|
|
sci_out(&sh_sci, SCLSR, 0x00);
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-08 20:57:44 +00:00
|
|
|
|
static void serial_raw_putc(const char c)
|
2007-05-13 11:58:00 +00:00
|
|
|
|
{
|
|
|
|
|
while (1) {
|
2010-10-25 18:55:15 +00:00
|
|
|
|
/* Tx fifo is empty */
|
|
|
|
|
if (sci_in(&sh_sci, SCxSR) & SCxSR_TEND(&sh_sci))
|
2007-05-13 11:58:00 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2010-10-25 18:55:15 +00:00
|
|
|
|
sci_out(&sh_sci, SCxTDR, c);
|
|
|
|
|
sci_out(&sh_sci, SCxSR, sci_in(&sh_sci, SCxSR) & ~SCxSR_TEND(&sh_sci));
|
2007-05-13 11:58:00 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-09-14 20:40:08 +00:00
|
|
|
|
static void sh_serial_putc(const char c)
|
2007-05-13 11:58:00 +00:00
|
|
|
|
{
|
|
|
|
|
if (c == '\n')
|
2008-06-06 07:16:08 +00:00
|
|
|
|
serial_raw_putc('\r');
|
|
|
|
|
serial_raw_putc(c);
|
2007-05-13 11:58:00 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-09-14 20:40:08 +00:00
|
|
|
|
static int sh_serial_tstc(void)
|
2007-05-13 11:58:00 +00:00
|
|
|
|
{
|
2012-11-19 21:37:38 +00:00
|
|
|
|
if (sci_in(&sh_sci, SCxSR) & SCIF_ERRORS) {
|
|
|
|
|
handle_error();
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2008-08-22 08:48:51 +00:00
|
|
|
|
return serial_rx_fifo_level() ? 1 : 0;
|
2007-05-13 11:58:00 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2014-10-08 20:57:44 +00:00
|
|
|
|
static int serial_getc_check(void)
|
2008-06-06 07:16:08 +00:00
|
|
|
|
{
|
2007-05-13 11:58:00 +00:00
|
|
|
|
unsigned short status;
|
|
|
|
|
|
2010-10-25 18:55:15 +00:00
|
|
|
|
status = sci_in(&sh_sci, SCxSR);
|
2007-05-13 11:58:00 +00:00
|
|
|
|
|
2010-10-25 18:55:15 +00:00
|
|
|
|
if (status & SCIF_ERRORS)
|
2007-05-13 11:58:00 +00:00
|
|
|
|
handle_error();
|
2010-10-25 18:55:15 +00:00
|
|
|
|
if (sci_in(&sh_sci, SCLSR) & SCxSR_ORER(&sh_sci))
|
2007-05-13 11:58:00 +00:00
|
|
|
|
handle_error();
|
2010-10-25 18:55:15 +00:00
|
|
|
|
return status & (SCIF_DR | SCxSR_RDxF(&sh_sci));
|
2007-05-13 11:58:00 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-09-14 20:40:08 +00:00
|
|
|
|
static int sh_serial_getc(void)
|
2007-05-13 11:58:00 +00:00
|
|
|
|
{
|
2008-06-06 07:16:08 +00:00
|
|
|
|
unsigned short status;
|
2007-05-13 11:58:00 +00:00
|
|
|
|
char ch;
|
2008-08-22 08:48:51 +00:00
|
|
|
|
|
|
|
|
|
while (!serial_getc_check())
|
|
|
|
|
;
|
2007-05-13 11:58:00 +00:00
|
|
|
|
|
2010-10-25 18:55:15 +00:00
|
|
|
|
ch = sci_in(&sh_sci, SCxRDR);
|
|
|
|
|
status = sci_in(&sh_sci, SCxSR);
|
2007-05-13 11:58:00 +00:00
|
|
|
|
|
2010-10-25 18:55:15 +00:00
|
|
|
|
sci_out(&sh_sci, SCxSR, SCxSR_RDxF_CLEAR(&sh_sci));
|
2007-05-13 11:58:00 +00:00
|
|
|
|
|
2010-10-25 18:55:15 +00:00
|
|
|
|
if (status & SCIF_ERRORS)
|
|
|
|
|
handle_error();
|
2007-05-13 11:58:00 +00:00
|
|
|
|
|
2010-10-25 18:55:15 +00:00
|
|
|
|
if (sci_in(&sh_sci, SCLSR) & SCxSR_ORER(&sh_sci))
|
2007-05-13 11:58:00 +00:00
|
|
|
|
handle_error();
|
2008-06-06 07:16:08 +00:00
|
|
|
|
return ch;
|
2007-05-13 11:58:00 +00:00
|
|
|
|
}
|
2012-09-14 20:40:08 +00:00
|
|
|
|
|
|
|
|
|
static struct serial_device sh_serial_drv = {
|
|
|
|
|
.name = "sh_serial",
|
|
|
|
|
.start = sh_serial_init,
|
|
|
|
|
.stop = NULL,
|
|
|
|
|
.setbrg = sh_serial_setbrg,
|
|
|
|
|
.putc = sh_serial_putc,
|
2012-10-06 14:07:02 +00:00
|
|
|
|
.puts = default_serial_puts,
|
2012-09-14 20:40:08 +00:00
|
|
|
|
.getc = sh_serial_getc,
|
|
|
|
|
.tstc = sh_serial_tstc,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
void sh_serial_initialize(void)
|
|
|
|
|
{
|
|
|
|
|
serial_register(&sh_serial_drv);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
__weak struct serial_device *default_serial_console(void)
|
|
|
|
|
{
|
|
|
|
|
return &sh_serial_drv;
|
|
|
|
|
}
|