blob: 141b8da7e0e7d3beaeb527e871f5fe691a685c3f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Driver for the PSC of the Freescale MPC52xx PSCs configured as UARTs.
3 *
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.
12 *
13 *
14 * Maintainer : Sylvain Munaut <tnt@246tNt.com>
Grant Likely9b9129e2006-11-27 14:21:01 -070015 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070016 * Some of the code has been inspired/copied from the 2.4 code written
17 * by Dale Farnsworth <dfarnsworth@mvista.com>.
Grant Likely9b9129e2006-11-27 14:21:01 -070018 *
Grant Likelyb9272df2006-11-27 14:21:02 -070019 * Copyright (C) 2006 Secret Lab Technologies Ltd.
20 * Grant Likely <grant.likely@secretlab.ca>
21 * Copyright (C) 2004-2006 Sylvain Munaut <tnt@246tNt.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 * Copyright (C) 2003 MontaVista, Software, Inc.
Grant Likely9b9129e2006-11-27 14:21:01 -070023 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 * 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.
27 */
Grant Likely9b9129e2006-11-27 14:21:01 -070028
Linus Torvalds1da177e2005-04-16 15:20:36 -070029/* Platform device Usage :
30 *
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".
34 *
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.
38 *
39 * The idx field must be equal to the PSC index ( e.g. 0 for PSC1, 1 for PSC2,
Sylvain Munautd62de3a2006-01-06 00:11:32 -080040 * 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
Uwe Zeisbergerc30fe7f2006-03-24 18:23:14 +010043 * are parsing the kernel args console=ttyPSC?, we wouldn't know which PSC it
Sylvain Munautd62de3a2006-01-06 00:11:32 -080044 * will be mapped to.
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 */
46
Grant Likelyb9272df2006-11-27 14:21:02 -070047/* OF Platform device Usage :
48 *
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
52 *
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.
56 *
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.
60 */
61
62#undef DEBUG
63
64#include <linux/device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#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
71#include <asm/delay.h>
72#include <asm/io.h>
73
Grant Likelyb9272df2006-11-27 14:21:02 -070074#if defined(CONFIG_PPC_MERGE)
Grant Likely283029d2008-01-09 06:20:40 +110075#include <linux/of.h>
76#include <linux/of_platform.h>
Grant Likelyb9272df2006-11-27 14:21:02 -070077#else
78#include <linux/platform_device.h>
79#endif
80
Linus Torvalds1da177e2005-04-16 15:20:36 -070081#include <asm/mpc52xx.h>
82#include <asm/mpc52xx_psc.h>
83
84#if defined(CONFIG_SERIAL_MPC52xx_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
85#define SUPPORT_SYSRQ
86#endif
87
88#include <linux/serial_core.h>
89
90
Sylvain Munautd62de3a2006-01-06 00:11:32 -080091/* We've been assigned a range on the "Low-density serial ports" major */
92#define SERIAL_PSC_MAJOR 204
93#define SERIAL_PSC_MINOR 148
94
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
96#define ISR_PASS_LIMIT 256 /* Max number of iteration in the interrupt */
97
98
99static struct uart_port mpc52xx_uart_ports[MPC52xx_PSC_MAXNUM];
100 /* Rem: - We use the read_status_mask as a shadow of
101 * psc->mpc52xx_psc_imr
102 * - It's important that is array is all zero on start as we
103 * use it to know if it's initialized or not ! If it's not sure
104 * it's cleared, then a memset(...,0,...) should be added to
105 * the console_init
106 */
Grant Likelyb9272df2006-11-27 14:21:02 -0700107#if defined(CONFIG_PPC_MERGE)
108/* lookup table for matching device nodes to index numbers */
109static struct device_node *mpc52xx_uart_nodes[MPC52xx_PSC_MAXNUM];
110
111static void mpc52xx_uart_of_enumerate(void);
112#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
114#define PSC(port) ((struct mpc52xx_psc __iomem *)((port)->membase))
John Rigby94f38942008-01-17 08:37:24 +1100115#define FIFO(port) ((struct mpc52xx_psc_fifo __iomem *)(PSC(port)+1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
117
118/* Forward declaration of the interruption handling routine */
David Howells7d12e782006-10-05 14:55:46 +0100119static irqreturn_t mpc52xx_uart_int(int irq,void *dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
121
122/* Simple macro to test if a port is console or not. This one is taken
123 * for serial_core.c and maybe should be moved to serial_core.h ? */
124#ifdef CONFIG_SERIAL_CORE_CONSOLE
125#define uart_console(port) ((port)->cons && (port)->cons->index == (port)->line)
126#else
127#define uart_console(port) (0)
128#endif
129
Grant Likelyb9272df2006-11-27 14:21:02 -0700130#if defined(CONFIG_PPC_MERGE)
131static struct of_device_id mpc52xx_uart_of_match[] = {
Grant Likelye3aba812007-02-12 13:36:55 -0700132 { .type = "serial", .compatible = "mpc5200-psc-uart", },
Grant Likelyb9272df2006-11-27 14:21:02 -0700133 {},
134};
135#endif
136
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
138/* ======================================================================== */
139/* UART operations */
140/* ======================================================================== */
141
Grant Likely9b9129e2006-11-27 14:21:01 -0700142static unsigned int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143mpc52xx_uart_tx_empty(struct uart_port *port)
144{
145 int status = in_be16(&PSC(port)->mpc52xx_psc_status);
146 return (status & MPC52xx_PSC_SR_TXEMP) ? TIOCSER_TEMT : 0;
147}
148
Grant Likely9b9129e2006-11-27 14:21:01 -0700149static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150mpc52xx_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
151{
152 /* Not implemented */
153}
154
Grant Likely9b9129e2006-11-27 14:21:01 -0700155static unsigned int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156mpc52xx_uart_get_mctrl(struct uart_port *port)
157{
158 /* Not implemented */
159 return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
160}
161
Grant Likely9b9129e2006-11-27 14:21:01 -0700162static void
Russell Kingb129a8c2005-08-31 10:12:14 +0100163mpc52xx_uart_stop_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164{
165 /* port->lock taken by caller */
166 port->read_status_mask &= ~MPC52xx_PSC_IMR_TXRDY;
167 out_be16(&PSC(port)->mpc52xx_psc_imr,port->read_status_mask);
168}
169
Grant Likely9b9129e2006-11-27 14:21:01 -0700170static void
Russell Kingb129a8c2005-08-31 10:12:14 +0100171mpc52xx_uart_start_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172{
173 /* port->lock taken by caller */
174 port->read_status_mask |= MPC52xx_PSC_IMR_TXRDY;
175 out_be16(&PSC(port)->mpc52xx_psc_imr,port->read_status_mask);
176}
177
Grant Likely9b9129e2006-11-27 14:21:01 -0700178static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179mpc52xx_uart_send_xchar(struct uart_port *port, char ch)
180{
181 unsigned long flags;
182 spin_lock_irqsave(&port->lock, flags);
Grant Likely9b9129e2006-11-27 14:21:01 -0700183
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 port->x_char = ch;
185 if (ch) {
186 /* Make sure tx interrupts are on */
187 /* Truly necessary ??? They should be anyway */
188 port->read_status_mask |= MPC52xx_PSC_IMR_TXRDY;
189 out_be16(&PSC(port)->mpc52xx_psc_imr,port->read_status_mask);
190 }
Grant Likely9b9129e2006-11-27 14:21:01 -0700191
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 spin_unlock_irqrestore(&port->lock, flags);
193}
194
195static void
196mpc52xx_uart_stop_rx(struct uart_port *port)
197{
198 /* port->lock taken by caller */
199 port->read_status_mask &= ~MPC52xx_PSC_IMR_RXRDY;
200 out_be16(&PSC(port)->mpc52xx_psc_imr,port->read_status_mask);
201}
202
203static void
204mpc52xx_uart_enable_ms(struct uart_port *port)
205{
206 /* Not implemented */
207}
208
209static void
210mpc52xx_uart_break_ctl(struct uart_port *port, int ctl)
211{
212 unsigned long flags;
213 spin_lock_irqsave(&port->lock, flags);
214
215 if ( ctl == -1 )
216 out_8(&PSC(port)->command,MPC52xx_PSC_START_BRK);
217 else
218 out_8(&PSC(port)->command,MPC52xx_PSC_STOP_BRK);
Grant Likely9b9129e2006-11-27 14:21:01 -0700219
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 spin_unlock_irqrestore(&port->lock, flags);
221}
222
223static int
224mpc52xx_uart_startup(struct uart_port *port)
225{
226 struct mpc52xx_psc __iomem *psc = PSC(port);
John Rigby94f38942008-01-17 08:37:24 +1100227 struct mpc52xx_psc_fifo __iomem *fifo = FIFO(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 int ret;
229
230 /* Request IRQ */
231 ret = request_irq(port->irq, mpc52xx_uart_int,
Thomas Gleixner40663cc2006-07-01 19:29:43 -0700232 IRQF_DISABLED | IRQF_SAMPLE_RANDOM, "mpc52xx_psc_uart", port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 if (ret)
234 return ret;
235
236 /* Reset/activate the port, clear and enable interrupts */
237 out_8(&psc->command,MPC52xx_PSC_RST_RX);
238 out_8(&psc->command,MPC52xx_PSC_RST_TX);
Grant Likely9b9129e2006-11-27 14:21:01 -0700239
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 out_be32(&psc->sicr,0); /* UART mode DCD ignored */
241
242 out_be16(&psc->mpc52xx_psc_clock_select, 0xdd00); /* /16 prescaler on */
Grant Likely9b9129e2006-11-27 14:21:01 -0700243
John Rigby94f38942008-01-17 08:37:24 +1100244 out_8(&fifo->rfcntl, 0x00);
245 out_be16(&fifo->rfalarm, 0x1ff);
246 out_8(&fifo->tfcntl, 0x07);
247 out_be16(&fifo->tfalarm, 0x80);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
249 port->read_status_mask |= MPC52xx_PSC_IMR_RXRDY | MPC52xx_PSC_IMR_TXRDY;
250 out_be16(&psc->mpc52xx_psc_imr,port->read_status_mask);
Grant Likely9b9129e2006-11-27 14:21:01 -0700251
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 out_8(&psc->command,MPC52xx_PSC_TX_ENABLE);
253 out_8(&psc->command,MPC52xx_PSC_RX_ENABLE);
Grant Likely9b9129e2006-11-27 14:21:01 -0700254
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 return 0;
256}
257
258static void
259mpc52xx_uart_shutdown(struct uart_port *port)
260{
261 struct mpc52xx_psc __iomem *psc = PSC(port);
Grant Likely9b9129e2006-11-27 14:21:01 -0700262
Grant Likelya3481192007-05-07 01:38:51 +1000263 /* Shut down the port. Leave TX active if on a console port */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 out_8(&psc->command,MPC52xx_PSC_RST_RX);
Grant Likelya3481192007-05-07 01:38:51 +1000265 if (!uart_console(port))
266 out_8(&psc->command,MPC52xx_PSC_RST_TX);
Grant Likely9b9129e2006-11-27 14:21:01 -0700267
268 port->read_status_mask = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 out_be16(&psc->mpc52xx_psc_imr,port->read_status_mask);
270
271 /* Release interrupt */
272 free_irq(port->irq, port);
273}
274
Grant Likely9b9129e2006-11-27 14:21:01 -0700275static void
Alan Cox606d0992006-12-08 02:38:45 -0800276mpc52xx_uart_set_termios(struct uart_port *port, struct ktermios *new,
277 struct ktermios *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278{
279 struct mpc52xx_psc __iomem *psc = PSC(port);
280 unsigned long flags;
281 unsigned char mr1, mr2;
282 unsigned short ctr;
283 unsigned int j, baud, quot;
Grant Likely9b9129e2006-11-27 14:21:01 -0700284
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 /* Prepare what we're gonna write */
286 mr1 = 0;
Grant Likely9b9129e2006-11-27 14:21:01 -0700287
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 switch (new->c_cflag & CSIZE) {
289 case CS5: mr1 |= MPC52xx_PSC_MODE_5_BITS;
290 break;
291 case CS6: mr1 |= MPC52xx_PSC_MODE_6_BITS;
292 break;
293 case CS7: mr1 |= MPC52xx_PSC_MODE_7_BITS;
294 break;
295 case CS8:
296 default: mr1 |= MPC52xx_PSC_MODE_8_BITS;
297 }
298
299 if (new->c_cflag & PARENB) {
300 mr1 |= (new->c_cflag & PARODD) ?
301 MPC52xx_PSC_MODE_PARODD : MPC52xx_PSC_MODE_PAREVEN;
302 } else
303 mr1 |= MPC52xx_PSC_MODE_PARNONE;
Grant Likely9b9129e2006-11-27 14:21:01 -0700304
305
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 mr2 = 0;
307
308 if (new->c_cflag & CSTOPB)
309 mr2 |= MPC52xx_PSC_MODE_TWO_STOP;
310 else
311 mr2 |= ((new->c_cflag & CSIZE) == CS5) ?
312 MPC52xx_PSC_MODE_ONE_STOP_5_BITS :
313 MPC52xx_PSC_MODE_ONE_STOP;
314
315
316 baud = uart_get_baud_rate(port, new, old, 0, port->uartclk/16);
317 quot = uart_get_divisor(port, baud);
318 ctr = quot & 0xffff;
Grant Likely9b9129e2006-11-27 14:21:01 -0700319
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 /* Get the lock */
321 spin_lock_irqsave(&port->lock, flags);
322
323 /* Update the per-port timeout */
324 uart_update_timeout(port, new->c_cflag, baud);
325
326 /* Do our best to flush TX & RX, so we don't loose anything */
327 /* But we don't wait indefinitly ! */
328 j = 5000000; /* Maximum wait */
329 /* FIXME Can't receive chars since set_termios might be called at early
330 * boot for the console, all stuff is not yet ready to receive at that
331 * time and that just makes the kernel oops */
332 /* while (j-- && mpc52xx_uart_int_rx_chars(port)); */
Grant Likely9b9129e2006-11-27 14:21:01 -0700333 while (!(in_be16(&psc->mpc52xx_psc_status) & MPC52xx_PSC_SR_TXEMP) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 --j)
335 udelay(1);
336
337 if (!j)
338 printk( KERN_ERR "mpc52xx_uart.c: "
339 "Unable to flush RX & TX fifos in-time in set_termios."
Grant Likely9b9129e2006-11-27 14:21:01 -0700340 "Some chars may have been lost.\n" );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
342 /* Reset the TX & RX */
343 out_8(&psc->command,MPC52xx_PSC_RST_RX);
344 out_8(&psc->command,MPC52xx_PSC_RST_TX);
345
346 /* Send new mode settings */
347 out_8(&psc->command,MPC52xx_PSC_SEL_MODE_REG_1);
348 out_8(&psc->mode,mr1);
349 out_8(&psc->mode,mr2);
350 out_8(&psc->ctur,ctr >> 8);
351 out_8(&psc->ctlr,ctr & 0xff);
Grant Likely9b9129e2006-11-27 14:21:01 -0700352
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 /* Reenable TX & RX */
354 out_8(&psc->command,MPC52xx_PSC_TX_ENABLE);
355 out_8(&psc->command,MPC52xx_PSC_RX_ENABLE);
356
357 /* We're all set, release the lock */
358 spin_unlock_irqrestore(&port->lock, flags);
359}
360
361static const char *
362mpc52xx_uart_type(struct uart_port *port)
363{
364 return port->type == PORT_MPC52xx ? "MPC52xx PSC" : NULL;
365}
366
367static void
368mpc52xx_uart_release_port(struct uart_port *port)
369{
370 if (port->flags & UPF_IOREMAP) { /* remapped by us ? */
371 iounmap(port->membase);
372 port->membase = NULL;
373 }
374
Grant Likelyb9272df2006-11-27 14:21:02 -0700375 release_mem_region(port->mapbase, sizeof(struct mpc52xx_psc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376}
377
378static int
379mpc52xx_uart_request_port(struct uart_port *port)
380{
Amol Ladbe618f52006-09-30 23:29:23 -0700381 int err;
382
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 if (port->flags & UPF_IOREMAP) /* Need to remap ? */
Grant Likelyb9272df2006-11-27 14:21:02 -0700384 port->membase = ioremap(port->mapbase,
385 sizeof(struct mpc52xx_psc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
387 if (!port->membase)
388 return -EINVAL;
389
Grant Likelyb9272df2006-11-27 14:21:02 -0700390 err = request_mem_region(port->mapbase, sizeof(struct mpc52xx_psc),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 "mpc52xx_psc_uart") != NULL ? 0 : -EBUSY;
Amol Ladbe618f52006-09-30 23:29:23 -0700392
393 if (err && (port->flags & UPF_IOREMAP)) {
394 iounmap(port->membase);
395 port->membase = NULL;
396 }
397
398 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399}
400
401static void
402mpc52xx_uart_config_port(struct uart_port *port, int flags)
403{
404 if ( (flags & UART_CONFIG_TYPE) &&
405 (mpc52xx_uart_request_port(port) == 0) )
406 port->type = PORT_MPC52xx;
407}
408
409static int
410mpc52xx_uart_verify_port(struct uart_port *port, struct serial_struct *ser)
411{
412 if ( ser->type != PORT_UNKNOWN && ser->type != PORT_MPC52xx )
413 return -EINVAL;
414
415 if ( (ser->irq != port->irq) ||
416 (ser->io_type != SERIAL_IO_MEM) ||
Grant Likely9b9129e2006-11-27 14:21:01 -0700417 (ser->baud_base != port->uartclk) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 (ser->iomem_base != (void*)port->mapbase) ||
419 (ser->hub6 != 0 ) )
420 return -EINVAL;
421
422 return 0;
423}
424
425
426static struct uart_ops mpc52xx_uart_ops = {
427 .tx_empty = mpc52xx_uart_tx_empty,
428 .set_mctrl = mpc52xx_uart_set_mctrl,
429 .get_mctrl = mpc52xx_uart_get_mctrl,
430 .stop_tx = mpc52xx_uart_stop_tx,
431 .start_tx = mpc52xx_uart_start_tx,
432 .send_xchar = mpc52xx_uart_send_xchar,
433 .stop_rx = mpc52xx_uart_stop_rx,
434 .enable_ms = mpc52xx_uart_enable_ms,
435 .break_ctl = mpc52xx_uart_break_ctl,
436 .startup = mpc52xx_uart_startup,
437 .shutdown = mpc52xx_uart_shutdown,
438 .set_termios = mpc52xx_uart_set_termios,
439/* .pm = mpc52xx_uart_pm, Not supported yet */
440/* .set_wake = mpc52xx_uart_set_wake, Not supported yet */
441 .type = mpc52xx_uart_type,
442 .release_port = mpc52xx_uart_release_port,
443 .request_port = mpc52xx_uart_request_port,
444 .config_port = mpc52xx_uart_config_port,
445 .verify_port = mpc52xx_uart_verify_port
446};
447
Grant Likely9b9129e2006-11-27 14:21:01 -0700448
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449/* ======================================================================== */
450/* Interrupt handling */
451/* ======================================================================== */
Grant Likely9b9129e2006-11-27 14:21:01 -0700452
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453static inline int
David Howells7d12e782006-10-05 14:55:46 +0100454mpc52xx_uart_int_rx_chars(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455{
456 struct tty_struct *tty = port->info->tty;
Alan Cox33f0f882006-01-09 20:54:13 -0800457 unsigned char ch, flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 unsigned short status;
459
460 /* While we can read, do so ! */
461 while ( (status = in_be16(&PSC(port)->mpc52xx_psc_status)) &
462 MPC52xx_PSC_SR_RXRDY) {
463
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 /* Get the char */
465 ch = in_8(&PSC(port)->mpc52xx_psc_buffer_8);
466
467 /* Handle sysreq char */
468#ifdef SUPPORT_SYSRQ
David Howells7d12e782006-10-05 14:55:46 +0100469 if (uart_handle_sysrq_char(port, ch)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 port->sysrq = 0;
471 continue;
472 }
473#endif
474
475 /* Store it */
Alan Cox33f0f882006-01-09 20:54:13 -0800476
477 flag = TTY_NORMAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 port->icount.rx++;
Grant Likely9b9129e2006-11-27 14:21:01 -0700479
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 if ( status & (MPC52xx_PSC_SR_PE |
481 MPC52xx_PSC_SR_FE |
Alan Cox33f0f882006-01-09 20:54:13 -0800482 MPC52xx_PSC_SR_RB) ) {
Grant Likely9b9129e2006-11-27 14:21:01 -0700483
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 if (status & MPC52xx_PSC_SR_RB) {
Alan Cox33f0f882006-01-09 20:54:13 -0800485 flag = TTY_BREAK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 uart_handle_break(port);
487 } else if (status & MPC52xx_PSC_SR_PE)
Alan Cox33f0f882006-01-09 20:54:13 -0800488 flag = TTY_PARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 else if (status & MPC52xx_PSC_SR_FE)
Alan Cox33f0f882006-01-09 20:54:13 -0800490 flag = TTY_FRAME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
492 /* Clear error condition */
493 out_8(&PSC(port)->command,MPC52xx_PSC_RST_ERR_STAT);
494
495 }
Alan Cox33f0f882006-01-09 20:54:13 -0800496 tty_insert_flip_char(tty, ch, flag);
497 if (status & MPC52xx_PSC_SR_OE) {
498 /*
499 * Overrun is special, since it's
500 * reported immediately, and doesn't
501 * affect the current character
502 */
503 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
504 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 }
506
507 tty_flip_buffer_push(tty);
Grant Likely9b9129e2006-11-27 14:21:01 -0700508
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 return in_be16(&PSC(port)->mpc52xx_psc_status) & MPC52xx_PSC_SR_RXRDY;
510}
511
512static inline int
513mpc52xx_uart_int_tx_chars(struct uart_port *port)
514{
515 struct circ_buf *xmit = &port->info->xmit;
516
517 /* Process out of band chars */
518 if (port->x_char) {
519 out_8(&PSC(port)->mpc52xx_psc_buffer_8, port->x_char);
520 port->icount.tx++;
521 port->x_char = 0;
522 return 1;
523 }
524
525 /* Nothing to do ? */
526 if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
Russell Kingb129a8c2005-08-31 10:12:14 +0100527 mpc52xx_uart_stop_tx(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 return 0;
529 }
530
531 /* Send chars */
532 while (in_be16(&PSC(port)->mpc52xx_psc_status) & MPC52xx_PSC_SR_TXRDY) {
533 out_8(&PSC(port)->mpc52xx_psc_buffer_8, xmit->buf[xmit->tail]);
534 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
535 port->icount.tx++;
536 if (uart_circ_empty(xmit))
537 break;
538 }
539
540 /* Wake up */
541 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
542 uart_write_wakeup(port);
543
544 /* Maybe we're done after all */
545 if (uart_circ_empty(xmit)) {
Russell Kingb129a8c2005-08-31 10:12:14 +0100546 mpc52xx_uart_stop_tx(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 return 0;
548 }
549
550 return 1;
551}
552
Grant Likely9b9129e2006-11-27 14:21:01 -0700553static irqreturn_t
David Howells7d12e782006-10-05 14:55:46 +0100554mpc52xx_uart_int(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555{
Jeff Garzikc7bec5a2006-10-06 15:00:58 -0400556 struct uart_port *port = dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 unsigned long pass = ISR_PASS_LIMIT;
558 unsigned int keepgoing;
559 unsigned short status;
Grant Likely9b9129e2006-11-27 14:21:01 -0700560
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 spin_lock(&port->lock);
Grant Likely9b9129e2006-11-27 14:21:01 -0700562
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 /* While we have stuff to do, we continue */
564 do {
565 /* If we don't find anything to do, we stop */
Grant Likely9b9129e2006-11-27 14:21:01 -0700566 keepgoing = 0;
567
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 /* Read status */
569 status = in_be16(&PSC(port)->mpc52xx_psc_isr);
570 status &= port->read_status_mask;
Grant Likely9b9129e2006-11-27 14:21:01 -0700571
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 /* Do we need to receive chars ? */
573 /* For this RX interrupts must be on and some chars waiting */
574 if ( status & MPC52xx_PSC_IMR_RXRDY )
David Howells7d12e782006-10-05 14:55:46 +0100575 keepgoing |= mpc52xx_uart_int_rx_chars(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
577 /* Do we need to send chars ? */
578 /* For this, TX must be ready and TX interrupt enabled */
579 if ( status & MPC52xx_PSC_IMR_TXRDY )
580 keepgoing |= mpc52xx_uart_int_tx_chars(port);
Grant Likely9b9129e2006-11-27 14:21:01 -0700581
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 /* Limit number of iteration */
583 if ( !(--pass) )
584 keepgoing = 0;
585
586 } while (keepgoing);
Grant Likely9b9129e2006-11-27 14:21:01 -0700587
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 spin_unlock(&port->lock);
Grant Likely9b9129e2006-11-27 14:21:01 -0700589
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 return IRQ_HANDLED;
591}
592
593
594/* ======================================================================== */
595/* Console ( if applicable ) */
596/* ======================================================================== */
597
598#ifdef CONFIG_SERIAL_MPC52xx_CONSOLE
599
600static void __init
601mpc52xx_console_get_options(struct uart_port *port,
602 int *baud, int *parity, int *bits, int *flow)
603{
604 struct mpc52xx_psc __iomem *psc = PSC(port);
605 unsigned char mr1;
606
Grant Likelyb9272df2006-11-27 14:21:02 -0700607 pr_debug("mpc52xx_console_get_options(port=%p)\n", port);
608
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 /* Read the mode registers */
610 out_8(&psc->command,MPC52xx_PSC_SEL_MODE_REG_1);
611 mr1 = in_8(&psc->mode);
Grant Likely9b9129e2006-11-27 14:21:01 -0700612
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 /* CT{U,L}R are write-only ! */
Grant Likelyb9272df2006-11-27 14:21:02 -0700614 *baud = CONFIG_SERIAL_MPC52xx_CONSOLE_BAUD;
615#if !defined(CONFIG_PPC_MERGE)
616 if (__res.bi_baudrate)
617 *baud = __res.bi_baudrate;
618#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
620 /* Parse them */
621 switch (mr1 & MPC52xx_PSC_MODE_BITS_MASK) {
622 case MPC52xx_PSC_MODE_5_BITS: *bits = 5; break;
623 case MPC52xx_PSC_MODE_6_BITS: *bits = 6; break;
624 case MPC52xx_PSC_MODE_7_BITS: *bits = 7; break;
625 case MPC52xx_PSC_MODE_8_BITS:
626 default: *bits = 8;
627 }
Grant Likely9b9129e2006-11-27 14:21:01 -0700628
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 if (mr1 & MPC52xx_PSC_MODE_PARNONE)
630 *parity = 'n';
631 else
632 *parity = mr1 & MPC52xx_PSC_MODE_PARODD ? 'o' : 'e';
633}
634
Grant Likely9b9129e2006-11-27 14:21:01 -0700635static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636mpc52xx_console_write(struct console *co, const char *s, unsigned int count)
637{
638 struct uart_port *port = &mpc52xx_uart_ports[co->index];
639 struct mpc52xx_psc __iomem *psc = PSC(port);
640 unsigned int i, j;
Grant Likely9b9129e2006-11-27 14:21:01 -0700641
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 /* Disable interrupts */
643 out_be16(&psc->mpc52xx_psc_imr, 0);
644
645 /* Wait the TX buffer to be empty */
Grant Likely9b9129e2006-11-27 14:21:01 -0700646 j = 5000000; /* Maximum wait */
647 while (!(in_be16(&psc->mpc52xx_psc_status) & MPC52xx_PSC_SR_TXEMP) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 --j)
649 udelay(1);
650
651 /* Write all the chars */
Russell Kingd3587882006-03-20 20:00:09 +0000652 for (i = 0; i < count; i++, s++) {
653 /* Line return handling */
654 if (*s == '\n')
655 out_8(&psc->mpc52xx_psc_buffer_8, '\r');
Grant Likely9b9129e2006-11-27 14:21:01 -0700656
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 /* Send the char */
658 out_8(&psc->mpc52xx_psc_buffer_8, *s);
659
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 /* Wait the TX buffer to be empty */
Grant Likely9b9129e2006-11-27 14:21:01 -0700661 j = 20000; /* Maximum wait */
662 while (!(in_be16(&psc->mpc52xx_psc_status) &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 MPC52xx_PSC_SR_TXEMP) && --j)
664 udelay(1);
665 }
666
667 /* Restore interrupt state */
668 out_be16(&psc->mpc52xx_psc_imr, port->read_status_mask);
669}
670
Grant Likelyb9272df2006-11-27 14:21:02 -0700671#if !defined(CONFIG_PPC_MERGE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672static int __init
673mpc52xx_console_setup(struct console *co, char *options)
674{
675 struct uart_port *port = &mpc52xx_uart_ports[co->index];
676
677 int baud = CONFIG_SERIAL_MPC52xx_CONSOLE_BAUD;
678 int bits = 8;
679 int parity = 'n';
680 int flow = 'n';
681
682 if (co->index < 0 || co->index >= MPC52xx_PSC_MAXNUM)
683 return -EINVAL;
Grant Likely9b9129e2006-11-27 14:21:01 -0700684
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 /* Basic port init. Needed since we use some uart_??? func before
686 * real init for early access */
687 spin_lock_init(&port->lock);
688 port->uartclk = __res.bi_ipbfreq / 2; /* Look at CTLR doc */
689 port->ops = &mpc52xx_uart_ops;
690 port->mapbase = MPC52xx_PA(MPC52xx_PSCx_OFFSET(co->index+1));
691
692 /* We ioremap ourself */
693 port->membase = ioremap(port->mapbase, MPC52xx_PSC_SIZE);
694 if (port->membase == NULL)
695 return -EINVAL;
696
697 /* Setup the port parameters accoding to options */
698 if (options)
699 uart_parse_options(options, &baud, &parity, &bits, &flow);
700 else
701 mpc52xx_console_get_options(port, &baud, &parity, &bits, &flow);
702
703 return uart_set_options(port, co, baud, parity, bits, flow);
704}
705
Grant Likelyb9272df2006-11-27 14:21:02 -0700706#else
707
708static int __init
709mpc52xx_console_setup(struct console *co, char *options)
710{
711 struct uart_port *port = &mpc52xx_uart_ports[co->index];
712 struct device_node *np = mpc52xx_uart_nodes[co->index];
713 unsigned int ipb_freq;
714 struct resource res;
715 int ret;
716
717 int baud = CONFIG_SERIAL_MPC52xx_CONSOLE_BAUD;
718 int bits = 8;
719 int parity = 'n';
720 int flow = 'n';
721
722 pr_debug("mpc52xx_console_setup co=%p, co->index=%i, options=%s\n",
723 co, co->index, options);
724
725 if ((co->index < 0) || (co->index > MPC52xx_PSC_MAXNUM)) {
726 pr_debug("PSC%x out of range\n", co->index);
727 return -EINVAL;
728 }
729
730 if (!np) {
731 pr_debug("PSC%x not found in device tree\n", co->index);
732 return -EINVAL;
733 }
734
735 pr_debug("Console on ttyPSC%x is %s\n",
736 co->index, mpc52xx_uart_nodes[co->index]->full_name);
737
738 /* Fetch register locations */
739 if ((ret = of_address_to_resource(np, 0, &res)) != 0) {
740 pr_debug("Could not get resources for PSC%x\n", co->index);
741 return ret;
742 }
743
744 /* Search for bus-frequency property in this node or a parent */
745 if ((ipb_freq = mpc52xx_find_ipb_freq(np)) == 0) {
746 pr_debug("Could not find IPB bus frequency!\n");
747 return -EINVAL;
748 }
749
750 /* Basic port init. Needed since we use some uart_??? func before
751 * real init for early access */
752 spin_lock_init(&port->lock);
753 port->uartclk = ipb_freq / 2;
754 port->ops = &mpc52xx_uart_ops;
755 port->mapbase = res.start;
756 port->membase = ioremap(res.start, sizeof(struct mpc52xx_psc));
757 port->irq = irq_of_parse_and_map(np, 0);
758
759 if (port->membase == NULL)
760 return -EINVAL;
761
Grant Likely5dd80d52007-10-13 22:37:02 -0600762 pr_debug("mpc52xx-psc uart at %p, mapped to %p, irq=%x, freq=%i\n",
763 (void*)port->mapbase, port->membase, port->irq, port->uartclk);
Grant Likelyb9272df2006-11-27 14:21:02 -0700764
765 /* Setup the port parameters accoding to options */
766 if (options)
767 uart_parse_options(options, &baud, &parity, &bits, &flow);
768 else
769 mpc52xx_console_get_options(port, &baud, &parity, &bits, &flow);
770
771 pr_debug("Setting console parameters: %i %i%c1 flow=%c\n",
772 baud, bits, parity, flow);
773
774 return uart_set_options(port, co, baud, parity, bits, flow);
775}
776#endif /* defined(CONFIG_PPC_MERGE) */
777
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
Sylvain Munaut2d8179c2006-01-06 00:11:31 -0800779static struct uart_driver mpc52xx_uart_driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
781static struct console mpc52xx_console = {
Sylvain Munautd62de3a2006-01-06 00:11:32 -0800782 .name = "ttyPSC",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 .write = mpc52xx_console_write,
784 .device = uart_console_device,
785 .setup = mpc52xx_console_setup,
786 .flags = CON_PRINTBUFFER,
Sylvain Munautd62de3a2006-01-06 00:11:32 -0800787 .index = -1, /* Specified on the cmdline (e.g. console=ttyPSC0 ) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 .data = &mpc52xx_uart_driver,
789};
790
Grant Likely9b9129e2006-11-27 14:21:01 -0700791
792static int __init
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793mpc52xx_console_init(void)
794{
Grant Likelyc98750c2007-01-02 15:45:37 -0700795#if defined(CONFIG_PPC_MERGE)
Grant Likelyb9272df2006-11-27 14:21:02 -0700796 mpc52xx_uart_of_enumerate();
Grant Likelyc98750c2007-01-02 15:45:37 -0700797#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 register_console(&mpc52xx_console);
799 return 0;
800}
801
802console_initcall(mpc52xx_console_init);
803
804#define MPC52xx_PSC_CONSOLE &mpc52xx_console
805#else
806#define MPC52xx_PSC_CONSOLE NULL
807#endif
808
809
810/* ======================================================================== */
811/* UART Driver */
812/* ======================================================================== */
813
814static struct uart_driver mpc52xx_uart_driver = {
815 .owner = THIS_MODULE,
816 .driver_name = "mpc52xx_psc_uart",
Sylvain Munautd62de3a2006-01-06 00:11:32 -0800817 .dev_name = "ttyPSC",
Sylvain Munautd62de3a2006-01-06 00:11:32 -0800818 .major = SERIAL_PSC_MAJOR,
819 .minor = SERIAL_PSC_MINOR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 .nr = MPC52xx_PSC_MAXNUM,
821 .cons = MPC52xx_PSC_CONSOLE,
822};
823
824
Grant Likelyb9272df2006-11-27 14:21:02 -0700825#if !defined(CONFIG_PPC_MERGE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826/* ======================================================================== */
827/* Platform Driver */
828/* ======================================================================== */
829
830static int __devinit
Russell King3ae5eae2005-11-09 22:32:44 +0000831mpc52xx_uart_probe(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832{
Russell King3ae5eae2005-11-09 22:32:44 +0000833 struct resource *res = dev->resource;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
835 struct uart_port *port = NULL;
836 int i, idx, ret;
837
838 /* Check validity & presence */
Andrey Volkov38801e22005-11-12 22:04:06 +0000839 idx = dev->id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 if (idx < 0 || idx >= MPC52xx_PSC_MAXNUM)
841 return -EINVAL;
842
843 if (!mpc52xx_match_psc_function(idx,"uart"))
844 return -ENODEV;
845
846 /* Init the port structure */
847 port = &mpc52xx_uart_ports[idx];
848
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 spin_lock_init(&port->lock);
850 port->uartclk = __res.bi_ipbfreq / 2; /* Look at CTLR doc */
Russell King947deee2006-07-02 20:45:51 +0100851 port->fifosize = 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 port->iotype = UPIO_MEM;
853 port->flags = UPF_BOOT_AUTOCONF |
854 ( uart_console(port) ? 0 : UPF_IOREMAP );
855 port->line = idx;
856 port->ops = &mpc52xx_uart_ops;
Grant Likelyb9272df2006-11-27 14:21:02 -0700857 port->dev = &dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858
859 /* Search for IRQ and mapbase */
Andrey Volkov38801e22005-11-12 22:04:06 +0000860 for (i=0 ; i<dev->num_resources ; i++, res++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 if (res->flags & IORESOURCE_MEM)
862 port->mapbase = res->start;
863 else if (res->flags & IORESOURCE_IRQ)
864 port->irq = res->start;
865 }
866 if (!port->irq || !port->mapbase)
867 return -EINVAL;
868
869 /* Add the port to the uart sub-system */
870 ret = uart_add_one_port(&mpc52xx_uart_driver, port);
871 if (!ret)
Russell King3ae5eae2005-11-09 22:32:44 +0000872 platform_set_drvdata(dev, (void*)port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873
874 return ret;
875}
876
877static int
Russell King3ae5eae2005-11-09 22:32:44 +0000878mpc52xx_uart_remove(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879{
Russell King3ae5eae2005-11-09 22:32:44 +0000880 struct uart_port *port = (struct uart_port *) platform_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881
Russell King3ae5eae2005-11-09 22:32:44 +0000882 platform_set_drvdata(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883
884 if (port)
885 uart_remove_one_port(&mpc52xx_uart_driver, port);
886
887 return 0;
888}
889
890#ifdef CONFIG_PM
891static int
Russell King3ae5eae2005-11-09 22:32:44 +0000892mpc52xx_uart_suspend(struct platform_device *dev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893{
Russell King3ae5eae2005-11-09 22:32:44 +0000894 struct uart_port *port = (struct uart_port *) platform_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895
Grant Likely9b9129e2006-11-27 14:21:01 -0700896 if (port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 uart_suspend_port(&mpc52xx_uart_driver, port);
898
899 return 0;
900}
901
902static int
Russell King3ae5eae2005-11-09 22:32:44 +0000903mpc52xx_uart_resume(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904{
Russell King3ae5eae2005-11-09 22:32:44 +0000905 struct uart_port *port = (struct uart_port *) platform_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906
Russell King9480e302005-10-28 09:52:56 -0700907 if (port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 uart_resume_port(&mpc52xx_uart_driver, port);
909
910 return 0;
911}
912#endif
913
Grant Likelyb9272df2006-11-27 14:21:02 -0700914
Russell King3ae5eae2005-11-09 22:32:44 +0000915static struct platform_driver mpc52xx_uart_platform_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 .probe = mpc52xx_uart_probe,
917 .remove = mpc52xx_uart_remove,
918#ifdef CONFIG_PM
919 .suspend = mpc52xx_uart_suspend,
920 .resume = mpc52xx_uart_resume,
921#endif
Russell King3ae5eae2005-11-09 22:32:44 +0000922 .driver = {
923 .name = "mpc52xx-psc",
924 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925};
Grant Likelyb9272df2006-11-27 14:21:02 -0700926#endif /* !defined(CONFIG_PPC_MERGE) */
927
928
929#if defined(CONFIG_PPC_MERGE)
930/* ======================================================================== */
931/* OF Platform Driver */
932/* ======================================================================== */
933
934static int __devinit
935mpc52xx_uart_of_probe(struct of_device *op, const struct of_device_id *match)
936{
937 int idx = -1;
938 unsigned int ipb_freq;
939 struct uart_port *port = NULL;
940 struct resource res;
941 int ret;
942
943 dev_dbg(&op->dev, "mpc52xx_uart_probe(op=%p, match=%p)\n", op, match);
944
945 /* Check validity & presence */
946 for (idx = 0; idx < MPC52xx_PSC_MAXNUM; idx++)
947 if (mpc52xx_uart_nodes[idx] == op->node)
948 break;
949 if (idx >= MPC52xx_PSC_MAXNUM)
950 return -EINVAL;
951 pr_debug("Found %s assigned to ttyPSC%x\n",
952 mpc52xx_uart_nodes[idx]->full_name, idx);
953
954 /* Search for bus-frequency property in this node or a parent */
955 if ((ipb_freq = mpc52xx_find_ipb_freq(op->node)) == 0) {
956 dev_dbg(&op->dev, "Could not find IPB bus frequency!\n");
957 return -EINVAL;
958 }
959
960 /* Init the port structure */
961 port = &mpc52xx_uart_ports[idx];
962
963 spin_lock_init(&port->lock);
964 port->uartclk = ipb_freq / 2;
965 port->fifosize = 512;
966 port->iotype = UPIO_MEM;
967 port->flags = UPF_BOOT_AUTOCONF |
968 ( uart_console(port) ? 0 : UPF_IOREMAP );
969 port->line = idx;
970 port->ops = &mpc52xx_uart_ops;
971 port->dev = &op->dev;
972
973 /* Search for IRQ and mapbase */
974 if ((ret = of_address_to_resource(op->node, 0, &res)) != 0)
975 return ret;
976
977 port->mapbase = res.start;
978 port->irq = irq_of_parse_and_map(op->node, 0);
979
Grant Likely5dd80d52007-10-13 22:37:02 -0600980 dev_dbg(&op->dev, "mpc52xx-psc uart at %p, irq=%x, freq=%i\n",
981 (void*)port->mapbase, port->irq, port->uartclk);
Grant Likelyb9272df2006-11-27 14:21:02 -0700982
983 if ((port->irq==NO_IRQ) || !port->mapbase) {
984 printk(KERN_ERR "Could not allocate resources for PSC\n");
985 return -EINVAL;
986 }
987
988 /* Add the port to the uart sub-system */
989 ret = uart_add_one_port(&mpc52xx_uart_driver, port);
990 if (!ret)
991 dev_set_drvdata(&op->dev, (void*)port);
992
993 return ret;
994}
995
996static int
997mpc52xx_uart_of_remove(struct of_device *op)
998{
999 struct uart_port *port = dev_get_drvdata(&op->dev);
1000 dev_set_drvdata(&op->dev, NULL);
1001
Sylvain Munautfc7900b2007-02-15 23:18:08 +01001002 if (port) {
Grant Likelyb9272df2006-11-27 14:21:02 -07001003 uart_remove_one_port(&mpc52xx_uart_driver, port);
Sylvain Munautfc7900b2007-02-15 23:18:08 +01001004 irq_dispose_mapping(port->irq);
1005 }
Grant Likelyb9272df2006-11-27 14:21:02 -07001006
1007 return 0;
1008}
1009
1010#ifdef CONFIG_PM
1011static int
1012mpc52xx_uart_of_suspend(struct of_device *op, pm_message_t state)
1013{
1014 struct uart_port *port = (struct uart_port *) dev_get_drvdata(&op->dev);
1015
1016 if (port)
1017 uart_suspend_port(&mpc52xx_uart_driver, port);
1018
1019 return 0;
1020}
1021
1022static int
1023mpc52xx_uart_of_resume(struct of_device *op)
1024{
1025 struct uart_port *port = (struct uart_port *) dev_get_drvdata(&op->dev);
1026
1027 if (port)
1028 uart_resume_port(&mpc52xx_uart_driver, port);
1029
1030 return 0;
1031}
1032#endif
1033
1034static void
1035mpc52xx_uart_of_assign(struct device_node *np, int idx)
1036{
1037 int free_idx = -1;
1038 int i;
1039
1040 /* Find the first free node */
1041 for (i = 0; i < MPC52xx_PSC_MAXNUM; i++) {
1042 if (mpc52xx_uart_nodes[i] == NULL) {
1043 free_idx = i;
1044 break;
1045 }
1046 }
1047
1048 if ((idx < 0) || (idx >= MPC52xx_PSC_MAXNUM))
1049 idx = free_idx;
1050
1051 if (idx < 0)
1052 return; /* No free slot; abort */
1053
1054 /* If the slot is already occupied, then swap slots */
1055 if (mpc52xx_uart_nodes[idx] && (free_idx != -1))
1056 mpc52xx_uart_nodes[free_idx] = mpc52xx_uart_nodes[idx];
John Rigbyaf6a9aa2007-05-23 11:30:55 -06001057 mpc52xx_uart_nodes[idx] = np;
Grant Likelyb9272df2006-11-27 14:21:02 -07001058}
1059
1060static void
1061mpc52xx_uart_of_enumerate(void)
1062{
1063 static int enum_done = 0;
1064 struct device_node *np;
1065 const unsigned int *devno;
1066 int i;
1067
1068 if (enum_done)
1069 return;
1070
1071 for_each_node_by_type(np, "serial") {
1072 if (!of_match_node(mpc52xx_uart_of_match, np))
1073 continue;
1074
1075 /* Is a particular device number requested? */
Stephen Rothwell40cd3a42007-05-01 13:54:02 +10001076 devno = of_get_property(np, "port-number", NULL);
Grant Likelyb9272df2006-11-27 14:21:02 -07001077 mpc52xx_uart_of_assign(of_node_get(np), devno ? *devno : -1);
1078 }
1079
1080 enum_done = 1;
1081
1082 for (i = 0; i < MPC52xx_PSC_MAXNUM; i++) {
1083 if (mpc52xx_uart_nodes[i])
1084 pr_debug("%s assigned to ttyPSC%x\n",
1085 mpc52xx_uart_nodes[i]->full_name, i);
1086 }
1087}
1088
1089MODULE_DEVICE_TABLE(of, mpc52xx_uart_of_match);
1090
1091static struct of_platform_driver mpc52xx_uart_of_driver = {
1092 .owner = THIS_MODULE,
1093 .name = "mpc52xx-psc-uart",
1094 .match_table = mpc52xx_uart_of_match,
1095 .probe = mpc52xx_uart_of_probe,
1096 .remove = mpc52xx_uart_of_remove,
1097#ifdef CONFIG_PM
1098 .suspend = mpc52xx_uart_of_suspend,
1099 .resume = mpc52xx_uart_of_resume,
1100#endif
1101 .driver = {
1102 .name = "mpc52xx-psc-uart",
1103 },
1104};
1105#endif /* defined(CONFIG_PPC_MERGE) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106
1107
1108/* ======================================================================== */
1109/* Module */
1110/* ======================================================================== */
1111
1112static int __init
1113mpc52xx_uart_init(void)
1114{
1115 int ret;
1116
Grant Likelyb9272df2006-11-27 14:21:02 -07001117 printk(KERN_INFO "Serial: MPC52xx PSC UART driver\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118
Grant Likelyb9272df2006-11-27 14:21:02 -07001119 if ((ret = uart_register_driver(&mpc52xx_uart_driver)) != 0) {
1120 printk(KERN_ERR "%s: uart_register_driver failed (%i)\n",
1121 __FILE__, ret);
1122 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 }
1124
Grant Likelyb9272df2006-11-27 14:21:02 -07001125#if defined(CONFIG_PPC_MERGE)
1126 mpc52xx_uart_of_enumerate();
1127
1128 ret = of_register_platform_driver(&mpc52xx_uart_of_driver);
1129 if (ret) {
1130 printk(KERN_ERR "%s: of_register_platform_driver failed (%i)\n",
1131 __FILE__, ret);
1132 uart_unregister_driver(&mpc52xx_uart_driver);
1133 return ret;
1134 }
1135#else
1136 ret = platform_driver_register(&mpc52xx_uart_platform_driver);
1137 if (ret) {
1138 printk(KERN_ERR "%s: platform_driver_register failed (%i)\n",
1139 __FILE__, ret);
1140 uart_unregister_driver(&mpc52xx_uart_driver);
1141 return ret;
1142 }
1143#endif
1144
1145 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146}
1147
1148static void __exit
1149mpc52xx_uart_exit(void)
1150{
Grant Likelyb9272df2006-11-27 14:21:02 -07001151#if defined(CONFIG_PPC_MERGE)
1152 of_unregister_platform_driver(&mpc52xx_uart_of_driver);
1153#else
Russell King3ae5eae2005-11-09 22:32:44 +00001154 platform_driver_unregister(&mpc52xx_uart_platform_driver);
Grant Likelyb9272df2006-11-27 14:21:02 -07001155#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 uart_unregister_driver(&mpc52xx_uart_driver);
1157}
1158
1159
1160module_init(mpc52xx_uart_init);
1161module_exit(mpc52xx_uart_exit);
1162
1163MODULE_AUTHOR("Sylvain Munaut <tnt@246tNt.com>");
1164MODULE_DESCRIPTION("Freescale MPC52xx PSC UART");
1165MODULE_LICENSE("GPL");