blob: 51f7fbcfb795ee502e8ca55e5e0c61cd7a9d3d58 [file] [log] [blame]
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +09001/*
2 * SuperH SCIF device driver.
Nobuhiro Iwamatsu788b73f2013-07-23 13:58:20 +09003 * Copyright (C) 2013 Renesas Electronics Corporation
Nobuhiro Iwamatsu6d020352015-02-12 13:48:04 +09004 * Copyright (C) 2007,2008,2010, 2014 Nobuhiro Iwamatsu
Nobuhiro Iwamatsua5579ca2010-10-26 03:55:15 +09005 * Copyright (C) 2002 - 2008 Paul Mundt
Wolfgang Denk0a5c2142007-12-27 01:52:50 +01006 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02007 * SPDX-License-Identifier: GPL-2.0+
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +09008 */
9
10#include <common.h>
Nobuhiro Iwamatsu6d020352015-02-12 13:48:04 +090011#include <errno.h>
12#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>
18#include "serial_sh.h"
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +090019
Yoshinori Satoa6eed2b2016-04-18 16:51:04 +090020DECLARE_GLOBAL_DATA_PTR;
21
Nobuhiro Iwamatsua5579ca2010-10-26 03:55:15 +090022#if defined(CONFIG_CPU_SH7760) || \
23 defined(CONFIG_CPU_SH7780) || \
24 defined(CONFIG_CPU_SH7785) || \
25 defined(CONFIG_CPU_SH7786)
26static int scif_rxfill(struct uart_port *port)
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +090027{
Nobuhiro Iwamatsua5579ca2010-10-26 03:55:15 +090028 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) ||
Nobuhiro Iwamatsu6d020352015-02-12 13:48:04 +090034 (port->mapbase == 0xffe08000)) {
Nobuhiro Iwamatsua5579ca2010-10-26 03:55:15 +090035 /* SCIF0/1*/
36 return sci_in(port, SCRFDR) & 0xff;
37 } else {
38 /* SCIF2 */
39 return sci_in(port, SCFDR) & SCIF2_RFDC_MASK;
40 }
41}
42#elif defined(CONFIG_ARCH_SH7372)
43static int scif_rxfill(struct uart_port *port)
44{
45 if (port->type == PORT_SCIFA)
46 return sci_in(port, SCFDR) & SCIF_RFDC_MASK;
47 else
48 return sci_in(port, SCRFDR);
49}
Nobuhiro Iwamatsu1b36beb2008-03-06 14:05:53 +090050#else
Nobuhiro Iwamatsua5579ca2010-10-26 03:55:15 +090051static int scif_rxfill(struct uart_port *port)
52{
53 return sci_in(port, SCFDR) & SCIF_RFDC_MASK;
54}
Nobuhiro Iwamatsu1b36beb2008-03-06 14:05:53 +090055#endif
Nobuhiro Iwamatsua5579ca2010-10-26 03:55:15 +090056
Nobuhiro Iwamatsu6d020352015-02-12 13:48:04 +090057static void sh_serial_init_generic(struct uart_port *port)
Nobuhiro Iwamatsua5579ca2010-10-26 03:55:15 +090058{
Nobuhiro Iwamatsu6d020352015-02-12 13:48:04 +090059 sci_out(port, SCSCR , SCSCR_INIT(port));
60 sci_out(port, SCSCR , SCSCR_INIT(port));
61 sci_out(port, SCSMR, 0);
62 sci_out(port, SCSMR, 0);
63 sci_out(port, SCFCR, SCFCR_RFRST|SCFCR_TFRST);
64 sci_in(port, SCFCR);
65 sci_out(port, SCFCR, 0);
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +090066}
67
Nobuhiro Iwamatsu6d020352015-02-12 13:48:04 +090068static void
69sh_serial_setbrg_generic(struct uart_port *port, int clk, int baudrate)
Tetsuyuki Kobayashi5d2b5a22012-11-19 21:37:38 +000070{
Nobuhiro Iwamatsu6d020352015-02-12 13:48:04 +090071 if (port->clk_mode == EXT_CLK) {
72 unsigned short dl = DL_VALUE(baudrate, clk);
73 sci_out(port, DL, dl);
Nobuhiro Iwamatsu17861752014-12-10 14:42:05 +090074 /* Need wait: Clock * 1/dl * 1/16 */
Nobuhiro Iwamatsu6d020352015-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 }
Tetsuyuki Kobayashi5d2b5a22012-11-19 21:37:38 +000079}
80
Nobuhiro Iwamatsu6d020352015-02-12 13:48:04 +090081static void handle_error(struct uart_port *port)
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +090082{
Nobuhiro Iwamatsu6d020352015-02-12 13:48:04 +090083 sci_in(port, SCxSR);
84 sci_out(port, SCxSR, SCxSR_ERROR_CLEAR(port));
85 sci_in(port, SCLSR);
86 sci_out(port, SCLSR, 0x00);
87}
88
89static int serial_raw_putc(struct uart_port *port, const char c)
90{
91 /* Tx fifo is empty */
92 if (!(sci_in(port, SCxSR) & SCxSR_TEND(port)))
93 return -EAGAIN;
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +090094
Nobuhiro Iwamatsu6d020352015-02-12 13:48:04 +090095 sci_out(port, SCxTDR, c);
96 sci_out(port, SCxSR, sci_in(port, SCxSR) & ~SCxSR_TEND(port));
97
98 return 0;
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +090099}
100
Nobuhiro Iwamatsu6d020352015-02-12 13:48:04 +0900101static int serial_rx_fifo_level(struct uart_port *port)
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900102{
Nobuhiro Iwamatsu6d020352015-02-12 13:48:04 +0900103 return scif_rxfill(port);
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900104}
105
Nobuhiro Iwamatsu6d020352015-02-12 13:48:04 +0900106static int sh_serial_tstc_generic(struct uart_port *port)
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900107{
Nobuhiro Iwamatsu6d020352015-02-12 13:48:04 +0900108 if (sci_in(port, SCxSR) & SCIF_ERRORS) {
109 handle_error(port);
Tetsuyuki Kobayashi5d2b5a22012-11-19 21:37:38 +0000110 return 0;
111 }
112
Nobuhiro Iwamatsu6d020352015-02-12 13:48:04 +0900113 return serial_rx_fifo_level(port) ? 1 : 0;
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900114}
115
Nobuhiro Iwamatsu6d020352015-02-12 13:48:04 +0900116static int serial_getc_check(struct uart_port *port)
Nobuhiro Iwamatsu6564b1a2008-06-06 16:16:08 +0900117{
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900118 unsigned short status;
119
Nobuhiro Iwamatsu6d020352015-02-12 13:48:04 +0900120 status = sci_in(port, SCxSR);
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900121
Nobuhiro Iwamatsua5579ca2010-10-26 03:55:15 +0900122 if (status & SCIF_ERRORS)
Nobuhiro Iwamatsu6d020352015-02-12 13:48:04 +0900123 handle_error(port);
124 if (sci_in(port, SCLSR) & SCxSR_ORER(port))
125 handle_error(port);
126 return status & (SCIF_DR | SCxSR_RDxF(port));
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900127}
128
Nobuhiro Iwamatsu6d020352015-02-12 13:48:04 +0900129static int sh_serial_getc_generic(struct uart_port *port)
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900130{
Nobuhiro Iwamatsu6564b1a2008-06-06 16:16:08 +0900131 unsigned short status;
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900132 char ch;
Nobuhiro Iwamatsufcabccc2008-08-22 17:48:51 +0900133
Nobuhiro Iwamatsu6d020352015-02-12 13:48:04 +0900134 if (!serial_getc_check(port))
135 return -EAGAIN;
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900136
Nobuhiro Iwamatsu6d020352015-02-12 13:48:04 +0900137 ch = sci_in(port, SCxRDR);
138 status = sci_in(port, SCxSR);
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900139
Nobuhiro Iwamatsu6d020352015-02-12 13:48:04 +0900140 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900141
Nobuhiro Iwamatsua5579ca2010-10-26 03:55:15 +0900142 if (status & SCIF_ERRORS)
Nobuhiro Iwamatsu6d020352015-02-12 13:48:04 +0900143 handle_error(port);
144
145 if (sci_in(port, SCLSR) & SCxSR_ORER(port))
146 handle_error(port);
147
148 return ch;
149}
150
151#ifdef CONFIG_DM_SERIAL
152
153static int sh_serial_pending(struct udevice *dev, bool input)
154{
155 struct uart_port *priv = dev_get_priv(dev);
156
157 return sh_serial_tstc_generic(priv);
158}
159
160static int sh_serial_putc(struct udevice *dev, const char ch)
161{
162 struct uart_port *priv = dev_get_priv(dev);
163
164 return serial_raw_putc(priv, ch);
165}
166
167static int sh_serial_getc(struct udevice *dev)
168{
169 struct uart_port *priv = dev_get_priv(dev);
170
171 return sh_serial_getc_generic(priv);
172}
173
174static int sh_serial_setbrg(struct udevice *dev, int baudrate)
175{
176 struct sh_serial_platdata *plat = dev_get_platdata(dev);
177 struct uart_port *priv = dev_get_priv(dev);
178
179 sh_serial_setbrg_generic(priv, plat->clk, baudrate);
180
181 return 0;
182}
183
184static int sh_serial_probe(struct udevice *dev)
185{
186 struct sh_serial_platdata *plat = dev_get_platdata(dev);
187 struct uart_port *priv = dev_get_priv(dev);
188
189 priv->membase = (unsigned char *)plat->base;
190 priv->mapbase = plat->base;
191 priv->type = plat->type;
192 priv->clk_mode = plat->clk_mode;
193
194 sh_serial_init_generic(priv);
195
196 return 0;
197}
198
199static const struct dm_serial_ops sh_serial_ops = {
200 .putc = sh_serial_putc,
201 .pending = sh_serial_pending,
202 .getc = sh_serial_getc,
203 .setbrg = sh_serial_setbrg,
204};
205
Yoshinori Satoa6eed2b2016-04-18 16:51:04 +0900206#ifdef CONFIG_OF_CONTROL
207static const struct udevice_id sh_serial_id[] ={
Yoshinori Satoe5669a32016-04-18 16:51:05 +0900208 {.compatible = "renesas,sci", .data = PORT_SCI},
Yoshinori Satoa6eed2b2016-04-18 16:51:04 +0900209 {.compatible = "renesas,scif", .data = PORT_SCIF},
210 {.compatible = "renesas,scifa", .data = PORT_SCIFA},
211 {}
212};
213
214static int sh_serial_ofdata_to_platdata(struct udevice *dev)
215{
216 struct sh_serial_platdata *plat = dev_get_platdata(dev);
217 fdt_addr_t addr;
218
Simon Glassdd79d6e2017-01-17 16:52:55 -0700219 addr = fdtdec_get_addr(gd->fdt_blob, dev_of_offset(dev), "reg");
Yoshinori Satoa6eed2b2016-04-18 16:51:04 +0900220 if (addr == FDT_ADDR_T_NONE)
221 return -EINVAL;
222
223 plat->base = addr;
Simon Glassdd79d6e2017-01-17 16:52:55 -0700224 plat->clk = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev), "clock",
225 1);
Yoshinori Satoa6eed2b2016-04-18 16:51:04 +0900226 plat->type = dev_get_driver_data(dev);
227 return 0;
228}
229#endif
230
Nobuhiro Iwamatsu6d020352015-02-12 13:48:04 +0900231U_BOOT_DRIVER(serial_sh) = {
232 .name = "serial_sh",
233 .id = UCLASS_SERIAL,
Yoshinori Satoa6eed2b2016-04-18 16:51:04 +0900234 .of_match = of_match_ptr(sh_serial_id),
235 .ofdata_to_platdata = of_match_ptr(sh_serial_ofdata_to_platdata),
236 .platdata_auto_alloc_size = sizeof(struct sh_serial_platdata),
Nobuhiro Iwamatsu6d020352015-02-12 13:48:04 +0900237 .probe = sh_serial_probe,
238 .ops = &sh_serial_ops,
239 .flags = DM_FLAG_PRE_RELOC,
240 .priv_auto_alloc_size = sizeof(struct uart_port),
241};
242
243#else /* CONFIG_DM_SERIAL */
244
245#if defined(CONFIG_CONS_SCIF0)
246# define SCIF_BASE SCIF0_BASE
247#elif defined(CONFIG_CONS_SCIF1)
248# define SCIF_BASE SCIF1_BASE
249#elif defined(CONFIG_CONS_SCIF2)
250# define SCIF_BASE SCIF2_BASE
251#elif defined(CONFIG_CONS_SCIF3)
252# define SCIF_BASE SCIF3_BASE
253#elif defined(CONFIG_CONS_SCIF4)
254# define SCIF_BASE SCIF4_BASE
255#elif defined(CONFIG_CONS_SCIF5)
256# define SCIF_BASE SCIF5_BASE
257#elif defined(CONFIG_CONS_SCIF6)
258# define SCIF_BASE SCIF6_BASE
259#elif defined(CONFIG_CONS_SCIF7)
260# define SCIF_BASE SCIF7_BASE
261#else
262# error "Default SCIF doesn't set....."
263#endif
264
265#if defined(CONFIG_SCIF_A)
266 #define SCIF_BASE_PORT PORT_SCIFA
Yoshinori Satoe5669a32016-04-18 16:51:05 +0900267#elif defined(CONFIG_SCI)
268 #define SCIF_BASE_PORT PORT_SCI
Nobuhiro Iwamatsu6d020352015-02-12 13:48:04 +0900269#else
270 #define SCIF_BASE_PORT PORT_SCIF
271#endif
272
273static struct uart_port sh_sci = {
274 .membase = (unsigned char *)SCIF_BASE,
275 .mapbase = SCIF_BASE,
276 .type = SCIF_BASE_PORT,
277#ifdef CONFIG_SCIF_USE_EXT_CLK
278 .clk_mode = EXT_CLK,
279#endif
280};
281
282static void sh_serial_setbrg(void)
283{
284 DECLARE_GLOBAL_DATA_PTR;
285 struct uart_port *port = &sh_sci;
286
287 sh_serial_setbrg_generic(port, CONFIG_SH_SCIF_CLK_FREQ, gd->baudrate);
288}
289
290static int sh_serial_init(void)
291{
292 struct uart_port *port = &sh_sci;
293
294 sh_serial_init_generic(port);
295 serial_setbrg();
296
297 return 0;
298}
299
300static void sh_serial_putc(const char c)
301{
302 struct uart_port *port = &sh_sci;
303
304 if (c == '\n') {
305 while (1) {
306 if (serial_raw_putc(port, '\r') != -EAGAIN)
307 break;
308 }
309 }
310 while (1) {
311 if (serial_raw_putc(port, c) != -EAGAIN)
312 break;
313 }
314}
315
316static int sh_serial_tstc(void)
317{
318 struct uart_port *port = &sh_sci;
319
320 return sh_serial_tstc_generic(port);
321}
322
323static int sh_serial_getc(void)
324{
325 struct uart_port *port = &sh_sci;
326 int ch;
327
328 while (1) {
329 ch = sh_serial_getc_generic(port);
330 if (ch != -EAGAIN)
331 break;
332 }
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900333
Nobuhiro Iwamatsu6564b1a2008-06-06 16:16:08 +0900334 return ch;
Nobuhiro Iwamatsu970dc332007-05-13 20:58:00 +0900335}
Marek Vasut904d3d72012-09-14 22:40:08 +0200336
Marek Vasut904d3d72012-09-14 22:40:08 +0200337static struct serial_device sh_serial_drv = {
338 .name = "sh_serial",
339 .start = sh_serial_init,
340 .stop = NULL,
341 .setbrg = sh_serial_setbrg,
342 .putc = sh_serial_putc,
Marek Vasutd9c64492012-10-06 14:07:02 +0000343 .puts = default_serial_puts,
Marek Vasut904d3d72012-09-14 22:40:08 +0200344 .getc = sh_serial_getc,
345 .tstc = sh_serial_tstc,
346};
347
348void sh_serial_initialize(void)
349{
350 serial_register(&sh_serial_drv);
351}
352
353__weak struct serial_device *default_serial_console(void)
354{
355 return &sh_serial_drv;
356}
Nobuhiro Iwamatsu6d020352015-02-12 13:48:04 +0900357#endif /* CONFIG_DM_SERIAL */