blob: 0e7f045b8f4f887e7ba7f30bdf887e6ace6611bf [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 Walleijead76f32011-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 Walleijead76f32011-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 Walleijead76f32011-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 Kingb2a4e242015-11-03 14:51:03 +0000187static unsigned int pl011_read(const struct uart_amba_port *uap,
188 unsigned int reg)
Russell King75836332015-11-03 14:50:58 +0000189{
Russell Kingb2a4e242015-11-03 14:51:03 +0000190 return readw(uap->port.membase + reg);
Russell King75836332015-11-03 14:50:58 +0000191}
192
Russell Kingb2a4e242015-11-03 14:51:03 +0000193static void pl011_write(unsigned int val, const struct uart_amba_port *uap,
194 unsigned int reg)
Russell King75836332015-11-03 14:50:58 +0000195{
Russell Kingb2a4e242015-11-03 14:51:03 +0000196 writew(val, uap->port.membase + reg);
Russell King75836332015-11-03 14:50:58 +0000197}
198
Russell King68b65f72010-12-22 17:24:39 +0000199/*
Linus Walleij29772c42011-02-24 13:21:36 +0100200 * Reads up to 256 characters from the FIFO or until it's empty and
201 * inserts them into the TTY layer. Returns the number of characters
202 * read from the FIFO.
203 */
204static int pl011_fifo_to_tty(struct uart_amba_port *uap)
205{
Timur Tabi71a5cd82015-10-07 15:27:16 -0500206 u16 status;
207 unsigned int ch, flag, max_count = 256;
Linus Walleij29772c42011-02-24 13:21:36 +0100208 int fifotaken = 0;
209
210 while (max_count--) {
Russell Kingb2a4e242015-11-03 14:51:03 +0000211 status = pl011_read(uap, UART01x_FR);
Linus Walleij29772c42011-02-24 13:21:36 +0100212 if (status & UART01x_FR_RXFE)
213 break;
214
215 /* Take chars from the FIFO and update status */
Russell Kingb2a4e242015-11-03 14:51:03 +0000216 ch = pl011_read(uap, UART01x_DR) | UART_DUMMY_DR_RX;
Linus Walleij29772c42011-02-24 13:21:36 +0100217 flag = TTY_NORMAL;
218 uap->port.icount.rx++;
219 fifotaken++;
220
221 if (unlikely(ch & UART_DR_ERROR)) {
222 if (ch & UART011_DR_BE) {
223 ch &= ~(UART011_DR_FE | UART011_DR_PE);
224 uap->port.icount.brk++;
225 if (uart_handle_break(&uap->port))
226 continue;
227 } else if (ch & UART011_DR_PE)
228 uap->port.icount.parity++;
229 else if (ch & UART011_DR_FE)
230 uap->port.icount.frame++;
231 if (ch & UART011_DR_OE)
232 uap->port.icount.overrun++;
233
234 ch &= uap->port.read_status_mask;
235
236 if (ch & UART011_DR_BE)
237 flag = TTY_BREAK;
238 else if (ch & UART011_DR_PE)
239 flag = TTY_PARITY;
240 else if (ch & UART011_DR_FE)
241 flag = TTY_FRAME;
242 }
243
244 if (uart_handle_sysrq_char(&uap->port, ch & 255))
245 continue;
246
247 uart_insert_char(&uap->port, ch, UART011_DR_OE, ch, flag);
248 }
249
250 return fifotaken;
251}
252
253
254/*
Russell King68b65f72010-12-22 17:24:39 +0000255 * All the DMA operation mode stuff goes inside this ifdef.
256 * This assumes that you have a generic DMA device interface,
257 * no custom DMA interfaces are supported.
258 */
259#ifdef CONFIG_DMA_ENGINE
260
261#define PL011_DMA_BUFFER_SIZE PAGE_SIZE
262
Linus Walleijead76f32011-02-24 13:21:08 +0100263static int pl011_sgbuf_init(struct dma_chan *chan, struct pl011_sgbuf *sg,
264 enum dma_data_direction dir)
265{
Chanho Mincb06ff12013-03-27 18:38:11 +0900266 dma_addr_t dma_addr;
267
268 sg->buf = dma_alloc_coherent(chan->device->dev,
269 PL011_DMA_BUFFER_SIZE, &dma_addr, GFP_KERNEL);
Linus Walleijead76f32011-02-24 13:21:08 +0100270 if (!sg->buf)
271 return -ENOMEM;
272
Chanho Mincb06ff12013-03-27 18:38:11 +0900273 sg_init_table(&sg->sg, 1);
274 sg_set_page(&sg->sg, phys_to_page(dma_addr),
275 PL011_DMA_BUFFER_SIZE, offset_in_page(dma_addr));
276 sg_dma_address(&sg->sg) = dma_addr;
Andrew Jacksonc64be922014-11-07 14:14:43 +0000277 sg_dma_len(&sg->sg) = PL011_DMA_BUFFER_SIZE;
Linus Walleijead76f32011-02-24 13:21:08 +0100278
Linus Walleijead76f32011-02-24 13:21:08 +0100279 return 0;
280}
281
282static void pl011_sgbuf_free(struct dma_chan *chan, struct pl011_sgbuf *sg,
283 enum dma_data_direction dir)
284{
285 if (sg->buf) {
Chanho Mincb06ff12013-03-27 18:38:11 +0900286 dma_free_coherent(chan->device->dev,
287 PL011_DMA_BUFFER_SIZE, sg->buf,
288 sg_dma_address(&sg->sg));
Linus Walleijead76f32011-02-24 13:21:08 +0100289 }
290}
291
Jorge Ramirez-Ortiz1c9be312015-03-06 13:05:40 -0500292static void pl011_dma_probe(struct uart_amba_port *uap)
Russell King68b65f72010-12-22 17:24:39 +0000293{
294 /* DMA is the sole user of the platform data right now */
Jingoo Han574de552013-07-30 17:06:57 +0900295 struct amba_pl011_data *plat = dev_get_platdata(uap->port.dev);
Jorge Ramirez-Ortiz1c9be312015-03-06 13:05:40 -0500296 struct device *dev = uap->port.dev;
Russell King68b65f72010-12-22 17:24:39 +0000297 struct dma_slave_config tx_conf = {
Greg Kroah-Hartman0de6cfb2015-09-04 09:13:56 -0700298 .dst_addr = uap->port.mapbase + UART01x_DR,
Russell King68b65f72010-12-22 17:24:39 +0000299 .dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE,
Vinod Koula485df42011-10-14 10:47:38 +0530300 .direction = DMA_MEM_TO_DEV,
Russell King68b65f72010-12-22 17:24:39 +0000301 .dst_maxburst = uap->fifosize >> 1,
Viresh Kumar258aea72012-02-01 16:12:19 +0530302 .device_fc = false,
Russell King68b65f72010-12-22 17:24:39 +0000303 };
304 struct dma_chan *chan;
305 dma_cap_mask_t mask;
306
Jorge Ramirez-Ortiz1c9be312015-03-06 13:05:40 -0500307 uap->dma_probed = true;
308 chan = dma_request_slave_channel_reason(dev, "tx");
309 if (IS_ERR(chan)) {
310 if (PTR_ERR(chan) == -EPROBE_DEFER) {
Jorge Ramirez-Ortiz1c9be312015-03-06 13:05:40 -0500311 uap->dma_probed = false;
312 return;
313 }
Russell King68b65f72010-12-22 17:24:39 +0000314
Arnd Bergmann787b0c12013-01-28 16:24:37 +0000315 /* We need platform data */
316 if (!plat || !plat->dma_filter) {
317 dev_info(uap->port.dev, "no DMA platform data\n");
318 return;
319 }
320
321 /* Try to acquire a generic DMA engine slave TX channel */
322 dma_cap_zero(mask);
323 dma_cap_set(DMA_SLAVE, mask);
324
325 chan = dma_request_channel(mask, plat->dma_filter,
326 plat->dma_tx_param);
327 if (!chan) {
328 dev_err(uap->port.dev, "no TX DMA channel!\n");
329 return;
330 }
Russell King68b65f72010-12-22 17:24:39 +0000331 }
332
333 dmaengine_slave_config(chan, &tx_conf);
334 uap->dmatx.chan = chan;
335
336 dev_info(uap->port.dev, "DMA channel TX %s\n",
337 dma_chan_name(uap->dmatx.chan));
Linus Walleijead76f32011-02-24 13:21:08 +0100338
339 /* Optionally make use of an RX channel as well */
Arnd Bergmann787b0c12013-01-28 16:24:37 +0000340 chan = dma_request_slave_channel(dev, "rx");
Rob Herring0d3c6732014-04-18 17:19:57 -0500341
Arnd Bergmann787b0c12013-01-28 16:24:37 +0000342 if (!chan && plat->dma_rx_param) {
343 chan = dma_request_channel(mask, plat->dma_filter, plat->dma_rx_param);
344
345 if (!chan) {
346 dev_err(uap->port.dev, "no RX DMA channel!\n");
347 return;
348 }
349 }
350
351 if (chan) {
Linus Walleijead76f32011-02-24 13:21:08 +0100352 struct dma_slave_config rx_conf = {
Greg Kroah-Hartman0de6cfb2015-09-04 09:13:56 -0700353 .src_addr = uap->port.mapbase + UART01x_DR,
Linus Walleijead76f32011-02-24 13:21:08 +0100354 .src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE,
Vinod Koula485df42011-10-14 10:47:38 +0530355 .direction = DMA_DEV_TO_MEM,
Guennadi Liakhovetskib2aeb772014-04-12 19:47:17 +0200356 .src_maxburst = uap->fifosize >> 2,
Viresh Kumar258aea72012-02-01 16:12:19 +0530357 .device_fc = false,
Linus Walleijead76f32011-02-24 13:21:08 +0100358 };
Andrew Jackson2d3b7d62014-11-07 14:14:47 +0000359 struct dma_slave_caps caps;
Linus Walleijead76f32011-02-24 13:21:08 +0100360
Andrew Jackson2d3b7d62014-11-07 14:14:47 +0000361 /*
362 * Some DMA controllers provide information on their capabilities.
363 * If the controller does, check for suitable residue processing
364 * otherwise assime all is well.
365 */
366 if (0 == dma_get_slave_caps(chan, &caps)) {
367 if (caps.residue_granularity ==
368 DMA_RESIDUE_GRANULARITY_DESCRIPTOR) {
369 dma_release_channel(chan);
370 dev_info(uap->port.dev,
371 "RX DMA disabled - no residue processing\n");
372 return;
373 }
374 }
Linus Walleijead76f32011-02-24 13:21:08 +0100375 dmaengine_slave_config(chan, &rx_conf);
376 uap->dmarx.chan = chan;
377
Andrew Jackson98267d32014-11-07 14:14:23 +0000378 uap->dmarx.auto_poll_rate = false;
Greg Kroah-Hartman8f898bf2013-12-17 09:33:18 -0800379 if (plat && plat->dma_rx_poll_enable) {
Chanho Mincb06ff12013-03-27 18:38:11 +0900380 /* Set poll rate if specified. */
381 if (plat->dma_rx_poll_rate) {
382 uap->dmarx.auto_poll_rate = false;
383 uap->dmarx.poll_rate = plat->dma_rx_poll_rate;
384 } else {
385 /*
386 * 100 ms defaults to poll rate if not
387 * specified. This will be adjusted with
388 * the baud rate at set_termios.
389 */
390 uap->dmarx.auto_poll_rate = true;
391 uap->dmarx.poll_rate = 100;
392 }
393 /* 3 secs defaults poll_timeout if not specified. */
394 if (plat->dma_rx_poll_timeout)
395 uap->dmarx.poll_timeout =
396 plat->dma_rx_poll_timeout;
397 else
398 uap->dmarx.poll_timeout = 3000;
Andrew Jackson98267d32014-11-07 14:14:23 +0000399 } else if (!plat && dev->of_node) {
400 uap->dmarx.auto_poll_rate = of_property_read_bool(
401 dev->of_node, "auto-poll");
402 if (uap->dmarx.auto_poll_rate) {
403 u32 x;
Chanho Mincb06ff12013-03-27 18:38:11 +0900404
Andrew Jackson98267d32014-11-07 14:14:23 +0000405 if (0 == of_property_read_u32(dev->of_node,
406 "poll-rate-ms", &x))
407 uap->dmarx.poll_rate = x;
408 else
409 uap->dmarx.poll_rate = 100;
410 if (0 == of_property_read_u32(dev->of_node,
411 "poll-timeout-ms", &x))
412 uap->dmarx.poll_timeout = x;
413 else
414 uap->dmarx.poll_timeout = 3000;
415 }
416 }
Linus Walleijead76f32011-02-24 13:21:08 +0100417 dev_info(uap->port.dev, "DMA channel RX %s\n",
418 dma_chan_name(uap->dmarx.chan));
419 }
Russell King68b65f72010-12-22 17:24:39 +0000420}
421
Russell King68b65f72010-12-22 17:24:39 +0000422static void pl011_dma_remove(struct uart_amba_port *uap)
423{
Russell King68b65f72010-12-22 17:24:39 +0000424 if (uap->dmatx.chan)
425 dma_release_channel(uap->dmatx.chan);
Linus Walleijead76f32011-02-24 13:21:08 +0100426 if (uap->dmarx.chan)
427 dma_release_channel(uap->dmarx.chan);
Russell King68b65f72010-12-22 17:24:39 +0000428}
429
Dave Martin734745c2015-03-04 12:27:33 +0000430/* Forward declare these for the refill routine */
Russell King68b65f72010-12-22 17:24:39 +0000431static int pl011_dma_tx_refill(struct uart_amba_port *uap);
Dave Martin734745c2015-03-04 12:27:33 +0000432static void pl011_start_tx_pio(struct uart_amba_port *uap);
Russell King68b65f72010-12-22 17:24:39 +0000433
434/*
435 * The current DMA TX buffer has been sent.
436 * Try to queue up another DMA buffer.
437 */
438static void pl011_dma_tx_callback(void *data)
439{
440 struct uart_amba_port *uap = data;
441 struct pl011_dmatx_data *dmatx = &uap->dmatx;
442 unsigned long flags;
443 u16 dmacr;
444
445 spin_lock_irqsave(&uap->port.lock, flags);
446 if (uap->dmatx.queued)
447 dma_unmap_sg(dmatx->chan->device->dev, &dmatx->sg, 1,
448 DMA_TO_DEVICE);
449
450 dmacr = uap->dmacr;
451 uap->dmacr = dmacr & ~UART011_TXDMAE;
Russell Kingb2a4e242015-11-03 14:51:03 +0000452 pl011_write(uap->dmacr, uap, UART011_DMACR);
Russell King68b65f72010-12-22 17:24:39 +0000453
454 /*
455 * If TX DMA was disabled, it means that we've stopped the DMA for
456 * some reason (eg, XOFF received, or we want to send an X-char.)
457 *
458 * Note: we need to be careful here of a potential race between DMA
459 * and the rest of the driver - if the driver disables TX DMA while
460 * a TX buffer completing, we must update the tx queued status to
461 * get further refills (hence we check dmacr).
462 */
463 if (!(dmacr & UART011_TXDMAE) || uart_tx_stopped(&uap->port) ||
464 uart_circ_empty(&uap->port.state->xmit)) {
465 uap->dmatx.queued = false;
466 spin_unlock_irqrestore(&uap->port.lock, flags);
467 return;
468 }
469
Dave Martin734745c2015-03-04 12:27:33 +0000470 if (pl011_dma_tx_refill(uap) <= 0)
Russell King68b65f72010-12-22 17:24:39 +0000471 /*
472 * We didn't queue a DMA buffer for some reason, but we
473 * have data pending to be sent. Re-enable the TX IRQ.
474 */
Dave Martin734745c2015-03-04 12:27:33 +0000475 pl011_start_tx_pio(uap);
476
Russell King68b65f72010-12-22 17:24:39 +0000477 spin_unlock_irqrestore(&uap->port.lock, flags);
478}
479
480/*
481 * Try to refill the TX DMA buffer.
482 * Locking: called with port lock held and IRQs disabled.
483 * Returns:
484 * 1 if we queued up a TX DMA buffer.
485 * 0 if we didn't want to handle this by DMA
486 * <0 on error
487 */
488static int pl011_dma_tx_refill(struct uart_amba_port *uap)
489{
490 struct pl011_dmatx_data *dmatx = &uap->dmatx;
491 struct dma_chan *chan = dmatx->chan;
492 struct dma_device *dma_dev = chan->device;
493 struct dma_async_tx_descriptor *desc;
494 struct circ_buf *xmit = &uap->port.state->xmit;
495 unsigned int count;
496
497 /*
498 * Try to avoid the overhead involved in using DMA if the
499 * transaction fits in the first half of the FIFO, by using
500 * the standard interrupt handling. This ensures that we
501 * issue a uart_write_wakeup() at the appropriate time.
502 */
503 count = uart_circ_chars_pending(xmit);
504 if (count < (uap->fifosize >> 1)) {
505 uap->dmatx.queued = false;
506 return 0;
507 }
508
509 /*
510 * Bodge: don't send the last character by DMA, as this
511 * will prevent XON from notifying us to restart DMA.
512 */
513 count -= 1;
514
515 /* Else proceed to copy the TX chars to the DMA buffer and fire DMA */
516 if (count > PL011_DMA_BUFFER_SIZE)
517 count = PL011_DMA_BUFFER_SIZE;
518
519 if (xmit->tail < xmit->head)
520 memcpy(&dmatx->buf[0], &xmit->buf[xmit->tail], count);
521 else {
522 size_t first = UART_XMIT_SIZE - xmit->tail;
Andrew Jacksone2a545a2014-11-07 14:14:39 +0000523 size_t second;
524
525 if (first > count)
526 first = count;
527 second = count - first;
Russell King68b65f72010-12-22 17:24:39 +0000528
529 memcpy(&dmatx->buf[0], &xmit->buf[xmit->tail], first);
530 if (second)
531 memcpy(&dmatx->buf[first], &xmit->buf[0], second);
532 }
533
534 dmatx->sg.length = count;
535
536 if (dma_map_sg(dma_dev->dev, &dmatx->sg, 1, DMA_TO_DEVICE) != 1) {
537 uap->dmatx.queued = false;
538 dev_dbg(uap->port.dev, "unable to map TX DMA\n");
539 return -EBUSY;
540 }
541
Alexandre Bounine16052822012-03-08 16:11:18 -0500542 desc = dmaengine_prep_slave_sg(chan, &dmatx->sg, 1, DMA_MEM_TO_DEV,
Russell King68b65f72010-12-22 17:24:39 +0000543 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
544 if (!desc) {
545 dma_unmap_sg(dma_dev->dev, &dmatx->sg, 1, DMA_TO_DEVICE);
546 uap->dmatx.queued = false;
547 /*
548 * If DMA cannot be used right now, we complete this
549 * transaction via IRQ and let the TTY layer retry.
550 */
551 dev_dbg(uap->port.dev, "TX DMA busy\n");
552 return -EBUSY;
553 }
554
555 /* Some data to go along to the callback */
556 desc->callback = pl011_dma_tx_callback;
557 desc->callback_param = uap;
558
559 /* All errors should happen at prepare time */
560 dmaengine_submit(desc);
561
562 /* Fire the DMA transaction */
563 dma_dev->device_issue_pending(chan);
564
565 uap->dmacr |= UART011_TXDMAE;
Russell Kingb2a4e242015-11-03 14:51:03 +0000566 pl011_write(uap->dmacr, uap, UART011_DMACR);
Russell King68b65f72010-12-22 17:24:39 +0000567 uap->dmatx.queued = true;
568
569 /*
570 * Now we know that DMA will fire, so advance the ring buffer
571 * with the stuff we just dispatched.
572 */
573 xmit->tail = (xmit->tail + count) & (UART_XMIT_SIZE - 1);
574 uap->port.icount.tx += count;
575
576 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
577 uart_write_wakeup(&uap->port);
578
579 return 1;
580}
581
582/*
583 * We received a transmit interrupt without a pending X-char but with
584 * pending characters.
585 * Locking: called with port lock held and IRQs disabled.
586 * Returns:
587 * false if we want to use PIO to transmit
588 * true if we queued a DMA buffer
589 */
590static bool pl011_dma_tx_irq(struct uart_amba_port *uap)
591{
Linus Walleijead76f32011-02-24 13:21:08 +0100592 if (!uap->using_tx_dma)
Russell King68b65f72010-12-22 17:24:39 +0000593 return false;
594
595 /*
596 * If we already have a TX buffer queued, but received a
597 * TX interrupt, it will be because we've just sent an X-char.
598 * Ensure the TX DMA is enabled and the TX IRQ is disabled.
599 */
600 if (uap->dmatx.queued) {
601 uap->dmacr |= UART011_TXDMAE;
Russell Kingb2a4e242015-11-03 14:51:03 +0000602 pl011_write(uap->dmacr, uap, UART011_DMACR);
Russell King68b65f72010-12-22 17:24:39 +0000603 uap->im &= ~UART011_TXIM;
Russell Kingb2a4e242015-11-03 14:51:03 +0000604 pl011_write(uap->im, uap, UART011_IMSC);
Russell King68b65f72010-12-22 17:24:39 +0000605 return true;
606 }
607
608 /*
609 * We don't have a TX buffer queued, so try to queue one.
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300610 * If we successfully queued a buffer, mask the TX IRQ.
Russell King68b65f72010-12-22 17:24:39 +0000611 */
612 if (pl011_dma_tx_refill(uap) > 0) {
613 uap->im &= ~UART011_TXIM;
Russell Kingb2a4e242015-11-03 14:51:03 +0000614 pl011_write(uap->im, uap, UART011_IMSC);
Russell King68b65f72010-12-22 17:24:39 +0000615 return true;
616 }
617 return false;
618}
619
620/*
621 * Stop the DMA transmit (eg, due to received XOFF).
622 * Locking: called with port lock held and IRQs disabled.
623 */
624static inline void pl011_dma_tx_stop(struct uart_amba_port *uap)
625{
626 if (uap->dmatx.queued) {
627 uap->dmacr &= ~UART011_TXDMAE;
Russell Kingb2a4e242015-11-03 14:51:03 +0000628 pl011_write(uap->dmacr, uap, UART011_DMACR);
Russell King68b65f72010-12-22 17:24:39 +0000629 }
630}
631
632/*
633 * Try to start a DMA transmit, or in the case of an XON/OFF
634 * character queued for send, try to get that character out ASAP.
635 * Locking: called with port lock held and IRQs disabled.
636 * Returns:
637 * false if we want the TX IRQ to be enabled
638 * true if we have a buffer queued
639 */
640static inline bool pl011_dma_tx_start(struct uart_amba_port *uap)
641{
642 u16 dmacr;
643
Linus Walleijead76f32011-02-24 13:21:08 +0100644 if (!uap->using_tx_dma)
Russell King68b65f72010-12-22 17:24:39 +0000645 return false;
646
647 if (!uap->port.x_char) {
648 /* no X-char, try to push chars out in DMA mode */
649 bool ret = true;
650
651 if (!uap->dmatx.queued) {
652 if (pl011_dma_tx_refill(uap) > 0) {
653 uap->im &= ~UART011_TXIM;
Russell Kingb2a4e242015-11-03 14:51:03 +0000654 pl011_write(uap->im, uap, 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 Kingb2a4e242015-11-03 14:51:03 +0000659 pl011_write(uap->dmacr, uap, UART011_DMACR);
Russell King68b65f72010-12-22 17:24:39 +0000660 }
661 return ret;
662 }
663
664 /*
665 * We have an X-char to send. Disable DMA to prevent it loading
666 * the TX fifo, and then see if we can stuff it into the FIFO.
667 */
668 dmacr = uap->dmacr;
669 uap->dmacr &= ~UART011_TXDMAE;
Russell Kingb2a4e242015-11-03 14:51:03 +0000670 pl011_write(uap->dmacr, uap, UART011_DMACR);
Russell King68b65f72010-12-22 17:24:39 +0000671
Russell Kingb2a4e242015-11-03 14:51:03 +0000672 if (pl011_read(uap, UART01x_FR) & UART01x_FR_TXFF) {
Russell King68b65f72010-12-22 17:24:39 +0000673 /*
674 * No space in the FIFO, so enable the transmit interrupt
675 * so we know when there is space. Note that once we've
676 * loaded the character, we should just re-enable DMA.
677 */
678 return false;
679 }
680
Russell Kingb2a4e242015-11-03 14:51:03 +0000681 pl011_write(uap->port.x_char, uap, UART01x_DR);
Russell King68b65f72010-12-22 17:24:39 +0000682 uap->port.icount.tx++;
683 uap->port.x_char = 0;
684
685 /* Success - restore the DMA state */
686 uap->dmacr = dmacr;
Russell Kingb2a4e242015-11-03 14:51:03 +0000687 pl011_write(dmacr, uap, UART011_DMACR);
Russell King68b65f72010-12-22 17:24:39 +0000688
689 return true;
690}
691
692/*
693 * Flush the transmit buffer.
694 * Locking: called with port lock held and IRQs disabled.
695 */
696static void pl011_dma_flush_buffer(struct uart_port *port)
Fabio Estevamb83286b2013-08-09 17:58:51 -0300697__releases(&uap->port.lock)
698__acquires(&uap->port.lock)
Russell King68b65f72010-12-22 17:24:39 +0000699{
Daniel Thompsona5820c22014-09-03 12:51:55 +0100700 struct uart_amba_port *uap =
701 container_of(port, struct uart_amba_port, port);
Russell King68b65f72010-12-22 17:24:39 +0000702
Linus Walleijead76f32011-02-24 13:21:08 +0100703 if (!uap->using_tx_dma)
Russell King68b65f72010-12-22 17:24:39 +0000704 return;
705
706 /* Avoid deadlock with the DMA engine callback */
707 spin_unlock(&uap->port.lock);
708 dmaengine_terminate_all(uap->dmatx.chan);
709 spin_lock(&uap->port.lock);
710 if (uap->dmatx.queued) {
711 dma_unmap_sg(uap->dmatx.chan->device->dev, &uap->dmatx.sg, 1,
712 DMA_TO_DEVICE);
713 uap->dmatx.queued = false;
714 uap->dmacr &= ~UART011_TXDMAE;
Russell Kingb2a4e242015-11-03 14:51:03 +0000715 pl011_write(uap->dmacr, uap, UART011_DMACR);
Russell King68b65f72010-12-22 17:24:39 +0000716 }
717}
718
Linus Walleijead76f32011-02-24 13:21:08 +0100719static void pl011_dma_rx_callback(void *data);
720
721static int pl011_dma_rx_trigger_dma(struct uart_amba_port *uap)
722{
723 struct dma_chan *rxchan = uap->dmarx.chan;
Linus Walleijead76f32011-02-24 13:21:08 +0100724 struct pl011_dmarx_data *dmarx = &uap->dmarx;
725 struct dma_async_tx_descriptor *desc;
726 struct pl011_sgbuf *sgbuf;
727
728 if (!rxchan)
729 return -EIO;
730
731 /* Start the RX DMA job */
732 sgbuf = uap->dmarx.use_buf_b ?
733 &uap->dmarx.sgbuf_b : &uap->dmarx.sgbuf_a;
Alexandre Bounine16052822012-03-08 16:11:18 -0500734 desc = dmaengine_prep_slave_sg(rxchan, &sgbuf->sg, 1,
Vinod Koula485df42011-10-14 10:47:38 +0530735 DMA_DEV_TO_MEM,
Linus Walleijead76f32011-02-24 13:21:08 +0100736 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
737 /*
738 * If the DMA engine is busy and cannot prepare a
739 * channel, no big deal, the driver will fall back
740 * to interrupt mode as a result of this error code.
741 */
742 if (!desc) {
743 uap->dmarx.running = false;
744 dmaengine_terminate_all(rxchan);
745 return -EBUSY;
746 }
747
748 /* Some data to go along to the callback */
749 desc->callback = pl011_dma_rx_callback;
750 desc->callback_param = uap;
751 dmarx->cookie = dmaengine_submit(desc);
752 dma_async_issue_pending(rxchan);
753
754 uap->dmacr |= UART011_RXDMAE;
Russell Kingb2a4e242015-11-03 14:51:03 +0000755 pl011_write(uap->dmacr, uap, UART011_DMACR);
Linus Walleijead76f32011-02-24 13:21:08 +0100756 uap->dmarx.running = true;
757
758 uap->im &= ~UART011_RXIM;
Russell Kingb2a4e242015-11-03 14:51:03 +0000759 pl011_write(uap->im, uap, UART011_IMSC);
Linus Walleijead76f32011-02-24 13:21:08 +0100760
761 return 0;
762}
763
764/*
765 * This is called when either the DMA job is complete, or
766 * the FIFO timeout interrupt occurred. This must be called
767 * with the port spinlock uap->port.lock held.
768 */
769static void pl011_dma_rx_chars(struct uart_amba_port *uap,
770 u32 pending, bool use_buf_b,
771 bool readfifo)
772{
Jiri Slaby05c7cd32013-01-03 15:53:04 +0100773 struct tty_port *port = &uap->port.state->port;
Linus Walleijead76f32011-02-24 13:21:08 +0100774 struct pl011_sgbuf *sgbuf = use_buf_b ?
775 &uap->dmarx.sgbuf_b : &uap->dmarx.sgbuf_a;
Linus Walleijead76f32011-02-24 13:21:08 +0100776 int dma_count = 0;
777 u32 fifotaken = 0; /* only used for vdbg() */
778
Chanho Mincb06ff12013-03-27 18:38:11 +0900779 struct pl011_dmarx_data *dmarx = &uap->dmarx;
780 int dmataken = 0;
781
782 if (uap->dmarx.poll_rate) {
783 /* The data can be taken by polling */
784 dmataken = sgbuf->sg.length - dmarx->last_residue;
785 /* Recalculate the pending size */
786 if (pending >= dmataken)
787 pending -= dmataken;
788 }
789
790 /* Pick the remain data from the DMA */
Linus Walleijead76f32011-02-24 13:21:08 +0100791 if (pending) {
Linus Walleijead76f32011-02-24 13:21:08 +0100792
793 /*
794 * First take all chars in the DMA pipe, then look in the FIFO.
795 * Note that tty_insert_flip_buf() tries to take as many chars
796 * as it can.
797 */
Chanho Mincb06ff12013-03-27 18:38:11 +0900798 dma_count = tty_insert_flip_string(port, sgbuf->buf + dmataken,
799 pending);
Linus Walleijead76f32011-02-24 13:21:08 +0100800
801 uap->port.icount.rx += dma_count;
802 if (dma_count < pending)
803 dev_warn(uap->port.dev,
804 "couldn't insert all characters (TTY is full?)\n");
805 }
806
Chanho Mincb06ff12013-03-27 18:38:11 +0900807 /* Reset the last_residue for Rx DMA poll */
808 if (uap->dmarx.poll_rate)
809 dmarx->last_residue = sgbuf->sg.length;
810
Linus Walleijead76f32011-02-24 13:21:08 +0100811 /*
812 * Only continue with trying to read the FIFO if all DMA chars have
813 * been taken first.
814 */
815 if (dma_count == pending && readfifo) {
816 /* Clear any error flags */
Russell King75836332015-11-03 14:50:58 +0000817 pl011_write(UART011_OEIS | UART011_BEIS | UART011_PEIS |
Russell Kingb2a4e242015-11-03 14:51:03 +0000818 UART011_FEIS, uap, UART011_ICR);
Linus Walleijead76f32011-02-24 13:21:08 +0100819
820 /*
821 * If we read all the DMA'd characters, and we had an
Linus Walleij29772c42011-02-24 13:21:36 +0100822 * incomplete buffer, that could be due to an rx error, or
823 * maybe we just timed out. Read any pending chars and check
824 * the error status.
825 *
826 * Error conditions will only occur in the FIFO, these will
827 * trigger an immediate interrupt and stop the DMA job, so we
828 * will always find the error in the FIFO, never in the DMA
829 * buffer.
Linus Walleijead76f32011-02-24 13:21:08 +0100830 */
Linus Walleij29772c42011-02-24 13:21:36 +0100831 fifotaken = pl011_fifo_to_tty(uap);
Linus Walleijead76f32011-02-24 13:21:08 +0100832 }
833
834 spin_unlock(&uap->port.lock);
835 dev_vdbg(uap->port.dev,
836 "Took %d chars from DMA buffer and %d chars from the FIFO\n",
837 dma_count, fifotaken);
Jiri Slaby2e124b42013-01-03 15:53:06 +0100838 tty_flip_buffer_push(port);
Linus Walleijead76f32011-02-24 13:21:08 +0100839 spin_lock(&uap->port.lock);
840}
841
842static void pl011_dma_rx_irq(struct uart_amba_port *uap)
843{
844 struct pl011_dmarx_data *dmarx = &uap->dmarx;
845 struct dma_chan *rxchan = dmarx->chan;
846 struct pl011_sgbuf *sgbuf = dmarx->use_buf_b ?
847 &dmarx->sgbuf_b : &dmarx->sgbuf_a;
848 size_t pending;
849 struct dma_tx_state state;
850 enum dma_status dmastat;
851
852 /*
853 * Pause the transfer so we can trust the current counter,
854 * do this before we pause the PL011 block, else we may
855 * overflow the FIFO.
856 */
857 if (dmaengine_pause(rxchan))
858 dev_err(uap->port.dev, "unable to pause DMA transfer\n");
859 dmastat = rxchan->device->device_tx_status(rxchan,
860 dmarx->cookie, &state);
861 if (dmastat != DMA_PAUSED)
862 dev_err(uap->port.dev, "unable to pause DMA transfer\n");
863
864 /* Disable RX DMA - incoming data will wait in the FIFO */
865 uap->dmacr &= ~UART011_RXDMAE;
Russell Kingb2a4e242015-11-03 14:51:03 +0000866 pl011_write(uap->dmacr, uap, UART011_DMACR);
Linus Walleijead76f32011-02-24 13:21:08 +0100867 uap->dmarx.running = false;
868
869 pending = sgbuf->sg.length - state.residue;
870 BUG_ON(pending > PL011_DMA_BUFFER_SIZE);
871 /* Then we terminate the transfer - we now know our residue */
872 dmaengine_terminate_all(rxchan);
873
874 /*
875 * This will take the chars we have so far and insert
876 * into the framework.
877 */
878 pl011_dma_rx_chars(uap, pending, dmarx->use_buf_b, true);
879
880 /* Switch buffer & re-trigger DMA job */
881 dmarx->use_buf_b = !dmarx->use_buf_b;
882 if (pl011_dma_rx_trigger_dma(uap)) {
883 dev_dbg(uap->port.dev, "could not retrigger RX DMA job "
884 "fall back to interrupt mode\n");
885 uap->im |= UART011_RXIM;
Russell Kingb2a4e242015-11-03 14:51:03 +0000886 pl011_write(uap->im, uap, UART011_IMSC);
Linus Walleijead76f32011-02-24 13:21:08 +0100887 }
888}
889
890static void pl011_dma_rx_callback(void *data)
891{
892 struct uart_amba_port *uap = data;
893 struct pl011_dmarx_data *dmarx = &uap->dmarx;
Chanho Min6dc01aa2012-02-20 10:24:40 +0900894 struct dma_chan *rxchan = dmarx->chan;
Linus Walleijead76f32011-02-24 13:21:08 +0100895 bool lastbuf = dmarx->use_buf_b;
Chanho Min6dc01aa2012-02-20 10:24:40 +0900896 struct pl011_sgbuf *sgbuf = dmarx->use_buf_b ?
897 &dmarx->sgbuf_b : &dmarx->sgbuf_a;
898 size_t pending;
899 struct dma_tx_state state;
Linus Walleijead76f32011-02-24 13:21:08 +0100900 int ret;
901
902 /*
903 * This completion interrupt occurs typically when the
904 * RX buffer is totally stuffed but no timeout has yet
905 * occurred. When that happens, we just want the RX
906 * routine to flush out the secondary DMA buffer while
907 * we immediately trigger the next DMA job.
908 */
909 spin_lock_irq(&uap->port.lock);
Chanho Min6dc01aa2012-02-20 10:24:40 +0900910 /*
911 * Rx data can be taken by the UART interrupts during
912 * the DMA irq handler. So we check the residue here.
913 */
914 rxchan->device->device_tx_status(rxchan, dmarx->cookie, &state);
915 pending = sgbuf->sg.length - state.residue;
916 BUG_ON(pending > PL011_DMA_BUFFER_SIZE);
917 /* Then we terminate the transfer - we now know our residue */
918 dmaengine_terminate_all(rxchan);
919
Linus Walleijead76f32011-02-24 13:21:08 +0100920 uap->dmarx.running = false;
921 dmarx->use_buf_b = !lastbuf;
922 ret = pl011_dma_rx_trigger_dma(uap);
923
Chanho Min6dc01aa2012-02-20 10:24:40 +0900924 pl011_dma_rx_chars(uap, pending, lastbuf, false);
Linus Walleijead76f32011-02-24 13:21:08 +0100925 spin_unlock_irq(&uap->port.lock);
926 /*
927 * Do this check after we picked the DMA chars so we don't
928 * get some IRQ immediately from RX.
929 */
930 if (ret) {
931 dev_dbg(uap->port.dev, "could not retrigger RX DMA job "
932 "fall back to interrupt mode\n");
933 uap->im |= UART011_RXIM;
Russell Kingb2a4e242015-11-03 14:51:03 +0000934 pl011_write(uap->im, uap, UART011_IMSC);
Linus Walleijead76f32011-02-24 13:21:08 +0100935 }
936}
937
938/*
939 * Stop accepting received characters, when we're shutting down or
940 * suspending this port.
941 * Locking: called with port lock held and IRQs disabled.
942 */
943static inline void pl011_dma_rx_stop(struct uart_amba_port *uap)
944{
945 /* FIXME. Just disable the DMA enable */
946 uap->dmacr &= ~UART011_RXDMAE;
Russell Kingb2a4e242015-11-03 14:51:03 +0000947 pl011_write(uap->dmacr, uap, UART011_DMACR);
Linus Walleijead76f32011-02-24 13:21:08 +0100948}
Russell King68b65f72010-12-22 17:24:39 +0000949
Chanho Mincb06ff12013-03-27 18:38:11 +0900950/*
951 * Timer handler for Rx DMA polling.
952 * Every polling, It checks the residue in the dma buffer and transfer
953 * data to the tty. Also, last_residue is updated for the next polling.
954 */
955static void pl011_dma_rx_poll(unsigned long args)
956{
957 struct uart_amba_port *uap = (struct uart_amba_port *)args;
958 struct tty_port *port = &uap->port.state->port;
959 struct pl011_dmarx_data *dmarx = &uap->dmarx;
960 struct dma_chan *rxchan = uap->dmarx.chan;
961 unsigned long flags = 0;
962 unsigned int dmataken = 0;
963 unsigned int size = 0;
964 struct pl011_sgbuf *sgbuf;
965 int dma_count;
966 struct dma_tx_state state;
967
968 sgbuf = dmarx->use_buf_b ? &uap->dmarx.sgbuf_b : &uap->dmarx.sgbuf_a;
969 rxchan->device->device_tx_status(rxchan, dmarx->cookie, &state);
970 if (likely(state.residue < dmarx->last_residue)) {
971 dmataken = sgbuf->sg.length - dmarx->last_residue;
972 size = dmarx->last_residue - state.residue;
973 dma_count = tty_insert_flip_string(port, sgbuf->buf + dmataken,
974 size);
975 if (dma_count == size)
976 dmarx->last_residue = state.residue;
977 dmarx->last_jiffies = jiffies;
978 }
979 tty_flip_buffer_push(port);
980
981 /*
982 * If no data is received in poll_timeout, the driver will fall back
983 * to interrupt mode. We will retrigger DMA at the first interrupt.
984 */
985 if (jiffies_to_msecs(jiffies - dmarx->last_jiffies)
986 > uap->dmarx.poll_timeout) {
987
988 spin_lock_irqsave(&uap->port.lock, flags);
989 pl011_dma_rx_stop(uap);
Guennadi Liakhovetskic25a1ad2013-12-10 14:54:47 +0100990 uap->im |= UART011_RXIM;
Russell Kingb2a4e242015-11-03 14:51:03 +0000991 pl011_write(uap->im, uap, UART011_IMSC);
Chanho Mincb06ff12013-03-27 18:38:11 +0900992 spin_unlock_irqrestore(&uap->port.lock, flags);
993
994 uap->dmarx.running = false;
995 dmaengine_terminate_all(rxchan);
996 del_timer(&uap->dmarx.timer);
997 } else {
998 mod_timer(&uap->dmarx.timer,
999 jiffies + msecs_to_jiffies(uap->dmarx.poll_rate));
1000 }
1001}
1002
Russell King68b65f72010-12-22 17:24:39 +00001003static void pl011_dma_startup(struct uart_amba_port *uap)
1004{
Linus Walleijead76f32011-02-24 13:21:08 +01001005 int ret;
1006
Jorge Ramirez-Ortiz1c9be312015-03-06 13:05:40 -05001007 if (!uap->dma_probed)
1008 pl011_dma_probe(uap);
1009
Russell King68b65f72010-12-22 17:24:39 +00001010 if (!uap->dmatx.chan)
1011 return;
1012
Andrew Jackson4c0be452014-11-07 14:14:35 +00001013 uap->dmatx.buf = kmalloc(PL011_DMA_BUFFER_SIZE, GFP_KERNEL | __GFP_DMA);
Russell King68b65f72010-12-22 17:24:39 +00001014 if (!uap->dmatx.buf) {
1015 dev_err(uap->port.dev, "no memory for DMA TX buffer\n");
1016 uap->port.fifosize = uap->fifosize;
1017 return;
1018 }
1019
1020 sg_init_one(&uap->dmatx.sg, uap->dmatx.buf, PL011_DMA_BUFFER_SIZE);
1021
1022 /* The DMA buffer is now the FIFO the TTY subsystem can use */
1023 uap->port.fifosize = PL011_DMA_BUFFER_SIZE;
Linus Walleijead76f32011-02-24 13:21:08 +01001024 uap->using_tx_dma = true;
Russell King68b65f72010-12-22 17:24:39 +00001025
Linus Walleijead76f32011-02-24 13:21:08 +01001026 if (!uap->dmarx.chan)
1027 goto skip_rx;
1028
1029 /* Allocate and map DMA RX buffers */
1030 ret = pl011_sgbuf_init(uap->dmarx.chan, &uap->dmarx.sgbuf_a,
1031 DMA_FROM_DEVICE);
1032 if (ret) {
1033 dev_err(uap->port.dev, "failed to init DMA %s: %d\n",
1034 "RX buffer A", ret);
1035 goto skip_rx;
1036 }
1037
1038 ret = pl011_sgbuf_init(uap->dmarx.chan, &uap->dmarx.sgbuf_b,
1039 DMA_FROM_DEVICE);
1040 if (ret) {
1041 dev_err(uap->port.dev, "failed to init DMA %s: %d\n",
1042 "RX buffer B", ret);
1043 pl011_sgbuf_free(uap->dmarx.chan, &uap->dmarx.sgbuf_a,
1044 DMA_FROM_DEVICE);
1045 goto skip_rx;
1046 }
1047
1048 uap->using_rx_dma = true;
1049
1050skip_rx:
Russell King68b65f72010-12-22 17:24:39 +00001051 /* Turn on DMA error (RX/TX will be enabled on demand) */
1052 uap->dmacr |= UART011_DMAONERR;
Russell Kingb2a4e242015-11-03 14:51:03 +00001053 pl011_write(uap->dmacr, uap, UART011_DMACR);
Russell King38d62432010-12-22 17:59:16 +00001054
1055 /*
1056 * ST Micro variants has some specific dma burst threshold
1057 * compensation. Set this to 16 bytes, so burst will only
1058 * be issued above/below 16 bytes.
1059 */
1060 if (uap->vendor->dma_threshold)
Russell King75836332015-11-03 14:50:58 +00001061 pl011_write(ST_UART011_DMAWM_RX_16 | ST_UART011_DMAWM_TX_16,
Russell Kingb2a4e242015-11-03 14:51:03 +00001062 uap, ST_UART011_DMAWM);
Linus Walleijead76f32011-02-24 13:21:08 +01001063
1064 if (uap->using_rx_dma) {
1065 if (pl011_dma_rx_trigger_dma(uap))
1066 dev_dbg(uap->port.dev, "could not trigger initial "
1067 "RX DMA job, fall back to interrupt mode\n");
Chanho Mincb06ff12013-03-27 18:38:11 +09001068 if (uap->dmarx.poll_rate) {
1069 init_timer(&(uap->dmarx.timer));
1070 uap->dmarx.timer.function = pl011_dma_rx_poll;
1071 uap->dmarx.timer.data = (unsigned long)uap;
1072 mod_timer(&uap->dmarx.timer,
1073 jiffies +
1074 msecs_to_jiffies(uap->dmarx.poll_rate));
1075 uap->dmarx.last_residue = PL011_DMA_BUFFER_SIZE;
1076 uap->dmarx.last_jiffies = jiffies;
1077 }
Linus Walleijead76f32011-02-24 13:21:08 +01001078 }
Russell King68b65f72010-12-22 17:24:39 +00001079}
1080
1081static void pl011_dma_shutdown(struct uart_amba_port *uap)
1082{
Linus Walleijead76f32011-02-24 13:21:08 +01001083 if (!(uap->using_tx_dma || uap->using_rx_dma))
Russell King68b65f72010-12-22 17:24:39 +00001084 return;
1085
1086 /* Disable RX and TX DMA */
Russell Kingb2a4e242015-11-03 14:51:03 +00001087 while (pl011_read(uap, UART01x_FR) & UART01x_FR_BUSY)
Russell King68b65f72010-12-22 17:24:39 +00001088 barrier();
1089
1090 spin_lock_irq(&uap->port.lock);
1091 uap->dmacr &= ~(UART011_DMAONERR | UART011_RXDMAE | UART011_TXDMAE);
Russell Kingb2a4e242015-11-03 14:51:03 +00001092 pl011_write(uap->dmacr, uap, UART011_DMACR);
Russell King68b65f72010-12-22 17:24:39 +00001093 spin_unlock_irq(&uap->port.lock);
1094
Linus Walleijead76f32011-02-24 13:21:08 +01001095 if (uap->using_tx_dma) {
1096 /* In theory, this should already be done by pl011_dma_flush_buffer */
1097 dmaengine_terminate_all(uap->dmatx.chan);
1098 if (uap->dmatx.queued) {
1099 dma_unmap_sg(uap->dmatx.chan->device->dev, &uap->dmatx.sg, 1,
1100 DMA_TO_DEVICE);
1101 uap->dmatx.queued = false;
1102 }
1103
1104 kfree(uap->dmatx.buf);
1105 uap->using_tx_dma = false;
Russell King68b65f72010-12-22 17:24:39 +00001106 }
1107
Linus Walleijead76f32011-02-24 13:21:08 +01001108 if (uap->using_rx_dma) {
1109 dmaengine_terminate_all(uap->dmarx.chan);
1110 /* Clean up the RX DMA */
1111 pl011_sgbuf_free(uap->dmarx.chan, &uap->dmarx.sgbuf_a, DMA_FROM_DEVICE);
1112 pl011_sgbuf_free(uap->dmarx.chan, &uap->dmarx.sgbuf_b, DMA_FROM_DEVICE);
Chanho Mincb06ff12013-03-27 18:38:11 +09001113 if (uap->dmarx.poll_rate)
1114 del_timer_sync(&uap->dmarx.timer);
Linus Walleijead76f32011-02-24 13:21:08 +01001115 uap->using_rx_dma = false;
1116 }
Russell King68b65f72010-12-22 17:24:39 +00001117}
1118
Linus Walleijead76f32011-02-24 13:21:08 +01001119static inline bool pl011_dma_rx_available(struct uart_amba_port *uap)
1120{
1121 return uap->using_rx_dma;
1122}
1123
1124static inline bool pl011_dma_rx_running(struct uart_amba_port *uap)
1125{
1126 return uap->using_rx_dma && uap->dmarx.running;
1127}
1128
Russell King68b65f72010-12-22 17:24:39 +00001129#else
1130/* Blank functions if the DMA engine is not available */
Jorge Ramirez-Ortiz1c9be312015-03-06 13:05:40 -05001131static inline void pl011_dma_probe(struct uart_amba_port *uap)
Russell King68b65f72010-12-22 17:24:39 +00001132{
1133}
1134
1135static inline void pl011_dma_remove(struct uart_amba_port *uap)
1136{
1137}
1138
1139static inline void pl011_dma_startup(struct uart_amba_port *uap)
1140{
1141}
1142
1143static inline void pl011_dma_shutdown(struct uart_amba_port *uap)
1144{
1145}
1146
1147static inline bool pl011_dma_tx_irq(struct uart_amba_port *uap)
1148{
1149 return false;
1150}
1151
1152static inline void pl011_dma_tx_stop(struct uart_amba_port *uap)
1153{
1154}
1155
1156static inline bool pl011_dma_tx_start(struct uart_amba_port *uap)
1157{
1158 return false;
1159}
1160
Linus Walleijead76f32011-02-24 13:21:08 +01001161static inline void pl011_dma_rx_irq(struct uart_amba_port *uap)
1162{
1163}
1164
1165static inline void pl011_dma_rx_stop(struct uart_amba_port *uap)
1166{
1167}
1168
1169static inline int pl011_dma_rx_trigger_dma(struct uart_amba_port *uap)
1170{
1171 return -EIO;
1172}
1173
1174static inline bool pl011_dma_rx_available(struct uart_amba_port *uap)
1175{
1176 return false;
1177}
1178
1179static inline bool pl011_dma_rx_running(struct uart_amba_port *uap)
1180{
1181 return false;
1182}
1183
Russell King68b65f72010-12-22 17:24:39 +00001184#define pl011_dma_flush_buffer NULL
1185#endif
1186
Russell Kingb129a8c2005-08-31 10:12:14 +01001187static void pl011_stop_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001189 struct uart_amba_port *uap =
1190 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191
1192 uap->im &= ~UART011_TXIM;
Russell Kingb2a4e242015-11-03 14:51:03 +00001193 pl011_write(uap->im, uap, UART011_IMSC);
Russell King68b65f72010-12-22 17:24:39 +00001194 pl011_dma_tx_stop(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195}
1196
Dave Martin1e84d222015-04-27 16:49:05 +01001197static void pl011_tx_chars(struct uart_amba_port *uap, bool from_irq);
Dave Martin734745c2015-03-04 12:27:33 +00001198
1199/* Start TX with programmed I/O only (no DMA) */
1200static void pl011_start_tx_pio(struct uart_amba_port *uap)
1201{
1202 uap->im |= UART011_TXIM;
Russell Kingb2a4e242015-11-03 14:51:03 +00001203 pl011_write(uap->im, uap, UART011_IMSC);
Dave Martin1e84d222015-04-27 16:49:05 +01001204 pl011_tx_chars(uap, false);
Dave Martin734745c2015-03-04 12:27:33 +00001205}
1206
Russell Kingb129a8c2005-08-31 10:12:14 +01001207static void pl011_start_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001209 struct uart_amba_port *uap =
1210 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211
Dave Martin734745c2015-03-04 12:27:33 +00001212 if (!pl011_dma_tx_start(uap))
1213 pl011_start_tx_pio(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214}
1215
1216static void pl011_stop_rx(struct uart_port *port)
1217{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001218 struct uart_amba_port *uap =
1219 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220
1221 uap->im &= ~(UART011_RXIM|UART011_RTIM|UART011_FEIM|
1222 UART011_PEIM|UART011_BEIM|UART011_OEIM);
Russell Kingb2a4e242015-11-03 14:51:03 +00001223 pl011_write(uap->im, uap, UART011_IMSC);
Linus Walleijead76f32011-02-24 13:21:08 +01001224
1225 pl011_dma_rx_stop(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226}
1227
1228static void pl011_enable_ms(struct uart_port *port)
1229{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001230 struct uart_amba_port *uap =
1231 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232
1233 uap->im |= UART011_RIMIM|UART011_CTSMIM|UART011_DCDMIM|UART011_DSRMIM;
Russell Kingb2a4e242015-11-03 14:51:03 +00001234 pl011_write(uap->im, uap, UART011_IMSC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235}
1236
David Howells7d12e782006-10-05 14:55:46 +01001237static void pl011_rx_chars(struct uart_amba_port *uap)
Fabio Estevamb83286b2013-08-09 17:58:51 -03001238__releases(&uap->port.lock)
1239__acquires(&uap->port.lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240{
Linus Walleij29772c42011-02-24 13:21:36 +01001241 pl011_fifo_to_tty(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242
Thomas Gleixner2389b272007-05-29 21:53:50 +01001243 spin_unlock(&uap->port.lock);
Jiri Slaby2e124b42013-01-03 15:53:06 +01001244 tty_flip_buffer_push(&uap->port.state->port);
Linus Walleijead76f32011-02-24 13:21:08 +01001245 /*
1246 * If we were temporarily out of DMA mode for a while,
1247 * attempt to switch back to DMA mode again.
1248 */
1249 if (pl011_dma_rx_available(uap)) {
1250 if (pl011_dma_rx_trigger_dma(uap)) {
1251 dev_dbg(uap->port.dev, "could not trigger RX DMA job "
1252 "fall back to interrupt mode again\n");
1253 uap->im |= UART011_RXIM;
Russell Kingb2a4e242015-11-03 14:51:03 +00001254 pl011_write(uap->im, uap, UART011_IMSC);
Chanho Mincb06ff12013-03-27 18:38:11 +09001255 } else {
Chanho Min89fa28d2013-04-03 11:10:37 +09001256#ifdef CONFIG_DMA_ENGINE
Chanho Mincb06ff12013-03-27 18:38:11 +09001257 /* Start Rx DMA poll */
1258 if (uap->dmarx.poll_rate) {
1259 uap->dmarx.last_jiffies = jiffies;
1260 uap->dmarx.last_residue = PL011_DMA_BUFFER_SIZE;
1261 mod_timer(&uap->dmarx.timer,
1262 jiffies +
1263 msecs_to_jiffies(uap->dmarx.poll_rate));
1264 }
Chanho Min89fa28d2013-04-03 11:10:37 +09001265#endif
Chanho Mincb06ff12013-03-27 18:38:11 +09001266 }
Linus Walleijead76f32011-02-24 13:21:08 +01001267 }
Thomas Gleixner2389b272007-05-29 21:53:50 +01001268 spin_lock(&uap->port.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269}
1270
Dave Martin1e84d222015-04-27 16:49:05 +01001271static bool pl011_tx_char(struct uart_amba_port *uap, unsigned char c,
1272 bool from_irq)
Dave Martin734745c2015-03-04 12:27:33 +00001273{
Dave Martin1e84d222015-04-27 16:49:05 +01001274 if (unlikely(!from_irq) &&
Russell Kingb2a4e242015-11-03 14:51:03 +00001275 pl011_read(uap, UART01x_FR) & UART01x_FR_TXFF)
Dave Martin1e84d222015-04-27 16:49:05 +01001276 return false; /* unable to transmit character */
1277
Russell Kingb2a4e242015-11-03 14:51:03 +00001278 pl011_write(c, uap, UART01x_DR);
Dave Martin734745c2015-03-04 12:27:33 +00001279 uap->port.icount.tx++;
1280
Dave Martin1e84d222015-04-27 16:49:05 +01001281 return true;
Dave Martin734745c2015-03-04 12:27:33 +00001282}
1283
Dave Martin1e84d222015-04-27 16:49:05 +01001284static void pl011_tx_chars(struct uart_amba_port *uap, bool from_irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285{
Alan Coxebd2c8f2009-09-19 13:13:28 -07001286 struct circ_buf *xmit = &uap->port.state->xmit;
Dave Martin1e84d222015-04-27 16:49:05 +01001287 int count = uap->fifosize >> 1;
Dave Martin734745c2015-03-04 12:27:33 +00001288
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 if (uap->port.x_char) {
Dave Martin1e84d222015-04-27 16:49:05 +01001290 if (!pl011_tx_char(uap, uap->port.x_char, from_irq))
1291 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 uap->port.x_char = 0;
Dave Martin734745c2015-03-04 12:27:33 +00001293 --count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 }
1295 if (uart_circ_empty(xmit) || uart_tx_stopped(&uap->port)) {
Russell Kingb129a8c2005-08-31 10:12:14 +01001296 pl011_stop_tx(&uap->port);
Dave Martin1e84d222015-04-27 16:49:05 +01001297 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 }
1299
Russell King68b65f72010-12-22 17:24:39 +00001300 /* If we are using DMA mode, try to send some characters. */
1301 if (pl011_dma_tx_irq(uap))
Dave Martin1e84d222015-04-27 16:49:05 +01001302 return;
Russell King68b65f72010-12-22 17:24:39 +00001303
Dave Martin1e84d222015-04-27 16:49:05 +01001304 do {
1305 if (likely(from_irq) && count-- == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 break;
Dave Martin1e84d222015-04-27 16:49:05 +01001307
1308 if (!pl011_tx_char(uap, xmit->buf[xmit->tail], from_irq))
1309 break;
1310
1311 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
1312 } while (!uart_circ_empty(xmit));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313
1314 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
1315 uart_write_wakeup(&uap->port);
1316
Dave Martin1e84d222015-04-27 16:49:05 +01001317 if (uart_circ_empty(xmit))
Russell Kingb129a8c2005-08-31 10:12:14 +01001318 pl011_stop_tx(&uap->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319}
1320
1321static void pl011_modem_status(struct uart_amba_port *uap)
1322{
1323 unsigned int status, delta;
1324
Russell Kingb2a4e242015-11-03 14:51:03 +00001325 status = pl011_read(uap, UART01x_FR) & UART01x_FR_MODEM_ANY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326
1327 delta = status ^ uap->old_status;
1328 uap->old_status = status;
1329
1330 if (!delta)
1331 return;
1332
1333 if (delta & UART01x_FR_DCD)
1334 uart_handle_dcd_change(&uap->port, status & UART01x_FR_DCD);
1335
Greg Kroah-Hartman062a68a2015-09-04 09:11:24 -07001336 if (delta & UART01x_FR_DSR)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 uap->port.icount.dsr++;
1338
Greg Kroah-Hartman062a68a2015-09-04 09:11:24 -07001339 if (delta & UART01x_FR_CTS)
1340 uart_handle_cts_change(&uap->port, status & UART01x_FR_CTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341
Alan Coxbdc04e32009-09-19 13:13:31 -07001342 wake_up_interruptible(&uap->port.state->port.delta_msr_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343}
1344
Andre Przywara9c4ef4b2015-05-21 17:26:20 +01001345static void check_apply_cts_event_workaround(struct uart_amba_port *uap)
1346{
1347 unsigned int dummy_read;
1348
1349 if (!uap->vendor->cts_event_workaround)
1350 return;
1351
1352 /* workaround to make sure that all bits are unlocked.. */
Russell Kingb2a4e242015-11-03 14:51:03 +00001353 pl011_write(0x00, uap, UART011_ICR);
Andre Przywara9c4ef4b2015-05-21 17:26:20 +01001354
1355 /*
1356 * WA: introduce 26ns(1 uart clk) delay before W1C;
1357 * single apb access will incur 2 pclk(133.12Mhz) delay,
1358 * so add 2 dummy reads
1359 */
Russell Kingb2a4e242015-11-03 14:51:03 +00001360 dummy_read = pl011_read(uap, UART011_ICR);
1361 dummy_read = pl011_read(uap, UART011_ICR);
Andre Przywara9c4ef4b2015-05-21 17:26:20 +01001362}
1363
David Howells7d12e782006-10-05 14:55:46 +01001364static irqreturn_t pl011_int(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365{
1366 struct uart_amba_port *uap = dev_id;
Russell King963cc982010-12-22 17:16:09 +00001367 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 unsigned int status, pass_counter = AMBA_ISR_PASS_LIMIT;
Andre Przywara075167e2015-05-21 17:26:19 +01001369 u16 imsc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 int handled = 0;
1371
Russell King963cc982010-12-22 17:16:09 +00001372 spin_lock_irqsave(&uap->port.lock, flags);
Russell Kingb2a4e242015-11-03 14:51:03 +00001373 imsc = pl011_read(uap, UART011_IMSC);
1374 status = pl011_read(uap, UART011_RIS) & imsc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 if (status) {
1376 do {
Andre Przywara9c4ef4b2015-05-21 17:26:20 +01001377 check_apply_cts_event_workaround(uap);
Greg Kroah-Hartmanf11c9842015-09-04 09:13:39 -07001378
Russell King75836332015-11-03 14:50:58 +00001379 pl011_write(status & ~(UART011_TXIS|UART011_RTIS|
1380 UART011_RXIS),
Russell Kingb2a4e242015-11-03 14:51:03 +00001381 uap, UART011_ICR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382
Linus Walleijead76f32011-02-24 13:21:08 +01001383 if (status & (UART011_RTIS|UART011_RXIS)) {
1384 if (pl011_dma_rx_running(uap))
1385 pl011_dma_rx_irq(uap);
1386 else
1387 pl011_rx_chars(uap);
1388 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 if (status & (UART011_DSRMIS|UART011_DCDMIS|
1390 UART011_CTSMIS|UART011_RIMIS))
1391 pl011_modem_status(uap);
Dave Martin1e84d222015-04-27 16:49:05 +01001392 if (status & UART011_TXIS)
1393 pl011_tx_chars(uap, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394
Rajanikanth H.V4fd06902012-03-26 11:17:02 +02001395 if (pass_counter-- == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 break;
1397
Russell Kingb2a4e242015-11-03 14:51:03 +00001398 status = pl011_read(uap, UART011_RIS) & imsc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 } while (status != 0);
1400 handled = 1;
1401 }
1402
Russell King963cc982010-12-22 17:16:09 +00001403 spin_unlock_irqrestore(&uap->port.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404
1405 return IRQ_RETVAL(handled);
1406}
1407
Linus Walleije643f872012-06-17 15:44:19 +02001408static unsigned int pl011_tx_empty(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001410 struct uart_amba_port *uap =
1411 container_of(port, struct uart_amba_port, port);
Russell Kingb2a4e242015-11-03 14:51:03 +00001412 unsigned int status = pl011_read(uap, UART01x_FR);
Greg Kroah-Hartman062a68a2015-09-04 09:11:24 -07001413 return status & (UART01x_FR_BUSY|UART01x_FR_TXFF) ? 0 : TIOCSER_TEMT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414}
1415
Linus Walleije643f872012-06-17 15:44:19 +02001416static unsigned int pl011_get_mctrl(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001418 struct uart_amba_port *uap =
1419 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 unsigned int result = 0;
Russell Kingb2a4e242015-11-03 14:51:03 +00001421 unsigned int status = pl011_read(uap, UART01x_FR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422
Jiri Slaby5159f402007-10-18 23:40:31 -07001423#define TIOCMBIT(uartbit, tiocmbit) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 if (status & uartbit) \
1425 result |= tiocmbit
1426
Jiri Slaby5159f402007-10-18 23:40:31 -07001427 TIOCMBIT(UART01x_FR_DCD, TIOCM_CAR);
Greg Kroah-Hartman062a68a2015-09-04 09:11:24 -07001428 TIOCMBIT(UART01x_FR_DSR, TIOCM_DSR);
1429 TIOCMBIT(UART01x_FR_CTS, TIOCM_CTS);
1430 TIOCMBIT(UART011_FR_RI, TIOCM_RNG);
Jiri Slaby5159f402007-10-18 23:40:31 -07001431#undef TIOCMBIT
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 return result;
1433}
1434
1435static void pl011_set_mctrl(struct uart_port *port, unsigned int mctrl)
1436{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001437 struct uart_amba_port *uap =
1438 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 unsigned int cr;
1440
Russell Kingb2a4e242015-11-03 14:51:03 +00001441 cr = pl011_read(uap, UART011_CR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442
Jiri Slaby5159f402007-10-18 23:40:31 -07001443#define TIOCMBIT(tiocmbit, uartbit) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 if (mctrl & tiocmbit) \
1445 cr |= uartbit; \
1446 else \
1447 cr &= ~uartbit
1448
Jiri Slaby5159f402007-10-18 23:40:31 -07001449 TIOCMBIT(TIOCM_RTS, UART011_CR_RTS);
1450 TIOCMBIT(TIOCM_DTR, UART011_CR_DTR);
1451 TIOCMBIT(TIOCM_OUT1, UART011_CR_OUT1);
1452 TIOCMBIT(TIOCM_OUT2, UART011_CR_OUT2);
1453 TIOCMBIT(TIOCM_LOOP, UART011_CR_LBE);
Rabin Vincent3b438162010-02-12 06:43:11 +01001454
1455 if (uap->autorts) {
1456 /* We need to disable auto-RTS if we want to turn RTS off */
1457 TIOCMBIT(TIOCM_RTS, UART011_CR_RTSEN);
1458 }
Jiri Slaby5159f402007-10-18 23:40:31 -07001459#undef TIOCMBIT
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460
Russell Kingb2a4e242015-11-03 14:51:03 +00001461 pl011_write(cr, uap, UART011_CR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462}
1463
1464static void pl011_break_ctl(struct uart_port *port, int break_state)
1465{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001466 struct uart_amba_port *uap =
1467 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 unsigned long flags;
1469 unsigned int lcr_h;
1470
1471 spin_lock_irqsave(&uap->port.lock, flags);
Russell Kingb2a4e242015-11-03 14:51:03 +00001472 lcr_h = pl011_read(uap, uap->lcrh_tx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 if (break_state == -1)
1474 lcr_h |= UART01x_LCRH_BRK;
1475 else
1476 lcr_h &= ~UART01x_LCRH_BRK;
Russell Kingb2a4e242015-11-03 14:51:03 +00001477 pl011_write(lcr_h, uap, uap->lcrh_tx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 spin_unlock_irqrestore(&uap->port.lock, flags);
1479}
1480
Jason Wessel84b5ae12008-02-20 13:33:39 -06001481#ifdef CONFIG_CONSOLE_POLL
Anton Vorontsov5c8124a2012-09-24 14:27:55 -07001482
1483static void pl011_quiesce_irqs(struct uart_port *port)
1484{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001485 struct uart_amba_port *uap =
1486 container_of(port, struct uart_amba_port, port);
Anton Vorontsov5c8124a2012-09-24 14:27:55 -07001487
Russell Kingb2a4e242015-11-03 14:51:03 +00001488 pl011_write(pl011_read(uap, UART011_MIS), uap, UART011_ICR);
Anton Vorontsov5c8124a2012-09-24 14:27:55 -07001489 /*
1490 * There is no way to clear TXIM as this is "ready to transmit IRQ", so
1491 * we simply mask it. start_tx() will unmask it.
1492 *
1493 * Note we can race with start_tx(), and if the race happens, the
1494 * polling user might get another interrupt just after we clear it.
1495 * But it should be OK and can happen even w/o the race, e.g.
1496 * controller immediately got some new data and raised the IRQ.
1497 *
1498 * And whoever uses polling routines assumes that it manages the device
1499 * (including tx queue), so we're also fine with start_tx()'s caller
1500 * side.
1501 */
Russell Kingb2a4e242015-11-03 14:51:03 +00001502 pl011_write(pl011_read(uap, UART011_IMSC) & ~UART011_TXIM, uap,
1503 UART011_IMSC);
Anton Vorontsov5c8124a2012-09-24 14:27:55 -07001504}
1505
Linus Walleije643f872012-06-17 15:44:19 +02001506static int pl011_get_poll_char(struct uart_port *port)
Jason Wessel84b5ae12008-02-20 13:33:39 -06001507{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001508 struct uart_amba_port *uap =
1509 container_of(port, struct uart_amba_port, port);
Jason Wessel84b5ae12008-02-20 13:33:39 -06001510 unsigned int status;
1511
Anton Vorontsov5c8124a2012-09-24 14:27:55 -07001512 /*
1513 * The caller might need IRQs lowered, e.g. if used with KDB NMI
1514 * debugger.
1515 */
1516 pl011_quiesce_irqs(port);
1517
Russell Kingb2a4e242015-11-03 14:51:03 +00001518 status = pl011_read(uap, UART01x_FR);
Jason Wesself5316b42010-05-20 21:04:22 -05001519 if (status & UART01x_FR_RXFE)
1520 return NO_POLL_CHAR;
Jason Wessel84b5ae12008-02-20 13:33:39 -06001521
Russell Kingb2a4e242015-11-03 14:51:03 +00001522 return pl011_read(uap, UART01x_DR);
Jason Wessel84b5ae12008-02-20 13:33:39 -06001523}
1524
Linus Walleije643f872012-06-17 15:44:19 +02001525static void pl011_put_poll_char(struct uart_port *port,
Jason Wessel84b5ae12008-02-20 13:33:39 -06001526 unsigned char ch)
1527{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001528 struct uart_amba_port *uap =
1529 container_of(port, struct uart_amba_port, port);
Jason Wessel84b5ae12008-02-20 13:33:39 -06001530
Russell Kingb2a4e242015-11-03 14:51:03 +00001531 while (pl011_read(uap, UART01x_FR) & UART01x_FR_TXFF)
Jason Wessel84b5ae12008-02-20 13:33:39 -06001532 barrier();
1533
Russell Kingb2a4e242015-11-03 14:51:03 +00001534 pl011_write(ch, uap, UART01x_DR);
Jason Wessel84b5ae12008-02-20 13:33:39 -06001535}
1536
1537#endif /* CONFIG_CONSOLE_POLL */
1538
Anton Vorontsovb3564c22012-09-24 14:27:54 -07001539static int pl011_hwinit(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001541 struct uart_amba_port *uap =
1542 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 int retval;
1544
Linus Walleij78d80c52012-05-23 21:18:46 +02001545 /* Optionaly enable pins to be muxed in and configured */
Linus Walleij2b996fc2013-06-05 15:36:42 +02001546 pinctrl_pm_select_default_state(port->dev);
Linus Walleij78d80c52012-05-23 21:18:46 +02001547
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 /*
1549 * Try to enable the clock producer.
1550 */
Julia Lawall1c4c4392012-08-26 18:01:01 +02001551 retval = clk_prepare_enable(uap->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 if (retval)
Tushar Behera7f6d9422014-06-26 15:35:35 +05301553 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554
1555 uap->port.uartclk = clk_get_rate(uap->clk);
1556
Linus Walleij9b96fba2012-03-13 13:27:23 +01001557 /* Clear pending error and receive interrupts */
Russell King75836332015-11-03 14:50:58 +00001558 pl011_write(UART011_OEIS | UART011_BEIS | UART011_PEIS |
1559 UART011_FEIS | UART011_RTIS | UART011_RXIS,
Russell Kingb2a4e242015-11-03 14:51:03 +00001560 uap, UART011_ICR);
Linus Walleij9b96fba2012-03-13 13:27:23 +01001561
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 /*
Anton Vorontsovb3564c22012-09-24 14:27:54 -07001563 * Save interrupts enable mask, and enable RX interrupts in case if
1564 * the interrupt is used for NMI entry.
1565 */
Russell Kingb2a4e242015-11-03 14:51:03 +00001566 uap->im = pl011_read(uap, UART011_IMSC);
1567 pl011_write(UART011_RTIM | UART011_RXIM, uap, UART011_IMSC);
Anton Vorontsovb3564c22012-09-24 14:27:54 -07001568
Jingoo Han574de552013-07-30 17:06:57 +09001569 if (dev_get_platdata(uap->port.dev)) {
Anton Vorontsovb3564c22012-09-24 14:27:54 -07001570 struct amba_pl011_data *plat;
1571
Jingoo Han574de552013-07-30 17:06:57 +09001572 plat = dev_get_platdata(uap->port.dev);
Anton Vorontsovb3564c22012-09-24 14:27:54 -07001573 if (plat->init)
1574 plat->init();
1575 }
1576 return 0;
Anton Vorontsovb3564c22012-09-24 14:27:54 -07001577}
1578
Jon Medhurstb60f2f62013-12-10 10:18:59 +00001579static void pl011_write_lcr_h(struct uart_amba_port *uap, unsigned int lcr_h)
1580{
Russell Kingb2a4e242015-11-03 14:51:03 +00001581 pl011_write(lcr_h, uap, uap->lcrh_rx);
Greg Kroah-Hartman8e502542015-09-04 09:12:03 -07001582 if (uap->lcrh_rx != uap->lcrh_tx) {
Jon Medhurstb60f2f62013-12-10 10:18:59 +00001583 int i;
1584 /*
1585 * Wait 10 PCLKs before writing LCRH_TX register,
1586 * to get this delay write read only register 10 times
1587 */
1588 for (i = 0; i < 10; ++i)
Russell Kingb2a4e242015-11-03 14:51:03 +00001589 pl011_write(0xff, uap, UART011_MIS);
1590 pl011_write(lcr_h, uap, uap->lcrh_tx);
Jon Medhurstb60f2f62013-12-10 10:18:59 +00001591 }
1592}
1593
Andre Przywara867b8e82015-05-21 17:26:15 +01001594static int pl011_allocate_irq(struct uart_amba_port *uap)
1595{
Russell Kingb2a4e242015-11-03 14:51:03 +00001596 pl011_write(uap->im, uap, UART011_IMSC);
Andre Przywara867b8e82015-05-21 17:26:15 +01001597
1598 return request_irq(uap->port.irq, pl011_int, 0, "uart-pl011", uap);
1599}
1600
1601/*
1602 * Enable interrupts, only timeouts when using DMA
1603 * if initial RX DMA job failed, start in interrupt mode
1604 * as well.
1605 */
1606static void pl011_enable_interrupts(struct uart_amba_port *uap)
1607{
1608 spin_lock_irq(&uap->port.lock);
1609
1610 /* Clear out any spuriously appearing RX interrupts */
Russell Kingb2a4e242015-11-03 14:51:03 +00001611 pl011_write(UART011_RTIS | UART011_RXIS, uap, UART011_ICR);
Andre Przywara867b8e82015-05-21 17:26:15 +01001612 uap->im = UART011_RTIM;
1613 if (!pl011_dma_rx_running(uap))
1614 uap->im |= UART011_RXIM;
Russell Kingb2a4e242015-11-03 14:51:03 +00001615 pl011_write(uap->im, uap, UART011_IMSC);
Andre Przywara867b8e82015-05-21 17:26:15 +01001616 spin_unlock_irq(&uap->port.lock);
1617}
1618
Anton Vorontsovb3564c22012-09-24 14:27:54 -07001619static int pl011_startup(struct uart_port *port)
1620{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001621 struct uart_amba_port *uap =
1622 container_of(port, struct uart_amba_port, port);
Dave Martin734745c2015-03-04 12:27:33 +00001623 unsigned int cr;
Anton Vorontsovb3564c22012-09-24 14:27:54 -07001624 int retval;
1625
1626 retval = pl011_hwinit(port);
1627 if (retval)
1628 goto clk_dis;
1629
Andre Przywara867b8e82015-05-21 17:26:15 +01001630 retval = pl011_allocate_irq(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 if (retval)
1632 goto clk_dis;
1633
Russell Kingb2a4e242015-11-03 14:51:03 +00001634 pl011_write(uap->vendor->ifls, uap, UART011_IFLS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635
Jon Medhurstfe433902013-12-10 10:18:58 +00001636 spin_lock_irq(&uap->port.lock);
1637
Shreshtha Kumar Sahud8d8ffa2012-01-18 15:53:59 +05301638 /* restore RTS and DTR */
1639 cr = uap->old_cr & (UART011_CR_RTS | UART011_CR_DTR);
1640 cr |= UART01x_CR_UARTEN | UART011_CR_RXE | UART011_CR_TXE;
Russell Kingb2a4e242015-11-03 14:51:03 +00001641 pl011_write(cr, uap, UART011_CR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642
Jon Medhurstfe433902013-12-10 10:18:58 +00001643 spin_unlock_irq(&uap->port.lock);
1644
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 /*
1646 * initialise the old status of the modem signals
1647 */
Russell Kingb2a4e242015-11-03 14:51:03 +00001648 uap->old_status = pl011_read(uap, UART01x_FR) & UART01x_FR_MODEM_ANY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649
Russell King68b65f72010-12-22 17:24:39 +00001650 /* Startup DMA */
1651 pl011_dma_startup(uap);
1652
Andre Przywara867b8e82015-05-21 17:26:15 +01001653 pl011_enable_interrupts(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654
1655 return 0;
1656
1657 clk_dis:
Julia Lawall1c4c4392012-08-26 18:01:01 +02001658 clk_disable_unprepare(uap->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 return retval;
1660}
1661
Andre Przywara0dd1e242015-05-21 17:26:23 +01001662static int sbsa_uart_startup(struct uart_port *port)
1663{
1664 struct uart_amba_port *uap =
1665 container_of(port, struct uart_amba_port, port);
1666 int retval;
1667
1668 retval = pl011_hwinit(port);
1669 if (retval)
1670 return retval;
1671
1672 retval = pl011_allocate_irq(uap);
1673 if (retval)
1674 return retval;
1675
1676 /* The SBSA UART does not support any modem status lines. */
1677 uap->old_status = 0;
1678
1679 pl011_enable_interrupts(uap);
1680
1681 return 0;
1682}
1683
Linus Walleijec489aa2010-06-02 08:13:52 +01001684static void pl011_shutdown_channel(struct uart_amba_port *uap,
1685 unsigned int lcrh)
1686{
Greg Kroah-Hartmanf11c9842015-09-04 09:13:39 -07001687 unsigned long val;
Linus Walleijec489aa2010-06-02 08:13:52 +01001688
Russell Kingb2a4e242015-11-03 14:51:03 +00001689 val = pl011_read(uap, lcrh);
Greg Kroah-Hartmanf11c9842015-09-04 09:13:39 -07001690 val &= ~(UART01x_LCRH_BRK | UART01x_LCRH_FEN);
Russell Kingb2a4e242015-11-03 14:51:03 +00001691 pl011_write(val, uap, lcrh);
Linus Walleijec489aa2010-06-02 08:13:52 +01001692}
1693
Andre Przywara95166a32015-05-21 17:26:16 +01001694/*
1695 * disable the port. It should not disable RTS and DTR.
1696 * Also RTS and DTR state should be preserved to restore
1697 * it during startup().
1698 */
1699static void pl011_disable_uart(struct uart_amba_port *uap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700{
Shreshtha Kumar Sahud8d8ffa2012-01-18 15:53:59 +05301701 unsigned int cr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702
Rabin Vincent3b438162010-02-12 06:43:11 +01001703 uap->autorts = false;
Jon Medhurstfe433902013-12-10 10:18:58 +00001704 spin_lock_irq(&uap->port.lock);
Russell Kingb2a4e242015-11-03 14:51:03 +00001705 cr = pl011_read(uap, UART011_CR);
Shreshtha Kumar Sahud8d8ffa2012-01-18 15:53:59 +05301706 uap->old_cr = cr;
1707 cr &= UART011_CR_RTS | UART011_CR_DTR;
1708 cr |= UART01x_CR_UARTEN | UART011_CR_TXE;
Russell Kingb2a4e242015-11-03 14:51:03 +00001709 pl011_write(cr, uap, UART011_CR);
Jon Medhurstfe433902013-12-10 10:18:58 +00001710 spin_unlock_irq(&uap->port.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711
1712 /*
1713 * disable break condition and fifos
1714 */
Linus Walleijec489aa2010-06-02 08:13:52 +01001715 pl011_shutdown_channel(uap, uap->lcrh_rx);
Greg Kroah-Hartman8e502542015-09-04 09:12:03 -07001716 if (uap->lcrh_rx != uap->lcrh_tx)
Linus Walleijec489aa2010-06-02 08:13:52 +01001717 pl011_shutdown_channel(uap, uap->lcrh_tx);
Andre Przywara95166a32015-05-21 17:26:16 +01001718}
1719
1720static void pl011_disable_interrupts(struct uart_amba_port *uap)
1721{
1722 spin_lock_irq(&uap->port.lock);
1723
1724 /* mask all interrupts and clear all pending ones */
1725 uap->im = 0;
Russell Kingb2a4e242015-11-03 14:51:03 +00001726 pl011_write(uap->im, uap, UART011_IMSC);
1727 pl011_write(0xffff, uap, UART011_ICR);
Andre Przywara95166a32015-05-21 17:26:16 +01001728
1729 spin_unlock_irq(&uap->port.lock);
1730}
1731
1732static void pl011_shutdown(struct uart_port *port)
1733{
1734 struct uart_amba_port *uap =
1735 container_of(port, struct uart_amba_port, port);
1736
1737 pl011_disable_interrupts(uap);
1738
1739 pl011_dma_shutdown(uap);
1740
1741 free_irq(uap->port.irq, uap);
1742
1743 pl011_disable_uart(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744
1745 /*
1746 * Shut down the clock producer
1747 */
Julia Lawall1c4c4392012-08-26 18:01:01 +02001748 clk_disable_unprepare(uap->clk);
Linus Walleij78d80c52012-05-23 21:18:46 +02001749 /* Optionally let pins go into sleep states */
Linus Walleij2b996fc2013-06-05 15:36:42 +02001750 pinctrl_pm_select_sleep_state(port->dev);
Shreshtha Kumar Sahuc16d51a2011-06-13 10:11:33 +02001751
Jingoo Han574de552013-07-30 17:06:57 +09001752 if (dev_get_platdata(uap->port.dev)) {
Shreshtha Kumar Sahuc16d51a2011-06-13 10:11:33 +02001753 struct amba_pl011_data *plat;
1754
Jingoo Han574de552013-07-30 17:06:57 +09001755 plat = dev_get_platdata(uap->port.dev);
Shreshtha Kumar Sahuc16d51a2011-06-13 10:11:33 +02001756 if (plat->exit)
1757 plat->exit();
1758 }
1759
Peter Hurley36f339d2014-11-06 09:06:12 -05001760 if (uap->port.ops->flush_buffer)
1761 uap->port.ops->flush_buffer(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762}
1763
Andre Przywara0dd1e242015-05-21 17:26:23 +01001764static void sbsa_uart_shutdown(struct uart_port *port)
1765{
1766 struct uart_amba_port *uap =
1767 container_of(port, struct uart_amba_port, port);
1768
1769 pl011_disable_interrupts(uap);
1770
1771 free_irq(uap->port.irq, uap);
1772
1773 if (uap->port.ops->flush_buffer)
1774 uap->port.ops->flush_buffer(port);
1775}
1776
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777static void
Andre Przywaraef5a9352015-05-21 17:26:17 +01001778pl011_setup_status_masks(struct uart_port *port, struct ktermios *termios)
1779{
1780 port->read_status_mask = UART011_DR_OE | 255;
1781 if (termios->c_iflag & INPCK)
1782 port->read_status_mask |= UART011_DR_FE | UART011_DR_PE;
1783 if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
1784 port->read_status_mask |= UART011_DR_BE;
1785
1786 /*
1787 * Characters to ignore
1788 */
1789 port->ignore_status_mask = 0;
1790 if (termios->c_iflag & IGNPAR)
1791 port->ignore_status_mask |= UART011_DR_FE | UART011_DR_PE;
1792 if (termios->c_iflag & IGNBRK) {
1793 port->ignore_status_mask |= UART011_DR_BE;
1794 /*
1795 * If we're ignoring parity and break indicators,
1796 * ignore overruns too (for real raw support).
1797 */
1798 if (termios->c_iflag & IGNPAR)
1799 port->ignore_status_mask |= UART011_DR_OE;
1800 }
1801
1802 /*
1803 * Ignore all characters if CREAD is not set.
1804 */
1805 if ((termios->c_cflag & CREAD) == 0)
1806 port->ignore_status_mask |= UART_DUMMY_DR_RX;
1807}
1808
1809static void
Alan Cox606d0992006-12-08 02:38:45 -08001810pl011_set_termios(struct uart_port *port, struct ktermios *termios,
1811 struct ktermios *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001813 struct uart_amba_port *uap =
1814 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 unsigned int lcr_h, old_cr;
1816 unsigned long flags;
Russell Kingc19f12b2010-12-22 17:48:26 +00001817 unsigned int baud, quot, clkdiv;
1818
1819 if (uap->vendor->oversampling)
1820 clkdiv = 8;
1821 else
1822 clkdiv = 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823
1824 /*
1825 * Ask the core to calculate the divisor for us.
1826 */
Linus Walleijac3e3fb2010-06-02 20:40:22 +01001827 baud = uart_get_baud_rate(port, termios, old, 0,
Russell Kingc19f12b2010-12-22 17:48:26 +00001828 port->uartclk / clkdiv);
Chanho Min89fa28d2013-04-03 11:10:37 +09001829#ifdef CONFIG_DMA_ENGINE
Chanho Mincb06ff12013-03-27 18:38:11 +09001830 /*
1831 * Adjust RX DMA polling rate with baud rate if not specified.
1832 */
1833 if (uap->dmarx.auto_poll_rate)
1834 uap->dmarx.poll_rate = DIV_ROUND_UP(10000000, baud);
Chanho Min89fa28d2013-04-03 11:10:37 +09001835#endif
Linus Walleijac3e3fb2010-06-02 20:40:22 +01001836
1837 if (baud > port->uartclk/16)
1838 quot = DIV_ROUND_CLOSEST(port->uartclk * 8, baud);
1839 else
1840 quot = DIV_ROUND_CLOSEST(port->uartclk * 4, baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841
1842 switch (termios->c_cflag & CSIZE) {
1843 case CS5:
1844 lcr_h = UART01x_LCRH_WLEN_5;
1845 break;
1846 case CS6:
1847 lcr_h = UART01x_LCRH_WLEN_6;
1848 break;
1849 case CS7:
1850 lcr_h = UART01x_LCRH_WLEN_7;
1851 break;
1852 default: // CS8
1853 lcr_h = UART01x_LCRH_WLEN_8;
1854 break;
1855 }
1856 if (termios->c_cflag & CSTOPB)
1857 lcr_h |= UART01x_LCRH_STP2;
1858 if (termios->c_cflag & PARENB) {
1859 lcr_h |= UART01x_LCRH_PEN;
1860 if (!(termios->c_cflag & PARODD))
1861 lcr_h |= UART01x_LCRH_EPS;
1862 }
Russell Kingffca2b12010-12-22 17:13:05 +00001863 if (uap->fifosize > 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 lcr_h |= UART01x_LCRH_FEN;
1865
1866 spin_lock_irqsave(&port->lock, flags);
1867
1868 /*
1869 * Update the per-port timeout.
1870 */
1871 uart_update_timeout(port, termios->c_cflag, baud);
1872
Andre Przywaraef5a9352015-05-21 17:26:17 +01001873 pl011_setup_status_masks(port, termios);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874
1875 if (UART_ENABLE_MS(port, termios->c_cflag))
1876 pl011_enable_ms(port);
1877
1878 /* first, disable everything */
Russell Kingb2a4e242015-11-03 14:51:03 +00001879 old_cr = pl011_read(uap, UART011_CR);
1880 pl011_write(0, uap, UART011_CR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881
Rabin Vincent3b438162010-02-12 06:43:11 +01001882 if (termios->c_cflag & CRTSCTS) {
1883 if (old_cr & UART011_CR_RTS)
1884 old_cr |= UART011_CR_RTSEN;
1885
1886 old_cr |= UART011_CR_CTSEN;
1887 uap->autorts = true;
1888 } else {
1889 old_cr &= ~(UART011_CR_CTSEN | UART011_CR_RTSEN);
1890 uap->autorts = false;
1891 }
1892
Russell Kingc19f12b2010-12-22 17:48:26 +00001893 if (uap->vendor->oversampling) {
1894 if (baud > port->uartclk / 16)
Linus Walleijac3e3fb2010-06-02 20:40:22 +01001895 old_cr |= ST_UART011_CR_OVSFACT;
1896 else
1897 old_cr &= ~ST_UART011_CR_OVSFACT;
1898 }
1899
Linus Walleijc5dd5532012-09-26 17:21:36 +02001900 /*
1901 * Workaround for the ST Micro oversampling variants to
1902 * increase the bitrate slightly, by lowering the divisor,
1903 * to avoid delayed sampling of start bit at high speeds,
1904 * else we see data corruption.
1905 */
1906 if (uap->vendor->oversampling) {
1907 if ((baud >= 3000000) && (baud < 3250000) && (quot > 1))
1908 quot -= 1;
1909 else if ((baud > 3250000) && (quot > 2))
1910 quot -= 2;
1911 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 /* Set baud rate */
Russell Kingb2a4e242015-11-03 14:51:03 +00001913 pl011_write(quot & 0x3f, uap, UART011_FBRD);
1914 pl011_write(quot >> 6, uap, UART011_IBRD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915
1916 /*
1917 * ----------v----------v----------v----------v-----
Linus Walleijc5dd5532012-09-26 17:21:36 +02001918 * NOTE: lcrh_tx and lcrh_rx MUST BE WRITTEN AFTER
Greg Kroah-Hartman0de6cfb2015-09-04 09:13:56 -07001919 * UART011_FBRD & UART011_IBRD.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 * ----------^----------^----------^----------^-----
1921 */
Jon Medhurstb60f2f62013-12-10 10:18:59 +00001922 pl011_write_lcr_h(uap, lcr_h);
Russell Kingb2a4e242015-11-03 14:51:03 +00001923 pl011_write(old_cr, uap, UART011_CR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924
1925 spin_unlock_irqrestore(&port->lock, flags);
1926}
1927
Andre Przywara0dd1e242015-05-21 17:26:23 +01001928static void
1929sbsa_uart_set_termios(struct uart_port *port, struct ktermios *termios,
1930 struct ktermios *old)
1931{
1932 struct uart_amba_port *uap =
1933 container_of(port, struct uart_amba_port, port);
1934 unsigned long flags;
1935
1936 tty_termios_encode_baud_rate(termios, uap->fixed_baud, uap->fixed_baud);
1937
1938 /* The SBSA UART only supports 8n1 without hardware flow control. */
1939 termios->c_cflag &= ~(CSIZE | CSTOPB | PARENB | PARODD);
1940 termios->c_cflag &= ~(CMSPAR | CRTSCTS);
1941 termios->c_cflag |= CS8 | CLOCAL;
1942
1943 spin_lock_irqsave(&port->lock, flags);
1944 uart_update_timeout(port, CS8, uap->fixed_baud);
1945 pl011_setup_status_masks(port, termios);
1946 spin_unlock_irqrestore(&port->lock, flags);
1947}
1948
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949static const char *pl011_type(struct uart_port *port)
1950{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001951 struct uart_amba_port *uap =
1952 container_of(port, struct uart_amba_port, port);
Russell Kinge8a7ba82010-12-28 09:16:54 +00001953 return uap->port.type == PORT_AMBA ? uap->type : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954}
1955
1956/*
1957 * Release the memory region(s) being used by 'port'
1958 */
Linus Walleije643f872012-06-17 15:44:19 +02001959static void pl011_release_port(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960{
1961 release_mem_region(port->mapbase, SZ_4K);
1962}
1963
1964/*
1965 * Request the memory region(s) being used by 'port'
1966 */
Linus Walleije643f872012-06-17 15:44:19 +02001967static int pl011_request_port(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968{
1969 return request_mem_region(port->mapbase, SZ_4K, "uart-pl011")
1970 != NULL ? 0 : -EBUSY;
1971}
1972
1973/*
1974 * Configure/autoconfigure the port.
1975 */
Linus Walleije643f872012-06-17 15:44:19 +02001976static void pl011_config_port(struct uart_port *port, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977{
1978 if (flags & UART_CONFIG_TYPE) {
1979 port->type = PORT_AMBA;
Linus Walleije643f872012-06-17 15:44:19 +02001980 pl011_request_port(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 }
1982}
1983
1984/*
1985 * verify the new serial_struct (for TIOCSSERIAL).
1986 */
Linus Walleije643f872012-06-17 15:44:19 +02001987static int pl011_verify_port(struct uart_port *port, struct serial_struct *ser)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988{
1989 int ret = 0;
1990 if (ser->type != PORT_UNKNOWN && ser->type != PORT_AMBA)
1991 ret = -EINVAL;
Yinghai Lua62c4132008-08-19 20:49:55 -07001992 if (ser->irq < 0 || ser->irq >= nr_irqs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 ret = -EINVAL;
1994 if (ser->baud_base < 9600)
1995 ret = -EINVAL;
1996 return ret;
1997}
1998
1999static struct uart_ops amba_pl011_pops = {
Linus Walleije643f872012-06-17 15:44:19 +02002000 .tx_empty = pl011_tx_empty,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 .set_mctrl = pl011_set_mctrl,
Linus Walleije643f872012-06-17 15:44:19 +02002002 .get_mctrl = pl011_get_mctrl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 .stop_tx = pl011_stop_tx,
2004 .start_tx = pl011_start_tx,
2005 .stop_rx = pl011_stop_rx,
2006 .enable_ms = pl011_enable_ms,
2007 .break_ctl = pl011_break_ctl,
2008 .startup = pl011_startup,
2009 .shutdown = pl011_shutdown,
Russell King68b65f72010-12-22 17:24:39 +00002010 .flush_buffer = pl011_dma_flush_buffer,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 .set_termios = pl011_set_termios,
2012 .type = pl011_type,
Linus Walleije643f872012-06-17 15:44:19 +02002013 .release_port = pl011_release_port,
2014 .request_port = pl011_request_port,
2015 .config_port = pl011_config_port,
2016 .verify_port = pl011_verify_port,
Jason Wessel84b5ae12008-02-20 13:33:39 -06002017#ifdef CONFIG_CONSOLE_POLL
Anton Vorontsovb3564c22012-09-24 14:27:54 -07002018 .poll_init = pl011_hwinit,
Linus Walleije643f872012-06-17 15:44:19 +02002019 .poll_get_char = pl011_get_poll_char,
2020 .poll_put_char = pl011_put_poll_char,
Jason Wessel84b5ae12008-02-20 13:33:39 -06002021#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022};
2023
Andre Przywara0dd1e242015-05-21 17:26:23 +01002024static void sbsa_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
2025{
2026}
2027
2028static unsigned int sbsa_uart_get_mctrl(struct uart_port *port)
2029{
2030 return 0;
2031}
2032
2033static const struct uart_ops sbsa_uart_pops = {
2034 .tx_empty = pl011_tx_empty,
2035 .set_mctrl = sbsa_uart_set_mctrl,
2036 .get_mctrl = sbsa_uart_get_mctrl,
2037 .stop_tx = pl011_stop_tx,
2038 .start_tx = pl011_start_tx,
2039 .stop_rx = pl011_stop_rx,
2040 .startup = sbsa_uart_startup,
2041 .shutdown = sbsa_uart_shutdown,
2042 .set_termios = sbsa_uart_set_termios,
2043 .type = pl011_type,
2044 .release_port = pl011_release_port,
2045 .request_port = pl011_request_port,
2046 .config_port = pl011_config_port,
2047 .verify_port = pl011_verify_port,
2048#ifdef CONFIG_CONSOLE_POLL
2049 .poll_init = pl011_hwinit,
2050 .poll_get_char = pl011_get_poll_char,
2051 .poll_put_char = pl011_put_poll_char,
2052#endif
2053};
2054
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055static struct uart_amba_port *amba_ports[UART_NR];
2056
2057#ifdef CONFIG_SERIAL_AMBA_PL011_CONSOLE
2058
Russell Kingd3587882006-03-20 20:00:09 +00002059static void pl011_console_putchar(struct uart_port *port, int ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060{
Daniel Thompsona5820c22014-09-03 12:51:55 +01002061 struct uart_amba_port *uap =
2062 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063
Russell Kingb2a4e242015-11-03 14:51:03 +00002064 while (pl011_read(uap, UART01x_FR) & UART01x_FR_TXFF)
Russell Kingd3587882006-03-20 20:00:09 +00002065 barrier();
Russell Kingb2a4e242015-11-03 14:51:03 +00002066 pl011_write(ch, uap, UART01x_DR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067}
2068
2069static void
2070pl011_console_write(struct console *co, const char *s, unsigned int count)
2071{
2072 struct uart_amba_port *uap = amba_ports[co->index];
Andre Przywara71eec482015-05-21 17:26:21 +01002073 unsigned int status, old_cr = 0, new_cr;
Rabin Vincentef605fd2012-01-17 11:52:28 +01002074 unsigned long flags;
2075 int locked = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076
2077 clk_enable(uap->clk);
2078
Rabin Vincentef605fd2012-01-17 11:52:28 +01002079 local_irq_save(flags);
2080 if (uap->port.sysrq)
2081 locked = 0;
2082 else if (oops_in_progress)
2083 locked = spin_trylock(&uap->port.lock);
2084 else
2085 spin_lock(&uap->port.lock);
2086
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 /*
2088 * First save the CR then disable the interrupts
2089 */
Andre Przywara71eec482015-05-21 17:26:21 +01002090 if (!uap->vendor->always_enabled) {
Russell Kingb2a4e242015-11-03 14:51:03 +00002091 old_cr = pl011_read(uap, UART011_CR);
Andre Przywara71eec482015-05-21 17:26:21 +01002092 new_cr = old_cr & ~UART011_CR_CTSEN;
2093 new_cr |= UART01x_CR_UARTEN | UART011_CR_TXE;
Russell Kingb2a4e242015-11-03 14:51:03 +00002094 pl011_write(new_cr, uap, UART011_CR);
Andre Przywara71eec482015-05-21 17:26:21 +01002095 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096
Russell Kingd3587882006-03-20 20:00:09 +00002097 uart_console_write(&uap->port, s, count, pl011_console_putchar);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098
2099 /*
2100 * Finally, wait for transmitter to become empty
2101 * and restore the TCR
2102 */
2103 do {
Russell Kingb2a4e242015-11-03 14:51:03 +00002104 status = pl011_read(uap, UART01x_FR);
Greg Kroah-Hartman062a68a2015-09-04 09:11:24 -07002105 } while (status & UART01x_FR_BUSY);
Andre Przywara71eec482015-05-21 17:26:21 +01002106 if (!uap->vendor->always_enabled)
Russell Kingb2a4e242015-11-03 14:51:03 +00002107 pl011_write(old_cr, uap, UART011_CR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108
Rabin Vincentef605fd2012-01-17 11:52:28 +01002109 if (locked)
2110 spin_unlock(&uap->port.lock);
2111 local_irq_restore(flags);
2112
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 clk_disable(uap->clk);
2114}
2115
2116static void __init
2117pl011_console_get_options(struct uart_amba_port *uap, int *baud,
2118 int *parity, int *bits)
2119{
Russell Kingb2a4e242015-11-03 14:51:03 +00002120 if (pl011_read(uap, UART011_CR) & UART01x_CR_UARTEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 unsigned int lcr_h, ibrd, fbrd;
2122
Russell Kingb2a4e242015-11-03 14:51:03 +00002123 lcr_h = pl011_read(uap, uap->lcrh_tx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124
2125 *parity = 'n';
2126 if (lcr_h & UART01x_LCRH_PEN) {
2127 if (lcr_h & UART01x_LCRH_EPS)
2128 *parity = 'e';
2129 else
2130 *parity = 'o';
2131 }
2132
2133 if ((lcr_h & 0x60) == UART01x_LCRH_WLEN_7)
2134 *bits = 7;
2135 else
2136 *bits = 8;
2137
Russell Kingb2a4e242015-11-03 14:51:03 +00002138 ibrd = pl011_read(uap, UART011_IBRD);
2139 fbrd = pl011_read(uap, UART011_FBRD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140
2141 *baud = uap->port.uartclk * 4 / (64 * ibrd + fbrd);
Linus Walleijac3e3fb2010-06-02 20:40:22 +01002142
Russell Kingc19f12b2010-12-22 17:48:26 +00002143 if (uap->vendor->oversampling) {
Russell Kingb2a4e242015-11-03 14:51:03 +00002144 if (pl011_read(uap, UART011_CR)
Linus Walleijac3e3fb2010-06-02 20:40:22 +01002145 & ST_UART011_CR_OVSFACT)
2146 *baud *= 2;
2147 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 }
2149}
2150
2151static int __init pl011_console_setup(struct console *co, char *options)
2152{
2153 struct uart_amba_port *uap;
2154 int baud = 38400;
2155 int bits = 8;
2156 int parity = 'n';
2157 int flow = 'n';
Russell King4b4851c2011-09-22 11:35:30 +01002158 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159
2160 /*
2161 * Check whether an invalid uart number has been specified, and
2162 * if so, search for the first available port that does have
2163 * console support.
2164 */
2165 if (co->index >= UART_NR)
2166 co->index = 0;
2167 uap = amba_ports[co->index];
Russell Kingd28122a2007-01-22 18:59:42 +00002168 if (!uap)
2169 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170
Linus Walleij78d80c52012-05-23 21:18:46 +02002171 /* Allow pins to be muxed in and configured */
Linus Walleij2b996fc2013-06-05 15:36:42 +02002172 pinctrl_pm_select_default_state(uap->port.dev);
Linus Walleij78d80c52012-05-23 21:18:46 +02002173
Russell King4b4851c2011-09-22 11:35:30 +01002174 ret = clk_prepare(uap->clk);
2175 if (ret)
2176 return ret;
2177
Jingoo Han574de552013-07-30 17:06:57 +09002178 if (dev_get_platdata(uap->port.dev)) {
Shreshtha Kumar Sahuc16d51a2011-06-13 10:11:33 +02002179 struct amba_pl011_data *plat;
2180
Jingoo Han574de552013-07-30 17:06:57 +09002181 plat = dev_get_platdata(uap->port.dev);
Shreshtha Kumar Sahuc16d51a2011-06-13 10:11:33 +02002182 if (plat->init)
2183 plat->init();
2184 }
2185
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 uap->port.uartclk = clk_get_rate(uap->clk);
2187
Andre Przywaracefc2d12015-05-21 17:26:22 +01002188 if (uap->vendor->fixed_options) {
2189 baud = uap->fixed_baud;
2190 } else {
2191 if (options)
2192 uart_parse_options(options,
2193 &baud, &parity, &bits, &flow);
2194 else
2195 pl011_console_get_options(uap, &baud, &parity, &bits);
2196 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197
2198 return uart_set_options(&uap->port, co, baud, parity, bits, flow);
2199}
2200
Vincent Sanders2d934862005-09-14 22:36:03 +01002201static struct uart_driver amba_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202static struct console amba_console = {
2203 .name = "ttyAMA",
2204 .write = pl011_console_write,
2205 .device = uart_console_device,
2206 .setup = pl011_console_setup,
2207 .flags = CON_PRINTBUFFER,
2208 .index = -1,
2209 .data = &amba_reg,
2210};
2211
2212#define AMBA_CONSOLE (&amba_console)
Rob Herring0d3c6732014-04-18 17:19:57 -05002213
2214static void pl011_putc(struct uart_port *port, int c)
2215{
Greg Kroah-Hartman0de6cfb2015-09-04 09:13:56 -07002216 while (readl(port->membase + UART01x_FR) & UART01x_FR_TXFF)
Rob Herring0d3c6732014-04-18 17:19:57 -05002217 ;
Greg Kroah-Hartman0de6cfb2015-09-04 09:13:56 -07002218 writeb(c, port->membase + UART01x_DR);
2219 while (readl(port->membase + UART01x_FR) & UART01x_FR_BUSY)
Rob Herring0d3c6732014-04-18 17:19:57 -05002220 ;
2221}
2222
2223static void pl011_early_write(struct console *con, const char *s, unsigned n)
2224{
2225 struct earlycon_device *dev = con->data;
2226
2227 uart_console_write(&dev->port, s, n, pl011_putc);
2228}
2229
2230static int __init pl011_early_console_setup(struct earlycon_device *device,
2231 const char *opt)
2232{
2233 if (!device->port.membase)
2234 return -ENODEV;
2235
2236 device->con->write = pl011_early_write;
2237 return 0;
2238}
2239EARLYCON_DECLARE(pl011, pl011_early_console_setup);
Rob Herring45e0f0f2014-03-27 08:08:03 -05002240OF_EARLYCON_DECLARE(pl011, "arm,pl011", pl011_early_console_setup);
Rob Herring0d3c6732014-04-18 17:19:57 -05002241
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242#else
2243#define AMBA_CONSOLE NULL
2244#endif
2245
2246static struct uart_driver amba_reg = {
2247 .owner = THIS_MODULE,
2248 .driver_name = "ttyAMA",
2249 .dev_name = "ttyAMA",
2250 .major = SERIAL_AMBA_MAJOR,
2251 .minor = SERIAL_AMBA_MINOR,
2252 .nr = UART_NR,
2253 .cons = AMBA_CONSOLE,
2254};
2255
Matthew Leach32614aa2012-08-28 16:41:28 +01002256static int pl011_probe_dt_alias(int index, struct device *dev)
2257{
2258 struct device_node *np;
2259 static bool seen_dev_with_alias = false;
2260 static bool seen_dev_without_alias = false;
2261 int ret = index;
2262
2263 if (!IS_ENABLED(CONFIG_OF))
2264 return ret;
2265
2266 np = dev->of_node;
2267 if (!np)
2268 return ret;
2269
2270 ret = of_alias_get_id(np, "serial");
2271 if (IS_ERR_VALUE(ret)) {
2272 seen_dev_without_alias = true;
2273 ret = index;
2274 } else {
2275 seen_dev_with_alias = true;
2276 if (ret >= ARRAY_SIZE(amba_ports) || amba_ports[ret] != NULL) {
2277 dev_warn(dev, "requested serial port %d not available.\n", ret);
2278 ret = index;
2279 }
2280 }
2281
2282 if (seen_dev_with_alias && seen_dev_without_alias)
2283 dev_warn(dev, "aliased and non-aliased serial devices found in device tree. Serial port enumeration may be unpredictable.\n");
2284
2285 return ret;
2286}
2287
Andre Przywara49bb3c82015-05-21 17:26:14 +01002288/* unregisters the driver also if no more ports are left */
2289static void pl011_unregister_port(struct uart_amba_port *uap)
2290{
2291 int i;
2292 bool busy = false;
2293
2294 for (i = 0; i < ARRAY_SIZE(amba_ports); i++) {
2295 if (amba_ports[i] == uap)
2296 amba_ports[i] = NULL;
2297 else if (amba_ports[i])
2298 busy = true;
2299 }
2300 pl011_dma_remove(uap);
2301 if (!busy)
2302 uart_unregister_driver(&amba_reg);
2303}
2304
Andre Przywara3873e2d2015-05-21 17:26:18 +01002305static int pl011_find_free_port(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306{
Andre Przywara3873e2d2015-05-21 17:26:18 +01002307 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308
2309 for (i = 0; i < ARRAY_SIZE(amba_ports); i++)
2310 if (amba_ports[i] == NULL)
Andre Przywara3873e2d2015-05-21 17:26:18 +01002311 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312
Andre Przywara3873e2d2015-05-21 17:26:18 +01002313 return -EBUSY;
2314}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315
Andre Przywara3873e2d2015-05-21 17:26:18 +01002316static int pl011_setup_port(struct device *dev, struct uart_amba_port *uap,
2317 struct resource *mmiobase, int index)
2318{
2319 void __iomem *base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320
Andre Przywara3873e2d2015-05-21 17:26:18 +01002321 base = devm_ioremap_resource(dev, mmiobase);
Krzysztof Kozlowski97a60ea2015-07-09 22:21:41 +09002322 if (IS_ERR(base))
2323 return PTR_ERR(base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324
Andre Przywara3873e2d2015-05-21 17:26:18 +01002325 index = pl011_probe_dt_alias(index, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326
Shreshtha Kumar Sahud8d8ffa2012-01-18 15:53:59 +05302327 uap->old_cr = 0;
Andre Przywara3873e2d2015-05-21 17:26:18 +01002328 uap->port.dev = dev;
2329 uap->port.mapbase = mmiobase->start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 uap->port.membase = base;
2331 uap->port.iotype = UPIO_MEM;
Russell Kingffca2b12010-12-22 17:13:05 +00002332 uap->port.fifosize = uap->fifosize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 uap->port.flags = UPF_BOOT_AUTOCONF;
Andre Przywara3873e2d2015-05-21 17:26:18 +01002334 uap->port.line = index;
2335
2336 amba_ports[index] = uap;
2337
2338 return 0;
2339}
2340
2341static int pl011_register_port(struct uart_amba_port *uap)
2342{
2343 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344
Linus Walleijc3d8b762012-03-21 20:15:18 +01002345 /* Ensure interrupts from this UART are masked and cleared */
Russell Kingb2a4e242015-11-03 14:51:03 +00002346 pl011_write(0, uap, UART011_IMSC);
2347 pl011_write(0xffff, uap, UART011_ICR);
Linus Walleijc3d8b762012-03-21 20:15:18 +01002348
Tushar Beheraef2889f2014-01-20 14:32:35 +05302349 if (!amba_reg.state) {
2350 ret = uart_register_driver(&amba_reg);
2351 if (ret < 0) {
Andre Przywara3873e2d2015-05-21 17:26:18 +01002352 dev_err(uap->port.dev,
Jorge Ramirez-Ortiz1c9be312015-03-06 13:05:40 -05002353 "Failed to register AMBA-PL011 driver\n");
Tushar Beheraef2889f2014-01-20 14:32:35 +05302354 return ret;
2355 }
2356 }
2357
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 ret = uart_add_one_port(&amba_reg, &uap->port);
Andre Przywara49bb3c82015-05-21 17:26:14 +01002359 if (ret)
2360 pl011_unregister_port(uap);
Tushar Behera7f6d9422014-06-26 15:35:35 +05302361
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 return ret;
2363}
2364
Andre Przywara3873e2d2015-05-21 17:26:18 +01002365static int pl011_probe(struct amba_device *dev, const struct amba_id *id)
2366{
2367 struct uart_amba_port *uap;
2368 struct vendor_data *vendor = id->data;
2369 int portnr, ret;
2370
2371 portnr = pl011_find_free_port();
2372 if (portnr < 0)
2373 return portnr;
2374
2375 uap = devm_kzalloc(&dev->dev, sizeof(struct uart_amba_port),
2376 GFP_KERNEL);
2377 if (!uap)
2378 return -ENOMEM;
2379
2380 uap->clk = devm_clk_get(&dev->dev, NULL);
2381 if (IS_ERR(uap->clk))
2382 return PTR_ERR(uap->clk);
2383
2384 uap->vendor = vendor;
2385 uap->lcrh_rx = vendor->lcrh_rx;
2386 uap->lcrh_tx = vendor->lcrh_tx;
2387 uap->fifosize = vendor->get_fifosize(dev);
2388 uap->port.irq = dev->irq[0];
2389 uap->port.ops = &amba_pl011_pops;
2390
2391 snprintf(uap->type, sizeof(uap->type), "PL011 rev%u", amba_rev(dev));
2392
2393 ret = pl011_setup_port(&dev->dev, uap, &dev->res, portnr);
2394 if (ret)
2395 return ret;
2396
2397 amba_set_drvdata(dev, uap);
2398
2399 return pl011_register_port(uap);
2400}
2401
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402static int pl011_remove(struct amba_device *dev)
2403{
2404 struct uart_amba_port *uap = amba_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 uart_remove_one_port(&amba_reg, &uap->port);
Andre Przywara49bb3c82015-05-21 17:26:14 +01002407 pl011_unregister_port(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 return 0;
2409}
2410
Ulf Hanssond0ce8502013-12-03 11:04:28 +01002411#ifdef CONFIG_PM_SLEEP
2412static int pl011_suspend(struct device *dev)
Leo Chenb736b892009-07-28 23:43:33 +01002413{
Ulf Hanssond0ce8502013-12-03 11:04:28 +01002414 struct uart_amba_port *uap = dev_get_drvdata(dev);
Leo Chenb736b892009-07-28 23:43:33 +01002415
2416 if (!uap)
2417 return -EINVAL;
2418
2419 return uart_suspend_port(&amba_reg, &uap->port);
2420}
2421
Ulf Hanssond0ce8502013-12-03 11:04:28 +01002422static int pl011_resume(struct device *dev)
Leo Chenb736b892009-07-28 23:43:33 +01002423{
Ulf Hanssond0ce8502013-12-03 11:04:28 +01002424 struct uart_amba_port *uap = dev_get_drvdata(dev);
Leo Chenb736b892009-07-28 23:43:33 +01002425
2426 if (!uap)
2427 return -EINVAL;
2428
2429 return uart_resume_port(&amba_reg, &uap->port);
2430}
2431#endif
2432
Ulf Hanssond0ce8502013-12-03 11:04:28 +01002433static SIMPLE_DEV_PM_OPS(pl011_dev_pm_ops, pl011_suspend, pl011_resume);
2434
Andre Przywara0dd1e242015-05-21 17:26:23 +01002435static int sbsa_uart_probe(struct platform_device *pdev)
2436{
2437 struct uart_amba_port *uap;
2438 struct resource *r;
2439 int portnr, ret;
2440 int baudrate;
2441
2442 /*
2443 * Check the mandatory baud rate parameter in the DT node early
2444 * so that we can easily exit with the error.
2445 */
2446 if (pdev->dev.of_node) {
2447 struct device_node *np = pdev->dev.of_node;
2448
2449 ret = of_property_read_u32(np, "current-speed", &baudrate);
2450 if (ret)
2451 return ret;
2452 } else {
2453 baudrate = 115200;
2454 }
2455
2456 portnr = pl011_find_free_port();
2457 if (portnr < 0)
2458 return portnr;
2459
2460 uap = devm_kzalloc(&pdev->dev, sizeof(struct uart_amba_port),
2461 GFP_KERNEL);
2462 if (!uap)
2463 return -ENOMEM;
2464
2465 uap->vendor = &vendor_sbsa;
2466 uap->fifosize = 32;
2467 uap->port.irq = platform_get_irq(pdev, 0);
2468 uap->port.ops = &sbsa_uart_pops;
2469 uap->fixed_baud = baudrate;
2470
2471 snprintf(uap->type, sizeof(uap->type), "SBSA");
2472
2473 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2474
2475 ret = pl011_setup_port(&pdev->dev, uap, r, portnr);
2476 if (ret)
2477 return ret;
2478
2479 platform_set_drvdata(pdev, uap);
2480
2481 return pl011_register_port(uap);
2482}
2483
2484static int sbsa_uart_remove(struct platform_device *pdev)
2485{
2486 struct uart_amba_port *uap = platform_get_drvdata(pdev);
2487
2488 uart_remove_one_port(&amba_reg, &uap->port);
2489 pl011_unregister_port(uap);
2490 return 0;
2491}
2492
2493static const struct of_device_id sbsa_uart_of_match[] = {
2494 { .compatible = "arm,sbsa-uart", },
2495 {},
2496};
2497MODULE_DEVICE_TABLE(of, sbsa_uart_of_match);
2498
Graeme Gregory3db9ab02015-05-21 17:26:24 +01002499static const struct acpi_device_id sbsa_uart_acpi_match[] = {
2500 { "ARMH0011", 0 },
2501 {},
2502};
2503MODULE_DEVICE_TABLE(acpi, sbsa_uart_acpi_match);
2504
Andre Przywara0dd1e242015-05-21 17:26:23 +01002505static struct platform_driver arm_sbsa_uart_platform_driver = {
2506 .probe = sbsa_uart_probe,
2507 .remove = sbsa_uart_remove,
2508 .driver = {
2509 .name = "sbsa-uart",
2510 .of_match_table = of_match_ptr(sbsa_uart_of_match),
Graeme Gregory3db9ab02015-05-21 17:26:24 +01002511 .acpi_match_table = ACPI_PTR(sbsa_uart_acpi_match),
Andre Przywara0dd1e242015-05-21 17:26:23 +01002512 },
2513};
2514
Russell King2c39c9e2010-07-27 08:50:16 +01002515static struct amba_id pl011_ids[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 {
2517 .id = 0x00041011,
2518 .mask = 0x000fffff,
Alessandro Rubini5926a292009-06-04 17:43:04 +01002519 .data = &vendor_arm,
2520 },
2521 {
2522 .id = 0x00380802,
2523 .mask = 0x00ffffff,
2524 .data = &vendor_st,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525 },
2526 { 0, 0 },
2527};
2528
Dave Martin60f7a332011-10-05 15:15:22 +01002529MODULE_DEVICE_TABLE(amba, pl011_ids);
2530
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531static struct amba_driver pl011_driver = {
2532 .drv = {
2533 .name = "uart-pl011",
Ulf Hanssond0ce8502013-12-03 11:04:28 +01002534 .pm = &pl011_dev_pm_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535 },
2536 .id_table = pl011_ids,
2537 .probe = pl011_probe,
2538 .remove = pl011_remove,
2539};
2540
2541static int __init pl011_init(void)
2542{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 printk(KERN_INFO "Serial: AMBA PL011 UART driver\n");
2544
Andre Przywara0dd1e242015-05-21 17:26:23 +01002545 if (platform_driver_register(&arm_sbsa_uart_platform_driver))
2546 pr_warn("could not register SBSA UART platform driver\n");
Greg Kroah-Hartman062a68a2015-09-04 09:11:24 -07002547 return amba_driver_register(&pl011_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548}
2549
2550static void __exit pl011_exit(void)
2551{
Andre Przywara0dd1e242015-05-21 17:26:23 +01002552 platform_driver_unregister(&arm_sbsa_uart_platform_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553 amba_driver_unregister(&pl011_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554}
2555
Alessandro Rubini4dd9e742009-05-05 05:54:13 +01002556/*
2557 * While this can be a module, if builtin it's most likely the console
2558 * So let's leave module_exit but move module_init to an earlier place
2559 */
2560arch_initcall(pl011_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561module_exit(pl011_exit);
2562
2563MODULE_AUTHOR("ARM Ltd/Deep Blue Solutions Ltd");
2564MODULE_DESCRIPTION("ARM AMBA serial port driver");
2565MODULE_LICENSE("GPL");