blob: 4c122078d62bef6033e2729b34d5c9adbdeb4a3c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Driver for AMBA serial ports
3 *
4 * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
5 *
6 * Copyright 1999 ARM Limited
7 * Copyright (C) 2000 Deep Blue Solutions Ltd.
Russell King68b65f72010-12-22 17:24:39 +00008 * Copyright (C) 2010 ST-Ericsson SA
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 * This is a generic driver for ARM AMBA-type serial ports. They
25 * have a lot of 16550-like features, but are not register compatible.
26 * Note that although they do have CTS, DCD and DSR inputs, they do
27 * not have an RI input, nor do they have DTR or RTS outputs. If
28 * required, these have to be supplied via some other means (eg, GPIO)
29 * and hooked into this driver.
30 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Chanho Mincb06ff12013-03-27 18:38:11 +090032
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#if defined(CONFIG_SERIAL_AMBA_PL011_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
34#define SUPPORT_SYSRQ
35#endif
36
37#include <linux/module.h>
38#include <linux/ioport.h>
39#include <linux/init.h>
40#include <linux/console.h>
41#include <linux/sysrq.h>
42#include <linux/device.h>
43#include <linux/tty.h>
44#include <linux/tty_flip.h>
45#include <linux/serial_core.h>
46#include <linux/serial.h>
Russell Kinga62c80e2006-01-07 13:52:45 +000047#include <linux/amba/bus.h>
48#include <linux/amba/serial.h>
Russell Kingf8ce2542006-01-07 16:15:52 +000049#include <linux/clk.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090050#include <linux/slab.h>
Russell King68b65f72010-12-22 17:24:39 +000051#include <linux/dmaengine.h>
52#include <linux/dma-mapping.h>
53#include <linux/scatterlist.h>
Shreshtha Kumar Sahuc16d51a2011-06-13 10:11:33 +020054#include <linux/delay.h>
Viresh Kumar258aea72012-02-01 16:12:19 +053055#include <linux/types.h>
Matthew Leach32614aa2012-08-28 16:41:28 +010056#include <linux/of.h>
57#include <linux/of_device.h>
Shawn Guo258e0552012-05-06 22:53:35 +080058#include <linux/pinctrl/consumer.h>
Alessandro Rubinicb707062012-06-24 12:46:37 +010059#include <linux/sizes.h>
Linus Walleijde609582012-10-15 13:36:01 +020060#include <linux/io.h>
Graeme Gregory3db9ab02015-05-21 17:26:24 +010061#include <linux/acpi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Russell King9f25bc52015-11-03 14:51:13 +000063#include "amba-pl011.h"
64
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#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
Russell Kingb63d4f02005-11-19 11:10:35 +000073#define UART_DR_ERROR (UART011_DR_OE|UART011_DR_BE|UART011_DR_PE|UART011_DR_FE)
74#define UART_DUMMY_DR_RX (1 << 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
Russell Kingdebb7f62015-11-16 17:40:26 +000076static u16 pl011_std_offsets[REG_ARRAY_SIZE] = {
77 [REG_DR] = UART01x_DR,
78 [REG_ST_DMAWM] = ST_UART011_DMAWM,
79 [REG_ST_TIMEOUT] = ST_UART011_TIMEOUT,
80 [REG_FR] = UART01x_FR,
81 [REG_ST_LCRH_RX] = ST_UART011_LCRH_RX,
82 [REG_IBRD] = UART011_IBRD,
83 [REG_FBRD] = UART011_FBRD,
84 [REG_LCRH] = UART011_LCRH,
85 [REG_ST_LCRH_TX] = ST_UART011_LCRH_TX,
86 [REG_CR] = UART011_CR,
87 [REG_IFLS] = UART011_IFLS,
88 [REG_IMSC] = UART011_IMSC,
89 [REG_RIS] = UART011_RIS,
90 [REG_MIS] = UART011_MIS,
91 [REG_ICR] = UART011_ICR,
92 [REG_DMACR] = UART011_DMACR,
93 [REG_ST_XFCR] = ST_UART011_XFCR,
94 [REG_ST_XON1] = ST_UART011_XON1,
95 [REG_ST_XON2] = ST_UART011_XON2,
96 [REG_ST_XOFF1] = ST_UART011_XOFF1,
97 [REG_ST_XOFF2] = ST_UART011_XOFF2,
98 [REG_ST_ITCR] = ST_UART011_ITCR,
99 [REG_ST_ITIP] = ST_UART011_ITIP,
100 [REG_ST_ABCR] = ST_UART011_ABCR,
101 [REG_ST_ABIMSC] = ST_UART011_ABIMSC,
102};
103
Alessandro Rubini5926a292009-06-04 17:43:04 +0100104/* There is by now at least one vendor with differing details, so handle it */
105struct vendor_data {
Russell King439403b2015-11-16 17:40:31 +0000106 const u16 *reg_offset;
Alessandro Rubini5926a292009-06-04 17:43:04 +0100107 unsigned int ifls;
Linus Walleijec489aa2010-06-02 08:13:52 +0100108 unsigned int lcrh_tx;
109 unsigned int lcrh_rx;
Linus Walleijac3e3fb2010-06-02 20:40:22 +0100110 bool oversampling;
Russell King38d62432010-12-22 17:59:16 +0000111 bool dma_threshold;
Rajanikanth H.V4fd06902012-03-26 11:17:02 +0200112 bool cts_event_workaround;
Andre Przywara71eec482015-05-21 17:26:21 +0100113 bool always_enabled;
Andre Przywaracefc2d12015-05-21 17:26:22 +0100114 bool fixed_options;
Jongsung Kim78506f22013-04-15 14:45:25 +0900115
Jongsung Kimea336402013-05-10 18:05:35 +0900116 unsigned int (*get_fifosize)(struct amba_device *dev);
Alessandro Rubini5926a292009-06-04 17:43:04 +0100117};
118
Jongsung Kimea336402013-05-10 18:05:35 +0900119static unsigned int get_fifosize_arm(struct amba_device *dev)
Jongsung Kim78506f22013-04-15 14:45:25 +0900120{
Jongsung Kimea336402013-05-10 18:05:35 +0900121 return amba_rev(dev) < 3 ? 16 : 32;
Jongsung Kim78506f22013-04-15 14:45:25 +0900122}
123
Alessandro Rubini5926a292009-06-04 17:43:04 +0100124static struct vendor_data vendor_arm = {
Russell King439403b2015-11-16 17:40:31 +0000125 .reg_offset = pl011_std_offsets,
Alessandro Rubini5926a292009-06-04 17:43:04 +0100126 .ifls = UART011_IFLS_RX4_8|UART011_IFLS_TX4_8,
Russell King9f25bc52015-11-03 14:51:13 +0000127 .lcrh_tx = REG_LCRH,
128 .lcrh_rx = REG_LCRH,
Linus Walleijac3e3fb2010-06-02 20:40:22 +0100129 .oversampling = false,
Russell King38d62432010-12-22 17:59:16 +0000130 .dma_threshold = false,
Rajanikanth H.V4fd06902012-03-26 11:17:02 +0200131 .cts_event_workaround = false,
Andre Przywara71eec482015-05-21 17:26:21 +0100132 .always_enabled = false,
Andre Przywaracefc2d12015-05-21 17:26:22 +0100133 .fixed_options = false,
Jongsung Kim78506f22013-04-15 14:45:25 +0900134 .get_fifosize = get_fifosize_arm,
Alessandro Rubini5926a292009-06-04 17:43:04 +0100135};
136
Andre Przywara0dd1e242015-05-21 17:26:23 +0100137static struct vendor_data vendor_sbsa = {
Russell King439403b2015-11-16 17:40:31 +0000138 .reg_offset = pl011_std_offsets,
Andre Przywara0dd1e242015-05-21 17:26:23 +0100139 .oversampling = false,
140 .dma_threshold = false,
141 .cts_event_workaround = false,
142 .always_enabled = true,
143 .fixed_options = true,
144};
145
Jongsung Kimea336402013-05-10 18:05:35 +0900146static unsigned int get_fifosize_st(struct amba_device *dev)
Jongsung Kim78506f22013-04-15 14:45:25 +0900147{
148 return 64;
149}
150
Alessandro Rubini5926a292009-06-04 17:43:04 +0100151static struct vendor_data vendor_st = {
Russell King439403b2015-11-16 17:40:31 +0000152 .reg_offset = pl011_std_offsets,
Alessandro Rubini5926a292009-06-04 17:43:04 +0100153 .ifls = UART011_IFLS_RX_HALF|UART011_IFLS_TX_HALF,
Russell King9f25bc52015-11-03 14:51:13 +0000154 .lcrh_tx = REG_ST_LCRH_TX,
155 .lcrh_rx = REG_ST_LCRH_RX,
Linus Walleijac3e3fb2010-06-02 20:40:22 +0100156 .oversampling = true,
Russell King38d62432010-12-22 17:59:16 +0000157 .dma_threshold = true,
Rajanikanth H.V4fd06902012-03-26 11:17:02 +0200158 .cts_event_workaround = true,
Andre Przywara71eec482015-05-21 17:26:21 +0100159 .always_enabled = false,
Andre Przywaracefc2d12015-05-21 17:26:22 +0100160 .fixed_options = false,
Jongsung Kim78506f22013-04-15 14:45:25 +0900161 .get_fifosize = get_fifosize_st,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162};
163
Russell King68b65f72010-12-22 17:24:39 +0000164/* Deals with DMA transactions */
Linus Walleijead76f32011-02-24 13:21:08 +0100165
166struct pl011_sgbuf {
167 struct scatterlist sg;
168 char *buf;
169};
170
171struct pl011_dmarx_data {
172 struct dma_chan *chan;
173 struct completion complete;
174 bool use_buf_b;
175 struct pl011_sgbuf sgbuf_a;
176 struct pl011_sgbuf sgbuf_b;
177 dma_cookie_t cookie;
178 bool running;
Chanho Mincb06ff12013-03-27 18:38:11 +0900179 struct timer_list timer;
180 unsigned int last_residue;
181 unsigned long last_jiffies;
182 bool auto_poll_rate;
183 unsigned int poll_rate;
184 unsigned int poll_timeout;
Linus Walleijead76f32011-02-24 13:21:08 +0100185};
186
Russell King68b65f72010-12-22 17:24:39 +0000187struct pl011_dmatx_data {
188 struct dma_chan *chan;
189 struct scatterlist sg;
190 char *buf;
191 bool queued;
192};
193
Russell Kingc19f12b2010-12-22 17:48:26 +0000194/*
195 * We wrap our port structure around the generic uart_port.
196 */
197struct uart_amba_port {
198 struct uart_port port;
Russell Kingdebb7f62015-11-16 17:40:26 +0000199 const u16 *reg_offset;
Russell Kingc19f12b2010-12-22 17:48:26 +0000200 struct clk *clk;
201 const struct vendor_data *vendor;
Russell King68b65f72010-12-22 17:24:39 +0000202 unsigned int dmacr; /* dma control reg */
Russell Kingc19f12b2010-12-22 17:48:26 +0000203 unsigned int im; /* interrupt mask */
204 unsigned int old_status;
Russell Kingffca2b12010-12-22 17:13:05 +0000205 unsigned int fifosize; /* vendor-specific */
Russell Kingc19f12b2010-12-22 17:48:26 +0000206 unsigned int lcrh_tx; /* vendor-specific */
207 unsigned int lcrh_rx; /* vendor-specific */
Shreshtha Kumar Sahud8d8ffa2012-01-18 15:53:59 +0530208 unsigned int old_cr; /* state during shutdown */
Russell Kingc19f12b2010-12-22 17:48:26 +0000209 bool autorts;
Andre Przywaracefc2d12015-05-21 17:26:22 +0100210 unsigned int fixed_baud; /* vendor-set fixed baud rate */
Russell Kingc19f12b2010-12-22 17:48:26 +0000211 char type[12];
Russell King68b65f72010-12-22 17:24:39 +0000212#ifdef CONFIG_DMA_ENGINE
213 /* DMA stuff */
Linus Walleijead76f32011-02-24 13:21:08 +0100214 bool using_tx_dma;
215 bool using_rx_dma;
216 struct pl011_dmarx_data dmarx;
Russell King68b65f72010-12-22 17:24:39 +0000217 struct pl011_dmatx_data dmatx;
Jorge Ramirez-Ortiz1c9be312015-03-06 13:05:40 -0500218 bool dma_probed;
Russell King68b65f72010-12-22 17:24:39 +0000219#endif
Russell Kingc19f12b2010-12-22 17:48:26 +0000220};
221
Russell King9f25bc52015-11-03 14:51:13 +0000222static unsigned int pl011_reg_to_offset(const struct uart_amba_port *uap,
223 unsigned int reg)
224{
Russell Kingdebb7f62015-11-16 17:40:26 +0000225 return uap->reg_offset[reg];
Russell King9f25bc52015-11-03 14:51:13 +0000226}
227
Russell Kingb2a4e242015-11-03 14:51:03 +0000228static unsigned int pl011_read(const struct uart_amba_port *uap,
229 unsigned int reg)
Russell King75836332015-11-03 14:50:58 +0000230{
Russell King9f25bc52015-11-03 14:51:13 +0000231 return readw(uap->port.membase + pl011_reg_to_offset(uap, reg));
Russell King75836332015-11-03 14:50:58 +0000232}
233
Russell Kingb2a4e242015-11-03 14:51:03 +0000234static void pl011_write(unsigned int val, const struct uart_amba_port *uap,
235 unsigned int reg)
Russell King75836332015-11-03 14:50:58 +0000236{
Russell King9f25bc52015-11-03 14:51:13 +0000237 writew(val, uap->port.membase + pl011_reg_to_offset(uap, reg));
Russell King75836332015-11-03 14:50:58 +0000238}
239
Russell King68b65f72010-12-22 17:24:39 +0000240/*
Linus Walleij29772c42011-02-24 13:21:36 +0100241 * Reads up to 256 characters from the FIFO or until it's empty and
242 * inserts them into the TTY layer. Returns the number of characters
243 * read from the FIFO.
244 */
245static int pl011_fifo_to_tty(struct uart_amba_port *uap)
246{
Timur Tabi71a5cd82015-10-07 15:27:16 -0500247 u16 status;
248 unsigned int ch, flag, max_count = 256;
Linus Walleij29772c42011-02-24 13:21:36 +0100249 int fifotaken = 0;
250
251 while (max_count--) {
Russell King9f25bc52015-11-03 14:51:13 +0000252 status = pl011_read(uap, REG_FR);
Linus Walleij29772c42011-02-24 13:21:36 +0100253 if (status & UART01x_FR_RXFE)
254 break;
255
256 /* Take chars from the FIFO and update status */
Russell King9f25bc52015-11-03 14:51:13 +0000257 ch = pl011_read(uap, REG_DR) | UART_DUMMY_DR_RX;
Linus Walleij29772c42011-02-24 13:21:36 +0100258 flag = TTY_NORMAL;
259 uap->port.icount.rx++;
260 fifotaken++;
261
262 if (unlikely(ch & UART_DR_ERROR)) {
263 if (ch & UART011_DR_BE) {
264 ch &= ~(UART011_DR_FE | UART011_DR_PE);
265 uap->port.icount.brk++;
266 if (uart_handle_break(&uap->port))
267 continue;
268 } else if (ch & UART011_DR_PE)
269 uap->port.icount.parity++;
270 else if (ch & UART011_DR_FE)
271 uap->port.icount.frame++;
272 if (ch & UART011_DR_OE)
273 uap->port.icount.overrun++;
274
275 ch &= uap->port.read_status_mask;
276
277 if (ch & UART011_DR_BE)
278 flag = TTY_BREAK;
279 else if (ch & UART011_DR_PE)
280 flag = TTY_PARITY;
281 else if (ch & UART011_DR_FE)
282 flag = TTY_FRAME;
283 }
284
285 if (uart_handle_sysrq_char(&uap->port, ch & 255))
286 continue;
287
288 uart_insert_char(&uap->port, ch, UART011_DR_OE, ch, flag);
289 }
290
291 return fifotaken;
292}
293
294
295/*
Russell King68b65f72010-12-22 17:24:39 +0000296 * All the DMA operation mode stuff goes inside this ifdef.
297 * This assumes that you have a generic DMA device interface,
298 * no custom DMA interfaces are supported.
299 */
300#ifdef CONFIG_DMA_ENGINE
301
302#define PL011_DMA_BUFFER_SIZE PAGE_SIZE
303
Linus Walleijead76f32011-02-24 13:21:08 +0100304static int pl011_sgbuf_init(struct dma_chan *chan, struct pl011_sgbuf *sg,
305 enum dma_data_direction dir)
306{
Chanho Mincb06ff12013-03-27 18:38:11 +0900307 dma_addr_t dma_addr;
308
309 sg->buf = dma_alloc_coherent(chan->device->dev,
310 PL011_DMA_BUFFER_SIZE, &dma_addr, GFP_KERNEL);
Linus Walleijead76f32011-02-24 13:21:08 +0100311 if (!sg->buf)
312 return -ENOMEM;
313
Chanho Mincb06ff12013-03-27 18:38:11 +0900314 sg_init_table(&sg->sg, 1);
315 sg_set_page(&sg->sg, phys_to_page(dma_addr),
316 PL011_DMA_BUFFER_SIZE, offset_in_page(dma_addr));
317 sg_dma_address(&sg->sg) = dma_addr;
Andrew Jacksonc64be922014-11-07 14:14:43 +0000318 sg_dma_len(&sg->sg) = PL011_DMA_BUFFER_SIZE;
Linus Walleijead76f32011-02-24 13:21:08 +0100319
Linus Walleijead76f32011-02-24 13:21:08 +0100320 return 0;
321}
322
323static void pl011_sgbuf_free(struct dma_chan *chan, struct pl011_sgbuf *sg,
324 enum dma_data_direction dir)
325{
326 if (sg->buf) {
Chanho Mincb06ff12013-03-27 18:38:11 +0900327 dma_free_coherent(chan->device->dev,
328 PL011_DMA_BUFFER_SIZE, sg->buf,
329 sg_dma_address(&sg->sg));
Linus Walleijead76f32011-02-24 13:21:08 +0100330 }
331}
332
Jorge Ramirez-Ortiz1c9be312015-03-06 13:05:40 -0500333static void pl011_dma_probe(struct uart_amba_port *uap)
Russell King68b65f72010-12-22 17:24:39 +0000334{
335 /* DMA is the sole user of the platform data right now */
Jingoo Han574de552013-07-30 17:06:57 +0900336 struct amba_pl011_data *plat = dev_get_platdata(uap->port.dev);
Jorge Ramirez-Ortiz1c9be312015-03-06 13:05:40 -0500337 struct device *dev = uap->port.dev;
Russell King68b65f72010-12-22 17:24:39 +0000338 struct dma_slave_config tx_conf = {
Russell King9f25bc52015-11-03 14:51:13 +0000339 .dst_addr = uap->port.mapbase +
340 pl011_reg_to_offset(uap, REG_DR),
Russell King68b65f72010-12-22 17:24:39 +0000341 .dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE,
Vinod Koula485df42011-10-14 10:47:38 +0530342 .direction = DMA_MEM_TO_DEV,
Russell King68b65f72010-12-22 17:24:39 +0000343 .dst_maxburst = uap->fifosize >> 1,
Viresh Kumar258aea72012-02-01 16:12:19 +0530344 .device_fc = false,
Russell King68b65f72010-12-22 17:24:39 +0000345 };
346 struct dma_chan *chan;
347 dma_cap_mask_t mask;
348
Jorge Ramirez-Ortiz1c9be312015-03-06 13:05:40 -0500349 uap->dma_probed = true;
350 chan = dma_request_slave_channel_reason(dev, "tx");
351 if (IS_ERR(chan)) {
352 if (PTR_ERR(chan) == -EPROBE_DEFER) {
Jorge Ramirez-Ortiz1c9be312015-03-06 13:05:40 -0500353 uap->dma_probed = false;
354 return;
355 }
Russell King68b65f72010-12-22 17:24:39 +0000356
Arnd Bergmann787b0c12013-01-28 16:24:37 +0000357 /* We need platform data */
358 if (!plat || !plat->dma_filter) {
359 dev_info(uap->port.dev, "no DMA platform data\n");
360 return;
361 }
362
363 /* Try to acquire a generic DMA engine slave TX channel */
364 dma_cap_zero(mask);
365 dma_cap_set(DMA_SLAVE, mask);
366
367 chan = dma_request_channel(mask, plat->dma_filter,
368 plat->dma_tx_param);
369 if (!chan) {
370 dev_err(uap->port.dev, "no TX DMA channel!\n");
371 return;
372 }
Russell King68b65f72010-12-22 17:24:39 +0000373 }
374
375 dmaengine_slave_config(chan, &tx_conf);
376 uap->dmatx.chan = chan;
377
378 dev_info(uap->port.dev, "DMA channel TX %s\n",
379 dma_chan_name(uap->dmatx.chan));
Linus Walleijead76f32011-02-24 13:21:08 +0100380
381 /* Optionally make use of an RX channel as well */
Arnd Bergmann787b0c12013-01-28 16:24:37 +0000382 chan = dma_request_slave_channel(dev, "rx");
Rob Herring0d3c6732014-04-18 17:19:57 -0500383
Arnd Bergmann787b0c12013-01-28 16:24:37 +0000384 if (!chan && plat->dma_rx_param) {
385 chan = dma_request_channel(mask, plat->dma_filter, plat->dma_rx_param);
386
387 if (!chan) {
388 dev_err(uap->port.dev, "no RX DMA channel!\n");
389 return;
390 }
391 }
392
393 if (chan) {
Linus Walleijead76f32011-02-24 13:21:08 +0100394 struct dma_slave_config rx_conf = {
Russell King9f25bc52015-11-03 14:51:13 +0000395 .src_addr = uap->port.mapbase +
396 pl011_reg_to_offset(uap, REG_DR),
Linus Walleijead76f32011-02-24 13:21:08 +0100397 .src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE,
Vinod Koula485df42011-10-14 10:47:38 +0530398 .direction = DMA_DEV_TO_MEM,
Guennadi Liakhovetskib2aeb772014-04-12 19:47:17 +0200399 .src_maxburst = uap->fifosize >> 2,
Viresh Kumar258aea72012-02-01 16:12:19 +0530400 .device_fc = false,
Linus Walleijead76f32011-02-24 13:21:08 +0100401 };
Andrew Jackson2d3b7d62014-11-07 14:14:47 +0000402 struct dma_slave_caps caps;
Linus Walleijead76f32011-02-24 13:21:08 +0100403
Andrew Jackson2d3b7d62014-11-07 14:14:47 +0000404 /*
405 * Some DMA controllers provide information on their capabilities.
406 * If the controller does, check for suitable residue processing
407 * otherwise assime all is well.
408 */
409 if (0 == dma_get_slave_caps(chan, &caps)) {
410 if (caps.residue_granularity ==
411 DMA_RESIDUE_GRANULARITY_DESCRIPTOR) {
412 dma_release_channel(chan);
413 dev_info(uap->port.dev,
414 "RX DMA disabled - no residue processing\n");
415 return;
416 }
417 }
Linus Walleijead76f32011-02-24 13:21:08 +0100418 dmaengine_slave_config(chan, &rx_conf);
419 uap->dmarx.chan = chan;
420
Andrew Jackson98267d32014-11-07 14:14:23 +0000421 uap->dmarx.auto_poll_rate = false;
Greg Kroah-Hartman8f898bf2013-12-17 09:33:18 -0800422 if (plat && plat->dma_rx_poll_enable) {
Chanho Mincb06ff12013-03-27 18:38:11 +0900423 /* Set poll rate if specified. */
424 if (plat->dma_rx_poll_rate) {
425 uap->dmarx.auto_poll_rate = false;
426 uap->dmarx.poll_rate = plat->dma_rx_poll_rate;
427 } else {
428 /*
429 * 100 ms defaults to poll rate if not
430 * specified. This will be adjusted with
431 * the baud rate at set_termios.
432 */
433 uap->dmarx.auto_poll_rate = true;
434 uap->dmarx.poll_rate = 100;
435 }
436 /* 3 secs defaults poll_timeout if not specified. */
437 if (plat->dma_rx_poll_timeout)
438 uap->dmarx.poll_timeout =
439 plat->dma_rx_poll_timeout;
440 else
441 uap->dmarx.poll_timeout = 3000;
Andrew Jackson98267d32014-11-07 14:14:23 +0000442 } else if (!plat && dev->of_node) {
443 uap->dmarx.auto_poll_rate = of_property_read_bool(
444 dev->of_node, "auto-poll");
445 if (uap->dmarx.auto_poll_rate) {
446 u32 x;
Chanho Mincb06ff12013-03-27 18:38:11 +0900447
Andrew Jackson98267d32014-11-07 14:14:23 +0000448 if (0 == of_property_read_u32(dev->of_node,
449 "poll-rate-ms", &x))
450 uap->dmarx.poll_rate = x;
451 else
452 uap->dmarx.poll_rate = 100;
453 if (0 == of_property_read_u32(dev->of_node,
454 "poll-timeout-ms", &x))
455 uap->dmarx.poll_timeout = x;
456 else
457 uap->dmarx.poll_timeout = 3000;
458 }
459 }
Linus Walleijead76f32011-02-24 13:21:08 +0100460 dev_info(uap->port.dev, "DMA channel RX %s\n",
461 dma_chan_name(uap->dmarx.chan));
462 }
Russell King68b65f72010-12-22 17:24:39 +0000463}
464
Russell King68b65f72010-12-22 17:24:39 +0000465static void pl011_dma_remove(struct uart_amba_port *uap)
466{
Russell King68b65f72010-12-22 17:24:39 +0000467 if (uap->dmatx.chan)
468 dma_release_channel(uap->dmatx.chan);
Linus Walleijead76f32011-02-24 13:21:08 +0100469 if (uap->dmarx.chan)
470 dma_release_channel(uap->dmarx.chan);
Russell King68b65f72010-12-22 17:24:39 +0000471}
472
Dave Martin734745c2015-03-04 12:27:33 +0000473/* Forward declare these for the refill routine */
Russell King68b65f72010-12-22 17:24:39 +0000474static int pl011_dma_tx_refill(struct uart_amba_port *uap);
Dave Martin734745c2015-03-04 12:27:33 +0000475static void pl011_start_tx_pio(struct uart_amba_port *uap);
Russell King68b65f72010-12-22 17:24:39 +0000476
477/*
478 * The current DMA TX buffer has been sent.
479 * Try to queue up another DMA buffer.
480 */
481static void pl011_dma_tx_callback(void *data)
482{
483 struct uart_amba_port *uap = data;
484 struct pl011_dmatx_data *dmatx = &uap->dmatx;
485 unsigned long flags;
486 u16 dmacr;
487
488 spin_lock_irqsave(&uap->port.lock, flags);
489 if (uap->dmatx.queued)
490 dma_unmap_sg(dmatx->chan->device->dev, &dmatx->sg, 1,
491 DMA_TO_DEVICE);
492
493 dmacr = uap->dmacr;
494 uap->dmacr = dmacr & ~UART011_TXDMAE;
Russell King9f25bc52015-11-03 14:51:13 +0000495 pl011_write(uap->dmacr, uap, REG_DMACR);
Russell King68b65f72010-12-22 17:24:39 +0000496
497 /*
498 * If TX DMA was disabled, it means that we've stopped the DMA for
499 * some reason (eg, XOFF received, or we want to send an X-char.)
500 *
501 * Note: we need to be careful here of a potential race between DMA
502 * and the rest of the driver - if the driver disables TX DMA while
503 * a TX buffer completing, we must update the tx queued status to
504 * get further refills (hence we check dmacr).
505 */
506 if (!(dmacr & UART011_TXDMAE) || uart_tx_stopped(&uap->port) ||
507 uart_circ_empty(&uap->port.state->xmit)) {
508 uap->dmatx.queued = false;
509 spin_unlock_irqrestore(&uap->port.lock, flags);
510 return;
511 }
512
Dave Martin734745c2015-03-04 12:27:33 +0000513 if (pl011_dma_tx_refill(uap) <= 0)
Russell King68b65f72010-12-22 17:24:39 +0000514 /*
515 * We didn't queue a DMA buffer for some reason, but we
516 * have data pending to be sent. Re-enable the TX IRQ.
517 */
Dave Martin734745c2015-03-04 12:27:33 +0000518 pl011_start_tx_pio(uap);
519
Russell King68b65f72010-12-22 17:24:39 +0000520 spin_unlock_irqrestore(&uap->port.lock, flags);
521}
522
523/*
524 * Try to refill the TX DMA buffer.
525 * Locking: called with port lock held and IRQs disabled.
526 * Returns:
527 * 1 if we queued up a TX DMA buffer.
528 * 0 if we didn't want to handle this by DMA
529 * <0 on error
530 */
531static int pl011_dma_tx_refill(struct uart_amba_port *uap)
532{
533 struct pl011_dmatx_data *dmatx = &uap->dmatx;
534 struct dma_chan *chan = dmatx->chan;
535 struct dma_device *dma_dev = chan->device;
536 struct dma_async_tx_descriptor *desc;
537 struct circ_buf *xmit = &uap->port.state->xmit;
538 unsigned int count;
539
540 /*
541 * Try to avoid the overhead involved in using DMA if the
542 * transaction fits in the first half of the FIFO, by using
543 * the standard interrupt handling. This ensures that we
544 * issue a uart_write_wakeup() at the appropriate time.
545 */
546 count = uart_circ_chars_pending(xmit);
547 if (count < (uap->fifosize >> 1)) {
548 uap->dmatx.queued = false;
549 return 0;
550 }
551
552 /*
553 * Bodge: don't send the last character by DMA, as this
554 * will prevent XON from notifying us to restart DMA.
555 */
556 count -= 1;
557
558 /* Else proceed to copy the TX chars to the DMA buffer and fire DMA */
559 if (count > PL011_DMA_BUFFER_SIZE)
560 count = PL011_DMA_BUFFER_SIZE;
561
562 if (xmit->tail < xmit->head)
563 memcpy(&dmatx->buf[0], &xmit->buf[xmit->tail], count);
564 else {
565 size_t first = UART_XMIT_SIZE - xmit->tail;
Andrew Jacksone2a545a2014-11-07 14:14:39 +0000566 size_t second;
567
568 if (first > count)
569 first = count;
570 second = count - first;
Russell King68b65f72010-12-22 17:24:39 +0000571
572 memcpy(&dmatx->buf[0], &xmit->buf[xmit->tail], first);
573 if (second)
574 memcpy(&dmatx->buf[first], &xmit->buf[0], second);
575 }
576
577 dmatx->sg.length = count;
578
579 if (dma_map_sg(dma_dev->dev, &dmatx->sg, 1, DMA_TO_DEVICE) != 1) {
580 uap->dmatx.queued = false;
581 dev_dbg(uap->port.dev, "unable to map TX DMA\n");
582 return -EBUSY;
583 }
584
Alexandre Bounine16052822012-03-08 16:11:18 -0500585 desc = dmaengine_prep_slave_sg(chan, &dmatx->sg, 1, DMA_MEM_TO_DEV,
Russell King68b65f72010-12-22 17:24:39 +0000586 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
587 if (!desc) {
588 dma_unmap_sg(dma_dev->dev, &dmatx->sg, 1, DMA_TO_DEVICE);
589 uap->dmatx.queued = false;
590 /*
591 * If DMA cannot be used right now, we complete this
592 * transaction via IRQ and let the TTY layer retry.
593 */
594 dev_dbg(uap->port.dev, "TX DMA busy\n");
595 return -EBUSY;
596 }
597
598 /* Some data to go along to the callback */
599 desc->callback = pl011_dma_tx_callback;
600 desc->callback_param = uap;
601
602 /* All errors should happen at prepare time */
603 dmaengine_submit(desc);
604
605 /* Fire the DMA transaction */
606 dma_dev->device_issue_pending(chan);
607
608 uap->dmacr |= UART011_TXDMAE;
Russell King9f25bc52015-11-03 14:51:13 +0000609 pl011_write(uap->dmacr, uap, REG_DMACR);
Russell King68b65f72010-12-22 17:24:39 +0000610 uap->dmatx.queued = true;
611
612 /*
613 * Now we know that DMA will fire, so advance the ring buffer
614 * with the stuff we just dispatched.
615 */
616 xmit->tail = (xmit->tail + count) & (UART_XMIT_SIZE - 1);
617 uap->port.icount.tx += count;
618
619 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
620 uart_write_wakeup(&uap->port);
621
622 return 1;
623}
624
625/*
626 * We received a transmit interrupt without a pending X-char but with
627 * pending characters.
628 * Locking: called with port lock held and IRQs disabled.
629 * Returns:
630 * false if we want to use PIO to transmit
631 * true if we queued a DMA buffer
632 */
633static bool pl011_dma_tx_irq(struct uart_amba_port *uap)
634{
Linus Walleijead76f32011-02-24 13:21:08 +0100635 if (!uap->using_tx_dma)
Russell King68b65f72010-12-22 17:24:39 +0000636 return false;
637
638 /*
639 * If we already have a TX buffer queued, but received a
640 * TX interrupt, it will be because we've just sent an X-char.
641 * Ensure the TX DMA is enabled and the TX IRQ is disabled.
642 */
643 if (uap->dmatx.queued) {
644 uap->dmacr |= UART011_TXDMAE;
Russell King9f25bc52015-11-03 14:51:13 +0000645 pl011_write(uap->dmacr, uap, REG_DMACR);
Russell King68b65f72010-12-22 17:24:39 +0000646 uap->im &= ~UART011_TXIM;
Russell King9f25bc52015-11-03 14:51:13 +0000647 pl011_write(uap->im, uap, REG_IMSC);
Russell King68b65f72010-12-22 17:24:39 +0000648 return true;
649 }
650
651 /*
652 * We don't have a TX buffer queued, so try to queue one.
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300653 * If we successfully queued a buffer, mask the TX IRQ.
Russell King68b65f72010-12-22 17:24:39 +0000654 */
655 if (pl011_dma_tx_refill(uap) > 0) {
656 uap->im &= ~UART011_TXIM;
Russell King9f25bc52015-11-03 14:51:13 +0000657 pl011_write(uap->im, uap, REG_IMSC);
Russell King68b65f72010-12-22 17:24:39 +0000658 return true;
659 }
660 return false;
661}
662
663/*
664 * Stop the DMA transmit (eg, due to received XOFF).
665 * Locking: called with port lock held and IRQs disabled.
666 */
667static inline void pl011_dma_tx_stop(struct uart_amba_port *uap)
668{
669 if (uap->dmatx.queued) {
670 uap->dmacr &= ~UART011_TXDMAE;
Russell King9f25bc52015-11-03 14:51:13 +0000671 pl011_write(uap->dmacr, uap, REG_DMACR);
Russell King68b65f72010-12-22 17:24:39 +0000672 }
673}
674
675/*
676 * Try to start a DMA transmit, or in the case of an XON/OFF
677 * character queued for send, try to get that character out ASAP.
678 * Locking: called with port lock held and IRQs disabled.
679 * Returns:
680 * false if we want the TX IRQ to be enabled
681 * true if we have a buffer queued
682 */
683static inline bool pl011_dma_tx_start(struct uart_amba_port *uap)
684{
685 u16 dmacr;
686
Linus Walleijead76f32011-02-24 13:21:08 +0100687 if (!uap->using_tx_dma)
Russell King68b65f72010-12-22 17:24:39 +0000688 return false;
689
690 if (!uap->port.x_char) {
691 /* no X-char, try to push chars out in DMA mode */
692 bool ret = true;
693
694 if (!uap->dmatx.queued) {
695 if (pl011_dma_tx_refill(uap) > 0) {
696 uap->im &= ~UART011_TXIM;
Russell King9f25bc52015-11-03 14:51:13 +0000697 pl011_write(uap->im, uap, REG_IMSC);
Dave Martin734745c2015-03-04 12:27:33 +0000698 } else
Russell King68b65f72010-12-22 17:24:39 +0000699 ret = false;
Russell King68b65f72010-12-22 17:24:39 +0000700 } else if (!(uap->dmacr & UART011_TXDMAE)) {
701 uap->dmacr |= UART011_TXDMAE;
Russell King9f25bc52015-11-03 14:51:13 +0000702 pl011_write(uap->dmacr, uap, REG_DMACR);
Russell King68b65f72010-12-22 17:24:39 +0000703 }
704 return ret;
705 }
706
707 /*
708 * We have an X-char to send. Disable DMA to prevent it loading
709 * the TX fifo, and then see if we can stuff it into the FIFO.
710 */
711 dmacr = uap->dmacr;
712 uap->dmacr &= ~UART011_TXDMAE;
Russell King9f25bc52015-11-03 14:51:13 +0000713 pl011_write(uap->dmacr, uap, REG_DMACR);
Russell King68b65f72010-12-22 17:24:39 +0000714
Russell King9f25bc52015-11-03 14:51:13 +0000715 if (pl011_read(uap, REG_FR) & UART01x_FR_TXFF) {
Russell King68b65f72010-12-22 17:24:39 +0000716 /*
717 * No space in the FIFO, so enable the transmit interrupt
718 * so we know when there is space. Note that once we've
719 * loaded the character, we should just re-enable DMA.
720 */
721 return false;
722 }
723
Russell King9f25bc52015-11-03 14:51:13 +0000724 pl011_write(uap->port.x_char, uap, REG_DR);
Russell King68b65f72010-12-22 17:24:39 +0000725 uap->port.icount.tx++;
726 uap->port.x_char = 0;
727
728 /* Success - restore the DMA state */
729 uap->dmacr = dmacr;
Russell King9f25bc52015-11-03 14:51:13 +0000730 pl011_write(dmacr, uap, REG_DMACR);
Russell King68b65f72010-12-22 17:24:39 +0000731
732 return true;
733}
734
735/*
736 * Flush the transmit buffer.
737 * Locking: called with port lock held and IRQs disabled.
738 */
739static void pl011_dma_flush_buffer(struct uart_port *port)
Fabio Estevamb83286b2013-08-09 17:58:51 -0300740__releases(&uap->port.lock)
741__acquires(&uap->port.lock)
Russell King68b65f72010-12-22 17:24:39 +0000742{
Daniel Thompsona5820c22014-09-03 12:51:55 +0100743 struct uart_amba_port *uap =
744 container_of(port, struct uart_amba_port, port);
Russell King68b65f72010-12-22 17:24:39 +0000745
Linus Walleijead76f32011-02-24 13:21:08 +0100746 if (!uap->using_tx_dma)
Russell King68b65f72010-12-22 17:24:39 +0000747 return;
748
749 /* Avoid deadlock with the DMA engine callback */
750 spin_unlock(&uap->port.lock);
751 dmaengine_terminate_all(uap->dmatx.chan);
752 spin_lock(&uap->port.lock);
753 if (uap->dmatx.queued) {
754 dma_unmap_sg(uap->dmatx.chan->device->dev, &uap->dmatx.sg, 1,
755 DMA_TO_DEVICE);
756 uap->dmatx.queued = false;
757 uap->dmacr &= ~UART011_TXDMAE;
Russell King9f25bc52015-11-03 14:51:13 +0000758 pl011_write(uap->dmacr, uap, REG_DMACR);
Russell King68b65f72010-12-22 17:24:39 +0000759 }
760}
761
Linus Walleijead76f32011-02-24 13:21:08 +0100762static void pl011_dma_rx_callback(void *data);
763
764static int pl011_dma_rx_trigger_dma(struct uart_amba_port *uap)
765{
766 struct dma_chan *rxchan = uap->dmarx.chan;
Linus Walleijead76f32011-02-24 13:21:08 +0100767 struct pl011_dmarx_data *dmarx = &uap->dmarx;
768 struct dma_async_tx_descriptor *desc;
769 struct pl011_sgbuf *sgbuf;
770
771 if (!rxchan)
772 return -EIO;
773
774 /* Start the RX DMA job */
775 sgbuf = uap->dmarx.use_buf_b ?
776 &uap->dmarx.sgbuf_b : &uap->dmarx.sgbuf_a;
Alexandre Bounine16052822012-03-08 16:11:18 -0500777 desc = dmaengine_prep_slave_sg(rxchan, &sgbuf->sg, 1,
Vinod Koula485df42011-10-14 10:47:38 +0530778 DMA_DEV_TO_MEM,
Linus Walleijead76f32011-02-24 13:21:08 +0100779 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
780 /*
781 * If the DMA engine is busy and cannot prepare a
782 * channel, no big deal, the driver will fall back
783 * to interrupt mode as a result of this error code.
784 */
785 if (!desc) {
786 uap->dmarx.running = false;
787 dmaengine_terminate_all(rxchan);
788 return -EBUSY;
789 }
790
791 /* Some data to go along to the callback */
792 desc->callback = pl011_dma_rx_callback;
793 desc->callback_param = uap;
794 dmarx->cookie = dmaengine_submit(desc);
795 dma_async_issue_pending(rxchan);
796
797 uap->dmacr |= UART011_RXDMAE;
Russell King9f25bc52015-11-03 14:51:13 +0000798 pl011_write(uap->dmacr, uap, REG_DMACR);
Linus Walleijead76f32011-02-24 13:21:08 +0100799 uap->dmarx.running = true;
800
801 uap->im &= ~UART011_RXIM;
Russell King9f25bc52015-11-03 14:51:13 +0000802 pl011_write(uap->im, uap, REG_IMSC);
Linus Walleijead76f32011-02-24 13:21:08 +0100803
804 return 0;
805}
806
807/*
808 * This is called when either the DMA job is complete, or
809 * the FIFO timeout interrupt occurred. This must be called
810 * with the port spinlock uap->port.lock held.
811 */
812static void pl011_dma_rx_chars(struct uart_amba_port *uap,
813 u32 pending, bool use_buf_b,
814 bool readfifo)
815{
Jiri Slaby05c7cd32013-01-03 15:53:04 +0100816 struct tty_port *port = &uap->port.state->port;
Linus Walleijead76f32011-02-24 13:21:08 +0100817 struct pl011_sgbuf *sgbuf = use_buf_b ?
818 &uap->dmarx.sgbuf_b : &uap->dmarx.sgbuf_a;
Linus Walleijead76f32011-02-24 13:21:08 +0100819 int dma_count = 0;
820 u32 fifotaken = 0; /* only used for vdbg() */
821
Chanho Mincb06ff12013-03-27 18:38:11 +0900822 struct pl011_dmarx_data *dmarx = &uap->dmarx;
823 int dmataken = 0;
824
825 if (uap->dmarx.poll_rate) {
826 /* The data can be taken by polling */
827 dmataken = sgbuf->sg.length - dmarx->last_residue;
828 /* Recalculate the pending size */
829 if (pending >= dmataken)
830 pending -= dmataken;
831 }
832
833 /* Pick the remain data from the DMA */
Linus Walleijead76f32011-02-24 13:21:08 +0100834 if (pending) {
Linus Walleijead76f32011-02-24 13:21:08 +0100835
836 /*
837 * First take all chars in the DMA pipe, then look in the FIFO.
838 * Note that tty_insert_flip_buf() tries to take as many chars
839 * as it can.
840 */
Chanho Mincb06ff12013-03-27 18:38:11 +0900841 dma_count = tty_insert_flip_string(port, sgbuf->buf + dmataken,
842 pending);
Linus Walleijead76f32011-02-24 13:21:08 +0100843
844 uap->port.icount.rx += dma_count;
845 if (dma_count < pending)
846 dev_warn(uap->port.dev,
847 "couldn't insert all characters (TTY is full?)\n");
848 }
849
Chanho Mincb06ff12013-03-27 18:38:11 +0900850 /* Reset the last_residue for Rx DMA poll */
851 if (uap->dmarx.poll_rate)
852 dmarx->last_residue = sgbuf->sg.length;
853
Linus Walleijead76f32011-02-24 13:21:08 +0100854 /*
855 * Only continue with trying to read the FIFO if all DMA chars have
856 * been taken first.
857 */
858 if (dma_count == pending && readfifo) {
859 /* Clear any error flags */
Russell King75836332015-11-03 14:50:58 +0000860 pl011_write(UART011_OEIS | UART011_BEIS | UART011_PEIS |
Russell King9f25bc52015-11-03 14:51:13 +0000861 UART011_FEIS, uap, REG_ICR);
Linus Walleijead76f32011-02-24 13:21:08 +0100862
863 /*
864 * If we read all the DMA'd characters, and we had an
Linus Walleij29772c42011-02-24 13:21:36 +0100865 * incomplete buffer, that could be due to an rx error, or
866 * maybe we just timed out. Read any pending chars and check
867 * the error status.
868 *
869 * Error conditions will only occur in the FIFO, these will
870 * trigger an immediate interrupt and stop the DMA job, so we
871 * will always find the error in the FIFO, never in the DMA
872 * buffer.
Linus Walleijead76f32011-02-24 13:21:08 +0100873 */
Linus Walleij29772c42011-02-24 13:21:36 +0100874 fifotaken = pl011_fifo_to_tty(uap);
Linus Walleijead76f32011-02-24 13:21:08 +0100875 }
876
877 spin_unlock(&uap->port.lock);
878 dev_vdbg(uap->port.dev,
879 "Took %d chars from DMA buffer and %d chars from the FIFO\n",
880 dma_count, fifotaken);
Jiri Slaby2e124b42013-01-03 15:53:06 +0100881 tty_flip_buffer_push(port);
Linus Walleijead76f32011-02-24 13:21:08 +0100882 spin_lock(&uap->port.lock);
883}
884
885static void pl011_dma_rx_irq(struct uart_amba_port *uap)
886{
887 struct pl011_dmarx_data *dmarx = &uap->dmarx;
888 struct dma_chan *rxchan = dmarx->chan;
889 struct pl011_sgbuf *sgbuf = dmarx->use_buf_b ?
890 &dmarx->sgbuf_b : &dmarx->sgbuf_a;
891 size_t pending;
892 struct dma_tx_state state;
893 enum dma_status dmastat;
894
895 /*
896 * Pause the transfer so we can trust the current counter,
897 * do this before we pause the PL011 block, else we may
898 * overflow the FIFO.
899 */
900 if (dmaengine_pause(rxchan))
901 dev_err(uap->port.dev, "unable to pause DMA transfer\n");
902 dmastat = rxchan->device->device_tx_status(rxchan,
903 dmarx->cookie, &state);
904 if (dmastat != DMA_PAUSED)
905 dev_err(uap->port.dev, "unable to pause DMA transfer\n");
906
907 /* Disable RX DMA - incoming data will wait in the FIFO */
908 uap->dmacr &= ~UART011_RXDMAE;
Russell King9f25bc52015-11-03 14:51:13 +0000909 pl011_write(uap->dmacr, uap, REG_DMACR);
Linus Walleijead76f32011-02-24 13:21:08 +0100910 uap->dmarx.running = false;
911
912 pending = sgbuf->sg.length - state.residue;
913 BUG_ON(pending > PL011_DMA_BUFFER_SIZE);
914 /* Then we terminate the transfer - we now know our residue */
915 dmaengine_terminate_all(rxchan);
916
917 /*
918 * This will take the chars we have so far and insert
919 * into the framework.
920 */
921 pl011_dma_rx_chars(uap, pending, dmarx->use_buf_b, true);
922
923 /* Switch buffer & re-trigger DMA job */
924 dmarx->use_buf_b = !dmarx->use_buf_b;
925 if (pl011_dma_rx_trigger_dma(uap)) {
926 dev_dbg(uap->port.dev, "could not retrigger RX DMA job "
927 "fall back to interrupt mode\n");
928 uap->im |= UART011_RXIM;
Russell King9f25bc52015-11-03 14:51:13 +0000929 pl011_write(uap->im, uap, REG_IMSC);
Linus Walleijead76f32011-02-24 13:21:08 +0100930 }
931}
932
933static void pl011_dma_rx_callback(void *data)
934{
935 struct uart_amba_port *uap = data;
936 struct pl011_dmarx_data *dmarx = &uap->dmarx;
Chanho Min6dc01aa2012-02-20 10:24:40 +0900937 struct dma_chan *rxchan = dmarx->chan;
Linus Walleijead76f32011-02-24 13:21:08 +0100938 bool lastbuf = dmarx->use_buf_b;
Chanho Min6dc01aa2012-02-20 10:24:40 +0900939 struct pl011_sgbuf *sgbuf = dmarx->use_buf_b ?
940 &dmarx->sgbuf_b : &dmarx->sgbuf_a;
941 size_t pending;
942 struct dma_tx_state state;
Linus Walleijead76f32011-02-24 13:21:08 +0100943 int ret;
944
945 /*
946 * This completion interrupt occurs typically when the
947 * RX buffer is totally stuffed but no timeout has yet
948 * occurred. When that happens, we just want the RX
949 * routine to flush out the secondary DMA buffer while
950 * we immediately trigger the next DMA job.
951 */
952 spin_lock_irq(&uap->port.lock);
Chanho Min6dc01aa2012-02-20 10:24:40 +0900953 /*
954 * Rx data can be taken by the UART interrupts during
955 * the DMA irq handler. So we check the residue here.
956 */
957 rxchan->device->device_tx_status(rxchan, dmarx->cookie, &state);
958 pending = sgbuf->sg.length - state.residue;
959 BUG_ON(pending > PL011_DMA_BUFFER_SIZE);
960 /* Then we terminate the transfer - we now know our residue */
961 dmaengine_terminate_all(rxchan);
962
Linus Walleijead76f32011-02-24 13:21:08 +0100963 uap->dmarx.running = false;
964 dmarx->use_buf_b = !lastbuf;
965 ret = pl011_dma_rx_trigger_dma(uap);
966
Chanho Min6dc01aa2012-02-20 10:24:40 +0900967 pl011_dma_rx_chars(uap, pending, lastbuf, false);
Linus Walleijead76f32011-02-24 13:21:08 +0100968 spin_unlock_irq(&uap->port.lock);
969 /*
970 * Do this check after we picked the DMA chars so we don't
971 * get some IRQ immediately from RX.
972 */
973 if (ret) {
974 dev_dbg(uap->port.dev, "could not retrigger RX DMA job "
975 "fall back to interrupt mode\n");
976 uap->im |= UART011_RXIM;
Russell King9f25bc52015-11-03 14:51:13 +0000977 pl011_write(uap->im, uap, REG_IMSC);
Linus Walleijead76f32011-02-24 13:21:08 +0100978 }
979}
980
981/*
982 * Stop accepting received characters, when we're shutting down or
983 * suspending this port.
984 * Locking: called with port lock held and IRQs disabled.
985 */
986static inline void pl011_dma_rx_stop(struct uart_amba_port *uap)
987{
988 /* FIXME. Just disable the DMA enable */
989 uap->dmacr &= ~UART011_RXDMAE;
Russell King9f25bc52015-11-03 14:51:13 +0000990 pl011_write(uap->dmacr, uap, REG_DMACR);
Linus Walleijead76f32011-02-24 13:21:08 +0100991}
Russell King68b65f72010-12-22 17:24:39 +0000992
Chanho Mincb06ff12013-03-27 18:38:11 +0900993/*
994 * Timer handler for Rx DMA polling.
995 * Every polling, It checks the residue in the dma buffer and transfer
996 * data to the tty. Also, last_residue is updated for the next polling.
997 */
998static void pl011_dma_rx_poll(unsigned long args)
999{
1000 struct uart_amba_port *uap = (struct uart_amba_port *)args;
1001 struct tty_port *port = &uap->port.state->port;
1002 struct pl011_dmarx_data *dmarx = &uap->dmarx;
1003 struct dma_chan *rxchan = uap->dmarx.chan;
1004 unsigned long flags = 0;
1005 unsigned int dmataken = 0;
1006 unsigned int size = 0;
1007 struct pl011_sgbuf *sgbuf;
1008 int dma_count;
1009 struct dma_tx_state state;
1010
1011 sgbuf = dmarx->use_buf_b ? &uap->dmarx.sgbuf_b : &uap->dmarx.sgbuf_a;
1012 rxchan->device->device_tx_status(rxchan, dmarx->cookie, &state);
1013 if (likely(state.residue < dmarx->last_residue)) {
1014 dmataken = sgbuf->sg.length - dmarx->last_residue;
1015 size = dmarx->last_residue - state.residue;
1016 dma_count = tty_insert_flip_string(port, sgbuf->buf + dmataken,
1017 size);
1018 if (dma_count == size)
1019 dmarx->last_residue = state.residue;
1020 dmarx->last_jiffies = jiffies;
1021 }
1022 tty_flip_buffer_push(port);
1023
1024 /*
1025 * If no data is received in poll_timeout, the driver will fall back
1026 * to interrupt mode. We will retrigger DMA at the first interrupt.
1027 */
1028 if (jiffies_to_msecs(jiffies - dmarx->last_jiffies)
1029 > uap->dmarx.poll_timeout) {
1030
1031 spin_lock_irqsave(&uap->port.lock, flags);
1032 pl011_dma_rx_stop(uap);
Guennadi Liakhovetskic25a1ad2013-12-10 14:54:47 +01001033 uap->im |= UART011_RXIM;
Russell King9f25bc52015-11-03 14:51:13 +00001034 pl011_write(uap->im, uap, REG_IMSC);
Chanho Mincb06ff12013-03-27 18:38:11 +09001035 spin_unlock_irqrestore(&uap->port.lock, flags);
1036
1037 uap->dmarx.running = false;
1038 dmaengine_terminate_all(rxchan);
1039 del_timer(&uap->dmarx.timer);
1040 } else {
1041 mod_timer(&uap->dmarx.timer,
1042 jiffies + msecs_to_jiffies(uap->dmarx.poll_rate));
1043 }
1044}
1045
Russell King68b65f72010-12-22 17:24:39 +00001046static void pl011_dma_startup(struct uart_amba_port *uap)
1047{
Linus Walleijead76f32011-02-24 13:21:08 +01001048 int ret;
1049
Jorge Ramirez-Ortiz1c9be312015-03-06 13:05:40 -05001050 if (!uap->dma_probed)
1051 pl011_dma_probe(uap);
1052
Russell King68b65f72010-12-22 17:24:39 +00001053 if (!uap->dmatx.chan)
1054 return;
1055
Andrew Jackson4c0be452014-11-07 14:14:35 +00001056 uap->dmatx.buf = kmalloc(PL011_DMA_BUFFER_SIZE, GFP_KERNEL | __GFP_DMA);
Russell King68b65f72010-12-22 17:24:39 +00001057 if (!uap->dmatx.buf) {
1058 dev_err(uap->port.dev, "no memory for DMA TX buffer\n");
1059 uap->port.fifosize = uap->fifosize;
1060 return;
1061 }
1062
1063 sg_init_one(&uap->dmatx.sg, uap->dmatx.buf, PL011_DMA_BUFFER_SIZE);
1064
1065 /* The DMA buffer is now the FIFO the TTY subsystem can use */
1066 uap->port.fifosize = PL011_DMA_BUFFER_SIZE;
Linus Walleijead76f32011-02-24 13:21:08 +01001067 uap->using_tx_dma = true;
Russell King68b65f72010-12-22 17:24:39 +00001068
Linus Walleijead76f32011-02-24 13:21:08 +01001069 if (!uap->dmarx.chan)
1070 goto skip_rx;
1071
1072 /* Allocate and map DMA RX buffers */
1073 ret = pl011_sgbuf_init(uap->dmarx.chan, &uap->dmarx.sgbuf_a,
1074 DMA_FROM_DEVICE);
1075 if (ret) {
1076 dev_err(uap->port.dev, "failed to init DMA %s: %d\n",
1077 "RX buffer A", ret);
1078 goto skip_rx;
1079 }
1080
1081 ret = pl011_sgbuf_init(uap->dmarx.chan, &uap->dmarx.sgbuf_b,
1082 DMA_FROM_DEVICE);
1083 if (ret) {
1084 dev_err(uap->port.dev, "failed to init DMA %s: %d\n",
1085 "RX buffer B", ret);
1086 pl011_sgbuf_free(uap->dmarx.chan, &uap->dmarx.sgbuf_a,
1087 DMA_FROM_DEVICE);
1088 goto skip_rx;
1089 }
1090
1091 uap->using_rx_dma = true;
1092
1093skip_rx:
Russell King68b65f72010-12-22 17:24:39 +00001094 /* Turn on DMA error (RX/TX will be enabled on demand) */
1095 uap->dmacr |= UART011_DMAONERR;
Russell King9f25bc52015-11-03 14:51:13 +00001096 pl011_write(uap->dmacr, uap, REG_DMACR);
Russell King38d62432010-12-22 17:59:16 +00001097
1098 /*
1099 * ST Micro variants has some specific dma burst threshold
1100 * compensation. Set this to 16 bytes, so burst will only
1101 * be issued above/below 16 bytes.
1102 */
1103 if (uap->vendor->dma_threshold)
Russell King75836332015-11-03 14:50:58 +00001104 pl011_write(ST_UART011_DMAWM_RX_16 | ST_UART011_DMAWM_TX_16,
Russell King9f25bc52015-11-03 14:51:13 +00001105 uap, REG_ST_DMAWM);
Linus Walleijead76f32011-02-24 13:21:08 +01001106
1107 if (uap->using_rx_dma) {
1108 if (pl011_dma_rx_trigger_dma(uap))
1109 dev_dbg(uap->port.dev, "could not trigger initial "
1110 "RX DMA job, fall back to interrupt mode\n");
Chanho Mincb06ff12013-03-27 18:38:11 +09001111 if (uap->dmarx.poll_rate) {
1112 init_timer(&(uap->dmarx.timer));
1113 uap->dmarx.timer.function = pl011_dma_rx_poll;
1114 uap->dmarx.timer.data = (unsigned long)uap;
1115 mod_timer(&uap->dmarx.timer,
1116 jiffies +
1117 msecs_to_jiffies(uap->dmarx.poll_rate));
1118 uap->dmarx.last_residue = PL011_DMA_BUFFER_SIZE;
1119 uap->dmarx.last_jiffies = jiffies;
1120 }
Linus Walleijead76f32011-02-24 13:21:08 +01001121 }
Russell King68b65f72010-12-22 17:24:39 +00001122}
1123
1124static void pl011_dma_shutdown(struct uart_amba_port *uap)
1125{
Linus Walleijead76f32011-02-24 13:21:08 +01001126 if (!(uap->using_tx_dma || uap->using_rx_dma))
Russell King68b65f72010-12-22 17:24:39 +00001127 return;
1128
1129 /* Disable RX and TX DMA */
Russell King9f25bc52015-11-03 14:51:13 +00001130 while (pl011_read(uap, REG_FR) & UART01x_FR_BUSY)
Russell King68b65f72010-12-22 17:24:39 +00001131 barrier();
1132
1133 spin_lock_irq(&uap->port.lock);
1134 uap->dmacr &= ~(UART011_DMAONERR | UART011_RXDMAE | UART011_TXDMAE);
Russell King9f25bc52015-11-03 14:51:13 +00001135 pl011_write(uap->dmacr, uap, REG_DMACR);
Russell King68b65f72010-12-22 17:24:39 +00001136 spin_unlock_irq(&uap->port.lock);
1137
Linus Walleijead76f32011-02-24 13:21:08 +01001138 if (uap->using_tx_dma) {
1139 /* In theory, this should already be done by pl011_dma_flush_buffer */
1140 dmaengine_terminate_all(uap->dmatx.chan);
1141 if (uap->dmatx.queued) {
1142 dma_unmap_sg(uap->dmatx.chan->device->dev, &uap->dmatx.sg, 1,
1143 DMA_TO_DEVICE);
1144 uap->dmatx.queued = false;
1145 }
1146
1147 kfree(uap->dmatx.buf);
1148 uap->using_tx_dma = false;
Russell King68b65f72010-12-22 17:24:39 +00001149 }
1150
Linus Walleijead76f32011-02-24 13:21:08 +01001151 if (uap->using_rx_dma) {
1152 dmaengine_terminate_all(uap->dmarx.chan);
1153 /* Clean up the RX DMA */
1154 pl011_sgbuf_free(uap->dmarx.chan, &uap->dmarx.sgbuf_a, DMA_FROM_DEVICE);
1155 pl011_sgbuf_free(uap->dmarx.chan, &uap->dmarx.sgbuf_b, DMA_FROM_DEVICE);
Chanho Mincb06ff12013-03-27 18:38:11 +09001156 if (uap->dmarx.poll_rate)
1157 del_timer_sync(&uap->dmarx.timer);
Linus Walleijead76f32011-02-24 13:21:08 +01001158 uap->using_rx_dma = false;
1159 }
Russell King68b65f72010-12-22 17:24:39 +00001160}
1161
Linus Walleijead76f32011-02-24 13:21:08 +01001162static inline bool pl011_dma_rx_available(struct uart_amba_port *uap)
1163{
1164 return uap->using_rx_dma;
1165}
1166
1167static inline bool pl011_dma_rx_running(struct uart_amba_port *uap)
1168{
1169 return uap->using_rx_dma && uap->dmarx.running;
1170}
1171
Russell King68b65f72010-12-22 17:24:39 +00001172#else
1173/* Blank functions if the DMA engine is not available */
Jorge Ramirez-Ortiz1c9be312015-03-06 13:05:40 -05001174static inline void pl011_dma_probe(struct uart_amba_port *uap)
Russell King68b65f72010-12-22 17:24:39 +00001175{
1176}
1177
1178static inline void pl011_dma_remove(struct uart_amba_port *uap)
1179{
1180}
1181
1182static inline void pl011_dma_startup(struct uart_amba_port *uap)
1183{
1184}
1185
1186static inline void pl011_dma_shutdown(struct uart_amba_port *uap)
1187{
1188}
1189
1190static inline bool pl011_dma_tx_irq(struct uart_amba_port *uap)
1191{
1192 return false;
1193}
1194
1195static inline void pl011_dma_tx_stop(struct uart_amba_port *uap)
1196{
1197}
1198
1199static inline bool pl011_dma_tx_start(struct uart_amba_port *uap)
1200{
1201 return false;
1202}
1203
Linus Walleijead76f32011-02-24 13:21:08 +01001204static inline void pl011_dma_rx_irq(struct uart_amba_port *uap)
1205{
1206}
1207
1208static inline void pl011_dma_rx_stop(struct uart_amba_port *uap)
1209{
1210}
1211
1212static inline int pl011_dma_rx_trigger_dma(struct uart_amba_port *uap)
1213{
1214 return -EIO;
1215}
1216
1217static inline bool pl011_dma_rx_available(struct uart_amba_port *uap)
1218{
1219 return false;
1220}
1221
1222static inline bool pl011_dma_rx_running(struct uart_amba_port *uap)
1223{
1224 return false;
1225}
1226
Russell King68b65f72010-12-22 17:24:39 +00001227#define pl011_dma_flush_buffer NULL
1228#endif
1229
Russell Kingb129a8c2005-08-31 10:12:14 +01001230static void pl011_stop_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001232 struct uart_amba_port *uap =
1233 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234
1235 uap->im &= ~UART011_TXIM;
Russell King9f25bc52015-11-03 14:51:13 +00001236 pl011_write(uap->im, uap, REG_IMSC);
Russell King68b65f72010-12-22 17:24:39 +00001237 pl011_dma_tx_stop(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238}
1239
Dave Martin1e84d222015-04-27 16:49:05 +01001240static void pl011_tx_chars(struct uart_amba_port *uap, bool from_irq);
Dave Martin734745c2015-03-04 12:27:33 +00001241
1242/* Start TX with programmed I/O only (no DMA) */
1243static void pl011_start_tx_pio(struct uart_amba_port *uap)
1244{
1245 uap->im |= UART011_TXIM;
Russell King9f25bc52015-11-03 14:51:13 +00001246 pl011_write(uap->im, uap, REG_IMSC);
Dave Martin1e84d222015-04-27 16:49:05 +01001247 pl011_tx_chars(uap, false);
Dave Martin734745c2015-03-04 12:27:33 +00001248}
1249
Russell Kingb129a8c2005-08-31 10:12:14 +01001250static void pl011_start_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001252 struct uart_amba_port *uap =
1253 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254
Dave Martin734745c2015-03-04 12:27:33 +00001255 if (!pl011_dma_tx_start(uap))
1256 pl011_start_tx_pio(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257}
1258
1259static void pl011_stop_rx(struct uart_port *port)
1260{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001261 struct uart_amba_port *uap =
1262 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263
1264 uap->im &= ~(UART011_RXIM|UART011_RTIM|UART011_FEIM|
1265 UART011_PEIM|UART011_BEIM|UART011_OEIM);
Russell King9f25bc52015-11-03 14:51:13 +00001266 pl011_write(uap->im, uap, REG_IMSC);
Linus Walleijead76f32011-02-24 13:21:08 +01001267
1268 pl011_dma_rx_stop(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269}
1270
1271static void pl011_enable_ms(struct uart_port *port)
1272{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001273 struct uart_amba_port *uap =
1274 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275
1276 uap->im |= UART011_RIMIM|UART011_CTSMIM|UART011_DCDMIM|UART011_DSRMIM;
Russell King9f25bc52015-11-03 14:51:13 +00001277 pl011_write(uap->im, uap, REG_IMSC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278}
1279
David Howells7d12e782006-10-05 14:55:46 +01001280static void pl011_rx_chars(struct uart_amba_port *uap)
Fabio Estevamb83286b2013-08-09 17:58:51 -03001281__releases(&uap->port.lock)
1282__acquires(&uap->port.lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283{
Linus Walleij29772c42011-02-24 13:21:36 +01001284 pl011_fifo_to_tty(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285
Thomas Gleixner2389b272007-05-29 21:53:50 +01001286 spin_unlock(&uap->port.lock);
Jiri Slaby2e124b42013-01-03 15:53:06 +01001287 tty_flip_buffer_push(&uap->port.state->port);
Linus Walleijead76f32011-02-24 13:21:08 +01001288 /*
1289 * If we were temporarily out of DMA mode for a while,
1290 * attempt to switch back to DMA mode again.
1291 */
1292 if (pl011_dma_rx_available(uap)) {
1293 if (pl011_dma_rx_trigger_dma(uap)) {
1294 dev_dbg(uap->port.dev, "could not trigger RX DMA job "
1295 "fall back to interrupt mode again\n");
1296 uap->im |= UART011_RXIM;
Russell King9f25bc52015-11-03 14:51:13 +00001297 pl011_write(uap->im, uap, REG_IMSC);
Chanho Mincb06ff12013-03-27 18:38:11 +09001298 } else {
Chanho Min89fa28d2013-04-03 11:10:37 +09001299#ifdef CONFIG_DMA_ENGINE
Chanho Mincb06ff12013-03-27 18:38:11 +09001300 /* Start Rx DMA poll */
1301 if (uap->dmarx.poll_rate) {
1302 uap->dmarx.last_jiffies = jiffies;
1303 uap->dmarx.last_residue = PL011_DMA_BUFFER_SIZE;
1304 mod_timer(&uap->dmarx.timer,
1305 jiffies +
1306 msecs_to_jiffies(uap->dmarx.poll_rate));
1307 }
Chanho Min89fa28d2013-04-03 11:10:37 +09001308#endif
Chanho Mincb06ff12013-03-27 18:38:11 +09001309 }
Linus Walleijead76f32011-02-24 13:21:08 +01001310 }
Thomas Gleixner2389b272007-05-29 21:53:50 +01001311 spin_lock(&uap->port.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312}
1313
Dave Martin1e84d222015-04-27 16:49:05 +01001314static bool pl011_tx_char(struct uart_amba_port *uap, unsigned char c,
1315 bool from_irq)
Dave Martin734745c2015-03-04 12:27:33 +00001316{
Dave Martin1e84d222015-04-27 16:49:05 +01001317 if (unlikely(!from_irq) &&
Russell King9f25bc52015-11-03 14:51:13 +00001318 pl011_read(uap, REG_FR) & UART01x_FR_TXFF)
Dave Martin1e84d222015-04-27 16:49:05 +01001319 return false; /* unable to transmit character */
1320
Russell King9f25bc52015-11-03 14:51:13 +00001321 pl011_write(c, uap, REG_DR);
Dave Martin734745c2015-03-04 12:27:33 +00001322 uap->port.icount.tx++;
1323
Dave Martin1e84d222015-04-27 16:49:05 +01001324 return true;
Dave Martin734745c2015-03-04 12:27:33 +00001325}
1326
Dave Martin1e84d222015-04-27 16:49:05 +01001327static void pl011_tx_chars(struct uart_amba_port *uap, bool from_irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328{
Alan Coxebd2c8f2009-09-19 13:13:28 -07001329 struct circ_buf *xmit = &uap->port.state->xmit;
Dave Martin1e84d222015-04-27 16:49:05 +01001330 int count = uap->fifosize >> 1;
Dave Martin734745c2015-03-04 12:27:33 +00001331
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 if (uap->port.x_char) {
Dave Martin1e84d222015-04-27 16:49:05 +01001333 if (!pl011_tx_char(uap, uap->port.x_char, from_irq))
1334 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 uap->port.x_char = 0;
Dave Martin734745c2015-03-04 12:27:33 +00001336 --count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 }
1338 if (uart_circ_empty(xmit) || uart_tx_stopped(&uap->port)) {
Russell Kingb129a8c2005-08-31 10:12:14 +01001339 pl011_stop_tx(&uap->port);
Dave Martin1e84d222015-04-27 16:49:05 +01001340 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 }
1342
Russell King68b65f72010-12-22 17:24:39 +00001343 /* If we are using DMA mode, try to send some characters. */
1344 if (pl011_dma_tx_irq(uap))
Dave Martin1e84d222015-04-27 16:49:05 +01001345 return;
Russell King68b65f72010-12-22 17:24:39 +00001346
Dave Martin1e84d222015-04-27 16:49:05 +01001347 do {
1348 if (likely(from_irq) && count-- == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 break;
Dave Martin1e84d222015-04-27 16:49:05 +01001350
1351 if (!pl011_tx_char(uap, xmit->buf[xmit->tail], from_irq))
1352 break;
1353
1354 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
1355 } while (!uart_circ_empty(xmit));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356
1357 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
1358 uart_write_wakeup(&uap->port);
1359
Dave Martin1e84d222015-04-27 16:49:05 +01001360 if (uart_circ_empty(xmit))
Russell Kingb129a8c2005-08-31 10:12:14 +01001361 pl011_stop_tx(&uap->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362}
1363
1364static void pl011_modem_status(struct uart_amba_port *uap)
1365{
1366 unsigned int status, delta;
1367
Russell King9f25bc52015-11-03 14:51:13 +00001368 status = pl011_read(uap, REG_FR) & UART01x_FR_MODEM_ANY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369
1370 delta = status ^ uap->old_status;
1371 uap->old_status = status;
1372
1373 if (!delta)
1374 return;
1375
1376 if (delta & UART01x_FR_DCD)
1377 uart_handle_dcd_change(&uap->port, status & UART01x_FR_DCD);
1378
Greg Kroah-Hartman062a68a2015-09-04 09:11:24 -07001379 if (delta & UART01x_FR_DSR)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 uap->port.icount.dsr++;
1381
Greg Kroah-Hartman062a68a2015-09-04 09:11:24 -07001382 if (delta & UART01x_FR_CTS)
1383 uart_handle_cts_change(&uap->port, status & UART01x_FR_CTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384
Alan Coxbdc04e32009-09-19 13:13:31 -07001385 wake_up_interruptible(&uap->port.state->port.delta_msr_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386}
1387
Andre Przywara9c4ef4b2015-05-21 17:26:20 +01001388static void check_apply_cts_event_workaround(struct uart_amba_port *uap)
1389{
1390 unsigned int dummy_read;
1391
1392 if (!uap->vendor->cts_event_workaround)
1393 return;
1394
1395 /* workaround to make sure that all bits are unlocked.. */
Russell King9f25bc52015-11-03 14:51:13 +00001396 pl011_write(0x00, uap, REG_ICR);
Andre Przywara9c4ef4b2015-05-21 17:26:20 +01001397
1398 /*
1399 * WA: introduce 26ns(1 uart clk) delay before W1C;
1400 * single apb access will incur 2 pclk(133.12Mhz) delay,
1401 * so add 2 dummy reads
1402 */
Russell King9f25bc52015-11-03 14:51:13 +00001403 dummy_read = pl011_read(uap, REG_ICR);
1404 dummy_read = pl011_read(uap, REG_ICR);
Andre Przywara9c4ef4b2015-05-21 17:26:20 +01001405}
1406
David Howells7d12e782006-10-05 14:55:46 +01001407static irqreturn_t pl011_int(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408{
1409 struct uart_amba_port *uap = dev_id;
Russell King963cc982010-12-22 17:16:09 +00001410 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 unsigned int status, pass_counter = AMBA_ISR_PASS_LIMIT;
Andre Przywara075167e2015-05-21 17:26:19 +01001412 u16 imsc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 int handled = 0;
1414
Russell King963cc982010-12-22 17:16:09 +00001415 spin_lock_irqsave(&uap->port.lock, flags);
Russell King9f25bc52015-11-03 14:51:13 +00001416 imsc = pl011_read(uap, REG_IMSC);
1417 status = pl011_read(uap, REG_RIS) & imsc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 if (status) {
1419 do {
Andre Przywara9c4ef4b2015-05-21 17:26:20 +01001420 check_apply_cts_event_workaround(uap);
Greg Kroah-Hartmanf11c9842015-09-04 09:13:39 -07001421
Russell King75836332015-11-03 14:50:58 +00001422 pl011_write(status & ~(UART011_TXIS|UART011_RTIS|
1423 UART011_RXIS),
Russell King9f25bc52015-11-03 14:51:13 +00001424 uap, REG_ICR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425
Linus Walleijead76f32011-02-24 13:21:08 +01001426 if (status & (UART011_RTIS|UART011_RXIS)) {
1427 if (pl011_dma_rx_running(uap))
1428 pl011_dma_rx_irq(uap);
1429 else
1430 pl011_rx_chars(uap);
1431 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 if (status & (UART011_DSRMIS|UART011_DCDMIS|
1433 UART011_CTSMIS|UART011_RIMIS))
1434 pl011_modem_status(uap);
Dave Martin1e84d222015-04-27 16:49:05 +01001435 if (status & UART011_TXIS)
1436 pl011_tx_chars(uap, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437
Rajanikanth H.V4fd06902012-03-26 11:17:02 +02001438 if (pass_counter-- == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 break;
1440
Russell King9f25bc52015-11-03 14:51:13 +00001441 status = pl011_read(uap, REG_RIS) & imsc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 } while (status != 0);
1443 handled = 1;
1444 }
1445
Russell King963cc982010-12-22 17:16:09 +00001446 spin_unlock_irqrestore(&uap->port.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447
1448 return IRQ_RETVAL(handled);
1449}
1450
Linus Walleije643f872012-06-17 15:44:19 +02001451static unsigned int pl011_tx_empty(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001453 struct uart_amba_port *uap =
1454 container_of(port, struct uart_amba_port, port);
Russell King9f25bc52015-11-03 14:51:13 +00001455 unsigned int status = pl011_read(uap, REG_FR);
Greg Kroah-Hartman062a68a2015-09-04 09:11:24 -07001456 return status & (UART01x_FR_BUSY|UART01x_FR_TXFF) ? 0 : TIOCSER_TEMT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457}
1458
Linus Walleije643f872012-06-17 15:44:19 +02001459static unsigned int pl011_get_mctrl(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001461 struct uart_amba_port *uap =
1462 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 unsigned int result = 0;
Russell King9f25bc52015-11-03 14:51:13 +00001464 unsigned int status = pl011_read(uap, REG_FR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465
Jiri Slaby5159f402007-10-18 23:40:31 -07001466#define TIOCMBIT(uartbit, tiocmbit) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 if (status & uartbit) \
1468 result |= tiocmbit
1469
Jiri Slaby5159f402007-10-18 23:40:31 -07001470 TIOCMBIT(UART01x_FR_DCD, TIOCM_CAR);
Greg Kroah-Hartman062a68a2015-09-04 09:11:24 -07001471 TIOCMBIT(UART01x_FR_DSR, TIOCM_DSR);
1472 TIOCMBIT(UART01x_FR_CTS, TIOCM_CTS);
1473 TIOCMBIT(UART011_FR_RI, TIOCM_RNG);
Jiri Slaby5159f402007-10-18 23:40:31 -07001474#undef TIOCMBIT
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 return result;
1476}
1477
1478static void pl011_set_mctrl(struct uart_port *port, unsigned int mctrl)
1479{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001480 struct uart_amba_port *uap =
1481 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 unsigned int cr;
1483
Russell King9f25bc52015-11-03 14:51:13 +00001484 cr = pl011_read(uap, REG_CR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485
Jiri Slaby5159f402007-10-18 23:40:31 -07001486#define TIOCMBIT(tiocmbit, uartbit) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 if (mctrl & tiocmbit) \
1488 cr |= uartbit; \
1489 else \
1490 cr &= ~uartbit
1491
Jiri Slaby5159f402007-10-18 23:40:31 -07001492 TIOCMBIT(TIOCM_RTS, UART011_CR_RTS);
1493 TIOCMBIT(TIOCM_DTR, UART011_CR_DTR);
1494 TIOCMBIT(TIOCM_OUT1, UART011_CR_OUT1);
1495 TIOCMBIT(TIOCM_OUT2, UART011_CR_OUT2);
1496 TIOCMBIT(TIOCM_LOOP, UART011_CR_LBE);
Rabin Vincent3b438162010-02-12 06:43:11 +01001497
1498 if (uap->autorts) {
1499 /* We need to disable auto-RTS if we want to turn RTS off */
1500 TIOCMBIT(TIOCM_RTS, UART011_CR_RTSEN);
1501 }
Jiri Slaby5159f402007-10-18 23:40:31 -07001502#undef TIOCMBIT
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503
Russell King9f25bc52015-11-03 14:51:13 +00001504 pl011_write(cr, uap, REG_CR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505}
1506
1507static void pl011_break_ctl(struct uart_port *port, int break_state)
1508{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001509 struct uart_amba_port *uap =
1510 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 unsigned long flags;
1512 unsigned int lcr_h;
1513
1514 spin_lock_irqsave(&uap->port.lock, flags);
Russell Kingb2a4e242015-11-03 14:51:03 +00001515 lcr_h = pl011_read(uap, uap->lcrh_tx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 if (break_state == -1)
1517 lcr_h |= UART01x_LCRH_BRK;
1518 else
1519 lcr_h &= ~UART01x_LCRH_BRK;
Russell Kingb2a4e242015-11-03 14:51:03 +00001520 pl011_write(lcr_h, uap, uap->lcrh_tx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 spin_unlock_irqrestore(&uap->port.lock, flags);
1522}
1523
Jason Wessel84b5ae12008-02-20 13:33:39 -06001524#ifdef CONFIG_CONSOLE_POLL
Anton Vorontsov5c8124a2012-09-24 14:27:55 -07001525
1526static void pl011_quiesce_irqs(struct uart_port *port)
1527{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001528 struct uart_amba_port *uap =
1529 container_of(port, struct uart_amba_port, port);
Anton Vorontsov5c8124a2012-09-24 14:27:55 -07001530
Russell King9f25bc52015-11-03 14:51:13 +00001531 pl011_write(pl011_read(uap, REG_MIS), uap, REG_ICR);
Anton Vorontsov5c8124a2012-09-24 14:27:55 -07001532 /*
1533 * There is no way to clear TXIM as this is "ready to transmit IRQ", so
1534 * we simply mask it. start_tx() will unmask it.
1535 *
1536 * Note we can race with start_tx(), and if the race happens, the
1537 * polling user might get another interrupt just after we clear it.
1538 * But it should be OK and can happen even w/o the race, e.g.
1539 * controller immediately got some new data and raised the IRQ.
1540 *
1541 * And whoever uses polling routines assumes that it manages the device
1542 * (including tx queue), so we're also fine with start_tx()'s caller
1543 * side.
1544 */
Russell King9f25bc52015-11-03 14:51:13 +00001545 pl011_write(pl011_read(uap, REG_IMSC) & ~UART011_TXIM, uap,
1546 REG_IMSC);
Anton Vorontsov5c8124a2012-09-24 14:27:55 -07001547}
1548
Linus Walleije643f872012-06-17 15:44:19 +02001549static int pl011_get_poll_char(struct uart_port *port)
Jason Wessel84b5ae12008-02-20 13:33:39 -06001550{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001551 struct uart_amba_port *uap =
1552 container_of(port, struct uart_amba_port, port);
Jason Wessel84b5ae12008-02-20 13:33:39 -06001553 unsigned int status;
1554
Anton Vorontsov5c8124a2012-09-24 14:27:55 -07001555 /*
1556 * The caller might need IRQs lowered, e.g. if used with KDB NMI
1557 * debugger.
1558 */
1559 pl011_quiesce_irqs(port);
1560
Russell King9f25bc52015-11-03 14:51:13 +00001561 status = pl011_read(uap, REG_FR);
Jason Wesself5316b42010-05-20 21:04:22 -05001562 if (status & UART01x_FR_RXFE)
1563 return NO_POLL_CHAR;
Jason Wessel84b5ae12008-02-20 13:33:39 -06001564
Russell King9f25bc52015-11-03 14:51:13 +00001565 return pl011_read(uap, REG_DR);
Jason Wessel84b5ae12008-02-20 13:33:39 -06001566}
1567
Linus Walleije643f872012-06-17 15:44:19 +02001568static void pl011_put_poll_char(struct uart_port *port,
Jason Wessel84b5ae12008-02-20 13:33:39 -06001569 unsigned char ch)
1570{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001571 struct uart_amba_port *uap =
1572 container_of(port, struct uart_amba_port, port);
Jason Wessel84b5ae12008-02-20 13:33:39 -06001573
Russell King9f25bc52015-11-03 14:51:13 +00001574 while (pl011_read(uap, REG_FR) & UART01x_FR_TXFF)
Jason Wessel84b5ae12008-02-20 13:33:39 -06001575 barrier();
1576
Russell King9f25bc52015-11-03 14:51:13 +00001577 pl011_write(ch, uap, REG_DR);
Jason Wessel84b5ae12008-02-20 13:33:39 -06001578}
1579
1580#endif /* CONFIG_CONSOLE_POLL */
1581
Anton Vorontsovb3564c22012-09-24 14:27:54 -07001582static int pl011_hwinit(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001584 struct uart_amba_port *uap =
1585 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 int retval;
1587
Linus Walleij78d80c52012-05-23 21:18:46 +02001588 /* Optionaly enable pins to be muxed in and configured */
Linus Walleij2b996fc2013-06-05 15:36:42 +02001589 pinctrl_pm_select_default_state(port->dev);
Linus Walleij78d80c52012-05-23 21:18:46 +02001590
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 /*
1592 * Try to enable the clock producer.
1593 */
Julia Lawall1c4c4392012-08-26 18:01:01 +02001594 retval = clk_prepare_enable(uap->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 if (retval)
Tushar Behera7f6d9422014-06-26 15:35:35 +05301596 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597
1598 uap->port.uartclk = clk_get_rate(uap->clk);
1599
Linus Walleij9b96fba2012-03-13 13:27:23 +01001600 /* Clear pending error and receive interrupts */
Russell King75836332015-11-03 14:50:58 +00001601 pl011_write(UART011_OEIS | UART011_BEIS | UART011_PEIS |
1602 UART011_FEIS | UART011_RTIS | UART011_RXIS,
Russell King9f25bc52015-11-03 14:51:13 +00001603 uap, REG_ICR);
Linus Walleij9b96fba2012-03-13 13:27:23 +01001604
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 /*
Anton Vorontsovb3564c22012-09-24 14:27:54 -07001606 * Save interrupts enable mask, and enable RX interrupts in case if
1607 * the interrupt is used for NMI entry.
1608 */
Russell King9f25bc52015-11-03 14:51:13 +00001609 uap->im = pl011_read(uap, REG_IMSC);
1610 pl011_write(UART011_RTIM | UART011_RXIM, uap, REG_IMSC);
Anton Vorontsovb3564c22012-09-24 14:27:54 -07001611
Jingoo Han574de552013-07-30 17:06:57 +09001612 if (dev_get_platdata(uap->port.dev)) {
Anton Vorontsovb3564c22012-09-24 14:27:54 -07001613 struct amba_pl011_data *plat;
1614
Jingoo Han574de552013-07-30 17:06:57 +09001615 plat = dev_get_platdata(uap->port.dev);
Anton Vorontsovb3564c22012-09-24 14:27:54 -07001616 if (plat->init)
1617 plat->init();
1618 }
1619 return 0;
Anton Vorontsovb3564c22012-09-24 14:27:54 -07001620}
1621
Russell King7fe9a5a2015-11-03 14:51:08 +00001622static bool pl011_split_lcrh(const struct uart_amba_port *uap)
1623{
Russell King9f25bc52015-11-03 14:51:13 +00001624 return pl011_reg_to_offset(uap, uap->lcrh_rx) !=
1625 pl011_reg_to_offset(uap, uap->lcrh_tx);
Russell King7fe9a5a2015-11-03 14:51:08 +00001626}
1627
Jon Medhurstb60f2f62013-12-10 10:18:59 +00001628static void pl011_write_lcr_h(struct uart_amba_port *uap, unsigned int lcr_h)
1629{
Russell Kingb2a4e242015-11-03 14:51:03 +00001630 pl011_write(lcr_h, uap, uap->lcrh_rx);
Russell King7fe9a5a2015-11-03 14:51:08 +00001631 if (pl011_split_lcrh(uap)) {
Jon Medhurstb60f2f62013-12-10 10:18:59 +00001632 int i;
1633 /*
1634 * Wait 10 PCLKs before writing LCRH_TX register,
1635 * to get this delay write read only register 10 times
1636 */
1637 for (i = 0; i < 10; ++i)
Russell King9f25bc52015-11-03 14:51:13 +00001638 pl011_write(0xff, uap, REG_MIS);
Russell Kingb2a4e242015-11-03 14:51:03 +00001639 pl011_write(lcr_h, uap, uap->lcrh_tx);
Jon Medhurstb60f2f62013-12-10 10:18:59 +00001640 }
1641}
1642
Andre Przywara867b8e82015-05-21 17:26:15 +01001643static int pl011_allocate_irq(struct uart_amba_port *uap)
1644{
Russell King9f25bc52015-11-03 14:51:13 +00001645 pl011_write(uap->im, uap, REG_IMSC);
Andre Przywara867b8e82015-05-21 17:26:15 +01001646
1647 return request_irq(uap->port.irq, pl011_int, 0, "uart-pl011", uap);
1648}
1649
1650/*
1651 * Enable interrupts, only timeouts when using DMA
1652 * if initial RX DMA job failed, start in interrupt mode
1653 * as well.
1654 */
1655static void pl011_enable_interrupts(struct uart_amba_port *uap)
1656{
1657 spin_lock_irq(&uap->port.lock);
1658
1659 /* Clear out any spuriously appearing RX interrupts */
Russell King9f25bc52015-11-03 14:51:13 +00001660 pl011_write(UART011_RTIS | UART011_RXIS, uap, REG_ICR);
Andre Przywara867b8e82015-05-21 17:26:15 +01001661 uap->im = UART011_RTIM;
1662 if (!pl011_dma_rx_running(uap))
1663 uap->im |= UART011_RXIM;
Russell King9f25bc52015-11-03 14:51:13 +00001664 pl011_write(uap->im, uap, REG_IMSC);
Andre Przywara867b8e82015-05-21 17:26:15 +01001665 spin_unlock_irq(&uap->port.lock);
1666}
1667
Anton Vorontsovb3564c22012-09-24 14:27:54 -07001668static int pl011_startup(struct uart_port *port)
1669{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001670 struct uart_amba_port *uap =
1671 container_of(port, struct uart_amba_port, port);
Dave Martin734745c2015-03-04 12:27:33 +00001672 unsigned int cr;
Anton Vorontsovb3564c22012-09-24 14:27:54 -07001673 int retval;
1674
1675 retval = pl011_hwinit(port);
1676 if (retval)
1677 goto clk_dis;
1678
Andre Przywara867b8e82015-05-21 17:26:15 +01001679 retval = pl011_allocate_irq(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 if (retval)
1681 goto clk_dis;
1682
Russell King9f25bc52015-11-03 14:51:13 +00001683 pl011_write(uap->vendor->ifls, uap, REG_IFLS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684
Jon Medhurstfe433902013-12-10 10:18:58 +00001685 spin_lock_irq(&uap->port.lock);
1686
Shreshtha Kumar Sahud8d8ffa2012-01-18 15:53:59 +05301687 /* restore RTS and DTR */
1688 cr = uap->old_cr & (UART011_CR_RTS | UART011_CR_DTR);
1689 cr |= UART01x_CR_UARTEN | UART011_CR_RXE | UART011_CR_TXE;
Russell King9f25bc52015-11-03 14:51:13 +00001690 pl011_write(cr, uap, REG_CR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691
Jon Medhurstfe433902013-12-10 10:18:58 +00001692 spin_unlock_irq(&uap->port.lock);
1693
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 /*
1695 * initialise the old status of the modem signals
1696 */
Russell King9f25bc52015-11-03 14:51:13 +00001697 uap->old_status = pl011_read(uap, REG_FR) & UART01x_FR_MODEM_ANY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698
Russell King68b65f72010-12-22 17:24:39 +00001699 /* Startup DMA */
1700 pl011_dma_startup(uap);
1701
Andre Przywara867b8e82015-05-21 17:26:15 +01001702 pl011_enable_interrupts(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703
1704 return 0;
1705
1706 clk_dis:
Julia Lawall1c4c4392012-08-26 18:01:01 +02001707 clk_disable_unprepare(uap->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 return retval;
1709}
1710
Andre Przywara0dd1e242015-05-21 17:26:23 +01001711static int sbsa_uart_startup(struct uart_port *port)
1712{
1713 struct uart_amba_port *uap =
1714 container_of(port, struct uart_amba_port, port);
1715 int retval;
1716
1717 retval = pl011_hwinit(port);
1718 if (retval)
1719 return retval;
1720
1721 retval = pl011_allocate_irq(uap);
1722 if (retval)
1723 return retval;
1724
1725 /* The SBSA UART does not support any modem status lines. */
1726 uap->old_status = 0;
1727
1728 pl011_enable_interrupts(uap);
1729
1730 return 0;
1731}
1732
Linus Walleijec489aa2010-06-02 08:13:52 +01001733static void pl011_shutdown_channel(struct uart_amba_port *uap,
1734 unsigned int lcrh)
1735{
Greg Kroah-Hartmanf11c9842015-09-04 09:13:39 -07001736 unsigned long val;
Linus Walleijec489aa2010-06-02 08:13:52 +01001737
Russell Kingb2a4e242015-11-03 14:51:03 +00001738 val = pl011_read(uap, lcrh);
Greg Kroah-Hartmanf11c9842015-09-04 09:13:39 -07001739 val &= ~(UART01x_LCRH_BRK | UART01x_LCRH_FEN);
Russell Kingb2a4e242015-11-03 14:51:03 +00001740 pl011_write(val, uap, lcrh);
Linus Walleijec489aa2010-06-02 08:13:52 +01001741}
1742
Andre Przywara95166a32015-05-21 17:26:16 +01001743/*
1744 * disable the port. It should not disable RTS and DTR.
1745 * Also RTS and DTR state should be preserved to restore
1746 * it during startup().
1747 */
1748static void pl011_disable_uart(struct uart_amba_port *uap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749{
Shreshtha Kumar Sahud8d8ffa2012-01-18 15:53:59 +05301750 unsigned int cr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751
Rabin Vincent3b438162010-02-12 06:43:11 +01001752 uap->autorts = false;
Jon Medhurstfe433902013-12-10 10:18:58 +00001753 spin_lock_irq(&uap->port.lock);
Russell King9f25bc52015-11-03 14:51:13 +00001754 cr = pl011_read(uap, REG_CR);
Shreshtha Kumar Sahud8d8ffa2012-01-18 15:53:59 +05301755 uap->old_cr = cr;
1756 cr &= UART011_CR_RTS | UART011_CR_DTR;
1757 cr |= UART01x_CR_UARTEN | UART011_CR_TXE;
Russell King9f25bc52015-11-03 14:51:13 +00001758 pl011_write(cr, uap, REG_CR);
Jon Medhurstfe433902013-12-10 10:18:58 +00001759 spin_unlock_irq(&uap->port.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760
1761 /*
1762 * disable break condition and fifos
1763 */
Linus Walleijec489aa2010-06-02 08:13:52 +01001764 pl011_shutdown_channel(uap, uap->lcrh_rx);
Russell King7fe9a5a2015-11-03 14:51:08 +00001765 if (pl011_split_lcrh(uap))
Linus Walleijec489aa2010-06-02 08:13:52 +01001766 pl011_shutdown_channel(uap, uap->lcrh_tx);
Andre Przywara95166a32015-05-21 17:26:16 +01001767}
1768
1769static void pl011_disable_interrupts(struct uart_amba_port *uap)
1770{
1771 spin_lock_irq(&uap->port.lock);
1772
1773 /* mask all interrupts and clear all pending ones */
1774 uap->im = 0;
Russell King9f25bc52015-11-03 14:51:13 +00001775 pl011_write(uap->im, uap, REG_IMSC);
1776 pl011_write(0xffff, uap, REG_ICR);
Andre Przywara95166a32015-05-21 17:26:16 +01001777
1778 spin_unlock_irq(&uap->port.lock);
1779}
1780
1781static void pl011_shutdown(struct uart_port *port)
1782{
1783 struct uart_amba_port *uap =
1784 container_of(port, struct uart_amba_port, port);
1785
1786 pl011_disable_interrupts(uap);
1787
1788 pl011_dma_shutdown(uap);
1789
1790 free_irq(uap->port.irq, uap);
1791
1792 pl011_disable_uart(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793
1794 /*
1795 * Shut down the clock producer
1796 */
Julia Lawall1c4c4392012-08-26 18:01:01 +02001797 clk_disable_unprepare(uap->clk);
Linus Walleij78d80c52012-05-23 21:18:46 +02001798 /* Optionally let pins go into sleep states */
Linus Walleij2b996fc2013-06-05 15:36:42 +02001799 pinctrl_pm_select_sleep_state(port->dev);
Shreshtha Kumar Sahuc16d51a2011-06-13 10:11:33 +02001800
Jingoo Han574de552013-07-30 17:06:57 +09001801 if (dev_get_platdata(uap->port.dev)) {
Shreshtha Kumar Sahuc16d51a2011-06-13 10:11:33 +02001802 struct amba_pl011_data *plat;
1803
Jingoo Han574de552013-07-30 17:06:57 +09001804 plat = dev_get_platdata(uap->port.dev);
Shreshtha Kumar Sahuc16d51a2011-06-13 10:11:33 +02001805 if (plat->exit)
1806 plat->exit();
1807 }
1808
Peter Hurley36f339d2014-11-06 09:06:12 -05001809 if (uap->port.ops->flush_buffer)
1810 uap->port.ops->flush_buffer(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811}
1812
Andre Przywara0dd1e242015-05-21 17:26:23 +01001813static void sbsa_uart_shutdown(struct uart_port *port)
1814{
1815 struct uart_amba_port *uap =
1816 container_of(port, struct uart_amba_port, port);
1817
1818 pl011_disable_interrupts(uap);
1819
1820 free_irq(uap->port.irq, uap);
1821
1822 if (uap->port.ops->flush_buffer)
1823 uap->port.ops->flush_buffer(port);
1824}
1825
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826static void
Andre Przywaraef5a9352015-05-21 17:26:17 +01001827pl011_setup_status_masks(struct uart_port *port, struct ktermios *termios)
1828{
1829 port->read_status_mask = UART011_DR_OE | 255;
1830 if (termios->c_iflag & INPCK)
1831 port->read_status_mask |= UART011_DR_FE | UART011_DR_PE;
1832 if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
1833 port->read_status_mask |= UART011_DR_BE;
1834
1835 /*
1836 * Characters to ignore
1837 */
1838 port->ignore_status_mask = 0;
1839 if (termios->c_iflag & IGNPAR)
1840 port->ignore_status_mask |= UART011_DR_FE | UART011_DR_PE;
1841 if (termios->c_iflag & IGNBRK) {
1842 port->ignore_status_mask |= UART011_DR_BE;
1843 /*
1844 * If we're ignoring parity and break indicators,
1845 * ignore overruns too (for real raw support).
1846 */
1847 if (termios->c_iflag & IGNPAR)
1848 port->ignore_status_mask |= UART011_DR_OE;
1849 }
1850
1851 /*
1852 * Ignore all characters if CREAD is not set.
1853 */
1854 if ((termios->c_cflag & CREAD) == 0)
1855 port->ignore_status_mask |= UART_DUMMY_DR_RX;
1856}
1857
1858static void
Alan Cox606d0992006-12-08 02:38:45 -08001859pl011_set_termios(struct uart_port *port, struct ktermios *termios,
1860 struct ktermios *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001862 struct uart_amba_port *uap =
1863 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 unsigned int lcr_h, old_cr;
1865 unsigned long flags;
Russell Kingc19f12b2010-12-22 17:48:26 +00001866 unsigned int baud, quot, clkdiv;
1867
1868 if (uap->vendor->oversampling)
1869 clkdiv = 8;
1870 else
1871 clkdiv = 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872
1873 /*
1874 * Ask the core to calculate the divisor for us.
1875 */
Linus Walleijac3e3fb2010-06-02 20:40:22 +01001876 baud = uart_get_baud_rate(port, termios, old, 0,
Russell Kingc19f12b2010-12-22 17:48:26 +00001877 port->uartclk / clkdiv);
Chanho Min89fa28d2013-04-03 11:10:37 +09001878#ifdef CONFIG_DMA_ENGINE
Chanho Mincb06ff12013-03-27 18:38:11 +09001879 /*
1880 * Adjust RX DMA polling rate with baud rate if not specified.
1881 */
1882 if (uap->dmarx.auto_poll_rate)
1883 uap->dmarx.poll_rate = DIV_ROUND_UP(10000000, baud);
Chanho Min89fa28d2013-04-03 11:10:37 +09001884#endif
Linus Walleijac3e3fb2010-06-02 20:40:22 +01001885
1886 if (baud > port->uartclk/16)
1887 quot = DIV_ROUND_CLOSEST(port->uartclk * 8, baud);
1888 else
1889 quot = DIV_ROUND_CLOSEST(port->uartclk * 4, baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890
1891 switch (termios->c_cflag & CSIZE) {
1892 case CS5:
1893 lcr_h = UART01x_LCRH_WLEN_5;
1894 break;
1895 case CS6:
1896 lcr_h = UART01x_LCRH_WLEN_6;
1897 break;
1898 case CS7:
1899 lcr_h = UART01x_LCRH_WLEN_7;
1900 break;
1901 default: // CS8
1902 lcr_h = UART01x_LCRH_WLEN_8;
1903 break;
1904 }
1905 if (termios->c_cflag & CSTOPB)
1906 lcr_h |= UART01x_LCRH_STP2;
1907 if (termios->c_cflag & PARENB) {
1908 lcr_h |= UART01x_LCRH_PEN;
1909 if (!(termios->c_cflag & PARODD))
1910 lcr_h |= UART01x_LCRH_EPS;
1911 }
Russell Kingffca2b12010-12-22 17:13:05 +00001912 if (uap->fifosize > 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 lcr_h |= UART01x_LCRH_FEN;
1914
1915 spin_lock_irqsave(&port->lock, flags);
1916
1917 /*
1918 * Update the per-port timeout.
1919 */
1920 uart_update_timeout(port, termios->c_cflag, baud);
1921
Andre Przywaraef5a9352015-05-21 17:26:17 +01001922 pl011_setup_status_masks(port, termios);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923
1924 if (UART_ENABLE_MS(port, termios->c_cflag))
1925 pl011_enable_ms(port);
1926
1927 /* first, disable everything */
Russell King9f25bc52015-11-03 14:51:13 +00001928 old_cr = pl011_read(uap, REG_CR);
1929 pl011_write(0, uap, REG_CR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930
Rabin Vincent3b438162010-02-12 06:43:11 +01001931 if (termios->c_cflag & CRTSCTS) {
1932 if (old_cr & UART011_CR_RTS)
1933 old_cr |= UART011_CR_RTSEN;
1934
1935 old_cr |= UART011_CR_CTSEN;
1936 uap->autorts = true;
1937 } else {
1938 old_cr &= ~(UART011_CR_CTSEN | UART011_CR_RTSEN);
1939 uap->autorts = false;
1940 }
1941
Russell Kingc19f12b2010-12-22 17:48:26 +00001942 if (uap->vendor->oversampling) {
1943 if (baud > port->uartclk / 16)
Linus Walleijac3e3fb2010-06-02 20:40:22 +01001944 old_cr |= ST_UART011_CR_OVSFACT;
1945 else
1946 old_cr &= ~ST_UART011_CR_OVSFACT;
1947 }
1948
Linus Walleijc5dd5532012-09-26 17:21:36 +02001949 /*
1950 * Workaround for the ST Micro oversampling variants to
1951 * increase the bitrate slightly, by lowering the divisor,
1952 * to avoid delayed sampling of start bit at high speeds,
1953 * else we see data corruption.
1954 */
1955 if (uap->vendor->oversampling) {
1956 if ((baud >= 3000000) && (baud < 3250000) && (quot > 1))
1957 quot -= 1;
1958 else if ((baud > 3250000) && (quot > 2))
1959 quot -= 2;
1960 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 /* Set baud rate */
Russell King9f25bc52015-11-03 14:51:13 +00001962 pl011_write(quot & 0x3f, uap, REG_FBRD);
1963 pl011_write(quot >> 6, uap, REG_IBRD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964
1965 /*
1966 * ----------v----------v----------v----------v-----
Linus Walleijc5dd5532012-09-26 17:21:36 +02001967 * NOTE: lcrh_tx and lcrh_rx MUST BE WRITTEN AFTER
Russell King9f25bc52015-11-03 14:51:13 +00001968 * REG_FBRD & REG_IBRD.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 * ----------^----------^----------^----------^-----
1970 */
Jon Medhurstb60f2f62013-12-10 10:18:59 +00001971 pl011_write_lcr_h(uap, lcr_h);
Russell King9f25bc52015-11-03 14:51:13 +00001972 pl011_write(old_cr, uap, REG_CR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973
1974 spin_unlock_irqrestore(&port->lock, flags);
1975}
1976
Andre Przywara0dd1e242015-05-21 17:26:23 +01001977static void
1978sbsa_uart_set_termios(struct uart_port *port, struct ktermios *termios,
1979 struct ktermios *old)
1980{
1981 struct uart_amba_port *uap =
1982 container_of(port, struct uart_amba_port, port);
1983 unsigned long flags;
1984
1985 tty_termios_encode_baud_rate(termios, uap->fixed_baud, uap->fixed_baud);
1986
1987 /* The SBSA UART only supports 8n1 without hardware flow control. */
1988 termios->c_cflag &= ~(CSIZE | CSTOPB | PARENB | PARODD);
1989 termios->c_cflag &= ~(CMSPAR | CRTSCTS);
1990 termios->c_cflag |= CS8 | CLOCAL;
1991
1992 spin_lock_irqsave(&port->lock, flags);
1993 uart_update_timeout(port, CS8, uap->fixed_baud);
1994 pl011_setup_status_masks(port, termios);
1995 spin_unlock_irqrestore(&port->lock, flags);
1996}
1997
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998static const char *pl011_type(struct uart_port *port)
1999{
Daniel Thompsona5820c22014-09-03 12:51:55 +01002000 struct uart_amba_port *uap =
2001 container_of(port, struct uart_amba_port, port);
Russell Kinge8a7ba82010-12-28 09:16:54 +00002002 return uap->port.type == PORT_AMBA ? uap->type : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003}
2004
2005/*
2006 * Release the memory region(s) being used by 'port'
2007 */
Linus Walleije643f872012-06-17 15:44:19 +02002008static void pl011_release_port(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009{
2010 release_mem_region(port->mapbase, SZ_4K);
2011}
2012
2013/*
2014 * Request the memory region(s) being used by 'port'
2015 */
Linus Walleije643f872012-06-17 15:44:19 +02002016static int pl011_request_port(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017{
2018 return request_mem_region(port->mapbase, SZ_4K, "uart-pl011")
2019 != NULL ? 0 : -EBUSY;
2020}
2021
2022/*
2023 * Configure/autoconfigure the port.
2024 */
Linus Walleije643f872012-06-17 15:44:19 +02002025static void pl011_config_port(struct uart_port *port, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026{
2027 if (flags & UART_CONFIG_TYPE) {
2028 port->type = PORT_AMBA;
Linus Walleije643f872012-06-17 15:44:19 +02002029 pl011_request_port(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 }
2031}
2032
2033/*
2034 * verify the new serial_struct (for TIOCSSERIAL).
2035 */
Linus Walleije643f872012-06-17 15:44:19 +02002036static int pl011_verify_port(struct uart_port *port, struct serial_struct *ser)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037{
2038 int ret = 0;
2039 if (ser->type != PORT_UNKNOWN && ser->type != PORT_AMBA)
2040 ret = -EINVAL;
Yinghai Lua62c4132008-08-19 20:49:55 -07002041 if (ser->irq < 0 || ser->irq >= nr_irqs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 ret = -EINVAL;
2043 if (ser->baud_base < 9600)
2044 ret = -EINVAL;
2045 return ret;
2046}
2047
2048static struct uart_ops amba_pl011_pops = {
Linus Walleije643f872012-06-17 15:44:19 +02002049 .tx_empty = pl011_tx_empty,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 .set_mctrl = pl011_set_mctrl,
Linus Walleije643f872012-06-17 15:44:19 +02002051 .get_mctrl = pl011_get_mctrl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 .stop_tx = pl011_stop_tx,
2053 .start_tx = pl011_start_tx,
2054 .stop_rx = pl011_stop_rx,
2055 .enable_ms = pl011_enable_ms,
2056 .break_ctl = pl011_break_ctl,
2057 .startup = pl011_startup,
2058 .shutdown = pl011_shutdown,
Russell King68b65f72010-12-22 17:24:39 +00002059 .flush_buffer = pl011_dma_flush_buffer,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 .set_termios = pl011_set_termios,
2061 .type = pl011_type,
Linus Walleije643f872012-06-17 15:44:19 +02002062 .release_port = pl011_release_port,
2063 .request_port = pl011_request_port,
2064 .config_port = pl011_config_port,
2065 .verify_port = pl011_verify_port,
Jason Wessel84b5ae12008-02-20 13:33:39 -06002066#ifdef CONFIG_CONSOLE_POLL
Anton Vorontsovb3564c22012-09-24 14:27:54 -07002067 .poll_init = pl011_hwinit,
Linus Walleije643f872012-06-17 15:44:19 +02002068 .poll_get_char = pl011_get_poll_char,
2069 .poll_put_char = pl011_put_poll_char,
Jason Wessel84b5ae12008-02-20 13:33:39 -06002070#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071};
2072
Andre Przywara0dd1e242015-05-21 17:26:23 +01002073static void sbsa_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
2074{
2075}
2076
2077static unsigned int sbsa_uart_get_mctrl(struct uart_port *port)
2078{
2079 return 0;
2080}
2081
2082static const struct uart_ops sbsa_uart_pops = {
2083 .tx_empty = pl011_tx_empty,
2084 .set_mctrl = sbsa_uart_set_mctrl,
2085 .get_mctrl = sbsa_uart_get_mctrl,
2086 .stop_tx = pl011_stop_tx,
2087 .start_tx = pl011_start_tx,
2088 .stop_rx = pl011_stop_rx,
2089 .startup = sbsa_uart_startup,
2090 .shutdown = sbsa_uart_shutdown,
2091 .set_termios = sbsa_uart_set_termios,
2092 .type = pl011_type,
2093 .release_port = pl011_release_port,
2094 .request_port = pl011_request_port,
2095 .config_port = pl011_config_port,
2096 .verify_port = pl011_verify_port,
2097#ifdef CONFIG_CONSOLE_POLL
2098 .poll_init = pl011_hwinit,
2099 .poll_get_char = pl011_get_poll_char,
2100 .poll_put_char = pl011_put_poll_char,
2101#endif
2102};
2103
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104static struct uart_amba_port *amba_ports[UART_NR];
2105
2106#ifdef CONFIG_SERIAL_AMBA_PL011_CONSOLE
2107
Russell Kingd3587882006-03-20 20:00:09 +00002108static void pl011_console_putchar(struct uart_port *port, int ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109{
Daniel Thompsona5820c22014-09-03 12:51:55 +01002110 struct uart_amba_port *uap =
2111 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112
Russell King9f25bc52015-11-03 14:51:13 +00002113 while (pl011_read(uap, REG_FR) & UART01x_FR_TXFF)
Russell Kingd3587882006-03-20 20:00:09 +00002114 barrier();
Russell King9f25bc52015-11-03 14:51:13 +00002115 pl011_write(ch, uap, REG_DR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116}
2117
2118static void
2119pl011_console_write(struct console *co, const char *s, unsigned int count)
2120{
2121 struct uart_amba_port *uap = amba_ports[co->index];
Andre Przywara71eec482015-05-21 17:26:21 +01002122 unsigned int status, old_cr = 0, new_cr;
Rabin Vincentef605fd2012-01-17 11:52:28 +01002123 unsigned long flags;
2124 int locked = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125
2126 clk_enable(uap->clk);
2127
Rabin Vincentef605fd2012-01-17 11:52:28 +01002128 local_irq_save(flags);
2129 if (uap->port.sysrq)
2130 locked = 0;
2131 else if (oops_in_progress)
2132 locked = spin_trylock(&uap->port.lock);
2133 else
2134 spin_lock(&uap->port.lock);
2135
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 /*
2137 * First save the CR then disable the interrupts
2138 */
Andre Przywara71eec482015-05-21 17:26:21 +01002139 if (!uap->vendor->always_enabled) {
Russell King9f25bc52015-11-03 14:51:13 +00002140 old_cr = pl011_read(uap, REG_CR);
Andre Przywara71eec482015-05-21 17:26:21 +01002141 new_cr = old_cr & ~UART011_CR_CTSEN;
2142 new_cr |= UART01x_CR_UARTEN | UART011_CR_TXE;
Russell King9f25bc52015-11-03 14:51:13 +00002143 pl011_write(new_cr, uap, REG_CR);
Andre Przywara71eec482015-05-21 17:26:21 +01002144 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145
Russell Kingd3587882006-03-20 20:00:09 +00002146 uart_console_write(&uap->port, s, count, pl011_console_putchar);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147
2148 /*
2149 * Finally, wait for transmitter to become empty
2150 * and restore the TCR
2151 */
2152 do {
Russell King9f25bc52015-11-03 14:51:13 +00002153 status = pl011_read(uap, REG_FR);
Greg Kroah-Hartman062a68a2015-09-04 09:11:24 -07002154 } while (status & UART01x_FR_BUSY);
Andre Przywara71eec482015-05-21 17:26:21 +01002155 if (!uap->vendor->always_enabled)
Russell King9f25bc52015-11-03 14:51:13 +00002156 pl011_write(old_cr, uap, REG_CR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157
Rabin Vincentef605fd2012-01-17 11:52:28 +01002158 if (locked)
2159 spin_unlock(&uap->port.lock);
2160 local_irq_restore(flags);
2161
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 clk_disable(uap->clk);
2163}
2164
2165static void __init
2166pl011_console_get_options(struct uart_amba_port *uap, int *baud,
2167 int *parity, int *bits)
2168{
Russell King9f25bc52015-11-03 14:51:13 +00002169 if (pl011_read(uap, REG_CR) & UART01x_CR_UARTEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 unsigned int lcr_h, ibrd, fbrd;
2171
Russell Kingb2a4e242015-11-03 14:51:03 +00002172 lcr_h = pl011_read(uap, uap->lcrh_tx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173
2174 *parity = 'n';
2175 if (lcr_h & UART01x_LCRH_PEN) {
2176 if (lcr_h & UART01x_LCRH_EPS)
2177 *parity = 'e';
2178 else
2179 *parity = 'o';
2180 }
2181
2182 if ((lcr_h & 0x60) == UART01x_LCRH_WLEN_7)
2183 *bits = 7;
2184 else
2185 *bits = 8;
2186
Russell King9f25bc52015-11-03 14:51:13 +00002187 ibrd = pl011_read(uap, REG_IBRD);
2188 fbrd = pl011_read(uap, REG_FBRD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189
2190 *baud = uap->port.uartclk * 4 / (64 * ibrd + fbrd);
Linus Walleijac3e3fb2010-06-02 20:40:22 +01002191
Russell Kingc19f12b2010-12-22 17:48:26 +00002192 if (uap->vendor->oversampling) {
Russell King9f25bc52015-11-03 14:51:13 +00002193 if (pl011_read(uap, REG_CR)
Linus Walleijac3e3fb2010-06-02 20:40:22 +01002194 & ST_UART011_CR_OVSFACT)
2195 *baud *= 2;
2196 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 }
2198}
2199
2200static int __init pl011_console_setup(struct console *co, char *options)
2201{
2202 struct uart_amba_port *uap;
2203 int baud = 38400;
2204 int bits = 8;
2205 int parity = 'n';
2206 int flow = 'n';
Russell King4b4851c2011-09-22 11:35:30 +01002207 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208
2209 /*
2210 * Check whether an invalid uart number has been specified, and
2211 * if so, search for the first available port that does have
2212 * console support.
2213 */
2214 if (co->index >= UART_NR)
2215 co->index = 0;
2216 uap = amba_ports[co->index];
Russell Kingd28122a2007-01-22 18:59:42 +00002217 if (!uap)
2218 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219
Linus Walleij78d80c52012-05-23 21:18:46 +02002220 /* Allow pins to be muxed in and configured */
Linus Walleij2b996fc2013-06-05 15:36:42 +02002221 pinctrl_pm_select_default_state(uap->port.dev);
Linus Walleij78d80c52012-05-23 21:18:46 +02002222
Russell King4b4851c2011-09-22 11:35:30 +01002223 ret = clk_prepare(uap->clk);
2224 if (ret)
2225 return ret;
2226
Jingoo Han574de552013-07-30 17:06:57 +09002227 if (dev_get_platdata(uap->port.dev)) {
Shreshtha Kumar Sahuc16d51a2011-06-13 10:11:33 +02002228 struct amba_pl011_data *plat;
2229
Jingoo Han574de552013-07-30 17:06:57 +09002230 plat = dev_get_platdata(uap->port.dev);
Shreshtha Kumar Sahuc16d51a2011-06-13 10:11:33 +02002231 if (plat->init)
2232 plat->init();
2233 }
2234
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 uap->port.uartclk = clk_get_rate(uap->clk);
2236
Andre Przywaracefc2d12015-05-21 17:26:22 +01002237 if (uap->vendor->fixed_options) {
2238 baud = uap->fixed_baud;
2239 } else {
2240 if (options)
2241 uart_parse_options(options,
2242 &baud, &parity, &bits, &flow);
2243 else
2244 pl011_console_get_options(uap, &baud, &parity, &bits);
2245 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246
2247 return uart_set_options(&uap->port, co, baud, parity, bits, flow);
2248}
2249
Vincent Sanders2d934862005-09-14 22:36:03 +01002250static struct uart_driver amba_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251static struct console amba_console = {
2252 .name = "ttyAMA",
2253 .write = pl011_console_write,
2254 .device = uart_console_device,
2255 .setup = pl011_console_setup,
2256 .flags = CON_PRINTBUFFER,
2257 .index = -1,
2258 .data = &amba_reg,
2259};
2260
2261#define AMBA_CONSOLE (&amba_console)
Rob Herring0d3c6732014-04-18 17:19:57 -05002262
2263static void pl011_putc(struct uart_port *port, int c)
2264{
Russell King9f25bc52015-11-03 14:51:13 +00002265 while (readl(port->membase + REG_FR) & UART01x_FR_TXFF)
Rob Herring0d3c6732014-04-18 17:19:57 -05002266 ;
Russell King9f25bc52015-11-03 14:51:13 +00002267 writeb(c, port->membase + REG_DR);
2268 while (readl(port->membase + REG_FR) & UART01x_FR_BUSY)
Rob Herring0d3c6732014-04-18 17:19:57 -05002269 ;
2270}
2271
2272static void pl011_early_write(struct console *con, const char *s, unsigned n)
2273{
2274 struct earlycon_device *dev = con->data;
2275
2276 uart_console_write(&dev->port, s, n, pl011_putc);
2277}
2278
2279static int __init pl011_early_console_setup(struct earlycon_device *device,
2280 const char *opt)
2281{
2282 if (!device->port.membase)
2283 return -ENODEV;
2284
2285 device->con->write = pl011_early_write;
2286 return 0;
2287}
2288EARLYCON_DECLARE(pl011, pl011_early_console_setup);
Rob Herring45e0f0f2014-03-27 08:08:03 -05002289OF_EARLYCON_DECLARE(pl011, "arm,pl011", pl011_early_console_setup);
Rob Herring0d3c6732014-04-18 17:19:57 -05002290
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291#else
2292#define AMBA_CONSOLE NULL
2293#endif
2294
2295static struct uart_driver amba_reg = {
2296 .owner = THIS_MODULE,
2297 .driver_name = "ttyAMA",
2298 .dev_name = "ttyAMA",
2299 .major = SERIAL_AMBA_MAJOR,
2300 .minor = SERIAL_AMBA_MINOR,
2301 .nr = UART_NR,
2302 .cons = AMBA_CONSOLE,
2303};
2304
Matthew Leach32614aa2012-08-28 16:41:28 +01002305static int pl011_probe_dt_alias(int index, struct device *dev)
2306{
2307 struct device_node *np;
2308 static bool seen_dev_with_alias = false;
2309 static bool seen_dev_without_alias = false;
2310 int ret = index;
2311
2312 if (!IS_ENABLED(CONFIG_OF))
2313 return ret;
2314
2315 np = dev->of_node;
2316 if (!np)
2317 return ret;
2318
2319 ret = of_alias_get_id(np, "serial");
2320 if (IS_ERR_VALUE(ret)) {
2321 seen_dev_without_alias = true;
2322 ret = index;
2323 } else {
2324 seen_dev_with_alias = true;
2325 if (ret >= ARRAY_SIZE(amba_ports) || amba_ports[ret] != NULL) {
2326 dev_warn(dev, "requested serial port %d not available.\n", ret);
2327 ret = index;
2328 }
2329 }
2330
2331 if (seen_dev_with_alias && seen_dev_without_alias)
2332 dev_warn(dev, "aliased and non-aliased serial devices found in device tree. Serial port enumeration may be unpredictable.\n");
2333
2334 return ret;
2335}
2336
Andre Przywara49bb3c82015-05-21 17:26:14 +01002337/* unregisters the driver also if no more ports are left */
2338static void pl011_unregister_port(struct uart_amba_port *uap)
2339{
2340 int i;
2341 bool busy = false;
2342
2343 for (i = 0; i < ARRAY_SIZE(amba_ports); i++) {
2344 if (amba_ports[i] == uap)
2345 amba_ports[i] = NULL;
2346 else if (amba_ports[i])
2347 busy = true;
2348 }
2349 pl011_dma_remove(uap);
2350 if (!busy)
2351 uart_unregister_driver(&amba_reg);
2352}
2353
Andre Przywara3873e2d2015-05-21 17:26:18 +01002354static int pl011_find_free_port(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355{
Andre Przywara3873e2d2015-05-21 17:26:18 +01002356 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357
2358 for (i = 0; i < ARRAY_SIZE(amba_ports); i++)
2359 if (amba_ports[i] == NULL)
Andre Przywara3873e2d2015-05-21 17:26:18 +01002360 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361
Andre Przywara3873e2d2015-05-21 17:26:18 +01002362 return -EBUSY;
2363}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364
Andre Przywara3873e2d2015-05-21 17:26:18 +01002365static int pl011_setup_port(struct device *dev, struct uart_amba_port *uap,
2366 struct resource *mmiobase, int index)
2367{
2368 void __iomem *base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369
Andre Przywara3873e2d2015-05-21 17:26:18 +01002370 base = devm_ioremap_resource(dev, mmiobase);
Krzysztof Kozlowski97a60ea2015-07-09 22:21:41 +09002371 if (IS_ERR(base))
2372 return PTR_ERR(base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373
Andre Przywara3873e2d2015-05-21 17:26:18 +01002374 index = pl011_probe_dt_alias(index, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375
Shreshtha Kumar Sahud8d8ffa2012-01-18 15:53:59 +05302376 uap->old_cr = 0;
Andre Przywara3873e2d2015-05-21 17:26:18 +01002377 uap->port.dev = dev;
2378 uap->port.mapbase = mmiobase->start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 uap->port.membase = base;
2380 uap->port.iotype = UPIO_MEM;
Russell Kingffca2b12010-12-22 17:13:05 +00002381 uap->port.fifosize = uap->fifosize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 uap->port.flags = UPF_BOOT_AUTOCONF;
Andre Przywara3873e2d2015-05-21 17:26:18 +01002383 uap->port.line = index;
2384
2385 amba_ports[index] = uap;
2386
2387 return 0;
2388}
2389
2390static int pl011_register_port(struct uart_amba_port *uap)
2391{
2392 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393
Linus Walleijc3d8b762012-03-21 20:15:18 +01002394 /* Ensure interrupts from this UART are masked and cleared */
Russell King9f25bc52015-11-03 14:51:13 +00002395 pl011_write(0, uap, REG_IMSC);
2396 pl011_write(0xffff, uap, REG_ICR);
Linus Walleijc3d8b762012-03-21 20:15:18 +01002397
Tushar Beheraef2889f2014-01-20 14:32:35 +05302398 if (!amba_reg.state) {
2399 ret = uart_register_driver(&amba_reg);
2400 if (ret < 0) {
Andre Przywara3873e2d2015-05-21 17:26:18 +01002401 dev_err(uap->port.dev,
Jorge Ramirez-Ortiz1c9be312015-03-06 13:05:40 -05002402 "Failed to register AMBA-PL011 driver\n");
Tushar Beheraef2889f2014-01-20 14:32:35 +05302403 return ret;
2404 }
2405 }
2406
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407 ret = uart_add_one_port(&amba_reg, &uap->port);
Andre Przywara49bb3c82015-05-21 17:26:14 +01002408 if (ret)
2409 pl011_unregister_port(uap);
Tushar Behera7f6d9422014-06-26 15:35:35 +05302410
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 return ret;
2412}
2413
Andre Przywara3873e2d2015-05-21 17:26:18 +01002414static int pl011_probe(struct amba_device *dev, const struct amba_id *id)
2415{
2416 struct uart_amba_port *uap;
2417 struct vendor_data *vendor = id->data;
2418 int portnr, ret;
2419
2420 portnr = pl011_find_free_port();
2421 if (portnr < 0)
2422 return portnr;
2423
2424 uap = devm_kzalloc(&dev->dev, sizeof(struct uart_amba_port),
2425 GFP_KERNEL);
2426 if (!uap)
2427 return -ENOMEM;
2428
2429 uap->clk = devm_clk_get(&dev->dev, NULL);
2430 if (IS_ERR(uap->clk))
2431 return PTR_ERR(uap->clk);
2432
Russell King439403b2015-11-16 17:40:31 +00002433 uap->reg_offset = vendor->reg_offset;
Andre Przywara3873e2d2015-05-21 17:26:18 +01002434 uap->vendor = vendor;
2435 uap->lcrh_rx = vendor->lcrh_rx;
2436 uap->lcrh_tx = vendor->lcrh_tx;
2437 uap->fifosize = vendor->get_fifosize(dev);
2438 uap->port.irq = dev->irq[0];
2439 uap->port.ops = &amba_pl011_pops;
2440
2441 snprintf(uap->type, sizeof(uap->type), "PL011 rev%u", amba_rev(dev));
2442
2443 ret = pl011_setup_port(&dev->dev, uap, &dev->res, portnr);
2444 if (ret)
2445 return ret;
2446
2447 amba_set_drvdata(dev, uap);
2448
2449 return pl011_register_port(uap);
2450}
2451
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452static int pl011_remove(struct amba_device *dev)
2453{
2454 struct uart_amba_port *uap = amba_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456 uart_remove_one_port(&amba_reg, &uap->port);
Andre Przywara49bb3c82015-05-21 17:26:14 +01002457 pl011_unregister_port(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 return 0;
2459}
2460
Ulf Hanssond0ce8502013-12-03 11:04:28 +01002461#ifdef CONFIG_PM_SLEEP
2462static int pl011_suspend(struct device *dev)
Leo Chenb736b892009-07-28 23:43:33 +01002463{
Ulf Hanssond0ce8502013-12-03 11:04:28 +01002464 struct uart_amba_port *uap = dev_get_drvdata(dev);
Leo Chenb736b892009-07-28 23:43:33 +01002465
2466 if (!uap)
2467 return -EINVAL;
2468
2469 return uart_suspend_port(&amba_reg, &uap->port);
2470}
2471
Ulf Hanssond0ce8502013-12-03 11:04:28 +01002472static int pl011_resume(struct device *dev)
Leo Chenb736b892009-07-28 23:43:33 +01002473{
Ulf Hanssond0ce8502013-12-03 11:04:28 +01002474 struct uart_amba_port *uap = dev_get_drvdata(dev);
Leo Chenb736b892009-07-28 23:43:33 +01002475
2476 if (!uap)
2477 return -EINVAL;
2478
2479 return uart_resume_port(&amba_reg, &uap->port);
2480}
2481#endif
2482
Ulf Hanssond0ce8502013-12-03 11:04:28 +01002483static SIMPLE_DEV_PM_OPS(pl011_dev_pm_ops, pl011_suspend, pl011_resume);
2484
Andre Przywara0dd1e242015-05-21 17:26:23 +01002485static int sbsa_uart_probe(struct platform_device *pdev)
2486{
2487 struct uart_amba_port *uap;
2488 struct resource *r;
2489 int portnr, ret;
2490 int baudrate;
2491
2492 /*
2493 * Check the mandatory baud rate parameter in the DT node early
2494 * so that we can easily exit with the error.
2495 */
2496 if (pdev->dev.of_node) {
2497 struct device_node *np = pdev->dev.of_node;
2498
2499 ret = of_property_read_u32(np, "current-speed", &baudrate);
2500 if (ret)
2501 return ret;
2502 } else {
2503 baudrate = 115200;
2504 }
2505
2506 portnr = pl011_find_free_port();
2507 if (portnr < 0)
2508 return portnr;
2509
2510 uap = devm_kzalloc(&pdev->dev, sizeof(struct uart_amba_port),
2511 GFP_KERNEL);
2512 if (!uap)
2513 return -ENOMEM;
2514
Russell King439403b2015-11-16 17:40:31 +00002515 uap->reg_offset = vendor_sbsa.reg_offset;
Andre Przywara0dd1e242015-05-21 17:26:23 +01002516 uap->vendor = &vendor_sbsa;
2517 uap->fifosize = 32;
2518 uap->port.irq = platform_get_irq(pdev, 0);
2519 uap->port.ops = &sbsa_uart_pops;
2520 uap->fixed_baud = baudrate;
2521
2522 snprintf(uap->type, sizeof(uap->type), "SBSA");
2523
2524 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2525
2526 ret = pl011_setup_port(&pdev->dev, uap, r, portnr);
2527 if (ret)
2528 return ret;
2529
2530 platform_set_drvdata(pdev, uap);
2531
2532 return pl011_register_port(uap);
2533}
2534
2535static int sbsa_uart_remove(struct platform_device *pdev)
2536{
2537 struct uart_amba_port *uap = platform_get_drvdata(pdev);
2538
2539 uart_remove_one_port(&amba_reg, &uap->port);
2540 pl011_unregister_port(uap);
2541 return 0;
2542}
2543
2544static const struct of_device_id sbsa_uart_of_match[] = {
2545 { .compatible = "arm,sbsa-uart", },
2546 {},
2547};
2548MODULE_DEVICE_TABLE(of, sbsa_uart_of_match);
2549
Graeme Gregory3db9ab02015-05-21 17:26:24 +01002550static const struct acpi_device_id sbsa_uart_acpi_match[] = {
2551 { "ARMH0011", 0 },
2552 {},
2553};
2554MODULE_DEVICE_TABLE(acpi, sbsa_uart_acpi_match);
2555
Andre Przywara0dd1e242015-05-21 17:26:23 +01002556static struct platform_driver arm_sbsa_uart_platform_driver = {
2557 .probe = sbsa_uart_probe,
2558 .remove = sbsa_uart_remove,
2559 .driver = {
2560 .name = "sbsa-uart",
2561 .of_match_table = of_match_ptr(sbsa_uart_of_match),
Graeme Gregory3db9ab02015-05-21 17:26:24 +01002562 .acpi_match_table = ACPI_PTR(sbsa_uart_acpi_match),
Andre Przywara0dd1e242015-05-21 17:26:23 +01002563 },
2564};
2565
Russell King2c39c9e2010-07-27 08:50:16 +01002566static struct amba_id pl011_ids[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 {
2568 .id = 0x00041011,
2569 .mask = 0x000fffff,
Alessandro Rubini5926a292009-06-04 17:43:04 +01002570 .data = &vendor_arm,
2571 },
2572 {
2573 .id = 0x00380802,
2574 .mask = 0x00ffffff,
2575 .data = &vendor_st,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576 },
2577 { 0, 0 },
2578};
2579
Dave Martin60f7a332011-10-05 15:15:22 +01002580MODULE_DEVICE_TABLE(amba, pl011_ids);
2581
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582static struct amba_driver pl011_driver = {
2583 .drv = {
2584 .name = "uart-pl011",
Ulf Hanssond0ce8502013-12-03 11:04:28 +01002585 .pm = &pl011_dev_pm_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586 },
2587 .id_table = pl011_ids,
2588 .probe = pl011_probe,
2589 .remove = pl011_remove,
2590};
2591
2592static int __init pl011_init(void)
2593{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594 printk(KERN_INFO "Serial: AMBA PL011 UART driver\n");
2595
Andre Przywara0dd1e242015-05-21 17:26:23 +01002596 if (platform_driver_register(&arm_sbsa_uart_platform_driver))
2597 pr_warn("could not register SBSA UART platform driver\n");
Greg Kroah-Hartman062a68a2015-09-04 09:11:24 -07002598 return amba_driver_register(&pl011_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599}
2600
2601static void __exit pl011_exit(void)
2602{
Andre Przywara0dd1e242015-05-21 17:26:23 +01002603 platform_driver_unregister(&arm_sbsa_uart_platform_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 amba_driver_unregister(&pl011_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605}
2606
Alessandro Rubini4dd9e742009-05-05 05:54:13 +01002607/*
2608 * While this can be a module, if builtin it's most likely the console
2609 * So let's leave module_exit but move module_init to an earlier place
2610 */
2611arch_initcall(pl011_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612module_exit(pl011_exit);
2613
2614MODULE_AUTHOR("ARM Ltd/Deep Blue Solutions Ltd");
2615MODULE_DESCRIPTION("ARM AMBA serial port driver");
2616MODULE_LICENSE("GPL");