[PATCH] VFS: extra check inside dentry_unhash()
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / serial / mpc52xx_uart.c
blob6dd579ed977782d66e26465806afe5ee97f9701d
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 <asm/of_platform.h>
76 #else
77 #include <linux/platform_device.h>
78 #endif
80 #include <asm/mpc52xx.h>
81 #include <asm/mpc52xx_psc.h>
83 #if defined(CONFIG_SERIAL_MPC52xx_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
84 #define SUPPORT_SYSRQ
85 #endif
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
104 * the console_init
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);
111 #endif
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)
124 #else
125 #define uart_console(port) (0)
126 #endif
128 #if defined(CONFIG_PPC_MERGE)
129 static struct of_device_id mpc52xx_uart_of_match[] = {
130 { .type = "serial", .compatible = "mpc52xx-psc-uart", },
131 { .type = "serial", .compatible = "mpc5200-psc", }, /* Efika only! */
134 #endif
137 /* ======================================================================== */
138 /* UART operations */
139 /* ======================================================================== */
141 static unsigned int
142 mpc52xx_uart_tx_empty(struct uart_port *port)
144 int status = in_be16(&PSC(port)->mpc52xx_psc_status);
145 return (status & MPC52xx_PSC_SR_TXEMP) ? TIOCSER_TEMT : 0;
148 static void
149 mpc52xx_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
151 /* Not implemented */
154 static unsigned int
155 mpc52xx_uart_get_mctrl(struct uart_port *port)
157 /* Not implemented */
158 return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
161 static void
162 mpc52xx_uart_stop_tx(struct uart_port *port)
164 /* port->lock taken by caller */
165 port->read_status_mask &= ~MPC52xx_PSC_IMR_TXRDY;
166 out_be16(&PSC(port)->mpc52xx_psc_imr,port->read_status_mask);
169 static void
170 mpc52xx_uart_start_tx(struct uart_port *port)
172 /* port->lock taken by caller */
173 port->read_status_mask |= MPC52xx_PSC_IMR_TXRDY;
174 out_be16(&PSC(port)->mpc52xx_psc_imr,port->read_status_mask);
177 static void
178 mpc52xx_uart_send_xchar(struct uart_port *port, char ch)
180 unsigned long flags;
181 spin_lock_irqsave(&port->lock, flags);
183 port->x_char = ch;
184 if (ch) {
185 /* Make sure tx interrupts are on */
186 /* Truly necessary ??? They should be anyway */
187 port->read_status_mask |= MPC52xx_PSC_IMR_TXRDY;
188 out_be16(&PSC(port)->mpc52xx_psc_imr,port->read_status_mask);
191 spin_unlock_irqrestore(&port->lock, flags);
194 static void
195 mpc52xx_uart_stop_rx(struct uart_port *port)
197 /* port->lock taken by caller */
198 port->read_status_mask &= ~MPC52xx_PSC_IMR_RXRDY;
199 out_be16(&PSC(port)->mpc52xx_psc_imr,port->read_status_mask);
202 static void
203 mpc52xx_uart_enable_ms(struct uart_port *port)
205 /* Not implemented */
208 static void
209 mpc52xx_uart_break_ctl(struct uart_port *port, int ctl)
211 unsigned long flags;
212 spin_lock_irqsave(&port->lock, flags);
214 if ( ctl == -1 )
215 out_8(&PSC(port)->command,MPC52xx_PSC_START_BRK);
216 else
217 out_8(&PSC(port)->command,MPC52xx_PSC_STOP_BRK);
219 spin_unlock_irqrestore(&port->lock, flags);
222 static int
223 mpc52xx_uart_startup(struct uart_port *port)
225 struct mpc52xx_psc __iomem *psc = PSC(port);
226 int ret;
228 /* Request IRQ */
229 ret = request_irq(port->irq, mpc52xx_uart_int,
230 IRQF_DISABLED | IRQF_SAMPLE_RANDOM, "mpc52xx_psc_uart", port);
231 if (ret)
232 return ret;
234 /* Reset/activate the port, clear and enable interrupts */
235 out_8(&psc->command,MPC52xx_PSC_RST_RX);
236 out_8(&psc->command,MPC52xx_PSC_RST_TX);
238 out_be32(&psc->sicr,0); /* UART mode DCD ignored */
240 out_be16(&psc->mpc52xx_psc_clock_select, 0xdd00); /* /16 prescaler on */
242 out_8(&psc->rfcntl, 0x00);
243 out_be16(&psc->rfalarm, 0x1ff);
244 out_8(&psc->tfcntl, 0x07);
245 out_be16(&psc->tfalarm, 0x80);
247 port->read_status_mask |= MPC52xx_PSC_IMR_RXRDY | MPC52xx_PSC_IMR_TXRDY;
248 out_be16(&psc->mpc52xx_psc_imr,port->read_status_mask);
250 out_8(&psc->command,MPC52xx_PSC_TX_ENABLE);
251 out_8(&psc->command,MPC52xx_PSC_RX_ENABLE);
253 return 0;
256 static void
257 mpc52xx_uart_shutdown(struct uart_port *port)
259 struct mpc52xx_psc __iomem *psc = PSC(port);
261 /* Shut down the port, interrupt and all */
262 out_8(&psc->command,MPC52xx_PSC_RST_RX);
263 out_8(&psc->command,MPC52xx_PSC_RST_TX);
265 port->read_status_mask = 0;
266 out_be16(&psc->mpc52xx_psc_imr,port->read_status_mask);
268 /* Release interrupt */
269 free_irq(port->irq, port);
272 static void
273 mpc52xx_uart_set_termios(struct uart_port *port, struct termios *new,
274 struct termios *old)
276 struct mpc52xx_psc __iomem *psc = PSC(port);
277 unsigned long flags;
278 unsigned char mr1, mr2;
279 unsigned short ctr;
280 unsigned int j, baud, quot;
282 /* Prepare what we're gonna write */
283 mr1 = 0;
285 switch (new->c_cflag & CSIZE) {
286 case CS5: mr1 |= MPC52xx_PSC_MODE_5_BITS;
287 break;
288 case CS6: mr1 |= MPC52xx_PSC_MODE_6_BITS;
289 break;
290 case CS7: mr1 |= MPC52xx_PSC_MODE_7_BITS;
291 break;
292 case CS8:
293 default: mr1 |= MPC52xx_PSC_MODE_8_BITS;
296 if (new->c_cflag & PARENB) {
297 mr1 |= (new->c_cflag & PARODD) ?
298 MPC52xx_PSC_MODE_PARODD : MPC52xx_PSC_MODE_PAREVEN;
299 } else
300 mr1 |= MPC52xx_PSC_MODE_PARNONE;
303 mr2 = 0;
305 if (new->c_cflag & CSTOPB)
306 mr2 |= MPC52xx_PSC_MODE_TWO_STOP;
307 else
308 mr2 |= ((new->c_cflag & CSIZE) == CS5) ?
309 MPC52xx_PSC_MODE_ONE_STOP_5_BITS :
310 MPC52xx_PSC_MODE_ONE_STOP;
313 baud = uart_get_baud_rate(port, new, old, 0, port->uartclk/16);
314 quot = uart_get_divisor(port, baud);
315 ctr = quot & 0xffff;
317 /* Get the lock */
318 spin_lock_irqsave(&port->lock, flags);
320 /* Update the per-port timeout */
321 uart_update_timeout(port, new->c_cflag, baud);
323 /* Do our best to flush TX & RX, so we don't loose anything */
324 /* But we don't wait indefinitly ! */
325 j = 5000000; /* Maximum wait */
326 /* FIXME Can't receive chars since set_termios might be called at early
327 * boot for the console, all stuff is not yet ready to receive at that
328 * time and that just makes the kernel oops */
329 /* while (j-- && mpc52xx_uart_int_rx_chars(port)); */
330 while (!(in_be16(&psc->mpc52xx_psc_status) & MPC52xx_PSC_SR_TXEMP) &&
331 --j)
332 udelay(1);
334 if (!j)
335 printk( KERN_ERR "mpc52xx_uart.c: "
336 "Unable to flush RX & TX fifos in-time in set_termios."
337 "Some chars may have been lost.\n" );
339 /* Reset the TX & RX */
340 out_8(&psc->command,MPC52xx_PSC_RST_RX);
341 out_8(&psc->command,MPC52xx_PSC_RST_TX);
343 /* Send new mode settings */
344 out_8(&psc->command,MPC52xx_PSC_SEL_MODE_REG_1);
345 out_8(&psc->mode,mr1);
346 out_8(&psc->mode,mr2);
347 out_8(&psc->ctur,ctr >> 8);
348 out_8(&psc->ctlr,ctr & 0xff);
350 /* Reenable TX & RX */
351 out_8(&psc->command,MPC52xx_PSC_TX_ENABLE);
352 out_8(&psc->command,MPC52xx_PSC_RX_ENABLE);
354 /* We're all set, release the lock */
355 spin_unlock_irqrestore(&port->lock, flags);
358 static const char *
359 mpc52xx_uart_type(struct uart_port *port)
361 return port->type == PORT_MPC52xx ? "MPC52xx PSC" : NULL;
364 static void
365 mpc52xx_uart_release_port(struct uart_port *port)
367 if (port->flags & UPF_IOREMAP) { /* remapped by us ? */
368 iounmap(port->membase);
369 port->membase = NULL;
372 release_mem_region(port->mapbase, sizeof(struct mpc52xx_psc));
375 static int
376 mpc52xx_uart_request_port(struct uart_port *port)
378 int err;
380 if (port->flags & UPF_IOREMAP) /* Need to remap ? */
381 port->membase = ioremap(port->mapbase,
382 sizeof(struct mpc52xx_psc));
384 if (!port->membase)
385 return -EINVAL;
387 err = request_mem_region(port->mapbase, sizeof(struct mpc52xx_psc),
388 "mpc52xx_psc_uart") != NULL ? 0 : -EBUSY;
390 if (err && (port->flags & UPF_IOREMAP)) {
391 iounmap(port->membase);
392 port->membase = NULL;
395 return err;
398 static void
399 mpc52xx_uart_config_port(struct uart_port *port, int flags)
401 if ( (flags & UART_CONFIG_TYPE) &&
402 (mpc52xx_uart_request_port(port) == 0) )
403 port->type = PORT_MPC52xx;
406 static int
407 mpc52xx_uart_verify_port(struct uart_port *port, struct serial_struct *ser)
409 if ( ser->type != PORT_UNKNOWN && ser->type != PORT_MPC52xx )
410 return -EINVAL;
412 if ( (ser->irq != port->irq) ||
413 (ser->io_type != SERIAL_IO_MEM) ||
414 (ser->baud_base != port->uartclk) ||
415 (ser->iomem_base != (void*)port->mapbase) ||
416 (ser->hub6 != 0 ) )
417 return -EINVAL;
419 return 0;
423 static struct uart_ops mpc52xx_uart_ops = {
424 .tx_empty = mpc52xx_uart_tx_empty,
425 .set_mctrl = mpc52xx_uart_set_mctrl,
426 .get_mctrl = mpc52xx_uart_get_mctrl,
427 .stop_tx = mpc52xx_uart_stop_tx,
428 .start_tx = mpc52xx_uart_start_tx,
429 .send_xchar = mpc52xx_uart_send_xchar,
430 .stop_rx = mpc52xx_uart_stop_rx,
431 .enable_ms = mpc52xx_uart_enable_ms,
432 .break_ctl = mpc52xx_uart_break_ctl,
433 .startup = mpc52xx_uart_startup,
434 .shutdown = mpc52xx_uart_shutdown,
435 .set_termios = mpc52xx_uart_set_termios,
436 /* .pm = mpc52xx_uart_pm, Not supported yet */
437 /* .set_wake = mpc52xx_uart_set_wake, Not supported yet */
438 .type = mpc52xx_uart_type,
439 .release_port = mpc52xx_uart_release_port,
440 .request_port = mpc52xx_uart_request_port,
441 .config_port = mpc52xx_uart_config_port,
442 .verify_port = mpc52xx_uart_verify_port
446 /* ======================================================================== */
447 /* Interrupt handling */
448 /* ======================================================================== */
450 static inline int
451 mpc52xx_uart_int_rx_chars(struct uart_port *port)
453 struct tty_struct *tty = port->info->tty;
454 unsigned char ch, flag;
455 unsigned short status;
457 /* While we can read, do so ! */
458 while ( (status = in_be16(&PSC(port)->mpc52xx_psc_status)) &
459 MPC52xx_PSC_SR_RXRDY) {
461 /* Get the char */
462 ch = in_8(&PSC(port)->mpc52xx_psc_buffer_8);
464 /* Handle sysreq char */
465 #ifdef SUPPORT_SYSRQ
466 if (uart_handle_sysrq_char(port, ch)) {
467 port->sysrq = 0;
468 continue;
470 #endif
472 /* Store it */
474 flag = TTY_NORMAL;
475 port->icount.rx++;
477 if ( status & (MPC52xx_PSC_SR_PE |
478 MPC52xx_PSC_SR_FE |
479 MPC52xx_PSC_SR_RB) ) {
481 if (status & MPC52xx_PSC_SR_RB) {
482 flag = TTY_BREAK;
483 uart_handle_break(port);
484 } else if (status & MPC52xx_PSC_SR_PE)
485 flag = TTY_PARITY;
486 else if (status & MPC52xx_PSC_SR_FE)
487 flag = TTY_FRAME;
489 /* Clear error condition */
490 out_8(&PSC(port)->command,MPC52xx_PSC_RST_ERR_STAT);
493 tty_insert_flip_char(tty, ch, flag);
494 if (status & MPC52xx_PSC_SR_OE) {
496 * Overrun is special, since it's
497 * reported immediately, and doesn't
498 * affect the current character
500 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
504 tty_flip_buffer_push(tty);
506 return in_be16(&PSC(port)->mpc52xx_psc_status) & MPC52xx_PSC_SR_RXRDY;
509 static inline int
510 mpc52xx_uart_int_tx_chars(struct uart_port *port)
512 struct circ_buf *xmit = &port->info->xmit;
514 /* Process out of band chars */
515 if (port->x_char) {
516 out_8(&PSC(port)->mpc52xx_psc_buffer_8, port->x_char);
517 port->icount.tx++;
518 port->x_char = 0;
519 return 1;
522 /* Nothing to do ? */
523 if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
524 mpc52xx_uart_stop_tx(port);
525 return 0;
528 /* Send chars */
529 while (in_be16(&PSC(port)->mpc52xx_psc_status) & MPC52xx_PSC_SR_TXRDY) {
530 out_8(&PSC(port)->mpc52xx_psc_buffer_8, xmit->buf[xmit->tail]);
531 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
532 port->icount.tx++;
533 if (uart_circ_empty(xmit))
534 break;
537 /* Wake up */
538 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
539 uart_write_wakeup(port);
541 /* Maybe we're done after all */
542 if (uart_circ_empty(xmit)) {
543 mpc52xx_uart_stop_tx(port);
544 return 0;
547 return 1;
550 static irqreturn_t
551 mpc52xx_uart_int(int irq, void *dev_id)
553 struct uart_port *port = dev_id;
554 unsigned long pass = ISR_PASS_LIMIT;
555 unsigned int keepgoing;
556 unsigned short status;
558 spin_lock(&port->lock);
560 /* While we have stuff to do, we continue */
561 do {
562 /* If we don't find anything to do, we stop */
563 keepgoing = 0;
565 /* Read status */
566 status = in_be16(&PSC(port)->mpc52xx_psc_isr);
567 status &= port->read_status_mask;
569 /* Do we need to receive chars ? */
570 /* For this RX interrupts must be on and some chars waiting */
571 if ( status & MPC52xx_PSC_IMR_RXRDY )
572 keepgoing |= mpc52xx_uart_int_rx_chars(port);
574 /* Do we need to send chars ? */
575 /* For this, TX must be ready and TX interrupt enabled */
576 if ( status & MPC52xx_PSC_IMR_TXRDY )
577 keepgoing |= mpc52xx_uart_int_tx_chars(port);
579 /* Limit number of iteration */
580 if ( !(--pass) )
581 keepgoing = 0;
583 } while (keepgoing);
585 spin_unlock(&port->lock);
587 return IRQ_HANDLED;
591 /* ======================================================================== */
592 /* Console ( if applicable ) */
593 /* ======================================================================== */
595 #ifdef CONFIG_SERIAL_MPC52xx_CONSOLE
597 static void __init
598 mpc52xx_console_get_options(struct uart_port *port,
599 int *baud, int *parity, int *bits, int *flow)
601 struct mpc52xx_psc __iomem *psc = PSC(port);
602 unsigned char mr1;
604 pr_debug("mpc52xx_console_get_options(port=%p)\n", port);
606 /* Read the mode registers */
607 out_8(&psc->command,MPC52xx_PSC_SEL_MODE_REG_1);
608 mr1 = in_8(&psc->mode);
610 /* CT{U,L}R are write-only ! */
611 *baud = CONFIG_SERIAL_MPC52xx_CONSOLE_BAUD;
612 #if !defined(CONFIG_PPC_MERGE)
613 if (__res.bi_baudrate)
614 *baud = __res.bi_baudrate;
615 #endif
617 /* Parse them */
618 switch (mr1 & MPC52xx_PSC_MODE_BITS_MASK) {
619 case MPC52xx_PSC_MODE_5_BITS: *bits = 5; break;
620 case MPC52xx_PSC_MODE_6_BITS: *bits = 6; break;
621 case MPC52xx_PSC_MODE_7_BITS: *bits = 7; break;
622 case MPC52xx_PSC_MODE_8_BITS:
623 default: *bits = 8;
626 if (mr1 & MPC52xx_PSC_MODE_PARNONE)
627 *parity = 'n';
628 else
629 *parity = mr1 & MPC52xx_PSC_MODE_PARODD ? 'o' : 'e';
632 static void
633 mpc52xx_console_write(struct console *co, const char *s, unsigned int count)
635 struct uart_port *port = &mpc52xx_uart_ports[co->index];
636 struct mpc52xx_psc __iomem *psc = PSC(port);
637 unsigned int i, j;
639 /* Disable interrupts */
640 out_be16(&psc->mpc52xx_psc_imr, 0);
642 /* Wait the TX buffer to be empty */
643 j = 5000000; /* Maximum wait */
644 while (!(in_be16(&psc->mpc52xx_psc_status) & MPC52xx_PSC_SR_TXEMP) &&
645 --j)
646 udelay(1);
648 /* Write all the chars */
649 for (i = 0; i < count; i++, s++) {
650 /* Line return handling */
651 if (*s == '\n')
652 out_8(&psc->mpc52xx_psc_buffer_8, '\r');
654 /* Send the char */
655 out_8(&psc->mpc52xx_psc_buffer_8, *s);
657 /* Wait the TX buffer to be empty */
658 j = 20000; /* Maximum wait */
659 while (!(in_be16(&psc->mpc52xx_psc_status) &
660 MPC52xx_PSC_SR_TXEMP) && --j)
661 udelay(1);
664 /* Restore interrupt state */
665 out_be16(&psc->mpc52xx_psc_imr, port->read_status_mask);
668 #if !defined(CONFIG_PPC_MERGE)
669 static int __init
670 mpc52xx_console_setup(struct console *co, char *options)
672 struct uart_port *port = &mpc52xx_uart_ports[co->index];
674 int baud = CONFIG_SERIAL_MPC52xx_CONSOLE_BAUD;
675 int bits = 8;
676 int parity = 'n';
677 int flow = 'n';
679 if (co->index < 0 || co->index >= MPC52xx_PSC_MAXNUM)
680 return -EINVAL;
682 /* Basic port init. Needed since we use some uart_??? func before
683 * real init for early access */
684 spin_lock_init(&port->lock);
685 port->uartclk = __res.bi_ipbfreq / 2; /* Look at CTLR doc */
686 port->ops = &mpc52xx_uart_ops;
687 port->mapbase = MPC52xx_PA(MPC52xx_PSCx_OFFSET(co->index+1));
689 /* We ioremap ourself */
690 port->membase = ioremap(port->mapbase, MPC52xx_PSC_SIZE);
691 if (port->membase == NULL)
692 return -EINVAL;
694 /* Setup the port parameters accoding to options */
695 if (options)
696 uart_parse_options(options, &baud, &parity, &bits, &flow);
697 else
698 mpc52xx_console_get_options(port, &baud, &parity, &bits, &flow);
700 return uart_set_options(port, co, baud, parity, bits, flow);
703 #else
705 static int __init
706 mpc52xx_console_setup(struct console *co, char *options)
708 struct uart_port *port = &mpc52xx_uart_ports[co->index];
709 struct device_node *np = mpc52xx_uart_nodes[co->index];
710 unsigned int ipb_freq;
711 struct resource res;
712 int ret;
714 int baud = CONFIG_SERIAL_MPC52xx_CONSOLE_BAUD;
715 int bits = 8;
716 int parity = 'n';
717 int flow = 'n';
719 pr_debug("mpc52xx_console_setup co=%p, co->index=%i, options=%s\n",
720 co, co->index, options);
722 if ((co->index < 0) || (co->index > MPC52xx_PSC_MAXNUM)) {
723 pr_debug("PSC%x out of range\n", co->index);
724 return -EINVAL;
727 if (!np) {
728 pr_debug("PSC%x not found in device tree\n", co->index);
729 return -EINVAL;
732 pr_debug("Console on ttyPSC%x is %s\n",
733 co->index, mpc52xx_uart_nodes[co->index]->full_name);
735 /* Fetch register locations */
736 if ((ret = of_address_to_resource(np, 0, &res)) != 0) {
737 pr_debug("Could not get resources for PSC%x\n", co->index);
738 return ret;
741 /* Search for bus-frequency property in this node or a parent */
742 if ((ipb_freq = mpc52xx_find_ipb_freq(np)) == 0) {
743 pr_debug("Could not find IPB bus frequency!\n");
744 return -EINVAL;
747 /* Basic port init. Needed since we use some uart_??? func before
748 * real init for early access */
749 spin_lock_init(&port->lock);
750 port->uartclk = ipb_freq / 2;
751 port->ops = &mpc52xx_uart_ops;
752 port->mapbase = res.start;
753 port->membase = ioremap(res.start, sizeof(struct mpc52xx_psc));
754 port->irq = irq_of_parse_and_map(np, 0);
756 if (port->membase == NULL)
757 return -EINVAL;
759 pr_debug("mpc52xx-psc uart at %lx, mapped to %p, irq=%x, freq=%i\n",
760 port->mapbase, port->membase, port->irq, port->uartclk);
762 /* Setup the port parameters accoding to options */
763 if (options)
764 uart_parse_options(options, &baud, &parity, &bits, &flow);
765 else
766 mpc52xx_console_get_options(port, &baud, &parity, &bits, &flow);
768 pr_debug("Setting console parameters: %i %i%c1 flow=%c\n",
769 baud, bits, parity, flow);
771 return uart_set_options(port, co, baud, parity, bits, flow);
773 #endif /* defined(CONFIG_PPC_MERGE) */
776 static struct uart_driver mpc52xx_uart_driver;
778 static struct console mpc52xx_console = {
779 .name = "ttyPSC",
780 .write = mpc52xx_console_write,
781 .device = uart_console_device,
782 .setup = mpc52xx_console_setup,
783 .flags = CON_PRINTBUFFER,
784 .index = -1, /* Specified on the cmdline (e.g. console=ttyPSC0 ) */
785 .data = &mpc52xx_uart_driver,
789 static int __init
790 mpc52xx_console_init(void)
792 mpc52xx_uart_of_enumerate();
793 register_console(&mpc52xx_console);
794 return 0;
797 console_initcall(mpc52xx_console_init);
799 #define MPC52xx_PSC_CONSOLE &mpc52xx_console
800 #else
801 #define MPC52xx_PSC_CONSOLE NULL
802 #endif
805 /* ======================================================================== */
806 /* UART Driver */
807 /* ======================================================================== */
809 static struct uart_driver mpc52xx_uart_driver = {
810 .owner = THIS_MODULE,
811 .driver_name = "mpc52xx_psc_uart",
812 .dev_name = "ttyPSC",
813 .major = SERIAL_PSC_MAJOR,
814 .minor = SERIAL_PSC_MINOR,
815 .nr = MPC52xx_PSC_MAXNUM,
816 .cons = MPC52xx_PSC_CONSOLE,
820 #if !defined(CONFIG_PPC_MERGE)
821 /* ======================================================================== */
822 /* Platform Driver */
823 /* ======================================================================== */
825 static int __devinit
826 mpc52xx_uart_probe(struct platform_device *dev)
828 struct resource *res = dev->resource;
830 struct uart_port *port = NULL;
831 int i, idx, ret;
833 /* Check validity & presence */
834 idx = dev->id;
835 if (idx < 0 || idx >= MPC52xx_PSC_MAXNUM)
836 return -EINVAL;
838 if (!mpc52xx_match_psc_function(idx,"uart"))
839 return -ENODEV;
841 /* Init the port structure */
842 port = &mpc52xx_uart_ports[idx];
844 spin_lock_init(&port->lock);
845 port->uartclk = __res.bi_ipbfreq / 2; /* Look at CTLR doc */
846 port->fifosize = 512;
847 port->iotype = UPIO_MEM;
848 port->flags = UPF_BOOT_AUTOCONF |
849 ( uart_console(port) ? 0 : UPF_IOREMAP );
850 port->line = idx;
851 port->ops = &mpc52xx_uart_ops;
852 port->dev = &dev->dev;
854 /* Search for IRQ and mapbase */
855 for (i=0 ; i<dev->num_resources ; i++, res++) {
856 if (res->flags & IORESOURCE_MEM)
857 port->mapbase = res->start;
858 else if (res->flags & IORESOURCE_IRQ)
859 port->irq = res->start;
861 if (!port->irq || !port->mapbase)
862 return -EINVAL;
864 /* Add the port to the uart sub-system */
865 ret = uart_add_one_port(&mpc52xx_uart_driver, port);
866 if (!ret)
867 platform_set_drvdata(dev, (void*)port);
869 return ret;
872 static int
873 mpc52xx_uart_remove(struct platform_device *dev)
875 struct uart_port *port = (struct uart_port *) platform_get_drvdata(dev);
877 platform_set_drvdata(dev, NULL);
879 if (port)
880 uart_remove_one_port(&mpc52xx_uart_driver, port);
882 return 0;
885 #ifdef CONFIG_PM
886 static int
887 mpc52xx_uart_suspend(struct platform_device *dev, pm_message_t state)
889 struct uart_port *port = (struct uart_port *) platform_get_drvdata(dev);
891 if (port)
892 uart_suspend_port(&mpc52xx_uart_driver, port);
894 return 0;
897 static int
898 mpc52xx_uart_resume(struct platform_device *dev)
900 struct uart_port *port = (struct uart_port *) platform_get_drvdata(dev);
902 if (port)
903 uart_resume_port(&mpc52xx_uart_driver, port);
905 return 0;
907 #endif
910 static struct platform_driver mpc52xx_uart_platform_driver = {
911 .probe = mpc52xx_uart_probe,
912 .remove = mpc52xx_uart_remove,
913 #ifdef CONFIG_PM
914 .suspend = mpc52xx_uart_suspend,
915 .resume = mpc52xx_uart_resume,
916 #endif
917 .driver = {
918 .name = "mpc52xx-psc",
921 #endif /* !defined(CONFIG_PPC_MERGE) */
924 #if defined(CONFIG_PPC_MERGE)
925 /* ======================================================================== */
926 /* OF Platform Driver */
927 /* ======================================================================== */
929 static int __devinit
930 mpc52xx_uart_of_probe(struct of_device *op, const struct of_device_id *match)
932 int idx = -1;
933 unsigned int ipb_freq;
934 struct uart_port *port = NULL;
935 struct resource res;
936 int ret;
938 dev_dbg(&op->dev, "mpc52xx_uart_probe(op=%p, match=%p)\n", op, match);
940 /* Check validity & presence */
941 for (idx = 0; idx < MPC52xx_PSC_MAXNUM; idx++)
942 if (mpc52xx_uart_nodes[idx] == op->node)
943 break;
944 if (idx >= MPC52xx_PSC_MAXNUM)
945 return -EINVAL;
946 pr_debug("Found %s assigned to ttyPSC%x\n",
947 mpc52xx_uart_nodes[idx]->full_name, idx);
949 /* Search for bus-frequency property in this node or a parent */
950 if ((ipb_freq = mpc52xx_find_ipb_freq(op->node)) == 0) {
951 dev_dbg(&op->dev, "Could not find IPB bus frequency!\n");
952 return -EINVAL;
955 /* Init the port structure */
956 port = &mpc52xx_uart_ports[idx];
958 spin_lock_init(&port->lock);
959 port->uartclk = ipb_freq / 2;
960 port->fifosize = 512;
961 port->iotype = UPIO_MEM;
962 port->flags = UPF_BOOT_AUTOCONF |
963 ( uart_console(port) ? 0 : UPF_IOREMAP );
964 port->line = idx;
965 port->ops = &mpc52xx_uart_ops;
966 port->dev = &op->dev;
968 /* Search for IRQ and mapbase */
969 if ((ret = of_address_to_resource(op->node, 0, &res)) != 0)
970 return ret;
972 port->mapbase = res.start;
973 port->irq = irq_of_parse_and_map(op->node, 0);
975 dev_dbg(&op->dev, "mpc52xx-psc uart at %lx, irq=%x, freq=%i\n",
976 port->mapbase, port->irq, port->uartclk);
978 if ((port->irq==NO_IRQ) || !port->mapbase) {
979 printk(KERN_ERR "Could not allocate resources for PSC\n");
980 return -EINVAL;
983 /* Add the port to the uart sub-system */
984 ret = uart_add_one_port(&mpc52xx_uart_driver, port);
985 if (!ret)
986 dev_set_drvdata(&op->dev, (void*)port);
988 return ret;
991 static int
992 mpc52xx_uart_of_remove(struct of_device *op)
994 struct uart_port *port = dev_get_drvdata(&op->dev);
995 dev_set_drvdata(&op->dev, NULL);
997 if (port)
998 uart_remove_one_port(&mpc52xx_uart_driver, port);
1000 return 0;
1003 #ifdef CONFIG_PM
1004 static int
1005 mpc52xx_uart_of_suspend(struct of_device *op, pm_message_t state)
1007 struct uart_port *port = (struct uart_port *) dev_get_drvdata(&op->dev);
1009 if (port)
1010 uart_suspend_port(&mpc52xx_uart_driver, port);
1012 return 0;
1015 static int
1016 mpc52xx_uart_of_resume(struct of_device *op)
1018 struct uart_port *port = (struct uart_port *) dev_get_drvdata(&op->dev);
1020 if (port)
1021 uart_resume_port(&mpc52xx_uart_driver, port);
1023 return 0;
1025 #endif
1027 static void
1028 mpc52xx_uart_of_assign(struct device_node *np, int idx)
1030 int free_idx = -1;
1031 int i;
1033 /* Find the first free node */
1034 for (i = 0; i < MPC52xx_PSC_MAXNUM; i++) {
1035 if (mpc52xx_uart_nodes[i] == NULL) {
1036 free_idx = i;
1037 break;
1041 if ((idx < 0) || (idx >= MPC52xx_PSC_MAXNUM))
1042 idx = free_idx;
1044 if (idx < 0)
1045 return; /* No free slot; abort */
1047 /* If the slot is already occupied, then swap slots */
1048 if (mpc52xx_uart_nodes[idx] && (free_idx != -1))
1049 mpc52xx_uart_nodes[free_idx] = mpc52xx_uart_nodes[idx];
1050 mpc52xx_uart_nodes[i] = np;
1053 static void
1054 mpc52xx_uart_of_enumerate(void)
1056 static int enum_done = 0;
1057 struct device_node *np;
1058 const unsigned int *devno;
1059 int i;
1061 if (enum_done)
1062 return;
1064 for_each_node_by_type(np, "serial") {
1065 if (!of_match_node(mpc52xx_uart_of_match, np))
1066 continue;
1068 /* Is a particular device number requested? */
1069 devno = get_property(np, "device_no", NULL);
1070 mpc52xx_uart_of_assign(of_node_get(np), devno ? *devno : -1);
1073 enum_done = 1;
1075 for (i = 0; i < MPC52xx_PSC_MAXNUM; i++) {
1076 if (mpc52xx_uart_nodes[i])
1077 pr_debug("%s assigned to ttyPSC%x\n",
1078 mpc52xx_uart_nodes[i]->full_name, i);
1082 MODULE_DEVICE_TABLE(of, mpc52xx_uart_of_match);
1084 static struct of_platform_driver mpc52xx_uart_of_driver = {
1085 .owner = THIS_MODULE,
1086 .name = "mpc52xx-psc-uart",
1087 .match_table = mpc52xx_uart_of_match,
1088 .probe = mpc52xx_uart_of_probe,
1089 .remove = mpc52xx_uart_of_remove,
1090 #ifdef CONFIG_PM
1091 .suspend = mpc52xx_uart_of_suspend,
1092 .resume = mpc52xx_uart_of_resume,
1093 #endif
1094 .driver = {
1095 .name = "mpc52xx-psc-uart",
1098 #endif /* defined(CONFIG_PPC_MERGE) */
1101 /* ======================================================================== */
1102 /* Module */
1103 /* ======================================================================== */
1105 static int __init
1106 mpc52xx_uart_init(void)
1108 int ret;
1110 printk(KERN_INFO "Serial: MPC52xx PSC UART driver\n");
1112 if ((ret = uart_register_driver(&mpc52xx_uart_driver)) != 0) {
1113 printk(KERN_ERR "%s: uart_register_driver failed (%i)\n",
1114 __FILE__, ret);
1115 return ret;
1118 #if defined(CONFIG_PPC_MERGE)
1119 mpc52xx_uart_of_enumerate();
1121 ret = of_register_platform_driver(&mpc52xx_uart_of_driver);
1122 if (ret) {
1123 printk(KERN_ERR "%s: of_register_platform_driver failed (%i)\n",
1124 __FILE__, ret);
1125 uart_unregister_driver(&mpc52xx_uart_driver);
1126 return ret;
1128 #else
1129 ret = platform_driver_register(&mpc52xx_uart_platform_driver);
1130 if (ret) {
1131 printk(KERN_ERR "%s: platform_driver_register failed (%i)\n",
1132 __FILE__, ret);
1133 uart_unregister_driver(&mpc52xx_uart_driver);
1134 return ret;
1136 #endif
1138 return 0;
1141 static void __exit
1142 mpc52xx_uart_exit(void)
1144 #if defined(CONFIG_PPC_MERGE)
1145 of_unregister_platform_driver(&mpc52xx_uart_of_driver);
1146 #else
1147 platform_driver_unregister(&mpc52xx_uart_platform_driver);
1148 #endif
1149 uart_unregister_driver(&mpc52xx_uart_driver);
1153 module_init(mpc52xx_uart_init);
1154 module_exit(mpc52xx_uart_exit);
1156 MODULE_AUTHOR("Sylvain Munaut <tnt@246tNt.com>");
1157 MODULE_DESCRIPTION("Freescale MPC52xx PSC UART");
1158 MODULE_LICENSE("GPL");