blob: acfcc2954a9e60ff81bece443b450d788e529fa2 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +09002/*
3 * SuperH SCIF device driver.
Nobuhiro Iwamatsu48ca8822013-07-23 13:58:20 +09004 * Copyright (C) 2013 Renesas Electronics Corporation
Nobuhiro Iwamatsu59088e42015-02-12 13:48:04 +09005 * Copyright (C) 2007,2008,2010, 2014 Nobuhiro Iwamatsu
Nobuhiro Iwamatsu3f6c8e32010-10-26 03:55:15 +09006 * Copyright (C) 2002 - 2008 Paul Mundt
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +09007 */
8
9#include <common.h>
Nobuhiro Iwamatsu59088e42015-02-12 13:48:04 +090010#include <errno.h>
Marek Vasut81714992017-07-21 23:19:18 +020011#include <clk.h>
Nobuhiro Iwamatsu59088e42015-02-12 13:48:04 +090012#include <dm.h>
Jean-Christophe PLAGNIOL-VILLARDfc83c922009-01-11 16:35:16 +010013#include <asm/io.h>
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +090014#include <asm/processor.h>
Marek Vasut8bdd7ef2012-09-14 22:40:08 +020015#include <serial.h>
16#include <linux/compiler.h>
Nobuhiro Iwamatsu59088e42015-02-12 13:48:04 +090017#include <dm/platform_data/serial_sh.h>
18#include "serial_sh.h"
19
Yoshinori Sato359787c2016-04-18 16:51:04 +090020DECLARE_GLOBAL_DATA_PTR;
21
Marek Vasut10e91cf2019-05-07 22:31:23 +020022#if defined(CONFIG_CPU_SH7780)
Nobuhiro Iwamatsu59088e42015-02-12 13:48:04 +090023static int scif_rxfill(struct uart_port *port)
24{
25 return sci_in(port, SCRFDR) & 0xff;
26}
27#elif defined(CONFIG_CPU_SH7763)
28static int scif_rxfill(struct uart_port *port)
29{
30 if ((port->mapbase == 0xffe00000) ||
31 (port->mapbase == 0xffe08000)) {
32 /* SCIF0/1*/
33 return sci_in(port, SCRFDR) & 0xff;
34 } else {
35 /* SCIF2 */
36 return sci_in(port, SCFDR) & SCIF2_RFDC_MASK;
37 }
38}
Nobuhiro Iwamatsu59088e42015-02-12 13:48:04 +090039#else
40static int scif_rxfill(struct uart_port *port)
41{
42 return sci_in(port, SCFDR) & SCIF_RFDC_MASK;
43}
44#endif
45
46static void sh_serial_init_generic(struct uart_port *port)
47{
48 sci_out(port, SCSCR , SCSCR_INIT(port));
49 sci_out(port, SCSCR , SCSCR_INIT(port));
50 sci_out(port, SCSMR, 0);
51 sci_out(port, SCSMR, 0);
52 sci_out(port, SCFCR, SCFCR_RFRST|SCFCR_TFRST);
53 sci_in(port, SCFCR);
54 sci_out(port, SCFCR, 0);
Marek Vasut67180fe2019-05-01 18:20:00 +020055#if defined(CONFIG_RZA1)
56 sci_out(port, SCSPTR, 0x0003);
57#endif
Nobuhiro Iwamatsu59088e42015-02-12 13:48:04 +090058}
59
60static void
61sh_serial_setbrg_generic(struct uart_port *port, int clk, int baudrate)
62{
63 if (port->clk_mode == EXT_CLK) {
64 unsigned short dl = DL_VALUE(baudrate, clk);
65 sci_out(port, DL, dl);
Nobuhiro Iwamatsu89f99a62014-12-10 14:42:05 +090066 /* Need wait: Clock * 1/dl * 1/16 */
Nobuhiro Iwamatsu59088e42015-02-12 13:48:04 +090067 udelay((1000000 * dl * 16 / clk) * 1000 + 1);
68 } else {
69 sci_out(port, SCBRR, SCBRR_VALUE(baudrate, clk));
70 }
71}
72
73static void handle_error(struct uart_port *port)
74{
75 sci_in(port, SCxSR);
76 sci_out(port, SCxSR, SCxSR_ERROR_CLEAR(port));
77 sci_in(port, SCLSR);
78 sci_out(port, SCLSR, 0x00);
79}
80
81static int serial_raw_putc(struct uart_port *port, const char c)
82{
83 /* Tx fifo is empty */
84 if (!(sci_in(port, SCxSR) & SCxSR_TEND(port)))
85 return -EAGAIN;
86
87 sci_out(port, SCxTDR, c);
88 sci_out(port, SCxSR, sci_in(port, SCxSR) & ~SCxSR_TEND(port));
89
90 return 0;
91}
92
93static int serial_rx_fifo_level(struct uart_port *port)
94{
95 return scif_rxfill(port);
96}
97
98static int sh_serial_tstc_generic(struct uart_port *port)
99{
100 if (sci_in(port, SCxSR) & SCIF_ERRORS) {
101 handle_error(port);
102 return 0;
103 }
104
105 return serial_rx_fifo_level(port) ? 1 : 0;
106}
107
108static int serial_getc_check(struct uart_port *port)
109{
110 unsigned short status;
111
112 status = sci_in(port, SCxSR);
113
114 if (status & SCIF_ERRORS)
115 handle_error(port);
116 if (sci_in(port, SCLSR) & SCxSR_ORER(port))
117 handle_error(port);
118 return status & (SCIF_DR | SCxSR_RDxF(port));
119}
120
121static int sh_serial_getc_generic(struct uart_port *port)
122{
123 unsigned short status;
124 char ch;
125
126 if (!serial_getc_check(port))
127 return -EAGAIN;
128
129 ch = sci_in(port, SCxRDR);
130 status = sci_in(port, SCxSR);
131
132 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
133
134 if (status & SCIF_ERRORS)
135 handle_error(port);
136
137 if (sci_in(port, SCLSR) & SCxSR_ORER(port))
138 handle_error(port);
139
140 return ch;
141}
142
Marek Vasut5c44ddc2018-02-16 01:33:27 +0100143#if CONFIG_IS_ENABLED(DM_SERIAL)
Nobuhiro Iwamatsu59088e42015-02-12 13:48:04 +0900144
145static int sh_serial_pending(struct udevice *dev, bool input)
146{
147 struct uart_port *priv = dev_get_priv(dev);
148
149 return sh_serial_tstc_generic(priv);
150}
151
152static int sh_serial_putc(struct udevice *dev, const char ch)
153{
154 struct uart_port *priv = dev_get_priv(dev);
155
156 return serial_raw_putc(priv, ch);
157}
158
159static int sh_serial_getc(struct udevice *dev)
160{
161 struct uart_port *priv = dev_get_priv(dev);
162
163 return sh_serial_getc_generic(priv);
164}
165
166static int sh_serial_setbrg(struct udevice *dev, int baudrate)
167{
168 struct sh_serial_platdata *plat = dev_get_platdata(dev);
169 struct uart_port *priv = dev_get_priv(dev);
170
171 sh_serial_setbrg_generic(priv, plat->clk, baudrate);
172
173 return 0;
174}
175
176static int sh_serial_probe(struct udevice *dev)
177{
178 struct sh_serial_platdata *plat = dev_get_platdata(dev);
179 struct uart_port *priv = dev_get_priv(dev);
180
181 priv->membase = (unsigned char *)plat->base;
182 priv->mapbase = plat->base;
183 priv->type = plat->type;
184 priv->clk_mode = plat->clk_mode;
185
186 sh_serial_init_generic(priv);
187
188 return 0;
189}
190
191static const struct dm_serial_ops sh_serial_ops = {
192 .putc = sh_serial_putc,
193 .pending = sh_serial_pending,
194 .getc = sh_serial_getc,
195 .setbrg = sh_serial_setbrg,
196};
197
Marek Vasut5c44ddc2018-02-16 01:33:27 +0100198#if CONFIG_IS_ENABLED(OF_CONTROL)
Yoshinori Sato359787c2016-04-18 16:51:04 +0900199static const struct udevice_id sh_serial_id[] ={
Yoshinori Sato747431b2016-04-18 16:51:05 +0900200 {.compatible = "renesas,sci", .data = PORT_SCI},
Yoshinori Sato359787c2016-04-18 16:51:04 +0900201 {.compatible = "renesas,scif", .data = PORT_SCIF},
202 {.compatible = "renesas,scifa", .data = PORT_SCIFA},
203 {}
204};
205
206static int sh_serial_ofdata_to_platdata(struct udevice *dev)
207{
208 struct sh_serial_platdata *plat = dev_get_platdata(dev);
Marek Vasut81714992017-07-21 23:19:18 +0200209 struct clk sh_serial_clk;
Yoshinori Sato359787c2016-04-18 16:51:04 +0900210 fdt_addr_t addr;
Marek Vasut81714992017-07-21 23:19:18 +0200211 int ret;
Yoshinori Sato359787c2016-04-18 16:51:04 +0900212
Marek Vasutc4937562018-01-17 22:36:37 +0100213 addr = devfdt_get_addr(dev);
214 if (!addr)
Yoshinori Sato359787c2016-04-18 16:51:04 +0900215 return -EINVAL;
216
217 plat->base = addr;
Marek Vasut81714992017-07-21 23:19:18 +0200218
219 ret = clk_get_by_name(dev, "fck", &sh_serial_clk);
Marek Vasut791c1742017-09-15 21:11:27 +0200220 if (!ret) {
221 ret = clk_enable(&sh_serial_clk);
222 if (!ret)
223 plat->clk = clk_get_rate(&sh_serial_clk);
224 } else {
Marek Vasut81714992017-07-21 23:19:18 +0200225 plat->clk = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev),
226 "clock", 1);
Marek Vasut791c1742017-09-15 21:11:27 +0200227 }
Marek Vasut81714992017-07-21 23:19:18 +0200228
Yoshinori Sato359787c2016-04-18 16:51:04 +0900229 plat->type = dev_get_driver_data(dev);
230 return 0;
231}
232#endif
233
Nobuhiro Iwamatsu59088e42015-02-12 13:48:04 +0900234U_BOOT_DRIVER(serial_sh) = {
235 .name = "serial_sh",
236 .id = UCLASS_SERIAL,
Yoshinori Sato359787c2016-04-18 16:51:04 +0900237 .of_match = of_match_ptr(sh_serial_id),
238 .ofdata_to_platdata = of_match_ptr(sh_serial_ofdata_to_platdata),
239 .platdata_auto_alloc_size = sizeof(struct sh_serial_platdata),
Nobuhiro Iwamatsu59088e42015-02-12 13:48:04 +0900240 .probe = sh_serial_probe,
241 .ops = &sh_serial_ops,
Bin Meng46879192018-10-24 06:36:36 -0700242#if !CONFIG_IS_ENABLED(OF_CONTROL)
Nobuhiro Iwamatsu59088e42015-02-12 13:48:04 +0900243 .flags = DM_FLAG_PRE_RELOC,
Bin Meng46879192018-10-24 06:36:36 -0700244#endif
Nobuhiro Iwamatsu59088e42015-02-12 13:48:04 +0900245 .priv_auto_alloc_size = sizeof(struct uart_port),
246};
247
248#else /* CONFIG_DM_SERIAL */
John Rigby29565322010-12-20 18:27:51 -0700249
Nobuhiro Iwamatsuab09f432008-08-22 17:48:51 +0900250#if defined(CONFIG_CONS_SCIF0)
251# define SCIF_BASE SCIF0_BASE
252#elif defined(CONFIG_CONS_SCIF1)
253# define SCIF_BASE SCIF1_BASE
254#elif defined(CONFIG_CONS_SCIF2)
255# define SCIF_BASE SCIF2_BASE
256#elif defined(CONFIG_CONS_SCIF3)
257# define SCIF_BASE SCIF3_BASE
258#elif defined(CONFIG_CONS_SCIF4)
259# define SCIF_BASE SCIF4_BASE
260#elif defined(CONFIG_CONS_SCIF5)
261# define SCIF_BASE SCIF5_BASE
Phil Edworthy99744b72012-05-15 22:15:51 +0000262#elif defined(CONFIG_CONS_SCIF6)
263# define SCIF_BASE SCIF6_BASE
264#elif defined(CONFIG_CONS_SCIF7)
265# define SCIF_BASE SCIF7_BASE
Marek Vasut451e22f2018-04-12 15:23:46 +0200266#elif defined(CONFIG_CONS_SCIFA0)
267# define SCIF_BASE SCIFA0_BASE
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900268#else
Nobuhiro Iwamatsuab09f432008-08-22 17:48:51 +0900269# error "Default SCIF doesn't set....."
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900270#endif
271
Nobuhiro Iwamatsu3f6c8e32010-10-26 03:55:15 +0900272#if defined(CONFIG_SCIF_A)
273 #define SCIF_BASE_PORT PORT_SCIFA
Yoshinori Sato747431b2016-04-18 16:51:05 +0900274#elif defined(CONFIG_SCI)
275 #define SCIF_BASE_PORT PORT_SCI
Yoshihiro Shimoda7c10c572008-01-09 14:30:02 +0900276#else
Nobuhiro Iwamatsu3f6c8e32010-10-26 03:55:15 +0900277 #define SCIF_BASE_PORT PORT_SCIF
Yoshihiro Shimoda7c10c572008-01-09 14:30:02 +0900278#endif
279
Nobuhiro Iwamatsu3f6c8e32010-10-26 03:55:15 +0900280static struct uart_port sh_sci = {
Nobuhiro Iwamatsu59088e42015-02-12 13:48:04 +0900281 .membase = (unsigned char *)SCIF_BASE,
Nobuhiro Iwamatsu3f6c8e32010-10-26 03:55:15 +0900282 .mapbase = SCIF_BASE,
283 .type = SCIF_BASE_PORT,
Nobuhiro Iwamatsu59088e42015-02-12 13:48:04 +0900284#ifdef CONFIG_SCIF_USE_EXT_CLK
285 .clk_mode = EXT_CLK,
286#endif
Nobuhiro Iwamatsu3f6c8e32010-10-26 03:55:15 +0900287};
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900288
Marek Vasut8bdd7ef2012-09-14 22:40:08 +0200289static void sh_serial_setbrg(void)
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900290{
Nobuhiro Iwamatsu3f6c8e32010-10-26 03:55:15 +0900291 DECLARE_GLOBAL_DATA_PTR;
Nobuhiro Iwamatsu59088e42015-02-12 13:48:04 +0900292 struct uart_port *port = &sh_sci;
293
294 sh_serial_setbrg_generic(port, CONFIG_SH_SCIF_CLK_FREQ, gd->baudrate);
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900295}
296
Marek Vasut8bdd7ef2012-09-14 22:40:08 +0200297static int sh_serial_init(void)
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900298{
Nobuhiro Iwamatsu59088e42015-02-12 13:48:04 +0900299 struct uart_port *port = &sh_sci;
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900300
Nobuhiro Iwamatsu59088e42015-02-12 13:48:04 +0900301 sh_serial_init_generic(port);
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900302 serial_setbrg();
Nobuhiro Iwamatsu59088e42015-02-12 13:48:04 +0900303
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900304 return 0;
305}
306
Marek Vasut8bdd7ef2012-09-14 22:40:08 +0200307static void sh_serial_putc(const char c)
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900308{
Nobuhiro Iwamatsu59088e42015-02-12 13:48:04 +0900309 struct uart_port *port = &sh_sci;
310
311 if (c == '\n') {
312 while (1) {
313 if (serial_raw_putc(port, '\r') != -EAGAIN)
314 break;
315 }
316 }
317 while (1) {
318 if (serial_raw_putc(port, c) != -EAGAIN)
319 break;
320 }
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900321}
322
Marek Vasut8bdd7ef2012-09-14 22:40:08 +0200323static int sh_serial_tstc(void)
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900324{
Nobuhiro Iwamatsu59088e42015-02-12 13:48:04 +0900325 struct uart_port *port = &sh_sci;
Tetsuyuki Kobayashi7c791b32012-11-19 21:37:38 +0000326
Nobuhiro Iwamatsu59088e42015-02-12 13:48:04 +0900327 return sh_serial_tstc_generic(port);
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900328}
329
Marek Vasut8bdd7ef2012-09-14 22:40:08 +0200330static int sh_serial_getc(void)
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900331{
Nobuhiro Iwamatsu59088e42015-02-12 13:48:04 +0900332 struct uart_port *port = &sh_sci;
333 int ch;
Nobuhiro Iwamatsuab09f432008-08-22 17:48:51 +0900334
Nobuhiro Iwamatsu59088e42015-02-12 13:48:04 +0900335 while (1) {
336 ch = sh_serial_getc_generic(port);
337 if (ch != -EAGAIN)
338 break;
339 }
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900340
Nobuhiro Iwamatsu08c5fab2008-06-06 16:16:08 +0900341 return ch;
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900342}
Marek Vasut8bdd7ef2012-09-14 22:40:08 +0200343
Marek Vasut8bdd7ef2012-09-14 22:40:08 +0200344static struct serial_device sh_serial_drv = {
345 .name = "sh_serial",
346 .start = sh_serial_init,
347 .stop = NULL,
348 .setbrg = sh_serial_setbrg,
349 .putc = sh_serial_putc,
Marek Vasutec3fd682012-10-06 14:07:02 +0000350 .puts = default_serial_puts,
Marek Vasut8bdd7ef2012-09-14 22:40:08 +0200351 .getc = sh_serial_getc,
352 .tstc = sh_serial_tstc,
353};
354
355void sh_serial_initialize(void)
356{
357 serial_register(&sh_serial_drv);
358}
359
360__weak struct serial_device *default_serial_console(void)
361{
362 return &sh_serial_drv;
363}
Nobuhiro Iwamatsu59088e42015-02-12 13:48:04 +0900364#endif /* CONFIG_DM_SERIAL */