1 // SPDX-License-Identifier: GPL-2.0
3 * SuperH on-chip serial module support. (SCI with no FIFO / with FIFO)
5 * Copyright (C) 2002 - 2011 Paul Mundt
6 * Copyright (C) 2015 Glider bvba
7 * Modified to support SH7720 SCIF. Markus Brunner, Mark Jonas (Jul 2007).
9 * based off of the old drivers/char/sh-sci.c by:
11 * Copyright (C) 1999, 2000 Niibe Yutaka
12 * Copyright (C) 2000 Sugioka Toshinobu
13 * Modified to support multiple serial ports. Stuart Menefy (May 2000).
14 * Modified to support SecureEdge. David McCullough (2002)
15 * Modified to support SH7300 SCIF. Takashi Kusuda (Jun 2003).
16 * Removed SH7300 support (Jul 2007).
20 #include <linux/clk.h>
21 #include <linux/console.h>
22 #include <linux/ctype.h>
23 #include <linux/cpufreq.h>
24 #include <linux/delay.h>
25 #include <linux/dmaengine.h>
26 #include <linux/dma-mapping.h>
27 #include <linux/err.h>
28 #include <linux/errno.h>
29 #include <linux/init.h>
30 #include <linux/interrupt.h>
31 #include <linux/ioport.h>
32 #include <linux/ktime.h>
33 #include <linux/major.h>
34 #include <linux/minmax.h>
35 #include <linux/module.h>
38 #include <linux/platform_device.h>
39 #include <linux/pm_runtime.h>
40 #include <linux/reset.h>
41 #include <linux/scatterlist.h>
42 #include <linux/serial.h>
43 #include <linux/serial_sci.h>
44 #include <linux/sh_dma.h>
45 #include <linux/slab.h>
46 #include <linux/string.h>
47 #include <linux/sysrq.h>
48 #include <linux/timer.h>
49 #include <linux/tty.h>
50 #include <linux/tty_flip.h>
53 #include <asm/sh_bios.h>
54 #include <asm/platform_early.h>
57 #include "serial_mctrl_gpio.h"
60 /* Offsets into the sci_port->irqs array */
70 SCIx_MUX_IRQ = SCIx_NR_IRQS, /* special case */
73 #define SCIx_IRQ_IS_MUXED(port) \
74 ((port)->irqs[SCIx_ERI_IRQ] == \
75 (port)->irqs[SCIx_RXI_IRQ]) || \
76 ((port)->irqs[SCIx_ERI_IRQ] && \
77 ((port)->irqs[SCIx_RXI_IRQ] < 0))
80 SCI_FCK, /* Functional Clock */
81 SCI_SCK, /* Optional External Clock */
82 SCI_BRG_INT, /* Optional BRG Internal Clock Source */
83 SCI_SCIF_CLK, /* Optional BRG External Clock Source */
87 /* Bit x set means sampling rate x + 1 is supported */
88 #define SCI_SR(x) BIT((x) - 1)
89 #define SCI_SR_RANGE(x, y) GENMASK((y) - 1, (x) - 1)
91 #define SCI_SR_SCIFAB SCI_SR(5) | SCI_SR(7) | SCI_SR(11) | \
92 SCI_SR(13) | SCI_SR(16) | SCI_SR(17) | \
93 SCI_SR(19) | SCI_SR(27)
95 #define min_sr(_port) ffs((_port)->sampling_rate_mask)
96 #define max_sr(_port) fls((_port)->sampling_rate_mask)
98 /* Iterate over all supported sampling rates, from high to low */
99 #define for_each_sr(_sr, _port) \
100 for ((_sr) = max_sr(_port); (_sr) >= min_sr(_port); (_sr)--) \
101 if ((_port)->sampling_rate_mask & SCI_SR((_sr)))
103 struct plat_sci_reg {
107 struct sci_port_params {
108 const struct plat_sci_reg regs[SCIx_NR_REGS];
109 unsigned int fifosize;
110 unsigned int overrun_reg;
111 unsigned int overrun_mask;
112 unsigned int sampling_rate_mask;
113 unsigned int error_mask;
114 unsigned int error_clear;
118 struct uart_port port;
120 /* Platform configuration */
121 const struct sci_port_params *params;
122 const struct plat_sci_port *cfg;
123 unsigned int sampling_rate_mask;
124 resource_size_t reg_size;
125 struct mctrl_gpios *gpios;
128 struct clk *clks[SCI_NUM_CLKS];
129 unsigned long clk_rates[SCI_NUM_CLKS];
131 int irqs[SCIx_NR_IRQS];
132 char *irqstr[SCIx_NR_IRQS];
134 struct dma_chan *chan_tx;
135 struct dma_chan *chan_rx;
137 #ifdef CONFIG_SERIAL_SH_SCI_DMA
138 struct dma_chan *chan_tx_saved;
139 struct dma_chan *chan_rx_saved;
140 dma_cookie_t cookie_tx;
141 dma_cookie_t cookie_rx[2];
142 dma_cookie_t active_rx;
143 dma_addr_t tx_dma_addr;
144 unsigned int tx_dma_len;
145 struct scatterlist sg_rx[2];
148 struct work_struct work_tx;
149 struct hrtimer rx_timer;
150 unsigned int rx_timeout; /* microseconds */
152 unsigned int rx_frame;
154 struct timer_list rx_fifo_timer;
163 #define SCI_NPORTS CONFIG_SERIAL_SH_SCI_NR_UARTS
165 static struct sci_port sci_ports[SCI_NPORTS];
166 static unsigned long sci_ports_in_use;
167 static struct uart_driver sci_uart_driver;
169 static inline struct sci_port *
170 to_sci_port(struct uart_port *uart)
172 return container_of(uart, struct sci_port, port);
175 static const struct sci_port_params sci_port_params[SCIx_NR_REGTYPES] = {
177 * Common SCI definitions, dependent on the port's regshift
180 [SCIx_SCI_REGTYPE] = {
182 [SCSMR] = { 0x00, 8 },
183 [SCBRR] = { 0x01, 8 },
184 [SCSCR] = { 0x02, 8 },
185 [SCxTDR] = { 0x03, 8 },
186 [SCxSR] = { 0x04, 8 },
187 [SCxRDR] = { 0x05, 8 },
190 .overrun_reg = SCxSR,
191 .overrun_mask = SCI_ORER,
192 .sampling_rate_mask = SCI_SR(32),
193 .error_mask = SCI_DEFAULT_ERROR_MASK | SCI_ORER,
194 .error_clear = SCI_ERROR_CLEAR & ~SCI_ORER,
198 * Common definitions for legacy IrDA ports.
200 [SCIx_IRDA_REGTYPE] = {
202 [SCSMR] = { 0x00, 8 },
203 [SCBRR] = { 0x02, 8 },
204 [SCSCR] = { 0x04, 8 },
205 [SCxTDR] = { 0x06, 8 },
206 [SCxSR] = { 0x08, 16 },
207 [SCxRDR] = { 0x0a, 8 },
208 [SCFCR] = { 0x0c, 8 },
209 [SCFDR] = { 0x0e, 16 },
212 .overrun_reg = SCxSR,
213 .overrun_mask = SCI_ORER,
214 .sampling_rate_mask = SCI_SR(32),
215 .error_mask = SCI_DEFAULT_ERROR_MASK | SCI_ORER,
216 .error_clear = SCI_ERROR_CLEAR & ~SCI_ORER,
220 * Common SCIFA definitions.
222 [SCIx_SCIFA_REGTYPE] = {
224 [SCSMR] = { 0x00, 16 },
225 [SCBRR] = { 0x04, 8 },
226 [SCSCR] = { 0x08, 16 },
227 [SCxTDR] = { 0x20, 8 },
228 [SCxSR] = { 0x14, 16 },
229 [SCxRDR] = { 0x24, 8 },
230 [SCFCR] = { 0x18, 16 },
231 [SCFDR] = { 0x1c, 16 },
232 [SCPCR] = { 0x30, 16 },
233 [SCPDR] = { 0x34, 16 },
236 .overrun_reg = SCxSR,
237 .overrun_mask = SCIFA_ORER,
238 .sampling_rate_mask = SCI_SR_SCIFAB,
239 .error_mask = SCIF_DEFAULT_ERROR_MASK | SCIFA_ORER,
240 .error_clear = SCIF_ERROR_CLEAR & ~SCIFA_ORER,
244 * Common SCIFB definitions.
246 [SCIx_SCIFB_REGTYPE] = {
248 [SCSMR] = { 0x00, 16 },
249 [SCBRR] = { 0x04, 8 },
250 [SCSCR] = { 0x08, 16 },
251 [SCxTDR] = { 0x40, 8 },
252 [SCxSR] = { 0x14, 16 },
253 [SCxRDR] = { 0x60, 8 },
254 [SCFCR] = { 0x18, 16 },
255 [SCTFDR] = { 0x38, 16 },
256 [SCRFDR] = { 0x3c, 16 },
257 [SCPCR] = { 0x30, 16 },
258 [SCPDR] = { 0x34, 16 },
261 .overrun_reg = SCxSR,
262 .overrun_mask = SCIFA_ORER,
263 .sampling_rate_mask = SCI_SR_SCIFAB,
264 .error_mask = SCIF_DEFAULT_ERROR_MASK | SCIFA_ORER,
265 .error_clear = SCIF_ERROR_CLEAR & ~SCIFA_ORER,
269 * Common SH-2(A) SCIF definitions for ports with FIFO data
272 [SCIx_SH2_SCIF_FIFODATA_REGTYPE] = {
274 [SCSMR] = { 0x00, 16 },
275 [SCBRR] = { 0x04, 8 },
276 [SCSCR] = { 0x08, 16 },
277 [SCxTDR] = { 0x0c, 8 },
278 [SCxSR] = { 0x10, 16 },
279 [SCxRDR] = { 0x14, 8 },
280 [SCFCR] = { 0x18, 16 },
281 [SCFDR] = { 0x1c, 16 },
282 [SCSPTR] = { 0x20, 16 },
283 [SCLSR] = { 0x24, 16 },
286 .overrun_reg = SCLSR,
287 .overrun_mask = SCLSR_ORER,
288 .sampling_rate_mask = SCI_SR(32),
289 .error_mask = SCIF_DEFAULT_ERROR_MASK,
290 .error_clear = SCIF_ERROR_CLEAR,
294 * The "SCIFA" that is in RZ/A2, RZ/G2L and RZ/T.
295 * It looks like a normal SCIF with FIFO data, but with a
296 * compressed address space. Also, the break out of interrupts
297 * are different: ERI/BRI, RXI, TXI, TEI, DRI.
299 [SCIx_RZ_SCIFA_REGTYPE] = {
301 [SCSMR] = { 0x00, 16 },
302 [SCBRR] = { 0x02, 8 },
303 [SCSCR] = { 0x04, 16 },
304 [SCxTDR] = { 0x06, 8 },
305 [SCxSR] = { 0x08, 16 },
306 [SCxRDR] = { 0x0A, 8 },
307 [SCFCR] = { 0x0C, 16 },
308 [SCFDR] = { 0x0E, 16 },
309 [SCSPTR] = { 0x10, 16 },
310 [SCLSR] = { 0x12, 16 },
311 [SEMR] = { 0x14, 8 },
314 .overrun_reg = SCLSR,
315 .overrun_mask = SCLSR_ORER,
316 .sampling_rate_mask = SCI_SR(32),
317 .error_mask = SCIF_DEFAULT_ERROR_MASK,
318 .error_clear = SCIF_ERROR_CLEAR,
322 * The "SCIF" that is in RZ/V2H(P) SoC is similar to one found on RZ/G2L SoC
323 * with below differences,
324 * - Break out of interrupts are different: ERI, BRI, RXI, TXI, TEI, DRI,
325 * TEI-DRI, RXI-EDGE and TXI-EDGE.
326 * - SCSMR register does not have CM bit (BIT(7)) ie it does not support synchronous mode.
327 * - SCFCR register does not have SCFCR_MCE bit.
328 * - SCSPTR register has only bits SCSPTR_SPB2DT and SCSPTR_SPB2IO.
330 [SCIx_RZV2H_SCIF_REGTYPE] = {
332 [SCSMR] = { 0x00, 16 },
333 [SCBRR] = { 0x02, 8 },
334 [SCSCR] = { 0x04, 16 },
335 [SCxTDR] = { 0x06, 8 },
336 [SCxSR] = { 0x08, 16 },
337 [SCxRDR] = { 0x0a, 8 },
338 [SCFCR] = { 0x0c, 16 },
339 [SCFDR] = { 0x0e, 16 },
340 [SCSPTR] = { 0x10, 16 },
341 [SCLSR] = { 0x12, 16 },
342 [SEMR] = { 0x14, 8 },
345 .overrun_reg = SCLSR,
346 .overrun_mask = SCLSR_ORER,
347 .sampling_rate_mask = SCI_SR(32),
348 .error_mask = SCIF_DEFAULT_ERROR_MASK,
349 .error_clear = SCIF_ERROR_CLEAR,
353 * Common SH-3 SCIF definitions.
355 [SCIx_SH3_SCIF_REGTYPE] = {
357 [SCSMR] = { 0x00, 8 },
358 [SCBRR] = { 0x02, 8 },
359 [SCSCR] = { 0x04, 8 },
360 [SCxTDR] = { 0x06, 8 },
361 [SCxSR] = { 0x08, 16 },
362 [SCxRDR] = { 0x0a, 8 },
363 [SCFCR] = { 0x0c, 8 },
364 [SCFDR] = { 0x0e, 16 },
367 .overrun_reg = SCLSR,
368 .overrun_mask = SCLSR_ORER,
369 .sampling_rate_mask = SCI_SR(32),
370 .error_mask = SCIF_DEFAULT_ERROR_MASK,
371 .error_clear = SCIF_ERROR_CLEAR,
375 * Common SH-4(A) SCIF(B) definitions.
377 [SCIx_SH4_SCIF_REGTYPE] = {
379 [SCSMR] = { 0x00, 16 },
380 [SCBRR] = { 0x04, 8 },
381 [SCSCR] = { 0x08, 16 },
382 [SCxTDR] = { 0x0c, 8 },
383 [SCxSR] = { 0x10, 16 },
384 [SCxRDR] = { 0x14, 8 },
385 [SCFCR] = { 0x18, 16 },
386 [SCFDR] = { 0x1c, 16 },
387 [SCSPTR] = { 0x20, 16 },
388 [SCLSR] = { 0x24, 16 },
391 .overrun_reg = SCLSR,
392 .overrun_mask = SCLSR_ORER,
393 .sampling_rate_mask = SCI_SR(32),
394 .error_mask = SCIF_DEFAULT_ERROR_MASK,
395 .error_clear = SCIF_ERROR_CLEAR,
399 * Common SCIF definitions for ports with a Baud Rate Generator for
400 * External Clock (BRG).
402 [SCIx_SH4_SCIF_BRG_REGTYPE] = {
404 [SCSMR] = { 0x00, 16 },
405 [SCBRR] = { 0x04, 8 },
406 [SCSCR] = { 0x08, 16 },
407 [SCxTDR] = { 0x0c, 8 },
408 [SCxSR] = { 0x10, 16 },
409 [SCxRDR] = { 0x14, 8 },
410 [SCFCR] = { 0x18, 16 },
411 [SCFDR] = { 0x1c, 16 },
412 [SCSPTR] = { 0x20, 16 },
413 [SCLSR] = { 0x24, 16 },
414 [SCDL] = { 0x30, 16 },
415 [SCCKS] = { 0x34, 16 },
418 .overrun_reg = SCLSR,
419 .overrun_mask = SCLSR_ORER,
420 .sampling_rate_mask = SCI_SR(32),
421 .error_mask = SCIF_DEFAULT_ERROR_MASK,
422 .error_clear = SCIF_ERROR_CLEAR,
426 * Common HSCIF definitions.
428 [SCIx_HSCIF_REGTYPE] = {
430 [SCSMR] = { 0x00, 16 },
431 [SCBRR] = { 0x04, 8 },
432 [SCSCR] = { 0x08, 16 },
433 [SCxTDR] = { 0x0c, 8 },
434 [SCxSR] = { 0x10, 16 },
435 [SCxRDR] = { 0x14, 8 },
436 [SCFCR] = { 0x18, 16 },
437 [SCFDR] = { 0x1c, 16 },
438 [SCSPTR] = { 0x20, 16 },
439 [SCLSR] = { 0x24, 16 },
440 [HSSRR] = { 0x40, 16 },
441 [SCDL] = { 0x30, 16 },
442 [SCCKS] = { 0x34, 16 },
443 [HSRTRGR] = { 0x54, 16 },
444 [HSTTRGR] = { 0x58, 16 },
447 .overrun_reg = SCLSR,
448 .overrun_mask = SCLSR_ORER,
449 .sampling_rate_mask = SCI_SR_RANGE(8, 32),
450 .error_mask = SCIF_DEFAULT_ERROR_MASK,
451 .error_clear = SCIF_ERROR_CLEAR,
455 * Common SH-4(A) SCIF(B) definitions for ports without an SCSPTR
458 [SCIx_SH4_SCIF_NO_SCSPTR_REGTYPE] = {
460 [SCSMR] = { 0x00, 16 },
461 [SCBRR] = { 0x04, 8 },
462 [SCSCR] = { 0x08, 16 },
463 [SCxTDR] = { 0x0c, 8 },
464 [SCxSR] = { 0x10, 16 },
465 [SCxRDR] = { 0x14, 8 },
466 [SCFCR] = { 0x18, 16 },
467 [SCFDR] = { 0x1c, 16 },
468 [SCLSR] = { 0x24, 16 },
471 .overrun_reg = SCLSR,
472 .overrun_mask = SCLSR_ORER,
473 .sampling_rate_mask = SCI_SR(32),
474 .error_mask = SCIF_DEFAULT_ERROR_MASK,
475 .error_clear = SCIF_ERROR_CLEAR,
479 * Common SH-4(A) SCIF(B) definitions for ports with FIFO data
482 [SCIx_SH4_SCIF_FIFODATA_REGTYPE] = {
484 [SCSMR] = { 0x00, 16 },
485 [SCBRR] = { 0x04, 8 },
486 [SCSCR] = { 0x08, 16 },
487 [SCxTDR] = { 0x0c, 8 },
488 [SCxSR] = { 0x10, 16 },
489 [SCxRDR] = { 0x14, 8 },
490 [SCFCR] = { 0x18, 16 },
491 [SCFDR] = { 0x1c, 16 },
492 [SCTFDR] = { 0x1c, 16 }, /* aliased to SCFDR */
493 [SCRFDR] = { 0x20, 16 },
494 [SCSPTR] = { 0x24, 16 },
495 [SCLSR] = { 0x28, 16 },
498 .overrun_reg = SCLSR,
499 .overrun_mask = SCLSR_ORER,
500 .sampling_rate_mask = SCI_SR(32),
501 .error_mask = SCIF_DEFAULT_ERROR_MASK,
502 .error_clear = SCIF_ERROR_CLEAR,
506 * SH7705-style SCIF(B) ports, lacking both SCSPTR and SCLSR
509 [SCIx_SH7705_SCIF_REGTYPE] = {
511 [SCSMR] = { 0x00, 16 },
512 [SCBRR] = { 0x04, 8 },
513 [SCSCR] = { 0x08, 16 },
514 [SCxTDR] = { 0x20, 8 },
515 [SCxSR] = { 0x14, 16 },
516 [SCxRDR] = { 0x24, 8 },
517 [SCFCR] = { 0x18, 16 },
518 [SCFDR] = { 0x1c, 16 },
521 .overrun_reg = SCxSR,
522 .overrun_mask = SCIFA_ORER,
523 .sampling_rate_mask = SCI_SR(16),
524 .error_mask = SCIF_DEFAULT_ERROR_MASK | SCIFA_ORER,
525 .error_clear = SCIF_ERROR_CLEAR & ~SCIFA_ORER,
529 #define sci_getreg(up, offset) (&to_sci_port(up)->params->regs[offset])
532 * The "offset" here is rather misleading, in that it refers to an enum
533 * value relative to the port mapping rather than the fixed offset
534 * itself, which needs to be manually retrieved from the platform's
535 * register map for the given port.
537 static unsigned int sci_serial_in(struct uart_port *p, int offset)
539 const struct plat_sci_reg *reg = sci_getreg(p, offset);
542 return ioread8(p->membase + (reg->offset << p->regshift));
543 else if (reg->size == 16)
544 return ioread16(p->membase + (reg->offset << p->regshift));
546 WARN(1, "Invalid register access\n");
551 static void sci_serial_out(struct uart_port *p, int offset, int value)
553 const struct plat_sci_reg *reg = sci_getreg(p, offset);
556 iowrite8(value, p->membase + (reg->offset << p->regshift));
557 else if (reg->size == 16)
558 iowrite16(value, p->membase + (reg->offset << p->regshift));
560 WARN(1, "Invalid register access\n");
563 static void sci_port_enable(struct sci_port *sci_port)
567 if (!sci_port->port.dev)
570 pm_runtime_get_sync(sci_port->port.dev);
572 for (i = 0; i < SCI_NUM_CLKS; i++) {
573 clk_prepare_enable(sci_port->clks[i]);
574 sci_port->clk_rates[i] = clk_get_rate(sci_port->clks[i]);
576 sci_port->port.uartclk = sci_port->clk_rates[SCI_FCK];
579 static void sci_port_disable(struct sci_port *sci_port)
583 if (!sci_port->port.dev)
586 for (i = SCI_NUM_CLKS; i-- > 0; )
587 clk_disable_unprepare(sci_port->clks[i]);
589 pm_runtime_put_sync(sci_port->port.dev);
592 static inline unsigned long port_rx_irq_mask(struct uart_port *port)
595 * Not all ports (such as SCIFA) will support REIE. Rather than
596 * special-casing the port type, we check the port initialization
597 * IRQ enable mask to see whether the IRQ is desired at all. If
598 * it's unset, it's logically inferred that there's no point in
601 return SCSCR_RIE | (to_sci_port(port)->cfg->scscr & SCSCR_REIE);
604 static void sci_start_tx(struct uart_port *port)
606 struct sci_port *s = to_sci_port(port);
609 #ifdef CONFIG_SERIAL_SH_SCI_DMA
610 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB) {
611 u16 new, scr = sci_serial_in(port, SCSCR);
613 new = scr | SCSCR_TDRQE;
615 new = scr & ~SCSCR_TDRQE;
617 sci_serial_out(port, SCSCR, new);
620 if (s->chan_tx && !kfifo_is_empty(&port->state->port.xmit_fifo) &&
621 dma_submit_error(s->cookie_tx)) {
622 if (s->cfg->regtype == SCIx_RZ_SCIFA_REGTYPE)
623 /* Switch irq from SCIF to DMA */
624 disable_irq_nosync(s->irqs[SCIx_TXI_IRQ]);
627 schedule_work(&s->work_tx);
631 if (!s->chan_tx || s->cfg->regtype == SCIx_RZ_SCIFA_REGTYPE ||
632 port->type == PORT_SCIFA || port->type == PORT_SCIFB) {
633 /* Set TIE (Transmit Interrupt Enable) bit in SCSCR */
634 ctrl = sci_serial_in(port, SCSCR);
637 * For SCI, TE (transmit enable) must be set after setting TIE
638 * (transmit interrupt enable) or in the same instruction to start
639 * the transmit process.
641 if (port->type == PORT_SCI)
644 sci_serial_out(port, SCSCR, ctrl | SCSCR_TIE);
648 static void sci_stop_tx(struct uart_port *port)
652 /* Clear TIE (Transmit Interrupt Enable) bit in SCSCR */
653 ctrl = sci_serial_in(port, SCSCR);
655 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB)
656 ctrl &= ~SCSCR_TDRQE;
660 sci_serial_out(port, SCSCR, ctrl);
662 #ifdef CONFIG_SERIAL_SH_SCI_DMA
663 if (to_sci_port(port)->chan_tx &&
664 !dma_submit_error(to_sci_port(port)->cookie_tx)) {
665 dmaengine_terminate_async(to_sci_port(port)->chan_tx);
666 to_sci_port(port)->cookie_tx = -EINVAL;
671 static void sci_start_rx(struct uart_port *port)
675 ctrl = sci_serial_in(port, SCSCR) | port_rx_irq_mask(port);
677 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB)
678 ctrl &= ~SCSCR_RDRQE;
680 sci_serial_out(port, SCSCR, ctrl);
683 static void sci_stop_rx(struct uart_port *port)
687 ctrl = sci_serial_in(port, SCSCR);
689 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB)
690 ctrl &= ~SCSCR_RDRQE;
692 ctrl &= ~port_rx_irq_mask(port);
694 sci_serial_out(port, SCSCR, ctrl);
697 static void sci_clear_SCxSR(struct uart_port *port, unsigned int mask)
699 if (port->type == PORT_SCI) {
700 /* Just store the mask */
701 sci_serial_out(port, SCxSR, mask);
702 } else if (to_sci_port(port)->params->overrun_mask == SCIFA_ORER) {
703 /* SCIFA/SCIFB and SCIF on SH7705/SH7720/SH7721 */
704 /* Only clear the status bits we want to clear */
705 sci_serial_out(port, SCxSR, sci_serial_in(port, SCxSR) & mask);
707 /* Store the mask, clear parity/framing errors */
708 sci_serial_out(port, SCxSR, mask & ~(SCIF_FERC | SCIF_PERC));
712 #if defined(CONFIG_CONSOLE_POLL) || defined(CONFIG_SERIAL_SH_SCI_CONSOLE) || \
713 defined(CONFIG_SERIAL_SH_SCI_EARLYCON)
715 #ifdef CONFIG_CONSOLE_POLL
716 static int sci_poll_get_char(struct uart_port *port)
718 unsigned short status;
722 status = sci_serial_in(port, SCxSR);
723 if (status & SCxSR_ERRORS(port)) {
724 sci_clear_SCxSR(port, SCxSR_ERROR_CLEAR(port));
730 if (!(status & SCxSR_RDxF(port)))
733 c = sci_serial_in(port, SCxRDR);
736 sci_serial_in(port, SCxSR);
737 sci_clear_SCxSR(port, SCxSR_RDxF_CLEAR(port));
743 static void sci_poll_put_char(struct uart_port *port, unsigned char c)
745 unsigned short status;
748 status = sci_serial_in(port, SCxSR);
749 } while (!(status & SCxSR_TDxE(port)));
751 sci_serial_out(port, SCxTDR, c);
752 sci_clear_SCxSR(port, SCxSR_TDxE_CLEAR(port) & ~SCxSR_TEND(port));
754 #endif /* CONFIG_CONSOLE_POLL || CONFIG_SERIAL_SH_SCI_CONSOLE ||
755 CONFIG_SERIAL_SH_SCI_EARLYCON */
757 static void sci_init_pins(struct uart_port *port, unsigned int cflag)
759 struct sci_port *s = to_sci_port(port);
762 * Use port-specific handler if provided.
764 if (s->cfg->ops && s->cfg->ops->init_pins) {
765 s->cfg->ops->init_pins(port, cflag);
769 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB) {
770 u16 data = sci_serial_in(port, SCPDR);
771 u16 ctrl = sci_serial_in(port, SCPCR);
773 /* Enable RXD and TXD pin functions */
774 ctrl &= ~(SCPCR_RXDC | SCPCR_TXDC);
775 if (to_sci_port(port)->has_rtscts) {
776 /* RTS# is output, active low, unless autorts */
777 if (!(port->mctrl & TIOCM_RTS)) {
780 } else if (!s->autorts) {
784 /* Enable RTS# pin function */
787 /* Enable CTS# pin function */
790 sci_serial_out(port, SCPDR, data);
791 sci_serial_out(port, SCPCR, ctrl);
792 } else if (sci_getreg(port, SCSPTR)->size && s->cfg->regtype != SCIx_RZV2H_SCIF_REGTYPE) {
793 u16 status = sci_serial_in(port, SCSPTR);
795 /* RTS# is always output; and active low, unless autorts */
796 status |= SCSPTR_RTSIO;
797 if (!(port->mctrl & TIOCM_RTS))
798 status |= SCSPTR_RTSDT;
799 else if (!s->autorts)
800 status &= ~SCSPTR_RTSDT;
801 /* CTS# and SCK are inputs */
802 status &= ~(SCSPTR_CTSIO | SCSPTR_SCKIO);
803 sci_serial_out(port, SCSPTR, status);
807 static int sci_txfill(struct uart_port *port)
809 struct sci_port *s = to_sci_port(port);
810 unsigned int fifo_mask = (s->params->fifosize << 1) - 1;
811 const struct plat_sci_reg *reg;
813 reg = sci_getreg(port, SCTFDR);
815 return sci_serial_in(port, SCTFDR) & fifo_mask;
817 reg = sci_getreg(port, SCFDR);
819 return sci_serial_in(port, SCFDR) >> 8;
821 return !(sci_serial_in(port, SCxSR) & SCI_TDRE);
824 static int sci_txroom(struct uart_port *port)
826 return port->fifosize - sci_txfill(port);
829 static int sci_rxfill(struct uart_port *port)
831 struct sci_port *s = to_sci_port(port);
832 unsigned int fifo_mask = (s->params->fifosize << 1) - 1;
833 const struct plat_sci_reg *reg;
835 reg = sci_getreg(port, SCRFDR);
837 return sci_serial_in(port, SCRFDR) & fifo_mask;
839 reg = sci_getreg(port, SCFDR);
841 return sci_serial_in(port, SCFDR) & fifo_mask;
843 return (sci_serial_in(port, SCxSR) & SCxSR_RDxF(port)) != 0;
846 /* ********************************************************************** *
847 * the interrupt related routines *
848 * ********************************************************************** */
850 static void sci_transmit_chars(struct uart_port *port)
852 struct tty_port *tport = &port->state->port;
853 unsigned int stopped = uart_tx_stopped(port);
854 struct sci_port *s = to_sci_port(port);
855 unsigned short status;
859 status = sci_serial_in(port, SCxSR);
860 if (!(status & SCxSR_TDxE(port))) {
861 ctrl = sci_serial_in(port, SCSCR);
862 if (kfifo_is_empty(&tport->xmit_fifo))
866 sci_serial_out(port, SCSCR, ctrl);
870 count = sci_txroom(port);
878 } else if (stopped || !kfifo_get(&tport->xmit_fifo, &c)) {
879 if (port->type == PORT_SCI &&
880 kfifo_is_empty(&tport->xmit_fifo)) {
881 ctrl = sci_serial_in(port, SCSCR);
883 sci_serial_out(port, SCSCR, ctrl);
889 sci_serial_out(port, SCxTDR, c);
890 s->tx_occurred = true;
893 } while (--count > 0);
895 sci_clear_SCxSR(port, SCxSR_TDxE_CLEAR(port));
897 if (kfifo_len(&tport->xmit_fifo) < WAKEUP_CHARS)
898 uart_write_wakeup(port);
899 if (kfifo_is_empty(&tport->xmit_fifo)) {
900 if (port->type == PORT_SCI) {
901 ctrl = sci_serial_in(port, SCSCR);
904 sci_serial_out(port, SCSCR, ctrl);
911 static void sci_receive_chars(struct uart_port *port)
913 struct tty_port *tport = &port->state->port;
914 int i, count, copied = 0;
915 unsigned short status;
918 status = sci_serial_in(port, SCxSR);
919 if (!(status & SCxSR_RDxF(port)))
923 /* Don't copy more bytes than there is room for in the buffer */
924 count = tty_buffer_request_room(tport, sci_rxfill(port));
926 /* If for any reason we can't copy more data, we're done! */
930 if (port->type == PORT_SCI) {
931 char c = sci_serial_in(port, SCxRDR);
932 if (uart_handle_sysrq_char(port, c))
935 tty_insert_flip_char(tport, c, TTY_NORMAL);
937 for (i = 0; i < count; i++) {
940 if (port->type == PORT_SCIF ||
941 port->type == PORT_HSCIF) {
942 status = sci_serial_in(port, SCxSR);
943 c = sci_serial_in(port, SCxRDR);
945 c = sci_serial_in(port, SCxRDR);
946 status = sci_serial_in(port, SCxSR);
948 if (uart_handle_sysrq_char(port, c)) {
953 /* Store data and status */
954 if (status & SCxSR_FER(port)) {
956 port->icount.frame++;
957 } else if (status & SCxSR_PER(port)) {
959 port->icount.parity++;
963 tty_insert_flip_char(tport, c, flag);
967 sci_serial_in(port, SCxSR); /* dummy read */
968 sci_clear_SCxSR(port, SCxSR_RDxF_CLEAR(port));
971 port->icount.rx += count;
975 /* Tell the rest of the system the news. New characters! */
976 tty_flip_buffer_push(tport);
978 /* TTY buffers full; read from RX reg to prevent lockup */
979 sci_serial_in(port, SCxRDR);
980 sci_serial_in(port, SCxSR); /* dummy read */
981 sci_clear_SCxSR(port, SCxSR_RDxF_CLEAR(port));
985 static int sci_handle_errors(struct uart_port *port)
988 unsigned short status = sci_serial_in(port, SCxSR);
989 struct tty_port *tport = &port->state->port;
990 struct sci_port *s = to_sci_port(port);
992 /* Handle overruns */
993 if (status & s->params->overrun_mask) {
994 port->icount.overrun++;
997 if (tty_insert_flip_char(tport, 0, TTY_OVERRUN))
1001 if (status & SCxSR_FER(port)) {
1003 port->icount.frame++;
1005 if (tty_insert_flip_char(tport, 0, TTY_FRAME))
1009 if (status & SCxSR_PER(port)) {
1011 port->icount.parity++;
1013 if (tty_insert_flip_char(tport, 0, TTY_PARITY))
1018 tty_flip_buffer_push(tport);
1023 static int sci_handle_fifo_overrun(struct uart_port *port)
1025 struct tty_port *tport = &port->state->port;
1026 struct sci_port *s = to_sci_port(port);
1027 const struct plat_sci_reg *reg;
1031 reg = sci_getreg(port, s->params->overrun_reg);
1035 status = sci_serial_in(port, s->params->overrun_reg);
1036 if (status & s->params->overrun_mask) {
1037 status &= ~s->params->overrun_mask;
1038 sci_serial_out(port, s->params->overrun_reg, status);
1040 port->icount.overrun++;
1042 tty_insert_flip_char(tport, 0, TTY_OVERRUN);
1043 tty_flip_buffer_push(tport);
1050 static int sci_handle_breaks(struct uart_port *port)
1053 unsigned short status = sci_serial_in(port, SCxSR);
1054 struct tty_port *tport = &port->state->port;
1056 if (uart_handle_break(port))
1059 if (status & SCxSR_BRK(port)) {
1062 /* Notify of BREAK */
1063 if (tty_insert_flip_char(tport, 0, TTY_BREAK))
1068 tty_flip_buffer_push(tport);
1070 copied += sci_handle_fifo_overrun(port);
1075 static int scif_set_rtrg(struct uart_port *port, int rx_trig)
1079 if (rx_trig >= port->fifosize)
1080 rx_trig = port->fifosize - 1;
1084 /* HSCIF can be set to an arbitrary level. */
1085 if (sci_getreg(port, HSRTRGR)->size) {
1086 sci_serial_out(port, HSRTRGR, rx_trig);
1090 switch (port->type) {
1095 } else if (rx_trig < 8) {
1098 } else if (rx_trig < 14) {
1102 bits = SCFCR_RTRG0 | SCFCR_RTRG1;
1111 } else if (rx_trig < 32) {
1114 } else if (rx_trig < 48) {
1118 bits = SCFCR_RTRG0 | SCFCR_RTRG1;
1123 WARN(1, "unknown FIFO configuration");
1127 sci_serial_out(port, SCFCR,
1128 (sci_serial_in(port, SCFCR) &
1129 ~(SCFCR_RTRG1 | SCFCR_RTRG0)) | bits);
1134 static int scif_rtrg_enabled(struct uart_port *port)
1136 if (sci_getreg(port, HSRTRGR)->size)
1137 return sci_serial_in(port, HSRTRGR) != 0;
1139 return (sci_serial_in(port, SCFCR) &
1140 (SCFCR_RTRG0 | SCFCR_RTRG1)) != 0;
1143 static void rx_fifo_timer_fn(struct timer_list *t)
1145 struct sci_port *s = from_timer(s, t, rx_fifo_timer);
1146 struct uart_port *port = &s->port;
1148 dev_dbg(port->dev, "Rx timed out\n");
1149 scif_set_rtrg(port, 1);
1152 static ssize_t rx_fifo_trigger_show(struct device *dev,
1153 struct device_attribute *attr, char *buf)
1155 struct uart_port *port = dev_get_drvdata(dev);
1156 struct sci_port *sci = to_sci_port(port);
1158 return sprintf(buf, "%d\n", sci->rx_trigger);
1161 static ssize_t rx_fifo_trigger_store(struct device *dev,
1162 struct device_attribute *attr,
1163 const char *buf, size_t count)
1165 struct uart_port *port = dev_get_drvdata(dev);
1166 struct sci_port *sci = to_sci_port(port);
1170 ret = kstrtol(buf, 0, &r);
1174 sci->rx_trigger = scif_set_rtrg(port, r);
1175 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB)
1176 scif_set_rtrg(port, 1);
1181 static DEVICE_ATTR_RW(rx_fifo_trigger);
1183 static ssize_t rx_fifo_timeout_show(struct device *dev,
1184 struct device_attribute *attr,
1187 struct uart_port *port = dev_get_drvdata(dev);
1188 struct sci_port *sci = to_sci_port(port);
1191 if (port->type == PORT_HSCIF)
1192 v = sci->hscif_tot >> HSSCR_TOT_SHIFT;
1194 v = sci->rx_fifo_timeout;
1196 return sprintf(buf, "%d\n", v);
1199 static ssize_t rx_fifo_timeout_store(struct device *dev,
1200 struct device_attribute *attr,
1204 struct uart_port *port = dev_get_drvdata(dev);
1205 struct sci_port *sci = to_sci_port(port);
1209 ret = kstrtol(buf, 0, &r);
1213 if (port->type == PORT_HSCIF) {
1216 sci->hscif_tot = r << HSSCR_TOT_SHIFT;
1218 sci->rx_fifo_timeout = r;
1219 scif_set_rtrg(port, 1);
1221 timer_setup(&sci->rx_fifo_timer, rx_fifo_timer_fn, 0);
1227 static DEVICE_ATTR_RW(rx_fifo_timeout);
1230 #ifdef CONFIG_SERIAL_SH_SCI_DMA
1231 static void sci_dma_tx_complete(void *arg)
1233 struct sci_port *s = arg;
1234 struct uart_port *port = &s->port;
1235 struct tty_port *tport = &port->state->port;
1236 unsigned long flags;
1238 dev_dbg(port->dev, "%s(%d)\n", __func__, port->line);
1240 uart_port_lock_irqsave(port, &flags);
1242 uart_xmit_advance(port, s->tx_dma_len);
1244 if (kfifo_len(&tport->xmit_fifo) < WAKEUP_CHARS)
1245 uart_write_wakeup(port);
1247 s->tx_occurred = true;
1249 if (!kfifo_is_empty(&tport->xmit_fifo)) {
1251 schedule_work(&s->work_tx);
1253 s->cookie_tx = -EINVAL;
1254 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB ||
1255 s->cfg->regtype == SCIx_RZ_SCIFA_REGTYPE) {
1256 u16 ctrl = sci_serial_in(port, SCSCR);
1257 sci_serial_out(port, SCSCR, ctrl & ~SCSCR_TIE);
1258 if (s->cfg->regtype == SCIx_RZ_SCIFA_REGTYPE) {
1259 /* Switch irq from DMA to SCIF */
1260 dmaengine_pause(s->chan_tx_saved);
1261 enable_irq(s->irqs[SCIx_TXI_IRQ]);
1266 uart_port_unlock_irqrestore(port, flags);
1269 /* Locking: called with port lock held */
1270 static int sci_dma_rx_push(struct sci_port *s, void *buf, size_t count)
1272 struct uart_port *port = &s->port;
1273 struct tty_port *tport = &port->state->port;
1276 copied = tty_insert_flip_string(tport, buf, count);
1278 port->icount.buf_overrun++;
1280 port->icount.rx += copied;
1285 static int sci_dma_rx_find_active(struct sci_port *s)
1289 for (i = 0; i < ARRAY_SIZE(s->cookie_rx); i++)
1290 if (s->active_rx == s->cookie_rx[i])
1296 /* Must only be called with uart_port_lock taken */
1297 static void sci_dma_rx_chan_invalidate(struct sci_port *s)
1302 for (i = 0; i < ARRAY_SIZE(s->cookie_rx); i++)
1303 s->cookie_rx[i] = -EINVAL;
1307 static void sci_dma_rx_release(struct sci_port *s)
1309 struct dma_chan *chan = s->chan_rx_saved;
1310 struct uart_port *port = &s->port;
1311 unsigned long flags;
1313 uart_port_lock_irqsave(port, &flags);
1314 s->chan_rx_saved = NULL;
1315 sci_dma_rx_chan_invalidate(s);
1316 uart_port_unlock_irqrestore(port, flags);
1318 dmaengine_terminate_sync(chan);
1319 dma_free_coherent(chan->device->dev, s->buf_len_rx * 2, s->rx_buf[0],
1320 sg_dma_address(&s->sg_rx[0]));
1321 dma_release_channel(chan);
1324 static void start_hrtimer_us(struct hrtimer *hrt, unsigned long usec)
1326 long sec = usec / 1000000;
1327 long nsec = (usec % 1000000) * 1000;
1328 ktime_t t = ktime_set(sec, nsec);
1330 hrtimer_start(hrt, t, HRTIMER_MODE_REL);
1333 static void sci_dma_rx_reenable_irq(struct sci_port *s)
1335 struct uart_port *port = &s->port;
1338 /* Direct new serial port interrupts back to CPU */
1339 scr = sci_serial_in(port, SCSCR);
1340 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB ||
1341 s->cfg->regtype == SCIx_RZ_SCIFA_REGTYPE) {
1342 enable_irq(s->irqs[SCIx_RXI_IRQ]);
1343 if (s->cfg->regtype == SCIx_RZ_SCIFA_REGTYPE)
1344 scif_set_rtrg(port, s->rx_trigger);
1346 scr &= ~SCSCR_RDRQE;
1348 sci_serial_out(port, SCSCR, scr | SCSCR_RIE);
1351 static void sci_dma_rx_complete(void *arg)
1353 struct sci_port *s = arg;
1354 struct dma_chan *chan = s->chan_rx;
1355 struct uart_port *port = &s->port;
1356 struct dma_async_tx_descriptor *desc;
1357 unsigned long flags;
1358 int active, count = 0;
1360 dev_dbg(port->dev, "%s(%d) active cookie %d\n", __func__, port->line,
1363 hrtimer_cancel(&s->rx_timer);
1365 uart_port_lock_irqsave(port, &flags);
1367 active = sci_dma_rx_find_active(s);
1369 count = sci_dma_rx_push(s, s->rx_buf[active], s->buf_len_rx);
1372 tty_flip_buffer_push(&port->state->port);
1374 desc = dmaengine_prep_slave_sg(s->chan_rx, &s->sg_rx[active], 1,
1376 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
1380 desc->callback = sci_dma_rx_complete;
1381 desc->callback_param = s;
1382 s->cookie_rx[active] = dmaengine_submit(desc);
1383 if (dma_submit_error(s->cookie_rx[active]))
1386 s->active_rx = s->cookie_rx[!active];
1388 dma_async_issue_pending(chan);
1390 uart_port_unlock_irqrestore(port, flags);
1391 dev_dbg(port->dev, "%s: cookie %d #%d, new active cookie %d\n",
1392 __func__, s->cookie_rx[active], active, s->active_rx);
1394 start_hrtimer_us(&s->rx_timer, s->rx_timeout);
1400 dmaengine_terminate_async(chan);
1401 sci_dma_rx_chan_invalidate(s);
1402 sci_dma_rx_reenable_irq(s);
1403 uart_port_unlock_irqrestore(port, flags);
1404 dev_warn(port->dev, "Failed submitting Rx DMA descriptor\n");
1407 static void sci_dma_tx_release(struct sci_port *s)
1409 struct dma_chan *chan = s->chan_tx_saved;
1411 cancel_work_sync(&s->work_tx);
1412 s->chan_tx_saved = s->chan_tx = NULL;
1413 s->cookie_tx = -EINVAL;
1414 dmaengine_terminate_sync(chan);
1415 dma_unmap_single(chan->device->dev, s->tx_dma_addr, UART_XMIT_SIZE,
1417 dma_release_channel(chan);
1420 static int sci_dma_rx_submit(struct sci_port *s, bool port_lock_held)
1422 struct dma_chan *chan = s->chan_rx;
1423 struct uart_port *port = &s->port;
1424 unsigned long flags;
1427 for (i = 0; i < 2; i++) {
1428 struct scatterlist *sg = &s->sg_rx[i];
1429 struct dma_async_tx_descriptor *desc;
1431 desc = dmaengine_prep_slave_sg(chan,
1432 sg, 1, DMA_DEV_TO_MEM,
1433 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
1437 desc->callback = sci_dma_rx_complete;
1438 desc->callback_param = s;
1439 s->cookie_rx[i] = dmaengine_submit(desc);
1440 if (dma_submit_error(s->cookie_rx[i]))
1445 s->active_rx = s->cookie_rx[0];
1447 dma_async_issue_pending(chan);
1452 if (!port_lock_held)
1453 uart_port_lock_irqsave(port, &flags);
1455 dmaengine_terminate_async(chan);
1456 sci_dma_rx_chan_invalidate(s);
1458 if (!port_lock_held)
1459 uart_port_unlock_irqrestore(port, flags);
1463 static void sci_dma_tx_work_fn(struct work_struct *work)
1465 struct sci_port *s = container_of(work, struct sci_port, work_tx);
1466 struct dma_async_tx_descriptor *desc;
1467 struct dma_chan *chan = s->chan_tx;
1468 struct uart_port *port = &s->port;
1469 struct tty_port *tport = &port->state->port;
1470 unsigned long flags;
1476 * Port xmit buffer is already mapped, and it is one page... Just adjust
1477 * offsets and lengths. Since it is a circular buffer, we have to
1478 * transmit till the end, and then the rest. Take the port lock to get a
1479 * consistent xmit buffer state.
1481 uart_port_lock_irq(port);
1482 s->tx_dma_len = kfifo_out_linear(&tport->xmit_fifo, &tail,
1484 buf = s->tx_dma_addr + tail;
1485 if (!s->tx_dma_len) {
1486 /* Transmit buffer has been flushed */
1487 uart_port_unlock_irq(port);
1491 desc = dmaengine_prep_slave_single(chan, buf, s->tx_dma_len,
1493 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
1495 uart_port_unlock_irq(port);
1496 dev_warn(port->dev, "Failed preparing Tx DMA descriptor\n");
1500 dma_sync_single_for_device(chan->device->dev, buf, s->tx_dma_len,
1503 desc->callback = sci_dma_tx_complete;
1504 desc->callback_param = s;
1505 s->cookie_tx = dmaengine_submit(desc);
1506 if (dma_submit_error(s->cookie_tx)) {
1507 uart_port_unlock_irq(port);
1508 dev_warn(port->dev, "Failed submitting Tx DMA descriptor\n");
1512 uart_port_unlock_irq(port);
1513 dev_dbg(port->dev, "%s: %p: %u, cookie %d\n",
1514 __func__, tport->xmit_buf, tail, s->cookie_tx);
1516 dma_async_issue_pending(chan);
1520 uart_port_lock_irqsave(port, &flags);
1523 uart_port_unlock_irqrestore(port, flags);
1527 static enum hrtimer_restart sci_dma_rx_timer_fn(struct hrtimer *t)
1529 struct sci_port *s = container_of(t, struct sci_port, rx_timer);
1530 struct dma_chan *chan = s->chan_rx;
1531 struct uart_port *port = &s->port;
1532 struct dma_tx_state state;
1533 enum dma_status status;
1534 unsigned long flags;
1538 dev_dbg(port->dev, "DMA Rx timed out\n");
1540 uart_port_lock_irqsave(port, &flags);
1542 active = sci_dma_rx_find_active(s);
1544 uart_port_unlock_irqrestore(port, flags);
1545 return HRTIMER_NORESTART;
1548 status = dmaengine_tx_status(s->chan_rx, s->active_rx, &state);
1549 if (status == DMA_COMPLETE) {
1550 uart_port_unlock_irqrestore(port, flags);
1551 dev_dbg(port->dev, "Cookie %d #%d has already completed\n",
1552 s->active_rx, active);
1554 /* Let packet complete handler take care of the packet */
1555 return HRTIMER_NORESTART;
1558 dmaengine_pause(chan);
1561 * sometimes DMA transfer doesn't stop even if it is stopped and
1562 * data keeps on coming until transaction is complete so check
1563 * for DMA_COMPLETE again
1564 * Let packet complete handler take care of the packet
1566 status = dmaengine_tx_status(s->chan_rx, s->active_rx, &state);
1567 if (status == DMA_COMPLETE) {
1568 uart_port_unlock_irqrestore(port, flags);
1569 dev_dbg(port->dev, "Transaction complete after DMA engine was stopped");
1570 return HRTIMER_NORESTART;
1573 /* Handle incomplete DMA receive */
1574 dmaengine_terminate_async(s->chan_rx);
1575 read = sg_dma_len(&s->sg_rx[active]) - state.residue;
1578 count = sci_dma_rx_push(s, s->rx_buf[active], read);
1580 tty_flip_buffer_push(&port->state->port);
1583 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB ||
1584 s->cfg->regtype == SCIx_RZ_SCIFA_REGTYPE)
1585 sci_dma_rx_submit(s, true);
1587 sci_dma_rx_reenable_irq(s);
1589 uart_port_unlock_irqrestore(port, flags);
1591 return HRTIMER_NORESTART;
1594 static struct dma_chan *sci_request_dma_chan(struct uart_port *port,
1595 enum dma_transfer_direction dir)
1597 struct dma_chan *chan;
1598 struct dma_slave_config cfg;
1601 chan = dma_request_chan(port->dev, dir == DMA_MEM_TO_DEV ? "tx" : "rx");
1603 dev_dbg(port->dev, "dma_request_chan failed\n");
1607 memset(&cfg, 0, sizeof(cfg));
1608 cfg.direction = dir;
1609 cfg.dst_addr = port->mapbase +
1610 (sci_getreg(port, SCxTDR)->offset << port->regshift);
1611 cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
1612 cfg.src_addr = port->mapbase +
1613 (sci_getreg(port, SCxRDR)->offset << port->regshift);
1614 cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
1616 ret = dmaengine_slave_config(chan, &cfg);
1618 dev_warn(port->dev, "dmaengine_slave_config failed %d\n", ret);
1619 dma_release_channel(chan);
1626 static void sci_request_dma(struct uart_port *port)
1628 struct sci_port *s = to_sci_port(port);
1629 struct tty_port *tport = &port->state->port;
1630 struct dma_chan *chan;
1632 dev_dbg(port->dev, "%s: port %d\n", __func__, port->line);
1635 * DMA on console may interfere with Kernel log messages which use
1636 * plain putchar(). So, simply don't use it with a console.
1638 if (uart_console(port))
1641 if (!port->dev->of_node)
1644 s->cookie_tx = -EINVAL;
1647 * Don't request a dma channel if no channel was specified
1648 * in the device tree.
1650 if (!of_property_present(port->dev->of_node, "dmas"))
1653 chan = sci_request_dma_chan(port, DMA_MEM_TO_DEV);
1654 dev_dbg(port->dev, "%s: TX: got channel %p\n", __func__, chan);
1656 /* UART circular tx buffer is an aligned page. */
1657 s->tx_dma_addr = dma_map_single(chan->device->dev,
1661 if (dma_mapping_error(chan->device->dev, s->tx_dma_addr)) {
1662 dev_warn(port->dev, "Failed mapping Tx DMA descriptor\n");
1663 dma_release_channel(chan);
1665 dev_dbg(port->dev, "%s: mapped %lu@%p to %pad\n",
1666 __func__, UART_XMIT_SIZE,
1667 tport->xmit_buf, &s->tx_dma_addr);
1669 INIT_WORK(&s->work_tx, sci_dma_tx_work_fn);
1670 s->chan_tx_saved = s->chan_tx = chan;
1674 chan = sci_request_dma_chan(port, DMA_DEV_TO_MEM);
1675 dev_dbg(port->dev, "%s: RX: got channel %p\n", __func__, chan);
1681 s->buf_len_rx = 2 * max_t(size_t, 16, port->fifosize);
1682 buf = dma_alloc_coherent(chan->device->dev, s->buf_len_rx * 2,
1686 "Failed to allocate Rx dma buffer, using PIO\n");
1687 dma_release_channel(chan);
1691 for (i = 0; i < 2; i++) {
1692 struct scatterlist *sg = &s->sg_rx[i];
1694 sg_init_table(sg, 1);
1696 sg_dma_address(sg) = dma;
1697 sg_dma_len(sg) = s->buf_len_rx;
1699 buf += s->buf_len_rx;
1700 dma += s->buf_len_rx;
1703 hrtimer_init(&s->rx_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1704 s->rx_timer.function = sci_dma_rx_timer_fn;
1706 s->chan_rx_saved = s->chan_rx = chan;
1708 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB ||
1709 s->cfg->regtype == SCIx_RZ_SCIFA_REGTYPE)
1710 sci_dma_rx_submit(s, false);
1714 static void sci_free_dma(struct uart_port *port)
1716 struct sci_port *s = to_sci_port(port);
1718 if (s->chan_tx_saved)
1719 sci_dma_tx_release(s);
1720 if (s->chan_rx_saved)
1721 sci_dma_rx_release(s);
1724 static void sci_flush_buffer(struct uart_port *port)
1726 struct sci_port *s = to_sci_port(port);
1729 * In uart_flush_buffer(), the xmit circular buffer has just been
1730 * cleared, so we have to reset tx_dma_len accordingly, and stop any
1735 dmaengine_terminate_async(s->chan_tx);
1736 s->cookie_tx = -EINVAL;
1740 static void sci_dma_check_tx_occurred(struct sci_port *s)
1742 struct dma_tx_state state;
1743 enum dma_status status;
1748 status = dmaengine_tx_status(s->chan_tx, s->cookie_tx, &state);
1749 if (status == DMA_COMPLETE || status == DMA_IN_PROGRESS)
1750 s->tx_occurred = true;
1752 #else /* !CONFIG_SERIAL_SH_SCI_DMA */
1753 static inline void sci_request_dma(struct uart_port *port)
1757 static inline void sci_free_dma(struct uart_port *port)
1761 static void sci_dma_check_tx_occurred(struct sci_port *s)
1765 #define sci_flush_buffer NULL
1766 #endif /* !CONFIG_SERIAL_SH_SCI_DMA */
1768 static irqreturn_t sci_rx_interrupt(int irq, void *ptr)
1770 struct uart_port *port = ptr;
1771 struct sci_port *s = to_sci_port(port);
1773 #ifdef CONFIG_SERIAL_SH_SCI_DMA
1775 u16 scr = sci_serial_in(port, SCSCR);
1776 u16 ssr = sci_serial_in(port, SCxSR);
1778 /* Disable future Rx interrupts */
1779 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB ||
1780 s->cfg->regtype == SCIx_RZ_SCIFA_REGTYPE) {
1781 disable_irq_nosync(s->irqs[SCIx_RXI_IRQ]);
1782 if (s->cfg->regtype == SCIx_RZ_SCIFA_REGTYPE) {
1783 scif_set_rtrg(port, 1);
1789 if (sci_dma_rx_submit(s, false) < 0)
1794 sci_serial_out(port, SCSCR, scr);
1795 /* Clear current interrupt */
1796 sci_serial_out(port, SCxSR,
1797 ssr & ~(SCIF_DR | SCxSR_RDxF(port)));
1798 dev_dbg(port->dev, "Rx IRQ %lu: setup t-out in %u us\n",
1799 jiffies, s->rx_timeout);
1800 start_hrtimer_us(&s->rx_timer, s->rx_timeout);
1808 if (s->rx_trigger > 1 && s->rx_fifo_timeout > 0) {
1809 if (!scif_rtrg_enabled(port))
1810 scif_set_rtrg(port, s->rx_trigger);
1812 mod_timer(&s->rx_fifo_timer, jiffies + DIV_ROUND_UP(
1813 s->rx_frame * HZ * s->rx_fifo_timeout, 1000000));
1816 /* I think sci_receive_chars has to be called irrespective
1817 * of whether the I_IXOFF is set, otherwise, how is the interrupt
1820 sci_receive_chars(port);
1825 static irqreturn_t sci_tx_interrupt(int irq, void *ptr)
1827 struct uart_port *port = ptr;
1828 unsigned long flags;
1830 uart_port_lock_irqsave(port, &flags);
1831 sci_transmit_chars(port);
1832 uart_port_unlock_irqrestore(port, flags);
1837 static irqreturn_t sci_tx_end_interrupt(int irq, void *ptr)
1839 struct uart_port *port = ptr;
1840 unsigned long flags;
1841 unsigned short ctrl;
1843 if (port->type != PORT_SCI)
1844 return sci_tx_interrupt(irq, ptr);
1846 uart_port_lock_irqsave(port, &flags);
1847 ctrl = sci_serial_in(port, SCSCR);
1848 ctrl &= ~(SCSCR_TE | SCSCR_TEIE);
1849 sci_serial_out(port, SCSCR, ctrl);
1850 uart_port_unlock_irqrestore(port, flags);
1855 static irqreturn_t sci_br_interrupt(int irq, void *ptr)
1857 struct uart_port *port = ptr;
1860 sci_handle_breaks(port);
1862 /* drop invalid character received before break was detected */
1863 sci_serial_in(port, SCxRDR);
1865 sci_clear_SCxSR(port, SCxSR_BREAK_CLEAR(port));
1870 static irqreturn_t sci_er_interrupt(int irq, void *ptr)
1872 struct uart_port *port = ptr;
1873 struct sci_port *s = to_sci_port(port);
1875 if (s->irqs[SCIx_ERI_IRQ] == s->irqs[SCIx_BRI_IRQ]) {
1876 /* Break and Error interrupts are muxed */
1877 unsigned short ssr_status = sci_serial_in(port, SCxSR);
1879 /* Break Interrupt */
1880 if (ssr_status & SCxSR_BRK(port))
1881 sci_br_interrupt(irq, ptr);
1884 if (!(ssr_status & SCxSR_ERRORS(port)))
1889 if (port->type == PORT_SCI) {
1890 if (sci_handle_errors(port)) {
1891 /* discard character in rx buffer */
1892 sci_serial_in(port, SCxSR);
1893 sci_clear_SCxSR(port, SCxSR_RDxF_CLEAR(port));
1896 sci_handle_fifo_overrun(port);
1898 sci_receive_chars(port);
1901 sci_clear_SCxSR(port, SCxSR_ERROR_CLEAR(port));
1903 /* Kick the transmission */
1905 sci_tx_interrupt(irq, ptr);
1910 static irqreturn_t sci_mpxed_interrupt(int irq, void *ptr)
1912 unsigned short ssr_status, scr_status, err_enabled, orer_status = 0;
1913 struct uart_port *port = ptr;
1914 struct sci_port *s = to_sci_port(port);
1915 irqreturn_t ret = IRQ_NONE;
1917 ssr_status = sci_serial_in(port, SCxSR);
1918 scr_status = sci_serial_in(port, SCSCR);
1919 if (s->params->overrun_reg == SCxSR)
1920 orer_status = ssr_status;
1921 else if (sci_getreg(port, s->params->overrun_reg)->size)
1922 orer_status = sci_serial_in(port, s->params->overrun_reg);
1924 err_enabled = scr_status & port_rx_irq_mask(port);
1927 if ((ssr_status & SCxSR_TDxE(port)) && (scr_status & SCSCR_TIE) &&
1929 ret = sci_tx_interrupt(irq, ptr);
1932 * Rx Interrupt: if we're using DMA, the DMA controller clears RDF /
1935 if (((ssr_status & SCxSR_RDxF(port)) || s->chan_rx) &&
1936 (scr_status & SCSCR_RIE))
1937 ret = sci_rx_interrupt(irq, ptr);
1939 /* Error Interrupt */
1940 if ((ssr_status & SCxSR_ERRORS(port)) && err_enabled)
1941 ret = sci_er_interrupt(irq, ptr);
1943 /* Break Interrupt */
1944 if (s->irqs[SCIx_ERI_IRQ] != s->irqs[SCIx_BRI_IRQ] &&
1945 (ssr_status & SCxSR_BRK(port)) && err_enabled)
1946 ret = sci_br_interrupt(irq, ptr);
1948 /* Overrun Interrupt */
1949 if (orer_status & s->params->overrun_mask) {
1950 sci_handle_fifo_overrun(port);
1957 static const struct sci_irq_desc {
1959 irq_handler_t handler;
1960 } sci_irq_desc[] = {
1962 * Split out handlers, the default case.
1966 .handler = sci_er_interrupt,
1971 .handler = sci_rx_interrupt,
1976 .handler = sci_tx_interrupt,
1981 .handler = sci_br_interrupt,
1986 .handler = sci_rx_interrupt,
1991 .handler = sci_tx_end_interrupt,
1995 * Special muxed handler.
1999 .handler = sci_mpxed_interrupt,
2003 static int sci_request_irq(struct sci_port *port)
2005 struct uart_port *up = &port->port;
2006 int i, j, w, ret = 0;
2008 for (i = j = 0; i < SCIx_NR_IRQS; i++, j++) {
2009 const struct sci_irq_desc *desc;
2012 /* Check if already registered (muxed) */
2013 for (w = 0; w < i; w++)
2014 if (port->irqs[w] == port->irqs[i])
2019 if (SCIx_IRQ_IS_MUXED(port)) {
2023 irq = port->irqs[i];
2026 * Certain port types won't support all of the
2027 * available interrupt sources.
2029 if (unlikely(irq < 0))
2033 desc = sci_irq_desc + i;
2034 port->irqstr[j] = kasprintf(GFP_KERNEL, "%s:%s",
2035 dev_name(up->dev), desc->desc);
2036 if (!port->irqstr[j]) {
2041 ret = request_irq(irq, desc->handler, up->irqflags,
2042 port->irqstr[j], port);
2043 if (unlikely(ret)) {
2044 dev_err(up->dev, "Can't allocate %s IRQ\n", desc->desc);
2053 free_irq(port->irqs[i], port);
2057 kfree(port->irqstr[j]);
2062 static void sci_free_irq(struct sci_port *port)
2067 * Intentionally in reverse order so we iterate over the muxed
2070 for (i = 0; i < SCIx_NR_IRQS; i++) {
2071 int irq = port->irqs[i];
2074 * Certain port types won't support all of the available
2075 * interrupt sources.
2077 if (unlikely(irq < 0))
2080 /* Check if already freed (irq was muxed) */
2081 for (j = 0; j < i; j++)
2082 if (port->irqs[j] == irq)
2087 free_irq(port->irqs[i], port);
2088 kfree(port->irqstr[i]);
2090 if (SCIx_IRQ_IS_MUXED(port)) {
2091 /* If there's only one IRQ, we're done. */
2097 static unsigned int sci_tx_empty(struct uart_port *port)
2099 unsigned short status = sci_serial_in(port, SCxSR);
2100 unsigned short in_tx_fifo = sci_txfill(port);
2101 struct sci_port *s = to_sci_port(port);
2103 sci_dma_check_tx_occurred(s);
2105 if (!s->tx_occurred)
2106 return TIOCSER_TEMT;
2108 return (status & SCxSR_TEND(port)) && !in_tx_fifo ? TIOCSER_TEMT : 0;
2111 static void sci_set_rts(struct uart_port *port, bool state)
2113 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB) {
2114 u16 data = sci_serial_in(port, SCPDR);
2118 data &= ~SCPDR_RTSD;
2121 sci_serial_out(port, SCPDR, data);
2123 /* RTS# is output */
2124 sci_serial_out(port, SCPCR,
2125 sci_serial_in(port, SCPCR) | SCPCR_RTSC);
2126 } else if (sci_getreg(port, SCSPTR)->size) {
2127 u16 ctrl = sci_serial_in(port, SCSPTR);
2131 ctrl &= ~SCSPTR_RTSDT;
2133 ctrl |= SCSPTR_RTSDT;
2134 sci_serial_out(port, SCSPTR, ctrl);
2138 static bool sci_get_cts(struct uart_port *port)
2140 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB) {
2142 return !(sci_serial_in(port, SCPDR) & SCPDR_CTSD);
2143 } else if (sci_getreg(port, SCSPTR)->size) {
2145 return !(sci_serial_in(port, SCSPTR) & SCSPTR_CTSDT);
2152 * Modem control is a bit of a mixed bag for SCI(F) ports. Generally
2153 * CTS/RTS is supported in hardware by at least one port and controlled
2154 * via SCSPTR (SCxPCR for SCIFA/B parts), or external pins (presently
2155 * handled via the ->init_pins() op, which is a bit of a one-way street,
2156 * lacking any ability to defer pin control -- this will later be
2157 * converted over to the GPIO framework).
2159 * Other modes (such as loopback) are supported generically on certain
2160 * port types, but not others. For these it's sufficient to test for the
2161 * existence of the support register and simply ignore the port type.
2163 static void sci_set_mctrl(struct uart_port *port, unsigned int mctrl)
2165 struct sci_port *s = to_sci_port(port);
2167 if (mctrl & TIOCM_LOOP) {
2168 const struct plat_sci_reg *reg;
2171 * Standard loopback mode for SCFCR ports.
2173 reg = sci_getreg(port, SCFCR);
2175 sci_serial_out(port, SCFCR,
2176 sci_serial_in(port, SCFCR) | SCFCR_LOOP);
2179 mctrl_gpio_set(s->gpios, mctrl);
2184 if (!(mctrl & TIOCM_RTS)) {
2185 /* Disable Auto RTS */
2186 if (s->cfg->regtype != SCIx_RZV2H_SCIF_REGTYPE)
2187 sci_serial_out(port, SCFCR,
2188 sci_serial_in(port, SCFCR) & ~SCFCR_MCE);
2191 sci_set_rts(port, 0);
2192 } else if (s->autorts) {
2193 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB) {
2194 /* Enable RTS# pin function */
2195 sci_serial_out(port, SCPCR,
2196 sci_serial_in(port, SCPCR) & ~SCPCR_RTSC);
2199 /* Enable Auto RTS */
2200 if (s->cfg->regtype != SCIx_RZV2H_SCIF_REGTYPE)
2201 sci_serial_out(port, SCFCR,
2202 sci_serial_in(port, SCFCR) | SCFCR_MCE);
2205 sci_set_rts(port, 1);
2209 static unsigned int sci_get_mctrl(struct uart_port *port)
2211 struct sci_port *s = to_sci_port(port);
2212 struct mctrl_gpios *gpios = s->gpios;
2213 unsigned int mctrl = 0;
2215 mctrl_gpio_get(gpios, &mctrl);
2218 * CTS/RTS is handled in hardware when supported, while nothing
2222 if (sci_get_cts(port))
2224 } else if (!mctrl_gpio_to_gpiod(gpios, UART_GPIO_CTS)) {
2227 if (!mctrl_gpio_to_gpiod(gpios, UART_GPIO_DSR))
2229 if (!mctrl_gpio_to_gpiod(gpios, UART_GPIO_DCD))
2235 static void sci_enable_ms(struct uart_port *port)
2237 mctrl_gpio_enable_ms(to_sci_port(port)->gpios);
2240 static void sci_break_ctl(struct uart_port *port, int break_state)
2242 unsigned short scscr, scsptr;
2243 unsigned long flags;
2245 /* check whether the port has SCSPTR */
2246 if (!sci_getreg(port, SCSPTR)->size) {
2248 * Not supported by hardware. Most parts couple break and rx
2249 * interrupts together, with break detection always enabled.
2254 uart_port_lock_irqsave(port, &flags);
2255 scsptr = sci_serial_in(port, SCSPTR);
2256 scscr = sci_serial_in(port, SCSCR);
2258 if (break_state == -1) {
2259 scsptr = (scsptr | SCSPTR_SPB2IO) & ~SCSPTR_SPB2DT;
2262 scsptr = (scsptr | SCSPTR_SPB2DT) & ~SCSPTR_SPB2IO;
2266 sci_serial_out(port, SCSPTR, scsptr);
2267 sci_serial_out(port, SCSCR, scscr);
2268 uart_port_unlock_irqrestore(port, flags);
2271 static int sci_startup(struct uart_port *port)
2273 struct sci_port *s = to_sci_port(port);
2276 dev_dbg(port->dev, "%s(%d)\n", __func__, port->line);
2278 s->tx_occurred = false;
2279 sci_request_dma(port);
2281 ret = sci_request_irq(s);
2282 if (unlikely(ret < 0)) {
2290 static void sci_shutdown(struct uart_port *port)
2292 struct sci_port *s = to_sci_port(port);
2293 unsigned long flags;
2296 dev_dbg(port->dev, "%s(%d)\n", __func__, port->line);
2299 mctrl_gpio_disable_ms(to_sci_port(port)->gpios);
2301 uart_port_lock_irqsave(port, &flags);
2305 * Stop RX and TX, disable related interrupts, keep clock source
2306 * and HSCIF TOT bits
2308 scr = sci_serial_in(port, SCSCR);
2309 sci_serial_out(port, SCSCR,
2310 scr & (SCSCR_CKE1 | SCSCR_CKE0 | s->hscif_tot));
2311 uart_port_unlock_irqrestore(port, flags);
2313 #ifdef CONFIG_SERIAL_SH_SCI_DMA
2314 if (s->chan_rx_saved) {
2315 dev_dbg(port->dev, "%s(%d) deleting rx_timer\n", __func__,
2317 hrtimer_cancel(&s->rx_timer);
2321 if (s->rx_trigger > 1 && s->rx_fifo_timeout > 0)
2322 del_timer_sync(&s->rx_fifo_timer);
2327 static int sci_sck_calc(struct sci_port *s, unsigned int bps,
2330 unsigned long freq = s->clk_rates[SCI_SCK];
2331 int err, min_err = INT_MAX;
2334 if (s->port.type != PORT_HSCIF)
2337 for_each_sr(sr, s) {
2338 err = DIV_ROUND_CLOSEST(freq, sr) - bps;
2339 if (abs(err) >= abs(min_err))
2349 dev_dbg(s->port.dev, "SCK: %u%+d bps using SR %u\n", bps, min_err,
2354 static int sci_brg_calc(struct sci_port *s, unsigned int bps,
2355 unsigned long freq, unsigned int *dlr,
2358 int err, min_err = INT_MAX;
2359 unsigned int sr, dl;
2361 if (s->port.type != PORT_HSCIF)
2364 for_each_sr(sr, s) {
2365 dl = DIV_ROUND_CLOSEST(freq, sr * bps);
2366 dl = clamp(dl, 1U, 65535U);
2368 err = DIV_ROUND_CLOSEST(freq, sr * dl) - bps;
2369 if (abs(err) >= abs(min_err))
2380 dev_dbg(s->port.dev, "BRG: %u%+d bps using DL %u SR %u\n", bps,
2381 min_err, *dlr, *srr + 1);
2385 /* calculate sample rate, BRR, and clock select */
2386 static int sci_scbrr_calc(struct sci_port *s, unsigned int bps,
2387 unsigned int *brr, unsigned int *srr,
2390 unsigned long freq = s->clk_rates[SCI_FCK];
2391 unsigned int sr, br, prediv, scrate, c;
2392 int err, min_err = INT_MAX;
2394 if (s->port.type != PORT_HSCIF)
2398 * Find the combination of sample rate and clock select with the
2399 * smallest deviation from the desired baud rate.
2400 * Prefer high sample rates to maximise the receive margin.
2402 * M: Receive margin (%)
2403 * N: Ratio of bit rate to clock (N = sampling rate)
2404 * D: Clock duty (D = 0 to 1.0)
2405 * L: Frame length (L = 9 to 12)
2406 * F: Absolute value of clock frequency deviation
2408 * M = |(0.5 - 1 / 2 * N) - ((L - 0.5) * F) -
2409 * (|D - 0.5| / N * (1 + F))|
2410 * NOTE: Usually, treat D for 0.5, F is 0 by this calculation.
2412 for_each_sr(sr, s) {
2413 for (c = 0; c <= 3; c++) {
2414 /* integerized formulas from HSCIF documentation */
2415 prediv = sr << (2 * c + 1);
2418 * We need to calculate:
2420 * br = freq / (prediv * bps) clamped to [1..256]
2421 * err = freq / (br * prediv) - bps
2423 * Watch out for overflow when calculating the desired
2424 * sampling clock rate!
2426 if (bps > UINT_MAX / prediv)
2429 scrate = prediv * bps;
2430 br = DIV_ROUND_CLOSEST(freq, scrate);
2431 br = clamp(br, 1U, 256U);
2433 err = DIV_ROUND_CLOSEST(freq, br * prediv) - bps;
2434 if (abs(err) >= abs(min_err))
2448 dev_dbg(s->port.dev, "BRR: %u%+d bps using N %u SR %u cks %u\n", bps,
2449 min_err, *brr, *srr + 1, *cks);
2453 static void sci_reset(struct uart_port *port)
2455 const struct plat_sci_reg *reg;
2456 unsigned int status;
2457 struct sci_port *s = to_sci_port(port);
2459 sci_serial_out(port, SCSCR, s->hscif_tot); /* TE=0, RE=0, CKE1=0 */
2461 reg = sci_getreg(port, SCFCR);
2463 sci_serial_out(port, SCFCR, SCFCR_RFRST | SCFCR_TFRST);
2465 sci_clear_SCxSR(port,
2466 SCxSR_RDxF_CLEAR(port) & SCxSR_ERROR_CLEAR(port) &
2467 SCxSR_BREAK_CLEAR(port));
2468 if (sci_getreg(port, SCLSR)->size) {
2469 status = sci_serial_in(port, SCLSR);
2470 status &= ~(SCLSR_TO | SCLSR_ORER);
2471 sci_serial_out(port, SCLSR, status);
2474 if (s->rx_trigger > 1) {
2475 if (s->rx_fifo_timeout) {
2476 scif_set_rtrg(port, 1);
2477 timer_setup(&s->rx_fifo_timer, rx_fifo_timer_fn, 0);
2479 if (port->type == PORT_SCIFA ||
2480 port->type == PORT_SCIFB)
2481 scif_set_rtrg(port, 1);
2483 scif_set_rtrg(port, s->rx_trigger);
2488 static void sci_set_termios(struct uart_port *port, struct ktermios *termios,
2489 const struct ktermios *old)
2491 unsigned int baud, smr_val = SCSMR_ASYNC, scr_val = 0, i, bits;
2492 unsigned int brr = 255, cks = 0, srr = 15, dl = 0, sccks = 0;
2493 unsigned int brr1 = 255, cks1 = 0, srr1 = 15, dl1 = 0;
2494 struct sci_port *s = to_sci_port(port);
2495 const struct plat_sci_reg *reg;
2496 int min_err = INT_MAX, err;
2497 unsigned long max_freq = 0;
2499 unsigned long flags;
2501 if ((termios->c_cflag & CSIZE) == CS7) {
2502 smr_val |= SCSMR_CHR;
2504 termios->c_cflag &= ~CSIZE;
2505 termios->c_cflag |= CS8;
2507 if (termios->c_cflag & PARENB)
2508 smr_val |= SCSMR_PE;
2509 if (termios->c_cflag & PARODD)
2510 smr_val |= SCSMR_PE | SCSMR_ODD;
2511 if (termios->c_cflag & CSTOPB)
2512 smr_val |= SCSMR_STOP;
2515 * earlyprintk comes here early on with port->uartclk set to zero.
2516 * the clock framework is not up and running at this point so here
2517 * we assume that 115200 is the maximum baud rate. please note that
2518 * the baud rate is not programmed during earlyprintk - it is assumed
2519 * that the previous boot loader has enabled required clocks and
2520 * setup the baud rate generator hardware for us already.
2522 if (!port->uartclk) {
2523 baud = uart_get_baud_rate(port, termios, old, 0, 115200);
2527 for (i = 0; i < SCI_NUM_CLKS; i++)
2528 max_freq = max(max_freq, s->clk_rates[i]);
2530 baud = uart_get_baud_rate(port, termios, old, 0, max_freq / min_sr(s));
2535 * There can be multiple sources for the sampling clock. Find the one
2536 * that gives us the smallest deviation from the desired baud rate.
2539 /* Optional Undivided External Clock */
2540 if (s->clk_rates[SCI_SCK] && port->type != PORT_SCIFA &&
2541 port->type != PORT_SCIFB) {
2542 err = sci_sck_calc(s, baud, &srr1);
2543 if (abs(err) < abs(min_err)) {
2545 scr_val = SCSCR_CKE1;
2554 /* Optional BRG Frequency Divided External Clock */
2555 if (s->clk_rates[SCI_SCIF_CLK] && sci_getreg(port, SCDL)->size) {
2556 err = sci_brg_calc(s, baud, s->clk_rates[SCI_SCIF_CLK], &dl1,
2558 if (abs(err) < abs(min_err)) {
2559 best_clk = SCI_SCIF_CLK;
2560 scr_val = SCSCR_CKE1;
2570 /* Optional BRG Frequency Divided Internal Clock */
2571 if (s->clk_rates[SCI_BRG_INT] && sci_getreg(port, SCDL)->size) {
2572 err = sci_brg_calc(s, baud, s->clk_rates[SCI_BRG_INT], &dl1,
2574 if (abs(err) < abs(min_err)) {
2575 best_clk = SCI_BRG_INT;
2576 scr_val = SCSCR_CKE1;
2586 /* Divided Functional Clock using standard Bit Rate Register */
2587 err = sci_scbrr_calc(s, baud, &brr1, &srr1, &cks1);
2588 if (abs(err) < abs(min_err)) {
2599 dev_dbg(port->dev, "Using clk %pC for %u%+d bps\n",
2600 s->clks[best_clk], baud, min_err);
2605 * Program the optional External Baud Rate Generator (BRG) first.
2606 * It controls the mux to select (H)SCK or frequency divided clock.
2608 if (best_clk >= 0 && sci_getreg(port, SCCKS)->size) {
2609 sci_serial_out(port, SCDL, dl);
2610 sci_serial_out(port, SCCKS, sccks);
2613 uart_port_lock_irqsave(port, &flags);
2617 uart_update_timeout(port, termios->c_cflag, baud);
2619 /* byte size and parity */
2620 bits = tty_get_frame_size(termios->c_cflag);
2622 if (sci_getreg(port, SEMR)->size)
2623 sci_serial_out(port, SEMR, 0);
2625 if (best_clk >= 0) {
2626 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB)
2628 case 5: smr_val |= SCSMR_SRC_5; break;
2629 case 7: smr_val |= SCSMR_SRC_7; break;
2630 case 11: smr_val |= SCSMR_SRC_11; break;
2631 case 13: smr_val |= SCSMR_SRC_13; break;
2632 case 16: smr_val |= SCSMR_SRC_16; break;
2633 case 17: smr_val |= SCSMR_SRC_17; break;
2634 case 19: smr_val |= SCSMR_SRC_19; break;
2635 case 27: smr_val |= SCSMR_SRC_27; break;
2638 sci_serial_out(port, SCSCR, scr_val | s->hscif_tot);
2639 sci_serial_out(port, SCSMR, smr_val);
2640 sci_serial_out(port, SCBRR, brr);
2641 if (sci_getreg(port, HSSRR)->size) {
2642 unsigned int hssrr = srr | HSCIF_SRE;
2643 /* Calculate deviation from intended rate at the
2644 * center of the last stop bit in sampling clocks.
2646 int last_stop = bits * 2 - 1;
2647 int deviation = DIV_ROUND_CLOSEST(min_err * last_stop *
2651 if (abs(deviation) >= 2) {
2652 /* At least two sampling clocks off at the
2653 * last stop bit; we can increase the error
2654 * margin by shifting the sampling point.
2656 int shift = clamp(deviation / 2, -8, 7);
2658 hssrr |= (shift << HSCIF_SRHP_SHIFT) &
2660 hssrr |= HSCIF_SRDE;
2662 sci_serial_out(port, HSSRR, hssrr);
2665 /* Wait one bit interval */
2666 udelay((1000000 + (baud - 1)) / baud);
2668 /* Don't touch the bit rate configuration */
2669 scr_val = s->cfg->scscr & (SCSCR_CKE1 | SCSCR_CKE0);
2670 smr_val |= sci_serial_in(port, SCSMR) &
2671 (SCSMR_CKEDG | SCSMR_SRC_MASK | SCSMR_CKS);
2672 sci_serial_out(port, SCSCR, scr_val | s->hscif_tot);
2673 sci_serial_out(port, SCSMR, smr_val);
2676 sci_init_pins(port, termios->c_cflag);
2678 port->status &= ~UPSTAT_AUTOCTS;
2680 reg = sci_getreg(port, SCFCR);
2682 unsigned short ctrl = sci_serial_in(port, SCFCR);
2684 if ((port->flags & UPF_HARD_FLOW) &&
2685 (termios->c_cflag & CRTSCTS)) {
2686 /* There is no CTS interrupt to restart the hardware */
2687 port->status |= UPSTAT_AUTOCTS;
2688 /* MCE is enabled when RTS is raised */
2693 * As we've done a sci_reset() above, ensure we don't
2694 * interfere with the FIFOs while toggling MCE. As the
2695 * reset values could still be set, simply mask them out.
2697 ctrl &= ~(SCFCR_RFRST | SCFCR_TFRST);
2699 sci_serial_out(port, SCFCR, ctrl);
2701 if (port->flags & UPF_HARD_FLOW) {
2702 /* Refresh (Auto) RTS */
2703 sci_set_mctrl(port, port->mctrl);
2707 * For SCI, TE (transmit enable) must be set after setting TIE
2708 * (transmit interrupt enable) or in the same instruction to
2709 * start the transmitting process. So skip setting TE here for SCI.
2711 if (port->type != PORT_SCI)
2712 scr_val |= SCSCR_TE;
2713 scr_val |= SCSCR_RE | (s->cfg->scscr & ~(SCSCR_CKE1 | SCSCR_CKE0));
2714 sci_serial_out(port, SCSCR, scr_val | s->hscif_tot);
2715 if ((srr + 1 == 5) &&
2716 (port->type == PORT_SCIFA || port->type == PORT_SCIFB)) {
2718 * In asynchronous mode, when the sampling rate is 1/5, first
2719 * received data may become invalid on some SCIFA and SCIFB.
2720 * To avoid this problem wait more than 1 serial data time (1
2721 * bit time x serial data number) after setting SCSCR.RE = 1.
2723 udelay(DIV_ROUND_UP(10 * 1000000, baud));
2726 /* Calculate delay for 2 DMA buffers (4 FIFO). */
2727 s->rx_frame = (10000 * bits) / (baud / 100);
2728 #ifdef CONFIG_SERIAL_SH_SCI_DMA
2729 s->rx_timeout = s->buf_len_rx * 2 * s->rx_frame;
2732 if ((termios->c_cflag & CREAD) != 0)
2735 uart_port_unlock_irqrestore(port, flags);
2737 sci_port_disable(s);
2739 if (UART_ENABLE_MS(port, termios->c_cflag))
2740 sci_enable_ms(port);
2743 static void sci_pm(struct uart_port *port, unsigned int state,
2744 unsigned int oldstate)
2746 struct sci_port *sci_port = to_sci_port(port);
2749 case UART_PM_STATE_OFF:
2750 sci_port_disable(sci_port);
2753 sci_port_enable(sci_port);
2758 static const char *sci_type(struct uart_port *port)
2760 switch (port->type) {
2778 static int sci_remap_port(struct uart_port *port)
2780 struct sci_port *sport = to_sci_port(port);
2783 * Nothing to do if there's already an established membase.
2788 if (port->dev->of_node || (port->flags & UPF_IOREMAP)) {
2789 port->membase = ioremap(port->mapbase, sport->reg_size);
2790 if (unlikely(!port->membase)) {
2791 dev_err(port->dev, "can't remap port#%d\n", port->line);
2796 * For the simple (and majority of) cases where we don't
2797 * need to do any remapping, just cast the cookie
2800 port->membase = (void __iomem *)(uintptr_t)port->mapbase;
2806 static void sci_release_port(struct uart_port *port)
2808 struct sci_port *sport = to_sci_port(port);
2810 if (port->dev->of_node || (port->flags & UPF_IOREMAP)) {
2811 iounmap(port->membase);
2812 port->membase = NULL;
2815 release_mem_region(port->mapbase, sport->reg_size);
2818 static int sci_request_port(struct uart_port *port)
2820 struct resource *res;
2821 struct sci_port *sport = to_sci_port(port);
2824 res = request_mem_region(port->mapbase, sport->reg_size,
2825 dev_name(port->dev));
2826 if (unlikely(res == NULL)) {
2827 dev_err(port->dev, "request_mem_region failed.");
2831 ret = sci_remap_port(port);
2832 if (unlikely(ret != 0)) {
2833 release_resource(res);
2840 static void sci_config_port(struct uart_port *port, int flags)
2842 if (flags & UART_CONFIG_TYPE) {
2843 struct sci_port *sport = to_sci_port(port);
2845 port->type = sport->cfg->type;
2846 sci_request_port(port);
2850 static int sci_verify_port(struct uart_port *port, struct serial_struct *ser)
2852 if (ser->baud_base < 2400)
2853 /* No paper tape reader for Mitch.. */
2859 static const struct uart_ops sci_uart_ops = {
2860 .tx_empty = sci_tx_empty,
2861 .set_mctrl = sci_set_mctrl,
2862 .get_mctrl = sci_get_mctrl,
2863 .start_tx = sci_start_tx,
2864 .stop_tx = sci_stop_tx,
2865 .stop_rx = sci_stop_rx,
2866 .enable_ms = sci_enable_ms,
2867 .break_ctl = sci_break_ctl,
2868 .startup = sci_startup,
2869 .shutdown = sci_shutdown,
2870 .flush_buffer = sci_flush_buffer,
2871 .set_termios = sci_set_termios,
2874 .release_port = sci_release_port,
2875 .request_port = sci_request_port,
2876 .config_port = sci_config_port,
2877 .verify_port = sci_verify_port,
2878 #ifdef CONFIG_CONSOLE_POLL
2879 .poll_get_char = sci_poll_get_char,
2880 .poll_put_char = sci_poll_put_char,
2884 static int sci_init_clocks(struct sci_port *sci_port, struct device *dev)
2886 const char *clk_names[] = {
2889 [SCI_BRG_INT] = "brg_int",
2890 [SCI_SCIF_CLK] = "scif_clk",
2895 if (sci_port->cfg->type == PORT_HSCIF)
2896 clk_names[SCI_SCK] = "hsck";
2898 for (i = 0; i < SCI_NUM_CLKS; i++) {
2899 clk = devm_clk_get_optional(dev, clk_names[i]);
2901 return PTR_ERR(clk);
2903 if (!clk && i == SCI_FCK) {
2905 * Not all SH platforms declare a clock lookup entry
2906 * for SCI devices, in which case we need to get the
2907 * global "peripheral_clk" clock.
2909 clk = devm_clk_get(dev, "peripheral_clk");
2911 return dev_err_probe(dev, PTR_ERR(clk),
2912 "failed to get %s\n",
2917 dev_dbg(dev, "failed to get %s\n", clk_names[i]);
2919 dev_dbg(dev, "clk %s is %pC rate %lu\n", clk_names[i],
2920 clk, clk_get_rate(clk));
2921 sci_port->clks[i] = clk;
2926 static const struct sci_port_params *
2927 sci_probe_regmap(const struct plat_sci_port *cfg)
2929 unsigned int regtype;
2931 if (cfg->regtype != SCIx_PROBE_REGTYPE)
2932 return &sci_port_params[cfg->regtype];
2934 switch (cfg->type) {
2936 regtype = SCIx_SCI_REGTYPE;
2939 regtype = SCIx_IRDA_REGTYPE;
2942 regtype = SCIx_SCIFA_REGTYPE;
2945 regtype = SCIx_SCIFB_REGTYPE;
2949 * The SH-4 is a bit of a misnomer here, although that's
2950 * where this particular port layout originated. This
2951 * configuration (or some slight variation thereof)
2952 * remains the dominant model for all SCIFs.
2954 regtype = SCIx_SH4_SCIF_REGTYPE;
2957 regtype = SCIx_HSCIF_REGTYPE;
2960 pr_err("Can't probe register map for given port\n");
2964 return &sci_port_params[regtype];
2967 static int sci_init_single(struct platform_device *dev,
2968 struct sci_port *sci_port, unsigned int index,
2969 const struct plat_sci_port *p, bool early)
2971 struct uart_port *port = &sci_port->port;
2972 const struct resource *res;
2978 port->ops = &sci_uart_ops;
2979 port->iotype = UPIO_MEM;
2981 port->has_sysrq = IS_ENABLED(CONFIG_SERIAL_SH_SCI_CONSOLE);
2983 res = platform_get_resource(dev, IORESOURCE_MEM, 0);
2987 port->mapbase = res->start;
2988 sci_port->reg_size = resource_size(res);
2990 for (i = 0; i < ARRAY_SIZE(sci_port->irqs); ++i) {
2992 sci_port->irqs[i] = platform_get_irq_optional(dev, i);
2994 sci_port->irqs[i] = platform_get_irq(dev, i);
2998 * The fourth interrupt on SCI port is transmit end interrupt, so
2999 * shuffle the interrupts.
3001 if (p->type == PORT_SCI)
3002 swap(sci_port->irqs[SCIx_BRI_IRQ], sci_port->irqs[SCIx_TEI_IRQ]);
3004 /* The SCI generates several interrupts. They can be muxed together or
3005 * connected to different interrupt lines. In the muxed case only one
3006 * interrupt resource is specified as there is only one interrupt ID.
3007 * In the non-muxed case, up to 6 interrupt signals might be generated
3008 * from the SCI, however those signals might have their own individual
3009 * interrupt ID numbers, or muxed together with another interrupt.
3011 if (sci_port->irqs[0] < 0)
3014 if (sci_port->irqs[1] < 0)
3015 for (i = 1; i < ARRAY_SIZE(sci_port->irqs); i++)
3016 sci_port->irqs[i] = sci_port->irqs[0];
3018 sci_port->params = sci_probe_regmap(p);
3019 if (unlikely(sci_port->params == NULL))
3024 sci_port->rx_trigger = 48;
3027 sci_port->rx_trigger = 64;
3030 sci_port->rx_trigger = 32;
3033 if (p->regtype == SCIx_SH7705_SCIF_REGTYPE)
3034 /* RX triggering not implemented for this IP */
3035 sci_port->rx_trigger = 1;
3037 sci_port->rx_trigger = 8;
3040 sci_port->rx_trigger = 1;
3044 sci_port->rx_fifo_timeout = 0;
3045 sci_port->hscif_tot = 0;
3047 /* SCIFA on sh7723 and sh7724 need a custom sampling rate that doesn't
3048 * match the SoC datasheet, this should be investigated. Let platform
3049 * data override the sampling rate for now.
3051 sci_port->sampling_rate_mask = p->sampling_rate
3052 ? SCI_SR(p->sampling_rate)
3053 : sci_port->params->sampling_rate_mask;
3056 ret = sci_init_clocks(sci_port, &dev->dev);
3060 port->dev = &dev->dev;
3062 pm_runtime_enable(&dev->dev);
3065 port->type = p->type;
3066 port->flags = UPF_FIXED_PORT | UPF_BOOT_AUTOCONF | p->flags;
3067 port->fifosize = sci_port->params->fifosize;
3069 if (port->type == PORT_SCI && !dev->dev.of_node) {
3070 if (sci_port->reg_size >= 0x20)
3077 * The UART port needs an IRQ value, so we peg this to the RX IRQ
3078 * for the multi-IRQ ports, which is where we are primarily
3079 * concerned with the shutdown path synchronization.
3081 * For the muxed case there's nothing more to do.
3083 port->irq = sci_port->irqs[SCIx_RXI_IRQ];
3089 static void sci_cleanup_single(struct sci_port *port)
3091 pm_runtime_disable(port->port.dev);
3094 #if defined(CONFIG_SERIAL_SH_SCI_CONSOLE) || \
3095 defined(CONFIG_SERIAL_SH_SCI_EARLYCON)
3096 static void serial_console_putchar(struct uart_port *port, unsigned char ch)
3098 sci_poll_put_char(port, ch);
3102 * Print a string to the serial port trying not to disturb
3103 * any possible real use of the port...
3105 static void serial_console_write(struct console *co, const char *s,
3108 struct sci_port *sci_port = &sci_ports[co->index];
3109 struct uart_port *port = &sci_port->port;
3110 unsigned short bits, ctrl, ctrl_temp;
3111 unsigned long flags;
3116 else if (oops_in_progress)
3117 locked = uart_port_trylock_irqsave(port, &flags);
3119 uart_port_lock_irqsave(port, &flags);
3121 /* first save SCSCR then disable interrupts, keep clock source */
3122 ctrl = sci_serial_in(port, SCSCR);
3123 ctrl_temp = SCSCR_RE | SCSCR_TE |
3124 (sci_port->cfg->scscr & ~(SCSCR_CKE1 | SCSCR_CKE0)) |
3125 (ctrl & (SCSCR_CKE1 | SCSCR_CKE0));
3126 sci_serial_out(port, SCSCR, ctrl_temp | sci_port->hscif_tot);
3128 uart_console_write(port, s, count, serial_console_putchar);
3130 /* wait until fifo is empty and last bit has been transmitted */
3131 bits = SCxSR_TDxE(port) | SCxSR_TEND(port);
3132 while ((sci_serial_in(port, SCxSR) & bits) != bits)
3135 /* restore the SCSCR */
3136 sci_serial_out(port, SCSCR, ctrl);
3139 uart_port_unlock_irqrestore(port, flags);
3142 static int serial_console_setup(struct console *co, char *options)
3144 struct sci_port *sci_port;
3145 struct uart_port *port;
3153 * Refuse to handle any bogus ports.
3155 if (co->index < 0 || co->index >= SCI_NPORTS)
3158 sci_port = &sci_ports[co->index];
3159 port = &sci_port->port;
3162 * Refuse to handle uninitialized ports.
3167 ret = sci_remap_port(port);
3168 if (unlikely(ret != 0))
3172 uart_parse_options(options, &baud, &parity, &bits, &flow);
3174 return uart_set_options(port, co, baud, parity, bits, flow);
3177 static struct console serial_console = {
3179 .device = uart_console_device,
3180 .write = serial_console_write,
3181 .setup = serial_console_setup,
3182 .flags = CON_PRINTBUFFER,
3184 .data = &sci_uart_driver,
3187 #ifdef CONFIG_SUPERH
3188 static char early_serial_buf[32];
3190 static int early_serial_console_setup(struct console *co, char *options)
3193 * This early console is always registered using the earlyprintk=
3194 * parameter, which does not call add_preferred_console(). Thus
3195 * @options is always NULL and the options for this early console
3196 * are passed using a custom buffer.
3200 return serial_console_setup(co, early_serial_buf);
3203 static struct console early_serial_console = {
3204 .name = "early_ttySC",
3205 .write = serial_console_write,
3206 .setup = early_serial_console_setup,
3207 .flags = CON_PRINTBUFFER,
3211 static int sci_probe_earlyprintk(struct platform_device *pdev)
3213 const struct plat_sci_port *cfg = dev_get_platdata(&pdev->dev);
3215 if (early_serial_console.data)
3218 early_serial_console.index = pdev->id;
3220 sci_init_single(pdev, &sci_ports[pdev->id], pdev->id, cfg, true);
3222 if (!strstr(early_serial_buf, "keep"))
3223 early_serial_console.flags |= CON_BOOT;
3225 register_console(&early_serial_console);
3230 #define SCI_CONSOLE (&serial_console)
3233 static inline int sci_probe_earlyprintk(struct platform_device *pdev)
3238 #define SCI_CONSOLE NULL
3240 #endif /* CONFIG_SERIAL_SH_SCI_CONSOLE || CONFIG_SERIAL_SH_SCI_EARLYCON */
3242 static const char banner[] __initconst = "SuperH (H)SCI(F) driver initialized";
3244 static DEFINE_MUTEX(sci_uart_registration_lock);
3245 static struct uart_driver sci_uart_driver = {
3246 .owner = THIS_MODULE,
3247 .driver_name = "sci",
3248 .dev_name = "ttySC",
3250 .minor = SCI_MINOR_START,
3252 .cons = SCI_CONSOLE,
3255 static void sci_remove(struct platform_device *dev)
3257 struct sci_port *port = platform_get_drvdata(dev);
3258 unsigned int type = port->port.type; /* uart_remove_... clears it */
3260 sci_ports_in_use &= ~BIT(port->port.line);
3261 uart_remove_one_port(&sci_uart_driver, &port->port);
3263 sci_cleanup_single(port);
3265 if (port->port.fifosize > 1)
3266 device_remove_file(&dev->dev, &dev_attr_rx_fifo_trigger);
3267 if (type == PORT_SCIFA || type == PORT_SCIFB || type == PORT_HSCIF)
3268 device_remove_file(&dev->dev, &dev_attr_rx_fifo_timeout);
3272 #define SCI_OF_DATA(type, regtype) (void *)((type) << 16 | (regtype))
3273 #define SCI_OF_TYPE(data) ((unsigned long)(data) >> 16)
3274 #define SCI_OF_REGTYPE(data) ((unsigned long)(data) & 0xffff)
3276 static const struct of_device_id of_sci_match[] __maybe_unused = {
3277 /* SoC-specific types */
3279 .compatible = "renesas,scif-r7s72100",
3280 .data = SCI_OF_DATA(PORT_SCIF, SCIx_SH2_SCIF_FIFODATA_REGTYPE),
3283 .compatible = "renesas,scif-r7s9210",
3284 .data = SCI_OF_DATA(PORT_SCIF, SCIx_RZ_SCIFA_REGTYPE),
3287 .compatible = "renesas,scif-r9a07g044",
3288 .data = SCI_OF_DATA(PORT_SCIF, SCIx_RZ_SCIFA_REGTYPE),
3291 .compatible = "renesas,scif-r9a09g057",
3292 .data = SCI_OF_DATA(PORT_SCIF, SCIx_RZV2H_SCIF_REGTYPE),
3294 /* Family-specific types */
3296 .compatible = "renesas,rcar-gen1-scif",
3297 .data = SCI_OF_DATA(PORT_SCIF, SCIx_SH4_SCIF_BRG_REGTYPE),
3299 .compatible = "renesas,rcar-gen2-scif",
3300 .data = SCI_OF_DATA(PORT_SCIF, SCIx_SH4_SCIF_BRG_REGTYPE),
3302 .compatible = "renesas,rcar-gen3-scif",
3303 .data = SCI_OF_DATA(PORT_SCIF, SCIx_SH4_SCIF_BRG_REGTYPE),
3305 .compatible = "renesas,rcar-gen4-scif",
3306 .data = SCI_OF_DATA(PORT_SCIF, SCIx_SH4_SCIF_BRG_REGTYPE),
3310 .compatible = "renesas,scif",
3311 .data = SCI_OF_DATA(PORT_SCIF, SCIx_SH4_SCIF_REGTYPE),
3313 .compatible = "renesas,scifa",
3314 .data = SCI_OF_DATA(PORT_SCIFA, SCIx_SCIFA_REGTYPE),
3316 .compatible = "renesas,scifb",
3317 .data = SCI_OF_DATA(PORT_SCIFB, SCIx_SCIFB_REGTYPE),
3319 .compatible = "renesas,hscif",
3320 .data = SCI_OF_DATA(PORT_HSCIF, SCIx_HSCIF_REGTYPE),
3322 .compatible = "renesas,sci",
3323 .data = SCI_OF_DATA(PORT_SCI, SCIx_SCI_REGTYPE),
3328 MODULE_DEVICE_TABLE(of, of_sci_match);
3330 static void sci_reset_control_assert(void *data)
3332 reset_control_assert(data);
3335 static struct plat_sci_port *sci_parse_dt(struct platform_device *pdev,
3336 unsigned int *dev_id)
3338 struct device_node *np = pdev->dev.of_node;
3339 struct reset_control *rstc;
3340 struct plat_sci_port *p;
3341 struct sci_port *sp;
3345 if (!IS_ENABLED(CONFIG_OF) || !np)
3346 return ERR_PTR(-EINVAL);
3348 data = of_device_get_match_data(&pdev->dev);
3350 rstc = devm_reset_control_get_optional_exclusive(&pdev->dev, NULL);
3352 return ERR_PTR(dev_err_probe(&pdev->dev, PTR_ERR(rstc),
3353 "failed to get reset ctrl\n"));
3355 ret = reset_control_deassert(rstc);
3357 dev_err(&pdev->dev, "failed to deassert reset %d\n", ret);
3358 return ERR_PTR(ret);
3361 ret = devm_add_action_or_reset(&pdev->dev, sci_reset_control_assert, rstc);
3363 dev_err(&pdev->dev, "failed to register assert devm action, %d\n",
3365 return ERR_PTR(ret);
3368 p = devm_kzalloc(&pdev->dev, sizeof(struct plat_sci_port), GFP_KERNEL);
3370 return ERR_PTR(-ENOMEM);
3372 /* Get the line number from the aliases node. */
3373 id = of_alias_get_id(np, "serial");
3374 if (id < 0 && ~sci_ports_in_use)
3375 id = ffz(sci_ports_in_use);
3377 dev_err(&pdev->dev, "failed to get alias id (%d)\n", id);
3378 return ERR_PTR(-EINVAL);
3380 if (id >= ARRAY_SIZE(sci_ports)) {
3381 dev_err(&pdev->dev, "serial%d out of range\n", id);
3382 return ERR_PTR(-EINVAL);
3385 sp = &sci_ports[id];
3388 p->type = SCI_OF_TYPE(data);
3389 p->regtype = SCI_OF_REGTYPE(data);
3391 sp->has_rtscts = of_property_read_bool(np, "uart-has-rtscts");
3396 static int sci_probe_single(struct platform_device *dev,
3398 struct plat_sci_port *p,
3399 struct sci_port *sciport)
3404 if (unlikely(index >= SCI_NPORTS)) {
3405 dev_notice(&dev->dev, "Attempting to register port %d when only %d are available\n",
3406 index+1, SCI_NPORTS);
3407 dev_notice(&dev->dev, "Consider bumping CONFIG_SERIAL_SH_SCI_NR_UARTS!\n");
3410 BUILD_BUG_ON(SCI_NPORTS > sizeof(sci_ports_in_use) * 8);
3411 if (sci_ports_in_use & BIT(index))
3414 mutex_lock(&sci_uart_registration_lock);
3415 if (!sci_uart_driver.state) {
3416 ret = uart_register_driver(&sci_uart_driver);
3418 mutex_unlock(&sci_uart_registration_lock);
3422 mutex_unlock(&sci_uart_registration_lock);
3424 ret = sci_init_single(dev, sciport, index, p, false);
3428 sciport->gpios = mctrl_gpio_init(&sciport->port, 0);
3429 if (IS_ERR(sciport->gpios))
3430 return PTR_ERR(sciport->gpios);
3432 if (sciport->has_rtscts) {
3433 if (mctrl_gpio_to_gpiod(sciport->gpios, UART_GPIO_CTS) ||
3434 mctrl_gpio_to_gpiod(sciport->gpios, UART_GPIO_RTS)) {
3435 dev_err(&dev->dev, "Conflicting RTS/CTS config\n");
3438 sciport->port.flags |= UPF_HARD_FLOW;
3441 ret = uart_add_one_port(&sci_uart_driver, &sciport->port);
3443 sci_cleanup_single(sciport);
3450 static int sci_probe(struct platform_device *dev)
3452 struct plat_sci_port *p;
3453 struct sci_port *sp;
3454 unsigned int dev_id;
3458 * If we've come here via earlyprintk initialization, head off to
3459 * the special early probe. We don't have sufficient device state
3460 * to make it beyond this yet.
3462 #ifdef CONFIG_SUPERH
3463 if (is_sh_early_platform_device(dev))
3464 return sci_probe_earlyprintk(dev);
3467 if (dev->dev.of_node) {
3468 p = sci_parse_dt(dev, &dev_id);
3472 p = dev->dev.platform_data;
3474 dev_err(&dev->dev, "no platform data supplied\n");
3481 sp = &sci_ports[dev_id];
3482 platform_set_drvdata(dev, sp);
3484 ret = sci_probe_single(dev, dev_id, p, sp);
3488 if (sp->port.fifosize > 1) {
3489 ret = device_create_file(&dev->dev, &dev_attr_rx_fifo_trigger);
3493 if (sp->port.type == PORT_SCIFA || sp->port.type == PORT_SCIFB ||
3494 sp->port.type == PORT_HSCIF) {
3495 ret = device_create_file(&dev->dev, &dev_attr_rx_fifo_timeout);
3497 if (sp->port.fifosize > 1) {
3498 device_remove_file(&dev->dev,
3499 &dev_attr_rx_fifo_trigger);
3505 #ifdef CONFIG_SH_STANDARD_BIOS
3506 sh_bios_gdb_detach();
3509 sci_ports_in_use |= BIT(dev_id);
3513 static __maybe_unused int sci_suspend(struct device *dev)
3515 struct sci_port *sport = dev_get_drvdata(dev);
3518 uart_suspend_port(&sci_uart_driver, &sport->port);
3523 static __maybe_unused int sci_resume(struct device *dev)
3525 struct sci_port *sport = dev_get_drvdata(dev);
3528 uart_resume_port(&sci_uart_driver, &sport->port);
3533 static SIMPLE_DEV_PM_OPS(sci_dev_pm_ops, sci_suspend, sci_resume);
3535 static struct platform_driver sci_driver = {
3537 .remove = sci_remove,
3540 .pm = &sci_dev_pm_ops,
3541 .of_match_table = of_match_ptr(of_sci_match),
3545 static int __init sci_init(void)
3547 pr_info("%s\n", banner);
3549 return platform_driver_register(&sci_driver);
3552 static void __exit sci_exit(void)
3554 platform_driver_unregister(&sci_driver);
3556 if (sci_uart_driver.state)
3557 uart_unregister_driver(&sci_uart_driver);
3560 #if defined(CONFIG_SUPERH) && defined(CONFIG_SERIAL_SH_SCI_CONSOLE)
3561 sh_early_platform_init_buffer("earlyprintk", &sci_driver,
3562 early_serial_buf, ARRAY_SIZE(early_serial_buf));
3564 #ifdef CONFIG_SERIAL_SH_SCI_EARLYCON
3565 static struct plat_sci_port port_cfg __initdata;
3567 static int __init early_console_setup(struct earlycon_device *device,
3570 if (!device->port.membase)
3573 device->port.type = type;
3574 memcpy(&sci_ports[0].port, &device->port, sizeof(struct uart_port));
3575 port_cfg.type = type;
3576 sci_ports[0].cfg = &port_cfg;
3577 sci_ports[0].params = sci_probe_regmap(&port_cfg);
3578 port_cfg.scscr = sci_serial_in(&sci_ports[0].port, SCSCR);
3579 sci_serial_out(&sci_ports[0].port, SCSCR,
3580 SCSCR_RE | SCSCR_TE | port_cfg.scscr);
3582 device->con->write = serial_console_write;
3585 static int __init sci_early_console_setup(struct earlycon_device *device,
3588 return early_console_setup(device, PORT_SCI);
3590 static int __init scif_early_console_setup(struct earlycon_device *device,
3593 return early_console_setup(device, PORT_SCIF);
3595 static int __init rzscifa_early_console_setup(struct earlycon_device *device,
3598 port_cfg.regtype = SCIx_RZ_SCIFA_REGTYPE;
3599 return early_console_setup(device, PORT_SCIF);
3602 static int __init rzv2hscif_early_console_setup(struct earlycon_device *device,
3605 port_cfg.regtype = SCIx_RZV2H_SCIF_REGTYPE;
3606 return early_console_setup(device, PORT_SCIF);
3609 static int __init scifa_early_console_setup(struct earlycon_device *device,
3612 return early_console_setup(device, PORT_SCIFA);
3614 static int __init scifb_early_console_setup(struct earlycon_device *device,
3617 return early_console_setup(device, PORT_SCIFB);
3619 static int __init hscif_early_console_setup(struct earlycon_device *device,
3622 return early_console_setup(device, PORT_HSCIF);
3625 OF_EARLYCON_DECLARE(sci, "renesas,sci", sci_early_console_setup);
3626 OF_EARLYCON_DECLARE(scif, "renesas,scif", scif_early_console_setup);
3627 OF_EARLYCON_DECLARE(scif, "renesas,scif-r7s9210", rzscifa_early_console_setup);
3628 OF_EARLYCON_DECLARE(scif, "renesas,scif-r9a07g044", rzscifa_early_console_setup);
3629 OF_EARLYCON_DECLARE(scif, "renesas,scif-r9a09g057", rzv2hscif_early_console_setup);
3630 OF_EARLYCON_DECLARE(scifa, "renesas,scifa", scifa_early_console_setup);
3631 OF_EARLYCON_DECLARE(scifb, "renesas,scifb", scifb_early_console_setup);
3632 OF_EARLYCON_DECLARE(hscif, "renesas,hscif", hscif_early_console_setup);
3633 #endif /* CONFIG_SERIAL_SH_SCI_EARLYCON */
3635 module_init(sci_init);
3636 module_exit(sci_exit);
3638 MODULE_LICENSE("GPL");
3639 MODULE_ALIAS("platform:sh-sci");
3640 MODULE_AUTHOR("Paul Mundt");
3641 MODULE_DESCRIPTION("SuperH (H)SCI(F) serial driver");