4 * Driver for M32R serial ports
6 * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
7 * Based on drivers/serial/8250.c.
9 * Copyright (C) 2001 Russell King.
10 * Copyright (C) 2004 Hirokazu Takata <takata at linux-m32r.org>
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.
19 * A note about mapbase / membase
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.
25 * membase is an 'ioremapped' cookie. This is compatible with the old
26 * serial.c driver, and is currently the preferred form.
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>
43 #if defined(CONFIG_SERIAL_M32R_SIO_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
48 #define PORT_MAX_SIO 1
49 #define BAUD_RATE 115200
51 #include <linux/serial_core.h>
53 #include "m32r_sio_reg.h"
57 * share_irqs - whether we pass SA_SHIRQ to request_irq(). This option
58 * is unsafe when used on edge-triggered interrupts.
60 unsigned int share_irqs_sio = M32R_SIO_SHARE_IRQS;
66 #define DEBUG_AUTOCONF(fmt...) printk(fmt)
68 #define DEBUG_AUTOCONF(fmt...) do { } while (0)
72 #define DEBUG_INTR(fmt...) printk(fmt)
74 #define DEBUG_INTR(fmt...) do { } while (0)
77 #define PASS_LIMIT 256
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.
84 #define is_real_interrupt(irq) ((irq) != 0)
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
92 #undef CONFIG_SERIAL_MANY_PORTS
93 #undef CONFIG_SERIAL_DETECT_IRQ
94 #undef CONFIG_SERIAL_MULTIPORT
97 #ifdef CONFIG_SERIAL_M32R_SIO_DETECT_IRQ
98 #define CONFIG_SERIAL_DETECT_IRQ 1
100 #ifdef CONFIG_SERIAL_M32R_SIO_MULTIPORT
101 #define CONFIG_SERIAL_MULTIPORT 1
103 #ifdef CONFIG_SERIAL_M32R_SIO_MANY_PORTS
104 #define CONFIG_SERIAL_MANY_PORTS 1
108 * HUB6 is always on. This will be removed once the header
109 * files have been cleaned.
111 #define CONFIG_HUB6 1
113 #include <asm/serial.h>
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 },
120 static struct old_serial_port old_serial_port[] = {
121 { 0, BASE_BAUD, M32R_SIO_OFFSET, M32R_IRQ_SIO0_R, STD_COM_FLAGS },
125 #define UART_NR ARRAY_SIZE(old_serial_port)
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 */
135 unsigned char mcr_mask; /* mask of user bits */
136 unsigned char mcr_force; /* mask of forced bits */
137 unsigned char lsr_break_flag;
140 * We provide a per-port pm hook.
142 void (*pm)(struct uart_port *port,
143 unsigned int state, unsigned int old);
148 struct list_head *head;
151 static struct irq_info irq_lists[NR_IRQS];
154 * Here we define the default xmit fifo size used for each type of UART.
156 static const struct serial_uart_config uart_config[PORT_MAX_SIO+1] = {
161 #ifdef CONFIG_SERIAL_M32R_PLDSIO
163 #define __sio_in(x) inw((unsigned long)(x))
164 #define __sio_out(v,x) outw((v),(unsigned long)(x))
166 static inline void sio_set_baud_rate(unsigned long baud)
168 unsigned short sbaud;
169 sbaud = (boot_cpu_data.bus_clock / (baud * 4))-1;
170 __sio_out(sbaud, PLD_ESIO0BAUR);
173 static void sio_reset(void)
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);
185 static void sio_init(void)
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);
196 static void sio_error(int *status)
198 printk("SIO0 error[%04x]\n", *status);
201 } while ((*status = __sio_in(PLD_ESIO0CR)) != 3);
204 #else /* not CONFIG_SERIAL_M32R_PLDSIO */
206 #define __sio_in(x) inl(x)
207 #define __sio_out(v,x) outl((v),(x))
209 static inline void sio_set_baud_rate(unsigned long baud)
213 i = boot_cpu_data.bus_clock / (baud * 16);
214 j = (boot_cpu_data.bus_clock - (i * baud * 16)) / baud;
218 __sio_out(i, M32R_SIO0_BAUR_PORTL);
219 __sio_out(j, M32R_SIO0_RBAUR_PORTL);
222 static void sio_reset(void)
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 */
232 static void sio_init(void)
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);
242 static void sio_error(int *status)
244 printk("SIO0 error[%04x]\n", *status);
247 } while ((*status = __sio_in(M32R_SIO0_CR_PORTL)) != 3);
250 #endif /* CONFIG_SERIAL_M32R_PLDSIO */
252 static _INLINE_ unsigned int sio_in(struct uart_sio_port *up, int offset)
254 return __sio_in(up->port.iobase + offset);
257 static _INLINE_ void sio_out(struct uart_sio_port *up, int offset, int value)
259 __sio_out(value, up->port.iobase + offset);
262 static _INLINE_ unsigned int serial_in(struct uart_sio_port *up, int offset)
267 return __sio_in(offset);
271 serial_out(struct uart_sio_port *up, int offset, int value)
276 __sio_out(value, offset);
279 static void m32r_sio_stop_tx(struct uart_port *port, unsigned int tty_stop)
281 struct uart_sio_port *up = (struct uart_sio_port *)port;
283 if (up->ier & UART_IER_THRI) {
284 up->ier &= ~UART_IER_THRI;
285 serial_out(up, UART_IER, up->ier);
289 static void m32r_sio_start_tx(struct uart_port *port, unsigned int tty_start)
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;
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++;
302 while((serial_in(up, UART_LSR) & UART_EMPTY) != UART_EMPTY);
304 struct uart_sio_port *up = (struct uart_sio_port *)port;
306 if (!(up->ier & UART_IER_THRI)) {
307 up->ier |= UART_IER_THRI;
308 serial_out(up, UART_IER, up->ier);
313 static void m32r_sio_stop_rx(struct uart_port *port)
315 struct uart_sio_port *up = (struct uart_sio_port *)port;
317 up->ier &= ~UART_IER_RLSI;
318 up->port.read_status_mask &= ~UART_LSR_DR;
319 serial_out(up, UART_IER, up->ier);
322 static void m32r_sio_enable_ms(struct uart_port *port)
324 struct uart_sio_port *up = (struct uart_sio_port *)port;
326 up->ier |= UART_IER_MSI;
327 serial_out(up, UART_IER, up->ier);
330 static _INLINE_ void receive_chars(struct uart_sio_port *up, int *status,
331 struct pt_regs *regs)
333 struct tty_struct *tty = up->port.info->tty;
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
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++;
348 if (unlikely(*status & (UART_LSR_BI | UART_LSR_PE |
349 UART_LSR_FE | UART_LSR_OE))) {
351 * For statistics only
353 if (*status & UART_LSR_BI) {
354 *status &= ~(UART_LSR_FE | UART_LSR_PE);
355 up->port.icount.brk++;
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.
362 if (uart_handle_break(&up->port))
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++;
372 * Mask off conditions which should be ingored.
374 *status &= up->port.read_status_mask;
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;
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;
390 if (uart_handle_sysrq_char(&up->port, ch, regs))
392 if ((*status & up->port.ignore_status_mask) == 0) {
393 tty->flip.flag_buf_ptr++;
394 tty->flip.char_buf_ptr++;
397 if ((*status & UART_LSR_OE) &&
398 tty->flip.count < TTY_FLIPBUF_SIZE) {
400 * Overrun is special, since it's reported
401 * immediately, and doesn't affect the current
404 *tty->flip.flag_buf_ptr = TTY_OVERRUN;
405 tty->flip.flag_buf_ptr++;
406 tty->flip.char_buf_ptr++;
410 *status = serial_in(up, UART_LSR);
411 } while ((*status & UART_LSR_DR) && (max_count-- > 0));
412 tty_flip_buffer_push(tty);
415 static _INLINE_ void transmit_chars(struct uart_sio_port *up)
417 struct circ_buf *xmit = &up->port.info->xmit;
420 if (up->port.x_char) {
421 #ifndef CONFIG_SERIAL_M32R_PLDSIO /* XXX */
422 serial_out(up, UART_TX, up->port.x_char);
424 up->port.icount.tx++;
428 if (uart_circ_empty(xmit) || uart_tx_stopped(&up->port)) {
429 m32r_sio_stop_tx(&up->port, 0);
433 count = up->port.fifosize;
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))
440 while (!serial_in(up, UART_LSR) & UART_LSR_THRE);
442 } while (--count > 0);
444 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
445 uart_write_wakeup(&up->port);
447 DEBUG_INTR("THRE...");
449 if (uart_circ_empty(xmit))
450 m32r_sio_stop_tx(&up->port, 0);
454 * This handles the interrupt from one port.
456 static inline void m32r_sio_handle_port(struct uart_sio_port *up,
457 unsigned int status, struct pt_regs *regs)
459 DEBUG_INTR("status = %x...", status);
462 receive_chars(up, &status, regs);
463 // check_modem_status(up);
469 * This is the serial driver's interrupt routine.
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.)
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.
479 * This means we need to loop through all ports. checking that they
480 * don't have an interrupt pending.
482 static irqreturn_t m32r_sio_interrupt(int irq, void *dev_id,
483 struct pt_regs *regs)
485 struct irq_info *i = dev_id;
486 struct list_head *l, *end = NULL;
487 int pass_counter = 0;
489 DEBUG_INTR("m32r_sio_interrupt(%d)...", irq);
491 #ifdef CONFIG_SERIAL_M32R_PLDSIO
492 // if (irq == PLD_IRQ_SIO0_SND)
493 // irq = PLD_IRQ_SIO0_RCV;
495 if (irq == M32R_IRQ_SIO0_S)
496 irq = M32R_IRQ_SIO0_R;
503 struct uart_sio_port *up;
506 up = list_entry(l, struct uart_sio_port, list);
508 sts = sio_in(up, SIOSTS);
510 spin_lock(&up->port.lock);
511 m32r_sio_handle_port(up, sts, regs);
512 spin_unlock(&up->port.lock);
515 } else if (end == NULL)
520 if (l == i->head && pass_counter++ > PASS_LIMIT) {
527 spin_unlock(&i->lock);
529 DEBUG_INTR("end.\n");
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.
541 static void serial_do_unlink(struct irq_info *i, struct uart_sio_port *up)
543 spin_lock_irq(&i->lock);
545 if (!list_empty(i->head)) {
546 if (i->head == &up->list)
547 i->head = i->head->next;
550 BUG_ON(i->head != &up->list);
554 spin_unlock_irq(&i->lock);
557 static int serial_link_irq_chain(struct uart_sio_port *up)
559 struct irq_info *i = irq_lists + up->port.irq;
560 int ret, irq_flags = up->port.flags & UPF_SHARE_IRQ ? SA_SHIRQ : 0;
562 spin_lock_irq(&i->lock);
565 list_add(&up->list, i->head);
566 spin_unlock_irq(&i->lock);
570 INIT_LIST_HEAD(&up->list);
572 spin_unlock_irq(&i->lock);
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);
579 serial_do_unlink(i, up);
585 static void serial_unlink_irq_chain(struct uart_sio_port *up)
587 struct irq_info *i = irq_lists + up->port.irq;
589 BUG_ON(i->head == NULL);
591 if (list_empty(i->head)) {
592 free_irq(up->port.irq, i);
593 free_irq(up->port.irq + 1, i);
596 serial_do_unlink(i, up);
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).
605 static void m32r_sio_timeout(unsigned long data)
607 struct uart_sio_port *up = (struct uart_sio_port *)data;
608 unsigned int timeout;
611 sts = sio_in(up, SIOSTS);
613 spin_lock(&up->port.lock);
614 m32r_sio_handle_port(up, sts, NULL);
615 spin_unlock(&up->port.lock);
618 timeout = up->port.timeout;
619 timeout = timeout > 6 ? (timeout / 2 - 2) : 1;
620 mod_timer(&up->timer, jiffies + timeout);
623 static unsigned int m32r_sio_tx_empty(struct uart_port *port)
625 struct uart_sio_port *up = (struct uart_sio_port *)port;
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);
636 static unsigned int m32r_sio_get_mctrl(struct uart_port *port)
641 static void m32r_sio_set_mctrl(struct uart_port *port, unsigned int mctrl)
646 static void m32r_sio_break_ctl(struct uart_port *port, int break_state)
651 static int m32r_sio_startup(struct uart_port *port)
653 struct uart_sio_port *up = (struct uart_sio_port *)port;
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.
663 if (!is_real_interrupt(up->port.irq)) {
664 unsigned int timeout = up->port.timeout;
666 timeout = timeout > 6 ? (timeout / 2 - 2) : 1;
668 up->timer.data = (unsigned long)up;
669 mod_timer(&up->timer, jiffies + timeout);
671 retval = serial_link_irq_chain(up);
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.
681 * - M32R_PLDSIO: 0x04
683 up->ier = UART_IER_MSI | UART_IER_RLSI | UART_IER_RDI;
684 sio_out(up, SIOTRCR, up->ier);
687 * And clear the interrupt registers again for luck.
694 static void m32r_sio_shutdown(struct uart_port *port)
696 struct uart_sio_port *up = (struct uart_sio_port *)port;
699 * Disable interrupts from this port
702 sio_out(up, SIOTRCR, 0);
705 * Disable break condition and FIFOs
710 if (!is_real_interrupt(up->port.irq))
711 del_timer_sync(&up->timer);
713 serial_unlink_irq_chain(up);
716 static unsigned int m32r_sio_get_divisor(struct uart_port *port,
719 return uart_get_divisor(port, baud);
722 static void m32r_sio_set_termios(struct uart_port *port,
723 struct termios *termios, struct termios *old)
725 struct uart_sio_port *up = (struct uart_sio_port *)port;
726 unsigned char cval = 0;
728 unsigned int baud, quot;
730 switch (termios->c_cflag & CSIZE) {
746 if (termios->c_cflag & CSTOPB)
748 if (termios->c_cflag & PARENB)
749 cval |= UART_LCR_PARITY;
750 if (!(termios->c_cflag & PARODD))
751 cval |= UART_LCR_EPAR;
753 if (termios->c_cflag & CMSPAR)
754 cval |= UART_LCR_SPAR;
758 * Ask the core to calculate the divisor for us.
760 #ifdef CONFIG_SERIAL_M32R_PLDSIO
761 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/4);
763 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
765 quot = m32r_sio_get_divisor(port, baud);
768 * Ok, we're now changing the port state. Do it with
769 * interrupts disabled.
771 spin_lock_irqsave(&up->port.lock, flags);
773 sio_set_baud_rate(baud);
776 * Update the per-port timeout.
778 uart_update_timeout(port, termios->c_cflag, baud);
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;
787 * Characteres to ignore
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;
795 * If we're ignoring parity and break indicators,
796 * ignore overruns too (for real raw support).
798 if (termios->c_iflag & IGNPAR)
799 up->port.ignore_status_mask |= UART_LSR_OE;
803 * ignore all characters if CREAD is not set
805 if ((termios->c_cflag & CREAD) == 0)
806 up->port.ignore_status_mask |= UART_LSR_DR;
809 * CTS flow control flag and modem status interrupts
811 up->ier &= ~UART_IER_MSI;
812 if (UART_ENABLE_MS(&up->port, termios->c_cflag))
813 up->ier |= UART_IER_MSI;
815 serial_out(up, UART_IER, up->ier);
817 up->lcr = cval; /* Save LCR */
818 spin_unlock_irqrestore(&up->port.lock, flags);
821 static void m32r_sio_pm(struct uart_port *port, unsigned int state,
822 unsigned int oldstate)
824 struct uart_sio_port *up = (struct uart_sio_port *)port;
827 up->pm(port, state, oldstate);
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?
836 m32r_sio_request_std_resource(struct uart_sio_port *up, struct resource **res)
838 unsigned int size = 8 << up->port.regshift;
839 #ifndef CONFIG_SERIAL_M32R_PLDSIO
844 switch (up->port.iotype) {
846 if (up->port.mapbase) {
847 #ifdef CONFIG_SERIAL_M32R_PLDSIO
848 *res = request_mem_region(up->port.mapbase, size, "serial");
850 start = up->port.mapbase;
851 start += UART_RSA_BASE << up->port.regshift;
852 *res = request_mem_region(start, size, "serial");
861 *res = request_region(up->port.iobase, size, "serial");
870 m32r_sio_request_rsa_resource(struct uart_sio_port *up, struct resource **res)
872 unsigned int size = 8 << up->port.regshift;
876 switch (up->port.iotype) {
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");
884 *res = request_mem_region(up->port.mapbase, size, "serial-rsa");
893 start = up->port.iobase;
894 start += UART_RSA_BASE << up->port.regshift;
895 *res = request_region(up->port.iobase, size, "serial-rsa");
904 static void m32r_sio_release_port(struct uart_port *port)
906 struct uart_sio_port *up = (struct uart_sio_port *)port;
907 unsigned long start, offset = 0, size = 0;
909 if (up->port.type == PORT_RSA) {
910 offset = UART_RSA_BASE << up->port.regshift;
914 size <<= up->port.regshift;
916 switch (up->port.iotype) {
918 if (up->port.mapbase) {
922 iounmap(up->port.membase);
923 up->port.membase = NULL;
925 start = up->port.mapbase;
928 release_mem_region(start + offset, size);
929 release_mem_region(start, 8 << up->port.regshift);
935 start = up->port.iobase;
938 release_region(start + offset, size);
939 release_region(start + offset, 8 << up->port.regshift);
947 static int m32r_sio_request_port(struct uart_port *port)
949 struct uart_sio_port *up = (struct uart_sio_port *)port;
950 struct resource *res = NULL, *res_rsa = NULL;
953 if (up->port.type == PORT_RSA){
954 ret = m32r_sio_request_rsa_resource(up, &res_rsa);
958 ret = m32r_sio_request_std_resource(up, &res);
961 * If we have a mapbase, then request that as well.
963 if (ret == 0 && up->port.flags & UPF_IOREMAP) {
964 int size = res->end - res->start + 1;
966 up->port.membase = ioremap(up->port.mapbase, size);
967 if (!up->port.membase)
973 release_resource(res_rsa);
975 release_resource(res);
980 static void m32r_sio_config_port(struct uart_port *port, int flags)
982 struct uart_sio_port *up = (struct uart_sio_port *)port;
984 spin_lock_irqsave(&up->port.lock, flags);
986 up->port.type = PORT_SIO;
987 up->port.fifosize = uart_config[up->port.type].dfl_xmit_fifo_size;
989 spin_unlock_irqrestore(&up->port.lock, flags);
993 m32r_sio_verify_port(struct uart_port *port, struct serial_struct *ser)
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)
1004 m32r_sio_type(struct uart_port *port)
1006 int type = port->type;
1008 if (type >= ARRAY_SIZE(uart_config))
1010 return uart_config[type].name;
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,
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,
1033 static struct uart_sio_port m32r_sio_ports[UART_NR];
1035 static void __init m32r_sio_isa_init_ports(void)
1037 struct uart_sio_port *up;
1038 static int first = 1;
1045 for (i = 0, up = m32r_sio_ports; i < ARRAY_SIZE(old_serial_port);
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;
1057 up->port.flags |= UPF_SHARE_IRQ;
1061 static void __init m32r_sio_register_ports(struct uart_driver *drv)
1065 m32r_sio_isa_init_ports();
1067 for (i = 0; i < UART_NR; i++) {
1068 struct uart_sio_port *up = &m32r_sio_ports[i];
1071 up->port.ops = &m32r_sio_pops;
1072 init_timer(&up->timer);
1073 up->timer.function = m32r_sio_timeout;
1076 * ALPHA_KLUDGE_MCR needs to be killed.
1078 up->mcr_mask = ~ALPHA_KLUDGE_MCR;
1079 up->mcr_force = ALPHA_KLUDGE_MCR;
1081 uart_add_one_port(drv, &up->port);
1085 #ifdef CONFIG_SERIAL_M32R_SIO_CONSOLE
1088 * Wait for transmitter & holding register to empty
1090 static inline void wait_for_xmitr(struct uart_sio_port *up)
1092 unsigned int status, tmout = 10000;
1094 /* Wait up to 10ms for the character(s) to be sent. */
1096 status = sio_in(up, SIOSTS);
1101 } while ((status & UART_EMPTY) != UART_EMPTY);
1103 /* Wait up to 1s for flow control if necessary */
1104 if (up->port.flags & UPF_CONS_FLOW) {
1112 * Print a string to the serial port trying not to disturb
1113 * any possible real use of the port...
1115 * The console_lock must be held when we get here.
1117 static void m32r_sio_console_write(struct console *co, const char *s,
1120 struct uart_sio_port *up = &m32r_sio_ports[co->index];
1125 * First save the UER then disable the interrupts
1127 ier = sio_in(up, SIOTRCR);
1128 sio_out(up, SIOTRCR, 0);
1131 * Now, do each character
1133 for (i = 0; i < count; i++, s++) {
1137 * Send the character out.
1138 * If a LF, also do CR...
1140 sio_out(up, SIOTXB, *s);
1144 sio_out(up, SIOTXB, 13);
1149 * Finally, wait for transmitter to become empty
1150 * and restore the IER
1153 sio_out(up, SIOTRCR, ier);
1156 static int __init m32r_sio_console_setup(struct console *co, char *options)
1158 struct uart_port *port;
1165 * Check whether an invalid uart number has been specified, and
1166 * if so, search for the first available port that does have
1169 if (co->index >= UART_NR)
1171 port = &m32r_sio_ports[co->index].port;
1176 spin_lock_init(&port->lock);
1179 uart_parse_options(options, &baud, &parity, &bits, &flow);
1181 return uart_set_options(port, co, baud, parity, bits, flow);
1184 extern struct uart_driver m32r_sio_reg;
1185 static struct console m32r_sio_console = {
1187 .write = m32r_sio_console_write,
1188 .device = uart_console_device,
1189 .setup = m32r_sio_console_setup,
1190 .flags = CON_PRINTBUFFER,
1192 .data = &m32r_sio_reg,
1195 static int __init m32r_sio_console_init(void)
1199 m32r_sio_isa_init_ports();
1200 register_console(&m32r_sio_console);
1203 console_initcall(m32r_sio_console_init);
1205 #define M32R_SIO_CONSOLE &m32r_sio_console
1207 #define M32R_SIO_CONSOLE NULL
1210 static struct uart_driver m32r_sio_reg = {
1211 .owner = THIS_MODULE,
1212 .driver_name = "sio",
1213 .devfs_name = "tts/",
1218 .cons = M32R_SIO_CONSOLE,
1222 * register_serial and unregister_serial allows for 16x50 serial ports to be
1223 * configured at run-time, to support PCMCIA modems.
1226 static int __register_m32r_sio(struct serial_struct *req, int line)
1228 struct uart_port port;
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;
1242 port.flags |= UPF_SHARE_IRQ;
1244 if (HIGH_BITS_OFFSET)
1245 port.iobase |= (long) req->port_high << HIGH_BITS_OFFSET;
1248 * If a clock rate wasn't specified by the low level
1249 * driver, then default to the standard clock rate.
1251 if (port.uartclk == 0)
1252 port.uartclk = BASE_BAUD * 16;
1254 return uart_register_port(&m32r_sio_reg, &port);
1258 * register_serial - configure a 16x50 serial port at runtime
1259 * @req: request structure
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.
1265 * The port is then probed and if necessary the IRQ is autodetected
1266 * If this fails an error is returned.
1268 * On success the port is ready to use and the line number is returned.
1270 int register_m32r_sio(struct serial_struct *req)
1272 return __register_m32r_sio(req, -1);
1275 int __init early_m32r_sio_setup(struct uart_port *port)
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;
1285 * unregister_serial - remove a 16x50 serial port at runtime
1286 * @line: serial line number
1288 * Remove one serial port. This may be called from interrupt
1291 void unregister_m32r_sio(int line)
1293 uart_unregister_port(&m32r_sio_reg, line);
1297 * This is for ISAPNP only.
1299 void m32r_sio_get_irq_map(unsigned int *map)
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;
1311 * m32r_sio_suspend_port - suspend one serial port
1312 * @line: serial line number
1314 * Suspend one serial port.
1316 void m32r_sio_suspend_port(int line)
1318 uart_suspend_port(&m32r_sio_reg, &m32r_sio_ports[line].port);
1322 * m32r_sio_resume_port - resume one serial port
1323 * @line: serial line number
1325 * Resume one serial port.
1327 void m32r_sio_resume_port(int line)
1329 uart_resume_port(&m32r_sio_reg, &m32r_sio_ports[line].port);
1332 static int __init m32r_sio_init(void)
1336 printk(KERN_INFO "Serial: M32R SIO driver $Revision: 1.9 $ "
1337 "IRQ sharing %sabled\n", share_irqs_sio ? "en" : "dis");
1339 for (i = 0; i < NR_IRQS; i++)
1340 spin_lock_init(&irq_lists[i].lock);
1342 ret = uart_register_driver(&m32r_sio_reg);
1344 m32r_sio_register_ports(&m32r_sio_reg);
1349 static void __exit m32r_sio_exit(void)
1353 for (i = 0; i < UART_NR; i++)
1354 uart_remove_one_port(&m32r_sio_reg, &m32r_sio_ports[i].port);
1356 uart_unregister_driver(&m32r_sio_reg);
1359 module_init(m32r_sio_init);
1360 module_exit(m32r_sio_exit);
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);
1368 MODULE_LICENSE("GPL");
1369 MODULE_DESCRIPTION("Generic M32R SIO serial driver $Revision: 1.9 $");
1371 module_param(share_irqs_sio, bool, 0400);
1372 MODULE_PARM_DESC(share_irqs_sio, "Share IRQs with other non-M32R SIO devices"