blob: 955bbd653e22c0c94764c1503c7f3c1818bcdb9a [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)
75#include <asm/of_platform.h>
76#else
77#include <linux/platform_device.h>
78#endif
79
Linus Torvalds1da177e2005-04-16 15:20:36 -070080#include <asm/mpc52xx.h>
81#include <asm/mpc52xx_psc.h>
82
83#if defined(CONFIG_SERIAL_MPC52xx_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
84#define SUPPORT_SYSRQ
85#endif
86
87#include <linux/serial_core.h>
88
89
Sylvain Munautd62de3a2006-01-06 00:11:32 -080090/* 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
93
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
95#define ISR_PASS_LIMIT 256 /* Max number of iteration in the interrupt */
96
97
98static 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
105 */
Grant Likelyb9272df2006-11-27 14:21:02 -0700106#if defined(CONFIG_PPC_MERGE)
107/* lookup table for matching device nodes to index numbers */
108static struct device_node *mpc52xx_uart_nodes[MPC52xx_PSC_MAXNUM];
109
110static void mpc52xx_uart_of_enumerate(void);
111#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
113#define PSC(port) ((struct mpc52xx_psc __iomem *)((port)->membase))
114
115
116/* Forward declaration of the interruption handling routine */
David Howells7d12e782006-10-05 14:55:46 +0100117static irqreturn_t mpc52xx_uart_int(int irq,void *dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
119
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
127
Grant Likelyb9272df2006-11-27 14:21:02 -0700128#if defined(CONFIG_PPC_MERGE)
129static struct of_device_id mpc52xx_uart_of_match[] = {
Grant Likelye3aba812007-02-12 13:36:55 -0700130 { .type = "serial", .compatible = "mpc5200-psc-uart", },
Grant Likelyb9272df2006-11-27 14:21:02 -0700131 {},
132};
133#endif
134
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
136/* ======================================================================== */
137/* UART operations */
138/* ======================================================================== */
139
Grant Likely9b9129e2006-11-27 14:21:01 -0700140static unsigned int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141mpc52xx_uart_tx_empty(struct uart_port *port)
142{
143 int status = in_be16(&PSC(port)->mpc52xx_psc_status);
144 return (status & MPC52xx_PSC_SR_TXEMP) ? TIOCSER_TEMT : 0;
145}
146
Grant Likely9b9129e2006-11-27 14:21:01 -0700147static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148mpc52xx_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
149{
150 /* Not implemented */
151}
152
Grant Likely9b9129e2006-11-27 14:21:01 -0700153static unsigned int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154mpc52xx_uart_get_mctrl(struct uart_port *port)
155{
156 /* Not implemented */
157 return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
158}
159
Grant Likely9b9129e2006-11-27 14:21:01 -0700160static void
Russell Kingb129a8c2005-08-31 10:12:14 +0100161mpc52xx_uart_stop_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162{
163 /* port->lock taken by caller */
164 port->read_status_mask &= ~MPC52xx_PSC_IMR_TXRDY;
165 out_be16(&PSC(port)->mpc52xx_psc_imr,port->read_status_mask);
166}
167
Grant Likely9b9129e2006-11-27 14:21:01 -0700168static void
Russell Kingb129a8c2005-08-31 10:12:14 +0100169mpc52xx_uart_start_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170{
171 /* port->lock taken by caller */
172 port->read_status_mask |= MPC52xx_PSC_IMR_TXRDY;
173 out_be16(&PSC(port)->mpc52xx_psc_imr,port->read_status_mask);
174}
175
Grant Likely9b9129e2006-11-27 14:21:01 -0700176static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177mpc52xx_uart_send_xchar(struct uart_port *port, char ch)
178{
179 unsigned long flags;
180 spin_lock_irqsave(&port->lock, flags);
Grant Likely9b9129e2006-11-27 14:21:01 -0700181
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 port->x_char = ch;
183 if (ch) {
184 /* Make sure tx interrupts are on */
185 /* Truly necessary ??? They should be anyway */
186 port->read_status_mask |= MPC52xx_PSC_IMR_TXRDY;
187 out_be16(&PSC(port)->mpc52xx_psc_imr,port->read_status_mask);
188 }
Grant Likely9b9129e2006-11-27 14:21:01 -0700189
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 spin_unlock_irqrestore(&port->lock, flags);
191}
192
193static void
194mpc52xx_uart_stop_rx(struct uart_port *port)
195{
196 /* port->lock taken by caller */
197 port->read_status_mask &= ~MPC52xx_PSC_IMR_RXRDY;
198 out_be16(&PSC(port)->mpc52xx_psc_imr,port->read_status_mask);
199}
200
201static void
202mpc52xx_uart_enable_ms(struct uart_port *port)
203{
204 /* Not implemented */
205}
206
207static void
208mpc52xx_uart_break_ctl(struct uart_port *port, int ctl)
209{
210 unsigned long flags;
211 spin_lock_irqsave(&port->lock, flags);
212
213 if ( ctl == -1 )
214 out_8(&PSC(port)->command,MPC52xx_PSC_START_BRK);
215 else
216 out_8(&PSC(port)->command,MPC52xx_PSC_STOP_BRK);
Grant Likely9b9129e2006-11-27 14:21:01 -0700217
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 spin_unlock_irqrestore(&port->lock, flags);
219}
220
221static int
222mpc52xx_uart_startup(struct uart_port *port)
223{
224 struct mpc52xx_psc __iomem *psc = PSC(port);
225 int ret;
226
227 /* Request IRQ */
228 ret = request_irq(port->irq, mpc52xx_uart_int,
Thomas Gleixner40663cc2006-07-01 19:29:43 -0700229 IRQF_DISABLED | IRQF_SAMPLE_RANDOM, "mpc52xx_psc_uart", port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 if (ret)
231 return ret;
232
233 /* Reset/activate the port, clear and enable interrupts */
234 out_8(&psc->command,MPC52xx_PSC_RST_RX);
235 out_8(&psc->command,MPC52xx_PSC_RST_TX);
Grant Likely9b9129e2006-11-27 14:21:01 -0700236
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 out_be32(&psc->sicr,0); /* UART mode DCD ignored */
238
239 out_be16(&psc->mpc52xx_psc_clock_select, 0xdd00); /* /16 prescaler on */
Grant Likely9b9129e2006-11-27 14:21:01 -0700240
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 out_8(&psc->rfcntl, 0x00);
242 out_be16(&psc->rfalarm, 0x1ff);
243 out_8(&psc->tfcntl, 0x07);
244 out_be16(&psc->tfalarm, 0x80);
245
246 port->read_status_mask |= MPC52xx_PSC_IMR_RXRDY | MPC52xx_PSC_IMR_TXRDY;
247 out_be16(&psc->mpc52xx_psc_imr,port->read_status_mask);
Grant Likely9b9129e2006-11-27 14:21:01 -0700248
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 out_8(&psc->command,MPC52xx_PSC_TX_ENABLE);
250 out_8(&psc->command,MPC52xx_PSC_RX_ENABLE);
Grant Likely9b9129e2006-11-27 14:21:01 -0700251
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 return 0;
253}
254
255static void
256mpc52xx_uart_shutdown(struct uart_port *port)
257{
258 struct mpc52xx_psc __iomem *psc = PSC(port);
Grant Likely9b9129e2006-11-27 14:21:01 -0700259
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 /* Shut down the port, interrupt and all */
261 out_8(&psc->command,MPC52xx_PSC_RST_RX);
262 out_8(&psc->command,MPC52xx_PSC_RST_TX);
Grant Likely9b9129e2006-11-27 14:21:01 -0700263
264 port->read_status_mask = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 out_be16(&psc->mpc52xx_psc_imr,port->read_status_mask);
266
267 /* Release interrupt */
268 free_irq(port->irq, port);
269}
270
Grant Likely9b9129e2006-11-27 14:21:01 -0700271static void
Alan Cox606d0992006-12-08 02:38:45 -0800272mpc52xx_uart_set_termios(struct uart_port *port, struct ktermios *new,
273 struct ktermios *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274{
275 struct mpc52xx_psc __iomem *psc = PSC(port);
276 unsigned long flags;
277 unsigned char mr1, mr2;
278 unsigned short ctr;
279 unsigned int j, baud, quot;
Grant Likely9b9129e2006-11-27 14:21:01 -0700280
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 /* Prepare what we're gonna write */
282 mr1 = 0;
Grant Likely9b9129e2006-11-27 14:21:01 -0700283
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 switch (new->c_cflag & CSIZE) {
285 case CS5: mr1 |= MPC52xx_PSC_MODE_5_BITS;
286 break;
287 case CS6: mr1 |= MPC52xx_PSC_MODE_6_BITS;
288 break;
289 case CS7: mr1 |= MPC52xx_PSC_MODE_7_BITS;
290 break;
291 case CS8:
292 default: mr1 |= MPC52xx_PSC_MODE_8_BITS;
293 }
294
295 if (new->c_cflag & PARENB) {
296 mr1 |= (new->c_cflag & PARODD) ?
297 MPC52xx_PSC_MODE_PARODD : MPC52xx_PSC_MODE_PAREVEN;
298 } else
299 mr1 |= MPC52xx_PSC_MODE_PARNONE;
Grant Likely9b9129e2006-11-27 14:21:01 -0700300
301
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 mr2 = 0;
303
304 if (new->c_cflag & CSTOPB)
305 mr2 |= MPC52xx_PSC_MODE_TWO_STOP;
306 else
307 mr2 |= ((new->c_cflag & CSIZE) == CS5) ?
308 MPC52xx_PSC_MODE_ONE_STOP_5_BITS :
309 MPC52xx_PSC_MODE_ONE_STOP;
310
311
312 baud = uart_get_baud_rate(port, new, old, 0, port->uartclk/16);
313 quot = uart_get_divisor(port, baud);
314 ctr = quot & 0xffff;
Grant Likely9b9129e2006-11-27 14:21:01 -0700315
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 /* Get the lock */
317 spin_lock_irqsave(&port->lock, flags);
318
319 /* Update the per-port timeout */
320 uart_update_timeout(port, new->c_cflag, baud);
321
322 /* Do our best to flush TX & RX, so we don't loose anything */
323 /* But we don't wait indefinitly ! */
324 j = 5000000; /* Maximum wait */
325 /* FIXME Can't receive chars since set_termios might be called at early
326 * boot for the console, all stuff is not yet ready to receive at that
327 * time and that just makes the kernel oops */
328 /* while (j-- && mpc52xx_uart_int_rx_chars(port)); */
Grant Likely9b9129e2006-11-27 14:21:01 -0700329 while (!(in_be16(&psc->mpc52xx_psc_status) & MPC52xx_PSC_SR_TXEMP) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 --j)
331 udelay(1);
332
333 if (!j)
334 printk( KERN_ERR "mpc52xx_uart.c: "
335 "Unable to flush RX & TX fifos in-time in set_termios."
Grant Likely9b9129e2006-11-27 14:21:01 -0700336 "Some chars may have been lost.\n" );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
338 /* Reset the TX & RX */
339 out_8(&psc->command,MPC52xx_PSC_RST_RX);
340 out_8(&psc->command,MPC52xx_PSC_RST_TX);
341
342 /* Send new mode settings */
343 out_8(&psc->command,MPC52xx_PSC_SEL_MODE_REG_1);
344 out_8(&psc->mode,mr1);
345 out_8(&psc->mode,mr2);
346 out_8(&psc->ctur,ctr >> 8);
347 out_8(&psc->ctlr,ctr & 0xff);
Grant Likely9b9129e2006-11-27 14:21:01 -0700348
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 /* Reenable TX & RX */
350 out_8(&psc->command,MPC52xx_PSC_TX_ENABLE);
351 out_8(&psc->command,MPC52xx_PSC_RX_ENABLE);
352
353 /* We're all set, release the lock */
354 spin_unlock_irqrestore(&port->lock, flags);
355}
356
357static const char *
358mpc52xx_uart_type(struct uart_port *port)
359{
360 return port->type == PORT_MPC52xx ? "MPC52xx PSC" : NULL;
361}
362
363static void
364mpc52xx_uart_release_port(struct uart_port *port)
365{
366 if (port->flags & UPF_IOREMAP) { /* remapped by us ? */
367 iounmap(port->membase);
368 port->membase = NULL;
369 }
370
Grant Likelyb9272df2006-11-27 14:21:02 -0700371 release_mem_region(port->mapbase, sizeof(struct mpc52xx_psc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372}
373
374static int
375mpc52xx_uart_request_port(struct uart_port *port)
376{
Amol Ladbe618f52006-09-30 23:29:23 -0700377 int err;
378
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 if (port->flags & UPF_IOREMAP) /* Need to remap ? */
Grant Likelyb9272df2006-11-27 14:21:02 -0700380 port->membase = ioremap(port->mapbase,
381 sizeof(struct mpc52xx_psc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
383 if (!port->membase)
384 return -EINVAL;
385
Grant Likelyb9272df2006-11-27 14:21:02 -0700386 err = request_mem_region(port->mapbase, sizeof(struct mpc52xx_psc),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 "mpc52xx_psc_uart") != NULL ? 0 : -EBUSY;
Amol Ladbe618f52006-09-30 23:29:23 -0700388
389 if (err && (port->flags & UPF_IOREMAP)) {
390 iounmap(port->membase);
391 port->membase = NULL;
392 }
393
394 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395}
396
397static void
398mpc52xx_uart_config_port(struct uart_port *port, int flags)
399{
400 if ( (flags & UART_CONFIG_TYPE) &&
401 (mpc52xx_uart_request_port(port) == 0) )
402 port->type = PORT_MPC52xx;
403}
404
405static int
406mpc52xx_uart_verify_port(struct uart_port *port, struct serial_struct *ser)
407{
408 if ( ser->type != PORT_UNKNOWN && ser->type != PORT_MPC52xx )
409 return -EINVAL;
410
411 if ( (ser->irq != port->irq) ||
412 (ser->io_type != SERIAL_IO_MEM) ||
Grant Likely9b9129e2006-11-27 14:21:01 -0700413 (ser->baud_base != port->uartclk) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 (ser->iomem_base != (void*)port->mapbase) ||
415 (ser->hub6 != 0 ) )
416 return -EINVAL;
417
418 return 0;
419}
420
421
422static struct uart_ops mpc52xx_uart_ops = {
423 .tx_empty = mpc52xx_uart_tx_empty,
424 .set_mctrl = mpc52xx_uart_set_mctrl,
425 .get_mctrl = mpc52xx_uart_get_mctrl,
426 .stop_tx = mpc52xx_uart_stop_tx,
427 .start_tx = mpc52xx_uart_start_tx,
428 .send_xchar = mpc52xx_uart_send_xchar,
429 .stop_rx = mpc52xx_uart_stop_rx,
430 .enable_ms = mpc52xx_uart_enable_ms,
431 .break_ctl = mpc52xx_uart_break_ctl,
432 .startup = mpc52xx_uart_startup,
433 .shutdown = mpc52xx_uart_shutdown,
434 .set_termios = mpc52xx_uart_set_termios,
435/* .pm = mpc52xx_uart_pm, Not supported yet */
436/* .set_wake = mpc52xx_uart_set_wake, Not supported yet */
437 .type = mpc52xx_uart_type,
438 .release_port = mpc52xx_uart_release_port,
439 .request_port = mpc52xx_uart_request_port,
440 .config_port = mpc52xx_uart_config_port,
441 .verify_port = mpc52xx_uart_verify_port
442};
443
Grant Likely9b9129e2006-11-27 14:21:01 -0700444
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445/* ======================================================================== */
446/* Interrupt handling */
447/* ======================================================================== */
Grant Likely9b9129e2006-11-27 14:21:01 -0700448
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449static inline int
David Howells7d12e782006-10-05 14:55:46 +0100450mpc52xx_uart_int_rx_chars(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451{
452 struct tty_struct *tty = port->info->tty;
Alan Cox33f0f882006-01-09 20:54:13 -0800453 unsigned char ch, flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 unsigned short status;
455
456 /* While we can read, do so ! */
457 while ( (status = in_be16(&PSC(port)->mpc52xx_psc_status)) &
458 MPC52xx_PSC_SR_RXRDY) {
459
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 /* Get the char */
461 ch = in_8(&PSC(port)->mpc52xx_psc_buffer_8);
462
463 /* Handle sysreq char */
464#ifdef SUPPORT_SYSRQ
David Howells7d12e782006-10-05 14:55:46 +0100465 if (uart_handle_sysrq_char(port, ch)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 port->sysrq = 0;
467 continue;
468 }
469#endif
470
471 /* Store it */
Alan Cox33f0f882006-01-09 20:54:13 -0800472
473 flag = TTY_NORMAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 port->icount.rx++;
Grant Likely9b9129e2006-11-27 14:21:01 -0700475
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 if ( status & (MPC52xx_PSC_SR_PE |
477 MPC52xx_PSC_SR_FE |
Alan Cox33f0f882006-01-09 20:54:13 -0800478 MPC52xx_PSC_SR_RB) ) {
Grant Likely9b9129e2006-11-27 14:21:01 -0700479
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 if (status & MPC52xx_PSC_SR_RB) {
Alan Cox33f0f882006-01-09 20:54:13 -0800481 flag = TTY_BREAK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 uart_handle_break(port);
483 } else if (status & MPC52xx_PSC_SR_PE)
Alan Cox33f0f882006-01-09 20:54:13 -0800484 flag = TTY_PARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 else if (status & MPC52xx_PSC_SR_FE)
Alan Cox33f0f882006-01-09 20:54:13 -0800486 flag = TTY_FRAME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
488 /* Clear error condition */
489 out_8(&PSC(port)->command,MPC52xx_PSC_RST_ERR_STAT);
490
491 }
Alan Cox33f0f882006-01-09 20:54:13 -0800492 tty_insert_flip_char(tty, ch, flag);
493 if (status & MPC52xx_PSC_SR_OE) {
494 /*
495 * Overrun is special, since it's
496 * reported immediately, and doesn't
497 * affect the current character
498 */
499 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
500 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 }
502
503 tty_flip_buffer_push(tty);
Grant Likely9b9129e2006-11-27 14:21:01 -0700504
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 return in_be16(&PSC(port)->mpc52xx_psc_status) & MPC52xx_PSC_SR_RXRDY;
506}
507
508static inline int
509mpc52xx_uart_int_tx_chars(struct uart_port *port)
510{
511 struct circ_buf *xmit = &port->info->xmit;
512
513 /* Process out of band chars */
514 if (port->x_char) {
515 out_8(&PSC(port)->mpc52xx_psc_buffer_8, port->x_char);
516 port->icount.tx++;
517 port->x_char = 0;
518 return 1;
519 }
520
521 /* Nothing to do ? */
522 if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
Russell Kingb129a8c2005-08-31 10:12:14 +0100523 mpc52xx_uart_stop_tx(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 return 0;
525 }
526
527 /* Send chars */
528 while (in_be16(&PSC(port)->mpc52xx_psc_status) & MPC52xx_PSC_SR_TXRDY) {
529 out_8(&PSC(port)->mpc52xx_psc_buffer_8, xmit->buf[xmit->tail]);
530 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
531 port->icount.tx++;
532 if (uart_circ_empty(xmit))
533 break;
534 }
535
536 /* Wake up */
537 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
538 uart_write_wakeup(port);
539
540 /* Maybe we're done after all */
541 if (uart_circ_empty(xmit)) {
Russell Kingb129a8c2005-08-31 10:12:14 +0100542 mpc52xx_uart_stop_tx(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 return 0;
544 }
545
546 return 1;
547}
548
Grant Likely9b9129e2006-11-27 14:21:01 -0700549static irqreturn_t
David Howells7d12e782006-10-05 14:55:46 +0100550mpc52xx_uart_int(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551{
Jeff Garzikc7bec5a2006-10-06 15:00:58 -0400552 struct uart_port *port = dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 unsigned long pass = ISR_PASS_LIMIT;
554 unsigned int keepgoing;
555 unsigned short status;
Grant Likely9b9129e2006-11-27 14:21:01 -0700556
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 spin_lock(&port->lock);
Grant Likely9b9129e2006-11-27 14:21:01 -0700558
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 /* While we have stuff to do, we continue */
560 do {
561 /* If we don't find anything to do, we stop */
Grant Likely9b9129e2006-11-27 14:21:01 -0700562 keepgoing = 0;
563
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 /* Read status */
565 status = in_be16(&PSC(port)->mpc52xx_psc_isr);
566 status &= port->read_status_mask;
Grant Likely9b9129e2006-11-27 14:21:01 -0700567
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 /* Do we need to receive chars ? */
569 /* For this RX interrupts must be on and some chars waiting */
570 if ( status & MPC52xx_PSC_IMR_RXRDY )
David Howells7d12e782006-10-05 14:55:46 +0100571 keepgoing |= mpc52xx_uart_int_rx_chars(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
573 /* Do we need to send chars ? */
574 /* For this, TX must be ready and TX interrupt enabled */
575 if ( status & MPC52xx_PSC_IMR_TXRDY )
576 keepgoing |= mpc52xx_uart_int_tx_chars(port);
Grant Likely9b9129e2006-11-27 14:21:01 -0700577
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 /* Limit number of iteration */
579 if ( !(--pass) )
580 keepgoing = 0;
581
582 } while (keepgoing);
Grant Likely9b9129e2006-11-27 14:21:01 -0700583
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 spin_unlock(&port->lock);
Grant Likely9b9129e2006-11-27 14:21:01 -0700585
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 return IRQ_HANDLED;
587}
588
589
590/* ======================================================================== */
591/* Console ( if applicable ) */
592/* ======================================================================== */
593
594#ifdef CONFIG_SERIAL_MPC52xx_CONSOLE
595
596static void __init
597mpc52xx_console_get_options(struct uart_port *port,
598 int *baud, int *parity, int *bits, int *flow)
599{
600 struct mpc52xx_psc __iomem *psc = PSC(port);
601 unsigned char mr1;
602
Grant Likelyb9272df2006-11-27 14:21:02 -0700603 pr_debug("mpc52xx_console_get_options(port=%p)\n", port);
604
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 /* Read the mode registers */
606 out_8(&psc->command,MPC52xx_PSC_SEL_MODE_REG_1);
607 mr1 = in_8(&psc->mode);
Grant Likely9b9129e2006-11-27 14:21:01 -0700608
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 /* CT{U,L}R are write-only ! */
Grant Likelyb9272df2006-11-27 14:21:02 -0700610 *baud = CONFIG_SERIAL_MPC52xx_CONSOLE_BAUD;
611#if !defined(CONFIG_PPC_MERGE)
612 if (__res.bi_baudrate)
613 *baud = __res.bi_baudrate;
614#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
616 /* Parse them */
617 switch (mr1 & MPC52xx_PSC_MODE_BITS_MASK) {
618 case MPC52xx_PSC_MODE_5_BITS: *bits = 5; break;
619 case MPC52xx_PSC_MODE_6_BITS: *bits = 6; break;
620 case MPC52xx_PSC_MODE_7_BITS: *bits = 7; break;
621 case MPC52xx_PSC_MODE_8_BITS:
622 default: *bits = 8;
623 }
Grant Likely9b9129e2006-11-27 14:21:01 -0700624
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 if (mr1 & MPC52xx_PSC_MODE_PARNONE)
626 *parity = 'n';
627 else
628 *parity = mr1 & MPC52xx_PSC_MODE_PARODD ? 'o' : 'e';
629}
630
Grant Likely9b9129e2006-11-27 14:21:01 -0700631static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632mpc52xx_console_write(struct console *co, const char *s, unsigned int count)
633{
634 struct uart_port *port = &mpc52xx_uart_ports[co->index];
635 struct mpc52xx_psc __iomem *psc = PSC(port);
636 unsigned int i, j;
Grant Likely9b9129e2006-11-27 14:21:01 -0700637
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 /* Disable interrupts */
639 out_be16(&psc->mpc52xx_psc_imr, 0);
640
641 /* Wait the TX buffer to be empty */
Grant Likely9b9129e2006-11-27 14:21:01 -0700642 j = 5000000; /* Maximum wait */
643 while (!(in_be16(&psc->mpc52xx_psc_status) & MPC52xx_PSC_SR_TXEMP) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 --j)
645 udelay(1);
646
647 /* Write all the chars */
Russell Kingd3587882006-03-20 20:00:09 +0000648 for (i = 0; i < count; i++, s++) {
649 /* Line return handling */
650 if (*s == '\n')
651 out_8(&psc->mpc52xx_psc_buffer_8, '\r');
Grant Likely9b9129e2006-11-27 14:21:01 -0700652
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 /* Send the char */
654 out_8(&psc->mpc52xx_psc_buffer_8, *s);
655
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 /* Wait the TX buffer to be empty */
Grant Likely9b9129e2006-11-27 14:21:01 -0700657 j = 20000; /* Maximum wait */
658 while (!(in_be16(&psc->mpc52xx_psc_status) &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 MPC52xx_PSC_SR_TXEMP) && --j)
660 udelay(1);
661 }
662
663 /* Restore interrupt state */
664 out_be16(&psc->mpc52xx_psc_imr, port->read_status_mask);
665}
666
Grant Likelyb9272df2006-11-27 14:21:02 -0700667#if !defined(CONFIG_PPC_MERGE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668static int __init
669mpc52xx_console_setup(struct console *co, char *options)
670{
671 struct uart_port *port = &mpc52xx_uart_ports[co->index];
672
673 int baud = CONFIG_SERIAL_MPC52xx_CONSOLE_BAUD;
674 int bits = 8;
675 int parity = 'n';
676 int flow = 'n';
677
678 if (co->index < 0 || co->index >= MPC52xx_PSC_MAXNUM)
679 return -EINVAL;
Grant Likely9b9129e2006-11-27 14:21:01 -0700680
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 /* Basic port init. Needed since we use some uart_??? func before
682 * real init for early access */
683 spin_lock_init(&port->lock);
684 port->uartclk = __res.bi_ipbfreq / 2; /* Look at CTLR doc */
685 port->ops = &mpc52xx_uart_ops;
686 port->mapbase = MPC52xx_PA(MPC52xx_PSCx_OFFSET(co->index+1));
687
688 /* We ioremap ourself */
689 port->membase = ioremap(port->mapbase, MPC52xx_PSC_SIZE);
690 if (port->membase == NULL)
691 return -EINVAL;
692
693 /* Setup the port parameters accoding to options */
694 if (options)
695 uart_parse_options(options, &baud, &parity, &bits, &flow);
696 else
697 mpc52xx_console_get_options(port, &baud, &parity, &bits, &flow);
698
699 return uart_set_options(port, co, baud, parity, bits, flow);
700}
701
Grant Likelyb9272df2006-11-27 14:21:02 -0700702#else
703
704static int __init
705mpc52xx_console_setup(struct console *co, char *options)
706{
707 struct uart_port *port = &mpc52xx_uart_ports[co->index];
708 struct device_node *np = mpc52xx_uart_nodes[co->index];
709 unsigned int ipb_freq;
710 struct resource res;
711 int ret;
712
713 int baud = CONFIG_SERIAL_MPC52xx_CONSOLE_BAUD;
714 int bits = 8;
715 int parity = 'n';
716 int flow = 'n';
717
718 pr_debug("mpc52xx_console_setup co=%p, co->index=%i, options=%s\n",
719 co, co->index, options);
720
721 if ((co->index < 0) || (co->index > MPC52xx_PSC_MAXNUM)) {
722 pr_debug("PSC%x out of range\n", co->index);
723 return -EINVAL;
724 }
725
726 if (!np) {
727 pr_debug("PSC%x not found in device tree\n", co->index);
728 return -EINVAL;
729 }
730
731 pr_debug("Console on ttyPSC%x is %s\n",
732 co->index, mpc52xx_uart_nodes[co->index]->full_name);
733
734 /* Fetch register locations */
735 if ((ret = of_address_to_resource(np, 0, &res)) != 0) {
736 pr_debug("Could not get resources for PSC%x\n", co->index);
737 return ret;
738 }
739
740 /* Search for bus-frequency property in this node or a parent */
741 if ((ipb_freq = mpc52xx_find_ipb_freq(np)) == 0) {
742 pr_debug("Could not find IPB bus frequency!\n");
743 return -EINVAL;
744 }
745
746 /* Basic port init. Needed since we use some uart_??? func before
747 * real init for early access */
748 spin_lock_init(&port->lock);
749 port->uartclk = ipb_freq / 2;
750 port->ops = &mpc52xx_uart_ops;
751 port->mapbase = res.start;
752 port->membase = ioremap(res.start, sizeof(struct mpc52xx_psc));
753 port->irq = irq_of_parse_and_map(np, 0);
754
755 if (port->membase == NULL)
756 return -EINVAL;
757
758 pr_debug("mpc52xx-psc uart at %lx, mapped to %p, irq=%x, freq=%i\n",
759 port->mapbase, port->membase, port->irq, port->uartclk);
760
761 /* Setup the port parameters accoding to options */
762 if (options)
763 uart_parse_options(options, &baud, &parity, &bits, &flow);
764 else
765 mpc52xx_console_get_options(port, &baud, &parity, &bits, &flow);
766
767 pr_debug("Setting console parameters: %i %i%c1 flow=%c\n",
768 baud, bits, parity, flow);
769
770 return uart_set_options(port, co, baud, parity, bits, flow);
771}
772#endif /* defined(CONFIG_PPC_MERGE) */
773
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
Sylvain Munaut2d8179c2006-01-06 00:11:31 -0800775static struct uart_driver mpc52xx_uart_driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
777static struct console mpc52xx_console = {
Sylvain Munautd62de3a2006-01-06 00:11:32 -0800778 .name = "ttyPSC",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 .write = mpc52xx_console_write,
780 .device = uart_console_device,
781 .setup = mpc52xx_console_setup,
782 .flags = CON_PRINTBUFFER,
Sylvain Munautd62de3a2006-01-06 00:11:32 -0800783 .index = -1, /* Specified on the cmdline (e.g. console=ttyPSC0 ) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 .data = &mpc52xx_uart_driver,
785};
786
Grant Likely9b9129e2006-11-27 14:21:01 -0700787
788static int __init
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789mpc52xx_console_init(void)
790{
Grant Likelyc98750c2007-01-02 15:45:37 -0700791#if defined(CONFIG_PPC_MERGE)
Grant Likelyb9272df2006-11-27 14:21:02 -0700792 mpc52xx_uart_of_enumerate();
Grant Likelyc98750c2007-01-02 15:45:37 -0700793#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 register_console(&mpc52xx_console);
795 return 0;
796}
797
798console_initcall(mpc52xx_console_init);
799
800#define MPC52xx_PSC_CONSOLE &mpc52xx_console
801#else
802#define MPC52xx_PSC_CONSOLE NULL
803#endif
804
805
806/* ======================================================================== */
807/* UART Driver */
808/* ======================================================================== */
809
810static struct uart_driver mpc52xx_uart_driver = {
811 .owner = THIS_MODULE,
812 .driver_name = "mpc52xx_psc_uart",
Sylvain Munautd62de3a2006-01-06 00:11:32 -0800813 .dev_name = "ttyPSC",
Sylvain Munautd62de3a2006-01-06 00:11:32 -0800814 .major = SERIAL_PSC_MAJOR,
815 .minor = SERIAL_PSC_MINOR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 .nr = MPC52xx_PSC_MAXNUM,
817 .cons = MPC52xx_PSC_CONSOLE,
818};
819
820
Grant Likelyb9272df2006-11-27 14:21:02 -0700821#if !defined(CONFIG_PPC_MERGE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822/* ======================================================================== */
823/* Platform Driver */
824/* ======================================================================== */
825
826static int __devinit
Russell King3ae5eae2005-11-09 22:32:44 +0000827mpc52xx_uart_probe(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828{
Russell King3ae5eae2005-11-09 22:32:44 +0000829 struct resource *res = dev->resource;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830
831 struct uart_port *port = NULL;
832 int i, idx, ret;
833
834 /* Check validity & presence */
Andrey Volkov38801e22005-11-12 22:04:06 +0000835 idx = dev->id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 if (idx < 0 || idx >= MPC52xx_PSC_MAXNUM)
837 return -EINVAL;
838
839 if (!mpc52xx_match_psc_function(idx,"uart"))
840 return -ENODEV;
841
842 /* Init the port structure */
843 port = &mpc52xx_uart_ports[idx];
844
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 spin_lock_init(&port->lock);
846 port->uartclk = __res.bi_ipbfreq / 2; /* Look at CTLR doc */
Russell King947deee2006-07-02 20:45:51 +0100847 port->fifosize = 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 port->iotype = UPIO_MEM;
849 port->flags = UPF_BOOT_AUTOCONF |
850 ( uart_console(port) ? 0 : UPF_IOREMAP );
851 port->line = idx;
852 port->ops = &mpc52xx_uart_ops;
Grant Likelyb9272df2006-11-27 14:21:02 -0700853 port->dev = &dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854
855 /* Search for IRQ and mapbase */
Andrey Volkov38801e22005-11-12 22:04:06 +0000856 for (i=0 ; i<dev->num_resources ; i++, res++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 if (res->flags & IORESOURCE_MEM)
858 port->mapbase = res->start;
859 else if (res->flags & IORESOURCE_IRQ)
860 port->irq = res->start;
861 }
862 if (!port->irq || !port->mapbase)
863 return -EINVAL;
864
865 /* Add the port to the uart sub-system */
866 ret = uart_add_one_port(&mpc52xx_uart_driver, port);
867 if (!ret)
Russell King3ae5eae2005-11-09 22:32:44 +0000868 platform_set_drvdata(dev, (void*)port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869
870 return ret;
871}
872
873static int
Russell King3ae5eae2005-11-09 22:32:44 +0000874mpc52xx_uart_remove(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875{
Russell King3ae5eae2005-11-09 22:32:44 +0000876 struct uart_port *port = (struct uart_port *) platform_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
Russell King3ae5eae2005-11-09 22:32:44 +0000878 platform_set_drvdata(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879
880 if (port)
881 uart_remove_one_port(&mpc52xx_uart_driver, port);
882
883 return 0;
884}
885
886#ifdef CONFIG_PM
887static int
Russell King3ae5eae2005-11-09 22:32:44 +0000888mpc52xx_uart_suspend(struct platform_device *dev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889{
Russell King3ae5eae2005-11-09 22:32:44 +0000890 struct uart_port *port = (struct uart_port *) platform_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891
Grant Likely9b9129e2006-11-27 14:21:01 -0700892 if (port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 uart_suspend_port(&mpc52xx_uart_driver, port);
894
895 return 0;
896}
897
898static int
Russell King3ae5eae2005-11-09 22:32:44 +0000899mpc52xx_uart_resume(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900{
Russell King3ae5eae2005-11-09 22:32:44 +0000901 struct uart_port *port = (struct uart_port *) platform_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902
Russell King9480e302005-10-28 09:52:56 -0700903 if (port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 uart_resume_port(&mpc52xx_uart_driver, port);
905
906 return 0;
907}
908#endif
909
Grant Likelyb9272df2006-11-27 14:21:02 -0700910
Russell King3ae5eae2005-11-09 22:32:44 +0000911static struct platform_driver mpc52xx_uart_platform_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 .probe = mpc52xx_uart_probe,
913 .remove = mpc52xx_uart_remove,
914#ifdef CONFIG_PM
915 .suspend = mpc52xx_uart_suspend,
916 .resume = mpc52xx_uart_resume,
917#endif
Russell King3ae5eae2005-11-09 22:32:44 +0000918 .driver = {
919 .name = "mpc52xx-psc",
920 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921};
Grant Likelyb9272df2006-11-27 14:21:02 -0700922#endif /* !defined(CONFIG_PPC_MERGE) */
923
924
925#if defined(CONFIG_PPC_MERGE)
926/* ======================================================================== */
927/* OF Platform Driver */
928/* ======================================================================== */
929
930static int __devinit
931mpc52xx_uart_of_probe(struct of_device *op, const struct of_device_id *match)
932{
933 int idx = -1;
934 unsigned int ipb_freq;
935 struct uart_port *port = NULL;
936 struct resource res;
937 int ret;
938
939 dev_dbg(&op->dev, "mpc52xx_uart_probe(op=%p, match=%p)\n", op, match);
940
941 /* Check validity & presence */
942 for (idx = 0; idx < MPC52xx_PSC_MAXNUM; idx++)
943 if (mpc52xx_uart_nodes[idx] == op->node)
944 break;
945 if (idx >= MPC52xx_PSC_MAXNUM)
946 return -EINVAL;
947 pr_debug("Found %s assigned to ttyPSC%x\n",
948 mpc52xx_uart_nodes[idx]->full_name, idx);
949
950 /* Search for bus-frequency property in this node or a parent */
951 if ((ipb_freq = mpc52xx_find_ipb_freq(op->node)) == 0) {
952 dev_dbg(&op->dev, "Could not find IPB bus frequency!\n");
953 return -EINVAL;
954 }
955
956 /* Init the port structure */
957 port = &mpc52xx_uart_ports[idx];
958
959 spin_lock_init(&port->lock);
960 port->uartclk = ipb_freq / 2;
961 port->fifosize = 512;
962 port->iotype = UPIO_MEM;
963 port->flags = UPF_BOOT_AUTOCONF |
964 ( uart_console(port) ? 0 : UPF_IOREMAP );
965 port->line = idx;
966 port->ops = &mpc52xx_uart_ops;
967 port->dev = &op->dev;
968
969 /* Search for IRQ and mapbase */
970 if ((ret = of_address_to_resource(op->node, 0, &res)) != 0)
971 return ret;
972
973 port->mapbase = res.start;
974 port->irq = irq_of_parse_and_map(op->node, 0);
975
976 dev_dbg(&op->dev, "mpc52xx-psc uart at %lx, irq=%x, freq=%i\n",
977 port->mapbase, port->irq, port->uartclk);
978
979 if ((port->irq==NO_IRQ) || !port->mapbase) {
980 printk(KERN_ERR "Could not allocate resources for PSC\n");
981 return -EINVAL;
982 }
983
984 /* Add the port to the uart sub-system */
985 ret = uart_add_one_port(&mpc52xx_uart_driver, port);
986 if (!ret)
987 dev_set_drvdata(&op->dev, (void*)port);
988
989 return ret;
990}
991
992static int
993mpc52xx_uart_of_remove(struct of_device *op)
994{
995 struct uart_port *port = dev_get_drvdata(&op->dev);
996 dev_set_drvdata(&op->dev, NULL);
997
998 if (port)
999 uart_remove_one_port(&mpc52xx_uart_driver, port);
1000
1001 return 0;
1002}
1003
1004#ifdef CONFIG_PM
1005static int
1006mpc52xx_uart_of_suspend(struct of_device *op, pm_message_t state)
1007{
1008 struct uart_port *port = (struct uart_port *) dev_get_drvdata(&op->dev);
1009
1010 if (port)
1011 uart_suspend_port(&mpc52xx_uart_driver, port);
1012
1013 return 0;
1014}
1015
1016static int
1017mpc52xx_uart_of_resume(struct of_device *op)
1018{
1019 struct uart_port *port = (struct uart_port *) dev_get_drvdata(&op->dev);
1020
1021 if (port)
1022 uart_resume_port(&mpc52xx_uart_driver, port);
1023
1024 return 0;
1025}
1026#endif
1027
1028static void
1029mpc52xx_uart_of_assign(struct device_node *np, int idx)
1030{
1031 int free_idx = -1;
1032 int i;
1033
1034 /* Find the first free node */
1035 for (i = 0; i < MPC52xx_PSC_MAXNUM; i++) {
1036 if (mpc52xx_uart_nodes[i] == NULL) {
1037 free_idx = i;
1038 break;
1039 }
1040 }
1041
1042 if ((idx < 0) || (idx >= MPC52xx_PSC_MAXNUM))
1043 idx = free_idx;
1044
1045 if (idx < 0)
1046 return; /* No free slot; abort */
1047
1048 /* If the slot is already occupied, then swap slots */
1049 if (mpc52xx_uart_nodes[idx] && (free_idx != -1))
1050 mpc52xx_uart_nodes[free_idx] = mpc52xx_uart_nodes[idx];
1051 mpc52xx_uart_nodes[i] = np;
1052}
1053
1054static void
1055mpc52xx_uart_of_enumerate(void)
1056{
1057 static int enum_done = 0;
1058 struct device_node *np;
1059 const unsigned int *devno;
1060 int i;
1061
1062 if (enum_done)
1063 return;
1064
1065 for_each_node_by_type(np, "serial") {
1066 if (!of_match_node(mpc52xx_uart_of_match, np))
1067 continue;
1068
1069 /* Is a particular device number requested? */
Grant Likelye3aba812007-02-12 13:36:55 -07001070 devno = get_property(np, "port-number", NULL);
Grant Likelyb9272df2006-11-27 14:21:02 -07001071 mpc52xx_uart_of_assign(of_node_get(np), devno ? *devno : -1);
1072 }
1073
1074 enum_done = 1;
1075
1076 for (i = 0; i < MPC52xx_PSC_MAXNUM; i++) {
1077 if (mpc52xx_uart_nodes[i])
1078 pr_debug("%s assigned to ttyPSC%x\n",
1079 mpc52xx_uart_nodes[i]->full_name, i);
1080 }
1081}
1082
1083MODULE_DEVICE_TABLE(of, mpc52xx_uart_of_match);
1084
1085static struct of_platform_driver mpc52xx_uart_of_driver = {
1086 .owner = THIS_MODULE,
1087 .name = "mpc52xx-psc-uart",
1088 .match_table = mpc52xx_uart_of_match,
1089 .probe = mpc52xx_uart_of_probe,
1090 .remove = mpc52xx_uart_of_remove,
1091#ifdef CONFIG_PM
1092 .suspend = mpc52xx_uart_of_suspend,
1093 .resume = mpc52xx_uart_of_resume,
1094#endif
1095 .driver = {
1096 .name = "mpc52xx-psc-uart",
1097 },
1098};
1099#endif /* defined(CONFIG_PPC_MERGE) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100
1101
1102/* ======================================================================== */
1103/* Module */
1104/* ======================================================================== */
1105
1106static int __init
1107mpc52xx_uart_init(void)
1108{
1109 int ret;
1110
Grant Likelyb9272df2006-11-27 14:21:02 -07001111 printk(KERN_INFO "Serial: MPC52xx PSC UART driver\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112
Grant Likelyb9272df2006-11-27 14:21:02 -07001113 if ((ret = uart_register_driver(&mpc52xx_uart_driver)) != 0) {
1114 printk(KERN_ERR "%s: uart_register_driver failed (%i)\n",
1115 __FILE__, ret);
1116 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 }
1118
Grant Likelyb9272df2006-11-27 14:21:02 -07001119#if defined(CONFIG_PPC_MERGE)
1120 mpc52xx_uart_of_enumerate();
1121
1122 ret = of_register_platform_driver(&mpc52xx_uart_of_driver);
1123 if (ret) {
1124 printk(KERN_ERR "%s: of_register_platform_driver failed (%i)\n",
1125 __FILE__, ret);
1126 uart_unregister_driver(&mpc52xx_uart_driver);
1127 return ret;
1128 }
1129#else
1130 ret = platform_driver_register(&mpc52xx_uart_platform_driver);
1131 if (ret) {
1132 printk(KERN_ERR "%s: platform_driver_register failed (%i)\n",
1133 __FILE__, ret);
1134 uart_unregister_driver(&mpc52xx_uart_driver);
1135 return ret;
1136 }
1137#endif
1138
1139 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140}
1141
1142static void __exit
1143mpc52xx_uart_exit(void)
1144{
Grant Likelyb9272df2006-11-27 14:21:02 -07001145#if defined(CONFIG_PPC_MERGE)
1146 of_unregister_platform_driver(&mpc52xx_uart_of_driver);
1147#else
Russell King3ae5eae2005-11-09 22:32:44 +00001148 platform_driver_unregister(&mpc52xx_uart_platform_driver);
Grant Likelyb9272df2006-11-27 14:21:02 -07001149#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 uart_unregister_driver(&mpc52xx_uart_driver);
1151}
1152
1153
1154module_init(mpc52xx_uart_init);
1155module_exit(mpc52xx_uart_exit);
1156
1157MODULE_AUTHOR("Sylvain Munaut <tnt@246tNt.com>");
1158MODULE_DESCRIPTION("Freescale MPC52xx PSC UART");
1159MODULE_LICENSE("GPL");