blob: 29526a1d39df623f99610dcd32602b998fee771e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Driver for AMBA serial ports
3 *
4 * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
5 *
6 * Copyright 1999 ARM Limited
7 * Copyright (C) 2000 Deep Blue Solutions Ltd.
Russell King68b65f72010-12-22 17:24:39 +00008 * Copyright (C) 2010 ST-Ericsson SA
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 * This is a generic driver for ARM AMBA-type serial ports. They
25 * have a lot of 16550-like features, but are not register compatible.
26 * Note that although they do have CTS, DCD and DSR inputs, they do
27 * not have an RI input, nor do they have DTR or RTS outputs. If
28 * required, these have to be supplied via some other means (eg, GPIO)
29 * and hooked into this driver.
30 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Chanho Mincb06ff12013-03-27 18:38:11 +090032
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#if defined(CONFIG_SERIAL_AMBA_PL011_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
34#define SUPPORT_SYSRQ
35#endif
36
37#include <linux/module.h>
38#include <linux/ioport.h>
39#include <linux/init.h>
40#include <linux/console.h>
41#include <linux/sysrq.h>
42#include <linux/device.h>
43#include <linux/tty.h>
44#include <linux/tty_flip.h>
45#include <linux/serial_core.h>
46#include <linux/serial.h>
Russell Kinga62c80e2006-01-07 13:52:45 +000047#include <linux/amba/bus.h>
48#include <linux/amba/serial.h>
Russell Kingf8ce2542006-01-07 16:15:52 +000049#include <linux/clk.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090050#include <linux/slab.h>
Russell King68b65f72010-12-22 17:24:39 +000051#include <linux/dmaengine.h>
52#include <linux/dma-mapping.h>
53#include <linux/scatterlist.h>
Shreshtha Kumar Sahuc16d51a2011-06-13 10:11:33 +020054#include <linux/delay.h>
Viresh Kumar258aea72012-02-01 16:12:19 +053055#include <linux/types.h>
Matthew Leach32614aa2012-08-28 16:41:28 +010056#include <linux/of.h>
57#include <linux/of_device.h>
Shawn Guo258e0552012-05-06 22:53:35 +080058#include <linux/pinctrl/consumer.h>
Alessandro Rubinicb707062012-06-24 12:46:37 +010059#include <linux/sizes.h>
Linus Walleijde609582012-10-15 13:36:01 +020060#include <linux/io.h>
Graeme Gregory3db9ab02015-05-21 17:26:24 +010061#include <linux/acpi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Russell King9f25bc52015-11-03 14:51:13 +000063#include "amba-pl011.h"
64
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#define UART_NR 14
66
67#define SERIAL_AMBA_MAJOR 204
68#define SERIAL_AMBA_MINOR 64
69#define SERIAL_AMBA_NR UART_NR
70
71#define AMBA_ISR_PASS_LIMIT 256
72
Russell Kingb63d4f02005-11-19 11:10:35 +000073#define UART_DR_ERROR (UART011_DR_OE|UART011_DR_BE|UART011_DR_PE|UART011_DR_FE)
74#define UART_DUMMY_DR_RX (1 << 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
Russell Kingdebb7f62015-11-16 17:40:26 +000076static u16 pl011_std_offsets[REG_ARRAY_SIZE] = {
77 [REG_DR] = UART01x_DR,
78 [REG_ST_DMAWM] = ST_UART011_DMAWM,
79 [REG_ST_TIMEOUT] = ST_UART011_TIMEOUT,
80 [REG_FR] = UART01x_FR,
81 [REG_ST_LCRH_RX] = ST_UART011_LCRH_RX,
82 [REG_IBRD] = UART011_IBRD,
83 [REG_FBRD] = UART011_FBRD,
84 [REG_LCRH] = UART011_LCRH,
85 [REG_ST_LCRH_TX] = ST_UART011_LCRH_TX,
86 [REG_CR] = UART011_CR,
87 [REG_IFLS] = UART011_IFLS,
88 [REG_IMSC] = UART011_IMSC,
89 [REG_RIS] = UART011_RIS,
90 [REG_MIS] = UART011_MIS,
91 [REG_ICR] = UART011_ICR,
92 [REG_DMACR] = UART011_DMACR,
93 [REG_ST_XFCR] = ST_UART011_XFCR,
94 [REG_ST_XON1] = ST_UART011_XON1,
95 [REG_ST_XON2] = ST_UART011_XON2,
96 [REG_ST_XOFF1] = ST_UART011_XOFF1,
97 [REG_ST_XOFF2] = ST_UART011_XOFF2,
98 [REG_ST_ITCR] = ST_UART011_ITCR,
99 [REG_ST_ITIP] = ST_UART011_ITIP,
100 [REG_ST_ABCR] = ST_UART011_ABCR,
101 [REG_ST_ABIMSC] = ST_UART011_ABIMSC,
102};
103
Alessandro Rubini5926a292009-06-04 17:43:04 +0100104/* There is by now at least one vendor with differing details, so handle it */
105struct vendor_data {
106 unsigned int ifls;
Linus Walleijec489aa2010-06-02 08:13:52 +0100107 unsigned int lcrh_tx;
108 unsigned int lcrh_rx;
Linus Walleijac3e3fb2010-06-02 20:40:22 +0100109 bool oversampling;
Russell King38d62432010-12-22 17:59:16 +0000110 bool dma_threshold;
Rajanikanth H.V4fd06902012-03-26 11:17:02 +0200111 bool cts_event_workaround;
Andre Przywara71eec482015-05-21 17:26:21 +0100112 bool always_enabled;
Andre Przywaracefc2d12015-05-21 17:26:22 +0100113 bool fixed_options;
Jongsung Kim78506f22013-04-15 14:45:25 +0900114
Jongsung Kimea336402013-05-10 18:05:35 +0900115 unsigned int (*get_fifosize)(struct amba_device *dev);
Alessandro Rubini5926a292009-06-04 17:43:04 +0100116};
117
Jongsung Kimea336402013-05-10 18:05:35 +0900118static unsigned int get_fifosize_arm(struct amba_device *dev)
Jongsung Kim78506f22013-04-15 14:45:25 +0900119{
Jongsung Kimea336402013-05-10 18:05:35 +0900120 return amba_rev(dev) < 3 ? 16 : 32;
Jongsung Kim78506f22013-04-15 14:45:25 +0900121}
122
Alessandro Rubini5926a292009-06-04 17:43:04 +0100123static struct vendor_data vendor_arm = {
124 .ifls = UART011_IFLS_RX4_8|UART011_IFLS_TX4_8,
Russell King9f25bc52015-11-03 14:51:13 +0000125 .lcrh_tx = REG_LCRH,
126 .lcrh_rx = REG_LCRH,
Linus Walleijac3e3fb2010-06-02 20:40:22 +0100127 .oversampling = false,
Russell King38d62432010-12-22 17:59:16 +0000128 .dma_threshold = false,
Rajanikanth H.V4fd06902012-03-26 11:17:02 +0200129 .cts_event_workaround = false,
Andre Przywara71eec482015-05-21 17:26:21 +0100130 .always_enabled = false,
Andre Przywaracefc2d12015-05-21 17:26:22 +0100131 .fixed_options = false,
Jongsung Kim78506f22013-04-15 14:45:25 +0900132 .get_fifosize = get_fifosize_arm,
Alessandro Rubini5926a292009-06-04 17:43:04 +0100133};
134
Andre Przywara0dd1e242015-05-21 17:26:23 +0100135static struct vendor_data vendor_sbsa = {
136 .oversampling = false,
137 .dma_threshold = false,
138 .cts_event_workaround = false,
139 .always_enabled = true,
140 .fixed_options = true,
141};
142
Jongsung Kimea336402013-05-10 18:05:35 +0900143static unsigned int get_fifosize_st(struct amba_device *dev)
Jongsung Kim78506f22013-04-15 14:45:25 +0900144{
145 return 64;
146}
147
Alessandro Rubini5926a292009-06-04 17:43:04 +0100148static struct vendor_data vendor_st = {
149 .ifls = UART011_IFLS_RX_HALF|UART011_IFLS_TX_HALF,
Russell King9f25bc52015-11-03 14:51:13 +0000150 .lcrh_tx = REG_ST_LCRH_TX,
151 .lcrh_rx = REG_ST_LCRH_RX,
Linus Walleijac3e3fb2010-06-02 20:40:22 +0100152 .oversampling = true,
Russell King38d62432010-12-22 17:59:16 +0000153 .dma_threshold = true,
Rajanikanth H.V4fd06902012-03-26 11:17:02 +0200154 .cts_event_workaround = true,
Andre Przywara71eec482015-05-21 17:26:21 +0100155 .always_enabled = false,
Andre Przywaracefc2d12015-05-21 17:26:22 +0100156 .fixed_options = false,
Jongsung Kim78506f22013-04-15 14:45:25 +0900157 .get_fifosize = get_fifosize_st,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158};
159
Russell King68b65f72010-12-22 17:24:39 +0000160/* Deals with DMA transactions */
Linus Walleijead76f322011-02-24 13:21:08 +0100161
162struct pl011_sgbuf {
163 struct scatterlist sg;
164 char *buf;
165};
166
167struct pl011_dmarx_data {
168 struct dma_chan *chan;
169 struct completion complete;
170 bool use_buf_b;
171 struct pl011_sgbuf sgbuf_a;
172 struct pl011_sgbuf sgbuf_b;
173 dma_cookie_t cookie;
174 bool running;
Chanho Mincb06ff12013-03-27 18:38:11 +0900175 struct timer_list timer;
176 unsigned int last_residue;
177 unsigned long last_jiffies;
178 bool auto_poll_rate;
179 unsigned int poll_rate;
180 unsigned int poll_timeout;
Linus Walleijead76f322011-02-24 13:21:08 +0100181};
182
Russell King68b65f72010-12-22 17:24:39 +0000183struct pl011_dmatx_data {
184 struct dma_chan *chan;
185 struct scatterlist sg;
186 char *buf;
187 bool queued;
188};
189
Russell Kingc19f12b2010-12-22 17:48:26 +0000190/*
191 * We wrap our port structure around the generic uart_port.
192 */
193struct uart_amba_port {
194 struct uart_port port;
Russell Kingdebb7f62015-11-16 17:40:26 +0000195 const u16 *reg_offset;
Russell Kingc19f12b2010-12-22 17:48:26 +0000196 struct clk *clk;
197 const struct vendor_data *vendor;
Russell King68b65f72010-12-22 17:24:39 +0000198 unsigned int dmacr; /* dma control reg */
Russell Kingc19f12b2010-12-22 17:48:26 +0000199 unsigned int im; /* interrupt mask */
200 unsigned int old_status;
Russell Kingffca2b12010-12-22 17:13:05 +0000201 unsigned int fifosize; /* vendor-specific */
Russell Kingc19f12b2010-12-22 17:48:26 +0000202 unsigned int lcrh_tx; /* vendor-specific */
203 unsigned int lcrh_rx; /* vendor-specific */
Shreshtha Kumar Sahud8d8ffa2012-01-18 15:53:59 +0530204 unsigned int old_cr; /* state during shutdown */
Russell Kingc19f12b2010-12-22 17:48:26 +0000205 bool autorts;
Andre Przywaracefc2d12015-05-21 17:26:22 +0100206 unsigned int fixed_baud; /* vendor-set fixed baud rate */
Russell Kingc19f12b2010-12-22 17:48:26 +0000207 char type[12];
Russell King68b65f72010-12-22 17:24:39 +0000208#ifdef CONFIG_DMA_ENGINE
209 /* DMA stuff */
Linus Walleijead76f322011-02-24 13:21:08 +0100210 bool using_tx_dma;
211 bool using_rx_dma;
212 struct pl011_dmarx_data dmarx;
Russell King68b65f72010-12-22 17:24:39 +0000213 struct pl011_dmatx_data dmatx;
Jorge Ramirez-Ortiz1c9be312015-03-06 13:05:40 -0500214 bool dma_probed;
Russell King68b65f72010-12-22 17:24:39 +0000215#endif
Russell Kingc19f12b2010-12-22 17:48:26 +0000216};
217
Russell King9f25bc52015-11-03 14:51:13 +0000218static unsigned int pl011_reg_to_offset(const struct uart_amba_port *uap,
219 unsigned int reg)
220{
Russell Kingdebb7f62015-11-16 17:40:26 +0000221 return uap->reg_offset[reg];
Russell King9f25bc52015-11-03 14:51:13 +0000222}
223
Russell Kingb2a4e242015-11-03 14:51:03 +0000224static unsigned int pl011_read(const struct uart_amba_port *uap,
225 unsigned int reg)
Russell King75836332015-11-03 14:50:58 +0000226{
Russell King9f25bc52015-11-03 14:51:13 +0000227 return readw(uap->port.membase + pl011_reg_to_offset(uap, reg));
Russell King75836332015-11-03 14:50:58 +0000228}
229
Russell Kingb2a4e242015-11-03 14:51:03 +0000230static void pl011_write(unsigned int val, const struct uart_amba_port *uap,
231 unsigned int reg)
Russell King75836332015-11-03 14:50:58 +0000232{
Russell King9f25bc52015-11-03 14:51:13 +0000233 writew(val, uap->port.membase + pl011_reg_to_offset(uap, reg));
Russell King75836332015-11-03 14:50:58 +0000234}
235
Russell King68b65f72010-12-22 17:24:39 +0000236/*
Linus Walleij29772c42011-02-24 13:21:36 +0100237 * Reads up to 256 characters from the FIFO or until it's empty and
238 * inserts them into the TTY layer. Returns the number of characters
239 * read from the FIFO.
240 */
241static int pl011_fifo_to_tty(struct uart_amba_port *uap)
242{
Timur Tabi71a5cd82015-10-07 15:27:16 -0500243 u16 status;
244 unsigned int ch, flag, max_count = 256;
Linus Walleij29772c42011-02-24 13:21:36 +0100245 int fifotaken = 0;
246
247 while (max_count--) {
Russell King9f25bc52015-11-03 14:51:13 +0000248 status = pl011_read(uap, REG_FR);
Linus Walleij29772c42011-02-24 13:21:36 +0100249 if (status & UART01x_FR_RXFE)
250 break;
251
252 /* Take chars from the FIFO and update status */
Russell King9f25bc52015-11-03 14:51:13 +0000253 ch = pl011_read(uap, REG_DR) | UART_DUMMY_DR_RX;
Linus Walleij29772c42011-02-24 13:21:36 +0100254 flag = TTY_NORMAL;
255 uap->port.icount.rx++;
256 fifotaken++;
257
258 if (unlikely(ch & UART_DR_ERROR)) {
259 if (ch & UART011_DR_BE) {
260 ch &= ~(UART011_DR_FE | UART011_DR_PE);
261 uap->port.icount.brk++;
262 if (uart_handle_break(&uap->port))
263 continue;
264 } else if (ch & UART011_DR_PE)
265 uap->port.icount.parity++;
266 else if (ch & UART011_DR_FE)
267 uap->port.icount.frame++;
268 if (ch & UART011_DR_OE)
269 uap->port.icount.overrun++;
270
271 ch &= uap->port.read_status_mask;
272
273 if (ch & UART011_DR_BE)
274 flag = TTY_BREAK;
275 else if (ch & UART011_DR_PE)
276 flag = TTY_PARITY;
277 else if (ch & UART011_DR_FE)
278 flag = TTY_FRAME;
279 }
280
281 if (uart_handle_sysrq_char(&uap->port, ch & 255))
282 continue;
283
284 uart_insert_char(&uap->port, ch, UART011_DR_OE, ch, flag);
285 }
286
287 return fifotaken;
288}
289
290
291/*
Russell King68b65f72010-12-22 17:24:39 +0000292 * All the DMA operation mode stuff goes inside this ifdef.
293 * This assumes that you have a generic DMA device interface,
294 * no custom DMA interfaces are supported.
295 */
296#ifdef CONFIG_DMA_ENGINE
297
298#define PL011_DMA_BUFFER_SIZE PAGE_SIZE
299
Linus Walleijead76f322011-02-24 13:21:08 +0100300static int pl011_sgbuf_init(struct dma_chan *chan, struct pl011_sgbuf *sg,
301 enum dma_data_direction dir)
302{
Chanho Mincb06ff12013-03-27 18:38:11 +0900303 dma_addr_t dma_addr;
304
305 sg->buf = dma_alloc_coherent(chan->device->dev,
306 PL011_DMA_BUFFER_SIZE, &dma_addr, GFP_KERNEL);
Linus Walleijead76f322011-02-24 13:21:08 +0100307 if (!sg->buf)
308 return -ENOMEM;
309
Chanho Mincb06ff12013-03-27 18:38:11 +0900310 sg_init_table(&sg->sg, 1);
311 sg_set_page(&sg->sg, phys_to_page(dma_addr),
312 PL011_DMA_BUFFER_SIZE, offset_in_page(dma_addr));
313 sg_dma_address(&sg->sg) = dma_addr;
Andrew Jacksonc64be922014-11-07 14:14:43 +0000314 sg_dma_len(&sg->sg) = PL011_DMA_BUFFER_SIZE;
Linus Walleijead76f322011-02-24 13:21:08 +0100315
Linus Walleijead76f322011-02-24 13:21:08 +0100316 return 0;
317}
318
319static void pl011_sgbuf_free(struct dma_chan *chan, struct pl011_sgbuf *sg,
320 enum dma_data_direction dir)
321{
322 if (sg->buf) {
Chanho Mincb06ff12013-03-27 18:38:11 +0900323 dma_free_coherent(chan->device->dev,
324 PL011_DMA_BUFFER_SIZE, sg->buf,
325 sg_dma_address(&sg->sg));
Linus Walleijead76f322011-02-24 13:21:08 +0100326 }
327}
328
Jorge Ramirez-Ortiz1c9be312015-03-06 13:05:40 -0500329static void pl011_dma_probe(struct uart_amba_port *uap)
Russell King68b65f72010-12-22 17:24:39 +0000330{
331 /* DMA is the sole user of the platform data right now */
Jingoo Han574de552013-07-30 17:06:57 +0900332 struct amba_pl011_data *plat = dev_get_platdata(uap->port.dev);
Jorge Ramirez-Ortiz1c9be312015-03-06 13:05:40 -0500333 struct device *dev = uap->port.dev;
Russell King68b65f72010-12-22 17:24:39 +0000334 struct dma_slave_config tx_conf = {
Russell King9f25bc52015-11-03 14:51:13 +0000335 .dst_addr = uap->port.mapbase +
336 pl011_reg_to_offset(uap, REG_DR),
Russell King68b65f72010-12-22 17:24:39 +0000337 .dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE,
Vinod Koula485df42011-10-14 10:47:38 +0530338 .direction = DMA_MEM_TO_DEV,
Russell King68b65f72010-12-22 17:24:39 +0000339 .dst_maxburst = uap->fifosize >> 1,
Viresh Kumar258aea72012-02-01 16:12:19 +0530340 .device_fc = false,
Russell King68b65f72010-12-22 17:24:39 +0000341 };
342 struct dma_chan *chan;
343 dma_cap_mask_t mask;
344
Jorge Ramirez-Ortiz1c9be312015-03-06 13:05:40 -0500345 uap->dma_probed = true;
346 chan = dma_request_slave_channel_reason(dev, "tx");
347 if (IS_ERR(chan)) {
348 if (PTR_ERR(chan) == -EPROBE_DEFER) {
Jorge Ramirez-Ortiz1c9be312015-03-06 13:05:40 -0500349 uap->dma_probed = false;
350 return;
351 }
Russell King68b65f72010-12-22 17:24:39 +0000352
Arnd Bergmann787b0c12013-01-28 16:24:37 +0000353 /* We need platform data */
354 if (!plat || !plat->dma_filter) {
355 dev_info(uap->port.dev, "no DMA platform data\n");
356 return;
357 }
358
359 /* Try to acquire a generic DMA engine slave TX channel */
360 dma_cap_zero(mask);
361 dma_cap_set(DMA_SLAVE, mask);
362
363 chan = dma_request_channel(mask, plat->dma_filter,
364 plat->dma_tx_param);
365 if (!chan) {
366 dev_err(uap->port.dev, "no TX DMA channel!\n");
367 return;
368 }
Russell King68b65f72010-12-22 17:24:39 +0000369 }
370
371 dmaengine_slave_config(chan, &tx_conf);
372 uap->dmatx.chan = chan;
373
374 dev_info(uap->port.dev, "DMA channel TX %s\n",
375 dma_chan_name(uap->dmatx.chan));
Linus Walleijead76f322011-02-24 13:21:08 +0100376
377 /* Optionally make use of an RX channel as well */
Arnd Bergmann787b0c12013-01-28 16:24:37 +0000378 chan = dma_request_slave_channel(dev, "rx");
Rob Herring0d3c6732014-04-18 17:19:57 -0500379
Arnd Bergmann787b0c12013-01-28 16:24:37 +0000380 if (!chan && plat->dma_rx_param) {
381 chan = dma_request_channel(mask, plat->dma_filter, plat->dma_rx_param);
382
383 if (!chan) {
384 dev_err(uap->port.dev, "no RX DMA channel!\n");
385 return;
386 }
387 }
388
389 if (chan) {
Linus Walleijead76f322011-02-24 13:21:08 +0100390 struct dma_slave_config rx_conf = {
Russell King9f25bc52015-11-03 14:51:13 +0000391 .src_addr = uap->port.mapbase +
392 pl011_reg_to_offset(uap, REG_DR),
Linus Walleijead76f322011-02-24 13:21:08 +0100393 .src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE,
Vinod Koula485df42011-10-14 10:47:38 +0530394 .direction = DMA_DEV_TO_MEM,
Guennadi Liakhovetskib2aeb772014-04-12 19:47:17 +0200395 .src_maxburst = uap->fifosize >> 2,
Viresh Kumar258aea72012-02-01 16:12:19 +0530396 .device_fc = false,
Linus Walleijead76f322011-02-24 13:21:08 +0100397 };
Andrew Jackson2d3b7d62014-11-07 14:14:47 +0000398 struct dma_slave_caps caps;
Linus Walleijead76f322011-02-24 13:21:08 +0100399
Andrew Jackson2d3b7d62014-11-07 14:14:47 +0000400 /*
401 * Some DMA controllers provide information on their capabilities.
402 * If the controller does, check for suitable residue processing
403 * otherwise assime all is well.
404 */
405 if (0 == dma_get_slave_caps(chan, &caps)) {
406 if (caps.residue_granularity ==
407 DMA_RESIDUE_GRANULARITY_DESCRIPTOR) {
408 dma_release_channel(chan);
409 dev_info(uap->port.dev,
410 "RX DMA disabled - no residue processing\n");
411 return;
412 }
413 }
Linus Walleijead76f322011-02-24 13:21:08 +0100414 dmaengine_slave_config(chan, &rx_conf);
415 uap->dmarx.chan = chan;
416
Andrew Jackson98267d32014-11-07 14:14:23 +0000417 uap->dmarx.auto_poll_rate = false;
Greg Kroah-Hartman8f898bf2013-12-17 09:33:18 -0800418 if (plat && plat->dma_rx_poll_enable) {
Chanho Mincb06ff12013-03-27 18:38:11 +0900419 /* Set poll rate if specified. */
420 if (plat->dma_rx_poll_rate) {
421 uap->dmarx.auto_poll_rate = false;
422 uap->dmarx.poll_rate = plat->dma_rx_poll_rate;
423 } else {
424 /*
425 * 100 ms defaults to poll rate if not
426 * specified. This will be adjusted with
427 * the baud rate at set_termios.
428 */
429 uap->dmarx.auto_poll_rate = true;
430 uap->dmarx.poll_rate = 100;
431 }
432 /* 3 secs defaults poll_timeout if not specified. */
433 if (plat->dma_rx_poll_timeout)
434 uap->dmarx.poll_timeout =
435 plat->dma_rx_poll_timeout;
436 else
437 uap->dmarx.poll_timeout = 3000;
Andrew Jackson98267d32014-11-07 14:14:23 +0000438 } else if (!plat && dev->of_node) {
439 uap->dmarx.auto_poll_rate = of_property_read_bool(
440 dev->of_node, "auto-poll");
441 if (uap->dmarx.auto_poll_rate) {
442 u32 x;
Chanho Mincb06ff12013-03-27 18:38:11 +0900443
Andrew Jackson98267d32014-11-07 14:14:23 +0000444 if (0 == of_property_read_u32(dev->of_node,
445 "poll-rate-ms", &x))
446 uap->dmarx.poll_rate = x;
447 else
448 uap->dmarx.poll_rate = 100;
449 if (0 == of_property_read_u32(dev->of_node,
450 "poll-timeout-ms", &x))
451 uap->dmarx.poll_timeout = x;
452 else
453 uap->dmarx.poll_timeout = 3000;
454 }
455 }
Linus Walleijead76f322011-02-24 13:21:08 +0100456 dev_info(uap->port.dev, "DMA channel RX %s\n",
457 dma_chan_name(uap->dmarx.chan));
458 }
Russell King68b65f72010-12-22 17:24:39 +0000459}
460
Russell King68b65f72010-12-22 17:24:39 +0000461static void pl011_dma_remove(struct uart_amba_port *uap)
462{
Russell King68b65f72010-12-22 17:24:39 +0000463 if (uap->dmatx.chan)
464 dma_release_channel(uap->dmatx.chan);
Linus Walleijead76f322011-02-24 13:21:08 +0100465 if (uap->dmarx.chan)
466 dma_release_channel(uap->dmarx.chan);
Russell King68b65f72010-12-22 17:24:39 +0000467}
468
Dave Martin734745c2015-03-04 12:27:33 +0000469/* Forward declare these for the refill routine */
Russell King68b65f72010-12-22 17:24:39 +0000470static int pl011_dma_tx_refill(struct uart_amba_port *uap);
Dave Martin734745c2015-03-04 12:27:33 +0000471static void pl011_start_tx_pio(struct uart_amba_port *uap);
Russell King68b65f72010-12-22 17:24:39 +0000472
473/*
474 * The current DMA TX buffer has been sent.
475 * Try to queue up another DMA buffer.
476 */
477static void pl011_dma_tx_callback(void *data)
478{
479 struct uart_amba_port *uap = data;
480 struct pl011_dmatx_data *dmatx = &uap->dmatx;
481 unsigned long flags;
482 u16 dmacr;
483
484 spin_lock_irqsave(&uap->port.lock, flags);
485 if (uap->dmatx.queued)
486 dma_unmap_sg(dmatx->chan->device->dev, &dmatx->sg, 1,
487 DMA_TO_DEVICE);
488
489 dmacr = uap->dmacr;
490 uap->dmacr = dmacr & ~UART011_TXDMAE;
Russell King9f25bc52015-11-03 14:51:13 +0000491 pl011_write(uap->dmacr, uap, REG_DMACR);
Russell King68b65f72010-12-22 17:24:39 +0000492
493 /*
494 * If TX DMA was disabled, it means that we've stopped the DMA for
495 * some reason (eg, XOFF received, or we want to send an X-char.)
496 *
497 * Note: we need to be careful here of a potential race between DMA
498 * and the rest of the driver - if the driver disables TX DMA while
499 * a TX buffer completing, we must update the tx queued status to
500 * get further refills (hence we check dmacr).
501 */
502 if (!(dmacr & UART011_TXDMAE) || uart_tx_stopped(&uap->port) ||
503 uart_circ_empty(&uap->port.state->xmit)) {
504 uap->dmatx.queued = false;
505 spin_unlock_irqrestore(&uap->port.lock, flags);
506 return;
507 }
508
Dave Martin734745c2015-03-04 12:27:33 +0000509 if (pl011_dma_tx_refill(uap) <= 0)
Russell King68b65f72010-12-22 17:24:39 +0000510 /*
511 * We didn't queue a DMA buffer for some reason, but we
512 * have data pending to be sent. Re-enable the TX IRQ.
513 */
Dave Martin734745c2015-03-04 12:27:33 +0000514 pl011_start_tx_pio(uap);
515
Russell King68b65f72010-12-22 17:24:39 +0000516 spin_unlock_irqrestore(&uap->port.lock, flags);
517}
518
519/*
520 * Try to refill the TX DMA buffer.
521 * Locking: called with port lock held and IRQs disabled.
522 * Returns:
523 * 1 if we queued up a TX DMA buffer.
524 * 0 if we didn't want to handle this by DMA
525 * <0 on error
526 */
527static int pl011_dma_tx_refill(struct uart_amba_port *uap)
528{
529 struct pl011_dmatx_data *dmatx = &uap->dmatx;
530 struct dma_chan *chan = dmatx->chan;
531 struct dma_device *dma_dev = chan->device;
532 struct dma_async_tx_descriptor *desc;
533 struct circ_buf *xmit = &uap->port.state->xmit;
534 unsigned int count;
535
536 /*
537 * Try to avoid the overhead involved in using DMA if the
538 * transaction fits in the first half of the FIFO, by using
539 * the standard interrupt handling. This ensures that we
540 * issue a uart_write_wakeup() at the appropriate time.
541 */
542 count = uart_circ_chars_pending(xmit);
543 if (count < (uap->fifosize >> 1)) {
544 uap->dmatx.queued = false;
545 return 0;
546 }
547
548 /*
549 * Bodge: don't send the last character by DMA, as this
550 * will prevent XON from notifying us to restart DMA.
551 */
552 count -= 1;
553
554 /* Else proceed to copy the TX chars to the DMA buffer and fire DMA */
555 if (count > PL011_DMA_BUFFER_SIZE)
556 count = PL011_DMA_BUFFER_SIZE;
557
558 if (xmit->tail < xmit->head)
559 memcpy(&dmatx->buf[0], &xmit->buf[xmit->tail], count);
560 else {
561 size_t first = UART_XMIT_SIZE - xmit->tail;
Andrew Jacksone2a545a2014-11-07 14:14:39 +0000562 size_t second;
563
564 if (first > count)
565 first = count;
566 second = count - first;
Russell King68b65f72010-12-22 17:24:39 +0000567
568 memcpy(&dmatx->buf[0], &xmit->buf[xmit->tail], first);
569 if (second)
570 memcpy(&dmatx->buf[first], &xmit->buf[0], second);
571 }
572
573 dmatx->sg.length = count;
574
575 if (dma_map_sg(dma_dev->dev, &dmatx->sg, 1, DMA_TO_DEVICE) != 1) {
576 uap->dmatx.queued = false;
577 dev_dbg(uap->port.dev, "unable to map TX DMA\n");
578 return -EBUSY;
579 }
580
Alexandre Bounine16052822012-03-08 16:11:18 -0500581 desc = dmaengine_prep_slave_sg(chan, &dmatx->sg, 1, DMA_MEM_TO_DEV,
Russell King68b65f72010-12-22 17:24:39 +0000582 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
583 if (!desc) {
584 dma_unmap_sg(dma_dev->dev, &dmatx->sg, 1, DMA_TO_DEVICE);
585 uap->dmatx.queued = false;
586 /*
587 * If DMA cannot be used right now, we complete this
588 * transaction via IRQ and let the TTY layer retry.
589 */
590 dev_dbg(uap->port.dev, "TX DMA busy\n");
591 return -EBUSY;
592 }
593
594 /* Some data to go along to the callback */
595 desc->callback = pl011_dma_tx_callback;
596 desc->callback_param = uap;
597
598 /* All errors should happen at prepare time */
599 dmaengine_submit(desc);
600
601 /* Fire the DMA transaction */
602 dma_dev->device_issue_pending(chan);
603
604 uap->dmacr |= UART011_TXDMAE;
Russell King9f25bc52015-11-03 14:51:13 +0000605 pl011_write(uap->dmacr, uap, REG_DMACR);
Russell King68b65f72010-12-22 17:24:39 +0000606 uap->dmatx.queued = true;
607
608 /*
609 * Now we know that DMA will fire, so advance the ring buffer
610 * with the stuff we just dispatched.
611 */
612 xmit->tail = (xmit->tail + count) & (UART_XMIT_SIZE - 1);
613 uap->port.icount.tx += count;
614
615 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
616 uart_write_wakeup(&uap->port);
617
618 return 1;
619}
620
621/*
622 * We received a transmit interrupt without a pending X-char but with
623 * pending characters.
624 * Locking: called with port lock held and IRQs disabled.
625 * Returns:
626 * false if we want to use PIO to transmit
627 * true if we queued a DMA buffer
628 */
629static bool pl011_dma_tx_irq(struct uart_amba_port *uap)
630{
Linus Walleijead76f322011-02-24 13:21:08 +0100631 if (!uap->using_tx_dma)
Russell King68b65f72010-12-22 17:24:39 +0000632 return false;
633
634 /*
635 * If we already have a TX buffer queued, but received a
636 * TX interrupt, it will be because we've just sent an X-char.
637 * Ensure the TX DMA is enabled and the TX IRQ is disabled.
638 */
639 if (uap->dmatx.queued) {
640 uap->dmacr |= UART011_TXDMAE;
Russell King9f25bc52015-11-03 14:51:13 +0000641 pl011_write(uap->dmacr, uap, REG_DMACR);
Russell King68b65f72010-12-22 17:24:39 +0000642 uap->im &= ~UART011_TXIM;
Russell King9f25bc52015-11-03 14:51:13 +0000643 pl011_write(uap->im, uap, REG_IMSC);
Russell King68b65f72010-12-22 17:24:39 +0000644 return true;
645 }
646
647 /*
648 * We don't have a TX buffer queued, so try to queue one.
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300649 * If we successfully queued a buffer, mask the TX IRQ.
Russell King68b65f72010-12-22 17:24:39 +0000650 */
651 if (pl011_dma_tx_refill(uap) > 0) {
652 uap->im &= ~UART011_TXIM;
Russell King9f25bc52015-11-03 14:51:13 +0000653 pl011_write(uap->im, uap, REG_IMSC);
Russell King68b65f72010-12-22 17:24:39 +0000654 return true;
655 }
656 return false;
657}
658
659/*
660 * Stop the DMA transmit (eg, due to received XOFF).
661 * Locking: called with port lock held and IRQs disabled.
662 */
663static inline void pl011_dma_tx_stop(struct uart_amba_port *uap)
664{
665 if (uap->dmatx.queued) {
666 uap->dmacr &= ~UART011_TXDMAE;
Russell King9f25bc52015-11-03 14:51:13 +0000667 pl011_write(uap->dmacr, uap, REG_DMACR);
Russell King68b65f72010-12-22 17:24:39 +0000668 }
669}
670
671/*
672 * Try to start a DMA transmit, or in the case of an XON/OFF
673 * character queued for send, try to get that character out ASAP.
674 * Locking: called with port lock held and IRQs disabled.
675 * Returns:
676 * false if we want the TX IRQ to be enabled
677 * true if we have a buffer queued
678 */
679static inline bool pl011_dma_tx_start(struct uart_amba_port *uap)
680{
681 u16 dmacr;
682
Linus Walleijead76f322011-02-24 13:21:08 +0100683 if (!uap->using_tx_dma)
Russell King68b65f72010-12-22 17:24:39 +0000684 return false;
685
686 if (!uap->port.x_char) {
687 /* no X-char, try to push chars out in DMA mode */
688 bool ret = true;
689
690 if (!uap->dmatx.queued) {
691 if (pl011_dma_tx_refill(uap) > 0) {
692 uap->im &= ~UART011_TXIM;
Russell King9f25bc52015-11-03 14:51:13 +0000693 pl011_write(uap->im, uap, REG_IMSC);
Dave Martin734745c2015-03-04 12:27:33 +0000694 } else
Russell King68b65f72010-12-22 17:24:39 +0000695 ret = false;
Russell King68b65f72010-12-22 17:24:39 +0000696 } else if (!(uap->dmacr & UART011_TXDMAE)) {
697 uap->dmacr |= UART011_TXDMAE;
Russell King9f25bc52015-11-03 14:51:13 +0000698 pl011_write(uap->dmacr, uap, REG_DMACR);
Russell King68b65f72010-12-22 17:24:39 +0000699 }
700 return ret;
701 }
702
703 /*
704 * We have an X-char to send. Disable DMA to prevent it loading
705 * the TX fifo, and then see if we can stuff it into the FIFO.
706 */
707 dmacr = uap->dmacr;
708 uap->dmacr &= ~UART011_TXDMAE;
Russell King9f25bc52015-11-03 14:51:13 +0000709 pl011_write(uap->dmacr, uap, REG_DMACR);
Russell King68b65f72010-12-22 17:24:39 +0000710
Russell King9f25bc52015-11-03 14:51:13 +0000711 if (pl011_read(uap, REG_FR) & UART01x_FR_TXFF) {
Russell King68b65f72010-12-22 17:24:39 +0000712 /*
713 * No space in the FIFO, so enable the transmit interrupt
714 * so we know when there is space. Note that once we've
715 * loaded the character, we should just re-enable DMA.
716 */
717 return false;
718 }
719
Russell King9f25bc52015-11-03 14:51:13 +0000720 pl011_write(uap->port.x_char, uap, REG_DR);
Russell King68b65f72010-12-22 17:24:39 +0000721 uap->port.icount.tx++;
722 uap->port.x_char = 0;
723
724 /* Success - restore the DMA state */
725 uap->dmacr = dmacr;
Russell King9f25bc52015-11-03 14:51:13 +0000726 pl011_write(dmacr, uap, REG_DMACR);
Russell King68b65f72010-12-22 17:24:39 +0000727
728 return true;
729}
730
731/*
732 * Flush the transmit buffer.
733 * Locking: called with port lock held and IRQs disabled.
734 */
735static void pl011_dma_flush_buffer(struct uart_port *port)
Fabio Estevamb83286b2013-08-09 17:58:51 -0300736__releases(&uap->port.lock)
737__acquires(&uap->port.lock)
Russell King68b65f72010-12-22 17:24:39 +0000738{
Daniel Thompsona5820c22014-09-03 12:51:55 +0100739 struct uart_amba_port *uap =
740 container_of(port, struct uart_amba_port, port);
Russell King68b65f72010-12-22 17:24:39 +0000741
Linus Walleijead76f322011-02-24 13:21:08 +0100742 if (!uap->using_tx_dma)
Russell King68b65f72010-12-22 17:24:39 +0000743 return;
744
745 /* Avoid deadlock with the DMA engine callback */
746 spin_unlock(&uap->port.lock);
747 dmaengine_terminate_all(uap->dmatx.chan);
748 spin_lock(&uap->port.lock);
749 if (uap->dmatx.queued) {
750 dma_unmap_sg(uap->dmatx.chan->device->dev, &uap->dmatx.sg, 1,
751 DMA_TO_DEVICE);
752 uap->dmatx.queued = false;
753 uap->dmacr &= ~UART011_TXDMAE;
Russell King9f25bc52015-11-03 14:51:13 +0000754 pl011_write(uap->dmacr, uap, REG_DMACR);
Russell King68b65f72010-12-22 17:24:39 +0000755 }
756}
757
Linus Walleijead76f322011-02-24 13:21:08 +0100758static void pl011_dma_rx_callback(void *data);
759
760static int pl011_dma_rx_trigger_dma(struct uart_amba_port *uap)
761{
762 struct dma_chan *rxchan = uap->dmarx.chan;
Linus Walleijead76f322011-02-24 13:21:08 +0100763 struct pl011_dmarx_data *dmarx = &uap->dmarx;
764 struct dma_async_tx_descriptor *desc;
765 struct pl011_sgbuf *sgbuf;
766
767 if (!rxchan)
768 return -EIO;
769
770 /* Start the RX DMA job */
771 sgbuf = uap->dmarx.use_buf_b ?
772 &uap->dmarx.sgbuf_b : &uap->dmarx.sgbuf_a;
Alexandre Bounine16052822012-03-08 16:11:18 -0500773 desc = dmaengine_prep_slave_sg(rxchan, &sgbuf->sg, 1,
Vinod Koula485df42011-10-14 10:47:38 +0530774 DMA_DEV_TO_MEM,
Linus Walleijead76f322011-02-24 13:21:08 +0100775 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
776 /*
777 * If the DMA engine is busy and cannot prepare a
778 * channel, no big deal, the driver will fall back
779 * to interrupt mode as a result of this error code.
780 */
781 if (!desc) {
782 uap->dmarx.running = false;
783 dmaengine_terminate_all(rxchan);
784 return -EBUSY;
785 }
786
787 /* Some data to go along to the callback */
788 desc->callback = pl011_dma_rx_callback;
789 desc->callback_param = uap;
790 dmarx->cookie = dmaengine_submit(desc);
791 dma_async_issue_pending(rxchan);
792
793 uap->dmacr |= UART011_RXDMAE;
Russell King9f25bc52015-11-03 14:51:13 +0000794 pl011_write(uap->dmacr, uap, REG_DMACR);
Linus Walleijead76f322011-02-24 13:21:08 +0100795 uap->dmarx.running = true;
796
797 uap->im &= ~UART011_RXIM;
Russell King9f25bc52015-11-03 14:51:13 +0000798 pl011_write(uap->im, uap, REG_IMSC);
Linus Walleijead76f322011-02-24 13:21:08 +0100799
800 return 0;
801}
802
803/*
804 * This is called when either the DMA job is complete, or
805 * the FIFO timeout interrupt occurred. This must be called
806 * with the port spinlock uap->port.lock held.
807 */
808static void pl011_dma_rx_chars(struct uart_amba_port *uap,
809 u32 pending, bool use_buf_b,
810 bool readfifo)
811{
Jiri Slaby05c7cd32013-01-03 15:53:04 +0100812 struct tty_port *port = &uap->port.state->port;
Linus Walleijead76f322011-02-24 13:21:08 +0100813 struct pl011_sgbuf *sgbuf = use_buf_b ?
814 &uap->dmarx.sgbuf_b : &uap->dmarx.sgbuf_a;
Linus Walleijead76f322011-02-24 13:21:08 +0100815 int dma_count = 0;
816 u32 fifotaken = 0; /* only used for vdbg() */
817
Chanho Mincb06ff12013-03-27 18:38:11 +0900818 struct pl011_dmarx_data *dmarx = &uap->dmarx;
819 int dmataken = 0;
820
821 if (uap->dmarx.poll_rate) {
822 /* The data can be taken by polling */
823 dmataken = sgbuf->sg.length - dmarx->last_residue;
824 /* Recalculate the pending size */
825 if (pending >= dmataken)
826 pending -= dmataken;
827 }
828
829 /* Pick the remain data from the DMA */
Linus Walleijead76f322011-02-24 13:21:08 +0100830 if (pending) {
Linus Walleijead76f322011-02-24 13:21:08 +0100831
832 /*
833 * First take all chars in the DMA pipe, then look in the FIFO.
834 * Note that tty_insert_flip_buf() tries to take as many chars
835 * as it can.
836 */
Chanho Mincb06ff12013-03-27 18:38:11 +0900837 dma_count = tty_insert_flip_string(port, sgbuf->buf + dmataken,
838 pending);
Linus Walleijead76f322011-02-24 13:21:08 +0100839
840 uap->port.icount.rx += dma_count;
841 if (dma_count < pending)
842 dev_warn(uap->port.dev,
843 "couldn't insert all characters (TTY is full?)\n");
844 }
845
Chanho Mincb06ff12013-03-27 18:38:11 +0900846 /* Reset the last_residue for Rx DMA poll */
847 if (uap->dmarx.poll_rate)
848 dmarx->last_residue = sgbuf->sg.length;
849
Linus Walleijead76f322011-02-24 13:21:08 +0100850 /*
851 * Only continue with trying to read the FIFO if all DMA chars have
852 * been taken first.
853 */
854 if (dma_count == pending && readfifo) {
855 /* Clear any error flags */
Russell King75836332015-11-03 14:50:58 +0000856 pl011_write(UART011_OEIS | UART011_BEIS | UART011_PEIS |
Russell King9f25bc52015-11-03 14:51:13 +0000857 UART011_FEIS, uap, REG_ICR);
Linus Walleijead76f322011-02-24 13:21:08 +0100858
859 /*
860 * If we read all the DMA'd characters, and we had an
Linus Walleij29772c42011-02-24 13:21:36 +0100861 * incomplete buffer, that could be due to an rx error, or
862 * maybe we just timed out. Read any pending chars and check
863 * the error status.
864 *
865 * Error conditions will only occur in the FIFO, these will
866 * trigger an immediate interrupt and stop the DMA job, so we
867 * will always find the error in the FIFO, never in the DMA
868 * buffer.
Linus Walleijead76f322011-02-24 13:21:08 +0100869 */
Linus Walleij29772c42011-02-24 13:21:36 +0100870 fifotaken = pl011_fifo_to_tty(uap);
Linus Walleijead76f322011-02-24 13:21:08 +0100871 }
872
873 spin_unlock(&uap->port.lock);
874 dev_vdbg(uap->port.dev,
875 "Took %d chars from DMA buffer and %d chars from the FIFO\n",
876 dma_count, fifotaken);
Jiri Slaby2e124b42013-01-03 15:53:06 +0100877 tty_flip_buffer_push(port);
Linus Walleijead76f322011-02-24 13:21:08 +0100878 spin_lock(&uap->port.lock);
879}
880
881static void pl011_dma_rx_irq(struct uart_amba_port *uap)
882{
883 struct pl011_dmarx_data *dmarx = &uap->dmarx;
884 struct dma_chan *rxchan = dmarx->chan;
885 struct pl011_sgbuf *sgbuf = dmarx->use_buf_b ?
886 &dmarx->sgbuf_b : &dmarx->sgbuf_a;
887 size_t pending;
888 struct dma_tx_state state;
889 enum dma_status dmastat;
890
891 /*
892 * Pause the transfer so we can trust the current counter,
893 * do this before we pause the PL011 block, else we may
894 * overflow the FIFO.
895 */
896 if (dmaengine_pause(rxchan))
897 dev_err(uap->port.dev, "unable to pause DMA transfer\n");
898 dmastat = rxchan->device->device_tx_status(rxchan,
899 dmarx->cookie, &state);
900 if (dmastat != DMA_PAUSED)
901 dev_err(uap->port.dev, "unable to pause DMA transfer\n");
902
903 /* Disable RX DMA - incoming data will wait in the FIFO */
904 uap->dmacr &= ~UART011_RXDMAE;
Russell King9f25bc52015-11-03 14:51:13 +0000905 pl011_write(uap->dmacr, uap, REG_DMACR);
Linus Walleijead76f322011-02-24 13:21:08 +0100906 uap->dmarx.running = false;
907
908 pending = sgbuf->sg.length - state.residue;
909 BUG_ON(pending > PL011_DMA_BUFFER_SIZE);
910 /* Then we terminate the transfer - we now know our residue */
911 dmaengine_terminate_all(rxchan);
912
913 /*
914 * This will take the chars we have so far and insert
915 * into the framework.
916 */
917 pl011_dma_rx_chars(uap, pending, dmarx->use_buf_b, true);
918
919 /* Switch buffer & re-trigger DMA job */
920 dmarx->use_buf_b = !dmarx->use_buf_b;
921 if (pl011_dma_rx_trigger_dma(uap)) {
922 dev_dbg(uap->port.dev, "could not retrigger RX DMA job "
923 "fall back to interrupt mode\n");
924 uap->im |= UART011_RXIM;
Russell King9f25bc52015-11-03 14:51:13 +0000925 pl011_write(uap->im, uap, REG_IMSC);
Linus Walleijead76f322011-02-24 13:21:08 +0100926 }
927}
928
929static void pl011_dma_rx_callback(void *data)
930{
931 struct uart_amba_port *uap = data;
932 struct pl011_dmarx_data *dmarx = &uap->dmarx;
Chanho Min6dc01aa2012-02-20 10:24:40 +0900933 struct dma_chan *rxchan = dmarx->chan;
Linus Walleijead76f322011-02-24 13:21:08 +0100934 bool lastbuf = dmarx->use_buf_b;
Chanho Min6dc01aa2012-02-20 10:24:40 +0900935 struct pl011_sgbuf *sgbuf = dmarx->use_buf_b ?
936 &dmarx->sgbuf_b : &dmarx->sgbuf_a;
937 size_t pending;
938 struct dma_tx_state state;
Linus Walleijead76f322011-02-24 13:21:08 +0100939 int ret;
940
941 /*
942 * This completion interrupt occurs typically when the
943 * RX buffer is totally stuffed but no timeout has yet
944 * occurred. When that happens, we just want the RX
945 * routine to flush out the secondary DMA buffer while
946 * we immediately trigger the next DMA job.
947 */
948 spin_lock_irq(&uap->port.lock);
Chanho Min6dc01aa2012-02-20 10:24:40 +0900949 /*
950 * Rx data can be taken by the UART interrupts during
951 * the DMA irq handler. So we check the residue here.
952 */
953 rxchan->device->device_tx_status(rxchan, dmarx->cookie, &state);
954 pending = sgbuf->sg.length - state.residue;
955 BUG_ON(pending > PL011_DMA_BUFFER_SIZE);
956 /* Then we terminate the transfer - we now know our residue */
957 dmaengine_terminate_all(rxchan);
958
Linus Walleijead76f322011-02-24 13:21:08 +0100959 uap->dmarx.running = false;
960 dmarx->use_buf_b = !lastbuf;
961 ret = pl011_dma_rx_trigger_dma(uap);
962
Chanho Min6dc01aa2012-02-20 10:24:40 +0900963 pl011_dma_rx_chars(uap, pending, lastbuf, false);
Linus Walleijead76f322011-02-24 13:21:08 +0100964 spin_unlock_irq(&uap->port.lock);
965 /*
966 * Do this check after we picked the DMA chars so we don't
967 * get some IRQ immediately from RX.
968 */
969 if (ret) {
970 dev_dbg(uap->port.dev, "could not retrigger RX DMA job "
971 "fall back to interrupt mode\n");
972 uap->im |= UART011_RXIM;
Russell King9f25bc52015-11-03 14:51:13 +0000973 pl011_write(uap->im, uap, REG_IMSC);
Linus Walleijead76f322011-02-24 13:21:08 +0100974 }
975}
976
977/*
978 * Stop accepting received characters, when we're shutting down or
979 * suspending this port.
980 * Locking: called with port lock held and IRQs disabled.
981 */
982static inline void pl011_dma_rx_stop(struct uart_amba_port *uap)
983{
984 /* FIXME. Just disable the DMA enable */
985 uap->dmacr &= ~UART011_RXDMAE;
Russell King9f25bc52015-11-03 14:51:13 +0000986 pl011_write(uap->dmacr, uap, REG_DMACR);
Linus Walleijead76f322011-02-24 13:21:08 +0100987}
Russell King68b65f72010-12-22 17:24:39 +0000988
Chanho Mincb06ff12013-03-27 18:38:11 +0900989/*
990 * Timer handler for Rx DMA polling.
991 * Every polling, It checks the residue in the dma buffer and transfer
992 * data to the tty. Also, last_residue is updated for the next polling.
993 */
994static void pl011_dma_rx_poll(unsigned long args)
995{
996 struct uart_amba_port *uap = (struct uart_amba_port *)args;
997 struct tty_port *port = &uap->port.state->port;
998 struct pl011_dmarx_data *dmarx = &uap->dmarx;
999 struct dma_chan *rxchan = uap->dmarx.chan;
1000 unsigned long flags = 0;
1001 unsigned int dmataken = 0;
1002 unsigned int size = 0;
1003 struct pl011_sgbuf *sgbuf;
1004 int dma_count;
1005 struct dma_tx_state state;
1006
1007 sgbuf = dmarx->use_buf_b ? &uap->dmarx.sgbuf_b : &uap->dmarx.sgbuf_a;
1008 rxchan->device->device_tx_status(rxchan, dmarx->cookie, &state);
1009 if (likely(state.residue < dmarx->last_residue)) {
1010 dmataken = sgbuf->sg.length - dmarx->last_residue;
1011 size = dmarx->last_residue - state.residue;
1012 dma_count = tty_insert_flip_string(port, sgbuf->buf + dmataken,
1013 size);
1014 if (dma_count == size)
1015 dmarx->last_residue = state.residue;
1016 dmarx->last_jiffies = jiffies;
1017 }
1018 tty_flip_buffer_push(port);
1019
1020 /*
1021 * If no data is received in poll_timeout, the driver will fall back
1022 * to interrupt mode. We will retrigger DMA at the first interrupt.
1023 */
1024 if (jiffies_to_msecs(jiffies - dmarx->last_jiffies)
1025 > uap->dmarx.poll_timeout) {
1026
1027 spin_lock_irqsave(&uap->port.lock, flags);
1028 pl011_dma_rx_stop(uap);
Guennadi Liakhovetskic25a1ad2013-12-10 14:54:47 +01001029 uap->im |= UART011_RXIM;
Russell King9f25bc52015-11-03 14:51:13 +00001030 pl011_write(uap->im, uap, REG_IMSC);
Chanho Mincb06ff12013-03-27 18:38:11 +09001031 spin_unlock_irqrestore(&uap->port.lock, flags);
1032
1033 uap->dmarx.running = false;
1034 dmaengine_terminate_all(rxchan);
1035 del_timer(&uap->dmarx.timer);
1036 } else {
1037 mod_timer(&uap->dmarx.timer,
1038 jiffies + msecs_to_jiffies(uap->dmarx.poll_rate));
1039 }
1040}
1041
Russell King68b65f72010-12-22 17:24:39 +00001042static void pl011_dma_startup(struct uart_amba_port *uap)
1043{
Linus Walleijead76f322011-02-24 13:21:08 +01001044 int ret;
1045
Jorge Ramirez-Ortiz1c9be312015-03-06 13:05:40 -05001046 if (!uap->dma_probed)
1047 pl011_dma_probe(uap);
1048
Russell King68b65f72010-12-22 17:24:39 +00001049 if (!uap->dmatx.chan)
1050 return;
1051
Andrew Jackson4c0be452014-11-07 14:14:35 +00001052 uap->dmatx.buf = kmalloc(PL011_DMA_BUFFER_SIZE, GFP_KERNEL | __GFP_DMA);
Russell King68b65f72010-12-22 17:24:39 +00001053 if (!uap->dmatx.buf) {
1054 dev_err(uap->port.dev, "no memory for DMA TX buffer\n");
1055 uap->port.fifosize = uap->fifosize;
1056 return;
1057 }
1058
1059 sg_init_one(&uap->dmatx.sg, uap->dmatx.buf, PL011_DMA_BUFFER_SIZE);
1060
1061 /* The DMA buffer is now the FIFO the TTY subsystem can use */
1062 uap->port.fifosize = PL011_DMA_BUFFER_SIZE;
Linus Walleijead76f322011-02-24 13:21:08 +01001063 uap->using_tx_dma = true;
Russell King68b65f72010-12-22 17:24:39 +00001064
Linus Walleijead76f322011-02-24 13:21:08 +01001065 if (!uap->dmarx.chan)
1066 goto skip_rx;
1067
1068 /* Allocate and map DMA RX buffers */
1069 ret = pl011_sgbuf_init(uap->dmarx.chan, &uap->dmarx.sgbuf_a,
1070 DMA_FROM_DEVICE);
1071 if (ret) {
1072 dev_err(uap->port.dev, "failed to init DMA %s: %d\n",
1073 "RX buffer A", ret);
1074 goto skip_rx;
1075 }
1076
1077 ret = pl011_sgbuf_init(uap->dmarx.chan, &uap->dmarx.sgbuf_b,
1078 DMA_FROM_DEVICE);
1079 if (ret) {
1080 dev_err(uap->port.dev, "failed to init DMA %s: %d\n",
1081 "RX buffer B", ret);
1082 pl011_sgbuf_free(uap->dmarx.chan, &uap->dmarx.sgbuf_a,
1083 DMA_FROM_DEVICE);
1084 goto skip_rx;
1085 }
1086
1087 uap->using_rx_dma = true;
1088
1089skip_rx:
Russell King68b65f72010-12-22 17:24:39 +00001090 /* Turn on DMA error (RX/TX will be enabled on demand) */
1091 uap->dmacr |= UART011_DMAONERR;
Russell King9f25bc52015-11-03 14:51:13 +00001092 pl011_write(uap->dmacr, uap, REG_DMACR);
Russell King38d62432010-12-22 17:59:16 +00001093
1094 /*
1095 * ST Micro variants has some specific dma burst threshold
1096 * compensation. Set this to 16 bytes, so burst will only
1097 * be issued above/below 16 bytes.
1098 */
1099 if (uap->vendor->dma_threshold)
Russell King75836332015-11-03 14:50:58 +00001100 pl011_write(ST_UART011_DMAWM_RX_16 | ST_UART011_DMAWM_TX_16,
Russell King9f25bc52015-11-03 14:51:13 +00001101 uap, REG_ST_DMAWM);
Linus Walleijead76f322011-02-24 13:21:08 +01001102
1103 if (uap->using_rx_dma) {
1104 if (pl011_dma_rx_trigger_dma(uap))
1105 dev_dbg(uap->port.dev, "could not trigger initial "
1106 "RX DMA job, fall back to interrupt mode\n");
Chanho Mincb06ff12013-03-27 18:38:11 +09001107 if (uap->dmarx.poll_rate) {
1108 init_timer(&(uap->dmarx.timer));
1109 uap->dmarx.timer.function = pl011_dma_rx_poll;
1110 uap->dmarx.timer.data = (unsigned long)uap;
1111 mod_timer(&uap->dmarx.timer,
1112 jiffies +
1113 msecs_to_jiffies(uap->dmarx.poll_rate));
1114 uap->dmarx.last_residue = PL011_DMA_BUFFER_SIZE;
1115 uap->dmarx.last_jiffies = jiffies;
1116 }
Linus Walleijead76f322011-02-24 13:21:08 +01001117 }
Russell King68b65f72010-12-22 17:24:39 +00001118}
1119
1120static void pl011_dma_shutdown(struct uart_amba_port *uap)
1121{
Linus Walleijead76f322011-02-24 13:21:08 +01001122 if (!(uap->using_tx_dma || uap->using_rx_dma))
Russell King68b65f72010-12-22 17:24:39 +00001123 return;
1124
1125 /* Disable RX and TX DMA */
Russell King9f25bc52015-11-03 14:51:13 +00001126 while (pl011_read(uap, REG_FR) & UART01x_FR_BUSY)
Russell King68b65f72010-12-22 17:24:39 +00001127 barrier();
1128
1129 spin_lock_irq(&uap->port.lock);
1130 uap->dmacr &= ~(UART011_DMAONERR | UART011_RXDMAE | UART011_TXDMAE);
Russell King9f25bc52015-11-03 14:51:13 +00001131 pl011_write(uap->dmacr, uap, REG_DMACR);
Russell King68b65f72010-12-22 17:24:39 +00001132 spin_unlock_irq(&uap->port.lock);
1133
Linus Walleijead76f322011-02-24 13:21:08 +01001134 if (uap->using_tx_dma) {
1135 /* In theory, this should already be done by pl011_dma_flush_buffer */
1136 dmaengine_terminate_all(uap->dmatx.chan);
1137 if (uap->dmatx.queued) {
1138 dma_unmap_sg(uap->dmatx.chan->device->dev, &uap->dmatx.sg, 1,
1139 DMA_TO_DEVICE);
1140 uap->dmatx.queued = false;
1141 }
1142
1143 kfree(uap->dmatx.buf);
1144 uap->using_tx_dma = false;
Russell King68b65f72010-12-22 17:24:39 +00001145 }
1146
Linus Walleijead76f322011-02-24 13:21:08 +01001147 if (uap->using_rx_dma) {
1148 dmaengine_terminate_all(uap->dmarx.chan);
1149 /* Clean up the RX DMA */
1150 pl011_sgbuf_free(uap->dmarx.chan, &uap->dmarx.sgbuf_a, DMA_FROM_DEVICE);
1151 pl011_sgbuf_free(uap->dmarx.chan, &uap->dmarx.sgbuf_b, DMA_FROM_DEVICE);
Chanho Mincb06ff12013-03-27 18:38:11 +09001152 if (uap->dmarx.poll_rate)
1153 del_timer_sync(&uap->dmarx.timer);
Linus Walleijead76f322011-02-24 13:21:08 +01001154 uap->using_rx_dma = false;
1155 }
Russell King68b65f72010-12-22 17:24:39 +00001156}
1157
Linus Walleijead76f322011-02-24 13:21:08 +01001158static inline bool pl011_dma_rx_available(struct uart_amba_port *uap)
1159{
1160 return uap->using_rx_dma;
1161}
1162
1163static inline bool pl011_dma_rx_running(struct uart_amba_port *uap)
1164{
1165 return uap->using_rx_dma && uap->dmarx.running;
1166}
1167
Russell King68b65f72010-12-22 17:24:39 +00001168#else
1169/* Blank functions if the DMA engine is not available */
Jorge Ramirez-Ortiz1c9be312015-03-06 13:05:40 -05001170static inline void pl011_dma_probe(struct uart_amba_port *uap)
Russell King68b65f72010-12-22 17:24:39 +00001171{
1172}
1173
1174static inline void pl011_dma_remove(struct uart_amba_port *uap)
1175{
1176}
1177
1178static inline void pl011_dma_startup(struct uart_amba_port *uap)
1179{
1180}
1181
1182static inline void pl011_dma_shutdown(struct uart_amba_port *uap)
1183{
1184}
1185
1186static inline bool pl011_dma_tx_irq(struct uart_amba_port *uap)
1187{
1188 return false;
1189}
1190
1191static inline void pl011_dma_tx_stop(struct uart_amba_port *uap)
1192{
1193}
1194
1195static inline bool pl011_dma_tx_start(struct uart_amba_port *uap)
1196{
1197 return false;
1198}
1199
Linus Walleijead76f322011-02-24 13:21:08 +01001200static inline void pl011_dma_rx_irq(struct uart_amba_port *uap)
1201{
1202}
1203
1204static inline void pl011_dma_rx_stop(struct uart_amba_port *uap)
1205{
1206}
1207
1208static inline int pl011_dma_rx_trigger_dma(struct uart_amba_port *uap)
1209{
1210 return -EIO;
1211}
1212
1213static inline bool pl011_dma_rx_available(struct uart_amba_port *uap)
1214{
1215 return false;
1216}
1217
1218static inline bool pl011_dma_rx_running(struct uart_amba_port *uap)
1219{
1220 return false;
1221}
1222
Russell King68b65f72010-12-22 17:24:39 +00001223#define pl011_dma_flush_buffer NULL
1224#endif
1225
Russell Kingb129a8c2005-08-31 10:12:14 +01001226static void pl011_stop_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001228 struct uart_amba_port *uap =
1229 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230
1231 uap->im &= ~UART011_TXIM;
Russell King9f25bc52015-11-03 14:51:13 +00001232 pl011_write(uap->im, uap, REG_IMSC);
Russell King68b65f72010-12-22 17:24:39 +00001233 pl011_dma_tx_stop(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234}
1235
Dave Martin1e84d222015-04-27 16:49:05 +01001236static void pl011_tx_chars(struct uart_amba_port *uap, bool from_irq);
Dave Martin734745c2015-03-04 12:27:33 +00001237
1238/* Start TX with programmed I/O only (no DMA) */
1239static void pl011_start_tx_pio(struct uart_amba_port *uap)
1240{
1241 uap->im |= UART011_TXIM;
Russell King9f25bc52015-11-03 14:51:13 +00001242 pl011_write(uap->im, uap, REG_IMSC);
Dave Martin1e84d222015-04-27 16:49:05 +01001243 pl011_tx_chars(uap, false);
Dave Martin734745c2015-03-04 12:27:33 +00001244}
1245
Russell Kingb129a8c2005-08-31 10:12:14 +01001246static void pl011_start_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001248 struct uart_amba_port *uap =
1249 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250
Dave Martin734745c2015-03-04 12:27:33 +00001251 if (!pl011_dma_tx_start(uap))
1252 pl011_start_tx_pio(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253}
1254
1255static void pl011_stop_rx(struct uart_port *port)
1256{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001257 struct uart_amba_port *uap =
1258 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259
1260 uap->im &= ~(UART011_RXIM|UART011_RTIM|UART011_FEIM|
1261 UART011_PEIM|UART011_BEIM|UART011_OEIM);
Russell King9f25bc52015-11-03 14:51:13 +00001262 pl011_write(uap->im, uap, REG_IMSC);
Linus Walleijead76f322011-02-24 13:21:08 +01001263
1264 pl011_dma_rx_stop(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265}
1266
1267static void pl011_enable_ms(struct uart_port *port)
1268{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001269 struct uart_amba_port *uap =
1270 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271
1272 uap->im |= UART011_RIMIM|UART011_CTSMIM|UART011_DCDMIM|UART011_DSRMIM;
Russell King9f25bc52015-11-03 14:51:13 +00001273 pl011_write(uap->im, uap, REG_IMSC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274}
1275
David Howells7d12e782006-10-05 14:55:46 +01001276static void pl011_rx_chars(struct uart_amba_port *uap)
Fabio Estevamb83286b2013-08-09 17:58:51 -03001277__releases(&uap->port.lock)
1278__acquires(&uap->port.lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279{
Linus Walleij29772c42011-02-24 13:21:36 +01001280 pl011_fifo_to_tty(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281
Thomas Gleixner2389b272007-05-29 21:53:50 +01001282 spin_unlock(&uap->port.lock);
Jiri Slaby2e124b42013-01-03 15:53:06 +01001283 tty_flip_buffer_push(&uap->port.state->port);
Linus Walleijead76f322011-02-24 13:21:08 +01001284 /*
1285 * If we were temporarily out of DMA mode for a while,
1286 * attempt to switch back to DMA mode again.
1287 */
1288 if (pl011_dma_rx_available(uap)) {
1289 if (pl011_dma_rx_trigger_dma(uap)) {
1290 dev_dbg(uap->port.dev, "could not trigger RX DMA job "
1291 "fall back to interrupt mode again\n");
1292 uap->im |= UART011_RXIM;
Russell King9f25bc52015-11-03 14:51:13 +00001293 pl011_write(uap->im, uap, REG_IMSC);
Chanho Mincb06ff12013-03-27 18:38:11 +09001294 } else {
Chanho Min89fa28d2013-04-03 11:10:37 +09001295#ifdef CONFIG_DMA_ENGINE
Chanho Mincb06ff12013-03-27 18:38:11 +09001296 /* Start Rx DMA poll */
1297 if (uap->dmarx.poll_rate) {
1298 uap->dmarx.last_jiffies = jiffies;
1299 uap->dmarx.last_residue = PL011_DMA_BUFFER_SIZE;
1300 mod_timer(&uap->dmarx.timer,
1301 jiffies +
1302 msecs_to_jiffies(uap->dmarx.poll_rate));
1303 }
Chanho Min89fa28d2013-04-03 11:10:37 +09001304#endif
Chanho Mincb06ff12013-03-27 18:38:11 +09001305 }
Linus Walleijead76f322011-02-24 13:21:08 +01001306 }
Thomas Gleixner2389b272007-05-29 21:53:50 +01001307 spin_lock(&uap->port.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308}
1309
Dave Martin1e84d222015-04-27 16:49:05 +01001310static bool pl011_tx_char(struct uart_amba_port *uap, unsigned char c,
1311 bool from_irq)
Dave Martin734745c2015-03-04 12:27:33 +00001312{
Dave Martin1e84d222015-04-27 16:49:05 +01001313 if (unlikely(!from_irq) &&
Russell King9f25bc52015-11-03 14:51:13 +00001314 pl011_read(uap, REG_FR) & UART01x_FR_TXFF)
Dave Martin1e84d222015-04-27 16:49:05 +01001315 return false; /* unable to transmit character */
1316
Russell King9f25bc52015-11-03 14:51:13 +00001317 pl011_write(c, uap, REG_DR);
Dave Martin734745c2015-03-04 12:27:33 +00001318 uap->port.icount.tx++;
1319
Dave Martin1e84d222015-04-27 16:49:05 +01001320 return true;
Dave Martin734745c2015-03-04 12:27:33 +00001321}
1322
Dave Martin1e84d222015-04-27 16:49:05 +01001323static void pl011_tx_chars(struct uart_amba_port *uap, bool from_irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324{
Alan Coxebd2c8f2009-09-19 13:13:28 -07001325 struct circ_buf *xmit = &uap->port.state->xmit;
Dave Martin1e84d222015-04-27 16:49:05 +01001326 int count = uap->fifosize >> 1;
Dave Martin734745c2015-03-04 12:27:33 +00001327
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 if (uap->port.x_char) {
Dave Martin1e84d222015-04-27 16:49:05 +01001329 if (!pl011_tx_char(uap, uap->port.x_char, from_irq))
1330 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 uap->port.x_char = 0;
Dave Martin734745c2015-03-04 12:27:33 +00001332 --count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 }
1334 if (uart_circ_empty(xmit) || uart_tx_stopped(&uap->port)) {
Russell Kingb129a8c2005-08-31 10:12:14 +01001335 pl011_stop_tx(&uap->port);
Dave Martin1e84d222015-04-27 16:49:05 +01001336 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 }
1338
Russell King68b65f72010-12-22 17:24:39 +00001339 /* If we are using DMA mode, try to send some characters. */
1340 if (pl011_dma_tx_irq(uap))
Dave Martin1e84d222015-04-27 16:49:05 +01001341 return;
Russell King68b65f72010-12-22 17:24:39 +00001342
Dave Martin1e84d222015-04-27 16:49:05 +01001343 do {
1344 if (likely(from_irq) && count-- == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 break;
Dave Martin1e84d222015-04-27 16:49:05 +01001346
1347 if (!pl011_tx_char(uap, xmit->buf[xmit->tail], from_irq))
1348 break;
1349
1350 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
1351 } while (!uart_circ_empty(xmit));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
1353 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
1354 uart_write_wakeup(&uap->port);
1355
Dave Martin1e84d222015-04-27 16:49:05 +01001356 if (uart_circ_empty(xmit))
Russell Kingb129a8c2005-08-31 10:12:14 +01001357 pl011_stop_tx(&uap->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358}
1359
1360static void pl011_modem_status(struct uart_amba_port *uap)
1361{
1362 unsigned int status, delta;
1363
Russell King9f25bc52015-11-03 14:51:13 +00001364 status = pl011_read(uap, REG_FR) & UART01x_FR_MODEM_ANY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365
1366 delta = status ^ uap->old_status;
1367 uap->old_status = status;
1368
1369 if (!delta)
1370 return;
1371
1372 if (delta & UART01x_FR_DCD)
1373 uart_handle_dcd_change(&uap->port, status & UART01x_FR_DCD);
1374
Greg Kroah-Hartman062a68a2015-09-04 09:11:24 -07001375 if (delta & UART01x_FR_DSR)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 uap->port.icount.dsr++;
1377
Greg Kroah-Hartman062a68a2015-09-04 09:11:24 -07001378 if (delta & UART01x_FR_CTS)
1379 uart_handle_cts_change(&uap->port, status & UART01x_FR_CTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380
Alan Coxbdc04e32009-09-19 13:13:31 -07001381 wake_up_interruptible(&uap->port.state->port.delta_msr_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382}
1383
Andre Przywara9c4ef4b2015-05-21 17:26:20 +01001384static void check_apply_cts_event_workaround(struct uart_amba_port *uap)
1385{
1386 unsigned int dummy_read;
1387
1388 if (!uap->vendor->cts_event_workaround)
1389 return;
1390
1391 /* workaround to make sure that all bits are unlocked.. */
Russell King9f25bc52015-11-03 14:51:13 +00001392 pl011_write(0x00, uap, REG_ICR);
Andre Przywara9c4ef4b2015-05-21 17:26:20 +01001393
1394 /*
1395 * WA: introduce 26ns(1 uart clk) delay before W1C;
1396 * single apb access will incur 2 pclk(133.12Mhz) delay,
1397 * so add 2 dummy reads
1398 */
Russell King9f25bc52015-11-03 14:51:13 +00001399 dummy_read = pl011_read(uap, REG_ICR);
1400 dummy_read = pl011_read(uap, REG_ICR);
Andre Przywara9c4ef4b2015-05-21 17:26:20 +01001401}
1402
David Howells7d12e782006-10-05 14:55:46 +01001403static irqreturn_t pl011_int(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404{
1405 struct uart_amba_port *uap = dev_id;
Russell King963cc982010-12-22 17:16:09 +00001406 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 unsigned int status, pass_counter = AMBA_ISR_PASS_LIMIT;
Andre Przywara075167e2015-05-21 17:26:19 +01001408 u16 imsc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 int handled = 0;
1410
Russell King963cc982010-12-22 17:16:09 +00001411 spin_lock_irqsave(&uap->port.lock, flags);
Russell King9f25bc52015-11-03 14:51:13 +00001412 imsc = pl011_read(uap, REG_IMSC);
1413 status = pl011_read(uap, REG_RIS) & imsc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 if (status) {
1415 do {
Andre Przywara9c4ef4b2015-05-21 17:26:20 +01001416 check_apply_cts_event_workaround(uap);
Greg Kroah-Hartmanf11c9842015-09-04 09:13:39 -07001417
Russell King75836332015-11-03 14:50:58 +00001418 pl011_write(status & ~(UART011_TXIS|UART011_RTIS|
1419 UART011_RXIS),
Russell King9f25bc52015-11-03 14:51:13 +00001420 uap, REG_ICR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421
Linus Walleijead76f322011-02-24 13:21:08 +01001422 if (status & (UART011_RTIS|UART011_RXIS)) {
1423 if (pl011_dma_rx_running(uap))
1424 pl011_dma_rx_irq(uap);
1425 else
1426 pl011_rx_chars(uap);
1427 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 if (status & (UART011_DSRMIS|UART011_DCDMIS|
1429 UART011_CTSMIS|UART011_RIMIS))
1430 pl011_modem_status(uap);
Dave Martin1e84d222015-04-27 16:49:05 +01001431 if (status & UART011_TXIS)
1432 pl011_tx_chars(uap, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433
Rajanikanth H.V4fd06902012-03-26 11:17:02 +02001434 if (pass_counter-- == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 break;
1436
Russell King9f25bc52015-11-03 14:51:13 +00001437 status = pl011_read(uap, REG_RIS) & imsc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 } while (status != 0);
1439 handled = 1;
1440 }
1441
Russell King963cc982010-12-22 17:16:09 +00001442 spin_unlock_irqrestore(&uap->port.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443
1444 return IRQ_RETVAL(handled);
1445}
1446
Linus Walleije643f872012-06-17 15:44:19 +02001447static unsigned int pl011_tx_empty(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001449 struct uart_amba_port *uap =
1450 container_of(port, struct uart_amba_port, port);
Russell King9f25bc52015-11-03 14:51:13 +00001451 unsigned int status = pl011_read(uap, REG_FR);
Greg Kroah-Hartman062a68a2015-09-04 09:11:24 -07001452 return status & (UART01x_FR_BUSY|UART01x_FR_TXFF) ? 0 : TIOCSER_TEMT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453}
1454
Linus Walleije643f872012-06-17 15:44:19 +02001455static unsigned int pl011_get_mctrl(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001457 struct uart_amba_port *uap =
1458 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 unsigned int result = 0;
Russell King9f25bc52015-11-03 14:51:13 +00001460 unsigned int status = pl011_read(uap, REG_FR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461
Jiri Slaby5159f402007-10-18 23:40:31 -07001462#define TIOCMBIT(uartbit, tiocmbit) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 if (status & uartbit) \
1464 result |= tiocmbit
1465
Jiri Slaby5159f402007-10-18 23:40:31 -07001466 TIOCMBIT(UART01x_FR_DCD, TIOCM_CAR);
Greg Kroah-Hartman062a68a2015-09-04 09:11:24 -07001467 TIOCMBIT(UART01x_FR_DSR, TIOCM_DSR);
1468 TIOCMBIT(UART01x_FR_CTS, TIOCM_CTS);
1469 TIOCMBIT(UART011_FR_RI, TIOCM_RNG);
Jiri Slaby5159f402007-10-18 23:40:31 -07001470#undef TIOCMBIT
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 return result;
1472}
1473
1474static void pl011_set_mctrl(struct uart_port *port, unsigned int mctrl)
1475{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001476 struct uart_amba_port *uap =
1477 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 unsigned int cr;
1479
Russell King9f25bc52015-11-03 14:51:13 +00001480 cr = pl011_read(uap, REG_CR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481
Jiri Slaby5159f402007-10-18 23:40:31 -07001482#define TIOCMBIT(tiocmbit, uartbit) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 if (mctrl & tiocmbit) \
1484 cr |= uartbit; \
1485 else \
1486 cr &= ~uartbit
1487
Jiri Slaby5159f402007-10-18 23:40:31 -07001488 TIOCMBIT(TIOCM_RTS, UART011_CR_RTS);
1489 TIOCMBIT(TIOCM_DTR, UART011_CR_DTR);
1490 TIOCMBIT(TIOCM_OUT1, UART011_CR_OUT1);
1491 TIOCMBIT(TIOCM_OUT2, UART011_CR_OUT2);
1492 TIOCMBIT(TIOCM_LOOP, UART011_CR_LBE);
Rabin Vincent3b438162010-02-12 06:43:11 +01001493
1494 if (uap->autorts) {
1495 /* We need to disable auto-RTS if we want to turn RTS off */
1496 TIOCMBIT(TIOCM_RTS, UART011_CR_RTSEN);
1497 }
Jiri Slaby5159f402007-10-18 23:40:31 -07001498#undef TIOCMBIT
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499
Russell King9f25bc52015-11-03 14:51:13 +00001500 pl011_write(cr, uap, REG_CR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501}
1502
1503static void pl011_break_ctl(struct uart_port *port, int break_state)
1504{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001505 struct uart_amba_port *uap =
1506 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 unsigned long flags;
1508 unsigned int lcr_h;
1509
1510 spin_lock_irqsave(&uap->port.lock, flags);
Russell Kingb2a4e242015-11-03 14:51:03 +00001511 lcr_h = pl011_read(uap, uap->lcrh_tx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 if (break_state == -1)
1513 lcr_h |= UART01x_LCRH_BRK;
1514 else
1515 lcr_h &= ~UART01x_LCRH_BRK;
Russell Kingb2a4e242015-11-03 14:51:03 +00001516 pl011_write(lcr_h, uap, uap->lcrh_tx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 spin_unlock_irqrestore(&uap->port.lock, flags);
1518}
1519
Jason Wessel84b5ae12008-02-20 13:33:39 -06001520#ifdef CONFIG_CONSOLE_POLL
Anton Vorontsov5c8124a2012-09-24 14:27:55 -07001521
1522static void pl011_quiesce_irqs(struct uart_port *port)
1523{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001524 struct uart_amba_port *uap =
1525 container_of(port, struct uart_amba_port, port);
Anton Vorontsov5c8124a2012-09-24 14:27:55 -07001526
Russell King9f25bc52015-11-03 14:51:13 +00001527 pl011_write(pl011_read(uap, REG_MIS), uap, REG_ICR);
Anton Vorontsov5c8124a2012-09-24 14:27:55 -07001528 /*
1529 * There is no way to clear TXIM as this is "ready to transmit IRQ", so
1530 * we simply mask it. start_tx() will unmask it.
1531 *
1532 * Note we can race with start_tx(), and if the race happens, the
1533 * polling user might get another interrupt just after we clear it.
1534 * But it should be OK and can happen even w/o the race, e.g.
1535 * controller immediately got some new data and raised the IRQ.
1536 *
1537 * And whoever uses polling routines assumes that it manages the device
1538 * (including tx queue), so we're also fine with start_tx()'s caller
1539 * side.
1540 */
Russell King9f25bc52015-11-03 14:51:13 +00001541 pl011_write(pl011_read(uap, REG_IMSC) & ~UART011_TXIM, uap,
1542 REG_IMSC);
Anton Vorontsov5c8124a2012-09-24 14:27:55 -07001543}
1544
Linus Walleije643f872012-06-17 15:44:19 +02001545static int pl011_get_poll_char(struct uart_port *port)
Jason Wessel84b5ae12008-02-20 13:33:39 -06001546{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001547 struct uart_amba_port *uap =
1548 container_of(port, struct uart_amba_port, port);
Jason Wessel84b5ae12008-02-20 13:33:39 -06001549 unsigned int status;
1550
Anton Vorontsov5c8124a2012-09-24 14:27:55 -07001551 /*
1552 * The caller might need IRQs lowered, e.g. if used with KDB NMI
1553 * debugger.
1554 */
1555 pl011_quiesce_irqs(port);
1556
Russell King9f25bc52015-11-03 14:51:13 +00001557 status = pl011_read(uap, REG_FR);
Jason Wesself5316b42010-05-20 21:04:22 -05001558 if (status & UART01x_FR_RXFE)
1559 return NO_POLL_CHAR;
Jason Wessel84b5ae12008-02-20 13:33:39 -06001560
Russell King9f25bc52015-11-03 14:51:13 +00001561 return pl011_read(uap, REG_DR);
Jason Wessel84b5ae12008-02-20 13:33:39 -06001562}
1563
Linus Walleije643f872012-06-17 15:44:19 +02001564static void pl011_put_poll_char(struct uart_port *port,
Jason Wessel84b5ae12008-02-20 13:33:39 -06001565 unsigned char ch)
1566{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001567 struct uart_amba_port *uap =
1568 container_of(port, struct uart_amba_port, port);
Jason Wessel84b5ae12008-02-20 13:33:39 -06001569
Russell King9f25bc52015-11-03 14:51:13 +00001570 while (pl011_read(uap, REG_FR) & UART01x_FR_TXFF)
Jason Wessel84b5ae12008-02-20 13:33:39 -06001571 barrier();
1572
Russell King9f25bc52015-11-03 14:51:13 +00001573 pl011_write(ch, uap, REG_DR);
Jason Wessel84b5ae12008-02-20 13:33:39 -06001574}
1575
1576#endif /* CONFIG_CONSOLE_POLL */
1577
Anton Vorontsovb3564c22012-09-24 14:27:54 -07001578static int pl011_hwinit(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001580 struct uart_amba_port *uap =
1581 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 int retval;
1583
Linus Walleij78d80c52012-05-23 21:18:46 +02001584 /* Optionaly enable pins to be muxed in and configured */
Linus Walleij2b996fc2013-06-05 15:36:42 +02001585 pinctrl_pm_select_default_state(port->dev);
Linus Walleij78d80c52012-05-23 21:18:46 +02001586
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 /*
1588 * Try to enable the clock producer.
1589 */
Julia Lawall1c4c4392012-08-26 18:01:01 +02001590 retval = clk_prepare_enable(uap->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 if (retval)
Tushar Behera7f6d9422014-06-26 15:35:35 +05301592 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593
1594 uap->port.uartclk = clk_get_rate(uap->clk);
1595
Linus Walleij9b96fba2012-03-13 13:27:23 +01001596 /* Clear pending error and receive interrupts */
Russell King75836332015-11-03 14:50:58 +00001597 pl011_write(UART011_OEIS | UART011_BEIS | UART011_PEIS |
1598 UART011_FEIS | UART011_RTIS | UART011_RXIS,
Russell King9f25bc52015-11-03 14:51:13 +00001599 uap, REG_ICR);
Linus Walleij9b96fba2012-03-13 13:27:23 +01001600
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 /*
Anton Vorontsovb3564c22012-09-24 14:27:54 -07001602 * Save interrupts enable mask, and enable RX interrupts in case if
1603 * the interrupt is used for NMI entry.
1604 */
Russell King9f25bc52015-11-03 14:51:13 +00001605 uap->im = pl011_read(uap, REG_IMSC);
1606 pl011_write(UART011_RTIM | UART011_RXIM, uap, REG_IMSC);
Anton Vorontsovb3564c22012-09-24 14:27:54 -07001607
Jingoo Han574de552013-07-30 17:06:57 +09001608 if (dev_get_platdata(uap->port.dev)) {
Anton Vorontsovb3564c22012-09-24 14:27:54 -07001609 struct amba_pl011_data *plat;
1610
Jingoo Han574de552013-07-30 17:06:57 +09001611 plat = dev_get_platdata(uap->port.dev);
Anton Vorontsovb3564c22012-09-24 14:27:54 -07001612 if (plat->init)
1613 plat->init();
1614 }
1615 return 0;
Anton Vorontsovb3564c22012-09-24 14:27:54 -07001616}
1617
Russell King7fe9a5a2015-11-03 14:51:08 +00001618static bool pl011_split_lcrh(const struct uart_amba_port *uap)
1619{
Russell King9f25bc52015-11-03 14:51:13 +00001620 return pl011_reg_to_offset(uap, uap->lcrh_rx) !=
1621 pl011_reg_to_offset(uap, uap->lcrh_tx);
Russell King7fe9a5a2015-11-03 14:51:08 +00001622}
1623
Jon Medhurstb60f2f62013-12-10 10:18:59 +00001624static void pl011_write_lcr_h(struct uart_amba_port *uap, unsigned int lcr_h)
1625{
Russell Kingb2a4e242015-11-03 14:51:03 +00001626 pl011_write(lcr_h, uap, uap->lcrh_rx);
Russell King7fe9a5a2015-11-03 14:51:08 +00001627 if (pl011_split_lcrh(uap)) {
Jon Medhurstb60f2f62013-12-10 10:18:59 +00001628 int i;
1629 /*
1630 * Wait 10 PCLKs before writing LCRH_TX register,
1631 * to get this delay write read only register 10 times
1632 */
1633 for (i = 0; i < 10; ++i)
Russell King9f25bc52015-11-03 14:51:13 +00001634 pl011_write(0xff, uap, REG_MIS);
Russell Kingb2a4e242015-11-03 14:51:03 +00001635 pl011_write(lcr_h, uap, uap->lcrh_tx);
Jon Medhurstb60f2f62013-12-10 10:18:59 +00001636 }
1637}
1638
Andre Przywara867b8e82015-05-21 17:26:15 +01001639static int pl011_allocate_irq(struct uart_amba_port *uap)
1640{
Russell King9f25bc52015-11-03 14:51:13 +00001641 pl011_write(uap->im, uap, REG_IMSC);
Andre Przywara867b8e82015-05-21 17:26:15 +01001642
1643 return request_irq(uap->port.irq, pl011_int, 0, "uart-pl011", uap);
1644}
1645
1646/*
1647 * Enable interrupts, only timeouts when using DMA
1648 * if initial RX DMA job failed, start in interrupt mode
1649 * as well.
1650 */
1651static void pl011_enable_interrupts(struct uart_amba_port *uap)
1652{
1653 spin_lock_irq(&uap->port.lock);
1654
1655 /* Clear out any spuriously appearing RX interrupts */
Russell King9f25bc52015-11-03 14:51:13 +00001656 pl011_write(UART011_RTIS | UART011_RXIS, uap, REG_ICR);
Andre Przywara867b8e82015-05-21 17:26:15 +01001657 uap->im = UART011_RTIM;
1658 if (!pl011_dma_rx_running(uap))
1659 uap->im |= UART011_RXIM;
Russell King9f25bc52015-11-03 14:51:13 +00001660 pl011_write(uap->im, uap, REG_IMSC);
Andre Przywara867b8e82015-05-21 17:26:15 +01001661 spin_unlock_irq(&uap->port.lock);
1662}
1663
Anton Vorontsovb3564c22012-09-24 14:27:54 -07001664static int pl011_startup(struct uart_port *port)
1665{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001666 struct uart_amba_port *uap =
1667 container_of(port, struct uart_amba_port, port);
Dave Martin734745c2015-03-04 12:27:33 +00001668 unsigned int cr;
Anton Vorontsovb3564c22012-09-24 14:27:54 -07001669 int retval;
1670
1671 retval = pl011_hwinit(port);
1672 if (retval)
1673 goto clk_dis;
1674
Andre Przywara867b8e82015-05-21 17:26:15 +01001675 retval = pl011_allocate_irq(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 if (retval)
1677 goto clk_dis;
1678
Russell King9f25bc52015-11-03 14:51:13 +00001679 pl011_write(uap->vendor->ifls, uap, REG_IFLS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680
Jon Medhurstfe433902013-12-10 10:18:58 +00001681 spin_lock_irq(&uap->port.lock);
1682
Shreshtha Kumar Sahud8d8ffa2012-01-18 15:53:59 +05301683 /* restore RTS and DTR */
1684 cr = uap->old_cr & (UART011_CR_RTS | UART011_CR_DTR);
1685 cr |= UART01x_CR_UARTEN | UART011_CR_RXE | UART011_CR_TXE;
Russell King9f25bc52015-11-03 14:51:13 +00001686 pl011_write(cr, uap, REG_CR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687
Jon Medhurstfe433902013-12-10 10:18:58 +00001688 spin_unlock_irq(&uap->port.lock);
1689
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 /*
1691 * initialise the old status of the modem signals
1692 */
Russell King9f25bc52015-11-03 14:51:13 +00001693 uap->old_status = pl011_read(uap, REG_FR) & UART01x_FR_MODEM_ANY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694
Russell King68b65f72010-12-22 17:24:39 +00001695 /* Startup DMA */
1696 pl011_dma_startup(uap);
1697
Andre Przywara867b8e82015-05-21 17:26:15 +01001698 pl011_enable_interrupts(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699
1700 return 0;
1701
1702 clk_dis:
Julia Lawall1c4c4392012-08-26 18:01:01 +02001703 clk_disable_unprepare(uap->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 return retval;
1705}
1706
Andre Przywara0dd1e242015-05-21 17:26:23 +01001707static int sbsa_uart_startup(struct uart_port *port)
1708{
1709 struct uart_amba_port *uap =
1710 container_of(port, struct uart_amba_port, port);
1711 int retval;
1712
1713 retval = pl011_hwinit(port);
1714 if (retval)
1715 return retval;
1716
1717 retval = pl011_allocate_irq(uap);
1718 if (retval)
1719 return retval;
1720
1721 /* The SBSA UART does not support any modem status lines. */
1722 uap->old_status = 0;
1723
1724 pl011_enable_interrupts(uap);
1725
1726 return 0;
1727}
1728
Linus Walleijec489aa2010-06-02 08:13:52 +01001729static void pl011_shutdown_channel(struct uart_amba_port *uap,
1730 unsigned int lcrh)
1731{
Greg Kroah-Hartmanf11c9842015-09-04 09:13:39 -07001732 unsigned long val;
Linus Walleijec489aa2010-06-02 08:13:52 +01001733
Russell Kingb2a4e242015-11-03 14:51:03 +00001734 val = pl011_read(uap, lcrh);
Greg Kroah-Hartmanf11c9842015-09-04 09:13:39 -07001735 val &= ~(UART01x_LCRH_BRK | UART01x_LCRH_FEN);
Russell Kingb2a4e242015-11-03 14:51:03 +00001736 pl011_write(val, uap, lcrh);
Linus Walleijec489aa2010-06-02 08:13:52 +01001737}
1738
Andre Przywara95166a32015-05-21 17:26:16 +01001739/*
1740 * disable the port. It should not disable RTS and DTR.
1741 * Also RTS and DTR state should be preserved to restore
1742 * it during startup().
1743 */
1744static void pl011_disable_uart(struct uart_amba_port *uap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745{
Shreshtha Kumar Sahud8d8ffa2012-01-18 15:53:59 +05301746 unsigned int cr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747
Rabin Vincent3b438162010-02-12 06:43:11 +01001748 uap->autorts = false;
Jon Medhurstfe433902013-12-10 10:18:58 +00001749 spin_lock_irq(&uap->port.lock);
Russell King9f25bc52015-11-03 14:51:13 +00001750 cr = pl011_read(uap, REG_CR);
Shreshtha Kumar Sahud8d8ffa2012-01-18 15:53:59 +05301751 uap->old_cr = cr;
1752 cr &= UART011_CR_RTS | UART011_CR_DTR;
1753 cr |= UART01x_CR_UARTEN | UART011_CR_TXE;
Russell King9f25bc52015-11-03 14:51:13 +00001754 pl011_write(cr, uap, REG_CR);
Jon Medhurstfe433902013-12-10 10:18:58 +00001755 spin_unlock_irq(&uap->port.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756
1757 /*
1758 * disable break condition and fifos
1759 */
Linus Walleijec489aa2010-06-02 08:13:52 +01001760 pl011_shutdown_channel(uap, uap->lcrh_rx);
Russell King7fe9a5a2015-11-03 14:51:08 +00001761 if (pl011_split_lcrh(uap))
Linus Walleijec489aa2010-06-02 08:13:52 +01001762 pl011_shutdown_channel(uap, uap->lcrh_tx);
Andre Przywara95166a32015-05-21 17:26:16 +01001763}
1764
1765static void pl011_disable_interrupts(struct uart_amba_port *uap)
1766{
1767 spin_lock_irq(&uap->port.lock);
1768
1769 /* mask all interrupts and clear all pending ones */
1770 uap->im = 0;
Russell King9f25bc52015-11-03 14:51:13 +00001771 pl011_write(uap->im, uap, REG_IMSC);
1772 pl011_write(0xffff, uap, REG_ICR);
Andre Przywara95166a32015-05-21 17:26:16 +01001773
1774 spin_unlock_irq(&uap->port.lock);
1775}
1776
1777static void pl011_shutdown(struct uart_port *port)
1778{
1779 struct uart_amba_port *uap =
1780 container_of(port, struct uart_amba_port, port);
1781
1782 pl011_disable_interrupts(uap);
1783
1784 pl011_dma_shutdown(uap);
1785
1786 free_irq(uap->port.irq, uap);
1787
1788 pl011_disable_uart(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789
1790 /*
1791 * Shut down the clock producer
1792 */
Julia Lawall1c4c4392012-08-26 18:01:01 +02001793 clk_disable_unprepare(uap->clk);
Linus Walleij78d80c52012-05-23 21:18:46 +02001794 /* Optionally let pins go into sleep states */
Linus Walleij2b996fc2013-06-05 15:36:42 +02001795 pinctrl_pm_select_sleep_state(port->dev);
Shreshtha Kumar Sahuc16d51a2011-06-13 10:11:33 +02001796
Jingoo Han574de552013-07-30 17:06:57 +09001797 if (dev_get_platdata(uap->port.dev)) {
Shreshtha Kumar Sahuc16d51a2011-06-13 10:11:33 +02001798 struct amba_pl011_data *plat;
1799
Jingoo Han574de552013-07-30 17:06:57 +09001800 plat = dev_get_platdata(uap->port.dev);
Shreshtha Kumar Sahuc16d51a2011-06-13 10:11:33 +02001801 if (plat->exit)
1802 plat->exit();
1803 }
1804
Peter Hurley36f339d2014-11-06 09:06:12 -05001805 if (uap->port.ops->flush_buffer)
1806 uap->port.ops->flush_buffer(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807}
1808
Andre Przywara0dd1e242015-05-21 17:26:23 +01001809static void sbsa_uart_shutdown(struct uart_port *port)
1810{
1811 struct uart_amba_port *uap =
1812 container_of(port, struct uart_amba_port, port);
1813
1814 pl011_disable_interrupts(uap);
1815
1816 free_irq(uap->port.irq, uap);
1817
1818 if (uap->port.ops->flush_buffer)
1819 uap->port.ops->flush_buffer(port);
1820}
1821
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822static void
Andre Przywaraef5a9352015-05-21 17:26:17 +01001823pl011_setup_status_masks(struct uart_port *port, struct ktermios *termios)
1824{
1825 port->read_status_mask = UART011_DR_OE | 255;
1826 if (termios->c_iflag & INPCK)
1827 port->read_status_mask |= UART011_DR_FE | UART011_DR_PE;
1828 if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
1829 port->read_status_mask |= UART011_DR_BE;
1830
1831 /*
1832 * Characters to ignore
1833 */
1834 port->ignore_status_mask = 0;
1835 if (termios->c_iflag & IGNPAR)
1836 port->ignore_status_mask |= UART011_DR_FE | UART011_DR_PE;
1837 if (termios->c_iflag & IGNBRK) {
1838 port->ignore_status_mask |= UART011_DR_BE;
1839 /*
1840 * If we're ignoring parity and break indicators,
1841 * ignore overruns too (for real raw support).
1842 */
1843 if (termios->c_iflag & IGNPAR)
1844 port->ignore_status_mask |= UART011_DR_OE;
1845 }
1846
1847 /*
1848 * Ignore all characters if CREAD is not set.
1849 */
1850 if ((termios->c_cflag & CREAD) == 0)
1851 port->ignore_status_mask |= UART_DUMMY_DR_RX;
1852}
1853
1854static void
Alan Cox606d0992006-12-08 02:38:45 -08001855pl011_set_termios(struct uart_port *port, struct ktermios *termios,
1856 struct ktermios *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001858 struct uart_amba_port *uap =
1859 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 unsigned int lcr_h, old_cr;
1861 unsigned long flags;
Russell Kingc19f12b2010-12-22 17:48:26 +00001862 unsigned int baud, quot, clkdiv;
1863
1864 if (uap->vendor->oversampling)
1865 clkdiv = 8;
1866 else
1867 clkdiv = 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868
1869 /*
1870 * Ask the core to calculate the divisor for us.
1871 */
Linus Walleijac3e3fb2010-06-02 20:40:22 +01001872 baud = uart_get_baud_rate(port, termios, old, 0,
Russell Kingc19f12b2010-12-22 17:48:26 +00001873 port->uartclk / clkdiv);
Chanho Min89fa28d2013-04-03 11:10:37 +09001874#ifdef CONFIG_DMA_ENGINE
Chanho Mincb06ff12013-03-27 18:38:11 +09001875 /*
1876 * Adjust RX DMA polling rate with baud rate if not specified.
1877 */
1878 if (uap->dmarx.auto_poll_rate)
1879 uap->dmarx.poll_rate = DIV_ROUND_UP(10000000, baud);
Chanho Min89fa28d2013-04-03 11:10:37 +09001880#endif
Linus Walleijac3e3fb2010-06-02 20:40:22 +01001881
1882 if (baud > port->uartclk/16)
1883 quot = DIV_ROUND_CLOSEST(port->uartclk * 8, baud);
1884 else
1885 quot = DIV_ROUND_CLOSEST(port->uartclk * 4, baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886
1887 switch (termios->c_cflag & CSIZE) {
1888 case CS5:
1889 lcr_h = UART01x_LCRH_WLEN_5;
1890 break;
1891 case CS6:
1892 lcr_h = UART01x_LCRH_WLEN_6;
1893 break;
1894 case CS7:
1895 lcr_h = UART01x_LCRH_WLEN_7;
1896 break;
1897 default: // CS8
1898 lcr_h = UART01x_LCRH_WLEN_8;
1899 break;
1900 }
1901 if (termios->c_cflag & CSTOPB)
1902 lcr_h |= UART01x_LCRH_STP2;
1903 if (termios->c_cflag & PARENB) {
1904 lcr_h |= UART01x_LCRH_PEN;
1905 if (!(termios->c_cflag & PARODD))
1906 lcr_h |= UART01x_LCRH_EPS;
1907 }
Russell Kingffca2b12010-12-22 17:13:05 +00001908 if (uap->fifosize > 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 lcr_h |= UART01x_LCRH_FEN;
1910
1911 spin_lock_irqsave(&port->lock, flags);
1912
1913 /*
1914 * Update the per-port timeout.
1915 */
1916 uart_update_timeout(port, termios->c_cflag, baud);
1917
Andre Przywaraef5a9352015-05-21 17:26:17 +01001918 pl011_setup_status_masks(port, termios);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919
1920 if (UART_ENABLE_MS(port, termios->c_cflag))
1921 pl011_enable_ms(port);
1922
1923 /* first, disable everything */
Russell King9f25bc52015-11-03 14:51:13 +00001924 old_cr = pl011_read(uap, REG_CR);
1925 pl011_write(0, uap, REG_CR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926
Rabin Vincent3b438162010-02-12 06:43:11 +01001927 if (termios->c_cflag & CRTSCTS) {
1928 if (old_cr & UART011_CR_RTS)
1929 old_cr |= UART011_CR_RTSEN;
1930
1931 old_cr |= UART011_CR_CTSEN;
1932 uap->autorts = true;
1933 } else {
1934 old_cr &= ~(UART011_CR_CTSEN | UART011_CR_RTSEN);
1935 uap->autorts = false;
1936 }
1937
Russell Kingc19f12b2010-12-22 17:48:26 +00001938 if (uap->vendor->oversampling) {
1939 if (baud > port->uartclk / 16)
Linus Walleijac3e3fb2010-06-02 20:40:22 +01001940 old_cr |= ST_UART011_CR_OVSFACT;
1941 else
1942 old_cr &= ~ST_UART011_CR_OVSFACT;
1943 }
1944
Linus Walleijc5dd5532012-09-26 17:21:36 +02001945 /*
1946 * Workaround for the ST Micro oversampling variants to
1947 * increase the bitrate slightly, by lowering the divisor,
1948 * to avoid delayed sampling of start bit at high speeds,
1949 * else we see data corruption.
1950 */
1951 if (uap->vendor->oversampling) {
1952 if ((baud >= 3000000) && (baud < 3250000) && (quot > 1))
1953 quot -= 1;
1954 else if ((baud > 3250000) && (quot > 2))
1955 quot -= 2;
1956 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 /* Set baud rate */
Russell King9f25bc52015-11-03 14:51:13 +00001958 pl011_write(quot & 0x3f, uap, REG_FBRD);
1959 pl011_write(quot >> 6, uap, REG_IBRD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960
1961 /*
1962 * ----------v----------v----------v----------v-----
Linus Walleijc5dd5532012-09-26 17:21:36 +02001963 * NOTE: lcrh_tx and lcrh_rx MUST BE WRITTEN AFTER
Russell King9f25bc52015-11-03 14:51:13 +00001964 * REG_FBRD & REG_IBRD.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 * ----------^----------^----------^----------^-----
1966 */
Jon Medhurstb60f2f62013-12-10 10:18:59 +00001967 pl011_write_lcr_h(uap, lcr_h);
Russell King9f25bc52015-11-03 14:51:13 +00001968 pl011_write(old_cr, uap, REG_CR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969
1970 spin_unlock_irqrestore(&port->lock, flags);
1971}
1972
Andre Przywara0dd1e242015-05-21 17:26:23 +01001973static void
1974sbsa_uart_set_termios(struct uart_port *port, struct ktermios *termios,
1975 struct ktermios *old)
1976{
1977 struct uart_amba_port *uap =
1978 container_of(port, struct uart_amba_port, port);
1979 unsigned long flags;
1980
1981 tty_termios_encode_baud_rate(termios, uap->fixed_baud, uap->fixed_baud);
1982
1983 /* The SBSA UART only supports 8n1 without hardware flow control. */
1984 termios->c_cflag &= ~(CSIZE | CSTOPB | PARENB | PARODD);
1985 termios->c_cflag &= ~(CMSPAR | CRTSCTS);
1986 termios->c_cflag |= CS8 | CLOCAL;
1987
1988 spin_lock_irqsave(&port->lock, flags);
1989 uart_update_timeout(port, CS8, uap->fixed_baud);
1990 pl011_setup_status_masks(port, termios);
1991 spin_unlock_irqrestore(&port->lock, flags);
1992}
1993
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994static const char *pl011_type(struct uart_port *port)
1995{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001996 struct uart_amba_port *uap =
1997 container_of(port, struct uart_amba_port, port);
Russell Kinge8a7ba82010-12-28 09:16:54 +00001998 return uap->port.type == PORT_AMBA ? uap->type : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999}
2000
2001/*
2002 * Release the memory region(s) being used by 'port'
2003 */
Linus Walleije643f872012-06-17 15:44:19 +02002004static void pl011_release_port(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005{
2006 release_mem_region(port->mapbase, SZ_4K);
2007}
2008
2009/*
2010 * Request the memory region(s) being used by 'port'
2011 */
Linus Walleije643f872012-06-17 15:44:19 +02002012static int pl011_request_port(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013{
2014 return request_mem_region(port->mapbase, SZ_4K, "uart-pl011")
2015 != NULL ? 0 : -EBUSY;
2016}
2017
2018/*
2019 * Configure/autoconfigure the port.
2020 */
Linus Walleije643f872012-06-17 15:44:19 +02002021static void pl011_config_port(struct uart_port *port, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022{
2023 if (flags & UART_CONFIG_TYPE) {
2024 port->type = PORT_AMBA;
Linus Walleije643f872012-06-17 15:44:19 +02002025 pl011_request_port(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 }
2027}
2028
2029/*
2030 * verify the new serial_struct (for TIOCSSERIAL).
2031 */
Linus Walleije643f872012-06-17 15:44:19 +02002032static int pl011_verify_port(struct uart_port *port, struct serial_struct *ser)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033{
2034 int ret = 0;
2035 if (ser->type != PORT_UNKNOWN && ser->type != PORT_AMBA)
2036 ret = -EINVAL;
Yinghai Lua62c4132008-08-19 20:49:55 -07002037 if (ser->irq < 0 || ser->irq >= nr_irqs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 ret = -EINVAL;
2039 if (ser->baud_base < 9600)
2040 ret = -EINVAL;
2041 return ret;
2042}
2043
2044static struct uart_ops amba_pl011_pops = {
Linus Walleije643f872012-06-17 15:44:19 +02002045 .tx_empty = pl011_tx_empty,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 .set_mctrl = pl011_set_mctrl,
Linus Walleije643f872012-06-17 15:44:19 +02002047 .get_mctrl = pl011_get_mctrl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 .stop_tx = pl011_stop_tx,
2049 .start_tx = pl011_start_tx,
2050 .stop_rx = pl011_stop_rx,
2051 .enable_ms = pl011_enable_ms,
2052 .break_ctl = pl011_break_ctl,
2053 .startup = pl011_startup,
2054 .shutdown = pl011_shutdown,
Russell King68b65f72010-12-22 17:24:39 +00002055 .flush_buffer = pl011_dma_flush_buffer,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 .set_termios = pl011_set_termios,
2057 .type = pl011_type,
Linus Walleije643f872012-06-17 15:44:19 +02002058 .release_port = pl011_release_port,
2059 .request_port = pl011_request_port,
2060 .config_port = pl011_config_port,
2061 .verify_port = pl011_verify_port,
Jason Wessel84b5ae12008-02-20 13:33:39 -06002062#ifdef CONFIG_CONSOLE_POLL
Anton Vorontsovb3564c22012-09-24 14:27:54 -07002063 .poll_init = pl011_hwinit,
Linus Walleije643f872012-06-17 15:44:19 +02002064 .poll_get_char = pl011_get_poll_char,
2065 .poll_put_char = pl011_put_poll_char,
Jason Wessel84b5ae12008-02-20 13:33:39 -06002066#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067};
2068
Andre Przywara0dd1e242015-05-21 17:26:23 +01002069static void sbsa_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
2070{
2071}
2072
2073static unsigned int sbsa_uart_get_mctrl(struct uart_port *port)
2074{
2075 return 0;
2076}
2077
2078static const struct uart_ops sbsa_uart_pops = {
2079 .tx_empty = pl011_tx_empty,
2080 .set_mctrl = sbsa_uart_set_mctrl,
2081 .get_mctrl = sbsa_uart_get_mctrl,
2082 .stop_tx = pl011_stop_tx,
2083 .start_tx = pl011_start_tx,
2084 .stop_rx = pl011_stop_rx,
2085 .startup = sbsa_uart_startup,
2086 .shutdown = sbsa_uart_shutdown,
2087 .set_termios = sbsa_uart_set_termios,
2088 .type = pl011_type,
2089 .release_port = pl011_release_port,
2090 .request_port = pl011_request_port,
2091 .config_port = pl011_config_port,
2092 .verify_port = pl011_verify_port,
2093#ifdef CONFIG_CONSOLE_POLL
2094 .poll_init = pl011_hwinit,
2095 .poll_get_char = pl011_get_poll_char,
2096 .poll_put_char = pl011_put_poll_char,
2097#endif
2098};
2099
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100static struct uart_amba_port *amba_ports[UART_NR];
2101
2102#ifdef CONFIG_SERIAL_AMBA_PL011_CONSOLE
2103
Russell Kingd3587882006-03-20 20:00:09 +00002104static void pl011_console_putchar(struct uart_port *port, int ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105{
Daniel Thompsona5820c22014-09-03 12:51:55 +01002106 struct uart_amba_port *uap =
2107 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108
Russell King9f25bc52015-11-03 14:51:13 +00002109 while (pl011_read(uap, REG_FR) & UART01x_FR_TXFF)
Russell Kingd3587882006-03-20 20:00:09 +00002110 barrier();
Russell King9f25bc52015-11-03 14:51:13 +00002111 pl011_write(ch, uap, REG_DR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112}
2113
2114static void
2115pl011_console_write(struct console *co, const char *s, unsigned int count)
2116{
2117 struct uart_amba_port *uap = amba_ports[co->index];
Andre Przywara71eec482015-05-21 17:26:21 +01002118 unsigned int status, old_cr = 0, new_cr;
Rabin Vincentef605fd2012-01-17 11:52:28 +01002119 unsigned long flags;
2120 int locked = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121
2122 clk_enable(uap->clk);
2123
Rabin Vincentef605fd2012-01-17 11:52:28 +01002124 local_irq_save(flags);
2125 if (uap->port.sysrq)
2126 locked = 0;
2127 else if (oops_in_progress)
2128 locked = spin_trylock(&uap->port.lock);
2129 else
2130 spin_lock(&uap->port.lock);
2131
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 /*
2133 * First save the CR then disable the interrupts
2134 */
Andre Przywara71eec482015-05-21 17:26:21 +01002135 if (!uap->vendor->always_enabled) {
Russell King9f25bc52015-11-03 14:51:13 +00002136 old_cr = pl011_read(uap, REG_CR);
Andre Przywara71eec482015-05-21 17:26:21 +01002137 new_cr = old_cr & ~UART011_CR_CTSEN;
2138 new_cr |= UART01x_CR_UARTEN | UART011_CR_TXE;
Russell King9f25bc52015-11-03 14:51:13 +00002139 pl011_write(new_cr, uap, REG_CR);
Andre Przywara71eec482015-05-21 17:26:21 +01002140 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141
Russell Kingd3587882006-03-20 20:00:09 +00002142 uart_console_write(&uap->port, s, count, pl011_console_putchar);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143
2144 /*
2145 * Finally, wait for transmitter to become empty
2146 * and restore the TCR
2147 */
2148 do {
Russell King9f25bc52015-11-03 14:51:13 +00002149 status = pl011_read(uap, REG_FR);
Greg Kroah-Hartman062a68a2015-09-04 09:11:24 -07002150 } while (status & UART01x_FR_BUSY);
Andre Przywara71eec482015-05-21 17:26:21 +01002151 if (!uap->vendor->always_enabled)
Russell King9f25bc52015-11-03 14:51:13 +00002152 pl011_write(old_cr, uap, REG_CR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153
Rabin Vincentef605fd2012-01-17 11:52:28 +01002154 if (locked)
2155 spin_unlock(&uap->port.lock);
2156 local_irq_restore(flags);
2157
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 clk_disable(uap->clk);
2159}
2160
2161static void __init
2162pl011_console_get_options(struct uart_amba_port *uap, int *baud,
2163 int *parity, int *bits)
2164{
Russell King9f25bc52015-11-03 14:51:13 +00002165 if (pl011_read(uap, REG_CR) & UART01x_CR_UARTEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 unsigned int lcr_h, ibrd, fbrd;
2167
Russell Kingb2a4e242015-11-03 14:51:03 +00002168 lcr_h = pl011_read(uap, uap->lcrh_tx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169
2170 *parity = 'n';
2171 if (lcr_h & UART01x_LCRH_PEN) {
2172 if (lcr_h & UART01x_LCRH_EPS)
2173 *parity = 'e';
2174 else
2175 *parity = 'o';
2176 }
2177
2178 if ((lcr_h & 0x60) == UART01x_LCRH_WLEN_7)
2179 *bits = 7;
2180 else
2181 *bits = 8;
2182
Russell King9f25bc52015-11-03 14:51:13 +00002183 ibrd = pl011_read(uap, REG_IBRD);
2184 fbrd = pl011_read(uap, REG_FBRD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185
2186 *baud = uap->port.uartclk * 4 / (64 * ibrd + fbrd);
Linus Walleijac3e3fb2010-06-02 20:40:22 +01002187
Russell Kingc19f12b2010-12-22 17:48:26 +00002188 if (uap->vendor->oversampling) {
Russell King9f25bc52015-11-03 14:51:13 +00002189 if (pl011_read(uap, REG_CR)
Linus Walleijac3e3fb2010-06-02 20:40:22 +01002190 & ST_UART011_CR_OVSFACT)
2191 *baud *= 2;
2192 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 }
2194}
2195
2196static int __init pl011_console_setup(struct console *co, char *options)
2197{
2198 struct uart_amba_port *uap;
2199 int baud = 38400;
2200 int bits = 8;
2201 int parity = 'n';
2202 int flow = 'n';
Russell King4b4851c2011-09-22 11:35:30 +01002203 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204
2205 /*
2206 * Check whether an invalid uart number has been specified, and
2207 * if so, search for the first available port that does have
2208 * console support.
2209 */
2210 if (co->index >= UART_NR)
2211 co->index = 0;
2212 uap = amba_ports[co->index];
Russell Kingd28122a2007-01-22 18:59:42 +00002213 if (!uap)
2214 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215
Linus Walleij78d80c52012-05-23 21:18:46 +02002216 /* Allow pins to be muxed in and configured */
Linus Walleij2b996fc2013-06-05 15:36:42 +02002217 pinctrl_pm_select_default_state(uap->port.dev);
Linus Walleij78d80c52012-05-23 21:18:46 +02002218
Russell King4b4851c2011-09-22 11:35:30 +01002219 ret = clk_prepare(uap->clk);
2220 if (ret)
2221 return ret;
2222
Jingoo Han574de552013-07-30 17:06:57 +09002223 if (dev_get_platdata(uap->port.dev)) {
Shreshtha Kumar Sahuc16d51a2011-06-13 10:11:33 +02002224 struct amba_pl011_data *plat;
2225
Jingoo Han574de552013-07-30 17:06:57 +09002226 plat = dev_get_platdata(uap->port.dev);
Shreshtha Kumar Sahuc16d51a2011-06-13 10:11:33 +02002227 if (plat->init)
2228 plat->init();
2229 }
2230
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 uap->port.uartclk = clk_get_rate(uap->clk);
2232
Andre Przywaracefc2d12015-05-21 17:26:22 +01002233 if (uap->vendor->fixed_options) {
2234 baud = uap->fixed_baud;
2235 } else {
2236 if (options)
2237 uart_parse_options(options,
2238 &baud, &parity, &bits, &flow);
2239 else
2240 pl011_console_get_options(uap, &baud, &parity, &bits);
2241 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242
2243 return uart_set_options(&uap->port, co, baud, parity, bits, flow);
2244}
2245
Vincent Sanders2d934862005-09-14 22:36:03 +01002246static struct uart_driver amba_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247static struct console amba_console = {
2248 .name = "ttyAMA",
2249 .write = pl011_console_write,
2250 .device = uart_console_device,
2251 .setup = pl011_console_setup,
2252 .flags = CON_PRINTBUFFER,
2253 .index = -1,
2254 .data = &amba_reg,
2255};
2256
2257#define AMBA_CONSOLE (&amba_console)
Rob Herring0d3c6732014-04-18 17:19:57 -05002258
2259static void pl011_putc(struct uart_port *port, int c)
2260{
Russell King9f25bc52015-11-03 14:51:13 +00002261 while (readl(port->membase + REG_FR) & UART01x_FR_TXFF)
Rob Herring0d3c6732014-04-18 17:19:57 -05002262 ;
Russell King9f25bc52015-11-03 14:51:13 +00002263 writeb(c, port->membase + REG_DR);
2264 while (readl(port->membase + REG_FR) & UART01x_FR_BUSY)
Rob Herring0d3c6732014-04-18 17:19:57 -05002265 ;
2266}
2267
2268static void pl011_early_write(struct console *con, const char *s, unsigned n)
2269{
2270 struct earlycon_device *dev = con->data;
2271
2272 uart_console_write(&dev->port, s, n, pl011_putc);
2273}
2274
2275static int __init pl011_early_console_setup(struct earlycon_device *device,
2276 const char *opt)
2277{
2278 if (!device->port.membase)
2279 return -ENODEV;
2280
2281 device->con->write = pl011_early_write;
2282 return 0;
2283}
2284EARLYCON_DECLARE(pl011, pl011_early_console_setup);
Rob Herring45e0f0f2014-03-27 08:08:03 -05002285OF_EARLYCON_DECLARE(pl011, "arm,pl011", pl011_early_console_setup);
Rob Herring0d3c6732014-04-18 17:19:57 -05002286
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287#else
2288#define AMBA_CONSOLE NULL
2289#endif
2290
2291static struct uart_driver amba_reg = {
2292 .owner = THIS_MODULE,
2293 .driver_name = "ttyAMA",
2294 .dev_name = "ttyAMA",
2295 .major = SERIAL_AMBA_MAJOR,
2296 .minor = SERIAL_AMBA_MINOR,
2297 .nr = UART_NR,
2298 .cons = AMBA_CONSOLE,
2299};
2300
Matthew Leach32614aa2012-08-28 16:41:28 +01002301static int pl011_probe_dt_alias(int index, struct device *dev)
2302{
2303 struct device_node *np;
2304 static bool seen_dev_with_alias = false;
2305 static bool seen_dev_without_alias = false;
2306 int ret = index;
2307
2308 if (!IS_ENABLED(CONFIG_OF))
2309 return ret;
2310
2311 np = dev->of_node;
2312 if (!np)
2313 return ret;
2314
2315 ret = of_alias_get_id(np, "serial");
2316 if (IS_ERR_VALUE(ret)) {
2317 seen_dev_without_alias = true;
2318 ret = index;
2319 } else {
2320 seen_dev_with_alias = true;
2321 if (ret >= ARRAY_SIZE(amba_ports) || amba_ports[ret] != NULL) {
2322 dev_warn(dev, "requested serial port %d not available.\n", ret);
2323 ret = index;
2324 }
2325 }
2326
2327 if (seen_dev_with_alias && seen_dev_without_alias)
2328 dev_warn(dev, "aliased and non-aliased serial devices found in device tree. Serial port enumeration may be unpredictable.\n");
2329
2330 return ret;
2331}
2332
Andre Przywara49bb3c82015-05-21 17:26:14 +01002333/* unregisters the driver also if no more ports are left */
2334static void pl011_unregister_port(struct uart_amba_port *uap)
2335{
2336 int i;
2337 bool busy = false;
2338
2339 for (i = 0; i < ARRAY_SIZE(amba_ports); i++) {
2340 if (amba_ports[i] == uap)
2341 amba_ports[i] = NULL;
2342 else if (amba_ports[i])
2343 busy = true;
2344 }
2345 pl011_dma_remove(uap);
2346 if (!busy)
2347 uart_unregister_driver(&amba_reg);
2348}
2349
Andre Przywara3873e2d2015-05-21 17:26:18 +01002350static int pl011_find_free_port(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351{
Andre Przywara3873e2d2015-05-21 17:26:18 +01002352 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353
2354 for (i = 0; i < ARRAY_SIZE(amba_ports); i++)
2355 if (amba_ports[i] == NULL)
Andre Przywara3873e2d2015-05-21 17:26:18 +01002356 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357
Andre Przywara3873e2d2015-05-21 17:26:18 +01002358 return -EBUSY;
2359}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360
Andre Przywara3873e2d2015-05-21 17:26:18 +01002361static int pl011_setup_port(struct device *dev, struct uart_amba_port *uap,
2362 struct resource *mmiobase, int index)
2363{
2364 void __iomem *base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365
Andre Przywara3873e2d2015-05-21 17:26:18 +01002366 base = devm_ioremap_resource(dev, mmiobase);
Krzysztof Kozlowski97a60ea2015-07-09 22:21:41 +09002367 if (IS_ERR(base))
2368 return PTR_ERR(base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369
Andre Przywara3873e2d2015-05-21 17:26:18 +01002370 index = pl011_probe_dt_alias(index, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371
Shreshtha Kumar Sahud8d8ffa2012-01-18 15:53:59 +05302372 uap->old_cr = 0;
Andre Przywara3873e2d2015-05-21 17:26:18 +01002373 uap->port.dev = dev;
2374 uap->port.mapbase = mmiobase->start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 uap->port.membase = base;
2376 uap->port.iotype = UPIO_MEM;
Russell Kingffca2b12010-12-22 17:13:05 +00002377 uap->port.fifosize = uap->fifosize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 uap->port.flags = UPF_BOOT_AUTOCONF;
Andre Przywara3873e2d2015-05-21 17:26:18 +01002379 uap->port.line = index;
2380
2381 amba_ports[index] = uap;
2382
2383 return 0;
2384}
2385
2386static int pl011_register_port(struct uart_amba_port *uap)
2387{
2388 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389
Linus Walleijc3d8b762012-03-21 20:15:18 +01002390 /* Ensure interrupts from this UART are masked and cleared */
Russell King9f25bc52015-11-03 14:51:13 +00002391 pl011_write(0, uap, REG_IMSC);
2392 pl011_write(0xffff, uap, REG_ICR);
Linus Walleijc3d8b762012-03-21 20:15:18 +01002393
Tushar Beheraef2889f2014-01-20 14:32:35 +05302394 if (!amba_reg.state) {
2395 ret = uart_register_driver(&amba_reg);
2396 if (ret < 0) {
Andre Przywara3873e2d2015-05-21 17:26:18 +01002397 dev_err(uap->port.dev,
Jorge Ramirez-Ortiz1c9be312015-03-06 13:05:40 -05002398 "Failed to register AMBA-PL011 driver\n");
Tushar Beheraef2889f2014-01-20 14:32:35 +05302399 return ret;
2400 }
2401 }
2402
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 ret = uart_add_one_port(&amba_reg, &uap->port);
Andre Przywara49bb3c82015-05-21 17:26:14 +01002404 if (ret)
2405 pl011_unregister_port(uap);
Tushar Behera7f6d9422014-06-26 15:35:35 +05302406
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407 return ret;
2408}
2409
Andre Przywara3873e2d2015-05-21 17:26:18 +01002410static int pl011_probe(struct amba_device *dev, const struct amba_id *id)
2411{
2412 struct uart_amba_port *uap;
2413 struct vendor_data *vendor = id->data;
2414 int portnr, ret;
2415
2416 portnr = pl011_find_free_port();
2417 if (portnr < 0)
2418 return portnr;
2419
2420 uap = devm_kzalloc(&dev->dev, sizeof(struct uart_amba_port),
2421 GFP_KERNEL);
2422 if (!uap)
2423 return -ENOMEM;
2424
2425 uap->clk = devm_clk_get(&dev->dev, NULL);
2426 if (IS_ERR(uap->clk))
2427 return PTR_ERR(uap->clk);
2428
Russell Kingdebb7f62015-11-16 17:40:26 +00002429 uap->reg_offset = pl011_std_offsets;
Andre Przywara3873e2d2015-05-21 17:26:18 +01002430 uap->vendor = vendor;
2431 uap->lcrh_rx = vendor->lcrh_rx;
2432 uap->lcrh_tx = vendor->lcrh_tx;
2433 uap->fifosize = vendor->get_fifosize(dev);
2434 uap->port.irq = dev->irq[0];
2435 uap->port.ops = &amba_pl011_pops;
2436
2437 snprintf(uap->type, sizeof(uap->type), "PL011 rev%u", amba_rev(dev));
2438
2439 ret = pl011_setup_port(&dev->dev, uap, &dev->res, portnr);
2440 if (ret)
2441 return ret;
2442
2443 amba_set_drvdata(dev, uap);
2444
2445 return pl011_register_port(uap);
2446}
2447
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448static int pl011_remove(struct amba_device *dev)
2449{
2450 struct uart_amba_port *uap = amba_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452 uart_remove_one_port(&amba_reg, &uap->port);
Andre Przywara49bb3c82015-05-21 17:26:14 +01002453 pl011_unregister_port(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 return 0;
2455}
2456
Ulf Hanssond0ce8502013-12-03 11:04:28 +01002457#ifdef CONFIG_PM_SLEEP
2458static int pl011_suspend(struct device *dev)
Leo Chenb736b892009-07-28 23:43:33 +01002459{
Ulf Hanssond0ce8502013-12-03 11:04:28 +01002460 struct uart_amba_port *uap = dev_get_drvdata(dev);
Leo Chenb736b892009-07-28 23:43:33 +01002461
2462 if (!uap)
2463 return -EINVAL;
2464
2465 return uart_suspend_port(&amba_reg, &uap->port);
2466}
2467
Ulf Hanssond0ce8502013-12-03 11:04:28 +01002468static int pl011_resume(struct device *dev)
Leo Chenb736b892009-07-28 23:43:33 +01002469{
Ulf Hanssond0ce8502013-12-03 11:04:28 +01002470 struct uart_amba_port *uap = dev_get_drvdata(dev);
Leo Chenb736b892009-07-28 23:43:33 +01002471
2472 if (!uap)
2473 return -EINVAL;
2474
2475 return uart_resume_port(&amba_reg, &uap->port);
2476}
2477#endif
2478
Ulf Hanssond0ce8502013-12-03 11:04:28 +01002479static SIMPLE_DEV_PM_OPS(pl011_dev_pm_ops, pl011_suspend, pl011_resume);
2480
Andre Przywara0dd1e242015-05-21 17:26:23 +01002481static int sbsa_uart_probe(struct platform_device *pdev)
2482{
2483 struct uart_amba_port *uap;
2484 struct resource *r;
2485 int portnr, ret;
2486 int baudrate;
2487
2488 /*
2489 * Check the mandatory baud rate parameter in the DT node early
2490 * so that we can easily exit with the error.
2491 */
2492 if (pdev->dev.of_node) {
2493 struct device_node *np = pdev->dev.of_node;
2494
2495 ret = of_property_read_u32(np, "current-speed", &baudrate);
2496 if (ret)
2497 return ret;
2498 } else {
2499 baudrate = 115200;
2500 }
2501
2502 portnr = pl011_find_free_port();
2503 if (portnr < 0)
2504 return portnr;
2505
2506 uap = devm_kzalloc(&pdev->dev, sizeof(struct uart_amba_port),
2507 GFP_KERNEL);
2508 if (!uap)
2509 return -ENOMEM;
2510
Russell Kingdebb7f62015-11-16 17:40:26 +00002511 uap->reg_offset = pl011_std_offsets;
Andre Przywara0dd1e242015-05-21 17:26:23 +01002512 uap->vendor = &vendor_sbsa;
2513 uap->fifosize = 32;
2514 uap->port.irq = platform_get_irq(pdev, 0);
2515 uap->port.ops = &sbsa_uart_pops;
2516 uap->fixed_baud = baudrate;
2517
2518 snprintf(uap->type, sizeof(uap->type), "SBSA");
2519
2520 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2521
2522 ret = pl011_setup_port(&pdev->dev, uap, r, portnr);
2523 if (ret)
2524 return ret;
2525
2526 platform_set_drvdata(pdev, uap);
2527
2528 return pl011_register_port(uap);
2529}
2530
2531static int sbsa_uart_remove(struct platform_device *pdev)
2532{
2533 struct uart_amba_port *uap = platform_get_drvdata(pdev);
2534
2535 uart_remove_one_port(&amba_reg, &uap->port);
2536 pl011_unregister_port(uap);
2537 return 0;
2538}
2539
2540static const struct of_device_id sbsa_uart_of_match[] = {
2541 { .compatible = "arm,sbsa-uart", },
2542 {},
2543};
2544MODULE_DEVICE_TABLE(of, sbsa_uart_of_match);
2545
Graeme Gregory3db9ab02015-05-21 17:26:24 +01002546static const struct acpi_device_id sbsa_uart_acpi_match[] = {
2547 { "ARMH0011", 0 },
2548 {},
2549};
2550MODULE_DEVICE_TABLE(acpi, sbsa_uart_acpi_match);
2551
Andre Przywara0dd1e242015-05-21 17:26:23 +01002552static struct platform_driver arm_sbsa_uart_platform_driver = {
2553 .probe = sbsa_uart_probe,
2554 .remove = sbsa_uart_remove,
2555 .driver = {
2556 .name = "sbsa-uart",
2557 .of_match_table = of_match_ptr(sbsa_uart_of_match),
Graeme Gregory3db9ab02015-05-21 17:26:24 +01002558 .acpi_match_table = ACPI_PTR(sbsa_uart_acpi_match),
Andre Przywara0dd1e242015-05-21 17:26:23 +01002559 },
2560};
2561
Russell King2c39c9e2010-07-27 08:50:16 +01002562static struct amba_id pl011_ids[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 {
2564 .id = 0x00041011,
2565 .mask = 0x000fffff,
Alessandro Rubini5926a292009-06-04 17:43:04 +01002566 .data = &vendor_arm,
2567 },
2568 {
2569 .id = 0x00380802,
2570 .mask = 0x00ffffff,
2571 .data = &vendor_st,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572 },
2573 { 0, 0 },
2574};
2575
Dave Martin60f7a332011-10-05 15:15:22 +01002576MODULE_DEVICE_TABLE(amba, pl011_ids);
2577
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578static struct amba_driver pl011_driver = {
2579 .drv = {
2580 .name = "uart-pl011",
Ulf Hanssond0ce8502013-12-03 11:04:28 +01002581 .pm = &pl011_dev_pm_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582 },
2583 .id_table = pl011_ids,
2584 .probe = pl011_probe,
2585 .remove = pl011_remove,
2586};
2587
2588static int __init pl011_init(void)
2589{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590 printk(KERN_INFO "Serial: AMBA PL011 UART driver\n");
2591
Andre Przywara0dd1e242015-05-21 17:26:23 +01002592 if (platform_driver_register(&arm_sbsa_uart_platform_driver))
2593 pr_warn("could not register SBSA UART platform driver\n");
Greg Kroah-Hartman062a68a2015-09-04 09:11:24 -07002594 return amba_driver_register(&pl011_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595}
2596
2597static void __exit pl011_exit(void)
2598{
Andre Przywara0dd1e242015-05-21 17:26:23 +01002599 platform_driver_unregister(&arm_sbsa_uart_platform_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600 amba_driver_unregister(&pl011_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601}
2602
Alessandro Rubini4dd9e742009-05-05 05:54:13 +01002603/*
2604 * While this can be a module, if builtin it's most likely the console
2605 * So let's leave module_exit but move module_init to an earlier place
2606 */
2607arch_initcall(pl011_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608module_exit(pl011_exit);
2609
2610MODULE_AUTHOR("ARM Ltd/Deep Blue Solutions Ltd");
2611MODULE_DESCRIPTION("ARM AMBA serial port driver");
2612MODULE_LICENSE("GPL");