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 <linux/delay.h>
74 #if defined(CONFIG_PPC_MERGE)
76 #include <linux/of_platform.h>
78 #include <linux/platform_device.h>
81 #include <asm/mpc52xx.h>
82 #include <asm/mpc52xx_psc.h>
84 #if defined(CONFIG_SERIAL_MPC52xx_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
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
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);
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) \
126 ((port)->cons && (port)->cons->index == (port)->line)
128 #define uart_console(port) (0)
131 #if defined(CONFIG_PPC_MERGE)
132 static struct of_device_id mpc52xx_uart_of_match[] = {
133 { .type = "serial", .compatible = "fsl,mpc5200-psc-uart", },
134 { .type = "serial", .compatible = "mpc5200-psc-uart", }, /* lite5200 */
135 { .type = "serial", .compatible = "mpc5200-serial", }, /* efika */
141 /* ======================================================================== */
142 /* UART operations */
143 /* ======================================================================== */
146 mpc52xx_uart_tx_empty(struct uart_port *port)
148 int status = in_be16(&PSC(port)->mpc52xx_psc_status);
149 return (status & MPC52xx_PSC_SR_TXEMP) ? TIOCSER_TEMT : 0;
153 mpc52xx_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
155 /* Not implemented */
159 mpc52xx_uart_get_mctrl(struct uart_port *port)
161 /* Not implemented */
162 return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
166 mpc52xx_uart_stop_tx(struct uart_port *port)
168 /* port->lock taken by caller */
169 port->read_status_mask &= ~MPC52xx_PSC_IMR_TXRDY;
170 out_be16(&PSC(port)->mpc52xx_psc_imr, port->read_status_mask);
174 mpc52xx_uart_start_tx(struct uart_port *port)
176 /* port->lock taken by caller */
177 port->read_status_mask |= MPC52xx_PSC_IMR_TXRDY;
178 out_be16(&PSC(port)->mpc52xx_psc_imr, port->read_status_mask);
182 mpc52xx_uart_send_xchar(struct uart_port *port, char ch)
185 spin_lock_irqsave(&port->lock, flags);
189 /* Make sure tx interrupts are on */
190 /* Truly necessary ??? They should be anyway */
191 port->read_status_mask |= MPC52xx_PSC_IMR_TXRDY;
192 out_be16(&PSC(port)->mpc52xx_psc_imr, port->read_status_mask);
195 spin_unlock_irqrestore(&port->lock, flags);
199 mpc52xx_uart_stop_rx(struct uart_port *port)
201 /* port->lock taken by caller */
202 port->read_status_mask &= ~MPC52xx_PSC_IMR_RXRDY;
203 out_be16(&PSC(port)->mpc52xx_psc_imr, port->read_status_mask);
207 mpc52xx_uart_enable_ms(struct uart_port *port)
209 /* Not implemented */
213 mpc52xx_uart_break_ctl(struct uart_port *port, int ctl)
216 spin_lock_irqsave(&port->lock, flags);
219 out_8(&PSC(port)->command, MPC52xx_PSC_START_BRK);
221 out_8(&PSC(port)->command, MPC52xx_PSC_STOP_BRK);
223 spin_unlock_irqrestore(&port->lock, flags);
227 mpc52xx_uart_startup(struct uart_port *port)
229 struct mpc52xx_psc __iomem *psc = PSC(port);
230 struct mpc52xx_psc_fifo __iomem *fifo = FIFO(port);
234 ret = request_irq(port->irq, mpc52xx_uart_int,
235 IRQF_DISABLED | IRQF_SAMPLE_RANDOM, "mpc52xx_psc_uart", port);
239 /* Reset/activate the port, clear and enable interrupts */
240 out_8(&psc->command, MPC52xx_PSC_RST_RX);
241 out_8(&psc->command, MPC52xx_PSC_RST_TX);
243 out_be32(&psc->sicr, 0); /* UART mode DCD ignored */
245 out_be16(&psc->mpc52xx_psc_clock_select, 0xdd00); /* /16 prescaler on */
247 out_8(&fifo->rfcntl, 0x00);
248 out_be16(&fifo->rfalarm, 0x1ff);
249 out_8(&fifo->tfcntl, 0x07);
250 out_be16(&fifo->tfalarm, 0x80);
252 port->read_status_mask |= MPC52xx_PSC_IMR_RXRDY | MPC52xx_PSC_IMR_TXRDY;
253 out_be16(&psc->mpc52xx_psc_imr, port->read_status_mask);
255 out_8(&psc->command, MPC52xx_PSC_TX_ENABLE);
256 out_8(&psc->command, MPC52xx_PSC_RX_ENABLE);
262 mpc52xx_uart_shutdown(struct uart_port *port)
264 struct mpc52xx_psc __iomem *psc = PSC(port);
266 /* Shut down the port. Leave TX active if on a console port */
267 out_8(&psc->command, MPC52xx_PSC_RST_RX);
268 if (!uart_console(port))
269 out_8(&psc->command, MPC52xx_PSC_RST_TX);
271 port->read_status_mask = 0;
272 out_be16(&psc->mpc52xx_psc_imr, port->read_status_mask);
274 /* Release interrupt */
275 free_irq(port->irq, port);
279 mpc52xx_uart_set_termios(struct uart_port *port, struct ktermios *new,
280 struct ktermios *old)
282 struct mpc52xx_psc __iomem *psc = PSC(port);
284 unsigned char mr1, mr2;
286 unsigned int j, baud, quot;
288 /* Prepare what we're gonna write */
291 switch (new->c_cflag & CSIZE) {
292 case CS5: mr1 |= MPC52xx_PSC_MODE_5_BITS;
294 case CS6: mr1 |= MPC52xx_PSC_MODE_6_BITS;
296 case CS7: mr1 |= MPC52xx_PSC_MODE_7_BITS;
299 default: mr1 |= MPC52xx_PSC_MODE_8_BITS;
302 if (new->c_cflag & PARENB) {
303 mr1 |= (new->c_cflag & PARODD) ?
304 MPC52xx_PSC_MODE_PARODD : MPC52xx_PSC_MODE_PAREVEN;
306 mr1 |= MPC52xx_PSC_MODE_PARNONE;
311 if (new->c_cflag & CSTOPB)
312 mr2 |= MPC52xx_PSC_MODE_TWO_STOP;
314 mr2 |= ((new->c_cflag & CSIZE) == CS5) ?
315 MPC52xx_PSC_MODE_ONE_STOP_5_BITS :
316 MPC52xx_PSC_MODE_ONE_STOP;
319 baud = uart_get_baud_rate(port, new, old, 0, port->uartclk/16);
320 quot = uart_get_divisor(port, baud);
324 spin_lock_irqsave(&port->lock, flags);
326 /* Update the per-port timeout */
327 uart_update_timeout(port, new->c_cflag, baud);
329 /* Do our best to flush TX & RX, so we don't loose anything */
330 /* But we don't wait indefinitly ! */
331 j = 5000000; /* Maximum wait */
332 /* FIXME Can't receive chars since set_termios might be called at early
333 * boot for the console, all stuff is not yet ready to receive at that
334 * time and that just makes the kernel oops */
335 /* while (j-- && mpc52xx_uart_int_rx_chars(port)); */
336 while (!(in_be16(&psc->mpc52xx_psc_status) & MPC52xx_PSC_SR_TXEMP) &&
341 printk(KERN_ERR "mpc52xx_uart.c: "
342 "Unable to flush RX & TX fifos in-time in set_termios."
343 "Some chars may have been lost.\n");
345 /* Reset the TX & RX */
346 out_8(&psc->command, MPC52xx_PSC_RST_RX);
347 out_8(&psc->command, MPC52xx_PSC_RST_TX);
349 /* Send new mode settings */
350 out_8(&psc->command, MPC52xx_PSC_SEL_MODE_REG_1);
351 out_8(&psc->mode, mr1);
352 out_8(&psc->mode, mr2);
353 out_8(&psc->ctur, ctr >> 8);
354 out_8(&psc->ctlr, ctr & 0xff);
356 /* Reenable TX & RX */
357 out_8(&psc->command, MPC52xx_PSC_TX_ENABLE);
358 out_8(&psc->command, MPC52xx_PSC_RX_ENABLE);
360 /* We're all set, release the lock */
361 spin_unlock_irqrestore(&port->lock, flags);
365 mpc52xx_uart_type(struct uart_port *port)
367 return port->type == PORT_MPC52xx ? "MPC52xx PSC" : NULL;
371 mpc52xx_uart_release_port(struct uart_port *port)
373 /* remapped by us ? */
374 if (port->flags & UPF_IOREMAP) {
375 iounmap(port->membase);
376 port->membase = NULL;
379 release_mem_region(port->mapbase, sizeof(struct mpc52xx_psc));
383 mpc52xx_uart_request_port(struct uart_port *port)
387 if (port->flags & UPF_IOREMAP) /* Need to remap ? */
388 port->membase = ioremap(port->mapbase,
389 sizeof(struct mpc52xx_psc));
394 err = request_mem_region(port->mapbase, sizeof(struct mpc52xx_psc),
395 "mpc52xx_psc_uart") != NULL ? 0 : -EBUSY;
397 if (err && (port->flags & UPF_IOREMAP)) {
398 iounmap(port->membase);
399 port->membase = NULL;
406 mpc52xx_uart_config_port(struct uart_port *port, int flags)
408 if ((flags & UART_CONFIG_TYPE)
409 && (mpc52xx_uart_request_port(port) == 0))
410 port->type = PORT_MPC52xx;
414 mpc52xx_uart_verify_port(struct uart_port *port, struct serial_struct *ser)
416 if (ser->type != PORT_UNKNOWN && ser->type != PORT_MPC52xx)
419 if ((ser->irq != port->irq) ||
420 (ser->io_type != SERIAL_IO_MEM) ||
421 (ser->baud_base != port->uartclk) ||
422 (ser->iomem_base != (void *)port->mapbase) ||
430 static struct uart_ops mpc52xx_uart_ops = {
431 .tx_empty = mpc52xx_uart_tx_empty,
432 .set_mctrl = mpc52xx_uart_set_mctrl,
433 .get_mctrl = mpc52xx_uart_get_mctrl,
434 .stop_tx = mpc52xx_uart_stop_tx,
435 .start_tx = mpc52xx_uart_start_tx,
436 .send_xchar = mpc52xx_uart_send_xchar,
437 .stop_rx = mpc52xx_uart_stop_rx,
438 .enable_ms = mpc52xx_uart_enable_ms,
439 .break_ctl = mpc52xx_uart_break_ctl,
440 .startup = mpc52xx_uart_startup,
441 .shutdown = mpc52xx_uart_shutdown,
442 .set_termios = mpc52xx_uart_set_termios,
443 /* .pm = mpc52xx_uart_pm, Not supported yet */
444 /* .set_wake = mpc52xx_uart_set_wake, Not supported yet */
445 .type = mpc52xx_uart_type,
446 .release_port = mpc52xx_uart_release_port,
447 .request_port = mpc52xx_uart_request_port,
448 .config_port = mpc52xx_uart_config_port,
449 .verify_port = mpc52xx_uart_verify_port
453 /* ======================================================================== */
454 /* Interrupt handling */
455 /* ======================================================================== */
458 mpc52xx_uart_int_rx_chars(struct uart_port *port)
460 struct tty_struct *tty = port->info->tty;
461 unsigned char ch, flag;
462 unsigned short status;
464 /* While we can read, do so ! */
465 while ((status = in_be16(&PSC(port)->mpc52xx_psc_status)) &
466 MPC52xx_PSC_SR_RXRDY) {
469 ch = in_8(&PSC(port)->mpc52xx_psc_buffer_8);
471 /* Handle sysreq char */
473 if (uart_handle_sysrq_char(port, ch)) {
484 if (status & (MPC52xx_PSC_SR_PE |
486 MPC52xx_PSC_SR_RB)) {
488 if (status & MPC52xx_PSC_SR_RB) {
490 uart_handle_break(port);
491 } else if (status & MPC52xx_PSC_SR_PE)
493 else if (status & MPC52xx_PSC_SR_FE)
496 /* Clear error condition */
497 out_8(&PSC(port)->command, MPC52xx_PSC_RST_ERR_STAT);
500 tty_insert_flip_char(tty, ch, flag);
501 if (status & MPC52xx_PSC_SR_OE) {
503 * Overrun is special, since it's
504 * reported immediately, and doesn't
505 * affect the current character
507 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
511 tty_flip_buffer_push(tty);
513 return in_be16(&PSC(port)->mpc52xx_psc_status) & MPC52xx_PSC_SR_RXRDY;
517 mpc52xx_uart_int_tx_chars(struct uart_port *port)
519 struct circ_buf *xmit = &port->info->xmit;
521 /* Process out of band chars */
523 out_8(&PSC(port)->mpc52xx_psc_buffer_8, port->x_char);
529 /* Nothing to do ? */
530 if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
531 mpc52xx_uart_stop_tx(port);
536 while (in_be16(&PSC(port)->mpc52xx_psc_status) & MPC52xx_PSC_SR_TXRDY) {
537 out_8(&PSC(port)->mpc52xx_psc_buffer_8, xmit->buf[xmit->tail]);
538 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
540 if (uart_circ_empty(xmit))
545 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
546 uart_write_wakeup(port);
548 /* Maybe we're done after all */
549 if (uart_circ_empty(xmit)) {
550 mpc52xx_uart_stop_tx(port);
558 mpc52xx_uart_int(int irq, void *dev_id)
560 struct uart_port *port = dev_id;
561 unsigned long pass = ISR_PASS_LIMIT;
562 unsigned int keepgoing;
563 unsigned short status;
565 spin_lock(&port->lock);
567 /* While we have stuff to do, we continue */
569 /* If we don't find anything to do, we stop */
573 status = in_be16(&PSC(port)->mpc52xx_psc_isr);
574 status &= port->read_status_mask;
576 /* Do we need to receive chars ? */
577 /* For this RX interrupts must be on and some chars waiting */
578 if (status & MPC52xx_PSC_IMR_RXRDY)
579 keepgoing |= mpc52xx_uart_int_rx_chars(port);
581 /* Do we need to send chars ? */
582 /* For this, TX must be ready and TX interrupt enabled */
583 if (status & MPC52xx_PSC_IMR_TXRDY)
584 keepgoing |= mpc52xx_uart_int_tx_chars(port);
586 /* Limit number of iteration */
592 spin_unlock(&port->lock);
598 /* ======================================================================== */
599 /* Console ( if applicable ) */
600 /* ======================================================================== */
602 #ifdef CONFIG_SERIAL_MPC52xx_CONSOLE
605 mpc52xx_console_get_options(struct uart_port *port,
606 int *baud, int *parity, int *bits, int *flow)
608 struct mpc52xx_psc __iomem *psc = PSC(port);
611 pr_debug("mpc52xx_console_get_options(port=%p)\n", port);
613 /* Read the mode registers */
614 out_8(&psc->command, MPC52xx_PSC_SEL_MODE_REG_1);
615 mr1 = in_8(&psc->mode);
617 /* CT{U,L}R are write-only ! */
618 *baud = CONFIG_SERIAL_MPC52xx_CONSOLE_BAUD;
619 #if !defined(CONFIG_PPC_MERGE)
620 if (__res.bi_baudrate)
621 *baud = __res.bi_baudrate;
625 switch (mr1 & MPC52xx_PSC_MODE_BITS_MASK) {
626 case MPC52xx_PSC_MODE_5_BITS:
629 case MPC52xx_PSC_MODE_6_BITS:
632 case MPC52xx_PSC_MODE_7_BITS:
635 case MPC52xx_PSC_MODE_8_BITS:
640 if (mr1 & MPC52xx_PSC_MODE_PARNONE)
643 *parity = mr1 & MPC52xx_PSC_MODE_PARODD ? 'o' : 'e';
647 mpc52xx_console_write(struct console *co, const char *s, unsigned int count)
649 struct uart_port *port = &mpc52xx_uart_ports[co->index];
650 struct mpc52xx_psc __iomem *psc = PSC(port);
653 /* Disable interrupts */
654 out_be16(&psc->mpc52xx_psc_imr, 0);
656 /* Wait the TX buffer to be empty */
657 j = 5000000; /* Maximum wait */
658 while (!(in_be16(&psc->mpc52xx_psc_status) & MPC52xx_PSC_SR_TXEMP) &&
662 /* Write all the chars */
663 for (i = 0; i < count; i++, s++) {
664 /* Line return handling */
666 out_8(&psc->mpc52xx_psc_buffer_8, '\r');
669 out_8(&psc->mpc52xx_psc_buffer_8, *s);
671 /* Wait the TX buffer to be empty */
672 j = 20000; /* Maximum wait */
673 while (!(in_be16(&psc->mpc52xx_psc_status) &
674 MPC52xx_PSC_SR_TXEMP) && --j)
678 /* Restore interrupt state */
679 out_be16(&psc->mpc52xx_psc_imr, port->read_status_mask);
682 #if !defined(CONFIG_PPC_MERGE)
684 mpc52xx_console_setup(struct console *co, char *options)
686 struct uart_port *port = &mpc52xx_uart_ports[co->index];
688 int baud = CONFIG_SERIAL_MPC52xx_CONSOLE_BAUD;
693 if (co->index < 0 || co->index >= MPC52xx_PSC_MAXNUM)
696 /* Basic port init. Needed since we use some uart_??? func before
697 * real init for early access */
698 spin_lock_init(&port->lock);
699 port->uartclk = __res.bi_ipbfreq / 2; /* Look at CTLR doc */
700 port->ops = &mpc52xx_uart_ops;
701 port->mapbase = MPC52xx_PA(MPC52xx_PSCx_OFFSET(co->index+1));
703 /* We ioremap ourself */
704 port->membase = ioremap(port->mapbase, MPC52xx_PSC_SIZE);
705 if (port->membase == NULL)
708 /* Setup the port parameters accoding to options */
710 uart_parse_options(options, &baud, &parity, &bits, &flow);
712 mpc52xx_console_get_options(port, &baud, &parity, &bits, &flow);
714 return uart_set_options(port, co, baud, parity, bits, flow);
720 mpc52xx_console_setup(struct console *co, char *options)
722 struct uart_port *port = &mpc52xx_uart_ports[co->index];
723 struct device_node *np = mpc52xx_uart_nodes[co->index];
724 unsigned int ipb_freq;
728 int baud = CONFIG_SERIAL_MPC52xx_CONSOLE_BAUD;
733 pr_debug("mpc52xx_console_setup co=%p, co->index=%i, options=%s\n",
734 co, co->index, options);
736 if ((co->index < 0) || (co->index > MPC52xx_PSC_MAXNUM)) {
737 pr_debug("PSC%x out of range\n", co->index);
742 pr_debug("PSC%x not found in device tree\n", co->index);
746 pr_debug("Console on ttyPSC%x is %s\n",
747 co->index, mpc52xx_uart_nodes[co->index]->full_name);
749 /* Fetch register locations */
750 ret = of_address_to_resource(np, 0, &res);
752 pr_debug("Could not get resources for PSC%x\n", co->index);
756 /* Search for bus-frequency property in this node or a parent */
757 ipb_freq = mpc52xx_find_ipb_freq(np);
759 pr_debug("Could not find IPB bus frequency!\n");
763 /* Basic port init. Needed since we use some uart_??? func before
764 * real init for early access */
765 spin_lock_init(&port->lock);
766 port->uartclk = ipb_freq / 2;
767 port->ops = &mpc52xx_uart_ops;
768 port->mapbase = res.start;
769 port->membase = ioremap(res.start, sizeof(struct mpc52xx_psc));
770 port->irq = irq_of_parse_and_map(np, 0);
772 if (port->membase == NULL)
775 pr_debug("mpc52xx-psc uart at %p, mapped to %p, irq=%x, freq=%i\n",
776 (void *)port->mapbase, port->membase,
777 port->irq, port->uartclk);
779 /* Setup the port parameters accoding to options */
781 uart_parse_options(options, &baud, &parity, &bits, &flow);
783 mpc52xx_console_get_options(port, &baud, &parity, &bits, &flow);
785 pr_debug("Setting console parameters: %i %i%c1 flow=%c\n",
786 baud, bits, parity, flow);
788 return uart_set_options(port, co, baud, parity, bits, flow);
790 #endif /* defined(CONFIG_PPC_MERGE) */
793 static struct uart_driver mpc52xx_uart_driver;
795 static struct console mpc52xx_console = {
797 .write = mpc52xx_console_write,
798 .device = uart_console_device,
799 .setup = mpc52xx_console_setup,
800 .flags = CON_PRINTBUFFER,
801 .index = -1, /* Specified on the cmdline (e.g. console=ttyPSC0) */
802 .data = &mpc52xx_uart_driver,
807 mpc52xx_console_init(void)
809 #if defined(CONFIG_PPC_MERGE)
810 mpc52xx_uart_of_enumerate();
812 register_console(&mpc52xx_console);
816 console_initcall(mpc52xx_console_init);
818 #define MPC52xx_PSC_CONSOLE &mpc52xx_console
820 #define MPC52xx_PSC_CONSOLE NULL
824 /* ======================================================================== */
826 /* ======================================================================== */
828 static struct uart_driver mpc52xx_uart_driver = {
829 .driver_name = "mpc52xx_psc_uart",
830 .dev_name = "ttyPSC",
831 .major = SERIAL_PSC_MAJOR,
832 .minor = SERIAL_PSC_MINOR,
833 .nr = MPC52xx_PSC_MAXNUM,
834 .cons = MPC52xx_PSC_CONSOLE,
838 #if !defined(CONFIG_PPC_MERGE)
839 /* ======================================================================== */
840 /* Platform Driver */
841 /* ======================================================================== */
844 mpc52xx_uart_probe(struct platform_device *dev)
846 struct resource *res = dev->resource;
848 struct uart_port *port = NULL;
851 /* Check validity & presence */
853 if (idx < 0 || idx >= MPC52xx_PSC_MAXNUM)
856 if (!mpc52xx_match_psc_function(idx, "uart"))
859 /* Init the port structure */
860 port = &mpc52xx_uart_ports[idx];
862 spin_lock_init(&port->lock);
863 port->uartclk = __res.bi_ipbfreq / 2; /* Look at CTLR doc */
864 port->fifosize = 512;
865 port->iotype = UPIO_MEM;
866 port->flags = UPF_BOOT_AUTOCONF |
867 (uart_console(port) ? 0 : UPF_IOREMAP);
869 port->ops = &mpc52xx_uart_ops;
870 port->dev = &dev->dev;
872 /* Search for IRQ and mapbase */
873 for (i = 0 ; i < dev->num_resources ; i++, res++) {
874 if (res->flags & IORESOURCE_MEM)
875 port->mapbase = res->start;
876 else if (res->flags & IORESOURCE_IRQ)
877 port->irq = res->start;
879 if (!port->irq || !port->mapbase)
882 /* Add the port to the uart sub-system */
883 ret = uart_add_one_port(&mpc52xx_uart_driver, port);
885 platform_set_drvdata(dev, (void *)port);
891 mpc52xx_uart_remove(struct platform_device *dev)
893 struct uart_port *port = (struct uart_port *) platform_get_drvdata(dev);
895 platform_set_drvdata(dev, NULL);
898 uart_remove_one_port(&mpc52xx_uart_driver, port);
905 mpc52xx_uart_suspend(struct platform_device *dev, pm_message_t state)
907 struct uart_port *port = (struct uart_port *) platform_get_drvdata(dev);
910 uart_suspend_port(&mpc52xx_uart_driver, port);
916 mpc52xx_uart_resume(struct platform_device *dev)
918 struct uart_port *port = (struct uart_port *) platform_get_drvdata(dev);
921 uart_resume_port(&mpc52xx_uart_driver, port);
928 static struct platform_driver mpc52xx_uart_platform_driver = {
929 .probe = mpc52xx_uart_probe,
930 .remove = mpc52xx_uart_remove,
932 .suspend = mpc52xx_uart_suspend,
933 .resume = mpc52xx_uart_resume,
936 .owner = THIS_MODULE,
937 .name = "mpc52xx-psc",
940 #endif /* !defined(CONFIG_PPC_MERGE) */
943 #if defined(CONFIG_PPC_MERGE)
944 /* ======================================================================== */
945 /* OF Platform Driver */
946 /* ======================================================================== */
949 mpc52xx_uart_of_probe(struct of_device *op, const struct of_device_id *match)
952 unsigned int ipb_freq;
953 struct uart_port *port = NULL;
957 dev_dbg(&op->dev, "mpc52xx_uart_probe(op=%p, match=%p)\n", op, match);
959 /* Check validity & presence */
960 for (idx = 0; idx < MPC52xx_PSC_MAXNUM; idx++)
961 if (mpc52xx_uart_nodes[idx] == op->node)
963 if (idx >= MPC52xx_PSC_MAXNUM)
965 pr_debug("Found %s assigned to ttyPSC%x\n",
966 mpc52xx_uart_nodes[idx]->full_name, idx);
968 /* Search for bus-frequency property in this node or a parent */
969 ipb_freq = mpc52xx_find_ipb_freq(op->node);
971 dev_dbg(&op->dev, "Could not find IPB bus frequency!\n");
975 /* Init the port structure */
976 port = &mpc52xx_uart_ports[idx];
978 spin_lock_init(&port->lock);
979 port->uartclk = ipb_freq / 2;
980 port->fifosize = 512;
981 port->iotype = UPIO_MEM;
982 port->flags = UPF_BOOT_AUTOCONF |
983 (uart_console(port) ? 0 : UPF_IOREMAP);
985 port->ops = &mpc52xx_uart_ops;
986 port->dev = &op->dev;
988 /* Search for IRQ and mapbase */
989 ret = of_address_to_resource(op->node, 0, &res);
993 port->mapbase = res.start;
994 port->irq = irq_of_parse_and_map(op->node, 0);
996 dev_dbg(&op->dev, "mpc52xx-psc uart at %p, irq=%x, freq=%i\n",
997 (void *)port->mapbase, port->irq, port->uartclk);
999 if ((port->irq == NO_IRQ) || !port->mapbase) {
1000 printk(KERN_ERR "Could not allocate resources for PSC\n");
1004 /* Add the port to the uart sub-system */
1005 ret = uart_add_one_port(&mpc52xx_uart_driver, port);
1007 dev_set_drvdata(&op->dev, (void *)port);
1013 mpc52xx_uart_of_remove(struct of_device *op)
1015 struct uart_port *port = dev_get_drvdata(&op->dev);
1016 dev_set_drvdata(&op->dev, NULL);
1019 uart_remove_one_port(&mpc52xx_uart_driver, port);
1020 irq_dispose_mapping(port->irq);
1028 mpc52xx_uart_of_suspend(struct of_device *op, pm_message_t state)
1030 struct uart_port *port = (struct uart_port *) dev_get_drvdata(&op->dev);
1033 uart_suspend_port(&mpc52xx_uart_driver, port);
1039 mpc52xx_uart_of_resume(struct of_device *op)
1041 struct uart_port *port = (struct uart_port *) dev_get_drvdata(&op->dev);
1044 uart_resume_port(&mpc52xx_uart_driver, port);
1051 mpc52xx_uart_of_assign(struct device_node *np, int idx)
1056 /* Find the first free node */
1057 for (i = 0; i < MPC52xx_PSC_MAXNUM; i++) {
1058 if (mpc52xx_uart_nodes[i] == NULL) {
1064 if ((idx < 0) || (idx >= MPC52xx_PSC_MAXNUM))
1068 return; /* No free slot; abort */
1071 /* If the slot is already occupied, then swap slots */
1072 if (mpc52xx_uart_nodes[idx] && (free_idx != -1))
1073 mpc52xx_uart_nodes[free_idx] = mpc52xx_uart_nodes[idx];
1074 mpc52xx_uart_nodes[idx] = np;
1078 mpc52xx_uart_of_enumerate(void)
1080 static int enum_done;
1081 struct device_node *np;
1082 const unsigned int *devno;
1088 for_each_node_by_type(np, "serial") {
1089 if (!of_match_node(mpc52xx_uart_of_match, np))
1092 /* Is a particular device number requested? */
1093 devno = of_get_property(np, "port-number", NULL);
1094 mpc52xx_uart_of_assign(np, devno ? *devno : -1);
1099 for (i = 0; i < MPC52xx_PSC_MAXNUM; i++) {
1100 if (mpc52xx_uart_nodes[i])
1101 pr_debug("%s assigned to ttyPSC%x\n",
1102 mpc52xx_uart_nodes[i]->full_name, i);
1106 MODULE_DEVICE_TABLE(of, mpc52xx_uart_of_match);
1108 static struct of_platform_driver mpc52xx_uart_of_driver = {
1109 .match_table = mpc52xx_uart_of_match,
1110 .probe = mpc52xx_uart_of_probe,
1111 .remove = mpc52xx_uart_of_remove,
1113 .suspend = mpc52xx_uart_of_suspend,
1114 .resume = mpc52xx_uart_of_resume,
1117 .name = "mpc52xx-psc-uart",
1120 #endif /* defined(CONFIG_PPC_MERGE) */
1123 /* ======================================================================== */
1125 /* ======================================================================== */
1128 mpc52xx_uart_init(void)
1132 printk(KERN_INFO "Serial: MPC52xx PSC UART driver\n");
1134 ret = uart_register_driver(&mpc52xx_uart_driver);
1136 printk(KERN_ERR "%s: uart_register_driver failed (%i)\n",
1141 #if defined(CONFIG_PPC_MERGE)
1142 mpc52xx_uart_of_enumerate();
1144 ret = of_register_platform_driver(&mpc52xx_uart_of_driver);
1146 printk(KERN_ERR "%s: of_register_platform_driver failed (%i)\n",
1148 uart_unregister_driver(&mpc52xx_uart_driver);
1152 ret = platform_driver_register(&mpc52xx_uart_platform_driver);
1154 printk(KERN_ERR "%s: platform_driver_register failed (%i)\n",
1156 uart_unregister_driver(&mpc52xx_uart_driver);
1165 mpc52xx_uart_exit(void)
1167 #if defined(CONFIG_PPC_MERGE)
1168 of_unregister_platform_driver(&mpc52xx_uart_of_driver);
1170 platform_driver_unregister(&mpc52xx_uart_platform_driver);
1172 uart_unregister_driver(&mpc52xx_uart_driver);
1176 module_init(mpc52xx_uart_init);
1177 module_exit(mpc52xx_uart_exit);
1179 MODULE_AUTHOR("Sylvain Munaut <tnt@246tNt.com>");
1180 MODULE_DESCRIPTION("Freescale MPC52xx PSC UART");
1181 MODULE_LICENSE("GPL");