VServer 1.9.2 (patch-2.6.8.1-vs1.9.2.diff)
[linux-2.6.git] / drivers / serial / pxa.c
1 /*
2  *  linux/drivers/serial/pxa.c
3  *
4  *  Based on drivers/serial/8250.c by Russell King.
5  *
6  *  Author:     Nicolas Pitre
7  *  Created:    Feb 20, 2003
8  *  Copyright:  (C) 2003 Monta Vista Software, Inc.
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * Note 1: This driver is made separate from the already too overloaded
16  * 8250.c because it needs some kirks of its own and that'll make it
17  * easier to add DMA support.
18  *
19  * Note 2: I'm too sick of device allocation policies for serial ports.
20  * If someone else wants to request an "official" allocation of major/minor
21  * for this driver please be my guest.  And don't forget that new hardware
22  * to come from Intel might have more than 3 or 4 of those UARTs.  Let's
23  * hope for a better port registration and dynamic device allocation scheme
24  * with the serial core maintainer satisfaction to appear soon.
25  */
26
27 #include <linux/config.h>
28 #include <linux/module.h>
29 #include <linux/tty.h>
30 #include <linux/ioport.h>
31 #include <linux/init.h>
32 #include <linux/console.h>
33 #include <linux/sysrq.h>
34 #include <linux/serial_reg.h>
35 #include <linux/circ_buf.h>
36 #include <linux/delay.h>
37 #include <linux/interrupt.h>
38 #include <linux/device.h>
39
40 #include <asm/io.h>
41 #include <asm/hardware.h>
42 #include <asm/irq.h>
43
44 #if defined(CONFIG_SERIAL_PXA_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
45 #define SUPPORT_SYSRQ
46 #endif
47
48 #include <linux/serial_core.h>
49
50
51 struct uart_pxa_port {
52         struct uart_port        port;
53         unsigned char           ier;
54         unsigned char           lcr;
55         unsigned char           mcr;
56         unsigned int            lsr_break_flag;
57         unsigned int            cken;
58         char                    *name;
59 };
60
61 static inline unsigned int serial_in(struct uart_pxa_port *up, int offset)
62 {
63         offset <<= 2;
64         return readl(up->port.membase + offset);
65 }
66
67 static inline void serial_out(struct uart_pxa_port *up, int offset, int value)
68 {
69         offset <<= 2;
70         writel(value, up->port.membase + offset);
71 }
72
73 static void serial_pxa_enable_ms(struct uart_port *port)
74 {
75         struct uart_pxa_port *up = (struct uart_pxa_port *)port;
76
77         up->ier |= UART_IER_MSI;
78         serial_out(up, UART_IER, up->ier);
79 }
80
81 static void serial_pxa_stop_tx(struct uart_port *port, unsigned int tty_stop)
82 {
83         struct uart_pxa_port *up = (struct uart_pxa_port *)port;
84
85         if (up->ier & UART_IER_THRI) {
86                 up->ier &= ~UART_IER_THRI;
87                 serial_out(up, UART_IER, up->ier);
88         }
89 }
90
91 static void serial_pxa_stop_rx(struct uart_port *port)
92 {
93         struct uart_pxa_port *up = (struct uart_pxa_port *)port;
94
95         up->ier &= ~UART_IER_RLSI;
96         up->port.read_status_mask &= ~UART_LSR_DR;
97         serial_out(up, UART_IER, up->ier);
98 }
99
100 static inline void
101 receive_chars(struct uart_pxa_port *up, int *status, struct pt_regs *regs)
102 {
103         struct tty_struct *tty = up->port.info->tty;
104         unsigned char ch;
105         int max_count = 256;
106
107         do {
108                 if (unlikely(tty->flip.count >= TTY_FLIPBUF_SIZE)) {
109                         /*
110                          * FIXME: Deadlock can happen here if we're a
111                          * low-latency port.  We're holding the per-port
112                          * spinlock, and we call flush_to_ldisc->
113                          * n_tty_receive_buf->n_tty_receive_char->
114                          * opost->uart_put_char.
115                          */
116                         tty->flip.work.func((void *)tty);
117                         if (tty->flip.count >= TTY_FLIPBUF_SIZE)
118                                 return; // if TTY_DONT_FLIP is set
119                 }
120                 ch = serial_in(up, UART_RX);
121                 *tty->flip.char_buf_ptr = ch;
122                 *tty->flip.flag_buf_ptr = TTY_NORMAL;
123                 up->port.icount.rx++;
124
125                 if (unlikely(*status & (UART_LSR_BI | UART_LSR_PE |
126                                        UART_LSR_FE | UART_LSR_OE))) {
127                         /*
128                          * For statistics only
129                          */
130                         if (*status & UART_LSR_BI) {
131                                 *status &= ~(UART_LSR_FE | UART_LSR_PE);
132                                 up->port.icount.brk++;
133                                 /*
134                                  * We do the SysRQ and SAK checking
135                                  * here because otherwise the break
136                                  * may get masked by ignore_status_mask
137                                  * or read_status_mask.
138                                  */
139                                 if (uart_handle_break(&up->port))
140                                         goto ignore_char;
141                         } else if (*status & UART_LSR_PE)
142                                 up->port.icount.parity++;
143                         else if (*status & UART_LSR_FE)
144                                 up->port.icount.frame++;
145                         if (*status & UART_LSR_OE)
146                                 up->port.icount.overrun++;
147
148                         /*
149                          * Mask off conditions which should be ignored.
150                          */
151                         *status &= up->port.read_status_mask;
152
153 #ifdef CONFIG_SERIAL_PXA_CONSOLE
154                         if (up->port.line == up->port.cons->index) {
155                                 /* Recover the break flag from console xmit */
156                                 *status |= up->lsr_break_flag;
157                                 up->lsr_break_flag = 0;
158                         }
159 #endif
160                         if (*status & UART_LSR_BI) {
161                                 *tty->flip.flag_buf_ptr = TTY_BREAK;
162                         } else if (*status & UART_LSR_PE)
163                                 *tty->flip.flag_buf_ptr = TTY_PARITY;
164                         else if (*status & UART_LSR_FE)
165                                 *tty->flip.flag_buf_ptr = TTY_FRAME;
166                 }
167                 if (uart_handle_sysrq_char(&up->port, ch, regs))
168                         goto ignore_char;
169                 if ((*status & up->port.ignore_status_mask) == 0) {
170                         tty->flip.flag_buf_ptr++;
171                         tty->flip.char_buf_ptr++;
172                         tty->flip.count++;
173                 }
174                 if ((*status & UART_LSR_OE) &&
175                     tty->flip.count < TTY_FLIPBUF_SIZE) {
176                         /*
177                          * Overrun is special, since it's reported
178                          * immediately, and doesn't affect the current
179                          * character.
180                          */
181                         *tty->flip.flag_buf_ptr = TTY_OVERRUN;
182                         tty->flip.flag_buf_ptr++;
183                         tty->flip.char_buf_ptr++;
184                         tty->flip.count++;
185                 }
186         ignore_char:
187                 *status = serial_in(up, UART_LSR);
188         } while ((*status & UART_LSR_DR) && (max_count-- > 0));
189         tty_flip_buffer_push(tty);
190 }
191
192 static void transmit_chars(struct uart_pxa_port *up)
193 {
194         struct circ_buf *xmit = &up->port.info->xmit;
195         int count;
196
197         if (up->port.x_char) {
198                 serial_out(up, UART_TX, up->port.x_char);
199                 up->port.icount.tx++;
200                 up->port.x_char = 0;
201                 return;
202         }
203         if (uart_circ_empty(xmit) || uart_tx_stopped(&up->port)) {
204                 serial_pxa_stop_tx(&up->port, 0);
205                 return;
206         }
207
208         count = up->port.fifosize / 2;
209         do {
210                 serial_out(up, UART_TX, xmit->buf[xmit->tail]);
211                 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
212                 up->port.icount.tx++;
213                 if (uart_circ_empty(xmit))
214                         break;
215         } while (--count > 0);
216
217         if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
218                 uart_write_wakeup(&up->port);
219
220
221         if (uart_circ_empty(xmit))
222                 serial_pxa_stop_tx(&up->port, 0);
223 }
224
225 static void serial_pxa_start_tx(struct uart_port *port, unsigned int tty_start)
226 {
227         struct uart_pxa_port *up = (struct uart_pxa_port *)port;
228
229         if (!(up->ier & UART_IER_THRI)) {
230                 up->ier |= UART_IER_THRI;
231                 serial_out(up, UART_IER, up->ier);
232         }
233 }
234
235 static inline void check_modem_status(struct uart_pxa_port *up)
236 {
237         int status;
238
239         status = serial_in(up, UART_MSR);
240
241         if ((status & UART_MSR_ANY_DELTA) == 0)
242                 return;
243
244         if (status & UART_MSR_TERI)
245                 up->port.icount.rng++;
246         if (status & UART_MSR_DDSR)
247                 up->port.icount.dsr++;
248         if (status & UART_MSR_DDCD)
249                 uart_handle_dcd_change(&up->port, status & UART_MSR_DCD);
250         if (status & UART_MSR_DCTS)
251                 uart_handle_cts_change(&up->port, status & UART_MSR_CTS);
252
253         wake_up_interruptible(&up->port.info->delta_msr_wait);
254 }
255
256 /*
257  * This handles the interrupt from one port.
258  */
259 static inline irqreturn_t
260 serial_pxa_irq(int irq, void *dev_id, struct pt_regs *regs)
261 {
262         struct uart_pxa_port *up = (struct uart_pxa_port *)dev_id;
263         unsigned int iir, lsr;
264
265         iir = serial_in(up, UART_IIR);
266         if (iir & UART_IIR_NO_INT)
267                 return IRQ_NONE;
268         lsr = serial_in(up, UART_LSR);
269         if (lsr & UART_LSR_DR)
270                 receive_chars(up, &lsr, regs);
271         check_modem_status(up);
272         if (lsr & UART_LSR_THRE)
273                 transmit_chars(up);
274         return IRQ_HANDLED;
275 }
276
277 static unsigned int serial_pxa_tx_empty(struct uart_port *port)
278 {
279         struct uart_pxa_port *up = (struct uart_pxa_port *)port;
280         unsigned long flags;
281         unsigned int ret;
282
283         spin_lock_irqsave(&up->port.lock, flags);
284         ret = serial_in(up, UART_LSR) & UART_LSR_TEMT ? TIOCSER_TEMT : 0;
285         spin_unlock_irqrestore(&up->port.lock, flags);
286
287         return ret;
288 }
289
290 static unsigned int serial_pxa_get_mctrl(struct uart_port *port)
291 {
292         struct uart_pxa_port *up = (struct uart_pxa_port *)port;
293         unsigned long flags;
294         unsigned char status;
295         unsigned int ret;
296
297 return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
298         spin_lock_irqsave(&up->port.lock, flags);
299         status = serial_in(up, UART_MSR);
300         spin_unlock_irqrestore(&up->port.lock, flags);
301
302         ret = 0;
303         if (status & UART_MSR_DCD)
304                 ret |= TIOCM_CAR;
305         if (status & UART_MSR_RI)
306                 ret |= TIOCM_RNG;
307         if (status & UART_MSR_DSR)
308                 ret |= TIOCM_DSR;
309         if (status & UART_MSR_CTS)
310                 ret |= TIOCM_CTS;
311         return ret;
312 }
313
314 static void serial_pxa_set_mctrl(struct uart_port *port, unsigned int mctrl)
315 {
316         struct uart_pxa_port *up = (struct uart_pxa_port *)port;
317         unsigned char mcr = 0;
318
319         if (mctrl & TIOCM_RTS)
320                 mcr |= UART_MCR_RTS;
321         if (mctrl & TIOCM_DTR)
322                 mcr |= UART_MCR_DTR;
323         if (mctrl & TIOCM_OUT1)
324                 mcr |= UART_MCR_OUT1;
325         if (mctrl & TIOCM_OUT2)
326                 mcr |= UART_MCR_OUT2;
327         if (mctrl & TIOCM_LOOP)
328                 mcr |= UART_MCR_LOOP;
329
330         mcr |= up->mcr;
331
332         serial_out(up, UART_MCR, mcr);
333 }
334
335 static void serial_pxa_break_ctl(struct uart_port *port, int break_state)
336 {
337         struct uart_pxa_port *up = (struct uart_pxa_port *)port;
338         unsigned long flags;
339
340         spin_lock_irqsave(&up->port.lock, flags);
341         if (break_state == -1)
342                 up->lcr |= UART_LCR_SBC;
343         else
344                 up->lcr &= ~UART_LCR_SBC;
345         serial_out(up, UART_LCR, up->lcr);
346         spin_unlock_irqrestore(&up->port.lock, flags);
347 }
348
349 #if 0
350 static void serial_pxa_dma_init(struct pxa_uart *up)
351 {
352         up->rxdma =
353                 pxa_request_dma(up->name, DMA_PRIO_LOW, pxa_receive_dma, up);
354         if (up->rxdma < 0)
355                 goto out;
356         up->txdma =
357                 pxa_request_dma(up->name, DMA_PRIO_LOW, pxa_transmit_dma, up);
358         if (up->txdma < 0)
359                 goto err_txdma;
360         up->dmadesc = kmalloc(4 * sizeof(pxa_dma_desc), GFP_KERNEL);
361         if (!up->dmadesc)
362                 goto err_alloc;
363
364         /* ... */
365 err_alloc:
366         pxa_free_dma(up->txdma);
367 err_rxdma:
368         pxa_free_dma(up->rxdma);
369 out:
370         return;
371 }
372 #endif
373
374 static int serial_pxa_startup(struct uart_port *port)
375 {
376         struct uart_pxa_port *up = (struct uart_pxa_port *)port;
377         unsigned long flags;
378         int retval;
379
380         up->mcr = 0;
381
382         /*
383          * Allocate the IRQ
384          */
385         retval = request_irq(up->port.irq, serial_pxa_irq, 0, up->name, up);
386         if (retval)
387                 return retval;
388
389         /*
390          * Clear the FIFO buffers and disable them.
391          * (they will be reenabled in set_termios())
392          */
393         serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO);
394         serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO |
395                         UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
396         serial_out(up, UART_FCR, 0);
397
398         /*
399          * Clear the interrupt registers.
400          */
401         (void) serial_in(up, UART_LSR);
402         (void) serial_in(up, UART_RX);
403         (void) serial_in(up, UART_IIR);
404         (void) serial_in(up, UART_MSR);
405
406         /*
407          * Now, initialize the UART
408          */
409         serial_out(up, UART_LCR, UART_LCR_WLEN8);
410
411         spin_lock_irqsave(&up->port.lock, flags);
412         up->port.mctrl |= TIOCM_OUT2;
413         serial_pxa_set_mctrl(&up->port, up->port.mctrl);
414         spin_unlock_irqrestore(&up->port.lock, flags);
415
416         /*
417          * Finally, enable interrupts.  Note: Modem status interrupts
418          * are set via set_termios(), which will be occuring imminently
419          * anyway, so we don't enable them here.
420          */
421         up->ier = UART_IER_RLSI | UART_IER_RDI | UART_IER_RTOIE | UART_IER_UUE;
422         serial_out(up, UART_IER, up->ier);
423
424         /*
425          * And clear the interrupt registers again for luck.
426          */
427         (void) serial_in(up, UART_LSR);
428         (void) serial_in(up, UART_RX);
429         (void) serial_in(up, UART_IIR);
430         (void) serial_in(up, UART_MSR);
431
432         return 0;
433 }
434
435 static void serial_pxa_shutdown(struct uart_port *port)
436 {
437         struct uart_pxa_port *up = (struct uart_pxa_port *)port;
438         unsigned long flags;
439
440         free_irq(up->port.irq, up);
441
442         /*
443          * Disable interrupts from this port
444          */
445         up->ier = 0;
446         serial_out(up, UART_IER, 0);
447
448         spin_lock_irqsave(&up->port.lock, flags);
449         up->port.mctrl &= ~TIOCM_OUT2;
450         serial_pxa_set_mctrl(&up->port, up->port.mctrl);
451         spin_unlock_irqrestore(&up->port.lock, flags);
452
453         /*
454          * Disable break condition and FIFOs
455          */
456         serial_out(up, UART_LCR, serial_in(up, UART_LCR) & ~UART_LCR_SBC);
457         serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO |
458                                   UART_FCR_CLEAR_RCVR |
459                                   UART_FCR_CLEAR_XMIT);
460         serial_out(up, UART_FCR, 0);
461 }
462
463 static void
464 serial_pxa_set_termios(struct uart_port *port, struct termios *termios,
465                        struct termios *old)
466 {
467         struct uart_pxa_port *up = (struct uart_pxa_port *)port;
468         unsigned char cval, fcr = 0;
469         unsigned long flags;
470         unsigned int baud, quot;
471
472         switch (termios->c_cflag & CSIZE) {
473         case CS5:
474                 cval = 0x00;
475                 break;
476         case CS6:
477                 cval = 0x01;
478                 break;
479         case CS7:
480                 cval = 0x02;
481                 break;
482         default:
483         case CS8:
484                 cval = 0x03;
485                 break;
486         }
487
488         if (termios->c_cflag & CSTOPB)
489                 cval |= 0x04;
490         if (termios->c_cflag & PARENB)
491                 cval |= UART_LCR_PARITY;
492         if (!(termios->c_cflag & PARODD))
493                 cval |= UART_LCR_EPAR;
494
495         /*
496          * Ask the core to calculate the divisor for us.
497          */
498         baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
499         quot = uart_get_divisor(port, baud);
500
501         if ((up->port.uartclk / quot) < (2400 * 16))
502                 fcr = UART_FCR_ENABLE_FIFO | UART_FCR_PXAR1;
503         else
504                 fcr = UART_FCR_ENABLE_FIFO | UART_FCR_PXAR8;
505
506         /*
507          * Ok, we're now changing the port state.  Do it with
508          * interrupts disabled.
509          */
510         spin_lock_irqsave(&up->port.lock, flags);
511
512         /*
513          * Ensure the port will be enabled.
514          * This is required especially for serial console.
515          */
516         up->ier |= IER_UUE;
517
518         /*
519          * Update the per-port timeout.
520          */
521         uart_update_timeout(port, termios->c_cflag, quot);
522
523         up->port.read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
524         if (termios->c_iflag & INPCK)
525                 up->port.read_status_mask |= UART_LSR_FE | UART_LSR_PE;
526         if (termios->c_iflag & (BRKINT | PARMRK))
527                 up->port.read_status_mask |= UART_LSR_BI;
528
529         /*
530          * Characters to ignore
531          */
532         up->port.ignore_status_mask = 0;
533         if (termios->c_iflag & IGNPAR)
534                 up->port.ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
535         if (termios->c_iflag & IGNBRK) {
536                 up->port.ignore_status_mask |= UART_LSR_BI;
537                 /*
538                  * If we're ignoring parity and break indicators,
539                  * ignore overruns too (for real raw support).
540                  */
541                 if (termios->c_iflag & IGNPAR)
542                         up->port.ignore_status_mask |= UART_LSR_OE;
543         }
544
545         /*
546          * ignore all characters if CREAD is not set
547          */
548         if ((termios->c_cflag & CREAD) == 0)
549                 up->port.ignore_status_mask |= UART_LSR_DR;
550
551         /*
552          * CTS flow control flag and modem status interrupts
553          */
554         up->ier &= ~UART_IER_MSI;
555         if (UART_ENABLE_MS(&up->port, termios->c_cflag))
556                 up->ier |= UART_IER_MSI;
557
558         serial_out(up, UART_IER, up->ier);
559
560         serial_out(up, UART_LCR, cval | UART_LCR_DLAB);/* set DLAB */
561         serial_out(up, UART_DLL, quot & 0xff);          /* LS of divisor */
562         serial_out(up, UART_DLM, quot >> 8);            /* MS of divisor */
563         serial_out(up, UART_LCR, cval);         /* reset DLAB */
564         up->lcr = cval;                                 /* Save LCR */
565         serial_pxa_set_mctrl(&up->port, up->port.mctrl);
566         serial_out(up, UART_FCR, fcr);
567         spin_unlock_irqrestore(&up->port.lock, flags);
568 }
569
570 static void
571 serial_pxa_pm(struct uart_port *port, unsigned int state,
572               unsigned int oldstate)
573 {
574         struct uart_pxa_port *up = (struct uart_pxa_port *)port;
575         pxa_set_cken(up->cken, !state);
576         if (!state)
577                 udelay(1);
578 }
579
580 static void serial_pxa_release_port(struct uart_port *port)
581 {
582 }
583
584 static int serial_pxa_request_port(struct uart_port *port)
585 {
586         return 0;
587 }
588
589 static void serial_pxa_config_port(struct uart_port *port, int flags)
590 {
591         struct uart_pxa_port *up = (struct uart_pxa_port *)port;
592         up->port.type = PORT_PXA;
593 }
594
595 static int
596 serial_pxa_verify_port(struct uart_port *port, struct serial_struct *ser)
597 {
598         /* we don't want the core code to modify any port params */
599         return -EINVAL;
600 }
601
602 static const char *
603 serial_pxa_type(struct uart_port *port)
604 {
605         struct uart_pxa_port *up = (struct uart_pxa_port *)port;
606         return up->name;
607 }
608
609 #ifdef CONFIG_SERIAL_PXA_CONSOLE
610
611 extern struct uart_pxa_port serial_pxa_ports[];
612 extern struct uart_driver serial_pxa_reg;
613
614 #define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)
615
616 /*
617  *      Wait for transmitter & holding register to empty
618  */
619 static inline void wait_for_xmitr(struct uart_pxa_port *up)
620 {
621         unsigned int status, tmout = 10000;
622
623         /* Wait up to 10ms for the character(s) to be sent. */
624         do {
625                 status = serial_in(up, UART_LSR);
626
627                 if (status & UART_LSR_BI)
628                         up->lsr_break_flag = UART_LSR_BI;
629
630                 if (--tmout == 0)
631                         break;
632                 udelay(1);
633         } while ((status & BOTH_EMPTY) != BOTH_EMPTY);
634
635         /* Wait up to 1s for flow control if necessary */
636         if (up->port.flags & UPF_CONS_FLOW) {
637                 tmout = 1000000;
638                 while (--tmout &&
639                        ((serial_in(up, UART_MSR) & UART_MSR_CTS) == 0))
640                         udelay(1);
641         }
642 }
643
644 /*
645  * Print a string to the serial port trying not to disturb
646  * any possible real use of the port...
647  *
648  *      The console_lock must be held when we get here.
649  */
650 static void
651 serial_pxa_console_write(struct console *co, const char *s, unsigned int count)
652 {
653         struct uart_pxa_port *up = &serial_pxa_ports[co->index];
654         unsigned int ier;
655         int i;
656
657         /*
658          *      First save the UER then disable the interrupts
659          */
660         ier = serial_in(up, UART_IER);
661         serial_out(up, UART_IER, UART_IER_UUE);
662
663         /*
664          *      Now, do each character
665          */
666         for (i = 0; i < count; i++, s++) {
667                 wait_for_xmitr(up);
668
669                 /*
670                  *      Send the character out.
671                  *      If a LF, also do CR...
672                  */
673                 serial_out(up, UART_TX, *s);
674                 if (*s == 10) {
675                         wait_for_xmitr(up);
676                         serial_out(up, UART_TX, 13);
677                 }
678         }
679
680         /*
681          *      Finally, wait for transmitter to become empty
682          *      and restore the IER
683          */
684         wait_for_xmitr(up);
685         serial_out(up, UART_IER, ier);
686 }
687
688 static int __init
689 serial_pxa_console_setup(struct console *co, char *options)
690 {
691         struct uart_pxa_port *up;
692         int baud = 9600;
693         int bits = 8;
694         int parity = 'n';
695         int flow = 'n';
696
697         if (co->index == -1 || co->index >= serial_pxa_reg.nr)
698                 co->index = 0;
699         up = &serial_pxa_ports[co->index];
700
701         if (options)
702                 uart_parse_options(options, &baud, &parity, &bits, &flow);
703
704         return uart_set_options(&up->port, co, baud, parity, bits, flow);
705 }
706
707 static struct console serial_pxa_console = {
708         .name           = "ttyS",
709         .write          = serial_pxa_console_write,
710         .device         = uart_console_device,
711         .setup          = serial_pxa_console_setup,
712         .flags          = CON_PRINTBUFFER,
713         .index          = -1,
714         .data           = &serial_pxa_reg,
715 };
716
717 static int __init
718 serial_pxa_console_init(void)
719 {
720         register_console(&serial_pxa_console);
721         return 0;
722 }
723
724 console_initcall(serial_pxa_console_init);
725
726 #define PXA_CONSOLE     &serial_pxa_console
727 #else
728 #define PXA_CONSOLE     NULL
729 #endif
730
731 struct uart_ops serial_pxa_pops = {
732         .tx_empty       = serial_pxa_tx_empty,
733         .set_mctrl      = serial_pxa_set_mctrl,
734         .get_mctrl      = serial_pxa_get_mctrl,
735         .stop_tx        = serial_pxa_stop_tx,
736         .start_tx       = serial_pxa_start_tx,
737         .stop_rx        = serial_pxa_stop_rx,
738         .enable_ms      = serial_pxa_enable_ms,
739         .break_ctl      = serial_pxa_break_ctl,
740         .startup        = serial_pxa_startup,
741         .shutdown       = serial_pxa_shutdown,
742         .set_termios    = serial_pxa_set_termios,
743         .pm             = serial_pxa_pm,
744         .type           = serial_pxa_type,
745         .release_port   = serial_pxa_release_port,
746         .request_port   = serial_pxa_request_port,
747         .config_port    = serial_pxa_config_port,
748         .verify_port    = serial_pxa_verify_port,
749 };
750
751 static struct uart_pxa_port serial_pxa_ports[] = {
752      {  /* FFUART */
753         .name   = "FFUART",
754         .cken   = CKEN6_FFUART,
755         .port   = {
756                 .type           = PORT_PXA,
757                 .iotype         = UPIO_MEM,
758                 .membase        = (void *)&FFUART,
759                 .mapbase        = __PREG(FFUART),
760                 .irq            = IRQ_FFUART,
761                 .uartclk        = 921600 * 16,
762                 .fifosize       = 64,
763                 .ops            = &serial_pxa_pops,
764                 .line           = 0,
765         },
766   }, {  /* BTUART */
767         .name   = "BTUART",
768         .cken   = CKEN7_BTUART,
769         .port   = {
770                 .type           = PORT_PXA,
771                 .iotype         = UPIO_MEM,
772                 .membase        = (void *)&BTUART,
773                 .mapbase        = __PREG(BTUART),
774                 .irq            = IRQ_BTUART,
775                 .uartclk        = 921600 * 16,
776                 .fifosize       = 64,
777                 .ops            = &serial_pxa_pops,
778                 .line           = 1,
779         },
780   }, {  /* STUART */
781         .name   = "STUART",
782         .cken   = CKEN5_STUART,
783         .port   = {
784                 .type           = PORT_PXA,
785                 .iotype         = UPIO_MEM,
786                 .membase        = (void *)&STUART,
787                 .mapbase        = __PREG(STUART),
788                 .irq            = IRQ_STUART,
789                 .uartclk        = 921600 * 16,
790                 .fifosize       = 64,
791                 .ops            = &serial_pxa_pops,
792                 .line           = 2,
793         },
794   }
795 };
796
797 static struct uart_driver serial_pxa_reg = {
798         .owner          = THIS_MODULE,
799         .driver_name    = "PXA serial",
800         .devfs_name     = "tts/",
801         .dev_name       = "ttyS",
802         .major          = TTY_MAJOR,
803         .minor          = 64,
804         .nr             = ARRAY_SIZE(serial_pxa_ports),
805         .cons           = PXA_CONSOLE,
806 };
807
808 static int serial_pxa_suspend(struct device *_dev, u32 state, u32 level)
809 {
810         struct uart_pxa_port *sport = dev_get_drvdata(_dev);
811
812         if (sport && level == SUSPEND_DISABLE)
813                 uart_suspend_port(&serial_pxa_reg, &sport->port);
814
815         return 0;
816 }
817
818 static int serial_pxa_resume(struct device *_dev, u32 level)
819 {
820         struct uart_pxa_port *sport = dev_get_drvdata(_dev);
821
822         if (sport && level == RESUME_ENABLE)
823                 uart_resume_port(&serial_pxa_reg, &sport->port);
824
825         return 0;
826 }
827
828 static int serial_pxa_probe(struct device *_dev)
829 {
830         struct platform_device *dev = to_platform_device(_dev);
831
832         serial_pxa_ports[dev->id].port.dev = _dev;
833         uart_add_one_port(&serial_pxa_reg, &serial_pxa_ports[dev->id].port);
834         dev_set_drvdata(_dev, &serial_pxa_ports[dev->id]);
835         return 0;
836 }
837
838 static int serial_pxa_remove(struct device *_dev)
839 {
840         struct uart_pxa_port *sport = dev_get_drvdata(_dev);
841
842         dev_set_drvdata(_dev, NULL);
843
844         if (sport)
845                 uart_remove_one_port(&serial_pxa_reg, &sport->port);
846
847         return 0;
848 }
849
850 static struct device_driver serial_pxa_driver = {
851         .name           = "pxa2xx-uart",
852         .bus            = &platform_bus_type,
853         .probe          = serial_pxa_probe,
854         .remove         = serial_pxa_remove,
855
856         .suspend        = serial_pxa_suspend,
857         .resume         = serial_pxa_resume,
858 };
859
860 int __init serial_pxa_init(void)
861 {
862         int ret;
863
864         ret = uart_register_driver(&serial_pxa_reg);
865         if (ret != 0)
866                 return ret;
867
868         ret = driver_register(&serial_pxa_driver);
869         if (ret != 0)
870                 uart_unregister_driver(&serial_pxa_reg);
871
872         return ret;
873 }
874
875 void __exit serial_pxa_exit(void)
876 {
877         driver_unregister(&serial_pxa_driver);
878         uart_unregister_driver(&serial_pxa_reg);
879 }
880
881 module_init(serial_pxa_init);
882 module_exit(serial_pxa_exit);
883
884 MODULE_LICENSE("GPL");
885