blob: c77b3eb5142d2952f717c880622200e42ddbde00 [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090050#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
52#include <asm/io.h>
Russell Kingc6b8fda2005-10-28 14:05:16 +010053#include <asm/sizes.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55#define UART_NR 14
56
57#define SERIAL_AMBA_MAJOR 204
58#define SERIAL_AMBA_MINOR 64
59#define SERIAL_AMBA_NR UART_NR
60
61#define AMBA_ISR_PASS_LIMIT 256
62
Russell Kingb63d4f02005-11-19 11:10:35 +000063#define UART_DR_ERROR (UART011_DR_OE|UART011_DR_BE|UART011_DR_PE|UART011_DR_FE)
64#define UART_DUMMY_DR_RX (1 << 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
66/*
67 * We wrap our port structure around the generic uart_port.
68 */
69struct uart_amba_port {
70 struct uart_port port;
71 struct clk *clk;
Linus Walleijec489aa2010-06-02 08:13:52 +010072 unsigned int im; /* interrupt mask */
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 unsigned int old_status;
Linus Walleijec489aa2010-06-02 08:13:52 +010074 unsigned int ifls; /* vendor-specific */
75 unsigned int lcrh_tx; /* vendor-specific */
76 unsigned int lcrh_rx; /* vendor-specific */
Linus Walleijac3e3fb2010-06-02 20:40:22 +010077 bool oversampling; /* vendor-specific */
Rabin Vincent3b438162010-02-12 06:43:11 +010078 bool autorts;
Russell Kinge8a7ba82010-12-28 09:16:54 +000079 char type[12];
Alessandro Rubini5926a292009-06-04 17:43:04 +010080};
81
82/* There is by now at least one vendor with differing details, so handle it */
83struct vendor_data {
84 unsigned int ifls;
85 unsigned int fifosize;
Linus Walleijec489aa2010-06-02 08:13:52 +010086 unsigned int lcrh_tx;
87 unsigned int lcrh_rx;
Linus Walleijac3e3fb2010-06-02 20:40:22 +010088 bool oversampling;
Alessandro Rubini5926a292009-06-04 17:43:04 +010089};
90
91static struct vendor_data vendor_arm = {
92 .ifls = UART011_IFLS_RX4_8|UART011_IFLS_TX4_8,
93 .fifosize = 16,
Linus Walleijec489aa2010-06-02 08:13:52 +010094 .lcrh_tx = UART011_LCRH,
95 .lcrh_rx = UART011_LCRH,
Linus Walleijac3e3fb2010-06-02 20:40:22 +010096 .oversampling = false,
Alessandro Rubini5926a292009-06-04 17:43:04 +010097};
98
99static struct vendor_data vendor_st = {
100 .ifls = UART011_IFLS_RX_HALF|UART011_IFLS_TX_HALF,
101 .fifosize = 64,
Linus Walleijec489aa2010-06-02 08:13:52 +0100102 .lcrh_tx = ST_UART011_LCRH_TX,
103 .lcrh_rx = ST_UART011_LCRH_RX,
Linus Walleijac3e3fb2010-06-02 20:40:22 +0100104 .oversampling = true,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105};
106
Russell Kingb129a8c2005-08-31 10:12:14 +0100107static void pl011_stop_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108{
109 struct uart_amba_port *uap = (struct uart_amba_port *)port;
110
111 uap->im &= ~UART011_TXIM;
112 writew(uap->im, uap->port.membase + UART011_IMSC);
113}
114
Russell Kingb129a8c2005-08-31 10:12:14 +0100115static void pl011_start_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116{
117 struct uart_amba_port *uap = (struct uart_amba_port *)port;
118
119 uap->im |= UART011_TXIM;
120 writew(uap->im, uap->port.membase + UART011_IMSC);
121}
122
123static void pl011_stop_rx(struct uart_port *port)
124{
125 struct uart_amba_port *uap = (struct uart_amba_port *)port;
126
127 uap->im &= ~(UART011_RXIM|UART011_RTIM|UART011_FEIM|
128 UART011_PEIM|UART011_BEIM|UART011_OEIM);
129 writew(uap->im, uap->port.membase + UART011_IMSC);
130}
131
132static void pl011_enable_ms(struct uart_port *port)
133{
134 struct uart_amba_port *uap = (struct uart_amba_port *)port;
135
136 uap->im |= UART011_RIMIM|UART011_CTSMIM|UART011_DCDMIM|UART011_DSRMIM;
137 writew(uap->im, uap->port.membase + UART011_IMSC);
138}
139
David Howells7d12e782006-10-05 14:55:46 +0100140static void pl011_rx_chars(struct uart_amba_port *uap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141{
Alan Coxebd2c8f2009-09-19 13:13:28 -0700142 struct tty_struct *tty = uap->port.state->port.tty;
Russell Kingb63d4f02005-11-19 11:10:35 +0000143 unsigned int status, ch, flag, max_count = 256;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
145 status = readw(uap->port.membase + UART01x_FR);
146 while ((status & UART01x_FR_RXFE) == 0 && max_count--) {
Russell Kingb63d4f02005-11-19 11:10:35 +0000147 ch = readw(uap->port.membase + UART01x_DR) | UART_DUMMY_DR_RX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 flag = TTY_NORMAL;
149 uap->port.icount.rx++;
150
151 /*
152 * Note that the error handling code is
153 * out of the main execution path
154 */
Russell Kingb63d4f02005-11-19 11:10:35 +0000155 if (unlikely(ch & UART_DR_ERROR)) {
156 if (ch & UART011_DR_BE) {
157 ch &= ~(UART011_DR_FE | UART011_DR_PE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 uap->port.icount.brk++;
159 if (uart_handle_break(&uap->port))
160 goto ignore_char;
Russell Kingb63d4f02005-11-19 11:10:35 +0000161 } else if (ch & UART011_DR_PE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 uap->port.icount.parity++;
Russell Kingb63d4f02005-11-19 11:10:35 +0000163 else if (ch & UART011_DR_FE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 uap->port.icount.frame++;
Russell Kingb63d4f02005-11-19 11:10:35 +0000165 if (ch & UART011_DR_OE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 uap->port.icount.overrun++;
167
Russell Kingb63d4f02005-11-19 11:10:35 +0000168 ch &= uap->port.read_status_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
Russell Kingb63d4f02005-11-19 11:10:35 +0000170 if (ch & UART011_DR_BE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 flag = TTY_BREAK;
Russell Kingb63d4f02005-11-19 11:10:35 +0000172 else if (ch & UART011_DR_PE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 flag = TTY_PARITY;
Russell Kingb63d4f02005-11-19 11:10:35 +0000174 else if (ch & UART011_DR_FE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 flag = TTY_FRAME;
176 }
177
David Howells7d12e782006-10-05 14:55:46 +0100178 if (uart_handle_sysrq_char(&uap->port, ch & 255))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 goto ignore_char;
180
Russell Kingb63d4f02005-11-19 11:10:35 +0000181 uart_insert_char(&uap->port, ch, UART011_DR_OE, ch, flag);
Russell King05ab3012005-05-09 23:21:59 +0100182
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 ignore_char:
184 status = readw(uap->port.membase + UART01x_FR);
185 }
Thomas Gleixner2389b272007-05-29 21:53:50 +0100186 spin_unlock(&uap->port.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 tty_flip_buffer_push(tty);
Thomas Gleixner2389b272007-05-29 21:53:50 +0100188 spin_lock(&uap->port.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189}
190
191static void pl011_tx_chars(struct uart_amba_port *uap)
192{
Alan Coxebd2c8f2009-09-19 13:13:28 -0700193 struct circ_buf *xmit = &uap->port.state->xmit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 int count;
195
196 if (uap->port.x_char) {
197 writew(uap->port.x_char, uap->port.membase + UART01x_DR);
198 uap->port.icount.tx++;
199 uap->port.x_char = 0;
200 return;
201 }
202 if (uart_circ_empty(xmit) || uart_tx_stopped(&uap->port)) {
Russell Kingb129a8c2005-08-31 10:12:14 +0100203 pl011_stop_tx(&uap->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 return;
205 }
206
207 count = uap->port.fifosize >> 1;
208 do {
209 writew(xmit->buf[xmit->tail], uap->port.membase + UART01x_DR);
210 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
211 uap->port.icount.tx++;
212 if (uart_circ_empty(xmit))
213 break;
214 } while (--count > 0);
215
216 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
217 uart_write_wakeup(&uap->port);
218
219 if (uart_circ_empty(xmit))
Russell Kingb129a8c2005-08-31 10:12:14 +0100220 pl011_stop_tx(&uap->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221}
222
223static void pl011_modem_status(struct uart_amba_port *uap)
224{
225 unsigned int status, delta;
226
227 status = readw(uap->port.membase + UART01x_FR) & UART01x_FR_MODEM_ANY;
228
229 delta = status ^ uap->old_status;
230 uap->old_status = status;
231
232 if (!delta)
233 return;
234
235 if (delta & UART01x_FR_DCD)
236 uart_handle_dcd_change(&uap->port, status & UART01x_FR_DCD);
237
238 if (delta & UART01x_FR_DSR)
239 uap->port.icount.dsr++;
240
241 if (delta & UART01x_FR_CTS)
242 uart_handle_cts_change(&uap->port, status & UART01x_FR_CTS);
243
Alan Coxbdc04e32009-09-19 13:13:31 -0700244 wake_up_interruptible(&uap->port.state->port.delta_msr_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245}
246
David Howells7d12e782006-10-05 14:55:46 +0100247static irqreturn_t pl011_int(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248{
249 struct uart_amba_port *uap = dev_id;
250 unsigned int status, pass_counter = AMBA_ISR_PASS_LIMIT;
251 int handled = 0;
252
253 spin_lock(&uap->port.lock);
254
255 status = readw(uap->port.membase + UART011_MIS);
256 if (status) {
257 do {
258 writew(status & ~(UART011_TXIS|UART011_RTIS|
259 UART011_RXIS),
260 uap->port.membase + UART011_ICR);
261
262 if (status & (UART011_RTIS|UART011_RXIS))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 pl011_rx_chars(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 if (status & (UART011_DSRMIS|UART011_DCDMIS|
265 UART011_CTSMIS|UART011_RIMIS))
266 pl011_modem_status(uap);
267 if (status & UART011_TXIS)
268 pl011_tx_chars(uap);
269
270 if (pass_counter-- == 0)
271 break;
272
273 status = readw(uap->port.membase + UART011_MIS);
274 } while (status != 0);
275 handled = 1;
276 }
277
278 spin_unlock(&uap->port.lock);
279
280 return IRQ_RETVAL(handled);
281}
282
283static unsigned int pl01x_tx_empty(struct uart_port *port)
284{
285 struct uart_amba_port *uap = (struct uart_amba_port *)port;
286 unsigned int status = readw(uap->port.membase + UART01x_FR);
287 return status & (UART01x_FR_BUSY|UART01x_FR_TXFF) ? 0 : TIOCSER_TEMT;
288}
289
290static unsigned int pl01x_get_mctrl(struct uart_port *port)
291{
292 struct uart_amba_port *uap = (struct uart_amba_port *)port;
293 unsigned int result = 0;
294 unsigned int status = readw(uap->port.membase + UART01x_FR);
295
Jiri Slaby5159f402007-10-18 23:40:31 -0700296#define TIOCMBIT(uartbit, tiocmbit) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 if (status & uartbit) \
298 result |= tiocmbit
299
Jiri Slaby5159f402007-10-18 23:40:31 -0700300 TIOCMBIT(UART01x_FR_DCD, TIOCM_CAR);
301 TIOCMBIT(UART01x_FR_DSR, TIOCM_DSR);
302 TIOCMBIT(UART01x_FR_CTS, TIOCM_CTS);
303 TIOCMBIT(UART011_FR_RI, TIOCM_RNG);
304#undef TIOCMBIT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 return result;
306}
307
308static void pl011_set_mctrl(struct uart_port *port, unsigned int mctrl)
309{
310 struct uart_amba_port *uap = (struct uart_amba_port *)port;
311 unsigned int cr;
312
313 cr = readw(uap->port.membase + UART011_CR);
314
Jiri Slaby5159f402007-10-18 23:40:31 -0700315#define TIOCMBIT(tiocmbit, uartbit) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 if (mctrl & tiocmbit) \
317 cr |= uartbit; \
318 else \
319 cr &= ~uartbit
320
Jiri Slaby5159f402007-10-18 23:40:31 -0700321 TIOCMBIT(TIOCM_RTS, UART011_CR_RTS);
322 TIOCMBIT(TIOCM_DTR, UART011_CR_DTR);
323 TIOCMBIT(TIOCM_OUT1, UART011_CR_OUT1);
324 TIOCMBIT(TIOCM_OUT2, UART011_CR_OUT2);
325 TIOCMBIT(TIOCM_LOOP, UART011_CR_LBE);
Rabin Vincent3b438162010-02-12 06:43:11 +0100326
327 if (uap->autorts) {
328 /* We need to disable auto-RTS if we want to turn RTS off */
329 TIOCMBIT(TIOCM_RTS, UART011_CR_RTSEN);
330 }
Jiri Slaby5159f402007-10-18 23:40:31 -0700331#undef TIOCMBIT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
333 writew(cr, uap->port.membase + UART011_CR);
334}
335
336static void pl011_break_ctl(struct uart_port *port, int break_state)
337{
338 struct uart_amba_port *uap = (struct uart_amba_port *)port;
339 unsigned long flags;
340 unsigned int lcr_h;
341
342 spin_lock_irqsave(&uap->port.lock, flags);
Linus Walleijec489aa2010-06-02 08:13:52 +0100343 lcr_h = readw(uap->port.membase + uap->lcrh_tx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 if (break_state == -1)
345 lcr_h |= UART01x_LCRH_BRK;
346 else
347 lcr_h &= ~UART01x_LCRH_BRK;
Linus Walleijec489aa2010-06-02 08:13:52 +0100348 writew(lcr_h, uap->port.membase + uap->lcrh_tx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 spin_unlock_irqrestore(&uap->port.lock, flags);
350}
351
Jason Wessel84b5ae12008-02-20 13:33:39 -0600352#ifdef CONFIG_CONSOLE_POLL
353static int pl010_get_poll_char(struct uart_port *port)
354{
355 struct uart_amba_port *uap = (struct uart_amba_port *)port;
356 unsigned int status;
357
Jason Wesself5316b42010-05-20 21:04:22 -0500358 status = readw(uap->port.membase + UART01x_FR);
359 if (status & UART01x_FR_RXFE)
360 return NO_POLL_CHAR;
Jason Wessel84b5ae12008-02-20 13:33:39 -0600361
362 return readw(uap->port.membase + UART01x_DR);
363}
364
365static void pl010_put_poll_char(struct uart_port *port,
366 unsigned char ch)
367{
368 struct uart_amba_port *uap = (struct uart_amba_port *)port;
369
370 while (readw(uap->port.membase + UART01x_FR) & UART01x_FR_TXFF)
371 barrier();
372
373 writew(ch, uap->port.membase + UART01x_DR);
374}
375
376#endif /* CONFIG_CONSOLE_POLL */
377
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378static int pl011_startup(struct uart_port *port)
379{
380 struct uart_amba_port *uap = (struct uart_amba_port *)port;
381 unsigned int cr;
382 int retval;
383
384 /*
385 * Try to enable the clock producer.
386 */
387 retval = clk_enable(uap->clk);
388 if (retval)
389 goto out;
390
391 uap->port.uartclk = clk_get_rate(uap->clk);
392
393 /*
394 * Allocate the IRQ
395 */
396 retval = request_irq(uap->port.irq, pl011_int, 0, "uart-pl011", uap);
397 if (retval)
398 goto clk_dis;
399
Alessandro Rubini5926a292009-06-04 17:43:04 +0100400 writew(uap->ifls, uap->port.membase + UART011_IFLS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
402 /*
403 * Provoke TX FIFO interrupt into asserting.
404 */
405 cr = UART01x_CR_UARTEN | UART011_CR_TXE | UART011_CR_LBE;
406 writew(cr, uap->port.membase + UART011_CR);
407 writew(0, uap->port.membase + UART011_FBRD);
408 writew(1, uap->port.membase + UART011_IBRD);
Linus Walleijec489aa2010-06-02 08:13:52 +0100409 writew(0, uap->port.membase + uap->lcrh_rx);
410 if (uap->lcrh_tx != uap->lcrh_rx) {
411 int i;
412 /*
413 * Wait 10 PCLKs before writing LCRH_TX register,
414 * to get this delay write read only register 10 times
415 */
416 for (i = 0; i < 10; ++i)
417 writew(0xff, uap->port.membase + UART011_MIS);
418 writew(0, uap->port.membase + uap->lcrh_tx);
419 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 writew(0, uap->port.membase + UART01x_DR);
421 while (readw(uap->port.membase + UART01x_FR) & UART01x_FR_BUSY)
422 barrier();
423
424 cr = UART01x_CR_UARTEN | UART011_CR_RXE | UART011_CR_TXE;
425 writew(cr, uap->port.membase + UART011_CR);
426
Russell King5063e2c2010-12-22 17:09:08 +0000427 /* Clear pending error interrupts */
428 writew(UART011_OEIS | UART011_BEIS | UART011_PEIS | UART011_FEIS,
429 uap->port.membase + UART011_ICR);
430
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 /*
432 * initialise the old status of the modem signals
433 */
434 uap->old_status = readw(uap->port.membase + UART01x_FR) & UART01x_FR_MODEM_ANY;
435
436 /*
437 * Finally, enable interrupts
438 */
439 spin_lock_irq(&uap->port.lock);
440 uap->im = UART011_RXIM | UART011_RTIM;
441 writew(uap->im, uap->port.membase + UART011_IMSC);
442 spin_unlock_irq(&uap->port.lock);
443
444 return 0;
445
446 clk_dis:
447 clk_disable(uap->clk);
448 out:
449 return retval;
450}
451
Linus Walleijec489aa2010-06-02 08:13:52 +0100452static void pl011_shutdown_channel(struct uart_amba_port *uap,
453 unsigned int lcrh)
454{
455 unsigned long val;
456
457 val = readw(uap->port.membase + lcrh);
458 val &= ~(UART01x_LCRH_BRK | UART01x_LCRH_FEN);
459 writew(val, uap->port.membase + lcrh);
460}
461
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462static void pl011_shutdown(struct uart_port *port)
463{
464 struct uart_amba_port *uap = (struct uart_amba_port *)port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
466 /*
467 * disable all interrupts
468 */
469 spin_lock_irq(&uap->port.lock);
470 uap->im = 0;
471 writew(uap->im, uap->port.membase + UART011_IMSC);
472 writew(0xffff, uap->port.membase + UART011_ICR);
473 spin_unlock_irq(&uap->port.lock);
474
475 /*
476 * Free the interrupt
477 */
478 free_irq(uap->port.irq, uap);
479
480 /*
481 * disable the port
482 */
Rabin Vincent3b438162010-02-12 06:43:11 +0100483 uap->autorts = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 writew(UART01x_CR_UARTEN | UART011_CR_TXE, uap->port.membase + UART011_CR);
485
486 /*
487 * disable break condition and fifos
488 */
Linus Walleijec489aa2010-06-02 08:13:52 +0100489 pl011_shutdown_channel(uap, uap->lcrh_rx);
490 if (uap->lcrh_rx != uap->lcrh_tx)
491 pl011_shutdown_channel(uap, uap->lcrh_tx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
493 /*
494 * Shut down the clock producer
495 */
496 clk_disable(uap->clk);
497}
498
499static void
Alan Cox606d0992006-12-08 02:38:45 -0800500pl011_set_termios(struct uart_port *port, struct ktermios *termios,
501 struct ktermios *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502{
Rabin Vincent3b438162010-02-12 06:43:11 +0100503 struct uart_amba_port *uap = (struct uart_amba_port *)port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 unsigned int lcr_h, old_cr;
505 unsigned long flags;
506 unsigned int baud, quot;
507
508 /*
509 * Ask the core to calculate the divisor for us.
510 */
Linus Walleijac3e3fb2010-06-02 20:40:22 +0100511 baud = uart_get_baud_rate(port, termios, old, 0,
512 port->uartclk/(uap->oversampling ? 8 : 16));
513
514 if (baud > port->uartclk/16)
515 quot = DIV_ROUND_CLOSEST(port->uartclk * 8, baud);
516 else
517 quot = DIV_ROUND_CLOSEST(port->uartclk * 4, baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
519 switch (termios->c_cflag & CSIZE) {
520 case CS5:
521 lcr_h = UART01x_LCRH_WLEN_5;
522 break;
523 case CS6:
524 lcr_h = UART01x_LCRH_WLEN_6;
525 break;
526 case CS7:
527 lcr_h = UART01x_LCRH_WLEN_7;
528 break;
529 default: // CS8
530 lcr_h = UART01x_LCRH_WLEN_8;
531 break;
532 }
533 if (termios->c_cflag & CSTOPB)
534 lcr_h |= UART01x_LCRH_STP2;
535 if (termios->c_cflag & PARENB) {
536 lcr_h |= UART01x_LCRH_PEN;
537 if (!(termios->c_cflag & PARODD))
538 lcr_h |= UART01x_LCRH_EPS;
539 }
540 if (port->fifosize > 1)
541 lcr_h |= UART01x_LCRH_FEN;
542
543 spin_lock_irqsave(&port->lock, flags);
544
545 /*
546 * Update the per-port timeout.
547 */
548 uart_update_timeout(port, termios->c_cflag, baud);
549
Russell Kingb63d4f02005-11-19 11:10:35 +0000550 port->read_status_mask = UART011_DR_OE | 255;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 if (termios->c_iflag & INPCK)
Russell Kingb63d4f02005-11-19 11:10:35 +0000552 port->read_status_mask |= UART011_DR_FE | UART011_DR_PE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 if (termios->c_iflag & (BRKINT | PARMRK))
Russell Kingb63d4f02005-11-19 11:10:35 +0000554 port->read_status_mask |= UART011_DR_BE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
556 /*
557 * Characters to ignore
558 */
559 port->ignore_status_mask = 0;
560 if (termios->c_iflag & IGNPAR)
Russell Kingb63d4f02005-11-19 11:10:35 +0000561 port->ignore_status_mask |= UART011_DR_FE | UART011_DR_PE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 if (termios->c_iflag & IGNBRK) {
Russell Kingb63d4f02005-11-19 11:10:35 +0000563 port->ignore_status_mask |= UART011_DR_BE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 /*
565 * If we're ignoring parity and break indicators,
566 * ignore overruns too (for real raw support).
567 */
568 if (termios->c_iflag & IGNPAR)
Russell Kingb63d4f02005-11-19 11:10:35 +0000569 port->ignore_status_mask |= UART011_DR_OE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 }
571
572 /*
573 * Ignore all characters if CREAD is not set.
574 */
575 if ((termios->c_cflag & CREAD) == 0)
Russell Kingb63d4f02005-11-19 11:10:35 +0000576 port->ignore_status_mask |= UART_DUMMY_DR_RX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
578 if (UART_ENABLE_MS(port, termios->c_cflag))
579 pl011_enable_ms(port);
580
581 /* first, disable everything */
582 old_cr = readw(port->membase + UART011_CR);
583 writew(0, port->membase + UART011_CR);
584
Rabin Vincent3b438162010-02-12 06:43:11 +0100585 if (termios->c_cflag & CRTSCTS) {
586 if (old_cr & UART011_CR_RTS)
587 old_cr |= UART011_CR_RTSEN;
588
589 old_cr |= UART011_CR_CTSEN;
590 uap->autorts = true;
591 } else {
592 old_cr &= ~(UART011_CR_CTSEN | UART011_CR_RTSEN);
593 uap->autorts = false;
594 }
595
Linus Walleijac3e3fb2010-06-02 20:40:22 +0100596 if (uap->oversampling) {
597 if (baud > port->uartclk/16)
598 old_cr |= ST_UART011_CR_OVSFACT;
599 else
600 old_cr &= ~ST_UART011_CR_OVSFACT;
601 }
602
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 /* Set baud rate */
604 writew(quot & 0x3f, port->membase + UART011_FBRD);
605 writew(quot >> 6, port->membase + UART011_IBRD);
606
607 /*
608 * ----------v----------v----------v----------v-----
609 * NOTE: MUST BE WRITTEN AFTER UARTLCR_M & UARTLCR_L
610 * ----------^----------^----------^----------^-----
611 */
Linus Walleijec489aa2010-06-02 08:13:52 +0100612 writew(lcr_h, port->membase + uap->lcrh_rx);
613 if (uap->lcrh_rx != uap->lcrh_tx) {
614 int i;
615 /*
616 * Wait 10 PCLKs before writing LCRH_TX register,
617 * to get this delay write read only register 10 times
618 */
619 for (i = 0; i < 10; ++i)
620 writew(0xff, uap->port.membase + UART011_MIS);
621 writew(lcr_h, port->membase + uap->lcrh_tx);
622 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 writew(old_cr, port->membase + UART011_CR);
624
625 spin_unlock_irqrestore(&port->lock, flags);
626}
627
628static const char *pl011_type(struct uart_port *port)
629{
Russell Kinge8a7ba82010-12-28 09:16:54 +0000630 struct uart_amba_port *uap = (struct uart_amba_port *)port;
631 return uap->port.type == PORT_AMBA ? uap->type : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632}
633
634/*
635 * Release the memory region(s) being used by 'port'
636 */
637static void pl010_release_port(struct uart_port *port)
638{
639 release_mem_region(port->mapbase, SZ_4K);
640}
641
642/*
643 * Request the memory region(s) being used by 'port'
644 */
645static int pl010_request_port(struct uart_port *port)
646{
647 return request_mem_region(port->mapbase, SZ_4K, "uart-pl011")
648 != NULL ? 0 : -EBUSY;
649}
650
651/*
652 * Configure/autoconfigure the port.
653 */
654static void pl010_config_port(struct uart_port *port, int flags)
655{
656 if (flags & UART_CONFIG_TYPE) {
657 port->type = PORT_AMBA;
658 pl010_request_port(port);
659 }
660}
661
662/*
663 * verify the new serial_struct (for TIOCSSERIAL).
664 */
665static int pl010_verify_port(struct uart_port *port, struct serial_struct *ser)
666{
667 int ret = 0;
668 if (ser->type != PORT_UNKNOWN && ser->type != PORT_AMBA)
669 ret = -EINVAL;
Yinghai Lua62c4132008-08-19 20:49:55 -0700670 if (ser->irq < 0 || ser->irq >= nr_irqs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 ret = -EINVAL;
672 if (ser->baud_base < 9600)
673 ret = -EINVAL;
674 return ret;
675}
676
677static struct uart_ops amba_pl011_pops = {
678 .tx_empty = pl01x_tx_empty,
679 .set_mctrl = pl011_set_mctrl,
680 .get_mctrl = pl01x_get_mctrl,
681 .stop_tx = pl011_stop_tx,
682 .start_tx = pl011_start_tx,
683 .stop_rx = pl011_stop_rx,
684 .enable_ms = pl011_enable_ms,
685 .break_ctl = pl011_break_ctl,
686 .startup = pl011_startup,
687 .shutdown = pl011_shutdown,
688 .set_termios = pl011_set_termios,
689 .type = pl011_type,
690 .release_port = pl010_release_port,
691 .request_port = pl010_request_port,
692 .config_port = pl010_config_port,
693 .verify_port = pl010_verify_port,
Jason Wessel84b5ae12008-02-20 13:33:39 -0600694#ifdef CONFIG_CONSOLE_POLL
695 .poll_get_char = pl010_get_poll_char,
696 .poll_put_char = pl010_put_poll_char,
697#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698};
699
700static struct uart_amba_port *amba_ports[UART_NR];
701
702#ifdef CONFIG_SERIAL_AMBA_PL011_CONSOLE
703
Russell Kingd3587882006-03-20 20:00:09 +0000704static void pl011_console_putchar(struct uart_port *port, int ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705{
Russell Kingd3587882006-03-20 20:00:09 +0000706 struct uart_amba_port *uap = (struct uart_amba_port *)port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
Russell Kingd3587882006-03-20 20:00:09 +0000708 while (readw(uap->port.membase + UART01x_FR) & UART01x_FR_TXFF)
709 barrier();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 writew(ch, uap->port.membase + UART01x_DR);
711}
712
713static void
714pl011_console_write(struct console *co, const char *s, unsigned int count)
715{
716 struct uart_amba_port *uap = amba_ports[co->index];
717 unsigned int status, old_cr, new_cr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
719 clk_enable(uap->clk);
720
721 /*
722 * First save the CR then disable the interrupts
723 */
724 old_cr = readw(uap->port.membase + UART011_CR);
725 new_cr = old_cr & ~UART011_CR_CTSEN;
726 new_cr |= UART01x_CR_UARTEN | UART011_CR_TXE;
727 writew(new_cr, uap->port.membase + UART011_CR);
728
Russell Kingd3587882006-03-20 20:00:09 +0000729 uart_console_write(&uap->port, s, count, pl011_console_putchar);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
731 /*
732 * Finally, wait for transmitter to become empty
733 * and restore the TCR
734 */
735 do {
736 status = readw(uap->port.membase + UART01x_FR);
737 } while (status & UART01x_FR_BUSY);
738 writew(old_cr, uap->port.membase + UART011_CR);
739
740 clk_disable(uap->clk);
741}
742
743static void __init
744pl011_console_get_options(struct uart_amba_port *uap, int *baud,
745 int *parity, int *bits)
746{
747 if (readw(uap->port.membase + UART011_CR) & UART01x_CR_UARTEN) {
748 unsigned int lcr_h, ibrd, fbrd;
749
Linus Walleijec489aa2010-06-02 08:13:52 +0100750 lcr_h = readw(uap->port.membase + uap->lcrh_tx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751
752 *parity = 'n';
753 if (lcr_h & UART01x_LCRH_PEN) {
754 if (lcr_h & UART01x_LCRH_EPS)
755 *parity = 'e';
756 else
757 *parity = 'o';
758 }
759
760 if ((lcr_h & 0x60) == UART01x_LCRH_WLEN_7)
761 *bits = 7;
762 else
763 *bits = 8;
764
765 ibrd = readw(uap->port.membase + UART011_IBRD);
766 fbrd = readw(uap->port.membase + UART011_FBRD);
767
768 *baud = uap->port.uartclk * 4 / (64 * ibrd + fbrd);
Linus Walleijac3e3fb2010-06-02 20:40:22 +0100769
770 if (uap->oversampling) {
771 if (readw(uap->port.membase + UART011_CR)
772 & ST_UART011_CR_OVSFACT)
773 *baud *= 2;
774 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 }
776}
777
778static int __init pl011_console_setup(struct console *co, char *options)
779{
780 struct uart_amba_port *uap;
781 int baud = 38400;
782 int bits = 8;
783 int parity = 'n';
784 int flow = 'n';
785
786 /*
787 * Check whether an invalid uart number has been specified, and
788 * if so, search for the first available port that does have
789 * console support.
790 */
791 if (co->index >= UART_NR)
792 co->index = 0;
793 uap = amba_ports[co->index];
Russell Kingd28122a2007-01-22 18:59:42 +0000794 if (!uap)
795 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
797 uap->port.uartclk = clk_get_rate(uap->clk);
798
799 if (options)
800 uart_parse_options(options, &baud, &parity, &bits, &flow);
801 else
802 pl011_console_get_options(uap, &baud, &parity, &bits);
803
804 return uart_set_options(&uap->port, co, baud, parity, bits, flow);
805}
806
Vincent Sanders2d934862005-09-14 22:36:03 +0100807static struct uart_driver amba_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808static struct console amba_console = {
809 .name = "ttyAMA",
810 .write = pl011_console_write,
811 .device = uart_console_device,
812 .setup = pl011_console_setup,
813 .flags = CON_PRINTBUFFER,
814 .index = -1,
815 .data = &amba_reg,
816};
817
818#define AMBA_CONSOLE (&amba_console)
819#else
820#define AMBA_CONSOLE NULL
821#endif
822
823static struct uart_driver amba_reg = {
824 .owner = THIS_MODULE,
825 .driver_name = "ttyAMA",
826 .dev_name = "ttyAMA",
827 .major = SERIAL_AMBA_MAJOR,
828 .minor = SERIAL_AMBA_MINOR,
829 .nr = UART_NR,
830 .cons = AMBA_CONSOLE,
831};
832
Alessandro Rubini03fbdb12009-05-20 22:39:08 +0100833static int pl011_probe(struct amba_device *dev, struct amba_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834{
835 struct uart_amba_port *uap;
Alessandro Rubini5926a292009-06-04 17:43:04 +0100836 struct vendor_data *vendor = id->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 void __iomem *base;
838 int i, ret;
839
840 for (i = 0; i < ARRAY_SIZE(amba_ports); i++)
841 if (amba_ports[i] == NULL)
842 break;
843
844 if (i == ARRAY_SIZE(amba_ports)) {
845 ret = -EBUSY;
846 goto out;
847 }
848
Yoann Padioleaudd00cc42007-07-19 01:49:03 -0700849 uap = kzalloc(sizeof(struct uart_amba_port), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 if (uap == NULL) {
851 ret = -ENOMEM;
852 goto out;
853 }
854
Linus Walleijdc890c22009-06-07 23:27:31 +0100855 base = ioremap(dev->res.start, resource_size(&dev->res));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 if (!base) {
857 ret = -ENOMEM;
858 goto free;
859 }
860
Russell Kingee569c42008-11-30 17:38:14 +0000861 uap->clk = clk_get(&dev->dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 if (IS_ERR(uap->clk)) {
863 ret = PTR_ERR(uap->clk);
864 goto unmap;
865 }
866
Alessandro Rubini5926a292009-06-04 17:43:04 +0100867 uap->ifls = vendor->ifls;
Linus Walleijec489aa2010-06-02 08:13:52 +0100868 uap->lcrh_rx = vendor->lcrh_rx;
869 uap->lcrh_tx = vendor->lcrh_tx;
Linus Walleijac3e3fb2010-06-02 20:40:22 +0100870 uap->oversampling = vendor->oversampling;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 uap->port.dev = &dev->dev;
872 uap->port.mapbase = dev->res.start;
873 uap->port.membase = base;
874 uap->port.iotype = UPIO_MEM;
875 uap->port.irq = dev->irq[0];
Alessandro Rubini5926a292009-06-04 17:43:04 +0100876 uap->port.fifosize = vendor->fifosize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 uap->port.ops = &amba_pl011_pops;
878 uap->port.flags = UPF_BOOT_AUTOCONF;
879 uap->port.line = i;
880
Russell Kinge8a7ba82010-12-28 09:16:54 +0000881 snprintf(uap->type, sizeof(uap->type), "PL011 rev%u", amba_rev(dev));
882
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 amba_ports[i] = uap;
884
885 amba_set_drvdata(dev, uap);
886 ret = uart_add_one_port(&amba_reg, &uap->port);
887 if (ret) {
888 amba_set_drvdata(dev, NULL);
889 amba_ports[i] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 clk_put(uap->clk);
891 unmap:
892 iounmap(base);
893 free:
894 kfree(uap);
895 }
896 out:
897 return ret;
898}
899
900static int pl011_remove(struct amba_device *dev)
901{
902 struct uart_amba_port *uap = amba_get_drvdata(dev);
903 int i;
904
905 amba_set_drvdata(dev, NULL);
906
907 uart_remove_one_port(&amba_reg, &uap->port);
908
909 for (i = 0; i < ARRAY_SIZE(amba_ports); i++)
910 if (amba_ports[i] == uap)
911 amba_ports[i] = NULL;
912
913 iounmap(uap->port.membase);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 clk_put(uap->clk);
915 kfree(uap);
916 return 0;
917}
918
Leo Chenb736b892009-07-28 23:43:33 +0100919#ifdef CONFIG_PM
920static int pl011_suspend(struct amba_device *dev, pm_message_t state)
921{
922 struct uart_amba_port *uap = amba_get_drvdata(dev);
923
924 if (!uap)
925 return -EINVAL;
926
927 return uart_suspend_port(&amba_reg, &uap->port);
928}
929
930static int pl011_resume(struct amba_device *dev)
931{
932 struct uart_amba_port *uap = amba_get_drvdata(dev);
933
934 if (!uap)
935 return -EINVAL;
936
937 return uart_resume_port(&amba_reg, &uap->port);
938}
939#endif
940
Russell King2c39c9e2010-07-27 08:50:16 +0100941static struct amba_id pl011_ids[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 {
943 .id = 0x00041011,
944 .mask = 0x000fffff,
Alessandro Rubini5926a292009-06-04 17:43:04 +0100945 .data = &vendor_arm,
946 },
947 {
948 .id = 0x00380802,
949 .mask = 0x00ffffff,
950 .data = &vendor_st,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 },
952 { 0, 0 },
953};
954
955static struct amba_driver pl011_driver = {
956 .drv = {
957 .name = "uart-pl011",
958 },
959 .id_table = pl011_ids,
960 .probe = pl011_probe,
961 .remove = pl011_remove,
Leo Chenb736b892009-07-28 23:43:33 +0100962#ifdef CONFIG_PM
963 .suspend = pl011_suspend,
964 .resume = pl011_resume,
965#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966};
967
968static int __init pl011_init(void)
969{
970 int ret;
971 printk(KERN_INFO "Serial: AMBA PL011 UART driver\n");
972
973 ret = uart_register_driver(&amba_reg);
974 if (ret == 0) {
975 ret = amba_driver_register(&pl011_driver);
976 if (ret)
977 uart_unregister_driver(&amba_reg);
978 }
979 return ret;
980}
981
982static void __exit pl011_exit(void)
983{
984 amba_driver_unregister(&pl011_driver);
985 uart_unregister_driver(&amba_reg);
986}
987
Alessandro Rubini4dd9e742009-05-05 05:54:13 +0100988/*
989 * While this can be a module, if builtin it's most likely the console
990 * So let's leave module_exit but move module_init to an earlier place
991 */
992arch_initcall(pl011_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993module_exit(pl011_exit);
994
995MODULE_AUTHOR("ARM Ltd/Deep Blue Solutions Ltd");
996MODULE_DESCRIPTION("ARM AMBA serial port driver");
997MODULE_LICENSE("GPL");