blob: ff2cdc58472c8c8c918e5037f79fc6da3f040949 [file] [log] [blame]
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +09001/*
2 * SuperH SCIF device driver.
Nobuhiro Iwamatsu3f6c8e32010-10-26 03:55:15 +09003 * Copyright (C) 2007,2008,2010 Nobuhiro Iwamatsu
4 * Copyright (C) 2002 - 2008 Paul Mundt
Wolfgang Denk61fb15c52007-12-27 01:52:50 +01005 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02006 * SPDX-License-Identifier: GPL-2.0+
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +09007 */
8
9#include <common.h>
Jean-Christophe PLAGNIOL-VILLARDfc83c922009-01-11 16:35:16 +010010#include <asm/io.h>
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +090011#include <asm/processor.h>
Nobuhiro Iwamatsu3f6c8e32010-10-26 03:55:15 +090012#include "serial_sh.h"
Marek Vasut8bdd7ef2012-09-14 22:40:08 +020013#include <serial.h>
14#include <linux/compiler.h>
John Rigby29565322010-12-20 18:27:51 -070015
Nobuhiro Iwamatsuab09f432008-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 Edworthy99744b72012-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 Iwamatsu0b135cf2007-05-13 20:58:00 +090032#else
Nobuhiro Iwamatsuab09f432008-08-22 17:48:51 +090033# error "Default SCIF doesn't set....."
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +090034#endif
35
Nobuhiro Iwamatsu3f6c8e32010-10-26 03:55:15 +090036#if defined(CONFIG_SCIF_A)
37 #define SCIF_BASE_PORT PORT_SCIFA
Yoshihiro Shimoda7c10c572008-01-09 14:30:02 +090038#else
Nobuhiro Iwamatsu3f6c8e32010-10-26 03:55:15 +090039 #define SCIF_BASE_PORT PORT_SCIF
Yoshihiro Shimoda7c10c572008-01-09 14:30:02 +090040#endif
41
Nobuhiro Iwamatsu3f6c8e32010-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 Iwamatsu0b135cf2007-05-13 20:58:00 +090047
Marek Vasut8bdd7ef2012-09-14 22:40:08 +020048static void sh_serial_setbrg(void)
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +090049{
Nobuhiro Iwamatsu3f6c8e32010-10-26 03:55:15 +090050 DECLARE_GLOBAL_DATA_PTR;
51 sci_out(&sh_sci, SCBRR, SCBRR_VALUE(gd->baudrate, CONFIG_SYS_CLK_FREQ));
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +090052}
53
Marek Vasut8bdd7ef2012-09-14 22:40:08 +020054static int sh_serial_init(void)
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +090055{
Nobuhiro Iwamatsu3f6c8e32010-10-26 03:55:15 +090056 sci_out(&sh_sci, SCSCR , SCSCR_INIT(&sh_sci));
57 sci_out(&sh_sci, SCSCR , SCSCR_INIT(&sh_sci));
58 sci_out(&sh_sci, SCSMR, 0);
59 sci_out(&sh_sci, SCSMR, 0);
60 sci_out(&sh_sci, SCFCR, SCFCR_RFRST|SCFCR_TFRST);
61 sci_in(&sh_sci, SCFCR);
62 sci_out(&sh_sci, SCFCR, 0);
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +090063
64 serial_setbrg();
65 return 0;
66}
67
Nobuhiro Iwamatsu3f6c8e32010-10-26 03:55:15 +090068#if defined(CONFIG_CPU_SH7760) || \
69 defined(CONFIG_CPU_SH7780) || \
70 defined(CONFIG_CPU_SH7785) || \
71 defined(CONFIG_CPU_SH7786)
72static int scif_rxfill(struct uart_port *port)
73{
74 return sci_in(port, SCRFDR) & 0xff;
75}
76#elif defined(CONFIG_CPU_SH7763)
77static int scif_rxfill(struct uart_port *port)
78{
79 if ((port->mapbase == 0xffe00000) ||
80 (port->mapbase == 0xffe08000)) {
81 /* SCIF0/1*/
82 return sci_in(port, SCRFDR) & 0xff;
83 } else {
84 /* SCIF2 */
85 return sci_in(port, SCFDR) & SCIF2_RFDC_MASK;
86 }
87}
88#elif defined(CONFIG_ARCH_SH7372)
89static int scif_rxfill(struct uart_port *port)
90{
91 if (port->type == PORT_SCIFA)
92 return sci_in(port, SCFDR) & SCIF_RFDC_MASK;
93 else
94 return sci_in(port, SCRFDR);
95}
96#else
97static int scif_rxfill(struct uart_port *port)
98{
99 return sci_in(port, SCFDR) & SCIF_RFDC_MASK;
100}
101#endif
102
Nobuhiro Iwamatsu08c5fab2008-06-06 16:16:08 +0900103static int serial_rx_fifo_level(void)
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900104{
Nobuhiro Iwamatsu3f6c8e32010-10-26 03:55:15 +0900105 return scif_rxfill(&sh_sci);
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900106}
107
Tetsuyuki Kobayashi7c791b32012-11-19 21:37:38 +0000108static void handle_error(void)
109{
110 sci_in(&sh_sci, SCxSR);
111 sci_out(&sh_sci, SCxSR, SCxSR_ERROR_CLEAR(&sh_sci));
112 sci_in(&sh_sci, SCLSR);
113 sci_out(&sh_sci, SCLSR, 0x00);
114}
115
Nobuhiro Iwamatsu08c5fab2008-06-06 16:16:08 +0900116void serial_raw_putc(const char c)
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900117{
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900118 while (1) {
Nobuhiro Iwamatsu3f6c8e32010-10-26 03:55:15 +0900119 /* Tx fifo is empty */
120 if (sci_in(&sh_sci, SCxSR) & SCxSR_TEND(&sh_sci))
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900121 break;
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900122 }
123
Nobuhiro Iwamatsu3f6c8e32010-10-26 03:55:15 +0900124 sci_out(&sh_sci, SCxTDR, c);
125 sci_out(&sh_sci, SCxSR, sci_in(&sh_sci, SCxSR) & ~SCxSR_TEND(&sh_sci));
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900126}
127
Marek Vasut8bdd7ef2012-09-14 22:40:08 +0200128static void sh_serial_putc(const char c)
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900129{
130 if (c == '\n')
Nobuhiro Iwamatsu08c5fab2008-06-06 16:16:08 +0900131 serial_raw_putc('\r');
132 serial_raw_putc(c);
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900133}
134
Marek Vasut8bdd7ef2012-09-14 22:40:08 +0200135static int sh_serial_tstc(void)
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900136{
Tetsuyuki Kobayashi7c791b32012-11-19 21:37:38 +0000137 if (sci_in(&sh_sci, SCxSR) & SCIF_ERRORS) {
138 handle_error();
139 return 0;
140 }
141
Nobuhiro Iwamatsuab09f432008-08-22 17:48:51 +0900142 return serial_rx_fifo_level() ? 1 : 0;
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900143}
144
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900145
Nobuhiro Iwamatsu08c5fab2008-06-06 16:16:08 +0900146int serial_getc_check(void)
147{
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900148 unsigned short status;
149
Nobuhiro Iwamatsu3f6c8e32010-10-26 03:55:15 +0900150 status = sci_in(&sh_sci, SCxSR);
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900151
Nobuhiro Iwamatsu3f6c8e32010-10-26 03:55:15 +0900152 if (status & SCIF_ERRORS)
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900153 handle_error();
Nobuhiro Iwamatsu3f6c8e32010-10-26 03:55:15 +0900154 if (sci_in(&sh_sci, SCLSR) & SCxSR_ORER(&sh_sci))
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900155 handle_error();
Nobuhiro Iwamatsu3f6c8e32010-10-26 03:55:15 +0900156 return status & (SCIF_DR | SCxSR_RDxF(&sh_sci));
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900157}
158
Marek Vasut8bdd7ef2012-09-14 22:40:08 +0200159static int sh_serial_getc(void)
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900160{
Nobuhiro Iwamatsu08c5fab2008-06-06 16:16:08 +0900161 unsigned short status;
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900162 char ch;
Nobuhiro Iwamatsuab09f432008-08-22 17:48:51 +0900163
164 while (!serial_getc_check())
165 ;
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900166
Nobuhiro Iwamatsu3f6c8e32010-10-26 03:55:15 +0900167 ch = sci_in(&sh_sci, SCxRDR);
168 status = sci_in(&sh_sci, SCxSR);
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900169
Nobuhiro Iwamatsu3f6c8e32010-10-26 03:55:15 +0900170 sci_out(&sh_sci, SCxSR, SCxSR_RDxF_CLEAR(&sh_sci));
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900171
Nobuhiro Iwamatsu3f6c8e32010-10-26 03:55:15 +0900172 if (status & SCIF_ERRORS)
173 handle_error();
174
175 if (sci_in(&sh_sci, SCLSR) & SCxSR_ORER(&sh_sci))
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900176 handle_error();
Nobuhiro Iwamatsu08c5fab2008-06-06 16:16:08 +0900177 return ch;
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900178}
Marek Vasut8bdd7ef2012-09-14 22:40:08 +0200179
Marek Vasut8bdd7ef2012-09-14 22:40:08 +0200180static struct serial_device sh_serial_drv = {
181 .name = "sh_serial",
182 .start = sh_serial_init,
183 .stop = NULL,
184 .setbrg = sh_serial_setbrg,
185 .putc = sh_serial_putc,
Marek Vasutec3fd682012-10-06 14:07:02 +0000186 .puts = default_serial_puts,
Marek Vasut8bdd7ef2012-09-14 22:40:08 +0200187 .getc = sh_serial_getc,
188 .tstc = sh_serial_tstc,
189};
190
191void sh_serial_initialize(void)
192{
193 serial_register(&sh_serial_drv);
194}
195
196__weak struct serial_device *default_serial_console(void)
197{
198 return &sh_serial_drv;
199}