1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * SuperH on-chip serial module support. (SCI with no FIFO / with FIFO)
4 *
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).
8 *
9 * based off of the old drivers/char/sh-sci.c by:
10 *
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).
17 */
18 #undef DEBUG
19
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>
36 #include <linux/mm.h>
37 #include <linux/of.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>
51
52 #ifdef CONFIG_SUPERH
53 #include <asm/sh_bios.h>
54 #include <asm/platform_early.h>
55 #endif
56
57 #include "serial_mctrl_gpio.h"
58 #include "sh-sci.h"
59
60 /* Offsets into the sci_port->irqs array */
61 enum {
62 SCIx_ERI_IRQ,
63 SCIx_RXI_IRQ,
64 SCIx_TXI_IRQ,
65 SCIx_BRI_IRQ,
66 SCIx_DRI_IRQ,
67 SCIx_TEI_IRQ,
68 SCIx_NR_IRQS,
69
70 SCIx_MUX_IRQ = SCIx_NR_IRQS, /* special case */
71 };
72
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))
78
79 enum SCI_CLKS {
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 */
84 SCI_NUM_CLKS
85 };
86
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)
90
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)
94
95 #define min_sr(_port) ffs((_port)->sampling_rate_mask)
96 #define max_sr(_port) fls((_port)->sampling_rate_mask)
97
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)))
102
103 struct plat_sci_reg {
104 u8 offset, size;
105 };
106
107 struct sci_suspend_regs {
108 u16 scdl;
109 u16 sccks;
110 u16 scsmr;
111 u16 scscr;
112 u16 scfcr;
113 u16 scsptr;
114 u16 hssrr;
115 u16 scpcr;
116 u16 scpdr;
117 u8 scbrr;
118 u8 semr;
119 };
120
121 struct sci_port_params {
122 const struct plat_sci_reg regs[SCIx_NR_REGS];
123 unsigned int fifosize;
124 unsigned int overrun_reg;
125 unsigned int overrun_mask;
126 unsigned int sampling_rate_mask;
127 unsigned int error_mask;
128 unsigned int error_clear;
129 };
130
131 struct sci_port {
132 struct uart_port port;
133
134 /* Platform configuration */
135 const struct sci_port_params *params;
136 const struct plat_sci_port *cfg;
137 unsigned int sampling_rate_mask;
138 resource_size_t reg_size;
139 struct mctrl_gpios *gpios;
140
141 /* Clocks */
142 struct clk *clks[SCI_NUM_CLKS];
143 unsigned long clk_rates[SCI_NUM_CLKS];
144
145 int irqs[SCIx_NR_IRQS];
146 char *irqstr[SCIx_NR_IRQS];
147
148 struct dma_chan *chan_tx;
149 struct dma_chan *chan_rx;
150
151 struct reset_control *rstc;
152
153 #ifdef CONFIG_SERIAL_SH_SCI_DMA
154 struct dma_chan *chan_tx_saved;
155 struct dma_chan *chan_rx_saved;
156 dma_cookie_t cookie_tx;
157 dma_cookie_t cookie_rx[2];
158 dma_cookie_t active_rx;
159 dma_addr_t tx_dma_addr;
160 unsigned int tx_dma_len;
161 struct scatterlist sg_rx[2];
162 void *rx_buf[2];
163 size_t buf_len_rx;
164 struct work_struct work_tx;
165 struct hrtimer rx_timer;
166 unsigned int rx_timeout; /* microseconds */
167 #endif
168 unsigned int rx_frame;
169 int rx_trigger;
170 struct timer_list rx_fifo_timer;
171 int rx_fifo_timeout;
172 struct sci_suspend_regs suspend_regs;
173 u16 hscif_tot;
174
175 bool has_rtscts;
176 bool autorts;
177 bool tx_occurred;
178 };
179
180 #define SCI_NPORTS CONFIG_SERIAL_SH_SCI_NR_UARTS
181
182 static struct sci_port sci_ports[SCI_NPORTS];
183 static unsigned long sci_ports_in_use;
184 static struct uart_driver sci_uart_driver;
185 static bool sci_uart_earlycon;
186 static bool sci_uart_earlycon_dev_probing;
187
188 static inline struct sci_port *
to_sci_port(struct uart_port * uart)189 to_sci_port(struct uart_port *uart)
190 {
191 return container_of(uart, struct sci_port, port);
192 }
193
194 static const struct sci_port_params sci_port_params[SCIx_NR_REGTYPES] = {
195 /*
196 * Common SCI definitions, dependent on the port's regshift
197 * value.
198 */
199 [SCIx_SCI_REGTYPE] = {
200 .regs = {
201 [SCSMR] = { 0x00, 8 },
202 [SCBRR] = { 0x01, 8 },
203 [SCSCR] = { 0x02, 8 },
204 [SCxTDR] = { 0x03, 8 },
205 [SCxSR] = { 0x04, 8 },
206 [SCxRDR] = { 0x05, 8 },
207 },
208 .fifosize = 1,
209 .overrun_reg = SCxSR,
210 .overrun_mask = SCI_ORER,
211 .sampling_rate_mask = SCI_SR(32),
212 .error_mask = SCI_DEFAULT_ERROR_MASK | SCI_ORER,
213 .error_clear = SCI_ERROR_CLEAR & ~SCI_ORER,
214 },
215
216 /*
217 * Common definitions for legacy IrDA ports.
218 */
219 [SCIx_IRDA_REGTYPE] = {
220 .regs = {
221 [SCSMR] = { 0x00, 8 },
222 [SCBRR] = { 0x02, 8 },
223 [SCSCR] = { 0x04, 8 },
224 [SCxTDR] = { 0x06, 8 },
225 [SCxSR] = { 0x08, 16 },
226 [SCxRDR] = { 0x0a, 8 },
227 [SCFCR] = { 0x0c, 8 },
228 [SCFDR] = { 0x0e, 16 },
229 },
230 .fifosize = 1,
231 .overrun_reg = SCxSR,
232 .overrun_mask = SCI_ORER,
233 .sampling_rate_mask = SCI_SR(32),
234 .error_mask = SCI_DEFAULT_ERROR_MASK | SCI_ORER,
235 .error_clear = SCI_ERROR_CLEAR & ~SCI_ORER,
236 },
237
238 /*
239 * Common SCIFA definitions.
240 */
241 [SCIx_SCIFA_REGTYPE] = {
242 .regs = {
243 [SCSMR] = { 0x00, 16 },
244 [SCBRR] = { 0x04, 8 },
245 [SCSCR] = { 0x08, 16 },
246 [SCxTDR] = { 0x20, 8 },
247 [SCxSR] = { 0x14, 16 },
248 [SCxRDR] = { 0x24, 8 },
249 [SCFCR] = { 0x18, 16 },
250 [SCFDR] = { 0x1c, 16 },
251 [SCPCR] = { 0x30, 16 },
252 [SCPDR] = { 0x34, 16 },
253 },
254 .fifosize = 64,
255 .overrun_reg = SCxSR,
256 .overrun_mask = SCIFA_ORER,
257 .sampling_rate_mask = SCI_SR_SCIFAB,
258 .error_mask = SCIF_DEFAULT_ERROR_MASK | SCIFA_ORER,
259 .error_clear = SCIF_ERROR_CLEAR & ~SCIFA_ORER,
260 },
261
262 /*
263 * Common SCIFB definitions.
264 */
265 [SCIx_SCIFB_REGTYPE] = {
266 .regs = {
267 [SCSMR] = { 0x00, 16 },
268 [SCBRR] = { 0x04, 8 },
269 [SCSCR] = { 0x08, 16 },
270 [SCxTDR] = { 0x40, 8 },
271 [SCxSR] = { 0x14, 16 },
272 [SCxRDR] = { 0x60, 8 },
273 [SCFCR] = { 0x18, 16 },
274 [SCTFDR] = { 0x38, 16 },
275 [SCRFDR] = { 0x3c, 16 },
276 [SCPCR] = { 0x30, 16 },
277 [SCPDR] = { 0x34, 16 },
278 },
279 .fifosize = 256,
280 .overrun_reg = SCxSR,
281 .overrun_mask = SCIFA_ORER,
282 .sampling_rate_mask = SCI_SR_SCIFAB,
283 .error_mask = SCIF_DEFAULT_ERROR_MASK | SCIFA_ORER,
284 .error_clear = SCIF_ERROR_CLEAR & ~SCIFA_ORER,
285 },
286
287 /*
288 * Common SH-2(A) SCIF definitions for ports with FIFO data
289 * count registers.
290 */
291 [SCIx_SH2_SCIF_FIFODATA_REGTYPE] = {
292 .regs = {
293 [SCSMR] = { 0x00, 16 },
294 [SCBRR] = { 0x04, 8 },
295 [SCSCR] = { 0x08, 16 },
296 [SCxTDR] = { 0x0c, 8 },
297 [SCxSR] = { 0x10, 16 },
298 [SCxRDR] = { 0x14, 8 },
299 [SCFCR] = { 0x18, 16 },
300 [SCFDR] = { 0x1c, 16 },
301 [SCSPTR] = { 0x20, 16 },
302 [SCLSR] = { 0x24, 16 },
303 },
304 .fifosize = 16,
305 .overrun_reg = SCLSR,
306 .overrun_mask = SCLSR_ORER,
307 .sampling_rate_mask = SCI_SR(32),
308 .error_mask = SCIF_DEFAULT_ERROR_MASK,
309 .error_clear = SCIF_ERROR_CLEAR,
310 },
311
312 /*
313 * The "SCIFA" that is in RZ/A2, RZ/G2L and RZ/T.
314 * It looks like a normal SCIF with FIFO data, but with a
315 * compressed address space. Also, the break out of interrupts
316 * are different: ERI/BRI, RXI, TXI, TEI, DRI.
317 */
318 [SCIx_RZ_SCIFA_REGTYPE] = {
319 .regs = {
320 [SCSMR] = { 0x00, 16 },
321 [SCBRR] = { 0x02, 8 },
322 [SCSCR] = { 0x04, 16 },
323 [SCxTDR] = { 0x06, 8 },
324 [SCxSR] = { 0x08, 16 },
325 [SCxRDR] = { 0x0A, 8 },
326 [SCFCR] = { 0x0C, 16 },
327 [SCFDR] = { 0x0E, 16 },
328 [SCSPTR] = { 0x10, 16 },
329 [SCLSR] = { 0x12, 16 },
330 [SEMR] = { 0x14, 8 },
331 },
332 .fifosize = 16,
333 .overrun_reg = SCLSR,
334 .overrun_mask = SCLSR_ORER,
335 .sampling_rate_mask = SCI_SR(32),
336 .error_mask = SCIF_DEFAULT_ERROR_MASK,
337 .error_clear = SCIF_ERROR_CLEAR,
338 },
339
340 /*
341 * The "SCIF" that is in RZ/V2H(P) SoC is similar to one found on RZ/G2L SoC
342 * with below differences,
343 * - Break out of interrupts are different: ERI, BRI, RXI, TXI, TEI, DRI,
344 * TEI-DRI, RXI-EDGE and TXI-EDGE.
345 * - SCSMR register does not have CM bit (BIT(7)) ie it does not support synchronous mode.
346 * - SCFCR register does not have SCFCR_MCE bit.
347 * - SCSPTR register has only bits SCSPTR_SPB2DT and SCSPTR_SPB2IO.
348 */
349 [SCIx_RZV2H_SCIF_REGTYPE] = {
350 .regs = {
351 [SCSMR] = { 0x00, 16 },
352 [SCBRR] = { 0x02, 8 },
353 [SCSCR] = { 0x04, 16 },
354 [SCxTDR] = { 0x06, 8 },
355 [SCxSR] = { 0x08, 16 },
356 [SCxRDR] = { 0x0a, 8 },
357 [SCFCR] = { 0x0c, 16 },
358 [SCFDR] = { 0x0e, 16 },
359 [SCSPTR] = { 0x10, 16 },
360 [SCLSR] = { 0x12, 16 },
361 [SEMR] = { 0x14, 8 },
362 },
363 .fifosize = 16,
364 .overrun_reg = SCLSR,
365 .overrun_mask = SCLSR_ORER,
366 .sampling_rate_mask = SCI_SR(32),
367 .error_mask = SCIF_DEFAULT_ERROR_MASK,
368 .error_clear = SCIF_ERROR_CLEAR,
369 },
370
371 /*
372 * Common SH-3 SCIF definitions.
373 */
374 [SCIx_SH3_SCIF_REGTYPE] = {
375 .regs = {
376 [SCSMR] = { 0x00, 8 },
377 [SCBRR] = { 0x02, 8 },
378 [SCSCR] = { 0x04, 8 },
379 [SCxTDR] = { 0x06, 8 },
380 [SCxSR] = { 0x08, 16 },
381 [SCxRDR] = { 0x0a, 8 },
382 [SCFCR] = { 0x0c, 8 },
383 [SCFDR] = { 0x0e, 16 },
384 },
385 .fifosize = 16,
386 .overrun_reg = SCLSR,
387 .overrun_mask = SCLSR_ORER,
388 .sampling_rate_mask = SCI_SR(32),
389 .error_mask = SCIF_DEFAULT_ERROR_MASK,
390 .error_clear = SCIF_ERROR_CLEAR,
391 },
392
393 /*
394 * Common SH-4(A) SCIF(B) definitions.
395 */
396 [SCIx_SH4_SCIF_REGTYPE] = {
397 .regs = {
398 [SCSMR] = { 0x00, 16 },
399 [SCBRR] = { 0x04, 8 },
400 [SCSCR] = { 0x08, 16 },
401 [SCxTDR] = { 0x0c, 8 },
402 [SCxSR] = { 0x10, 16 },
403 [SCxRDR] = { 0x14, 8 },
404 [SCFCR] = { 0x18, 16 },
405 [SCFDR] = { 0x1c, 16 },
406 [SCSPTR] = { 0x20, 16 },
407 [SCLSR] = { 0x24, 16 },
408 },
409 .fifosize = 16,
410 .overrun_reg = SCLSR,
411 .overrun_mask = SCLSR_ORER,
412 .sampling_rate_mask = SCI_SR(32),
413 .error_mask = SCIF_DEFAULT_ERROR_MASK,
414 .error_clear = SCIF_ERROR_CLEAR,
415 },
416
417 /*
418 * Common SCIF definitions for ports with a Baud Rate Generator for
419 * External Clock (BRG).
420 */
421 [SCIx_SH4_SCIF_BRG_REGTYPE] = {
422 .regs = {
423 [SCSMR] = { 0x00, 16 },
424 [SCBRR] = { 0x04, 8 },
425 [SCSCR] = { 0x08, 16 },
426 [SCxTDR] = { 0x0c, 8 },
427 [SCxSR] = { 0x10, 16 },
428 [SCxRDR] = { 0x14, 8 },
429 [SCFCR] = { 0x18, 16 },
430 [SCFDR] = { 0x1c, 16 },
431 [SCSPTR] = { 0x20, 16 },
432 [SCLSR] = { 0x24, 16 },
433 [SCDL] = { 0x30, 16 },
434 [SCCKS] = { 0x34, 16 },
435 },
436 .fifosize = 16,
437 .overrun_reg = SCLSR,
438 .overrun_mask = SCLSR_ORER,
439 .sampling_rate_mask = SCI_SR(32),
440 .error_mask = SCIF_DEFAULT_ERROR_MASK,
441 .error_clear = SCIF_ERROR_CLEAR,
442 },
443
444 /*
445 * Common HSCIF definitions.
446 */
447 [SCIx_HSCIF_REGTYPE] = {
448 .regs = {
449 [SCSMR] = { 0x00, 16 },
450 [SCBRR] = { 0x04, 8 },
451 [SCSCR] = { 0x08, 16 },
452 [SCxTDR] = { 0x0c, 8 },
453 [SCxSR] = { 0x10, 16 },
454 [SCxRDR] = { 0x14, 8 },
455 [SCFCR] = { 0x18, 16 },
456 [SCFDR] = { 0x1c, 16 },
457 [SCSPTR] = { 0x20, 16 },
458 [SCLSR] = { 0x24, 16 },
459 [HSSRR] = { 0x40, 16 },
460 [SCDL] = { 0x30, 16 },
461 [SCCKS] = { 0x34, 16 },
462 [HSRTRGR] = { 0x54, 16 },
463 [HSTTRGR] = { 0x58, 16 },
464 },
465 .fifosize = 128,
466 .overrun_reg = SCLSR,
467 .overrun_mask = SCLSR_ORER,
468 .sampling_rate_mask = SCI_SR_RANGE(8, 32),
469 .error_mask = SCIF_DEFAULT_ERROR_MASK,
470 .error_clear = SCIF_ERROR_CLEAR,
471 },
472
473 /*
474 * Common SH-4(A) SCIF(B) definitions for ports without an SCSPTR
475 * register.
476 */
477 [SCIx_SH4_SCIF_NO_SCSPTR_REGTYPE] = {
478 .regs = {
479 [SCSMR] = { 0x00, 16 },
480 [SCBRR] = { 0x04, 8 },
481 [SCSCR] = { 0x08, 16 },
482 [SCxTDR] = { 0x0c, 8 },
483 [SCxSR] = { 0x10, 16 },
484 [SCxRDR] = { 0x14, 8 },
485 [SCFCR] = { 0x18, 16 },
486 [SCFDR] = { 0x1c, 16 },
487 [SCLSR] = { 0x24, 16 },
488 },
489 .fifosize = 16,
490 .overrun_reg = SCLSR,
491 .overrun_mask = SCLSR_ORER,
492 .sampling_rate_mask = SCI_SR(32),
493 .error_mask = SCIF_DEFAULT_ERROR_MASK,
494 .error_clear = SCIF_ERROR_CLEAR,
495 },
496
497 /*
498 * Common SH-4(A) SCIF(B) definitions for ports with FIFO data
499 * count registers.
500 */
501 [SCIx_SH4_SCIF_FIFODATA_REGTYPE] = {
502 .regs = {
503 [SCSMR] = { 0x00, 16 },
504 [SCBRR] = { 0x04, 8 },
505 [SCSCR] = { 0x08, 16 },
506 [SCxTDR] = { 0x0c, 8 },
507 [SCxSR] = { 0x10, 16 },
508 [SCxRDR] = { 0x14, 8 },
509 [SCFCR] = { 0x18, 16 },
510 [SCFDR] = { 0x1c, 16 },
511 [SCTFDR] = { 0x1c, 16 }, /* aliased to SCFDR */
512 [SCRFDR] = { 0x20, 16 },
513 [SCSPTR] = { 0x24, 16 },
514 [SCLSR] = { 0x28, 16 },
515 },
516 .fifosize = 16,
517 .overrun_reg = SCLSR,
518 .overrun_mask = SCLSR_ORER,
519 .sampling_rate_mask = SCI_SR(32),
520 .error_mask = SCIF_DEFAULT_ERROR_MASK,
521 .error_clear = SCIF_ERROR_CLEAR,
522 },
523
524 /*
525 * SH7705-style SCIF(B) ports, lacking both SCSPTR and SCLSR
526 * registers.
527 */
528 [SCIx_SH7705_SCIF_REGTYPE] = {
529 .regs = {
530 [SCSMR] = { 0x00, 16 },
531 [SCBRR] = { 0x04, 8 },
532 [SCSCR] = { 0x08, 16 },
533 [SCxTDR] = { 0x20, 8 },
534 [SCxSR] = { 0x14, 16 },
535 [SCxRDR] = { 0x24, 8 },
536 [SCFCR] = { 0x18, 16 },
537 [SCFDR] = { 0x1c, 16 },
538 },
539 .fifosize = 64,
540 .overrun_reg = SCxSR,
541 .overrun_mask = SCIFA_ORER,
542 .sampling_rate_mask = SCI_SR(16),
543 .error_mask = SCIF_DEFAULT_ERROR_MASK | SCIFA_ORER,
544 .error_clear = SCIF_ERROR_CLEAR & ~SCIFA_ORER,
545 },
546 };
547
548 #define sci_getreg(up, offset) (&to_sci_port(up)->params->regs[offset])
549
550 /*
551 * The "offset" here is rather misleading, in that it refers to an enum
552 * value relative to the port mapping rather than the fixed offset
553 * itself, which needs to be manually retrieved from the platform's
554 * register map for the given port.
555 */
sci_serial_in(struct uart_port * p,int offset)556 static unsigned int sci_serial_in(struct uart_port *p, int offset)
557 {
558 const struct plat_sci_reg *reg = sci_getreg(p, offset);
559
560 if (reg->size == 8)
561 return ioread8(p->membase + (reg->offset << p->regshift));
562 else if (reg->size == 16)
563 return ioread16(p->membase + (reg->offset << p->regshift));
564 else
565 WARN(1, "Invalid register access\n");
566
567 return 0;
568 }
569
sci_serial_out(struct uart_port * p,int offset,int value)570 static void sci_serial_out(struct uart_port *p, int offset, int value)
571 {
572 const struct plat_sci_reg *reg = sci_getreg(p, offset);
573
574 if (reg->size == 8)
575 iowrite8(value, p->membase + (reg->offset << p->regshift));
576 else if (reg->size == 16)
577 iowrite16(value, p->membase + (reg->offset << p->regshift));
578 else
579 WARN(1, "Invalid register access\n");
580 }
581
sci_port_enable(struct sci_port * sci_port)582 static void sci_port_enable(struct sci_port *sci_port)
583 {
584 unsigned int i;
585
586 if (!sci_port->port.dev)
587 return;
588
589 pm_runtime_get_sync(sci_port->port.dev);
590
591 for (i = 0; i < SCI_NUM_CLKS; i++) {
592 clk_prepare_enable(sci_port->clks[i]);
593 sci_port->clk_rates[i] = clk_get_rate(sci_port->clks[i]);
594 }
595 sci_port->port.uartclk = sci_port->clk_rates[SCI_FCK];
596 }
597
sci_port_disable(struct sci_port * sci_port)598 static void sci_port_disable(struct sci_port *sci_port)
599 {
600 unsigned int i;
601
602 if (!sci_port->port.dev)
603 return;
604
605 for (i = SCI_NUM_CLKS; i-- > 0; )
606 clk_disable_unprepare(sci_port->clks[i]);
607
608 pm_runtime_put_sync(sci_port->port.dev);
609 }
610
port_rx_irq_mask(struct uart_port * port)611 static inline unsigned long port_rx_irq_mask(struct uart_port *port)
612 {
613 /*
614 * Not all ports (such as SCIFA) will support REIE. Rather than
615 * special-casing the port type, we check the port initialization
616 * IRQ enable mask to see whether the IRQ is desired at all. If
617 * it's unset, it's logically inferred that there's no point in
618 * testing for it.
619 */
620 return SCSCR_RIE | (to_sci_port(port)->cfg->scscr & SCSCR_REIE);
621 }
622
sci_start_tx(struct uart_port * port)623 static void sci_start_tx(struct uart_port *port)
624 {
625 struct sci_port *s = to_sci_port(port);
626 unsigned short ctrl;
627
628 #ifdef CONFIG_SERIAL_SH_SCI_DMA
629 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB) {
630 u16 new, scr = sci_serial_in(port, SCSCR);
631 if (s->chan_tx)
632 new = scr | SCSCR_TDRQE;
633 else
634 new = scr & ~SCSCR_TDRQE;
635 if (new != scr)
636 sci_serial_out(port, SCSCR, new);
637 }
638
639 if (s->chan_tx && !kfifo_is_empty(&port->state->port.xmit_fifo) &&
640 dma_submit_error(s->cookie_tx)) {
641 if (s->cfg->regtype == SCIx_RZ_SCIFA_REGTYPE)
642 /* Switch irq from SCIF to DMA */
643 disable_irq_nosync(s->irqs[SCIx_TXI_IRQ]);
644
645 s->cookie_tx = 0;
646 schedule_work(&s->work_tx);
647 }
648 #endif
649
650 if (!s->chan_tx || s->cfg->regtype == SCIx_RZ_SCIFA_REGTYPE ||
651 port->type == PORT_SCIFA || port->type == PORT_SCIFB) {
652 /* Set TIE (Transmit Interrupt Enable) bit in SCSCR */
653 ctrl = sci_serial_in(port, SCSCR);
654
655 /*
656 * For SCI, TE (transmit enable) must be set after setting TIE
657 * (transmit interrupt enable) or in the same instruction to start
658 * the transmit process.
659 */
660 if (port->type == PORT_SCI)
661 ctrl |= SCSCR_TE;
662
663 sci_serial_out(port, SCSCR, ctrl | SCSCR_TIE);
664 }
665 }
666
sci_stop_tx(struct uart_port * port)667 static void sci_stop_tx(struct uart_port *port)
668 {
669 unsigned short ctrl;
670
671 /* Clear TIE (Transmit Interrupt Enable) bit in SCSCR */
672 ctrl = sci_serial_in(port, SCSCR);
673
674 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB)
675 ctrl &= ~SCSCR_TDRQE;
676
677 ctrl &= ~SCSCR_TIE;
678
679 sci_serial_out(port, SCSCR, ctrl);
680
681 #ifdef CONFIG_SERIAL_SH_SCI_DMA
682 if (to_sci_port(port)->chan_tx &&
683 !dma_submit_error(to_sci_port(port)->cookie_tx)) {
684 dmaengine_terminate_async(to_sci_port(port)->chan_tx);
685 to_sci_port(port)->cookie_tx = -EINVAL;
686 }
687 #endif
688 }
689
sci_start_rx(struct uart_port * port)690 static void sci_start_rx(struct uart_port *port)
691 {
692 unsigned short ctrl;
693
694 ctrl = sci_serial_in(port, SCSCR) | port_rx_irq_mask(port);
695
696 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB)
697 ctrl &= ~SCSCR_RDRQE;
698
699 sci_serial_out(port, SCSCR, ctrl);
700 }
701
sci_stop_rx(struct uart_port * port)702 static void sci_stop_rx(struct uart_port *port)
703 {
704 unsigned short ctrl;
705
706 ctrl = sci_serial_in(port, SCSCR);
707
708 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB)
709 ctrl &= ~SCSCR_RDRQE;
710
711 ctrl &= ~port_rx_irq_mask(port);
712
713 sci_serial_out(port, SCSCR, ctrl);
714 }
715
sci_clear_SCxSR(struct uart_port * port,unsigned int mask)716 static void sci_clear_SCxSR(struct uart_port *port, unsigned int mask)
717 {
718 if (port->type == PORT_SCI) {
719 /* Just store the mask */
720 sci_serial_out(port, SCxSR, mask);
721 } else if (to_sci_port(port)->params->overrun_mask == SCIFA_ORER) {
722 /* SCIFA/SCIFB and SCIF on SH7705/SH7720/SH7721 */
723 /* Only clear the status bits we want to clear */
724 sci_serial_out(port, SCxSR, sci_serial_in(port, SCxSR) & mask);
725 } else {
726 /* Store the mask, clear parity/framing errors */
727 sci_serial_out(port, SCxSR, mask & ~(SCIF_FERC | SCIF_PERC));
728 }
729 }
730
731 #if defined(CONFIG_CONSOLE_POLL) || defined(CONFIG_SERIAL_SH_SCI_CONSOLE) || \
732 defined(CONFIG_SERIAL_SH_SCI_EARLYCON)
733
734 #ifdef CONFIG_CONSOLE_POLL
sci_poll_get_char(struct uart_port * port)735 static int sci_poll_get_char(struct uart_port *port)
736 {
737 unsigned short status;
738 int c;
739
740 do {
741 status = sci_serial_in(port, SCxSR);
742 if (status & SCxSR_ERRORS(port)) {
743 sci_clear_SCxSR(port, SCxSR_ERROR_CLEAR(port));
744 continue;
745 }
746 break;
747 } while (1);
748
749 if (!(status & SCxSR_RDxF(port)))
750 return NO_POLL_CHAR;
751
752 c = sci_serial_in(port, SCxRDR);
753
754 /* Dummy read */
755 sci_serial_in(port, SCxSR);
756 sci_clear_SCxSR(port, SCxSR_RDxF_CLEAR(port));
757
758 return c;
759 }
760 #endif
761
sci_poll_put_char(struct uart_port * port,unsigned char c)762 static void sci_poll_put_char(struct uart_port *port, unsigned char c)
763 {
764 unsigned short status;
765
766 do {
767 status = sci_serial_in(port, SCxSR);
768 } while (!(status & SCxSR_TDxE(port)));
769
770 sci_serial_out(port, SCxTDR, c);
771 sci_clear_SCxSR(port, SCxSR_TDxE_CLEAR(port) & ~SCxSR_TEND(port));
772 }
773 #endif /* CONFIG_CONSOLE_POLL || CONFIG_SERIAL_SH_SCI_CONSOLE ||
774 CONFIG_SERIAL_SH_SCI_EARLYCON */
775
sci_init_pins(struct uart_port * port,unsigned int cflag)776 static void sci_init_pins(struct uart_port *port, unsigned int cflag)
777 {
778 struct sci_port *s = to_sci_port(port);
779
780 /*
781 * Use port-specific handler if provided.
782 */
783 if (s->cfg->ops && s->cfg->ops->init_pins) {
784 s->cfg->ops->init_pins(port, cflag);
785 return;
786 }
787
788 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB) {
789 u16 data = sci_serial_in(port, SCPDR);
790 u16 ctrl = sci_serial_in(port, SCPCR);
791
792 /* Enable RXD and TXD pin functions */
793 ctrl &= ~(SCPCR_RXDC | SCPCR_TXDC);
794 if (to_sci_port(port)->has_rtscts) {
795 /* RTS# is output, active low, unless autorts */
796 if (!(port->mctrl & TIOCM_RTS)) {
797 ctrl |= SCPCR_RTSC;
798 data |= SCPDR_RTSD;
799 } else if (!s->autorts) {
800 ctrl |= SCPCR_RTSC;
801 data &= ~SCPDR_RTSD;
802 } else {
803 /* Enable RTS# pin function */
804 ctrl &= ~SCPCR_RTSC;
805 }
806 /* Enable CTS# pin function */
807 ctrl &= ~SCPCR_CTSC;
808 }
809 sci_serial_out(port, SCPDR, data);
810 sci_serial_out(port, SCPCR, ctrl);
811 } else if (sci_getreg(port, SCSPTR)->size && s->cfg->regtype != SCIx_RZV2H_SCIF_REGTYPE) {
812 u16 status = sci_serial_in(port, SCSPTR);
813
814 /* RTS# is always output; and active low, unless autorts */
815 status |= SCSPTR_RTSIO;
816 if (!(port->mctrl & TIOCM_RTS))
817 status |= SCSPTR_RTSDT;
818 else if (!s->autorts)
819 status &= ~SCSPTR_RTSDT;
820 /* CTS# and SCK are inputs */
821 status &= ~(SCSPTR_CTSIO | SCSPTR_SCKIO);
822 sci_serial_out(port, SCSPTR, status);
823 }
824 }
825
sci_txfill(struct uart_port * port)826 static int sci_txfill(struct uart_port *port)
827 {
828 struct sci_port *s = to_sci_port(port);
829 unsigned int fifo_mask = (s->params->fifosize << 1) - 1;
830 const struct plat_sci_reg *reg;
831
832 reg = sci_getreg(port, SCTFDR);
833 if (reg->size)
834 return sci_serial_in(port, SCTFDR) & fifo_mask;
835
836 reg = sci_getreg(port, SCFDR);
837 if (reg->size)
838 return sci_serial_in(port, SCFDR) >> 8;
839
840 return !(sci_serial_in(port, SCxSR) & SCI_TDRE);
841 }
842
sci_txroom(struct uart_port * port)843 static int sci_txroom(struct uart_port *port)
844 {
845 return port->fifosize - sci_txfill(port);
846 }
847
sci_rxfill(struct uart_port * port)848 static int sci_rxfill(struct uart_port *port)
849 {
850 struct sci_port *s = to_sci_port(port);
851 unsigned int fifo_mask = (s->params->fifosize << 1) - 1;
852 const struct plat_sci_reg *reg;
853
854 reg = sci_getreg(port, SCRFDR);
855 if (reg->size)
856 return sci_serial_in(port, SCRFDR) & fifo_mask;
857
858 reg = sci_getreg(port, SCFDR);
859 if (reg->size)
860 return sci_serial_in(port, SCFDR) & fifo_mask;
861
862 return (sci_serial_in(port, SCxSR) & SCxSR_RDxF(port)) != 0;
863 }
864
865 /* ********************************************************************** *
866 * the interrupt related routines *
867 * ********************************************************************** */
868
sci_transmit_chars(struct uart_port * port)869 static void sci_transmit_chars(struct uart_port *port)
870 {
871 struct tty_port *tport = &port->state->port;
872 unsigned int stopped = uart_tx_stopped(port);
873 struct sci_port *s = to_sci_port(port);
874 unsigned short status;
875 unsigned short ctrl;
876 int count;
877
878 status = sci_serial_in(port, SCxSR);
879 if (!(status & SCxSR_TDxE(port))) {
880 ctrl = sci_serial_in(port, SCSCR);
881 if (kfifo_is_empty(&tport->xmit_fifo))
882 ctrl &= ~SCSCR_TIE;
883 else
884 ctrl |= SCSCR_TIE;
885 sci_serial_out(port, SCSCR, ctrl);
886 return;
887 }
888
889 count = sci_txroom(port);
890
891 do {
892 unsigned char c;
893
894 if (port->x_char) {
895 c = port->x_char;
896 port->x_char = 0;
897 } else if (stopped || !kfifo_get(&tport->xmit_fifo, &c)) {
898 if (port->type == PORT_SCI &&
899 kfifo_is_empty(&tport->xmit_fifo)) {
900 ctrl = sci_serial_in(port, SCSCR);
901 ctrl &= ~SCSCR_TE;
902 sci_serial_out(port, SCSCR, ctrl);
903 return;
904 }
905 break;
906 }
907
908 sci_serial_out(port, SCxTDR, c);
909 s->tx_occurred = true;
910
911 port->icount.tx++;
912 } while (--count > 0);
913
914 sci_clear_SCxSR(port, SCxSR_TDxE_CLEAR(port));
915
916 if (kfifo_len(&tport->xmit_fifo) < WAKEUP_CHARS)
917 uart_write_wakeup(port);
918 if (kfifo_is_empty(&tport->xmit_fifo)) {
919 if (port->type == PORT_SCI) {
920 ctrl = sci_serial_in(port, SCSCR);
921 ctrl &= ~SCSCR_TIE;
922 ctrl |= SCSCR_TEIE;
923 sci_serial_out(port, SCSCR, ctrl);
924 }
925
926 sci_stop_tx(port);
927 }
928 }
929
sci_receive_chars(struct uart_port * port)930 static void sci_receive_chars(struct uart_port *port)
931 {
932 struct tty_port *tport = &port->state->port;
933 int i, count, copied = 0;
934 unsigned short status;
935 unsigned char flag;
936
937 status = sci_serial_in(port, SCxSR);
938 if (!(status & SCxSR_RDxF(port)))
939 return;
940
941 while (1) {
942 /* Don't copy more bytes than there is room for in the buffer */
943 count = tty_buffer_request_room(tport, sci_rxfill(port));
944
945 /* If for any reason we can't copy more data, we're done! */
946 if (count == 0)
947 break;
948
949 if (port->type == PORT_SCI) {
950 char c = sci_serial_in(port, SCxRDR);
951 if (uart_handle_sysrq_char(port, c))
952 count = 0;
953 else
954 tty_insert_flip_char(tport, c, TTY_NORMAL);
955 } else {
956 for (i = 0; i < count; i++) {
957 char c;
958
959 if (port->type == PORT_SCIF ||
960 port->type == PORT_HSCIF) {
961 status = sci_serial_in(port, SCxSR);
962 c = sci_serial_in(port, SCxRDR);
963 } else {
964 c = sci_serial_in(port, SCxRDR);
965 status = sci_serial_in(port, SCxSR);
966 }
967 if (uart_handle_sysrq_char(port, c)) {
968 count--; i--;
969 continue;
970 }
971
972 /* Store data and status */
973 if (status & SCxSR_FER(port)) {
974 flag = TTY_FRAME;
975 port->icount.frame++;
976 } else if (status & SCxSR_PER(port)) {
977 flag = TTY_PARITY;
978 port->icount.parity++;
979 } else
980 flag = TTY_NORMAL;
981
982 tty_insert_flip_char(tport, c, flag);
983 }
984 }
985
986 sci_serial_in(port, SCxSR); /* dummy read */
987 sci_clear_SCxSR(port, SCxSR_RDxF_CLEAR(port));
988
989 copied += count;
990 port->icount.rx += count;
991 }
992
993 if (copied) {
994 /* Tell the rest of the system the news. New characters! */
995 tty_flip_buffer_push(tport);
996 } else {
997 /* TTY buffers full; read from RX reg to prevent lockup */
998 sci_serial_in(port, SCxRDR);
999 sci_serial_in(port, SCxSR); /* dummy read */
1000 sci_clear_SCxSR(port, SCxSR_RDxF_CLEAR(port));
1001 }
1002 }
1003
sci_handle_errors(struct uart_port * port)1004 static int sci_handle_errors(struct uart_port *port)
1005 {
1006 int copied = 0;
1007 unsigned short status = sci_serial_in(port, SCxSR);
1008 struct tty_port *tport = &port->state->port;
1009 struct sci_port *s = to_sci_port(port);
1010
1011 /* Handle overruns */
1012 if (status & s->params->overrun_mask) {
1013 port->icount.overrun++;
1014
1015 /* overrun error */
1016 if (tty_insert_flip_char(tport, 0, TTY_OVERRUN))
1017 copied++;
1018 }
1019
1020 if (status & SCxSR_FER(port)) {
1021 /* frame error */
1022 port->icount.frame++;
1023
1024 if (tty_insert_flip_char(tport, 0, TTY_FRAME))
1025 copied++;
1026 }
1027
1028 if (status & SCxSR_PER(port)) {
1029 /* parity error */
1030 port->icount.parity++;
1031
1032 if (tty_insert_flip_char(tport, 0, TTY_PARITY))
1033 copied++;
1034 }
1035
1036 if (copied)
1037 tty_flip_buffer_push(tport);
1038
1039 return copied;
1040 }
1041
sci_handle_fifo_overrun(struct uart_port * port)1042 static int sci_handle_fifo_overrun(struct uart_port *port)
1043 {
1044 struct tty_port *tport = &port->state->port;
1045 struct sci_port *s = to_sci_port(port);
1046 const struct plat_sci_reg *reg;
1047 int copied = 0;
1048 u16 status;
1049
1050 reg = sci_getreg(port, s->params->overrun_reg);
1051 if (!reg->size)
1052 return 0;
1053
1054 status = sci_serial_in(port, s->params->overrun_reg);
1055 if (status & s->params->overrun_mask) {
1056 status &= ~s->params->overrun_mask;
1057 sci_serial_out(port, s->params->overrun_reg, status);
1058
1059 port->icount.overrun++;
1060
1061 tty_insert_flip_char(tport, 0, TTY_OVERRUN);
1062 tty_flip_buffer_push(tport);
1063 copied++;
1064 }
1065
1066 return copied;
1067 }
1068
sci_handle_breaks(struct uart_port * port)1069 static int sci_handle_breaks(struct uart_port *port)
1070 {
1071 int copied = 0;
1072 unsigned short status = sci_serial_in(port, SCxSR);
1073 struct tty_port *tport = &port->state->port;
1074
1075 if (uart_handle_break(port))
1076 return 0;
1077
1078 if (status & SCxSR_BRK(port)) {
1079 port->icount.brk++;
1080
1081 /* Notify of BREAK */
1082 if (tty_insert_flip_char(tport, 0, TTY_BREAK))
1083 copied++;
1084 }
1085
1086 if (copied)
1087 tty_flip_buffer_push(tport);
1088
1089 copied += sci_handle_fifo_overrun(port);
1090
1091 return copied;
1092 }
1093
scif_set_rtrg(struct uart_port * port,int rx_trig)1094 static int scif_set_rtrg(struct uart_port *port, int rx_trig)
1095 {
1096 unsigned int bits;
1097
1098 if (rx_trig >= port->fifosize)
1099 rx_trig = port->fifosize - 1;
1100 if (rx_trig < 1)
1101 rx_trig = 1;
1102
1103 /* HSCIF can be set to an arbitrary level. */
1104 if (sci_getreg(port, HSRTRGR)->size) {
1105 sci_serial_out(port, HSRTRGR, rx_trig);
1106 return rx_trig;
1107 }
1108
1109 switch (port->type) {
1110 case PORT_SCIF:
1111 if (rx_trig < 4) {
1112 bits = 0;
1113 rx_trig = 1;
1114 } else if (rx_trig < 8) {
1115 bits = SCFCR_RTRG0;
1116 rx_trig = 4;
1117 } else if (rx_trig < 14) {
1118 bits = SCFCR_RTRG1;
1119 rx_trig = 8;
1120 } else {
1121 bits = SCFCR_RTRG0 | SCFCR_RTRG1;
1122 rx_trig = 14;
1123 }
1124 break;
1125 case PORT_SCIFA:
1126 case PORT_SCIFB:
1127 if (rx_trig < 16) {
1128 bits = 0;
1129 rx_trig = 1;
1130 } else if (rx_trig < 32) {
1131 bits = SCFCR_RTRG0;
1132 rx_trig = 16;
1133 } else if (rx_trig < 48) {
1134 bits = SCFCR_RTRG1;
1135 rx_trig = 32;
1136 } else {
1137 bits = SCFCR_RTRG0 | SCFCR_RTRG1;
1138 rx_trig = 48;
1139 }
1140 break;
1141 default:
1142 WARN(1, "unknown FIFO configuration");
1143 return 1;
1144 }
1145
1146 sci_serial_out(port, SCFCR,
1147 (sci_serial_in(port, SCFCR) &
1148 ~(SCFCR_RTRG1 | SCFCR_RTRG0)) | bits);
1149
1150 return rx_trig;
1151 }
1152
scif_rtrg_enabled(struct uart_port * port)1153 static int scif_rtrg_enabled(struct uart_port *port)
1154 {
1155 if (sci_getreg(port, HSRTRGR)->size)
1156 return sci_serial_in(port, HSRTRGR) != 0;
1157 else
1158 return (sci_serial_in(port, SCFCR) &
1159 (SCFCR_RTRG0 | SCFCR_RTRG1)) != 0;
1160 }
1161
rx_fifo_timer_fn(struct timer_list * t)1162 static void rx_fifo_timer_fn(struct timer_list *t)
1163 {
1164 struct sci_port *s = from_timer(s, t, rx_fifo_timer);
1165 struct uart_port *port = &s->port;
1166
1167 dev_dbg(port->dev, "Rx timed out\n");
1168 scif_set_rtrg(port, 1);
1169 }
1170
rx_fifo_trigger_show(struct device * dev,struct device_attribute * attr,char * buf)1171 static ssize_t rx_fifo_trigger_show(struct device *dev,
1172 struct device_attribute *attr, char *buf)
1173 {
1174 struct uart_port *port = dev_get_drvdata(dev);
1175 struct sci_port *sci = to_sci_port(port);
1176
1177 return sprintf(buf, "%d\n", sci->rx_trigger);
1178 }
1179
rx_fifo_trigger_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)1180 static ssize_t rx_fifo_trigger_store(struct device *dev,
1181 struct device_attribute *attr,
1182 const char *buf, size_t count)
1183 {
1184 struct uart_port *port = dev_get_drvdata(dev);
1185 struct sci_port *sci = to_sci_port(port);
1186 int ret;
1187 long r;
1188
1189 ret = kstrtol(buf, 0, &r);
1190 if (ret)
1191 return ret;
1192
1193 sci->rx_trigger = scif_set_rtrg(port, r);
1194 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB)
1195 scif_set_rtrg(port, 1);
1196
1197 return count;
1198 }
1199
1200 static DEVICE_ATTR_RW(rx_fifo_trigger);
1201
rx_fifo_timeout_show(struct device * dev,struct device_attribute * attr,char * buf)1202 static ssize_t rx_fifo_timeout_show(struct device *dev,
1203 struct device_attribute *attr,
1204 char *buf)
1205 {
1206 struct uart_port *port = dev_get_drvdata(dev);
1207 struct sci_port *sci = to_sci_port(port);
1208 int v;
1209
1210 if (port->type == PORT_HSCIF)
1211 v = sci->hscif_tot >> HSSCR_TOT_SHIFT;
1212 else
1213 v = sci->rx_fifo_timeout;
1214
1215 return sprintf(buf, "%d\n", v);
1216 }
1217
rx_fifo_timeout_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)1218 static ssize_t rx_fifo_timeout_store(struct device *dev,
1219 struct device_attribute *attr,
1220 const char *buf,
1221 size_t count)
1222 {
1223 struct uart_port *port = dev_get_drvdata(dev);
1224 struct sci_port *sci = to_sci_port(port);
1225 int ret;
1226 long r;
1227
1228 ret = kstrtol(buf, 0, &r);
1229 if (ret)
1230 return ret;
1231
1232 if (port->type == PORT_HSCIF) {
1233 if (r < 0 || r > 3)
1234 return -EINVAL;
1235 sci->hscif_tot = r << HSSCR_TOT_SHIFT;
1236 } else {
1237 sci->rx_fifo_timeout = r;
1238 scif_set_rtrg(port, 1);
1239 if (r > 0)
1240 timer_setup(&sci->rx_fifo_timer, rx_fifo_timer_fn, 0);
1241 }
1242
1243 return count;
1244 }
1245
1246 static DEVICE_ATTR_RW(rx_fifo_timeout);
1247
1248
1249 #ifdef CONFIG_SERIAL_SH_SCI_DMA
sci_dma_tx_complete(void * arg)1250 static void sci_dma_tx_complete(void *arg)
1251 {
1252 struct sci_port *s = arg;
1253 struct uart_port *port = &s->port;
1254 struct tty_port *tport = &port->state->port;
1255 unsigned long flags;
1256
1257 dev_dbg(port->dev, "%s(%d)\n", __func__, port->line);
1258
1259 uart_port_lock_irqsave(port, &flags);
1260
1261 uart_xmit_advance(port, s->tx_dma_len);
1262
1263 if (kfifo_len(&tport->xmit_fifo) < WAKEUP_CHARS)
1264 uart_write_wakeup(port);
1265
1266 s->tx_occurred = true;
1267
1268 if (!kfifo_is_empty(&tport->xmit_fifo)) {
1269 s->cookie_tx = 0;
1270 schedule_work(&s->work_tx);
1271 } else {
1272 s->cookie_tx = -EINVAL;
1273 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB ||
1274 s->cfg->regtype == SCIx_RZ_SCIFA_REGTYPE) {
1275 u16 ctrl = sci_serial_in(port, SCSCR);
1276 sci_serial_out(port, SCSCR, ctrl & ~SCSCR_TIE);
1277 if (s->cfg->regtype == SCIx_RZ_SCIFA_REGTYPE) {
1278 /* Switch irq from DMA to SCIF */
1279 dmaengine_pause(s->chan_tx_saved);
1280 enable_irq(s->irqs[SCIx_TXI_IRQ]);
1281 }
1282 }
1283 }
1284
1285 uart_port_unlock_irqrestore(port, flags);
1286 }
1287
1288 /* Locking: called with port lock held */
sci_dma_rx_push(struct sci_port * s,void * buf,size_t count)1289 static int sci_dma_rx_push(struct sci_port *s, void *buf, size_t count)
1290 {
1291 struct uart_port *port = &s->port;
1292 struct tty_port *tport = &port->state->port;
1293 int copied;
1294
1295 copied = tty_insert_flip_string(tport, buf, count);
1296 if (copied < count)
1297 port->icount.buf_overrun++;
1298
1299 port->icount.rx += copied;
1300
1301 return copied;
1302 }
1303
sci_dma_rx_find_active(struct sci_port * s)1304 static int sci_dma_rx_find_active(struct sci_port *s)
1305 {
1306 unsigned int i;
1307
1308 for (i = 0; i < ARRAY_SIZE(s->cookie_rx); i++)
1309 if (s->active_rx == s->cookie_rx[i])
1310 return i;
1311
1312 return -1;
1313 }
1314
1315 /* Must only be called with uart_port_lock taken */
sci_dma_rx_chan_invalidate(struct sci_port * s)1316 static void sci_dma_rx_chan_invalidate(struct sci_port *s)
1317 {
1318 unsigned int i;
1319
1320 s->chan_rx = NULL;
1321 for (i = 0; i < ARRAY_SIZE(s->cookie_rx); i++)
1322 s->cookie_rx[i] = -EINVAL;
1323 s->active_rx = 0;
1324 }
1325
sci_dma_rx_release(struct sci_port * s)1326 static void sci_dma_rx_release(struct sci_port *s)
1327 {
1328 struct dma_chan *chan = s->chan_rx_saved;
1329 struct uart_port *port = &s->port;
1330 unsigned long flags;
1331
1332 uart_port_lock_irqsave(port, &flags);
1333 s->chan_rx_saved = NULL;
1334 sci_dma_rx_chan_invalidate(s);
1335 uart_port_unlock_irqrestore(port, flags);
1336
1337 dmaengine_terminate_sync(chan);
1338 dma_free_coherent(chan->device->dev, s->buf_len_rx * 2, s->rx_buf[0],
1339 sg_dma_address(&s->sg_rx[0]));
1340 dma_release_channel(chan);
1341 }
1342
start_hrtimer_us(struct hrtimer * hrt,unsigned long usec)1343 static void start_hrtimer_us(struct hrtimer *hrt, unsigned long usec)
1344 {
1345 long sec = usec / 1000000;
1346 long nsec = (usec % 1000000) * 1000;
1347 ktime_t t = ktime_set(sec, nsec);
1348
1349 hrtimer_start(hrt, t, HRTIMER_MODE_REL);
1350 }
1351
sci_dma_rx_reenable_irq(struct sci_port * s)1352 static void sci_dma_rx_reenable_irq(struct sci_port *s)
1353 {
1354 struct uart_port *port = &s->port;
1355 u16 scr;
1356
1357 /* Direct new serial port interrupts back to CPU */
1358 scr = sci_serial_in(port, SCSCR);
1359 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB ||
1360 s->cfg->regtype == SCIx_RZ_SCIFA_REGTYPE) {
1361 enable_irq(s->irqs[SCIx_RXI_IRQ]);
1362 if (s->cfg->regtype == SCIx_RZ_SCIFA_REGTYPE)
1363 scif_set_rtrg(port, s->rx_trigger);
1364 else
1365 scr &= ~SCSCR_RDRQE;
1366 }
1367 sci_serial_out(port, SCSCR, scr | SCSCR_RIE);
1368 }
1369
sci_dma_rx_complete(void * arg)1370 static void sci_dma_rx_complete(void *arg)
1371 {
1372 struct sci_port *s = arg;
1373 struct dma_chan *chan = s->chan_rx;
1374 struct uart_port *port = &s->port;
1375 struct dma_async_tx_descriptor *desc;
1376 unsigned long flags;
1377 int active, count = 0;
1378
1379 dev_dbg(port->dev, "%s(%d) active cookie %d\n", __func__, port->line,
1380 s->active_rx);
1381
1382 hrtimer_cancel(&s->rx_timer);
1383
1384 uart_port_lock_irqsave(port, &flags);
1385
1386 active = sci_dma_rx_find_active(s);
1387 if (active >= 0)
1388 count = sci_dma_rx_push(s, s->rx_buf[active], s->buf_len_rx);
1389
1390 if (count)
1391 tty_flip_buffer_push(&port->state->port);
1392
1393 desc = dmaengine_prep_slave_sg(s->chan_rx, &s->sg_rx[active], 1,
1394 DMA_DEV_TO_MEM,
1395 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
1396 if (!desc)
1397 goto fail;
1398
1399 desc->callback = sci_dma_rx_complete;
1400 desc->callback_param = s;
1401 s->cookie_rx[active] = dmaengine_submit(desc);
1402 if (dma_submit_error(s->cookie_rx[active]))
1403 goto fail;
1404
1405 s->active_rx = s->cookie_rx[!active];
1406
1407 dma_async_issue_pending(chan);
1408
1409 uart_port_unlock_irqrestore(port, flags);
1410 dev_dbg(port->dev, "%s: cookie %d #%d, new active cookie %d\n",
1411 __func__, s->cookie_rx[active], active, s->active_rx);
1412
1413 start_hrtimer_us(&s->rx_timer, s->rx_timeout);
1414
1415 return;
1416
1417 fail:
1418 /* Switch to PIO */
1419 dmaengine_terminate_async(chan);
1420 sci_dma_rx_chan_invalidate(s);
1421 sci_dma_rx_reenable_irq(s);
1422 uart_port_unlock_irqrestore(port, flags);
1423 dev_warn(port->dev, "Failed submitting Rx DMA descriptor\n");
1424 }
1425
sci_dma_tx_release(struct sci_port * s)1426 static void sci_dma_tx_release(struct sci_port *s)
1427 {
1428 struct dma_chan *chan = s->chan_tx_saved;
1429
1430 cancel_work_sync(&s->work_tx);
1431 s->chan_tx_saved = s->chan_tx = NULL;
1432 s->cookie_tx = -EINVAL;
1433 dmaengine_terminate_sync(chan);
1434 dma_unmap_single(chan->device->dev, s->tx_dma_addr, UART_XMIT_SIZE,
1435 DMA_TO_DEVICE);
1436 dma_release_channel(chan);
1437 }
1438
sci_dma_rx_submit(struct sci_port * s,bool port_lock_held)1439 static int sci_dma_rx_submit(struct sci_port *s, bool port_lock_held)
1440 {
1441 struct dma_chan *chan = s->chan_rx;
1442 struct uart_port *port = &s->port;
1443 unsigned long flags;
1444 int i;
1445
1446 for (i = 0; i < 2; i++) {
1447 struct scatterlist *sg = &s->sg_rx[i];
1448 struct dma_async_tx_descriptor *desc;
1449
1450 desc = dmaengine_prep_slave_sg(chan,
1451 sg, 1, DMA_DEV_TO_MEM,
1452 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
1453 if (!desc)
1454 goto fail;
1455
1456 desc->callback = sci_dma_rx_complete;
1457 desc->callback_param = s;
1458 s->cookie_rx[i] = dmaengine_submit(desc);
1459 if (dma_submit_error(s->cookie_rx[i]))
1460 goto fail;
1461
1462 }
1463
1464 s->active_rx = s->cookie_rx[0];
1465
1466 dma_async_issue_pending(chan);
1467 return 0;
1468
1469 fail:
1470 /* Switch to PIO */
1471 if (!port_lock_held)
1472 uart_port_lock_irqsave(port, &flags);
1473 if (i)
1474 dmaengine_terminate_async(chan);
1475 sci_dma_rx_chan_invalidate(s);
1476 sci_start_rx(port);
1477 if (!port_lock_held)
1478 uart_port_unlock_irqrestore(port, flags);
1479 return -EAGAIN;
1480 }
1481
sci_dma_tx_work_fn(struct work_struct * work)1482 static void sci_dma_tx_work_fn(struct work_struct *work)
1483 {
1484 struct sci_port *s = container_of(work, struct sci_port, work_tx);
1485 struct dma_async_tx_descriptor *desc;
1486 struct dma_chan *chan = s->chan_tx;
1487 struct uart_port *port = &s->port;
1488 struct tty_port *tport = &port->state->port;
1489 unsigned long flags;
1490 unsigned int tail;
1491 dma_addr_t buf;
1492
1493 /*
1494 * DMA is idle now.
1495 * Port xmit buffer is already mapped, and it is one page... Just adjust
1496 * offsets and lengths. Since it is a circular buffer, we have to
1497 * transmit till the end, and then the rest. Take the port lock to get a
1498 * consistent xmit buffer state.
1499 */
1500 uart_port_lock_irq(port);
1501 s->tx_dma_len = kfifo_out_linear(&tport->xmit_fifo, &tail,
1502 UART_XMIT_SIZE);
1503 buf = s->tx_dma_addr + tail;
1504 if (!s->tx_dma_len) {
1505 /* Transmit buffer has been flushed */
1506 uart_port_unlock_irq(port);
1507 return;
1508 }
1509
1510 desc = dmaengine_prep_slave_single(chan, buf, s->tx_dma_len,
1511 DMA_MEM_TO_DEV,
1512 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
1513 if (!desc) {
1514 uart_port_unlock_irq(port);
1515 dev_warn(port->dev, "Failed preparing Tx DMA descriptor\n");
1516 goto switch_to_pio;
1517 }
1518
1519 dma_sync_single_for_device(chan->device->dev, buf, s->tx_dma_len,
1520 DMA_TO_DEVICE);
1521
1522 desc->callback = sci_dma_tx_complete;
1523 desc->callback_param = s;
1524 s->cookie_tx = dmaengine_submit(desc);
1525 if (dma_submit_error(s->cookie_tx)) {
1526 uart_port_unlock_irq(port);
1527 dev_warn(port->dev, "Failed submitting Tx DMA descriptor\n");
1528 goto switch_to_pio;
1529 }
1530
1531 uart_port_unlock_irq(port);
1532 dev_dbg(port->dev, "%s: %p: %u, cookie %d\n",
1533 __func__, tport->xmit_buf, tail, s->cookie_tx);
1534
1535 dma_async_issue_pending(chan);
1536 return;
1537
1538 switch_to_pio:
1539 uart_port_lock_irqsave(port, &flags);
1540 s->chan_tx = NULL;
1541 sci_start_tx(port);
1542 uart_port_unlock_irqrestore(port, flags);
1543 return;
1544 }
1545
sci_dma_rx_timer_fn(struct hrtimer * t)1546 static enum hrtimer_restart sci_dma_rx_timer_fn(struct hrtimer *t)
1547 {
1548 struct sci_port *s = container_of(t, struct sci_port, rx_timer);
1549 struct dma_chan *chan = s->chan_rx;
1550 struct uart_port *port = &s->port;
1551 struct dma_tx_state state;
1552 enum dma_status status;
1553 unsigned long flags;
1554 unsigned int read;
1555 int active, count;
1556
1557 dev_dbg(port->dev, "DMA Rx timed out\n");
1558
1559 uart_port_lock_irqsave(port, &flags);
1560
1561 active = sci_dma_rx_find_active(s);
1562 if (active < 0) {
1563 uart_port_unlock_irqrestore(port, flags);
1564 return HRTIMER_NORESTART;
1565 }
1566
1567 status = dmaengine_tx_status(s->chan_rx, s->active_rx, &state);
1568 if (status == DMA_COMPLETE) {
1569 uart_port_unlock_irqrestore(port, flags);
1570 dev_dbg(port->dev, "Cookie %d #%d has already completed\n",
1571 s->active_rx, active);
1572
1573 /* Let packet complete handler take care of the packet */
1574 return HRTIMER_NORESTART;
1575 }
1576
1577 dmaengine_pause(chan);
1578
1579 /*
1580 * sometimes DMA transfer doesn't stop even if it is stopped and
1581 * data keeps on coming until transaction is complete so check
1582 * for DMA_COMPLETE again
1583 * Let packet complete handler take care of the packet
1584 */
1585 status = dmaengine_tx_status(s->chan_rx, s->active_rx, &state);
1586 if (status == DMA_COMPLETE) {
1587 uart_port_unlock_irqrestore(port, flags);
1588 dev_dbg(port->dev, "Transaction complete after DMA engine was stopped");
1589 return HRTIMER_NORESTART;
1590 }
1591
1592 /* Handle incomplete DMA receive */
1593 dmaengine_terminate_async(s->chan_rx);
1594 read = sg_dma_len(&s->sg_rx[active]) - state.residue;
1595
1596 if (read) {
1597 count = sci_dma_rx_push(s, s->rx_buf[active], read);
1598 if (count)
1599 tty_flip_buffer_push(&port->state->port);
1600 }
1601
1602 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB ||
1603 s->cfg->regtype == SCIx_RZ_SCIFA_REGTYPE)
1604 sci_dma_rx_submit(s, true);
1605
1606 sci_dma_rx_reenable_irq(s);
1607
1608 uart_port_unlock_irqrestore(port, flags);
1609
1610 return HRTIMER_NORESTART;
1611 }
1612
sci_request_dma_chan(struct uart_port * port,enum dma_transfer_direction dir)1613 static struct dma_chan *sci_request_dma_chan(struct uart_port *port,
1614 enum dma_transfer_direction dir)
1615 {
1616 struct dma_chan *chan;
1617 struct dma_slave_config cfg;
1618 int ret;
1619
1620 chan = dma_request_chan(port->dev, dir == DMA_MEM_TO_DEV ? "tx" : "rx");
1621 if (IS_ERR(chan)) {
1622 dev_dbg(port->dev, "dma_request_chan failed\n");
1623 return NULL;
1624 }
1625
1626 memset(&cfg, 0, sizeof(cfg));
1627 cfg.direction = dir;
1628 cfg.dst_addr = port->mapbase +
1629 (sci_getreg(port, SCxTDR)->offset << port->regshift);
1630 cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
1631 cfg.src_addr = port->mapbase +
1632 (sci_getreg(port, SCxRDR)->offset << port->regshift);
1633 cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
1634
1635 ret = dmaengine_slave_config(chan, &cfg);
1636 if (ret) {
1637 dev_warn(port->dev, "dmaengine_slave_config failed %d\n", ret);
1638 dma_release_channel(chan);
1639 return NULL;
1640 }
1641
1642 return chan;
1643 }
1644
sci_request_dma(struct uart_port * port)1645 static void sci_request_dma(struct uart_port *port)
1646 {
1647 struct sci_port *s = to_sci_port(port);
1648 struct tty_port *tport = &port->state->port;
1649 struct dma_chan *chan;
1650
1651 dev_dbg(port->dev, "%s: port %d\n", __func__, port->line);
1652
1653 /*
1654 * DMA on console may interfere with Kernel log messages which use
1655 * plain putchar(). So, simply don't use it with a console.
1656 */
1657 if (uart_console(port))
1658 return;
1659
1660 if (!port->dev->of_node)
1661 return;
1662
1663 s->cookie_tx = -EINVAL;
1664
1665 /*
1666 * Don't request a dma channel if no channel was specified
1667 * in the device tree.
1668 */
1669 if (!of_property_present(port->dev->of_node, "dmas"))
1670 return;
1671
1672 chan = sci_request_dma_chan(port, DMA_MEM_TO_DEV);
1673 dev_dbg(port->dev, "%s: TX: got channel %p\n", __func__, chan);
1674 if (chan) {
1675 /* UART circular tx buffer is an aligned page. */
1676 s->tx_dma_addr = dma_map_single(chan->device->dev,
1677 tport->xmit_buf,
1678 UART_XMIT_SIZE,
1679 DMA_TO_DEVICE);
1680 if (dma_mapping_error(chan->device->dev, s->tx_dma_addr)) {
1681 dev_warn(port->dev, "Failed mapping Tx DMA descriptor\n");
1682 dma_release_channel(chan);
1683 } else {
1684 dev_dbg(port->dev, "%s: mapped %lu@%p to %pad\n",
1685 __func__, UART_XMIT_SIZE,
1686 tport->xmit_buf, &s->tx_dma_addr);
1687
1688 INIT_WORK(&s->work_tx, sci_dma_tx_work_fn);
1689 s->chan_tx_saved = s->chan_tx = chan;
1690 }
1691 }
1692
1693 chan = sci_request_dma_chan(port, DMA_DEV_TO_MEM);
1694 dev_dbg(port->dev, "%s: RX: got channel %p\n", __func__, chan);
1695 if (chan) {
1696 unsigned int i;
1697 dma_addr_t dma;
1698 void *buf;
1699
1700 s->buf_len_rx = 2 * max_t(size_t, 16, port->fifosize);
1701 buf = dma_alloc_coherent(chan->device->dev, s->buf_len_rx * 2,
1702 &dma, GFP_KERNEL);
1703 if (!buf) {
1704 dev_warn(port->dev,
1705 "Failed to allocate Rx dma buffer, using PIO\n");
1706 dma_release_channel(chan);
1707 return;
1708 }
1709
1710 for (i = 0; i < 2; i++) {
1711 struct scatterlist *sg = &s->sg_rx[i];
1712
1713 sg_init_table(sg, 1);
1714 s->rx_buf[i] = buf;
1715 sg_dma_address(sg) = dma;
1716 sg_dma_len(sg) = s->buf_len_rx;
1717
1718 buf += s->buf_len_rx;
1719 dma += s->buf_len_rx;
1720 }
1721
1722 hrtimer_init(&s->rx_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1723 s->rx_timer.function = sci_dma_rx_timer_fn;
1724
1725 s->chan_rx_saved = s->chan_rx = chan;
1726
1727 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB ||
1728 s->cfg->regtype == SCIx_RZ_SCIFA_REGTYPE)
1729 sci_dma_rx_submit(s, false);
1730 }
1731 }
1732
sci_free_dma(struct uart_port * port)1733 static void sci_free_dma(struct uart_port *port)
1734 {
1735 struct sci_port *s = to_sci_port(port);
1736
1737 if (s->chan_tx_saved)
1738 sci_dma_tx_release(s);
1739 if (s->chan_rx_saved)
1740 sci_dma_rx_release(s);
1741 }
1742
sci_flush_buffer(struct uart_port * port)1743 static void sci_flush_buffer(struct uart_port *port)
1744 {
1745 struct sci_port *s = to_sci_port(port);
1746
1747 /*
1748 * In uart_flush_buffer(), the xmit circular buffer has just been
1749 * cleared, so we have to reset tx_dma_len accordingly, and stop any
1750 * pending transfers
1751 */
1752 s->tx_dma_len = 0;
1753 if (s->chan_tx) {
1754 dmaengine_terminate_async(s->chan_tx);
1755 s->cookie_tx = -EINVAL;
1756 }
1757 }
1758
sci_dma_check_tx_occurred(struct sci_port * s)1759 static void sci_dma_check_tx_occurred(struct sci_port *s)
1760 {
1761 struct dma_tx_state state;
1762 enum dma_status status;
1763
1764 if (!s->chan_tx)
1765 return;
1766
1767 status = dmaengine_tx_status(s->chan_tx, s->cookie_tx, &state);
1768 if (status == DMA_COMPLETE || status == DMA_IN_PROGRESS)
1769 s->tx_occurred = true;
1770 }
1771 #else /* !CONFIG_SERIAL_SH_SCI_DMA */
sci_request_dma(struct uart_port * port)1772 static inline void sci_request_dma(struct uart_port *port)
1773 {
1774 }
1775
sci_free_dma(struct uart_port * port)1776 static inline void sci_free_dma(struct uart_port *port)
1777 {
1778 }
1779
sci_dma_check_tx_occurred(struct sci_port * s)1780 static void sci_dma_check_tx_occurred(struct sci_port *s)
1781 {
1782 }
1783
1784 #define sci_flush_buffer NULL
1785 #endif /* !CONFIG_SERIAL_SH_SCI_DMA */
1786
sci_rx_interrupt(int irq,void * ptr)1787 static irqreturn_t sci_rx_interrupt(int irq, void *ptr)
1788 {
1789 struct uart_port *port = ptr;
1790 struct sci_port *s = to_sci_port(port);
1791
1792 #ifdef CONFIG_SERIAL_SH_SCI_DMA
1793 if (s->chan_rx) {
1794 u16 scr = sci_serial_in(port, SCSCR);
1795 u16 ssr = sci_serial_in(port, SCxSR);
1796
1797 /* Disable future Rx interrupts */
1798 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB ||
1799 s->cfg->regtype == SCIx_RZ_SCIFA_REGTYPE) {
1800 disable_irq_nosync(s->irqs[SCIx_RXI_IRQ]);
1801 if (s->cfg->regtype == SCIx_RZ_SCIFA_REGTYPE) {
1802 scif_set_rtrg(port, 1);
1803 scr |= SCSCR_RIE;
1804 } else {
1805 scr |= SCSCR_RDRQE;
1806 }
1807 } else {
1808 if (sci_dma_rx_submit(s, false) < 0)
1809 goto handle_pio;
1810
1811 scr &= ~SCSCR_RIE;
1812 }
1813 sci_serial_out(port, SCSCR, scr);
1814 /* Clear current interrupt */
1815 sci_serial_out(port, SCxSR,
1816 ssr & ~(SCIF_DR | SCxSR_RDxF(port)));
1817 dev_dbg(port->dev, "Rx IRQ %lu: setup t-out in %u us\n",
1818 jiffies, s->rx_timeout);
1819 start_hrtimer_us(&s->rx_timer, s->rx_timeout);
1820
1821 return IRQ_HANDLED;
1822 }
1823
1824 handle_pio:
1825 #endif
1826
1827 if (s->rx_trigger > 1 && s->rx_fifo_timeout > 0) {
1828 if (!scif_rtrg_enabled(port))
1829 scif_set_rtrg(port, s->rx_trigger);
1830
1831 mod_timer(&s->rx_fifo_timer, jiffies + DIV_ROUND_UP(
1832 s->rx_frame * HZ * s->rx_fifo_timeout, 1000000));
1833 }
1834
1835 /* I think sci_receive_chars has to be called irrespective
1836 * of whether the I_IXOFF is set, otherwise, how is the interrupt
1837 * to be disabled?
1838 */
1839 sci_receive_chars(port);
1840
1841 return IRQ_HANDLED;
1842 }
1843
sci_tx_interrupt(int irq,void * ptr)1844 static irqreturn_t sci_tx_interrupt(int irq, void *ptr)
1845 {
1846 struct uart_port *port = ptr;
1847 unsigned long flags;
1848
1849 uart_port_lock_irqsave(port, &flags);
1850 sci_transmit_chars(port);
1851 uart_port_unlock_irqrestore(port, flags);
1852
1853 return IRQ_HANDLED;
1854 }
1855
sci_tx_end_interrupt(int irq,void * ptr)1856 static irqreturn_t sci_tx_end_interrupt(int irq, void *ptr)
1857 {
1858 struct uart_port *port = ptr;
1859 unsigned long flags;
1860 unsigned short ctrl;
1861
1862 if (port->type != PORT_SCI)
1863 return sci_tx_interrupt(irq, ptr);
1864
1865 uart_port_lock_irqsave(port, &flags);
1866 ctrl = sci_serial_in(port, SCSCR);
1867 ctrl &= ~(SCSCR_TE | SCSCR_TEIE);
1868 sci_serial_out(port, SCSCR, ctrl);
1869 uart_port_unlock_irqrestore(port, flags);
1870
1871 return IRQ_HANDLED;
1872 }
1873
sci_br_interrupt(int irq,void * ptr)1874 static irqreturn_t sci_br_interrupt(int irq, void *ptr)
1875 {
1876 struct uart_port *port = ptr;
1877
1878 /* Handle BREAKs */
1879 sci_handle_breaks(port);
1880
1881 /* drop invalid character received before break was detected */
1882 sci_serial_in(port, SCxRDR);
1883
1884 sci_clear_SCxSR(port, SCxSR_BREAK_CLEAR(port));
1885
1886 return IRQ_HANDLED;
1887 }
1888
sci_er_interrupt(int irq,void * ptr)1889 static irqreturn_t sci_er_interrupt(int irq, void *ptr)
1890 {
1891 struct uart_port *port = ptr;
1892 struct sci_port *s = to_sci_port(port);
1893
1894 if (s->irqs[SCIx_ERI_IRQ] == s->irqs[SCIx_BRI_IRQ]) {
1895 /* Break and Error interrupts are muxed */
1896 unsigned short ssr_status = sci_serial_in(port, SCxSR);
1897
1898 /* Break Interrupt */
1899 if (ssr_status & SCxSR_BRK(port))
1900 sci_br_interrupt(irq, ptr);
1901
1902 /* Break only? */
1903 if (!(ssr_status & SCxSR_ERRORS(port)))
1904 return IRQ_HANDLED;
1905 }
1906
1907 /* Handle errors */
1908 if (port->type == PORT_SCI) {
1909 if (sci_handle_errors(port)) {
1910 /* discard character in rx buffer */
1911 sci_serial_in(port, SCxSR);
1912 sci_clear_SCxSR(port, SCxSR_RDxF_CLEAR(port));
1913 }
1914 } else {
1915 sci_handle_fifo_overrun(port);
1916 if (!s->chan_rx)
1917 sci_receive_chars(port);
1918 }
1919
1920 sci_clear_SCxSR(port, SCxSR_ERROR_CLEAR(port));
1921
1922 /* Kick the transmission */
1923 if (!s->chan_tx)
1924 sci_tx_interrupt(irq, ptr);
1925
1926 return IRQ_HANDLED;
1927 }
1928
sci_mpxed_interrupt(int irq,void * ptr)1929 static irqreturn_t sci_mpxed_interrupt(int irq, void *ptr)
1930 {
1931 unsigned short ssr_status, scr_status, err_enabled, orer_status = 0;
1932 struct uart_port *port = ptr;
1933 struct sci_port *s = to_sci_port(port);
1934 irqreturn_t ret = IRQ_NONE;
1935
1936 ssr_status = sci_serial_in(port, SCxSR);
1937 scr_status = sci_serial_in(port, SCSCR);
1938 if (s->params->overrun_reg == SCxSR)
1939 orer_status = ssr_status;
1940 else if (sci_getreg(port, s->params->overrun_reg)->size)
1941 orer_status = sci_serial_in(port, s->params->overrun_reg);
1942
1943 err_enabled = scr_status & port_rx_irq_mask(port);
1944
1945 /* Tx Interrupt */
1946 if ((ssr_status & SCxSR_TDxE(port)) && (scr_status & SCSCR_TIE) &&
1947 !s->chan_tx)
1948 ret = sci_tx_interrupt(irq, ptr);
1949
1950 /*
1951 * Rx Interrupt: if we're using DMA, the DMA controller clears RDF /
1952 * DR flags
1953 */
1954 if (((ssr_status & SCxSR_RDxF(port)) || s->chan_rx) &&
1955 (scr_status & SCSCR_RIE))
1956 ret = sci_rx_interrupt(irq, ptr);
1957
1958 /* Error Interrupt */
1959 if ((ssr_status & SCxSR_ERRORS(port)) && err_enabled)
1960 ret = sci_er_interrupt(irq, ptr);
1961
1962 /* Break Interrupt */
1963 if (s->irqs[SCIx_ERI_IRQ] != s->irqs[SCIx_BRI_IRQ] &&
1964 (ssr_status & SCxSR_BRK(port)) && err_enabled)
1965 ret = sci_br_interrupt(irq, ptr);
1966
1967 /* Overrun Interrupt */
1968 if (orer_status & s->params->overrun_mask) {
1969 sci_handle_fifo_overrun(port);
1970 ret = IRQ_HANDLED;
1971 }
1972
1973 return ret;
1974 }
1975
1976 static const struct sci_irq_desc {
1977 const char *desc;
1978 irq_handler_t handler;
1979 } sci_irq_desc[] = {
1980 /*
1981 * Split out handlers, the default case.
1982 */
1983 [SCIx_ERI_IRQ] = {
1984 .desc = "rx err",
1985 .handler = sci_er_interrupt,
1986 },
1987
1988 [SCIx_RXI_IRQ] = {
1989 .desc = "rx full",
1990 .handler = sci_rx_interrupt,
1991 },
1992
1993 [SCIx_TXI_IRQ] = {
1994 .desc = "tx empty",
1995 .handler = sci_tx_interrupt,
1996 },
1997
1998 [SCIx_BRI_IRQ] = {
1999 .desc = "break",
2000 .handler = sci_br_interrupt,
2001 },
2002
2003 [SCIx_DRI_IRQ] = {
2004 .desc = "rx ready",
2005 .handler = sci_rx_interrupt,
2006 },
2007
2008 [SCIx_TEI_IRQ] = {
2009 .desc = "tx end",
2010 .handler = sci_tx_end_interrupt,
2011 },
2012
2013 /*
2014 * Special muxed handler.
2015 */
2016 [SCIx_MUX_IRQ] = {
2017 .desc = "mux",
2018 .handler = sci_mpxed_interrupt,
2019 },
2020 };
2021
sci_request_irq(struct sci_port * port)2022 static int sci_request_irq(struct sci_port *port)
2023 {
2024 struct uart_port *up = &port->port;
2025 int i, j, w, ret = 0;
2026
2027 for (i = j = 0; i < SCIx_NR_IRQS; i++, j++) {
2028 const struct sci_irq_desc *desc;
2029 int irq;
2030
2031 /* Check if already registered (muxed) */
2032 for (w = 0; w < i; w++)
2033 if (port->irqs[w] == port->irqs[i])
2034 w = i + 1;
2035 if (w > i)
2036 continue;
2037
2038 if (SCIx_IRQ_IS_MUXED(port)) {
2039 i = SCIx_MUX_IRQ;
2040 irq = up->irq;
2041 } else {
2042 irq = port->irqs[i];
2043
2044 /*
2045 * Certain port types won't support all of the
2046 * available interrupt sources.
2047 */
2048 if (unlikely(irq < 0))
2049 continue;
2050 }
2051
2052 desc = sci_irq_desc + i;
2053 port->irqstr[j] = kasprintf(GFP_KERNEL, "%s:%s",
2054 dev_name(up->dev), desc->desc);
2055 if (!port->irqstr[j]) {
2056 ret = -ENOMEM;
2057 goto out_nomem;
2058 }
2059
2060 ret = request_irq(irq, desc->handler, up->irqflags,
2061 port->irqstr[j], port);
2062 if (unlikely(ret)) {
2063 dev_err(up->dev, "Can't allocate %s IRQ\n", desc->desc);
2064 goto out_noirq;
2065 }
2066 }
2067
2068 return 0;
2069
2070 out_noirq:
2071 while (--i >= 0)
2072 free_irq(port->irqs[i], port);
2073
2074 out_nomem:
2075 while (--j >= 0)
2076 kfree(port->irqstr[j]);
2077
2078 return ret;
2079 }
2080
sci_free_irq(struct sci_port * port)2081 static void sci_free_irq(struct sci_port *port)
2082 {
2083 int i, j;
2084
2085 /*
2086 * Intentionally in reverse order so we iterate over the muxed
2087 * IRQ first.
2088 */
2089 for (i = 0; i < SCIx_NR_IRQS; i++) {
2090 int irq = port->irqs[i];
2091
2092 /*
2093 * Certain port types won't support all of the available
2094 * interrupt sources.
2095 */
2096 if (unlikely(irq < 0))
2097 continue;
2098
2099 /* Check if already freed (irq was muxed) */
2100 for (j = 0; j < i; j++)
2101 if (port->irqs[j] == irq)
2102 j = i + 1;
2103 if (j > i)
2104 continue;
2105
2106 free_irq(port->irqs[i], port);
2107 kfree(port->irqstr[i]);
2108
2109 if (SCIx_IRQ_IS_MUXED(port)) {
2110 /* If there's only one IRQ, we're done. */
2111 return;
2112 }
2113 }
2114 }
2115
sci_tx_empty(struct uart_port * port)2116 static unsigned int sci_tx_empty(struct uart_port *port)
2117 {
2118 unsigned short status = sci_serial_in(port, SCxSR);
2119 unsigned short in_tx_fifo = sci_txfill(port);
2120 struct sci_port *s = to_sci_port(port);
2121
2122 sci_dma_check_tx_occurred(s);
2123
2124 if (!s->tx_occurred)
2125 return TIOCSER_TEMT;
2126
2127 return (status & SCxSR_TEND(port)) && !in_tx_fifo ? TIOCSER_TEMT : 0;
2128 }
2129
sci_set_rts(struct uart_port * port,bool state)2130 static void sci_set_rts(struct uart_port *port, bool state)
2131 {
2132 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB) {
2133 u16 data = sci_serial_in(port, SCPDR);
2134
2135 /* Active low */
2136 if (state)
2137 data &= ~SCPDR_RTSD;
2138 else
2139 data |= SCPDR_RTSD;
2140 sci_serial_out(port, SCPDR, data);
2141
2142 /* RTS# is output */
2143 sci_serial_out(port, SCPCR,
2144 sci_serial_in(port, SCPCR) | SCPCR_RTSC);
2145 } else if (sci_getreg(port, SCSPTR)->size) {
2146 u16 ctrl = sci_serial_in(port, SCSPTR);
2147
2148 /* Active low */
2149 if (state)
2150 ctrl &= ~SCSPTR_RTSDT;
2151 else
2152 ctrl |= SCSPTR_RTSDT;
2153 sci_serial_out(port, SCSPTR, ctrl);
2154 }
2155 }
2156
sci_get_cts(struct uart_port * port)2157 static bool sci_get_cts(struct uart_port *port)
2158 {
2159 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB) {
2160 /* Active low */
2161 return !(sci_serial_in(port, SCPDR) & SCPDR_CTSD);
2162 } else if (sci_getreg(port, SCSPTR)->size) {
2163 /* Active low */
2164 return !(sci_serial_in(port, SCSPTR) & SCSPTR_CTSDT);
2165 }
2166
2167 return true;
2168 }
2169
2170 /*
2171 * Modem control is a bit of a mixed bag for SCI(F) ports. Generally
2172 * CTS/RTS is supported in hardware by at least one port and controlled
2173 * via SCSPTR (SCxPCR for SCIFA/B parts), or external pins (presently
2174 * handled via the ->init_pins() op, which is a bit of a one-way street,
2175 * lacking any ability to defer pin control -- this will later be
2176 * converted over to the GPIO framework).
2177 *
2178 * Other modes (such as loopback) are supported generically on certain
2179 * port types, but not others. For these it's sufficient to test for the
2180 * existence of the support register and simply ignore the port type.
2181 */
sci_set_mctrl(struct uart_port * port,unsigned int mctrl)2182 static void sci_set_mctrl(struct uart_port *port, unsigned int mctrl)
2183 {
2184 struct sci_port *s = to_sci_port(port);
2185
2186 if (mctrl & TIOCM_LOOP) {
2187 const struct plat_sci_reg *reg;
2188
2189 /*
2190 * Standard loopback mode for SCFCR ports.
2191 */
2192 reg = sci_getreg(port, SCFCR);
2193 if (reg->size)
2194 sci_serial_out(port, SCFCR,
2195 sci_serial_in(port, SCFCR) | SCFCR_LOOP);
2196 }
2197
2198 mctrl_gpio_set(s->gpios, mctrl);
2199
2200 if (!s->has_rtscts)
2201 return;
2202
2203 if (!(mctrl & TIOCM_RTS)) {
2204 /* Disable Auto RTS */
2205 if (s->cfg->regtype != SCIx_RZV2H_SCIF_REGTYPE)
2206 sci_serial_out(port, SCFCR,
2207 sci_serial_in(port, SCFCR) & ~SCFCR_MCE);
2208
2209 /* Clear RTS */
2210 sci_set_rts(port, 0);
2211 } else if (s->autorts) {
2212 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB) {
2213 /* Enable RTS# pin function */
2214 sci_serial_out(port, SCPCR,
2215 sci_serial_in(port, SCPCR) & ~SCPCR_RTSC);
2216 }
2217
2218 /* Enable Auto RTS */
2219 if (s->cfg->regtype != SCIx_RZV2H_SCIF_REGTYPE)
2220 sci_serial_out(port, SCFCR,
2221 sci_serial_in(port, SCFCR) | SCFCR_MCE);
2222 } else {
2223 /* Set RTS */
2224 sci_set_rts(port, 1);
2225 }
2226 }
2227
sci_get_mctrl(struct uart_port * port)2228 static unsigned int sci_get_mctrl(struct uart_port *port)
2229 {
2230 struct sci_port *s = to_sci_port(port);
2231 struct mctrl_gpios *gpios = s->gpios;
2232 unsigned int mctrl = 0;
2233
2234 mctrl_gpio_get(gpios, &mctrl);
2235
2236 /*
2237 * CTS/RTS is handled in hardware when supported, while nothing
2238 * else is wired up.
2239 */
2240 if (s->autorts) {
2241 if (sci_get_cts(port))
2242 mctrl |= TIOCM_CTS;
2243 } else if (!mctrl_gpio_to_gpiod(gpios, UART_GPIO_CTS)) {
2244 mctrl |= TIOCM_CTS;
2245 }
2246 if (!mctrl_gpio_to_gpiod(gpios, UART_GPIO_DSR))
2247 mctrl |= TIOCM_DSR;
2248 if (!mctrl_gpio_to_gpiod(gpios, UART_GPIO_DCD))
2249 mctrl |= TIOCM_CAR;
2250
2251 return mctrl;
2252 }
2253
sci_enable_ms(struct uart_port * port)2254 static void sci_enable_ms(struct uart_port *port)
2255 {
2256 mctrl_gpio_enable_ms(to_sci_port(port)->gpios);
2257 }
2258
sci_break_ctl(struct uart_port * port,int break_state)2259 static void sci_break_ctl(struct uart_port *port, int break_state)
2260 {
2261 unsigned short scscr, scsptr;
2262 unsigned long flags;
2263
2264 /* check whether the port has SCSPTR */
2265 if (!sci_getreg(port, SCSPTR)->size) {
2266 /*
2267 * Not supported by hardware. Most parts couple break and rx
2268 * interrupts together, with break detection always enabled.
2269 */
2270 return;
2271 }
2272
2273 uart_port_lock_irqsave(port, &flags);
2274 scsptr = sci_serial_in(port, SCSPTR);
2275 scscr = sci_serial_in(port, SCSCR);
2276
2277 if (break_state == -1) {
2278 scsptr = (scsptr | SCSPTR_SPB2IO) & ~SCSPTR_SPB2DT;
2279 scscr &= ~SCSCR_TE;
2280 } else {
2281 scsptr = (scsptr | SCSPTR_SPB2DT) & ~SCSPTR_SPB2IO;
2282 scscr |= SCSCR_TE;
2283 }
2284
2285 sci_serial_out(port, SCSPTR, scsptr);
2286 sci_serial_out(port, SCSCR, scscr);
2287 uart_port_unlock_irqrestore(port, flags);
2288 }
2289
sci_startup(struct uart_port * port)2290 static int sci_startup(struct uart_port *port)
2291 {
2292 struct sci_port *s = to_sci_port(port);
2293 int ret;
2294
2295 dev_dbg(port->dev, "%s(%d)\n", __func__, port->line);
2296
2297 s->tx_occurred = false;
2298 sci_request_dma(port);
2299
2300 ret = sci_request_irq(s);
2301 if (unlikely(ret < 0)) {
2302 sci_free_dma(port);
2303 return ret;
2304 }
2305
2306 return 0;
2307 }
2308
sci_shutdown(struct uart_port * port)2309 static void sci_shutdown(struct uart_port *port)
2310 {
2311 struct sci_port *s = to_sci_port(port);
2312 unsigned long flags;
2313 u16 scr;
2314
2315 dev_dbg(port->dev, "%s(%d)\n", __func__, port->line);
2316
2317 s->autorts = false;
2318 mctrl_gpio_disable_ms(to_sci_port(port)->gpios);
2319
2320 uart_port_lock_irqsave(port, &flags);
2321 sci_stop_rx(port);
2322 sci_stop_tx(port);
2323 /*
2324 * Stop RX and TX, disable related interrupts, keep clock source
2325 * and HSCIF TOT bits
2326 */
2327 scr = sci_serial_in(port, SCSCR);
2328 sci_serial_out(port, SCSCR,
2329 scr & (SCSCR_CKE1 | SCSCR_CKE0 | s->hscif_tot));
2330 uart_port_unlock_irqrestore(port, flags);
2331
2332 #ifdef CONFIG_SERIAL_SH_SCI_DMA
2333 if (s->chan_rx_saved) {
2334 dev_dbg(port->dev, "%s(%d) deleting rx_timer\n", __func__,
2335 port->line);
2336 hrtimer_cancel(&s->rx_timer);
2337 }
2338 #endif
2339
2340 if (s->rx_trigger > 1 && s->rx_fifo_timeout > 0)
2341 del_timer_sync(&s->rx_fifo_timer);
2342 sci_free_irq(s);
2343 sci_free_dma(port);
2344 }
2345
sci_sck_calc(struct sci_port * s,unsigned int bps,unsigned int * srr)2346 static int sci_sck_calc(struct sci_port *s, unsigned int bps,
2347 unsigned int *srr)
2348 {
2349 unsigned long freq = s->clk_rates[SCI_SCK];
2350 int err, min_err = INT_MAX;
2351 unsigned int sr;
2352
2353 if (s->port.type != PORT_HSCIF)
2354 freq *= 2;
2355
2356 for_each_sr(sr, s) {
2357 err = DIV_ROUND_CLOSEST(freq, sr) - bps;
2358 if (abs(err) >= abs(min_err))
2359 continue;
2360
2361 min_err = err;
2362 *srr = sr - 1;
2363
2364 if (!err)
2365 break;
2366 }
2367
2368 dev_dbg(s->port.dev, "SCK: %u%+d bps using SR %u\n", bps, min_err,
2369 *srr + 1);
2370 return min_err;
2371 }
2372
sci_brg_calc(struct sci_port * s,unsigned int bps,unsigned long freq,unsigned int * dlr,unsigned int * srr)2373 static int sci_brg_calc(struct sci_port *s, unsigned int bps,
2374 unsigned long freq, unsigned int *dlr,
2375 unsigned int *srr)
2376 {
2377 int err, min_err = INT_MAX;
2378 unsigned int sr, dl;
2379
2380 if (s->port.type != PORT_HSCIF)
2381 freq *= 2;
2382
2383 for_each_sr(sr, s) {
2384 dl = DIV_ROUND_CLOSEST(freq, sr * bps);
2385 dl = clamp(dl, 1U, 65535U);
2386
2387 err = DIV_ROUND_CLOSEST(freq, sr * dl) - bps;
2388 if (abs(err) >= abs(min_err))
2389 continue;
2390
2391 min_err = err;
2392 *dlr = dl;
2393 *srr = sr - 1;
2394
2395 if (!err)
2396 break;
2397 }
2398
2399 dev_dbg(s->port.dev, "BRG: %u%+d bps using DL %u SR %u\n", bps,
2400 min_err, *dlr, *srr + 1);
2401 return min_err;
2402 }
2403
2404 /* calculate sample rate, BRR, and clock select */
sci_scbrr_calc(struct sci_port * s,unsigned int bps,unsigned int * brr,unsigned int * srr,unsigned int * cks)2405 static int sci_scbrr_calc(struct sci_port *s, unsigned int bps,
2406 unsigned int *brr, unsigned int *srr,
2407 unsigned int *cks)
2408 {
2409 unsigned long freq = s->clk_rates[SCI_FCK];
2410 unsigned int sr, br, prediv, scrate, c;
2411 int err, min_err = INT_MAX;
2412
2413 if (s->port.type != PORT_HSCIF)
2414 freq *= 2;
2415
2416 /*
2417 * Find the combination of sample rate and clock select with the
2418 * smallest deviation from the desired baud rate.
2419 * Prefer high sample rates to maximise the receive margin.
2420 *
2421 * M: Receive margin (%)
2422 * N: Ratio of bit rate to clock (N = sampling rate)
2423 * D: Clock duty (D = 0 to 1.0)
2424 * L: Frame length (L = 9 to 12)
2425 * F: Absolute value of clock frequency deviation
2426 *
2427 * M = |(0.5 - 1 / 2 * N) - ((L - 0.5) * F) -
2428 * (|D - 0.5| / N * (1 + F))|
2429 * NOTE: Usually, treat D for 0.5, F is 0 by this calculation.
2430 */
2431 for_each_sr(sr, s) {
2432 for (c = 0; c <= 3; c++) {
2433 /* integerized formulas from HSCIF documentation */
2434 prediv = sr << (2 * c + 1);
2435
2436 /*
2437 * We need to calculate:
2438 *
2439 * br = freq / (prediv * bps) clamped to [1..256]
2440 * err = freq / (br * prediv) - bps
2441 *
2442 * Watch out for overflow when calculating the desired
2443 * sampling clock rate!
2444 */
2445 if (bps > UINT_MAX / prediv)
2446 break;
2447
2448 scrate = prediv * bps;
2449 br = DIV_ROUND_CLOSEST(freq, scrate);
2450 br = clamp(br, 1U, 256U);
2451
2452 err = DIV_ROUND_CLOSEST(freq, br * prediv) - bps;
2453 if (abs(err) >= abs(min_err))
2454 continue;
2455
2456 min_err = err;
2457 *brr = br - 1;
2458 *srr = sr - 1;
2459 *cks = c;
2460
2461 if (!err)
2462 goto found;
2463 }
2464 }
2465
2466 found:
2467 dev_dbg(s->port.dev, "BRR: %u%+d bps using N %u SR %u cks %u\n", bps,
2468 min_err, *brr, *srr + 1, *cks);
2469 return min_err;
2470 }
2471
sci_reset(struct uart_port * port)2472 static void sci_reset(struct uart_port *port)
2473 {
2474 const struct plat_sci_reg *reg;
2475 unsigned int status;
2476 struct sci_port *s = to_sci_port(port);
2477
2478 sci_serial_out(port, SCSCR, s->hscif_tot); /* TE=0, RE=0, CKE1=0 */
2479
2480 reg = sci_getreg(port, SCFCR);
2481 if (reg->size)
2482 sci_serial_out(port, SCFCR, SCFCR_RFRST | SCFCR_TFRST);
2483
2484 sci_clear_SCxSR(port,
2485 SCxSR_RDxF_CLEAR(port) & SCxSR_ERROR_CLEAR(port) &
2486 SCxSR_BREAK_CLEAR(port));
2487 if (sci_getreg(port, SCLSR)->size) {
2488 status = sci_serial_in(port, SCLSR);
2489 status &= ~(SCLSR_TO | SCLSR_ORER);
2490 sci_serial_out(port, SCLSR, status);
2491 }
2492
2493 if (s->rx_trigger > 1) {
2494 if (s->rx_fifo_timeout) {
2495 scif_set_rtrg(port, 1);
2496 timer_setup(&s->rx_fifo_timer, rx_fifo_timer_fn, 0);
2497 } else {
2498 if (port->type == PORT_SCIFA ||
2499 port->type == PORT_SCIFB)
2500 scif_set_rtrg(port, 1);
2501 else
2502 scif_set_rtrg(port, s->rx_trigger);
2503 }
2504 }
2505 }
2506
sci_set_termios(struct uart_port * port,struct ktermios * termios,const struct ktermios * old)2507 static void sci_set_termios(struct uart_port *port, struct ktermios *termios,
2508 const struct ktermios *old)
2509 {
2510 unsigned int baud, smr_val = SCSMR_ASYNC, scr_val = 0, i, bits;
2511 unsigned int brr = 255, cks = 0, srr = 15, dl = 0, sccks = 0;
2512 unsigned int brr1 = 255, cks1 = 0, srr1 = 15, dl1 = 0;
2513 struct sci_port *s = to_sci_port(port);
2514 const struct plat_sci_reg *reg;
2515 int min_err = INT_MAX, err;
2516 unsigned long max_freq = 0;
2517 int best_clk = -1;
2518 unsigned long flags;
2519
2520 if ((termios->c_cflag & CSIZE) == CS7) {
2521 smr_val |= SCSMR_CHR;
2522 } else {
2523 termios->c_cflag &= ~CSIZE;
2524 termios->c_cflag |= CS8;
2525 }
2526 if (termios->c_cflag & PARENB)
2527 smr_val |= SCSMR_PE;
2528 if (termios->c_cflag & PARODD)
2529 smr_val |= SCSMR_PE | SCSMR_ODD;
2530 if (termios->c_cflag & CSTOPB)
2531 smr_val |= SCSMR_STOP;
2532
2533 /*
2534 * earlyprintk comes here early on with port->uartclk set to zero.
2535 * the clock framework is not up and running at this point so here
2536 * we assume that 115200 is the maximum baud rate. please note that
2537 * the baud rate is not programmed during earlyprintk - it is assumed
2538 * that the previous boot loader has enabled required clocks and
2539 * setup the baud rate generator hardware for us already.
2540 */
2541 if (!port->uartclk) {
2542 baud = uart_get_baud_rate(port, termios, old, 0, 115200);
2543 goto done;
2544 }
2545
2546 for (i = 0; i < SCI_NUM_CLKS; i++)
2547 max_freq = max(max_freq, s->clk_rates[i]);
2548
2549 baud = uart_get_baud_rate(port, termios, old, 0, max_freq / min_sr(s));
2550 if (!baud)
2551 goto done;
2552
2553 /*
2554 * There can be multiple sources for the sampling clock. Find the one
2555 * that gives us the smallest deviation from the desired baud rate.
2556 */
2557
2558 /* Optional Undivided External Clock */
2559 if (s->clk_rates[SCI_SCK] && port->type != PORT_SCIFA &&
2560 port->type != PORT_SCIFB) {
2561 err = sci_sck_calc(s, baud, &srr1);
2562 if (abs(err) < abs(min_err)) {
2563 best_clk = SCI_SCK;
2564 scr_val = SCSCR_CKE1;
2565 sccks = SCCKS_CKS;
2566 min_err = err;
2567 srr = srr1;
2568 if (!err)
2569 goto done;
2570 }
2571 }
2572
2573 /* Optional BRG Frequency Divided External Clock */
2574 if (s->clk_rates[SCI_SCIF_CLK] && sci_getreg(port, SCDL)->size) {
2575 err = sci_brg_calc(s, baud, s->clk_rates[SCI_SCIF_CLK], &dl1,
2576 &srr1);
2577 if (abs(err) < abs(min_err)) {
2578 best_clk = SCI_SCIF_CLK;
2579 scr_val = SCSCR_CKE1;
2580 sccks = 0;
2581 min_err = err;
2582 dl = dl1;
2583 srr = srr1;
2584 if (!err)
2585 goto done;
2586 }
2587 }
2588
2589 /* Optional BRG Frequency Divided Internal Clock */
2590 if (s->clk_rates[SCI_BRG_INT] && sci_getreg(port, SCDL)->size) {
2591 err = sci_brg_calc(s, baud, s->clk_rates[SCI_BRG_INT], &dl1,
2592 &srr1);
2593 if (abs(err) < abs(min_err)) {
2594 best_clk = SCI_BRG_INT;
2595 scr_val = SCSCR_CKE1;
2596 sccks = SCCKS_XIN;
2597 min_err = err;
2598 dl = dl1;
2599 srr = srr1;
2600 if (!min_err)
2601 goto done;
2602 }
2603 }
2604
2605 /* Divided Functional Clock using standard Bit Rate Register */
2606 err = sci_scbrr_calc(s, baud, &brr1, &srr1, &cks1);
2607 if (abs(err) < abs(min_err)) {
2608 best_clk = SCI_FCK;
2609 scr_val = 0;
2610 min_err = err;
2611 brr = brr1;
2612 srr = srr1;
2613 cks = cks1;
2614 }
2615
2616 done:
2617 if (best_clk >= 0)
2618 dev_dbg(port->dev, "Using clk %pC for %u%+d bps\n",
2619 s->clks[best_clk], baud, min_err);
2620
2621 sci_port_enable(s);
2622
2623 /*
2624 * Program the optional External Baud Rate Generator (BRG) first.
2625 * It controls the mux to select (H)SCK or frequency divided clock.
2626 */
2627 if (best_clk >= 0 && sci_getreg(port, SCCKS)->size) {
2628 sci_serial_out(port, SCDL, dl);
2629 sci_serial_out(port, SCCKS, sccks);
2630 }
2631
2632 uart_port_lock_irqsave(port, &flags);
2633
2634 sci_reset(port);
2635
2636 uart_update_timeout(port, termios->c_cflag, baud);
2637
2638 /* byte size and parity */
2639 bits = tty_get_frame_size(termios->c_cflag);
2640
2641 if (sci_getreg(port, SEMR)->size)
2642 sci_serial_out(port, SEMR, 0);
2643
2644 if (best_clk >= 0) {
2645 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB)
2646 switch (srr + 1) {
2647 case 5: smr_val |= SCSMR_SRC_5; break;
2648 case 7: smr_val |= SCSMR_SRC_7; break;
2649 case 11: smr_val |= SCSMR_SRC_11; break;
2650 case 13: smr_val |= SCSMR_SRC_13; break;
2651 case 16: smr_val |= SCSMR_SRC_16; break;
2652 case 17: smr_val |= SCSMR_SRC_17; break;
2653 case 19: smr_val |= SCSMR_SRC_19; break;
2654 case 27: smr_val |= SCSMR_SRC_27; break;
2655 }
2656 smr_val |= cks;
2657 sci_serial_out(port, SCSCR, scr_val | s->hscif_tot);
2658 sci_serial_out(port, SCSMR, smr_val);
2659 sci_serial_out(port, SCBRR, brr);
2660 if (sci_getreg(port, HSSRR)->size) {
2661 unsigned int hssrr = srr | HSCIF_SRE;
2662 /* Calculate deviation from intended rate at the
2663 * center of the last stop bit in sampling clocks.
2664 */
2665 int last_stop = bits * 2 - 1;
2666 int deviation = DIV_ROUND_CLOSEST(min_err * last_stop *
2667 (int)(srr + 1),
2668 2 * (int)baud);
2669
2670 if (abs(deviation) >= 2) {
2671 /* At least two sampling clocks off at the
2672 * last stop bit; we can increase the error
2673 * margin by shifting the sampling point.
2674 */
2675 int shift = clamp(deviation / 2, -8, 7);
2676
2677 hssrr |= (shift << HSCIF_SRHP_SHIFT) &
2678 HSCIF_SRHP_MASK;
2679 hssrr |= HSCIF_SRDE;
2680 }
2681 sci_serial_out(port, HSSRR, hssrr);
2682 }
2683
2684 /* Wait one bit interval */
2685 udelay((1000000 + (baud - 1)) / baud);
2686 } else {
2687 /* Don't touch the bit rate configuration */
2688 scr_val = s->cfg->scscr & (SCSCR_CKE1 | SCSCR_CKE0);
2689 smr_val |= sci_serial_in(port, SCSMR) &
2690 (SCSMR_CKEDG | SCSMR_SRC_MASK | SCSMR_CKS);
2691 sci_serial_out(port, SCSCR, scr_val | s->hscif_tot);
2692 sci_serial_out(port, SCSMR, smr_val);
2693 }
2694
2695 sci_init_pins(port, termios->c_cflag);
2696
2697 port->status &= ~UPSTAT_AUTOCTS;
2698 s->autorts = false;
2699 reg = sci_getreg(port, SCFCR);
2700 if (reg->size) {
2701 unsigned short ctrl = sci_serial_in(port, SCFCR);
2702
2703 if ((port->flags & UPF_HARD_FLOW) &&
2704 (termios->c_cflag & CRTSCTS)) {
2705 /* There is no CTS interrupt to restart the hardware */
2706 port->status |= UPSTAT_AUTOCTS;
2707 /* MCE is enabled when RTS is raised */
2708 s->autorts = true;
2709 }
2710
2711 /*
2712 * As we've done a sci_reset() above, ensure we don't
2713 * interfere with the FIFOs while toggling MCE. As the
2714 * reset values could still be set, simply mask them out.
2715 */
2716 ctrl &= ~(SCFCR_RFRST | SCFCR_TFRST);
2717
2718 sci_serial_out(port, SCFCR, ctrl);
2719 }
2720 if (port->flags & UPF_HARD_FLOW) {
2721 /* Refresh (Auto) RTS */
2722 sci_set_mctrl(port, port->mctrl);
2723 }
2724
2725 /*
2726 * For SCI, TE (transmit enable) must be set after setting TIE
2727 * (transmit interrupt enable) or in the same instruction to
2728 * start the transmitting process. So skip setting TE here for SCI.
2729 */
2730 if (port->type != PORT_SCI)
2731 scr_val |= SCSCR_TE;
2732 scr_val |= SCSCR_RE | (s->cfg->scscr & ~(SCSCR_CKE1 | SCSCR_CKE0));
2733 sci_serial_out(port, SCSCR, scr_val | s->hscif_tot);
2734 if ((srr + 1 == 5) &&
2735 (port->type == PORT_SCIFA || port->type == PORT_SCIFB)) {
2736 /*
2737 * In asynchronous mode, when the sampling rate is 1/5, first
2738 * received data may become invalid on some SCIFA and SCIFB.
2739 * To avoid this problem wait more than 1 serial data time (1
2740 * bit time x serial data number) after setting SCSCR.RE = 1.
2741 */
2742 udelay(DIV_ROUND_UP(10 * 1000000, baud));
2743 }
2744
2745 /* Calculate delay for 2 DMA buffers (4 FIFO). */
2746 s->rx_frame = (10000 * bits) / (baud / 100);
2747 #ifdef CONFIG_SERIAL_SH_SCI_DMA
2748 s->rx_timeout = s->buf_len_rx * 2 * s->rx_frame;
2749 #endif
2750
2751 if ((termios->c_cflag & CREAD) != 0)
2752 sci_start_rx(port);
2753
2754 uart_port_unlock_irqrestore(port, flags);
2755
2756 sci_port_disable(s);
2757
2758 if (UART_ENABLE_MS(port, termios->c_cflag))
2759 sci_enable_ms(port);
2760 }
2761
sci_pm(struct uart_port * port,unsigned int state,unsigned int oldstate)2762 static void sci_pm(struct uart_port *port, unsigned int state,
2763 unsigned int oldstate)
2764 {
2765 struct sci_port *sci_port = to_sci_port(port);
2766
2767 switch (state) {
2768 case UART_PM_STATE_OFF:
2769 sci_port_disable(sci_port);
2770 break;
2771 default:
2772 sci_port_enable(sci_port);
2773 break;
2774 }
2775 }
2776
sci_type(struct uart_port * port)2777 static const char *sci_type(struct uart_port *port)
2778 {
2779 switch (port->type) {
2780 case PORT_IRDA:
2781 return "irda";
2782 case PORT_SCI:
2783 return "sci";
2784 case PORT_SCIF:
2785 return "scif";
2786 case PORT_SCIFA:
2787 return "scifa";
2788 case PORT_SCIFB:
2789 return "scifb";
2790 case PORT_HSCIF:
2791 return "hscif";
2792 }
2793
2794 return NULL;
2795 }
2796
sci_remap_port(struct uart_port * port)2797 static int sci_remap_port(struct uart_port *port)
2798 {
2799 struct sci_port *sport = to_sci_port(port);
2800
2801 /*
2802 * Nothing to do if there's already an established membase.
2803 */
2804 if (port->membase)
2805 return 0;
2806
2807 if (port->dev->of_node || (port->flags & UPF_IOREMAP)) {
2808 port->membase = ioremap(port->mapbase, sport->reg_size);
2809 if (unlikely(!port->membase)) {
2810 dev_err(port->dev, "can't remap port#%d\n", port->line);
2811 return -ENXIO;
2812 }
2813 } else {
2814 /*
2815 * For the simple (and majority of) cases where we don't
2816 * need to do any remapping, just cast the cookie
2817 * directly.
2818 */
2819 port->membase = (void __iomem *)(uintptr_t)port->mapbase;
2820 }
2821
2822 return 0;
2823 }
2824
sci_release_port(struct uart_port * port)2825 static void sci_release_port(struct uart_port *port)
2826 {
2827 struct sci_port *sport = to_sci_port(port);
2828
2829 if (port->dev->of_node || (port->flags & UPF_IOREMAP)) {
2830 iounmap(port->membase);
2831 port->membase = NULL;
2832 }
2833
2834 release_mem_region(port->mapbase, sport->reg_size);
2835 }
2836
sci_request_port(struct uart_port * port)2837 static int sci_request_port(struct uart_port *port)
2838 {
2839 struct resource *res;
2840 struct sci_port *sport = to_sci_port(port);
2841 int ret;
2842
2843 res = request_mem_region(port->mapbase, sport->reg_size,
2844 dev_name(port->dev));
2845 if (unlikely(res == NULL)) {
2846 dev_err(port->dev, "request_mem_region failed.");
2847 return -EBUSY;
2848 }
2849
2850 ret = sci_remap_port(port);
2851 if (unlikely(ret != 0)) {
2852 release_resource(res);
2853 return ret;
2854 }
2855
2856 return 0;
2857 }
2858
sci_config_port(struct uart_port * port,int flags)2859 static void sci_config_port(struct uart_port *port, int flags)
2860 {
2861 if (flags & UART_CONFIG_TYPE) {
2862 struct sci_port *sport = to_sci_port(port);
2863
2864 port->type = sport->cfg->type;
2865 sci_request_port(port);
2866 }
2867 }
2868
sci_verify_port(struct uart_port * port,struct serial_struct * ser)2869 static int sci_verify_port(struct uart_port *port, struct serial_struct *ser)
2870 {
2871 if (ser->baud_base < 2400)
2872 /* No paper tape reader for Mitch.. */
2873 return -EINVAL;
2874
2875 return 0;
2876 }
2877
2878 static const struct uart_ops sci_uart_ops = {
2879 .tx_empty = sci_tx_empty,
2880 .set_mctrl = sci_set_mctrl,
2881 .get_mctrl = sci_get_mctrl,
2882 .start_tx = sci_start_tx,
2883 .stop_tx = sci_stop_tx,
2884 .stop_rx = sci_stop_rx,
2885 .enable_ms = sci_enable_ms,
2886 .break_ctl = sci_break_ctl,
2887 .startup = sci_startup,
2888 .shutdown = sci_shutdown,
2889 .flush_buffer = sci_flush_buffer,
2890 .set_termios = sci_set_termios,
2891 .pm = sci_pm,
2892 .type = sci_type,
2893 .release_port = sci_release_port,
2894 .request_port = sci_request_port,
2895 .config_port = sci_config_port,
2896 .verify_port = sci_verify_port,
2897 #ifdef CONFIG_CONSOLE_POLL
2898 .poll_get_char = sci_poll_get_char,
2899 .poll_put_char = sci_poll_put_char,
2900 #endif
2901 };
2902
sci_init_clocks(struct sci_port * sci_port,struct device * dev)2903 static int sci_init_clocks(struct sci_port *sci_port, struct device *dev)
2904 {
2905 const char *clk_names[] = {
2906 [SCI_FCK] = "fck",
2907 [SCI_SCK] = "sck",
2908 [SCI_BRG_INT] = "brg_int",
2909 [SCI_SCIF_CLK] = "scif_clk",
2910 };
2911 struct clk *clk;
2912 unsigned int i;
2913
2914 if (sci_port->cfg->type == PORT_HSCIF)
2915 clk_names[SCI_SCK] = "hsck";
2916
2917 for (i = 0; i < SCI_NUM_CLKS; i++) {
2918 clk = devm_clk_get_optional(dev, clk_names[i]);
2919 if (IS_ERR(clk))
2920 return PTR_ERR(clk);
2921
2922 if (!clk && i == SCI_FCK) {
2923 /*
2924 * Not all SH platforms declare a clock lookup entry
2925 * for SCI devices, in which case we need to get the
2926 * global "peripheral_clk" clock.
2927 */
2928 clk = devm_clk_get(dev, "peripheral_clk");
2929 if (IS_ERR(clk))
2930 return dev_err_probe(dev, PTR_ERR(clk),
2931 "failed to get %s\n",
2932 clk_names[i]);
2933 }
2934
2935 if (!clk)
2936 dev_dbg(dev, "failed to get %s\n", clk_names[i]);
2937 else
2938 dev_dbg(dev, "clk %s is %pC rate %lu\n", clk_names[i],
2939 clk, clk_get_rate(clk));
2940 sci_port->clks[i] = clk;
2941 }
2942 return 0;
2943 }
2944
2945 static const struct sci_port_params *
sci_probe_regmap(const struct plat_sci_port * cfg)2946 sci_probe_regmap(const struct plat_sci_port *cfg)
2947 {
2948 unsigned int regtype;
2949
2950 if (cfg->regtype != SCIx_PROBE_REGTYPE)
2951 return &sci_port_params[cfg->regtype];
2952
2953 switch (cfg->type) {
2954 case PORT_SCI:
2955 regtype = SCIx_SCI_REGTYPE;
2956 break;
2957 case PORT_IRDA:
2958 regtype = SCIx_IRDA_REGTYPE;
2959 break;
2960 case PORT_SCIFA:
2961 regtype = SCIx_SCIFA_REGTYPE;
2962 break;
2963 case PORT_SCIFB:
2964 regtype = SCIx_SCIFB_REGTYPE;
2965 break;
2966 case PORT_SCIF:
2967 /*
2968 * The SH-4 is a bit of a misnomer here, although that's
2969 * where this particular port layout originated. This
2970 * configuration (or some slight variation thereof)
2971 * remains the dominant model for all SCIFs.
2972 */
2973 regtype = SCIx_SH4_SCIF_REGTYPE;
2974 break;
2975 case PORT_HSCIF:
2976 regtype = SCIx_HSCIF_REGTYPE;
2977 break;
2978 default:
2979 pr_err("Can't probe register map for given port\n");
2980 return NULL;
2981 }
2982
2983 return &sci_port_params[regtype];
2984 }
2985
sci_init_single(struct platform_device * dev,struct sci_port * sci_port,unsigned int index,const struct plat_sci_port * p,bool early)2986 static int sci_init_single(struct platform_device *dev,
2987 struct sci_port *sci_port, unsigned int index,
2988 const struct plat_sci_port *p, bool early)
2989 {
2990 struct uart_port *port = &sci_port->port;
2991 const struct resource *res;
2992 unsigned int i;
2993 int ret;
2994
2995 sci_port->cfg = p;
2996
2997 port->ops = &sci_uart_ops;
2998 port->iotype = UPIO_MEM;
2999 port->line = index;
3000 port->has_sysrq = IS_ENABLED(CONFIG_SERIAL_SH_SCI_CONSOLE);
3001
3002 res = platform_get_resource(dev, IORESOURCE_MEM, 0);
3003 if (res == NULL)
3004 return -ENOMEM;
3005
3006 port->mapbase = res->start;
3007 sci_port->reg_size = resource_size(res);
3008
3009 for (i = 0; i < ARRAY_SIZE(sci_port->irqs); ++i) {
3010 if (i)
3011 sci_port->irqs[i] = platform_get_irq_optional(dev, i);
3012 else
3013 sci_port->irqs[i] = platform_get_irq(dev, i);
3014 }
3015
3016 /*
3017 * The fourth interrupt on SCI port is transmit end interrupt, so
3018 * shuffle the interrupts.
3019 */
3020 if (p->type == PORT_SCI)
3021 swap(sci_port->irqs[SCIx_BRI_IRQ], sci_port->irqs[SCIx_TEI_IRQ]);
3022
3023 /* The SCI generates several interrupts. They can be muxed together or
3024 * connected to different interrupt lines. In the muxed case only one
3025 * interrupt resource is specified as there is only one interrupt ID.
3026 * In the non-muxed case, up to 6 interrupt signals might be generated
3027 * from the SCI, however those signals might have their own individual
3028 * interrupt ID numbers, or muxed together with another interrupt.
3029 */
3030 if (sci_port->irqs[0] < 0)
3031 return -ENXIO;
3032
3033 if (sci_port->irqs[1] < 0)
3034 for (i = 1; i < ARRAY_SIZE(sci_port->irqs); i++)
3035 sci_port->irqs[i] = sci_port->irqs[0];
3036
3037 sci_port->params = sci_probe_regmap(p);
3038 if (unlikely(sci_port->params == NULL))
3039 return -EINVAL;
3040
3041 switch (p->type) {
3042 case PORT_SCIFB:
3043 sci_port->rx_trigger = 48;
3044 break;
3045 case PORT_HSCIF:
3046 sci_port->rx_trigger = 64;
3047 break;
3048 case PORT_SCIFA:
3049 sci_port->rx_trigger = 32;
3050 break;
3051 case PORT_SCIF:
3052 if (p->regtype == SCIx_SH7705_SCIF_REGTYPE)
3053 /* RX triggering not implemented for this IP */
3054 sci_port->rx_trigger = 1;
3055 else
3056 sci_port->rx_trigger = 8;
3057 break;
3058 default:
3059 sci_port->rx_trigger = 1;
3060 break;
3061 }
3062
3063 sci_port->rx_fifo_timeout = 0;
3064 sci_port->hscif_tot = 0;
3065
3066 /* SCIFA on sh7723 and sh7724 need a custom sampling rate that doesn't
3067 * match the SoC datasheet, this should be investigated. Let platform
3068 * data override the sampling rate for now.
3069 */
3070 sci_port->sampling_rate_mask = p->sampling_rate
3071 ? SCI_SR(p->sampling_rate)
3072 : sci_port->params->sampling_rate_mask;
3073
3074 if (!early) {
3075 ret = sci_init_clocks(sci_port, &dev->dev);
3076 if (ret < 0)
3077 return ret;
3078 }
3079
3080 port->type = p->type;
3081 port->flags = UPF_FIXED_PORT | UPF_BOOT_AUTOCONF | p->flags;
3082 port->fifosize = sci_port->params->fifosize;
3083
3084 if (port->type == PORT_SCI && !dev->dev.of_node) {
3085 if (sci_port->reg_size >= 0x20)
3086 port->regshift = 2;
3087 else
3088 port->regshift = 1;
3089 }
3090
3091 /*
3092 * The UART port needs an IRQ value, so we peg this to the RX IRQ
3093 * for the multi-IRQ ports, which is where we are primarily
3094 * concerned with the shutdown path synchronization.
3095 *
3096 * For the muxed case there's nothing more to do.
3097 */
3098 port->irq = sci_port->irqs[SCIx_RXI_IRQ];
3099 port->irqflags = 0;
3100
3101 return 0;
3102 }
3103
3104 #if defined(CONFIG_SERIAL_SH_SCI_CONSOLE) || \
3105 defined(CONFIG_SERIAL_SH_SCI_EARLYCON)
serial_console_putchar(struct uart_port * port,unsigned char ch)3106 static void serial_console_putchar(struct uart_port *port, unsigned char ch)
3107 {
3108 sci_poll_put_char(port, ch);
3109 }
3110
3111 /*
3112 * Print a string to the serial port trying not to disturb
3113 * any possible real use of the port...
3114 */
serial_console_write(struct console * co,const char * s,unsigned count)3115 static void serial_console_write(struct console *co, const char *s,
3116 unsigned count)
3117 {
3118 struct sci_port *sci_port = &sci_ports[co->index];
3119 struct uart_port *port = &sci_port->port;
3120 unsigned short bits, ctrl, ctrl_temp;
3121 unsigned long flags;
3122 int locked = 1;
3123
3124 if (port->sysrq)
3125 locked = 0;
3126 else if (oops_in_progress)
3127 locked = uart_port_trylock_irqsave(port, &flags);
3128 else
3129 uart_port_lock_irqsave(port, &flags);
3130
3131 /* first save SCSCR then disable interrupts, keep clock source */
3132 ctrl = sci_serial_in(port, SCSCR);
3133 ctrl_temp = SCSCR_RE | SCSCR_TE |
3134 (sci_port->cfg->scscr & ~(SCSCR_CKE1 | SCSCR_CKE0)) |
3135 (ctrl & (SCSCR_CKE1 | SCSCR_CKE0));
3136 sci_serial_out(port, SCSCR, ctrl_temp | sci_port->hscif_tot);
3137
3138 uart_console_write(port, s, count, serial_console_putchar);
3139
3140 /* wait until fifo is empty and last bit has been transmitted */
3141 bits = SCxSR_TDxE(port) | SCxSR_TEND(port);
3142 while ((sci_serial_in(port, SCxSR) & bits) != bits)
3143 cpu_relax();
3144
3145 /* restore the SCSCR */
3146 sci_serial_out(port, SCSCR, ctrl);
3147
3148 if (locked)
3149 uart_port_unlock_irqrestore(port, flags);
3150 }
3151
serial_console_setup(struct console * co,char * options)3152 static int serial_console_setup(struct console *co, char *options)
3153 {
3154 struct sci_port *sci_port;
3155 struct uart_port *port;
3156 int baud = 115200;
3157 int bits = 8;
3158 int parity = 'n';
3159 int flow = 'n';
3160 int ret;
3161
3162 /*
3163 * Refuse to handle any bogus ports.
3164 */
3165 if (co->index < 0 || co->index >= SCI_NPORTS)
3166 return -ENODEV;
3167
3168 sci_port = &sci_ports[co->index];
3169 port = &sci_port->port;
3170
3171 /*
3172 * Refuse to handle uninitialized ports.
3173 */
3174 if (!port->ops)
3175 return -ENODEV;
3176
3177 ret = sci_remap_port(port);
3178 if (unlikely(ret != 0))
3179 return ret;
3180
3181 if (options)
3182 uart_parse_options(options, &baud, &parity, &bits, &flow);
3183
3184 return uart_set_options(port, co, baud, parity, bits, flow);
3185 }
3186
3187 static struct console serial_console = {
3188 .name = "ttySC",
3189 .device = uart_console_device,
3190 .write = serial_console_write,
3191 .setup = serial_console_setup,
3192 .flags = CON_PRINTBUFFER,
3193 .index = -1,
3194 .data = &sci_uart_driver,
3195 };
3196
3197 #ifdef CONFIG_SUPERH
3198 static char early_serial_buf[32];
3199
early_serial_console_setup(struct console * co,char * options)3200 static int early_serial_console_setup(struct console *co, char *options)
3201 {
3202 /*
3203 * This early console is always registered using the earlyprintk=
3204 * parameter, which does not call add_preferred_console(). Thus
3205 * @options is always NULL and the options for this early console
3206 * are passed using a custom buffer.
3207 */
3208 WARN_ON(options);
3209
3210 return serial_console_setup(co, early_serial_buf);
3211 }
3212
3213 static struct console early_serial_console = {
3214 .name = "early_ttySC",
3215 .write = serial_console_write,
3216 .setup = early_serial_console_setup,
3217 .flags = CON_PRINTBUFFER,
3218 .index = -1,
3219 };
3220
sci_probe_earlyprintk(struct platform_device * pdev)3221 static int sci_probe_earlyprintk(struct platform_device *pdev)
3222 {
3223 const struct plat_sci_port *cfg = dev_get_platdata(&pdev->dev);
3224
3225 if (early_serial_console.data)
3226 return -EEXIST;
3227
3228 early_serial_console.index = pdev->id;
3229
3230 sci_init_single(pdev, &sci_ports[pdev->id], pdev->id, cfg, true);
3231
3232 if (!strstr(early_serial_buf, "keep"))
3233 early_serial_console.flags |= CON_BOOT;
3234
3235 register_console(&early_serial_console);
3236 return 0;
3237 }
3238 #endif
3239
3240 #define SCI_CONSOLE (&serial_console)
3241
3242 #else
sci_probe_earlyprintk(struct platform_device * pdev)3243 static inline int sci_probe_earlyprintk(struct platform_device *pdev)
3244 {
3245 return -EINVAL;
3246 }
3247
3248 #define SCI_CONSOLE NULL
3249
3250 #endif /* CONFIG_SERIAL_SH_SCI_CONSOLE || CONFIG_SERIAL_SH_SCI_EARLYCON */
3251
3252 static const char banner[] __initconst = "SuperH (H)SCI(F) driver initialized";
3253
3254 static DEFINE_MUTEX(sci_uart_registration_lock);
3255 static struct uart_driver sci_uart_driver = {
3256 .owner = THIS_MODULE,
3257 .driver_name = "sci",
3258 .dev_name = "ttySC",
3259 .major = SCI_MAJOR,
3260 .minor = SCI_MINOR_START,
3261 .nr = SCI_NPORTS,
3262 .cons = SCI_CONSOLE,
3263 };
3264
sci_remove(struct platform_device * dev)3265 static void sci_remove(struct platform_device *dev)
3266 {
3267 struct sci_port *port = platform_get_drvdata(dev);
3268 unsigned int type = port->port.type; /* uart_remove_... clears it */
3269
3270 sci_ports_in_use &= ~BIT(port->port.line);
3271 uart_remove_one_port(&sci_uart_driver, &port->port);
3272
3273 if (port->port.fifosize > 1)
3274 device_remove_file(&dev->dev, &dev_attr_rx_fifo_trigger);
3275 if (type == PORT_SCIFA || type == PORT_SCIFB || type == PORT_HSCIF)
3276 device_remove_file(&dev->dev, &dev_attr_rx_fifo_timeout);
3277 }
3278
3279
3280 #define SCI_OF_DATA(type, regtype) (void *)((type) << 16 | (regtype))
3281 #define SCI_OF_TYPE(data) ((unsigned long)(data) >> 16)
3282 #define SCI_OF_REGTYPE(data) ((unsigned long)(data) & 0xffff)
3283
3284 static const struct of_device_id of_sci_match[] __maybe_unused = {
3285 /* SoC-specific types */
3286 {
3287 .compatible = "renesas,scif-r7s72100",
3288 .data = SCI_OF_DATA(PORT_SCIF, SCIx_SH2_SCIF_FIFODATA_REGTYPE),
3289 },
3290 {
3291 .compatible = "renesas,scif-r7s9210",
3292 .data = SCI_OF_DATA(PORT_SCIF, SCIx_RZ_SCIFA_REGTYPE),
3293 },
3294 {
3295 .compatible = "renesas,scif-r9a07g044",
3296 .data = SCI_OF_DATA(PORT_SCIF, SCIx_RZ_SCIFA_REGTYPE),
3297 },
3298 {
3299 .compatible = "renesas,scif-r9a09g057",
3300 .data = SCI_OF_DATA(PORT_SCIF, SCIx_RZV2H_SCIF_REGTYPE),
3301 },
3302 /* Family-specific types */
3303 {
3304 .compatible = "renesas,rcar-gen1-scif",
3305 .data = SCI_OF_DATA(PORT_SCIF, SCIx_SH4_SCIF_BRG_REGTYPE),
3306 }, {
3307 .compatible = "renesas,rcar-gen2-scif",
3308 .data = SCI_OF_DATA(PORT_SCIF, SCIx_SH4_SCIF_BRG_REGTYPE),
3309 }, {
3310 .compatible = "renesas,rcar-gen3-scif",
3311 .data = SCI_OF_DATA(PORT_SCIF, SCIx_SH4_SCIF_BRG_REGTYPE),
3312 }, {
3313 .compatible = "renesas,rcar-gen4-scif",
3314 .data = SCI_OF_DATA(PORT_SCIF, SCIx_SH4_SCIF_BRG_REGTYPE),
3315 },
3316 /* Generic types */
3317 {
3318 .compatible = "renesas,scif",
3319 .data = SCI_OF_DATA(PORT_SCIF, SCIx_SH4_SCIF_REGTYPE),
3320 }, {
3321 .compatible = "renesas,scifa",
3322 .data = SCI_OF_DATA(PORT_SCIFA, SCIx_SCIFA_REGTYPE),
3323 }, {
3324 .compatible = "renesas,scifb",
3325 .data = SCI_OF_DATA(PORT_SCIFB, SCIx_SCIFB_REGTYPE),
3326 }, {
3327 .compatible = "renesas,hscif",
3328 .data = SCI_OF_DATA(PORT_HSCIF, SCIx_HSCIF_REGTYPE),
3329 }, {
3330 .compatible = "renesas,sci",
3331 .data = SCI_OF_DATA(PORT_SCI, SCIx_SCI_REGTYPE),
3332 }, {
3333 /* Terminator */
3334 },
3335 };
3336 MODULE_DEVICE_TABLE(of, of_sci_match);
3337
sci_reset_control_assert(void * data)3338 static void sci_reset_control_assert(void *data)
3339 {
3340 reset_control_assert(data);
3341 }
3342
sci_parse_dt(struct platform_device * pdev,unsigned int * dev_id)3343 static struct plat_sci_port *sci_parse_dt(struct platform_device *pdev,
3344 unsigned int *dev_id)
3345 {
3346 struct device_node *np = pdev->dev.of_node;
3347 struct reset_control *rstc;
3348 struct plat_sci_port *p;
3349 struct sci_port *sp;
3350 const void *data;
3351 int id, ret;
3352
3353 if (!IS_ENABLED(CONFIG_OF) || !np)
3354 return ERR_PTR(-EINVAL);
3355
3356 data = of_device_get_match_data(&pdev->dev);
3357
3358 rstc = devm_reset_control_get_optional_exclusive(&pdev->dev, NULL);
3359 if (IS_ERR(rstc))
3360 return ERR_PTR(dev_err_probe(&pdev->dev, PTR_ERR(rstc),
3361 "failed to get reset ctrl\n"));
3362
3363 ret = reset_control_deassert(rstc);
3364 if (ret) {
3365 dev_err(&pdev->dev, "failed to deassert reset %d\n", ret);
3366 return ERR_PTR(ret);
3367 }
3368
3369 ret = devm_add_action_or_reset(&pdev->dev, sci_reset_control_assert, rstc);
3370 if (ret) {
3371 dev_err(&pdev->dev, "failed to register assert devm action, %d\n",
3372 ret);
3373 return ERR_PTR(ret);
3374 }
3375
3376 p = devm_kzalloc(&pdev->dev, sizeof(struct plat_sci_port), GFP_KERNEL);
3377 if (!p)
3378 return ERR_PTR(-ENOMEM);
3379
3380 /* Get the line number from the aliases node. */
3381 id = of_alias_get_id(np, "serial");
3382 if (id < 0 && ~sci_ports_in_use)
3383 id = ffz(sci_ports_in_use);
3384 if (id < 0) {
3385 dev_err(&pdev->dev, "failed to get alias id (%d)\n", id);
3386 return ERR_PTR(-EINVAL);
3387 }
3388 if (id >= ARRAY_SIZE(sci_ports)) {
3389 dev_err(&pdev->dev, "serial%d out of range\n", id);
3390 return ERR_PTR(-EINVAL);
3391 }
3392
3393 sp = &sci_ports[id];
3394 sp->rstc = rstc;
3395 *dev_id = id;
3396
3397 p->type = SCI_OF_TYPE(data);
3398 p->regtype = SCI_OF_REGTYPE(data);
3399
3400 sp->has_rtscts = of_property_read_bool(np, "uart-has-rtscts");
3401
3402 return p;
3403 }
3404
sci_probe_single(struct platform_device * dev,unsigned int index,struct plat_sci_port * p,struct sci_port * sciport,struct resource * sci_res)3405 static int sci_probe_single(struct platform_device *dev,
3406 unsigned int index,
3407 struct plat_sci_port *p,
3408 struct sci_port *sciport,
3409 struct resource *sci_res)
3410 {
3411 int ret;
3412
3413 /* Sanity check */
3414 if (unlikely(index >= SCI_NPORTS)) {
3415 dev_notice(&dev->dev, "Attempting to register port %d when only %d are available\n",
3416 index+1, SCI_NPORTS);
3417 dev_notice(&dev->dev, "Consider bumping CONFIG_SERIAL_SH_SCI_NR_UARTS!\n");
3418 return -EINVAL;
3419 }
3420 BUILD_BUG_ON(SCI_NPORTS > sizeof(sci_ports_in_use) * 8);
3421 if (sci_ports_in_use & BIT(index))
3422 return -EBUSY;
3423
3424 mutex_lock(&sci_uart_registration_lock);
3425 if (!sci_uart_driver.state) {
3426 ret = uart_register_driver(&sci_uart_driver);
3427 if (ret) {
3428 mutex_unlock(&sci_uart_registration_lock);
3429 return ret;
3430 }
3431 }
3432 mutex_unlock(&sci_uart_registration_lock);
3433
3434 ret = sci_init_single(dev, sciport, index, p, false);
3435 if (ret)
3436 return ret;
3437
3438 sciport->port.dev = &dev->dev;
3439 ret = devm_pm_runtime_enable(&dev->dev);
3440 if (ret)
3441 return ret;
3442
3443 sciport->gpios = mctrl_gpio_init(&sciport->port, 0);
3444 if (IS_ERR(sciport->gpios))
3445 return PTR_ERR(sciport->gpios);
3446
3447 if (sciport->has_rtscts) {
3448 if (mctrl_gpio_to_gpiod(sciport->gpios, UART_GPIO_CTS) ||
3449 mctrl_gpio_to_gpiod(sciport->gpios, UART_GPIO_RTS)) {
3450 dev_err(&dev->dev, "Conflicting RTS/CTS config\n");
3451 return -EINVAL;
3452 }
3453 sciport->port.flags |= UPF_HARD_FLOW;
3454 }
3455
3456 if (sci_uart_earlycon && sci_ports[0].port.mapbase == sci_res->start) {
3457 /*
3458 * In case:
3459 * - this is the earlycon port (mapped on index 0 in sci_ports[]) and
3460 * - it now maps to an alias other than zero and
3461 * - the earlycon is still alive (e.g., "earlycon keep_bootcon" is
3462 * available in bootargs)
3463 *
3464 * we need to avoid disabling clocks and PM domains through the runtime
3465 * PM APIs called in __device_attach(). For this, increment the runtime
3466 * PM reference counter (the clocks and PM domains were already enabled
3467 * by the bootloader). Otherwise the earlycon may access the HW when it
3468 * has no clocks enabled leading to failures (infinite loop in
3469 * sci_poll_put_char()).
3470 */
3471 pm_runtime_get_noresume(&dev->dev);
3472
3473 /*
3474 * Skip cleanup the sci_port[0] in early_console_exit(), this
3475 * port is the same as the earlycon one.
3476 */
3477 sci_uart_earlycon_dev_probing = true;
3478 }
3479
3480 return uart_add_one_port(&sci_uart_driver, &sciport->port);
3481 }
3482
sci_probe(struct platform_device * dev)3483 static int sci_probe(struct platform_device *dev)
3484 {
3485 struct plat_sci_port *p;
3486 struct resource *res;
3487 struct sci_port *sp;
3488 unsigned int dev_id;
3489 int ret;
3490
3491 /*
3492 * If we've come here via earlyprintk initialization, head off to
3493 * the special early probe. We don't have sufficient device state
3494 * to make it beyond this yet.
3495 */
3496 #ifdef CONFIG_SUPERH
3497 if (is_sh_early_platform_device(dev))
3498 return sci_probe_earlyprintk(dev);
3499 #endif
3500
3501 if (dev->dev.of_node) {
3502 p = sci_parse_dt(dev, &dev_id);
3503 if (IS_ERR(p))
3504 return PTR_ERR(p);
3505 } else {
3506 p = dev->dev.platform_data;
3507 if (p == NULL) {
3508 dev_err(&dev->dev, "no platform data supplied\n");
3509 return -EINVAL;
3510 }
3511
3512 dev_id = dev->id;
3513 }
3514
3515 sp = &sci_ports[dev_id];
3516
3517 /*
3518 * In case:
3519 * - the probed port alias is zero (as the one used by earlycon), and
3520 * - the earlycon is still active (e.g., "earlycon keep_bootcon" in
3521 * bootargs)
3522 *
3523 * defer the probe of this serial. This is a debug scenario and the user
3524 * must be aware of it.
3525 *
3526 * Except when the probed port is the same as the earlycon port.
3527 */
3528
3529 res = platform_get_resource(dev, IORESOURCE_MEM, 0);
3530 if (!res)
3531 return -ENODEV;
3532
3533 if (sci_uart_earlycon && sp == &sci_ports[0] && sp->port.mapbase != res->start)
3534 return dev_err_probe(&dev->dev, -EBUSY, "sci_port[0] is used by earlycon!\n");
3535
3536 platform_set_drvdata(dev, sp);
3537
3538 ret = sci_probe_single(dev, dev_id, p, sp, res);
3539 if (ret)
3540 return ret;
3541
3542 if (sp->port.fifosize > 1) {
3543 ret = device_create_file(&dev->dev, &dev_attr_rx_fifo_trigger);
3544 if (ret)
3545 return ret;
3546 }
3547 if (sp->port.type == PORT_SCIFA || sp->port.type == PORT_SCIFB ||
3548 sp->port.type == PORT_HSCIF) {
3549 ret = device_create_file(&dev->dev, &dev_attr_rx_fifo_timeout);
3550 if (ret) {
3551 if (sp->port.fifosize > 1) {
3552 device_remove_file(&dev->dev,
3553 &dev_attr_rx_fifo_trigger);
3554 }
3555 return ret;
3556 }
3557 }
3558
3559 #ifdef CONFIG_SH_STANDARD_BIOS
3560 sh_bios_gdb_detach();
3561 #endif
3562
3563 sci_ports_in_use |= BIT(dev_id);
3564 return 0;
3565 }
3566
sci_console_save(struct sci_port * s)3567 static void sci_console_save(struct sci_port *s)
3568 {
3569 struct sci_suspend_regs *regs = &s->suspend_regs;
3570 struct uart_port *port = &s->port;
3571
3572 if (sci_getreg(port, SCDL)->size)
3573 regs->scdl = sci_serial_in(port, SCDL);
3574 if (sci_getreg(port, SCCKS)->size)
3575 regs->sccks = sci_serial_in(port, SCCKS);
3576 if (sci_getreg(port, SCSMR)->size)
3577 regs->scsmr = sci_serial_in(port, SCSMR);
3578 if (sci_getreg(port, SCSCR)->size)
3579 regs->scscr = sci_serial_in(port, SCSCR);
3580 if (sci_getreg(port, SCFCR)->size)
3581 regs->scfcr = sci_serial_in(port, SCFCR);
3582 if (sci_getreg(port, SCSPTR)->size)
3583 regs->scsptr = sci_serial_in(port, SCSPTR);
3584 if (sci_getreg(port, SCBRR)->size)
3585 regs->scbrr = sci_serial_in(port, SCBRR);
3586 if (sci_getreg(port, HSSRR)->size)
3587 regs->hssrr = sci_serial_in(port, HSSRR);
3588 if (sci_getreg(port, SCPCR)->size)
3589 regs->scpcr = sci_serial_in(port, SCPCR);
3590 if (sci_getreg(port, SCPDR)->size)
3591 regs->scpdr = sci_serial_in(port, SCPDR);
3592 if (sci_getreg(port, SEMR)->size)
3593 regs->semr = sci_serial_in(port, SEMR);
3594 }
3595
sci_console_restore(struct sci_port * s)3596 static void sci_console_restore(struct sci_port *s)
3597 {
3598 struct sci_suspend_regs *regs = &s->suspend_regs;
3599 struct uart_port *port = &s->port;
3600
3601 if (sci_getreg(port, SCDL)->size)
3602 sci_serial_out(port, SCDL, regs->scdl);
3603 if (sci_getreg(port, SCCKS)->size)
3604 sci_serial_out(port, SCCKS, regs->sccks);
3605 if (sci_getreg(port, SCSMR)->size)
3606 sci_serial_out(port, SCSMR, regs->scsmr);
3607 if (sci_getreg(port, SCSCR)->size)
3608 sci_serial_out(port, SCSCR, regs->scscr);
3609 if (sci_getreg(port, SCFCR)->size)
3610 sci_serial_out(port, SCFCR, regs->scfcr);
3611 if (sci_getreg(port, SCSPTR)->size)
3612 sci_serial_out(port, SCSPTR, regs->scsptr);
3613 if (sci_getreg(port, SCBRR)->size)
3614 sci_serial_out(port, SCBRR, regs->scbrr);
3615 if (sci_getreg(port, HSSRR)->size)
3616 sci_serial_out(port, HSSRR, regs->hssrr);
3617 if (sci_getreg(port, SCPCR)->size)
3618 sci_serial_out(port, SCPCR, regs->scpcr);
3619 if (sci_getreg(port, SCPDR)->size)
3620 sci_serial_out(port, SCPDR, regs->scpdr);
3621 if (sci_getreg(port, SEMR)->size)
3622 sci_serial_out(port, SEMR, regs->semr);
3623 }
3624
sci_suspend(struct device * dev)3625 static __maybe_unused int sci_suspend(struct device *dev)
3626 {
3627 struct sci_port *sport = dev_get_drvdata(dev);
3628
3629 if (sport) {
3630 uart_suspend_port(&sci_uart_driver, &sport->port);
3631
3632 if (!console_suspend_enabled && uart_console(&sport->port))
3633 sci_console_save(sport);
3634 else
3635 return reset_control_assert(sport->rstc);
3636 }
3637
3638 return 0;
3639 }
3640
sci_resume(struct device * dev)3641 static __maybe_unused int sci_resume(struct device *dev)
3642 {
3643 struct sci_port *sport = dev_get_drvdata(dev);
3644
3645 if (sport) {
3646 if (!console_suspend_enabled && uart_console(&sport->port)) {
3647 sci_console_restore(sport);
3648 } else {
3649 int ret = reset_control_deassert(sport->rstc);
3650
3651 if (ret)
3652 return ret;
3653 }
3654
3655 uart_resume_port(&sci_uart_driver, &sport->port);
3656 }
3657
3658 return 0;
3659 }
3660
3661 static SIMPLE_DEV_PM_OPS(sci_dev_pm_ops, sci_suspend, sci_resume);
3662
3663 static struct platform_driver sci_driver = {
3664 .probe = sci_probe,
3665 .remove_new = sci_remove,
3666 .driver = {
3667 .name = "sh-sci",
3668 .pm = &sci_dev_pm_ops,
3669 .of_match_table = of_match_ptr(of_sci_match),
3670 },
3671 };
3672
sci_init(void)3673 static int __init sci_init(void)
3674 {
3675 pr_info("%s\n", banner);
3676
3677 return platform_driver_register(&sci_driver);
3678 }
3679
sci_exit(void)3680 static void __exit sci_exit(void)
3681 {
3682 platform_driver_unregister(&sci_driver);
3683
3684 if (sci_uart_driver.state)
3685 uart_unregister_driver(&sci_uart_driver);
3686 }
3687
3688 #if defined(CONFIG_SUPERH) && defined(CONFIG_SERIAL_SH_SCI_CONSOLE)
3689 sh_early_platform_init_buffer("earlyprintk", &sci_driver,
3690 early_serial_buf, ARRAY_SIZE(early_serial_buf));
3691 #endif
3692 #ifdef CONFIG_SERIAL_SH_SCI_EARLYCON
3693 static struct plat_sci_port port_cfg;
3694
early_console_exit(struct console * co)3695 static int early_console_exit(struct console *co)
3696 {
3697 struct sci_port *sci_port = &sci_ports[0];
3698
3699 /*
3700 * Clean the slot used by earlycon. A new SCI device might
3701 * map to this slot.
3702 */
3703 if (!sci_uart_earlycon_dev_probing) {
3704 memset(sci_port, 0, sizeof(*sci_port));
3705 sci_uart_earlycon = false;
3706 }
3707
3708 return 0;
3709 }
3710
early_console_setup(struct earlycon_device * device,int type)3711 static int __init early_console_setup(struct earlycon_device *device,
3712 int type)
3713 {
3714 if (!device->port.membase)
3715 return -ENODEV;
3716
3717 device->port.type = type;
3718 memcpy(&sci_ports[0].port, &device->port, sizeof(struct uart_port));
3719 port_cfg.type = type;
3720 sci_ports[0].cfg = &port_cfg;
3721 sci_ports[0].params = sci_probe_regmap(&port_cfg);
3722 sci_uart_earlycon = true;
3723 port_cfg.scscr = sci_serial_in(&sci_ports[0].port, SCSCR);
3724 sci_serial_out(&sci_ports[0].port, SCSCR,
3725 SCSCR_RE | SCSCR_TE | port_cfg.scscr);
3726
3727 device->con->write = serial_console_write;
3728 device->con->exit = early_console_exit;
3729
3730 return 0;
3731 }
sci_early_console_setup(struct earlycon_device * device,const char * opt)3732 static int __init sci_early_console_setup(struct earlycon_device *device,
3733 const char *opt)
3734 {
3735 return early_console_setup(device, PORT_SCI);
3736 }
scif_early_console_setup(struct earlycon_device * device,const char * opt)3737 static int __init scif_early_console_setup(struct earlycon_device *device,
3738 const char *opt)
3739 {
3740 return early_console_setup(device, PORT_SCIF);
3741 }
rzscifa_early_console_setup(struct earlycon_device * device,const char * opt)3742 static int __init rzscifa_early_console_setup(struct earlycon_device *device,
3743 const char *opt)
3744 {
3745 port_cfg.regtype = SCIx_RZ_SCIFA_REGTYPE;
3746 return early_console_setup(device, PORT_SCIF);
3747 }
3748
rzv2hscif_early_console_setup(struct earlycon_device * device,const char * opt)3749 static int __init rzv2hscif_early_console_setup(struct earlycon_device *device,
3750 const char *opt)
3751 {
3752 port_cfg.regtype = SCIx_RZV2H_SCIF_REGTYPE;
3753 return early_console_setup(device, PORT_SCIF);
3754 }
3755
scifa_early_console_setup(struct earlycon_device * device,const char * opt)3756 static int __init scifa_early_console_setup(struct earlycon_device *device,
3757 const char *opt)
3758 {
3759 return early_console_setup(device, PORT_SCIFA);
3760 }
scifb_early_console_setup(struct earlycon_device * device,const char * opt)3761 static int __init scifb_early_console_setup(struct earlycon_device *device,
3762 const char *opt)
3763 {
3764 return early_console_setup(device, PORT_SCIFB);
3765 }
hscif_early_console_setup(struct earlycon_device * device,const char * opt)3766 static int __init hscif_early_console_setup(struct earlycon_device *device,
3767 const char *opt)
3768 {
3769 return early_console_setup(device, PORT_HSCIF);
3770 }
3771
3772 OF_EARLYCON_DECLARE(sci, "renesas,sci", sci_early_console_setup);
3773 OF_EARLYCON_DECLARE(scif, "renesas,scif", scif_early_console_setup);
3774 OF_EARLYCON_DECLARE(scif, "renesas,scif-r7s9210", rzscifa_early_console_setup);
3775 OF_EARLYCON_DECLARE(scif, "renesas,scif-r9a07g044", rzscifa_early_console_setup);
3776 OF_EARLYCON_DECLARE(scif, "renesas,scif-r9a09g057", rzv2hscif_early_console_setup);
3777 OF_EARLYCON_DECLARE(scifa, "renesas,scifa", scifa_early_console_setup);
3778 OF_EARLYCON_DECLARE(scifb, "renesas,scifb", scifb_early_console_setup);
3779 OF_EARLYCON_DECLARE(hscif, "renesas,hscif", hscif_early_console_setup);
3780 #endif /* CONFIG_SERIAL_SH_SCI_EARLYCON */
3781
3782 module_init(sci_init);
3783 module_exit(sci_exit);
3784
3785 MODULE_LICENSE("GPL");
3786 MODULE_ALIAS("platform:sh-sci");
3787 MODULE_AUTHOR("Paul Mundt");
3788 MODULE_DESCRIPTION("SuperH (H)SCI(F) serial driver");
3789