blob: ee57e2bee9a1b709a223e57dc8f26d15f77eb2a7 [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
Jun Nie534e14e2015-07-31 15:49:15 +080088enum reg_idx {
89 REG_DR = UART01x_DR,
90 REG_RSR = UART01x_RSR,
91 REG_ST_DMAWM = ST_UART011_DMAWM,
92 REG_FR = UART01x_FR,
93 REG_ST_LCRH_RX = ST_UART011_LCRH_RX,
94 REG_ILPR = UART01x_ILPR,
95 REG_IBRD = UART011_IBRD,
96 REG_FBRD = UART011_FBRD,
97 REG_LCRH = UART011_LCRH,
98 REG_CR = UART011_CR,
99 REG_IFLS = UART011_IFLS,
100 REG_IMSC = UART011_IMSC,
101 REG_RIS = UART011_RIS,
102 REG_MIS = UART011_MIS,
103 REG_ICR = UART011_ICR,
104 REG_DMACR = UART011_DMACR,
105};
106
Jongsung Kimea336402013-05-10 18:05:35 +0900107static unsigned int get_fifosize_arm(struct amba_device *dev)
Jongsung Kim78506f22013-04-15 14:45:25 +0900108{
Jongsung Kimea336402013-05-10 18:05:35 +0900109 return amba_rev(dev) < 3 ? 16 : 32;
Jongsung Kim78506f22013-04-15 14:45:25 +0900110}
111
Alessandro Rubini5926a292009-06-04 17:43:04 +0100112static struct vendor_data vendor_arm = {
113 .ifls = UART011_IFLS_RX4_8|UART011_IFLS_TX4_8,
Jun Nie534e14e2015-07-31 15:49:15 +0800114 .lcrh_tx = REG_LCRH,
115 .lcrh_rx = REG_LCRH,
Linus Walleijac3e3fb2010-06-02 20:40:22 +0100116 .oversampling = false,
Russell King38d62432010-12-22 17:59:16 +0000117 .dma_threshold = false,
Rajanikanth H.V4fd06902012-03-26 11:17:02 +0200118 .cts_event_workaround = false,
Andre Przywara71eec482015-05-21 17:26:21 +0100119 .always_enabled = false,
Andre Przywaracefc2d12015-05-21 17:26:22 +0100120 .fixed_options = false,
Jongsung Kim78506f22013-04-15 14:45:25 +0900121 .get_fifosize = get_fifosize_arm,
Alessandro Rubini5926a292009-06-04 17:43:04 +0100122};
123
Andre Przywara0dd1e242015-05-21 17:26:23 +0100124static struct vendor_data vendor_sbsa = {
125 .oversampling = false,
126 .dma_threshold = false,
127 .cts_event_workaround = false,
128 .always_enabled = true,
129 .fixed_options = true,
130};
131
Jongsung Kimea336402013-05-10 18:05:35 +0900132static unsigned int get_fifosize_st(struct amba_device *dev)
Jongsung Kim78506f22013-04-15 14:45:25 +0900133{
134 return 64;
135}
136
Alessandro Rubini5926a292009-06-04 17:43:04 +0100137static struct vendor_data vendor_st = {
138 .ifls = UART011_IFLS_RX_HALF|UART011_IFLS_TX_HALF,
Jun Nie534e14e2015-07-31 15:49:15 +0800139 .lcrh_tx = REG_LCRH,
140 .lcrh_rx = REG_ST_LCRH_RX,
Linus Walleijac3e3fb2010-06-02 20:40:22 +0100141 .oversampling = true,
Russell King38d62432010-12-22 17:59:16 +0000142 .dma_threshold = true,
Rajanikanth H.V4fd06902012-03-26 11:17:02 +0200143 .cts_event_workaround = true,
Andre Przywara71eec482015-05-21 17:26:21 +0100144 .always_enabled = false,
Andre Przywaracefc2d12015-05-21 17:26:22 +0100145 .fixed_options = false,
Jongsung Kim78506f22013-04-15 14:45:25 +0900146 .get_fifosize = get_fifosize_st,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147};
148
Russell King68b65f72010-12-22 17:24:39 +0000149/* Deals with DMA transactions */
Linus Walleijead76f322011-02-24 13:21:08 +0100150
151struct pl011_sgbuf {
152 struct scatterlist sg;
153 char *buf;
154};
155
156struct pl011_dmarx_data {
157 struct dma_chan *chan;
158 struct completion complete;
159 bool use_buf_b;
160 struct pl011_sgbuf sgbuf_a;
161 struct pl011_sgbuf sgbuf_b;
162 dma_cookie_t cookie;
163 bool running;
Chanho Mincb06ff12013-03-27 18:38:11 +0900164 struct timer_list timer;
165 unsigned int last_residue;
166 unsigned long last_jiffies;
167 bool auto_poll_rate;
168 unsigned int poll_rate;
169 unsigned int poll_timeout;
Linus Walleijead76f322011-02-24 13:21:08 +0100170};
171
Russell King68b65f72010-12-22 17:24:39 +0000172struct pl011_dmatx_data {
173 struct dma_chan *chan;
174 struct scatterlist sg;
175 char *buf;
176 bool queued;
177};
178
Russell Kingc19f12b2010-12-22 17:48:26 +0000179/*
180 * We wrap our port structure around the generic uart_port.
181 */
182struct uart_amba_port {
183 struct uart_port port;
184 struct clk *clk;
185 const struct vendor_data *vendor;
Russell King68b65f72010-12-22 17:24:39 +0000186 unsigned int dmacr; /* dma control reg */
Russell Kingc19f12b2010-12-22 17:48:26 +0000187 unsigned int im; /* interrupt mask */
188 unsigned int old_status;
Russell Kingffca2b12010-12-22 17:13:05 +0000189 unsigned int fifosize; /* vendor-specific */
Russell Kingc19f12b2010-12-22 17:48:26 +0000190 unsigned int lcrh_tx; /* vendor-specific */
191 unsigned int lcrh_rx; /* vendor-specific */
Shreshtha Kumar Sahud8d8ffa2012-01-18 15:53:59 +0530192 unsigned int old_cr; /* state during shutdown */
Russell Kingc19f12b2010-12-22 17:48:26 +0000193 bool autorts;
Andre Przywaracefc2d12015-05-21 17:26:22 +0100194 unsigned int fixed_baud; /* vendor-set fixed baud rate */
Russell Kingc19f12b2010-12-22 17:48:26 +0000195 char type[12];
Russell King68b65f72010-12-22 17:24:39 +0000196#ifdef CONFIG_DMA_ENGINE
197 /* DMA stuff */
Linus Walleijead76f322011-02-24 13:21:08 +0100198 bool using_tx_dma;
199 bool using_rx_dma;
200 struct pl011_dmarx_data dmarx;
Russell King68b65f72010-12-22 17:24:39 +0000201 struct pl011_dmatx_data dmatx;
Jorge Ramirez-Ortiz1c9be312015-03-06 13:05:40 -0500202 bool dma_probed;
Russell King68b65f72010-12-22 17:24:39 +0000203#endif
Russell Kingc19f12b2010-12-22 17:48:26 +0000204};
205
Russell King68b65f72010-12-22 17:24:39 +0000206/*
Linus Walleij29772c42011-02-24 13:21:36 +0100207 * Reads up to 256 characters from the FIFO or until it's empty and
208 * inserts them into the TTY layer. Returns the number of characters
209 * read from the FIFO.
210 */
211static int pl011_fifo_to_tty(struct uart_amba_port *uap)
212{
213 u16 status, ch;
214 unsigned int flag, max_count = 256;
215 int fifotaken = 0;
216
217 while (max_count--) {
Jun Nie534e14e2015-07-31 15:49:15 +0800218 status = readw(uap->port.membase + REG_FR);
Linus Walleij29772c42011-02-24 13:21:36 +0100219 if (status & UART01x_FR_RXFE)
220 break;
221
222 /* Take chars from the FIFO and update status */
Jun Nie534e14e2015-07-31 15:49:15 +0800223 ch = readw(uap->port.membase + REG_DR) |
Linus Walleij29772c42011-02-24 13:21:36 +0100224 UART_DUMMY_DR_RX;
225 flag = TTY_NORMAL;
226 uap->port.icount.rx++;
227 fifotaken++;
228
229 if (unlikely(ch & UART_DR_ERROR)) {
230 if (ch & UART011_DR_BE) {
231 ch &= ~(UART011_DR_FE | UART011_DR_PE);
232 uap->port.icount.brk++;
233 if (uart_handle_break(&uap->port))
234 continue;
235 } else if (ch & UART011_DR_PE)
236 uap->port.icount.parity++;
237 else if (ch & UART011_DR_FE)
238 uap->port.icount.frame++;
239 if (ch & UART011_DR_OE)
240 uap->port.icount.overrun++;
241
242 ch &= uap->port.read_status_mask;
243
244 if (ch & UART011_DR_BE)
245 flag = TTY_BREAK;
246 else if (ch & UART011_DR_PE)
247 flag = TTY_PARITY;
248 else if (ch & UART011_DR_FE)
249 flag = TTY_FRAME;
250 }
251
252 if (uart_handle_sysrq_char(&uap->port, ch & 255))
253 continue;
254
255 uart_insert_char(&uap->port, ch, UART011_DR_OE, ch, flag);
256 }
257
258 return fifotaken;
259}
260
261
262/*
Russell King68b65f72010-12-22 17:24:39 +0000263 * All the DMA operation mode stuff goes inside this ifdef.
264 * This assumes that you have a generic DMA device interface,
265 * no custom DMA interfaces are supported.
266 */
267#ifdef CONFIG_DMA_ENGINE
268
269#define PL011_DMA_BUFFER_SIZE PAGE_SIZE
270
Linus Walleijead76f322011-02-24 13:21:08 +0100271static int pl011_sgbuf_init(struct dma_chan *chan, struct pl011_sgbuf *sg,
272 enum dma_data_direction dir)
273{
Chanho Mincb06ff12013-03-27 18:38:11 +0900274 dma_addr_t dma_addr;
275
276 sg->buf = dma_alloc_coherent(chan->device->dev,
277 PL011_DMA_BUFFER_SIZE, &dma_addr, GFP_KERNEL);
Linus Walleijead76f322011-02-24 13:21:08 +0100278 if (!sg->buf)
279 return -ENOMEM;
280
Chanho Mincb06ff12013-03-27 18:38:11 +0900281 sg_init_table(&sg->sg, 1);
282 sg_set_page(&sg->sg, phys_to_page(dma_addr),
283 PL011_DMA_BUFFER_SIZE, offset_in_page(dma_addr));
284 sg_dma_address(&sg->sg) = dma_addr;
Andrew Jacksonc64be922014-11-07 14:14:43 +0000285 sg_dma_len(&sg->sg) = PL011_DMA_BUFFER_SIZE;
Linus Walleijead76f322011-02-24 13:21:08 +0100286
Linus Walleijead76f322011-02-24 13:21:08 +0100287 return 0;
288}
289
290static void pl011_sgbuf_free(struct dma_chan *chan, struct pl011_sgbuf *sg,
291 enum dma_data_direction dir)
292{
293 if (sg->buf) {
Chanho Mincb06ff12013-03-27 18:38:11 +0900294 dma_free_coherent(chan->device->dev,
295 PL011_DMA_BUFFER_SIZE, sg->buf,
296 sg_dma_address(&sg->sg));
Linus Walleijead76f322011-02-24 13:21:08 +0100297 }
298}
299
Jorge Ramirez-Ortiz1c9be312015-03-06 13:05:40 -0500300static void pl011_dma_probe(struct uart_amba_port *uap)
Russell King68b65f72010-12-22 17:24:39 +0000301{
302 /* DMA is the sole user of the platform data right now */
Jingoo Han574de552013-07-30 17:06:57 +0900303 struct amba_pl011_data *plat = dev_get_platdata(uap->port.dev);
Jorge Ramirez-Ortiz1c9be312015-03-06 13:05:40 -0500304 struct device *dev = uap->port.dev;
Russell King68b65f72010-12-22 17:24:39 +0000305 struct dma_slave_config tx_conf = {
Jun Nie534e14e2015-07-31 15:49:15 +0800306 .dst_addr = uap->port.mapbase + REG_DR,
Russell King68b65f72010-12-22 17:24:39 +0000307 .dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE,
Vinod Koula485df42011-10-14 10:47:38 +0530308 .direction = DMA_MEM_TO_DEV,
Russell King68b65f72010-12-22 17:24:39 +0000309 .dst_maxburst = uap->fifosize >> 1,
Viresh Kumar258aea72012-02-01 16:12:19 +0530310 .device_fc = false,
Russell King68b65f72010-12-22 17:24:39 +0000311 };
312 struct dma_chan *chan;
313 dma_cap_mask_t mask;
314
Jorge Ramirez-Ortiz1c9be312015-03-06 13:05:40 -0500315 uap->dma_probed = true;
316 chan = dma_request_slave_channel_reason(dev, "tx");
317 if (IS_ERR(chan)) {
318 if (PTR_ERR(chan) == -EPROBE_DEFER) {
Jorge Ramirez-Ortiz1c9be312015-03-06 13:05:40 -0500319 uap->dma_probed = false;
320 return;
321 }
Russell King68b65f72010-12-22 17:24:39 +0000322
Arnd Bergmann787b0c12013-01-28 16:24:37 +0000323 /* We need platform data */
324 if (!plat || !plat->dma_filter) {
325 dev_info(uap->port.dev, "no DMA platform data\n");
326 return;
327 }
328
329 /* Try to acquire a generic DMA engine slave TX channel */
330 dma_cap_zero(mask);
331 dma_cap_set(DMA_SLAVE, mask);
332
333 chan = dma_request_channel(mask, plat->dma_filter,
334 plat->dma_tx_param);
335 if (!chan) {
336 dev_err(uap->port.dev, "no TX DMA channel!\n");
337 return;
338 }
Russell King68b65f72010-12-22 17:24:39 +0000339 }
340
341 dmaengine_slave_config(chan, &tx_conf);
342 uap->dmatx.chan = chan;
343
344 dev_info(uap->port.dev, "DMA channel TX %s\n",
345 dma_chan_name(uap->dmatx.chan));
Linus Walleijead76f322011-02-24 13:21:08 +0100346
347 /* Optionally make use of an RX channel as well */
Arnd Bergmann787b0c12013-01-28 16:24:37 +0000348 chan = dma_request_slave_channel(dev, "rx");
Rob Herring0d3c6732014-04-18 17:19:57 -0500349
Arnd Bergmann787b0c12013-01-28 16:24:37 +0000350 if (!chan && plat->dma_rx_param) {
351 chan = dma_request_channel(mask, plat->dma_filter, plat->dma_rx_param);
352
353 if (!chan) {
354 dev_err(uap->port.dev, "no RX DMA channel!\n");
355 return;
356 }
357 }
358
359 if (chan) {
Linus Walleijead76f322011-02-24 13:21:08 +0100360 struct dma_slave_config rx_conf = {
Jun Nie534e14e2015-07-31 15:49:15 +0800361 .src_addr = uap->port.mapbase + REG_DR,
Linus Walleijead76f322011-02-24 13:21:08 +0100362 .src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE,
Vinod Koula485df42011-10-14 10:47:38 +0530363 .direction = DMA_DEV_TO_MEM,
Guennadi Liakhovetskib2aeb772014-04-12 19:47:17 +0200364 .src_maxburst = uap->fifosize >> 2,
Viresh Kumar258aea72012-02-01 16:12:19 +0530365 .device_fc = false,
Linus Walleijead76f322011-02-24 13:21:08 +0100366 };
Andrew Jackson2d3b7d62014-11-07 14:14:47 +0000367 struct dma_slave_caps caps;
Linus Walleijead76f322011-02-24 13:21:08 +0100368
Andrew Jackson2d3b7d62014-11-07 14:14:47 +0000369 /*
370 * Some DMA controllers provide information on their capabilities.
371 * If the controller does, check for suitable residue processing
372 * otherwise assime all is well.
373 */
374 if (0 == dma_get_slave_caps(chan, &caps)) {
375 if (caps.residue_granularity ==
376 DMA_RESIDUE_GRANULARITY_DESCRIPTOR) {
377 dma_release_channel(chan);
378 dev_info(uap->port.dev,
379 "RX DMA disabled - no residue processing\n");
380 return;
381 }
382 }
Linus Walleijead76f322011-02-24 13:21:08 +0100383 dmaengine_slave_config(chan, &rx_conf);
384 uap->dmarx.chan = chan;
385
Andrew Jackson98267d32014-11-07 14:14:23 +0000386 uap->dmarx.auto_poll_rate = false;
Greg Kroah-Hartman8f898bf2013-12-17 09:33:18 -0800387 if (plat && plat->dma_rx_poll_enable) {
Chanho Mincb06ff12013-03-27 18:38:11 +0900388 /* Set poll rate if specified. */
389 if (plat->dma_rx_poll_rate) {
390 uap->dmarx.auto_poll_rate = false;
391 uap->dmarx.poll_rate = plat->dma_rx_poll_rate;
392 } else {
393 /*
394 * 100 ms defaults to poll rate if not
395 * specified. This will be adjusted with
396 * the baud rate at set_termios.
397 */
398 uap->dmarx.auto_poll_rate = true;
399 uap->dmarx.poll_rate = 100;
400 }
401 /* 3 secs defaults poll_timeout if not specified. */
402 if (plat->dma_rx_poll_timeout)
403 uap->dmarx.poll_timeout =
404 plat->dma_rx_poll_timeout;
405 else
406 uap->dmarx.poll_timeout = 3000;
Andrew Jackson98267d32014-11-07 14:14:23 +0000407 } else if (!plat && dev->of_node) {
408 uap->dmarx.auto_poll_rate = of_property_read_bool(
409 dev->of_node, "auto-poll");
410 if (uap->dmarx.auto_poll_rate) {
411 u32 x;
Chanho Mincb06ff12013-03-27 18:38:11 +0900412
Andrew Jackson98267d32014-11-07 14:14:23 +0000413 if (0 == of_property_read_u32(dev->of_node,
414 "poll-rate-ms", &x))
415 uap->dmarx.poll_rate = x;
416 else
417 uap->dmarx.poll_rate = 100;
418 if (0 == of_property_read_u32(dev->of_node,
419 "poll-timeout-ms", &x))
420 uap->dmarx.poll_timeout = x;
421 else
422 uap->dmarx.poll_timeout = 3000;
423 }
424 }
Linus Walleijead76f322011-02-24 13:21:08 +0100425 dev_info(uap->port.dev, "DMA channel RX %s\n",
426 dma_chan_name(uap->dmarx.chan));
427 }
Russell King68b65f72010-12-22 17:24:39 +0000428}
429
Russell King68b65f72010-12-22 17:24:39 +0000430static void pl011_dma_remove(struct uart_amba_port *uap)
431{
Russell King68b65f72010-12-22 17:24:39 +0000432 if (uap->dmatx.chan)
433 dma_release_channel(uap->dmatx.chan);
Linus Walleijead76f322011-02-24 13:21:08 +0100434 if (uap->dmarx.chan)
435 dma_release_channel(uap->dmarx.chan);
Russell King68b65f72010-12-22 17:24:39 +0000436}
437
Dave Martin734745c2015-03-04 12:27:33 +0000438/* Forward declare these for the refill routine */
Russell King68b65f72010-12-22 17:24:39 +0000439static int pl011_dma_tx_refill(struct uart_amba_port *uap);
Dave Martin734745c2015-03-04 12:27:33 +0000440static void pl011_start_tx_pio(struct uart_amba_port *uap);
Russell King68b65f72010-12-22 17:24:39 +0000441
442/*
443 * The current DMA TX buffer has been sent.
444 * Try to queue up another DMA buffer.
445 */
446static void pl011_dma_tx_callback(void *data)
447{
448 struct uart_amba_port *uap = data;
449 struct pl011_dmatx_data *dmatx = &uap->dmatx;
450 unsigned long flags;
451 u16 dmacr;
452
453 spin_lock_irqsave(&uap->port.lock, flags);
454 if (uap->dmatx.queued)
455 dma_unmap_sg(dmatx->chan->device->dev, &dmatx->sg, 1,
456 DMA_TO_DEVICE);
457
458 dmacr = uap->dmacr;
459 uap->dmacr = dmacr & ~UART011_TXDMAE;
Jun Nie534e14e2015-07-31 15:49:15 +0800460 writew(uap->dmacr, uap->port.membase + REG_DMACR);
Russell King68b65f72010-12-22 17:24:39 +0000461
462 /*
463 * If TX DMA was disabled, it means that we've stopped the DMA for
464 * some reason (eg, XOFF received, or we want to send an X-char.)
465 *
466 * Note: we need to be careful here of a potential race between DMA
467 * and the rest of the driver - if the driver disables TX DMA while
468 * a TX buffer completing, we must update the tx queued status to
469 * get further refills (hence we check dmacr).
470 */
471 if (!(dmacr & UART011_TXDMAE) || uart_tx_stopped(&uap->port) ||
472 uart_circ_empty(&uap->port.state->xmit)) {
473 uap->dmatx.queued = false;
474 spin_unlock_irqrestore(&uap->port.lock, flags);
475 return;
476 }
477
Dave Martin734745c2015-03-04 12:27:33 +0000478 if (pl011_dma_tx_refill(uap) <= 0)
Russell King68b65f72010-12-22 17:24:39 +0000479 /*
480 * We didn't queue a DMA buffer for some reason, but we
481 * have data pending to be sent. Re-enable the TX IRQ.
482 */
Dave Martin734745c2015-03-04 12:27:33 +0000483 pl011_start_tx_pio(uap);
484
Russell King68b65f72010-12-22 17:24:39 +0000485 spin_unlock_irqrestore(&uap->port.lock, flags);
486}
487
488/*
489 * Try to refill the TX DMA buffer.
490 * Locking: called with port lock held and IRQs disabled.
491 * Returns:
492 * 1 if we queued up a TX DMA buffer.
493 * 0 if we didn't want to handle this by DMA
494 * <0 on error
495 */
496static int pl011_dma_tx_refill(struct uart_amba_port *uap)
497{
498 struct pl011_dmatx_data *dmatx = &uap->dmatx;
499 struct dma_chan *chan = dmatx->chan;
500 struct dma_device *dma_dev = chan->device;
501 struct dma_async_tx_descriptor *desc;
502 struct circ_buf *xmit = &uap->port.state->xmit;
503 unsigned int count;
504
505 /*
506 * Try to avoid the overhead involved in using DMA if the
507 * transaction fits in the first half of the FIFO, by using
508 * the standard interrupt handling. This ensures that we
509 * issue a uart_write_wakeup() at the appropriate time.
510 */
511 count = uart_circ_chars_pending(xmit);
512 if (count < (uap->fifosize >> 1)) {
513 uap->dmatx.queued = false;
514 return 0;
515 }
516
517 /*
518 * Bodge: don't send the last character by DMA, as this
519 * will prevent XON from notifying us to restart DMA.
520 */
521 count -= 1;
522
523 /* Else proceed to copy the TX chars to the DMA buffer and fire DMA */
524 if (count > PL011_DMA_BUFFER_SIZE)
525 count = PL011_DMA_BUFFER_SIZE;
526
527 if (xmit->tail < xmit->head)
528 memcpy(&dmatx->buf[0], &xmit->buf[xmit->tail], count);
529 else {
530 size_t first = UART_XMIT_SIZE - xmit->tail;
Andrew Jacksone2a545a2014-11-07 14:14:39 +0000531 size_t second;
532
533 if (first > count)
534 first = count;
535 second = count - first;
Russell King68b65f72010-12-22 17:24:39 +0000536
537 memcpy(&dmatx->buf[0], &xmit->buf[xmit->tail], first);
538 if (second)
539 memcpy(&dmatx->buf[first], &xmit->buf[0], second);
540 }
541
542 dmatx->sg.length = count;
543
544 if (dma_map_sg(dma_dev->dev, &dmatx->sg, 1, DMA_TO_DEVICE) != 1) {
545 uap->dmatx.queued = false;
546 dev_dbg(uap->port.dev, "unable to map TX DMA\n");
547 return -EBUSY;
548 }
549
Alexandre Bounine16052822012-03-08 16:11:18 -0500550 desc = dmaengine_prep_slave_sg(chan, &dmatx->sg, 1, DMA_MEM_TO_DEV,
Russell King68b65f72010-12-22 17:24:39 +0000551 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
552 if (!desc) {
553 dma_unmap_sg(dma_dev->dev, &dmatx->sg, 1, DMA_TO_DEVICE);
554 uap->dmatx.queued = false;
555 /*
556 * If DMA cannot be used right now, we complete this
557 * transaction via IRQ and let the TTY layer retry.
558 */
559 dev_dbg(uap->port.dev, "TX DMA busy\n");
560 return -EBUSY;
561 }
562
563 /* Some data to go along to the callback */
564 desc->callback = pl011_dma_tx_callback;
565 desc->callback_param = uap;
566
567 /* All errors should happen at prepare time */
568 dmaengine_submit(desc);
569
570 /* Fire the DMA transaction */
571 dma_dev->device_issue_pending(chan);
572
573 uap->dmacr |= UART011_TXDMAE;
Jun Nie534e14e2015-07-31 15:49:15 +0800574 writew(uap->dmacr, uap->port.membase + REG_DMACR);
Russell King68b65f72010-12-22 17:24:39 +0000575 uap->dmatx.queued = true;
576
577 /*
578 * Now we know that DMA will fire, so advance the ring buffer
579 * with the stuff we just dispatched.
580 */
581 xmit->tail = (xmit->tail + count) & (UART_XMIT_SIZE - 1);
582 uap->port.icount.tx += count;
583
584 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
585 uart_write_wakeup(&uap->port);
586
587 return 1;
588}
589
590/*
591 * We received a transmit interrupt without a pending X-char but with
592 * pending characters.
593 * Locking: called with port lock held and IRQs disabled.
594 * Returns:
595 * false if we want to use PIO to transmit
596 * true if we queued a DMA buffer
597 */
598static bool pl011_dma_tx_irq(struct uart_amba_port *uap)
599{
Linus Walleijead76f322011-02-24 13:21:08 +0100600 if (!uap->using_tx_dma)
Russell King68b65f72010-12-22 17:24:39 +0000601 return false;
602
603 /*
604 * If we already have a TX buffer queued, but received a
605 * TX interrupt, it will be because we've just sent an X-char.
606 * Ensure the TX DMA is enabled and the TX IRQ is disabled.
607 */
608 if (uap->dmatx.queued) {
609 uap->dmacr |= UART011_TXDMAE;
Jun Nie534e14e2015-07-31 15:49:15 +0800610 writew(uap->dmacr, uap->port.membase + REG_DMACR);
Russell King68b65f72010-12-22 17:24:39 +0000611 uap->im &= ~UART011_TXIM;
Jun Nie534e14e2015-07-31 15:49:15 +0800612 writew(uap->im, uap->port.membase + REG_IMSC);
Russell King68b65f72010-12-22 17:24:39 +0000613 return true;
614 }
615
616 /*
617 * We don't have a TX buffer queued, so try to queue one.
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300618 * If we successfully queued a buffer, mask the TX IRQ.
Russell King68b65f72010-12-22 17:24:39 +0000619 */
620 if (pl011_dma_tx_refill(uap) > 0) {
621 uap->im &= ~UART011_TXIM;
Jun Nie534e14e2015-07-31 15:49:15 +0800622 writew(uap->im, uap->port.membase + REG_IMSC);
Russell King68b65f72010-12-22 17:24:39 +0000623 return true;
624 }
625 return false;
626}
627
628/*
629 * Stop the DMA transmit (eg, due to received XOFF).
630 * Locking: called with port lock held and IRQs disabled.
631 */
632static inline void pl011_dma_tx_stop(struct uart_amba_port *uap)
633{
634 if (uap->dmatx.queued) {
635 uap->dmacr &= ~UART011_TXDMAE;
Jun Nie534e14e2015-07-31 15:49:15 +0800636 writew(uap->dmacr, uap->port.membase + REG_DMACR);
Russell King68b65f72010-12-22 17:24:39 +0000637 }
638}
639
640/*
641 * Try to start a DMA transmit, or in the case of an XON/OFF
642 * character queued for send, try to get that character out ASAP.
643 * Locking: called with port lock held and IRQs disabled.
644 * Returns:
645 * false if we want the TX IRQ to be enabled
646 * true if we have a buffer queued
647 */
648static inline bool pl011_dma_tx_start(struct uart_amba_port *uap)
649{
650 u16 dmacr;
651
Linus Walleijead76f322011-02-24 13:21:08 +0100652 if (!uap->using_tx_dma)
Russell King68b65f72010-12-22 17:24:39 +0000653 return false;
654
655 if (!uap->port.x_char) {
656 /* no X-char, try to push chars out in DMA mode */
657 bool ret = true;
658
659 if (!uap->dmatx.queued) {
660 if (pl011_dma_tx_refill(uap) > 0) {
661 uap->im &= ~UART011_TXIM;
Dave Martin734745c2015-03-04 12:27:33 +0000662 writew(uap->im, uap->port.membase +
Jun Nie534e14e2015-07-31 15:49:15 +0800663 REG_IMSC);
Dave Martin734745c2015-03-04 12:27:33 +0000664 } else
Russell King68b65f72010-12-22 17:24:39 +0000665 ret = false;
Russell King68b65f72010-12-22 17:24:39 +0000666 } else if (!(uap->dmacr & UART011_TXDMAE)) {
667 uap->dmacr |= UART011_TXDMAE;
668 writew(uap->dmacr,
Jun Nie534e14e2015-07-31 15:49:15 +0800669 uap->port.membase + REG_DMACR);
Russell King68b65f72010-12-22 17:24:39 +0000670 }
671 return ret;
672 }
673
674 /*
675 * We have an X-char to send. Disable DMA to prevent it loading
676 * the TX fifo, and then see if we can stuff it into the FIFO.
677 */
678 dmacr = uap->dmacr;
679 uap->dmacr &= ~UART011_TXDMAE;
Jun Nie534e14e2015-07-31 15:49:15 +0800680 writew(uap->dmacr, uap->port.membase + REG_DMACR);
Russell King68b65f72010-12-22 17:24:39 +0000681
Jun Nie534e14e2015-07-31 15:49:15 +0800682 if (readw(uap->port.membase + REG_FR) & UART01x_FR_TXFF) {
Russell King68b65f72010-12-22 17:24:39 +0000683 /*
684 * No space in the FIFO, so enable the transmit interrupt
685 * so we know when there is space. Note that once we've
686 * loaded the character, we should just re-enable DMA.
687 */
688 return false;
689 }
690
Jun Nie534e14e2015-07-31 15:49:15 +0800691 writew(uap->port.x_char, uap->port.membase + REG_DR);
Russell King68b65f72010-12-22 17:24:39 +0000692 uap->port.icount.tx++;
693 uap->port.x_char = 0;
694
695 /* Success - restore the DMA state */
696 uap->dmacr = dmacr;
Jun Nie534e14e2015-07-31 15:49:15 +0800697 writew(dmacr, uap->port.membase + REG_DMACR);
Russell King68b65f72010-12-22 17:24:39 +0000698
699 return true;
700}
701
702/*
703 * Flush the transmit buffer.
704 * Locking: called with port lock held and IRQs disabled.
705 */
706static void pl011_dma_flush_buffer(struct uart_port *port)
Fabio Estevamb83286b2013-08-09 17:58:51 -0300707__releases(&uap->port.lock)
708__acquires(&uap->port.lock)
Russell King68b65f72010-12-22 17:24:39 +0000709{
Daniel Thompsona5820c22014-09-03 12:51:55 +0100710 struct uart_amba_port *uap =
711 container_of(port, struct uart_amba_port, port);
Russell King68b65f72010-12-22 17:24:39 +0000712
Linus Walleijead76f322011-02-24 13:21:08 +0100713 if (!uap->using_tx_dma)
Russell King68b65f72010-12-22 17:24:39 +0000714 return;
715
716 /* Avoid deadlock with the DMA engine callback */
717 spin_unlock(&uap->port.lock);
718 dmaengine_terminate_all(uap->dmatx.chan);
719 spin_lock(&uap->port.lock);
720 if (uap->dmatx.queued) {
721 dma_unmap_sg(uap->dmatx.chan->device->dev, &uap->dmatx.sg, 1,
722 DMA_TO_DEVICE);
723 uap->dmatx.queued = false;
724 uap->dmacr &= ~UART011_TXDMAE;
Jun Nie534e14e2015-07-31 15:49:15 +0800725 writew(uap->dmacr, uap->port.membase + REG_DMACR);
Russell King68b65f72010-12-22 17:24:39 +0000726 }
727}
728
Linus Walleijead76f322011-02-24 13:21:08 +0100729static void pl011_dma_rx_callback(void *data);
730
731static int pl011_dma_rx_trigger_dma(struct uart_amba_port *uap)
732{
733 struct dma_chan *rxchan = uap->dmarx.chan;
Linus Walleijead76f322011-02-24 13:21:08 +0100734 struct pl011_dmarx_data *dmarx = &uap->dmarx;
735 struct dma_async_tx_descriptor *desc;
736 struct pl011_sgbuf *sgbuf;
737
738 if (!rxchan)
739 return -EIO;
740
741 /* Start the RX DMA job */
742 sgbuf = uap->dmarx.use_buf_b ?
743 &uap->dmarx.sgbuf_b : &uap->dmarx.sgbuf_a;
Alexandre Bounine16052822012-03-08 16:11:18 -0500744 desc = dmaengine_prep_slave_sg(rxchan, &sgbuf->sg, 1,
Vinod Koula485df42011-10-14 10:47:38 +0530745 DMA_DEV_TO_MEM,
Linus Walleijead76f322011-02-24 13:21:08 +0100746 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
747 /*
748 * If the DMA engine is busy and cannot prepare a
749 * channel, no big deal, the driver will fall back
750 * to interrupt mode as a result of this error code.
751 */
752 if (!desc) {
753 uap->dmarx.running = false;
754 dmaengine_terminate_all(rxchan);
755 return -EBUSY;
756 }
757
758 /* Some data to go along to the callback */
759 desc->callback = pl011_dma_rx_callback;
760 desc->callback_param = uap;
761 dmarx->cookie = dmaengine_submit(desc);
762 dma_async_issue_pending(rxchan);
763
764 uap->dmacr |= UART011_RXDMAE;
Jun Nie534e14e2015-07-31 15:49:15 +0800765 writew(uap->dmacr, uap->port.membase + REG_DMACR);
Linus Walleijead76f322011-02-24 13:21:08 +0100766 uap->dmarx.running = true;
767
768 uap->im &= ~UART011_RXIM;
Jun Nie534e14e2015-07-31 15:49:15 +0800769 writew(uap->im, uap->port.membase + REG_IMSC);
Linus Walleijead76f322011-02-24 13:21:08 +0100770
771 return 0;
772}
773
774/*
775 * This is called when either the DMA job is complete, or
776 * the FIFO timeout interrupt occurred. This must be called
777 * with the port spinlock uap->port.lock held.
778 */
779static void pl011_dma_rx_chars(struct uart_amba_port *uap,
780 u32 pending, bool use_buf_b,
781 bool readfifo)
782{
Jiri Slaby05c7cd32013-01-03 15:53:04 +0100783 struct tty_port *port = &uap->port.state->port;
Linus Walleijead76f322011-02-24 13:21:08 +0100784 struct pl011_sgbuf *sgbuf = use_buf_b ?
785 &uap->dmarx.sgbuf_b : &uap->dmarx.sgbuf_a;
Linus Walleijead76f322011-02-24 13:21:08 +0100786 int dma_count = 0;
787 u32 fifotaken = 0; /* only used for vdbg() */
788
Chanho Mincb06ff12013-03-27 18:38:11 +0900789 struct pl011_dmarx_data *dmarx = &uap->dmarx;
790 int dmataken = 0;
791
792 if (uap->dmarx.poll_rate) {
793 /* The data can be taken by polling */
794 dmataken = sgbuf->sg.length - dmarx->last_residue;
795 /* Recalculate the pending size */
796 if (pending >= dmataken)
797 pending -= dmataken;
798 }
799
800 /* Pick the remain data from the DMA */
Linus Walleijead76f322011-02-24 13:21:08 +0100801 if (pending) {
Linus Walleijead76f322011-02-24 13:21:08 +0100802
803 /*
804 * First take all chars in the DMA pipe, then look in the FIFO.
805 * Note that tty_insert_flip_buf() tries to take as many chars
806 * as it can.
807 */
Chanho Mincb06ff12013-03-27 18:38:11 +0900808 dma_count = tty_insert_flip_string(port, sgbuf->buf + dmataken,
809 pending);
Linus Walleijead76f322011-02-24 13:21:08 +0100810
811 uap->port.icount.rx += dma_count;
812 if (dma_count < pending)
813 dev_warn(uap->port.dev,
814 "couldn't insert all characters (TTY is full?)\n");
815 }
816
Chanho Mincb06ff12013-03-27 18:38:11 +0900817 /* Reset the last_residue for Rx DMA poll */
818 if (uap->dmarx.poll_rate)
819 dmarx->last_residue = sgbuf->sg.length;
820
Linus Walleijead76f322011-02-24 13:21:08 +0100821 /*
822 * Only continue with trying to read the FIFO if all DMA chars have
823 * been taken first.
824 */
825 if (dma_count == pending && readfifo) {
826 /* Clear any error flags */
827 writew(UART011_OEIS | UART011_BEIS | UART011_PEIS | UART011_FEIS,
Jun Nie534e14e2015-07-31 15:49:15 +0800828 uap->port.membase + REG_ICR);
Linus Walleijead76f322011-02-24 13:21:08 +0100829
830 /*
831 * If we read all the DMA'd characters, and we had an
Linus Walleij29772c42011-02-24 13:21:36 +0100832 * incomplete buffer, that could be due to an rx error, or
833 * maybe we just timed out. Read any pending chars and check
834 * the error status.
835 *
836 * Error conditions will only occur in the FIFO, these will
837 * trigger an immediate interrupt and stop the DMA job, so we
838 * will always find the error in the FIFO, never in the DMA
839 * buffer.
Linus Walleijead76f322011-02-24 13:21:08 +0100840 */
Linus Walleij29772c42011-02-24 13:21:36 +0100841 fifotaken = pl011_fifo_to_tty(uap);
Linus Walleijead76f322011-02-24 13:21:08 +0100842 }
843
844 spin_unlock(&uap->port.lock);
845 dev_vdbg(uap->port.dev,
846 "Took %d chars from DMA buffer and %d chars from the FIFO\n",
847 dma_count, fifotaken);
Jiri Slaby2e124b42013-01-03 15:53:06 +0100848 tty_flip_buffer_push(port);
Linus Walleijead76f322011-02-24 13:21:08 +0100849 spin_lock(&uap->port.lock);
850}
851
852static void pl011_dma_rx_irq(struct uart_amba_port *uap)
853{
854 struct pl011_dmarx_data *dmarx = &uap->dmarx;
855 struct dma_chan *rxchan = dmarx->chan;
856 struct pl011_sgbuf *sgbuf = dmarx->use_buf_b ?
857 &dmarx->sgbuf_b : &dmarx->sgbuf_a;
858 size_t pending;
859 struct dma_tx_state state;
860 enum dma_status dmastat;
861
862 /*
863 * Pause the transfer so we can trust the current counter,
864 * do this before we pause the PL011 block, else we may
865 * overflow the FIFO.
866 */
867 if (dmaengine_pause(rxchan))
868 dev_err(uap->port.dev, "unable to pause DMA transfer\n");
869 dmastat = rxchan->device->device_tx_status(rxchan,
870 dmarx->cookie, &state);
871 if (dmastat != DMA_PAUSED)
872 dev_err(uap->port.dev, "unable to pause DMA transfer\n");
873
874 /* Disable RX DMA - incoming data will wait in the FIFO */
875 uap->dmacr &= ~UART011_RXDMAE;
Jun Nie534e14e2015-07-31 15:49:15 +0800876 writew(uap->dmacr, uap->port.membase + REG_DMACR);
Linus Walleijead76f322011-02-24 13:21:08 +0100877 uap->dmarx.running = false;
878
879 pending = sgbuf->sg.length - state.residue;
880 BUG_ON(pending > PL011_DMA_BUFFER_SIZE);
881 /* Then we terminate the transfer - we now know our residue */
882 dmaengine_terminate_all(rxchan);
883
884 /*
885 * This will take the chars we have so far and insert
886 * into the framework.
887 */
888 pl011_dma_rx_chars(uap, pending, dmarx->use_buf_b, true);
889
890 /* Switch buffer & re-trigger DMA job */
891 dmarx->use_buf_b = !dmarx->use_buf_b;
892 if (pl011_dma_rx_trigger_dma(uap)) {
893 dev_dbg(uap->port.dev, "could not retrigger RX DMA job "
894 "fall back to interrupt mode\n");
895 uap->im |= UART011_RXIM;
Jun Nie534e14e2015-07-31 15:49:15 +0800896 writew(uap->im, uap->port.membase + REG_IMSC);
Linus Walleijead76f322011-02-24 13:21:08 +0100897 }
898}
899
900static void pl011_dma_rx_callback(void *data)
901{
902 struct uart_amba_port *uap = data;
903 struct pl011_dmarx_data *dmarx = &uap->dmarx;
Chanho Min6dc01aa2012-02-20 10:24:40 +0900904 struct dma_chan *rxchan = dmarx->chan;
Linus Walleijead76f322011-02-24 13:21:08 +0100905 bool lastbuf = dmarx->use_buf_b;
Chanho Min6dc01aa2012-02-20 10:24:40 +0900906 struct pl011_sgbuf *sgbuf = dmarx->use_buf_b ?
907 &dmarx->sgbuf_b : &dmarx->sgbuf_a;
908 size_t pending;
909 struct dma_tx_state state;
Linus Walleijead76f322011-02-24 13:21:08 +0100910 int ret;
911
912 /*
913 * This completion interrupt occurs typically when the
914 * RX buffer is totally stuffed but no timeout has yet
915 * occurred. When that happens, we just want the RX
916 * routine to flush out the secondary DMA buffer while
917 * we immediately trigger the next DMA job.
918 */
919 spin_lock_irq(&uap->port.lock);
Chanho Min6dc01aa2012-02-20 10:24:40 +0900920 /*
921 * Rx data can be taken by the UART interrupts during
922 * the DMA irq handler. So we check the residue here.
923 */
924 rxchan->device->device_tx_status(rxchan, dmarx->cookie, &state);
925 pending = sgbuf->sg.length - state.residue;
926 BUG_ON(pending > PL011_DMA_BUFFER_SIZE);
927 /* Then we terminate the transfer - we now know our residue */
928 dmaengine_terminate_all(rxchan);
929
Linus Walleijead76f322011-02-24 13:21:08 +0100930 uap->dmarx.running = false;
931 dmarx->use_buf_b = !lastbuf;
932 ret = pl011_dma_rx_trigger_dma(uap);
933
Chanho Min6dc01aa2012-02-20 10:24:40 +0900934 pl011_dma_rx_chars(uap, pending, lastbuf, false);
Linus Walleijead76f322011-02-24 13:21:08 +0100935 spin_unlock_irq(&uap->port.lock);
936 /*
937 * Do this check after we picked the DMA chars so we don't
938 * get some IRQ immediately from RX.
939 */
940 if (ret) {
941 dev_dbg(uap->port.dev, "could not retrigger RX DMA job "
942 "fall back to interrupt mode\n");
943 uap->im |= UART011_RXIM;
Jun Nie534e14e2015-07-31 15:49:15 +0800944 writew(uap->im, uap->port.membase + REG_IMSC);
Linus Walleijead76f322011-02-24 13:21:08 +0100945 }
946}
947
948/*
949 * Stop accepting received characters, when we're shutting down or
950 * suspending this port.
951 * Locking: called with port lock held and IRQs disabled.
952 */
953static inline void pl011_dma_rx_stop(struct uart_amba_port *uap)
954{
955 /* FIXME. Just disable the DMA enable */
956 uap->dmacr &= ~UART011_RXDMAE;
Jun Nie534e14e2015-07-31 15:49:15 +0800957 writew(uap->dmacr, uap->port.membase + REG_DMACR);
Linus Walleijead76f322011-02-24 13:21:08 +0100958}
Russell King68b65f72010-12-22 17:24:39 +0000959
Chanho Mincb06ff12013-03-27 18:38:11 +0900960/*
961 * Timer handler for Rx DMA polling.
962 * Every polling, It checks the residue in the dma buffer and transfer
963 * data to the tty. Also, last_residue is updated for the next polling.
964 */
965static void pl011_dma_rx_poll(unsigned long args)
966{
967 struct uart_amba_port *uap = (struct uart_amba_port *)args;
968 struct tty_port *port = &uap->port.state->port;
969 struct pl011_dmarx_data *dmarx = &uap->dmarx;
970 struct dma_chan *rxchan = uap->dmarx.chan;
971 unsigned long flags = 0;
972 unsigned int dmataken = 0;
973 unsigned int size = 0;
974 struct pl011_sgbuf *sgbuf;
975 int dma_count;
976 struct dma_tx_state state;
977
978 sgbuf = dmarx->use_buf_b ? &uap->dmarx.sgbuf_b : &uap->dmarx.sgbuf_a;
979 rxchan->device->device_tx_status(rxchan, dmarx->cookie, &state);
980 if (likely(state.residue < dmarx->last_residue)) {
981 dmataken = sgbuf->sg.length - dmarx->last_residue;
982 size = dmarx->last_residue - state.residue;
983 dma_count = tty_insert_flip_string(port, sgbuf->buf + dmataken,
984 size);
985 if (dma_count == size)
986 dmarx->last_residue = state.residue;
987 dmarx->last_jiffies = jiffies;
988 }
989 tty_flip_buffer_push(port);
990
991 /*
992 * If no data is received in poll_timeout, the driver will fall back
993 * to interrupt mode. We will retrigger DMA at the first interrupt.
994 */
995 if (jiffies_to_msecs(jiffies - dmarx->last_jiffies)
996 > uap->dmarx.poll_timeout) {
997
998 spin_lock_irqsave(&uap->port.lock, flags);
999 pl011_dma_rx_stop(uap);
Guennadi Liakhovetskic25a1ad2013-12-10 14:54:47 +01001000 uap->im |= UART011_RXIM;
Jun Nie534e14e2015-07-31 15:49:15 +08001001 writew(uap->im, uap->port.membase + REG_IMSC);
Chanho Mincb06ff12013-03-27 18:38:11 +09001002 spin_unlock_irqrestore(&uap->port.lock, flags);
1003
1004 uap->dmarx.running = false;
1005 dmaengine_terminate_all(rxchan);
1006 del_timer(&uap->dmarx.timer);
1007 } else {
1008 mod_timer(&uap->dmarx.timer,
1009 jiffies + msecs_to_jiffies(uap->dmarx.poll_rate));
1010 }
1011}
1012
Russell King68b65f72010-12-22 17:24:39 +00001013static void pl011_dma_startup(struct uart_amba_port *uap)
1014{
Linus Walleijead76f322011-02-24 13:21:08 +01001015 int ret;
1016
Jorge Ramirez-Ortiz1c9be312015-03-06 13:05:40 -05001017 if (!uap->dma_probed)
1018 pl011_dma_probe(uap);
1019
Russell King68b65f72010-12-22 17:24:39 +00001020 if (!uap->dmatx.chan)
1021 return;
1022
Andrew Jackson4c0be452014-11-07 14:14:35 +00001023 uap->dmatx.buf = kmalloc(PL011_DMA_BUFFER_SIZE, GFP_KERNEL | __GFP_DMA);
Russell King68b65f72010-12-22 17:24:39 +00001024 if (!uap->dmatx.buf) {
1025 dev_err(uap->port.dev, "no memory for DMA TX buffer\n");
1026 uap->port.fifosize = uap->fifosize;
1027 return;
1028 }
1029
1030 sg_init_one(&uap->dmatx.sg, uap->dmatx.buf, PL011_DMA_BUFFER_SIZE);
1031
1032 /* The DMA buffer is now the FIFO the TTY subsystem can use */
1033 uap->port.fifosize = PL011_DMA_BUFFER_SIZE;
Linus Walleijead76f322011-02-24 13:21:08 +01001034 uap->using_tx_dma = true;
Russell King68b65f72010-12-22 17:24:39 +00001035
Linus Walleijead76f322011-02-24 13:21:08 +01001036 if (!uap->dmarx.chan)
1037 goto skip_rx;
1038
1039 /* Allocate and map DMA RX buffers */
1040 ret = pl011_sgbuf_init(uap->dmarx.chan, &uap->dmarx.sgbuf_a,
1041 DMA_FROM_DEVICE);
1042 if (ret) {
1043 dev_err(uap->port.dev, "failed to init DMA %s: %d\n",
1044 "RX buffer A", ret);
1045 goto skip_rx;
1046 }
1047
1048 ret = pl011_sgbuf_init(uap->dmarx.chan, &uap->dmarx.sgbuf_b,
1049 DMA_FROM_DEVICE);
1050 if (ret) {
1051 dev_err(uap->port.dev, "failed to init DMA %s: %d\n",
1052 "RX buffer B", ret);
1053 pl011_sgbuf_free(uap->dmarx.chan, &uap->dmarx.sgbuf_a,
1054 DMA_FROM_DEVICE);
1055 goto skip_rx;
1056 }
1057
1058 uap->using_rx_dma = true;
1059
1060skip_rx:
Russell King68b65f72010-12-22 17:24:39 +00001061 /* Turn on DMA error (RX/TX will be enabled on demand) */
1062 uap->dmacr |= UART011_DMAONERR;
Jun Nie534e14e2015-07-31 15:49:15 +08001063 writew(uap->dmacr, uap->port.membase + REG_DMACR);
Russell King38d62432010-12-22 17:59:16 +00001064
1065 /*
1066 * ST Micro variants has some specific dma burst threshold
1067 * compensation. Set this to 16 bytes, so burst will only
1068 * be issued above/below 16 bytes.
1069 */
1070 if (uap->vendor->dma_threshold)
1071 writew(ST_UART011_DMAWM_RX_16 | ST_UART011_DMAWM_TX_16,
Jun Nie534e14e2015-07-31 15:49:15 +08001072 uap->port.membase + REG_ST_DMAWM);
1073
Linus Walleijead76f322011-02-24 13:21:08 +01001074
1075 if (uap->using_rx_dma) {
1076 if (pl011_dma_rx_trigger_dma(uap))
1077 dev_dbg(uap->port.dev, "could not trigger initial "
1078 "RX DMA job, fall back to interrupt mode\n");
Chanho Mincb06ff12013-03-27 18:38:11 +09001079 if (uap->dmarx.poll_rate) {
1080 init_timer(&(uap->dmarx.timer));
1081 uap->dmarx.timer.function = pl011_dma_rx_poll;
1082 uap->dmarx.timer.data = (unsigned long)uap;
1083 mod_timer(&uap->dmarx.timer,
1084 jiffies +
1085 msecs_to_jiffies(uap->dmarx.poll_rate));
1086 uap->dmarx.last_residue = PL011_DMA_BUFFER_SIZE;
1087 uap->dmarx.last_jiffies = jiffies;
1088 }
Linus Walleijead76f322011-02-24 13:21:08 +01001089 }
Russell King68b65f72010-12-22 17:24:39 +00001090}
1091
1092static void pl011_dma_shutdown(struct uart_amba_port *uap)
1093{
Linus Walleijead76f322011-02-24 13:21:08 +01001094 if (!(uap->using_tx_dma || uap->using_rx_dma))
Russell King68b65f72010-12-22 17:24:39 +00001095 return;
1096
1097 /* Disable RX and TX DMA */
Jun Nie534e14e2015-07-31 15:49:15 +08001098 while (readw(uap->port.membase + REG_FR) & UART01x_FR_BUSY)
Russell King68b65f72010-12-22 17:24:39 +00001099 barrier();
1100
1101 spin_lock_irq(&uap->port.lock);
1102 uap->dmacr &= ~(UART011_DMAONERR | UART011_RXDMAE | UART011_TXDMAE);
Jun Nie534e14e2015-07-31 15:49:15 +08001103 writew(uap->dmacr, uap->port.membase + REG_DMACR);
Russell King68b65f72010-12-22 17:24:39 +00001104 spin_unlock_irq(&uap->port.lock);
1105
Linus Walleijead76f322011-02-24 13:21:08 +01001106 if (uap->using_tx_dma) {
1107 /* In theory, this should already be done by pl011_dma_flush_buffer */
1108 dmaengine_terminate_all(uap->dmatx.chan);
1109 if (uap->dmatx.queued) {
1110 dma_unmap_sg(uap->dmatx.chan->device->dev, &uap->dmatx.sg, 1,
1111 DMA_TO_DEVICE);
1112 uap->dmatx.queued = false;
1113 }
1114
1115 kfree(uap->dmatx.buf);
1116 uap->using_tx_dma = false;
Russell King68b65f72010-12-22 17:24:39 +00001117 }
1118
Linus Walleijead76f322011-02-24 13:21:08 +01001119 if (uap->using_rx_dma) {
1120 dmaengine_terminate_all(uap->dmarx.chan);
1121 /* Clean up the RX DMA */
1122 pl011_sgbuf_free(uap->dmarx.chan, &uap->dmarx.sgbuf_a, DMA_FROM_DEVICE);
1123 pl011_sgbuf_free(uap->dmarx.chan, &uap->dmarx.sgbuf_b, DMA_FROM_DEVICE);
Chanho Mincb06ff12013-03-27 18:38:11 +09001124 if (uap->dmarx.poll_rate)
1125 del_timer_sync(&uap->dmarx.timer);
Linus Walleijead76f322011-02-24 13:21:08 +01001126 uap->using_rx_dma = false;
1127 }
Russell King68b65f72010-12-22 17:24:39 +00001128}
1129
Linus Walleijead76f322011-02-24 13:21:08 +01001130static inline bool pl011_dma_rx_available(struct uart_amba_port *uap)
1131{
1132 return uap->using_rx_dma;
1133}
1134
1135static inline bool pl011_dma_rx_running(struct uart_amba_port *uap)
1136{
1137 return uap->using_rx_dma && uap->dmarx.running;
1138}
1139
Russell King68b65f72010-12-22 17:24:39 +00001140#else
1141/* Blank functions if the DMA engine is not available */
Jorge Ramirez-Ortiz1c9be312015-03-06 13:05:40 -05001142static inline void pl011_dma_probe(struct uart_amba_port *uap)
Russell King68b65f72010-12-22 17:24:39 +00001143{
1144}
1145
1146static inline void pl011_dma_remove(struct uart_amba_port *uap)
1147{
1148}
1149
1150static inline void pl011_dma_startup(struct uart_amba_port *uap)
1151{
1152}
1153
1154static inline void pl011_dma_shutdown(struct uart_amba_port *uap)
1155{
1156}
1157
1158static inline bool pl011_dma_tx_irq(struct uart_amba_port *uap)
1159{
1160 return false;
1161}
1162
1163static inline void pl011_dma_tx_stop(struct uart_amba_port *uap)
1164{
1165}
1166
1167static inline bool pl011_dma_tx_start(struct uart_amba_port *uap)
1168{
1169 return false;
1170}
1171
Linus Walleijead76f322011-02-24 13:21:08 +01001172static inline void pl011_dma_rx_irq(struct uart_amba_port *uap)
1173{
1174}
1175
1176static inline void pl011_dma_rx_stop(struct uart_amba_port *uap)
1177{
1178}
1179
1180static inline int pl011_dma_rx_trigger_dma(struct uart_amba_port *uap)
1181{
1182 return -EIO;
1183}
1184
1185static inline bool pl011_dma_rx_available(struct uart_amba_port *uap)
1186{
1187 return false;
1188}
1189
1190static inline bool pl011_dma_rx_running(struct uart_amba_port *uap)
1191{
1192 return false;
1193}
1194
Russell King68b65f72010-12-22 17:24:39 +00001195#define pl011_dma_flush_buffer NULL
1196#endif
1197
Russell Kingb129a8c2005-08-31 10:12:14 +01001198static void pl011_stop_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001200 struct uart_amba_port *uap =
1201 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202
1203 uap->im &= ~UART011_TXIM;
Jun Nie534e14e2015-07-31 15:49:15 +08001204 writew(uap->im, uap->port.membase + REG_IMSC);
Russell King68b65f72010-12-22 17:24:39 +00001205 pl011_dma_tx_stop(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206}
1207
Dave Martin1e84d222015-04-27 16:49:05 +01001208static void pl011_tx_chars(struct uart_amba_port *uap, bool from_irq);
Dave Martin734745c2015-03-04 12:27:33 +00001209
1210/* Start TX with programmed I/O only (no DMA) */
1211static void pl011_start_tx_pio(struct uart_amba_port *uap)
1212{
1213 uap->im |= UART011_TXIM;
Jun Nie534e14e2015-07-31 15:49:15 +08001214 writew(uap->im, uap->port.membase + REG_IMSC);
Dave Martin1e84d222015-04-27 16:49:05 +01001215 pl011_tx_chars(uap, false);
Dave Martin734745c2015-03-04 12:27:33 +00001216}
1217
Russell Kingb129a8c2005-08-31 10:12:14 +01001218static void pl011_start_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001220 struct uart_amba_port *uap =
1221 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222
Dave Martin734745c2015-03-04 12:27:33 +00001223 if (!pl011_dma_tx_start(uap))
1224 pl011_start_tx_pio(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225}
1226
1227static void pl011_stop_rx(struct uart_port *port)
1228{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001229 struct uart_amba_port *uap =
1230 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231
1232 uap->im &= ~(UART011_RXIM|UART011_RTIM|UART011_FEIM|
1233 UART011_PEIM|UART011_BEIM|UART011_OEIM);
Jun Nie534e14e2015-07-31 15:49:15 +08001234 writew(uap->im, uap->port.membase + REG_IMSC);
Linus Walleijead76f322011-02-24 13:21:08 +01001235
1236 pl011_dma_rx_stop(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237}
1238
1239static void pl011_enable_ms(struct uart_port *port)
1240{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001241 struct uart_amba_port *uap =
1242 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243
1244 uap->im |= UART011_RIMIM|UART011_CTSMIM|UART011_DCDMIM|UART011_DSRMIM;
Jun Nie534e14e2015-07-31 15:49:15 +08001245 writew(uap->im, uap->port.membase + REG_IMSC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246}
1247
David Howells7d12e782006-10-05 14:55:46 +01001248static void pl011_rx_chars(struct uart_amba_port *uap)
Fabio Estevamb83286b2013-08-09 17:58:51 -03001249__releases(&uap->port.lock)
1250__acquires(&uap->port.lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251{
Linus Walleij29772c42011-02-24 13:21:36 +01001252 pl011_fifo_to_tty(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253
Thomas Gleixner2389b272007-05-29 21:53:50 +01001254 spin_unlock(&uap->port.lock);
Jiri Slaby2e124b42013-01-03 15:53:06 +01001255 tty_flip_buffer_push(&uap->port.state->port);
Linus Walleijead76f322011-02-24 13:21:08 +01001256 /*
1257 * If we were temporarily out of DMA mode for a while,
1258 * attempt to switch back to DMA mode again.
1259 */
1260 if (pl011_dma_rx_available(uap)) {
1261 if (pl011_dma_rx_trigger_dma(uap)) {
1262 dev_dbg(uap->port.dev, "could not trigger RX DMA job "
1263 "fall back to interrupt mode again\n");
1264 uap->im |= UART011_RXIM;
Jun Nie534e14e2015-07-31 15:49:15 +08001265 writew(uap->im, uap->port.membase + REG_IMSC);
Chanho Mincb06ff12013-03-27 18:38:11 +09001266 } else {
Chanho Min89fa28d2013-04-03 11:10:37 +09001267#ifdef CONFIG_DMA_ENGINE
Chanho Mincb06ff12013-03-27 18:38:11 +09001268 /* Start Rx DMA poll */
1269 if (uap->dmarx.poll_rate) {
1270 uap->dmarx.last_jiffies = jiffies;
1271 uap->dmarx.last_residue = PL011_DMA_BUFFER_SIZE;
1272 mod_timer(&uap->dmarx.timer,
1273 jiffies +
1274 msecs_to_jiffies(uap->dmarx.poll_rate));
1275 }
Chanho Min89fa28d2013-04-03 11:10:37 +09001276#endif
Chanho Mincb06ff12013-03-27 18:38:11 +09001277 }
Linus Walleijead76f322011-02-24 13:21:08 +01001278 }
Thomas Gleixner2389b272007-05-29 21:53:50 +01001279 spin_lock(&uap->port.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280}
1281
Dave Martin1e84d222015-04-27 16:49:05 +01001282static bool pl011_tx_char(struct uart_amba_port *uap, unsigned char c,
1283 bool from_irq)
Dave Martin734745c2015-03-04 12:27:33 +00001284{
Dave Martin1e84d222015-04-27 16:49:05 +01001285 if (unlikely(!from_irq) &&
Jun Nie534e14e2015-07-31 15:49:15 +08001286 readw(uap->port.membase + REG_FR) & UART01x_FR_TXFF)
Dave Martin1e84d222015-04-27 16:49:05 +01001287 return false; /* unable to transmit character */
1288
Jun Nie534e14e2015-07-31 15:49:15 +08001289 writew(c, uap->port.membase + REG_DR);
Dave Martin734745c2015-03-04 12:27:33 +00001290 uap->port.icount.tx++;
1291
Dave Martin1e84d222015-04-27 16:49:05 +01001292 return true;
Dave Martin734745c2015-03-04 12:27:33 +00001293}
1294
Dave Martin1e84d222015-04-27 16:49:05 +01001295static void pl011_tx_chars(struct uart_amba_port *uap, bool from_irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296{
Alan Coxebd2c8f2009-09-19 13:13:28 -07001297 struct circ_buf *xmit = &uap->port.state->xmit;
Dave Martin1e84d222015-04-27 16:49:05 +01001298 int count = uap->fifosize >> 1;
Dave Martin734745c2015-03-04 12:27:33 +00001299
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 if (uap->port.x_char) {
Dave Martin1e84d222015-04-27 16:49:05 +01001301 if (!pl011_tx_char(uap, uap->port.x_char, from_irq))
1302 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 uap->port.x_char = 0;
Dave Martin734745c2015-03-04 12:27:33 +00001304 --count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 }
1306 if (uart_circ_empty(xmit) || uart_tx_stopped(&uap->port)) {
Russell Kingb129a8c2005-08-31 10:12:14 +01001307 pl011_stop_tx(&uap->port);
Dave Martin1e84d222015-04-27 16:49:05 +01001308 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 }
1310
Russell King68b65f72010-12-22 17:24:39 +00001311 /* If we are using DMA mode, try to send some characters. */
1312 if (pl011_dma_tx_irq(uap))
Dave Martin1e84d222015-04-27 16:49:05 +01001313 return;
Russell King68b65f72010-12-22 17:24:39 +00001314
Dave Martin1e84d222015-04-27 16:49:05 +01001315 do {
1316 if (likely(from_irq) && count-- == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 break;
Dave Martin1e84d222015-04-27 16:49:05 +01001318
1319 if (!pl011_tx_char(uap, xmit->buf[xmit->tail], from_irq))
1320 break;
1321
1322 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
1323 } while (!uart_circ_empty(xmit));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324
1325 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
1326 uart_write_wakeup(&uap->port);
1327
Dave Martin1e84d222015-04-27 16:49:05 +01001328 if (uart_circ_empty(xmit))
Russell Kingb129a8c2005-08-31 10:12:14 +01001329 pl011_stop_tx(&uap->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330}
1331
1332static void pl011_modem_status(struct uart_amba_port *uap)
1333{
1334 unsigned int status, delta;
1335
Jun Nie534e14e2015-07-31 15:49:15 +08001336 status = readw(uap->port.membase + REG_FR) & UART01x_FR_MODEM_ANY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337
1338 delta = status ^ uap->old_status;
1339 uap->old_status = status;
1340
1341 if (!delta)
1342 return;
1343
1344 if (delta & UART01x_FR_DCD)
1345 uart_handle_dcd_change(&uap->port, status & UART01x_FR_DCD);
1346
1347 if (delta & UART01x_FR_DSR)
1348 uap->port.icount.dsr++;
1349
1350 if (delta & UART01x_FR_CTS)
1351 uart_handle_cts_change(&uap->port, status & UART01x_FR_CTS);
1352
Alan Coxbdc04e32009-09-19 13:13:31 -07001353 wake_up_interruptible(&uap->port.state->port.delta_msr_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354}
1355
Andre Przywara9c4ef4b2015-05-21 17:26:20 +01001356static void check_apply_cts_event_workaround(struct uart_amba_port *uap)
1357{
1358 unsigned int dummy_read;
1359
1360 if (!uap->vendor->cts_event_workaround)
1361 return;
1362
1363 /* workaround to make sure that all bits are unlocked.. */
Jun Nie534e14e2015-07-31 15:49:15 +08001364 writew(0x00, uap->port.membase + REG_ICR);
Andre Przywara9c4ef4b2015-05-21 17:26:20 +01001365
1366 /*
1367 * WA: introduce 26ns(1 uart clk) delay before W1C;
1368 * single apb access will incur 2 pclk(133.12Mhz) delay,
1369 * so add 2 dummy reads
1370 */
Jun Nie534e14e2015-07-31 15:49:15 +08001371 dummy_read = readw(uap->port.membase + REG_ICR);
1372 dummy_read = readw(uap->port.membase + REG_ICR);
Andre Przywara9c4ef4b2015-05-21 17:26:20 +01001373}
1374
David Howells7d12e782006-10-05 14:55:46 +01001375static irqreturn_t pl011_int(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376{
1377 struct uart_amba_port *uap = dev_id;
Russell King963cc982010-12-22 17:16:09 +00001378 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 unsigned int status, pass_counter = AMBA_ISR_PASS_LIMIT;
Andre Przywara075167e2015-05-21 17:26:19 +01001380 u16 imsc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 int handled = 0;
1382
Russell King963cc982010-12-22 17:16:09 +00001383 spin_lock_irqsave(&uap->port.lock, flags);
Jun Nie534e14e2015-07-31 15:49:15 +08001384 imsc = readw(uap->port.membase + REG_IMSC);
1385 status = readw(uap->port.membase + REG_RIS) & imsc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 if (status) {
1387 do {
Andre Przywara9c4ef4b2015-05-21 17:26:20 +01001388 check_apply_cts_event_workaround(uap);
Rajanikanth H.V4fd06902012-03-26 11:17:02 +02001389
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 writew(status & ~(UART011_TXIS|UART011_RTIS|
1391 UART011_RXIS),
Jun Nie534e14e2015-07-31 15:49:15 +08001392 uap->port.membase + REG_ICR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393
Linus Walleijead76f322011-02-24 13:21:08 +01001394 if (status & (UART011_RTIS|UART011_RXIS)) {
1395 if (pl011_dma_rx_running(uap))
1396 pl011_dma_rx_irq(uap);
1397 else
1398 pl011_rx_chars(uap);
1399 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 if (status & (UART011_DSRMIS|UART011_DCDMIS|
1401 UART011_CTSMIS|UART011_RIMIS))
1402 pl011_modem_status(uap);
Dave Martin1e84d222015-04-27 16:49:05 +01001403 if (status & UART011_TXIS)
1404 pl011_tx_chars(uap, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405
Rajanikanth H.V4fd06902012-03-26 11:17:02 +02001406 if (pass_counter-- == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 break;
1408
Jun Nie534e14e2015-07-31 15:49:15 +08001409 status = readw(uap->port.membase + REG_RIS) & imsc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 } while (status != 0);
1411 handled = 1;
1412 }
1413
Russell King963cc982010-12-22 17:16:09 +00001414 spin_unlock_irqrestore(&uap->port.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415
1416 return IRQ_RETVAL(handled);
1417}
1418
Linus Walleije643f872012-06-17 15:44:19 +02001419static unsigned int pl011_tx_empty(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001421 struct uart_amba_port *uap =
1422 container_of(port, struct uart_amba_port, port);
Jun Nie534e14e2015-07-31 15:49:15 +08001423 unsigned int status = readw(uap->port.membase + REG_FR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 return status & (UART01x_FR_BUSY|UART01x_FR_TXFF) ? 0 : TIOCSER_TEMT;
1425}
1426
Linus Walleije643f872012-06-17 15:44:19 +02001427static unsigned int pl011_get_mctrl(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001429 struct uart_amba_port *uap =
1430 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 unsigned int result = 0;
Jun Nie534e14e2015-07-31 15:49:15 +08001432 unsigned int status = readw(uap->port.membase + REG_FR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433
Jiri Slaby5159f402007-10-18 23:40:31 -07001434#define TIOCMBIT(uartbit, tiocmbit) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 if (status & uartbit) \
1436 result |= tiocmbit
1437
Jiri Slaby5159f402007-10-18 23:40:31 -07001438 TIOCMBIT(UART01x_FR_DCD, TIOCM_CAR);
1439 TIOCMBIT(UART01x_FR_DSR, TIOCM_DSR);
1440 TIOCMBIT(UART01x_FR_CTS, TIOCM_CTS);
1441 TIOCMBIT(UART011_FR_RI, TIOCM_RNG);
1442#undef TIOCMBIT
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 return result;
1444}
1445
1446static void pl011_set_mctrl(struct uart_port *port, unsigned int mctrl)
1447{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001448 struct uart_amba_port *uap =
1449 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 unsigned int cr;
1451
Jun Nie534e14e2015-07-31 15:49:15 +08001452 cr = readw(uap->port.membase + REG_CR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453
Jiri Slaby5159f402007-10-18 23:40:31 -07001454#define TIOCMBIT(tiocmbit, uartbit) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 if (mctrl & tiocmbit) \
1456 cr |= uartbit; \
1457 else \
1458 cr &= ~uartbit
1459
Jiri Slaby5159f402007-10-18 23:40:31 -07001460 TIOCMBIT(TIOCM_RTS, UART011_CR_RTS);
1461 TIOCMBIT(TIOCM_DTR, UART011_CR_DTR);
1462 TIOCMBIT(TIOCM_OUT1, UART011_CR_OUT1);
1463 TIOCMBIT(TIOCM_OUT2, UART011_CR_OUT2);
1464 TIOCMBIT(TIOCM_LOOP, UART011_CR_LBE);
Rabin Vincent3b438162010-02-12 06:43:11 +01001465
1466 if (uap->autorts) {
1467 /* We need to disable auto-RTS if we want to turn RTS off */
1468 TIOCMBIT(TIOCM_RTS, UART011_CR_RTSEN);
1469 }
Jiri Slaby5159f402007-10-18 23:40:31 -07001470#undef TIOCMBIT
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471
Jun Nie534e14e2015-07-31 15:49:15 +08001472 writew(cr, uap->port.membase + REG_CR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473}
1474
1475static void pl011_break_ctl(struct uart_port *port, int break_state)
1476{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001477 struct uart_amba_port *uap =
1478 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 unsigned long flags;
1480 unsigned int lcr_h;
1481
1482 spin_lock_irqsave(&uap->port.lock, flags);
Linus Walleijec489aa2010-06-02 08:13:52 +01001483 lcr_h = readw(uap->port.membase + uap->lcrh_tx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 if (break_state == -1)
1485 lcr_h |= UART01x_LCRH_BRK;
1486 else
1487 lcr_h &= ~UART01x_LCRH_BRK;
Linus Walleijec489aa2010-06-02 08:13:52 +01001488 writew(lcr_h, uap->port.membase + uap->lcrh_tx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 spin_unlock_irqrestore(&uap->port.lock, flags);
1490}
1491
Jason Wessel84b5ae12008-02-20 13:33:39 -06001492#ifdef CONFIG_CONSOLE_POLL
Anton Vorontsov5c8124a2012-09-24 14:27:55 -07001493
1494static void pl011_quiesce_irqs(struct uart_port *port)
1495{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001496 struct uart_amba_port *uap =
1497 container_of(port, struct uart_amba_port, port);
Anton Vorontsov5c8124a2012-09-24 14:27:55 -07001498 unsigned char __iomem *regs = uap->port.membase;
1499
Jun Nie534e14e2015-07-31 15:49:15 +08001500 writew(readw(regs + REG_MIS), regs + REG_ICR);
Anton Vorontsov5c8124a2012-09-24 14:27:55 -07001501 /*
1502 * There is no way to clear TXIM as this is "ready to transmit IRQ", so
1503 * we simply mask it. start_tx() will unmask it.
1504 *
1505 * Note we can race with start_tx(), and if the race happens, the
1506 * polling user might get another interrupt just after we clear it.
1507 * But it should be OK and can happen even w/o the race, e.g.
1508 * controller immediately got some new data and raised the IRQ.
1509 *
1510 * And whoever uses polling routines assumes that it manages the device
1511 * (including tx queue), so we're also fine with start_tx()'s caller
1512 * side.
1513 */
Jun Nie534e14e2015-07-31 15:49:15 +08001514 writew(readw(regs + REG_IMSC) & ~UART011_TXIM, regs + REG_IMSC);
Anton Vorontsov5c8124a2012-09-24 14:27:55 -07001515}
1516
Linus Walleije643f872012-06-17 15:44:19 +02001517static int pl011_get_poll_char(struct uart_port *port)
Jason Wessel84b5ae12008-02-20 13:33:39 -06001518{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001519 struct uart_amba_port *uap =
1520 container_of(port, struct uart_amba_port, port);
Jason Wessel84b5ae12008-02-20 13:33:39 -06001521 unsigned int status;
1522
Anton Vorontsov5c8124a2012-09-24 14:27:55 -07001523 /*
1524 * The caller might need IRQs lowered, e.g. if used with KDB NMI
1525 * debugger.
1526 */
1527 pl011_quiesce_irqs(port);
1528
Jun Nie534e14e2015-07-31 15:49:15 +08001529 status = readw(uap->port.membase + REG_FR);
Jason Wesself5316b42010-05-20 21:04:22 -05001530 if (status & UART01x_FR_RXFE)
1531 return NO_POLL_CHAR;
Jason Wessel84b5ae12008-02-20 13:33:39 -06001532
Jun Nie534e14e2015-07-31 15:49:15 +08001533 return readw(uap->port.membase + REG_DR);
Jason Wessel84b5ae12008-02-20 13:33:39 -06001534}
1535
Linus Walleije643f872012-06-17 15:44:19 +02001536static void pl011_put_poll_char(struct uart_port *port,
Jason Wessel84b5ae12008-02-20 13:33:39 -06001537 unsigned char ch)
1538{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001539 struct uart_amba_port *uap =
1540 container_of(port, struct uart_amba_port, port);
Jason Wessel84b5ae12008-02-20 13:33:39 -06001541
Jun Nie534e14e2015-07-31 15:49:15 +08001542 while (readw(uap->port.membase + REG_FR) & UART01x_FR_TXFF)
Jason Wessel84b5ae12008-02-20 13:33:39 -06001543 barrier();
1544
Jun Nie534e14e2015-07-31 15:49:15 +08001545 writew(ch, uap->port.membase + REG_DR);
Jason Wessel84b5ae12008-02-20 13:33:39 -06001546}
1547
1548#endif /* CONFIG_CONSOLE_POLL */
1549
Anton Vorontsovb3564c22012-09-24 14:27:54 -07001550static int pl011_hwinit(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001552 struct uart_amba_port *uap =
1553 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 int retval;
1555
Linus Walleij78d80c52012-05-23 21:18:46 +02001556 /* Optionaly enable pins to be muxed in and configured */
Linus Walleij2b996fc2013-06-05 15:36:42 +02001557 pinctrl_pm_select_default_state(port->dev);
Linus Walleij78d80c52012-05-23 21:18:46 +02001558
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 /*
1560 * Try to enable the clock producer.
1561 */
Julia Lawall1c4c4392012-08-26 18:01:01 +02001562 retval = clk_prepare_enable(uap->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 if (retval)
Tushar Behera7f6d9422014-06-26 15:35:35 +05301564 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565
1566 uap->port.uartclk = clk_get_rate(uap->clk);
1567
Linus Walleij9b96fba2012-03-13 13:27:23 +01001568 /* Clear pending error and receive interrupts */
1569 writew(UART011_OEIS | UART011_BEIS | UART011_PEIS | UART011_FEIS |
Jun Nie534e14e2015-07-31 15:49:15 +08001570 UART011_RTIS | UART011_RXIS, uap->port.membase + REG_ICR);
Linus Walleij9b96fba2012-03-13 13:27:23 +01001571
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 /*
Anton Vorontsovb3564c22012-09-24 14:27:54 -07001573 * Save interrupts enable mask, and enable RX interrupts in case if
1574 * the interrupt is used for NMI entry.
1575 */
Jun Nie534e14e2015-07-31 15:49:15 +08001576 uap->im = readw(uap->port.membase + REG_IMSC);
1577 writew(UART011_RTIM | UART011_RXIM, uap->port.membase + REG_IMSC);
Anton Vorontsovb3564c22012-09-24 14:27:54 -07001578
Jingoo Han574de552013-07-30 17:06:57 +09001579 if (dev_get_platdata(uap->port.dev)) {
Anton Vorontsovb3564c22012-09-24 14:27:54 -07001580 struct amba_pl011_data *plat;
1581
Jingoo Han574de552013-07-30 17:06:57 +09001582 plat = dev_get_platdata(uap->port.dev);
Anton Vorontsovb3564c22012-09-24 14:27:54 -07001583 if (plat->init)
1584 plat->init();
1585 }
1586 return 0;
Anton Vorontsovb3564c22012-09-24 14:27:54 -07001587}
1588
Jon Medhurstb60f2f62013-12-10 10:18:59 +00001589static void pl011_write_lcr_h(struct uart_amba_port *uap, unsigned int lcr_h)
1590{
1591 writew(lcr_h, uap->port.membase + uap->lcrh_rx);
1592 if (uap->lcrh_rx != uap->lcrh_tx) {
1593 int i;
1594 /*
1595 * Wait 10 PCLKs before writing LCRH_TX register,
1596 * to get this delay write read only register 10 times
1597 */
1598 for (i = 0; i < 10; ++i)
Jun Nie534e14e2015-07-31 15:49:15 +08001599 writew(0xff, uap->port.membase + REG_MIS);
Jon Medhurstb60f2f62013-12-10 10:18:59 +00001600 writew(lcr_h, uap->port.membase + uap->lcrh_tx);
1601 }
1602}
1603
Andre Przywara867b8e82015-05-21 17:26:15 +01001604static int pl011_allocate_irq(struct uart_amba_port *uap)
1605{
Jun Nie534e14e2015-07-31 15:49:15 +08001606 writew(uap->im, uap->port.membase + REG_IMSC);
Andre Przywara867b8e82015-05-21 17:26:15 +01001607
1608 return request_irq(uap->port.irq, pl011_int, 0, "uart-pl011", uap);
1609}
1610
1611/*
1612 * Enable interrupts, only timeouts when using DMA
1613 * if initial RX DMA job failed, start in interrupt mode
1614 * as well.
1615 */
1616static void pl011_enable_interrupts(struct uart_amba_port *uap)
1617{
1618 spin_lock_irq(&uap->port.lock);
1619
1620 /* Clear out any spuriously appearing RX interrupts */
1621 writew(UART011_RTIS | UART011_RXIS,
Jun Nie534e14e2015-07-31 15:49:15 +08001622 uap->port.membase + REG_ICR);
Andre Przywara867b8e82015-05-21 17:26:15 +01001623 uap->im = UART011_RTIM;
1624 if (!pl011_dma_rx_running(uap))
1625 uap->im |= UART011_RXIM;
Jun Nie534e14e2015-07-31 15:49:15 +08001626 writew(uap->im, uap->port.membase + REG_IMSC);
Andre Przywara867b8e82015-05-21 17:26:15 +01001627 spin_unlock_irq(&uap->port.lock);
1628}
1629
Anton Vorontsovb3564c22012-09-24 14:27:54 -07001630static int pl011_startup(struct uart_port *port)
1631{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001632 struct uart_amba_port *uap =
1633 container_of(port, struct uart_amba_port, port);
Dave Martin734745c2015-03-04 12:27:33 +00001634 unsigned int cr;
Anton Vorontsovb3564c22012-09-24 14:27:54 -07001635 int retval;
1636
1637 retval = pl011_hwinit(port);
1638 if (retval)
1639 goto clk_dis;
1640
Andre Przywara867b8e82015-05-21 17:26:15 +01001641 retval = pl011_allocate_irq(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 if (retval)
1643 goto clk_dis;
1644
Jun Nie534e14e2015-07-31 15:49:15 +08001645 writew(uap->vendor->ifls, uap->port.membase + REG_IFLS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646
Jon Medhurstfe433902013-12-10 10:18:58 +00001647 spin_lock_irq(&uap->port.lock);
1648
Shreshtha Kumar Sahud8d8ffa2012-01-18 15:53:59 +05301649 /* restore RTS and DTR */
1650 cr = uap->old_cr & (UART011_CR_RTS | UART011_CR_DTR);
1651 cr |= UART01x_CR_UARTEN | UART011_CR_RXE | UART011_CR_TXE;
Jun Nie534e14e2015-07-31 15:49:15 +08001652 writew(cr, uap->port.membase + REG_CR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653
Jon Medhurstfe433902013-12-10 10:18:58 +00001654 spin_unlock_irq(&uap->port.lock);
1655
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 /*
1657 * initialise the old status of the modem signals
1658 */
Jun Nie534e14e2015-07-31 15:49:15 +08001659 uap->old_status = readw(uap->port.membase + REG_FR) &
1660 UART01x_FR_MODEM_ANY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661
Russell King68b65f72010-12-22 17:24:39 +00001662 /* Startup DMA */
1663 pl011_dma_startup(uap);
1664
Andre Przywara867b8e82015-05-21 17:26:15 +01001665 pl011_enable_interrupts(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666
1667 return 0;
1668
1669 clk_dis:
Julia Lawall1c4c4392012-08-26 18:01:01 +02001670 clk_disable_unprepare(uap->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 return retval;
1672}
1673
Andre Przywara0dd1e242015-05-21 17:26:23 +01001674static int sbsa_uart_startup(struct uart_port *port)
1675{
1676 struct uart_amba_port *uap =
1677 container_of(port, struct uart_amba_port, port);
1678 int retval;
1679
1680 retval = pl011_hwinit(port);
1681 if (retval)
1682 return retval;
1683
1684 retval = pl011_allocate_irq(uap);
1685 if (retval)
1686 return retval;
1687
1688 /* The SBSA UART does not support any modem status lines. */
1689 uap->old_status = 0;
1690
1691 pl011_enable_interrupts(uap);
1692
1693 return 0;
1694}
1695
Linus Walleijec489aa2010-06-02 08:13:52 +01001696static void pl011_shutdown_channel(struct uart_amba_port *uap,
1697 unsigned int lcrh)
1698{
1699 unsigned long val;
1700
1701 val = readw(uap->port.membase + lcrh);
1702 val &= ~(UART01x_LCRH_BRK | UART01x_LCRH_FEN);
1703 writew(val, uap->port.membase + lcrh);
1704}
1705
Andre Przywara95166a32015-05-21 17:26:16 +01001706/*
1707 * disable the port. It should not disable RTS and DTR.
1708 * Also RTS and DTR state should be preserved to restore
1709 * it during startup().
1710 */
1711static void pl011_disable_uart(struct uart_amba_port *uap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712{
Shreshtha Kumar Sahud8d8ffa2012-01-18 15:53:59 +05301713 unsigned int cr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714
Rabin Vincent3b438162010-02-12 06:43:11 +01001715 uap->autorts = false;
Jon Medhurstfe433902013-12-10 10:18:58 +00001716 spin_lock_irq(&uap->port.lock);
Jun Nie534e14e2015-07-31 15:49:15 +08001717 cr = readw(uap->port.membase + REG_CR);
Shreshtha Kumar Sahud8d8ffa2012-01-18 15:53:59 +05301718 uap->old_cr = cr;
1719 cr &= UART011_CR_RTS | UART011_CR_DTR;
1720 cr |= UART01x_CR_UARTEN | UART011_CR_TXE;
Jun Nie534e14e2015-07-31 15:49:15 +08001721 writew(cr, uap->port.membase + REG_CR);
Jon Medhurstfe433902013-12-10 10:18:58 +00001722 spin_unlock_irq(&uap->port.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723
1724 /*
1725 * disable break condition and fifos
1726 */
Linus Walleijec489aa2010-06-02 08:13:52 +01001727 pl011_shutdown_channel(uap, uap->lcrh_rx);
1728 if (uap->lcrh_rx != uap->lcrh_tx)
1729 pl011_shutdown_channel(uap, uap->lcrh_tx);
Andre Przywara95166a32015-05-21 17:26:16 +01001730}
1731
1732static void pl011_disable_interrupts(struct uart_amba_port *uap)
1733{
1734 spin_lock_irq(&uap->port.lock);
1735
1736 /* mask all interrupts and clear all pending ones */
1737 uap->im = 0;
Jun Nie534e14e2015-07-31 15:49:15 +08001738 writew(uap->im, uap->port.membase + REG_IMSC);
1739 writew(0xffff, uap->port.membase + REG_ICR);
Andre Przywara95166a32015-05-21 17:26:16 +01001740
1741 spin_unlock_irq(&uap->port.lock);
1742}
1743
1744static void pl011_shutdown(struct uart_port *port)
1745{
1746 struct uart_amba_port *uap =
1747 container_of(port, struct uart_amba_port, port);
1748
1749 pl011_disable_interrupts(uap);
1750
1751 pl011_dma_shutdown(uap);
1752
1753 free_irq(uap->port.irq, uap);
1754
1755 pl011_disable_uart(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756
1757 /*
1758 * Shut down the clock producer
1759 */
Julia Lawall1c4c4392012-08-26 18:01:01 +02001760 clk_disable_unprepare(uap->clk);
Linus Walleij78d80c52012-05-23 21:18:46 +02001761 /* Optionally let pins go into sleep states */
Linus Walleij2b996fc2013-06-05 15:36:42 +02001762 pinctrl_pm_select_sleep_state(port->dev);
Shreshtha Kumar Sahuc16d51a2011-06-13 10:11:33 +02001763
Jingoo Han574de552013-07-30 17:06:57 +09001764 if (dev_get_platdata(uap->port.dev)) {
Shreshtha Kumar Sahuc16d51a2011-06-13 10:11:33 +02001765 struct amba_pl011_data *plat;
1766
Jingoo Han574de552013-07-30 17:06:57 +09001767 plat = dev_get_platdata(uap->port.dev);
Shreshtha Kumar Sahuc16d51a2011-06-13 10:11:33 +02001768 if (plat->exit)
1769 plat->exit();
1770 }
1771
Peter Hurley36f339d2014-11-06 09:06:12 -05001772 if (uap->port.ops->flush_buffer)
1773 uap->port.ops->flush_buffer(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774}
1775
Andre Przywara0dd1e242015-05-21 17:26:23 +01001776static void sbsa_uart_shutdown(struct uart_port *port)
1777{
1778 struct uart_amba_port *uap =
1779 container_of(port, struct uart_amba_port, port);
1780
1781 pl011_disable_interrupts(uap);
1782
1783 free_irq(uap->port.irq, uap);
1784
1785 if (uap->port.ops->flush_buffer)
1786 uap->port.ops->flush_buffer(port);
1787}
1788
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789static void
Andre Przywaraef5a9352015-05-21 17:26:17 +01001790pl011_setup_status_masks(struct uart_port *port, struct ktermios *termios)
1791{
1792 port->read_status_mask = UART011_DR_OE | 255;
1793 if (termios->c_iflag & INPCK)
1794 port->read_status_mask |= UART011_DR_FE | UART011_DR_PE;
1795 if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
1796 port->read_status_mask |= UART011_DR_BE;
1797
1798 /*
1799 * Characters to ignore
1800 */
1801 port->ignore_status_mask = 0;
1802 if (termios->c_iflag & IGNPAR)
1803 port->ignore_status_mask |= UART011_DR_FE | UART011_DR_PE;
1804 if (termios->c_iflag & IGNBRK) {
1805 port->ignore_status_mask |= UART011_DR_BE;
1806 /*
1807 * If we're ignoring parity and break indicators,
1808 * ignore overruns too (for real raw support).
1809 */
1810 if (termios->c_iflag & IGNPAR)
1811 port->ignore_status_mask |= UART011_DR_OE;
1812 }
1813
1814 /*
1815 * Ignore all characters if CREAD is not set.
1816 */
1817 if ((termios->c_cflag & CREAD) == 0)
1818 port->ignore_status_mask |= UART_DUMMY_DR_RX;
1819}
1820
1821static void
Alan Cox606d0992006-12-08 02:38:45 -08001822pl011_set_termios(struct uart_port *port, struct ktermios *termios,
1823 struct ktermios *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001825 struct uart_amba_port *uap =
1826 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 unsigned int lcr_h, old_cr;
1828 unsigned long flags;
Russell Kingc19f12b2010-12-22 17:48:26 +00001829 unsigned int baud, quot, clkdiv;
1830
1831 if (uap->vendor->oversampling)
1832 clkdiv = 8;
1833 else
1834 clkdiv = 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835
1836 /*
1837 * Ask the core to calculate the divisor for us.
1838 */
Linus Walleijac3e3fb2010-06-02 20:40:22 +01001839 baud = uart_get_baud_rate(port, termios, old, 0,
Russell Kingc19f12b2010-12-22 17:48:26 +00001840 port->uartclk / clkdiv);
Chanho Min89fa28d2013-04-03 11:10:37 +09001841#ifdef CONFIG_DMA_ENGINE
Chanho Mincb06ff12013-03-27 18:38:11 +09001842 /*
1843 * Adjust RX DMA polling rate with baud rate if not specified.
1844 */
1845 if (uap->dmarx.auto_poll_rate)
1846 uap->dmarx.poll_rate = DIV_ROUND_UP(10000000, baud);
Chanho Min89fa28d2013-04-03 11:10:37 +09001847#endif
Linus Walleijac3e3fb2010-06-02 20:40:22 +01001848
1849 if (baud > port->uartclk/16)
1850 quot = DIV_ROUND_CLOSEST(port->uartclk * 8, baud);
1851 else
1852 quot = DIV_ROUND_CLOSEST(port->uartclk * 4, baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853
1854 switch (termios->c_cflag & CSIZE) {
1855 case CS5:
1856 lcr_h = UART01x_LCRH_WLEN_5;
1857 break;
1858 case CS6:
1859 lcr_h = UART01x_LCRH_WLEN_6;
1860 break;
1861 case CS7:
1862 lcr_h = UART01x_LCRH_WLEN_7;
1863 break;
1864 default: // CS8
1865 lcr_h = UART01x_LCRH_WLEN_8;
1866 break;
1867 }
1868 if (termios->c_cflag & CSTOPB)
1869 lcr_h |= UART01x_LCRH_STP2;
1870 if (termios->c_cflag & PARENB) {
1871 lcr_h |= UART01x_LCRH_PEN;
1872 if (!(termios->c_cflag & PARODD))
1873 lcr_h |= UART01x_LCRH_EPS;
1874 }
Russell Kingffca2b12010-12-22 17:13:05 +00001875 if (uap->fifosize > 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 lcr_h |= UART01x_LCRH_FEN;
1877
1878 spin_lock_irqsave(&port->lock, flags);
1879
1880 /*
1881 * Update the per-port timeout.
1882 */
1883 uart_update_timeout(port, termios->c_cflag, baud);
1884
Andre Przywaraef5a9352015-05-21 17:26:17 +01001885 pl011_setup_status_masks(port, termios);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886
1887 if (UART_ENABLE_MS(port, termios->c_cflag))
1888 pl011_enable_ms(port);
1889
1890 /* first, disable everything */
Jun Nie534e14e2015-07-31 15:49:15 +08001891 old_cr = readw(port->membase + REG_CR);
1892 writew(0, port->membase + REG_CR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893
Rabin Vincent3b438162010-02-12 06:43:11 +01001894 if (termios->c_cflag & CRTSCTS) {
1895 if (old_cr & UART011_CR_RTS)
1896 old_cr |= UART011_CR_RTSEN;
1897
1898 old_cr |= UART011_CR_CTSEN;
1899 uap->autorts = true;
1900 } else {
1901 old_cr &= ~(UART011_CR_CTSEN | UART011_CR_RTSEN);
1902 uap->autorts = false;
1903 }
1904
Russell Kingc19f12b2010-12-22 17:48:26 +00001905 if (uap->vendor->oversampling) {
1906 if (baud > port->uartclk / 16)
Linus Walleijac3e3fb2010-06-02 20:40:22 +01001907 old_cr |= ST_UART011_CR_OVSFACT;
1908 else
1909 old_cr &= ~ST_UART011_CR_OVSFACT;
1910 }
1911
Linus Walleijc5dd5532012-09-26 17:21:36 +02001912 /*
1913 * Workaround for the ST Micro oversampling variants to
1914 * increase the bitrate slightly, by lowering the divisor,
1915 * to avoid delayed sampling of start bit at high speeds,
1916 * else we see data corruption.
1917 */
1918 if (uap->vendor->oversampling) {
1919 if ((baud >= 3000000) && (baud < 3250000) && (quot > 1))
1920 quot -= 1;
1921 else if ((baud > 3250000) && (quot > 2))
1922 quot -= 2;
1923 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 /* Set baud rate */
Jun Nie534e14e2015-07-31 15:49:15 +08001925 writew(quot & 0x3f, port->membase + REG_FBRD);
1926 writew(quot >> 6, port->membase + REG_IBRD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927
1928 /*
1929 * ----------v----------v----------v----------v-----
Linus Walleijc5dd5532012-09-26 17:21:36 +02001930 * NOTE: lcrh_tx and lcrh_rx MUST BE WRITTEN AFTER
Jun Nie534e14e2015-07-31 15:49:15 +08001931 * REG_FBRD & REG_IBRD.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 * ----------^----------^----------^----------^-----
1933 */
Jon Medhurstb60f2f62013-12-10 10:18:59 +00001934 pl011_write_lcr_h(uap, lcr_h);
Jun Nie534e14e2015-07-31 15:49:15 +08001935 writew(old_cr, port->membase + REG_CR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936
1937 spin_unlock_irqrestore(&port->lock, flags);
1938}
1939
Andre Przywara0dd1e242015-05-21 17:26:23 +01001940static void
1941sbsa_uart_set_termios(struct uart_port *port, struct ktermios *termios,
1942 struct ktermios *old)
1943{
1944 struct uart_amba_port *uap =
1945 container_of(port, struct uart_amba_port, port);
1946 unsigned long flags;
1947
1948 tty_termios_encode_baud_rate(termios, uap->fixed_baud, uap->fixed_baud);
1949
1950 /* The SBSA UART only supports 8n1 without hardware flow control. */
1951 termios->c_cflag &= ~(CSIZE | CSTOPB | PARENB | PARODD);
1952 termios->c_cflag &= ~(CMSPAR | CRTSCTS);
1953 termios->c_cflag |= CS8 | CLOCAL;
1954
1955 spin_lock_irqsave(&port->lock, flags);
1956 uart_update_timeout(port, CS8, uap->fixed_baud);
1957 pl011_setup_status_masks(port, termios);
1958 spin_unlock_irqrestore(&port->lock, flags);
1959}
1960
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961static const char *pl011_type(struct uart_port *port)
1962{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001963 struct uart_amba_port *uap =
1964 container_of(port, struct uart_amba_port, port);
Russell Kinge8a7ba82010-12-28 09:16:54 +00001965 return uap->port.type == PORT_AMBA ? uap->type : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966}
1967
1968/*
1969 * Release the memory region(s) being used by 'port'
1970 */
Linus Walleije643f872012-06-17 15:44:19 +02001971static void pl011_release_port(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972{
1973 release_mem_region(port->mapbase, SZ_4K);
1974}
1975
1976/*
1977 * Request the memory region(s) being used by 'port'
1978 */
Linus Walleije643f872012-06-17 15:44:19 +02001979static int pl011_request_port(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980{
1981 return request_mem_region(port->mapbase, SZ_4K, "uart-pl011")
1982 != NULL ? 0 : -EBUSY;
1983}
1984
1985/*
1986 * Configure/autoconfigure the port.
1987 */
Linus Walleije643f872012-06-17 15:44:19 +02001988static void pl011_config_port(struct uart_port *port, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989{
1990 if (flags & UART_CONFIG_TYPE) {
1991 port->type = PORT_AMBA;
Linus Walleije643f872012-06-17 15:44:19 +02001992 pl011_request_port(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 }
1994}
1995
1996/*
1997 * verify the new serial_struct (for TIOCSSERIAL).
1998 */
Linus Walleije643f872012-06-17 15:44:19 +02001999static int pl011_verify_port(struct uart_port *port, struct serial_struct *ser)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000{
2001 int ret = 0;
2002 if (ser->type != PORT_UNKNOWN && ser->type != PORT_AMBA)
2003 ret = -EINVAL;
Yinghai Lua62c4132008-08-19 20:49:55 -07002004 if (ser->irq < 0 || ser->irq >= nr_irqs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 ret = -EINVAL;
2006 if (ser->baud_base < 9600)
2007 ret = -EINVAL;
2008 return ret;
2009}
2010
2011static struct uart_ops amba_pl011_pops = {
Linus Walleije643f872012-06-17 15:44:19 +02002012 .tx_empty = pl011_tx_empty,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 .set_mctrl = pl011_set_mctrl,
Linus Walleije643f872012-06-17 15:44:19 +02002014 .get_mctrl = pl011_get_mctrl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 .stop_tx = pl011_stop_tx,
2016 .start_tx = pl011_start_tx,
2017 .stop_rx = pl011_stop_rx,
2018 .enable_ms = pl011_enable_ms,
2019 .break_ctl = pl011_break_ctl,
2020 .startup = pl011_startup,
2021 .shutdown = pl011_shutdown,
Russell King68b65f72010-12-22 17:24:39 +00002022 .flush_buffer = pl011_dma_flush_buffer,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 .set_termios = pl011_set_termios,
2024 .type = pl011_type,
Linus Walleije643f872012-06-17 15:44:19 +02002025 .release_port = pl011_release_port,
2026 .request_port = pl011_request_port,
2027 .config_port = pl011_config_port,
2028 .verify_port = pl011_verify_port,
Jason Wessel84b5ae12008-02-20 13:33:39 -06002029#ifdef CONFIG_CONSOLE_POLL
Anton Vorontsovb3564c22012-09-24 14:27:54 -07002030 .poll_init = pl011_hwinit,
Linus Walleije643f872012-06-17 15:44:19 +02002031 .poll_get_char = pl011_get_poll_char,
2032 .poll_put_char = pl011_put_poll_char,
Jason Wessel84b5ae12008-02-20 13:33:39 -06002033#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034};
2035
Andre Przywara0dd1e242015-05-21 17:26:23 +01002036static void sbsa_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
2037{
2038}
2039
2040static unsigned int sbsa_uart_get_mctrl(struct uart_port *port)
2041{
2042 return 0;
2043}
2044
2045static const struct uart_ops sbsa_uart_pops = {
2046 .tx_empty = pl011_tx_empty,
2047 .set_mctrl = sbsa_uart_set_mctrl,
2048 .get_mctrl = sbsa_uart_get_mctrl,
2049 .stop_tx = pl011_stop_tx,
2050 .start_tx = pl011_start_tx,
2051 .stop_rx = pl011_stop_rx,
2052 .startup = sbsa_uart_startup,
2053 .shutdown = sbsa_uart_shutdown,
2054 .set_termios = sbsa_uart_set_termios,
2055 .type = pl011_type,
2056 .release_port = pl011_release_port,
2057 .request_port = pl011_request_port,
2058 .config_port = pl011_config_port,
2059 .verify_port = pl011_verify_port,
2060#ifdef CONFIG_CONSOLE_POLL
2061 .poll_init = pl011_hwinit,
2062 .poll_get_char = pl011_get_poll_char,
2063 .poll_put_char = pl011_put_poll_char,
2064#endif
2065};
2066
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067static struct uart_amba_port *amba_ports[UART_NR];
2068
2069#ifdef CONFIG_SERIAL_AMBA_PL011_CONSOLE
2070
Russell Kingd3587882006-03-20 20:00:09 +00002071static void pl011_console_putchar(struct uart_port *port, int ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072{
Daniel Thompsona5820c22014-09-03 12:51:55 +01002073 struct uart_amba_port *uap =
2074 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075
Jun Nie534e14e2015-07-31 15:49:15 +08002076 while (readw(uap->port.membase + REG_FR) & UART01x_FR_TXFF)
Russell Kingd3587882006-03-20 20:00:09 +00002077 barrier();
Jun Nie534e14e2015-07-31 15:49:15 +08002078 writew(ch, uap->port.membase + REG_DR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079}
2080
2081static void
2082pl011_console_write(struct console *co, const char *s, unsigned int count)
2083{
2084 struct uart_amba_port *uap = amba_ports[co->index];
Andre Przywara71eec482015-05-21 17:26:21 +01002085 unsigned int status, old_cr = 0, new_cr;
Rabin Vincentef605fd2012-01-17 11:52:28 +01002086 unsigned long flags;
2087 int locked = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088
2089 clk_enable(uap->clk);
2090
Rabin Vincentef605fd2012-01-17 11:52:28 +01002091 local_irq_save(flags);
2092 if (uap->port.sysrq)
2093 locked = 0;
2094 else if (oops_in_progress)
2095 locked = spin_trylock(&uap->port.lock);
2096 else
2097 spin_lock(&uap->port.lock);
2098
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 /*
2100 * First save the CR then disable the interrupts
2101 */
Andre Przywara71eec482015-05-21 17:26:21 +01002102 if (!uap->vendor->always_enabled) {
Jun Nie534e14e2015-07-31 15:49:15 +08002103 old_cr = readw(uap->port.membase + REG_CR);
Andre Przywara71eec482015-05-21 17:26:21 +01002104 new_cr = old_cr & ~UART011_CR_CTSEN;
2105 new_cr |= UART01x_CR_UARTEN | UART011_CR_TXE;
Jun Nie534e14e2015-07-31 15:49:15 +08002106 writew(new_cr, uap->port.membase + REG_CR);
Andre Przywara71eec482015-05-21 17:26:21 +01002107 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108
Russell Kingd3587882006-03-20 20:00:09 +00002109 uart_console_write(&uap->port, s, count, pl011_console_putchar);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110
2111 /*
2112 * Finally, wait for transmitter to become empty
2113 * and restore the TCR
2114 */
2115 do {
Jun Nie534e14e2015-07-31 15:49:15 +08002116 status = readw(uap->port.membase + REG_FR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 } while (status & UART01x_FR_BUSY);
Andre Przywara71eec482015-05-21 17:26:21 +01002118 if (!uap->vendor->always_enabled)
Jun Nie534e14e2015-07-31 15:49:15 +08002119 writew(old_cr, uap->port.membase + REG_CR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120
Rabin Vincentef605fd2012-01-17 11:52:28 +01002121 if (locked)
2122 spin_unlock(&uap->port.lock);
2123 local_irq_restore(flags);
2124
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 clk_disable(uap->clk);
2126}
2127
2128static void __init
2129pl011_console_get_options(struct uart_amba_port *uap, int *baud,
2130 int *parity, int *bits)
2131{
Jun Nie534e14e2015-07-31 15:49:15 +08002132 if (readw(uap->port.membase + REG_CR) & UART01x_CR_UARTEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 unsigned int lcr_h, ibrd, fbrd;
2134
Linus Walleijec489aa2010-06-02 08:13:52 +01002135 lcr_h = readw(uap->port.membase + uap->lcrh_tx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136
2137 *parity = 'n';
2138 if (lcr_h & UART01x_LCRH_PEN) {
2139 if (lcr_h & UART01x_LCRH_EPS)
2140 *parity = 'e';
2141 else
2142 *parity = 'o';
2143 }
2144
2145 if ((lcr_h & 0x60) == UART01x_LCRH_WLEN_7)
2146 *bits = 7;
2147 else
2148 *bits = 8;
2149
Jun Nie534e14e2015-07-31 15:49:15 +08002150 ibrd = readw(uap->port.membase + REG_IBRD);
2151 fbrd = readw(uap->port.membase + REG_FBRD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152
2153 *baud = uap->port.uartclk * 4 / (64 * ibrd + fbrd);
Linus Walleijac3e3fb2010-06-02 20:40:22 +01002154
Russell Kingc19f12b2010-12-22 17:48:26 +00002155 if (uap->vendor->oversampling) {
Jun Nie534e14e2015-07-31 15:49:15 +08002156 if (readw(uap->port.membase + REG_CR)
Linus Walleijac3e3fb2010-06-02 20:40:22 +01002157 & ST_UART011_CR_OVSFACT)
2158 *baud *= 2;
2159 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 }
2161}
2162
2163static int __init pl011_console_setup(struct console *co, char *options)
2164{
2165 struct uart_amba_port *uap;
2166 int baud = 38400;
2167 int bits = 8;
2168 int parity = 'n';
2169 int flow = 'n';
Russell King4b4851c2011-09-22 11:35:30 +01002170 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171
2172 /*
2173 * Check whether an invalid uart number has been specified, and
2174 * if so, search for the first available port that does have
2175 * console support.
2176 */
2177 if (co->index >= UART_NR)
2178 co->index = 0;
2179 uap = amba_ports[co->index];
Russell Kingd28122a2007-01-22 18:59:42 +00002180 if (!uap)
2181 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182
Linus Walleij78d80c52012-05-23 21:18:46 +02002183 /* Allow pins to be muxed in and configured */
Linus Walleij2b996fc2013-06-05 15:36:42 +02002184 pinctrl_pm_select_default_state(uap->port.dev);
Linus Walleij78d80c52012-05-23 21:18:46 +02002185
Russell King4b4851c2011-09-22 11:35:30 +01002186 ret = clk_prepare(uap->clk);
2187 if (ret)
2188 return ret;
2189
Jingoo Han574de552013-07-30 17:06:57 +09002190 if (dev_get_platdata(uap->port.dev)) {
Shreshtha Kumar Sahuc16d51a2011-06-13 10:11:33 +02002191 struct amba_pl011_data *plat;
2192
Jingoo Han574de552013-07-30 17:06:57 +09002193 plat = dev_get_platdata(uap->port.dev);
Shreshtha Kumar Sahuc16d51a2011-06-13 10:11:33 +02002194 if (plat->init)
2195 plat->init();
2196 }
2197
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 uap->port.uartclk = clk_get_rate(uap->clk);
2199
Andre Przywaracefc2d12015-05-21 17:26:22 +01002200 if (uap->vendor->fixed_options) {
2201 baud = uap->fixed_baud;
2202 } else {
2203 if (options)
2204 uart_parse_options(options,
2205 &baud, &parity, &bits, &flow);
2206 else
2207 pl011_console_get_options(uap, &baud, &parity, &bits);
2208 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209
2210 return uart_set_options(&uap->port, co, baud, parity, bits, flow);
2211}
2212
Vincent Sanders2d934862005-09-14 22:36:03 +01002213static struct uart_driver amba_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214static struct console amba_console = {
2215 .name = "ttyAMA",
2216 .write = pl011_console_write,
2217 .device = uart_console_device,
2218 .setup = pl011_console_setup,
2219 .flags = CON_PRINTBUFFER,
2220 .index = -1,
2221 .data = &amba_reg,
2222};
2223
2224#define AMBA_CONSOLE (&amba_console)
Rob Herring0d3c6732014-04-18 17:19:57 -05002225
2226static void pl011_putc(struct uart_port *port, int c)
2227{
Jun Nie534e14e2015-07-31 15:49:15 +08002228 while (readl(port->membase + REG_FR) & UART01x_FR_TXFF)
Rob Herring0d3c6732014-04-18 17:19:57 -05002229 ;
Jun Nie534e14e2015-07-31 15:49:15 +08002230 writeb(c, port->membase + REG_DR);
2231 while (readl(port->membase + REG_FR) & UART01x_FR_BUSY)
Rob Herring0d3c6732014-04-18 17:19:57 -05002232 ;
2233}
2234
2235static void pl011_early_write(struct console *con, const char *s, unsigned n)
2236{
2237 struct earlycon_device *dev = con->data;
2238
2239 uart_console_write(&dev->port, s, n, pl011_putc);
2240}
2241
2242static int __init pl011_early_console_setup(struct earlycon_device *device,
2243 const char *opt)
2244{
2245 if (!device->port.membase)
2246 return -ENODEV;
2247
2248 device->con->write = pl011_early_write;
2249 return 0;
2250}
2251EARLYCON_DECLARE(pl011, pl011_early_console_setup);
Rob Herring45e0f0f2014-03-27 08:08:03 -05002252OF_EARLYCON_DECLARE(pl011, "arm,pl011", pl011_early_console_setup);
Rob Herring0d3c6732014-04-18 17:19:57 -05002253
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254#else
2255#define AMBA_CONSOLE NULL
2256#endif
2257
2258static struct uart_driver amba_reg = {
2259 .owner = THIS_MODULE,
2260 .driver_name = "ttyAMA",
2261 .dev_name = "ttyAMA",
2262 .major = SERIAL_AMBA_MAJOR,
2263 .minor = SERIAL_AMBA_MINOR,
2264 .nr = UART_NR,
2265 .cons = AMBA_CONSOLE,
2266};
2267
Matthew Leach32614aa2012-08-28 16:41:28 +01002268static int pl011_probe_dt_alias(int index, struct device *dev)
2269{
2270 struct device_node *np;
2271 static bool seen_dev_with_alias = false;
2272 static bool seen_dev_without_alias = false;
2273 int ret = index;
2274
2275 if (!IS_ENABLED(CONFIG_OF))
2276 return ret;
2277
2278 np = dev->of_node;
2279 if (!np)
2280 return ret;
2281
2282 ret = of_alias_get_id(np, "serial");
2283 if (IS_ERR_VALUE(ret)) {
2284 seen_dev_without_alias = true;
2285 ret = index;
2286 } else {
2287 seen_dev_with_alias = true;
2288 if (ret >= ARRAY_SIZE(amba_ports) || amba_ports[ret] != NULL) {
2289 dev_warn(dev, "requested serial port %d not available.\n", ret);
2290 ret = index;
2291 }
2292 }
2293
2294 if (seen_dev_with_alias && seen_dev_without_alias)
2295 dev_warn(dev, "aliased and non-aliased serial devices found in device tree. Serial port enumeration may be unpredictable.\n");
2296
2297 return ret;
2298}
2299
Andre Przywara49bb3c82015-05-21 17:26:14 +01002300/* unregisters the driver also if no more ports are left */
2301static void pl011_unregister_port(struct uart_amba_port *uap)
2302{
2303 int i;
2304 bool busy = false;
2305
2306 for (i = 0; i < ARRAY_SIZE(amba_ports); i++) {
2307 if (amba_ports[i] == uap)
2308 amba_ports[i] = NULL;
2309 else if (amba_ports[i])
2310 busy = true;
2311 }
2312 pl011_dma_remove(uap);
2313 if (!busy)
2314 uart_unregister_driver(&amba_reg);
2315}
2316
Andre Przywara3873e2d2015-05-21 17:26:18 +01002317static int pl011_find_free_port(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318{
Andre Przywara3873e2d2015-05-21 17:26:18 +01002319 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320
2321 for (i = 0; i < ARRAY_SIZE(amba_ports); i++)
2322 if (amba_ports[i] == NULL)
Andre Przywara3873e2d2015-05-21 17:26:18 +01002323 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324
Andre Przywara3873e2d2015-05-21 17:26:18 +01002325 return -EBUSY;
2326}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327
Andre Przywara3873e2d2015-05-21 17:26:18 +01002328static int pl011_setup_port(struct device *dev, struct uart_amba_port *uap,
2329 struct resource *mmiobase, int index)
2330{
2331 void __iomem *base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332
Andre Przywara3873e2d2015-05-21 17:26:18 +01002333 base = devm_ioremap_resource(dev, mmiobase);
Krzysztof Kozlowski97a60ea2015-07-09 22:21:41 +09002334 if (IS_ERR(base))
2335 return PTR_ERR(base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336
Andre Przywara3873e2d2015-05-21 17:26:18 +01002337 index = pl011_probe_dt_alias(index, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338
Shreshtha Kumar Sahud8d8ffa2012-01-18 15:53:59 +05302339 uap->old_cr = 0;
Andre Przywara3873e2d2015-05-21 17:26:18 +01002340 uap->port.dev = dev;
2341 uap->port.mapbase = mmiobase->start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 uap->port.membase = base;
2343 uap->port.iotype = UPIO_MEM;
Russell Kingffca2b12010-12-22 17:13:05 +00002344 uap->port.fifosize = uap->fifosize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 uap->port.flags = UPF_BOOT_AUTOCONF;
Andre Przywara3873e2d2015-05-21 17:26:18 +01002346 uap->port.line = index;
2347
2348 amba_ports[index] = uap;
2349
2350 return 0;
2351}
2352
2353static int pl011_register_port(struct uart_amba_port *uap)
2354{
2355 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356
Linus Walleijc3d8b762012-03-21 20:15:18 +01002357 /* Ensure interrupts from this UART are masked and cleared */
Jun Nie534e14e2015-07-31 15:49:15 +08002358 writew(0, uap->port.membase + REG_IMSC);
2359 writew(0xffff, uap->port.membase + REG_ICR);
Linus Walleijc3d8b762012-03-21 20:15:18 +01002360
Tushar Beheraef2889f2014-01-20 14:32:35 +05302361 if (!amba_reg.state) {
2362 ret = uart_register_driver(&amba_reg);
2363 if (ret < 0) {
Andre Przywara3873e2d2015-05-21 17:26:18 +01002364 dev_err(uap->port.dev,
Jorge Ramirez-Ortiz1c9be312015-03-06 13:05:40 -05002365 "Failed to register AMBA-PL011 driver\n");
Tushar Beheraef2889f2014-01-20 14:32:35 +05302366 return ret;
2367 }
2368 }
2369
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 ret = uart_add_one_port(&amba_reg, &uap->port);
Andre Przywara49bb3c82015-05-21 17:26:14 +01002371 if (ret)
2372 pl011_unregister_port(uap);
Tushar Behera7f6d9422014-06-26 15:35:35 +05302373
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 return ret;
2375}
2376
Andre Przywara3873e2d2015-05-21 17:26:18 +01002377static int pl011_probe(struct amba_device *dev, const struct amba_id *id)
2378{
2379 struct uart_amba_port *uap;
2380 struct vendor_data *vendor = id->data;
2381 int portnr, ret;
2382
2383 portnr = pl011_find_free_port();
2384 if (portnr < 0)
2385 return portnr;
2386
2387 uap = devm_kzalloc(&dev->dev, sizeof(struct uart_amba_port),
2388 GFP_KERNEL);
2389 if (!uap)
2390 return -ENOMEM;
2391
2392 uap->clk = devm_clk_get(&dev->dev, NULL);
2393 if (IS_ERR(uap->clk))
2394 return PTR_ERR(uap->clk);
2395
2396 uap->vendor = vendor;
2397 uap->lcrh_rx = vendor->lcrh_rx;
2398 uap->lcrh_tx = vendor->lcrh_tx;
2399 uap->fifosize = vendor->get_fifosize(dev);
2400 uap->port.irq = dev->irq[0];
2401 uap->port.ops = &amba_pl011_pops;
2402
2403 snprintf(uap->type, sizeof(uap->type), "PL011 rev%u", amba_rev(dev));
2404
2405 ret = pl011_setup_port(&dev->dev, uap, &dev->res, portnr);
2406 if (ret)
2407 return ret;
2408
2409 amba_set_drvdata(dev, uap);
2410
2411 return pl011_register_port(uap);
2412}
2413
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414static int pl011_remove(struct amba_device *dev)
2415{
2416 struct uart_amba_port *uap = amba_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 uart_remove_one_port(&amba_reg, &uap->port);
Andre Przywara49bb3c82015-05-21 17:26:14 +01002419 pl011_unregister_port(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 return 0;
2421}
2422
Ulf Hanssond0ce8502013-12-03 11:04:28 +01002423#ifdef CONFIG_PM_SLEEP
2424static int pl011_suspend(struct device *dev)
Leo Chenb736b892009-07-28 23:43:33 +01002425{
Ulf Hanssond0ce8502013-12-03 11:04:28 +01002426 struct uart_amba_port *uap = dev_get_drvdata(dev);
Leo Chenb736b892009-07-28 23:43:33 +01002427
2428 if (!uap)
2429 return -EINVAL;
2430
2431 return uart_suspend_port(&amba_reg, &uap->port);
2432}
2433
Ulf Hanssond0ce8502013-12-03 11:04:28 +01002434static int pl011_resume(struct device *dev)
Leo Chenb736b892009-07-28 23:43:33 +01002435{
Ulf Hanssond0ce8502013-12-03 11:04:28 +01002436 struct uart_amba_port *uap = dev_get_drvdata(dev);
Leo Chenb736b892009-07-28 23:43:33 +01002437
2438 if (!uap)
2439 return -EINVAL;
2440
2441 return uart_resume_port(&amba_reg, &uap->port);
2442}
2443#endif
2444
Ulf Hanssond0ce8502013-12-03 11:04:28 +01002445static SIMPLE_DEV_PM_OPS(pl011_dev_pm_ops, pl011_suspend, pl011_resume);
2446
Andre Przywara0dd1e242015-05-21 17:26:23 +01002447static int sbsa_uart_probe(struct platform_device *pdev)
2448{
2449 struct uart_amba_port *uap;
2450 struct resource *r;
2451 int portnr, ret;
2452 int baudrate;
2453
2454 /*
2455 * Check the mandatory baud rate parameter in the DT node early
2456 * so that we can easily exit with the error.
2457 */
2458 if (pdev->dev.of_node) {
2459 struct device_node *np = pdev->dev.of_node;
2460
2461 ret = of_property_read_u32(np, "current-speed", &baudrate);
2462 if (ret)
2463 return ret;
2464 } else {
2465 baudrate = 115200;
2466 }
2467
2468 portnr = pl011_find_free_port();
2469 if (portnr < 0)
2470 return portnr;
2471
2472 uap = devm_kzalloc(&pdev->dev, sizeof(struct uart_amba_port),
2473 GFP_KERNEL);
2474 if (!uap)
2475 return -ENOMEM;
2476
2477 uap->vendor = &vendor_sbsa;
2478 uap->fifosize = 32;
2479 uap->port.irq = platform_get_irq(pdev, 0);
2480 uap->port.ops = &sbsa_uart_pops;
2481 uap->fixed_baud = baudrate;
2482
2483 snprintf(uap->type, sizeof(uap->type), "SBSA");
2484
2485 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2486
2487 ret = pl011_setup_port(&pdev->dev, uap, r, portnr);
2488 if (ret)
2489 return ret;
2490
2491 platform_set_drvdata(pdev, uap);
2492
2493 return pl011_register_port(uap);
2494}
2495
2496static int sbsa_uart_remove(struct platform_device *pdev)
2497{
2498 struct uart_amba_port *uap = platform_get_drvdata(pdev);
2499
2500 uart_remove_one_port(&amba_reg, &uap->port);
2501 pl011_unregister_port(uap);
2502 return 0;
2503}
2504
2505static const struct of_device_id sbsa_uart_of_match[] = {
2506 { .compatible = "arm,sbsa-uart", },
2507 {},
2508};
2509MODULE_DEVICE_TABLE(of, sbsa_uart_of_match);
2510
Graeme Gregory3db9ab02015-05-21 17:26:24 +01002511static const struct acpi_device_id sbsa_uart_acpi_match[] = {
2512 { "ARMH0011", 0 },
2513 {},
2514};
2515MODULE_DEVICE_TABLE(acpi, sbsa_uart_acpi_match);
2516
Andre Przywara0dd1e242015-05-21 17:26:23 +01002517static struct platform_driver arm_sbsa_uart_platform_driver = {
2518 .probe = sbsa_uart_probe,
2519 .remove = sbsa_uart_remove,
2520 .driver = {
2521 .name = "sbsa-uart",
2522 .of_match_table = of_match_ptr(sbsa_uart_of_match),
Graeme Gregory3db9ab02015-05-21 17:26:24 +01002523 .acpi_match_table = ACPI_PTR(sbsa_uart_acpi_match),
Andre Przywara0dd1e242015-05-21 17:26:23 +01002524 },
2525};
2526
Russell King2c39c9e2010-07-27 08:50:16 +01002527static struct amba_id pl011_ids[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528 {
2529 .id = 0x00041011,
2530 .mask = 0x000fffff,
Alessandro Rubini5926a292009-06-04 17:43:04 +01002531 .data = &vendor_arm,
2532 },
2533 {
2534 .id = 0x00380802,
2535 .mask = 0x00ffffff,
2536 .data = &vendor_st,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537 },
2538 { 0, 0 },
2539};
2540
Dave Martin60f7a332011-10-05 15:15:22 +01002541MODULE_DEVICE_TABLE(amba, pl011_ids);
2542
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543static struct amba_driver pl011_driver = {
2544 .drv = {
2545 .name = "uart-pl011",
Ulf Hanssond0ce8502013-12-03 11:04:28 +01002546 .pm = &pl011_dev_pm_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547 },
2548 .id_table = pl011_ids,
2549 .probe = pl011_probe,
2550 .remove = pl011_remove,
2551};
2552
2553static int __init pl011_init(void)
2554{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555 printk(KERN_INFO "Serial: AMBA PL011 UART driver\n");
2556
Andre Przywara0dd1e242015-05-21 17:26:23 +01002557 if (platform_driver_register(&arm_sbsa_uart_platform_driver))
2558 pr_warn("could not register SBSA UART platform driver\n");
Tushar Beheraef2889f2014-01-20 14:32:35 +05302559 return amba_driver_register(&pl011_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560}
2561
2562static void __exit pl011_exit(void)
2563{
Andre Przywara0dd1e242015-05-21 17:26:23 +01002564 platform_driver_unregister(&arm_sbsa_uart_platform_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565 amba_driver_unregister(&pl011_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566}
2567
Alessandro Rubini4dd9e742009-05-05 05:54:13 +01002568/*
2569 * While this can be a module, if builtin it's most likely the console
2570 * So let's leave module_exit but move module_init to an earlier place
2571 */
2572arch_initcall(pl011_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573module_exit(pl011_exit);
2574
2575MODULE_AUTHOR("ARM Ltd/Deep Blue Solutions Ltd");
2576MODULE_DESCRIPTION("ARM AMBA serial port driver");
2577MODULE_LICENSE("GPL");