blob: bd01e75128dc6e4d29725124d7e608315c10ee77 [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
63#define UART_NR 14
64
65#define SERIAL_AMBA_MAJOR 204
66#define SERIAL_AMBA_MINOR 64
67#define SERIAL_AMBA_NR UART_NR
68
69#define AMBA_ISR_PASS_LIMIT 256
70
Russell Kingb63d4f02005-11-19 11:10:35 +000071#define UART_DR_ERROR (UART011_DR_OE|UART011_DR_BE|UART011_DR_PE|UART011_DR_FE)
72#define UART_DUMMY_DR_RX (1 << 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
Alessandro Rubini5926a292009-06-04 17:43:04 +010074/* There is by now at least one vendor with differing details, so handle it */
75struct vendor_data {
76 unsigned int ifls;
Linus Walleijec489aa2010-06-02 08:13:52 +010077 unsigned int lcrh_tx;
78 unsigned int lcrh_rx;
Linus Walleijac3e3fb2010-06-02 20:40:22 +010079 bool oversampling;
Russell King38d62432010-12-22 17:59:16 +000080 bool dma_threshold;
Rajanikanth H.V4fd06902012-03-26 11:17:02 +020081 bool cts_event_workaround;
Andre Przywara71eec482015-05-21 17:26:21 +010082 bool always_enabled;
Andre Przywaracefc2d12015-05-21 17:26:22 +010083 bool fixed_options;
Jongsung Kim78506f22013-04-15 14:45:25 +090084
Jongsung Kimea336402013-05-10 18:05:35 +090085 unsigned int (*get_fifosize)(struct amba_device *dev);
Alessandro Rubini5926a292009-06-04 17:43:04 +010086};
87
Jongsung Kimea336402013-05-10 18:05:35 +090088static unsigned int get_fifosize_arm(struct amba_device *dev)
Jongsung Kim78506f22013-04-15 14:45:25 +090089{
Jongsung Kimea336402013-05-10 18:05:35 +090090 return amba_rev(dev) < 3 ? 16 : 32;
Jongsung Kim78506f22013-04-15 14:45:25 +090091}
92
Alessandro Rubini5926a292009-06-04 17:43:04 +010093static struct vendor_data vendor_arm = {
94 .ifls = UART011_IFLS_RX4_8|UART011_IFLS_TX4_8,
Greg Kroah-Hartman0de6cfb2015-09-04 09:13:56 -070095 .lcrh_tx = UART011_LCRH,
96 .lcrh_rx = UART011_LCRH,
Linus Walleijac3e3fb2010-06-02 20:40:22 +010097 .oversampling = false,
Russell King38d62432010-12-22 17:59:16 +000098 .dma_threshold = false,
Rajanikanth H.V4fd06902012-03-26 11:17:02 +020099 .cts_event_workaround = false,
Andre Przywara71eec482015-05-21 17:26:21 +0100100 .always_enabled = false,
Andre Przywaracefc2d12015-05-21 17:26:22 +0100101 .fixed_options = false,
Jongsung Kim78506f22013-04-15 14:45:25 +0900102 .get_fifosize = get_fifosize_arm,
Alessandro Rubini5926a292009-06-04 17:43:04 +0100103};
104
Andre Przywara0dd1e242015-05-21 17:26:23 +0100105static struct vendor_data vendor_sbsa = {
106 .oversampling = false,
107 .dma_threshold = false,
108 .cts_event_workaround = false,
109 .always_enabled = true,
110 .fixed_options = true,
111};
112
Jongsung Kimea336402013-05-10 18:05:35 +0900113static unsigned int get_fifosize_st(struct amba_device *dev)
Jongsung Kim78506f22013-04-15 14:45:25 +0900114{
115 return 64;
116}
117
Alessandro Rubini5926a292009-06-04 17:43:04 +0100118static struct vendor_data vendor_st = {
119 .ifls = UART011_IFLS_RX_HALF|UART011_IFLS_TX_HALF,
Greg Kroah-Hartman0de6cfb2015-09-04 09:13:56 -0700120 .lcrh_tx = ST_UART011_LCRH_TX,
121 .lcrh_rx = ST_UART011_LCRH_RX,
Linus Walleijac3e3fb2010-06-02 20:40:22 +0100122 .oversampling = true,
Russell King38d62432010-12-22 17:59:16 +0000123 .dma_threshold = true,
Rajanikanth H.V4fd06902012-03-26 11:17:02 +0200124 .cts_event_workaround = true,
Andre Przywara71eec482015-05-21 17:26:21 +0100125 .always_enabled = false,
Andre Przywaracefc2d12015-05-21 17:26:22 +0100126 .fixed_options = false,
Jongsung Kim78506f22013-04-15 14:45:25 +0900127 .get_fifosize = get_fifosize_st,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128};
129
Russell King68b65f72010-12-22 17:24:39 +0000130/* Deals with DMA transactions */
Linus Walleijead76f322011-02-24 13:21:08 +0100131
132struct pl011_sgbuf {
133 struct scatterlist sg;
134 char *buf;
135};
136
137struct pl011_dmarx_data {
138 struct dma_chan *chan;
139 struct completion complete;
140 bool use_buf_b;
141 struct pl011_sgbuf sgbuf_a;
142 struct pl011_sgbuf sgbuf_b;
143 dma_cookie_t cookie;
144 bool running;
Chanho Mincb06ff12013-03-27 18:38:11 +0900145 struct timer_list timer;
146 unsigned int last_residue;
147 unsigned long last_jiffies;
148 bool auto_poll_rate;
149 unsigned int poll_rate;
150 unsigned int poll_timeout;
Linus Walleijead76f322011-02-24 13:21:08 +0100151};
152
Russell King68b65f72010-12-22 17:24:39 +0000153struct pl011_dmatx_data {
154 struct dma_chan *chan;
155 struct scatterlist sg;
156 char *buf;
157 bool queued;
158};
159
Russell Kingc19f12b2010-12-22 17:48:26 +0000160/*
161 * We wrap our port structure around the generic uart_port.
162 */
163struct uart_amba_port {
164 struct uart_port port;
165 struct clk *clk;
166 const struct vendor_data *vendor;
Russell King68b65f72010-12-22 17:24:39 +0000167 unsigned int dmacr; /* dma control reg */
Russell Kingc19f12b2010-12-22 17:48:26 +0000168 unsigned int im; /* interrupt mask */
169 unsigned int old_status;
Russell Kingffca2b12010-12-22 17:13:05 +0000170 unsigned int fifosize; /* vendor-specific */
Russell Kingc19f12b2010-12-22 17:48:26 +0000171 unsigned int lcrh_tx; /* vendor-specific */
172 unsigned int lcrh_rx; /* vendor-specific */
Shreshtha Kumar Sahud8d8ffa2012-01-18 15:53:59 +0530173 unsigned int old_cr; /* state during shutdown */
Russell Kingc19f12b2010-12-22 17:48:26 +0000174 bool autorts;
Andre Przywaracefc2d12015-05-21 17:26:22 +0100175 unsigned int fixed_baud; /* vendor-set fixed baud rate */
Russell Kingc19f12b2010-12-22 17:48:26 +0000176 char type[12];
Russell King68b65f72010-12-22 17:24:39 +0000177#ifdef CONFIG_DMA_ENGINE
178 /* DMA stuff */
Linus Walleijead76f322011-02-24 13:21:08 +0100179 bool using_tx_dma;
180 bool using_rx_dma;
181 struct pl011_dmarx_data dmarx;
Russell King68b65f72010-12-22 17:24:39 +0000182 struct pl011_dmatx_data dmatx;
Jorge Ramirez-Ortiz1c9be312015-03-06 13:05:40 -0500183 bool dma_probed;
Russell King68b65f72010-12-22 17:24:39 +0000184#endif
Russell Kingc19f12b2010-12-22 17:48:26 +0000185};
186
Russell King75836332015-11-03 14:50:58 +0000187static unsigned int pl011_read(void __iomem *base, unsigned int reg)
188{
189 return readw(base + reg);
190}
191
192static void pl011_write(unsigned int val, void __iomem *base, unsigned int reg)
193{
194 writew(val, base + reg);
195}
196
Russell King68b65f72010-12-22 17:24:39 +0000197/*
Linus Walleij29772c42011-02-24 13:21:36 +0100198 * Reads up to 256 characters from the FIFO or until it's empty and
199 * inserts them into the TTY layer. Returns the number of characters
200 * read from the FIFO.
201 */
202static int pl011_fifo_to_tty(struct uart_amba_port *uap)
203{
Timur Tabi71a5cd82015-10-07 15:27:16 -0500204 u16 status;
205 unsigned int ch, flag, max_count = 256;
Linus Walleij29772c42011-02-24 13:21:36 +0100206 int fifotaken = 0;
207
208 while (max_count--) {
Russell King75836332015-11-03 14:50:58 +0000209 status = pl011_read(uap->port.membase, UART01x_FR);
Linus Walleij29772c42011-02-24 13:21:36 +0100210 if (status & UART01x_FR_RXFE)
211 break;
212
213 /* Take chars from the FIFO and update status */
Russell King75836332015-11-03 14:50:58 +0000214 ch = pl011_read(uap->port.membase, UART01x_DR) |
Linus Walleij29772c42011-02-24 13:21:36 +0100215 UART_DUMMY_DR_RX;
216 flag = TTY_NORMAL;
217 uap->port.icount.rx++;
218 fifotaken++;
219
220 if (unlikely(ch & UART_DR_ERROR)) {
221 if (ch & UART011_DR_BE) {
222 ch &= ~(UART011_DR_FE | UART011_DR_PE);
223 uap->port.icount.brk++;
224 if (uart_handle_break(&uap->port))
225 continue;
226 } else if (ch & UART011_DR_PE)
227 uap->port.icount.parity++;
228 else if (ch & UART011_DR_FE)
229 uap->port.icount.frame++;
230 if (ch & UART011_DR_OE)
231 uap->port.icount.overrun++;
232
233 ch &= uap->port.read_status_mask;
234
235 if (ch & UART011_DR_BE)
236 flag = TTY_BREAK;
237 else if (ch & UART011_DR_PE)
238 flag = TTY_PARITY;
239 else if (ch & UART011_DR_FE)
240 flag = TTY_FRAME;
241 }
242
243 if (uart_handle_sysrq_char(&uap->port, ch & 255))
244 continue;
245
246 uart_insert_char(&uap->port, ch, UART011_DR_OE, ch, flag);
247 }
248
249 return fifotaken;
250}
251
252
253/*
Russell King68b65f72010-12-22 17:24:39 +0000254 * All the DMA operation mode stuff goes inside this ifdef.
255 * This assumes that you have a generic DMA device interface,
256 * no custom DMA interfaces are supported.
257 */
258#ifdef CONFIG_DMA_ENGINE
259
260#define PL011_DMA_BUFFER_SIZE PAGE_SIZE
261
Linus Walleijead76f322011-02-24 13:21:08 +0100262static int pl011_sgbuf_init(struct dma_chan *chan, struct pl011_sgbuf *sg,
263 enum dma_data_direction dir)
264{
Chanho Mincb06ff12013-03-27 18:38:11 +0900265 dma_addr_t dma_addr;
266
267 sg->buf = dma_alloc_coherent(chan->device->dev,
268 PL011_DMA_BUFFER_SIZE, &dma_addr, GFP_KERNEL);
Linus Walleijead76f322011-02-24 13:21:08 +0100269 if (!sg->buf)
270 return -ENOMEM;
271
Chanho Mincb06ff12013-03-27 18:38:11 +0900272 sg_init_table(&sg->sg, 1);
273 sg_set_page(&sg->sg, phys_to_page(dma_addr),
274 PL011_DMA_BUFFER_SIZE, offset_in_page(dma_addr));
275 sg_dma_address(&sg->sg) = dma_addr;
Andrew Jacksonc64be922014-11-07 14:14:43 +0000276 sg_dma_len(&sg->sg) = PL011_DMA_BUFFER_SIZE;
Linus Walleijead76f322011-02-24 13:21:08 +0100277
Linus Walleijead76f322011-02-24 13:21:08 +0100278 return 0;
279}
280
281static void pl011_sgbuf_free(struct dma_chan *chan, struct pl011_sgbuf *sg,
282 enum dma_data_direction dir)
283{
284 if (sg->buf) {
Chanho Mincb06ff12013-03-27 18:38:11 +0900285 dma_free_coherent(chan->device->dev,
286 PL011_DMA_BUFFER_SIZE, sg->buf,
287 sg_dma_address(&sg->sg));
Linus Walleijead76f322011-02-24 13:21:08 +0100288 }
289}
290
Jorge Ramirez-Ortiz1c9be312015-03-06 13:05:40 -0500291static void pl011_dma_probe(struct uart_amba_port *uap)
Russell King68b65f72010-12-22 17:24:39 +0000292{
293 /* DMA is the sole user of the platform data right now */
Jingoo Han574de552013-07-30 17:06:57 +0900294 struct amba_pl011_data *plat = dev_get_platdata(uap->port.dev);
Jorge Ramirez-Ortiz1c9be312015-03-06 13:05:40 -0500295 struct device *dev = uap->port.dev;
Russell King68b65f72010-12-22 17:24:39 +0000296 struct dma_slave_config tx_conf = {
Greg Kroah-Hartman0de6cfb2015-09-04 09:13:56 -0700297 .dst_addr = uap->port.mapbase + UART01x_DR,
Russell King68b65f72010-12-22 17:24:39 +0000298 .dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE,
Vinod Koula485df42011-10-14 10:47:38 +0530299 .direction = DMA_MEM_TO_DEV,
Russell King68b65f72010-12-22 17:24:39 +0000300 .dst_maxburst = uap->fifosize >> 1,
Viresh Kumar258aea72012-02-01 16:12:19 +0530301 .device_fc = false,
Russell King68b65f72010-12-22 17:24:39 +0000302 };
303 struct dma_chan *chan;
304 dma_cap_mask_t mask;
305
Jorge Ramirez-Ortiz1c9be312015-03-06 13:05:40 -0500306 uap->dma_probed = true;
307 chan = dma_request_slave_channel_reason(dev, "tx");
308 if (IS_ERR(chan)) {
309 if (PTR_ERR(chan) == -EPROBE_DEFER) {
Jorge Ramirez-Ortiz1c9be312015-03-06 13:05:40 -0500310 uap->dma_probed = false;
311 return;
312 }
Russell King68b65f72010-12-22 17:24:39 +0000313
Arnd Bergmann787b0c12013-01-28 16:24:37 +0000314 /* We need platform data */
315 if (!plat || !plat->dma_filter) {
316 dev_info(uap->port.dev, "no DMA platform data\n");
317 return;
318 }
319
320 /* Try to acquire a generic DMA engine slave TX channel */
321 dma_cap_zero(mask);
322 dma_cap_set(DMA_SLAVE, mask);
323
324 chan = dma_request_channel(mask, plat->dma_filter,
325 plat->dma_tx_param);
326 if (!chan) {
327 dev_err(uap->port.dev, "no TX DMA channel!\n");
328 return;
329 }
Russell King68b65f72010-12-22 17:24:39 +0000330 }
331
332 dmaengine_slave_config(chan, &tx_conf);
333 uap->dmatx.chan = chan;
334
335 dev_info(uap->port.dev, "DMA channel TX %s\n",
336 dma_chan_name(uap->dmatx.chan));
Linus Walleijead76f322011-02-24 13:21:08 +0100337
338 /* Optionally make use of an RX channel as well */
Arnd Bergmann787b0c12013-01-28 16:24:37 +0000339 chan = dma_request_slave_channel(dev, "rx");
Rob Herring0d3c6732014-04-18 17:19:57 -0500340
Arnd Bergmann787b0c12013-01-28 16:24:37 +0000341 if (!chan && plat->dma_rx_param) {
342 chan = dma_request_channel(mask, plat->dma_filter, plat->dma_rx_param);
343
344 if (!chan) {
345 dev_err(uap->port.dev, "no RX DMA channel!\n");
346 return;
347 }
348 }
349
350 if (chan) {
Linus Walleijead76f322011-02-24 13:21:08 +0100351 struct dma_slave_config rx_conf = {
Greg Kroah-Hartman0de6cfb2015-09-04 09:13:56 -0700352 .src_addr = uap->port.mapbase + UART01x_DR,
Linus Walleijead76f322011-02-24 13:21:08 +0100353 .src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE,
Vinod Koula485df42011-10-14 10:47:38 +0530354 .direction = DMA_DEV_TO_MEM,
Guennadi Liakhovetskib2aeb772014-04-12 19:47:17 +0200355 .src_maxburst = uap->fifosize >> 2,
Viresh Kumar258aea72012-02-01 16:12:19 +0530356 .device_fc = false,
Linus Walleijead76f322011-02-24 13:21:08 +0100357 };
Andrew Jackson2d3b7d62014-11-07 14:14:47 +0000358 struct dma_slave_caps caps;
Linus Walleijead76f322011-02-24 13:21:08 +0100359
Andrew Jackson2d3b7d62014-11-07 14:14:47 +0000360 /*
361 * Some DMA controllers provide information on their capabilities.
362 * If the controller does, check for suitable residue processing
363 * otherwise assime all is well.
364 */
365 if (0 == dma_get_slave_caps(chan, &caps)) {
366 if (caps.residue_granularity ==
367 DMA_RESIDUE_GRANULARITY_DESCRIPTOR) {
368 dma_release_channel(chan);
369 dev_info(uap->port.dev,
370 "RX DMA disabled - no residue processing\n");
371 return;
372 }
373 }
Linus Walleijead76f322011-02-24 13:21:08 +0100374 dmaengine_slave_config(chan, &rx_conf);
375 uap->dmarx.chan = chan;
376
Andrew Jackson98267d32014-11-07 14:14:23 +0000377 uap->dmarx.auto_poll_rate = false;
Greg Kroah-Hartman8f898bf2013-12-17 09:33:18 -0800378 if (plat && plat->dma_rx_poll_enable) {
Chanho Mincb06ff12013-03-27 18:38:11 +0900379 /* Set poll rate if specified. */
380 if (plat->dma_rx_poll_rate) {
381 uap->dmarx.auto_poll_rate = false;
382 uap->dmarx.poll_rate = plat->dma_rx_poll_rate;
383 } else {
384 /*
385 * 100 ms defaults to poll rate if not
386 * specified. This will be adjusted with
387 * the baud rate at set_termios.
388 */
389 uap->dmarx.auto_poll_rate = true;
390 uap->dmarx.poll_rate = 100;
391 }
392 /* 3 secs defaults poll_timeout if not specified. */
393 if (plat->dma_rx_poll_timeout)
394 uap->dmarx.poll_timeout =
395 plat->dma_rx_poll_timeout;
396 else
397 uap->dmarx.poll_timeout = 3000;
Andrew Jackson98267d32014-11-07 14:14:23 +0000398 } else if (!plat && dev->of_node) {
399 uap->dmarx.auto_poll_rate = of_property_read_bool(
400 dev->of_node, "auto-poll");
401 if (uap->dmarx.auto_poll_rate) {
402 u32 x;
Chanho Mincb06ff12013-03-27 18:38:11 +0900403
Andrew Jackson98267d32014-11-07 14:14:23 +0000404 if (0 == of_property_read_u32(dev->of_node,
405 "poll-rate-ms", &x))
406 uap->dmarx.poll_rate = x;
407 else
408 uap->dmarx.poll_rate = 100;
409 if (0 == of_property_read_u32(dev->of_node,
410 "poll-timeout-ms", &x))
411 uap->dmarx.poll_timeout = x;
412 else
413 uap->dmarx.poll_timeout = 3000;
414 }
415 }
Linus Walleijead76f322011-02-24 13:21:08 +0100416 dev_info(uap->port.dev, "DMA channel RX %s\n",
417 dma_chan_name(uap->dmarx.chan));
418 }
Russell King68b65f72010-12-22 17:24:39 +0000419}
420
Russell King68b65f72010-12-22 17:24:39 +0000421static void pl011_dma_remove(struct uart_amba_port *uap)
422{
Russell King68b65f72010-12-22 17:24:39 +0000423 if (uap->dmatx.chan)
424 dma_release_channel(uap->dmatx.chan);
Linus Walleijead76f322011-02-24 13:21:08 +0100425 if (uap->dmarx.chan)
426 dma_release_channel(uap->dmarx.chan);
Russell King68b65f72010-12-22 17:24:39 +0000427}
428
Dave Martin734745c2015-03-04 12:27:33 +0000429/* Forward declare these for the refill routine */
Russell King68b65f72010-12-22 17:24:39 +0000430static int pl011_dma_tx_refill(struct uart_amba_port *uap);
Dave Martin734745c2015-03-04 12:27:33 +0000431static void pl011_start_tx_pio(struct uart_amba_port *uap);
Russell King68b65f72010-12-22 17:24:39 +0000432
433/*
434 * The current DMA TX buffer has been sent.
435 * Try to queue up another DMA buffer.
436 */
437static void pl011_dma_tx_callback(void *data)
438{
439 struct uart_amba_port *uap = data;
440 struct pl011_dmatx_data *dmatx = &uap->dmatx;
441 unsigned long flags;
442 u16 dmacr;
443
444 spin_lock_irqsave(&uap->port.lock, flags);
445 if (uap->dmatx.queued)
446 dma_unmap_sg(dmatx->chan->device->dev, &dmatx->sg, 1,
447 DMA_TO_DEVICE);
448
449 dmacr = uap->dmacr;
450 uap->dmacr = dmacr & ~UART011_TXDMAE;
Russell King75836332015-11-03 14:50:58 +0000451 pl011_write(uap->dmacr, uap->port.membase, UART011_DMACR);
Russell King68b65f72010-12-22 17:24:39 +0000452
453 /*
454 * If TX DMA was disabled, it means that we've stopped the DMA for
455 * some reason (eg, XOFF received, or we want to send an X-char.)
456 *
457 * Note: we need to be careful here of a potential race between DMA
458 * and the rest of the driver - if the driver disables TX DMA while
459 * a TX buffer completing, we must update the tx queued status to
460 * get further refills (hence we check dmacr).
461 */
462 if (!(dmacr & UART011_TXDMAE) || uart_tx_stopped(&uap->port) ||
463 uart_circ_empty(&uap->port.state->xmit)) {
464 uap->dmatx.queued = false;
465 spin_unlock_irqrestore(&uap->port.lock, flags);
466 return;
467 }
468
Dave Martin734745c2015-03-04 12:27:33 +0000469 if (pl011_dma_tx_refill(uap) <= 0)
Russell King68b65f72010-12-22 17:24:39 +0000470 /*
471 * We didn't queue a DMA buffer for some reason, but we
472 * have data pending to be sent. Re-enable the TX IRQ.
473 */
Dave Martin734745c2015-03-04 12:27:33 +0000474 pl011_start_tx_pio(uap);
475
Russell King68b65f72010-12-22 17:24:39 +0000476 spin_unlock_irqrestore(&uap->port.lock, flags);
477}
478
479/*
480 * Try to refill the TX DMA buffer.
481 * Locking: called with port lock held and IRQs disabled.
482 * Returns:
483 * 1 if we queued up a TX DMA buffer.
484 * 0 if we didn't want to handle this by DMA
485 * <0 on error
486 */
487static int pl011_dma_tx_refill(struct uart_amba_port *uap)
488{
489 struct pl011_dmatx_data *dmatx = &uap->dmatx;
490 struct dma_chan *chan = dmatx->chan;
491 struct dma_device *dma_dev = chan->device;
492 struct dma_async_tx_descriptor *desc;
493 struct circ_buf *xmit = &uap->port.state->xmit;
494 unsigned int count;
495
496 /*
497 * Try to avoid the overhead involved in using DMA if the
498 * transaction fits in the first half of the FIFO, by using
499 * the standard interrupt handling. This ensures that we
500 * issue a uart_write_wakeup() at the appropriate time.
501 */
502 count = uart_circ_chars_pending(xmit);
503 if (count < (uap->fifosize >> 1)) {
504 uap->dmatx.queued = false;
505 return 0;
506 }
507
508 /*
509 * Bodge: don't send the last character by DMA, as this
510 * will prevent XON from notifying us to restart DMA.
511 */
512 count -= 1;
513
514 /* Else proceed to copy the TX chars to the DMA buffer and fire DMA */
515 if (count > PL011_DMA_BUFFER_SIZE)
516 count = PL011_DMA_BUFFER_SIZE;
517
518 if (xmit->tail < xmit->head)
519 memcpy(&dmatx->buf[0], &xmit->buf[xmit->tail], count);
520 else {
521 size_t first = UART_XMIT_SIZE - xmit->tail;
Andrew Jacksone2a545a2014-11-07 14:14:39 +0000522 size_t second;
523
524 if (first > count)
525 first = count;
526 second = count - first;
Russell King68b65f72010-12-22 17:24:39 +0000527
528 memcpy(&dmatx->buf[0], &xmit->buf[xmit->tail], first);
529 if (second)
530 memcpy(&dmatx->buf[first], &xmit->buf[0], second);
531 }
532
533 dmatx->sg.length = count;
534
535 if (dma_map_sg(dma_dev->dev, &dmatx->sg, 1, DMA_TO_DEVICE) != 1) {
536 uap->dmatx.queued = false;
537 dev_dbg(uap->port.dev, "unable to map TX DMA\n");
538 return -EBUSY;
539 }
540
Alexandre Bounine16052822012-03-08 16:11:18 -0500541 desc = dmaengine_prep_slave_sg(chan, &dmatx->sg, 1, DMA_MEM_TO_DEV,
Russell King68b65f72010-12-22 17:24:39 +0000542 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
543 if (!desc) {
544 dma_unmap_sg(dma_dev->dev, &dmatx->sg, 1, DMA_TO_DEVICE);
545 uap->dmatx.queued = false;
546 /*
547 * If DMA cannot be used right now, we complete this
548 * transaction via IRQ and let the TTY layer retry.
549 */
550 dev_dbg(uap->port.dev, "TX DMA busy\n");
551 return -EBUSY;
552 }
553
554 /* Some data to go along to the callback */
555 desc->callback = pl011_dma_tx_callback;
556 desc->callback_param = uap;
557
558 /* All errors should happen at prepare time */
559 dmaengine_submit(desc);
560
561 /* Fire the DMA transaction */
562 dma_dev->device_issue_pending(chan);
563
564 uap->dmacr |= UART011_TXDMAE;
Russell King75836332015-11-03 14:50:58 +0000565 pl011_write(uap->dmacr, uap->port.membase, UART011_DMACR);
Russell King68b65f72010-12-22 17:24:39 +0000566 uap->dmatx.queued = true;
567
568 /*
569 * Now we know that DMA will fire, so advance the ring buffer
570 * with the stuff we just dispatched.
571 */
572 xmit->tail = (xmit->tail + count) & (UART_XMIT_SIZE - 1);
573 uap->port.icount.tx += count;
574
575 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
576 uart_write_wakeup(&uap->port);
577
578 return 1;
579}
580
581/*
582 * We received a transmit interrupt without a pending X-char but with
583 * pending characters.
584 * Locking: called with port lock held and IRQs disabled.
585 * Returns:
586 * false if we want to use PIO to transmit
587 * true if we queued a DMA buffer
588 */
589static bool pl011_dma_tx_irq(struct uart_amba_port *uap)
590{
Linus Walleijead76f322011-02-24 13:21:08 +0100591 if (!uap->using_tx_dma)
Russell King68b65f72010-12-22 17:24:39 +0000592 return false;
593
594 /*
595 * If we already have a TX buffer queued, but received a
596 * TX interrupt, it will be because we've just sent an X-char.
597 * Ensure the TX DMA is enabled and the TX IRQ is disabled.
598 */
599 if (uap->dmatx.queued) {
600 uap->dmacr |= UART011_TXDMAE;
Russell King75836332015-11-03 14:50:58 +0000601 pl011_write(uap->dmacr, uap->port.membase, UART011_DMACR);
Russell King68b65f72010-12-22 17:24:39 +0000602 uap->im &= ~UART011_TXIM;
Russell King75836332015-11-03 14:50:58 +0000603 pl011_write(uap->im, uap->port.membase, UART011_IMSC);
Russell King68b65f72010-12-22 17:24:39 +0000604 return true;
605 }
606
607 /*
608 * We don't have a TX buffer queued, so try to queue one.
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300609 * If we successfully queued a buffer, mask the TX IRQ.
Russell King68b65f72010-12-22 17:24:39 +0000610 */
611 if (pl011_dma_tx_refill(uap) > 0) {
612 uap->im &= ~UART011_TXIM;
Russell King75836332015-11-03 14:50:58 +0000613 pl011_write(uap->im, uap->port.membase, UART011_IMSC);
Russell King68b65f72010-12-22 17:24:39 +0000614 return true;
615 }
616 return false;
617}
618
619/*
620 * Stop the DMA transmit (eg, due to received XOFF).
621 * Locking: called with port lock held and IRQs disabled.
622 */
623static inline void pl011_dma_tx_stop(struct uart_amba_port *uap)
624{
625 if (uap->dmatx.queued) {
626 uap->dmacr &= ~UART011_TXDMAE;
Russell King75836332015-11-03 14:50:58 +0000627 pl011_write(uap->dmacr, uap->port.membase, UART011_DMACR);
Russell King68b65f72010-12-22 17:24:39 +0000628 }
629}
630
631/*
632 * Try to start a DMA transmit, or in the case of an XON/OFF
633 * character queued for send, try to get that character out ASAP.
634 * Locking: called with port lock held and IRQs disabled.
635 * Returns:
636 * false if we want the TX IRQ to be enabled
637 * true if we have a buffer queued
638 */
639static inline bool pl011_dma_tx_start(struct uart_amba_port *uap)
640{
641 u16 dmacr;
642
Linus Walleijead76f322011-02-24 13:21:08 +0100643 if (!uap->using_tx_dma)
Russell King68b65f72010-12-22 17:24:39 +0000644 return false;
645
646 if (!uap->port.x_char) {
647 /* no X-char, try to push chars out in DMA mode */
648 bool ret = true;
649
650 if (!uap->dmatx.queued) {
651 if (pl011_dma_tx_refill(uap) > 0) {
652 uap->im &= ~UART011_TXIM;
Russell King75836332015-11-03 14:50:58 +0000653 pl011_write(uap->im, uap->port.membase,
654 UART011_IMSC);
Dave Martin734745c2015-03-04 12:27:33 +0000655 } else
Russell King68b65f72010-12-22 17:24:39 +0000656 ret = false;
Russell King68b65f72010-12-22 17:24:39 +0000657 } else if (!(uap->dmacr & UART011_TXDMAE)) {
658 uap->dmacr |= UART011_TXDMAE;
Russell King75836332015-11-03 14:50:58 +0000659 pl011_write(uap->dmacr, uap->port.membase,
660 UART011_DMACR);
Russell King68b65f72010-12-22 17:24:39 +0000661 }
662 return ret;
663 }
664
665 /*
666 * We have an X-char to send. Disable DMA to prevent it loading
667 * the TX fifo, and then see if we can stuff it into the FIFO.
668 */
669 dmacr = uap->dmacr;
670 uap->dmacr &= ~UART011_TXDMAE;
Russell King75836332015-11-03 14:50:58 +0000671 pl011_write(uap->dmacr, uap->port.membase, UART011_DMACR);
Russell King68b65f72010-12-22 17:24:39 +0000672
Russell King75836332015-11-03 14:50:58 +0000673 if (pl011_read(uap->port.membase, UART01x_FR) & UART01x_FR_TXFF) {
Russell King68b65f72010-12-22 17:24:39 +0000674 /*
675 * No space in the FIFO, so enable the transmit interrupt
676 * so we know when there is space. Note that once we've
677 * loaded the character, we should just re-enable DMA.
678 */
679 return false;
680 }
681
Russell King75836332015-11-03 14:50:58 +0000682 pl011_write(uap->port.x_char, uap->port.membase, UART01x_DR);
Russell King68b65f72010-12-22 17:24:39 +0000683 uap->port.icount.tx++;
684 uap->port.x_char = 0;
685
686 /* Success - restore the DMA state */
687 uap->dmacr = dmacr;
Russell King75836332015-11-03 14:50:58 +0000688 pl011_write(dmacr, uap->port.membase, UART011_DMACR);
Russell King68b65f72010-12-22 17:24:39 +0000689
690 return true;
691}
692
693/*
694 * Flush the transmit buffer.
695 * Locking: called with port lock held and IRQs disabled.
696 */
697static void pl011_dma_flush_buffer(struct uart_port *port)
Fabio Estevamb83286b2013-08-09 17:58:51 -0300698__releases(&uap->port.lock)
699__acquires(&uap->port.lock)
Russell King68b65f72010-12-22 17:24:39 +0000700{
Daniel Thompsona5820c22014-09-03 12:51:55 +0100701 struct uart_amba_port *uap =
702 container_of(port, struct uart_amba_port, port);
Russell King68b65f72010-12-22 17:24:39 +0000703
Linus Walleijead76f322011-02-24 13:21:08 +0100704 if (!uap->using_tx_dma)
Russell King68b65f72010-12-22 17:24:39 +0000705 return;
706
707 /* Avoid deadlock with the DMA engine callback */
708 spin_unlock(&uap->port.lock);
709 dmaengine_terminate_all(uap->dmatx.chan);
710 spin_lock(&uap->port.lock);
711 if (uap->dmatx.queued) {
712 dma_unmap_sg(uap->dmatx.chan->device->dev, &uap->dmatx.sg, 1,
713 DMA_TO_DEVICE);
714 uap->dmatx.queued = false;
715 uap->dmacr &= ~UART011_TXDMAE;
Russell King75836332015-11-03 14:50:58 +0000716 pl011_write(uap->dmacr, uap->port.membase, UART011_DMACR);
Russell King68b65f72010-12-22 17:24:39 +0000717 }
718}
719
Linus Walleijead76f322011-02-24 13:21:08 +0100720static void pl011_dma_rx_callback(void *data);
721
722static int pl011_dma_rx_trigger_dma(struct uart_amba_port *uap)
723{
724 struct dma_chan *rxchan = uap->dmarx.chan;
Linus Walleijead76f322011-02-24 13:21:08 +0100725 struct pl011_dmarx_data *dmarx = &uap->dmarx;
726 struct dma_async_tx_descriptor *desc;
727 struct pl011_sgbuf *sgbuf;
728
729 if (!rxchan)
730 return -EIO;
731
732 /* Start the RX DMA job */
733 sgbuf = uap->dmarx.use_buf_b ?
734 &uap->dmarx.sgbuf_b : &uap->dmarx.sgbuf_a;
Alexandre Bounine16052822012-03-08 16:11:18 -0500735 desc = dmaengine_prep_slave_sg(rxchan, &sgbuf->sg, 1,
Vinod Koula485df42011-10-14 10:47:38 +0530736 DMA_DEV_TO_MEM,
Linus Walleijead76f322011-02-24 13:21:08 +0100737 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
738 /*
739 * If the DMA engine is busy and cannot prepare a
740 * channel, no big deal, the driver will fall back
741 * to interrupt mode as a result of this error code.
742 */
743 if (!desc) {
744 uap->dmarx.running = false;
745 dmaengine_terminate_all(rxchan);
746 return -EBUSY;
747 }
748
749 /* Some data to go along to the callback */
750 desc->callback = pl011_dma_rx_callback;
751 desc->callback_param = uap;
752 dmarx->cookie = dmaengine_submit(desc);
753 dma_async_issue_pending(rxchan);
754
755 uap->dmacr |= UART011_RXDMAE;
Russell King75836332015-11-03 14:50:58 +0000756 pl011_write(uap->dmacr, uap->port.membase, UART011_DMACR);
Linus Walleijead76f322011-02-24 13:21:08 +0100757 uap->dmarx.running = true;
758
759 uap->im &= ~UART011_RXIM;
Russell King75836332015-11-03 14:50:58 +0000760 pl011_write(uap->im, uap->port.membase, UART011_IMSC);
Linus Walleijead76f322011-02-24 13:21:08 +0100761
762 return 0;
763}
764
765/*
766 * This is called when either the DMA job is complete, or
767 * the FIFO timeout interrupt occurred. This must be called
768 * with the port spinlock uap->port.lock held.
769 */
770static void pl011_dma_rx_chars(struct uart_amba_port *uap,
771 u32 pending, bool use_buf_b,
772 bool readfifo)
773{
Jiri Slaby05c7cd32013-01-03 15:53:04 +0100774 struct tty_port *port = &uap->port.state->port;
Linus Walleijead76f322011-02-24 13:21:08 +0100775 struct pl011_sgbuf *sgbuf = use_buf_b ?
776 &uap->dmarx.sgbuf_b : &uap->dmarx.sgbuf_a;
Linus Walleijead76f322011-02-24 13:21:08 +0100777 int dma_count = 0;
778 u32 fifotaken = 0; /* only used for vdbg() */
779
Chanho Mincb06ff12013-03-27 18:38:11 +0900780 struct pl011_dmarx_data *dmarx = &uap->dmarx;
781 int dmataken = 0;
782
783 if (uap->dmarx.poll_rate) {
784 /* The data can be taken by polling */
785 dmataken = sgbuf->sg.length - dmarx->last_residue;
786 /* Recalculate the pending size */
787 if (pending >= dmataken)
788 pending -= dmataken;
789 }
790
791 /* Pick the remain data from the DMA */
Linus Walleijead76f322011-02-24 13:21:08 +0100792 if (pending) {
Linus Walleijead76f322011-02-24 13:21:08 +0100793
794 /*
795 * First take all chars in the DMA pipe, then look in the FIFO.
796 * Note that tty_insert_flip_buf() tries to take as many chars
797 * as it can.
798 */
Chanho Mincb06ff12013-03-27 18:38:11 +0900799 dma_count = tty_insert_flip_string(port, sgbuf->buf + dmataken,
800 pending);
Linus Walleijead76f322011-02-24 13:21:08 +0100801
802 uap->port.icount.rx += dma_count;
803 if (dma_count < pending)
804 dev_warn(uap->port.dev,
805 "couldn't insert all characters (TTY is full?)\n");
806 }
807
Chanho Mincb06ff12013-03-27 18:38:11 +0900808 /* Reset the last_residue for Rx DMA poll */
809 if (uap->dmarx.poll_rate)
810 dmarx->last_residue = sgbuf->sg.length;
811
Linus Walleijead76f322011-02-24 13:21:08 +0100812 /*
813 * Only continue with trying to read the FIFO if all DMA chars have
814 * been taken first.
815 */
816 if (dma_count == pending && readfifo) {
817 /* Clear any error flags */
Russell King75836332015-11-03 14:50:58 +0000818 pl011_write(UART011_OEIS | UART011_BEIS | UART011_PEIS |
819 UART011_FEIS, uap->port.membase, UART011_ICR);
Linus Walleijead76f322011-02-24 13:21:08 +0100820
821 /*
822 * If we read all the DMA'd characters, and we had an
Linus Walleij29772c42011-02-24 13:21:36 +0100823 * incomplete buffer, that could be due to an rx error, or
824 * maybe we just timed out. Read any pending chars and check
825 * the error status.
826 *
827 * Error conditions will only occur in the FIFO, these will
828 * trigger an immediate interrupt and stop the DMA job, so we
829 * will always find the error in the FIFO, never in the DMA
830 * buffer.
Linus Walleijead76f322011-02-24 13:21:08 +0100831 */
Linus Walleij29772c42011-02-24 13:21:36 +0100832 fifotaken = pl011_fifo_to_tty(uap);
Linus Walleijead76f322011-02-24 13:21:08 +0100833 }
834
835 spin_unlock(&uap->port.lock);
836 dev_vdbg(uap->port.dev,
837 "Took %d chars from DMA buffer and %d chars from the FIFO\n",
838 dma_count, fifotaken);
Jiri Slaby2e124b42013-01-03 15:53:06 +0100839 tty_flip_buffer_push(port);
Linus Walleijead76f322011-02-24 13:21:08 +0100840 spin_lock(&uap->port.lock);
841}
842
843static void pl011_dma_rx_irq(struct uart_amba_port *uap)
844{
845 struct pl011_dmarx_data *dmarx = &uap->dmarx;
846 struct dma_chan *rxchan = dmarx->chan;
847 struct pl011_sgbuf *sgbuf = dmarx->use_buf_b ?
848 &dmarx->sgbuf_b : &dmarx->sgbuf_a;
849 size_t pending;
850 struct dma_tx_state state;
851 enum dma_status dmastat;
852
853 /*
854 * Pause the transfer so we can trust the current counter,
855 * do this before we pause the PL011 block, else we may
856 * overflow the FIFO.
857 */
858 if (dmaengine_pause(rxchan))
859 dev_err(uap->port.dev, "unable to pause DMA transfer\n");
860 dmastat = rxchan->device->device_tx_status(rxchan,
861 dmarx->cookie, &state);
862 if (dmastat != DMA_PAUSED)
863 dev_err(uap->port.dev, "unable to pause DMA transfer\n");
864
865 /* Disable RX DMA - incoming data will wait in the FIFO */
866 uap->dmacr &= ~UART011_RXDMAE;
Russell King75836332015-11-03 14:50:58 +0000867 pl011_write(uap->dmacr, uap->port.membase, UART011_DMACR);
Linus Walleijead76f322011-02-24 13:21:08 +0100868 uap->dmarx.running = false;
869
870 pending = sgbuf->sg.length - state.residue;
871 BUG_ON(pending > PL011_DMA_BUFFER_SIZE);
872 /* Then we terminate the transfer - we now know our residue */
873 dmaengine_terminate_all(rxchan);
874
875 /*
876 * This will take the chars we have so far and insert
877 * into the framework.
878 */
879 pl011_dma_rx_chars(uap, pending, dmarx->use_buf_b, true);
880
881 /* Switch buffer & re-trigger DMA job */
882 dmarx->use_buf_b = !dmarx->use_buf_b;
883 if (pl011_dma_rx_trigger_dma(uap)) {
884 dev_dbg(uap->port.dev, "could not retrigger RX DMA job "
885 "fall back to interrupt mode\n");
886 uap->im |= UART011_RXIM;
Russell King75836332015-11-03 14:50:58 +0000887 pl011_write(uap->im, uap->port.membase, UART011_IMSC);
Linus Walleijead76f322011-02-24 13:21:08 +0100888 }
889}
890
891static void pl011_dma_rx_callback(void *data)
892{
893 struct uart_amba_port *uap = data;
894 struct pl011_dmarx_data *dmarx = &uap->dmarx;
Chanho Min6dc01aa2012-02-20 10:24:40 +0900895 struct dma_chan *rxchan = dmarx->chan;
Linus Walleijead76f322011-02-24 13:21:08 +0100896 bool lastbuf = dmarx->use_buf_b;
Chanho Min6dc01aa2012-02-20 10:24:40 +0900897 struct pl011_sgbuf *sgbuf = dmarx->use_buf_b ?
898 &dmarx->sgbuf_b : &dmarx->sgbuf_a;
899 size_t pending;
900 struct dma_tx_state state;
Linus Walleijead76f322011-02-24 13:21:08 +0100901 int ret;
902
903 /*
904 * This completion interrupt occurs typically when the
905 * RX buffer is totally stuffed but no timeout has yet
906 * occurred. When that happens, we just want the RX
907 * routine to flush out the secondary DMA buffer while
908 * we immediately trigger the next DMA job.
909 */
910 spin_lock_irq(&uap->port.lock);
Chanho Min6dc01aa2012-02-20 10:24:40 +0900911 /*
912 * Rx data can be taken by the UART interrupts during
913 * the DMA irq handler. So we check the residue here.
914 */
915 rxchan->device->device_tx_status(rxchan, dmarx->cookie, &state);
916 pending = sgbuf->sg.length - state.residue;
917 BUG_ON(pending > PL011_DMA_BUFFER_SIZE);
918 /* Then we terminate the transfer - we now know our residue */
919 dmaengine_terminate_all(rxchan);
920
Linus Walleijead76f322011-02-24 13:21:08 +0100921 uap->dmarx.running = false;
922 dmarx->use_buf_b = !lastbuf;
923 ret = pl011_dma_rx_trigger_dma(uap);
924
Chanho Min6dc01aa2012-02-20 10:24:40 +0900925 pl011_dma_rx_chars(uap, pending, lastbuf, false);
Linus Walleijead76f322011-02-24 13:21:08 +0100926 spin_unlock_irq(&uap->port.lock);
927 /*
928 * Do this check after we picked the DMA chars so we don't
929 * get some IRQ immediately from RX.
930 */
931 if (ret) {
932 dev_dbg(uap->port.dev, "could not retrigger RX DMA job "
933 "fall back to interrupt mode\n");
934 uap->im |= UART011_RXIM;
Russell King75836332015-11-03 14:50:58 +0000935 pl011_write(uap->im, uap->port.membase, UART011_IMSC);
Linus Walleijead76f322011-02-24 13:21:08 +0100936 }
937}
938
939/*
940 * Stop accepting received characters, when we're shutting down or
941 * suspending this port.
942 * Locking: called with port lock held and IRQs disabled.
943 */
944static inline void pl011_dma_rx_stop(struct uart_amba_port *uap)
945{
946 /* FIXME. Just disable the DMA enable */
947 uap->dmacr &= ~UART011_RXDMAE;
Russell King75836332015-11-03 14:50:58 +0000948 pl011_write(uap->dmacr, uap->port.membase, UART011_DMACR);
Linus Walleijead76f322011-02-24 13:21:08 +0100949}
Russell King68b65f72010-12-22 17:24:39 +0000950
Chanho Mincb06ff12013-03-27 18:38:11 +0900951/*
952 * Timer handler for Rx DMA polling.
953 * Every polling, It checks the residue in the dma buffer and transfer
954 * data to the tty. Also, last_residue is updated for the next polling.
955 */
956static void pl011_dma_rx_poll(unsigned long args)
957{
958 struct uart_amba_port *uap = (struct uart_amba_port *)args;
959 struct tty_port *port = &uap->port.state->port;
960 struct pl011_dmarx_data *dmarx = &uap->dmarx;
961 struct dma_chan *rxchan = uap->dmarx.chan;
962 unsigned long flags = 0;
963 unsigned int dmataken = 0;
964 unsigned int size = 0;
965 struct pl011_sgbuf *sgbuf;
966 int dma_count;
967 struct dma_tx_state state;
968
969 sgbuf = dmarx->use_buf_b ? &uap->dmarx.sgbuf_b : &uap->dmarx.sgbuf_a;
970 rxchan->device->device_tx_status(rxchan, dmarx->cookie, &state);
971 if (likely(state.residue < dmarx->last_residue)) {
972 dmataken = sgbuf->sg.length - dmarx->last_residue;
973 size = dmarx->last_residue - state.residue;
974 dma_count = tty_insert_flip_string(port, sgbuf->buf + dmataken,
975 size);
976 if (dma_count == size)
977 dmarx->last_residue = state.residue;
978 dmarx->last_jiffies = jiffies;
979 }
980 tty_flip_buffer_push(port);
981
982 /*
983 * If no data is received in poll_timeout, the driver will fall back
984 * to interrupt mode. We will retrigger DMA at the first interrupt.
985 */
986 if (jiffies_to_msecs(jiffies - dmarx->last_jiffies)
987 > uap->dmarx.poll_timeout) {
988
989 spin_lock_irqsave(&uap->port.lock, flags);
990 pl011_dma_rx_stop(uap);
Guennadi Liakhovetskic25a1ad2013-12-10 14:54:47 +0100991 uap->im |= UART011_RXIM;
Russell King75836332015-11-03 14:50:58 +0000992 pl011_write(uap->im, uap->port.membase, UART011_IMSC);
Chanho Mincb06ff12013-03-27 18:38:11 +0900993 spin_unlock_irqrestore(&uap->port.lock, flags);
994
995 uap->dmarx.running = false;
996 dmaengine_terminate_all(rxchan);
997 del_timer(&uap->dmarx.timer);
998 } else {
999 mod_timer(&uap->dmarx.timer,
1000 jiffies + msecs_to_jiffies(uap->dmarx.poll_rate));
1001 }
1002}
1003
Russell King68b65f72010-12-22 17:24:39 +00001004static void pl011_dma_startup(struct uart_amba_port *uap)
1005{
Linus Walleijead76f322011-02-24 13:21:08 +01001006 int ret;
1007
Jorge Ramirez-Ortiz1c9be312015-03-06 13:05:40 -05001008 if (!uap->dma_probed)
1009 pl011_dma_probe(uap);
1010
Russell King68b65f72010-12-22 17:24:39 +00001011 if (!uap->dmatx.chan)
1012 return;
1013
Andrew Jackson4c0be452014-11-07 14:14:35 +00001014 uap->dmatx.buf = kmalloc(PL011_DMA_BUFFER_SIZE, GFP_KERNEL | __GFP_DMA);
Russell King68b65f72010-12-22 17:24:39 +00001015 if (!uap->dmatx.buf) {
1016 dev_err(uap->port.dev, "no memory for DMA TX buffer\n");
1017 uap->port.fifosize = uap->fifosize;
1018 return;
1019 }
1020
1021 sg_init_one(&uap->dmatx.sg, uap->dmatx.buf, PL011_DMA_BUFFER_SIZE);
1022
1023 /* The DMA buffer is now the FIFO the TTY subsystem can use */
1024 uap->port.fifosize = PL011_DMA_BUFFER_SIZE;
Linus Walleijead76f322011-02-24 13:21:08 +01001025 uap->using_tx_dma = true;
Russell King68b65f72010-12-22 17:24:39 +00001026
Linus Walleijead76f322011-02-24 13:21:08 +01001027 if (!uap->dmarx.chan)
1028 goto skip_rx;
1029
1030 /* Allocate and map DMA RX buffers */
1031 ret = pl011_sgbuf_init(uap->dmarx.chan, &uap->dmarx.sgbuf_a,
1032 DMA_FROM_DEVICE);
1033 if (ret) {
1034 dev_err(uap->port.dev, "failed to init DMA %s: %d\n",
1035 "RX buffer A", ret);
1036 goto skip_rx;
1037 }
1038
1039 ret = pl011_sgbuf_init(uap->dmarx.chan, &uap->dmarx.sgbuf_b,
1040 DMA_FROM_DEVICE);
1041 if (ret) {
1042 dev_err(uap->port.dev, "failed to init DMA %s: %d\n",
1043 "RX buffer B", ret);
1044 pl011_sgbuf_free(uap->dmarx.chan, &uap->dmarx.sgbuf_a,
1045 DMA_FROM_DEVICE);
1046 goto skip_rx;
1047 }
1048
1049 uap->using_rx_dma = true;
1050
1051skip_rx:
Russell King68b65f72010-12-22 17:24:39 +00001052 /* Turn on DMA error (RX/TX will be enabled on demand) */
1053 uap->dmacr |= UART011_DMAONERR;
Russell King75836332015-11-03 14:50:58 +00001054 pl011_write(uap->dmacr, uap->port.membase, UART011_DMACR);
Russell King38d62432010-12-22 17:59:16 +00001055
1056 /*
1057 * ST Micro variants has some specific dma burst threshold
1058 * compensation. Set this to 16 bytes, so burst will only
1059 * be issued above/below 16 bytes.
1060 */
1061 if (uap->vendor->dma_threshold)
Russell King75836332015-11-03 14:50:58 +00001062 pl011_write(ST_UART011_DMAWM_RX_16 | ST_UART011_DMAWM_TX_16,
1063 uap->port.membase, ST_UART011_DMAWM);
Linus Walleijead76f322011-02-24 13:21:08 +01001064
1065 if (uap->using_rx_dma) {
1066 if (pl011_dma_rx_trigger_dma(uap))
1067 dev_dbg(uap->port.dev, "could not trigger initial "
1068 "RX DMA job, fall back to interrupt mode\n");
Chanho Mincb06ff12013-03-27 18:38:11 +09001069 if (uap->dmarx.poll_rate) {
1070 init_timer(&(uap->dmarx.timer));
1071 uap->dmarx.timer.function = pl011_dma_rx_poll;
1072 uap->dmarx.timer.data = (unsigned long)uap;
1073 mod_timer(&uap->dmarx.timer,
1074 jiffies +
1075 msecs_to_jiffies(uap->dmarx.poll_rate));
1076 uap->dmarx.last_residue = PL011_DMA_BUFFER_SIZE;
1077 uap->dmarx.last_jiffies = jiffies;
1078 }
Linus Walleijead76f322011-02-24 13:21:08 +01001079 }
Russell King68b65f72010-12-22 17:24:39 +00001080}
1081
1082static void pl011_dma_shutdown(struct uart_amba_port *uap)
1083{
Linus Walleijead76f322011-02-24 13:21:08 +01001084 if (!(uap->using_tx_dma || uap->using_rx_dma))
Russell King68b65f72010-12-22 17:24:39 +00001085 return;
1086
1087 /* Disable RX and TX DMA */
Russell King75836332015-11-03 14:50:58 +00001088 while (pl011_read(uap->port.membase, UART01x_FR) & UART01x_FR_BUSY)
Russell King68b65f72010-12-22 17:24:39 +00001089 barrier();
1090
1091 spin_lock_irq(&uap->port.lock);
1092 uap->dmacr &= ~(UART011_DMAONERR | UART011_RXDMAE | UART011_TXDMAE);
Russell King75836332015-11-03 14:50:58 +00001093 pl011_write(uap->dmacr, uap->port.membase, UART011_DMACR);
Russell King68b65f72010-12-22 17:24:39 +00001094 spin_unlock_irq(&uap->port.lock);
1095
Linus Walleijead76f322011-02-24 13:21:08 +01001096 if (uap->using_tx_dma) {
1097 /* In theory, this should already be done by pl011_dma_flush_buffer */
1098 dmaengine_terminate_all(uap->dmatx.chan);
1099 if (uap->dmatx.queued) {
1100 dma_unmap_sg(uap->dmatx.chan->device->dev, &uap->dmatx.sg, 1,
1101 DMA_TO_DEVICE);
1102 uap->dmatx.queued = false;
1103 }
1104
1105 kfree(uap->dmatx.buf);
1106 uap->using_tx_dma = false;
Russell King68b65f72010-12-22 17:24:39 +00001107 }
1108
Linus Walleijead76f322011-02-24 13:21:08 +01001109 if (uap->using_rx_dma) {
1110 dmaengine_terminate_all(uap->dmarx.chan);
1111 /* Clean up the RX DMA */
1112 pl011_sgbuf_free(uap->dmarx.chan, &uap->dmarx.sgbuf_a, DMA_FROM_DEVICE);
1113 pl011_sgbuf_free(uap->dmarx.chan, &uap->dmarx.sgbuf_b, DMA_FROM_DEVICE);
Chanho Mincb06ff12013-03-27 18:38:11 +09001114 if (uap->dmarx.poll_rate)
1115 del_timer_sync(&uap->dmarx.timer);
Linus Walleijead76f322011-02-24 13:21:08 +01001116 uap->using_rx_dma = false;
1117 }
Russell King68b65f72010-12-22 17:24:39 +00001118}
1119
Linus Walleijead76f322011-02-24 13:21:08 +01001120static inline bool pl011_dma_rx_available(struct uart_amba_port *uap)
1121{
1122 return uap->using_rx_dma;
1123}
1124
1125static inline bool pl011_dma_rx_running(struct uart_amba_port *uap)
1126{
1127 return uap->using_rx_dma && uap->dmarx.running;
1128}
1129
Russell King68b65f72010-12-22 17:24:39 +00001130#else
1131/* Blank functions if the DMA engine is not available */
Jorge Ramirez-Ortiz1c9be312015-03-06 13:05:40 -05001132static inline void pl011_dma_probe(struct uart_amba_port *uap)
Russell King68b65f72010-12-22 17:24:39 +00001133{
1134}
1135
1136static inline void pl011_dma_remove(struct uart_amba_port *uap)
1137{
1138}
1139
1140static inline void pl011_dma_startup(struct uart_amba_port *uap)
1141{
1142}
1143
1144static inline void pl011_dma_shutdown(struct uart_amba_port *uap)
1145{
1146}
1147
1148static inline bool pl011_dma_tx_irq(struct uart_amba_port *uap)
1149{
1150 return false;
1151}
1152
1153static inline void pl011_dma_tx_stop(struct uart_amba_port *uap)
1154{
1155}
1156
1157static inline bool pl011_dma_tx_start(struct uart_amba_port *uap)
1158{
1159 return false;
1160}
1161
Linus Walleijead76f322011-02-24 13:21:08 +01001162static inline void pl011_dma_rx_irq(struct uart_amba_port *uap)
1163{
1164}
1165
1166static inline void pl011_dma_rx_stop(struct uart_amba_port *uap)
1167{
1168}
1169
1170static inline int pl011_dma_rx_trigger_dma(struct uart_amba_port *uap)
1171{
1172 return -EIO;
1173}
1174
1175static inline bool pl011_dma_rx_available(struct uart_amba_port *uap)
1176{
1177 return false;
1178}
1179
1180static inline bool pl011_dma_rx_running(struct uart_amba_port *uap)
1181{
1182 return false;
1183}
1184
Russell King68b65f72010-12-22 17:24:39 +00001185#define pl011_dma_flush_buffer NULL
1186#endif
1187
Russell Kingb129a8c2005-08-31 10:12:14 +01001188static void pl011_stop_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001190 struct uart_amba_port *uap =
1191 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192
1193 uap->im &= ~UART011_TXIM;
Russell King75836332015-11-03 14:50:58 +00001194 pl011_write(uap->im, uap->port.membase, UART011_IMSC);
Russell King68b65f72010-12-22 17:24:39 +00001195 pl011_dma_tx_stop(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196}
1197
Dave Martin1e84d222015-04-27 16:49:05 +01001198static void pl011_tx_chars(struct uart_amba_port *uap, bool from_irq);
Dave Martin734745c2015-03-04 12:27:33 +00001199
1200/* Start TX with programmed I/O only (no DMA) */
1201static void pl011_start_tx_pio(struct uart_amba_port *uap)
1202{
1203 uap->im |= UART011_TXIM;
Russell King75836332015-11-03 14:50:58 +00001204 pl011_write(uap->im, uap->port.membase, UART011_IMSC);
Dave Martin1e84d222015-04-27 16:49:05 +01001205 pl011_tx_chars(uap, false);
Dave Martin734745c2015-03-04 12:27:33 +00001206}
1207
Russell Kingb129a8c2005-08-31 10:12:14 +01001208static void pl011_start_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001210 struct uart_amba_port *uap =
1211 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212
Dave Martin734745c2015-03-04 12:27:33 +00001213 if (!pl011_dma_tx_start(uap))
1214 pl011_start_tx_pio(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215}
1216
1217static void pl011_stop_rx(struct uart_port *port)
1218{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001219 struct uart_amba_port *uap =
1220 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221
1222 uap->im &= ~(UART011_RXIM|UART011_RTIM|UART011_FEIM|
1223 UART011_PEIM|UART011_BEIM|UART011_OEIM);
Russell King75836332015-11-03 14:50:58 +00001224 pl011_write(uap->im, uap->port.membase, UART011_IMSC);
Linus Walleijead76f322011-02-24 13:21:08 +01001225
1226 pl011_dma_rx_stop(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227}
1228
1229static void pl011_enable_ms(struct uart_port *port)
1230{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001231 struct uart_amba_port *uap =
1232 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233
1234 uap->im |= UART011_RIMIM|UART011_CTSMIM|UART011_DCDMIM|UART011_DSRMIM;
Russell King75836332015-11-03 14:50:58 +00001235 pl011_write(uap->im, uap->port.membase, UART011_IMSC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236}
1237
David Howells7d12e782006-10-05 14:55:46 +01001238static void pl011_rx_chars(struct uart_amba_port *uap)
Fabio Estevamb83286b2013-08-09 17:58:51 -03001239__releases(&uap->port.lock)
1240__acquires(&uap->port.lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241{
Linus Walleij29772c42011-02-24 13:21:36 +01001242 pl011_fifo_to_tty(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243
Thomas Gleixner2389b272007-05-29 21:53:50 +01001244 spin_unlock(&uap->port.lock);
Jiri Slaby2e124b42013-01-03 15:53:06 +01001245 tty_flip_buffer_push(&uap->port.state->port);
Linus Walleijead76f322011-02-24 13:21:08 +01001246 /*
1247 * If we were temporarily out of DMA mode for a while,
1248 * attempt to switch back to DMA mode again.
1249 */
1250 if (pl011_dma_rx_available(uap)) {
1251 if (pl011_dma_rx_trigger_dma(uap)) {
1252 dev_dbg(uap->port.dev, "could not trigger RX DMA job "
1253 "fall back to interrupt mode again\n");
1254 uap->im |= UART011_RXIM;
Russell King75836332015-11-03 14:50:58 +00001255 pl011_write(uap->im, uap->port.membase, UART011_IMSC);
Chanho Mincb06ff12013-03-27 18:38:11 +09001256 } else {
Chanho Min89fa28d2013-04-03 11:10:37 +09001257#ifdef CONFIG_DMA_ENGINE
Chanho Mincb06ff12013-03-27 18:38:11 +09001258 /* Start Rx DMA poll */
1259 if (uap->dmarx.poll_rate) {
1260 uap->dmarx.last_jiffies = jiffies;
1261 uap->dmarx.last_residue = PL011_DMA_BUFFER_SIZE;
1262 mod_timer(&uap->dmarx.timer,
1263 jiffies +
1264 msecs_to_jiffies(uap->dmarx.poll_rate));
1265 }
Chanho Min89fa28d2013-04-03 11:10:37 +09001266#endif
Chanho Mincb06ff12013-03-27 18:38:11 +09001267 }
Linus Walleijead76f322011-02-24 13:21:08 +01001268 }
Thomas Gleixner2389b272007-05-29 21:53:50 +01001269 spin_lock(&uap->port.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270}
1271
Dave Martin1e84d222015-04-27 16:49:05 +01001272static bool pl011_tx_char(struct uart_amba_port *uap, unsigned char c,
1273 bool from_irq)
Dave Martin734745c2015-03-04 12:27:33 +00001274{
Dave Martin1e84d222015-04-27 16:49:05 +01001275 if (unlikely(!from_irq) &&
Russell King75836332015-11-03 14:50:58 +00001276 pl011_read(uap->port.membase, UART01x_FR) & UART01x_FR_TXFF)
Dave Martin1e84d222015-04-27 16:49:05 +01001277 return false; /* unable to transmit character */
1278
Russell King75836332015-11-03 14:50:58 +00001279 pl011_write(c, uap->port.membase, UART01x_DR);
Dave Martin734745c2015-03-04 12:27:33 +00001280 uap->port.icount.tx++;
1281
Dave Martin1e84d222015-04-27 16:49:05 +01001282 return true;
Dave Martin734745c2015-03-04 12:27:33 +00001283}
1284
Dave Martin1e84d222015-04-27 16:49:05 +01001285static void pl011_tx_chars(struct uart_amba_port *uap, bool from_irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286{
Alan Coxebd2c8f2009-09-19 13:13:28 -07001287 struct circ_buf *xmit = &uap->port.state->xmit;
Dave Martin1e84d222015-04-27 16:49:05 +01001288 int count = uap->fifosize >> 1;
Dave Martin734745c2015-03-04 12:27:33 +00001289
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 if (uap->port.x_char) {
Dave Martin1e84d222015-04-27 16:49:05 +01001291 if (!pl011_tx_char(uap, uap->port.x_char, from_irq))
1292 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 uap->port.x_char = 0;
Dave Martin734745c2015-03-04 12:27:33 +00001294 --count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 }
1296 if (uart_circ_empty(xmit) || uart_tx_stopped(&uap->port)) {
Russell Kingb129a8c2005-08-31 10:12:14 +01001297 pl011_stop_tx(&uap->port);
Dave Martin1e84d222015-04-27 16:49:05 +01001298 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 }
1300
Russell King68b65f72010-12-22 17:24:39 +00001301 /* If we are using DMA mode, try to send some characters. */
1302 if (pl011_dma_tx_irq(uap))
Dave Martin1e84d222015-04-27 16:49:05 +01001303 return;
Russell King68b65f72010-12-22 17:24:39 +00001304
Dave Martin1e84d222015-04-27 16:49:05 +01001305 do {
1306 if (likely(from_irq) && count-- == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 break;
Dave Martin1e84d222015-04-27 16:49:05 +01001308
1309 if (!pl011_tx_char(uap, xmit->buf[xmit->tail], from_irq))
1310 break;
1311
1312 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
1313 } while (!uart_circ_empty(xmit));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314
1315 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
1316 uart_write_wakeup(&uap->port);
1317
Dave Martin1e84d222015-04-27 16:49:05 +01001318 if (uart_circ_empty(xmit))
Russell Kingb129a8c2005-08-31 10:12:14 +01001319 pl011_stop_tx(&uap->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320}
1321
1322static void pl011_modem_status(struct uart_amba_port *uap)
1323{
1324 unsigned int status, delta;
1325
Russell King75836332015-11-03 14:50:58 +00001326 status = pl011_read(uap->port.membase, UART01x_FR);
1327 status &= UART01x_FR_MODEM_ANY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328
1329 delta = status ^ uap->old_status;
1330 uap->old_status = status;
1331
1332 if (!delta)
1333 return;
1334
1335 if (delta & UART01x_FR_DCD)
1336 uart_handle_dcd_change(&uap->port, status & UART01x_FR_DCD);
1337
Greg Kroah-Hartman062a68a2015-09-04 09:11:24 -07001338 if (delta & UART01x_FR_DSR)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 uap->port.icount.dsr++;
1340
Greg Kroah-Hartman062a68a2015-09-04 09:11:24 -07001341 if (delta & UART01x_FR_CTS)
1342 uart_handle_cts_change(&uap->port, status & UART01x_FR_CTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343
Alan Coxbdc04e32009-09-19 13:13:31 -07001344 wake_up_interruptible(&uap->port.state->port.delta_msr_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345}
1346
Andre Przywara9c4ef4b2015-05-21 17:26:20 +01001347static void check_apply_cts_event_workaround(struct uart_amba_port *uap)
1348{
1349 unsigned int dummy_read;
1350
1351 if (!uap->vendor->cts_event_workaround)
1352 return;
1353
1354 /* workaround to make sure that all bits are unlocked.. */
Russell King75836332015-11-03 14:50:58 +00001355 pl011_write(0x00, uap->port.membase, UART011_ICR);
Andre Przywara9c4ef4b2015-05-21 17:26:20 +01001356
1357 /*
1358 * WA: introduce 26ns(1 uart clk) delay before W1C;
1359 * single apb access will incur 2 pclk(133.12Mhz) delay,
1360 * so add 2 dummy reads
1361 */
Russell King75836332015-11-03 14:50:58 +00001362 dummy_read = pl011_read(uap->port.membase, UART011_ICR);
1363 dummy_read = pl011_read(uap->port.membase, UART011_ICR);
Andre Przywara9c4ef4b2015-05-21 17:26:20 +01001364}
1365
David Howells7d12e782006-10-05 14:55:46 +01001366static irqreturn_t pl011_int(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367{
1368 struct uart_amba_port *uap = dev_id;
Russell King963cc982010-12-22 17:16:09 +00001369 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 unsigned int status, pass_counter = AMBA_ISR_PASS_LIMIT;
Andre Przywara075167e2015-05-21 17:26:19 +01001371 u16 imsc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 int handled = 0;
1373
Russell King963cc982010-12-22 17:16:09 +00001374 spin_lock_irqsave(&uap->port.lock, flags);
Russell King75836332015-11-03 14:50:58 +00001375 imsc = pl011_read(uap->port.membase, UART011_IMSC);
1376 status = pl011_read(uap->port.membase, UART011_RIS) & imsc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 if (status) {
1378 do {
Andre Przywara9c4ef4b2015-05-21 17:26:20 +01001379 check_apply_cts_event_workaround(uap);
Greg Kroah-Hartmanf11c9842015-09-04 09:13:39 -07001380
Russell King75836332015-11-03 14:50:58 +00001381 pl011_write(status & ~(UART011_TXIS|UART011_RTIS|
1382 UART011_RXIS),
1383 uap->port.membase, UART011_ICR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384
Linus Walleijead76f322011-02-24 13:21:08 +01001385 if (status & (UART011_RTIS|UART011_RXIS)) {
1386 if (pl011_dma_rx_running(uap))
1387 pl011_dma_rx_irq(uap);
1388 else
1389 pl011_rx_chars(uap);
1390 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 if (status & (UART011_DSRMIS|UART011_DCDMIS|
1392 UART011_CTSMIS|UART011_RIMIS))
1393 pl011_modem_status(uap);
Dave Martin1e84d222015-04-27 16:49:05 +01001394 if (status & UART011_TXIS)
1395 pl011_tx_chars(uap, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396
Rajanikanth H.V4fd06902012-03-26 11:17:02 +02001397 if (pass_counter-- == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 break;
1399
Russell King75836332015-11-03 14:50:58 +00001400 status = pl011_read(uap->port.membase, UART011_RIS) & imsc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 } while (status != 0);
1402 handled = 1;
1403 }
1404
Russell King963cc982010-12-22 17:16:09 +00001405 spin_unlock_irqrestore(&uap->port.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406
1407 return IRQ_RETVAL(handled);
1408}
1409
Linus Walleije643f872012-06-17 15:44:19 +02001410static unsigned int pl011_tx_empty(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001412 struct uart_amba_port *uap =
1413 container_of(port, struct uart_amba_port, port);
Russell King75836332015-11-03 14:50:58 +00001414 unsigned int status = pl011_read(uap->port.membase, UART01x_FR);
Greg Kroah-Hartman062a68a2015-09-04 09:11:24 -07001415 return status & (UART01x_FR_BUSY|UART01x_FR_TXFF) ? 0 : TIOCSER_TEMT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416}
1417
Linus Walleije643f872012-06-17 15:44:19 +02001418static unsigned int pl011_get_mctrl(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001420 struct uart_amba_port *uap =
1421 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 unsigned int result = 0;
Russell King75836332015-11-03 14:50:58 +00001423 unsigned int status = pl011_read(uap->port.membase, UART01x_FR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424
Jiri Slaby5159f402007-10-18 23:40:31 -07001425#define TIOCMBIT(uartbit, tiocmbit) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 if (status & uartbit) \
1427 result |= tiocmbit
1428
Jiri Slaby5159f402007-10-18 23:40:31 -07001429 TIOCMBIT(UART01x_FR_DCD, TIOCM_CAR);
Greg Kroah-Hartman062a68a2015-09-04 09:11:24 -07001430 TIOCMBIT(UART01x_FR_DSR, TIOCM_DSR);
1431 TIOCMBIT(UART01x_FR_CTS, TIOCM_CTS);
1432 TIOCMBIT(UART011_FR_RI, TIOCM_RNG);
Jiri Slaby5159f402007-10-18 23:40:31 -07001433#undef TIOCMBIT
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 return result;
1435}
1436
1437static void pl011_set_mctrl(struct uart_port *port, unsigned int mctrl)
1438{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001439 struct uart_amba_port *uap =
1440 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 unsigned int cr;
1442
Russell King75836332015-11-03 14:50:58 +00001443 cr = pl011_read(uap->port.membase, UART011_CR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444
Jiri Slaby5159f402007-10-18 23:40:31 -07001445#define TIOCMBIT(tiocmbit, uartbit) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 if (mctrl & tiocmbit) \
1447 cr |= uartbit; \
1448 else \
1449 cr &= ~uartbit
1450
Jiri Slaby5159f402007-10-18 23:40:31 -07001451 TIOCMBIT(TIOCM_RTS, UART011_CR_RTS);
1452 TIOCMBIT(TIOCM_DTR, UART011_CR_DTR);
1453 TIOCMBIT(TIOCM_OUT1, UART011_CR_OUT1);
1454 TIOCMBIT(TIOCM_OUT2, UART011_CR_OUT2);
1455 TIOCMBIT(TIOCM_LOOP, UART011_CR_LBE);
Rabin Vincent3b438162010-02-12 06:43:11 +01001456
1457 if (uap->autorts) {
1458 /* We need to disable auto-RTS if we want to turn RTS off */
1459 TIOCMBIT(TIOCM_RTS, UART011_CR_RTSEN);
1460 }
Jiri Slaby5159f402007-10-18 23:40:31 -07001461#undef TIOCMBIT
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462
Russell King75836332015-11-03 14:50:58 +00001463 pl011_write(cr, uap->port.membase, UART011_CR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464}
1465
1466static void pl011_break_ctl(struct uart_port *port, int break_state)
1467{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001468 struct uart_amba_port *uap =
1469 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 unsigned long flags;
1471 unsigned int lcr_h;
1472
1473 spin_lock_irqsave(&uap->port.lock, flags);
Russell King75836332015-11-03 14:50:58 +00001474 lcr_h = pl011_read(uap->port.membase, uap->lcrh_tx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 if (break_state == -1)
1476 lcr_h |= UART01x_LCRH_BRK;
1477 else
1478 lcr_h &= ~UART01x_LCRH_BRK;
Russell King75836332015-11-03 14:50:58 +00001479 pl011_write(lcr_h, uap->port.membase, uap->lcrh_tx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 spin_unlock_irqrestore(&uap->port.lock, flags);
1481}
1482
Jason Wessel84b5ae12008-02-20 13:33:39 -06001483#ifdef CONFIG_CONSOLE_POLL
Anton Vorontsov5c8124a2012-09-24 14:27:55 -07001484
1485static void pl011_quiesce_irqs(struct uart_port *port)
1486{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001487 struct uart_amba_port *uap =
1488 container_of(port, struct uart_amba_port, port);
Greg Kroah-Hartmanf11c9842015-09-04 09:13:39 -07001489 unsigned char __iomem *regs = uap->port.membase;
Anton Vorontsov5c8124a2012-09-24 14:27:55 -07001490
Russell King75836332015-11-03 14:50:58 +00001491 pl011_write(pl011_read(regs, UART011_MIS), regs, UART011_ICR);
Anton Vorontsov5c8124a2012-09-24 14:27:55 -07001492 /*
1493 * There is no way to clear TXIM as this is "ready to transmit IRQ", so
1494 * we simply mask it. start_tx() will unmask it.
1495 *
1496 * Note we can race with start_tx(), and if the race happens, the
1497 * polling user might get another interrupt just after we clear it.
1498 * But it should be OK and can happen even w/o the race, e.g.
1499 * controller immediately got some new data and raised the IRQ.
1500 *
1501 * And whoever uses polling routines assumes that it manages the device
1502 * (including tx queue), so we're also fine with start_tx()'s caller
1503 * side.
1504 */
Russell King75836332015-11-03 14:50:58 +00001505 pl011_write(pl011_read(regs, UART011_IMSC) & ~UART011_TXIM,
1506 regs, UART011_IMSC);
Anton Vorontsov5c8124a2012-09-24 14:27:55 -07001507}
1508
Linus Walleije643f872012-06-17 15:44:19 +02001509static int pl011_get_poll_char(struct uart_port *port)
Jason Wessel84b5ae12008-02-20 13:33:39 -06001510{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001511 struct uart_amba_port *uap =
1512 container_of(port, struct uart_amba_port, port);
Jason Wessel84b5ae12008-02-20 13:33:39 -06001513 unsigned int status;
1514
Anton Vorontsov5c8124a2012-09-24 14:27:55 -07001515 /*
1516 * The caller might need IRQs lowered, e.g. if used with KDB NMI
1517 * debugger.
1518 */
1519 pl011_quiesce_irqs(port);
1520
Russell King75836332015-11-03 14:50:58 +00001521 status = pl011_read(uap->port.membase, UART01x_FR);
Jason Wesself5316b42010-05-20 21:04:22 -05001522 if (status & UART01x_FR_RXFE)
1523 return NO_POLL_CHAR;
Jason Wessel84b5ae12008-02-20 13:33:39 -06001524
Russell King75836332015-11-03 14:50:58 +00001525 return pl011_read(uap->port.membase, UART01x_DR);
Jason Wessel84b5ae12008-02-20 13:33:39 -06001526}
1527
Linus Walleije643f872012-06-17 15:44:19 +02001528static void pl011_put_poll_char(struct uart_port *port,
Jason Wessel84b5ae12008-02-20 13:33:39 -06001529 unsigned char ch)
1530{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001531 struct uart_amba_port *uap =
1532 container_of(port, struct uart_amba_port, port);
Jason Wessel84b5ae12008-02-20 13:33:39 -06001533
Russell King75836332015-11-03 14:50:58 +00001534 while (pl011_read(uap->port.membase, UART01x_FR) & UART01x_FR_TXFF)
Jason Wessel84b5ae12008-02-20 13:33:39 -06001535 barrier();
1536
Russell King75836332015-11-03 14:50:58 +00001537 pl011_write(ch, uap->port.membase, UART01x_DR);
Jason Wessel84b5ae12008-02-20 13:33:39 -06001538}
1539
1540#endif /* CONFIG_CONSOLE_POLL */
1541
Anton Vorontsovb3564c22012-09-24 14:27:54 -07001542static int pl011_hwinit(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001544 struct uart_amba_port *uap =
1545 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 int retval;
1547
Linus Walleij78d80c52012-05-23 21:18:46 +02001548 /* Optionaly enable pins to be muxed in and configured */
Linus Walleij2b996fc2013-06-05 15:36:42 +02001549 pinctrl_pm_select_default_state(port->dev);
Linus Walleij78d80c52012-05-23 21:18:46 +02001550
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 /*
1552 * Try to enable the clock producer.
1553 */
Julia Lawall1c4c4392012-08-26 18:01:01 +02001554 retval = clk_prepare_enable(uap->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 if (retval)
Tushar Behera7f6d9422014-06-26 15:35:35 +05301556 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557
1558 uap->port.uartclk = clk_get_rate(uap->clk);
1559
Linus Walleij9b96fba2012-03-13 13:27:23 +01001560 /* Clear pending error and receive interrupts */
Russell King75836332015-11-03 14:50:58 +00001561 pl011_write(UART011_OEIS | UART011_BEIS | UART011_PEIS |
1562 UART011_FEIS | UART011_RTIS | UART011_RXIS,
1563 uap->port.membase, UART011_ICR);
Linus Walleij9b96fba2012-03-13 13:27:23 +01001564
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 /*
Anton Vorontsovb3564c22012-09-24 14:27:54 -07001566 * Save interrupts enable mask, and enable RX interrupts in case if
1567 * the interrupt is used for NMI entry.
1568 */
Russell King75836332015-11-03 14:50:58 +00001569 uap->im = pl011_read(uap->port.membase, UART011_IMSC);
1570 pl011_write(UART011_RTIM | UART011_RXIM, uap->port.membase,
1571 UART011_IMSC);
Anton Vorontsovb3564c22012-09-24 14:27:54 -07001572
Jingoo Han574de552013-07-30 17:06:57 +09001573 if (dev_get_platdata(uap->port.dev)) {
Anton Vorontsovb3564c22012-09-24 14:27:54 -07001574 struct amba_pl011_data *plat;
1575
Jingoo Han574de552013-07-30 17:06:57 +09001576 plat = dev_get_platdata(uap->port.dev);
Anton Vorontsovb3564c22012-09-24 14:27:54 -07001577 if (plat->init)
1578 plat->init();
1579 }
1580 return 0;
Anton Vorontsovb3564c22012-09-24 14:27:54 -07001581}
1582
Jon Medhurstb60f2f62013-12-10 10:18:59 +00001583static void pl011_write_lcr_h(struct uart_amba_port *uap, unsigned int lcr_h)
1584{
Russell King75836332015-11-03 14:50:58 +00001585 pl011_write(lcr_h, uap->port.membase, uap->lcrh_rx);
Greg Kroah-Hartman8e502542015-09-04 09:12:03 -07001586 if (uap->lcrh_rx != uap->lcrh_tx) {
Jon Medhurstb60f2f62013-12-10 10:18:59 +00001587 int i;
1588 /*
1589 * Wait 10 PCLKs before writing LCRH_TX register,
1590 * to get this delay write read only register 10 times
1591 */
1592 for (i = 0; i < 10; ++i)
Russell King75836332015-11-03 14:50:58 +00001593 pl011_write(0xff, uap->port.membase, UART011_MIS);
1594 pl011_write(lcr_h, uap->port.membase, uap->lcrh_tx);
Jon Medhurstb60f2f62013-12-10 10:18:59 +00001595 }
1596}
1597
Andre Przywara867b8e82015-05-21 17:26:15 +01001598static int pl011_allocate_irq(struct uart_amba_port *uap)
1599{
Russell King75836332015-11-03 14:50:58 +00001600 pl011_write(uap->im, uap->port.membase, UART011_IMSC);
Andre Przywara867b8e82015-05-21 17:26:15 +01001601
1602 return request_irq(uap->port.irq, pl011_int, 0, "uart-pl011", uap);
1603}
1604
1605/*
1606 * Enable interrupts, only timeouts when using DMA
1607 * if initial RX DMA job failed, start in interrupt mode
1608 * as well.
1609 */
1610static void pl011_enable_interrupts(struct uart_amba_port *uap)
1611{
1612 spin_lock_irq(&uap->port.lock);
1613
1614 /* Clear out any spuriously appearing RX interrupts */
Russell King75836332015-11-03 14:50:58 +00001615 pl011_write(UART011_RTIS | UART011_RXIS, uap->port.membase,
1616 UART011_ICR);
Andre Przywara867b8e82015-05-21 17:26:15 +01001617 uap->im = UART011_RTIM;
1618 if (!pl011_dma_rx_running(uap))
1619 uap->im |= UART011_RXIM;
Russell King75836332015-11-03 14:50:58 +00001620 pl011_write(uap->im, uap->port.membase, UART011_IMSC);
Andre Przywara867b8e82015-05-21 17:26:15 +01001621 spin_unlock_irq(&uap->port.lock);
1622}
1623
Anton Vorontsovb3564c22012-09-24 14:27:54 -07001624static int pl011_startup(struct uart_port *port)
1625{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001626 struct uart_amba_port *uap =
1627 container_of(port, struct uart_amba_port, port);
Dave Martin734745c2015-03-04 12:27:33 +00001628 unsigned int cr;
Anton Vorontsovb3564c22012-09-24 14:27:54 -07001629 int retval;
1630
1631 retval = pl011_hwinit(port);
1632 if (retval)
1633 goto clk_dis;
1634
Andre Przywara867b8e82015-05-21 17:26:15 +01001635 retval = pl011_allocate_irq(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 if (retval)
1637 goto clk_dis;
1638
Russell King75836332015-11-03 14:50:58 +00001639 pl011_write(uap->vendor->ifls, uap->port.membase, UART011_IFLS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640
Jon Medhurstfe433902013-12-10 10:18:58 +00001641 spin_lock_irq(&uap->port.lock);
1642
Shreshtha Kumar Sahud8d8ffa2012-01-18 15:53:59 +05301643 /* restore RTS and DTR */
1644 cr = uap->old_cr & (UART011_CR_RTS | UART011_CR_DTR);
1645 cr |= UART01x_CR_UARTEN | UART011_CR_RXE | UART011_CR_TXE;
Russell King75836332015-11-03 14:50:58 +00001646 pl011_write(cr, uap->port.membase, UART011_CR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647
Jon Medhurstfe433902013-12-10 10:18:58 +00001648 spin_unlock_irq(&uap->port.lock);
1649
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 /*
1651 * initialise the old status of the modem signals
1652 */
Russell King75836332015-11-03 14:50:58 +00001653 uap->old_status = pl011_read(uap->port.membase, UART01x_FR) & UART01x_FR_MODEM_ANY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654
Russell King68b65f72010-12-22 17:24:39 +00001655 /* Startup DMA */
1656 pl011_dma_startup(uap);
1657
Andre Przywara867b8e82015-05-21 17:26:15 +01001658 pl011_enable_interrupts(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659
1660 return 0;
1661
1662 clk_dis:
Julia Lawall1c4c4392012-08-26 18:01:01 +02001663 clk_disable_unprepare(uap->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 return retval;
1665}
1666
Andre Przywara0dd1e242015-05-21 17:26:23 +01001667static int sbsa_uart_startup(struct uart_port *port)
1668{
1669 struct uart_amba_port *uap =
1670 container_of(port, struct uart_amba_port, port);
1671 int retval;
1672
1673 retval = pl011_hwinit(port);
1674 if (retval)
1675 return retval;
1676
1677 retval = pl011_allocate_irq(uap);
1678 if (retval)
1679 return retval;
1680
1681 /* The SBSA UART does not support any modem status lines. */
1682 uap->old_status = 0;
1683
1684 pl011_enable_interrupts(uap);
1685
1686 return 0;
1687}
1688
Linus Walleijec489aa2010-06-02 08:13:52 +01001689static void pl011_shutdown_channel(struct uart_amba_port *uap,
1690 unsigned int lcrh)
1691{
Greg Kroah-Hartmanf11c9842015-09-04 09:13:39 -07001692 unsigned long val;
Linus Walleijec489aa2010-06-02 08:13:52 +01001693
Russell King75836332015-11-03 14:50:58 +00001694 val = pl011_read(uap->port.membase, lcrh);
Greg Kroah-Hartmanf11c9842015-09-04 09:13:39 -07001695 val &= ~(UART01x_LCRH_BRK | UART01x_LCRH_FEN);
Russell King75836332015-11-03 14:50:58 +00001696 pl011_write(val, uap->port.membase, lcrh);
Linus Walleijec489aa2010-06-02 08:13:52 +01001697}
1698
Andre Przywara95166a32015-05-21 17:26:16 +01001699/*
1700 * disable the port. It should not disable RTS and DTR.
1701 * Also RTS and DTR state should be preserved to restore
1702 * it during startup().
1703 */
1704static void pl011_disable_uart(struct uart_amba_port *uap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705{
Shreshtha Kumar Sahud8d8ffa2012-01-18 15:53:59 +05301706 unsigned int cr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707
Rabin Vincent3b438162010-02-12 06:43:11 +01001708 uap->autorts = false;
Jon Medhurstfe433902013-12-10 10:18:58 +00001709 spin_lock_irq(&uap->port.lock);
Russell King75836332015-11-03 14:50:58 +00001710 cr = pl011_read(uap->port.membase, UART011_CR);
Shreshtha Kumar Sahud8d8ffa2012-01-18 15:53:59 +05301711 uap->old_cr = cr;
1712 cr &= UART011_CR_RTS | UART011_CR_DTR;
1713 cr |= UART01x_CR_UARTEN | UART011_CR_TXE;
Russell King75836332015-11-03 14:50:58 +00001714 pl011_write(cr, uap->port.membase, UART011_CR);
Jon Medhurstfe433902013-12-10 10:18:58 +00001715 spin_unlock_irq(&uap->port.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716
1717 /*
1718 * disable break condition and fifos
1719 */
Linus Walleijec489aa2010-06-02 08:13:52 +01001720 pl011_shutdown_channel(uap, uap->lcrh_rx);
Greg Kroah-Hartman8e502542015-09-04 09:12:03 -07001721 if (uap->lcrh_rx != uap->lcrh_tx)
Linus Walleijec489aa2010-06-02 08:13:52 +01001722 pl011_shutdown_channel(uap, uap->lcrh_tx);
Andre Przywara95166a32015-05-21 17:26:16 +01001723}
1724
1725static void pl011_disable_interrupts(struct uart_amba_port *uap)
1726{
1727 spin_lock_irq(&uap->port.lock);
1728
1729 /* mask all interrupts and clear all pending ones */
1730 uap->im = 0;
Russell King75836332015-11-03 14:50:58 +00001731 pl011_write(uap->im, uap->port.membase, UART011_IMSC);
1732 pl011_write(0xffff, uap->port.membase, UART011_ICR);
Andre Przywara95166a32015-05-21 17:26:16 +01001733
1734 spin_unlock_irq(&uap->port.lock);
1735}
1736
1737static void pl011_shutdown(struct uart_port *port)
1738{
1739 struct uart_amba_port *uap =
1740 container_of(port, struct uart_amba_port, port);
1741
1742 pl011_disable_interrupts(uap);
1743
1744 pl011_dma_shutdown(uap);
1745
1746 free_irq(uap->port.irq, uap);
1747
1748 pl011_disable_uart(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749
1750 /*
1751 * Shut down the clock producer
1752 */
Julia Lawall1c4c4392012-08-26 18:01:01 +02001753 clk_disable_unprepare(uap->clk);
Linus Walleij78d80c52012-05-23 21:18:46 +02001754 /* Optionally let pins go into sleep states */
Linus Walleij2b996fc2013-06-05 15:36:42 +02001755 pinctrl_pm_select_sleep_state(port->dev);
Shreshtha Kumar Sahuc16d51a2011-06-13 10:11:33 +02001756
Jingoo Han574de552013-07-30 17:06:57 +09001757 if (dev_get_platdata(uap->port.dev)) {
Shreshtha Kumar Sahuc16d51a2011-06-13 10:11:33 +02001758 struct amba_pl011_data *plat;
1759
Jingoo Han574de552013-07-30 17:06:57 +09001760 plat = dev_get_platdata(uap->port.dev);
Shreshtha Kumar Sahuc16d51a2011-06-13 10:11:33 +02001761 if (plat->exit)
1762 plat->exit();
1763 }
1764
Peter Hurley36f339d2014-11-06 09:06:12 -05001765 if (uap->port.ops->flush_buffer)
1766 uap->port.ops->flush_buffer(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767}
1768
Andre Przywara0dd1e242015-05-21 17:26:23 +01001769static void sbsa_uart_shutdown(struct uart_port *port)
1770{
1771 struct uart_amba_port *uap =
1772 container_of(port, struct uart_amba_port, port);
1773
1774 pl011_disable_interrupts(uap);
1775
1776 free_irq(uap->port.irq, uap);
1777
1778 if (uap->port.ops->flush_buffer)
1779 uap->port.ops->flush_buffer(port);
1780}
1781
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782static void
Andre Przywaraef5a9352015-05-21 17:26:17 +01001783pl011_setup_status_masks(struct uart_port *port, struct ktermios *termios)
1784{
1785 port->read_status_mask = UART011_DR_OE | 255;
1786 if (termios->c_iflag & INPCK)
1787 port->read_status_mask |= UART011_DR_FE | UART011_DR_PE;
1788 if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
1789 port->read_status_mask |= UART011_DR_BE;
1790
1791 /*
1792 * Characters to ignore
1793 */
1794 port->ignore_status_mask = 0;
1795 if (termios->c_iflag & IGNPAR)
1796 port->ignore_status_mask |= UART011_DR_FE | UART011_DR_PE;
1797 if (termios->c_iflag & IGNBRK) {
1798 port->ignore_status_mask |= UART011_DR_BE;
1799 /*
1800 * If we're ignoring parity and break indicators,
1801 * ignore overruns too (for real raw support).
1802 */
1803 if (termios->c_iflag & IGNPAR)
1804 port->ignore_status_mask |= UART011_DR_OE;
1805 }
1806
1807 /*
1808 * Ignore all characters if CREAD is not set.
1809 */
1810 if ((termios->c_cflag & CREAD) == 0)
1811 port->ignore_status_mask |= UART_DUMMY_DR_RX;
1812}
1813
1814static void
Alan Cox606d0992006-12-08 02:38:45 -08001815pl011_set_termios(struct uart_port *port, struct ktermios *termios,
1816 struct ktermios *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001818 struct uart_amba_port *uap =
1819 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 unsigned int lcr_h, old_cr;
1821 unsigned long flags;
Russell Kingc19f12b2010-12-22 17:48:26 +00001822 unsigned int baud, quot, clkdiv;
1823
1824 if (uap->vendor->oversampling)
1825 clkdiv = 8;
1826 else
1827 clkdiv = 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828
1829 /*
1830 * Ask the core to calculate the divisor for us.
1831 */
Linus Walleijac3e3fb2010-06-02 20:40:22 +01001832 baud = uart_get_baud_rate(port, termios, old, 0,
Russell Kingc19f12b2010-12-22 17:48:26 +00001833 port->uartclk / clkdiv);
Chanho Min89fa28d2013-04-03 11:10:37 +09001834#ifdef CONFIG_DMA_ENGINE
Chanho Mincb06ff12013-03-27 18:38:11 +09001835 /*
1836 * Adjust RX DMA polling rate with baud rate if not specified.
1837 */
1838 if (uap->dmarx.auto_poll_rate)
1839 uap->dmarx.poll_rate = DIV_ROUND_UP(10000000, baud);
Chanho Min89fa28d2013-04-03 11:10:37 +09001840#endif
Linus Walleijac3e3fb2010-06-02 20:40:22 +01001841
1842 if (baud > port->uartclk/16)
1843 quot = DIV_ROUND_CLOSEST(port->uartclk * 8, baud);
1844 else
1845 quot = DIV_ROUND_CLOSEST(port->uartclk * 4, baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846
1847 switch (termios->c_cflag & CSIZE) {
1848 case CS5:
1849 lcr_h = UART01x_LCRH_WLEN_5;
1850 break;
1851 case CS6:
1852 lcr_h = UART01x_LCRH_WLEN_6;
1853 break;
1854 case CS7:
1855 lcr_h = UART01x_LCRH_WLEN_7;
1856 break;
1857 default: // CS8
1858 lcr_h = UART01x_LCRH_WLEN_8;
1859 break;
1860 }
1861 if (termios->c_cflag & CSTOPB)
1862 lcr_h |= UART01x_LCRH_STP2;
1863 if (termios->c_cflag & PARENB) {
1864 lcr_h |= UART01x_LCRH_PEN;
1865 if (!(termios->c_cflag & PARODD))
1866 lcr_h |= UART01x_LCRH_EPS;
1867 }
Russell Kingffca2b12010-12-22 17:13:05 +00001868 if (uap->fifosize > 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 lcr_h |= UART01x_LCRH_FEN;
1870
1871 spin_lock_irqsave(&port->lock, flags);
1872
1873 /*
1874 * Update the per-port timeout.
1875 */
1876 uart_update_timeout(port, termios->c_cflag, baud);
1877
Andre Przywaraef5a9352015-05-21 17:26:17 +01001878 pl011_setup_status_masks(port, termios);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879
1880 if (UART_ENABLE_MS(port, termios->c_cflag))
1881 pl011_enable_ms(port);
1882
1883 /* first, disable everything */
Russell King75836332015-11-03 14:50:58 +00001884 old_cr = pl011_read(port->membase, UART011_CR);
1885 pl011_write(0, port->membase, UART011_CR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886
Rabin Vincent3b438162010-02-12 06:43:11 +01001887 if (termios->c_cflag & CRTSCTS) {
1888 if (old_cr & UART011_CR_RTS)
1889 old_cr |= UART011_CR_RTSEN;
1890
1891 old_cr |= UART011_CR_CTSEN;
1892 uap->autorts = true;
1893 } else {
1894 old_cr &= ~(UART011_CR_CTSEN | UART011_CR_RTSEN);
1895 uap->autorts = false;
1896 }
1897
Russell Kingc19f12b2010-12-22 17:48:26 +00001898 if (uap->vendor->oversampling) {
1899 if (baud > port->uartclk / 16)
Linus Walleijac3e3fb2010-06-02 20:40:22 +01001900 old_cr |= ST_UART011_CR_OVSFACT;
1901 else
1902 old_cr &= ~ST_UART011_CR_OVSFACT;
1903 }
1904
Linus Walleijc5dd5532012-09-26 17:21:36 +02001905 /*
1906 * Workaround for the ST Micro oversampling variants to
1907 * increase the bitrate slightly, by lowering the divisor,
1908 * to avoid delayed sampling of start bit at high speeds,
1909 * else we see data corruption.
1910 */
1911 if (uap->vendor->oversampling) {
1912 if ((baud >= 3000000) && (baud < 3250000) && (quot > 1))
1913 quot -= 1;
1914 else if ((baud > 3250000) && (quot > 2))
1915 quot -= 2;
1916 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 /* Set baud rate */
Russell King75836332015-11-03 14:50:58 +00001918 pl011_write(quot & 0x3f, port->membase, UART011_FBRD);
1919 pl011_write(quot >> 6, port->membase, UART011_IBRD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920
1921 /*
1922 * ----------v----------v----------v----------v-----
Linus Walleijc5dd5532012-09-26 17:21:36 +02001923 * NOTE: lcrh_tx and lcrh_rx MUST BE WRITTEN AFTER
Greg Kroah-Hartman0de6cfb2015-09-04 09:13:56 -07001924 * UART011_FBRD & UART011_IBRD.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 * ----------^----------^----------^----------^-----
1926 */
Jon Medhurstb60f2f62013-12-10 10:18:59 +00001927 pl011_write_lcr_h(uap, lcr_h);
Russell King75836332015-11-03 14:50:58 +00001928 pl011_write(old_cr, port->membase, UART011_CR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929
1930 spin_unlock_irqrestore(&port->lock, flags);
1931}
1932
Andre Przywara0dd1e242015-05-21 17:26:23 +01001933static void
1934sbsa_uart_set_termios(struct uart_port *port, struct ktermios *termios,
1935 struct ktermios *old)
1936{
1937 struct uart_amba_port *uap =
1938 container_of(port, struct uart_amba_port, port);
1939 unsigned long flags;
1940
1941 tty_termios_encode_baud_rate(termios, uap->fixed_baud, uap->fixed_baud);
1942
1943 /* The SBSA UART only supports 8n1 without hardware flow control. */
1944 termios->c_cflag &= ~(CSIZE | CSTOPB | PARENB | PARODD);
1945 termios->c_cflag &= ~(CMSPAR | CRTSCTS);
1946 termios->c_cflag |= CS8 | CLOCAL;
1947
1948 spin_lock_irqsave(&port->lock, flags);
1949 uart_update_timeout(port, CS8, uap->fixed_baud);
1950 pl011_setup_status_masks(port, termios);
1951 spin_unlock_irqrestore(&port->lock, flags);
1952}
1953
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954static const char *pl011_type(struct uart_port *port)
1955{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001956 struct uart_amba_port *uap =
1957 container_of(port, struct uart_amba_port, port);
Russell Kinge8a7ba82010-12-28 09:16:54 +00001958 return uap->port.type == PORT_AMBA ? uap->type : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959}
1960
1961/*
1962 * Release the memory region(s) being used by 'port'
1963 */
Linus Walleije643f872012-06-17 15:44:19 +02001964static void pl011_release_port(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965{
1966 release_mem_region(port->mapbase, SZ_4K);
1967}
1968
1969/*
1970 * Request the memory region(s) being used by 'port'
1971 */
Linus Walleije643f872012-06-17 15:44:19 +02001972static int pl011_request_port(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973{
1974 return request_mem_region(port->mapbase, SZ_4K, "uart-pl011")
1975 != NULL ? 0 : -EBUSY;
1976}
1977
1978/*
1979 * Configure/autoconfigure the port.
1980 */
Linus Walleije643f872012-06-17 15:44:19 +02001981static void pl011_config_port(struct uart_port *port, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982{
1983 if (flags & UART_CONFIG_TYPE) {
1984 port->type = PORT_AMBA;
Linus Walleije643f872012-06-17 15:44:19 +02001985 pl011_request_port(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 }
1987}
1988
1989/*
1990 * verify the new serial_struct (for TIOCSSERIAL).
1991 */
Linus Walleije643f872012-06-17 15:44:19 +02001992static int pl011_verify_port(struct uart_port *port, struct serial_struct *ser)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993{
1994 int ret = 0;
1995 if (ser->type != PORT_UNKNOWN && ser->type != PORT_AMBA)
1996 ret = -EINVAL;
Yinghai Lua62c4132008-08-19 20:49:55 -07001997 if (ser->irq < 0 || ser->irq >= nr_irqs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 ret = -EINVAL;
1999 if (ser->baud_base < 9600)
2000 ret = -EINVAL;
2001 return ret;
2002}
2003
2004static struct uart_ops amba_pl011_pops = {
Linus Walleije643f872012-06-17 15:44:19 +02002005 .tx_empty = pl011_tx_empty,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 .set_mctrl = pl011_set_mctrl,
Linus Walleije643f872012-06-17 15:44:19 +02002007 .get_mctrl = pl011_get_mctrl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 .stop_tx = pl011_stop_tx,
2009 .start_tx = pl011_start_tx,
2010 .stop_rx = pl011_stop_rx,
2011 .enable_ms = pl011_enable_ms,
2012 .break_ctl = pl011_break_ctl,
2013 .startup = pl011_startup,
2014 .shutdown = pl011_shutdown,
Russell King68b65f72010-12-22 17:24:39 +00002015 .flush_buffer = pl011_dma_flush_buffer,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 .set_termios = pl011_set_termios,
2017 .type = pl011_type,
Linus Walleije643f872012-06-17 15:44:19 +02002018 .release_port = pl011_release_port,
2019 .request_port = pl011_request_port,
2020 .config_port = pl011_config_port,
2021 .verify_port = pl011_verify_port,
Jason Wessel84b5ae12008-02-20 13:33:39 -06002022#ifdef CONFIG_CONSOLE_POLL
Anton Vorontsovb3564c22012-09-24 14:27:54 -07002023 .poll_init = pl011_hwinit,
Linus Walleije643f872012-06-17 15:44:19 +02002024 .poll_get_char = pl011_get_poll_char,
2025 .poll_put_char = pl011_put_poll_char,
Jason Wessel84b5ae12008-02-20 13:33:39 -06002026#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027};
2028
Andre Przywara0dd1e242015-05-21 17:26:23 +01002029static void sbsa_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
2030{
2031}
2032
2033static unsigned int sbsa_uart_get_mctrl(struct uart_port *port)
2034{
2035 return 0;
2036}
2037
2038static const struct uart_ops sbsa_uart_pops = {
2039 .tx_empty = pl011_tx_empty,
2040 .set_mctrl = sbsa_uart_set_mctrl,
2041 .get_mctrl = sbsa_uart_get_mctrl,
2042 .stop_tx = pl011_stop_tx,
2043 .start_tx = pl011_start_tx,
2044 .stop_rx = pl011_stop_rx,
2045 .startup = sbsa_uart_startup,
2046 .shutdown = sbsa_uart_shutdown,
2047 .set_termios = sbsa_uart_set_termios,
2048 .type = pl011_type,
2049 .release_port = pl011_release_port,
2050 .request_port = pl011_request_port,
2051 .config_port = pl011_config_port,
2052 .verify_port = pl011_verify_port,
2053#ifdef CONFIG_CONSOLE_POLL
2054 .poll_init = pl011_hwinit,
2055 .poll_get_char = pl011_get_poll_char,
2056 .poll_put_char = pl011_put_poll_char,
2057#endif
2058};
2059
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060static struct uart_amba_port *amba_ports[UART_NR];
2061
2062#ifdef CONFIG_SERIAL_AMBA_PL011_CONSOLE
2063
Russell Kingd3587882006-03-20 20:00:09 +00002064static void pl011_console_putchar(struct uart_port *port, int ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065{
Daniel Thompsona5820c22014-09-03 12:51:55 +01002066 struct uart_amba_port *uap =
2067 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068
Russell King75836332015-11-03 14:50:58 +00002069 while (pl011_read(uap->port.membase, UART01x_FR) & UART01x_FR_TXFF)
Russell Kingd3587882006-03-20 20:00:09 +00002070 barrier();
Russell King75836332015-11-03 14:50:58 +00002071 pl011_write(ch, uap->port.membase, UART01x_DR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072}
2073
2074static void
2075pl011_console_write(struct console *co, const char *s, unsigned int count)
2076{
2077 struct uart_amba_port *uap = amba_ports[co->index];
Andre Przywara71eec482015-05-21 17:26:21 +01002078 unsigned int status, old_cr = 0, new_cr;
Rabin Vincentef605fd2012-01-17 11:52:28 +01002079 unsigned long flags;
2080 int locked = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081
2082 clk_enable(uap->clk);
2083
Rabin Vincentef605fd2012-01-17 11:52:28 +01002084 local_irq_save(flags);
2085 if (uap->port.sysrq)
2086 locked = 0;
2087 else if (oops_in_progress)
2088 locked = spin_trylock(&uap->port.lock);
2089 else
2090 spin_lock(&uap->port.lock);
2091
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 /*
2093 * First save the CR then disable the interrupts
2094 */
Andre Przywara71eec482015-05-21 17:26:21 +01002095 if (!uap->vendor->always_enabled) {
Russell King75836332015-11-03 14:50:58 +00002096 old_cr = pl011_read(uap->port.membase, UART011_CR);
Andre Przywara71eec482015-05-21 17:26:21 +01002097 new_cr = old_cr & ~UART011_CR_CTSEN;
2098 new_cr |= UART01x_CR_UARTEN | UART011_CR_TXE;
Russell King75836332015-11-03 14:50:58 +00002099 pl011_write(new_cr, uap->port.membase, UART011_CR);
Andre Przywara71eec482015-05-21 17:26:21 +01002100 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101
Russell Kingd3587882006-03-20 20:00:09 +00002102 uart_console_write(&uap->port, s, count, pl011_console_putchar);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103
2104 /*
2105 * Finally, wait for transmitter to become empty
2106 * and restore the TCR
2107 */
2108 do {
Russell King75836332015-11-03 14:50:58 +00002109 status = pl011_read(uap->port.membase, UART01x_FR);
Greg Kroah-Hartman062a68a2015-09-04 09:11:24 -07002110 } while (status & UART01x_FR_BUSY);
Andre Przywara71eec482015-05-21 17:26:21 +01002111 if (!uap->vendor->always_enabled)
Russell King75836332015-11-03 14:50:58 +00002112 pl011_write(old_cr, uap->port.membase, UART011_CR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113
Rabin Vincentef605fd2012-01-17 11:52:28 +01002114 if (locked)
2115 spin_unlock(&uap->port.lock);
2116 local_irq_restore(flags);
2117
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 clk_disable(uap->clk);
2119}
2120
2121static void __init
2122pl011_console_get_options(struct uart_amba_port *uap, int *baud,
2123 int *parity, int *bits)
2124{
Russell King75836332015-11-03 14:50:58 +00002125 if (pl011_read(uap->port.membase, UART011_CR) & UART01x_CR_UARTEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 unsigned int lcr_h, ibrd, fbrd;
2127
Russell King75836332015-11-03 14:50:58 +00002128 lcr_h = pl011_read(uap->port.membase, uap->lcrh_tx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129
2130 *parity = 'n';
2131 if (lcr_h & UART01x_LCRH_PEN) {
2132 if (lcr_h & UART01x_LCRH_EPS)
2133 *parity = 'e';
2134 else
2135 *parity = 'o';
2136 }
2137
2138 if ((lcr_h & 0x60) == UART01x_LCRH_WLEN_7)
2139 *bits = 7;
2140 else
2141 *bits = 8;
2142
Russell King75836332015-11-03 14:50:58 +00002143 ibrd = pl011_read(uap->port.membase, UART011_IBRD);
2144 fbrd = pl011_read(uap->port.membase, UART011_FBRD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145
2146 *baud = uap->port.uartclk * 4 / (64 * ibrd + fbrd);
Linus Walleijac3e3fb2010-06-02 20:40:22 +01002147
Russell Kingc19f12b2010-12-22 17:48:26 +00002148 if (uap->vendor->oversampling) {
Russell King75836332015-11-03 14:50:58 +00002149 if (pl011_read(uap->port.membase, UART011_CR)
Linus Walleijac3e3fb2010-06-02 20:40:22 +01002150 & ST_UART011_CR_OVSFACT)
2151 *baud *= 2;
2152 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 }
2154}
2155
2156static int __init pl011_console_setup(struct console *co, char *options)
2157{
2158 struct uart_amba_port *uap;
2159 int baud = 38400;
2160 int bits = 8;
2161 int parity = 'n';
2162 int flow = 'n';
Russell King4b4851c2011-09-22 11:35:30 +01002163 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164
2165 /*
2166 * Check whether an invalid uart number has been specified, and
2167 * if so, search for the first available port that does have
2168 * console support.
2169 */
2170 if (co->index >= UART_NR)
2171 co->index = 0;
2172 uap = amba_ports[co->index];
Russell Kingd28122a2007-01-22 18:59:42 +00002173 if (!uap)
2174 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175
Linus Walleij78d80c52012-05-23 21:18:46 +02002176 /* Allow pins to be muxed in and configured */
Linus Walleij2b996fc2013-06-05 15:36:42 +02002177 pinctrl_pm_select_default_state(uap->port.dev);
Linus Walleij78d80c52012-05-23 21:18:46 +02002178
Russell King4b4851c2011-09-22 11:35:30 +01002179 ret = clk_prepare(uap->clk);
2180 if (ret)
2181 return ret;
2182
Jingoo Han574de552013-07-30 17:06:57 +09002183 if (dev_get_platdata(uap->port.dev)) {
Shreshtha Kumar Sahuc16d51a2011-06-13 10:11:33 +02002184 struct amba_pl011_data *plat;
2185
Jingoo Han574de552013-07-30 17:06:57 +09002186 plat = dev_get_platdata(uap->port.dev);
Shreshtha Kumar Sahuc16d51a2011-06-13 10:11:33 +02002187 if (plat->init)
2188 plat->init();
2189 }
2190
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 uap->port.uartclk = clk_get_rate(uap->clk);
2192
Andre Przywaracefc2d12015-05-21 17:26:22 +01002193 if (uap->vendor->fixed_options) {
2194 baud = uap->fixed_baud;
2195 } else {
2196 if (options)
2197 uart_parse_options(options,
2198 &baud, &parity, &bits, &flow);
2199 else
2200 pl011_console_get_options(uap, &baud, &parity, &bits);
2201 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202
2203 return uart_set_options(&uap->port, co, baud, parity, bits, flow);
2204}
2205
Vincent Sanders2d934862005-09-14 22:36:03 +01002206static struct uart_driver amba_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207static struct console amba_console = {
2208 .name = "ttyAMA",
2209 .write = pl011_console_write,
2210 .device = uart_console_device,
2211 .setup = pl011_console_setup,
2212 .flags = CON_PRINTBUFFER,
2213 .index = -1,
2214 .data = &amba_reg,
2215};
2216
2217#define AMBA_CONSOLE (&amba_console)
Rob Herring0d3c6732014-04-18 17:19:57 -05002218
2219static void pl011_putc(struct uart_port *port, int c)
2220{
Greg Kroah-Hartman0de6cfb2015-09-04 09:13:56 -07002221 while (readl(port->membase + UART01x_FR) & UART01x_FR_TXFF)
Rob Herring0d3c6732014-04-18 17:19:57 -05002222 ;
Greg Kroah-Hartman0de6cfb2015-09-04 09:13:56 -07002223 writeb(c, port->membase + UART01x_DR);
2224 while (readl(port->membase + UART01x_FR) & UART01x_FR_BUSY)
Rob Herring0d3c6732014-04-18 17:19:57 -05002225 ;
2226}
2227
2228static void pl011_early_write(struct console *con, const char *s, unsigned n)
2229{
2230 struct earlycon_device *dev = con->data;
2231
2232 uart_console_write(&dev->port, s, n, pl011_putc);
2233}
2234
2235static int __init pl011_early_console_setup(struct earlycon_device *device,
2236 const char *opt)
2237{
2238 if (!device->port.membase)
2239 return -ENODEV;
2240
2241 device->con->write = pl011_early_write;
2242 return 0;
2243}
2244EARLYCON_DECLARE(pl011, pl011_early_console_setup);
Rob Herring45e0f0f2014-03-27 08:08:03 -05002245OF_EARLYCON_DECLARE(pl011, "arm,pl011", pl011_early_console_setup);
Rob Herring0d3c6732014-04-18 17:19:57 -05002246
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247#else
2248#define AMBA_CONSOLE NULL
2249#endif
2250
2251static struct uart_driver amba_reg = {
2252 .owner = THIS_MODULE,
2253 .driver_name = "ttyAMA",
2254 .dev_name = "ttyAMA",
2255 .major = SERIAL_AMBA_MAJOR,
2256 .minor = SERIAL_AMBA_MINOR,
2257 .nr = UART_NR,
2258 .cons = AMBA_CONSOLE,
2259};
2260
Matthew Leach32614aa2012-08-28 16:41:28 +01002261static int pl011_probe_dt_alias(int index, struct device *dev)
2262{
2263 struct device_node *np;
2264 static bool seen_dev_with_alias = false;
2265 static bool seen_dev_without_alias = false;
2266 int ret = index;
2267
2268 if (!IS_ENABLED(CONFIG_OF))
2269 return ret;
2270
2271 np = dev->of_node;
2272 if (!np)
2273 return ret;
2274
2275 ret = of_alias_get_id(np, "serial");
2276 if (IS_ERR_VALUE(ret)) {
2277 seen_dev_without_alias = true;
2278 ret = index;
2279 } else {
2280 seen_dev_with_alias = true;
2281 if (ret >= ARRAY_SIZE(amba_ports) || amba_ports[ret] != NULL) {
2282 dev_warn(dev, "requested serial port %d not available.\n", ret);
2283 ret = index;
2284 }
2285 }
2286
2287 if (seen_dev_with_alias && seen_dev_without_alias)
2288 dev_warn(dev, "aliased and non-aliased serial devices found in device tree. Serial port enumeration may be unpredictable.\n");
2289
2290 return ret;
2291}
2292
Andre Przywara49bb3c82015-05-21 17:26:14 +01002293/* unregisters the driver also if no more ports are left */
2294static void pl011_unregister_port(struct uart_amba_port *uap)
2295{
2296 int i;
2297 bool busy = false;
2298
2299 for (i = 0; i < ARRAY_SIZE(amba_ports); i++) {
2300 if (amba_ports[i] == uap)
2301 amba_ports[i] = NULL;
2302 else if (amba_ports[i])
2303 busy = true;
2304 }
2305 pl011_dma_remove(uap);
2306 if (!busy)
2307 uart_unregister_driver(&amba_reg);
2308}
2309
Andre Przywara3873e2d2015-05-21 17:26:18 +01002310static int pl011_find_free_port(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311{
Andre Przywara3873e2d2015-05-21 17:26:18 +01002312 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313
2314 for (i = 0; i < ARRAY_SIZE(amba_ports); i++)
2315 if (amba_ports[i] == NULL)
Andre Przywara3873e2d2015-05-21 17:26:18 +01002316 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317
Andre Przywara3873e2d2015-05-21 17:26:18 +01002318 return -EBUSY;
2319}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320
Andre Przywara3873e2d2015-05-21 17:26:18 +01002321static int pl011_setup_port(struct device *dev, struct uart_amba_port *uap,
2322 struct resource *mmiobase, int index)
2323{
2324 void __iomem *base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325
Andre Przywara3873e2d2015-05-21 17:26:18 +01002326 base = devm_ioremap_resource(dev, mmiobase);
Krzysztof Kozlowski97a60ea2015-07-09 22:21:41 +09002327 if (IS_ERR(base))
2328 return PTR_ERR(base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329
Andre Przywara3873e2d2015-05-21 17:26:18 +01002330 index = pl011_probe_dt_alias(index, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331
Shreshtha Kumar Sahud8d8ffa2012-01-18 15:53:59 +05302332 uap->old_cr = 0;
Andre Przywara3873e2d2015-05-21 17:26:18 +01002333 uap->port.dev = dev;
2334 uap->port.mapbase = mmiobase->start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 uap->port.membase = base;
2336 uap->port.iotype = UPIO_MEM;
Russell Kingffca2b12010-12-22 17:13:05 +00002337 uap->port.fifosize = uap->fifosize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 uap->port.flags = UPF_BOOT_AUTOCONF;
Andre Przywara3873e2d2015-05-21 17:26:18 +01002339 uap->port.line = index;
2340
2341 amba_ports[index] = uap;
2342
2343 return 0;
2344}
2345
2346static int pl011_register_port(struct uart_amba_port *uap)
2347{
2348 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349
Linus Walleijc3d8b762012-03-21 20:15:18 +01002350 /* Ensure interrupts from this UART are masked and cleared */
Russell King75836332015-11-03 14:50:58 +00002351 pl011_write(0, uap->port.membase, UART011_IMSC);
2352 pl011_write(0xffff, uap->port.membase, UART011_ICR);
Linus Walleijc3d8b762012-03-21 20:15:18 +01002353
Tushar Beheraef2889f2014-01-20 14:32:35 +05302354 if (!amba_reg.state) {
2355 ret = uart_register_driver(&amba_reg);
2356 if (ret < 0) {
Andre Przywara3873e2d2015-05-21 17:26:18 +01002357 dev_err(uap->port.dev,
Jorge Ramirez-Ortiz1c9be312015-03-06 13:05:40 -05002358 "Failed to register AMBA-PL011 driver\n");
Tushar Beheraef2889f2014-01-20 14:32:35 +05302359 return ret;
2360 }
2361 }
2362
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363 ret = uart_add_one_port(&amba_reg, &uap->port);
Andre Przywara49bb3c82015-05-21 17:26:14 +01002364 if (ret)
2365 pl011_unregister_port(uap);
Tushar Behera7f6d9422014-06-26 15:35:35 +05302366
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 return ret;
2368}
2369
Andre Przywara3873e2d2015-05-21 17:26:18 +01002370static int pl011_probe(struct amba_device *dev, const struct amba_id *id)
2371{
2372 struct uart_amba_port *uap;
2373 struct vendor_data *vendor = id->data;
2374 int portnr, ret;
2375
2376 portnr = pl011_find_free_port();
2377 if (portnr < 0)
2378 return portnr;
2379
2380 uap = devm_kzalloc(&dev->dev, sizeof(struct uart_amba_port),
2381 GFP_KERNEL);
2382 if (!uap)
2383 return -ENOMEM;
2384
2385 uap->clk = devm_clk_get(&dev->dev, NULL);
2386 if (IS_ERR(uap->clk))
2387 return PTR_ERR(uap->clk);
2388
2389 uap->vendor = vendor;
2390 uap->lcrh_rx = vendor->lcrh_rx;
2391 uap->lcrh_tx = vendor->lcrh_tx;
2392 uap->fifosize = vendor->get_fifosize(dev);
2393 uap->port.irq = dev->irq[0];
2394 uap->port.ops = &amba_pl011_pops;
2395
2396 snprintf(uap->type, sizeof(uap->type), "PL011 rev%u", amba_rev(dev));
2397
2398 ret = pl011_setup_port(&dev->dev, uap, &dev->res, portnr);
2399 if (ret)
2400 return ret;
2401
2402 amba_set_drvdata(dev, uap);
2403
2404 return pl011_register_port(uap);
2405}
2406
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407static int pl011_remove(struct amba_device *dev)
2408{
2409 struct uart_amba_port *uap = amba_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 uart_remove_one_port(&amba_reg, &uap->port);
Andre Przywara49bb3c82015-05-21 17:26:14 +01002412 pl011_unregister_port(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 return 0;
2414}
2415
Ulf Hanssond0ce8502013-12-03 11:04:28 +01002416#ifdef CONFIG_PM_SLEEP
2417static int pl011_suspend(struct device *dev)
Leo Chenb736b892009-07-28 23:43:33 +01002418{
Ulf Hanssond0ce8502013-12-03 11:04:28 +01002419 struct uart_amba_port *uap = dev_get_drvdata(dev);
Leo Chenb736b892009-07-28 23:43:33 +01002420
2421 if (!uap)
2422 return -EINVAL;
2423
2424 return uart_suspend_port(&amba_reg, &uap->port);
2425}
2426
Ulf Hanssond0ce8502013-12-03 11:04:28 +01002427static int pl011_resume(struct device *dev)
Leo Chenb736b892009-07-28 23:43:33 +01002428{
Ulf Hanssond0ce8502013-12-03 11:04:28 +01002429 struct uart_amba_port *uap = dev_get_drvdata(dev);
Leo Chenb736b892009-07-28 23:43:33 +01002430
2431 if (!uap)
2432 return -EINVAL;
2433
2434 return uart_resume_port(&amba_reg, &uap->port);
2435}
2436#endif
2437
Ulf Hanssond0ce8502013-12-03 11:04:28 +01002438static SIMPLE_DEV_PM_OPS(pl011_dev_pm_ops, pl011_suspend, pl011_resume);
2439
Andre Przywara0dd1e242015-05-21 17:26:23 +01002440static int sbsa_uart_probe(struct platform_device *pdev)
2441{
2442 struct uart_amba_port *uap;
2443 struct resource *r;
2444 int portnr, ret;
2445 int baudrate;
2446
2447 /*
2448 * Check the mandatory baud rate parameter in the DT node early
2449 * so that we can easily exit with the error.
2450 */
2451 if (pdev->dev.of_node) {
2452 struct device_node *np = pdev->dev.of_node;
2453
2454 ret = of_property_read_u32(np, "current-speed", &baudrate);
2455 if (ret)
2456 return ret;
2457 } else {
2458 baudrate = 115200;
2459 }
2460
2461 portnr = pl011_find_free_port();
2462 if (portnr < 0)
2463 return portnr;
2464
2465 uap = devm_kzalloc(&pdev->dev, sizeof(struct uart_amba_port),
2466 GFP_KERNEL);
2467 if (!uap)
2468 return -ENOMEM;
2469
2470 uap->vendor = &vendor_sbsa;
2471 uap->fifosize = 32;
2472 uap->port.irq = platform_get_irq(pdev, 0);
2473 uap->port.ops = &sbsa_uart_pops;
2474 uap->fixed_baud = baudrate;
2475
2476 snprintf(uap->type, sizeof(uap->type), "SBSA");
2477
2478 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2479
2480 ret = pl011_setup_port(&pdev->dev, uap, r, portnr);
2481 if (ret)
2482 return ret;
2483
2484 platform_set_drvdata(pdev, uap);
2485
2486 return pl011_register_port(uap);
2487}
2488
2489static int sbsa_uart_remove(struct platform_device *pdev)
2490{
2491 struct uart_amba_port *uap = platform_get_drvdata(pdev);
2492
2493 uart_remove_one_port(&amba_reg, &uap->port);
2494 pl011_unregister_port(uap);
2495 return 0;
2496}
2497
2498static const struct of_device_id sbsa_uart_of_match[] = {
2499 { .compatible = "arm,sbsa-uart", },
2500 {},
2501};
2502MODULE_DEVICE_TABLE(of, sbsa_uart_of_match);
2503
Graeme Gregory3db9ab02015-05-21 17:26:24 +01002504static const struct acpi_device_id sbsa_uart_acpi_match[] = {
2505 { "ARMH0011", 0 },
2506 {},
2507};
2508MODULE_DEVICE_TABLE(acpi, sbsa_uart_acpi_match);
2509
Andre Przywara0dd1e242015-05-21 17:26:23 +01002510static struct platform_driver arm_sbsa_uart_platform_driver = {
2511 .probe = sbsa_uart_probe,
2512 .remove = sbsa_uart_remove,
2513 .driver = {
2514 .name = "sbsa-uart",
2515 .of_match_table = of_match_ptr(sbsa_uart_of_match),
Graeme Gregory3db9ab02015-05-21 17:26:24 +01002516 .acpi_match_table = ACPI_PTR(sbsa_uart_acpi_match),
Andre Przywara0dd1e242015-05-21 17:26:23 +01002517 },
2518};
2519
Russell King2c39c9e2010-07-27 08:50:16 +01002520static struct amba_id pl011_ids[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 {
2522 .id = 0x00041011,
2523 .mask = 0x000fffff,
Alessandro Rubini5926a292009-06-04 17:43:04 +01002524 .data = &vendor_arm,
2525 },
2526 {
2527 .id = 0x00380802,
2528 .mask = 0x00ffffff,
2529 .data = &vendor_st,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 },
2531 { 0, 0 },
2532};
2533
Dave Martin60f7a332011-10-05 15:15:22 +01002534MODULE_DEVICE_TABLE(amba, pl011_ids);
2535
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536static struct amba_driver pl011_driver = {
2537 .drv = {
2538 .name = "uart-pl011",
Ulf Hanssond0ce8502013-12-03 11:04:28 +01002539 .pm = &pl011_dev_pm_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 },
2541 .id_table = pl011_ids,
2542 .probe = pl011_probe,
2543 .remove = pl011_remove,
2544};
2545
2546static int __init pl011_init(void)
2547{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548 printk(KERN_INFO "Serial: AMBA PL011 UART driver\n");
2549
Andre Przywara0dd1e242015-05-21 17:26:23 +01002550 if (platform_driver_register(&arm_sbsa_uart_platform_driver))
2551 pr_warn("could not register SBSA UART platform driver\n");
Greg Kroah-Hartman062a68a2015-09-04 09:11:24 -07002552 return amba_driver_register(&pl011_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553}
2554
2555static void __exit pl011_exit(void)
2556{
Andre Przywara0dd1e242015-05-21 17:26:23 +01002557 platform_driver_unregister(&arm_sbsa_uart_platform_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558 amba_driver_unregister(&pl011_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559}
2560
Alessandro Rubini4dd9e742009-05-05 05:54:13 +01002561/*
2562 * While this can be a module, if builtin it's most likely the console
2563 * So let's leave module_exit but move module_init to an earlier place
2564 */
2565arch_initcall(pl011_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566module_exit(pl011_exit);
2567
2568MODULE_AUTHOR("ARM Ltd/Deep Blue Solutions Ltd");
2569MODULE_DESCRIPTION("ARM AMBA serial port driver");
2570MODULE_LICENSE("GPL");