blob: c25b00ef9dbbcc6b174de6765d7d148bcf707a89 [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>
Matthew Leach32614aa2012-08-28 16:41:28 +010055#include <linux/of.h>
56#include <linux/of_device.h>
Shawn Guo258e0552012-05-06 22:53:35 +080057#include <linux/pinctrl/consumer.h>
Alessandro Rubinicb707062012-06-24 12:46:37 +010058#include <linux/sizes.h>
Linus Walleijde609582012-10-15 13:36:01 +020059#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
61#define UART_NR 14
62
63#define SERIAL_AMBA_MAJOR 204
64#define SERIAL_AMBA_MINOR 64
65#define SERIAL_AMBA_NR UART_NR
66
67#define AMBA_ISR_PASS_LIMIT 256
68
Russell Kingb63d4f02005-11-19 11:10:35 +000069#define UART_DR_ERROR (UART011_DR_OE|UART011_DR_BE|UART011_DR_PE|UART011_DR_FE)
70#define UART_DUMMY_DR_RX (1 << 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Alessandro Rubini5926a292009-06-04 17:43:04 +010072/* There is by now at least one vendor with differing details, so handle it */
73struct vendor_data {
74 unsigned int ifls;
75 unsigned int fifosize;
Linus Walleijec489aa2010-06-02 08:13:52 +010076 unsigned int lcrh_tx;
77 unsigned int lcrh_rx;
Linus Walleijac3e3fb2010-06-02 20:40:22 +010078 bool oversampling;
Russell King38d62432010-12-22 17:59:16 +000079 bool dma_threshold;
Rajanikanth H.V4fd06902012-03-26 11:17:02 +020080 bool cts_event_workaround;
Alessandro Rubini5926a292009-06-04 17:43:04 +010081};
82
83static struct vendor_data vendor_arm = {
84 .ifls = UART011_IFLS_RX4_8|UART011_IFLS_TX4_8,
85 .fifosize = 16,
Linus Walleijec489aa2010-06-02 08:13:52 +010086 .lcrh_tx = UART011_LCRH,
87 .lcrh_rx = UART011_LCRH,
Linus Walleijac3e3fb2010-06-02 20:40:22 +010088 .oversampling = false,
Russell King38d62432010-12-22 17:59:16 +000089 .dma_threshold = false,
Rajanikanth H.V4fd06902012-03-26 11:17:02 +020090 .cts_event_workaround = false,
Alessandro Rubini5926a292009-06-04 17:43:04 +010091};
92
93static struct vendor_data vendor_st = {
94 .ifls = UART011_IFLS_RX_HALF|UART011_IFLS_TX_HALF,
95 .fifosize = 64,
Linus Walleijec489aa2010-06-02 08:13:52 +010096 .lcrh_tx = ST_UART011_LCRH_TX,
97 .lcrh_rx = ST_UART011_LCRH_RX,
Linus Walleijac3e3fb2010-06-02 20:40:22 +010098 .oversampling = true,
Russell King38d62432010-12-22 17:59:16 +000099 .dma_threshold = true,
Rajanikanth H.V4fd06902012-03-26 11:17:02 +0200100 .cts_event_workaround = true,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101};
102
Shreshtha Kumar Sahuc16d51a2011-06-13 10:11:33 +0200103static struct uart_amba_port *amba_ports[UART_NR];
104
Russell King68b65f72010-12-22 17:24:39 +0000105/* Deals with DMA transactions */
Linus Walleijead76f32011-02-24 13:21:08 +0100106
107struct pl011_sgbuf {
108 struct scatterlist sg;
109 char *buf;
110};
111
112struct pl011_dmarx_data {
113 struct dma_chan *chan;
114 struct completion complete;
115 bool use_buf_b;
116 struct pl011_sgbuf sgbuf_a;
117 struct pl011_sgbuf sgbuf_b;
118 dma_cookie_t cookie;
119 bool running;
120};
121
Russell King68b65f72010-12-22 17:24:39 +0000122struct pl011_dmatx_data {
123 struct dma_chan *chan;
124 struct scatterlist sg;
125 char *buf;
126 bool queued;
127};
128
Russell Kingc19f12b2010-12-22 17:48:26 +0000129/*
130 * We wrap our port structure around the generic uart_port.
131 */
132struct uart_amba_port {
133 struct uart_port port;
134 struct clk *clk;
Linus Walleij78d80c52012-05-23 21:18:46 +0200135 /* Two optional pin states - default & sleep */
136 struct pinctrl *pinctrl;
137 struct pinctrl_state *pins_default;
138 struct pinctrl_state *pins_sleep;
Russell Kingc19f12b2010-12-22 17:48:26 +0000139 const struct vendor_data *vendor;
Russell King68b65f72010-12-22 17:24:39 +0000140 unsigned int dmacr; /* dma control reg */
Russell Kingc19f12b2010-12-22 17:48:26 +0000141 unsigned int im; /* interrupt mask */
142 unsigned int old_status;
Russell Kingffca2b12010-12-22 17:13:05 +0000143 unsigned int fifosize; /* vendor-specific */
Russell Kingc19f12b2010-12-22 17:48:26 +0000144 unsigned int lcrh_tx; /* vendor-specific */
145 unsigned int lcrh_rx; /* vendor-specific */
Shreshtha Kumar Sahud8d8ffa2012-01-18 15:53:59 +0530146 unsigned int old_cr; /* state during shutdown */
Russell Kingc19f12b2010-12-22 17:48:26 +0000147 bool autorts;
148 char type[12];
Russell King68b65f72010-12-22 17:24:39 +0000149#ifdef CONFIG_DMA_ENGINE
150 /* DMA stuff */
Linus Walleijead76f32011-02-24 13:21:08 +0100151 bool using_tx_dma;
152 bool using_rx_dma;
153 struct pl011_dmarx_data dmarx;
Russell King68b65f72010-12-22 17:24:39 +0000154 struct pl011_dmatx_data dmatx;
155#endif
Russell Kingc19f12b2010-12-22 17:48:26 +0000156};
157
Russell King68b65f72010-12-22 17:24:39 +0000158/*
Linus Walleij29772c42011-02-24 13:21:36 +0100159 * Reads up to 256 characters from the FIFO or until it's empty and
160 * inserts them into the TTY layer. Returns the number of characters
161 * read from the FIFO.
162 */
163static int pl011_fifo_to_tty(struct uart_amba_port *uap)
164{
165 u16 status, ch;
166 unsigned int flag, max_count = 256;
167 int fifotaken = 0;
168
169 while (max_count--) {
170 status = readw(uap->port.membase + UART01x_FR);
171 if (status & UART01x_FR_RXFE)
172 break;
173
174 /* Take chars from the FIFO and update status */
175 ch = readw(uap->port.membase + UART01x_DR) |
176 UART_DUMMY_DR_RX;
177 flag = TTY_NORMAL;
178 uap->port.icount.rx++;
179 fifotaken++;
180
181 if (unlikely(ch & UART_DR_ERROR)) {
182 if (ch & UART011_DR_BE) {
183 ch &= ~(UART011_DR_FE | UART011_DR_PE);
184 uap->port.icount.brk++;
185 if (uart_handle_break(&uap->port))
186 continue;
187 } else if (ch & UART011_DR_PE)
188 uap->port.icount.parity++;
189 else if (ch & UART011_DR_FE)
190 uap->port.icount.frame++;
191 if (ch & UART011_DR_OE)
192 uap->port.icount.overrun++;
193
194 ch &= uap->port.read_status_mask;
195
196 if (ch & UART011_DR_BE)
197 flag = TTY_BREAK;
198 else if (ch & UART011_DR_PE)
199 flag = TTY_PARITY;
200 else if (ch & UART011_DR_FE)
201 flag = TTY_FRAME;
202 }
203
204 if (uart_handle_sysrq_char(&uap->port, ch & 255))
205 continue;
206
207 uart_insert_char(&uap->port, ch, UART011_DR_OE, ch, flag);
208 }
209
210 return fifotaken;
211}
212
213
214/*
Russell King68b65f72010-12-22 17:24:39 +0000215 * All the DMA operation mode stuff goes inside this ifdef.
216 * This assumes that you have a generic DMA device interface,
217 * no custom DMA interfaces are supported.
218 */
219#ifdef CONFIG_DMA_ENGINE
220
221#define PL011_DMA_BUFFER_SIZE PAGE_SIZE
222
Linus Walleijead76f32011-02-24 13:21:08 +0100223static int pl011_sgbuf_init(struct dma_chan *chan, struct pl011_sgbuf *sg,
224 enum dma_data_direction dir)
225{
226 sg->buf = kmalloc(PL011_DMA_BUFFER_SIZE, GFP_KERNEL);
227 if (!sg->buf)
228 return -ENOMEM;
229
230 sg_init_one(&sg->sg, sg->buf, PL011_DMA_BUFFER_SIZE);
231
232 if (dma_map_sg(chan->device->dev, &sg->sg, 1, dir) != 1) {
233 kfree(sg->buf);
234 return -EINVAL;
235 }
236 return 0;
237}
238
239static void pl011_sgbuf_free(struct dma_chan *chan, struct pl011_sgbuf *sg,
240 enum dma_data_direction dir)
241{
242 if (sg->buf) {
243 dma_unmap_sg(chan->device->dev, &sg->sg, 1, dir);
244 kfree(sg->buf);
245 }
246}
247
Arnd Bergmann787b0c12013-01-28 16:24:37 +0000248static void pl011_dma_probe_initcall(struct device *dev, struct uart_amba_port *uap)
Russell King68b65f72010-12-22 17:24:39 +0000249{
250 /* DMA is the sole user of the platform data right now */
251 struct amba_pl011_data *plat = uap->port.dev->platform_data;
252 struct dma_slave_config tx_conf = {
253 .dst_addr = uap->port.mapbase + UART01x_DR,
254 .dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE,
Vinod Koula485df42011-10-14 10:47:38 +0530255 .direction = DMA_MEM_TO_DEV,
Russell King68b65f72010-12-22 17:24:39 +0000256 .dst_maxburst = uap->fifosize >> 1,
Viresh Kumar258aea72012-02-01 16:12:19 +0530257 .device_fc = false,
Russell King68b65f72010-12-22 17:24:39 +0000258 };
259 struct dma_chan *chan;
260 dma_cap_mask_t mask;
261
Arnd Bergmann787b0c12013-01-28 16:24:37 +0000262 chan = dma_request_slave_channel(dev, "tx");
Russell King68b65f72010-12-22 17:24:39 +0000263
Russell King68b65f72010-12-22 17:24:39 +0000264 if (!chan) {
Arnd Bergmann787b0c12013-01-28 16:24:37 +0000265 /* We need platform data */
266 if (!plat || !plat->dma_filter) {
267 dev_info(uap->port.dev, "no DMA platform data\n");
268 return;
269 }
270
271 /* Try to acquire a generic DMA engine slave TX channel */
272 dma_cap_zero(mask);
273 dma_cap_set(DMA_SLAVE, mask);
274
275 chan = dma_request_channel(mask, plat->dma_filter,
276 plat->dma_tx_param);
277 if (!chan) {
278 dev_err(uap->port.dev, "no TX DMA channel!\n");
279 return;
280 }
Russell King68b65f72010-12-22 17:24:39 +0000281 }
282
283 dmaengine_slave_config(chan, &tx_conf);
284 uap->dmatx.chan = chan;
285
286 dev_info(uap->port.dev, "DMA channel TX %s\n",
287 dma_chan_name(uap->dmatx.chan));
Linus Walleijead76f32011-02-24 13:21:08 +0100288
289 /* Optionally make use of an RX channel as well */
Arnd Bergmann787b0c12013-01-28 16:24:37 +0000290 chan = dma_request_slave_channel(dev, "rx");
291
292 if (!chan && plat->dma_rx_param) {
293 chan = dma_request_channel(mask, plat->dma_filter, plat->dma_rx_param);
294
295 if (!chan) {
296 dev_err(uap->port.dev, "no RX DMA channel!\n");
297 return;
298 }
299 }
300
301 if (chan) {
Linus Walleijead76f32011-02-24 13:21:08 +0100302 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,
Viresh Kumar258aea72012-02-01 16:12:19 +0530307 .device_fc = false,
Linus Walleijead76f32011-02-24 13:21:08 +0100308 };
309
Linus Walleijead76f32011-02-24 13:21:08 +0100310 dmaengine_slave_config(chan, &rx_conf);
311 uap->dmarx.chan = chan;
312
313 dev_info(uap->port.dev, "DMA channel RX %s\n",
314 dma_chan_name(uap->dmarx.chan));
315 }
Russell King68b65f72010-12-22 17:24:39 +0000316}
317
318#ifndef MODULE
319/*
320 * Stack up the UARTs and let the above initcall be done at device
321 * initcall time, because the serial driver is called as an arch
322 * initcall, and at this time the DMA subsystem is not yet registered.
323 * At this point the driver will switch over to using DMA where desired.
324 */
325struct dma_uap {
326 struct list_head node;
327 struct uart_amba_port *uap;
Arnd Bergmann787b0c12013-01-28 16:24:37 +0000328 struct device *dev;
Russell King68b65f72010-12-22 17:24:39 +0000329};
330
331static LIST_HEAD(pl011_dma_uarts);
332
333static int __init pl011_dma_initcall(void)
334{
335 struct list_head *node, *tmp;
336
337 list_for_each_safe(node, tmp, &pl011_dma_uarts) {
338 struct dma_uap *dmau = list_entry(node, struct dma_uap, node);
Arnd Bergmann787b0c12013-01-28 16:24:37 +0000339 pl011_dma_probe_initcall(dmau->dev, dmau->uap);
Russell King68b65f72010-12-22 17:24:39 +0000340 list_del(node);
341 kfree(dmau);
342 }
343 return 0;
344}
345
346device_initcall(pl011_dma_initcall);
347
Arnd Bergmann787b0c12013-01-28 16:24:37 +0000348static void pl011_dma_probe(struct device *dev, struct uart_amba_port *uap)
Russell King68b65f72010-12-22 17:24:39 +0000349{
350 struct dma_uap *dmau = kzalloc(sizeof(struct dma_uap), GFP_KERNEL);
351 if (dmau) {
352 dmau->uap = uap;
Arnd Bergmann787b0c12013-01-28 16:24:37 +0000353 dmau->dev = dev;
Russell King68b65f72010-12-22 17:24:39 +0000354 list_add_tail(&dmau->node, &pl011_dma_uarts);
355 }
356}
357#else
Arnd Bergmann787b0c12013-01-28 16:24:37 +0000358static void pl011_dma_probe(struct device *dev, struct uart_amba_port *uap)
Russell King68b65f72010-12-22 17:24:39 +0000359{
Arnd Bergmann787b0c12013-01-28 16:24:37 +0000360 pl011_dma_probe_initcall(dev, uap);
Russell King68b65f72010-12-22 17:24:39 +0000361}
362#endif
363
364static void pl011_dma_remove(struct uart_amba_port *uap)
365{
366 /* TODO: remove the initcall if it has not yet executed */
367 if (uap->dmatx.chan)
368 dma_release_channel(uap->dmatx.chan);
Linus Walleijead76f32011-02-24 13:21:08 +0100369 if (uap->dmarx.chan)
370 dma_release_channel(uap->dmarx.chan);
Russell King68b65f72010-12-22 17:24:39 +0000371}
372
Russell King68b65f72010-12-22 17:24:39 +0000373/* Forward declare this for the refill routine */
374static int pl011_dma_tx_refill(struct uart_amba_port *uap);
375
376/*
377 * The current DMA TX buffer has been sent.
378 * Try to queue up another DMA buffer.
379 */
380static void pl011_dma_tx_callback(void *data)
381{
382 struct uart_amba_port *uap = data;
383 struct pl011_dmatx_data *dmatx = &uap->dmatx;
384 unsigned long flags;
385 u16 dmacr;
386
387 spin_lock_irqsave(&uap->port.lock, flags);
388 if (uap->dmatx.queued)
389 dma_unmap_sg(dmatx->chan->device->dev, &dmatx->sg, 1,
390 DMA_TO_DEVICE);
391
392 dmacr = uap->dmacr;
393 uap->dmacr = dmacr & ~UART011_TXDMAE;
394 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
395
396 /*
397 * If TX DMA was disabled, it means that we've stopped the DMA for
398 * some reason (eg, XOFF received, or we want to send an X-char.)
399 *
400 * Note: we need to be careful here of a potential race between DMA
401 * and the rest of the driver - if the driver disables TX DMA while
402 * a TX buffer completing, we must update the tx queued status to
403 * get further refills (hence we check dmacr).
404 */
405 if (!(dmacr & UART011_TXDMAE) || uart_tx_stopped(&uap->port) ||
406 uart_circ_empty(&uap->port.state->xmit)) {
407 uap->dmatx.queued = false;
408 spin_unlock_irqrestore(&uap->port.lock, flags);
409 return;
410 }
411
412 if (pl011_dma_tx_refill(uap) <= 0) {
413 /*
414 * We didn't queue a DMA buffer for some reason, but we
415 * have data pending to be sent. Re-enable the TX IRQ.
416 */
417 uap->im |= UART011_TXIM;
418 writew(uap->im, uap->port.membase + UART011_IMSC);
419 }
420 spin_unlock_irqrestore(&uap->port.lock, flags);
421}
422
423/*
424 * Try to refill the TX DMA buffer.
425 * Locking: called with port lock held and IRQs disabled.
426 * Returns:
427 * 1 if we queued up a TX DMA buffer.
428 * 0 if we didn't want to handle this by DMA
429 * <0 on error
430 */
431static int pl011_dma_tx_refill(struct uart_amba_port *uap)
432{
433 struct pl011_dmatx_data *dmatx = &uap->dmatx;
434 struct dma_chan *chan = dmatx->chan;
435 struct dma_device *dma_dev = chan->device;
436 struct dma_async_tx_descriptor *desc;
437 struct circ_buf *xmit = &uap->port.state->xmit;
438 unsigned int count;
439
440 /*
441 * Try to avoid the overhead involved in using DMA if the
442 * transaction fits in the first half of the FIFO, by using
443 * the standard interrupt handling. This ensures that we
444 * issue a uart_write_wakeup() at the appropriate time.
445 */
446 count = uart_circ_chars_pending(xmit);
447 if (count < (uap->fifosize >> 1)) {
448 uap->dmatx.queued = false;
449 return 0;
450 }
451
452 /*
453 * Bodge: don't send the last character by DMA, as this
454 * will prevent XON from notifying us to restart DMA.
455 */
456 count -= 1;
457
458 /* Else proceed to copy the TX chars to the DMA buffer and fire DMA */
459 if (count > PL011_DMA_BUFFER_SIZE)
460 count = PL011_DMA_BUFFER_SIZE;
461
462 if (xmit->tail < xmit->head)
463 memcpy(&dmatx->buf[0], &xmit->buf[xmit->tail], count);
464 else {
465 size_t first = UART_XMIT_SIZE - xmit->tail;
466 size_t second = xmit->head;
467
468 memcpy(&dmatx->buf[0], &xmit->buf[xmit->tail], first);
469 if (second)
470 memcpy(&dmatx->buf[first], &xmit->buf[0], second);
471 }
472
473 dmatx->sg.length = count;
474
475 if (dma_map_sg(dma_dev->dev, &dmatx->sg, 1, DMA_TO_DEVICE) != 1) {
476 uap->dmatx.queued = false;
477 dev_dbg(uap->port.dev, "unable to map TX DMA\n");
478 return -EBUSY;
479 }
480
Alexandre Bounine16052822012-03-08 16:11:18 -0500481 desc = dmaengine_prep_slave_sg(chan, &dmatx->sg, 1, DMA_MEM_TO_DEV,
Russell King68b65f72010-12-22 17:24:39 +0000482 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
483 if (!desc) {
484 dma_unmap_sg(dma_dev->dev, &dmatx->sg, 1, DMA_TO_DEVICE);
485 uap->dmatx.queued = false;
486 /*
487 * If DMA cannot be used right now, we complete this
488 * transaction via IRQ and let the TTY layer retry.
489 */
490 dev_dbg(uap->port.dev, "TX DMA busy\n");
491 return -EBUSY;
492 }
493
494 /* Some data to go along to the callback */
495 desc->callback = pl011_dma_tx_callback;
496 desc->callback_param = uap;
497
498 /* All errors should happen at prepare time */
499 dmaengine_submit(desc);
500
501 /* Fire the DMA transaction */
502 dma_dev->device_issue_pending(chan);
503
504 uap->dmacr |= UART011_TXDMAE;
505 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
506 uap->dmatx.queued = true;
507
508 /*
509 * Now we know that DMA will fire, so advance the ring buffer
510 * with the stuff we just dispatched.
511 */
512 xmit->tail = (xmit->tail + count) & (UART_XMIT_SIZE - 1);
513 uap->port.icount.tx += count;
514
515 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
516 uart_write_wakeup(&uap->port);
517
518 return 1;
519}
520
521/*
522 * We received a transmit interrupt without a pending X-char but with
523 * pending characters.
524 * Locking: called with port lock held and IRQs disabled.
525 * Returns:
526 * false if we want to use PIO to transmit
527 * true if we queued a DMA buffer
528 */
529static bool pl011_dma_tx_irq(struct uart_amba_port *uap)
530{
Linus Walleijead76f32011-02-24 13:21:08 +0100531 if (!uap->using_tx_dma)
Russell King68b65f72010-12-22 17:24:39 +0000532 return false;
533
534 /*
535 * If we already have a TX buffer queued, but received a
536 * TX interrupt, it will be because we've just sent an X-char.
537 * Ensure the TX DMA is enabled and the TX IRQ is disabled.
538 */
539 if (uap->dmatx.queued) {
540 uap->dmacr |= UART011_TXDMAE;
541 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
542 uap->im &= ~UART011_TXIM;
543 writew(uap->im, uap->port.membase + UART011_IMSC);
544 return true;
545 }
546
547 /*
548 * We don't have a TX buffer queued, so try to queue one.
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300549 * If we successfully queued a buffer, mask the TX IRQ.
Russell King68b65f72010-12-22 17:24:39 +0000550 */
551 if (pl011_dma_tx_refill(uap) > 0) {
552 uap->im &= ~UART011_TXIM;
553 writew(uap->im, uap->port.membase + UART011_IMSC);
554 return true;
555 }
556 return false;
557}
558
559/*
560 * Stop the DMA transmit (eg, due to received XOFF).
561 * Locking: called with port lock held and IRQs disabled.
562 */
563static inline void pl011_dma_tx_stop(struct uart_amba_port *uap)
564{
565 if (uap->dmatx.queued) {
566 uap->dmacr &= ~UART011_TXDMAE;
567 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
568 }
569}
570
571/*
572 * Try to start a DMA transmit, or in the case of an XON/OFF
573 * character queued for send, try to get that character out ASAP.
574 * Locking: called with port lock held and IRQs disabled.
575 * Returns:
576 * false if we want the TX IRQ to be enabled
577 * true if we have a buffer queued
578 */
579static inline bool pl011_dma_tx_start(struct uart_amba_port *uap)
580{
581 u16 dmacr;
582
Linus Walleijead76f32011-02-24 13:21:08 +0100583 if (!uap->using_tx_dma)
Russell King68b65f72010-12-22 17:24:39 +0000584 return false;
585
586 if (!uap->port.x_char) {
587 /* no X-char, try to push chars out in DMA mode */
588 bool ret = true;
589
590 if (!uap->dmatx.queued) {
591 if (pl011_dma_tx_refill(uap) > 0) {
592 uap->im &= ~UART011_TXIM;
593 ret = true;
594 } else {
595 uap->im |= UART011_TXIM;
596 ret = false;
597 }
598 writew(uap->im, uap->port.membase + UART011_IMSC);
599 } else if (!(uap->dmacr & UART011_TXDMAE)) {
600 uap->dmacr |= UART011_TXDMAE;
601 writew(uap->dmacr,
602 uap->port.membase + UART011_DMACR);
603 }
604 return ret;
605 }
606
607 /*
608 * We have an X-char to send. Disable DMA to prevent it loading
609 * the TX fifo, and then see if we can stuff it into the FIFO.
610 */
611 dmacr = uap->dmacr;
612 uap->dmacr &= ~UART011_TXDMAE;
613 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
614
615 if (readw(uap->port.membase + UART01x_FR) & UART01x_FR_TXFF) {
616 /*
617 * No space in the FIFO, so enable the transmit interrupt
618 * so we know when there is space. Note that once we've
619 * loaded the character, we should just re-enable DMA.
620 */
621 return false;
622 }
623
624 writew(uap->port.x_char, uap->port.membase + UART01x_DR);
625 uap->port.icount.tx++;
626 uap->port.x_char = 0;
627
628 /* Success - restore the DMA state */
629 uap->dmacr = dmacr;
630 writew(dmacr, uap->port.membase + UART011_DMACR);
631
632 return true;
633}
634
635/*
636 * Flush the transmit buffer.
637 * Locking: called with port lock held and IRQs disabled.
638 */
639static void pl011_dma_flush_buffer(struct uart_port *port)
640{
641 struct uart_amba_port *uap = (struct uart_amba_port *)port;
642
Linus Walleijead76f32011-02-24 13:21:08 +0100643 if (!uap->using_tx_dma)
Russell King68b65f72010-12-22 17:24:39 +0000644 return;
645
646 /* Avoid deadlock with the DMA engine callback */
647 spin_unlock(&uap->port.lock);
648 dmaengine_terminate_all(uap->dmatx.chan);
649 spin_lock(&uap->port.lock);
650 if (uap->dmatx.queued) {
651 dma_unmap_sg(uap->dmatx.chan->device->dev, &uap->dmatx.sg, 1,
652 DMA_TO_DEVICE);
653 uap->dmatx.queued = false;
654 uap->dmacr &= ~UART011_TXDMAE;
655 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
656 }
657}
658
Linus Walleijead76f32011-02-24 13:21:08 +0100659static void pl011_dma_rx_callback(void *data);
660
661static int pl011_dma_rx_trigger_dma(struct uart_amba_port *uap)
662{
663 struct dma_chan *rxchan = uap->dmarx.chan;
Linus Walleijead76f32011-02-24 13:21:08 +0100664 struct pl011_dmarx_data *dmarx = &uap->dmarx;
665 struct dma_async_tx_descriptor *desc;
666 struct pl011_sgbuf *sgbuf;
667
668 if (!rxchan)
669 return -EIO;
670
671 /* Start the RX DMA job */
672 sgbuf = uap->dmarx.use_buf_b ?
673 &uap->dmarx.sgbuf_b : &uap->dmarx.sgbuf_a;
Alexandre Bounine16052822012-03-08 16:11:18 -0500674 desc = dmaengine_prep_slave_sg(rxchan, &sgbuf->sg, 1,
Vinod Koula485df42011-10-14 10:47:38 +0530675 DMA_DEV_TO_MEM,
Linus Walleijead76f32011-02-24 13:21:08 +0100676 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
677 /*
678 * If the DMA engine is busy and cannot prepare a
679 * channel, no big deal, the driver will fall back
680 * to interrupt mode as a result of this error code.
681 */
682 if (!desc) {
683 uap->dmarx.running = false;
684 dmaengine_terminate_all(rxchan);
685 return -EBUSY;
686 }
687
688 /* Some data to go along to the callback */
689 desc->callback = pl011_dma_rx_callback;
690 desc->callback_param = uap;
691 dmarx->cookie = dmaengine_submit(desc);
692 dma_async_issue_pending(rxchan);
693
694 uap->dmacr |= UART011_RXDMAE;
695 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
696 uap->dmarx.running = true;
697
698 uap->im &= ~UART011_RXIM;
699 writew(uap->im, uap->port.membase + UART011_IMSC);
700
701 return 0;
702}
703
704/*
705 * This is called when either the DMA job is complete, or
706 * the FIFO timeout interrupt occurred. This must be called
707 * with the port spinlock uap->port.lock held.
708 */
709static void pl011_dma_rx_chars(struct uart_amba_port *uap,
710 u32 pending, bool use_buf_b,
711 bool readfifo)
712{
Jiri Slaby05c7cd32013-01-03 15:53:04 +0100713 struct tty_port *port = &uap->port.state->port;
Linus Walleijead76f32011-02-24 13:21:08 +0100714 struct pl011_sgbuf *sgbuf = use_buf_b ?
715 &uap->dmarx.sgbuf_b : &uap->dmarx.sgbuf_a;
716 struct device *dev = uap->dmarx.chan->device->dev;
Linus Walleijead76f32011-02-24 13:21:08 +0100717 int dma_count = 0;
718 u32 fifotaken = 0; /* only used for vdbg() */
719
720 /* Pick everything from the DMA first */
721 if (pending) {
722 /* Sync in buffer */
723 dma_sync_sg_for_cpu(dev, &sgbuf->sg, 1, DMA_FROM_DEVICE);
724
725 /*
726 * First take all chars in the DMA pipe, then look in the FIFO.
727 * Note that tty_insert_flip_buf() tries to take as many chars
728 * as it can.
729 */
Jiri Slaby05c7cd32013-01-03 15:53:04 +0100730 dma_count = tty_insert_flip_string(port, sgbuf->buf, pending);
Linus Walleijead76f32011-02-24 13:21:08 +0100731
732 /* Return buffer to device */
733 dma_sync_sg_for_device(dev, &sgbuf->sg, 1, DMA_FROM_DEVICE);
734
735 uap->port.icount.rx += dma_count;
736 if (dma_count < pending)
737 dev_warn(uap->port.dev,
738 "couldn't insert all characters (TTY is full?)\n");
739 }
740
741 /*
742 * Only continue with trying to read the FIFO if all DMA chars have
743 * been taken first.
744 */
745 if (dma_count == pending && readfifo) {
746 /* Clear any error flags */
747 writew(UART011_OEIS | UART011_BEIS | UART011_PEIS | UART011_FEIS,
748 uap->port.membase + UART011_ICR);
749
750 /*
751 * If we read all the DMA'd characters, and we had an
Linus Walleij29772c42011-02-24 13:21:36 +0100752 * incomplete buffer, that could be due to an rx error, or
753 * maybe we just timed out. Read any pending chars and check
754 * the error status.
755 *
756 * Error conditions will only occur in the FIFO, these will
757 * trigger an immediate interrupt and stop the DMA job, so we
758 * will always find the error in the FIFO, never in the DMA
759 * buffer.
Linus Walleijead76f32011-02-24 13:21:08 +0100760 */
Linus Walleij29772c42011-02-24 13:21:36 +0100761 fifotaken = pl011_fifo_to_tty(uap);
Linus Walleijead76f32011-02-24 13:21:08 +0100762 }
763
764 spin_unlock(&uap->port.lock);
765 dev_vdbg(uap->port.dev,
766 "Took %d chars from DMA buffer and %d chars from the FIFO\n",
767 dma_count, fifotaken);
Jiri Slaby2e124b42013-01-03 15:53:06 +0100768 tty_flip_buffer_push(port);
Linus Walleijead76f32011-02-24 13:21:08 +0100769 spin_lock(&uap->port.lock);
770}
771
772static void pl011_dma_rx_irq(struct uart_amba_port *uap)
773{
774 struct pl011_dmarx_data *dmarx = &uap->dmarx;
775 struct dma_chan *rxchan = dmarx->chan;
776 struct pl011_sgbuf *sgbuf = dmarx->use_buf_b ?
777 &dmarx->sgbuf_b : &dmarx->sgbuf_a;
778 size_t pending;
779 struct dma_tx_state state;
780 enum dma_status dmastat;
781
782 /*
783 * Pause the transfer so we can trust the current counter,
784 * do this before we pause the PL011 block, else we may
785 * overflow the FIFO.
786 */
787 if (dmaengine_pause(rxchan))
788 dev_err(uap->port.dev, "unable to pause DMA transfer\n");
789 dmastat = rxchan->device->device_tx_status(rxchan,
790 dmarx->cookie, &state);
791 if (dmastat != DMA_PAUSED)
792 dev_err(uap->port.dev, "unable to pause DMA transfer\n");
793
794 /* Disable RX DMA - incoming data will wait in the FIFO */
795 uap->dmacr &= ~UART011_RXDMAE;
796 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
797 uap->dmarx.running = false;
798
799 pending = sgbuf->sg.length - state.residue;
800 BUG_ON(pending > PL011_DMA_BUFFER_SIZE);
801 /* Then we terminate the transfer - we now know our residue */
802 dmaengine_terminate_all(rxchan);
803
804 /*
805 * This will take the chars we have so far and insert
806 * into the framework.
807 */
808 pl011_dma_rx_chars(uap, pending, dmarx->use_buf_b, true);
809
810 /* Switch buffer & re-trigger DMA job */
811 dmarx->use_buf_b = !dmarx->use_buf_b;
812 if (pl011_dma_rx_trigger_dma(uap)) {
813 dev_dbg(uap->port.dev, "could not retrigger RX DMA job "
814 "fall back to interrupt mode\n");
815 uap->im |= UART011_RXIM;
816 writew(uap->im, uap->port.membase + UART011_IMSC);
817 }
818}
819
820static void pl011_dma_rx_callback(void *data)
821{
822 struct uart_amba_port *uap = data;
823 struct pl011_dmarx_data *dmarx = &uap->dmarx;
Chanho Min6dc01aa2012-02-20 10:24:40 +0900824 struct dma_chan *rxchan = dmarx->chan;
Linus Walleijead76f32011-02-24 13:21:08 +0100825 bool lastbuf = dmarx->use_buf_b;
Chanho Min6dc01aa2012-02-20 10:24:40 +0900826 struct pl011_sgbuf *sgbuf = dmarx->use_buf_b ?
827 &dmarx->sgbuf_b : &dmarx->sgbuf_a;
828 size_t pending;
829 struct dma_tx_state state;
Linus Walleijead76f32011-02-24 13:21:08 +0100830 int ret;
831
832 /*
833 * This completion interrupt occurs typically when the
834 * RX buffer is totally stuffed but no timeout has yet
835 * occurred. When that happens, we just want the RX
836 * routine to flush out the secondary DMA buffer while
837 * we immediately trigger the next DMA job.
838 */
839 spin_lock_irq(&uap->port.lock);
Chanho Min6dc01aa2012-02-20 10:24:40 +0900840 /*
841 * Rx data can be taken by the UART interrupts during
842 * the DMA irq handler. So we check the residue here.
843 */
844 rxchan->device->device_tx_status(rxchan, dmarx->cookie, &state);
845 pending = sgbuf->sg.length - state.residue;
846 BUG_ON(pending > PL011_DMA_BUFFER_SIZE);
847 /* Then we terminate the transfer - we now know our residue */
848 dmaengine_terminate_all(rxchan);
849
Linus Walleijead76f32011-02-24 13:21:08 +0100850 uap->dmarx.running = false;
851 dmarx->use_buf_b = !lastbuf;
852 ret = pl011_dma_rx_trigger_dma(uap);
853
Chanho Min6dc01aa2012-02-20 10:24:40 +0900854 pl011_dma_rx_chars(uap, pending, lastbuf, false);
Linus Walleijead76f32011-02-24 13:21:08 +0100855 spin_unlock_irq(&uap->port.lock);
856 /*
857 * Do this check after we picked the DMA chars so we don't
858 * get some IRQ immediately from RX.
859 */
860 if (ret) {
861 dev_dbg(uap->port.dev, "could not retrigger RX DMA job "
862 "fall back to interrupt mode\n");
863 uap->im |= UART011_RXIM;
864 writew(uap->im, uap->port.membase + UART011_IMSC);
865 }
866}
867
868/*
869 * Stop accepting received characters, when we're shutting down or
870 * suspending this port.
871 * Locking: called with port lock held and IRQs disabled.
872 */
873static inline void pl011_dma_rx_stop(struct uart_amba_port *uap)
874{
875 /* FIXME. Just disable the DMA enable */
876 uap->dmacr &= ~UART011_RXDMAE;
877 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
878}
Russell King68b65f72010-12-22 17:24:39 +0000879
880static void pl011_dma_startup(struct uart_amba_port *uap)
881{
Linus Walleijead76f32011-02-24 13:21:08 +0100882 int ret;
883
Russell King68b65f72010-12-22 17:24:39 +0000884 if (!uap->dmatx.chan)
885 return;
886
887 uap->dmatx.buf = kmalloc(PL011_DMA_BUFFER_SIZE, GFP_KERNEL);
888 if (!uap->dmatx.buf) {
889 dev_err(uap->port.dev, "no memory for DMA TX buffer\n");
890 uap->port.fifosize = uap->fifosize;
891 return;
892 }
893
894 sg_init_one(&uap->dmatx.sg, uap->dmatx.buf, PL011_DMA_BUFFER_SIZE);
895
896 /* The DMA buffer is now the FIFO the TTY subsystem can use */
897 uap->port.fifosize = PL011_DMA_BUFFER_SIZE;
Linus Walleijead76f32011-02-24 13:21:08 +0100898 uap->using_tx_dma = true;
Russell King68b65f72010-12-22 17:24:39 +0000899
Linus Walleijead76f32011-02-24 13:21:08 +0100900 if (!uap->dmarx.chan)
901 goto skip_rx;
902
903 /* Allocate and map DMA RX buffers */
904 ret = pl011_sgbuf_init(uap->dmarx.chan, &uap->dmarx.sgbuf_a,
905 DMA_FROM_DEVICE);
906 if (ret) {
907 dev_err(uap->port.dev, "failed to init DMA %s: %d\n",
908 "RX buffer A", ret);
909 goto skip_rx;
910 }
911
912 ret = pl011_sgbuf_init(uap->dmarx.chan, &uap->dmarx.sgbuf_b,
913 DMA_FROM_DEVICE);
914 if (ret) {
915 dev_err(uap->port.dev, "failed to init DMA %s: %d\n",
916 "RX buffer B", ret);
917 pl011_sgbuf_free(uap->dmarx.chan, &uap->dmarx.sgbuf_a,
918 DMA_FROM_DEVICE);
919 goto skip_rx;
920 }
921
922 uap->using_rx_dma = true;
923
924skip_rx:
Russell King68b65f72010-12-22 17:24:39 +0000925 /* Turn on DMA error (RX/TX will be enabled on demand) */
926 uap->dmacr |= UART011_DMAONERR;
927 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
Russell King38d62432010-12-22 17:59:16 +0000928
929 /*
930 * ST Micro variants has some specific dma burst threshold
931 * compensation. Set this to 16 bytes, so burst will only
932 * be issued above/below 16 bytes.
933 */
934 if (uap->vendor->dma_threshold)
935 writew(ST_UART011_DMAWM_RX_16 | ST_UART011_DMAWM_TX_16,
936 uap->port.membase + ST_UART011_DMAWM);
Linus Walleijead76f32011-02-24 13:21:08 +0100937
938 if (uap->using_rx_dma) {
939 if (pl011_dma_rx_trigger_dma(uap))
940 dev_dbg(uap->port.dev, "could not trigger initial "
941 "RX DMA job, fall back to interrupt mode\n");
942 }
Russell King68b65f72010-12-22 17:24:39 +0000943}
944
945static void pl011_dma_shutdown(struct uart_amba_port *uap)
946{
Linus Walleijead76f32011-02-24 13:21:08 +0100947 if (!(uap->using_tx_dma || uap->using_rx_dma))
Russell King68b65f72010-12-22 17:24:39 +0000948 return;
949
950 /* Disable RX and TX DMA */
951 while (readw(uap->port.membase + UART01x_FR) & UART01x_FR_BUSY)
952 barrier();
953
954 spin_lock_irq(&uap->port.lock);
955 uap->dmacr &= ~(UART011_DMAONERR | UART011_RXDMAE | UART011_TXDMAE);
956 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
957 spin_unlock_irq(&uap->port.lock);
958
Linus Walleijead76f32011-02-24 13:21:08 +0100959 if (uap->using_tx_dma) {
960 /* In theory, this should already be done by pl011_dma_flush_buffer */
961 dmaengine_terminate_all(uap->dmatx.chan);
962 if (uap->dmatx.queued) {
963 dma_unmap_sg(uap->dmatx.chan->device->dev, &uap->dmatx.sg, 1,
964 DMA_TO_DEVICE);
965 uap->dmatx.queued = false;
966 }
967
968 kfree(uap->dmatx.buf);
969 uap->using_tx_dma = false;
Russell King68b65f72010-12-22 17:24:39 +0000970 }
971
Linus Walleijead76f32011-02-24 13:21:08 +0100972 if (uap->using_rx_dma) {
973 dmaengine_terminate_all(uap->dmarx.chan);
974 /* Clean up the RX DMA */
975 pl011_sgbuf_free(uap->dmarx.chan, &uap->dmarx.sgbuf_a, DMA_FROM_DEVICE);
976 pl011_sgbuf_free(uap->dmarx.chan, &uap->dmarx.sgbuf_b, DMA_FROM_DEVICE);
977 uap->using_rx_dma = false;
978 }
Russell King68b65f72010-12-22 17:24:39 +0000979}
980
Linus Walleijead76f32011-02-24 13:21:08 +0100981static inline bool pl011_dma_rx_available(struct uart_amba_port *uap)
982{
983 return uap->using_rx_dma;
984}
985
986static inline bool pl011_dma_rx_running(struct uart_amba_port *uap)
987{
988 return uap->using_rx_dma && uap->dmarx.running;
989}
990
991
Russell King68b65f72010-12-22 17:24:39 +0000992#else
993/* Blank functions if the DMA engine is not available */
994static inline void pl011_dma_probe(struct uart_amba_port *uap)
995{
996}
997
998static inline void pl011_dma_remove(struct uart_amba_port *uap)
999{
1000}
1001
1002static inline void pl011_dma_startup(struct uart_amba_port *uap)
1003{
1004}
1005
1006static inline void pl011_dma_shutdown(struct uart_amba_port *uap)
1007{
1008}
1009
1010static inline bool pl011_dma_tx_irq(struct uart_amba_port *uap)
1011{
1012 return false;
1013}
1014
1015static inline void pl011_dma_tx_stop(struct uart_amba_port *uap)
1016{
1017}
1018
1019static inline bool pl011_dma_tx_start(struct uart_amba_port *uap)
1020{
1021 return false;
1022}
1023
Linus Walleijead76f32011-02-24 13:21:08 +01001024static inline void pl011_dma_rx_irq(struct uart_amba_port *uap)
1025{
1026}
1027
1028static inline void pl011_dma_rx_stop(struct uart_amba_port *uap)
1029{
1030}
1031
1032static inline int pl011_dma_rx_trigger_dma(struct uart_amba_port *uap)
1033{
1034 return -EIO;
1035}
1036
1037static inline bool pl011_dma_rx_available(struct uart_amba_port *uap)
1038{
1039 return false;
1040}
1041
1042static inline bool pl011_dma_rx_running(struct uart_amba_port *uap)
1043{
1044 return false;
1045}
1046
Russell King68b65f72010-12-22 17:24:39 +00001047#define pl011_dma_flush_buffer NULL
1048#endif
1049
Russell Kingb129a8c2005-08-31 10:12:14 +01001050static void pl011_stop_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051{
1052 struct uart_amba_port *uap = (struct uart_amba_port *)port;
1053
1054 uap->im &= ~UART011_TXIM;
1055 writew(uap->im, uap->port.membase + UART011_IMSC);
Russell King68b65f72010-12-22 17:24:39 +00001056 pl011_dma_tx_stop(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057}
1058
Russell Kingb129a8c2005-08-31 10:12:14 +01001059static void pl011_start_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060{
1061 struct uart_amba_port *uap = (struct uart_amba_port *)port;
1062
Russell King68b65f72010-12-22 17:24:39 +00001063 if (!pl011_dma_tx_start(uap)) {
1064 uap->im |= UART011_TXIM;
1065 writew(uap->im, uap->port.membase + UART011_IMSC);
1066 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067}
1068
1069static void pl011_stop_rx(struct uart_port *port)
1070{
1071 struct uart_amba_port *uap = (struct uart_amba_port *)port;
1072
1073 uap->im &= ~(UART011_RXIM|UART011_RTIM|UART011_FEIM|
1074 UART011_PEIM|UART011_BEIM|UART011_OEIM);
1075 writew(uap->im, uap->port.membase + UART011_IMSC);
Linus Walleijead76f32011-02-24 13:21:08 +01001076
1077 pl011_dma_rx_stop(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078}
1079
1080static void pl011_enable_ms(struct uart_port *port)
1081{
1082 struct uart_amba_port *uap = (struct uart_amba_port *)port;
1083
1084 uap->im |= UART011_RIMIM|UART011_CTSMIM|UART011_DCDMIM|UART011_DSRMIM;
1085 writew(uap->im, uap->port.membase + UART011_IMSC);
1086}
1087
David Howells7d12e782006-10-05 14:55:46 +01001088static void pl011_rx_chars(struct uart_amba_port *uap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089{
Linus Walleij29772c42011-02-24 13:21:36 +01001090 pl011_fifo_to_tty(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091
Thomas Gleixner2389b272007-05-29 21:53:50 +01001092 spin_unlock(&uap->port.lock);
Jiri Slaby2e124b42013-01-03 15:53:06 +01001093 tty_flip_buffer_push(&uap->port.state->port);
Linus Walleijead76f32011-02-24 13:21:08 +01001094 /*
1095 * If we were temporarily out of DMA mode for a while,
1096 * attempt to switch back to DMA mode again.
1097 */
1098 if (pl011_dma_rx_available(uap)) {
1099 if (pl011_dma_rx_trigger_dma(uap)) {
1100 dev_dbg(uap->port.dev, "could not trigger RX DMA job "
1101 "fall back to interrupt mode again\n");
1102 uap->im |= UART011_RXIM;
1103 } else
1104 uap->im &= ~UART011_RXIM;
1105 writew(uap->im, uap->port.membase + UART011_IMSC);
1106 }
Thomas Gleixner2389b272007-05-29 21:53:50 +01001107 spin_lock(&uap->port.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108}
1109
1110static void pl011_tx_chars(struct uart_amba_port *uap)
1111{
Alan Coxebd2c8f2009-09-19 13:13:28 -07001112 struct circ_buf *xmit = &uap->port.state->xmit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 int count;
1114
1115 if (uap->port.x_char) {
1116 writew(uap->port.x_char, uap->port.membase + UART01x_DR);
1117 uap->port.icount.tx++;
1118 uap->port.x_char = 0;
1119 return;
1120 }
1121 if (uart_circ_empty(xmit) || uart_tx_stopped(&uap->port)) {
Russell Kingb129a8c2005-08-31 10:12:14 +01001122 pl011_stop_tx(&uap->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 return;
1124 }
1125
Russell King68b65f72010-12-22 17:24:39 +00001126 /* If we are using DMA mode, try to send some characters. */
1127 if (pl011_dma_tx_irq(uap))
1128 return;
1129
Russell Kingffca2b12010-12-22 17:13:05 +00001130 count = uap->fifosize >> 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 do {
1132 writew(xmit->buf[xmit->tail], uap->port.membase + UART01x_DR);
1133 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
1134 uap->port.icount.tx++;
1135 if (uart_circ_empty(xmit))
1136 break;
1137 } while (--count > 0);
1138
1139 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
1140 uart_write_wakeup(&uap->port);
1141
1142 if (uart_circ_empty(xmit))
Russell Kingb129a8c2005-08-31 10:12:14 +01001143 pl011_stop_tx(&uap->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144}
1145
1146static void pl011_modem_status(struct uart_amba_port *uap)
1147{
1148 unsigned int status, delta;
1149
1150 status = readw(uap->port.membase + UART01x_FR) & UART01x_FR_MODEM_ANY;
1151
1152 delta = status ^ uap->old_status;
1153 uap->old_status = status;
1154
1155 if (!delta)
1156 return;
1157
1158 if (delta & UART01x_FR_DCD)
1159 uart_handle_dcd_change(&uap->port, status & UART01x_FR_DCD);
1160
1161 if (delta & UART01x_FR_DSR)
1162 uap->port.icount.dsr++;
1163
1164 if (delta & UART01x_FR_CTS)
1165 uart_handle_cts_change(&uap->port, status & UART01x_FR_CTS);
1166
Alan Coxbdc04e32009-09-19 13:13:31 -07001167 wake_up_interruptible(&uap->port.state->port.delta_msr_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168}
1169
David Howells7d12e782006-10-05 14:55:46 +01001170static irqreturn_t pl011_int(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171{
1172 struct uart_amba_port *uap = dev_id;
Russell King963cc982010-12-22 17:16:09 +00001173 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 unsigned int status, pass_counter = AMBA_ISR_PASS_LIMIT;
1175 int handled = 0;
Rajanikanth H.V4fd06902012-03-26 11:17:02 +02001176 unsigned int dummy_read;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177
Russell King963cc982010-12-22 17:16:09 +00001178 spin_lock_irqsave(&uap->port.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179
1180 status = readw(uap->port.membase + UART011_MIS);
1181 if (status) {
1182 do {
Rajanikanth H.V4fd06902012-03-26 11:17:02 +02001183 if (uap->vendor->cts_event_workaround) {
1184 /* workaround to make sure that all bits are unlocked.. */
1185 writew(0x00, uap->port.membase + UART011_ICR);
1186
1187 /*
1188 * WA: introduce 26ns(1 uart clk) delay before W1C;
1189 * single apb access will incur 2 pclk(133.12Mhz) delay,
1190 * so add 2 dummy reads
1191 */
1192 dummy_read = readw(uap->port.membase + UART011_ICR);
1193 dummy_read = readw(uap->port.membase + UART011_ICR);
1194 }
1195
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 writew(status & ~(UART011_TXIS|UART011_RTIS|
1197 UART011_RXIS),
1198 uap->port.membase + UART011_ICR);
1199
Linus Walleijead76f32011-02-24 13:21:08 +01001200 if (status & (UART011_RTIS|UART011_RXIS)) {
1201 if (pl011_dma_rx_running(uap))
1202 pl011_dma_rx_irq(uap);
1203 else
1204 pl011_rx_chars(uap);
1205 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 if (status & (UART011_DSRMIS|UART011_DCDMIS|
1207 UART011_CTSMIS|UART011_RIMIS))
1208 pl011_modem_status(uap);
1209 if (status & UART011_TXIS)
1210 pl011_tx_chars(uap);
1211
Rajanikanth H.V4fd06902012-03-26 11:17:02 +02001212 if (pass_counter-- == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 break;
1214
1215 status = readw(uap->port.membase + UART011_MIS);
1216 } while (status != 0);
1217 handled = 1;
1218 }
1219
Russell King963cc982010-12-22 17:16:09 +00001220 spin_unlock_irqrestore(&uap->port.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221
1222 return IRQ_RETVAL(handled);
1223}
1224
Linus Walleije643f872012-06-17 15:44:19 +02001225static unsigned int pl011_tx_empty(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226{
1227 struct uart_amba_port *uap = (struct uart_amba_port *)port;
1228 unsigned int status = readw(uap->port.membase + UART01x_FR);
1229 return status & (UART01x_FR_BUSY|UART01x_FR_TXFF) ? 0 : TIOCSER_TEMT;
1230}
1231
Linus Walleije643f872012-06-17 15:44:19 +02001232static unsigned int pl011_get_mctrl(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233{
1234 struct uart_amba_port *uap = (struct uart_amba_port *)port;
1235 unsigned int result = 0;
1236 unsigned int status = readw(uap->port.membase + UART01x_FR);
1237
Jiri Slaby5159f402007-10-18 23:40:31 -07001238#define TIOCMBIT(uartbit, tiocmbit) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 if (status & uartbit) \
1240 result |= tiocmbit
1241
Jiri Slaby5159f402007-10-18 23:40:31 -07001242 TIOCMBIT(UART01x_FR_DCD, TIOCM_CAR);
1243 TIOCMBIT(UART01x_FR_DSR, TIOCM_DSR);
1244 TIOCMBIT(UART01x_FR_CTS, TIOCM_CTS);
1245 TIOCMBIT(UART011_FR_RI, TIOCM_RNG);
1246#undef TIOCMBIT
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 return result;
1248}
1249
1250static void pl011_set_mctrl(struct uart_port *port, unsigned int mctrl)
1251{
1252 struct uart_amba_port *uap = (struct uart_amba_port *)port;
1253 unsigned int cr;
1254
1255 cr = readw(uap->port.membase + UART011_CR);
1256
Jiri Slaby5159f402007-10-18 23:40:31 -07001257#define TIOCMBIT(tiocmbit, uartbit) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 if (mctrl & tiocmbit) \
1259 cr |= uartbit; \
1260 else \
1261 cr &= ~uartbit
1262
Jiri Slaby5159f402007-10-18 23:40:31 -07001263 TIOCMBIT(TIOCM_RTS, UART011_CR_RTS);
1264 TIOCMBIT(TIOCM_DTR, UART011_CR_DTR);
1265 TIOCMBIT(TIOCM_OUT1, UART011_CR_OUT1);
1266 TIOCMBIT(TIOCM_OUT2, UART011_CR_OUT2);
1267 TIOCMBIT(TIOCM_LOOP, UART011_CR_LBE);
Rabin Vincent3b438162010-02-12 06:43:11 +01001268
1269 if (uap->autorts) {
1270 /* We need to disable auto-RTS if we want to turn RTS off */
1271 TIOCMBIT(TIOCM_RTS, UART011_CR_RTSEN);
1272 }
Jiri Slaby5159f402007-10-18 23:40:31 -07001273#undef TIOCMBIT
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274
1275 writew(cr, uap->port.membase + UART011_CR);
1276}
1277
1278static void pl011_break_ctl(struct uart_port *port, int break_state)
1279{
1280 struct uart_amba_port *uap = (struct uart_amba_port *)port;
1281 unsigned long flags;
1282 unsigned int lcr_h;
1283
1284 spin_lock_irqsave(&uap->port.lock, flags);
Linus Walleijec489aa2010-06-02 08:13:52 +01001285 lcr_h = readw(uap->port.membase + uap->lcrh_tx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 if (break_state == -1)
1287 lcr_h |= UART01x_LCRH_BRK;
1288 else
1289 lcr_h &= ~UART01x_LCRH_BRK;
Linus Walleijec489aa2010-06-02 08:13:52 +01001290 writew(lcr_h, uap->port.membase + uap->lcrh_tx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 spin_unlock_irqrestore(&uap->port.lock, flags);
1292}
1293
Jason Wessel84b5ae12008-02-20 13:33:39 -06001294#ifdef CONFIG_CONSOLE_POLL
Anton Vorontsov5c8124a2012-09-24 14:27:55 -07001295
1296static void pl011_quiesce_irqs(struct uart_port *port)
1297{
1298 struct uart_amba_port *uap = (struct uart_amba_port *)port;
1299 unsigned char __iomem *regs = uap->port.membase;
1300
1301 writew(readw(regs + UART011_MIS), regs + UART011_ICR);
1302 /*
1303 * There is no way to clear TXIM as this is "ready to transmit IRQ", so
1304 * we simply mask it. start_tx() will unmask it.
1305 *
1306 * Note we can race with start_tx(), and if the race happens, the
1307 * polling user might get another interrupt just after we clear it.
1308 * But it should be OK and can happen even w/o the race, e.g.
1309 * controller immediately got some new data and raised the IRQ.
1310 *
1311 * And whoever uses polling routines assumes that it manages the device
1312 * (including tx queue), so we're also fine with start_tx()'s caller
1313 * side.
1314 */
1315 writew(readw(regs + UART011_IMSC) & ~UART011_TXIM, regs + UART011_IMSC);
1316}
1317
Linus Walleije643f872012-06-17 15:44:19 +02001318static int pl011_get_poll_char(struct uart_port *port)
Jason Wessel84b5ae12008-02-20 13:33:39 -06001319{
1320 struct uart_amba_port *uap = (struct uart_amba_port *)port;
1321 unsigned int status;
1322
Anton Vorontsov5c8124a2012-09-24 14:27:55 -07001323 /*
1324 * The caller might need IRQs lowered, e.g. if used with KDB NMI
1325 * debugger.
1326 */
1327 pl011_quiesce_irqs(port);
1328
Jason Wesself5316b42010-05-20 21:04:22 -05001329 status = readw(uap->port.membase + UART01x_FR);
1330 if (status & UART01x_FR_RXFE)
1331 return NO_POLL_CHAR;
Jason Wessel84b5ae12008-02-20 13:33:39 -06001332
1333 return readw(uap->port.membase + UART01x_DR);
1334}
1335
Linus Walleije643f872012-06-17 15:44:19 +02001336static void pl011_put_poll_char(struct uart_port *port,
Jason Wessel84b5ae12008-02-20 13:33:39 -06001337 unsigned char ch)
1338{
1339 struct uart_amba_port *uap = (struct uart_amba_port *)port;
1340
1341 while (readw(uap->port.membase + UART01x_FR) & UART01x_FR_TXFF)
1342 barrier();
1343
1344 writew(ch, uap->port.membase + UART01x_DR);
1345}
1346
1347#endif /* CONFIG_CONSOLE_POLL */
1348
Anton Vorontsovb3564c22012-09-24 14:27:54 -07001349static int pl011_hwinit(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350{
1351 struct uart_amba_port *uap = (struct uart_amba_port *)port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 int retval;
1353
Linus Walleij78d80c52012-05-23 21:18:46 +02001354 /* Optionaly enable pins to be muxed in and configured */
1355 if (!IS_ERR(uap->pins_default)) {
1356 retval = pinctrl_select_state(uap->pinctrl, uap->pins_default);
1357 if (retval)
1358 dev_err(port->dev,
1359 "could not set default pins\n");
1360 }
1361
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 /*
1363 * Try to enable the clock producer.
1364 */
Julia Lawall1c4c4392012-08-26 18:01:01 +02001365 retval = clk_prepare_enable(uap->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 if (retval)
Julia Lawall1c4c4392012-08-26 18:01:01 +02001367 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368
1369 uap->port.uartclk = clk_get_rate(uap->clk);
1370
Linus Walleij9b96fba2012-03-13 13:27:23 +01001371 /* Clear pending error and receive interrupts */
1372 writew(UART011_OEIS | UART011_BEIS | UART011_PEIS | UART011_FEIS |
1373 UART011_RTIS | UART011_RXIS, uap->port.membase + UART011_ICR);
1374
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 /*
Anton Vorontsovb3564c22012-09-24 14:27:54 -07001376 * Save interrupts enable mask, and enable RX interrupts in case if
1377 * the interrupt is used for NMI entry.
1378 */
1379 uap->im = readw(uap->port.membase + UART011_IMSC);
1380 writew(UART011_RTIM | UART011_RXIM, uap->port.membase + UART011_IMSC);
1381
1382 if (uap->port.dev->platform_data) {
1383 struct amba_pl011_data *plat;
1384
1385 plat = uap->port.dev->platform_data;
1386 if (plat->init)
1387 plat->init();
1388 }
1389 return 0;
1390 out:
1391 return retval;
1392}
1393
1394static int pl011_startup(struct uart_port *port)
1395{
1396 struct uart_amba_port *uap = (struct uart_amba_port *)port;
1397 unsigned int cr;
1398 int retval;
1399
1400 retval = pl011_hwinit(port);
1401 if (retval)
1402 goto clk_dis;
1403
1404 writew(uap->im, uap->port.membase + UART011_IMSC);
1405
1406 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 * Allocate the IRQ
1408 */
1409 retval = request_irq(uap->port.irq, pl011_int, 0, "uart-pl011", uap);
1410 if (retval)
1411 goto clk_dis;
1412
Russell Kingc19f12b2010-12-22 17:48:26 +00001413 writew(uap->vendor->ifls, uap->port.membase + UART011_IFLS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414
1415 /*
1416 * Provoke TX FIFO interrupt into asserting.
1417 */
1418 cr = UART01x_CR_UARTEN | UART011_CR_TXE | UART011_CR_LBE;
1419 writew(cr, uap->port.membase + UART011_CR);
1420 writew(0, uap->port.membase + UART011_FBRD);
1421 writew(1, uap->port.membase + UART011_IBRD);
Linus Walleijec489aa2010-06-02 08:13:52 +01001422 writew(0, uap->port.membase + uap->lcrh_rx);
1423 if (uap->lcrh_tx != uap->lcrh_rx) {
1424 int i;
1425 /*
1426 * Wait 10 PCLKs before writing LCRH_TX register,
1427 * to get this delay write read only register 10 times
1428 */
1429 for (i = 0; i < 10; ++i)
1430 writew(0xff, uap->port.membase + UART011_MIS);
1431 writew(0, uap->port.membase + uap->lcrh_tx);
1432 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 writew(0, uap->port.membase + UART01x_DR);
1434 while (readw(uap->port.membase + UART01x_FR) & UART01x_FR_BUSY)
1435 barrier();
1436
Shreshtha Kumar Sahud8d8ffa2012-01-18 15:53:59 +05301437 /* restore RTS and DTR */
1438 cr = uap->old_cr & (UART011_CR_RTS | UART011_CR_DTR);
1439 cr |= UART01x_CR_UARTEN | UART011_CR_RXE | UART011_CR_TXE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 writew(cr, uap->port.membase + UART011_CR);
1441
1442 /*
1443 * initialise the old status of the modem signals
1444 */
1445 uap->old_status = readw(uap->port.membase + UART01x_FR) & UART01x_FR_MODEM_ANY;
1446
Russell King68b65f72010-12-22 17:24:39 +00001447 /* Startup DMA */
1448 pl011_dma_startup(uap);
1449
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 /*
Linus Walleijead76f32011-02-24 13:21:08 +01001451 * Finally, enable interrupts, only timeouts when using DMA
1452 * if initial RX DMA job failed, start in interrupt mode
1453 * as well.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 */
1455 spin_lock_irq(&uap->port.lock);
Linus Walleij9b96fba2012-03-13 13:27:23 +01001456 /* Clear out any spuriously appearing RX interrupts */
1457 writew(UART011_RTIS | UART011_RXIS,
1458 uap->port.membase + UART011_ICR);
Linus Walleijead76f32011-02-24 13:21:08 +01001459 uap->im = UART011_RTIM;
1460 if (!pl011_dma_rx_running(uap))
1461 uap->im |= UART011_RXIM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 writew(uap->im, uap->port.membase + UART011_IMSC);
1463 spin_unlock_irq(&uap->port.lock);
1464
1465 return 0;
1466
1467 clk_dis:
Julia Lawall1c4c4392012-08-26 18:01:01 +02001468 clk_disable_unprepare(uap->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 return retval;
1470}
1471
Linus Walleijec489aa2010-06-02 08:13:52 +01001472static void pl011_shutdown_channel(struct uart_amba_port *uap,
1473 unsigned int lcrh)
1474{
1475 unsigned long val;
1476
1477 val = readw(uap->port.membase + lcrh);
1478 val &= ~(UART01x_LCRH_BRK | UART01x_LCRH_FEN);
1479 writew(val, uap->port.membase + lcrh);
1480}
1481
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482static void pl011_shutdown(struct uart_port *port)
1483{
1484 struct uart_amba_port *uap = (struct uart_amba_port *)port;
Shreshtha Kumar Sahud8d8ffa2012-01-18 15:53:59 +05301485 unsigned int cr;
Linus Walleij78d80c52012-05-23 21:18:46 +02001486 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487
1488 /*
1489 * disable all interrupts
1490 */
1491 spin_lock_irq(&uap->port.lock);
1492 uap->im = 0;
1493 writew(uap->im, uap->port.membase + UART011_IMSC);
1494 writew(0xffff, uap->port.membase + UART011_ICR);
1495 spin_unlock_irq(&uap->port.lock);
1496
Russell King68b65f72010-12-22 17:24:39 +00001497 pl011_dma_shutdown(uap);
1498
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 /*
1500 * Free the interrupt
1501 */
1502 free_irq(uap->port.irq, uap);
1503
1504 /*
1505 * disable the port
Shreshtha Kumar Sahud8d8ffa2012-01-18 15:53:59 +05301506 * disable the port. It should not disable RTS and DTR.
1507 * Also RTS and DTR state should be preserved to restore
1508 * it during startup().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 */
Rabin Vincent3b438162010-02-12 06:43:11 +01001510 uap->autorts = false;
Shreshtha Kumar Sahud8d8ffa2012-01-18 15:53:59 +05301511 cr = readw(uap->port.membase + UART011_CR);
1512 uap->old_cr = cr;
1513 cr &= UART011_CR_RTS | UART011_CR_DTR;
1514 cr |= UART01x_CR_UARTEN | UART011_CR_TXE;
1515 writew(cr, uap->port.membase + UART011_CR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516
1517 /*
1518 * disable break condition and fifos
1519 */
Linus Walleijec489aa2010-06-02 08:13:52 +01001520 pl011_shutdown_channel(uap, uap->lcrh_rx);
1521 if (uap->lcrh_rx != uap->lcrh_tx)
1522 pl011_shutdown_channel(uap, uap->lcrh_tx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523
1524 /*
1525 * Shut down the clock producer
1526 */
Julia Lawall1c4c4392012-08-26 18:01:01 +02001527 clk_disable_unprepare(uap->clk);
Linus Walleij78d80c52012-05-23 21:18:46 +02001528 /* Optionally let pins go into sleep states */
1529 if (!IS_ERR(uap->pins_sleep)) {
1530 retval = pinctrl_select_state(uap->pinctrl, uap->pins_sleep);
1531 if (retval)
1532 dev_err(port->dev,
1533 "could not set pins to sleep state\n");
1534 }
1535
Shreshtha Kumar Sahuc16d51a2011-06-13 10:11:33 +02001536
1537 if (uap->port.dev->platform_data) {
1538 struct amba_pl011_data *plat;
1539
1540 plat = uap->port.dev->platform_data;
1541 if (plat->exit)
1542 plat->exit();
1543 }
1544
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545}
1546
1547static void
Alan Cox606d0992006-12-08 02:38:45 -08001548pl011_set_termios(struct uart_port *port, struct ktermios *termios,
1549 struct ktermios *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550{
Rabin Vincent3b438162010-02-12 06:43:11 +01001551 struct uart_amba_port *uap = (struct uart_amba_port *)port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 unsigned int lcr_h, old_cr;
1553 unsigned long flags;
Russell Kingc19f12b2010-12-22 17:48:26 +00001554 unsigned int baud, quot, clkdiv;
1555
1556 if (uap->vendor->oversampling)
1557 clkdiv = 8;
1558 else
1559 clkdiv = 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560
1561 /*
1562 * Ask the core to calculate the divisor for us.
1563 */
Linus Walleijac3e3fb2010-06-02 20:40:22 +01001564 baud = uart_get_baud_rate(port, termios, old, 0,
Russell Kingc19f12b2010-12-22 17:48:26 +00001565 port->uartclk / clkdiv);
Linus Walleijac3e3fb2010-06-02 20:40:22 +01001566
1567 if (baud > port->uartclk/16)
1568 quot = DIV_ROUND_CLOSEST(port->uartclk * 8, baud);
1569 else
1570 quot = DIV_ROUND_CLOSEST(port->uartclk * 4, baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571
1572 switch (termios->c_cflag & CSIZE) {
1573 case CS5:
1574 lcr_h = UART01x_LCRH_WLEN_5;
1575 break;
1576 case CS6:
1577 lcr_h = UART01x_LCRH_WLEN_6;
1578 break;
1579 case CS7:
1580 lcr_h = UART01x_LCRH_WLEN_7;
1581 break;
1582 default: // CS8
1583 lcr_h = UART01x_LCRH_WLEN_8;
1584 break;
1585 }
1586 if (termios->c_cflag & CSTOPB)
1587 lcr_h |= UART01x_LCRH_STP2;
1588 if (termios->c_cflag & PARENB) {
1589 lcr_h |= UART01x_LCRH_PEN;
1590 if (!(termios->c_cflag & PARODD))
1591 lcr_h |= UART01x_LCRH_EPS;
1592 }
Russell Kingffca2b12010-12-22 17:13:05 +00001593 if (uap->fifosize > 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 lcr_h |= UART01x_LCRH_FEN;
1595
1596 spin_lock_irqsave(&port->lock, flags);
1597
1598 /*
1599 * Update the per-port timeout.
1600 */
1601 uart_update_timeout(port, termios->c_cflag, baud);
1602
Russell Kingb63d4f02005-11-19 11:10:35 +00001603 port->read_status_mask = UART011_DR_OE | 255;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 if (termios->c_iflag & INPCK)
Russell Kingb63d4f02005-11-19 11:10:35 +00001605 port->read_status_mask |= UART011_DR_FE | UART011_DR_PE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 if (termios->c_iflag & (BRKINT | PARMRK))
Russell Kingb63d4f02005-11-19 11:10:35 +00001607 port->read_status_mask |= UART011_DR_BE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608
1609 /*
1610 * Characters to ignore
1611 */
1612 port->ignore_status_mask = 0;
1613 if (termios->c_iflag & IGNPAR)
Russell Kingb63d4f02005-11-19 11:10:35 +00001614 port->ignore_status_mask |= UART011_DR_FE | UART011_DR_PE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 if (termios->c_iflag & IGNBRK) {
Russell Kingb63d4f02005-11-19 11:10:35 +00001616 port->ignore_status_mask |= UART011_DR_BE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 /*
1618 * If we're ignoring parity and break indicators,
1619 * ignore overruns too (for real raw support).
1620 */
1621 if (termios->c_iflag & IGNPAR)
Russell Kingb63d4f02005-11-19 11:10:35 +00001622 port->ignore_status_mask |= UART011_DR_OE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 }
1624
1625 /*
1626 * Ignore all characters if CREAD is not set.
1627 */
1628 if ((termios->c_cflag & CREAD) == 0)
Russell Kingb63d4f02005-11-19 11:10:35 +00001629 port->ignore_status_mask |= UART_DUMMY_DR_RX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630
1631 if (UART_ENABLE_MS(port, termios->c_cflag))
1632 pl011_enable_ms(port);
1633
1634 /* first, disable everything */
1635 old_cr = readw(port->membase + UART011_CR);
1636 writew(0, port->membase + UART011_CR);
1637
Rabin Vincent3b438162010-02-12 06:43:11 +01001638 if (termios->c_cflag & CRTSCTS) {
1639 if (old_cr & UART011_CR_RTS)
1640 old_cr |= UART011_CR_RTSEN;
1641
1642 old_cr |= UART011_CR_CTSEN;
1643 uap->autorts = true;
1644 } else {
1645 old_cr &= ~(UART011_CR_CTSEN | UART011_CR_RTSEN);
1646 uap->autorts = false;
1647 }
1648
Russell Kingc19f12b2010-12-22 17:48:26 +00001649 if (uap->vendor->oversampling) {
1650 if (baud > port->uartclk / 16)
Linus Walleijac3e3fb2010-06-02 20:40:22 +01001651 old_cr |= ST_UART011_CR_OVSFACT;
1652 else
1653 old_cr &= ~ST_UART011_CR_OVSFACT;
1654 }
1655
Linus Walleijc5dd5532012-09-26 17:21:36 +02001656 /*
1657 * Workaround for the ST Micro oversampling variants to
1658 * increase the bitrate slightly, by lowering the divisor,
1659 * to avoid delayed sampling of start bit at high speeds,
1660 * else we see data corruption.
1661 */
1662 if (uap->vendor->oversampling) {
1663 if ((baud >= 3000000) && (baud < 3250000) && (quot > 1))
1664 quot -= 1;
1665 else if ((baud > 3250000) && (quot > 2))
1666 quot -= 2;
1667 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 /* Set baud rate */
1669 writew(quot & 0x3f, port->membase + UART011_FBRD);
1670 writew(quot >> 6, port->membase + UART011_IBRD);
1671
1672 /*
1673 * ----------v----------v----------v----------v-----
Linus Walleijc5dd5532012-09-26 17:21:36 +02001674 * NOTE: lcrh_tx and lcrh_rx MUST BE WRITTEN AFTER
1675 * UART011_FBRD & UART011_IBRD.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 * ----------^----------^----------^----------^-----
1677 */
Linus Walleijec489aa2010-06-02 08:13:52 +01001678 writew(lcr_h, port->membase + uap->lcrh_rx);
1679 if (uap->lcrh_rx != uap->lcrh_tx) {
1680 int i;
1681 /*
1682 * Wait 10 PCLKs before writing LCRH_TX register,
1683 * to get this delay write read only register 10 times
1684 */
1685 for (i = 0; i < 10; ++i)
1686 writew(0xff, uap->port.membase + UART011_MIS);
1687 writew(lcr_h, port->membase + uap->lcrh_tx);
1688 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 writew(old_cr, port->membase + UART011_CR);
1690
1691 spin_unlock_irqrestore(&port->lock, flags);
1692}
1693
1694static const char *pl011_type(struct uart_port *port)
1695{
Russell Kinge8a7ba82010-12-28 09:16:54 +00001696 struct uart_amba_port *uap = (struct uart_amba_port *)port;
1697 return uap->port.type == PORT_AMBA ? uap->type : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698}
1699
1700/*
1701 * Release the memory region(s) being used by 'port'
1702 */
Linus Walleije643f872012-06-17 15:44:19 +02001703static void pl011_release_port(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704{
1705 release_mem_region(port->mapbase, SZ_4K);
1706}
1707
1708/*
1709 * Request the memory region(s) being used by 'port'
1710 */
Linus Walleije643f872012-06-17 15:44:19 +02001711static int pl011_request_port(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712{
1713 return request_mem_region(port->mapbase, SZ_4K, "uart-pl011")
1714 != NULL ? 0 : -EBUSY;
1715}
1716
1717/*
1718 * Configure/autoconfigure the port.
1719 */
Linus Walleije643f872012-06-17 15:44:19 +02001720static void pl011_config_port(struct uart_port *port, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721{
1722 if (flags & UART_CONFIG_TYPE) {
1723 port->type = PORT_AMBA;
Linus Walleije643f872012-06-17 15:44:19 +02001724 pl011_request_port(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 }
1726}
1727
1728/*
1729 * verify the new serial_struct (for TIOCSSERIAL).
1730 */
Linus Walleije643f872012-06-17 15:44:19 +02001731static int pl011_verify_port(struct uart_port *port, struct serial_struct *ser)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732{
1733 int ret = 0;
1734 if (ser->type != PORT_UNKNOWN && ser->type != PORT_AMBA)
1735 ret = -EINVAL;
Yinghai Lua62c4132008-08-19 20:49:55 -07001736 if (ser->irq < 0 || ser->irq >= nr_irqs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 ret = -EINVAL;
1738 if (ser->baud_base < 9600)
1739 ret = -EINVAL;
1740 return ret;
1741}
1742
1743static struct uart_ops amba_pl011_pops = {
Linus Walleije643f872012-06-17 15:44:19 +02001744 .tx_empty = pl011_tx_empty,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 .set_mctrl = pl011_set_mctrl,
Linus Walleije643f872012-06-17 15:44:19 +02001746 .get_mctrl = pl011_get_mctrl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 .stop_tx = pl011_stop_tx,
1748 .start_tx = pl011_start_tx,
1749 .stop_rx = pl011_stop_rx,
1750 .enable_ms = pl011_enable_ms,
1751 .break_ctl = pl011_break_ctl,
1752 .startup = pl011_startup,
1753 .shutdown = pl011_shutdown,
Russell King68b65f72010-12-22 17:24:39 +00001754 .flush_buffer = pl011_dma_flush_buffer,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 .set_termios = pl011_set_termios,
1756 .type = pl011_type,
Linus Walleije643f872012-06-17 15:44:19 +02001757 .release_port = pl011_release_port,
1758 .request_port = pl011_request_port,
1759 .config_port = pl011_config_port,
1760 .verify_port = pl011_verify_port,
Jason Wessel84b5ae12008-02-20 13:33:39 -06001761#ifdef CONFIG_CONSOLE_POLL
Anton Vorontsovb3564c22012-09-24 14:27:54 -07001762 .poll_init = pl011_hwinit,
Linus Walleije643f872012-06-17 15:44:19 +02001763 .poll_get_char = pl011_get_poll_char,
1764 .poll_put_char = pl011_put_poll_char,
Jason Wessel84b5ae12008-02-20 13:33:39 -06001765#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766};
1767
1768static struct uart_amba_port *amba_ports[UART_NR];
1769
1770#ifdef CONFIG_SERIAL_AMBA_PL011_CONSOLE
1771
Russell Kingd3587882006-03-20 20:00:09 +00001772static void pl011_console_putchar(struct uart_port *port, int ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773{
Russell Kingd3587882006-03-20 20:00:09 +00001774 struct uart_amba_port *uap = (struct uart_amba_port *)port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775
Russell Kingd3587882006-03-20 20:00:09 +00001776 while (readw(uap->port.membase + UART01x_FR) & UART01x_FR_TXFF)
1777 barrier();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 writew(ch, uap->port.membase + UART01x_DR);
1779}
1780
1781static void
1782pl011_console_write(struct console *co, const char *s, unsigned int count)
1783{
1784 struct uart_amba_port *uap = amba_ports[co->index];
1785 unsigned int status, old_cr, new_cr;
Rabin Vincentef605fd2012-01-17 11:52:28 +01001786 unsigned long flags;
1787 int locked = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788
1789 clk_enable(uap->clk);
1790
Rabin Vincentef605fd2012-01-17 11:52:28 +01001791 local_irq_save(flags);
1792 if (uap->port.sysrq)
1793 locked = 0;
1794 else if (oops_in_progress)
1795 locked = spin_trylock(&uap->port.lock);
1796 else
1797 spin_lock(&uap->port.lock);
1798
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 /*
1800 * First save the CR then disable the interrupts
1801 */
1802 old_cr = readw(uap->port.membase + UART011_CR);
1803 new_cr = old_cr & ~UART011_CR_CTSEN;
1804 new_cr |= UART01x_CR_UARTEN | UART011_CR_TXE;
1805 writew(new_cr, uap->port.membase + UART011_CR);
1806
Russell Kingd3587882006-03-20 20:00:09 +00001807 uart_console_write(&uap->port, s, count, pl011_console_putchar);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808
1809 /*
1810 * Finally, wait for transmitter to become empty
1811 * and restore the TCR
1812 */
1813 do {
1814 status = readw(uap->port.membase + UART01x_FR);
1815 } while (status & UART01x_FR_BUSY);
1816 writew(old_cr, uap->port.membase + UART011_CR);
1817
Rabin Vincentef605fd2012-01-17 11:52:28 +01001818 if (locked)
1819 spin_unlock(&uap->port.lock);
1820 local_irq_restore(flags);
1821
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 clk_disable(uap->clk);
1823}
1824
1825static void __init
1826pl011_console_get_options(struct uart_amba_port *uap, int *baud,
1827 int *parity, int *bits)
1828{
1829 if (readw(uap->port.membase + UART011_CR) & UART01x_CR_UARTEN) {
1830 unsigned int lcr_h, ibrd, fbrd;
1831
Linus Walleijec489aa2010-06-02 08:13:52 +01001832 lcr_h = readw(uap->port.membase + uap->lcrh_tx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833
1834 *parity = 'n';
1835 if (lcr_h & UART01x_LCRH_PEN) {
1836 if (lcr_h & UART01x_LCRH_EPS)
1837 *parity = 'e';
1838 else
1839 *parity = 'o';
1840 }
1841
1842 if ((lcr_h & 0x60) == UART01x_LCRH_WLEN_7)
1843 *bits = 7;
1844 else
1845 *bits = 8;
1846
1847 ibrd = readw(uap->port.membase + UART011_IBRD);
1848 fbrd = readw(uap->port.membase + UART011_FBRD);
1849
1850 *baud = uap->port.uartclk * 4 / (64 * ibrd + fbrd);
Linus Walleijac3e3fb2010-06-02 20:40:22 +01001851
Russell Kingc19f12b2010-12-22 17:48:26 +00001852 if (uap->vendor->oversampling) {
Linus Walleijac3e3fb2010-06-02 20:40:22 +01001853 if (readw(uap->port.membase + UART011_CR)
1854 & ST_UART011_CR_OVSFACT)
1855 *baud *= 2;
1856 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 }
1858}
1859
1860static int __init pl011_console_setup(struct console *co, char *options)
1861{
1862 struct uart_amba_port *uap;
1863 int baud = 38400;
1864 int bits = 8;
1865 int parity = 'n';
1866 int flow = 'n';
Russell King4b4851c2011-09-22 11:35:30 +01001867 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868
1869 /*
1870 * Check whether an invalid uart number has been specified, and
1871 * if so, search for the first available port that does have
1872 * console support.
1873 */
1874 if (co->index >= UART_NR)
1875 co->index = 0;
1876 uap = amba_ports[co->index];
Russell Kingd28122a2007-01-22 18:59:42 +00001877 if (!uap)
1878 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879
Linus Walleij78d80c52012-05-23 21:18:46 +02001880 /* Allow pins to be muxed in and configured */
1881 if (!IS_ERR(uap->pins_default)) {
1882 ret = pinctrl_select_state(uap->pinctrl, uap->pins_default);
1883 if (ret)
1884 dev_err(uap->port.dev,
1885 "could not set default pins\n");
1886 }
1887
Russell King4b4851c2011-09-22 11:35:30 +01001888 ret = clk_prepare(uap->clk);
1889 if (ret)
1890 return ret;
1891
Shreshtha Kumar Sahuc16d51a2011-06-13 10:11:33 +02001892 if (uap->port.dev->platform_data) {
1893 struct amba_pl011_data *plat;
1894
1895 plat = uap->port.dev->platform_data;
1896 if (plat->init)
1897 plat->init();
1898 }
1899
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 uap->port.uartclk = clk_get_rate(uap->clk);
1901
1902 if (options)
1903 uart_parse_options(options, &baud, &parity, &bits, &flow);
1904 else
1905 pl011_console_get_options(uap, &baud, &parity, &bits);
1906
1907 return uart_set_options(&uap->port, co, baud, parity, bits, flow);
1908}
1909
Vincent Sanders2d934862005-09-14 22:36:03 +01001910static struct uart_driver amba_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911static struct console amba_console = {
1912 .name = "ttyAMA",
1913 .write = pl011_console_write,
1914 .device = uart_console_device,
1915 .setup = pl011_console_setup,
1916 .flags = CON_PRINTBUFFER,
1917 .index = -1,
1918 .data = &amba_reg,
1919};
1920
1921#define AMBA_CONSOLE (&amba_console)
1922#else
1923#define AMBA_CONSOLE NULL
1924#endif
1925
1926static struct uart_driver amba_reg = {
1927 .owner = THIS_MODULE,
1928 .driver_name = "ttyAMA",
1929 .dev_name = "ttyAMA",
1930 .major = SERIAL_AMBA_MAJOR,
1931 .minor = SERIAL_AMBA_MINOR,
1932 .nr = UART_NR,
1933 .cons = AMBA_CONSOLE,
1934};
1935
Matthew Leach32614aa2012-08-28 16:41:28 +01001936static int pl011_probe_dt_alias(int index, struct device *dev)
1937{
1938 struct device_node *np;
1939 static bool seen_dev_with_alias = false;
1940 static bool seen_dev_without_alias = false;
1941 int ret = index;
1942
1943 if (!IS_ENABLED(CONFIG_OF))
1944 return ret;
1945
1946 np = dev->of_node;
1947 if (!np)
1948 return ret;
1949
1950 ret = of_alias_get_id(np, "serial");
1951 if (IS_ERR_VALUE(ret)) {
1952 seen_dev_without_alias = true;
1953 ret = index;
1954 } else {
1955 seen_dev_with_alias = true;
1956 if (ret >= ARRAY_SIZE(amba_ports) || amba_ports[ret] != NULL) {
1957 dev_warn(dev, "requested serial port %d not available.\n", ret);
1958 ret = index;
1959 }
1960 }
1961
1962 if (seen_dev_with_alias && seen_dev_without_alias)
1963 dev_warn(dev, "aliased and non-aliased serial devices found in device tree. Serial port enumeration may be unpredictable.\n");
1964
1965 return ret;
1966}
1967
Russell Kingaa25afa2011-02-19 15:55:00 +00001968static int pl011_probe(struct amba_device *dev, const struct amba_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969{
1970 struct uart_amba_port *uap;
Alessandro Rubini5926a292009-06-04 17:43:04 +01001971 struct vendor_data *vendor = id->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 void __iomem *base;
1973 int i, ret;
1974
1975 for (i = 0; i < ARRAY_SIZE(amba_ports); i++)
1976 if (amba_ports[i] == NULL)
1977 break;
1978
1979 if (i == ARRAY_SIZE(amba_ports)) {
1980 ret = -EBUSY;
1981 goto out;
1982 }
1983
Linus Walleijde609582012-10-15 13:36:01 +02001984 uap = devm_kzalloc(&dev->dev, sizeof(struct uart_amba_port),
1985 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 if (uap == NULL) {
1987 ret = -ENOMEM;
1988 goto out;
1989 }
1990
Matthew Leach32614aa2012-08-28 16:41:28 +01001991 i = pl011_probe_dt_alias(i, &dev->dev);
1992
Linus Walleijde609582012-10-15 13:36:01 +02001993 base = devm_ioremap(&dev->dev, dev->res.start,
1994 resource_size(&dev->res));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 if (!base) {
1996 ret = -ENOMEM;
Linus Walleijde609582012-10-15 13:36:01 +02001997 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 }
1999
Linus Walleij78d80c52012-05-23 21:18:46 +02002000 uap->pinctrl = devm_pinctrl_get(&dev->dev);
2001 if (IS_ERR(uap->pinctrl)) {
2002 ret = PTR_ERR(uap->pinctrl);
Linus Walleijde609582012-10-15 13:36:01 +02002003 goto out;
Shawn Guo258e0552012-05-06 22:53:35 +08002004 }
Linus Walleij78d80c52012-05-23 21:18:46 +02002005 uap->pins_default = pinctrl_lookup_state(uap->pinctrl,
2006 PINCTRL_STATE_DEFAULT);
2007 if (IS_ERR(uap->pins_default))
2008 dev_err(&dev->dev, "could not get default pinstate\n");
2009
2010 uap->pins_sleep = pinctrl_lookup_state(uap->pinctrl,
2011 PINCTRL_STATE_SLEEP);
2012 if (IS_ERR(uap->pins_sleep))
2013 dev_dbg(&dev->dev, "could not get sleep pinstate\n");
Shawn Guo258e0552012-05-06 22:53:35 +08002014
Linus Walleijde609582012-10-15 13:36:01 +02002015 uap->clk = devm_clk_get(&dev->dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 if (IS_ERR(uap->clk)) {
2017 ret = PTR_ERR(uap->clk);
Linus Walleijde609582012-10-15 13:36:01 +02002018 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 }
2020
Russell Kingc19f12b2010-12-22 17:48:26 +00002021 uap->vendor = vendor;
Linus Walleijec489aa2010-06-02 08:13:52 +01002022 uap->lcrh_rx = vendor->lcrh_rx;
2023 uap->lcrh_tx = vendor->lcrh_tx;
Shreshtha Kumar Sahud8d8ffa2012-01-18 15:53:59 +05302024 uap->old_cr = 0;
Russell Kingffca2b12010-12-22 17:13:05 +00002025 uap->fifosize = vendor->fifosize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 uap->port.dev = &dev->dev;
2027 uap->port.mapbase = dev->res.start;
2028 uap->port.membase = base;
2029 uap->port.iotype = UPIO_MEM;
2030 uap->port.irq = dev->irq[0];
Russell Kingffca2b12010-12-22 17:13:05 +00002031 uap->port.fifosize = uap->fifosize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 uap->port.ops = &amba_pl011_pops;
2033 uap->port.flags = UPF_BOOT_AUTOCONF;
2034 uap->port.line = i;
Arnd Bergmann787b0c12013-01-28 16:24:37 +00002035 pl011_dma_probe(&dev->dev, uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036
Linus Walleijc3d8b762012-03-21 20:15:18 +01002037 /* Ensure interrupts from this UART are masked and cleared */
2038 writew(0, uap->port.membase + UART011_IMSC);
2039 writew(0xffff, uap->port.membase + UART011_ICR);
2040
Russell Kinge8a7ba82010-12-28 09:16:54 +00002041 snprintf(uap->type, sizeof(uap->type), "PL011 rev%u", amba_rev(dev));
2042
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 amba_ports[i] = uap;
2044
2045 amba_set_drvdata(dev, uap);
2046 ret = uart_add_one_port(&amba_reg, &uap->port);
2047 if (ret) {
2048 amba_set_drvdata(dev, NULL);
2049 amba_ports[i] = NULL;
Russell King68b65f72010-12-22 17:24:39 +00002050 pl011_dma_remove(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 }
2052 out:
2053 return ret;
2054}
2055
2056static int pl011_remove(struct amba_device *dev)
2057{
2058 struct uart_amba_port *uap = amba_get_drvdata(dev);
2059 int i;
2060
2061 amba_set_drvdata(dev, NULL);
2062
2063 uart_remove_one_port(&amba_reg, &uap->port);
2064
2065 for (i = 0; i < ARRAY_SIZE(amba_ports); i++)
2066 if (amba_ports[i] == uap)
2067 amba_ports[i] = NULL;
2068
Russell King68b65f72010-12-22 17:24:39 +00002069 pl011_dma_remove(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 return 0;
2071}
2072
Leo Chenb736b892009-07-28 23:43:33 +01002073#ifdef CONFIG_PM
2074static int pl011_suspend(struct amba_device *dev, pm_message_t state)
2075{
2076 struct uart_amba_port *uap = amba_get_drvdata(dev);
2077
2078 if (!uap)
2079 return -EINVAL;
2080
2081 return uart_suspend_port(&amba_reg, &uap->port);
2082}
2083
2084static int pl011_resume(struct amba_device *dev)
2085{
2086 struct uart_amba_port *uap = amba_get_drvdata(dev);
2087
2088 if (!uap)
2089 return -EINVAL;
2090
2091 return uart_resume_port(&amba_reg, &uap->port);
2092}
2093#endif
2094
Russell King2c39c9e2010-07-27 08:50:16 +01002095static struct amba_id pl011_ids[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 {
2097 .id = 0x00041011,
2098 .mask = 0x000fffff,
Alessandro Rubini5926a292009-06-04 17:43:04 +01002099 .data = &vendor_arm,
2100 },
2101 {
2102 .id = 0x00380802,
2103 .mask = 0x00ffffff,
2104 .data = &vendor_st,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 },
2106 { 0, 0 },
2107};
2108
Dave Martin60f7a332011-10-05 15:15:22 +01002109MODULE_DEVICE_TABLE(amba, pl011_ids);
2110
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111static struct amba_driver pl011_driver = {
2112 .drv = {
2113 .name = "uart-pl011",
2114 },
2115 .id_table = pl011_ids,
2116 .probe = pl011_probe,
2117 .remove = pl011_remove,
Leo Chenb736b892009-07-28 23:43:33 +01002118#ifdef CONFIG_PM
2119 .suspend = pl011_suspend,
2120 .resume = pl011_resume,
2121#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122};
2123
2124static int __init pl011_init(void)
2125{
2126 int ret;
2127 printk(KERN_INFO "Serial: AMBA PL011 UART driver\n");
2128
2129 ret = uart_register_driver(&amba_reg);
2130 if (ret == 0) {
2131 ret = amba_driver_register(&pl011_driver);
2132 if (ret)
2133 uart_unregister_driver(&amba_reg);
2134 }
2135 return ret;
2136}
2137
2138static void __exit pl011_exit(void)
2139{
2140 amba_driver_unregister(&pl011_driver);
2141 uart_unregister_driver(&amba_reg);
2142}
2143
Alessandro Rubini4dd9e742009-05-05 05:54:13 +01002144/*
2145 * While this can be a module, if builtin it's most likely the console
2146 * So let's leave module_exit but move module_init to an earlier place
2147 */
2148arch_initcall(pl011_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149module_exit(pl011_exit);
2150
2151MODULE_AUTHOR("ARM Ltd/Deep Blue Solutions Ltd");
2152MODULE_DESCRIPTION("ARM AMBA serial port driver");
2153MODULE_LICENSE("GPL");