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
14 #include <asm/processor.h>
16 #include <linux/compiler.h>
17 #include <dm/platform_data/serial_sh.h>
18 #include "serial_sh.h"
20 DECLARE_GLOBAL_DATA_PTR;
22 #if defined(CONFIG_CPU_SH7780)
23 static int scif_rxfill(struct uart_port *port)
25 return sci_in(port, SCRFDR) & 0xff;
27 #elif defined(CONFIG_CPU_SH7763)
28 static int scif_rxfill(struct uart_port *port)
30 if ((port->mapbase == 0xffe00000) ||
31 (port->mapbase == 0xffe08000)) {
33 return sci_in(port, SCRFDR) & 0xff;
36 return sci_in(port, SCFDR) & SCIF2_RFDC_MASK;
40 static int scif_rxfill(struct uart_port *port)
42 return sci_in(port, SCFDR) & SCIF_RFDC_MASK;
46 static void sh_serial_init_generic(struct uart_port *port)
48 sci_out(port, SCSCR , SCSCR_INIT(port));
49 sci_out(port, SCSCR , SCSCR_INIT(port));
50 sci_out(port, SCSMR, 0);
51 sci_out(port, SCSMR, 0);
52 sci_out(port, SCFCR, SCFCR_RFRST|SCFCR_TFRST);
54 sci_out(port, SCFCR, 0);
55 #if defined(CONFIG_RZA1)
56 sci_out(port, SCSPTR, 0x0003);
61 sh_serial_setbrg_generic(struct uart_port *port, int clk, int baudrate)
63 if (port->clk_mode == EXT_CLK) {
64 unsigned short dl = DL_VALUE(baudrate, clk);
65 sci_out(port, DL, dl);
66 /* Need wait: Clock * 1/dl * 1/16 */
67 udelay((1000000 * dl * 16 / clk) * 1000 + 1);
69 sci_out(port, SCBRR, SCBRR_VALUE(baudrate, clk));
73 static void handle_error(struct uart_port *port)
76 sci_out(port, SCxSR, SCxSR_ERROR_CLEAR(port));
78 sci_out(port, SCLSR, 0x00);
81 static int serial_raw_putc(struct uart_port *port, const char c)
83 /* Tx fifo is empty */
84 if (!(sci_in(port, SCxSR) & SCxSR_TEND(port)))
87 sci_out(port, SCxTDR, c);
88 sci_out(port, SCxSR, sci_in(port, SCxSR) & ~SCxSR_TEND(port));
93 static int serial_rx_fifo_level(struct uart_port *port)
95 return scif_rxfill(port);
98 static int sh_serial_tstc_generic(struct uart_port *port)
100 if (sci_in(port, SCxSR) & SCIF_ERRORS) {
105 return serial_rx_fifo_level(port) ? 1 : 0;
108 static int serial_getc_check(struct uart_port *port)
110 unsigned short status;
112 status = sci_in(port, SCxSR);
114 if (status & SCIF_ERRORS)
116 if (sci_in(port, SCLSR) & SCxSR_ORER(port))
118 return status & (SCIF_DR | SCxSR_RDxF(port));
121 static int sh_serial_getc_generic(struct uart_port *port)
123 unsigned short status;
126 if (!serial_getc_check(port))
129 ch = sci_in(port, SCxRDR);
130 status = sci_in(port, SCxSR);
132 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
134 if (status & SCIF_ERRORS)
137 if (sci_in(port, SCLSR) & SCxSR_ORER(port))
143 #if CONFIG_IS_ENABLED(DM_SERIAL)
145 static int sh_serial_pending(struct udevice *dev, bool input)
147 struct uart_port *priv = dev_get_priv(dev);
149 return sh_serial_tstc_generic(priv);
152 static int sh_serial_putc(struct udevice *dev, const char ch)
154 struct uart_port *priv = dev_get_priv(dev);
156 return serial_raw_putc(priv, ch);
159 static int sh_serial_getc(struct udevice *dev)
161 struct uart_port *priv = dev_get_priv(dev);
163 return sh_serial_getc_generic(priv);
166 static int sh_serial_setbrg(struct udevice *dev, int baudrate)
168 struct sh_serial_platdata *plat = dev_get_platdata(dev);
169 struct uart_port *priv = dev_get_priv(dev);
171 sh_serial_setbrg_generic(priv, plat->clk, baudrate);
176 static int sh_serial_probe(struct udevice *dev)
178 struct sh_serial_platdata *plat = dev_get_platdata(dev);
179 struct uart_port *priv = dev_get_priv(dev);
181 priv->membase = (unsigned char *)plat->base;
182 priv->mapbase = plat->base;
183 priv->type = plat->type;
184 priv->clk_mode = plat->clk_mode;
186 sh_serial_init_generic(priv);
191 static const struct dm_serial_ops sh_serial_ops = {
192 .putc = sh_serial_putc,
193 .pending = sh_serial_pending,
194 .getc = sh_serial_getc,
195 .setbrg = sh_serial_setbrg,
198 #if CONFIG_IS_ENABLED(OF_CONTROL)
199 static const struct udevice_id sh_serial_id[] ={
200 {.compatible = "renesas,sci", .data = PORT_SCI},
201 {.compatible = "renesas,scif", .data = PORT_SCIF},
202 {.compatible = "renesas,scifa", .data = PORT_SCIFA},
206 static int sh_serial_ofdata_to_platdata(struct udevice *dev)
208 struct sh_serial_platdata *plat = dev_get_platdata(dev);
209 struct clk sh_serial_clk;
213 addr = devfdt_get_addr(dev);
219 ret = clk_get_by_name(dev, "fck", &sh_serial_clk);
221 ret = clk_enable(&sh_serial_clk);
223 plat->clk = clk_get_rate(&sh_serial_clk);
225 plat->clk = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev),
229 plat->type = dev_get_driver_data(dev);
234 U_BOOT_DRIVER(serial_sh) = {
237 .of_match = of_match_ptr(sh_serial_id),
238 .ofdata_to_platdata = of_match_ptr(sh_serial_ofdata_to_platdata),
239 .platdata_auto_alloc_size = sizeof(struct sh_serial_platdata),
240 .probe = sh_serial_probe,
241 .ops = &sh_serial_ops,
242 #if !CONFIG_IS_ENABLED(OF_CONTROL)
243 .flags = DM_FLAG_PRE_RELOC,
245 .priv_auto_alloc_size = sizeof(struct uart_port),
248 #else /* CONFIG_DM_SERIAL */
250 #if defined(CONFIG_CONS_SCIF0)
251 # define SCIF_BASE SCIF0_BASE
252 #elif defined(CONFIG_CONS_SCIF1)
253 # define SCIF_BASE SCIF1_BASE
254 #elif defined(CONFIG_CONS_SCIF2)
255 # define SCIF_BASE SCIF2_BASE
256 #elif defined(CONFIG_CONS_SCIF3)
257 # define SCIF_BASE SCIF3_BASE
258 #elif defined(CONFIG_CONS_SCIF4)
259 # define SCIF_BASE SCIF4_BASE
260 #elif defined(CONFIG_CONS_SCIF5)
261 # define SCIF_BASE SCIF5_BASE
262 #elif defined(CONFIG_CONS_SCIF6)
263 # define SCIF_BASE SCIF6_BASE
264 #elif defined(CONFIG_CONS_SCIF7)
265 # define SCIF_BASE SCIF7_BASE
266 #elif defined(CONFIG_CONS_SCIFA0)
267 # define SCIF_BASE SCIFA0_BASE
269 # error "Default SCIF doesn't set....."
272 #if defined(CONFIG_SCIF_A)
273 #define SCIF_BASE_PORT PORT_SCIFA
274 #elif defined(CONFIG_SCI)
275 #define SCIF_BASE_PORT PORT_SCI
277 #define SCIF_BASE_PORT PORT_SCIF
280 static struct uart_port sh_sci = {
281 .membase = (unsigned char *)SCIF_BASE,
282 .mapbase = SCIF_BASE,
283 .type = SCIF_BASE_PORT,
284 #ifdef CONFIG_SCIF_USE_EXT_CLK
289 static void sh_serial_setbrg(void)
291 DECLARE_GLOBAL_DATA_PTR;
292 struct uart_port *port = &sh_sci;
294 sh_serial_setbrg_generic(port, CONFIG_SH_SCIF_CLK_FREQ, gd->baudrate);
297 static int sh_serial_init(void)
299 struct uart_port *port = &sh_sci;
301 sh_serial_init_generic(port);
307 static void sh_serial_putc(const char c)
309 struct uart_port *port = &sh_sci;
313 if (serial_raw_putc(port, '\r') != -EAGAIN)
318 if (serial_raw_putc(port, c) != -EAGAIN)
323 static int sh_serial_tstc(void)
325 struct uart_port *port = &sh_sci;
327 return sh_serial_tstc_generic(port);
330 static int sh_serial_getc(void)
332 struct uart_port *port = &sh_sci;
336 ch = sh_serial_getc_generic(port);
344 static struct serial_device sh_serial_drv = {
346 .start = sh_serial_init,
348 .setbrg = sh_serial_setbrg,
349 .putc = sh_serial_putc,
350 .puts = default_serial_puts,
351 .getc = sh_serial_getc,
352 .tstc = sh_serial_tstc,
355 void sh_serial_initialize(void)
357 serial_register(&sh_serial_drv);
360 __weak struct serial_device *default_serial_console(void)
362 return &sh_serial_drv;
364 #endif /* CONFIG_DM_SERIAL */