blob: 5644cf2385bbad2f60cbb5dd251395b8d6d2be8e [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 */
Rabin Vincent3b438162010-02-12 06:43:11 +010077 bool autorts;
Alessandro Rubini5926a292009-06-04 17:43:04 +010078};
79
80/* There is by now at least one vendor with differing details, so handle it */
81struct vendor_data {
82 unsigned int ifls;
83 unsigned int fifosize;
Linus Walleijec489aa2010-06-02 08:13:52 +010084 unsigned int lcrh_tx;
85 unsigned int lcrh_rx;
Alessandro Rubini5926a292009-06-04 17:43:04 +010086};
87
88static struct vendor_data vendor_arm = {
89 .ifls = UART011_IFLS_RX4_8|UART011_IFLS_TX4_8,
90 .fifosize = 16,
Linus Walleijec489aa2010-06-02 08:13:52 +010091 .lcrh_tx = UART011_LCRH,
92 .lcrh_rx = UART011_LCRH,
Alessandro Rubini5926a292009-06-04 17:43:04 +010093};
94
95static struct vendor_data vendor_st = {
96 .ifls = UART011_IFLS_RX_HALF|UART011_IFLS_TX_HALF,
97 .fifosize = 64,
Linus Walleijec489aa2010-06-02 08:13:52 +010098 .lcrh_tx = ST_UART011_LCRH_TX,
99 .lcrh_rx = ST_UART011_LCRH_RX,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100};
101
Russell Kingb129a8c2005-08-31 10:12:14 +0100102static void pl011_stop_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103{
104 struct uart_amba_port *uap = (struct uart_amba_port *)port;
105
106 uap->im &= ~UART011_TXIM;
107 writew(uap->im, uap->port.membase + UART011_IMSC);
108}
109
Russell Kingb129a8c2005-08-31 10:12:14 +0100110static void pl011_start_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111{
112 struct uart_amba_port *uap = (struct uart_amba_port *)port;
113
114 uap->im |= UART011_TXIM;
115 writew(uap->im, uap->port.membase + UART011_IMSC);
116}
117
118static void pl011_stop_rx(struct uart_port *port)
119{
120 struct uart_amba_port *uap = (struct uart_amba_port *)port;
121
122 uap->im &= ~(UART011_RXIM|UART011_RTIM|UART011_FEIM|
123 UART011_PEIM|UART011_BEIM|UART011_OEIM);
124 writew(uap->im, uap->port.membase + UART011_IMSC);
125}
126
127static void pl011_enable_ms(struct uart_port *port)
128{
129 struct uart_amba_port *uap = (struct uart_amba_port *)port;
130
131 uap->im |= UART011_RIMIM|UART011_CTSMIM|UART011_DCDMIM|UART011_DSRMIM;
132 writew(uap->im, uap->port.membase + UART011_IMSC);
133}
134
David Howells7d12e782006-10-05 14:55:46 +0100135static void pl011_rx_chars(struct uart_amba_port *uap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136{
Alan Coxebd2c8f2009-09-19 13:13:28 -0700137 struct tty_struct *tty = uap->port.state->port.tty;
Russell Kingb63d4f02005-11-19 11:10:35 +0000138 unsigned int status, ch, flag, max_count = 256;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
140 status = readw(uap->port.membase + UART01x_FR);
141 while ((status & UART01x_FR_RXFE) == 0 && max_count--) {
Russell Kingb63d4f02005-11-19 11:10:35 +0000142 ch = readw(uap->port.membase + UART01x_DR) | UART_DUMMY_DR_RX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 flag = TTY_NORMAL;
144 uap->port.icount.rx++;
145
146 /*
147 * Note that the error handling code is
148 * out of the main execution path
149 */
Russell Kingb63d4f02005-11-19 11:10:35 +0000150 if (unlikely(ch & UART_DR_ERROR)) {
151 if (ch & UART011_DR_BE) {
152 ch &= ~(UART011_DR_FE | UART011_DR_PE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 uap->port.icount.brk++;
154 if (uart_handle_break(&uap->port))
155 goto ignore_char;
Russell Kingb63d4f02005-11-19 11:10:35 +0000156 } else if (ch & UART011_DR_PE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 uap->port.icount.parity++;
Russell Kingb63d4f02005-11-19 11:10:35 +0000158 else if (ch & UART011_DR_FE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 uap->port.icount.frame++;
Russell Kingb63d4f02005-11-19 11:10:35 +0000160 if (ch & UART011_DR_OE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 uap->port.icount.overrun++;
162
Russell Kingb63d4f02005-11-19 11:10:35 +0000163 ch &= uap->port.read_status_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
Russell Kingb63d4f02005-11-19 11:10:35 +0000165 if (ch & UART011_DR_BE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 flag = TTY_BREAK;
Russell Kingb63d4f02005-11-19 11:10:35 +0000167 else if (ch & UART011_DR_PE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 flag = TTY_PARITY;
Russell Kingb63d4f02005-11-19 11:10:35 +0000169 else if (ch & UART011_DR_FE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 flag = TTY_FRAME;
171 }
172
David Howells7d12e782006-10-05 14:55:46 +0100173 if (uart_handle_sysrq_char(&uap->port, ch & 255))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 goto ignore_char;
175
Russell Kingb63d4f02005-11-19 11:10:35 +0000176 uart_insert_char(&uap->port, ch, UART011_DR_OE, ch, flag);
Russell King05ab3012005-05-09 23:21:59 +0100177
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 ignore_char:
179 status = readw(uap->port.membase + UART01x_FR);
180 }
Thomas Gleixner2389b272007-05-29 21:53:50 +0100181 spin_unlock(&uap->port.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 tty_flip_buffer_push(tty);
Thomas Gleixner2389b272007-05-29 21:53:50 +0100183 spin_lock(&uap->port.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184}
185
186static void pl011_tx_chars(struct uart_amba_port *uap)
187{
Alan Coxebd2c8f2009-09-19 13:13:28 -0700188 struct circ_buf *xmit = &uap->port.state->xmit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 int count;
190
191 if (uap->port.x_char) {
192 writew(uap->port.x_char, uap->port.membase + UART01x_DR);
193 uap->port.icount.tx++;
194 uap->port.x_char = 0;
195 return;
196 }
197 if (uart_circ_empty(xmit) || uart_tx_stopped(&uap->port)) {
Russell Kingb129a8c2005-08-31 10:12:14 +0100198 pl011_stop_tx(&uap->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 return;
200 }
201
202 count = uap->port.fifosize >> 1;
203 do {
204 writew(xmit->buf[xmit->tail], uap->port.membase + UART01x_DR);
205 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
206 uap->port.icount.tx++;
207 if (uart_circ_empty(xmit))
208 break;
209 } while (--count > 0);
210
211 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
212 uart_write_wakeup(&uap->port);
213
214 if (uart_circ_empty(xmit))
Russell Kingb129a8c2005-08-31 10:12:14 +0100215 pl011_stop_tx(&uap->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216}
217
218static void pl011_modem_status(struct uart_amba_port *uap)
219{
220 unsigned int status, delta;
221
222 status = readw(uap->port.membase + UART01x_FR) & UART01x_FR_MODEM_ANY;
223
224 delta = status ^ uap->old_status;
225 uap->old_status = status;
226
227 if (!delta)
228 return;
229
230 if (delta & UART01x_FR_DCD)
231 uart_handle_dcd_change(&uap->port, status & UART01x_FR_DCD);
232
233 if (delta & UART01x_FR_DSR)
234 uap->port.icount.dsr++;
235
236 if (delta & UART01x_FR_CTS)
237 uart_handle_cts_change(&uap->port, status & UART01x_FR_CTS);
238
Alan Coxbdc04e32009-09-19 13:13:31 -0700239 wake_up_interruptible(&uap->port.state->port.delta_msr_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240}
241
David Howells7d12e782006-10-05 14:55:46 +0100242static irqreturn_t pl011_int(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243{
244 struct uart_amba_port *uap = dev_id;
245 unsigned int status, pass_counter = AMBA_ISR_PASS_LIMIT;
246 int handled = 0;
247
248 spin_lock(&uap->port.lock);
249
250 status = readw(uap->port.membase + UART011_MIS);
251 if (status) {
252 do {
253 writew(status & ~(UART011_TXIS|UART011_RTIS|
254 UART011_RXIS),
255 uap->port.membase + UART011_ICR);
256
257 if (status & (UART011_RTIS|UART011_RXIS))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 pl011_rx_chars(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 if (status & (UART011_DSRMIS|UART011_DCDMIS|
260 UART011_CTSMIS|UART011_RIMIS))
261 pl011_modem_status(uap);
262 if (status & UART011_TXIS)
263 pl011_tx_chars(uap);
264
265 if (pass_counter-- == 0)
266 break;
267
268 status = readw(uap->port.membase + UART011_MIS);
269 } while (status != 0);
270 handled = 1;
271 }
272
273 spin_unlock(&uap->port.lock);
274
275 return IRQ_RETVAL(handled);
276}
277
278static unsigned int pl01x_tx_empty(struct uart_port *port)
279{
280 struct uart_amba_port *uap = (struct uart_amba_port *)port;
281 unsigned int status = readw(uap->port.membase + UART01x_FR);
282 return status & (UART01x_FR_BUSY|UART01x_FR_TXFF) ? 0 : TIOCSER_TEMT;
283}
284
285static unsigned int pl01x_get_mctrl(struct uart_port *port)
286{
287 struct uart_amba_port *uap = (struct uart_amba_port *)port;
288 unsigned int result = 0;
289 unsigned int status = readw(uap->port.membase + UART01x_FR);
290
Jiri Slaby5159f402007-10-18 23:40:31 -0700291#define TIOCMBIT(uartbit, tiocmbit) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 if (status & uartbit) \
293 result |= tiocmbit
294
Jiri Slaby5159f402007-10-18 23:40:31 -0700295 TIOCMBIT(UART01x_FR_DCD, TIOCM_CAR);
296 TIOCMBIT(UART01x_FR_DSR, TIOCM_DSR);
297 TIOCMBIT(UART01x_FR_CTS, TIOCM_CTS);
298 TIOCMBIT(UART011_FR_RI, TIOCM_RNG);
299#undef TIOCMBIT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 return result;
301}
302
303static void pl011_set_mctrl(struct uart_port *port, unsigned int mctrl)
304{
305 struct uart_amba_port *uap = (struct uart_amba_port *)port;
306 unsigned int cr;
307
308 cr = readw(uap->port.membase + UART011_CR);
309
Jiri Slaby5159f402007-10-18 23:40:31 -0700310#define TIOCMBIT(tiocmbit, uartbit) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 if (mctrl & tiocmbit) \
312 cr |= uartbit; \
313 else \
314 cr &= ~uartbit
315
Jiri Slaby5159f402007-10-18 23:40:31 -0700316 TIOCMBIT(TIOCM_RTS, UART011_CR_RTS);
317 TIOCMBIT(TIOCM_DTR, UART011_CR_DTR);
318 TIOCMBIT(TIOCM_OUT1, UART011_CR_OUT1);
319 TIOCMBIT(TIOCM_OUT2, UART011_CR_OUT2);
320 TIOCMBIT(TIOCM_LOOP, UART011_CR_LBE);
Rabin Vincent3b438162010-02-12 06:43:11 +0100321
322 if (uap->autorts) {
323 /* We need to disable auto-RTS if we want to turn RTS off */
324 TIOCMBIT(TIOCM_RTS, UART011_CR_RTSEN);
325 }
Jiri Slaby5159f402007-10-18 23:40:31 -0700326#undef TIOCMBIT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
328 writew(cr, uap->port.membase + UART011_CR);
329}
330
331static void pl011_break_ctl(struct uart_port *port, int break_state)
332{
333 struct uart_amba_port *uap = (struct uart_amba_port *)port;
334 unsigned long flags;
335 unsigned int lcr_h;
336
337 spin_lock_irqsave(&uap->port.lock, flags);
Linus Walleijec489aa2010-06-02 08:13:52 +0100338 lcr_h = readw(uap->port.membase + uap->lcrh_tx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 if (break_state == -1)
340 lcr_h |= UART01x_LCRH_BRK;
341 else
342 lcr_h &= ~UART01x_LCRH_BRK;
Linus Walleijec489aa2010-06-02 08:13:52 +0100343 writew(lcr_h, uap->port.membase + uap->lcrh_tx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 spin_unlock_irqrestore(&uap->port.lock, flags);
345}
346
Jason Wessel84b5ae12008-02-20 13:33:39 -0600347#ifdef CONFIG_CONSOLE_POLL
348static int pl010_get_poll_char(struct uart_port *port)
349{
350 struct uart_amba_port *uap = (struct uart_amba_port *)port;
351 unsigned int status;
352
Jason Wesself5316b42010-05-20 21:04:22 -0500353 status = readw(uap->port.membase + UART01x_FR);
354 if (status & UART01x_FR_RXFE)
355 return NO_POLL_CHAR;
Jason Wessel84b5ae12008-02-20 13:33:39 -0600356
357 return readw(uap->port.membase + UART01x_DR);
358}
359
360static void pl010_put_poll_char(struct uart_port *port,
361 unsigned char ch)
362{
363 struct uart_amba_port *uap = (struct uart_amba_port *)port;
364
365 while (readw(uap->port.membase + UART01x_FR) & UART01x_FR_TXFF)
366 barrier();
367
368 writew(ch, uap->port.membase + UART01x_DR);
369}
370
371#endif /* CONFIG_CONSOLE_POLL */
372
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373static int pl011_startup(struct uart_port *port)
374{
375 struct uart_amba_port *uap = (struct uart_amba_port *)port;
376 unsigned int cr;
377 int retval;
378
379 /*
380 * Try to enable the clock producer.
381 */
382 retval = clk_enable(uap->clk);
383 if (retval)
384 goto out;
385
386 uap->port.uartclk = clk_get_rate(uap->clk);
387
388 /*
389 * Allocate the IRQ
390 */
391 retval = request_irq(uap->port.irq, pl011_int, 0, "uart-pl011", uap);
392 if (retval)
393 goto clk_dis;
394
Alessandro Rubini5926a292009-06-04 17:43:04 +0100395 writew(uap->ifls, uap->port.membase + UART011_IFLS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
397 /*
398 * Provoke TX FIFO interrupt into asserting.
399 */
400 cr = UART01x_CR_UARTEN | UART011_CR_TXE | UART011_CR_LBE;
401 writew(cr, uap->port.membase + UART011_CR);
402 writew(0, uap->port.membase + UART011_FBRD);
403 writew(1, uap->port.membase + UART011_IBRD);
Linus Walleijec489aa2010-06-02 08:13:52 +0100404 writew(0, uap->port.membase + uap->lcrh_rx);
405 if (uap->lcrh_tx != uap->lcrh_rx) {
406 int i;
407 /*
408 * Wait 10 PCLKs before writing LCRH_TX register,
409 * to get this delay write read only register 10 times
410 */
411 for (i = 0; i < 10; ++i)
412 writew(0xff, uap->port.membase + UART011_MIS);
413 writew(0, uap->port.membase + uap->lcrh_tx);
414 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 writew(0, uap->port.membase + UART01x_DR);
416 while (readw(uap->port.membase + UART01x_FR) & UART01x_FR_BUSY)
417 barrier();
418
419 cr = UART01x_CR_UARTEN | UART011_CR_RXE | UART011_CR_TXE;
420 writew(cr, uap->port.membase + UART011_CR);
421
422 /*
423 * initialise the old status of the modem signals
424 */
425 uap->old_status = readw(uap->port.membase + UART01x_FR) & UART01x_FR_MODEM_ANY;
426
427 /*
428 * Finally, enable interrupts
429 */
430 spin_lock_irq(&uap->port.lock);
431 uap->im = UART011_RXIM | UART011_RTIM;
432 writew(uap->im, uap->port.membase + UART011_IMSC);
433 spin_unlock_irq(&uap->port.lock);
434
435 return 0;
436
437 clk_dis:
438 clk_disable(uap->clk);
439 out:
440 return retval;
441}
442
Linus Walleijec489aa2010-06-02 08:13:52 +0100443static void pl011_shutdown_channel(struct uart_amba_port *uap,
444 unsigned int lcrh)
445{
446 unsigned long val;
447
448 val = readw(uap->port.membase + lcrh);
449 val &= ~(UART01x_LCRH_BRK | UART01x_LCRH_FEN);
450 writew(val, uap->port.membase + lcrh);
451}
452
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453static void pl011_shutdown(struct uart_port *port)
454{
455 struct uart_amba_port *uap = (struct uart_amba_port *)port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
457 /*
458 * disable all interrupts
459 */
460 spin_lock_irq(&uap->port.lock);
461 uap->im = 0;
462 writew(uap->im, uap->port.membase + UART011_IMSC);
463 writew(0xffff, uap->port.membase + UART011_ICR);
464 spin_unlock_irq(&uap->port.lock);
465
466 /*
467 * Free the interrupt
468 */
469 free_irq(uap->port.irq, uap);
470
471 /*
472 * disable the port
473 */
Rabin Vincent3b438162010-02-12 06:43:11 +0100474 uap->autorts = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 writew(UART01x_CR_UARTEN | UART011_CR_TXE, uap->port.membase + UART011_CR);
476
477 /*
478 * disable break condition and fifos
479 */
Linus Walleijec489aa2010-06-02 08:13:52 +0100480 pl011_shutdown_channel(uap, uap->lcrh_rx);
481 if (uap->lcrh_rx != uap->lcrh_tx)
482 pl011_shutdown_channel(uap, uap->lcrh_tx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
484 /*
485 * Shut down the clock producer
486 */
487 clk_disable(uap->clk);
488}
489
490static void
Alan Cox606d0992006-12-08 02:38:45 -0800491pl011_set_termios(struct uart_port *port, struct ktermios *termios,
492 struct ktermios *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493{
Rabin Vincent3b438162010-02-12 06:43:11 +0100494 struct uart_amba_port *uap = (struct uart_amba_port *)port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 unsigned int lcr_h, old_cr;
496 unsigned long flags;
497 unsigned int baud, quot;
498
499 /*
500 * Ask the core to calculate the divisor for us.
501 */
502 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
503 quot = port->uartclk * 4 / baud;
504
505 switch (termios->c_cflag & CSIZE) {
506 case CS5:
507 lcr_h = UART01x_LCRH_WLEN_5;
508 break;
509 case CS6:
510 lcr_h = UART01x_LCRH_WLEN_6;
511 break;
512 case CS7:
513 lcr_h = UART01x_LCRH_WLEN_7;
514 break;
515 default: // CS8
516 lcr_h = UART01x_LCRH_WLEN_8;
517 break;
518 }
519 if (termios->c_cflag & CSTOPB)
520 lcr_h |= UART01x_LCRH_STP2;
521 if (termios->c_cflag & PARENB) {
522 lcr_h |= UART01x_LCRH_PEN;
523 if (!(termios->c_cflag & PARODD))
524 lcr_h |= UART01x_LCRH_EPS;
525 }
526 if (port->fifosize > 1)
527 lcr_h |= UART01x_LCRH_FEN;
528
529 spin_lock_irqsave(&port->lock, flags);
530
531 /*
532 * Update the per-port timeout.
533 */
534 uart_update_timeout(port, termios->c_cflag, baud);
535
Russell Kingb63d4f02005-11-19 11:10:35 +0000536 port->read_status_mask = UART011_DR_OE | 255;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 if (termios->c_iflag & INPCK)
Russell Kingb63d4f02005-11-19 11:10:35 +0000538 port->read_status_mask |= UART011_DR_FE | UART011_DR_PE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 if (termios->c_iflag & (BRKINT | PARMRK))
Russell Kingb63d4f02005-11-19 11:10:35 +0000540 port->read_status_mask |= UART011_DR_BE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
542 /*
543 * Characters to ignore
544 */
545 port->ignore_status_mask = 0;
546 if (termios->c_iflag & IGNPAR)
Russell Kingb63d4f02005-11-19 11:10:35 +0000547 port->ignore_status_mask |= UART011_DR_FE | UART011_DR_PE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 if (termios->c_iflag & IGNBRK) {
Russell Kingb63d4f02005-11-19 11:10:35 +0000549 port->ignore_status_mask |= UART011_DR_BE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 /*
551 * If we're ignoring parity and break indicators,
552 * ignore overruns too (for real raw support).
553 */
554 if (termios->c_iflag & IGNPAR)
Russell Kingb63d4f02005-11-19 11:10:35 +0000555 port->ignore_status_mask |= UART011_DR_OE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 }
557
558 /*
559 * Ignore all characters if CREAD is not set.
560 */
561 if ((termios->c_cflag & CREAD) == 0)
Russell Kingb63d4f02005-11-19 11:10:35 +0000562 port->ignore_status_mask |= UART_DUMMY_DR_RX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
564 if (UART_ENABLE_MS(port, termios->c_cflag))
565 pl011_enable_ms(port);
566
567 /* first, disable everything */
568 old_cr = readw(port->membase + UART011_CR);
569 writew(0, port->membase + UART011_CR);
570
Rabin Vincent3b438162010-02-12 06:43:11 +0100571 if (termios->c_cflag & CRTSCTS) {
572 if (old_cr & UART011_CR_RTS)
573 old_cr |= UART011_CR_RTSEN;
574
575 old_cr |= UART011_CR_CTSEN;
576 uap->autorts = true;
577 } else {
578 old_cr &= ~(UART011_CR_CTSEN | UART011_CR_RTSEN);
579 uap->autorts = false;
580 }
581
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 /* Set baud rate */
583 writew(quot & 0x3f, port->membase + UART011_FBRD);
584 writew(quot >> 6, port->membase + UART011_IBRD);
585
586 /*
587 * ----------v----------v----------v----------v-----
588 * NOTE: MUST BE WRITTEN AFTER UARTLCR_M & UARTLCR_L
589 * ----------^----------^----------^----------^-----
590 */
Linus Walleijec489aa2010-06-02 08:13:52 +0100591 writew(lcr_h, port->membase + uap->lcrh_rx);
592 if (uap->lcrh_rx != uap->lcrh_tx) {
593 int i;
594 /*
595 * Wait 10 PCLKs before writing LCRH_TX register,
596 * to get this delay write read only register 10 times
597 */
598 for (i = 0; i < 10; ++i)
599 writew(0xff, uap->port.membase + UART011_MIS);
600 writew(lcr_h, port->membase + uap->lcrh_tx);
601 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 writew(old_cr, port->membase + UART011_CR);
603
604 spin_unlock_irqrestore(&port->lock, flags);
605}
606
607static const char *pl011_type(struct uart_port *port)
608{
609 return port->type == PORT_AMBA ? "AMBA/PL011" : NULL;
610}
611
612/*
613 * Release the memory region(s) being used by 'port'
614 */
615static void pl010_release_port(struct uart_port *port)
616{
617 release_mem_region(port->mapbase, SZ_4K);
618}
619
620/*
621 * Request the memory region(s) being used by 'port'
622 */
623static int pl010_request_port(struct uart_port *port)
624{
625 return request_mem_region(port->mapbase, SZ_4K, "uart-pl011")
626 != NULL ? 0 : -EBUSY;
627}
628
629/*
630 * Configure/autoconfigure the port.
631 */
632static void pl010_config_port(struct uart_port *port, int flags)
633{
634 if (flags & UART_CONFIG_TYPE) {
635 port->type = PORT_AMBA;
636 pl010_request_port(port);
637 }
638}
639
640/*
641 * verify the new serial_struct (for TIOCSSERIAL).
642 */
643static int pl010_verify_port(struct uart_port *port, struct serial_struct *ser)
644{
645 int ret = 0;
646 if (ser->type != PORT_UNKNOWN && ser->type != PORT_AMBA)
647 ret = -EINVAL;
Yinghai Lua62c4132008-08-19 20:49:55 -0700648 if (ser->irq < 0 || ser->irq >= nr_irqs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 ret = -EINVAL;
650 if (ser->baud_base < 9600)
651 ret = -EINVAL;
652 return ret;
653}
654
655static struct uart_ops amba_pl011_pops = {
656 .tx_empty = pl01x_tx_empty,
657 .set_mctrl = pl011_set_mctrl,
658 .get_mctrl = pl01x_get_mctrl,
659 .stop_tx = pl011_stop_tx,
660 .start_tx = pl011_start_tx,
661 .stop_rx = pl011_stop_rx,
662 .enable_ms = pl011_enable_ms,
663 .break_ctl = pl011_break_ctl,
664 .startup = pl011_startup,
665 .shutdown = pl011_shutdown,
666 .set_termios = pl011_set_termios,
667 .type = pl011_type,
668 .release_port = pl010_release_port,
669 .request_port = pl010_request_port,
670 .config_port = pl010_config_port,
671 .verify_port = pl010_verify_port,
Jason Wessel84b5ae12008-02-20 13:33:39 -0600672#ifdef CONFIG_CONSOLE_POLL
673 .poll_get_char = pl010_get_poll_char,
674 .poll_put_char = pl010_put_poll_char,
675#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676};
677
678static struct uart_amba_port *amba_ports[UART_NR];
679
680#ifdef CONFIG_SERIAL_AMBA_PL011_CONSOLE
681
Russell Kingd3587882006-03-20 20:00:09 +0000682static void pl011_console_putchar(struct uart_port *port, int ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683{
Russell Kingd3587882006-03-20 20:00:09 +0000684 struct uart_amba_port *uap = (struct uart_amba_port *)port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
Russell Kingd3587882006-03-20 20:00:09 +0000686 while (readw(uap->port.membase + UART01x_FR) & UART01x_FR_TXFF)
687 barrier();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 writew(ch, uap->port.membase + UART01x_DR);
689}
690
691static void
692pl011_console_write(struct console *co, const char *s, unsigned int count)
693{
694 struct uart_amba_port *uap = amba_ports[co->index];
695 unsigned int status, old_cr, new_cr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
697 clk_enable(uap->clk);
698
699 /*
700 * First save the CR then disable the interrupts
701 */
702 old_cr = readw(uap->port.membase + UART011_CR);
703 new_cr = old_cr & ~UART011_CR_CTSEN;
704 new_cr |= UART01x_CR_UARTEN | UART011_CR_TXE;
705 writew(new_cr, uap->port.membase + UART011_CR);
706
Russell Kingd3587882006-03-20 20:00:09 +0000707 uart_console_write(&uap->port, s, count, pl011_console_putchar);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
709 /*
710 * Finally, wait for transmitter to become empty
711 * and restore the TCR
712 */
713 do {
714 status = readw(uap->port.membase + UART01x_FR);
715 } while (status & UART01x_FR_BUSY);
716 writew(old_cr, uap->port.membase + UART011_CR);
717
718 clk_disable(uap->clk);
719}
720
721static void __init
722pl011_console_get_options(struct uart_amba_port *uap, int *baud,
723 int *parity, int *bits)
724{
725 if (readw(uap->port.membase + UART011_CR) & UART01x_CR_UARTEN) {
726 unsigned int lcr_h, ibrd, fbrd;
727
Linus Walleijec489aa2010-06-02 08:13:52 +0100728 lcr_h = readw(uap->port.membase + uap->lcrh_tx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
730 *parity = 'n';
731 if (lcr_h & UART01x_LCRH_PEN) {
732 if (lcr_h & UART01x_LCRH_EPS)
733 *parity = 'e';
734 else
735 *parity = 'o';
736 }
737
738 if ((lcr_h & 0x60) == UART01x_LCRH_WLEN_7)
739 *bits = 7;
740 else
741 *bits = 8;
742
743 ibrd = readw(uap->port.membase + UART011_IBRD);
744 fbrd = readw(uap->port.membase + UART011_FBRD);
745
746 *baud = uap->port.uartclk * 4 / (64 * ibrd + fbrd);
747 }
748}
749
750static int __init pl011_console_setup(struct console *co, char *options)
751{
752 struct uart_amba_port *uap;
753 int baud = 38400;
754 int bits = 8;
755 int parity = 'n';
756 int flow = 'n';
757
758 /*
759 * Check whether an invalid uart number has been specified, and
760 * if so, search for the first available port that does have
761 * console support.
762 */
763 if (co->index >= UART_NR)
764 co->index = 0;
765 uap = amba_ports[co->index];
Russell Kingd28122a2007-01-22 18:59:42 +0000766 if (!uap)
767 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
769 uap->port.uartclk = clk_get_rate(uap->clk);
770
771 if (options)
772 uart_parse_options(options, &baud, &parity, &bits, &flow);
773 else
774 pl011_console_get_options(uap, &baud, &parity, &bits);
775
776 return uart_set_options(&uap->port, co, baud, parity, bits, flow);
777}
778
Vincent Sanders2d934862005-09-14 22:36:03 +0100779static struct uart_driver amba_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780static struct console amba_console = {
781 .name = "ttyAMA",
782 .write = pl011_console_write,
783 .device = uart_console_device,
784 .setup = pl011_console_setup,
785 .flags = CON_PRINTBUFFER,
786 .index = -1,
787 .data = &amba_reg,
788};
789
790#define AMBA_CONSOLE (&amba_console)
791#else
792#define AMBA_CONSOLE NULL
793#endif
794
795static struct uart_driver amba_reg = {
796 .owner = THIS_MODULE,
797 .driver_name = "ttyAMA",
798 .dev_name = "ttyAMA",
799 .major = SERIAL_AMBA_MAJOR,
800 .minor = SERIAL_AMBA_MINOR,
801 .nr = UART_NR,
802 .cons = AMBA_CONSOLE,
803};
804
Alessandro Rubini03fbdb12009-05-20 22:39:08 +0100805static int pl011_probe(struct amba_device *dev, struct amba_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806{
807 struct uart_amba_port *uap;
Alessandro Rubini5926a292009-06-04 17:43:04 +0100808 struct vendor_data *vendor = id->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 void __iomem *base;
810 int i, ret;
811
812 for (i = 0; i < ARRAY_SIZE(amba_ports); i++)
813 if (amba_ports[i] == NULL)
814 break;
815
816 if (i == ARRAY_SIZE(amba_ports)) {
817 ret = -EBUSY;
818 goto out;
819 }
820
Yoann Padioleaudd00cc42007-07-19 01:49:03 -0700821 uap = kzalloc(sizeof(struct uart_amba_port), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 if (uap == NULL) {
823 ret = -ENOMEM;
824 goto out;
825 }
826
Linus Walleijdc890c22009-06-07 23:27:31 +0100827 base = ioremap(dev->res.start, resource_size(&dev->res));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 if (!base) {
829 ret = -ENOMEM;
830 goto free;
831 }
832
Russell Kingee569c42008-11-30 17:38:14 +0000833 uap->clk = clk_get(&dev->dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 if (IS_ERR(uap->clk)) {
835 ret = PTR_ERR(uap->clk);
836 goto unmap;
837 }
838
Alessandro Rubini5926a292009-06-04 17:43:04 +0100839 uap->ifls = vendor->ifls;
Linus Walleijec489aa2010-06-02 08:13:52 +0100840 uap->lcrh_rx = vendor->lcrh_rx;
841 uap->lcrh_tx = vendor->lcrh_tx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 uap->port.dev = &dev->dev;
843 uap->port.mapbase = dev->res.start;
844 uap->port.membase = base;
845 uap->port.iotype = UPIO_MEM;
846 uap->port.irq = dev->irq[0];
Alessandro Rubini5926a292009-06-04 17:43:04 +0100847 uap->port.fifosize = vendor->fifosize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 uap->port.ops = &amba_pl011_pops;
849 uap->port.flags = UPF_BOOT_AUTOCONF;
850 uap->port.line = i;
851
852 amba_ports[i] = uap;
853
854 amba_set_drvdata(dev, uap);
855 ret = uart_add_one_port(&amba_reg, &uap->port);
856 if (ret) {
857 amba_set_drvdata(dev, NULL);
858 amba_ports[i] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 clk_put(uap->clk);
860 unmap:
861 iounmap(base);
862 free:
863 kfree(uap);
864 }
865 out:
866 return ret;
867}
868
869static int pl011_remove(struct amba_device *dev)
870{
871 struct uart_amba_port *uap = amba_get_drvdata(dev);
872 int i;
873
874 amba_set_drvdata(dev, NULL);
875
876 uart_remove_one_port(&amba_reg, &uap->port);
877
878 for (i = 0; i < ARRAY_SIZE(amba_ports); i++)
879 if (amba_ports[i] == uap)
880 amba_ports[i] = NULL;
881
882 iounmap(uap->port.membase);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 clk_put(uap->clk);
884 kfree(uap);
885 return 0;
886}
887
Leo Chenb736b892009-07-28 23:43:33 +0100888#ifdef CONFIG_PM
889static int pl011_suspend(struct amba_device *dev, pm_message_t state)
890{
891 struct uart_amba_port *uap = amba_get_drvdata(dev);
892
893 if (!uap)
894 return -EINVAL;
895
896 return uart_suspend_port(&amba_reg, &uap->port);
897}
898
899static int pl011_resume(struct amba_device *dev)
900{
901 struct uart_amba_port *uap = amba_get_drvdata(dev);
902
903 if (!uap)
904 return -EINVAL;
905
906 return uart_resume_port(&amba_reg, &uap->port);
907}
908#endif
909
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910static struct amba_id pl011_ids[] __initdata = {
911 {
912 .id = 0x00041011,
913 .mask = 0x000fffff,
Alessandro Rubini5926a292009-06-04 17:43:04 +0100914 .data = &vendor_arm,
915 },
916 {
917 .id = 0x00380802,
918 .mask = 0x00ffffff,
919 .data = &vendor_st,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 },
921 { 0, 0 },
922};
923
924static struct amba_driver pl011_driver = {
925 .drv = {
926 .name = "uart-pl011",
927 },
928 .id_table = pl011_ids,
929 .probe = pl011_probe,
930 .remove = pl011_remove,
Leo Chenb736b892009-07-28 23:43:33 +0100931#ifdef CONFIG_PM
932 .suspend = pl011_suspend,
933 .resume = pl011_resume,
934#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935};
936
937static int __init pl011_init(void)
938{
939 int ret;
940 printk(KERN_INFO "Serial: AMBA PL011 UART driver\n");
941
942 ret = uart_register_driver(&amba_reg);
943 if (ret == 0) {
944 ret = amba_driver_register(&pl011_driver);
945 if (ret)
946 uart_unregister_driver(&amba_reg);
947 }
948 return ret;
949}
950
951static void __exit pl011_exit(void)
952{
953 amba_driver_unregister(&pl011_driver);
954 uart_unregister_driver(&amba_reg);
955}
956
Alessandro Rubini4dd9e742009-05-05 05:54:13 +0100957/*
958 * While this can be a module, if builtin it's most likely the console
959 * So let's leave module_exit but move module_init to an earlier place
960 */
961arch_initcall(pl011_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962module_exit(pl011_exit);
963
964MODULE_AUTHOR("ARM Ltd/Deep Blue Solutions Ltd");
965MODULE_DESCRIPTION("ARM AMBA serial port driver");
966MODULE_LICENSE("GPL");