dm: eliminate some holes data structures
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / serial / mpc52xx_uart.c
blob3119fddaedb5708ec80346dfb239323365292608
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) 2008 Freescale Semiconductor Inc.
20 * John Rigby <jrigby@gmail.com>
21 * Added support for MPC5121
22 * Copyright (C) 2006 Secret Lab Technologies Ltd.
23 * Grant Likely <grant.likely@secretlab.ca>
24 * Copyright (C) 2004-2006 Sylvain Munaut <tnt@246tNt.com>
25 * Copyright (C) 2003 MontaVista, Software, Inc.
27 * This file is licensed under the terms of the GNU General Public License
28 * version 2. This program is licensed "as is" without any warranty of any
29 * kind, whether express or implied.
32 /* Platform device Usage :
34 * Since PSCs can have multiple function, the correct driver for each one
35 * is selected by calling mpc52xx_match_psc_function(...). The function
36 * handled by this driver is "uart".
38 * The driver init all necessary registers to place the PSC in uart mode without
39 * DCD. However, the pin multiplexing aren't changed and should be set either
40 * by the bootloader or in the platform init code.
42 * The idx field must be equal to the PSC index (e.g. 0 for PSC1, 1 for PSC2,
43 * and so on). So the PSC1 is mapped to /dev/ttyPSC0, PSC2 to /dev/ttyPSC1 and
44 * so on. But be warned, it's an ABSOLUTE REQUIREMENT ! This is needed mainly
45 * fpr the console code : without this 1:1 mapping, at early boot time, when we
46 * are parsing the kernel args console=ttyPSC?, we wouldn't know which PSC it
47 * will be mapped to.
50 /* OF Platform device Usage :
52 * This driver is only used for PSCs configured in uart mode. The device
53 * tree will have a node for each PSC with "mpc52xx-psc-uart" in the compatible
54 * list.
56 * By default, PSC devices are enumerated in the order they are found. However
57 * a particular PSC number can be forces by adding 'device_no = <port#>'
58 * to the device node.
60 * The driver init all necessary registers to place the PSC in uart mode without
61 * DCD. However, the pin multiplexing aren't changed and should be set either
62 * by the bootloader or in the platform init code.
65 #undef DEBUG
67 #include <linux/device.h>
68 #include <linux/module.h>
69 #include <linux/tty.h>
70 #include <linux/serial.h>
71 #include <linux/sysrq.h>
72 #include <linux/console.h>
73 #include <linux/delay.h>
74 #include <linux/io.h>
75 #include <linux/of.h>
76 #include <linux/of_platform.h>
77 #include <linux/clk.h>
79 #include <asm/mpc52xx.h>
80 #include <asm/mpc52xx_psc.h>
82 #if defined(CONFIG_SERIAL_MPC52xx_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
83 #define SUPPORT_SYSRQ
84 #endif
86 #include <linux/serial_core.h>
89 /* We've been assigned a range on the "Low-density serial ports" major */
90 #define SERIAL_PSC_MAJOR 204
91 #define SERIAL_PSC_MINOR 148
94 #define ISR_PASS_LIMIT 256 /* Max number of iteration in the interrupt */
97 static struct uart_port mpc52xx_uart_ports[MPC52xx_PSC_MAXNUM];
98 /* Rem: - We use the read_status_mask as a shadow of
99 * psc->mpc52xx_psc_imr
100 * - It's important that is array is all zero on start as we
101 * use it to know if it's initialized or not ! If it's not sure
102 * it's cleared, then a memset(...,0,...) should be added to
103 * the console_init
106 /* lookup table for matching device nodes to index numbers */
107 static struct device_node *mpc52xx_uart_nodes[MPC52xx_PSC_MAXNUM];
109 static void mpc52xx_uart_of_enumerate(void);
112 #define PSC(port) ((struct mpc52xx_psc __iomem *)((port)->membase))
115 /* Forward declaration of the interruption handling routine */
116 static irqreturn_t mpc52xx_uart_int(int irq, void *dev_id);
117 static irqreturn_t mpc5xxx_uart_process_int(struct uart_port *port);
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) \
124 ((port)->cons && (port)->cons->index == (port)->line)
125 #else
126 #define uart_console(port) (0)
127 #endif
129 /* ======================================================================== */
130 /* PSC fifo operations for isolating differences between 52xx and 512x */
131 /* ======================================================================== */
133 struct psc_ops {
134 void (*fifo_init)(struct uart_port *port);
135 int (*raw_rx_rdy)(struct uart_port *port);
136 int (*raw_tx_rdy)(struct uart_port *port);
137 int (*rx_rdy)(struct uart_port *port);
138 int (*tx_rdy)(struct uart_port *port);
139 int (*tx_empty)(struct uart_port *port);
140 void (*stop_rx)(struct uart_port *port);
141 void (*start_tx)(struct uart_port *port);
142 void (*stop_tx)(struct uart_port *port);
143 void (*rx_clr_irq)(struct uart_port *port);
144 void (*tx_clr_irq)(struct uart_port *port);
145 void (*write_char)(struct uart_port *port, unsigned char c);
146 unsigned char (*read_char)(struct uart_port *port);
147 void (*cw_disable_ints)(struct uart_port *port);
148 void (*cw_restore_ints)(struct uart_port *port);
149 unsigned long (*getuartclk)(void *p);
150 int (*clock)(struct uart_port *port, int enable);
151 int (*fifoc_init)(void);
152 void (*fifoc_uninit)(void);
153 void (*get_irq)(struct uart_port *, struct device_node *);
154 irqreturn_t (*handle_irq)(struct uart_port *port);
157 #ifdef CONFIG_PPC_MPC52xx
158 #define FIFO_52xx(port) ((struct mpc52xx_psc_fifo __iomem *)(PSC(port)+1))
159 static void mpc52xx_psc_fifo_init(struct uart_port *port)
161 struct mpc52xx_psc __iomem *psc = PSC(port);
162 struct mpc52xx_psc_fifo __iomem *fifo = FIFO_52xx(port);
164 /* /32 prescaler */
165 out_be16(&psc->mpc52xx_psc_clock_select, 0xdd00);
167 out_8(&fifo->rfcntl, 0x00);
168 out_be16(&fifo->rfalarm, 0x1ff);
169 out_8(&fifo->tfcntl, 0x07);
170 out_be16(&fifo->tfalarm, 0x80);
172 port->read_status_mask |= MPC52xx_PSC_IMR_RXRDY | MPC52xx_PSC_IMR_TXRDY;
173 out_be16(&psc->mpc52xx_psc_imr, port->read_status_mask);
176 static int mpc52xx_psc_raw_rx_rdy(struct uart_port *port)
178 return in_be16(&PSC(port)->mpc52xx_psc_status)
179 & MPC52xx_PSC_SR_RXRDY;
182 static int mpc52xx_psc_raw_tx_rdy(struct uart_port *port)
184 return in_be16(&PSC(port)->mpc52xx_psc_status)
185 & MPC52xx_PSC_SR_TXRDY;
189 static int mpc52xx_psc_rx_rdy(struct uart_port *port)
191 return in_be16(&PSC(port)->mpc52xx_psc_isr)
192 & port->read_status_mask
193 & MPC52xx_PSC_IMR_RXRDY;
196 static int mpc52xx_psc_tx_rdy(struct uart_port *port)
198 return in_be16(&PSC(port)->mpc52xx_psc_isr)
199 & port->read_status_mask
200 & MPC52xx_PSC_IMR_TXRDY;
203 static int mpc52xx_psc_tx_empty(struct uart_port *port)
205 return in_be16(&PSC(port)->mpc52xx_psc_status)
206 & MPC52xx_PSC_SR_TXEMP;
209 static void mpc52xx_psc_start_tx(struct uart_port *port)
211 port->read_status_mask |= MPC52xx_PSC_IMR_TXRDY;
212 out_be16(&PSC(port)->mpc52xx_psc_imr, port->read_status_mask);
215 static void mpc52xx_psc_stop_tx(struct uart_port *port)
217 port->read_status_mask &= ~MPC52xx_PSC_IMR_TXRDY;
218 out_be16(&PSC(port)->mpc52xx_psc_imr, port->read_status_mask);
221 static void mpc52xx_psc_stop_rx(struct uart_port *port)
223 port->read_status_mask &= ~MPC52xx_PSC_IMR_RXRDY;
224 out_be16(&PSC(port)->mpc52xx_psc_imr, port->read_status_mask);
227 static void mpc52xx_psc_rx_clr_irq(struct uart_port *port)
231 static void mpc52xx_psc_tx_clr_irq(struct uart_port *port)
235 static void mpc52xx_psc_write_char(struct uart_port *port, unsigned char c)
237 out_8(&PSC(port)->mpc52xx_psc_buffer_8, c);
240 static unsigned char mpc52xx_psc_read_char(struct uart_port *port)
242 return in_8(&PSC(port)->mpc52xx_psc_buffer_8);
245 static void mpc52xx_psc_cw_disable_ints(struct uart_port *port)
247 out_be16(&PSC(port)->mpc52xx_psc_imr, 0);
250 static void mpc52xx_psc_cw_restore_ints(struct uart_port *port)
252 out_be16(&PSC(port)->mpc52xx_psc_imr, port->read_status_mask);
255 /* Search for bus-frequency property in this node or a parent */
256 static unsigned long mpc52xx_getuartclk(void *p)
259 * 5200 UARTs have a / 32 prescaler
260 * but the generic serial code assumes 16
261 * so return ipb freq / 2
263 return mpc5xxx_get_bus_frequency(p) / 2;
266 static void mpc52xx_psc_get_irq(struct uart_port *port, struct device_node *np)
268 port->irqflags = IRQF_DISABLED;
269 port->irq = irq_of_parse_and_map(np, 0);
272 /* 52xx specific interrupt handler. The caller holds the port lock */
273 static irqreturn_t mpc52xx_psc_handle_irq(struct uart_port *port)
275 return mpc5xxx_uart_process_int(port);
278 static struct psc_ops mpc52xx_psc_ops = {
279 .fifo_init = mpc52xx_psc_fifo_init,
280 .raw_rx_rdy = mpc52xx_psc_raw_rx_rdy,
281 .raw_tx_rdy = mpc52xx_psc_raw_tx_rdy,
282 .rx_rdy = mpc52xx_psc_rx_rdy,
283 .tx_rdy = mpc52xx_psc_tx_rdy,
284 .tx_empty = mpc52xx_psc_tx_empty,
285 .stop_rx = mpc52xx_psc_stop_rx,
286 .start_tx = mpc52xx_psc_start_tx,
287 .stop_tx = mpc52xx_psc_stop_tx,
288 .rx_clr_irq = mpc52xx_psc_rx_clr_irq,
289 .tx_clr_irq = mpc52xx_psc_tx_clr_irq,
290 .write_char = mpc52xx_psc_write_char,
291 .read_char = mpc52xx_psc_read_char,
292 .cw_disable_ints = mpc52xx_psc_cw_disable_ints,
293 .cw_restore_ints = mpc52xx_psc_cw_restore_ints,
294 .getuartclk = mpc52xx_getuartclk,
295 .get_irq = mpc52xx_psc_get_irq,
296 .handle_irq = mpc52xx_psc_handle_irq,
299 #endif /* CONFIG_MPC52xx */
301 #ifdef CONFIG_PPC_MPC512x
302 #define FIFO_512x(port) ((struct mpc512x_psc_fifo __iomem *)(PSC(port)+1))
304 /* PSC FIFO Controller for mpc512x */
305 struct psc_fifoc {
306 u32 fifoc_cmd;
307 u32 fifoc_int;
308 u32 fifoc_dma;
309 u32 fifoc_axe;
310 u32 fifoc_debug;
313 static struct psc_fifoc __iomem *psc_fifoc;
314 static unsigned int psc_fifoc_irq;
316 static void mpc512x_psc_fifo_init(struct uart_port *port)
318 /* /32 prescaler */
319 out_be16(&PSC(port)->mpc52xx_psc_clock_select, 0xdd00);
321 out_be32(&FIFO_512x(port)->txcmd, MPC512x_PSC_FIFO_RESET_SLICE);
322 out_be32(&FIFO_512x(port)->txcmd, MPC512x_PSC_FIFO_ENABLE_SLICE);
323 out_be32(&FIFO_512x(port)->txalarm, 1);
324 out_be32(&FIFO_512x(port)->tximr, 0);
326 out_be32(&FIFO_512x(port)->rxcmd, MPC512x_PSC_FIFO_RESET_SLICE);
327 out_be32(&FIFO_512x(port)->rxcmd, MPC512x_PSC_FIFO_ENABLE_SLICE);
328 out_be32(&FIFO_512x(port)->rxalarm, 1);
329 out_be32(&FIFO_512x(port)->rximr, 0);
331 out_be32(&FIFO_512x(port)->tximr, MPC512x_PSC_FIFO_ALARM);
332 out_be32(&FIFO_512x(port)->rximr, MPC512x_PSC_FIFO_ALARM);
335 static int mpc512x_psc_raw_rx_rdy(struct uart_port *port)
337 return !(in_be32(&FIFO_512x(port)->rxsr) & MPC512x_PSC_FIFO_EMPTY);
340 static int mpc512x_psc_raw_tx_rdy(struct uart_port *port)
342 return !(in_be32(&FIFO_512x(port)->txsr) & MPC512x_PSC_FIFO_FULL);
345 static int mpc512x_psc_rx_rdy(struct uart_port *port)
347 return in_be32(&FIFO_512x(port)->rxsr)
348 & in_be32(&FIFO_512x(port)->rximr)
349 & MPC512x_PSC_FIFO_ALARM;
352 static int mpc512x_psc_tx_rdy(struct uart_port *port)
354 return in_be32(&FIFO_512x(port)->txsr)
355 & in_be32(&FIFO_512x(port)->tximr)
356 & MPC512x_PSC_FIFO_ALARM;
359 static int mpc512x_psc_tx_empty(struct uart_port *port)
361 return in_be32(&FIFO_512x(port)->txsr)
362 & MPC512x_PSC_FIFO_EMPTY;
365 static void mpc512x_psc_stop_rx(struct uart_port *port)
367 unsigned long rx_fifo_imr;
369 rx_fifo_imr = in_be32(&FIFO_512x(port)->rximr);
370 rx_fifo_imr &= ~MPC512x_PSC_FIFO_ALARM;
371 out_be32(&FIFO_512x(port)->rximr, rx_fifo_imr);
374 static void mpc512x_psc_start_tx(struct uart_port *port)
376 unsigned long tx_fifo_imr;
378 tx_fifo_imr = in_be32(&FIFO_512x(port)->tximr);
379 tx_fifo_imr |= MPC512x_PSC_FIFO_ALARM;
380 out_be32(&FIFO_512x(port)->tximr, tx_fifo_imr);
383 static void mpc512x_psc_stop_tx(struct uart_port *port)
385 unsigned long tx_fifo_imr;
387 tx_fifo_imr = in_be32(&FIFO_512x(port)->tximr);
388 tx_fifo_imr &= ~MPC512x_PSC_FIFO_ALARM;
389 out_be32(&FIFO_512x(port)->tximr, tx_fifo_imr);
392 static void mpc512x_psc_rx_clr_irq(struct uart_port *port)
394 out_be32(&FIFO_512x(port)->rxisr, in_be32(&FIFO_512x(port)->rxisr));
397 static void mpc512x_psc_tx_clr_irq(struct uart_port *port)
399 out_be32(&FIFO_512x(port)->txisr, in_be32(&FIFO_512x(port)->txisr));
402 static void mpc512x_psc_write_char(struct uart_port *port, unsigned char c)
404 out_8(&FIFO_512x(port)->txdata_8, c);
407 static unsigned char mpc512x_psc_read_char(struct uart_port *port)
409 return in_8(&FIFO_512x(port)->rxdata_8);
412 static void mpc512x_psc_cw_disable_ints(struct uart_port *port)
414 port->read_status_mask =
415 in_be32(&FIFO_512x(port)->tximr) << 16 |
416 in_be32(&FIFO_512x(port)->rximr);
417 out_be32(&FIFO_512x(port)->tximr, 0);
418 out_be32(&FIFO_512x(port)->rximr, 0);
421 static void mpc512x_psc_cw_restore_ints(struct uart_port *port)
423 out_be32(&FIFO_512x(port)->tximr,
424 (port->read_status_mask >> 16) & 0x7f);
425 out_be32(&FIFO_512x(port)->rximr, port->read_status_mask & 0x7f);
428 static unsigned long mpc512x_getuartclk(void *p)
430 return mpc5xxx_get_bus_frequency(p);
433 #define DEFAULT_FIFO_SIZE 16
435 static unsigned int __init get_fifo_size(struct device_node *np,
436 char *fifo_name)
438 const unsigned int *fp;
440 fp = of_get_property(np, fifo_name, NULL);
441 if (fp)
442 return *fp;
444 pr_warning("no %s property in %s node, defaulting to %d\n",
445 fifo_name, np->full_name, DEFAULT_FIFO_SIZE);
447 return DEFAULT_FIFO_SIZE;
450 #define FIFOC(_base) ((struct mpc512x_psc_fifo __iomem *) \
451 ((u32)(_base) + sizeof(struct mpc52xx_psc)))
453 /* Init PSC FIFO Controller */
454 static int __init mpc512x_psc_fifoc_init(void)
456 struct device_node *np;
457 void __iomem *psc;
458 unsigned int tx_fifo_size;
459 unsigned int rx_fifo_size;
460 int fifobase = 0; /* current fifo address in 32 bit words */
462 np = of_find_compatible_node(NULL, NULL,
463 "fsl,mpc5121-psc-fifo");
464 if (!np) {
465 pr_err("%s: Can't find FIFOC node\n", __func__);
466 return -ENODEV;
469 psc_fifoc = of_iomap(np, 0);
470 if (!psc_fifoc) {
471 pr_err("%s: Can't map FIFOC\n", __func__);
472 return -ENODEV;
475 psc_fifoc_irq = irq_of_parse_and_map(np, 0);
476 of_node_put(np);
477 if (psc_fifoc_irq == NO_IRQ) {
478 pr_err("%s: Can't get FIFOC irq\n", __func__);
479 iounmap(psc_fifoc);
480 return -ENODEV;
483 for_each_compatible_node(np, NULL, "fsl,mpc5121-psc-uart") {
484 tx_fifo_size = get_fifo_size(np, "fsl,tx-fifo-size");
485 rx_fifo_size = get_fifo_size(np, "fsl,rx-fifo-size");
487 /* size in register is in 4 byte units */
488 tx_fifo_size /= 4;
489 rx_fifo_size /= 4;
490 if (!tx_fifo_size)
491 tx_fifo_size = 1;
492 if (!rx_fifo_size)
493 rx_fifo_size = 1;
495 psc = of_iomap(np, 0);
496 if (!psc) {
497 pr_err("%s: Can't map %s device\n",
498 __func__, np->full_name);
499 continue;
502 /* FIFO space is 4KiB, check if requested size is available */
503 if ((fifobase + tx_fifo_size + rx_fifo_size) > 0x1000) {
504 pr_err("%s: no fifo space available for %s\n",
505 __func__, np->full_name);
506 iounmap(psc);
508 * chances are that another device requests less
509 * fifo space, so we continue.
511 continue;
513 /* set tx and rx fifo size registers */
514 out_be32(&FIFOC(psc)->txsz, (fifobase << 16) | tx_fifo_size);
515 fifobase += tx_fifo_size;
516 out_be32(&FIFOC(psc)->rxsz, (fifobase << 16) | rx_fifo_size);
517 fifobase += rx_fifo_size;
519 /* reset and enable the slices */
520 out_be32(&FIFOC(psc)->txcmd, 0x80);
521 out_be32(&FIFOC(psc)->txcmd, 0x01);
522 out_be32(&FIFOC(psc)->rxcmd, 0x80);
523 out_be32(&FIFOC(psc)->rxcmd, 0x01);
525 iounmap(psc);
528 return 0;
531 static void __exit mpc512x_psc_fifoc_uninit(void)
533 iounmap(psc_fifoc);
536 /* 512x specific interrupt handler. The caller holds the port lock */
537 static irqreturn_t mpc512x_psc_handle_irq(struct uart_port *port)
539 unsigned long fifoc_int;
540 int psc_num;
542 /* Read pending PSC FIFOC interrupts */
543 fifoc_int = in_be32(&psc_fifoc->fifoc_int);
545 /* Check if it is an interrupt for this port */
546 psc_num = (port->mapbase & 0xf00) >> 8;
547 if (test_bit(psc_num, &fifoc_int) ||
548 test_bit(psc_num + 16, &fifoc_int))
549 return mpc5xxx_uart_process_int(port);
551 return IRQ_NONE;
554 static int mpc512x_psc_clock(struct uart_port *port, int enable)
556 struct clk *psc_clk;
557 int psc_num;
558 char clk_name[10];
560 if (uart_console(port))
561 return 0;
563 psc_num = (port->mapbase & 0xf00) >> 8;
564 snprintf(clk_name, sizeof(clk_name), "psc%d_clk", psc_num);
565 psc_clk = clk_get(port->dev, clk_name);
566 if (IS_ERR(psc_clk)) {
567 dev_err(port->dev, "Failed to get PSC clock entry!\n");
568 return -ENODEV;
571 dev_dbg(port->dev, "%s %sable\n", clk_name, enable ? "en" : "dis");
573 if (enable)
574 clk_enable(psc_clk);
575 else
576 clk_disable(psc_clk);
578 return 0;
581 static void mpc512x_psc_get_irq(struct uart_port *port, struct device_node *np)
583 port->irqflags = IRQF_SHARED;
584 port->irq = psc_fifoc_irq;
587 static struct psc_ops mpc512x_psc_ops = {
588 .fifo_init = mpc512x_psc_fifo_init,
589 .raw_rx_rdy = mpc512x_psc_raw_rx_rdy,
590 .raw_tx_rdy = mpc512x_psc_raw_tx_rdy,
591 .rx_rdy = mpc512x_psc_rx_rdy,
592 .tx_rdy = mpc512x_psc_tx_rdy,
593 .tx_empty = mpc512x_psc_tx_empty,
594 .stop_rx = mpc512x_psc_stop_rx,
595 .start_tx = mpc512x_psc_start_tx,
596 .stop_tx = mpc512x_psc_stop_tx,
597 .rx_clr_irq = mpc512x_psc_rx_clr_irq,
598 .tx_clr_irq = mpc512x_psc_tx_clr_irq,
599 .write_char = mpc512x_psc_write_char,
600 .read_char = mpc512x_psc_read_char,
601 .cw_disable_ints = mpc512x_psc_cw_disable_ints,
602 .cw_restore_ints = mpc512x_psc_cw_restore_ints,
603 .getuartclk = mpc512x_getuartclk,
604 .clock = mpc512x_psc_clock,
605 .fifoc_init = mpc512x_psc_fifoc_init,
606 .fifoc_uninit = mpc512x_psc_fifoc_uninit,
607 .get_irq = mpc512x_psc_get_irq,
608 .handle_irq = mpc512x_psc_handle_irq,
610 #endif
612 static struct psc_ops *psc_ops;
614 /* ======================================================================== */
615 /* UART operations */
616 /* ======================================================================== */
618 static unsigned int
619 mpc52xx_uart_tx_empty(struct uart_port *port)
621 return psc_ops->tx_empty(port) ? TIOCSER_TEMT : 0;
624 static void
625 mpc52xx_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
627 if (mctrl & TIOCM_RTS)
628 out_8(&PSC(port)->op1, MPC52xx_PSC_OP_RTS);
629 else
630 out_8(&PSC(port)->op0, MPC52xx_PSC_OP_RTS);
633 static unsigned int
634 mpc52xx_uart_get_mctrl(struct uart_port *port)
636 unsigned int ret = TIOCM_DSR;
637 u8 status = in_8(&PSC(port)->mpc52xx_psc_ipcr);
639 if (!(status & MPC52xx_PSC_CTS))
640 ret |= TIOCM_CTS;
641 if (!(status & MPC52xx_PSC_DCD))
642 ret |= TIOCM_CAR;
644 return ret;
647 static void
648 mpc52xx_uart_stop_tx(struct uart_port *port)
650 /* port->lock taken by caller */
651 psc_ops->stop_tx(port);
654 static void
655 mpc52xx_uart_start_tx(struct uart_port *port)
657 /* port->lock taken by caller */
658 psc_ops->start_tx(port);
661 static void
662 mpc52xx_uart_send_xchar(struct uart_port *port, char ch)
664 unsigned long flags;
665 spin_lock_irqsave(&port->lock, flags);
667 port->x_char = ch;
668 if (ch) {
669 /* Make sure tx interrupts are on */
670 /* Truly necessary ??? They should be anyway */
671 psc_ops->start_tx(port);
674 spin_unlock_irqrestore(&port->lock, flags);
677 static void
678 mpc52xx_uart_stop_rx(struct uart_port *port)
680 /* port->lock taken by caller */
681 psc_ops->stop_rx(port);
684 static void
685 mpc52xx_uart_enable_ms(struct uart_port *port)
687 struct mpc52xx_psc __iomem *psc = PSC(port);
689 /* clear D_*-bits by reading them */
690 in_8(&psc->mpc52xx_psc_ipcr);
691 /* enable CTS and DCD as IPC interrupts */
692 out_8(&psc->mpc52xx_psc_acr, MPC52xx_PSC_IEC_CTS | MPC52xx_PSC_IEC_DCD);
694 port->read_status_mask |= MPC52xx_PSC_IMR_IPC;
695 out_be16(&psc->mpc52xx_psc_imr, port->read_status_mask);
698 static void
699 mpc52xx_uart_break_ctl(struct uart_port *port, int ctl)
701 unsigned long flags;
702 spin_lock_irqsave(&port->lock, flags);
704 if (ctl == -1)
705 out_8(&PSC(port)->command, MPC52xx_PSC_START_BRK);
706 else
707 out_8(&PSC(port)->command, MPC52xx_PSC_STOP_BRK);
709 spin_unlock_irqrestore(&port->lock, flags);
712 static int
713 mpc52xx_uart_startup(struct uart_port *port)
715 struct mpc52xx_psc __iomem *psc = PSC(port);
716 int ret;
718 if (psc_ops->clock) {
719 ret = psc_ops->clock(port, 1);
720 if (ret)
721 return ret;
724 /* Request IRQ */
725 ret = request_irq(port->irq, mpc52xx_uart_int,
726 port->irqflags, "mpc52xx_psc_uart", port);
727 if (ret)
728 return ret;
730 /* Reset/activate the port, clear and enable interrupts */
731 out_8(&psc->command, MPC52xx_PSC_RST_RX);
732 out_8(&psc->command, MPC52xx_PSC_RST_TX);
734 out_be32(&psc->sicr, 0); /* UART mode DCD ignored */
736 psc_ops->fifo_init(port);
738 out_8(&psc->command, MPC52xx_PSC_TX_ENABLE);
739 out_8(&psc->command, MPC52xx_PSC_RX_ENABLE);
741 return 0;
744 static void
745 mpc52xx_uart_shutdown(struct uart_port *port)
747 struct mpc52xx_psc __iomem *psc = PSC(port);
749 /* Shut down the port. Leave TX active if on a console port */
750 out_8(&psc->command, MPC52xx_PSC_RST_RX);
751 if (!uart_console(port))
752 out_8(&psc->command, MPC52xx_PSC_RST_TX);
754 port->read_status_mask = 0;
755 out_be16(&psc->mpc52xx_psc_imr, port->read_status_mask);
757 if (psc_ops->clock)
758 psc_ops->clock(port, 0);
760 /* Release interrupt */
761 free_irq(port->irq, port);
764 static void
765 mpc52xx_uart_set_termios(struct uart_port *port, struct ktermios *new,
766 struct ktermios *old)
768 struct mpc52xx_psc __iomem *psc = PSC(port);
769 unsigned long flags;
770 unsigned char mr1, mr2;
771 unsigned short ctr;
772 unsigned int j, baud, quot;
774 /* Prepare what we're gonna write */
775 mr1 = 0;
777 switch (new->c_cflag & CSIZE) {
778 case CS5: mr1 |= MPC52xx_PSC_MODE_5_BITS;
779 break;
780 case CS6: mr1 |= MPC52xx_PSC_MODE_6_BITS;
781 break;
782 case CS7: mr1 |= MPC52xx_PSC_MODE_7_BITS;
783 break;
784 case CS8:
785 default: mr1 |= MPC52xx_PSC_MODE_8_BITS;
788 if (new->c_cflag & PARENB) {
789 mr1 |= (new->c_cflag & PARODD) ?
790 MPC52xx_PSC_MODE_PARODD : MPC52xx_PSC_MODE_PAREVEN;
791 } else
792 mr1 |= MPC52xx_PSC_MODE_PARNONE;
795 mr2 = 0;
797 if (new->c_cflag & CSTOPB)
798 mr2 |= MPC52xx_PSC_MODE_TWO_STOP;
799 else
800 mr2 |= ((new->c_cflag & CSIZE) == CS5) ?
801 MPC52xx_PSC_MODE_ONE_STOP_5_BITS :
802 MPC52xx_PSC_MODE_ONE_STOP;
804 if (new->c_cflag & CRTSCTS) {
805 mr1 |= MPC52xx_PSC_MODE_RXRTS;
806 mr2 |= MPC52xx_PSC_MODE_TXCTS;
809 baud = uart_get_baud_rate(port, new, old, 0, port->uartclk/16);
810 quot = uart_get_divisor(port, baud);
811 ctr = quot & 0xffff;
813 /* Get the lock */
814 spin_lock_irqsave(&port->lock, flags);
816 /* Update the per-port timeout */
817 uart_update_timeout(port, new->c_cflag, baud);
819 /* Do our best to flush TX & RX, so we don't lose anything */
820 /* But we don't wait indefinitely ! */
821 j = 5000000; /* Maximum wait */
822 /* FIXME Can't receive chars since set_termios might be called at early
823 * boot for the console, all stuff is not yet ready to receive at that
824 * time and that just makes the kernel oops */
825 /* while (j-- && mpc52xx_uart_int_rx_chars(port)); */
826 while (!mpc52xx_uart_tx_empty(port) && --j)
827 udelay(1);
829 if (!j)
830 printk(KERN_ERR "mpc52xx_uart.c: "
831 "Unable to flush RX & TX fifos in-time in set_termios."
832 "Some chars may have been lost.\n");
834 /* Reset the TX & RX */
835 out_8(&psc->command, MPC52xx_PSC_RST_RX);
836 out_8(&psc->command, MPC52xx_PSC_RST_TX);
838 /* Send new mode settings */
839 out_8(&psc->command, MPC52xx_PSC_SEL_MODE_REG_1);
840 out_8(&psc->mode, mr1);
841 out_8(&psc->mode, mr2);
842 out_8(&psc->ctur, ctr >> 8);
843 out_8(&psc->ctlr, ctr & 0xff);
845 if (UART_ENABLE_MS(port, new->c_cflag))
846 mpc52xx_uart_enable_ms(port);
848 /* Reenable TX & RX */
849 out_8(&psc->command, MPC52xx_PSC_TX_ENABLE);
850 out_8(&psc->command, MPC52xx_PSC_RX_ENABLE);
852 /* We're all set, release the lock */
853 spin_unlock_irqrestore(&port->lock, flags);
856 static const char *
857 mpc52xx_uart_type(struct uart_port *port)
859 return port->type == PORT_MPC52xx ? "MPC52xx PSC" : NULL;
862 static void
863 mpc52xx_uart_release_port(struct uart_port *port)
865 /* remapped by us ? */
866 if (port->flags & UPF_IOREMAP) {
867 iounmap(port->membase);
868 port->membase = NULL;
871 release_mem_region(port->mapbase, sizeof(struct mpc52xx_psc));
874 static int
875 mpc52xx_uart_request_port(struct uart_port *port)
877 int err;
879 if (port->flags & UPF_IOREMAP) /* Need to remap ? */
880 port->membase = ioremap(port->mapbase,
881 sizeof(struct mpc52xx_psc));
883 if (!port->membase)
884 return -EINVAL;
886 err = request_mem_region(port->mapbase, sizeof(struct mpc52xx_psc),
887 "mpc52xx_psc_uart") != NULL ? 0 : -EBUSY;
889 if (err && (port->flags & UPF_IOREMAP)) {
890 iounmap(port->membase);
891 port->membase = NULL;
894 return err;
897 static void
898 mpc52xx_uart_config_port(struct uart_port *port, int flags)
900 if ((flags & UART_CONFIG_TYPE)
901 && (mpc52xx_uart_request_port(port) == 0))
902 port->type = PORT_MPC52xx;
905 static int
906 mpc52xx_uart_verify_port(struct uart_port *port, struct serial_struct *ser)
908 if (ser->type != PORT_UNKNOWN && ser->type != PORT_MPC52xx)
909 return -EINVAL;
911 if ((ser->irq != port->irq) ||
912 (ser->io_type != UPIO_MEM) ||
913 (ser->baud_base != port->uartclk) ||
914 (ser->iomem_base != (void *)port->mapbase) ||
915 (ser->hub6 != 0))
916 return -EINVAL;
918 return 0;
922 static struct uart_ops mpc52xx_uart_ops = {
923 .tx_empty = mpc52xx_uart_tx_empty,
924 .set_mctrl = mpc52xx_uart_set_mctrl,
925 .get_mctrl = mpc52xx_uart_get_mctrl,
926 .stop_tx = mpc52xx_uart_stop_tx,
927 .start_tx = mpc52xx_uart_start_tx,
928 .send_xchar = mpc52xx_uart_send_xchar,
929 .stop_rx = mpc52xx_uart_stop_rx,
930 .enable_ms = mpc52xx_uart_enable_ms,
931 .break_ctl = mpc52xx_uart_break_ctl,
932 .startup = mpc52xx_uart_startup,
933 .shutdown = mpc52xx_uart_shutdown,
934 .set_termios = mpc52xx_uart_set_termios,
935 /* .pm = mpc52xx_uart_pm, Not supported yet */
936 /* .set_wake = mpc52xx_uart_set_wake, Not supported yet */
937 .type = mpc52xx_uart_type,
938 .release_port = mpc52xx_uart_release_port,
939 .request_port = mpc52xx_uart_request_port,
940 .config_port = mpc52xx_uart_config_port,
941 .verify_port = mpc52xx_uart_verify_port
945 /* ======================================================================== */
946 /* Interrupt handling */
947 /* ======================================================================== */
949 static inline int
950 mpc52xx_uart_int_rx_chars(struct uart_port *port)
952 struct tty_struct *tty = port->state->port.tty;
953 unsigned char ch, flag;
954 unsigned short status;
956 /* While we can read, do so ! */
957 while (psc_ops->raw_rx_rdy(port)) {
958 /* Get the char */
959 ch = psc_ops->read_char(port);
961 /* Handle sysreq char */
962 #ifdef SUPPORT_SYSRQ
963 if (uart_handle_sysrq_char(port, ch)) {
964 port->sysrq = 0;
965 continue;
967 #endif
969 /* Store it */
971 flag = TTY_NORMAL;
972 port->icount.rx++;
974 status = in_be16(&PSC(port)->mpc52xx_psc_status);
976 if (status & (MPC52xx_PSC_SR_PE |
977 MPC52xx_PSC_SR_FE |
978 MPC52xx_PSC_SR_RB)) {
980 if (status & MPC52xx_PSC_SR_RB) {
981 flag = TTY_BREAK;
982 uart_handle_break(port);
983 port->icount.brk++;
984 } else if (status & MPC52xx_PSC_SR_PE) {
985 flag = TTY_PARITY;
986 port->icount.parity++;
988 else if (status & MPC52xx_PSC_SR_FE) {
989 flag = TTY_FRAME;
990 port->icount.frame++;
993 /* Clear error condition */
994 out_8(&PSC(port)->command, MPC52xx_PSC_RST_ERR_STAT);
997 tty_insert_flip_char(tty, ch, flag);
998 if (status & MPC52xx_PSC_SR_OE) {
1000 * Overrun is special, since it's
1001 * reported immediately, and doesn't
1002 * affect the current character
1004 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
1005 port->icount.overrun++;
1009 spin_unlock(&port->lock);
1010 tty_flip_buffer_push(tty);
1011 spin_lock(&port->lock);
1013 return psc_ops->raw_rx_rdy(port);
1016 static inline int
1017 mpc52xx_uart_int_tx_chars(struct uart_port *port)
1019 struct circ_buf *xmit = &port->state->xmit;
1021 /* Process out of band chars */
1022 if (port->x_char) {
1023 psc_ops->write_char(port, port->x_char);
1024 port->icount.tx++;
1025 port->x_char = 0;
1026 return 1;
1029 /* Nothing to do ? */
1030 if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
1031 mpc52xx_uart_stop_tx(port);
1032 return 0;
1035 /* Send chars */
1036 while (psc_ops->raw_tx_rdy(port)) {
1037 psc_ops->write_char(port, xmit->buf[xmit->tail]);
1038 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
1039 port->icount.tx++;
1040 if (uart_circ_empty(xmit))
1041 break;
1044 /* Wake up */
1045 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
1046 uart_write_wakeup(port);
1048 /* Maybe we're done after all */
1049 if (uart_circ_empty(xmit)) {
1050 mpc52xx_uart_stop_tx(port);
1051 return 0;
1054 return 1;
1057 static irqreturn_t
1058 mpc5xxx_uart_process_int(struct uart_port *port)
1060 unsigned long pass = ISR_PASS_LIMIT;
1061 unsigned int keepgoing;
1062 u8 status;
1064 /* While we have stuff to do, we continue */
1065 do {
1066 /* If we don't find anything to do, we stop */
1067 keepgoing = 0;
1069 psc_ops->rx_clr_irq(port);
1070 if (psc_ops->rx_rdy(port))
1071 keepgoing |= mpc52xx_uart_int_rx_chars(port);
1073 psc_ops->tx_clr_irq(port);
1074 if (psc_ops->tx_rdy(port))
1075 keepgoing |= mpc52xx_uart_int_tx_chars(port);
1077 status = in_8(&PSC(port)->mpc52xx_psc_ipcr);
1078 if (status & MPC52xx_PSC_D_DCD)
1079 uart_handle_dcd_change(port, !(status & MPC52xx_PSC_DCD));
1081 if (status & MPC52xx_PSC_D_CTS)
1082 uart_handle_cts_change(port, !(status & MPC52xx_PSC_CTS));
1084 /* Limit number of iteration */
1085 if (!(--pass))
1086 keepgoing = 0;
1088 } while (keepgoing);
1090 return IRQ_HANDLED;
1093 static irqreturn_t
1094 mpc52xx_uart_int(int irq, void *dev_id)
1096 struct uart_port *port = dev_id;
1097 irqreturn_t ret;
1099 spin_lock(&port->lock);
1101 ret = psc_ops->handle_irq(port);
1103 spin_unlock(&port->lock);
1105 return ret;
1108 /* ======================================================================== */
1109 /* Console ( if applicable ) */
1110 /* ======================================================================== */
1112 #ifdef CONFIG_SERIAL_MPC52xx_CONSOLE
1114 static void __init
1115 mpc52xx_console_get_options(struct uart_port *port,
1116 int *baud, int *parity, int *bits, int *flow)
1118 struct mpc52xx_psc __iomem *psc = PSC(port);
1119 unsigned char mr1;
1121 pr_debug("mpc52xx_console_get_options(port=%p)\n", port);
1123 /* Read the mode registers */
1124 out_8(&psc->command, MPC52xx_PSC_SEL_MODE_REG_1);
1125 mr1 = in_8(&psc->mode);
1127 /* CT{U,L}R are write-only ! */
1128 *baud = CONFIG_SERIAL_MPC52xx_CONSOLE_BAUD;
1130 /* Parse them */
1131 switch (mr1 & MPC52xx_PSC_MODE_BITS_MASK) {
1132 case MPC52xx_PSC_MODE_5_BITS:
1133 *bits = 5;
1134 break;
1135 case MPC52xx_PSC_MODE_6_BITS:
1136 *bits = 6;
1137 break;
1138 case MPC52xx_PSC_MODE_7_BITS:
1139 *bits = 7;
1140 break;
1141 case MPC52xx_PSC_MODE_8_BITS:
1142 default:
1143 *bits = 8;
1146 if (mr1 & MPC52xx_PSC_MODE_PARNONE)
1147 *parity = 'n';
1148 else
1149 *parity = mr1 & MPC52xx_PSC_MODE_PARODD ? 'o' : 'e';
1152 static void
1153 mpc52xx_console_write(struct console *co, const char *s, unsigned int count)
1155 struct uart_port *port = &mpc52xx_uart_ports[co->index];
1156 unsigned int i, j;
1158 /* Disable interrupts */
1159 psc_ops->cw_disable_ints(port);
1161 /* Wait the TX buffer to be empty */
1162 j = 5000000; /* Maximum wait */
1163 while (!mpc52xx_uart_tx_empty(port) && --j)
1164 udelay(1);
1166 /* Write all the chars */
1167 for (i = 0; i < count; i++, s++) {
1168 /* Line return handling */
1169 if (*s == '\n')
1170 psc_ops->write_char(port, '\r');
1172 /* Send the char */
1173 psc_ops->write_char(port, *s);
1175 /* Wait the TX buffer to be empty */
1176 j = 20000; /* Maximum wait */
1177 while (!mpc52xx_uart_tx_empty(port) && --j)
1178 udelay(1);
1181 /* Restore interrupt state */
1182 psc_ops->cw_restore_ints(port);
1186 static int __init
1187 mpc52xx_console_setup(struct console *co, char *options)
1189 struct uart_port *port = &mpc52xx_uart_ports[co->index];
1190 struct device_node *np = mpc52xx_uart_nodes[co->index];
1191 unsigned int uartclk;
1192 struct resource res;
1193 int ret;
1195 int baud = CONFIG_SERIAL_MPC52xx_CONSOLE_BAUD;
1196 int bits = 8;
1197 int parity = 'n';
1198 int flow = 'n';
1200 pr_debug("mpc52xx_console_setup co=%p, co->index=%i, options=%s\n",
1201 co, co->index, options);
1203 if ((co->index < 0) || (co->index >= MPC52xx_PSC_MAXNUM)) {
1204 pr_debug("PSC%x out of range\n", co->index);
1205 return -EINVAL;
1208 if (!np) {
1209 pr_debug("PSC%x not found in device tree\n", co->index);
1210 return -EINVAL;
1213 pr_debug("Console on ttyPSC%x is %s\n",
1214 co->index, mpc52xx_uart_nodes[co->index]->full_name);
1216 /* Fetch register locations */
1217 ret = of_address_to_resource(np, 0, &res);
1218 if (ret) {
1219 pr_debug("Could not get resources for PSC%x\n", co->index);
1220 return ret;
1223 uartclk = psc_ops->getuartclk(np);
1224 if (uartclk == 0) {
1225 pr_debug("Could not find uart clock frequency!\n");
1226 return -EINVAL;
1229 /* Basic port init. Needed since we use some uart_??? func before
1230 * real init for early access */
1231 spin_lock_init(&port->lock);
1232 port->uartclk = uartclk;
1233 port->ops = &mpc52xx_uart_ops;
1234 port->mapbase = res.start;
1235 port->membase = ioremap(res.start, sizeof(struct mpc52xx_psc));
1236 port->irq = irq_of_parse_and_map(np, 0);
1238 if (port->membase == NULL)
1239 return -EINVAL;
1241 pr_debug("mpc52xx-psc uart at %p, mapped to %p, irq=%x, freq=%i\n",
1242 (void *)port->mapbase, port->membase,
1243 port->irq, port->uartclk);
1245 /* Setup the port parameters accoding to options */
1246 if (options)
1247 uart_parse_options(options, &baud, &parity, &bits, &flow);
1248 else
1249 mpc52xx_console_get_options(port, &baud, &parity, &bits, &flow);
1251 pr_debug("Setting console parameters: %i %i%c1 flow=%c\n",
1252 baud, bits, parity, flow);
1254 return uart_set_options(port, co, baud, parity, bits, flow);
1258 static struct uart_driver mpc52xx_uart_driver;
1260 static struct console mpc52xx_console = {
1261 .name = "ttyPSC",
1262 .write = mpc52xx_console_write,
1263 .device = uart_console_device,
1264 .setup = mpc52xx_console_setup,
1265 .flags = CON_PRINTBUFFER,
1266 .index = -1, /* Specified on the cmdline (e.g. console=ttyPSC0) */
1267 .data = &mpc52xx_uart_driver,
1271 static int __init
1272 mpc52xx_console_init(void)
1274 mpc52xx_uart_of_enumerate();
1275 register_console(&mpc52xx_console);
1276 return 0;
1279 console_initcall(mpc52xx_console_init);
1281 #define MPC52xx_PSC_CONSOLE &mpc52xx_console
1282 #else
1283 #define MPC52xx_PSC_CONSOLE NULL
1284 #endif
1287 /* ======================================================================== */
1288 /* UART Driver */
1289 /* ======================================================================== */
1291 static struct uart_driver mpc52xx_uart_driver = {
1292 .driver_name = "mpc52xx_psc_uart",
1293 .dev_name = "ttyPSC",
1294 .major = SERIAL_PSC_MAJOR,
1295 .minor = SERIAL_PSC_MINOR,
1296 .nr = MPC52xx_PSC_MAXNUM,
1297 .cons = MPC52xx_PSC_CONSOLE,
1300 /* ======================================================================== */
1301 /* OF Platform Driver */
1302 /* ======================================================================== */
1304 static struct of_device_id mpc52xx_uart_of_match[] = {
1305 #ifdef CONFIG_PPC_MPC52xx
1306 { .compatible = "fsl,mpc5200-psc-uart", .data = &mpc52xx_psc_ops, },
1307 /* binding used by old lite5200 device trees: */
1308 { .compatible = "mpc5200-psc-uart", .data = &mpc52xx_psc_ops, },
1309 /* binding used by efika: */
1310 { .compatible = "mpc5200-serial", .data = &mpc52xx_psc_ops, },
1311 #endif
1312 #ifdef CONFIG_PPC_MPC512x
1313 { .compatible = "fsl,mpc5121-psc-uart", .data = &mpc512x_psc_ops, },
1314 #endif
1318 static int __devinit
1319 mpc52xx_uart_of_probe(struct of_device *op, const struct of_device_id *match)
1321 int idx = -1;
1322 unsigned int uartclk;
1323 struct uart_port *port = NULL;
1324 struct resource res;
1325 int ret;
1327 dev_dbg(&op->dev, "mpc52xx_uart_probe(op=%p, match=%p)\n", op, match);
1329 /* Check validity & presence */
1330 for (idx = 0; idx < MPC52xx_PSC_MAXNUM; idx++)
1331 if (mpc52xx_uart_nodes[idx] == op->node)
1332 break;
1333 if (idx >= MPC52xx_PSC_MAXNUM)
1334 return -EINVAL;
1335 pr_debug("Found %s assigned to ttyPSC%x\n",
1336 mpc52xx_uart_nodes[idx]->full_name, idx);
1338 uartclk = psc_ops->getuartclk(op->node);
1339 if (uartclk == 0) {
1340 dev_dbg(&op->dev, "Could not find uart clock frequency!\n");
1341 return -EINVAL;
1344 /* Init the port structure */
1345 port = &mpc52xx_uart_ports[idx];
1347 spin_lock_init(&port->lock);
1348 port->uartclk = uartclk;
1349 port->fifosize = 512;
1350 port->iotype = UPIO_MEM;
1351 port->flags = UPF_BOOT_AUTOCONF |
1352 (uart_console(port) ? 0 : UPF_IOREMAP);
1353 port->line = idx;
1354 port->ops = &mpc52xx_uart_ops;
1355 port->dev = &op->dev;
1357 /* Search for IRQ and mapbase */
1358 ret = of_address_to_resource(op->node, 0, &res);
1359 if (ret)
1360 return ret;
1362 port->mapbase = res.start;
1363 if (!port->mapbase) {
1364 dev_dbg(&op->dev, "Could not allocate resources for PSC\n");
1365 return -EINVAL;
1368 psc_ops->get_irq(port, op->node);
1369 if (port->irq == NO_IRQ) {
1370 dev_dbg(&op->dev, "Could not get irq\n");
1371 return -EINVAL;
1374 dev_dbg(&op->dev, "mpc52xx-psc uart at %p, irq=%x, freq=%i\n",
1375 (void *)port->mapbase, port->irq, port->uartclk);
1377 /* Add the port to the uart sub-system */
1378 ret = uart_add_one_port(&mpc52xx_uart_driver, port);
1379 if (ret)
1380 return ret;
1382 dev_set_drvdata(&op->dev, (void *)port);
1383 return 0;
1386 static int
1387 mpc52xx_uart_of_remove(struct of_device *op)
1389 struct uart_port *port = dev_get_drvdata(&op->dev);
1390 dev_set_drvdata(&op->dev, NULL);
1392 if (port)
1393 uart_remove_one_port(&mpc52xx_uart_driver, port);
1395 return 0;
1398 #ifdef CONFIG_PM
1399 static int
1400 mpc52xx_uart_of_suspend(struct of_device *op, pm_message_t state)
1402 struct uart_port *port = (struct uart_port *) dev_get_drvdata(&op->dev);
1404 if (port)
1405 uart_suspend_port(&mpc52xx_uart_driver, port);
1407 return 0;
1410 static int
1411 mpc52xx_uart_of_resume(struct of_device *op)
1413 struct uart_port *port = (struct uart_port *) dev_get_drvdata(&op->dev);
1415 if (port)
1416 uart_resume_port(&mpc52xx_uart_driver, port);
1418 return 0;
1420 #endif
1422 static void
1423 mpc52xx_uart_of_assign(struct device_node *np)
1425 int i;
1427 /* Find the first free PSC number */
1428 for (i = 0; i < MPC52xx_PSC_MAXNUM; i++) {
1429 if (mpc52xx_uart_nodes[i] == NULL) {
1430 of_node_get(np);
1431 mpc52xx_uart_nodes[i] = np;
1432 return;
1437 static void
1438 mpc52xx_uart_of_enumerate(void)
1440 static int enum_done;
1441 struct device_node *np;
1442 const struct of_device_id *match;
1443 int i;
1445 if (enum_done)
1446 return;
1448 /* Assign index to each PSC in device tree */
1449 for_each_matching_node(np, mpc52xx_uart_of_match) {
1450 match = of_match_node(mpc52xx_uart_of_match, np);
1451 psc_ops = match->data;
1452 mpc52xx_uart_of_assign(np);
1455 enum_done = 1;
1457 for (i = 0; i < MPC52xx_PSC_MAXNUM; i++) {
1458 if (mpc52xx_uart_nodes[i])
1459 pr_debug("%s assigned to ttyPSC%x\n",
1460 mpc52xx_uart_nodes[i]->full_name, i);
1464 MODULE_DEVICE_TABLE(of, mpc52xx_uart_of_match);
1466 static struct of_platform_driver mpc52xx_uart_of_driver = {
1467 .match_table = mpc52xx_uart_of_match,
1468 .probe = mpc52xx_uart_of_probe,
1469 .remove = mpc52xx_uart_of_remove,
1470 #ifdef CONFIG_PM
1471 .suspend = mpc52xx_uart_of_suspend,
1472 .resume = mpc52xx_uart_of_resume,
1473 #endif
1474 .driver = {
1475 .name = "mpc52xx-psc-uart",
1480 /* ======================================================================== */
1481 /* Module */
1482 /* ======================================================================== */
1484 static int __init
1485 mpc52xx_uart_init(void)
1487 int ret;
1489 printk(KERN_INFO "Serial: MPC52xx PSC UART driver\n");
1491 ret = uart_register_driver(&mpc52xx_uart_driver);
1492 if (ret) {
1493 printk(KERN_ERR "%s: uart_register_driver failed (%i)\n",
1494 __FILE__, ret);
1495 return ret;
1498 mpc52xx_uart_of_enumerate();
1501 * Map the PSC FIFO Controller and init if on MPC512x.
1503 if (psc_ops->fifoc_init) {
1504 ret = psc_ops->fifoc_init();
1505 if (ret)
1506 return ret;
1509 ret = of_register_platform_driver(&mpc52xx_uart_of_driver);
1510 if (ret) {
1511 printk(KERN_ERR "%s: of_register_platform_driver failed (%i)\n",
1512 __FILE__, ret);
1513 uart_unregister_driver(&mpc52xx_uart_driver);
1514 return ret;
1517 return 0;
1520 static void __exit
1521 mpc52xx_uart_exit(void)
1523 if (psc_ops->fifoc_uninit)
1524 psc_ops->fifoc_uninit();
1526 of_unregister_platform_driver(&mpc52xx_uart_of_driver);
1527 uart_unregister_driver(&mpc52xx_uart_driver);
1531 module_init(mpc52xx_uart_init);
1532 module_exit(mpc52xx_uart_exit);
1534 MODULE_AUTHOR("Sylvain Munaut <tnt@246tNt.com>");
1535 MODULE_DESCRIPTION("Freescale MPC52xx PSC UART");
1536 MODULE_LICENSE("GPL");