blob: 85cfe116f423c00990cc5dd397ac87e564f82f81 [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
Jongsung Kimea336402013-05-10 18:05:35 +090082 unsigned int (*get_fifosize)(struct amba_device *dev);
Alessandro Rubini5926a292009-06-04 17:43:04 +010083};
84
Jongsung Kimea336402013-05-10 18:05:35 +090085static unsigned int get_fifosize_arm(struct amba_device *dev)
Jongsung Kim78506f22013-04-15 14:45:25 +090086{
Jongsung Kimea336402013-05-10 18:05:35 +090087 return amba_rev(dev) < 3 ? 16 : 32;
Jongsung Kim78506f22013-04-15 14:45:25 +090088}
89
Alessandro Rubini5926a292009-06-04 17:43:04 +010090static struct vendor_data vendor_arm = {
91 .ifls = UART011_IFLS_RX4_8|UART011_IFLS_TX4_8,
Linus Walleijec489aa2010-06-02 08:13:52 +010092 .lcrh_tx = UART011_LCRH,
93 .lcrh_rx = UART011_LCRH,
Linus Walleijac3e3fb2010-06-02 20:40:22 +010094 .oversampling = false,
Russell King38d62432010-12-22 17:59:16 +000095 .dma_threshold = false,
Rajanikanth H.V4fd06902012-03-26 11:17:02 +020096 .cts_event_workaround = false,
Jongsung Kim78506f22013-04-15 14:45:25 +090097 .get_fifosize = get_fifosize_arm,
Alessandro Rubini5926a292009-06-04 17:43:04 +010098};
99
Jongsung Kimea336402013-05-10 18:05:35 +0900100static unsigned int get_fifosize_st(struct amba_device *dev)
Jongsung Kim78506f22013-04-15 14:45:25 +0900101{
102 return 64;
103}
104
Alessandro Rubini5926a292009-06-04 17:43:04 +0100105static struct vendor_data vendor_st = {
106 .ifls = UART011_IFLS_RX_HALF|UART011_IFLS_TX_HALF,
Linus Walleijec489aa2010-06-02 08:13:52 +0100107 .lcrh_tx = ST_UART011_LCRH_TX,
108 .lcrh_rx = ST_UART011_LCRH_RX,
Linus Walleijac3e3fb2010-06-02 20:40:22 +0100109 .oversampling = true,
Russell King38d62432010-12-22 17:59:16 +0000110 .dma_threshold = true,
Rajanikanth H.V4fd06902012-03-26 11:17:02 +0200111 .cts_event_workaround = true,
Jongsung Kim78506f22013-04-15 14:45:25 +0900112 .get_fifosize = get_fifosize_st,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113};
114
Shreshtha Kumar Sahuc16d51a2011-06-13 10:11:33 +0200115static struct uart_amba_port *amba_ports[UART_NR];
116
Russell King68b65f72010-12-22 17:24:39 +0000117/* Deals with DMA transactions */
Linus Walleijead76f32011-02-24 13:21:08 +0100118
119struct pl011_sgbuf {
120 struct scatterlist sg;
121 char *buf;
122};
123
124struct pl011_dmarx_data {
125 struct dma_chan *chan;
126 struct completion complete;
127 bool use_buf_b;
128 struct pl011_sgbuf sgbuf_a;
129 struct pl011_sgbuf sgbuf_b;
130 dma_cookie_t cookie;
131 bool running;
Chanho Mincb06ff12013-03-27 18:38:11 +0900132 struct timer_list timer;
133 unsigned int last_residue;
134 unsigned long last_jiffies;
135 bool auto_poll_rate;
136 unsigned int poll_rate;
137 unsigned int poll_timeout;
Linus Walleijead76f32011-02-24 13:21:08 +0100138};
139
Russell King68b65f72010-12-22 17:24:39 +0000140struct pl011_dmatx_data {
141 struct dma_chan *chan;
142 struct scatterlist sg;
143 char *buf;
144 bool queued;
145};
146
Russell Kingc19f12b2010-12-22 17:48:26 +0000147/*
148 * We wrap our port structure around the generic uart_port.
149 */
150struct uart_amba_port {
151 struct uart_port port;
152 struct clk *clk;
Linus Walleij78d80c52012-05-23 21:18:46 +0200153 /* Two optional pin states - default & sleep */
154 struct pinctrl *pinctrl;
155 struct pinctrl_state *pins_default;
156 struct pinctrl_state *pins_sleep;
Russell Kingc19f12b2010-12-22 17:48:26 +0000157 const struct vendor_data *vendor;
Russell King68b65f72010-12-22 17:24:39 +0000158 unsigned int dmacr; /* dma control reg */
Russell Kingc19f12b2010-12-22 17:48:26 +0000159 unsigned int im; /* interrupt mask */
160 unsigned int old_status;
Russell Kingffca2b12010-12-22 17:13:05 +0000161 unsigned int fifosize; /* vendor-specific */
Russell Kingc19f12b2010-12-22 17:48:26 +0000162 unsigned int lcrh_tx; /* vendor-specific */
163 unsigned int lcrh_rx; /* vendor-specific */
Shreshtha Kumar Sahud8d8ffa2012-01-18 15:53:59 +0530164 unsigned int old_cr; /* state during shutdown */
Russell Kingc19f12b2010-12-22 17:48:26 +0000165 bool autorts;
166 char type[12];
Russell King68b65f72010-12-22 17:24:39 +0000167#ifdef CONFIG_DMA_ENGINE
168 /* DMA stuff */
Linus Walleijead76f32011-02-24 13:21:08 +0100169 bool using_tx_dma;
170 bool using_rx_dma;
171 struct pl011_dmarx_data dmarx;
Russell King68b65f72010-12-22 17:24:39 +0000172 struct pl011_dmatx_data dmatx;
173#endif
Russell Kingc19f12b2010-12-22 17:48:26 +0000174};
175
Russell King68b65f72010-12-22 17:24:39 +0000176/*
Linus Walleij29772c42011-02-24 13:21:36 +0100177 * Reads up to 256 characters from the FIFO or until it's empty and
178 * inserts them into the TTY layer. Returns the number of characters
179 * read from the FIFO.
180 */
181static int pl011_fifo_to_tty(struct uart_amba_port *uap)
182{
183 u16 status, ch;
184 unsigned int flag, max_count = 256;
185 int fifotaken = 0;
186
187 while (max_count--) {
188 status = readw(uap->port.membase + UART01x_FR);
189 if (status & UART01x_FR_RXFE)
190 break;
191
192 /* Take chars from the FIFO and update status */
193 ch = readw(uap->port.membase + UART01x_DR) |
194 UART_DUMMY_DR_RX;
195 flag = TTY_NORMAL;
196 uap->port.icount.rx++;
197 fifotaken++;
198
199 if (unlikely(ch & UART_DR_ERROR)) {
200 if (ch & UART011_DR_BE) {
201 ch &= ~(UART011_DR_FE | UART011_DR_PE);
202 uap->port.icount.brk++;
203 if (uart_handle_break(&uap->port))
204 continue;
205 } else if (ch & UART011_DR_PE)
206 uap->port.icount.parity++;
207 else if (ch & UART011_DR_FE)
208 uap->port.icount.frame++;
209 if (ch & UART011_DR_OE)
210 uap->port.icount.overrun++;
211
212 ch &= uap->port.read_status_mask;
213
214 if (ch & UART011_DR_BE)
215 flag = TTY_BREAK;
216 else if (ch & UART011_DR_PE)
217 flag = TTY_PARITY;
218 else if (ch & UART011_DR_FE)
219 flag = TTY_FRAME;
220 }
221
222 if (uart_handle_sysrq_char(&uap->port, ch & 255))
223 continue;
224
225 uart_insert_char(&uap->port, ch, UART011_DR_OE, ch, flag);
226 }
227
228 return fifotaken;
229}
230
231
232/*
Russell King68b65f72010-12-22 17:24:39 +0000233 * All the DMA operation mode stuff goes inside this ifdef.
234 * This assumes that you have a generic DMA device interface,
235 * no custom DMA interfaces are supported.
236 */
237#ifdef CONFIG_DMA_ENGINE
238
239#define PL011_DMA_BUFFER_SIZE PAGE_SIZE
240
Linus Walleijead76f32011-02-24 13:21:08 +0100241static int pl011_sgbuf_init(struct dma_chan *chan, struct pl011_sgbuf *sg,
242 enum dma_data_direction dir)
243{
Chanho Mincb06ff12013-03-27 18:38:11 +0900244 dma_addr_t dma_addr;
245
246 sg->buf = dma_alloc_coherent(chan->device->dev,
247 PL011_DMA_BUFFER_SIZE, &dma_addr, GFP_KERNEL);
Linus Walleijead76f32011-02-24 13:21:08 +0100248 if (!sg->buf)
249 return -ENOMEM;
250
Chanho Mincb06ff12013-03-27 18:38:11 +0900251 sg_init_table(&sg->sg, 1);
252 sg_set_page(&sg->sg, phys_to_page(dma_addr),
253 PL011_DMA_BUFFER_SIZE, offset_in_page(dma_addr));
254 sg_dma_address(&sg->sg) = dma_addr;
Linus Walleijead76f32011-02-24 13:21:08 +0100255
Linus Walleijead76f32011-02-24 13:21:08 +0100256 return 0;
257}
258
259static void pl011_sgbuf_free(struct dma_chan *chan, struct pl011_sgbuf *sg,
260 enum dma_data_direction dir)
261{
262 if (sg->buf) {
Chanho Mincb06ff12013-03-27 18:38:11 +0900263 dma_free_coherent(chan->device->dev,
264 PL011_DMA_BUFFER_SIZE, sg->buf,
265 sg_dma_address(&sg->sg));
Linus Walleijead76f32011-02-24 13:21:08 +0100266 }
267}
268
Arnd Bergmann787b0c12013-01-28 16:24:37 +0000269static void pl011_dma_probe_initcall(struct device *dev, struct uart_amba_port *uap)
Russell King68b65f72010-12-22 17:24:39 +0000270{
271 /* DMA is the sole user of the platform data right now */
272 struct amba_pl011_data *plat = uap->port.dev->platform_data;
273 struct dma_slave_config tx_conf = {
274 .dst_addr = uap->port.mapbase + UART01x_DR,
275 .dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE,
Vinod Koula485df42011-10-14 10:47:38 +0530276 .direction = DMA_MEM_TO_DEV,
Russell King68b65f72010-12-22 17:24:39 +0000277 .dst_maxburst = uap->fifosize >> 1,
Viresh Kumar258aea72012-02-01 16:12:19 +0530278 .device_fc = false,
Russell King68b65f72010-12-22 17:24:39 +0000279 };
280 struct dma_chan *chan;
281 dma_cap_mask_t mask;
282
Arnd Bergmann787b0c12013-01-28 16:24:37 +0000283 chan = dma_request_slave_channel(dev, "tx");
Russell King68b65f72010-12-22 17:24:39 +0000284
Russell King68b65f72010-12-22 17:24:39 +0000285 if (!chan) {
Arnd Bergmann787b0c12013-01-28 16:24:37 +0000286 /* We need platform data */
287 if (!plat || !plat->dma_filter) {
288 dev_info(uap->port.dev, "no DMA platform data\n");
289 return;
290 }
291
292 /* Try to acquire a generic DMA engine slave TX channel */
293 dma_cap_zero(mask);
294 dma_cap_set(DMA_SLAVE, mask);
295
296 chan = dma_request_channel(mask, plat->dma_filter,
297 plat->dma_tx_param);
298 if (!chan) {
299 dev_err(uap->port.dev, "no TX DMA channel!\n");
300 return;
301 }
Russell King68b65f72010-12-22 17:24:39 +0000302 }
303
304 dmaengine_slave_config(chan, &tx_conf);
305 uap->dmatx.chan = chan;
306
307 dev_info(uap->port.dev, "DMA channel TX %s\n",
308 dma_chan_name(uap->dmatx.chan));
Linus Walleijead76f32011-02-24 13:21:08 +0100309
310 /* Optionally make use of an RX channel as well */
Arnd Bergmann787b0c12013-01-28 16:24:37 +0000311 chan = dma_request_slave_channel(dev, "rx");
312
313 if (!chan && plat->dma_rx_param) {
314 chan = dma_request_channel(mask, plat->dma_filter, plat->dma_rx_param);
315
316 if (!chan) {
317 dev_err(uap->port.dev, "no RX DMA channel!\n");
318 return;
319 }
320 }
321
322 if (chan) {
Linus Walleijead76f32011-02-24 13:21:08 +0100323 struct dma_slave_config rx_conf = {
324 .src_addr = uap->port.mapbase + UART01x_DR,
325 .src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE,
Vinod Koula485df42011-10-14 10:47:38 +0530326 .direction = DMA_DEV_TO_MEM,
Linus Walleijead76f32011-02-24 13:21:08 +0100327 .src_maxburst = uap->fifosize >> 1,
Viresh Kumar258aea72012-02-01 16:12:19 +0530328 .device_fc = false,
Linus Walleijead76f32011-02-24 13:21:08 +0100329 };
330
Linus Walleijead76f32011-02-24 13:21:08 +0100331 dmaengine_slave_config(chan, &rx_conf);
332 uap->dmarx.chan = chan;
333
Chanho Mincb06ff12013-03-27 18:38:11 +0900334 if (plat->dma_rx_poll_enable) {
335 /* Set poll rate if specified. */
336 if (plat->dma_rx_poll_rate) {
337 uap->dmarx.auto_poll_rate = false;
338 uap->dmarx.poll_rate = plat->dma_rx_poll_rate;
339 } else {
340 /*
341 * 100 ms defaults to poll rate if not
342 * specified. This will be adjusted with
343 * the baud rate at set_termios.
344 */
345 uap->dmarx.auto_poll_rate = true;
346 uap->dmarx.poll_rate = 100;
347 }
348 /* 3 secs defaults poll_timeout if not specified. */
349 if (plat->dma_rx_poll_timeout)
350 uap->dmarx.poll_timeout =
351 plat->dma_rx_poll_timeout;
352 else
353 uap->dmarx.poll_timeout = 3000;
354 } else
355 uap->dmarx.auto_poll_rate = false;
356
Linus Walleijead76f32011-02-24 13:21:08 +0100357 dev_info(uap->port.dev, "DMA channel RX %s\n",
358 dma_chan_name(uap->dmarx.chan));
359 }
Russell King68b65f72010-12-22 17:24:39 +0000360}
361
362#ifndef MODULE
363/*
364 * Stack up the UARTs and let the above initcall be done at device
365 * initcall time, because the serial driver is called as an arch
366 * initcall, and at this time the DMA subsystem is not yet registered.
367 * At this point the driver will switch over to using DMA where desired.
368 */
369struct dma_uap {
370 struct list_head node;
371 struct uart_amba_port *uap;
Arnd Bergmann787b0c12013-01-28 16:24:37 +0000372 struct device *dev;
Russell King68b65f72010-12-22 17:24:39 +0000373};
374
375static LIST_HEAD(pl011_dma_uarts);
376
377static int __init pl011_dma_initcall(void)
378{
379 struct list_head *node, *tmp;
380
381 list_for_each_safe(node, tmp, &pl011_dma_uarts) {
382 struct dma_uap *dmau = list_entry(node, struct dma_uap, node);
Arnd Bergmann787b0c12013-01-28 16:24:37 +0000383 pl011_dma_probe_initcall(dmau->dev, dmau->uap);
Russell King68b65f72010-12-22 17:24:39 +0000384 list_del(node);
385 kfree(dmau);
386 }
387 return 0;
388}
389
390device_initcall(pl011_dma_initcall);
391
Arnd Bergmann787b0c12013-01-28 16:24:37 +0000392static void pl011_dma_probe(struct device *dev, struct uart_amba_port *uap)
Russell King68b65f72010-12-22 17:24:39 +0000393{
394 struct dma_uap *dmau = kzalloc(sizeof(struct dma_uap), GFP_KERNEL);
395 if (dmau) {
396 dmau->uap = uap;
Arnd Bergmann787b0c12013-01-28 16:24:37 +0000397 dmau->dev = dev;
Russell King68b65f72010-12-22 17:24:39 +0000398 list_add_tail(&dmau->node, &pl011_dma_uarts);
399 }
400}
401#else
Arnd Bergmann787b0c12013-01-28 16:24:37 +0000402static void pl011_dma_probe(struct device *dev, struct uart_amba_port *uap)
Russell King68b65f72010-12-22 17:24:39 +0000403{
Arnd Bergmann787b0c12013-01-28 16:24:37 +0000404 pl011_dma_probe_initcall(dev, uap);
Russell King68b65f72010-12-22 17:24:39 +0000405}
406#endif
407
408static void pl011_dma_remove(struct uart_amba_port *uap)
409{
410 /* TODO: remove the initcall if it has not yet executed */
411 if (uap->dmatx.chan)
412 dma_release_channel(uap->dmatx.chan);
Linus Walleijead76f32011-02-24 13:21:08 +0100413 if (uap->dmarx.chan)
414 dma_release_channel(uap->dmarx.chan);
Russell King68b65f72010-12-22 17:24:39 +0000415}
416
Russell King68b65f72010-12-22 17:24:39 +0000417/* Forward declare this for the refill routine */
418static int pl011_dma_tx_refill(struct uart_amba_port *uap);
419
420/*
421 * The current DMA TX buffer has been sent.
422 * Try to queue up another DMA buffer.
423 */
424static void pl011_dma_tx_callback(void *data)
425{
426 struct uart_amba_port *uap = data;
427 struct pl011_dmatx_data *dmatx = &uap->dmatx;
428 unsigned long flags;
429 u16 dmacr;
430
431 spin_lock_irqsave(&uap->port.lock, flags);
432 if (uap->dmatx.queued)
433 dma_unmap_sg(dmatx->chan->device->dev, &dmatx->sg, 1,
434 DMA_TO_DEVICE);
435
436 dmacr = uap->dmacr;
437 uap->dmacr = dmacr & ~UART011_TXDMAE;
438 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
439
440 /*
441 * If TX DMA was disabled, it means that we've stopped the DMA for
442 * some reason (eg, XOFF received, or we want to send an X-char.)
443 *
444 * Note: we need to be careful here of a potential race between DMA
445 * and the rest of the driver - if the driver disables TX DMA while
446 * a TX buffer completing, we must update the tx queued status to
447 * get further refills (hence we check dmacr).
448 */
449 if (!(dmacr & UART011_TXDMAE) || uart_tx_stopped(&uap->port) ||
450 uart_circ_empty(&uap->port.state->xmit)) {
451 uap->dmatx.queued = false;
452 spin_unlock_irqrestore(&uap->port.lock, flags);
453 return;
454 }
455
456 if (pl011_dma_tx_refill(uap) <= 0) {
457 /*
458 * We didn't queue a DMA buffer for some reason, but we
459 * have data pending to be sent. Re-enable the TX IRQ.
460 */
461 uap->im |= UART011_TXIM;
462 writew(uap->im, uap->port.membase + UART011_IMSC);
463 }
464 spin_unlock_irqrestore(&uap->port.lock, flags);
465}
466
467/*
468 * Try to refill the TX DMA buffer.
469 * Locking: called with port lock held and IRQs disabled.
470 * Returns:
471 * 1 if we queued up a TX DMA buffer.
472 * 0 if we didn't want to handle this by DMA
473 * <0 on error
474 */
475static int pl011_dma_tx_refill(struct uart_amba_port *uap)
476{
477 struct pl011_dmatx_data *dmatx = &uap->dmatx;
478 struct dma_chan *chan = dmatx->chan;
479 struct dma_device *dma_dev = chan->device;
480 struct dma_async_tx_descriptor *desc;
481 struct circ_buf *xmit = &uap->port.state->xmit;
482 unsigned int count;
483
484 /*
485 * Try to avoid the overhead involved in using DMA if the
486 * transaction fits in the first half of the FIFO, by using
487 * the standard interrupt handling. This ensures that we
488 * issue a uart_write_wakeup() at the appropriate time.
489 */
490 count = uart_circ_chars_pending(xmit);
491 if (count < (uap->fifosize >> 1)) {
492 uap->dmatx.queued = false;
493 return 0;
494 }
495
496 /*
497 * Bodge: don't send the last character by DMA, as this
498 * will prevent XON from notifying us to restart DMA.
499 */
500 count -= 1;
501
502 /* Else proceed to copy the TX chars to the DMA buffer and fire DMA */
503 if (count > PL011_DMA_BUFFER_SIZE)
504 count = PL011_DMA_BUFFER_SIZE;
505
506 if (xmit->tail < xmit->head)
507 memcpy(&dmatx->buf[0], &xmit->buf[xmit->tail], count);
508 else {
509 size_t first = UART_XMIT_SIZE - xmit->tail;
510 size_t second = xmit->head;
511
512 memcpy(&dmatx->buf[0], &xmit->buf[xmit->tail], first);
513 if (second)
514 memcpy(&dmatx->buf[first], &xmit->buf[0], second);
515 }
516
517 dmatx->sg.length = count;
518
519 if (dma_map_sg(dma_dev->dev, &dmatx->sg, 1, DMA_TO_DEVICE) != 1) {
520 uap->dmatx.queued = false;
521 dev_dbg(uap->port.dev, "unable to map TX DMA\n");
522 return -EBUSY;
523 }
524
Alexandre Bounine16052822012-03-08 16:11:18 -0500525 desc = dmaengine_prep_slave_sg(chan, &dmatx->sg, 1, DMA_MEM_TO_DEV,
Russell King68b65f72010-12-22 17:24:39 +0000526 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
527 if (!desc) {
528 dma_unmap_sg(dma_dev->dev, &dmatx->sg, 1, DMA_TO_DEVICE);
529 uap->dmatx.queued = false;
530 /*
531 * If DMA cannot be used right now, we complete this
532 * transaction via IRQ and let the TTY layer retry.
533 */
534 dev_dbg(uap->port.dev, "TX DMA busy\n");
535 return -EBUSY;
536 }
537
538 /* Some data to go along to the callback */
539 desc->callback = pl011_dma_tx_callback;
540 desc->callback_param = uap;
541
542 /* All errors should happen at prepare time */
543 dmaengine_submit(desc);
544
545 /* Fire the DMA transaction */
546 dma_dev->device_issue_pending(chan);
547
548 uap->dmacr |= UART011_TXDMAE;
549 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
550 uap->dmatx.queued = true;
551
552 /*
553 * Now we know that DMA will fire, so advance the ring buffer
554 * with the stuff we just dispatched.
555 */
556 xmit->tail = (xmit->tail + count) & (UART_XMIT_SIZE - 1);
557 uap->port.icount.tx += count;
558
559 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
560 uart_write_wakeup(&uap->port);
561
562 return 1;
563}
564
565/*
566 * We received a transmit interrupt without a pending X-char but with
567 * pending characters.
568 * Locking: called with port lock held and IRQs disabled.
569 * Returns:
570 * false if we want to use PIO to transmit
571 * true if we queued a DMA buffer
572 */
573static bool pl011_dma_tx_irq(struct uart_amba_port *uap)
574{
Linus Walleijead76f32011-02-24 13:21:08 +0100575 if (!uap->using_tx_dma)
Russell King68b65f72010-12-22 17:24:39 +0000576 return false;
577
578 /*
579 * If we already have a TX buffer queued, but received a
580 * TX interrupt, it will be because we've just sent an X-char.
581 * Ensure the TX DMA is enabled and the TX IRQ is disabled.
582 */
583 if (uap->dmatx.queued) {
584 uap->dmacr |= UART011_TXDMAE;
585 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
586 uap->im &= ~UART011_TXIM;
587 writew(uap->im, uap->port.membase + UART011_IMSC);
588 return true;
589 }
590
591 /*
592 * We don't have a TX buffer queued, so try to queue one.
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300593 * If we successfully queued a buffer, mask the TX IRQ.
Russell King68b65f72010-12-22 17:24:39 +0000594 */
595 if (pl011_dma_tx_refill(uap) > 0) {
596 uap->im &= ~UART011_TXIM;
597 writew(uap->im, uap->port.membase + UART011_IMSC);
598 return true;
599 }
600 return false;
601}
602
603/*
604 * Stop the DMA transmit (eg, due to received XOFF).
605 * Locking: called with port lock held and IRQs disabled.
606 */
607static inline void pl011_dma_tx_stop(struct uart_amba_port *uap)
608{
609 if (uap->dmatx.queued) {
610 uap->dmacr &= ~UART011_TXDMAE;
611 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
612 }
613}
614
615/*
616 * Try to start a DMA transmit, or in the case of an XON/OFF
617 * character queued for send, try to get that character out ASAP.
618 * Locking: called with port lock held and IRQs disabled.
619 * Returns:
620 * false if we want the TX IRQ to be enabled
621 * true if we have a buffer queued
622 */
623static inline bool pl011_dma_tx_start(struct uart_amba_port *uap)
624{
625 u16 dmacr;
626
Linus Walleijead76f32011-02-24 13:21:08 +0100627 if (!uap->using_tx_dma)
Russell King68b65f72010-12-22 17:24:39 +0000628 return false;
629
630 if (!uap->port.x_char) {
631 /* no X-char, try to push chars out in DMA mode */
632 bool ret = true;
633
634 if (!uap->dmatx.queued) {
635 if (pl011_dma_tx_refill(uap) > 0) {
636 uap->im &= ~UART011_TXIM;
637 ret = true;
638 } else {
639 uap->im |= UART011_TXIM;
640 ret = false;
641 }
642 writew(uap->im, uap->port.membase + UART011_IMSC);
643 } else if (!(uap->dmacr & UART011_TXDMAE)) {
644 uap->dmacr |= UART011_TXDMAE;
645 writew(uap->dmacr,
646 uap->port.membase + UART011_DMACR);
647 }
648 return ret;
649 }
650
651 /*
652 * We have an X-char to send. Disable DMA to prevent it loading
653 * the TX fifo, and then see if we can stuff it into the FIFO.
654 */
655 dmacr = uap->dmacr;
656 uap->dmacr &= ~UART011_TXDMAE;
657 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
658
659 if (readw(uap->port.membase + UART01x_FR) & UART01x_FR_TXFF) {
660 /*
661 * No space in the FIFO, so enable the transmit interrupt
662 * so we know when there is space. Note that once we've
663 * loaded the character, we should just re-enable DMA.
664 */
665 return false;
666 }
667
668 writew(uap->port.x_char, uap->port.membase + UART01x_DR);
669 uap->port.icount.tx++;
670 uap->port.x_char = 0;
671
672 /* Success - restore the DMA state */
673 uap->dmacr = dmacr;
674 writew(dmacr, uap->port.membase + UART011_DMACR);
675
676 return true;
677}
678
679/*
680 * Flush the transmit buffer.
681 * Locking: called with port lock held and IRQs disabled.
682 */
683static void pl011_dma_flush_buffer(struct uart_port *port)
684{
685 struct uart_amba_port *uap = (struct uart_amba_port *)port;
686
Linus Walleijead76f32011-02-24 13:21:08 +0100687 if (!uap->using_tx_dma)
Russell King68b65f72010-12-22 17:24:39 +0000688 return;
689
690 /* Avoid deadlock with the DMA engine callback */
691 spin_unlock(&uap->port.lock);
692 dmaengine_terminate_all(uap->dmatx.chan);
693 spin_lock(&uap->port.lock);
694 if (uap->dmatx.queued) {
695 dma_unmap_sg(uap->dmatx.chan->device->dev, &uap->dmatx.sg, 1,
696 DMA_TO_DEVICE);
697 uap->dmatx.queued = false;
698 uap->dmacr &= ~UART011_TXDMAE;
699 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
700 }
701}
702
Linus Walleijead76f32011-02-24 13:21:08 +0100703static void pl011_dma_rx_callback(void *data);
704
705static int pl011_dma_rx_trigger_dma(struct uart_amba_port *uap)
706{
707 struct dma_chan *rxchan = uap->dmarx.chan;
Linus Walleijead76f32011-02-24 13:21:08 +0100708 struct pl011_dmarx_data *dmarx = &uap->dmarx;
709 struct dma_async_tx_descriptor *desc;
710 struct pl011_sgbuf *sgbuf;
711
712 if (!rxchan)
713 return -EIO;
714
715 /* Start the RX DMA job */
716 sgbuf = uap->dmarx.use_buf_b ?
717 &uap->dmarx.sgbuf_b : &uap->dmarx.sgbuf_a;
Alexandre Bounine16052822012-03-08 16:11:18 -0500718 desc = dmaengine_prep_slave_sg(rxchan, &sgbuf->sg, 1,
Vinod Koula485df42011-10-14 10:47:38 +0530719 DMA_DEV_TO_MEM,
Linus Walleijead76f32011-02-24 13:21:08 +0100720 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
721 /*
722 * If the DMA engine is busy and cannot prepare a
723 * channel, no big deal, the driver will fall back
724 * to interrupt mode as a result of this error code.
725 */
726 if (!desc) {
727 uap->dmarx.running = false;
728 dmaengine_terminate_all(rxchan);
729 return -EBUSY;
730 }
731
732 /* Some data to go along to the callback */
733 desc->callback = pl011_dma_rx_callback;
734 desc->callback_param = uap;
735 dmarx->cookie = dmaengine_submit(desc);
736 dma_async_issue_pending(rxchan);
737
738 uap->dmacr |= UART011_RXDMAE;
739 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
740 uap->dmarx.running = true;
741
742 uap->im &= ~UART011_RXIM;
743 writew(uap->im, uap->port.membase + UART011_IMSC);
744
745 return 0;
746}
747
748/*
749 * This is called when either the DMA job is complete, or
750 * the FIFO timeout interrupt occurred. This must be called
751 * with the port spinlock uap->port.lock held.
752 */
753static void pl011_dma_rx_chars(struct uart_amba_port *uap,
754 u32 pending, bool use_buf_b,
755 bool readfifo)
756{
Jiri Slaby05c7cd32013-01-03 15:53:04 +0100757 struct tty_port *port = &uap->port.state->port;
Linus Walleijead76f32011-02-24 13:21:08 +0100758 struct pl011_sgbuf *sgbuf = use_buf_b ?
759 &uap->dmarx.sgbuf_b : &uap->dmarx.sgbuf_a;
Linus Walleijead76f32011-02-24 13:21:08 +0100760 int dma_count = 0;
761 u32 fifotaken = 0; /* only used for vdbg() */
762
Chanho Mincb06ff12013-03-27 18:38:11 +0900763 struct pl011_dmarx_data *dmarx = &uap->dmarx;
764 int dmataken = 0;
765
766 if (uap->dmarx.poll_rate) {
767 /* The data can be taken by polling */
768 dmataken = sgbuf->sg.length - dmarx->last_residue;
769 /* Recalculate the pending size */
770 if (pending >= dmataken)
771 pending -= dmataken;
772 }
773
774 /* Pick the remain data from the DMA */
Linus Walleijead76f32011-02-24 13:21:08 +0100775 if (pending) {
Linus Walleijead76f32011-02-24 13:21:08 +0100776
777 /*
778 * First take all chars in the DMA pipe, then look in the FIFO.
779 * Note that tty_insert_flip_buf() tries to take as many chars
780 * as it can.
781 */
Chanho Mincb06ff12013-03-27 18:38:11 +0900782 dma_count = tty_insert_flip_string(port, sgbuf->buf + dmataken,
783 pending);
Linus Walleijead76f32011-02-24 13:21:08 +0100784
785 uap->port.icount.rx += dma_count;
786 if (dma_count < pending)
787 dev_warn(uap->port.dev,
788 "couldn't insert all characters (TTY is full?)\n");
789 }
790
Chanho Mincb06ff12013-03-27 18:38:11 +0900791 /* Reset the last_residue for Rx DMA poll */
792 if (uap->dmarx.poll_rate)
793 dmarx->last_residue = sgbuf->sg.length;
794
Linus Walleijead76f32011-02-24 13:21:08 +0100795 /*
796 * Only continue with trying to read the FIFO if all DMA chars have
797 * been taken first.
798 */
799 if (dma_count == pending && readfifo) {
800 /* Clear any error flags */
801 writew(UART011_OEIS | UART011_BEIS | UART011_PEIS | UART011_FEIS,
802 uap->port.membase + UART011_ICR);
803
804 /*
805 * If we read all the DMA'd characters, and we had an
Linus Walleij29772c42011-02-24 13:21:36 +0100806 * incomplete buffer, that could be due to an rx error, or
807 * maybe we just timed out. Read any pending chars and check
808 * the error status.
809 *
810 * Error conditions will only occur in the FIFO, these will
811 * trigger an immediate interrupt and stop the DMA job, so we
812 * will always find the error in the FIFO, never in the DMA
813 * buffer.
Linus Walleijead76f32011-02-24 13:21:08 +0100814 */
Linus Walleij29772c42011-02-24 13:21:36 +0100815 fifotaken = pl011_fifo_to_tty(uap);
Linus Walleijead76f32011-02-24 13:21:08 +0100816 }
817
818 spin_unlock(&uap->port.lock);
819 dev_vdbg(uap->port.dev,
820 "Took %d chars from DMA buffer and %d chars from the FIFO\n",
821 dma_count, fifotaken);
Jiri Slaby2e124b42013-01-03 15:53:06 +0100822 tty_flip_buffer_push(port);
Linus Walleijead76f32011-02-24 13:21:08 +0100823 spin_lock(&uap->port.lock);
824}
825
826static void pl011_dma_rx_irq(struct uart_amba_port *uap)
827{
828 struct pl011_dmarx_data *dmarx = &uap->dmarx;
829 struct dma_chan *rxchan = dmarx->chan;
830 struct pl011_sgbuf *sgbuf = dmarx->use_buf_b ?
831 &dmarx->sgbuf_b : &dmarx->sgbuf_a;
832 size_t pending;
833 struct dma_tx_state state;
834 enum dma_status dmastat;
835
836 /*
837 * Pause the transfer so we can trust the current counter,
838 * do this before we pause the PL011 block, else we may
839 * overflow the FIFO.
840 */
841 if (dmaengine_pause(rxchan))
842 dev_err(uap->port.dev, "unable to pause DMA transfer\n");
843 dmastat = rxchan->device->device_tx_status(rxchan,
844 dmarx->cookie, &state);
845 if (dmastat != DMA_PAUSED)
846 dev_err(uap->port.dev, "unable to pause DMA transfer\n");
847
848 /* Disable RX DMA - incoming data will wait in the FIFO */
849 uap->dmacr &= ~UART011_RXDMAE;
850 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
851 uap->dmarx.running = false;
852
853 pending = sgbuf->sg.length - state.residue;
854 BUG_ON(pending > PL011_DMA_BUFFER_SIZE);
855 /* Then we terminate the transfer - we now know our residue */
856 dmaengine_terminate_all(rxchan);
857
858 /*
859 * This will take the chars we have so far and insert
860 * into the framework.
861 */
862 pl011_dma_rx_chars(uap, pending, dmarx->use_buf_b, true);
863
864 /* Switch buffer & re-trigger DMA job */
865 dmarx->use_buf_b = !dmarx->use_buf_b;
866 if (pl011_dma_rx_trigger_dma(uap)) {
867 dev_dbg(uap->port.dev, "could not retrigger RX DMA job "
868 "fall back to interrupt mode\n");
869 uap->im |= UART011_RXIM;
870 writew(uap->im, uap->port.membase + UART011_IMSC);
871 }
872}
873
874static void pl011_dma_rx_callback(void *data)
875{
876 struct uart_amba_port *uap = data;
877 struct pl011_dmarx_data *dmarx = &uap->dmarx;
Chanho Min6dc01aa2012-02-20 10:24:40 +0900878 struct dma_chan *rxchan = dmarx->chan;
Linus Walleijead76f32011-02-24 13:21:08 +0100879 bool lastbuf = dmarx->use_buf_b;
Chanho Min6dc01aa2012-02-20 10:24:40 +0900880 struct pl011_sgbuf *sgbuf = dmarx->use_buf_b ?
881 &dmarx->sgbuf_b : &dmarx->sgbuf_a;
882 size_t pending;
883 struct dma_tx_state state;
Linus Walleijead76f32011-02-24 13:21:08 +0100884 int ret;
885
886 /*
887 * This completion interrupt occurs typically when the
888 * RX buffer is totally stuffed but no timeout has yet
889 * occurred. When that happens, we just want the RX
890 * routine to flush out the secondary DMA buffer while
891 * we immediately trigger the next DMA job.
892 */
893 spin_lock_irq(&uap->port.lock);
Chanho Min6dc01aa2012-02-20 10:24:40 +0900894 /*
895 * Rx data can be taken by the UART interrupts during
896 * the DMA irq handler. So we check the residue here.
897 */
898 rxchan->device->device_tx_status(rxchan, dmarx->cookie, &state);
899 pending = sgbuf->sg.length - state.residue;
900 BUG_ON(pending > PL011_DMA_BUFFER_SIZE);
901 /* Then we terminate the transfer - we now know our residue */
902 dmaengine_terminate_all(rxchan);
903
Linus Walleijead76f32011-02-24 13:21:08 +0100904 uap->dmarx.running = false;
905 dmarx->use_buf_b = !lastbuf;
906 ret = pl011_dma_rx_trigger_dma(uap);
907
Chanho Min6dc01aa2012-02-20 10:24:40 +0900908 pl011_dma_rx_chars(uap, pending, lastbuf, false);
Linus Walleijead76f32011-02-24 13:21:08 +0100909 spin_unlock_irq(&uap->port.lock);
910 /*
911 * Do this check after we picked the DMA chars so we don't
912 * get some IRQ immediately from RX.
913 */
914 if (ret) {
915 dev_dbg(uap->port.dev, "could not retrigger RX DMA job "
916 "fall back to interrupt mode\n");
917 uap->im |= UART011_RXIM;
918 writew(uap->im, uap->port.membase + UART011_IMSC);
919 }
920}
921
922/*
923 * Stop accepting received characters, when we're shutting down or
924 * suspending this port.
925 * Locking: called with port lock held and IRQs disabled.
926 */
927static inline void pl011_dma_rx_stop(struct uart_amba_port *uap)
928{
929 /* FIXME. Just disable the DMA enable */
930 uap->dmacr &= ~UART011_RXDMAE;
931 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
932}
Russell King68b65f72010-12-22 17:24:39 +0000933
Chanho Mincb06ff12013-03-27 18:38:11 +0900934/*
935 * Timer handler for Rx DMA polling.
936 * Every polling, It checks the residue in the dma buffer and transfer
937 * data to the tty. Also, last_residue is updated for the next polling.
938 */
939static void pl011_dma_rx_poll(unsigned long args)
940{
941 struct uart_amba_port *uap = (struct uart_amba_port *)args;
942 struct tty_port *port = &uap->port.state->port;
943 struct pl011_dmarx_data *dmarx = &uap->dmarx;
944 struct dma_chan *rxchan = uap->dmarx.chan;
945 unsigned long flags = 0;
946 unsigned int dmataken = 0;
947 unsigned int size = 0;
948 struct pl011_sgbuf *sgbuf;
949 int dma_count;
950 struct dma_tx_state state;
951
952 sgbuf = dmarx->use_buf_b ? &uap->dmarx.sgbuf_b : &uap->dmarx.sgbuf_a;
953 rxchan->device->device_tx_status(rxchan, dmarx->cookie, &state);
954 if (likely(state.residue < dmarx->last_residue)) {
955 dmataken = sgbuf->sg.length - dmarx->last_residue;
956 size = dmarx->last_residue - state.residue;
957 dma_count = tty_insert_flip_string(port, sgbuf->buf + dmataken,
958 size);
959 if (dma_count == size)
960 dmarx->last_residue = state.residue;
961 dmarx->last_jiffies = jiffies;
962 }
963 tty_flip_buffer_push(port);
964
965 /*
966 * If no data is received in poll_timeout, the driver will fall back
967 * to interrupt mode. We will retrigger DMA at the first interrupt.
968 */
969 if (jiffies_to_msecs(jiffies - dmarx->last_jiffies)
970 > uap->dmarx.poll_timeout) {
971
972 spin_lock_irqsave(&uap->port.lock, flags);
973 pl011_dma_rx_stop(uap);
974 spin_unlock_irqrestore(&uap->port.lock, flags);
975
976 uap->dmarx.running = false;
977 dmaengine_terminate_all(rxchan);
978 del_timer(&uap->dmarx.timer);
979 } else {
980 mod_timer(&uap->dmarx.timer,
981 jiffies + msecs_to_jiffies(uap->dmarx.poll_rate));
982 }
983}
984
Russell King68b65f72010-12-22 17:24:39 +0000985static void pl011_dma_startup(struct uart_amba_port *uap)
986{
Linus Walleijead76f32011-02-24 13:21:08 +0100987 int ret;
988
Russell King68b65f72010-12-22 17:24:39 +0000989 if (!uap->dmatx.chan)
990 return;
991
992 uap->dmatx.buf = kmalloc(PL011_DMA_BUFFER_SIZE, GFP_KERNEL);
993 if (!uap->dmatx.buf) {
994 dev_err(uap->port.dev, "no memory for DMA TX buffer\n");
995 uap->port.fifosize = uap->fifosize;
996 return;
997 }
998
999 sg_init_one(&uap->dmatx.sg, uap->dmatx.buf, PL011_DMA_BUFFER_SIZE);
1000
1001 /* The DMA buffer is now the FIFO the TTY subsystem can use */
1002 uap->port.fifosize = PL011_DMA_BUFFER_SIZE;
Linus Walleijead76f32011-02-24 13:21:08 +01001003 uap->using_tx_dma = true;
Russell King68b65f72010-12-22 17:24:39 +00001004
Linus Walleijead76f32011-02-24 13:21:08 +01001005 if (!uap->dmarx.chan)
1006 goto skip_rx;
1007
1008 /* Allocate and map DMA RX buffers */
1009 ret = pl011_sgbuf_init(uap->dmarx.chan, &uap->dmarx.sgbuf_a,
1010 DMA_FROM_DEVICE);
1011 if (ret) {
1012 dev_err(uap->port.dev, "failed to init DMA %s: %d\n",
1013 "RX buffer A", ret);
1014 goto skip_rx;
1015 }
1016
1017 ret = pl011_sgbuf_init(uap->dmarx.chan, &uap->dmarx.sgbuf_b,
1018 DMA_FROM_DEVICE);
1019 if (ret) {
1020 dev_err(uap->port.dev, "failed to init DMA %s: %d\n",
1021 "RX buffer B", ret);
1022 pl011_sgbuf_free(uap->dmarx.chan, &uap->dmarx.sgbuf_a,
1023 DMA_FROM_DEVICE);
1024 goto skip_rx;
1025 }
1026
1027 uap->using_rx_dma = true;
1028
1029skip_rx:
Russell King68b65f72010-12-22 17:24:39 +00001030 /* Turn on DMA error (RX/TX will be enabled on demand) */
1031 uap->dmacr |= UART011_DMAONERR;
1032 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
Russell King38d62432010-12-22 17:59:16 +00001033
1034 /*
1035 * ST Micro variants has some specific dma burst threshold
1036 * compensation. Set this to 16 bytes, so burst will only
1037 * be issued above/below 16 bytes.
1038 */
1039 if (uap->vendor->dma_threshold)
1040 writew(ST_UART011_DMAWM_RX_16 | ST_UART011_DMAWM_TX_16,
1041 uap->port.membase + ST_UART011_DMAWM);
Linus Walleijead76f32011-02-24 13:21:08 +01001042
1043 if (uap->using_rx_dma) {
1044 if (pl011_dma_rx_trigger_dma(uap))
1045 dev_dbg(uap->port.dev, "could not trigger initial "
1046 "RX DMA job, fall back to interrupt mode\n");
Chanho Mincb06ff12013-03-27 18:38:11 +09001047 if (uap->dmarx.poll_rate) {
1048 init_timer(&(uap->dmarx.timer));
1049 uap->dmarx.timer.function = pl011_dma_rx_poll;
1050 uap->dmarx.timer.data = (unsigned long)uap;
1051 mod_timer(&uap->dmarx.timer,
1052 jiffies +
1053 msecs_to_jiffies(uap->dmarx.poll_rate));
1054 uap->dmarx.last_residue = PL011_DMA_BUFFER_SIZE;
1055 uap->dmarx.last_jiffies = jiffies;
1056 }
Linus Walleijead76f32011-02-24 13:21:08 +01001057 }
Russell King68b65f72010-12-22 17:24:39 +00001058}
1059
1060static void pl011_dma_shutdown(struct uart_amba_port *uap)
1061{
Linus Walleijead76f32011-02-24 13:21:08 +01001062 if (!(uap->using_tx_dma || uap->using_rx_dma))
Russell King68b65f72010-12-22 17:24:39 +00001063 return;
1064
1065 /* Disable RX and TX DMA */
1066 while (readw(uap->port.membase + UART01x_FR) & UART01x_FR_BUSY)
1067 barrier();
1068
1069 spin_lock_irq(&uap->port.lock);
1070 uap->dmacr &= ~(UART011_DMAONERR | UART011_RXDMAE | UART011_TXDMAE);
1071 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
1072 spin_unlock_irq(&uap->port.lock);
1073
Linus Walleijead76f32011-02-24 13:21:08 +01001074 if (uap->using_tx_dma) {
1075 /* In theory, this should already be done by pl011_dma_flush_buffer */
1076 dmaengine_terminate_all(uap->dmatx.chan);
1077 if (uap->dmatx.queued) {
1078 dma_unmap_sg(uap->dmatx.chan->device->dev, &uap->dmatx.sg, 1,
1079 DMA_TO_DEVICE);
1080 uap->dmatx.queued = false;
1081 }
1082
1083 kfree(uap->dmatx.buf);
1084 uap->using_tx_dma = false;
Russell King68b65f72010-12-22 17:24:39 +00001085 }
1086
Linus Walleijead76f32011-02-24 13:21:08 +01001087 if (uap->using_rx_dma) {
1088 dmaengine_terminate_all(uap->dmarx.chan);
1089 /* Clean up the RX DMA */
1090 pl011_sgbuf_free(uap->dmarx.chan, &uap->dmarx.sgbuf_a, DMA_FROM_DEVICE);
1091 pl011_sgbuf_free(uap->dmarx.chan, &uap->dmarx.sgbuf_b, DMA_FROM_DEVICE);
Chanho Mincb06ff12013-03-27 18:38:11 +09001092 if (uap->dmarx.poll_rate)
1093 del_timer_sync(&uap->dmarx.timer);
Linus Walleijead76f32011-02-24 13:21:08 +01001094 uap->using_rx_dma = false;
1095 }
Russell King68b65f72010-12-22 17:24:39 +00001096}
1097
Linus Walleijead76f32011-02-24 13:21:08 +01001098static inline bool pl011_dma_rx_available(struct uart_amba_port *uap)
1099{
1100 return uap->using_rx_dma;
1101}
1102
1103static inline bool pl011_dma_rx_running(struct uart_amba_port *uap)
1104{
1105 return uap->using_rx_dma && uap->dmarx.running;
1106}
1107
Russell King68b65f72010-12-22 17:24:39 +00001108#else
1109/* Blank functions if the DMA engine is not available */
Arnd Bergmannaabdd292013-04-20 09:40:33 +02001110static inline void pl011_dma_probe(struct device *dev, struct uart_amba_port *uap)
Russell King68b65f72010-12-22 17:24:39 +00001111{
1112}
1113
1114static inline void pl011_dma_remove(struct uart_amba_port *uap)
1115{
1116}
1117
1118static inline void pl011_dma_startup(struct uart_amba_port *uap)
1119{
1120}
1121
1122static inline void pl011_dma_shutdown(struct uart_amba_port *uap)
1123{
1124}
1125
1126static inline bool pl011_dma_tx_irq(struct uart_amba_port *uap)
1127{
1128 return false;
1129}
1130
1131static inline void pl011_dma_tx_stop(struct uart_amba_port *uap)
1132{
1133}
1134
1135static inline bool pl011_dma_tx_start(struct uart_amba_port *uap)
1136{
1137 return false;
1138}
1139
Linus Walleijead76f32011-02-24 13:21:08 +01001140static inline void pl011_dma_rx_irq(struct uart_amba_port *uap)
1141{
1142}
1143
1144static inline void pl011_dma_rx_stop(struct uart_amba_port *uap)
1145{
1146}
1147
1148static inline int pl011_dma_rx_trigger_dma(struct uart_amba_port *uap)
1149{
1150 return -EIO;
1151}
1152
1153static inline bool pl011_dma_rx_available(struct uart_amba_port *uap)
1154{
1155 return false;
1156}
1157
1158static inline bool pl011_dma_rx_running(struct uart_amba_port *uap)
1159{
1160 return false;
1161}
1162
Russell King68b65f72010-12-22 17:24:39 +00001163#define pl011_dma_flush_buffer NULL
1164#endif
1165
Russell Kingb129a8c2005-08-31 10:12:14 +01001166static void pl011_stop_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167{
1168 struct uart_amba_port *uap = (struct uart_amba_port *)port;
1169
1170 uap->im &= ~UART011_TXIM;
1171 writew(uap->im, uap->port.membase + UART011_IMSC);
Russell King68b65f72010-12-22 17:24:39 +00001172 pl011_dma_tx_stop(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173}
1174
Russell Kingb129a8c2005-08-31 10:12:14 +01001175static void pl011_start_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176{
1177 struct uart_amba_port *uap = (struct uart_amba_port *)port;
1178
Russell King68b65f72010-12-22 17:24:39 +00001179 if (!pl011_dma_tx_start(uap)) {
1180 uap->im |= UART011_TXIM;
1181 writew(uap->im, uap->port.membase + UART011_IMSC);
1182 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183}
1184
1185static void pl011_stop_rx(struct uart_port *port)
1186{
1187 struct uart_amba_port *uap = (struct uart_amba_port *)port;
1188
1189 uap->im &= ~(UART011_RXIM|UART011_RTIM|UART011_FEIM|
1190 UART011_PEIM|UART011_BEIM|UART011_OEIM);
1191 writew(uap->im, uap->port.membase + UART011_IMSC);
Linus Walleijead76f32011-02-24 13:21:08 +01001192
1193 pl011_dma_rx_stop(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194}
1195
1196static void pl011_enable_ms(struct uart_port *port)
1197{
1198 struct uart_amba_port *uap = (struct uart_amba_port *)port;
1199
1200 uap->im |= UART011_RIMIM|UART011_CTSMIM|UART011_DCDMIM|UART011_DSRMIM;
1201 writew(uap->im, uap->port.membase + UART011_IMSC);
1202}
1203
David Howells7d12e782006-10-05 14:55:46 +01001204static void pl011_rx_chars(struct uart_amba_port *uap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205{
Linus Walleij29772c42011-02-24 13:21:36 +01001206 pl011_fifo_to_tty(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207
Thomas Gleixner2389b272007-05-29 21:53:50 +01001208 spin_unlock(&uap->port.lock);
Jiri Slaby2e124b42013-01-03 15:53:06 +01001209 tty_flip_buffer_push(&uap->port.state->port);
Linus Walleijead76f32011-02-24 13:21:08 +01001210 /*
1211 * If we were temporarily out of DMA mode for a while,
1212 * attempt to switch back to DMA mode again.
1213 */
1214 if (pl011_dma_rx_available(uap)) {
1215 if (pl011_dma_rx_trigger_dma(uap)) {
1216 dev_dbg(uap->port.dev, "could not trigger RX DMA job "
1217 "fall back to interrupt mode again\n");
1218 uap->im |= UART011_RXIM;
Chanho Mincb06ff12013-03-27 18:38:11 +09001219 } else {
Linus Walleijead76f32011-02-24 13:21:08 +01001220 uap->im &= ~UART011_RXIM;
Chanho Min89fa28d2013-04-03 11:10:37 +09001221#ifdef CONFIG_DMA_ENGINE
Chanho Mincb06ff12013-03-27 18:38:11 +09001222 /* Start Rx DMA poll */
1223 if (uap->dmarx.poll_rate) {
1224 uap->dmarx.last_jiffies = jiffies;
1225 uap->dmarx.last_residue = PL011_DMA_BUFFER_SIZE;
1226 mod_timer(&uap->dmarx.timer,
1227 jiffies +
1228 msecs_to_jiffies(uap->dmarx.poll_rate));
1229 }
Chanho Min89fa28d2013-04-03 11:10:37 +09001230#endif
Chanho Mincb06ff12013-03-27 18:38:11 +09001231 }
1232
Linus Walleijead76f32011-02-24 13:21:08 +01001233 writew(uap->im, uap->port.membase + UART011_IMSC);
1234 }
Thomas Gleixner2389b272007-05-29 21:53:50 +01001235 spin_lock(&uap->port.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236}
1237
1238static void pl011_tx_chars(struct uart_amba_port *uap)
1239{
Alan Coxebd2c8f2009-09-19 13:13:28 -07001240 struct circ_buf *xmit = &uap->port.state->xmit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 int count;
1242
1243 if (uap->port.x_char) {
1244 writew(uap->port.x_char, uap->port.membase + UART01x_DR);
1245 uap->port.icount.tx++;
1246 uap->port.x_char = 0;
1247 return;
1248 }
1249 if (uart_circ_empty(xmit) || uart_tx_stopped(&uap->port)) {
Russell Kingb129a8c2005-08-31 10:12:14 +01001250 pl011_stop_tx(&uap->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 return;
1252 }
1253
Russell King68b65f72010-12-22 17:24:39 +00001254 /* If we are using DMA mode, try to send some characters. */
1255 if (pl011_dma_tx_irq(uap))
1256 return;
1257
Russell Kingffca2b12010-12-22 17:13:05 +00001258 count = uap->fifosize >> 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 do {
1260 writew(xmit->buf[xmit->tail], uap->port.membase + UART01x_DR);
1261 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
1262 uap->port.icount.tx++;
1263 if (uart_circ_empty(xmit))
1264 break;
1265 } while (--count > 0);
1266
1267 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
1268 uart_write_wakeup(&uap->port);
1269
1270 if (uart_circ_empty(xmit))
Russell Kingb129a8c2005-08-31 10:12:14 +01001271 pl011_stop_tx(&uap->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272}
1273
1274static void pl011_modem_status(struct uart_amba_port *uap)
1275{
1276 unsigned int status, delta;
1277
1278 status = readw(uap->port.membase + UART01x_FR) & UART01x_FR_MODEM_ANY;
1279
1280 delta = status ^ uap->old_status;
1281 uap->old_status = status;
1282
1283 if (!delta)
1284 return;
1285
1286 if (delta & UART01x_FR_DCD)
1287 uart_handle_dcd_change(&uap->port, status & UART01x_FR_DCD);
1288
1289 if (delta & UART01x_FR_DSR)
1290 uap->port.icount.dsr++;
1291
1292 if (delta & UART01x_FR_CTS)
1293 uart_handle_cts_change(&uap->port, status & UART01x_FR_CTS);
1294
Alan Coxbdc04e32009-09-19 13:13:31 -07001295 wake_up_interruptible(&uap->port.state->port.delta_msr_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296}
1297
David Howells7d12e782006-10-05 14:55:46 +01001298static irqreturn_t pl011_int(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299{
1300 struct uart_amba_port *uap = dev_id;
Russell King963cc982010-12-22 17:16:09 +00001301 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 unsigned int status, pass_counter = AMBA_ISR_PASS_LIMIT;
1303 int handled = 0;
Rajanikanth H.V4fd06902012-03-26 11:17:02 +02001304 unsigned int dummy_read;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305
Russell King963cc982010-12-22 17:16:09 +00001306 spin_lock_irqsave(&uap->port.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 status = readw(uap->port.membase + UART011_MIS);
1308 if (status) {
1309 do {
Rajanikanth H.V4fd06902012-03-26 11:17:02 +02001310 if (uap->vendor->cts_event_workaround) {
1311 /* workaround to make sure that all bits are unlocked.. */
1312 writew(0x00, uap->port.membase + UART011_ICR);
1313
1314 /*
1315 * WA: introduce 26ns(1 uart clk) delay before W1C;
1316 * single apb access will incur 2 pclk(133.12Mhz) delay,
1317 * so add 2 dummy reads
1318 */
1319 dummy_read = readw(uap->port.membase + UART011_ICR);
1320 dummy_read = readw(uap->port.membase + UART011_ICR);
1321 }
1322
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 writew(status & ~(UART011_TXIS|UART011_RTIS|
1324 UART011_RXIS),
1325 uap->port.membase + UART011_ICR);
1326
Linus Walleijead76f32011-02-24 13:21:08 +01001327 if (status & (UART011_RTIS|UART011_RXIS)) {
1328 if (pl011_dma_rx_running(uap))
1329 pl011_dma_rx_irq(uap);
1330 else
1331 pl011_rx_chars(uap);
1332 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 if (status & (UART011_DSRMIS|UART011_DCDMIS|
1334 UART011_CTSMIS|UART011_RIMIS))
1335 pl011_modem_status(uap);
1336 if (status & UART011_TXIS)
1337 pl011_tx_chars(uap);
1338
Rajanikanth H.V4fd06902012-03-26 11:17:02 +02001339 if (pass_counter-- == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 break;
1341
1342 status = readw(uap->port.membase + UART011_MIS);
1343 } while (status != 0);
1344 handled = 1;
1345 }
1346
Russell King963cc982010-12-22 17:16:09 +00001347 spin_unlock_irqrestore(&uap->port.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348
1349 return IRQ_RETVAL(handled);
1350}
1351
Linus Walleije643f872012-06-17 15:44:19 +02001352static unsigned int pl011_tx_empty(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353{
1354 struct uart_amba_port *uap = (struct uart_amba_port *)port;
1355 unsigned int status = readw(uap->port.membase + UART01x_FR);
1356 return status & (UART01x_FR_BUSY|UART01x_FR_TXFF) ? 0 : TIOCSER_TEMT;
1357}
1358
Linus Walleije643f872012-06-17 15:44:19 +02001359static unsigned int pl011_get_mctrl(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360{
1361 struct uart_amba_port *uap = (struct uart_amba_port *)port;
1362 unsigned int result = 0;
1363 unsigned int status = readw(uap->port.membase + UART01x_FR);
1364
Jiri Slaby5159f402007-10-18 23:40:31 -07001365#define TIOCMBIT(uartbit, tiocmbit) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 if (status & uartbit) \
1367 result |= tiocmbit
1368
Jiri Slaby5159f402007-10-18 23:40:31 -07001369 TIOCMBIT(UART01x_FR_DCD, TIOCM_CAR);
1370 TIOCMBIT(UART01x_FR_DSR, TIOCM_DSR);
1371 TIOCMBIT(UART01x_FR_CTS, TIOCM_CTS);
1372 TIOCMBIT(UART011_FR_RI, TIOCM_RNG);
1373#undef TIOCMBIT
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 return result;
1375}
1376
1377static void pl011_set_mctrl(struct uart_port *port, unsigned int mctrl)
1378{
1379 struct uart_amba_port *uap = (struct uart_amba_port *)port;
1380 unsigned int cr;
1381
1382 cr = readw(uap->port.membase + UART011_CR);
1383
Jiri Slaby5159f402007-10-18 23:40:31 -07001384#define TIOCMBIT(tiocmbit, uartbit) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 if (mctrl & tiocmbit) \
1386 cr |= uartbit; \
1387 else \
1388 cr &= ~uartbit
1389
Jiri Slaby5159f402007-10-18 23:40:31 -07001390 TIOCMBIT(TIOCM_RTS, UART011_CR_RTS);
1391 TIOCMBIT(TIOCM_DTR, UART011_CR_DTR);
1392 TIOCMBIT(TIOCM_OUT1, UART011_CR_OUT1);
1393 TIOCMBIT(TIOCM_OUT2, UART011_CR_OUT2);
1394 TIOCMBIT(TIOCM_LOOP, UART011_CR_LBE);
Rabin Vincent3b438162010-02-12 06:43:11 +01001395
1396 if (uap->autorts) {
1397 /* We need to disable auto-RTS if we want to turn RTS off */
1398 TIOCMBIT(TIOCM_RTS, UART011_CR_RTSEN);
1399 }
Jiri Slaby5159f402007-10-18 23:40:31 -07001400#undef TIOCMBIT
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401
1402 writew(cr, uap->port.membase + UART011_CR);
1403}
1404
1405static void pl011_break_ctl(struct uart_port *port, int break_state)
1406{
1407 struct uart_amba_port *uap = (struct uart_amba_port *)port;
1408 unsigned long flags;
1409 unsigned int lcr_h;
1410
1411 spin_lock_irqsave(&uap->port.lock, flags);
Linus Walleijec489aa2010-06-02 08:13:52 +01001412 lcr_h = readw(uap->port.membase + uap->lcrh_tx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 if (break_state == -1)
1414 lcr_h |= UART01x_LCRH_BRK;
1415 else
1416 lcr_h &= ~UART01x_LCRH_BRK;
Linus Walleijec489aa2010-06-02 08:13:52 +01001417 writew(lcr_h, uap->port.membase + uap->lcrh_tx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 spin_unlock_irqrestore(&uap->port.lock, flags);
1419}
1420
Jason Wessel84b5ae12008-02-20 13:33:39 -06001421#ifdef CONFIG_CONSOLE_POLL
Anton Vorontsov5c8124a2012-09-24 14:27:55 -07001422
1423static void pl011_quiesce_irqs(struct uart_port *port)
1424{
1425 struct uart_amba_port *uap = (struct uart_amba_port *)port;
1426 unsigned char __iomem *regs = uap->port.membase;
1427
1428 writew(readw(regs + UART011_MIS), regs + UART011_ICR);
1429 /*
1430 * There is no way to clear TXIM as this is "ready to transmit IRQ", so
1431 * we simply mask it. start_tx() will unmask it.
1432 *
1433 * Note we can race with start_tx(), and if the race happens, the
1434 * polling user might get another interrupt just after we clear it.
1435 * But it should be OK and can happen even w/o the race, e.g.
1436 * controller immediately got some new data and raised the IRQ.
1437 *
1438 * And whoever uses polling routines assumes that it manages the device
1439 * (including tx queue), so we're also fine with start_tx()'s caller
1440 * side.
1441 */
1442 writew(readw(regs + UART011_IMSC) & ~UART011_TXIM, regs + UART011_IMSC);
1443}
1444
Linus Walleije643f872012-06-17 15:44:19 +02001445static int pl011_get_poll_char(struct uart_port *port)
Jason Wessel84b5ae12008-02-20 13:33:39 -06001446{
1447 struct uart_amba_port *uap = (struct uart_amba_port *)port;
1448 unsigned int status;
1449
Anton Vorontsov5c8124a2012-09-24 14:27:55 -07001450 /*
1451 * The caller might need IRQs lowered, e.g. if used with KDB NMI
1452 * debugger.
1453 */
1454 pl011_quiesce_irqs(port);
1455
Jason Wesself5316b42010-05-20 21:04:22 -05001456 status = readw(uap->port.membase + UART01x_FR);
1457 if (status & UART01x_FR_RXFE)
1458 return NO_POLL_CHAR;
Jason Wessel84b5ae12008-02-20 13:33:39 -06001459
1460 return readw(uap->port.membase + UART01x_DR);
1461}
1462
Linus Walleije643f872012-06-17 15:44:19 +02001463static void pl011_put_poll_char(struct uart_port *port,
Jason Wessel84b5ae12008-02-20 13:33:39 -06001464 unsigned char ch)
1465{
1466 struct uart_amba_port *uap = (struct uart_amba_port *)port;
1467
1468 while (readw(uap->port.membase + UART01x_FR) & UART01x_FR_TXFF)
1469 barrier();
1470
1471 writew(ch, uap->port.membase + UART01x_DR);
1472}
1473
1474#endif /* CONFIG_CONSOLE_POLL */
1475
Anton Vorontsovb3564c22012-09-24 14:27:54 -07001476static int pl011_hwinit(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477{
1478 struct uart_amba_port *uap = (struct uart_amba_port *)port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 int retval;
1480
Linus Walleij78d80c52012-05-23 21:18:46 +02001481 /* Optionaly enable pins to be muxed in and configured */
1482 if (!IS_ERR(uap->pins_default)) {
1483 retval = pinctrl_select_state(uap->pinctrl, uap->pins_default);
1484 if (retval)
1485 dev_err(port->dev,
1486 "could not set default pins\n");
1487 }
1488
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 /*
1490 * Try to enable the clock producer.
1491 */
Julia Lawall1c4c4392012-08-26 18:01:01 +02001492 retval = clk_prepare_enable(uap->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 if (retval)
Julia Lawall1c4c4392012-08-26 18:01:01 +02001494 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495
1496 uap->port.uartclk = clk_get_rate(uap->clk);
1497
Linus Walleij9b96fba2012-03-13 13:27:23 +01001498 /* Clear pending error and receive interrupts */
1499 writew(UART011_OEIS | UART011_BEIS | UART011_PEIS | UART011_FEIS |
1500 UART011_RTIS | UART011_RXIS, uap->port.membase + UART011_ICR);
1501
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 /*
Anton Vorontsovb3564c22012-09-24 14:27:54 -07001503 * Save interrupts enable mask, and enable RX interrupts in case if
1504 * the interrupt is used for NMI entry.
1505 */
1506 uap->im = readw(uap->port.membase + UART011_IMSC);
1507 writew(UART011_RTIM | UART011_RXIM, uap->port.membase + UART011_IMSC);
1508
1509 if (uap->port.dev->platform_data) {
1510 struct amba_pl011_data *plat;
1511
1512 plat = uap->port.dev->platform_data;
1513 if (plat->init)
1514 plat->init();
1515 }
1516 return 0;
1517 out:
1518 return retval;
1519}
1520
1521static int pl011_startup(struct uart_port *port)
1522{
1523 struct uart_amba_port *uap = (struct uart_amba_port *)port;
1524 unsigned int cr;
1525 int retval;
1526
1527 retval = pl011_hwinit(port);
1528 if (retval)
1529 goto clk_dis;
1530
1531 writew(uap->im, uap->port.membase + UART011_IMSC);
1532
1533 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 * Allocate the IRQ
1535 */
1536 retval = request_irq(uap->port.irq, pl011_int, 0, "uart-pl011", uap);
1537 if (retval)
1538 goto clk_dis;
1539
Russell Kingc19f12b2010-12-22 17:48:26 +00001540 writew(uap->vendor->ifls, uap->port.membase + UART011_IFLS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541
1542 /*
1543 * Provoke TX FIFO interrupt into asserting.
1544 */
1545 cr = UART01x_CR_UARTEN | UART011_CR_TXE | UART011_CR_LBE;
1546 writew(cr, uap->port.membase + UART011_CR);
1547 writew(0, uap->port.membase + UART011_FBRD);
1548 writew(1, uap->port.membase + UART011_IBRD);
Linus Walleijec489aa2010-06-02 08:13:52 +01001549 writew(0, uap->port.membase + uap->lcrh_rx);
1550 if (uap->lcrh_tx != uap->lcrh_rx) {
1551 int i;
1552 /*
1553 * Wait 10 PCLKs before writing LCRH_TX register,
1554 * to get this delay write read only register 10 times
1555 */
1556 for (i = 0; i < 10; ++i)
1557 writew(0xff, uap->port.membase + UART011_MIS);
1558 writew(0, uap->port.membase + uap->lcrh_tx);
1559 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 writew(0, uap->port.membase + UART01x_DR);
1561 while (readw(uap->port.membase + UART01x_FR) & UART01x_FR_BUSY)
1562 barrier();
1563
Shreshtha Kumar Sahud8d8ffa2012-01-18 15:53:59 +05301564 /* restore RTS and DTR */
1565 cr = uap->old_cr & (UART011_CR_RTS | UART011_CR_DTR);
1566 cr |= UART01x_CR_UARTEN | UART011_CR_RXE | UART011_CR_TXE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 writew(cr, uap->port.membase + UART011_CR);
1568
1569 /*
1570 * initialise the old status of the modem signals
1571 */
1572 uap->old_status = readw(uap->port.membase + UART01x_FR) & UART01x_FR_MODEM_ANY;
1573
Russell King68b65f72010-12-22 17:24:39 +00001574 /* Startup DMA */
1575 pl011_dma_startup(uap);
1576
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 /*
Linus Walleijead76f32011-02-24 13:21:08 +01001578 * Finally, enable interrupts, only timeouts when using DMA
1579 * if initial RX DMA job failed, start in interrupt mode
1580 * as well.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 */
1582 spin_lock_irq(&uap->port.lock);
Linus Walleij9b96fba2012-03-13 13:27:23 +01001583 /* Clear out any spuriously appearing RX interrupts */
1584 writew(UART011_RTIS | UART011_RXIS,
1585 uap->port.membase + UART011_ICR);
Linus Walleijead76f32011-02-24 13:21:08 +01001586 uap->im = UART011_RTIM;
1587 if (!pl011_dma_rx_running(uap))
1588 uap->im |= UART011_RXIM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 writew(uap->im, uap->port.membase + UART011_IMSC);
1590 spin_unlock_irq(&uap->port.lock);
1591
1592 return 0;
1593
1594 clk_dis:
Julia Lawall1c4c4392012-08-26 18:01:01 +02001595 clk_disable_unprepare(uap->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 return retval;
1597}
1598
Linus Walleijec489aa2010-06-02 08:13:52 +01001599static void pl011_shutdown_channel(struct uart_amba_port *uap,
1600 unsigned int lcrh)
1601{
1602 unsigned long val;
1603
1604 val = readw(uap->port.membase + lcrh);
1605 val &= ~(UART01x_LCRH_BRK | UART01x_LCRH_FEN);
1606 writew(val, uap->port.membase + lcrh);
1607}
1608
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609static void pl011_shutdown(struct uart_port *port)
1610{
1611 struct uart_amba_port *uap = (struct uart_amba_port *)port;
Shreshtha Kumar Sahud8d8ffa2012-01-18 15:53:59 +05301612 unsigned int cr;
Linus Walleij78d80c52012-05-23 21:18:46 +02001613 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614
1615 /*
1616 * disable all interrupts
1617 */
1618 spin_lock_irq(&uap->port.lock);
1619 uap->im = 0;
1620 writew(uap->im, uap->port.membase + UART011_IMSC);
1621 writew(0xffff, uap->port.membase + UART011_ICR);
1622 spin_unlock_irq(&uap->port.lock);
1623
Russell King68b65f72010-12-22 17:24:39 +00001624 pl011_dma_shutdown(uap);
1625
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 /*
1627 * Free the interrupt
1628 */
1629 free_irq(uap->port.irq, uap);
1630
1631 /*
1632 * disable the port
Shreshtha Kumar Sahud8d8ffa2012-01-18 15:53:59 +05301633 * disable the port. It should not disable RTS and DTR.
1634 * Also RTS and DTR state should be preserved to restore
1635 * it during startup().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 */
Rabin Vincent3b438162010-02-12 06:43:11 +01001637 uap->autorts = false;
Shreshtha Kumar Sahud8d8ffa2012-01-18 15:53:59 +05301638 cr = readw(uap->port.membase + UART011_CR);
1639 uap->old_cr = cr;
1640 cr &= UART011_CR_RTS | UART011_CR_DTR;
1641 cr |= UART01x_CR_UARTEN | UART011_CR_TXE;
1642 writew(cr, uap->port.membase + UART011_CR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643
1644 /*
1645 * disable break condition and fifos
1646 */
Linus Walleijec489aa2010-06-02 08:13:52 +01001647 pl011_shutdown_channel(uap, uap->lcrh_rx);
1648 if (uap->lcrh_rx != uap->lcrh_tx)
1649 pl011_shutdown_channel(uap, uap->lcrh_tx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650
1651 /*
1652 * Shut down the clock producer
1653 */
Julia Lawall1c4c4392012-08-26 18:01:01 +02001654 clk_disable_unprepare(uap->clk);
Linus Walleij78d80c52012-05-23 21:18:46 +02001655 /* Optionally let pins go into sleep states */
1656 if (!IS_ERR(uap->pins_sleep)) {
1657 retval = pinctrl_select_state(uap->pinctrl, uap->pins_sleep);
1658 if (retval)
1659 dev_err(port->dev,
1660 "could not set pins to sleep state\n");
1661 }
1662
Shreshtha Kumar Sahuc16d51a2011-06-13 10:11:33 +02001663
1664 if (uap->port.dev->platform_data) {
1665 struct amba_pl011_data *plat;
1666
1667 plat = uap->port.dev->platform_data;
1668 if (plat->exit)
1669 plat->exit();
1670 }
1671
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672}
1673
1674static void
Alan Cox606d0992006-12-08 02:38:45 -08001675pl011_set_termios(struct uart_port *port, struct ktermios *termios,
1676 struct ktermios *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677{
Rabin Vincent3b438162010-02-12 06:43:11 +01001678 struct uart_amba_port *uap = (struct uart_amba_port *)port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 unsigned int lcr_h, old_cr;
1680 unsigned long flags;
Russell Kingc19f12b2010-12-22 17:48:26 +00001681 unsigned int baud, quot, clkdiv;
1682
1683 if (uap->vendor->oversampling)
1684 clkdiv = 8;
1685 else
1686 clkdiv = 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687
1688 /*
1689 * Ask the core to calculate the divisor for us.
1690 */
Linus Walleijac3e3fb2010-06-02 20:40:22 +01001691 baud = uart_get_baud_rate(port, termios, old, 0,
Russell Kingc19f12b2010-12-22 17:48:26 +00001692 port->uartclk / clkdiv);
Chanho Min89fa28d2013-04-03 11:10:37 +09001693#ifdef CONFIG_DMA_ENGINE
Chanho Mincb06ff12013-03-27 18:38:11 +09001694 /*
1695 * Adjust RX DMA polling rate with baud rate if not specified.
1696 */
1697 if (uap->dmarx.auto_poll_rate)
1698 uap->dmarx.poll_rate = DIV_ROUND_UP(10000000, baud);
Chanho Min89fa28d2013-04-03 11:10:37 +09001699#endif
Linus Walleijac3e3fb2010-06-02 20:40:22 +01001700
1701 if (baud > port->uartclk/16)
1702 quot = DIV_ROUND_CLOSEST(port->uartclk * 8, baud);
1703 else
1704 quot = DIV_ROUND_CLOSEST(port->uartclk * 4, baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705
1706 switch (termios->c_cflag & CSIZE) {
1707 case CS5:
1708 lcr_h = UART01x_LCRH_WLEN_5;
1709 break;
1710 case CS6:
1711 lcr_h = UART01x_LCRH_WLEN_6;
1712 break;
1713 case CS7:
1714 lcr_h = UART01x_LCRH_WLEN_7;
1715 break;
1716 default: // CS8
1717 lcr_h = UART01x_LCRH_WLEN_8;
1718 break;
1719 }
1720 if (termios->c_cflag & CSTOPB)
1721 lcr_h |= UART01x_LCRH_STP2;
1722 if (termios->c_cflag & PARENB) {
1723 lcr_h |= UART01x_LCRH_PEN;
1724 if (!(termios->c_cflag & PARODD))
1725 lcr_h |= UART01x_LCRH_EPS;
1726 }
Russell Kingffca2b12010-12-22 17:13:05 +00001727 if (uap->fifosize > 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 lcr_h |= UART01x_LCRH_FEN;
1729
1730 spin_lock_irqsave(&port->lock, flags);
1731
1732 /*
1733 * Update the per-port timeout.
1734 */
1735 uart_update_timeout(port, termios->c_cflag, baud);
1736
Russell Kingb63d4f02005-11-19 11:10:35 +00001737 port->read_status_mask = UART011_DR_OE | 255;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 if (termios->c_iflag & INPCK)
Russell Kingb63d4f02005-11-19 11:10:35 +00001739 port->read_status_mask |= UART011_DR_FE | UART011_DR_PE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 if (termios->c_iflag & (BRKINT | PARMRK))
Russell Kingb63d4f02005-11-19 11:10:35 +00001741 port->read_status_mask |= UART011_DR_BE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742
1743 /*
1744 * Characters to ignore
1745 */
1746 port->ignore_status_mask = 0;
1747 if (termios->c_iflag & IGNPAR)
Russell Kingb63d4f02005-11-19 11:10:35 +00001748 port->ignore_status_mask |= UART011_DR_FE | UART011_DR_PE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 if (termios->c_iflag & IGNBRK) {
Russell Kingb63d4f02005-11-19 11:10:35 +00001750 port->ignore_status_mask |= UART011_DR_BE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 /*
1752 * If we're ignoring parity and break indicators,
1753 * ignore overruns too (for real raw support).
1754 */
1755 if (termios->c_iflag & IGNPAR)
Russell Kingb63d4f02005-11-19 11:10:35 +00001756 port->ignore_status_mask |= UART011_DR_OE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 }
1758
1759 /*
1760 * Ignore all characters if CREAD is not set.
1761 */
1762 if ((termios->c_cflag & CREAD) == 0)
Russell Kingb63d4f02005-11-19 11:10:35 +00001763 port->ignore_status_mask |= UART_DUMMY_DR_RX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764
1765 if (UART_ENABLE_MS(port, termios->c_cflag))
1766 pl011_enable_ms(port);
1767
1768 /* first, disable everything */
1769 old_cr = readw(port->membase + UART011_CR);
1770 writew(0, port->membase + UART011_CR);
1771
Rabin Vincent3b438162010-02-12 06:43:11 +01001772 if (termios->c_cflag & CRTSCTS) {
1773 if (old_cr & UART011_CR_RTS)
1774 old_cr |= UART011_CR_RTSEN;
1775
1776 old_cr |= UART011_CR_CTSEN;
1777 uap->autorts = true;
1778 } else {
1779 old_cr &= ~(UART011_CR_CTSEN | UART011_CR_RTSEN);
1780 uap->autorts = false;
1781 }
1782
Russell Kingc19f12b2010-12-22 17:48:26 +00001783 if (uap->vendor->oversampling) {
1784 if (baud > port->uartclk / 16)
Linus Walleijac3e3fb2010-06-02 20:40:22 +01001785 old_cr |= ST_UART011_CR_OVSFACT;
1786 else
1787 old_cr &= ~ST_UART011_CR_OVSFACT;
1788 }
1789
Linus Walleijc5dd5532012-09-26 17:21:36 +02001790 /*
1791 * Workaround for the ST Micro oversampling variants to
1792 * increase the bitrate slightly, by lowering the divisor,
1793 * to avoid delayed sampling of start bit at high speeds,
1794 * else we see data corruption.
1795 */
1796 if (uap->vendor->oversampling) {
1797 if ((baud >= 3000000) && (baud < 3250000) && (quot > 1))
1798 quot -= 1;
1799 else if ((baud > 3250000) && (quot > 2))
1800 quot -= 2;
1801 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 /* Set baud rate */
1803 writew(quot & 0x3f, port->membase + UART011_FBRD);
1804 writew(quot >> 6, port->membase + UART011_IBRD);
1805
1806 /*
1807 * ----------v----------v----------v----------v-----
Linus Walleijc5dd5532012-09-26 17:21:36 +02001808 * NOTE: lcrh_tx and lcrh_rx MUST BE WRITTEN AFTER
1809 * UART011_FBRD & UART011_IBRD.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 * ----------^----------^----------^----------^-----
1811 */
Linus Walleijec489aa2010-06-02 08:13:52 +01001812 writew(lcr_h, port->membase + uap->lcrh_rx);
1813 if (uap->lcrh_rx != uap->lcrh_tx) {
1814 int i;
1815 /*
1816 * Wait 10 PCLKs before writing LCRH_TX register,
1817 * to get this delay write read only register 10 times
1818 */
1819 for (i = 0; i < 10; ++i)
1820 writew(0xff, uap->port.membase + UART011_MIS);
1821 writew(lcr_h, port->membase + uap->lcrh_tx);
1822 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 writew(old_cr, port->membase + UART011_CR);
1824
1825 spin_unlock_irqrestore(&port->lock, flags);
1826}
1827
1828static const char *pl011_type(struct uart_port *port)
1829{
Russell Kinge8a7ba82010-12-28 09:16:54 +00001830 struct uart_amba_port *uap = (struct uart_amba_port *)port;
1831 return uap->port.type == PORT_AMBA ? uap->type : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832}
1833
1834/*
1835 * Release the memory region(s) being used by 'port'
1836 */
Linus Walleije643f872012-06-17 15:44:19 +02001837static void pl011_release_port(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838{
1839 release_mem_region(port->mapbase, SZ_4K);
1840}
1841
1842/*
1843 * Request the memory region(s) being used by 'port'
1844 */
Linus Walleije643f872012-06-17 15:44:19 +02001845static int pl011_request_port(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846{
1847 return request_mem_region(port->mapbase, SZ_4K, "uart-pl011")
1848 != NULL ? 0 : -EBUSY;
1849}
1850
1851/*
1852 * Configure/autoconfigure the port.
1853 */
Linus Walleije643f872012-06-17 15:44:19 +02001854static void pl011_config_port(struct uart_port *port, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855{
1856 if (flags & UART_CONFIG_TYPE) {
1857 port->type = PORT_AMBA;
Linus Walleije643f872012-06-17 15:44:19 +02001858 pl011_request_port(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 }
1860}
1861
1862/*
1863 * verify the new serial_struct (for TIOCSSERIAL).
1864 */
Linus Walleije643f872012-06-17 15:44:19 +02001865static int pl011_verify_port(struct uart_port *port, struct serial_struct *ser)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866{
1867 int ret = 0;
1868 if (ser->type != PORT_UNKNOWN && ser->type != PORT_AMBA)
1869 ret = -EINVAL;
Yinghai Lua62c4132008-08-19 20:49:55 -07001870 if (ser->irq < 0 || ser->irq >= nr_irqs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 ret = -EINVAL;
1872 if (ser->baud_base < 9600)
1873 ret = -EINVAL;
1874 return ret;
1875}
1876
1877static struct uart_ops amba_pl011_pops = {
Linus Walleije643f872012-06-17 15:44:19 +02001878 .tx_empty = pl011_tx_empty,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 .set_mctrl = pl011_set_mctrl,
Linus Walleije643f872012-06-17 15:44:19 +02001880 .get_mctrl = pl011_get_mctrl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 .stop_tx = pl011_stop_tx,
1882 .start_tx = pl011_start_tx,
1883 .stop_rx = pl011_stop_rx,
1884 .enable_ms = pl011_enable_ms,
1885 .break_ctl = pl011_break_ctl,
1886 .startup = pl011_startup,
1887 .shutdown = pl011_shutdown,
Russell King68b65f72010-12-22 17:24:39 +00001888 .flush_buffer = pl011_dma_flush_buffer,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 .set_termios = pl011_set_termios,
1890 .type = pl011_type,
Linus Walleije643f872012-06-17 15:44:19 +02001891 .release_port = pl011_release_port,
1892 .request_port = pl011_request_port,
1893 .config_port = pl011_config_port,
1894 .verify_port = pl011_verify_port,
Jason Wessel84b5ae12008-02-20 13:33:39 -06001895#ifdef CONFIG_CONSOLE_POLL
Anton Vorontsovb3564c22012-09-24 14:27:54 -07001896 .poll_init = pl011_hwinit,
Linus Walleije643f872012-06-17 15:44:19 +02001897 .poll_get_char = pl011_get_poll_char,
1898 .poll_put_char = pl011_put_poll_char,
Jason Wessel84b5ae12008-02-20 13:33:39 -06001899#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900};
1901
1902static struct uart_amba_port *amba_ports[UART_NR];
1903
1904#ifdef CONFIG_SERIAL_AMBA_PL011_CONSOLE
1905
Russell Kingd3587882006-03-20 20:00:09 +00001906static void pl011_console_putchar(struct uart_port *port, int ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907{
Russell Kingd3587882006-03-20 20:00:09 +00001908 struct uart_amba_port *uap = (struct uart_amba_port *)port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909
Russell Kingd3587882006-03-20 20:00:09 +00001910 while (readw(uap->port.membase + UART01x_FR) & UART01x_FR_TXFF)
1911 barrier();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 writew(ch, uap->port.membase + UART01x_DR);
1913}
1914
1915static void
1916pl011_console_write(struct console *co, const char *s, unsigned int count)
1917{
1918 struct uart_amba_port *uap = amba_ports[co->index];
1919 unsigned int status, old_cr, new_cr;
Rabin Vincentef605fd2012-01-17 11:52:28 +01001920 unsigned long flags;
1921 int locked = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922
1923 clk_enable(uap->clk);
1924
Rabin Vincentef605fd2012-01-17 11:52:28 +01001925 local_irq_save(flags);
1926 if (uap->port.sysrq)
1927 locked = 0;
1928 else if (oops_in_progress)
1929 locked = spin_trylock(&uap->port.lock);
1930 else
1931 spin_lock(&uap->port.lock);
1932
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 /*
1934 * First save the CR then disable the interrupts
1935 */
1936 old_cr = readw(uap->port.membase + UART011_CR);
1937 new_cr = old_cr & ~UART011_CR_CTSEN;
1938 new_cr |= UART01x_CR_UARTEN | UART011_CR_TXE;
1939 writew(new_cr, uap->port.membase + UART011_CR);
1940
Russell Kingd3587882006-03-20 20:00:09 +00001941 uart_console_write(&uap->port, s, count, pl011_console_putchar);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942
1943 /*
1944 * Finally, wait for transmitter to become empty
1945 * and restore the TCR
1946 */
1947 do {
1948 status = readw(uap->port.membase + UART01x_FR);
1949 } while (status & UART01x_FR_BUSY);
1950 writew(old_cr, uap->port.membase + UART011_CR);
1951
Rabin Vincentef605fd2012-01-17 11:52:28 +01001952 if (locked)
1953 spin_unlock(&uap->port.lock);
1954 local_irq_restore(flags);
1955
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 clk_disable(uap->clk);
1957}
1958
1959static void __init
1960pl011_console_get_options(struct uart_amba_port *uap, int *baud,
1961 int *parity, int *bits)
1962{
1963 if (readw(uap->port.membase + UART011_CR) & UART01x_CR_UARTEN) {
1964 unsigned int lcr_h, ibrd, fbrd;
1965
Linus Walleijec489aa2010-06-02 08:13:52 +01001966 lcr_h = readw(uap->port.membase + uap->lcrh_tx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967
1968 *parity = 'n';
1969 if (lcr_h & UART01x_LCRH_PEN) {
1970 if (lcr_h & UART01x_LCRH_EPS)
1971 *parity = 'e';
1972 else
1973 *parity = 'o';
1974 }
1975
1976 if ((lcr_h & 0x60) == UART01x_LCRH_WLEN_7)
1977 *bits = 7;
1978 else
1979 *bits = 8;
1980
1981 ibrd = readw(uap->port.membase + UART011_IBRD);
1982 fbrd = readw(uap->port.membase + UART011_FBRD);
1983
1984 *baud = uap->port.uartclk * 4 / (64 * ibrd + fbrd);
Linus Walleijac3e3fb2010-06-02 20:40:22 +01001985
Russell Kingc19f12b2010-12-22 17:48:26 +00001986 if (uap->vendor->oversampling) {
Linus Walleijac3e3fb2010-06-02 20:40:22 +01001987 if (readw(uap->port.membase + UART011_CR)
1988 & ST_UART011_CR_OVSFACT)
1989 *baud *= 2;
1990 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 }
1992}
1993
1994static int __init pl011_console_setup(struct console *co, char *options)
1995{
1996 struct uart_amba_port *uap;
1997 int baud = 38400;
1998 int bits = 8;
1999 int parity = 'n';
2000 int flow = 'n';
Russell King4b4851c2011-09-22 11:35:30 +01002001 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002
2003 /*
2004 * Check whether an invalid uart number has been specified, and
2005 * if so, search for the first available port that does have
2006 * console support.
2007 */
2008 if (co->index >= UART_NR)
2009 co->index = 0;
2010 uap = amba_ports[co->index];
Russell Kingd28122a2007-01-22 18:59:42 +00002011 if (!uap)
2012 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013
Linus Walleij78d80c52012-05-23 21:18:46 +02002014 /* Allow pins to be muxed in and configured */
2015 if (!IS_ERR(uap->pins_default)) {
2016 ret = pinctrl_select_state(uap->pinctrl, uap->pins_default);
2017 if (ret)
2018 dev_err(uap->port.dev,
2019 "could not set default pins\n");
2020 }
2021
Russell King4b4851c2011-09-22 11:35:30 +01002022 ret = clk_prepare(uap->clk);
2023 if (ret)
2024 return ret;
2025
Shreshtha Kumar Sahuc16d51a2011-06-13 10:11:33 +02002026 if (uap->port.dev->platform_data) {
2027 struct amba_pl011_data *plat;
2028
2029 plat = uap->port.dev->platform_data;
2030 if (plat->init)
2031 plat->init();
2032 }
2033
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 uap->port.uartclk = clk_get_rate(uap->clk);
2035
2036 if (options)
2037 uart_parse_options(options, &baud, &parity, &bits, &flow);
2038 else
2039 pl011_console_get_options(uap, &baud, &parity, &bits);
2040
2041 return uart_set_options(&uap->port, co, baud, parity, bits, flow);
2042}
2043
Vincent Sanders2d934862005-09-14 22:36:03 +01002044static struct uart_driver amba_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045static struct console amba_console = {
2046 .name = "ttyAMA",
2047 .write = pl011_console_write,
2048 .device = uart_console_device,
2049 .setup = pl011_console_setup,
2050 .flags = CON_PRINTBUFFER,
2051 .index = -1,
2052 .data = &amba_reg,
2053};
2054
2055#define AMBA_CONSOLE (&amba_console)
2056#else
2057#define AMBA_CONSOLE NULL
2058#endif
2059
2060static struct uart_driver amba_reg = {
2061 .owner = THIS_MODULE,
2062 .driver_name = "ttyAMA",
2063 .dev_name = "ttyAMA",
2064 .major = SERIAL_AMBA_MAJOR,
2065 .minor = SERIAL_AMBA_MINOR,
2066 .nr = UART_NR,
2067 .cons = AMBA_CONSOLE,
2068};
2069
Matthew Leach32614aa2012-08-28 16:41:28 +01002070static int pl011_probe_dt_alias(int index, struct device *dev)
2071{
2072 struct device_node *np;
2073 static bool seen_dev_with_alias = false;
2074 static bool seen_dev_without_alias = false;
2075 int ret = index;
2076
2077 if (!IS_ENABLED(CONFIG_OF))
2078 return ret;
2079
2080 np = dev->of_node;
2081 if (!np)
2082 return ret;
2083
2084 ret = of_alias_get_id(np, "serial");
2085 if (IS_ERR_VALUE(ret)) {
2086 seen_dev_without_alias = true;
2087 ret = index;
2088 } else {
2089 seen_dev_with_alias = true;
2090 if (ret >= ARRAY_SIZE(amba_ports) || amba_ports[ret] != NULL) {
2091 dev_warn(dev, "requested serial port %d not available.\n", ret);
2092 ret = index;
2093 }
2094 }
2095
2096 if (seen_dev_with_alias && seen_dev_without_alias)
2097 dev_warn(dev, "aliased and non-aliased serial devices found in device tree. Serial port enumeration may be unpredictable.\n");
2098
2099 return ret;
2100}
2101
Russell Kingaa25afa2011-02-19 15:55:00 +00002102static int pl011_probe(struct amba_device *dev, const struct amba_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103{
2104 struct uart_amba_port *uap;
Alessandro Rubini5926a292009-06-04 17:43:04 +01002105 struct vendor_data *vendor = id->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 void __iomem *base;
2107 int i, ret;
2108
2109 for (i = 0; i < ARRAY_SIZE(amba_ports); i++)
2110 if (amba_ports[i] == NULL)
2111 break;
2112
2113 if (i == ARRAY_SIZE(amba_ports)) {
2114 ret = -EBUSY;
2115 goto out;
2116 }
2117
Linus Walleijde609582012-10-15 13:36:01 +02002118 uap = devm_kzalloc(&dev->dev, sizeof(struct uart_amba_port),
2119 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 if (uap == NULL) {
2121 ret = -ENOMEM;
2122 goto out;
2123 }
2124
Matthew Leach32614aa2012-08-28 16:41:28 +01002125 i = pl011_probe_dt_alias(i, &dev->dev);
2126
Linus Walleijde609582012-10-15 13:36:01 +02002127 base = devm_ioremap(&dev->dev, dev->res.start,
2128 resource_size(&dev->res));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 if (!base) {
2130 ret = -ENOMEM;
Linus Walleijde609582012-10-15 13:36:01 +02002131 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 }
2133
Linus Walleij78d80c52012-05-23 21:18:46 +02002134 uap->pinctrl = devm_pinctrl_get(&dev->dev);
2135 if (IS_ERR(uap->pinctrl)) {
2136 ret = PTR_ERR(uap->pinctrl);
Linus Walleijde609582012-10-15 13:36:01 +02002137 goto out;
Shawn Guo258e0552012-05-06 22:53:35 +08002138 }
Linus Walleij78d80c52012-05-23 21:18:46 +02002139 uap->pins_default = pinctrl_lookup_state(uap->pinctrl,
2140 PINCTRL_STATE_DEFAULT);
2141 if (IS_ERR(uap->pins_default))
2142 dev_err(&dev->dev, "could not get default pinstate\n");
2143
2144 uap->pins_sleep = pinctrl_lookup_state(uap->pinctrl,
2145 PINCTRL_STATE_SLEEP);
2146 if (IS_ERR(uap->pins_sleep))
2147 dev_dbg(&dev->dev, "could not get sleep pinstate\n");
Shawn Guo258e0552012-05-06 22:53:35 +08002148
Linus Walleijde609582012-10-15 13:36:01 +02002149 uap->clk = devm_clk_get(&dev->dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 if (IS_ERR(uap->clk)) {
2151 ret = PTR_ERR(uap->clk);
Linus Walleijde609582012-10-15 13:36:01 +02002152 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 }
2154
Russell Kingc19f12b2010-12-22 17:48:26 +00002155 uap->vendor = vendor;
Linus Walleijec489aa2010-06-02 08:13:52 +01002156 uap->lcrh_rx = vendor->lcrh_rx;
2157 uap->lcrh_tx = vendor->lcrh_tx;
Shreshtha Kumar Sahud8d8ffa2012-01-18 15:53:59 +05302158 uap->old_cr = 0;
Jongsung Kimea336402013-05-10 18:05:35 +09002159 uap->fifosize = vendor->get_fifosize(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 uap->port.dev = &dev->dev;
2161 uap->port.mapbase = dev->res.start;
2162 uap->port.membase = base;
2163 uap->port.iotype = UPIO_MEM;
2164 uap->port.irq = dev->irq[0];
Russell Kingffca2b12010-12-22 17:13:05 +00002165 uap->port.fifosize = uap->fifosize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 uap->port.ops = &amba_pl011_pops;
2167 uap->port.flags = UPF_BOOT_AUTOCONF;
2168 uap->port.line = i;
Arnd Bergmann787b0c12013-01-28 16:24:37 +00002169 pl011_dma_probe(&dev->dev, uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170
Linus Walleijc3d8b762012-03-21 20:15:18 +01002171 /* Ensure interrupts from this UART are masked and cleared */
2172 writew(0, uap->port.membase + UART011_IMSC);
2173 writew(0xffff, uap->port.membase + UART011_ICR);
2174
Russell Kinge8a7ba82010-12-28 09:16:54 +00002175 snprintf(uap->type, sizeof(uap->type), "PL011 rev%u", amba_rev(dev));
2176
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 amba_ports[i] = uap;
2178
2179 amba_set_drvdata(dev, uap);
2180 ret = uart_add_one_port(&amba_reg, &uap->port);
2181 if (ret) {
2182 amba_set_drvdata(dev, NULL);
2183 amba_ports[i] = NULL;
Russell King68b65f72010-12-22 17:24:39 +00002184 pl011_dma_remove(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 }
2186 out:
2187 return ret;
2188}
2189
2190static int pl011_remove(struct amba_device *dev)
2191{
2192 struct uart_amba_port *uap = amba_get_drvdata(dev);
2193 int i;
2194
2195 amba_set_drvdata(dev, NULL);
2196
2197 uart_remove_one_port(&amba_reg, &uap->port);
2198
2199 for (i = 0; i < ARRAY_SIZE(amba_ports); i++)
2200 if (amba_ports[i] == uap)
2201 amba_ports[i] = NULL;
2202
Russell King68b65f72010-12-22 17:24:39 +00002203 pl011_dma_remove(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 return 0;
2205}
2206
Leo Chenb736b892009-07-28 23:43:33 +01002207#ifdef CONFIG_PM
2208static int pl011_suspend(struct amba_device *dev, pm_message_t state)
2209{
2210 struct uart_amba_port *uap = amba_get_drvdata(dev);
2211
2212 if (!uap)
2213 return -EINVAL;
2214
2215 return uart_suspend_port(&amba_reg, &uap->port);
2216}
2217
2218static int pl011_resume(struct amba_device *dev)
2219{
2220 struct uart_amba_port *uap = amba_get_drvdata(dev);
2221
2222 if (!uap)
2223 return -EINVAL;
2224
2225 return uart_resume_port(&amba_reg, &uap->port);
2226}
2227#endif
2228
Russell King2c39c9e2010-07-27 08:50:16 +01002229static struct amba_id pl011_ids[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 {
2231 .id = 0x00041011,
2232 .mask = 0x000fffff,
Alessandro Rubini5926a292009-06-04 17:43:04 +01002233 .data = &vendor_arm,
2234 },
2235 {
2236 .id = 0x00380802,
2237 .mask = 0x00ffffff,
2238 .data = &vendor_st,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 },
2240 { 0, 0 },
2241};
2242
Dave Martin60f7a332011-10-05 15:15:22 +01002243MODULE_DEVICE_TABLE(amba, pl011_ids);
2244
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245static struct amba_driver pl011_driver = {
2246 .drv = {
2247 .name = "uart-pl011",
2248 },
2249 .id_table = pl011_ids,
2250 .probe = pl011_probe,
2251 .remove = pl011_remove,
Leo Chenb736b892009-07-28 23:43:33 +01002252#ifdef CONFIG_PM
2253 .suspend = pl011_suspend,
2254 .resume = pl011_resume,
2255#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256};
2257
2258static int __init pl011_init(void)
2259{
2260 int ret;
2261 printk(KERN_INFO "Serial: AMBA PL011 UART driver\n");
2262
2263 ret = uart_register_driver(&amba_reg);
2264 if (ret == 0) {
2265 ret = amba_driver_register(&pl011_driver);
2266 if (ret)
2267 uart_unregister_driver(&amba_reg);
2268 }
2269 return ret;
2270}
2271
2272static void __exit pl011_exit(void)
2273{
2274 amba_driver_unregister(&pl011_driver);
2275 uart_unregister_driver(&amba_reg);
2276}
2277
Alessandro Rubini4dd9e742009-05-05 05:54:13 +01002278/*
2279 * While this can be a module, if builtin it's most likely the console
2280 * So let's leave module_exit but move module_init to an earlier place
2281 */
2282arch_initcall(pl011_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283module_exit(pl011_exit);
2284
2285MODULE_AUTHOR("ARM Ltd/Deep Blue Solutions Ltd");
2286MODULE_DESCRIPTION("ARM AMBA serial port driver");
2287MODULE_LICENSE("GPL");