blob: 5f45d58e5800bc0bd79af480ecb754b5bb2c76db [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +09002/*
3 * SuperH SCIF device driver.
Nobuhiro Iwamatsu788b73f2013-07-23 13:58:20 +09004 * Copyright (C) 2013 Renesas Electronics Corporation
Nobuhiro Iwamatsu6d020352015-02-12 13:48:04 +09005 * Copyright (C) 2007,2008,2010, 2014 Nobuhiro Iwamatsu
Nobuhiro Iwamatsua5579ca2010-10-26 03:55:15 +09006 * Copyright (C) 2002 - 2008 Paul Mundt
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +09007 */
8
9#include <common.h>
Nobuhiro Iwamatsu6d020352015-02-12 13:48:04 +090010#include <errno.h>
Marek Vasut8fe2ffc2017-07-21 23:19:18 +020011#include <clk.h>
Nobuhiro Iwamatsu6d020352015-02-12 13:48:04 +090012#include <dm.h>
Jean-Christophe PLAGNIOL-VILLARDb27a8e32009-01-11 16:35:16 +010013#include <asm/io.h>
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +090014#include <asm/processor.h>
Marek Vasut904d3d72012-09-14 22:40:08 +020015#include <serial.h>
16#include <linux/compiler.h>
Nobuhiro Iwamatsu6d020352015-02-12 13:48:04 +090017#include <dm/platform_data/serial_sh.h>
Simon Glassdbd79542020-05-10 11:40:11 -060018#include <linux/delay.h>
Nobuhiro Iwamatsu6d020352015-02-12 13:48:04 +090019#include "serial_sh.h"
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +090020
Yoshinori Satoa6eed2b2016-04-18 16:51:04 +090021DECLARE_GLOBAL_DATA_PTR;
22
Marek Vasut39df77a2019-05-07 22:31:23 +020023#if defined(CONFIG_CPU_SH7780)
Nobuhiro Iwamatsua5579ca2010-10-26 03:55:15 +090024static int scif_rxfill(struct uart_port *port)
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +090025{
Nobuhiro Iwamatsua5579ca2010-10-26 03:55:15 +090026 return sci_in(port, SCRFDR) & 0xff;
27}
28#elif defined(CONFIG_CPU_SH7763)
29static int scif_rxfill(struct uart_port *port)
30{
31 if ((port->mapbase == 0xffe00000) ||
Nobuhiro Iwamatsu6d020352015-02-12 13:48:04 +090032 (port->mapbase == 0xffe08000)) {
Nobuhiro Iwamatsua5579ca2010-10-26 03:55:15 +090033 /* SCIF0/1*/
34 return sci_in(port, SCRFDR) & 0xff;
35 } else {
36 /* SCIF2 */
37 return sci_in(port, SCFDR) & SCIF2_RFDC_MASK;
38 }
39}
Nobuhiro Iwamatsu1b36beb2008-03-06 14:05:53 +090040#else
Nobuhiro Iwamatsua5579ca2010-10-26 03:55:15 +090041static int scif_rxfill(struct uart_port *port)
42{
43 return sci_in(port, SCFDR) & SCIF_RFDC_MASK;
44}
Nobuhiro Iwamatsu1b36beb2008-03-06 14:05:53 +090045#endif
Nobuhiro Iwamatsua5579ca2010-10-26 03:55:15 +090046
Nobuhiro Iwamatsu6d020352015-02-12 13:48:04 +090047static void sh_serial_init_generic(struct uart_port *port)
Nobuhiro Iwamatsua5579ca2010-10-26 03:55:15 +090048{
Nobuhiro Iwamatsu6d020352015-02-12 13:48:04 +090049 sci_out(port, SCSCR , SCSCR_INIT(port));
50 sci_out(port, SCSCR , SCSCR_INIT(port));
51 sci_out(port, SCSMR, 0);
52 sci_out(port, SCSMR, 0);
53 sci_out(port, SCFCR, SCFCR_RFRST|SCFCR_TFRST);
54 sci_in(port, SCFCR);
55 sci_out(port, SCFCR, 0);
Marek Vasut2d2e3ff2019-05-01 18:20:00 +020056#if defined(CONFIG_RZA1)
57 sci_out(port, SCSPTR, 0x0003);
58#endif
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +090059}
60
Nobuhiro Iwamatsu6d020352015-02-12 13:48:04 +090061static void
62sh_serial_setbrg_generic(struct uart_port *port, int clk, int baudrate)
Tetsuyuki Kobayashi5d2b5a22012-11-19 21:37:38 +000063{
Nobuhiro Iwamatsu6d020352015-02-12 13:48:04 +090064 if (port->clk_mode == EXT_CLK) {
65 unsigned short dl = DL_VALUE(baudrate, clk);
66 sci_out(port, DL, dl);
Nobuhiro Iwamatsu17861752014-12-10 14:42:05 +090067 /* Need wait: Clock * 1/dl * 1/16 */
Nobuhiro Iwamatsu6d020352015-02-12 13:48:04 +090068 udelay((1000000 * dl * 16 / clk) * 1000 + 1);
69 } else {
70 sci_out(port, SCBRR, SCBRR_VALUE(baudrate, clk));
71 }
Tetsuyuki Kobayashi5d2b5a22012-11-19 21:37:38 +000072}
73
Nobuhiro Iwamatsu6d020352015-02-12 13:48:04 +090074static void handle_error(struct uart_port *port)
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +090075{
Nobuhiro Iwamatsu6d020352015-02-12 13:48:04 +090076 sci_in(port, SCxSR);
77 sci_out(port, SCxSR, SCxSR_ERROR_CLEAR(port));
78 sci_in(port, SCLSR);
79 sci_out(port, SCLSR, 0x00);
80}
81
82static int serial_raw_putc(struct uart_port *port, const char c)
83{
84 /* Tx fifo is empty */
85 if (!(sci_in(port, SCxSR) & SCxSR_TEND(port)))
86 return -EAGAIN;
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +090087
Nobuhiro Iwamatsu6d020352015-02-12 13:48:04 +090088 sci_out(port, SCxTDR, c);
89 sci_out(port, SCxSR, sci_in(port, SCxSR) & ~SCxSR_TEND(port));
90
91 return 0;
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +090092}
93
Nobuhiro Iwamatsu6d020352015-02-12 13:48:04 +090094static int serial_rx_fifo_level(struct uart_port *port)
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +090095{
Nobuhiro Iwamatsu6d020352015-02-12 13:48:04 +090096 return scif_rxfill(port);
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +090097}
98
Nobuhiro Iwamatsu6d020352015-02-12 13:48:04 +090099static int sh_serial_tstc_generic(struct uart_port *port)
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900100{
Nobuhiro Iwamatsu6d020352015-02-12 13:48:04 +0900101 if (sci_in(port, SCxSR) & SCIF_ERRORS) {
102 handle_error(port);
Tetsuyuki Kobayashi5d2b5a22012-11-19 21:37:38 +0000103 return 0;
104 }
105
Nobuhiro Iwamatsu6d020352015-02-12 13:48:04 +0900106 return serial_rx_fifo_level(port) ? 1 : 0;
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900107}
108
Nobuhiro Iwamatsu6d020352015-02-12 13:48:04 +0900109static int serial_getc_check(struct uart_port *port)
Nobuhiro Iwamatsu6564b1a2008-06-06 16:16:08 +0900110{
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900111 unsigned short status;
112
Nobuhiro Iwamatsu6d020352015-02-12 13:48:04 +0900113 status = sci_in(port, SCxSR);
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900114
Nobuhiro Iwamatsua5579ca2010-10-26 03:55:15 +0900115 if (status & SCIF_ERRORS)
Nobuhiro Iwamatsu6d020352015-02-12 13:48:04 +0900116 handle_error(port);
117 if (sci_in(port, SCLSR) & SCxSR_ORER(port))
118 handle_error(port);
119 return status & (SCIF_DR | SCxSR_RDxF(port));
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900120}
121
Nobuhiro Iwamatsu6d020352015-02-12 13:48:04 +0900122static int sh_serial_getc_generic(struct uart_port *port)
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900123{
Nobuhiro Iwamatsu6564b1a2008-06-06 16:16:08 +0900124 unsigned short status;
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900125 char ch;
Nobuhiro Iwamatsufcabccc2008-08-22 17:48:51 +0900126
Nobuhiro Iwamatsu6d020352015-02-12 13:48:04 +0900127 if (!serial_getc_check(port))
128 return -EAGAIN;
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900129
Nobuhiro Iwamatsu6d020352015-02-12 13:48:04 +0900130 ch = sci_in(port, SCxRDR);
131 status = sci_in(port, SCxSR);
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900132
Nobuhiro Iwamatsu6d020352015-02-12 13:48:04 +0900133 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900134
Nobuhiro Iwamatsua5579ca2010-10-26 03:55:15 +0900135 if (status & SCIF_ERRORS)
Nobuhiro Iwamatsu6d020352015-02-12 13:48:04 +0900136 handle_error(port);
137
138 if (sci_in(port, SCLSR) & SCxSR_ORER(port))
139 handle_error(port);
140
141 return ch;
142}
143
Marek Vasut0dfa9912018-02-16 01:33:27 +0100144#if CONFIG_IS_ENABLED(DM_SERIAL)
Nobuhiro Iwamatsu6d020352015-02-12 13:48:04 +0900145
146static int sh_serial_pending(struct udevice *dev, bool input)
147{
148 struct uart_port *priv = dev_get_priv(dev);
149
150 return sh_serial_tstc_generic(priv);
151}
152
153static int sh_serial_putc(struct udevice *dev, const char ch)
154{
155 struct uart_port *priv = dev_get_priv(dev);
156
157 return serial_raw_putc(priv, ch);
158}
159
160static int sh_serial_getc(struct udevice *dev)
161{
162 struct uart_port *priv = dev_get_priv(dev);
163
164 return sh_serial_getc_generic(priv);
165}
166
167static int sh_serial_setbrg(struct udevice *dev, int baudrate)
168{
169 struct sh_serial_platdata *plat = dev_get_platdata(dev);
170 struct uart_port *priv = dev_get_priv(dev);
171
172 sh_serial_setbrg_generic(priv, plat->clk, baudrate);
173
174 return 0;
175}
176
177static int sh_serial_probe(struct udevice *dev)
178{
179 struct sh_serial_platdata *plat = dev_get_platdata(dev);
180 struct uart_port *priv = dev_get_priv(dev);
181
182 priv->membase = (unsigned char *)plat->base;
183 priv->mapbase = plat->base;
184 priv->type = plat->type;
185 priv->clk_mode = plat->clk_mode;
186
187 sh_serial_init_generic(priv);
188
189 return 0;
190}
191
192static const struct dm_serial_ops sh_serial_ops = {
193 .putc = sh_serial_putc,
194 .pending = sh_serial_pending,
195 .getc = sh_serial_getc,
196 .setbrg = sh_serial_setbrg,
197};
198
Marek Vasut0dfa9912018-02-16 01:33:27 +0100199#if CONFIG_IS_ENABLED(OF_CONTROL)
Yoshinori Satoa6eed2b2016-04-18 16:51:04 +0900200static const struct udevice_id sh_serial_id[] ={
Yoshinori Satoe5669a32016-04-18 16:51:05 +0900201 {.compatible = "renesas,sci", .data = PORT_SCI},
Yoshinori Satoa6eed2b2016-04-18 16:51:04 +0900202 {.compatible = "renesas,scif", .data = PORT_SCIF},
203 {.compatible = "renesas,scifa", .data = PORT_SCIFA},
204 {}
205};
206
207static int sh_serial_ofdata_to_platdata(struct udevice *dev)
208{
209 struct sh_serial_platdata *plat = dev_get_platdata(dev);
Marek Vasut8fe2ffc2017-07-21 23:19:18 +0200210 struct clk sh_serial_clk;
Yoshinori Satoa6eed2b2016-04-18 16:51:04 +0900211 fdt_addr_t addr;
Marek Vasut8fe2ffc2017-07-21 23:19:18 +0200212 int ret;
Yoshinori Satoa6eed2b2016-04-18 16:51:04 +0900213
Marek Vasut48db7762018-01-17 22:36:37 +0100214 addr = devfdt_get_addr(dev);
215 if (!addr)
Yoshinori Satoa6eed2b2016-04-18 16:51:04 +0900216 return -EINVAL;
217
218 plat->base = addr;
Marek Vasut8fe2ffc2017-07-21 23:19:18 +0200219
220 ret = clk_get_by_name(dev, "fck", &sh_serial_clk);
Marek Vasutfd6a4a72017-09-15 21:11:27 +0200221 if (!ret) {
222 ret = clk_enable(&sh_serial_clk);
223 if (!ret)
224 plat->clk = clk_get_rate(&sh_serial_clk);
225 } else {
Marek Vasut8fe2ffc2017-07-21 23:19:18 +0200226 plat->clk = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev),
227 "clock", 1);
Marek Vasutfd6a4a72017-09-15 21:11:27 +0200228 }
Marek Vasut8fe2ffc2017-07-21 23:19:18 +0200229
Yoshinori Satoa6eed2b2016-04-18 16:51:04 +0900230 plat->type = dev_get_driver_data(dev);
231 return 0;
232}
233#endif
234
Nobuhiro Iwamatsu6d020352015-02-12 13:48:04 +0900235U_BOOT_DRIVER(serial_sh) = {
236 .name = "serial_sh",
237 .id = UCLASS_SERIAL,
Yoshinori Satoa6eed2b2016-04-18 16:51:04 +0900238 .of_match = of_match_ptr(sh_serial_id),
239 .ofdata_to_platdata = of_match_ptr(sh_serial_ofdata_to_platdata),
240 .platdata_auto_alloc_size = sizeof(struct sh_serial_platdata),
Nobuhiro Iwamatsu6d020352015-02-12 13:48:04 +0900241 .probe = sh_serial_probe,
242 .ops = &sh_serial_ops,
Bin Mengbdb33d82018-10-24 06:36:36 -0700243#if !CONFIG_IS_ENABLED(OF_CONTROL)
Nobuhiro Iwamatsu6d020352015-02-12 13:48:04 +0900244 .flags = DM_FLAG_PRE_RELOC,
Bin Mengbdb33d82018-10-24 06:36:36 -0700245#endif
Nobuhiro Iwamatsu6d020352015-02-12 13:48:04 +0900246 .priv_auto_alloc_size = sizeof(struct uart_port),
247};
248
249#else /* CONFIG_DM_SERIAL */
250
251#if defined(CONFIG_CONS_SCIF0)
252# define SCIF_BASE SCIF0_BASE
253#elif defined(CONFIG_CONS_SCIF1)
254# define SCIF_BASE SCIF1_BASE
255#elif defined(CONFIG_CONS_SCIF2)
256# define SCIF_BASE SCIF2_BASE
257#elif defined(CONFIG_CONS_SCIF3)
258# define SCIF_BASE SCIF3_BASE
259#elif defined(CONFIG_CONS_SCIF4)
260# define SCIF_BASE SCIF4_BASE
261#elif defined(CONFIG_CONS_SCIF5)
262# define SCIF_BASE SCIF5_BASE
263#elif defined(CONFIG_CONS_SCIF6)
264# define SCIF_BASE SCIF6_BASE
265#elif defined(CONFIG_CONS_SCIF7)
266# define SCIF_BASE SCIF7_BASE
Marek Vasut1d9756b2018-04-12 15:23:46 +0200267#elif defined(CONFIG_CONS_SCIFA0)
268# define SCIF_BASE SCIFA0_BASE
Nobuhiro Iwamatsu6d020352015-02-12 13:48:04 +0900269#else
270# error "Default SCIF doesn't set....."
271#endif
272
273#if defined(CONFIG_SCIF_A)
274 #define SCIF_BASE_PORT PORT_SCIFA
Yoshinori Satoe5669a32016-04-18 16:51:05 +0900275#elif defined(CONFIG_SCI)
276 #define SCIF_BASE_PORT PORT_SCI
Nobuhiro Iwamatsu6d020352015-02-12 13:48:04 +0900277#else
278 #define SCIF_BASE_PORT PORT_SCIF
279#endif
280
281static struct uart_port sh_sci = {
282 .membase = (unsigned char *)SCIF_BASE,
283 .mapbase = SCIF_BASE,
284 .type = SCIF_BASE_PORT,
285#ifdef CONFIG_SCIF_USE_EXT_CLK
286 .clk_mode = EXT_CLK,
287#endif
288};
289
290static void sh_serial_setbrg(void)
291{
292 DECLARE_GLOBAL_DATA_PTR;
293 struct uart_port *port = &sh_sci;
294
295 sh_serial_setbrg_generic(port, CONFIG_SH_SCIF_CLK_FREQ, gd->baudrate);
296}
297
298static int sh_serial_init(void)
299{
300 struct uart_port *port = &sh_sci;
301
302 sh_serial_init_generic(port);
303 serial_setbrg();
304
305 return 0;
306}
307
308static void sh_serial_putc(const char c)
309{
310 struct uart_port *port = &sh_sci;
311
312 if (c == '\n') {
313 while (1) {
314 if (serial_raw_putc(port, '\r') != -EAGAIN)
315 break;
316 }
317 }
318 while (1) {
319 if (serial_raw_putc(port, c) != -EAGAIN)
320 break;
321 }
322}
323
324static int sh_serial_tstc(void)
325{
326 struct uart_port *port = &sh_sci;
327
328 return sh_serial_tstc_generic(port);
329}
330
331static int sh_serial_getc(void)
332{
333 struct uart_port *port = &sh_sci;
334 int ch;
335
336 while (1) {
337 ch = sh_serial_getc_generic(port);
338 if (ch != -EAGAIN)
339 break;
340 }
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900341
Nobuhiro Iwamatsu6564b1a2008-06-06 16:16:08 +0900342 return ch;
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900343}
Marek Vasut904d3d72012-09-14 22:40:08 +0200344
Marek Vasut904d3d72012-09-14 22:40:08 +0200345static struct serial_device sh_serial_drv = {
346 .name = "sh_serial",
347 .start = sh_serial_init,
348 .stop = NULL,
349 .setbrg = sh_serial_setbrg,
350 .putc = sh_serial_putc,
Marek Vasutd9c64492012-10-06 14:07:02 +0000351 .puts = default_serial_puts,
Marek Vasut904d3d72012-09-14 22:40:08 +0200352 .getc = sh_serial_getc,
353 .tstc = sh_serial_tstc,
354};
355
356void sh_serial_initialize(void)
357{
358 serial_register(&sh_serial_drv);
359}
360
361__weak struct serial_device *default_serial_console(void)
362{
363 return &sh_serial_drv;
364}
Nobuhiro Iwamatsu6d020352015-02-12 13:48:04 +0900365#endif /* CONFIG_DM_SERIAL */