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