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 <linux/delay.h>
19 #include "serial_sh.h"
21 DECLARE_GLOBAL_DATA_PTR;
23 #if defined(CONFIG_CPU_SH7780)
24 static int scif_rxfill(struct uart_port *port)
26 return sci_in(port, SCRFDR) & 0xff;
28 #elif defined(CONFIG_CPU_SH7763)
29 static int scif_rxfill(struct uart_port *port)
31 if ((port->mapbase == 0xffe00000) ||
32 (port->mapbase == 0xffe08000)) {
34 return sci_in(port, SCRFDR) & 0xff;
37 return sci_in(port, SCFDR) & SCIF2_RFDC_MASK;
41 static int scif_rxfill(struct uart_port *port)
43 return sci_in(port, SCFDR) & SCIF_RFDC_MASK;
47 static void sh_serial_init_generic(struct uart_port *port)
49 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);
55 sci_out(port, SCFCR, 0);
56 #if defined(CONFIG_RZA1)
57 sci_out(port, SCSPTR, 0x0003);
62 sh_serial_setbrg_generic(struct uart_port *port, int clk, int baudrate)
64 if (port->clk_mode == EXT_CLK) {
65 unsigned short dl = DL_VALUE(baudrate, clk);
66 sci_out(port, DL, dl);
67 /* Need wait: Clock * 1/dl * 1/16 */
68 udelay((1000000 * dl * 16 / clk) * 1000 + 1);
70 sci_out(port, SCBRR, SCBRR_VALUE(baudrate, clk));
74 static void handle_error(struct uart_port *port)
77 sci_out(port, SCxSR, SCxSR_ERROR_CLEAR(port));
79 sci_out(port, SCLSR, 0x00);
82 static int serial_raw_putc(struct uart_port *port, const char c)
84 /* Tx fifo is empty */
85 if (!(sci_in(port, SCxSR) & SCxSR_TEND(port)))
88 sci_out(port, SCxTDR, c);
89 sci_out(port, SCxSR, sci_in(port, SCxSR) & ~SCxSR_TEND(port));
94 static int serial_rx_fifo_level(struct uart_port *port)
96 return scif_rxfill(port);
99 static int sh_serial_tstc_generic(struct uart_port *port)
101 if (sci_in(port, SCxSR) & SCIF_ERRORS) {
106 return serial_rx_fifo_level(port) ? 1 : 0;
109 static int serial_getc_check(struct uart_port *port)
111 unsigned short status;
113 status = sci_in(port, SCxSR);
115 if (status & SCIF_ERRORS)
117 if (sci_in(port, SCLSR) & SCxSR_ORER(port))
119 return status & (SCIF_DR | SCxSR_RDxF(port));
122 static int sh_serial_getc_generic(struct uart_port *port)
124 unsigned short status;
127 if (!serial_getc_check(port))
130 ch = sci_in(port, SCxRDR);
131 status = sci_in(port, SCxSR);
133 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
135 if (status & SCIF_ERRORS)
138 if (sci_in(port, SCLSR) & SCxSR_ORER(port))
144 #if CONFIG_IS_ENABLED(DM_SERIAL)
146 static int sh_serial_pending(struct udevice *dev, bool input)
148 struct uart_port *priv = dev_get_priv(dev);
150 return sh_serial_tstc_generic(priv);
153 static int sh_serial_putc(struct udevice *dev, const char ch)
155 struct uart_port *priv = dev_get_priv(dev);
157 return serial_raw_putc(priv, ch);
160 static int sh_serial_getc(struct udevice *dev)
162 struct uart_port *priv = dev_get_priv(dev);
164 return sh_serial_getc_generic(priv);
167 static int sh_serial_setbrg(struct udevice *dev, int baudrate)
169 struct sh_serial_platdata *plat = dev_get_platdata(dev);
170 struct uart_port *priv = dev_get_priv(dev);
172 sh_serial_setbrg_generic(priv, plat->clk, baudrate);
177 static int sh_serial_probe(struct udevice *dev)
179 struct sh_serial_platdata *plat = dev_get_platdata(dev);
180 struct uart_port *priv = dev_get_priv(dev);
182 priv->membase = (unsigned char *)plat->base;
183 priv->mapbase = plat->base;
184 priv->type = plat->type;
185 priv->clk_mode = plat->clk_mode;
187 sh_serial_init_generic(priv);
192 static 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,
199 #if CONFIG_IS_ENABLED(OF_CONTROL)
200 static const struct udevice_id sh_serial_id[] ={
201 {.compatible = "renesas,sci", .data = PORT_SCI},
202 {.compatible = "renesas,scif", .data = PORT_SCIF},
203 {.compatible = "renesas,scifa", .data = PORT_SCIFA},
207 static int sh_serial_ofdata_to_platdata(struct udevice *dev)
209 struct sh_serial_platdata *plat = dev_get_platdata(dev);
210 struct clk sh_serial_clk;
214 addr = dev_read_addr(dev);
220 ret = clk_get_by_name(dev, "fck", &sh_serial_clk);
222 ret = clk_enable(&sh_serial_clk);
224 plat->clk = clk_get_rate(&sh_serial_clk);
226 plat->clk = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev),
230 plat->type = dev_get_driver_data(dev);
235 U_BOOT_DRIVER(serial_sh) = {
238 .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),
241 .probe = sh_serial_probe,
242 .ops = &sh_serial_ops,
243 #if !CONFIG_IS_ENABLED(OF_CONTROL)
244 .flags = DM_FLAG_PRE_RELOC,
246 .priv_auto_alloc_size = sizeof(struct uart_port),
249 #else /* CONFIG_DM_SERIAL */
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
267 #elif defined(CONFIG_CONS_SCIFA0)
268 # define SCIF_BASE SCIFA0_BASE
270 # error "Default SCIF doesn't set....."
273 #if defined(CONFIG_SCIF_A)
274 #define SCIF_BASE_PORT PORT_SCIFA
275 #elif defined(CONFIG_SCI)
276 #define SCIF_BASE_PORT PORT_SCI
278 #define SCIF_BASE_PORT PORT_SCIF
281 static 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
290 static void sh_serial_setbrg(void)
292 DECLARE_GLOBAL_DATA_PTR;
293 struct uart_port *port = &sh_sci;
295 sh_serial_setbrg_generic(port, CONFIG_SH_SCIF_CLK_FREQ, gd->baudrate);
298 static int sh_serial_init(void)
300 struct uart_port *port = &sh_sci;
302 sh_serial_init_generic(port);
308 static void sh_serial_putc(const char c)
310 struct uart_port *port = &sh_sci;
314 if (serial_raw_putc(port, '\r') != -EAGAIN)
319 if (serial_raw_putc(port, c) != -EAGAIN)
324 static int sh_serial_tstc(void)
326 struct uart_port *port = &sh_sci;
328 return sh_serial_tstc_generic(port);
331 static int sh_serial_getc(void)
333 struct uart_port *port = &sh_sci;
337 ch = sh_serial_getc_generic(port);
345 static struct serial_device sh_serial_drv = {
347 .start = sh_serial_init,
349 .setbrg = sh_serial_setbrg,
350 .putc = sh_serial_putc,
351 .puts = default_serial_puts,
352 .getc = sh_serial_getc,
353 .tstc = sh_serial_tstc,
356 void sh_serial_initialize(void)
358 serial_register(&sh_serial_drv);
361 __weak struct serial_device *default_serial_console(void)
363 return &sh_serial_drv;
365 #endif /* CONFIG_DM_SERIAL */