blob: 7c1f27137615faa4a56f0b40f567c49a0ff73378 [file] [log] [blame]
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +09001/*
2 * SuperH SCIF device driver.
Nobuhiro Iwamatsu48ca8822013-07-23 13:58:20 +09003 * Copyright (C) 2013 Renesas Electronics Corporation
Nobuhiro Iwamatsu3f6c8e32010-10-26 03:55:15 +09004 * Copyright (C) 2007,2008,2010 Nobuhiro Iwamatsu
5 * Copyright (C) 2002 - 2008 Paul Mundt
Wolfgang Denk61fb15c52007-12-27 01:52:50 +01006 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02007 * SPDX-License-Identifier: GPL-2.0+
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +09008 */
9
10#include <common.h>
Jean-Christophe PLAGNIOL-VILLARDfc83c922009-01-11 16:35:16 +010011#include <asm/io.h>
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +090012#include <asm/processor.h>
Nobuhiro Iwamatsu3f6c8e32010-10-26 03:55:15 +090013#include "serial_sh.h"
Marek Vasut8bdd7ef2012-09-14 22:40:08 +020014#include <serial.h>
15#include <linux/compiler.h>
John Rigby29565322010-12-20 18:27:51 -070016
Nobuhiro Iwamatsuab09f432008-08-22 17:48:51 +090017#if defined(CONFIG_CONS_SCIF0)
18# define SCIF_BASE SCIF0_BASE
19#elif defined(CONFIG_CONS_SCIF1)
20# define SCIF_BASE SCIF1_BASE
21#elif defined(CONFIG_CONS_SCIF2)
22# define SCIF_BASE SCIF2_BASE
23#elif defined(CONFIG_CONS_SCIF3)
24# define SCIF_BASE SCIF3_BASE
25#elif defined(CONFIG_CONS_SCIF4)
26# define SCIF_BASE SCIF4_BASE
27#elif defined(CONFIG_CONS_SCIF5)
28# define SCIF_BASE SCIF5_BASE
Phil Edworthy99744b72012-05-15 22:15:51 +000029#elif defined(CONFIG_CONS_SCIF6)
30# define SCIF_BASE SCIF6_BASE
31#elif defined(CONFIG_CONS_SCIF7)
32# define SCIF_BASE SCIF7_BASE
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +090033#else
Nobuhiro Iwamatsuab09f432008-08-22 17:48:51 +090034# error "Default SCIF doesn't set....."
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +090035#endif
36
Nobuhiro Iwamatsu3f6c8e32010-10-26 03:55:15 +090037#if defined(CONFIG_SCIF_A)
38 #define SCIF_BASE_PORT PORT_SCIFA
Yoshihiro Shimoda7c10c572008-01-09 14:30:02 +090039#else
Nobuhiro Iwamatsu3f6c8e32010-10-26 03:55:15 +090040 #define SCIF_BASE_PORT PORT_SCIF
Yoshihiro Shimoda7c10c572008-01-09 14:30:02 +090041#endif
42
Nobuhiro Iwamatsu3f6c8e32010-10-26 03:55:15 +090043static struct uart_port sh_sci = {
44 .membase = (unsigned char*)SCIF_BASE,
45 .mapbase = SCIF_BASE,
46 .type = SCIF_BASE_PORT,
47};
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +090048
Marek Vasut8bdd7ef2012-09-14 22:40:08 +020049static void sh_serial_setbrg(void)
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +090050{
Nobuhiro Iwamatsu3f6c8e32010-10-26 03:55:15 +090051 DECLARE_GLOBAL_DATA_PTR;
Nobuhiro Iwamatsu53022c32014-07-28 15:07:44 +090052#ifdef CONFIG_SCIF_USE_EXT_CLK
53 unsigned short dl = DL_VALUE(gd->baudrate, CONFIG_SH_SCIF_CLK_FREQ);
54 sci_out(&sh_sci, DL, dl);
55 /* Need wait: Clock * 1/dl × 1/16 */
56 udelay((1000000 * dl * 16 / CONFIG_SYS_CLK_FREQ) * 1000 + 1);
57#else
Nobuhiro Iwamatsuf8b7d9e2013-08-20 10:31:53 +090058 sci_out(&sh_sci, SCBRR,
59 SCBRR_VALUE(gd->baudrate, CONFIG_SH_SCIF_CLK_FREQ));
Nobuhiro Iwamatsu53022c32014-07-28 15:07:44 +090060#endif
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +090061}
62
Marek Vasut8bdd7ef2012-09-14 22:40:08 +020063static int sh_serial_init(void)
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +090064{
Nobuhiro Iwamatsu3f6c8e32010-10-26 03:55:15 +090065 sci_out(&sh_sci, SCSCR , SCSCR_INIT(&sh_sci));
66 sci_out(&sh_sci, SCSCR , SCSCR_INIT(&sh_sci));
67 sci_out(&sh_sci, SCSMR, 0);
68 sci_out(&sh_sci, SCSMR, 0);
69 sci_out(&sh_sci, SCFCR, SCFCR_RFRST|SCFCR_TFRST);
70 sci_in(&sh_sci, SCFCR);
71 sci_out(&sh_sci, SCFCR, 0);
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +090072
73 serial_setbrg();
74 return 0;
75}
76
Nobuhiro Iwamatsu3f6c8e32010-10-26 03:55:15 +090077#if defined(CONFIG_CPU_SH7760) || \
78 defined(CONFIG_CPU_SH7780) || \
79 defined(CONFIG_CPU_SH7785) || \
80 defined(CONFIG_CPU_SH7786)
81static int scif_rxfill(struct uart_port *port)
82{
83 return sci_in(port, SCRFDR) & 0xff;
84}
85#elif defined(CONFIG_CPU_SH7763)
86static int scif_rxfill(struct uart_port *port)
87{
88 if ((port->mapbase == 0xffe00000) ||
89 (port->mapbase == 0xffe08000)) {
90 /* SCIF0/1*/
91 return sci_in(port, SCRFDR) & 0xff;
92 } else {
93 /* SCIF2 */
94 return sci_in(port, SCFDR) & SCIF2_RFDC_MASK;
95 }
96}
97#elif defined(CONFIG_ARCH_SH7372)
98static int scif_rxfill(struct uart_port *port)
99{
100 if (port->type == PORT_SCIFA)
101 return sci_in(port, SCFDR) & SCIF_RFDC_MASK;
102 else
103 return sci_in(port, SCRFDR);
104}
105#else
106static int scif_rxfill(struct uart_port *port)
107{
108 return sci_in(port, SCFDR) & SCIF_RFDC_MASK;
109}
110#endif
111
Nobuhiro Iwamatsu08c5fab2008-06-06 16:16:08 +0900112static int serial_rx_fifo_level(void)
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900113{
Nobuhiro Iwamatsu3f6c8e32010-10-26 03:55:15 +0900114 return scif_rxfill(&sh_sci);
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900115}
116
Tetsuyuki Kobayashi7c791b32012-11-19 21:37:38 +0000117static void handle_error(void)
118{
119 sci_in(&sh_sci, SCxSR);
120 sci_out(&sh_sci, SCxSR, SCxSR_ERROR_CLEAR(&sh_sci));
121 sci_in(&sh_sci, SCLSR);
122 sci_out(&sh_sci, SCLSR, 0x00);
123}
124
Jeroen Hofstee654f8d02014-10-08 22:57:44 +0200125static void serial_raw_putc(const char c)
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900126{
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900127 while (1) {
Nobuhiro Iwamatsu3f6c8e32010-10-26 03:55:15 +0900128 /* Tx fifo is empty */
129 if (sci_in(&sh_sci, SCxSR) & SCxSR_TEND(&sh_sci))
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900130 break;
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900131 }
132
Nobuhiro Iwamatsu3f6c8e32010-10-26 03:55:15 +0900133 sci_out(&sh_sci, SCxTDR, c);
134 sci_out(&sh_sci, SCxSR, sci_in(&sh_sci, SCxSR) & ~SCxSR_TEND(&sh_sci));
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900135}
136
Marek Vasut8bdd7ef2012-09-14 22:40:08 +0200137static void sh_serial_putc(const char c)
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900138{
139 if (c == '\n')
Nobuhiro Iwamatsu08c5fab2008-06-06 16:16:08 +0900140 serial_raw_putc('\r');
141 serial_raw_putc(c);
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900142}
143
Marek Vasut8bdd7ef2012-09-14 22:40:08 +0200144static int sh_serial_tstc(void)
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900145{
Tetsuyuki Kobayashi7c791b32012-11-19 21:37:38 +0000146 if (sci_in(&sh_sci, SCxSR) & SCIF_ERRORS) {
147 handle_error();
148 return 0;
149 }
150
Nobuhiro Iwamatsuab09f432008-08-22 17:48:51 +0900151 return serial_rx_fifo_level() ? 1 : 0;
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900152}
153
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900154
Jeroen Hofstee654f8d02014-10-08 22:57:44 +0200155static int serial_getc_check(void)
Nobuhiro Iwamatsu08c5fab2008-06-06 16:16:08 +0900156{
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900157 unsigned short status;
158
Nobuhiro Iwamatsu3f6c8e32010-10-26 03:55:15 +0900159 status = sci_in(&sh_sci, SCxSR);
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900160
Nobuhiro Iwamatsu3f6c8e32010-10-26 03:55:15 +0900161 if (status & SCIF_ERRORS)
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900162 handle_error();
Nobuhiro Iwamatsu3f6c8e32010-10-26 03:55:15 +0900163 if (sci_in(&sh_sci, SCLSR) & SCxSR_ORER(&sh_sci))
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900164 handle_error();
Nobuhiro Iwamatsu3f6c8e32010-10-26 03:55:15 +0900165 return status & (SCIF_DR | SCxSR_RDxF(&sh_sci));
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900166}
167
Marek Vasut8bdd7ef2012-09-14 22:40:08 +0200168static int sh_serial_getc(void)
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900169{
Nobuhiro Iwamatsu08c5fab2008-06-06 16:16:08 +0900170 unsigned short status;
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900171 char ch;
Nobuhiro Iwamatsuab09f432008-08-22 17:48:51 +0900172
173 while (!serial_getc_check())
174 ;
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900175
Nobuhiro Iwamatsu3f6c8e32010-10-26 03:55:15 +0900176 ch = sci_in(&sh_sci, SCxRDR);
177 status = sci_in(&sh_sci, SCxSR);
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900178
Nobuhiro Iwamatsu3f6c8e32010-10-26 03:55:15 +0900179 sci_out(&sh_sci, SCxSR, SCxSR_RDxF_CLEAR(&sh_sci));
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900180
Nobuhiro Iwamatsu3f6c8e32010-10-26 03:55:15 +0900181 if (status & SCIF_ERRORS)
182 handle_error();
183
184 if (sci_in(&sh_sci, SCLSR) & SCxSR_ORER(&sh_sci))
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900185 handle_error();
Nobuhiro Iwamatsu08c5fab2008-06-06 16:16:08 +0900186 return ch;
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900187}
Marek Vasut8bdd7ef2012-09-14 22:40:08 +0200188
Marek Vasut8bdd7ef2012-09-14 22:40:08 +0200189static struct serial_device sh_serial_drv = {
190 .name = "sh_serial",
191 .start = sh_serial_init,
192 .stop = NULL,
193 .setbrg = sh_serial_setbrg,
194 .putc = sh_serial_putc,
Marek Vasutec3fd682012-10-06 14:07:02 +0000195 .puts = default_serial_puts,
Marek Vasut8bdd7ef2012-09-14 22:40:08 +0200196 .getc = sh_serial_getc,
197 .tstc = sh_serial_tstc,
198};
199
200void sh_serial_initialize(void)
201{
202 serial_register(&sh_serial_drv);
203}
204
205__weak struct serial_device *default_serial_console(void)
206{
207 return &sh_serial_drv;
208}