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
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#>'
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.
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>
74 #if defined(CONFIG_PPC_MERGE)
75 #include <asm/of_platform.h>
77 #include <linux/platform_device.h>
80 #include <asm/mpc52xx.h>
81 #include <asm/mpc52xx_psc.h>
83 #if defined(CONFIG_SERIAL_MPC52xx_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
87 #include <linux/serial_core.h>
90 /* We've been assigned a range on the "Low-density serial ports" major */
91 #define SERIAL_PSC_MAJOR 204
92 #define SERIAL_PSC_MINOR 148
95 #define ISR_PASS_LIMIT 256 /* Max number of iteration in the interrupt */
98 static struct uart_port mpc52xx_uart_ports[MPC52xx_PSC_MAXNUM];
99 /* Rem: - We use the read_status_mask as a shadow of
100 * psc->mpc52xx_psc_imr
101 * - It's important that is array is all zero on start as we
102 * use it to know if it's initialized or not ! If it's not sure
103 * it's cleared, then a memset(...,0,...) should be added to
106 #if defined(CONFIG_PPC_MERGE)
107 /* lookup table for matching device nodes to index numbers */
108 static struct device_node *mpc52xx_uart_nodes[MPC52xx_PSC_MAXNUM];
110 static void mpc52xx_uart_of_enumerate(void);
113 #define PSC(port) ((struct mpc52xx_psc __iomem *)((port)->membase))
116 /* Forward declaration of the interruption handling routine */
117 static irqreturn_t mpc52xx_uart_int(int irq,void *dev_id);
120 /* Simple macro to test if a port is console or not. This one is taken
121 * for serial_core.c and maybe should be moved to serial_core.h ? */
122 #ifdef CONFIG_SERIAL_CORE_CONSOLE
123 #define uart_console(port) ((port)->cons && (port)->cons->index == (port)->line)
125 #define uart_console(port) (0)
128 #if defined(CONFIG_PPC_MERGE)
129 static struct of_device_id mpc52xx_uart_of_match[] = {
130 { .type = "serial", .compatible = "mpc5200-psc-uart", },
136 /* ======================================================================== */
137 /* UART operations */
138 /* ======================================================================== */
141 mpc52xx_uart_tx_empty(struct uart_port *port)
143 int status = in_be16(&PSC(port)->mpc52xx_psc_status);
144 return (status & MPC52xx_PSC_SR_TXEMP) ? TIOCSER_TEMT : 0;
148 mpc52xx_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
150 /* Not implemented */
154 mpc52xx_uart_get_mctrl(struct uart_port *port)
156 /* Not implemented */
157 return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
161 mpc52xx_uart_stop_tx(struct uart_port *port)
163 /* port->lock taken by caller */
164 port->read_status_mask &= ~MPC52xx_PSC_IMR_TXRDY;
165 out_be16(&PSC(port)->mpc52xx_psc_imr,port->read_status_mask);
169 mpc52xx_uart_start_tx(struct uart_port *port)
171 /* port->lock taken by caller */
172 port->read_status_mask |= MPC52xx_PSC_IMR_TXRDY;
173 out_be16(&PSC(port)->mpc52xx_psc_imr,port->read_status_mask);
177 mpc52xx_uart_send_xchar(struct uart_port *port, char ch)
180 spin_lock_irqsave(&port->lock, flags);
184 /* Make sure tx interrupts are on */
185 /* Truly necessary ??? They should be anyway */
186 port->read_status_mask |= MPC52xx_PSC_IMR_TXRDY;
187 out_be16(&PSC(port)->mpc52xx_psc_imr,port->read_status_mask);
190 spin_unlock_irqrestore(&port->lock, flags);
194 mpc52xx_uart_stop_rx(struct uart_port *port)
196 /* port->lock taken by caller */
197 port->read_status_mask &= ~MPC52xx_PSC_IMR_RXRDY;
198 out_be16(&PSC(port)->mpc52xx_psc_imr,port->read_status_mask);
202 mpc52xx_uart_enable_ms(struct uart_port *port)
204 /* Not implemented */
208 mpc52xx_uart_break_ctl(struct uart_port *port, int ctl)
211 spin_lock_irqsave(&port->lock, flags);
214 out_8(&PSC(port)->command,MPC52xx_PSC_START_BRK);
216 out_8(&PSC(port)->command,MPC52xx_PSC_STOP_BRK);
218 spin_unlock_irqrestore(&port->lock, flags);
222 mpc52xx_uart_startup(struct uart_port *port)
224 struct mpc52xx_psc __iomem *psc = PSC(port);
228 ret = request_irq(port->irq, mpc52xx_uart_int,
229 IRQF_DISABLED | IRQF_SAMPLE_RANDOM, "mpc52xx_psc_uart", port);
233 /* Reset/activate the port, clear and enable interrupts */
234 out_8(&psc->command,MPC52xx_PSC_RST_RX);
235 out_8(&psc->command,MPC52xx_PSC_RST_TX);
237 out_be32(&psc->sicr,0); /* UART mode DCD ignored */
239 out_be16(&psc->mpc52xx_psc_clock_select, 0xdd00); /* /16 prescaler on */
241 out_8(&psc->rfcntl, 0x00);
242 out_be16(&psc->rfalarm, 0x1ff);
243 out_8(&psc->tfcntl, 0x07);
244 out_be16(&psc->tfalarm, 0x80);
246 port->read_status_mask |= MPC52xx_PSC_IMR_RXRDY | MPC52xx_PSC_IMR_TXRDY;
247 out_be16(&psc->mpc52xx_psc_imr,port->read_status_mask);
249 out_8(&psc->command,MPC52xx_PSC_TX_ENABLE);
250 out_8(&psc->command,MPC52xx_PSC_RX_ENABLE);
256 mpc52xx_uart_shutdown(struct uart_port *port)
258 struct mpc52xx_psc __iomem *psc = PSC(port);
260 /* Shut down the port, interrupt and all */
261 out_8(&psc->command,MPC52xx_PSC_RST_RX);
262 out_8(&psc->command,MPC52xx_PSC_RST_TX);
264 port->read_status_mask = 0;
265 out_be16(&psc->mpc52xx_psc_imr,port->read_status_mask);
267 /* Release interrupt */
268 free_irq(port->irq, port);
272 mpc52xx_uart_set_termios(struct uart_port *port, struct ktermios *new,
273 struct ktermios *old)
275 struct mpc52xx_psc __iomem *psc = PSC(port);
277 unsigned char mr1, mr2;
279 unsigned int j, baud, quot;
281 /* Prepare what we're gonna write */
284 switch (new->c_cflag & CSIZE) {
285 case CS5: mr1 |= MPC52xx_PSC_MODE_5_BITS;
287 case CS6: mr1 |= MPC52xx_PSC_MODE_6_BITS;
289 case CS7: mr1 |= MPC52xx_PSC_MODE_7_BITS;
292 default: mr1 |= MPC52xx_PSC_MODE_8_BITS;
295 if (new->c_cflag & PARENB) {
296 mr1 |= (new->c_cflag & PARODD) ?
297 MPC52xx_PSC_MODE_PARODD : MPC52xx_PSC_MODE_PAREVEN;
299 mr1 |= MPC52xx_PSC_MODE_PARNONE;
304 if (new->c_cflag & CSTOPB)
305 mr2 |= MPC52xx_PSC_MODE_TWO_STOP;
307 mr2 |= ((new->c_cflag & CSIZE) == CS5) ?
308 MPC52xx_PSC_MODE_ONE_STOP_5_BITS :
309 MPC52xx_PSC_MODE_ONE_STOP;
312 baud = uart_get_baud_rate(port, new, old, 0, port->uartclk/16);
313 quot = uart_get_divisor(port, baud);
317 spin_lock_irqsave(&port->lock, flags);
319 /* Update the per-port timeout */
320 uart_update_timeout(port, new->c_cflag, baud);
322 /* Do our best to flush TX & RX, so we don't loose anything */
323 /* But we don't wait indefinitly ! */
324 j = 5000000; /* Maximum wait */
325 /* FIXME Can't receive chars since set_termios might be called at early
326 * boot for the console, all stuff is not yet ready to receive at that
327 * time and that just makes the kernel oops */
328 /* while (j-- && mpc52xx_uart_int_rx_chars(port)); */
329 while (!(in_be16(&psc->mpc52xx_psc_status) & MPC52xx_PSC_SR_TXEMP) &&
334 printk( KERN_ERR "mpc52xx_uart.c: "
335 "Unable to flush RX & TX fifos in-time in set_termios."
336 "Some chars may have been lost.\n" );
338 /* Reset the TX & RX */
339 out_8(&psc->command,MPC52xx_PSC_RST_RX);
340 out_8(&psc->command,MPC52xx_PSC_RST_TX);
342 /* Send new mode settings */
343 out_8(&psc->command,MPC52xx_PSC_SEL_MODE_REG_1);
344 out_8(&psc->mode,mr1);
345 out_8(&psc->mode,mr2);
346 out_8(&psc->ctur,ctr >> 8);
347 out_8(&psc->ctlr,ctr & 0xff);
349 /* Reenable TX & RX */
350 out_8(&psc->command,MPC52xx_PSC_TX_ENABLE);
351 out_8(&psc->command,MPC52xx_PSC_RX_ENABLE);
353 /* We're all set, release the lock */
354 spin_unlock_irqrestore(&port->lock, flags);
358 mpc52xx_uart_type(struct uart_port *port)
360 return port->type == PORT_MPC52xx ? "MPC52xx PSC" : NULL;
364 mpc52xx_uart_release_port(struct uart_port *port)
366 if (port->flags & UPF_IOREMAP) { /* remapped by us ? */
367 iounmap(port->membase);
368 port->membase = NULL;
371 release_mem_region(port->mapbase, sizeof(struct mpc52xx_psc));
375 mpc52xx_uart_request_port(struct uart_port *port)
379 if (port->flags & UPF_IOREMAP) /* Need to remap ? */
380 port->membase = ioremap(port->mapbase,
381 sizeof(struct mpc52xx_psc));
386 err = request_mem_region(port->mapbase, sizeof(struct mpc52xx_psc),
387 "mpc52xx_psc_uart") != NULL ? 0 : -EBUSY;
389 if (err && (port->flags & UPF_IOREMAP)) {
390 iounmap(port->membase);
391 port->membase = NULL;
398 mpc52xx_uart_config_port(struct uart_port *port, int flags)
400 if ( (flags & UART_CONFIG_TYPE) &&
401 (mpc52xx_uart_request_port(port) == 0) )
402 port->type = PORT_MPC52xx;
406 mpc52xx_uart_verify_port(struct uart_port *port, struct serial_struct *ser)
408 if ( ser->type != PORT_UNKNOWN && ser->type != PORT_MPC52xx )
411 if ( (ser->irq != port->irq) ||
412 (ser->io_type != SERIAL_IO_MEM) ||
413 (ser->baud_base != port->uartclk) ||
414 (ser->iomem_base != (void*)port->mapbase) ||
422 static struct uart_ops mpc52xx_uart_ops = {
423 .tx_empty = mpc52xx_uart_tx_empty,
424 .set_mctrl = mpc52xx_uart_set_mctrl,
425 .get_mctrl = mpc52xx_uart_get_mctrl,
426 .stop_tx = mpc52xx_uart_stop_tx,
427 .start_tx = mpc52xx_uart_start_tx,
428 .send_xchar = mpc52xx_uart_send_xchar,
429 .stop_rx = mpc52xx_uart_stop_rx,
430 .enable_ms = mpc52xx_uart_enable_ms,
431 .break_ctl = mpc52xx_uart_break_ctl,
432 .startup = mpc52xx_uart_startup,
433 .shutdown = mpc52xx_uart_shutdown,
434 .set_termios = mpc52xx_uart_set_termios,
435 /* .pm = mpc52xx_uart_pm, Not supported yet */
436 /* .set_wake = mpc52xx_uart_set_wake, Not supported yet */
437 .type = mpc52xx_uart_type,
438 .release_port = mpc52xx_uart_release_port,
439 .request_port = mpc52xx_uart_request_port,
440 .config_port = mpc52xx_uart_config_port,
441 .verify_port = mpc52xx_uart_verify_port
445 /* ======================================================================== */
446 /* Interrupt handling */
447 /* ======================================================================== */
450 mpc52xx_uart_int_rx_chars(struct uart_port *port)
452 struct tty_struct *tty = port->info->tty;
453 unsigned char ch, flag;
454 unsigned short status;
456 /* While we can read, do so ! */
457 while ( (status = in_be16(&PSC(port)->mpc52xx_psc_status)) &
458 MPC52xx_PSC_SR_RXRDY) {
461 ch = in_8(&PSC(port)->mpc52xx_psc_buffer_8);
463 /* Handle sysreq char */
465 if (uart_handle_sysrq_char(port, ch)) {
476 if ( status & (MPC52xx_PSC_SR_PE |
478 MPC52xx_PSC_SR_RB) ) {
480 if (status & MPC52xx_PSC_SR_RB) {
482 uart_handle_break(port);
483 } else if (status & MPC52xx_PSC_SR_PE)
485 else if (status & MPC52xx_PSC_SR_FE)
488 /* Clear error condition */
489 out_8(&PSC(port)->command,MPC52xx_PSC_RST_ERR_STAT);
492 tty_insert_flip_char(tty, ch, flag);
493 if (status & MPC52xx_PSC_SR_OE) {
495 * Overrun is special, since it's
496 * reported immediately, and doesn't
497 * affect the current character
499 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
503 tty_flip_buffer_push(tty);
505 return in_be16(&PSC(port)->mpc52xx_psc_status) & MPC52xx_PSC_SR_RXRDY;
509 mpc52xx_uart_int_tx_chars(struct uart_port *port)
511 struct circ_buf *xmit = &port->info->xmit;
513 /* Process out of band chars */
515 out_8(&PSC(port)->mpc52xx_psc_buffer_8, port->x_char);
521 /* Nothing to do ? */
522 if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
523 mpc52xx_uart_stop_tx(port);
528 while (in_be16(&PSC(port)->mpc52xx_psc_status) & MPC52xx_PSC_SR_TXRDY) {
529 out_8(&PSC(port)->mpc52xx_psc_buffer_8, xmit->buf[xmit->tail]);
530 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
532 if (uart_circ_empty(xmit))
537 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
538 uart_write_wakeup(port);
540 /* Maybe we're done after all */
541 if (uart_circ_empty(xmit)) {
542 mpc52xx_uart_stop_tx(port);
550 mpc52xx_uart_int(int irq, void *dev_id)
552 struct uart_port *port = dev_id;
553 unsigned long pass = ISR_PASS_LIMIT;
554 unsigned int keepgoing;
555 unsigned short status;
557 spin_lock(&port->lock);
559 /* While we have stuff to do, we continue */
561 /* If we don't find anything to do, we stop */
565 status = in_be16(&PSC(port)->mpc52xx_psc_isr);
566 status &= port->read_status_mask;
568 /* Do we need to receive chars ? */
569 /* For this RX interrupts must be on and some chars waiting */
570 if ( status & MPC52xx_PSC_IMR_RXRDY )
571 keepgoing |= mpc52xx_uart_int_rx_chars(port);
573 /* Do we need to send chars ? */
574 /* For this, TX must be ready and TX interrupt enabled */
575 if ( status & MPC52xx_PSC_IMR_TXRDY )
576 keepgoing |= mpc52xx_uart_int_tx_chars(port);
578 /* Limit number of iteration */
584 spin_unlock(&port->lock);
590 /* ======================================================================== */
591 /* Console ( if applicable ) */
592 /* ======================================================================== */
594 #ifdef CONFIG_SERIAL_MPC52xx_CONSOLE
597 mpc52xx_console_get_options(struct uart_port *port,
598 int *baud, int *parity, int *bits, int *flow)
600 struct mpc52xx_psc __iomem *psc = PSC(port);
603 pr_debug("mpc52xx_console_get_options(port=%p)\n", port);
605 /* Read the mode registers */
606 out_8(&psc->command,MPC52xx_PSC_SEL_MODE_REG_1);
607 mr1 = in_8(&psc->mode);
609 /* CT{U,L}R are write-only ! */
610 *baud = CONFIG_SERIAL_MPC52xx_CONSOLE_BAUD;
611 #if !defined(CONFIG_PPC_MERGE)
612 if (__res.bi_baudrate)
613 *baud = __res.bi_baudrate;
617 switch (mr1 & MPC52xx_PSC_MODE_BITS_MASK) {
618 case MPC52xx_PSC_MODE_5_BITS: *bits = 5; break;
619 case MPC52xx_PSC_MODE_6_BITS: *bits = 6; break;
620 case MPC52xx_PSC_MODE_7_BITS: *bits = 7; break;
621 case MPC52xx_PSC_MODE_8_BITS:
625 if (mr1 & MPC52xx_PSC_MODE_PARNONE)
628 *parity = mr1 & MPC52xx_PSC_MODE_PARODD ? 'o' : 'e';
632 mpc52xx_console_write(struct console *co, const char *s, unsigned int count)
634 struct uart_port *port = &mpc52xx_uart_ports[co->index];
635 struct mpc52xx_psc __iomem *psc = PSC(port);
638 /* Disable interrupts */
639 out_be16(&psc->mpc52xx_psc_imr, 0);
641 /* Wait the TX buffer to be empty */
642 j = 5000000; /* Maximum wait */
643 while (!(in_be16(&psc->mpc52xx_psc_status) & MPC52xx_PSC_SR_TXEMP) &&
647 /* Write all the chars */
648 for (i = 0; i < count; i++, s++) {
649 /* Line return handling */
651 out_8(&psc->mpc52xx_psc_buffer_8, '\r');
654 out_8(&psc->mpc52xx_psc_buffer_8, *s);
656 /* Wait the TX buffer to be empty */
657 j = 20000; /* Maximum wait */
658 while (!(in_be16(&psc->mpc52xx_psc_status) &
659 MPC52xx_PSC_SR_TXEMP) && --j)
663 /* Restore interrupt state */
664 out_be16(&psc->mpc52xx_psc_imr, port->read_status_mask);
667 #if !defined(CONFIG_PPC_MERGE)
669 mpc52xx_console_setup(struct console *co, char *options)
671 struct uart_port *port = &mpc52xx_uart_ports[co->index];
673 int baud = CONFIG_SERIAL_MPC52xx_CONSOLE_BAUD;
678 if (co->index < 0 || co->index >= MPC52xx_PSC_MAXNUM)
681 /* Basic port init. Needed since we use some uart_??? func before
682 * real init for early access */
683 spin_lock_init(&port->lock);
684 port->uartclk = __res.bi_ipbfreq / 2; /* Look at CTLR doc */
685 port->ops = &mpc52xx_uart_ops;
686 port->mapbase = MPC52xx_PA(MPC52xx_PSCx_OFFSET(co->index+1));
688 /* We ioremap ourself */
689 port->membase = ioremap(port->mapbase, MPC52xx_PSC_SIZE);
690 if (port->membase == NULL)
693 /* Setup the port parameters accoding to options */
695 uart_parse_options(options, &baud, &parity, &bits, &flow);
697 mpc52xx_console_get_options(port, &baud, &parity, &bits, &flow);
699 return uart_set_options(port, co, baud, parity, bits, flow);
705 mpc52xx_console_setup(struct console *co, char *options)
707 struct uart_port *port = &mpc52xx_uart_ports[co->index];
708 struct device_node *np = mpc52xx_uart_nodes[co->index];
709 unsigned int ipb_freq;
713 int baud = CONFIG_SERIAL_MPC52xx_CONSOLE_BAUD;
718 pr_debug("mpc52xx_console_setup co=%p, co->index=%i, options=%s\n",
719 co, co->index, options);
721 if ((co->index < 0) || (co->index > MPC52xx_PSC_MAXNUM)) {
722 pr_debug("PSC%x out of range\n", co->index);
727 pr_debug("PSC%x not found in device tree\n", co->index);
731 pr_debug("Console on ttyPSC%x is %s\n",
732 co->index, mpc52xx_uart_nodes[co->index]->full_name);
734 /* Fetch register locations */
735 if ((ret = of_address_to_resource(np, 0, &res)) != 0) {
736 pr_debug("Could not get resources for PSC%x\n", co->index);
740 /* Search for bus-frequency property in this node or a parent */
741 if ((ipb_freq = mpc52xx_find_ipb_freq(np)) == 0) {
742 pr_debug("Could not find IPB bus frequency!\n");
746 /* Basic port init. Needed since we use some uart_??? func before
747 * real init for early access */
748 spin_lock_init(&port->lock);
749 port->uartclk = ipb_freq / 2;
750 port->ops = &mpc52xx_uart_ops;
751 port->mapbase = res.start;
752 port->membase = ioremap(res.start, sizeof(struct mpc52xx_psc));
753 port->irq = irq_of_parse_and_map(np, 0);
755 if (port->membase == NULL)
758 pr_debug("mpc52xx-psc uart at %lx, mapped to %p, irq=%x, freq=%i\n",
759 port->mapbase, port->membase, port->irq, port->uartclk);
761 /* Setup the port parameters accoding to options */
763 uart_parse_options(options, &baud, &parity, &bits, &flow);
765 mpc52xx_console_get_options(port, &baud, &parity, &bits, &flow);
767 pr_debug("Setting console parameters: %i %i%c1 flow=%c\n",
768 baud, bits, parity, flow);
770 return uart_set_options(port, co, baud, parity, bits, flow);
772 #endif /* defined(CONFIG_PPC_MERGE) */
775 static struct uart_driver mpc52xx_uart_driver;
777 static struct console mpc52xx_console = {
779 .write = mpc52xx_console_write,
780 .device = uart_console_device,
781 .setup = mpc52xx_console_setup,
782 .flags = CON_PRINTBUFFER,
783 .index = -1, /* Specified on the cmdline (e.g. console=ttyPSC0 ) */
784 .data = &mpc52xx_uart_driver,
789 mpc52xx_console_init(void)
791 #if defined(CONFIG_PPC_MERGE)
792 mpc52xx_uart_of_enumerate();
794 register_console(&mpc52xx_console);
798 console_initcall(mpc52xx_console_init);
800 #define MPC52xx_PSC_CONSOLE &mpc52xx_console
802 #define MPC52xx_PSC_CONSOLE NULL
806 /* ======================================================================== */
808 /* ======================================================================== */
810 static struct uart_driver mpc52xx_uart_driver = {
811 .owner = THIS_MODULE,
812 .driver_name = "mpc52xx_psc_uart",
813 .dev_name = "ttyPSC",
814 .major = SERIAL_PSC_MAJOR,
815 .minor = SERIAL_PSC_MINOR,
816 .nr = MPC52xx_PSC_MAXNUM,
817 .cons = MPC52xx_PSC_CONSOLE,
821 #if !defined(CONFIG_PPC_MERGE)
822 /* ======================================================================== */
823 /* Platform Driver */
824 /* ======================================================================== */
827 mpc52xx_uart_probe(struct platform_device *dev)
829 struct resource *res = dev->resource;
831 struct uart_port *port = NULL;
834 /* Check validity & presence */
836 if (idx < 0 || idx >= MPC52xx_PSC_MAXNUM)
839 if (!mpc52xx_match_psc_function(idx,"uart"))
842 /* Init the port structure */
843 port = &mpc52xx_uart_ports[idx];
845 spin_lock_init(&port->lock);
846 port->uartclk = __res.bi_ipbfreq / 2; /* Look at CTLR doc */
847 port->fifosize = 512;
848 port->iotype = UPIO_MEM;
849 port->flags = UPF_BOOT_AUTOCONF |
850 ( uart_console(port) ? 0 : UPF_IOREMAP );
852 port->ops = &mpc52xx_uart_ops;
853 port->dev = &dev->dev;
855 /* Search for IRQ and mapbase */
856 for (i=0 ; i<dev->num_resources ; i++, res++) {
857 if (res->flags & IORESOURCE_MEM)
858 port->mapbase = res->start;
859 else if (res->flags & IORESOURCE_IRQ)
860 port->irq = res->start;
862 if (!port->irq || !port->mapbase)
865 /* Add the port to the uart sub-system */
866 ret = uart_add_one_port(&mpc52xx_uart_driver, port);
868 platform_set_drvdata(dev, (void*)port);
874 mpc52xx_uart_remove(struct platform_device *dev)
876 struct uart_port *port = (struct uart_port *) platform_get_drvdata(dev);
878 platform_set_drvdata(dev, NULL);
881 uart_remove_one_port(&mpc52xx_uart_driver, port);
888 mpc52xx_uart_suspend(struct platform_device *dev, pm_message_t state)
890 struct uart_port *port = (struct uart_port *) platform_get_drvdata(dev);
893 uart_suspend_port(&mpc52xx_uart_driver, port);
899 mpc52xx_uart_resume(struct platform_device *dev)
901 struct uart_port *port = (struct uart_port *) platform_get_drvdata(dev);
904 uart_resume_port(&mpc52xx_uart_driver, port);
911 static struct platform_driver mpc52xx_uart_platform_driver = {
912 .probe = mpc52xx_uart_probe,
913 .remove = mpc52xx_uart_remove,
915 .suspend = mpc52xx_uart_suspend,
916 .resume = mpc52xx_uart_resume,
919 .name = "mpc52xx-psc",
922 #endif /* !defined(CONFIG_PPC_MERGE) */
925 #if defined(CONFIG_PPC_MERGE)
926 /* ======================================================================== */
927 /* OF Platform Driver */
928 /* ======================================================================== */
931 mpc52xx_uart_of_probe(struct of_device *op, const struct of_device_id *match)
934 unsigned int ipb_freq;
935 struct uart_port *port = NULL;
939 dev_dbg(&op->dev, "mpc52xx_uart_probe(op=%p, match=%p)\n", op, match);
941 /* Check validity & presence */
942 for (idx = 0; idx < MPC52xx_PSC_MAXNUM; idx++)
943 if (mpc52xx_uart_nodes[idx] == op->node)
945 if (idx >= MPC52xx_PSC_MAXNUM)
947 pr_debug("Found %s assigned to ttyPSC%x\n",
948 mpc52xx_uart_nodes[idx]->full_name, idx);
950 /* Search for bus-frequency property in this node or a parent */
951 if ((ipb_freq = mpc52xx_find_ipb_freq(op->node)) == 0) {
952 dev_dbg(&op->dev, "Could not find IPB bus frequency!\n");
956 /* Init the port structure */
957 port = &mpc52xx_uart_ports[idx];
959 spin_lock_init(&port->lock);
960 port->uartclk = ipb_freq / 2;
961 port->fifosize = 512;
962 port->iotype = UPIO_MEM;
963 port->flags = UPF_BOOT_AUTOCONF |
964 ( uart_console(port) ? 0 : UPF_IOREMAP );
966 port->ops = &mpc52xx_uart_ops;
967 port->dev = &op->dev;
969 /* Search for IRQ and mapbase */
970 if ((ret = of_address_to_resource(op->node, 0, &res)) != 0)
973 port->mapbase = res.start;
974 port->irq = irq_of_parse_and_map(op->node, 0);
976 dev_dbg(&op->dev, "mpc52xx-psc uart at %lx, irq=%x, freq=%i\n",
977 port->mapbase, port->irq, port->uartclk);
979 if ((port->irq==NO_IRQ) || !port->mapbase) {
980 printk(KERN_ERR "Could not allocate resources for PSC\n");
984 /* Add the port to the uart sub-system */
985 ret = uart_add_one_port(&mpc52xx_uart_driver, port);
987 dev_set_drvdata(&op->dev, (void*)port);
993 mpc52xx_uart_of_remove(struct of_device *op)
995 struct uart_port *port = dev_get_drvdata(&op->dev);
996 dev_set_drvdata(&op->dev, NULL);
999 uart_remove_one_port(&mpc52xx_uart_driver, port);
1000 irq_dispose_mapping(port->irq);
1008 mpc52xx_uart_of_suspend(struct of_device *op, pm_message_t state)
1010 struct uart_port *port = (struct uart_port *) dev_get_drvdata(&op->dev);
1013 uart_suspend_port(&mpc52xx_uart_driver, port);
1019 mpc52xx_uart_of_resume(struct of_device *op)
1021 struct uart_port *port = (struct uart_port *) dev_get_drvdata(&op->dev);
1024 uart_resume_port(&mpc52xx_uart_driver, port);
1031 mpc52xx_uart_of_assign(struct device_node *np, int idx)
1036 /* Find the first free node */
1037 for (i = 0; i < MPC52xx_PSC_MAXNUM; i++) {
1038 if (mpc52xx_uart_nodes[i] == NULL) {
1044 if ((idx < 0) || (idx >= MPC52xx_PSC_MAXNUM))
1048 return; /* No free slot; abort */
1050 /* If the slot is already occupied, then swap slots */
1051 if (mpc52xx_uart_nodes[idx] && (free_idx != -1))
1052 mpc52xx_uart_nodes[free_idx] = mpc52xx_uart_nodes[idx];
1053 mpc52xx_uart_nodes[i] = np;
1057 mpc52xx_uart_of_enumerate(void)
1059 static int enum_done = 0;
1060 struct device_node *np;
1061 const unsigned int *devno;
1067 for_each_node_by_type(np, "serial") {
1068 if (!of_match_node(mpc52xx_uart_of_match, np))
1071 /* Is a particular device number requested? */
1072 devno = get_property(np, "port-number", NULL);
1073 mpc52xx_uart_of_assign(of_node_get(np), devno ? *devno : -1);
1078 for (i = 0; i < MPC52xx_PSC_MAXNUM; i++) {
1079 if (mpc52xx_uart_nodes[i])
1080 pr_debug("%s assigned to ttyPSC%x\n",
1081 mpc52xx_uart_nodes[i]->full_name, i);
1085 MODULE_DEVICE_TABLE(of, mpc52xx_uart_of_match);
1087 static struct of_platform_driver mpc52xx_uart_of_driver = {
1088 .owner = THIS_MODULE,
1089 .name = "mpc52xx-psc-uart",
1090 .match_table = mpc52xx_uart_of_match,
1091 .probe = mpc52xx_uart_of_probe,
1092 .remove = mpc52xx_uart_of_remove,
1094 .suspend = mpc52xx_uart_of_suspend,
1095 .resume = mpc52xx_uart_of_resume,
1098 .name = "mpc52xx-psc-uart",
1101 #endif /* defined(CONFIG_PPC_MERGE) */
1104 /* ======================================================================== */
1106 /* ======================================================================== */
1109 mpc52xx_uart_init(void)
1113 printk(KERN_INFO "Serial: MPC52xx PSC UART driver\n");
1115 if ((ret = uart_register_driver(&mpc52xx_uart_driver)) != 0) {
1116 printk(KERN_ERR "%s: uart_register_driver failed (%i)\n",
1121 #if defined(CONFIG_PPC_MERGE)
1122 mpc52xx_uart_of_enumerate();
1124 ret = of_register_platform_driver(&mpc52xx_uart_of_driver);
1126 printk(KERN_ERR "%s: of_register_platform_driver failed (%i)\n",
1128 uart_unregister_driver(&mpc52xx_uart_driver);
1132 ret = platform_driver_register(&mpc52xx_uart_platform_driver);
1134 printk(KERN_ERR "%s: platform_driver_register failed (%i)\n",
1136 uart_unregister_driver(&mpc52xx_uart_driver);
1145 mpc52xx_uart_exit(void)
1147 #if defined(CONFIG_PPC_MERGE)
1148 of_unregister_platform_driver(&mpc52xx_uart_of_driver);
1150 platform_driver_unregister(&mpc52xx_uart_platform_driver);
1152 uart_unregister_driver(&mpc52xx_uart_driver);
1156 module_init(mpc52xx_uart_init);
1157 module_exit(mpc52xx_uart_exit);
1159 MODULE_AUTHOR("Sylvain Munaut <tnt@246tNt.com>");
1160 MODULE_DESCRIPTION("Freescale MPC52xx PSC UART");
1161 MODULE_LICENSE("GPL");