1 // SPDX-License-Identifier: GPL-2.0+
5 * Driver for M32R serial ports
7 * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
8 * Based on drivers/serial/8250.c.
10 * Copyright (C) 2001 Russell King.
11 * Copyright (C) 2004 Hirokazu Takata <takata at linux-m32r.org>
15 * A note about mapbase / membase
17 * mapbase is the physical address of the IO port. Currently, we don't
18 * support this very well, and it may well be dropped from this driver
19 * in future. As such, mapbase should be NULL.
21 * membase is an 'ioremapped' cookie. This is compatible with the old
22 * serial.c driver, and is currently the preferred form.
25 #if defined(CONFIG_SERIAL_M32R_SIO_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
29 #include <linux/tty.h>
30 #include <linux/tty_flip.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/delay.h>
42 #define BAUD_RATE 115200
44 #include <linux/serial_core.h>
45 #include "m32r_sio_reg.h"
47 #define PASS_LIMIT 256
52 } old_serial_port
[] = {
53 #if defined(CONFIG_PLAT_USRV)
55 { 0x3F8, PLD_IRQ_UART0
}, /* ttyS0 */
56 { 0x2F8, PLD_IRQ_UART1
}, /* ttyS1 */
57 #elif defined(CONFIG_SERIAL_M32R_PLDSIO)
58 { ((unsigned long)PLD_ESIO0CR
), PLD_IRQ_SIO0_RCV
}, /* ttyS0 */
60 { M32R_SIO_OFFSET
, M32R_IRQ_SIO0_R
}, /* ttyS0 */
64 #define UART_NR ARRAY_SIZE(old_serial_port)
66 struct uart_sio_port
{
67 struct uart_port port
;
68 struct timer_list timer
; /* "no irq" timer */
69 struct list_head list
; /* ports on this IRQ */
75 struct list_head
*head
;
78 static struct irq_info irq_lists
[NR_IRQS
];
80 #ifdef CONFIG_SERIAL_M32R_PLDSIO
82 #define __sio_in(x) inw((unsigned long)(x))
83 #define __sio_out(v,x) outw((v),(unsigned long)(x))
85 static inline void sio_set_baud_rate(unsigned long baud
)
88 sbaud
= (boot_cpu_data
.bus_clock
/ (baud
* 4))-1;
89 __sio_out(sbaud
, PLD_ESIO0BAUR
);
92 static void sio_reset(void)
96 tmp
= __sio_in(PLD_ESIO0RXB
);
97 tmp
= __sio_in(PLD_ESIO0RXB
);
98 tmp
= __sio_in(PLD_ESIO0CR
);
99 sio_set_baud_rate(BAUD_RATE
);
100 __sio_out(0x0300, PLD_ESIO0CR
);
101 __sio_out(0x0003, PLD_ESIO0CR
);
104 static void sio_init(void)
108 tmp
= __sio_in(PLD_ESIO0RXB
);
109 tmp
= __sio_in(PLD_ESIO0RXB
);
110 tmp
= __sio_in(PLD_ESIO0CR
);
111 __sio_out(0x0300, PLD_ESIO0CR
);
112 __sio_out(0x0003, PLD_ESIO0CR
);
115 static void sio_error(int *status
)
117 printk("SIO0 error[%04x]\n", *status
);
120 } while ((*status
= __sio_in(PLD_ESIO0CR
)) != 3);
123 #else /* not CONFIG_SERIAL_M32R_PLDSIO */
125 #define __sio_in(x) inl(x)
126 #define __sio_out(v,x) outl((v),(x))
128 static inline void sio_set_baud_rate(unsigned long baud
)
132 i
= boot_cpu_data
.bus_clock
/ (baud
* 16);
133 j
= (boot_cpu_data
.bus_clock
- (i
* baud
* 16)) / baud
;
137 __sio_out(i
, M32R_SIO0_BAUR_PORTL
);
138 __sio_out(j
, M32R_SIO0_RBAUR_PORTL
);
141 static void sio_reset(void)
143 __sio_out(0x00000300, M32R_SIO0_CR_PORTL
); /* init status */
144 __sio_out(0x00000800, M32R_SIO0_MOD1_PORTL
); /* 8bit */
145 __sio_out(0x00000080, M32R_SIO0_MOD0_PORTL
); /* 1stop non */
146 sio_set_baud_rate(BAUD_RATE
);
147 __sio_out(0x00000000, M32R_SIO0_TRCR_PORTL
);
148 __sio_out(0x00000003, M32R_SIO0_CR_PORTL
); /* RXCEN */
151 static void sio_init(void)
155 tmp
= __sio_in(M32R_SIO0_RXB_PORTL
);
156 tmp
= __sio_in(M32R_SIO0_RXB_PORTL
);
157 tmp
= __sio_in(M32R_SIO0_STS_PORTL
);
158 __sio_out(0x00000003, M32R_SIO0_CR_PORTL
);
161 static void sio_error(int *status
)
163 printk("SIO0 error[%04x]\n", *status
);
166 } while ((*status
= __sio_in(M32R_SIO0_CR_PORTL
)) != 3);
169 #endif /* CONFIG_SERIAL_M32R_PLDSIO */
171 static unsigned int sio_in(struct uart_sio_port
*up
, int offset
)
173 return __sio_in(up
->port
.iobase
+ offset
);
176 static void sio_out(struct uart_sio_port
*up
, int offset
, int value
)
178 __sio_out(value
, up
->port
.iobase
+ offset
);
181 static unsigned int serial_in(struct uart_sio_port
*up
, int offset
)
186 return __sio_in(offset
);
189 static void serial_out(struct uart_sio_port
*up
, int offset
, int value
)
194 __sio_out(value
, offset
);
197 static void m32r_sio_stop_tx(struct uart_port
*port
)
199 struct uart_sio_port
*up
=
200 container_of(port
, struct uart_sio_port
, port
);
202 if (up
->ier
& UART_IER_THRI
) {
203 up
->ier
&= ~UART_IER_THRI
;
204 serial_out(up
, UART_IER
, up
->ier
);
208 static void m32r_sio_start_tx(struct uart_port
*port
)
210 #ifdef CONFIG_SERIAL_M32R_PLDSIO
211 struct uart_sio_port
*up
=
212 container_of(port
, struct uart_sio_port
, port
);
213 struct circ_buf
*xmit
= &up
->port
.state
->xmit
;
215 if (!(up
->ier
& UART_IER_THRI
)) {
216 up
->ier
|= UART_IER_THRI
;
217 serial_out(up
, UART_IER
, up
->ier
);
218 if (!uart_circ_empty(xmit
)) {
219 serial_out(up
, UART_TX
, xmit
->buf
[xmit
->tail
]);
220 xmit
->tail
= (xmit
->tail
+ 1) & (UART_XMIT_SIZE
- 1);
221 up
->port
.icount
.tx
++;
224 while((serial_in(up
, UART_LSR
) & UART_EMPTY
) != UART_EMPTY
);
226 struct uart_sio_port
*up
=
227 container_of(port
, struct uart_sio_port
, port
);
229 if (!(up
->ier
& UART_IER_THRI
)) {
230 up
->ier
|= UART_IER_THRI
;
231 serial_out(up
, UART_IER
, up
->ier
);
236 static void m32r_sio_stop_rx(struct uart_port
*port
)
238 struct uart_sio_port
*up
=
239 container_of(port
, struct uart_sio_port
, port
);
241 up
->ier
&= ~UART_IER_RLSI
;
242 up
->port
.read_status_mask
&= ~UART_LSR_DR
;
243 serial_out(up
, UART_IER
, up
->ier
);
246 static void m32r_sio_enable_ms(struct uart_port
*port
)
248 struct uart_sio_port
*up
=
249 container_of(port
, struct uart_sio_port
, port
);
251 up
->ier
|= UART_IER_MSI
;
252 serial_out(up
, UART_IER
, up
->ier
);
255 static void receive_chars(struct uart_sio_port
*up
, int *status
)
257 struct tty_port
*port
= &up
->port
.state
->port
;
263 ch
= sio_in(up
, SIORXB
);
265 up
->port
.icount
.rx
++;
267 if (unlikely(*status
& (UART_LSR_BI
| UART_LSR_PE
|
268 UART_LSR_FE
| UART_LSR_OE
))) {
270 * For statistics only
272 if (*status
& UART_LSR_BI
) {
273 *status
&= ~(UART_LSR_FE
| UART_LSR_PE
);
274 up
->port
.icount
.brk
++;
276 * We do the SysRQ and SAK checking
277 * here because otherwise the break
278 * may get masked by ignore_status_mask
279 * or read_status_mask.
281 if (uart_handle_break(&up
->port
))
283 } else if (*status
& UART_LSR_PE
)
284 up
->port
.icount
.parity
++;
285 else if (*status
& UART_LSR_FE
)
286 up
->port
.icount
.frame
++;
287 if (*status
& UART_LSR_OE
)
288 up
->port
.icount
.overrun
++;
291 * Mask off conditions which should be ingored.
293 *status
&= up
->port
.read_status_mask
;
295 if (*status
& UART_LSR_BI
) {
296 pr_debug("handling break....\n");
298 } else if (*status
& UART_LSR_PE
)
300 else if (*status
& UART_LSR_FE
)
303 if (uart_handle_sysrq_char(&up
->port
, ch
))
305 if ((*status
& up
->port
.ignore_status_mask
) == 0)
306 tty_insert_flip_char(port
, ch
, flag
);
308 if (*status
& UART_LSR_OE
) {
310 * Overrun is special, since it's reported
311 * immediately, and doesn't affect the current
314 tty_insert_flip_char(port
, 0, TTY_OVERRUN
);
317 *status
= serial_in(up
, UART_LSR
);
318 } while ((*status
& UART_LSR_DR
) && (max_count
-- > 0));
320 spin_unlock(&up
->port
.lock
);
321 tty_flip_buffer_push(port
);
322 spin_lock(&up
->port
.lock
);
325 static void transmit_chars(struct uart_sio_port
*up
)
327 struct circ_buf
*xmit
= &up
->port
.state
->xmit
;
330 if (up
->port
.x_char
) {
331 #ifndef CONFIG_SERIAL_M32R_PLDSIO /* XXX */
332 serial_out(up
, UART_TX
, up
->port
.x_char
);
334 up
->port
.icount
.tx
++;
338 if (uart_circ_empty(xmit
) || uart_tx_stopped(&up
->port
)) {
339 m32r_sio_stop_tx(&up
->port
);
343 count
= up
->port
.fifosize
;
345 serial_out(up
, UART_TX
, xmit
->buf
[xmit
->tail
]);
346 xmit
->tail
= (xmit
->tail
+ 1) & (UART_XMIT_SIZE
- 1);
347 up
->port
.icount
.tx
++;
348 if (uart_circ_empty(xmit
))
350 while (!(serial_in(up
, UART_LSR
) & UART_LSR_THRE
));
352 } while (--count
> 0);
354 if (uart_circ_chars_pending(xmit
) < WAKEUP_CHARS
)
355 uart_write_wakeup(&up
->port
);
357 pr_debug("THRE...\n");
359 if (uart_circ_empty(xmit
))
360 m32r_sio_stop_tx(&up
->port
);
364 * This handles the interrupt from one port.
366 static inline void m32r_sio_handle_port(struct uart_sio_port
*up
,
369 pr_debug("status = %x...\n", status
);
372 receive_chars(up
, &status
);
378 * This is the serial driver's interrupt routine.
380 * Arjan thinks the old way was overly complex, so it got simplified.
381 * Alan disagrees, saying that need the complexity to handle the weird
382 * nature of ISA shared interrupts. (This is a special exception.)
384 * In order to handle ISA shared interrupts properly, we need to check
385 * that all ports have been serviced, and therefore the ISA interrupt
386 * line has been de-asserted.
388 * This means we need to loop through all ports. checking that they
389 * don't have an interrupt pending.
391 static irqreturn_t
m32r_sio_interrupt(int irq
, void *dev_id
)
393 struct irq_info
*i
= dev_id
;
394 struct list_head
*l
, *end
= NULL
;
395 int pass_counter
= 0;
397 pr_debug("m32r_sio_interrupt(%d)...\n", irq
);
399 #ifdef CONFIG_SERIAL_M32R_PLDSIO
400 // if (irq == PLD_IRQ_SIO0_SND)
401 // irq = PLD_IRQ_SIO0_RCV;
403 if (irq
== M32R_IRQ_SIO0_S
)
404 irq
= M32R_IRQ_SIO0_R
;
411 struct uart_sio_port
*up
;
414 up
= list_entry(l
, struct uart_sio_port
, list
);
416 sts
= sio_in(up
, SIOSTS
);
418 spin_lock(&up
->port
.lock
);
419 m32r_sio_handle_port(up
, sts
);
420 spin_unlock(&up
->port
.lock
);
423 } else if (end
== NULL
)
428 if (l
== i
->head
&& pass_counter
++ > PASS_LIMIT
) {
435 spin_unlock(&i
->lock
);
443 * To support ISA shared interrupts, we need to have one interrupt
444 * handler that ensures that the IRQ line has been deasserted
445 * before returning. Failing to do this will result in the IRQ
446 * line being stuck active, and, since ISA irqs are edge triggered,
447 * no more IRQs will be seen.
449 static void serial_do_unlink(struct irq_info
*i
, struct uart_sio_port
*up
)
451 spin_lock_irq(&i
->lock
);
453 if (!list_empty(i
->head
)) {
454 if (i
->head
== &up
->list
)
455 i
->head
= i
->head
->next
;
458 BUG_ON(i
->head
!= &up
->list
);
462 spin_unlock_irq(&i
->lock
);
465 static int serial_link_irq_chain(struct uart_sio_port
*up
)
467 struct irq_info
*i
= irq_lists
+ up
->port
.irq
;
468 int ret
, irq_flags
= 0;
470 spin_lock_irq(&i
->lock
);
473 list_add(&up
->list
, i
->head
);
474 spin_unlock_irq(&i
->lock
);
478 INIT_LIST_HEAD(&up
->list
);
480 spin_unlock_irq(&i
->lock
);
482 ret
= request_irq(up
->port
.irq
, m32r_sio_interrupt
,
483 irq_flags
, "SIO0-RX", i
);
484 ret
|= request_irq(up
->port
.irq
+ 1, m32r_sio_interrupt
,
485 irq_flags
, "SIO0-TX", i
);
487 serial_do_unlink(i
, up
);
493 static void serial_unlink_irq_chain(struct uart_sio_port
*up
)
495 struct irq_info
*i
= irq_lists
+ up
->port
.irq
;
497 BUG_ON(i
->head
== NULL
);
499 if (list_empty(i
->head
)) {
500 free_irq(up
->port
.irq
, i
);
501 free_irq(up
->port
.irq
+ 1, i
);
504 serial_do_unlink(i
, up
);
508 * This function is used to handle ports that do not have an interrupt.
510 static void m32r_sio_timeout(struct timer_list
*t
)
512 struct uart_sio_port
*up
= from_timer(up
, t
, timer
);
513 unsigned int timeout
;
516 sts
= sio_in(up
, SIOSTS
);
518 spin_lock(&up
->port
.lock
);
519 m32r_sio_handle_port(up
, sts
);
520 spin_unlock(&up
->port
.lock
);
523 timeout
= up
->port
.timeout
;
524 timeout
= timeout
> 6 ? (timeout
/ 2 - 2) : 1;
525 mod_timer(&up
->timer
, jiffies
+ timeout
);
528 static unsigned int m32r_sio_tx_empty(struct uart_port
*port
)
530 struct uart_sio_port
*up
=
531 container_of(port
, struct uart_sio_port
, port
);
535 spin_lock_irqsave(&up
->port
.lock
, flags
);
536 ret
= serial_in(up
, UART_LSR
) & UART_LSR_TEMT
? TIOCSER_TEMT
: 0;
537 spin_unlock_irqrestore(&up
->port
.lock
, flags
);
542 static unsigned int m32r_sio_get_mctrl(struct uart_port
*port
)
547 static void m32r_sio_set_mctrl(struct uart_port
*port
, unsigned int mctrl
)
552 static void m32r_sio_break_ctl(struct uart_port
*port
, int break_state
)
557 static int m32r_sio_startup(struct uart_port
*port
)
559 struct uart_sio_port
*up
=
560 container_of(port
, struct uart_sio_port
, port
);
566 * If the "interrupt" for this port doesn't correspond with any
567 * hardware interrupt, we use a timer-based system. The original
568 * driver used to do this with IRQ0.
571 unsigned int timeout
= up
->port
.timeout
;
573 timeout
= timeout
> 6 ? (timeout
/ 2 - 2) : 1;
575 mod_timer(&up
->timer
, jiffies
+ timeout
);
577 retval
= serial_link_irq_chain(up
);
583 * Finally, enable interrupts. Note: Modem status interrupts
584 * are set via set_termios(), which will be occurring imminently
585 * anyway, so we don't enable them here.
587 * - M32R_PLDSIO: 0x04
589 up
->ier
= UART_IER_MSI
| UART_IER_RLSI
| UART_IER_RDI
;
590 sio_out(up
, SIOTRCR
, up
->ier
);
593 * And clear the interrupt registers again for luck.
600 static void m32r_sio_shutdown(struct uart_port
*port
)
602 struct uart_sio_port
*up
=
603 container_of(port
, struct uart_sio_port
, port
);
606 * Disable interrupts from this port
609 sio_out(up
, SIOTRCR
, 0);
612 * Disable break condition and FIFOs
618 del_timer_sync(&up
->timer
);
620 serial_unlink_irq_chain(up
);
623 static unsigned int m32r_sio_get_divisor(struct uart_port
*port
,
626 return uart_get_divisor(port
, baud
);
629 static void m32r_sio_set_termios(struct uart_port
*port
,
630 struct ktermios
*termios
, struct ktermios
*old
)
632 struct uart_sio_port
*up
=
633 container_of(port
, struct uart_sio_port
, port
);
634 unsigned char cval
= 0;
636 unsigned int baud
, quot
;
638 switch (termios
->c_cflag
& CSIZE
) {
640 cval
= UART_LCR_WLEN5
;
643 cval
= UART_LCR_WLEN6
;
646 cval
= UART_LCR_WLEN7
;
650 cval
= UART_LCR_WLEN8
;
654 if (termios
->c_cflag
& CSTOPB
)
655 cval
|= UART_LCR_STOP
;
656 if (termios
->c_cflag
& PARENB
)
657 cval
|= UART_LCR_PARITY
;
658 if (!(termios
->c_cflag
& PARODD
))
659 cval
|= UART_LCR_EPAR
;
661 if (termios
->c_cflag
& CMSPAR
)
662 cval
|= UART_LCR_SPAR
;
666 * Ask the core to calculate the divisor for us.
668 #ifdef CONFIG_SERIAL_M32R_PLDSIO
669 baud
= uart_get_baud_rate(port
, termios
, old
, 0, port
->uartclk
/4);
671 baud
= uart_get_baud_rate(port
, termios
, old
, 0, port
->uartclk
/16);
673 quot
= m32r_sio_get_divisor(port
, baud
);
676 * Ok, we're now changing the port state. Do it with
677 * interrupts disabled.
679 spin_lock_irqsave(&up
->port
.lock
, flags
);
681 sio_set_baud_rate(baud
);
684 * Update the per-port timeout.
686 uart_update_timeout(port
, termios
->c_cflag
, baud
);
688 up
->port
.read_status_mask
= UART_LSR_OE
| UART_LSR_THRE
| UART_LSR_DR
;
689 if (termios
->c_iflag
& INPCK
)
690 up
->port
.read_status_mask
|= UART_LSR_FE
| UART_LSR_PE
;
691 if (termios
->c_iflag
& (IGNBRK
| BRKINT
| PARMRK
))
692 up
->port
.read_status_mask
|= UART_LSR_BI
;
695 * Characteres to ignore
697 up
->port
.ignore_status_mask
= 0;
698 if (termios
->c_iflag
& IGNPAR
)
699 up
->port
.ignore_status_mask
|= UART_LSR_PE
| UART_LSR_FE
;
700 if (termios
->c_iflag
& IGNBRK
) {
701 up
->port
.ignore_status_mask
|= UART_LSR_BI
;
703 * If we're ignoring parity and break indicators,
704 * ignore overruns too (for real raw support).
706 if (termios
->c_iflag
& IGNPAR
)
707 up
->port
.ignore_status_mask
|= UART_LSR_OE
;
711 * ignore all characters if CREAD is not set
713 if ((termios
->c_cflag
& CREAD
) == 0)
714 up
->port
.ignore_status_mask
|= UART_LSR_DR
;
717 * CTS flow control flag and modem status interrupts
719 up
->ier
&= ~UART_IER_MSI
;
720 if (UART_ENABLE_MS(&up
->port
, termios
->c_cflag
))
721 up
->ier
|= UART_IER_MSI
;
723 serial_out(up
, UART_IER
, up
->ier
);
725 spin_unlock_irqrestore(&up
->port
.lock
, flags
);
729 * Resource handling. This is complicated by the fact that resources
730 * depend on the port type. Maybe we should be claiming the standard
731 * 8250 ports, and then trying to get other resources as necessary?
734 m32r_sio_request_std_resource(struct uart_sio_port
*up
, struct resource
**res
)
736 unsigned int size
= 8 << up
->port
.regshift
;
737 #ifndef CONFIG_SERIAL_M32R_PLDSIO
742 switch (up
->port
.iotype
) {
744 if (up
->port
.mapbase
) {
745 #ifdef CONFIG_SERIAL_M32R_PLDSIO
746 *res
= request_mem_region(up
->port
.mapbase
, size
, "serial");
748 start
= up
->port
.mapbase
;
749 *res
= request_mem_region(start
, size
, "serial");
757 *res
= request_region(up
->port
.iobase
, size
, "serial");
765 static void m32r_sio_release_port(struct uart_port
*port
)
767 struct uart_sio_port
*up
=
768 container_of(port
, struct uart_sio_port
, port
);
769 unsigned long start
, offset
= 0, size
= 0;
771 size
<<= up
->port
.regshift
;
773 switch (up
->port
.iotype
) {
775 if (up
->port
.mapbase
) {
779 iounmap(up
->port
.membase
);
780 up
->port
.membase
= NULL
;
782 start
= up
->port
.mapbase
;
785 release_mem_region(start
+ offset
, size
);
786 release_mem_region(start
, 8 << up
->port
.regshift
);
791 start
= up
->port
.iobase
;
794 release_region(start
+ offset
, size
);
795 release_region(start
+ offset
, 8 << up
->port
.regshift
);
803 static int m32r_sio_request_port(struct uart_port
*port
)
805 struct uart_sio_port
*up
=
806 container_of(port
, struct uart_sio_port
, port
);
807 struct resource
*res
= NULL
;
810 ret
= m32r_sio_request_std_resource(up
, &res
);
813 * If we have a mapbase, then request that as well.
815 if (ret
== 0 && up
->port
.flags
& UPF_IOREMAP
) {
816 int size
= resource_size(res
);
818 up
->port
.membase
= ioremap(up
->port
.mapbase
, size
);
819 if (!up
->port
.membase
)
825 release_resource(res
);
831 static void m32r_sio_config_port(struct uart_port
*port
, int unused
)
833 struct uart_sio_port
*up
=
834 container_of(port
, struct uart_sio_port
, port
);
837 spin_lock_irqsave(&up
->port
.lock
, flags
);
839 up
->port
.fifosize
= 1;
841 spin_unlock_irqrestore(&up
->port
.lock
, flags
);
845 m32r_sio_verify_port(struct uart_port
*port
, struct serial_struct
*ser
)
847 if (ser
->irq
>= nr_irqs
|| ser
->irq
< 0 || ser
->baud_base
< 9600)
852 static const struct uart_ops m32r_sio_pops
= {
853 .tx_empty
= m32r_sio_tx_empty
,
854 .set_mctrl
= m32r_sio_set_mctrl
,
855 .get_mctrl
= m32r_sio_get_mctrl
,
856 .stop_tx
= m32r_sio_stop_tx
,
857 .start_tx
= m32r_sio_start_tx
,
858 .stop_rx
= m32r_sio_stop_rx
,
859 .enable_ms
= m32r_sio_enable_ms
,
860 .break_ctl
= m32r_sio_break_ctl
,
861 .startup
= m32r_sio_startup
,
862 .shutdown
= m32r_sio_shutdown
,
863 .set_termios
= m32r_sio_set_termios
,
864 .release_port
= m32r_sio_release_port
,
865 .request_port
= m32r_sio_request_port
,
866 .config_port
= m32r_sio_config_port
,
867 .verify_port
= m32r_sio_verify_port
,
870 static struct uart_sio_port m32r_sio_ports
[UART_NR
];
872 static void __init
m32r_sio_init_ports(void)
874 struct uart_sio_port
*up
;
875 static int first
= 1;
882 for (i
= 0, up
= m32r_sio_ports
; i
< UART_NR
; i
++, up
++) {
883 up
->port
.iobase
= old_serial_port
[i
].port
;
884 up
->port
.irq
= irq_canonicalize(old_serial_port
[i
].irq
);
885 up
->port
.uartclk
= BAUD_RATE
* 16;
886 up
->port
.flags
= UPF_BOOT_AUTOCONF
| UPF_SKIP_TEST
;
887 up
->port
.membase
= 0;
889 up
->port
.regshift
= 0;
890 up
->port
.ops
= &m32r_sio_pops
;
894 static void __init
m32r_sio_register_ports(struct uart_driver
*drv
)
898 m32r_sio_init_ports();
900 for (i
= 0; i
< UART_NR
; i
++) {
901 struct uart_sio_port
*up
= &m32r_sio_ports
[i
];
904 up
->port
.ops
= &m32r_sio_pops
;
905 timer_setup(&up
->timer
, m32r_sio_timeout
, 0);
907 uart_add_one_port(drv
, &up
->port
);
911 #ifdef CONFIG_SERIAL_M32R_SIO_CONSOLE
914 * Wait for transmitter & holding register to empty
916 static void wait_for_xmitr(struct uart_sio_port
*up
)
918 unsigned int status
, tmout
= 10000;
920 /* Wait up to 10ms for the character(s) to be sent. */
922 status
= sio_in(up
, SIOSTS
);
927 } while ((status
& UART_EMPTY
) != UART_EMPTY
);
929 /* Wait up to 1s for flow control if necessary */
930 if (up
->port
.flags
& UPF_CONS_FLOW
) {
937 static void m32r_sio_console_putchar(struct uart_port
*port
, int ch
)
939 struct uart_sio_port
*up
=
940 container_of(port
, struct uart_sio_port
, port
);
943 sio_out(up
, SIOTXB
, ch
);
947 * Print a string to the serial port trying not to disturb
948 * any possible real use of the port...
950 * The console_lock must be held when we get here.
952 static void m32r_sio_console_write(struct console
*co
, const char *s
,
955 struct uart_sio_port
*up
= &m32r_sio_ports
[co
->index
];
959 * First save the UER then disable the interrupts
961 ier
= sio_in(up
, SIOTRCR
);
962 sio_out(up
, SIOTRCR
, 0);
964 uart_console_write(&up
->port
, s
, count
, m32r_sio_console_putchar
);
967 * Finally, wait for transmitter to become empty
968 * and restore the IER
971 sio_out(up
, SIOTRCR
, ier
);
974 static int __init
m32r_sio_console_setup(struct console
*co
, char *options
)
976 struct uart_port
*port
;
983 * Check whether an invalid uart number has been specified, and
984 * if so, search for the first available port that does have
987 if (co
->index
>= UART_NR
)
989 port
= &m32r_sio_ports
[co
->index
].port
;
994 spin_lock_init(&port
->lock
);
997 uart_parse_options(options
, &baud
, &parity
, &bits
, &flow
);
999 return uart_set_options(port
, co
, baud
, parity
, bits
, flow
);
1002 static struct uart_driver m32r_sio_reg
;
1003 static struct console m32r_sio_console
= {
1005 .write
= m32r_sio_console_write
,
1006 .device
= uart_console_device
,
1007 .setup
= m32r_sio_console_setup
,
1008 .flags
= CON_PRINTBUFFER
,
1010 .data
= &m32r_sio_reg
,
1013 static int __init
m32r_sio_console_init(void)
1017 m32r_sio_init_ports();
1018 register_console(&m32r_sio_console
);
1021 console_initcall(m32r_sio_console_init
);
1023 #define M32R_SIO_CONSOLE &m32r_sio_console
1025 #define M32R_SIO_CONSOLE NULL
1028 static struct uart_driver m32r_sio_reg
= {
1029 .owner
= THIS_MODULE
,
1030 .driver_name
= "sio",
1035 .cons
= M32R_SIO_CONSOLE
,
1038 static int __init
m32r_sio_init(void)
1042 printk(KERN_INFO
"Serial: M32R SIO driver\n");
1044 for (i
= 0; i
< nr_irqs
; i
++)
1045 spin_lock_init(&irq_lists
[i
].lock
);
1047 ret
= uart_register_driver(&m32r_sio_reg
);
1049 m32r_sio_register_ports(&m32r_sio_reg
);
1053 device_initcall(m32r_sio_init
);