blob: 8e24133166af043a0404a43367786dd206a0b9b4 [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 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 * Copyright (C) 2004-2005 Sylvain Munaut <tnt@246tNt.com>
20 * Copyright (C) 2003 MontaVista, Software, Inc.
Grant Likely9b9129e2006-11-27 14:21:01 -070021 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 * This file is licensed under the terms of the GNU General Public License
23 * version 2. This program is licensed "as is" without any warranty of any
24 * kind, whether express or implied.
25 */
Grant Likely9b9129e2006-11-27 14:21:01 -070026
Linus Torvalds1da177e2005-04-16 15:20:36 -070027/* Platform device Usage :
28 *
29 * Since PSCs can have multiple function, the correct driver for each one
30 * is selected by calling mpc52xx_match_psc_function(...). The function
31 * handled by this driver is "uart".
32 *
33 * The driver init all necessary registers to place the PSC in uart mode without
34 * DCD. However, the pin multiplexing aren't changed and should be set either
35 * by the bootloader or in the platform init code.
36 *
37 * 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 -080038 * and so on). So the PSC1 is mapped to /dev/ttyPSC0, PSC2 to /dev/ttyPSC1 and
39 * so on. But be warned, it's an ABSOLUTE REQUIREMENT ! This is needed mainly
40 * fpr the console code : without this 1:1 mapping, at early boot time, when we
Uwe Zeisbergerc30fe7f2006-03-24 18:23:14 +010041 * are parsing the kernel args console=ttyPSC?, we wouldn't know which PSC it
Sylvain Munautd62de3a2006-01-06 00:11:32 -080042 * will be mapped to.
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 */
44
Russell Kingd052d1b2005-10-29 19:07:23 +010045#include <linux/platform_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <linux/module.h>
47#include <linux/tty.h>
48#include <linux/serial.h>
49#include <linux/sysrq.h>
50#include <linux/console.h>
51
52#include <asm/delay.h>
53#include <asm/io.h>
54
55#include <asm/mpc52xx.h>
56#include <asm/mpc52xx_psc.h>
57
58#if defined(CONFIG_SERIAL_MPC52xx_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
59#define SUPPORT_SYSRQ
60#endif
61
62#include <linux/serial_core.h>
63
64
Sylvain Munautd62de3a2006-01-06 00:11:32 -080065/* We've been assigned a range on the "Low-density serial ports" major */
66#define SERIAL_PSC_MAJOR 204
67#define SERIAL_PSC_MINOR 148
68
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
70#define ISR_PASS_LIMIT 256 /* Max number of iteration in the interrupt */
71
72
73static struct uart_port mpc52xx_uart_ports[MPC52xx_PSC_MAXNUM];
74 /* Rem: - We use the read_status_mask as a shadow of
75 * psc->mpc52xx_psc_imr
76 * - It's important that is array is all zero on start as we
77 * use it to know if it's initialized or not ! If it's not sure
78 * it's cleared, then a memset(...,0,...) should be added to
79 * the console_init
80 */
81
82#define PSC(port) ((struct mpc52xx_psc __iomem *)((port)->membase))
83
84
85/* Forward declaration of the interruption handling routine */
David Howells7d12e782006-10-05 14:55:46 +010086static irqreturn_t mpc52xx_uart_int(int irq,void *dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
88
89/* Simple macro to test if a port is console or not. This one is taken
90 * for serial_core.c and maybe should be moved to serial_core.h ? */
91#ifdef CONFIG_SERIAL_CORE_CONSOLE
92#define uart_console(port) ((port)->cons && (port)->cons->index == (port)->line)
93#else
94#define uart_console(port) (0)
95#endif
96
97
98/* ======================================================================== */
99/* UART operations */
100/* ======================================================================== */
101
Grant Likely9b9129e2006-11-27 14:21:01 -0700102static unsigned int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103mpc52xx_uart_tx_empty(struct uart_port *port)
104{
105 int status = in_be16(&PSC(port)->mpc52xx_psc_status);
106 return (status & MPC52xx_PSC_SR_TXEMP) ? TIOCSER_TEMT : 0;
107}
108
Grant Likely9b9129e2006-11-27 14:21:01 -0700109static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110mpc52xx_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
111{
112 /* Not implemented */
113}
114
Grant Likely9b9129e2006-11-27 14:21:01 -0700115static unsigned int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116mpc52xx_uart_get_mctrl(struct uart_port *port)
117{
118 /* Not implemented */
119 return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
120}
121
Grant Likely9b9129e2006-11-27 14:21:01 -0700122static void
Russell Kingb129a8c2005-08-31 10:12:14 +0100123mpc52xx_uart_stop_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124{
125 /* port->lock taken by caller */
126 port->read_status_mask &= ~MPC52xx_PSC_IMR_TXRDY;
127 out_be16(&PSC(port)->mpc52xx_psc_imr,port->read_status_mask);
128}
129
Grant Likely9b9129e2006-11-27 14:21:01 -0700130static void
Russell Kingb129a8c2005-08-31 10:12:14 +0100131mpc52xx_uart_start_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132{
133 /* port->lock taken by caller */
134 port->read_status_mask |= MPC52xx_PSC_IMR_TXRDY;
135 out_be16(&PSC(port)->mpc52xx_psc_imr,port->read_status_mask);
136}
137
Grant Likely9b9129e2006-11-27 14:21:01 -0700138static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139mpc52xx_uart_send_xchar(struct uart_port *port, char ch)
140{
141 unsigned long flags;
142 spin_lock_irqsave(&port->lock, flags);
Grant Likely9b9129e2006-11-27 14:21:01 -0700143
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 port->x_char = ch;
145 if (ch) {
146 /* Make sure tx interrupts are on */
147 /* Truly necessary ??? They should be anyway */
148 port->read_status_mask |= MPC52xx_PSC_IMR_TXRDY;
149 out_be16(&PSC(port)->mpc52xx_psc_imr,port->read_status_mask);
150 }
Grant Likely9b9129e2006-11-27 14:21:01 -0700151
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 spin_unlock_irqrestore(&port->lock, flags);
153}
154
155static void
156mpc52xx_uart_stop_rx(struct uart_port *port)
157{
158 /* port->lock taken by caller */
159 port->read_status_mask &= ~MPC52xx_PSC_IMR_RXRDY;
160 out_be16(&PSC(port)->mpc52xx_psc_imr,port->read_status_mask);
161}
162
163static void
164mpc52xx_uart_enable_ms(struct uart_port *port)
165{
166 /* Not implemented */
167}
168
169static void
170mpc52xx_uart_break_ctl(struct uart_port *port, int ctl)
171{
172 unsigned long flags;
173 spin_lock_irqsave(&port->lock, flags);
174
175 if ( ctl == -1 )
176 out_8(&PSC(port)->command,MPC52xx_PSC_START_BRK);
177 else
178 out_8(&PSC(port)->command,MPC52xx_PSC_STOP_BRK);
Grant Likely9b9129e2006-11-27 14:21:01 -0700179
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 spin_unlock_irqrestore(&port->lock, flags);
181}
182
183static int
184mpc52xx_uart_startup(struct uart_port *port)
185{
186 struct mpc52xx_psc __iomem *psc = PSC(port);
187 int ret;
188
189 /* Request IRQ */
190 ret = request_irq(port->irq, mpc52xx_uart_int,
Thomas Gleixner40663cc2006-07-01 19:29:43 -0700191 IRQF_DISABLED | IRQF_SAMPLE_RANDOM, "mpc52xx_psc_uart", port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 if (ret)
193 return ret;
194
195 /* Reset/activate the port, clear and enable interrupts */
196 out_8(&psc->command,MPC52xx_PSC_RST_RX);
197 out_8(&psc->command,MPC52xx_PSC_RST_TX);
Grant Likely9b9129e2006-11-27 14:21:01 -0700198
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 out_be32(&psc->sicr,0); /* UART mode DCD ignored */
200
201 out_be16(&psc->mpc52xx_psc_clock_select, 0xdd00); /* /16 prescaler on */
Grant Likely9b9129e2006-11-27 14:21:01 -0700202
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 out_8(&psc->rfcntl, 0x00);
204 out_be16(&psc->rfalarm, 0x1ff);
205 out_8(&psc->tfcntl, 0x07);
206 out_be16(&psc->tfalarm, 0x80);
207
208 port->read_status_mask |= MPC52xx_PSC_IMR_RXRDY | MPC52xx_PSC_IMR_TXRDY;
209 out_be16(&psc->mpc52xx_psc_imr,port->read_status_mask);
Grant Likely9b9129e2006-11-27 14:21:01 -0700210
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 out_8(&psc->command,MPC52xx_PSC_TX_ENABLE);
212 out_8(&psc->command,MPC52xx_PSC_RX_ENABLE);
Grant Likely9b9129e2006-11-27 14:21:01 -0700213
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 return 0;
215}
216
217static void
218mpc52xx_uart_shutdown(struct uart_port *port)
219{
220 struct mpc52xx_psc __iomem *psc = PSC(port);
Grant Likely9b9129e2006-11-27 14:21:01 -0700221
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 /* Shut down the port, interrupt and all */
223 out_8(&psc->command,MPC52xx_PSC_RST_RX);
224 out_8(&psc->command,MPC52xx_PSC_RST_TX);
Grant Likely9b9129e2006-11-27 14:21:01 -0700225
226 port->read_status_mask = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 out_be16(&psc->mpc52xx_psc_imr,port->read_status_mask);
228
229 /* Release interrupt */
230 free_irq(port->irq, port);
231}
232
Grant Likely9b9129e2006-11-27 14:21:01 -0700233static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234mpc52xx_uart_set_termios(struct uart_port *port, struct termios *new,
235 struct termios *old)
236{
237 struct mpc52xx_psc __iomem *psc = PSC(port);
238 unsigned long flags;
239 unsigned char mr1, mr2;
240 unsigned short ctr;
241 unsigned int j, baud, quot;
Grant Likely9b9129e2006-11-27 14:21:01 -0700242
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 /* Prepare what we're gonna write */
244 mr1 = 0;
Grant Likely9b9129e2006-11-27 14:21:01 -0700245
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 switch (new->c_cflag & CSIZE) {
247 case CS5: mr1 |= MPC52xx_PSC_MODE_5_BITS;
248 break;
249 case CS6: mr1 |= MPC52xx_PSC_MODE_6_BITS;
250 break;
251 case CS7: mr1 |= MPC52xx_PSC_MODE_7_BITS;
252 break;
253 case CS8:
254 default: mr1 |= MPC52xx_PSC_MODE_8_BITS;
255 }
256
257 if (new->c_cflag & PARENB) {
258 mr1 |= (new->c_cflag & PARODD) ?
259 MPC52xx_PSC_MODE_PARODD : MPC52xx_PSC_MODE_PAREVEN;
260 } else
261 mr1 |= MPC52xx_PSC_MODE_PARNONE;
Grant Likely9b9129e2006-11-27 14:21:01 -0700262
263
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 mr2 = 0;
265
266 if (new->c_cflag & CSTOPB)
267 mr2 |= MPC52xx_PSC_MODE_TWO_STOP;
268 else
269 mr2 |= ((new->c_cflag & CSIZE) == CS5) ?
270 MPC52xx_PSC_MODE_ONE_STOP_5_BITS :
271 MPC52xx_PSC_MODE_ONE_STOP;
272
273
274 baud = uart_get_baud_rate(port, new, old, 0, port->uartclk/16);
275 quot = uart_get_divisor(port, baud);
276 ctr = quot & 0xffff;
Grant Likely9b9129e2006-11-27 14:21:01 -0700277
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 /* Get the lock */
279 spin_lock_irqsave(&port->lock, flags);
280
281 /* Update the per-port timeout */
282 uart_update_timeout(port, new->c_cflag, baud);
283
284 /* Do our best to flush TX & RX, so we don't loose anything */
285 /* But we don't wait indefinitly ! */
286 j = 5000000; /* Maximum wait */
287 /* FIXME Can't receive chars since set_termios might be called at early
288 * boot for the console, all stuff is not yet ready to receive at that
289 * time and that just makes the kernel oops */
290 /* while (j-- && mpc52xx_uart_int_rx_chars(port)); */
Grant Likely9b9129e2006-11-27 14:21:01 -0700291 while (!(in_be16(&psc->mpc52xx_psc_status) & MPC52xx_PSC_SR_TXEMP) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 --j)
293 udelay(1);
294
295 if (!j)
296 printk( KERN_ERR "mpc52xx_uart.c: "
297 "Unable to flush RX & TX fifos in-time in set_termios."
Grant Likely9b9129e2006-11-27 14:21:01 -0700298 "Some chars may have been lost.\n" );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
300 /* Reset the TX & RX */
301 out_8(&psc->command,MPC52xx_PSC_RST_RX);
302 out_8(&psc->command,MPC52xx_PSC_RST_TX);
303
304 /* Send new mode settings */
305 out_8(&psc->command,MPC52xx_PSC_SEL_MODE_REG_1);
306 out_8(&psc->mode,mr1);
307 out_8(&psc->mode,mr2);
308 out_8(&psc->ctur,ctr >> 8);
309 out_8(&psc->ctlr,ctr & 0xff);
Grant Likely9b9129e2006-11-27 14:21:01 -0700310
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 /* Reenable TX & RX */
312 out_8(&psc->command,MPC52xx_PSC_TX_ENABLE);
313 out_8(&psc->command,MPC52xx_PSC_RX_ENABLE);
314
315 /* We're all set, release the lock */
316 spin_unlock_irqrestore(&port->lock, flags);
317}
318
319static const char *
320mpc52xx_uart_type(struct uart_port *port)
321{
322 return port->type == PORT_MPC52xx ? "MPC52xx PSC" : NULL;
323}
324
325static void
326mpc52xx_uart_release_port(struct uart_port *port)
327{
328 if (port->flags & UPF_IOREMAP) { /* remapped by us ? */
329 iounmap(port->membase);
330 port->membase = NULL;
331 }
332
333 release_mem_region(port->mapbase, MPC52xx_PSC_SIZE);
334}
335
336static int
337mpc52xx_uart_request_port(struct uart_port *port)
338{
Amol Ladbe618f52006-09-30 23:29:23 -0700339 int err;
340
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 if (port->flags & UPF_IOREMAP) /* Need to remap ? */
342 port->membase = ioremap(port->mapbase, MPC52xx_PSC_SIZE);
343
344 if (!port->membase)
345 return -EINVAL;
346
Amol Ladbe618f52006-09-30 23:29:23 -0700347 err = request_mem_region(port->mapbase, MPC52xx_PSC_SIZE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 "mpc52xx_psc_uart") != NULL ? 0 : -EBUSY;
Amol Ladbe618f52006-09-30 23:29:23 -0700349
350 if (err && (port->flags & UPF_IOREMAP)) {
351 iounmap(port->membase);
352 port->membase = NULL;
353 }
354
355 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356}
357
358static void
359mpc52xx_uart_config_port(struct uart_port *port, int flags)
360{
361 if ( (flags & UART_CONFIG_TYPE) &&
362 (mpc52xx_uart_request_port(port) == 0) )
363 port->type = PORT_MPC52xx;
364}
365
366static int
367mpc52xx_uart_verify_port(struct uart_port *port, struct serial_struct *ser)
368{
369 if ( ser->type != PORT_UNKNOWN && ser->type != PORT_MPC52xx )
370 return -EINVAL;
371
372 if ( (ser->irq != port->irq) ||
373 (ser->io_type != SERIAL_IO_MEM) ||
Grant Likely9b9129e2006-11-27 14:21:01 -0700374 (ser->baud_base != port->uartclk) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 (ser->iomem_base != (void*)port->mapbase) ||
376 (ser->hub6 != 0 ) )
377 return -EINVAL;
378
379 return 0;
380}
381
382
383static struct uart_ops mpc52xx_uart_ops = {
384 .tx_empty = mpc52xx_uart_tx_empty,
385 .set_mctrl = mpc52xx_uart_set_mctrl,
386 .get_mctrl = mpc52xx_uart_get_mctrl,
387 .stop_tx = mpc52xx_uart_stop_tx,
388 .start_tx = mpc52xx_uart_start_tx,
389 .send_xchar = mpc52xx_uart_send_xchar,
390 .stop_rx = mpc52xx_uart_stop_rx,
391 .enable_ms = mpc52xx_uart_enable_ms,
392 .break_ctl = mpc52xx_uart_break_ctl,
393 .startup = mpc52xx_uart_startup,
394 .shutdown = mpc52xx_uart_shutdown,
395 .set_termios = mpc52xx_uart_set_termios,
396/* .pm = mpc52xx_uart_pm, Not supported yet */
397/* .set_wake = mpc52xx_uart_set_wake, Not supported yet */
398 .type = mpc52xx_uart_type,
399 .release_port = mpc52xx_uart_release_port,
400 .request_port = mpc52xx_uart_request_port,
401 .config_port = mpc52xx_uart_config_port,
402 .verify_port = mpc52xx_uart_verify_port
403};
404
Grant Likely9b9129e2006-11-27 14:21:01 -0700405
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406/* ======================================================================== */
407/* Interrupt handling */
408/* ======================================================================== */
Grant Likely9b9129e2006-11-27 14:21:01 -0700409
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410static inline int
David Howells7d12e782006-10-05 14:55:46 +0100411mpc52xx_uart_int_rx_chars(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412{
413 struct tty_struct *tty = port->info->tty;
Alan Cox33f0f882006-01-09 20:54:13 -0800414 unsigned char ch, flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 unsigned short status;
416
417 /* While we can read, do so ! */
418 while ( (status = in_be16(&PSC(port)->mpc52xx_psc_status)) &
419 MPC52xx_PSC_SR_RXRDY) {
420
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 /* Get the char */
422 ch = in_8(&PSC(port)->mpc52xx_psc_buffer_8);
423
424 /* Handle sysreq char */
425#ifdef SUPPORT_SYSRQ
David Howells7d12e782006-10-05 14:55:46 +0100426 if (uart_handle_sysrq_char(port, ch)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 port->sysrq = 0;
428 continue;
429 }
430#endif
431
432 /* Store it */
Alan Cox33f0f882006-01-09 20:54:13 -0800433
434 flag = TTY_NORMAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 port->icount.rx++;
Grant Likely9b9129e2006-11-27 14:21:01 -0700436
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 if ( status & (MPC52xx_PSC_SR_PE |
438 MPC52xx_PSC_SR_FE |
Alan Cox33f0f882006-01-09 20:54:13 -0800439 MPC52xx_PSC_SR_RB) ) {
Grant Likely9b9129e2006-11-27 14:21:01 -0700440
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 if (status & MPC52xx_PSC_SR_RB) {
Alan Cox33f0f882006-01-09 20:54:13 -0800442 flag = TTY_BREAK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 uart_handle_break(port);
444 } else if (status & MPC52xx_PSC_SR_PE)
Alan Cox33f0f882006-01-09 20:54:13 -0800445 flag = TTY_PARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 else if (status & MPC52xx_PSC_SR_FE)
Alan Cox33f0f882006-01-09 20:54:13 -0800447 flag = TTY_FRAME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
449 /* Clear error condition */
450 out_8(&PSC(port)->command,MPC52xx_PSC_RST_ERR_STAT);
451
452 }
Alan Cox33f0f882006-01-09 20:54:13 -0800453 tty_insert_flip_char(tty, ch, flag);
454 if (status & MPC52xx_PSC_SR_OE) {
455 /*
456 * Overrun is special, since it's
457 * reported immediately, and doesn't
458 * affect the current character
459 */
460 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
461 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 }
463
464 tty_flip_buffer_push(tty);
Grant Likely9b9129e2006-11-27 14:21:01 -0700465
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 return in_be16(&PSC(port)->mpc52xx_psc_status) & MPC52xx_PSC_SR_RXRDY;
467}
468
469static inline int
470mpc52xx_uart_int_tx_chars(struct uart_port *port)
471{
472 struct circ_buf *xmit = &port->info->xmit;
473
474 /* Process out of band chars */
475 if (port->x_char) {
476 out_8(&PSC(port)->mpc52xx_psc_buffer_8, port->x_char);
477 port->icount.tx++;
478 port->x_char = 0;
479 return 1;
480 }
481
482 /* Nothing to do ? */
483 if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
Russell Kingb129a8c2005-08-31 10:12:14 +0100484 mpc52xx_uart_stop_tx(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 return 0;
486 }
487
488 /* Send chars */
489 while (in_be16(&PSC(port)->mpc52xx_psc_status) & MPC52xx_PSC_SR_TXRDY) {
490 out_8(&PSC(port)->mpc52xx_psc_buffer_8, xmit->buf[xmit->tail]);
491 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
492 port->icount.tx++;
493 if (uart_circ_empty(xmit))
494 break;
495 }
496
497 /* Wake up */
498 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
499 uart_write_wakeup(port);
500
501 /* Maybe we're done after all */
502 if (uart_circ_empty(xmit)) {
Russell Kingb129a8c2005-08-31 10:12:14 +0100503 mpc52xx_uart_stop_tx(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 return 0;
505 }
506
507 return 1;
508}
509
Grant Likely9b9129e2006-11-27 14:21:01 -0700510static irqreturn_t
David Howells7d12e782006-10-05 14:55:46 +0100511mpc52xx_uart_int(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512{
Jeff Garzikc7bec5a2006-10-06 15:00:58 -0400513 struct uart_port *port = dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 unsigned long pass = ISR_PASS_LIMIT;
515 unsigned int keepgoing;
516 unsigned short status;
Grant Likely9b9129e2006-11-27 14:21:01 -0700517
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 spin_lock(&port->lock);
Grant Likely9b9129e2006-11-27 14:21:01 -0700519
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 /* While we have stuff to do, we continue */
521 do {
522 /* If we don't find anything to do, we stop */
Grant Likely9b9129e2006-11-27 14:21:01 -0700523 keepgoing = 0;
524
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 /* Read status */
526 status = in_be16(&PSC(port)->mpc52xx_psc_isr);
527 status &= port->read_status_mask;
Grant Likely9b9129e2006-11-27 14:21:01 -0700528
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 /* Do we need to receive chars ? */
530 /* For this RX interrupts must be on and some chars waiting */
531 if ( status & MPC52xx_PSC_IMR_RXRDY )
David Howells7d12e782006-10-05 14:55:46 +0100532 keepgoing |= mpc52xx_uart_int_rx_chars(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
534 /* Do we need to send chars ? */
535 /* For this, TX must be ready and TX interrupt enabled */
536 if ( status & MPC52xx_PSC_IMR_TXRDY )
537 keepgoing |= mpc52xx_uart_int_tx_chars(port);
Grant Likely9b9129e2006-11-27 14:21:01 -0700538
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 /* Limit number of iteration */
540 if ( !(--pass) )
541 keepgoing = 0;
542
543 } while (keepgoing);
Grant Likely9b9129e2006-11-27 14:21:01 -0700544
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 spin_unlock(&port->lock);
Grant Likely9b9129e2006-11-27 14:21:01 -0700546
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 return IRQ_HANDLED;
548}
549
550
551/* ======================================================================== */
552/* Console ( if applicable ) */
553/* ======================================================================== */
554
555#ifdef CONFIG_SERIAL_MPC52xx_CONSOLE
556
557static void __init
558mpc52xx_console_get_options(struct uart_port *port,
559 int *baud, int *parity, int *bits, int *flow)
560{
561 struct mpc52xx_psc __iomem *psc = PSC(port);
562 unsigned char mr1;
563
564 /* Read the mode registers */
565 out_8(&psc->command,MPC52xx_PSC_SEL_MODE_REG_1);
566 mr1 = in_8(&psc->mode);
Grant Likely9b9129e2006-11-27 14:21:01 -0700567
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 /* CT{U,L}R are write-only ! */
569 *baud = __res.bi_baudrate ?
570 __res.bi_baudrate : CONFIG_SERIAL_MPC52xx_CONSOLE_BAUD;
571
572 /* Parse them */
573 switch (mr1 & MPC52xx_PSC_MODE_BITS_MASK) {
574 case MPC52xx_PSC_MODE_5_BITS: *bits = 5; break;
575 case MPC52xx_PSC_MODE_6_BITS: *bits = 6; break;
576 case MPC52xx_PSC_MODE_7_BITS: *bits = 7; break;
577 case MPC52xx_PSC_MODE_8_BITS:
578 default: *bits = 8;
579 }
Grant Likely9b9129e2006-11-27 14:21:01 -0700580
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 if (mr1 & MPC52xx_PSC_MODE_PARNONE)
582 *parity = 'n';
583 else
584 *parity = mr1 & MPC52xx_PSC_MODE_PARODD ? 'o' : 'e';
585}
586
Grant Likely9b9129e2006-11-27 14:21:01 -0700587static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588mpc52xx_console_write(struct console *co, const char *s, unsigned int count)
589{
590 struct uart_port *port = &mpc52xx_uart_ports[co->index];
591 struct mpc52xx_psc __iomem *psc = PSC(port);
592 unsigned int i, j;
Grant Likely9b9129e2006-11-27 14:21:01 -0700593
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 /* Disable interrupts */
595 out_be16(&psc->mpc52xx_psc_imr, 0);
596
597 /* Wait the TX buffer to be empty */
Grant Likely9b9129e2006-11-27 14:21:01 -0700598 j = 5000000; /* Maximum wait */
599 while (!(in_be16(&psc->mpc52xx_psc_status) & MPC52xx_PSC_SR_TXEMP) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 --j)
601 udelay(1);
602
603 /* Write all the chars */
Russell Kingd3587882006-03-20 20:00:09 +0000604 for (i = 0; i < count; i++, s++) {
605 /* Line return handling */
606 if (*s == '\n')
607 out_8(&psc->mpc52xx_psc_buffer_8, '\r');
Grant Likely9b9129e2006-11-27 14:21:01 -0700608
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 /* Send the char */
610 out_8(&psc->mpc52xx_psc_buffer_8, *s);
611
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 /* Wait the TX buffer to be empty */
Grant Likely9b9129e2006-11-27 14:21:01 -0700613 j = 20000; /* Maximum wait */
614 while (!(in_be16(&psc->mpc52xx_psc_status) &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 MPC52xx_PSC_SR_TXEMP) && --j)
616 udelay(1);
617 }
618
619 /* Restore interrupt state */
620 out_be16(&psc->mpc52xx_psc_imr, port->read_status_mask);
621}
622
623static int __init
624mpc52xx_console_setup(struct console *co, char *options)
625{
626 struct uart_port *port = &mpc52xx_uart_ports[co->index];
627
628 int baud = CONFIG_SERIAL_MPC52xx_CONSOLE_BAUD;
629 int bits = 8;
630 int parity = 'n';
631 int flow = 'n';
632
633 if (co->index < 0 || co->index >= MPC52xx_PSC_MAXNUM)
634 return -EINVAL;
Grant Likely9b9129e2006-11-27 14:21:01 -0700635
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 /* Basic port init. Needed since we use some uart_??? func before
637 * real init for early access */
638 spin_lock_init(&port->lock);
639 port->uartclk = __res.bi_ipbfreq / 2; /* Look at CTLR doc */
640 port->ops = &mpc52xx_uart_ops;
641 port->mapbase = MPC52xx_PA(MPC52xx_PSCx_OFFSET(co->index+1));
642
643 /* We ioremap ourself */
644 port->membase = ioremap(port->mapbase, MPC52xx_PSC_SIZE);
645 if (port->membase == NULL)
646 return -EINVAL;
647
648 /* Setup the port parameters accoding to options */
649 if (options)
650 uart_parse_options(options, &baud, &parity, &bits, &flow);
651 else
652 mpc52xx_console_get_options(port, &baud, &parity, &bits, &flow);
653
654 return uart_set_options(port, co, baud, parity, bits, flow);
655}
656
657
Sylvain Munaut2d8179c2006-01-06 00:11:31 -0800658static struct uart_driver mpc52xx_uart_driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
660static struct console mpc52xx_console = {
Sylvain Munautd62de3a2006-01-06 00:11:32 -0800661 .name = "ttyPSC",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 .write = mpc52xx_console_write,
663 .device = uart_console_device,
664 .setup = mpc52xx_console_setup,
665 .flags = CON_PRINTBUFFER,
Sylvain Munautd62de3a2006-01-06 00:11:32 -0800666 .index = -1, /* Specified on the cmdline (e.g. console=ttyPSC0 ) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 .data = &mpc52xx_uart_driver,
668};
669
Grant Likely9b9129e2006-11-27 14:21:01 -0700670
671static int __init
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672mpc52xx_console_init(void)
673{
674 register_console(&mpc52xx_console);
675 return 0;
676}
677
678console_initcall(mpc52xx_console_init);
679
680#define MPC52xx_PSC_CONSOLE &mpc52xx_console
681#else
682#define MPC52xx_PSC_CONSOLE NULL
683#endif
684
685
686/* ======================================================================== */
687/* UART Driver */
688/* ======================================================================== */
689
690static struct uart_driver mpc52xx_uart_driver = {
691 .owner = THIS_MODULE,
692 .driver_name = "mpc52xx_psc_uart",
Sylvain Munautd62de3a2006-01-06 00:11:32 -0800693 .dev_name = "ttyPSC",
Sylvain Munautd62de3a2006-01-06 00:11:32 -0800694 .major = SERIAL_PSC_MAJOR,
695 .minor = SERIAL_PSC_MINOR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 .nr = MPC52xx_PSC_MAXNUM,
697 .cons = MPC52xx_PSC_CONSOLE,
698};
699
700
701/* ======================================================================== */
702/* Platform Driver */
703/* ======================================================================== */
704
705static int __devinit
Russell King3ae5eae2005-11-09 22:32:44 +0000706mpc52xx_uart_probe(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707{
Russell King3ae5eae2005-11-09 22:32:44 +0000708 struct resource *res = dev->resource;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
710 struct uart_port *port = NULL;
711 int i, idx, ret;
712
713 /* Check validity & presence */
Andrey Volkov38801e22005-11-12 22:04:06 +0000714 idx = dev->id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 if (idx < 0 || idx >= MPC52xx_PSC_MAXNUM)
716 return -EINVAL;
717
718 if (!mpc52xx_match_psc_function(idx,"uart"))
719 return -ENODEV;
720
721 /* Init the port structure */
722 port = &mpc52xx_uart_ports[idx];
723
724 memset(port, 0x00, sizeof(struct uart_port));
725
726 spin_lock_init(&port->lock);
727 port->uartclk = __res.bi_ipbfreq / 2; /* Look at CTLR doc */
Russell King947deee2006-07-02 20:45:51 +0100728 port->fifosize = 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 port->iotype = UPIO_MEM;
730 port->flags = UPF_BOOT_AUTOCONF |
731 ( uart_console(port) ? 0 : UPF_IOREMAP );
732 port->line = idx;
733 port->ops = &mpc52xx_uart_ops;
734
735 /* Search for IRQ and mapbase */
Andrey Volkov38801e22005-11-12 22:04:06 +0000736 for (i=0 ; i<dev->num_resources ; i++, res++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 if (res->flags & IORESOURCE_MEM)
738 port->mapbase = res->start;
739 else if (res->flags & IORESOURCE_IRQ)
740 port->irq = res->start;
741 }
742 if (!port->irq || !port->mapbase)
743 return -EINVAL;
744
745 /* Add the port to the uart sub-system */
746 ret = uart_add_one_port(&mpc52xx_uart_driver, port);
747 if (!ret)
Russell King3ae5eae2005-11-09 22:32:44 +0000748 platform_set_drvdata(dev, (void*)port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
750 return ret;
751}
752
753static int
Russell King3ae5eae2005-11-09 22:32:44 +0000754mpc52xx_uart_remove(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755{
Russell King3ae5eae2005-11-09 22:32:44 +0000756 struct uart_port *port = (struct uart_port *) platform_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
Russell King3ae5eae2005-11-09 22:32:44 +0000758 platform_set_drvdata(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
760 if (port)
761 uart_remove_one_port(&mpc52xx_uart_driver, port);
762
763 return 0;
764}
765
766#ifdef CONFIG_PM
767static int
Russell King3ae5eae2005-11-09 22:32:44 +0000768mpc52xx_uart_suspend(struct platform_device *dev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769{
Russell King3ae5eae2005-11-09 22:32:44 +0000770 struct uart_port *port = (struct uart_port *) platform_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
Grant Likely9b9129e2006-11-27 14:21:01 -0700772 if (port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 uart_suspend_port(&mpc52xx_uart_driver, port);
774
775 return 0;
776}
777
778static int
Russell King3ae5eae2005-11-09 22:32:44 +0000779mpc52xx_uart_resume(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780{
Russell King3ae5eae2005-11-09 22:32:44 +0000781 struct uart_port *port = (struct uart_port *) platform_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
Russell King9480e302005-10-28 09:52:56 -0700783 if (port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 uart_resume_port(&mpc52xx_uart_driver, port);
785
786 return 0;
787}
788#endif
789
Russell King3ae5eae2005-11-09 22:32:44 +0000790static struct platform_driver mpc52xx_uart_platform_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 .probe = mpc52xx_uart_probe,
792 .remove = mpc52xx_uart_remove,
793#ifdef CONFIG_PM
794 .suspend = mpc52xx_uart_suspend,
795 .resume = mpc52xx_uart_resume,
796#endif
Russell King3ae5eae2005-11-09 22:32:44 +0000797 .driver = {
798 .name = "mpc52xx-psc",
799 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800};
801
802
803/* ======================================================================== */
804/* Module */
805/* ======================================================================== */
806
807static int __init
808mpc52xx_uart_init(void)
809{
810 int ret;
811
812 printk(KERN_INFO "Serial: MPC52xx PSC driver\n");
813
814 ret = uart_register_driver(&mpc52xx_uart_driver);
815 if (ret == 0) {
Russell King3ae5eae2005-11-09 22:32:44 +0000816 ret = platform_driver_register(&mpc52xx_uart_platform_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 if (ret)
818 uart_unregister_driver(&mpc52xx_uart_driver);
819 }
820
821 return ret;
822}
823
824static void __exit
825mpc52xx_uart_exit(void)
826{
Russell King3ae5eae2005-11-09 22:32:44 +0000827 platform_driver_unregister(&mpc52xx_uart_platform_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 uart_unregister_driver(&mpc52xx_uart_driver);
829}
830
831
832module_init(mpc52xx_uart_init);
833module_exit(mpc52xx_uart_exit);
834
835MODULE_AUTHOR("Sylvain Munaut <tnt@246tNt.com>");
836MODULE_DESCRIPTION("Freescale MPC52xx PSC UART");
837MODULE_LICENSE("GPL");