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+
14 #include <asm/processor.h>
16 #include <linux/compiler.h>
17 #include <dm/platform_data/serial_sh.h>
18 #include "serial_sh.h"
20 #if defined(CONFIG_CPU_SH7760) || \
21 defined(CONFIG_CPU_SH7780) || \
22 defined(CONFIG_CPU_SH7785) || \
23 defined(CONFIG_CPU_SH7786)
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;
40 #elif defined(CONFIG_ARCH_SH7372)
41 static int scif_rxfill(struct uart_port *port)
43 if (port->type == PORT_SCIFA)
44 return sci_in(port, SCFDR) & SCIF_RFDC_MASK;
46 return sci_in(port, SCRFDR);
49 static int scif_rxfill(struct uart_port *port)
51 return sci_in(port, SCFDR) & SCIF_RFDC_MASK;
55 static void sh_serial_init_generic(struct uart_port *port)
57 sci_out(port, SCSCR , SCSCR_INIT(port));
58 sci_out(port, SCSCR , SCSCR_INIT(port));
59 sci_out(port, SCSMR, 0);
60 sci_out(port, SCSMR, 0);
61 sci_out(port, SCFCR, SCFCR_RFRST|SCFCR_TFRST);
63 sci_out(port, SCFCR, 0);
67 sh_serial_setbrg_generic(struct uart_port *port, int clk, int baudrate)
69 if (port->clk_mode == EXT_CLK) {
70 unsigned short dl = DL_VALUE(baudrate, clk);
71 sci_out(port, DL, dl);
72 /* Need wait: Clock * 1/dl
\e$B!_
\e(B 1/16 */
73 udelay((1000000 * dl * 16 / clk) * 1000 + 1);
75 sci_out(port, SCBRR, SCBRR_VALUE(baudrate, clk));
79 static void handle_error(struct uart_port *port)
82 sci_out(port, SCxSR, SCxSR_ERROR_CLEAR(port));
84 sci_out(port, SCLSR, 0x00);
87 static int serial_raw_putc(struct uart_port *port, const char c)
89 /* Tx fifo is empty */
90 if (!(sci_in(port, SCxSR) & SCxSR_TEND(port)))
93 sci_out(port, SCxTDR, c);
94 sci_out(port, SCxSR, sci_in(port, SCxSR) & ~SCxSR_TEND(port));
99 static int serial_rx_fifo_level(struct uart_port *port)
101 return scif_rxfill(port);
104 static int sh_serial_tstc_generic(struct uart_port *port)
106 if (sci_in(port, SCxSR) & SCIF_ERRORS) {
111 return serial_rx_fifo_level(port) ? 1 : 0;
114 static int serial_getc_check(struct uart_port *port)
116 unsigned short status;
118 status = sci_in(port, SCxSR);
120 if (status & SCIF_ERRORS)
122 if (sci_in(port, SCLSR) & SCxSR_ORER(port))
124 return status & (SCIF_DR | SCxSR_RDxF(port));
127 static int sh_serial_getc_generic(struct uart_port *port)
129 unsigned short status;
132 if (!serial_getc_check(port))
135 ch = sci_in(port, SCxRDR);
136 status = sci_in(port, SCxSR);
138 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
140 if (status & SCIF_ERRORS)
143 if (sci_in(port, SCLSR) & SCxSR_ORER(port))
149 #ifdef CONFIG_DM_SERIAL
151 static int sh_serial_pending(struct udevice *dev, bool input)
153 struct uart_port *priv = dev_get_priv(dev);
155 return sh_serial_tstc_generic(priv);
158 static int sh_serial_putc(struct udevice *dev, const char ch)
160 struct uart_port *priv = dev_get_priv(dev);
162 return serial_raw_putc(priv, ch);
165 static int sh_serial_getc(struct udevice *dev)
167 struct uart_port *priv = dev_get_priv(dev);
169 return sh_serial_getc_generic(priv);
172 static int sh_serial_setbrg(struct udevice *dev, int baudrate)
174 struct sh_serial_platdata *plat = dev_get_platdata(dev);
175 struct uart_port *priv = dev_get_priv(dev);
177 sh_serial_setbrg_generic(priv, plat->clk, baudrate);
182 static int sh_serial_probe(struct udevice *dev)
184 struct sh_serial_platdata *plat = dev_get_platdata(dev);
185 struct uart_port *priv = dev_get_priv(dev);
187 priv->membase = (unsigned char *)plat->base;
188 priv->mapbase = plat->base;
189 priv->type = plat->type;
190 priv->clk_mode = plat->clk_mode;
192 sh_serial_init_generic(priv);
197 static const struct dm_serial_ops sh_serial_ops = {
198 .putc = sh_serial_putc,
199 .pending = sh_serial_pending,
200 .getc = sh_serial_getc,
201 .setbrg = sh_serial_setbrg,
204 U_BOOT_DRIVER(serial_sh) = {
207 .probe = sh_serial_probe,
208 .ops = &sh_serial_ops,
209 .flags = DM_FLAG_PRE_RELOC,
210 .priv_auto_alloc_size = sizeof(struct uart_port),
213 #else /* CONFIG_DM_SERIAL */
215 #if defined(CONFIG_CONS_SCIF0)
216 # define SCIF_BASE SCIF0_BASE
217 #elif defined(CONFIG_CONS_SCIF1)
218 # define SCIF_BASE SCIF1_BASE
219 #elif defined(CONFIG_CONS_SCIF2)
220 # define SCIF_BASE SCIF2_BASE
221 #elif defined(CONFIG_CONS_SCIF3)
222 # define SCIF_BASE SCIF3_BASE
223 #elif defined(CONFIG_CONS_SCIF4)
224 # define SCIF_BASE SCIF4_BASE
225 #elif defined(CONFIG_CONS_SCIF5)
226 # define SCIF_BASE SCIF5_BASE
227 #elif defined(CONFIG_CONS_SCIF6)
228 # define SCIF_BASE SCIF6_BASE
229 #elif defined(CONFIG_CONS_SCIF7)
230 # define SCIF_BASE SCIF7_BASE
232 # error "Default SCIF doesn't set....."
235 #if defined(CONFIG_SCIF_A)
236 #define SCIF_BASE_PORT PORT_SCIFA
238 #define SCIF_BASE_PORT PORT_SCIF
241 static struct uart_port sh_sci = {
242 .membase = (unsigned char *)SCIF_BASE,
243 .mapbase = SCIF_BASE,
244 .type = SCIF_BASE_PORT,
245 #ifdef CONFIG_SCIF_USE_EXT_CLK
250 static void sh_serial_setbrg(void)
252 DECLARE_GLOBAL_DATA_PTR;
253 struct uart_port *port = &sh_sci;
255 sh_serial_setbrg_generic(port, CONFIG_SH_SCIF_CLK_FREQ, gd->baudrate);
258 static int sh_serial_init(void)
260 struct uart_port *port = &sh_sci;
262 sh_serial_init_generic(port);
268 static void sh_serial_putc(const char c)
270 struct uart_port *port = &sh_sci;
274 if (serial_raw_putc(port, '\r') != -EAGAIN)
279 if (serial_raw_putc(port, c) != -EAGAIN)
284 static int sh_serial_tstc(void)
286 struct uart_port *port = &sh_sci;
288 return sh_serial_tstc_generic(port);
291 static int sh_serial_getc(void)
293 struct uart_port *port = &sh_sci;
297 ch = sh_serial_getc_generic(port);
305 static struct serial_device sh_serial_drv = {
307 .start = sh_serial_init,
309 .setbrg = sh_serial_setbrg,
310 .putc = sh_serial_putc,
311 .puts = default_serial_puts,
312 .getc = sh_serial_getc,
313 .tstc = sh_serial_tstc,
316 void sh_serial_initialize(void)
318 serial_register(&sh_serial_drv);
321 __weak struct serial_device *default_serial_console(void)
323 return &sh_serial_drv;
325 #endif /* CONFIG_DM_SERIAL */