1 /****************************************************************************/
4 * mcf.c -- Motorola ColdFire UART driver
6 * (C) Copyright 2003, Greg Ungerer <gerg@snapgear.com>
9 /****************************************************************************/
11 #include <linux/kernel.h>
12 #include <linux/init.h>
13 #include <linux/module.h>
14 #include <linux/console.h>
15 #include <linux/tty.h>
16 #include <linux/tty_flip.h>
17 #include <linux/serial.h>
18 #include <linux/serial_core.h>
20 #include <asm/coldfire.h>
21 #include <asm/mcfsim.h>
22 #include <asm/mcfuart.h>
23 #include <asm/nettel.h>
25 /****************************************************************************/
28 * Define the standard UART resources for each UART.
29 * All current ColdFire devices have 2 UARTs.
32 unsigned int irq
; /* IRQ used by device */
33 unsigned int memory
; /* Memory address device registers */
36 struct mcf_resource mcf_porttable
[] = {
37 { IRQBASE
, MCF_MBAR
+MCFUART_BASE1
}, /* ttyS0 */
38 { IRQBASE
+1, MCF_MBAR
+MCFUART_BASE2
}, /* ttyS1 */
41 #define MCF_MAXPORTS ((sizeof(mcf_porttable) / sizeof(struct mcf_resource))
43 /****************************************************************************/
46 * Default console baud rate, we use this as the default for all
47 * ports so init can just open /dev/console and keep going.
48 * Perhaps one day the cflag settings for the console can be used
51 #if defined(CONFIG_ARNEWSH) || defined(CONFIG_MOTOROLA) || \
52 defined(CONFIG_senTec)
53 #define CONSOLE_BAUD_RATE 19200
54 #define DEFAULT_CBAUD B19200
57 #ifndef CONSOLE_BAUD_RATE
58 #define CONSOLE_BAUD_RATE 9600
59 #define DEFAULT_CBAUD B9600
62 int mcf_console_inited
= 0;
63 int mcf_console_port
= -1;
64 int mcf_console_baud
= CONSOLE_BAUD_RATE
;
65 int mcf_console_cbaud
= DEFAULT_CBAUD
;
67 /****************************************************************************/
70 * Local per-uart structure.
73 struct uart_port port
;
74 unsigned int sigs
; /* Local copy of line sigs */
75 unsigned char imr
; /* Local IMR mirror */
78 /****************************************************************************/
80 unsigned int mcf_tx_empty(struct uart_port
*port
)
82 volatile unsigned char *uartp
;
87 printk("%s(%d): mcf_tx_empty(port=%x)\n", __FILE__
, __LINE__
,
91 uartp
= (volatile unsigned char *) port
->membase
;
92 spin_lock_irqsave(&port
->lock
, flags
);
93 rc
= (uartp
[MCFUART_USR
] & MCFUART_USR_TXEMPTY
) ? TIOCSER_TEMT
: 0;
94 spin_unlock_irqrestore(&port
->lock
, flags
);
98 /****************************************************************************/
100 unsigned int mcf_get_mctrl(struct uart_port
*port
)
102 volatile unsigned char *uartp
;
107 printk("%s(%d): mcf_get_mctrl(port=%x)\n", __FILE__
, __LINE__
,
111 uartp
= (volatile unsigned char *) port
->membase
;
113 spin_lock_irqsave(&port
->lock
, flags
);
114 sigs
= (uartp
[MCFUART_UIPR
] & MCFUART_UIPR_CTS
) ? 0 : TIOCM_CTS
;
115 sigs
|= (info
->sigs
& TIOCM_RTS
);
116 sigs
|= (mcf_getppdcd(port
->line
) ? TIOCM_CD
: 0);
117 sigs
|= (mcf_getppdtr(port
->line
) ? TIOCM_DTR
: 0);
118 spin_unlock_irqrestore(&port
->lock
, flags
);
122 /****************************************************************************/
124 void mcf_set_mctrl(struct uart_port
*port
, unsigned int sigs
)
126 volatile unsigned char *uartp
;
131 printk("%s(%d): mcf_set_mctrl(port=%x,sigs=%x)\n", __FILE__
, __LINE__
,
135 up
= (struct mcf_uart
*) port
;
136 uartp
= (volatile unsigned char *) port
->membase
;
138 spin_lock_irqsave(&port
->lock
, flags
);
140 mcf_setppdtr(port
->line
, (sigs
& TIOCM_DTR
));
141 if (sigs
& TIOCM_RTS
)
142 uartp
[MCFUART_UOP1
] = MCFUART_UOP_RTS
;
144 uartp
[MCFUART_UOP0
] = MCFUART_UOP_RTS
;
145 spin_unlock_irqrestore(&port
->lock
, flags
);
148 /****************************************************************************/
150 void mcf_start_tx(struct uart_port
*port
, unsigned int tty_start
)
152 volatile unsigned char *uartp
;
157 printk("%s(%d): mcf_start_tx(port=%x,tty_start=%x)\n",
158 __FILE__
, __LINE__
, (int)port
, tty_start
);
161 pp
= (struct mcf_uart
*) port
;
162 uartp
= (volatile unsigned char *) port
->membase
;
164 spin_lock_irqsave(&port
->lock
, flags
);
165 pp
->imr
|= MCFUART_UIR_TXREADY
;
166 uartp
[MCFUART_UIMR
] = pp
->imr
;
167 spin_unlock_irqrestore(&port
->lock
, flags
);
170 /****************************************************************************/
172 void mcf_stop_tx(struct uart_port
*port
, unsigned int tty_stop
)
174 volatile unsigned char *uartp
;
179 printk("%s(%d): mcf_stop_tx(port=%x,tty_start=%x)\n",
180 __FILE__
, __LINE__
, (int)port
, tty_start
);
183 pp
= (struct mcf_uart
*) port
;
184 uartp
= (volatile unsigned char *) port
->membase
;
186 spin_lock_irqsave(&port
->lock
, flags
);
187 pp
->imr
&= ~MCFUART_UIR_TXREADY
;
188 uartp
[MCFUART_UIMR
] = info
->imr
;
189 spin_unlock_irqrestore(&port
->lock
, flags
);
192 /****************************************************************************/
194 void mcf_start_rx(struct uart_port
*port
)
196 volatile unsigned char *uartp
;
201 printk("%s(%d): mcf_start_rx(port=%x)\n", __FILE__
, __LINE__
,
205 pp
= (struct mcf_uart
*) port
;
206 uartp
= (volatile unsigned char *) port
->membase
;
208 spin_lock_irqsave(&port
->lock
, flags
);
209 pp
->imr
|= MCFUART_UIR_RXREADY
;
210 uartp
[MCFUART_UIMR
] = pp
->imr
;
211 spin_unlock_irqrestore(&port
->lock
, flags
);
214 /****************************************************************************/
216 void mcf_stop_rx(struct uart_port
*port
)
218 volatile unsigned char *uartp
;
223 printk("%s(%d): mcf_stop_rx(port=%x)\n", __FILE__
, __LINE__
,
227 pp
= (struct mcf_uart
*) port
;
228 uartp
= (volatile unsigned char *) port
->membase
;
230 spin_lock_irqsave(&port
->lock
, flags
);
231 pp
->imr
&= ~MCFUART_UIR_RXREADY
;
232 uartp
[MCFUART_UIMR
] = info
->imr
;
233 spin_unlock_irqrestore(&port
->lock
, flags
);
236 /****************************************************************************/
238 void mcf_break_ctl(struct uart_port
*port
, int break_state
)
240 volatile unsigned char *uartp
;
244 printk("%s(%d): mcf_break_ctl(port=%x,break_state=%x)\n",
245 __FILE__
, __LINE__
, (int)port
, break_state
);
248 uartp
= (volatile unsigned char *) port
->membase
;
250 spin_lock_irqsave(&port
->lock
, flags
);
251 if (break_state
== -1)
252 uartp
[MCFUART_UCR
] = MCFUART_UCR_CMDBREAKSTART
;
254 uartp
[MCFUART_UCR
] = MCFUART_UCR_CMDBREAKSTOP
;
255 spin_unlock_irqrestore(&port
->lock
, flags
);
258 /****************************************************************************/
260 void mcf_enable_ms(struct uart_port
*port
)
263 printk("%s(%d): mcf_enable_ms(port=%x)\n", __FILE__
, __LINE__
,
268 /****************************************************************************/
270 int mcf_startup(struct uart_port
*port
)
272 volatile unsigned char *uartp
;
277 printk("%s(%d): mcf_startup(port=%x)\n", __FILE__
, __LINE__
, (int)port
);
280 pp
= (struct mcf_uart
*) port
;
281 uartp
= (volatile unsigned char *) port
->membase
;
283 spin_lock_irqsave(&port
->lock
, flags
);
285 /* Reset UART, get it into known state... */
286 uartp
[MCFUART_UCR
] = MCFUART_UCR_CMDRESETRX
;
287 uartp
[MCFUART_UCR
] = MCFUART_UCR_CMDRESETTX
;
289 /* Enable the UART transmitter and receiver */
290 uartp
[MCFUART_UCR
] = MCFUART_UCR_RXENABLE
| MCFUART_UCR_TXENABLE
;
292 /* Enable RX interrupts now */
293 pp
->imr
= MCFUART_UIR_RXREADY
;
294 uartp
[MCFUART_UIMR
] = info
->imr
;
296 spin_unlock_irqrestore(&port
->lock
, flags
);
301 /****************************************************************************/
303 void mcf_shutdown(struct uart_port
*port
)
305 volatile unsigned char *uartp
;
310 printk("%s(%d): mcf_shutdown(port=%x)\n", __FILE__
, __LINE__
,
314 pp
= (struct mcf_uart
*) port
;
315 uartp
= (volatile unsigned char *) port
->membase
;
317 spin_lock_irqsave(&port
->lock
, flags
);
319 /* Disable all interrupts now */
321 uartp
[MCFUART_UIMR
] = info
->imr
;
323 /* Disable UART transmitter and receiver */
324 uartp
[MCFUART_UCR
] = MCFUART_UCR_CMDRESETRX
;
325 uartp
[MCFUART_UCR
] = MCFUART_UCR_CMDRESETTX
;
327 spin_unlock_irqrestore(&port
->lock
, flags
);
330 /****************************************************************************/
332 void mcf_set_termios(struct uart_port
*port
, struct termios
*termios
,
333 struct termios
*old_termios
)
335 volatile unsigned char *uartp
;
338 unsigned int baud
, baudclk
;
339 unsigned char mr1
, mr2
;
342 printk("%s(%d): mcf_set_termios(port=%x,termios=%x,old_termios=%x)\n",
343 __FILE__
, __LINE__
, (int)port
, (int)termios
, (int)old_termios
);
346 pp
= (struct mcf_uart
*) port
;
347 uartp
= (volatile unsigned char *) port
->membase
;
349 spin_lock_irqsave(&port
->lock
, flags
);
351 baud
= uart_get_baud_rate(port
, termios
, old
, 0, 230400);
352 baudclk
= ((MCF_BUSCLK
/ baud
) + 16) / 32;
354 mr1
= MCFUART_MR1_RXIRQRDY
| MCFUART_MR1_RXERRCHAR
;
357 switch (termios
->c_cflag
& CSIZE
) {
358 case CS5
: mr1
|= MCFUART_MR1_CS5
; break;
359 case CS6
: mr1
|= MCFUART_MR1_CS6
; break;
360 case CS7
: mr1
|= MCFUART_MR1_CS7
; break;
362 default: mr1
|= MCFUART_MR1_CS8
; break;
365 if (termios
->c_cflag
& PARENB
) {
366 if (termios
->c_cflag
& CMSPAR
) {
367 if (termios
->c_cflag
& PARODD
)
368 mr1
|= MCFUART_MR1_PARITYMARK
;
370 mr1
|= MCFUART_MR1_PARITYSPACE
;
372 if (termios
->c_cflag
& PARODD
)
373 mr1
|= MCFUART_MR1_PARITYODD
;
375 mr1
|= MCFUART_MR1_PARITYEVEN
;
378 mr1
|= MCFUART_MR1_PARITYNONE
;
381 if (termios
->c_cflag
& CSTOPB
)
382 mr2
|= MCFUART_MR2_STOP2
;
384 mr2
|= MCFUART_MR2_STOP1
;
386 if (termios
->c_cflag
& CRTSCTS
) {
387 mr1
|= MCFUART_MR1_RXRTS
;
388 mr2
|= MCFUART_MR2_TXCTS
;
392 printk("%s(%d): mr1=%x mr2=%x baudclk=%x\n", __FILE__
, __LINE__
,
395 uartp
[MCFUART_UCR
] = MCFUART_UCR_CMDRESETRX
; /* reset RX */
396 uartp
[MCFUART_UCR
] = MCFUART_UCR_CMDRESETTX
; /* reset TX */
397 uartp
[MCFUART_UCR
] = MCFUART_UCR_CMDRESETMRPTR
; /* reset MR pointer */
398 uartp
[MCFUART_UMR
] = mr1
;
399 uartp
[MCFUART_UMR
] = mr2
;
400 uartp
[MCFUART_UBG1
] = (baudclk
& 0xff00) >> 8; /* set msb byte */
401 uartp
[MCFUART_UBG2
] = (baudclk
& 0xff); /* set lsb byte */
402 uartp
[MCFUART_UCSR
] = MCFUART_UCSR_RXCLKTIMER
| MCFUART_UCSR_TXCLKTIMER
;
403 uartp
[MCFUART_UCR
] = MCFUART_UCR_RXENABLE
| MCFUART_UCR_TXENABLE
;
405 spin_unlock_irqrestore(&port
->lock
, flags
);
408 /****************************************************************************/
410 void mcf_config_port(struct uart_port
*port
, int flags
)
412 volatile unsigned char *uartp
= (volatile unsigned char *) port
->membase
;
413 struct mcf_uart
*pp
= (struct mcf_uart
*) port
;
416 #if defined(CONFIG_M5272)
417 volatile unsigned long *icrp
;
418 volatile unsigned long *iop
;
420 icrp
= (volatile unsigned long *) (MCF_MBAR
+ MCFSIM_ICR2
);
422 switch (port
->line
) {
430 printk("MCF: don't know how to handle UART %d interrupt?\n",
435 /* Enable the output lines for the serial ports */
436 iop
= (volatile unsigned long *) (MCF_MBAR
+ MCFSIM_PBCNT
);
437 *iop
= (*portp
& ~0x000000ff) | 0x00000055;
438 iop
= (volatile unsigned long *) (MCF_MBAR
+ MCFSIM_PDCNT
);
439 *iop
= (*portp
& ~0x000003fc) | 0x000002a8;
440 #elif defined(CONFIG_M527x) || defined(CONFIG_M528x)
441 volatile unsigned char *icrp
;
442 volatile unsigned long *imrp
;
444 icrp
= (volatile unsigned char *) (MCF_MBAR
+ MCFICM_INTC0
+
445 MCFINTC_ICR0
+ MCFINT_UART0
+ port
->line
);
446 *icrp
= 0x33; /* UART0 with level 6, priority 3 */
448 imrp
= (volatile unsigned long *) (MCF_MBAR
+ MCFICM_INTC0
+
450 *imrp
&= ~((1 << (info
->irq
- 64)) | 1);
452 volatile unsigned char *icrp
;
454 switch (port
->line
) {
456 icrp
= (volatile unsigned char *) (MCF_MBAR
+ MCFSIM_UART1ICR
);
457 *icrp
= MCFSIM_ICR_LEVEL6
| MCFSIM_ICR_PRI1
;
458 mcf_setimr(mcf_getimr() & ~MCFSIM_IMR_UART1
);
461 icrp
= (volatile unsigned char *) (MCF_MBAR
+ MCFSIM_UART2ICR
);
462 *icrp
= MCFSIM_ICR_LEVEL6
| MCFSIM_ICR_PRI2
;
463 mcf_setimr(mcf_getimr() & ~MCFSIM_IMR_UART2
);
466 printk("MCF: don't know how to handle UART %d interrupt?\n",
471 uartp
[MCFUART_UIVR
] = port
->irq
;
475 printk("%s(%d): mcf_config_port(port=%x,flags=%x)\n",
476 __FILE__
, __LINE__
, (int)port
, flags
);
479 /* Clear mask, so no surprise interrupts. */
480 uartp
[MCFUART_UIMR
] = 0;
482 if (request_irq(port
->irq
, mcf_interrupt
, SA_INTERRUPT
,
483 "ColdFire UART", NULL
)) {
484 printk("MCF: Unable to attach ColdFire UART %d interrupt "
485 "vector=%d\n", port
->line
, port
->irq
);
490 /****************************************************************************/
492 const char mcf_type(struct uart_port
*port
)
494 return ((port
->type
== PORT_MCF_UART
) ? "ColdFire UART" : NULL
);
497 /****************************************************************************/
499 int mcf_request_port(struct uart_port
*port
)
501 /* UARTs always present */
505 /****************************************************************************/
507 void mcf_release_port(struct uart_port
*port
)
509 /* Nothing to release... */
512 /****************************************************************************/
514 int mcf_verify_port(struct uart_port
*port
, struct serial_struct
*ser
)
516 if ((ser
->type
!= PORT_UNKNOWN
) && (ser
->type
!= PORT_MCF_UART
))
521 /****************************************************************************/
524 * Define the baic serial functions we support.
526 struct uart_ops mcf_uart_ops
= {
527 .tx_empty
= mcf_tx_empty
,
528 .get_mctrl
= mcf_get_mctrl
,
529 .set_mctrl
= mcf_set_mctrl
,
530 .start_tx
= mcf_start_tx
,
531 .stop_tx
= mcf_stop_tx
,
532 .stop_rx
= mcf_stop_rx
,
533 .enable_ms
= mcf_enable_ms
,
534 .break_ctl
= mcf_break_ctl
,
535 .startup
= mcf_startup
,
536 .shutdown
= mcf_shutdown
,
537 .set_termios
= mcf_set_termios
539 .request_port
= mcf_request_port
,
540 .release_port
= mcf_release_port
,
541 .config_port
= mcf_config_port
,
542 .verify_port
= mcf_verify_port
,
545 /****************************************************************************/
548 * Define the mcf UART driver structure.
550 struct uart_driver mcf_driver
= {
551 .owner
= THIS_MODULE
,
552 .driver_name
= "mcf",
554 .dev_name
= "tts/%d",
565 * Define the port structures, 1 per port/uart.
567 struct uart_port mcf_ports
[MCF_MAXPORTS
];
569 /****************************************************************************/
571 int __init
mcf_init(void)
573 struct uart_port
*pp
;
576 printk("ColdFire internal UART serial driver\n");
578 if ((i
= uart_register_driver(&mcf_driver
)))
581 for (i
= 0; (i
< MCF_MAXPORTS
); i
++) {
583 memset(pp
, 0, sizeof(*pp
));
585 pp
->iotype
= SERIAL_IO_MEM
;
586 pp
->flags
= UPF_BOOT_AUTOCONF
;
588 pp
->irq
= mcf_porttable
[i
].irq
;
589 pp
->mapbase
= mcf_porttable
[i
].memory
;
590 pp
->membase
= (char *) mcf_porttable
[i
].memory
;
591 pp
->uartclk
= MCF_BUSCLK
;
592 uart_add_one_port(&mcf_driver
, pp
);
597 /****************************************************************************/
599 void __exit
mcf_exit(void)
603 for (i
= 0; (i
< MCF_MAXPORTS
); i
++)
604 uart_remove_one_port(&mcf_driver
, &mcf_ports
[i
]);
605 uart_unregister_driver(&mcf_driver
);
608 /****************************************************************************/
610 module_init(mcf_init
);
611 module_init(mcf_exit
);
613 MODULE_AUTHOR("Greg Ungerer <gerg@snapgear.org>");
614 MODULE_DESCRIPTION("Motorola ColdFire UART driver");
615 MODULE_LICENSE("GPL");
617 /****************************************************************************/