Merge branch 'stable-3.2' into pandora-3.2
[pandora-kernel.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  *  Modified to support SH7720 SCIF. Markus Brunner, Mark Jonas (Jul 2007).
6  *
7  * based off of the old drivers/char/sh-sci.c by:
8  *
9  *   Copyright (C) 1999, 2000  Niibe Yutaka
10  *   Copyright (C) 2000  Sugioka Toshinobu
11  *   Modified to support multiple serial ports. Stuart Menefy (May 2000).
12  *   Modified to support SecureEdge. David McCullough (2002)
13  *   Modified to support SH7300 SCIF. Takashi Kusuda (Jun 2003).
14  *   Removed SH7300 support (Jul 2007).
15  *
16  * This file is subject to the terms and conditions of the GNU General Public
17  * License.  See the file "COPYING" in the main directory of this archive
18  * for more details.
19  */
20 #if defined(CONFIG_SERIAL_SH_SCI_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
21 #define SUPPORT_SYSRQ
22 #endif
23
24 #undef DEBUG
25
26 #include <linux/module.h>
27 #include <linux/errno.h>
28 #include <linux/timer.h>
29 #include <linux/interrupt.h>
30 #include <linux/tty.h>
31 #include <linux/tty_flip.h>
32 #include <linux/serial.h>
33 #include <linux/major.h>
34 #include <linux/string.h>
35 #include <linux/sysrq.h>
36 #include <linux/ioport.h>
37 #include <linux/mm.h>
38 #include <linux/init.h>
39 #include <linux/delay.h>
40 #include <linux/console.h>
41 #include <linux/platform_device.h>
42 #include <linux/serial_sci.h>
43 #include <linux/pm_runtime.h>
44 #include <linux/cpufreq.h>
45 #include <linux/clk.h>
46 #include <linux/ctype.h>
47 #include <linux/err.h>
48 #include <linux/dmaengine.h>
49 #include <linux/dma-mapping.h>
50 #include <linux/scatterlist.h>
51 #include <linux/slab.h>
52
53 #ifdef CONFIG_SUPERH
54 #include <asm/sh_bios.h>
55 #endif
56
57 #include "sh-sci.h"
58
59 struct sci_port {
60         struct uart_port        port;
61
62         /* Platform configuration */
63         struct plat_sci_port    *cfg;
64
65         /* Break timer */
66         struct timer_list       break_timer;
67         int                     break_flag;
68
69         /* Interface clock */
70         struct clk              *iclk;
71         /* Function clock */
72         struct clk              *fclk;
73
74         char                    *irqstr[SCIx_NR_IRQS];
75
76         struct dma_chan                 *chan_tx;
77         struct dma_chan                 *chan_rx;
78
79 #ifdef CONFIG_SERIAL_SH_SCI_DMA
80         struct dma_async_tx_descriptor  *desc_tx;
81         struct dma_async_tx_descriptor  *desc_rx[2];
82         dma_cookie_t                    cookie_tx;
83         dma_cookie_t                    cookie_rx[2];
84         dma_cookie_t                    active_rx;
85         struct scatterlist              sg_tx;
86         unsigned int                    sg_len_tx;
87         struct scatterlist              sg_rx[2];
88         size_t                          buf_len_rx;
89         struct sh_dmae_slave            param_tx;
90         struct sh_dmae_slave            param_rx;
91         struct work_struct              work_tx;
92         struct work_struct              work_rx;
93         struct timer_list               rx_timer;
94         unsigned int                    rx_timeout;
95 #endif
96
97 #ifdef CONFIG_SERIAL_SH_SCI_CONSOLE
98         unsigned short saved_smr;
99         unsigned short saved_fcr;
100         unsigned char saved_brr;
101 #endif
102 };
103
104 /* Function prototypes */
105 static void sci_start_tx(struct uart_port *port);
106 static void sci_stop_tx(struct uart_port *port);
107 static void sci_start_rx(struct uart_port *port);
108
109 #define SCI_NPORTS CONFIG_SERIAL_SH_SCI_NR_UARTS
110
111 static struct sci_port sci_ports[SCI_NPORTS];
112 static struct uart_driver sci_uart_driver;
113
114 static inline struct sci_port *
115 to_sci_port(struct uart_port *uart)
116 {
117         return container_of(uart, struct sci_port, port);
118 }
119
120 struct plat_sci_reg {
121         u8 offset, size;
122 };
123
124 /* Helper for invalidating specific entries of an inherited map. */
125 #define sci_reg_invalid { .offset = 0, .size = 0 }
126
127 static struct plat_sci_reg sci_regmap[SCIx_NR_REGTYPES][SCIx_NR_REGS] = {
128         [SCIx_PROBE_REGTYPE] = {
129                 [0 ... SCIx_NR_REGS - 1] = sci_reg_invalid,
130         },
131
132         /*
133          * Common SCI definitions, dependent on the port's regshift
134          * value.
135          */
136         [SCIx_SCI_REGTYPE] = {
137                 [SCSMR]         = { 0x00,  8 },
138                 [SCBRR]         = { 0x01,  8 },
139                 [SCSCR]         = { 0x02,  8 },
140                 [SCxTDR]        = { 0x03,  8 },
141                 [SCxSR]         = { 0x04,  8 },
142                 [SCxRDR]        = { 0x05,  8 },
143                 [SCFCR]         = sci_reg_invalid,
144                 [SCFDR]         = sci_reg_invalid,
145                 [SCTFDR]        = sci_reg_invalid,
146                 [SCRFDR]        = sci_reg_invalid,
147                 [SCSPTR]        = sci_reg_invalid,
148                 [SCLSR]         = sci_reg_invalid,
149         },
150
151         /*
152          * Common definitions for legacy IrDA ports, dependent on
153          * regshift value.
154          */
155         [SCIx_IRDA_REGTYPE] = {
156                 [SCSMR]         = { 0x00,  8 },
157                 [SCBRR]         = { 0x01,  8 },
158                 [SCSCR]         = { 0x02,  8 },
159                 [SCxTDR]        = { 0x03,  8 },
160                 [SCxSR]         = { 0x04,  8 },
161                 [SCxRDR]        = { 0x05,  8 },
162                 [SCFCR]         = { 0x06,  8 },
163                 [SCFDR]         = { 0x07, 16 },
164                 [SCTFDR]        = sci_reg_invalid,
165                 [SCRFDR]        = sci_reg_invalid,
166                 [SCSPTR]        = sci_reg_invalid,
167                 [SCLSR]         = sci_reg_invalid,
168         },
169
170         /*
171          * Common SCIFA definitions.
172          */
173         [SCIx_SCIFA_REGTYPE] = {
174                 [SCSMR]         = { 0x00, 16 },
175                 [SCBRR]         = { 0x04,  8 },
176                 [SCSCR]         = { 0x08, 16 },
177                 [SCxTDR]        = { 0x20,  8 },
178                 [SCxSR]         = { 0x14, 16 },
179                 [SCxRDR]        = { 0x24,  8 },
180                 [SCFCR]         = { 0x18, 16 },
181                 [SCFDR]         = { 0x1c, 16 },
182                 [SCTFDR]        = sci_reg_invalid,
183                 [SCRFDR]        = sci_reg_invalid,
184                 [SCSPTR]        = sci_reg_invalid,
185                 [SCLSR]         = sci_reg_invalid,
186         },
187
188         /*
189          * Common SCIFB definitions.
190          */
191         [SCIx_SCIFB_REGTYPE] = {
192                 [SCSMR]         = { 0x00, 16 },
193                 [SCBRR]         = { 0x04,  8 },
194                 [SCSCR]         = { 0x08, 16 },
195                 [SCxTDR]        = { 0x40,  8 },
196                 [SCxSR]         = { 0x14, 16 },
197                 [SCxRDR]        = { 0x60,  8 },
198                 [SCFCR]         = { 0x18, 16 },
199                 [SCFDR]         = { 0x1c, 16 },
200                 [SCTFDR]        = sci_reg_invalid,
201                 [SCRFDR]        = sci_reg_invalid,
202                 [SCSPTR]        = sci_reg_invalid,
203                 [SCLSR]         = sci_reg_invalid,
204         },
205
206         /*
207          * Common SH-2(A) SCIF definitions for ports with FIFO data
208          * count registers.
209          */
210         [SCIx_SH2_SCIF_FIFODATA_REGTYPE] = {
211                 [SCSMR]         = { 0x00, 16 },
212                 [SCBRR]         = { 0x04,  8 },
213                 [SCSCR]         = { 0x08, 16 },
214                 [SCxTDR]        = { 0x0c,  8 },
215                 [SCxSR]         = { 0x10, 16 },
216                 [SCxRDR]        = { 0x14,  8 },
217                 [SCFCR]         = { 0x18, 16 },
218                 [SCFDR]         = { 0x1c, 16 },
219                 [SCTFDR]        = sci_reg_invalid,
220                 [SCRFDR]        = sci_reg_invalid,
221                 [SCSPTR]        = { 0x20, 16 },
222                 [SCLSR]         = { 0x24, 16 },
223         },
224
225         /*
226          * Common SH-3 SCIF definitions.
227          */
228         [SCIx_SH3_SCIF_REGTYPE] = {
229                 [SCSMR]         = { 0x00,  8 },
230                 [SCBRR]         = { 0x02,  8 },
231                 [SCSCR]         = { 0x04,  8 },
232                 [SCxTDR]        = { 0x06,  8 },
233                 [SCxSR]         = { 0x08, 16 },
234                 [SCxRDR]        = { 0x0a,  8 },
235                 [SCFCR]         = { 0x0c,  8 },
236                 [SCFDR]         = { 0x0e, 16 },
237                 [SCTFDR]        = sci_reg_invalid,
238                 [SCRFDR]        = sci_reg_invalid,
239                 [SCSPTR]        = sci_reg_invalid,
240                 [SCLSR]         = sci_reg_invalid,
241         },
242
243         /*
244          * Common SH-4(A) SCIF(B) definitions.
245          */
246         [SCIx_SH4_SCIF_REGTYPE] = {
247                 [SCSMR]         = { 0x00, 16 },
248                 [SCBRR]         = { 0x04,  8 },
249                 [SCSCR]         = { 0x08, 16 },
250                 [SCxTDR]        = { 0x0c,  8 },
251                 [SCxSR]         = { 0x10, 16 },
252                 [SCxRDR]        = { 0x14,  8 },
253                 [SCFCR]         = { 0x18, 16 },
254                 [SCFDR]         = { 0x1c, 16 },
255                 [SCTFDR]        = sci_reg_invalid,
256                 [SCRFDR]        = sci_reg_invalid,
257                 [SCSPTR]        = { 0x20, 16 },
258                 [SCLSR]         = { 0x24, 16 },
259         },
260
261         /*
262          * Common SH-4(A) SCIF(B) definitions for ports without an SCSPTR
263          * register.
264          */
265         [SCIx_SH4_SCIF_NO_SCSPTR_REGTYPE] = {
266                 [SCSMR]         = { 0x00, 16 },
267                 [SCBRR]         = { 0x04,  8 },
268                 [SCSCR]         = { 0x08, 16 },
269                 [SCxTDR]        = { 0x0c,  8 },
270                 [SCxSR]         = { 0x10, 16 },
271                 [SCxRDR]        = { 0x14,  8 },
272                 [SCFCR]         = { 0x18, 16 },
273                 [SCFDR]         = { 0x1c, 16 },
274                 [SCTFDR]        = sci_reg_invalid,
275                 [SCRFDR]        = sci_reg_invalid,
276                 [SCSPTR]        = sci_reg_invalid,
277                 [SCLSR]         = { 0x24, 16 },
278         },
279
280         /*
281          * Common SH-4(A) SCIF(B) definitions for ports with FIFO data
282          * count registers.
283          */
284         [SCIx_SH4_SCIF_FIFODATA_REGTYPE] = {
285                 [SCSMR]         = { 0x00, 16 },
286                 [SCBRR]         = { 0x04,  8 },
287                 [SCSCR]         = { 0x08, 16 },
288                 [SCxTDR]        = { 0x0c,  8 },
289                 [SCxSR]         = { 0x10, 16 },
290                 [SCxRDR]        = { 0x14,  8 },
291                 [SCFCR]         = { 0x18, 16 },
292                 [SCFDR]         = { 0x1c, 16 },
293                 [SCTFDR]        = { 0x1c, 16 }, /* aliased to SCFDR */
294                 [SCRFDR]        = { 0x20, 16 },
295                 [SCSPTR]        = { 0x24, 16 },
296                 [SCLSR]         = { 0x28, 16 },
297         },
298
299         /*
300          * SH7705-style SCIF(B) ports, lacking both SCSPTR and SCLSR
301          * registers.
302          */
303         [SCIx_SH7705_SCIF_REGTYPE] = {
304                 [SCSMR]         = { 0x00, 16 },
305                 [SCBRR]         = { 0x04,  8 },
306                 [SCSCR]         = { 0x08, 16 },
307                 [SCxTDR]        = { 0x20,  8 },
308                 [SCxSR]         = { 0x14, 16 },
309                 [SCxRDR]        = { 0x24,  8 },
310                 [SCFCR]         = { 0x18, 16 },
311                 [SCFDR]         = { 0x1c, 16 },
312                 [SCTFDR]        = sci_reg_invalid,
313                 [SCRFDR]        = sci_reg_invalid,
314                 [SCSPTR]        = sci_reg_invalid,
315                 [SCLSR]         = sci_reg_invalid,
316         },
317 };
318
319 #define sci_getreg(up, offset)          (sci_regmap[to_sci_port(up)->cfg->regtype] + offset)
320
321 /*
322  * The "offset" here is rather misleading, in that it refers to an enum
323  * value relative to the port mapping rather than the fixed offset
324  * itself, which needs to be manually retrieved from the platform's
325  * register map for the given port.
326  */
327 static unsigned int sci_serial_in(struct uart_port *p, int offset)
328 {
329         struct plat_sci_reg *reg = sci_getreg(p, offset);
330
331         if (reg->size == 8)
332                 return ioread8(p->membase + (reg->offset << p->regshift));
333         else if (reg->size == 16)
334                 return ioread16(p->membase + (reg->offset << p->regshift));
335         else
336                 WARN(1, "Invalid register access\n");
337
338         return 0;
339 }
340
341 static void sci_serial_out(struct uart_port *p, int offset, int value)
342 {
343         struct plat_sci_reg *reg = sci_getreg(p, offset);
344
345         if (reg->size == 8)
346                 iowrite8(value, p->membase + (reg->offset << p->regshift));
347         else if (reg->size == 16)
348                 iowrite16(value, p->membase + (reg->offset << p->regshift));
349         else
350                 WARN(1, "Invalid register access\n");
351 }
352
353 #define sci_in(up, offset)              (up->serial_in(up, offset))
354 #define sci_out(up, offset, value)      (up->serial_out(up, offset, value))
355
356 static int sci_probe_regmap(struct plat_sci_port *cfg)
357 {
358         switch (cfg->type) {
359         case PORT_SCI:
360                 cfg->regtype = SCIx_SCI_REGTYPE;
361                 break;
362         case PORT_IRDA:
363                 cfg->regtype = SCIx_IRDA_REGTYPE;
364                 break;
365         case PORT_SCIFA:
366                 cfg->regtype = SCIx_SCIFA_REGTYPE;
367                 break;
368         case PORT_SCIFB:
369                 cfg->regtype = SCIx_SCIFB_REGTYPE;
370                 break;
371         case PORT_SCIF:
372                 /*
373                  * The SH-4 is a bit of a misnomer here, although that's
374                  * where this particular port layout originated. This
375                  * configuration (or some slight variation thereof)
376                  * remains the dominant model for all SCIFs.
377                  */
378                 cfg->regtype = SCIx_SH4_SCIF_REGTYPE;
379                 break;
380         default:
381                 printk(KERN_ERR "Can't probe register map for given port\n");
382                 return -EINVAL;
383         }
384
385         return 0;
386 }
387
388 static void sci_port_enable(struct sci_port *sci_port)
389 {
390         if (!sci_port->port.dev)
391                 return;
392
393         pm_runtime_get_sync(sci_port->port.dev);
394
395         clk_enable(sci_port->iclk);
396         sci_port->port.uartclk = clk_get_rate(sci_port->iclk);
397         clk_enable(sci_port->fclk);
398 }
399
400 static void sci_port_disable(struct sci_port *sci_port)
401 {
402         if (!sci_port->port.dev)
403                 return;
404
405         clk_disable(sci_port->fclk);
406         clk_disable(sci_port->iclk);
407
408         pm_runtime_put_sync(sci_port->port.dev);
409 }
410
411 #if defined(CONFIG_CONSOLE_POLL) || defined(CONFIG_SERIAL_SH_SCI_CONSOLE)
412
413 #ifdef CONFIG_CONSOLE_POLL
414 static int sci_poll_get_char(struct uart_port *port)
415 {
416         unsigned short status;
417         int c;
418
419         do {
420                 status = sci_in(port, SCxSR);
421                 if (status & SCxSR_ERRORS(port)) {
422                         sci_out(port, SCxSR, SCxSR_ERROR_CLEAR(port));
423                         continue;
424                 }
425                 break;
426         } while (1);
427
428         if (!(status & SCxSR_RDxF(port)))
429                 return NO_POLL_CHAR;
430
431         c = sci_in(port, SCxRDR);
432
433         /* Dummy read */
434         sci_in(port, SCxSR);
435         sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
436
437         return c;
438 }
439 #endif
440
441 static void sci_poll_put_char(struct uart_port *port, unsigned char c)
442 {
443         unsigned short status;
444
445         do {
446                 status = sci_in(port, SCxSR);
447         } while (!(status & SCxSR_TDxE(port)));
448
449         sci_out(port, SCxTDR, c);
450         sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port) & ~SCxSR_TEND(port));
451 }
452 #endif /* CONFIG_CONSOLE_POLL || CONFIG_SERIAL_SH_SCI_CONSOLE */
453
454 static void sci_init_pins(struct uart_port *port, unsigned int cflag)
455 {
456         struct sci_port *s = to_sci_port(port);
457         struct plat_sci_reg *reg = sci_regmap[s->cfg->regtype] + SCSPTR;
458
459         /*
460          * Use port-specific handler if provided.
461          */
462         if (s->cfg->ops && s->cfg->ops->init_pins) {
463                 s->cfg->ops->init_pins(port, cflag);
464                 return;
465         }
466
467         /*
468          * For the generic path SCSPTR is necessary. Bail out if that's
469          * unavailable, too.
470          */
471         if (!reg->size)
472                 return;
473
474         if (!(cflag & CRTSCTS))
475                 sci_out(port, SCSPTR, 0x0080); /* Set RTS = 1 */
476 }
477
478 static int sci_txfill(struct uart_port *port)
479 {
480         struct plat_sci_reg *reg;
481
482         reg = sci_getreg(port, SCTFDR);
483         if (reg->size)
484                 return sci_in(port, SCTFDR) & 0xff;
485
486         reg = sci_getreg(port, SCFDR);
487         if (reg->size)
488                 return sci_in(port, SCFDR) >> 8;
489
490         return !(sci_in(port, SCxSR) & SCI_TDRE);
491 }
492
493 static int sci_txroom(struct uart_port *port)
494 {
495         return port->fifosize - sci_txfill(port);
496 }
497
498 static int sci_rxfill(struct uart_port *port)
499 {
500         struct plat_sci_reg *reg;
501
502         reg = sci_getreg(port, SCRFDR);
503         if (reg->size)
504                 return sci_in(port, SCRFDR) & 0xff;
505
506         reg = sci_getreg(port, SCFDR);
507         if (reg->size)
508                 return sci_in(port, SCFDR) & ((port->fifosize << 1) - 1);
509
510         return (sci_in(port, SCxSR) & SCxSR_RDxF(port)) != 0;
511 }
512
513 /*
514  * SCI helper for checking the state of the muxed port/RXD pins.
515  */
516 static inline int sci_rxd_in(struct uart_port *port)
517 {
518         struct sci_port *s = to_sci_port(port);
519
520         if (s->cfg->port_reg <= 0)
521                 return 1;
522
523         return !!__raw_readb(s->cfg->port_reg);
524 }
525
526 /* ********************************************************************** *
527  *                   the interrupt related routines                       *
528  * ********************************************************************** */
529
530 static void sci_transmit_chars(struct uart_port *port)
531 {
532         struct circ_buf *xmit = &port->state->xmit;
533         unsigned int stopped = uart_tx_stopped(port);
534         unsigned short status;
535         unsigned short ctrl;
536         int count;
537
538         status = sci_in(port, SCxSR);
539         if (!(status & SCxSR_TDxE(port))) {
540                 ctrl = sci_in(port, SCSCR);
541                 if (uart_circ_empty(xmit))
542                         ctrl &= ~SCSCR_TIE;
543                 else
544                         ctrl |= SCSCR_TIE;
545                 sci_out(port, SCSCR, ctrl);
546                 return;
547         }
548
549         count = sci_txroom(port);
550
551         do {
552                 unsigned char c;
553
554                 if (port->x_char) {
555                         c = port->x_char;
556                         port->x_char = 0;
557                 } else if (!uart_circ_empty(xmit) && !stopped) {
558                         c = xmit->buf[xmit->tail];
559                         xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
560                 } else {
561                         break;
562                 }
563
564                 sci_out(port, SCxTDR, c);
565
566                 port->icount.tx++;
567         } while (--count > 0);
568
569         sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port));
570
571         if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
572                 uart_write_wakeup(port);
573         if (uart_circ_empty(xmit)) {
574                 sci_stop_tx(port);
575         } else {
576                 ctrl = sci_in(port, SCSCR);
577
578                 if (port->type != PORT_SCI) {
579                         sci_in(port, SCxSR); /* Dummy read */
580                         sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port));
581                 }
582
583                 ctrl |= SCSCR_TIE;
584                 sci_out(port, SCSCR, ctrl);
585         }
586 }
587
588 /* On SH3, SCIF may read end-of-break as a space->mark char */
589 #define STEPFN(c)  ({int __c = (c); (((__c-1)|(__c)) == -1); })
590
591 static void sci_receive_chars(struct uart_port *port)
592 {
593         struct sci_port *sci_port = to_sci_port(port);
594         struct tty_struct *tty = port->state->port.tty;
595         int i, count, copied = 0;
596         unsigned short status;
597         unsigned char flag;
598
599         status = sci_in(port, SCxSR);
600         if (!(status & SCxSR_RDxF(port)))
601                 return;
602
603         while (1) {
604                 /* Don't copy more bytes than there is room for in the buffer */
605                 count = tty_buffer_request_room(tty, sci_rxfill(port));
606
607                 /* If for any reason we can't copy more data, we're done! */
608                 if (count == 0)
609                         break;
610
611                 if (port->type == PORT_SCI) {
612                         char c = sci_in(port, SCxRDR);
613                         if (uart_handle_sysrq_char(port, c) ||
614                             sci_port->break_flag)
615                                 count = 0;
616                         else
617                                 tty_insert_flip_char(tty, c, TTY_NORMAL);
618                 } else {
619                         for (i = 0; i < count; i++) {
620                                 char c = sci_in(port, SCxRDR);
621                                 status = sci_in(port, SCxSR);
622 #if defined(CONFIG_CPU_SH3)
623                                 /* Skip "chars" during break */
624                                 if (sci_port->break_flag) {
625                                         if ((c == 0) &&
626                                             (status & SCxSR_FER(port))) {
627                                                 count--; i--;
628                                                 continue;
629                                         }
630
631                                         /* Nonzero => end-of-break */
632                                         dev_dbg(port->dev, "debounce<%02x>\n", c);
633                                         sci_port->break_flag = 0;
634
635                                         if (STEPFN(c)) {
636                                                 count--; i--;
637                                                 continue;
638                                         }
639                                 }
640 #endif /* CONFIG_CPU_SH3 */
641                                 if (uart_handle_sysrq_char(port, c)) {
642                                         count--; i--;
643                                         continue;
644                                 }
645
646                                 /* Store data and status */
647                                 if (status & SCxSR_FER(port)) {
648                                         flag = TTY_FRAME;
649                                         dev_notice(port->dev, "frame error\n");
650                                 } else if (status & SCxSR_PER(port)) {
651                                         flag = TTY_PARITY;
652                                         dev_notice(port->dev, "parity error\n");
653                                 } else
654                                         flag = TTY_NORMAL;
655
656                                 tty_insert_flip_char(tty, c, flag);
657                         }
658                 }
659
660                 sci_in(port, SCxSR); /* dummy read */
661                 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
662
663                 copied += count;
664                 port->icount.rx += count;
665         }
666
667         if (copied) {
668                 /* Tell the rest of the system the news. New characters! */
669                 tty_flip_buffer_push(tty);
670         } else {
671                 sci_in(port, SCxSR); /* dummy read */
672                 sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
673         }
674 }
675
676 #define SCI_BREAK_JIFFIES (HZ/20)
677
678 /*
679  * The sci generates interrupts during the break,
680  * 1 per millisecond or so during the break period, for 9600 baud.
681  * So dont bother disabling interrupts.
682  * But dont want more than 1 break event.
683  * Use a kernel timer to periodically poll the rx line until
684  * the break is finished.
685  */
686 static inline void sci_schedule_break_timer(struct sci_port *port)
687 {
688         mod_timer(&port->break_timer, jiffies + SCI_BREAK_JIFFIES);
689 }
690
691 /* Ensure that two consecutive samples find the break over. */
692 static void sci_break_timer(unsigned long data)
693 {
694         struct sci_port *port = (struct sci_port *)data;
695
696         sci_port_enable(port);
697
698         if (sci_rxd_in(&port->port) == 0) {
699                 port->break_flag = 1;
700                 sci_schedule_break_timer(port);
701         } else if (port->break_flag == 1) {
702                 /* break is over. */
703                 port->break_flag = 2;
704                 sci_schedule_break_timer(port);
705         } else
706                 port->break_flag = 0;
707
708         sci_port_disable(port);
709 }
710
711 static int sci_handle_errors(struct uart_port *port)
712 {
713         int copied = 0;
714         unsigned short status = sci_in(port, SCxSR);
715         struct tty_struct *tty = port->state->port.tty;
716         struct sci_port *s = to_sci_port(port);
717
718         /*
719          * Handle overruns, if supported.
720          */
721         if (s->cfg->overrun_bit != SCIx_NOT_SUPPORTED) {
722                 if (status & (1 << s->cfg->overrun_bit)) {
723                         /* overrun error */
724                         if (tty_insert_flip_char(tty, 0, TTY_OVERRUN))
725                                 copied++;
726
727                         dev_notice(port->dev, "overrun error");
728                 }
729         }
730
731         if (status & SCxSR_FER(port)) {
732                 if (sci_rxd_in(port) == 0) {
733                         /* Notify of BREAK */
734                         struct sci_port *sci_port = to_sci_port(port);
735
736                         if (!sci_port->break_flag) {
737                                 sci_port->break_flag = 1;
738                                 sci_schedule_break_timer(sci_port);
739
740                                 /* Do sysrq handling. */
741                                 if (uart_handle_break(port))
742                                         return 0;
743
744                                 dev_dbg(port->dev, "BREAK detected\n");
745
746                                 if (tty_insert_flip_char(tty, 0, TTY_BREAK))
747                                         copied++;
748                         }
749
750                 } else {
751                         /* frame error */
752                         if (tty_insert_flip_char(tty, 0, TTY_FRAME))
753                                 copied++;
754
755                         dev_notice(port->dev, "frame error\n");
756                 }
757         }
758
759         if (status & SCxSR_PER(port)) {
760                 /* parity error */
761                 if (tty_insert_flip_char(tty, 0, TTY_PARITY))
762                         copied++;
763
764                 dev_notice(port->dev, "parity error");
765         }
766
767         if (copied)
768                 tty_flip_buffer_push(tty);
769
770         return copied;
771 }
772
773 static int sci_handle_fifo_overrun(struct uart_port *port)
774 {
775         struct tty_struct *tty = port->state->port.tty;
776         struct sci_port *s = to_sci_port(port);
777         struct plat_sci_reg *reg;
778         int copied = 0;
779
780         reg = sci_getreg(port, SCLSR);
781         if (!reg->size)
782                 return 0;
783
784         if ((sci_in(port, SCLSR) & (1 << s->cfg->overrun_bit))) {
785                 sci_out(port, SCLSR, 0);
786
787                 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
788                 tty_flip_buffer_push(tty);
789
790                 dev_notice(port->dev, "overrun error\n");
791                 copied++;
792         }
793
794         return copied;
795 }
796
797 static int sci_handle_breaks(struct uart_port *port)
798 {
799         int copied = 0;
800         unsigned short status = sci_in(port, SCxSR);
801         struct tty_struct *tty = port->state->port.tty;
802         struct sci_port *s = to_sci_port(port);
803
804         if (uart_handle_break(port))
805                 return 0;
806
807         if (!s->break_flag && status & SCxSR_BRK(port)) {
808 #if defined(CONFIG_CPU_SH3)
809                 /* Debounce break */
810                 s->break_flag = 1;
811 #endif
812                 /* Notify of BREAK */
813                 if (tty_insert_flip_char(tty, 0, TTY_BREAK))
814                         copied++;
815
816                 dev_dbg(port->dev, "BREAK detected\n");
817         }
818
819         if (copied)
820                 tty_flip_buffer_push(tty);
821
822         copied += sci_handle_fifo_overrun(port);
823
824         return copied;
825 }
826
827 static irqreturn_t sci_rx_interrupt(int irq, void *ptr)
828 {
829 #ifdef CONFIG_SERIAL_SH_SCI_DMA
830         struct uart_port *port = ptr;
831         struct sci_port *s = to_sci_port(port);
832
833         if (s->chan_rx) {
834                 u16 scr = sci_in(port, SCSCR);
835                 u16 ssr = sci_in(port, SCxSR);
836
837                 /* Disable future Rx interrupts */
838                 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB) {
839                         disable_irq_nosync(irq);
840                         scr |= 0x4000;
841                 } else {
842                         scr &= ~SCSCR_RIE;
843                 }
844                 sci_out(port, SCSCR, scr);
845                 /* Clear current interrupt */
846                 sci_out(port, SCxSR, ssr & ~(1 | SCxSR_RDxF(port)));
847                 dev_dbg(port->dev, "Rx IRQ %lu: setup t-out in %u jiffies\n",
848                         jiffies, s->rx_timeout);
849                 mod_timer(&s->rx_timer, jiffies + s->rx_timeout);
850
851                 return IRQ_HANDLED;
852         }
853 #endif
854
855         /* I think sci_receive_chars has to be called irrespective
856          * of whether the I_IXOFF is set, otherwise, how is the interrupt
857          * to be disabled?
858          */
859         sci_receive_chars(ptr);
860
861         return IRQ_HANDLED;
862 }
863
864 static irqreturn_t sci_tx_interrupt(int irq, void *ptr)
865 {
866         struct uart_port *port = ptr;
867         unsigned long flags;
868
869         spin_lock_irqsave(&port->lock, flags);
870         sci_transmit_chars(port);
871         spin_unlock_irqrestore(&port->lock, flags);
872
873         return IRQ_HANDLED;
874 }
875
876 static irqreturn_t sci_er_interrupt(int irq, void *ptr)
877 {
878         struct uart_port *port = ptr;
879
880         /* Handle errors */
881         if (port->type == PORT_SCI) {
882                 if (sci_handle_errors(port)) {
883                         /* discard character in rx buffer */
884                         sci_in(port, SCxSR);
885                         sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));
886                 }
887         } else {
888                 sci_handle_fifo_overrun(port);
889                 sci_rx_interrupt(irq, ptr);
890         }
891
892         sci_out(port, SCxSR, SCxSR_ERROR_CLEAR(port));
893
894         /* Kick the transmission */
895         sci_tx_interrupt(irq, ptr);
896
897         return IRQ_HANDLED;
898 }
899
900 static irqreturn_t sci_br_interrupt(int irq, void *ptr)
901 {
902         struct uart_port *port = ptr;
903
904         /* Handle BREAKs */
905         sci_handle_breaks(port);
906         sci_out(port, SCxSR, SCxSR_BREAK_CLEAR(port));
907
908         return IRQ_HANDLED;
909 }
910
911 static inline unsigned long port_rx_irq_mask(struct uart_port *port)
912 {
913         /*
914          * Not all ports (such as SCIFA) will support REIE. Rather than
915          * special-casing the port type, we check the port initialization
916          * IRQ enable mask to see whether the IRQ is desired at all. If
917          * it's unset, it's logically inferred that there's no point in
918          * testing for it.
919          */
920         return SCSCR_RIE | (to_sci_port(port)->cfg->scscr & SCSCR_REIE);
921 }
922
923 static irqreturn_t sci_mpxed_interrupt(int irq, void *ptr)
924 {
925         unsigned short ssr_status, scr_status, err_enabled;
926         struct uart_port *port = ptr;
927         struct sci_port *s = to_sci_port(port);
928         irqreturn_t ret = IRQ_NONE;
929
930         ssr_status = sci_in(port, SCxSR);
931         scr_status = sci_in(port, SCSCR);
932         err_enabled = scr_status & port_rx_irq_mask(port);
933
934         /* Tx Interrupt */
935         if ((ssr_status & SCxSR_TDxE(port)) && (scr_status & SCSCR_TIE) &&
936             !s->chan_tx)
937                 ret = sci_tx_interrupt(irq, ptr);
938
939         /*
940          * Rx Interrupt: if we're using DMA, the DMA controller clears RDF /
941          * DR flags
942          */
943         if (((ssr_status & SCxSR_RDxF(port)) || s->chan_rx) &&
944             (scr_status & SCSCR_RIE))
945                 ret = sci_rx_interrupt(irq, ptr);
946
947         /* Error Interrupt */
948         if ((ssr_status & SCxSR_ERRORS(port)) && err_enabled)
949                 ret = sci_er_interrupt(irq, ptr);
950
951         /* Break Interrupt */
952         if ((ssr_status & SCxSR_BRK(port)) && err_enabled)
953                 ret = sci_br_interrupt(irq, ptr);
954
955         return ret;
956 }
957
958 static struct sci_irq_desc {
959         const char      *desc;
960         irq_handler_t   handler;
961 } sci_irq_desc[] = {
962         /*
963          * Split out handlers, the default case.
964          */
965         [SCIx_ERI_IRQ] = {
966                 .desc = "rx err",
967                 .handler = sci_er_interrupt,
968         },
969
970         [SCIx_RXI_IRQ] = {
971                 .desc = "rx full",
972                 .handler = sci_rx_interrupt,
973         },
974
975         [SCIx_TXI_IRQ] = {
976                 .desc = "tx empty",
977                 .handler = sci_tx_interrupt,
978         },
979
980         [SCIx_BRI_IRQ] = {
981                 .desc = "break",
982                 .handler = sci_br_interrupt,
983         },
984
985         /*
986          * Special muxed handler.
987          */
988         [SCIx_MUX_IRQ] = {
989                 .desc = "mux",
990                 .handler = sci_mpxed_interrupt,
991         },
992 };
993
994 static int sci_request_irq(struct sci_port *port)
995 {
996         struct uart_port *up = &port->port;
997         int i, j, ret = 0;
998
999         for (i = j = 0; i < SCIx_NR_IRQS; i++, j++) {
1000                 struct sci_irq_desc *desc;
1001                 unsigned int irq;
1002
1003                 if (SCIx_IRQ_IS_MUXED(port)) {
1004                         i = SCIx_MUX_IRQ;
1005                         irq = up->irq;
1006                 } else
1007                         irq = port->cfg->irqs[i];
1008
1009                 desc = sci_irq_desc + i;
1010                 port->irqstr[j] = kasprintf(GFP_KERNEL, "%s:%s",
1011                                             dev_name(up->dev), desc->desc);
1012                 if (!port->irqstr[j]) {
1013                         dev_err(up->dev, "Failed to allocate %s IRQ string\n",
1014                                 desc->desc);
1015                         goto out_nomem;
1016                 }
1017
1018                 ret = request_irq(irq, desc->handler, up->irqflags,
1019                                   port->irqstr[j], port);
1020                 if (unlikely(ret)) {
1021                         dev_err(up->dev, "Can't allocate %s IRQ\n", desc->desc);
1022                         goto out_noirq;
1023                 }
1024         }
1025
1026         return 0;
1027
1028 out_noirq:
1029         while (--i >= 0)
1030                 free_irq(port->cfg->irqs[i], port);
1031
1032 out_nomem:
1033         while (--j >= 0)
1034                 kfree(port->irqstr[j]);
1035
1036         return ret;
1037 }
1038
1039 static void sci_free_irq(struct sci_port *port)
1040 {
1041         int i;
1042
1043         /*
1044          * Intentionally in reverse order so we iterate over the muxed
1045          * IRQ first.
1046          */
1047         for (i = 0; i < SCIx_NR_IRQS; i++) {
1048                 free_irq(port->cfg->irqs[i], port);
1049                 kfree(port->irqstr[i]);
1050
1051                 if (SCIx_IRQ_IS_MUXED(port)) {
1052                         /* If there's only one IRQ, we're done. */
1053                         return;
1054                 }
1055         }
1056 }
1057
1058 static unsigned int sci_tx_empty(struct uart_port *port)
1059 {
1060         unsigned short status = sci_in(port, SCxSR);
1061         unsigned short in_tx_fifo = sci_txfill(port);
1062
1063         return (status & SCxSR_TEND(port)) && !in_tx_fifo ? TIOCSER_TEMT : 0;
1064 }
1065
1066 static void sci_set_mctrl(struct uart_port *port, unsigned int mctrl)
1067 {
1068         /* This routine is used for seting signals of: DTR, DCD, CTS/RTS */
1069         /* We use SCIF's hardware for CTS/RTS, so don't need any for that. */
1070         /* If you have signals for DTR and DCD, please implement here. */
1071 }
1072
1073 static unsigned int sci_get_mctrl(struct uart_port *port)
1074 {
1075         /* This routine is used for getting signals of: DTR, DCD, DSR, RI,
1076            and CTS/RTS */
1077
1078         return TIOCM_DTR | TIOCM_RTS | TIOCM_CTS | TIOCM_DSR;
1079 }
1080
1081 #ifdef CONFIG_SERIAL_SH_SCI_DMA
1082 static void sci_dma_tx_complete(void *arg)
1083 {
1084         struct sci_port *s = arg;
1085         struct uart_port *port = &s->port;
1086         struct circ_buf *xmit = &port->state->xmit;
1087         unsigned long flags;
1088
1089         dev_dbg(port->dev, "%s(%d)\n", __func__, port->line);
1090
1091         spin_lock_irqsave(&port->lock, flags);
1092
1093         xmit->tail += sg_dma_len(&s->sg_tx);
1094         xmit->tail &= UART_XMIT_SIZE - 1;
1095
1096         port->icount.tx += sg_dma_len(&s->sg_tx);
1097
1098         async_tx_ack(s->desc_tx);
1099         s->desc_tx = NULL;
1100
1101         if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
1102                 uart_write_wakeup(port);
1103
1104         if (!uart_circ_empty(xmit)) {
1105                 s->cookie_tx = 0;
1106                 schedule_work(&s->work_tx);
1107         } else {
1108                 s->cookie_tx = -EINVAL;
1109                 if (port->type == PORT_SCIFA || port->type == PORT_SCIFB) {
1110                         u16 ctrl = sci_in(port, SCSCR);
1111                         sci_out(port, SCSCR, ctrl & ~SCSCR_TIE);
1112                 }
1113         }
1114
1115         spin_unlock_irqrestore(&port->lock, flags);
1116 }
1117
1118 /* Locking: called with port lock held */
1119 static int sci_dma_rx_push(struct sci_port *s, struct tty_struct *tty,
1120                            size_t count)
1121 {
1122         struct uart_port *port = &s->port;
1123         int i, active, room;
1124
1125         room = tty_buffer_request_room(tty, count);
1126
1127         if (s->active_rx == s->cookie_rx[0]) {
1128                 active = 0;
1129         } else if (s->active_rx == s->cookie_rx[1]) {
1130                 active = 1;
1131         } else {
1132                 dev_err(port->dev, "cookie %d not found!\n", s->active_rx);
1133                 return 0;
1134         }
1135
1136         if (room < count)
1137                 dev_warn(port->dev, "Rx overrun: dropping %u bytes\n",
1138                          count - room);
1139         if (!room)
1140                 return room;
1141
1142         for (i = 0; i < room; i++)
1143                 tty_insert_flip_char(tty, ((u8 *)sg_virt(&s->sg_rx[active]))[i],
1144                                      TTY_NORMAL);
1145
1146         port->icount.rx += room;
1147
1148         return room;
1149 }
1150
1151 static void sci_dma_rx_complete(void *arg)
1152 {
1153         struct sci_port *s = arg;
1154         struct uart_port *port = &s->port;
1155         struct tty_struct *tty = port->state->port.tty;
1156         unsigned long flags;
1157         int count;
1158
1159         dev_dbg(port->dev, "%s(%d) active #%d\n", __func__, port->line, s->active_rx);
1160
1161         spin_lock_irqsave(&port->lock, flags);
1162
1163         count = sci_dma_rx_push(s, tty, s->buf_len_rx);
1164
1165         mod_timer(&s->rx_timer, jiffies + s->rx_timeout);
1166
1167         spin_unlock_irqrestore(&port->lock, flags);
1168
1169         if (count)
1170                 tty_flip_buffer_push(tty);
1171
1172         schedule_work(&s->work_rx);
1173 }
1174
1175 static void sci_rx_dma_release(struct sci_port *s, bool enable_pio)
1176 {
1177         struct dma_chan *chan = s->chan_rx;
1178         struct uart_port *port = &s->port;
1179
1180         s->chan_rx = NULL;
1181         s->cookie_rx[0] = s->cookie_rx[1] = -EINVAL;
1182         dma_release_channel(chan);
1183         if (sg_dma_address(&s->sg_rx[0]))
1184                 dma_free_coherent(port->dev, s->buf_len_rx * 2,
1185                                   sg_virt(&s->sg_rx[0]), sg_dma_address(&s->sg_rx[0]));
1186         if (enable_pio)
1187                 sci_start_rx(port);
1188 }
1189
1190 static void sci_tx_dma_release(struct sci_port *s, bool enable_pio)
1191 {
1192         struct dma_chan *chan = s->chan_tx;
1193         struct uart_port *port = &s->port;
1194
1195         s->chan_tx = NULL;
1196         s->cookie_tx = -EINVAL;
1197         dma_release_channel(chan);
1198         if (enable_pio)
1199                 sci_start_tx(port);
1200 }
1201
1202 static void sci_submit_rx(struct sci_port *s)
1203 {
1204         struct dma_chan *chan = s->chan_rx;
1205         int i;
1206
1207         for (i = 0; i < 2; i++) {
1208                 struct scatterlist *sg = &s->sg_rx[i];
1209                 struct dma_async_tx_descriptor *desc;
1210
1211                 desc = chan->device->device_prep_slave_sg(chan,
1212                         sg, 1, DMA_FROM_DEVICE, DMA_PREP_INTERRUPT);
1213
1214                 if (desc) {
1215                         s->desc_rx[i] = desc;
1216                         desc->callback = sci_dma_rx_complete;
1217                         desc->callback_param = s;
1218                         s->cookie_rx[i] = desc->tx_submit(desc);
1219                 }
1220
1221                 if (!desc || s->cookie_rx[i] < 0) {
1222                         if (i) {
1223                                 async_tx_ack(s->desc_rx[0]);
1224                                 s->cookie_rx[0] = -EINVAL;
1225                         }
1226                         if (desc) {
1227                                 async_tx_ack(desc);
1228                                 s->cookie_rx[i] = -EINVAL;
1229                         }
1230                         dev_warn(s->port.dev,
1231                                  "failed to re-start DMA, using PIO\n");
1232                         sci_rx_dma_release(s, true);
1233                         return;
1234                 }
1235                 dev_dbg(s->port.dev, "%s(): cookie %d to #%d\n", __func__,
1236                         s->cookie_rx[i], i);
1237         }
1238
1239         s->active_rx = s->cookie_rx[0];
1240
1241         dma_async_issue_pending(chan);
1242 }
1243
1244 static void work_fn_rx(struct work_struct *work)
1245 {
1246         struct sci_port *s = container_of(work, struct sci_port, work_rx);
1247         struct uart_port *port = &s->port;
1248         struct dma_async_tx_descriptor *desc;
1249         int new;
1250
1251         if (s->active_rx == s->cookie_rx[0]) {
1252                 new = 0;
1253         } else if (s->active_rx == s->cookie_rx[1]) {
1254                 new = 1;
1255         } else {
1256                 dev_err(port->dev, "cookie %d not found!\n", s->active_rx);
1257                 return;
1258         }
1259         desc = s->desc_rx[new];
1260
1261         if (dma_async_is_tx_complete(s->chan_rx, s->active_rx, NULL, NULL) !=
1262             DMA_SUCCESS) {
1263                 /* Handle incomplete DMA receive */
1264                 struct tty_struct *tty = port->state->port.tty;
1265                 struct dma_chan *chan = s->chan_rx;
1266                 struct sh_desc *sh_desc = container_of(desc, struct sh_desc,
1267                                                        async_tx);
1268                 unsigned long flags;
1269                 int count;
1270
1271                 chan->device->device_control(chan, DMA_TERMINATE_ALL, 0);
1272                 dev_dbg(port->dev, "Read %u bytes with cookie %d\n",
1273                         sh_desc->partial, sh_desc->cookie);
1274
1275                 spin_lock_irqsave(&port->lock, flags);
1276                 count = sci_dma_rx_push(s, tty, sh_desc->partial);
1277                 spin_unlock_irqrestore(&port->lock, flags);
1278
1279                 if (count)
1280                         tty_flip_buffer_push(tty);
1281
1282                 sci_submit_rx(s);
1283
1284                 return;
1285         }
1286
1287         s->cookie_rx[new] = desc->tx_submit(desc);
1288         if (s->cookie_rx[new] < 0) {
1289                 dev_warn(port->dev, "Failed submitting Rx DMA descriptor\n");
1290                 sci_rx_dma_release(s, true);
1291                 return;
1292         }
1293
1294         s->active_rx = s->cookie_rx[!new];
1295
1296         dev_dbg(port->dev, "%s: cookie %d #%d, new active #%d\n", __func__,
1297                 s->cookie_rx[new], new, s->active_rx);
1298 }
1299
1300 static void work_fn_tx(struct work_struct *work)
1301 {
1302         struct sci_port *s = container_of(work, struct sci_port, work_tx);
1303         struct dma_async_tx_descriptor *desc;
1304         struct dma_chan *chan = s->chan_tx;
1305         struct uart_port *port = &s->port;
1306         struct circ_buf *xmit = &port->state->xmit;
1307         struct scatterlist *sg = &s->sg_tx;
1308
1309         /*
1310          * DMA is idle now.
1311          * Port xmit buffer is already mapped, and it is one page... Just adjust
1312          * offsets and lengths. Since it is a circular buffer, we have to
1313          * transmit till the end, and then the rest. Take the port lock to get a
1314          * consistent xmit buffer state.
1315          */
1316         spin_lock_irq(&port->lock);
1317         sg->offset = xmit->tail & (UART_XMIT_SIZE - 1);
1318         sg_dma_address(sg) = (sg_dma_address(sg) & ~(UART_XMIT_SIZE - 1)) +
1319                 sg->offset;
1320         sg_dma_len(sg) = min((int)CIRC_CNT(xmit->head, xmit->tail, UART_XMIT_SIZE),
1321                 CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE));
1322         spin_unlock_irq(&port->lock);
1323
1324         BUG_ON(!sg_dma_len(sg));
1325
1326         desc = chan->device->device_prep_slave_sg(chan,
1327                         sg, s->sg_len_tx, DMA_TO_DEVICE,
1328                         DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
1329         if (!desc) {
1330                 /* switch to PIO */
1331                 sci_tx_dma_release(s, true);
1332                 return;
1333         }
1334
1335         dma_sync_sg_for_device(port->dev, sg, 1, DMA_TO_DEVICE);
1336
1337         spin_lock_irq(&port->lock);
1338         s->desc_tx = desc;
1339         desc->callback = sci_dma_tx_complete;
1340         desc->callback_param = s;
1341         spin_unlock_irq(&port->lock);
1342         s->cookie_tx = desc->tx_submit(desc);
1343         if (s->cookie_tx < 0) {
1344                 dev_warn(port->dev, "Failed submitting Tx DMA descriptor\n");
1345                 /* switch to PIO */
1346                 sci_tx_dma_release(s, true);
1347                 return;
1348         }
1349
1350         dev_dbg(port->dev, "%s: %p: %d...%d, cookie %d\n", __func__,
1351                 xmit->buf, xmit->tail, xmit->head, s->cookie_tx);
1352
1353         dma_async_issue_pending(chan);
1354 }
1355 #endif
1356
1357 static void sci_start_tx(struct uart_port *port)
1358 {
1359         struct sci_port *s = to_sci_port(port);
1360         unsigned short ctrl;
1361
1362 #ifdef CONFIG_SERIAL_SH_SCI_DMA
1363         if (port->type == PORT_SCIFA || port->type == PORT_SCIFB) {
1364                 u16 new, scr = sci_in(port, SCSCR);
1365                 if (s->chan_tx)
1366                         new = scr | 0x8000;
1367                 else
1368                         new = scr & ~0x8000;
1369                 if (new != scr)
1370                         sci_out(port, SCSCR, new);
1371         }
1372
1373         if (s->chan_tx && !uart_circ_empty(&s->port.state->xmit) &&
1374             s->cookie_tx < 0) {
1375                 s->cookie_tx = 0;
1376                 schedule_work(&s->work_tx);
1377         }
1378 #endif
1379
1380         if (!s->chan_tx || port->type == PORT_SCIFA || port->type == PORT_SCIFB) {
1381                 /* Set TIE (Transmit Interrupt Enable) bit in SCSCR */
1382                 ctrl = sci_in(port, SCSCR);
1383                 sci_out(port, SCSCR, ctrl | SCSCR_TIE);
1384         }
1385 }
1386
1387 static void sci_stop_tx(struct uart_port *port)
1388 {
1389         unsigned short ctrl;
1390
1391         /* Clear TIE (Transmit Interrupt Enable) bit in SCSCR */
1392         ctrl = sci_in(port, SCSCR);
1393
1394         if (port->type == PORT_SCIFA || port->type == PORT_SCIFB)
1395                 ctrl &= ~0x8000;
1396
1397         ctrl &= ~SCSCR_TIE;
1398
1399         sci_out(port, SCSCR, ctrl);
1400 }
1401
1402 static void sci_start_rx(struct uart_port *port)
1403 {
1404         unsigned short ctrl;
1405
1406         ctrl = sci_in(port, SCSCR) | port_rx_irq_mask(port);
1407
1408         if (port->type == PORT_SCIFA || port->type == PORT_SCIFB)
1409                 ctrl &= ~0x4000;
1410
1411         sci_out(port, SCSCR, ctrl);
1412 }
1413
1414 static void sci_stop_rx(struct uart_port *port)
1415 {
1416         unsigned short ctrl;
1417
1418         ctrl = sci_in(port, SCSCR);
1419
1420         if (port->type == PORT_SCIFA || port->type == PORT_SCIFB)
1421                 ctrl &= ~0x4000;
1422
1423         ctrl &= ~port_rx_irq_mask(port);
1424
1425         sci_out(port, SCSCR, ctrl);
1426 }
1427
1428 static void sci_enable_ms(struct uart_port *port)
1429 {
1430         /* Nothing here yet .. */
1431 }
1432
1433 static void sci_break_ctl(struct uart_port *port, int break_state)
1434 {
1435         /* Nothing here yet .. */
1436 }
1437
1438 #ifdef CONFIG_SERIAL_SH_SCI_DMA
1439 static bool filter(struct dma_chan *chan, void *slave)
1440 {
1441         struct sh_dmae_slave *param = slave;
1442
1443         dev_dbg(chan->device->dev, "%s: slave ID %d\n", __func__,
1444                 param->slave_id);
1445
1446         chan->private = param;
1447         return true;
1448 }
1449
1450 static void rx_timer_fn(unsigned long arg)
1451 {
1452         struct sci_port *s = (struct sci_port *)arg;
1453         struct uart_port *port = &s->port;
1454         u16 scr = sci_in(port, SCSCR);
1455
1456         if (port->type == PORT_SCIFA || port->type == PORT_SCIFB) {
1457                 scr &= ~0x4000;
1458                 enable_irq(s->cfg->irqs[1]);
1459         }
1460         sci_out(port, SCSCR, scr | SCSCR_RIE);
1461         dev_dbg(port->dev, "DMA Rx timed out\n");
1462         schedule_work(&s->work_rx);
1463 }
1464
1465 static void sci_request_dma(struct uart_port *port)
1466 {
1467         struct sci_port *s = to_sci_port(port);
1468         struct sh_dmae_slave *param;
1469         struct dma_chan *chan;
1470         dma_cap_mask_t mask;
1471         int nent;
1472
1473         dev_dbg(port->dev, "%s: port %d\n", __func__,
1474                 port->line);
1475
1476         if (s->cfg->dma_slave_tx <= 0 || s->cfg->dma_slave_rx <= 0)
1477                 return;
1478
1479         dma_cap_zero(mask);
1480         dma_cap_set(DMA_SLAVE, mask);
1481
1482         param = &s->param_tx;
1483
1484         /* Slave ID, e.g., SHDMA_SLAVE_SCIF0_TX */
1485         param->slave_id = s->cfg->dma_slave_tx;
1486
1487         s->cookie_tx = -EINVAL;
1488         chan = dma_request_channel(mask, filter, param);
1489         dev_dbg(port->dev, "%s: TX: got channel %p\n", __func__, chan);
1490         if (chan) {
1491                 s->chan_tx = chan;
1492                 sg_init_table(&s->sg_tx, 1);
1493                 /* UART circular tx buffer is an aligned page. */
1494                 BUG_ON((int)port->state->xmit.buf & ~PAGE_MASK);
1495                 sg_set_page(&s->sg_tx, virt_to_page(port->state->xmit.buf),
1496                             UART_XMIT_SIZE, (int)port->state->xmit.buf & ~PAGE_MASK);
1497                 nent = dma_map_sg(port->dev, &s->sg_tx, 1, DMA_TO_DEVICE);
1498                 if (!nent)
1499                         sci_tx_dma_release(s, false);
1500                 else
1501                         dev_dbg(port->dev, "%s: mapped %d@%p to %x\n", __func__,
1502                                 sg_dma_len(&s->sg_tx),
1503                                 port->state->xmit.buf, sg_dma_address(&s->sg_tx));
1504
1505                 s->sg_len_tx = nent;
1506
1507                 INIT_WORK(&s->work_tx, work_fn_tx);
1508         }
1509
1510         param = &s->param_rx;
1511
1512         /* Slave ID, e.g., SHDMA_SLAVE_SCIF0_RX */
1513         param->slave_id = s->cfg->dma_slave_rx;
1514
1515         chan = dma_request_channel(mask, filter, param);
1516         dev_dbg(port->dev, "%s: RX: got channel %p\n", __func__, chan);
1517         if (chan) {
1518                 dma_addr_t dma[2];
1519                 void *buf[2];
1520                 int i;
1521
1522                 s->chan_rx = chan;
1523
1524                 s->buf_len_rx = 2 * max(16, (int)port->fifosize);
1525                 buf[0] = dma_alloc_coherent(port->dev, s->buf_len_rx * 2,
1526                                             &dma[0], GFP_KERNEL);
1527
1528                 if (!buf[0]) {
1529                         dev_warn(port->dev,
1530                                  "failed to allocate dma buffer, using PIO\n");
1531                         sci_rx_dma_release(s, true);
1532                         return;
1533                 }
1534
1535                 buf[1] = buf[0] + s->buf_len_rx;
1536                 dma[1] = dma[0] + s->buf_len_rx;
1537
1538                 for (i = 0; i < 2; i++) {
1539                         struct scatterlist *sg = &s->sg_rx[i];
1540
1541                         sg_init_table(sg, 1);
1542                         sg_set_page(sg, virt_to_page(buf[i]), s->buf_len_rx,
1543                                     (int)buf[i] & ~PAGE_MASK);
1544                         sg_dma_address(sg) = dma[i];
1545                 }
1546
1547                 INIT_WORK(&s->work_rx, work_fn_rx);
1548                 setup_timer(&s->rx_timer, rx_timer_fn, (unsigned long)s);
1549
1550                 sci_submit_rx(s);
1551         }
1552 }
1553
1554 static void sci_free_dma(struct uart_port *port)
1555 {
1556         struct sci_port *s = to_sci_port(port);
1557
1558         if (s->chan_tx)
1559                 sci_tx_dma_release(s, false);
1560         if (s->chan_rx)
1561                 sci_rx_dma_release(s, false);
1562 }
1563 #else
1564 static inline void sci_request_dma(struct uart_port *port)
1565 {
1566 }
1567
1568 static inline void sci_free_dma(struct uart_port *port)
1569 {
1570 }
1571 #endif
1572
1573 static int sci_startup(struct uart_port *port)
1574 {
1575         struct sci_port *s = to_sci_port(port);
1576         int ret;
1577
1578         dev_dbg(port->dev, "%s(%d)\n", __func__, port->line);
1579
1580         sci_port_enable(s);
1581
1582         ret = sci_request_irq(s);
1583         if (unlikely(ret < 0))
1584                 return ret;
1585
1586         sci_request_dma(port);
1587
1588         sci_start_tx(port);
1589         sci_start_rx(port);
1590
1591         return 0;
1592 }
1593
1594 static void sci_shutdown(struct uart_port *port)
1595 {
1596         struct sci_port *s = to_sci_port(port);
1597
1598         dev_dbg(port->dev, "%s(%d)\n", __func__, port->line);
1599
1600         sci_stop_rx(port);
1601         sci_stop_tx(port);
1602
1603         sci_free_dma(port);
1604         sci_free_irq(s);
1605
1606         sci_port_disable(s);
1607 }
1608
1609 static unsigned int sci_scbrr_calc(unsigned int algo_id, unsigned int bps,
1610                                    unsigned long freq)
1611 {
1612         switch (algo_id) {
1613         case SCBRR_ALGO_1:
1614                 return ((freq + 16 * bps) / (16 * bps) - 1);
1615         case SCBRR_ALGO_2:
1616                 return ((freq + 16 * bps) / (32 * bps) - 1);
1617         case SCBRR_ALGO_3:
1618                 return (((freq * 2) + 16 * bps) / (16 * bps) - 1);
1619         case SCBRR_ALGO_4:
1620                 return (((freq * 2) + 16 * bps) / (32 * bps) - 1);
1621         case SCBRR_ALGO_5:
1622                 return (((freq * 1000 / 32) / bps) - 1);
1623         }
1624
1625         /* Warn, but use a safe default */
1626         WARN_ON(1);
1627
1628         return ((freq + 16 * bps) / (32 * bps) - 1);
1629 }
1630
1631 static void sci_reset(struct uart_port *port)
1632 {
1633         unsigned int status;
1634
1635         do {
1636                 status = sci_in(port, SCxSR);
1637         } while (!(status & SCxSR_TEND(port)));
1638
1639         sci_out(port, SCSCR, 0x00);     /* TE=0, RE=0, CKE1=0 */
1640
1641         if (port->type != PORT_SCI)
1642                 sci_out(port, SCFCR, SCFCR_RFRST | SCFCR_TFRST);
1643 }
1644
1645 static void sci_set_termios(struct uart_port *port, struct ktermios *termios,
1646                             struct ktermios *old)
1647 {
1648         struct sci_port *s = to_sci_port(port);
1649         unsigned int baud, smr_val, max_baud;
1650         int t = -1;
1651         u16 scfcr = 0;
1652
1653         /*
1654          * earlyprintk comes here early on with port->uartclk set to zero.
1655          * the clock framework is not up and running at this point so here
1656          * we assume that 115200 is the maximum baud rate. please note that
1657          * the baud rate is not programmed during earlyprintk - it is assumed
1658          * that the previous boot loader has enabled required clocks and
1659          * setup the baud rate generator hardware for us already.
1660          */
1661         max_baud = port->uartclk ? port->uartclk / 16 : 115200;
1662
1663         baud = uart_get_baud_rate(port, termios, old, 0, max_baud);
1664         if (likely(baud && port->uartclk))
1665                 t = sci_scbrr_calc(s->cfg->scbrr_algo_id, baud, port->uartclk);
1666
1667         sci_port_enable(s);
1668
1669         sci_reset(port);
1670
1671         smr_val = sci_in(port, SCSMR) & 3;
1672
1673         if ((termios->c_cflag & CSIZE) == CS7)
1674                 smr_val |= 0x40;
1675         if (termios->c_cflag & PARENB)
1676                 smr_val |= 0x20;
1677         if (termios->c_cflag & PARODD)
1678                 smr_val |= 0x30;
1679         if (termios->c_cflag & CSTOPB)
1680                 smr_val |= 0x08;
1681
1682         uart_update_timeout(port, termios->c_cflag, baud);
1683
1684         sci_out(port, SCSMR, smr_val);
1685
1686         dev_dbg(port->dev, "%s: SMR %x, t %x, SCSCR %x\n", __func__, smr_val, t,
1687                 s->cfg->scscr);
1688
1689         if (t > 0) {
1690                 if (t >= 256) {
1691                         sci_out(port, SCSMR, (sci_in(port, SCSMR) & ~3) | 1);
1692                         t >>= 2;
1693                 } else
1694                         sci_out(port, SCSMR, sci_in(port, SCSMR) & ~3);
1695
1696                 sci_out(port, SCBRR, t);
1697                 udelay((1000000+(baud-1)) / baud); /* Wait one bit interval */
1698         }
1699
1700         sci_init_pins(port, termios->c_cflag);
1701         sci_out(port, SCFCR, scfcr | ((termios->c_cflag & CRTSCTS) ? SCFCR_MCE : 0));
1702
1703         sci_out(port, SCSCR, s->cfg->scscr);
1704
1705 #ifdef CONFIG_SERIAL_SH_SCI_DMA
1706         /*
1707          * Calculate delay for 1.5 DMA buffers: see
1708          * drivers/serial/serial_core.c::uart_update_timeout(). With 10 bits
1709          * (CS8), 250Hz, 115200 baud and 64 bytes FIFO, the above function
1710          * calculates 1 jiffie for the data plus 5 jiffies for the "slop(e)."
1711          * Then below we calculate 3 jiffies (12ms) for 1.5 DMA buffers (3 FIFO
1712          * sizes), but it has been found out experimentally, that this is not
1713          * enough: the driver too often needlessly runs on a DMA timeout. 20ms
1714          * as a minimum seem to work perfectly.
1715          */
1716         if (s->chan_rx) {
1717                 s->rx_timeout = (port->timeout - HZ / 50) * s->buf_len_rx * 3 /
1718                         port->fifosize / 2;
1719                 dev_dbg(port->dev,
1720                         "DMA Rx t-out %ums, tty t-out %u jiffies\n",
1721                         s->rx_timeout * 1000 / HZ, port->timeout);
1722                 if (s->rx_timeout < msecs_to_jiffies(20))
1723                         s->rx_timeout = msecs_to_jiffies(20);
1724         }
1725 #endif
1726
1727         if ((termios->c_cflag & CREAD) != 0)
1728                 sci_start_rx(port);
1729
1730         sci_port_disable(s);
1731 }
1732
1733 static const char *sci_type(struct uart_port *port)
1734 {
1735         switch (port->type) {
1736         case PORT_IRDA:
1737                 return "irda";
1738         case PORT_SCI:
1739                 return "sci";
1740         case PORT_SCIF:
1741                 return "scif";
1742         case PORT_SCIFA:
1743                 return "scifa";
1744         case PORT_SCIFB:
1745                 return "scifb";
1746         }
1747
1748         return NULL;
1749 }
1750
1751 static inline unsigned long sci_port_size(struct uart_port *port)
1752 {
1753         /*
1754          * Pick an arbitrary size that encapsulates all of the base
1755          * registers by default. This can be optimized later, or derived
1756          * from platform resource data at such a time that ports begin to
1757          * behave more erratically.
1758          */
1759         return 64;
1760 }
1761
1762 static int sci_remap_port(struct uart_port *port)
1763 {
1764         unsigned long size = sci_port_size(port);
1765
1766         /*
1767          * Nothing to do if there's already an established membase.
1768          */
1769         if (port->membase)
1770                 return 0;
1771
1772         if (port->flags & UPF_IOREMAP) {
1773                 port->membase = ioremap_nocache(port->mapbase, size);
1774                 if (unlikely(!port->membase)) {
1775                         dev_err(port->dev, "can't remap port#%d\n", port->line);
1776                         return -ENXIO;
1777                 }
1778         } else {
1779                 /*
1780                  * For the simple (and majority of) cases where we don't
1781                  * need to do any remapping, just cast the cookie
1782                  * directly.
1783                  */
1784                 port->membase = (void __iomem *)port->mapbase;
1785         }
1786
1787         return 0;
1788 }
1789
1790 static void sci_release_port(struct uart_port *port)
1791 {
1792         if (port->flags & UPF_IOREMAP) {
1793                 iounmap(port->membase);
1794                 port->membase = NULL;
1795         }
1796
1797         release_mem_region(port->mapbase, sci_port_size(port));
1798 }
1799
1800 static int sci_request_port(struct uart_port *port)
1801 {
1802         unsigned long size = sci_port_size(port);
1803         struct resource *res;
1804         int ret;
1805
1806         res = request_mem_region(port->mapbase, size, dev_name(port->dev));
1807         if (unlikely(res == NULL))
1808                 return -EBUSY;
1809
1810         ret = sci_remap_port(port);
1811         if (unlikely(ret != 0)) {
1812                 release_resource(res);
1813                 return ret;
1814         }
1815
1816         return 0;
1817 }
1818
1819 static void sci_config_port(struct uart_port *port, int flags)
1820 {
1821         if (flags & UART_CONFIG_TYPE) {
1822                 struct sci_port *sport = to_sci_port(port);
1823
1824                 port->type = sport->cfg->type;
1825                 sci_request_port(port);
1826         }
1827 }
1828
1829 static int sci_verify_port(struct uart_port *port, struct serial_struct *ser)
1830 {
1831         struct sci_port *s = to_sci_port(port);
1832
1833         if (ser->irq != s->cfg->irqs[SCIx_TXI_IRQ] || ser->irq > nr_irqs)
1834                 return -EINVAL;
1835         if (ser->baud_base < 2400)
1836                 /* No paper tape reader for Mitch.. */
1837                 return -EINVAL;
1838
1839         return 0;
1840 }
1841
1842 static struct uart_ops sci_uart_ops = {
1843         .tx_empty       = sci_tx_empty,
1844         .set_mctrl      = sci_set_mctrl,
1845         .get_mctrl      = sci_get_mctrl,
1846         .start_tx       = sci_start_tx,
1847         .stop_tx        = sci_stop_tx,
1848         .stop_rx        = sci_stop_rx,
1849         .enable_ms      = sci_enable_ms,
1850         .break_ctl      = sci_break_ctl,
1851         .startup        = sci_startup,
1852         .shutdown       = sci_shutdown,
1853         .set_termios    = sci_set_termios,
1854         .type           = sci_type,
1855         .release_port   = sci_release_port,
1856         .request_port   = sci_request_port,
1857         .config_port    = sci_config_port,
1858         .verify_port    = sci_verify_port,
1859 #ifdef CONFIG_CONSOLE_POLL
1860         .poll_get_char  = sci_poll_get_char,
1861         .poll_put_char  = sci_poll_put_char,
1862 #endif
1863 };
1864
1865 static int __devinit sci_init_single(struct platform_device *dev,
1866                                      struct sci_port *sci_port,
1867                                      unsigned int index,
1868                                      struct plat_sci_port *p)
1869 {
1870         struct uart_port *port = &sci_port->port;
1871         int ret;
1872
1873         port->ops       = &sci_uart_ops;
1874         port->iotype    = UPIO_MEM;
1875         port->line      = index;
1876
1877         switch (p->type) {
1878         case PORT_SCIFB:
1879                 port->fifosize = 256;
1880                 break;
1881         case PORT_SCIFA:
1882                 port->fifosize = 64;
1883                 break;
1884         case PORT_SCIF:
1885                 port->fifosize = 16;
1886                 break;
1887         default:
1888                 port->fifosize = 1;
1889                 break;
1890         }
1891
1892         if (p->regtype == SCIx_PROBE_REGTYPE) {
1893                 ret = sci_probe_regmap(p);
1894                 if (unlikely(ret))
1895                         return ret;
1896         }
1897
1898         if (dev) {
1899                 sci_port->iclk = clk_get(&dev->dev, "sci_ick");
1900                 if (IS_ERR(sci_port->iclk)) {
1901                         sci_port->iclk = clk_get(&dev->dev, "peripheral_clk");
1902                         if (IS_ERR(sci_port->iclk)) {
1903                                 dev_err(&dev->dev, "can't get iclk\n");
1904                                 return PTR_ERR(sci_port->iclk);
1905                         }
1906                 }
1907
1908                 /*
1909                  * The function clock is optional, ignore it if we can't
1910                  * find it.
1911                  */
1912                 sci_port->fclk = clk_get(&dev->dev, "sci_fck");
1913                 if (IS_ERR(sci_port->fclk))
1914                         sci_port->fclk = NULL;
1915
1916                 port->dev = &dev->dev;
1917
1918                 pm_runtime_irq_safe(&dev->dev);
1919                 pm_runtime_enable(&dev->dev);
1920         }
1921
1922         sci_port->break_timer.data = (unsigned long)sci_port;
1923         sci_port->break_timer.function = sci_break_timer;
1924         init_timer(&sci_port->break_timer);
1925
1926         /*
1927          * Establish some sensible defaults for the error detection.
1928          */
1929         if (!p->error_mask)
1930                 p->error_mask = (p->type == PORT_SCI) ?
1931                         SCI_DEFAULT_ERROR_MASK : SCIF_DEFAULT_ERROR_MASK;
1932
1933         /*
1934          * Establish sensible defaults for the overrun detection, unless
1935          * the part has explicitly disabled support for it.
1936          */
1937         if (p->overrun_bit != SCIx_NOT_SUPPORTED) {
1938                 if (p->type == PORT_SCI)
1939                         p->overrun_bit = 5;
1940                 else if (p->scbrr_algo_id == SCBRR_ALGO_4)
1941                         p->overrun_bit = 9;
1942                 else
1943                         p->overrun_bit = 0;
1944
1945                 /*
1946                  * Make the error mask inclusive of overrun detection, if
1947                  * supported.
1948                  */
1949                 p->error_mask |= (1 << p->overrun_bit);
1950         }
1951
1952         sci_port->cfg           = p;
1953
1954         port->mapbase           = p->mapbase;
1955         port->type              = p->type;
1956         port->flags             = p->flags;
1957         port->regshift          = p->regshift;
1958
1959         /*
1960          * The UART port needs an IRQ value, so we peg this to the RX IRQ
1961          * for the multi-IRQ ports, which is where we are primarily
1962          * concerned with the shutdown path synchronization.
1963          *
1964          * For the muxed case there's nothing more to do.
1965          */
1966         port->irq               = p->irqs[SCIx_RXI_IRQ];
1967         port->irqflags          = 0;
1968
1969         port->serial_in         = sci_serial_in;
1970         port->serial_out        = sci_serial_out;
1971
1972         if (p->dma_slave_tx > 0 && p->dma_slave_rx > 0)
1973                 dev_dbg(port->dev, "DMA tx %d, rx %d\n",
1974                         p->dma_slave_tx, p->dma_slave_rx);
1975
1976         return 0;
1977 }
1978
1979 #ifdef CONFIG_SERIAL_SH_SCI_CONSOLE
1980 static void serial_console_putchar(struct uart_port *port, int ch)
1981 {
1982         sci_poll_put_char(port, ch);
1983 }
1984
1985 /*
1986  *      Print a string to the serial port trying not to disturb
1987  *      any possible real use of the port...
1988  */
1989 static void serial_console_write(struct console *co, const char *s,
1990                                  unsigned count)
1991 {
1992         struct sci_port *sci_port = &sci_ports[co->index];
1993         struct uart_port *port = &sci_port->port;
1994         unsigned short bits;
1995
1996         sci_port_enable(sci_port);
1997
1998         uart_console_write(port, s, count, serial_console_putchar);
1999
2000         /* wait until fifo is empty and last bit has been transmitted */
2001         bits = SCxSR_TDxE(port) | SCxSR_TEND(port);
2002         while ((sci_in(port, SCxSR) & bits) != bits)
2003                 cpu_relax();
2004
2005         sci_port_disable(sci_port);
2006 }
2007
2008 static int __devinit serial_console_setup(struct console *co, char *options)
2009 {
2010         struct sci_port *sci_port;
2011         struct uart_port *port;
2012         int baud = 115200;
2013         int bits = 8;
2014         int parity = 'n';
2015         int flow = 'n';
2016         int ret;
2017
2018         /*
2019          * Refuse to handle any bogus ports.
2020          */
2021         if (co->index < 0 || co->index >= SCI_NPORTS)
2022                 return -ENODEV;
2023
2024         sci_port = &sci_ports[co->index];
2025         port = &sci_port->port;
2026
2027         /*
2028          * Refuse to handle uninitialized ports.
2029          */
2030         if (!port->ops)
2031                 return -ENODEV;
2032
2033         ret = sci_remap_port(port);
2034         if (unlikely(ret != 0))
2035                 return ret;
2036
2037         sci_port_enable(sci_port);
2038
2039         if (options)
2040                 uart_parse_options(options, &baud, &parity, &bits, &flow);
2041
2042         sci_port_disable(sci_port);
2043
2044         return uart_set_options(port, co, baud, parity, bits, flow);
2045 }
2046
2047 static struct console serial_console = {
2048         .name           = "ttySC",
2049         .device         = uart_console_device,
2050         .write          = serial_console_write,
2051         .setup          = serial_console_setup,
2052         .flags          = CON_PRINTBUFFER,
2053         .index          = -1,
2054         .data           = &sci_uart_driver,
2055 };
2056
2057 static struct console early_serial_console = {
2058         .name           = "early_ttySC",
2059         .write          = serial_console_write,
2060         .flags          = CON_PRINTBUFFER,
2061         .index          = -1,
2062 };
2063
2064 static char early_serial_buf[32];
2065
2066 static int __devinit sci_probe_earlyprintk(struct platform_device *pdev)
2067 {
2068         struct plat_sci_port *cfg = pdev->dev.platform_data;
2069
2070         if (early_serial_console.data)
2071                 return -EEXIST;
2072
2073         early_serial_console.index = pdev->id;
2074
2075         sci_init_single(NULL, &sci_ports[pdev->id], pdev->id, cfg);
2076
2077         serial_console_setup(&early_serial_console, early_serial_buf);
2078
2079         if (!strstr(early_serial_buf, "keep"))
2080                 early_serial_console.flags |= CON_BOOT;
2081
2082         register_console(&early_serial_console);
2083         return 0;
2084 }
2085
2086 #define uart_console(port)      ((port)->cons->index == (port)->line)
2087
2088 static int sci_runtime_suspend(struct device *dev)
2089 {
2090         struct sci_port *sci_port = dev_get_drvdata(dev);
2091         struct uart_port *port = &sci_port->port;
2092
2093         if (uart_console(port)) {
2094                 sci_port->saved_smr = sci_in(port, SCSMR);
2095                 sci_port->saved_brr = sci_in(port, SCBRR);
2096                 sci_port->saved_fcr = sci_in(port, SCFCR);
2097         }
2098         return 0;
2099 }
2100
2101 static int sci_runtime_resume(struct device *dev)
2102 {
2103         struct sci_port *sci_port = dev_get_drvdata(dev);
2104         struct uart_port *port = &sci_port->port;
2105
2106         if (uart_console(port)) {
2107                 sci_reset(port);
2108                 sci_out(port, SCSMR, sci_port->saved_smr);
2109                 sci_out(port, SCBRR, sci_port->saved_brr);
2110                 sci_out(port, SCFCR, sci_port->saved_fcr);
2111                 sci_out(port, SCSCR, sci_port->cfg->scscr);
2112         }
2113         return 0;
2114 }
2115
2116 #define SCI_CONSOLE     (&serial_console)
2117
2118 #else
2119 static inline int __devinit sci_probe_earlyprintk(struct platform_device *pdev)
2120 {
2121         return -EINVAL;
2122 }
2123
2124 #define SCI_CONSOLE     NULL
2125 #define sci_runtime_suspend     NULL
2126 #define sci_runtime_resume      NULL
2127
2128 #endif /* CONFIG_SERIAL_SH_SCI_CONSOLE */
2129
2130 static char banner[] __initdata =
2131         KERN_INFO "SuperH SCI(F) driver initialized\n";
2132
2133 static struct uart_driver sci_uart_driver = {
2134         .owner          = THIS_MODULE,
2135         .driver_name    = "sci",
2136         .dev_name       = "ttySC",
2137         .major          = SCI_MAJOR,
2138         .minor          = SCI_MINOR_START,
2139         .nr             = SCI_NPORTS,
2140         .cons           = SCI_CONSOLE,
2141 };
2142
2143 static int sci_remove(struct platform_device *dev)
2144 {
2145         struct sci_port *port = platform_get_drvdata(dev);
2146
2147         uart_remove_one_port(&sci_uart_driver, &port->port);
2148
2149         clk_put(port->iclk);
2150         clk_put(port->fclk);
2151
2152         pm_runtime_disable(&dev->dev);
2153         return 0;
2154 }
2155
2156 static int __devinit sci_probe_single(struct platform_device *dev,
2157                                       unsigned int index,
2158                                       struct plat_sci_port *p,
2159                                       struct sci_port *sciport)
2160 {
2161         int ret;
2162
2163         /* Sanity check */
2164         if (unlikely(index >= SCI_NPORTS)) {
2165                 dev_notice(&dev->dev, "Attempting to register port "
2166                            "%d when only %d are available.\n",
2167                            index+1, SCI_NPORTS);
2168                 dev_notice(&dev->dev, "Consider bumping "
2169                            "CONFIG_SERIAL_SH_SCI_NR_UARTS!\n");
2170                 return 0;
2171         }
2172
2173         ret = sci_init_single(dev, sciport, index, p);
2174         if (ret)
2175                 return ret;
2176
2177         return uart_add_one_port(&sci_uart_driver, &sciport->port);
2178 }
2179
2180 static int __devinit sci_probe(struct platform_device *dev)
2181 {
2182         struct plat_sci_port *p = dev->dev.platform_data;
2183         struct sci_port *sp = &sci_ports[dev->id];
2184         int ret;
2185
2186         /*
2187          * If we've come here via earlyprintk initialization, head off to
2188          * the special early probe. We don't have sufficient device state
2189          * to make it beyond this yet.
2190          */
2191         if (is_early_platform_device(dev))
2192                 return sci_probe_earlyprintk(dev);
2193
2194         platform_set_drvdata(dev, sp);
2195
2196         ret = sci_probe_single(dev, dev->id, p, sp);
2197         if (ret)
2198                 goto err_unreg;
2199
2200 #ifdef CONFIG_SH_STANDARD_BIOS
2201         sh_bios_gdb_detach();
2202 #endif
2203
2204         return 0;
2205
2206 err_unreg:
2207         sci_remove(dev);
2208         return ret;
2209 }
2210
2211 static int sci_suspend(struct device *dev)
2212 {
2213         struct sci_port *sport = dev_get_drvdata(dev);
2214
2215         if (sport)
2216                 uart_suspend_port(&sci_uart_driver, &sport->port);
2217
2218         return 0;
2219 }
2220
2221 static int sci_resume(struct device *dev)
2222 {
2223         struct sci_port *sport = dev_get_drvdata(dev);
2224
2225         if (sport)
2226                 uart_resume_port(&sci_uart_driver, &sport->port);
2227
2228         return 0;
2229 }
2230
2231 static const struct dev_pm_ops sci_dev_pm_ops = {
2232         .runtime_suspend = sci_runtime_suspend,
2233         .runtime_resume = sci_runtime_resume,
2234         .suspend        = sci_suspend,
2235         .resume         = sci_resume,
2236 };
2237
2238 static struct platform_driver sci_driver = {
2239         .probe          = sci_probe,
2240         .remove         = sci_remove,
2241         .driver         = {
2242                 .name   = "sh-sci",
2243                 .owner  = THIS_MODULE,
2244                 .pm     = &sci_dev_pm_ops,
2245         },
2246 };
2247
2248 static int __init sci_init(void)
2249 {
2250         int ret;
2251
2252         printk(banner);
2253
2254         ret = uart_register_driver(&sci_uart_driver);
2255         if (likely(ret == 0)) {
2256                 ret = platform_driver_register(&sci_driver);
2257                 if (unlikely(ret))
2258                         uart_unregister_driver(&sci_uart_driver);
2259         }
2260
2261         return ret;
2262 }
2263
2264 static void __exit sci_exit(void)
2265 {
2266         platform_driver_unregister(&sci_driver);
2267         uart_unregister_driver(&sci_uart_driver);
2268 }
2269
2270 #ifdef CONFIG_SERIAL_SH_SCI_CONSOLE
2271 early_platform_init_buffer("earlyprintk", &sci_driver,
2272                            early_serial_buf, ARRAY_SIZE(early_serial_buf));
2273 #endif
2274 module_init(sci_init);
2275 module_exit(sci_exit);
2276
2277 MODULE_LICENSE("GPL");
2278 MODULE_ALIAS("platform:sh-sci");
2279 MODULE_AUTHOR("Paul Mundt");
2280 MODULE_DESCRIPTION("SuperH SCI(F) serial driver");