blob: 062ef8c2b3cb07630a7dc2eb4fbb9369b0a2d7f5 [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>
Shawn Guo258e0552012-05-06 22:53:35 +080055#include <linux/pinctrl/consumer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
57#include <asm/io.h>
Russell Kingc6b8fda2005-10-28 14:05:16 +010058#include <asm/sizes.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
60#define UART_NR 14
61
62#define SERIAL_AMBA_MAJOR 204
63#define SERIAL_AMBA_MINOR 64
64#define SERIAL_AMBA_NR UART_NR
65
66#define AMBA_ISR_PASS_LIMIT 256
67
Russell Kingb63d4f02005-11-19 11:10:35 +000068#define UART_DR_ERROR (UART011_DR_OE|UART011_DR_BE|UART011_DR_PE|UART011_DR_FE)
69#define UART_DUMMY_DR_RX (1 << 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
Shreshtha Kumar Sahuc16d51a2011-06-13 10:11:33 +020071
72#define UART_WA_SAVE_NR 14
73
74static void pl011_lockup_wa(unsigned long data);
75static const u32 uart_wa_reg[UART_WA_SAVE_NR] = {
76 ST_UART011_DMAWM,
77 ST_UART011_TIMEOUT,
78 ST_UART011_LCRH_RX,
79 UART011_IBRD,
80 UART011_FBRD,
81 ST_UART011_LCRH_TX,
82 UART011_IFLS,
83 ST_UART011_XFCR,
84 ST_UART011_XON1,
85 ST_UART011_XON2,
86 ST_UART011_XOFF1,
87 ST_UART011_XOFF2,
88 UART011_CR,
89 UART011_IMSC
90};
91
92static u32 uart_wa_regdata[UART_WA_SAVE_NR];
93static DECLARE_TASKLET(pl011_lockup_tlet, pl011_lockup_wa, 0);
94
Alessandro Rubini5926a292009-06-04 17:43:04 +010095/* There is by now at least one vendor with differing details, so handle it */
96struct vendor_data {
97 unsigned int ifls;
98 unsigned int fifosize;
Linus Walleijec489aa2010-06-02 08:13:52 +010099 unsigned int lcrh_tx;
100 unsigned int lcrh_rx;
Linus Walleijac3e3fb2010-06-02 20:40:22 +0100101 bool oversampling;
Shreshtha Kumar Sahuc16d51a2011-06-13 10:11:33 +0200102 bool interrupt_may_hang; /* vendor-specific */
Russell King38d62432010-12-22 17:59:16 +0000103 bool dma_threshold;
Alessandro Rubini5926a292009-06-04 17:43:04 +0100104};
105
106static struct vendor_data vendor_arm = {
107 .ifls = UART011_IFLS_RX4_8|UART011_IFLS_TX4_8,
108 .fifosize = 16,
Linus Walleijec489aa2010-06-02 08:13:52 +0100109 .lcrh_tx = UART011_LCRH,
110 .lcrh_rx = UART011_LCRH,
Linus Walleijac3e3fb2010-06-02 20:40:22 +0100111 .oversampling = false,
Russell King38d62432010-12-22 17:59:16 +0000112 .dma_threshold = false,
Alessandro Rubini5926a292009-06-04 17:43:04 +0100113};
114
115static struct vendor_data vendor_st = {
116 .ifls = UART011_IFLS_RX_HALF|UART011_IFLS_TX_HALF,
117 .fifosize = 64,
Linus Walleijec489aa2010-06-02 08:13:52 +0100118 .lcrh_tx = ST_UART011_LCRH_TX,
119 .lcrh_rx = ST_UART011_LCRH_RX,
Linus Walleijac3e3fb2010-06-02 20:40:22 +0100120 .oversampling = true,
Shreshtha Kumar Sahuc16d51a2011-06-13 10:11:33 +0200121 .interrupt_may_hang = true,
Russell King38d62432010-12-22 17:59:16 +0000122 .dma_threshold = true,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123};
124
Shreshtha Kumar Sahuc16d51a2011-06-13 10:11:33 +0200125static struct uart_amba_port *amba_ports[UART_NR];
126
Russell King68b65f72010-12-22 17:24:39 +0000127/* Deals with DMA transactions */
Linus Walleijead76f32011-02-24 13:21:08 +0100128
129struct pl011_sgbuf {
130 struct scatterlist sg;
131 char *buf;
132};
133
134struct pl011_dmarx_data {
135 struct dma_chan *chan;
136 struct completion complete;
137 bool use_buf_b;
138 struct pl011_sgbuf sgbuf_a;
139 struct pl011_sgbuf sgbuf_b;
140 dma_cookie_t cookie;
141 bool running;
142};
143
Russell King68b65f72010-12-22 17:24:39 +0000144struct pl011_dmatx_data {
145 struct dma_chan *chan;
146 struct scatterlist sg;
147 char *buf;
148 bool queued;
149};
150
Russell Kingc19f12b2010-12-22 17:48:26 +0000151/*
152 * We wrap our port structure around the generic uart_port.
153 */
154struct uart_amba_port {
155 struct uart_port port;
156 struct clk *clk;
157 const struct vendor_data *vendor;
Russell King68b65f72010-12-22 17:24:39 +0000158 unsigned int dmacr; /* dma control reg */
Russell Kingc19f12b2010-12-22 17:48:26 +0000159 unsigned int im; /* interrupt mask */
160 unsigned int old_status;
Russell Kingffca2b12010-12-22 17:13:05 +0000161 unsigned int fifosize; /* vendor-specific */
Russell Kingc19f12b2010-12-22 17:48:26 +0000162 unsigned int lcrh_tx; /* vendor-specific */
163 unsigned int lcrh_rx; /* vendor-specific */
Shreshtha Kumar Sahud8d8ffa2012-01-18 15:53:59 +0530164 unsigned int old_cr; /* state during shutdown */
Russell Kingc19f12b2010-12-22 17:48:26 +0000165 bool autorts;
166 char type[12];
Shreshtha Kumar Sahuc16d51a2011-06-13 10:11:33 +0200167 bool interrupt_may_hang; /* vendor-specific */
Russell King68b65f72010-12-22 17:24:39 +0000168#ifdef CONFIG_DMA_ENGINE
169 /* DMA stuff */
Linus Walleijead76f32011-02-24 13:21:08 +0100170 bool using_tx_dma;
171 bool using_rx_dma;
172 struct pl011_dmarx_data dmarx;
Russell King68b65f72010-12-22 17:24:39 +0000173 struct pl011_dmatx_data dmatx;
174#endif
Russell Kingc19f12b2010-12-22 17:48:26 +0000175};
176
Russell King68b65f72010-12-22 17:24:39 +0000177/*
Linus Walleij29772c42011-02-24 13:21:36 +0100178 * Reads up to 256 characters from the FIFO or until it's empty and
179 * inserts them into the TTY layer. Returns the number of characters
180 * read from the FIFO.
181 */
182static int pl011_fifo_to_tty(struct uart_amba_port *uap)
183{
184 u16 status, ch;
185 unsigned int flag, max_count = 256;
186 int fifotaken = 0;
187
188 while (max_count--) {
189 status = readw(uap->port.membase + UART01x_FR);
190 if (status & UART01x_FR_RXFE)
191 break;
192
193 /* Take chars from the FIFO and update status */
194 ch = readw(uap->port.membase + UART01x_DR) |
195 UART_DUMMY_DR_RX;
196 flag = TTY_NORMAL;
197 uap->port.icount.rx++;
198 fifotaken++;
199
200 if (unlikely(ch & UART_DR_ERROR)) {
201 if (ch & UART011_DR_BE) {
202 ch &= ~(UART011_DR_FE | UART011_DR_PE);
203 uap->port.icount.brk++;
204 if (uart_handle_break(&uap->port))
205 continue;
206 } else if (ch & UART011_DR_PE)
207 uap->port.icount.parity++;
208 else if (ch & UART011_DR_FE)
209 uap->port.icount.frame++;
210 if (ch & UART011_DR_OE)
211 uap->port.icount.overrun++;
212
213 ch &= uap->port.read_status_mask;
214
215 if (ch & UART011_DR_BE)
216 flag = TTY_BREAK;
217 else if (ch & UART011_DR_PE)
218 flag = TTY_PARITY;
219 else if (ch & UART011_DR_FE)
220 flag = TTY_FRAME;
221 }
222
223 if (uart_handle_sysrq_char(&uap->port, ch & 255))
224 continue;
225
226 uart_insert_char(&uap->port, ch, UART011_DR_OE, ch, flag);
227 }
228
229 return fifotaken;
230}
231
232
233/*
Russell King68b65f72010-12-22 17:24:39 +0000234 * All the DMA operation mode stuff goes inside this ifdef.
235 * This assumes that you have a generic DMA device interface,
236 * no custom DMA interfaces are supported.
237 */
238#ifdef CONFIG_DMA_ENGINE
239
240#define PL011_DMA_BUFFER_SIZE PAGE_SIZE
241
Linus Walleijead76f32011-02-24 13:21:08 +0100242static int pl011_sgbuf_init(struct dma_chan *chan, struct pl011_sgbuf *sg,
243 enum dma_data_direction dir)
244{
245 sg->buf = kmalloc(PL011_DMA_BUFFER_SIZE, GFP_KERNEL);
246 if (!sg->buf)
247 return -ENOMEM;
248
249 sg_init_one(&sg->sg, sg->buf, PL011_DMA_BUFFER_SIZE);
250
251 if (dma_map_sg(chan->device->dev, &sg->sg, 1, dir) != 1) {
252 kfree(sg->buf);
253 return -EINVAL;
254 }
255 return 0;
256}
257
258static void pl011_sgbuf_free(struct dma_chan *chan, struct pl011_sgbuf *sg,
259 enum dma_data_direction dir)
260{
261 if (sg->buf) {
262 dma_unmap_sg(chan->device->dev, &sg->sg, 1, dir);
263 kfree(sg->buf);
264 }
265}
266
Russell King68b65f72010-12-22 17:24:39 +0000267static void pl011_dma_probe_initcall(struct uart_amba_port *uap)
268{
269 /* DMA is the sole user of the platform data right now */
270 struct amba_pl011_data *plat = uap->port.dev->platform_data;
271 struct dma_slave_config tx_conf = {
272 .dst_addr = uap->port.mapbase + UART01x_DR,
273 .dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE,
Vinod Koula485df42011-10-14 10:47:38 +0530274 .direction = DMA_MEM_TO_DEV,
Russell King68b65f72010-12-22 17:24:39 +0000275 .dst_maxburst = uap->fifosize >> 1,
Viresh Kumar258aea72012-02-01 16:12:19 +0530276 .device_fc = false,
Russell King68b65f72010-12-22 17:24:39 +0000277 };
278 struct dma_chan *chan;
279 dma_cap_mask_t mask;
280
281 /* We need platform data */
282 if (!plat || !plat->dma_filter) {
283 dev_info(uap->port.dev, "no DMA platform data\n");
284 return;
285 }
286
Linus Walleijead76f32011-02-24 13:21:08 +0100287 /* Try to acquire a generic DMA engine slave TX channel */
Russell King68b65f72010-12-22 17:24:39 +0000288 dma_cap_zero(mask);
289 dma_cap_set(DMA_SLAVE, mask);
290
291 chan = dma_request_channel(mask, plat->dma_filter, plat->dma_tx_param);
292 if (!chan) {
293 dev_err(uap->port.dev, "no TX DMA channel!\n");
294 return;
295 }
296
297 dmaengine_slave_config(chan, &tx_conf);
298 uap->dmatx.chan = chan;
299
300 dev_info(uap->port.dev, "DMA channel TX %s\n",
301 dma_chan_name(uap->dmatx.chan));
Linus Walleijead76f32011-02-24 13:21:08 +0100302
303 /* Optionally make use of an RX channel as well */
304 if (plat->dma_rx_param) {
305 struct dma_slave_config rx_conf = {
306 .src_addr = uap->port.mapbase + UART01x_DR,
307 .src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE,
Vinod Koula485df42011-10-14 10:47:38 +0530308 .direction = DMA_DEV_TO_MEM,
Linus Walleijead76f32011-02-24 13:21:08 +0100309 .src_maxburst = uap->fifosize >> 1,
Viresh Kumar258aea72012-02-01 16:12:19 +0530310 .device_fc = false,
Linus Walleijead76f32011-02-24 13:21:08 +0100311 };
312
313 chan = dma_request_channel(mask, plat->dma_filter, plat->dma_rx_param);
314 if (!chan) {
315 dev_err(uap->port.dev, "no RX DMA channel!\n");
316 return;
317 }
318
319 dmaengine_slave_config(chan, &rx_conf);
320 uap->dmarx.chan = chan;
321
322 dev_info(uap->port.dev, "DMA channel RX %s\n",
323 dma_chan_name(uap->dmarx.chan));
324 }
Russell King68b65f72010-12-22 17:24:39 +0000325}
326
327#ifndef MODULE
328/*
329 * Stack up the UARTs and let the above initcall be done at device
330 * initcall time, because the serial driver is called as an arch
331 * initcall, and at this time the DMA subsystem is not yet registered.
332 * At this point the driver will switch over to using DMA where desired.
333 */
334struct dma_uap {
335 struct list_head node;
336 struct uart_amba_port *uap;
337};
338
339static LIST_HEAD(pl011_dma_uarts);
340
341static int __init pl011_dma_initcall(void)
342{
343 struct list_head *node, *tmp;
344
345 list_for_each_safe(node, tmp, &pl011_dma_uarts) {
346 struct dma_uap *dmau = list_entry(node, struct dma_uap, node);
347 pl011_dma_probe_initcall(dmau->uap);
348 list_del(node);
349 kfree(dmau);
350 }
351 return 0;
352}
353
354device_initcall(pl011_dma_initcall);
355
356static void pl011_dma_probe(struct uart_amba_port *uap)
357{
358 struct dma_uap *dmau = kzalloc(sizeof(struct dma_uap), GFP_KERNEL);
359 if (dmau) {
360 dmau->uap = uap;
361 list_add_tail(&dmau->node, &pl011_dma_uarts);
362 }
363}
364#else
365static void pl011_dma_probe(struct uart_amba_port *uap)
366{
367 pl011_dma_probe_initcall(uap);
368}
369#endif
370
371static void pl011_dma_remove(struct uart_amba_port *uap)
372{
373 /* TODO: remove the initcall if it has not yet executed */
374 if (uap->dmatx.chan)
375 dma_release_channel(uap->dmatx.chan);
Linus Walleijead76f32011-02-24 13:21:08 +0100376 if (uap->dmarx.chan)
377 dma_release_channel(uap->dmarx.chan);
Russell King68b65f72010-12-22 17:24:39 +0000378}
379
Russell King68b65f72010-12-22 17:24:39 +0000380/* Forward declare this for the refill routine */
381static int pl011_dma_tx_refill(struct uart_amba_port *uap);
382
383/*
384 * The current DMA TX buffer has been sent.
385 * Try to queue up another DMA buffer.
386 */
387static void pl011_dma_tx_callback(void *data)
388{
389 struct uart_amba_port *uap = data;
390 struct pl011_dmatx_data *dmatx = &uap->dmatx;
391 unsigned long flags;
392 u16 dmacr;
393
394 spin_lock_irqsave(&uap->port.lock, flags);
395 if (uap->dmatx.queued)
396 dma_unmap_sg(dmatx->chan->device->dev, &dmatx->sg, 1,
397 DMA_TO_DEVICE);
398
399 dmacr = uap->dmacr;
400 uap->dmacr = dmacr & ~UART011_TXDMAE;
401 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
402
403 /*
404 * If TX DMA was disabled, it means that we've stopped the DMA for
405 * some reason (eg, XOFF received, or we want to send an X-char.)
406 *
407 * Note: we need to be careful here of a potential race between DMA
408 * and the rest of the driver - if the driver disables TX DMA while
409 * a TX buffer completing, we must update the tx queued status to
410 * get further refills (hence we check dmacr).
411 */
412 if (!(dmacr & UART011_TXDMAE) || uart_tx_stopped(&uap->port) ||
413 uart_circ_empty(&uap->port.state->xmit)) {
414 uap->dmatx.queued = false;
415 spin_unlock_irqrestore(&uap->port.lock, flags);
416 return;
417 }
418
419 if (pl011_dma_tx_refill(uap) <= 0) {
420 /*
421 * We didn't queue a DMA buffer for some reason, but we
422 * have data pending to be sent. Re-enable the TX IRQ.
423 */
424 uap->im |= UART011_TXIM;
425 writew(uap->im, uap->port.membase + UART011_IMSC);
426 }
427 spin_unlock_irqrestore(&uap->port.lock, flags);
428}
429
430/*
431 * Try to refill the TX DMA buffer.
432 * Locking: called with port lock held and IRQs disabled.
433 * Returns:
434 * 1 if we queued up a TX DMA buffer.
435 * 0 if we didn't want to handle this by DMA
436 * <0 on error
437 */
438static int pl011_dma_tx_refill(struct uart_amba_port *uap)
439{
440 struct pl011_dmatx_data *dmatx = &uap->dmatx;
441 struct dma_chan *chan = dmatx->chan;
442 struct dma_device *dma_dev = chan->device;
443 struct dma_async_tx_descriptor *desc;
444 struct circ_buf *xmit = &uap->port.state->xmit;
445 unsigned int count;
446
447 /*
448 * Try to avoid the overhead involved in using DMA if the
449 * transaction fits in the first half of the FIFO, by using
450 * the standard interrupt handling. This ensures that we
451 * issue a uart_write_wakeup() at the appropriate time.
452 */
453 count = uart_circ_chars_pending(xmit);
454 if (count < (uap->fifosize >> 1)) {
455 uap->dmatx.queued = false;
456 return 0;
457 }
458
459 /*
460 * Bodge: don't send the last character by DMA, as this
461 * will prevent XON from notifying us to restart DMA.
462 */
463 count -= 1;
464
465 /* Else proceed to copy the TX chars to the DMA buffer and fire DMA */
466 if (count > PL011_DMA_BUFFER_SIZE)
467 count = PL011_DMA_BUFFER_SIZE;
468
469 if (xmit->tail < xmit->head)
470 memcpy(&dmatx->buf[0], &xmit->buf[xmit->tail], count);
471 else {
472 size_t first = UART_XMIT_SIZE - xmit->tail;
473 size_t second = xmit->head;
474
475 memcpy(&dmatx->buf[0], &xmit->buf[xmit->tail], first);
476 if (second)
477 memcpy(&dmatx->buf[first], &xmit->buf[0], second);
478 }
479
480 dmatx->sg.length = count;
481
482 if (dma_map_sg(dma_dev->dev, &dmatx->sg, 1, DMA_TO_DEVICE) != 1) {
483 uap->dmatx.queued = false;
484 dev_dbg(uap->port.dev, "unable to map TX DMA\n");
485 return -EBUSY;
486 }
487
Alexandre Bounine16052822012-03-08 16:11:18 -0500488 desc = dmaengine_prep_slave_sg(chan, &dmatx->sg, 1, DMA_MEM_TO_DEV,
Russell King68b65f72010-12-22 17:24:39 +0000489 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
490 if (!desc) {
491 dma_unmap_sg(dma_dev->dev, &dmatx->sg, 1, DMA_TO_DEVICE);
492 uap->dmatx.queued = false;
493 /*
494 * If DMA cannot be used right now, we complete this
495 * transaction via IRQ and let the TTY layer retry.
496 */
497 dev_dbg(uap->port.dev, "TX DMA busy\n");
498 return -EBUSY;
499 }
500
501 /* Some data to go along to the callback */
502 desc->callback = pl011_dma_tx_callback;
503 desc->callback_param = uap;
504
505 /* All errors should happen at prepare time */
506 dmaengine_submit(desc);
507
508 /* Fire the DMA transaction */
509 dma_dev->device_issue_pending(chan);
510
511 uap->dmacr |= UART011_TXDMAE;
512 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
513 uap->dmatx.queued = true;
514
515 /*
516 * Now we know that DMA will fire, so advance the ring buffer
517 * with the stuff we just dispatched.
518 */
519 xmit->tail = (xmit->tail + count) & (UART_XMIT_SIZE - 1);
520 uap->port.icount.tx += count;
521
522 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
523 uart_write_wakeup(&uap->port);
524
525 return 1;
526}
527
528/*
529 * We received a transmit interrupt without a pending X-char but with
530 * pending characters.
531 * Locking: called with port lock held and IRQs disabled.
532 * Returns:
533 * false if we want to use PIO to transmit
534 * true if we queued a DMA buffer
535 */
536static bool pl011_dma_tx_irq(struct uart_amba_port *uap)
537{
Linus Walleijead76f32011-02-24 13:21:08 +0100538 if (!uap->using_tx_dma)
Russell King68b65f72010-12-22 17:24:39 +0000539 return false;
540
541 /*
542 * If we already have a TX buffer queued, but received a
543 * TX interrupt, it will be because we've just sent an X-char.
544 * Ensure the TX DMA is enabled and the TX IRQ is disabled.
545 */
546 if (uap->dmatx.queued) {
547 uap->dmacr |= UART011_TXDMAE;
548 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
549 uap->im &= ~UART011_TXIM;
550 writew(uap->im, uap->port.membase + UART011_IMSC);
551 return true;
552 }
553
554 /*
555 * We don't have a TX buffer queued, so try to queue one.
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300556 * If we successfully queued a buffer, mask the TX IRQ.
Russell King68b65f72010-12-22 17:24:39 +0000557 */
558 if (pl011_dma_tx_refill(uap) > 0) {
559 uap->im &= ~UART011_TXIM;
560 writew(uap->im, uap->port.membase + UART011_IMSC);
561 return true;
562 }
563 return false;
564}
565
566/*
567 * Stop the DMA transmit (eg, due to received XOFF).
568 * Locking: called with port lock held and IRQs disabled.
569 */
570static inline void pl011_dma_tx_stop(struct uart_amba_port *uap)
571{
572 if (uap->dmatx.queued) {
573 uap->dmacr &= ~UART011_TXDMAE;
574 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
575 }
576}
577
578/*
579 * Try to start a DMA transmit, or in the case of an XON/OFF
580 * character queued for send, try to get that character out ASAP.
581 * Locking: called with port lock held and IRQs disabled.
582 * Returns:
583 * false if we want the TX IRQ to be enabled
584 * true if we have a buffer queued
585 */
586static inline bool pl011_dma_tx_start(struct uart_amba_port *uap)
587{
588 u16 dmacr;
589
Linus Walleijead76f32011-02-24 13:21:08 +0100590 if (!uap->using_tx_dma)
Russell King68b65f72010-12-22 17:24:39 +0000591 return false;
592
593 if (!uap->port.x_char) {
594 /* no X-char, try to push chars out in DMA mode */
595 bool ret = true;
596
597 if (!uap->dmatx.queued) {
598 if (pl011_dma_tx_refill(uap) > 0) {
599 uap->im &= ~UART011_TXIM;
600 ret = true;
601 } else {
602 uap->im |= UART011_TXIM;
603 ret = false;
604 }
605 writew(uap->im, uap->port.membase + UART011_IMSC);
606 } else if (!(uap->dmacr & UART011_TXDMAE)) {
607 uap->dmacr |= UART011_TXDMAE;
608 writew(uap->dmacr,
609 uap->port.membase + UART011_DMACR);
610 }
611 return ret;
612 }
613
614 /*
615 * We have an X-char to send. Disable DMA to prevent it loading
616 * the TX fifo, and then see if we can stuff it into the FIFO.
617 */
618 dmacr = uap->dmacr;
619 uap->dmacr &= ~UART011_TXDMAE;
620 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
621
622 if (readw(uap->port.membase + UART01x_FR) & UART01x_FR_TXFF) {
623 /*
624 * No space in the FIFO, so enable the transmit interrupt
625 * so we know when there is space. Note that once we've
626 * loaded the character, we should just re-enable DMA.
627 */
628 return false;
629 }
630
631 writew(uap->port.x_char, uap->port.membase + UART01x_DR);
632 uap->port.icount.tx++;
633 uap->port.x_char = 0;
634
635 /* Success - restore the DMA state */
636 uap->dmacr = dmacr;
637 writew(dmacr, uap->port.membase + UART011_DMACR);
638
639 return true;
640}
641
642/*
643 * Flush the transmit buffer.
644 * Locking: called with port lock held and IRQs disabled.
645 */
646static void pl011_dma_flush_buffer(struct uart_port *port)
647{
648 struct uart_amba_port *uap = (struct uart_amba_port *)port;
649
Linus Walleijead76f32011-02-24 13:21:08 +0100650 if (!uap->using_tx_dma)
Russell King68b65f72010-12-22 17:24:39 +0000651 return;
652
653 /* Avoid deadlock with the DMA engine callback */
654 spin_unlock(&uap->port.lock);
655 dmaengine_terminate_all(uap->dmatx.chan);
656 spin_lock(&uap->port.lock);
657 if (uap->dmatx.queued) {
658 dma_unmap_sg(uap->dmatx.chan->device->dev, &uap->dmatx.sg, 1,
659 DMA_TO_DEVICE);
660 uap->dmatx.queued = false;
661 uap->dmacr &= ~UART011_TXDMAE;
662 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
663 }
664}
665
Linus Walleijead76f32011-02-24 13:21:08 +0100666static void pl011_dma_rx_callback(void *data);
667
668static int pl011_dma_rx_trigger_dma(struct uart_amba_port *uap)
669{
670 struct dma_chan *rxchan = uap->dmarx.chan;
Linus Walleijead76f32011-02-24 13:21:08 +0100671 struct pl011_dmarx_data *dmarx = &uap->dmarx;
672 struct dma_async_tx_descriptor *desc;
673 struct pl011_sgbuf *sgbuf;
674
675 if (!rxchan)
676 return -EIO;
677
678 /* Start the RX DMA job */
679 sgbuf = uap->dmarx.use_buf_b ?
680 &uap->dmarx.sgbuf_b : &uap->dmarx.sgbuf_a;
Alexandre Bounine16052822012-03-08 16:11:18 -0500681 desc = dmaengine_prep_slave_sg(rxchan, &sgbuf->sg, 1,
Vinod Koula485df42011-10-14 10:47:38 +0530682 DMA_DEV_TO_MEM,
Linus Walleijead76f32011-02-24 13:21:08 +0100683 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
684 /*
685 * If the DMA engine is busy and cannot prepare a
686 * channel, no big deal, the driver will fall back
687 * to interrupt mode as a result of this error code.
688 */
689 if (!desc) {
690 uap->dmarx.running = false;
691 dmaengine_terminate_all(rxchan);
692 return -EBUSY;
693 }
694
695 /* Some data to go along to the callback */
696 desc->callback = pl011_dma_rx_callback;
697 desc->callback_param = uap;
698 dmarx->cookie = dmaengine_submit(desc);
699 dma_async_issue_pending(rxchan);
700
701 uap->dmacr |= UART011_RXDMAE;
702 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
703 uap->dmarx.running = true;
704
705 uap->im &= ~UART011_RXIM;
706 writew(uap->im, uap->port.membase + UART011_IMSC);
707
708 return 0;
709}
710
711/*
712 * This is called when either the DMA job is complete, or
713 * the FIFO timeout interrupt occurred. This must be called
714 * with the port spinlock uap->port.lock held.
715 */
716static void pl011_dma_rx_chars(struct uart_amba_port *uap,
717 u32 pending, bool use_buf_b,
718 bool readfifo)
719{
720 struct tty_struct *tty = uap->port.state->port.tty;
721 struct pl011_sgbuf *sgbuf = use_buf_b ?
722 &uap->dmarx.sgbuf_b : &uap->dmarx.sgbuf_a;
723 struct device *dev = uap->dmarx.chan->device->dev;
Linus Walleijead76f32011-02-24 13:21:08 +0100724 int dma_count = 0;
725 u32 fifotaken = 0; /* only used for vdbg() */
726
727 /* Pick everything from the DMA first */
728 if (pending) {
729 /* Sync in buffer */
730 dma_sync_sg_for_cpu(dev, &sgbuf->sg, 1, DMA_FROM_DEVICE);
731
732 /*
733 * First take all chars in the DMA pipe, then look in the FIFO.
734 * Note that tty_insert_flip_buf() tries to take as many chars
735 * as it can.
736 */
737 dma_count = tty_insert_flip_string(uap->port.state->port.tty,
738 sgbuf->buf, pending);
739
740 /* Return buffer to device */
741 dma_sync_sg_for_device(dev, &sgbuf->sg, 1, DMA_FROM_DEVICE);
742
743 uap->port.icount.rx += dma_count;
744 if (dma_count < pending)
745 dev_warn(uap->port.dev,
746 "couldn't insert all characters (TTY is full?)\n");
747 }
748
749 /*
750 * Only continue with trying to read the FIFO if all DMA chars have
751 * been taken first.
752 */
753 if (dma_count == pending && readfifo) {
754 /* Clear any error flags */
755 writew(UART011_OEIS | UART011_BEIS | UART011_PEIS | UART011_FEIS,
756 uap->port.membase + UART011_ICR);
757
758 /*
759 * If we read all the DMA'd characters, and we had an
Linus Walleij29772c42011-02-24 13:21:36 +0100760 * incomplete buffer, that could be due to an rx error, or
761 * maybe we just timed out. Read any pending chars and check
762 * the error status.
763 *
764 * Error conditions will only occur in the FIFO, these will
765 * trigger an immediate interrupt and stop the DMA job, so we
766 * will always find the error in the FIFO, never in the DMA
767 * buffer.
Linus Walleijead76f32011-02-24 13:21:08 +0100768 */
Linus Walleij29772c42011-02-24 13:21:36 +0100769 fifotaken = pl011_fifo_to_tty(uap);
Linus Walleijead76f32011-02-24 13:21:08 +0100770 }
771
772 spin_unlock(&uap->port.lock);
773 dev_vdbg(uap->port.dev,
774 "Took %d chars from DMA buffer and %d chars from the FIFO\n",
775 dma_count, fifotaken);
776 tty_flip_buffer_push(tty);
777 spin_lock(&uap->port.lock);
778}
779
780static void pl011_dma_rx_irq(struct uart_amba_port *uap)
781{
782 struct pl011_dmarx_data *dmarx = &uap->dmarx;
783 struct dma_chan *rxchan = dmarx->chan;
784 struct pl011_sgbuf *sgbuf = dmarx->use_buf_b ?
785 &dmarx->sgbuf_b : &dmarx->sgbuf_a;
786 size_t pending;
787 struct dma_tx_state state;
788 enum dma_status dmastat;
789
790 /*
791 * Pause the transfer so we can trust the current counter,
792 * do this before we pause the PL011 block, else we may
793 * overflow the FIFO.
794 */
795 if (dmaengine_pause(rxchan))
796 dev_err(uap->port.dev, "unable to pause DMA transfer\n");
797 dmastat = rxchan->device->device_tx_status(rxchan,
798 dmarx->cookie, &state);
799 if (dmastat != DMA_PAUSED)
800 dev_err(uap->port.dev, "unable to pause DMA transfer\n");
801
802 /* Disable RX DMA - incoming data will wait in the FIFO */
803 uap->dmacr &= ~UART011_RXDMAE;
804 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
805 uap->dmarx.running = false;
806
807 pending = sgbuf->sg.length - state.residue;
808 BUG_ON(pending > PL011_DMA_BUFFER_SIZE);
809 /* Then we terminate the transfer - we now know our residue */
810 dmaengine_terminate_all(rxchan);
811
812 /*
813 * This will take the chars we have so far and insert
814 * into the framework.
815 */
816 pl011_dma_rx_chars(uap, pending, dmarx->use_buf_b, true);
817
818 /* Switch buffer & re-trigger DMA job */
819 dmarx->use_buf_b = !dmarx->use_buf_b;
820 if (pl011_dma_rx_trigger_dma(uap)) {
821 dev_dbg(uap->port.dev, "could not retrigger RX DMA job "
822 "fall back to interrupt mode\n");
823 uap->im |= UART011_RXIM;
824 writew(uap->im, uap->port.membase + UART011_IMSC);
825 }
826}
827
828static void pl011_dma_rx_callback(void *data)
829{
830 struct uart_amba_port *uap = data;
831 struct pl011_dmarx_data *dmarx = &uap->dmarx;
Chanho Min6dc01aa2012-02-20 10:24:40 +0900832 struct dma_chan *rxchan = dmarx->chan;
Linus Walleijead76f32011-02-24 13:21:08 +0100833 bool lastbuf = dmarx->use_buf_b;
Chanho Min6dc01aa2012-02-20 10:24:40 +0900834 struct pl011_sgbuf *sgbuf = dmarx->use_buf_b ?
835 &dmarx->sgbuf_b : &dmarx->sgbuf_a;
836 size_t pending;
837 struct dma_tx_state state;
Linus Walleijead76f32011-02-24 13:21:08 +0100838 int ret;
839
840 /*
841 * This completion interrupt occurs typically when the
842 * RX buffer is totally stuffed but no timeout has yet
843 * occurred. When that happens, we just want the RX
844 * routine to flush out the secondary DMA buffer while
845 * we immediately trigger the next DMA job.
846 */
847 spin_lock_irq(&uap->port.lock);
Chanho Min6dc01aa2012-02-20 10:24:40 +0900848 /*
849 * Rx data can be taken by the UART interrupts during
850 * the DMA irq handler. So we check the residue here.
851 */
852 rxchan->device->device_tx_status(rxchan, dmarx->cookie, &state);
853 pending = sgbuf->sg.length - state.residue;
854 BUG_ON(pending > PL011_DMA_BUFFER_SIZE);
855 /* Then we terminate the transfer - we now know our residue */
856 dmaengine_terminate_all(rxchan);
857
Linus Walleijead76f32011-02-24 13:21:08 +0100858 uap->dmarx.running = false;
859 dmarx->use_buf_b = !lastbuf;
860 ret = pl011_dma_rx_trigger_dma(uap);
861
Chanho Min6dc01aa2012-02-20 10:24:40 +0900862 pl011_dma_rx_chars(uap, pending, lastbuf, false);
Linus Walleijead76f32011-02-24 13:21:08 +0100863 spin_unlock_irq(&uap->port.lock);
864 /*
865 * Do this check after we picked the DMA chars so we don't
866 * get some IRQ immediately from RX.
867 */
868 if (ret) {
869 dev_dbg(uap->port.dev, "could not retrigger RX DMA job "
870 "fall back to interrupt mode\n");
871 uap->im |= UART011_RXIM;
872 writew(uap->im, uap->port.membase + UART011_IMSC);
873 }
874}
875
876/*
877 * Stop accepting received characters, when we're shutting down or
878 * suspending this port.
879 * Locking: called with port lock held and IRQs disabled.
880 */
881static inline void pl011_dma_rx_stop(struct uart_amba_port *uap)
882{
883 /* FIXME. Just disable the DMA enable */
884 uap->dmacr &= ~UART011_RXDMAE;
885 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
886}
Russell King68b65f72010-12-22 17:24:39 +0000887
888static void pl011_dma_startup(struct uart_amba_port *uap)
889{
Linus Walleijead76f32011-02-24 13:21:08 +0100890 int ret;
891
Russell King68b65f72010-12-22 17:24:39 +0000892 if (!uap->dmatx.chan)
893 return;
894
895 uap->dmatx.buf = kmalloc(PL011_DMA_BUFFER_SIZE, GFP_KERNEL);
896 if (!uap->dmatx.buf) {
897 dev_err(uap->port.dev, "no memory for DMA TX buffer\n");
898 uap->port.fifosize = uap->fifosize;
899 return;
900 }
901
902 sg_init_one(&uap->dmatx.sg, uap->dmatx.buf, PL011_DMA_BUFFER_SIZE);
903
904 /* The DMA buffer is now the FIFO the TTY subsystem can use */
905 uap->port.fifosize = PL011_DMA_BUFFER_SIZE;
Linus Walleijead76f32011-02-24 13:21:08 +0100906 uap->using_tx_dma = true;
Russell King68b65f72010-12-22 17:24:39 +0000907
Linus Walleijead76f32011-02-24 13:21:08 +0100908 if (!uap->dmarx.chan)
909 goto skip_rx;
910
911 /* Allocate and map DMA RX buffers */
912 ret = pl011_sgbuf_init(uap->dmarx.chan, &uap->dmarx.sgbuf_a,
913 DMA_FROM_DEVICE);
914 if (ret) {
915 dev_err(uap->port.dev, "failed to init DMA %s: %d\n",
916 "RX buffer A", ret);
917 goto skip_rx;
918 }
919
920 ret = pl011_sgbuf_init(uap->dmarx.chan, &uap->dmarx.sgbuf_b,
921 DMA_FROM_DEVICE);
922 if (ret) {
923 dev_err(uap->port.dev, "failed to init DMA %s: %d\n",
924 "RX buffer B", ret);
925 pl011_sgbuf_free(uap->dmarx.chan, &uap->dmarx.sgbuf_a,
926 DMA_FROM_DEVICE);
927 goto skip_rx;
928 }
929
930 uap->using_rx_dma = true;
931
932skip_rx:
Russell King68b65f72010-12-22 17:24:39 +0000933 /* Turn on DMA error (RX/TX will be enabled on demand) */
934 uap->dmacr |= UART011_DMAONERR;
935 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
Russell King38d62432010-12-22 17:59:16 +0000936
937 /*
938 * ST Micro variants has some specific dma burst threshold
939 * compensation. Set this to 16 bytes, so burst will only
940 * be issued above/below 16 bytes.
941 */
942 if (uap->vendor->dma_threshold)
943 writew(ST_UART011_DMAWM_RX_16 | ST_UART011_DMAWM_TX_16,
944 uap->port.membase + ST_UART011_DMAWM);
Linus Walleijead76f32011-02-24 13:21:08 +0100945
946 if (uap->using_rx_dma) {
947 if (pl011_dma_rx_trigger_dma(uap))
948 dev_dbg(uap->port.dev, "could not trigger initial "
949 "RX DMA job, fall back to interrupt mode\n");
950 }
Russell King68b65f72010-12-22 17:24:39 +0000951}
952
953static void pl011_dma_shutdown(struct uart_amba_port *uap)
954{
Linus Walleijead76f32011-02-24 13:21:08 +0100955 if (!(uap->using_tx_dma || uap->using_rx_dma))
Russell King68b65f72010-12-22 17:24:39 +0000956 return;
957
958 /* Disable RX and TX DMA */
959 while (readw(uap->port.membase + UART01x_FR) & UART01x_FR_BUSY)
960 barrier();
961
962 spin_lock_irq(&uap->port.lock);
963 uap->dmacr &= ~(UART011_DMAONERR | UART011_RXDMAE | UART011_TXDMAE);
964 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
965 spin_unlock_irq(&uap->port.lock);
966
Linus Walleijead76f32011-02-24 13:21:08 +0100967 if (uap->using_tx_dma) {
968 /* In theory, this should already be done by pl011_dma_flush_buffer */
969 dmaengine_terminate_all(uap->dmatx.chan);
970 if (uap->dmatx.queued) {
971 dma_unmap_sg(uap->dmatx.chan->device->dev, &uap->dmatx.sg, 1,
972 DMA_TO_DEVICE);
973 uap->dmatx.queued = false;
974 }
975
976 kfree(uap->dmatx.buf);
977 uap->using_tx_dma = false;
Russell King68b65f72010-12-22 17:24:39 +0000978 }
979
Linus Walleijead76f32011-02-24 13:21:08 +0100980 if (uap->using_rx_dma) {
981 dmaengine_terminate_all(uap->dmarx.chan);
982 /* Clean up the RX DMA */
983 pl011_sgbuf_free(uap->dmarx.chan, &uap->dmarx.sgbuf_a, DMA_FROM_DEVICE);
984 pl011_sgbuf_free(uap->dmarx.chan, &uap->dmarx.sgbuf_b, DMA_FROM_DEVICE);
985 uap->using_rx_dma = false;
986 }
Russell King68b65f72010-12-22 17:24:39 +0000987}
988
Linus Walleijead76f32011-02-24 13:21:08 +0100989static inline bool pl011_dma_rx_available(struct uart_amba_port *uap)
990{
991 return uap->using_rx_dma;
992}
993
994static inline bool pl011_dma_rx_running(struct uart_amba_port *uap)
995{
996 return uap->using_rx_dma && uap->dmarx.running;
997}
998
999
Russell King68b65f72010-12-22 17:24:39 +00001000#else
1001/* Blank functions if the DMA engine is not available */
1002static inline void pl011_dma_probe(struct uart_amba_port *uap)
1003{
1004}
1005
1006static inline void pl011_dma_remove(struct uart_amba_port *uap)
1007{
1008}
1009
1010static inline void pl011_dma_startup(struct uart_amba_port *uap)
1011{
1012}
1013
1014static inline void pl011_dma_shutdown(struct uart_amba_port *uap)
1015{
1016}
1017
1018static inline bool pl011_dma_tx_irq(struct uart_amba_port *uap)
1019{
1020 return false;
1021}
1022
1023static inline void pl011_dma_tx_stop(struct uart_amba_port *uap)
1024{
1025}
1026
1027static inline bool pl011_dma_tx_start(struct uart_amba_port *uap)
1028{
1029 return false;
1030}
1031
Linus Walleijead76f32011-02-24 13:21:08 +01001032static inline void pl011_dma_rx_irq(struct uart_amba_port *uap)
1033{
1034}
1035
1036static inline void pl011_dma_rx_stop(struct uart_amba_port *uap)
1037{
1038}
1039
1040static inline int pl011_dma_rx_trigger_dma(struct uart_amba_port *uap)
1041{
1042 return -EIO;
1043}
1044
1045static inline bool pl011_dma_rx_available(struct uart_amba_port *uap)
1046{
1047 return false;
1048}
1049
1050static inline bool pl011_dma_rx_running(struct uart_amba_port *uap)
1051{
1052 return false;
1053}
1054
Russell King68b65f72010-12-22 17:24:39 +00001055#define pl011_dma_flush_buffer NULL
1056#endif
1057
1058
Shreshtha Kumar Sahuc16d51a2011-06-13 10:11:33 +02001059/*
1060 * pl011_lockup_wa
1061 * This workaround aims to break the deadlock situation
1062 * when after long transfer over uart in hardware flow
1063 * control, uart interrupt registers cannot be cleared.
1064 * Hence uart transfer gets blocked.
1065 *
1066 * It is seen that during such deadlock condition ICR
1067 * don't get cleared even on multiple write. This leads
1068 * pass_counter to decrease and finally reach zero. This
1069 * can be taken as trigger point to run this UART_BT_WA.
1070 *
1071 */
1072static void pl011_lockup_wa(unsigned long data)
1073{
1074 struct uart_amba_port *uap = amba_ports[0];
1075 void __iomem *base = uap->port.membase;
1076 struct circ_buf *xmit = &uap->port.state->xmit;
1077 struct tty_struct *tty = uap->port.state->port.tty;
1078 int buf_empty_retries = 200;
1079 int loop;
1080
1081 /* Stop HCI layer from submitting data for tx */
1082 tty->hw_stopped = 1;
1083 while (!uart_circ_empty(xmit)) {
1084 if (buf_empty_retries-- == 0)
1085 break;
1086 udelay(100);
1087 }
1088
1089 /* Backup registers */
1090 for (loop = 0; loop < UART_WA_SAVE_NR; loop++)
1091 uart_wa_regdata[loop] = readl(base + uart_wa_reg[loop]);
1092
1093 /* Disable UART so that FIFO data is flushed out */
1094 writew(0x00, uap->port.membase + UART011_CR);
1095
1096 /* Soft reset UART module */
1097 if (uap->port.dev->platform_data) {
1098 struct amba_pl011_data *plat;
1099
1100 plat = uap->port.dev->platform_data;
1101 if (plat->reset)
1102 plat->reset();
1103 }
1104
1105 /* Restore registers */
1106 for (loop = 0; loop < UART_WA_SAVE_NR; loop++)
1107 writew(uart_wa_regdata[loop] ,
1108 uap->port.membase + uart_wa_reg[loop]);
1109
1110 /* Initialise the old status of the modem signals */
1111 uap->old_status = readw(uap->port.membase + UART01x_FR) &
1112 UART01x_FR_MODEM_ANY;
1113
1114 if (readl(base + UART011_MIS) & 0x2)
1115 printk(KERN_EMERG "UART_BT_WA: ***FAILED***\n");
1116
1117 /* Start Tx/Rx */
1118 tty->hw_stopped = 0;
1119}
1120
Russell Kingb129a8c2005-08-31 10:12:14 +01001121static void pl011_stop_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122{
1123 struct uart_amba_port *uap = (struct uart_amba_port *)port;
1124
1125 uap->im &= ~UART011_TXIM;
1126 writew(uap->im, uap->port.membase + UART011_IMSC);
Russell King68b65f72010-12-22 17:24:39 +00001127 pl011_dma_tx_stop(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128}
1129
Russell Kingb129a8c2005-08-31 10:12:14 +01001130static void pl011_start_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131{
1132 struct uart_amba_port *uap = (struct uart_amba_port *)port;
1133
Russell King68b65f72010-12-22 17:24:39 +00001134 if (!pl011_dma_tx_start(uap)) {
1135 uap->im |= UART011_TXIM;
1136 writew(uap->im, uap->port.membase + UART011_IMSC);
1137 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138}
1139
1140static void pl011_stop_rx(struct uart_port *port)
1141{
1142 struct uart_amba_port *uap = (struct uart_amba_port *)port;
1143
1144 uap->im &= ~(UART011_RXIM|UART011_RTIM|UART011_FEIM|
1145 UART011_PEIM|UART011_BEIM|UART011_OEIM);
1146 writew(uap->im, uap->port.membase + UART011_IMSC);
Linus Walleijead76f32011-02-24 13:21:08 +01001147
1148 pl011_dma_rx_stop(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149}
1150
1151static void pl011_enable_ms(struct uart_port *port)
1152{
1153 struct uart_amba_port *uap = (struct uart_amba_port *)port;
1154
1155 uap->im |= UART011_RIMIM|UART011_CTSMIM|UART011_DCDMIM|UART011_DSRMIM;
1156 writew(uap->im, uap->port.membase + UART011_IMSC);
1157}
1158
David Howells7d12e782006-10-05 14:55:46 +01001159static void pl011_rx_chars(struct uart_amba_port *uap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160{
Alan Coxebd2c8f2009-09-19 13:13:28 -07001161 struct tty_struct *tty = uap->port.state->port.tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162
Linus Walleij29772c42011-02-24 13:21:36 +01001163 pl011_fifo_to_tty(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164
Thomas Gleixner2389b272007-05-29 21:53:50 +01001165 spin_unlock(&uap->port.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 tty_flip_buffer_push(tty);
Linus Walleijead76f32011-02-24 13:21:08 +01001167 /*
1168 * If we were temporarily out of DMA mode for a while,
1169 * attempt to switch back to DMA mode again.
1170 */
1171 if (pl011_dma_rx_available(uap)) {
1172 if (pl011_dma_rx_trigger_dma(uap)) {
1173 dev_dbg(uap->port.dev, "could not trigger RX DMA job "
1174 "fall back to interrupt mode again\n");
1175 uap->im |= UART011_RXIM;
1176 } else
1177 uap->im &= ~UART011_RXIM;
1178 writew(uap->im, uap->port.membase + UART011_IMSC);
1179 }
Thomas Gleixner2389b272007-05-29 21:53:50 +01001180 spin_lock(&uap->port.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181}
1182
1183static void pl011_tx_chars(struct uart_amba_port *uap)
1184{
Alan Coxebd2c8f2009-09-19 13:13:28 -07001185 struct circ_buf *xmit = &uap->port.state->xmit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 int count;
1187
1188 if (uap->port.x_char) {
1189 writew(uap->port.x_char, uap->port.membase + UART01x_DR);
1190 uap->port.icount.tx++;
1191 uap->port.x_char = 0;
1192 return;
1193 }
1194 if (uart_circ_empty(xmit) || uart_tx_stopped(&uap->port)) {
Russell Kingb129a8c2005-08-31 10:12:14 +01001195 pl011_stop_tx(&uap->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 return;
1197 }
1198
Russell King68b65f72010-12-22 17:24:39 +00001199 /* If we are using DMA mode, try to send some characters. */
1200 if (pl011_dma_tx_irq(uap))
1201 return;
1202
Russell Kingffca2b12010-12-22 17:13:05 +00001203 count = uap->fifosize >> 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 do {
1205 writew(xmit->buf[xmit->tail], uap->port.membase + UART01x_DR);
1206 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
1207 uap->port.icount.tx++;
1208 if (uart_circ_empty(xmit))
1209 break;
1210 } while (--count > 0);
1211
1212 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
1213 uart_write_wakeup(&uap->port);
1214
1215 if (uart_circ_empty(xmit))
Russell Kingb129a8c2005-08-31 10:12:14 +01001216 pl011_stop_tx(&uap->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217}
1218
1219static void pl011_modem_status(struct uart_amba_port *uap)
1220{
1221 unsigned int status, delta;
1222
1223 status = readw(uap->port.membase + UART01x_FR) & UART01x_FR_MODEM_ANY;
1224
1225 delta = status ^ uap->old_status;
1226 uap->old_status = status;
1227
1228 if (!delta)
1229 return;
1230
1231 if (delta & UART01x_FR_DCD)
1232 uart_handle_dcd_change(&uap->port, status & UART01x_FR_DCD);
1233
1234 if (delta & UART01x_FR_DSR)
1235 uap->port.icount.dsr++;
1236
1237 if (delta & UART01x_FR_CTS)
1238 uart_handle_cts_change(&uap->port, status & UART01x_FR_CTS);
1239
Alan Coxbdc04e32009-09-19 13:13:31 -07001240 wake_up_interruptible(&uap->port.state->port.delta_msr_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241}
1242
David Howells7d12e782006-10-05 14:55:46 +01001243static irqreturn_t pl011_int(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244{
1245 struct uart_amba_port *uap = dev_id;
Russell King963cc982010-12-22 17:16:09 +00001246 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 unsigned int status, pass_counter = AMBA_ISR_PASS_LIMIT;
1248 int handled = 0;
1249
Russell King963cc982010-12-22 17:16:09 +00001250 spin_lock_irqsave(&uap->port.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251
1252 status = readw(uap->port.membase + UART011_MIS);
1253 if (status) {
1254 do {
1255 writew(status & ~(UART011_TXIS|UART011_RTIS|
1256 UART011_RXIS),
1257 uap->port.membase + UART011_ICR);
1258
Linus Walleijead76f32011-02-24 13:21:08 +01001259 if (status & (UART011_RTIS|UART011_RXIS)) {
1260 if (pl011_dma_rx_running(uap))
1261 pl011_dma_rx_irq(uap);
1262 else
1263 pl011_rx_chars(uap);
1264 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 if (status & (UART011_DSRMIS|UART011_DCDMIS|
1266 UART011_CTSMIS|UART011_RIMIS))
1267 pl011_modem_status(uap);
1268 if (status & UART011_TXIS)
1269 pl011_tx_chars(uap);
1270
Shreshtha Kumar Sahuc16d51a2011-06-13 10:11:33 +02001271 if (pass_counter-- == 0) {
1272 if (uap->interrupt_may_hang)
1273 tasklet_schedule(&pl011_lockup_tlet);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 break;
Shreshtha Kumar Sahuc16d51a2011-06-13 10:11:33 +02001275 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276
1277 status = readw(uap->port.membase + UART011_MIS);
1278 } while (status != 0);
1279 handled = 1;
1280 }
1281
Russell King963cc982010-12-22 17:16:09 +00001282 spin_unlock_irqrestore(&uap->port.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283
1284 return IRQ_RETVAL(handled);
1285}
1286
1287static unsigned int pl01x_tx_empty(struct uart_port *port)
1288{
1289 struct uart_amba_port *uap = (struct uart_amba_port *)port;
1290 unsigned int status = readw(uap->port.membase + UART01x_FR);
1291 return status & (UART01x_FR_BUSY|UART01x_FR_TXFF) ? 0 : TIOCSER_TEMT;
1292}
1293
1294static unsigned int pl01x_get_mctrl(struct uart_port *port)
1295{
1296 struct uart_amba_port *uap = (struct uart_amba_port *)port;
1297 unsigned int result = 0;
1298 unsigned int status = readw(uap->port.membase + UART01x_FR);
1299
Jiri Slaby5159f402007-10-18 23:40:31 -07001300#define TIOCMBIT(uartbit, tiocmbit) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 if (status & uartbit) \
1302 result |= tiocmbit
1303
Jiri Slaby5159f402007-10-18 23:40:31 -07001304 TIOCMBIT(UART01x_FR_DCD, TIOCM_CAR);
1305 TIOCMBIT(UART01x_FR_DSR, TIOCM_DSR);
1306 TIOCMBIT(UART01x_FR_CTS, TIOCM_CTS);
1307 TIOCMBIT(UART011_FR_RI, TIOCM_RNG);
1308#undef TIOCMBIT
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 return result;
1310}
1311
1312static void pl011_set_mctrl(struct uart_port *port, unsigned int mctrl)
1313{
1314 struct uart_amba_port *uap = (struct uart_amba_port *)port;
1315 unsigned int cr;
1316
1317 cr = readw(uap->port.membase + UART011_CR);
1318
Jiri Slaby5159f402007-10-18 23:40:31 -07001319#define TIOCMBIT(tiocmbit, uartbit) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 if (mctrl & tiocmbit) \
1321 cr |= uartbit; \
1322 else \
1323 cr &= ~uartbit
1324
Jiri Slaby5159f402007-10-18 23:40:31 -07001325 TIOCMBIT(TIOCM_RTS, UART011_CR_RTS);
1326 TIOCMBIT(TIOCM_DTR, UART011_CR_DTR);
1327 TIOCMBIT(TIOCM_OUT1, UART011_CR_OUT1);
1328 TIOCMBIT(TIOCM_OUT2, UART011_CR_OUT2);
1329 TIOCMBIT(TIOCM_LOOP, UART011_CR_LBE);
Rabin Vincent3b438162010-02-12 06:43:11 +01001330
1331 if (uap->autorts) {
1332 /* We need to disable auto-RTS if we want to turn RTS off */
1333 TIOCMBIT(TIOCM_RTS, UART011_CR_RTSEN);
1334 }
Jiri Slaby5159f402007-10-18 23:40:31 -07001335#undef TIOCMBIT
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336
1337 writew(cr, uap->port.membase + UART011_CR);
1338}
1339
1340static void pl011_break_ctl(struct uart_port *port, int break_state)
1341{
1342 struct uart_amba_port *uap = (struct uart_amba_port *)port;
1343 unsigned long flags;
1344 unsigned int lcr_h;
1345
1346 spin_lock_irqsave(&uap->port.lock, flags);
Linus Walleijec489aa2010-06-02 08:13:52 +01001347 lcr_h = readw(uap->port.membase + uap->lcrh_tx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 if (break_state == -1)
1349 lcr_h |= UART01x_LCRH_BRK;
1350 else
1351 lcr_h &= ~UART01x_LCRH_BRK;
Linus Walleijec489aa2010-06-02 08:13:52 +01001352 writew(lcr_h, uap->port.membase + uap->lcrh_tx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 spin_unlock_irqrestore(&uap->port.lock, flags);
1354}
1355
Jason Wessel84b5ae12008-02-20 13:33:39 -06001356#ifdef CONFIG_CONSOLE_POLL
1357static int pl010_get_poll_char(struct uart_port *port)
1358{
1359 struct uart_amba_port *uap = (struct uart_amba_port *)port;
1360 unsigned int status;
1361
Jason Wesself5316b42010-05-20 21:04:22 -05001362 status = readw(uap->port.membase + UART01x_FR);
1363 if (status & UART01x_FR_RXFE)
1364 return NO_POLL_CHAR;
Jason Wessel84b5ae12008-02-20 13:33:39 -06001365
1366 return readw(uap->port.membase + UART01x_DR);
1367}
1368
1369static void pl010_put_poll_char(struct uart_port *port,
1370 unsigned char ch)
1371{
1372 struct uart_amba_port *uap = (struct uart_amba_port *)port;
1373
1374 while (readw(uap->port.membase + UART01x_FR) & UART01x_FR_TXFF)
1375 barrier();
1376
1377 writew(ch, uap->port.membase + UART01x_DR);
1378}
1379
1380#endif /* CONFIG_CONSOLE_POLL */
1381
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382static int pl011_startup(struct uart_port *port)
1383{
1384 struct uart_amba_port *uap = (struct uart_amba_port *)port;
1385 unsigned int cr;
1386 int retval;
1387
Russell King4b4851c2011-09-22 11:35:30 +01001388 retval = clk_prepare(uap->clk);
1389 if (retval)
1390 goto out;
1391
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 /*
1393 * Try to enable the clock producer.
1394 */
1395 retval = clk_enable(uap->clk);
1396 if (retval)
Russell King4b4851c2011-09-22 11:35:30 +01001397 goto clk_unprep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398
1399 uap->port.uartclk = clk_get_rate(uap->clk);
1400
Linus Walleij9b96fba2012-03-13 13:27:23 +01001401 /* Clear pending error and receive interrupts */
1402 writew(UART011_OEIS | UART011_BEIS | UART011_PEIS | UART011_FEIS |
1403 UART011_RTIS | UART011_RXIS, uap->port.membase + UART011_ICR);
1404
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 /*
1406 * Allocate the IRQ
1407 */
1408 retval = request_irq(uap->port.irq, pl011_int, 0, "uart-pl011", uap);
1409 if (retval)
1410 goto clk_dis;
1411
Russell Kingc19f12b2010-12-22 17:48:26 +00001412 writew(uap->vendor->ifls, uap->port.membase + UART011_IFLS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413
1414 /*
1415 * Provoke TX FIFO interrupt into asserting.
1416 */
1417 cr = UART01x_CR_UARTEN | UART011_CR_TXE | UART011_CR_LBE;
1418 writew(cr, uap->port.membase + UART011_CR);
1419 writew(0, uap->port.membase + UART011_FBRD);
1420 writew(1, uap->port.membase + UART011_IBRD);
Linus Walleijec489aa2010-06-02 08:13:52 +01001421 writew(0, uap->port.membase + uap->lcrh_rx);
1422 if (uap->lcrh_tx != uap->lcrh_rx) {
1423 int i;
1424 /*
1425 * Wait 10 PCLKs before writing LCRH_TX register,
1426 * to get this delay write read only register 10 times
1427 */
1428 for (i = 0; i < 10; ++i)
1429 writew(0xff, uap->port.membase + UART011_MIS);
1430 writew(0, uap->port.membase + uap->lcrh_tx);
1431 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 writew(0, uap->port.membase + UART01x_DR);
1433 while (readw(uap->port.membase + UART01x_FR) & UART01x_FR_BUSY)
1434 barrier();
1435
Shreshtha Kumar Sahud8d8ffa2012-01-18 15:53:59 +05301436 /* restore RTS and DTR */
1437 cr = uap->old_cr & (UART011_CR_RTS | UART011_CR_DTR);
1438 cr |= UART01x_CR_UARTEN | UART011_CR_RXE | UART011_CR_TXE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 writew(cr, uap->port.membase + UART011_CR);
1440
1441 /*
1442 * initialise the old status of the modem signals
1443 */
1444 uap->old_status = readw(uap->port.membase + UART01x_FR) & UART01x_FR_MODEM_ANY;
1445
Russell King68b65f72010-12-22 17:24:39 +00001446 /* Startup DMA */
1447 pl011_dma_startup(uap);
1448
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 /*
Linus Walleijead76f32011-02-24 13:21:08 +01001450 * Finally, enable interrupts, only timeouts when using DMA
1451 * if initial RX DMA job failed, start in interrupt mode
1452 * as well.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 */
1454 spin_lock_irq(&uap->port.lock);
Linus Walleij9b96fba2012-03-13 13:27:23 +01001455 /* Clear out any spuriously appearing RX interrupts */
1456 writew(UART011_RTIS | UART011_RXIS,
1457 uap->port.membase + UART011_ICR);
Linus Walleijead76f32011-02-24 13:21:08 +01001458 uap->im = UART011_RTIM;
1459 if (!pl011_dma_rx_running(uap))
1460 uap->im |= UART011_RXIM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 writew(uap->im, uap->port.membase + UART011_IMSC);
1462 spin_unlock_irq(&uap->port.lock);
1463
Shreshtha Kumar Sahuc16d51a2011-06-13 10:11:33 +02001464 if (uap->port.dev->platform_data) {
1465 struct amba_pl011_data *plat;
1466
1467 plat = uap->port.dev->platform_data;
1468 if (plat->init)
1469 plat->init();
1470 }
1471
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 return 0;
1473
1474 clk_dis:
1475 clk_disable(uap->clk);
Russell King4b4851c2011-09-22 11:35:30 +01001476 clk_unprep:
1477 clk_unprepare(uap->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 out:
1479 return retval;
1480}
1481
Linus Walleijec489aa2010-06-02 08:13:52 +01001482static void pl011_shutdown_channel(struct uart_amba_port *uap,
1483 unsigned int lcrh)
1484{
1485 unsigned long val;
1486
1487 val = readw(uap->port.membase + lcrh);
1488 val &= ~(UART01x_LCRH_BRK | UART01x_LCRH_FEN);
1489 writew(val, uap->port.membase + lcrh);
1490}
1491
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492static void pl011_shutdown(struct uart_port *port)
1493{
1494 struct uart_amba_port *uap = (struct uart_amba_port *)port;
Shreshtha Kumar Sahud8d8ffa2012-01-18 15:53:59 +05301495 unsigned int cr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496
1497 /*
1498 * disable all interrupts
1499 */
1500 spin_lock_irq(&uap->port.lock);
1501 uap->im = 0;
1502 writew(uap->im, uap->port.membase + UART011_IMSC);
1503 writew(0xffff, uap->port.membase + UART011_ICR);
1504 spin_unlock_irq(&uap->port.lock);
1505
Russell King68b65f72010-12-22 17:24:39 +00001506 pl011_dma_shutdown(uap);
1507
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 /*
1509 * Free the interrupt
1510 */
1511 free_irq(uap->port.irq, uap);
1512
1513 /*
1514 * disable the port
Shreshtha Kumar Sahud8d8ffa2012-01-18 15:53:59 +05301515 * disable the port. It should not disable RTS and DTR.
1516 * Also RTS and DTR state should be preserved to restore
1517 * it during startup().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 */
Rabin Vincent3b438162010-02-12 06:43:11 +01001519 uap->autorts = false;
Shreshtha Kumar Sahud8d8ffa2012-01-18 15:53:59 +05301520 cr = readw(uap->port.membase + UART011_CR);
1521 uap->old_cr = cr;
1522 cr &= UART011_CR_RTS | UART011_CR_DTR;
1523 cr |= UART01x_CR_UARTEN | UART011_CR_TXE;
1524 writew(cr, uap->port.membase + UART011_CR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525
1526 /*
1527 * disable break condition and fifos
1528 */
Linus Walleijec489aa2010-06-02 08:13:52 +01001529 pl011_shutdown_channel(uap, uap->lcrh_rx);
1530 if (uap->lcrh_rx != uap->lcrh_tx)
1531 pl011_shutdown_channel(uap, uap->lcrh_tx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532
1533 /*
1534 * Shut down the clock producer
1535 */
1536 clk_disable(uap->clk);
Russell King4b4851c2011-09-22 11:35:30 +01001537 clk_unprepare(uap->clk);
Shreshtha Kumar Sahuc16d51a2011-06-13 10:11:33 +02001538
1539 if (uap->port.dev->platform_data) {
1540 struct amba_pl011_data *plat;
1541
1542 plat = uap->port.dev->platform_data;
1543 if (plat->exit)
1544 plat->exit();
1545 }
1546
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547}
1548
1549static void
Alan Cox606d0992006-12-08 02:38:45 -08001550pl011_set_termios(struct uart_port *port, struct ktermios *termios,
1551 struct ktermios *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552{
Rabin Vincent3b438162010-02-12 06:43:11 +01001553 struct uart_amba_port *uap = (struct uart_amba_port *)port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 unsigned int lcr_h, old_cr;
1555 unsigned long flags;
Russell Kingc19f12b2010-12-22 17:48:26 +00001556 unsigned int baud, quot, clkdiv;
1557
1558 if (uap->vendor->oversampling)
1559 clkdiv = 8;
1560 else
1561 clkdiv = 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562
1563 /*
1564 * Ask the core to calculate the divisor for us.
1565 */
Linus Walleijac3e3fb2010-06-02 20:40:22 +01001566 baud = uart_get_baud_rate(port, termios, old, 0,
Russell Kingc19f12b2010-12-22 17:48:26 +00001567 port->uartclk / clkdiv);
Linus Walleijac3e3fb2010-06-02 20:40:22 +01001568
1569 if (baud > port->uartclk/16)
1570 quot = DIV_ROUND_CLOSEST(port->uartclk * 8, baud);
1571 else
1572 quot = DIV_ROUND_CLOSEST(port->uartclk * 4, baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573
1574 switch (termios->c_cflag & CSIZE) {
1575 case CS5:
1576 lcr_h = UART01x_LCRH_WLEN_5;
1577 break;
1578 case CS6:
1579 lcr_h = UART01x_LCRH_WLEN_6;
1580 break;
1581 case CS7:
1582 lcr_h = UART01x_LCRH_WLEN_7;
1583 break;
1584 default: // CS8
1585 lcr_h = UART01x_LCRH_WLEN_8;
1586 break;
1587 }
1588 if (termios->c_cflag & CSTOPB)
1589 lcr_h |= UART01x_LCRH_STP2;
1590 if (termios->c_cflag & PARENB) {
1591 lcr_h |= UART01x_LCRH_PEN;
1592 if (!(termios->c_cflag & PARODD))
1593 lcr_h |= UART01x_LCRH_EPS;
1594 }
Russell Kingffca2b12010-12-22 17:13:05 +00001595 if (uap->fifosize > 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 lcr_h |= UART01x_LCRH_FEN;
1597
1598 spin_lock_irqsave(&port->lock, flags);
1599
1600 /*
1601 * Update the per-port timeout.
1602 */
1603 uart_update_timeout(port, termios->c_cflag, baud);
1604
Russell Kingb63d4f02005-11-19 11:10:35 +00001605 port->read_status_mask = UART011_DR_OE | 255;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 if (termios->c_iflag & INPCK)
Russell Kingb63d4f02005-11-19 11:10:35 +00001607 port->read_status_mask |= UART011_DR_FE | UART011_DR_PE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 if (termios->c_iflag & (BRKINT | PARMRK))
Russell Kingb63d4f02005-11-19 11:10:35 +00001609 port->read_status_mask |= UART011_DR_BE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610
1611 /*
1612 * Characters to ignore
1613 */
1614 port->ignore_status_mask = 0;
1615 if (termios->c_iflag & IGNPAR)
Russell Kingb63d4f02005-11-19 11:10:35 +00001616 port->ignore_status_mask |= UART011_DR_FE | UART011_DR_PE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 if (termios->c_iflag & IGNBRK) {
Russell Kingb63d4f02005-11-19 11:10:35 +00001618 port->ignore_status_mask |= UART011_DR_BE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 /*
1620 * If we're ignoring parity and break indicators,
1621 * ignore overruns too (for real raw support).
1622 */
1623 if (termios->c_iflag & IGNPAR)
Russell Kingb63d4f02005-11-19 11:10:35 +00001624 port->ignore_status_mask |= UART011_DR_OE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 }
1626
1627 /*
1628 * Ignore all characters if CREAD is not set.
1629 */
1630 if ((termios->c_cflag & CREAD) == 0)
Russell Kingb63d4f02005-11-19 11:10:35 +00001631 port->ignore_status_mask |= UART_DUMMY_DR_RX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632
1633 if (UART_ENABLE_MS(port, termios->c_cflag))
1634 pl011_enable_ms(port);
1635
1636 /* first, disable everything */
1637 old_cr = readw(port->membase + UART011_CR);
1638 writew(0, port->membase + UART011_CR);
1639
Rabin Vincent3b438162010-02-12 06:43:11 +01001640 if (termios->c_cflag & CRTSCTS) {
1641 if (old_cr & UART011_CR_RTS)
1642 old_cr |= UART011_CR_RTSEN;
1643
1644 old_cr |= UART011_CR_CTSEN;
1645 uap->autorts = true;
1646 } else {
1647 old_cr &= ~(UART011_CR_CTSEN | UART011_CR_RTSEN);
1648 uap->autorts = false;
1649 }
1650
Russell Kingc19f12b2010-12-22 17:48:26 +00001651 if (uap->vendor->oversampling) {
1652 if (baud > port->uartclk / 16)
Linus Walleijac3e3fb2010-06-02 20:40:22 +01001653 old_cr |= ST_UART011_CR_OVSFACT;
1654 else
1655 old_cr &= ~ST_UART011_CR_OVSFACT;
1656 }
1657
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 /* Set baud rate */
1659 writew(quot & 0x3f, port->membase + UART011_FBRD);
1660 writew(quot >> 6, port->membase + UART011_IBRD);
1661
1662 /*
1663 * ----------v----------v----------v----------v-----
1664 * NOTE: MUST BE WRITTEN AFTER UARTLCR_M & UARTLCR_L
1665 * ----------^----------^----------^----------^-----
1666 */
Linus Walleijec489aa2010-06-02 08:13:52 +01001667 writew(lcr_h, port->membase + uap->lcrh_rx);
1668 if (uap->lcrh_rx != uap->lcrh_tx) {
1669 int i;
1670 /*
1671 * Wait 10 PCLKs before writing LCRH_TX register,
1672 * to get this delay write read only register 10 times
1673 */
1674 for (i = 0; i < 10; ++i)
1675 writew(0xff, uap->port.membase + UART011_MIS);
1676 writew(lcr_h, port->membase + uap->lcrh_tx);
1677 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 writew(old_cr, port->membase + UART011_CR);
1679
1680 spin_unlock_irqrestore(&port->lock, flags);
1681}
1682
1683static const char *pl011_type(struct uart_port *port)
1684{
Russell Kinge8a7ba82010-12-28 09:16:54 +00001685 struct uart_amba_port *uap = (struct uart_amba_port *)port;
1686 return uap->port.type == PORT_AMBA ? uap->type : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687}
1688
1689/*
1690 * Release the memory region(s) being used by 'port'
1691 */
1692static void pl010_release_port(struct uart_port *port)
1693{
1694 release_mem_region(port->mapbase, SZ_4K);
1695}
1696
1697/*
1698 * Request the memory region(s) being used by 'port'
1699 */
1700static int pl010_request_port(struct uart_port *port)
1701{
1702 return request_mem_region(port->mapbase, SZ_4K, "uart-pl011")
1703 != NULL ? 0 : -EBUSY;
1704}
1705
1706/*
1707 * Configure/autoconfigure the port.
1708 */
1709static void pl010_config_port(struct uart_port *port, int flags)
1710{
1711 if (flags & UART_CONFIG_TYPE) {
1712 port->type = PORT_AMBA;
1713 pl010_request_port(port);
1714 }
1715}
1716
1717/*
1718 * verify the new serial_struct (for TIOCSSERIAL).
1719 */
1720static int pl010_verify_port(struct uart_port *port, struct serial_struct *ser)
1721{
1722 int ret = 0;
1723 if (ser->type != PORT_UNKNOWN && ser->type != PORT_AMBA)
1724 ret = -EINVAL;
Yinghai Lua62c4132008-08-19 20:49:55 -07001725 if (ser->irq < 0 || ser->irq >= nr_irqs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 ret = -EINVAL;
1727 if (ser->baud_base < 9600)
1728 ret = -EINVAL;
1729 return ret;
1730}
1731
1732static struct uart_ops amba_pl011_pops = {
1733 .tx_empty = pl01x_tx_empty,
1734 .set_mctrl = pl011_set_mctrl,
1735 .get_mctrl = pl01x_get_mctrl,
1736 .stop_tx = pl011_stop_tx,
1737 .start_tx = pl011_start_tx,
1738 .stop_rx = pl011_stop_rx,
1739 .enable_ms = pl011_enable_ms,
1740 .break_ctl = pl011_break_ctl,
1741 .startup = pl011_startup,
1742 .shutdown = pl011_shutdown,
Russell King68b65f72010-12-22 17:24:39 +00001743 .flush_buffer = pl011_dma_flush_buffer,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 .set_termios = pl011_set_termios,
1745 .type = pl011_type,
1746 .release_port = pl010_release_port,
1747 .request_port = pl010_request_port,
1748 .config_port = pl010_config_port,
1749 .verify_port = pl010_verify_port,
Jason Wessel84b5ae12008-02-20 13:33:39 -06001750#ifdef CONFIG_CONSOLE_POLL
1751 .poll_get_char = pl010_get_poll_char,
1752 .poll_put_char = pl010_put_poll_char,
1753#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754};
1755
1756static struct uart_amba_port *amba_ports[UART_NR];
1757
1758#ifdef CONFIG_SERIAL_AMBA_PL011_CONSOLE
1759
Russell Kingd3587882006-03-20 20:00:09 +00001760static void pl011_console_putchar(struct uart_port *port, int ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761{
Russell Kingd3587882006-03-20 20:00:09 +00001762 struct uart_amba_port *uap = (struct uart_amba_port *)port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763
Russell Kingd3587882006-03-20 20:00:09 +00001764 while (readw(uap->port.membase + UART01x_FR) & UART01x_FR_TXFF)
1765 barrier();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 writew(ch, uap->port.membase + UART01x_DR);
1767}
1768
1769static void
1770pl011_console_write(struct console *co, const char *s, unsigned int count)
1771{
1772 struct uart_amba_port *uap = amba_ports[co->index];
1773 unsigned int status, old_cr, new_cr;
Rabin Vincentef605fd2012-01-17 11:52:28 +01001774 unsigned long flags;
1775 int locked = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776
1777 clk_enable(uap->clk);
1778
Rabin Vincentef605fd2012-01-17 11:52:28 +01001779 local_irq_save(flags);
1780 if (uap->port.sysrq)
1781 locked = 0;
1782 else if (oops_in_progress)
1783 locked = spin_trylock(&uap->port.lock);
1784 else
1785 spin_lock(&uap->port.lock);
1786
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 /*
1788 * First save the CR then disable the interrupts
1789 */
1790 old_cr = readw(uap->port.membase + UART011_CR);
1791 new_cr = old_cr & ~UART011_CR_CTSEN;
1792 new_cr |= UART01x_CR_UARTEN | UART011_CR_TXE;
1793 writew(new_cr, uap->port.membase + UART011_CR);
1794
Russell Kingd3587882006-03-20 20:00:09 +00001795 uart_console_write(&uap->port, s, count, pl011_console_putchar);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796
1797 /*
1798 * Finally, wait for transmitter to become empty
1799 * and restore the TCR
1800 */
1801 do {
1802 status = readw(uap->port.membase + UART01x_FR);
1803 } while (status & UART01x_FR_BUSY);
1804 writew(old_cr, uap->port.membase + UART011_CR);
1805
Rabin Vincentef605fd2012-01-17 11:52:28 +01001806 if (locked)
1807 spin_unlock(&uap->port.lock);
1808 local_irq_restore(flags);
1809
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 clk_disable(uap->clk);
1811}
1812
1813static void __init
1814pl011_console_get_options(struct uart_amba_port *uap, int *baud,
1815 int *parity, int *bits)
1816{
1817 if (readw(uap->port.membase + UART011_CR) & UART01x_CR_UARTEN) {
1818 unsigned int lcr_h, ibrd, fbrd;
1819
Linus Walleijec489aa2010-06-02 08:13:52 +01001820 lcr_h = readw(uap->port.membase + uap->lcrh_tx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821
1822 *parity = 'n';
1823 if (lcr_h & UART01x_LCRH_PEN) {
1824 if (lcr_h & UART01x_LCRH_EPS)
1825 *parity = 'e';
1826 else
1827 *parity = 'o';
1828 }
1829
1830 if ((lcr_h & 0x60) == UART01x_LCRH_WLEN_7)
1831 *bits = 7;
1832 else
1833 *bits = 8;
1834
1835 ibrd = readw(uap->port.membase + UART011_IBRD);
1836 fbrd = readw(uap->port.membase + UART011_FBRD);
1837
1838 *baud = uap->port.uartclk * 4 / (64 * ibrd + fbrd);
Linus Walleijac3e3fb2010-06-02 20:40:22 +01001839
Russell Kingc19f12b2010-12-22 17:48:26 +00001840 if (uap->vendor->oversampling) {
Linus Walleijac3e3fb2010-06-02 20:40:22 +01001841 if (readw(uap->port.membase + UART011_CR)
1842 & ST_UART011_CR_OVSFACT)
1843 *baud *= 2;
1844 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 }
1846}
1847
1848static int __init pl011_console_setup(struct console *co, char *options)
1849{
1850 struct uart_amba_port *uap;
1851 int baud = 38400;
1852 int bits = 8;
1853 int parity = 'n';
1854 int flow = 'n';
Russell King4b4851c2011-09-22 11:35:30 +01001855 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856
1857 /*
1858 * Check whether an invalid uart number has been specified, and
1859 * if so, search for the first available port that does have
1860 * console support.
1861 */
1862 if (co->index >= UART_NR)
1863 co->index = 0;
1864 uap = amba_ports[co->index];
Russell Kingd28122a2007-01-22 18:59:42 +00001865 if (!uap)
1866 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867
Russell King4b4851c2011-09-22 11:35:30 +01001868 ret = clk_prepare(uap->clk);
1869 if (ret)
1870 return ret;
1871
Shreshtha Kumar Sahuc16d51a2011-06-13 10:11:33 +02001872 if (uap->port.dev->platform_data) {
1873 struct amba_pl011_data *plat;
1874
1875 plat = uap->port.dev->platform_data;
1876 if (plat->init)
1877 plat->init();
1878 }
1879
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 uap->port.uartclk = clk_get_rate(uap->clk);
1881
1882 if (options)
1883 uart_parse_options(options, &baud, &parity, &bits, &flow);
1884 else
1885 pl011_console_get_options(uap, &baud, &parity, &bits);
1886
1887 return uart_set_options(&uap->port, co, baud, parity, bits, flow);
1888}
1889
Vincent Sanders2d934862005-09-14 22:36:03 +01001890static struct uart_driver amba_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891static struct console amba_console = {
1892 .name = "ttyAMA",
1893 .write = pl011_console_write,
1894 .device = uart_console_device,
1895 .setup = pl011_console_setup,
1896 .flags = CON_PRINTBUFFER,
1897 .index = -1,
1898 .data = &amba_reg,
1899};
1900
1901#define AMBA_CONSOLE (&amba_console)
1902#else
1903#define AMBA_CONSOLE NULL
1904#endif
1905
1906static struct uart_driver amba_reg = {
1907 .owner = THIS_MODULE,
1908 .driver_name = "ttyAMA",
1909 .dev_name = "ttyAMA",
1910 .major = SERIAL_AMBA_MAJOR,
1911 .minor = SERIAL_AMBA_MINOR,
1912 .nr = UART_NR,
1913 .cons = AMBA_CONSOLE,
1914};
1915
Russell Kingaa25afa2011-02-19 15:55:00 +00001916static int pl011_probe(struct amba_device *dev, const struct amba_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917{
1918 struct uart_amba_port *uap;
Alessandro Rubini5926a292009-06-04 17:43:04 +01001919 struct vendor_data *vendor = id->data;
Shawn Guo258e0552012-05-06 22:53:35 +08001920 struct pinctrl *pinctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 void __iomem *base;
1922 int i, ret;
1923
1924 for (i = 0; i < ARRAY_SIZE(amba_ports); i++)
1925 if (amba_ports[i] == NULL)
1926 break;
1927
1928 if (i == ARRAY_SIZE(amba_ports)) {
1929 ret = -EBUSY;
1930 goto out;
1931 }
1932
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07001933 uap = kzalloc(sizeof(struct uart_amba_port), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 if (uap == NULL) {
1935 ret = -ENOMEM;
1936 goto out;
1937 }
1938
Linus Walleijdc890c22009-06-07 23:27:31 +01001939 base = ioremap(dev->res.start, resource_size(&dev->res));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 if (!base) {
1941 ret = -ENOMEM;
1942 goto free;
1943 }
1944
Shawn Guo258e0552012-05-06 22:53:35 +08001945 pinctrl = devm_pinctrl_get_select_default(&dev->dev);
1946 if (IS_ERR(pinctrl)) {
1947 ret = PTR_ERR(pinctrl);
1948 goto unmap;
1949 }
1950
Russell Kingee569c42008-11-30 17:38:14 +00001951 uap->clk = clk_get(&dev->dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 if (IS_ERR(uap->clk)) {
1953 ret = PTR_ERR(uap->clk);
1954 goto unmap;
1955 }
1956
Russell Kingc19f12b2010-12-22 17:48:26 +00001957 uap->vendor = vendor;
Linus Walleijec489aa2010-06-02 08:13:52 +01001958 uap->lcrh_rx = vendor->lcrh_rx;
1959 uap->lcrh_tx = vendor->lcrh_tx;
Shreshtha Kumar Sahud8d8ffa2012-01-18 15:53:59 +05301960 uap->old_cr = 0;
Russell Kingffca2b12010-12-22 17:13:05 +00001961 uap->fifosize = vendor->fifosize;
Shreshtha Kumar Sahuc16d51a2011-06-13 10:11:33 +02001962 uap->interrupt_may_hang = vendor->interrupt_may_hang;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 uap->port.dev = &dev->dev;
1964 uap->port.mapbase = dev->res.start;
1965 uap->port.membase = base;
1966 uap->port.iotype = UPIO_MEM;
1967 uap->port.irq = dev->irq[0];
Russell Kingffca2b12010-12-22 17:13:05 +00001968 uap->port.fifosize = uap->fifosize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 uap->port.ops = &amba_pl011_pops;
1970 uap->port.flags = UPF_BOOT_AUTOCONF;
1971 uap->port.line = i;
Russell King68b65f72010-12-22 17:24:39 +00001972 pl011_dma_probe(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973
Linus Walleijc3d8b762012-03-21 20:15:18 +01001974 /* Ensure interrupts from this UART are masked and cleared */
1975 writew(0, uap->port.membase + UART011_IMSC);
1976 writew(0xffff, uap->port.membase + UART011_ICR);
1977
Russell Kinge8a7ba82010-12-28 09:16:54 +00001978 snprintf(uap->type, sizeof(uap->type), "PL011 rev%u", amba_rev(dev));
1979
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 amba_ports[i] = uap;
1981
1982 amba_set_drvdata(dev, uap);
1983 ret = uart_add_one_port(&amba_reg, &uap->port);
1984 if (ret) {
1985 amba_set_drvdata(dev, NULL);
1986 amba_ports[i] = NULL;
Russell King68b65f72010-12-22 17:24:39 +00001987 pl011_dma_remove(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 clk_put(uap->clk);
1989 unmap:
1990 iounmap(base);
1991 free:
1992 kfree(uap);
1993 }
1994 out:
1995 return ret;
1996}
1997
1998static int pl011_remove(struct amba_device *dev)
1999{
2000 struct uart_amba_port *uap = amba_get_drvdata(dev);
2001 int i;
2002
2003 amba_set_drvdata(dev, NULL);
2004
2005 uart_remove_one_port(&amba_reg, &uap->port);
2006
2007 for (i = 0; i < ARRAY_SIZE(amba_ports); i++)
2008 if (amba_ports[i] == uap)
2009 amba_ports[i] = NULL;
2010
Russell King68b65f72010-12-22 17:24:39 +00002011 pl011_dma_remove(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 iounmap(uap->port.membase);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 clk_put(uap->clk);
2014 kfree(uap);
2015 return 0;
2016}
2017
Leo Chenb736b892009-07-28 23:43:33 +01002018#ifdef CONFIG_PM
2019static int pl011_suspend(struct amba_device *dev, pm_message_t state)
2020{
2021 struct uart_amba_port *uap = amba_get_drvdata(dev);
2022
2023 if (!uap)
2024 return -EINVAL;
2025
2026 return uart_suspend_port(&amba_reg, &uap->port);
2027}
2028
2029static int pl011_resume(struct amba_device *dev)
2030{
2031 struct uart_amba_port *uap = amba_get_drvdata(dev);
2032
2033 if (!uap)
2034 return -EINVAL;
2035
2036 return uart_resume_port(&amba_reg, &uap->port);
2037}
2038#endif
2039
Russell King2c39c9e2010-07-27 08:50:16 +01002040static struct amba_id pl011_ids[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 {
2042 .id = 0x00041011,
2043 .mask = 0x000fffff,
Alessandro Rubini5926a292009-06-04 17:43:04 +01002044 .data = &vendor_arm,
2045 },
2046 {
2047 .id = 0x00380802,
2048 .mask = 0x00ffffff,
2049 .data = &vendor_st,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 },
2051 { 0, 0 },
2052};
2053
Dave Martin60f7a332011-10-05 15:15:22 +01002054MODULE_DEVICE_TABLE(amba, pl011_ids);
2055
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056static struct amba_driver pl011_driver = {
2057 .drv = {
2058 .name = "uart-pl011",
2059 },
2060 .id_table = pl011_ids,
2061 .probe = pl011_probe,
2062 .remove = pl011_remove,
Leo Chenb736b892009-07-28 23:43:33 +01002063#ifdef CONFIG_PM
2064 .suspend = pl011_suspend,
2065 .resume = pl011_resume,
2066#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067};
2068
2069static int __init pl011_init(void)
2070{
2071 int ret;
2072 printk(KERN_INFO "Serial: AMBA PL011 UART driver\n");
2073
2074 ret = uart_register_driver(&amba_reg);
2075 if (ret == 0) {
2076 ret = amba_driver_register(&pl011_driver);
2077 if (ret)
2078 uart_unregister_driver(&amba_reg);
2079 }
2080 return ret;
2081}
2082
2083static void __exit pl011_exit(void)
2084{
2085 amba_driver_unregister(&pl011_driver);
2086 uart_unregister_driver(&amba_reg);
2087}
2088
Alessandro Rubini4dd9e742009-05-05 05:54:13 +01002089/*
2090 * While this can be a module, if builtin it's most likely the console
2091 * So let's leave module_exit but move module_init to an earlier place
2092 */
2093arch_initcall(pl011_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094module_exit(pl011_exit);
2095
2096MODULE_AUTHOR("ARM Ltd/Deep Blue Solutions Ltd");
2097MODULE_DESCRIPTION("ARM AMBA serial port driver");
2098MODULE_LICENSE("GPL");