[POWERPC] Separate MPC52xx PSC FIFO registers from rest of PSC
[deliverable/linux.git] / drivers / serial / mpc52xx_uart.c
1 /*
2 * 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>
15 *
16 * Some of the code has been inspired/copied from the 2.4 code written
17 * by Dale Farnsworth <dfarnsworth@mvista.com>.
18 *
19 * Copyright (C) 2006 Secret Lab Technologies Ltd.
20 * Grant Likely <grant.likely@secretlab.ca>
21 * Copyright (C) 2004-2006 Sylvain Munaut <tnt@246tNt.com>
22 * Copyright (C) 2003 MontaVista, Software, Inc.
23 *
24 * This file is licensed under the terms of the GNU General Public License
25 * version 2. This program is licensed "as is" without any warranty of any
26 * kind, whether express or implied.
27 */
28
29 /* 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,
40 * and so on). So the PSC1 is mapped to /dev/ttyPSC0, PSC2 to /dev/ttyPSC1 and
41 * so on. But be warned, it's an ABSOLUTE REQUIREMENT ! This is needed mainly
42 * fpr the console code : without this 1:1 mapping, at early boot time, when we
43 * are parsing the kernel args console=ttyPSC?, we wouldn't know which PSC it
44 * will be mapped to.
45 */
46
47 /* 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>
65 #include <linux/module.h>
66 #include <linux/tty.h>
67 #include <linux/serial.h>
68 #include <linux/sysrq.h>
69 #include <linux/console.h>
70
71 #include <asm/delay.h>
72 #include <asm/io.h>
73
74 #if defined(CONFIG_PPC_MERGE)
75 #include <linux/of.h>
76 #include <linux/of_platform.h>
77 #else
78 #include <linux/platform_device.h>
79 #endif
80
81 #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
91 /* 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
95
96 #define ISR_PASS_LIMIT 256 /* Max number of iteration in the interrupt */
97
98
99 static 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 */
107 #if defined(CONFIG_PPC_MERGE)
108 /* lookup table for matching device nodes to index numbers */
109 static struct device_node *mpc52xx_uart_nodes[MPC52xx_PSC_MAXNUM];
110
111 static void mpc52xx_uart_of_enumerate(void);
112 #endif
113
114 #define PSC(port) ((struct mpc52xx_psc __iomem *)((port)->membase))
115 #define FIFO(port) ((struct mpc52xx_psc_fifo __iomem *)(PSC(port)+1))
116
117
118 /* Forward declaration of the interruption handling routine */
119 static irqreturn_t mpc52xx_uart_int(int irq,void *dev_id);
120
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
130 #if defined(CONFIG_PPC_MERGE)
131 static struct of_device_id mpc52xx_uart_of_match[] = {
132 { .type = "serial", .compatible = "mpc5200-psc-uart", },
133 {},
134 };
135 #endif
136
137
138 /* ======================================================================== */
139 /* UART operations */
140 /* ======================================================================== */
141
142 static unsigned int
143 mpc52xx_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
149 static void
150 mpc52xx_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
151 {
152 /* Not implemented */
153 }
154
155 static unsigned int
156 mpc52xx_uart_get_mctrl(struct uart_port *port)
157 {
158 /* Not implemented */
159 return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
160 }
161
162 static void
163 mpc52xx_uart_stop_tx(struct uart_port *port)
164 {
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
170 static void
171 mpc52xx_uart_start_tx(struct uart_port *port)
172 {
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
178 static void
179 mpc52xx_uart_send_xchar(struct uart_port *port, char ch)
180 {
181 unsigned long flags;
182 spin_lock_irqsave(&port->lock, flags);
183
184 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 }
191
192 spin_unlock_irqrestore(&port->lock, flags);
193 }
194
195 static void
196 mpc52xx_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
203 static void
204 mpc52xx_uart_enable_ms(struct uart_port *port)
205 {
206 /* Not implemented */
207 }
208
209 static void
210 mpc52xx_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);
219
220 spin_unlock_irqrestore(&port->lock, flags);
221 }
222
223 static int
224 mpc52xx_uart_startup(struct uart_port *port)
225 {
226 struct mpc52xx_psc __iomem *psc = PSC(port);
227 struct mpc52xx_psc_fifo __iomem *fifo = FIFO(port);
228 int ret;
229
230 /* Request IRQ */
231 ret = request_irq(port->irq, mpc52xx_uart_int,
232 IRQF_DISABLED | IRQF_SAMPLE_RANDOM, "mpc52xx_psc_uart", port);
233 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);
239
240 out_be32(&psc->sicr,0); /* UART mode DCD ignored */
241
242 out_be16(&psc->mpc52xx_psc_clock_select, 0xdd00); /* /16 prescaler on */
243
244 out_8(&fifo->rfcntl, 0x00);
245 out_be16(&fifo->rfalarm, 0x1ff);
246 out_8(&fifo->tfcntl, 0x07);
247 out_be16(&fifo->tfalarm, 0x80);
248
249 port->read_status_mask |= MPC52xx_PSC_IMR_RXRDY | MPC52xx_PSC_IMR_TXRDY;
250 out_be16(&psc->mpc52xx_psc_imr,port->read_status_mask);
251
252 out_8(&psc->command,MPC52xx_PSC_TX_ENABLE);
253 out_8(&psc->command,MPC52xx_PSC_RX_ENABLE);
254
255 return 0;
256 }
257
258 static void
259 mpc52xx_uart_shutdown(struct uart_port *port)
260 {
261 struct mpc52xx_psc __iomem *psc = PSC(port);
262
263 /* Shut down the port. Leave TX active if on a console port */
264 out_8(&psc->command,MPC52xx_PSC_RST_RX);
265 if (!uart_console(port))
266 out_8(&psc->command,MPC52xx_PSC_RST_TX);
267
268 port->read_status_mask = 0;
269 out_be16(&psc->mpc52xx_psc_imr,port->read_status_mask);
270
271 /* Release interrupt */
272 free_irq(port->irq, port);
273 }
274
275 static void
276 mpc52xx_uart_set_termios(struct uart_port *port, struct ktermios *new,
277 struct ktermios *old)
278 {
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;
284
285 /* Prepare what we're gonna write */
286 mr1 = 0;
287
288 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;
304
305
306 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;
319
320 /* 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)); */
333 while (!(in_be16(&psc->mpc52xx_psc_status) & MPC52xx_PSC_SR_TXEMP) &&
334 --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."
340 "Some chars may have been lost.\n" );
341
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);
352
353 /* 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
361 static const char *
362 mpc52xx_uart_type(struct uart_port *port)
363 {
364 return port->type == PORT_MPC52xx ? "MPC52xx PSC" : NULL;
365 }
366
367 static void
368 mpc52xx_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
375 release_mem_region(port->mapbase, sizeof(struct mpc52xx_psc));
376 }
377
378 static int
379 mpc52xx_uart_request_port(struct uart_port *port)
380 {
381 int err;
382
383 if (port->flags & UPF_IOREMAP) /* Need to remap ? */
384 port->membase = ioremap(port->mapbase,
385 sizeof(struct mpc52xx_psc));
386
387 if (!port->membase)
388 return -EINVAL;
389
390 err = request_mem_region(port->mapbase, sizeof(struct mpc52xx_psc),
391 "mpc52xx_psc_uart") != NULL ? 0 : -EBUSY;
392
393 if (err && (port->flags & UPF_IOREMAP)) {
394 iounmap(port->membase);
395 port->membase = NULL;
396 }
397
398 return err;
399 }
400
401 static void
402 mpc52xx_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
409 static int
410 mpc52xx_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) ||
417 (ser->baud_base != port->uartclk) ||
418 (ser->iomem_base != (void*)port->mapbase) ||
419 (ser->hub6 != 0 ) )
420 return -EINVAL;
421
422 return 0;
423 }
424
425
426 static 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
448
449 /* ======================================================================== */
450 /* Interrupt handling */
451 /* ======================================================================== */
452
453 static inline int
454 mpc52xx_uart_int_rx_chars(struct uart_port *port)
455 {
456 struct tty_struct *tty = port->info->tty;
457 unsigned char ch, flag;
458 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
464 /* Get the char */
465 ch = in_8(&PSC(port)->mpc52xx_psc_buffer_8);
466
467 /* Handle sysreq char */
468 #ifdef SUPPORT_SYSRQ
469 if (uart_handle_sysrq_char(port, ch)) {
470 port->sysrq = 0;
471 continue;
472 }
473 #endif
474
475 /* Store it */
476
477 flag = TTY_NORMAL;
478 port->icount.rx++;
479
480 if ( status & (MPC52xx_PSC_SR_PE |
481 MPC52xx_PSC_SR_FE |
482 MPC52xx_PSC_SR_RB) ) {
483
484 if (status & MPC52xx_PSC_SR_RB) {
485 flag = TTY_BREAK;
486 uart_handle_break(port);
487 } else if (status & MPC52xx_PSC_SR_PE)
488 flag = TTY_PARITY;
489 else if (status & MPC52xx_PSC_SR_FE)
490 flag = TTY_FRAME;
491
492 /* Clear error condition */
493 out_8(&PSC(port)->command,MPC52xx_PSC_RST_ERR_STAT);
494
495 }
496 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 }
505 }
506
507 tty_flip_buffer_push(tty);
508
509 return in_be16(&PSC(port)->mpc52xx_psc_status) & MPC52xx_PSC_SR_RXRDY;
510 }
511
512 static inline int
513 mpc52xx_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)) {
527 mpc52xx_uart_stop_tx(port);
528 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)) {
546 mpc52xx_uart_stop_tx(port);
547 return 0;
548 }
549
550 return 1;
551 }
552
553 static irqreturn_t
554 mpc52xx_uart_int(int irq, void *dev_id)
555 {
556 struct uart_port *port = dev_id;
557 unsigned long pass = ISR_PASS_LIMIT;
558 unsigned int keepgoing;
559 unsigned short status;
560
561 spin_lock(&port->lock);
562
563 /* While we have stuff to do, we continue */
564 do {
565 /* If we don't find anything to do, we stop */
566 keepgoing = 0;
567
568 /* Read status */
569 status = in_be16(&PSC(port)->mpc52xx_psc_isr);
570 status &= port->read_status_mask;
571
572 /* 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 )
575 keepgoing |= mpc52xx_uart_int_rx_chars(port);
576
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);
581
582 /* Limit number of iteration */
583 if ( !(--pass) )
584 keepgoing = 0;
585
586 } while (keepgoing);
587
588 spin_unlock(&port->lock);
589
590 return IRQ_HANDLED;
591 }
592
593
594 /* ======================================================================== */
595 /* Console ( if applicable ) */
596 /* ======================================================================== */
597
598 #ifdef CONFIG_SERIAL_MPC52xx_CONSOLE
599
600 static void __init
601 mpc52xx_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
607 pr_debug("mpc52xx_console_get_options(port=%p)\n", port);
608
609 /* Read the mode registers */
610 out_8(&psc->command,MPC52xx_PSC_SEL_MODE_REG_1);
611 mr1 = in_8(&psc->mode);
612
613 /* CT{U,L}R are write-only ! */
614 *baud = CONFIG_SERIAL_MPC52xx_CONSOLE_BAUD;
615 #if !defined(CONFIG_PPC_MERGE)
616 if (__res.bi_baudrate)
617 *baud = __res.bi_baudrate;
618 #endif
619
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 }
628
629 if (mr1 & MPC52xx_PSC_MODE_PARNONE)
630 *parity = 'n';
631 else
632 *parity = mr1 & MPC52xx_PSC_MODE_PARODD ? 'o' : 'e';
633 }
634
635 static void
636 mpc52xx_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;
641
642 /* Disable interrupts */
643 out_be16(&psc->mpc52xx_psc_imr, 0);
644
645 /* Wait the TX buffer to be empty */
646 j = 5000000; /* Maximum wait */
647 while (!(in_be16(&psc->mpc52xx_psc_status) & MPC52xx_PSC_SR_TXEMP) &&
648 --j)
649 udelay(1);
650
651 /* Write all the chars */
652 for (i = 0; i < count; i++, s++) {
653 /* Line return handling */
654 if (*s == '\n')
655 out_8(&psc->mpc52xx_psc_buffer_8, '\r');
656
657 /* Send the char */
658 out_8(&psc->mpc52xx_psc_buffer_8, *s);
659
660 /* Wait the TX buffer to be empty */
661 j = 20000; /* Maximum wait */
662 while (!(in_be16(&psc->mpc52xx_psc_status) &
663 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
671 #if !defined(CONFIG_PPC_MERGE)
672 static int __init
673 mpc52xx_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;
684
685 /* 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
706 #else
707
708 static int __init
709 mpc52xx_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
762 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);
764
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
778
779 static struct uart_driver mpc52xx_uart_driver;
780
781 static struct console mpc52xx_console = {
782 .name = "ttyPSC",
783 .write = mpc52xx_console_write,
784 .device = uart_console_device,
785 .setup = mpc52xx_console_setup,
786 .flags = CON_PRINTBUFFER,
787 .index = -1, /* Specified on the cmdline (e.g. console=ttyPSC0 ) */
788 .data = &mpc52xx_uart_driver,
789 };
790
791
792 static int __init
793 mpc52xx_console_init(void)
794 {
795 #if defined(CONFIG_PPC_MERGE)
796 mpc52xx_uart_of_enumerate();
797 #endif
798 register_console(&mpc52xx_console);
799 return 0;
800 }
801
802 console_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
814 static struct uart_driver mpc52xx_uart_driver = {
815 .owner = THIS_MODULE,
816 .driver_name = "mpc52xx_psc_uart",
817 .dev_name = "ttyPSC",
818 .major = SERIAL_PSC_MAJOR,
819 .minor = SERIAL_PSC_MINOR,
820 .nr = MPC52xx_PSC_MAXNUM,
821 .cons = MPC52xx_PSC_CONSOLE,
822 };
823
824
825 #if !defined(CONFIG_PPC_MERGE)
826 /* ======================================================================== */
827 /* Platform Driver */
828 /* ======================================================================== */
829
830 static int __devinit
831 mpc52xx_uart_probe(struct platform_device *dev)
832 {
833 struct resource *res = dev->resource;
834
835 struct uart_port *port = NULL;
836 int i, idx, ret;
837
838 /* Check validity & presence */
839 idx = dev->id;
840 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
849 spin_lock_init(&port->lock);
850 port->uartclk = __res.bi_ipbfreq / 2; /* Look at CTLR doc */
851 port->fifosize = 512;
852 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;
857 port->dev = &dev->dev;
858
859 /* Search for IRQ and mapbase */
860 for (i=0 ; i<dev->num_resources ; i++, res++) {
861 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)
872 platform_set_drvdata(dev, (void*)port);
873
874 return ret;
875 }
876
877 static int
878 mpc52xx_uart_remove(struct platform_device *dev)
879 {
880 struct uart_port *port = (struct uart_port *) platform_get_drvdata(dev);
881
882 platform_set_drvdata(dev, NULL);
883
884 if (port)
885 uart_remove_one_port(&mpc52xx_uart_driver, port);
886
887 return 0;
888 }
889
890 #ifdef CONFIG_PM
891 static int
892 mpc52xx_uart_suspend(struct platform_device *dev, pm_message_t state)
893 {
894 struct uart_port *port = (struct uart_port *) platform_get_drvdata(dev);
895
896 if (port)
897 uart_suspend_port(&mpc52xx_uart_driver, port);
898
899 return 0;
900 }
901
902 static int
903 mpc52xx_uart_resume(struct platform_device *dev)
904 {
905 struct uart_port *port = (struct uart_port *) platform_get_drvdata(dev);
906
907 if (port)
908 uart_resume_port(&mpc52xx_uart_driver, port);
909
910 return 0;
911 }
912 #endif
913
914
915 static struct platform_driver mpc52xx_uart_platform_driver = {
916 .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
922 .driver = {
923 .name = "mpc52xx-psc",
924 },
925 };
926 #endif /* !defined(CONFIG_PPC_MERGE) */
927
928
929 #if defined(CONFIG_PPC_MERGE)
930 /* ======================================================================== */
931 /* OF Platform Driver */
932 /* ======================================================================== */
933
934 static int __devinit
935 mpc52xx_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
980 dev_dbg(&op->dev, "mpc52xx-psc uart at %p, irq=%x, freq=%i\n",
981 (void*)port->mapbase, port->irq, port->uartclk);
982
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
996 static int
997 mpc52xx_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
1002 if (port) {
1003 uart_remove_one_port(&mpc52xx_uart_driver, port);
1004 irq_dispose_mapping(port->irq);
1005 }
1006
1007 return 0;
1008 }
1009
1010 #ifdef CONFIG_PM
1011 static int
1012 mpc52xx_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
1022 static int
1023 mpc52xx_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
1034 static void
1035 mpc52xx_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];
1057 mpc52xx_uart_nodes[idx] = np;
1058 }
1059
1060 static void
1061 mpc52xx_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? */
1076 devno = of_get_property(np, "port-number", NULL);
1077 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
1089 MODULE_DEVICE_TABLE(of, mpc52xx_uart_of_match);
1090
1091 static 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) */
1106
1107
1108 /* ======================================================================== */
1109 /* Module */
1110 /* ======================================================================== */
1111
1112 static int __init
1113 mpc52xx_uart_init(void)
1114 {
1115 int ret;
1116
1117 printk(KERN_INFO "Serial: MPC52xx PSC UART driver\n");
1118
1119 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;
1123 }
1124
1125 #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;
1146 }
1147
1148 static void __exit
1149 mpc52xx_uart_exit(void)
1150 {
1151 #if defined(CONFIG_PPC_MERGE)
1152 of_unregister_platform_driver(&mpc52xx_uart_of_driver);
1153 #else
1154 platform_driver_unregister(&mpc52xx_uart_platform_driver);
1155 #endif
1156 uart_unregister_driver(&mpc52xx_uart_driver);
1157 }
1158
1159
1160 module_init(mpc52xx_uart_init);
1161 module_exit(mpc52xx_uart_exit);
1162
1163 MODULE_AUTHOR("Sylvain Munaut <tnt@246tNt.com>");
1164 MODULE_DESCRIPTION("Freescale MPC52xx PSC UART");
1165 MODULE_LICENSE("GPL");
This page took 0.055148 seconds and 5 git commands to generate.