MOXA linux-2.6.x / linux-2.6.9-uc0 from sdlinux-moxaart.tgz
[linux-2.6.9-moxart.git] / drivers / serial / mcf.c
blob45a428274b9c47a0e725e8556c70a23fe23cf8d0
1 /****************************************************************************/
3 /*
4 * mcf.c -- Motorola ColdFire UART driver
6 * (C) Copyright 2003, Greg Ungerer <gerg@snapgear.com>
7 */
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.
31 struct mcf_resource {
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
49 * instead.
51 #if defined(CONFIG_ARNEWSH) || defined(CONFIG_MOTOROLA) || \
52 defined(CONFIG_senTec)
53 #define CONSOLE_BAUD_RATE 19200
54 #define DEFAULT_CBAUD B19200
55 #endif
57 #ifndef CONSOLE_BAUD_RATE
58 #define CONSOLE_BAUD_RATE 9600
59 #define DEFAULT_CBAUD B9600
60 #endif
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.
72 struct mcf_uart {
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;
83 unsigned long flags;
84 unsigned int rc;
86 #if DEBUG
87 printk("%s(%d): mcf_tx_empty(port=%x)\n", __FILE__, __LINE__,
88 (int)port);
89 #endif
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);
95 return rc;
98 /****************************************************************************/
100 unsigned int mcf_get_mctrl(struct uart_port *port)
102 volatile unsigned char *uartp;
103 unsigned long flags;
104 unsigned int sigs;
106 #if DEBUG
107 printk("%s(%d): mcf_get_mctrl(port=%x)\n", __FILE__, __LINE__,
108 (int)port);
109 #endif
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);
119 return sigs;
122 /****************************************************************************/
124 void mcf_set_mctrl(struct uart_port *port, unsigned int sigs)
126 volatile unsigned char *uartp;
127 struct mcf_uart *pp;
128 unsigned long flags;
130 #if DEBUG
131 printk("%s(%d): mcf_set_mctrl(port=%x,sigs=%x)\n", __FILE__, __LINE__,
132 (int)port, sigs);
133 #endif
135 up = (struct mcf_uart *) port;
136 uartp = (volatile unsigned char *) port->membase;
138 spin_lock_irqsave(&port->lock, flags);
139 pp->sigs = sigs;
140 mcf_setppdtr(port->line, (sigs & TIOCM_DTR));
141 if (sigs & TIOCM_RTS)
142 uartp[MCFUART_UOP1] = MCFUART_UOP_RTS;
143 else
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;
153 struct mcf_uart *pp;
154 unsigned long flags;
156 #if DEBUG
157 printk("%s(%d): mcf_start_tx(port=%x,tty_start=%x)\n",
158 __FILE__, __LINE__, (int)port, tty_start);
159 #endif
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;
175 struct mcf_uart *pp;
176 unsigned long flags;
178 #if DEBUG
179 printk("%s(%d): mcf_stop_tx(port=%x,tty_start=%x)\n",
180 __FILE__, __LINE__, (int)port, tty_start);
181 #endif
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;
197 struct mcf_uart *pp;
198 unsigned long flags;
200 #if DEBUG
201 printk("%s(%d): mcf_start_rx(port=%x)\n", __FILE__, __LINE__,
202 (int)port);
203 #endif
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;
219 struct mcf_uart *pp;
220 unsigned long flags;
222 #if DEBUG
223 printk("%s(%d): mcf_stop_rx(port=%x)\n", __FILE__, __LINE__,
224 (int)port);
225 #endif
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;
241 unsigned long flags;
243 #if DEBUG
244 printk("%s(%d): mcf_break_ctl(port=%x,break_state=%x)\n",
245 __FILE__, __LINE__, (int)port, break_state);
246 #endif
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;
253 else
254 uartp[MCFUART_UCR] = MCFUART_UCR_CMDBREAKSTOP;
255 spin_unlock_irqrestore(&port->lock, flags);
258 /****************************************************************************/
260 void mcf_enable_ms(struct uart_port *port)
262 #if DEBUG
263 printk("%s(%d): mcf_enable_ms(port=%x)\n", __FILE__, __LINE__,
264 (int)port);
265 #endif
268 /****************************************************************************/
270 int mcf_startup(struct uart_port *port)
272 volatile unsigned char *uartp;
273 struct mcf_uart *pp;
274 unsigned long flags;
276 #if DEBUG
277 printk("%s(%d): mcf_startup(port=%x)\n", __FILE__, __LINE__, (int)port);
278 #endif
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);
298 return 0;
301 /****************************************************************************/
303 void mcf_shutdown(struct uart_port *port)
305 volatile unsigned char *uartp;
306 struct mcf_uart *pp;
307 unsigned long flags;
309 #if DEBUG
310 printk("%s(%d): mcf_shutdown(port=%x)\n", __FILE__, __LINE__,
311 (int)port);
312 #endif
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 */
320 pp->imr = 0;
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;
336 struct mcf_uart *pp;
337 unsigned long flags;
338 unsigned int baud, baudclk;
339 unsigned char mr1, mr2;
341 #if DEBUG
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);
344 #endif
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;
355 mr2 = 0;
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;
361 case CS8:
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;
369 else
370 mr1 |= MCFUART_MR1_PARITYSPACE;
371 } else {
372 if (termios->c_cflag & PARODD)
373 mr1 |= MCFUART_MR1_PARITYODD;
374 else
375 mr1 |= MCFUART_MR1_PARITYEVEN;
377 } else {
378 mr1 |= MCFUART_MR1_PARITYNONE;
381 if (termios->c_cflag & CSTOPB)
382 mr2 |= MCFUART_MR2_STOP2;
383 else
384 mr2 |= MCFUART_MR2_STOP1;
386 if (termios->c_cflag & CRTSCTS) {
387 mr1 |= MCFUART_MR1_RXRTS;
388 mr2 |= MCFUART_MR2_TXCTS;
391 #if 0
392 printk("%s(%d): mr1=%x mr2=%x baudclk=%x\n", __FILE__, __LINE__,
393 mr1, mr2, baudclk);
394 #endif
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;
414 unsigned long flags;
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) {
423 case 0:
424 *icrp = 0xe0000000;
425 break;
426 case 1:
427 *icrp = 0x0e000000;
428 break;
429 default:
430 printk("MCF: don't know how to handle UART %d interrupt?\n",
431 port->line);
432 return;
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 +
449 MCFINTC_IMRL);
450 *imrp &= ~((1 << (info->irq - 64)) | 1);
451 #else
452 volatile unsigned char *icrp;
454 switch (port->line) {
455 case 0:
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);
459 break;
460 case 1:
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);
464 break;
465 default:
466 printk("MCF: don't know how to handle UART %d interrupt?\n",
467 info->line);
468 return;
471 uartp[MCFUART_UIVR] = port->irq;
472 #endif
474 #if DEBUG
475 printk("%s(%d): mcf_config_port(port=%x,flags=%x)\n",
476 __FILE__, __LINE__, (int)port, flags);
477 #endif
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 */
502 return 0;
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))
517 return -EINVAL;
518 return 0;
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
538 .type = mcf_type,
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",
553 #ifdef CONFIG_DEV_FS
554 .dev_name = "tts/%d",
555 #else
556 .dev_name = "ttyS",
557 #endif
558 .major = TTY_MAJOR,
559 .minor = 0,
560 .nr = MCF_MAXPORTS,
561 .cons = 0,
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;
574 int i;
576 printk("ColdFire internal UART serial driver\n");
578 if ((i = uart_register_driver(&mcf_driver)))
579 return i;
581 for (i = 0; (i < MCF_MAXPORTS); i++) {
582 pp = &mcf_ports[i];
583 memset(pp, 0, sizeof(*pp));
584 pp->line = i;
585 pp->iotype = SERIAL_IO_MEM;
586 pp->flags = UPF_BOOT_AUTOCONF;
587 pp->ops = &mcf_ops;
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);
594 return 0;
597 /****************************************************************************/
599 void __exit mcf_exit(void)
601 int i;
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 /****************************************************************************/