TTY: switch tty_insert_flip_char
[firefly-linux-kernel-4.4.55.git] / drivers / tty / serial / m32r_sio.c
1 /*
2  *  m32r_sio.c
3  *
4  *  Driver for M32R serial ports
5  *
6  *  Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
7  *  Based on drivers/serial/8250.c.
8  *
9  *  Copyright (C) 2001  Russell King.
10  *  Copyright (C) 2004  Hirokazu Takata <takata at linux-m32r.org>
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  */
17
18 /*
19  * A note about mapbase / membase
20  *
21  *  mapbase is the physical address of the IO port.  Currently, we don't
22  *  support this very well, and it may well be dropped from this driver
23  *  in future.  As such, mapbase should be NULL.
24  *
25  *  membase is an 'ioremapped' cookie.  This is compatible with the old
26  *  serial.c driver, and is currently the preferred form.
27  */
28
29 #if defined(CONFIG_SERIAL_M32R_SIO_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
30 #define SUPPORT_SYSRQ
31 #endif
32
33 #include <linux/module.h>
34 #include <linux/tty.h>
35 #include <linux/tty_flip.h>
36 #include <linux/ioport.h>
37 #include <linux/init.h>
38 #include <linux/console.h>
39 #include <linux/sysrq.h>
40 #include <linux/serial.h>
41 #include <linux/delay.h>
42
43 #include <asm/m32r.h>
44 #include <asm/io.h>
45 #include <asm/irq.h>
46
47 #define BAUD_RATE       115200
48
49 #include <linux/serial_core.h>
50 #include "m32r_sio.h"
51 #include "m32r_sio_reg.h"
52
53 /*
54  * Debugging.
55  */
56 #if 0
57 #define DEBUG_AUTOCONF(fmt...)  printk(fmt)
58 #else
59 #define DEBUG_AUTOCONF(fmt...)  do { } while (0)
60 #endif
61
62 #if 0
63 #define DEBUG_INTR(fmt...)      printk(fmt)
64 #else
65 #define DEBUG_INTR(fmt...)      do { } while (0)
66 #endif
67
68 #define PASS_LIMIT      256
69
70 #define BASE_BAUD       115200
71
72 /* Standard COM flags */
73 #define STD_COM_FLAGS (UPF_BOOT_AUTOCONF | UPF_SKIP_TEST)
74
75 /*
76  * SERIAL_PORT_DFNS tells us about built-in ports that have no
77  * standard enumeration mechanism.   Platforms that can find all
78  * serial ports via mechanisms like ACPI or PCI need not supply it.
79  */
80 #if defined(CONFIG_PLAT_USRV)
81
82 #define SERIAL_PORT_DFNS                                                \
83        /* UART  CLK     PORT   IRQ            FLAGS */                  \
84         { 0, BASE_BAUD, 0x3F8, PLD_IRQ_UART0, STD_COM_FLAGS }, /* ttyS0 */ \
85         { 0, BASE_BAUD, 0x2F8, PLD_IRQ_UART1, STD_COM_FLAGS }, /* ttyS1 */
86
87 #else /* !CONFIG_PLAT_USRV */
88
89 #if defined(CONFIG_SERIAL_M32R_PLDSIO)
90 #define SERIAL_PORT_DFNS                                                \
91         { 0, BASE_BAUD, ((unsigned long)PLD_ESIO0CR), PLD_IRQ_SIO0_RCV, \
92           STD_COM_FLAGS }, /* ttyS0 */
93 #else
94 #define SERIAL_PORT_DFNS                                                \
95         { 0, BASE_BAUD, M32R_SIO_OFFSET, M32R_IRQ_SIO0_R,               \
96           STD_COM_FLAGS }, /* ttyS0 */
97 #endif
98
99 #endif /* !CONFIG_PLAT_USRV */
100
101 static struct old_serial_port old_serial_port[] = {
102         SERIAL_PORT_DFNS
103 };
104
105 #define UART_NR ARRAY_SIZE(old_serial_port)
106
107 struct uart_sio_port {
108         struct uart_port        port;
109         struct timer_list       timer;          /* "no irq" timer */
110         struct list_head        list;           /* ports on this IRQ */
111         unsigned short          rev;
112         unsigned char           acr;
113         unsigned char           ier;
114         unsigned char           lcr;
115         unsigned char           mcr_mask;       /* mask of user bits */
116         unsigned char           mcr_force;      /* mask of forced bits */
117         unsigned char           lsr_break_flag;
118
119         /*
120          * We provide a per-port pm hook.
121          */
122         void                    (*pm)(struct uart_port *port,
123                                       unsigned int state, unsigned int old);
124 };
125
126 struct irq_info {
127         spinlock_t              lock;
128         struct list_head        *head;
129 };
130
131 static struct irq_info irq_lists[NR_IRQS];
132
133 #ifdef CONFIG_SERIAL_M32R_PLDSIO
134
135 #define __sio_in(x) inw((unsigned long)(x))
136 #define __sio_out(v,x) outw((v),(unsigned long)(x))
137
138 static inline void sio_set_baud_rate(unsigned long baud)
139 {
140         unsigned short sbaud;
141         sbaud = (boot_cpu_data.bus_clock / (baud * 4))-1;
142         __sio_out(sbaud, PLD_ESIO0BAUR);
143 }
144
145 static void sio_reset(void)
146 {
147         unsigned short tmp;
148
149         tmp = __sio_in(PLD_ESIO0RXB);
150         tmp = __sio_in(PLD_ESIO0RXB);
151         tmp = __sio_in(PLD_ESIO0CR);
152         sio_set_baud_rate(BAUD_RATE);
153         __sio_out(0x0300, PLD_ESIO0CR);
154         __sio_out(0x0003, PLD_ESIO0CR);
155 }
156
157 static void sio_init(void)
158 {
159         unsigned short tmp;
160
161         tmp = __sio_in(PLD_ESIO0RXB);
162         tmp = __sio_in(PLD_ESIO0RXB);
163         tmp = __sio_in(PLD_ESIO0CR);
164         __sio_out(0x0300, PLD_ESIO0CR);
165         __sio_out(0x0003, PLD_ESIO0CR);
166 }
167
168 static void sio_error(int *status)
169 {
170         printk("SIO0 error[%04x]\n", *status);
171         do {
172                 sio_init();
173         } while ((*status = __sio_in(PLD_ESIO0CR)) != 3);
174 }
175
176 #else /* not CONFIG_SERIAL_M32R_PLDSIO */
177
178 #define __sio_in(x) inl(x)
179 #define __sio_out(v,x) outl((v),(x))
180
181 static inline void sio_set_baud_rate(unsigned long baud)
182 {
183         unsigned long i, j;
184
185         i = boot_cpu_data.bus_clock / (baud * 16);
186         j = (boot_cpu_data.bus_clock - (i * baud * 16)) / baud;
187         i -= 1;
188         j = (j + 1) >> 1;
189
190         __sio_out(i, M32R_SIO0_BAUR_PORTL);
191         __sio_out(j, M32R_SIO0_RBAUR_PORTL);
192 }
193
194 static void sio_reset(void)
195 {
196         __sio_out(0x00000300, M32R_SIO0_CR_PORTL);      /* init status */
197         __sio_out(0x00000800, M32R_SIO0_MOD1_PORTL);    /* 8bit        */
198         __sio_out(0x00000080, M32R_SIO0_MOD0_PORTL);    /* 1stop non   */
199         sio_set_baud_rate(BAUD_RATE);
200         __sio_out(0x00000000, M32R_SIO0_TRCR_PORTL);
201         __sio_out(0x00000003, M32R_SIO0_CR_PORTL);      /* RXCEN */
202 }
203
204 static void sio_init(void)
205 {
206         unsigned int tmp;
207
208         tmp = __sio_in(M32R_SIO0_RXB_PORTL);
209         tmp = __sio_in(M32R_SIO0_RXB_PORTL);
210         tmp = __sio_in(M32R_SIO0_STS_PORTL);
211         __sio_out(0x00000003, M32R_SIO0_CR_PORTL);
212 }
213
214 static void sio_error(int *status)
215 {
216         printk("SIO0 error[%04x]\n", *status);
217         do {
218                 sio_init();
219         } while ((*status = __sio_in(M32R_SIO0_CR_PORTL)) != 3);
220 }
221
222 #endif /* CONFIG_SERIAL_M32R_PLDSIO */
223
224 static unsigned int sio_in(struct uart_sio_port *up, int offset)
225 {
226         return __sio_in(up->port.iobase + offset);
227 }
228
229 static void sio_out(struct uart_sio_port *up, int offset, int value)
230 {
231         __sio_out(value, up->port.iobase + offset);
232 }
233
234 static unsigned int serial_in(struct uart_sio_port *up, int offset)
235 {
236         if (!offset)
237                 return 0;
238
239         return __sio_in(offset);
240 }
241
242 static void serial_out(struct uart_sio_port *up, int offset, int value)
243 {
244         if (!offset)
245                 return;
246
247         __sio_out(value, offset);
248 }
249
250 static void m32r_sio_stop_tx(struct uart_port *port)
251 {
252         struct uart_sio_port *up = (struct uart_sio_port *)port;
253
254         if (up->ier & UART_IER_THRI) {
255                 up->ier &= ~UART_IER_THRI;
256                 serial_out(up, UART_IER, up->ier);
257         }
258 }
259
260 static void m32r_sio_start_tx(struct uart_port *port)
261 {
262 #ifdef CONFIG_SERIAL_M32R_PLDSIO
263         struct uart_sio_port *up = (struct uart_sio_port *)port;
264         struct circ_buf *xmit = &up->port.state->xmit;
265
266         if (!(up->ier & UART_IER_THRI)) {
267                 up->ier |= UART_IER_THRI;
268                 serial_out(up, UART_IER, up->ier);
269                 serial_out(up, UART_TX, xmit->buf[xmit->tail]);
270                 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
271                 up->port.icount.tx++;
272         }
273         while((serial_in(up, UART_LSR) & UART_EMPTY) != UART_EMPTY);
274 #else
275         struct uart_sio_port *up = (struct uart_sio_port *)port;
276
277         if (!(up->ier & UART_IER_THRI)) {
278                 up->ier |= UART_IER_THRI;
279                 serial_out(up, UART_IER, up->ier);
280         }
281 #endif
282 }
283
284 static void m32r_sio_stop_rx(struct uart_port *port)
285 {
286         struct uart_sio_port *up = (struct uart_sio_port *)port;
287
288         up->ier &= ~UART_IER_RLSI;
289         up->port.read_status_mask &= ~UART_LSR_DR;
290         serial_out(up, UART_IER, up->ier);
291 }
292
293 static void m32r_sio_enable_ms(struct uart_port *port)
294 {
295         struct uart_sio_port *up = (struct uart_sio_port *)port;
296
297         up->ier |= UART_IER_MSI;
298         serial_out(up, UART_IER, up->ier);
299 }
300
301 static void receive_chars(struct uart_sio_port *up, int *status)
302 {
303         struct tty_port *port = &up->port.state->port;
304         struct tty_struct *tty = tport->tty;
305         unsigned char ch;
306         unsigned char flag;
307         int max_count = 256;
308
309         do {
310                 ch = sio_in(up, SIORXB);
311                 flag = TTY_NORMAL;
312                 up->port.icount.rx++;
313
314                 if (unlikely(*status & (UART_LSR_BI | UART_LSR_PE |
315                                        UART_LSR_FE | UART_LSR_OE))) {
316                         /*
317                          * For statistics only
318                          */
319                         if (*status & UART_LSR_BI) {
320                                 *status &= ~(UART_LSR_FE | UART_LSR_PE);
321                                 up->port.icount.brk++;
322                                 /*
323                                  * We do the SysRQ and SAK checking
324                                  * here because otherwise the break
325                                  * may get masked by ignore_status_mask
326                                  * or read_status_mask.
327                                  */
328                                 if (uart_handle_break(&up->port))
329                                         goto ignore_char;
330                         } else if (*status & UART_LSR_PE)
331                                 up->port.icount.parity++;
332                         else if (*status & UART_LSR_FE)
333                                 up->port.icount.frame++;
334                         if (*status & UART_LSR_OE)
335                                 up->port.icount.overrun++;
336
337                         /*
338                          * Mask off conditions which should be ingored.
339                          */
340                         *status &= up->port.read_status_mask;
341
342                         if (up->port.line == up->port.cons->index) {
343                                 /* Recover the break flag from console xmit */
344                                 *status |= up->lsr_break_flag;
345                                 up->lsr_break_flag = 0;
346                         }
347
348                         if (*status & UART_LSR_BI) {
349                                 DEBUG_INTR("handling break....");
350                                 flag = TTY_BREAK;
351                         } else if (*status & UART_LSR_PE)
352                                 flag = TTY_PARITY;
353                         else if (*status & UART_LSR_FE)
354                                 flag = TTY_FRAME;
355                 }
356                 if (uart_handle_sysrq_char(&up->port, ch))
357                         goto ignore_char;
358                 if ((*status & up->port.ignore_status_mask) == 0)
359                         tty_insert_flip_char(port, ch, flag);
360
361                 if (*status & UART_LSR_OE) {
362                         /*
363                          * Overrun is special, since it's reported
364                          * immediately, and doesn't affect the current
365                          * character.
366                          */
367                         tty_insert_flip_char(port, 0, TTY_OVERRUN);
368                 }
369         ignore_char:
370                 *status = serial_in(up, UART_LSR);
371         } while ((*status & UART_LSR_DR) && (max_count-- > 0));
372         tty_flip_buffer_push(tty);
373 }
374
375 static void transmit_chars(struct uart_sio_port *up)
376 {
377         struct circ_buf *xmit = &up->port.state->xmit;
378         int count;
379
380         if (up->port.x_char) {
381 #ifndef CONFIG_SERIAL_M32R_PLDSIO       /* XXX */
382                 serial_out(up, UART_TX, up->port.x_char);
383 #endif
384                 up->port.icount.tx++;
385                 up->port.x_char = 0;
386                 return;
387         }
388         if (uart_circ_empty(xmit) || uart_tx_stopped(&up->port)) {
389                 m32r_sio_stop_tx(&up->port);
390                 return;
391         }
392
393         count = up->port.fifosize;
394         do {
395                 serial_out(up, UART_TX, xmit->buf[xmit->tail]);
396                 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
397                 up->port.icount.tx++;
398                 if (uart_circ_empty(xmit))
399                         break;
400                 while (!(serial_in(up, UART_LSR) & UART_LSR_THRE));
401
402         } while (--count > 0);
403
404         if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
405                 uart_write_wakeup(&up->port);
406
407         DEBUG_INTR("THRE...");
408
409         if (uart_circ_empty(xmit))
410                 m32r_sio_stop_tx(&up->port);
411 }
412
413 /*
414  * This handles the interrupt from one port.
415  */
416 static inline void m32r_sio_handle_port(struct uart_sio_port *up,
417         unsigned int status)
418 {
419         DEBUG_INTR("status = %x...", status);
420
421         if (status & 0x04)
422                 receive_chars(up, &status);
423         if (status & 0x01)
424                 transmit_chars(up);
425 }
426
427 /*
428  * This is the serial driver's interrupt routine.
429  *
430  * Arjan thinks the old way was overly complex, so it got simplified.
431  * Alan disagrees, saying that need the complexity to handle the weird
432  * nature of ISA shared interrupts.  (This is a special exception.)
433  *
434  * In order to handle ISA shared interrupts properly, we need to check
435  * that all ports have been serviced, and therefore the ISA interrupt
436  * line has been de-asserted.
437  *
438  * This means we need to loop through all ports. checking that they
439  * don't have an interrupt pending.
440  */
441 static irqreturn_t m32r_sio_interrupt(int irq, void *dev_id)
442 {
443         struct irq_info *i = dev_id;
444         struct list_head *l, *end = NULL;
445         int pass_counter = 0;
446
447         DEBUG_INTR("m32r_sio_interrupt(%d)...", irq);
448
449 #ifdef CONFIG_SERIAL_M32R_PLDSIO
450 //      if (irq == PLD_IRQ_SIO0_SND)
451 //              irq = PLD_IRQ_SIO0_RCV;
452 #else
453         if (irq == M32R_IRQ_SIO0_S)
454                 irq = M32R_IRQ_SIO0_R;
455 #endif
456
457         spin_lock(&i->lock);
458
459         l = i->head;
460         do {
461                 struct uart_sio_port *up;
462                 unsigned int sts;
463
464                 up = list_entry(l, struct uart_sio_port, list);
465
466                 sts = sio_in(up, SIOSTS);
467                 if (sts & 0x5) {
468                         spin_lock(&up->port.lock);
469                         m32r_sio_handle_port(up, sts);
470                         spin_unlock(&up->port.lock);
471
472                         end = NULL;
473                 } else if (end == NULL)
474                         end = l;
475
476                 l = l->next;
477
478                 if (l == i->head && pass_counter++ > PASS_LIMIT) {
479                         if (sts & 0xe0)
480                                 sio_error(&sts);
481                         break;
482                 }
483         } while (l != end);
484
485         spin_unlock(&i->lock);
486
487         DEBUG_INTR("end.\n");
488
489         return IRQ_HANDLED;
490 }
491
492 /*
493  * To support ISA shared interrupts, we need to have one interrupt
494  * handler that ensures that the IRQ line has been deasserted
495  * before returning.  Failing to do this will result in the IRQ
496  * line being stuck active, and, since ISA irqs are edge triggered,
497  * no more IRQs will be seen.
498  */
499 static void serial_do_unlink(struct irq_info *i, struct uart_sio_port *up)
500 {
501         spin_lock_irq(&i->lock);
502
503         if (!list_empty(i->head)) {
504                 if (i->head == &up->list)
505                         i->head = i->head->next;
506                 list_del(&up->list);
507         } else {
508                 BUG_ON(i->head != &up->list);
509                 i->head = NULL;
510         }
511
512         spin_unlock_irq(&i->lock);
513 }
514
515 static int serial_link_irq_chain(struct uart_sio_port *up)
516 {
517         struct irq_info *i = irq_lists + up->port.irq;
518         int ret, irq_flags = 0;
519
520         spin_lock_irq(&i->lock);
521
522         if (i->head) {
523                 list_add(&up->list, i->head);
524                 spin_unlock_irq(&i->lock);
525
526                 ret = 0;
527         } else {
528                 INIT_LIST_HEAD(&up->list);
529                 i->head = &up->list;
530                 spin_unlock_irq(&i->lock);
531
532                 ret = request_irq(up->port.irq, m32r_sio_interrupt,
533                                   irq_flags, "SIO0-RX", i);
534                 ret |= request_irq(up->port.irq + 1, m32r_sio_interrupt,
535                                   irq_flags, "SIO0-TX", i);
536                 if (ret < 0)
537                         serial_do_unlink(i, up);
538         }
539
540         return ret;
541 }
542
543 static void serial_unlink_irq_chain(struct uart_sio_port *up)
544 {
545         struct irq_info *i = irq_lists + up->port.irq;
546
547         BUG_ON(i->head == NULL);
548
549         if (list_empty(i->head)) {
550                 free_irq(up->port.irq, i);
551                 free_irq(up->port.irq + 1, i);
552         }
553
554         serial_do_unlink(i, up);
555 }
556
557 /*
558  * This function is used to handle ports that do not have an interrupt.
559  */
560 static void m32r_sio_timeout(unsigned long data)
561 {
562         struct uart_sio_port *up = (struct uart_sio_port *)data;
563         unsigned int timeout;
564         unsigned int sts;
565
566         sts = sio_in(up, SIOSTS);
567         if (sts & 0x5) {
568                 spin_lock(&up->port.lock);
569                 m32r_sio_handle_port(up, sts);
570                 spin_unlock(&up->port.lock);
571         }
572
573         timeout = up->port.timeout;
574         timeout = timeout > 6 ? (timeout / 2 - 2) : 1;
575         mod_timer(&up->timer, jiffies + timeout);
576 }
577
578 static unsigned int m32r_sio_tx_empty(struct uart_port *port)
579 {
580         struct uart_sio_port *up = (struct uart_sio_port *)port;
581         unsigned long flags;
582         unsigned int ret;
583
584         spin_lock_irqsave(&up->port.lock, flags);
585         ret = serial_in(up, UART_LSR) & UART_LSR_TEMT ? TIOCSER_TEMT : 0;
586         spin_unlock_irqrestore(&up->port.lock, flags);
587
588         return ret;
589 }
590
591 static unsigned int m32r_sio_get_mctrl(struct uart_port *port)
592 {
593         return 0;
594 }
595
596 static void m32r_sio_set_mctrl(struct uart_port *port, unsigned int mctrl)
597 {
598
599 }
600
601 static void m32r_sio_break_ctl(struct uart_port *port, int break_state)
602 {
603
604 }
605
606 static int m32r_sio_startup(struct uart_port *port)
607 {
608         struct uart_sio_port *up = (struct uart_sio_port *)port;
609         int retval;
610
611         sio_init();
612
613         /*
614          * If the "interrupt" for this port doesn't correspond with any
615          * hardware interrupt, we use a timer-based system.  The original
616          * driver used to do this with IRQ0.
617          */
618         if (!up->port.irq) {
619                 unsigned int timeout = up->port.timeout;
620
621                 timeout = timeout > 6 ? (timeout / 2 - 2) : 1;
622
623                 up->timer.data = (unsigned long)up;
624                 mod_timer(&up->timer, jiffies + timeout);
625         } else {
626                 retval = serial_link_irq_chain(up);
627                 if (retval)
628                         return retval;
629         }
630
631         /*
632          * Finally, enable interrupts.  Note: Modem status interrupts
633          * are set via set_termios(), which will be occurring imminently
634          * anyway, so we don't enable them here.
635          * - M32R_SIO: 0x0c
636          * - M32R_PLDSIO: 0x04
637          */
638         up->ier = UART_IER_MSI | UART_IER_RLSI | UART_IER_RDI;
639         sio_out(up, SIOTRCR, up->ier);
640
641         /*
642          * And clear the interrupt registers again for luck.
643          */
644         sio_reset();
645
646         return 0;
647 }
648
649 static void m32r_sio_shutdown(struct uart_port *port)
650 {
651         struct uart_sio_port *up = (struct uart_sio_port *)port;
652
653         /*
654          * Disable interrupts from this port
655          */
656         up->ier = 0;
657         sio_out(up, SIOTRCR, 0);
658
659         /*
660          * Disable break condition and FIFOs
661          */
662
663         sio_init();
664
665         if (!up->port.irq)
666                 del_timer_sync(&up->timer);
667         else
668                 serial_unlink_irq_chain(up);
669 }
670
671 static unsigned int m32r_sio_get_divisor(struct uart_port *port,
672         unsigned int baud)
673 {
674         return uart_get_divisor(port, baud);
675 }
676
677 static void m32r_sio_set_termios(struct uart_port *port,
678         struct ktermios *termios, struct ktermios *old)
679 {
680         struct uart_sio_port *up = (struct uart_sio_port *)port;
681         unsigned char cval = 0;
682         unsigned long flags;
683         unsigned int baud, quot;
684
685         switch (termios->c_cflag & CSIZE) {
686         case CS5:
687                 cval = UART_LCR_WLEN5;
688                 break;
689         case CS6:
690                 cval = UART_LCR_WLEN6;
691                 break;
692         case CS7:
693                 cval = UART_LCR_WLEN7;
694                 break;
695         default:
696         case CS8:
697                 cval = UART_LCR_WLEN8;
698                 break;
699         }
700
701         if (termios->c_cflag & CSTOPB)
702                 cval |= UART_LCR_STOP;
703         if (termios->c_cflag & PARENB)
704                 cval |= UART_LCR_PARITY;
705         if (!(termios->c_cflag & PARODD))
706                 cval |= UART_LCR_EPAR;
707 #ifdef CMSPAR
708         if (termios->c_cflag & CMSPAR)
709                 cval |= UART_LCR_SPAR;
710 #endif
711
712         /*
713          * Ask the core to calculate the divisor for us.
714          */
715 #ifdef CONFIG_SERIAL_M32R_PLDSIO
716         baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/4);
717 #else
718         baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
719 #endif
720         quot = m32r_sio_get_divisor(port, baud);
721
722         /*
723          * Ok, we're now changing the port state.  Do it with
724          * interrupts disabled.
725          */
726         spin_lock_irqsave(&up->port.lock, flags);
727
728         sio_set_baud_rate(baud);
729
730         /*
731          * Update the per-port timeout.
732          */
733         uart_update_timeout(port, termios->c_cflag, baud);
734
735         up->port.read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
736         if (termios->c_iflag & INPCK)
737                 up->port.read_status_mask |= UART_LSR_FE | UART_LSR_PE;
738         if (termios->c_iflag & (BRKINT | PARMRK))
739                 up->port.read_status_mask |= UART_LSR_BI;
740
741         /*
742          * Characteres to ignore
743          */
744         up->port.ignore_status_mask = 0;
745         if (termios->c_iflag & IGNPAR)
746                 up->port.ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
747         if (termios->c_iflag & IGNBRK) {
748                 up->port.ignore_status_mask |= UART_LSR_BI;
749                 /*
750                  * If we're ignoring parity and break indicators,
751                  * ignore overruns too (for real raw support).
752                  */
753                 if (termios->c_iflag & IGNPAR)
754                         up->port.ignore_status_mask |= UART_LSR_OE;
755         }
756
757         /*
758          * ignore all characters if CREAD is not set
759          */
760         if ((termios->c_cflag & CREAD) == 0)
761                 up->port.ignore_status_mask |= UART_LSR_DR;
762
763         /*
764          * CTS flow control flag and modem status interrupts
765          */
766         up->ier &= ~UART_IER_MSI;
767         if (UART_ENABLE_MS(&up->port, termios->c_cflag))
768                 up->ier |= UART_IER_MSI;
769
770         serial_out(up, UART_IER, up->ier);
771
772         up->lcr = cval;                                 /* Save LCR */
773         spin_unlock_irqrestore(&up->port.lock, flags);
774 }
775
776 static void m32r_sio_pm(struct uart_port *port, unsigned int state,
777         unsigned int oldstate)
778 {
779         struct uart_sio_port *up = (struct uart_sio_port *)port;
780
781         if (up->pm)
782                 up->pm(port, state, oldstate);
783 }
784
785 /*
786  * Resource handling.  This is complicated by the fact that resources
787  * depend on the port type.  Maybe we should be claiming the standard
788  * 8250 ports, and then trying to get other resources as necessary?
789  */
790 static int
791 m32r_sio_request_std_resource(struct uart_sio_port *up, struct resource **res)
792 {
793         unsigned int size = 8 << up->port.regshift;
794 #ifndef CONFIG_SERIAL_M32R_PLDSIO
795         unsigned long start;
796 #endif
797         int ret = 0;
798
799         switch (up->port.iotype) {
800         case UPIO_MEM:
801                 if (up->port.mapbase) {
802 #ifdef CONFIG_SERIAL_M32R_PLDSIO
803                         *res = request_mem_region(up->port.mapbase, size, "serial");
804 #else
805                         start = up->port.mapbase;
806                         *res = request_mem_region(start, size, "serial");
807 #endif
808                         if (!*res)
809                                 ret = -EBUSY;
810                 }
811                 break;
812
813         case UPIO_PORT:
814                 *res = request_region(up->port.iobase, size, "serial");
815                 if (!*res)
816                         ret = -EBUSY;
817                 break;
818         }
819         return ret;
820 }
821
822 static void m32r_sio_release_port(struct uart_port *port)
823 {
824         struct uart_sio_port *up = (struct uart_sio_port *)port;
825         unsigned long start, offset = 0, size = 0;
826
827         size <<= up->port.regshift;
828
829         switch (up->port.iotype) {
830         case UPIO_MEM:
831                 if (up->port.mapbase) {
832                         /*
833                          * Unmap the area.
834                          */
835                         iounmap(up->port.membase);
836                         up->port.membase = NULL;
837
838                         start = up->port.mapbase;
839
840                         if (size)
841                                 release_mem_region(start + offset, size);
842                         release_mem_region(start, 8 << up->port.regshift);
843                 }
844                 break;
845
846         case UPIO_PORT:
847                 start = up->port.iobase;
848
849                 if (size)
850                         release_region(start + offset, size);
851                 release_region(start + offset, 8 << up->port.regshift);
852                 break;
853
854         default:
855                 break;
856         }
857 }
858
859 static int m32r_sio_request_port(struct uart_port *port)
860 {
861         struct uart_sio_port *up = (struct uart_sio_port *)port;
862         struct resource *res = NULL;
863         int ret = 0;
864
865         ret = m32r_sio_request_std_resource(up, &res);
866
867         /*
868          * If we have a mapbase, then request that as well.
869          */
870         if (ret == 0 && up->port.flags & UPF_IOREMAP) {
871                 int size = resource_size(res);
872
873                 up->port.membase = ioremap(up->port.mapbase, size);
874                 if (!up->port.membase)
875                         ret = -ENOMEM;
876         }
877
878         if (ret < 0) {
879                 if (res)
880                         release_resource(res);
881         }
882
883         return ret;
884 }
885
886 static void m32r_sio_config_port(struct uart_port *port, int unused)
887 {
888         struct uart_sio_port *up = (struct uart_sio_port *)port;
889         unsigned long flags;
890
891         spin_lock_irqsave(&up->port.lock, flags);
892
893         up->port.fifosize = 1;
894
895         spin_unlock_irqrestore(&up->port.lock, flags);
896 }
897
898 static int
899 m32r_sio_verify_port(struct uart_port *port, struct serial_struct *ser)
900 {
901         if (ser->irq >= nr_irqs || ser->irq < 0 || ser->baud_base < 9600)
902                 return -EINVAL;
903         return 0;
904 }
905
906 static struct uart_ops m32r_sio_pops = {
907         .tx_empty       = m32r_sio_tx_empty,
908         .set_mctrl      = m32r_sio_set_mctrl,
909         .get_mctrl      = m32r_sio_get_mctrl,
910         .stop_tx        = m32r_sio_stop_tx,
911         .start_tx       = m32r_sio_start_tx,
912         .stop_rx        = m32r_sio_stop_rx,
913         .enable_ms      = m32r_sio_enable_ms,
914         .break_ctl      = m32r_sio_break_ctl,
915         .startup        = m32r_sio_startup,
916         .shutdown       = m32r_sio_shutdown,
917         .set_termios    = m32r_sio_set_termios,
918         .pm             = m32r_sio_pm,
919         .release_port   = m32r_sio_release_port,
920         .request_port   = m32r_sio_request_port,
921         .config_port    = m32r_sio_config_port,
922         .verify_port    = m32r_sio_verify_port,
923 };
924
925 static struct uart_sio_port m32r_sio_ports[UART_NR];
926
927 static void __init m32r_sio_init_ports(void)
928 {
929         struct uart_sio_port *up;
930         static int first = 1;
931         int i;
932
933         if (!first)
934                 return;
935         first = 0;
936
937         for (i = 0, up = m32r_sio_ports; i < ARRAY_SIZE(old_serial_port);
938              i++, up++) {
939                 up->port.iobase   = old_serial_port[i].port;
940                 up->port.irq      = irq_canonicalize(old_serial_port[i].irq);
941                 up->port.uartclk  = old_serial_port[i].baud_base * 16;
942                 up->port.flags    = old_serial_port[i].flags;
943                 up->port.membase  = old_serial_port[i].iomem_base;
944                 up->port.iotype   = old_serial_port[i].io_type;
945                 up->port.regshift = old_serial_port[i].iomem_reg_shift;
946                 up->port.ops      = &m32r_sio_pops;
947         }
948 }
949
950 static void __init m32r_sio_register_ports(struct uart_driver *drv)
951 {
952         int i;
953
954         m32r_sio_init_ports();
955
956         for (i = 0; i < UART_NR; i++) {
957                 struct uart_sio_port *up = &m32r_sio_ports[i];
958
959                 up->port.line = i;
960                 up->port.ops = &m32r_sio_pops;
961                 init_timer(&up->timer);
962                 up->timer.function = m32r_sio_timeout;
963
964                 up->mcr_mask = ~0;
965                 up->mcr_force = 0;
966
967                 uart_add_one_port(drv, &up->port);
968         }
969 }
970
971 #ifdef CONFIG_SERIAL_M32R_SIO_CONSOLE
972
973 /*
974  *      Wait for transmitter & holding register to empty
975  */
976 static inline void wait_for_xmitr(struct uart_sio_port *up)
977 {
978         unsigned int status, tmout = 10000;
979
980         /* Wait up to 10ms for the character(s) to be sent. */
981         do {
982                 status = sio_in(up, SIOSTS);
983
984                 if (--tmout == 0)
985                         break;
986                 udelay(1);
987         } while ((status & UART_EMPTY) != UART_EMPTY);
988
989         /* Wait up to 1s for flow control if necessary */
990         if (up->port.flags & UPF_CONS_FLOW) {
991                 tmout = 1000000;
992                 while (--tmout)
993                         udelay(1);
994         }
995 }
996
997 static void m32r_sio_console_putchar(struct uart_port *port, int ch)
998 {
999         struct uart_sio_port *up = (struct uart_sio_port *)port;
1000
1001         wait_for_xmitr(up);
1002         sio_out(up, SIOTXB, ch);
1003 }
1004
1005 /*
1006  *      Print a string to the serial port trying not to disturb
1007  *      any possible real use of the port...
1008  *
1009  *      The console_lock must be held when we get here.
1010  */
1011 static void m32r_sio_console_write(struct console *co, const char *s,
1012         unsigned int count)
1013 {
1014         struct uart_sio_port *up = &m32r_sio_ports[co->index];
1015         unsigned int ier;
1016
1017         /*
1018          *      First save the UER then disable the interrupts
1019          */
1020         ier = sio_in(up, SIOTRCR);
1021         sio_out(up, SIOTRCR, 0);
1022
1023         uart_console_write(&up->port, s, count, m32r_sio_console_putchar);
1024
1025         /*
1026          *      Finally, wait for transmitter to become empty
1027          *      and restore the IER
1028          */
1029         wait_for_xmitr(up);
1030         sio_out(up, SIOTRCR, ier);
1031 }
1032
1033 static int __init m32r_sio_console_setup(struct console *co, char *options)
1034 {
1035         struct uart_port *port;
1036         int baud = 9600;
1037         int bits = 8;
1038         int parity = 'n';
1039         int flow = 'n';
1040
1041         /*
1042          * Check whether an invalid uart number has been specified, and
1043          * if so, search for the first available port that does have
1044          * console support.
1045          */
1046         if (co->index >= UART_NR)
1047                 co->index = 0;
1048         port = &m32r_sio_ports[co->index].port;
1049
1050         /*
1051          * Temporary fix.
1052          */
1053         spin_lock_init(&port->lock);
1054
1055         if (options)
1056                 uart_parse_options(options, &baud, &parity, &bits, &flow);
1057
1058         return uart_set_options(port, co, baud, parity, bits, flow);
1059 }
1060
1061 static struct uart_driver m32r_sio_reg;
1062 static struct console m32r_sio_console = {
1063         .name           = "ttyS",
1064         .write          = m32r_sio_console_write,
1065         .device         = uart_console_device,
1066         .setup          = m32r_sio_console_setup,
1067         .flags          = CON_PRINTBUFFER,
1068         .index          = -1,
1069         .data           = &m32r_sio_reg,
1070 };
1071
1072 static int __init m32r_sio_console_init(void)
1073 {
1074         sio_reset();
1075         sio_init();
1076         m32r_sio_init_ports();
1077         register_console(&m32r_sio_console);
1078         return 0;
1079 }
1080 console_initcall(m32r_sio_console_init);
1081
1082 #define M32R_SIO_CONSOLE        &m32r_sio_console
1083 #else
1084 #define M32R_SIO_CONSOLE        NULL
1085 #endif
1086
1087 static struct uart_driver m32r_sio_reg = {
1088         .owner                  = THIS_MODULE,
1089         .driver_name            = "sio",
1090         .dev_name               = "ttyS",
1091         .major                  = TTY_MAJOR,
1092         .minor                  = 64,
1093         .nr                     = UART_NR,
1094         .cons                   = M32R_SIO_CONSOLE,
1095 };
1096
1097 /**
1098  *      m32r_sio_suspend_port - suspend one serial port
1099  *      @line: serial line number
1100  *
1101  *      Suspend one serial port.
1102  */
1103 void m32r_sio_suspend_port(int line)
1104 {
1105         uart_suspend_port(&m32r_sio_reg, &m32r_sio_ports[line].port);
1106 }
1107
1108 /**
1109  *      m32r_sio_resume_port - resume one serial port
1110  *      @line: serial line number
1111  *
1112  *      Resume one serial port.
1113  */
1114 void m32r_sio_resume_port(int line)
1115 {
1116         uart_resume_port(&m32r_sio_reg, &m32r_sio_ports[line].port);
1117 }
1118
1119 static int __init m32r_sio_init(void)
1120 {
1121         int ret, i;
1122
1123         printk(KERN_INFO "Serial: M32R SIO driver\n");
1124
1125         for (i = 0; i < nr_irqs; i++)
1126                 spin_lock_init(&irq_lists[i].lock);
1127
1128         ret = uart_register_driver(&m32r_sio_reg);
1129         if (ret >= 0)
1130                 m32r_sio_register_ports(&m32r_sio_reg);
1131
1132         return ret;
1133 }
1134
1135 static void __exit m32r_sio_exit(void)
1136 {
1137         int i;
1138
1139         for (i = 0; i < UART_NR; i++)
1140                 uart_remove_one_port(&m32r_sio_reg, &m32r_sio_ports[i].port);
1141
1142         uart_unregister_driver(&m32r_sio_reg);
1143 }
1144
1145 module_init(m32r_sio_init);
1146 module_exit(m32r_sio_exit);
1147
1148 EXPORT_SYMBOL(m32r_sio_suspend_port);
1149 EXPORT_SYMBOL(m32r_sio_resume_port);
1150
1151 MODULE_LICENSE("GPL");
1152 MODULE_DESCRIPTION("Generic M32R SIO serial driver");