2007-05-13 11:58:00 +00:00
|
|
|
/*
|
|
|
|
* SuperH SCIF device driver.
|
2008-01-17 06:53:52 +00:00
|
|
|
* Copyright (c) 2007,2008 Nobuhiro Iwamatsu
|
2007-12-27 00:52:50 +00:00
|
|
|
*
|
2007-05-13 11:58:00 +00:00
|
|
|
* 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 <asm/processor.h>
|
|
|
|
|
|
|
|
#ifdef CFG_SCIF_CONSOLE
|
|
|
|
|
|
|
|
#if defined (CONFIG_CONS_SCIF0)
|
|
|
|
#define SCIF_BASE SCIF0_BASE
|
|
|
|
#elif defined (CONFIG_CONS_SCIF1)
|
|
|
|
#define SCIF_BASE SCIF1_BASE
|
2008-06-06 07:16:08 +00:00
|
|
|
#elif defined (CONFIG_CONS_SCIF2)
|
|
|
|
#define SCIF_BASE SCIF2_BASE
|
2007-05-13 11:58:00 +00:00
|
|
|
#else
|
|
|
|
#error "Default SCIF doesn't set....."
|
|
|
|
#endif
|
|
|
|
|
2008-01-15 14:25:25 +00:00
|
|
|
/* Base register */
|
|
|
|
#define SCSMR (vu_short *)(SCIF_BASE + 0x0)
|
|
|
|
#define SCBRR (vu_char *)(SCIF_BASE + 0x4)
|
|
|
|
#define SCSCR (vu_short *)(SCIF_BASE + 0x8)
|
|
|
|
#define SCFCR (vu_short *)(SCIF_BASE + 0x18)
|
|
|
|
#define SCFDR (vu_short *)(SCIF_BASE + 0x1C)
|
2008-06-06 07:16:08 +00:00
|
|
|
#ifdef CONFIG_CPU_SH7720 /* SH7720 specific */
|
|
|
|
# define SCFSR (vu_short *)(SCIF_BASE + 0x14) /* SCSSR */
|
2008-03-06 05:05:53 +00:00
|
|
|
# define SCFTDR (vu_char *)(SCIF_BASE + 0x20)
|
|
|
|
# define SCFRDR (vu_char *)(SCIF_BASE + 0x24)
|
2008-01-09 05:30:02 +00:00
|
|
|
#else
|
2008-03-06 05:05:53 +00:00
|
|
|
# define SCFTDR (vu_char *)(SCIF_BASE + 0xC)
|
2008-06-06 07:16:08 +00:00
|
|
|
# define SCFSR (vu_short *)(SCIF_BASE + 0x10)
|
2008-03-06 05:05:53 +00:00
|
|
|
# define SCFRDR (vu_char *)(SCIF_BASE + 0x14)
|
2008-01-09 05:30:02 +00:00
|
|
|
#endif
|
|
|
|
|
2008-06-06 07:16:08 +00:00
|
|
|
#if defined(CONFIG_CPU_SH7780) || \
|
2008-03-05 05:23:26 +00:00
|
|
|
defined(CONFIG_CPU_SH7785)
|
2008-03-06 05:05:53 +00:00
|
|
|
# define SCRFDR (vu_short *)(SCIF_BASE + 0x20)
|
|
|
|
# define SCSPTR (vu_short *)(SCIF_BASE + 0x24)
|
2008-05-20 14:00:29 +00:00
|
|
|
# define SCLSR (vu_short *)(SCIF_BASE + 0x28)
|
2008-03-06 05:05:53 +00:00
|
|
|
# define SCRER (vu_short *)(SCIF_BASE + 0x2C)
|
|
|
|
# define LSR_ORER 1
|
|
|
|
# define FIFOLEVEL_MASK 0xFF
|
2008-06-06 07:16:08 +00:00
|
|
|
#elif defined(CONFIG_CPU_SH7763)
|
|
|
|
# if defined (CONFIG_CONS_SCIF2)
|
|
|
|
# define SCSPTR (vu_short *)(SCIF_BASE + 0x20)
|
|
|
|
# define SCLSR (vu_short *)(SCIF_BASE + 0x24)
|
|
|
|
# define LSR_ORER 1
|
|
|
|
# define FIFOLEVEL_MASK 0x1F
|
|
|
|
# else
|
|
|
|
# define SCRFDR (vu_short *)(SCIF_BASE + 0x20)
|
|
|
|
# define SCSPTR (vu_short *)(SCIF_BASE + 0x24)
|
|
|
|
# define SCLSR (vu_short *)(SCIF_BASE + 0x28)
|
|
|
|
# define SCRER (vu_short *)(SCIF_BASE + 0x2C)
|
|
|
|
# define LSR_ORER 1
|
|
|
|
# define FIFOLEVEL_MASK 0xFF
|
|
|
|
# endif
|
2008-03-05 05:23:26 +00:00
|
|
|
#elif defined(CONFIG_CPU_SH7750) || \
|
2008-03-12 03:10:28 +00:00
|
|
|
defined(CONFIG_CPU_SH7751) || \
|
2008-03-05 05:23:26 +00:00
|
|
|
defined(CONFIG_CPU_SH7722)
|
2008-05-20 14:00:29 +00:00
|
|
|
# define SCSPTR (vu_short *)(SCIF_BASE + 0x20)
|
2008-06-06 07:16:08 +00:00
|
|
|
# define SCLSR (vu_short *)(SCIF_BASE + 0x24)
|
2008-03-06 05:05:53 +00:00
|
|
|
# define LSR_ORER 1
|
|
|
|
# define FIFOLEVEL_MASK 0x1F
|
2008-03-05 05:23:26 +00:00
|
|
|
#elif defined(CONFIG_CPU_SH7720)
|
2008-06-06 07:16:08 +00:00
|
|
|
# define SCLSR (vu_short *)(SCIF_BASE + 0x24)
|
2008-03-06 05:05:53 +00:00
|
|
|
# define LSR_ORER 0x0200
|
|
|
|
# define FIFOLEVEL_MASK 0x1F
|
2008-06-06 07:16:08 +00:00
|
|
|
#elif defined(CONFIG_CPU_SH7710) || \
|
2008-03-05 05:23:26 +00:00
|
|
|
defined(CONFIG_CPU_SH7712)
|
2008-06-06 07:16:08 +00:00
|
|
|
# define SCLSR SCFSR /* SCSSR */
|
2008-03-06 05:05:53 +00:00
|
|
|
# define LSR_ORER 1
|
|
|
|
# define FIFOLEVEL_MASK 0x1F
|
2008-01-15 14:25:25 +00:00
|
|
|
#endif
|
2008-01-17 06:53:52 +00:00
|
|
|
|
2008-03-05 05:23:26 +00:00
|
|
|
/* SCBRR register value setting */
|
2008-01-17 06:53:52 +00:00
|
|
|
#if defined(CONFIG_CPU_SH7720)
|
2008-03-06 05:05:53 +00:00
|
|
|
# define SCBRR_VALUE(bps, clk) (((clk*2)+16*bps)/(32*bps)-1)
|
2008-06-06 07:16:08 +00:00
|
|
|
#else /* Generic SuperH */
|
2008-03-06 05:05:53 +00:00
|
|
|
# define SCBRR_VALUE(bps, clk) ((clk+16*bps)/(32*bps)-1)
|
2007-05-13 11:58:00 +00:00
|
|
|
#endif
|
|
|
|
|
2008-06-06 07:16:08 +00:00
|
|
|
#define SCR_RE (1 << 4)
|
|
|
|
#define SCR_TE (1 << 5)
|
|
|
|
#define FCR_RFRST (1 << 1) /* RFCL */
|
|
|
|
#define FCR_TFRST (1 << 2) /* TFCL */
|
|
|
|
#define FSR_DR (1 << 0)
|
|
|
|
#define FSR_RDF (1 << 1)
|
|
|
|
#define FSR_FER (1 << 3)
|
|
|
|
#define FSR_BRK (1 << 4)
|
|
|
|
#define FSR_FER (1 << 3)
|
|
|
|
#define FSR_TEND (1 << 6)
|
|
|
|
#define FSR_ER (1 << 7)
|
2007-05-13 11:58:00 +00:00
|
|
|
|
|
|
|
/*----------------------------------------------------------------------*/
|
|
|
|
|
2008-06-06 07:16:08 +00:00
|
|
|
void serial_setbrg(void)
|
2007-05-13 11:58:00 +00:00
|
|
|
{
|
|
|
|
DECLARE_GLOBAL_DATA_PTR;
|
2008-06-06 07:16:08 +00:00
|
|
|
*SCBRR = SCBRR_VALUE(gd->baudrate, CONFIG_SYS_CLK_FREQ);
|
2007-05-13 11:58:00 +00:00
|
|
|
}
|
|
|
|
|
2008-06-06 07:16:08 +00:00
|
|
|
int serial_init(void)
|
2007-05-13 11:58:00 +00:00
|
|
|
{
|
|
|
|
*SCSCR = (SCR_RE | SCR_TE);
|
2008-06-06 07:16:08 +00:00
|
|
|
*SCSMR = 0;
|
2007-05-13 11:58:00 +00:00
|
|
|
*SCSMR = 0;
|
|
|
|
*SCFCR = (FCR_RFRST | FCR_TFRST);
|
|
|
|
*SCFCR;
|
|
|
|
*SCFCR = 0;
|
|
|
|
|
|
|
|
serial_setbrg();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-06-06 07:16:08 +00:00
|
|
|
static int serial_rx_fifo_level(void)
|
2007-05-13 11:58:00 +00:00
|
|
|
{
|
2008-06-06 07:16:08 +00:00
|
|
|
#if defined(SCRFDR)
|
2008-03-06 05:05:53 +00:00
|
|
|
return (*SCRFDR >> 0) & FIFOLEVEL_MASK;
|
|
|
|
#else
|
|
|
|
return (*SCFDR >> 0) & FIFOLEVEL_MASK;
|
|
|
|
#endif
|
2007-05-13 11:58:00 +00:00
|
|
|
}
|
|
|
|
|
2008-06-06 07:16:08 +00:00
|
|
|
void serial_raw_putc(const char c)
|
2007-05-13 11:58:00 +00:00
|
|
|
{
|
|
|
|
unsigned int fsr_bits_to_clear;
|
|
|
|
|
|
|
|
while (1) {
|
2008-06-06 07:16:08 +00:00
|
|
|
if (*SCFSR & FSR_TEND) { /* Tx fifo is empty */
|
2007-05-13 11:58:00 +00:00
|
|
|
fsr_bits_to_clear = FSR_TEND;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*SCFTDR = c;
|
|
|
|
if (fsr_bits_to_clear != 0)
|
|
|
|
*SCFSR &= ~fsr_bits_to_clear;
|
|
|
|
}
|
|
|
|
|
2008-06-06 07:16:08 +00:00
|
|
|
void 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
|
|
|
}
|
|
|
|
|
2008-06-06 07:16:08 +00:00
|
|
|
void serial_puts(const char *s)
|
2007-05-13 11:58:00 +00:00
|
|
|
{
|
|
|
|
char c;
|
|
|
|
while ((c = *s++) != 0)
|
2008-06-06 07:16:08 +00:00
|
|
|
serial_putc(c);
|
2007-05-13 11:58:00 +00:00
|
|
|
}
|
|
|
|
|
2008-06-06 07:16:08 +00:00
|
|
|
int serial_tstc(void)
|
2007-05-13 11:58:00 +00:00
|
|
|
{
|
2008-06-06 07:16:08 +00:00
|
|
|
return serial_rx_fifo_level()? 1 : 0;
|
2007-05-13 11:58:00 +00:00
|
|
|
}
|
|
|
|
|
2008-06-06 07:16:08 +00:00
|
|
|
#define FSR_ERR_CLEAR 0x0063
|
|
|
|
#define RDRF_CLEAR 0x00fc
|
|
|
|
void handle_error(void)
|
|
|
|
{
|
2007-05-13 11:58:00 +00:00
|
|
|
|
2008-06-06 07:16:08 +00:00
|
|
|
(void)*SCFSR;
|
|
|
|
*SCFSR = FSR_ERR_CLEAR;
|
|
|
|
(void)*SCLSR;
|
|
|
|
*SCLSR = 0x00;
|
2007-05-13 11:58:00 +00:00
|
|
|
}
|
|
|
|
|
2008-06-06 07:16:08 +00:00
|
|
|
int serial_getc_check(void)
|
|
|
|
{
|
2007-05-13 11:58:00 +00:00
|
|
|
unsigned short status;
|
|
|
|
|
2008-06-06 07:16:08 +00:00
|
|
|
status = *SCFSR;
|
2007-05-13 11:58:00 +00:00
|
|
|
|
2008-06-06 07:16:08 +00:00
|
|
|
if (status & (FSR_FER | FSR_ER | FSR_BRK))
|
2007-05-13 11:58:00 +00:00
|
|
|
handle_error();
|
2008-06-06 07:16:08 +00:00
|
|
|
if (*SCLSR & LSR_ORER)
|
2007-05-13 11:58:00 +00:00
|
|
|
handle_error();
|
2008-06-06 07:16:08 +00:00
|
|
|
return (status & (FSR_DR | FSR_RDF));
|
2007-05-13 11:58:00 +00:00
|
|
|
}
|
|
|
|
|
2008-06-06 07:16:08 +00:00
|
|
|
int 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-06-06 07:16:08 +00:00
|
|
|
while (!serial_getc_check()) ;
|
2007-05-13 11:58:00 +00:00
|
|
|
|
|
|
|
ch = *SCFRDR;
|
2008-06-06 07:16:08 +00:00
|
|
|
status = *SCFSR;
|
2007-05-13 11:58:00 +00:00
|
|
|
|
2008-06-06 07:16:08 +00:00
|
|
|
*SCFSR = RDRF_CLEAR;
|
2007-05-13 11:58:00 +00:00
|
|
|
|
|
|
|
if (status & (FSR_FER | FSR_FER | FSR_ER | FSR_BRK))
|
|
|
|
handle_error();
|
|
|
|
|
2008-06-06 07:16:08 +00:00
|
|
|
if (*SCLSR & LSR_ORER)
|
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
|
|
|
}
|
|
|
|
|
2008-06-06 07:16:08 +00:00
|
|
|
#endif /* CFG_SCIF_CONSOLE */
|