[POWERPC] Separate MPC52xx PSC FIFO registers from rest of PSC
[linux-2.6/sactl.git] / drivers / serial / mpc52xx_uart.c
blob141b8da7e0e7d3beaeb527e871f5fe691a685c3f
1 /*
2 * Driver for the PSC of the Freescale MPC52xx PSCs configured as UARTs.
4 * FIXME According to the usermanual the status bits in the status register
5 * are only updated when the peripherals access the FIFO and not when the
6 * CPU access them. So since we use this bits to know when we stop writing
7 * and reading, they may not be updated in-time and a race condition may
8 * exists. But I haven't be able to prove this and I don't care. But if
9 * any problem arises, it might worth checking. The TX/RX FIFO Stats
10 * registers should be used in addition.
11 * Update: Actually, they seem updated ... At least the bits we use.
14 * Maintainer : Sylvain Munaut <tnt@246tNt.com>
16 * Some of the code has been inspired/copied from the 2.4 code written
17 * by Dale Farnsworth <dfarnsworth@mvista.com>.
19 * Copyright (C) 2006 Secret Lab Technologies Ltd.
20 * Grant Likely <grant.likely@secretlab.ca>
21 * Copyright (C) 2004-2006 Sylvain Munaut <tnt@246tNt.com>
22 * Copyright (C) 2003 MontaVista, Software, Inc.
24 * This file is licensed under the terms of the GNU General Public License
25 * version 2. This program is licensed "as is" without any warranty of any
26 * kind, whether express or implied.
29 /* Platform device Usage :
31 * Since PSCs can have multiple function, the correct driver for each one
32 * is selected by calling mpc52xx_match_psc_function(...). The function
33 * handled by this driver is "uart".
35 * The driver init all necessary registers to place the PSC in uart mode without
36 * DCD. However, the pin multiplexing aren't changed and should be set either
37 * by the bootloader or in the platform init code.
39 * The idx field must be equal to the PSC index ( e.g. 0 for PSC1, 1 for PSC2,
40 * and so on). So the PSC1 is mapped to /dev/ttyPSC0, PSC2 to /dev/ttyPSC1 and
41 * so on. But be warned, it's an ABSOLUTE REQUIREMENT ! This is needed mainly
42 * fpr the console code : without this 1:1 mapping, at early boot time, when we
43 * are parsing the kernel args console=ttyPSC?, we wouldn't know which PSC it
44 * will be mapped to.
47 /* OF Platform device Usage :
49 * This driver is only used for PSCs configured in uart mode. The device
50 * tree will have a node for each PSC in uart mode w/ device_type = "serial"
51 * and "mpc52xx-psc-uart" in the compatible string
53 * By default, PSC devices are enumerated in the order they are found. However
54 * a particular PSC number can be forces by adding 'device_no = <port#>'
55 * to the device node.
57 * The driver init all necessary registers to place the PSC in uart mode without
58 * DCD. However, the pin multiplexing aren't changed and should be set either
59 * by the bootloader or in the platform init code.
62 #undef DEBUG
64 #include <linux/device.h>
65 #include <linux/module.h>
66 #include <linux/tty.h>
67 #include <linux/serial.h>
68 #include <linux/sysrq.h>
69 #include <linux/console.h>
71 #include <asm/delay.h>
72 #include <asm/io.h>
74 #if defined(CONFIG_PPC_MERGE)
75 #include <linux/of.h>
76 #include <linux/of_platform.h>
77 #else
78 #include <linux/platform_device.h>
79 #endif
81 #include <asm/mpc52xx.h>
82 #include <asm/mpc52xx_psc.h>
84 #if defined(CONFIG_SERIAL_MPC52xx_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
85 #define SUPPORT_SYSRQ
86 #endif
88 #include <linux/serial_core.h>
91 /* We've been assigned a range on the "Low-density serial ports" major */
92 #define SERIAL_PSC_MAJOR 204
93 #define SERIAL_PSC_MINOR 148
96 #define ISR_PASS_LIMIT 256 /* Max number of iteration in the interrupt */
99 static struct uart_port mpc52xx_uart_ports[MPC52xx_PSC_MAXNUM];
100 /* Rem: - We use the read_status_mask as a shadow of
101 * psc->mpc52xx_psc_imr
102 * - It's important that is array is all zero on start as we
103 * use it to know if it's initialized or not ! If it's not sure
104 * it's cleared, then a memset(...,0,...) should be added to
105 * the console_init
107 #if defined(CONFIG_PPC_MERGE)
108 /* lookup table for matching device nodes to index numbers */
109 static struct device_node *mpc52xx_uart_nodes[MPC52xx_PSC_MAXNUM];
111 static void mpc52xx_uart_of_enumerate(void);
112 #endif
114 #define PSC(port) ((struct mpc52xx_psc __iomem *)((port)->membase))
115 #define FIFO(port) ((struct mpc52xx_psc_fifo __iomem *)(PSC(port)+1))
118 /* Forward declaration of the interruption handling routine */
119 static irqreturn_t mpc52xx_uart_int(int irq,void *dev_id);
122 /* Simple macro to test if a port is console or not. This one is taken
123 * for serial_core.c and maybe should be moved to serial_core.h ? */
124 #ifdef CONFIG_SERIAL_CORE_CONSOLE
125 #define uart_console(port) ((port)->cons && (port)->cons->index == (port)->line)
126 #else
127 #define uart_console(port) (0)
128 #endif
130 #if defined(CONFIG_PPC_MERGE)
131 static struct of_device_id mpc52xx_uart_of_match[] = {
132 { .type = "serial", .compatible = "mpc5200-psc-uart", },
135 #endif
138 /* ======================================================================== */
139 /* UART operations */
140 /* ======================================================================== */
142 static unsigned int
143 mpc52xx_uart_tx_empty(struct uart_port *port)
145 int status = in_be16(&PSC(port)->mpc52xx_psc_status);
146 return (status & MPC52xx_PSC_SR_TXEMP) ? TIOCSER_TEMT : 0;
149 static void
150 mpc52xx_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
152 /* Not implemented */
155 static unsigned int
156 mpc52xx_uart_get_mctrl(struct uart_port *port)
158 /* Not implemented */
159 return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
162 static void
163 mpc52xx_uart_stop_tx(struct uart_port *port)
165 /* port->lock taken by caller */
166 port->read_status_mask &= ~MPC52xx_PSC_IMR_TXRDY;
167 out_be16(&PSC(port)->mpc52xx_psc_imr,port->read_status_mask);
170 static void
171 mpc52xx_uart_start_tx(struct uart_port *port)
173 /* port->lock taken by caller */
174 port->read_status_mask |= MPC52xx_PSC_IMR_TXRDY;
175 out_be16(&PSC(port)->mpc52xx_psc_imr,port->read_status_mask);
178 static void
179 mpc52xx_uart_send_xchar(struct uart_port *port, char ch)
181 unsigned long flags;
182 spin_lock_irqsave(&port->lock, flags);
184 port->x_char = ch;
185 if (ch) {
186 /* Make sure tx interrupts are on */
187 /* Truly necessary ??? They should be anyway */
188 port->read_status_mask |= MPC52xx_PSC_IMR_TXRDY;
189 out_be16(&PSC(port)->mpc52xx_psc_imr,port->read_status_mask);
192 spin_unlock_irqrestore(&port->lock, flags);
195 static void
196 mpc52xx_uart_stop_rx(struct uart_port *port)
198 /* port->lock taken by caller */
199 port->read_status_mask &= ~MPC52xx_PSC_IMR_RXRDY;
200 out_be16(&PSC(port)->mpc52xx_psc_imr,port->read_status_mask);
203 static void
204 mpc52xx_uart_enable_ms(struct uart_port *port)
206 /* Not implemented */
209 static void
210 mpc52xx_uart_break_ctl(struct uart_port *port, int ctl)
212 unsigned long flags;
213 spin_lock_irqsave(&port->lock, flags);
215 if ( ctl == -1 )
216 out_8(&PSC(port)->command,MPC52xx_PSC_START_BRK);
217 else
218 out_8(&PSC(port)->command,MPC52xx_PSC_STOP_BRK);
220 spin_unlock_irqrestore(&port->lock, flags);
223 static int
224 mpc52xx_uart_startup(struct uart_port *port)
226 struct mpc52xx_psc __iomem *psc = PSC(port);
227 struct mpc52xx_psc_fifo __iomem *fifo = FIFO(port);
228 int ret;
230 /* Request IRQ */
231 ret = request_irq(port->irq, mpc52xx_uart_int,
232 IRQF_DISABLED | IRQF_SAMPLE_RANDOM, "mpc52xx_psc_uart", port);
233 if (ret)
234 return ret;
236 /* Reset/activate the port, clear and enable interrupts */
237 out_8(&psc->command,MPC52xx_PSC_RST_RX);
238 out_8(&psc->command,MPC52xx_PSC_RST_TX);
240 out_be32(&psc->sicr,0); /* UART mode DCD ignored */
242 out_be16(&psc->mpc52xx_psc_clock_select, 0xdd00); /* /16 prescaler on */
244 out_8(&fifo->rfcntl, 0x00);
245 out_be16(&fifo->rfalarm, 0x1ff);
246 out_8(&fifo->tfcntl, 0x07);
247 out_be16(&fifo->tfalarm, 0x80);
249 port->read_status_mask |= MPC52xx_PSC_IMR_RXRDY | MPC52xx_PSC_IMR_TXRDY;
250 out_be16(&psc->mpc52xx_psc_imr,port->read_status_mask);
252 out_8(&psc->command,MPC52xx_PSC_TX_ENABLE);
253 out_8(&psc->command,MPC52xx_PSC_RX_ENABLE);
255 return 0;
258 static void
259 mpc52xx_uart_shutdown(struct uart_port *port)
261 struct mpc52xx_psc __iomem *psc = PSC(port);
263 /* Shut down the port. Leave TX active if on a console port */
264 out_8(&psc->command,MPC52xx_PSC_RST_RX);
265 if (!uart_console(port))
266 out_8(&psc->command,MPC52xx_PSC_RST_TX);
268 port->read_status_mask = 0;
269 out_be16(&psc->mpc52xx_psc_imr,port->read_status_mask);
271 /* Release interrupt */
272 free_irq(port->irq, port);
275 static void
276 mpc52xx_uart_set_termios(struct uart_port *port, struct ktermios *new,
277 struct ktermios *old)
279 struct mpc52xx_psc __iomem *psc = PSC(port);
280 unsigned long flags;
281 unsigned char mr1, mr2;
282 unsigned short ctr;
283 unsigned int j, baud, quot;
285 /* Prepare what we're gonna write */
286 mr1 = 0;
288 switch (new->c_cflag & CSIZE) {
289 case CS5: mr1 |= MPC52xx_PSC_MODE_5_BITS;
290 break;
291 case CS6: mr1 |= MPC52xx_PSC_MODE_6_BITS;
292 break;
293 case CS7: mr1 |= MPC52xx_PSC_MODE_7_BITS;
294 break;
295 case CS8:
296 default: mr1 |= MPC52xx_PSC_MODE_8_BITS;
299 if (new->c_cflag & PARENB) {
300 mr1 |= (new->c_cflag & PARODD) ?
301 MPC52xx_PSC_MODE_PARODD : MPC52xx_PSC_MODE_PAREVEN;
302 } else
303 mr1 |= MPC52xx_PSC_MODE_PARNONE;
306 mr2 = 0;
308 if (new->c_cflag & CSTOPB)
309 mr2 |= MPC52xx_PSC_MODE_TWO_STOP;
310 else
311 mr2 |= ((new->c_cflag & CSIZE) == CS5) ?
312 MPC52xx_PSC_MODE_ONE_STOP_5_BITS :
313 MPC52xx_PSC_MODE_ONE_STOP;
316 baud = uart_get_baud_rate(port, new, old, 0, port->uartclk/16);
317 quot = uart_get_divisor(port, baud);
318 ctr = quot & 0xffff;
320 /* Get the lock */
321 spin_lock_irqsave(&port->lock, flags);
323 /* Update the per-port timeout */
324 uart_update_timeout(port, new->c_cflag, baud);
326 /* Do our best to flush TX & RX, so we don't loose anything */
327 /* But we don't wait indefinitly ! */
328 j = 5000000; /* Maximum wait */
329 /* FIXME Can't receive chars since set_termios might be called at early
330 * boot for the console, all stuff is not yet ready to receive at that
331 * time and that just makes the kernel oops */
332 /* while (j-- && mpc52xx_uart_int_rx_chars(port)); */
333 while (!(in_be16(&psc->mpc52xx_psc_status) & MPC52xx_PSC_SR_TXEMP) &&
334 --j)
335 udelay(1);
337 if (!j)
338 printk( KERN_ERR "mpc52xx_uart.c: "
339 "Unable to flush RX & TX fifos in-time in set_termios."
340 "Some chars may have been lost.\n" );
342 /* Reset the TX & RX */
343 out_8(&psc->command,MPC52xx_PSC_RST_RX);
344 out_8(&psc->command,MPC52xx_PSC_RST_TX);
346 /* Send new mode settings */
347 out_8(&psc->command,MPC52xx_PSC_SEL_MODE_REG_1);
348 out_8(&psc->mode,mr1);
349 out_8(&psc->mode,mr2);
350 out_8(&psc->ctur,ctr >> 8);
351 out_8(&psc->ctlr,ctr & 0xff);
353 /* Reenable TX & RX */
354 out_8(&psc->command,MPC52xx_PSC_TX_ENABLE);
355 out_8(&psc->command,MPC52xx_PSC_RX_ENABLE);
357 /* We're all set, release the lock */
358 spin_unlock_irqrestore(&port->lock, flags);
361 static const char *
362 mpc52xx_uart_type(struct uart_port *port)
364 return port->type == PORT_MPC52xx ? "MPC52xx PSC" : NULL;
367 static void
368 mpc52xx_uart_release_port(struct uart_port *port)
370 if (port->flags & UPF_IOREMAP) { /* remapped by us ? */
371 iounmap(port->membase);
372 port->membase = NULL;
375 release_mem_region(port->mapbase, sizeof(struct mpc52xx_psc));
378 static int
379 mpc52xx_uart_request_port(struct uart_port *port)
381 int err;
383 if (port->flags & UPF_IOREMAP) /* Need to remap ? */
384 port->membase = ioremap(port->mapbase,
385 sizeof(struct mpc52xx_psc));
387 if (!port->membase)
388 return -EINVAL;
390 err = request_mem_region(port->mapbase, sizeof(struct mpc52xx_psc),
391 "mpc52xx_psc_uart") != NULL ? 0 : -EBUSY;
393 if (err && (port->flags & UPF_IOREMAP)) {
394 iounmap(port->membase);
395 port->membase = NULL;
398 return err;
401 static void
402 mpc52xx_uart_config_port(struct uart_port *port, int flags)
404 if ( (flags & UART_CONFIG_TYPE) &&
405 (mpc52xx_uart_request_port(port) == 0) )
406 port->type = PORT_MPC52xx;
409 static int
410 mpc52xx_uart_verify_port(struct uart_port *port, struct serial_struct *ser)
412 if ( ser->type != PORT_UNKNOWN && ser->type != PORT_MPC52xx )
413 return -EINVAL;
415 if ( (ser->irq != port->irq) ||
416 (ser->io_type != SERIAL_IO_MEM) ||
417 (ser->baud_base != port->uartclk) ||
418 (ser->iomem_base != (void*)port->mapbase) ||
419 (ser->hub6 != 0 ) )
420 return -EINVAL;
422 return 0;
426 static struct uart_ops mpc52xx_uart_ops = {
427 .tx_empty = mpc52xx_uart_tx_empty,
428 .set_mctrl = mpc52xx_uart_set_mctrl,
429 .get_mctrl = mpc52xx_uart_get_mctrl,
430 .stop_tx = mpc52xx_uart_stop_tx,
431 .start_tx = mpc52xx_uart_start_tx,
432 .send_xchar = mpc52xx_uart_send_xchar,
433 .stop_rx = mpc52xx_uart_stop_rx,
434 .enable_ms = mpc52xx_uart_enable_ms,
435 .break_ctl = mpc52xx_uart_break_ctl,
436 .startup = mpc52xx_uart_startup,
437 .shutdown = mpc52xx_uart_shutdown,
438 .set_termios = mpc52xx_uart_set_termios,
439 /* .pm = mpc52xx_uart_pm, Not supported yet */
440 /* .set_wake = mpc52xx_uart_set_wake, Not supported yet */
441 .type = mpc52xx_uart_type,
442 .release_port = mpc52xx_uart_release_port,
443 .request_port = mpc52xx_uart_request_port,
444 .config_port = mpc52xx_uart_config_port,
445 .verify_port = mpc52xx_uart_verify_port
449 /* ======================================================================== */
450 /* Interrupt handling */
451 /* ======================================================================== */
453 static inline int
454 mpc52xx_uart_int_rx_chars(struct uart_port *port)
456 struct tty_struct *tty = port->info->tty;
457 unsigned char ch, flag;
458 unsigned short status;
460 /* While we can read, do so ! */
461 while ( (status = in_be16(&PSC(port)->mpc52xx_psc_status)) &
462 MPC52xx_PSC_SR_RXRDY) {
464 /* Get the char */
465 ch = in_8(&PSC(port)->mpc52xx_psc_buffer_8);
467 /* Handle sysreq char */
468 #ifdef SUPPORT_SYSRQ
469 if (uart_handle_sysrq_char(port, ch)) {
470 port->sysrq = 0;
471 continue;
473 #endif
475 /* Store it */
477 flag = TTY_NORMAL;
478 port->icount.rx++;
480 if ( status & (MPC52xx_PSC_SR_PE |
481 MPC52xx_PSC_SR_FE |
482 MPC52xx_PSC_SR_RB) ) {
484 if (status & MPC52xx_PSC_SR_RB) {
485 flag = TTY_BREAK;
486 uart_handle_break(port);
487 } else if (status & MPC52xx_PSC_SR_PE)
488 flag = TTY_PARITY;
489 else if (status & MPC52xx_PSC_SR_FE)
490 flag = TTY_FRAME;
492 /* Clear error condition */
493 out_8(&PSC(port)->command,MPC52xx_PSC_RST_ERR_STAT);
496 tty_insert_flip_char(tty, ch, flag);
497 if (status & MPC52xx_PSC_SR_OE) {
499 * Overrun is special, since it's
500 * reported immediately, and doesn't
501 * affect the current character
503 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
507 tty_flip_buffer_push(tty);
509 return in_be16(&PSC(port)->mpc52xx_psc_status) & MPC52xx_PSC_SR_RXRDY;
512 static inline int
513 mpc52xx_uart_int_tx_chars(struct uart_port *port)
515 struct circ_buf *xmit = &port->info->xmit;
517 /* Process out of band chars */
518 if (port->x_char) {
519 out_8(&PSC(port)->mpc52xx_psc_buffer_8, port->x_char);
520 port->icount.tx++;
521 port->x_char = 0;
522 return 1;
525 /* Nothing to do ? */
526 if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
527 mpc52xx_uart_stop_tx(port);
528 return 0;
531 /* Send chars */
532 while (in_be16(&PSC(port)->mpc52xx_psc_status) & MPC52xx_PSC_SR_TXRDY) {
533 out_8(&PSC(port)->mpc52xx_psc_buffer_8, xmit->buf[xmit->tail]);
534 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
535 port->icount.tx++;
536 if (uart_circ_empty(xmit))
537 break;
540 /* Wake up */
541 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
542 uart_write_wakeup(port);
544 /* Maybe we're done after all */
545 if (uart_circ_empty(xmit)) {
546 mpc52xx_uart_stop_tx(port);
547 return 0;
550 return 1;
553 static irqreturn_t
554 mpc52xx_uart_int(int irq, void *dev_id)
556 struct uart_port *port = dev_id;
557 unsigned long pass = ISR_PASS_LIMIT;
558 unsigned int keepgoing;
559 unsigned short status;
561 spin_lock(&port->lock);
563 /* While we have stuff to do, we continue */
564 do {
565 /* If we don't find anything to do, we stop */
566 keepgoing = 0;
568 /* Read status */
569 status = in_be16(&PSC(port)->mpc52xx_psc_isr);
570 status &= port->read_status_mask;
572 /* Do we need to receive chars ? */
573 /* For this RX interrupts must be on and some chars waiting */
574 if ( status & MPC52xx_PSC_IMR_RXRDY )
575 keepgoing |= mpc52xx_uart_int_rx_chars(port);
577 /* Do we need to send chars ? */
578 /* For this, TX must be ready and TX interrupt enabled */
579 if ( status & MPC52xx_PSC_IMR_TXRDY )
580 keepgoing |= mpc52xx_uart_int_tx_chars(port);
582 /* Limit number of iteration */
583 if ( !(--pass) )
584 keepgoing = 0;
586 } while (keepgoing);
588 spin_unlock(&port->lock);
590 return IRQ_HANDLED;
594 /* ======================================================================== */
595 /* Console ( if applicable ) */
596 /* ======================================================================== */
598 #ifdef CONFIG_SERIAL_MPC52xx_CONSOLE
600 static void __init
601 mpc52xx_console_get_options(struct uart_port *port,
602 int *baud, int *parity, int *bits, int *flow)
604 struct mpc52xx_psc __iomem *psc = PSC(port);
605 unsigned char mr1;
607 pr_debug("mpc52xx_console_get_options(port=%p)\n", port);
609 /* Read the mode registers */
610 out_8(&psc->command,MPC52xx_PSC_SEL_MODE_REG_1);
611 mr1 = in_8(&psc->mode);
613 /* CT{U,L}R are write-only ! */
614 *baud = CONFIG_SERIAL_MPC52xx_CONSOLE_BAUD;
615 #if !defined(CONFIG_PPC_MERGE)
616 if (__res.bi_baudrate)
617 *baud = __res.bi_baudrate;
618 #endif
620 /* Parse them */
621 switch (mr1 & MPC52xx_PSC_MODE_BITS_MASK) {
622 case MPC52xx_PSC_MODE_5_BITS: *bits = 5; break;
623 case MPC52xx_PSC_MODE_6_BITS: *bits = 6; break;
624 case MPC52xx_PSC_MODE_7_BITS: *bits = 7; break;
625 case MPC52xx_PSC_MODE_8_BITS:
626 default: *bits = 8;
629 if (mr1 & MPC52xx_PSC_MODE_PARNONE)
630 *parity = 'n';
631 else
632 *parity = mr1 & MPC52xx_PSC_MODE_PARODD ? 'o' : 'e';
635 static void
636 mpc52xx_console_write(struct console *co, const char *s, unsigned int count)
638 struct uart_port *port = &mpc52xx_uart_ports[co->index];
639 struct mpc52xx_psc __iomem *psc = PSC(port);
640 unsigned int i, j;
642 /* Disable interrupts */
643 out_be16(&psc->mpc52xx_psc_imr, 0);
645 /* Wait the TX buffer to be empty */
646 j = 5000000; /* Maximum wait */
647 while (!(in_be16(&psc->mpc52xx_psc_status) & MPC52xx_PSC_SR_TXEMP) &&
648 --j)
649 udelay(1);
651 /* Write all the chars */
652 for (i = 0; i < count; i++, s++) {
653 /* Line return handling */
654 if (*s == '\n')
655 out_8(&psc->mpc52xx_psc_buffer_8, '\r');
657 /* Send the char */
658 out_8(&psc->mpc52xx_psc_buffer_8, *s);
660 /* Wait the TX buffer to be empty */
661 j = 20000; /* Maximum wait */
662 while (!(in_be16(&psc->mpc52xx_psc_status) &
663 MPC52xx_PSC_SR_TXEMP) && --j)
664 udelay(1);
667 /* Restore interrupt state */
668 out_be16(&psc->mpc52xx_psc_imr, port->read_status_mask);
671 #if !defined(CONFIG_PPC_MERGE)
672 static int __init
673 mpc52xx_console_setup(struct console *co, char *options)
675 struct uart_port *port = &mpc52xx_uart_ports[co->index];
677 int baud = CONFIG_SERIAL_MPC52xx_CONSOLE_BAUD;
678 int bits = 8;
679 int parity = 'n';
680 int flow = 'n';
682 if (co->index < 0 || co->index >= MPC52xx_PSC_MAXNUM)
683 return -EINVAL;
685 /* Basic port init. Needed since we use some uart_??? func before
686 * real init for early access */
687 spin_lock_init(&port->lock);
688 port->uartclk = __res.bi_ipbfreq / 2; /* Look at CTLR doc */
689 port->ops = &mpc52xx_uart_ops;
690 port->mapbase = MPC52xx_PA(MPC52xx_PSCx_OFFSET(co->index+1));
692 /* We ioremap ourself */
693 port->membase = ioremap(port->mapbase, MPC52xx_PSC_SIZE);
694 if (port->membase == NULL)
695 return -EINVAL;
697 /* Setup the port parameters accoding to options */
698 if (options)
699 uart_parse_options(options, &baud, &parity, &bits, &flow);
700 else
701 mpc52xx_console_get_options(port, &baud, &parity, &bits, &flow);
703 return uart_set_options(port, co, baud, parity, bits, flow);
706 #else
708 static int __init
709 mpc52xx_console_setup(struct console *co, char *options)
711 struct uart_port *port = &mpc52xx_uart_ports[co->index];
712 struct device_node *np = mpc52xx_uart_nodes[co->index];
713 unsigned int ipb_freq;
714 struct resource res;
715 int ret;
717 int baud = CONFIG_SERIAL_MPC52xx_CONSOLE_BAUD;
718 int bits = 8;
719 int parity = 'n';
720 int flow = 'n';
722 pr_debug("mpc52xx_console_setup co=%p, co->index=%i, options=%s\n",
723 co, co->index, options);
725 if ((co->index < 0) || (co->index > MPC52xx_PSC_MAXNUM)) {
726 pr_debug("PSC%x out of range\n", co->index);
727 return -EINVAL;
730 if (!np) {
731 pr_debug("PSC%x not found in device tree\n", co->index);
732 return -EINVAL;
735 pr_debug("Console on ttyPSC%x is %s\n",
736 co->index, mpc52xx_uart_nodes[co->index]->full_name);
738 /* Fetch register locations */
739 if ((ret = of_address_to_resource(np, 0, &res)) != 0) {
740 pr_debug("Could not get resources for PSC%x\n", co->index);
741 return ret;
744 /* Search for bus-frequency property in this node or a parent */
745 if ((ipb_freq = mpc52xx_find_ipb_freq(np)) == 0) {
746 pr_debug("Could not find IPB bus frequency!\n");
747 return -EINVAL;
750 /* Basic port init. Needed since we use some uart_??? func before
751 * real init for early access */
752 spin_lock_init(&port->lock);
753 port->uartclk = ipb_freq / 2;
754 port->ops = &mpc52xx_uart_ops;
755 port->mapbase = res.start;
756 port->membase = ioremap(res.start, sizeof(struct mpc52xx_psc));
757 port->irq = irq_of_parse_and_map(np, 0);
759 if (port->membase == NULL)
760 return -EINVAL;
762 pr_debug("mpc52xx-psc uart at %p, mapped to %p, irq=%x, freq=%i\n",
763 (void*)port->mapbase, port->membase, port->irq, port->uartclk);
765 /* Setup the port parameters accoding to options */
766 if (options)
767 uart_parse_options(options, &baud, &parity, &bits, &flow);
768 else
769 mpc52xx_console_get_options(port, &baud, &parity, &bits, &flow);
771 pr_debug("Setting console parameters: %i %i%c1 flow=%c\n",
772 baud, bits, parity, flow);
774 return uart_set_options(port, co, baud, parity, bits, flow);
776 #endif /* defined(CONFIG_PPC_MERGE) */
779 static struct uart_driver mpc52xx_uart_driver;
781 static struct console mpc52xx_console = {
782 .name = "ttyPSC",
783 .write = mpc52xx_console_write,
784 .device = uart_console_device,
785 .setup = mpc52xx_console_setup,
786 .flags = CON_PRINTBUFFER,
787 .index = -1, /* Specified on the cmdline (e.g. console=ttyPSC0 ) */
788 .data = &mpc52xx_uart_driver,
792 static int __init
793 mpc52xx_console_init(void)
795 #if defined(CONFIG_PPC_MERGE)
796 mpc52xx_uart_of_enumerate();
797 #endif
798 register_console(&mpc52xx_console);
799 return 0;
802 console_initcall(mpc52xx_console_init);
804 #define MPC52xx_PSC_CONSOLE &mpc52xx_console
805 #else
806 #define MPC52xx_PSC_CONSOLE NULL
807 #endif
810 /* ======================================================================== */
811 /* UART Driver */
812 /* ======================================================================== */
814 static struct uart_driver mpc52xx_uart_driver = {
815 .owner = THIS_MODULE,
816 .driver_name = "mpc52xx_psc_uart",
817 .dev_name = "ttyPSC",
818 .major = SERIAL_PSC_MAJOR,
819 .minor = SERIAL_PSC_MINOR,
820 .nr = MPC52xx_PSC_MAXNUM,
821 .cons = MPC52xx_PSC_CONSOLE,
825 #if !defined(CONFIG_PPC_MERGE)
826 /* ======================================================================== */
827 /* Platform Driver */
828 /* ======================================================================== */
830 static int __devinit
831 mpc52xx_uart_probe(struct platform_device *dev)
833 struct resource *res = dev->resource;
835 struct uart_port *port = NULL;
836 int i, idx, ret;
838 /* Check validity & presence */
839 idx = dev->id;
840 if (idx < 0 || idx >= MPC52xx_PSC_MAXNUM)
841 return -EINVAL;
843 if (!mpc52xx_match_psc_function(idx,"uart"))
844 return -ENODEV;
846 /* Init the port structure */
847 port = &mpc52xx_uart_ports[idx];
849 spin_lock_init(&port->lock);
850 port->uartclk = __res.bi_ipbfreq / 2; /* Look at CTLR doc */
851 port->fifosize = 512;
852 port->iotype = UPIO_MEM;
853 port->flags = UPF_BOOT_AUTOCONF |
854 ( uart_console(port) ? 0 : UPF_IOREMAP );
855 port->line = idx;
856 port->ops = &mpc52xx_uart_ops;
857 port->dev = &dev->dev;
859 /* Search for IRQ and mapbase */
860 for (i=0 ; i<dev->num_resources ; i++, res++) {
861 if (res->flags & IORESOURCE_MEM)
862 port->mapbase = res->start;
863 else if (res->flags & IORESOURCE_IRQ)
864 port->irq = res->start;
866 if (!port->irq || !port->mapbase)
867 return -EINVAL;
869 /* Add the port to the uart sub-system */
870 ret = uart_add_one_port(&mpc52xx_uart_driver, port);
871 if (!ret)
872 platform_set_drvdata(dev, (void*)port);
874 return ret;
877 static int
878 mpc52xx_uart_remove(struct platform_device *dev)
880 struct uart_port *port = (struct uart_port *) platform_get_drvdata(dev);
882 platform_set_drvdata(dev, NULL);
884 if (port)
885 uart_remove_one_port(&mpc52xx_uart_driver, port);
887 return 0;
890 #ifdef CONFIG_PM
891 static int
892 mpc52xx_uart_suspend(struct platform_device *dev, pm_message_t state)
894 struct uart_port *port = (struct uart_port *) platform_get_drvdata(dev);
896 if (port)
897 uart_suspend_port(&mpc52xx_uart_driver, port);
899 return 0;
902 static int
903 mpc52xx_uart_resume(struct platform_device *dev)
905 struct uart_port *port = (struct uart_port *) platform_get_drvdata(dev);
907 if (port)
908 uart_resume_port(&mpc52xx_uart_driver, port);
910 return 0;
912 #endif
915 static struct platform_driver mpc52xx_uart_platform_driver = {
916 .probe = mpc52xx_uart_probe,
917 .remove = mpc52xx_uart_remove,
918 #ifdef CONFIG_PM
919 .suspend = mpc52xx_uart_suspend,
920 .resume = mpc52xx_uart_resume,
921 #endif
922 .driver = {
923 .name = "mpc52xx-psc",
926 #endif /* !defined(CONFIG_PPC_MERGE) */
929 #if defined(CONFIG_PPC_MERGE)
930 /* ======================================================================== */
931 /* OF Platform Driver */
932 /* ======================================================================== */
934 static int __devinit
935 mpc52xx_uart_of_probe(struct of_device *op, const struct of_device_id *match)
937 int idx = -1;
938 unsigned int ipb_freq;
939 struct uart_port *port = NULL;
940 struct resource res;
941 int ret;
943 dev_dbg(&op->dev, "mpc52xx_uart_probe(op=%p, match=%p)\n", op, match);
945 /* Check validity & presence */
946 for (idx = 0; idx < MPC52xx_PSC_MAXNUM; idx++)
947 if (mpc52xx_uart_nodes[idx] == op->node)
948 break;
949 if (idx >= MPC52xx_PSC_MAXNUM)
950 return -EINVAL;
951 pr_debug("Found %s assigned to ttyPSC%x\n",
952 mpc52xx_uart_nodes[idx]->full_name, idx);
954 /* Search for bus-frequency property in this node or a parent */
955 if ((ipb_freq = mpc52xx_find_ipb_freq(op->node)) == 0) {
956 dev_dbg(&op->dev, "Could not find IPB bus frequency!\n");
957 return -EINVAL;
960 /* Init the port structure */
961 port = &mpc52xx_uart_ports[idx];
963 spin_lock_init(&port->lock);
964 port->uartclk = ipb_freq / 2;
965 port->fifosize = 512;
966 port->iotype = UPIO_MEM;
967 port->flags = UPF_BOOT_AUTOCONF |
968 ( uart_console(port) ? 0 : UPF_IOREMAP );
969 port->line = idx;
970 port->ops = &mpc52xx_uart_ops;
971 port->dev = &op->dev;
973 /* Search for IRQ and mapbase */
974 if ((ret = of_address_to_resource(op->node, 0, &res)) != 0)
975 return ret;
977 port->mapbase = res.start;
978 port->irq = irq_of_parse_and_map(op->node, 0);
980 dev_dbg(&op->dev, "mpc52xx-psc uart at %p, irq=%x, freq=%i\n",
981 (void*)port->mapbase, port->irq, port->uartclk);
983 if ((port->irq==NO_IRQ) || !port->mapbase) {
984 printk(KERN_ERR "Could not allocate resources for PSC\n");
985 return -EINVAL;
988 /* Add the port to the uart sub-system */
989 ret = uart_add_one_port(&mpc52xx_uart_driver, port);
990 if (!ret)
991 dev_set_drvdata(&op->dev, (void*)port);
993 return ret;
996 static int
997 mpc52xx_uart_of_remove(struct of_device *op)
999 struct uart_port *port = dev_get_drvdata(&op->dev);
1000 dev_set_drvdata(&op->dev, NULL);
1002 if (port) {
1003 uart_remove_one_port(&mpc52xx_uart_driver, port);
1004 irq_dispose_mapping(port->irq);
1007 return 0;
1010 #ifdef CONFIG_PM
1011 static int
1012 mpc52xx_uart_of_suspend(struct of_device *op, pm_message_t state)
1014 struct uart_port *port = (struct uart_port *) dev_get_drvdata(&op->dev);
1016 if (port)
1017 uart_suspend_port(&mpc52xx_uart_driver, port);
1019 return 0;
1022 static int
1023 mpc52xx_uart_of_resume(struct of_device *op)
1025 struct uart_port *port = (struct uart_port *) dev_get_drvdata(&op->dev);
1027 if (port)
1028 uart_resume_port(&mpc52xx_uart_driver, port);
1030 return 0;
1032 #endif
1034 static void
1035 mpc52xx_uart_of_assign(struct device_node *np, int idx)
1037 int free_idx = -1;
1038 int i;
1040 /* Find the first free node */
1041 for (i = 0; i < MPC52xx_PSC_MAXNUM; i++) {
1042 if (mpc52xx_uart_nodes[i] == NULL) {
1043 free_idx = i;
1044 break;
1048 if ((idx < 0) || (idx >= MPC52xx_PSC_MAXNUM))
1049 idx = free_idx;
1051 if (idx < 0)
1052 return; /* No free slot; abort */
1054 /* If the slot is already occupied, then swap slots */
1055 if (mpc52xx_uart_nodes[idx] && (free_idx != -1))
1056 mpc52xx_uart_nodes[free_idx] = mpc52xx_uart_nodes[idx];
1057 mpc52xx_uart_nodes[idx] = np;
1060 static void
1061 mpc52xx_uart_of_enumerate(void)
1063 static int enum_done = 0;
1064 struct device_node *np;
1065 const unsigned int *devno;
1066 int i;
1068 if (enum_done)
1069 return;
1071 for_each_node_by_type(np, "serial") {
1072 if (!of_match_node(mpc52xx_uart_of_match, np))
1073 continue;
1075 /* Is a particular device number requested? */
1076 devno = of_get_property(np, "port-number", NULL);
1077 mpc52xx_uart_of_assign(of_node_get(np), devno ? *devno : -1);
1080 enum_done = 1;
1082 for (i = 0; i < MPC52xx_PSC_MAXNUM; i++) {
1083 if (mpc52xx_uart_nodes[i])
1084 pr_debug("%s assigned to ttyPSC%x\n",
1085 mpc52xx_uart_nodes[i]->full_name, i);
1089 MODULE_DEVICE_TABLE(of, mpc52xx_uart_of_match);
1091 static struct of_platform_driver mpc52xx_uart_of_driver = {
1092 .owner = THIS_MODULE,
1093 .name = "mpc52xx-psc-uart",
1094 .match_table = mpc52xx_uart_of_match,
1095 .probe = mpc52xx_uart_of_probe,
1096 .remove = mpc52xx_uart_of_remove,
1097 #ifdef CONFIG_PM
1098 .suspend = mpc52xx_uart_of_suspend,
1099 .resume = mpc52xx_uart_of_resume,
1100 #endif
1101 .driver = {
1102 .name = "mpc52xx-psc-uart",
1105 #endif /* defined(CONFIG_PPC_MERGE) */
1108 /* ======================================================================== */
1109 /* Module */
1110 /* ======================================================================== */
1112 static int __init
1113 mpc52xx_uart_init(void)
1115 int ret;
1117 printk(KERN_INFO "Serial: MPC52xx PSC UART driver\n");
1119 if ((ret = uart_register_driver(&mpc52xx_uart_driver)) != 0) {
1120 printk(KERN_ERR "%s: uart_register_driver failed (%i)\n",
1121 __FILE__, ret);
1122 return ret;
1125 #if defined(CONFIG_PPC_MERGE)
1126 mpc52xx_uart_of_enumerate();
1128 ret = of_register_platform_driver(&mpc52xx_uart_of_driver);
1129 if (ret) {
1130 printk(KERN_ERR "%s: of_register_platform_driver failed (%i)\n",
1131 __FILE__, ret);
1132 uart_unregister_driver(&mpc52xx_uart_driver);
1133 return ret;
1135 #else
1136 ret = platform_driver_register(&mpc52xx_uart_platform_driver);
1137 if (ret) {
1138 printk(KERN_ERR "%s: platform_driver_register failed (%i)\n",
1139 __FILE__, ret);
1140 uart_unregister_driver(&mpc52xx_uart_driver);
1141 return ret;
1143 #endif
1145 return 0;
1148 static void __exit
1149 mpc52xx_uart_exit(void)
1151 #if defined(CONFIG_PPC_MERGE)
1152 of_unregister_platform_driver(&mpc52xx_uart_of_driver);
1153 #else
1154 platform_driver_unregister(&mpc52xx_uart_platform_driver);
1155 #endif
1156 uart_unregister_driver(&mpc52xx_uart_driver);
1160 module_init(mpc52xx_uart_init);
1161 module_exit(mpc52xx_uart_exit);
1163 MODULE_AUTHOR("Sylvain Munaut <tnt@246tNt.com>");
1164 MODULE_DESCRIPTION("Freescale MPC52xx PSC UART");
1165 MODULE_LICENSE("GPL");