blob: 0801ac4a8c8e7ab8952eff9fc4b167e2767d3277 [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
23#ifdef CFG_SCIF_CONSOLE
24
25#if defined (CONFIG_CONS_SCIF0)
26#define SCIF_BASE SCIF0_BASE
27#elif defined (CONFIG_CONS_SCIF1)
28#define SCIF_BASE SCIF1_BASE
Nobuhiro Iwamatsu08c5fab2008-06-06 16:16:08 +090029#elif defined (CONFIG_CONS_SCIF2)
30#define SCIF_BASE SCIF2_BASE
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +090031#else
32#error "Default SCIF doesn't set....."
33#endif
34
Nobuhiro Iwamatsu76e49aa2008-01-15 23:25:25 +090035/* Base register */
36#define SCSMR (vu_short *)(SCIF_BASE + 0x0)
37#define SCBRR (vu_char *)(SCIF_BASE + 0x4)
38#define SCSCR (vu_short *)(SCIF_BASE + 0x8)
39#define SCFCR (vu_short *)(SCIF_BASE + 0x18)
40#define SCFDR (vu_short *)(SCIF_BASE + 0x1C)
Nobuhiro Iwamatsu08c5fab2008-06-06 16:16:08 +090041#ifdef CONFIG_CPU_SH7720 /* SH7720 specific */
42# define SCFSR (vu_short *)(SCIF_BASE + 0x14) /* SCSSR */
Nobuhiro Iwamatsu3ecff1d2008-03-06 14:05:53 +090043# define SCFTDR (vu_char *)(SCIF_BASE + 0x20)
44# define SCFRDR (vu_char *)(SCIF_BASE + 0x24)
Yoshihiro Shimoda7c10c572008-01-09 14:30:02 +090045#else
Nobuhiro Iwamatsu3ecff1d2008-03-06 14:05:53 +090046# define SCFTDR (vu_char *)(SCIF_BASE + 0xC)
Nobuhiro Iwamatsu08c5fab2008-06-06 16:16:08 +090047# define SCFSR (vu_short *)(SCIF_BASE + 0x10)
Nobuhiro Iwamatsu3ecff1d2008-03-06 14:05:53 +090048# define SCFRDR (vu_char *)(SCIF_BASE + 0x14)
Yoshihiro Shimoda7c10c572008-01-09 14:30:02 +090049#endif
50
Nobuhiro Iwamatsu08c5fab2008-06-06 16:16:08 +090051#if defined(CONFIG_CPU_SH7780) || \
Yusuke Godab55523e2008-03-05 14:23:26 +090052 defined(CONFIG_CPU_SH7785)
Nobuhiro Iwamatsu3ecff1d2008-03-06 14:05:53 +090053# define SCRFDR (vu_short *)(SCIF_BASE + 0x20)
54# define SCSPTR (vu_short *)(SCIF_BASE + 0x24)
Wolfgang Denk53677ef2008-05-20 16:00:29 +020055# define SCLSR (vu_short *)(SCIF_BASE + 0x28)
Nobuhiro Iwamatsu3ecff1d2008-03-06 14:05:53 +090056# define SCRER (vu_short *)(SCIF_BASE + 0x2C)
57# define LSR_ORER 1
58# define FIFOLEVEL_MASK 0xFF
Nobuhiro Iwamatsu08c5fab2008-06-06 16:16:08 +090059#elif defined(CONFIG_CPU_SH7763)
60# if defined (CONFIG_CONS_SCIF2)
61# define SCSPTR (vu_short *)(SCIF_BASE + 0x20)
62# define SCLSR (vu_short *)(SCIF_BASE + 0x24)
63# define LSR_ORER 1
64# define FIFOLEVEL_MASK 0x1F
65# else
66# define SCRFDR (vu_short *)(SCIF_BASE + 0x20)
67# define SCSPTR (vu_short *)(SCIF_BASE + 0x24)
68# define SCLSR (vu_short *)(SCIF_BASE + 0x28)
69# define SCRER (vu_short *)(SCIF_BASE + 0x2C)
70# define LSR_ORER 1
71# define FIFOLEVEL_MASK 0xFF
72# endif
Yusuke Godab55523e2008-03-05 14:23:26 +090073#elif defined(CONFIG_CPU_SH7750) || \
Nobuhiro Iwamatsu56693322008-03-12 12:10:28 +090074 defined(CONFIG_CPU_SH7751) || \
Yusuke Godab55523e2008-03-05 14:23:26 +090075 defined(CONFIG_CPU_SH7722)
Wolfgang Denk53677ef2008-05-20 16:00:29 +020076# define SCSPTR (vu_short *)(SCIF_BASE + 0x20)
Nobuhiro Iwamatsu08c5fab2008-06-06 16:16:08 +090077# define SCLSR (vu_short *)(SCIF_BASE + 0x24)
Nobuhiro Iwamatsu3ecff1d2008-03-06 14:05:53 +090078# define LSR_ORER 1
79# define FIFOLEVEL_MASK 0x1F
Yusuke Godab55523e2008-03-05 14:23:26 +090080#elif defined(CONFIG_CPU_SH7720)
Nobuhiro Iwamatsu08c5fab2008-06-06 16:16:08 +090081# define SCLSR (vu_short *)(SCIF_BASE + 0x24)
Nobuhiro Iwamatsu3ecff1d2008-03-06 14:05:53 +090082# define LSR_ORER 0x0200
83# define FIFOLEVEL_MASK 0x1F
Nobuhiro Iwamatsu08c5fab2008-06-06 16:16:08 +090084#elif defined(CONFIG_CPU_SH7710) || \
Yusuke Godab55523e2008-03-05 14:23:26 +090085 defined(CONFIG_CPU_SH7712)
Nobuhiro Iwamatsu08c5fab2008-06-06 16:16:08 +090086# define SCLSR SCFSR /* SCSSR */
Nobuhiro Iwamatsu3ecff1d2008-03-06 14:05:53 +090087# define LSR_ORER 1
88# define FIFOLEVEL_MASK 0x1F
Nobuhiro Iwamatsuac331da2008-01-17 15:53:52 +090089#endif
Nobuhiro Iwamatsuac331da2008-01-17 15:53:52 +090090
Yusuke Godab55523e2008-03-05 14:23:26 +090091/* SCBRR register value setting */
Nobuhiro Iwamatsuac331da2008-01-17 15:53:52 +090092#if defined(CONFIG_CPU_SH7720)
Nobuhiro Iwamatsu3ecff1d2008-03-06 14:05:53 +090093# define SCBRR_VALUE(bps, clk) (((clk*2)+16*bps)/(32*bps)-1)
Nobuhiro Iwamatsu08c5fab2008-06-06 16:16:08 +090094#else /* Generic SuperH */
Nobuhiro Iwamatsu3ecff1d2008-03-06 14:05:53 +090095# define SCBRR_VALUE(bps, clk) ((clk+16*bps)/(32*bps)-1)
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +090096#endif
97
Nobuhiro Iwamatsu08c5fab2008-06-06 16:16:08 +090098#define SCR_RE (1 << 4)
99#define SCR_TE (1 << 5)
100#define FCR_RFRST (1 << 1) /* RFCL */
101#define FCR_TFRST (1 << 2) /* TFCL */
102#define FSR_DR (1 << 0)
103#define FSR_RDF (1 << 1)
104#define FSR_FER (1 << 3)
105#define FSR_BRK (1 << 4)
106#define FSR_FER (1 << 3)
107#define FSR_TEND (1 << 6)
108#define FSR_ER (1 << 7)
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900109
110/*----------------------------------------------------------------------*/
111
Nobuhiro Iwamatsu08c5fab2008-06-06 16:16:08 +0900112void serial_setbrg(void)
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900113{
114 DECLARE_GLOBAL_DATA_PTR;
Nobuhiro Iwamatsu08c5fab2008-06-06 16:16:08 +0900115 *SCBRR = SCBRR_VALUE(gd->baudrate, CONFIG_SYS_CLK_FREQ);
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900116}
117
Nobuhiro Iwamatsu08c5fab2008-06-06 16:16:08 +0900118int serial_init(void)
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900119{
120 *SCSCR = (SCR_RE | SCR_TE);
Nobuhiro Iwamatsu08c5fab2008-06-06 16:16:08 +0900121 *SCSMR = 0;
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900122 *SCSMR = 0;
123 *SCFCR = (FCR_RFRST | FCR_TFRST);
124 *SCFCR;
125 *SCFCR = 0;
126
127 serial_setbrg();
128 return 0;
129}
130
Nobuhiro Iwamatsu08c5fab2008-06-06 16:16:08 +0900131static int serial_rx_fifo_level(void)
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900132{
Nobuhiro Iwamatsu08c5fab2008-06-06 16:16:08 +0900133#if defined(SCRFDR)
Nobuhiro Iwamatsu3ecff1d2008-03-06 14:05:53 +0900134 return (*SCRFDR >> 0) & FIFOLEVEL_MASK;
135#else
136 return (*SCFDR >> 0) & FIFOLEVEL_MASK;
137#endif
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900138}
139
Nobuhiro Iwamatsu08c5fab2008-06-06 16:16:08 +0900140void serial_raw_putc(const char c)
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900141{
142 unsigned int fsr_bits_to_clear;
143
144 while (1) {
Nobuhiro Iwamatsu08c5fab2008-06-06 16:16:08 +0900145 if (*SCFSR & FSR_TEND) { /* Tx fifo is empty */
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900146 fsr_bits_to_clear = FSR_TEND;
147 break;
148 }
149 }
150
151 *SCFTDR = c;
152 if (fsr_bits_to_clear != 0)
153 *SCFSR &= ~fsr_bits_to_clear;
154}
155
Nobuhiro Iwamatsu08c5fab2008-06-06 16:16:08 +0900156void serial_putc(const char c)
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900157{
158 if (c == '\n')
Nobuhiro Iwamatsu08c5fab2008-06-06 16:16:08 +0900159 serial_raw_putc('\r');
160 serial_raw_putc(c);
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900161}
162
Nobuhiro Iwamatsu08c5fab2008-06-06 16:16:08 +0900163void serial_puts(const char *s)
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900164{
165 char c;
166 while ((c = *s++) != 0)
Nobuhiro Iwamatsu08c5fab2008-06-06 16:16:08 +0900167 serial_putc(c);
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900168}
169
Nobuhiro Iwamatsu08c5fab2008-06-06 16:16:08 +0900170int serial_tstc(void)
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900171{
Nobuhiro Iwamatsu08c5fab2008-06-06 16:16:08 +0900172 return serial_rx_fifo_level()? 1 : 0;
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900173}
174
Nobuhiro Iwamatsu08c5fab2008-06-06 16:16:08 +0900175#define FSR_ERR_CLEAR 0x0063
176#define RDRF_CLEAR 0x00fc
177void handle_error(void)
178{
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900179
Nobuhiro Iwamatsu08c5fab2008-06-06 16:16:08 +0900180 (void)*SCFSR;
181 *SCFSR = FSR_ERR_CLEAR;
182 (void)*SCLSR;
183 *SCLSR = 0x00;
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900184}
185
Nobuhiro Iwamatsu08c5fab2008-06-06 16:16:08 +0900186int serial_getc_check(void)
187{
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900188 unsigned short status;
189
Nobuhiro Iwamatsu08c5fab2008-06-06 16:16:08 +0900190 status = *SCFSR;
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900191
Nobuhiro Iwamatsu08c5fab2008-06-06 16:16:08 +0900192 if (status & (FSR_FER | FSR_ER | FSR_BRK))
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900193 handle_error();
Nobuhiro Iwamatsu08c5fab2008-06-06 16:16:08 +0900194 if (*SCLSR & LSR_ORER)
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900195 handle_error();
Nobuhiro Iwamatsu08c5fab2008-06-06 16:16:08 +0900196 return (status & (FSR_DR | FSR_RDF));
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900197}
198
Nobuhiro Iwamatsu08c5fab2008-06-06 16:16:08 +0900199int serial_getc(void)
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900200{
Nobuhiro Iwamatsu08c5fab2008-06-06 16:16:08 +0900201 unsigned short status;
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900202 char ch;
Nobuhiro Iwamatsu08c5fab2008-06-06 16:16:08 +0900203 while (!serial_getc_check()) ;
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900204
205 ch = *SCFRDR;
Nobuhiro Iwamatsu08c5fab2008-06-06 16:16:08 +0900206 status = *SCFSR;
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900207
Nobuhiro Iwamatsu08c5fab2008-06-06 16:16:08 +0900208 *SCFSR = RDRF_CLEAR;
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900209
210 if (status & (FSR_FER | FSR_FER | FSR_ER | FSR_BRK))
211 handle_error();
212
Nobuhiro Iwamatsu08c5fab2008-06-06 16:16:08 +0900213 if (*SCLSR & LSR_ORER)
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900214 handle_error();
215
Nobuhiro Iwamatsu08c5fab2008-06-06 16:16:08 +0900216 return ch;
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900217}
218
Nobuhiro Iwamatsu08c5fab2008-06-06 16:16:08 +0900219#endif /* CFG_SCIF_CONSOLE */