blob: f741a8b51400a4da9bf88d0c9da666ed813b9761 [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
Alessandro Rubini5926a292009-06-04 17:43:04 +010066/* There is by now at least one vendor with differing details, so handle it */
67struct vendor_data {
68 unsigned int ifls;
69 unsigned int fifosize;
Linus Walleijec489aa2010-06-02 08:13:52 +010070 unsigned int lcrh_tx;
71 unsigned int lcrh_rx;
Linus Walleijac3e3fb2010-06-02 20:40:22 +010072 bool oversampling;
Alessandro Rubini5926a292009-06-04 17:43:04 +010073};
74
75static struct vendor_data vendor_arm = {
76 .ifls = UART011_IFLS_RX4_8|UART011_IFLS_TX4_8,
77 .fifosize = 16,
Linus Walleijec489aa2010-06-02 08:13:52 +010078 .lcrh_tx = UART011_LCRH,
79 .lcrh_rx = UART011_LCRH,
Linus Walleijac3e3fb2010-06-02 20:40:22 +010080 .oversampling = false,
Alessandro Rubini5926a292009-06-04 17:43:04 +010081};
82
83static struct vendor_data vendor_st = {
84 .ifls = UART011_IFLS_RX_HALF|UART011_IFLS_TX_HALF,
85 .fifosize = 64,
Linus Walleijec489aa2010-06-02 08:13:52 +010086 .lcrh_tx = ST_UART011_LCRH_TX,
87 .lcrh_rx = ST_UART011_LCRH_RX,
Linus Walleijac3e3fb2010-06-02 20:40:22 +010088 .oversampling = true,
Linus Torvalds1da177e2005-04-16 15:20:36 -070089};
90
Russell Kingc19f12b2010-12-22 17:48:26 +000091/*
92 * We wrap our port structure around the generic uart_port.
93 */
94struct uart_amba_port {
95 struct uart_port port;
96 struct clk *clk;
97 const struct vendor_data *vendor;
98 unsigned int im; /* interrupt mask */
99 unsigned int old_status;
Russell Kingffca2b12010-12-22 17:13:05 +0000100 unsigned int fifosize; /* vendor-specific */
Russell Kingc19f12b2010-12-22 17:48:26 +0000101 unsigned int lcrh_tx; /* vendor-specific */
102 unsigned int lcrh_rx; /* vendor-specific */
103 bool autorts;
104 char type[12];
105};
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
Russell Kingffca2b12010-12-22 17:13:05 +0000207 count = uap->fifosize >> 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 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;
Russell King963cc982010-12-22 17:16:09 +0000250 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 unsigned int status, pass_counter = AMBA_ISR_PASS_LIMIT;
252 int handled = 0;
253
Russell King963cc982010-12-22 17:16:09 +0000254 spin_lock_irqsave(&uap->port.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
256 status = readw(uap->port.membase + UART011_MIS);
257 if (status) {
258 do {
259 writew(status & ~(UART011_TXIS|UART011_RTIS|
260 UART011_RXIS),
261 uap->port.membase + UART011_ICR);
262
263 if (status & (UART011_RTIS|UART011_RXIS))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 pl011_rx_chars(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 if (status & (UART011_DSRMIS|UART011_DCDMIS|
266 UART011_CTSMIS|UART011_RIMIS))
267 pl011_modem_status(uap);
268 if (status & UART011_TXIS)
269 pl011_tx_chars(uap);
270
271 if (pass_counter-- == 0)
272 break;
273
274 status = readw(uap->port.membase + UART011_MIS);
275 } while (status != 0);
276 handled = 1;
277 }
278
Russell King963cc982010-12-22 17:16:09 +0000279 spin_unlock_irqrestore(&uap->port.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
281 return IRQ_RETVAL(handled);
282}
283
284static unsigned int pl01x_tx_empty(struct uart_port *port)
285{
286 struct uart_amba_port *uap = (struct uart_amba_port *)port;
287 unsigned int status = readw(uap->port.membase + UART01x_FR);
288 return status & (UART01x_FR_BUSY|UART01x_FR_TXFF) ? 0 : TIOCSER_TEMT;
289}
290
291static unsigned int pl01x_get_mctrl(struct uart_port *port)
292{
293 struct uart_amba_port *uap = (struct uart_amba_port *)port;
294 unsigned int result = 0;
295 unsigned int status = readw(uap->port.membase + UART01x_FR);
296
Jiri Slaby5159f402007-10-18 23:40:31 -0700297#define TIOCMBIT(uartbit, tiocmbit) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 if (status & uartbit) \
299 result |= tiocmbit
300
Jiri Slaby5159f402007-10-18 23:40:31 -0700301 TIOCMBIT(UART01x_FR_DCD, TIOCM_CAR);
302 TIOCMBIT(UART01x_FR_DSR, TIOCM_DSR);
303 TIOCMBIT(UART01x_FR_CTS, TIOCM_CTS);
304 TIOCMBIT(UART011_FR_RI, TIOCM_RNG);
305#undef TIOCMBIT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 return result;
307}
308
309static void pl011_set_mctrl(struct uart_port *port, unsigned int mctrl)
310{
311 struct uart_amba_port *uap = (struct uart_amba_port *)port;
312 unsigned int cr;
313
314 cr = readw(uap->port.membase + UART011_CR);
315
Jiri Slaby5159f402007-10-18 23:40:31 -0700316#define TIOCMBIT(tiocmbit, uartbit) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 if (mctrl & tiocmbit) \
318 cr |= uartbit; \
319 else \
320 cr &= ~uartbit
321
Jiri Slaby5159f402007-10-18 23:40:31 -0700322 TIOCMBIT(TIOCM_RTS, UART011_CR_RTS);
323 TIOCMBIT(TIOCM_DTR, UART011_CR_DTR);
324 TIOCMBIT(TIOCM_OUT1, UART011_CR_OUT1);
325 TIOCMBIT(TIOCM_OUT2, UART011_CR_OUT2);
326 TIOCMBIT(TIOCM_LOOP, UART011_CR_LBE);
Rabin Vincent3b438162010-02-12 06:43:11 +0100327
328 if (uap->autorts) {
329 /* We need to disable auto-RTS if we want to turn RTS off */
330 TIOCMBIT(TIOCM_RTS, UART011_CR_RTSEN);
331 }
Jiri Slaby5159f402007-10-18 23:40:31 -0700332#undef TIOCMBIT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
334 writew(cr, uap->port.membase + UART011_CR);
335}
336
337static void pl011_break_ctl(struct uart_port *port, int break_state)
338{
339 struct uart_amba_port *uap = (struct uart_amba_port *)port;
340 unsigned long flags;
341 unsigned int lcr_h;
342
343 spin_lock_irqsave(&uap->port.lock, flags);
Linus Walleijec489aa2010-06-02 08:13:52 +0100344 lcr_h = readw(uap->port.membase + uap->lcrh_tx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 if (break_state == -1)
346 lcr_h |= UART01x_LCRH_BRK;
347 else
348 lcr_h &= ~UART01x_LCRH_BRK;
Linus Walleijec489aa2010-06-02 08:13:52 +0100349 writew(lcr_h, uap->port.membase + uap->lcrh_tx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 spin_unlock_irqrestore(&uap->port.lock, flags);
351}
352
Jason Wessel84b5ae12008-02-20 13:33:39 -0600353#ifdef CONFIG_CONSOLE_POLL
354static int pl010_get_poll_char(struct uart_port *port)
355{
356 struct uart_amba_port *uap = (struct uart_amba_port *)port;
357 unsigned int status;
358
Jason Wesself5316b42010-05-20 21:04:22 -0500359 status = readw(uap->port.membase + UART01x_FR);
360 if (status & UART01x_FR_RXFE)
361 return NO_POLL_CHAR;
Jason Wessel84b5ae12008-02-20 13:33:39 -0600362
363 return readw(uap->port.membase + UART01x_DR);
364}
365
366static void pl010_put_poll_char(struct uart_port *port,
367 unsigned char ch)
368{
369 struct uart_amba_port *uap = (struct uart_amba_port *)port;
370
371 while (readw(uap->port.membase + UART01x_FR) & UART01x_FR_TXFF)
372 barrier();
373
374 writew(ch, uap->port.membase + UART01x_DR);
375}
376
377#endif /* CONFIG_CONSOLE_POLL */
378
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379static int pl011_startup(struct uart_port *port)
380{
381 struct uart_amba_port *uap = (struct uart_amba_port *)port;
382 unsigned int cr;
383 int retval;
384
385 /*
386 * Try to enable the clock producer.
387 */
388 retval = clk_enable(uap->clk);
389 if (retval)
390 goto out;
391
392 uap->port.uartclk = clk_get_rate(uap->clk);
393
394 /*
395 * Allocate the IRQ
396 */
397 retval = request_irq(uap->port.irq, pl011_int, 0, "uart-pl011", uap);
398 if (retval)
399 goto clk_dis;
400
Russell Kingc19f12b2010-12-22 17:48:26 +0000401 writew(uap->vendor->ifls, uap->port.membase + UART011_IFLS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
403 /*
404 * Provoke TX FIFO interrupt into asserting.
405 */
406 cr = UART01x_CR_UARTEN | UART011_CR_TXE | UART011_CR_LBE;
407 writew(cr, uap->port.membase + UART011_CR);
408 writew(0, uap->port.membase + UART011_FBRD);
409 writew(1, uap->port.membase + UART011_IBRD);
Linus Walleijec489aa2010-06-02 08:13:52 +0100410 writew(0, uap->port.membase + uap->lcrh_rx);
411 if (uap->lcrh_tx != uap->lcrh_rx) {
412 int i;
413 /*
414 * Wait 10 PCLKs before writing LCRH_TX register,
415 * to get this delay write read only register 10 times
416 */
417 for (i = 0; i < 10; ++i)
418 writew(0xff, uap->port.membase + UART011_MIS);
419 writew(0, uap->port.membase + uap->lcrh_tx);
420 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 writew(0, uap->port.membase + UART01x_DR);
422 while (readw(uap->port.membase + UART01x_FR) & UART01x_FR_BUSY)
423 barrier();
424
425 cr = UART01x_CR_UARTEN | UART011_CR_RXE | UART011_CR_TXE;
426 writew(cr, uap->port.membase + UART011_CR);
427
Russell King5063e2c2010-12-22 17:09:08 +0000428 /* Clear pending error interrupts */
429 writew(UART011_OEIS | UART011_BEIS | UART011_PEIS | UART011_FEIS,
430 uap->port.membase + UART011_ICR);
431
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 /*
433 * initialise the old status of the modem signals
434 */
435 uap->old_status = readw(uap->port.membase + UART01x_FR) & UART01x_FR_MODEM_ANY;
436
437 /*
438 * Finally, enable interrupts
439 */
440 spin_lock_irq(&uap->port.lock);
441 uap->im = UART011_RXIM | UART011_RTIM;
442 writew(uap->im, uap->port.membase + UART011_IMSC);
443 spin_unlock_irq(&uap->port.lock);
444
445 return 0;
446
447 clk_dis:
448 clk_disable(uap->clk);
449 out:
450 return retval;
451}
452
Linus Walleijec489aa2010-06-02 08:13:52 +0100453static void pl011_shutdown_channel(struct uart_amba_port *uap,
454 unsigned int lcrh)
455{
456 unsigned long val;
457
458 val = readw(uap->port.membase + lcrh);
459 val &= ~(UART01x_LCRH_BRK | UART01x_LCRH_FEN);
460 writew(val, uap->port.membase + lcrh);
461}
462
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463static void pl011_shutdown(struct uart_port *port)
464{
465 struct uart_amba_port *uap = (struct uart_amba_port *)port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
467 /*
468 * disable all interrupts
469 */
470 spin_lock_irq(&uap->port.lock);
471 uap->im = 0;
472 writew(uap->im, uap->port.membase + UART011_IMSC);
473 writew(0xffff, uap->port.membase + UART011_ICR);
474 spin_unlock_irq(&uap->port.lock);
475
476 /*
477 * Free the interrupt
478 */
479 free_irq(uap->port.irq, uap);
480
481 /*
482 * disable the port
483 */
Rabin Vincent3b438162010-02-12 06:43:11 +0100484 uap->autorts = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 writew(UART01x_CR_UARTEN | UART011_CR_TXE, uap->port.membase + UART011_CR);
486
487 /*
488 * disable break condition and fifos
489 */
Linus Walleijec489aa2010-06-02 08:13:52 +0100490 pl011_shutdown_channel(uap, uap->lcrh_rx);
491 if (uap->lcrh_rx != uap->lcrh_tx)
492 pl011_shutdown_channel(uap, uap->lcrh_tx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
494 /*
495 * Shut down the clock producer
496 */
497 clk_disable(uap->clk);
498}
499
500static void
Alan Cox606d0992006-12-08 02:38:45 -0800501pl011_set_termios(struct uart_port *port, struct ktermios *termios,
502 struct ktermios *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503{
Rabin Vincent3b438162010-02-12 06:43:11 +0100504 struct uart_amba_port *uap = (struct uart_amba_port *)port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 unsigned int lcr_h, old_cr;
506 unsigned long flags;
Russell Kingc19f12b2010-12-22 17:48:26 +0000507 unsigned int baud, quot, clkdiv;
508
509 if (uap->vendor->oversampling)
510 clkdiv = 8;
511 else
512 clkdiv = 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
514 /*
515 * Ask the core to calculate the divisor for us.
516 */
Linus Walleijac3e3fb2010-06-02 20:40:22 +0100517 baud = uart_get_baud_rate(port, termios, old, 0,
Russell Kingc19f12b2010-12-22 17:48:26 +0000518 port->uartclk / clkdiv);
Linus Walleijac3e3fb2010-06-02 20:40:22 +0100519
520 if (baud > port->uartclk/16)
521 quot = DIV_ROUND_CLOSEST(port->uartclk * 8, baud);
522 else
523 quot = DIV_ROUND_CLOSEST(port->uartclk * 4, baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
525 switch (termios->c_cflag & CSIZE) {
526 case CS5:
527 lcr_h = UART01x_LCRH_WLEN_5;
528 break;
529 case CS6:
530 lcr_h = UART01x_LCRH_WLEN_6;
531 break;
532 case CS7:
533 lcr_h = UART01x_LCRH_WLEN_7;
534 break;
535 default: // CS8
536 lcr_h = UART01x_LCRH_WLEN_8;
537 break;
538 }
539 if (termios->c_cflag & CSTOPB)
540 lcr_h |= UART01x_LCRH_STP2;
541 if (termios->c_cflag & PARENB) {
542 lcr_h |= UART01x_LCRH_PEN;
543 if (!(termios->c_cflag & PARODD))
544 lcr_h |= UART01x_LCRH_EPS;
545 }
Russell Kingffca2b12010-12-22 17:13:05 +0000546 if (uap->fifosize > 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 lcr_h |= UART01x_LCRH_FEN;
548
549 spin_lock_irqsave(&port->lock, flags);
550
551 /*
552 * Update the per-port timeout.
553 */
554 uart_update_timeout(port, termios->c_cflag, baud);
555
Russell Kingb63d4f02005-11-19 11:10:35 +0000556 port->read_status_mask = UART011_DR_OE | 255;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 if (termios->c_iflag & INPCK)
Russell Kingb63d4f02005-11-19 11:10:35 +0000558 port->read_status_mask |= UART011_DR_FE | UART011_DR_PE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 if (termios->c_iflag & (BRKINT | PARMRK))
Russell Kingb63d4f02005-11-19 11:10:35 +0000560 port->read_status_mask |= UART011_DR_BE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
562 /*
563 * Characters to ignore
564 */
565 port->ignore_status_mask = 0;
566 if (termios->c_iflag & IGNPAR)
Russell Kingb63d4f02005-11-19 11:10:35 +0000567 port->ignore_status_mask |= UART011_DR_FE | UART011_DR_PE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 if (termios->c_iflag & IGNBRK) {
Russell Kingb63d4f02005-11-19 11:10:35 +0000569 port->ignore_status_mask |= UART011_DR_BE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 /*
571 * If we're ignoring parity and break indicators,
572 * ignore overruns too (for real raw support).
573 */
574 if (termios->c_iflag & IGNPAR)
Russell Kingb63d4f02005-11-19 11:10:35 +0000575 port->ignore_status_mask |= UART011_DR_OE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 }
577
578 /*
579 * Ignore all characters if CREAD is not set.
580 */
581 if ((termios->c_cflag & CREAD) == 0)
Russell Kingb63d4f02005-11-19 11:10:35 +0000582 port->ignore_status_mask |= UART_DUMMY_DR_RX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
584 if (UART_ENABLE_MS(port, termios->c_cflag))
585 pl011_enable_ms(port);
586
587 /* first, disable everything */
588 old_cr = readw(port->membase + UART011_CR);
589 writew(0, port->membase + UART011_CR);
590
Rabin Vincent3b438162010-02-12 06:43:11 +0100591 if (termios->c_cflag & CRTSCTS) {
592 if (old_cr & UART011_CR_RTS)
593 old_cr |= UART011_CR_RTSEN;
594
595 old_cr |= UART011_CR_CTSEN;
596 uap->autorts = true;
597 } else {
598 old_cr &= ~(UART011_CR_CTSEN | UART011_CR_RTSEN);
599 uap->autorts = false;
600 }
601
Russell Kingc19f12b2010-12-22 17:48:26 +0000602 if (uap->vendor->oversampling) {
603 if (baud > port->uartclk / 16)
Linus Walleijac3e3fb2010-06-02 20:40:22 +0100604 old_cr |= ST_UART011_CR_OVSFACT;
605 else
606 old_cr &= ~ST_UART011_CR_OVSFACT;
607 }
608
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 /* Set baud rate */
610 writew(quot & 0x3f, port->membase + UART011_FBRD);
611 writew(quot >> 6, port->membase + UART011_IBRD);
612
613 /*
614 * ----------v----------v----------v----------v-----
615 * NOTE: MUST BE WRITTEN AFTER UARTLCR_M & UARTLCR_L
616 * ----------^----------^----------^----------^-----
617 */
Linus Walleijec489aa2010-06-02 08:13:52 +0100618 writew(lcr_h, port->membase + uap->lcrh_rx);
619 if (uap->lcrh_rx != uap->lcrh_tx) {
620 int i;
621 /*
622 * Wait 10 PCLKs before writing LCRH_TX register,
623 * to get this delay write read only register 10 times
624 */
625 for (i = 0; i < 10; ++i)
626 writew(0xff, uap->port.membase + UART011_MIS);
627 writew(lcr_h, port->membase + uap->lcrh_tx);
628 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 writew(old_cr, port->membase + UART011_CR);
630
631 spin_unlock_irqrestore(&port->lock, flags);
632}
633
634static const char *pl011_type(struct uart_port *port)
635{
Russell Kinge8a7ba82010-12-28 09:16:54 +0000636 struct uart_amba_port *uap = (struct uart_amba_port *)port;
637 return uap->port.type == PORT_AMBA ? uap->type : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638}
639
640/*
641 * Release the memory region(s) being used by 'port'
642 */
643static void pl010_release_port(struct uart_port *port)
644{
645 release_mem_region(port->mapbase, SZ_4K);
646}
647
648/*
649 * Request the memory region(s) being used by 'port'
650 */
651static int pl010_request_port(struct uart_port *port)
652{
653 return request_mem_region(port->mapbase, SZ_4K, "uart-pl011")
654 != NULL ? 0 : -EBUSY;
655}
656
657/*
658 * Configure/autoconfigure the port.
659 */
660static void pl010_config_port(struct uart_port *port, int flags)
661{
662 if (flags & UART_CONFIG_TYPE) {
663 port->type = PORT_AMBA;
664 pl010_request_port(port);
665 }
666}
667
668/*
669 * verify the new serial_struct (for TIOCSSERIAL).
670 */
671static int pl010_verify_port(struct uart_port *port, struct serial_struct *ser)
672{
673 int ret = 0;
674 if (ser->type != PORT_UNKNOWN && ser->type != PORT_AMBA)
675 ret = -EINVAL;
Yinghai Lua62c4132008-08-19 20:49:55 -0700676 if (ser->irq < 0 || ser->irq >= nr_irqs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 ret = -EINVAL;
678 if (ser->baud_base < 9600)
679 ret = -EINVAL;
680 return ret;
681}
682
683static struct uart_ops amba_pl011_pops = {
684 .tx_empty = pl01x_tx_empty,
685 .set_mctrl = pl011_set_mctrl,
686 .get_mctrl = pl01x_get_mctrl,
687 .stop_tx = pl011_stop_tx,
688 .start_tx = pl011_start_tx,
689 .stop_rx = pl011_stop_rx,
690 .enable_ms = pl011_enable_ms,
691 .break_ctl = pl011_break_ctl,
692 .startup = pl011_startup,
693 .shutdown = pl011_shutdown,
694 .set_termios = pl011_set_termios,
695 .type = pl011_type,
696 .release_port = pl010_release_port,
697 .request_port = pl010_request_port,
698 .config_port = pl010_config_port,
699 .verify_port = pl010_verify_port,
Jason Wessel84b5ae12008-02-20 13:33:39 -0600700#ifdef CONFIG_CONSOLE_POLL
701 .poll_get_char = pl010_get_poll_char,
702 .poll_put_char = pl010_put_poll_char,
703#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704};
705
706static struct uart_amba_port *amba_ports[UART_NR];
707
708#ifdef CONFIG_SERIAL_AMBA_PL011_CONSOLE
709
Russell Kingd3587882006-03-20 20:00:09 +0000710static void pl011_console_putchar(struct uart_port *port, int ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711{
Russell Kingd3587882006-03-20 20:00:09 +0000712 struct uart_amba_port *uap = (struct uart_amba_port *)port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
Russell Kingd3587882006-03-20 20:00:09 +0000714 while (readw(uap->port.membase + UART01x_FR) & UART01x_FR_TXFF)
715 barrier();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 writew(ch, uap->port.membase + UART01x_DR);
717}
718
719static void
720pl011_console_write(struct console *co, const char *s, unsigned int count)
721{
722 struct uart_amba_port *uap = amba_ports[co->index];
723 unsigned int status, old_cr, new_cr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
725 clk_enable(uap->clk);
726
727 /*
728 * First save the CR then disable the interrupts
729 */
730 old_cr = readw(uap->port.membase + UART011_CR);
731 new_cr = old_cr & ~UART011_CR_CTSEN;
732 new_cr |= UART01x_CR_UARTEN | UART011_CR_TXE;
733 writew(new_cr, uap->port.membase + UART011_CR);
734
Russell Kingd3587882006-03-20 20:00:09 +0000735 uart_console_write(&uap->port, s, count, pl011_console_putchar);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
737 /*
738 * Finally, wait for transmitter to become empty
739 * and restore the TCR
740 */
741 do {
742 status = readw(uap->port.membase + UART01x_FR);
743 } while (status & UART01x_FR_BUSY);
744 writew(old_cr, uap->port.membase + UART011_CR);
745
746 clk_disable(uap->clk);
747}
748
749static void __init
750pl011_console_get_options(struct uart_amba_port *uap, int *baud,
751 int *parity, int *bits)
752{
753 if (readw(uap->port.membase + UART011_CR) & UART01x_CR_UARTEN) {
754 unsigned int lcr_h, ibrd, fbrd;
755
Linus Walleijec489aa2010-06-02 08:13:52 +0100756 lcr_h = readw(uap->port.membase + uap->lcrh_tx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
758 *parity = 'n';
759 if (lcr_h & UART01x_LCRH_PEN) {
760 if (lcr_h & UART01x_LCRH_EPS)
761 *parity = 'e';
762 else
763 *parity = 'o';
764 }
765
766 if ((lcr_h & 0x60) == UART01x_LCRH_WLEN_7)
767 *bits = 7;
768 else
769 *bits = 8;
770
771 ibrd = readw(uap->port.membase + UART011_IBRD);
772 fbrd = readw(uap->port.membase + UART011_FBRD);
773
774 *baud = uap->port.uartclk * 4 / (64 * ibrd + fbrd);
Linus Walleijac3e3fb2010-06-02 20:40:22 +0100775
Russell Kingc19f12b2010-12-22 17:48:26 +0000776 if (uap->vendor->oversampling) {
Linus Walleijac3e3fb2010-06-02 20:40:22 +0100777 if (readw(uap->port.membase + UART011_CR)
778 & ST_UART011_CR_OVSFACT)
779 *baud *= 2;
780 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 }
782}
783
784static int __init pl011_console_setup(struct console *co, char *options)
785{
786 struct uart_amba_port *uap;
787 int baud = 38400;
788 int bits = 8;
789 int parity = 'n';
790 int flow = 'n';
791
792 /*
793 * Check whether an invalid uart number has been specified, and
794 * if so, search for the first available port that does have
795 * console support.
796 */
797 if (co->index >= UART_NR)
798 co->index = 0;
799 uap = amba_ports[co->index];
Russell Kingd28122a2007-01-22 18:59:42 +0000800 if (!uap)
801 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802
803 uap->port.uartclk = clk_get_rate(uap->clk);
804
805 if (options)
806 uart_parse_options(options, &baud, &parity, &bits, &flow);
807 else
808 pl011_console_get_options(uap, &baud, &parity, &bits);
809
810 return uart_set_options(&uap->port, co, baud, parity, bits, flow);
811}
812
Vincent Sanders2d934862005-09-14 22:36:03 +0100813static struct uart_driver amba_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814static struct console amba_console = {
815 .name = "ttyAMA",
816 .write = pl011_console_write,
817 .device = uart_console_device,
818 .setup = pl011_console_setup,
819 .flags = CON_PRINTBUFFER,
820 .index = -1,
821 .data = &amba_reg,
822};
823
824#define AMBA_CONSOLE (&amba_console)
825#else
826#define AMBA_CONSOLE NULL
827#endif
828
829static struct uart_driver amba_reg = {
830 .owner = THIS_MODULE,
831 .driver_name = "ttyAMA",
832 .dev_name = "ttyAMA",
833 .major = SERIAL_AMBA_MAJOR,
834 .minor = SERIAL_AMBA_MINOR,
835 .nr = UART_NR,
836 .cons = AMBA_CONSOLE,
837};
838
Alessandro Rubini03fbdb12009-05-20 22:39:08 +0100839static int pl011_probe(struct amba_device *dev, struct amba_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840{
841 struct uart_amba_port *uap;
Alessandro Rubini5926a292009-06-04 17:43:04 +0100842 struct vendor_data *vendor = id->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 void __iomem *base;
844 int i, ret;
845
846 for (i = 0; i < ARRAY_SIZE(amba_ports); i++)
847 if (amba_ports[i] == NULL)
848 break;
849
850 if (i == ARRAY_SIZE(amba_ports)) {
851 ret = -EBUSY;
852 goto out;
853 }
854
Yoann Padioleaudd00cc42007-07-19 01:49:03 -0700855 uap = kzalloc(sizeof(struct uart_amba_port), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 if (uap == NULL) {
857 ret = -ENOMEM;
858 goto out;
859 }
860
Linus Walleijdc890c22009-06-07 23:27:31 +0100861 base = ioremap(dev->res.start, resource_size(&dev->res));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 if (!base) {
863 ret = -ENOMEM;
864 goto free;
865 }
866
Russell Kingee569c42008-11-30 17:38:14 +0000867 uap->clk = clk_get(&dev->dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 if (IS_ERR(uap->clk)) {
869 ret = PTR_ERR(uap->clk);
870 goto unmap;
871 }
872
Russell Kingc19f12b2010-12-22 17:48:26 +0000873 uap->vendor = vendor;
Linus Walleijec489aa2010-06-02 08:13:52 +0100874 uap->lcrh_rx = vendor->lcrh_rx;
875 uap->lcrh_tx = vendor->lcrh_tx;
Russell Kingffca2b12010-12-22 17:13:05 +0000876 uap->fifosize = vendor->fifosize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 uap->port.dev = &dev->dev;
878 uap->port.mapbase = dev->res.start;
879 uap->port.membase = base;
880 uap->port.iotype = UPIO_MEM;
881 uap->port.irq = dev->irq[0];
Russell Kingffca2b12010-12-22 17:13:05 +0000882 uap->port.fifosize = uap->fifosize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 uap->port.ops = &amba_pl011_pops;
884 uap->port.flags = UPF_BOOT_AUTOCONF;
885 uap->port.line = i;
886
Russell Kinge8a7ba82010-12-28 09:16:54 +0000887 snprintf(uap->type, sizeof(uap->type), "PL011 rev%u", amba_rev(dev));
888
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 amba_ports[i] = uap;
890
891 amba_set_drvdata(dev, uap);
892 ret = uart_add_one_port(&amba_reg, &uap->port);
893 if (ret) {
894 amba_set_drvdata(dev, NULL);
895 amba_ports[i] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 clk_put(uap->clk);
897 unmap:
898 iounmap(base);
899 free:
900 kfree(uap);
901 }
902 out:
903 return ret;
904}
905
906static int pl011_remove(struct amba_device *dev)
907{
908 struct uart_amba_port *uap = amba_get_drvdata(dev);
909 int i;
910
911 amba_set_drvdata(dev, NULL);
912
913 uart_remove_one_port(&amba_reg, &uap->port);
914
915 for (i = 0; i < ARRAY_SIZE(amba_ports); i++)
916 if (amba_ports[i] == uap)
917 amba_ports[i] = NULL;
918
919 iounmap(uap->port.membase);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 clk_put(uap->clk);
921 kfree(uap);
922 return 0;
923}
924
Leo Chenb736b892009-07-28 23:43:33 +0100925#ifdef CONFIG_PM
926static int pl011_suspend(struct amba_device *dev, pm_message_t state)
927{
928 struct uart_amba_port *uap = amba_get_drvdata(dev);
929
930 if (!uap)
931 return -EINVAL;
932
933 return uart_suspend_port(&amba_reg, &uap->port);
934}
935
936static int pl011_resume(struct amba_device *dev)
937{
938 struct uart_amba_port *uap = amba_get_drvdata(dev);
939
940 if (!uap)
941 return -EINVAL;
942
943 return uart_resume_port(&amba_reg, &uap->port);
944}
945#endif
946
Russell King2c39c9e2010-07-27 08:50:16 +0100947static struct amba_id pl011_ids[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 {
949 .id = 0x00041011,
950 .mask = 0x000fffff,
Alessandro Rubini5926a292009-06-04 17:43:04 +0100951 .data = &vendor_arm,
952 },
953 {
954 .id = 0x00380802,
955 .mask = 0x00ffffff,
956 .data = &vendor_st,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 },
958 { 0, 0 },
959};
960
961static struct amba_driver pl011_driver = {
962 .drv = {
963 .name = "uart-pl011",
964 },
965 .id_table = pl011_ids,
966 .probe = pl011_probe,
967 .remove = pl011_remove,
Leo Chenb736b892009-07-28 23:43:33 +0100968#ifdef CONFIG_PM
969 .suspend = pl011_suspend,
970 .resume = pl011_resume,
971#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972};
973
974static int __init pl011_init(void)
975{
976 int ret;
977 printk(KERN_INFO "Serial: AMBA PL011 UART driver\n");
978
979 ret = uart_register_driver(&amba_reg);
980 if (ret == 0) {
981 ret = amba_driver_register(&pl011_driver);
982 if (ret)
983 uart_unregister_driver(&amba_reg);
984 }
985 return ret;
986}
987
988static void __exit pl011_exit(void)
989{
990 amba_driver_unregister(&pl011_driver);
991 uart_unregister_driver(&amba_reg);
992}
993
Alessandro Rubini4dd9e742009-05-05 05:54:13 +0100994/*
995 * While this can be a module, if builtin it's most likely the console
996 * So let's leave module_exit but move module_init to an earlier place
997 */
998arch_initcall(pl011_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999module_exit(pl011_exit);
1000
1001MODULE_AUTHOR("ARM Ltd/Deep Blue Solutions Ltd");
1002MODULE_DESCRIPTION("ARM AMBA serial port driver");
1003MODULE_LICENSE("GPL");