ah6: convert to ahash
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / serial / amba-pl011.c
blobef7adc8135dd4755fbd3cbde2c560988ea2afc0f
1 /*
2 * linux/drivers/char/amba.c
4 * Driver for AMBA serial ports
6 * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
8 * Copyright 1999 ARM Limited
9 * Copyright (C) 2000 Deep Blue Solutions Ltd.
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 * This is a generic driver for ARM AMBA-type serial ports. They
26 * have a lot of 16550-like features, but are not register compatible.
27 * Note that although they do have CTS, DCD and DSR inputs, they do
28 * not have an RI input, nor do they have DTR or RTS outputs. If
29 * required, these have to be supplied via some other means (eg, GPIO)
30 * and hooked into this driver.
33 #if defined(CONFIG_SERIAL_AMBA_PL011_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
34 #define SUPPORT_SYSRQ
35 #endif
37 #include <linux/module.h>
38 #include <linux/ioport.h>
39 #include <linux/init.h>
40 #include <linux/console.h>
41 #include <linux/sysrq.h>
42 #include <linux/device.h>
43 #include <linux/tty.h>
44 #include <linux/tty_flip.h>
45 #include <linux/serial_core.h>
46 #include <linux/serial.h>
47 #include <linux/amba/bus.h>
48 #include <linux/amba/serial.h>
49 #include <linux/clk.h>
51 #include <asm/io.h>
52 #include <asm/sizes.h>
54 #define UART_NR 14
56 #define SERIAL_AMBA_MAJOR 204
57 #define SERIAL_AMBA_MINOR 64
58 #define SERIAL_AMBA_NR UART_NR
60 #define AMBA_ISR_PASS_LIMIT 256
62 #define UART_DR_ERROR (UART011_DR_OE|UART011_DR_BE|UART011_DR_PE|UART011_DR_FE)
63 #define UART_DUMMY_DR_RX (1 << 16)
66 * We wrap our port structure around the generic uart_port.
68 struct uart_amba_port {
69 struct uart_port port;
70 struct clk *clk;
71 unsigned int im; /* interrupt mask */
72 unsigned int old_status;
73 unsigned int ifls; /* vendor-specific */
76 /* There is by now at least one vendor with differing details, so handle it */
77 struct vendor_data {
78 unsigned int ifls;
79 unsigned int fifosize;
82 static struct vendor_data vendor_arm = {
83 .ifls = UART011_IFLS_RX4_8|UART011_IFLS_TX4_8,
84 .fifosize = 16,
87 static struct vendor_data vendor_st = {
88 .ifls = UART011_IFLS_RX_HALF|UART011_IFLS_TX_HALF,
89 .fifosize = 64,
92 static void pl011_stop_tx(struct uart_port *port)
94 struct uart_amba_port *uap = (struct uart_amba_port *)port;
96 uap->im &= ~UART011_TXIM;
97 writew(uap->im, uap->port.membase + UART011_IMSC);
100 static void pl011_start_tx(struct uart_port *port)
102 struct uart_amba_port *uap = (struct uart_amba_port *)port;
104 uap->im |= UART011_TXIM;
105 writew(uap->im, uap->port.membase + UART011_IMSC);
108 static void pl011_stop_rx(struct uart_port *port)
110 struct uart_amba_port *uap = (struct uart_amba_port *)port;
112 uap->im &= ~(UART011_RXIM|UART011_RTIM|UART011_FEIM|
113 UART011_PEIM|UART011_BEIM|UART011_OEIM);
114 writew(uap->im, uap->port.membase + UART011_IMSC);
117 static void pl011_enable_ms(struct uart_port *port)
119 struct uart_amba_port *uap = (struct uart_amba_port *)port;
121 uap->im |= UART011_RIMIM|UART011_CTSMIM|UART011_DCDMIM|UART011_DSRMIM;
122 writew(uap->im, uap->port.membase + UART011_IMSC);
125 static void pl011_rx_chars(struct uart_amba_port *uap)
127 struct tty_struct *tty = uap->port.state->port.tty;
128 unsigned int status, ch, flag, max_count = 256;
130 status = readw(uap->port.membase + UART01x_FR);
131 while ((status & UART01x_FR_RXFE) == 0 && max_count--) {
132 ch = readw(uap->port.membase + UART01x_DR) | UART_DUMMY_DR_RX;
133 flag = TTY_NORMAL;
134 uap->port.icount.rx++;
137 * Note that the error handling code is
138 * out of the main execution path
140 if (unlikely(ch & UART_DR_ERROR)) {
141 if (ch & UART011_DR_BE) {
142 ch &= ~(UART011_DR_FE | UART011_DR_PE);
143 uap->port.icount.brk++;
144 if (uart_handle_break(&uap->port))
145 goto ignore_char;
146 } else if (ch & UART011_DR_PE)
147 uap->port.icount.parity++;
148 else if (ch & UART011_DR_FE)
149 uap->port.icount.frame++;
150 if (ch & UART011_DR_OE)
151 uap->port.icount.overrun++;
153 ch &= uap->port.read_status_mask;
155 if (ch & UART011_DR_BE)
156 flag = TTY_BREAK;
157 else if (ch & UART011_DR_PE)
158 flag = TTY_PARITY;
159 else if (ch & UART011_DR_FE)
160 flag = TTY_FRAME;
163 if (uart_handle_sysrq_char(&uap->port, ch & 255))
164 goto ignore_char;
166 uart_insert_char(&uap->port, ch, UART011_DR_OE, ch, flag);
168 ignore_char:
169 status = readw(uap->port.membase + UART01x_FR);
171 spin_unlock(&uap->port.lock);
172 tty_flip_buffer_push(tty);
173 spin_lock(&uap->port.lock);
176 static void pl011_tx_chars(struct uart_amba_port *uap)
178 struct circ_buf *xmit = &uap->port.state->xmit;
179 int count;
181 if (uap->port.x_char) {
182 writew(uap->port.x_char, uap->port.membase + UART01x_DR);
183 uap->port.icount.tx++;
184 uap->port.x_char = 0;
185 return;
187 if (uart_circ_empty(xmit) || uart_tx_stopped(&uap->port)) {
188 pl011_stop_tx(&uap->port);
189 return;
192 count = uap->port.fifosize >> 1;
193 do {
194 writew(xmit->buf[xmit->tail], uap->port.membase + UART01x_DR);
195 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
196 uap->port.icount.tx++;
197 if (uart_circ_empty(xmit))
198 break;
199 } while (--count > 0);
201 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
202 uart_write_wakeup(&uap->port);
204 if (uart_circ_empty(xmit))
205 pl011_stop_tx(&uap->port);
208 static void pl011_modem_status(struct uart_amba_port *uap)
210 unsigned int status, delta;
212 status = readw(uap->port.membase + UART01x_FR) & UART01x_FR_MODEM_ANY;
214 delta = status ^ uap->old_status;
215 uap->old_status = status;
217 if (!delta)
218 return;
220 if (delta & UART01x_FR_DCD)
221 uart_handle_dcd_change(&uap->port, status & UART01x_FR_DCD);
223 if (delta & UART01x_FR_DSR)
224 uap->port.icount.dsr++;
226 if (delta & UART01x_FR_CTS)
227 uart_handle_cts_change(&uap->port, status & UART01x_FR_CTS);
229 wake_up_interruptible(&uap->port.state->port.delta_msr_wait);
232 static irqreturn_t pl011_int(int irq, void *dev_id)
234 struct uart_amba_port *uap = dev_id;
235 unsigned int status, pass_counter = AMBA_ISR_PASS_LIMIT;
236 int handled = 0;
238 spin_lock(&uap->port.lock);
240 status = readw(uap->port.membase + UART011_MIS);
241 if (status) {
242 do {
243 writew(status & ~(UART011_TXIS|UART011_RTIS|
244 UART011_RXIS),
245 uap->port.membase + UART011_ICR);
247 if (status & (UART011_RTIS|UART011_RXIS))
248 pl011_rx_chars(uap);
249 if (status & (UART011_DSRMIS|UART011_DCDMIS|
250 UART011_CTSMIS|UART011_RIMIS))
251 pl011_modem_status(uap);
252 if (status & UART011_TXIS)
253 pl011_tx_chars(uap);
255 if (pass_counter-- == 0)
256 break;
258 status = readw(uap->port.membase + UART011_MIS);
259 } while (status != 0);
260 handled = 1;
263 spin_unlock(&uap->port.lock);
265 return IRQ_RETVAL(handled);
268 static unsigned int pl01x_tx_empty(struct uart_port *port)
270 struct uart_amba_port *uap = (struct uart_amba_port *)port;
271 unsigned int status = readw(uap->port.membase + UART01x_FR);
272 return status & (UART01x_FR_BUSY|UART01x_FR_TXFF) ? 0 : TIOCSER_TEMT;
275 static unsigned int pl01x_get_mctrl(struct uart_port *port)
277 struct uart_amba_port *uap = (struct uart_amba_port *)port;
278 unsigned int result = 0;
279 unsigned int status = readw(uap->port.membase + UART01x_FR);
281 #define TIOCMBIT(uartbit, tiocmbit) \
282 if (status & uartbit) \
283 result |= tiocmbit
285 TIOCMBIT(UART01x_FR_DCD, TIOCM_CAR);
286 TIOCMBIT(UART01x_FR_DSR, TIOCM_DSR);
287 TIOCMBIT(UART01x_FR_CTS, TIOCM_CTS);
288 TIOCMBIT(UART011_FR_RI, TIOCM_RNG);
289 #undef TIOCMBIT
290 return result;
293 static void pl011_set_mctrl(struct uart_port *port, unsigned int mctrl)
295 struct uart_amba_port *uap = (struct uart_amba_port *)port;
296 unsigned int cr;
298 cr = readw(uap->port.membase + UART011_CR);
300 #define TIOCMBIT(tiocmbit, uartbit) \
301 if (mctrl & tiocmbit) \
302 cr |= uartbit; \
303 else \
304 cr &= ~uartbit
306 TIOCMBIT(TIOCM_RTS, UART011_CR_RTS);
307 TIOCMBIT(TIOCM_DTR, UART011_CR_DTR);
308 TIOCMBIT(TIOCM_OUT1, UART011_CR_OUT1);
309 TIOCMBIT(TIOCM_OUT2, UART011_CR_OUT2);
310 TIOCMBIT(TIOCM_LOOP, UART011_CR_LBE);
311 #undef TIOCMBIT
313 writew(cr, uap->port.membase + UART011_CR);
316 static void pl011_break_ctl(struct uart_port *port, int break_state)
318 struct uart_amba_port *uap = (struct uart_amba_port *)port;
319 unsigned long flags;
320 unsigned int lcr_h;
322 spin_lock_irqsave(&uap->port.lock, flags);
323 lcr_h = readw(uap->port.membase + UART011_LCRH);
324 if (break_state == -1)
325 lcr_h |= UART01x_LCRH_BRK;
326 else
327 lcr_h &= ~UART01x_LCRH_BRK;
328 writew(lcr_h, uap->port.membase + UART011_LCRH);
329 spin_unlock_irqrestore(&uap->port.lock, flags);
332 #ifdef CONFIG_CONSOLE_POLL
333 static int pl010_get_poll_char(struct uart_port *port)
335 struct uart_amba_port *uap = (struct uart_amba_port *)port;
336 unsigned int status;
338 do {
339 status = readw(uap->port.membase + UART01x_FR);
340 } while (status & UART01x_FR_RXFE);
342 return readw(uap->port.membase + UART01x_DR);
345 static void pl010_put_poll_char(struct uart_port *port,
346 unsigned char ch)
348 struct uart_amba_port *uap = (struct uart_amba_port *)port;
350 while (readw(uap->port.membase + UART01x_FR) & UART01x_FR_TXFF)
351 barrier();
353 writew(ch, uap->port.membase + UART01x_DR);
356 #endif /* CONFIG_CONSOLE_POLL */
358 static int pl011_startup(struct uart_port *port)
360 struct uart_amba_port *uap = (struct uart_amba_port *)port;
361 unsigned int cr;
362 int retval;
365 * Try to enable the clock producer.
367 retval = clk_enable(uap->clk);
368 if (retval)
369 goto out;
371 uap->port.uartclk = clk_get_rate(uap->clk);
374 * Allocate the IRQ
376 retval = request_irq(uap->port.irq, pl011_int, 0, "uart-pl011", uap);
377 if (retval)
378 goto clk_dis;
380 writew(uap->ifls, uap->port.membase + UART011_IFLS);
383 * Provoke TX FIFO interrupt into asserting.
385 cr = UART01x_CR_UARTEN | UART011_CR_TXE | UART011_CR_LBE;
386 writew(cr, uap->port.membase + UART011_CR);
387 writew(0, uap->port.membase + UART011_FBRD);
388 writew(1, uap->port.membase + UART011_IBRD);
389 writew(0, uap->port.membase + UART011_LCRH);
390 writew(0, uap->port.membase + UART01x_DR);
391 while (readw(uap->port.membase + UART01x_FR) & UART01x_FR_BUSY)
392 barrier();
394 cr = UART01x_CR_UARTEN | UART011_CR_RXE | UART011_CR_TXE;
395 writew(cr, uap->port.membase + UART011_CR);
398 * initialise the old status of the modem signals
400 uap->old_status = readw(uap->port.membase + UART01x_FR) & UART01x_FR_MODEM_ANY;
403 * Finally, enable interrupts
405 spin_lock_irq(&uap->port.lock);
406 uap->im = UART011_RXIM | UART011_RTIM;
407 writew(uap->im, uap->port.membase + UART011_IMSC);
408 spin_unlock_irq(&uap->port.lock);
410 return 0;
412 clk_dis:
413 clk_disable(uap->clk);
414 out:
415 return retval;
418 static void pl011_shutdown(struct uart_port *port)
420 struct uart_amba_port *uap = (struct uart_amba_port *)port;
421 unsigned long val;
424 * disable all interrupts
426 spin_lock_irq(&uap->port.lock);
427 uap->im = 0;
428 writew(uap->im, uap->port.membase + UART011_IMSC);
429 writew(0xffff, uap->port.membase + UART011_ICR);
430 spin_unlock_irq(&uap->port.lock);
433 * Free the interrupt
435 free_irq(uap->port.irq, uap);
438 * disable the port
440 writew(UART01x_CR_UARTEN | UART011_CR_TXE, uap->port.membase + UART011_CR);
443 * disable break condition and fifos
445 val = readw(uap->port.membase + UART011_LCRH);
446 val &= ~(UART01x_LCRH_BRK | UART01x_LCRH_FEN);
447 writew(val, uap->port.membase + UART011_LCRH);
450 * Shut down the clock producer
452 clk_disable(uap->clk);
455 static void
456 pl011_set_termios(struct uart_port *port, struct ktermios *termios,
457 struct ktermios *old)
459 unsigned int lcr_h, old_cr;
460 unsigned long flags;
461 unsigned int baud, quot;
464 * Ask the core to calculate the divisor for us.
466 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
467 quot = port->uartclk * 4 / baud;
469 switch (termios->c_cflag & CSIZE) {
470 case CS5:
471 lcr_h = UART01x_LCRH_WLEN_5;
472 break;
473 case CS6:
474 lcr_h = UART01x_LCRH_WLEN_6;
475 break;
476 case CS7:
477 lcr_h = UART01x_LCRH_WLEN_7;
478 break;
479 default: // CS8
480 lcr_h = UART01x_LCRH_WLEN_8;
481 break;
483 if (termios->c_cflag & CSTOPB)
484 lcr_h |= UART01x_LCRH_STP2;
485 if (termios->c_cflag & PARENB) {
486 lcr_h |= UART01x_LCRH_PEN;
487 if (!(termios->c_cflag & PARODD))
488 lcr_h |= UART01x_LCRH_EPS;
490 if (port->fifosize > 1)
491 lcr_h |= UART01x_LCRH_FEN;
493 spin_lock_irqsave(&port->lock, flags);
496 * Update the per-port timeout.
498 uart_update_timeout(port, termios->c_cflag, baud);
500 port->read_status_mask = UART011_DR_OE | 255;
501 if (termios->c_iflag & INPCK)
502 port->read_status_mask |= UART011_DR_FE | UART011_DR_PE;
503 if (termios->c_iflag & (BRKINT | PARMRK))
504 port->read_status_mask |= UART011_DR_BE;
507 * Characters to ignore
509 port->ignore_status_mask = 0;
510 if (termios->c_iflag & IGNPAR)
511 port->ignore_status_mask |= UART011_DR_FE | UART011_DR_PE;
512 if (termios->c_iflag & IGNBRK) {
513 port->ignore_status_mask |= UART011_DR_BE;
515 * If we're ignoring parity and break indicators,
516 * ignore overruns too (for real raw support).
518 if (termios->c_iflag & IGNPAR)
519 port->ignore_status_mask |= UART011_DR_OE;
523 * Ignore all characters if CREAD is not set.
525 if ((termios->c_cflag & CREAD) == 0)
526 port->ignore_status_mask |= UART_DUMMY_DR_RX;
528 if (UART_ENABLE_MS(port, termios->c_cflag))
529 pl011_enable_ms(port);
531 /* first, disable everything */
532 old_cr = readw(port->membase + UART011_CR);
533 writew(0, port->membase + UART011_CR);
535 /* Set baud rate */
536 writew(quot & 0x3f, port->membase + UART011_FBRD);
537 writew(quot >> 6, port->membase + UART011_IBRD);
540 * ----------v----------v----------v----------v-----
541 * NOTE: MUST BE WRITTEN AFTER UARTLCR_M & UARTLCR_L
542 * ----------^----------^----------^----------^-----
544 writew(lcr_h, port->membase + UART011_LCRH);
545 writew(old_cr, port->membase + UART011_CR);
547 spin_unlock_irqrestore(&port->lock, flags);
550 static const char *pl011_type(struct uart_port *port)
552 return port->type == PORT_AMBA ? "AMBA/PL011" : NULL;
556 * Release the memory region(s) being used by 'port'
558 static void pl010_release_port(struct uart_port *port)
560 release_mem_region(port->mapbase, SZ_4K);
564 * Request the memory region(s) being used by 'port'
566 static int pl010_request_port(struct uart_port *port)
568 return request_mem_region(port->mapbase, SZ_4K, "uart-pl011")
569 != NULL ? 0 : -EBUSY;
573 * Configure/autoconfigure the port.
575 static void pl010_config_port(struct uart_port *port, int flags)
577 if (flags & UART_CONFIG_TYPE) {
578 port->type = PORT_AMBA;
579 pl010_request_port(port);
584 * verify the new serial_struct (for TIOCSSERIAL).
586 static int pl010_verify_port(struct uart_port *port, struct serial_struct *ser)
588 int ret = 0;
589 if (ser->type != PORT_UNKNOWN && ser->type != PORT_AMBA)
590 ret = -EINVAL;
591 if (ser->irq < 0 || ser->irq >= nr_irqs)
592 ret = -EINVAL;
593 if (ser->baud_base < 9600)
594 ret = -EINVAL;
595 return ret;
598 static struct uart_ops amba_pl011_pops = {
599 .tx_empty = pl01x_tx_empty,
600 .set_mctrl = pl011_set_mctrl,
601 .get_mctrl = pl01x_get_mctrl,
602 .stop_tx = pl011_stop_tx,
603 .start_tx = pl011_start_tx,
604 .stop_rx = pl011_stop_rx,
605 .enable_ms = pl011_enable_ms,
606 .break_ctl = pl011_break_ctl,
607 .startup = pl011_startup,
608 .shutdown = pl011_shutdown,
609 .set_termios = pl011_set_termios,
610 .type = pl011_type,
611 .release_port = pl010_release_port,
612 .request_port = pl010_request_port,
613 .config_port = pl010_config_port,
614 .verify_port = pl010_verify_port,
615 #ifdef CONFIG_CONSOLE_POLL
616 .poll_get_char = pl010_get_poll_char,
617 .poll_put_char = pl010_put_poll_char,
618 #endif
621 static struct uart_amba_port *amba_ports[UART_NR];
623 #ifdef CONFIG_SERIAL_AMBA_PL011_CONSOLE
625 static void pl011_console_putchar(struct uart_port *port, int ch)
627 struct uart_amba_port *uap = (struct uart_amba_port *)port;
629 while (readw(uap->port.membase + UART01x_FR) & UART01x_FR_TXFF)
630 barrier();
631 writew(ch, uap->port.membase + UART01x_DR);
634 static void
635 pl011_console_write(struct console *co, const char *s, unsigned int count)
637 struct uart_amba_port *uap = amba_ports[co->index];
638 unsigned int status, old_cr, new_cr;
640 clk_enable(uap->clk);
643 * First save the CR then disable the interrupts
645 old_cr = readw(uap->port.membase + UART011_CR);
646 new_cr = old_cr & ~UART011_CR_CTSEN;
647 new_cr |= UART01x_CR_UARTEN | UART011_CR_TXE;
648 writew(new_cr, uap->port.membase + UART011_CR);
650 uart_console_write(&uap->port, s, count, pl011_console_putchar);
653 * Finally, wait for transmitter to become empty
654 * and restore the TCR
656 do {
657 status = readw(uap->port.membase + UART01x_FR);
658 } while (status & UART01x_FR_BUSY);
659 writew(old_cr, uap->port.membase + UART011_CR);
661 clk_disable(uap->clk);
664 static void __init
665 pl011_console_get_options(struct uart_amba_port *uap, int *baud,
666 int *parity, int *bits)
668 if (readw(uap->port.membase + UART011_CR) & UART01x_CR_UARTEN) {
669 unsigned int lcr_h, ibrd, fbrd;
671 lcr_h = readw(uap->port.membase + UART011_LCRH);
673 *parity = 'n';
674 if (lcr_h & UART01x_LCRH_PEN) {
675 if (lcr_h & UART01x_LCRH_EPS)
676 *parity = 'e';
677 else
678 *parity = 'o';
681 if ((lcr_h & 0x60) == UART01x_LCRH_WLEN_7)
682 *bits = 7;
683 else
684 *bits = 8;
686 ibrd = readw(uap->port.membase + UART011_IBRD);
687 fbrd = readw(uap->port.membase + UART011_FBRD);
689 *baud = uap->port.uartclk * 4 / (64 * ibrd + fbrd);
693 static int __init pl011_console_setup(struct console *co, char *options)
695 struct uart_amba_port *uap;
696 int baud = 38400;
697 int bits = 8;
698 int parity = 'n';
699 int flow = 'n';
702 * Check whether an invalid uart number has been specified, and
703 * if so, search for the first available port that does have
704 * console support.
706 if (co->index >= UART_NR)
707 co->index = 0;
708 uap = amba_ports[co->index];
709 if (!uap)
710 return -ENODEV;
712 uap->port.uartclk = clk_get_rate(uap->clk);
714 if (options)
715 uart_parse_options(options, &baud, &parity, &bits, &flow);
716 else
717 pl011_console_get_options(uap, &baud, &parity, &bits);
719 return uart_set_options(&uap->port, co, baud, parity, bits, flow);
722 static struct uart_driver amba_reg;
723 static struct console amba_console = {
724 .name = "ttyAMA",
725 .write = pl011_console_write,
726 .device = uart_console_device,
727 .setup = pl011_console_setup,
728 .flags = CON_PRINTBUFFER,
729 .index = -1,
730 .data = &amba_reg,
733 #define AMBA_CONSOLE (&amba_console)
734 #else
735 #define AMBA_CONSOLE NULL
736 #endif
738 static struct uart_driver amba_reg = {
739 .owner = THIS_MODULE,
740 .driver_name = "ttyAMA",
741 .dev_name = "ttyAMA",
742 .major = SERIAL_AMBA_MAJOR,
743 .minor = SERIAL_AMBA_MINOR,
744 .nr = UART_NR,
745 .cons = AMBA_CONSOLE,
748 static int pl011_probe(struct amba_device *dev, struct amba_id *id)
750 struct uart_amba_port *uap;
751 struct vendor_data *vendor = id->data;
752 void __iomem *base;
753 int i, ret;
755 for (i = 0; i < ARRAY_SIZE(amba_ports); i++)
756 if (amba_ports[i] == NULL)
757 break;
759 if (i == ARRAY_SIZE(amba_ports)) {
760 ret = -EBUSY;
761 goto out;
764 uap = kzalloc(sizeof(struct uart_amba_port), GFP_KERNEL);
765 if (uap == NULL) {
766 ret = -ENOMEM;
767 goto out;
770 base = ioremap(dev->res.start, resource_size(&dev->res));
771 if (!base) {
772 ret = -ENOMEM;
773 goto free;
776 uap->clk = clk_get(&dev->dev, NULL);
777 if (IS_ERR(uap->clk)) {
778 ret = PTR_ERR(uap->clk);
779 goto unmap;
782 uap->ifls = vendor->ifls;
783 uap->port.dev = &dev->dev;
784 uap->port.mapbase = dev->res.start;
785 uap->port.membase = base;
786 uap->port.iotype = UPIO_MEM;
787 uap->port.irq = dev->irq[0];
788 uap->port.fifosize = vendor->fifosize;
789 uap->port.ops = &amba_pl011_pops;
790 uap->port.flags = UPF_BOOT_AUTOCONF;
791 uap->port.line = i;
793 amba_ports[i] = uap;
795 amba_set_drvdata(dev, uap);
796 ret = uart_add_one_port(&amba_reg, &uap->port);
797 if (ret) {
798 amba_set_drvdata(dev, NULL);
799 amba_ports[i] = NULL;
800 clk_put(uap->clk);
801 unmap:
802 iounmap(base);
803 free:
804 kfree(uap);
806 out:
807 return ret;
810 static int pl011_remove(struct amba_device *dev)
812 struct uart_amba_port *uap = amba_get_drvdata(dev);
813 int i;
815 amba_set_drvdata(dev, NULL);
817 uart_remove_one_port(&amba_reg, &uap->port);
819 for (i = 0; i < ARRAY_SIZE(amba_ports); i++)
820 if (amba_ports[i] == uap)
821 amba_ports[i] = NULL;
823 iounmap(uap->port.membase);
824 clk_put(uap->clk);
825 kfree(uap);
826 return 0;
829 #ifdef CONFIG_PM
830 static int pl011_suspend(struct amba_device *dev, pm_message_t state)
832 struct uart_amba_port *uap = amba_get_drvdata(dev);
834 if (!uap)
835 return -EINVAL;
837 return uart_suspend_port(&amba_reg, &uap->port);
840 static int pl011_resume(struct amba_device *dev)
842 struct uart_amba_port *uap = amba_get_drvdata(dev);
844 if (!uap)
845 return -EINVAL;
847 return uart_resume_port(&amba_reg, &uap->port);
849 #endif
851 static struct amba_id pl011_ids[] __initdata = {
853 .id = 0x00041011,
854 .mask = 0x000fffff,
855 .data = &vendor_arm,
858 .id = 0x00380802,
859 .mask = 0x00ffffff,
860 .data = &vendor_st,
862 { 0, 0 },
865 static struct amba_driver pl011_driver = {
866 .drv = {
867 .name = "uart-pl011",
869 .id_table = pl011_ids,
870 .probe = pl011_probe,
871 .remove = pl011_remove,
872 #ifdef CONFIG_PM
873 .suspend = pl011_suspend,
874 .resume = pl011_resume,
875 #endif
878 static int __init pl011_init(void)
880 int ret;
881 printk(KERN_INFO "Serial: AMBA PL011 UART driver\n");
883 ret = uart_register_driver(&amba_reg);
884 if (ret == 0) {
885 ret = amba_driver_register(&pl011_driver);
886 if (ret)
887 uart_unregister_driver(&amba_reg);
889 return ret;
892 static void __exit pl011_exit(void)
894 amba_driver_unregister(&pl011_driver);
895 uart_unregister_driver(&amba_reg);
899 * While this can be a module, if builtin it's most likely the console
900 * So let's leave module_exit but move module_init to an earlier place
902 arch_initcall(pl011_init);
903 module_exit(pl011_exit);
905 MODULE_AUTHOR("ARM Ltd/Deep Blue Solutions Ltd");
906 MODULE_DESCRIPTION("ARM AMBA serial port driver");
907 MODULE_LICENSE("GPL");