blob: e6c23cedff11a3a46670f521bdcbc5bf7e0244fb [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
Nobuhiro Iwamatsu59088e42015-02-12 13:48:04 +090060}
61
62static void
63sh_serial_setbrg_generic(struct uart_port *port, int clk, int baudrate)
64{
65 if (port->clk_mode == EXT_CLK) {
66 unsigned short dl = DL_VALUE(baudrate, clk);
67 sci_out(port, DL, dl);
Nobuhiro Iwamatsu89f99a62014-12-10 14:42:05 +090068 /* Need wait: Clock * 1/dl * 1/16 */
Nobuhiro Iwamatsu59088e42015-02-12 13:48:04 +090069 udelay((1000000 * dl * 16 / clk) * 1000 + 1);
70 } else {
71 sci_out(port, SCBRR, SCBRR_VALUE(baudrate, clk));
72 }
73}
74
75static void handle_error(struct uart_port *port)
76{
77 sci_in(port, SCxSR);
78 sci_out(port, SCxSR, SCxSR_ERROR_CLEAR(port));
79 sci_in(port, SCLSR);
80 sci_out(port, SCLSR, 0x00);
81}
82
83static int serial_raw_putc(struct uart_port *port, const char c)
84{
85 /* Tx fifo is empty */
86 if (!(sci_in(port, SCxSR) & SCxSR_TEND(port)))
87 return -EAGAIN;
88
89 sci_out(port, SCxTDR, c);
90 sci_out(port, SCxSR, sci_in(port, SCxSR) & ~SCxSR_TEND(port));
91
92 return 0;
93}
94
95static int serial_rx_fifo_level(struct uart_port *port)
96{
97 return scif_rxfill(port);
98}
99
100static int sh_serial_tstc_generic(struct uart_port *port)
101{
102 if (sci_in(port, SCxSR) & SCIF_ERRORS) {
103 handle_error(port);
104 return 0;
105 }
106
107 return serial_rx_fifo_level(port) ? 1 : 0;
108}
109
110static int serial_getc_check(struct uart_port *port)
111{
112 unsigned short status;
113
114 status = sci_in(port, SCxSR);
115
116 if (status & SCIF_ERRORS)
117 handle_error(port);
118 if (sci_in(port, SCLSR) & SCxSR_ORER(port))
119 handle_error(port);
Marek Vasutf5ba5c92020-05-09 22:30:05 +0200120 status &= (SCIF_DR | SCxSR_RDxF(port));
121 if (status)
122 return status;
123 return scif_rxfill(port);
Nobuhiro Iwamatsu59088e42015-02-12 13:48:04 +0900124}
125
126static int sh_serial_getc_generic(struct uart_port *port)
127{
128 unsigned short status;
129 char ch;
130
131 if (!serial_getc_check(port))
132 return -EAGAIN;
133
134 ch = sci_in(port, SCxRDR);
135 status = sci_in(port, SCxSR);
136
137 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
138
139 if (status & SCIF_ERRORS)
140 handle_error(port);
141
142 if (sci_in(port, SCLSR) & SCxSR_ORER(port))
143 handle_error(port);
144
145 return ch;
146}
147
Marek Vasut5c44ddc2018-02-16 01:33:27 +0100148#if CONFIG_IS_ENABLED(DM_SERIAL)
Nobuhiro Iwamatsu59088e42015-02-12 13:48:04 +0900149
150static int sh_serial_pending(struct udevice *dev, bool input)
151{
152 struct uart_port *priv = dev_get_priv(dev);
153
154 return sh_serial_tstc_generic(priv);
155}
156
157static int sh_serial_putc(struct udevice *dev, const char ch)
158{
159 struct uart_port *priv = dev_get_priv(dev);
160
161 return serial_raw_putc(priv, ch);
162}
163
164static int sh_serial_getc(struct udevice *dev)
165{
166 struct uart_port *priv = dev_get_priv(dev);
167
168 return sh_serial_getc_generic(priv);
169}
170
171static int sh_serial_setbrg(struct udevice *dev, int baudrate)
172{
Simon Glass8a8d24b2020-12-03 16:55:23 -0700173 struct sh_serial_plat *plat = dev_get_plat(dev);
Nobuhiro Iwamatsu59088e42015-02-12 13:48:04 +0900174 struct uart_port *priv = dev_get_priv(dev);
175
176 sh_serial_setbrg_generic(priv, plat->clk, baudrate);
177
178 return 0;
179}
180
181static int sh_serial_probe(struct udevice *dev)
182{
Simon Glass8a8d24b2020-12-03 16:55:23 -0700183 struct sh_serial_plat *plat = dev_get_plat(dev);
Nobuhiro Iwamatsu59088e42015-02-12 13:48:04 +0900184 struct uart_port *priv = dev_get_priv(dev);
185
186 priv->membase = (unsigned char *)plat->base;
187 priv->mapbase = plat->base;
188 priv->type = plat->type;
189 priv->clk_mode = plat->clk_mode;
190
191 sh_serial_init_generic(priv);
192
193 return 0;
194}
195
196static const struct dm_serial_ops sh_serial_ops = {
197 .putc = sh_serial_putc,
198 .pending = sh_serial_pending,
199 .getc = sh_serial_getc,
200 .setbrg = sh_serial_setbrg,
201};
202
Marek Vasut5c44ddc2018-02-16 01:33:27 +0100203#if CONFIG_IS_ENABLED(OF_CONTROL)
Yoshinori Sato359787c2016-04-18 16:51:04 +0900204static const struct udevice_id sh_serial_id[] ={
Yoshinori Sato747431b2016-04-18 16:51:05 +0900205 {.compatible = "renesas,sci", .data = PORT_SCI},
Yoshinori Sato359787c2016-04-18 16:51:04 +0900206 {.compatible = "renesas,scif", .data = PORT_SCIF},
207 {.compatible = "renesas,scifa", .data = PORT_SCIFA},
208 {}
209};
210
Simon Glassd1998a92020-12-03 16:55:21 -0700211static int sh_serial_of_to_plat(struct udevice *dev)
Yoshinori Sato359787c2016-04-18 16:51:04 +0900212{
Simon Glass8a8d24b2020-12-03 16:55:23 -0700213 struct sh_serial_plat *plat = dev_get_plat(dev);
Marek Vasut81714992017-07-21 23:19:18 +0200214 struct clk sh_serial_clk;
Yoshinori Sato359787c2016-04-18 16:51:04 +0900215 fdt_addr_t addr;
Marek Vasut81714992017-07-21 23:19:18 +0200216 int ret;
Yoshinori Sato359787c2016-04-18 16:51:04 +0900217
Masahiro Yamada25484932020-07-17 14:36:48 +0900218 addr = dev_read_addr(dev);
Marek Vasutc4937562018-01-17 22:36:37 +0100219 if (!addr)
Yoshinori Sato359787c2016-04-18 16:51:04 +0900220 return -EINVAL;
221
222 plat->base = addr;
Marek Vasut81714992017-07-21 23:19:18 +0200223
224 ret = clk_get_by_name(dev, "fck", &sh_serial_clk);
Marek Vasut791c1742017-09-15 21:11:27 +0200225 if (!ret) {
226 ret = clk_enable(&sh_serial_clk);
227 if (!ret)
228 plat->clk = clk_get_rate(&sh_serial_clk);
229 } else {
Marek Vasut81714992017-07-21 23:19:18 +0200230 plat->clk = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev),
231 "clock", 1);
Marek Vasut791c1742017-09-15 21:11:27 +0200232 }
Marek Vasut81714992017-07-21 23:19:18 +0200233
Yoshinori Sato359787c2016-04-18 16:51:04 +0900234 plat->type = dev_get_driver_data(dev);
235 return 0;
236}
237#endif
238
Nobuhiro Iwamatsu59088e42015-02-12 13:48:04 +0900239U_BOOT_DRIVER(serial_sh) = {
240 .name = "serial_sh",
241 .id = UCLASS_SERIAL,
Yoshinori Sato359787c2016-04-18 16:51:04 +0900242 .of_match = of_match_ptr(sh_serial_id),
Simon Glassd1998a92020-12-03 16:55:21 -0700243 .of_to_plat = of_match_ptr(sh_serial_of_to_plat),
Simon Glass8a8d24b2020-12-03 16:55:23 -0700244 .plat_auto = sizeof(struct sh_serial_plat),
Nobuhiro Iwamatsu59088e42015-02-12 13:48:04 +0900245 .probe = sh_serial_probe,
246 .ops = &sh_serial_ops,
Bin Meng46879192018-10-24 06:36:36 -0700247#if !CONFIG_IS_ENABLED(OF_CONTROL)
Nobuhiro Iwamatsu59088e42015-02-12 13:48:04 +0900248 .flags = DM_FLAG_PRE_RELOC,
Bin Meng46879192018-10-24 06:36:36 -0700249#endif
Simon Glass41575d82020-12-03 16:55:17 -0700250 .priv_auto = sizeof(struct uart_port),
Nobuhiro Iwamatsu59088e42015-02-12 13:48:04 +0900251};
252
253#else /* CONFIG_DM_SERIAL */
John Rigby29565322010-12-20 18:27:51 -0700254
Nobuhiro Iwamatsuab09f432008-08-22 17:48:51 +0900255#if defined(CONFIG_CONS_SCIF0)
256# define SCIF_BASE SCIF0_BASE
257#elif defined(CONFIG_CONS_SCIF1)
258# define SCIF_BASE SCIF1_BASE
259#elif defined(CONFIG_CONS_SCIF2)
260# define SCIF_BASE SCIF2_BASE
261#elif defined(CONFIG_CONS_SCIF3)
262# define SCIF_BASE SCIF3_BASE
263#elif defined(CONFIG_CONS_SCIF4)
264# define SCIF_BASE SCIF4_BASE
265#elif defined(CONFIG_CONS_SCIF5)
266# define SCIF_BASE SCIF5_BASE
Phil Edworthy99744b72012-05-15 22:15:51 +0000267#elif defined(CONFIG_CONS_SCIF6)
268# define SCIF_BASE SCIF6_BASE
269#elif defined(CONFIG_CONS_SCIF7)
270# define SCIF_BASE SCIF7_BASE
Marek Vasut451e22f2018-04-12 15:23:46 +0200271#elif defined(CONFIG_CONS_SCIFA0)
272# define SCIF_BASE SCIFA0_BASE
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900273#else
Nobuhiro Iwamatsuab09f432008-08-22 17:48:51 +0900274# error "Default SCIF doesn't set....."
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900275#endif
276
Nobuhiro Iwamatsu3f6c8e32010-10-26 03:55:15 +0900277#if defined(CONFIG_SCIF_A)
278 #define SCIF_BASE_PORT PORT_SCIFA
Yoshinori Sato747431b2016-04-18 16:51:05 +0900279#elif defined(CONFIG_SCI)
280 #define SCIF_BASE_PORT PORT_SCI
Yoshihiro Shimoda7c10c572008-01-09 14:30:02 +0900281#else
Nobuhiro Iwamatsu3f6c8e32010-10-26 03:55:15 +0900282 #define SCIF_BASE_PORT PORT_SCIF
Yoshihiro Shimoda7c10c572008-01-09 14:30:02 +0900283#endif
284
Nobuhiro Iwamatsu3f6c8e32010-10-26 03:55:15 +0900285static struct uart_port sh_sci = {
Nobuhiro Iwamatsu59088e42015-02-12 13:48:04 +0900286 .membase = (unsigned char *)SCIF_BASE,
Nobuhiro Iwamatsu3f6c8e32010-10-26 03:55:15 +0900287 .mapbase = SCIF_BASE,
288 .type = SCIF_BASE_PORT,
Nobuhiro Iwamatsu59088e42015-02-12 13:48:04 +0900289#ifdef CONFIG_SCIF_USE_EXT_CLK
290 .clk_mode = EXT_CLK,
291#endif
Nobuhiro Iwamatsu3f6c8e32010-10-26 03:55:15 +0900292};
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900293
Marek Vasut8bdd7ef2012-09-14 22:40:08 +0200294static void sh_serial_setbrg(void)
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900295{
Nobuhiro Iwamatsu3f6c8e32010-10-26 03:55:15 +0900296 DECLARE_GLOBAL_DATA_PTR;
Nobuhiro Iwamatsu59088e42015-02-12 13:48:04 +0900297 struct uart_port *port = &sh_sci;
298
299 sh_serial_setbrg_generic(port, CONFIG_SH_SCIF_CLK_FREQ, gd->baudrate);
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900300}
301
Marek Vasut8bdd7ef2012-09-14 22:40:08 +0200302static int sh_serial_init(void)
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900303{
Nobuhiro Iwamatsu59088e42015-02-12 13:48:04 +0900304 struct uart_port *port = &sh_sci;
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900305
Nobuhiro Iwamatsu59088e42015-02-12 13:48:04 +0900306 sh_serial_init_generic(port);
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900307 serial_setbrg();
Nobuhiro Iwamatsu59088e42015-02-12 13:48:04 +0900308
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900309 return 0;
310}
311
Marek Vasut8bdd7ef2012-09-14 22:40:08 +0200312static void sh_serial_putc(const char c)
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900313{
Nobuhiro Iwamatsu59088e42015-02-12 13:48:04 +0900314 struct uart_port *port = &sh_sci;
315
316 if (c == '\n') {
317 while (1) {
318 if (serial_raw_putc(port, '\r') != -EAGAIN)
319 break;
320 }
321 }
322 while (1) {
323 if (serial_raw_putc(port, c) != -EAGAIN)
324 break;
325 }
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900326}
327
Marek Vasut8bdd7ef2012-09-14 22:40:08 +0200328static int sh_serial_tstc(void)
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900329{
Nobuhiro Iwamatsu59088e42015-02-12 13:48:04 +0900330 struct uart_port *port = &sh_sci;
Tetsuyuki Kobayashi7c791b32012-11-19 21:37:38 +0000331
Nobuhiro Iwamatsu59088e42015-02-12 13:48:04 +0900332 return sh_serial_tstc_generic(port);
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900333}
334
Marek Vasut8bdd7ef2012-09-14 22:40:08 +0200335static int sh_serial_getc(void)
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900336{
Nobuhiro Iwamatsu59088e42015-02-12 13:48:04 +0900337 struct uart_port *port = &sh_sci;
338 int ch;
Nobuhiro Iwamatsuab09f432008-08-22 17:48:51 +0900339
Nobuhiro Iwamatsu59088e42015-02-12 13:48:04 +0900340 while (1) {
341 ch = sh_serial_getc_generic(port);
342 if (ch != -EAGAIN)
343 break;
344 }
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900345
Nobuhiro Iwamatsu08c5fab2008-06-06 16:16:08 +0900346 return ch;
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900347}
Marek Vasut8bdd7ef2012-09-14 22:40:08 +0200348
Marek Vasut8bdd7ef2012-09-14 22:40:08 +0200349static struct serial_device sh_serial_drv = {
350 .name = "sh_serial",
351 .start = sh_serial_init,
352 .stop = NULL,
353 .setbrg = sh_serial_setbrg,
354 .putc = sh_serial_putc,
Marek Vasutec3fd682012-10-06 14:07:02 +0000355 .puts = default_serial_puts,
Marek Vasut8bdd7ef2012-09-14 22:40:08 +0200356 .getc = sh_serial_getc,
357 .tstc = sh_serial_tstc,
358};
359
360void sh_serial_initialize(void)
361{
362 serial_register(&sh_serial_drv);
363}
364
365__weak struct serial_device *default_serial_console(void)
366{
367 return &sh_serial_drv;
368}
Nobuhiro Iwamatsu59088e42015-02-12 13:48:04 +0900369#endif /* CONFIG_DM_SERIAL */