blob: f9dcb5379b92c5cda3ff32a9463bcae1283ef3b6 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Driver for AMBA serial ports
3 *
4 * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
5 *
6 * Copyright 1999 ARM Limited
7 * Copyright (C) 2000 Deep Blue Solutions Ltd.
Russell King68b65f72010-12-22 17:24:39 +00008 * Copyright (C) 2010 ST-Ericsson SA
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 * This is a generic driver for ARM AMBA-type serial ports. They
25 * have a lot of 16550-like features, but are not register compatible.
26 * Note that although they do have CTS, DCD and DSR inputs, they do
27 * not have an RI input, nor do they have DTR or RTS outputs. If
28 * required, these have to be supplied via some other means (eg, GPIO)
29 * and hooked into this driver.
30 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
32#if defined(CONFIG_SERIAL_AMBA_PL011_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
33#define SUPPORT_SYSRQ
34#endif
35
36#include <linux/module.h>
37#include <linux/ioport.h>
38#include <linux/init.h>
39#include <linux/console.h>
40#include <linux/sysrq.h>
41#include <linux/device.h>
42#include <linux/tty.h>
43#include <linux/tty_flip.h>
44#include <linux/serial_core.h>
45#include <linux/serial.h>
Russell Kinga62c80e2006-01-07 13:52:45 +000046#include <linux/amba/bus.h>
47#include <linux/amba/serial.h>
Russell Kingf8ce2542006-01-07 16:15:52 +000048#include <linux/clk.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090049#include <linux/slab.h>
Russell King68b65f72010-12-22 17:24:39 +000050#include <linux/dmaengine.h>
51#include <linux/dma-mapping.h>
52#include <linux/scatterlist.h>
Shreshtha Kumar Sahuc16d51a2011-06-13 10:11:33 +020053#include <linux/delay.h>
Viresh Kumar258aea72012-02-01 16:12:19 +053054#include <linux/types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
56#include <asm/io.h>
Russell Kingc6b8fda2005-10-28 14:05:16 +010057#include <asm/sizes.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
59#define UART_NR 14
60
61#define SERIAL_AMBA_MAJOR 204
62#define SERIAL_AMBA_MINOR 64
63#define SERIAL_AMBA_NR UART_NR
64
65#define AMBA_ISR_PASS_LIMIT 256
66
Russell Kingb63d4f02005-11-19 11:10:35 +000067#define UART_DR_ERROR (UART011_DR_OE|UART011_DR_BE|UART011_DR_PE|UART011_DR_FE)
68#define UART_DUMMY_DR_RX (1 << 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Shreshtha Kumar Sahuc16d51a2011-06-13 10:11:33 +020070
71#define UART_WA_SAVE_NR 14
72
73static void pl011_lockup_wa(unsigned long data);
74static const u32 uart_wa_reg[UART_WA_SAVE_NR] = {
75 ST_UART011_DMAWM,
76 ST_UART011_TIMEOUT,
77 ST_UART011_LCRH_RX,
78 UART011_IBRD,
79 UART011_FBRD,
80 ST_UART011_LCRH_TX,
81 UART011_IFLS,
82 ST_UART011_XFCR,
83 ST_UART011_XON1,
84 ST_UART011_XON2,
85 ST_UART011_XOFF1,
86 ST_UART011_XOFF2,
87 UART011_CR,
88 UART011_IMSC
89};
90
91static u32 uart_wa_regdata[UART_WA_SAVE_NR];
92static DECLARE_TASKLET(pl011_lockup_tlet, pl011_lockup_wa, 0);
93
Alessandro Rubini5926a292009-06-04 17:43:04 +010094/* There is by now at least one vendor with differing details, so handle it */
95struct vendor_data {
96 unsigned int ifls;
97 unsigned int fifosize;
Linus Walleijec489aa2010-06-02 08:13:52 +010098 unsigned int lcrh_tx;
99 unsigned int lcrh_rx;
Linus Walleijac3e3fb2010-06-02 20:40:22 +0100100 bool oversampling;
Shreshtha Kumar Sahuc16d51a2011-06-13 10:11:33 +0200101 bool interrupt_may_hang; /* vendor-specific */
Russell King38d62432010-12-22 17:59:16 +0000102 bool dma_threshold;
Alessandro Rubini5926a292009-06-04 17:43:04 +0100103};
104
105static struct vendor_data vendor_arm = {
106 .ifls = UART011_IFLS_RX4_8|UART011_IFLS_TX4_8,
107 .fifosize = 16,
Linus Walleijec489aa2010-06-02 08:13:52 +0100108 .lcrh_tx = UART011_LCRH,
109 .lcrh_rx = UART011_LCRH,
Linus Walleijac3e3fb2010-06-02 20:40:22 +0100110 .oversampling = false,
Russell King38d62432010-12-22 17:59:16 +0000111 .dma_threshold = false,
Alessandro Rubini5926a292009-06-04 17:43:04 +0100112};
113
114static struct vendor_data vendor_st = {
115 .ifls = UART011_IFLS_RX_HALF|UART011_IFLS_TX_HALF,
116 .fifosize = 64,
Linus Walleijec489aa2010-06-02 08:13:52 +0100117 .lcrh_tx = ST_UART011_LCRH_TX,
118 .lcrh_rx = ST_UART011_LCRH_RX,
Linus Walleijac3e3fb2010-06-02 20:40:22 +0100119 .oversampling = true,
Shreshtha Kumar Sahuc16d51a2011-06-13 10:11:33 +0200120 .interrupt_may_hang = true,
Russell King38d62432010-12-22 17:59:16 +0000121 .dma_threshold = true,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122};
123
Shreshtha Kumar Sahuc16d51a2011-06-13 10:11:33 +0200124static struct uart_amba_port *amba_ports[UART_NR];
125
Russell King68b65f72010-12-22 17:24:39 +0000126/* Deals with DMA transactions */
Linus Walleijead76f32011-02-24 13:21:08 +0100127
128struct pl011_sgbuf {
129 struct scatterlist sg;
130 char *buf;
131};
132
133struct pl011_dmarx_data {
134 struct dma_chan *chan;
135 struct completion complete;
136 bool use_buf_b;
137 struct pl011_sgbuf sgbuf_a;
138 struct pl011_sgbuf sgbuf_b;
139 dma_cookie_t cookie;
140 bool running;
141};
142
Russell King68b65f72010-12-22 17:24:39 +0000143struct pl011_dmatx_data {
144 struct dma_chan *chan;
145 struct scatterlist sg;
146 char *buf;
147 bool queued;
148};
149
Russell Kingc19f12b2010-12-22 17:48:26 +0000150/*
151 * We wrap our port structure around the generic uart_port.
152 */
153struct uart_amba_port {
154 struct uart_port port;
155 struct clk *clk;
156 const struct vendor_data *vendor;
Russell King68b65f72010-12-22 17:24:39 +0000157 unsigned int dmacr; /* dma control reg */
Russell Kingc19f12b2010-12-22 17:48:26 +0000158 unsigned int im; /* interrupt mask */
159 unsigned int old_status;
Russell Kingffca2b12010-12-22 17:13:05 +0000160 unsigned int fifosize; /* vendor-specific */
Russell Kingc19f12b2010-12-22 17:48:26 +0000161 unsigned int lcrh_tx; /* vendor-specific */
162 unsigned int lcrh_rx; /* vendor-specific */
163 bool autorts;
164 char type[12];
Shreshtha Kumar Sahuc16d51a2011-06-13 10:11:33 +0200165 bool interrupt_may_hang; /* vendor-specific */
Russell King68b65f72010-12-22 17:24:39 +0000166#ifdef CONFIG_DMA_ENGINE
167 /* DMA stuff */
Linus Walleijead76f32011-02-24 13:21:08 +0100168 bool using_tx_dma;
169 bool using_rx_dma;
170 struct pl011_dmarx_data dmarx;
Russell King68b65f72010-12-22 17:24:39 +0000171 struct pl011_dmatx_data dmatx;
172#endif
Russell Kingc19f12b2010-12-22 17:48:26 +0000173};
174
Russell King68b65f72010-12-22 17:24:39 +0000175/*
Linus Walleij29772c42011-02-24 13:21:36 +0100176 * Reads up to 256 characters from the FIFO or until it's empty and
177 * inserts them into the TTY layer. Returns the number of characters
178 * read from the FIFO.
179 */
180static int pl011_fifo_to_tty(struct uart_amba_port *uap)
181{
182 u16 status, ch;
183 unsigned int flag, max_count = 256;
184 int fifotaken = 0;
185
186 while (max_count--) {
187 status = readw(uap->port.membase + UART01x_FR);
188 if (status & UART01x_FR_RXFE)
189 break;
190
191 /* Take chars from the FIFO and update status */
192 ch = readw(uap->port.membase + UART01x_DR) |
193 UART_DUMMY_DR_RX;
194 flag = TTY_NORMAL;
195 uap->port.icount.rx++;
196 fifotaken++;
197
198 if (unlikely(ch & UART_DR_ERROR)) {
199 if (ch & UART011_DR_BE) {
200 ch &= ~(UART011_DR_FE | UART011_DR_PE);
201 uap->port.icount.brk++;
202 if (uart_handle_break(&uap->port))
203 continue;
204 } else if (ch & UART011_DR_PE)
205 uap->port.icount.parity++;
206 else if (ch & UART011_DR_FE)
207 uap->port.icount.frame++;
208 if (ch & UART011_DR_OE)
209 uap->port.icount.overrun++;
210
211 ch &= uap->port.read_status_mask;
212
213 if (ch & UART011_DR_BE)
214 flag = TTY_BREAK;
215 else if (ch & UART011_DR_PE)
216 flag = TTY_PARITY;
217 else if (ch & UART011_DR_FE)
218 flag = TTY_FRAME;
219 }
220
221 if (uart_handle_sysrq_char(&uap->port, ch & 255))
222 continue;
223
224 uart_insert_char(&uap->port, ch, UART011_DR_OE, ch, flag);
225 }
226
227 return fifotaken;
228}
229
230
231/*
Russell King68b65f72010-12-22 17:24:39 +0000232 * All the DMA operation mode stuff goes inside this ifdef.
233 * This assumes that you have a generic DMA device interface,
234 * no custom DMA interfaces are supported.
235 */
236#ifdef CONFIG_DMA_ENGINE
237
238#define PL011_DMA_BUFFER_SIZE PAGE_SIZE
239
Linus Walleijead76f32011-02-24 13:21:08 +0100240static int pl011_sgbuf_init(struct dma_chan *chan, struct pl011_sgbuf *sg,
241 enum dma_data_direction dir)
242{
243 sg->buf = kmalloc(PL011_DMA_BUFFER_SIZE, GFP_KERNEL);
244 if (!sg->buf)
245 return -ENOMEM;
246
247 sg_init_one(&sg->sg, sg->buf, PL011_DMA_BUFFER_SIZE);
248
249 if (dma_map_sg(chan->device->dev, &sg->sg, 1, dir) != 1) {
250 kfree(sg->buf);
251 return -EINVAL;
252 }
253 return 0;
254}
255
256static void pl011_sgbuf_free(struct dma_chan *chan, struct pl011_sgbuf *sg,
257 enum dma_data_direction dir)
258{
259 if (sg->buf) {
260 dma_unmap_sg(chan->device->dev, &sg->sg, 1, dir);
261 kfree(sg->buf);
262 }
263}
264
Russell King68b65f72010-12-22 17:24:39 +0000265static void pl011_dma_probe_initcall(struct uart_amba_port *uap)
266{
267 /* DMA is the sole user of the platform data right now */
268 struct amba_pl011_data *plat = uap->port.dev->platform_data;
269 struct dma_slave_config tx_conf = {
270 .dst_addr = uap->port.mapbase + UART01x_DR,
271 .dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE,
Vinod Koula485df42011-10-14 10:47:38 +0530272 .direction = DMA_MEM_TO_DEV,
Russell King68b65f72010-12-22 17:24:39 +0000273 .dst_maxburst = uap->fifosize >> 1,
Viresh Kumar258aea72012-02-01 16:12:19 +0530274 .device_fc = false,
Russell King68b65f72010-12-22 17:24:39 +0000275 };
276 struct dma_chan *chan;
277 dma_cap_mask_t mask;
278
279 /* We need platform data */
280 if (!plat || !plat->dma_filter) {
281 dev_info(uap->port.dev, "no DMA platform data\n");
282 return;
283 }
284
Linus Walleijead76f32011-02-24 13:21:08 +0100285 /* Try to acquire a generic DMA engine slave TX channel */
Russell King68b65f72010-12-22 17:24:39 +0000286 dma_cap_zero(mask);
287 dma_cap_set(DMA_SLAVE, mask);
288
289 chan = dma_request_channel(mask, plat->dma_filter, plat->dma_tx_param);
290 if (!chan) {
291 dev_err(uap->port.dev, "no TX DMA channel!\n");
292 return;
293 }
294
295 dmaengine_slave_config(chan, &tx_conf);
296 uap->dmatx.chan = chan;
297
298 dev_info(uap->port.dev, "DMA channel TX %s\n",
299 dma_chan_name(uap->dmatx.chan));
Linus Walleijead76f32011-02-24 13:21:08 +0100300
301 /* Optionally make use of an RX channel as well */
302 if (plat->dma_rx_param) {
303 struct dma_slave_config rx_conf = {
304 .src_addr = uap->port.mapbase + UART01x_DR,
305 .src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE,
Vinod Koula485df42011-10-14 10:47:38 +0530306 .direction = DMA_DEV_TO_MEM,
Linus Walleijead76f32011-02-24 13:21:08 +0100307 .src_maxburst = uap->fifosize >> 1,
Viresh Kumar258aea72012-02-01 16:12:19 +0530308 .device_fc = false,
Linus Walleijead76f32011-02-24 13:21:08 +0100309 };
310
311 chan = dma_request_channel(mask, plat->dma_filter, plat->dma_rx_param);
312 if (!chan) {
313 dev_err(uap->port.dev, "no RX DMA channel!\n");
314 return;
315 }
316
317 dmaengine_slave_config(chan, &rx_conf);
318 uap->dmarx.chan = chan;
319
320 dev_info(uap->port.dev, "DMA channel RX %s\n",
321 dma_chan_name(uap->dmarx.chan));
322 }
Russell King68b65f72010-12-22 17:24:39 +0000323}
324
325#ifndef MODULE
326/*
327 * Stack up the UARTs and let the above initcall be done at device
328 * initcall time, because the serial driver is called as an arch
329 * initcall, and at this time the DMA subsystem is not yet registered.
330 * At this point the driver will switch over to using DMA where desired.
331 */
332struct dma_uap {
333 struct list_head node;
334 struct uart_amba_port *uap;
335};
336
337static LIST_HEAD(pl011_dma_uarts);
338
339static int __init pl011_dma_initcall(void)
340{
341 struct list_head *node, *tmp;
342
343 list_for_each_safe(node, tmp, &pl011_dma_uarts) {
344 struct dma_uap *dmau = list_entry(node, struct dma_uap, node);
345 pl011_dma_probe_initcall(dmau->uap);
346 list_del(node);
347 kfree(dmau);
348 }
349 return 0;
350}
351
352device_initcall(pl011_dma_initcall);
353
354static void pl011_dma_probe(struct uart_amba_port *uap)
355{
356 struct dma_uap *dmau = kzalloc(sizeof(struct dma_uap), GFP_KERNEL);
357 if (dmau) {
358 dmau->uap = uap;
359 list_add_tail(&dmau->node, &pl011_dma_uarts);
360 }
361}
362#else
363static void pl011_dma_probe(struct uart_amba_port *uap)
364{
365 pl011_dma_probe_initcall(uap);
366}
367#endif
368
369static void pl011_dma_remove(struct uart_amba_port *uap)
370{
371 /* TODO: remove the initcall if it has not yet executed */
372 if (uap->dmatx.chan)
373 dma_release_channel(uap->dmatx.chan);
Linus Walleijead76f32011-02-24 13:21:08 +0100374 if (uap->dmarx.chan)
375 dma_release_channel(uap->dmarx.chan);
Russell King68b65f72010-12-22 17:24:39 +0000376}
377
Russell King68b65f72010-12-22 17:24:39 +0000378/* Forward declare this for the refill routine */
379static int pl011_dma_tx_refill(struct uart_amba_port *uap);
380
381/*
382 * The current DMA TX buffer has been sent.
383 * Try to queue up another DMA buffer.
384 */
385static void pl011_dma_tx_callback(void *data)
386{
387 struct uart_amba_port *uap = data;
388 struct pl011_dmatx_data *dmatx = &uap->dmatx;
389 unsigned long flags;
390 u16 dmacr;
391
392 spin_lock_irqsave(&uap->port.lock, flags);
393 if (uap->dmatx.queued)
394 dma_unmap_sg(dmatx->chan->device->dev, &dmatx->sg, 1,
395 DMA_TO_DEVICE);
396
397 dmacr = uap->dmacr;
398 uap->dmacr = dmacr & ~UART011_TXDMAE;
399 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
400
401 /*
402 * If TX DMA was disabled, it means that we've stopped the DMA for
403 * some reason (eg, XOFF received, or we want to send an X-char.)
404 *
405 * Note: we need to be careful here of a potential race between DMA
406 * and the rest of the driver - if the driver disables TX DMA while
407 * a TX buffer completing, we must update the tx queued status to
408 * get further refills (hence we check dmacr).
409 */
410 if (!(dmacr & UART011_TXDMAE) || uart_tx_stopped(&uap->port) ||
411 uart_circ_empty(&uap->port.state->xmit)) {
412 uap->dmatx.queued = false;
413 spin_unlock_irqrestore(&uap->port.lock, flags);
414 return;
415 }
416
417 if (pl011_dma_tx_refill(uap) <= 0) {
418 /*
419 * We didn't queue a DMA buffer for some reason, but we
420 * have data pending to be sent. Re-enable the TX IRQ.
421 */
422 uap->im |= UART011_TXIM;
423 writew(uap->im, uap->port.membase + UART011_IMSC);
424 }
425 spin_unlock_irqrestore(&uap->port.lock, flags);
426}
427
428/*
429 * Try to refill the TX DMA buffer.
430 * Locking: called with port lock held and IRQs disabled.
431 * Returns:
432 * 1 if we queued up a TX DMA buffer.
433 * 0 if we didn't want to handle this by DMA
434 * <0 on error
435 */
436static int pl011_dma_tx_refill(struct uart_amba_port *uap)
437{
438 struct pl011_dmatx_data *dmatx = &uap->dmatx;
439 struct dma_chan *chan = dmatx->chan;
440 struct dma_device *dma_dev = chan->device;
441 struct dma_async_tx_descriptor *desc;
442 struct circ_buf *xmit = &uap->port.state->xmit;
443 unsigned int count;
444
445 /*
446 * Try to avoid the overhead involved in using DMA if the
447 * transaction fits in the first half of the FIFO, by using
448 * the standard interrupt handling. This ensures that we
449 * issue a uart_write_wakeup() at the appropriate time.
450 */
451 count = uart_circ_chars_pending(xmit);
452 if (count < (uap->fifosize >> 1)) {
453 uap->dmatx.queued = false;
454 return 0;
455 }
456
457 /*
458 * Bodge: don't send the last character by DMA, as this
459 * will prevent XON from notifying us to restart DMA.
460 */
461 count -= 1;
462
463 /* Else proceed to copy the TX chars to the DMA buffer and fire DMA */
464 if (count > PL011_DMA_BUFFER_SIZE)
465 count = PL011_DMA_BUFFER_SIZE;
466
467 if (xmit->tail < xmit->head)
468 memcpy(&dmatx->buf[0], &xmit->buf[xmit->tail], count);
469 else {
470 size_t first = UART_XMIT_SIZE - xmit->tail;
471 size_t second = xmit->head;
472
473 memcpy(&dmatx->buf[0], &xmit->buf[xmit->tail], first);
474 if (second)
475 memcpy(&dmatx->buf[first], &xmit->buf[0], second);
476 }
477
478 dmatx->sg.length = count;
479
480 if (dma_map_sg(dma_dev->dev, &dmatx->sg, 1, DMA_TO_DEVICE) != 1) {
481 uap->dmatx.queued = false;
482 dev_dbg(uap->port.dev, "unable to map TX DMA\n");
483 return -EBUSY;
484 }
485
Alexandre Bounine16052822012-03-08 16:11:18 -0500486 desc = dmaengine_prep_slave_sg(chan, &dmatx->sg, 1, DMA_MEM_TO_DEV,
Russell King68b65f72010-12-22 17:24:39 +0000487 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
488 if (!desc) {
489 dma_unmap_sg(dma_dev->dev, &dmatx->sg, 1, DMA_TO_DEVICE);
490 uap->dmatx.queued = false;
491 /*
492 * If DMA cannot be used right now, we complete this
493 * transaction via IRQ and let the TTY layer retry.
494 */
495 dev_dbg(uap->port.dev, "TX DMA busy\n");
496 return -EBUSY;
497 }
498
499 /* Some data to go along to the callback */
500 desc->callback = pl011_dma_tx_callback;
501 desc->callback_param = uap;
502
503 /* All errors should happen at prepare time */
504 dmaengine_submit(desc);
505
506 /* Fire the DMA transaction */
507 dma_dev->device_issue_pending(chan);
508
509 uap->dmacr |= UART011_TXDMAE;
510 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
511 uap->dmatx.queued = true;
512
513 /*
514 * Now we know that DMA will fire, so advance the ring buffer
515 * with the stuff we just dispatched.
516 */
517 xmit->tail = (xmit->tail + count) & (UART_XMIT_SIZE - 1);
518 uap->port.icount.tx += count;
519
520 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
521 uart_write_wakeup(&uap->port);
522
523 return 1;
524}
525
526/*
527 * We received a transmit interrupt without a pending X-char but with
528 * pending characters.
529 * Locking: called with port lock held and IRQs disabled.
530 * Returns:
531 * false if we want to use PIO to transmit
532 * true if we queued a DMA buffer
533 */
534static bool pl011_dma_tx_irq(struct uart_amba_port *uap)
535{
Linus Walleijead76f32011-02-24 13:21:08 +0100536 if (!uap->using_tx_dma)
Russell King68b65f72010-12-22 17:24:39 +0000537 return false;
538
539 /*
540 * If we already have a TX buffer queued, but received a
541 * TX interrupt, it will be because we've just sent an X-char.
542 * Ensure the TX DMA is enabled and the TX IRQ is disabled.
543 */
544 if (uap->dmatx.queued) {
545 uap->dmacr |= UART011_TXDMAE;
546 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
547 uap->im &= ~UART011_TXIM;
548 writew(uap->im, uap->port.membase + UART011_IMSC);
549 return true;
550 }
551
552 /*
553 * We don't have a TX buffer queued, so try to queue one.
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300554 * If we successfully queued a buffer, mask the TX IRQ.
Russell King68b65f72010-12-22 17:24:39 +0000555 */
556 if (pl011_dma_tx_refill(uap) > 0) {
557 uap->im &= ~UART011_TXIM;
558 writew(uap->im, uap->port.membase + UART011_IMSC);
559 return true;
560 }
561 return false;
562}
563
564/*
565 * Stop the DMA transmit (eg, due to received XOFF).
566 * Locking: called with port lock held and IRQs disabled.
567 */
568static inline void pl011_dma_tx_stop(struct uart_amba_port *uap)
569{
570 if (uap->dmatx.queued) {
571 uap->dmacr &= ~UART011_TXDMAE;
572 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
573 }
574}
575
576/*
577 * Try to start a DMA transmit, or in the case of an XON/OFF
578 * character queued for send, try to get that character out ASAP.
579 * Locking: called with port lock held and IRQs disabled.
580 * Returns:
581 * false if we want the TX IRQ to be enabled
582 * true if we have a buffer queued
583 */
584static inline bool pl011_dma_tx_start(struct uart_amba_port *uap)
585{
586 u16 dmacr;
587
Linus Walleijead76f32011-02-24 13:21:08 +0100588 if (!uap->using_tx_dma)
Russell King68b65f72010-12-22 17:24:39 +0000589 return false;
590
591 if (!uap->port.x_char) {
592 /* no X-char, try to push chars out in DMA mode */
593 bool ret = true;
594
595 if (!uap->dmatx.queued) {
596 if (pl011_dma_tx_refill(uap) > 0) {
597 uap->im &= ~UART011_TXIM;
598 ret = true;
599 } else {
600 uap->im |= UART011_TXIM;
601 ret = false;
602 }
603 writew(uap->im, uap->port.membase + UART011_IMSC);
604 } else if (!(uap->dmacr & UART011_TXDMAE)) {
605 uap->dmacr |= UART011_TXDMAE;
606 writew(uap->dmacr,
607 uap->port.membase + UART011_DMACR);
608 }
609 return ret;
610 }
611
612 /*
613 * We have an X-char to send. Disable DMA to prevent it loading
614 * the TX fifo, and then see if we can stuff it into the FIFO.
615 */
616 dmacr = uap->dmacr;
617 uap->dmacr &= ~UART011_TXDMAE;
618 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
619
620 if (readw(uap->port.membase + UART01x_FR) & UART01x_FR_TXFF) {
621 /*
622 * No space in the FIFO, so enable the transmit interrupt
623 * so we know when there is space. Note that once we've
624 * loaded the character, we should just re-enable DMA.
625 */
626 return false;
627 }
628
629 writew(uap->port.x_char, uap->port.membase + UART01x_DR);
630 uap->port.icount.tx++;
631 uap->port.x_char = 0;
632
633 /* Success - restore the DMA state */
634 uap->dmacr = dmacr;
635 writew(dmacr, uap->port.membase + UART011_DMACR);
636
637 return true;
638}
639
640/*
641 * Flush the transmit buffer.
642 * Locking: called with port lock held and IRQs disabled.
643 */
644static void pl011_dma_flush_buffer(struct uart_port *port)
645{
646 struct uart_amba_port *uap = (struct uart_amba_port *)port;
647
Linus Walleijead76f32011-02-24 13:21:08 +0100648 if (!uap->using_tx_dma)
Russell King68b65f72010-12-22 17:24:39 +0000649 return;
650
651 /* Avoid deadlock with the DMA engine callback */
652 spin_unlock(&uap->port.lock);
653 dmaengine_terminate_all(uap->dmatx.chan);
654 spin_lock(&uap->port.lock);
655 if (uap->dmatx.queued) {
656 dma_unmap_sg(uap->dmatx.chan->device->dev, &uap->dmatx.sg, 1,
657 DMA_TO_DEVICE);
658 uap->dmatx.queued = false;
659 uap->dmacr &= ~UART011_TXDMAE;
660 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
661 }
662}
663
Linus Walleijead76f32011-02-24 13:21:08 +0100664static void pl011_dma_rx_callback(void *data);
665
666static int pl011_dma_rx_trigger_dma(struct uart_amba_port *uap)
667{
668 struct dma_chan *rxchan = uap->dmarx.chan;
Linus Walleijead76f32011-02-24 13:21:08 +0100669 struct pl011_dmarx_data *dmarx = &uap->dmarx;
670 struct dma_async_tx_descriptor *desc;
671 struct pl011_sgbuf *sgbuf;
672
673 if (!rxchan)
674 return -EIO;
675
676 /* Start the RX DMA job */
677 sgbuf = uap->dmarx.use_buf_b ?
678 &uap->dmarx.sgbuf_b : &uap->dmarx.sgbuf_a;
Alexandre Bounine16052822012-03-08 16:11:18 -0500679 desc = dmaengine_prep_slave_sg(rxchan, &sgbuf->sg, 1,
Vinod Koula485df42011-10-14 10:47:38 +0530680 DMA_DEV_TO_MEM,
Linus Walleijead76f32011-02-24 13:21:08 +0100681 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
682 /*
683 * If the DMA engine is busy and cannot prepare a
684 * channel, no big deal, the driver will fall back
685 * to interrupt mode as a result of this error code.
686 */
687 if (!desc) {
688 uap->dmarx.running = false;
689 dmaengine_terminate_all(rxchan);
690 return -EBUSY;
691 }
692
693 /* Some data to go along to the callback */
694 desc->callback = pl011_dma_rx_callback;
695 desc->callback_param = uap;
696 dmarx->cookie = dmaengine_submit(desc);
697 dma_async_issue_pending(rxchan);
698
699 uap->dmacr |= UART011_RXDMAE;
700 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
701 uap->dmarx.running = true;
702
703 uap->im &= ~UART011_RXIM;
704 writew(uap->im, uap->port.membase + UART011_IMSC);
705
706 return 0;
707}
708
709/*
710 * This is called when either the DMA job is complete, or
711 * the FIFO timeout interrupt occurred. This must be called
712 * with the port spinlock uap->port.lock held.
713 */
714static void pl011_dma_rx_chars(struct uart_amba_port *uap,
715 u32 pending, bool use_buf_b,
716 bool readfifo)
717{
718 struct tty_struct *tty = uap->port.state->port.tty;
719 struct pl011_sgbuf *sgbuf = use_buf_b ?
720 &uap->dmarx.sgbuf_b : &uap->dmarx.sgbuf_a;
721 struct device *dev = uap->dmarx.chan->device->dev;
Linus Walleijead76f32011-02-24 13:21:08 +0100722 int dma_count = 0;
723 u32 fifotaken = 0; /* only used for vdbg() */
724
725 /* Pick everything from the DMA first */
726 if (pending) {
727 /* Sync in buffer */
728 dma_sync_sg_for_cpu(dev, &sgbuf->sg, 1, DMA_FROM_DEVICE);
729
730 /*
731 * First take all chars in the DMA pipe, then look in the FIFO.
732 * Note that tty_insert_flip_buf() tries to take as many chars
733 * as it can.
734 */
735 dma_count = tty_insert_flip_string(uap->port.state->port.tty,
736 sgbuf->buf, pending);
737
738 /* Return buffer to device */
739 dma_sync_sg_for_device(dev, &sgbuf->sg, 1, DMA_FROM_DEVICE);
740
741 uap->port.icount.rx += dma_count;
742 if (dma_count < pending)
743 dev_warn(uap->port.dev,
744 "couldn't insert all characters (TTY is full?)\n");
745 }
746
747 /*
748 * Only continue with trying to read the FIFO if all DMA chars have
749 * been taken first.
750 */
751 if (dma_count == pending && readfifo) {
752 /* Clear any error flags */
753 writew(UART011_OEIS | UART011_BEIS | UART011_PEIS | UART011_FEIS,
754 uap->port.membase + UART011_ICR);
755
756 /*
757 * If we read all the DMA'd characters, and we had an
Linus Walleij29772c42011-02-24 13:21:36 +0100758 * incomplete buffer, that could be due to an rx error, or
759 * maybe we just timed out. Read any pending chars and check
760 * the error status.
761 *
762 * Error conditions will only occur in the FIFO, these will
763 * trigger an immediate interrupt and stop the DMA job, so we
764 * will always find the error in the FIFO, never in the DMA
765 * buffer.
Linus Walleijead76f32011-02-24 13:21:08 +0100766 */
Linus Walleij29772c42011-02-24 13:21:36 +0100767 fifotaken = pl011_fifo_to_tty(uap);
Linus Walleijead76f32011-02-24 13:21:08 +0100768 }
769
770 spin_unlock(&uap->port.lock);
771 dev_vdbg(uap->port.dev,
772 "Took %d chars from DMA buffer and %d chars from the FIFO\n",
773 dma_count, fifotaken);
774 tty_flip_buffer_push(tty);
775 spin_lock(&uap->port.lock);
776}
777
778static void pl011_dma_rx_irq(struct uart_amba_port *uap)
779{
780 struct pl011_dmarx_data *dmarx = &uap->dmarx;
781 struct dma_chan *rxchan = dmarx->chan;
782 struct pl011_sgbuf *sgbuf = dmarx->use_buf_b ?
783 &dmarx->sgbuf_b : &dmarx->sgbuf_a;
784 size_t pending;
785 struct dma_tx_state state;
786 enum dma_status dmastat;
787
788 /*
789 * Pause the transfer so we can trust the current counter,
790 * do this before we pause the PL011 block, else we may
791 * overflow the FIFO.
792 */
793 if (dmaengine_pause(rxchan))
794 dev_err(uap->port.dev, "unable to pause DMA transfer\n");
795 dmastat = rxchan->device->device_tx_status(rxchan,
796 dmarx->cookie, &state);
797 if (dmastat != DMA_PAUSED)
798 dev_err(uap->port.dev, "unable to pause DMA transfer\n");
799
800 /* Disable RX DMA - incoming data will wait in the FIFO */
801 uap->dmacr &= ~UART011_RXDMAE;
802 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
803 uap->dmarx.running = false;
804
805 pending = sgbuf->sg.length - state.residue;
806 BUG_ON(pending > PL011_DMA_BUFFER_SIZE);
807 /* Then we terminate the transfer - we now know our residue */
808 dmaengine_terminate_all(rxchan);
809
810 /*
811 * This will take the chars we have so far and insert
812 * into the framework.
813 */
814 pl011_dma_rx_chars(uap, pending, dmarx->use_buf_b, true);
815
816 /* Switch buffer & re-trigger DMA job */
817 dmarx->use_buf_b = !dmarx->use_buf_b;
818 if (pl011_dma_rx_trigger_dma(uap)) {
819 dev_dbg(uap->port.dev, "could not retrigger RX DMA job "
820 "fall back to interrupt mode\n");
821 uap->im |= UART011_RXIM;
822 writew(uap->im, uap->port.membase + UART011_IMSC);
823 }
824}
825
826static void pl011_dma_rx_callback(void *data)
827{
828 struct uart_amba_port *uap = data;
829 struct pl011_dmarx_data *dmarx = &uap->dmarx;
830 bool lastbuf = dmarx->use_buf_b;
831 int ret;
832
833 /*
834 * This completion interrupt occurs typically when the
835 * RX buffer is totally stuffed but no timeout has yet
836 * occurred. When that happens, we just want the RX
837 * routine to flush out the secondary DMA buffer while
838 * we immediately trigger the next DMA job.
839 */
840 spin_lock_irq(&uap->port.lock);
841 uap->dmarx.running = false;
842 dmarx->use_buf_b = !lastbuf;
843 ret = pl011_dma_rx_trigger_dma(uap);
844
845 pl011_dma_rx_chars(uap, PL011_DMA_BUFFER_SIZE, lastbuf, false);
846 spin_unlock_irq(&uap->port.lock);
847 /*
848 * Do this check after we picked the DMA chars so we don't
849 * get some IRQ immediately from RX.
850 */
851 if (ret) {
852 dev_dbg(uap->port.dev, "could not retrigger RX DMA job "
853 "fall back to interrupt mode\n");
854 uap->im |= UART011_RXIM;
855 writew(uap->im, uap->port.membase + UART011_IMSC);
856 }
857}
858
859/*
860 * Stop accepting received characters, when we're shutting down or
861 * suspending this port.
862 * Locking: called with port lock held and IRQs disabled.
863 */
864static inline void pl011_dma_rx_stop(struct uart_amba_port *uap)
865{
866 /* FIXME. Just disable the DMA enable */
867 uap->dmacr &= ~UART011_RXDMAE;
868 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
869}
Russell King68b65f72010-12-22 17:24:39 +0000870
871static void pl011_dma_startup(struct uart_amba_port *uap)
872{
Linus Walleijead76f32011-02-24 13:21:08 +0100873 int ret;
874
Russell King68b65f72010-12-22 17:24:39 +0000875 if (!uap->dmatx.chan)
876 return;
877
878 uap->dmatx.buf = kmalloc(PL011_DMA_BUFFER_SIZE, GFP_KERNEL);
879 if (!uap->dmatx.buf) {
880 dev_err(uap->port.dev, "no memory for DMA TX buffer\n");
881 uap->port.fifosize = uap->fifosize;
882 return;
883 }
884
885 sg_init_one(&uap->dmatx.sg, uap->dmatx.buf, PL011_DMA_BUFFER_SIZE);
886
887 /* The DMA buffer is now the FIFO the TTY subsystem can use */
888 uap->port.fifosize = PL011_DMA_BUFFER_SIZE;
Linus Walleijead76f32011-02-24 13:21:08 +0100889 uap->using_tx_dma = true;
Russell King68b65f72010-12-22 17:24:39 +0000890
Linus Walleijead76f32011-02-24 13:21:08 +0100891 if (!uap->dmarx.chan)
892 goto skip_rx;
893
894 /* Allocate and map DMA RX buffers */
895 ret = pl011_sgbuf_init(uap->dmarx.chan, &uap->dmarx.sgbuf_a,
896 DMA_FROM_DEVICE);
897 if (ret) {
898 dev_err(uap->port.dev, "failed to init DMA %s: %d\n",
899 "RX buffer A", ret);
900 goto skip_rx;
901 }
902
903 ret = pl011_sgbuf_init(uap->dmarx.chan, &uap->dmarx.sgbuf_b,
904 DMA_FROM_DEVICE);
905 if (ret) {
906 dev_err(uap->port.dev, "failed to init DMA %s: %d\n",
907 "RX buffer B", ret);
908 pl011_sgbuf_free(uap->dmarx.chan, &uap->dmarx.sgbuf_a,
909 DMA_FROM_DEVICE);
910 goto skip_rx;
911 }
912
913 uap->using_rx_dma = true;
914
915skip_rx:
Russell King68b65f72010-12-22 17:24:39 +0000916 /* Turn on DMA error (RX/TX will be enabled on demand) */
917 uap->dmacr |= UART011_DMAONERR;
918 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
Russell King38d62432010-12-22 17:59:16 +0000919
920 /*
921 * ST Micro variants has some specific dma burst threshold
922 * compensation. Set this to 16 bytes, so burst will only
923 * be issued above/below 16 bytes.
924 */
925 if (uap->vendor->dma_threshold)
926 writew(ST_UART011_DMAWM_RX_16 | ST_UART011_DMAWM_TX_16,
927 uap->port.membase + ST_UART011_DMAWM);
Linus Walleijead76f32011-02-24 13:21:08 +0100928
929 if (uap->using_rx_dma) {
930 if (pl011_dma_rx_trigger_dma(uap))
931 dev_dbg(uap->port.dev, "could not trigger initial "
932 "RX DMA job, fall back to interrupt mode\n");
933 }
Russell King68b65f72010-12-22 17:24:39 +0000934}
935
936static void pl011_dma_shutdown(struct uart_amba_port *uap)
937{
Linus Walleijead76f32011-02-24 13:21:08 +0100938 if (!(uap->using_tx_dma || uap->using_rx_dma))
Russell King68b65f72010-12-22 17:24:39 +0000939 return;
940
941 /* Disable RX and TX DMA */
942 while (readw(uap->port.membase + UART01x_FR) & UART01x_FR_BUSY)
943 barrier();
944
945 spin_lock_irq(&uap->port.lock);
946 uap->dmacr &= ~(UART011_DMAONERR | UART011_RXDMAE | UART011_TXDMAE);
947 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
948 spin_unlock_irq(&uap->port.lock);
949
Linus Walleijead76f32011-02-24 13:21:08 +0100950 if (uap->using_tx_dma) {
951 /* In theory, this should already be done by pl011_dma_flush_buffer */
952 dmaengine_terminate_all(uap->dmatx.chan);
953 if (uap->dmatx.queued) {
954 dma_unmap_sg(uap->dmatx.chan->device->dev, &uap->dmatx.sg, 1,
955 DMA_TO_DEVICE);
956 uap->dmatx.queued = false;
957 }
958
959 kfree(uap->dmatx.buf);
960 uap->using_tx_dma = false;
Russell King68b65f72010-12-22 17:24:39 +0000961 }
962
Linus Walleijead76f32011-02-24 13:21:08 +0100963 if (uap->using_rx_dma) {
964 dmaengine_terminate_all(uap->dmarx.chan);
965 /* Clean up the RX DMA */
966 pl011_sgbuf_free(uap->dmarx.chan, &uap->dmarx.sgbuf_a, DMA_FROM_DEVICE);
967 pl011_sgbuf_free(uap->dmarx.chan, &uap->dmarx.sgbuf_b, DMA_FROM_DEVICE);
968 uap->using_rx_dma = false;
969 }
Russell King68b65f72010-12-22 17:24:39 +0000970}
971
Linus Walleijead76f32011-02-24 13:21:08 +0100972static inline bool pl011_dma_rx_available(struct uart_amba_port *uap)
973{
974 return uap->using_rx_dma;
975}
976
977static inline bool pl011_dma_rx_running(struct uart_amba_port *uap)
978{
979 return uap->using_rx_dma && uap->dmarx.running;
980}
981
982
Russell King68b65f72010-12-22 17:24:39 +0000983#else
984/* Blank functions if the DMA engine is not available */
985static inline void pl011_dma_probe(struct uart_amba_port *uap)
986{
987}
988
989static inline void pl011_dma_remove(struct uart_amba_port *uap)
990{
991}
992
993static inline void pl011_dma_startup(struct uart_amba_port *uap)
994{
995}
996
997static inline void pl011_dma_shutdown(struct uart_amba_port *uap)
998{
999}
1000
1001static inline bool pl011_dma_tx_irq(struct uart_amba_port *uap)
1002{
1003 return false;
1004}
1005
1006static inline void pl011_dma_tx_stop(struct uart_amba_port *uap)
1007{
1008}
1009
1010static inline bool pl011_dma_tx_start(struct uart_amba_port *uap)
1011{
1012 return false;
1013}
1014
Linus Walleijead76f32011-02-24 13:21:08 +01001015static inline void pl011_dma_rx_irq(struct uart_amba_port *uap)
1016{
1017}
1018
1019static inline void pl011_dma_rx_stop(struct uart_amba_port *uap)
1020{
1021}
1022
1023static inline int pl011_dma_rx_trigger_dma(struct uart_amba_port *uap)
1024{
1025 return -EIO;
1026}
1027
1028static inline bool pl011_dma_rx_available(struct uart_amba_port *uap)
1029{
1030 return false;
1031}
1032
1033static inline bool pl011_dma_rx_running(struct uart_amba_port *uap)
1034{
1035 return false;
1036}
1037
Russell King68b65f72010-12-22 17:24:39 +00001038#define pl011_dma_flush_buffer NULL
1039#endif
1040
1041
Shreshtha Kumar Sahuc16d51a2011-06-13 10:11:33 +02001042/*
1043 * pl011_lockup_wa
1044 * This workaround aims to break the deadlock situation
1045 * when after long transfer over uart in hardware flow
1046 * control, uart interrupt registers cannot be cleared.
1047 * Hence uart transfer gets blocked.
1048 *
1049 * It is seen that during such deadlock condition ICR
1050 * don't get cleared even on multiple write. This leads
1051 * pass_counter to decrease and finally reach zero. This
1052 * can be taken as trigger point to run this UART_BT_WA.
1053 *
1054 */
1055static void pl011_lockup_wa(unsigned long data)
1056{
1057 struct uart_amba_port *uap = amba_ports[0];
1058 void __iomem *base = uap->port.membase;
1059 struct circ_buf *xmit = &uap->port.state->xmit;
1060 struct tty_struct *tty = uap->port.state->port.tty;
1061 int buf_empty_retries = 200;
1062 int loop;
1063
1064 /* Stop HCI layer from submitting data for tx */
1065 tty->hw_stopped = 1;
1066 while (!uart_circ_empty(xmit)) {
1067 if (buf_empty_retries-- == 0)
1068 break;
1069 udelay(100);
1070 }
1071
1072 /* Backup registers */
1073 for (loop = 0; loop < UART_WA_SAVE_NR; loop++)
1074 uart_wa_regdata[loop] = readl(base + uart_wa_reg[loop]);
1075
1076 /* Disable UART so that FIFO data is flushed out */
1077 writew(0x00, uap->port.membase + UART011_CR);
1078
1079 /* Soft reset UART module */
1080 if (uap->port.dev->platform_data) {
1081 struct amba_pl011_data *plat;
1082
1083 plat = uap->port.dev->platform_data;
1084 if (plat->reset)
1085 plat->reset();
1086 }
1087
1088 /* Restore registers */
1089 for (loop = 0; loop < UART_WA_SAVE_NR; loop++)
1090 writew(uart_wa_regdata[loop] ,
1091 uap->port.membase + uart_wa_reg[loop]);
1092
1093 /* Initialise the old status of the modem signals */
1094 uap->old_status = readw(uap->port.membase + UART01x_FR) &
1095 UART01x_FR_MODEM_ANY;
1096
1097 if (readl(base + UART011_MIS) & 0x2)
1098 printk(KERN_EMERG "UART_BT_WA: ***FAILED***\n");
1099
1100 /* Start Tx/Rx */
1101 tty->hw_stopped = 0;
1102}
1103
Russell Kingb129a8c2005-08-31 10:12:14 +01001104static void pl011_stop_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105{
1106 struct uart_amba_port *uap = (struct uart_amba_port *)port;
1107
1108 uap->im &= ~UART011_TXIM;
1109 writew(uap->im, uap->port.membase + UART011_IMSC);
Russell King68b65f72010-12-22 17:24:39 +00001110 pl011_dma_tx_stop(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111}
1112
Russell Kingb129a8c2005-08-31 10:12:14 +01001113static void pl011_start_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114{
1115 struct uart_amba_port *uap = (struct uart_amba_port *)port;
1116
Russell King68b65f72010-12-22 17:24:39 +00001117 if (!pl011_dma_tx_start(uap)) {
1118 uap->im |= UART011_TXIM;
1119 writew(uap->im, uap->port.membase + UART011_IMSC);
1120 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121}
1122
1123static void pl011_stop_rx(struct uart_port *port)
1124{
1125 struct uart_amba_port *uap = (struct uart_amba_port *)port;
1126
1127 uap->im &= ~(UART011_RXIM|UART011_RTIM|UART011_FEIM|
1128 UART011_PEIM|UART011_BEIM|UART011_OEIM);
1129 writew(uap->im, uap->port.membase + UART011_IMSC);
Linus Walleijead76f32011-02-24 13:21:08 +01001130
1131 pl011_dma_rx_stop(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132}
1133
1134static void pl011_enable_ms(struct uart_port *port)
1135{
1136 struct uart_amba_port *uap = (struct uart_amba_port *)port;
1137
1138 uap->im |= UART011_RIMIM|UART011_CTSMIM|UART011_DCDMIM|UART011_DSRMIM;
1139 writew(uap->im, uap->port.membase + UART011_IMSC);
1140}
1141
David Howells7d12e782006-10-05 14:55:46 +01001142static void pl011_rx_chars(struct uart_amba_port *uap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143{
Alan Coxebd2c8f2009-09-19 13:13:28 -07001144 struct tty_struct *tty = uap->port.state->port.tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145
Linus Walleij29772c42011-02-24 13:21:36 +01001146 pl011_fifo_to_tty(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147
Thomas Gleixner2389b272007-05-29 21:53:50 +01001148 spin_unlock(&uap->port.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 tty_flip_buffer_push(tty);
Linus Walleijead76f32011-02-24 13:21:08 +01001150 /*
1151 * If we were temporarily out of DMA mode for a while,
1152 * attempt to switch back to DMA mode again.
1153 */
1154 if (pl011_dma_rx_available(uap)) {
1155 if (pl011_dma_rx_trigger_dma(uap)) {
1156 dev_dbg(uap->port.dev, "could not trigger RX DMA job "
1157 "fall back to interrupt mode again\n");
1158 uap->im |= UART011_RXIM;
1159 } else
1160 uap->im &= ~UART011_RXIM;
1161 writew(uap->im, uap->port.membase + UART011_IMSC);
1162 }
Thomas Gleixner2389b272007-05-29 21:53:50 +01001163 spin_lock(&uap->port.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164}
1165
1166static void pl011_tx_chars(struct uart_amba_port *uap)
1167{
Alan Coxebd2c8f2009-09-19 13:13:28 -07001168 struct circ_buf *xmit = &uap->port.state->xmit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 int count;
1170
1171 if (uap->port.x_char) {
1172 writew(uap->port.x_char, uap->port.membase + UART01x_DR);
1173 uap->port.icount.tx++;
1174 uap->port.x_char = 0;
1175 return;
1176 }
1177 if (uart_circ_empty(xmit) || uart_tx_stopped(&uap->port)) {
Russell Kingb129a8c2005-08-31 10:12:14 +01001178 pl011_stop_tx(&uap->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 return;
1180 }
1181
Russell King68b65f72010-12-22 17:24:39 +00001182 /* If we are using DMA mode, try to send some characters. */
1183 if (pl011_dma_tx_irq(uap))
1184 return;
1185
Russell Kingffca2b12010-12-22 17:13:05 +00001186 count = uap->fifosize >> 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 do {
1188 writew(xmit->buf[xmit->tail], uap->port.membase + UART01x_DR);
1189 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
1190 uap->port.icount.tx++;
1191 if (uart_circ_empty(xmit))
1192 break;
1193 } while (--count > 0);
1194
1195 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
1196 uart_write_wakeup(&uap->port);
1197
1198 if (uart_circ_empty(xmit))
Russell Kingb129a8c2005-08-31 10:12:14 +01001199 pl011_stop_tx(&uap->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200}
1201
1202static void pl011_modem_status(struct uart_amba_port *uap)
1203{
1204 unsigned int status, delta;
1205
1206 status = readw(uap->port.membase + UART01x_FR) & UART01x_FR_MODEM_ANY;
1207
1208 delta = status ^ uap->old_status;
1209 uap->old_status = status;
1210
1211 if (!delta)
1212 return;
1213
1214 if (delta & UART01x_FR_DCD)
1215 uart_handle_dcd_change(&uap->port, status & UART01x_FR_DCD);
1216
1217 if (delta & UART01x_FR_DSR)
1218 uap->port.icount.dsr++;
1219
1220 if (delta & UART01x_FR_CTS)
1221 uart_handle_cts_change(&uap->port, status & UART01x_FR_CTS);
1222
Alan Coxbdc04e32009-09-19 13:13:31 -07001223 wake_up_interruptible(&uap->port.state->port.delta_msr_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224}
1225
David Howells7d12e782006-10-05 14:55:46 +01001226static irqreturn_t pl011_int(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227{
1228 struct uart_amba_port *uap = dev_id;
Russell King963cc982010-12-22 17:16:09 +00001229 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 unsigned int status, pass_counter = AMBA_ISR_PASS_LIMIT;
1231 int handled = 0;
1232
Russell King963cc982010-12-22 17:16:09 +00001233 spin_lock_irqsave(&uap->port.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234
1235 status = readw(uap->port.membase + UART011_MIS);
1236 if (status) {
1237 do {
1238 writew(status & ~(UART011_TXIS|UART011_RTIS|
1239 UART011_RXIS),
1240 uap->port.membase + UART011_ICR);
1241
Linus Walleijead76f32011-02-24 13:21:08 +01001242 if (status & (UART011_RTIS|UART011_RXIS)) {
1243 if (pl011_dma_rx_running(uap))
1244 pl011_dma_rx_irq(uap);
1245 else
1246 pl011_rx_chars(uap);
1247 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 if (status & (UART011_DSRMIS|UART011_DCDMIS|
1249 UART011_CTSMIS|UART011_RIMIS))
1250 pl011_modem_status(uap);
1251 if (status & UART011_TXIS)
1252 pl011_tx_chars(uap);
1253
Shreshtha Kumar Sahuc16d51a2011-06-13 10:11:33 +02001254 if (pass_counter-- == 0) {
1255 if (uap->interrupt_may_hang)
1256 tasklet_schedule(&pl011_lockup_tlet);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 break;
Shreshtha Kumar Sahuc16d51a2011-06-13 10:11:33 +02001258 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259
1260 status = readw(uap->port.membase + UART011_MIS);
1261 } while (status != 0);
1262 handled = 1;
1263 }
1264
Russell King963cc982010-12-22 17:16:09 +00001265 spin_unlock_irqrestore(&uap->port.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266
1267 return IRQ_RETVAL(handled);
1268}
1269
1270static unsigned int pl01x_tx_empty(struct uart_port *port)
1271{
1272 struct uart_amba_port *uap = (struct uart_amba_port *)port;
1273 unsigned int status = readw(uap->port.membase + UART01x_FR);
1274 return status & (UART01x_FR_BUSY|UART01x_FR_TXFF) ? 0 : TIOCSER_TEMT;
1275}
1276
1277static unsigned int pl01x_get_mctrl(struct uart_port *port)
1278{
1279 struct uart_amba_port *uap = (struct uart_amba_port *)port;
1280 unsigned int result = 0;
1281 unsigned int status = readw(uap->port.membase + UART01x_FR);
1282
Jiri Slaby5159f402007-10-18 23:40:31 -07001283#define TIOCMBIT(uartbit, tiocmbit) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 if (status & uartbit) \
1285 result |= tiocmbit
1286
Jiri Slaby5159f402007-10-18 23:40:31 -07001287 TIOCMBIT(UART01x_FR_DCD, TIOCM_CAR);
1288 TIOCMBIT(UART01x_FR_DSR, TIOCM_DSR);
1289 TIOCMBIT(UART01x_FR_CTS, TIOCM_CTS);
1290 TIOCMBIT(UART011_FR_RI, TIOCM_RNG);
1291#undef TIOCMBIT
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 return result;
1293}
1294
1295static void pl011_set_mctrl(struct uart_port *port, unsigned int mctrl)
1296{
1297 struct uart_amba_port *uap = (struct uart_amba_port *)port;
1298 unsigned int cr;
1299
1300 cr = readw(uap->port.membase + UART011_CR);
1301
Jiri Slaby5159f402007-10-18 23:40:31 -07001302#define TIOCMBIT(tiocmbit, uartbit) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 if (mctrl & tiocmbit) \
1304 cr |= uartbit; \
1305 else \
1306 cr &= ~uartbit
1307
Jiri Slaby5159f402007-10-18 23:40:31 -07001308 TIOCMBIT(TIOCM_RTS, UART011_CR_RTS);
1309 TIOCMBIT(TIOCM_DTR, UART011_CR_DTR);
1310 TIOCMBIT(TIOCM_OUT1, UART011_CR_OUT1);
1311 TIOCMBIT(TIOCM_OUT2, UART011_CR_OUT2);
1312 TIOCMBIT(TIOCM_LOOP, UART011_CR_LBE);
Rabin Vincent3b438162010-02-12 06:43:11 +01001313
1314 if (uap->autorts) {
1315 /* We need to disable auto-RTS if we want to turn RTS off */
1316 TIOCMBIT(TIOCM_RTS, UART011_CR_RTSEN);
1317 }
Jiri Slaby5159f402007-10-18 23:40:31 -07001318#undef TIOCMBIT
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319
1320 writew(cr, uap->port.membase + UART011_CR);
1321}
1322
1323static void pl011_break_ctl(struct uart_port *port, int break_state)
1324{
1325 struct uart_amba_port *uap = (struct uart_amba_port *)port;
1326 unsigned long flags;
1327 unsigned int lcr_h;
1328
1329 spin_lock_irqsave(&uap->port.lock, flags);
Linus Walleijec489aa2010-06-02 08:13:52 +01001330 lcr_h = readw(uap->port.membase + uap->lcrh_tx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 if (break_state == -1)
1332 lcr_h |= UART01x_LCRH_BRK;
1333 else
1334 lcr_h &= ~UART01x_LCRH_BRK;
Linus Walleijec489aa2010-06-02 08:13:52 +01001335 writew(lcr_h, uap->port.membase + uap->lcrh_tx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 spin_unlock_irqrestore(&uap->port.lock, flags);
1337}
1338
Jason Wessel84b5ae12008-02-20 13:33:39 -06001339#ifdef CONFIG_CONSOLE_POLL
1340static int pl010_get_poll_char(struct uart_port *port)
1341{
1342 struct uart_amba_port *uap = (struct uart_amba_port *)port;
1343 unsigned int status;
1344
Jason Wesself5316b42010-05-20 21:04:22 -05001345 status = readw(uap->port.membase + UART01x_FR);
1346 if (status & UART01x_FR_RXFE)
1347 return NO_POLL_CHAR;
Jason Wessel84b5ae12008-02-20 13:33:39 -06001348
1349 return readw(uap->port.membase + UART01x_DR);
1350}
1351
1352static void pl010_put_poll_char(struct uart_port *port,
1353 unsigned char ch)
1354{
1355 struct uart_amba_port *uap = (struct uart_amba_port *)port;
1356
1357 while (readw(uap->port.membase + UART01x_FR) & UART01x_FR_TXFF)
1358 barrier();
1359
1360 writew(ch, uap->port.membase + UART01x_DR);
1361}
1362
1363#endif /* CONFIG_CONSOLE_POLL */
1364
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365static int pl011_startup(struct uart_port *port)
1366{
1367 struct uart_amba_port *uap = (struct uart_amba_port *)port;
1368 unsigned int cr;
1369 int retval;
1370
Russell King4b4851c2011-09-22 11:35:30 +01001371 retval = clk_prepare(uap->clk);
1372 if (retval)
1373 goto out;
1374
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 /*
1376 * Try to enable the clock producer.
1377 */
1378 retval = clk_enable(uap->clk);
1379 if (retval)
Russell King4b4851c2011-09-22 11:35:30 +01001380 goto clk_unprep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381
1382 uap->port.uartclk = clk_get_rate(uap->clk);
1383
1384 /*
1385 * Allocate the IRQ
1386 */
1387 retval = request_irq(uap->port.irq, pl011_int, 0, "uart-pl011", uap);
1388 if (retval)
1389 goto clk_dis;
1390
Russell Kingc19f12b2010-12-22 17:48:26 +00001391 writew(uap->vendor->ifls, uap->port.membase + UART011_IFLS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392
1393 /*
1394 * Provoke TX FIFO interrupt into asserting.
1395 */
1396 cr = UART01x_CR_UARTEN | UART011_CR_TXE | UART011_CR_LBE;
1397 writew(cr, uap->port.membase + UART011_CR);
1398 writew(0, uap->port.membase + UART011_FBRD);
1399 writew(1, uap->port.membase + UART011_IBRD);
Linus Walleijec489aa2010-06-02 08:13:52 +01001400 writew(0, uap->port.membase + uap->lcrh_rx);
1401 if (uap->lcrh_tx != uap->lcrh_rx) {
1402 int i;
1403 /*
1404 * Wait 10 PCLKs before writing LCRH_TX register,
1405 * to get this delay write read only register 10 times
1406 */
1407 for (i = 0; i < 10; ++i)
1408 writew(0xff, uap->port.membase + UART011_MIS);
1409 writew(0, uap->port.membase + uap->lcrh_tx);
1410 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 writew(0, uap->port.membase + UART01x_DR);
1412 while (readw(uap->port.membase + UART01x_FR) & UART01x_FR_BUSY)
1413 barrier();
1414
1415 cr = UART01x_CR_UARTEN | UART011_CR_RXE | UART011_CR_TXE;
1416 writew(cr, uap->port.membase + UART011_CR);
1417
Russell King5063e2c2010-12-22 17:09:08 +00001418 /* Clear pending error interrupts */
1419 writew(UART011_OEIS | UART011_BEIS | UART011_PEIS | UART011_FEIS,
1420 uap->port.membase + UART011_ICR);
1421
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 /*
1423 * initialise the old status of the modem signals
1424 */
1425 uap->old_status = readw(uap->port.membase + UART01x_FR) & UART01x_FR_MODEM_ANY;
1426
Russell King68b65f72010-12-22 17:24:39 +00001427 /* Startup DMA */
1428 pl011_dma_startup(uap);
1429
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 /*
Linus Walleijead76f32011-02-24 13:21:08 +01001431 * Finally, enable interrupts, only timeouts when using DMA
1432 * if initial RX DMA job failed, start in interrupt mode
1433 * as well.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 */
1435 spin_lock_irq(&uap->port.lock);
Linus Walleijead76f32011-02-24 13:21:08 +01001436 uap->im = UART011_RTIM;
1437 if (!pl011_dma_rx_running(uap))
1438 uap->im |= UART011_RXIM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 writew(uap->im, uap->port.membase + UART011_IMSC);
1440 spin_unlock_irq(&uap->port.lock);
1441
Shreshtha Kumar Sahuc16d51a2011-06-13 10:11:33 +02001442 if (uap->port.dev->platform_data) {
1443 struct amba_pl011_data *plat;
1444
1445 plat = uap->port.dev->platform_data;
1446 if (plat->init)
1447 plat->init();
1448 }
1449
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 return 0;
1451
1452 clk_dis:
1453 clk_disable(uap->clk);
Russell King4b4851c2011-09-22 11:35:30 +01001454 clk_unprep:
1455 clk_unprepare(uap->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 out:
1457 return retval;
1458}
1459
Linus Walleijec489aa2010-06-02 08:13:52 +01001460static void pl011_shutdown_channel(struct uart_amba_port *uap,
1461 unsigned int lcrh)
1462{
1463 unsigned long val;
1464
1465 val = readw(uap->port.membase + lcrh);
1466 val &= ~(UART01x_LCRH_BRK | UART01x_LCRH_FEN);
1467 writew(val, uap->port.membase + lcrh);
1468}
1469
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470static void pl011_shutdown(struct uart_port *port)
1471{
1472 struct uart_amba_port *uap = (struct uart_amba_port *)port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473
1474 /*
1475 * disable all interrupts
1476 */
1477 spin_lock_irq(&uap->port.lock);
1478 uap->im = 0;
1479 writew(uap->im, uap->port.membase + UART011_IMSC);
1480 writew(0xffff, uap->port.membase + UART011_ICR);
1481 spin_unlock_irq(&uap->port.lock);
1482
Russell King68b65f72010-12-22 17:24:39 +00001483 pl011_dma_shutdown(uap);
1484
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 /*
1486 * Free the interrupt
1487 */
1488 free_irq(uap->port.irq, uap);
1489
1490 /*
1491 * disable the port
1492 */
Rabin Vincent3b438162010-02-12 06:43:11 +01001493 uap->autorts = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 writew(UART01x_CR_UARTEN | UART011_CR_TXE, uap->port.membase + UART011_CR);
1495
1496 /*
1497 * disable break condition and fifos
1498 */
Linus Walleijec489aa2010-06-02 08:13:52 +01001499 pl011_shutdown_channel(uap, uap->lcrh_rx);
1500 if (uap->lcrh_rx != uap->lcrh_tx)
1501 pl011_shutdown_channel(uap, uap->lcrh_tx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502
1503 /*
1504 * Shut down the clock producer
1505 */
1506 clk_disable(uap->clk);
Russell King4b4851c2011-09-22 11:35:30 +01001507 clk_unprepare(uap->clk);
Shreshtha Kumar Sahuc16d51a2011-06-13 10:11:33 +02001508
1509 if (uap->port.dev->platform_data) {
1510 struct amba_pl011_data *plat;
1511
1512 plat = uap->port.dev->platform_data;
1513 if (plat->exit)
1514 plat->exit();
1515 }
1516
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517}
1518
1519static void
Alan Cox606d0992006-12-08 02:38:45 -08001520pl011_set_termios(struct uart_port *port, struct ktermios *termios,
1521 struct ktermios *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522{
Rabin Vincent3b438162010-02-12 06:43:11 +01001523 struct uart_amba_port *uap = (struct uart_amba_port *)port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 unsigned int lcr_h, old_cr;
1525 unsigned long flags;
Russell Kingc19f12b2010-12-22 17:48:26 +00001526 unsigned int baud, quot, clkdiv;
1527
1528 if (uap->vendor->oversampling)
1529 clkdiv = 8;
1530 else
1531 clkdiv = 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532
1533 /*
1534 * Ask the core to calculate the divisor for us.
1535 */
Linus Walleijac3e3fb2010-06-02 20:40:22 +01001536 baud = uart_get_baud_rate(port, termios, old, 0,
Russell Kingc19f12b2010-12-22 17:48:26 +00001537 port->uartclk / clkdiv);
Linus Walleijac3e3fb2010-06-02 20:40:22 +01001538
1539 if (baud > port->uartclk/16)
1540 quot = DIV_ROUND_CLOSEST(port->uartclk * 8, baud);
1541 else
1542 quot = DIV_ROUND_CLOSEST(port->uartclk * 4, baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543
1544 switch (termios->c_cflag & CSIZE) {
1545 case CS5:
1546 lcr_h = UART01x_LCRH_WLEN_5;
1547 break;
1548 case CS6:
1549 lcr_h = UART01x_LCRH_WLEN_6;
1550 break;
1551 case CS7:
1552 lcr_h = UART01x_LCRH_WLEN_7;
1553 break;
1554 default: // CS8
1555 lcr_h = UART01x_LCRH_WLEN_8;
1556 break;
1557 }
1558 if (termios->c_cflag & CSTOPB)
1559 lcr_h |= UART01x_LCRH_STP2;
1560 if (termios->c_cflag & PARENB) {
1561 lcr_h |= UART01x_LCRH_PEN;
1562 if (!(termios->c_cflag & PARODD))
1563 lcr_h |= UART01x_LCRH_EPS;
1564 }
Russell Kingffca2b12010-12-22 17:13:05 +00001565 if (uap->fifosize > 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 lcr_h |= UART01x_LCRH_FEN;
1567
1568 spin_lock_irqsave(&port->lock, flags);
1569
1570 /*
1571 * Update the per-port timeout.
1572 */
1573 uart_update_timeout(port, termios->c_cflag, baud);
1574
Russell Kingb63d4f02005-11-19 11:10:35 +00001575 port->read_status_mask = UART011_DR_OE | 255;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 if (termios->c_iflag & INPCK)
Russell Kingb63d4f02005-11-19 11:10:35 +00001577 port->read_status_mask |= UART011_DR_FE | UART011_DR_PE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 if (termios->c_iflag & (BRKINT | PARMRK))
Russell Kingb63d4f02005-11-19 11:10:35 +00001579 port->read_status_mask |= UART011_DR_BE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580
1581 /*
1582 * Characters to ignore
1583 */
1584 port->ignore_status_mask = 0;
1585 if (termios->c_iflag & IGNPAR)
Russell Kingb63d4f02005-11-19 11:10:35 +00001586 port->ignore_status_mask |= UART011_DR_FE | UART011_DR_PE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 if (termios->c_iflag & IGNBRK) {
Russell Kingb63d4f02005-11-19 11:10:35 +00001588 port->ignore_status_mask |= UART011_DR_BE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 /*
1590 * If we're ignoring parity and break indicators,
1591 * ignore overruns too (for real raw support).
1592 */
1593 if (termios->c_iflag & IGNPAR)
Russell Kingb63d4f02005-11-19 11:10:35 +00001594 port->ignore_status_mask |= UART011_DR_OE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 }
1596
1597 /*
1598 * Ignore all characters if CREAD is not set.
1599 */
1600 if ((termios->c_cflag & CREAD) == 0)
Russell Kingb63d4f02005-11-19 11:10:35 +00001601 port->ignore_status_mask |= UART_DUMMY_DR_RX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602
1603 if (UART_ENABLE_MS(port, termios->c_cflag))
1604 pl011_enable_ms(port);
1605
1606 /* first, disable everything */
1607 old_cr = readw(port->membase + UART011_CR);
1608 writew(0, port->membase + UART011_CR);
1609
Rabin Vincent3b438162010-02-12 06:43:11 +01001610 if (termios->c_cflag & CRTSCTS) {
1611 if (old_cr & UART011_CR_RTS)
1612 old_cr |= UART011_CR_RTSEN;
1613
1614 old_cr |= UART011_CR_CTSEN;
1615 uap->autorts = true;
1616 } else {
1617 old_cr &= ~(UART011_CR_CTSEN | UART011_CR_RTSEN);
1618 uap->autorts = false;
1619 }
1620
Russell Kingc19f12b2010-12-22 17:48:26 +00001621 if (uap->vendor->oversampling) {
1622 if (baud > port->uartclk / 16)
Linus Walleijac3e3fb2010-06-02 20:40:22 +01001623 old_cr |= ST_UART011_CR_OVSFACT;
1624 else
1625 old_cr &= ~ST_UART011_CR_OVSFACT;
1626 }
1627
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 /* Set baud rate */
1629 writew(quot & 0x3f, port->membase + UART011_FBRD);
1630 writew(quot >> 6, port->membase + UART011_IBRD);
1631
1632 /*
1633 * ----------v----------v----------v----------v-----
1634 * NOTE: MUST BE WRITTEN AFTER UARTLCR_M & UARTLCR_L
1635 * ----------^----------^----------^----------^-----
1636 */
Linus Walleijec489aa2010-06-02 08:13:52 +01001637 writew(lcr_h, port->membase + uap->lcrh_rx);
1638 if (uap->lcrh_rx != uap->lcrh_tx) {
1639 int i;
1640 /*
1641 * Wait 10 PCLKs before writing LCRH_TX register,
1642 * to get this delay write read only register 10 times
1643 */
1644 for (i = 0; i < 10; ++i)
1645 writew(0xff, uap->port.membase + UART011_MIS);
1646 writew(lcr_h, port->membase + uap->lcrh_tx);
1647 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 writew(old_cr, port->membase + UART011_CR);
1649
1650 spin_unlock_irqrestore(&port->lock, flags);
1651}
1652
1653static const char *pl011_type(struct uart_port *port)
1654{
Russell Kinge8a7ba82010-12-28 09:16:54 +00001655 struct uart_amba_port *uap = (struct uart_amba_port *)port;
1656 return uap->port.type == PORT_AMBA ? uap->type : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657}
1658
1659/*
1660 * Release the memory region(s) being used by 'port'
1661 */
1662static void pl010_release_port(struct uart_port *port)
1663{
1664 release_mem_region(port->mapbase, SZ_4K);
1665}
1666
1667/*
1668 * Request the memory region(s) being used by 'port'
1669 */
1670static int pl010_request_port(struct uart_port *port)
1671{
1672 return request_mem_region(port->mapbase, SZ_4K, "uart-pl011")
1673 != NULL ? 0 : -EBUSY;
1674}
1675
1676/*
1677 * Configure/autoconfigure the port.
1678 */
1679static void pl010_config_port(struct uart_port *port, int flags)
1680{
1681 if (flags & UART_CONFIG_TYPE) {
1682 port->type = PORT_AMBA;
1683 pl010_request_port(port);
1684 }
1685}
1686
1687/*
1688 * verify the new serial_struct (for TIOCSSERIAL).
1689 */
1690static int pl010_verify_port(struct uart_port *port, struct serial_struct *ser)
1691{
1692 int ret = 0;
1693 if (ser->type != PORT_UNKNOWN && ser->type != PORT_AMBA)
1694 ret = -EINVAL;
Yinghai Lua62c4132008-08-19 20:49:55 -07001695 if (ser->irq < 0 || ser->irq >= nr_irqs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 ret = -EINVAL;
1697 if (ser->baud_base < 9600)
1698 ret = -EINVAL;
1699 return ret;
1700}
1701
1702static struct uart_ops amba_pl011_pops = {
1703 .tx_empty = pl01x_tx_empty,
1704 .set_mctrl = pl011_set_mctrl,
1705 .get_mctrl = pl01x_get_mctrl,
1706 .stop_tx = pl011_stop_tx,
1707 .start_tx = pl011_start_tx,
1708 .stop_rx = pl011_stop_rx,
1709 .enable_ms = pl011_enable_ms,
1710 .break_ctl = pl011_break_ctl,
1711 .startup = pl011_startup,
1712 .shutdown = pl011_shutdown,
Russell King68b65f72010-12-22 17:24:39 +00001713 .flush_buffer = pl011_dma_flush_buffer,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 .set_termios = pl011_set_termios,
1715 .type = pl011_type,
1716 .release_port = pl010_release_port,
1717 .request_port = pl010_request_port,
1718 .config_port = pl010_config_port,
1719 .verify_port = pl010_verify_port,
Jason Wessel84b5ae12008-02-20 13:33:39 -06001720#ifdef CONFIG_CONSOLE_POLL
1721 .poll_get_char = pl010_get_poll_char,
1722 .poll_put_char = pl010_put_poll_char,
1723#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724};
1725
1726static struct uart_amba_port *amba_ports[UART_NR];
1727
1728#ifdef CONFIG_SERIAL_AMBA_PL011_CONSOLE
1729
Russell Kingd3587882006-03-20 20:00:09 +00001730static void pl011_console_putchar(struct uart_port *port, int ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731{
Russell Kingd3587882006-03-20 20:00:09 +00001732 struct uart_amba_port *uap = (struct uart_amba_port *)port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733
Russell Kingd3587882006-03-20 20:00:09 +00001734 while (readw(uap->port.membase + UART01x_FR) & UART01x_FR_TXFF)
1735 barrier();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 writew(ch, uap->port.membase + UART01x_DR);
1737}
1738
1739static void
1740pl011_console_write(struct console *co, const char *s, unsigned int count)
1741{
1742 struct uart_amba_port *uap = amba_ports[co->index];
1743 unsigned int status, old_cr, new_cr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744
1745 clk_enable(uap->clk);
1746
1747 /*
1748 * First save the CR then disable the interrupts
1749 */
1750 old_cr = readw(uap->port.membase + UART011_CR);
1751 new_cr = old_cr & ~UART011_CR_CTSEN;
1752 new_cr |= UART01x_CR_UARTEN | UART011_CR_TXE;
1753 writew(new_cr, uap->port.membase + UART011_CR);
1754
Russell Kingd3587882006-03-20 20:00:09 +00001755 uart_console_write(&uap->port, s, count, pl011_console_putchar);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756
1757 /*
1758 * Finally, wait for transmitter to become empty
1759 * and restore the TCR
1760 */
1761 do {
1762 status = readw(uap->port.membase + UART01x_FR);
1763 } while (status & UART01x_FR_BUSY);
1764 writew(old_cr, uap->port.membase + UART011_CR);
1765
1766 clk_disable(uap->clk);
1767}
1768
1769static void __init
1770pl011_console_get_options(struct uart_amba_port *uap, int *baud,
1771 int *parity, int *bits)
1772{
1773 if (readw(uap->port.membase + UART011_CR) & UART01x_CR_UARTEN) {
1774 unsigned int lcr_h, ibrd, fbrd;
1775
Linus Walleijec489aa2010-06-02 08:13:52 +01001776 lcr_h = readw(uap->port.membase + uap->lcrh_tx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777
1778 *parity = 'n';
1779 if (lcr_h & UART01x_LCRH_PEN) {
1780 if (lcr_h & UART01x_LCRH_EPS)
1781 *parity = 'e';
1782 else
1783 *parity = 'o';
1784 }
1785
1786 if ((lcr_h & 0x60) == UART01x_LCRH_WLEN_7)
1787 *bits = 7;
1788 else
1789 *bits = 8;
1790
1791 ibrd = readw(uap->port.membase + UART011_IBRD);
1792 fbrd = readw(uap->port.membase + UART011_FBRD);
1793
1794 *baud = uap->port.uartclk * 4 / (64 * ibrd + fbrd);
Linus Walleijac3e3fb2010-06-02 20:40:22 +01001795
Russell Kingc19f12b2010-12-22 17:48:26 +00001796 if (uap->vendor->oversampling) {
Linus Walleijac3e3fb2010-06-02 20:40:22 +01001797 if (readw(uap->port.membase + UART011_CR)
1798 & ST_UART011_CR_OVSFACT)
1799 *baud *= 2;
1800 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 }
1802}
1803
1804static int __init pl011_console_setup(struct console *co, char *options)
1805{
1806 struct uart_amba_port *uap;
1807 int baud = 38400;
1808 int bits = 8;
1809 int parity = 'n';
1810 int flow = 'n';
Russell King4b4851c2011-09-22 11:35:30 +01001811 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812
1813 /*
1814 * Check whether an invalid uart number has been specified, and
1815 * if so, search for the first available port that does have
1816 * console support.
1817 */
1818 if (co->index >= UART_NR)
1819 co->index = 0;
1820 uap = amba_ports[co->index];
Russell Kingd28122a2007-01-22 18:59:42 +00001821 if (!uap)
1822 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823
Russell King4b4851c2011-09-22 11:35:30 +01001824 ret = clk_prepare(uap->clk);
1825 if (ret)
1826 return ret;
1827
Shreshtha Kumar Sahuc16d51a2011-06-13 10:11:33 +02001828 if (uap->port.dev->platform_data) {
1829 struct amba_pl011_data *plat;
1830
1831 plat = uap->port.dev->platform_data;
1832 if (plat->init)
1833 plat->init();
1834 }
1835
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 uap->port.uartclk = clk_get_rate(uap->clk);
1837
1838 if (options)
1839 uart_parse_options(options, &baud, &parity, &bits, &flow);
1840 else
1841 pl011_console_get_options(uap, &baud, &parity, &bits);
1842
1843 return uart_set_options(&uap->port, co, baud, parity, bits, flow);
1844}
1845
Vincent Sanders2d934862005-09-14 22:36:03 +01001846static struct uart_driver amba_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847static struct console amba_console = {
1848 .name = "ttyAMA",
1849 .write = pl011_console_write,
1850 .device = uart_console_device,
1851 .setup = pl011_console_setup,
1852 .flags = CON_PRINTBUFFER,
1853 .index = -1,
1854 .data = &amba_reg,
1855};
1856
1857#define AMBA_CONSOLE (&amba_console)
1858#else
1859#define AMBA_CONSOLE NULL
1860#endif
1861
1862static struct uart_driver amba_reg = {
1863 .owner = THIS_MODULE,
1864 .driver_name = "ttyAMA",
1865 .dev_name = "ttyAMA",
1866 .major = SERIAL_AMBA_MAJOR,
1867 .minor = SERIAL_AMBA_MINOR,
1868 .nr = UART_NR,
1869 .cons = AMBA_CONSOLE,
1870};
1871
Russell Kingaa25afa2011-02-19 15:55:00 +00001872static int pl011_probe(struct amba_device *dev, const struct amba_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873{
1874 struct uart_amba_port *uap;
Alessandro Rubini5926a292009-06-04 17:43:04 +01001875 struct vendor_data *vendor = id->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 void __iomem *base;
1877 int i, ret;
1878
1879 for (i = 0; i < ARRAY_SIZE(amba_ports); i++)
1880 if (amba_ports[i] == NULL)
1881 break;
1882
1883 if (i == ARRAY_SIZE(amba_ports)) {
1884 ret = -EBUSY;
1885 goto out;
1886 }
1887
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07001888 uap = kzalloc(sizeof(struct uart_amba_port), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 if (uap == NULL) {
1890 ret = -ENOMEM;
1891 goto out;
1892 }
1893
Linus Walleijdc890c22009-06-07 23:27:31 +01001894 base = ioremap(dev->res.start, resource_size(&dev->res));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 if (!base) {
1896 ret = -ENOMEM;
1897 goto free;
1898 }
1899
Russell Kingee569c42008-11-30 17:38:14 +00001900 uap->clk = clk_get(&dev->dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 if (IS_ERR(uap->clk)) {
1902 ret = PTR_ERR(uap->clk);
1903 goto unmap;
1904 }
1905
Russell Kingc19f12b2010-12-22 17:48:26 +00001906 uap->vendor = vendor;
Linus Walleijec489aa2010-06-02 08:13:52 +01001907 uap->lcrh_rx = vendor->lcrh_rx;
1908 uap->lcrh_tx = vendor->lcrh_tx;
Russell Kingffca2b12010-12-22 17:13:05 +00001909 uap->fifosize = vendor->fifosize;
Shreshtha Kumar Sahuc16d51a2011-06-13 10:11:33 +02001910 uap->interrupt_may_hang = vendor->interrupt_may_hang;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 uap->port.dev = &dev->dev;
1912 uap->port.mapbase = dev->res.start;
1913 uap->port.membase = base;
1914 uap->port.iotype = UPIO_MEM;
1915 uap->port.irq = dev->irq[0];
Russell Kingffca2b12010-12-22 17:13:05 +00001916 uap->port.fifosize = uap->fifosize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 uap->port.ops = &amba_pl011_pops;
1918 uap->port.flags = UPF_BOOT_AUTOCONF;
1919 uap->port.line = i;
Russell King68b65f72010-12-22 17:24:39 +00001920 pl011_dma_probe(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921
Russell Kinge8a7ba82010-12-28 09:16:54 +00001922 snprintf(uap->type, sizeof(uap->type), "PL011 rev%u", amba_rev(dev));
1923
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 amba_ports[i] = uap;
1925
1926 amba_set_drvdata(dev, uap);
1927 ret = uart_add_one_port(&amba_reg, &uap->port);
1928 if (ret) {
1929 amba_set_drvdata(dev, NULL);
1930 amba_ports[i] = NULL;
Russell King68b65f72010-12-22 17:24:39 +00001931 pl011_dma_remove(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 clk_put(uap->clk);
1933 unmap:
1934 iounmap(base);
1935 free:
1936 kfree(uap);
1937 }
1938 out:
1939 return ret;
1940}
1941
1942static int pl011_remove(struct amba_device *dev)
1943{
1944 struct uart_amba_port *uap = amba_get_drvdata(dev);
1945 int i;
1946
1947 amba_set_drvdata(dev, NULL);
1948
1949 uart_remove_one_port(&amba_reg, &uap->port);
1950
1951 for (i = 0; i < ARRAY_SIZE(amba_ports); i++)
1952 if (amba_ports[i] == uap)
1953 amba_ports[i] = NULL;
1954
Russell King68b65f72010-12-22 17:24:39 +00001955 pl011_dma_remove(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 iounmap(uap->port.membase);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 clk_put(uap->clk);
1958 kfree(uap);
1959 return 0;
1960}
1961
Leo Chenb736b892009-07-28 23:43:33 +01001962#ifdef CONFIG_PM
1963static int pl011_suspend(struct amba_device *dev, pm_message_t state)
1964{
1965 struct uart_amba_port *uap = amba_get_drvdata(dev);
1966
1967 if (!uap)
1968 return -EINVAL;
1969
1970 return uart_suspend_port(&amba_reg, &uap->port);
1971}
1972
1973static int pl011_resume(struct amba_device *dev)
1974{
1975 struct uart_amba_port *uap = amba_get_drvdata(dev);
1976
1977 if (!uap)
1978 return -EINVAL;
1979
1980 return uart_resume_port(&amba_reg, &uap->port);
1981}
1982#endif
1983
Russell King2c39c9e2010-07-27 08:50:16 +01001984static struct amba_id pl011_ids[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 {
1986 .id = 0x00041011,
1987 .mask = 0x000fffff,
Alessandro Rubini5926a292009-06-04 17:43:04 +01001988 .data = &vendor_arm,
1989 },
1990 {
1991 .id = 0x00380802,
1992 .mask = 0x00ffffff,
1993 .data = &vendor_st,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 },
1995 { 0, 0 },
1996};
1997
Dave Martin60f7a332011-10-05 15:15:22 +01001998MODULE_DEVICE_TABLE(amba, pl011_ids);
1999
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000static struct amba_driver pl011_driver = {
2001 .drv = {
2002 .name = "uart-pl011",
2003 },
2004 .id_table = pl011_ids,
2005 .probe = pl011_probe,
2006 .remove = pl011_remove,
Leo Chenb736b892009-07-28 23:43:33 +01002007#ifdef CONFIG_PM
2008 .suspend = pl011_suspend,
2009 .resume = pl011_resume,
2010#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011};
2012
2013static int __init pl011_init(void)
2014{
2015 int ret;
2016 printk(KERN_INFO "Serial: AMBA PL011 UART driver\n");
2017
2018 ret = uart_register_driver(&amba_reg);
2019 if (ret == 0) {
2020 ret = amba_driver_register(&pl011_driver);
2021 if (ret)
2022 uart_unregister_driver(&amba_reg);
2023 }
2024 return ret;
2025}
2026
2027static void __exit pl011_exit(void)
2028{
2029 amba_driver_unregister(&pl011_driver);
2030 uart_unregister_driver(&amba_reg);
2031}
2032
Alessandro Rubini4dd9e742009-05-05 05:54:13 +01002033/*
2034 * While this can be a module, if builtin it's most likely the console
2035 * So let's leave module_exit but move module_init to an earlier place
2036 */
2037arch_initcall(pl011_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038module_exit(pl011_exit);
2039
2040MODULE_AUTHOR("ARM Ltd/Deep Blue Solutions Ltd");
2041MODULE_DESCRIPTION("ARM AMBA serial port driver");
2042MODULE_LICENSE("GPL");