1 /****************************************************************************/
4 * mcf.c -- Freescale ColdFire UART driver
6 * (C) Copyright 2003-2007, Greg Ungerer <gerg@snapgear.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
14 /****************************************************************************/
16 #include <linux/kernel.h>
17 #include <linux/init.h>
18 #include <linux/interrupt.h>
19 #include <linux/module.h>
20 #include <linux/console.h>
21 #include <linux/tty.h>
22 #include <linux/tty_flip.h>
23 #include <linux/serial.h>
24 #include <linux/serial_core.h>
26 #include <linux/uaccess.h>
27 #include <linux/platform_device.h>
28 #include <asm/coldfire.h>
29 #include <asm/mcfsim.h>
30 #include <asm/mcfuart.h>
31 #include <asm/nettel.h>
33 /****************************************************************************/
36 * Some boards implement the DTR/DCD lines using GPIO lines, most
37 * don't. Dummy out the access macros for those that don't. Those
38 * that do should define these macros somewhere in there board
39 * specific inlude files.
41 #if !defined(mcf_getppdcd)
42 #define mcf_getppdcd(p) (1)
44 #if !defined(mcf_getppdtr)
45 #define mcf_getppdtr(p) (1)
47 #if !defined(mcf_setppdtr)
48 #define mcf_setppdtr(p, v) do { } while (0)
51 /****************************************************************************/
54 * Local per-uart structure.
57 struct uart_port port
;
58 unsigned int sigs
; /* Local copy of line sigs */
59 unsigned char imr
; /* Local IMR mirror */
60 struct serial_rs485 rs485
; /* RS485 settings */
63 /****************************************************************************/
65 static unsigned int mcf_tx_empty(struct uart_port
*port
)
67 return (readb(port
->membase
+ MCFUART_USR
) & MCFUART_USR_TXEMPTY
) ?
71 /****************************************************************************/
73 static unsigned int mcf_get_mctrl(struct uart_port
*port
)
75 struct mcf_uart
*pp
= container_of(port
, struct mcf_uart
, port
);
78 sigs
= (readb(port
->membase
+ MCFUART_UIPR
) & MCFUART_UIPR_CTS
) ?
80 sigs
|= (pp
->sigs
& TIOCM_RTS
);
81 sigs
|= (mcf_getppdcd(port
->line
) ? TIOCM_CD
: 0);
82 sigs
|= (mcf_getppdtr(port
->line
) ? TIOCM_DTR
: 0);
87 /****************************************************************************/
89 static void mcf_set_mctrl(struct uart_port
*port
, unsigned int sigs
)
91 struct mcf_uart
*pp
= container_of(port
, struct mcf_uart
, port
);
94 mcf_setppdtr(port
->line
, (sigs
& TIOCM_DTR
));
96 writeb(MCFUART_UOP_RTS
, port
->membase
+ MCFUART_UOP1
);
98 writeb(MCFUART_UOP_RTS
, port
->membase
+ MCFUART_UOP0
);
101 /****************************************************************************/
103 static void mcf_start_tx(struct uart_port
*port
)
105 struct mcf_uart
*pp
= container_of(port
, struct mcf_uart
, port
);
107 if (pp
->rs485
.flags
& SER_RS485_ENABLED
) {
108 /* Enable Transmitter */
109 writeb(MCFUART_UCR_TXENABLE
, port
->membase
+ MCFUART_UCR
);
110 /* Manually assert RTS */
111 writeb(MCFUART_UOP_RTS
, port
->membase
+ MCFUART_UOP1
);
113 pp
->imr
|= MCFUART_UIR_TXREADY
;
114 writeb(pp
->imr
, port
->membase
+ MCFUART_UIMR
);
117 /****************************************************************************/
119 static void mcf_stop_tx(struct uart_port
*port
)
121 struct mcf_uart
*pp
= container_of(port
, struct mcf_uart
, port
);
123 pp
->imr
&= ~MCFUART_UIR_TXREADY
;
124 writeb(pp
->imr
, port
->membase
+ MCFUART_UIMR
);
127 /****************************************************************************/
129 static void mcf_stop_rx(struct uart_port
*port
)
131 struct mcf_uart
*pp
= container_of(port
, struct mcf_uart
, port
);
133 pp
->imr
&= ~MCFUART_UIR_RXREADY
;
134 writeb(pp
->imr
, port
->membase
+ MCFUART_UIMR
);
137 /****************************************************************************/
139 static void mcf_break_ctl(struct uart_port
*port
, int break_state
)
143 spin_lock_irqsave(&port
->lock
, flags
);
144 if (break_state
== -1)
145 writeb(MCFUART_UCR_CMDBREAKSTART
, port
->membase
+ MCFUART_UCR
);
147 writeb(MCFUART_UCR_CMDBREAKSTOP
, port
->membase
+ MCFUART_UCR
);
148 spin_unlock_irqrestore(&port
->lock
, flags
);
151 /****************************************************************************/
153 static void mcf_enable_ms(struct uart_port
*port
)
157 /****************************************************************************/
159 static int mcf_startup(struct uart_port
*port
)
161 struct mcf_uart
*pp
= container_of(port
, struct mcf_uart
, port
);
164 spin_lock_irqsave(&port
->lock
, flags
);
166 /* Reset UART, get it into known state... */
167 writeb(MCFUART_UCR_CMDRESETRX
, port
->membase
+ MCFUART_UCR
);
168 writeb(MCFUART_UCR_CMDRESETTX
, port
->membase
+ MCFUART_UCR
);
170 /* Enable the UART transmitter and receiver */
171 writeb(MCFUART_UCR_RXENABLE
| MCFUART_UCR_TXENABLE
,
172 port
->membase
+ MCFUART_UCR
);
174 /* Enable RX interrupts now */
175 pp
->imr
= MCFUART_UIR_RXREADY
;
176 writeb(pp
->imr
, port
->membase
+ MCFUART_UIMR
);
178 spin_unlock_irqrestore(&port
->lock
, flags
);
183 /****************************************************************************/
185 static void mcf_shutdown(struct uart_port
*port
)
187 struct mcf_uart
*pp
= container_of(port
, struct mcf_uart
, port
);
190 spin_lock_irqsave(&port
->lock
, flags
);
192 /* Disable all interrupts now */
194 writeb(pp
->imr
, port
->membase
+ MCFUART_UIMR
);
196 /* Disable UART transmitter and receiver */
197 writeb(MCFUART_UCR_CMDRESETRX
, port
->membase
+ MCFUART_UCR
);
198 writeb(MCFUART_UCR_CMDRESETTX
, port
->membase
+ MCFUART_UCR
);
200 spin_unlock_irqrestore(&port
->lock
, flags
);
203 /****************************************************************************/
205 static void mcf_set_termios(struct uart_port
*port
, struct ktermios
*termios
,
206 struct ktermios
*old
)
208 struct mcf_uart
*pp
= container_of(port
, struct mcf_uart
, port
);
210 unsigned int baud
, baudclk
;
211 #if defined(CONFIG_M5272)
214 unsigned char mr1
, mr2
;
216 baud
= uart_get_baud_rate(port
, termios
, old
, 0, 230400);
217 #if defined(CONFIG_M5272)
218 baudclk
= (MCF_BUSCLK
/ baud
) / 32;
219 baudfr
= (((MCF_BUSCLK
/ baud
) + 1) / 2) % 16;
221 baudclk
= ((MCF_BUSCLK
/ baud
) + 16) / 32;
224 mr1
= MCFUART_MR1_RXIRQRDY
| MCFUART_MR1_RXERRCHAR
;
227 switch (termios
->c_cflag
& CSIZE
) {
228 case CS5
: mr1
|= MCFUART_MR1_CS5
; break;
229 case CS6
: mr1
|= MCFUART_MR1_CS6
; break;
230 case CS7
: mr1
|= MCFUART_MR1_CS7
; break;
232 default: mr1
|= MCFUART_MR1_CS8
; break;
235 if (termios
->c_cflag
& PARENB
) {
236 if (termios
->c_cflag
& CMSPAR
) {
237 if (termios
->c_cflag
& PARODD
)
238 mr1
|= MCFUART_MR1_PARITYMARK
;
240 mr1
|= MCFUART_MR1_PARITYSPACE
;
242 if (termios
->c_cflag
& PARODD
)
243 mr1
|= MCFUART_MR1_PARITYODD
;
245 mr1
|= MCFUART_MR1_PARITYEVEN
;
248 mr1
|= MCFUART_MR1_PARITYNONE
;
251 if (termios
->c_cflag
& CSTOPB
)
252 mr2
|= MCFUART_MR2_STOP2
;
254 mr2
|= MCFUART_MR2_STOP1
;
256 if (termios
->c_cflag
& CRTSCTS
) {
257 mr1
|= MCFUART_MR1_RXRTS
;
258 mr2
|= MCFUART_MR2_TXCTS
;
261 if (pp
->rs485
.flags
& SER_RS485_ENABLED
) {
262 dev_dbg(port
->dev
, "Setting UART to RS485\n");
263 mr2
|= MCFUART_MR2_TXRTS
;
266 spin_lock_irqsave(&port
->lock
, flags
);
267 uart_update_timeout(port
, termios
->c_cflag
, baud
);
268 writeb(MCFUART_UCR_CMDRESETRX
, port
->membase
+ MCFUART_UCR
);
269 writeb(MCFUART_UCR_CMDRESETTX
, port
->membase
+ MCFUART_UCR
);
270 writeb(MCFUART_UCR_CMDRESETMRPTR
, port
->membase
+ MCFUART_UCR
);
271 writeb(mr1
, port
->membase
+ MCFUART_UMR
);
272 writeb(mr2
, port
->membase
+ MCFUART_UMR
);
273 writeb((baudclk
& 0xff00) >> 8, port
->membase
+ MCFUART_UBG1
);
274 writeb((baudclk
& 0xff), port
->membase
+ MCFUART_UBG2
);
275 #if defined(CONFIG_M5272)
276 writeb((baudfr
& 0x0f), port
->membase
+ MCFUART_UFPD
);
278 writeb(MCFUART_UCSR_RXCLKTIMER
| MCFUART_UCSR_TXCLKTIMER
,
279 port
->membase
+ MCFUART_UCSR
);
280 writeb(MCFUART_UCR_RXENABLE
| MCFUART_UCR_TXENABLE
,
281 port
->membase
+ MCFUART_UCR
);
282 spin_unlock_irqrestore(&port
->lock
, flags
);
285 /****************************************************************************/
287 static void mcf_rx_chars(struct mcf_uart
*pp
)
289 struct uart_port
*port
= &pp
->port
;
290 unsigned char status
, ch
, flag
;
292 while ((status
= readb(port
->membase
+ MCFUART_USR
)) & MCFUART_USR_RXREADY
) {
293 ch
= readb(port
->membase
+ MCFUART_URB
);
297 if (status
& MCFUART_USR_RXERR
) {
298 writeb(MCFUART_UCR_CMDRESETERR
,
299 port
->membase
+ MCFUART_UCR
);
301 if (status
& MCFUART_USR_RXBREAK
) {
303 if (uart_handle_break(port
))
305 } else if (status
& MCFUART_USR_RXPARITY
) {
306 port
->icount
.parity
++;
307 } else if (status
& MCFUART_USR_RXOVERRUN
) {
308 port
->icount
.overrun
++;
309 } else if (status
& MCFUART_USR_RXFRAMING
) {
310 port
->icount
.frame
++;
313 status
&= port
->read_status_mask
;
315 if (status
& MCFUART_USR_RXBREAK
)
317 else if (status
& MCFUART_USR_RXPARITY
)
319 else if (status
& MCFUART_USR_RXFRAMING
)
323 if (uart_handle_sysrq_char(port
, ch
))
325 uart_insert_char(port
, status
, MCFUART_USR_RXOVERRUN
, ch
, flag
);
328 spin_unlock(&port
->lock
);
329 tty_flip_buffer_push(&port
->state
->port
);
330 spin_lock(&port
->lock
);
333 /****************************************************************************/
335 static void mcf_tx_chars(struct mcf_uart
*pp
)
337 struct uart_port
*port
= &pp
->port
;
338 struct circ_buf
*xmit
= &port
->state
->xmit
;
341 /* Send special char - probably flow control */
342 writeb(port
->x_char
, port
->membase
+ MCFUART_UTB
);
348 while (readb(port
->membase
+ MCFUART_USR
) & MCFUART_USR_TXREADY
) {
349 if (xmit
->head
== xmit
->tail
)
351 writeb(xmit
->buf
[xmit
->tail
], port
->membase
+ MCFUART_UTB
);
352 xmit
->tail
= (xmit
->tail
+ 1) & (UART_XMIT_SIZE
-1);
356 if (uart_circ_chars_pending(xmit
) < WAKEUP_CHARS
)
357 uart_write_wakeup(port
);
359 if (xmit
->head
== xmit
->tail
) {
360 pp
->imr
&= ~MCFUART_UIR_TXREADY
;
361 writeb(pp
->imr
, port
->membase
+ MCFUART_UIMR
);
362 /* Disable TX to negate RTS automatically */
363 if (pp
->rs485
.flags
& SER_RS485_ENABLED
)
364 writeb(MCFUART_UCR_TXDISABLE
,
365 port
->membase
+ MCFUART_UCR
);
369 /****************************************************************************/
371 static irqreturn_t
mcf_interrupt(int irq
, void *data
)
373 struct uart_port
*port
= data
;
374 struct mcf_uart
*pp
= container_of(port
, struct mcf_uart
, port
);
376 irqreturn_t ret
= IRQ_NONE
;
378 isr
= readb(port
->membase
+ MCFUART_UISR
) & pp
->imr
;
380 spin_lock(&port
->lock
);
381 if (isr
& MCFUART_UIR_RXREADY
) {
385 if (isr
& MCFUART_UIR_TXREADY
) {
389 spin_unlock(&port
->lock
);
394 /****************************************************************************/
396 static void mcf_config_port(struct uart_port
*port
, int flags
)
398 port
->type
= PORT_MCF
;
399 port
->fifosize
= MCFUART_TXFIFOSIZE
;
401 /* Clear mask, so no surprise interrupts. */
402 writeb(0, port
->membase
+ MCFUART_UIMR
);
404 if (request_irq(port
->irq
, mcf_interrupt
, 0, "UART", port
))
405 printk(KERN_ERR
"MCF: unable to attach ColdFire UART %d "
406 "interrupt vector=%d\n", port
->line
, port
->irq
);
409 /****************************************************************************/
411 static const char *mcf_type(struct uart_port
*port
)
413 return (port
->type
== PORT_MCF
) ? "ColdFire UART" : NULL
;
416 /****************************************************************************/
418 static int mcf_request_port(struct uart_port
*port
)
420 /* UARTs always present */
424 /****************************************************************************/
426 static void mcf_release_port(struct uart_port
*port
)
428 /* Nothing to release... */
431 /****************************************************************************/
433 static int mcf_verify_port(struct uart_port
*port
, struct serial_struct
*ser
)
435 if ((ser
->type
!= PORT_UNKNOWN
) && (ser
->type
!= PORT_MCF
))
440 /****************************************************************************/
442 /* Enable or disable the RS485 support */
443 static void mcf_config_rs485(struct uart_port
*port
, struct serial_rs485
*rs485
)
445 struct mcf_uart
*pp
= container_of(port
, struct mcf_uart
, port
);
447 unsigned char mr1
, mr2
;
449 spin_lock_irqsave(&port
->lock
, flags
);
450 /* Get mode registers */
451 mr1
= readb(port
->membase
+ MCFUART_UMR
);
452 mr2
= readb(port
->membase
+ MCFUART_UMR
);
453 if (rs485
->flags
& SER_RS485_ENABLED
) {
454 dev_dbg(port
->dev
, "Setting UART to RS485\n");
455 /* Automatically negate RTS after TX completes */
456 mr2
|= MCFUART_MR2_TXRTS
;
458 dev_dbg(port
->dev
, "Setting UART to RS232\n");
459 mr2
&= ~MCFUART_MR2_TXRTS
;
461 writeb(mr1
, port
->membase
+ MCFUART_UMR
);
462 writeb(mr2
, port
->membase
+ MCFUART_UMR
);
464 spin_unlock_irqrestore(&port
->lock
, flags
);
467 static int mcf_ioctl(struct uart_port
*port
, unsigned int cmd
,
472 struct serial_rs485 rs485
;
473 if (copy_from_user(&rs485
, (struct serial_rs485
*)arg
,
474 sizeof(struct serial_rs485
)))
476 mcf_config_rs485(port
, &rs485
);
480 struct mcf_uart
*pp
= container_of(port
, struct mcf_uart
, port
);
481 if (copy_to_user((struct serial_rs485
*)arg
, &pp
->rs485
,
482 sizeof(struct serial_rs485
)))
492 /****************************************************************************/
495 * Define the basic serial functions we support.
497 static const struct uart_ops mcf_uart_ops
= {
498 .tx_empty
= mcf_tx_empty
,
499 .get_mctrl
= mcf_get_mctrl
,
500 .set_mctrl
= mcf_set_mctrl
,
501 .start_tx
= mcf_start_tx
,
502 .stop_tx
= mcf_stop_tx
,
503 .stop_rx
= mcf_stop_rx
,
504 .enable_ms
= mcf_enable_ms
,
505 .break_ctl
= mcf_break_ctl
,
506 .startup
= mcf_startup
,
507 .shutdown
= mcf_shutdown
,
508 .set_termios
= mcf_set_termios
,
510 .request_port
= mcf_request_port
,
511 .release_port
= mcf_release_port
,
512 .config_port
= mcf_config_port
,
513 .verify_port
= mcf_verify_port
,
517 static struct mcf_uart mcf_ports
[4];
519 #define MCF_MAXPORTS ARRAY_SIZE(mcf_ports)
521 /****************************************************************************/
522 #if defined(CONFIG_SERIAL_MCF_CONSOLE)
523 /****************************************************************************/
525 int __init
early_mcf_setup(struct mcf_platform_uart
*platp
)
527 struct uart_port
*port
;
530 for (i
= 0; ((i
< MCF_MAXPORTS
) && (platp
[i
].mapbase
)); i
++) {
531 port
= &mcf_ports
[i
].port
;
534 port
->type
= PORT_MCF
;
535 port
->mapbase
= platp
[i
].mapbase
;
536 port
->membase
= (platp
[i
].membase
) ? platp
[i
].membase
:
537 (unsigned char __iomem
*) port
->mapbase
;
538 port
->iotype
= SERIAL_IO_MEM
;
539 port
->irq
= platp
[i
].irq
;
540 port
->uartclk
= MCF_BUSCLK
;
541 port
->flags
= ASYNC_BOOT_AUTOCONF
;
542 port
->ops
= &mcf_uart_ops
;
548 /****************************************************************************/
550 static void mcf_console_putc(struct console
*co
, const char c
)
552 struct uart_port
*port
= &(mcf_ports
+ co
->index
)->port
;
555 for (i
= 0; (i
< 0x10000); i
++) {
556 if (readb(port
->membase
+ MCFUART_USR
) & MCFUART_USR_TXREADY
)
559 writeb(c
, port
->membase
+ MCFUART_UTB
);
560 for (i
= 0; (i
< 0x10000); i
++) {
561 if (readb(port
->membase
+ MCFUART_USR
) & MCFUART_USR_TXREADY
)
566 /****************************************************************************/
568 static void mcf_console_write(struct console
*co
, const char *s
, unsigned int count
)
570 for (; (count
); count
--, s
++) {
571 mcf_console_putc(co
, *s
);
573 mcf_console_putc(co
, '\r');
577 /****************************************************************************/
579 static int __init
mcf_console_setup(struct console
*co
, char *options
)
581 struct uart_port
*port
;
582 int baud
= CONFIG_SERIAL_MCF_BAUDRATE
;
587 if ((co
->index
< 0) || (co
->index
>= MCF_MAXPORTS
))
589 port
= &mcf_ports
[co
->index
].port
;
590 if (port
->membase
== 0)
594 uart_parse_options(options
, &baud
, &parity
, &bits
, &flow
);
596 return uart_set_options(port
, co
, baud
, parity
, bits
, flow
);
599 /****************************************************************************/
601 static struct uart_driver mcf_driver
;
603 static struct console mcf_console
= {
605 .write
= mcf_console_write
,
606 .device
= uart_console_device
,
607 .setup
= mcf_console_setup
,
608 .flags
= CON_PRINTBUFFER
,
613 static int __init
mcf_console_init(void)
615 register_console(&mcf_console
);
619 console_initcall(mcf_console_init
);
621 #define MCF_CONSOLE &mcf_console
623 /****************************************************************************/
625 /****************************************************************************/
627 #define MCF_CONSOLE NULL
629 /****************************************************************************/
630 #endif /* CONFIG_MCF_CONSOLE */
631 /****************************************************************************/
634 * Define the mcf UART driver structure.
636 static struct uart_driver mcf_driver
= {
637 .owner
= THIS_MODULE
,
638 .driver_name
= "mcf",
646 /****************************************************************************/
648 static int mcf_probe(struct platform_device
*pdev
)
650 struct mcf_platform_uart
*platp
= dev_get_platdata(&pdev
->dev
);
651 struct uart_port
*port
;
654 for (i
= 0; ((i
< MCF_MAXPORTS
) && (platp
[i
].mapbase
)); i
++) {
655 port
= &mcf_ports
[i
].port
;
658 port
->type
= PORT_MCF
;
659 port
->mapbase
= platp
[i
].mapbase
;
660 port
->membase
= (platp
[i
].membase
) ? platp
[i
].membase
:
661 (unsigned char __iomem
*) platp
[i
].mapbase
;
662 port
->iotype
= SERIAL_IO_MEM
;
663 port
->irq
= platp
[i
].irq
;
664 port
->uartclk
= MCF_BUSCLK
;
665 port
->ops
= &mcf_uart_ops
;
666 port
->flags
= ASYNC_BOOT_AUTOCONF
;
668 uart_add_one_port(&mcf_driver
, port
);
674 /****************************************************************************/
676 static int mcf_remove(struct platform_device
*pdev
)
678 struct uart_port
*port
;
681 for (i
= 0; (i
< MCF_MAXPORTS
); i
++) {
682 port
= &mcf_ports
[i
].port
;
684 uart_remove_one_port(&mcf_driver
, port
);
690 /****************************************************************************/
692 static struct platform_driver mcf_platform_driver
= {
694 .remove
= mcf_remove
,
697 .owner
= THIS_MODULE
,
701 /****************************************************************************/
703 static int __init
mcf_init(void)
707 printk("ColdFire internal UART serial driver\n");
709 rc
= uart_register_driver(&mcf_driver
);
712 rc
= platform_driver_register(&mcf_platform_driver
);
714 uart_unregister_driver(&mcf_driver
);
720 /****************************************************************************/
722 static void __exit
mcf_exit(void)
724 platform_driver_unregister(&mcf_platform_driver
);
725 uart_unregister_driver(&mcf_driver
);
728 /****************************************************************************/
730 module_init(mcf_init
);
731 module_exit(mcf_exit
);
733 MODULE_AUTHOR("Greg Ungerer <gerg@snapgear.com>");
734 MODULE_DESCRIPTION("Freescale ColdFire UART driver");
735 MODULE_LICENSE("GPL");
736 MODULE_ALIAS("platform:mcfuart");
738 /****************************************************************************/