blob: cc3ea066c43a2293490f40011d57b6b33d043a72 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55#include <asm/io.h>
Russell Kingc6b8fda2005-10-28 14:05:16 +010056#include <asm/sizes.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
58#define UART_NR 14
59
60#define SERIAL_AMBA_MAJOR 204
61#define SERIAL_AMBA_MINOR 64
62#define SERIAL_AMBA_NR UART_NR
63
64#define AMBA_ISR_PASS_LIMIT 256
65
Russell Kingb63d4f02005-11-19 11:10:35 +000066#define UART_DR_ERROR (UART011_DR_OE|UART011_DR_BE|UART011_DR_PE|UART011_DR_FE)
67#define UART_DUMMY_DR_RX (1 << 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Shreshtha Kumar Sahuc16d51a2011-06-13 10:11:33 +020069
70#define UART_WA_SAVE_NR 14
71
72static void pl011_lockup_wa(unsigned long data);
73static const u32 uart_wa_reg[UART_WA_SAVE_NR] = {
74 ST_UART011_DMAWM,
75 ST_UART011_TIMEOUT,
76 ST_UART011_LCRH_RX,
77 UART011_IBRD,
78 UART011_FBRD,
79 ST_UART011_LCRH_TX,
80 UART011_IFLS,
81 ST_UART011_XFCR,
82 ST_UART011_XON1,
83 ST_UART011_XON2,
84 ST_UART011_XOFF1,
85 ST_UART011_XOFF2,
86 UART011_CR,
87 UART011_IMSC
88};
89
90static u32 uart_wa_regdata[UART_WA_SAVE_NR];
91static DECLARE_TASKLET(pl011_lockup_tlet, pl011_lockup_wa, 0);
92
Alessandro Rubini5926a292009-06-04 17:43:04 +010093/* There is by now at least one vendor with differing details, so handle it */
94struct vendor_data {
95 unsigned int ifls;
96 unsigned int fifosize;
Linus Walleijec489aa2010-06-02 08:13:52 +010097 unsigned int lcrh_tx;
98 unsigned int lcrh_rx;
Linus Walleijac3e3fb2010-06-02 20:40:22 +010099 bool oversampling;
Shreshtha Kumar Sahuc16d51a2011-06-13 10:11:33 +0200100 bool interrupt_may_hang; /* vendor-specific */
Russell King38d62432010-12-22 17:59:16 +0000101 bool dma_threshold;
Alessandro Rubini5926a292009-06-04 17:43:04 +0100102};
103
104static struct vendor_data vendor_arm = {
105 .ifls = UART011_IFLS_RX4_8|UART011_IFLS_TX4_8,
106 .fifosize = 16,
Linus Walleijec489aa2010-06-02 08:13:52 +0100107 .lcrh_tx = UART011_LCRH,
108 .lcrh_rx = UART011_LCRH,
Linus Walleijac3e3fb2010-06-02 20:40:22 +0100109 .oversampling = false,
Russell King38d62432010-12-22 17:59:16 +0000110 .dma_threshold = false,
Alessandro Rubini5926a292009-06-04 17:43:04 +0100111};
112
113static struct vendor_data vendor_st = {
114 .ifls = UART011_IFLS_RX_HALF|UART011_IFLS_TX_HALF,
115 .fifosize = 64,
Linus Walleijec489aa2010-06-02 08:13:52 +0100116 .lcrh_tx = ST_UART011_LCRH_TX,
117 .lcrh_rx = ST_UART011_LCRH_RX,
Linus Walleijac3e3fb2010-06-02 20:40:22 +0100118 .oversampling = true,
Shreshtha Kumar Sahuc16d51a2011-06-13 10:11:33 +0200119 .interrupt_may_hang = true,
Russell King38d62432010-12-22 17:59:16 +0000120 .dma_threshold = true,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121};
122
Shreshtha Kumar Sahuc16d51a2011-06-13 10:11:33 +0200123static struct uart_amba_port *amba_ports[UART_NR];
124
Russell King68b65f72010-12-22 17:24:39 +0000125/* Deals with DMA transactions */
Linus Walleijead76f32011-02-24 13:21:08 +0100126
127struct pl011_sgbuf {
128 struct scatterlist sg;
129 char *buf;
130};
131
132struct pl011_dmarx_data {
133 struct dma_chan *chan;
134 struct completion complete;
135 bool use_buf_b;
136 struct pl011_sgbuf sgbuf_a;
137 struct pl011_sgbuf sgbuf_b;
138 dma_cookie_t cookie;
139 bool running;
140};
141
Russell King68b65f72010-12-22 17:24:39 +0000142struct pl011_dmatx_data {
143 struct dma_chan *chan;
144 struct scatterlist sg;
145 char *buf;
146 bool queued;
147};
148
Russell Kingc19f12b2010-12-22 17:48:26 +0000149/*
150 * We wrap our port structure around the generic uart_port.
151 */
152struct uart_amba_port {
153 struct uart_port port;
154 struct clk *clk;
155 const struct vendor_data *vendor;
Russell King68b65f72010-12-22 17:24:39 +0000156 unsigned int dmacr; /* dma control reg */
Russell Kingc19f12b2010-12-22 17:48:26 +0000157 unsigned int im; /* interrupt mask */
158 unsigned int old_status;
Russell Kingffca2b12010-12-22 17:13:05 +0000159 unsigned int fifosize; /* vendor-specific */
Russell Kingc19f12b2010-12-22 17:48:26 +0000160 unsigned int lcrh_tx; /* vendor-specific */
161 unsigned int lcrh_rx; /* vendor-specific */
Shreshtha Kumar Sahud8d8ffa2012-01-18 15:53:59 +0530162 unsigned int old_cr; /* state during shutdown */
Russell Kingc19f12b2010-12-22 17:48:26 +0000163 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,
274 };
275 struct dma_chan *chan;
276 dma_cap_mask_t mask;
277
278 /* We need platform data */
279 if (!plat || !plat->dma_filter) {
280 dev_info(uap->port.dev, "no DMA platform data\n");
281 return;
282 }
283
Linus Walleijead76f32011-02-24 13:21:08 +0100284 /* Try to acquire a generic DMA engine slave TX channel */
Russell King68b65f72010-12-22 17:24:39 +0000285 dma_cap_zero(mask);
286 dma_cap_set(DMA_SLAVE, mask);
287
288 chan = dma_request_channel(mask, plat->dma_filter, plat->dma_tx_param);
289 if (!chan) {
290 dev_err(uap->port.dev, "no TX DMA channel!\n");
291 return;
292 }
293
294 dmaengine_slave_config(chan, &tx_conf);
295 uap->dmatx.chan = chan;
296
297 dev_info(uap->port.dev, "DMA channel TX %s\n",
298 dma_chan_name(uap->dmatx.chan));
Linus Walleijead76f32011-02-24 13:21:08 +0100299
300 /* Optionally make use of an RX channel as well */
301 if (plat->dma_rx_param) {
302 struct dma_slave_config rx_conf = {
303 .src_addr = uap->port.mapbase + UART01x_DR,
304 .src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE,
Vinod Koula485df42011-10-14 10:47:38 +0530305 .direction = DMA_DEV_TO_MEM,
Linus Walleijead76f32011-02-24 13:21:08 +0100306 .src_maxburst = uap->fifosize >> 1,
307 };
308
309 chan = dma_request_channel(mask, plat->dma_filter, plat->dma_rx_param);
310 if (!chan) {
311 dev_err(uap->port.dev, "no RX DMA channel!\n");
312 return;
313 }
314
315 dmaengine_slave_config(chan, &rx_conf);
316 uap->dmarx.chan = chan;
317
318 dev_info(uap->port.dev, "DMA channel RX %s\n",
319 dma_chan_name(uap->dmarx.chan));
320 }
Russell King68b65f72010-12-22 17:24:39 +0000321}
322
323#ifndef MODULE
324/*
325 * Stack up the UARTs and let the above initcall be done at device
326 * initcall time, because the serial driver is called as an arch
327 * initcall, and at this time the DMA subsystem is not yet registered.
328 * At this point the driver will switch over to using DMA where desired.
329 */
330struct dma_uap {
331 struct list_head node;
332 struct uart_amba_port *uap;
333};
334
335static LIST_HEAD(pl011_dma_uarts);
336
337static int __init pl011_dma_initcall(void)
338{
339 struct list_head *node, *tmp;
340
341 list_for_each_safe(node, tmp, &pl011_dma_uarts) {
342 struct dma_uap *dmau = list_entry(node, struct dma_uap, node);
343 pl011_dma_probe_initcall(dmau->uap);
344 list_del(node);
345 kfree(dmau);
346 }
347 return 0;
348}
349
350device_initcall(pl011_dma_initcall);
351
352static void pl011_dma_probe(struct uart_amba_port *uap)
353{
354 struct dma_uap *dmau = kzalloc(sizeof(struct dma_uap), GFP_KERNEL);
355 if (dmau) {
356 dmau->uap = uap;
357 list_add_tail(&dmau->node, &pl011_dma_uarts);
358 }
359}
360#else
361static void pl011_dma_probe(struct uart_amba_port *uap)
362{
363 pl011_dma_probe_initcall(uap);
364}
365#endif
366
367static void pl011_dma_remove(struct uart_amba_port *uap)
368{
369 /* TODO: remove the initcall if it has not yet executed */
370 if (uap->dmatx.chan)
371 dma_release_channel(uap->dmatx.chan);
Linus Walleijead76f32011-02-24 13:21:08 +0100372 if (uap->dmarx.chan)
373 dma_release_channel(uap->dmarx.chan);
Russell King68b65f72010-12-22 17:24:39 +0000374}
375
Russell King68b65f72010-12-22 17:24:39 +0000376/* Forward declare this for the refill routine */
377static int pl011_dma_tx_refill(struct uart_amba_port *uap);
378
379/*
380 * The current DMA TX buffer has been sent.
381 * Try to queue up another DMA buffer.
382 */
383static void pl011_dma_tx_callback(void *data)
384{
385 struct uart_amba_port *uap = data;
386 struct pl011_dmatx_data *dmatx = &uap->dmatx;
387 unsigned long flags;
388 u16 dmacr;
389
390 spin_lock_irqsave(&uap->port.lock, flags);
391 if (uap->dmatx.queued)
392 dma_unmap_sg(dmatx->chan->device->dev, &dmatx->sg, 1,
393 DMA_TO_DEVICE);
394
395 dmacr = uap->dmacr;
396 uap->dmacr = dmacr & ~UART011_TXDMAE;
397 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
398
399 /*
400 * If TX DMA was disabled, it means that we've stopped the DMA for
401 * some reason (eg, XOFF received, or we want to send an X-char.)
402 *
403 * Note: we need to be careful here of a potential race between DMA
404 * and the rest of the driver - if the driver disables TX DMA while
405 * a TX buffer completing, we must update the tx queued status to
406 * get further refills (hence we check dmacr).
407 */
408 if (!(dmacr & UART011_TXDMAE) || uart_tx_stopped(&uap->port) ||
409 uart_circ_empty(&uap->port.state->xmit)) {
410 uap->dmatx.queued = false;
411 spin_unlock_irqrestore(&uap->port.lock, flags);
412 return;
413 }
414
415 if (pl011_dma_tx_refill(uap) <= 0) {
416 /*
417 * We didn't queue a DMA buffer for some reason, but we
418 * have data pending to be sent. Re-enable the TX IRQ.
419 */
420 uap->im |= UART011_TXIM;
421 writew(uap->im, uap->port.membase + UART011_IMSC);
422 }
423 spin_unlock_irqrestore(&uap->port.lock, flags);
424}
425
426/*
427 * Try to refill the TX DMA buffer.
428 * Locking: called with port lock held and IRQs disabled.
429 * Returns:
430 * 1 if we queued up a TX DMA buffer.
431 * 0 if we didn't want to handle this by DMA
432 * <0 on error
433 */
434static int pl011_dma_tx_refill(struct uart_amba_port *uap)
435{
436 struct pl011_dmatx_data *dmatx = &uap->dmatx;
437 struct dma_chan *chan = dmatx->chan;
438 struct dma_device *dma_dev = chan->device;
439 struct dma_async_tx_descriptor *desc;
440 struct circ_buf *xmit = &uap->port.state->xmit;
441 unsigned int count;
442
443 /*
444 * Try to avoid the overhead involved in using DMA if the
445 * transaction fits in the first half of the FIFO, by using
446 * the standard interrupt handling. This ensures that we
447 * issue a uart_write_wakeup() at the appropriate time.
448 */
449 count = uart_circ_chars_pending(xmit);
450 if (count < (uap->fifosize >> 1)) {
451 uap->dmatx.queued = false;
452 return 0;
453 }
454
455 /*
456 * Bodge: don't send the last character by DMA, as this
457 * will prevent XON from notifying us to restart DMA.
458 */
459 count -= 1;
460
461 /* Else proceed to copy the TX chars to the DMA buffer and fire DMA */
462 if (count > PL011_DMA_BUFFER_SIZE)
463 count = PL011_DMA_BUFFER_SIZE;
464
465 if (xmit->tail < xmit->head)
466 memcpy(&dmatx->buf[0], &xmit->buf[xmit->tail], count);
467 else {
468 size_t first = UART_XMIT_SIZE - xmit->tail;
469 size_t second = xmit->head;
470
471 memcpy(&dmatx->buf[0], &xmit->buf[xmit->tail], first);
472 if (second)
473 memcpy(&dmatx->buf[first], &xmit->buf[0], second);
474 }
475
476 dmatx->sg.length = count;
477
478 if (dma_map_sg(dma_dev->dev, &dmatx->sg, 1, DMA_TO_DEVICE) != 1) {
479 uap->dmatx.queued = false;
480 dev_dbg(uap->port.dev, "unable to map TX DMA\n");
481 return -EBUSY;
482 }
483
Vinod Koula485df42011-10-14 10:47:38 +0530484 desc = dma_dev->device_prep_slave_sg(chan, &dmatx->sg, 1, DMA_MEM_TO_DEV,
Russell King68b65f72010-12-22 17:24:39 +0000485 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
486 if (!desc) {
487 dma_unmap_sg(dma_dev->dev, &dmatx->sg, 1, DMA_TO_DEVICE);
488 uap->dmatx.queued = false;
489 /*
490 * If DMA cannot be used right now, we complete this
491 * transaction via IRQ and let the TTY layer retry.
492 */
493 dev_dbg(uap->port.dev, "TX DMA busy\n");
494 return -EBUSY;
495 }
496
497 /* Some data to go along to the callback */
498 desc->callback = pl011_dma_tx_callback;
499 desc->callback_param = uap;
500
501 /* All errors should happen at prepare time */
502 dmaengine_submit(desc);
503
504 /* Fire the DMA transaction */
505 dma_dev->device_issue_pending(chan);
506
507 uap->dmacr |= UART011_TXDMAE;
508 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
509 uap->dmatx.queued = true;
510
511 /*
512 * Now we know that DMA will fire, so advance the ring buffer
513 * with the stuff we just dispatched.
514 */
515 xmit->tail = (xmit->tail + count) & (UART_XMIT_SIZE - 1);
516 uap->port.icount.tx += count;
517
518 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
519 uart_write_wakeup(&uap->port);
520
521 return 1;
522}
523
524/*
525 * We received a transmit interrupt without a pending X-char but with
526 * pending characters.
527 * Locking: called with port lock held and IRQs disabled.
528 * Returns:
529 * false if we want to use PIO to transmit
530 * true if we queued a DMA buffer
531 */
532static bool pl011_dma_tx_irq(struct uart_amba_port *uap)
533{
Linus Walleijead76f32011-02-24 13:21:08 +0100534 if (!uap->using_tx_dma)
Russell King68b65f72010-12-22 17:24:39 +0000535 return false;
536
537 /*
538 * If we already have a TX buffer queued, but received a
539 * TX interrupt, it will be because we've just sent an X-char.
540 * Ensure the TX DMA is enabled and the TX IRQ is disabled.
541 */
542 if (uap->dmatx.queued) {
543 uap->dmacr |= UART011_TXDMAE;
544 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
545 uap->im &= ~UART011_TXIM;
546 writew(uap->im, uap->port.membase + UART011_IMSC);
547 return true;
548 }
549
550 /*
551 * We don't have a TX buffer queued, so try to queue one.
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300552 * If we successfully queued a buffer, mask the TX IRQ.
Russell King68b65f72010-12-22 17:24:39 +0000553 */
554 if (pl011_dma_tx_refill(uap) > 0) {
555 uap->im &= ~UART011_TXIM;
556 writew(uap->im, uap->port.membase + UART011_IMSC);
557 return true;
558 }
559 return false;
560}
561
562/*
563 * Stop the DMA transmit (eg, due to received XOFF).
564 * Locking: called with port lock held and IRQs disabled.
565 */
566static inline void pl011_dma_tx_stop(struct uart_amba_port *uap)
567{
568 if (uap->dmatx.queued) {
569 uap->dmacr &= ~UART011_TXDMAE;
570 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
571 }
572}
573
574/*
575 * Try to start a DMA transmit, or in the case of an XON/OFF
576 * character queued for send, try to get that character out ASAP.
577 * Locking: called with port lock held and IRQs disabled.
578 * Returns:
579 * false if we want the TX IRQ to be enabled
580 * true if we have a buffer queued
581 */
582static inline bool pl011_dma_tx_start(struct uart_amba_port *uap)
583{
584 u16 dmacr;
585
Linus Walleijead76f32011-02-24 13:21:08 +0100586 if (!uap->using_tx_dma)
Russell King68b65f72010-12-22 17:24:39 +0000587 return false;
588
589 if (!uap->port.x_char) {
590 /* no X-char, try to push chars out in DMA mode */
591 bool ret = true;
592
593 if (!uap->dmatx.queued) {
594 if (pl011_dma_tx_refill(uap) > 0) {
595 uap->im &= ~UART011_TXIM;
596 ret = true;
597 } else {
598 uap->im |= UART011_TXIM;
599 ret = false;
600 }
601 writew(uap->im, uap->port.membase + UART011_IMSC);
602 } else if (!(uap->dmacr & UART011_TXDMAE)) {
603 uap->dmacr |= UART011_TXDMAE;
604 writew(uap->dmacr,
605 uap->port.membase + UART011_DMACR);
606 }
607 return ret;
608 }
609
610 /*
611 * We have an X-char to send. Disable DMA to prevent it loading
612 * the TX fifo, and then see if we can stuff it into the FIFO.
613 */
614 dmacr = uap->dmacr;
615 uap->dmacr &= ~UART011_TXDMAE;
616 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
617
618 if (readw(uap->port.membase + UART01x_FR) & UART01x_FR_TXFF) {
619 /*
620 * No space in the FIFO, so enable the transmit interrupt
621 * so we know when there is space. Note that once we've
622 * loaded the character, we should just re-enable DMA.
623 */
624 return false;
625 }
626
627 writew(uap->port.x_char, uap->port.membase + UART01x_DR);
628 uap->port.icount.tx++;
629 uap->port.x_char = 0;
630
631 /* Success - restore the DMA state */
632 uap->dmacr = dmacr;
633 writew(dmacr, uap->port.membase + UART011_DMACR);
634
635 return true;
636}
637
638/*
639 * Flush the transmit buffer.
640 * Locking: called with port lock held and IRQs disabled.
641 */
642static void pl011_dma_flush_buffer(struct uart_port *port)
643{
644 struct uart_amba_port *uap = (struct uart_amba_port *)port;
645
Linus Walleijead76f32011-02-24 13:21:08 +0100646 if (!uap->using_tx_dma)
Russell King68b65f72010-12-22 17:24:39 +0000647 return;
648
649 /* Avoid deadlock with the DMA engine callback */
650 spin_unlock(&uap->port.lock);
651 dmaengine_terminate_all(uap->dmatx.chan);
652 spin_lock(&uap->port.lock);
653 if (uap->dmatx.queued) {
654 dma_unmap_sg(uap->dmatx.chan->device->dev, &uap->dmatx.sg, 1,
655 DMA_TO_DEVICE);
656 uap->dmatx.queued = false;
657 uap->dmacr &= ~UART011_TXDMAE;
658 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
659 }
660}
661
Linus Walleijead76f32011-02-24 13:21:08 +0100662static void pl011_dma_rx_callback(void *data);
663
664static int pl011_dma_rx_trigger_dma(struct uart_amba_port *uap)
665{
666 struct dma_chan *rxchan = uap->dmarx.chan;
667 struct dma_device *dma_dev;
668 struct pl011_dmarx_data *dmarx = &uap->dmarx;
669 struct dma_async_tx_descriptor *desc;
670 struct pl011_sgbuf *sgbuf;
671
672 if (!rxchan)
673 return -EIO;
674
675 /* Start the RX DMA job */
676 sgbuf = uap->dmarx.use_buf_b ?
677 &uap->dmarx.sgbuf_b : &uap->dmarx.sgbuf_a;
678 dma_dev = rxchan->device;
679 desc = rxchan->device->device_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;
Chanho Min6dc01aa2012-02-20 10:24:40 +0900830 struct dma_chan *rxchan = dmarx->chan;
Linus Walleijead76f32011-02-24 13:21:08 +0100831 bool lastbuf = dmarx->use_buf_b;
Chanho Min6dc01aa2012-02-20 10:24:40 +0900832 struct pl011_sgbuf *sgbuf = dmarx->use_buf_b ?
833 &dmarx->sgbuf_b : &dmarx->sgbuf_a;
834 size_t pending;
835 struct dma_tx_state state;
Linus Walleijead76f32011-02-24 13:21:08 +0100836 int ret;
837
838 /*
839 * This completion interrupt occurs typically when the
840 * RX buffer is totally stuffed but no timeout has yet
841 * occurred. When that happens, we just want the RX
842 * routine to flush out the secondary DMA buffer while
843 * we immediately trigger the next DMA job.
844 */
845 spin_lock_irq(&uap->port.lock);
Chanho Min6dc01aa2012-02-20 10:24:40 +0900846 /*
847 * Rx data can be taken by the UART interrupts during
848 * the DMA irq handler. So we check the residue here.
849 */
850 rxchan->device->device_tx_status(rxchan, dmarx->cookie, &state);
851 pending = sgbuf->sg.length - state.residue;
852 BUG_ON(pending > PL011_DMA_BUFFER_SIZE);
853 /* Then we terminate the transfer - we now know our residue */
854 dmaengine_terminate_all(rxchan);
855
Linus Walleijead76f32011-02-24 13:21:08 +0100856 uap->dmarx.running = false;
857 dmarx->use_buf_b = !lastbuf;
858 ret = pl011_dma_rx_trigger_dma(uap);
859
Chanho Min6dc01aa2012-02-20 10:24:40 +0900860 pl011_dma_rx_chars(uap, pending, lastbuf, false);
Linus Walleijead76f32011-02-24 13:21:08 +0100861 spin_unlock_irq(&uap->port.lock);
862 /*
863 * Do this check after we picked the DMA chars so we don't
864 * get some IRQ immediately from RX.
865 */
866 if (ret) {
867 dev_dbg(uap->port.dev, "could not retrigger RX DMA job "
868 "fall back to interrupt mode\n");
869 uap->im |= UART011_RXIM;
870 writew(uap->im, uap->port.membase + UART011_IMSC);
871 }
872}
873
874/*
875 * Stop accepting received characters, when we're shutting down or
876 * suspending this port.
877 * Locking: called with port lock held and IRQs disabled.
878 */
879static inline void pl011_dma_rx_stop(struct uart_amba_port *uap)
880{
881 /* FIXME. Just disable the DMA enable */
882 uap->dmacr &= ~UART011_RXDMAE;
883 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
884}
Russell King68b65f72010-12-22 17:24:39 +0000885
886static void pl011_dma_startup(struct uart_amba_port *uap)
887{
Linus Walleijead76f32011-02-24 13:21:08 +0100888 int ret;
889
Russell King68b65f72010-12-22 17:24:39 +0000890 if (!uap->dmatx.chan)
891 return;
892
893 uap->dmatx.buf = kmalloc(PL011_DMA_BUFFER_SIZE, GFP_KERNEL);
894 if (!uap->dmatx.buf) {
895 dev_err(uap->port.dev, "no memory for DMA TX buffer\n");
896 uap->port.fifosize = uap->fifosize;
897 return;
898 }
899
900 sg_init_one(&uap->dmatx.sg, uap->dmatx.buf, PL011_DMA_BUFFER_SIZE);
901
902 /* The DMA buffer is now the FIFO the TTY subsystem can use */
903 uap->port.fifosize = PL011_DMA_BUFFER_SIZE;
Linus Walleijead76f32011-02-24 13:21:08 +0100904 uap->using_tx_dma = true;
Russell King68b65f72010-12-22 17:24:39 +0000905
Linus Walleijead76f32011-02-24 13:21:08 +0100906 if (!uap->dmarx.chan)
907 goto skip_rx;
908
909 /* Allocate and map DMA RX buffers */
910 ret = pl011_sgbuf_init(uap->dmarx.chan, &uap->dmarx.sgbuf_a,
911 DMA_FROM_DEVICE);
912 if (ret) {
913 dev_err(uap->port.dev, "failed to init DMA %s: %d\n",
914 "RX buffer A", ret);
915 goto skip_rx;
916 }
917
918 ret = pl011_sgbuf_init(uap->dmarx.chan, &uap->dmarx.sgbuf_b,
919 DMA_FROM_DEVICE);
920 if (ret) {
921 dev_err(uap->port.dev, "failed to init DMA %s: %d\n",
922 "RX buffer B", ret);
923 pl011_sgbuf_free(uap->dmarx.chan, &uap->dmarx.sgbuf_a,
924 DMA_FROM_DEVICE);
925 goto skip_rx;
926 }
927
928 uap->using_rx_dma = true;
929
930skip_rx:
Russell King68b65f72010-12-22 17:24:39 +0000931 /* Turn on DMA error (RX/TX will be enabled on demand) */
932 uap->dmacr |= UART011_DMAONERR;
933 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
Russell King38d62432010-12-22 17:59:16 +0000934
935 /*
936 * ST Micro variants has some specific dma burst threshold
937 * compensation. Set this to 16 bytes, so burst will only
938 * be issued above/below 16 bytes.
939 */
940 if (uap->vendor->dma_threshold)
941 writew(ST_UART011_DMAWM_RX_16 | ST_UART011_DMAWM_TX_16,
942 uap->port.membase + ST_UART011_DMAWM);
Linus Walleijead76f32011-02-24 13:21:08 +0100943
944 if (uap->using_rx_dma) {
945 if (pl011_dma_rx_trigger_dma(uap))
946 dev_dbg(uap->port.dev, "could not trigger initial "
947 "RX DMA job, fall back to interrupt mode\n");
948 }
Russell King68b65f72010-12-22 17:24:39 +0000949}
950
951static void pl011_dma_shutdown(struct uart_amba_port *uap)
952{
Linus Walleijead76f32011-02-24 13:21:08 +0100953 if (!(uap->using_tx_dma || uap->using_rx_dma))
Russell King68b65f72010-12-22 17:24:39 +0000954 return;
955
956 /* Disable RX and TX DMA */
957 while (readw(uap->port.membase + UART01x_FR) & UART01x_FR_BUSY)
958 barrier();
959
960 spin_lock_irq(&uap->port.lock);
961 uap->dmacr &= ~(UART011_DMAONERR | UART011_RXDMAE | UART011_TXDMAE);
962 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
963 spin_unlock_irq(&uap->port.lock);
964
Linus Walleijead76f32011-02-24 13:21:08 +0100965 if (uap->using_tx_dma) {
966 /* In theory, this should already be done by pl011_dma_flush_buffer */
967 dmaengine_terminate_all(uap->dmatx.chan);
968 if (uap->dmatx.queued) {
969 dma_unmap_sg(uap->dmatx.chan->device->dev, &uap->dmatx.sg, 1,
970 DMA_TO_DEVICE);
971 uap->dmatx.queued = false;
972 }
973
974 kfree(uap->dmatx.buf);
975 uap->using_tx_dma = false;
Russell King68b65f72010-12-22 17:24:39 +0000976 }
977
Linus Walleijead76f32011-02-24 13:21:08 +0100978 if (uap->using_rx_dma) {
979 dmaengine_terminate_all(uap->dmarx.chan);
980 /* Clean up the RX DMA */
981 pl011_sgbuf_free(uap->dmarx.chan, &uap->dmarx.sgbuf_a, DMA_FROM_DEVICE);
982 pl011_sgbuf_free(uap->dmarx.chan, &uap->dmarx.sgbuf_b, DMA_FROM_DEVICE);
983 uap->using_rx_dma = false;
984 }
Russell King68b65f72010-12-22 17:24:39 +0000985}
986
Linus Walleijead76f32011-02-24 13:21:08 +0100987static inline bool pl011_dma_rx_available(struct uart_amba_port *uap)
988{
989 return uap->using_rx_dma;
990}
991
992static inline bool pl011_dma_rx_running(struct uart_amba_port *uap)
993{
994 return uap->using_rx_dma && uap->dmarx.running;
995}
996
997
Russell King68b65f72010-12-22 17:24:39 +0000998#else
999/* Blank functions if the DMA engine is not available */
1000static inline void pl011_dma_probe(struct uart_amba_port *uap)
1001{
1002}
1003
1004static inline void pl011_dma_remove(struct uart_amba_port *uap)
1005{
1006}
1007
1008static inline void pl011_dma_startup(struct uart_amba_port *uap)
1009{
1010}
1011
1012static inline void pl011_dma_shutdown(struct uart_amba_port *uap)
1013{
1014}
1015
1016static inline bool pl011_dma_tx_irq(struct uart_amba_port *uap)
1017{
1018 return false;
1019}
1020
1021static inline void pl011_dma_tx_stop(struct uart_amba_port *uap)
1022{
1023}
1024
1025static inline bool pl011_dma_tx_start(struct uart_amba_port *uap)
1026{
1027 return false;
1028}
1029
Linus Walleijead76f32011-02-24 13:21:08 +01001030static inline void pl011_dma_rx_irq(struct uart_amba_port *uap)
1031{
1032}
1033
1034static inline void pl011_dma_rx_stop(struct uart_amba_port *uap)
1035{
1036}
1037
1038static inline int pl011_dma_rx_trigger_dma(struct uart_amba_port *uap)
1039{
1040 return -EIO;
1041}
1042
1043static inline bool pl011_dma_rx_available(struct uart_amba_port *uap)
1044{
1045 return false;
1046}
1047
1048static inline bool pl011_dma_rx_running(struct uart_amba_port *uap)
1049{
1050 return false;
1051}
1052
Russell King68b65f72010-12-22 17:24:39 +00001053#define pl011_dma_flush_buffer NULL
1054#endif
1055
1056
Shreshtha Kumar Sahuc16d51a2011-06-13 10:11:33 +02001057/*
1058 * pl011_lockup_wa
1059 * This workaround aims to break the deadlock situation
1060 * when after long transfer over uart in hardware flow
1061 * control, uart interrupt registers cannot be cleared.
1062 * Hence uart transfer gets blocked.
1063 *
1064 * It is seen that during such deadlock condition ICR
1065 * don't get cleared even on multiple write. This leads
1066 * pass_counter to decrease and finally reach zero. This
1067 * can be taken as trigger point to run this UART_BT_WA.
1068 *
1069 */
1070static void pl011_lockup_wa(unsigned long data)
1071{
1072 struct uart_amba_port *uap = amba_ports[0];
1073 void __iomem *base = uap->port.membase;
1074 struct circ_buf *xmit = &uap->port.state->xmit;
1075 struct tty_struct *tty = uap->port.state->port.tty;
1076 int buf_empty_retries = 200;
1077 int loop;
1078
1079 /* Stop HCI layer from submitting data for tx */
1080 tty->hw_stopped = 1;
1081 while (!uart_circ_empty(xmit)) {
1082 if (buf_empty_retries-- == 0)
1083 break;
1084 udelay(100);
1085 }
1086
1087 /* Backup registers */
1088 for (loop = 0; loop < UART_WA_SAVE_NR; loop++)
1089 uart_wa_regdata[loop] = readl(base + uart_wa_reg[loop]);
1090
1091 /* Disable UART so that FIFO data is flushed out */
1092 writew(0x00, uap->port.membase + UART011_CR);
1093
1094 /* Soft reset UART module */
1095 if (uap->port.dev->platform_data) {
1096 struct amba_pl011_data *plat;
1097
1098 plat = uap->port.dev->platform_data;
1099 if (plat->reset)
1100 plat->reset();
1101 }
1102
1103 /* Restore registers */
1104 for (loop = 0; loop < UART_WA_SAVE_NR; loop++)
1105 writew(uart_wa_regdata[loop] ,
1106 uap->port.membase + uart_wa_reg[loop]);
1107
1108 /* Initialise the old status of the modem signals */
1109 uap->old_status = readw(uap->port.membase + UART01x_FR) &
1110 UART01x_FR_MODEM_ANY;
1111
1112 if (readl(base + UART011_MIS) & 0x2)
1113 printk(KERN_EMERG "UART_BT_WA: ***FAILED***\n");
1114
1115 /* Start Tx/Rx */
1116 tty->hw_stopped = 0;
1117}
1118
Russell Kingb129a8c2005-08-31 10:12:14 +01001119static void pl011_stop_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120{
1121 struct uart_amba_port *uap = (struct uart_amba_port *)port;
1122
1123 uap->im &= ~UART011_TXIM;
1124 writew(uap->im, uap->port.membase + UART011_IMSC);
Russell King68b65f72010-12-22 17:24:39 +00001125 pl011_dma_tx_stop(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126}
1127
Russell Kingb129a8c2005-08-31 10:12:14 +01001128static void pl011_start_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129{
1130 struct uart_amba_port *uap = (struct uart_amba_port *)port;
1131
Russell King68b65f72010-12-22 17:24:39 +00001132 if (!pl011_dma_tx_start(uap)) {
1133 uap->im |= UART011_TXIM;
1134 writew(uap->im, uap->port.membase + UART011_IMSC);
1135 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136}
1137
1138static void pl011_stop_rx(struct uart_port *port)
1139{
1140 struct uart_amba_port *uap = (struct uart_amba_port *)port;
1141
1142 uap->im &= ~(UART011_RXIM|UART011_RTIM|UART011_FEIM|
1143 UART011_PEIM|UART011_BEIM|UART011_OEIM);
1144 writew(uap->im, uap->port.membase + UART011_IMSC);
Linus Walleijead76f32011-02-24 13:21:08 +01001145
1146 pl011_dma_rx_stop(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147}
1148
1149static void pl011_enable_ms(struct uart_port *port)
1150{
1151 struct uart_amba_port *uap = (struct uart_amba_port *)port;
1152
1153 uap->im |= UART011_RIMIM|UART011_CTSMIM|UART011_DCDMIM|UART011_DSRMIM;
1154 writew(uap->im, uap->port.membase + UART011_IMSC);
1155}
1156
David Howells7d12e782006-10-05 14:55:46 +01001157static void pl011_rx_chars(struct uart_amba_port *uap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158{
Alan Coxebd2c8f2009-09-19 13:13:28 -07001159 struct tty_struct *tty = uap->port.state->port.tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160
Linus Walleij29772c42011-02-24 13:21:36 +01001161 pl011_fifo_to_tty(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162
Thomas Gleixner2389b272007-05-29 21:53:50 +01001163 spin_unlock(&uap->port.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 tty_flip_buffer_push(tty);
Linus Walleijead76f32011-02-24 13:21:08 +01001165 /*
1166 * If we were temporarily out of DMA mode for a while,
1167 * attempt to switch back to DMA mode again.
1168 */
1169 if (pl011_dma_rx_available(uap)) {
1170 if (pl011_dma_rx_trigger_dma(uap)) {
1171 dev_dbg(uap->port.dev, "could not trigger RX DMA job "
1172 "fall back to interrupt mode again\n");
1173 uap->im |= UART011_RXIM;
1174 } else
1175 uap->im &= ~UART011_RXIM;
1176 writew(uap->im, uap->port.membase + UART011_IMSC);
1177 }
Thomas Gleixner2389b272007-05-29 21:53:50 +01001178 spin_lock(&uap->port.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179}
1180
1181static void pl011_tx_chars(struct uart_amba_port *uap)
1182{
Alan Coxebd2c8f2009-09-19 13:13:28 -07001183 struct circ_buf *xmit = &uap->port.state->xmit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 int count;
1185
1186 if (uap->port.x_char) {
1187 writew(uap->port.x_char, uap->port.membase + UART01x_DR);
1188 uap->port.icount.tx++;
1189 uap->port.x_char = 0;
1190 return;
1191 }
1192 if (uart_circ_empty(xmit) || uart_tx_stopped(&uap->port)) {
Russell Kingb129a8c2005-08-31 10:12:14 +01001193 pl011_stop_tx(&uap->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 return;
1195 }
1196
Russell King68b65f72010-12-22 17:24:39 +00001197 /* If we are using DMA mode, try to send some characters. */
1198 if (pl011_dma_tx_irq(uap))
1199 return;
1200
Russell Kingffca2b12010-12-22 17:13:05 +00001201 count = uap->fifosize >> 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 do {
1203 writew(xmit->buf[xmit->tail], uap->port.membase + UART01x_DR);
1204 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
1205 uap->port.icount.tx++;
1206 if (uart_circ_empty(xmit))
1207 break;
1208 } while (--count > 0);
1209
1210 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
1211 uart_write_wakeup(&uap->port);
1212
1213 if (uart_circ_empty(xmit))
Russell Kingb129a8c2005-08-31 10:12:14 +01001214 pl011_stop_tx(&uap->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215}
1216
1217static void pl011_modem_status(struct uart_amba_port *uap)
1218{
1219 unsigned int status, delta;
1220
1221 status = readw(uap->port.membase + UART01x_FR) & UART01x_FR_MODEM_ANY;
1222
1223 delta = status ^ uap->old_status;
1224 uap->old_status = status;
1225
1226 if (!delta)
1227 return;
1228
1229 if (delta & UART01x_FR_DCD)
1230 uart_handle_dcd_change(&uap->port, status & UART01x_FR_DCD);
1231
1232 if (delta & UART01x_FR_DSR)
1233 uap->port.icount.dsr++;
1234
1235 if (delta & UART01x_FR_CTS)
1236 uart_handle_cts_change(&uap->port, status & UART01x_FR_CTS);
1237
Alan Coxbdc04e32009-09-19 13:13:31 -07001238 wake_up_interruptible(&uap->port.state->port.delta_msr_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239}
1240
David Howells7d12e782006-10-05 14:55:46 +01001241static irqreturn_t pl011_int(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242{
1243 struct uart_amba_port *uap = dev_id;
Russell King963cc982010-12-22 17:16:09 +00001244 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 unsigned int status, pass_counter = AMBA_ISR_PASS_LIMIT;
1246 int handled = 0;
1247
Russell King963cc982010-12-22 17:16:09 +00001248 spin_lock_irqsave(&uap->port.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249
1250 status = readw(uap->port.membase + UART011_MIS);
1251 if (status) {
1252 do {
1253 writew(status & ~(UART011_TXIS|UART011_RTIS|
1254 UART011_RXIS),
1255 uap->port.membase + UART011_ICR);
1256
Linus Walleijead76f32011-02-24 13:21:08 +01001257 if (status & (UART011_RTIS|UART011_RXIS)) {
1258 if (pl011_dma_rx_running(uap))
1259 pl011_dma_rx_irq(uap);
1260 else
1261 pl011_rx_chars(uap);
1262 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 if (status & (UART011_DSRMIS|UART011_DCDMIS|
1264 UART011_CTSMIS|UART011_RIMIS))
1265 pl011_modem_status(uap);
1266 if (status & UART011_TXIS)
1267 pl011_tx_chars(uap);
1268
Shreshtha Kumar Sahuc16d51a2011-06-13 10:11:33 +02001269 if (pass_counter-- == 0) {
1270 if (uap->interrupt_may_hang)
1271 tasklet_schedule(&pl011_lockup_tlet);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 break;
Shreshtha Kumar Sahuc16d51a2011-06-13 10:11:33 +02001273 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274
1275 status = readw(uap->port.membase + UART011_MIS);
1276 } while (status != 0);
1277 handled = 1;
1278 }
1279
Russell King963cc982010-12-22 17:16:09 +00001280 spin_unlock_irqrestore(&uap->port.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281
1282 return IRQ_RETVAL(handled);
1283}
1284
1285static unsigned int pl01x_tx_empty(struct uart_port *port)
1286{
1287 struct uart_amba_port *uap = (struct uart_amba_port *)port;
1288 unsigned int status = readw(uap->port.membase + UART01x_FR);
1289 return status & (UART01x_FR_BUSY|UART01x_FR_TXFF) ? 0 : TIOCSER_TEMT;
1290}
1291
1292static unsigned int pl01x_get_mctrl(struct uart_port *port)
1293{
1294 struct uart_amba_port *uap = (struct uart_amba_port *)port;
1295 unsigned int result = 0;
1296 unsigned int status = readw(uap->port.membase + UART01x_FR);
1297
Jiri Slaby5159f402007-10-18 23:40:31 -07001298#define TIOCMBIT(uartbit, tiocmbit) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 if (status & uartbit) \
1300 result |= tiocmbit
1301
Jiri Slaby5159f402007-10-18 23:40:31 -07001302 TIOCMBIT(UART01x_FR_DCD, TIOCM_CAR);
1303 TIOCMBIT(UART01x_FR_DSR, TIOCM_DSR);
1304 TIOCMBIT(UART01x_FR_CTS, TIOCM_CTS);
1305 TIOCMBIT(UART011_FR_RI, TIOCM_RNG);
1306#undef TIOCMBIT
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 return result;
1308}
1309
1310static void pl011_set_mctrl(struct uart_port *port, unsigned int mctrl)
1311{
1312 struct uart_amba_port *uap = (struct uart_amba_port *)port;
1313 unsigned int cr;
1314
1315 cr = readw(uap->port.membase + UART011_CR);
1316
Jiri Slaby5159f402007-10-18 23:40:31 -07001317#define TIOCMBIT(tiocmbit, uartbit) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 if (mctrl & tiocmbit) \
1319 cr |= uartbit; \
1320 else \
1321 cr &= ~uartbit
1322
Jiri Slaby5159f402007-10-18 23:40:31 -07001323 TIOCMBIT(TIOCM_RTS, UART011_CR_RTS);
1324 TIOCMBIT(TIOCM_DTR, UART011_CR_DTR);
1325 TIOCMBIT(TIOCM_OUT1, UART011_CR_OUT1);
1326 TIOCMBIT(TIOCM_OUT2, UART011_CR_OUT2);
1327 TIOCMBIT(TIOCM_LOOP, UART011_CR_LBE);
Rabin Vincent3b438162010-02-12 06:43:11 +01001328
1329 if (uap->autorts) {
1330 /* We need to disable auto-RTS if we want to turn RTS off */
1331 TIOCMBIT(TIOCM_RTS, UART011_CR_RTSEN);
1332 }
Jiri Slaby5159f402007-10-18 23:40:31 -07001333#undef TIOCMBIT
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334
1335 writew(cr, uap->port.membase + UART011_CR);
1336}
1337
1338static void pl011_break_ctl(struct uart_port *port, int break_state)
1339{
1340 struct uart_amba_port *uap = (struct uart_amba_port *)port;
1341 unsigned long flags;
1342 unsigned int lcr_h;
1343
1344 spin_lock_irqsave(&uap->port.lock, flags);
Linus Walleijec489aa2010-06-02 08:13:52 +01001345 lcr_h = readw(uap->port.membase + uap->lcrh_tx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 if (break_state == -1)
1347 lcr_h |= UART01x_LCRH_BRK;
1348 else
1349 lcr_h &= ~UART01x_LCRH_BRK;
Linus Walleijec489aa2010-06-02 08:13:52 +01001350 writew(lcr_h, uap->port.membase + uap->lcrh_tx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 spin_unlock_irqrestore(&uap->port.lock, flags);
1352}
1353
Jason Wessel84b5ae12008-02-20 13:33:39 -06001354#ifdef CONFIG_CONSOLE_POLL
1355static int pl010_get_poll_char(struct uart_port *port)
1356{
1357 struct uart_amba_port *uap = (struct uart_amba_port *)port;
1358 unsigned int status;
1359
Jason Wesself5316b42010-05-20 21:04:22 -05001360 status = readw(uap->port.membase + UART01x_FR);
1361 if (status & UART01x_FR_RXFE)
1362 return NO_POLL_CHAR;
Jason Wessel84b5ae12008-02-20 13:33:39 -06001363
1364 return readw(uap->port.membase + UART01x_DR);
1365}
1366
1367static void pl010_put_poll_char(struct uart_port *port,
1368 unsigned char ch)
1369{
1370 struct uart_amba_port *uap = (struct uart_amba_port *)port;
1371
1372 while (readw(uap->port.membase + UART01x_FR) & UART01x_FR_TXFF)
1373 barrier();
1374
1375 writew(ch, uap->port.membase + UART01x_DR);
1376}
1377
1378#endif /* CONFIG_CONSOLE_POLL */
1379
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380static int pl011_startup(struct uart_port *port)
1381{
1382 struct uart_amba_port *uap = (struct uart_amba_port *)port;
1383 unsigned int cr;
1384 int retval;
1385
Russell King4b4851c2011-09-22 11:35:30 +01001386 retval = clk_prepare(uap->clk);
1387 if (retval)
1388 goto out;
1389
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 /*
1391 * Try to enable the clock producer.
1392 */
1393 retval = clk_enable(uap->clk);
1394 if (retval)
Russell King4b4851c2011-09-22 11:35:30 +01001395 goto clk_unprep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396
1397 uap->port.uartclk = clk_get_rate(uap->clk);
1398
1399 /*
1400 * Allocate the IRQ
1401 */
1402 retval = request_irq(uap->port.irq, pl011_int, 0, "uart-pl011", uap);
1403 if (retval)
1404 goto clk_dis;
1405
Russell Kingc19f12b2010-12-22 17:48:26 +00001406 writew(uap->vendor->ifls, uap->port.membase + UART011_IFLS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407
1408 /*
1409 * Provoke TX FIFO interrupt into asserting.
1410 */
1411 cr = UART01x_CR_UARTEN | UART011_CR_TXE | UART011_CR_LBE;
1412 writew(cr, uap->port.membase + UART011_CR);
1413 writew(0, uap->port.membase + UART011_FBRD);
1414 writew(1, uap->port.membase + UART011_IBRD);
Linus Walleijec489aa2010-06-02 08:13:52 +01001415 writew(0, uap->port.membase + uap->lcrh_rx);
1416 if (uap->lcrh_tx != uap->lcrh_rx) {
1417 int i;
1418 /*
1419 * Wait 10 PCLKs before writing LCRH_TX register,
1420 * to get this delay write read only register 10 times
1421 */
1422 for (i = 0; i < 10; ++i)
1423 writew(0xff, uap->port.membase + UART011_MIS);
1424 writew(0, uap->port.membase + uap->lcrh_tx);
1425 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 writew(0, uap->port.membase + UART01x_DR);
1427 while (readw(uap->port.membase + UART01x_FR) & UART01x_FR_BUSY)
1428 barrier();
1429
Shreshtha Kumar Sahud8d8ffa2012-01-18 15:53:59 +05301430 /* restore RTS and DTR */
1431 cr = uap->old_cr & (UART011_CR_RTS | UART011_CR_DTR);
1432 cr |= UART01x_CR_UARTEN | UART011_CR_RXE | UART011_CR_TXE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 writew(cr, uap->port.membase + UART011_CR);
1434
Russell King5063e2c2010-12-22 17:09:08 +00001435 /* Clear pending error interrupts */
1436 writew(UART011_OEIS | UART011_BEIS | UART011_PEIS | UART011_FEIS,
1437 uap->port.membase + UART011_ICR);
1438
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 /*
1440 * initialise the old status of the modem signals
1441 */
1442 uap->old_status = readw(uap->port.membase + UART01x_FR) & UART01x_FR_MODEM_ANY;
1443
Russell King68b65f72010-12-22 17:24:39 +00001444 /* Startup DMA */
1445 pl011_dma_startup(uap);
1446
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 /*
Linus Walleijead76f32011-02-24 13:21:08 +01001448 * Finally, enable interrupts, only timeouts when using DMA
1449 * if initial RX DMA job failed, start in interrupt mode
1450 * as well.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 */
1452 spin_lock_irq(&uap->port.lock);
Linus Walleijead76f32011-02-24 13:21:08 +01001453 uap->im = UART011_RTIM;
1454 if (!pl011_dma_rx_running(uap))
1455 uap->im |= UART011_RXIM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 writew(uap->im, uap->port.membase + UART011_IMSC);
1457 spin_unlock_irq(&uap->port.lock);
1458
Shreshtha Kumar Sahuc16d51a2011-06-13 10:11:33 +02001459 if (uap->port.dev->platform_data) {
1460 struct amba_pl011_data *plat;
1461
1462 plat = uap->port.dev->platform_data;
1463 if (plat->init)
1464 plat->init();
1465 }
1466
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 return 0;
1468
1469 clk_dis:
1470 clk_disable(uap->clk);
Russell King4b4851c2011-09-22 11:35:30 +01001471 clk_unprep:
1472 clk_unprepare(uap->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 out:
1474 return retval;
1475}
1476
Linus Walleijec489aa2010-06-02 08:13:52 +01001477static void pl011_shutdown_channel(struct uart_amba_port *uap,
1478 unsigned int lcrh)
1479{
1480 unsigned long val;
1481
1482 val = readw(uap->port.membase + lcrh);
1483 val &= ~(UART01x_LCRH_BRK | UART01x_LCRH_FEN);
1484 writew(val, uap->port.membase + lcrh);
1485}
1486
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487static void pl011_shutdown(struct uart_port *port)
1488{
1489 struct uart_amba_port *uap = (struct uart_amba_port *)port;
Shreshtha Kumar Sahud8d8ffa2012-01-18 15:53:59 +05301490 unsigned int cr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491
1492 /*
1493 * disable all interrupts
1494 */
1495 spin_lock_irq(&uap->port.lock);
1496 uap->im = 0;
1497 writew(uap->im, uap->port.membase + UART011_IMSC);
1498 writew(0xffff, uap->port.membase + UART011_ICR);
1499 spin_unlock_irq(&uap->port.lock);
1500
Russell King68b65f72010-12-22 17:24:39 +00001501 pl011_dma_shutdown(uap);
1502
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 /*
1504 * Free the interrupt
1505 */
1506 free_irq(uap->port.irq, uap);
1507
1508 /*
1509 * disable the port
Shreshtha Kumar Sahud8d8ffa2012-01-18 15:53:59 +05301510 * disable the port. It should not disable RTS and DTR.
1511 * Also RTS and DTR state should be preserved to restore
1512 * it during startup().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 */
Rabin Vincent3b438162010-02-12 06:43:11 +01001514 uap->autorts = false;
Shreshtha Kumar Sahud8d8ffa2012-01-18 15:53:59 +05301515 cr = readw(uap->port.membase + UART011_CR);
1516 uap->old_cr = cr;
1517 cr &= UART011_CR_RTS | UART011_CR_DTR;
1518 cr |= UART01x_CR_UARTEN | UART011_CR_TXE;
1519 writew(cr, uap->port.membase + UART011_CR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520
1521 /*
1522 * disable break condition and fifos
1523 */
Linus Walleijec489aa2010-06-02 08:13:52 +01001524 pl011_shutdown_channel(uap, uap->lcrh_rx);
1525 if (uap->lcrh_rx != uap->lcrh_tx)
1526 pl011_shutdown_channel(uap, uap->lcrh_tx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527
1528 /*
1529 * Shut down the clock producer
1530 */
1531 clk_disable(uap->clk);
Russell King4b4851c2011-09-22 11:35:30 +01001532 clk_unprepare(uap->clk);
Shreshtha Kumar Sahuc16d51a2011-06-13 10:11:33 +02001533
1534 if (uap->port.dev->platform_data) {
1535 struct amba_pl011_data *plat;
1536
1537 plat = uap->port.dev->platform_data;
1538 if (plat->exit)
1539 plat->exit();
1540 }
1541
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542}
1543
1544static void
Alan Cox606d0992006-12-08 02:38:45 -08001545pl011_set_termios(struct uart_port *port, struct ktermios *termios,
1546 struct ktermios *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547{
Rabin Vincent3b438162010-02-12 06:43:11 +01001548 struct uart_amba_port *uap = (struct uart_amba_port *)port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 unsigned int lcr_h, old_cr;
1550 unsigned long flags;
Russell Kingc19f12b2010-12-22 17:48:26 +00001551 unsigned int baud, quot, clkdiv;
1552
1553 if (uap->vendor->oversampling)
1554 clkdiv = 8;
1555 else
1556 clkdiv = 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557
1558 /*
1559 * Ask the core to calculate the divisor for us.
1560 */
Linus Walleijac3e3fb2010-06-02 20:40:22 +01001561 baud = uart_get_baud_rate(port, termios, old, 0,
Russell Kingc19f12b2010-12-22 17:48:26 +00001562 port->uartclk / clkdiv);
Linus Walleijac3e3fb2010-06-02 20:40:22 +01001563
1564 if (baud > port->uartclk/16)
1565 quot = DIV_ROUND_CLOSEST(port->uartclk * 8, baud);
1566 else
1567 quot = DIV_ROUND_CLOSEST(port->uartclk * 4, baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568
1569 switch (termios->c_cflag & CSIZE) {
1570 case CS5:
1571 lcr_h = UART01x_LCRH_WLEN_5;
1572 break;
1573 case CS6:
1574 lcr_h = UART01x_LCRH_WLEN_6;
1575 break;
1576 case CS7:
1577 lcr_h = UART01x_LCRH_WLEN_7;
1578 break;
1579 default: // CS8
1580 lcr_h = UART01x_LCRH_WLEN_8;
1581 break;
1582 }
1583 if (termios->c_cflag & CSTOPB)
1584 lcr_h |= UART01x_LCRH_STP2;
1585 if (termios->c_cflag & PARENB) {
1586 lcr_h |= UART01x_LCRH_PEN;
1587 if (!(termios->c_cflag & PARODD))
1588 lcr_h |= UART01x_LCRH_EPS;
1589 }
Russell Kingffca2b12010-12-22 17:13:05 +00001590 if (uap->fifosize > 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 lcr_h |= UART01x_LCRH_FEN;
1592
1593 spin_lock_irqsave(&port->lock, flags);
1594
1595 /*
1596 * Update the per-port timeout.
1597 */
1598 uart_update_timeout(port, termios->c_cflag, baud);
1599
Russell Kingb63d4f02005-11-19 11:10:35 +00001600 port->read_status_mask = UART011_DR_OE | 255;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 if (termios->c_iflag & INPCK)
Russell Kingb63d4f02005-11-19 11:10:35 +00001602 port->read_status_mask |= UART011_DR_FE | UART011_DR_PE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 if (termios->c_iflag & (BRKINT | PARMRK))
Russell Kingb63d4f02005-11-19 11:10:35 +00001604 port->read_status_mask |= UART011_DR_BE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605
1606 /*
1607 * Characters to ignore
1608 */
1609 port->ignore_status_mask = 0;
1610 if (termios->c_iflag & IGNPAR)
Russell Kingb63d4f02005-11-19 11:10:35 +00001611 port->ignore_status_mask |= UART011_DR_FE | UART011_DR_PE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 if (termios->c_iflag & IGNBRK) {
Russell Kingb63d4f02005-11-19 11:10:35 +00001613 port->ignore_status_mask |= UART011_DR_BE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 /*
1615 * If we're ignoring parity and break indicators,
1616 * ignore overruns too (for real raw support).
1617 */
1618 if (termios->c_iflag & IGNPAR)
Russell Kingb63d4f02005-11-19 11:10:35 +00001619 port->ignore_status_mask |= UART011_DR_OE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 }
1621
1622 /*
1623 * Ignore all characters if CREAD is not set.
1624 */
1625 if ((termios->c_cflag & CREAD) == 0)
Russell Kingb63d4f02005-11-19 11:10:35 +00001626 port->ignore_status_mask |= UART_DUMMY_DR_RX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627
1628 if (UART_ENABLE_MS(port, termios->c_cflag))
1629 pl011_enable_ms(port);
1630
1631 /* first, disable everything */
1632 old_cr = readw(port->membase + UART011_CR);
1633 writew(0, port->membase + UART011_CR);
1634
Rabin Vincent3b438162010-02-12 06:43:11 +01001635 if (termios->c_cflag & CRTSCTS) {
1636 if (old_cr & UART011_CR_RTS)
1637 old_cr |= UART011_CR_RTSEN;
1638
1639 old_cr |= UART011_CR_CTSEN;
1640 uap->autorts = true;
1641 } else {
1642 old_cr &= ~(UART011_CR_CTSEN | UART011_CR_RTSEN);
1643 uap->autorts = false;
1644 }
1645
Russell Kingc19f12b2010-12-22 17:48:26 +00001646 if (uap->vendor->oversampling) {
1647 if (baud > port->uartclk / 16)
Linus Walleijac3e3fb2010-06-02 20:40:22 +01001648 old_cr |= ST_UART011_CR_OVSFACT;
1649 else
1650 old_cr &= ~ST_UART011_CR_OVSFACT;
1651 }
1652
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 /* Set baud rate */
1654 writew(quot & 0x3f, port->membase + UART011_FBRD);
1655 writew(quot >> 6, port->membase + UART011_IBRD);
1656
1657 /*
1658 * ----------v----------v----------v----------v-----
1659 * NOTE: MUST BE WRITTEN AFTER UARTLCR_M & UARTLCR_L
1660 * ----------^----------^----------^----------^-----
1661 */
Linus Walleijec489aa2010-06-02 08:13:52 +01001662 writew(lcr_h, port->membase + uap->lcrh_rx);
1663 if (uap->lcrh_rx != uap->lcrh_tx) {
1664 int i;
1665 /*
1666 * Wait 10 PCLKs before writing LCRH_TX register,
1667 * to get this delay write read only register 10 times
1668 */
1669 for (i = 0; i < 10; ++i)
1670 writew(0xff, uap->port.membase + UART011_MIS);
1671 writew(lcr_h, port->membase + uap->lcrh_tx);
1672 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 writew(old_cr, port->membase + UART011_CR);
1674
1675 spin_unlock_irqrestore(&port->lock, flags);
1676}
1677
1678static const char *pl011_type(struct uart_port *port)
1679{
Russell Kinge8a7ba82010-12-28 09:16:54 +00001680 struct uart_amba_port *uap = (struct uart_amba_port *)port;
1681 return uap->port.type == PORT_AMBA ? uap->type : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682}
1683
1684/*
1685 * Release the memory region(s) being used by 'port'
1686 */
1687static void pl010_release_port(struct uart_port *port)
1688{
1689 release_mem_region(port->mapbase, SZ_4K);
1690}
1691
1692/*
1693 * Request the memory region(s) being used by 'port'
1694 */
1695static int pl010_request_port(struct uart_port *port)
1696{
1697 return request_mem_region(port->mapbase, SZ_4K, "uart-pl011")
1698 != NULL ? 0 : -EBUSY;
1699}
1700
1701/*
1702 * Configure/autoconfigure the port.
1703 */
1704static void pl010_config_port(struct uart_port *port, int flags)
1705{
1706 if (flags & UART_CONFIG_TYPE) {
1707 port->type = PORT_AMBA;
1708 pl010_request_port(port);
1709 }
1710}
1711
1712/*
1713 * verify the new serial_struct (for TIOCSSERIAL).
1714 */
1715static int pl010_verify_port(struct uart_port *port, struct serial_struct *ser)
1716{
1717 int ret = 0;
1718 if (ser->type != PORT_UNKNOWN && ser->type != PORT_AMBA)
1719 ret = -EINVAL;
Yinghai Lua62c4132008-08-19 20:49:55 -07001720 if (ser->irq < 0 || ser->irq >= nr_irqs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 ret = -EINVAL;
1722 if (ser->baud_base < 9600)
1723 ret = -EINVAL;
1724 return ret;
1725}
1726
1727static struct uart_ops amba_pl011_pops = {
1728 .tx_empty = pl01x_tx_empty,
1729 .set_mctrl = pl011_set_mctrl,
1730 .get_mctrl = pl01x_get_mctrl,
1731 .stop_tx = pl011_stop_tx,
1732 .start_tx = pl011_start_tx,
1733 .stop_rx = pl011_stop_rx,
1734 .enable_ms = pl011_enable_ms,
1735 .break_ctl = pl011_break_ctl,
1736 .startup = pl011_startup,
1737 .shutdown = pl011_shutdown,
Russell King68b65f72010-12-22 17:24:39 +00001738 .flush_buffer = pl011_dma_flush_buffer,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 .set_termios = pl011_set_termios,
1740 .type = pl011_type,
1741 .release_port = pl010_release_port,
1742 .request_port = pl010_request_port,
1743 .config_port = pl010_config_port,
1744 .verify_port = pl010_verify_port,
Jason Wessel84b5ae12008-02-20 13:33:39 -06001745#ifdef CONFIG_CONSOLE_POLL
1746 .poll_get_char = pl010_get_poll_char,
1747 .poll_put_char = pl010_put_poll_char,
1748#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749};
1750
1751static struct uart_amba_port *amba_ports[UART_NR];
1752
1753#ifdef CONFIG_SERIAL_AMBA_PL011_CONSOLE
1754
Russell Kingd3587882006-03-20 20:00:09 +00001755static void pl011_console_putchar(struct uart_port *port, int ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756{
Russell Kingd3587882006-03-20 20:00:09 +00001757 struct uart_amba_port *uap = (struct uart_amba_port *)port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758
Russell Kingd3587882006-03-20 20:00:09 +00001759 while (readw(uap->port.membase + UART01x_FR) & UART01x_FR_TXFF)
1760 barrier();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 writew(ch, uap->port.membase + UART01x_DR);
1762}
1763
1764static void
1765pl011_console_write(struct console *co, const char *s, unsigned int count)
1766{
1767 struct uart_amba_port *uap = amba_ports[co->index];
1768 unsigned int status, old_cr, new_cr;
Rabin Vincentef605fd2012-01-17 11:52:28 +01001769 unsigned long flags;
1770 int locked = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771
1772 clk_enable(uap->clk);
1773
Rabin Vincentef605fd2012-01-17 11:52:28 +01001774 local_irq_save(flags);
1775 if (uap->port.sysrq)
1776 locked = 0;
1777 else if (oops_in_progress)
1778 locked = spin_trylock(&uap->port.lock);
1779 else
1780 spin_lock(&uap->port.lock);
1781
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 /*
1783 * First save the CR then disable the interrupts
1784 */
1785 old_cr = readw(uap->port.membase + UART011_CR);
1786 new_cr = old_cr & ~UART011_CR_CTSEN;
1787 new_cr |= UART01x_CR_UARTEN | UART011_CR_TXE;
1788 writew(new_cr, uap->port.membase + UART011_CR);
1789
Russell Kingd3587882006-03-20 20:00:09 +00001790 uart_console_write(&uap->port, s, count, pl011_console_putchar);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791
1792 /*
1793 * Finally, wait for transmitter to become empty
1794 * and restore the TCR
1795 */
1796 do {
1797 status = readw(uap->port.membase + UART01x_FR);
1798 } while (status & UART01x_FR_BUSY);
1799 writew(old_cr, uap->port.membase + UART011_CR);
1800
Rabin Vincentef605fd2012-01-17 11:52:28 +01001801 if (locked)
1802 spin_unlock(&uap->port.lock);
1803 local_irq_restore(flags);
1804
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 clk_disable(uap->clk);
1806}
1807
1808static void __init
1809pl011_console_get_options(struct uart_amba_port *uap, int *baud,
1810 int *parity, int *bits)
1811{
1812 if (readw(uap->port.membase + UART011_CR) & UART01x_CR_UARTEN) {
1813 unsigned int lcr_h, ibrd, fbrd;
1814
Linus Walleijec489aa2010-06-02 08:13:52 +01001815 lcr_h = readw(uap->port.membase + uap->lcrh_tx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816
1817 *parity = 'n';
1818 if (lcr_h & UART01x_LCRH_PEN) {
1819 if (lcr_h & UART01x_LCRH_EPS)
1820 *parity = 'e';
1821 else
1822 *parity = 'o';
1823 }
1824
1825 if ((lcr_h & 0x60) == UART01x_LCRH_WLEN_7)
1826 *bits = 7;
1827 else
1828 *bits = 8;
1829
1830 ibrd = readw(uap->port.membase + UART011_IBRD);
1831 fbrd = readw(uap->port.membase + UART011_FBRD);
1832
1833 *baud = uap->port.uartclk * 4 / (64 * ibrd + fbrd);
Linus Walleijac3e3fb2010-06-02 20:40:22 +01001834
Russell Kingc19f12b2010-12-22 17:48:26 +00001835 if (uap->vendor->oversampling) {
Linus Walleijac3e3fb2010-06-02 20:40:22 +01001836 if (readw(uap->port.membase + UART011_CR)
1837 & ST_UART011_CR_OVSFACT)
1838 *baud *= 2;
1839 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 }
1841}
1842
1843static int __init pl011_console_setup(struct console *co, char *options)
1844{
1845 struct uart_amba_port *uap;
1846 int baud = 38400;
1847 int bits = 8;
1848 int parity = 'n';
1849 int flow = 'n';
Russell King4b4851c2011-09-22 11:35:30 +01001850 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851
1852 /*
1853 * Check whether an invalid uart number has been specified, and
1854 * if so, search for the first available port that does have
1855 * console support.
1856 */
1857 if (co->index >= UART_NR)
1858 co->index = 0;
1859 uap = amba_ports[co->index];
Russell Kingd28122a2007-01-22 18:59:42 +00001860 if (!uap)
1861 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862
Russell King4b4851c2011-09-22 11:35:30 +01001863 ret = clk_prepare(uap->clk);
1864 if (ret)
1865 return ret;
1866
Shreshtha Kumar Sahuc16d51a2011-06-13 10:11:33 +02001867 if (uap->port.dev->platform_data) {
1868 struct amba_pl011_data *plat;
1869
1870 plat = uap->port.dev->platform_data;
1871 if (plat->init)
1872 plat->init();
1873 }
1874
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 uap->port.uartclk = clk_get_rate(uap->clk);
1876
1877 if (options)
1878 uart_parse_options(options, &baud, &parity, &bits, &flow);
1879 else
1880 pl011_console_get_options(uap, &baud, &parity, &bits);
1881
1882 return uart_set_options(&uap->port, co, baud, parity, bits, flow);
1883}
1884
Vincent Sanders2d934862005-09-14 22:36:03 +01001885static struct uart_driver amba_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886static struct console amba_console = {
1887 .name = "ttyAMA",
1888 .write = pl011_console_write,
1889 .device = uart_console_device,
1890 .setup = pl011_console_setup,
1891 .flags = CON_PRINTBUFFER,
1892 .index = -1,
1893 .data = &amba_reg,
1894};
1895
1896#define AMBA_CONSOLE (&amba_console)
1897#else
1898#define AMBA_CONSOLE NULL
1899#endif
1900
1901static struct uart_driver amba_reg = {
1902 .owner = THIS_MODULE,
1903 .driver_name = "ttyAMA",
1904 .dev_name = "ttyAMA",
1905 .major = SERIAL_AMBA_MAJOR,
1906 .minor = SERIAL_AMBA_MINOR,
1907 .nr = UART_NR,
1908 .cons = AMBA_CONSOLE,
1909};
1910
Russell Kingaa25afa2011-02-19 15:55:00 +00001911static int pl011_probe(struct amba_device *dev, const struct amba_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912{
1913 struct uart_amba_port *uap;
Alessandro Rubini5926a292009-06-04 17:43:04 +01001914 struct vendor_data *vendor = id->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 void __iomem *base;
1916 int i, ret;
1917
1918 for (i = 0; i < ARRAY_SIZE(amba_ports); i++)
1919 if (amba_ports[i] == NULL)
1920 break;
1921
1922 if (i == ARRAY_SIZE(amba_ports)) {
1923 ret = -EBUSY;
1924 goto out;
1925 }
1926
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07001927 uap = kzalloc(sizeof(struct uart_amba_port), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 if (uap == NULL) {
1929 ret = -ENOMEM;
1930 goto out;
1931 }
1932
Linus Walleijdc890c22009-06-07 23:27:31 +01001933 base = ioremap(dev->res.start, resource_size(&dev->res));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 if (!base) {
1935 ret = -ENOMEM;
1936 goto free;
1937 }
1938
Russell Kingee569c42008-11-30 17:38:14 +00001939 uap->clk = clk_get(&dev->dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 if (IS_ERR(uap->clk)) {
1941 ret = PTR_ERR(uap->clk);
1942 goto unmap;
1943 }
1944
Russell Kingc19f12b2010-12-22 17:48:26 +00001945 uap->vendor = vendor;
Linus Walleijec489aa2010-06-02 08:13:52 +01001946 uap->lcrh_rx = vendor->lcrh_rx;
1947 uap->lcrh_tx = vendor->lcrh_tx;
Shreshtha Kumar Sahud8d8ffa2012-01-18 15:53:59 +05301948 uap->old_cr = 0;
Russell Kingffca2b12010-12-22 17:13:05 +00001949 uap->fifosize = vendor->fifosize;
Shreshtha Kumar Sahuc16d51a2011-06-13 10:11:33 +02001950 uap->interrupt_may_hang = vendor->interrupt_may_hang;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 uap->port.dev = &dev->dev;
1952 uap->port.mapbase = dev->res.start;
1953 uap->port.membase = base;
1954 uap->port.iotype = UPIO_MEM;
1955 uap->port.irq = dev->irq[0];
Russell Kingffca2b12010-12-22 17:13:05 +00001956 uap->port.fifosize = uap->fifosize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 uap->port.ops = &amba_pl011_pops;
1958 uap->port.flags = UPF_BOOT_AUTOCONF;
1959 uap->port.line = i;
Russell King68b65f72010-12-22 17:24:39 +00001960 pl011_dma_probe(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961
Russell Kinge8a7ba82010-12-28 09:16:54 +00001962 snprintf(uap->type, sizeof(uap->type), "PL011 rev%u", amba_rev(dev));
1963
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 amba_ports[i] = uap;
1965
1966 amba_set_drvdata(dev, uap);
1967 ret = uart_add_one_port(&amba_reg, &uap->port);
1968 if (ret) {
1969 amba_set_drvdata(dev, NULL);
1970 amba_ports[i] = NULL;
Russell King68b65f72010-12-22 17:24:39 +00001971 pl011_dma_remove(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 clk_put(uap->clk);
1973 unmap:
1974 iounmap(base);
1975 free:
1976 kfree(uap);
1977 }
1978 out:
1979 return ret;
1980}
1981
1982static int pl011_remove(struct amba_device *dev)
1983{
1984 struct uart_amba_port *uap = amba_get_drvdata(dev);
1985 int i;
1986
1987 amba_set_drvdata(dev, NULL);
1988
1989 uart_remove_one_port(&amba_reg, &uap->port);
1990
1991 for (i = 0; i < ARRAY_SIZE(amba_ports); i++)
1992 if (amba_ports[i] == uap)
1993 amba_ports[i] = NULL;
1994
Russell King68b65f72010-12-22 17:24:39 +00001995 pl011_dma_remove(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 iounmap(uap->port.membase);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 clk_put(uap->clk);
1998 kfree(uap);
1999 return 0;
2000}
2001
Leo Chenb736b892009-07-28 23:43:33 +01002002#ifdef CONFIG_PM
2003static int pl011_suspend(struct amba_device *dev, pm_message_t state)
2004{
2005 struct uart_amba_port *uap = amba_get_drvdata(dev);
2006
2007 if (!uap)
2008 return -EINVAL;
2009
2010 return uart_suspend_port(&amba_reg, &uap->port);
2011}
2012
2013static int pl011_resume(struct amba_device *dev)
2014{
2015 struct uart_amba_port *uap = amba_get_drvdata(dev);
2016
2017 if (!uap)
2018 return -EINVAL;
2019
2020 return uart_resume_port(&amba_reg, &uap->port);
2021}
2022#endif
2023
Russell King2c39c9e2010-07-27 08:50:16 +01002024static struct amba_id pl011_ids[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 {
2026 .id = 0x00041011,
2027 .mask = 0x000fffff,
Alessandro Rubini5926a292009-06-04 17:43:04 +01002028 .data = &vendor_arm,
2029 },
2030 {
2031 .id = 0x00380802,
2032 .mask = 0x00ffffff,
2033 .data = &vendor_st,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 },
2035 { 0, 0 },
2036};
2037
Dave Martin60f7a332011-10-05 15:15:22 +01002038MODULE_DEVICE_TABLE(amba, pl011_ids);
2039
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040static struct amba_driver pl011_driver = {
2041 .drv = {
2042 .name = "uart-pl011",
2043 },
2044 .id_table = pl011_ids,
2045 .probe = pl011_probe,
2046 .remove = pl011_remove,
Leo Chenb736b892009-07-28 23:43:33 +01002047#ifdef CONFIG_PM
2048 .suspend = pl011_suspend,
2049 .resume = pl011_resume,
2050#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051};
2052
2053static int __init pl011_init(void)
2054{
2055 int ret;
2056 printk(KERN_INFO "Serial: AMBA PL011 UART driver\n");
2057
2058 ret = uart_register_driver(&amba_reg);
2059 if (ret == 0) {
2060 ret = amba_driver_register(&pl011_driver);
2061 if (ret)
2062 uart_unregister_driver(&amba_reg);
2063 }
2064 return ret;
2065}
2066
2067static void __exit pl011_exit(void)
2068{
2069 amba_driver_unregister(&pl011_driver);
2070 uart_unregister_driver(&amba_reg);
2071}
2072
Alessandro Rubini4dd9e742009-05-05 05:54:13 +01002073/*
2074 * While this can be a module, if builtin it's most likely the console
2075 * So let's leave module_exit but move module_init to an earlier place
2076 */
2077arch_initcall(pl011_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078module_exit(pl011_exit);
2079
2080MODULE_AUTHOR("ARM Ltd/Deep Blue Solutions Ltd");
2081MODULE_DESCRIPTION("ARM AMBA serial port driver");
2082MODULE_LICENSE("GPL");