blob: bfdb2ce77a3b9baea4e1e4f3a1182458102f57b0 [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>
Jean-Christophe PLAGNIOL-VILLARDfc83c922009-01-11 16:35:16 +010021#include <asm/io.h>
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +090022#include <asm/processor.h>
23
Nobuhiro Iwamatsuab09f432008-08-22 17:48:51 +090024#if defined(CONFIG_CONS_SCIF0)
25# define SCIF_BASE SCIF0_BASE
26#elif defined(CONFIG_CONS_SCIF1)
27# define SCIF_BASE SCIF1_BASE
28#elif defined(CONFIG_CONS_SCIF2)
29# define SCIF_BASE SCIF2_BASE
30#elif defined(CONFIG_CONS_SCIF3)
31# define SCIF_BASE SCIF3_BASE
32#elif defined(CONFIG_CONS_SCIF4)
33# define SCIF_BASE SCIF4_BASE
34#elif defined(CONFIG_CONS_SCIF5)
35# define SCIF_BASE SCIF5_BASE
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +090036#else
Nobuhiro Iwamatsuab09f432008-08-22 17:48:51 +090037# error "Default SCIF doesn't set....."
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +090038#endif
39
Nobuhiro Iwamatsu76e49aa2008-01-15 23:25:25 +090040/* Base register */
41#define SCSMR (vu_short *)(SCIF_BASE + 0x0)
42#define SCBRR (vu_char *)(SCIF_BASE + 0x4)
43#define SCSCR (vu_short *)(SCIF_BASE + 0x8)
44#define SCFCR (vu_short *)(SCIF_BASE + 0x18)
45#define SCFDR (vu_short *)(SCIF_BASE + 0x1C)
Nobuhiro Iwamatsuab09f432008-08-22 17:48:51 +090046#if defined(CONFIG_CPU_SH7720) || \
47 (defined(CONFIG_CPU_SH7723) && defined(CONFIG_SCIF_A))
Nobuhiro Iwamatsu08c5fab2008-06-06 16:16:08 +090048# define SCFSR (vu_short *)(SCIF_BASE + 0x14) /* SCSSR */
Nobuhiro Iwamatsu3ecff1d2008-03-06 14:05:53 +090049# define SCFTDR (vu_char *)(SCIF_BASE + 0x20)
50# define SCFRDR (vu_char *)(SCIF_BASE + 0x24)
Yoshihiro Shimoda7c10c572008-01-09 14:30:02 +090051#else
Nobuhiro Iwamatsu3ecff1d2008-03-06 14:05:53 +090052# define SCFTDR (vu_char *)(SCIF_BASE + 0xC)
Jean-Christophe PLAGNIOL-VILLARD9e1fa622009-01-11 16:35:15 +010053# define SCFSR (vu_short *)(SCIF_BASE + 0x10)
Nobuhiro Iwamatsu3ecff1d2008-03-06 14:05:53 +090054# define SCFRDR (vu_char *)(SCIF_BASE + 0x14)
Yoshihiro Shimoda7c10c572008-01-09 14:30:02 +090055#endif
56
Nobuhiro Iwamatsu08c5fab2008-06-06 16:16:08 +090057#if defined(CONFIG_CPU_SH7780) || \
Yusuke Godab55523e2008-03-05 14:23:26 +090058 defined(CONFIG_CPU_SH7785)
Nobuhiro Iwamatsu3ecff1d2008-03-06 14:05:53 +090059# define SCRFDR (vu_short *)(SCIF_BASE + 0x20)
60# define SCSPTR (vu_short *)(SCIF_BASE + 0x24)
Wolfgang Denk53677ef2008-05-20 16:00:29 +020061# define SCLSR (vu_short *)(SCIF_BASE + 0x28)
Nobuhiro Iwamatsu3ecff1d2008-03-06 14:05:53 +090062# define SCRER (vu_short *)(SCIF_BASE + 0x2C)
63# define LSR_ORER 1
64# define FIFOLEVEL_MASK 0xFF
Nobuhiro Iwamatsu08c5fab2008-06-06 16:16:08 +090065#elif defined(CONFIG_CPU_SH7763)
Nobuhiro Iwamatsuab09f432008-08-22 17:48:51 +090066# if defined(CONFIG_CONS_SCIF2)
Nobuhiro Iwamatsu08c5fab2008-06-06 16:16:08 +090067# define SCSPTR (vu_short *)(SCIF_BASE + 0x20)
Jean-Christophe PLAGNIOL-VILLARD9e1fa622009-01-11 16:35:15 +010068# define SCLSR (vu_short *)(SCIF_BASE + 0x24)
Nobuhiro Iwamatsu08c5fab2008-06-06 16:16:08 +090069# define LSR_ORER 1
70# define FIFOLEVEL_MASK 0x1F
71# else
72# define SCRFDR (vu_short *)(SCIF_BASE + 0x20)
73# define SCSPTR (vu_short *)(SCIF_BASE + 0x24)
74# define SCLSR (vu_short *)(SCIF_BASE + 0x28)
75# define SCRER (vu_short *)(SCIF_BASE + 0x2C)
76# define LSR_ORER 1
77# define FIFOLEVEL_MASK 0xFF
78# endif
Nobuhiro Iwamatsuab09f432008-08-22 17:48:51 +090079#elif defined(CONFIG_CPU_SH7723)
Nobuhiro Iwamatsua03c09c2008-09-17 11:45:26 +090080# if defined(CONFIG_SCIF_A)
Nobuhiro Iwamatsuab09f432008-08-22 17:48:51 +090081# define SCLSR SCFSR
82# define LSR_ORER 0x0200
83# define FIFOLEVEL_MASK 0x3F
84#else
85# define SCLSR (vu_short *)(SCIF_BASE + 0x24)
86# define LSR_ORER 1
87# define FIFOLEVEL_MASK 0x1F
88#endif
Yusuke Godab55523e2008-03-05 14:23:26 +090089#elif defined(CONFIG_CPU_SH7750) || \
Nobuhiro Iwamatsu56693322008-03-12 12:10:28 +090090 defined(CONFIG_CPU_SH7751) || \
Nobuhiro Iwamatsu6ede7532008-07-03 23:11:02 +090091 defined(CONFIG_CPU_SH7722) || \
92 defined(CONFIG_CPU_SH7203)
Wolfgang Denk53677ef2008-05-20 16:00:29 +020093# define SCSPTR (vu_short *)(SCIF_BASE + 0x20)
Jean-Christophe PLAGNIOL-VILLARD9e1fa622009-01-11 16:35:15 +010094# define SCLSR (vu_short *)(SCIF_BASE + 0x24)
Nobuhiro Iwamatsu3ecff1d2008-03-06 14:05:53 +090095# define LSR_ORER 1
96# define FIFOLEVEL_MASK 0x1F
Yusuke Godab55523e2008-03-05 14:23:26 +090097#elif defined(CONFIG_CPU_SH7720)
Nobuhiro Iwamatsu15e26972008-11-17 16:53:09 +090098# define SCLSR SCFSR
Nobuhiro Iwamatsu3ecff1d2008-03-06 14:05:53 +090099# define LSR_ORER 0x0200
100# define FIFOLEVEL_MASK 0x1F
Nobuhiro Iwamatsu08c5fab2008-06-06 16:16:08 +0900101#elif defined(CONFIG_CPU_SH7710) || \
Yusuke Godab55523e2008-03-05 14:23:26 +0900102 defined(CONFIG_CPU_SH7712)
Nobuhiro Iwamatsu08c5fab2008-06-06 16:16:08 +0900103# define SCLSR SCFSR /* SCSSR */
Nobuhiro Iwamatsu3ecff1d2008-03-06 14:05:53 +0900104# define LSR_ORER 1
105# define FIFOLEVEL_MASK 0x1F
Nobuhiro Iwamatsuac331da2008-01-17 15:53:52 +0900106#endif
Nobuhiro Iwamatsuac331da2008-01-17 15:53:52 +0900107
Yusuke Godab55523e2008-03-05 14:23:26 +0900108/* SCBRR register value setting */
Nobuhiro Iwamatsuac331da2008-01-17 15:53:52 +0900109#if defined(CONFIG_CPU_SH7720)
Jean-Christophe PLAGNIOL-VILLARD9e1fa622009-01-11 16:35:15 +0100110# define SCBRR_VALUE(bps, clk) (((clk * 2) + 16 * bps) / (32 * bps) - 1)
Nobuhiro Iwamatsuab09f432008-08-22 17:48:51 +0900111#elif defined(CONFIG_CPU_SH7723) && defined(CONFIG_SCIF_A)
112/* SH7723 SCIFA use bus clock. So clock *2 */
Jean-Christophe PLAGNIOL-VILLARD9e1fa622009-01-11 16:35:15 +0100113# define SCBRR_VALUE(bps, clk) (((clk * 2 * 2) + 16 * bps) / (32 * bps) - 1)
Nobuhiro Iwamatsu08c5fab2008-06-06 16:16:08 +0900114#else /* Generic SuperH */
Jean-Christophe PLAGNIOL-VILLARD9e1fa622009-01-11 16:35:15 +0100115# define SCBRR_VALUE(bps, clk) ((clk + 16 * bps) / (32 * bps) - 1)
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900116#endif
117
Jean-Christophe PLAGNIOL-VILLARD9e1fa622009-01-11 16:35:15 +0100118#define SCR_RE (1 << 4)
119#define SCR_TE (1 << 5)
Nobuhiro Iwamatsu08c5fab2008-06-06 16:16:08 +0900120#define FCR_RFRST (1 << 1) /* RFCL */
121#define FCR_TFRST (1 << 2) /* TFCL */
Jean-Christophe PLAGNIOL-VILLARD9e1fa622009-01-11 16:35:15 +0100122#define FSR_DR (1 << 0)
123#define FSR_RDF (1 << 1)
124#define FSR_FER (1 << 3)
125#define FSR_BRK (1 << 4)
126#define FSR_FER (1 << 3)
127#define FSR_TEND (1 << 6)
128#define FSR_ER (1 << 7)
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900129
130/*----------------------------------------------------------------------*/
131
Nobuhiro Iwamatsu08c5fab2008-06-06 16:16:08 +0900132void serial_setbrg(void)
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900133{
134 DECLARE_GLOBAL_DATA_PTR;
Jean-Christophe PLAGNIOL-VILLARD9e1fa622009-01-11 16:35:15 +0100135
Jean-Christophe PLAGNIOL-VILLARDfc83c922009-01-11 16:35:16 +0100136 writeb(SCBRR_VALUE(gd->baudrate, CONFIG_SYS_CLK_FREQ), SCBRR);
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900137}
138
Nobuhiro Iwamatsu08c5fab2008-06-06 16:16:08 +0900139int serial_init(void)
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900140{
Jean-Christophe PLAGNIOL-VILLARDfc83c922009-01-11 16:35:16 +0100141 writew((SCR_RE | SCR_TE), SCSCR);
142 writew(0, SCSMR);
143 writew(0, SCSMR);
144 writew((FCR_RFRST | FCR_TFRST), SCFCR);
145 readw(SCFCR);
146 writew(0, SCFCR);
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900147
148 serial_setbrg();
149 return 0;
150}
151
Nobuhiro Iwamatsu08c5fab2008-06-06 16:16:08 +0900152static int serial_rx_fifo_level(void)
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900153{
Nobuhiro Iwamatsu08c5fab2008-06-06 16:16:08 +0900154#if defined(SCRFDR)
Jean-Christophe PLAGNIOL-VILLARDfc83c922009-01-11 16:35:16 +0100155 return (readw(SCRFDR) >> 0) & FIFOLEVEL_MASK;
Nobuhiro Iwamatsu3ecff1d2008-03-06 14:05:53 +0900156#else
Jean-Christophe PLAGNIOL-VILLARDfc83c922009-01-11 16:35:16 +0100157 return (readw(SCFDR) >> 0) & FIFOLEVEL_MASK;
Nobuhiro Iwamatsu3ecff1d2008-03-06 14:05:53 +0900158#endif
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900159}
160
Nobuhiro Iwamatsu08c5fab2008-06-06 16:16:08 +0900161void serial_raw_putc(const char c)
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900162{
163 unsigned int fsr_bits_to_clear;
164
165 while (1) {
Jean-Christophe PLAGNIOL-VILLARDfc83c922009-01-11 16:35:16 +0100166 if (readw(SCFSR) & FSR_TEND) { /* Tx fifo is empty */
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900167 fsr_bits_to_clear = FSR_TEND;
168 break;
169 }
170 }
171
Jean-Christophe PLAGNIOL-VILLARDfc83c922009-01-11 16:35:16 +0100172 writeb(c, SCFTDR);
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900173 if (fsr_bits_to_clear != 0)
Jean-Christophe PLAGNIOL-VILLARDfc83c922009-01-11 16:35:16 +0100174 writew(readw(SCFSR) & ~fsr_bits_to_clear, SCFSR);
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900175}
176
Nobuhiro Iwamatsu08c5fab2008-06-06 16:16:08 +0900177void serial_putc(const char c)
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900178{
179 if (c == '\n')
Nobuhiro Iwamatsu08c5fab2008-06-06 16:16:08 +0900180 serial_raw_putc('\r');
181 serial_raw_putc(c);
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900182}
183
Nobuhiro Iwamatsu08c5fab2008-06-06 16:16:08 +0900184void serial_puts(const char *s)
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900185{
186 char c;
187 while ((c = *s++) != 0)
Nobuhiro Iwamatsu08c5fab2008-06-06 16:16:08 +0900188 serial_putc(c);
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900189}
190
Nobuhiro Iwamatsu08c5fab2008-06-06 16:16:08 +0900191int serial_tstc(void)
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900192{
Nobuhiro Iwamatsuab09f432008-08-22 17:48:51 +0900193 return serial_rx_fifo_level() ? 1 : 0;
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900194}
195
Jean-Christophe PLAGNIOL-VILLARD9e1fa622009-01-11 16:35:15 +0100196#define FSR_ERR_CLEAR 0x0063
197#define RDRF_CLEAR 0x00fc
Nobuhiro Iwamatsu08c5fab2008-06-06 16:16:08 +0900198void handle_error(void)
199{
Jean-Christophe PLAGNIOL-VILLARDfc83c922009-01-11 16:35:16 +0100200 readw(SCFSR);
201 writew(FSR_ERR_CLEAR, SCFSR);
202 readw(SCLSR);
203 writew(0x00, SCLSR);
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900204}
205
Nobuhiro Iwamatsu08c5fab2008-06-06 16:16:08 +0900206int serial_getc_check(void)
207{
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900208 unsigned short status;
209
Jean-Christophe PLAGNIOL-VILLARDfc83c922009-01-11 16:35:16 +0100210 status = readw(SCFSR);
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900211
Nobuhiro Iwamatsu08c5fab2008-06-06 16:16:08 +0900212 if (status & (FSR_FER | FSR_ER | FSR_BRK))
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900213 handle_error();
Jean-Christophe PLAGNIOL-VILLARDfc83c922009-01-11 16:35:16 +0100214 if (readw(SCLSR) & LSR_ORER)
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900215 handle_error();
Nobuhiro Iwamatsuab09f432008-08-22 17:48:51 +0900216 return status & (FSR_DR | FSR_RDF);
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900217}
218
Nobuhiro Iwamatsu08c5fab2008-06-06 16:16:08 +0900219int serial_getc(void)
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900220{
Nobuhiro Iwamatsu08c5fab2008-06-06 16:16:08 +0900221 unsigned short status;
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900222 char ch;
Nobuhiro Iwamatsuab09f432008-08-22 17:48:51 +0900223
224 while (!serial_getc_check())
225 ;
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900226
Jean-Christophe PLAGNIOL-VILLARDfc83c922009-01-11 16:35:16 +0100227 ch = readb(SCFRDR);
228 status = readw(SCFSR);
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900229
Jean-Christophe PLAGNIOL-VILLARDfc83c922009-01-11 16:35:16 +0100230 writew(RDRF_CLEAR, SCFSR);
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900231
232 if (status & (FSR_FER | FSR_FER | FSR_ER | FSR_BRK))
233 handle_error();
234
Jean-Christophe PLAGNIOL-VILLARDfc83c922009-01-11 16:35:16 +0100235 if (readw(SCLSR) & LSR_ORER)
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900236 handle_error();
237
Nobuhiro Iwamatsu08c5fab2008-06-06 16:16:08 +0900238 return ch;
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900239}