n_tty: Reduce branching in canon_copy_from_read_buf()
[deliverable/linux.git] / drivers / tty / serial / amba-pl011.c
CommitLineData
1da177e4 1/*
1da177e4
LT
2 * 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.
68b65f73 8 * Copyright (C) 2010 ST-Ericsson SA
1da177e4
LT
9 *
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 *
1da177e4
LT
24 * 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 */
1da177e4 31
cb06ff10 32
1da177e4
LT
33#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>
a62c80e5
RK
47#include <linux/amba/bus.h>
48#include <linux/amba/serial.h>
f8ce2547 49#include <linux/clk.h>
5a0e3ad6 50#include <linux/slab.h>
68b65f73
RK
51#include <linux/dmaengine.h>
52#include <linux/dma-mapping.h>
53#include <linux/scatterlist.h>
c16d51a3 54#include <linux/delay.h>
258aea76 55#include <linux/types.h>
32614aad
ML
56#include <linux/of.h>
57#include <linux/of_device.h>
258e0551 58#include <linux/pinctrl/consumer.h>
cb70706c 59#include <linux/sizes.h>
de609582 60#include <linux/io.h>
3db9ab0b 61#include <linux/acpi.h>
1da177e4 62
9f25bc51
RK
63#include "amba-pl011.h"
64
1da177e4
LT
65#define UART_NR 14
66
67#define SERIAL_AMBA_MAJOR 204
68#define SERIAL_AMBA_MINOR 64
69#define SERIAL_AMBA_NR UART_NR
70
71#define AMBA_ISR_PASS_LIMIT 256
72
b63d4f0f
RK
73#define UART_DR_ERROR (UART011_DR_OE|UART011_DR_BE|UART011_DR_PE|UART011_DR_FE)
74#define UART_DUMMY_DR_RX (1 << 16)
1da177e4 75
5926a295
AR
76/* There is by now at least one vendor with differing details, so handle it */
77struct vendor_data {
78 unsigned int ifls;
ec489aa8
LW
79 unsigned int lcrh_tx;
80 unsigned int lcrh_rx;
ac3e3fb4 81 bool oversampling;
38d62436 82 bool dma_threshold;
4fd0690b 83 bool cts_event_workaround;
71eec483 84 bool always_enabled;
cefc2d1d 85 bool fixed_options;
78506f22 86
ea33640a 87 unsigned int (*get_fifosize)(struct amba_device *dev);
5926a295
AR
88};
89
ea33640a 90static unsigned int get_fifosize_arm(struct amba_device *dev)
78506f22 91{
ea33640a 92 return amba_rev(dev) < 3 ? 16 : 32;
78506f22
JK
93}
94
5926a295
AR
95static struct vendor_data vendor_arm = {
96 .ifls = UART011_IFLS_RX4_8|UART011_IFLS_TX4_8,
9f25bc51
RK
97 .lcrh_tx = REG_LCRH,
98 .lcrh_rx = REG_LCRH,
ac3e3fb4 99 .oversampling = false,
38d62436 100 .dma_threshold = false,
4fd0690b 101 .cts_event_workaround = false,
71eec483 102 .always_enabled = false,
cefc2d1d 103 .fixed_options = false,
78506f22 104 .get_fifosize = get_fifosize_arm,
5926a295
AR
105};
106
0dd1e247
AP
107static struct vendor_data vendor_sbsa = {
108 .oversampling = false,
109 .dma_threshold = false,
110 .cts_event_workaround = false,
111 .always_enabled = true,
112 .fixed_options = true,
113};
114
ea33640a 115static unsigned int get_fifosize_st(struct amba_device *dev)
78506f22
JK
116{
117 return 64;
118}
119
5926a295
AR
120static struct vendor_data vendor_st = {
121 .ifls = UART011_IFLS_RX_HALF|UART011_IFLS_TX_HALF,
9f25bc51
RK
122 .lcrh_tx = REG_ST_LCRH_TX,
123 .lcrh_rx = REG_ST_LCRH_RX,
ac3e3fb4 124 .oversampling = true,
38d62436 125 .dma_threshold = true,
4fd0690b 126 .cts_event_workaround = true,
71eec483 127 .always_enabled = false,
cefc2d1d 128 .fixed_options = false,
78506f22 129 .get_fifosize = get_fifosize_st,
1da177e4
LT
130};
131
68b65f73 132/* Deals with DMA transactions */
ead76f32
LW
133
134struct pl011_sgbuf {
135 struct scatterlist sg;
136 char *buf;
137};
138
139struct pl011_dmarx_data {
140 struct dma_chan *chan;
141 struct completion complete;
142 bool use_buf_b;
143 struct pl011_sgbuf sgbuf_a;
144 struct pl011_sgbuf sgbuf_b;
145 dma_cookie_t cookie;
146 bool running;
cb06ff10
CM
147 struct timer_list timer;
148 unsigned int last_residue;
149 unsigned long last_jiffies;
150 bool auto_poll_rate;
151 unsigned int poll_rate;
152 unsigned int poll_timeout;
ead76f32
LW
153};
154
68b65f73
RK
155struct pl011_dmatx_data {
156 struct dma_chan *chan;
157 struct scatterlist sg;
158 char *buf;
159 bool queued;
160};
161
c19f12b5
RK
162/*
163 * We wrap our port structure around the generic uart_port.
164 */
165struct uart_amba_port {
166 struct uart_port port;
167 struct clk *clk;
168 const struct vendor_data *vendor;
68b65f73 169 unsigned int dmacr; /* dma control reg */
c19f12b5
RK
170 unsigned int im; /* interrupt mask */
171 unsigned int old_status;
ffca2b11 172 unsigned int fifosize; /* vendor-specific */
c19f12b5
RK
173 unsigned int lcrh_tx; /* vendor-specific */
174 unsigned int lcrh_rx; /* vendor-specific */
d8d8ffa4 175 unsigned int old_cr; /* state during shutdown */
c19f12b5 176 bool autorts;
cefc2d1d 177 unsigned int fixed_baud; /* vendor-set fixed baud rate */
c19f12b5 178 char type[12];
68b65f73
RK
179#ifdef CONFIG_DMA_ENGINE
180 /* DMA stuff */
ead76f32
LW
181 bool using_tx_dma;
182 bool using_rx_dma;
183 struct pl011_dmarx_data dmarx;
68b65f73 184 struct pl011_dmatx_data dmatx;
1c9be310 185 bool dma_probed;
68b65f73
RK
186#endif
187};
188
9f25bc51
RK
189static unsigned int pl011_reg_to_offset(const struct uart_amba_port *uap,
190 unsigned int reg)
191{
192 return reg;
193}
194
b2a4e24c
RK
195static unsigned int pl011_read(const struct uart_amba_port *uap,
196 unsigned int reg)
75836339 197{
9f25bc51 198 return readw(uap->port.membase + pl011_reg_to_offset(uap, reg));
75836339
RK
199}
200
b2a4e24c
RK
201static void pl011_write(unsigned int val, const struct uart_amba_port *uap,
202 unsigned int reg)
75836339 203{
9f25bc51 204 writew(val, uap->port.membase + pl011_reg_to_offset(uap, reg));
75836339
RK
205}
206
29772c4e
LW
207/*
208 * Reads up to 256 characters from the FIFO or until it's empty and
209 * inserts them into the TTY layer. Returns the number of characters
210 * read from the FIFO.
211 */
212static int pl011_fifo_to_tty(struct uart_amba_port *uap)
213{
71a5cd8a
TT
214 u16 status;
215 unsigned int ch, flag, max_count = 256;
29772c4e
LW
216 int fifotaken = 0;
217
218 while (max_count--) {
9f25bc51 219 status = pl011_read(uap, REG_FR);
29772c4e
LW
220 if (status & UART01x_FR_RXFE)
221 break;
222
223 /* Take chars from the FIFO and update status */
9f25bc51 224 ch = pl011_read(uap, REG_DR) | UART_DUMMY_DR_RX;
29772c4e
LW
225 flag = TTY_NORMAL;
226 uap->port.icount.rx++;
227 fifotaken++;
228
229 if (unlikely(ch & UART_DR_ERROR)) {
230 if (ch & UART011_DR_BE) {
231 ch &= ~(UART011_DR_FE | UART011_DR_PE);
232 uap->port.icount.brk++;
233 if (uart_handle_break(&uap->port))
234 continue;
235 } else if (ch & UART011_DR_PE)
236 uap->port.icount.parity++;
237 else if (ch & UART011_DR_FE)
238 uap->port.icount.frame++;
239 if (ch & UART011_DR_OE)
240 uap->port.icount.overrun++;
241
242 ch &= uap->port.read_status_mask;
243
244 if (ch & UART011_DR_BE)
245 flag = TTY_BREAK;
246 else if (ch & UART011_DR_PE)
247 flag = TTY_PARITY;
248 else if (ch & UART011_DR_FE)
249 flag = TTY_FRAME;
250 }
251
252 if (uart_handle_sysrq_char(&uap->port, ch & 255))
253 continue;
254
255 uart_insert_char(&uap->port, ch, UART011_DR_OE, ch, flag);
256 }
257
258 return fifotaken;
259}
260
261
68b65f73
RK
262/*
263 * All the DMA operation mode stuff goes inside this ifdef.
264 * This assumes that you have a generic DMA device interface,
265 * no custom DMA interfaces are supported.
266 */
267#ifdef CONFIG_DMA_ENGINE
268
269#define PL011_DMA_BUFFER_SIZE PAGE_SIZE
270
ead76f32
LW
271static int pl011_sgbuf_init(struct dma_chan *chan, struct pl011_sgbuf *sg,
272 enum dma_data_direction dir)
273{
cb06ff10
CM
274 dma_addr_t dma_addr;
275
276 sg->buf = dma_alloc_coherent(chan->device->dev,
277 PL011_DMA_BUFFER_SIZE, &dma_addr, GFP_KERNEL);
ead76f32
LW
278 if (!sg->buf)
279 return -ENOMEM;
280
cb06ff10
CM
281 sg_init_table(&sg->sg, 1);
282 sg_set_page(&sg->sg, phys_to_page(dma_addr),
283 PL011_DMA_BUFFER_SIZE, offset_in_page(dma_addr));
284 sg_dma_address(&sg->sg) = dma_addr;
c64be923 285 sg_dma_len(&sg->sg) = PL011_DMA_BUFFER_SIZE;
ead76f32 286
ead76f32
LW
287 return 0;
288}
289
290static void pl011_sgbuf_free(struct dma_chan *chan, struct pl011_sgbuf *sg,
291 enum dma_data_direction dir)
292{
293 if (sg->buf) {
cb06ff10
CM
294 dma_free_coherent(chan->device->dev,
295 PL011_DMA_BUFFER_SIZE, sg->buf,
296 sg_dma_address(&sg->sg));
ead76f32
LW
297 }
298}
299
1c9be310 300static void pl011_dma_probe(struct uart_amba_port *uap)
68b65f73
RK
301{
302 /* DMA is the sole user of the platform data right now */
574de559 303 struct amba_pl011_data *plat = dev_get_platdata(uap->port.dev);
1c9be310 304 struct device *dev = uap->port.dev;
68b65f73 305 struct dma_slave_config tx_conf = {
9f25bc51
RK
306 .dst_addr = uap->port.mapbase +
307 pl011_reg_to_offset(uap, REG_DR),
68b65f73 308 .dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE,
a485df4b 309 .direction = DMA_MEM_TO_DEV,
68b65f73 310 .dst_maxburst = uap->fifosize >> 1,
258aea76 311 .device_fc = false,
68b65f73
RK
312 };
313 struct dma_chan *chan;
314 dma_cap_mask_t mask;
315
1c9be310
JRO
316 uap->dma_probed = true;
317 chan = dma_request_slave_channel_reason(dev, "tx");
318 if (IS_ERR(chan)) {
319 if (PTR_ERR(chan) == -EPROBE_DEFER) {
1c9be310
JRO
320 uap->dma_probed = false;
321 return;
322 }
68b65f73 323
787b0c1f
AB
324 /* We need platform data */
325 if (!plat || !plat->dma_filter) {
326 dev_info(uap->port.dev, "no DMA platform data\n");
327 return;
328 }
329
330 /* Try to acquire a generic DMA engine slave TX channel */
331 dma_cap_zero(mask);
332 dma_cap_set(DMA_SLAVE, mask);
333
334 chan = dma_request_channel(mask, plat->dma_filter,
335 plat->dma_tx_param);
336 if (!chan) {
337 dev_err(uap->port.dev, "no TX DMA channel!\n");
338 return;
339 }
68b65f73
RK
340 }
341
342 dmaengine_slave_config(chan, &tx_conf);
343 uap->dmatx.chan = chan;
344
345 dev_info(uap->port.dev, "DMA channel TX %s\n",
346 dma_chan_name(uap->dmatx.chan));
ead76f32
LW
347
348 /* Optionally make use of an RX channel as well */
787b0c1f 349 chan = dma_request_slave_channel(dev, "rx");
0d3c673e 350
787b0c1f
AB
351 if (!chan && plat->dma_rx_param) {
352 chan = dma_request_channel(mask, plat->dma_filter, plat->dma_rx_param);
353
354 if (!chan) {
355 dev_err(uap->port.dev, "no RX DMA channel!\n");
356 return;
357 }
358 }
359
360 if (chan) {
ead76f32 361 struct dma_slave_config rx_conf = {
9f25bc51
RK
362 .src_addr = uap->port.mapbase +
363 pl011_reg_to_offset(uap, REG_DR),
ead76f32 364 .src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE,
a485df4b 365 .direction = DMA_DEV_TO_MEM,
b2aeb775 366 .src_maxburst = uap->fifosize >> 2,
258aea76 367 .device_fc = false,
ead76f32 368 };
2d3b7d6e
AJ
369 struct dma_slave_caps caps;
370
371 /*
372 * Some DMA controllers provide information on their capabilities.
373 * If the controller does, check for suitable residue processing
374 * otherwise assime all is well.
375 */
376 if (0 == dma_get_slave_caps(chan, &caps)) {
377 if (caps.residue_granularity ==
378 DMA_RESIDUE_GRANULARITY_DESCRIPTOR) {
379 dma_release_channel(chan);
380 dev_info(uap->port.dev,
381 "RX DMA disabled - no residue processing\n");
382 return;
383 }
384 }
ead76f32
LW
385 dmaengine_slave_config(chan, &rx_conf);
386 uap->dmarx.chan = chan;
387
98267d33 388 uap->dmarx.auto_poll_rate = false;
8f898bfd 389 if (plat && plat->dma_rx_poll_enable) {
cb06ff10
CM
390 /* Set poll rate if specified. */
391 if (plat->dma_rx_poll_rate) {
392 uap->dmarx.auto_poll_rate = false;
393 uap->dmarx.poll_rate = plat->dma_rx_poll_rate;
394 } else {
395 /*
396 * 100 ms defaults to poll rate if not
397 * specified. This will be adjusted with
398 * the baud rate at set_termios.
399 */
400 uap->dmarx.auto_poll_rate = true;
401 uap->dmarx.poll_rate = 100;
402 }
403 /* 3 secs defaults poll_timeout if not specified. */
404 if (plat->dma_rx_poll_timeout)
405 uap->dmarx.poll_timeout =
406 plat->dma_rx_poll_timeout;
407 else
408 uap->dmarx.poll_timeout = 3000;
98267d33
AJ
409 } else if (!plat && dev->of_node) {
410 uap->dmarx.auto_poll_rate = of_property_read_bool(
411 dev->of_node, "auto-poll");
412 if (uap->dmarx.auto_poll_rate) {
413 u32 x;
414
415 if (0 == of_property_read_u32(dev->of_node,
416 "poll-rate-ms", &x))
417 uap->dmarx.poll_rate = x;
418 else
419 uap->dmarx.poll_rate = 100;
420 if (0 == of_property_read_u32(dev->of_node,
421 "poll-timeout-ms", &x))
422 uap->dmarx.poll_timeout = x;
423 else
424 uap->dmarx.poll_timeout = 3000;
425 }
426 }
ead76f32
LW
427 dev_info(uap->port.dev, "DMA channel RX %s\n",
428 dma_chan_name(uap->dmarx.chan));
429 }
68b65f73
RK
430}
431
68b65f73
RK
432static void pl011_dma_remove(struct uart_amba_port *uap)
433{
68b65f73
RK
434 if (uap->dmatx.chan)
435 dma_release_channel(uap->dmatx.chan);
ead76f32
LW
436 if (uap->dmarx.chan)
437 dma_release_channel(uap->dmarx.chan);
68b65f73
RK
438}
439
734745ca 440/* Forward declare these for the refill routine */
68b65f73 441static int pl011_dma_tx_refill(struct uart_amba_port *uap);
734745ca 442static void pl011_start_tx_pio(struct uart_amba_port *uap);
68b65f73
RK
443
444/*
445 * The current DMA TX buffer has been sent.
446 * Try to queue up another DMA buffer.
447 */
448static void pl011_dma_tx_callback(void *data)
449{
450 struct uart_amba_port *uap = data;
451 struct pl011_dmatx_data *dmatx = &uap->dmatx;
452 unsigned long flags;
453 u16 dmacr;
454
455 spin_lock_irqsave(&uap->port.lock, flags);
456 if (uap->dmatx.queued)
457 dma_unmap_sg(dmatx->chan->device->dev, &dmatx->sg, 1,
458 DMA_TO_DEVICE);
459
460 dmacr = uap->dmacr;
461 uap->dmacr = dmacr & ~UART011_TXDMAE;
9f25bc51 462 pl011_write(uap->dmacr, uap, REG_DMACR);
68b65f73
RK
463
464 /*
465 * If TX DMA was disabled, it means that we've stopped the DMA for
466 * some reason (eg, XOFF received, or we want to send an X-char.)
467 *
468 * Note: we need to be careful here of a potential race between DMA
469 * and the rest of the driver - if the driver disables TX DMA while
470 * a TX buffer completing, we must update the tx queued status to
471 * get further refills (hence we check dmacr).
472 */
473 if (!(dmacr & UART011_TXDMAE) || uart_tx_stopped(&uap->port) ||
474 uart_circ_empty(&uap->port.state->xmit)) {
475 uap->dmatx.queued = false;
476 spin_unlock_irqrestore(&uap->port.lock, flags);
477 return;
478 }
479
734745ca 480 if (pl011_dma_tx_refill(uap) <= 0)
68b65f73
RK
481 /*
482 * We didn't queue a DMA buffer for some reason, but we
483 * have data pending to be sent. Re-enable the TX IRQ.
484 */
734745ca
DM
485 pl011_start_tx_pio(uap);
486
68b65f73
RK
487 spin_unlock_irqrestore(&uap->port.lock, flags);
488}
489
490/*
491 * Try to refill the TX DMA buffer.
492 * Locking: called with port lock held and IRQs disabled.
493 * Returns:
494 * 1 if we queued up a TX DMA buffer.
495 * 0 if we didn't want to handle this by DMA
496 * <0 on error
497 */
498static int pl011_dma_tx_refill(struct uart_amba_port *uap)
499{
500 struct pl011_dmatx_data *dmatx = &uap->dmatx;
501 struct dma_chan *chan = dmatx->chan;
502 struct dma_device *dma_dev = chan->device;
503 struct dma_async_tx_descriptor *desc;
504 struct circ_buf *xmit = &uap->port.state->xmit;
505 unsigned int count;
506
507 /*
508 * Try to avoid the overhead involved in using DMA if the
509 * transaction fits in the first half of the FIFO, by using
510 * the standard interrupt handling. This ensures that we
511 * issue a uart_write_wakeup() at the appropriate time.
512 */
513 count = uart_circ_chars_pending(xmit);
514 if (count < (uap->fifosize >> 1)) {
515 uap->dmatx.queued = false;
516 return 0;
517 }
518
519 /*
520 * Bodge: don't send the last character by DMA, as this
521 * will prevent XON from notifying us to restart DMA.
522 */
523 count -= 1;
524
525 /* Else proceed to copy the TX chars to the DMA buffer and fire DMA */
526 if (count > PL011_DMA_BUFFER_SIZE)
527 count = PL011_DMA_BUFFER_SIZE;
528
529 if (xmit->tail < xmit->head)
530 memcpy(&dmatx->buf[0], &xmit->buf[xmit->tail], count);
531 else {
532 size_t first = UART_XMIT_SIZE - xmit->tail;
e2a545a6
AJ
533 size_t second;
534
535 if (first > count)
536 first = count;
537 second = count - first;
68b65f73
RK
538
539 memcpy(&dmatx->buf[0], &xmit->buf[xmit->tail], first);
540 if (second)
541 memcpy(&dmatx->buf[first], &xmit->buf[0], second);
542 }
543
544 dmatx->sg.length = count;
545
546 if (dma_map_sg(dma_dev->dev, &dmatx->sg, 1, DMA_TO_DEVICE) != 1) {
547 uap->dmatx.queued = false;
548 dev_dbg(uap->port.dev, "unable to map TX DMA\n");
549 return -EBUSY;
550 }
551
16052827 552 desc = dmaengine_prep_slave_sg(chan, &dmatx->sg, 1, DMA_MEM_TO_DEV,
68b65f73
RK
553 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
554 if (!desc) {
555 dma_unmap_sg(dma_dev->dev, &dmatx->sg, 1, DMA_TO_DEVICE);
556 uap->dmatx.queued = false;
557 /*
558 * If DMA cannot be used right now, we complete this
559 * transaction via IRQ and let the TTY layer retry.
560 */
561 dev_dbg(uap->port.dev, "TX DMA busy\n");
562 return -EBUSY;
563 }
564
565 /* Some data to go along to the callback */
566 desc->callback = pl011_dma_tx_callback;
567 desc->callback_param = uap;
568
569 /* All errors should happen at prepare time */
570 dmaengine_submit(desc);
571
572 /* Fire the DMA transaction */
573 dma_dev->device_issue_pending(chan);
574
575 uap->dmacr |= UART011_TXDMAE;
9f25bc51 576 pl011_write(uap->dmacr, uap, REG_DMACR);
68b65f73
RK
577 uap->dmatx.queued = true;
578
579 /*
580 * Now we know that DMA will fire, so advance the ring buffer
581 * with the stuff we just dispatched.
582 */
583 xmit->tail = (xmit->tail + count) & (UART_XMIT_SIZE - 1);
584 uap->port.icount.tx += count;
585
586 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
587 uart_write_wakeup(&uap->port);
588
589 return 1;
590}
591
592/*
593 * We received a transmit interrupt without a pending X-char but with
594 * pending characters.
595 * Locking: called with port lock held and IRQs disabled.
596 * Returns:
597 * false if we want to use PIO to transmit
598 * true if we queued a DMA buffer
599 */
600static bool pl011_dma_tx_irq(struct uart_amba_port *uap)
601{
ead76f32 602 if (!uap->using_tx_dma)
68b65f73
RK
603 return false;
604
605 /*
606 * If we already have a TX buffer queued, but received a
607 * TX interrupt, it will be because we've just sent an X-char.
608 * Ensure the TX DMA is enabled and the TX IRQ is disabled.
609 */
610 if (uap->dmatx.queued) {
611 uap->dmacr |= UART011_TXDMAE;
9f25bc51 612 pl011_write(uap->dmacr, uap, REG_DMACR);
68b65f73 613 uap->im &= ~UART011_TXIM;
9f25bc51 614 pl011_write(uap->im, uap, REG_IMSC);
68b65f73
RK
615 return true;
616 }
617
618 /*
619 * We don't have a TX buffer queued, so try to queue one.
25985edc 620 * If we successfully queued a buffer, mask the TX IRQ.
68b65f73
RK
621 */
622 if (pl011_dma_tx_refill(uap) > 0) {
623 uap->im &= ~UART011_TXIM;
9f25bc51 624 pl011_write(uap->im, uap, REG_IMSC);
68b65f73
RK
625 return true;
626 }
627 return false;
628}
629
630/*
631 * Stop the DMA transmit (eg, due to received XOFF).
632 * Locking: called with port lock held and IRQs disabled.
633 */
634static inline void pl011_dma_tx_stop(struct uart_amba_port *uap)
635{
636 if (uap->dmatx.queued) {
637 uap->dmacr &= ~UART011_TXDMAE;
9f25bc51 638 pl011_write(uap->dmacr, uap, REG_DMACR);
68b65f73
RK
639 }
640}
641
642/*
643 * Try to start a DMA transmit, or in the case of an XON/OFF
644 * character queued for send, try to get that character out ASAP.
645 * Locking: called with port lock held and IRQs disabled.
646 * Returns:
647 * false if we want the TX IRQ to be enabled
648 * true if we have a buffer queued
649 */
650static inline bool pl011_dma_tx_start(struct uart_amba_port *uap)
651{
652 u16 dmacr;
653
ead76f32 654 if (!uap->using_tx_dma)
68b65f73
RK
655 return false;
656
657 if (!uap->port.x_char) {
658 /* no X-char, try to push chars out in DMA mode */
659 bool ret = true;
660
661 if (!uap->dmatx.queued) {
662 if (pl011_dma_tx_refill(uap) > 0) {
663 uap->im &= ~UART011_TXIM;
9f25bc51 664 pl011_write(uap->im, uap, REG_IMSC);
734745ca 665 } else
68b65f73 666 ret = false;
68b65f73
RK
667 } else if (!(uap->dmacr & UART011_TXDMAE)) {
668 uap->dmacr |= UART011_TXDMAE;
9f25bc51 669 pl011_write(uap->dmacr, uap, REG_DMACR);
68b65f73
RK
670 }
671 return ret;
672 }
673
674 /*
675 * We have an X-char to send. Disable DMA to prevent it loading
676 * the TX fifo, and then see if we can stuff it into the FIFO.
677 */
678 dmacr = uap->dmacr;
679 uap->dmacr &= ~UART011_TXDMAE;
9f25bc51 680 pl011_write(uap->dmacr, uap, REG_DMACR);
68b65f73 681
9f25bc51 682 if (pl011_read(uap, REG_FR) & UART01x_FR_TXFF) {
68b65f73
RK
683 /*
684 * No space in the FIFO, so enable the transmit interrupt
685 * so we know when there is space. Note that once we've
686 * loaded the character, we should just re-enable DMA.
687 */
688 return false;
689 }
690
9f25bc51 691 pl011_write(uap->port.x_char, uap, REG_DR);
68b65f73
RK
692 uap->port.icount.tx++;
693 uap->port.x_char = 0;
694
695 /* Success - restore the DMA state */
696 uap->dmacr = dmacr;
9f25bc51 697 pl011_write(dmacr, uap, REG_DMACR);
68b65f73
RK
698
699 return true;
700}
701
702/*
703 * Flush the transmit buffer.
704 * Locking: called with port lock held and IRQs disabled.
705 */
706static void pl011_dma_flush_buffer(struct uart_port *port)
b83286bf
FE
707__releases(&uap->port.lock)
708__acquires(&uap->port.lock)
68b65f73 709{
a5820c24
DT
710 struct uart_amba_port *uap =
711 container_of(port, struct uart_amba_port, port);
68b65f73 712
ead76f32 713 if (!uap->using_tx_dma)
68b65f73
RK
714 return;
715
716 /* Avoid deadlock with the DMA engine callback */
717 spin_unlock(&uap->port.lock);
718 dmaengine_terminate_all(uap->dmatx.chan);
719 spin_lock(&uap->port.lock);
720 if (uap->dmatx.queued) {
721 dma_unmap_sg(uap->dmatx.chan->device->dev, &uap->dmatx.sg, 1,
722 DMA_TO_DEVICE);
723 uap->dmatx.queued = false;
724 uap->dmacr &= ~UART011_TXDMAE;
9f25bc51 725 pl011_write(uap->dmacr, uap, REG_DMACR);
68b65f73
RK
726 }
727}
728
ead76f32
LW
729static void pl011_dma_rx_callback(void *data);
730
731static int pl011_dma_rx_trigger_dma(struct uart_amba_port *uap)
732{
733 struct dma_chan *rxchan = uap->dmarx.chan;
ead76f32
LW
734 struct pl011_dmarx_data *dmarx = &uap->dmarx;
735 struct dma_async_tx_descriptor *desc;
736 struct pl011_sgbuf *sgbuf;
737
738 if (!rxchan)
739 return -EIO;
740
741 /* Start the RX DMA job */
742 sgbuf = uap->dmarx.use_buf_b ?
743 &uap->dmarx.sgbuf_b : &uap->dmarx.sgbuf_a;
16052827 744 desc = dmaengine_prep_slave_sg(rxchan, &sgbuf->sg, 1,
a485df4b 745 DMA_DEV_TO_MEM,
ead76f32
LW
746 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
747 /*
748 * If the DMA engine is busy and cannot prepare a
749 * channel, no big deal, the driver will fall back
750 * to interrupt mode as a result of this error code.
751 */
752 if (!desc) {
753 uap->dmarx.running = false;
754 dmaengine_terminate_all(rxchan);
755 return -EBUSY;
756 }
757
758 /* Some data to go along to the callback */
759 desc->callback = pl011_dma_rx_callback;
760 desc->callback_param = uap;
761 dmarx->cookie = dmaengine_submit(desc);
762 dma_async_issue_pending(rxchan);
763
764 uap->dmacr |= UART011_RXDMAE;
9f25bc51 765 pl011_write(uap->dmacr, uap, REG_DMACR);
ead76f32
LW
766 uap->dmarx.running = true;
767
768 uap->im &= ~UART011_RXIM;
9f25bc51 769 pl011_write(uap->im, uap, REG_IMSC);
ead76f32
LW
770
771 return 0;
772}
773
774/*
775 * This is called when either the DMA job is complete, or
776 * the FIFO timeout interrupt occurred. This must be called
777 * with the port spinlock uap->port.lock held.
778 */
779static void pl011_dma_rx_chars(struct uart_amba_port *uap,
780 u32 pending, bool use_buf_b,
781 bool readfifo)
782{
05c7cd39 783 struct tty_port *port = &uap->port.state->port;
ead76f32
LW
784 struct pl011_sgbuf *sgbuf = use_buf_b ?
785 &uap->dmarx.sgbuf_b : &uap->dmarx.sgbuf_a;
ead76f32
LW
786 int dma_count = 0;
787 u32 fifotaken = 0; /* only used for vdbg() */
788
cb06ff10
CM
789 struct pl011_dmarx_data *dmarx = &uap->dmarx;
790 int dmataken = 0;
791
792 if (uap->dmarx.poll_rate) {
793 /* The data can be taken by polling */
794 dmataken = sgbuf->sg.length - dmarx->last_residue;
795 /* Recalculate the pending size */
796 if (pending >= dmataken)
797 pending -= dmataken;
798 }
799
800 /* Pick the remain data from the DMA */
ead76f32 801 if (pending) {
ead76f32
LW
802
803 /*
804 * First take all chars in the DMA pipe, then look in the FIFO.
805 * Note that tty_insert_flip_buf() tries to take as many chars
806 * as it can.
807 */
cb06ff10
CM
808 dma_count = tty_insert_flip_string(port, sgbuf->buf + dmataken,
809 pending);
ead76f32
LW
810
811 uap->port.icount.rx += dma_count;
812 if (dma_count < pending)
813 dev_warn(uap->port.dev,
814 "couldn't insert all characters (TTY is full?)\n");
815 }
816
cb06ff10
CM
817 /* Reset the last_residue for Rx DMA poll */
818 if (uap->dmarx.poll_rate)
819 dmarx->last_residue = sgbuf->sg.length;
820
ead76f32
LW
821 /*
822 * Only continue with trying to read the FIFO if all DMA chars have
823 * been taken first.
824 */
825 if (dma_count == pending && readfifo) {
826 /* Clear any error flags */
75836339 827 pl011_write(UART011_OEIS | UART011_BEIS | UART011_PEIS |
9f25bc51 828 UART011_FEIS, uap, REG_ICR);
ead76f32
LW
829
830 /*
831 * If we read all the DMA'd characters, and we had an
29772c4e
LW
832 * incomplete buffer, that could be due to an rx error, or
833 * maybe we just timed out. Read any pending chars and check
834 * the error status.
835 *
836 * Error conditions will only occur in the FIFO, these will
837 * trigger an immediate interrupt and stop the DMA job, so we
838 * will always find the error in the FIFO, never in the DMA
839 * buffer.
ead76f32 840 */
29772c4e 841 fifotaken = pl011_fifo_to_tty(uap);
ead76f32
LW
842 }
843
844 spin_unlock(&uap->port.lock);
845 dev_vdbg(uap->port.dev,
846 "Took %d chars from DMA buffer and %d chars from the FIFO\n",
847 dma_count, fifotaken);
2e124b4a 848 tty_flip_buffer_push(port);
ead76f32
LW
849 spin_lock(&uap->port.lock);
850}
851
852static void pl011_dma_rx_irq(struct uart_amba_port *uap)
853{
854 struct pl011_dmarx_data *dmarx = &uap->dmarx;
855 struct dma_chan *rxchan = dmarx->chan;
856 struct pl011_sgbuf *sgbuf = dmarx->use_buf_b ?
857 &dmarx->sgbuf_b : &dmarx->sgbuf_a;
858 size_t pending;
859 struct dma_tx_state state;
860 enum dma_status dmastat;
861
862 /*
863 * Pause the transfer so we can trust the current counter,
864 * do this before we pause the PL011 block, else we may
865 * overflow the FIFO.
866 */
867 if (dmaengine_pause(rxchan))
868 dev_err(uap->port.dev, "unable to pause DMA transfer\n");
869 dmastat = rxchan->device->device_tx_status(rxchan,
870 dmarx->cookie, &state);
871 if (dmastat != DMA_PAUSED)
872 dev_err(uap->port.dev, "unable to pause DMA transfer\n");
873
874 /* Disable RX DMA - incoming data will wait in the FIFO */
875 uap->dmacr &= ~UART011_RXDMAE;
9f25bc51 876 pl011_write(uap->dmacr, uap, REG_DMACR);
ead76f32
LW
877 uap->dmarx.running = false;
878
879 pending = sgbuf->sg.length - state.residue;
880 BUG_ON(pending > PL011_DMA_BUFFER_SIZE);
881 /* Then we terminate the transfer - we now know our residue */
882 dmaengine_terminate_all(rxchan);
883
884 /*
885 * This will take the chars we have so far and insert
886 * into the framework.
887 */
888 pl011_dma_rx_chars(uap, pending, dmarx->use_buf_b, true);
889
890 /* Switch buffer & re-trigger DMA job */
891 dmarx->use_buf_b = !dmarx->use_buf_b;
892 if (pl011_dma_rx_trigger_dma(uap)) {
893 dev_dbg(uap->port.dev, "could not retrigger RX DMA job "
894 "fall back to interrupt mode\n");
895 uap->im |= UART011_RXIM;
9f25bc51 896 pl011_write(uap->im, uap, REG_IMSC);
ead76f32
LW
897 }
898}
899
900static void pl011_dma_rx_callback(void *data)
901{
902 struct uart_amba_port *uap = data;
903 struct pl011_dmarx_data *dmarx = &uap->dmarx;
6dc01aa6 904 struct dma_chan *rxchan = dmarx->chan;
ead76f32 905 bool lastbuf = dmarx->use_buf_b;
6dc01aa6
CM
906 struct pl011_sgbuf *sgbuf = dmarx->use_buf_b ?
907 &dmarx->sgbuf_b : &dmarx->sgbuf_a;
908 size_t pending;
909 struct dma_tx_state state;
ead76f32
LW
910 int ret;
911
912 /*
913 * This completion interrupt occurs typically when the
914 * RX buffer is totally stuffed but no timeout has yet
915 * occurred. When that happens, we just want the RX
916 * routine to flush out the secondary DMA buffer while
917 * we immediately trigger the next DMA job.
918 */
919 spin_lock_irq(&uap->port.lock);
6dc01aa6
CM
920 /*
921 * Rx data can be taken by the UART interrupts during
922 * the DMA irq handler. So we check the residue here.
923 */
924 rxchan->device->device_tx_status(rxchan, dmarx->cookie, &state);
925 pending = sgbuf->sg.length - state.residue;
926 BUG_ON(pending > PL011_DMA_BUFFER_SIZE);
927 /* Then we terminate the transfer - we now know our residue */
928 dmaengine_terminate_all(rxchan);
929
ead76f32
LW
930 uap->dmarx.running = false;
931 dmarx->use_buf_b = !lastbuf;
932 ret = pl011_dma_rx_trigger_dma(uap);
933
6dc01aa6 934 pl011_dma_rx_chars(uap, pending, lastbuf, false);
ead76f32
LW
935 spin_unlock_irq(&uap->port.lock);
936 /*
937 * Do this check after we picked the DMA chars so we don't
938 * get some IRQ immediately from RX.
939 */
940 if (ret) {
941 dev_dbg(uap->port.dev, "could not retrigger RX DMA job "
942 "fall back to interrupt mode\n");
943 uap->im |= UART011_RXIM;
9f25bc51 944 pl011_write(uap->im, uap, REG_IMSC);
ead76f32
LW
945 }
946}
947
948/*
949 * Stop accepting received characters, when we're shutting down or
950 * suspending this port.
951 * Locking: called with port lock held and IRQs disabled.
952 */
953static inline void pl011_dma_rx_stop(struct uart_amba_port *uap)
954{
955 /* FIXME. Just disable the DMA enable */
956 uap->dmacr &= ~UART011_RXDMAE;
9f25bc51 957 pl011_write(uap->dmacr, uap, REG_DMACR);
ead76f32 958}
68b65f73 959
cb06ff10
CM
960/*
961 * Timer handler for Rx DMA polling.
962 * Every polling, It checks the residue in the dma buffer and transfer
963 * data to the tty. Also, last_residue is updated for the next polling.
964 */
965static void pl011_dma_rx_poll(unsigned long args)
966{
967 struct uart_amba_port *uap = (struct uart_amba_port *)args;
968 struct tty_port *port = &uap->port.state->port;
969 struct pl011_dmarx_data *dmarx = &uap->dmarx;
970 struct dma_chan *rxchan = uap->dmarx.chan;
971 unsigned long flags = 0;
972 unsigned int dmataken = 0;
973 unsigned int size = 0;
974 struct pl011_sgbuf *sgbuf;
975 int dma_count;
976 struct dma_tx_state state;
977
978 sgbuf = dmarx->use_buf_b ? &uap->dmarx.sgbuf_b : &uap->dmarx.sgbuf_a;
979 rxchan->device->device_tx_status(rxchan, dmarx->cookie, &state);
980 if (likely(state.residue < dmarx->last_residue)) {
981 dmataken = sgbuf->sg.length - dmarx->last_residue;
982 size = dmarx->last_residue - state.residue;
983 dma_count = tty_insert_flip_string(port, sgbuf->buf + dmataken,
984 size);
985 if (dma_count == size)
986 dmarx->last_residue = state.residue;
987 dmarx->last_jiffies = jiffies;
988 }
989 tty_flip_buffer_push(port);
990
991 /*
992 * If no data is received in poll_timeout, the driver will fall back
993 * to interrupt mode. We will retrigger DMA at the first interrupt.
994 */
995 if (jiffies_to_msecs(jiffies - dmarx->last_jiffies)
996 > uap->dmarx.poll_timeout) {
997
998 spin_lock_irqsave(&uap->port.lock, flags);
999 pl011_dma_rx_stop(uap);
c25a1ad7 1000 uap->im |= UART011_RXIM;
9f25bc51 1001 pl011_write(uap->im, uap, REG_IMSC);
cb06ff10
CM
1002 spin_unlock_irqrestore(&uap->port.lock, flags);
1003
1004 uap->dmarx.running = false;
1005 dmaengine_terminate_all(rxchan);
1006 del_timer(&uap->dmarx.timer);
1007 } else {
1008 mod_timer(&uap->dmarx.timer,
1009 jiffies + msecs_to_jiffies(uap->dmarx.poll_rate));
1010 }
1011}
1012
68b65f73
RK
1013static void pl011_dma_startup(struct uart_amba_port *uap)
1014{
ead76f32
LW
1015 int ret;
1016
1c9be310
JRO
1017 if (!uap->dma_probed)
1018 pl011_dma_probe(uap);
1019
68b65f73
RK
1020 if (!uap->dmatx.chan)
1021 return;
1022
4c0be45b 1023 uap->dmatx.buf = kmalloc(PL011_DMA_BUFFER_SIZE, GFP_KERNEL | __GFP_DMA);
68b65f73
RK
1024 if (!uap->dmatx.buf) {
1025 dev_err(uap->port.dev, "no memory for DMA TX buffer\n");
1026 uap->port.fifosize = uap->fifosize;
1027 return;
1028 }
1029
1030 sg_init_one(&uap->dmatx.sg, uap->dmatx.buf, PL011_DMA_BUFFER_SIZE);
1031
1032 /* The DMA buffer is now the FIFO the TTY subsystem can use */
1033 uap->port.fifosize = PL011_DMA_BUFFER_SIZE;
ead76f32
LW
1034 uap->using_tx_dma = true;
1035
1036 if (!uap->dmarx.chan)
1037 goto skip_rx;
1038
1039 /* Allocate and map DMA RX buffers */
1040 ret = pl011_sgbuf_init(uap->dmarx.chan, &uap->dmarx.sgbuf_a,
1041 DMA_FROM_DEVICE);
1042 if (ret) {
1043 dev_err(uap->port.dev, "failed to init DMA %s: %d\n",
1044 "RX buffer A", ret);
1045 goto skip_rx;
1046 }
68b65f73 1047
ead76f32
LW
1048 ret = pl011_sgbuf_init(uap->dmarx.chan, &uap->dmarx.sgbuf_b,
1049 DMA_FROM_DEVICE);
1050 if (ret) {
1051 dev_err(uap->port.dev, "failed to init DMA %s: %d\n",
1052 "RX buffer B", ret);
1053 pl011_sgbuf_free(uap->dmarx.chan, &uap->dmarx.sgbuf_a,
1054 DMA_FROM_DEVICE);
1055 goto skip_rx;
1056 }
1057
1058 uap->using_rx_dma = true;
68b65f73 1059
ead76f32 1060skip_rx:
68b65f73
RK
1061 /* Turn on DMA error (RX/TX will be enabled on demand) */
1062 uap->dmacr |= UART011_DMAONERR;
9f25bc51 1063 pl011_write(uap->dmacr, uap, REG_DMACR);
38d62436
RK
1064
1065 /*
1066 * ST Micro variants has some specific dma burst threshold
1067 * compensation. Set this to 16 bytes, so burst will only
1068 * be issued above/below 16 bytes.
1069 */
1070 if (uap->vendor->dma_threshold)
75836339 1071 pl011_write(ST_UART011_DMAWM_RX_16 | ST_UART011_DMAWM_TX_16,
9f25bc51 1072 uap, REG_ST_DMAWM);
ead76f32
LW
1073
1074 if (uap->using_rx_dma) {
1075 if (pl011_dma_rx_trigger_dma(uap))
1076 dev_dbg(uap->port.dev, "could not trigger initial "
1077 "RX DMA job, fall back to interrupt mode\n");
cb06ff10
CM
1078 if (uap->dmarx.poll_rate) {
1079 init_timer(&(uap->dmarx.timer));
1080 uap->dmarx.timer.function = pl011_dma_rx_poll;
1081 uap->dmarx.timer.data = (unsigned long)uap;
1082 mod_timer(&uap->dmarx.timer,
1083 jiffies +
1084 msecs_to_jiffies(uap->dmarx.poll_rate));
1085 uap->dmarx.last_residue = PL011_DMA_BUFFER_SIZE;
1086 uap->dmarx.last_jiffies = jiffies;
1087 }
ead76f32 1088 }
68b65f73
RK
1089}
1090
1091static void pl011_dma_shutdown(struct uart_amba_port *uap)
1092{
ead76f32 1093 if (!(uap->using_tx_dma || uap->using_rx_dma))
68b65f73
RK
1094 return;
1095
1096 /* Disable RX and TX DMA */
9f25bc51 1097 while (pl011_read(uap, REG_FR) & UART01x_FR_BUSY)
68b65f73
RK
1098 barrier();
1099
1100 spin_lock_irq(&uap->port.lock);
1101 uap->dmacr &= ~(UART011_DMAONERR | UART011_RXDMAE | UART011_TXDMAE);
9f25bc51 1102 pl011_write(uap->dmacr, uap, REG_DMACR);
68b65f73
RK
1103 spin_unlock_irq(&uap->port.lock);
1104
ead76f32
LW
1105 if (uap->using_tx_dma) {
1106 /* In theory, this should already be done by pl011_dma_flush_buffer */
1107 dmaengine_terminate_all(uap->dmatx.chan);
1108 if (uap->dmatx.queued) {
1109 dma_unmap_sg(uap->dmatx.chan->device->dev, &uap->dmatx.sg, 1,
1110 DMA_TO_DEVICE);
1111 uap->dmatx.queued = false;
1112 }
1113
1114 kfree(uap->dmatx.buf);
1115 uap->using_tx_dma = false;
68b65f73
RK
1116 }
1117
ead76f32
LW
1118 if (uap->using_rx_dma) {
1119 dmaengine_terminate_all(uap->dmarx.chan);
1120 /* Clean up the RX DMA */
1121 pl011_sgbuf_free(uap->dmarx.chan, &uap->dmarx.sgbuf_a, DMA_FROM_DEVICE);
1122 pl011_sgbuf_free(uap->dmarx.chan, &uap->dmarx.sgbuf_b, DMA_FROM_DEVICE);
cb06ff10
CM
1123 if (uap->dmarx.poll_rate)
1124 del_timer_sync(&uap->dmarx.timer);
ead76f32
LW
1125 uap->using_rx_dma = false;
1126 }
1127}
68b65f73 1128
ead76f32
LW
1129static inline bool pl011_dma_rx_available(struct uart_amba_port *uap)
1130{
1131 return uap->using_rx_dma;
68b65f73
RK
1132}
1133
ead76f32
LW
1134static inline bool pl011_dma_rx_running(struct uart_amba_port *uap)
1135{
1136 return uap->using_rx_dma && uap->dmarx.running;
1137}
1138
68b65f73
RK
1139#else
1140/* Blank functions if the DMA engine is not available */
1c9be310 1141static inline void pl011_dma_probe(struct uart_amba_port *uap)
68b65f73
RK
1142{
1143}
1144
1145static inline void pl011_dma_remove(struct uart_amba_port *uap)
1146{
1147}
1148
1149static inline void pl011_dma_startup(struct uart_amba_port *uap)
1150{
1151}
1152
1153static inline void pl011_dma_shutdown(struct uart_amba_port *uap)
1154{
1155}
1156
1157static inline bool pl011_dma_tx_irq(struct uart_amba_port *uap)
1158{
1159 return false;
1160}
1161
1162static inline void pl011_dma_tx_stop(struct uart_amba_port *uap)
1163{
1164}
1165
1166static inline bool pl011_dma_tx_start(struct uart_amba_port *uap)
1167{
1168 return false;
1169}
1170
ead76f32
LW
1171static inline void pl011_dma_rx_irq(struct uart_amba_port *uap)
1172{
1173}
1174
1175static inline void pl011_dma_rx_stop(struct uart_amba_port *uap)
1176{
1177}
1178
1179static inline int pl011_dma_rx_trigger_dma(struct uart_amba_port *uap)
1180{
1181 return -EIO;
1182}
1183
1184static inline bool pl011_dma_rx_available(struct uart_amba_port *uap)
1185{
1186 return false;
1187}
1188
1189static inline bool pl011_dma_rx_running(struct uart_amba_port *uap)
1190{
1191 return false;
1192}
1193
68b65f73
RK
1194#define pl011_dma_flush_buffer NULL
1195#endif
1196
b129a8cc 1197static void pl011_stop_tx(struct uart_port *port)
1da177e4 1198{
a5820c24
DT
1199 struct uart_amba_port *uap =
1200 container_of(port, struct uart_amba_port, port);
1da177e4
LT
1201
1202 uap->im &= ~UART011_TXIM;
9f25bc51 1203 pl011_write(uap->im, uap, REG_IMSC);
68b65f73 1204 pl011_dma_tx_stop(uap);
1da177e4
LT
1205}
1206
1e84d223 1207static void pl011_tx_chars(struct uart_amba_port *uap, bool from_irq);
734745ca
DM
1208
1209/* Start TX with programmed I/O only (no DMA) */
1210static void pl011_start_tx_pio(struct uart_amba_port *uap)
1211{
1212 uap->im |= UART011_TXIM;
9f25bc51 1213 pl011_write(uap->im, uap, REG_IMSC);
1e84d223 1214 pl011_tx_chars(uap, false);
734745ca
DM
1215}
1216
b129a8cc 1217static void pl011_start_tx(struct uart_port *port)
1da177e4 1218{
a5820c24
DT
1219 struct uart_amba_port *uap =
1220 container_of(port, struct uart_amba_port, port);
1da177e4 1221
734745ca
DM
1222 if (!pl011_dma_tx_start(uap))
1223 pl011_start_tx_pio(uap);
1da177e4
LT
1224}
1225
1226static void pl011_stop_rx(struct uart_port *port)
1227{
a5820c24
DT
1228 struct uart_amba_port *uap =
1229 container_of(port, struct uart_amba_port, port);
1da177e4
LT
1230
1231 uap->im &= ~(UART011_RXIM|UART011_RTIM|UART011_FEIM|
1232 UART011_PEIM|UART011_BEIM|UART011_OEIM);
9f25bc51 1233 pl011_write(uap->im, uap, REG_IMSC);
ead76f32
LW
1234
1235 pl011_dma_rx_stop(uap);
1da177e4
LT
1236}
1237
1238static void pl011_enable_ms(struct uart_port *port)
1239{
a5820c24
DT
1240 struct uart_amba_port *uap =
1241 container_of(port, struct uart_amba_port, port);
1da177e4
LT
1242
1243 uap->im |= UART011_RIMIM|UART011_CTSMIM|UART011_DCDMIM|UART011_DSRMIM;
9f25bc51 1244 pl011_write(uap->im, uap, REG_IMSC);
1da177e4
LT
1245}
1246
7d12e780 1247static void pl011_rx_chars(struct uart_amba_port *uap)
b83286bf
FE
1248__releases(&uap->port.lock)
1249__acquires(&uap->port.lock)
1da177e4 1250{
29772c4e 1251 pl011_fifo_to_tty(uap);
1da177e4 1252
2389b272 1253 spin_unlock(&uap->port.lock);
2e124b4a 1254 tty_flip_buffer_push(&uap->port.state->port);
ead76f32
LW
1255 /*
1256 * If we were temporarily out of DMA mode for a while,
1257 * attempt to switch back to DMA mode again.
1258 */
1259 if (pl011_dma_rx_available(uap)) {
1260 if (pl011_dma_rx_trigger_dma(uap)) {
1261 dev_dbg(uap->port.dev, "could not trigger RX DMA job "
1262 "fall back to interrupt mode again\n");
1263 uap->im |= UART011_RXIM;
9f25bc51 1264 pl011_write(uap->im, uap, REG_IMSC);
cb06ff10 1265 } else {
89fa28db 1266#ifdef CONFIG_DMA_ENGINE
cb06ff10
CM
1267 /* Start Rx DMA poll */
1268 if (uap->dmarx.poll_rate) {
1269 uap->dmarx.last_jiffies = jiffies;
1270 uap->dmarx.last_residue = PL011_DMA_BUFFER_SIZE;
1271 mod_timer(&uap->dmarx.timer,
1272 jiffies +
1273 msecs_to_jiffies(uap->dmarx.poll_rate));
1274 }
89fa28db 1275#endif
cb06ff10 1276 }
ead76f32 1277 }
2389b272 1278 spin_lock(&uap->port.lock);
1da177e4
LT
1279}
1280
1e84d223
DM
1281static bool pl011_tx_char(struct uart_amba_port *uap, unsigned char c,
1282 bool from_irq)
734745ca 1283{
1e84d223 1284 if (unlikely(!from_irq) &&
9f25bc51 1285 pl011_read(uap, REG_FR) & UART01x_FR_TXFF)
1e84d223
DM
1286 return false; /* unable to transmit character */
1287
9f25bc51 1288 pl011_write(c, uap, REG_DR);
734745ca
DM
1289 uap->port.icount.tx++;
1290
1e84d223 1291 return true;
734745ca
DM
1292}
1293
1e84d223 1294static void pl011_tx_chars(struct uart_amba_port *uap, bool from_irq)
1da177e4 1295{
ebd2c8f6 1296 struct circ_buf *xmit = &uap->port.state->xmit;
1e84d223 1297 int count = uap->fifosize >> 1;
734745ca 1298
1da177e4 1299 if (uap->port.x_char) {
1e84d223
DM
1300 if (!pl011_tx_char(uap, uap->port.x_char, from_irq))
1301 return;
1da177e4 1302 uap->port.x_char = 0;
734745ca 1303 --count;
1da177e4
LT
1304 }
1305 if (uart_circ_empty(xmit) || uart_tx_stopped(&uap->port)) {
b129a8cc 1306 pl011_stop_tx(&uap->port);
1e84d223 1307 return;
1da177e4
LT
1308 }
1309
68b65f73
RK
1310 /* If we are using DMA mode, try to send some characters. */
1311 if (pl011_dma_tx_irq(uap))
1e84d223 1312 return;
68b65f73 1313
1e84d223
DM
1314 do {
1315 if (likely(from_irq) && count-- == 0)
1da177e4 1316 break;
1e84d223
DM
1317
1318 if (!pl011_tx_char(uap, xmit->buf[xmit->tail], from_irq))
1319 break;
1320
1321 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
1322 } while (!uart_circ_empty(xmit));
1da177e4
LT
1323
1324 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
1325 uart_write_wakeup(&uap->port);
1326
1e84d223 1327 if (uart_circ_empty(xmit))
b129a8cc 1328 pl011_stop_tx(&uap->port);
1da177e4
LT
1329}
1330
1331static void pl011_modem_status(struct uart_amba_port *uap)
1332{
1333 unsigned int status, delta;
1334
9f25bc51 1335 status = pl011_read(uap, REG_FR) & UART01x_FR_MODEM_ANY;
1da177e4
LT
1336
1337 delta = status ^ uap->old_status;
1338 uap->old_status = status;
1339
1340 if (!delta)
1341 return;
1342
1343 if (delta & UART01x_FR_DCD)
1344 uart_handle_dcd_change(&uap->port, status & UART01x_FR_DCD);
1345
062a68a5 1346 if (delta & UART01x_FR_DSR)
1da177e4
LT
1347 uap->port.icount.dsr++;
1348
062a68a5
GKH
1349 if (delta & UART01x_FR_CTS)
1350 uart_handle_cts_change(&uap->port, status & UART01x_FR_CTS);
1da177e4 1351
bdc04e31 1352 wake_up_interruptible(&uap->port.state->port.delta_msr_wait);
1da177e4
LT
1353}
1354
9c4ef4b0
AP
1355static void check_apply_cts_event_workaround(struct uart_amba_port *uap)
1356{
1357 unsigned int dummy_read;
1358
1359 if (!uap->vendor->cts_event_workaround)
1360 return;
1361
1362 /* workaround to make sure that all bits are unlocked.. */
9f25bc51 1363 pl011_write(0x00, uap, REG_ICR);
9c4ef4b0
AP
1364
1365 /*
1366 * WA: introduce 26ns(1 uart clk) delay before W1C;
1367 * single apb access will incur 2 pclk(133.12Mhz) delay,
1368 * so add 2 dummy reads
1369 */
9f25bc51
RK
1370 dummy_read = pl011_read(uap, REG_ICR);
1371 dummy_read = pl011_read(uap, REG_ICR);
9c4ef4b0
AP
1372}
1373
7d12e780 1374static irqreturn_t pl011_int(int irq, void *dev_id)
1da177e4
LT
1375{
1376 struct uart_amba_port *uap = dev_id;
963cc981 1377 unsigned long flags;
1da177e4 1378 unsigned int status, pass_counter = AMBA_ISR_PASS_LIMIT;
075167ed 1379 u16 imsc;
1da177e4
LT
1380 int handled = 0;
1381
963cc981 1382 spin_lock_irqsave(&uap->port.lock, flags);
9f25bc51
RK
1383 imsc = pl011_read(uap, REG_IMSC);
1384 status = pl011_read(uap, REG_RIS) & imsc;
1da177e4
LT
1385 if (status) {
1386 do {
9c4ef4b0 1387 check_apply_cts_event_workaround(uap);
f11c9841 1388
75836339
RK
1389 pl011_write(status & ~(UART011_TXIS|UART011_RTIS|
1390 UART011_RXIS),
9f25bc51 1391 uap, REG_ICR);
1da177e4 1392
ead76f32
LW
1393 if (status & (UART011_RTIS|UART011_RXIS)) {
1394 if (pl011_dma_rx_running(uap))
1395 pl011_dma_rx_irq(uap);
1396 else
1397 pl011_rx_chars(uap);
1398 }
1da177e4
LT
1399 if (status & (UART011_DSRMIS|UART011_DCDMIS|
1400 UART011_CTSMIS|UART011_RIMIS))
1401 pl011_modem_status(uap);
1e84d223
DM
1402 if (status & UART011_TXIS)
1403 pl011_tx_chars(uap, true);
1da177e4 1404
4fd0690b 1405 if (pass_counter-- == 0)
1da177e4
LT
1406 break;
1407
9f25bc51 1408 status = pl011_read(uap, REG_RIS) & imsc;
1da177e4
LT
1409 } while (status != 0);
1410 handled = 1;
1411 }
1412
963cc981 1413 spin_unlock_irqrestore(&uap->port.lock, flags);
1da177e4
LT
1414
1415 return IRQ_RETVAL(handled);
1416}
1417
e643f87f 1418static unsigned int pl011_tx_empty(struct uart_port *port)
1da177e4 1419{
a5820c24
DT
1420 struct uart_amba_port *uap =
1421 container_of(port, struct uart_amba_port, port);
9f25bc51 1422 unsigned int status = pl011_read(uap, REG_FR);
062a68a5 1423 return status & (UART01x_FR_BUSY|UART01x_FR_TXFF) ? 0 : TIOCSER_TEMT;
1da177e4
LT
1424}
1425
e643f87f 1426static unsigned int pl011_get_mctrl(struct uart_port *port)
1da177e4 1427{
a5820c24
DT
1428 struct uart_amba_port *uap =
1429 container_of(port, struct uart_amba_port, port);
1da177e4 1430 unsigned int result = 0;
9f25bc51 1431 unsigned int status = pl011_read(uap, REG_FR);
1da177e4 1432
5159f407 1433#define TIOCMBIT(uartbit, tiocmbit) \
1da177e4
LT
1434 if (status & uartbit) \
1435 result |= tiocmbit
1436
5159f407 1437 TIOCMBIT(UART01x_FR_DCD, TIOCM_CAR);
062a68a5
GKH
1438 TIOCMBIT(UART01x_FR_DSR, TIOCM_DSR);
1439 TIOCMBIT(UART01x_FR_CTS, TIOCM_CTS);
1440 TIOCMBIT(UART011_FR_RI, TIOCM_RNG);
5159f407 1441#undef TIOCMBIT
1da177e4
LT
1442 return result;
1443}
1444
1445static void pl011_set_mctrl(struct uart_port *port, unsigned int mctrl)
1446{
a5820c24
DT
1447 struct uart_amba_port *uap =
1448 container_of(port, struct uart_amba_port, port);
1da177e4
LT
1449 unsigned int cr;
1450
9f25bc51 1451 cr = pl011_read(uap, REG_CR);
1da177e4 1452
5159f407 1453#define TIOCMBIT(tiocmbit, uartbit) \
1da177e4
LT
1454 if (mctrl & tiocmbit) \
1455 cr |= uartbit; \
1456 else \
1457 cr &= ~uartbit
1458
5159f407
JS
1459 TIOCMBIT(TIOCM_RTS, UART011_CR_RTS);
1460 TIOCMBIT(TIOCM_DTR, UART011_CR_DTR);
1461 TIOCMBIT(TIOCM_OUT1, UART011_CR_OUT1);
1462 TIOCMBIT(TIOCM_OUT2, UART011_CR_OUT2);
1463 TIOCMBIT(TIOCM_LOOP, UART011_CR_LBE);
3b43816f
RV
1464
1465 if (uap->autorts) {
1466 /* We need to disable auto-RTS if we want to turn RTS off */
1467 TIOCMBIT(TIOCM_RTS, UART011_CR_RTSEN);
1468 }
5159f407 1469#undef TIOCMBIT
1da177e4 1470
9f25bc51 1471 pl011_write(cr, uap, REG_CR);
1da177e4
LT
1472}
1473
1474static void pl011_break_ctl(struct uart_port *port, int break_state)
1475{
a5820c24
DT
1476 struct uart_amba_port *uap =
1477 container_of(port, struct uart_amba_port, port);
1da177e4
LT
1478 unsigned long flags;
1479 unsigned int lcr_h;
1480
1481 spin_lock_irqsave(&uap->port.lock, flags);
b2a4e24c 1482 lcr_h = pl011_read(uap, uap->lcrh_tx);
1da177e4
LT
1483 if (break_state == -1)
1484 lcr_h |= UART01x_LCRH_BRK;
1485 else
1486 lcr_h &= ~UART01x_LCRH_BRK;
b2a4e24c 1487 pl011_write(lcr_h, uap, uap->lcrh_tx);
1da177e4
LT
1488 spin_unlock_irqrestore(&uap->port.lock, flags);
1489}
1490
84b5ae15 1491#ifdef CONFIG_CONSOLE_POLL
5c8124a0
AV
1492
1493static void pl011_quiesce_irqs(struct uart_port *port)
1494{
a5820c24
DT
1495 struct uart_amba_port *uap =
1496 container_of(port, struct uart_amba_port, port);
5c8124a0 1497
9f25bc51 1498 pl011_write(pl011_read(uap, REG_MIS), uap, REG_ICR);
5c8124a0
AV
1499 /*
1500 * There is no way to clear TXIM as this is "ready to transmit IRQ", so
1501 * we simply mask it. start_tx() will unmask it.
1502 *
1503 * Note we can race with start_tx(), and if the race happens, the
1504 * polling user might get another interrupt just after we clear it.
1505 * But it should be OK and can happen even w/o the race, e.g.
1506 * controller immediately got some new data and raised the IRQ.
1507 *
1508 * And whoever uses polling routines assumes that it manages the device
1509 * (including tx queue), so we're also fine with start_tx()'s caller
1510 * side.
1511 */
9f25bc51
RK
1512 pl011_write(pl011_read(uap, REG_IMSC) & ~UART011_TXIM, uap,
1513 REG_IMSC);
5c8124a0
AV
1514}
1515
e643f87f 1516static int pl011_get_poll_char(struct uart_port *port)
84b5ae15 1517{
a5820c24
DT
1518 struct uart_amba_port *uap =
1519 container_of(port, struct uart_amba_port, port);
84b5ae15
JW
1520 unsigned int status;
1521
5c8124a0
AV
1522 /*
1523 * The caller might need IRQs lowered, e.g. if used with KDB NMI
1524 * debugger.
1525 */
1526 pl011_quiesce_irqs(port);
1527
9f25bc51 1528 status = pl011_read(uap, REG_FR);
f5316b4a
JW
1529 if (status & UART01x_FR_RXFE)
1530 return NO_POLL_CHAR;
84b5ae15 1531
9f25bc51 1532 return pl011_read(uap, REG_DR);
84b5ae15
JW
1533}
1534
e643f87f 1535static void pl011_put_poll_char(struct uart_port *port,
84b5ae15
JW
1536 unsigned char ch)
1537{
a5820c24
DT
1538 struct uart_amba_port *uap =
1539 container_of(port, struct uart_amba_port, port);
84b5ae15 1540
9f25bc51 1541 while (pl011_read(uap, REG_FR) & UART01x_FR_TXFF)
84b5ae15
JW
1542 barrier();
1543
9f25bc51 1544 pl011_write(ch, uap, REG_DR);
84b5ae15
JW
1545}
1546
1547#endif /* CONFIG_CONSOLE_POLL */
1548
b3564c2c 1549static int pl011_hwinit(struct uart_port *port)
1da177e4 1550{
a5820c24
DT
1551 struct uart_amba_port *uap =
1552 container_of(port, struct uart_amba_port, port);
1da177e4
LT
1553 int retval;
1554
78d80c5a 1555 /* Optionaly enable pins to be muxed in and configured */
2b996fc5 1556 pinctrl_pm_select_default_state(port->dev);
78d80c5a 1557
1da177e4
LT
1558 /*
1559 * Try to enable the clock producer.
1560 */
1c4c4394 1561 retval = clk_prepare_enable(uap->clk);
1da177e4 1562 if (retval)
7f6d942a 1563 return retval;
1da177e4
LT
1564
1565 uap->port.uartclk = clk_get_rate(uap->clk);
1566
9b96fbac 1567 /* Clear pending error and receive interrupts */
75836339
RK
1568 pl011_write(UART011_OEIS | UART011_BEIS | UART011_PEIS |
1569 UART011_FEIS | UART011_RTIS | UART011_RXIS,
9f25bc51 1570 uap, REG_ICR);
9b96fbac 1571
b3564c2c
AV
1572 /*
1573 * Save interrupts enable mask, and enable RX interrupts in case if
1574 * the interrupt is used for NMI entry.
1575 */
9f25bc51
RK
1576 uap->im = pl011_read(uap, REG_IMSC);
1577 pl011_write(UART011_RTIM | UART011_RXIM, uap, REG_IMSC);
b3564c2c 1578
574de559 1579 if (dev_get_platdata(uap->port.dev)) {
b3564c2c
AV
1580 struct amba_pl011_data *plat;
1581
574de559 1582 plat = dev_get_platdata(uap->port.dev);
b3564c2c
AV
1583 if (plat->init)
1584 plat->init();
1585 }
1586 return 0;
b3564c2c
AV
1587}
1588
7fe9a5a9
RK
1589static bool pl011_split_lcrh(const struct uart_amba_port *uap)
1590{
9f25bc51
RK
1591 return pl011_reg_to_offset(uap, uap->lcrh_rx) !=
1592 pl011_reg_to_offset(uap, uap->lcrh_tx);
7fe9a5a9
RK
1593}
1594
b60f2f66
JM
1595static void pl011_write_lcr_h(struct uart_amba_port *uap, unsigned int lcr_h)
1596{
b2a4e24c 1597 pl011_write(lcr_h, uap, uap->lcrh_rx);
7fe9a5a9 1598 if (pl011_split_lcrh(uap)) {
b60f2f66
JM
1599 int i;
1600 /*
1601 * Wait 10 PCLKs before writing LCRH_TX register,
1602 * to get this delay write read only register 10 times
1603 */
1604 for (i = 0; i < 10; ++i)
9f25bc51 1605 pl011_write(0xff, uap, REG_MIS);
b2a4e24c 1606 pl011_write(lcr_h, uap, uap->lcrh_tx);
b60f2f66
JM
1607 }
1608}
1609
867b8e8e
AP
1610static int pl011_allocate_irq(struct uart_amba_port *uap)
1611{
9f25bc51 1612 pl011_write(uap->im, uap, REG_IMSC);
867b8e8e
AP
1613
1614 return request_irq(uap->port.irq, pl011_int, 0, "uart-pl011", uap);
1615}
1616
1617/*
1618 * Enable interrupts, only timeouts when using DMA
1619 * if initial RX DMA job failed, start in interrupt mode
1620 * as well.
1621 */
1622static void pl011_enable_interrupts(struct uart_amba_port *uap)
1623{
1624 spin_lock_irq(&uap->port.lock);
1625
1626 /* Clear out any spuriously appearing RX interrupts */
9f25bc51 1627 pl011_write(UART011_RTIS | UART011_RXIS, uap, REG_ICR);
867b8e8e
AP
1628 uap->im = UART011_RTIM;
1629 if (!pl011_dma_rx_running(uap))
1630 uap->im |= UART011_RXIM;
9f25bc51 1631 pl011_write(uap->im, uap, REG_IMSC);
867b8e8e
AP
1632 spin_unlock_irq(&uap->port.lock);
1633}
1634
b3564c2c
AV
1635static int pl011_startup(struct uart_port *port)
1636{
a5820c24
DT
1637 struct uart_amba_port *uap =
1638 container_of(port, struct uart_amba_port, port);
734745ca 1639 unsigned int cr;
b3564c2c
AV
1640 int retval;
1641
1642 retval = pl011_hwinit(port);
1643 if (retval)
1644 goto clk_dis;
1645
867b8e8e 1646 retval = pl011_allocate_irq(uap);
1da177e4
LT
1647 if (retval)
1648 goto clk_dis;
1649
9f25bc51 1650 pl011_write(uap->vendor->ifls, uap, REG_IFLS);
1da177e4 1651
734745ca 1652 spin_lock_irq(&uap->port.lock);
570d2910 1653
d8d8ffa4
SKS
1654 /* restore RTS and DTR */
1655 cr = uap->old_cr & (UART011_CR_RTS | UART011_CR_DTR);
1656 cr |= UART01x_CR_UARTEN | UART011_CR_RXE | UART011_CR_TXE;
9f25bc51 1657 pl011_write(cr, uap, REG_CR);
1da177e4 1658
fe433907
JM
1659 spin_unlock_irq(&uap->port.lock);
1660
1da177e4
LT
1661 /*
1662 * initialise the old status of the modem signals
1663 */
9f25bc51 1664 uap->old_status = pl011_read(uap, REG_FR) & UART01x_FR_MODEM_ANY;
1da177e4 1665
68b65f73
RK
1666 /* Startup DMA */
1667 pl011_dma_startup(uap);
1668
867b8e8e 1669 pl011_enable_interrupts(uap);
1da177e4
LT
1670
1671 return 0;
1672
1673 clk_dis:
1c4c4394 1674 clk_disable_unprepare(uap->clk);
1da177e4
LT
1675 return retval;
1676}
1677
0dd1e247
AP
1678static int sbsa_uart_startup(struct uart_port *port)
1679{
1680 struct uart_amba_port *uap =
1681 container_of(port, struct uart_amba_port, port);
1682 int retval;
1683
1684 retval = pl011_hwinit(port);
1685 if (retval)
1686 return retval;
1687
1688 retval = pl011_allocate_irq(uap);
1689 if (retval)
1690 return retval;
1691
1692 /* The SBSA UART does not support any modem status lines. */
1693 uap->old_status = 0;
1694
1695 pl011_enable_interrupts(uap);
1696
1697 return 0;
1698}
1699
ec489aa8
LW
1700static void pl011_shutdown_channel(struct uart_amba_port *uap,
1701 unsigned int lcrh)
1702{
f11c9841 1703 unsigned long val;
ec489aa8 1704
b2a4e24c 1705 val = pl011_read(uap, lcrh);
f11c9841 1706 val &= ~(UART01x_LCRH_BRK | UART01x_LCRH_FEN);
b2a4e24c 1707 pl011_write(val, uap, lcrh);
ec489aa8
LW
1708}
1709
95166a3f
AP
1710/*
1711 * disable the port. It should not disable RTS and DTR.
1712 * Also RTS and DTR state should be preserved to restore
1713 * it during startup().
1714 */
1715static void pl011_disable_uart(struct uart_amba_port *uap)
1da177e4 1716{
d8d8ffa4 1717 unsigned int cr;
1da177e4 1718
3b43816f 1719 uap->autorts = false;
fe433907 1720 spin_lock_irq(&uap->port.lock);
9f25bc51 1721 cr = pl011_read(uap, REG_CR);
d8d8ffa4
SKS
1722 uap->old_cr = cr;
1723 cr &= UART011_CR_RTS | UART011_CR_DTR;
1724 cr |= UART01x_CR_UARTEN | UART011_CR_TXE;
9f25bc51 1725 pl011_write(cr, uap, REG_CR);
fe433907 1726 spin_unlock_irq(&uap->port.lock);
1da177e4
LT
1727
1728 /*
1729 * disable break condition and fifos
1730 */
ec489aa8 1731 pl011_shutdown_channel(uap, uap->lcrh_rx);
7fe9a5a9 1732 if (pl011_split_lcrh(uap))
ec489aa8 1733 pl011_shutdown_channel(uap, uap->lcrh_tx);
95166a3f
AP
1734}
1735
1736static void pl011_disable_interrupts(struct uart_amba_port *uap)
1737{
1738 spin_lock_irq(&uap->port.lock);
1739
1740 /* mask all interrupts and clear all pending ones */
1741 uap->im = 0;
9f25bc51
RK
1742 pl011_write(uap->im, uap, REG_IMSC);
1743 pl011_write(0xffff, uap, REG_ICR);
95166a3f
AP
1744
1745 spin_unlock_irq(&uap->port.lock);
1746}
1747
1748static void pl011_shutdown(struct uart_port *port)
1749{
1750 struct uart_amba_port *uap =
1751 container_of(port, struct uart_amba_port, port);
1752
1753 pl011_disable_interrupts(uap);
1754
1755 pl011_dma_shutdown(uap);
1756
1757 free_irq(uap->port.irq, uap);
1758
1759 pl011_disable_uart(uap);
1da177e4
LT
1760
1761 /*
1762 * Shut down the clock producer
1763 */
1c4c4394 1764 clk_disable_unprepare(uap->clk);
78d80c5a 1765 /* Optionally let pins go into sleep states */
2b996fc5 1766 pinctrl_pm_select_sleep_state(port->dev);
c16d51a3 1767
574de559 1768 if (dev_get_platdata(uap->port.dev)) {
c16d51a3
SKS
1769 struct amba_pl011_data *plat;
1770
574de559 1771 plat = dev_get_platdata(uap->port.dev);
c16d51a3
SKS
1772 if (plat->exit)
1773 plat->exit();
1774 }
1775
36f339d1
PH
1776 if (uap->port.ops->flush_buffer)
1777 uap->port.ops->flush_buffer(port);
1da177e4
LT
1778}
1779
0dd1e247
AP
1780static void sbsa_uart_shutdown(struct uart_port *port)
1781{
1782 struct uart_amba_port *uap =
1783 container_of(port, struct uart_amba_port, port);
1784
1785 pl011_disable_interrupts(uap);
1786
1787 free_irq(uap->port.irq, uap);
1788
1789 if (uap->port.ops->flush_buffer)
1790 uap->port.ops->flush_buffer(port);
1791}
1792
ef5a9358
AP
1793static void
1794pl011_setup_status_masks(struct uart_port *port, struct ktermios *termios)
1795{
1796 port->read_status_mask = UART011_DR_OE | 255;
1797 if (termios->c_iflag & INPCK)
1798 port->read_status_mask |= UART011_DR_FE | UART011_DR_PE;
1799 if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
1800 port->read_status_mask |= UART011_DR_BE;
1801
1802 /*
1803 * Characters to ignore
1804 */
1805 port->ignore_status_mask = 0;
1806 if (termios->c_iflag & IGNPAR)
1807 port->ignore_status_mask |= UART011_DR_FE | UART011_DR_PE;
1808 if (termios->c_iflag & IGNBRK) {
1809 port->ignore_status_mask |= UART011_DR_BE;
1810 /*
1811 * If we're ignoring parity and break indicators,
1812 * ignore overruns too (for real raw support).
1813 */
1814 if (termios->c_iflag & IGNPAR)
1815 port->ignore_status_mask |= UART011_DR_OE;
1816 }
1817
1818 /*
1819 * Ignore all characters if CREAD is not set.
1820 */
1821 if ((termios->c_cflag & CREAD) == 0)
1822 port->ignore_status_mask |= UART_DUMMY_DR_RX;
1823}
1824
1da177e4 1825static void
606d099c
AC
1826pl011_set_termios(struct uart_port *port, struct ktermios *termios,
1827 struct ktermios *old)
1da177e4 1828{
a5820c24
DT
1829 struct uart_amba_port *uap =
1830 container_of(port, struct uart_amba_port, port);
1da177e4
LT
1831 unsigned int lcr_h, old_cr;
1832 unsigned long flags;
c19f12b5
RK
1833 unsigned int baud, quot, clkdiv;
1834
1835 if (uap->vendor->oversampling)
1836 clkdiv = 8;
1837 else
1838 clkdiv = 16;
1da177e4
LT
1839
1840 /*
1841 * Ask the core to calculate the divisor for us.
1842 */
ac3e3fb4 1843 baud = uart_get_baud_rate(port, termios, old, 0,
c19f12b5 1844 port->uartclk / clkdiv);
89fa28db 1845#ifdef CONFIG_DMA_ENGINE
cb06ff10
CM
1846 /*
1847 * Adjust RX DMA polling rate with baud rate if not specified.
1848 */
1849 if (uap->dmarx.auto_poll_rate)
1850 uap->dmarx.poll_rate = DIV_ROUND_UP(10000000, baud);
89fa28db 1851#endif
ac3e3fb4
LW
1852
1853 if (baud > port->uartclk/16)
1854 quot = DIV_ROUND_CLOSEST(port->uartclk * 8, baud);
1855 else
1856 quot = DIV_ROUND_CLOSEST(port->uartclk * 4, baud);
1da177e4
LT
1857
1858 switch (termios->c_cflag & CSIZE) {
1859 case CS5:
1860 lcr_h = UART01x_LCRH_WLEN_5;
1861 break;
1862 case CS6:
1863 lcr_h = UART01x_LCRH_WLEN_6;
1864 break;
1865 case CS7:
1866 lcr_h = UART01x_LCRH_WLEN_7;
1867 break;
1868 default: // CS8
1869 lcr_h = UART01x_LCRH_WLEN_8;
1870 break;
1871 }
1872 if (termios->c_cflag & CSTOPB)
1873 lcr_h |= UART01x_LCRH_STP2;
1874 if (termios->c_cflag & PARENB) {
1875 lcr_h |= UART01x_LCRH_PEN;
1876 if (!(termios->c_cflag & PARODD))
1877 lcr_h |= UART01x_LCRH_EPS;
1878 }
ffca2b11 1879 if (uap->fifosize > 1)
1da177e4
LT
1880 lcr_h |= UART01x_LCRH_FEN;
1881
1882 spin_lock_irqsave(&port->lock, flags);
1883
1884 /*
1885 * Update the per-port timeout.
1886 */
1887 uart_update_timeout(port, termios->c_cflag, baud);
1888
ef5a9358 1889 pl011_setup_status_masks(port, termios);
1da177e4
LT
1890
1891 if (UART_ENABLE_MS(port, termios->c_cflag))
1892 pl011_enable_ms(port);
1893
1894 /* first, disable everything */
9f25bc51
RK
1895 old_cr = pl011_read(uap, REG_CR);
1896 pl011_write(0, uap, REG_CR);
1da177e4 1897
3b43816f
RV
1898 if (termios->c_cflag & CRTSCTS) {
1899 if (old_cr & UART011_CR_RTS)
1900 old_cr |= UART011_CR_RTSEN;
1901
1902 old_cr |= UART011_CR_CTSEN;
1903 uap->autorts = true;
1904 } else {
1905 old_cr &= ~(UART011_CR_CTSEN | UART011_CR_RTSEN);
1906 uap->autorts = false;
1907 }
1908
c19f12b5
RK
1909 if (uap->vendor->oversampling) {
1910 if (baud > port->uartclk / 16)
ac3e3fb4
LW
1911 old_cr |= ST_UART011_CR_OVSFACT;
1912 else
1913 old_cr &= ~ST_UART011_CR_OVSFACT;
1914 }
1915
c5dd553b
LW
1916 /*
1917 * Workaround for the ST Micro oversampling variants to
1918 * increase the bitrate slightly, by lowering the divisor,
1919 * to avoid delayed sampling of start bit at high speeds,
1920 * else we see data corruption.
1921 */
1922 if (uap->vendor->oversampling) {
1923 if ((baud >= 3000000) && (baud < 3250000) && (quot > 1))
1924 quot -= 1;
1925 else if ((baud > 3250000) && (quot > 2))
1926 quot -= 2;
1927 }
1da177e4 1928 /* Set baud rate */
9f25bc51
RK
1929 pl011_write(quot & 0x3f, uap, REG_FBRD);
1930 pl011_write(quot >> 6, uap, REG_IBRD);
1da177e4
LT
1931
1932 /*
1933 * ----------v----------v----------v----------v-----
c5dd553b 1934 * NOTE: lcrh_tx and lcrh_rx MUST BE WRITTEN AFTER
9f25bc51 1935 * REG_FBRD & REG_IBRD.
1da177e4
LT
1936 * ----------^----------^----------^----------^-----
1937 */
b60f2f66 1938 pl011_write_lcr_h(uap, lcr_h);
9f25bc51 1939 pl011_write(old_cr, uap, REG_CR);
1da177e4
LT
1940
1941 spin_unlock_irqrestore(&port->lock, flags);
1942}
1943
0dd1e247
AP
1944static void
1945sbsa_uart_set_termios(struct uart_port *port, struct ktermios *termios,
1946 struct ktermios *old)
1947{
1948 struct uart_amba_port *uap =
1949 container_of(port, struct uart_amba_port, port);
1950 unsigned long flags;
1951
1952 tty_termios_encode_baud_rate(termios, uap->fixed_baud, uap->fixed_baud);
1953
1954 /* The SBSA UART only supports 8n1 without hardware flow control. */
1955 termios->c_cflag &= ~(CSIZE | CSTOPB | PARENB | PARODD);
1956 termios->c_cflag &= ~(CMSPAR | CRTSCTS);
1957 termios->c_cflag |= CS8 | CLOCAL;
1958
1959 spin_lock_irqsave(&port->lock, flags);
1960 uart_update_timeout(port, CS8, uap->fixed_baud);
1961 pl011_setup_status_masks(port, termios);
1962 spin_unlock_irqrestore(&port->lock, flags);
1963}
1964
1da177e4
LT
1965static const char *pl011_type(struct uart_port *port)
1966{
a5820c24
DT
1967 struct uart_amba_port *uap =
1968 container_of(port, struct uart_amba_port, port);
e8a7ba86 1969 return uap->port.type == PORT_AMBA ? uap->type : NULL;
1da177e4
LT
1970}
1971
1972/*
1973 * Release the memory region(s) being used by 'port'
1974 */
e643f87f 1975static void pl011_release_port(struct uart_port *port)
1da177e4
LT
1976{
1977 release_mem_region(port->mapbase, SZ_4K);
1978}
1979
1980/*
1981 * Request the memory region(s) being used by 'port'
1982 */
e643f87f 1983static int pl011_request_port(struct uart_port *port)
1da177e4
LT
1984{
1985 return request_mem_region(port->mapbase, SZ_4K, "uart-pl011")
1986 != NULL ? 0 : -EBUSY;
1987}
1988
1989/*
1990 * Configure/autoconfigure the port.
1991 */
e643f87f 1992static void pl011_config_port(struct uart_port *port, int flags)
1da177e4
LT
1993{
1994 if (flags & UART_CONFIG_TYPE) {
1995 port->type = PORT_AMBA;
e643f87f 1996 pl011_request_port(port);
1da177e4
LT
1997 }
1998}
1999
2000/*
2001 * verify the new serial_struct (for TIOCSSERIAL).
2002 */
e643f87f 2003static int pl011_verify_port(struct uart_port *port, struct serial_struct *ser)
1da177e4
LT
2004{
2005 int ret = 0;
2006 if (ser->type != PORT_UNKNOWN && ser->type != PORT_AMBA)
2007 ret = -EINVAL;
a62c4133 2008 if (ser->irq < 0 || ser->irq >= nr_irqs)
1da177e4
LT
2009 ret = -EINVAL;
2010 if (ser->baud_base < 9600)
2011 ret = -EINVAL;
2012 return ret;
2013}
2014
2015static struct uart_ops amba_pl011_pops = {
e643f87f 2016 .tx_empty = pl011_tx_empty,
1da177e4 2017 .set_mctrl = pl011_set_mctrl,
e643f87f 2018 .get_mctrl = pl011_get_mctrl,
1da177e4
LT
2019 .stop_tx = pl011_stop_tx,
2020 .start_tx = pl011_start_tx,
2021 .stop_rx = pl011_stop_rx,
2022 .enable_ms = pl011_enable_ms,
2023 .break_ctl = pl011_break_ctl,
2024 .startup = pl011_startup,
2025 .shutdown = pl011_shutdown,
68b65f73 2026 .flush_buffer = pl011_dma_flush_buffer,
1da177e4
LT
2027 .set_termios = pl011_set_termios,
2028 .type = pl011_type,
e643f87f
LW
2029 .release_port = pl011_release_port,
2030 .request_port = pl011_request_port,
2031 .config_port = pl011_config_port,
2032 .verify_port = pl011_verify_port,
84b5ae15 2033#ifdef CONFIG_CONSOLE_POLL
b3564c2c 2034 .poll_init = pl011_hwinit,
e643f87f
LW
2035 .poll_get_char = pl011_get_poll_char,
2036 .poll_put_char = pl011_put_poll_char,
84b5ae15 2037#endif
1da177e4
LT
2038};
2039
0dd1e247
AP
2040static void sbsa_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
2041{
2042}
2043
2044static unsigned int sbsa_uart_get_mctrl(struct uart_port *port)
2045{
2046 return 0;
2047}
2048
2049static const struct uart_ops sbsa_uart_pops = {
2050 .tx_empty = pl011_tx_empty,
2051 .set_mctrl = sbsa_uart_set_mctrl,
2052 .get_mctrl = sbsa_uart_get_mctrl,
2053 .stop_tx = pl011_stop_tx,
2054 .start_tx = pl011_start_tx,
2055 .stop_rx = pl011_stop_rx,
2056 .startup = sbsa_uart_startup,
2057 .shutdown = sbsa_uart_shutdown,
2058 .set_termios = sbsa_uart_set_termios,
2059 .type = pl011_type,
2060 .release_port = pl011_release_port,
2061 .request_port = pl011_request_port,
2062 .config_port = pl011_config_port,
2063 .verify_port = pl011_verify_port,
2064#ifdef CONFIG_CONSOLE_POLL
2065 .poll_init = pl011_hwinit,
2066 .poll_get_char = pl011_get_poll_char,
2067 .poll_put_char = pl011_put_poll_char,
2068#endif
2069};
2070
1da177e4
LT
2071static struct uart_amba_port *amba_ports[UART_NR];
2072
2073#ifdef CONFIG_SERIAL_AMBA_PL011_CONSOLE
2074
d358788f 2075static void pl011_console_putchar(struct uart_port *port, int ch)
1da177e4 2076{
a5820c24
DT
2077 struct uart_amba_port *uap =
2078 container_of(port, struct uart_amba_port, port);
1da177e4 2079
9f25bc51 2080 while (pl011_read(uap, REG_FR) & UART01x_FR_TXFF)
d358788f 2081 barrier();
9f25bc51 2082 pl011_write(ch, uap, REG_DR);
1da177e4
LT
2083}
2084
2085static void
2086pl011_console_write(struct console *co, const char *s, unsigned int count)
2087{
2088 struct uart_amba_port *uap = amba_ports[co->index];
71eec483 2089 unsigned int status, old_cr = 0, new_cr;
ef605fdb
RV
2090 unsigned long flags;
2091 int locked = 1;
1da177e4
LT
2092
2093 clk_enable(uap->clk);
2094
ef605fdb
RV
2095 local_irq_save(flags);
2096 if (uap->port.sysrq)
2097 locked = 0;
2098 else if (oops_in_progress)
2099 locked = spin_trylock(&uap->port.lock);
2100 else
2101 spin_lock(&uap->port.lock);
2102
1da177e4
LT
2103 /*
2104 * First save the CR then disable the interrupts
2105 */
71eec483 2106 if (!uap->vendor->always_enabled) {
9f25bc51 2107 old_cr = pl011_read(uap, REG_CR);
71eec483
AP
2108 new_cr = old_cr & ~UART011_CR_CTSEN;
2109 new_cr |= UART01x_CR_UARTEN | UART011_CR_TXE;
9f25bc51 2110 pl011_write(new_cr, uap, REG_CR);
71eec483 2111 }
1da177e4 2112
d358788f 2113 uart_console_write(&uap->port, s, count, pl011_console_putchar);
1da177e4
LT
2114
2115 /*
2116 * Finally, wait for transmitter to become empty
2117 * and restore the TCR
2118 */
2119 do {
9f25bc51 2120 status = pl011_read(uap, REG_FR);
062a68a5 2121 } while (status & UART01x_FR_BUSY);
71eec483 2122 if (!uap->vendor->always_enabled)
9f25bc51 2123 pl011_write(old_cr, uap, REG_CR);
1da177e4 2124
ef605fdb
RV
2125 if (locked)
2126 spin_unlock(&uap->port.lock);
2127 local_irq_restore(flags);
2128
1da177e4
LT
2129 clk_disable(uap->clk);
2130}
2131
2132static void __init
2133pl011_console_get_options(struct uart_amba_port *uap, int *baud,
2134 int *parity, int *bits)
2135{
9f25bc51 2136 if (pl011_read(uap, REG_CR) & UART01x_CR_UARTEN) {
1da177e4
LT
2137 unsigned int lcr_h, ibrd, fbrd;
2138
b2a4e24c 2139 lcr_h = pl011_read(uap, uap->lcrh_tx);
1da177e4
LT
2140
2141 *parity = 'n';
2142 if (lcr_h & UART01x_LCRH_PEN) {
2143 if (lcr_h & UART01x_LCRH_EPS)
2144 *parity = 'e';
2145 else
2146 *parity = 'o';
2147 }
2148
2149 if ((lcr_h & 0x60) == UART01x_LCRH_WLEN_7)
2150 *bits = 7;
2151 else
2152 *bits = 8;
2153
9f25bc51
RK
2154 ibrd = pl011_read(uap, REG_IBRD);
2155 fbrd = pl011_read(uap, REG_FBRD);
1da177e4
LT
2156
2157 *baud = uap->port.uartclk * 4 / (64 * ibrd + fbrd);
ac3e3fb4 2158
c19f12b5 2159 if (uap->vendor->oversampling) {
9f25bc51 2160 if (pl011_read(uap, REG_CR)
ac3e3fb4
LW
2161 & ST_UART011_CR_OVSFACT)
2162 *baud *= 2;
2163 }
1da177e4
LT
2164 }
2165}
2166
2167static int __init pl011_console_setup(struct console *co, char *options)
2168{
2169 struct uart_amba_port *uap;
2170 int baud = 38400;
2171 int bits = 8;
2172 int parity = 'n';
2173 int flow = 'n';
4b4851c6 2174 int ret;
1da177e4
LT
2175
2176 /*
2177 * Check whether an invalid uart number has been specified, and
2178 * if so, search for the first available port that does have
2179 * console support.
2180 */
2181 if (co->index >= UART_NR)
2182 co->index = 0;
2183 uap = amba_ports[co->index];
d28122a5
RK
2184 if (!uap)
2185 return -ENODEV;
1da177e4 2186
78d80c5a 2187 /* Allow pins to be muxed in and configured */
2b996fc5 2188 pinctrl_pm_select_default_state(uap->port.dev);
78d80c5a 2189
4b4851c6
RK
2190 ret = clk_prepare(uap->clk);
2191 if (ret)
2192 return ret;
2193
574de559 2194 if (dev_get_platdata(uap->port.dev)) {
c16d51a3
SKS
2195 struct amba_pl011_data *plat;
2196
574de559 2197 plat = dev_get_platdata(uap->port.dev);
c16d51a3
SKS
2198 if (plat->init)
2199 plat->init();
2200 }
2201
1da177e4
LT
2202 uap->port.uartclk = clk_get_rate(uap->clk);
2203
cefc2d1d
AP
2204 if (uap->vendor->fixed_options) {
2205 baud = uap->fixed_baud;
2206 } else {
2207 if (options)
2208 uart_parse_options(options,
2209 &baud, &parity, &bits, &flow);
2210 else
2211 pl011_console_get_options(uap, &baud, &parity, &bits);
2212 }
1da177e4
LT
2213
2214 return uart_set_options(&uap->port, co, baud, parity, bits, flow);
2215}
2216
2d93486c 2217static struct uart_driver amba_reg;
1da177e4
LT
2218static struct console amba_console = {
2219 .name = "ttyAMA",
2220 .write = pl011_console_write,
2221 .device = uart_console_device,
2222 .setup = pl011_console_setup,
2223 .flags = CON_PRINTBUFFER,
2224 .index = -1,
2225 .data = &amba_reg,
2226};
2227
2228#define AMBA_CONSOLE (&amba_console)
0d3c673e
RH
2229
2230static void pl011_putc(struct uart_port *port, int c)
2231{
9f25bc51 2232 while (readl(port->membase + REG_FR) & UART01x_FR_TXFF)
0d3c673e 2233 ;
9f25bc51
RK
2234 writeb(c, port->membase + REG_DR);
2235 while (readl(port->membase + REG_FR) & UART01x_FR_BUSY)
0d3c673e
RH
2236 ;
2237}
2238
2239static void pl011_early_write(struct console *con, const char *s, unsigned n)
2240{
2241 struct earlycon_device *dev = con->data;
2242
2243 uart_console_write(&dev->port, s, n, pl011_putc);
2244}
2245
2246static int __init pl011_early_console_setup(struct earlycon_device *device,
2247 const char *opt)
2248{
2249 if (!device->port.membase)
2250 return -ENODEV;
2251
2252 device->con->write = pl011_early_write;
2253 return 0;
2254}
2255EARLYCON_DECLARE(pl011, pl011_early_console_setup);
45e0f0f5 2256OF_EARLYCON_DECLARE(pl011, "arm,pl011", pl011_early_console_setup);
0d3c673e 2257
1da177e4
LT
2258#else
2259#define AMBA_CONSOLE NULL
2260#endif
2261
2262static struct uart_driver amba_reg = {
2263 .owner = THIS_MODULE,
2264 .driver_name = "ttyAMA",
2265 .dev_name = "ttyAMA",
2266 .major = SERIAL_AMBA_MAJOR,
2267 .minor = SERIAL_AMBA_MINOR,
2268 .nr = UART_NR,
2269 .cons = AMBA_CONSOLE,
2270};
2271
32614aad
ML
2272static int pl011_probe_dt_alias(int index, struct device *dev)
2273{
2274 struct device_node *np;
2275 static bool seen_dev_with_alias = false;
2276 static bool seen_dev_without_alias = false;
2277 int ret = index;
2278
2279 if (!IS_ENABLED(CONFIG_OF))
2280 return ret;
2281
2282 np = dev->of_node;
2283 if (!np)
2284 return ret;
2285
2286 ret = of_alias_get_id(np, "serial");
2287 if (IS_ERR_VALUE(ret)) {
2288 seen_dev_without_alias = true;
2289 ret = index;
2290 } else {
2291 seen_dev_with_alias = true;
2292 if (ret >= ARRAY_SIZE(amba_ports) || amba_ports[ret] != NULL) {
2293 dev_warn(dev, "requested serial port %d not available.\n", ret);
2294 ret = index;
2295 }
2296 }
2297
2298 if (seen_dev_with_alias && seen_dev_without_alias)
2299 dev_warn(dev, "aliased and non-aliased serial devices found in device tree. Serial port enumeration may be unpredictable.\n");
2300
2301 return ret;
2302}
2303
49bb3c86
AP
2304/* unregisters the driver also if no more ports are left */
2305static void pl011_unregister_port(struct uart_amba_port *uap)
2306{
2307 int i;
2308 bool busy = false;
2309
2310 for (i = 0; i < ARRAY_SIZE(amba_ports); i++) {
2311 if (amba_ports[i] == uap)
2312 amba_ports[i] = NULL;
2313 else if (amba_ports[i])
2314 busy = true;
2315 }
2316 pl011_dma_remove(uap);
2317 if (!busy)
2318 uart_unregister_driver(&amba_reg);
2319}
2320
3873e2d7 2321static int pl011_find_free_port(void)
1da177e4 2322{
3873e2d7 2323 int i;
1da177e4
LT
2324
2325 for (i = 0; i < ARRAY_SIZE(amba_ports); i++)
2326 if (amba_ports[i] == NULL)
3873e2d7 2327 return i;
1da177e4 2328
3873e2d7
AP
2329 return -EBUSY;
2330}
1da177e4 2331
3873e2d7
AP
2332static int pl011_setup_port(struct device *dev, struct uart_amba_port *uap,
2333 struct resource *mmiobase, int index)
2334{
2335 void __iomem *base;
32614aad 2336
3873e2d7 2337 base = devm_ioremap_resource(dev, mmiobase);
97a60eac
KK
2338 if (IS_ERR(base))
2339 return PTR_ERR(base);
1da177e4 2340
3873e2d7 2341 index = pl011_probe_dt_alias(index, dev);
1da177e4 2342
d8d8ffa4 2343 uap->old_cr = 0;
3873e2d7
AP
2344 uap->port.dev = dev;
2345 uap->port.mapbase = mmiobase->start;
1da177e4
LT
2346 uap->port.membase = base;
2347 uap->port.iotype = UPIO_MEM;
ffca2b11 2348 uap->port.fifosize = uap->fifosize;
1da177e4 2349 uap->port.flags = UPF_BOOT_AUTOCONF;
3873e2d7 2350 uap->port.line = index;
1da177e4 2351
3873e2d7 2352 amba_ports[index] = uap;
c3d8b76f 2353
3873e2d7
AP
2354 return 0;
2355}
e8a7ba86 2356
3873e2d7
AP
2357static int pl011_register_port(struct uart_amba_port *uap)
2358{
2359 int ret;
1da177e4 2360
3873e2d7 2361 /* Ensure interrupts from this UART are masked and cleared */
9f25bc51
RK
2362 pl011_write(0, uap, REG_IMSC);
2363 pl011_write(0xffff, uap, REG_ICR);
ef2889f7
TB
2364
2365 if (!amba_reg.state) {
2366 ret = uart_register_driver(&amba_reg);
2367 if (ret < 0) {
3873e2d7 2368 dev_err(uap->port.dev,
1c9be310 2369 "Failed to register AMBA-PL011 driver\n");
ef2889f7
TB
2370 return ret;
2371 }
2372 }
2373
1da177e4 2374 ret = uart_add_one_port(&amba_reg, &uap->port);
49bb3c86
AP
2375 if (ret)
2376 pl011_unregister_port(uap);
7f6d942a 2377
1da177e4
LT
2378 return ret;
2379}
2380
3873e2d7
AP
2381static int pl011_probe(struct amba_device *dev, const struct amba_id *id)
2382{
2383 struct uart_amba_port *uap;
2384 struct vendor_data *vendor = id->data;
2385 int portnr, ret;
2386
2387 portnr = pl011_find_free_port();
2388 if (portnr < 0)
2389 return portnr;
2390
2391 uap = devm_kzalloc(&dev->dev, sizeof(struct uart_amba_port),
2392 GFP_KERNEL);
2393 if (!uap)
2394 return -ENOMEM;
2395
2396 uap->clk = devm_clk_get(&dev->dev, NULL);
2397 if (IS_ERR(uap->clk))
2398 return PTR_ERR(uap->clk);
2399
2400 uap->vendor = vendor;
2401 uap->lcrh_rx = vendor->lcrh_rx;
2402 uap->lcrh_tx = vendor->lcrh_tx;
2403 uap->fifosize = vendor->get_fifosize(dev);
2404 uap->port.irq = dev->irq[0];
2405 uap->port.ops = &amba_pl011_pops;
2406
2407 snprintf(uap->type, sizeof(uap->type), "PL011 rev%u", amba_rev(dev));
2408
2409 ret = pl011_setup_port(&dev->dev, uap, &dev->res, portnr);
2410 if (ret)
2411 return ret;
2412
2413 amba_set_drvdata(dev, uap);
2414
2415 return pl011_register_port(uap);
2416}
2417
1da177e4
LT
2418static int pl011_remove(struct amba_device *dev)
2419{
2420 struct uart_amba_port *uap = amba_get_drvdata(dev);
1da177e4 2421
1da177e4 2422 uart_remove_one_port(&amba_reg, &uap->port);
49bb3c86 2423 pl011_unregister_port(uap);
1da177e4
LT
2424 return 0;
2425}
2426
d0ce850d
UH
2427#ifdef CONFIG_PM_SLEEP
2428static int pl011_suspend(struct device *dev)
b736b89f 2429{
d0ce850d 2430 struct uart_amba_port *uap = dev_get_drvdata(dev);
b736b89f
LC
2431
2432 if (!uap)
2433 return -EINVAL;
2434
2435 return uart_suspend_port(&amba_reg, &uap->port);
2436}
2437
d0ce850d 2438static int pl011_resume(struct device *dev)
b736b89f 2439{
d0ce850d 2440 struct uart_amba_port *uap = dev_get_drvdata(dev);
b736b89f
LC
2441
2442 if (!uap)
2443 return -EINVAL;
2444
2445 return uart_resume_port(&amba_reg, &uap->port);
2446}
2447#endif
2448
d0ce850d
UH
2449static SIMPLE_DEV_PM_OPS(pl011_dev_pm_ops, pl011_suspend, pl011_resume);
2450
0dd1e247
AP
2451static int sbsa_uart_probe(struct platform_device *pdev)
2452{
2453 struct uart_amba_port *uap;
2454 struct resource *r;
2455 int portnr, ret;
2456 int baudrate;
2457
2458 /*
2459 * Check the mandatory baud rate parameter in the DT node early
2460 * so that we can easily exit with the error.
2461 */
2462 if (pdev->dev.of_node) {
2463 struct device_node *np = pdev->dev.of_node;
2464
2465 ret = of_property_read_u32(np, "current-speed", &baudrate);
2466 if (ret)
2467 return ret;
2468 } else {
2469 baudrate = 115200;
2470 }
2471
2472 portnr = pl011_find_free_port();
2473 if (portnr < 0)
2474 return portnr;
2475
2476 uap = devm_kzalloc(&pdev->dev, sizeof(struct uart_amba_port),
2477 GFP_KERNEL);
2478 if (!uap)
2479 return -ENOMEM;
2480
2481 uap->vendor = &vendor_sbsa;
2482 uap->fifosize = 32;
2483 uap->port.irq = platform_get_irq(pdev, 0);
2484 uap->port.ops = &sbsa_uart_pops;
2485 uap->fixed_baud = baudrate;
2486
2487 snprintf(uap->type, sizeof(uap->type), "SBSA");
2488
2489 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2490
2491 ret = pl011_setup_port(&pdev->dev, uap, r, portnr);
2492 if (ret)
2493 return ret;
2494
2495 platform_set_drvdata(pdev, uap);
2496
2497 return pl011_register_port(uap);
2498}
2499
2500static int sbsa_uart_remove(struct platform_device *pdev)
2501{
2502 struct uart_amba_port *uap = platform_get_drvdata(pdev);
2503
2504 uart_remove_one_port(&amba_reg, &uap->port);
2505 pl011_unregister_port(uap);
2506 return 0;
2507}
2508
2509static const struct of_device_id sbsa_uart_of_match[] = {
2510 { .compatible = "arm,sbsa-uart", },
2511 {},
2512};
2513MODULE_DEVICE_TABLE(of, sbsa_uart_of_match);
2514
3db9ab0b
GG
2515static const struct acpi_device_id sbsa_uart_acpi_match[] = {
2516 { "ARMH0011", 0 },
2517 {},
2518};
2519MODULE_DEVICE_TABLE(acpi, sbsa_uart_acpi_match);
2520
0dd1e247
AP
2521static struct platform_driver arm_sbsa_uart_platform_driver = {
2522 .probe = sbsa_uart_probe,
2523 .remove = sbsa_uart_remove,
2524 .driver = {
2525 .name = "sbsa-uart",
2526 .of_match_table = of_match_ptr(sbsa_uart_of_match),
3db9ab0b 2527 .acpi_match_table = ACPI_PTR(sbsa_uart_acpi_match),
0dd1e247
AP
2528 },
2529};
2530
2c39c9e1 2531static struct amba_id pl011_ids[] = {
1da177e4
LT
2532 {
2533 .id = 0x00041011,
2534 .mask = 0x000fffff,
5926a295
AR
2535 .data = &vendor_arm,
2536 },
2537 {
2538 .id = 0x00380802,
2539 .mask = 0x00ffffff,
2540 .data = &vendor_st,
1da177e4
LT
2541 },
2542 { 0, 0 },
2543};
2544
60f7a33b
DM
2545MODULE_DEVICE_TABLE(amba, pl011_ids);
2546
1da177e4
LT
2547static struct amba_driver pl011_driver = {
2548 .drv = {
2549 .name = "uart-pl011",
d0ce850d 2550 .pm = &pl011_dev_pm_ops,
1da177e4
LT
2551 },
2552 .id_table = pl011_ids,
2553 .probe = pl011_probe,
2554 .remove = pl011_remove,
2555};
2556
2557static int __init pl011_init(void)
2558{
1da177e4
LT
2559 printk(KERN_INFO "Serial: AMBA PL011 UART driver\n");
2560
0dd1e247
AP
2561 if (platform_driver_register(&arm_sbsa_uart_platform_driver))
2562 pr_warn("could not register SBSA UART platform driver\n");
062a68a5 2563 return amba_driver_register(&pl011_driver);
1da177e4
LT
2564}
2565
2566static void __exit pl011_exit(void)
2567{
0dd1e247 2568 platform_driver_unregister(&arm_sbsa_uart_platform_driver);
1da177e4 2569 amba_driver_unregister(&pl011_driver);
1da177e4
LT
2570}
2571
4dd9e742
AR
2572/*
2573 * While this can be a module, if builtin it's most likely the console
2574 * So let's leave module_exit but move module_init to an earlier place
2575 */
2576arch_initcall(pl011_init);
1da177e4
LT
2577module_exit(pl011_exit);
2578
2579MODULE_AUTHOR("ARM Ltd/Deep Blue Solutions Ltd");
2580MODULE_DESCRIPTION("ARM AMBA serial port driver");
2581MODULE_LICENSE("GPL");
This page took 0.958681 seconds and 5 git commands to generate.