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