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>
70 #include <linux/delay.h>
73 #if defined(CONFIG_PPC_MERGE)
75 #include <linux/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);
114 #define PSC(port) ((struct mpc52xx_psc __iomem *)((port)->membase))
117 /* Forward declaration of the interruption handling routine */
118 static irqreturn_t mpc52xx_uart_int(int irq, void *dev_id);
121 /* Simple macro to test if a port is console or not. This one is taken
122 * for serial_core.c and maybe should be moved to serial_core.h ? */
123 #ifdef CONFIG_SERIAL_CORE_CONSOLE
124 #define uart_console(port) \
125 ((port)->cons && (port)->cons->index == (port)->line)
127 #define uart_console(port) (0)
130 #if defined(CONFIG_PPC_MERGE)
131 static struct of_device_id mpc52xx_uart_of_match[] = {
132 { .type = "serial", .compatible = "fsl,mpc5200-psc-uart", },
133 { .type = "serial", .compatible = "mpc5200-psc-uart", }, /* lite5200 */
134 { .type = "serial", .compatible = "mpc5200-serial", }, /* efika */
139 /* ======================================================================== */
140 /* PSC fifo operations for isolating differences between 52xx and 512x */
141 /* ======================================================================== */
144 void (*fifo_init)(struct uart_port *port);
145 int (*raw_rx_rdy)(struct uart_port *port);
146 int (*raw_tx_rdy)(struct uart_port *port);
147 int (*rx_rdy)(struct uart_port *port);
148 int (*tx_rdy)(struct uart_port *port);
149 int (*tx_empty)(struct uart_port *port);
150 void (*stop_rx)(struct uart_port *port);
151 void (*start_tx)(struct uart_port *port);
152 void (*stop_tx)(struct uart_port *port);
153 void (*rx_clr_irq)(struct uart_port *port);
154 void (*tx_clr_irq)(struct uart_port *port);
155 void (*write_char)(struct uart_port *port, unsigned char c);
156 unsigned char (*read_char)(struct uart_port *port);
157 void (*cw_disable_ints)(struct uart_port *port);
158 void (*cw_restore_ints)(struct uart_port *port);
159 unsigned long (*getuartclk)(void *p);
162 #define FIFO_52xx(port) ((struct mpc52xx_psc_fifo __iomem *)(PSC(port)+1))
163 static void mpc52xx_psc_fifo_init(struct uart_port *port)
165 struct mpc52xx_psc __iomem *psc = PSC(port);
166 struct mpc52xx_psc_fifo __iomem *fifo = FIFO_52xx(port);
169 out_be16(&psc->mpc52xx_psc_clock_select, 0xdd00);
171 out_8(&fifo->rfcntl, 0x00);
172 out_be16(&fifo->rfalarm, 0x1ff);
173 out_8(&fifo->tfcntl, 0x07);
174 out_be16(&fifo->tfalarm, 0x80);
176 port->read_status_mask |= MPC52xx_PSC_IMR_RXRDY | MPC52xx_PSC_IMR_TXRDY;
177 out_be16(&psc->mpc52xx_psc_imr, port->read_status_mask);
180 static int mpc52xx_psc_raw_rx_rdy(struct uart_port *port)
182 return in_be16(&PSC(port)->mpc52xx_psc_status)
183 & MPC52xx_PSC_SR_RXRDY;
186 static int mpc52xx_psc_raw_tx_rdy(struct uart_port *port)
188 return in_be16(&PSC(port)->mpc52xx_psc_status)
189 & MPC52xx_PSC_SR_TXRDY;
193 static int mpc52xx_psc_rx_rdy(struct uart_port *port)
195 return in_be16(&PSC(port)->mpc52xx_psc_isr)
196 & port->read_status_mask
197 & MPC52xx_PSC_IMR_RXRDY;
200 static int mpc52xx_psc_tx_rdy(struct uart_port *port)
202 return in_be16(&PSC(port)->mpc52xx_psc_isr)
203 & port->read_status_mask
204 & MPC52xx_PSC_IMR_TXRDY;
207 static int mpc52xx_psc_tx_empty(struct uart_port *port)
209 return in_be16(&PSC(port)->mpc52xx_psc_status)
210 & MPC52xx_PSC_SR_TXEMP;
213 static void mpc52xx_psc_start_tx(struct uart_port *port)
215 port->read_status_mask |= MPC52xx_PSC_IMR_TXRDY;
216 out_be16(&PSC(port)->mpc52xx_psc_imr, port->read_status_mask);
219 static void mpc52xx_psc_stop_tx(struct uart_port *port)
221 port->read_status_mask &= ~MPC52xx_PSC_IMR_TXRDY;
222 out_be16(&PSC(port)->mpc52xx_psc_imr, port->read_status_mask);
225 static void mpc52xx_psc_stop_rx(struct uart_port *port)
227 port->read_status_mask &= ~MPC52xx_PSC_IMR_RXRDY;
228 out_be16(&PSC(port)->mpc52xx_psc_imr, port->read_status_mask);
231 static void mpc52xx_psc_rx_clr_irq(struct uart_port *port)
235 static void mpc52xx_psc_tx_clr_irq(struct uart_port *port)
239 static void mpc52xx_psc_write_char(struct uart_port *port, unsigned char c)
241 out_8(&PSC(port)->mpc52xx_psc_buffer_8, c);
244 static unsigned char mpc52xx_psc_read_char(struct uart_port *port)
246 return in_8(&PSC(port)->mpc52xx_psc_buffer_8);
249 static void mpc52xx_psc_cw_disable_ints(struct uart_port *port)
251 out_be16(&PSC(port)->mpc52xx_psc_imr, 0);
254 static void mpc52xx_psc_cw_restore_ints(struct uart_port *port)
256 out_be16(&PSC(port)->mpc52xx_psc_imr, port->read_status_mask);
259 /* Search for bus-frequency property in this node or a parent */
260 static unsigned long mpc52xx_getuartclk(void *p)
262 #if defined(CONFIG_PPC_MERGE)
264 * 5200 UARTs have a / 32 prescaler
265 * but the generic serial code assumes 16
266 * so return ipb freq / 2
268 return mpc52xx_find_ipb_freq(p) / 2;
270 pr_debug("unexpected call to mpc52xx_getuartclk with arch/ppc\n");
275 static struct psc_ops mpc52xx_psc_ops = {
276 .fifo_init = mpc52xx_psc_fifo_init,
277 .raw_rx_rdy = mpc52xx_psc_raw_rx_rdy,
278 .raw_tx_rdy = mpc52xx_psc_raw_tx_rdy,
279 .rx_rdy = mpc52xx_psc_rx_rdy,
280 .tx_rdy = mpc52xx_psc_tx_rdy,
281 .tx_empty = mpc52xx_psc_tx_empty,
282 .stop_rx = mpc52xx_psc_stop_rx,
283 .start_tx = mpc52xx_psc_start_tx,
284 .stop_tx = mpc52xx_psc_stop_tx,
285 .rx_clr_irq = mpc52xx_psc_rx_clr_irq,
286 .tx_clr_irq = mpc52xx_psc_tx_clr_irq,
287 .write_char = mpc52xx_psc_write_char,
288 .read_char = mpc52xx_psc_read_char,
289 .cw_disable_ints = mpc52xx_psc_cw_disable_ints,
290 .cw_restore_ints = mpc52xx_psc_cw_restore_ints,
291 .getuartclk = mpc52xx_getuartclk,
294 static struct psc_ops *psc_ops = &mpc52xx_psc_ops;
296 /* ======================================================================== */
297 /* UART operations */
298 /* ======================================================================== */
301 mpc52xx_uart_tx_empty(struct uart_port *port)
303 return psc_ops->tx_empty(port) ? TIOCSER_TEMT : 0;
307 mpc52xx_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
309 /* Not implemented */
313 mpc52xx_uart_get_mctrl(struct uart_port *port)
315 /* Not implemented */
316 return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
320 mpc52xx_uart_stop_tx(struct uart_port *port)
322 /* port->lock taken by caller */
323 psc_ops->stop_tx(port);
327 mpc52xx_uart_start_tx(struct uart_port *port)
329 /* port->lock taken by caller */
330 psc_ops->start_tx(port);
334 mpc52xx_uart_send_xchar(struct uart_port *port, char ch)
337 spin_lock_irqsave(&port->lock, flags);
341 /* Make sure tx interrupts are on */
342 /* Truly necessary ??? They should be anyway */
343 psc_ops->start_tx(port);
346 spin_unlock_irqrestore(&port->lock, flags);
350 mpc52xx_uart_stop_rx(struct uart_port *port)
352 /* port->lock taken by caller */
353 psc_ops->stop_rx(port);
357 mpc52xx_uart_enable_ms(struct uart_port *port)
359 /* Not implemented */
363 mpc52xx_uart_break_ctl(struct uart_port *port, int ctl)
366 spin_lock_irqsave(&port->lock, flags);
369 out_8(&PSC(port)->command, MPC52xx_PSC_START_BRK);
371 out_8(&PSC(port)->command, MPC52xx_PSC_STOP_BRK);
373 spin_unlock_irqrestore(&port->lock, flags);
377 mpc52xx_uart_startup(struct uart_port *port)
379 struct mpc52xx_psc __iomem *psc = PSC(port);
383 ret = request_irq(port->irq, mpc52xx_uart_int,
384 IRQF_DISABLED | IRQF_SAMPLE_RANDOM, "mpc52xx_psc_uart", port);
388 /* Reset/activate the port, clear and enable interrupts */
389 out_8(&psc->command, MPC52xx_PSC_RST_RX);
390 out_8(&psc->command, MPC52xx_PSC_RST_TX);
392 out_be32(&psc->sicr, 0); /* UART mode DCD ignored */
394 psc_ops->fifo_init(port);
396 out_8(&psc->command, MPC52xx_PSC_TX_ENABLE);
397 out_8(&psc->command, MPC52xx_PSC_RX_ENABLE);
403 mpc52xx_uart_shutdown(struct uart_port *port)
405 struct mpc52xx_psc __iomem *psc = PSC(port);
407 /* Shut down the port. Leave TX active if on a console port */
408 out_8(&psc->command, MPC52xx_PSC_RST_RX);
409 if (!uart_console(port))
410 out_8(&psc->command, MPC52xx_PSC_RST_TX);
412 port->read_status_mask = 0;
413 out_be16(&psc->mpc52xx_psc_imr, port->read_status_mask);
415 /* Release interrupt */
416 free_irq(port->irq, port);
420 mpc52xx_uart_set_termios(struct uart_port *port, struct ktermios *new,
421 struct ktermios *old)
423 struct mpc52xx_psc __iomem *psc = PSC(port);
425 unsigned char mr1, mr2;
427 unsigned int j, baud, quot;
429 /* Prepare what we're gonna write */
432 switch (new->c_cflag & CSIZE) {
433 case CS5: mr1 |= MPC52xx_PSC_MODE_5_BITS;
435 case CS6: mr1 |= MPC52xx_PSC_MODE_6_BITS;
437 case CS7: mr1 |= MPC52xx_PSC_MODE_7_BITS;
440 default: mr1 |= MPC52xx_PSC_MODE_8_BITS;
443 if (new->c_cflag & PARENB) {
444 mr1 |= (new->c_cflag & PARODD) ?
445 MPC52xx_PSC_MODE_PARODD : MPC52xx_PSC_MODE_PAREVEN;
447 mr1 |= MPC52xx_PSC_MODE_PARNONE;
452 if (new->c_cflag & CSTOPB)
453 mr2 |= MPC52xx_PSC_MODE_TWO_STOP;
455 mr2 |= ((new->c_cflag & CSIZE) == CS5) ?
456 MPC52xx_PSC_MODE_ONE_STOP_5_BITS :
457 MPC52xx_PSC_MODE_ONE_STOP;
460 baud = uart_get_baud_rate(port, new, old, 0, port->uartclk/16);
461 quot = uart_get_divisor(port, baud);
465 spin_lock_irqsave(&port->lock, flags);
467 /* Update the per-port timeout */
468 uart_update_timeout(port, new->c_cflag, baud);
470 /* Do our best to flush TX & RX, so we don't loose anything */
471 /* But we don't wait indefinitly ! */
472 j = 5000000; /* Maximum wait */
473 /* FIXME Can't receive chars since set_termios might be called at early
474 * boot for the console, all stuff is not yet ready to receive at that
475 * time and that just makes the kernel oops */
476 /* while (j-- && mpc52xx_uart_int_rx_chars(port)); */
477 while (!mpc52xx_uart_tx_empty(port) && --j)
481 printk(KERN_ERR "mpc52xx_uart.c: "
482 "Unable to flush RX & TX fifos in-time in set_termios."
483 "Some chars may have been lost.\n");
485 /* Reset the TX & RX */
486 out_8(&psc->command, MPC52xx_PSC_RST_RX);
487 out_8(&psc->command, MPC52xx_PSC_RST_TX);
489 /* Send new mode settings */
490 out_8(&psc->command, MPC52xx_PSC_SEL_MODE_REG_1);
491 out_8(&psc->mode, mr1);
492 out_8(&psc->mode, mr2);
493 out_8(&psc->ctur, ctr >> 8);
494 out_8(&psc->ctlr, ctr & 0xff);
496 /* Reenable TX & RX */
497 out_8(&psc->command, MPC52xx_PSC_TX_ENABLE);
498 out_8(&psc->command, MPC52xx_PSC_RX_ENABLE);
500 /* We're all set, release the lock */
501 spin_unlock_irqrestore(&port->lock, flags);
505 mpc52xx_uart_type(struct uart_port *port)
507 return port->type == PORT_MPC52xx ? "MPC52xx PSC" : NULL;
511 mpc52xx_uart_release_port(struct uart_port *port)
513 /* remapped by us ? */
514 if (port->flags & UPF_IOREMAP) {
515 iounmap(port->membase);
516 port->membase = NULL;
519 release_mem_region(port->mapbase, sizeof(struct mpc52xx_psc));
523 mpc52xx_uart_request_port(struct uart_port *port)
527 if (port->flags & UPF_IOREMAP) /* Need to remap ? */
528 port->membase = ioremap(port->mapbase,
529 sizeof(struct mpc52xx_psc));
534 err = request_mem_region(port->mapbase, sizeof(struct mpc52xx_psc),
535 "mpc52xx_psc_uart") != NULL ? 0 : -EBUSY;
537 if (err && (port->flags & UPF_IOREMAP)) {
538 iounmap(port->membase);
539 port->membase = NULL;
546 mpc52xx_uart_config_port(struct uart_port *port, int flags)
548 if ((flags & UART_CONFIG_TYPE)
549 && (mpc52xx_uart_request_port(port) == 0))
550 port->type = PORT_MPC52xx;
554 mpc52xx_uart_verify_port(struct uart_port *port, struct serial_struct *ser)
556 if (ser->type != PORT_UNKNOWN && ser->type != PORT_MPC52xx)
559 if ((ser->irq != port->irq) ||
560 (ser->io_type != SERIAL_IO_MEM) ||
561 (ser->baud_base != port->uartclk) ||
562 (ser->iomem_base != (void *)port->mapbase) ||
570 static struct uart_ops mpc52xx_uart_ops = {
571 .tx_empty = mpc52xx_uart_tx_empty,
572 .set_mctrl = mpc52xx_uart_set_mctrl,
573 .get_mctrl = mpc52xx_uart_get_mctrl,
574 .stop_tx = mpc52xx_uart_stop_tx,
575 .start_tx = mpc52xx_uart_start_tx,
576 .send_xchar = mpc52xx_uart_send_xchar,
577 .stop_rx = mpc52xx_uart_stop_rx,
578 .enable_ms = mpc52xx_uart_enable_ms,
579 .break_ctl = mpc52xx_uart_break_ctl,
580 .startup = mpc52xx_uart_startup,
581 .shutdown = mpc52xx_uart_shutdown,
582 .set_termios = mpc52xx_uart_set_termios,
583 /* .pm = mpc52xx_uart_pm, Not supported yet */
584 /* .set_wake = mpc52xx_uart_set_wake, Not supported yet */
585 .type = mpc52xx_uart_type,
586 .release_port = mpc52xx_uart_release_port,
587 .request_port = mpc52xx_uart_request_port,
588 .config_port = mpc52xx_uart_config_port,
589 .verify_port = mpc52xx_uart_verify_port
593 /* ======================================================================== */
594 /* Interrupt handling */
595 /* ======================================================================== */
598 mpc52xx_uart_int_rx_chars(struct uart_port *port)
600 struct tty_struct *tty = port->info->tty;
601 unsigned char ch, flag;
602 unsigned short status;
604 /* While we can read, do so ! */
605 while (psc_ops->raw_rx_rdy(port)) {
607 ch = psc_ops->read_char(port);
609 /* Handle sysreq char */
611 if (uart_handle_sysrq_char(port, ch)) {
622 status = in_be16(&PSC(port)->mpc52xx_psc_status);
624 if (status & (MPC52xx_PSC_SR_PE |
626 MPC52xx_PSC_SR_RB)) {
628 if (status & MPC52xx_PSC_SR_RB) {
630 uart_handle_break(port);
631 } else if (status & MPC52xx_PSC_SR_PE)
633 else if (status & MPC52xx_PSC_SR_FE)
636 /* Clear error condition */
637 out_8(&PSC(port)->command, MPC52xx_PSC_RST_ERR_STAT);
640 tty_insert_flip_char(tty, ch, flag);
641 if (status & MPC52xx_PSC_SR_OE) {
643 * Overrun is special, since it's
644 * reported immediately, and doesn't
645 * affect the current character
647 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
651 tty_flip_buffer_push(tty);
653 return psc_ops->raw_rx_rdy(port);
657 mpc52xx_uart_int_tx_chars(struct uart_port *port)
659 struct circ_buf *xmit = &port->info->xmit;
661 /* Process out of band chars */
663 psc_ops->write_char(port, port->x_char);
669 /* Nothing to do ? */
670 if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
671 mpc52xx_uart_stop_tx(port);
676 while (psc_ops->raw_tx_rdy(port)) {
677 psc_ops->write_char(port, xmit->buf[xmit->tail]);
678 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
680 if (uart_circ_empty(xmit))
685 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
686 uart_write_wakeup(port);
688 /* Maybe we're done after all */
689 if (uart_circ_empty(xmit)) {
690 mpc52xx_uart_stop_tx(port);
698 mpc52xx_uart_int(int irq, void *dev_id)
700 struct uart_port *port = dev_id;
701 unsigned long pass = ISR_PASS_LIMIT;
702 unsigned int keepgoing;
704 spin_lock(&port->lock);
706 /* While we have stuff to do, we continue */
708 /* If we don't find anything to do, we stop */
711 psc_ops->rx_clr_irq(port);
712 if (psc_ops->rx_rdy(port))
713 keepgoing |= mpc52xx_uart_int_rx_chars(port);
715 psc_ops->tx_clr_irq(port);
716 if (psc_ops->tx_rdy(port))
717 keepgoing |= mpc52xx_uart_int_tx_chars(port);
719 /* Limit number of iteration */
725 spin_unlock(&port->lock);
731 /* ======================================================================== */
732 /* Console ( if applicable ) */
733 /* ======================================================================== */
735 #ifdef CONFIG_SERIAL_MPC52xx_CONSOLE
738 mpc52xx_console_get_options(struct uart_port *port,
739 int *baud, int *parity, int *bits, int *flow)
741 struct mpc52xx_psc __iomem *psc = PSC(port);
744 pr_debug("mpc52xx_console_get_options(port=%p)\n", port);
746 /* Read the mode registers */
747 out_8(&psc->command, MPC52xx_PSC_SEL_MODE_REG_1);
748 mr1 = in_8(&psc->mode);
750 /* CT{U,L}R are write-only ! */
751 *baud = CONFIG_SERIAL_MPC52xx_CONSOLE_BAUD;
752 #if !defined(CONFIG_PPC_MERGE)
753 if (__res.bi_baudrate)
754 *baud = __res.bi_baudrate;
758 switch (mr1 & MPC52xx_PSC_MODE_BITS_MASK) {
759 case MPC52xx_PSC_MODE_5_BITS:
762 case MPC52xx_PSC_MODE_6_BITS:
765 case MPC52xx_PSC_MODE_7_BITS:
768 case MPC52xx_PSC_MODE_8_BITS:
773 if (mr1 & MPC52xx_PSC_MODE_PARNONE)
776 *parity = mr1 & MPC52xx_PSC_MODE_PARODD ? 'o' : 'e';
780 mpc52xx_console_write(struct console *co, const char *s, unsigned int count)
782 struct uart_port *port = &mpc52xx_uart_ports[co->index];
785 /* Disable interrupts */
786 psc_ops->cw_disable_ints(port);
788 /* Wait the TX buffer to be empty */
789 j = 5000000; /* Maximum wait */
790 while (!mpc52xx_uart_tx_empty(port) && --j)
793 /* Write all the chars */
794 for (i = 0; i < count; i++, s++) {
795 /* Line return handling */
797 psc_ops->write_char(port, '\r');
800 psc_ops->write_char(port, *s);
802 /* Wait the TX buffer to be empty */
803 j = 20000; /* Maximum wait */
804 while (!mpc52xx_uart_tx_empty(port) && --j)
808 /* Restore interrupt state */
809 psc_ops->cw_restore_ints(port);
812 #if !defined(CONFIG_PPC_MERGE)
814 mpc52xx_console_setup(struct console *co, char *options)
816 struct uart_port *port = &mpc52xx_uart_ports[co->index];
818 int baud = CONFIG_SERIAL_MPC52xx_CONSOLE_BAUD;
823 if (co->index < 0 || co->index >= MPC52xx_PSC_MAXNUM)
826 /* Basic port init. Needed since we use some uart_??? func before
827 * real init for early access */
828 spin_lock_init(&port->lock);
829 port->uartclk = __res.bi_ipbfreq / 2; /* Look at CTLR doc */
830 port->ops = &mpc52xx_uart_ops;
831 port->mapbase = MPC52xx_PA(MPC52xx_PSCx_OFFSET(co->index+1));
833 /* We ioremap ourself */
834 port->membase = ioremap(port->mapbase, MPC52xx_PSC_SIZE);
835 if (port->membase == NULL)
838 /* Setup the port parameters accoding to options */
840 uart_parse_options(options, &baud, &parity, &bits, &flow);
842 mpc52xx_console_get_options(port, &baud, &parity, &bits, &flow);
844 return uart_set_options(port, co, baud, parity, bits, flow);
850 mpc52xx_console_setup(struct console *co, char *options)
852 struct uart_port *port = &mpc52xx_uart_ports[co->index];
853 struct device_node *np = mpc52xx_uart_nodes[co->index];
854 unsigned int uartclk;
858 int baud = CONFIG_SERIAL_MPC52xx_CONSOLE_BAUD;
863 pr_debug("mpc52xx_console_setup co=%p, co->index=%i, options=%s\n",
864 co, co->index, options);
866 if ((co->index < 0) || (co->index > MPC52xx_PSC_MAXNUM)) {
867 pr_debug("PSC%x out of range\n", co->index);
872 pr_debug("PSC%x not found in device tree\n", co->index);
876 pr_debug("Console on ttyPSC%x is %s\n",
877 co->index, mpc52xx_uart_nodes[co->index]->full_name);
879 /* Fetch register locations */
880 ret = of_address_to_resource(np, 0, &res);
882 pr_debug("Could not get resources for PSC%x\n", co->index);
886 uartclk = psc_ops->getuartclk(np);
888 pr_debug("Could not find uart clock frequency!\n");
892 /* Basic port init. Needed since we use some uart_??? func before
893 * real init for early access */
894 spin_lock_init(&port->lock);
895 port->uartclk = uartclk;
896 port->ops = &mpc52xx_uart_ops;
897 port->mapbase = res.start;
898 port->membase = ioremap(res.start, sizeof(struct mpc52xx_psc));
899 port->irq = irq_of_parse_and_map(np, 0);
901 if (port->membase == NULL)
904 pr_debug("mpc52xx-psc uart at %p, mapped to %p, irq=%x, freq=%i\n",
905 (void *)port->mapbase, port->membase,
906 port->irq, port->uartclk);
908 /* Setup the port parameters accoding to options */
910 uart_parse_options(options, &baud, &parity, &bits, &flow);
912 mpc52xx_console_get_options(port, &baud, &parity, &bits, &flow);
914 pr_debug("Setting console parameters: %i %i%c1 flow=%c\n",
915 baud, bits, parity, flow);
917 return uart_set_options(port, co, baud, parity, bits, flow);
919 #endif /* defined(CONFIG_PPC_MERGE) */
922 static struct uart_driver mpc52xx_uart_driver;
924 static struct console mpc52xx_console = {
926 .write = mpc52xx_console_write,
927 .device = uart_console_device,
928 .setup = mpc52xx_console_setup,
929 .flags = CON_PRINTBUFFER,
930 .index = -1, /* Specified on the cmdline (e.g. console=ttyPSC0) */
931 .data = &mpc52xx_uart_driver,
936 mpc52xx_console_init(void)
938 #if defined(CONFIG_PPC_MERGE)
939 mpc52xx_uart_of_enumerate();
941 register_console(&mpc52xx_console);
945 console_initcall(mpc52xx_console_init);
947 #define MPC52xx_PSC_CONSOLE &mpc52xx_console
949 #define MPC52xx_PSC_CONSOLE NULL
953 /* ======================================================================== */
955 /* ======================================================================== */
957 static struct uart_driver mpc52xx_uart_driver = {
958 .driver_name = "mpc52xx_psc_uart",
959 .dev_name = "ttyPSC",
960 .major = SERIAL_PSC_MAJOR,
961 .minor = SERIAL_PSC_MINOR,
962 .nr = MPC52xx_PSC_MAXNUM,
963 .cons = MPC52xx_PSC_CONSOLE,
967 #if !defined(CONFIG_PPC_MERGE)
968 /* ======================================================================== */
969 /* Platform Driver */
970 /* ======================================================================== */
973 mpc52xx_uart_probe(struct platform_device *dev)
975 struct resource *res = dev->resource;
977 struct uart_port *port = NULL;
980 /* Check validity & presence */
982 if (idx < 0 || idx >= MPC52xx_PSC_MAXNUM)
985 if (!mpc52xx_match_psc_function(idx, "uart"))
988 /* Init the port structure */
989 port = &mpc52xx_uart_ports[idx];
991 spin_lock_init(&port->lock);
992 port->uartclk = __res.bi_ipbfreq / 2; /* Look at CTLR doc */
993 port->fifosize = 512;
994 port->iotype = UPIO_MEM;
995 port->flags = UPF_BOOT_AUTOCONF |
996 (uart_console(port) ? 0 : UPF_IOREMAP);
998 port->ops = &mpc52xx_uart_ops;
999 port->dev = &dev->dev;
1001 /* Search for IRQ and mapbase */
1002 for (i = 0 ; i < dev->num_resources ; i++, res++) {
1003 if (res->flags & IORESOURCE_MEM)
1004 port->mapbase = res->start;
1005 else if (res->flags & IORESOURCE_IRQ)
1006 port->irq = res->start;
1008 if (!port->irq || !port->mapbase)
1011 /* Add the port to the uart sub-system */
1012 ret = uart_add_one_port(&mpc52xx_uart_driver, port);
1014 platform_set_drvdata(dev, (void *)port);
1020 mpc52xx_uart_remove(struct platform_device *dev)
1022 struct uart_port *port = (struct uart_port *) platform_get_drvdata(dev);
1024 platform_set_drvdata(dev, NULL);
1027 uart_remove_one_port(&mpc52xx_uart_driver, port);
1034 mpc52xx_uart_suspend(struct platform_device *dev, pm_message_t state)
1036 struct uart_port *port = (struct uart_port *) platform_get_drvdata(dev);
1039 uart_suspend_port(&mpc52xx_uart_driver, port);
1045 mpc52xx_uart_resume(struct platform_device *dev)
1047 struct uart_port *port = (struct uart_port *) platform_get_drvdata(dev);
1050 uart_resume_port(&mpc52xx_uart_driver, port);
1057 static struct platform_driver mpc52xx_uart_platform_driver = {
1058 .probe = mpc52xx_uart_probe,
1059 .remove = mpc52xx_uart_remove,
1061 .suspend = mpc52xx_uart_suspend,
1062 .resume = mpc52xx_uart_resume,
1065 .owner = THIS_MODULE,
1066 .name = "mpc52xx-psc",
1069 #endif /* !defined(CONFIG_PPC_MERGE) */
1072 #if defined(CONFIG_PPC_MERGE)
1073 /* ======================================================================== */
1074 /* OF Platform Driver */
1075 /* ======================================================================== */
1077 static int __devinit
1078 mpc52xx_uart_of_probe(struct of_device *op, const struct of_device_id *match)
1081 unsigned int uartclk;
1082 struct uart_port *port = NULL;
1083 struct resource res;
1086 dev_dbg(&op->dev, "mpc52xx_uart_probe(op=%p, match=%p)\n", op, match);
1088 /* Check validity & presence */
1089 for (idx = 0; idx < MPC52xx_PSC_MAXNUM; idx++)
1090 if (mpc52xx_uart_nodes[idx] == op->node)
1092 if (idx >= MPC52xx_PSC_MAXNUM)
1094 pr_debug("Found %s assigned to ttyPSC%x\n",
1095 mpc52xx_uart_nodes[idx]->full_name, idx);
1097 uartclk = psc_ops->getuartclk(op->node);
1099 dev_dbg(&op->dev, "Could not find uart clock frequency!\n");
1103 /* Init the port structure */
1104 port = &mpc52xx_uart_ports[idx];
1106 spin_lock_init(&port->lock);
1107 port->uartclk = uartclk;
1108 port->fifosize = 512;
1109 port->iotype = UPIO_MEM;
1110 port->flags = UPF_BOOT_AUTOCONF |
1111 (uart_console(port) ? 0 : UPF_IOREMAP);
1113 port->ops = &mpc52xx_uart_ops;
1114 port->dev = &op->dev;
1116 /* Search for IRQ and mapbase */
1117 ret = of_address_to_resource(op->node, 0, &res);
1121 port->mapbase = res.start;
1122 port->irq = irq_of_parse_and_map(op->node, 0);
1124 dev_dbg(&op->dev, "mpc52xx-psc uart at %p, irq=%x, freq=%i\n",
1125 (void *)port->mapbase, port->irq, port->uartclk);
1127 if ((port->irq == NO_IRQ) || !port->mapbase) {
1128 printk(KERN_ERR "Could not allocate resources for PSC\n");
1132 /* Add the port to the uart sub-system */
1133 ret = uart_add_one_port(&mpc52xx_uart_driver, port);
1135 dev_set_drvdata(&op->dev, (void *)port);
1141 mpc52xx_uart_of_remove(struct of_device *op)
1143 struct uart_port *port = dev_get_drvdata(&op->dev);
1144 dev_set_drvdata(&op->dev, NULL);
1147 uart_remove_one_port(&mpc52xx_uart_driver, port);
1148 irq_dispose_mapping(port->irq);
1156 mpc52xx_uart_of_suspend(struct of_device *op, pm_message_t state)
1158 struct uart_port *port = (struct uart_port *) dev_get_drvdata(&op->dev);
1161 uart_suspend_port(&mpc52xx_uart_driver, port);
1167 mpc52xx_uart_of_resume(struct of_device *op)
1169 struct uart_port *port = (struct uart_port *) dev_get_drvdata(&op->dev);
1172 uart_resume_port(&mpc52xx_uart_driver, port);
1179 mpc52xx_uart_of_assign(struct device_node *np, int idx)
1184 /* Find the first free node */
1185 for (i = 0; i < MPC52xx_PSC_MAXNUM; i++) {
1186 if (mpc52xx_uart_nodes[i] == NULL) {
1192 if ((idx < 0) || (idx >= MPC52xx_PSC_MAXNUM))
1196 return; /* No free slot; abort */
1199 /* If the slot is already occupied, then swap slots */
1200 if (mpc52xx_uart_nodes[idx] && (free_idx != -1))
1201 mpc52xx_uart_nodes[free_idx] = mpc52xx_uart_nodes[idx];
1202 mpc52xx_uart_nodes[idx] = np;
1206 mpc52xx_uart_of_enumerate(void)
1208 static int enum_done;
1209 struct device_node *np;
1210 const unsigned int *devno;
1216 for_each_node_by_type(np, "serial") {
1217 if (!of_match_node(mpc52xx_uart_of_match, np))
1220 /* Is a particular device number requested? */
1221 devno = of_get_property(np, "port-number", NULL);
1222 mpc52xx_uart_of_assign(np, devno ? *devno : -1);
1227 for (i = 0; i < MPC52xx_PSC_MAXNUM; i++) {
1228 if (mpc52xx_uart_nodes[i])
1229 pr_debug("%s assigned to ttyPSC%x\n",
1230 mpc52xx_uart_nodes[i]->full_name, i);
1234 MODULE_DEVICE_TABLE(of, mpc52xx_uart_of_match);
1236 static struct of_platform_driver mpc52xx_uart_of_driver = {
1237 .match_table = mpc52xx_uart_of_match,
1238 .probe = mpc52xx_uart_of_probe,
1239 .remove = mpc52xx_uart_of_remove,
1241 .suspend = mpc52xx_uart_of_suspend,
1242 .resume = mpc52xx_uart_of_resume,
1245 .name = "mpc52xx-psc-uart",
1248 #endif /* defined(CONFIG_PPC_MERGE) */
1251 /* ======================================================================== */
1253 /* ======================================================================== */
1256 mpc52xx_uart_init(void)
1260 printk(KERN_INFO "Serial: MPC52xx PSC UART driver\n");
1262 ret = uart_register_driver(&mpc52xx_uart_driver);
1264 printk(KERN_ERR "%s: uart_register_driver failed (%i)\n",
1269 #if defined(CONFIG_PPC_MERGE)
1270 mpc52xx_uart_of_enumerate();
1272 ret = of_register_platform_driver(&mpc52xx_uart_of_driver);
1274 printk(KERN_ERR "%s: of_register_platform_driver failed (%i)\n",
1276 uart_unregister_driver(&mpc52xx_uart_driver);
1280 ret = platform_driver_register(&mpc52xx_uart_platform_driver);
1282 printk(KERN_ERR "%s: platform_driver_register failed (%i)\n",
1284 uart_unregister_driver(&mpc52xx_uart_driver);
1293 mpc52xx_uart_exit(void)
1295 #if defined(CONFIG_PPC_MERGE)
1296 of_unregister_platform_driver(&mpc52xx_uart_of_driver);
1298 platform_driver_unregister(&mpc52xx_uart_platform_driver);
1300 uart_unregister_driver(&mpc52xx_uart_driver);
1304 module_init(mpc52xx_uart_init);
1305 module_exit(mpc52xx_uart_exit);
1307 MODULE_AUTHOR("Sylvain Munaut <tnt@246tNt.com>");
1308 MODULE_DESCRIPTION("Freescale MPC52xx PSC UART");
1309 MODULE_LICENSE("GPL");