blob: bf82e28770a9845af5cfef55098605e98bcef728 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 * 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.
31 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
33#if defined(CONFIG_SERIAL_AMBA_PL011_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
34#define SUPPORT_SYSRQ
35#endif
36
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>
Russell Kinga62c80e2006-01-07 13:52:45 +000047#include <linux/amba/bus.h>
48#include <linux/amba/serial.h>
Russell Kingf8ce2542006-01-07 16:15:52 +000049#include <linux/clk.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51#include <asm/io.h>
Russell Kingc6b8fda2005-10-28 14:05:16 +010052#include <asm/sizes.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
54#define UART_NR 14
55
56#define SERIAL_AMBA_MAJOR 204
57#define SERIAL_AMBA_MINOR 64
58#define SERIAL_AMBA_NR UART_NR
59
60#define AMBA_ISR_PASS_LIMIT 256
61
Russell Kingb63d4f02005-11-19 11:10:35 +000062#define UART_DR_ERROR (UART011_DR_OE|UART011_DR_BE|UART011_DR_PE|UART011_DR_FE)
63#define UART_DUMMY_DR_RX (1 << 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
65/*
66 * We wrap our port structure around the generic uart_port.
67 */
68struct uart_amba_port {
69 struct uart_port port;
70 struct clk *clk;
71 unsigned int im; /* interrupt mask */
72 unsigned int old_status;
Alessandro Rubini5926a292009-06-04 17:43:04 +010073 unsigned int ifls; /* vendor-specific */
74};
75
76/* There is by now at least one vendor with differing details, so handle it */
77struct vendor_data {
78 unsigned int ifls;
79 unsigned int fifosize;
80};
81
82static struct vendor_data vendor_arm = {
83 .ifls = UART011_IFLS_RX4_8|UART011_IFLS_TX4_8,
84 .fifosize = 16,
85};
86
87static struct vendor_data vendor_st = {
88 .ifls = UART011_IFLS_RX_HALF|UART011_IFLS_TX_HALF,
89 .fifosize = 64,
Linus Torvalds1da177e2005-04-16 15:20:36 -070090};
91
Russell Kingb129a8c2005-08-31 10:12:14 +010092static void pl011_stop_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -070093{
94 struct uart_amba_port *uap = (struct uart_amba_port *)port;
95
96 uap->im &= ~UART011_TXIM;
97 writew(uap->im, uap->port.membase + UART011_IMSC);
98}
99
Russell Kingb129a8c2005-08-31 10:12:14 +0100100static void pl011_start_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101{
102 struct uart_amba_port *uap = (struct uart_amba_port *)port;
103
104 uap->im |= UART011_TXIM;
105 writew(uap->im, uap->port.membase + UART011_IMSC);
106}
107
108static void pl011_stop_rx(struct uart_port *port)
109{
110 struct uart_amba_port *uap = (struct uart_amba_port *)port;
111
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);
115}
116
117static void pl011_enable_ms(struct uart_port *port)
118{
119 struct uart_amba_port *uap = (struct uart_amba_port *)port;
120
121 uap->im |= UART011_RIMIM|UART011_CTSMIM|UART011_DCDMIM|UART011_DSRMIM;
122 writew(uap->im, uap->port.membase + UART011_IMSC);
123}
124
David Howells7d12e782006-10-05 14:55:46 +0100125static void pl011_rx_chars(struct uart_amba_port *uap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126{
Takashi Iwaia88487c2008-07-16 21:54:42 +0100127 struct tty_struct *tty = uap->port.info->port.tty;
Russell Kingb63d4f02005-11-19 11:10:35 +0000128 unsigned int status, ch, flag, max_count = 256;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
130 status = readw(uap->port.membase + UART01x_FR);
131 while ((status & UART01x_FR_RXFE) == 0 && max_count--) {
Russell Kingb63d4f02005-11-19 11:10:35 +0000132 ch = readw(uap->port.membase + UART01x_DR) | UART_DUMMY_DR_RX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 flag = TTY_NORMAL;
134 uap->port.icount.rx++;
135
136 /*
137 * Note that the error handling code is
138 * out of the main execution path
139 */
Russell Kingb63d4f02005-11-19 11:10:35 +0000140 if (unlikely(ch & UART_DR_ERROR)) {
141 if (ch & UART011_DR_BE) {
142 ch &= ~(UART011_DR_FE | UART011_DR_PE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 uap->port.icount.brk++;
144 if (uart_handle_break(&uap->port))
145 goto ignore_char;
Russell Kingb63d4f02005-11-19 11:10:35 +0000146 } else if (ch & UART011_DR_PE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 uap->port.icount.parity++;
Russell Kingb63d4f02005-11-19 11:10:35 +0000148 else if (ch & UART011_DR_FE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 uap->port.icount.frame++;
Russell Kingb63d4f02005-11-19 11:10:35 +0000150 if (ch & UART011_DR_OE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 uap->port.icount.overrun++;
152
Russell Kingb63d4f02005-11-19 11:10:35 +0000153 ch &= uap->port.read_status_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
Russell Kingb63d4f02005-11-19 11:10:35 +0000155 if (ch & UART011_DR_BE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 flag = TTY_BREAK;
Russell Kingb63d4f02005-11-19 11:10:35 +0000157 else if (ch & UART011_DR_PE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 flag = TTY_PARITY;
Russell Kingb63d4f02005-11-19 11:10:35 +0000159 else if (ch & UART011_DR_FE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 flag = TTY_FRAME;
161 }
162
David Howells7d12e782006-10-05 14:55:46 +0100163 if (uart_handle_sysrq_char(&uap->port, ch & 255))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 goto ignore_char;
165
Russell Kingb63d4f02005-11-19 11:10:35 +0000166 uart_insert_char(&uap->port, ch, UART011_DR_OE, ch, flag);
Russell King05ab3012005-05-09 23:21:59 +0100167
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 ignore_char:
169 status = readw(uap->port.membase + UART01x_FR);
170 }
Thomas Gleixner2389b272007-05-29 21:53:50 +0100171 spin_unlock(&uap->port.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 tty_flip_buffer_push(tty);
Thomas Gleixner2389b272007-05-29 21:53:50 +0100173 spin_lock(&uap->port.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174}
175
176static void pl011_tx_chars(struct uart_amba_port *uap)
177{
178 struct circ_buf *xmit = &uap->port.info->xmit;
179 int count;
180
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;
186 }
187 if (uart_circ_empty(xmit) || uart_tx_stopped(&uap->port)) {
Russell Kingb129a8c2005-08-31 10:12:14 +0100188 pl011_stop_tx(&uap->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 return;
190 }
191
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);
200
201 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
202 uart_write_wakeup(&uap->port);
203
204 if (uart_circ_empty(xmit))
Russell Kingb129a8c2005-08-31 10:12:14 +0100205 pl011_stop_tx(&uap->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206}
207
208static void pl011_modem_status(struct uart_amba_port *uap)
209{
210 unsigned int status, delta;
211
212 status = readw(uap->port.membase + UART01x_FR) & UART01x_FR_MODEM_ANY;
213
214 delta = status ^ uap->old_status;
215 uap->old_status = status;
216
217 if (!delta)
218 return;
219
220 if (delta & UART01x_FR_DCD)
221 uart_handle_dcd_change(&uap->port, status & UART01x_FR_DCD);
222
223 if (delta & UART01x_FR_DSR)
224 uap->port.icount.dsr++;
225
226 if (delta & UART01x_FR_CTS)
227 uart_handle_cts_change(&uap->port, status & UART01x_FR_CTS);
228
229 wake_up_interruptible(&uap->port.info->delta_msr_wait);
230}
231
David Howells7d12e782006-10-05 14:55:46 +0100232static irqreturn_t pl011_int(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233{
234 struct uart_amba_port *uap = dev_id;
235 unsigned int status, pass_counter = AMBA_ISR_PASS_LIMIT;
236 int handled = 0;
237
238 spin_lock(&uap->port.lock);
239
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);
246
247 if (status & (UART011_RTIS|UART011_RXIS))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 pl011_rx_chars(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 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);
254
255 if (pass_counter-- == 0)
256 break;
257
258 status = readw(uap->port.membase + UART011_MIS);
259 } while (status != 0);
260 handled = 1;
261 }
262
263 spin_unlock(&uap->port.lock);
264
265 return IRQ_RETVAL(handled);
266}
267
268static unsigned int pl01x_tx_empty(struct uart_port *port)
269{
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;
273}
274
275static unsigned int pl01x_get_mctrl(struct uart_port *port)
276{
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);
280
Jiri Slaby5159f402007-10-18 23:40:31 -0700281#define TIOCMBIT(uartbit, tiocmbit) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 if (status & uartbit) \
283 result |= tiocmbit
284
Jiri Slaby5159f402007-10-18 23:40:31 -0700285 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 return result;
291}
292
293static void pl011_set_mctrl(struct uart_port *port, unsigned int mctrl)
294{
295 struct uart_amba_port *uap = (struct uart_amba_port *)port;
296 unsigned int cr;
297
298 cr = readw(uap->port.membase + UART011_CR);
299
Jiri Slaby5159f402007-10-18 23:40:31 -0700300#define TIOCMBIT(tiocmbit, uartbit) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 if (mctrl & tiocmbit) \
302 cr |= uartbit; \
303 else \
304 cr &= ~uartbit
305
Jiri Slaby5159f402007-10-18 23:40:31 -0700306 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
313 writew(cr, uap->port.membase + UART011_CR);
314}
315
316static void pl011_break_ctl(struct uart_port *port, int break_state)
317{
318 struct uart_amba_port *uap = (struct uart_amba_port *)port;
319 unsigned long flags;
320 unsigned int lcr_h;
321
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);
330}
331
Jason Wessel84b5ae12008-02-20 13:33:39 -0600332#ifdef CONFIG_CONSOLE_POLL
333static int pl010_get_poll_char(struct uart_port *port)
334{
335 struct uart_amba_port *uap = (struct uart_amba_port *)port;
336 unsigned int status;
337
338 do {
339 status = readw(uap->port.membase + UART01x_FR);
340 } while (status & UART01x_FR_RXFE);
341
342 return readw(uap->port.membase + UART01x_DR);
343}
344
345static void pl010_put_poll_char(struct uart_port *port,
346 unsigned char ch)
347{
348 struct uart_amba_port *uap = (struct uart_amba_port *)port;
349
350 while (readw(uap->port.membase + UART01x_FR) & UART01x_FR_TXFF)
351 barrier();
352
353 writew(ch, uap->port.membase + UART01x_DR);
354}
355
356#endif /* CONFIG_CONSOLE_POLL */
357
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358static int pl011_startup(struct uart_port *port)
359{
360 struct uart_amba_port *uap = (struct uart_amba_port *)port;
361 unsigned int cr;
362 int retval;
363
364 /*
365 * Try to enable the clock producer.
366 */
367 retval = clk_enable(uap->clk);
368 if (retval)
369 goto out;
370
371 uap->port.uartclk = clk_get_rate(uap->clk);
372
373 /*
374 * Allocate the IRQ
375 */
376 retval = request_irq(uap->port.irq, pl011_int, 0, "uart-pl011", uap);
377 if (retval)
378 goto clk_dis;
379
Alessandro Rubini5926a292009-06-04 17:43:04 +0100380 writew(uap->ifls, uap->port.membase + UART011_IFLS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
382 /*
383 * Provoke TX FIFO interrupt into asserting.
384 */
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();
393
394 cr = UART01x_CR_UARTEN | UART011_CR_RXE | UART011_CR_TXE;
395 writew(cr, uap->port.membase + UART011_CR);
396
397 /*
398 * initialise the old status of the modem signals
399 */
400 uap->old_status = readw(uap->port.membase + UART01x_FR) & UART01x_FR_MODEM_ANY;
401
402 /*
403 * Finally, enable interrupts
404 */
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);
409
410 return 0;
411
412 clk_dis:
413 clk_disable(uap->clk);
414 out:
415 return retval;
416}
417
418static void pl011_shutdown(struct uart_port *port)
419{
420 struct uart_amba_port *uap = (struct uart_amba_port *)port;
421 unsigned long val;
422
423 /*
424 * disable all interrupts
425 */
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);
431
432 /*
433 * Free the interrupt
434 */
435 free_irq(uap->port.irq, uap);
436
437 /*
438 * disable the port
439 */
440 writew(UART01x_CR_UARTEN | UART011_CR_TXE, uap->port.membase + UART011_CR);
441
442 /*
443 * disable break condition and fifos
444 */
445 val = readw(uap->port.membase + UART011_LCRH);
446 val &= ~(UART01x_LCRH_BRK | UART01x_LCRH_FEN);
447 writew(val, uap->port.membase + UART011_LCRH);
448
449 /*
450 * Shut down the clock producer
451 */
452 clk_disable(uap->clk);
453}
454
455static void
Alan Cox606d0992006-12-08 02:38:45 -0800456pl011_set_termios(struct uart_port *port, struct ktermios *termios,
457 struct ktermios *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458{
459 unsigned int lcr_h, old_cr;
460 unsigned long flags;
461 unsigned int baud, quot;
462
463 /*
464 * Ask the core to calculate the divisor for us.
465 */
466 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
467 quot = port->uartclk * 4 / baud;
468
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;
482 }
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;
489 }
490 if (port->fifosize > 1)
491 lcr_h |= UART01x_LCRH_FEN;
492
493 spin_lock_irqsave(&port->lock, flags);
494
495 /*
496 * Update the per-port timeout.
497 */
498 uart_update_timeout(port, termios->c_cflag, baud);
499
Russell Kingb63d4f02005-11-19 11:10:35 +0000500 port->read_status_mask = UART011_DR_OE | 255;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 if (termios->c_iflag & INPCK)
Russell Kingb63d4f02005-11-19 11:10:35 +0000502 port->read_status_mask |= UART011_DR_FE | UART011_DR_PE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 if (termios->c_iflag & (BRKINT | PARMRK))
Russell Kingb63d4f02005-11-19 11:10:35 +0000504 port->read_status_mask |= UART011_DR_BE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
506 /*
507 * Characters to ignore
508 */
509 port->ignore_status_mask = 0;
510 if (termios->c_iflag & IGNPAR)
Russell Kingb63d4f02005-11-19 11:10:35 +0000511 port->ignore_status_mask |= UART011_DR_FE | UART011_DR_PE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 if (termios->c_iflag & IGNBRK) {
Russell Kingb63d4f02005-11-19 11:10:35 +0000513 port->ignore_status_mask |= UART011_DR_BE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 /*
515 * If we're ignoring parity and break indicators,
516 * ignore overruns too (for real raw support).
517 */
518 if (termios->c_iflag & IGNPAR)
Russell Kingb63d4f02005-11-19 11:10:35 +0000519 port->ignore_status_mask |= UART011_DR_OE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 }
521
522 /*
523 * Ignore all characters if CREAD is not set.
524 */
525 if ((termios->c_cflag & CREAD) == 0)
Russell Kingb63d4f02005-11-19 11:10:35 +0000526 port->ignore_status_mask |= UART_DUMMY_DR_RX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
528 if (UART_ENABLE_MS(port, termios->c_cflag))
529 pl011_enable_ms(port);
530
531 /* first, disable everything */
532 old_cr = readw(port->membase + UART011_CR);
533 writew(0, port->membase + UART011_CR);
534
535 /* Set baud rate */
536 writew(quot & 0x3f, port->membase + UART011_FBRD);
537 writew(quot >> 6, port->membase + UART011_IBRD);
538
539 /*
540 * ----------v----------v----------v----------v-----
541 * NOTE: MUST BE WRITTEN AFTER UARTLCR_M & UARTLCR_L
542 * ----------^----------^----------^----------^-----
543 */
544 writew(lcr_h, port->membase + UART011_LCRH);
545 writew(old_cr, port->membase + UART011_CR);
546
547 spin_unlock_irqrestore(&port->lock, flags);
548}
549
550static const char *pl011_type(struct uart_port *port)
551{
552 return port->type == PORT_AMBA ? "AMBA/PL011" : NULL;
553}
554
555/*
556 * Release the memory region(s) being used by 'port'
557 */
558static void pl010_release_port(struct uart_port *port)
559{
560 release_mem_region(port->mapbase, SZ_4K);
561}
562
563/*
564 * Request the memory region(s) being used by 'port'
565 */
566static int pl010_request_port(struct uart_port *port)
567{
568 return request_mem_region(port->mapbase, SZ_4K, "uart-pl011")
569 != NULL ? 0 : -EBUSY;
570}
571
572/*
573 * Configure/autoconfigure the port.
574 */
575static void pl010_config_port(struct uart_port *port, int flags)
576{
577 if (flags & UART_CONFIG_TYPE) {
578 port->type = PORT_AMBA;
579 pl010_request_port(port);
580 }
581}
582
583/*
584 * verify the new serial_struct (for TIOCSSERIAL).
585 */
586static int pl010_verify_port(struct uart_port *port, struct serial_struct *ser)
587{
588 int ret = 0;
589 if (ser->type != PORT_UNKNOWN && ser->type != PORT_AMBA)
590 ret = -EINVAL;
Yinghai Lua62c4132008-08-19 20:49:55 -0700591 if (ser->irq < 0 || ser->irq >= nr_irqs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 ret = -EINVAL;
593 if (ser->baud_base < 9600)
594 ret = -EINVAL;
595 return ret;
596}
597
598static 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,
Jason Wessel84b5ae12008-02-20 13:33:39 -0600615#ifdef CONFIG_CONSOLE_POLL
616 .poll_get_char = pl010_get_poll_char,
617 .poll_put_char = pl010_put_poll_char,
618#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619};
620
621static struct uart_amba_port *amba_ports[UART_NR];
622
623#ifdef CONFIG_SERIAL_AMBA_PL011_CONSOLE
624
Russell Kingd3587882006-03-20 20:00:09 +0000625static void pl011_console_putchar(struct uart_port *port, int ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626{
Russell Kingd3587882006-03-20 20:00:09 +0000627 struct uart_amba_port *uap = (struct uart_amba_port *)port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
Russell Kingd3587882006-03-20 20:00:09 +0000629 while (readw(uap->port.membase + UART01x_FR) & UART01x_FR_TXFF)
630 barrier();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 writew(ch, uap->port.membase + UART01x_DR);
632}
633
634static void
635pl011_console_write(struct console *co, const char *s, unsigned int count)
636{
637 struct uart_amba_port *uap = amba_ports[co->index];
638 unsigned int status, old_cr, new_cr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
640 clk_enable(uap->clk);
641
642 /*
643 * First save the CR then disable the interrupts
644 */
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);
649
Russell Kingd3587882006-03-20 20:00:09 +0000650 uart_console_write(&uap->port, s, count, pl011_console_putchar);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
652 /*
653 * Finally, wait for transmitter to become empty
654 * and restore the TCR
655 */
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);
660
661 clk_disable(uap->clk);
662}
663
664static void __init
665pl011_console_get_options(struct uart_amba_port *uap, int *baud,
666 int *parity, int *bits)
667{
668 if (readw(uap->port.membase + UART011_CR) & UART01x_CR_UARTEN) {
669 unsigned int lcr_h, ibrd, fbrd;
670
671 lcr_h = readw(uap->port.membase + UART011_LCRH);
672
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';
679 }
680
681 if ((lcr_h & 0x60) == UART01x_LCRH_WLEN_7)
682 *bits = 7;
683 else
684 *bits = 8;
685
686 ibrd = readw(uap->port.membase + UART011_IBRD);
687 fbrd = readw(uap->port.membase + UART011_FBRD);
688
689 *baud = uap->port.uartclk * 4 / (64 * ibrd + fbrd);
690 }
691}
692
693static int __init pl011_console_setup(struct console *co, char *options)
694{
695 struct uart_amba_port *uap;
696 int baud = 38400;
697 int bits = 8;
698 int parity = 'n';
699 int flow = 'n';
700
701 /*
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.
705 */
706 if (co->index >= UART_NR)
707 co->index = 0;
708 uap = amba_ports[co->index];
Russell Kingd28122a2007-01-22 18:59:42 +0000709 if (!uap)
710 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
712 uap->port.uartclk = clk_get_rate(uap->clk);
713
714 if (options)
715 uart_parse_options(options, &baud, &parity, &bits, &flow);
716 else
717 pl011_console_get_options(uap, &baud, &parity, &bits);
718
719 return uart_set_options(&uap->port, co, baud, parity, bits, flow);
720}
721
Vincent Sanders2d934862005-09-14 22:36:03 +0100722static struct uart_driver amba_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723static 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,
731};
732
733#define AMBA_CONSOLE (&amba_console)
734#else
735#define AMBA_CONSOLE NULL
736#endif
737
738static 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,
746};
747
Alessandro Rubini03fbdb12009-05-20 22:39:08 +0100748static int pl011_probe(struct amba_device *dev, struct amba_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749{
750 struct uart_amba_port *uap;
Alessandro Rubini5926a292009-06-04 17:43:04 +0100751 struct vendor_data *vendor = id->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 void __iomem *base;
753 int i, ret;
754
755 for (i = 0; i < ARRAY_SIZE(amba_ports); i++)
756 if (amba_ports[i] == NULL)
757 break;
758
759 if (i == ARRAY_SIZE(amba_ports)) {
760 ret = -EBUSY;
761 goto out;
762 }
763
Yoann Padioleaudd00cc42007-07-19 01:49:03 -0700764 uap = kzalloc(sizeof(struct uart_amba_port), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 if (uap == NULL) {
766 ret = -ENOMEM;
767 goto out;
768 }
769
Linus Walleijdc890c22009-06-07 23:27:31 +0100770 base = ioremap(dev->res.start, resource_size(&dev->res));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 if (!base) {
772 ret = -ENOMEM;
773 goto free;
774 }
775
Russell Kingee569c42008-11-30 17:38:14 +0000776 uap->clk = clk_get(&dev->dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 if (IS_ERR(uap->clk)) {
778 ret = PTR_ERR(uap->clk);
779 goto unmap;
780 }
781
Alessandro Rubini5926a292009-06-04 17:43:04 +0100782 uap->ifls = vendor->ifls;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 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];
Alessandro Rubini5926a292009-06-04 17:43:04 +0100788 uap->port.fifosize = vendor->fifosize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 uap->port.ops = &amba_pl011_pops;
790 uap->port.flags = UPF_BOOT_AUTOCONF;
791 uap->port.line = i;
792
793 amba_ports[i] = uap;
794
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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 clk_put(uap->clk);
801 unmap:
802 iounmap(base);
803 free:
804 kfree(uap);
805 }
806 out:
807 return ret;
808}
809
810static int pl011_remove(struct amba_device *dev)
811{
812 struct uart_amba_port *uap = amba_get_drvdata(dev);
813 int i;
814
815 amba_set_drvdata(dev, NULL);
816
817 uart_remove_one_port(&amba_reg, &uap->port);
818
819 for (i = 0; i < ARRAY_SIZE(amba_ports); i++)
820 if (amba_ports[i] == uap)
821 amba_ports[i] = NULL;
822
823 iounmap(uap->port.membase);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 clk_put(uap->clk);
825 kfree(uap);
826 return 0;
827}
828
829static struct amba_id pl011_ids[] __initdata = {
830 {
831 .id = 0x00041011,
832 .mask = 0x000fffff,
Alessandro Rubini5926a292009-06-04 17:43:04 +0100833 .data = &vendor_arm,
834 },
835 {
836 .id = 0x00380802,
837 .mask = 0x00ffffff,
838 .data = &vendor_st,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 },
840 { 0, 0 },
841};
842
843static struct amba_driver pl011_driver = {
844 .drv = {
845 .name = "uart-pl011",
846 },
847 .id_table = pl011_ids,
848 .probe = pl011_probe,
849 .remove = pl011_remove,
850};
851
852static int __init pl011_init(void)
853{
854 int ret;
855 printk(KERN_INFO "Serial: AMBA PL011 UART driver\n");
856
857 ret = uart_register_driver(&amba_reg);
858 if (ret == 0) {
859 ret = amba_driver_register(&pl011_driver);
860 if (ret)
861 uart_unregister_driver(&amba_reg);
862 }
863 return ret;
864}
865
866static void __exit pl011_exit(void)
867{
868 amba_driver_unregister(&pl011_driver);
869 uart_unregister_driver(&amba_reg);
870}
871
Alessandro Rubini4dd9e742009-05-05 05:54:13 +0100872/*
873 * While this can be a module, if builtin it's most likely the console
874 * So let's leave module_exit but move module_init to an earlier place
875 */
876arch_initcall(pl011_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877module_exit(pl011_exit);
878
879MODULE_AUTHOR("ARM Ltd/Deep Blue Solutions Ltd");
880MODULE_DESCRIPTION("ARM AMBA serial port driver");
881MODULE_LICENSE("GPL");