blob: e4cc4ee426039c7d3ad6cf7be31ddee9f84c771c [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
Simon Glass401d1c42020-10-30 21:38:53 -06009#include <asm/global_data.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>
Paul Barkeref7ab752023-10-16 10:25:35 +010012#include <clk.h>
13#include <dm.h>
Paul Barker966caed2023-10-19 15:30:44 +010014#include <dm/device_compat.h>
Nobuhiro Iwamatsu59088e42015-02-12 13:48:04 +090015#include <dm/platform_data/serial_sh.h>
Paul Barkeref7ab752023-10-16 10:25:35 +010016#include <errno.h>
17#include <linux/compiler.h>
Simon Glassc05ed002020-05-10 11:40:11 -060018#include <linux/delay.h>
Paul Barker966caed2023-10-19 15:30:44 +010019#include <reset.h>
Paul Barkeref7ab752023-10-16 10:25:35 +010020#include <serial.h>
Nobuhiro Iwamatsu59088e42015-02-12 13:48:04 +090021#include "serial_sh.h"
22
Yoshinori Sato359787c2016-04-18 16:51:04 +090023DECLARE_GLOBAL_DATA_PTR;
24
Marek Vasut10e91cf2019-05-07 22:31:23 +020025#if defined(CONFIG_CPU_SH7780)
Nobuhiro Iwamatsu59088e42015-02-12 13:48:04 +090026static int scif_rxfill(struct uart_port *port)
27{
28 return sci_in(port, SCRFDR) & 0xff;
29}
30#elif defined(CONFIG_CPU_SH7763)
31static int scif_rxfill(struct uart_port *port)
32{
33 if ((port->mapbase == 0xffe00000) ||
34 (port->mapbase == 0xffe08000)) {
35 /* SCIF0/1*/
36 return sci_in(port, SCRFDR) & 0xff;
37 } else {
38 /* SCIF2 */
39 return sci_in(port, SCFDR) & SCIF2_RFDC_MASK;
40 }
41}
Nobuhiro Iwamatsu59088e42015-02-12 13:48:04 +090042#else
43static int scif_rxfill(struct uart_port *port)
44{
45 return sci_in(port, SCFDR) & SCIF_RFDC_MASK;
46}
47#endif
48
49static void sh_serial_init_generic(struct uart_port *port)
50{
51 sci_out(port, SCSCR , SCSCR_INIT(port));
52 sci_out(port, SCSCR , SCSCR_INIT(port));
53 sci_out(port, SCSMR, 0);
54 sci_out(port, SCSMR, 0);
55 sci_out(port, SCFCR, SCFCR_RFRST|SCFCR_TFRST);
56 sci_in(port, SCFCR);
57 sci_out(port, SCFCR, 0);
Marek Vasut67180fe2019-05-01 18:20:00 +020058#if defined(CONFIG_RZA1)
59 sci_out(port, SCSPTR, 0x0003);
60#endif
Hai Phambbe36e22023-02-28 22:29:19 +010061
Paul Barkercaf35032023-10-16 10:25:23 +010062#if IS_ENABLED(CONFIG_RCAR_GEN2) || IS_ENABLED(CONFIG_RCAR_GEN3) || IS_ENABLED(CONFIG_RCAR_GEN4)
Hai Phambbe36e22023-02-28 22:29:19 +010063 if (port->type == PORT_HSCIF)
64 sci_out(port, HSSRR, HSSRR_SRE | HSSRR_SRCYC8);
Paul Barkercaf35032023-10-16 10:25:23 +010065#endif
Nobuhiro Iwamatsu59088e42015-02-12 13:48:04 +090066}
67
68static void
69sh_serial_setbrg_generic(struct uart_port *port, int clk, int baudrate)
70{
71 if (port->clk_mode == EXT_CLK) {
72 unsigned short dl = DL_VALUE(baudrate, clk);
73 sci_out(port, DL, dl);
Nobuhiro Iwamatsu89f99a62014-12-10 14:42:05 +090074 /* Need wait: Clock * 1/dl * 1/16 */
Nobuhiro Iwamatsu59088e42015-02-12 13:48:04 +090075 udelay((1000000 * dl * 16 / clk) * 1000 + 1);
76 } else {
77 sci_out(port, SCBRR, SCBRR_VALUE(baudrate, clk));
78 }
79}
80
81static void handle_error(struct uart_port *port)
82{
Paul Barker0f924d82023-10-19 15:30:43 +010083 /*
84 * Most errors are cleared by resetting the relevant error bits to zero
85 * in the FSR & LSR registers. For each register, a read followed by a
86 * write is needed according to the relevant datasheets.
87 */
88 unsigned short status = sci_in(port, SCxSR);
89 sci_out(port, SCxSR, status & ~SCxSR_ERRORS(port));
Nobuhiro Iwamatsu59088e42015-02-12 13:48:04 +090090 sci_in(port, SCLSR);
91 sci_out(port, SCLSR, 0x00);
Paul Barker0f924d82023-10-19 15:30:43 +010092
93 /*
94 * To clear framing errors, we also need to read and discard a
95 * character.
96 */
97 if ((port->type != PORT_SCI) && (status & SCIF_FER))
98 sci_in(port, SCxRDR);
Nobuhiro Iwamatsu59088e42015-02-12 13:48:04 +090099}
100
101static int serial_raw_putc(struct uart_port *port, const char c)
102{
103 /* Tx fifo is empty */
104 if (!(sci_in(port, SCxSR) & SCxSR_TEND(port)))
105 return -EAGAIN;
106
107 sci_out(port, SCxTDR, c);
108 sci_out(port, SCxSR, sci_in(port, SCxSR) & ~SCxSR_TEND(port));
109
110 return 0;
111}
112
113static int serial_rx_fifo_level(struct uart_port *port)
114{
115 return scif_rxfill(port);
116}
117
118static int sh_serial_tstc_generic(struct uart_port *port)
119{
120 if (sci_in(port, SCxSR) & SCIF_ERRORS) {
121 handle_error(port);
122 return 0;
123 }
124
125 return serial_rx_fifo_level(port) ? 1 : 0;
126}
127
128static int serial_getc_check(struct uart_port *port)
129{
130 unsigned short status;
131
132 status = sci_in(port, SCxSR);
133
134 if (status & SCIF_ERRORS)
135 handle_error(port);
136 if (sci_in(port, SCLSR) & SCxSR_ORER(port))
137 handle_error(port);
Marek Vasutf5ba5c92020-05-09 22:30:05 +0200138 status &= (SCIF_DR | SCxSR_RDxF(port));
139 if (status)
140 return status;
141 return scif_rxfill(port);
Nobuhiro Iwamatsu59088e42015-02-12 13:48:04 +0900142}
143
144static int sh_serial_getc_generic(struct uart_port *port)
145{
146 unsigned short status;
147 char ch;
148
149 if (!serial_getc_check(port))
150 return -EAGAIN;
151
152 ch = sci_in(port, SCxRDR);
153 status = sci_in(port, SCxSR);
154
155 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
156
157 if (status & SCIF_ERRORS)
158 handle_error(port);
159
160 if (sci_in(port, SCLSR) & SCxSR_ORER(port))
161 handle_error(port);
162
163 return ch;
164}
165
Marek Vasut5c44ddc2018-02-16 01:33:27 +0100166#if CONFIG_IS_ENABLED(DM_SERIAL)
Nobuhiro Iwamatsu59088e42015-02-12 13:48:04 +0900167
168static int sh_serial_pending(struct udevice *dev, bool input)
169{
170 struct uart_port *priv = dev_get_priv(dev);
171
172 return sh_serial_tstc_generic(priv);
173}
174
175static int sh_serial_putc(struct udevice *dev, const char ch)
176{
177 struct uart_port *priv = dev_get_priv(dev);
178
179 return serial_raw_putc(priv, ch);
180}
181
182static int sh_serial_getc(struct udevice *dev)
183{
184 struct uart_port *priv = dev_get_priv(dev);
185
186 return sh_serial_getc_generic(priv);
187}
188
189static int sh_serial_setbrg(struct udevice *dev, int baudrate)
190{
Simon Glass8a8d24b2020-12-03 16:55:23 -0700191 struct sh_serial_plat *plat = dev_get_plat(dev);
Nobuhiro Iwamatsu59088e42015-02-12 13:48:04 +0900192 struct uart_port *priv = dev_get_priv(dev);
193
194 sh_serial_setbrg_generic(priv, plat->clk, baudrate);
195
196 return 0;
197}
198
199static int sh_serial_probe(struct udevice *dev)
200{
Simon Glass8a8d24b2020-12-03 16:55:23 -0700201 struct sh_serial_plat *plat = dev_get_plat(dev);
Nobuhiro Iwamatsu59088e42015-02-12 13:48:04 +0900202 struct uart_port *priv = dev_get_priv(dev);
Paul Barker966caed2023-10-19 15:30:44 +0100203 struct reset_ctl rst;
204 int ret;
Nobuhiro Iwamatsu59088e42015-02-12 13:48:04 +0900205
206 priv->membase = (unsigned char *)plat->base;
207 priv->mapbase = plat->base;
208 priv->type = plat->type;
209 priv->clk_mode = plat->clk_mode;
210
Paul Barker966caed2023-10-19 15:30:44 +0100211 /* De-assert the module reset if it is defined. */
212 ret = reset_get_by_index(dev, 0, &rst);
213 if (!ret) {
214 ret = reset_deassert(&rst);
215 if (ret < 0) {
216 dev_err(dev, "failed to de-assert reset line\n");
217 return ret;
218 }
219 }
220
Nobuhiro Iwamatsu59088e42015-02-12 13:48:04 +0900221 sh_serial_init_generic(priv);
222
223 return 0;
224}
225
226static const struct dm_serial_ops sh_serial_ops = {
227 .putc = sh_serial_putc,
228 .pending = sh_serial_pending,
229 .getc = sh_serial_getc,
230 .setbrg = sh_serial_setbrg,
231};
232
Marek Vasut5c44ddc2018-02-16 01:33:27 +0100233#if CONFIG_IS_ENABLED(OF_CONTROL)
Yoshinori Sato359787c2016-04-18 16:51:04 +0900234static const struct udevice_id sh_serial_id[] ={
Yoshinori Sato747431b2016-04-18 16:51:05 +0900235 {.compatible = "renesas,sci", .data = PORT_SCI},
Yoshinori Sato359787c2016-04-18 16:51:04 +0900236 {.compatible = "renesas,scif", .data = PORT_SCIF},
Paul Barker966caed2023-10-19 15:30:44 +0100237 {.compatible = "renesas,scif-r9a07g044", .data = PORT_SCIFA},
Yoshinori Sato359787c2016-04-18 16:51:04 +0900238 {.compatible = "renesas,scifa", .data = PORT_SCIFA},
Hai Phambbe36e22023-02-28 22:29:19 +0100239 {.compatible = "renesas,hscif", .data = PORT_HSCIF},
Yoshinori Sato359787c2016-04-18 16:51:04 +0900240 {}
241};
242
Simon Glassd1998a92020-12-03 16:55:21 -0700243static int sh_serial_of_to_plat(struct udevice *dev)
Yoshinori Sato359787c2016-04-18 16:51:04 +0900244{
Simon Glass8a8d24b2020-12-03 16:55:23 -0700245 struct sh_serial_plat *plat = dev_get_plat(dev);
Marek Vasut81714992017-07-21 23:19:18 +0200246 struct clk sh_serial_clk;
Yoshinori Sato359787c2016-04-18 16:51:04 +0900247 fdt_addr_t addr;
Marek Vasut81714992017-07-21 23:19:18 +0200248 int ret;
Yoshinori Sato359787c2016-04-18 16:51:04 +0900249
Masahiro Yamada25484932020-07-17 14:36:48 +0900250 addr = dev_read_addr(dev);
Marek Vasutc4937562018-01-17 22:36:37 +0100251 if (!addr)
Yoshinori Sato359787c2016-04-18 16:51:04 +0900252 return -EINVAL;
253
254 plat->base = addr;
Marek Vasut81714992017-07-21 23:19:18 +0200255
256 ret = clk_get_by_name(dev, "fck", &sh_serial_clk);
Marek Vasut791c1742017-09-15 21:11:27 +0200257 if (!ret) {
258 ret = clk_enable(&sh_serial_clk);
259 if (!ret)
260 plat->clk = clk_get_rate(&sh_serial_clk);
261 } else {
Marek Vasut81714992017-07-21 23:19:18 +0200262 plat->clk = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev),
263 "clock", 1);
Marek Vasut791c1742017-09-15 21:11:27 +0200264 }
Marek Vasut81714992017-07-21 23:19:18 +0200265
Yoshinori Sato359787c2016-04-18 16:51:04 +0900266 plat->type = dev_get_driver_data(dev);
267 return 0;
268}
269#endif
270
Nobuhiro Iwamatsu59088e42015-02-12 13:48:04 +0900271U_BOOT_DRIVER(serial_sh) = {
272 .name = "serial_sh",
273 .id = UCLASS_SERIAL,
Yoshinori Sato359787c2016-04-18 16:51:04 +0900274 .of_match = of_match_ptr(sh_serial_id),
Simon Glassd1998a92020-12-03 16:55:21 -0700275 .of_to_plat = of_match_ptr(sh_serial_of_to_plat),
Simon Glass8a8d24b2020-12-03 16:55:23 -0700276 .plat_auto = sizeof(struct sh_serial_plat),
Nobuhiro Iwamatsu59088e42015-02-12 13:48:04 +0900277 .probe = sh_serial_probe,
278 .ops = &sh_serial_ops,
Bin Meng46879192018-10-24 06:36:36 -0700279#if !CONFIG_IS_ENABLED(OF_CONTROL)
Nobuhiro Iwamatsu59088e42015-02-12 13:48:04 +0900280 .flags = DM_FLAG_PRE_RELOC,
Bin Meng46879192018-10-24 06:36:36 -0700281#endif
Simon Glass41575d82020-12-03 16:55:17 -0700282 .priv_auto = sizeof(struct uart_port),
Nobuhiro Iwamatsu59088e42015-02-12 13:48:04 +0900283};
Marek Vasut836d1bf2023-02-28 22:17:22 +0100284#endif
Nobuhiro Iwamatsu59088e42015-02-12 13:48:04 +0900285
Marek Vasut836d1bf2023-02-28 22:17:22 +0100286#if !CONFIG_IS_ENABLED(DM_SERIAL) || IS_ENABLED(CONFIG_DEBUG_UART_SCIF)
John Rigby29565322010-12-20 18:27:51 -0700287
Marek Vasut836d1bf2023-02-28 22:17:22 +0100288#if defined(CFG_SCIF_A)
289 #define SCIF_BASE_PORT PORT_SCIFA
290#elif defined(CFG_SCI)
291 #define SCIF_BASE_PORT PORT_SCI
Hai Phambbe36e22023-02-28 22:29:19 +0100292#elif defined(CFG_HSCIF)
293 #define SCIF_BASE_PORT PORT_HSCIF
Marek Vasut836d1bf2023-02-28 22:17:22 +0100294#else
295 #define SCIF_BASE_PORT PORT_SCIF
296#endif
297
298static void sh_serial_init_nodm(struct uart_port *port)
299{
300 sh_serial_init_generic(port);
301 serial_setbrg();
302}
303
304static void sh_serial_putc_nondm(struct uart_port *port, const char c)
305{
306 if (c == '\n') {
307 while (1) {
308 if (serial_raw_putc(port, '\r') != -EAGAIN)
309 break;
310 }
311 }
312 while (1) {
313 if (serial_raw_putc(port, c) != -EAGAIN)
314 break;
315 }
316}
317#endif
318
319#if !CONFIG_IS_ENABLED(DM_SERIAL)
Nobuhiro Iwamatsuab09f432008-08-22 17:48:51 +0900320#if defined(CONFIG_CONS_SCIF0)
321# define SCIF_BASE SCIF0_BASE
322#elif defined(CONFIG_CONS_SCIF1)
323# define SCIF_BASE SCIF1_BASE
324#elif defined(CONFIG_CONS_SCIF2)
325# define SCIF_BASE SCIF2_BASE
326#elif defined(CONFIG_CONS_SCIF3)
327# define SCIF_BASE SCIF3_BASE
328#elif defined(CONFIG_CONS_SCIF4)
329# define SCIF_BASE SCIF4_BASE
330#elif defined(CONFIG_CONS_SCIF5)
331# define SCIF_BASE SCIF5_BASE
Phil Edworthy99744b72012-05-15 22:15:51 +0000332#elif defined(CONFIG_CONS_SCIF6)
333# define SCIF_BASE SCIF6_BASE
334#elif defined(CONFIG_CONS_SCIF7)
335# define SCIF_BASE SCIF7_BASE
Marek Vasut451e22f2018-04-12 15:23:46 +0200336#elif defined(CONFIG_CONS_SCIFA0)
337# define SCIF_BASE SCIFA0_BASE
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900338#else
Nobuhiro Iwamatsuab09f432008-08-22 17:48:51 +0900339# error "Default SCIF doesn't set....."
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900340#endif
341
Nobuhiro Iwamatsu3f6c8e32010-10-26 03:55:15 +0900342static struct uart_port sh_sci = {
Nobuhiro Iwamatsu59088e42015-02-12 13:48:04 +0900343 .membase = (unsigned char *)SCIF_BASE,
Nobuhiro Iwamatsu3f6c8e32010-10-26 03:55:15 +0900344 .mapbase = SCIF_BASE,
345 .type = SCIF_BASE_PORT,
Marek Vasut5e12d7d2023-02-28 22:17:21 +0100346#ifdef CFG_SCIF_USE_EXT_CLK
Nobuhiro Iwamatsu59088e42015-02-12 13:48:04 +0900347 .clk_mode = EXT_CLK,
348#endif
Nobuhiro Iwamatsu3f6c8e32010-10-26 03:55:15 +0900349};
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900350
Marek Vasut8bdd7ef2012-09-14 22:40:08 +0200351static void sh_serial_setbrg(void)
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900352{
Nobuhiro Iwamatsu3f6c8e32010-10-26 03:55:15 +0900353 DECLARE_GLOBAL_DATA_PTR;
Nobuhiro Iwamatsu59088e42015-02-12 13:48:04 +0900354 struct uart_port *port = &sh_sci;
355
356 sh_serial_setbrg_generic(port, CONFIG_SH_SCIF_CLK_FREQ, gd->baudrate);
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900357}
358
Marek Vasut8bdd7ef2012-09-14 22:40:08 +0200359static int sh_serial_init(void)
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900360{
Marek Vasut836d1bf2023-02-28 22:17:22 +0100361 sh_serial_init_nodm(&sh_sci);
Nobuhiro Iwamatsu59088e42015-02-12 13:48:04 +0900362
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900363 return 0;
364}
365
Marek Vasut8bdd7ef2012-09-14 22:40:08 +0200366static void sh_serial_putc(const char c)
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900367{
Marek Vasut836d1bf2023-02-28 22:17:22 +0100368 sh_serial_putc_nondm(&sh_sci, c);
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900369}
370
Marek Vasut8bdd7ef2012-09-14 22:40:08 +0200371static int sh_serial_tstc(void)
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900372{
Nobuhiro Iwamatsu59088e42015-02-12 13:48:04 +0900373 struct uart_port *port = &sh_sci;
Tetsuyuki Kobayashi7c791b32012-11-19 21:37:38 +0000374
Nobuhiro Iwamatsu59088e42015-02-12 13:48:04 +0900375 return sh_serial_tstc_generic(port);
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900376}
377
Marek Vasut8bdd7ef2012-09-14 22:40:08 +0200378static int sh_serial_getc(void)
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900379{
Nobuhiro Iwamatsu59088e42015-02-12 13:48:04 +0900380 struct uart_port *port = &sh_sci;
381 int ch;
Nobuhiro Iwamatsuab09f432008-08-22 17:48:51 +0900382
Nobuhiro Iwamatsu59088e42015-02-12 13:48:04 +0900383 while (1) {
384 ch = sh_serial_getc_generic(port);
385 if (ch != -EAGAIN)
386 break;
387 }
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900388
Nobuhiro Iwamatsu08c5fab2008-06-06 16:16:08 +0900389 return ch;
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900390}
Marek Vasut8bdd7ef2012-09-14 22:40:08 +0200391
Marek Vasut8bdd7ef2012-09-14 22:40:08 +0200392static struct serial_device sh_serial_drv = {
393 .name = "sh_serial",
394 .start = sh_serial_init,
395 .stop = NULL,
396 .setbrg = sh_serial_setbrg,
397 .putc = sh_serial_putc,
Marek Vasutec3fd682012-10-06 14:07:02 +0000398 .puts = default_serial_puts,
Marek Vasut8bdd7ef2012-09-14 22:40:08 +0200399 .getc = sh_serial_getc,
400 .tstc = sh_serial_tstc,
401};
402
403void sh_serial_initialize(void)
404{
405 serial_register(&sh_serial_drv);
406}
407
408__weak struct serial_device *default_serial_console(void)
409{
410 return &sh_serial_drv;
411}
Nobuhiro Iwamatsu59088e42015-02-12 13:48:04 +0900412#endif /* CONFIG_DM_SERIAL */
Marek Vasut836d1bf2023-02-28 22:17:22 +0100413
414#ifdef CONFIG_DEBUG_UART_SCIF
415#include <debug_uart.h>
416
417static struct uart_port debug_uart_sci = {
418 .membase = (unsigned char *)CONFIG_DEBUG_UART_BASE,
419 .mapbase = CONFIG_DEBUG_UART_BASE,
420 .type = SCIF_BASE_PORT,
421#ifdef CFG_SCIF_USE_EXT_CLK
422 .clk_mode = EXT_CLK,
423#endif
424};
425
426static inline void _debug_uart_init(void)
427{
428 sh_serial_init_nodm(&debug_uart_sci);
429}
430
431static inline void _debug_uart_putc(int c)
432{
433 sh_serial_putc_nondm(&debug_uart_sci, c);
434}
435
436DEBUG_UART_FUNCS
437
438#endif