blob: 1ddfc7d87f607356057c8f9b06e00dd1cc8a0177 [file] [log] [blame]
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +09001/*
2 * SuperH SCIF device driver.
Nobuhiro Iwamatsua5579ca2010-10-26 03:55:15 +09003 * Copyright (C) 2007,2008,2010 Nobuhiro Iwamatsu
4 * Copyright (C) 2002 - 2008 Paul Mundt
Wolfgang Denk0a5c2142007-12-27 01:52:50 +01005 *
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +09006 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21#include <common.h>
Jean-Christophe PLAGNIOL-VILLARDb27a8e32009-01-11 16:35:16 +010022#include <asm/io.h>
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +090023#include <asm/processor.h>
Nobuhiro Iwamatsua5579ca2010-10-26 03:55:15 +090024#include "serial_sh.h"
Marek Vasut904d3d72012-09-14 22:40:08 +020025#include <serial.h>
26#include <linux/compiler.h>
John Rigby0d21ed02010-12-20 18:27:51 -070027
Nobuhiro Iwamatsufcabccc2008-08-22 17:48:51 +090028#if defined(CONFIG_CONS_SCIF0)
29# define SCIF_BASE SCIF0_BASE
30#elif defined(CONFIG_CONS_SCIF1)
31# define SCIF_BASE SCIF1_BASE
32#elif defined(CONFIG_CONS_SCIF2)
33# define SCIF_BASE SCIF2_BASE
34#elif defined(CONFIG_CONS_SCIF3)
35# define SCIF_BASE SCIF3_BASE
36#elif defined(CONFIG_CONS_SCIF4)
37# define SCIF_BASE SCIF4_BASE
38#elif defined(CONFIG_CONS_SCIF5)
39# define SCIF_BASE SCIF5_BASE
Phil Edworthy04a62752012-05-15 22:15:51 +000040#elif defined(CONFIG_CONS_SCIF6)
41# define SCIF_BASE SCIF6_BASE
42#elif defined(CONFIG_CONS_SCIF7)
43# define SCIF_BASE SCIF7_BASE
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +090044#else
Nobuhiro Iwamatsufcabccc2008-08-22 17:48:51 +090045# error "Default SCIF doesn't set....."
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +090046#endif
47
Nobuhiro Iwamatsua5579ca2010-10-26 03:55:15 +090048#if defined(CONFIG_SCIF_A)
49 #define SCIF_BASE_PORT PORT_SCIFA
Yoshihiro Shimodae6258342008-01-09 14:30:02 +090050#else
Nobuhiro Iwamatsua5579ca2010-10-26 03:55:15 +090051 #define SCIF_BASE_PORT PORT_SCIF
Yoshihiro Shimodae6258342008-01-09 14:30:02 +090052#endif
53
Nobuhiro Iwamatsua5579ca2010-10-26 03:55:15 +090054static struct uart_port sh_sci = {
55 .membase = (unsigned char*)SCIF_BASE,
56 .mapbase = SCIF_BASE,
57 .type = SCIF_BASE_PORT,
58};
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +090059
Marek Vasut904d3d72012-09-14 22:40:08 +020060static void sh_serial_setbrg(void)
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +090061{
Nobuhiro Iwamatsua5579ca2010-10-26 03:55:15 +090062 DECLARE_GLOBAL_DATA_PTR;
63 sci_out(&sh_sci, SCBRR, SCBRR_VALUE(gd->baudrate, CONFIG_SYS_CLK_FREQ));
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +090064}
65
Marek Vasut904d3d72012-09-14 22:40:08 +020066static int sh_serial_init(void)
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +090067{
Nobuhiro Iwamatsua5579ca2010-10-26 03:55:15 +090068 sci_out(&sh_sci, SCSCR , SCSCR_INIT(&sh_sci));
69 sci_out(&sh_sci, SCSCR , SCSCR_INIT(&sh_sci));
70 sci_out(&sh_sci, SCSMR, 0);
71 sci_out(&sh_sci, SCSMR, 0);
72 sci_out(&sh_sci, SCFCR, SCFCR_RFRST|SCFCR_TFRST);
73 sci_in(&sh_sci, SCFCR);
74 sci_out(&sh_sci, SCFCR, 0);
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +090075
76 serial_setbrg();
77 return 0;
78}
79
Nobuhiro Iwamatsua5579ca2010-10-26 03:55:15 +090080#if defined(CONFIG_CPU_SH7760) || \
81 defined(CONFIG_CPU_SH7780) || \
82 defined(CONFIG_CPU_SH7785) || \
83 defined(CONFIG_CPU_SH7786)
84static int scif_rxfill(struct uart_port *port)
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +090085{
Nobuhiro Iwamatsua5579ca2010-10-26 03:55:15 +090086 return sci_in(port, SCRFDR) & 0xff;
87}
88#elif defined(CONFIG_CPU_SH7763)
89static int scif_rxfill(struct uart_port *port)
90{
91 if ((port->mapbase == 0xffe00000) ||
92 (port->mapbase == 0xffe08000)) {
93 /* SCIF0/1*/
94 return sci_in(port, SCRFDR) & 0xff;
95 } else {
96 /* SCIF2 */
97 return sci_in(port, SCFDR) & SCIF2_RFDC_MASK;
98 }
99}
100#elif defined(CONFIG_ARCH_SH7372)
101static int scif_rxfill(struct uart_port *port)
102{
103 if (port->type == PORT_SCIFA)
104 return sci_in(port, SCFDR) & SCIF_RFDC_MASK;
105 else
106 return sci_in(port, SCRFDR);
107}
Nobuhiro Iwamatsu1b36beb2008-03-06 14:05:53 +0900108#else
Nobuhiro Iwamatsua5579ca2010-10-26 03:55:15 +0900109static int scif_rxfill(struct uart_port *port)
110{
111 return sci_in(port, SCFDR) & SCIF_RFDC_MASK;
112}
Nobuhiro Iwamatsu1b36beb2008-03-06 14:05:53 +0900113#endif
Nobuhiro Iwamatsua5579ca2010-10-26 03:55:15 +0900114
115static int serial_rx_fifo_level(void)
116{
117 return scif_rxfill(&sh_sci);
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900118}
119
Nobuhiro Iwamatsu6564b1a2008-06-06 16:16:08 +0900120void serial_raw_putc(const char c)
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900121{
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900122 while (1) {
Nobuhiro Iwamatsua5579ca2010-10-26 03:55:15 +0900123 /* Tx fifo is empty */
124 if (sci_in(&sh_sci, SCxSR) & SCxSR_TEND(&sh_sci))
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900125 break;
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900126 }
127
Nobuhiro Iwamatsua5579ca2010-10-26 03:55:15 +0900128 sci_out(&sh_sci, SCxTDR, c);
129 sci_out(&sh_sci, SCxSR, sci_in(&sh_sci, SCxSR) & ~SCxSR_TEND(&sh_sci));
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900130}
131
Marek Vasut904d3d72012-09-14 22:40:08 +0200132static void sh_serial_putc(const char c)
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900133{
134 if (c == '\n')
Nobuhiro Iwamatsu6564b1a2008-06-06 16:16:08 +0900135 serial_raw_putc('\r');
136 serial_raw_putc(c);
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900137}
138
Marek Vasut904d3d72012-09-14 22:40:08 +0200139static void sh_serial_puts(const char *s)
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900140{
141 char c;
142 while ((c = *s++) != 0)
Nobuhiro Iwamatsu6564b1a2008-06-06 16:16:08 +0900143 serial_putc(c);
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900144}
145
Marek Vasut904d3d72012-09-14 22:40:08 +0200146static int sh_serial_tstc(void)
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900147{
Nobuhiro Iwamatsufcabccc2008-08-22 17:48:51 +0900148 return serial_rx_fifo_level() ? 1 : 0;
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900149}
150
Nobuhiro Iwamatsu6564b1a2008-06-06 16:16:08 +0900151void handle_error(void)
152{
Nobuhiro Iwamatsua5579ca2010-10-26 03:55:15 +0900153 sci_in(&sh_sci, SCxSR);
154 sci_out(&sh_sci, SCxSR, SCxSR_ERROR_CLEAR(&sh_sci));
155 sci_in(&sh_sci, SCLSR);
156 sci_out(&sh_sci, SCLSR, 0x00);
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900157}
158
Nobuhiro Iwamatsu6564b1a2008-06-06 16:16:08 +0900159int serial_getc_check(void)
160{
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900161 unsigned short status;
162
Nobuhiro Iwamatsua5579ca2010-10-26 03:55:15 +0900163 status = sci_in(&sh_sci, SCxSR);
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900164
Nobuhiro Iwamatsua5579ca2010-10-26 03:55:15 +0900165 if (status & SCIF_ERRORS)
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900166 handle_error();
Nobuhiro Iwamatsua5579ca2010-10-26 03:55:15 +0900167 if (sci_in(&sh_sci, SCLSR) & SCxSR_ORER(&sh_sci))
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900168 handle_error();
Nobuhiro Iwamatsua5579ca2010-10-26 03:55:15 +0900169 return status & (SCIF_DR | SCxSR_RDxF(&sh_sci));
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900170}
171
Marek Vasut904d3d72012-09-14 22:40:08 +0200172static int sh_serial_getc(void)
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900173{
Nobuhiro Iwamatsu6564b1a2008-06-06 16:16:08 +0900174 unsigned short status;
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900175 char ch;
Nobuhiro Iwamatsufcabccc2008-08-22 17:48:51 +0900176
177 while (!serial_getc_check())
178 ;
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900179
Nobuhiro Iwamatsua5579ca2010-10-26 03:55:15 +0900180 ch = sci_in(&sh_sci, SCxRDR);
181 status = sci_in(&sh_sci, SCxSR);
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900182
Nobuhiro Iwamatsua5579ca2010-10-26 03:55:15 +0900183 sci_out(&sh_sci, SCxSR, SCxSR_RDxF_CLEAR(&sh_sci));
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900184
Nobuhiro Iwamatsua5579ca2010-10-26 03:55:15 +0900185 if (status & SCIF_ERRORS)
186 handle_error();
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900187
Nobuhiro Iwamatsua5579ca2010-10-26 03:55:15 +0900188 if (sci_in(&sh_sci, SCLSR) & SCxSR_ORER(&sh_sci))
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900189 handle_error();
Nobuhiro Iwamatsu6564b1a2008-06-06 16:16:08 +0900190 return ch;
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900191}
Marek Vasut904d3d72012-09-14 22:40:08 +0200192
Marek Vasut904d3d72012-09-14 22:40:08 +0200193static struct serial_device sh_serial_drv = {
194 .name = "sh_serial",
195 .start = sh_serial_init,
196 .stop = NULL,
197 .setbrg = sh_serial_setbrg,
198 .putc = sh_serial_putc,
199 .puts = sh_serial_puts,
200 .getc = sh_serial_getc,
201 .tstc = sh_serial_tstc,
202};
203
204void sh_serial_initialize(void)
205{
206 serial_register(&sh_serial_drv);
207}
208
209__weak struct serial_device *default_serial_console(void)
210{
211 return &sh_serial_drv;
212}