blob: b2e9e177a354dc16f1e6654a68c19855f9fdba0e [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
Chanho Mincb06ff12013-03-27 18:38:11 +090032
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#if defined(CONFIG_SERIAL_AMBA_PL011_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
34#define SUPPORT_SYSRQ
35#endif
36
37#include <linux/module.h>
38#include <linux/ioport.h>
39#include <linux/init.h>
40#include <linux/console.h>
41#include <linux/sysrq.h>
42#include <linux/device.h>
43#include <linux/tty.h>
44#include <linux/tty_flip.h>
45#include <linux/serial_core.h>
46#include <linux/serial.h>
Russell Kinga62c80e2006-01-07 13:52:45 +000047#include <linux/amba/bus.h>
48#include <linux/amba/serial.h>
Russell Kingf8ce2542006-01-07 16:15:52 +000049#include <linux/clk.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090050#include <linux/slab.h>
Russell King68b65f72010-12-22 17:24:39 +000051#include <linux/dmaengine.h>
52#include <linux/dma-mapping.h>
53#include <linux/scatterlist.h>
Shreshtha Kumar Sahuc16d51a2011-06-13 10:11:33 +020054#include <linux/delay.h>
Viresh Kumar258aea72012-02-01 16:12:19 +053055#include <linux/types.h>
Matthew Leach32614aa2012-08-28 16:41:28 +010056#include <linux/of.h>
57#include <linux/of_device.h>
Shawn Guo258e0552012-05-06 22:53:35 +080058#include <linux/pinctrl/consumer.h>
Alessandro Rubinicb707062012-06-24 12:46:37 +010059#include <linux/sizes.h>
Linus Walleijde609582012-10-15 13:36:01 +020060#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
62#define UART_NR 14
63
64#define SERIAL_AMBA_MAJOR 204
65#define SERIAL_AMBA_MINOR 64
66#define SERIAL_AMBA_NR UART_NR
67
68#define AMBA_ISR_PASS_LIMIT 256
69
Russell Kingb63d4f02005-11-19 11:10:35 +000070#define UART_DR_ERROR (UART011_DR_OE|UART011_DR_BE|UART011_DR_PE|UART011_DR_FE)
71#define UART_DUMMY_DR_RX (1 << 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Alessandro Rubini5926a292009-06-04 17:43:04 +010073/* There is by now at least one vendor with differing details, so handle it */
74struct vendor_data {
75 unsigned int ifls;
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;
Jongsung Kim78506f22013-04-15 14:45:25 +090081
82 unsigned int (*get_fifosize)(unsigned int periphid);
Alessandro Rubini5926a292009-06-04 17:43:04 +010083};
84
Jongsung Kim78506f22013-04-15 14:45:25 +090085static unsigned int get_fifosize_arm(unsigned int periphid)
86{
87 unsigned int rev = (periphid >> 20) & 0xf;
88 return rev < 3 ? 16 : 32;
89}
90
Alessandro Rubini5926a292009-06-04 17:43:04 +010091static struct vendor_data vendor_arm = {
92 .ifls = UART011_IFLS_RX4_8|UART011_IFLS_TX4_8,
Linus Walleijec489aa2010-06-02 08:13:52 +010093 .lcrh_tx = UART011_LCRH,
94 .lcrh_rx = UART011_LCRH,
Linus Walleijac3e3fb2010-06-02 20:40:22 +010095 .oversampling = false,
Russell King38d62432010-12-22 17:59:16 +000096 .dma_threshold = false,
Rajanikanth H.V4fd06902012-03-26 11:17:02 +020097 .cts_event_workaround = false,
Jongsung Kim78506f22013-04-15 14:45:25 +090098 .get_fifosize = get_fifosize_arm,
Alessandro Rubini5926a292009-06-04 17:43:04 +010099};
100
Jongsung Kim78506f22013-04-15 14:45:25 +0900101static unsigned int get_fifosize_st(unsigned int periphid)
102{
103 return 64;
104}
105
Alessandro Rubini5926a292009-06-04 17:43:04 +0100106static struct vendor_data vendor_st = {
107 .ifls = UART011_IFLS_RX_HALF|UART011_IFLS_TX_HALF,
Linus Walleijec489aa2010-06-02 08:13:52 +0100108 .lcrh_tx = ST_UART011_LCRH_TX,
109 .lcrh_rx = ST_UART011_LCRH_RX,
Linus Walleijac3e3fb2010-06-02 20:40:22 +0100110 .oversampling = true,
Russell King38d62432010-12-22 17:59:16 +0000111 .dma_threshold = true,
Rajanikanth H.V4fd06902012-03-26 11:17:02 +0200112 .cts_event_workaround = true,
Jongsung Kim78506f22013-04-15 14:45:25 +0900113 .get_fifosize = get_fifosize_st,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114};
115
Shreshtha Kumar Sahuc16d51a2011-06-13 10:11:33 +0200116static struct uart_amba_port *amba_ports[UART_NR];
117
Russell King68b65f72010-12-22 17:24:39 +0000118/* Deals with DMA transactions */
Linus Walleijead76f32011-02-24 13:21:08 +0100119
120struct pl011_sgbuf {
121 struct scatterlist sg;
122 char *buf;
123};
124
125struct pl011_dmarx_data {
126 struct dma_chan *chan;
127 struct completion complete;
128 bool use_buf_b;
129 struct pl011_sgbuf sgbuf_a;
130 struct pl011_sgbuf sgbuf_b;
131 dma_cookie_t cookie;
132 bool running;
Chanho Mincb06ff12013-03-27 18:38:11 +0900133 struct timer_list timer;
134 unsigned int last_residue;
135 unsigned long last_jiffies;
136 bool auto_poll_rate;
137 unsigned int poll_rate;
138 unsigned int poll_timeout;
Linus Walleijead76f32011-02-24 13:21:08 +0100139};
140
Russell King68b65f72010-12-22 17:24:39 +0000141struct pl011_dmatx_data {
142 struct dma_chan *chan;
143 struct scatterlist sg;
144 char *buf;
145 bool queued;
146};
147
Russell Kingc19f12b2010-12-22 17:48:26 +0000148/*
149 * We wrap our port structure around the generic uart_port.
150 */
151struct uart_amba_port {
152 struct uart_port port;
153 struct clk *clk;
Linus Walleij78d80c52012-05-23 21:18:46 +0200154 /* Two optional pin states - default & sleep */
155 struct pinctrl *pinctrl;
156 struct pinctrl_state *pins_default;
157 struct pinctrl_state *pins_sleep;
Russell Kingc19f12b2010-12-22 17:48:26 +0000158 const struct vendor_data *vendor;
Russell King68b65f72010-12-22 17:24:39 +0000159 unsigned int dmacr; /* dma control reg */
Russell Kingc19f12b2010-12-22 17:48:26 +0000160 unsigned int im; /* interrupt mask */
161 unsigned int old_status;
Russell Kingffca2b12010-12-22 17:13:05 +0000162 unsigned int fifosize; /* vendor-specific */
Russell Kingc19f12b2010-12-22 17:48:26 +0000163 unsigned int lcrh_tx; /* vendor-specific */
164 unsigned int lcrh_rx; /* vendor-specific */
Shreshtha Kumar Sahud8d8ffa2012-01-18 15:53:59 +0530165 unsigned int old_cr; /* state during shutdown */
Russell Kingc19f12b2010-12-22 17:48:26 +0000166 bool autorts;
167 char type[12];
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{
Chanho Mincb06ff12013-03-27 18:38:11 +0900245 dma_addr_t dma_addr;
246
247 sg->buf = dma_alloc_coherent(chan->device->dev,
248 PL011_DMA_BUFFER_SIZE, &dma_addr, GFP_KERNEL);
Linus Walleijead76f32011-02-24 13:21:08 +0100249 if (!sg->buf)
250 return -ENOMEM;
251
Chanho Mincb06ff12013-03-27 18:38:11 +0900252 sg_init_table(&sg->sg, 1);
253 sg_set_page(&sg->sg, phys_to_page(dma_addr),
254 PL011_DMA_BUFFER_SIZE, offset_in_page(dma_addr));
255 sg_dma_address(&sg->sg) = dma_addr;
Linus Walleijead76f32011-02-24 13:21:08 +0100256
Linus Walleijead76f32011-02-24 13:21:08 +0100257 return 0;
258}
259
260static void pl011_sgbuf_free(struct dma_chan *chan, struct pl011_sgbuf *sg,
261 enum dma_data_direction dir)
262{
263 if (sg->buf) {
Chanho Mincb06ff12013-03-27 18:38:11 +0900264 dma_free_coherent(chan->device->dev,
265 PL011_DMA_BUFFER_SIZE, sg->buf,
266 sg_dma_address(&sg->sg));
Linus Walleijead76f32011-02-24 13:21:08 +0100267 }
268}
269
Russell King68b65f72010-12-22 17:24:39 +0000270static void pl011_dma_probe_initcall(struct uart_amba_port *uap)
271{
272 /* DMA is the sole user of the platform data right now */
273 struct amba_pl011_data *plat = uap->port.dev->platform_data;
274 struct dma_slave_config tx_conf = {
275 .dst_addr = uap->port.mapbase + UART01x_DR,
276 .dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE,
Vinod Koula485df42011-10-14 10:47:38 +0530277 .direction = DMA_MEM_TO_DEV,
Russell King68b65f72010-12-22 17:24:39 +0000278 .dst_maxburst = uap->fifosize >> 1,
Viresh Kumar258aea72012-02-01 16:12:19 +0530279 .device_fc = false,
Russell King68b65f72010-12-22 17:24:39 +0000280 };
281 struct dma_chan *chan;
282 dma_cap_mask_t mask;
283
284 /* We need platform data */
285 if (!plat || !plat->dma_filter) {
286 dev_info(uap->port.dev, "no DMA platform data\n");
287 return;
288 }
289
Linus Walleijead76f32011-02-24 13:21:08 +0100290 /* Try to acquire a generic DMA engine slave TX channel */
Russell King68b65f72010-12-22 17:24:39 +0000291 dma_cap_zero(mask);
292 dma_cap_set(DMA_SLAVE, mask);
293
294 chan = dma_request_channel(mask, plat->dma_filter, plat->dma_tx_param);
295 if (!chan) {
296 dev_err(uap->port.dev, "no TX DMA channel!\n");
297 return;
298 }
299
300 dmaengine_slave_config(chan, &tx_conf);
301 uap->dmatx.chan = chan;
302
303 dev_info(uap->port.dev, "DMA channel TX %s\n",
304 dma_chan_name(uap->dmatx.chan));
Linus Walleijead76f32011-02-24 13:21:08 +0100305
306 /* Optionally make use of an RX channel as well */
307 if (plat->dma_rx_param) {
308 struct dma_slave_config rx_conf = {
309 .src_addr = uap->port.mapbase + UART01x_DR,
310 .src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE,
Vinod Koula485df42011-10-14 10:47:38 +0530311 .direction = DMA_DEV_TO_MEM,
Linus Walleijead76f32011-02-24 13:21:08 +0100312 .src_maxburst = uap->fifosize >> 1,
Viresh Kumar258aea72012-02-01 16:12:19 +0530313 .device_fc = false,
Linus Walleijead76f32011-02-24 13:21:08 +0100314 };
315
316 chan = dma_request_channel(mask, plat->dma_filter, plat->dma_rx_param);
317 if (!chan) {
318 dev_err(uap->port.dev, "no RX DMA channel!\n");
319 return;
320 }
321
322 dmaengine_slave_config(chan, &rx_conf);
323 uap->dmarx.chan = chan;
324
Chanho Mincb06ff12013-03-27 18:38:11 +0900325 if (plat->dma_rx_poll_enable) {
326 /* Set poll rate if specified. */
327 if (plat->dma_rx_poll_rate) {
328 uap->dmarx.auto_poll_rate = false;
329 uap->dmarx.poll_rate = plat->dma_rx_poll_rate;
330 } else {
331 /*
332 * 100 ms defaults to poll rate if not
333 * specified. This will be adjusted with
334 * the baud rate at set_termios.
335 */
336 uap->dmarx.auto_poll_rate = true;
337 uap->dmarx.poll_rate = 100;
338 }
339 /* 3 secs defaults poll_timeout if not specified. */
340 if (plat->dma_rx_poll_timeout)
341 uap->dmarx.poll_timeout =
342 plat->dma_rx_poll_timeout;
343 else
344 uap->dmarx.poll_timeout = 3000;
345 } else
346 uap->dmarx.auto_poll_rate = false;
347
Linus Walleijead76f32011-02-24 13:21:08 +0100348 dev_info(uap->port.dev, "DMA channel RX %s\n",
349 dma_chan_name(uap->dmarx.chan));
350 }
Russell King68b65f72010-12-22 17:24:39 +0000351}
352
353#ifndef MODULE
354/*
355 * Stack up the UARTs and let the above initcall be done at device
356 * initcall time, because the serial driver is called as an arch
357 * initcall, and at this time the DMA subsystem is not yet registered.
358 * At this point the driver will switch over to using DMA where desired.
359 */
360struct dma_uap {
361 struct list_head node;
362 struct uart_amba_port *uap;
363};
364
365static LIST_HEAD(pl011_dma_uarts);
366
367static int __init pl011_dma_initcall(void)
368{
369 struct list_head *node, *tmp;
370
371 list_for_each_safe(node, tmp, &pl011_dma_uarts) {
372 struct dma_uap *dmau = list_entry(node, struct dma_uap, node);
373 pl011_dma_probe_initcall(dmau->uap);
374 list_del(node);
375 kfree(dmau);
376 }
377 return 0;
378}
379
380device_initcall(pl011_dma_initcall);
381
382static void pl011_dma_probe(struct uart_amba_port *uap)
383{
384 struct dma_uap *dmau = kzalloc(sizeof(struct dma_uap), GFP_KERNEL);
385 if (dmau) {
386 dmau->uap = uap;
387 list_add_tail(&dmau->node, &pl011_dma_uarts);
388 }
389}
390#else
391static void pl011_dma_probe(struct uart_amba_port *uap)
392{
393 pl011_dma_probe_initcall(uap);
394}
395#endif
396
397static void pl011_dma_remove(struct uart_amba_port *uap)
398{
399 /* TODO: remove the initcall if it has not yet executed */
400 if (uap->dmatx.chan)
401 dma_release_channel(uap->dmatx.chan);
Linus Walleijead76f32011-02-24 13:21:08 +0100402 if (uap->dmarx.chan)
403 dma_release_channel(uap->dmarx.chan);
Russell King68b65f72010-12-22 17:24:39 +0000404}
405
Russell King68b65f72010-12-22 17:24:39 +0000406/* Forward declare this for the refill routine */
407static int pl011_dma_tx_refill(struct uart_amba_port *uap);
408
409/*
410 * The current DMA TX buffer has been sent.
411 * Try to queue up another DMA buffer.
412 */
413static void pl011_dma_tx_callback(void *data)
414{
415 struct uart_amba_port *uap = data;
416 struct pl011_dmatx_data *dmatx = &uap->dmatx;
417 unsigned long flags;
418 u16 dmacr;
419
420 spin_lock_irqsave(&uap->port.lock, flags);
421 if (uap->dmatx.queued)
422 dma_unmap_sg(dmatx->chan->device->dev, &dmatx->sg, 1,
423 DMA_TO_DEVICE);
424
425 dmacr = uap->dmacr;
426 uap->dmacr = dmacr & ~UART011_TXDMAE;
427 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
428
429 /*
430 * If TX DMA was disabled, it means that we've stopped the DMA for
431 * some reason (eg, XOFF received, or we want to send an X-char.)
432 *
433 * Note: we need to be careful here of a potential race between DMA
434 * and the rest of the driver - if the driver disables TX DMA while
435 * a TX buffer completing, we must update the tx queued status to
436 * get further refills (hence we check dmacr).
437 */
438 if (!(dmacr & UART011_TXDMAE) || uart_tx_stopped(&uap->port) ||
439 uart_circ_empty(&uap->port.state->xmit)) {
440 uap->dmatx.queued = false;
441 spin_unlock_irqrestore(&uap->port.lock, flags);
442 return;
443 }
444
445 if (pl011_dma_tx_refill(uap) <= 0) {
446 /*
447 * We didn't queue a DMA buffer for some reason, but we
448 * have data pending to be sent. Re-enable the TX IRQ.
449 */
450 uap->im |= UART011_TXIM;
451 writew(uap->im, uap->port.membase + UART011_IMSC);
452 }
453 spin_unlock_irqrestore(&uap->port.lock, flags);
454}
455
456/*
457 * Try to refill the TX DMA buffer.
458 * Locking: called with port lock held and IRQs disabled.
459 * Returns:
460 * 1 if we queued up a TX DMA buffer.
461 * 0 if we didn't want to handle this by DMA
462 * <0 on error
463 */
464static int pl011_dma_tx_refill(struct uart_amba_port *uap)
465{
466 struct pl011_dmatx_data *dmatx = &uap->dmatx;
467 struct dma_chan *chan = dmatx->chan;
468 struct dma_device *dma_dev = chan->device;
469 struct dma_async_tx_descriptor *desc;
470 struct circ_buf *xmit = &uap->port.state->xmit;
471 unsigned int count;
472
473 /*
474 * Try to avoid the overhead involved in using DMA if the
475 * transaction fits in the first half of the FIFO, by using
476 * the standard interrupt handling. This ensures that we
477 * issue a uart_write_wakeup() at the appropriate time.
478 */
479 count = uart_circ_chars_pending(xmit);
480 if (count < (uap->fifosize >> 1)) {
481 uap->dmatx.queued = false;
482 return 0;
483 }
484
485 /*
486 * Bodge: don't send the last character by DMA, as this
487 * will prevent XON from notifying us to restart DMA.
488 */
489 count -= 1;
490
491 /* Else proceed to copy the TX chars to the DMA buffer and fire DMA */
492 if (count > PL011_DMA_BUFFER_SIZE)
493 count = PL011_DMA_BUFFER_SIZE;
494
495 if (xmit->tail < xmit->head)
496 memcpy(&dmatx->buf[0], &xmit->buf[xmit->tail], count);
497 else {
498 size_t first = UART_XMIT_SIZE - xmit->tail;
499 size_t second = xmit->head;
500
501 memcpy(&dmatx->buf[0], &xmit->buf[xmit->tail], first);
502 if (second)
503 memcpy(&dmatx->buf[first], &xmit->buf[0], second);
504 }
505
506 dmatx->sg.length = count;
507
508 if (dma_map_sg(dma_dev->dev, &dmatx->sg, 1, DMA_TO_DEVICE) != 1) {
509 uap->dmatx.queued = false;
510 dev_dbg(uap->port.dev, "unable to map TX DMA\n");
511 return -EBUSY;
512 }
513
Alexandre Bounine16052822012-03-08 16:11:18 -0500514 desc = dmaengine_prep_slave_sg(chan, &dmatx->sg, 1, DMA_MEM_TO_DEV,
Russell King68b65f72010-12-22 17:24:39 +0000515 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
516 if (!desc) {
517 dma_unmap_sg(dma_dev->dev, &dmatx->sg, 1, DMA_TO_DEVICE);
518 uap->dmatx.queued = false;
519 /*
520 * If DMA cannot be used right now, we complete this
521 * transaction via IRQ and let the TTY layer retry.
522 */
523 dev_dbg(uap->port.dev, "TX DMA busy\n");
524 return -EBUSY;
525 }
526
527 /* Some data to go along to the callback */
528 desc->callback = pl011_dma_tx_callback;
529 desc->callback_param = uap;
530
531 /* All errors should happen at prepare time */
532 dmaengine_submit(desc);
533
534 /* Fire the DMA transaction */
535 dma_dev->device_issue_pending(chan);
536
537 uap->dmacr |= UART011_TXDMAE;
538 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
539 uap->dmatx.queued = true;
540
541 /*
542 * Now we know that DMA will fire, so advance the ring buffer
543 * with the stuff we just dispatched.
544 */
545 xmit->tail = (xmit->tail + count) & (UART_XMIT_SIZE - 1);
546 uap->port.icount.tx += count;
547
548 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
549 uart_write_wakeup(&uap->port);
550
551 return 1;
552}
553
554/*
555 * We received a transmit interrupt without a pending X-char but with
556 * pending characters.
557 * Locking: called with port lock held and IRQs disabled.
558 * Returns:
559 * false if we want to use PIO to transmit
560 * true if we queued a DMA buffer
561 */
562static bool pl011_dma_tx_irq(struct uart_amba_port *uap)
563{
Linus Walleijead76f32011-02-24 13:21:08 +0100564 if (!uap->using_tx_dma)
Russell King68b65f72010-12-22 17:24:39 +0000565 return false;
566
567 /*
568 * If we already have a TX buffer queued, but received a
569 * TX interrupt, it will be because we've just sent an X-char.
570 * Ensure the TX DMA is enabled and the TX IRQ is disabled.
571 */
572 if (uap->dmatx.queued) {
573 uap->dmacr |= UART011_TXDMAE;
574 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
575 uap->im &= ~UART011_TXIM;
576 writew(uap->im, uap->port.membase + UART011_IMSC);
577 return true;
578 }
579
580 /*
581 * We don't have a TX buffer queued, so try to queue one.
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300582 * If we successfully queued a buffer, mask the TX IRQ.
Russell King68b65f72010-12-22 17:24:39 +0000583 */
584 if (pl011_dma_tx_refill(uap) > 0) {
585 uap->im &= ~UART011_TXIM;
586 writew(uap->im, uap->port.membase + UART011_IMSC);
587 return true;
588 }
589 return false;
590}
591
592/*
593 * Stop the DMA transmit (eg, due to received XOFF).
594 * Locking: called with port lock held and IRQs disabled.
595 */
596static inline void pl011_dma_tx_stop(struct uart_amba_port *uap)
597{
598 if (uap->dmatx.queued) {
599 uap->dmacr &= ~UART011_TXDMAE;
600 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
601 }
602}
603
604/*
605 * Try to start a DMA transmit, or in the case of an XON/OFF
606 * character queued for send, try to get that character out ASAP.
607 * Locking: called with port lock held and IRQs disabled.
608 * Returns:
609 * false if we want the TX IRQ to be enabled
610 * true if we have a buffer queued
611 */
612static inline bool pl011_dma_tx_start(struct uart_amba_port *uap)
613{
614 u16 dmacr;
615
Linus Walleijead76f32011-02-24 13:21:08 +0100616 if (!uap->using_tx_dma)
Russell King68b65f72010-12-22 17:24:39 +0000617 return false;
618
619 if (!uap->port.x_char) {
620 /* no X-char, try to push chars out in DMA mode */
621 bool ret = true;
622
623 if (!uap->dmatx.queued) {
624 if (pl011_dma_tx_refill(uap) > 0) {
625 uap->im &= ~UART011_TXIM;
626 ret = true;
627 } else {
628 uap->im |= UART011_TXIM;
629 ret = false;
630 }
631 writew(uap->im, uap->port.membase + UART011_IMSC);
632 } else if (!(uap->dmacr & UART011_TXDMAE)) {
633 uap->dmacr |= UART011_TXDMAE;
634 writew(uap->dmacr,
635 uap->port.membase + UART011_DMACR);
636 }
637 return ret;
638 }
639
640 /*
641 * We have an X-char to send. Disable DMA to prevent it loading
642 * the TX fifo, and then see if we can stuff it into the FIFO.
643 */
644 dmacr = uap->dmacr;
645 uap->dmacr &= ~UART011_TXDMAE;
646 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
647
648 if (readw(uap->port.membase + UART01x_FR) & UART01x_FR_TXFF) {
649 /*
650 * No space in the FIFO, so enable the transmit interrupt
651 * so we know when there is space. Note that once we've
652 * loaded the character, we should just re-enable DMA.
653 */
654 return false;
655 }
656
657 writew(uap->port.x_char, uap->port.membase + UART01x_DR);
658 uap->port.icount.tx++;
659 uap->port.x_char = 0;
660
661 /* Success - restore the DMA state */
662 uap->dmacr = dmacr;
663 writew(dmacr, uap->port.membase + UART011_DMACR);
664
665 return true;
666}
667
668/*
669 * Flush the transmit buffer.
670 * Locking: called with port lock held and IRQs disabled.
671 */
672static void pl011_dma_flush_buffer(struct uart_port *port)
673{
674 struct uart_amba_port *uap = (struct uart_amba_port *)port;
675
Linus Walleijead76f32011-02-24 13:21:08 +0100676 if (!uap->using_tx_dma)
Russell King68b65f72010-12-22 17:24:39 +0000677 return;
678
679 /* Avoid deadlock with the DMA engine callback */
680 spin_unlock(&uap->port.lock);
681 dmaengine_terminate_all(uap->dmatx.chan);
682 spin_lock(&uap->port.lock);
683 if (uap->dmatx.queued) {
684 dma_unmap_sg(uap->dmatx.chan->device->dev, &uap->dmatx.sg, 1,
685 DMA_TO_DEVICE);
686 uap->dmatx.queued = false;
687 uap->dmacr &= ~UART011_TXDMAE;
688 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
689 }
690}
691
Linus Walleijead76f32011-02-24 13:21:08 +0100692static void pl011_dma_rx_callback(void *data);
693
694static int pl011_dma_rx_trigger_dma(struct uart_amba_port *uap)
695{
696 struct dma_chan *rxchan = uap->dmarx.chan;
Linus Walleijead76f32011-02-24 13:21:08 +0100697 struct pl011_dmarx_data *dmarx = &uap->dmarx;
698 struct dma_async_tx_descriptor *desc;
699 struct pl011_sgbuf *sgbuf;
700
701 if (!rxchan)
702 return -EIO;
703
704 /* Start the RX DMA job */
705 sgbuf = uap->dmarx.use_buf_b ?
706 &uap->dmarx.sgbuf_b : &uap->dmarx.sgbuf_a;
Alexandre Bounine16052822012-03-08 16:11:18 -0500707 desc = dmaengine_prep_slave_sg(rxchan, &sgbuf->sg, 1,
Vinod Koula485df42011-10-14 10:47:38 +0530708 DMA_DEV_TO_MEM,
Linus Walleijead76f32011-02-24 13:21:08 +0100709 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
710 /*
711 * If the DMA engine is busy and cannot prepare a
712 * channel, no big deal, the driver will fall back
713 * to interrupt mode as a result of this error code.
714 */
715 if (!desc) {
716 uap->dmarx.running = false;
717 dmaengine_terminate_all(rxchan);
718 return -EBUSY;
719 }
720
721 /* Some data to go along to the callback */
722 desc->callback = pl011_dma_rx_callback;
723 desc->callback_param = uap;
724 dmarx->cookie = dmaengine_submit(desc);
725 dma_async_issue_pending(rxchan);
726
727 uap->dmacr |= UART011_RXDMAE;
728 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
729 uap->dmarx.running = true;
730
731 uap->im &= ~UART011_RXIM;
732 writew(uap->im, uap->port.membase + UART011_IMSC);
733
734 return 0;
735}
736
737/*
738 * This is called when either the DMA job is complete, or
739 * the FIFO timeout interrupt occurred. This must be called
740 * with the port spinlock uap->port.lock held.
741 */
742static void pl011_dma_rx_chars(struct uart_amba_port *uap,
743 u32 pending, bool use_buf_b,
744 bool readfifo)
745{
Jiri Slaby05c7cd32013-01-03 15:53:04 +0100746 struct tty_port *port = &uap->port.state->port;
Linus Walleijead76f32011-02-24 13:21:08 +0100747 struct pl011_sgbuf *sgbuf = use_buf_b ?
748 &uap->dmarx.sgbuf_b : &uap->dmarx.sgbuf_a;
Linus Walleijead76f32011-02-24 13:21:08 +0100749 int dma_count = 0;
750 u32 fifotaken = 0; /* only used for vdbg() */
751
Chanho Mincb06ff12013-03-27 18:38:11 +0900752 struct pl011_dmarx_data *dmarx = &uap->dmarx;
753 int dmataken = 0;
754
755 if (uap->dmarx.poll_rate) {
756 /* The data can be taken by polling */
757 dmataken = sgbuf->sg.length - dmarx->last_residue;
758 /* Recalculate the pending size */
759 if (pending >= dmataken)
760 pending -= dmataken;
761 }
762
763 /* Pick the remain data from the DMA */
Linus Walleijead76f32011-02-24 13:21:08 +0100764 if (pending) {
Linus Walleijead76f32011-02-24 13:21:08 +0100765
766 /*
767 * First take all chars in the DMA pipe, then look in the FIFO.
768 * Note that tty_insert_flip_buf() tries to take as many chars
769 * as it can.
770 */
Chanho Mincb06ff12013-03-27 18:38:11 +0900771 dma_count = tty_insert_flip_string(port, sgbuf->buf + dmataken,
772 pending);
Linus Walleijead76f32011-02-24 13:21:08 +0100773
774 uap->port.icount.rx += dma_count;
775 if (dma_count < pending)
776 dev_warn(uap->port.dev,
777 "couldn't insert all characters (TTY is full?)\n");
778 }
779
Chanho Mincb06ff12013-03-27 18:38:11 +0900780 /* Reset the last_residue for Rx DMA poll */
781 if (uap->dmarx.poll_rate)
782 dmarx->last_residue = sgbuf->sg.length;
783
Linus Walleijead76f32011-02-24 13:21:08 +0100784 /*
785 * Only continue with trying to read the FIFO if all DMA chars have
786 * been taken first.
787 */
788 if (dma_count == pending && readfifo) {
789 /* Clear any error flags */
790 writew(UART011_OEIS | UART011_BEIS | UART011_PEIS | UART011_FEIS,
791 uap->port.membase + UART011_ICR);
792
793 /*
794 * If we read all the DMA'd characters, and we had an
Linus Walleij29772c42011-02-24 13:21:36 +0100795 * incomplete buffer, that could be due to an rx error, or
796 * maybe we just timed out. Read any pending chars and check
797 * the error status.
798 *
799 * Error conditions will only occur in the FIFO, these will
800 * trigger an immediate interrupt and stop the DMA job, so we
801 * will always find the error in the FIFO, never in the DMA
802 * buffer.
Linus Walleijead76f32011-02-24 13:21:08 +0100803 */
Linus Walleij29772c42011-02-24 13:21:36 +0100804 fifotaken = pl011_fifo_to_tty(uap);
Linus Walleijead76f32011-02-24 13:21:08 +0100805 }
806
807 spin_unlock(&uap->port.lock);
808 dev_vdbg(uap->port.dev,
809 "Took %d chars from DMA buffer and %d chars from the FIFO\n",
810 dma_count, fifotaken);
Jiri Slaby2e124b42013-01-03 15:53:06 +0100811 tty_flip_buffer_push(port);
Linus Walleijead76f32011-02-24 13:21:08 +0100812 spin_lock(&uap->port.lock);
813}
814
815static void pl011_dma_rx_irq(struct uart_amba_port *uap)
816{
817 struct pl011_dmarx_data *dmarx = &uap->dmarx;
818 struct dma_chan *rxchan = dmarx->chan;
819 struct pl011_sgbuf *sgbuf = dmarx->use_buf_b ?
820 &dmarx->sgbuf_b : &dmarx->sgbuf_a;
821 size_t pending;
822 struct dma_tx_state state;
823 enum dma_status dmastat;
824
825 /*
826 * Pause the transfer so we can trust the current counter,
827 * do this before we pause the PL011 block, else we may
828 * overflow the FIFO.
829 */
830 if (dmaengine_pause(rxchan))
831 dev_err(uap->port.dev, "unable to pause DMA transfer\n");
832 dmastat = rxchan->device->device_tx_status(rxchan,
833 dmarx->cookie, &state);
834 if (dmastat != DMA_PAUSED)
835 dev_err(uap->port.dev, "unable to pause DMA transfer\n");
836
837 /* Disable RX DMA - incoming data will wait in the FIFO */
838 uap->dmacr &= ~UART011_RXDMAE;
839 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
840 uap->dmarx.running = false;
841
842 pending = sgbuf->sg.length - state.residue;
843 BUG_ON(pending > PL011_DMA_BUFFER_SIZE);
844 /* Then we terminate the transfer - we now know our residue */
845 dmaengine_terminate_all(rxchan);
846
847 /*
848 * This will take the chars we have so far and insert
849 * into the framework.
850 */
851 pl011_dma_rx_chars(uap, pending, dmarx->use_buf_b, true);
852
853 /* Switch buffer & re-trigger DMA job */
854 dmarx->use_buf_b = !dmarx->use_buf_b;
855 if (pl011_dma_rx_trigger_dma(uap)) {
856 dev_dbg(uap->port.dev, "could not retrigger RX DMA job "
857 "fall back to interrupt mode\n");
858 uap->im |= UART011_RXIM;
859 writew(uap->im, uap->port.membase + UART011_IMSC);
860 }
861}
862
863static void pl011_dma_rx_callback(void *data)
864{
865 struct uart_amba_port *uap = data;
866 struct pl011_dmarx_data *dmarx = &uap->dmarx;
Chanho Min6dc01aa2012-02-20 10:24:40 +0900867 struct dma_chan *rxchan = dmarx->chan;
Linus Walleijead76f32011-02-24 13:21:08 +0100868 bool lastbuf = dmarx->use_buf_b;
Chanho Min6dc01aa2012-02-20 10:24:40 +0900869 struct pl011_sgbuf *sgbuf = dmarx->use_buf_b ?
870 &dmarx->sgbuf_b : &dmarx->sgbuf_a;
871 size_t pending;
872 struct dma_tx_state state;
Linus Walleijead76f32011-02-24 13:21:08 +0100873 int ret;
874
875 /*
876 * This completion interrupt occurs typically when the
877 * RX buffer is totally stuffed but no timeout has yet
878 * occurred. When that happens, we just want the RX
879 * routine to flush out the secondary DMA buffer while
880 * we immediately trigger the next DMA job.
881 */
882 spin_lock_irq(&uap->port.lock);
Chanho Min6dc01aa2012-02-20 10:24:40 +0900883 /*
884 * Rx data can be taken by the UART interrupts during
885 * the DMA irq handler. So we check the residue here.
886 */
887 rxchan->device->device_tx_status(rxchan, dmarx->cookie, &state);
888 pending = sgbuf->sg.length - state.residue;
889 BUG_ON(pending > PL011_DMA_BUFFER_SIZE);
890 /* Then we terminate the transfer - we now know our residue */
891 dmaengine_terminate_all(rxchan);
892
Linus Walleijead76f32011-02-24 13:21:08 +0100893 uap->dmarx.running = false;
894 dmarx->use_buf_b = !lastbuf;
895 ret = pl011_dma_rx_trigger_dma(uap);
896
Chanho Min6dc01aa2012-02-20 10:24:40 +0900897 pl011_dma_rx_chars(uap, pending, lastbuf, false);
Linus Walleijead76f32011-02-24 13:21:08 +0100898 spin_unlock_irq(&uap->port.lock);
899 /*
900 * Do this check after we picked the DMA chars so we don't
901 * get some IRQ immediately from RX.
902 */
903 if (ret) {
904 dev_dbg(uap->port.dev, "could not retrigger RX DMA job "
905 "fall back to interrupt mode\n");
906 uap->im |= UART011_RXIM;
907 writew(uap->im, uap->port.membase + UART011_IMSC);
908 }
909}
910
911/*
912 * Stop accepting received characters, when we're shutting down or
913 * suspending this port.
914 * Locking: called with port lock held and IRQs disabled.
915 */
916static inline void pl011_dma_rx_stop(struct uart_amba_port *uap)
917{
918 /* FIXME. Just disable the DMA enable */
919 uap->dmacr &= ~UART011_RXDMAE;
920 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
921}
Russell King68b65f72010-12-22 17:24:39 +0000922
Chanho Mincb06ff12013-03-27 18:38:11 +0900923/*
924 * Timer handler for Rx DMA polling.
925 * Every polling, It checks the residue in the dma buffer and transfer
926 * data to the tty. Also, last_residue is updated for the next polling.
927 */
928static void pl011_dma_rx_poll(unsigned long args)
929{
930 struct uart_amba_port *uap = (struct uart_amba_port *)args;
931 struct tty_port *port = &uap->port.state->port;
932 struct pl011_dmarx_data *dmarx = &uap->dmarx;
933 struct dma_chan *rxchan = uap->dmarx.chan;
934 unsigned long flags = 0;
935 unsigned int dmataken = 0;
936 unsigned int size = 0;
937 struct pl011_sgbuf *sgbuf;
938 int dma_count;
939 struct dma_tx_state state;
940
941 sgbuf = dmarx->use_buf_b ? &uap->dmarx.sgbuf_b : &uap->dmarx.sgbuf_a;
942 rxchan->device->device_tx_status(rxchan, dmarx->cookie, &state);
943 if (likely(state.residue < dmarx->last_residue)) {
944 dmataken = sgbuf->sg.length - dmarx->last_residue;
945 size = dmarx->last_residue - state.residue;
946 dma_count = tty_insert_flip_string(port, sgbuf->buf + dmataken,
947 size);
948 if (dma_count == size)
949 dmarx->last_residue = state.residue;
950 dmarx->last_jiffies = jiffies;
951 }
952 tty_flip_buffer_push(port);
953
954 /*
955 * If no data is received in poll_timeout, the driver will fall back
956 * to interrupt mode. We will retrigger DMA at the first interrupt.
957 */
958 if (jiffies_to_msecs(jiffies - dmarx->last_jiffies)
959 > uap->dmarx.poll_timeout) {
960
961 spin_lock_irqsave(&uap->port.lock, flags);
962 pl011_dma_rx_stop(uap);
963 spin_unlock_irqrestore(&uap->port.lock, flags);
964
965 uap->dmarx.running = false;
966 dmaengine_terminate_all(rxchan);
967 del_timer(&uap->dmarx.timer);
968 } else {
969 mod_timer(&uap->dmarx.timer,
970 jiffies + msecs_to_jiffies(uap->dmarx.poll_rate));
971 }
972}
973
Russell King68b65f72010-12-22 17:24:39 +0000974static void pl011_dma_startup(struct uart_amba_port *uap)
975{
Linus Walleijead76f32011-02-24 13:21:08 +0100976 int ret;
977
Russell King68b65f72010-12-22 17:24:39 +0000978 if (!uap->dmatx.chan)
979 return;
980
981 uap->dmatx.buf = kmalloc(PL011_DMA_BUFFER_SIZE, GFP_KERNEL);
982 if (!uap->dmatx.buf) {
983 dev_err(uap->port.dev, "no memory for DMA TX buffer\n");
984 uap->port.fifosize = uap->fifosize;
985 return;
986 }
987
988 sg_init_one(&uap->dmatx.sg, uap->dmatx.buf, PL011_DMA_BUFFER_SIZE);
989
990 /* The DMA buffer is now the FIFO the TTY subsystem can use */
991 uap->port.fifosize = PL011_DMA_BUFFER_SIZE;
Linus Walleijead76f32011-02-24 13:21:08 +0100992 uap->using_tx_dma = true;
Russell King68b65f72010-12-22 17:24:39 +0000993
Linus Walleijead76f32011-02-24 13:21:08 +0100994 if (!uap->dmarx.chan)
995 goto skip_rx;
996
997 /* Allocate and map DMA RX buffers */
998 ret = pl011_sgbuf_init(uap->dmarx.chan, &uap->dmarx.sgbuf_a,
999 DMA_FROM_DEVICE);
1000 if (ret) {
1001 dev_err(uap->port.dev, "failed to init DMA %s: %d\n",
1002 "RX buffer A", ret);
1003 goto skip_rx;
1004 }
1005
1006 ret = pl011_sgbuf_init(uap->dmarx.chan, &uap->dmarx.sgbuf_b,
1007 DMA_FROM_DEVICE);
1008 if (ret) {
1009 dev_err(uap->port.dev, "failed to init DMA %s: %d\n",
1010 "RX buffer B", ret);
1011 pl011_sgbuf_free(uap->dmarx.chan, &uap->dmarx.sgbuf_a,
1012 DMA_FROM_DEVICE);
1013 goto skip_rx;
1014 }
1015
1016 uap->using_rx_dma = true;
1017
1018skip_rx:
Russell King68b65f72010-12-22 17:24:39 +00001019 /* Turn on DMA error (RX/TX will be enabled on demand) */
1020 uap->dmacr |= UART011_DMAONERR;
1021 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
Russell King38d62432010-12-22 17:59:16 +00001022
1023 /*
1024 * ST Micro variants has some specific dma burst threshold
1025 * compensation. Set this to 16 bytes, so burst will only
1026 * be issued above/below 16 bytes.
1027 */
1028 if (uap->vendor->dma_threshold)
1029 writew(ST_UART011_DMAWM_RX_16 | ST_UART011_DMAWM_TX_16,
1030 uap->port.membase + ST_UART011_DMAWM);
Linus Walleijead76f32011-02-24 13:21:08 +01001031
1032 if (uap->using_rx_dma) {
1033 if (pl011_dma_rx_trigger_dma(uap))
1034 dev_dbg(uap->port.dev, "could not trigger initial "
1035 "RX DMA job, fall back to interrupt mode\n");
Chanho Mincb06ff12013-03-27 18:38:11 +09001036 if (uap->dmarx.poll_rate) {
1037 init_timer(&(uap->dmarx.timer));
1038 uap->dmarx.timer.function = pl011_dma_rx_poll;
1039 uap->dmarx.timer.data = (unsigned long)uap;
1040 mod_timer(&uap->dmarx.timer,
1041 jiffies +
1042 msecs_to_jiffies(uap->dmarx.poll_rate));
1043 uap->dmarx.last_residue = PL011_DMA_BUFFER_SIZE;
1044 uap->dmarx.last_jiffies = jiffies;
1045 }
Linus Walleijead76f32011-02-24 13:21:08 +01001046 }
Russell King68b65f72010-12-22 17:24:39 +00001047}
1048
1049static void pl011_dma_shutdown(struct uart_amba_port *uap)
1050{
Linus Walleijead76f32011-02-24 13:21:08 +01001051 if (!(uap->using_tx_dma || uap->using_rx_dma))
Russell King68b65f72010-12-22 17:24:39 +00001052 return;
1053
1054 /* Disable RX and TX DMA */
1055 while (readw(uap->port.membase + UART01x_FR) & UART01x_FR_BUSY)
1056 barrier();
1057
1058 spin_lock_irq(&uap->port.lock);
1059 uap->dmacr &= ~(UART011_DMAONERR | UART011_RXDMAE | UART011_TXDMAE);
1060 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
1061 spin_unlock_irq(&uap->port.lock);
1062
Linus Walleijead76f32011-02-24 13:21:08 +01001063 if (uap->using_tx_dma) {
1064 /* In theory, this should already be done by pl011_dma_flush_buffer */
1065 dmaengine_terminate_all(uap->dmatx.chan);
1066 if (uap->dmatx.queued) {
1067 dma_unmap_sg(uap->dmatx.chan->device->dev, &uap->dmatx.sg, 1,
1068 DMA_TO_DEVICE);
1069 uap->dmatx.queued = false;
1070 }
1071
1072 kfree(uap->dmatx.buf);
1073 uap->using_tx_dma = false;
Russell King68b65f72010-12-22 17:24:39 +00001074 }
1075
Linus Walleijead76f32011-02-24 13:21:08 +01001076 if (uap->using_rx_dma) {
1077 dmaengine_terminate_all(uap->dmarx.chan);
1078 /* Clean up the RX DMA */
1079 pl011_sgbuf_free(uap->dmarx.chan, &uap->dmarx.sgbuf_a, DMA_FROM_DEVICE);
1080 pl011_sgbuf_free(uap->dmarx.chan, &uap->dmarx.sgbuf_b, DMA_FROM_DEVICE);
Chanho Mincb06ff12013-03-27 18:38:11 +09001081 if (uap->dmarx.poll_rate)
1082 del_timer_sync(&uap->dmarx.timer);
Linus Walleijead76f32011-02-24 13:21:08 +01001083 uap->using_rx_dma = false;
1084 }
Russell King68b65f72010-12-22 17:24:39 +00001085}
1086
Linus Walleijead76f32011-02-24 13:21:08 +01001087static inline bool pl011_dma_rx_available(struct uart_amba_port *uap)
1088{
1089 return uap->using_rx_dma;
1090}
1091
1092static inline bool pl011_dma_rx_running(struct uart_amba_port *uap)
1093{
1094 return uap->using_rx_dma && uap->dmarx.running;
1095}
1096
Russell King68b65f72010-12-22 17:24:39 +00001097#else
1098/* Blank functions if the DMA engine is not available */
1099static inline void pl011_dma_probe(struct uart_amba_port *uap)
1100{
1101}
1102
1103static inline void pl011_dma_remove(struct uart_amba_port *uap)
1104{
1105}
1106
1107static inline void pl011_dma_startup(struct uart_amba_port *uap)
1108{
1109}
1110
1111static inline void pl011_dma_shutdown(struct uart_amba_port *uap)
1112{
1113}
1114
1115static inline bool pl011_dma_tx_irq(struct uart_amba_port *uap)
1116{
1117 return false;
1118}
1119
1120static inline void pl011_dma_tx_stop(struct uart_amba_port *uap)
1121{
1122}
1123
1124static inline bool pl011_dma_tx_start(struct uart_amba_port *uap)
1125{
1126 return false;
1127}
1128
Linus Walleijead76f32011-02-24 13:21:08 +01001129static inline void pl011_dma_rx_irq(struct uart_amba_port *uap)
1130{
1131}
1132
1133static inline void pl011_dma_rx_stop(struct uart_amba_port *uap)
1134{
1135}
1136
1137static inline int pl011_dma_rx_trigger_dma(struct uart_amba_port *uap)
1138{
1139 return -EIO;
1140}
1141
1142static inline bool pl011_dma_rx_available(struct uart_amba_port *uap)
1143{
1144 return false;
1145}
1146
1147static inline bool pl011_dma_rx_running(struct uart_amba_port *uap)
1148{
1149 return false;
1150}
1151
Russell King68b65f72010-12-22 17:24:39 +00001152#define pl011_dma_flush_buffer NULL
1153#endif
1154
Russell Kingb129a8c2005-08-31 10:12:14 +01001155static void pl011_stop_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156{
1157 struct uart_amba_port *uap = (struct uart_amba_port *)port;
1158
1159 uap->im &= ~UART011_TXIM;
1160 writew(uap->im, uap->port.membase + UART011_IMSC);
Russell King68b65f72010-12-22 17:24:39 +00001161 pl011_dma_tx_stop(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162}
1163
Russell Kingb129a8c2005-08-31 10:12:14 +01001164static void pl011_start_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165{
1166 struct uart_amba_port *uap = (struct uart_amba_port *)port;
1167
Russell King68b65f72010-12-22 17:24:39 +00001168 if (!pl011_dma_tx_start(uap)) {
1169 uap->im |= UART011_TXIM;
1170 writew(uap->im, uap->port.membase + UART011_IMSC);
1171 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172}
1173
1174static void pl011_stop_rx(struct uart_port *port)
1175{
1176 struct uart_amba_port *uap = (struct uart_amba_port *)port;
1177
1178 uap->im &= ~(UART011_RXIM|UART011_RTIM|UART011_FEIM|
1179 UART011_PEIM|UART011_BEIM|UART011_OEIM);
1180 writew(uap->im, uap->port.membase + UART011_IMSC);
Linus Walleijead76f32011-02-24 13:21:08 +01001181
1182 pl011_dma_rx_stop(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183}
1184
1185static void pl011_enable_ms(struct uart_port *port)
1186{
1187 struct uart_amba_port *uap = (struct uart_amba_port *)port;
1188
1189 uap->im |= UART011_RIMIM|UART011_CTSMIM|UART011_DCDMIM|UART011_DSRMIM;
1190 writew(uap->im, uap->port.membase + UART011_IMSC);
1191}
1192
David Howells7d12e782006-10-05 14:55:46 +01001193static void pl011_rx_chars(struct uart_amba_port *uap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194{
Linus Walleij29772c42011-02-24 13:21:36 +01001195 pl011_fifo_to_tty(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196
Thomas Gleixner2389b272007-05-29 21:53:50 +01001197 spin_unlock(&uap->port.lock);
Jiri Slaby2e124b42013-01-03 15:53:06 +01001198 tty_flip_buffer_push(&uap->port.state->port);
Linus Walleijead76f32011-02-24 13:21:08 +01001199 /*
1200 * If we were temporarily out of DMA mode for a while,
1201 * attempt to switch back to DMA mode again.
1202 */
1203 if (pl011_dma_rx_available(uap)) {
1204 if (pl011_dma_rx_trigger_dma(uap)) {
1205 dev_dbg(uap->port.dev, "could not trigger RX DMA job "
1206 "fall back to interrupt mode again\n");
1207 uap->im |= UART011_RXIM;
Chanho Mincb06ff12013-03-27 18:38:11 +09001208 } else {
Linus Walleijead76f32011-02-24 13:21:08 +01001209 uap->im &= ~UART011_RXIM;
Chanho Min89fa28d2013-04-03 11:10:37 +09001210#ifdef CONFIG_DMA_ENGINE
Chanho Mincb06ff12013-03-27 18:38:11 +09001211 /* Start Rx DMA poll */
1212 if (uap->dmarx.poll_rate) {
1213 uap->dmarx.last_jiffies = jiffies;
1214 uap->dmarx.last_residue = PL011_DMA_BUFFER_SIZE;
1215 mod_timer(&uap->dmarx.timer,
1216 jiffies +
1217 msecs_to_jiffies(uap->dmarx.poll_rate));
1218 }
Chanho Min89fa28d2013-04-03 11:10:37 +09001219#endif
Chanho Mincb06ff12013-03-27 18:38:11 +09001220 }
1221
Linus Walleijead76f32011-02-24 13:21:08 +01001222 writew(uap->im, uap->port.membase + UART011_IMSC);
1223 }
Thomas Gleixner2389b272007-05-29 21:53:50 +01001224 spin_lock(&uap->port.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225}
1226
1227static void pl011_tx_chars(struct uart_amba_port *uap)
1228{
Alan Coxebd2c8f2009-09-19 13:13:28 -07001229 struct circ_buf *xmit = &uap->port.state->xmit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 int count;
1231
1232 if (uap->port.x_char) {
1233 writew(uap->port.x_char, uap->port.membase + UART01x_DR);
1234 uap->port.icount.tx++;
1235 uap->port.x_char = 0;
1236 return;
1237 }
1238 if (uart_circ_empty(xmit) || uart_tx_stopped(&uap->port)) {
Russell Kingb129a8c2005-08-31 10:12:14 +01001239 pl011_stop_tx(&uap->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 return;
1241 }
1242
Russell King68b65f72010-12-22 17:24:39 +00001243 /* If we are using DMA mode, try to send some characters. */
1244 if (pl011_dma_tx_irq(uap))
1245 return;
1246
Russell Kingffca2b12010-12-22 17:13:05 +00001247 count = uap->fifosize >> 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 do {
1249 writew(xmit->buf[xmit->tail], uap->port.membase + UART01x_DR);
1250 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
1251 uap->port.icount.tx++;
1252 if (uart_circ_empty(xmit))
1253 break;
1254 } while (--count > 0);
1255
1256 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
1257 uart_write_wakeup(&uap->port);
1258
1259 if (uart_circ_empty(xmit))
Russell Kingb129a8c2005-08-31 10:12:14 +01001260 pl011_stop_tx(&uap->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261}
1262
1263static void pl011_modem_status(struct uart_amba_port *uap)
1264{
1265 unsigned int status, delta;
1266
1267 status = readw(uap->port.membase + UART01x_FR) & UART01x_FR_MODEM_ANY;
1268
1269 delta = status ^ uap->old_status;
1270 uap->old_status = status;
1271
1272 if (!delta)
1273 return;
1274
1275 if (delta & UART01x_FR_DCD)
1276 uart_handle_dcd_change(&uap->port, status & UART01x_FR_DCD);
1277
1278 if (delta & UART01x_FR_DSR)
1279 uap->port.icount.dsr++;
1280
1281 if (delta & UART01x_FR_CTS)
1282 uart_handle_cts_change(&uap->port, status & UART01x_FR_CTS);
1283
Alan Coxbdc04e32009-09-19 13:13:31 -07001284 wake_up_interruptible(&uap->port.state->port.delta_msr_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285}
1286
David Howells7d12e782006-10-05 14:55:46 +01001287static irqreturn_t pl011_int(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288{
1289 struct uart_amba_port *uap = dev_id;
Russell King963cc982010-12-22 17:16:09 +00001290 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 unsigned int status, pass_counter = AMBA_ISR_PASS_LIMIT;
1292 int handled = 0;
Rajanikanth H.V4fd06902012-03-26 11:17:02 +02001293 unsigned int dummy_read;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294
Russell King963cc982010-12-22 17:16:09 +00001295 spin_lock_irqsave(&uap->port.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 status = readw(uap->port.membase + UART011_MIS);
1297 if (status) {
1298 do {
Rajanikanth H.V4fd06902012-03-26 11:17:02 +02001299 if (uap->vendor->cts_event_workaround) {
1300 /* workaround to make sure that all bits are unlocked.. */
1301 writew(0x00, uap->port.membase + UART011_ICR);
1302
1303 /*
1304 * WA: introduce 26ns(1 uart clk) delay before W1C;
1305 * single apb access will incur 2 pclk(133.12Mhz) delay,
1306 * so add 2 dummy reads
1307 */
1308 dummy_read = readw(uap->port.membase + UART011_ICR);
1309 dummy_read = readw(uap->port.membase + UART011_ICR);
1310 }
1311
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 writew(status & ~(UART011_TXIS|UART011_RTIS|
1313 UART011_RXIS),
1314 uap->port.membase + UART011_ICR);
1315
Linus Walleijead76f32011-02-24 13:21:08 +01001316 if (status & (UART011_RTIS|UART011_RXIS)) {
1317 if (pl011_dma_rx_running(uap))
1318 pl011_dma_rx_irq(uap);
1319 else
1320 pl011_rx_chars(uap);
1321 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 if (status & (UART011_DSRMIS|UART011_DCDMIS|
1323 UART011_CTSMIS|UART011_RIMIS))
1324 pl011_modem_status(uap);
1325 if (status & UART011_TXIS)
1326 pl011_tx_chars(uap);
1327
Rajanikanth H.V4fd06902012-03-26 11:17:02 +02001328 if (pass_counter-- == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 break;
1330
1331 status = readw(uap->port.membase + UART011_MIS);
1332 } while (status != 0);
1333 handled = 1;
1334 }
1335
Russell King963cc982010-12-22 17:16:09 +00001336 spin_unlock_irqrestore(&uap->port.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337
1338 return IRQ_RETVAL(handled);
1339}
1340
Linus Walleije643f872012-06-17 15:44:19 +02001341static unsigned int pl011_tx_empty(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342{
1343 struct uart_amba_port *uap = (struct uart_amba_port *)port;
1344 unsigned int status = readw(uap->port.membase + UART01x_FR);
1345 return status & (UART01x_FR_BUSY|UART01x_FR_TXFF) ? 0 : TIOCSER_TEMT;
1346}
1347
Linus Walleije643f872012-06-17 15:44:19 +02001348static unsigned int pl011_get_mctrl(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349{
1350 struct uart_amba_port *uap = (struct uart_amba_port *)port;
1351 unsigned int result = 0;
1352 unsigned int status = readw(uap->port.membase + UART01x_FR);
1353
Jiri Slaby5159f402007-10-18 23:40:31 -07001354#define TIOCMBIT(uartbit, tiocmbit) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 if (status & uartbit) \
1356 result |= tiocmbit
1357
Jiri Slaby5159f402007-10-18 23:40:31 -07001358 TIOCMBIT(UART01x_FR_DCD, TIOCM_CAR);
1359 TIOCMBIT(UART01x_FR_DSR, TIOCM_DSR);
1360 TIOCMBIT(UART01x_FR_CTS, TIOCM_CTS);
1361 TIOCMBIT(UART011_FR_RI, TIOCM_RNG);
1362#undef TIOCMBIT
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 return result;
1364}
1365
1366static void pl011_set_mctrl(struct uart_port *port, unsigned int mctrl)
1367{
1368 struct uart_amba_port *uap = (struct uart_amba_port *)port;
1369 unsigned int cr;
1370
1371 cr = readw(uap->port.membase + UART011_CR);
1372
Jiri Slaby5159f402007-10-18 23:40:31 -07001373#define TIOCMBIT(tiocmbit, uartbit) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 if (mctrl & tiocmbit) \
1375 cr |= uartbit; \
1376 else \
1377 cr &= ~uartbit
1378
Jiri Slaby5159f402007-10-18 23:40:31 -07001379 TIOCMBIT(TIOCM_RTS, UART011_CR_RTS);
1380 TIOCMBIT(TIOCM_DTR, UART011_CR_DTR);
1381 TIOCMBIT(TIOCM_OUT1, UART011_CR_OUT1);
1382 TIOCMBIT(TIOCM_OUT2, UART011_CR_OUT2);
1383 TIOCMBIT(TIOCM_LOOP, UART011_CR_LBE);
Rabin Vincent3b438162010-02-12 06:43:11 +01001384
1385 if (uap->autorts) {
1386 /* We need to disable auto-RTS if we want to turn RTS off */
1387 TIOCMBIT(TIOCM_RTS, UART011_CR_RTSEN);
1388 }
Jiri Slaby5159f402007-10-18 23:40:31 -07001389#undef TIOCMBIT
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390
1391 writew(cr, uap->port.membase + UART011_CR);
1392}
1393
1394static void pl011_break_ctl(struct uart_port *port, int break_state)
1395{
1396 struct uart_amba_port *uap = (struct uart_amba_port *)port;
1397 unsigned long flags;
1398 unsigned int lcr_h;
1399
1400 spin_lock_irqsave(&uap->port.lock, flags);
Linus Walleijec489aa2010-06-02 08:13:52 +01001401 lcr_h = readw(uap->port.membase + uap->lcrh_tx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 if (break_state == -1)
1403 lcr_h |= UART01x_LCRH_BRK;
1404 else
1405 lcr_h &= ~UART01x_LCRH_BRK;
Linus Walleijec489aa2010-06-02 08:13:52 +01001406 writew(lcr_h, uap->port.membase + uap->lcrh_tx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 spin_unlock_irqrestore(&uap->port.lock, flags);
1408}
1409
Jason Wessel84b5ae12008-02-20 13:33:39 -06001410#ifdef CONFIG_CONSOLE_POLL
Anton Vorontsov5c8124a2012-09-24 14:27:55 -07001411
1412static void pl011_quiesce_irqs(struct uart_port *port)
1413{
1414 struct uart_amba_port *uap = (struct uart_amba_port *)port;
1415 unsigned char __iomem *regs = uap->port.membase;
1416
1417 writew(readw(regs + UART011_MIS), regs + UART011_ICR);
1418 /*
1419 * There is no way to clear TXIM as this is "ready to transmit IRQ", so
1420 * we simply mask it. start_tx() will unmask it.
1421 *
1422 * Note we can race with start_tx(), and if the race happens, the
1423 * polling user might get another interrupt just after we clear it.
1424 * But it should be OK and can happen even w/o the race, e.g.
1425 * controller immediately got some new data and raised the IRQ.
1426 *
1427 * And whoever uses polling routines assumes that it manages the device
1428 * (including tx queue), so we're also fine with start_tx()'s caller
1429 * side.
1430 */
1431 writew(readw(regs + UART011_IMSC) & ~UART011_TXIM, regs + UART011_IMSC);
1432}
1433
Linus Walleije643f872012-06-17 15:44:19 +02001434static int pl011_get_poll_char(struct uart_port *port)
Jason Wessel84b5ae12008-02-20 13:33:39 -06001435{
1436 struct uart_amba_port *uap = (struct uart_amba_port *)port;
1437 unsigned int status;
1438
Anton Vorontsov5c8124a2012-09-24 14:27:55 -07001439 /*
1440 * The caller might need IRQs lowered, e.g. if used with KDB NMI
1441 * debugger.
1442 */
1443 pl011_quiesce_irqs(port);
1444
Jason Wesself5316b42010-05-20 21:04:22 -05001445 status = readw(uap->port.membase + UART01x_FR);
1446 if (status & UART01x_FR_RXFE)
1447 return NO_POLL_CHAR;
Jason Wessel84b5ae12008-02-20 13:33:39 -06001448
1449 return readw(uap->port.membase + UART01x_DR);
1450}
1451
Linus Walleije643f872012-06-17 15:44:19 +02001452static void pl011_put_poll_char(struct uart_port *port,
Jason Wessel84b5ae12008-02-20 13:33:39 -06001453 unsigned char ch)
1454{
1455 struct uart_amba_port *uap = (struct uart_amba_port *)port;
1456
1457 while (readw(uap->port.membase + UART01x_FR) & UART01x_FR_TXFF)
1458 barrier();
1459
1460 writew(ch, uap->port.membase + UART01x_DR);
1461}
1462
1463#endif /* CONFIG_CONSOLE_POLL */
1464
Anton Vorontsovb3564c22012-09-24 14:27:54 -07001465static int pl011_hwinit(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466{
1467 struct uart_amba_port *uap = (struct uart_amba_port *)port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 int retval;
1469
Linus Walleij78d80c52012-05-23 21:18:46 +02001470 /* Optionaly enable pins to be muxed in and configured */
1471 if (!IS_ERR(uap->pins_default)) {
1472 retval = pinctrl_select_state(uap->pinctrl, uap->pins_default);
1473 if (retval)
1474 dev_err(port->dev,
1475 "could not set default pins\n");
1476 }
1477
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 /*
1479 * Try to enable the clock producer.
1480 */
Julia Lawall1c4c4392012-08-26 18:01:01 +02001481 retval = clk_prepare_enable(uap->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 if (retval)
Julia Lawall1c4c4392012-08-26 18:01:01 +02001483 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484
1485 uap->port.uartclk = clk_get_rate(uap->clk);
1486
Linus Walleij9b96fba2012-03-13 13:27:23 +01001487 /* Clear pending error and receive interrupts */
1488 writew(UART011_OEIS | UART011_BEIS | UART011_PEIS | UART011_FEIS |
1489 UART011_RTIS | UART011_RXIS, uap->port.membase + UART011_ICR);
1490
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 /*
Anton Vorontsovb3564c22012-09-24 14:27:54 -07001492 * Save interrupts enable mask, and enable RX interrupts in case if
1493 * the interrupt is used for NMI entry.
1494 */
1495 uap->im = readw(uap->port.membase + UART011_IMSC);
1496 writew(UART011_RTIM | UART011_RXIM, uap->port.membase + UART011_IMSC);
1497
1498 if (uap->port.dev->platform_data) {
1499 struct amba_pl011_data *plat;
1500
1501 plat = uap->port.dev->platform_data;
1502 if (plat->init)
1503 plat->init();
1504 }
1505 return 0;
1506 out:
1507 return retval;
1508}
1509
1510static int pl011_startup(struct uart_port *port)
1511{
1512 struct uart_amba_port *uap = (struct uart_amba_port *)port;
1513 unsigned int cr;
1514 int retval;
1515
1516 retval = pl011_hwinit(port);
1517 if (retval)
1518 goto clk_dis;
1519
1520 writew(uap->im, uap->port.membase + UART011_IMSC);
1521
1522 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 * Allocate the IRQ
1524 */
1525 retval = request_irq(uap->port.irq, pl011_int, 0, "uart-pl011", uap);
1526 if (retval)
1527 goto clk_dis;
1528
Russell Kingc19f12b2010-12-22 17:48:26 +00001529 writew(uap->vendor->ifls, uap->port.membase + UART011_IFLS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530
1531 /*
1532 * Provoke TX FIFO interrupt into asserting.
1533 */
1534 cr = UART01x_CR_UARTEN | UART011_CR_TXE | UART011_CR_LBE;
1535 writew(cr, uap->port.membase + UART011_CR);
1536 writew(0, uap->port.membase + UART011_FBRD);
1537 writew(1, uap->port.membase + UART011_IBRD);
Linus Walleijec489aa2010-06-02 08:13:52 +01001538 writew(0, uap->port.membase + uap->lcrh_rx);
1539 if (uap->lcrh_tx != uap->lcrh_rx) {
1540 int i;
1541 /*
1542 * Wait 10 PCLKs before writing LCRH_TX register,
1543 * to get this delay write read only register 10 times
1544 */
1545 for (i = 0; i < 10; ++i)
1546 writew(0xff, uap->port.membase + UART011_MIS);
1547 writew(0, uap->port.membase + uap->lcrh_tx);
1548 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 writew(0, uap->port.membase + UART01x_DR);
1550 while (readw(uap->port.membase + UART01x_FR) & UART01x_FR_BUSY)
1551 barrier();
1552
Shreshtha Kumar Sahud8d8ffa2012-01-18 15:53:59 +05301553 /* restore RTS and DTR */
1554 cr = uap->old_cr & (UART011_CR_RTS | UART011_CR_DTR);
1555 cr |= UART01x_CR_UARTEN | UART011_CR_RXE | UART011_CR_TXE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 writew(cr, uap->port.membase + UART011_CR);
1557
1558 /*
1559 * initialise the old status of the modem signals
1560 */
1561 uap->old_status = readw(uap->port.membase + UART01x_FR) & UART01x_FR_MODEM_ANY;
1562
Russell King68b65f72010-12-22 17:24:39 +00001563 /* Startup DMA */
1564 pl011_dma_startup(uap);
1565
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 /*
Linus Walleijead76f32011-02-24 13:21:08 +01001567 * Finally, enable interrupts, only timeouts when using DMA
1568 * if initial RX DMA job failed, start in interrupt mode
1569 * as well.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 */
1571 spin_lock_irq(&uap->port.lock);
Linus Walleij9b96fba2012-03-13 13:27:23 +01001572 /* Clear out any spuriously appearing RX interrupts */
1573 writew(UART011_RTIS | UART011_RXIS,
1574 uap->port.membase + UART011_ICR);
Linus Walleijead76f32011-02-24 13:21:08 +01001575 uap->im = UART011_RTIM;
1576 if (!pl011_dma_rx_running(uap))
1577 uap->im |= UART011_RXIM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 writew(uap->im, uap->port.membase + UART011_IMSC);
1579 spin_unlock_irq(&uap->port.lock);
1580
1581 return 0;
1582
1583 clk_dis:
Julia Lawall1c4c4392012-08-26 18:01:01 +02001584 clk_disable_unprepare(uap->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 return retval;
1586}
1587
Linus Walleijec489aa2010-06-02 08:13:52 +01001588static void pl011_shutdown_channel(struct uart_amba_port *uap,
1589 unsigned int lcrh)
1590{
1591 unsigned long val;
1592
1593 val = readw(uap->port.membase + lcrh);
1594 val &= ~(UART01x_LCRH_BRK | UART01x_LCRH_FEN);
1595 writew(val, uap->port.membase + lcrh);
1596}
1597
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598static void pl011_shutdown(struct uart_port *port)
1599{
1600 struct uart_amba_port *uap = (struct uart_amba_port *)port;
Shreshtha Kumar Sahud8d8ffa2012-01-18 15:53:59 +05301601 unsigned int cr;
Linus Walleij78d80c52012-05-23 21:18:46 +02001602 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603
1604 /*
1605 * disable all interrupts
1606 */
1607 spin_lock_irq(&uap->port.lock);
1608 uap->im = 0;
1609 writew(uap->im, uap->port.membase + UART011_IMSC);
1610 writew(0xffff, uap->port.membase + UART011_ICR);
1611 spin_unlock_irq(&uap->port.lock);
1612
Russell King68b65f72010-12-22 17:24:39 +00001613 pl011_dma_shutdown(uap);
1614
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 /*
1616 * Free the interrupt
1617 */
1618 free_irq(uap->port.irq, uap);
1619
1620 /*
1621 * disable the port
Shreshtha Kumar Sahud8d8ffa2012-01-18 15:53:59 +05301622 * disable the port. It should not disable RTS and DTR.
1623 * Also RTS and DTR state should be preserved to restore
1624 * it during startup().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 */
Rabin Vincent3b438162010-02-12 06:43:11 +01001626 uap->autorts = false;
Shreshtha Kumar Sahud8d8ffa2012-01-18 15:53:59 +05301627 cr = readw(uap->port.membase + UART011_CR);
1628 uap->old_cr = cr;
1629 cr &= UART011_CR_RTS | UART011_CR_DTR;
1630 cr |= UART01x_CR_UARTEN | UART011_CR_TXE;
1631 writew(cr, uap->port.membase + UART011_CR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632
1633 /*
1634 * disable break condition and fifos
1635 */
Linus Walleijec489aa2010-06-02 08:13:52 +01001636 pl011_shutdown_channel(uap, uap->lcrh_rx);
1637 if (uap->lcrh_rx != uap->lcrh_tx)
1638 pl011_shutdown_channel(uap, uap->lcrh_tx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639
1640 /*
1641 * Shut down the clock producer
1642 */
Julia Lawall1c4c4392012-08-26 18:01:01 +02001643 clk_disable_unprepare(uap->clk);
Linus Walleij78d80c52012-05-23 21:18:46 +02001644 /* Optionally let pins go into sleep states */
1645 if (!IS_ERR(uap->pins_sleep)) {
1646 retval = pinctrl_select_state(uap->pinctrl, uap->pins_sleep);
1647 if (retval)
1648 dev_err(port->dev,
1649 "could not set pins to sleep state\n");
1650 }
1651
Shreshtha Kumar Sahuc16d51a2011-06-13 10:11:33 +02001652
1653 if (uap->port.dev->platform_data) {
1654 struct amba_pl011_data *plat;
1655
1656 plat = uap->port.dev->platform_data;
1657 if (plat->exit)
1658 plat->exit();
1659 }
1660
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661}
1662
1663static void
Alan Cox606d0992006-12-08 02:38:45 -08001664pl011_set_termios(struct uart_port *port, struct ktermios *termios,
1665 struct ktermios *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666{
Rabin Vincent3b438162010-02-12 06:43:11 +01001667 struct uart_amba_port *uap = (struct uart_amba_port *)port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 unsigned int lcr_h, old_cr;
1669 unsigned long flags;
Russell Kingc19f12b2010-12-22 17:48:26 +00001670 unsigned int baud, quot, clkdiv;
1671
1672 if (uap->vendor->oversampling)
1673 clkdiv = 8;
1674 else
1675 clkdiv = 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676
1677 /*
1678 * Ask the core to calculate the divisor for us.
1679 */
Linus Walleijac3e3fb2010-06-02 20:40:22 +01001680 baud = uart_get_baud_rate(port, termios, old, 0,
Russell Kingc19f12b2010-12-22 17:48:26 +00001681 port->uartclk / clkdiv);
Chanho Min89fa28d2013-04-03 11:10:37 +09001682#ifdef CONFIG_DMA_ENGINE
Chanho Mincb06ff12013-03-27 18:38:11 +09001683 /*
1684 * Adjust RX DMA polling rate with baud rate if not specified.
1685 */
1686 if (uap->dmarx.auto_poll_rate)
1687 uap->dmarx.poll_rate = DIV_ROUND_UP(10000000, baud);
Chanho Min89fa28d2013-04-03 11:10:37 +09001688#endif
Linus Walleijac3e3fb2010-06-02 20:40:22 +01001689
1690 if (baud > port->uartclk/16)
1691 quot = DIV_ROUND_CLOSEST(port->uartclk * 8, baud);
1692 else
1693 quot = DIV_ROUND_CLOSEST(port->uartclk * 4, baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694
1695 switch (termios->c_cflag & CSIZE) {
1696 case CS5:
1697 lcr_h = UART01x_LCRH_WLEN_5;
1698 break;
1699 case CS6:
1700 lcr_h = UART01x_LCRH_WLEN_6;
1701 break;
1702 case CS7:
1703 lcr_h = UART01x_LCRH_WLEN_7;
1704 break;
1705 default: // CS8
1706 lcr_h = UART01x_LCRH_WLEN_8;
1707 break;
1708 }
1709 if (termios->c_cflag & CSTOPB)
1710 lcr_h |= UART01x_LCRH_STP2;
1711 if (termios->c_cflag & PARENB) {
1712 lcr_h |= UART01x_LCRH_PEN;
1713 if (!(termios->c_cflag & PARODD))
1714 lcr_h |= UART01x_LCRH_EPS;
1715 }
Russell Kingffca2b12010-12-22 17:13:05 +00001716 if (uap->fifosize > 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 lcr_h |= UART01x_LCRH_FEN;
1718
1719 spin_lock_irqsave(&port->lock, flags);
1720
1721 /*
1722 * Update the per-port timeout.
1723 */
1724 uart_update_timeout(port, termios->c_cflag, baud);
1725
Russell Kingb63d4f02005-11-19 11:10:35 +00001726 port->read_status_mask = UART011_DR_OE | 255;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 if (termios->c_iflag & INPCK)
Russell Kingb63d4f02005-11-19 11:10:35 +00001728 port->read_status_mask |= UART011_DR_FE | UART011_DR_PE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 if (termios->c_iflag & (BRKINT | PARMRK))
Russell Kingb63d4f02005-11-19 11:10:35 +00001730 port->read_status_mask |= UART011_DR_BE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731
1732 /*
1733 * Characters to ignore
1734 */
1735 port->ignore_status_mask = 0;
1736 if (termios->c_iflag & IGNPAR)
Russell Kingb63d4f02005-11-19 11:10:35 +00001737 port->ignore_status_mask |= UART011_DR_FE | UART011_DR_PE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 if (termios->c_iflag & IGNBRK) {
Russell Kingb63d4f02005-11-19 11:10:35 +00001739 port->ignore_status_mask |= UART011_DR_BE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 /*
1741 * If we're ignoring parity and break indicators,
1742 * ignore overruns too (for real raw support).
1743 */
1744 if (termios->c_iflag & IGNPAR)
Russell Kingb63d4f02005-11-19 11:10:35 +00001745 port->ignore_status_mask |= UART011_DR_OE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 }
1747
1748 /*
1749 * Ignore all characters if CREAD is not set.
1750 */
1751 if ((termios->c_cflag & CREAD) == 0)
Russell Kingb63d4f02005-11-19 11:10:35 +00001752 port->ignore_status_mask |= UART_DUMMY_DR_RX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753
1754 if (UART_ENABLE_MS(port, termios->c_cflag))
1755 pl011_enable_ms(port);
1756
1757 /* first, disable everything */
1758 old_cr = readw(port->membase + UART011_CR);
1759 writew(0, port->membase + UART011_CR);
1760
Rabin Vincent3b438162010-02-12 06:43:11 +01001761 if (termios->c_cflag & CRTSCTS) {
1762 if (old_cr & UART011_CR_RTS)
1763 old_cr |= UART011_CR_RTSEN;
1764
1765 old_cr |= UART011_CR_CTSEN;
1766 uap->autorts = true;
1767 } else {
1768 old_cr &= ~(UART011_CR_CTSEN | UART011_CR_RTSEN);
1769 uap->autorts = false;
1770 }
1771
Russell Kingc19f12b2010-12-22 17:48:26 +00001772 if (uap->vendor->oversampling) {
1773 if (baud > port->uartclk / 16)
Linus Walleijac3e3fb2010-06-02 20:40:22 +01001774 old_cr |= ST_UART011_CR_OVSFACT;
1775 else
1776 old_cr &= ~ST_UART011_CR_OVSFACT;
1777 }
1778
Linus Walleijc5dd5532012-09-26 17:21:36 +02001779 /*
1780 * Workaround for the ST Micro oversampling variants to
1781 * increase the bitrate slightly, by lowering the divisor,
1782 * to avoid delayed sampling of start bit at high speeds,
1783 * else we see data corruption.
1784 */
1785 if (uap->vendor->oversampling) {
1786 if ((baud >= 3000000) && (baud < 3250000) && (quot > 1))
1787 quot -= 1;
1788 else if ((baud > 3250000) && (quot > 2))
1789 quot -= 2;
1790 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 /* Set baud rate */
1792 writew(quot & 0x3f, port->membase + UART011_FBRD);
1793 writew(quot >> 6, port->membase + UART011_IBRD);
1794
1795 /*
1796 * ----------v----------v----------v----------v-----
Linus Walleijc5dd5532012-09-26 17:21:36 +02001797 * NOTE: lcrh_tx and lcrh_rx MUST BE WRITTEN AFTER
1798 * UART011_FBRD & UART011_IBRD.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 * ----------^----------^----------^----------^-----
1800 */
Linus Walleijec489aa2010-06-02 08:13:52 +01001801 writew(lcr_h, port->membase + uap->lcrh_rx);
1802 if (uap->lcrh_rx != uap->lcrh_tx) {
1803 int i;
1804 /*
1805 * Wait 10 PCLKs before writing LCRH_TX register,
1806 * to get this delay write read only register 10 times
1807 */
1808 for (i = 0; i < 10; ++i)
1809 writew(0xff, uap->port.membase + UART011_MIS);
1810 writew(lcr_h, port->membase + uap->lcrh_tx);
1811 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 writew(old_cr, port->membase + UART011_CR);
1813
1814 spin_unlock_irqrestore(&port->lock, flags);
1815}
1816
1817static const char *pl011_type(struct uart_port *port)
1818{
Russell Kinge8a7ba82010-12-28 09:16:54 +00001819 struct uart_amba_port *uap = (struct uart_amba_port *)port;
1820 return uap->port.type == PORT_AMBA ? uap->type : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821}
1822
1823/*
1824 * Release the memory region(s) being used by 'port'
1825 */
Linus Walleije643f872012-06-17 15:44:19 +02001826static void pl011_release_port(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827{
1828 release_mem_region(port->mapbase, SZ_4K);
1829}
1830
1831/*
1832 * Request the memory region(s) being used by 'port'
1833 */
Linus Walleije643f872012-06-17 15:44:19 +02001834static int pl011_request_port(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835{
1836 return request_mem_region(port->mapbase, SZ_4K, "uart-pl011")
1837 != NULL ? 0 : -EBUSY;
1838}
1839
1840/*
1841 * Configure/autoconfigure the port.
1842 */
Linus Walleije643f872012-06-17 15:44:19 +02001843static void pl011_config_port(struct uart_port *port, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844{
1845 if (flags & UART_CONFIG_TYPE) {
1846 port->type = PORT_AMBA;
Linus Walleije643f872012-06-17 15:44:19 +02001847 pl011_request_port(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 }
1849}
1850
1851/*
1852 * verify the new serial_struct (for TIOCSSERIAL).
1853 */
Linus Walleije643f872012-06-17 15:44:19 +02001854static int pl011_verify_port(struct uart_port *port, struct serial_struct *ser)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855{
1856 int ret = 0;
1857 if (ser->type != PORT_UNKNOWN && ser->type != PORT_AMBA)
1858 ret = -EINVAL;
Yinghai Lua62c4132008-08-19 20:49:55 -07001859 if (ser->irq < 0 || ser->irq >= nr_irqs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 ret = -EINVAL;
1861 if (ser->baud_base < 9600)
1862 ret = -EINVAL;
1863 return ret;
1864}
1865
1866static struct uart_ops amba_pl011_pops = {
Linus Walleije643f872012-06-17 15:44:19 +02001867 .tx_empty = pl011_tx_empty,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 .set_mctrl = pl011_set_mctrl,
Linus Walleije643f872012-06-17 15:44:19 +02001869 .get_mctrl = pl011_get_mctrl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 .stop_tx = pl011_stop_tx,
1871 .start_tx = pl011_start_tx,
1872 .stop_rx = pl011_stop_rx,
1873 .enable_ms = pl011_enable_ms,
1874 .break_ctl = pl011_break_ctl,
1875 .startup = pl011_startup,
1876 .shutdown = pl011_shutdown,
Russell King68b65f72010-12-22 17:24:39 +00001877 .flush_buffer = pl011_dma_flush_buffer,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 .set_termios = pl011_set_termios,
1879 .type = pl011_type,
Linus Walleije643f872012-06-17 15:44:19 +02001880 .release_port = pl011_release_port,
1881 .request_port = pl011_request_port,
1882 .config_port = pl011_config_port,
1883 .verify_port = pl011_verify_port,
Jason Wessel84b5ae12008-02-20 13:33:39 -06001884#ifdef CONFIG_CONSOLE_POLL
Anton Vorontsovb3564c22012-09-24 14:27:54 -07001885 .poll_init = pl011_hwinit,
Linus Walleije643f872012-06-17 15:44:19 +02001886 .poll_get_char = pl011_get_poll_char,
1887 .poll_put_char = pl011_put_poll_char,
Jason Wessel84b5ae12008-02-20 13:33:39 -06001888#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889};
1890
1891static struct uart_amba_port *amba_ports[UART_NR];
1892
1893#ifdef CONFIG_SERIAL_AMBA_PL011_CONSOLE
1894
Russell Kingd3587882006-03-20 20:00:09 +00001895static void pl011_console_putchar(struct uart_port *port, int ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896{
Russell Kingd3587882006-03-20 20:00:09 +00001897 struct uart_amba_port *uap = (struct uart_amba_port *)port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898
Russell Kingd3587882006-03-20 20:00:09 +00001899 while (readw(uap->port.membase + UART01x_FR) & UART01x_FR_TXFF)
1900 barrier();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 writew(ch, uap->port.membase + UART01x_DR);
1902}
1903
1904static void
1905pl011_console_write(struct console *co, const char *s, unsigned int count)
1906{
1907 struct uart_amba_port *uap = amba_ports[co->index];
1908 unsigned int status, old_cr, new_cr;
Rabin Vincentef605fd2012-01-17 11:52:28 +01001909 unsigned long flags;
1910 int locked = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911
1912 clk_enable(uap->clk);
1913
Rabin Vincentef605fd2012-01-17 11:52:28 +01001914 local_irq_save(flags);
1915 if (uap->port.sysrq)
1916 locked = 0;
1917 else if (oops_in_progress)
1918 locked = spin_trylock(&uap->port.lock);
1919 else
1920 spin_lock(&uap->port.lock);
1921
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 /*
1923 * First save the CR then disable the interrupts
1924 */
1925 old_cr = readw(uap->port.membase + UART011_CR);
1926 new_cr = old_cr & ~UART011_CR_CTSEN;
1927 new_cr |= UART01x_CR_UARTEN | UART011_CR_TXE;
1928 writew(new_cr, uap->port.membase + UART011_CR);
1929
Russell Kingd3587882006-03-20 20:00:09 +00001930 uart_console_write(&uap->port, s, count, pl011_console_putchar);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931
1932 /*
1933 * Finally, wait for transmitter to become empty
1934 * and restore the TCR
1935 */
1936 do {
1937 status = readw(uap->port.membase + UART01x_FR);
1938 } while (status & UART01x_FR_BUSY);
1939 writew(old_cr, uap->port.membase + UART011_CR);
1940
Rabin Vincentef605fd2012-01-17 11:52:28 +01001941 if (locked)
1942 spin_unlock(&uap->port.lock);
1943 local_irq_restore(flags);
1944
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 clk_disable(uap->clk);
1946}
1947
1948static void __init
1949pl011_console_get_options(struct uart_amba_port *uap, int *baud,
1950 int *parity, int *bits)
1951{
1952 if (readw(uap->port.membase + UART011_CR) & UART01x_CR_UARTEN) {
1953 unsigned int lcr_h, ibrd, fbrd;
1954
Linus Walleijec489aa2010-06-02 08:13:52 +01001955 lcr_h = readw(uap->port.membase + uap->lcrh_tx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956
1957 *parity = 'n';
1958 if (lcr_h & UART01x_LCRH_PEN) {
1959 if (lcr_h & UART01x_LCRH_EPS)
1960 *parity = 'e';
1961 else
1962 *parity = 'o';
1963 }
1964
1965 if ((lcr_h & 0x60) == UART01x_LCRH_WLEN_7)
1966 *bits = 7;
1967 else
1968 *bits = 8;
1969
1970 ibrd = readw(uap->port.membase + UART011_IBRD);
1971 fbrd = readw(uap->port.membase + UART011_FBRD);
1972
1973 *baud = uap->port.uartclk * 4 / (64 * ibrd + fbrd);
Linus Walleijac3e3fb2010-06-02 20:40:22 +01001974
Russell Kingc19f12b2010-12-22 17:48:26 +00001975 if (uap->vendor->oversampling) {
Linus Walleijac3e3fb2010-06-02 20:40:22 +01001976 if (readw(uap->port.membase + UART011_CR)
1977 & ST_UART011_CR_OVSFACT)
1978 *baud *= 2;
1979 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 }
1981}
1982
1983static int __init pl011_console_setup(struct console *co, char *options)
1984{
1985 struct uart_amba_port *uap;
1986 int baud = 38400;
1987 int bits = 8;
1988 int parity = 'n';
1989 int flow = 'n';
Russell King4b4851c2011-09-22 11:35:30 +01001990 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991
1992 /*
1993 * Check whether an invalid uart number has been specified, and
1994 * if so, search for the first available port that does have
1995 * console support.
1996 */
1997 if (co->index >= UART_NR)
1998 co->index = 0;
1999 uap = amba_ports[co->index];
Russell Kingd28122a2007-01-22 18:59:42 +00002000 if (!uap)
2001 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002
Linus Walleij78d80c52012-05-23 21:18:46 +02002003 /* Allow pins to be muxed in and configured */
2004 if (!IS_ERR(uap->pins_default)) {
2005 ret = pinctrl_select_state(uap->pinctrl, uap->pins_default);
2006 if (ret)
2007 dev_err(uap->port.dev,
2008 "could not set default pins\n");
2009 }
2010
Russell King4b4851c2011-09-22 11:35:30 +01002011 ret = clk_prepare(uap->clk);
2012 if (ret)
2013 return ret;
2014
Shreshtha Kumar Sahuc16d51a2011-06-13 10:11:33 +02002015 if (uap->port.dev->platform_data) {
2016 struct amba_pl011_data *plat;
2017
2018 plat = uap->port.dev->platform_data;
2019 if (plat->init)
2020 plat->init();
2021 }
2022
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 uap->port.uartclk = clk_get_rate(uap->clk);
2024
2025 if (options)
2026 uart_parse_options(options, &baud, &parity, &bits, &flow);
2027 else
2028 pl011_console_get_options(uap, &baud, &parity, &bits);
2029
2030 return uart_set_options(&uap->port, co, baud, parity, bits, flow);
2031}
2032
Vincent Sanders2d934862005-09-14 22:36:03 +01002033static struct uart_driver amba_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034static struct console amba_console = {
2035 .name = "ttyAMA",
2036 .write = pl011_console_write,
2037 .device = uart_console_device,
2038 .setup = pl011_console_setup,
2039 .flags = CON_PRINTBUFFER,
2040 .index = -1,
2041 .data = &amba_reg,
2042};
2043
2044#define AMBA_CONSOLE (&amba_console)
2045#else
2046#define AMBA_CONSOLE NULL
2047#endif
2048
2049static struct uart_driver amba_reg = {
2050 .owner = THIS_MODULE,
2051 .driver_name = "ttyAMA",
2052 .dev_name = "ttyAMA",
2053 .major = SERIAL_AMBA_MAJOR,
2054 .minor = SERIAL_AMBA_MINOR,
2055 .nr = UART_NR,
2056 .cons = AMBA_CONSOLE,
2057};
2058
Matthew Leach32614aa2012-08-28 16:41:28 +01002059static int pl011_probe_dt_alias(int index, struct device *dev)
2060{
2061 struct device_node *np;
2062 static bool seen_dev_with_alias = false;
2063 static bool seen_dev_without_alias = false;
2064 int ret = index;
2065
2066 if (!IS_ENABLED(CONFIG_OF))
2067 return ret;
2068
2069 np = dev->of_node;
2070 if (!np)
2071 return ret;
2072
2073 ret = of_alias_get_id(np, "serial");
2074 if (IS_ERR_VALUE(ret)) {
2075 seen_dev_without_alias = true;
2076 ret = index;
2077 } else {
2078 seen_dev_with_alias = true;
2079 if (ret >= ARRAY_SIZE(amba_ports) || amba_ports[ret] != NULL) {
2080 dev_warn(dev, "requested serial port %d not available.\n", ret);
2081 ret = index;
2082 }
2083 }
2084
2085 if (seen_dev_with_alias && seen_dev_without_alias)
2086 dev_warn(dev, "aliased and non-aliased serial devices found in device tree. Serial port enumeration may be unpredictable.\n");
2087
2088 return ret;
2089}
2090
Russell Kingaa25afa2011-02-19 15:55:00 +00002091static int pl011_probe(struct amba_device *dev, const struct amba_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092{
2093 struct uart_amba_port *uap;
Alessandro Rubini5926a292009-06-04 17:43:04 +01002094 struct vendor_data *vendor = id->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 void __iomem *base;
2096 int i, ret;
2097
2098 for (i = 0; i < ARRAY_SIZE(amba_ports); i++)
2099 if (amba_ports[i] == NULL)
2100 break;
2101
2102 if (i == ARRAY_SIZE(amba_ports)) {
2103 ret = -EBUSY;
2104 goto out;
2105 }
2106
Linus Walleijde609582012-10-15 13:36:01 +02002107 uap = devm_kzalloc(&dev->dev, sizeof(struct uart_amba_port),
2108 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 if (uap == NULL) {
2110 ret = -ENOMEM;
2111 goto out;
2112 }
2113
Matthew Leach32614aa2012-08-28 16:41:28 +01002114 i = pl011_probe_dt_alias(i, &dev->dev);
2115
Linus Walleijde609582012-10-15 13:36:01 +02002116 base = devm_ioremap(&dev->dev, dev->res.start,
2117 resource_size(&dev->res));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 if (!base) {
2119 ret = -ENOMEM;
Linus Walleijde609582012-10-15 13:36:01 +02002120 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 }
2122
Linus Walleij78d80c52012-05-23 21:18:46 +02002123 uap->pinctrl = devm_pinctrl_get(&dev->dev);
2124 if (IS_ERR(uap->pinctrl)) {
2125 ret = PTR_ERR(uap->pinctrl);
Linus Walleijde609582012-10-15 13:36:01 +02002126 goto out;
Shawn Guo258e0552012-05-06 22:53:35 +08002127 }
Linus Walleij78d80c52012-05-23 21:18:46 +02002128 uap->pins_default = pinctrl_lookup_state(uap->pinctrl,
2129 PINCTRL_STATE_DEFAULT);
2130 if (IS_ERR(uap->pins_default))
2131 dev_err(&dev->dev, "could not get default pinstate\n");
2132
2133 uap->pins_sleep = pinctrl_lookup_state(uap->pinctrl,
2134 PINCTRL_STATE_SLEEP);
2135 if (IS_ERR(uap->pins_sleep))
2136 dev_dbg(&dev->dev, "could not get sleep pinstate\n");
Shawn Guo258e0552012-05-06 22:53:35 +08002137
Linus Walleijde609582012-10-15 13:36:01 +02002138 uap->clk = devm_clk_get(&dev->dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 if (IS_ERR(uap->clk)) {
2140 ret = PTR_ERR(uap->clk);
Linus Walleijde609582012-10-15 13:36:01 +02002141 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 }
2143
Russell Kingc19f12b2010-12-22 17:48:26 +00002144 uap->vendor = vendor;
Linus Walleijec489aa2010-06-02 08:13:52 +01002145 uap->lcrh_rx = vendor->lcrh_rx;
2146 uap->lcrh_tx = vendor->lcrh_tx;
Shreshtha Kumar Sahud8d8ffa2012-01-18 15:53:59 +05302147 uap->old_cr = 0;
Jongsung Kim78506f22013-04-15 14:45:25 +09002148 uap->fifosize = vendor->get_fifosize(dev->periphid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 uap->port.dev = &dev->dev;
2150 uap->port.mapbase = dev->res.start;
2151 uap->port.membase = base;
2152 uap->port.iotype = UPIO_MEM;
2153 uap->port.irq = dev->irq[0];
Russell Kingffca2b12010-12-22 17:13:05 +00002154 uap->port.fifosize = uap->fifosize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 uap->port.ops = &amba_pl011_pops;
2156 uap->port.flags = UPF_BOOT_AUTOCONF;
2157 uap->port.line = i;
Russell King68b65f72010-12-22 17:24:39 +00002158 pl011_dma_probe(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159
Linus Walleijc3d8b762012-03-21 20:15:18 +01002160 /* Ensure interrupts from this UART are masked and cleared */
2161 writew(0, uap->port.membase + UART011_IMSC);
2162 writew(0xffff, uap->port.membase + UART011_ICR);
2163
Russell Kinge8a7ba82010-12-28 09:16:54 +00002164 snprintf(uap->type, sizeof(uap->type), "PL011 rev%u", amba_rev(dev));
2165
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 amba_ports[i] = uap;
2167
2168 amba_set_drvdata(dev, uap);
2169 ret = uart_add_one_port(&amba_reg, &uap->port);
2170 if (ret) {
2171 amba_set_drvdata(dev, NULL);
2172 amba_ports[i] = NULL;
Russell King68b65f72010-12-22 17:24:39 +00002173 pl011_dma_remove(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 }
2175 out:
2176 return ret;
2177}
2178
2179static int pl011_remove(struct amba_device *dev)
2180{
2181 struct uart_amba_port *uap = amba_get_drvdata(dev);
2182 int i;
2183
2184 amba_set_drvdata(dev, NULL);
2185
2186 uart_remove_one_port(&amba_reg, &uap->port);
2187
2188 for (i = 0; i < ARRAY_SIZE(amba_ports); i++)
2189 if (amba_ports[i] == uap)
2190 amba_ports[i] = NULL;
2191
Russell King68b65f72010-12-22 17:24:39 +00002192 pl011_dma_remove(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 return 0;
2194}
2195
Leo Chenb736b892009-07-28 23:43:33 +01002196#ifdef CONFIG_PM
2197static int pl011_suspend(struct amba_device *dev, pm_message_t state)
2198{
2199 struct uart_amba_port *uap = amba_get_drvdata(dev);
2200
2201 if (!uap)
2202 return -EINVAL;
2203
2204 return uart_suspend_port(&amba_reg, &uap->port);
2205}
2206
2207static int pl011_resume(struct amba_device *dev)
2208{
2209 struct uart_amba_port *uap = amba_get_drvdata(dev);
2210
2211 if (!uap)
2212 return -EINVAL;
2213
2214 return uart_resume_port(&amba_reg, &uap->port);
2215}
2216#endif
2217
Russell King2c39c9e2010-07-27 08:50:16 +01002218static struct amba_id pl011_ids[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 {
2220 .id = 0x00041011,
2221 .mask = 0x000fffff,
Alessandro Rubini5926a292009-06-04 17:43:04 +01002222 .data = &vendor_arm,
2223 },
2224 {
2225 .id = 0x00380802,
2226 .mask = 0x00ffffff,
2227 .data = &vendor_st,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 },
2229 { 0, 0 },
2230};
2231
Dave Martin60f7a332011-10-05 15:15:22 +01002232MODULE_DEVICE_TABLE(amba, pl011_ids);
2233
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234static struct amba_driver pl011_driver = {
2235 .drv = {
2236 .name = "uart-pl011",
2237 },
2238 .id_table = pl011_ids,
2239 .probe = pl011_probe,
2240 .remove = pl011_remove,
Leo Chenb736b892009-07-28 23:43:33 +01002241#ifdef CONFIG_PM
2242 .suspend = pl011_suspend,
2243 .resume = pl011_resume,
2244#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245};
2246
2247static int __init pl011_init(void)
2248{
2249 int ret;
2250 printk(KERN_INFO "Serial: AMBA PL011 UART driver\n");
2251
2252 ret = uart_register_driver(&amba_reg);
2253 if (ret == 0) {
2254 ret = amba_driver_register(&pl011_driver);
2255 if (ret)
2256 uart_unregister_driver(&amba_reg);
2257 }
2258 return ret;
2259}
2260
2261static void __exit pl011_exit(void)
2262{
2263 amba_driver_unregister(&pl011_driver);
2264 uart_unregister_driver(&amba_reg);
2265}
2266
Alessandro Rubini4dd9e742009-05-05 05:54:13 +01002267/*
2268 * While this can be a module, if builtin it's most likely the console
2269 * So let's leave module_exit but move module_init to an earlier place
2270 */
2271arch_initcall(pl011_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272module_exit(pl011_exit);
2273
2274MODULE_AUTHOR("ARM Ltd/Deep Blue Solutions Ltd");
2275MODULE_DESCRIPTION("ARM AMBA serial port driver");
2276MODULE_LICENSE("GPL");