blob: 46600e65874e33bad5df70747e720f44d41c2f72 [file] [log] [blame]
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +09001/*
2 * SuperH SCIF device driver.
Nobuhiro Iwamatsu30bf52c2008-01-17 15:53:52 +09003 * Copyright (c) 2007,2008 Nobuhiro Iwamatsu
Wolfgang Denk0a5c2142007-12-27 01:52:50 +01004 *
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +09005 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20#include <common.h>
21#include <asm/processor.h>
22
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +090023#if defined (CONFIG_CONS_SCIF0)
24#define SCIF_BASE SCIF0_BASE
25#elif defined (CONFIG_CONS_SCIF1)
26#define SCIF_BASE SCIF1_BASE
Nobuhiro Iwamatsu6564b1a2008-06-06 16:16:08 +090027#elif defined (CONFIG_CONS_SCIF2)
28#define SCIF_BASE SCIF2_BASE
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +090029#else
30#error "Default SCIF doesn't set....."
31#endif
32
Nobuhiro Iwamatsufb3205b2008-01-15 23:25:25 +090033/* Base register */
34#define SCSMR (vu_short *)(SCIF_BASE + 0x0)
35#define SCBRR (vu_char *)(SCIF_BASE + 0x4)
36#define SCSCR (vu_short *)(SCIF_BASE + 0x8)
37#define SCFCR (vu_short *)(SCIF_BASE + 0x18)
38#define SCFDR (vu_short *)(SCIF_BASE + 0x1C)
Nobuhiro Iwamatsu6564b1a2008-06-06 16:16:08 +090039#ifdef CONFIG_CPU_SH7720 /* SH7720 specific */
40# define SCFSR (vu_short *)(SCIF_BASE + 0x14) /* SCSSR */
Nobuhiro Iwamatsu1b36beb2008-03-06 14:05:53 +090041# define SCFTDR (vu_char *)(SCIF_BASE + 0x20)
42# define SCFRDR (vu_char *)(SCIF_BASE + 0x24)
Yoshihiro Shimodae6258342008-01-09 14:30:02 +090043#else
Nobuhiro Iwamatsu1b36beb2008-03-06 14:05:53 +090044# define SCFTDR (vu_char *)(SCIF_BASE + 0xC)
Nobuhiro Iwamatsu6564b1a2008-06-06 16:16:08 +090045# define SCFSR (vu_short *)(SCIF_BASE + 0x10)
Nobuhiro Iwamatsu1b36beb2008-03-06 14:05:53 +090046# define SCFRDR (vu_char *)(SCIF_BASE + 0x14)
Yoshihiro Shimodae6258342008-01-09 14:30:02 +090047#endif
48
Nobuhiro Iwamatsu6564b1a2008-06-06 16:16:08 +090049#if defined(CONFIG_CPU_SH7780) || \
Yusuke Goda54cda8b2008-03-05 14:23:26 +090050 defined(CONFIG_CPU_SH7785)
Nobuhiro Iwamatsu1b36beb2008-03-06 14:05:53 +090051# define SCRFDR (vu_short *)(SCIF_BASE + 0x20)
52# define SCSPTR (vu_short *)(SCIF_BASE + 0x24)
Wolfgang Denka1be4762008-05-20 16:00:29 +020053# define SCLSR (vu_short *)(SCIF_BASE + 0x28)
Nobuhiro Iwamatsu1b36beb2008-03-06 14:05:53 +090054# define SCRER (vu_short *)(SCIF_BASE + 0x2C)
55# define LSR_ORER 1
56# define FIFOLEVEL_MASK 0xFF
Nobuhiro Iwamatsu6564b1a2008-06-06 16:16:08 +090057#elif defined(CONFIG_CPU_SH7763)
58# if defined (CONFIG_CONS_SCIF2)
59# define SCSPTR (vu_short *)(SCIF_BASE + 0x20)
60# define SCLSR (vu_short *)(SCIF_BASE + 0x24)
61# define LSR_ORER 1
62# define FIFOLEVEL_MASK 0x1F
63# else
64# define SCRFDR (vu_short *)(SCIF_BASE + 0x20)
65# define SCSPTR (vu_short *)(SCIF_BASE + 0x24)
66# define SCLSR (vu_short *)(SCIF_BASE + 0x28)
67# define SCRER (vu_short *)(SCIF_BASE + 0x2C)
68# define LSR_ORER 1
69# define FIFOLEVEL_MASK 0xFF
70# endif
Yusuke Goda54cda8b2008-03-05 14:23:26 +090071#elif defined(CONFIG_CPU_SH7750) || \
Nobuhiro Iwamatsu53b31432008-03-12 12:10:28 +090072 defined(CONFIG_CPU_SH7751) || \
Nobuhiro Iwamatsu8e7130f2008-07-03 23:11:02 +090073 defined(CONFIG_CPU_SH7722) || \
74 defined(CONFIG_CPU_SH7203)
Wolfgang Denka1be4762008-05-20 16:00:29 +020075# define SCSPTR (vu_short *)(SCIF_BASE + 0x20)
Nobuhiro Iwamatsu6564b1a2008-06-06 16:16:08 +090076# define SCLSR (vu_short *)(SCIF_BASE + 0x24)
Nobuhiro Iwamatsu1b36beb2008-03-06 14:05:53 +090077# define LSR_ORER 1
78# define FIFOLEVEL_MASK 0x1F
Yusuke Goda54cda8b2008-03-05 14:23:26 +090079#elif defined(CONFIG_CPU_SH7720)
Nobuhiro Iwamatsu6564b1a2008-06-06 16:16:08 +090080# define SCLSR (vu_short *)(SCIF_BASE + 0x24)
Nobuhiro Iwamatsu1b36beb2008-03-06 14:05:53 +090081# define LSR_ORER 0x0200
82# define FIFOLEVEL_MASK 0x1F
Nobuhiro Iwamatsu6564b1a2008-06-06 16:16:08 +090083#elif defined(CONFIG_CPU_SH7710) || \
Yusuke Goda54cda8b2008-03-05 14:23:26 +090084 defined(CONFIG_CPU_SH7712)
Nobuhiro Iwamatsu6564b1a2008-06-06 16:16:08 +090085# define SCLSR SCFSR /* SCSSR */
Nobuhiro Iwamatsu1b36beb2008-03-06 14:05:53 +090086# define LSR_ORER 1
87# define FIFOLEVEL_MASK 0x1F
Nobuhiro Iwamatsufb3205b2008-01-15 23:25:25 +090088#endif
Nobuhiro Iwamatsu30bf52c2008-01-17 15:53:52 +090089
Yusuke Goda54cda8b2008-03-05 14:23:26 +090090/* SCBRR register value setting */
Nobuhiro Iwamatsu30bf52c2008-01-17 15:53:52 +090091#if defined(CONFIG_CPU_SH7720)
Nobuhiro Iwamatsu1b36beb2008-03-06 14:05:53 +090092# define SCBRR_VALUE(bps, clk) (((clk*2)+16*bps)/(32*bps)-1)
Nobuhiro Iwamatsu6564b1a2008-06-06 16:16:08 +090093#else /* Generic SuperH */
Nobuhiro Iwamatsu1b36beb2008-03-06 14:05:53 +090094# define SCBRR_VALUE(bps, clk) ((clk+16*bps)/(32*bps)-1)
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +090095#endif
96
Nobuhiro Iwamatsu6564b1a2008-06-06 16:16:08 +090097#define SCR_RE (1 << 4)
98#define SCR_TE (1 << 5)
99#define FCR_RFRST (1 << 1) /* RFCL */
100#define FCR_TFRST (1 << 2) /* TFCL */
101#define FSR_DR (1 << 0)
102#define FSR_RDF (1 << 1)
103#define FSR_FER (1 << 3)
104#define FSR_BRK (1 << 4)
105#define FSR_FER (1 << 3)
106#define FSR_TEND (1 << 6)
107#define FSR_ER (1 << 7)
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900108
109/*----------------------------------------------------------------------*/
110
Nobuhiro Iwamatsu6564b1a2008-06-06 16:16:08 +0900111void serial_setbrg(void)
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900112{
113 DECLARE_GLOBAL_DATA_PTR;
Nobuhiro Iwamatsu6564b1a2008-06-06 16:16:08 +0900114 *SCBRR = SCBRR_VALUE(gd->baudrate, CONFIG_SYS_CLK_FREQ);
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900115}
116
Nobuhiro Iwamatsu6564b1a2008-06-06 16:16:08 +0900117int serial_init(void)
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900118{
119 *SCSCR = (SCR_RE | SCR_TE);
Nobuhiro Iwamatsu6564b1a2008-06-06 16:16:08 +0900120 *SCSMR = 0;
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900121 *SCSMR = 0;
122 *SCFCR = (FCR_RFRST | FCR_TFRST);
123 *SCFCR;
124 *SCFCR = 0;
125
126 serial_setbrg();
127 return 0;
128}
129
Nobuhiro Iwamatsu6564b1a2008-06-06 16:16:08 +0900130static int serial_rx_fifo_level(void)
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900131{
Nobuhiro Iwamatsu6564b1a2008-06-06 16:16:08 +0900132#if defined(SCRFDR)
Nobuhiro Iwamatsu1b36beb2008-03-06 14:05:53 +0900133 return (*SCRFDR >> 0) & FIFOLEVEL_MASK;
134#else
135 return (*SCFDR >> 0) & FIFOLEVEL_MASK;
136#endif
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900137}
138
Nobuhiro Iwamatsu6564b1a2008-06-06 16:16:08 +0900139void serial_raw_putc(const char c)
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900140{
141 unsigned int fsr_bits_to_clear;
142
143 while (1) {
Nobuhiro Iwamatsu6564b1a2008-06-06 16:16:08 +0900144 if (*SCFSR & FSR_TEND) { /* Tx fifo is empty */
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900145 fsr_bits_to_clear = FSR_TEND;
146 break;
147 }
148 }
149
150 *SCFTDR = c;
151 if (fsr_bits_to_clear != 0)
152 *SCFSR &= ~fsr_bits_to_clear;
153}
154
Nobuhiro Iwamatsu6564b1a2008-06-06 16:16:08 +0900155void serial_putc(const char c)
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900156{
157 if (c == '\n')
Nobuhiro Iwamatsu6564b1a2008-06-06 16:16:08 +0900158 serial_raw_putc('\r');
159 serial_raw_putc(c);
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900160}
161
Nobuhiro Iwamatsu6564b1a2008-06-06 16:16:08 +0900162void serial_puts(const char *s)
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900163{
164 char c;
165 while ((c = *s++) != 0)
Nobuhiro Iwamatsu6564b1a2008-06-06 16:16:08 +0900166 serial_putc(c);
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900167}
168
Nobuhiro Iwamatsu6564b1a2008-06-06 16:16:08 +0900169int serial_tstc(void)
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900170{
Nobuhiro Iwamatsu6564b1a2008-06-06 16:16:08 +0900171 return serial_rx_fifo_level()? 1 : 0;
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900172}
173
Nobuhiro Iwamatsu6564b1a2008-06-06 16:16:08 +0900174#define FSR_ERR_CLEAR 0x0063
175#define RDRF_CLEAR 0x00fc
176void handle_error(void)
177{
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900178
Nobuhiro Iwamatsu6564b1a2008-06-06 16:16:08 +0900179 (void)*SCFSR;
180 *SCFSR = FSR_ERR_CLEAR;
181 (void)*SCLSR;
182 *SCLSR = 0x00;
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900183}
184
Nobuhiro Iwamatsu6564b1a2008-06-06 16:16:08 +0900185int serial_getc_check(void)
186{
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900187 unsigned short status;
188
Nobuhiro Iwamatsu6564b1a2008-06-06 16:16:08 +0900189 status = *SCFSR;
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900190
Nobuhiro Iwamatsu6564b1a2008-06-06 16:16:08 +0900191 if (status & (FSR_FER | FSR_ER | FSR_BRK))
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900192 handle_error();
Nobuhiro Iwamatsu6564b1a2008-06-06 16:16:08 +0900193 if (*SCLSR & LSR_ORER)
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900194 handle_error();
Nobuhiro Iwamatsu6564b1a2008-06-06 16:16:08 +0900195 return (status & (FSR_DR | FSR_RDF));
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900196}
197
Nobuhiro Iwamatsu6564b1a2008-06-06 16:16:08 +0900198int serial_getc(void)
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900199{
Nobuhiro Iwamatsu6564b1a2008-06-06 16:16:08 +0900200 unsigned short status;
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900201 char ch;
Nobuhiro Iwamatsu6564b1a2008-06-06 16:16:08 +0900202 while (!serial_getc_check()) ;
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900203
204 ch = *SCFRDR;
Nobuhiro Iwamatsu6564b1a2008-06-06 16:16:08 +0900205 status = *SCFSR;
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900206
Nobuhiro Iwamatsu6564b1a2008-06-06 16:16:08 +0900207 *SCFSR = RDRF_CLEAR;
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900208
209 if (status & (FSR_FER | FSR_FER | FSR_ER | FSR_BRK))
210 handle_error();
211
Nobuhiro Iwamatsu6564b1a2008-06-06 16:16:08 +0900212 if (*SCLSR & LSR_ORER)
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900213 handle_error();
214
Nobuhiro Iwamatsu6564b1a2008-06-06 16:16:08 +0900215 return ch;
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900216}