blob: 5e543dbf3d5870a453fad9ee7fdb812aa8cb451c [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>
Simon Glass401d1c42020-10-30 21:38:53 -060013#include <asm/global_data.h>
Jean-Christophe PLAGNIOL-VILLARDfc83c922009-01-11 16:35:16 +010014#include <asm/io.h>
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +090015#include <asm/processor.h>
Marek Vasut8bdd7ef2012-09-14 22:40:08 +020016#include <serial.h>
17#include <linux/compiler.h>
Nobuhiro Iwamatsu59088e42015-02-12 13:48:04 +090018#include <dm/platform_data/serial_sh.h>
Simon Glassc05ed002020-05-10 11:40:11 -060019#include <linux/delay.h>
Nobuhiro Iwamatsu59088e42015-02-12 13:48:04 +090020#include "serial_sh.h"
21
Yoshinori Sato359787c2016-04-18 16:51:04 +090022DECLARE_GLOBAL_DATA_PTR;
23
Marek Vasut10e91cf2019-05-07 22:31:23 +020024#if defined(CONFIG_CPU_SH7780)
Nobuhiro Iwamatsu59088e42015-02-12 13:48:04 +090025static int scif_rxfill(struct uart_port *port)
26{
27 return sci_in(port, SCRFDR) & 0xff;
28}
29#elif defined(CONFIG_CPU_SH7763)
30static int scif_rxfill(struct uart_port *port)
31{
32 if ((port->mapbase == 0xffe00000) ||
33 (port->mapbase == 0xffe08000)) {
34 /* SCIF0/1*/
35 return sci_in(port, SCRFDR) & 0xff;
36 } else {
37 /* SCIF2 */
38 return sci_in(port, SCFDR) & SCIF2_RFDC_MASK;
39 }
40}
Nobuhiro Iwamatsu59088e42015-02-12 13:48:04 +090041#else
42static int scif_rxfill(struct uart_port *port)
43{
44 return sci_in(port, SCFDR) & SCIF_RFDC_MASK;
45}
46#endif
47
48static void sh_serial_init_generic(struct uart_port *port)
49{
50 sci_out(port, SCSCR , SCSCR_INIT(port));
51 sci_out(port, SCSCR , SCSCR_INIT(port));
52 sci_out(port, SCSMR, 0);
53 sci_out(port, SCSMR, 0);
54 sci_out(port, SCFCR, SCFCR_RFRST|SCFCR_TFRST);
55 sci_in(port, SCFCR);
56 sci_out(port, SCFCR, 0);
Marek Vasut67180fe2019-05-01 18:20:00 +020057#if defined(CONFIG_RZA1)
58 sci_out(port, SCSPTR, 0x0003);
59#endif
Hai Phambbe36e22023-02-28 22:29:19 +010060
Paul Barkercaf35032023-10-16 10:25:23 +010061#if IS_ENABLED(CONFIG_RCAR_GEN2) || IS_ENABLED(CONFIG_RCAR_GEN3) || IS_ENABLED(CONFIG_RCAR_GEN4)
Hai Phambbe36e22023-02-28 22:29:19 +010062 if (port->type == PORT_HSCIF)
63 sci_out(port, HSSRR, HSSRR_SRE | HSSRR_SRCYC8);
Paul Barkercaf35032023-10-16 10:25:23 +010064#endif
Nobuhiro Iwamatsu59088e42015-02-12 13:48:04 +090065}
66
67static void
68sh_serial_setbrg_generic(struct uart_port *port, int clk, int baudrate)
69{
70 if (port->clk_mode == EXT_CLK) {
71 unsigned short dl = DL_VALUE(baudrate, clk);
72 sci_out(port, DL, dl);
Nobuhiro Iwamatsu89f99a62014-12-10 14:42:05 +090073 /* Need wait: Clock * 1/dl * 1/16 */
Nobuhiro Iwamatsu59088e42015-02-12 13:48:04 +090074 udelay((1000000 * dl * 16 / clk) * 1000 + 1);
75 } else {
76 sci_out(port, SCBRR, SCBRR_VALUE(baudrate, clk));
77 }
78}
79
80static void handle_error(struct uart_port *port)
81{
82 sci_in(port, SCxSR);
83 sci_out(port, SCxSR, SCxSR_ERROR_CLEAR(port));
84 sci_in(port, SCLSR);
85 sci_out(port, SCLSR, 0x00);
86}
87
88static int serial_raw_putc(struct uart_port *port, const char c)
89{
90 /* Tx fifo is empty */
91 if (!(sci_in(port, SCxSR) & SCxSR_TEND(port)))
92 return -EAGAIN;
93
94 sci_out(port, SCxTDR, c);
95 sci_out(port, SCxSR, sci_in(port, SCxSR) & ~SCxSR_TEND(port));
96
97 return 0;
98}
99
100static int serial_rx_fifo_level(struct uart_port *port)
101{
102 return scif_rxfill(port);
103}
104
105static int sh_serial_tstc_generic(struct uart_port *port)
106{
107 if (sci_in(port, SCxSR) & SCIF_ERRORS) {
108 handle_error(port);
109 return 0;
110 }
111
112 return serial_rx_fifo_level(port) ? 1 : 0;
113}
114
115static int serial_getc_check(struct uart_port *port)
116{
117 unsigned short status;
118
119 status = sci_in(port, SCxSR);
120
121 if (status & SCIF_ERRORS)
122 handle_error(port);
123 if (sci_in(port, SCLSR) & SCxSR_ORER(port))
124 handle_error(port);
Marek Vasutf5ba5c92020-05-09 22:30:05 +0200125 status &= (SCIF_DR | SCxSR_RDxF(port));
126 if (status)
127 return status;
128 return scif_rxfill(port);
Nobuhiro Iwamatsu59088e42015-02-12 13:48:04 +0900129}
130
131static int sh_serial_getc_generic(struct uart_port *port)
132{
133 unsigned short status;
134 char ch;
135
136 if (!serial_getc_check(port))
137 return -EAGAIN;
138
139 ch = sci_in(port, SCxRDR);
140 status = sci_in(port, SCxSR);
141
142 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
143
144 if (status & SCIF_ERRORS)
145 handle_error(port);
146
147 if (sci_in(port, SCLSR) & SCxSR_ORER(port))
148 handle_error(port);
149
150 return ch;
151}
152
Marek Vasut5c44ddc2018-02-16 01:33:27 +0100153#if CONFIG_IS_ENABLED(DM_SERIAL)
Nobuhiro Iwamatsu59088e42015-02-12 13:48:04 +0900154
155static int sh_serial_pending(struct udevice *dev, bool input)
156{
157 struct uart_port *priv = dev_get_priv(dev);
158
159 return sh_serial_tstc_generic(priv);
160}
161
162static int sh_serial_putc(struct udevice *dev, const char ch)
163{
164 struct uart_port *priv = dev_get_priv(dev);
165
166 return serial_raw_putc(priv, ch);
167}
168
169static int sh_serial_getc(struct udevice *dev)
170{
171 struct uart_port *priv = dev_get_priv(dev);
172
173 return sh_serial_getc_generic(priv);
174}
175
176static int sh_serial_setbrg(struct udevice *dev, int baudrate)
177{
Simon Glass8a8d24b2020-12-03 16:55:23 -0700178 struct sh_serial_plat *plat = dev_get_plat(dev);
Nobuhiro Iwamatsu59088e42015-02-12 13:48:04 +0900179 struct uart_port *priv = dev_get_priv(dev);
180
181 sh_serial_setbrg_generic(priv, plat->clk, baudrate);
182
183 return 0;
184}
185
186static int sh_serial_probe(struct udevice *dev)
187{
Simon Glass8a8d24b2020-12-03 16:55:23 -0700188 struct sh_serial_plat *plat = dev_get_plat(dev);
Nobuhiro Iwamatsu59088e42015-02-12 13:48:04 +0900189 struct uart_port *priv = dev_get_priv(dev);
190
191 priv->membase = (unsigned char *)plat->base;
192 priv->mapbase = plat->base;
193 priv->type = plat->type;
194 priv->clk_mode = plat->clk_mode;
195
196 sh_serial_init_generic(priv);
197
198 return 0;
199}
200
201static const struct dm_serial_ops sh_serial_ops = {
202 .putc = sh_serial_putc,
203 .pending = sh_serial_pending,
204 .getc = sh_serial_getc,
205 .setbrg = sh_serial_setbrg,
206};
207
Marek Vasut5c44ddc2018-02-16 01:33:27 +0100208#if CONFIG_IS_ENABLED(OF_CONTROL)
Yoshinori Sato359787c2016-04-18 16:51:04 +0900209static const struct udevice_id sh_serial_id[] ={
Yoshinori Sato747431b2016-04-18 16:51:05 +0900210 {.compatible = "renesas,sci", .data = PORT_SCI},
Yoshinori Sato359787c2016-04-18 16:51:04 +0900211 {.compatible = "renesas,scif", .data = PORT_SCIF},
212 {.compatible = "renesas,scifa", .data = PORT_SCIFA},
Hai Phambbe36e22023-02-28 22:29:19 +0100213 {.compatible = "renesas,hscif", .data = PORT_HSCIF},
Yoshinori Sato359787c2016-04-18 16:51:04 +0900214 {}
215};
216
Simon Glassd1998a92020-12-03 16:55:21 -0700217static int sh_serial_of_to_plat(struct udevice *dev)
Yoshinori Sato359787c2016-04-18 16:51:04 +0900218{
Simon Glass8a8d24b2020-12-03 16:55:23 -0700219 struct sh_serial_plat *plat = dev_get_plat(dev);
Marek Vasut81714992017-07-21 23:19:18 +0200220 struct clk sh_serial_clk;
Yoshinori Sato359787c2016-04-18 16:51:04 +0900221 fdt_addr_t addr;
Marek Vasut81714992017-07-21 23:19:18 +0200222 int ret;
Yoshinori Sato359787c2016-04-18 16:51:04 +0900223
Masahiro Yamada25484932020-07-17 14:36:48 +0900224 addr = dev_read_addr(dev);
Marek Vasutc4937562018-01-17 22:36:37 +0100225 if (!addr)
Yoshinori Sato359787c2016-04-18 16:51:04 +0900226 return -EINVAL;
227
228 plat->base = addr;
Marek Vasut81714992017-07-21 23:19:18 +0200229
230 ret = clk_get_by_name(dev, "fck", &sh_serial_clk);
Marek Vasut791c1742017-09-15 21:11:27 +0200231 if (!ret) {
232 ret = clk_enable(&sh_serial_clk);
233 if (!ret)
234 plat->clk = clk_get_rate(&sh_serial_clk);
235 } else {
Marek Vasut81714992017-07-21 23:19:18 +0200236 plat->clk = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev),
237 "clock", 1);
Marek Vasut791c1742017-09-15 21:11:27 +0200238 }
Marek Vasut81714992017-07-21 23:19:18 +0200239
Yoshinori Sato359787c2016-04-18 16:51:04 +0900240 plat->type = dev_get_driver_data(dev);
241 return 0;
242}
243#endif
244
Nobuhiro Iwamatsu59088e42015-02-12 13:48:04 +0900245U_BOOT_DRIVER(serial_sh) = {
246 .name = "serial_sh",
247 .id = UCLASS_SERIAL,
Yoshinori Sato359787c2016-04-18 16:51:04 +0900248 .of_match = of_match_ptr(sh_serial_id),
Simon Glassd1998a92020-12-03 16:55:21 -0700249 .of_to_plat = of_match_ptr(sh_serial_of_to_plat),
Simon Glass8a8d24b2020-12-03 16:55:23 -0700250 .plat_auto = sizeof(struct sh_serial_plat),
Nobuhiro Iwamatsu59088e42015-02-12 13:48:04 +0900251 .probe = sh_serial_probe,
252 .ops = &sh_serial_ops,
Bin Meng46879192018-10-24 06:36:36 -0700253#if !CONFIG_IS_ENABLED(OF_CONTROL)
Nobuhiro Iwamatsu59088e42015-02-12 13:48:04 +0900254 .flags = DM_FLAG_PRE_RELOC,
Bin Meng46879192018-10-24 06:36:36 -0700255#endif
Simon Glass41575d82020-12-03 16:55:17 -0700256 .priv_auto = sizeof(struct uart_port),
Nobuhiro Iwamatsu59088e42015-02-12 13:48:04 +0900257};
Marek Vasut836d1bf2023-02-28 22:17:22 +0100258#endif
Nobuhiro Iwamatsu59088e42015-02-12 13:48:04 +0900259
Marek Vasut836d1bf2023-02-28 22:17:22 +0100260#if !CONFIG_IS_ENABLED(DM_SERIAL) || IS_ENABLED(CONFIG_DEBUG_UART_SCIF)
John Rigby29565322010-12-20 18:27:51 -0700261
Marek Vasut836d1bf2023-02-28 22:17:22 +0100262#if defined(CFG_SCIF_A)
263 #define SCIF_BASE_PORT PORT_SCIFA
264#elif defined(CFG_SCI)
265 #define SCIF_BASE_PORT PORT_SCI
Hai Phambbe36e22023-02-28 22:29:19 +0100266#elif defined(CFG_HSCIF)
267 #define SCIF_BASE_PORT PORT_HSCIF
Marek Vasut836d1bf2023-02-28 22:17:22 +0100268#else
269 #define SCIF_BASE_PORT PORT_SCIF
270#endif
271
272static void sh_serial_init_nodm(struct uart_port *port)
273{
274 sh_serial_init_generic(port);
275 serial_setbrg();
276}
277
278static void sh_serial_putc_nondm(struct uart_port *port, const char c)
279{
280 if (c == '\n') {
281 while (1) {
282 if (serial_raw_putc(port, '\r') != -EAGAIN)
283 break;
284 }
285 }
286 while (1) {
287 if (serial_raw_putc(port, c) != -EAGAIN)
288 break;
289 }
290}
291#endif
292
293#if !CONFIG_IS_ENABLED(DM_SERIAL)
Nobuhiro Iwamatsuab09f432008-08-22 17:48:51 +0900294#if defined(CONFIG_CONS_SCIF0)
295# define SCIF_BASE SCIF0_BASE
296#elif defined(CONFIG_CONS_SCIF1)
297# define SCIF_BASE SCIF1_BASE
298#elif defined(CONFIG_CONS_SCIF2)
299# define SCIF_BASE SCIF2_BASE
300#elif defined(CONFIG_CONS_SCIF3)
301# define SCIF_BASE SCIF3_BASE
302#elif defined(CONFIG_CONS_SCIF4)
303# define SCIF_BASE SCIF4_BASE
304#elif defined(CONFIG_CONS_SCIF5)
305# define SCIF_BASE SCIF5_BASE
Phil Edworthy99744b72012-05-15 22:15:51 +0000306#elif defined(CONFIG_CONS_SCIF6)
307# define SCIF_BASE SCIF6_BASE
308#elif defined(CONFIG_CONS_SCIF7)
309# define SCIF_BASE SCIF7_BASE
Marek Vasut451e22f2018-04-12 15:23:46 +0200310#elif defined(CONFIG_CONS_SCIFA0)
311# define SCIF_BASE SCIFA0_BASE
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900312#else
Nobuhiro Iwamatsuab09f432008-08-22 17:48:51 +0900313# error "Default SCIF doesn't set....."
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900314#endif
315
Nobuhiro Iwamatsu3f6c8e32010-10-26 03:55:15 +0900316static struct uart_port sh_sci = {
Nobuhiro Iwamatsu59088e42015-02-12 13:48:04 +0900317 .membase = (unsigned char *)SCIF_BASE,
Nobuhiro Iwamatsu3f6c8e32010-10-26 03:55:15 +0900318 .mapbase = SCIF_BASE,
319 .type = SCIF_BASE_PORT,
Marek Vasut5e12d7d2023-02-28 22:17:21 +0100320#ifdef CFG_SCIF_USE_EXT_CLK
Nobuhiro Iwamatsu59088e42015-02-12 13:48:04 +0900321 .clk_mode = EXT_CLK,
322#endif
Nobuhiro Iwamatsu3f6c8e32010-10-26 03:55:15 +0900323};
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900324
Marek Vasut8bdd7ef2012-09-14 22:40:08 +0200325static void sh_serial_setbrg(void)
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900326{
Nobuhiro Iwamatsu3f6c8e32010-10-26 03:55:15 +0900327 DECLARE_GLOBAL_DATA_PTR;
Nobuhiro Iwamatsu59088e42015-02-12 13:48:04 +0900328 struct uart_port *port = &sh_sci;
329
330 sh_serial_setbrg_generic(port, CONFIG_SH_SCIF_CLK_FREQ, gd->baudrate);
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900331}
332
Marek Vasut8bdd7ef2012-09-14 22:40:08 +0200333static int sh_serial_init(void)
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900334{
Marek Vasut836d1bf2023-02-28 22:17:22 +0100335 sh_serial_init_nodm(&sh_sci);
Nobuhiro Iwamatsu59088e42015-02-12 13:48:04 +0900336
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900337 return 0;
338}
339
Marek Vasut8bdd7ef2012-09-14 22:40:08 +0200340static void sh_serial_putc(const char c)
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900341{
Marek Vasut836d1bf2023-02-28 22:17:22 +0100342 sh_serial_putc_nondm(&sh_sci, c);
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900343}
344
Marek Vasut8bdd7ef2012-09-14 22:40:08 +0200345static int sh_serial_tstc(void)
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900346{
Nobuhiro Iwamatsu59088e42015-02-12 13:48:04 +0900347 struct uart_port *port = &sh_sci;
Tetsuyuki Kobayashi7c791b32012-11-19 21:37:38 +0000348
Nobuhiro Iwamatsu59088e42015-02-12 13:48:04 +0900349 return sh_serial_tstc_generic(port);
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900350}
351
Marek Vasut8bdd7ef2012-09-14 22:40:08 +0200352static int sh_serial_getc(void)
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900353{
Nobuhiro Iwamatsu59088e42015-02-12 13:48:04 +0900354 struct uart_port *port = &sh_sci;
355 int ch;
Nobuhiro Iwamatsuab09f432008-08-22 17:48:51 +0900356
Nobuhiro Iwamatsu59088e42015-02-12 13:48:04 +0900357 while (1) {
358 ch = sh_serial_getc_generic(port);
359 if (ch != -EAGAIN)
360 break;
361 }
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900362
Nobuhiro Iwamatsu08c5fab2008-06-06 16:16:08 +0900363 return ch;
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900364}
Marek Vasut8bdd7ef2012-09-14 22:40:08 +0200365
Marek Vasut8bdd7ef2012-09-14 22:40:08 +0200366static struct serial_device sh_serial_drv = {
367 .name = "sh_serial",
368 .start = sh_serial_init,
369 .stop = NULL,
370 .setbrg = sh_serial_setbrg,
371 .putc = sh_serial_putc,
Marek Vasutec3fd682012-10-06 14:07:02 +0000372 .puts = default_serial_puts,
Marek Vasut8bdd7ef2012-09-14 22:40:08 +0200373 .getc = sh_serial_getc,
374 .tstc = sh_serial_tstc,
375};
376
377void sh_serial_initialize(void)
378{
379 serial_register(&sh_serial_drv);
380}
381
382__weak struct serial_device *default_serial_console(void)
383{
384 return &sh_serial_drv;
385}
Nobuhiro Iwamatsu59088e42015-02-12 13:48:04 +0900386#endif /* CONFIG_DM_SERIAL */
Marek Vasut836d1bf2023-02-28 22:17:22 +0100387
388#ifdef CONFIG_DEBUG_UART_SCIF
389#include <debug_uart.h>
390
391static struct uart_port debug_uart_sci = {
392 .membase = (unsigned char *)CONFIG_DEBUG_UART_BASE,
393 .mapbase = CONFIG_DEBUG_UART_BASE,
394 .type = SCIF_BASE_PORT,
395#ifdef CFG_SCIF_USE_EXT_CLK
396 .clk_mode = EXT_CLK,
397#endif
398};
399
400static inline void _debug_uart_init(void)
401{
402 sh_serial_init_nodm(&debug_uart_sci);
403}
404
405static inline void _debug_uart_putc(int c)
406{
407 sh_serial_putc_nondm(&debug_uart_sci, c);
408}
409
410DEBUG_UART_FUNCS
411
412#endif