blob: 67cc0dc8da6c382f326f7978eb305c81430d7fc4 [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 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02006 * SPDX-License-Identifier: GPL-2.0+
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +09007 */
8
9#include <common.h>
Jean-Christophe PLAGNIOL-VILLARDb27a8e32009-01-11 16:35:16 +010010#include <asm/io.h>
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +090011#include <asm/processor.h>
Nobuhiro Iwamatsua5579ca2010-10-26 03:55:15 +090012#include "serial_sh.h"
Marek Vasut904d3d72012-09-14 22:40:08 +020013#include <serial.h>
14#include <linux/compiler.h>
John Rigby0d21ed02010-12-20 18:27:51 -070015
Nobuhiro Iwamatsufcabccc2008-08-22 17:48:51 +090016#if defined(CONFIG_CONS_SCIF0)
17# define SCIF_BASE SCIF0_BASE
18#elif defined(CONFIG_CONS_SCIF1)
19# define SCIF_BASE SCIF1_BASE
20#elif defined(CONFIG_CONS_SCIF2)
21# define SCIF_BASE SCIF2_BASE
22#elif defined(CONFIG_CONS_SCIF3)
23# define SCIF_BASE SCIF3_BASE
24#elif defined(CONFIG_CONS_SCIF4)
25# define SCIF_BASE SCIF4_BASE
26#elif defined(CONFIG_CONS_SCIF5)
27# define SCIF_BASE SCIF5_BASE
Phil Edworthy04a62752012-05-15 22:15:51 +000028#elif defined(CONFIG_CONS_SCIF6)
29# define SCIF_BASE SCIF6_BASE
30#elif defined(CONFIG_CONS_SCIF7)
31# define SCIF_BASE SCIF7_BASE
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +090032#else
Nobuhiro Iwamatsufcabccc2008-08-22 17:48:51 +090033# error "Default SCIF doesn't set....."
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +090034#endif
35
Nobuhiro Iwamatsua5579ca2010-10-26 03:55:15 +090036#if defined(CONFIG_SCIF_A)
37 #define SCIF_BASE_PORT PORT_SCIFA
Yoshihiro Shimodae6258342008-01-09 14:30:02 +090038#else
Nobuhiro Iwamatsua5579ca2010-10-26 03:55:15 +090039 #define SCIF_BASE_PORT PORT_SCIF
Yoshihiro Shimodae6258342008-01-09 14:30:02 +090040#endif
41
Nobuhiro Iwamatsua5579ca2010-10-26 03:55:15 +090042static struct uart_port sh_sci = {
43 .membase = (unsigned char*)SCIF_BASE,
44 .mapbase = SCIF_BASE,
45 .type = SCIF_BASE_PORT,
46};
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +090047
Marek Vasut904d3d72012-09-14 22:40:08 +020048static void sh_serial_setbrg(void)
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +090049{
Nobuhiro Iwamatsua5579ca2010-10-26 03:55:15 +090050 DECLARE_GLOBAL_DATA_PTR;
Nobuhiro Iwamatsue9900db2013-08-20 10:31:53 +090051
52 sci_out(&sh_sci, SCBRR,
53 SCBRR_VALUE(gd->baudrate, CONFIG_SH_SCIF_CLK_FREQ));
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +090054}
55
Marek Vasut904d3d72012-09-14 22:40:08 +020056static int sh_serial_init(void)
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +090057{
Nobuhiro Iwamatsua5579ca2010-10-26 03:55:15 +090058 sci_out(&sh_sci, SCSCR , SCSCR_INIT(&sh_sci));
59 sci_out(&sh_sci, SCSCR , SCSCR_INIT(&sh_sci));
60 sci_out(&sh_sci, SCSMR, 0);
61 sci_out(&sh_sci, SCSMR, 0);
62 sci_out(&sh_sci, SCFCR, SCFCR_RFRST|SCFCR_TFRST);
63 sci_in(&sh_sci, SCFCR);
64 sci_out(&sh_sci, SCFCR, 0);
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +090065
66 serial_setbrg();
67 return 0;
68}
69
Nobuhiro Iwamatsua5579ca2010-10-26 03:55:15 +090070#if defined(CONFIG_CPU_SH7760) || \
71 defined(CONFIG_CPU_SH7780) || \
72 defined(CONFIG_CPU_SH7785) || \
73 defined(CONFIG_CPU_SH7786)
74static int scif_rxfill(struct uart_port *port)
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +090075{
Nobuhiro Iwamatsua5579ca2010-10-26 03:55:15 +090076 return sci_in(port, SCRFDR) & 0xff;
77}
78#elif defined(CONFIG_CPU_SH7763)
79static int scif_rxfill(struct uart_port *port)
80{
81 if ((port->mapbase == 0xffe00000) ||
82 (port->mapbase == 0xffe08000)) {
83 /* SCIF0/1*/
84 return sci_in(port, SCRFDR) & 0xff;
85 } else {
86 /* SCIF2 */
87 return sci_in(port, SCFDR) & SCIF2_RFDC_MASK;
88 }
89}
90#elif defined(CONFIG_ARCH_SH7372)
91static int scif_rxfill(struct uart_port *port)
92{
93 if (port->type == PORT_SCIFA)
94 return sci_in(port, SCFDR) & SCIF_RFDC_MASK;
95 else
96 return sci_in(port, SCRFDR);
97}
Nobuhiro Iwamatsu1b36beb2008-03-06 14:05:53 +090098#else
Nobuhiro Iwamatsua5579ca2010-10-26 03:55:15 +090099static int scif_rxfill(struct uart_port *port)
100{
101 return sci_in(port, SCFDR) & SCIF_RFDC_MASK;
102}
Nobuhiro Iwamatsu1b36beb2008-03-06 14:05:53 +0900103#endif
Nobuhiro Iwamatsua5579ca2010-10-26 03:55:15 +0900104
105static int serial_rx_fifo_level(void)
106{
107 return scif_rxfill(&sh_sci);
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900108}
109
Tetsuyuki Kobayashi5d2b5a22012-11-19 21:37:38 +0000110static void handle_error(void)
111{
112 sci_in(&sh_sci, SCxSR);
113 sci_out(&sh_sci, SCxSR, SCxSR_ERROR_CLEAR(&sh_sci));
114 sci_in(&sh_sci, SCLSR);
115 sci_out(&sh_sci, SCLSR, 0x00);
116}
117
Nobuhiro Iwamatsu6564b1a2008-06-06 16:16:08 +0900118void serial_raw_putc(const char c)
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900119{
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900120 while (1) {
Nobuhiro Iwamatsua5579ca2010-10-26 03:55:15 +0900121 /* Tx fifo is empty */
122 if (sci_in(&sh_sci, SCxSR) & SCxSR_TEND(&sh_sci))
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900123 break;
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900124 }
125
Nobuhiro Iwamatsua5579ca2010-10-26 03:55:15 +0900126 sci_out(&sh_sci, SCxTDR, c);
127 sci_out(&sh_sci, SCxSR, sci_in(&sh_sci, SCxSR) & ~SCxSR_TEND(&sh_sci));
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900128}
129
Marek Vasut904d3d72012-09-14 22:40:08 +0200130static void sh_serial_putc(const char c)
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900131{
132 if (c == '\n')
Nobuhiro Iwamatsu6564b1a2008-06-06 16:16:08 +0900133 serial_raw_putc('\r');
134 serial_raw_putc(c);
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900135}
136
Marek Vasut904d3d72012-09-14 22:40:08 +0200137static int sh_serial_tstc(void)
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900138{
Tetsuyuki Kobayashi5d2b5a22012-11-19 21:37:38 +0000139 if (sci_in(&sh_sci, SCxSR) & SCIF_ERRORS) {
140 handle_error();
141 return 0;
142 }
143
Nobuhiro Iwamatsufcabccc2008-08-22 17:48:51 +0900144 return serial_rx_fifo_level() ? 1 : 0;
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900145}
146
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900147
Nobuhiro Iwamatsu6564b1a2008-06-06 16:16:08 +0900148int serial_getc_check(void)
149{
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900150 unsigned short status;
151
Nobuhiro Iwamatsua5579ca2010-10-26 03:55:15 +0900152 status = sci_in(&sh_sci, SCxSR);
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900153
Nobuhiro Iwamatsua5579ca2010-10-26 03:55:15 +0900154 if (status & SCIF_ERRORS)
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900155 handle_error();
Nobuhiro Iwamatsua5579ca2010-10-26 03:55:15 +0900156 if (sci_in(&sh_sci, SCLSR) & SCxSR_ORER(&sh_sci))
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900157 handle_error();
Nobuhiro Iwamatsua5579ca2010-10-26 03:55:15 +0900158 return status & (SCIF_DR | SCxSR_RDxF(&sh_sci));
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900159}
160
Marek Vasut904d3d72012-09-14 22:40:08 +0200161static int sh_serial_getc(void)
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900162{
Nobuhiro Iwamatsu6564b1a2008-06-06 16:16:08 +0900163 unsigned short status;
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900164 char ch;
Nobuhiro Iwamatsufcabccc2008-08-22 17:48:51 +0900165
166 while (!serial_getc_check())
167 ;
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900168
Nobuhiro Iwamatsua5579ca2010-10-26 03:55:15 +0900169 ch = sci_in(&sh_sci, SCxRDR);
170 status = sci_in(&sh_sci, SCxSR);
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900171
Nobuhiro Iwamatsua5579ca2010-10-26 03:55:15 +0900172 sci_out(&sh_sci, SCxSR, SCxSR_RDxF_CLEAR(&sh_sci));
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900173
Nobuhiro Iwamatsua5579ca2010-10-26 03:55:15 +0900174 if (status & SCIF_ERRORS)
175 handle_error();
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900176
Nobuhiro Iwamatsua5579ca2010-10-26 03:55:15 +0900177 if (sci_in(&sh_sci, SCLSR) & SCxSR_ORER(&sh_sci))
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900178 handle_error();
Nobuhiro Iwamatsu6564b1a2008-06-06 16:16:08 +0900179 return ch;
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900180}
Marek Vasut904d3d72012-09-14 22:40:08 +0200181
Marek Vasut904d3d72012-09-14 22:40:08 +0200182static struct serial_device sh_serial_drv = {
183 .name = "sh_serial",
184 .start = sh_serial_init,
185 .stop = NULL,
186 .setbrg = sh_serial_setbrg,
187 .putc = sh_serial_putc,
Marek Vasutd9c64492012-10-06 14:07:02 +0000188 .puts = default_serial_puts,
Marek Vasut904d3d72012-09-14 22:40:08 +0200189 .getc = sh_serial_getc,
190 .tstc = sh_serial_tstc,
191};
192
193void sh_serial_initialize(void)
194{
195 serial_register(&sh_serial_drv);
196}
197
198__weak struct serial_device *default_serial_console(void)
199{
200 return &sh_serial_drv;
201}