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
;
162 #define SCI_NPORTS CONFIG_SERIAL_SH_SCI_NR_UARTS
164 static struct sci_port sci_ports
[SCI_NPORTS
];
165 static unsigned long sci_ports_in_use
;
166 static struct uart_driver sci_uart_driver
;
168 static inline struct sci_port
*
169 to_sci_port(struct uart_port
*uart
)
171 return container_of(uart
, struct sci_port
, port
);
174 static const struct sci_port_params sci_port_params
[SCIx_NR_REGTYPES
] = {
176 * Common SCI definitions, dependent on the port's regshift
179 [SCIx_SCI_REGTYPE
] = {
181 [SCSMR
] = { 0x00, 8 },
182 [SCBRR
] = { 0x01, 8 },
183 [SCSCR
] = { 0x02, 8 },
184 [SCxTDR
] = { 0x03, 8 },
185 [SCxSR
] = { 0x04, 8 },
186 [SCxRDR
] = { 0x05, 8 },
189 .overrun_reg
= SCxSR
,
190 .overrun_mask
= SCI_ORER
,
191 .sampling_rate_mask
= SCI_SR(32),
192 .error_mask
= SCI_DEFAULT_ERROR_MASK
| SCI_ORER
,
193 .error_clear
= SCI_ERROR_CLEAR
& ~SCI_ORER
,
197 * Common definitions for legacy IrDA ports.
199 [SCIx_IRDA_REGTYPE
] = {
201 [SCSMR
] = { 0x00, 8 },
202 [SCBRR
] = { 0x02, 8 },
203 [SCSCR
] = { 0x04, 8 },
204 [SCxTDR
] = { 0x06, 8 },
205 [SCxSR
] = { 0x08, 16 },
206 [SCxRDR
] = { 0x0a, 8 },
207 [SCFCR
] = { 0x0c, 8 },
208 [SCFDR
] = { 0x0e, 16 },
211 .overrun_reg
= SCxSR
,
212 .overrun_mask
= SCI_ORER
,
213 .sampling_rate_mask
= SCI_SR(32),
214 .error_mask
= SCI_DEFAULT_ERROR_MASK
| SCI_ORER
,
215 .error_clear
= SCI_ERROR_CLEAR
& ~SCI_ORER
,
219 * Common SCIFA definitions.
221 [SCIx_SCIFA_REGTYPE
] = {
223 [SCSMR
] = { 0x00, 16 },
224 [SCBRR
] = { 0x04, 8 },
225 [SCSCR
] = { 0x08, 16 },
226 [SCxTDR
] = { 0x20, 8 },
227 [SCxSR
] = { 0x14, 16 },
228 [SCxRDR
] = { 0x24, 8 },
229 [SCFCR
] = { 0x18, 16 },
230 [SCFDR
] = { 0x1c, 16 },
231 [SCPCR
] = { 0x30, 16 },
232 [SCPDR
] = { 0x34, 16 },
235 .overrun_reg
= SCxSR
,
236 .overrun_mask
= SCIFA_ORER
,
237 .sampling_rate_mask
= SCI_SR_SCIFAB
,
238 .error_mask
= SCIF_DEFAULT_ERROR_MASK
| SCIFA_ORER
,
239 .error_clear
= SCIF_ERROR_CLEAR
& ~SCIFA_ORER
,
243 * Common SCIFB definitions.
245 [SCIx_SCIFB_REGTYPE
] = {
247 [SCSMR
] = { 0x00, 16 },
248 [SCBRR
] = { 0x04, 8 },
249 [SCSCR
] = { 0x08, 16 },
250 [SCxTDR
] = { 0x40, 8 },
251 [SCxSR
] = { 0x14, 16 },
252 [SCxRDR
] = { 0x60, 8 },
253 [SCFCR
] = { 0x18, 16 },
254 [SCTFDR
] = { 0x38, 16 },
255 [SCRFDR
] = { 0x3c, 16 },
256 [SCPCR
] = { 0x30, 16 },
257 [SCPDR
] = { 0x34, 16 },
260 .overrun_reg
= SCxSR
,
261 .overrun_mask
= SCIFA_ORER
,
262 .sampling_rate_mask
= SCI_SR_SCIFAB
,
263 .error_mask
= SCIF_DEFAULT_ERROR_MASK
| SCIFA_ORER
,
264 .error_clear
= SCIF_ERROR_CLEAR
& ~SCIFA_ORER
,
268 * Common SH-2(A) SCIF definitions for ports with FIFO data
271 [SCIx_SH2_SCIF_FIFODATA_REGTYPE
] = {
273 [SCSMR
] = { 0x00, 16 },
274 [SCBRR
] = { 0x04, 8 },
275 [SCSCR
] = { 0x08, 16 },
276 [SCxTDR
] = { 0x0c, 8 },
277 [SCxSR
] = { 0x10, 16 },
278 [SCxRDR
] = { 0x14, 8 },
279 [SCFCR
] = { 0x18, 16 },
280 [SCFDR
] = { 0x1c, 16 },
281 [SCSPTR
] = { 0x20, 16 },
282 [SCLSR
] = { 0x24, 16 },
285 .overrun_reg
= SCLSR
,
286 .overrun_mask
= SCLSR_ORER
,
287 .sampling_rate_mask
= SCI_SR(32),
288 .error_mask
= SCIF_DEFAULT_ERROR_MASK
,
289 .error_clear
= SCIF_ERROR_CLEAR
,
293 * The "SCIFA" that is in RZ/A2, RZ/G2L and RZ/T.
294 * It looks like a normal SCIF with FIFO data, but with a
295 * compressed address space. Also, the break out of interrupts
296 * are different: ERI/BRI, RXI, TXI, TEI, DRI.
298 [SCIx_RZ_SCIFA_REGTYPE
] = {
300 [SCSMR
] = { 0x00, 16 },
301 [SCBRR
] = { 0x02, 8 },
302 [SCSCR
] = { 0x04, 16 },
303 [SCxTDR
] = { 0x06, 8 },
304 [SCxSR
] = { 0x08, 16 },
305 [SCxRDR
] = { 0x0A, 8 },
306 [SCFCR
] = { 0x0C, 16 },
307 [SCFDR
] = { 0x0E, 16 },
308 [SCSPTR
] = { 0x10, 16 },
309 [SCLSR
] = { 0x12, 16 },
310 [SEMR
] = { 0x14, 8 },
313 .overrun_reg
= SCLSR
,
314 .overrun_mask
= SCLSR_ORER
,
315 .sampling_rate_mask
= SCI_SR(32),
316 .error_mask
= SCIF_DEFAULT_ERROR_MASK
,
317 .error_clear
= SCIF_ERROR_CLEAR
,
321 * The "SCIF" that is in RZ/V2H(P) SoC is similar to one found on RZ/G2L SoC
322 * with below differences,
323 * - Break out of interrupts are different: ERI, BRI, RXI, TXI, TEI, DRI,
324 * TEI-DRI, RXI-EDGE and TXI-EDGE.
325 * - SCSMR register does not have CM bit (BIT(7)) ie it does not support synchronous mode.
326 * - SCFCR register does not have SCFCR_MCE bit.
327 * - SCSPTR register has only bits SCSPTR_SPB2DT and SCSPTR_SPB2IO.
329 [SCIx_RZV2H_SCIF_REGTYPE
] = {
331 [SCSMR
] = { 0x00, 16 },
332 [SCBRR
] = { 0x02, 8 },
333 [SCSCR
] = { 0x04, 16 },
334 [SCxTDR
] = { 0x06, 8 },
335 [SCxSR
] = { 0x08, 16 },
336 [SCxRDR
] = { 0x0a, 8 },
337 [SCFCR
] = { 0x0c, 16 },
338 [SCFDR
] = { 0x0e, 16 },
339 [SCSPTR
] = { 0x10, 16 },
340 [SCLSR
] = { 0x12, 16 },
341 [SEMR
] = { 0x14, 8 },
344 .overrun_reg
= SCLSR
,
345 .overrun_mask
= SCLSR_ORER
,
346 .sampling_rate_mask
= SCI_SR(32),
347 .error_mask
= SCIF_DEFAULT_ERROR_MASK
,
348 .error_clear
= SCIF_ERROR_CLEAR
,
352 * Common SH-3 SCIF definitions.
354 [SCIx_SH3_SCIF_REGTYPE
] = {
356 [SCSMR
] = { 0x00, 8 },
357 [SCBRR
] = { 0x02, 8 },
358 [SCSCR
] = { 0x04, 8 },
359 [SCxTDR
] = { 0x06, 8 },
360 [SCxSR
] = { 0x08, 16 },
361 [SCxRDR
] = { 0x0a, 8 },
362 [SCFCR
] = { 0x0c, 8 },
363 [SCFDR
] = { 0x0e, 16 },
366 .overrun_reg
= SCLSR
,
367 .overrun_mask
= SCLSR_ORER
,
368 .sampling_rate_mask
= SCI_SR(32),
369 .error_mask
= SCIF_DEFAULT_ERROR_MASK
,
370 .error_clear
= SCIF_ERROR_CLEAR
,
374 * Common SH-4(A) SCIF(B) definitions.
376 [SCIx_SH4_SCIF_REGTYPE
] = {
378 [SCSMR
] = { 0x00, 16 },
379 [SCBRR
] = { 0x04, 8 },
380 [SCSCR
] = { 0x08, 16 },
381 [SCxTDR
] = { 0x0c, 8 },
382 [SCxSR
] = { 0x10, 16 },
383 [SCxRDR
] = { 0x14, 8 },
384 [SCFCR
] = { 0x18, 16 },
385 [SCFDR
] = { 0x1c, 16 },
386 [SCSPTR
] = { 0x20, 16 },
387 [SCLSR
] = { 0x24, 16 },
390 .overrun_reg
= SCLSR
,
391 .overrun_mask
= SCLSR_ORER
,
392 .sampling_rate_mask
= SCI_SR(32),
393 .error_mask
= SCIF_DEFAULT_ERROR_MASK
,
394 .error_clear
= SCIF_ERROR_CLEAR
,
398 * Common SCIF definitions for ports with a Baud Rate Generator for
399 * External Clock (BRG).
401 [SCIx_SH4_SCIF_BRG_REGTYPE
] = {
403 [SCSMR
] = { 0x00, 16 },
404 [SCBRR
] = { 0x04, 8 },
405 [SCSCR
] = { 0x08, 16 },
406 [SCxTDR
] = { 0x0c, 8 },
407 [SCxSR
] = { 0x10, 16 },
408 [SCxRDR
] = { 0x14, 8 },
409 [SCFCR
] = { 0x18, 16 },
410 [SCFDR
] = { 0x1c, 16 },
411 [SCSPTR
] = { 0x20, 16 },
412 [SCLSR
] = { 0x24, 16 },
413 [SCDL
] = { 0x30, 16 },
414 [SCCKS
] = { 0x34, 16 },
417 .overrun_reg
= SCLSR
,
418 .overrun_mask
= SCLSR_ORER
,
419 .sampling_rate_mask
= SCI_SR(32),
420 .error_mask
= SCIF_DEFAULT_ERROR_MASK
,
421 .error_clear
= SCIF_ERROR_CLEAR
,
425 * Common HSCIF definitions.
427 [SCIx_HSCIF_REGTYPE
] = {
429 [SCSMR
] = { 0x00, 16 },
430 [SCBRR
] = { 0x04, 8 },
431 [SCSCR
] = { 0x08, 16 },
432 [SCxTDR
] = { 0x0c, 8 },
433 [SCxSR
] = { 0x10, 16 },
434 [SCxRDR
] = { 0x14, 8 },
435 [SCFCR
] = { 0x18, 16 },
436 [SCFDR
] = { 0x1c, 16 },
437 [SCSPTR
] = { 0x20, 16 },
438 [SCLSR
] = { 0x24, 16 },
439 [HSSRR
] = { 0x40, 16 },
440 [SCDL
] = { 0x30, 16 },
441 [SCCKS
] = { 0x34, 16 },
442 [HSRTRGR
] = { 0x54, 16 },
443 [HSTTRGR
] = { 0x58, 16 },
446 .overrun_reg
= SCLSR
,
447 .overrun_mask
= SCLSR_ORER
,
448 .sampling_rate_mask
= SCI_SR_RANGE(8, 32),
449 .error_mask
= SCIF_DEFAULT_ERROR_MASK
,
450 .error_clear
= SCIF_ERROR_CLEAR
,
454 * Common SH-4(A) SCIF(B) definitions for ports without an SCSPTR
457 [SCIx_SH4_SCIF_NO_SCSPTR_REGTYPE
] = {
459 [SCSMR
] = { 0x00, 16 },
460 [SCBRR
] = { 0x04, 8 },
461 [SCSCR
] = { 0x08, 16 },
462 [SCxTDR
] = { 0x0c, 8 },
463 [SCxSR
] = { 0x10, 16 },
464 [SCxRDR
] = { 0x14, 8 },
465 [SCFCR
] = { 0x18, 16 },
466 [SCFDR
] = { 0x1c, 16 },
467 [SCLSR
] = { 0x24, 16 },
470 .overrun_reg
= SCLSR
,
471 .overrun_mask
= SCLSR_ORER
,
472 .sampling_rate_mask
= SCI_SR(32),
473 .error_mask
= SCIF_DEFAULT_ERROR_MASK
,
474 .error_clear
= SCIF_ERROR_CLEAR
,
478 * Common SH-4(A) SCIF(B) definitions for ports with FIFO data
481 [SCIx_SH4_SCIF_FIFODATA_REGTYPE
] = {
483 [SCSMR
] = { 0x00, 16 },
484 [SCBRR
] = { 0x04, 8 },
485 [SCSCR
] = { 0x08, 16 },
486 [SCxTDR
] = { 0x0c, 8 },
487 [SCxSR
] = { 0x10, 16 },
488 [SCxRDR
] = { 0x14, 8 },
489 [SCFCR
] = { 0x18, 16 },
490 [SCFDR
] = { 0x1c, 16 },
491 [SCTFDR
] = { 0x1c, 16 }, /* aliased to SCFDR */
492 [SCRFDR
] = { 0x20, 16 },
493 [SCSPTR
] = { 0x24, 16 },
494 [SCLSR
] = { 0x28, 16 },
497 .overrun_reg
= SCLSR
,
498 .overrun_mask
= SCLSR_ORER
,
499 .sampling_rate_mask
= SCI_SR(32),
500 .error_mask
= SCIF_DEFAULT_ERROR_MASK
,
501 .error_clear
= SCIF_ERROR_CLEAR
,
505 * SH7705-style SCIF(B) ports, lacking both SCSPTR and SCLSR
508 [SCIx_SH7705_SCIF_REGTYPE
] = {
510 [SCSMR
] = { 0x00, 16 },
511 [SCBRR
] = { 0x04, 8 },
512 [SCSCR
] = { 0x08, 16 },
513 [SCxTDR
] = { 0x20, 8 },
514 [SCxSR
] = { 0x14, 16 },
515 [SCxRDR
] = { 0x24, 8 },
516 [SCFCR
] = { 0x18, 16 },
517 [SCFDR
] = { 0x1c, 16 },
520 .overrun_reg
= SCxSR
,
521 .overrun_mask
= SCIFA_ORER
,
522 .sampling_rate_mask
= SCI_SR(16),
523 .error_mask
= SCIF_DEFAULT_ERROR_MASK
| SCIFA_ORER
,
524 .error_clear
= SCIF_ERROR_CLEAR
& ~SCIFA_ORER
,
528 #define sci_getreg(up, offset) (&to_sci_port(up)->params->regs[offset])
531 * The "offset" here is rather misleading, in that it refers to an enum
532 * value relative to the port mapping rather than the fixed offset
533 * itself, which needs to be manually retrieved from the platform's
534 * register map for the given port.
536 static unsigned int sci_serial_in(struct uart_port
*p
, int offset
)
538 const struct plat_sci_reg
*reg
= sci_getreg(p
, offset
);
541 return ioread8(p
->membase
+ (reg
->offset
<< p
->regshift
));
542 else if (reg
->size
== 16)
543 return ioread16(p
->membase
+ (reg
->offset
<< p
->regshift
));
545 WARN(1, "Invalid register access\n");
550 static void sci_serial_out(struct uart_port
*p
, int offset
, int value
)
552 const struct plat_sci_reg
*reg
= sci_getreg(p
, offset
);
555 iowrite8(value
, p
->membase
+ (reg
->offset
<< p
->regshift
));
556 else if (reg
->size
== 16)
557 iowrite16(value
, p
->membase
+ (reg
->offset
<< p
->regshift
));
559 WARN(1, "Invalid register access\n");
562 static void sci_port_enable(struct sci_port
*sci_port
)
566 if (!sci_port
->port
.dev
)
569 pm_runtime_get_sync(sci_port
->port
.dev
);
571 for (i
= 0; i
< SCI_NUM_CLKS
; i
++) {
572 clk_prepare_enable(sci_port
->clks
[i
]);
573 sci_port
->clk_rates
[i
] = clk_get_rate(sci_port
->clks
[i
]);
575 sci_port
->port
.uartclk
= sci_port
->clk_rates
[SCI_FCK
];
578 static void sci_port_disable(struct sci_port
*sci_port
)
582 if (!sci_port
->port
.dev
)
585 for (i
= SCI_NUM_CLKS
; i
-- > 0; )
586 clk_disable_unprepare(sci_port
->clks
[i
]);
588 pm_runtime_put_sync(sci_port
->port
.dev
);
591 static inline unsigned long port_rx_irq_mask(struct uart_port
*port
)
594 * Not all ports (such as SCIFA) will support REIE. Rather than
595 * special-casing the port type, we check the port initialization
596 * IRQ enable mask to see whether the IRQ is desired at all. If
597 * it's unset, it's logically inferred that there's no point in
600 return SCSCR_RIE
| (to_sci_port(port
)->cfg
->scscr
& SCSCR_REIE
);
603 static void sci_start_tx(struct uart_port
*port
)
605 struct sci_port
*s
= to_sci_port(port
);
608 #ifdef CONFIG_SERIAL_SH_SCI_DMA
609 if (port
->type
== PORT_SCIFA
|| port
->type
== PORT_SCIFB
) {
610 u16
new, scr
= sci_serial_in(port
, SCSCR
);
612 new = scr
| SCSCR_TDRQE
;
614 new = scr
& ~SCSCR_TDRQE
;
616 sci_serial_out(port
, SCSCR
, new);
619 if (s
->chan_tx
&& !kfifo_is_empty(&port
->state
->port
.xmit_fifo
) &&
620 dma_submit_error(s
->cookie_tx
)) {
621 if (s
->cfg
->regtype
== SCIx_RZ_SCIFA_REGTYPE
)
622 /* Switch irq from SCIF to DMA */
623 disable_irq_nosync(s
->irqs
[SCIx_TXI_IRQ
]);
626 schedule_work(&s
->work_tx
);
630 if (!s
->chan_tx
|| s
->cfg
->regtype
== SCIx_RZ_SCIFA_REGTYPE
||
631 port
->type
== PORT_SCIFA
|| port
->type
== PORT_SCIFB
) {
632 /* Set TIE (Transmit Interrupt Enable) bit in SCSCR */
633 ctrl
= sci_serial_in(port
, SCSCR
);
636 * For SCI, TE (transmit enable) must be set after setting TIE
637 * (transmit interrupt enable) or in the same instruction to start
638 * the transmit process.
640 if (port
->type
== PORT_SCI
)
643 sci_serial_out(port
, SCSCR
, ctrl
| SCSCR_TIE
);
647 static void sci_stop_tx(struct uart_port
*port
)
651 /* Clear TIE (Transmit Interrupt Enable) bit in SCSCR */
652 ctrl
= sci_serial_in(port
, SCSCR
);
654 if (port
->type
== PORT_SCIFA
|| port
->type
== PORT_SCIFB
)
655 ctrl
&= ~SCSCR_TDRQE
;
659 sci_serial_out(port
, SCSCR
, ctrl
);
661 #ifdef CONFIG_SERIAL_SH_SCI_DMA
662 if (to_sci_port(port
)->chan_tx
&&
663 !dma_submit_error(to_sci_port(port
)->cookie_tx
)) {
664 dmaengine_terminate_async(to_sci_port(port
)->chan_tx
);
665 to_sci_port(port
)->cookie_tx
= -EINVAL
;
670 static void sci_start_rx(struct uart_port
*port
)
674 ctrl
= sci_serial_in(port
, SCSCR
) | port_rx_irq_mask(port
);
676 if (port
->type
== PORT_SCIFA
|| port
->type
== PORT_SCIFB
)
677 ctrl
&= ~SCSCR_RDRQE
;
679 sci_serial_out(port
, SCSCR
, ctrl
);
682 static void sci_stop_rx(struct uart_port
*port
)
686 ctrl
= sci_serial_in(port
, SCSCR
);
688 if (port
->type
== PORT_SCIFA
|| port
->type
== PORT_SCIFB
)
689 ctrl
&= ~SCSCR_RDRQE
;
691 ctrl
&= ~port_rx_irq_mask(port
);
693 sci_serial_out(port
, SCSCR
, ctrl
);
696 static void sci_clear_SCxSR(struct uart_port
*port
, unsigned int mask
)
698 if (port
->type
== PORT_SCI
) {
699 /* Just store the mask */
700 sci_serial_out(port
, SCxSR
, mask
);
701 } else if (to_sci_port(port
)->params
->overrun_mask
== SCIFA_ORER
) {
702 /* SCIFA/SCIFB and SCIF on SH7705/SH7720/SH7721 */
703 /* Only clear the status bits we want to clear */
704 sci_serial_out(port
, SCxSR
, sci_serial_in(port
, SCxSR
) & mask
);
706 /* Store the mask, clear parity/framing errors */
707 sci_serial_out(port
, SCxSR
, mask
& ~(SCIF_FERC
| SCIF_PERC
));
711 #if defined(CONFIG_CONSOLE_POLL) || defined(CONFIG_SERIAL_SH_SCI_CONSOLE) || \
712 defined(CONFIG_SERIAL_SH_SCI_EARLYCON)
714 #ifdef CONFIG_CONSOLE_POLL
715 static int sci_poll_get_char(struct uart_port
*port
)
717 unsigned short status
;
721 status
= sci_serial_in(port
, SCxSR
);
722 if (status
& SCxSR_ERRORS(port
)) {
723 sci_clear_SCxSR(port
, SCxSR_ERROR_CLEAR(port
));
729 if (!(status
& SCxSR_RDxF(port
)))
732 c
= sci_serial_in(port
, SCxRDR
);
735 sci_serial_in(port
, SCxSR
);
736 sci_clear_SCxSR(port
, SCxSR_RDxF_CLEAR(port
));
742 static void sci_poll_put_char(struct uart_port
*port
, unsigned char c
)
744 unsigned short status
;
747 status
= sci_serial_in(port
, SCxSR
);
748 } while (!(status
& SCxSR_TDxE(port
)));
750 sci_serial_out(port
, SCxTDR
, c
);
751 sci_clear_SCxSR(port
, SCxSR_TDxE_CLEAR(port
) & ~SCxSR_TEND(port
));
753 #endif /* CONFIG_CONSOLE_POLL || CONFIG_SERIAL_SH_SCI_CONSOLE ||
754 CONFIG_SERIAL_SH_SCI_EARLYCON */
756 static void sci_init_pins(struct uart_port
*port
, unsigned int cflag
)
758 struct sci_port
*s
= to_sci_port(port
);
761 * Use port-specific handler if provided.
763 if (s
->cfg
->ops
&& s
->cfg
->ops
->init_pins
) {
764 s
->cfg
->ops
->init_pins(port
, cflag
);
768 if (port
->type
== PORT_SCIFA
|| port
->type
== PORT_SCIFB
) {
769 u16 data
= sci_serial_in(port
, SCPDR
);
770 u16 ctrl
= sci_serial_in(port
, SCPCR
);
772 /* Enable RXD and TXD pin functions */
773 ctrl
&= ~(SCPCR_RXDC
| SCPCR_TXDC
);
774 if (to_sci_port(port
)->has_rtscts
) {
775 /* RTS# is output, active low, unless autorts */
776 if (!(port
->mctrl
& TIOCM_RTS
)) {
779 } else if (!s
->autorts
) {
783 /* Enable RTS# pin function */
786 /* Enable CTS# pin function */
789 sci_serial_out(port
, SCPDR
, data
);
790 sci_serial_out(port
, SCPCR
, ctrl
);
791 } else if (sci_getreg(port
, SCSPTR
)->size
&& s
->cfg
->regtype
!= SCIx_RZV2H_SCIF_REGTYPE
) {
792 u16 status
= sci_serial_in(port
, SCSPTR
);
794 /* RTS# is always output; and active low, unless autorts */
795 status
|= SCSPTR_RTSIO
;
796 if (!(port
->mctrl
& TIOCM_RTS
))
797 status
|= SCSPTR_RTSDT
;
798 else if (!s
->autorts
)
799 status
&= ~SCSPTR_RTSDT
;
800 /* CTS# and SCK are inputs */
801 status
&= ~(SCSPTR_CTSIO
| SCSPTR_SCKIO
);
802 sci_serial_out(port
, SCSPTR
, status
);
806 static int sci_txfill(struct uart_port
*port
)
808 struct sci_port
*s
= to_sci_port(port
);
809 unsigned int fifo_mask
= (s
->params
->fifosize
<< 1) - 1;
810 const struct plat_sci_reg
*reg
;
812 reg
= sci_getreg(port
, SCTFDR
);
814 return sci_serial_in(port
, SCTFDR
) & fifo_mask
;
816 reg
= sci_getreg(port
, SCFDR
);
818 return sci_serial_in(port
, SCFDR
) >> 8;
820 return !(sci_serial_in(port
, SCxSR
) & SCI_TDRE
);
823 static int sci_txroom(struct uart_port
*port
)
825 return port
->fifosize
- sci_txfill(port
);
828 static int sci_rxfill(struct uart_port
*port
)
830 struct sci_port
*s
= to_sci_port(port
);
831 unsigned int fifo_mask
= (s
->params
->fifosize
<< 1) - 1;
832 const struct plat_sci_reg
*reg
;
834 reg
= sci_getreg(port
, SCRFDR
);
836 return sci_serial_in(port
, SCRFDR
) & fifo_mask
;
838 reg
= sci_getreg(port
, SCFDR
);
840 return sci_serial_in(port
, SCFDR
) & fifo_mask
;
842 return (sci_serial_in(port
, SCxSR
) & SCxSR_RDxF(port
)) != 0;
845 /* ********************************************************************** *
846 * the interrupt related routines *
847 * ********************************************************************** */
849 static void sci_transmit_chars(struct uart_port
*port
)
851 struct tty_port
*tport
= &port
->state
->port
;
852 unsigned int stopped
= uart_tx_stopped(port
);
853 unsigned short status
;
857 status
= sci_serial_in(port
, SCxSR
);
858 if (!(status
& SCxSR_TDxE(port
))) {
859 ctrl
= sci_serial_in(port
, SCSCR
);
860 if (kfifo_is_empty(&tport
->xmit_fifo
))
864 sci_serial_out(port
, SCSCR
, ctrl
);
868 count
= sci_txroom(port
);
876 } else if (stopped
|| !kfifo_get(&tport
->xmit_fifo
, &c
)) {
877 if (port
->type
== PORT_SCI
&&
878 kfifo_is_empty(&tport
->xmit_fifo
)) {
879 ctrl
= sci_serial_in(port
, SCSCR
);
881 sci_serial_out(port
, SCSCR
, ctrl
);
887 sci_serial_out(port
, SCxTDR
, c
);
890 } while (--count
> 0);
892 sci_clear_SCxSR(port
, SCxSR_TDxE_CLEAR(port
));
894 if (kfifo_len(&tport
->xmit_fifo
) < WAKEUP_CHARS
)
895 uart_write_wakeup(port
);
896 if (kfifo_is_empty(&tport
->xmit_fifo
)) {
897 if (port
->type
== PORT_SCI
) {
898 ctrl
= sci_serial_in(port
, SCSCR
);
901 sci_serial_out(port
, SCSCR
, ctrl
);
908 static void sci_receive_chars(struct uart_port
*port
)
910 struct tty_port
*tport
= &port
->state
->port
;
911 int i
, count
, copied
= 0;
912 unsigned short status
;
915 status
= sci_serial_in(port
, SCxSR
);
916 if (!(status
& SCxSR_RDxF(port
)))
920 /* Don't copy more bytes than there is room for in the buffer */
921 count
= tty_buffer_request_room(tport
, sci_rxfill(port
));
923 /* If for any reason we can't copy more data, we're done! */
927 if (port
->type
== PORT_SCI
) {
928 char c
= sci_serial_in(port
, SCxRDR
);
929 if (uart_handle_sysrq_char(port
, c
))
932 tty_insert_flip_char(tport
, c
, TTY_NORMAL
);
934 for (i
= 0; i
< count
; i
++) {
937 if (port
->type
== PORT_SCIF
||
938 port
->type
== PORT_HSCIF
) {
939 status
= sci_serial_in(port
, SCxSR
);
940 c
= sci_serial_in(port
, SCxRDR
);
942 c
= sci_serial_in(port
, SCxRDR
);
943 status
= sci_serial_in(port
, SCxSR
);
945 if (uart_handle_sysrq_char(port
, c
)) {
950 /* Store data and status */
951 if (status
& SCxSR_FER(port
)) {
953 port
->icount
.frame
++;
954 } else if (status
& SCxSR_PER(port
)) {
956 port
->icount
.parity
++;
960 tty_insert_flip_char(tport
, c
, flag
);
964 sci_serial_in(port
, SCxSR
); /* dummy read */
965 sci_clear_SCxSR(port
, SCxSR_RDxF_CLEAR(port
));
968 port
->icount
.rx
+= count
;
972 /* Tell the rest of the system the news. New characters! */
973 tty_flip_buffer_push(tport
);
975 /* TTY buffers full; read from RX reg to prevent lockup */
976 sci_serial_in(port
, SCxRDR
);
977 sci_serial_in(port
, SCxSR
); /* dummy read */
978 sci_clear_SCxSR(port
, SCxSR_RDxF_CLEAR(port
));
982 static int sci_handle_errors(struct uart_port
*port
)
985 unsigned short status
= sci_serial_in(port
, SCxSR
);
986 struct tty_port
*tport
= &port
->state
->port
;
987 struct sci_port
*s
= to_sci_port(port
);
989 /* Handle overruns */
990 if (status
& s
->params
->overrun_mask
) {
991 port
->icount
.overrun
++;
994 if (tty_insert_flip_char(tport
, 0, TTY_OVERRUN
))
998 if (status
& SCxSR_FER(port
)) {
1000 port
->icount
.frame
++;
1002 if (tty_insert_flip_char(tport
, 0, TTY_FRAME
))
1006 if (status
& SCxSR_PER(port
)) {
1008 port
->icount
.parity
++;
1010 if (tty_insert_flip_char(tport
, 0, TTY_PARITY
))
1015 tty_flip_buffer_push(tport
);
1020 static int sci_handle_fifo_overrun(struct uart_port
*port
)
1022 struct tty_port
*tport
= &port
->state
->port
;
1023 struct sci_port
*s
= to_sci_port(port
);
1024 const struct plat_sci_reg
*reg
;
1028 reg
= sci_getreg(port
, s
->params
->overrun_reg
);
1032 status
= sci_serial_in(port
, s
->params
->overrun_reg
);
1033 if (status
& s
->params
->overrun_mask
) {
1034 status
&= ~s
->params
->overrun_mask
;
1035 sci_serial_out(port
, s
->params
->overrun_reg
, status
);
1037 port
->icount
.overrun
++;
1039 tty_insert_flip_char(tport
, 0, TTY_OVERRUN
);
1040 tty_flip_buffer_push(tport
);
1047 static int sci_handle_breaks(struct uart_port
*port
)
1050 unsigned short status
= sci_serial_in(port
, SCxSR
);
1051 struct tty_port
*tport
= &port
->state
->port
;
1053 if (uart_handle_break(port
))
1056 if (status
& SCxSR_BRK(port
)) {
1059 /* Notify of BREAK */
1060 if (tty_insert_flip_char(tport
, 0, TTY_BREAK
))
1065 tty_flip_buffer_push(tport
);
1067 copied
+= sci_handle_fifo_overrun(port
);
1072 static int scif_set_rtrg(struct uart_port
*port
, int rx_trig
)
1076 if (rx_trig
>= port
->fifosize
)
1077 rx_trig
= port
->fifosize
- 1;
1081 /* HSCIF can be set to an arbitrary level. */
1082 if (sci_getreg(port
, HSRTRGR
)->size
) {
1083 sci_serial_out(port
, HSRTRGR
, rx_trig
);
1087 switch (port
->type
) {
1092 } else if (rx_trig
< 8) {
1095 } else if (rx_trig
< 14) {
1099 bits
= SCFCR_RTRG0
| SCFCR_RTRG1
;
1108 } else if (rx_trig
< 32) {
1111 } else if (rx_trig
< 48) {
1115 bits
= SCFCR_RTRG0
| SCFCR_RTRG1
;
1120 WARN(1, "unknown FIFO configuration");
1124 sci_serial_out(port
, SCFCR
,
1125 (sci_serial_in(port
, SCFCR
) &
1126 ~(SCFCR_RTRG1
| SCFCR_RTRG0
)) | bits
);
1131 static int scif_rtrg_enabled(struct uart_port
*port
)
1133 if (sci_getreg(port
, HSRTRGR
)->size
)
1134 return sci_serial_in(port
, HSRTRGR
) != 0;
1136 return (sci_serial_in(port
, SCFCR
) &
1137 (SCFCR_RTRG0
| SCFCR_RTRG1
)) != 0;
1140 static void rx_fifo_timer_fn(struct timer_list
*t
)
1142 struct sci_port
*s
= from_timer(s
, t
, rx_fifo_timer
);
1143 struct uart_port
*port
= &s
->port
;
1145 dev_dbg(port
->dev
, "Rx timed out\n");
1146 scif_set_rtrg(port
, 1);
1149 static ssize_t
rx_fifo_trigger_show(struct device
*dev
,
1150 struct device_attribute
*attr
, char *buf
)
1152 struct uart_port
*port
= dev_get_drvdata(dev
);
1153 struct sci_port
*sci
= to_sci_port(port
);
1155 return sprintf(buf
, "%d\n", sci
->rx_trigger
);
1158 static ssize_t
rx_fifo_trigger_store(struct device
*dev
,
1159 struct device_attribute
*attr
,
1160 const char *buf
, size_t count
)
1162 struct uart_port
*port
= dev_get_drvdata(dev
);
1163 struct sci_port
*sci
= to_sci_port(port
);
1167 ret
= kstrtol(buf
, 0, &r
);
1171 sci
->rx_trigger
= scif_set_rtrg(port
, r
);
1172 if (port
->type
== PORT_SCIFA
|| port
->type
== PORT_SCIFB
)
1173 scif_set_rtrg(port
, 1);
1178 static DEVICE_ATTR_RW(rx_fifo_trigger
);
1180 static ssize_t
rx_fifo_timeout_show(struct device
*dev
,
1181 struct device_attribute
*attr
,
1184 struct uart_port
*port
= dev_get_drvdata(dev
);
1185 struct sci_port
*sci
= to_sci_port(port
);
1188 if (port
->type
== PORT_HSCIF
)
1189 v
= sci
->hscif_tot
>> HSSCR_TOT_SHIFT
;
1191 v
= sci
->rx_fifo_timeout
;
1193 return sprintf(buf
, "%d\n", v
);
1196 static ssize_t
rx_fifo_timeout_store(struct device
*dev
,
1197 struct device_attribute
*attr
,
1201 struct uart_port
*port
= dev_get_drvdata(dev
);
1202 struct sci_port
*sci
= to_sci_port(port
);
1206 ret
= kstrtol(buf
, 0, &r
);
1210 if (port
->type
== PORT_HSCIF
) {
1213 sci
->hscif_tot
= r
<< HSSCR_TOT_SHIFT
;
1215 sci
->rx_fifo_timeout
= r
;
1216 scif_set_rtrg(port
, 1);
1218 timer_setup(&sci
->rx_fifo_timer
, rx_fifo_timer_fn
, 0);
1224 static DEVICE_ATTR_RW(rx_fifo_timeout
);
1227 #ifdef CONFIG_SERIAL_SH_SCI_DMA
1228 static void sci_dma_tx_complete(void *arg
)
1230 struct sci_port
*s
= arg
;
1231 struct uart_port
*port
= &s
->port
;
1232 struct tty_port
*tport
= &port
->state
->port
;
1233 unsigned long flags
;
1235 dev_dbg(port
->dev
, "%s(%d)\n", __func__
, port
->line
);
1237 uart_port_lock_irqsave(port
, &flags
);
1239 uart_xmit_advance(port
, s
->tx_dma_len
);
1241 if (kfifo_len(&tport
->xmit_fifo
) < WAKEUP_CHARS
)
1242 uart_write_wakeup(port
);
1244 if (!kfifo_is_empty(&tport
->xmit_fifo
)) {
1246 schedule_work(&s
->work_tx
);
1248 s
->cookie_tx
= -EINVAL
;
1249 if (port
->type
== PORT_SCIFA
|| port
->type
== PORT_SCIFB
||
1250 s
->cfg
->regtype
== SCIx_RZ_SCIFA_REGTYPE
) {
1251 u16 ctrl
= sci_serial_in(port
, SCSCR
);
1252 sci_serial_out(port
, SCSCR
, ctrl
& ~SCSCR_TIE
);
1253 if (s
->cfg
->regtype
== SCIx_RZ_SCIFA_REGTYPE
) {
1254 /* Switch irq from DMA to SCIF */
1255 dmaengine_pause(s
->chan_tx_saved
);
1256 enable_irq(s
->irqs
[SCIx_TXI_IRQ
]);
1261 uart_port_unlock_irqrestore(port
, flags
);
1264 /* Locking: called with port lock held */
1265 static int sci_dma_rx_push(struct sci_port
*s
, void *buf
, size_t count
)
1267 struct uart_port
*port
= &s
->port
;
1268 struct tty_port
*tport
= &port
->state
->port
;
1271 copied
= tty_insert_flip_string(tport
, buf
, count
);
1273 port
->icount
.buf_overrun
++;
1275 port
->icount
.rx
+= copied
;
1280 static int sci_dma_rx_find_active(struct sci_port
*s
)
1284 for (i
= 0; i
< ARRAY_SIZE(s
->cookie_rx
); i
++)
1285 if (s
->active_rx
== s
->cookie_rx
[i
])
1291 /* Must only be called with uart_port_lock taken */
1292 static void sci_dma_rx_chan_invalidate(struct sci_port
*s
)
1297 for (i
= 0; i
< ARRAY_SIZE(s
->cookie_rx
); i
++)
1298 s
->cookie_rx
[i
] = -EINVAL
;
1302 static void sci_dma_rx_release(struct sci_port
*s
)
1304 struct dma_chan
*chan
= s
->chan_rx_saved
;
1305 struct uart_port
*port
= &s
->port
;
1306 unsigned long flags
;
1308 uart_port_lock_irqsave(port
, &flags
);
1309 s
->chan_rx_saved
= NULL
;
1310 sci_dma_rx_chan_invalidate(s
);
1311 uart_port_unlock_irqrestore(port
, flags
);
1313 dmaengine_terminate_sync(chan
);
1314 dma_free_coherent(chan
->device
->dev
, s
->buf_len_rx
* 2, s
->rx_buf
[0],
1315 sg_dma_address(&s
->sg_rx
[0]));
1316 dma_release_channel(chan
);
1319 static void start_hrtimer_us(struct hrtimer
*hrt
, unsigned long usec
)
1321 long sec
= usec
/ 1000000;
1322 long nsec
= (usec
% 1000000) * 1000;
1323 ktime_t t
= ktime_set(sec
, nsec
);
1325 hrtimer_start(hrt
, t
, HRTIMER_MODE_REL
);
1328 static void sci_dma_rx_reenable_irq(struct sci_port
*s
)
1330 struct uart_port
*port
= &s
->port
;
1333 /* Direct new serial port interrupts back to CPU */
1334 scr
= sci_serial_in(port
, SCSCR
);
1335 if (port
->type
== PORT_SCIFA
|| port
->type
== PORT_SCIFB
||
1336 s
->cfg
->regtype
== SCIx_RZ_SCIFA_REGTYPE
) {
1337 enable_irq(s
->irqs
[SCIx_RXI_IRQ
]);
1338 if (s
->cfg
->regtype
== SCIx_RZ_SCIFA_REGTYPE
)
1339 scif_set_rtrg(port
, s
->rx_trigger
);
1341 scr
&= ~SCSCR_RDRQE
;
1343 sci_serial_out(port
, SCSCR
, scr
| SCSCR_RIE
);
1346 static void sci_dma_rx_complete(void *arg
)
1348 struct sci_port
*s
= arg
;
1349 struct dma_chan
*chan
= s
->chan_rx
;
1350 struct uart_port
*port
= &s
->port
;
1351 struct dma_async_tx_descriptor
*desc
;
1352 unsigned long flags
;
1353 int active
, count
= 0;
1355 dev_dbg(port
->dev
, "%s(%d) active cookie %d\n", __func__
, port
->line
,
1358 hrtimer_cancel(&s
->rx_timer
);
1360 uart_port_lock_irqsave(port
, &flags
);
1362 active
= sci_dma_rx_find_active(s
);
1364 count
= sci_dma_rx_push(s
, s
->rx_buf
[active
], s
->buf_len_rx
);
1367 tty_flip_buffer_push(&port
->state
->port
);
1369 desc
= dmaengine_prep_slave_sg(s
->chan_rx
, &s
->sg_rx
[active
], 1,
1371 DMA_PREP_INTERRUPT
| DMA_CTRL_ACK
);
1375 desc
->callback
= sci_dma_rx_complete
;
1376 desc
->callback_param
= s
;
1377 s
->cookie_rx
[active
] = dmaengine_submit(desc
);
1378 if (dma_submit_error(s
->cookie_rx
[active
]))
1381 s
->active_rx
= s
->cookie_rx
[!active
];
1383 dma_async_issue_pending(chan
);
1385 uart_port_unlock_irqrestore(port
, flags
);
1386 dev_dbg(port
->dev
, "%s: cookie %d #%d, new active cookie %d\n",
1387 __func__
, s
->cookie_rx
[active
], active
, s
->active_rx
);
1389 start_hrtimer_us(&s
->rx_timer
, s
->rx_timeout
);
1395 dmaengine_terminate_async(chan
);
1396 sci_dma_rx_chan_invalidate(s
);
1397 sci_dma_rx_reenable_irq(s
);
1398 uart_port_unlock_irqrestore(port
, flags
);
1399 dev_warn(port
->dev
, "Failed submitting Rx DMA descriptor\n");
1402 static void sci_dma_tx_release(struct sci_port
*s
)
1404 struct dma_chan
*chan
= s
->chan_tx_saved
;
1406 cancel_work_sync(&s
->work_tx
);
1407 s
->chan_tx_saved
= s
->chan_tx
= NULL
;
1408 s
->cookie_tx
= -EINVAL
;
1409 dmaengine_terminate_sync(chan
);
1410 dma_unmap_single(chan
->device
->dev
, s
->tx_dma_addr
, UART_XMIT_SIZE
,
1412 dma_release_channel(chan
);
1415 static int sci_dma_rx_submit(struct sci_port
*s
, bool port_lock_held
)
1417 struct dma_chan
*chan
= s
->chan_rx
;
1418 struct uart_port
*port
= &s
->port
;
1419 unsigned long flags
;
1422 for (i
= 0; i
< 2; i
++) {
1423 struct scatterlist
*sg
= &s
->sg_rx
[i
];
1424 struct dma_async_tx_descriptor
*desc
;
1426 desc
= dmaengine_prep_slave_sg(chan
,
1427 sg
, 1, DMA_DEV_TO_MEM
,
1428 DMA_PREP_INTERRUPT
| DMA_CTRL_ACK
);
1432 desc
->callback
= sci_dma_rx_complete
;
1433 desc
->callback_param
= s
;
1434 s
->cookie_rx
[i
] = dmaengine_submit(desc
);
1435 if (dma_submit_error(s
->cookie_rx
[i
]))
1440 s
->active_rx
= s
->cookie_rx
[0];
1442 dma_async_issue_pending(chan
);
1447 if (!port_lock_held
)
1448 uart_port_lock_irqsave(port
, &flags
);
1450 dmaengine_terminate_async(chan
);
1451 sci_dma_rx_chan_invalidate(s
);
1453 if (!port_lock_held
)
1454 uart_port_unlock_irqrestore(port
, flags
);
1458 static void sci_dma_tx_work_fn(struct work_struct
*work
)
1460 struct sci_port
*s
= container_of(work
, struct sci_port
, work_tx
);
1461 struct dma_async_tx_descriptor
*desc
;
1462 struct dma_chan
*chan
= s
->chan_tx
;
1463 struct uart_port
*port
= &s
->port
;
1464 struct tty_port
*tport
= &port
->state
->port
;
1465 unsigned long flags
;
1471 * Port xmit buffer is already mapped, and it is one page... Just adjust
1472 * offsets and lengths. Since it is a circular buffer, we have to
1473 * transmit till the end, and then the rest. Take the port lock to get a
1474 * consistent xmit buffer state.
1476 uart_port_lock_irq(port
);
1477 s
->tx_dma_len
= kfifo_out_linear(&tport
->xmit_fifo
, &tail
,
1479 buf
= s
->tx_dma_addr
+ tail
;
1480 if (!s
->tx_dma_len
) {
1481 /* Transmit buffer has been flushed */
1482 uart_port_unlock_irq(port
);
1486 desc
= dmaengine_prep_slave_single(chan
, buf
, s
->tx_dma_len
,
1488 DMA_PREP_INTERRUPT
| DMA_CTRL_ACK
);
1490 uart_port_unlock_irq(port
);
1491 dev_warn(port
->dev
, "Failed preparing Tx DMA descriptor\n");
1495 dma_sync_single_for_device(chan
->device
->dev
, buf
, s
->tx_dma_len
,
1498 desc
->callback
= sci_dma_tx_complete
;
1499 desc
->callback_param
= s
;
1500 s
->cookie_tx
= dmaengine_submit(desc
);
1501 if (dma_submit_error(s
->cookie_tx
)) {
1502 uart_port_unlock_irq(port
);
1503 dev_warn(port
->dev
, "Failed submitting Tx DMA descriptor\n");
1507 uart_port_unlock_irq(port
);
1508 dev_dbg(port
->dev
, "%s: %p: %u, cookie %d\n",
1509 __func__
, tport
->xmit_buf
, tail
, s
->cookie_tx
);
1511 dma_async_issue_pending(chan
);
1515 uart_port_lock_irqsave(port
, &flags
);
1518 uart_port_unlock_irqrestore(port
, flags
);
1522 static enum hrtimer_restart
sci_dma_rx_timer_fn(struct hrtimer
*t
)
1524 struct sci_port
*s
= container_of(t
, struct sci_port
, rx_timer
);
1525 struct dma_chan
*chan
= s
->chan_rx
;
1526 struct uart_port
*port
= &s
->port
;
1527 struct dma_tx_state state
;
1528 enum dma_status status
;
1529 unsigned long flags
;
1533 dev_dbg(port
->dev
, "DMA Rx timed out\n");
1535 uart_port_lock_irqsave(port
, &flags
);
1537 active
= sci_dma_rx_find_active(s
);
1539 uart_port_unlock_irqrestore(port
, flags
);
1540 return HRTIMER_NORESTART
;
1543 status
= dmaengine_tx_status(s
->chan_rx
, s
->active_rx
, &state
);
1544 if (status
== DMA_COMPLETE
) {
1545 uart_port_unlock_irqrestore(port
, flags
);
1546 dev_dbg(port
->dev
, "Cookie %d #%d has already completed\n",
1547 s
->active_rx
, active
);
1549 /* Let packet complete handler take care of the packet */
1550 return HRTIMER_NORESTART
;
1553 dmaengine_pause(chan
);
1556 * sometimes DMA transfer doesn't stop even if it is stopped and
1557 * data keeps on coming until transaction is complete so check
1558 * for DMA_COMPLETE again
1559 * Let packet complete handler take care of the packet
1561 status
= dmaengine_tx_status(s
->chan_rx
, s
->active_rx
, &state
);
1562 if (status
== DMA_COMPLETE
) {
1563 uart_port_unlock_irqrestore(port
, flags
);
1564 dev_dbg(port
->dev
, "Transaction complete after DMA engine was stopped");
1565 return HRTIMER_NORESTART
;
1568 /* Handle incomplete DMA receive */
1569 dmaengine_terminate_async(s
->chan_rx
);
1570 read
= sg_dma_len(&s
->sg_rx
[active
]) - state
.residue
;
1573 count
= sci_dma_rx_push(s
, s
->rx_buf
[active
], read
);
1575 tty_flip_buffer_push(&port
->state
->port
);
1578 if (port
->type
== PORT_SCIFA
|| port
->type
== PORT_SCIFB
||
1579 s
->cfg
->regtype
== SCIx_RZ_SCIFA_REGTYPE
)
1580 sci_dma_rx_submit(s
, true);
1582 sci_dma_rx_reenable_irq(s
);
1584 uart_port_unlock_irqrestore(port
, flags
);
1586 return HRTIMER_NORESTART
;
1589 static struct dma_chan
*sci_request_dma_chan(struct uart_port
*port
,
1590 enum dma_transfer_direction dir
)
1592 struct dma_chan
*chan
;
1593 struct dma_slave_config cfg
;
1596 chan
= dma_request_chan(port
->dev
, dir
== DMA_MEM_TO_DEV
? "tx" : "rx");
1598 dev_dbg(port
->dev
, "dma_request_chan failed\n");
1602 memset(&cfg
, 0, sizeof(cfg
));
1603 cfg
.direction
= dir
;
1604 cfg
.dst_addr
= port
->mapbase
+
1605 (sci_getreg(port
, SCxTDR
)->offset
<< port
->regshift
);
1606 cfg
.dst_addr_width
= DMA_SLAVE_BUSWIDTH_1_BYTE
;
1607 cfg
.src_addr
= port
->mapbase
+
1608 (sci_getreg(port
, SCxRDR
)->offset
<< port
->regshift
);
1609 cfg
.src_addr_width
= DMA_SLAVE_BUSWIDTH_1_BYTE
;
1611 ret
= dmaengine_slave_config(chan
, &cfg
);
1613 dev_warn(port
->dev
, "dmaengine_slave_config failed %d\n", ret
);
1614 dma_release_channel(chan
);
1621 static void sci_request_dma(struct uart_port
*port
)
1623 struct sci_port
*s
= to_sci_port(port
);
1624 struct tty_port
*tport
= &port
->state
->port
;
1625 struct dma_chan
*chan
;
1627 dev_dbg(port
->dev
, "%s: port %d\n", __func__
, port
->line
);
1630 * DMA on console may interfere with Kernel log messages which use
1631 * plain putchar(). So, simply don't use it with a console.
1633 if (uart_console(port
))
1636 if (!port
->dev
->of_node
)
1639 s
->cookie_tx
= -EINVAL
;
1642 * Don't request a dma channel if no channel was specified
1643 * in the device tree.
1645 if (!of_property_present(port
->dev
->of_node
, "dmas"))
1648 chan
= sci_request_dma_chan(port
, DMA_MEM_TO_DEV
);
1649 dev_dbg(port
->dev
, "%s: TX: got channel %p\n", __func__
, chan
);
1651 /* UART circular tx buffer is an aligned page. */
1652 s
->tx_dma_addr
= dma_map_single(chan
->device
->dev
,
1656 if (dma_mapping_error(chan
->device
->dev
, s
->tx_dma_addr
)) {
1657 dev_warn(port
->dev
, "Failed mapping Tx DMA descriptor\n");
1658 dma_release_channel(chan
);
1660 dev_dbg(port
->dev
, "%s: mapped %lu@%p to %pad\n",
1661 __func__
, UART_XMIT_SIZE
,
1662 tport
->xmit_buf
, &s
->tx_dma_addr
);
1664 INIT_WORK(&s
->work_tx
, sci_dma_tx_work_fn
);
1665 s
->chan_tx_saved
= s
->chan_tx
= chan
;
1669 chan
= sci_request_dma_chan(port
, DMA_DEV_TO_MEM
);
1670 dev_dbg(port
->dev
, "%s: RX: got channel %p\n", __func__
, chan
);
1676 s
->buf_len_rx
= 2 * max_t(size_t, 16, port
->fifosize
);
1677 buf
= dma_alloc_coherent(chan
->device
->dev
, s
->buf_len_rx
* 2,
1681 "Failed to allocate Rx dma buffer, using PIO\n");
1682 dma_release_channel(chan
);
1686 for (i
= 0; i
< 2; i
++) {
1687 struct scatterlist
*sg
= &s
->sg_rx
[i
];
1689 sg_init_table(sg
, 1);
1691 sg_dma_address(sg
) = dma
;
1692 sg_dma_len(sg
) = s
->buf_len_rx
;
1694 buf
+= s
->buf_len_rx
;
1695 dma
+= s
->buf_len_rx
;
1698 hrtimer_init(&s
->rx_timer
, CLOCK_MONOTONIC
, HRTIMER_MODE_REL
);
1699 s
->rx_timer
.function
= sci_dma_rx_timer_fn
;
1701 s
->chan_rx_saved
= s
->chan_rx
= chan
;
1703 if (port
->type
== PORT_SCIFA
|| port
->type
== PORT_SCIFB
||
1704 s
->cfg
->regtype
== SCIx_RZ_SCIFA_REGTYPE
)
1705 sci_dma_rx_submit(s
, false);
1709 static void sci_free_dma(struct uart_port
*port
)
1711 struct sci_port
*s
= to_sci_port(port
);
1713 if (s
->chan_tx_saved
)
1714 sci_dma_tx_release(s
);
1715 if (s
->chan_rx_saved
)
1716 sci_dma_rx_release(s
);
1719 static void sci_flush_buffer(struct uart_port
*port
)
1721 struct sci_port
*s
= to_sci_port(port
);
1724 * In uart_flush_buffer(), the xmit circular buffer has just been
1725 * cleared, so we have to reset tx_dma_len accordingly, and stop any
1730 dmaengine_terminate_async(s
->chan_tx
);
1731 s
->cookie_tx
= -EINVAL
;
1734 #else /* !CONFIG_SERIAL_SH_SCI_DMA */
1735 static inline void sci_request_dma(struct uart_port
*port
)
1739 static inline void sci_free_dma(struct uart_port
*port
)
1743 #define sci_flush_buffer NULL
1744 #endif /* !CONFIG_SERIAL_SH_SCI_DMA */
1746 static irqreturn_t
sci_rx_interrupt(int irq
, void *ptr
)
1748 struct uart_port
*port
= ptr
;
1749 struct sci_port
*s
= to_sci_port(port
);
1751 #ifdef CONFIG_SERIAL_SH_SCI_DMA
1753 u16 scr
= sci_serial_in(port
, SCSCR
);
1754 u16 ssr
= sci_serial_in(port
, SCxSR
);
1756 /* Disable future Rx interrupts */
1757 if (port
->type
== PORT_SCIFA
|| port
->type
== PORT_SCIFB
||
1758 s
->cfg
->regtype
== SCIx_RZ_SCIFA_REGTYPE
) {
1759 disable_irq_nosync(s
->irqs
[SCIx_RXI_IRQ
]);
1760 if (s
->cfg
->regtype
== SCIx_RZ_SCIFA_REGTYPE
) {
1761 scif_set_rtrg(port
, 1);
1767 if (sci_dma_rx_submit(s
, false) < 0)
1772 sci_serial_out(port
, SCSCR
, scr
);
1773 /* Clear current interrupt */
1774 sci_serial_out(port
, SCxSR
,
1775 ssr
& ~(SCIF_DR
| SCxSR_RDxF(port
)));
1776 dev_dbg(port
->dev
, "Rx IRQ %lu: setup t-out in %u us\n",
1777 jiffies
, s
->rx_timeout
);
1778 start_hrtimer_us(&s
->rx_timer
, s
->rx_timeout
);
1786 if (s
->rx_trigger
> 1 && s
->rx_fifo_timeout
> 0) {
1787 if (!scif_rtrg_enabled(port
))
1788 scif_set_rtrg(port
, s
->rx_trigger
);
1790 mod_timer(&s
->rx_fifo_timer
, jiffies
+ DIV_ROUND_UP(
1791 s
->rx_frame
* HZ
* s
->rx_fifo_timeout
, 1000000));
1794 /* I think sci_receive_chars has to be called irrespective
1795 * of whether the I_IXOFF is set, otherwise, how is the interrupt
1798 sci_receive_chars(port
);
1803 static irqreturn_t
sci_tx_interrupt(int irq
, void *ptr
)
1805 struct uart_port
*port
= ptr
;
1806 unsigned long flags
;
1808 uart_port_lock_irqsave(port
, &flags
);
1809 sci_transmit_chars(port
);
1810 uart_port_unlock_irqrestore(port
, flags
);
1815 static irqreturn_t
sci_tx_end_interrupt(int irq
, void *ptr
)
1817 struct uart_port
*port
= ptr
;
1818 unsigned long flags
;
1819 unsigned short ctrl
;
1821 if (port
->type
!= PORT_SCI
)
1822 return sci_tx_interrupt(irq
, ptr
);
1824 uart_port_lock_irqsave(port
, &flags
);
1825 ctrl
= sci_serial_in(port
, SCSCR
);
1826 ctrl
&= ~(SCSCR_TE
| SCSCR_TEIE
);
1827 sci_serial_out(port
, SCSCR
, ctrl
);
1828 uart_port_unlock_irqrestore(port
, flags
);
1833 static irqreturn_t
sci_br_interrupt(int irq
, void *ptr
)
1835 struct uart_port
*port
= ptr
;
1838 sci_handle_breaks(port
);
1840 /* drop invalid character received before break was detected */
1841 sci_serial_in(port
, SCxRDR
);
1843 sci_clear_SCxSR(port
, SCxSR_BREAK_CLEAR(port
));
1848 static irqreturn_t
sci_er_interrupt(int irq
, void *ptr
)
1850 struct uart_port
*port
= ptr
;
1851 struct sci_port
*s
= to_sci_port(port
);
1853 if (s
->irqs
[SCIx_ERI_IRQ
] == s
->irqs
[SCIx_BRI_IRQ
]) {
1854 /* Break and Error interrupts are muxed */
1855 unsigned short ssr_status
= sci_serial_in(port
, SCxSR
);
1857 /* Break Interrupt */
1858 if (ssr_status
& SCxSR_BRK(port
))
1859 sci_br_interrupt(irq
, ptr
);
1862 if (!(ssr_status
& SCxSR_ERRORS(port
)))
1867 if (port
->type
== PORT_SCI
) {
1868 if (sci_handle_errors(port
)) {
1869 /* discard character in rx buffer */
1870 sci_serial_in(port
, SCxSR
);
1871 sci_clear_SCxSR(port
, SCxSR_RDxF_CLEAR(port
));
1874 sci_handle_fifo_overrun(port
);
1876 sci_receive_chars(port
);
1879 sci_clear_SCxSR(port
, SCxSR_ERROR_CLEAR(port
));
1881 /* Kick the transmission */
1883 sci_tx_interrupt(irq
, ptr
);
1888 static irqreturn_t
sci_mpxed_interrupt(int irq
, void *ptr
)
1890 unsigned short ssr_status
, scr_status
, err_enabled
, orer_status
= 0;
1891 struct uart_port
*port
= ptr
;
1892 struct sci_port
*s
= to_sci_port(port
);
1893 irqreturn_t ret
= IRQ_NONE
;
1895 ssr_status
= sci_serial_in(port
, SCxSR
);
1896 scr_status
= sci_serial_in(port
, SCSCR
);
1897 if (s
->params
->overrun_reg
== SCxSR
)
1898 orer_status
= ssr_status
;
1899 else if (sci_getreg(port
, s
->params
->overrun_reg
)->size
)
1900 orer_status
= sci_serial_in(port
, s
->params
->overrun_reg
);
1902 err_enabled
= scr_status
& port_rx_irq_mask(port
);
1905 if ((ssr_status
& SCxSR_TDxE(port
)) && (scr_status
& SCSCR_TIE
) &&
1907 ret
= sci_tx_interrupt(irq
, ptr
);
1910 * Rx Interrupt: if we're using DMA, the DMA controller clears RDF /
1913 if (((ssr_status
& SCxSR_RDxF(port
)) || s
->chan_rx
) &&
1914 (scr_status
& SCSCR_RIE
))
1915 ret
= sci_rx_interrupt(irq
, ptr
);
1917 /* Error Interrupt */
1918 if ((ssr_status
& SCxSR_ERRORS(port
)) && err_enabled
)
1919 ret
= sci_er_interrupt(irq
, ptr
);
1921 /* Break Interrupt */
1922 if (s
->irqs
[SCIx_ERI_IRQ
] != s
->irqs
[SCIx_BRI_IRQ
] &&
1923 (ssr_status
& SCxSR_BRK(port
)) && err_enabled
)
1924 ret
= sci_br_interrupt(irq
, ptr
);
1926 /* Overrun Interrupt */
1927 if (orer_status
& s
->params
->overrun_mask
) {
1928 sci_handle_fifo_overrun(port
);
1935 static const struct sci_irq_desc
{
1937 irq_handler_t handler
;
1938 } sci_irq_desc
[] = {
1940 * Split out handlers, the default case.
1944 .handler
= sci_er_interrupt
,
1949 .handler
= sci_rx_interrupt
,
1954 .handler
= sci_tx_interrupt
,
1959 .handler
= sci_br_interrupt
,
1964 .handler
= sci_rx_interrupt
,
1969 .handler
= sci_tx_end_interrupt
,
1973 * Special muxed handler.
1977 .handler
= sci_mpxed_interrupt
,
1981 static int sci_request_irq(struct sci_port
*port
)
1983 struct uart_port
*up
= &port
->port
;
1984 int i
, j
, w
, ret
= 0;
1986 for (i
= j
= 0; i
< SCIx_NR_IRQS
; i
++, j
++) {
1987 const struct sci_irq_desc
*desc
;
1990 /* Check if already registered (muxed) */
1991 for (w
= 0; w
< i
; w
++)
1992 if (port
->irqs
[w
] == port
->irqs
[i
])
1997 if (SCIx_IRQ_IS_MUXED(port
)) {
2001 irq
= port
->irqs
[i
];
2004 * Certain port types won't support all of the
2005 * available interrupt sources.
2007 if (unlikely(irq
< 0))
2011 desc
= sci_irq_desc
+ i
;
2012 port
->irqstr
[j
] = kasprintf(GFP_KERNEL
, "%s:%s",
2013 dev_name(up
->dev
), desc
->desc
);
2014 if (!port
->irqstr
[j
]) {
2019 ret
= request_irq(irq
, desc
->handler
, up
->irqflags
,
2020 port
->irqstr
[j
], port
);
2021 if (unlikely(ret
)) {
2022 dev_err(up
->dev
, "Can't allocate %s IRQ\n", desc
->desc
);
2031 free_irq(port
->irqs
[i
], port
);
2035 kfree(port
->irqstr
[j
]);
2040 static void sci_free_irq(struct sci_port
*port
)
2045 * Intentionally in reverse order so we iterate over the muxed
2048 for (i
= 0; i
< SCIx_NR_IRQS
; i
++) {
2049 int irq
= port
->irqs
[i
];
2052 * Certain port types won't support all of the available
2053 * interrupt sources.
2055 if (unlikely(irq
< 0))
2058 /* Check if already freed (irq was muxed) */
2059 for (j
= 0; j
< i
; j
++)
2060 if (port
->irqs
[j
] == irq
)
2065 free_irq(port
->irqs
[i
], port
);
2066 kfree(port
->irqstr
[i
]);
2068 if (SCIx_IRQ_IS_MUXED(port
)) {
2069 /* If there's only one IRQ, we're done. */
2075 static unsigned int sci_tx_empty(struct uart_port
*port
)
2077 unsigned short status
= sci_serial_in(port
, SCxSR
);
2078 unsigned short in_tx_fifo
= sci_txfill(port
);
2080 return (status
& SCxSR_TEND(port
)) && !in_tx_fifo
? TIOCSER_TEMT
: 0;
2083 static void sci_set_rts(struct uart_port
*port
, bool state
)
2085 if (port
->type
== PORT_SCIFA
|| port
->type
== PORT_SCIFB
) {
2086 u16 data
= sci_serial_in(port
, SCPDR
);
2090 data
&= ~SCPDR_RTSD
;
2093 sci_serial_out(port
, SCPDR
, data
);
2095 /* RTS# is output */
2096 sci_serial_out(port
, SCPCR
,
2097 sci_serial_in(port
, SCPCR
) | SCPCR_RTSC
);
2098 } else if (sci_getreg(port
, SCSPTR
)->size
) {
2099 u16 ctrl
= sci_serial_in(port
, SCSPTR
);
2103 ctrl
&= ~SCSPTR_RTSDT
;
2105 ctrl
|= SCSPTR_RTSDT
;
2106 sci_serial_out(port
, SCSPTR
, ctrl
);
2110 static bool sci_get_cts(struct uart_port
*port
)
2112 if (port
->type
== PORT_SCIFA
|| port
->type
== PORT_SCIFB
) {
2114 return !(sci_serial_in(port
, SCPDR
) & SCPDR_CTSD
);
2115 } else if (sci_getreg(port
, SCSPTR
)->size
) {
2117 return !(sci_serial_in(port
, SCSPTR
) & SCSPTR_CTSDT
);
2124 * Modem control is a bit of a mixed bag for SCI(F) ports. Generally
2125 * CTS/RTS is supported in hardware by at least one port and controlled
2126 * via SCSPTR (SCxPCR for SCIFA/B parts), or external pins (presently
2127 * handled via the ->init_pins() op, which is a bit of a one-way street,
2128 * lacking any ability to defer pin control -- this will later be
2129 * converted over to the GPIO framework).
2131 * Other modes (such as loopback) are supported generically on certain
2132 * port types, but not others. For these it's sufficient to test for the
2133 * existence of the support register and simply ignore the port type.
2135 static void sci_set_mctrl(struct uart_port
*port
, unsigned int mctrl
)
2137 struct sci_port
*s
= to_sci_port(port
);
2139 if (mctrl
& TIOCM_LOOP
) {
2140 const struct plat_sci_reg
*reg
;
2143 * Standard loopback mode for SCFCR ports.
2145 reg
= sci_getreg(port
, SCFCR
);
2147 sci_serial_out(port
, SCFCR
,
2148 sci_serial_in(port
, SCFCR
) | SCFCR_LOOP
);
2151 mctrl_gpio_set(s
->gpios
, mctrl
);
2156 if (!(mctrl
& TIOCM_RTS
)) {
2157 /* Disable Auto RTS */
2158 if (s
->cfg
->regtype
!= SCIx_RZV2H_SCIF_REGTYPE
)
2159 sci_serial_out(port
, SCFCR
,
2160 sci_serial_in(port
, SCFCR
) & ~SCFCR_MCE
);
2163 sci_set_rts(port
, 0);
2164 } else if (s
->autorts
) {
2165 if (port
->type
== PORT_SCIFA
|| port
->type
== PORT_SCIFB
) {
2166 /* Enable RTS# pin function */
2167 sci_serial_out(port
, SCPCR
,
2168 sci_serial_in(port
, SCPCR
) & ~SCPCR_RTSC
);
2171 /* Enable Auto RTS */
2172 if (s
->cfg
->regtype
!= SCIx_RZV2H_SCIF_REGTYPE
)
2173 sci_serial_out(port
, SCFCR
,
2174 sci_serial_in(port
, SCFCR
) | SCFCR_MCE
);
2177 sci_set_rts(port
, 1);
2181 static unsigned int sci_get_mctrl(struct uart_port
*port
)
2183 struct sci_port
*s
= to_sci_port(port
);
2184 struct mctrl_gpios
*gpios
= s
->gpios
;
2185 unsigned int mctrl
= 0;
2187 mctrl_gpio_get(gpios
, &mctrl
);
2190 * CTS/RTS is handled in hardware when supported, while nothing
2194 if (sci_get_cts(port
))
2196 } else if (!mctrl_gpio_to_gpiod(gpios
, UART_GPIO_CTS
)) {
2199 if (!mctrl_gpio_to_gpiod(gpios
, UART_GPIO_DSR
))
2201 if (!mctrl_gpio_to_gpiod(gpios
, UART_GPIO_DCD
))
2207 static void sci_enable_ms(struct uart_port
*port
)
2209 mctrl_gpio_enable_ms(to_sci_port(port
)->gpios
);
2212 static void sci_break_ctl(struct uart_port
*port
, int break_state
)
2214 unsigned short scscr
, scsptr
;
2215 unsigned long flags
;
2217 /* check whether the port has SCSPTR */
2218 if (!sci_getreg(port
, SCSPTR
)->size
) {
2220 * Not supported by hardware. Most parts couple break and rx
2221 * interrupts together, with break detection always enabled.
2226 uart_port_lock_irqsave(port
, &flags
);
2227 scsptr
= sci_serial_in(port
, SCSPTR
);
2228 scscr
= sci_serial_in(port
, SCSCR
);
2230 if (break_state
== -1) {
2231 scsptr
= (scsptr
| SCSPTR_SPB2IO
) & ~SCSPTR_SPB2DT
;
2234 scsptr
= (scsptr
| SCSPTR_SPB2DT
) & ~SCSPTR_SPB2IO
;
2238 sci_serial_out(port
, SCSPTR
, scsptr
);
2239 sci_serial_out(port
, SCSCR
, scscr
);
2240 uart_port_unlock_irqrestore(port
, flags
);
2243 static int sci_startup(struct uart_port
*port
)
2245 struct sci_port
*s
= to_sci_port(port
);
2248 dev_dbg(port
->dev
, "%s(%d)\n", __func__
, port
->line
);
2250 sci_request_dma(port
);
2252 ret
= sci_request_irq(s
);
2253 if (unlikely(ret
< 0)) {
2261 static void sci_shutdown(struct uart_port
*port
)
2263 struct sci_port
*s
= to_sci_port(port
);
2264 unsigned long flags
;
2267 dev_dbg(port
->dev
, "%s(%d)\n", __func__
, port
->line
);
2270 mctrl_gpio_disable_ms(to_sci_port(port
)->gpios
);
2272 uart_port_lock_irqsave(port
, &flags
);
2276 * Stop RX and TX, disable related interrupts, keep clock source
2277 * and HSCIF TOT bits
2279 scr
= sci_serial_in(port
, SCSCR
);
2280 sci_serial_out(port
, SCSCR
,
2281 scr
& (SCSCR_CKE1
| SCSCR_CKE0
| s
->hscif_tot
));
2282 uart_port_unlock_irqrestore(port
, flags
);
2284 #ifdef CONFIG_SERIAL_SH_SCI_DMA
2285 if (s
->chan_rx_saved
) {
2286 dev_dbg(port
->dev
, "%s(%d) deleting rx_timer\n", __func__
,
2288 hrtimer_cancel(&s
->rx_timer
);
2292 if (s
->rx_trigger
> 1 && s
->rx_fifo_timeout
> 0)
2293 del_timer_sync(&s
->rx_fifo_timer
);
2298 static int sci_sck_calc(struct sci_port
*s
, unsigned int bps
,
2301 unsigned long freq
= s
->clk_rates
[SCI_SCK
];
2302 int err
, min_err
= INT_MAX
;
2305 if (s
->port
.type
!= PORT_HSCIF
)
2308 for_each_sr(sr
, s
) {
2309 err
= DIV_ROUND_CLOSEST(freq
, sr
) - bps
;
2310 if (abs(err
) >= abs(min_err
))
2320 dev_dbg(s
->port
.dev
, "SCK: %u%+d bps using SR %u\n", bps
, min_err
,
2325 static int sci_brg_calc(struct sci_port
*s
, unsigned int bps
,
2326 unsigned long freq
, unsigned int *dlr
,
2329 int err
, min_err
= INT_MAX
;
2330 unsigned int sr
, dl
;
2332 if (s
->port
.type
!= PORT_HSCIF
)
2335 for_each_sr(sr
, s
) {
2336 dl
= DIV_ROUND_CLOSEST(freq
, sr
* bps
);
2337 dl
= clamp(dl
, 1U, 65535U);
2339 err
= DIV_ROUND_CLOSEST(freq
, sr
* dl
) - bps
;
2340 if (abs(err
) >= abs(min_err
))
2351 dev_dbg(s
->port
.dev
, "BRG: %u%+d bps using DL %u SR %u\n", bps
,
2352 min_err
, *dlr
, *srr
+ 1);
2356 /* calculate sample rate, BRR, and clock select */
2357 static int sci_scbrr_calc(struct sci_port
*s
, unsigned int bps
,
2358 unsigned int *brr
, unsigned int *srr
,
2361 unsigned long freq
= s
->clk_rates
[SCI_FCK
];
2362 unsigned int sr
, br
, prediv
, scrate
, c
;
2363 int err
, min_err
= INT_MAX
;
2365 if (s
->port
.type
!= PORT_HSCIF
)
2369 * Find the combination of sample rate and clock select with the
2370 * smallest deviation from the desired baud rate.
2371 * Prefer high sample rates to maximise the receive margin.
2373 * M: Receive margin (%)
2374 * N: Ratio of bit rate to clock (N = sampling rate)
2375 * D: Clock duty (D = 0 to 1.0)
2376 * L: Frame length (L = 9 to 12)
2377 * F: Absolute value of clock frequency deviation
2379 * M = |(0.5 - 1 / 2 * N) - ((L - 0.5) * F) -
2380 * (|D - 0.5| / N * (1 + F))|
2381 * NOTE: Usually, treat D for 0.5, F is 0 by this calculation.
2383 for_each_sr(sr
, s
) {
2384 for (c
= 0; c
<= 3; c
++) {
2385 /* integerized formulas from HSCIF documentation */
2386 prediv
= sr
<< (2 * c
+ 1);
2389 * We need to calculate:
2391 * br = freq / (prediv * bps) clamped to [1..256]
2392 * err = freq / (br * prediv) - bps
2394 * Watch out for overflow when calculating the desired
2395 * sampling clock rate!
2397 if (bps
> UINT_MAX
/ prediv
)
2400 scrate
= prediv
* bps
;
2401 br
= DIV_ROUND_CLOSEST(freq
, scrate
);
2402 br
= clamp(br
, 1U, 256U);
2404 err
= DIV_ROUND_CLOSEST(freq
, br
* prediv
) - bps
;
2405 if (abs(err
) >= abs(min_err
))
2419 dev_dbg(s
->port
.dev
, "BRR: %u%+d bps using N %u SR %u cks %u\n", bps
,
2420 min_err
, *brr
, *srr
+ 1, *cks
);
2424 static void sci_reset(struct uart_port
*port
)
2426 const struct plat_sci_reg
*reg
;
2427 unsigned int status
;
2428 struct sci_port
*s
= to_sci_port(port
);
2430 sci_serial_out(port
, SCSCR
, s
->hscif_tot
); /* TE=0, RE=0, CKE1=0 */
2432 reg
= sci_getreg(port
, SCFCR
);
2434 sci_serial_out(port
, SCFCR
, SCFCR_RFRST
| SCFCR_TFRST
);
2436 sci_clear_SCxSR(port
,
2437 SCxSR_RDxF_CLEAR(port
) & SCxSR_ERROR_CLEAR(port
) &
2438 SCxSR_BREAK_CLEAR(port
));
2439 if (sci_getreg(port
, SCLSR
)->size
) {
2440 status
= sci_serial_in(port
, SCLSR
);
2441 status
&= ~(SCLSR_TO
| SCLSR_ORER
);
2442 sci_serial_out(port
, SCLSR
, status
);
2445 if (s
->rx_trigger
> 1) {
2446 if (s
->rx_fifo_timeout
) {
2447 scif_set_rtrg(port
, 1);
2448 timer_setup(&s
->rx_fifo_timer
, rx_fifo_timer_fn
, 0);
2450 if (port
->type
== PORT_SCIFA
||
2451 port
->type
== PORT_SCIFB
)
2452 scif_set_rtrg(port
, 1);
2454 scif_set_rtrg(port
, s
->rx_trigger
);
2459 static void sci_set_termios(struct uart_port
*port
, struct ktermios
*termios
,
2460 const struct ktermios
*old
)
2462 unsigned int baud
, smr_val
= SCSMR_ASYNC
, scr_val
= 0, i
, bits
;
2463 unsigned int brr
= 255, cks
= 0, srr
= 15, dl
= 0, sccks
= 0;
2464 unsigned int brr1
= 255, cks1
= 0, srr1
= 15, dl1
= 0;
2465 struct sci_port
*s
= to_sci_port(port
);
2466 const struct plat_sci_reg
*reg
;
2467 int min_err
= INT_MAX
, err
;
2468 unsigned long max_freq
= 0;
2470 unsigned long flags
;
2472 if ((termios
->c_cflag
& CSIZE
) == CS7
) {
2473 smr_val
|= SCSMR_CHR
;
2475 termios
->c_cflag
&= ~CSIZE
;
2476 termios
->c_cflag
|= CS8
;
2478 if (termios
->c_cflag
& PARENB
)
2479 smr_val
|= SCSMR_PE
;
2480 if (termios
->c_cflag
& PARODD
)
2481 smr_val
|= SCSMR_PE
| SCSMR_ODD
;
2482 if (termios
->c_cflag
& CSTOPB
)
2483 smr_val
|= SCSMR_STOP
;
2486 * earlyprintk comes here early on with port->uartclk set to zero.
2487 * the clock framework is not up and running at this point so here
2488 * we assume that 115200 is the maximum baud rate. please note that
2489 * the baud rate is not programmed during earlyprintk - it is assumed
2490 * that the previous boot loader has enabled required clocks and
2491 * setup the baud rate generator hardware for us already.
2493 if (!port
->uartclk
) {
2494 baud
= uart_get_baud_rate(port
, termios
, old
, 0, 115200);
2498 for (i
= 0; i
< SCI_NUM_CLKS
; i
++)
2499 max_freq
= max(max_freq
, s
->clk_rates
[i
]);
2501 baud
= uart_get_baud_rate(port
, termios
, old
, 0, max_freq
/ min_sr(s
));
2506 * There can be multiple sources for the sampling clock. Find the one
2507 * that gives us the smallest deviation from the desired baud rate.
2510 /* Optional Undivided External Clock */
2511 if (s
->clk_rates
[SCI_SCK
] && port
->type
!= PORT_SCIFA
&&
2512 port
->type
!= PORT_SCIFB
) {
2513 err
= sci_sck_calc(s
, baud
, &srr1
);
2514 if (abs(err
) < abs(min_err
)) {
2516 scr_val
= SCSCR_CKE1
;
2525 /* Optional BRG Frequency Divided External Clock */
2526 if (s
->clk_rates
[SCI_SCIF_CLK
] && sci_getreg(port
, SCDL
)->size
) {
2527 err
= sci_brg_calc(s
, baud
, s
->clk_rates
[SCI_SCIF_CLK
], &dl1
,
2529 if (abs(err
) < abs(min_err
)) {
2530 best_clk
= SCI_SCIF_CLK
;
2531 scr_val
= SCSCR_CKE1
;
2541 /* Optional BRG Frequency Divided Internal Clock */
2542 if (s
->clk_rates
[SCI_BRG_INT
] && sci_getreg(port
, SCDL
)->size
) {
2543 err
= sci_brg_calc(s
, baud
, s
->clk_rates
[SCI_BRG_INT
], &dl1
,
2545 if (abs(err
) < abs(min_err
)) {
2546 best_clk
= SCI_BRG_INT
;
2547 scr_val
= SCSCR_CKE1
;
2557 /* Divided Functional Clock using standard Bit Rate Register */
2558 err
= sci_scbrr_calc(s
, baud
, &brr1
, &srr1
, &cks1
);
2559 if (abs(err
) < abs(min_err
)) {
2570 dev_dbg(port
->dev
, "Using clk %pC for %u%+d bps\n",
2571 s
->clks
[best_clk
], baud
, min_err
);
2576 * Program the optional External Baud Rate Generator (BRG) first.
2577 * It controls the mux to select (H)SCK or frequency divided clock.
2579 if (best_clk
>= 0 && sci_getreg(port
, SCCKS
)->size
) {
2580 sci_serial_out(port
, SCDL
, dl
);
2581 sci_serial_out(port
, SCCKS
, sccks
);
2584 uart_port_lock_irqsave(port
, &flags
);
2588 uart_update_timeout(port
, termios
->c_cflag
, baud
);
2590 /* byte size and parity */
2591 bits
= tty_get_frame_size(termios
->c_cflag
);
2593 if (sci_getreg(port
, SEMR
)->size
)
2594 sci_serial_out(port
, SEMR
, 0);
2596 if (best_clk
>= 0) {
2597 if (port
->type
== PORT_SCIFA
|| port
->type
== PORT_SCIFB
)
2599 case 5: smr_val
|= SCSMR_SRC_5
; break;
2600 case 7: smr_val
|= SCSMR_SRC_7
; break;
2601 case 11: smr_val
|= SCSMR_SRC_11
; break;
2602 case 13: smr_val
|= SCSMR_SRC_13
; break;
2603 case 16: smr_val
|= SCSMR_SRC_16
; break;
2604 case 17: smr_val
|= SCSMR_SRC_17
; break;
2605 case 19: smr_val
|= SCSMR_SRC_19
; break;
2606 case 27: smr_val
|= SCSMR_SRC_27
; break;
2609 sci_serial_out(port
, SCSCR
, scr_val
| s
->hscif_tot
);
2610 sci_serial_out(port
, SCSMR
, smr_val
);
2611 sci_serial_out(port
, SCBRR
, brr
);
2612 if (sci_getreg(port
, HSSRR
)->size
) {
2613 unsigned int hssrr
= srr
| HSCIF_SRE
;
2614 /* Calculate deviation from intended rate at the
2615 * center of the last stop bit in sampling clocks.
2617 int last_stop
= bits
* 2 - 1;
2618 int deviation
= DIV_ROUND_CLOSEST(min_err
* last_stop
*
2622 if (abs(deviation
) >= 2) {
2623 /* At least two sampling clocks off at the
2624 * last stop bit; we can increase the error
2625 * margin by shifting the sampling point.
2627 int shift
= clamp(deviation
/ 2, -8, 7);
2629 hssrr
|= (shift
<< HSCIF_SRHP_SHIFT
) &
2631 hssrr
|= HSCIF_SRDE
;
2633 sci_serial_out(port
, HSSRR
, hssrr
);
2636 /* Wait one bit interval */
2637 udelay((1000000 + (baud
- 1)) / baud
);
2639 /* Don't touch the bit rate configuration */
2640 scr_val
= s
->cfg
->scscr
& (SCSCR_CKE1
| SCSCR_CKE0
);
2641 smr_val
|= sci_serial_in(port
, SCSMR
) &
2642 (SCSMR_CKEDG
| SCSMR_SRC_MASK
| SCSMR_CKS
);
2643 sci_serial_out(port
, SCSCR
, scr_val
| s
->hscif_tot
);
2644 sci_serial_out(port
, SCSMR
, smr_val
);
2647 sci_init_pins(port
, termios
->c_cflag
);
2649 port
->status
&= ~UPSTAT_AUTOCTS
;
2651 reg
= sci_getreg(port
, SCFCR
);
2653 unsigned short ctrl
= sci_serial_in(port
, SCFCR
);
2655 if ((port
->flags
& UPF_HARD_FLOW
) &&
2656 (termios
->c_cflag
& CRTSCTS
)) {
2657 /* There is no CTS interrupt to restart the hardware */
2658 port
->status
|= UPSTAT_AUTOCTS
;
2659 /* MCE is enabled when RTS is raised */
2664 * As we've done a sci_reset() above, ensure we don't
2665 * interfere with the FIFOs while toggling MCE. As the
2666 * reset values could still be set, simply mask them out.
2668 ctrl
&= ~(SCFCR_RFRST
| SCFCR_TFRST
);
2670 sci_serial_out(port
, SCFCR
, ctrl
);
2672 if (port
->flags
& UPF_HARD_FLOW
) {
2673 /* Refresh (Auto) RTS */
2674 sci_set_mctrl(port
, port
->mctrl
);
2678 * For SCI, TE (transmit enable) must be set after setting TIE
2679 * (transmit interrupt enable) or in the same instruction to
2680 * start the transmitting process. So skip setting TE here for SCI.
2682 if (port
->type
!= PORT_SCI
)
2683 scr_val
|= SCSCR_TE
;
2684 scr_val
|= SCSCR_RE
| (s
->cfg
->scscr
& ~(SCSCR_CKE1
| SCSCR_CKE0
));
2685 sci_serial_out(port
, SCSCR
, scr_val
| s
->hscif_tot
);
2686 if ((srr
+ 1 == 5) &&
2687 (port
->type
== PORT_SCIFA
|| port
->type
== PORT_SCIFB
)) {
2689 * In asynchronous mode, when the sampling rate is 1/5, first
2690 * received data may become invalid on some SCIFA and SCIFB.
2691 * To avoid this problem wait more than 1 serial data time (1
2692 * bit time x serial data number) after setting SCSCR.RE = 1.
2694 udelay(DIV_ROUND_UP(10 * 1000000, baud
));
2697 /* Calculate delay for 2 DMA buffers (4 FIFO). */
2698 s
->rx_frame
= (10000 * bits
) / (baud
/ 100);
2699 #ifdef CONFIG_SERIAL_SH_SCI_DMA
2700 s
->rx_timeout
= s
->buf_len_rx
* 2 * s
->rx_frame
;
2703 if ((termios
->c_cflag
& CREAD
) != 0)
2706 uart_port_unlock_irqrestore(port
, flags
);
2708 sci_port_disable(s
);
2710 if (UART_ENABLE_MS(port
, termios
->c_cflag
))
2711 sci_enable_ms(port
);
2714 static void sci_pm(struct uart_port
*port
, unsigned int state
,
2715 unsigned int oldstate
)
2717 struct sci_port
*sci_port
= to_sci_port(port
);
2720 case UART_PM_STATE_OFF
:
2721 sci_port_disable(sci_port
);
2724 sci_port_enable(sci_port
);
2729 static const char *sci_type(struct uart_port
*port
)
2731 switch (port
->type
) {
2749 static int sci_remap_port(struct uart_port
*port
)
2751 struct sci_port
*sport
= to_sci_port(port
);
2754 * Nothing to do if there's already an established membase.
2759 if (port
->dev
->of_node
|| (port
->flags
& UPF_IOREMAP
)) {
2760 port
->membase
= ioremap(port
->mapbase
, sport
->reg_size
);
2761 if (unlikely(!port
->membase
)) {
2762 dev_err(port
->dev
, "can't remap port#%d\n", port
->line
);
2767 * For the simple (and majority of) cases where we don't
2768 * need to do any remapping, just cast the cookie
2771 port
->membase
= (void __iomem
*)(uintptr_t)port
->mapbase
;
2777 static void sci_release_port(struct uart_port
*port
)
2779 struct sci_port
*sport
= to_sci_port(port
);
2781 if (port
->dev
->of_node
|| (port
->flags
& UPF_IOREMAP
)) {
2782 iounmap(port
->membase
);
2783 port
->membase
= NULL
;
2786 release_mem_region(port
->mapbase
, sport
->reg_size
);
2789 static int sci_request_port(struct uart_port
*port
)
2791 struct resource
*res
;
2792 struct sci_port
*sport
= to_sci_port(port
);
2795 res
= request_mem_region(port
->mapbase
, sport
->reg_size
,
2796 dev_name(port
->dev
));
2797 if (unlikely(res
== NULL
)) {
2798 dev_err(port
->dev
, "request_mem_region failed.");
2802 ret
= sci_remap_port(port
);
2803 if (unlikely(ret
!= 0)) {
2804 release_resource(res
);
2811 static void sci_config_port(struct uart_port
*port
, int flags
)
2813 if (flags
& UART_CONFIG_TYPE
) {
2814 struct sci_port
*sport
= to_sci_port(port
);
2816 port
->type
= sport
->cfg
->type
;
2817 sci_request_port(port
);
2821 static int sci_verify_port(struct uart_port
*port
, struct serial_struct
*ser
)
2823 if (ser
->baud_base
< 2400)
2824 /* No paper tape reader for Mitch.. */
2830 static const struct uart_ops sci_uart_ops
= {
2831 .tx_empty
= sci_tx_empty
,
2832 .set_mctrl
= sci_set_mctrl
,
2833 .get_mctrl
= sci_get_mctrl
,
2834 .start_tx
= sci_start_tx
,
2835 .stop_tx
= sci_stop_tx
,
2836 .stop_rx
= sci_stop_rx
,
2837 .enable_ms
= sci_enable_ms
,
2838 .break_ctl
= sci_break_ctl
,
2839 .startup
= sci_startup
,
2840 .shutdown
= sci_shutdown
,
2841 .flush_buffer
= sci_flush_buffer
,
2842 .set_termios
= sci_set_termios
,
2845 .release_port
= sci_release_port
,
2846 .request_port
= sci_request_port
,
2847 .config_port
= sci_config_port
,
2848 .verify_port
= sci_verify_port
,
2849 #ifdef CONFIG_CONSOLE_POLL
2850 .poll_get_char
= sci_poll_get_char
,
2851 .poll_put_char
= sci_poll_put_char
,
2855 static int sci_init_clocks(struct sci_port
*sci_port
, struct device
*dev
)
2857 const char *clk_names
[] = {
2860 [SCI_BRG_INT
] = "brg_int",
2861 [SCI_SCIF_CLK
] = "scif_clk",
2866 if (sci_port
->cfg
->type
== PORT_HSCIF
)
2867 clk_names
[SCI_SCK
] = "hsck";
2869 for (i
= 0; i
< SCI_NUM_CLKS
; i
++) {
2870 clk
= devm_clk_get_optional(dev
, clk_names
[i
]);
2872 return PTR_ERR(clk
);
2874 if (!clk
&& i
== SCI_FCK
) {
2876 * Not all SH platforms declare a clock lookup entry
2877 * for SCI devices, in which case we need to get the
2878 * global "peripheral_clk" clock.
2880 clk
= devm_clk_get(dev
, "peripheral_clk");
2882 return dev_err_probe(dev
, PTR_ERR(clk
),
2883 "failed to get %s\n",
2888 dev_dbg(dev
, "failed to get %s\n", clk_names
[i
]);
2890 dev_dbg(dev
, "clk %s is %pC rate %lu\n", clk_names
[i
],
2891 clk
, clk_get_rate(clk
));
2892 sci_port
->clks
[i
] = clk
;
2897 static const struct sci_port_params
*
2898 sci_probe_regmap(const struct plat_sci_port
*cfg
)
2900 unsigned int regtype
;
2902 if (cfg
->regtype
!= SCIx_PROBE_REGTYPE
)
2903 return &sci_port_params
[cfg
->regtype
];
2905 switch (cfg
->type
) {
2907 regtype
= SCIx_SCI_REGTYPE
;
2910 regtype
= SCIx_IRDA_REGTYPE
;
2913 regtype
= SCIx_SCIFA_REGTYPE
;
2916 regtype
= SCIx_SCIFB_REGTYPE
;
2920 * The SH-4 is a bit of a misnomer here, although that's
2921 * where this particular port layout originated. This
2922 * configuration (or some slight variation thereof)
2923 * remains the dominant model for all SCIFs.
2925 regtype
= SCIx_SH4_SCIF_REGTYPE
;
2928 regtype
= SCIx_HSCIF_REGTYPE
;
2931 pr_err("Can't probe register map for given port\n");
2935 return &sci_port_params
[regtype
];
2938 static int sci_init_single(struct platform_device
*dev
,
2939 struct sci_port
*sci_port
, unsigned int index
,
2940 const struct plat_sci_port
*p
, bool early
)
2942 struct uart_port
*port
= &sci_port
->port
;
2943 const struct resource
*res
;
2949 port
->ops
= &sci_uart_ops
;
2950 port
->iotype
= UPIO_MEM
;
2952 port
->has_sysrq
= IS_ENABLED(CONFIG_SERIAL_SH_SCI_CONSOLE
);
2954 res
= platform_get_resource(dev
, IORESOURCE_MEM
, 0);
2958 port
->mapbase
= res
->start
;
2959 sci_port
->reg_size
= resource_size(res
);
2961 for (i
= 0; i
< ARRAY_SIZE(sci_port
->irqs
); ++i
) {
2963 sci_port
->irqs
[i
] = platform_get_irq_optional(dev
, i
);
2965 sci_port
->irqs
[i
] = platform_get_irq(dev
, i
);
2969 * The fourth interrupt on SCI port is transmit end interrupt, so
2970 * shuffle the interrupts.
2972 if (p
->type
== PORT_SCI
)
2973 swap(sci_port
->irqs
[SCIx_BRI_IRQ
], sci_port
->irqs
[SCIx_TEI_IRQ
]);
2975 /* The SCI generates several interrupts. They can be muxed together or
2976 * connected to different interrupt lines. In the muxed case only one
2977 * interrupt resource is specified as there is only one interrupt ID.
2978 * In the non-muxed case, up to 6 interrupt signals might be generated
2979 * from the SCI, however those signals might have their own individual
2980 * interrupt ID numbers, or muxed together with another interrupt.
2982 if (sci_port
->irqs
[0] < 0)
2985 if (sci_port
->irqs
[1] < 0)
2986 for (i
= 1; i
< ARRAY_SIZE(sci_port
->irqs
); i
++)
2987 sci_port
->irqs
[i
] = sci_port
->irqs
[0];
2989 sci_port
->params
= sci_probe_regmap(p
);
2990 if (unlikely(sci_port
->params
== NULL
))
2995 sci_port
->rx_trigger
= 48;
2998 sci_port
->rx_trigger
= 64;
3001 sci_port
->rx_trigger
= 32;
3004 if (p
->regtype
== SCIx_SH7705_SCIF_REGTYPE
)
3005 /* RX triggering not implemented for this IP */
3006 sci_port
->rx_trigger
= 1;
3008 sci_port
->rx_trigger
= 8;
3011 sci_port
->rx_trigger
= 1;
3015 sci_port
->rx_fifo_timeout
= 0;
3016 sci_port
->hscif_tot
= 0;
3018 /* SCIFA on sh7723 and sh7724 need a custom sampling rate that doesn't
3019 * match the SoC datasheet, this should be investigated. Let platform
3020 * data override the sampling rate for now.
3022 sci_port
->sampling_rate_mask
= p
->sampling_rate
3023 ? SCI_SR(p
->sampling_rate
)
3024 : sci_port
->params
->sampling_rate_mask
;
3027 ret
= sci_init_clocks(sci_port
, &dev
->dev
);
3031 port
->dev
= &dev
->dev
;
3033 pm_runtime_enable(&dev
->dev
);
3036 port
->type
= p
->type
;
3037 port
->flags
= UPF_FIXED_PORT
| UPF_BOOT_AUTOCONF
| p
->flags
;
3038 port
->fifosize
= sci_port
->params
->fifosize
;
3040 if (port
->type
== PORT_SCI
&& !dev
->dev
.of_node
) {
3041 if (sci_port
->reg_size
>= 0x20)
3048 * The UART port needs an IRQ value, so we peg this to the RX IRQ
3049 * for the multi-IRQ ports, which is where we are primarily
3050 * concerned with the shutdown path synchronization.
3052 * For the muxed case there's nothing more to do.
3054 port
->irq
= sci_port
->irqs
[SCIx_RXI_IRQ
];
3060 static void sci_cleanup_single(struct sci_port
*port
)
3062 pm_runtime_disable(port
->port
.dev
);
3065 #if defined(CONFIG_SERIAL_SH_SCI_CONSOLE) || \
3066 defined(CONFIG_SERIAL_SH_SCI_EARLYCON)
3067 static void serial_console_putchar(struct uart_port
*port
, unsigned char ch
)
3069 sci_poll_put_char(port
, ch
);
3073 * Print a string to the serial port trying not to disturb
3074 * any possible real use of the port...
3076 static void serial_console_write(struct console
*co
, const char *s
,
3079 struct sci_port
*sci_port
= &sci_ports
[co
->index
];
3080 struct uart_port
*port
= &sci_port
->port
;
3081 unsigned short bits
, ctrl
, ctrl_temp
;
3082 unsigned long flags
;
3087 else if (oops_in_progress
)
3088 locked
= uart_port_trylock_irqsave(port
, &flags
);
3090 uart_port_lock_irqsave(port
, &flags
);
3092 /* first save SCSCR then disable interrupts, keep clock source */
3093 ctrl
= sci_serial_in(port
, SCSCR
);
3094 ctrl_temp
= SCSCR_RE
| SCSCR_TE
|
3095 (sci_port
->cfg
->scscr
& ~(SCSCR_CKE1
| SCSCR_CKE0
)) |
3096 (ctrl
& (SCSCR_CKE1
| SCSCR_CKE0
));
3097 sci_serial_out(port
, SCSCR
, ctrl_temp
| sci_port
->hscif_tot
);
3099 uart_console_write(port
, s
, count
, serial_console_putchar
);
3101 /* wait until fifo is empty and last bit has been transmitted */
3102 bits
= SCxSR_TDxE(port
) | SCxSR_TEND(port
);
3103 while ((sci_serial_in(port
, SCxSR
) & bits
) != bits
)
3106 /* restore the SCSCR */
3107 sci_serial_out(port
, SCSCR
, ctrl
);
3110 uart_port_unlock_irqrestore(port
, flags
);
3113 static int serial_console_setup(struct console
*co
, char *options
)
3115 struct sci_port
*sci_port
;
3116 struct uart_port
*port
;
3124 * Refuse to handle any bogus ports.
3126 if (co
->index
< 0 || co
->index
>= SCI_NPORTS
)
3129 sci_port
= &sci_ports
[co
->index
];
3130 port
= &sci_port
->port
;
3133 * Refuse to handle uninitialized ports.
3138 ret
= sci_remap_port(port
);
3139 if (unlikely(ret
!= 0))
3143 uart_parse_options(options
, &baud
, &parity
, &bits
, &flow
);
3145 return uart_set_options(port
, co
, baud
, parity
, bits
, flow
);
3148 static struct console serial_console
= {
3150 .device
= uart_console_device
,
3151 .write
= serial_console_write
,
3152 .setup
= serial_console_setup
,
3153 .flags
= CON_PRINTBUFFER
,
3155 .data
= &sci_uart_driver
,
3158 #ifdef CONFIG_SUPERH
3159 static char early_serial_buf
[32];
3161 static int early_serial_console_setup(struct console
*co
, char *options
)
3164 * This early console is always registered using the earlyprintk=
3165 * parameter, which does not call add_preferred_console(). Thus
3166 * @options is always NULL and the options for this early console
3167 * are passed using a custom buffer.
3171 return serial_console_setup(co
, early_serial_buf
);
3174 static struct console early_serial_console
= {
3175 .name
= "early_ttySC",
3176 .write
= serial_console_write
,
3177 .setup
= early_serial_console_setup
,
3178 .flags
= CON_PRINTBUFFER
,
3182 static int sci_probe_earlyprintk(struct platform_device
*pdev
)
3184 const struct plat_sci_port
*cfg
= dev_get_platdata(&pdev
->dev
);
3186 if (early_serial_console
.data
)
3189 early_serial_console
.index
= pdev
->id
;
3191 sci_init_single(pdev
, &sci_ports
[pdev
->id
], pdev
->id
, cfg
, true);
3193 if (!strstr(early_serial_buf
, "keep"))
3194 early_serial_console
.flags
|= CON_BOOT
;
3196 register_console(&early_serial_console
);
3201 #define SCI_CONSOLE (&serial_console)
3204 static inline int sci_probe_earlyprintk(struct platform_device
*pdev
)
3209 #define SCI_CONSOLE NULL
3211 #endif /* CONFIG_SERIAL_SH_SCI_CONSOLE || CONFIG_SERIAL_SH_SCI_EARLYCON */
3213 static const char banner
[] __initconst
= "SuperH (H)SCI(F) driver initialized";
3215 static DEFINE_MUTEX(sci_uart_registration_lock
);
3216 static struct uart_driver sci_uart_driver
= {
3217 .owner
= THIS_MODULE
,
3218 .driver_name
= "sci",
3219 .dev_name
= "ttySC",
3221 .minor
= SCI_MINOR_START
,
3223 .cons
= SCI_CONSOLE
,
3226 static void sci_remove(struct platform_device
*dev
)
3228 struct sci_port
*port
= platform_get_drvdata(dev
);
3229 unsigned int type
= port
->port
.type
; /* uart_remove_... clears it */
3231 sci_ports_in_use
&= ~BIT(port
->port
.line
);
3232 uart_remove_one_port(&sci_uart_driver
, &port
->port
);
3234 sci_cleanup_single(port
);
3236 if (port
->port
.fifosize
> 1)
3237 device_remove_file(&dev
->dev
, &dev_attr_rx_fifo_trigger
);
3238 if (type
== PORT_SCIFA
|| type
== PORT_SCIFB
|| type
== PORT_HSCIF
)
3239 device_remove_file(&dev
->dev
, &dev_attr_rx_fifo_timeout
);
3243 #define SCI_OF_DATA(type, regtype) (void *)((type) << 16 | (regtype))
3244 #define SCI_OF_TYPE(data) ((unsigned long)(data) >> 16)
3245 #define SCI_OF_REGTYPE(data) ((unsigned long)(data) & 0xffff)
3247 static const struct of_device_id of_sci_match
[] __maybe_unused
= {
3248 /* SoC-specific types */
3250 .compatible
= "renesas,scif-r7s72100",
3251 .data
= SCI_OF_DATA(PORT_SCIF
, SCIx_SH2_SCIF_FIFODATA_REGTYPE
),
3254 .compatible
= "renesas,scif-r7s9210",
3255 .data
= SCI_OF_DATA(PORT_SCIF
, SCIx_RZ_SCIFA_REGTYPE
),
3258 .compatible
= "renesas,scif-r9a07g044",
3259 .data
= SCI_OF_DATA(PORT_SCIF
, SCIx_RZ_SCIFA_REGTYPE
),
3262 .compatible
= "renesas,scif-r9a09g057",
3263 .data
= SCI_OF_DATA(PORT_SCIF
, SCIx_RZV2H_SCIF_REGTYPE
),
3265 /* Family-specific types */
3267 .compatible
= "renesas,rcar-gen1-scif",
3268 .data
= SCI_OF_DATA(PORT_SCIF
, SCIx_SH4_SCIF_BRG_REGTYPE
),
3270 .compatible
= "renesas,rcar-gen2-scif",
3271 .data
= SCI_OF_DATA(PORT_SCIF
, SCIx_SH4_SCIF_BRG_REGTYPE
),
3273 .compatible
= "renesas,rcar-gen3-scif",
3274 .data
= SCI_OF_DATA(PORT_SCIF
, SCIx_SH4_SCIF_BRG_REGTYPE
),
3276 .compatible
= "renesas,rcar-gen4-scif",
3277 .data
= SCI_OF_DATA(PORT_SCIF
, SCIx_SH4_SCIF_BRG_REGTYPE
),
3281 .compatible
= "renesas,scif",
3282 .data
= SCI_OF_DATA(PORT_SCIF
, SCIx_SH4_SCIF_REGTYPE
),
3284 .compatible
= "renesas,scifa",
3285 .data
= SCI_OF_DATA(PORT_SCIFA
, SCIx_SCIFA_REGTYPE
),
3287 .compatible
= "renesas,scifb",
3288 .data
= SCI_OF_DATA(PORT_SCIFB
, SCIx_SCIFB_REGTYPE
),
3290 .compatible
= "renesas,hscif",
3291 .data
= SCI_OF_DATA(PORT_HSCIF
, SCIx_HSCIF_REGTYPE
),
3293 .compatible
= "renesas,sci",
3294 .data
= SCI_OF_DATA(PORT_SCI
, SCIx_SCI_REGTYPE
),
3299 MODULE_DEVICE_TABLE(of
, of_sci_match
);
3301 static void sci_reset_control_assert(void *data
)
3303 reset_control_assert(data
);
3306 static struct plat_sci_port
*sci_parse_dt(struct platform_device
*pdev
,
3307 unsigned int *dev_id
)
3309 struct device_node
*np
= pdev
->dev
.of_node
;
3310 struct reset_control
*rstc
;
3311 struct plat_sci_port
*p
;
3312 struct sci_port
*sp
;
3316 if (!IS_ENABLED(CONFIG_OF
) || !np
)
3317 return ERR_PTR(-EINVAL
);
3319 data
= of_device_get_match_data(&pdev
->dev
);
3321 rstc
= devm_reset_control_get_optional_exclusive(&pdev
->dev
, NULL
);
3323 return ERR_PTR(dev_err_probe(&pdev
->dev
, PTR_ERR(rstc
),
3324 "failed to get reset ctrl\n"));
3326 ret
= reset_control_deassert(rstc
);
3328 dev_err(&pdev
->dev
, "failed to deassert reset %d\n", ret
);
3329 return ERR_PTR(ret
);
3332 ret
= devm_add_action_or_reset(&pdev
->dev
, sci_reset_control_assert
, rstc
);
3334 dev_err(&pdev
->dev
, "failed to register assert devm action, %d\n",
3336 return ERR_PTR(ret
);
3339 p
= devm_kzalloc(&pdev
->dev
, sizeof(struct plat_sci_port
), GFP_KERNEL
);
3341 return ERR_PTR(-ENOMEM
);
3343 /* Get the line number from the aliases node. */
3344 id
= of_alias_get_id(np
, "serial");
3345 if (id
< 0 && ~sci_ports_in_use
)
3346 id
= ffz(sci_ports_in_use
);
3348 dev_err(&pdev
->dev
, "failed to get alias id (%d)\n", id
);
3349 return ERR_PTR(-EINVAL
);
3351 if (id
>= ARRAY_SIZE(sci_ports
)) {
3352 dev_err(&pdev
->dev
, "serial%d out of range\n", id
);
3353 return ERR_PTR(-EINVAL
);
3356 sp
= &sci_ports
[id
];
3359 p
->type
= SCI_OF_TYPE(data
);
3360 p
->regtype
= SCI_OF_REGTYPE(data
);
3362 sp
->has_rtscts
= of_property_read_bool(np
, "uart-has-rtscts");
3367 static int sci_probe_single(struct platform_device
*dev
,
3369 struct plat_sci_port
*p
,
3370 struct sci_port
*sciport
)
3375 if (unlikely(index
>= SCI_NPORTS
)) {
3376 dev_notice(&dev
->dev
, "Attempting to register port %d when only %d are available\n",
3377 index
+1, SCI_NPORTS
);
3378 dev_notice(&dev
->dev
, "Consider bumping CONFIG_SERIAL_SH_SCI_NR_UARTS!\n");
3381 BUILD_BUG_ON(SCI_NPORTS
> sizeof(sci_ports_in_use
) * 8);
3382 if (sci_ports_in_use
& BIT(index
))
3385 mutex_lock(&sci_uart_registration_lock
);
3386 if (!sci_uart_driver
.state
) {
3387 ret
= uart_register_driver(&sci_uart_driver
);
3389 mutex_unlock(&sci_uart_registration_lock
);
3393 mutex_unlock(&sci_uart_registration_lock
);
3395 ret
= sci_init_single(dev
, sciport
, index
, p
, false);
3399 sciport
->gpios
= mctrl_gpio_init(&sciport
->port
, 0);
3400 if (IS_ERR(sciport
->gpios
))
3401 return PTR_ERR(sciport
->gpios
);
3403 if (sciport
->has_rtscts
) {
3404 if (mctrl_gpio_to_gpiod(sciport
->gpios
, UART_GPIO_CTS
) ||
3405 mctrl_gpio_to_gpiod(sciport
->gpios
, UART_GPIO_RTS
)) {
3406 dev_err(&dev
->dev
, "Conflicting RTS/CTS config\n");
3409 sciport
->port
.flags
|= UPF_HARD_FLOW
;
3412 ret
= uart_add_one_port(&sci_uart_driver
, &sciport
->port
);
3414 sci_cleanup_single(sciport
);
3421 static int sci_probe(struct platform_device
*dev
)
3423 struct plat_sci_port
*p
;
3424 struct sci_port
*sp
;
3425 unsigned int dev_id
;
3429 * If we've come here via earlyprintk initialization, head off to
3430 * the special early probe. We don't have sufficient device state
3431 * to make it beyond this yet.
3433 #ifdef CONFIG_SUPERH
3434 if (is_sh_early_platform_device(dev
))
3435 return sci_probe_earlyprintk(dev
);
3438 if (dev
->dev
.of_node
) {
3439 p
= sci_parse_dt(dev
, &dev_id
);
3443 p
= dev
->dev
.platform_data
;
3445 dev_err(&dev
->dev
, "no platform data supplied\n");
3452 sp
= &sci_ports
[dev_id
];
3453 platform_set_drvdata(dev
, sp
);
3455 ret
= sci_probe_single(dev
, dev_id
, p
, sp
);
3459 if (sp
->port
.fifosize
> 1) {
3460 ret
= device_create_file(&dev
->dev
, &dev_attr_rx_fifo_trigger
);
3464 if (sp
->port
.type
== PORT_SCIFA
|| sp
->port
.type
== PORT_SCIFB
||
3465 sp
->port
.type
== PORT_HSCIF
) {
3466 ret
= device_create_file(&dev
->dev
, &dev_attr_rx_fifo_timeout
);
3468 if (sp
->port
.fifosize
> 1) {
3469 device_remove_file(&dev
->dev
,
3470 &dev_attr_rx_fifo_trigger
);
3476 #ifdef CONFIG_SH_STANDARD_BIOS
3477 sh_bios_gdb_detach();
3480 sci_ports_in_use
|= BIT(dev_id
);
3484 static __maybe_unused
int sci_suspend(struct device
*dev
)
3486 struct sci_port
*sport
= dev_get_drvdata(dev
);
3489 uart_suspend_port(&sci_uart_driver
, &sport
->port
);
3494 static __maybe_unused
int sci_resume(struct device
*dev
)
3496 struct sci_port
*sport
= dev_get_drvdata(dev
);
3499 uart_resume_port(&sci_uart_driver
, &sport
->port
);
3504 static SIMPLE_DEV_PM_OPS(sci_dev_pm_ops
, sci_suspend
, sci_resume
);
3506 static struct platform_driver sci_driver
= {
3508 .remove
= sci_remove
,
3511 .pm
= &sci_dev_pm_ops
,
3512 .of_match_table
= of_match_ptr(of_sci_match
),
3516 static int __init
sci_init(void)
3518 pr_info("%s\n", banner
);
3520 return platform_driver_register(&sci_driver
);
3523 static void __exit
sci_exit(void)
3525 platform_driver_unregister(&sci_driver
);
3527 if (sci_uart_driver
.state
)
3528 uart_unregister_driver(&sci_uart_driver
);
3531 #if defined(CONFIG_SUPERH) && defined(CONFIG_SERIAL_SH_SCI_CONSOLE)
3532 sh_early_platform_init_buffer("earlyprintk", &sci_driver
,
3533 early_serial_buf
, ARRAY_SIZE(early_serial_buf
));
3535 #ifdef CONFIG_SERIAL_SH_SCI_EARLYCON
3536 static struct plat_sci_port port_cfg __initdata
;
3538 static int __init
early_console_setup(struct earlycon_device
*device
,
3541 if (!device
->port
.membase
)
3544 device
->port
.type
= type
;
3545 memcpy(&sci_ports
[0].port
, &device
->port
, sizeof(struct uart_port
));
3546 port_cfg
.type
= type
;
3547 sci_ports
[0].cfg
= &port_cfg
;
3548 sci_ports
[0].params
= sci_probe_regmap(&port_cfg
);
3549 port_cfg
.scscr
= sci_serial_in(&sci_ports
[0].port
, SCSCR
);
3550 sci_serial_out(&sci_ports
[0].port
, SCSCR
,
3551 SCSCR_RE
| SCSCR_TE
| port_cfg
.scscr
);
3553 device
->con
->write
= serial_console_write
;
3556 static int __init
sci_early_console_setup(struct earlycon_device
*device
,
3559 return early_console_setup(device
, PORT_SCI
);
3561 static int __init
scif_early_console_setup(struct earlycon_device
*device
,
3564 return early_console_setup(device
, PORT_SCIF
);
3566 static int __init
rzscifa_early_console_setup(struct earlycon_device
*device
,
3569 port_cfg
.regtype
= SCIx_RZ_SCIFA_REGTYPE
;
3570 return early_console_setup(device
, PORT_SCIF
);
3573 static int __init
rzv2hscif_early_console_setup(struct earlycon_device
*device
,
3576 port_cfg
.regtype
= SCIx_RZV2H_SCIF_REGTYPE
;
3577 return early_console_setup(device
, PORT_SCIF
);
3580 static int __init
scifa_early_console_setup(struct earlycon_device
*device
,
3583 return early_console_setup(device
, PORT_SCIFA
);
3585 static int __init
scifb_early_console_setup(struct earlycon_device
*device
,
3588 return early_console_setup(device
, PORT_SCIFB
);
3590 static int __init
hscif_early_console_setup(struct earlycon_device
*device
,
3593 return early_console_setup(device
, PORT_HSCIF
);
3596 OF_EARLYCON_DECLARE(sci
, "renesas,sci", sci_early_console_setup
);
3597 OF_EARLYCON_DECLARE(scif
, "renesas,scif", scif_early_console_setup
);
3598 OF_EARLYCON_DECLARE(scif
, "renesas,scif-r7s9210", rzscifa_early_console_setup
);
3599 OF_EARLYCON_DECLARE(scif
, "renesas,scif-r9a07g044", rzscifa_early_console_setup
);
3600 OF_EARLYCON_DECLARE(scif
, "renesas,scif-r9a09g057", rzv2hscif_early_console_setup
);
3601 OF_EARLYCON_DECLARE(scifa
, "renesas,scifa", scifa_early_console_setup
);
3602 OF_EARLYCON_DECLARE(scifb
, "renesas,scifb", scifb_early_console_setup
);
3603 OF_EARLYCON_DECLARE(hscif
, "renesas,hscif", hscif_early_console_setup
);
3604 #endif /* CONFIG_SERIAL_SH_SCI_EARLYCON */
3606 module_init(sci_init
);
3607 module_exit(sci_exit
);
3609 MODULE_LICENSE("GPL");
3610 MODULE_ALIAS("platform:sh-sci");
3611 MODULE_AUTHOR("Paul Mundt");
3612 MODULE_DESCRIPTION("SuperH (H)SCI(F) serial driver");