blob: 2b9eeed47e0c68fdf530f89bc0362efbc5665303 [file] [log] [blame]
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +09001/*
2 * SuperH SCIF device driver.
Nobuhiro Iwamatsuac331da2008-01-17 15:53:52 +09003 * Copyright (c) 2007,2008 Nobuhiro Iwamatsu
Wolfgang Denk61fb15c52007-12-27 01:52:50 +01004 *
Nobuhiro Iwamatsu0b135cf2007-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 Iwamatsu0b135cf2007-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 Iwamatsu08c5fab2008-06-06 16:16:08 +090027#elif defined (CONFIG_CONS_SCIF2)
28#define SCIF_BASE SCIF2_BASE
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +090029#else
30#error "Default SCIF doesn't set....."
31#endif
32
Nobuhiro Iwamatsu76e49aa2008-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 Iwamatsu08c5fab2008-06-06 16:16:08 +090039#ifdef CONFIG_CPU_SH7720 /* SH7720 specific */
40# define SCFSR (vu_short *)(SCIF_BASE + 0x14) /* SCSSR */
Nobuhiro Iwamatsu3ecff1d2008-03-06 14:05:53 +090041# define SCFTDR (vu_char *)(SCIF_BASE + 0x20)
42# define SCFRDR (vu_char *)(SCIF_BASE + 0x24)
Yoshihiro Shimoda7c10c572008-01-09 14:30:02 +090043#else
Nobuhiro Iwamatsu3ecff1d2008-03-06 14:05:53 +090044# define SCFTDR (vu_char *)(SCIF_BASE + 0xC)
Nobuhiro Iwamatsu08c5fab2008-06-06 16:16:08 +090045# define SCFSR (vu_short *)(SCIF_BASE + 0x10)
Nobuhiro Iwamatsu3ecff1d2008-03-06 14:05:53 +090046# define SCFRDR (vu_char *)(SCIF_BASE + 0x14)
Yoshihiro Shimoda7c10c572008-01-09 14:30:02 +090047#endif
48
Nobuhiro Iwamatsu08c5fab2008-06-06 16:16:08 +090049#if defined(CONFIG_CPU_SH7780) || \
Yusuke Godab55523e2008-03-05 14:23:26 +090050 defined(CONFIG_CPU_SH7785)
Nobuhiro Iwamatsu3ecff1d2008-03-06 14:05:53 +090051# define SCRFDR (vu_short *)(SCIF_BASE + 0x20)
52# define SCSPTR (vu_short *)(SCIF_BASE + 0x24)
Wolfgang Denk53677ef2008-05-20 16:00:29 +020053# define SCLSR (vu_short *)(SCIF_BASE + 0x28)
Nobuhiro Iwamatsu3ecff1d2008-03-06 14:05:53 +090054# define SCRER (vu_short *)(SCIF_BASE + 0x2C)
55# define LSR_ORER 1
56# define FIFOLEVEL_MASK 0xFF
Nobuhiro Iwamatsu08c5fab2008-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 Godab55523e2008-03-05 14:23:26 +090071#elif defined(CONFIG_CPU_SH7750) || \
Nobuhiro Iwamatsu56693322008-03-12 12:10:28 +090072 defined(CONFIG_CPU_SH7751) || \
Yusuke Godab55523e2008-03-05 14:23:26 +090073 defined(CONFIG_CPU_SH7722)
Wolfgang Denk53677ef2008-05-20 16:00:29 +020074# define SCSPTR (vu_short *)(SCIF_BASE + 0x20)
Nobuhiro Iwamatsu08c5fab2008-06-06 16:16:08 +090075# define SCLSR (vu_short *)(SCIF_BASE + 0x24)
Nobuhiro Iwamatsu3ecff1d2008-03-06 14:05:53 +090076# define LSR_ORER 1
77# define FIFOLEVEL_MASK 0x1F
Yusuke Godab55523e2008-03-05 14:23:26 +090078#elif defined(CONFIG_CPU_SH7720)
Nobuhiro Iwamatsu08c5fab2008-06-06 16:16:08 +090079# define SCLSR (vu_short *)(SCIF_BASE + 0x24)
Nobuhiro Iwamatsu3ecff1d2008-03-06 14:05:53 +090080# define LSR_ORER 0x0200
81# define FIFOLEVEL_MASK 0x1F
Nobuhiro Iwamatsu08c5fab2008-06-06 16:16:08 +090082#elif defined(CONFIG_CPU_SH7710) || \
Yusuke Godab55523e2008-03-05 14:23:26 +090083 defined(CONFIG_CPU_SH7712)
Nobuhiro Iwamatsu08c5fab2008-06-06 16:16:08 +090084# define SCLSR SCFSR /* SCSSR */
Nobuhiro Iwamatsu3ecff1d2008-03-06 14:05:53 +090085# define LSR_ORER 1
86# define FIFOLEVEL_MASK 0x1F
Nobuhiro Iwamatsuac331da2008-01-17 15:53:52 +090087#endif
Nobuhiro Iwamatsuac331da2008-01-17 15:53:52 +090088
Yusuke Godab55523e2008-03-05 14:23:26 +090089/* SCBRR register value setting */
Nobuhiro Iwamatsuac331da2008-01-17 15:53:52 +090090#if defined(CONFIG_CPU_SH7720)
Nobuhiro Iwamatsu3ecff1d2008-03-06 14:05:53 +090091# define SCBRR_VALUE(bps, clk) (((clk*2)+16*bps)/(32*bps)-1)
Nobuhiro Iwamatsu08c5fab2008-06-06 16:16:08 +090092#else /* Generic SuperH */
Nobuhiro Iwamatsu3ecff1d2008-03-06 14:05:53 +090093# define SCBRR_VALUE(bps, clk) ((clk+16*bps)/(32*bps)-1)
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +090094#endif
95
Nobuhiro Iwamatsu08c5fab2008-06-06 16:16:08 +090096#define SCR_RE (1 << 4)
97#define SCR_TE (1 << 5)
98#define FCR_RFRST (1 << 1) /* RFCL */
99#define FCR_TFRST (1 << 2) /* TFCL */
100#define FSR_DR (1 << 0)
101#define FSR_RDF (1 << 1)
102#define FSR_FER (1 << 3)
103#define FSR_BRK (1 << 4)
104#define FSR_FER (1 << 3)
105#define FSR_TEND (1 << 6)
106#define FSR_ER (1 << 7)
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900107
108/*----------------------------------------------------------------------*/
109
Nobuhiro Iwamatsu08c5fab2008-06-06 16:16:08 +0900110void serial_setbrg(void)
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900111{
112 DECLARE_GLOBAL_DATA_PTR;
Nobuhiro Iwamatsu08c5fab2008-06-06 16:16:08 +0900113 *SCBRR = SCBRR_VALUE(gd->baudrate, CONFIG_SYS_CLK_FREQ);
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900114}
115
Nobuhiro Iwamatsu08c5fab2008-06-06 16:16:08 +0900116int serial_init(void)
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900117{
118 *SCSCR = (SCR_RE | SCR_TE);
Nobuhiro Iwamatsu08c5fab2008-06-06 16:16:08 +0900119 *SCSMR = 0;
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900120 *SCSMR = 0;
121 *SCFCR = (FCR_RFRST | FCR_TFRST);
122 *SCFCR;
123 *SCFCR = 0;
124
125 serial_setbrg();
126 return 0;
127}
128
Nobuhiro Iwamatsu08c5fab2008-06-06 16:16:08 +0900129static int serial_rx_fifo_level(void)
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900130{
Nobuhiro Iwamatsu08c5fab2008-06-06 16:16:08 +0900131#if defined(SCRFDR)
Nobuhiro Iwamatsu3ecff1d2008-03-06 14:05:53 +0900132 return (*SCRFDR >> 0) & FIFOLEVEL_MASK;
133#else
134 return (*SCFDR >> 0) & FIFOLEVEL_MASK;
135#endif
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900136}
137
Nobuhiro Iwamatsu08c5fab2008-06-06 16:16:08 +0900138void serial_raw_putc(const char c)
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900139{
140 unsigned int fsr_bits_to_clear;
141
142 while (1) {
Nobuhiro Iwamatsu08c5fab2008-06-06 16:16:08 +0900143 if (*SCFSR & FSR_TEND) { /* Tx fifo is empty */
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900144 fsr_bits_to_clear = FSR_TEND;
145 break;
146 }
147 }
148
149 *SCFTDR = c;
150 if (fsr_bits_to_clear != 0)
151 *SCFSR &= ~fsr_bits_to_clear;
152}
153
Nobuhiro Iwamatsu08c5fab2008-06-06 16:16:08 +0900154void serial_putc(const char c)
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900155{
156 if (c == '\n')
Nobuhiro Iwamatsu08c5fab2008-06-06 16:16:08 +0900157 serial_raw_putc('\r');
158 serial_raw_putc(c);
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900159}
160
Nobuhiro Iwamatsu08c5fab2008-06-06 16:16:08 +0900161void serial_puts(const char *s)
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900162{
163 char c;
164 while ((c = *s++) != 0)
Nobuhiro Iwamatsu08c5fab2008-06-06 16:16:08 +0900165 serial_putc(c);
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900166}
167
Nobuhiro Iwamatsu08c5fab2008-06-06 16:16:08 +0900168int serial_tstc(void)
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900169{
Nobuhiro Iwamatsu08c5fab2008-06-06 16:16:08 +0900170 return serial_rx_fifo_level()? 1 : 0;
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900171}
172
Nobuhiro Iwamatsu08c5fab2008-06-06 16:16:08 +0900173#define FSR_ERR_CLEAR 0x0063
174#define RDRF_CLEAR 0x00fc
175void handle_error(void)
176{
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900177
Nobuhiro Iwamatsu08c5fab2008-06-06 16:16:08 +0900178 (void)*SCFSR;
179 *SCFSR = FSR_ERR_CLEAR;
180 (void)*SCLSR;
181 *SCLSR = 0x00;
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900182}
183
Nobuhiro Iwamatsu08c5fab2008-06-06 16:16:08 +0900184int serial_getc_check(void)
185{
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900186 unsigned short status;
187
Nobuhiro Iwamatsu08c5fab2008-06-06 16:16:08 +0900188 status = *SCFSR;
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900189
Nobuhiro Iwamatsu08c5fab2008-06-06 16:16:08 +0900190 if (status & (FSR_FER | FSR_ER | FSR_BRK))
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900191 handle_error();
Nobuhiro Iwamatsu08c5fab2008-06-06 16:16:08 +0900192 if (*SCLSR & LSR_ORER)
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900193 handle_error();
Nobuhiro Iwamatsu08c5fab2008-06-06 16:16:08 +0900194 return (status & (FSR_DR | FSR_RDF));
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900195}
196
Nobuhiro Iwamatsu08c5fab2008-06-06 16:16:08 +0900197int serial_getc(void)
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900198{
Nobuhiro Iwamatsu08c5fab2008-06-06 16:16:08 +0900199 unsigned short status;
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900200 char ch;
Nobuhiro Iwamatsu08c5fab2008-06-06 16:16:08 +0900201 while (!serial_getc_check()) ;
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900202
203 ch = *SCFRDR;
Nobuhiro Iwamatsu08c5fab2008-06-06 16:16:08 +0900204 status = *SCFSR;
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900205
Nobuhiro Iwamatsu08c5fab2008-06-06 16:16:08 +0900206 *SCFSR = RDRF_CLEAR;
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900207
208 if (status & (FSR_FER | FSR_FER | FSR_ER | FSR_BRK))
209 handle_error();
210
Nobuhiro Iwamatsu08c5fab2008-06-06 16:16:08 +0900211 if (*SCLSR & LSR_ORER)
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900212 handle_error();
213
Nobuhiro Iwamatsu08c5fab2008-06-06 16:16:08 +0900214 return ch;
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900215}