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