Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/ericvh...
[deliverable/linux.git] / drivers / serial / amba-pl010.c
CommitLineData
1da177e4
LT
1/*
2 * linux/drivers/char/amba.c
3 *
4 * Driver for AMBA serial ports
5 *
6 * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
7 *
8 * Copyright 1999 ARM Limited
9 * Copyright (C) 2000 Deep Blue Solutions Ltd.
10 *
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.
15 *
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.
20 *
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
24 *
25 * $Id: amba.c,v 1.41 2002/07/28 10:03:27 rmk Exp $
26 *
27 * This is a generic driver for ARM AMBA-type serial ports. They
28 * have a lot of 16550-like features, but are not register compatible.
29 * Note that although they do have CTS, DCD and DSR inputs, they do
30 * not have an RI input, nor do they have DTR or RTS outputs. If
31 * required, these have to be supplied via some other means (eg, GPIO)
32 * and hooked into this driver.
33 */
1da177e4
LT
34
35#if defined(CONFIG_SERIAL_AMBA_PL010_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
36#define SUPPORT_SYSRQ
37#endif
38
39#include <linux/module.h>
40#include <linux/ioport.h>
41#include <linux/init.h>
42#include <linux/console.h>
43#include <linux/sysrq.h>
44#include <linux/device.h>
45#include <linux/tty.h>
46#include <linux/tty_flip.h>
47#include <linux/serial_core.h>
48#include <linux/serial.h>
a62c80e5
RK
49#include <linux/amba/bus.h>
50#include <linux/amba/serial.h>
ed519ded 51#include <linux/clk.h>
1da177e4
LT
52
53#include <asm/io.h>
1da177e4 54
4faf4e0e 55#define UART_NR 8
1da177e4
LT
56
57#define SERIAL_AMBA_MAJOR 204
58#define SERIAL_AMBA_MINOR 16
59#define SERIAL_AMBA_NR UART_NR
60
61#define AMBA_ISR_PASS_LIMIT 256
62
1da177e4
LT
63#define UART_RX_DATA(s) (((s) & UART01x_FR_RXFE) == 0)
64#define UART_TX_READY(s) (((s) & UART01x_FR_TXFF) == 0)
1da177e4 65
fbb18a27 66#define UART_DUMMY_RSR_RX 256
1da177e4
LT
67#define UART_PORT_SIZE 64
68
1da177e4
LT
69/*
70 * We wrap our port structure around the generic uart_port.
71 */
72struct uart_amba_port {
73 struct uart_port port;
ed519ded 74 struct clk *clk;
fbb18a27
RK
75 struct amba_device *dev;
76 struct amba_pl010_data *data;
1da177e4
LT
77 unsigned int old_status;
78};
79
b129a8cc 80static void pl010_stop_tx(struct uart_port *port)
1da177e4 81{
1b0646a0 82 struct uart_amba_port *uap = (struct uart_amba_port *)port;
1da177e4
LT
83 unsigned int cr;
84
1b0646a0 85 cr = readb(uap->port.membase + UART010_CR);
1da177e4 86 cr &= ~UART010_CR_TIE;
1b0646a0 87 writel(cr, uap->port.membase + UART010_CR);
1da177e4
LT
88}
89
b129a8cc 90static void pl010_start_tx(struct uart_port *port)
1da177e4 91{
1b0646a0 92 struct uart_amba_port *uap = (struct uart_amba_port *)port;
1da177e4
LT
93 unsigned int cr;
94
1b0646a0 95 cr = readb(uap->port.membase + UART010_CR);
1da177e4 96 cr |= UART010_CR_TIE;
1b0646a0 97 writel(cr, uap->port.membase + UART010_CR);
1da177e4
LT
98}
99
100static void pl010_stop_rx(struct uart_port *port)
101{
1b0646a0 102 struct uart_amba_port *uap = (struct uart_amba_port *)port;
1da177e4
LT
103 unsigned int cr;
104
1b0646a0 105 cr = readb(uap->port.membase + UART010_CR);
1da177e4 106 cr &= ~(UART010_CR_RIE | UART010_CR_RTIE);
1b0646a0 107 writel(cr, uap->port.membase + UART010_CR);
1da177e4
LT
108}
109
110static void pl010_enable_ms(struct uart_port *port)
111{
1b0646a0 112 struct uart_amba_port *uap = (struct uart_amba_port *)port;
1da177e4
LT
113 unsigned int cr;
114
1b0646a0 115 cr = readb(uap->port.membase + UART010_CR);
1da177e4 116 cr |= UART010_CR_MSIE;
1b0646a0 117 writel(cr, uap->port.membase + UART010_CR);
1da177e4
LT
118}
119
1b0646a0 120static void pl010_rx_chars(struct uart_amba_port *uap)
1da177e4 121{
1b0646a0 122 struct tty_struct *tty = uap->port.info->tty;
1da177e4
LT
123 unsigned int status, ch, flag, rsr, max_count = 256;
124
1b0646a0 125 status = readb(uap->port.membase + UART01x_FR);
1da177e4 126 while (UART_RX_DATA(status) && max_count--) {
1b0646a0 127 ch = readb(uap->port.membase + UART01x_DR);
1da177e4
LT
128 flag = TTY_NORMAL;
129
1b0646a0 130 uap->port.icount.rx++;
1da177e4
LT
131
132 /*
133 * Note that the error handling code is
134 * out of the main execution path
135 */
1b0646a0 136 rsr = readb(uap->port.membase + UART01x_RSR) | UART_DUMMY_RSR_RX;
45849282 137 if (unlikely(rsr & UART01x_RSR_ANY)) {
1b0646a0 138 writel(0, uap->port.membase + UART01x_ECR);
a4ed06ad 139
1da177e4
LT
140 if (rsr & UART01x_RSR_BE) {
141 rsr &= ~(UART01x_RSR_FE | UART01x_RSR_PE);
1b0646a0
RK
142 uap->port.icount.brk++;
143 if (uart_handle_break(&uap->port))
1da177e4
LT
144 goto ignore_char;
145 } else if (rsr & UART01x_RSR_PE)
1b0646a0 146 uap->port.icount.parity++;
1da177e4 147 else if (rsr & UART01x_RSR_FE)
1b0646a0 148 uap->port.icount.frame++;
1da177e4 149 if (rsr & UART01x_RSR_OE)
1b0646a0 150 uap->port.icount.overrun++;
1da177e4 151
1b0646a0 152 rsr &= uap->port.read_status_mask;
1da177e4
LT
153
154 if (rsr & UART01x_RSR_BE)
155 flag = TTY_BREAK;
156 else if (rsr & UART01x_RSR_PE)
157 flag = TTY_PARITY;
158 else if (rsr & UART01x_RSR_FE)
159 flag = TTY_FRAME;
160 }
161
1b0646a0 162 if (uart_handle_sysrq_char(&uap->port, ch))
1da177e4
LT
163 goto ignore_char;
164
1b0646a0 165 uart_insert_char(&uap->port, rsr, UART01x_RSR_OE, ch, flag);
05ab3014 166
1da177e4 167 ignore_char:
1b0646a0 168 status = readb(uap->port.membase + UART01x_FR);
1da177e4 169 }
db002b85 170 spin_unlock(&uap->port.lock);
1da177e4 171 tty_flip_buffer_push(tty);
db002b85 172 spin_lock(&uap->port.lock);
1da177e4
LT
173}
174
1b0646a0 175static void pl010_tx_chars(struct uart_amba_port *uap)
1da177e4 176{
1b0646a0 177 struct circ_buf *xmit = &uap->port.info->xmit;
1da177e4
LT
178 int count;
179
1b0646a0
RK
180 if (uap->port.x_char) {
181 writel(uap->port.x_char, uap->port.membase + UART01x_DR);
182 uap->port.icount.tx++;
183 uap->port.x_char = 0;
1da177e4
LT
184 return;
185 }
1b0646a0
RK
186 if (uart_circ_empty(xmit) || uart_tx_stopped(&uap->port)) {
187 pl010_stop_tx(&uap->port);
1da177e4
LT
188 return;
189 }
190
1b0646a0 191 count = uap->port.fifosize >> 1;
1da177e4 192 do {
1b0646a0 193 writel(xmit->buf[xmit->tail], uap->port.membase + UART01x_DR);
1da177e4 194 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
1b0646a0 195 uap->port.icount.tx++;
1da177e4
LT
196 if (uart_circ_empty(xmit))
197 break;
198 } while (--count > 0);
199
200 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
1b0646a0 201 uart_write_wakeup(&uap->port);
1da177e4
LT
202
203 if (uart_circ_empty(xmit))
1b0646a0 204 pl010_stop_tx(&uap->port);
1da177e4
LT
205}
206
1b0646a0 207static void pl010_modem_status(struct uart_amba_port *uap)
1da177e4 208{
1da177e4
LT
209 unsigned int status, delta;
210
98639a67 211 writel(0, uap->port.membase + UART010_ICR);
1da177e4 212
98639a67 213 status = readb(uap->port.membase + UART01x_FR) & UART01x_FR_MODEM_ANY;
1da177e4
LT
214
215 delta = status ^ uap->old_status;
216 uap->old_status = status;
217
218 if (!delta)
219 return;
220
221 if (delta & UART01x_FR_DCD)
222 uart_handle_dcd_change(&uap->port, status & UART01x_FR_DCD);
223
224 if (delta & UART01x_FR_DSR)
225 uap->port.icount.dsr++;
226
227 if (delta & UART01x_FR_CTS)
228 uart_handle_cts_change(&uap->port, status & UART01x_FR_CTS);
229
230 wake_up_interruptible(&uap->port.info->delta_msr_wait);
231}
232
7d12e780 233static irqreturn_t pl010_int(int irq, void *dev_id)
1da177e4 234{
1b0646a0 235 struct uart_amba_port *uap = dev_id;
1da177e4
LT
236 unsigned int status, pass_counter = AMBA_ISR_PASS_LIMIT;
237 int handled = 0;
238
1b0646a0 239 spin_lock(&uap->port.lock);
1da177e4 240
1b0646a0 241 status = readb(uap->port.membase + UART010_IIR);
1da177e4
LT
242 if (status) {
243 do {
244 if (status & (UART010_IIR_RTIS | UART010_IIR_RIS))
1b0646a0 245 pl010_rx_chars(uap);
1da177e4 246 if (status & UART010_IIR_MIS)
1b0646a0 247 pl010_modem_status(uap);
1da177e4 248 if (status & UART010_IIR_TIS)
1b0646a0 249 pl010_tx_chars(uap);
1da177e4
LT
250
251 if (pass_counter-- == 0)
252 break;
253
1b0646a0 254 status = readb(uap->port.membase + UART010_IIR);
1da177e4
LT
255 } while (status & (UART010_IIR_RTIS | UART010_IIR_RIS |
256 UART010_IIR_TIS));
257 handled = 1;
258 }
259
1b0646a0 260 spin_unlock(&uap->port.lock);
1da177e4
LT
261
262 return IRQ_RETVAL(handled);
263}
264
265static unsigned int pl010_tx_empty(struct uart_port *port)
266{
1b0646a0
RK
267 struct uart_amba_port *uap = (struct uart_amba_port *)port;
268 unsigned int status = readb(uap->port.membase + UART01x_FR);
269 return status & UART01x_FR_BUSY ? 0 : TIOCSER_TEMT;
1da177e4
LT
270}
271
272static unsigned int pl010_get_mctrl(struct uart_port *port)
273{
1b0646a0 274 struct uart_amba_port *uap = (struct uart_amba_port *)port;
1da177e4
LT
275 unsigned int result = 0;
276 unsigned int status;
277
1b0646a0 278 status = readb(uap->port.membase + UART01x_FR);
1da177e4
LT
279 if (status & UART01x_FR_DCD)
280 result |= TIOCM_CAR;
281 if (status & UART01x_FR_DSR)
282 result |= TIOCM_DSR;
283 if (status & UART01x_FR_CTS)
284 result |= TIOCM_CTS;
285
286 return result;
287}
288
289static void pl010_set_mctrl(struct uart_port *port, unsigned int mctrl)
290{
291 struct uart_amba_port *uap = (struct uart_amba_port *)port;
1da177e4 292
fbb18a27
RK
293 if (uap->data)
294 uap->data->set_mctrl(uap->dev, uap->port.membase, mctrl);
1da177e4
LT
295}
296
297static void pl010_break_ctl(struct uart_port *port, int break_state)
298{
1b0646a0 299 struct uart_amba_port *uap = (struct uart_amba_port *)port;
1da177e4
LT
300 unsigned long flags;
301 unsigned int lcr_h;
302
1b0646a0
RK
303 spin_lock_irqsave(&uap->port.lock, flags);
304 lcr_h = readb(uap->port.membase + UART010_LCRH);
1da177e4
LT
305 if (break_state == -1)
306 lcr_h |= UART01x_LCRH_BRK;
307 else
308 lcr_h &= ~UART01x_LCRH_BRK;
1b0646a0
RK
309 writel(lcr_h, uap->port.membase + UART010_LCRH);
310 spin_unlock_irqrestore(&uap->port.lock, flags);
1da177e4
LT
311}
312
313static int pl010_startup(struct uart_port *port)
314{
315 struct uart_amba_port *uap = (struct uart_amba_port *)port;
316 int retval;
317
ed519ded
RK
318 /*
319 * Try to enable the clock producer.
320 */
321 retval = clk_enable(uap->clk);
322 if (retval)
323 goto out;
324
325 uap->port.uartclk = clk_get_rate(uap->clk);
326
1da177e4
LT
327 /*
328 * Allocate the IRQ
329 */
1b0646a0 330 retval = request_irq(uap->port.irq, pl010_int, 0, "uart-pl010", uap);
1da177e4 331 if (retval)
ed519ded 332 goto clk_dis;
1da177e4
LT
333
334 /*
335 * initialise the old status of the modem signals
336 */
1b0646a0 337 uap->old_status = readb(uap->port.membase + UART01x_FR) & UART01x_FR_MODEM_ANY;
1da177e4
LT
338
339 /*
340 * Finally, enable interrupts
341 */
98639a67 342 writel(UART01x_CR_UARTEN | UART010_CR_RIE | UART010_CR_RTIE,
1b0646a0 343 uap->port.membase + UART010_CR);
1da177e4
LT
344
345 return 0;
ed519ded
RK
346
347 clk_dis:
348 clk_disable(uap->clk);
349 out:
350 return retval;
1da177e4
LT
351}
352
353static void pl010_shutdown(struct uart_port *port)
354{
1b0646a0
RK
355 struct uart_amba_port *uap = (struct uart_amba_port *)port;
356
1da177e4
LT
357 /*
358 * Free the interrupt
359 */
1b0646a0 360 free_irq(uap->port.irq, uap);
1da177e4
LT
361
362 /*
363 * disable all interrupts, disable the port
364 */
1b0646a0 365 writel(0, uap->port.membase + UART010_CR);
1da177e4
LT
366
367 /* disable break condition and fifos */
1b0646a0 368 writel(readb(uap->port.membase + UART010_LCRH) &
98639a67 369 ~(UART01x_LCRH_BRK | UART01x_LCRH_FEN),
1b0646a0 370 uap->port.membase + UART010_LCRH);
ed519ded
RK
371
372 /*
373 * Shut down the clock producer
374 */
375 clk_disable(uap->clk);
1da177e4
LT
376}
377
378static void
606d099c
AC
379pl010_set_termios(struct uart_port *port, struct ktermios *termios,
380 struct ktermios *old)
1da177e4 381{
1b0646a0 382 struct uart_amba_port *uap = (struct uart_amba_port *)port;
1da177e4
LT
383 unsigned int lcr_h, old_cr;
384 unsigned long flags;
385 unsigned int baud, quot;
386
387 /*
388 * Ask the core to calculate the divisor for us.
389 */
1b0646a0 390 baud = uart_get_baud_rate(port, termios, old, 0, uap->port.uartclk/16);
1da177e4
LT
391 quot = uart_get_divisor(port, baud);
392
393 switch (termios->c_cflag & CSIZE) {
394 case CS5:
395 lcr_h = UART01x_LCRH_WLEN_5;
396 break;
397 case CS6:
398 lcr_h = UART01x_LCRH_WLEN_6;
399 break;
400 case CS7:
401 lcr_h = UART01x_LCRH_WLEN_7;
402 break;
403 default: // CS8
404 lcr_h = UART01x_LCRH_WLEN_8;
405 break;
406 }
407 if (termios->c_cflag & CSTOPB)
408 lcr_h |= UART01x_LCRH_STP2;
409 if (termios->c_cflag & PARENB) {
410 lcr_h |= UART01x_LCRH_PEN;
411 if (!(termios->c_cflag & PARODD))
412 lcr_h |= UART01x_LCRH_EPS;
413 }
1b0646a0 414 if (uap->port.fifosize > 1)
1da177e4
LT
415 lcr_h |= UART01x_LCRH_FEN;
416
1b0646a0 417 spin_lock_irqsave(&uap->port.lock, flags);
1da177e4
LT
418
419 /*
420 * Update the per-port timeout.
421 */
422 uart_update_timeout(port, termios->c_cflag, baud);
423
1b0646a0 424 uap->port.read_status_mask = UART01x_RSR_OE;
1da177e4 425 if (termios->c_iflag & INPCK)
1b0646a0 426 uap->port.read_status_mask |= UART01x_RSR_FE | UART01x_RSR_PE;
1da177e4 427 if (termios->c_iflag & (BRKINT | PARMRK))
1b0646a0 428 uap->port.read_status_mask |= UART01x_RSR_BE;
1da177e4
LT
429
430 /*
431 * Characters to ignore
432 */
1b0646a0 433 uap->port.ignore_status_mask = 0;
1da177e4 434 if (termios->c_iflag & IGNPAR)
1b0646a0 435 uap->port.ignore_status_mask |= UART01x_RSR_FE | UART01x_RSR_PE;
1da177e4 436 if (termios->c_iflag & IGNBRK) {
1b0646a0 437 uap->port.ignore_status_mask |= UART01x_RSR_BE;
1da177e4
LT
438 /*
439 * If we're ignoring parity and break indicators,
440 * ignore overruns too (for real raw support).
441 */
442 if (termios->c_iflag & IGNPAR)
1b0646a0 443 uap->port.ignore_status_mask |= UART01x_RSR_OE;
1da177e4
LT
444 }
445
446 /*
447 * Ignore all characters if CREAD is not set.
448 */
449 if ((termios->c_cflag & CREAD) == 0)
1b0646a0 450 uap->port.ignore_status_mask |= UART_DUMMY_RSR_RX;
1da177e4
LT
451
452 /* first, disable everything */
1b0646a0 453 old_cr = readb(uap->port.membase + UART010_CR) & ~UART010_CR_MSIE;
1da177e4
LT
454
455 if (UART_ENABLE_MS(port, termios->c_cflag))
456 old_cr |= UART010_CR_MSIE;
457
1b0646a0 458 writel(0, uap->port.membase + UART010_CR);
1da177e4
LT
459
460 /* Set baud rate */
461 quot -= 1;
1b0646a0
RK
462 writel((quot & 0xf00) >> 8, uap->port.membase + UART010_LCRM);
463 writel(quot & 0xff, uap->port.membase + UART010_LCRL);
1da177e4
LT
464
465 /*
466 * ----------v----------v----------v----------v-----
467 * NOTE: MUST BE WRITTEN AFTER UARTLCR_M & UARTLCR_L
468 * ----------^----------^----------^----------^-----
469 */
1b0646a0
RK
470 writel(lcr_h, uap->port.membase + UART010_LCRH);
471 writel(old_cr, uap->port.membase + UART010_CR);
1da177e4 472
1b0646a0 473 spin_unlock_irqrestore(&uap->port.lock, flags);
1da177e4
LT
474}
475
476static const char *pl010_type(struct uart_port *port)
477{
478 return port->type == PORT_AMBA ? "AMBA" : NULL;
479}
480
481/*
482 * Release the memory region(s) being used by 'port'
483 */
484static void pl010_release_port(struct uart_port *port)
485{
486 release_mem_region(port->mapbase, UART_PORT_SIZE);
487}
488
489/*
490 * Request the memory region(s) being used by 'port'
491 */
492static int pl010_request_port(struct uart_port *port)
493{
494 return request_mem_region(port->mapbase, UART_PORT_SIZE, "uart-pl010")
495 != NULL ? 0 : -EBUSY;
496}
497
498/*
499 * Configure/autoconfigure the port.
500 */
501static void pl010_config_port(struct uart_port *port, int flags)
502{
503 if (flags & UART_CONFIG_TYPE) {
504 port->type = PORT_AMBA;
505 pl010_request_port(port);
506 }
507}
508
509/*
510 * verify the new serial_struct (for TIOCSSERIAL).
511 */
512static int pl010_verify_port(struct uart_port *port, struct serial_struct *ser)
513{
514 int ret = 0;
515 if (ser->type != PORT_UNKNOWN && ser->type != PORT_AMBA)
516 ret = -EINVAL;
517 if (ser->irq < 0 || ser->irq >= NR_IRQS)
518 ret = -EINVAL;
519 if (ser->baud_base < 9600)
520 ret = -EINVAL;
521 return ret;
522}
523
524static struct uart_ops amba_pl010_pops = {
525 .tx_empty = pl010_tx_empty,
526 .set_mctrl = pl010_set_mctrl,
527 .get_mctrl = pl010_get_mctrl,
528 .stop_tx = pl010_stop_tx,
529 .start_tx = pl010_start_tx,
530 .stop_rx = pl010_stop_rx,
531 .enable_ms = pl010_enable_ms,
532 .break_ctl = pl010_break_ctl,
533 .startup = pl010_startup,
534 .shutdown = pl010_shutdown,
535 .set_termios = pl010_set_termios,
536 .type = pl010_type,
537 .release_port = pl010_release_port,
538 .request_port = pl010_request_port,
539 .config_port = pl010_config_port,
540 .verify_port = pl010_verify_port,
541};
542
fbb18a27 543static struct uart_amba_port *amba_ports[UART_NR];
1da177e4
LT
544
545#ifdef CONFIG_SERIAL_AMBA_PL010_CONSOLE
546
d358788f
RK
547static void pl010_console_putchar(struct uart_port *port, int ch)
548{
1b0646a0 549 struct uart_amba_port *uap = (struct uart_amba_port *)port;
98639a67
RK
550 unsigned int status;
551
552 do {
1b0646a0 553 status = readb(uap->port.membase + UART01x_FR);
d358788f 554 barrier();
98639a67 555 } while (!UART_TX_READY(status));
1b0646a0 556 writel(ch, uap->port.membase + UART01x_DR);
d358788f
RK
557}
558
1da177e4
LT
559static void
560pl010_console_write(struct console *co, const char *s, unsigned int count)
561{
1b0646a0 562 struct uart_amba_port *uap = amba_ports[co->index];
1da177e4 563 unsigned int status, old_cr;
1da177e4 564
ed519ded
RK
565 clk_enable(uap->clk);
566
1da177e4
LT
567 /*
568 * First save the CR then disable the interrupts
569 */
1b0646a0
RK
570 old_cr = readb(uap->port.membase + UART010_CR);
571 writel(UART01x_CR_UARTEN, uap->port.membase + UART010_CR);
1da177e4 572
1b0646a0 573 uart_console_write(&uap->port, s, count, pl010_console_putchar);
1da177e4
LT
574
575 /*
576 * Finally, wait for transmitter to become empty
577 * and restore the TCR
578 */
579 do {
1b0646a0 580 status = readb(uap->port.membase + UART01x_FR);
98639a67 581 barrier();
1da177e4 582 } while (status & UART01x_FR_BUSY);
1b0646a0 583 writel(old_cr, uap->port.membase + UART010_CR);
ed519ded
RK
584
585 clk_disable(uap->clk);
1da177e4
LT
586}
587
588static void __init
1b0646a0 589pl010_console_get_options(struct uart_amba_port *uap, int *baud,
1da177e4
LT
590 int *parity, int *bits)
591{
1b0646a0 592 if (readb(uap->port.membase + UART010_CR) & UART01x_CR_UARTEN) {
1da177e4 593 unsigned int lcr_h, quot;
1b0646a0 594 lcr_h = readb(uap->port.membase + UART010_LCRH);
1da177e4
LT
595
596 *parity = 'n';
597 if (lcr_h & UART01x_LCRH_PEN) {
598 if (lcr_h & UART01x_LCRH_EPS)
599 *parity = 'e';
600 else
601 *parity = 'o';
602 }
603
604 if ((lcr_h & 0x60) == UART01x_LCRH_WLEN_7)
605 *bits = 7;
606 else
607 *bits = 8;
608
1b0646a0
RK
609 quot = readb(uap->port.membase + UART010_LCRL) |
610 readb(uap->port.membase + UART010_LCRM) << 8;
611 *baud = uap->port.uartclk / (16 * (quot + 1));
1da177e4
LT
612 }
613}
614
615static int __init pl010_console_setup(struct console *co, char *options)
616{
1b0646a0 617 struct uart_amba_port *uap;
1da177e4
LT
618 int baud = 38400;
619 int bits = 8;
620 int parity = 'n';
621 int flow = 'n';
622
623 /*
624 * Check whether an invalid uart number has been specified, and
625 * if so, search for the first available port that does have
626 * console support.
627 */
628 if (co->index >= UART_NR)
629 co->index = 0;
1b0646a0
RK
630 uap = amba_ports[co->index];
631 if (!uap)
d28122a5 632 return -ENODEV;
1da177e4 633
ed519ded
RK
634 uap->port.uartclk = clk_get_rate(uap->clk);
635
1da177e4
LT
636 if (options)
637 uart_parse_options(options, &baud, &parity, &bits, &flow);
638 else
1b0646a0 639 pl010_console_get_options(uap, &baud, &parity, &bits);
1da177e4 640
1b0646a0 641 return uart_set_options(&uap->port, co, baud, parity, bits, flow);
1da177e4
LT
642}
643
2d93486c 644static struct uart_driver amba_reg;
1da177e4
LT
645static struct console amba_console = {
646 .name = "ttyAM",
647 .write = pl010_console_write,
648 .device = uart_console_device,
649 .setup = pl010_console_setup,
650 .flags = CON_PRINTBUFFER,
651 .index = -1,
652 .data = &amba_reg,
653};
654
1da177e4
LT
655#define AMBA_CONSOLE &amba_console
656#else
657#define AMBA_CONSOLE NULL
658#endif
659
660static struct uart_driver amba_reg = {
661 .owner = THIS_MODULE,
662 .driver_name = "ttyAM",
663 .dev_name = "ttyAM",
664 .major = SERIAL_AMBA_MAJOR,
665 .minor = SERIAL_AMBA_MINOR,
666 .nr = UART_NR,
667 .cons = AMBA_CONSOLE,
668};
669
670static int pl010_probe(struct amba_device *dev, void *id)
671{
1b0646a0 672 struct uart_amba_port *uap;
fbb18a27
RK
673 void __iomem *base;
674 int i, ret;
1da177e4 675
fbb18a27
RK
676 for (i = 0; i < ARRAY_SIZE(amba_ports); i++)
677 if (amba_ports[i] == NULL)
678 break;
1da177e4 679
fbb18a27
RK
680 if (i == ARRAY_SIZE(amba_ports)) {
681 ret = -EBUSY;
682 goto out;
1da177e4
LT
683 }
684
1b0646a0
RK
685 uap = kzalloc(sizeof(struct uart_amba_port), GFP_KERNEL);
686 if (!uap) {
fbb18a27
RK
687 ret = -ENOMEM;
688 goto out;
689 }
690
691 base = ioremap(dev->res.start, PAGE_SIZE);
692 if (!base) {
693 ret = -ENOMEM;
694 goto free;
695 }
696
ed519ded
RK
697 uap->clk = clk_get(&dev->dev, "UARTCLK");
698 if (IS_ERR(uap->clk)) {
699 ret = PTR_ERR(uap->clk);
700 goto unmap;
701 }
702
1b0646a0
RK
703 uap->port.dev = &dev->dev;
704 uap->port.mapbase = dev->res.start;
705 uap->port.membase = base;
706 uap->port.iotype = UPIO_MEM;
707 uap->port.irq = dev->irq[0];
1b0646a0
RK
708 uap->port.fifosize = 16;
709 uap->port.ops = &amba_pl010_pops;
710 uap->port.flags = UPF_BOOT_AUTOCONF;
711 uap->port.line = i;
712 uap->dev = dev;
713 uap->data = dev->dev.platform_data;
714
715 amba_ports[i] = uap;
716
717 amba_set_drvdata(dev, uap);
718 ret = uart_add_one_port(&amba_reg, &uap->port);
fbb18a27
RK
719 if (ret) {
720 amba_set_drvdata(dev, NULL);
721 amba_ports[i] = NULL;
ed519ded
RK
722 clk_put(uap->clk);
723 unmap:
fbb18a27
RK
724 iounmap(base);
725 free:
1b0646a0 726 kfree(uap);
fbb18a27 727 }
fbb18a27
RK
728 out:
729 return ret;
1da177e4
LT
730}
731
732static int pl010_remove(struct amba_device *dev)
733{
1b0646a0 734 struct uart_amba_port *uap = amba_get_drvdata(dev);
fbb18a27 735 int i;
1da177e4
LT
736
737 amba_set_drvdata(dev, NULL);
738
1b0646a0 739 uart_remove_one_port(&amba_reg, &uap->port);
fbb18a27
RK
740
741 for (i = 0; i < ARRAY_SIZE(amba_ports); i++)
1b0646a0 742 if (amba_ports[i] == uap)
fbb18a27
RK
743 amba_ports[i] = NULL;
744
1b0646a0 745 iounmap(uap->port.membase);
ed519ded 746 clk_put(uap->clk);
1b0646a0 747 kfree(uap);
1da177e4
LT
748 return 0;
749}
750
0370affe 751static int pl010_suspend(struct amba_device *dev, pm_message_t state)
1da177e4
LT
752{
753 struct uart_amba_port *uap = amba_get_drvdata(dev);
754
755 if (uap)
756 uart_suspend_port(&amba_reg, &uap->port);
757
758 return 0;
759}
760
761static int pl010_resume(struct amba_device *dev)
762{
763 struct uart_amba_port *uap = amba_get_drvdata(dev);
764
765 if (uap)
766 uart_resume_port(&amba_reg, &uap->port);
767
768 return 0;
769}
770
771static struct amba_id pl010_ids[] __initdata = {
772 {
773 .id = 0x00041010,
774 .mask = 0x000fffff,
775 },
776 { 0, 0 },
777};
778
779static struct amba_driver pl010_driver = {
780 .drv = {
781 .name = "uart-pl010",
782 },
783 .id_table = pl010_ids,
784 .probe = pl010_probe,
785 .remove = pl010_remove,
786 .suspend = pl010_suspend,
787 .resume = pl010_resume,
788};
789
790static int __init pl010_init(void)
791{
792 int ret;
793
794 printk(KERN_INFO "Serial: AMBA driver $Revision: 1.41 $\n");
795
796 ret = uart_register_driver(&amba_reg);
797 if (ret == 0) {
798 ret = amba_driver_register(&pl010_driver);
799 if (ret)
800 uart_unregister_driver(&amba_reg);
801 }
802 return ret;
803}
804
805static void __exit pl010_exit(void)
806{
807 amba_driver_unregister(&pl010_driver);
808 uart_unregister_driver(&amba_reg);
809}
810
811module_init(pl010_init);
812module_exit(pl010_exit);
813
814MODULE_AUTHOR("ARM Ltd/Deep Blue Solutions Ltd");
815MODULE_DESCRIPTION("ARM AMBA serial port driver $Revision: 1.41 $");
816MODULE_LICENSE("GPL");
This page took 0.491786 seconds and 5 git commands to generate.