blob: cb45136f6867af16178a6773b550d1c434306b92 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/drivers/char/amba.c
3 *
4 * Driver for AMBA serial ports
5 *
6 * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
7 *
8 * Copyright 1999 ARM Limited
9 * Copyright (C) 2000 Deep Blue Solutions Ltd.
Russell King68b65f72010-12-22 17:24:39 +000010 * Copyright (C) 2010 ST-Ericsson SA
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 * This is a generic driver for ARM AMBA-type serial ports. They
27 * have a lot of 16550-like features, but are not register compatible.
28 * Note that although they do have CTS, DCD and DSR inputs, they do
29 * not have an RI input, nor do they have DTR or RTS outputs. If
30 * required, these have to be supplied via some other means (eg, GPIO)
31 * and hooked into this driver.
32 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
34#if defined(CONFIG_SERIAL_AMBA_PL011_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
35#define SUPPORT_SYSRQ
36#endif
37
38#include <linux/module.h>
39#include <linux/ioport.h>
40#include <linux/init.h>
41#include <linux/console.h>
42#include <linux/sysrq.h>
43#include <linux/device.h>
44#include <linux/tty.h>
45#include <linux/tty_flip.h>
46#include <linux/serial_core.h>
47#include <linux/serial.h>
Russell Kinga62c80e2006-01-07 13:52:45 +000048#include <linux/amba/bus.h>
49#include <linux/amba/serial.h>
Russell Kingf8ce2542006-01-07 16:15:52 +000050#include <linux/clk.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090051#include <linux/slab.h>
Russell King68b65f72010-12-22 17:24:39 +000052#include <linux/dmaengine.h>
53#include <linux/dma-mapping.h>
54#include <linux/scatterlist.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
56#include <asm/io.h>
Russell Kingc6b8fda2005-10-28 14:05:16 +010057#include <asm/sizes.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
59#define UART_NR 14
60
61#define SERIAL_AMBA_MAJOR 204
62#define SERIAL_AMBA_MINOR 64
63#define SERIAL_AMBA_NR UART_NR
64
65#define AMBA_ISR_PASS_LIMIT 256
66
Russell Kingb63d4f02005-11-19 11:10:35 +000067#define UART_DR_ERROR (UART011_DR_OE|UART011_DR_BE|UART011_DR_PE|UART011_DR_FE)
68#define UART_DUMMY_DR_RX (1 << 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Alessandro Rubini5926a292009-06-04 17:43:04 +010070/* There is by now at least one vendor with differing details, so handle it */
71struct vendor_data {
72 unsigned int ifls;
73 unsigned int fifosize;
Linus Walleijec489aa2010-06-02 08:13:52 +010074 unsigned int lcrh_tx;
75 unsigned int lcrh_rx;
Linus Walleijac3e3fb2010-06-02 20:40:22 +010076 bool oversampling;
Russell King38d62432010-12-22 17:59:16 +000077 bool dma_threshold;
Alessandro Rubini5926a292009-06-04 17:43:04 +010078};
79
80static struct vendor_data vendor_arm = {
81 .ifls = UART011_IFLS_RX4_8|UART011_IFLS_TX4_8,
82 .fifosize = 16,
Linus Walleijec489aa2010-06-02 08:13:52 +010083 .lcrh_tx = UART011_LCRH,
84 .lcrh_rx = UART011_LCRH,
Linus Walleijac3e3fb2010-06-02 20:40:22 +010085 .oversampling = false,
Russell King38d62432010-12-22 17:59:16 +000086 .dma_threshold = false,
Alessandro Rubini5926a292009-06-04 17:43:04 +010087};
88
89static struct vendor_data vendor_st = {
90 .ifls = UART011_IFLS_RX_HALF|UART011_IFLS_TX_HALF,
91 .fifosize = 64,
Linus Walleijec489aa2010-06-02 08:13:52 +010092 .lcrh_tx = ST_UART011_LCRH_TX,
93 .lcrh_rx = ST_UART011_LCRH_RX,
Linus Walleijac3e3fb2010-06-02 20:40:22 +010094 .oversampling = true,
Russell King38d62432010-12-22 17:59:16 +000095 .dma_threshold = true,
Linus Torvalds1da177e2005-04-16 15:20:36 -070096};
97
Russell King68b65f72010-12-22 17:24:39 +000098/* Deals with DMA transactions */
Linus Walleijead76f32011-02-24 13:21:08 +010099
100struct pl011_sgbuf {
101 struct scatterlist sg;
102 char *buf;
103};
104
105struct pl011_dmarx_data {
106 struct dma_chan *chan;
107 struct completion complete;
108 bool use_buf_b;
109 struct pl011_sgbuf sgbuf_a;
110 struct pl011_sgbuf sgbuf_b;
111 dma_cookie_t cookie;
112 bool running;
113};
114
Russell King68b65f72010-12-22 17:24:39 +0000115struct pl011_dmatx_data {
116 struct dma_chan *chan;
117 struct scatterlist sg;
118 char *buf;
119 bool queued;
120};
121
Russell Kingc19f12b2010-12-22 17:48:26 +0000122/*
123 * We wrap our port structure around the generic uart_port.
124 */
125struct uart_amba_port {
126 struct uart_port port;
127 struct clk *clk;
128 const struct vendor_data *vendor;
Russell King68b65f72010-12-22 17:24:39 +0000129 unsigned int dmacr; /* dma control reg */
Russell Kingc19f12b2010-12-22 17:48:26 +0000130 unsigned int im; /* interrupt mask */
131 unsigned int old_status;
Russell Kingffca2b12010-12-22 17:13:05 +0000132 unsigned int fifosize; /* vendor-specific */
Russell Kingc19f12b2010-12-22 17:48:26 +0000133 unsigned int lcrh_tx; /* vendor-specific */
134 unsigned int lcrh_rx; /* vendor-specific */
135 bool autorts;
136 char type[12];
Russell King68b65f72010-12-22 17:24:39 +0000137#ifdef CONFIG_DMA_ENGINE
138 /* DMA stuff */
Linus Walleijead76f32011-02-24 13:21:08 +0100139 bool using_tx_dma;
140 bool using_rx_dma;
141 struct pl011_dmarx_data dmarx;
Russell King68b65f72010-12-22 17:24:39 +0000142 struct pl011_dmatx_data dmatx;
143#endif
Russell Kingc19f12b2010-12-22 17:48:26 +0000144};
145
Russell King68b65f72010-12-22 17:24:39 +0000146/*
147 * All the DMA operation mode stuff goes inside this ifdef.
148 * This assumes that you have a generic DMA device interface,
149 * no custom DMA interfaces are supported.
150 */
151#ifdef CONFIG_DMA_ENGINE
152
153#define PL011_DMA_BUFFER_SIZE PAGE_SIZE
154
Linus Walleijead76f32011-02-24 13:21:08 +0100155static int pl011_sgbuf_init(struct dma_chan *chan, struct pl011_sgbuf *sg,
156 enum dma_data_direction dir)
157{
158 sg->buf = kmalloc(PL011_DMA_BUFFER_SIZE, GFP_KERNEL);
159 if (!sg->buf)
160 return -ENOMEM;
161
162 sg_init_one(&sg->sg, sg->buf, PL011_DMA_BUFFER_SIZE);
163
164 if (dma_map_sg(chan->device->dev, &sg->sg, 1, dir) != 1) {
165 kfree(sg->buf);
166 return -EINVAL;
167 }
168 return 0;
169}
170
171static void pl011_sgbuf_free(struct dma_chan *chan, struct pl011_sgbuf *sg,
172 enum dma_data_direction dir)
173{
174 if (sg->buf) {
175 dma_unmap_sg(chan->device->dev, &sg->sg, 1, dir);
176 kfree(sg->buf);
177 }
178}
179
Russell King68b65f72010-12-22 17:24:39 +0000180static void pl011_dma_probe_initcall(struct uart_amba_port *uap)
181{
182 /* DMA is the sole user of the platform data right now */
183 struct amba_pl011_data *plat = uap->port.dev->platform_data;
184 struct dma_slave_config tx_conf = {
185 .dst_addr = uap->port.mapbase + UART01x_DR,
186 .dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE,
187 .direction = DMA_TO_DEVICE,
188 .dst_maxburst = uap->fifosize >> 1,
189 };
190 struct dma_chan *chan;
191 dma_cap_mask_t mask;
192
193 /* We need platform data */
194 if (!plat || !plat->dma_filter) {
195 dev_info(uap->port.dev, "no DMA platform data\n");
196 return;
197 }
198
Linus Walleijead76f32011-02-24 13:21:08 +0100199 /* Try to acquire a generic DMA engine slave TX channel */
Russell King68b65f72010-12-22 17:24:39 +0000200 dma_cap_zero(mask);
201 dma_cap_set(DMA_SLAVE, mask);
202
203 chan = dma_request_channel(mask, plat->dma_filter, plat->dma_tx_param);
204 if (!chan) {
205 dev_err(uap->port.dev, "no TX DMA channel!\n");
206 return;
207 }
208
209 dmaengine_slave_config(chan, &tx_conf);
210 uap->dmatx.chan = chan;
211
212 dev_info(uap->port.dev, "DMA channel TX %s\n",
213 dma_chan_name(uap->dmatx.chan));
Linus Walleijead76f32011-02-24 13:21:08 +0100214
215 /* Optionally make use of an RX channel as well */
216 if (plat->dma_rx_param) {
217 struct dma_slave_config rx_conf = {
218 .src_addr = uap->port.mapbase + UART01x_DR,
219 .src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE,
220 .direction = DMA_FROM_DEVICE,
221 .src_maxburst = uap->fifosize >> 1,
222 };
223
224 chan = dma_request_channel(mask, plat->dma_filter, plat->dma_rx_param);
225 if (!chan) {
226 dev_err(uap->port.dev, "no RX DMA channel!\n");
227 return;
228 }
229
230 dmaengine_slave_config(chan, &rx_conf);
231 uap->dmarx.chan = chan;
232
233 dev_info(uap->port.dev, "DMA channel RX %s\n",
234 dma_chan_name(uap->dmarx.chan));
235 }
Russell King68b65f72010-12-22 17:24:39 +0000236}
237
238#ifndef MODULE
239/*
240 * Stack up the UARTs and let the above initcall be done at device
241 * initcall time, because the serial driver is called as an arch
242 * initcall, and at this time the DMA subsystem is not yet registered.
243 * At this point the driver will switch over to using DMA where desired.
244 */
245struct dma_uap {
246 struct list_head node;
247 struct uart_amba_port *uap;
248};
249
250static LIST_HEAD(pl011_dma_uarts);
251
252static int __init pl011_dma_initcall(void)
253{
254 struct list_head *node, *tmp;
255
256 list_for_each_safe(node, tmp, &pl011_dma_uarts) {
257 struct dma_uap *dmau = list_entry(node, struct dma_uap, node);
258 pl011_dma_probe_initcall(dmau->uap);
259 list_del(node);
260 kfree(dmau);
261 }
262 return 0;
263}
264
265device_initcall(pl011_dma_initcall);
266
267static void pl011_dma_probe(struct uart_amba_port *uap)
268{
269 struct dma_uap *dmau = kzalloc(sizeof(struct dma_uap), GFP_KERNEL);
270 if (dmau) {
271 dmau->uap = uap;
272 list_add_tail(&dmau->node, &pl011_dma_uarts);
273 }
274}
275#else
276static void pl011_dma_probe(struct uart_amba_port *uap)
277{
278 pl011_dma_probe_initcall(uap);
279}
280#endif
281
282static void pl011_dma_remove(struct uart_amba_port *uap)
283{
284 /* TODO: remove the initcall if it has not yet executed */
285 if (uap->dmatx.chan)
286 dma_release_channel(uap->dmatx.chan);
Linus Walleijead76f32011-02-24 13:21:08 +0100287 if (uap->dmarx.chan)
288 dma_release_channel(uap->dmarx.chan);
Russell King68b65f72010-12-22 17:24:39 +0000289}
290
Russell King68b65f72010-12-22 17:24:39 +0000291/* Forward declare this for the refill routine */
292static int pl011_dma_tx_refill(struct uart_amba_port *uap);
293
294/*
295 * The current DMA TX buffer has been sent.
296 * Try to queue up another DMA buffer.
297 */
298static void pl011_dma_tx_callback(void *data)
299{
300 struct uart_amba_port *uap = data;
301 struct pl011_dmatx_data *dmatx = &uap->dmatx;
302 unsigned long flags;
303 u16 dmacr;
304
305 spin_lock_irqsave(&uap->port.lock, flags);
306 if (uap->dmatx.queued)
307 dma_unmap_sg(dmatx->chan->device->dev, &dmatx->sg, 1,
308 DMA_TO_DEVICE);
309
310 dmacr = uap->dmacr;
311 uap->dmacr = dmacr & ~UART011_TXDMAE;
312 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
313
314 /*
315 * If TX DMA was disabled, it means that we've stopped the DMA for
316 * some reason (eg, XOFF received, or we want to send an X-char.)
317 *
318 * Note: we need to be careful here of a potential race between DMA
319 * and the rest of the driver - if the driver disables TX DMA while
320 * a TX buffer completing, we must update the tx queued status to
321 * get further refills (hence we check dmacr).
322 */
323 if (!(dmacr & UART011_TXDMAE) || uart_tx_stopped(&uap->port) ||
324 uart_circ_empty(&uap->port.state->xmit)) {
325 uap->dmatx.queued = false;
326 spin_unlock_irqrestore(&uap->port.lock, flags);
327 return;
328 }
329
330 if (pl011_dma_tx_refill(uap) <= 0) {
331 /*
332 * We didn't queue a DMA buffer for some reason, but we
333 * have data pending to be sent. Re-enable the TX IRQ.
334 */
335 uap->im |= UART011_TXIM;
336 writew(uap->im, uap->port.membase + UART011_IMSC);
337 }
338 spin_unlock_irqrestore(&uap->port.lock, flags);
339}
340
341/*
342 * Try to refill the TX DMA buffer.
343 * Locking: called with port lock held and IRQs disabled.
344 * Returns:
345 * 1 if we queued up a TX DMA buffer.
346 * 0 if we didn't want to handle this by DMA
347 * <0 on error
348 */
349static int pl011_dma_tx_refill(struct uart_amba_port *uap)
350{
351 struct pl011_dmatx_data *dmatx = &uap->dmatx;
352 struct dma_chan *chan = dmatx->chan;
353 struct dma_device *dma_dev = chan->device;
354 struct dma_async_tx_descriptor *desc;
355 struct circ_buf *xmit = &uap->port.state->xmit;
356 unsigned int count;
357
358 /*
359 * Try to avoid the overhead involved in using DMA if the
360 * transaction fits in the first half of the FIFO, by using
361 * the standard interrupt handling. This ensures that we
362 * issue a uart_write_wakeup() at the appropriate time.
363 */
364 count = uart_circ_chars_pending(xmit);
365 if (count < (uap->fifosize >> 1)) {
366 uap->dmatx.queued = false;
367 return 0;
368 }
369
370 /*
371 * Bodge: don't send the last character by DMA, as this
372 * will prevent XON from notifying us to restart DMA.
373 */
374 count -= 1;
375
376 /* Else proceed to copy the TX chars to the DMA buffer and fire DMA */
377 if (count > PL011_DMA_BUFFER_SIZE)
378 count = PL011_DMA_BUFFER_SIZE;
379
380 if (xmit->tail < xmit->head)
381 memcpy(&dmatx->buf[0], &xmit->buf[xmit->tail], count);
382 else {
383 size_t first = UART_XMIT_SIZE - xmit->tail;
384 size_t second = xmit->head;
385
386 memcpy(&dmatx->buf[0], &xmit->buf[xmit->tail], first);
387 if (second)
388 memcpy(&dmatx->buf[first], &xmit->buf[0], second);
389 }
390
391 dmatx->sg.length = count;
392
393 if (dma_map_sg(dma_dev->dev, &dmatx->sg, 1, DMA_TO_DEVICE) != 1) {
394 uap->dmatx.queued = false;
395 dev_dbg(uap->port.dev, "unable to map TX DMA\n");
396 return -EBUSY;
397 }
398
399 desc = dma_dev->device_prep_slave_sg(chan, &dmatx->sg, 1, DMA_TO_DEVICE,
400 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
401 if (!desc) {
402 dma_unmap_sg(dma_dev->dev, &dmatx->sg, 1, DMA_TO_DEVICE);
403 uap->dmatx.queued = false;
404 /*
405 * If DMA cannot be used right now, we complete this
406 * transaction via IRQ and let the TTY layer retry.
407 */
408 dev_dbg(uap->port.dev, "TX DMA busy\n");
409 return -EBUSY;
410 }
411
412 /* Some data to go along to the callback */
413 desc->callback = pl011_dma_tx_callback;
414 desc->callback_param = uap;
415
416 /* All errors should happen at prepare time */
417 dmaengine_submit(desc);
418
419 /* Fire the DMA transaction */
420 dma_dev->device_issue_pending(chan);
421
422 uap->dmacr |= UART011_TXDMAE;
423 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
424 uap->dmatx.queued = true;
425
426 /*
427 * Now we know that DMA will fire, so advance the ring buffer
428 * with the stuff we just dispatched.
429 */
430 xmit->tail = (xmit->tail + count) & (UART_XMIT_SIZE - 1);
431 uap->port.icount.tx += count;
432
433 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
434 uart_write_wakeup(&uap->port);
435
436 return 1;
437}
438
439/*
440 * We received a transmit interrupt without a pending X-char but with
441 * pending characters.
442 * Locking: called with port lock held and IRQs disabled.
443 * Returns:
444 * false if we want to use PIO to transmit
445 * true if we queued a DMA buffer
446 */
447static bool pl011_dma_tx_irq(struct uart_amba_port *uap)
448{
Linus Walleijead76f32011-02-24 13:21:08 +0100449 if (!uap->using_tx_dma)
Russell King68b65f72010-12-22 17:24:39 +0000450 return false;
451
452 /*
453 * If we already have a TX buffer queued, but received a
454 * TX interrupt, it will be because we've just sent an X-char.
455 * Ensure the TX DMA is enabled and the TX IRQ is disabled.
456 */
457 if (uap->dmatx.queued) {
458 uap->dmacr |= UART011_TXDMAE;
459 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
460 uap->im &= ~UART011_TXIM;
461 writew(uap->im, uap->port.membase + UART011_IMSC);
462 return true;
463 }
464
465 /*
466 * We don't have a TX buffer queued, so try to queue one.
467 * If we succesfully queued a buffer, mask the TX IRQ.
468 */
469 if (pl011_dma_tx_refill(uap) > 0) {
470 uap->im &= ~UART011_TXIM;
471 writew(uap->im, uap->port.membase + UART011_IMSC);
472 return true;
473 }
474 return false;
475}
476
477/*
478 * Stop the DMA transmit (eg, due to received XOFF).
479 * Locking: called with port lock held and IRQs disabled.
480 */
481static inline void pl011_dma_tx_stop(struct uart_amba_port *uap)
482{
483 if (uap->dmatx.queued) {
484 uap->dmacr &= ~UART011_TXDMAE;
485 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
486 }
487}
488
489/*
490 * Try to start a DMA transmit, or in the case of an XON/OFF
491 * character queued for send, try to get that character out ASAP.
492 * Locking: called with port lock held and IRQs disabled.
493 * Returns:
494 * false if we want the TX IRQ to be enabled
495 * true if we have a buffer queued
496 */
497static inline bool pl011_dma_tx_start(struct uart_amba_port *uap)
498{
499 u16 dmacr;
500
Linus Walleijead76f32011-02-24 13:21:08 +0100501 if (!uap->using_tx_dma)
Russell King68b65f72010-12-22 17:24:39 +0000502 return false;
503
504 if (!uap->port.x_char) {
505 /* no X-char, try to push chars out in DMA mode */
506 bool ret = true;
507
508 if (!uap->dmatx.queued) {
509 if (pl011_dma_tx_refill(uap) > 0) {
510 uap->im &= ~UART011_TXIM;
511 ret = true;
512 } else {
513 uap->im |= UART011_TXIM;
514 ret = false;
515 }
516 writew(uap->im, uap->port.membase + UART011_IMSC);
517 } else if (!(uap->dmacr & UART011_TXDMAE)) {
518 uap->dmacr |= UART011_TXDMAE;
519 writew(uap->dmacr,
520 uap->port.membase + UART011_DMACR);
521 }
522 return ret;
523 }
524
525 /*
526 * We have an X-char to send. Disable DMA to prevent it loading
527 * the TX fifo, and then see if we can stuff it into the FIFO.
528 */
529 dmacr = uap->dmacr;
530 uap->dmacr &= ~UART011_TXDMAE;
531 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
532
533 if (readw(uap->port.membase + UART01x_FR) & UART01x_FR_TXFF) {
534 /*
535 * No space in the FIFO, so enable the transmit interrupt
536 * so we know when there is space. Note that once we've
537 * loaded the character, we should just re-enable DMA.
538 */
539 return false;
540 }
541
542 writew(uap->port.x_char, uap->port.membase + UART01x_DR);
543 uap->port.icount.tx++;
544 uap->port.x_char = 0;
545
546 /* Success - restore the DMA state */
547 uap->dmacr = dmacr;
548 writew(dmacr, uap->port.membase + UART011_DMACR);
549
550 return true;
551}
552
553/*
554 * Flush the transmit buffer.
555 * Locking: called with port lock held and IRQs disabled.
556 */
557static void pl011_dma_flush_buffer(struct uart_port *port)
558{
559 struct uart_amba_port *uap = (struct uart_amba_port *)port;
560
Linus Walleijead76f32011-02-24 13:21:08 +0100561 if (!uap->using_tx_dma)
Russell King68b65f72010-12-22 17:24:39 +0000562 return;
563
564 /* Avoid deadlock with the DMA engine callback */
565 spin_unlock(&uap->port.lock);
566 dmaengine_terminate_all(uap->dmatx.chan);
567 spin_lock(&uap->port.lock);
568 if (uap->dmatx.queued) {
569 dma_unmap_sg(uap->dmatx.chan->device->dev, &uap->dmatx.sg, 1,
570 DMA_TO_DEVICE);
571 uap->dmatx.queued = false;
572 uap->dmacr &= ~UART011_TXDMAE;
573 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
574 }
575}
576
Linus Walleijead76f32011-02-24 13:21:08 +0100577static void pl011_dma_rx_callback(void *data);
578
579static int pl011_dma_rx_trigger_dma(struct uart_amba_port *uap)
580{
581 struct dma_chan *rxchan = uap->dmarx.chan;
582 struct dma_device *dma_dev;
583 struct pl011_dmarx_data *dmarx = &uap->dmarx;
584 struct dma_async_tx_descriptor *desc;
585 struct pl011_sgbuf *sgbuf;
586
587 if (!rxchan)
588 return -EIO;
589
590 /* Start the RX DMA job */
591 sgbuf = uap->dmarx.use_buf_b ?
592 &uap->dmarx.sgbuf_b : &uap->dmarx.sgbuf_a;
593 dma_dev = rxchan->device;
594 desc = rxchan->device->device_prep_slave_sg(rxchan, &sgbuf->sg, 1,
595 DMA_FROM_DEVICE,
596 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
597 /*
598 * If the DMA engine is busy and cannot prepare a
599 * channel, no big deal, the driver will fall back
600 * to interrupt mode as a result of this error code.
601 */
602 if (!desc) {
603 uap->dmarx.running = false;
604 dmaengine_terminate_all(rxchan);
605 return -EBUSY;
606 }
607
608 /* Some data to go along to the callback */
609 desc->callback = pl011_dma_rx_callback;
610 desc->callback_param = uap;
611 dmarx->cookie = dmaengine_submit(desc);
612 dma_async_issue_pending(rxchan);
613
614 uap->dmacr |= UART011_RXDMAE;
615 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
616 uap->dmarx.running = true;
617
618 uap->im &= ~UART011_RXIM;
619 writew(uap->im, uap->port.membase + UART011_IMSC);
620
621 return 0;
622}
623
624/*
625 * This is called when either the DMA job is complete, or
626 * the FIFO timeout interrupt occurred. This must be called
627 * with the port spinlock uap->port.lock held.
628 */
629static void pl011_dma_rx_chars(struct uart_amba_port *uap,
630 u32 pending, bool use_buf_b,
631 bool readfifo)
632{
633 struct tty_struct *tty = uap->port.state->port.tty;
634 struct pl011_sgbuf *sgbuf = use_buf_b ?
635 &uap->dmarx.sgbuf_b : &uap->dmarx.sgbuf_a;
636 struct device *dev = uap->dmarx.chan->device->dev;
637 unsigned int status, ch, flag;
638 int dma_count = 0;
639 u32 fifotaken = 0; /* only used for vdbg() */
640
641 /* Pick everything from the DMA first */
642 if (pending) {
643 /* Sync in buffer */
644 dma_sync_sg_for_cpu(dev, &sgbuf->sg, 1, DMA_FROM_DEVICE);
645
646 /*
647 * First take all chars in the DMA pipe, then look in the FIFO.
648 * Note that tty_insert_flip_buf() tries to take as many chars
649 * as it can.
650 */
651 dma_count = tty_insert_flip_string(uap->port.state->port.tty,
652 sgbuf->buf, pending);
653
654 /* Return buffer to device */
655 dma_sync_sg_for_device(dev, &sgbuf->sg, 1, DMA_FROM_DEVICE);
656
657 uap->port.icount.rx += dma_count;
658 if (dma_count < pending)
659 dev_warn(uap->port.dev,
660 "couldn't insert all characters (TTY is full?)\n");
661 }
662
663 /*
664 * Only continue with trying to read the FIFO if all DMA chars have
665 * been taken first.
666 */
667 if (dma_count == pending && readfifo) {
668 /* Clear any error flags */
669 writew(UART011_OEIS | UART011_BEIS | UART011_PEIS | UART011_FEIS,
670 uap->port.membase + UART011_ICR);
671
672 /*
673 * If we read all the DMA'd characters, and we had an
674 * incomplete buffer, that could be due to an rx error,
675 * or maybe we just timed out. Read any pending chars
676 * and check the error status.
677 */
678 while (1) {
679 status = readw(uap->port.membase + UART01x_FR);
680 if (status & UART01x_FR_RXFE)
681 break;
682
683 /* Take chars from the FIFO and update status */
684 ch = readw(uap->port.membase + UART01x_DR) |
685 UART_DUMMY_DR_RX;
686 flag = TTY_NORMAL;
687 uap->port.icount.rx++;
688 fifotaken++;
689
690 /*
691 * Error conditions will only occur in the FIFO,
692 * these will trigger an immediate interrupt and
693 * stop the DMA job, so we will always find the
694 * error in the FIFO, never in the DMA buffer.
695 */
696 if (unlikely(ch & UART_DR_ERROR)) {
697 if (ch & UART011_DR_BE) {
698 ch &= ~(UART011_DR_FE | UART011_DR_PE);
699 uap->port.icount.brk++;
700 if (uart_handle_break(&uap->port))
701 continue;
702 } else if (ch & UART011_DR_PE)
703 uap->port.icount.parity++;
704 else if (ch & UART011_DR_FE)
705 uap->port.icount.frame++;
706 if (ch & UART011_DR_OE)
707 uap->port.icount.overrun++;
708
709 ch &= uap->port.read_status_mask;
710
711 if (ch & UART011_DR_BE)
712 flag = TTY_BREAK;
713 else if (ch & UART011_DR_PE)
714 flag = TTY_PARITY;
715 else if (ch & UART011_DR_FE)
716 flag = TTY_FRAME;
717 }
718
719 if (uart_handle_sysrq_char(&uap->port, ch & 255))
720 continue;
721
722 uart_insert_char(&uap->port, ch, UART011_DR_OE, ch, flag);
723 }
724 }
725
726 spin_unlock(&uap->port.lock);
727 dev_vdbg(uap->port.dev,
728 "Took %d chars from DMA buffer and %d chars from the FIFO\n",
729 dma_count, fifotaken);
730 tty_flip_buffer_push(tty);
731 spin_lock(&uap->port.lock);
732}
733
734static void pl011_dma_rx_irq(struct uart_amba_port *uap)
735{
736 struct pl011_dmarx_data *dmarx = &uap->dmarx;
737 struct dma_chan *rxchan = dmarx->chan;
738 struct pl011_sgbuf *sgbuf = dmarx->use_buf_b ?
739 &dmarx->sgbuf_b : &dmarx->sgbuf_a;
740 size_t pending;
741 struct dma_tx_state state;
742 enum dma_status dmastat;
743
744 /*
745 * Pause the transfer so we can trust the current counter,
746 * do this before we pause the PL011 block, else we may
747 * overflow the FIFO.
748 */
749 if (dmaengine_pause(rxchan))
750 dev_err(uap->port.dev, "unable to pause DMA transfer\n");
751 dmastat = rxchan->device->device_tx_status(rxchan,
752 dmarx->cookie, &state);
753 if (dmastat != DMA_PAUSED)
754 dev_err(uap->port.dev, "unable to pause DMA transfer\n");
755
756 /* Disable RX DMA - incoming data will wait in the FIFO */
757 uap->dmacr &= ~UART011_RXDMAE;
758 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
759 uap->dmarx.running = false;
760
761 pending = sgbuf->sg.length - state.residue;
762 BUG_ON(pending > PL011_DMA_BUFFER_SIZE);
763 /* Then we terminate the transfer - we now know our residue */
764 dmaengine_terminate_all(rxchan);
765
766 /*
767 * This will take the chars we have so far and insert
768 * into the framework.
769 */
770 pl011_dma_rx_chars(uap, pending, dmarx->use_buf_b, true);
771
772 /* Switch buffer & re-trigger DMA job */
773 dmarx->use_buf_b = !dmarx->use_buf_b;
774 if (pl011_dma_rx_trigger_dma(uap)) {
775 dev_dbg(uap->port.dev, "could not retrigger RX DMA job "
776 "fall back to interrupt mode\n");
777 uap->im |= UART011_RXIM;
778 writew(uap->im, uap->port.membase + UART011_IMSC);
779 }
780}
781
782static void pl011_dma_rx_callback(void *data)
783{
784 struct uart_amba_port *uap = data;
785 struct pl011_dmarx_data *dmarx = &uap->dmarx;
786 bool lastbuf = dmarx->use_buf_b;
787 int ret;
788
789 /*
790 * This completion interrupt occurs typically when the
791 * RX buffer is totally stuffed but no timeout has yet
792 * occurred. When that happens, we just want the RX
793 * routine to flush out the secondary DMA buffer while
794 * we immediately trigger the next DMA job.
795 */
796 spin_lock_irq(&uap->port.lock);
797 uap->dmarx.running = false;
798 dmarx->use_buf_b = !lastbuf;
799 ret = pl011_dma_rx_trigger_dma(uap);
800
801 pl011_dma_rx_chars(uap, PL011_DMA_BUFFER_SIZE, lastbuf, false);
802 spin_unlock_irq(&uap->port.lock);
803 /*
804 * Do this check after we picked the DMA chars so we don't
805 * get some IRQ immediately from RX.
806 */
807 if (ret) {
808 dev_dbg(uap->port.dev, "could not retrigger RX DMA job "
809 "fall back to interrupt mode\n");
810 uap->im |= UART011_RXIM;
811 writew(uap->im, uap->port.membase + UART011_IMSC);
812 }
813}
814
815/*
816 * Stop accepting received characters, when we're shutting down or
817 * suspending this port.
818 * Locking: called with port lock held and IRQs disabled.
819 */
820static inline void pl011_dma_rx_stop(struct uart_amba_port *uap)
821{
822 /* FIXME. Just disable the DMA enable */
823 uap->dmacr &= ~UART011_RXDMAE;
824 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
825}
Russell King68b65f72010-12-22 17:24:39 +0000826
827static void pl011_dma_startup(struct uart_amba_port *uap)
828{
Linus Walleijead76f32011-02-24 13:21:08 +0100829 int ret;
830
Russell King68b65f72010-12-22 17:24:39 +0000831 if (!uap->dmatx.chan)
832 return;
833
834 uap->dmatx.buf = kmalloc(PL011_DMA_BUFFER_SIZE, GFP_KERNEL);
835 if (!uap->dmatx.buf) {
836 dev_err(uap->port.dev, "no memory for DMA TX buffer\n");
837 uap->port.fifosize = uap->fifosize;
838 return;
839 }
840
841 sg_init_one(&uap->dmatx.sg, uap->dmatx.buf, PL011_DMA_BUFFER_SIZE);
842
843 /* The DMA buffer is now the FIFO the TTY subsystem can use */
844 uap->port.fifosize = PL011_DMA_BUFFER_SIZE;
Linus Walleijead76f32011-02-24 13:21:08 +0100845 uap->using_tx_dma = true;
Russell King68b65f72010-12-22 17:24:39 +0000846
Linus Walleijead76f32011-02-24 13:21:08 +0100847 if (!uap->dmarx.chan)
848 goto skip_rx;
849
850 /* Allocate and map DMA RX buffers */
851 ret = pl011_sgbuf_init(uap->dmarx.chan, &uap->dmarx.sgbuf_a,
852 DMA_FROM_DEVICE);
853 if (ret) {
854 dev_err(uap->port.dev, "failed to init DMA %s: %d\n",
855 "RX buffer A", ret);
856 goto skip_rx;
857 }
858
859 ret = pl011_sgbuf_init(uap->dmarx.chan, &uap->dmarx.sgbuf_b,
860 DMA_FROM_DEVICE);
861 if (ret) {
862 dev_err(uap->port.dev, "failed to init DMA %s: %d\n",
863 "RX buffer B", ret);
864 pl011_sgbuf_free(uap->dmarx.chan, &uap->dmarx.sgbuf_a,
865 DMA_FROM_DEVICE);
866 goto skip_rx;
867 }
868
869 uap->using_rx_dma = true;
870
871skip_rx:
Russell King68b65f72010-12-22 17:24:39 +0000872 /* Turn on DMA error (RX/TX will be enabled on demand) */
873 uap->dmacr |= UART011_DMAONERR;
874 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
Russell King38d62432010-12-22 17:59:16 +0000875
876 /*
877 * ST Micro variants has some specific dma burst threshold
878 * compensation. Set this to 16 bytes, so burst will only
879 * be issued above/below 16 bytes.
880 */
881 if (uap->vendor->dma_threshold)
882 writew(ST_UART011_DMAWM_RX_16 | ST_UART011_DMAWM_TX_16,
883 uap->port.membase + ST_UART011_DMAWM);
Linus Walleijead76f32011-02-24 13:21:08 +0100884
885 if (uap->using_rx_dma) {
886 if (pl011_dma_rx_trigger_dma(uap))
887 dev_dbg(uap->port.dev, "could not trigger initial "
888 "RX DMA job, fall back to interrupt mode\n");
889 }
Russell King68b65f72010-12-22 17:24:39 +0000890}
891
892static void pl011_dma_shutdown(struct uart_amba_port *uap)
893{
Linus Walleijead76f32011-02-24 13:21:08 +0100894 if (!(uap->using_tx_dma || uap->using_rx_dma))
Russell King68b65f72010-12-22 17:24:39 +0000895 return;
896
897 /* Disable RX and TX DMA */
898 while (readw(uap->port.membase + UART01x_FR) & UART01x_FR_BUSY)
899 barrier();
900
901 spin_lock_irq(&uap->port.lock);
902 uap->dmacr &= ~(UART011_DMAONERR | UART011_RXDMAE | UART011_TXDMAE);
903 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
904 spin_unlock_irq(&uap->port.lock);
905
Linus Walleijead76f32011-02-24 13:21:08 +0100906 if (uap->using_tx_dma) {
907 /* In theory, this should already be done by pl011_dma_flush_buffer */
908 dmaengine_terminate_all(uap->dmatx.chan);
909 if (uap->dmatx.queued) {
910 dma_unmap_sg(uap->dmatx.chan->device->dev, &uap->dmatx.sg, 1,
911 DMA_TO_DEVICE);
912 uap->dmatx.queued = false;
913 }
914
915 kfree(uap->dmatx.buf);
916 uap->using_tx_dma = false;
Russell King68b65f72010-12-22 17:24:39 +0000917 }
918
Linus Walleijead76f32011-02-24 13:21:08 +0100919 if (uap->using_rx_dma) {
920 dmaengine_terminate_all(uap->dmarx.chan);
921 /* Clean up the RX DMA */
922 pl011_sgbuf_free(uap->dmarx.chan, &uap->dmarx.sgbuf_a, DMA_FROM_DEVICE);
923 pl011_sgbuf_free(uap->dmarx.chan, &uap->dmarx.sgbuf_b, DMA_FROM_DEVICE);
924 uap->using_rx_dma = false;
925 }
Russell King68b65f72010-12-22 17:24:39 +0000926}
927
Linus Walleijead76f32011-02-24 13:21:08 +0100928static inline bool pl011_dma_rx_available(struct uart_amba_port *uap)
929{
930 return uap->using_rx_dma;
931}
932
933static inline bool pl011_dma_rx_running(struct uart_amba_port *uap)
934{
935 return uap->using_rx_dma && uap->dmarx.running;
936}
937
938
Russell King68b65f72010-12-22 17:24:39 +0000939#else
940/* Blank functions if the DMA engine is not available */
941static inline void pl011_dma_probe(struct uart_amba_port *uap)
942{
943}
944
945static inline void pl011_dma_remove(struct uart_amba_port *uap)
946{
947}
948
949static inline void pl011_dma_startup(struct uart_amba_port *uap)
950{
951}
952
953static inline void pl011_dma_shutdown(struct uart_amba_port *uap)
954{
955}
956
957static inline bool pl011_dma_tx_irq(struct uart_amba_port *uap)
958{
959 return false;
960}
961
962static inline void pl011_dma_tx_stop(struct uart_amba_port *uap)
963{
964}
965
966static inline bool pl011_dma_tx_start(struct uart_amba_port *uap)
967{
968 return false;
969}
970
Linus Walleijead76f32011-02-24 13:21:08 +0100971static inline void pl011_dma_rx_irq(struct uart_amba_port *uap)
972{
973}
974
975static inline void pl011_dma_rx_stop(struct uart_amba_port *uap)
976{
977}
978
979static inline int pl011_dma_rx_trigger_dma(struct uart_amba_port *uap)
980{
981 return -EIO;
982}
983
984static inline bool pl011_dma_rx_available(struct uart_amba_port *uap)
985{
986 return false;
987}
988
989static inline bool pl011_dma_rx_running(struct uart_amba_port *uap)
990{
991 return false;
992}
993
Russell King68b65f72010-12-22 17:24:39 +0000994#define pl011_dma_flush_buffer NULL
995#endif
996
997
Russell Kingb129a8c2005-08-31 10:12:14 +0100998static void pl011_stop_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999{
1000 struct uart_amba_port *uap = (struct uart_amba_port *)port;
1001
1002 uap->im &= ~UART011_TXIM;
1003 writew(uap->im, uap->port.membase + UART011_IMSC);
Russell King68b65f72010-12-22 17:24:39 +00001004 pl011_dma_tx_stop(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005}
1006
Russell Kingb129a8c2005-08-31 10:12:14 +01001007static void pl011_start_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008{
1009 struct uart_amba_port *uap = (struct uart_amba_port *)port;
1010
Russell King68b65f72010-12-22 17:24:39 +00001011 if (!pl011_dma_tx_start(uap)) {
1012 uap->im |= UART011_TXIM;
1013 writew(uap->im, uap->port.membase + UART011_IMSC);
1014 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015}
1016
1017static void pl011_stop_rx(struct uart_port *port)
1018{
1019 struct uart_amba_port *uap = (struct uart_amba_port *)port;
1020
1021 uap->im &= ~(UART011_RXIM|UART011_RTIM|UART011_FEIM|
1022 UART011_PEIM|UART011_BEIM|UART011_OEIM);
1023 writew(uap->im, uap->port.membase + UART011_IMSC);
Linus Walleijead76f32011-02-24 13:21:08 +01001024
1025 pl011_dma_rx_stop(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026}
1027
1028static void pl011_enable_ms(struct uart_port *port)
1029{
1030 struct uart_amba_port *uap = (struct uart_amba_port *)port;
1031
1032 uap->im |= UART011_RIMIM|UART011_CTSMIM|UART011_DCDMIM|UART011_DSRMIM;
1033 writew(uap->im, uap->port.membase + UART011_IMSC);
1034}
1035
David Howells7d12e782006-10-05 14:55:46 +01001036static void pl011_rx_chars(struct uart_amba_port *uap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037{
Alan Coxebd2c8f2009-09-19 13:13:28 -07001038 struct tty_struct *tty = uap->port.state->port.tty;
Russell Kingb63d4f02005-11-19 11:10:35 +00001039 unsigned int status, ch, flag, max_count = 256;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040
1041 status = readw(uap->port.membase + UART01x_FR);
1042 while ((status & UART01x_FR_RXFE) == 0 && max_count--) {
Russell Kingb63d4f02005-11-19 11:10:35 +00001043 ch = readw(uap->port.membase + UART01x_DR) | UART_DUMMY_DR_RX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 flag = TTY_NORMAL;
1045 uap->port.icount.rx++;
1046
1047 /*
1048 * Note that the error handling code is
1049 * out of the main execution path
1050 */
Russell Kingb63d4f02005-11-19 11:10:35 +00001051 if (unlikely(ch & UART_DR_ERROR)) {
1052 if (ch & UART011_DR_BE) {
1053 ch &= ~(UART011_DR_FE | UART011_DR_PE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 uap->port.icount.brk++;
1055 if (uart_handle_break(&uap->port))
1056 goto ignore_char;
Russell Kingb63d4f02005-11-19 11:10:35 +00001057 } else if (ch & UART011_DR_PE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 uap->port.icount.parity++;
Russell Kingb63d4f02005-11-19 11:10:35 +00001059 else if (ch & UART011_DR_FE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 uap->port.icount.frame++;
Russell Kingb63d4f02005-11-19 11:10:35 +00001061 if (ch & UART011_DR_OE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 uap->port.icount.overrun++;
1063
Russell Kingb63d4f02005-11-19 11:10:35 +00001064 ch &= uap->port.read_status_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065
Russell Kingb63d4f02005-11-19 11:10:35 +00001066 if (ch & UART011_DR_BE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 flag = TTY_BREAK;
Russell Kingb63d4f02005-11-19 11:10:35 +00001068 else if (ch & UART011_DR_PE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 flag = TTY_PARITY;
Russell Kingb63d4f02005-11-19 11:10:35 +00001070 else if (ch & UART011_DR_FE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 flag = TTY_FRAME;
1072 }
1073
David Howells7d12e782006-10-05 14:55:46 +01001074 if (uart_handle_sysrq_char(&uap->port, ch & 255))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 goto ignore_char;
1076
Russell Kingb63d4f02005-11-19 11:10:35 +00001077 uart_insert_char(&uap->port, ch, UART011_DR_OE, ch, flag);
Russell King05ab3012005-05-09 23:21:59 +01001078
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 ignore_char:
1080 status = readw(uap->port.membase + UART01x_FR);
1081 }
Thomas Gleixner2389b272007-05-29 21:53:50 +01001082 spin_unlock(&uap->port.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 tty_flip_buffer_push(tty);
Linus Walleijead76f32011-02-24 13:21:08 +01001084 /*
1085 * If we were temporarily out of DMA mode for a while,
1086 * attempt to switch back to DMA mode again.
1087 */
1088 if (pl011_dma_rx_available(uap)) {
1089 if (pl011_dma_rx_trigger_dma(uap)) {
1090 dev_dbg(uap->port.dev, "could not trigger RX DMA job "
1091 "fall back to interrupt mode again\n");
1092 uap->im |= UART011_RXIM;
1093 } else
1094 uap->im &= ~UART011_RXIM;
1095 writew(uap->im, uap->port.membase + UART011_IMSC);
1096 }
Thomas Gleixner2389b272007-05-29 21:53:50 +01001097 spin_lock(&uap->port.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098}
1099
1100static void pl011_tx_chars(struct uart_amba_port *uap)
1101{
Alan Coxebd2c8f2009-09-19 13:13:28 -07001102 struct circ_buf *xmit = &uap->port.state->xmit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 int count;
1104
1105 if (uap->port.x_char) {
1106 writew(uap->port.x_char, uap->port.membase + UART01x_DR);
1107 uap->port.icount.tx++;
1108 uap->port.x_char = 0;
1109 return;
1110 }
1111 if (uart_circ_empty(xmit) || uart_tx_stopped(&uap->port)) {
Russell Kingb129a8c2005-08-31 10:12:14 +01001112 pl011_stop_tx(&uap->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 return;
1114 }
1115
Russell King68b65f72010-12-22 17:24:39 +00001116 /* If we are using DMA mode, try to send some characters. */
1117 if (pl011_dma_tx_irq(uap))
1118 return;
1119
Russell Kingffca2b12010-12-22 17:13:05 +00001120 count = uap->fifosize >> 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 do {
1122 writew(xmit->buf[xmit->tail], uap->port.membase + UART01x_DR);
1123 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
1124 uap->port.icount.tx++;
1125 if (uart_circ_empty(xmit))
1126 break;
1127 } while (--count > 0);
1128
1129 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
1130 uart_write_wakeup(&uap->port);
1131
1132 if (uart_circ_empty(xmit))
Russell Kingb129a8c2005-08-31 10:12:14 +01001133 pl011_stop_tx(&uap->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134}
1135
1136static void pl011_modem_status(struct uart_amba_port *uap)
1137{
1138 unsigned int status, delta;
1139
1140 status = readw(uap->port.membase + UART01x_FR) & UART01x_FR_MODEM_ANY;
1141
1142 delta = status ^ uap->old_status;
1143 uap->old_status = status;
1144
1145 if (!delta)
1146 return;
1147
1148 if (delta & UART01x_FR_DCD)
1149 uart_handle_dcd_change(&uap->port, status & UART01x_FR_DCD);
1150
1151 if (delta & UART01x_FR_DSR)
1152 uap->port.icount.dsr++;
1153
1154 if (delta & UART01x_FR_CTS)
1155 uart_handle_cts_change(&uap->port, status & UART01x_FR_CTS);
1156
Alan Coxbdc04e32009-09-19 13:13:31 -07001157 wake_up_interruptible(&uap->port.state->port.delta_msr_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158}
1159
David Howells7d12e782006-10-05 14:55:46 +01001160static irqreturn_t pl011_int(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161{
1162 struct uart_amba_port *uap = dev_id;
Russell King963cc982010-12-22 17:16:09 +00001163 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 unsigned int status, pass_counter = AMBA_ISR_PASS_LIMIT;
1165 int handled = 0;
1166
Russell King963cc982010-12-22 17:16:09 +00001167 spin_lock_irqsave(&uap->port.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168
1169 status = readw(uap->port.membase + UART011_MIS);
1170 if (status) {
1171 do {
1172 writew(status & ~(UART011_TXIS|UART011_RTIS|
1173 UART011_RXIS),
1174 uap->port.membase + UART011_ICR);
1175
Linus Walleijead76f32011-02-24 13:21:08 +01001176 if (status & (UART011_RTIS|UART011_RXIS)) {
1177 if (pl011_dma_rx_running(uap))
1178 pl011_dma_rx_irq(uap);
1179 else
1180 pl011_rx_chars(uap);
1181 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 if (status & (UART011_DSRMIS|UART011_DCDMIS|
1183 UART011_CTSMIS|UART011_RIMIS))
1184 pl011_modem_status(uap);
1185 if (status & UART011_TXIS)
1186 pl011_tx_chars(uap);
1187
1188 if (pass_counter-- == 0)
1189 break;
1190
1191 status = readw(uap->port.membase + UART011_MIS);
1192 } while (status != 0);
1193 handled = 1;
1194 }
1195
Russell King963cc982010-12-22 17:16:09 +00001196 spin_unlock_irqrestore(&uap->port.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197
1198 return IRQ_RETVAL(handled);
1199}
1200
1201static unsigned int pl01x_tx_empty(struct uart_port *port)
1202{
1203 struct uart_amba_port *uap = (struct uart_amba_port *)port;
1204 unsigned int status = readw(uap->port.membase + UART01x_FR);
1205 return status & (UART01x_FR_BUSY|UART01x_FR_TXFF) ? 0 : TIOCSER_TEMT;
1206}
1207
1208static unsigned int pl01x_get_mctrl(struct uart_port *port)
1209{
1210 struct uart_amba_port *uap = (struct uart_amba_port *)port;
1211 unsigned int result = 0;
1212 unsigned int status = readw(uap->port.membase + UART01x_FR);
1213
Jiri Slaby5159f402007-10-18 23:40:31 -07001214#define TIOCMBIT(uartbit, tiocmbit) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 if (status & uartbit) \
1216 result |= tiocmbit
1217
Jiri Slaby5159f402007-10-18 23:40:31 -07001218 TIOCMBIT(UART01x_FR_DCD, TIOCM_CAR);
1219 TIOCMBIT(UART01x_FR_DSR, TIOCM_DSR);
1220 TIOCMBIT(UART01x_FR_CTS, TIOCM_CTS);
1221 TIOCMBIT(UART011_FR_RI, TIOCM_RNG);
1222#undef TIOCMBIT
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 return result;
1224}
1225
1226static void pl011_set_mctrl(struct uart_port *port, unsigned int mctrl)
1227{
1228 struct uart_amba_port *uap = (struct uart_amba_port *)port;
1229 unsigned int cr;
1230
1231 cr = readw(uap->port.membase + UART011_CR);
1232
Jiri Slaby5159f402007-10-18 23:40:31 -07001233#define TIOCMBIT(tiocmbit, uartbit) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 if (mctrl & tiocmbit) \
1235 cr |= uartbit; \
1236 else \
1237 cr &= ~uartbit
1238
Jiri Slaby5159f402007-10-18 23:40:31 -07001239 TIOCMBIT(TIOCM_RTS, UART011_CR_RTS);
1240 TIOCMBIT(TIOCM_DTR, UART011_CR_DTR);
1241 TIOCMBIT(TIOCM_OUT1, UART011_CR_OUT1);
1242 TIOCMBIT(TIOCM_OUT2, UART011_CR_OUT2);
1243 TIOCMBIT(TIOCM_LOOP, UART011_CR_LBE);
Rabin Vincent3b438162010-02-12 06:43:11 +01001244
1245 if (uap->autorts) {
1246 /* We need to disable auto-RTS if we want to turn RTS off */
1247 TIOCMBIT(TIOCM_RTS, UART011_CR_RTSEN);
1248 }
Jiri Slaby5159f402007-10-18 23:40:31 -07001249#undef TIOCMBIT
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250
1251 writew(cr, uap->port.membase + UART011_CR);
1252}
1253
1254static void pl011_break_ctl(struct uart_port *port, int break_state)
1255{
1256 struct uart_amba_port *uap = (struct uart_amba_port *)port;
1257 unsigned long flags;
1258 unsigned int lcr_h;
1259
1260 spin_lock_irqsave(&uap->port.lock, flags);
Linus Walleijec489aa2010-06-02 08:13:52 +01001261 lcr_h = readw(uap->port.membase + uap->lcrh_tx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 if (break_state == -1)
1263 lcr_h |= UART01x_LCRH_BRK;
1264 else
1265 lcr_h &= ~UART01x_LCRH_BRK;
Linus Walleijec489aa2010-06-02 08:13:52 +01001266 writew(lcr_h, uap->port.membase + uap->lcrh_tx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 spin_unlock_irqrestore(&uap->port.lock, flags);
1268}
1269
Jason Wessel84b5ae12008-02-20 13:33:39 -06001270#ifdef CONFIG_CONSOLE_POLL
1271static int pl010_get_poll_char(struct uart_port *port)
1272{
1273 struct uart_amba_port *uap = (struct uart_amba_port *)port;
1274 unsigned int status;
1275
Jason Wesself5316b42010-05-20 21:04:22 -05001276 status = readw(uap->port.membase + UART01x_FR);
1277 if (status & UART01x_FR_RXFE)
1278 return NO_POLL_CHAR;
Jason Wessel84b5ae12008-02-20 13:33:39 -06001279
1280 return readw(uap->port.membase + UART01x_DR);
1281}
1282
1283static void pl010_put_poll_char(struct uart_port *port,
1284 unsigned char ch)
1285{
1286 struct uart_amba_port *uap = (struct uart_amba_port *)port;
1287
1288 while (readw(uap->port.membase + UART01x_FR) & UART01x_FR_TXFF)
1289 barrier();
1290
1291 writew(ch, uap->port.membase + UART01x_DR);
1292}
1293
1294#endif /* CONFIG_CONSOLE_POLL */
1295
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296static int pl011_startup(struct uart_port *port)
1297{
1298 struct uart_amba_port *uap = (struct uart_amba_port *)port;
1299 unsigned int cr;
1300 int retval;
1301
1302 /*
1303 * Try to enable the clock producer.
1304 */
1305 retval = clk_enable(uap->clk);
1306 if (retval)
1307 goto out;
1308
1309 uap->port.uartclk = clk_get_rate(uap->clk);
1310
1311 /*
1312 * Allocate the IRQ
1313 */
1314 retval = request_irq(uap->port.irq, pl011_int, 0, "uart-pl011", uap);
1315 if (retval)
1316 goto clk_dis;
1317
Russell Kingc19f12b2010-12-22 17:48:26 +00001318 writew(uap->vendor->ifls, uap->port.membase + UART011_IFLS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319
1320 /*
1321 * Provoke TX FIFO interrupt into asserting.
1322 */
1323 cr = UART01x_CR_UARTEN | UART011_CR_TXE | UART011_CR_LBE;
1324 writew(cr, uap->port.membase + UART011_CR);
1325 writew(0, uap->port.membase + UART011_FBRD);
1326 writew(1, uap->port.membase + UART011_IBRD);
Linus Walleijec489aa2010-06-02 08:13:52 +01001327 writew(0, uap->port.membase + uap->lcrh_rx);
1328 if (uap->lcrh_tx != uap->lcrh_rx) {
1329 int i;
1330 /*
1331 * Wait 10 PCLKs before writing LCRH_TX register,
1332 * to get this delay write read only register 10 times
1333 */
1334 for (i = 0; i < 10; ++i)
1335 writew(0xff, uap->port.membase + UART011_MIS);
1336 writew(0, uap->port.membase + uap->lcrh_tx);
1337 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 writew(0, uap->port.membase + UART01x_DR);
1339 while (readw(uap->port.membase + UART01x_FR) & UART01x_FR_BUSY)
1340 barrier();
1341
1342 cr = UART01x_CR_UARTEN | UART011_CR_RXE | UART011_CR_TXE;
1343 writew(cr, uap->port.membase + UART011_CR);
1344
Russell King5063e2c2010-12-22 17:09:08 +00001345 /* Clear pending error interrupts */
1346 writew(UART011_OEIS | UART011_BEIS | UART011_PEIS | UART011_FEIS,
1347 uap->port.membase + UART011_ICR);
1348
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 /*
1350 * initialise the old status of the modem signals
1351 */
1352 uap->old_status = readw(uap->port.membase + UART01x_FR) & UART01x_FR_MODEM_ANY;
1353
Russell King68b65f72010-12-22 17:24:39 +00001354 /* Startup DMA */
1355 pl011_dma_startup(uap);
1356
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 /*
Linus Walleijead76f32011-02-24 13:21:08 +01001358 * Finally, enable interrupts, only timeouts when using DMA
1359 * if initial RX DMA job failed, start in interrupt mode
1360 * as well.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 */
1362 spin_lock_irq(&uap->port.lock);
Linus Walleijead76f32011-02-24 13:21:08 +01001363 uap->im = UART011_RTIM;
1364 if (!pl011_dma_rx_running(uap))
1365 uap->im |= UART011_RXIM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 writew(uap->im, uap->port.membase + UART011_IMSC);
1367 spin_unlock_irq(&uap->port.lock);
1368
1369 return 0;
1370
1371 clk_dis:
1372 clk_disable(uap->clk);
1373 out:
1374 return retval;
1375}
1376
Linus Walleijec489aa2010-06-02 08:13:52 +01001377static void pl011_shutdown_channel(struct uart_amba_port *uap,
1378 unsigned int lcrh)
1379{
1380 unsigned long val;
1381
1382 val = readw(uap->port.membase + lcrh);
1383 val &= ~(UART01x_LCRH_BRK | UART01x_LCRH_FEN);
1384 writew(val, uap->port.membase + lcrh);
1385}
1386
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387static void pl011_shutdown(struct uart_port *port)
1388{
1389 struct uart_amba_port *uap = (struct uart_amba_port *)port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390
1391 /*
1392 * disable all interrupts
1393 */
1394 spin_lock_irq(&uap->port.lock);
1395 uap->im = 0;
1396 writew(uap->im, uap->port.membase + UART011_IMSC);
1397 writew(0xffff, uap->port.membase + UART011_ICR);
1398 spin_unlock_irq(&uap->port.lock);
1399
Russell King68b65f72010-12-22 17:24:39 +00001400 pl011_dma_shutdown(uap);
1401
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 /*
1403 * Free the interrupt
1404 */
1405 free_irq(uap->port.irq, uap);
1406
1407 /*
1408 * disable the port
1409 */
Rabin Vincent3b438162010-02-12 06:43:11 +01001410 uap->autorts = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 writew(UART01x_CR_UARTEN | UART011_CR_TXE, uap->port.membase + UART011_CR);
1412
1413 /*
1414 * disable break condition and fifos
1415 */
Linus Walleijec489aa2010-06-02 08:13:52 +01001416 pl011_shutdown_channel(uap, uap->lcrh_rx);
1417 if (uap->lcrh_rx != uap->lcrh_tx)
1418 pl011_shutdown_channel(uap, uap->lcrh_tx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419
1420 /*
1421 * Shut down the clock producer
1422 */
1423 clk_disable(uap->clk);
1424}
1425
1426static void
Alan Cox606d0992006-12-08 02:38:45 -08001427pl011_set_termios(struct uart_port *port, struct ktermios *termios,
1428 struct ktermios *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429{
Rabin Vincent3b438162010-02-12 06:43:11 +01001430 struct uart_amba_port *uap = (struct uart_amba_port *)port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 unsigned int lcr_h, old_cr;
1432 unsigned long flags;
Russell Kingc19f12b2010-12-22 17:48:26 +00001433 unsigned int baud, quot, clkdiv;
1434
1435 if (uap->vendor->oversampling)
1436 clkdiv = 8;
1437 else
1438 clkdiv = 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439
1440 /*
1441 * Ask the core to calculate the divisor for us.
1442 */
Linus Walleijac3e3fb2010-06-02 20:40:22 +01001443 baud = uart_get_baud_rate(port, termios, old, 0,
Russell Kingc19f12b2010-12-22 17:48:26 +00001444 port->uartclk / clkdiv);
Linus Walleijac3e3fb2010-06-02 20:40:22 +01001445
1446 if (baud > port->uartclk/16)
1447 quot = DIV_ROUND_CLOSEST(port->uartclk * 8, baud);
1448 else
1449 quot = DIV_ROUND_CLOSEST(port->uartclk * 4, baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450
1451 switch (termios->c_cflag & CSIZE) {
1452 case CS5:
1453 lcr_h = UART01x_LCRH_WLEN_5;
1454 break;
1455 case CS6:
1456 lcr_h = UART01x_LCRH_WLEN_6;
1457 break;
1458 case CS7:
1459 lcr_h = UART01x_LCRH_WLEN_7;
1460 break;
1461 default: // CS8
1462 lcr_h = UART01x_LCRH_WLEN_8;
1463 break;
1464 }
1465 if (termios->c_cflag & CSTOPB)
1466 lcr_h |= UART01x_LCRH_STP2;
1467 if (termios->c_cflag & PARENB) {
1468 lcr_h |= UART01x_LCRH_PEN;
1469 if (!(termios->c_cflag & PARODD))
1470 lcr_h |= UART01x_LCRH_EPS;
1471 }
Russell Kingffca2b12010-12-22 17:13:05 +00001472 if (uap->fifosize > 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 lcr_h |= UART01x_LCRH_FEN;
1474
1475 spin_lock_irqsave(&port->lock, flags);
1476
1477 /*
1478 * Update the per-port timeout.
1479 */
1480 uart_update_timeout(port, termios->c_cflag, baud);
1481
Russell Kingb63d4f02005-11-19 11:10:35 +00001482 port->read_status_mask = UART011_DR_OE | 255;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 if (termios->c_iflag & INPCK)
Russell Kingb63d4f02005-11-19 11:10:35 +00001484 port->read_status_mask |= UART011_DR_FE | UART011_DR_PE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 if (termios->c_iflag & (BRKINT | PARMRK))
Russell Kingb63d4f02005-11-19 11:10:35 +00001486 port->read_status_mask |= UART011_DR_BE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487
1488 /*
1489 * Characters to ignore
1490 */
1491 port->ignore_status_mask = 0;
1492 if (termios->c_iflag & IGNPAR)
Russell Kingb63d4f02005-11-19 11:10:35 +00001493 port->ignore_status_mask |= UART011_DR_FE | UART011_DR_PE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 if (termios->c_iflag & IGNBRK) {
Russell Kingb63d4f02005-11-19 11:10:35 +00001495 port->ignore_status_mask |= UART011_DR_BE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 /*
1497 * If we're ignoring parity and break indicators,
1498 * ignore overruns too (for real raw support).
1499 */
1500 if (termios->c_iflag & IGNPAR)
Russell Kingb63d4f02005-11-19 11:10:35 +00001501 port->ignore_status_mask |= UART011_DR_OE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 }
1503
1504 /*
1505 * Ignore all characters if CREAD is not set.
1506 */
1507 if ((termios->c_cflag & CREAD) == 0)
Russell Kingb63d4f02005-11-19 11:10:35 +00001508 port->ignore_status_mask |= UART_DUMMY_DR_RX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509
1510 if (UART_ENABLE_MS(port, termios->c_cflag))
1511 pl011_enable_ms(port);
1512
1513 /* first, disable everything */
1514 old_cr = readw(port->membase + UART011_CR);
1515 writew(0, port->membase + UART011_CR);
1516
Rabin Vincent3b438162010-02-12 06:43:11 +01001517 if (termios->c_cflag & CRTSCTS) {
1518 if (old_cr & UART011_CR_RTS)
1519 old_cr |= UART011_CR_RTSEN;
1520
1521 old_cr |= UART011_CR_CTSEN;
1522 uap->autorts = true;
1523 } else {
1524 old_cr &= ~(UART011_CR_CTSEN | UART011_CR_RTSEN);
1525 uap->autorts = false;
1526 }
1527
Russell Kingc19f12b2010-12-22 17:48:26 +00001528 if (uap->vendor->oversampling) {
1529 if (baud > port->uartclk / 16)
Linus Walleijac3e3fb2010-06-02 20:40:22 +01001530 old_cr |= ST_UART011_CR_OVSFACT;
1531 else
1532 old_cr &= ~ST_UART011_CR_OVSFACT;
1533 }
1534
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 /* Set baud rate */
1536 writew(quot & 0x3f, port->membase + UART011_FBRD);
1537 writew(quot >> 6, port->membase + UART011_IBRD);
1538
1539 /*
1540 * ----------v----------v----------v----------v-----
1541 * NOTE: MUST BE WRITTEN AFTER UARTLCR_M & UARTLCR_L
1542 * ----------^----------^----------^----------^-----
1543 */
Linus Walleijec489aa2010-06-02 08:13:52 +01001544 writew(lcr_h, port->membase + uap->lcrh_rx);
1545 if (uap->lcrh_rx != uap->lcrh_tx) {
1546 int i;
1547 /*
1548 * Wait 10 PCLKs before writing LCRH_TX register,
1549 * to get this delay write read only register 10 times
1550 */
1551 for (i = 0; i < 10; ++i)
1552 writew(0xff, uap->port.membase + UART011_MIS);
1553 writew(lcr_h, port->membase + uap->lcrh_tx);
1554 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 writew(old_cr, port->membase + UART011_CR);
1556
1557 spin_unlock_irqrestore(&port->lock, flags);
1558}
1559
1560static const char *pl011_type(struct uart_port *port)
1561{
Russell Kinge8a7ba82010-12-28 09:16:54 +00001562 struct uart_amba_port *uap = (struct uart_amba_port *)port;
1563 return uap->port.type == PORT_AMBA ? uap->type : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564}
1565
1566/*
1567 * Release the memory region(s) being used by 'port'
1568 */
1569static void pl010_release_port(struct uart_port *port)
1570{
1571 release_mem_region(port->mapbase, SZ_4K);
1572}
1573
1574/*
1575 * Request the memory region(s) being used by 'port'
1576 */
1577static int pl010_request_port(struct uart_port *port)
1578{
1579 return request_mem_region(port->mapbase, SZ_4K, "uart-pl011")
1580 != NULL ? 0 : -EBUSY;
1581}
1582
1583/*
1584 * Configure/autoconfigure the port.
1585 */
1586static void pl010_config_port(struct uart_port *port, int flags)
1587{
1588 if (flags & UART_CONFIG_TYPE) {
1589 port->type = PORT_AMBA;
1590 pl010_request_port(port);
1591 }
1592}
1593
1594/*
1595 * verify the new serial_struct (for TIOCSSERIAL).
1596 */
1597static int pl010_verify_port(struct uart_port *port, struct serial_struct *ser)
1598{
1599 int ret = 0;
1600 if (ser->type != PORT_UNKNOWN && ser->type != PORT_AMBA)
1601 ret = -EINVAL;
Yinghai Lua62c4132008-08-19 20:49:55 -07001602 if (ser->irq < 0 || ser->irq >= nr_irqs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 ret = -EINVAL;
1604 if (ser->baud_base < 9600)
1605 ret = -EINVAL;
1606 return ret;
1607}
1608
1609static struct uart_ops amba_pl011_pops = {
1610 .tx_empty = pl01x_tx_empty,
1611 .set_mctrl = pl011_set_mctrl,
1612 .get_mctrl = pl01x_get_mctrl,
1613 .stop_tx = pl011_stop_tx,
1614 .start_tx = pl011_start_tx,
1615 .stop_rx = pl011_stop_rx,
1616 .enable_ms = pl011_enable_ms,
1617 .break_ctl = pl011_break_ctl,
1618 .startup = pl011_startup,
1619 .shutdown = pl011_shutdown,
Russell King68b65f72010-12-22 17:24:39 +00001620 .flush_buffer = pl011_dma_flush_buffer,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 .set_termios = pl011_set_termios,
1622 .type = pl011_type,
1623 .release_port = pl010_release_port,
1624 .request_port = pl010_request_port,
1625 .config_port = pl010_config_port,
1626 .verify_port = pl010_verify_port,
Jason Wessel84b5ae12008-02-20 13:33:39 -06001627#ifdef CONFIG_CONSOLE_POLL
1628 .poll_get_char = pl010_get_poll_char,
1629 .poll_put_char = pl010_put_poll_char,
1630#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631};
1632
1633static struct uart_amba_port *amba_ports[UART_NR];
1634
1635#ifdef CONFIG_SERIAL_AMBA_PL011_CONSOLE
1636
Russell Kingd3587882006-03-20 20:00:09 +00001637static void pl011_console_putchar(struct uart_port *port, int ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638{
Russell Kingd3587882006-03-20 20:00:09 +00001639 struct uart_amba_port *uap = (struct uart_amba_port *)port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640
Russell Kingd3587882006-03-20 20:00:09 +00001641 while (readw(uap->port.membase + UART01x_FR) & UART01x_FR_TXFF)
1642 barrier();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 writew(ch, uap->port.membase + UART01x_DR);
1644}
1645
1646static void
1647pl011_console_write(struct console *co, const char *s, unsigned int count)
1648{
1649 struct uart_amba_port *uap = amba_ports[co->index];
1650 unsigned int status, old_cr, new_cr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651
1652 clk_enable(uap->clk);
1653
1654 /*
1655 * First save the CR then disable the interrupts
1656 */
1657 old_cr = readw(uap->port.membase + UART011_CR);
1658 new_cr = old_cr & ~UART011_CR_CTSEN;
1659 new_cr |= UART01x_CR_UARTEN | UART011_CR_TXE;
1660 writew(new_cr, uap->port.membase + UART011_CR);
1661
Russell Kingd3587882006-03-20 20:00:09 +00001662 uart_console_write(&uap->port, s, count, pl011_console_putchar);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663
1664 /*
1665 * Finally, wait for transmitter to become empty
1666 * and restore the TCR
1667 */
1668 do {
1669 status = readw(uap->port.membase + UART01x_FR);
1670 } while (status & UART01x_FR_BUSY);
1671 writew(old_cr, uap->port.membase + UART011_CR);
1672
1673 clk_disable(uap->clk);
1674}
1675
1676static void __init
1677pl011_console_get_options(struct uart_amba_port *uap, int *baud,
1678 int *parity, int *bits)
1679{
1680 if (readw(uap->port.membase + UART011_CR) & UART01x_CR_UARTEN) {
1681 unsigned int lcr_h, ibrd, fbrd;
1682
Linus Walleijec489aa2010-06-02 08:13:52 +01001683 lcr_h = readw(uap->port.membase + uap->lcrh_tx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684
1685 *parity = 'n';
1686 if (lcr_h & UART01x_LCRH_PEN) {
1687 if (lcr_h & UART01x_LCRH_EPS)
1688 *parity = 'e';
1689 else
1690 *parity = 'o';
1691 }
1692
1693 if ((lcr_h & 0x60) == UART01x_LCRH_WLEN_7)
1694 *bits = 7;
1695 else
1696 *bits = 8;
1697
1698 ibrd = readw(uap->port.membase + UART011_IBRD);
1699 fbrd = readw(uap->port.membase + UART011_FBRD);
1700
1701 *baud = uap->port.uartclk * 4 / (64 * ibrd + fbrd);
Linus Walleijac3e3fb2010-06-02 20:40:22 +01001702
Russell Kingc19f12b2010-12-22 17:48:26 +00001703 if (uap->vendor->oversampling) {
Linus Walleijac3e3fb2010-06-02 20:40:22 +01001704 if (readw(uap->port.membase + UART011_CR)
1705 & ST_UART011_CR_OVSFACT)
1706 *baud *= 2;
1707 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 }
1709}
1710
1711static int __init pl011_console_setup(struct console *co, char *options)
1712{
1713 struct uart_amba_port *uap;
1714 int baud = 38400;
1715 int bits = 8;
1716 int parity = 'n';
1717 int flow = 'n';
1718
1719 /*
1720 * Check whether an invalid uart number has been specified, and
1721 * if so, search for the first available port that does have
1722 * console support.
1723 */
1724 if (co->index >= UART_NR)
1725 co->index = 0;
1726 uap = amba_ports[co->index];
Russell Kingd28122a2007-01-22 18:59:42 +00001727 if (!uap)
1728 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729
1730 uap->port.uartclk = clk_get_rate(uap->clk);
1731
1732 if (options)
1733 uart_parse_options(options, &baud, &parity, &bits, &flow);
1734 else
1735 pl011_console_get_options(uap, &baud, &parity, &bits);
1736
1737 return uart_set_options(&uap->port, co, baud, parity, bits, flow);
1738}
1739
Vincent Sanders2d934862005-09-14 22:36:03 +01001740static struct uart_driver amba_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741static struct console amba_console = {
1742 .name = "ttyAMA",
1743 .write = pl011_console_write,
1744 .device = uart_console_device,
1745 .setup = pl011_console_setup,
1746 .flags = CON_PRINTBUFFER,
1747 .index = -1,
1748 .data = &amba_reg,
1749};
1750
1751#define AMBA_CONSOLE (&amba_console)
1752#else
1753#define AMBA_CONSOLE NULL
1754#endif
1755
1756static struct uart_driver amba_reg = {
1757 .owner = THIS_MODULE,
1758 .driver_name = "ttyAMA",
1759 .dev_name = "ttyAMA",
1760 .major = SERIAL_AMBA_MAJOR,
1761 .minor = SERIAL_AMBA_MINOR,
1762 .nr = UART_NR,
1763 .cons = AMBA_CONSOLE,
1764};
1765
Alessandro Rubini03fbdb12009-05-20 22:39:08 +01001766static int pl011_probe(struct amba_device *dev, struct amba_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767{
1768 struct uart_amba_port *uap;
Alessandro Rubini5926a292009-06-04 17:43:04 +01001769 struct vendor_data *vendor = id->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 void __iomem *base;
1771 int i, ret;
1772
1773 for (i = 0; i < ARRAY_SIZE(amba_ports); i++)
1774 if (amba_ports[i] == NULL)
1775 break;
1776
1777 if (i == ARRAY_SIZE(amba_ports)) {
1778 ret = -EBUSY;
1779 goto out;
1780 }
1781
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07001782 uap = kzalloc(sizeof(struct uart_amba_port), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 if (uap == NULL) {
1784 ret = -ENOMEM;
1785 goto out;
1786 }
1787
Linus Walleijdc890c22009-06-07 23:27:31 +01001788 base = ioremap(dev->res.start, resource_size(&dev->res));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 if (!base) {
1790 ret = -ENOMEM;
1791 goto free;
1792 }
1793
Russell Kingee569c42008-11-30 17:38:14 +00001794 uap->clk = clk_get(&dev->dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 if (IS_ERR(uap->clk)) {
1796 ret = PTR_ERR(uap->clk);
1797 goto unmap;
1798 }
1799
Russell Kingc19f12b2010-12-22 17:48:26 +00001800 uap->vendor = vendor;
Linus Walleijec489aa2010-06-02 08:13:52 +01001801 uap->lcrh_rx = vendor->lcrh_rx;
1802 uap->lcrh_tx = vendor->lcrh_tx;
Russell Kingffca2b12010-12-22 17:13:05 +00001803 uap->fifosize = vendor->fifosize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 uap->port.dev = &dev->dev;
1805 uap->port.mapbase = dev->res.start;
1806 uap->port.membase = base;
1807 uap->port.iotype = UPIO_MEM;
1808 uap->port.irq = dev->irq[0];
Russell Kingffca2b12010-12-22 17:13:05 +00001809 uap->port.fifosize = uap->fifosize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 uap->port.ops = &amba_pl011_pops;
1811 uap->port.flags = UPF_BOOT_AUTOCONF;
1812 uap->port.line = i;
Russell King68b65f72010-12-22 17:24:39 +00001813 pl011_dma_probe(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814
Russell Kinge8a7ba82010-12-28 09:16:54 +00001815 snprintf(uap->type, sizeof(uap->type), "PL011 rev%u", amba_rev(dev));
1816
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 amba_ports[i] = uap;
1818
1819 amba_set_drvdata(dev, uap);
1820 ret = uart_add_one_port(&amba_reg, &uap->port);
1821 if (ret) {
1822 amba_set_drvdata(dev, NULL);
1823 amba_ports[i] = NULL;
Russell King68b65f72010-12-22 17:24:39 +00001824 pl011_dma_remove(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 clk_put(uap->clk);
1826 unmap:
1827 iounmap(base);
1828 free:
1829 kfree(uap);
1830 }
1831 out:
1832 return ret;
1833}
1834
1835static int pl011_remove(struct amba_device *dev)
1836{
1837 struct uart_amba_port *uap = amba_get_drvdata(dev);
1838 int i;
1839
1840 amba_set_drvdata(dev, NULL);
1841
1842 uart_remove_one_port(&amba_reg, &uap->port);
1843
1844 for (i = 0; i < ARRAY_SIZE(amba_ports); i++)
1845 if (amba_ports[i] == uap)
1846 amba_ports[i] = NULL;
1847
Russell King68b65f72010-12-22 17:24:39 +00001848 pl011_dma_remove(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 iounmap(uap->port.membase);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 clk_put(uap->clk);
1851 kfree(uap);
1852 return 0;
1853}
1854
Leo Chenb736b892009-07-28 23:43:33 +01001855#ifdef CONFIG_PM
1856static int pl011_suspend(struct amba_device *dev, pm_message_t state)
1857{
1858 struct uart_amba_port *uap = amba_get_drvdata(dev);
1859
1860 if (!uap)
1861 return -EINVAL;
1862
1863 return uart_suspend_port(&amba_reg, &uap->port);
1864}
1865
1866static int pl011_resume(struct amba_device *dev)
1867{
1868 struct uart_amba_port *uap = amba_get_drvdata(dev);
1869
1870 if (!uap)
1871 return -EINVAL;
1872
1873 return uart_resume_port(&amba_reg, &uap->port);
1874}
1875#endif
1876
Russell King2c39c9e2010-07-27 08:50:16 +01001877static struct amba_id pl011_ids[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 {
1879 .id = 0x00041011,
1880 .mask = 0x000fffff,
Alessandro Rubini5926a292009-06-04 17:43:04 +01001881 .data = &vendor_arm,
1882 },
1883 {
1884 .id = 0x00380802,
1885 .mask = 0x00ffffff,
1886 .data = &vendor_st,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 },
1888 { 0, 0 },
1889};
1890
1891static struct amba_driver pl011_driver = {
1892 .drv = {
1893 .name = "uart-pl011",
1894 },
1895 .id_table = pl011_ids,
1896 .probe = pl011_probe,
1897 .remove = pl011_remove,
Leo Chenb736b892009-07-28 23:43:33 +01001898#ifdef CONFIG_PM
1899 .suspend = pl011_suspend,
1900 .resume = pl011_resume,
1901#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902};
1903
1904static int __init pl011_init(void)
1905{
1906 int ret;
1907 printk(KERN_INFO "Serial: AMBA PL011 UART driver\n");
1908
1909 ret = uart_register_driver(&amba_reg);
1910 if (ret == 0) {
1911 ret = amba_driver_register(&pl011_driver);
1912 if (ret)
1913 uart_unregister_driver(&amba_reg);
1914 }
1915 return ret;
1916}
1917
1918static void __exit pl011_exit(void)
1919{
1920 amba_driver_unregister(&pl011_driver);
1921 uart_unregister_driver(&amba_reg);
1922}
1923
Alessandro Rubini4dd9e742009-05-05 05:54:13 +01001924/*
1925 * While this can be a module, if builtin it's most likely the console
1926 * So let's leave module_exit but move module_init to an earlier place
1927 */
1928arch_initcall(pl011_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929module_exit(pl011_exit);
1930
1931MODULE_AUTHOR("ARM Ltd/Deep Blue Solutions Ltd");
1932MODULE_DESCRIPTION("ARM AMBA serial port driver");
1933MODULE_LICENSE("GPL");