1 // SPDX-License-Identifier: GPL-2.0+
3 * SuperH SCIF device driver.
4 * Copyright (C) 2013 Renesas Electronics Corporation
5 * Copyright (C) 2007,2008,2010, 2014 Nobuhiro Iwamatsu
6 * Copyright (C) 2002 - 2008 Paul Mundt
9 #include <asm/global_data.h>
11 #include <asm/processor.h>
14 #include <dm/device_compat.h>
15 #include <dm/platform_data/serial_sh.h>
17 #include <linux/compiler.h>
18 #include <linux/delay.h>
21 #include "serial_sh.h"
23 DECLARE_GLOBAL_DATA_PTR;
25 #if defined(CONFIG_CPU_SH7780)
26 static int scif_rxfill(struct uart_port *port)
28 return sci_in(port, SCRFDR) & 0xff;
30 #elif defined(CONFIG_CPU_SH7763)
31 static int scif_rxfill(struct uart_port *port)
33 if ((port->mapbase == 0xffe00000) ||
34 (port->mapbase == 0xffe08000)) {
36 return sci_in(port, SCRFDR) & 0xff;
39 return sci_in(port, SCFDR) & SCIF2_RFDC_MASK;
43 static int scif_rxfill(struct uart_port *port)
45 return sci_in(port, SCFDR) & SCIF_RFDC_MASK;
49 static void sh_serial_init_generic(struct uart_port *port)
51 sci_out(port, SCSCR , SCSCR_INIT(port));
52 sci_out(port, SCSCR , SCSCR_INIT(port));
53 sci_out(port, SCSMR, 0);
54 sci_out(port, SCSMR, 0);
55 sci_out(port, SCFCR, SCFCR_RFRST|SCFCR_TFRST);
57 sci_out(port, SCFCR, 0);
58 #if defined(CONFIG_RZA1)
59 sci_out(port, SCSPTR, 0x0003);
62 #if IS_ENABLED(CONFIG_RCAR_GEN2) || IS_ENABLED(CONFIG_RCAR_GEN3) || IS_ENABLED(CONFIG_RCAR_GEN4)
63 if (port->type == PORT_HSCIF)
64 sci_out(port, HSSRR, HSSRR_SRE | HSSRR_SRCYC8);
69 sh_serial_setbrg_generic(struct uart_port *port, int clk, int baudrate)
71 if (port->clk_mode == EXT_CLK) {
72 unsigned short dl = DL_VALUE(baudrate, clk);
73 sci_out(port, DL, dl);
74 /* Need wait: Clock * 1/dl * 1/16 */
75 udelay((1000000 * dl * 16 / clk) * 1000 + 1);
77 sci_out(port, SCBRR, SCBRR_VALUE(baudrate, clk));
81 static void handle_error(struct uart_port *port)
84 * Most errors are cleared by resetting the relevant error bits to zero
85 * in the FSR & LSR registers. For each register, a read followed by a
86 * write is needed according to the relevant datasheets.
88 unsigned short status = sci_in(port, SCxSR);
89 sci_out(port, SCxSR, status & ~SCxSR_ERRORS(port));
91 sci_out(port, SCLSR, 0x00);
94 * To clear framing errors, we also need to read and discard a
97 if ((port->type != PORT_SCI) && (status & SCIF_FER))
101 static int serial_raw_putc(struct uart_port *port, const char c)
103 /* Tx fifo is empty */
104 if (!(sci_in(port, SCxSR) & SCxSR_TEND(port)))
107 sci_out(port, SCxTDR, c);
108 sci_out(port, SCxSR, sci_in(port, SCxSR) & ~SCxSR_TEND(port));
113 static int serial_rx_fifo_level(struct uart_port *port)
115 return scif_rxfill(port);
118 static int sh_serial_tstc_generic(struct uart_port *port)
120 if (sci_in(port, SCxSR) & SCIF_ERRORS) {
125 return serial_rx_fifo_level(port) ? 1 : 0;
128 static int serial_getc_check(struct uart_port *port)
130 unsigned short status;
132 status = sci_in(port, SCxSR);
134 if (status & SCIF_ERRORS)
136 if (sci_in(port, SCLSR) & SCxSR_ORER(port))
138 status &= (SCIF_DR | SCxSR_RDxF(port));
141 return scif_rxfill(port);
144 static int sh_serial_getc_generic(struct uart_port *port)
146 unsigned short status;
149 if (!serial_getc_check(port))
152 ch = sci_in(port, SCxRDR);
153 status = sci_in(port, SCxSR);
155 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
157 if (status & SCIF_ERRORS)
160 if (sci_in(port, SCLSR) & SCxSR_ORER(port))
166 #if CONFIG_IS_ENABLED(DM_SERIAL)
168 static int sh_serial_pending(struct udevice *dev, bool input)
170 struct uart_port *priv = dev_get_priv(dev);
172 return sh_serial_tstc_generic(priv);
175 static int sh_serial_putc(struct udevice *dev, const char ch)
177 struct uart_port *priv = dev_get_priv(dev);
179 return serial_raw_putc(priv, ch);
182 static int sh_serial_getc(struct udevice *dev)
184 struct uart_port *priv = dev_get_priv(dev);
186 return sh_serial_getc_generic(priv);
189 static int sh_serial_setbrg(struct udevice *dev, int baudrate)
191 struct sh_serial_plat *plat = dev_get_plat(dev);
192 struct uart_port *priv = dev_get_priv(dev);
194 sh_serial_setbrg_generic(priv, plat->clk, baudrate);
199 static int sh_serial_probe(struct udevice *dev)
201 struct sh_serial_plat *plat = dev_get_plat(dev);
202 struct uart_port *priv = dev_get_priv(dev);
203 struct reset_ctl rst;
206 priv->membase = (unsigned char *)plat->base;
207 priv->mapbase = plat->base;
208 priv->type = plat->type;
209 priv->clk_mode = plat->clk_mode;
211 /* De-assert the module reset if it is defined. */
212 ret = reset_get_by_index(dev, 0, &rst);
214 ret = reset_deassert(&rst);
216 dev_err(dev, "failed to de-assert reset line\n");
221 sh_serial_init_generic(priv);
226 static const struct dm_serial_ops sh_serial_ops = {
227 .putc = sh_serial_putc,
228 .pending = sh_serial_pending,
229 .getc = sh_serial_getc,
230 .setbrg = sh_serial_setbrg,
233 #if CONFIG_IS_ENABLED(OF_CONTROL)
234 static const struct udevice_id sh_serial_id[] ={
235 {.compatible = "renesas,sci", .data = PORT_SCI},
236 {.compatible = "renesas,scif", .data = PORT_SCIF},
237 {.compatible = "renesas,scif-r9a07g044", .data = PORT_SCIFA},
238 {.compatible = "renesas,scifa", .data = PORT_SCIFA},
239 {.compatible = "renesas,hscif", .data = PORT_HSCIF},
243 static int sh_serial_of_to_plat(struct udevice *dev)
245 struct sh_serial_plat *plat = dev_get_plat(dev);
246 struct clk sh_serial_clk;
250 addr = dev_read_addr(dev);
256 ret = clk_get_by_name(dev, "fck", &sh_serial_clk);
258 ret = clk_enable(&sh_serial_clk);
260 plat->clk = clk_get_rate(&sh_serial_clk);
262 plat->clk = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev),
266 plat->type = dev_get_driver_data(dev);
271 U_BOOT_DRIVER(serial_sh) = {
274 .of_match = of_match_ptr(sh_serial_id),
275 .of_to_plat = of_match_ptr(sh_serial_of_to_plat),
276 .plat_auto = sizeof(struct sh_serial_plat),
277 .probe = sh_serial_probe,
278 .ops = &sh_serial_ops,
279 #if !CONFIG_IS_ENABLED(OF_CONTROL)
280 .flags = DM_FLAG_PRE_RELOC,
282 .priv_auto = sizeof(struct uart_port),
286 #if !CONFIG_IS_ENABLED(DM_SERIAL) || IS_ENABLED(CONFIG_DEBUG_UART_SCIF)
288 #if defined(CFG_SCIF_A)
289 #define SCIF_BASE_PORT PORT_SCIFA
290 #elif defined(CFG_SCI)
291 #define SCIF_BASE_PORT PORT_SCI
292 #elif defined(CFG_HSCIF)
293 #define SCIF_BASE_PORT PORT_HSCIF
295 #define SCIF_BASE_PORT PORT_SCIF
298 static void sh_serial_init_nodm(struct uart_port *port)
300 sh_serial_init_generic(port);
304 static void sh_serial_putc_nondm(struct uart_port *port, const char c)
308 if (serial_raw_putc(port, '\r') != -EAGAIN)
313 if (serial_raw_putc(port, c) != -EAGAIN)
319 #if !CONFIG_IS_ENABLED(DM_SERIAL)
320 #if defined(CONFIG_CONS_SCIF0)
321 # define SCIF_BASE SCIF0_BASE
322 #elif defined(CONFIG_CONS_SCIF1)
323 # define SCIF_BASE SCIF1_BASE
324 #elif defined(CONFIG_CONS_SCIF2)
325 # define SCIF_BASE SCIF2_BASE
326 #elif defined(CONFIG_CONS_SCIF3)
327 # define SCIF_BASE SCIF3_BASE
328 #elif defined(CONFIG_CONS_SCIF4)
329 # define SCIF_BASE SCIF4_BASE
330 #elif defined(CONFIG_CONS_SCIF5)
331 # define SCIF_BASE SCIF5_BASE
332 #elif defined(CONFIG_CONS_SCIF6)
333 # define SCIF_BASE SCIF6_BASE
334 #elif defined(CONFIG_CONS_SCIF7)
335 # define SCIF_BASE SCIF7_BASE
336 #elif defined(CONFIG_CONS_SCIFA0)
337 # define SCIF_BASE SCIFA0_BASE
339 # error "Default SCIF doesn't set....."
342 static struct uart_port sh_sci = {
343 .membase = (unsigned char *)SCIF_BASE,
344 .mapbase = SCIF_BASE,
345 .type = SCIF_BASE_PORT,
346 #ifdef CFG_SCIF_USE_EXT_CLK
351 static void sh_serial_setbrg(void)
353 DECLARE_GLOBAL_DATA_PTR;
354 struct uart_port *port = &sh_sci;
356 sh_serial_setbrg_generic(port, CONFIG_SH_SCIF_CLK_FREQ, gd->baudrate);
359 static int sh_serial_init(void)
361 sh_serial_init_nodm(&sh_sci);
366 static void sh_serial_putc(const char c)
368 sh_serial_putc_nondm(&sh_sci, c);
371 static int sh_serial_tstc(void)
373 struct uart_port *port = &sh_sci;
375 return sh_serial_tstc_generic(port);
378 static int sh_serial_getc(void)
380 struct uart_port *port = &sh_sci;
384 ch = sh_serial_getc_generic(port);
392 static struct serial_device sh_serial_drv = {
394 .start = sh_serial_init,
396 .setbrg = sh_serial_setbrg,
397 .putc = sh_serial_putc,
398 .puts = default_serial_puts,
399 .getc = sh_serial_getc,
400 .tstc = sh_serial_tstc,
403 void sh_serial_initialize(void)
405 serial_register(&sh_serial_drv);
408 __weak struct serial_device *default_serial_console(void)
410 return &sh_serial_drv;
412 #endif /* CONFIG_DM_SERIAL */
414 #ifdef CONFIG_DEBUG_UART_SCIF
415 #include <debug_uart.h>
417 static struct uart_port debug_uart_sci = {
418 .membase = (unsigned char *)CONFIG_DEBUG_UART_BASE,
419 .mapbase = CONFIG_DEBUG_UART_BASE,
420 .type = SCIF_BASE_PORT,
421 #ifdef CFG_SCIF_USE_EXT_CLK
426 static inline void _debug_uart_init(void)
428 sh_serial_init_nodm(&debug_uart_sci);
431 static inline void _debug_uart_putc(int c)
433 sh_serial_putc_nondm(&debug_uart_sci, c);