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