2 * SuperH SCIF device driver.
3 * Copyright (C) 2013 Renesas Electronics Corporation
4 * Copyright (C) 2007,2008,2010 Nobuhiro Iwamatsu
5 * Copyright (C) 2002 - 2008 Paul Mundt
7 * SPDX-License-Identifier: GPL-2.0+
12 #include <asm/processor.h>
13 #include "serial_sh.h"
15 #include <linux/compiler.h>
17 #if defined(CONFIG_CONS_SCIF0)
18 # define SCIF_BASE SCIF0_BASE
19 #elif defined(CONFIG_CONS_SCIF1)
20 # define SCIF_BASE SCIF1_BASE
21 #elif defined(CONFIG_CONS_SCIF2)
22 # define SCIF_BASE SCIF2_BASE
23 #elif defined(CONFIG_CONS_SCIF3)
24 # define SCIF_BASE SCIF3_BASE
25 #elif defined(CONFIG_CONS_SCIF4)
26 # define SCIF_BASE SCIF4_BASE
27 #elif defined(CONFIG_CONS_SCIF5)
28 # define SCIF_BASE SCIF5_BASE
29 #elif defined(CONFIG_CONS_SCIF6)
30 # define SCIF_BASE SCIF6_BASE
31 #elif defined(CONFIG_CONS_SCIF7)
32 # define SCIF_BASE SCIF7_BASE
34 # error "Default SCIF doesn't set....."
37 #if defined(CONFIG_SCIF_A)
38 #define SCIF_BASE_PORT PORT_SCIFA
40 #define SCIF_BASE_PORT PORT_SCIF
43 static struct uart_port sh_sci = {
44 .membase = (unsigned char*)SCIF_BASE,
46 .type = SCIF_BASE_PORT,
49 static void sh_serial_setbrg(void)
51 DECLARE_GLOBAL_DATA_PTR;
52 #ifdef CONFIG_SCIF_USE_EXT_CLK
53 unsigned short dl = DL_VALUE(gd->baudrate, CONFIG_SH_SCIF_CLK_FREQ);
54 sci_out(&sh_sci, DL, dl);
55 /* Need wait: Clock * 1/dl
\e$B!_
\e(B 1/16 */
56 udelay((1000000 * dl * 16 / CONFIG_SYS_CLK_FREQ) * 1000 + 1);
58 sci_out(&sh_sci, SCBRR,
59 SCBRR_VALUE(gd->baudrate, CONFIG_SH_SCIF_CLK_FREQ));
63 static int sh_serial_init(void)
65 sci_out(&sh_sci, SCSCR , SCSCR_INIT(&sh_sci));
66 sci_out(&sh_sci, SCSCR , SCSCR_INIT(&sh_sci));
67 sci_out(&sh_sci, SCSMR, 0);
68 sci_out(&sh_sci, SCSMR, 0);
69 sci_out(&sh_sci, SCFCR, SCFCR_RFRST|SCFCR_TFRST);
70 sci_in(&sh_sci, SCFCR);
71 sci_out(&sh_sci, SCFCR, 0);
77 #if defined(CONFIG_CPU_SH7760) || \
78 defined(CONFIG_CPU_SH7780) || \
79 defined(CONFIG_CPU_SH7785) || \
80 defined(CONFIG_CPU_SH7786)
81 static int scif_rxfill(struct uart_port *port)
83 return sci_in(port, SCRFDR) & 0xff;
85 #elif defined(CONFIG_CPU_SH7763)
86 static int scif_rxfill(struct uart_port *port)
88 if ((port->mapbase == 0xffe00000) ||
89 (port->mapbase == 0xffe08000)) {
91 return sci_in(port, SCRFDR) & 0xff;
94 return sci_in(port, SCFDR) & SCIF2_RFDC_MASK;
97 #elif defined(CONFIG_ARCH_SH7372)
98 static int scif_rxfill(struct uart_port *port)
100 if (port->type == PORT_SCIFA)
101 return sci_in(port, SCFDR) & SCIF_RFDC_MASK;
103 return sci_in(port, SCRFDR);
106 static int scif_rxfill(struct uart_port *port)
108 return sci_in(port, SCFDR) & SCIF_RFDC_MASK;
112 static int serial_rx_fifo_level(void)
114 return scif_rxfill(&sh_sci);
117 static void handle_error(void)
119 sci_in(&sh_sci, SCxSR);
120 sci_out(&sh_sci, SCxSR, SCxSR_ERROR_CLEAR(&sh_sci));
121 sci_in(&sh_sci, SCLSR);
122 sci_out(&sh_sci, SCLSR, 0x00);
125 static void serial_raw_putc(const char c)
128 /* Tx fifo is empty */
129 if (sci_in(&sh_sci, SCxSR) & SCxSR_TEND(&sh_sci))
133 sci_out(&sh_sci, SCxTDR, c);
134 sci_out(&sh_sci, SCxSR, sci_in(&sh_sci, SCxSR) & ~SCxSR_TEND(&sh_sci));
137 static void sh_serial_putc(const char c)
140 serial_raw_putc('\r');
144 static int sh_serial_tstc(void)
146 if (sci_in(&sh_sci, SCxSR) & SCIF_ERRORS) {
151 return serial_rx_fifo_level() ? 1 : 0;
155 static int serial_getc_check(void)
157 unsigned short status;
159 status = sci_in(&sh_sci, SCxSR);
161 if (status & SCIF_ERRORS)
163 if (sci_in(&sh_sci, SCLSR) & SCxSR_ORER(&sh_sci))
165 return status & (SCIF_DR | SCxSR_RDxF(&sh_sci));
168 static int sh_serial_getc(void)
170 unsigned short status;
173 while (!serial_getc_check())
176 ch = sci_in(&sh_sci, SCxRDR);
177 status = sci_in(&sh_sci, SCxSR);
179 sci_out(&sh_sci, SCxSR, SCxSR_RDxF_CLEAR(&sh_sci));
181 if (status & SCIF_ERRORS)
184 if (sci_in(&sh_sci, SCLSR) & SCxSR_ORER(&sh_sci))
189 static struct serial_device sh_serial_drv = {
191 .start = sh_serial_init,
193 .setbrg = sh_serial_setbrg,
194 .putc = sh_serial_putc,
195 .puts = default_serial_puts,
196 .getc = sh_serial_getc,
197 .tstc = sh_serial_tstc,
200 void sh_serial_initialize(void)
202 serial_register(&sh_serial_drv);
205 __weak struct serial_device *default_serial_console(void)
207 return &sh_serial_drv;