2 * SuperH SCIF device driver.
3 * Copyright (C) 2013 Renesas Electronics Corporation
4 * Copyright (C) 2007,2008,2010, 2014 Nobuhiro Iwamatsu
5 * Copyright (C) 2002 - 2008 Paul Mundt
7 * SPDX-License-Identifier: GPL-2.0+
15 #include <asm/processor.h>
17 #include <linux/compiler.h>
18 #include <dm/platform_data/serial_sh.h>
19 #include "serial_sh.h"
21 DECLARE_GLOBAL_DATA_PTR;
23 #if defined(CONFIG_CPU_SH7760) || \
24 defined(CONFIG_CPU_SH7780) || \
25 defined(CONFIG_CPU_SH7785) || \
26 defined(CONFIG_CPU_SH7786)
27 static int scif_rxfill(struct uart_port *port)
29 return sci_in(port, SCRFDR) & 0xff;
31 #elif defined(CONFIG_CPU_SH7763)
32 static int scif_rxfill(struct uart_port *port)
34 if ((port->mapbase == 0xffe00000) ||
35 (port->mapbase == 0xffe08000)) {
37 return sci_in(port, SCRFDR) & 0xff;
40 return sci_in(port, SCFDR) & SCIF2_RFDC_MASK;
43 #elif defined(CONFIG_ARCH_SH7372)
44 static int scif_rxfill(struct uart_port *port)
46 if (port->type == PORT_SCIFA)
47 return sci_in(port, SCFDR) & SCIF_RFDC_MASK;
49 return sci_in(port, SCRFDR);
52 static int scif_rxfill(struct uart_port *port)
54 return sci_in(port, SCFDR) & SCIF_RFDC_MASK;
58 static void sh_serial_init_generic(struct uart_port *port)
60 sci_out(port, SCSCR , SCSCR_INIT(port));
61 sci_out(port, SCSCR , SCSCR_INIT(port));
62 sci_out(port, SCSMR, 0);
63 sci_out(port, SCSMR, 0);
64 sci_out(port, SCFCR, SCFCR_RFRST|SCFCR_TFRST);
66 sci_out(port, SCFCR, 0);
70 sh_serial_setbrg_generic(struct uart_port *port, int clk, int baudrate)
72 if (port->clk_mode == EXT_CLK) {
73 unsigned short dl = DL_VALUE(baudrate, clk);
74 sci_out(port, DL, dl);
75 /* Need wait: Clock * 1/dl * 1/16 */
76 udelay((1000000 * dl * 16 / clk) * 1000 + 1);
78 sci_out(port, SCBRR, SCBRR_VALUE(baudrate, clk));
82 static void handle_error(struct uart_port *port)
85 sci_out(port, SCxSR, SCxSR_ERROR_CLEAR(port));
87 sci_out(port, SCLSR, 0x00);
90 static int serial_raw_putc(struct uart_port *port, const char c)
92 /* Tx fifo is empty */
93 if (!(sci_in(port, SCxSR) & SCxSR_TEND(port)))
96 sci_out(port, SCxTDR, c);
97 sci_out(port, SCxSR, sci_in(port, SCxSR) & ~SCxSR_TEND(port));
102 static int serial_rx_fifo_level(struct uart_port *port)
104 return scif_rxfill(port);
107 static int sh_serial_tstc_generic(struct uart_port *port)
109 if (sci_in(port, SCxSR) & SCIF_ERRORS) {
114 return serial_rx_fifo_level(port) ? 1 : 0;
117 static int serial_getc_check(struct uart_port *port)
119 unsigned short status;
121 status = sci_in(port, SCxSR);
123 if (status & SCIF_ERRORS)
125 if (sci_in(port, SCLSR) & SCxSR_ORER(port))
127 return status & (SCIF_DR | SCxSR_RDxF(port));
130 static int sh_serial_getc_generic(struct uart_port *port)
132 unsigned short status;
135 if (!serial_getc_check(port))
138 ch = sci_in(port, SCxRDR);
139 status = sci_in(port, SCxSR);
141 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
143 if (status & SCIF_ERRORS)
146 if (sci_in(port, SCLSR) & SCxSR_ORER(port))
152 #if CONFIG_IS_ENABLED(DM_SERIAL)
154 static int sh_serial_pending(struct udevice *dev, bool input)
156 struct uart_port *priv = dev_get_priv(dev);
158 return sh_serial_tstc_generic(priv);
161 static int sh_serial_putc(struct udevice *dev, const char ch)
163 struct uart_port *priv = dev_get_priv(dev);
165 return serial_raw_putc(priv, ch);
168 static int sh_serial_getc(struct udevice *dev)
170 struct uart_port *priv = dev_get_priv(dev);
172 return sh_serial_getc_generic(priv);
175 static int sh_serial_setbrg(struct udevice *dev, int baudrate)
177 struct sh_serial_platdata *plat = dev_get_platdata(dev);
178 struct uart_port *priv = dev_get_priv(dev);
180 sh_serial_setbrg_generic(priv, plat->clk, baudrate);
185 static int sh_serial_probe(struct udevice *dev)
187 struct sh_serial_platdata *plat = dev_get_platdata(dev);
188 struct uart_port *priv = dev_get_priv(dev);
190 priv->membase = (unsigned char *)plat->base;
191 priv->mapbase = plat->base;
192 priv->type = plat->type;
193 priv->clk_mode = plat->clk_mode;
195 sh_serial_init_generic(priv);
200 static const struct dm_serial_ops sh_serial_ops = {
201 .putc = sh_serial_putc,
202 .pending = sh_serial_pending,
203 .getc = sh_serial_getc,
204 .setbrg = sh_serial_setbrg,
207 #if CONFIG_IS_ENABLED(OF_CONTROL)
208 static const struct udevice_id sh_serial_id[] ={
209 {.compatible = "renesas,sci", .data = PORT_SCI},
210 {.compatible = "renesas,scif", .data = PORT_SCIF},
211 {.compatible = "renesas,scifa", .data = PORT_SCIFA},
215 static int sh_serial_ofdata_to_platdata(struct udevice *dev)
217 struct sh_serial_platdata *plat = dev_get_platdata(dev);
218 struct clk sh_serial_clk;
222 addr = devfdt_get_addr(dev);
228 ret = clk_get_by_name(dev, "fck", &sh_serial_clk);
230 ret = clk_enable(&sh_serial_clk);
232 plat->clk = clk_get_rate(&sh_serial_clk);
234 plat->clk = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev),
238 plat->type = dev_get_driver_data(dev);
243 U_BOOT_DRIVER(serial_sh) = {
246 .of_match = of_match_ptr(sh_serial_id),
247 .ofdata_to_platdata = of_match_ptr(sh_serial_ofdata_to_platdata),
248 .platdata_auto_alloc_size = sizeof(struct sh_serial_platdata),
249 .probe = sh_serial_probe,
250 .ops = &sh_serial_ops,
251 .flags = DM_FLAG_PRE_RELOC,
252 .priv_auto_alloc_size = sizeof(struct uart_port),
255 #else /* CONFIG_DM_SERIAL */
257 #if defined(CONFIG_CONS_SCIF0)
258 # define SCIF_BASE SCIF0_BASE
259 #elif defined(CONFIG_CONS_SCIF1)
260 # define SCIF_BASE SCIF1_BASE
261 #elif defined(CONFIG_CONS_SCIF2)
262 # define SCIF_BASE SCIF2_BASE
263 #elif defined(CONFIG_CONS_SCIF3)
264 # define SCIF_BASE SCIF3_BASE
265 #elif defined(CONFIG_CONS_SCIF4)
266 # define SCIF_BASE SCIF4_BASE
267 #elif defined(CONFIG_CONS_SCIF5)
268 # define SCIF_BASE SCIF5_BASE
269 #elif defined(CONFIG_CONS_SCIF6)
270 # define SCIF_BASE SCIF6_BASE
271 #elif defined(CONFIG_CONS_SCIF7)
272 # define SCIF_BASE SCIF7_BASE
273 #elif defined(CONFIG_CONS_SCIFA0)
274 # define SCIF_BASE SCIFA0_BASE
276 # error "Default SCIF doesn't set....."
279 #if defined(CONFIG_SCIF_A)
280 #define SCIF_BASE_PORT PORT_SCIFA
281 #elif defined(CONFIG_SCI)
282 #define SCIF_BASE_PORT PORT_SCI
284 #define SCIF_BASE_PORT PORT_SCIF
287 static struct uart_port sh_sci = {
288 .membase = (unsigned char *)SCIF_BASE,
289 .mapbase = SCIF_BASE,
290 .type = SCIF_BASE_PORT,
291 #ifdef CONFIG_SCIF_USE_EXT_CLK
296 static void sh_serial_setbrg(void)
298 DECLARE_GLOBAL_DATA_PTR;
299 struct uart_port *port = &sh_sci;
301 sh_serial_setbrg_generic(port, CONFIG_SH_SCIF_CLK_FREQ, gd->baudrate);
304 static int sh_serial_init(void)
306 struct uart_port *port = &sh_sci;
308 sh_serial_init_generic(port);
314 static void sh_serial_putc(const char c)
316 struct uart_port *port = &sh_sci;
320 if (serial_raw_putc(port, '\r') != -EAGAIN)
325 if (serial_raw_putc(port, c) != -EAGAIN)
330 static int sh_serial_tstc(void)
332 struct uart_port *port = &sh_sci;
334 return sh_serial_tstc_generic(port);
337 static int sh_serial_getc(void)
339 struct uart_port *port = &sh_sci;
343 ch = sh_serial_getc_generic(port);
351 static struct serial_device sh_serial_drv = {
353 .start = sh_serial_init,
355 .setbrg = sh_serial_setbrg,
356 .putc = sh_serial_putc,
357 .puts = default_serial_puts,
358 .getc = sh_serial_getc,
359 .tstc = sh_serial_tstc,
362 void sh_serial_initialize(void)
364 serial_register(&sh_serial_drv);
367 __weak struct serial_device *default_serial_console(void)
369 return &sh_serial_drv;
371 #endif /* CONFIG_DM_SERIAL */