2 * SuperH on-chip serial module support. (SCI with no FIFO / with FIFO)
4 * Copyright (C) 2002 - 2011 Paul Mundt
5 * Copyright (C) 2015 Glider bvba
6 * Modified to support SH7720 SCIF. Markus Brunner, Mark Jonas (Jul 2007).
8 * based off of the old drivers/char/sh-sci.c by:
10 * Copyright (C) 1999, 2000 Niibe Yutaka
11 * Copyright (C) 2000 Sugioka Toshinobu
12 * Modified to support multiple serial ports. Stuart Menefy (May 2000).
13 * Modified to support SecureEdge. David McCullough (2002)
14 * Modified to support SH7300 SCIF. Takashi Kusuda (Jun 2003).
15 * Removed SH7300 support (Jul 2007).
17 * This file is subject to the terms and conditions of the GNU General Public
18 * License. See the file "COPYING" in the main directory of this archive
21 #if defined(CONFIG_SERIAL_SH_SCI_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
27 #include <linux/clk.h>
28 #include <linux/console.h>
29 #include <linux/ctype.h>
30 #include <linux/cpufreq.h>
31 #include <linux/delay.h>
32 #include <linux/dmaengine.h>
33 #include <linux/dma-mapping.h>
34 #include <linux/err.h>
35 #include <linux/errno.h>
36 #include <linux/init.h>
37 #include <linux/interrupt.h>
38 #include <linux/ioport.h>
39 #include <linux/major.h>
40 #include <linux/module.h>
43 #include <linux/platform_device.h>
44 #include <linux/pm_runtime.h>
45 #include <linux/scatterlist.h>
46 #include <linux/serial.h>
47 #include <linux/serial_sci.h>
48 #include <linux/sh_dma.h>
49 #include <linux/slab.h>
50 #include <linux/string.h>
51 #include <linux/sysrq.h>
52 #include <linux/timer.h>
53 #include <linux/tty.h>
54 #include <linux/tty_flip.h>
57 #include <asm/sh_bios.h>
60 #include "serial_mctrl_gpio.h"
63 /* Offsets into the sci_port->irqs array */
71 SCIx_MUX_IRQ = SCIx_NR_IRQS, /* special case */
74 #define SCIx_IRQ_IS_MUXED(port) \
75 ((port)->irqs[SCIx_ERI_IRQ] == \
76 (port)->irqs[SCIx_RXI_IRQ]) || \
77 ((port)->irqs[SCIx_ERI_IRQ] && \
78 ((port)->irqs[SCIx_RXI_IRQ] < 0))
81 SCI_FCK, /* Functional Clock */
82 SCI_SCK, /* Optional External Clock */
83 SCI_BRG_INT, /* Optional BRG Internal Clock Source */
84 SCI_SCIF_CLK, /* Optional BRG External Clock Source */
88 /* Bit x set means sampling rate x + 1 is supported */
89 #define SCI_SR(x) BIT((x) - 1)
90 #define SCI_SR_RANGE(x, y) GENMASK((y) - 1, (x) - 1)
92 #define SCI_SR_SCIFAB SCI_SR(5) | SCI_SR(7) | SCI_SR(11) | \
93 SCI_SR(13) | SCI_SR(16) | SCI_SR(17) | \
94 SCI_SR(19) | SCI_SR(27)
96 #define min_sr(_port) ffs((_port)->sampling_rate_mask)
97 #define max_sr(_port) fls((_port)->sampling_rate_mask)
99 /* Iterate over all supported sampling rates, from high to low */
100 #define for_each_sr(_sr, _port) \
101 for ((_sr) = max_sr(_port); (_sr) >= min_sr(_port); (_sr)--) \
102 if ((_port)->sampling_rate_mask & SCI_SR((_sr)))
105 struct uart_port port;
107 /* Platform configuration */
108 struct plat_sci_port *cfg;
109 unsigned int overrun_reg;
110 unsigned int overrun_mask;
111 unsigned int error_mask;
112 unsigned int error_clear;
113 unsigned int sampling_rate_mask;
114 resource_size_t reg_size;
115 struct mctrl_gpios *gpios;
118 struct timer_list break_timer;
122 struct clk *clks[SCI_NUM_CLKS];
123 unsigned long clk_rates[SCI_NUM_CLKS];
125 int irqs[SCIx_NR_IRQS];
126 char *irqstr[SCIx_NR_IRQS];
128 struct dma_chan *chan_tx;
129 struct dma_chan *chan_rx;
131 #ifdef CONFIG_SERIAL_SH_SCI_DMA
132 dma_cookie_t cookie_tx;
133 dma_cookie_t cookie_rx[2];
134 dma_cookie_t active_rx;
135 dma_addr_t tx_dma_addr;
136 unsigned int tx_dma_len;
137 struct scatterlist sg_rx[2];
140 struct work_struct work_tx;
141 struct timer_list rx_timer;
142 unsigned int rx_timeout;
148 #define SCI_NPORTS CONFIG_SERIAL_SH_SCI_NR_UARTS
150 static struct sci_port sci_ports[SCI_NPORTS];
151 static struct uart_driver sci_uart_driver;
153 static inline struct sci_port *
154 to_sci_port(struct uart_port *uart)
156 return container_of(uart, struct sci_port, port);
159 struct plat_sci_reg {
163 /* Helper for invalidating specific entries of an inherited map. */
164 #define sci_reg_invalid { .offset = 0, .size = 0 }
166 static const struct plat_sci_reg sci_regmap[SCIx_NR_REGTYPES][SCIx_NR_REGS] = {
167 [SCIx_PROBE_REGTYPE] = {
168 [0 ... SCIx_NR_REGS - 1] = sci_reg_invalid,
172 * Common SCI definitions, dependent on the port's regshift
175 [SCIx_SCI_REGTYPE] = {
176 [SCSMR] = { 0x00, 8 },
177 [SCBRR] = { 0x01, 8 },
178 [SCSCR] = { 0x02, 8 },
179 [SCxTDR] = { 0x03, 8 },
180 [SCxSR] = { 0x04, 8 },
181 [SCxRDR] = { 0x05, 8 },
182 [SCFCR] = sci_reg_invalid,
183 [SCFDR] = sci_reg_invalid,
184 [SCTFDR] = sci_reg_invalid,
185 [SCRFDR] = sci_reg_invalid,
186 [SCSPTR] = sci_reg_invalid,
187 [SCLSR] = sci_reg_invalid,
188 [HSSRR] = sci_reg_invalid,
189 [SCPCR] = sci_reg_invalid,
190 [SCPDR] = sci_reg_invalid,
191 [SCDL] = sci_reg_invalid,
192 [SCCKS] = sci_reg_invalid,
196 * Common definitions for legacy IrDA ports.
198 [SCIx_IRDA_REGTYPE] = {
199 [SCSMR] = { 0x00, 8 },
200 [SCBRR] = { 0x02, 8 },
201 [SCSCR] = { 0x04, 8 },
202 [SCxTDR] = { 0x06, 8 },
203 [SCxSR] = { 0x08, 16 },
204 [SCxRDR] = { 0x0a, 8 },
205 [SCFCR] = { 0x0c, 8 },
206 [SCFDR] = { 0x0e, 16 },
207 [SCTFDR] = sci_reg_invalid,
208 [SCRFDR] = sci_reg_invalid,
209 [SCSPTR] = sci_reg_invalid,
210 [SCLSR] = sci_reg_invalid,
211 [HSSRR] = sci_reg_invalid,
212 [SCPCR] = sci_reg_invalid,
213 [SCPDR] = sci_reg_invalid,
214 [SCDL] = sci_reg_invalid,
215 [SCCKS] = sci_reg_invalid,
219 * Common SCIFA definitions.
221 [SCIx_SCIFA_REGTYPE] = {
222 [SCSMR] = { 0x00, 16 },
223 [SCBRR] = { 0x04, 8 },
224 [SCSCR] = { 0x08, 16 },
225 [SCxTDR] = { 0x20, 8 },
226 [SCxSR] = { 0x14, 16 },
227 [SCxRDR] = { 0x24, 8 },
228 [SCFCR] = { 0x18, 16 },
229 [SCFDR] = { 0x1c, 16 },
230 [SCTFDR] = sci_reg_invalid,
231 [SCRFDR] = sci_reg_invalid,
232 [SCSPTR] = sci_reg_invalid,
233 [SCLSR] = sci_reg_invalid,
234 [HSSRR] = sci_reg_invalid,
235 [SCPCR] = { 0x30, 16 },
236 [SCPDR] = { 0x34, 16 },
237 [SCDL] = sci_reg_invalid,
238 [SCCKS] = sci_reg_invalid,
242 * Common SCIFB definitions.
244 [SCIx_SCIFB_REGTYPE] = {
245 [SCSMR] = { 0x00, 16 },
246 [SCBRR] = { 0x04, 8 },
247 [SCSCR] = { 0x08, 16 },
248 [SCxTDR] = { 0x40, 8 },
249 [SCxSR] = { 0x14, 16 },
250 [SCxRDR] = { 0x60, 8 },
251 [SCFCR] = { 0x18, 16 },
252 [SCFDR] = sci_reg_invalid,
253 [SCTFDR] = { 0x38, 16 },
254 [SCRFDR] = { 0x3c, 16 },
255 [SCSPTR] = sci_reg_invalid,
256 [SCLSR] = sci_reg_invalid,
257 [HSSRR] = sci_reg_invalid,
258 [SCPCR] = { 0x30, 16 },
259 [SCPDR] = { 0x34, 16 },
260 [SCDL] = sci_reg_invalid,
261 [SCCKS] = sci_reg_invalid,
265 * Common SH-2(A) SCIF definitions for ports with FIFO data
268 [SCIx_SH2_SCIF_FIFODATA_REGTYPE] = {
269 [SCSMR] = { 0x00, 16 },
270 [SCBRR] = { 0x04, 8 },
271 [SCSCR] = { 0x08, 16 },
272 [SCxTDR] = { 0x0c, 8 },
273 [SCxSR] = { 0x10, 16 },
274 [SCxRDR] = { 0x14, 8 },
275 [SCFCR] = { 0x18, 16 },
276 [SCFDR] = { 0x1c, 16 },
277 [SCTFDR] = sci_reg_invalid,
278 [SCRFDR] = sci_reg_invalid,
279 [SCSPTR] = { 0x20, 16 },
280 [SCLSR] = { 0x24, 16 },
281 [HSSRR] = sci_reg_invalid,
282 [SCPCR] = sci_reg_invalid,
283 [SCPDR] = sci_reg_invalid,
284 [SCDL] = sci_reg_invalid,
285 [SCCKS] = sci_reg_invalid,
289 * Common SH-3 SCIF definitions.
291 [SCIx_SH3_SCIF_REGTYPE] = {
292 [SCSMR] = { 0x00, 8 },
293 [SCBRR] = { 0x02, 8 },
294 [SCSCR] = { 0x04, 8 },
295 [SCxTDR] = { 0x06, 8 },
296 [SCxSR] = { 0x08, 16 },
297 [SCxRDR] = { 0x0a, 8 },
298 [SCFCR] = { 0x0c, 8 },
299 [SCFDR] = { 0x0e, 16 },
300 [SCTFDR] = sci_reg_invalid,
301 [SCRFDR] = sci_reg_invalid,
302 [SCSPTR] = sci_reg_invalid,
303 [SCLSR] = sci_reg_invalid,
304 [HSSRR] = sci_reg_invalid,
305 [SCPCR] = sci_reg_invalid,
306 [SCPDR] = sci_reg_invalid,
307 [SCDL] = sci_reg_invalid,
308 [SCCKS] = sci_reg_invalid,
312 * Common SH-4(A) SCIF(B) definitions.
314 [SCIx_SH4_SCIF_REGTYPE] = {
315 [SCSMR] = { 0x00, 16 },
316 [SCBRR] = { 0x04, 8 },
317 [SCSCR] = { 0x08, 16 },
318 [SCxTDR] = { 0x0c, 8 },
319 [SCxSR] = { 0x10, 16 },
320 [SCxRDR] = { 0x14, 8 },
321 [SCFCR] = { 0x18, 16 },
322 [SCFDR] = { 0x1c, 16 },
323 [SCTFDR] = sci_reg_invalid,
324 [SCRFDR] = sci_reg_invalid,
325 [SCSPTR] = { 0x20, 16 },
326 [SCLSR] = { 0x24, 16 },
327 [HSSRR] = sci_reg_invalid,
328 [SCPCR] = sci_reg_invalid,
329 [SCPDR] = sci_reg_invalid,
330 [SCDL] = sci_reg_invalid,
331 [SCCKS] = sci_reg_invalid,
335 * Common SCIF definitions for ports with a Baud Rate Generator for
336 * External Clock (BRG).
338 [SCIx_SH4_SCIF_BRG_REGTYPE] = {
339 [SCSMR] = { 0x00, 16 },
340 [SCBRR] = { 0x04, 8 },
341 [SCSCR] = { 0x08, 16 },
342 [SCxTDR] = { 0x0c, 8 },
343 [SCxSR] = { 0x10, 16 },
344 [SCxRDR] = { 0x14, 8 },
345 [SCFCR] = { 0x18, 16 },
346 [SCFDR] = { 0x1c, 16 },
347 [SCTFDR] = sci_reg_invalid,
348 [SCRFDR] = sci_reg_invalid,
349 [SCSPTR] = { 0x20, 16 },
350 [SCLSR] = { 0x24, 16 },
351 [HSSRR] = sci_reg_invalid,
352 [SCPCR] = sci_reg_invalid,
353 [SCPDR] = sci_reg_invalid,
354 [SCDL] = { 0x30, 16 },
355 [SCCKS] = { 0x34, 16 },
359 * Common HSCIF definitions.
361 [SCIx_HSCIF_REGTYPE] = {
362 [SCSMR] = { 0x00, 16 },
363 [SCBRR] = { 0x04, 8 },
364 [SCSCR] = { 0x08, 16 },
365 [SCxTDR] = { 0x0c, 8 },
366 [SCxSR] = { 0x10, 16 },
367 [SCxRDR] = { 0x14, 8 },
368 [SCFCR] = { 0x18, 16 },
369 [SCFDR] = { 0x1c, 16 },
370 [SCTFDR] = sci_reg_invalid,
371 [SCRFDR] = sci_reg_invalid,
372 [SCSPTR] = { 0x20, 16 },
373 [SCLSR] = { 0x24, 16 },
374 [HSSRR] = { 0x40, 16 },
375 [SCPCR] = sci_reg_invalid,
376 [SCPDR] = sci_reg_invalid,
377 [SCDL] = { 0x30, 16 },
378 [SCCKS] = { 0x34, 16 },
382 * Common SH-4(A) SCIF(B) definitions for ports without an SCSPTR
385 [SCIx_SH4_SCIF_NO_SCSPTR_REGTYPE] = {
386 [SCSMR] = { 0x00, 16 },
387 [SCBRR] = { 0x04, 8 },
388 [SCSCR] = { 0x08, 16 },
389 [SCxTDR] = { 0x0c, 8 },
390 [SCxSR] = { 0x10, 16 },
391 [SCxRDR] = { 0x14, 8 },
392 [SCFCR] = { 0x18, 16 },
393 [SCFDR] = { 0x1c, 16 },
394 [SCTFDR] = sci_reg_invalid,
395 [SCRFDR] = sci_reg_invalid,
396 [SCSPTR] = sci_reg_invalid,
397 [SCLSR] = { 0x24, 16 },
398 [HSSRR] = sci_reg_invalid,
399 [SCPCR] = sci_reg_invalid,
400 [SCPDR] = sci_reg_invalid,
401 [SCDL] = sci_reg_invalid,
402 [SCCKS] = sci_reg_invalid,
406 * Common SH-4(A) SCIF(B) definitions for ports with FIFO data
409 [SCIx_SH4_SCIF_FIFODATA_REGTYPE] = {
410 [SCSMR] = { 0x00, 16 },
411 [SCBRR] = { 0x04, 8 },
412 [SCSCR] = { 0x08, 16 },
413 [SCxTDR] = { 0x0c, 8 },
414 [SCxSR] = { 0x10, 16 },
415 [SCxRDR] = { 0x14, 8 },
416 [SCFCR] = { 0x18, 16 },
417 [SCFDR] = { 0x1c, 16 },
418 [SCTFDR] = { 0x1c, 16 }, /* aliased to SCFDR */
419 [SCRFDR] = { 0x20, 16 },
420 [SCSPTR] = { 0x24, 16 },
421 [SCLSR] = { 0x28, 16 },
422 [HSSRR] = sci_reg_invalid,
423 [SCPCR] = sci_reg_invalid,
424 [SCPDR] = sci_reg_invalid,
425 [SCDL] = sci_reg_invalid,
426 [SCCKS] = sci_reg_invalid,
430 * SH7705-style SCIF(B) ports, lacking both SCSPTR and SCLSR
433 [SCIx_SH7705_SCIF_REGTYPE] = {
434 [SCSMR] = { 0x00, 16 },
435 [SCBRR] = { 0x04, 8 },
436 [SCSCR] = { 0x08, 16 },
437 [SCxTDR] = { 0x20, 8 },
438 [SCxSR] = { 0x14, 16 },
439 [SCxRDR] = { 0x24, 8 },
440 [SCFCR] = { 0x18, 16 },
441 [SCFDR] = { 0x1c, 16 },
442 [SCTFDR] = sci_reg_invalid,
443 [SCRFDR] = sci_reg_invalid,
444 [SCSPTR] = sci_reg_invalid,
445 [SCLSR] = sci_reg_invalid,
446 [HSSRR] = sci_reg_invalid,
447 [SCPCR] = sci_reg_invalid,
448 [SCPDR] = sci_reg_invalid,
449 [SCDL] = sci_reg_invalid,
450 [SCCKS] = sci_reg_invalid,
454 #define sci_getreg(up, offset) (sci_regmap[to_sci_port(up)->cfg->regtype] + offset)
457 * The "offset" here is rather misleading, in that it refers to an enum
458 * value relative to the port mapping rather than the fixed offset
459 * itself, which needs to be manually retrieved from the platform's
460 * register map for the given port.
462 static unsigned int sci_serial_in(struct uart_port *p, int offset)
464 const struct plat_sci_reg *reg = sci_getreg(p, offset);
467 return ioread8(p->membase + (reg->offset << p->regshift));
468 else if (reg->size == 16)
469 return ioread16(p->membase + (reg->offset << p->regshift));
471 WARN(1, "Invalid register access\n");
476 static void sci_serial_out(struct uart_port *p, int offset, int value)
478 const struct plat_sci_reg *reg = sci_getreg(p, offset);
481 iowrite8(value, p->membase + (reg->offset << p->regshift));
482 else if (reg->size == 16)
483 iowrite16(value, p->membase + (reg->offset << p->regshift));
485 WARN(1, "Invalid register access\n");
488 static int sci_probe_regmap(struct plat_sci_port *cfg)
492 cfg->regtype = SCIx_SCI_REGTYPE;
495 cfg->regtype = SCIx_IRDA_REGTYPE;
498 cfg->regtype = SCIx_SCIFA_REGTYPE;
501 cfg->regtype = SCIx_SCIFB_REGTYPE;
505 * The SH-4 is a bit of a misnomer here, although that's
506 * where this particular port layout originated. This
507 * configuration (or some slight variation thereof)
508 * remains the dominant model for all SCIFs.
510 cfg->regtype = SCIx_SH4_SCIF_REGTYPE;
513 cfg->regtype = SCIx_HSCIF_REGTYPE;
516 pr_err("Can't probe register map for given port\n");
523 static void sci_port_enable(struct sci_port *sci_port)
527 if (!sci_port->port.dev)
530 pm_runtime_get_sync(sci_port->port.dev);
532 for (i = 0; i < SCI_NUM_CLKS; i++) {
533 clk_prepare_enable(sci_port->clks[i]);
534 sci_port->clk_rates[i] = clk_get_rate(sci_port->clks[i]);
536 sci_port->port.uartclk = sci_port->clk_rates[SCI_FCK];
539 static void sci_port_disable(struct sci_port *sci_port)
543 if (!sci_port->port.dev)
546 /* Cancel the break timer to ensure that the timer handler will not try
547 * to access the hardware with clocks and power disabled. Reset the
548 * break flag to make the break debouncing state machine ready for the
551 del_timer_sync(&sci_port->break_timer);
552 sci_port->break_flag = 0;
554 for (i = SCI_NUM_CLKS; i-- > 0; )
555 clk_disable_unprepare(sci_port->clks[i]);
557 pm_runtime_put_sync(sci_port->port.dev);
560 static inline unsigned long port_rx_irq_mask(struct uart_port *port)
563 * Not all ports (such as SCIFA) will support REIE. Rather than
564 * special-casing the port type, we check the port initialization
565 * IRQ enable mask to see whether the IRQ is desired at all. If
566 * it's unset, it's logically inferred that there's no point in
569 return SCSCR_RIE | (to_sci_port(port)->cfg->scscr & SCSCR_REIE);
572 static void sci_start_tx(struct uart_port *port)
574 struct sci_port *s = to_sci_port(port);
577 #ifdef CONFIG_SERIAL_SH_SCI_DMA
578 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB) {
579 u16 new, scr = serial_port_in(port, SCSCR);
581 new = scr | SCSCR_TDRQE;
583 new = scr & ~SCSCR_TDRQE;
585 serial_port_out(port, SCSCR, new);
588 if (s->chan_tx && !uart_circ_empty(&s->port.state->xmit) &&
589 dma_submit_error(s->cookie_tx)) {
591 schedule_work(&s->work_tx);
595 if (!s->chan_tx || port->type == PORT_SCIFA || port->type == PORT_SCIFB) {
596 /* Set TIE (Transmit Interrupt Enable) bit in SCSCR */
597 ctrl = serial_port_in(port, SCSCR);
598 serial_port_out(port, SCSCR, ctrl | SCSCR_TIE);
602 static void sci_stop_tx(struct uart_port *port)
606 /* Clear TIE (Transmit Interrupt Enable) bit in SCSCR */
607 ctrl = serial_port_in(port, SCSCR);
609 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB)
610 ctrl &= ~SCSCR_TDRQE;
614 serial_port_out(port, SCSCR, ctrl);
616 #ifdef CONFIG_SERIAL_SH_SCI_DMA
617 if (to_sci_port(port)->chan_tx &&
618 !dma_submit_error(to_sci_port(port)->cookie_tx)) {
619 dmaengine_terminate_async(to_sci_port(port)->chan_tx);
620 to_sci_port(port)->cookie_tx = -EINVAL;
625 static void sci_start_rx(struct uart_port *port)
629 ctrl = serial_port_in(port, SCSCR) | port_rx_irq_mask(port);
631 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB)
632 ctrl &= ~SCSCR_RDRQE;
634 serial_port_out(port, SCSCR, ctrl);
637 static void sci_stop_rx(struct uart_port *port)
641 ctrl = serial_port_in(port, SCSCR);
643 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB)
644 ctrl &= ~SCSCR_RDRQE;
646 ctrl &= ~port_rx_irq_mask(port);
648 serial_port_out(port, SCSCR, ctrl);
651 static void sci_clear_SCxSR(struct uart_port *port, unsigned int mask)
653 if (port->type == PORT_SCI) {
654 /* Just store the mask */
655 serial_port_out(port, SCxSR, mask);
656 } else if (to_sci_port(port)->overrun_mask == SCIFA_ORER) {
657 /* SCIFA/SCIFB and SCIF on SH7705/SH7720/SH7721 */
658 /* Only clear the status bits we want to clear */
659 serial_port_out(port, SCxSR,
660 serial_port_in(port, SCxSR) & mask);
662 /* Store the mask, clear parity/framing errors */
663 serial_port_out(port, SCxSR, mask & ~(SCIF_FERC | SCIF_PERC));
667 #if defined(CONFIG_CONSOLE_POLL) || defined(CONFIG_SERIAL_SH_SCI_CONSOLE) || \
668 defined(CONFIG_SERIAL_SH_SCI_EARLYCON)
670 #ifdef CONFIG_CONSOLE_POLL
671 static int sci_poll_get_char(struct uart_port *port)
673 unsigned short status;
677 status = serial_port_in(port, SCxSR);
678 if (status & SCxSR_ERRORS(port)) {
679 sci_clear_SCxSR(port, SCxSR_ERROR_CLEAR(port));
685 if (!(status & SCxSR_RDxF(port)))
688 c = serial_port_in(port, SCxRDR);
691 serial_port_in(port, SCxSR);
692 sci_clear_SCxSR(port, SCxSR_RDxF_CLEAR(port));
698 static void sci_poll_put_char(struct uart_port *port, unsigned char c)
700 unsigned short status;
703 status = serial_port_in(port, SCxSR);
704 } while (!(status & SCxSR_TDxE(port)));
706 serial_port_out(port, SCxTDR, c);
707 sci_clear_SCxSR(port, SCxSR_TDxE_CLEAR(port) & ~SCxSR_TEND(port));
709 #endif /* CONFIG_CONSOLE_POLL || CONFIG_SERIAL_SH_SCI_CONSOLE ||
710 CONFIG_SERIAL_SH_SCI_EARLYCON */
712 static void sci_init_pins(struct uart_port *port, unsigned int cflag)
714 struct sci_port *s = to_sci_port(port);
717 * Use port-specific handler if provided.
719 if (s->cfg->ops && s->cfg->ops->init_pins) {
720 s->cfg->ops->init_pins(port, cflag);
724 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB) {
725 u16 ctrl = serial_port_in(port, SCPCR);
727 /* Enable RXD and TXD pin functions */
728 ctrl &= ~(SCPCR_RXDC | SCPCR_TXDC);
729 if (to_sci_port(port)->cfg->capabilities & SCIx_HAVE_RTSCTS) {
730 /* RTS# is output, driven 1 */
732 serial_port_out(port, SCPDR,
733 serial_port_in(port, SCPDR) | SCPDR_RTSD);
734 /* Enable CTS# pin function */
737 serial_port_out(port, SCPCR, ctrl);
738 } else if (sci_getreg(port, SCSPTR)->size) {
739 u16 status = serial_port_in(port, SCSPTR);
741 /* RTS# is output, driven 1 */
742 status |= SCSPTR_RTSIO | SCSPTR_RTSDT;
743 /* CTS# and SCK are inputs */
744 status &= ~(SCSPTR_CTSIO | SCSPTR_SCKIO);
745 serial_port_out(port, SCSPTR, status);
749 static int sci_txfill(struct uart_port *port)
751 const struct plat_sci_reg *reg;
753 reg = sci_getreg(port, SCTFDR);
755 return serial_port_in(port, SCTFDR) & ((port->fifosize << 1) - 1);
757 reg = sci_getreg(port, SCFDR);
759 return serial_port_in(port, SCFDR) >> 8;
761 return !(serial_port_in(port, SCxSR) & SCI_TDRE);
764 static int sci_txroom(struct uart_port *port)
766 return port->fifosize - sci_txfill(port);
769 static int sci_rxfill(struct uart_port *port)
771 const struct plat_sci_reg *reg;
773 reg = sci_getreg(port, SCRFDR);
775 return serial_port_in(port, SCRFDR) & ((port->fifosize << 1) - 1);
777 reg = sci_getreg(port, SCFDR);
779 return serial_port_in(port, SCFDR) & ((port->fifosize << 1) - 1);
781 return (serial_port_in(port, SCxSR) & SCxSR_RDxF(port)) != 0;
785 * SCI helper for checking the state of the muxed port/RXD pins.
787 static inline int sci_rxd_in(struct uart_port *port)
789 struct sci_port *s = to_sci_port(port);
791 if (s->cfg->port_reg <= 0)
794 /* Cast for ARM damage */
795 return !!__raw_readb((void __iomem *)(uintptr_t)s->cfg->port_reg);
798 /* ********************************************************************** *
799 * the interrupt related routines *
800 * ********************************************************************** */
802 static void sci_transmit_chars(struct uart_port *port)
804 struct circ_buf *xmit = &port->state->xmit;
805 unsigned int stopped = uart_tx_stopped(port);
806 unsigned short status;
810 status = serial_port_in(port, SCxSR);
811 if (!(status & SCxSR_TDxE(port))) {
812 ctrl = serial_port_in(port, SCSCR);
813 if (uart_circ_empty(xmit))
817 serial_port_out(port, SCSCR, ctrl);
821 count = sci_txroom(port);
829 } else if (!uart_circ_empty(xmit) && !stopped) {
830 c = xmit->buf[xmit->tail];
831 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
836 serial_port_out(port, SCxTDR, c);
839 } while (--count > 0);
841 sci_clear_SCxSR(port, SCxSR_TDxE_CLEAR(port));
843 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
844 uart_write_wakeup(port);
845 if (uart_circ_empty(xmit))
850 /* On SH3, SCIF may read end-of-break as a space->mark char */
851 #define STEPFN(c) ({int __c = (c); (((__c-1)|(__c)) == -1); })
853 static void sci_receive_chars(struct uart_port *port)
855 struct sci_port *sci_port = to_sci_port(port);
856 struct tty_port *tport = &port->state->port;
857 int i, count, copied = 0;
858 unsigned short status;
861 status = serial_port_in(port, SCxSR);
862 if (!(status & SCxSR_RDxF(port)))
866 /* Don't copy more bytes than there is room for in the buffer */
867 count = tty_buffer_request_room(tport, sci_rxfill(port));
869 /* If for any reason we can't copy more data, we're done! */
873 if (port->type == PORT_SCI) {
874 char c = serial_port_in(port, SCxRDR);
875 if (uart_handle_sysrq_char(port, c) ||
876 sci_port->break_flag)
879 tty_insert_flip_char(tport, c, TTY_NORMAL);
881 for (i = 0; i < count; i++) {
882 char c = serial_port_in(port, SCxRDR);
884 status = serial_port_in(port, SCxSR);
885 #if defined(CONFIG_CPU_SH3)
886 /* Skip "chars" during break */
887 if (sci_port->break_flag) {
889 (status & SCxSR_FER(port))) {
894 /* Nonzero => end-of-break */
895 dev_dbg(port->dev, "debounce<%02x>\n", c);
896 sci_port->break_flag = 0;
903 #endif /* CONFIG_CPU_SH3 */
904 if (uart_handle_sysrq_char(port, c)) {
909 /* Store data and status */
910 if (status & SCxSR_FER(port)) {
912 port->icount.frame++;
913 dev_notice(port->dev, "frame error\n");
914 } else if (status & SCxSR_PER(port)) {
916 port->icount.parity++;
917 dev_notice(port->dev, "parity error\n");
921 tty_insert_flip_char(tport, c, flag);
925 serial_port_in(port, SCxSR); /* dummy read */
926 sci_clear_SCxSR(port, SCxSR_RDxF_CLEAR(port));
929 port->icount.rx += count;
933 /* Tell the rest of the system the news. New characters! */
934 tty_flip_buffer_push(tport);
936 /* TTY buffers full; read from RX reg to prevent lockup */
937 serial_port_in(port, SCxRDR);
938 serial_port_in(port, SCxSR); /* dummy read */
939 sci_clear_SCxSR(port, SCxSR_RDxF_CLEAR(port));
943 #define SCI_BREAK_JIFFIES (HZ/20)
946 * The sci generates interrupts during the break,
947 * 1 per millisecond or so during the break period, for 9600 baud.
948 * So dont bother disabling interrupts.
949 * But dont want more than 1 break event.
950 * Use a kernel timer to periodically poll the rx line until
951 * the break is finished.
953 static inline void sci_schedule_break_timer(struct sci_port *port)
955 mod_timer(&port->break_timer, jiffies + SCI_BREAK_JIFFIES);
958 /* Ensure that two consecutive samples find the break over. */
959 static void sci_break_timer(unsigned long data)
961 struct sci_port *port = (struct sci_port *)data;
963 if (sci_rxd_in(&port->port) == 0) {
964 port->break_flag = 1;
965 sci_schedule_break_timer(port);
966 } else if (port->break_flag == 1) {
968 port->break_flag = 2;
969 sci_schedule_break_timer(port);
971 port->break_flag = 0;
974 static int sci_handle_errors(struct uart_port *port)
977 unsigned short status = serial_port_in(port, SCxSR);
978 struct tty_port *tport = &port->state->port;
979 struct sci_port *s = to_sci_port(port);
981 /* Handle overruns */
982 if (status & s->overrun_mask) {
983 port->icount.overrun++;
986 if (tty_insert_flip_char(tport, 0, TTY_OVERRUN))
989 dev_notice(port->dev, "overrun error\n");
992 if (status & SCxSR_FER(port)) {
993 if (sci_rxd_in(port) == 0) {
994 /* Notify of BREAK */
995 struct sci_port *sci_port = to_sci_port(port);
997 if (!sci_port->break_flag) {
1000 sci_port->break_flag = 1;
1001 sci_schedule_break_timer(sci_port);
1003 /* Do sysrq handling. */
1004 if (uart_handle_break(port))
1007 dev_dbg(port->dev, "BREAK detected\n");
1009 if (tty_insert_flip_char(tport, 0, TTY_BREAK))
1015 port->icount.frame++;
1017 if (tty_insert_flip_char(tport, 0, TTY_FRAME))
1020 dev_notice(port->dev, "frame error\n");
1024 if (status & SCxSR_PER(port)) {
1026 port->icount.parity++;
1028 if (tty_insert_flip_char(tport, 0, TTY_PARITY))
1031 dev_notice(port->dev, "parity error\n");
1035 tty_flip_buffer_push(tport);
1040 static int sci_handle_fifo_overrun(struct uart_port *port)
1042 struct tty_port *tport = &port->state->port;
1043 struct sci_port *s = to_sci_port(port);
1044 const struct plat_sci_reg *reg;
1048 reg = sci_getreg(port, s->overrun_reg);
1052 status = serial_port_in(port, s->overrun_reg);
1053 if (status & s->overrun_mask) {
1054 status &= ~s->overrun_mask;
1055 serial_port_out(port, s->overrun_reg, status);
1057 port->icount.overrun++;
1059 tty_insert_flip_char(tport, 0, TTY_OVERRUN);
1060 tty_flip_buffer_push(tport);
1062 dev_dbg(port->dev, "overrun error\n");
1069 static int sci_handle_breaks(struct uart_port *port)
1072 unsigned short status = serial_port_in(port, SCxSR);
1073 struct tty_port *tport = &port->state->port;
1074 struct sci_port *s = to_sci_port(port);
1076 if (uart_handle_break(port))
1079 if (!s->break_flag && status & SCxSR_BRK(port)) {
1080 #if defined(CONFIG_CPU_SH3)
1081 /* Debounce break */
1087 /* Notify of BREAK */
1088 if (tty_insert_flip_char(tport, 0, TTY_BREAK))
1091 dev_dbg(port->dev, "BREAK detected\n");
1095 tty_flip_buffer_push(tport);
1097 copied += sci_handle_fifo_overrun(port);
1102 #ifdef CONFIG_SERIAL_SH_SCI_DMA
1103 static void sci_dma_tx_complete(void *arg)
1105 struct sci_port *s = arg;
1106 struct uart_port *port = &s->port;
1107 struct circ_buf *xmit = &port->state->xmit;
1108 unsigned long flags;
1110 dev_dbg(port->dev, "%s(%d)\n", __func__, port->line);
1112 spin_lock_irqsave(&port->lock, flags);
1114 xmit->tail += s->tx_dma_len;
1115 xmit->tail &= UART_XMIT_SIZE - 1;
1117 port->icount.tx += s->tx_dma_len;
1119 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
1120 uart_write_wakeup(port);
1122 if (!uart_circ_empty(xmit)) {
1124 schedule_work(&s->work_tx);
1126 s->cookie_tx = -EINVAL;
1127 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB) {
1128 u16 ctrl = serial_port_in(port, SCSCR);
1129 serial_port_out(port, SCSCR, ctrl & ~SCSCR_TIE);
1133 spin_unlock_irqrestore(&port->lock, flags);
1136 /* Locking: called with port lock held */
1137 static int sci_dma_rx_push(struct sci_port *s, void *buf, size_t count)
1139 struct uart_port *port = &s->port;
1140 struct tty_port *tport = &port->state->port;
1143 copied = tty_insert_flip_string(tport, buf, count);
1144 if (copied < count) {
1145 dev_warn(port->dev, "Rx overrun: dropping %zu bytes\n",
1147 port->icount.buf_overrun++;
1150 port->icount.rx += copied;
1155 static int sci_dma_rx_find_active(struct sci_port *s)
1159 for (i = 0; i < ARRAY_SIZE(s->cookie_rx); i++)
1160 if (s->active_rx == s->cookie_rx[i])
1163 dev_err(s->port.dev, "%s: Rx cookie %d not found!\n", __func__,
1168 static void sci_rx_dma_release(struct sci_port *s, bool enable_pio)
1170 struct dma_chan *chan = s->chan_rx;
1171 struct uart_port *port = &s->port;
1172 unsigned long flags;
1174 spin_lock_irqsave(&port->lock, flags);
1176 s->cookie_rx[0] = s->cookie_rx[1] = -EINVAL;
1177 spin_unlock_irqrestore(&port->lock, flags);
1178 dmaengine_terminate_all(chan);
1179 dma_free_coherent(chan->device->dev, s->buf_len_rx * 2, s->rx_buf[0],
1180 sg_dma_address(&s->sg_rx[0]));
1181 dma_release_channel(chan);
1186 static void sci_dma_rx_complete(void *arg)
1188 struct sci_port *s = arg;
1189 struct dma_chan *chan = s->chan_rx;
1190 struct uart_port *port = &s->port;
1191 struct dma_async_tx_descriptor *desc;
1192 unsigned long flags;
1193 int active, count = 0;
1195 dev_dbg(port->dev, "%s(%d) active cookie %d\n", __func__, port->line,
1198 spin_lock_irqsave(&port->lock, flags);
1200 active = sci_dma_rx_find_active(s);
1202 count = sci_dma_rx_push(s, s->rx_buf[active], s->buf_len_rx);
1204 mod_timer(&s->rx_timer, jiffies + s->rx_timeout);
1207 tty_flip_buffer_push(&port->state->port);
1209 desc = dmaengine_prep_slave_sg(s->chan_rx, &s->sg_rx[active], 1,
1211 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
1215 desc->callback = sci_dma_rx_complete;
1216 desc->callback_param = s;
1217 s->cookie_rx[active] = dmaengine_submit(desc);
1218 if (dma_submit_error(s->cookie_rx[active]))
1221 s->active_rx = s->cookie_rx[!active];
1223 dma_async_issue_pending(chan);
1225 dev_dbg(port->dev, "%s: cookie %d #%d, new active cookie %d\n",
1226 __func__, s->cookie_rx[active], active, s->active_rx);
1227 spin_unlock_irqrestore(&port->lock, flags);
1231 spin_unlock_irqrestore(&port->lock, flags);
1232 dev_warn(port->dev, "Failed submitting Rx DMA descriptor\n");
1233 sci_rx_dma_release(s, true);
1236 static void sci_tx_dma_release(struct sci_port *s, bool enable_pio)
1238 struct dma_chan *chan = s->chan_tx;
1239 struct uart_port *port = &s->port;
1240 unsigned long flags;
1242 spin_lock_irqsave(&port->lock, flags);
1244 s->cookie_tx = -EINVAL;
1245 spin_unlock_irqrestore(&port->lock, flags);
1246 dmaengine_terminate_all(chan);
1247 dma_unmap_single(chan->device->dev, s->tx_dma_addr, UART_XMIT_SIZE,
1249 dma_release_channel(chan);
1254 static void sci_submit_rx(struct sci_port *s)
1256 struct dma_chan *chan = s->chan_rx;
1259 for (i = 0; i < 2; i++) {
1260 struct scatterlist *sg = &s->sg_rx[i];
1261 struct dma_async_tx_descriptor *desc;
1263 desc = dmaengine_prep_slave_sg(chan,
1264 sg, 1, DMA_DEV_TO_MEM,
1265 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
1269 desc->callback = sci_dma_rx_complete;
1270 desc->callback_param = s;
1271 s->cookie_rx[i] = dmaengine_submit(desc);
1272 if (dma_submit_error(s->cookie_rx[i]))
1275 dev_dbg(s->port.dev, "%s(): cookie %d to #%d\n", __func__,
1276 s->cookie_rx[i], i);
1279 s->active_rx = s->cookie_rx[0];
1281 dma_async_issue_pending(chan);
1286 dmaengine_terminate_all(chan);
1287 for (i = 0; i < 2; i++)
1288 s->cookie_rx[i] = -EINVAL;
1289 s->active_rx = -EINVAL;
1290 dev_warn(s->port.dev, "Failed to re-start Rx DMA, using PIO\n");
1291 sci_rx_dma_release(s, true);
1294 static void work_fn_tx(struct work_struct *work)
1296 struct sci_port *s = container_of(work, struct sci_port, work_tx);
1297 struct dma_async_tx_descriptor *desc;
1298 struct dma_chan *chan = s->chan_tx;
1299 struct uart_port *port = &s->port;
1300 struct circ_buf *xmit = &port->state->xmit;
1306 * Port xmit buffer is already mapped, and it is one page... Just adjust
1307 * offsets and lengths. Since it is a circular buffer, we have to
1308 * transmit till the end, and then the rest. Take the port lock to get a
1309 * consistent xmit buffer state.
1311 spin_lock_irq(&port->lock);
1314 buf = s->tx_dma_addr + (tail & (UART_XMIT_SIZE - 1));
1315 s->tx_dma_len = min_t(unsigned int,
1316 CIRC_CNT(head, tail, UART_XMIT_SIZE),
1317 CIRC_CNT_TO_END(head, tail, UART_XMIT_SIZE));
1318 if (!s->tx_dma_len) {
1319 /* Transmit buffer has been flushed */
1320 spin_unlock_irq(&port->lock);
1324 desc = dmaengine_prep_slave_single(chan, buf, s->tx_dma_len,
1326 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
1328 spin_unlock_irq(&port->lock);
1329 dev_warn(port->dev, "Failed preparing Tx DMA descriptor\n");
1331 sci_tx_dma_release(s, true);
1335 dma_sync_single_for_device(chan->device->dev, buf, s->tx_dma_len,
1338 desc->callback = sci_dma_tx_complete;
1339 desc->callback_param = s;
1340 s->cookie_tx = dmaengine_submit(desc);
1341 if (dma_submit_error(s->cookie_tx)) {
1342 spin_unlock_irq(&port->lock);
1343 dev_warn(port->dev, "Failed submitting Tx DMA descriptor\n");
1345 sci_tx_dma_release(s, true);
1349 spin_unlock_irq(&port->lock);
1350 dev_dbg(port->dev, "%s: %p: %d...%d, cookie %d\n",
1351 __func__, xmit->buf, tail, head, s->cookie_tx);
1353 dma_async_issue_pending(chan);
1356 static void rx_timer_fn(unsigned long arg)
1358 struct sci_port *s = (struct sci_port *)arg;
1359 struct dma_chan *chan = s->chan_rx;
1360 struct uart_port *port = &s->port;
1361 struct dma_tx_state state;
1362 enum dma_status status;
1363 unsigned long flags;
1368 spin_lock_irqsave(&port->lock, flags);
1370 dev_dbg(port->dev, "DMA Rx timed out\n");
1372 active = sci_dma_rx_find_active(s);
1374 spin_unlock_irqrestore(&port->lock, flags);
1378 status = dmaengine_tx_status(s->chan_rx, s->active_rx, &state);
1379 if (status == DMA_COMPLETE) {
1380 dev_dbg(port->dev, "Cookie %d #%d has already completed\n",
1381 s->active_rx, active);
1382 spin_unlock_irqrestore(&port->lock, flags);
1384 /* Let packet complete handler take care of the packet */
1388 dmaengine_pause(chan);
1391 * sometimes DMA transfer doesn't stop even if it is stopped and
1392 * data keeps on coming until transaction is complete so check
1393 * for DMA_COMPLETE again
1394 * Let packet complete handler take care of the packet
1396 status = dmaengine_tx_status(s->chan_rx, s->active_rx, &state);
1397 if (status == DMA_COMPLETE) {
1398 spin_unlock_irqrestore(&port->lock, flags);
1399 dev_dbg(port->dev, "Transaction complete after DMA engine was stopped");
1403 /* Handle incomplete DMA receive */
1404 dmaengine_terminate_all(s->chan_rx);
1405 read = sg_dma_len(&s->sg_rx[active]) - state.residue;
1406 dev_dbg(port->dev, "Read %u bytes with cookie %d\n", read,
1410 count = sci_dma_rx_push(s, s->rx_buf[active], read);
1412 tty_flip_buffer_push(&port->state->port);
1415 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB)
1418 /* Direct new serial port interrupts back to CPU */
1419 scr = serial_port_in(port, SCSCR);
1420 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB) {
1421 scr &= ~SCSCR_RDRQE;
1422 enable_irq(s->irqs[SCIx_RXI_IRQ]);
1424 serial_port_out(port, SCSCR, scr | SCSCR_RIE);
1426 spin_unlock_irqrestore(&port->lock, flags);
1429 static struct dma_chan *sci_request_dma_chan(struct uart_port *port,
1430 enum dma_transfer_direction dir,
1433 dma_cap_mask_t mask;
1434 struct dma_chan *chan;
1435 struct dma_slave_config cfg;
1439 dma_cap_set(DMA_SLAVE, mask);
1441 chan = dma_request_slave_channel_compat(mask, shdma_chan_filter,
1442 (void *)(unsigned long)id, port->dev,
1443 dir == DMA_MEM_TO_DEV ? "tx" : "rx");
1446 "dma_request_slave_channel_compat failed\n");
1450 memset(&cfg, 0, sizeof(cfg));
1451 cfg.direction = dir;
1452 if (dir == DMA_MEM_TO_DEV) {
1453 cfg.dst_addr = port->mapbase +
1454 (sci_getreg(port, SCxTDR)->offset << port->regshift);
1455 cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
1457 cfg.src_addr = port->mapbase +
1458 (sci_getreg(port, SCxRDR)->offset << port->regshift);
1459 cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
1462 ret = dmaengine_slave_config(chan, &cfg);
1464 dev_warn(port->dev, "dmaengine_slave_config failed %d\n", ret);
1465 dma_release_channel(chan);
1472 static void sci_request_dma(struct uart_port *port)
1474 struct sci_port *s = to_sci_port(port);
1475 struct dma_chan *chan;
1477 dev_dbg(port->dev, "%s: port %d\n", __func__, port->line);
1479 if (!port->dev->of_node &&
1480 (s->cfg->dma_slave_tx <= 0 || s->cfg->dma_slave_rx <= 0))
1483 s->cookie_tx = -EINVAL;
1484 chan = sci_request_dma_chan(port, DMA_MEM_TO_DEV, s->cfg->dma_slave_tx);
1485 dev_dbg(port->dev, "%s: TX: got channel %p\n", __func__, chan);
1488 /* UART circular tx buffer is an aligned page. */
1489 s->tx_dma_addr = dma_map_single(chan->device->dev,
1490 port->state->xmit.buf,
1493 if (dma_mapping_error(chan->device->dev, s->tx_dma_addr)) {
1494 dev_warn(port->dev, "Failed mapping Tx DMA descriptor\n");
1495 dma_release_channel(chan);
1498 dev_dbg(port->dev, "%s: mapped %lu@%p to %pad\n",
1499 __func__, UART_XMIT_SIZE,
1500 port->state->xmit.buf, &s->tx_dma_addr);
1503 INIT_WORK(&s->work_tx, work_fn_tx);
1506 chan = sci_request_dma_chan(port, DMA_DEV_TO_MEM, s->cfg->dma_slave_rx);
1507 dev_dbg(port->dev, "%s: RX: got channel %p\n", __func__, chan);
1515 s->buf_len_rx = 2 * max_t(size_t, 16, port->fifosize);
1516 buf = dma_alloc_coherent(chan->device->dev, s->buf_len_rx * 2,
1520 "Failed to allocate Rx dma buffer, using PIO\n");
1521 dma_release_channel(chan);
1526 for (i = 0; i < 2; i++) {
1527 struct scatterlist *sg = &s->sg_rx[i];
1529 sg_init_table(sg, 1);
1531 sg_dma_address(sg) = dma;
1532 sg_dma_len(sg) = s->buf_len_rx;
1534 buf += s->buf_len_rx;
1535 dma += s->buf_len_rx;
1538 setup_timer(&s->rx_timer, rx_timer_fn, (unsigned long)s);
1540 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB)
1545 static void sci_free_dma(struct uart_port *port)
1547 struct sci_port *s = to_sci_port(port);
1550 sci_tx_dma_release(s, false);
1552 sci_rx_dma_release(s, false);
1555 static void sci_flush_buffer(struct uart_port *port)
1557 struct sci_port *s = to_sci_port(port);
1560 * In uart_flush_buffer(), the xmit circular buffer has just been
1561 * cleared, so we have to reset tx_dma_len accordingly, and stop any
1566 dmaengine_terminate_async(s->chan_tx);
1567 s->cookie_tx = -EINVAL;
1570 #else /* !CONFIG_SERIAL_SH_SCI_DMA */
1571 static inline void sci_request_dma(struct uart_port *port)
1575 static inline void sci_free_dma(struct uart_port *port)
1579 #define sci_flush_buffer NULL
1580 #endif /* !CONFIG_SERIAL_SH_SCI_DMA */
1582 static irqreturn_t sci_rx_interrupt(int irq, void *ptr)
1584 #ifdef CONFIG_SERIAL_SH_SCI_DMA
1585 struct uart_port *port = ptr;
1586 struct sci_port *s = to_sci_port(port);
1589 u16 scr = serial_port_in(port, SCSCR);
1590 u16 ssr = serial_port_in(port, SCxSR);
1592 /* Disable future Rx interrupts */
1593 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB) {
1594 disable_irq_nosync(irq);
1600 serial_port_out(port, SCSCR, scr);
1601 /* Clear current interrupt */
1602 serial_port_out(port, SCxSR,
1603 ssr & ~(SCIF_DR | SCxSR_RDxF(port)));
1604 dev_dbg(port->dev, "Rx IRQ %lu: setup t-out in %u jiffies\n",
1605 jiffies, s->rx_timeout);
1606 mod_timer(&s->rx_timer, jiffies + s->rx_timeout);
1612 /* I think sci_receive_chars has to be called irrespective
1613 * of whether the I_IXOFF is set, otherwise, how is the interrupt
1616 sci_receive_chars(ptr);
1621 static irqreturn_t sci_tx_interrupt(int irq, void *ptr)
1623 struct uart_port *port = ptr;
1624 unsigned long flags;
1626 spin_lock_irqsave(&port->lock, flags);
1627 sci_transmit_chars(port);
1628 spin_unlock_irqrestore(&port->lock, flags);
1633 static irqreturn_t sci_er_interrupt(int irq, void *ptr)
1635 struct uart_port *port = ptr;
1636 struct sci_port *s = to_sci_port(port);
1639 if (port->type == PORT_SCI) {
1640 if (sci_handle_errors(port)) {
1641 /* discard character in rx buffer */
1642 serial_port_in(port, SCxSR);
1643 sci_clear_SCxSR(port, SCxSR_RDxF_CLEAR(port));
1646 sci_handle_fifo_overrun(port);
1648 sci_receive_chars(ptr);
1651 sci_clear_SCxSR(port, SCxSR_ERROR_CLEAR(port));
1653 /* Kick the transmission */
1655 sci_tx_interrupt(irq, ptr);
1660 static irqreturn_t sci_br_interrupt(int irq, void *ptr)
1662 struct uart_port *port = ptr;
1665 sci_handle_breaks(port);
1666 sci_clear_SCxSR(port, SCxSR_BREAK_CLEAR(port));
1671 static irqreturn_t sci_mpxed_interrupt(int irq, void *ptr)
1673 unsigned short ssr_status, scr_status, err_enabled, orer_status = 0;
1674 struct uart_port *port = ptr;
1675 struct sci_port *s = to_sci_port(port);
1676 irqreturn_t ret = IRQ_NONE;
1678 ssr_status = serial_port_in(port, SCxSR);
1679 scr_status = serial_port_in(port, SCSCR);
1680 if (s->overrun_reg == SCxSR)
1681 orer_status = ssr_status;
1683 if (sci_getreg(port, s->overrun_reg)->size)
1684 orer_status = serial_port_in(port, s->overrun_reg);
1687 err_enabled = scr_status & port_rx_irq_mask(port);
1690 if ((ssr_status & SCxSR_TDxE(port)) && (scr_status & SCSCR_TIE) &&
1692 ret = sci_tx_interrupt(irq, ptr);
1695 * Rx Interrupt: if we're using DMA, the DMA controller clears RDF /
1698 if (((ssr_status & SCxSR_RDxF(port)) || s->chan_rx) &&
1699 (scr_status & SCSCR_RIE))
1700 ret = sci_rx_interrupt(irq, ptr);
1702 /* Error Interrupt */
1703 if ((ssr_status & SCxSR_ERRORS(port)) && err_enabled)
1704 ret = sci_er_interrupt(irq, ptr);
1706 /* Break Interrupt */
1707 if ((ssr_status & SCxSR_BRK(port)) && err_enabled)
1708 ret = sci_br_interrupt(irq, ptr);
1710 /* Overrun Interrupt */
1711 if (orer_status & s->overrun_mask) {
1712 sci_handle_fifo_overrun(port);
1719 static const struct sci_irq_desc {
1721 irq_handler_t handler;
1722 } sci_irq_desc[] = {
1724 * Split out handlers, the default case.
1728 .handler = sci_er_interrupt,
1733 .handler = sci_rx_interrupt,
1738 .handler = sci_tx_interrupt,
1743 .handler = sci_br_interrupt,
1747 * Special muxed handler.
1751 .handler = sci_mpxed_interrupt,
1755 static int sci_request_irq(struct sci_port *port)
1757 struct uart_port *up = &port->port;
1760 for (i = j = 0; i < SCIx_NR_IRQS; i++, j++) {
1761 const struct sci_irq_desc *desc;
1764 if (SCIx_IRQ_IS_MUXED(port)) {
1768 irq = port->irqs[i];
1771 * Certain port types won't support all of the
1772 * available interrupt sources.
1774 if (unlikely(irq < 0))
1778 desc = sci_irq_desc + i;
1779 port->irqstr[j] = kasprintf(GFP_KERNEL, "%s:%s",
1780 dev_name(up->dev), desc->desc);
1781 if (!port->irqstr[j])
1784 ret = request_irq(irq, desc->handler, up->irqflags,
1785 port->irqstr[j], port);
1786 if (unlikely(ret)) {
1787 dev_err(up->dev, "Can't allocate %s IRQ\n", desc->desc);
1796 free_irq(port->irqs[i], port);
1800 kfree(port->irqstr[j]);
1805 static void sci_free_irq(struct sci_port *port)
1810 * Intentionally in reverse order so we iterate over the muxed
1813 for (i = 0; i < SCIx_NR_IRQS; i++) {
1814 int irq = port->irqs[i];
1817 * Certain port types won't support all of the available
1818 * interrupt sources.
1820 if (unlikely(irq < 0))
1823 free_irq(port->irqs[i], port);
1824 kfree(port->irqstr[i]);
1826 if (SCIx_IRQ_IS_MUXED(port)) {
1827 /* If there's only one IRQ, we're done. */
1833 static unsigned int sci_tx_empty(struct uart_port *port)
1835 unsigned short status = serial_port_in(port, SCxSR);
1836 unsigned short in_tx_fifo = sci_txfill(port);
1838 return (status & SCxSR_TEND(port)) && !in_tx_fifo ? TIOCSER_TEMT : 0;
1841 static void sci_set_rts(struct uart_port *port, bool state)
1843 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB) {
1844 u16 data = serial_port_in(port, SCPDR);
1848 data &= ~SCPDR_RTSD;
1851 serial_port_out(port, SCPDR, data);
1853 /* RTS# is output */
1854 serial_port_out(port, SCPCR,
1855 serial_port_in(port, SCPCR) | SCPCR_RTSC);
1856 } else if (sci_getreg(port, SCSPTR)->size) {
1857 u16 ctrl = serial_port_in(port, SCSPTR);
1861 ctrl &= ~SCSPTR_RTSDT;
1863 ctrl |= SCSPTR_RTSDT;
1864 serial_port_out(port, SCSPTR, ctrl);
1868 static bool sci_get_cts(struct uart_port *port)
1870 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB) {
1872 return !(serial_port_in(port, SCPDR) & SCPDR_CTSD);
1873 } else if (sci_getreg(port, SCSPTR)->size) {
1875 return !(serial_port_in(port, SCSPTR) & SCSPTR_CTSDT);
1882 * Modem control is a bit of a mixed bag for SCI(F) ports. Generally
1883 * CTS/RTS is supported in hardware by at least one port and controlled
1884 * via SCSPTR (SCxPCR for SCIFA/B parts), or external pins (presently
1885 * handled via the ->init_pins() op, which is a bit of a one-way street,
1886 * lacking any ability to defer pin control -- this will later be
1887 * converted over to the GPIO framework).
1889 * Other modes (such as loopback) are supported generically on certain
1890 * port types, but not others. For these it's sufficient to test for the
1891 * existence of the support register and simply ignore the port type.
1893 static void sci_set_mctrl(struct uart_port *port, unsigned int mctrl)
1895 struct sci_port *s = to_sci_port(port);
1897 if (mctrl & TIOCM_LOOP) {
1898 const struct plat_sci_reg *reg;
1901 * Standard loopback mode for SCFCR ports.
1903 reg = sci_getreg(port, SCFCR);
1905 serial_port_out(port, SCFCR,
1906 serial_port_in(port, SCFCR) |
1910 mctrl_gpio_set(s->gpios, mctrl);
1912 if (!(s->cfg->capabilities & SCIx_HAVE_RTSCTS))
1915 if (!(mctrl & TIOCM_RTS)) {
1916 /* Disable Auto RTS */
1917 serial_port_out(port, SCFCR,
1918 serial_port_in(port, SCFCR) & ~SCFCR_MCE);
1921 sci_set_rts(port, 0);
1922 } else if (s->autorts) {
1923 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB) {
1924 /* Enable RTS# pin function */
1925 serial_port_out(port, SCPCR,
1926 serial_port_in(port, SCPCR) & ~SCPCR_RTSC);
1929 /* Enable Auto RTS */
1930 serial_port_out(port, SCFCR,
1931 serial_port_in(port, SCFCR) | SCFCR_MCE);
1934 sci_set_rts(port, 1);
1938 static unsigned int sci_get_mctrl(struct uart_port *port)
1940 struct sci_port *s = to_sci_port(port);
1941 struct mctrl_gpios *gpios = s->gpios;
1942 unsigned int mctrl = 0;
1944 mctrl_gpio_get(gpios, &mctrl);
1947 * CTS/RTS is handled in hardware when supported, while nothing
1951 if (sci_get_cts(port))
1953 } else if (IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(gpios, UART_GPIO_CTS))) {
1956 if (IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(gpios, UART_GPIO_DSR)))
1958 if (IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(gpios, UART_GPIO_DCD)))
1964 static void sci_enable_ms(struct uart_port *port)
1966 mctrl_gpio_enable_ms(to_sci_port(port)->gpios);
1969 static void sci_break_ctl(struct uart_port *port, int break_state)
1971 unsigned short scscr, scsptr;
1973 /* check wheter the port has SCSPTR */
1974 if (!sci_getreg(port, SCSPTR)->size) {
1976 * Not supported by hardware. Most parts couple break and rx
1977 * interrupts together, with break detection always enabled.
1982 scsptr = serial_port_in(port, SCSPTR);
1983 scscr = serial_port_in(port, SCSCR);
1985 if (break_state == -1) {
1986 scsptr = (scsptr | SCSPTR_SPB2IO) & ~SCSPTR_SPB2DT;
1989 scsptr = (scsptr | SCSPTR_SPB2DT) & ~SCSPTR_SPB2IO;
1993 serial_port_out(port, SCSPTR, scsptr);
1994 serial_port_out(port, SCSCR, scscr);
1997 static int sci_startup(struct uart_port *port)
1999 struct sci_port *s = to_sci_port(port);
2002 dev_dbg(port->dev, "%s(%d)\n", __func__, port->line);
2004 sci_request_dma(port);
2006 ret = sci_request_irq(s);
2007 if (unlikely(ret < 0)) {
2015 static void sci_shutdown(struct uart_port *port)
2017 struct sci_port *s = to_sci_port(port);
2018 unsigned long flags;
2021 dev_dbg(port->dev, "%s(%d)\n", __func__, port->line);
2024 mctrl_gpio_disable_ms(to_sci_port(port)->gpios);
2026 spin_lock_irqsave(&port->lock, flags);
2029 /* Stop RX and TX, disable related interrupts, keep clock source */
2030 scr = serial_port_in(port, SCSCR);
2031 serial_port_out(port, SCSCR, scr & (SCSCR_CKE1 | SCSCR_CKE0));
2032 spin_unlock_irqrestore(&port->lock, flags);
2034 #ifdef CONFIG_SERIAL_SH_SCI_DMA
2036 dev_dbg(port->dev, "%s(%d) deleting rx_timer\n", __func__,
2038 del_timer_sync(&s->rx_timer);
2046 static int sci_sck_calc(struct sci_port *s, unsigned int bps,
2049 unsigned long freq = s->clk_rates[SCI_SCK];
2050 int err, min_err = INT_MAX;
2053 if (s->port.type != PORT_HSCIF)
2056 for_each_sr(sr, s) {
2057 err = DIV_ROUND_CLOSEST(freq, sr) - bps;
2058 if (abs(err) >= abs(min_err))
2068 dev_dbg(s->port.dev, "SCK: %u%+d bps using SR %u\n", bps, min_err,
2073 static int sci_brg_calc(struct sci_port *s, unsigned int bps,
2074 unsigned long freq, unsigned int *dlr,
2077 int err, min_err = INT_MAX;
2078 unsigned int sr, dl;
2080 if (s->port.type != PORT_HSCIF)
2083 for_each_sr(sr, s) {
2084 dl = DIV_ROUND_CLOSEST(freq, sr * bps);
2085 dl = clamp(dl, 1U, 65535U);
2087 err = DIV_ROUND_CLOSEST(freq, sr * dl) - bps;
2088 if (abs(err) >= abs(min_err))
2099 dev_dbg(s->port.dev, "BRG: %u%+d bps using DL %u SR %u\n", bps,
2100 min_err, *dlr, *srr + 1);
2104 /* calculate sample rate, BRR, and clock select */
2105 static int sci_scbrr_calc(struct sci_port *s, unsigned int bps,
2106 unsigned int *brr, unsigned int *srr,
2109 unsigned long freq = s->clk_rates[SCI_FCK];
2110 unsigned int sr, br, prediv, scrate, c;
2111 int err, min_err = INT_MAX;
2113 if (s->port.type != PORT_HSCIF)
2117 * Find the combination of sample rate and clock select with the
2118 * smallest deviation from the desired baud rate.
2119 * Prefer high sample rates to maximise the receive margin.
2121 * M: Receive margin (%)
2122 * N: Ratio of bit rate to clock (N = sampling rate)
2123 * D: Clock duty (D = 0 to 1.0)
2124 * L: Frame length (L = 9 to 12)
2125 * F: Absolute value of clock frequency deviation
2127 * M = |(0.5 - 1 / 2 * N) - ((L - 0.5) * F) -
2128 * (|D - 0.5| / N * (1 + F))|
2129 * NOTE: Usually, treat D for 0.5, F is 0 by this calculation.
2131 for_each_sr(sr, s) {
2132 for (c = 0; c <= 3; c++) {
2133 /* integerized formulas from HSCIF documentation */
2134 prediv = sr * (1 << (2 * c + 1));
2137 * We need to calculate:
2139 * br = freq / (prediv * bps) clamped to [1..256]
2140 * err = freq / (br * prediv) - bps
2142 * Watch out for overflow when calculating the desired
2143 * sampling clock rate!
2145 if (bps > UINT_MAX / prediv)
2148 scrate = prediv * bps;
2149 br = DIV_ROUND_CLOSEST(freq, scrate);
2150 br = clamp(br, 1U, 256U);
2152 err = DIV_ROUND_CLOSEST(freq, br * prediv) - bps;
2153 if (abs(err) >= abs(min_err))
2167 dev_dbg(s->port.dev, "BRR: %u%+d bps using N %u SR %u cks %u\n", bps,
2168 min_err, *brr, *srr + 1, *cks);
2172 static void sci_reset(struct uart_port *port)
2174 const struct plat_sci_reg *reg;
2175 unsigned int status;
2178 status = serial_port_in(port, SCxSR);
2179 } while (!(status & SCxSR_TEND(port)));
2181 serial_port_out(port, SCSCR, 0x00); /* TE=0, RE=0, CKE1=0 */
2183 reg = sci_getreg(port, SCFCR);
2185 serial_port_out(port, SCFCR, SCFCR_RFRST | SCFCR_TFRST);
2187 sci_clear_SCxSR(port,
2188 SCxSR_RDxF_CLEAR(port) & SCxSR_ERROR_CLEAR(port) &
2189 SCxSR_BREAK_CLEAR(port));
2190 if (sci_getreg(port, SCLSR)->size) {
2191 status = serial_port_in(port, SCLSR);
2192 status &= ~(SCLSR_TO | SCLSR_ORER);
2193 serial_port_out(port, SCLSR, status);
2197 static void sci_set_termios(struct uart_port *port, struct ktermios *termios,
2198 struct ktermios *old)
2200 unsigned int baud, smr_val = SCSMR_ASYNC, scr_val = 0, i;
2201 unsigned int brr = 255, cks = 0, srr = 15, dl = 0, sccks = 0;
2202 unsigned int brr1 = 255, cks1 = 0, srr1 = 15, dl1 = 0;
2203 struct sci_port *s = to_sci_port(port);
2204 const struct plat_sci_reg *reg;
2205 int min_err = INT_MAX, err;
2206 unsigned long max_freq = 0;
2209 if ((termios->c_cflag & CSIZE) == CS7) {
2210 smr_val |= SCSMR_CHR;
2212 termios->c_cflag &= ~CSIZE;
2213 termios->c_cflag |= CS8;
2215 if (termios->c_cflag & PARENB)
2216 smr_val |= SCSMR_PE;
2217 if (termios->c_cflag & PARODD)
2218 smr_val |= SCSMR_PE | SCSMR_ODD;
2219 if (termios->c_cflag & CSTOPB)
2220 smr_val |= SCSMR_STOP;
2223 * earlyprintk comes here early on with port->uartclk set to zero.
2224 * the clock framework is not up and running at this point so here
2225 * we assume that 115200 is the maximum baud rate. please note that
2226 * the baud rate is not programmed during earlyprintk - it is assumed
2227 * that the previous boot loader has enabled required clocks and
2228 * setup the baud rate generator hardware for us already.
2230 if (!port->uartclk) {
2231 baud = uart_get_baud_rate(port, termios, old, 0, 115200);
2235 for (i = 0; i < SCI_NUM_CLKS; i++)
2236 max_freq = max(max_freq, s->clk_rates[i]);
2238 baud = uart_get_baud_rate(port, termios, old, 0, max_freq / min_sr(s));
2243 * There can be multiple sources for the sampling clock. Find the one
2244 * that gives us the smallest deviation from the desired baud rate.
2247 /* Optional Undivided External Clock */
2248 if (s->clk_rates[SCI_SCK] && port->type != PORT_SCIFA &&
2249 port->type != PORT_SCIFB) {
2250 err = sci_sck_calc(s, baud, &srr1);
2251 if (abs(err) < abs(min_err)) {
2253 scr_val = SCSCR_CKE1;
2262 /* Optional BRG Frequency Divided External Clock */
2263 if (s->clk_rates[SCI_SCIF_CLK] && sci_getreg(port, SCDL)->size) {
2264 err = sci_brg_calc(s, baud, s->clk_rates[SCI_SCIF_CLK], &dl1,
2266 if (abs(err) < abs(min_err)) {
2267 best_clk = SCI_SCIF_CLK;
2268 scr_val = SCSCR_CKE1;
2278 /* Optional BRG Frequency Divided Internal Clock */
2279 if (s->clk_rates[SCI_BRG_INT] && sci_getreg(port, SCDL)->size) {
2280 err = sci_brg_calc(s, baud, s->clk_rates[SCI_BRG_INT], &dl1,
2282 if (abs(err) < abs(min_err)) {
2283 best_clk = SCI_BRG_INT;
2284 scr_val = SCSCR_CKE1;
2294 /* Divided Functional Clock using standard Bit Rate Register */
2295 err = sci_scbrr_calc(s, baud, &brr1, &srr1, &cks1);
2296 if (abs(err) < abs(min_err)) {
2307 dev_dbg(port->dev, "Using clk %pC for %u%+d bps\n",
2308 s->clks[best_clk], baud, min_err);
2313 * Program the optional External Baud Rate Generator (BRG) first.
2314 * It controls the mux to select (H)SCK or frequency divided clock.
2316 if (best_clk >= 0 && sci_getreg(port, SCCKS)->size) {
2317 serial_port_out(port, SCDL, dl);
2318 serial_port_out(port, SCCKS, sccks);
2323 uart_update_timeout(port, termios->c_cflag, baud);
2325 if (best_clk >= 0) {
2326 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB)
2328 case 5: smr_val |= SCSMR_SRC_5; break;
2329 case 7: smr_val |= SCSMR_SRC_7; break;
2330 case 11: smr_val |= SCSMR_SRC_11; break;
2331 case 13: smr_val |= SCSMR_SRC_13; break;
2332 case 16: smr_val |= SCSMR_SRC_16; break;
2333 case 17: smr_val |= SCSMR_SRC_17; break;
2334 case 19: smr_val |= SCSMR_SRC_19; break;
2335 case 27: smr_val |= SCSMR_SRC_27; break;
2339 "SCR 0x%x SMR 0x%x BRR %u CKS 0x%x DL %u SRR %u\n",
2340 scr_val, smr_val, brr, sccks, dl, srr);
2341 serial_port_out(port, SCSCR, scr_val);
2342 serial_port_out(port, SCSMR, smr_val);
2343 serial_port_out(port, SCBRR, brr);
2344 if (sci_getreg(port, HSSRR)->size)
2345 serial_port_out(port, HSSRR, srr | HSCIF_SRE);
2347 /* Wait one bit interval */
2348 udelay((1000000 + (baud - 1)) / baud);
2350 /* Don't touch the bit rate configuration */
2351 scr_val = s->cfg->scscr & (SCSCR_CKE1 | SCSCR_CKE0);
2352 smr_val |= serial_port_in(port, SCSMR) &
2353 (SCSMR_CKEDG | SCSMR_SRC_MASK | SCSMR_CKS);
2354 dev_dbg(port->dev, "SCR 0x%x SMR 0x%x\n", scr_val, smr_val);
2355 serial_port_out(port, SCSCR, scr_val);
2356 serial_port_out(port, SCSMR, smr_val);
2359 sci_init_pins(port, termios->c_cflag);
2361 port->status &= ~UPSTAT_AUTOCTS;
2363 reg = sci_getreg(port, SCFCR);
2365 unsigned short ctrl = serial_port_in(port, SCFCR);
2367 if ((port->flags & UPF_HARD_FLOW) &&
2368 (termios->c_cflag & CRTSCTS)) {
2369 /* There is no CTS interrupt to restart the hardware */
2370 port->status |= UPSTAT_AUTOCTS;
2371 /* MCE is enabled when RTS is raised */
2376 * As we've done a sci_reset() above, ensure we don't
2377 * interfere with the FIFOs while toggling MCE. As the
2378 * reset values could still be set, simply mask them out.
2380 ctrl &= ~(SCFCR_RFRST | SCFCR_TFRST);
2382 serial_port_out(port, SCFCR, ctrl);
2384 if (port->flags & UPF_HARD_FLOW) {
2385 /* Refresh (Auto) RTS */
2386 sci_set_mctrl(port, port->mctrl);
2389 scr_val |= s->cfg->scscr & ~(SCSCR_CKE1 | SCSCR_CKE0);
2390 dev_dbg(port->dev, "SCSCR 0x%x\n", scr_val);
2391 serial_port_out(port, SCSCR, scr_val);
2392 if ((srr + 1 == 5) &&
2393 (port->type == PORT_SCIFA || port->type == PORT_SCIFB)) {
2395 * In asynchronous mode, when the sampling rate is 1/5, first
2396 * received data may become invalid on some SCIFA and SCIFB.
2397 * To avoid this problem wait more than 1 serial data time (1
2398 * bit time x serial data number) after setting SCSCR.RE = 1.
2400 udelay(DIV_ROUND_UP(10 * 1000000, baud));
2403 #ifdef CONFIG_SERIAL_SH_SCI_DMA
2405 * Calculate delay for 2 DMA buffers (4 FIFO).
2406 * See serial_core.c::uart_update_timeout().
2407 * With 10 bits (CS8), 250Hz, 115200 baud and 64 bytes FIFO, the above
2408 * function calculates 1 jiffie for the data plus 5 jiffies for the
2409 * "slop(e)." Then below we calculate 5 jiffies (20ms) for 2 DMA
2410 * buffers (4 FIFO sizes), but when performing a faster transfer, the
2411 * value obtained by this formula is too small. Therefore, if the value
2412 * is smaller than 20ms, use 20ms as the timeout value for DMA.
2417 /* byte size and parity */
2418 switch (termios->c_cflag & CSIZE) {
2433 if (termios->c_cflag & CSTOPB)
2435 if (termios->c_cflag & PARENB)
2437 s->rx_timeout = DIV_ROUND_UP((s->buf_len_rx * 2 * bits * HZ) /
2439 dev_dbg(port->dev, "DMA Rx t-out %ums, tty t-out %u jiffies\n",
2440 s->rx_timeout * 1000 / HZ, port->timeout);
2441 if (s->rx_timeout < msecs_to_jiffies(20))
2442 s->rx_timeout = msecs_to_jiffies(20);
2446 if ((termios->c_cflag & CREAD) != 0)
2449 sci_port_disable(s);
2451 if (UART_ENABLE_MS(port, termios->c_cflag))
2452 sci_enable_ms(port);
2455 static void sci_pm(struct uart_port *port, unsigned int state,
2456 unsigned int oldstate)
2458 struct sci_port *sci_port = to_sci_port(port);
2461 case UART_PM_STATE_OFF:
2462 sci_port_disable(sci_port);
2465 sci_port_enable(sci_port);
2470 static const char *sci_type(struct uart_port *port)
2472 switch (port->type) {
2490 static int sci_remap_port(struct uart_port *port)
2492 struct sci_port *sport = to_sci_port(port);
2495 * Nothing to do if there's already an established membase.
2500 if (port->flags & UPF_IOREMAP) {
2501 port->membase = ioremap_nocache(port->mapbase, sport->reg_size);
2502 if (unlikely(!port->membase)) {
2503 dev_err(port->dev, "can't remap port#%d\n", port->line);
2508 * For the simple (and majority of) cases where we don't
2509 * need to do any remapping, just cast the cookie
2512 port->membase = (void __iomem *)(uintptr_t)port->mapbase;
2518 static void sci_release_port(struct uart_port *port)
2520 struct sci_port *sport = to_sci_port(port);
2522 if (port->flags & UPF_IOREMAP) {
2523 iounmap(port->membase);
2524 port->membase = NULL;
2527 release_mem_region(port->mapbase, sport->reg_size);
2530 static int sci_request_port(struct uart_port *port)
2532 struct resource *res;
2533 struct sci_port *sport = to_sci_port(port);
2536 res = request_mem_region(port->mapbase, sport->reg_size,
2537 dev_name(port->dev));
2538 if (unlikely(res == NULL)) {
2539 dev_err(port->dev, "request_mem_region failed.");
2543 ret = sci_remap_port(port);
2544 if (unlikely(ret != 0)) {
2545 release_resource(res);
2552 static void sci_config_port(struct uart_port *port, int flags)
2554 if (flags & UART_CONFIG_TYPE) {
2555 struct sci_port *sport = to_sci_port(port);
2557 port->type = sport->cfg->type;
2558 sci_request_port(port);
2562 static int sci_verify_port(struct uart_port *port, struct serial_struct *ser)
2564 if (ser->baud_base < 2400)
2565 /* No paper tape reader for Mitch.. */
2571 static const struct uart_ops sci_uart_ops = {
2572 .tx_empty = sci_tx_empty,
2573 .set_mctrl = sci_set_mctrl,
2574 .get_mctrl = sci_get_mctrl,
2575 .start_tx = sci_start_tx,
2576 .stop_tx = sci_stop_tx,
2577 .stop_rx = sci_stop_rx,
2578 .enable_ms = sci_enable_ms,
2579 .break_ctl = sci_break_ctl,
2580 .startup = sci_startup,
2581 .shutdown = sci_shutdown,
2582 .flush_buffer = sci_flush_buffer,
2583 .set_termios = sci_set_termios,
2586 .release_port = sci_release_port,
2587 .request_port = sci_request_port,
2588 .config_port = sci_config_port,
2589 .verify_port = sci_verify_port,
2590 #ifdef CONFIG_CONSOLE_POLL
2591 .poll_get_char = sci_poll_get_char,
2592 .poll_put_char = sci_poll_put_char,
2596 static int sci_init_clocks(struct sci_port *sci_port, struct device *dev)
2598 const char *clk_names[] = {
2601 [SCI_BRG_INT] = "brg_int",
2602 [SCI_SCIF_CLK] = "scif_clk",
2607 if (sci_port->cfg->type == PORT_HSCIF)
2608 clk_names[SCI_SCK] = "hsck";
2610 for (i = 0; i < SCI_NUM_CLKS; i++) {
2611 clk = devm_clk_get(dev, clk_names[i]);
2612 if (PTR_ERR(clk) == -EPROBE_DEFER)
2613 return -EPROBE_DEFER;
2615 if (IS_ERR(clk) && i == SCI_FCK) {
2617 * "fck" used to be called "sci_ick", and we need to
2618 * maintain DT backward compatibility.
2620 clk = devm_clk_get(dev, "sci_ick");
2621 if (PTR_ERR(clk) == -EPROBE_DEFER)
2622 return -EPROBE_DEFER;
2628 * Not all SH platforms declare a clock lookup entry
2629 * for SCI devices, in which case we need to get the
2630 * global "peripheral_clk" clock.
2632 clk = devm_clk_get(dev, "peripheral_clk");
2636 dev_err(dev, "failed to get %s (%ld)\n", clk_names[i],
2638 return PTR_ERR(clk);
2643 dev_dbg(dev, "failed to get %s (%ld)\n", clk_names[i],
2646 dev_dbg(dev, "clk %s is %pC rate %lu\n", clk_names[i],
2647 clk, clk_get_rate(clk));
2648 sci_port->clks[i] = IS_ERR(clk) ? NULL : clk;
2653 static int sci_init_single(struct platform_device *dev,
2654 struct sci_port *sci_port, unsigned int index,
2655 struct plat_sci_port *p, bool early)
2657 struct uart_port *port = &sci_port->port;
2658 const struct resource *res;
2664 port->ops = &sci_uart_ops;
2665 port->iotype = UPIO_MEM;
2668 res = platform_get_resource(dev, IORESOURCE_MEM, 0);
2672 port->mapbase = res->start;
2673 sci_port->reg_size = resource_size(res);
2675 for (i = 0; i < ARRAY_SIZE(sci_port->irqs); ++i)
2676 sci_port->irqs[i] = platform_get_irq(dev, i);
2678 /* The SCI generates several interrupts. They can be muxed together or
2679 * connected to different interrupt lines. In the muxed case only one
2680 * interrupt resource is specified. In the non-muxed case three or four
2681 * interrupt resources are specified, as the BRI interrupt is optional.
2683 if (sci_port->irqs[0] < 0)
2686 if (sci_port->irqs[1] < 0) {
2687 sci_port->irqs[1] = sci_port->irqs[0];
2688 sci_port->irqs[2] = sci_port->irqs[0];
2689 sci_port->irqs[3] = sci_port->irqs[0];
2692 if (p->regtype == SCIx_PROBE_REGTYPE) {
2693 ret = sci_probe_regmap(p);
2700 port->fifosize = 256;
2701 sci_port->overrun_reg = SCxSR;
2702 sci_port->overrun_mask = SCIFA_ORER;
2703 sci_port->sampling_rate_mask = SCI_SR_SCIFAB;
2706 port->fifosize = 128;
2707 sci_port->overrun_reg = SCLSR;
2708 sci_port->overrun_mask = SCLSR_ORER;
2709 sci_port->sampling_rate_mask = SCI_SR_RANGE(8, 32);
2712 port->fifosize = 64;
2713 sci_port->overrun_reg = SCxSR;
2714 sci_port->overrun_mask = SCIFA_ORER;
2715 sci_port->sampling_rate_mask = SCI_SR_SCIFAB;
2718 port->fifosize = 16;
2719 if (p->regtype == SCIx_SH7705_SCIF_REGTYPE) {
2720 sci_port->overrun_reg = SCxSR;
2721 sci_port->overrun_mask = SCIFA_ORER;
2722 sci_port->sampling_rate_mask = SCI_SR(16);
2724 sci_port->overrun_reg = SCLSR;
2725 sci_port->overrun_mask = SCLSR_ORER;
2726 sci_port->sampling_rate_mask = SCI_SR(32);
2731 sci_port->overrun_reg = SCxSR;
2732 sci_port->overrun_mask = SCI_ORER;
2733 sci_port->sampling_rate_mask = SCI_SR(32);
2737 /* SCIFA on sh7723 and sh7724 need a custom sampling rate that doesn't
2738 * match the SoC datasheet, this should be investigated. Let platform
2739 * data override the sampling rate for now.
2741 if (p->sampling_rate)
2742 sci_port->sampling_rate_mask = SCI_SR(p->sampling_rate);
2745 ret = sci_init_clocks(sci_port, &dev->dev);
2749 port->dev = &dev->dev;
2751 pm_runtime_enable(&dev->dev);
2754 sci_port->break_timer.data = (unsigned long)sci_port;
2755 sci_port->break_timer.function = sci_break_timer;
2756 init_timer(&sci_port->break_timer);
2759 * Establish some sensible defaults for the error detection.
2761 if (p->type == PORT_SCI) {
2762 sci_port->error_mask = SCI_DEFAULT_ERROR_MASK;
2763 sci_port->error_clear = SCI_ERROR_CLEAR;
2765 sci_port->error_mask = SCIF_DEFAULT_ERROR_MASK;
2766 sci_port->error_clear = SCIF_ERROR_CLEAR;
2770 * Make the error mask inclusive of overrun detection, if
2773 if (sci_port->overrun_reg == SCxSR) {
2774 sci_port->error_mask |= sci_port->overrun_mask;
2775 sci_port->error_clear &= ~sci_port->overrun_mask;
2778 port->type = p->type;
2779 port->flags = UPF_FIXED_PORT | p->flags;
2780 port->regshift = p->regshift;
2783 * The UART port needs an IRQ value, so we peg this to the RX IRQ
2784 * for the multi-IRQ ports, which is where we are primarily
2785 * concerned with the shutdown path synchronization.
2787 * For the muxed case there's nothing more to do.
2789 port->irq = sci_port->irqs[SCIx_RXI_IRQ];
2792 port->serial_in = sci_serial_in;
2793 port->serial_out = sci_serial_out;
2795 if (p->dma_slave_tx > 0 && p->dma_slave_rx > 0)
2796 dev_dbg(port->dev, "DMA tx %d, rx %d\n",
2797 p->dma_slave_tx, p->dma_slave_rx);
2802 static void sci_cleanup_single(struct sci_port *port)
2804 pm_runtime_disable(port->port.dev);
2807 #if defined(CONFIG_SERIAL_SH_SCI_CONSOLE) || \
2808 defined(CONFIG_SERIAL_SH_SCI_EARLYCON)
2809 static void serial_console_putchar(struct uart_port *port, int ch)
2811 sci_poll_put_char(port, ch);
2815 * Print a string to the serial port trying not to disturb
2816 * any possible real use of the port...
2818 static void serial_console_write(struct console *co, const char *s,
2821 struct sci_port *sci_port = &sci_ports[co->index];
2822 struct uart_port *port = &sci_port->port;
2823 unsigned short bits, ctrl, ctrl_temp;
2824 unsigned long flags;
2827 #if defined(SUPPORT_SYSRQ)
2832 if (oops_in_progress)
2833 locked = spin_trylock_irqsave(&port->lock, flags);
2835 spin_lock_irqsave(&port->lock, flags);
2837 /* first save SCSCR then disable interrupts, keep clock source */
2838 ctrl = serial_port_in(port, SCSCR);
2839 ctrl_temp = (sci_port->cfg->scscr & ~(SCSCR_CKE1 | SCSCR_CKE0)) |
2840 (ctrl & (SCSCR_CKE1 | SCSCR_CKE0));
2841 serial_port_out(port, SCSCR, ctrl_temp);
2843 uart_console_write(port, s, count, serial_console_putchar);
2845 /* wait until fifo is empty and last bit has been transmitted */
2846 bits = SCxSR_TDxE(port) | SCxSR_TEND(port);
2847 while ((serial_port_in(port, SCxSR) & bits) != bits)
2850 /* restore the SCSCR */
2851 serial_port_out(port, SCSCR, ctrl);
2854 spin_unlock_irqrestore(&port->lock, flags);
2857 static int serial_console_setup(struct console *co, char *options)
2859 struct sci_port *sci_port;
2860 struct uart_port *port;
2868 * Refuse to handle any bogus ports.
2870 if (co->index < 0 || co->index >= SCI_NPORTS)
2873 sci_port = &sci_ports[co->index];
2874 port = &sci_port->port;
2877 * Refuse to handle uninitialized ports.
2882 ret = sci_remap_port(port);
2883 if (unlikely(ret != 0))
2887 uart_parse_options(options, &baud, &parity, &bits, &flow);
2889 return uart_set_options(port, co, baud, parity, bits, flow);
2892 static struct console serial_console = {
2894 .device = uart_console_device,
2895 .write = serial_console_write,
2896 .setup = serial_console_setup,
2897 .flags = CON_PRINTBUFFER,
2899 .data = &sci_uart_driver,
2902 static struct console early_serial_console = {
2903 .name = "early_ttySC",
2904 .write = serial_console_write,
2905 .flags = CON_PRINTBUFFER,
2909 static char early_serial_buf[32];
2911 static int sci_probe_earlyprintk(struct platform_device *pdev)
2913 struct plat_sci_port *cfg = dev_get_platdata(&pdev->dev);
2915 if (early_serial_console.data)
2918 early_serial_console.index = pdev->id;
2920 sci_init_single(pdev, &sci_ports[pdev->id], pdev->id, cfg, true);
2922 serial_console_setup(&early_serial_console, early_serial_buf);
2924 if (!strstr(early_serial_buf, "keep"))
2925 early_serial_console.flags |= CON_BOOT;
2927 register_console(&early_serial_console);
2931 #define SCI_CONSOLE (&serial_console)
2934 static inline int sci_probe_earlyprintk(struct platform_device *pdev)
2939 #define SCI_CONSOLE NULL
2941 #endif /* CONFIG_SERIAL_SH_SCI_CONSOLE || CONFIG_SERIAL_SH_SCI_EARLYCON */
2943 static const char banner[] __initconst = "SuperH (H)SCI(F) driver initialized";
2945 static struct uart_driver sci_uart_driver = {
2946 .owner = THIS_MODULE,
2947 .driver_name = "sci",
2948 .dev_name = "ttySC",
2950 .minor = SCI_MINOR_START,
2952 .cons = SCI_CONSOLE,
2955 static int sci_remove(struct platform_device *dev)
2957 struct sci_port *port = platform_get_drvdata(dev);
2959 uart_remove_one_port(&sci_uart_driver, &port->port);
2961 sci_cleanup_single(port);
2967 #define SCI_OF_DATA(type, regtype) (void *)((type) << 16 | (regtype))
2968 #define SCI_OF_TYPE(data) ((unsigned long)(data) >> 16)
2969 #define SCI_OF_REGTYPE(data) ((unsigned long)(data) & 0xffff)
2971 static const struct of_device_id of_sci_match[] = {
2972 /* SoC-specific types */
2974 .compatible = "renesas,scif-r7s72100",
2975 .data = SCI_OF_DATA(PORT_SCIF, SCIx_SH2_SCIF_FIFODATA_REGTYPE),
2977 /* Family-specific types */
2979 .compatible = "renesas,rcar-gen1-scif",
2980 .data = SCI_OF_DATA(PORT_SCIF, SCIx_SH4_SCIF_BRG_REGTYPE),
2982 .compatible = "renesas,rcar-gen2-scif",
2983 .data = SCI_OF_DATA(PORT_SCIF, SCIx_SH4_SCIF_BRG_REGTYPE),
2985 .compatible = "renesas,rcar-gen3-scif",
2986 .data = SCI_OF_DATA(PORT_SCIF, SCIx_SH4_SCIF_BRG_REGTYPE),
2990 .compatible = "renesas,scif",
2991 .data = SCI_OF_DATA(PORT_SCIF, SCIx_SH4_SCIF_REGTYPE),
2993 .compatible = "renesas,scifa",
2994 .data = SCI_OF_DATA(PORT_SCIFA, SCIx_SCIFA_REGTYPE),
2996 .compatible = "renesas,scifb",
2997 .data = SCI_OF_DATA(PORT_SCIFB, SCIx_SCIFB_REGTYPE),
2999 .compatible = "renesas,hscif",
3000 .data = SCI_OF_DATA(PORT_HSCIF, SCIx_HSCIF_REGTYPE),
3002 .compatible = "renesas,sci",
3003 .data = SCI_OF_DATA(PORT_SCI, SCIx_SCI_REGTYPE),
3008 MODULE_DEVICE_TABLE(of, of_sci_match);
3010 static struct plat_sci_port *
3011 sci_parse_dt(struct platform_device *pdev, unsigned int *dev_id)
3013 struct device_node *np = pdev->dev.of_node;
3014 const struct of_device_id *match;
3015 struct plat_sci_port *p;
3018 if (!IS_ENABLED(CONFIG_OF) || !np)
3021 match = of_match_node(of_sci_match, np);
3025 p = devm_kzalloc(&pdev->dev, sizeof(struct plat_sci_port), GFP_KERNEL);
3029 /* Get the line number from the aliases node. */
3030 id = of_alias_get_id(np, "serial");
3032 dev_err(&pdev->dev, "failed to get alias id (%d)\n", id);
3038 p->flags = UPF_IOREMAP | UPF_BOOT_AUTOCONF;
3039 p->type = SCI_OF_TYPE(match->data);
3040 p->regtype = SCI_OF_REGTYPE(match->data);
3041 p->scscr = SCSCR_RE | SCSCR_TE;
3043 if (of_find_property(np, "uart-has-rtscts", NULL))
3044 p->capabilities |= SCIx_HAVE_RTSCTS;
3049 static int sci_probe_single(struct platform_device *dev,
3051 struct plat_sci_port *p,
3052 struct sci_port *sciport)
3057 if (unlikely(index >= SCI_NPORTS)) {
3058 dev_notice(&dev->dev, "Attempting to register port %d when only %d are available\n",
3059 index+1, SCI_NPORTS);
3060 dev_notice(&dev->dev, "Consider bumping CONFIG_SERIAL_SH_SCI_NR_UARTS!\n");
3064 ret = sci_init_single(dev, sciport, index, p, false);
3068 sciport->gpios = mctrl_gpio_init(&sciport->port, 0);
3069 if (IS_ERR(sciport->gpios) && PTR_ERR(sciport->gpios) != -ENOSYS)
3070 return PTR_ERR(sciport->gpios);
3072 if (p->capabilities & SCIx_HAVE_RTSCTS) {
3073 if (!IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(sciport->gpios,
3075 !IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(sciport->gpios,
3077 dev_err(&dev->dev, "Conflicting RTS/CTS config\n");
3080 sciport->port.flags |= UPF_HARD_FLOW;
3083 ret = uart_add_one_port(&sci_uart_driver, &sciport->port);
3085 sci_cleanup_single(sciport);
3092 static int sci_probe(struct platform_device *dev)
3094 struct plat_sci_port *p;
3095 struct sci_port *sp;
3096 unsigned int dev_id;
3100 * If we've come here via earlyprintk initialization, head off to
3101 * the special early probe. We don't have sufficient device state
3102 * to make it beyond this yet.
3104 if (is_early_platform_device(dev))
3105 return sci_probe_earlyprintk(dev);
3107 if (dev->dev.of_node) {
3108 p = sci_parse_dt(dev, &dev_id);
3112 p = dev->dev.platform_data;
3114 dev_err(&dev->dev, "no platform data supplied\n");
3121 sp = &sci_ports[dev_id];
3122 platform_set_drvdata(dev, sp);
3124 ret = sci_probe_single(dev, dev_id, p, sp);
3128 #ifdef CONFIG_SH_STANDARD_BIOS
3129 sh_bios_gdb_detach();
3135 static __maybe_unused int sci_suspend(struct device *dev)
3137 struct sci_port *sport = dev_get_drvdata(dev);
3140 uart_suspend_port(&sci_uart_driver, &sport->port);
3145 static __maybe_unused int sci_resume(struct device *dev)
3147 struct sci_port *sport = dev_get_drvdata(dev);
3150 uart_resume_port(&sci_uart_driver, &sport->port);
3155 static SIMPLE_DEV_PM_OPS(sci_dev_pm_ops, sci_suspend, sci_resume);
3157 static struct platform_driver sci_driver = {
3159 .remove = sci_remove,
3162 .pm = &sci_dev_pm_ops,
3163 .of_match_table = of_match_ptr(of_sci_match),
3167 static int __init sci_init(void)
3171 pr_info("%s\n", banner);
3173 ret = uart_register_driver(&sci_uart_driver);
3174 if (likely(ret == 0)) {
3175 ret = platform_driver_register(&sci_driver);
3177 uart_unregister_driver(&sci_uart_driver);
3183 static void __exit sci_exit(void)
3185 platform_driver_unregister(&sci_driver);
3186 uart_unregister_driver(&sci_uart_driver);
3189 #ifdef CONFIG_SERIAL_SH_SCI_CONSOLE
3190 early_platform_init_buffer("earlyprintk", &sci_driver,
3191 early_serial_buf, ARRAY_SIZE(early_serial_buf));
3193 #ifdef CONFIG_SERIAL_SH_SCI_EARLYCON
3194 static struct __init plat_sci_port port_cfg;
3196 static int __init early_console_setup(struct earlycon_device *device,
3199 if (!device->port.membase)
3202 device->port.serial_in = sci_serial_in;
3203 device->port.serial_out = sci_serial_out;
3204 device->port.type = type;
3205 memcpy(&sci_ports[0].port, &device->port, sizeof(struct uart_port));
3206 sci_ports[0].cfg = &port_cfg;
3207 sci_ports[0].cfg->type = type;
3208 sci_probe_regmap(sci_ports[0].cfg);
3209 port_cfg.scscr = sci_serial_in(&sci_ports[0].port, SCSCR) |
3210 SCSCR_RE | SCSCR_TE;
3211 sci_serial_out(&sci_ports[0].port, SCSCR, port_cfg.scscr);
3213 device->con->write = serial_console_write;
3216 static int __init sci_early_console_setup(struct earlycon_device *device,
3219 return early_console_setup(device, PORT_SCI);
3221 static int __init scif_early_console_setup(struct earlycon_device *device,
3224 return early_console_setup(device, PORT_SCIF);
3226 static int __init scifa_early_console_setup(struct earlycon_device *device,
3229 return early_console_setup(device, PORT_SCIFA);
3231 static int __init scifb_early_console_setup(struct earlycon_device *device,
3234 return early_console_setup(device, PORT_SCIFB);
3236 static int __init hscif_early_console_setup(struct earlycon_device *device,
3239 return early_console_setup(device, PORT_HSCIF);
3242 OF_EARLYCON_DECLARE(sci, "renesas,sci", sci_early_console_setup);
3243 OF_EARLYCON_DECLARE(scif, "renesas,scif", scif_early_console_setup);
3244 OF_EARLYCON_DECLARE(scifa, "renesas,scifa", scifa_early_console_setup);
3245 OF_EARLYCON_DECLARE(scifb, "renesas,scifb", scifb_early_console_setup);
3246 OF_EARLYCON_DECLARE(hscif, "renesas,hscif", hscif_early_console_setup);
3247 #endif /* CONFIG_SERIAL_SH_SCI_EARLYCON */
3249 module_init(sci_init);
3250 module_exit(sci_exit);
3252 MODULE_LICENSE("GPL");
3253 MODULE_ALIAS("platform:sh-sci");
3254 MODULE_AUTHOR("Paul Mundt");
3255 MODULE_DESCRIPTION("SuperH (H)SCI(F) serial driver");