blob: 62b9cb2754020bf9350a7edb7173b30e2dcbe2f1 [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
Alessandro Rubini5926a292009-06-04 17:43:04 +010076/* There is by now at least one vendor with differing details, so handle it */
77struct vendor_data {
78 unsigned int ifls;
Linus Walleijec489aa2010-06-02 08:13:52 +010079 unsigned int lcrh_tx;
80 unsigned int lcrh_rx;
Linus Walleijac3e3fb2010-06-02 20:40:22 +010081 bool oversampling;
Russell King38d62432010-12-22 17:59:16 +000082 bool dma_threshold;
Rajanikanth H.V4fd06902012-03-26 11:17:02 +020083 bool cts_event_workaround;
Andre Przywara71eec482015-05-21 17:26:21 +010084 bool always_enabled;
Andre Przywaracefc2d12015-05-21 17:26:22 +010085 bool fixed_options;
Jongsung Kim78506f22013-04-15 14:45:25 +090086
Jongsung Kimea336402013-05-10 18:05:35 +090087 unsigned int (*get_fifosize)(struct amba_device *dev);
Alessandro Rubini5926a292009-06-04 17:43:04 +010088};
89
Jongsung Kimea336402013-05-10 18:05:35 +090090static unsigned int get_fifosize_arm(struct amba_device *dev)
Jongsung Kim78506f22013-04-15 14:45:25 +090091{
Jongsung Kimea336402013-05-10 18:05:35 +090092 return amba_rev(dev) < 3 ? 16 : 32;
Jongsung Kim78506f22013-04-15 14:45:25 +090093}
94
Alessandro Rubini5926a292009-06-04 17:43:04 +010095static struct vendor_data vendor_arm = {
96 .ifls = UART011_IFLS_RX4_8|UART011_IFLS_TX4_8,
Russell King9f25bc52015-11-03 14:51:13 +000097 .lcrh_tx = REG_LCRH,
98 .lcrh_rx = REG_LCRH,
Linus Walleijac3e3fb2010-06-02 20:40:22 +010099 .oversampling = false,
Russell King38d62432010-12-22 17:59:16 +0000100 .dma_threshold = false,
Rajanikanth H.V4fd06902012-03-26 11:17:02 +0200101 .cts_event_workaround = false,
Andre Przywara71eec482015-05-21 17:26:21 +0100102 .always_enabled = false,
Andre Przywaracefc2d12015-05-21 17:26:22 +0100103 .fixed_options = false,
Jongsung Kim78506f22013-04-15 14:45:25 +0900104 .get_fifosize = get_fifosize_arm,
Alessandro Rubini5926a292009-06-04 17:43:04 +0100105};
106
Andre Przywara0dd1e242015-05-21 17:26:23 +0100107static struct vendor_data vendor_sbsa = {
108 .oversampling = false,
109 .dma_threshold = false,
110 .cts_event_workaround = false,
111 .always_enabled = true,
112 .fixed_options = true,
113};
114
Jongsung Kimea336402013-05-10 18:05:35 +0900115static unsigned int get_fifosize_st(struct amba_device *dev)
Jongsung Kim78506f22013-04-15 14:45:25 +0900116{
117 return 64;
118}
119
Alessandro Rubini5926a292009-06-04 17:43:04 +0100120static struct vendor_data vendor_st = {
121 .ifls = UART011_IFLS_RX_HALF|UART011_IFLS_TX_HALF,
Russell King9f25bc52015-11-03 14:51:13 +0000122 .lcrh_tx = REG_ST_LCRH_TX,
123 .lcrh_rx = REG_ST_LCRH_RX,
Linus Walleijac3e3fb2010-06-02 20:40:22 +0100124 .oversampling = true,
Russell King38d62432010-12-22 17:59:16 +0000125 .dma_threshold = true,
Rajanikanth H.V4fd06902012-03-26 11:17:02 +0200126 .cts_event_workaround = true,
Andre Przywara71eec482015-05-21 17:26:21 +0100127 .always_enabled = false,
Andre Przywaracefc2d12015-05-21 17:26:22 +0100128 .fixed_options = false,
Jongsung Kim78506f22013-04-15 14:45:25 +0900129 .get_fifosize = get_fifosize_st,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130};
131
Russell King68b65f72010-12-22 17:24:39 +0000132/* Deals with DMA transactions */
Linus Walleijead76f32011-02-24 13:21:08 +0100133
134struct pl011_sgbuf {
135 struct scatterlist sg;
136 char *buf;
137};
138
139struct pl011_dmarx_data {
140 struct dma_chan *chan;
141 struct completion complete;
142 bool use_buf_b;
143 struct pl011_sgbuf sgbuf_a;
144 struct pl011_sgbuf sgbuf_b;
145 dma_cookie_t cookie;
146 bool running;
Chanho Mincb06ff12013-03-27 18:38:11 +0900147 struct timer_list timer;
148 unsigned int last_residue;
149 unsigned long last_jiffies;
150 bool auto_poll_rate;
151 unsigned int poll_rate;
152 unsigned int poll_timeout;
Linus Walleijead76f32011-02-24 13:21:08 +0100153};
154
Russell King68b65f72010-12-22 17:24:39 +0000155struct pl011_dmatx_data {
156 struct dma_chan *chan;
157 struct scatterlist sg;
158 char *buf;
159 bool queued;
160};
161
Russell Kingc19f12b2010-12-22 17:48:26 +0000162/*
163 * We wrap our port structure around the generic uart_port.
164 */
165struct uart_amba_port {
166 struct uart_port port;
167 struct clk *clk;
168 const struct vendor_data *vendor;
Russell King68b65f72010-12-22 17:24:39 +0000169 unsigned int dmacr; /* dma control reg */
Russell Kingc19f12b2010-12-22 17:48:26 +0000170 unsigned int im; /* interrupt mask */
171 unsigned int old_status;
Russell Kingffca2b12010-12-22 17:13:05 +0000172 unsigned int fifosize; /* vendor-specific */
Russell Kingc19f12b2010-12-22 17:48:26 +0000173 unsigned int lcrh_tx; /* vendor-specific */
174 unsigned int lcrh_rx; /* vendor-specific */
Shreshtha Kumar Sahud8d8ffa2012-01-18 15:53:59 +0530175 unsigned int old_cr; /* state during shutdown */
Russell Kingc19f12b2010-12-22 17:48:26 +0000176 bool autorts;
Andre Przywaracefc2d12015-05-21 17:26:22 +0100177 unsigned int fixed_baud; /* vendor-set fixed baud rate */
Russell Kingc19f12b2010-12-22 17:48:26 +0000178 char type[12];
Russell King68b65f72010-12-22 17:24:39 +0000179#ifdef CONFIG_DMA_ENGINE
180 /* DMA stuff */
Linus Walleijead76f32011-02-24 13:21:08 +0100181 bool using_tx_dma;
182 bool using_rx_dma;
183 struct pl011_dmarx_data dmarx;
Russell King68b65f72010-12-22 17:24:39 +0000184 struct pl011_dmatx_data dmatx;
Jorge Ramirez-Ortiz1c9be312015-03-06 13:05:40 -0500185 bool dma_probed;
Russell King68b65f72010-12-22 17:24:39 +0000186#endif
Russell Kingc19f12b2010-12-22 17:48:26 +0000187};
188
Russell King9f25bc52015-11-03 14:51:13 +0000189static unsigned int pl011_reg_to_offset(const struct uart_amba_port *uap,
190 unsigned int reg)
191{
192 return reg;
193}
194
Russell Kingb2a4e242015-11-03 14:51:03 +0000195static unsigned int pl011_read(const struct uart_amba_port *uap,
196 unsigned int reg)
Russell King75836332015-11-03 14:50:58 +0000197{
Russell King9f25bc52015-11-03 14:51:13 +0000198 return readw(uap->port.membase + pl011_reg_to_offset(uap, reg));
Russell King75836332015-11-03 14:50:58 +0000199}
200
Russell Kingb2a4e242015-11-03 14:51:03 +0000201static void pl011_write(unsigned int val, const struct uart_amba_port *uap,
202 unsigned int reg)
Russell King75836332015-11-03 14:50:58 +0000203{
Russell King9f25bc52015-11-03 14:51:13 +0000204 writew(val, uap->port.membase + pl011_reg_to_offset(uap, reg));
Russell King75836332015-11-03 14:50:58 +0000205}
206
Russell King68b65f72010-12-22 17:24:39 +0000207/*
Linus Walleij29772c42011-02-24 13:21:36 +0100208 * Reads up to 256 characters from the FIFO or until it's empty and
209 * inserts them into the TTY layer. Returns the number of characters
210 * read from the FIFO.
211 */
212static int pl011_fifo_to_tty(struct uart_amba_port *uap)
213{
Timur Tabi71a5cd82015-10-07 15:27:16 -0500214 u16 status;
215 unsigned int ch, flag, max_count = 256;
Linus Walleij29772c42011-02-24 13:21:36 +0100216 int fifotaken = 0;
217
218 while (max_count--) {
Russell King9f25bc52015-11-03 14:51:13 +0000219 status = pl011_read(uap, REG_FR);
Linus Walleij29772c42011-02-24 13:21:36 +0100220 if (status & UART01x_FR_RXFE)
221 break;
222
223 /* Take chars from the FIFO and update status */
Russell King9f25bc52015-11-03 14:51:13 +0000224 ch = pl011_read(uap, REG_DR) | UART_DUMMY_DR_RX;
Linus Walleij29772c42011-02-24 13:21:36 +0100225 flag = TTY_NORMAL;
226 uap->port.icount.rx++;
227 fifotaken++;
228
229 if (unlikely(ch & UART_DR_ERROR)) {
230 if (ch & UART011_DR_BE) {
231 ch &= ~(UART011_DR_FE | UART011_DR_PE);
232 uap->port.icount.brk++;
233 if (uart_handle_break(&uap->port))
234 continue;
235 } else if (ch & UART011_DR_PE)
236 uap->port.icount.parity++;
237 else if (ch & UART011_DR_FE)
238 uap->port.icount.frame++;
239 if (ch & UART011_DR_OE)
240 uap->port.icount.overrun++;
241
242 ch &= uap->port.read_status_mask;
243
244 if (ch & UART011_DR_BE)
245 flag = TTY_BREAK;
246 else if (ch & UART011_DR_PE)
247 flag = TTY_PARITY;
248 else if (ch & UART011_DR_FE)
249 flag = TTY_FRAME;
250 }
251
252 if (uart_handle_sysrq_char(&uap->port, ch & 255))
253 continue;
254
255 uart_insert_char(&uap->port, ch, UART011_DR_OE, ch, flag);
256 }
257
258 return fifotaken;
259}
260
261
262/*
Russell King68b65f72010-12-22 17:24:39 +0000263 * All the DMA operation mode stuff goes inside this ifdef.
264 * This assumes that you have a generic DMA device interface,
265 * no custom DMA interfaces are supported.
266 */
267#ifdef CONFIG_DMA_ENGINE
268
269#define PL011_DMA_BUFFER_SIZE PAGE_SIZE
270
Linus Walleijead76f32011-02-24 13:21:08 +0100271static int pl011_sgbuf_init(struct dma_chan *chan, struct pl011_sgbuf *sg,
272 enum dma_data_direction dir)
273{
Chanho Mincb06ff12013-03-27 18:38:11 +0900274 dma_addr_t dma_addr;
275
276 sg->buf = dma_alloc_coherent(chan->device->dev,
277 PL011_DMA_BUFFER_SIZE, &dma_addr, GFP_KERNEL);
Linus Walleijead76f32011-02-24 13:21:08 +0100278 if (!sg->buf)
279 return -ENOMEM;
280
Chanho Mincb06ff12013-03-27 18:38:11 +0900281 sg_init_table(&sg->sg, 1);
282 sg_set_page(&sg->sg, phys_to_page(dma_addr),
283 PL011_DMA_BUFFER_SIZE, offset_in_page(dma_addr));
284 sg_dma_address(&sg->sg) = dma_addr;
Andrew Jacksonc64be922014-11-07 14:14:43 +0000285 sg_dma_len(&sg->sg) = PL011_DMA_BUFFER_SIZE;
Linus Walleijead76f32011-02-24 13:21:08 +0100286
Linus Walleijead76f32011-02-24 13:21:08 +0100287 return 0;
288}
289
290static void pl011_sgbuf_free(struct dma_chan *chan, struct pl011_sgbuf *sg,
291 enum dma_data_direction dir)
292{
293 if (sg->buf) {
Chanho Mincb06ff12013-03-27 18:38:11 +0900294 dma_free_coherent(chan->device->dev,
295 PL011_DMA_BUFFER_SIZE, sg->buf,
296 sg_dma_address(&sg->sg));
Linus Walleijead76f32011-02-24 13:21:08 +0100297 }
298}
299
Jorge Ramirez-Ortiz1c9be312015-03-06 13:05:40 -0500300static void pl011_dma_probe(struct uart_amba_port *uap)
Russell King68b65f72010-12-22 17:24:39 +0000301{
302 /* DMA is the sole user of the platform data right now */
Jingoo Han574de552013-07-30 17:06:57 +0900303 struct amba_pl011_data *plat = dev_get_platdata(uap->port.dev);
Jorge Ramirez-Ortiz1c9be312015-03-06 13:05:40 -0500304 struct device *dev = uap->port.dev;
Russell King68b65f72010-12-22 17:24:39 +0000305 struct dma_slave_config tx_conf = {
Russell King9f25bc52015-11-03 14:51:13 +0000306 .dst_addr = uap->port.mapbase +
307 pl011_reg_to_offset(uap, REG_DR),
Russell King68b65f72010-12-22 17:24:39 +0000308 .dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE,
Vinod Koula485df42011-10-14 10:47:38 +0530309 .direction = DMA_MEM_TO_DEV,
Russell King68b65f72010-12-22 17:24:39 +0000310 .dst_maxburst = uap->fifosize >> 1,
Viresh Kumar258aea72012-02-01 16:12:19 +0530311 .device_fc = false,
Russell King68b65f72010-12-22 17:24:39 +0000312 };
313 struct dma_chan *chan;
314 dma_cap_mask_t mask;
315
Jorge Ramirez-Ortiz1c9be312015-03-06 13:05:40 -0500316 uap->dma_probed = true;
317 chan = dma_request_slave_channel_reason(dev, "tx");
318 if (IS_ERR(chan)) {
319 if (PTR_ERR(chan) == -EPROBE_DEFER) {
Jorge Ramirez-Ortiz1c9be312015-03-06 13:05:40 -0500320 uap->dma_probed = false;
321 return;
322 }
Russell King68b65f72010-12-22 17:24:39 +0000323
Arnd Bergmann787b0c12013-01-28 16:24:37 +0000324 /* We need platform data */
325 if (!plat || !plat->dma_filter) {
326 dev_info(uap->port.dev, "no DMA platform data\n");
327 return;
328 }
329
330 /* Try to acquire a generic DMA engine slave TX channel */
331 dma_cap_zero(mask);
332 dma_cap_set(DMA_SLAVE, mask);
333
334 chan = dma_request_channel(mask, plat->dma_filter,
335 plat->dma_tx_param);
336 if (!chan) {
337 dev_err(uap->port.dev, "no TX DMA channel!\n");
338 return;
339 }
Russell King68b65f72010-12-22 17:24:39 +0000340 }
341
342 dmaengine_slave_config(chan, &tx_conf);
343 uap->dmatx.chan = chan;
344
345 dev_info(uap->port.dev, "DMA channel TX %s\n",
346 dma_chan_name(uap->dmatx.chan));
Linus Walleijead76f32011-02-24 13:21:08 +0100347
348 /* Optionally make use of an RX channel as well */
Arnd Bergmann787b0c12013-01-28 16:24:37 +0000349 chan = dma_request_slave_channel(dev, "rx");
Rob Herring0d3c6732014-04-18 17:19:57 -0500350
Arnd Bergmann787b0c12013-01-28 16:24:37 +0000351 if (!chan && plat->dma_rx_param) {
352 chan = dma_request_channel(mask, plat->dma_filter, plat->dma_rx_param);
353
354 if (!chan) {
355 dev_err(uap->port.dev, "no RX DMA channel!\n");
356 return;
357 }
358 }
359
360 if (chan) {
Linus Walleijead76f32011-02-24 13:21:08 +0100361 struct dma_slave_config rx_conf = {
Russell King9f25bc52015-11-03 14:51:13 +0000362 .src_addr = uap->port.mapbase +
363 pl011_reg_to_offset(uap, REG_DR),
Linus Walleijead76f32011-02-24 13:21:08 +0100364 .src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE,
Vinod Koula485df42011-10-14 10:47:38 +0530365 .direction = DMA_DEV_TO_MEM,
Guennadi Liakhovetskib2aeb772014-04-12 19:47:17 +0200366 .src_maxburst = uap->fifosize >> 2,
Viresh Kumar258aea72012-02-01 16:12:19 +0530367 .device_fc = false,
Linus Walleijead76f32011-02-24 13:21:08 +0100368 };
Andrew Jackson2d3b7d62014-11-07 14:14:47 +0000369 struct dma_slave_caps caps;
Linus Walleijead76f32011-02-24 13:21:08 +0100370
Andrew Jackson2d3b7d62014-11-07 14:14:47 +0000371 /*
372 * Some DMA controllers provide information on their capabilities.
373 * If the controller does, check for suitable residue processing
374 * otherwise assime all is well.
375 */
376 if (0 == dma_get_slave_caps(chan, &caps)) {
377 if (caps.residue_granularity ==
378 DMA_RESIDUE_GRANULARITY_DESCRIPTOR) {
379 dma_release_channel(chan);
380 dev_info(uap->port.dev,
381 "RX DMA disabled - no residue processing\n");
382 return;
383 }
384 }
Linus Walleijead76f32011-02-24 13:21:08 +0100385 dmaengine_slave_config(chan, &rx_conf);
386 uap->dmarx.chan = chan;
387
Andrew Jackson98267d32014-11-07 14:14:23 +0000388 uap->dmarx.auto_poll_rate = false;
Greg Kroah-Hartman8f898bf2013-12-17 09:33:18 -0800389 if (plat && plat->dma_rx_poll_enable) {
Chanho Mincb06ff12013-03-27 18:38:11 +0900390 /* Set poll rate if specified. */
391 if (plat->dma_rx_poll_rate) {
392 uap->dmarx.auto_poll_rate = false;
393 uap->dmarx.poll_rate = plat->dma_rx_poll_rate;
394 } else {
395 /*
396 * 100 ms defaults to poll rate if not
397 * specified. This will be adjusted with
398 * the baud rate at set_termios.
399 */
400 uap->dmarx.auto_poll_rate = true;
401 uap->dmarx.poll_rate = 100;
402 }
403 /* 3 secs defaults poll_timeout if not specified. */
404 if (plat->dma_rx_poll_timeout)
405 uap->dmarx.poll_timeout =
406 plat->dma_rx_poll_timeout;
407 else
408 uap->dmarx.poll_timeout = 3000;
Andrew Jackson98267d32014-11-07 14:14:23 +0000409 } else if (!plat && dev->of_node) {
410 uap->dmarx.auto_poll_rate = of_property_read_bool(
411 dev->of_node, "auto-poll");
412 if (uap->dmarx.auto_poll_rate) {
413 u32 x;
Chanho Mincb06ff12013-03-27 18:38:11 +0900414
Andrew Jackson98267d32014-11-07 14:14:23 +0000415 if (0 == of_property_read_u32(dev->of_node,
416 "poll-rate-ms", &x))
417 uap->dmarx.poll_rate = x;
418 else
419 uap->dmarx.poll_rate = 100;
420 if (0 == of_property_read_u32(dev->of_node,
421 "poll-timeout-ms", &x))
422 uap->dmarx.poll_timeout = x;
423 else
424 uap->dmarx.poll_timeout = 3000;
425 }
426 }
Linus Walleijead76f32011-02-24 13:21:08 +0100427 dev_info(uap->port.dev, "DMA channel RX %s\n",
428 dma_chan_name(uap->dmarx.chan));
429 }
Russell King68b65f72010-12-22 17:24:39 +0000430}
431
Russell King68b65f72010-12-22 17:24:39 +0000432static void pl011_dma_remove(struct uart_amba_port *uap)
433{
Russell King68b65f72010-12-22 17:24:39 +0000434 if (uap->dmatx.chan)
435 dma_release_channel(uap->dmatx.chan);
Linus Walleijead76f32011-02-24 13:21:08 +0100436 if (uap->dmarx.chan)
437 dma_release_channel(uap->dmarx.chan);
Russell King68b65f72010-12-22 17:24:39 +0000438}
439
Dave Martin734745c2015-03-04 12:27:33 +0000440/* Forward declare these for the refill routine */
Russell King68b65f72010-12-22 17:24:39 +0000441static int pl011_dma_tx_refill(struct uart_amba_port *uap);
Dave Martin734745c2015-03-04 12:27:33 +0000442static void pl011_start_tx_pio(struct uart_amba_port *uap);
Russell King68b65f72010-12-22 17:24:39 +0000443
444/*
445 * The current DMA TX buffer has been sent.
446 * Try to queue up another DMA buffer.
447 */
448static void pl011_dma_tx_callback(void *data)
449{
450 struct uart_amba_port *uap = data;
451 struct pl011_dmatx_data *dmatx = &uap->dmatx;
452 unsigned long flags;
453 u16 dmacr;
454
455 spin_lock_irqsave(&uap->port.lock, flags);
456 if (uap->dmatx.queued)
457 dma_unmap_sg(dmatx->chan->device->dev, &dmatx->sg, 1,
458 DMA_TO_DEVICE);
459
460 dmacr = uap->dmacr;
461 uap->dmacr = dmacr & ~UART011_TXDMAE;
Russell King9f25bc52015-11-03 14:51:13 +0000462 pl011_write(uap->dmacr, uap, REG_DMACR);
Russell King68b65f72010-12-22 17:24:39 +0000463
464 /*
465 * If TX DMA was disabled, it means that we've stopped the DMA for
466 * some reason (eg, XOFF received, or we want to send an X-char.)
467 *
468 * Note: we need to be careful here of a potential race between DMA
469 * and the rest of the driver - if the driver disables TX DMA while
470 * a TX buffer completing, we must update the tx queued status to
471 * get further refills (hence we check dmacr).
472 */
473 if (!(dmacr & UART011_TXDMAE) || uart_tx_stopped(&uap->port) ||
474 uart_circ_empty(&uap->port.state->xmit)) {
475 uap->dmatx.queued = false;
476 spin_unlock_irqrestore(&uap->port.lock, flags);
477 return;
478 }
479
Dave Martin734745c2015-03-04 12:27:33 +0000480 if (pl011_dma_tx_refill(uap) <= 0)
Russell King68b65f72010-12-22 17:24:39 +0000481 /*
482 * We didn't queue a DMA buffer for some reason, but we
483 * have data pending to be sent. Re-enable the TX IRQ.
484 */
Dave Martin734745c2015-03-04 12:27:33 +0000485 pl011_start_tx_pio(uap);
486
Russell King68b65f72010-12-22 17:24:39 +0000487 spin_unlock_irqrestore(&uap->port.lock, flags);
488}
489
490/*
491 * Try to refill the TX DMA buffer.
492 * Locking: called with port lock held and IRQs disabled.
493 * Returns:
494 * 1 if we queued up a TX DMA buffer.
495 * 0 if we didn't want to handle this by DMA
496 * <0 on error
497 */
498static int pl011_dma_tx_refill(struct uart_amba_port *uap)
499{
500 struct pl011_dmatx_data *dmatx = &uap->dmatx;
501 struct dma_chan *chan = dmatx->chan;
502 struct dma_device *dma_dev = chan->device;
503 struct dma_async_tx_descriptor *desc;
504 struct circ_buf *xmit = &uap->port.state->xmit;
505 unsigned int count;
506
507 /*
508 * Try to avoid the overhead involved in using DMA if the
509 * transaction fits in the first half of the FIFO, by using
510 * the standard interrupt handling. This ensures that we
511 * issue a uart_write_wakeup() at the appropriate time.
512 */
513 count = uart_circ_chars_pending(xmit);
514 if (count < (uap->fifosize >> 1)) {
515 uap->dmatx.queued = false;
516 return 0;
517 }
518
519 /*
520 * Bodge: don't send the last character by DMA, as this
521 * will prevent XON from notifying us to restart DMA.
522 */
523 count -= 1;
524
525 /* Else proceed to copy the TX chars to the DMA buffer and fire DMA */
526 if (count > PL011_DMA_BUFFER_SIZE)
527 count = PL011_DMA_BUFFER_SIZE;
528
529 if (xmit->tail < xmit->head)
530 memcpy(&dmatx->buf[0], &xmit->buf[xmit->tail], count);
531 else {
532 size_t first = UART_XMIT_SIZE - xmit->tail;
Andrew Jacksone2a545a2014-11-07 14:14:39 +0000533 size_t second;
534
535 if (first > count)
536 first = count;
537 second = count - first;
Russell King68b65f72010-12-22 17:24:39 +0000538
539 memcpy(&dmatx->buf[0], &xmit->buf[xmit->tail], first);
540 if (second)
541 memcpy(&dmatx->buf[first], &xmit->buf[0], second);
542 }
543
544 dmatx->sg.length = count;
545
546 if (dma_map_sg(dma_dev->dev, &dmatx->sg, 1, DMA_TO_DEVICE) != 1) {
547 uap->dmatx.queued = false;
548 dev_dbg(uap->port.dev, "unable to map TX DMA\n");
549 return -EBUSY;
550 }
551
Alexandre Bounine16052822012-03-08 16:11:18 -0500552 desc = dmaengine_prep_slave_sg(chan, &dmatx->sg, 1, DMA_MEM_TO_DEV,
Russell King68b65f72010-12-22 17:24:39 +0000553 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
554 if (!desc) {
555 dma_unmap_sg(dma_dev->dev, &dmatx->sg, 1, DMA_TO_DEVICE);
556 uap->dmatx.queued = false;
557 /*
558 * If DMA cannot be used right now, we complete this
559 * transaction via IRQ and let the TTY layer retry.
560 */
561 dev_dbg(uap->port.dev, "TX DMA busy\n");
562 return -EBUSY;
563 }
564
565 /* Some data to go along to the callback */
566 desc->callback = pl011_dma_tx_callback;
567 desc->callback_param = uap;
568
569 /* All errors should happen at prepare time */
570 dmaengine_submit(desc);
571
572 /* Fire the DMA transaction */
573 dma_dev->device_issue_pending(chan);
574
575 uap->dmacr |= UART011_TXDMAE;
Russell King9f25bc52015-11-03 14:51:13 +0000576 pl011_write(uap->dmacr, uap, REG_DMACR);
Russell King68b65f72010-12-22 17:24:39 +0000577 uap->dmatx.queued = true;
578
579 /*
580 * Now we know that DMA will fire, so advance the ring buffer
581 * with the stuff we just dispatched.
582 */
583 xmit->tail = (xmit->tail + count) & (UART_XMIT_SIZE - 1);
584 uap->port.icount.tx += count;
585
586 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
587 uart_write_wakeup(&uap->port);
588
589 return 1;
590}
591
592/*
593 * We received a transmit interrupt without a pending X-char but with
594 * pending characters.
595 * Locking: called with port lock held and IRQs disabled.
596 * Returns:
597 * false if we want to use PIO to transmit
598 * true if we queued a DMA buffer
599 */
600static bool pl011_dma_tx_irq(struct uart_amba_port *uap)
601{
Linus Walleijead76f32011-02-24 13:21:08 +0100602 if (!uap->using_tx_dma)
Russell King68b65f72010-12-22 17:24:39 +0000603 return false;
604
605 /*
606 * If we already have a TX buffer queued, but received a
607 * TX interrupt, it will be because we've just sent an X-char.
608 * Ensure the TX DMA is enabled and the TX IRQ is disabled.
609 */
610 if (uap->dmatx.queued) {
611 uap->dmacr |= UART011_TXDMAE;
Russell King9f25bc52015-11-03 14:51:13 +0000612 pl011_write(uap->dmacr, uap, REG_DMACR);
Russell King68b65f72010-12-22 17:24:39 +0000613 uap->im &= ~UART011_TXIM;
Russell King9f25bc52015-11-03 14:51:13 +0000614 pl011_write(uap->im, uap, REG_IMSC);
Russell King68b65f72010-12-22 17:24:39 +0000615 return true;
616 }
617
618 /*
619 * We don't have a TX buffer queued, so try to queue one.
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300620 * If we successfully queued a buffer, mask the TX IRQ.
Russell King68b65f72010-12-22 17:24:39 +0000621 */
622 if (pl011_dma_tx_refill(uap) > 0) {
623 uap->im &= ~UART011_TXIM;
Russell King9f25bc52015-11-03 14:51:13 +0000624 pl011_write(uap->im, uap, REG_IMSC);
Russell King68b65f72010-12-22 17:24:39 +0000625 return true;
626 }
627 return false;
628}
629
630/*
631 * Stop the DMA transmit (eg, due to received XOFF).
632 * Locking: called with port lock held and IRQs disabled.
633 */
634static inline void pl011_dma_tx_stop(struct uart_amba_port *uap)
635{
636 if (uap->dmatx.queued) {
637 uap->dmacr &= ~UART011_TXDMAE;
Russell King9f25bc52015-11-03 14:51:13 +0000638 pl011_write(uap->dmacr, uap, REG_DMACR);
Russell King68b65f72010-12-22 17:24:39 +0000639 }
640}
641
642/*
643 * Try to start a DMA transmit, or in the case of an XON/OFF
644 * character queued for send, try to get that character out ASAP.
645 * Locking: called with port lock held and IRQs disabled.
646 * Returns:
647 * false if we want the TX IRQ to be enabled
648 * true if we have a buffer queued
649 */
650static inline bool pl011_dma_tx_start(struct uart_amba_port *uap)
651{
652 u16 dmacr;
653
Linus Walleijead76f32011-02-24 13:21:08 +0100654 if (!uap->using_tx_dma)
Russell King68b65f72010-12-22 17:24:39 +0000655 return false;
656
657 if (!uap->port.x_char) {
658 /* no X-char, try to push chars out in DMA mode */
659 bool ret = true;
660
661 if (!uap->dmatx.queued) {
662 if (pl011_dma_tx_refill(uap) > 0) {
663 uap->im &= ~UART011_TXIM;
Russell King9f25bc52015-11-03 14:51:13 +0000664 pl011_write(uap->im, uap, REG_IMSC);
Dave Martin734745c2015-03-04 12:27:33 +0000665 } else
Russell King68b65f72010-12-22 17:24:39 +0000666 ret = false;
Russell King68b65f72010-12-22 17:24:39 +0000667 } else if (!(uap->dmacr & UART011_TXDMAE)) {
668 uap->dmacr |= UART011_TXDMAE;
Russell King9f25bc52015-11-03 14:51:13 +0000669 pl011_write(uap->dmacr, uap, REG_DMACR);
Russell King68b65f72010-12-22 17:24:39 +0000670 }
671 return ret;
672 }
673
674 /*
675 * We have an X-char to send. Disable DMA to prevent it loading
676 * the TX fifo, and then see if we can stuff it into the FIFO.
677 */
678 dmacr = uap->dmacr;
679 uap->dmacr &= ~UART011_TXDMAE;
Russell King9f25bc52015-11-03 14:51:13 +0000680 pl011_write(uap->dmacr, uap, REG_DMACR);
Russell King68b65f72010-12-22 17:24:39 +0000681
Russell King9f25bc52015-11-03 14:51:13 +0000682 if (pl011_read(uap, REG_FR) & UART01x_FR_TXFF) {
Russell King68b65f72010-12-22 17:24:39 +0000683 /*
684 * No space in the FIFO, so enable the transmit interrupt
685 * so we know when there is space. Note that once we've
686 * loaded the character, we should just re-enable DMA.
687 */
688 return false;
689 }
690
Russell King9f25bc52015-11-03 14:51:13 +0000691 pl011_write(uap->port.x_char, uap, REG_DR);
Russell King68b65f72010-12-22 17:24:39 +0000692 uap->port.icount.tx++;
693 uap->port.x_char = 0;
694
695 /* Success - restore the DMA state */
696 uap->dmacr = dmacr;
Russell King9f25bc52015-11-03 14:51:13 +0000697 pl011_write(dmacr, uap, REG_DMACR);
Russell King68b65f72010-12-22 17:24:39 +0000698
699 return true;
700}
701
702/*
703 * Flush the transmit buffer.
704 * Locking: called with port lock held and IRQs disabled.
705 */
706static void pl011_dma_flush_buffer(struct uart_port *port)
Fabio Estevamb83286b2013-08-09 17:58:51 -0300707__releases(&uap->port.lock)
708__acquires(&uap->port.lock)
Russell King68b65f72010-12-22 17:24:39 +0000709{
Daniel Thompsona5820c22014-09-03 12:51:55 +0100710 struct uart_amba_port *uap =
711 container_of(port, struct uart_amba_port, port);
Russell King68b65f72010-12-22 17:24:39 +0000712
Linus Walleijead76f32011-02-24 13:21:08 +0100713 if (!uap->using_tx_dma)
Russell King68b65f72010-12-22 17:24:39 +0000714 return;
715
716 /* Avoid deadlock with the DMA engine callback */
717 spin_unlock(&uap->port.lock);
718 dmaengine_terminate_all(uap->dmatx.chan);
719 spin_lock(&uap->port.lock);
720 if (uap->dmatx.queued) {
721 dma_unmap_sg(uap->dmatx.chan->device->dev, &uap->dmatx.sg, 1,
722 DMA_TO_DEVICE);
723 uap->dmatx.queued = false;
724 uap->dmacr &= ~UART011_TXDMAE;
Russell King9f25bc52015-11-03 14:51:13 +0000725 pl011_write(uap->dmacr, uap, REG_DMACR);
Russell King68b65f72010-12-22 17:24:39 +0000726 }
727}
728
Linus Walleijead76f32011-02-24 13:21:08 +0100729static void pl011_dma_rx_callback(void *data);
730
731static int pl011_dma_rx_trigger_dma(struct uart_amba_port *uap)
732{
733 struct dma_chan *rxchan = uap->dmarx.chan;
Linus Walleijead76f32011-02-24 13:21:08 +0100734 struct pl011_dmarx_data *dmarx = &uap->dmarx;
735 struct dma_async_tx_descriptor *desc;
736 struct pl011_sgbuf *sgbuf;
737
738 if (!rxchan)
739 return -EIO;
740
741 /* Start the RX DMA job */
742 sgbuf = uap->dmarx.use_buf_b ?
743 &uap->dmarx.sgbuf_b : &uap->dmarx.sgbuf_a;
Alexandre Bounine16052822012-03-08 16:11:18 -0500744 desc = dmaengine_prep_slave_sg(rxchan, &sgbuf->sg, 1,
Vinod Koula485df42011-10-14 10:47:38 +0530745 DMA_DEV_TO_MEM,
Linus Walleijead76f32011-02-24 13:21:08 +0100746 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
747 /*
748 * If the DMA engine is busy and cannot prepare a
749 * channel, no big deal, the driver will fall back
750 * to interrupt mode as a result of this error code.
751 */
752 if (!desc) {
753 uap->dmarx.running = false;
754 dmaengine_terminate_all(rxchan);
755 return -EBUSY;
756 }
757
758 /* Some data to go along to the callback */
759 desc->callback = pl011_dma_rx_callback;
760 desc->callback_param = uap;
761 dmarx->cookie = dmaengine_submit(desc);
762 dma_async_issue_pending(rxchan);
763
764 uap->dmacr |= UART011_RXDMAE;
Russell King9f25bc52015-11-03 14:51:13 +0000765 pl011_write(uap->dmacr, uap, REG_DMACR);
Linus Walleijead76f32011-02-24 13:21:08 +0100766 uap->dmarx.running = true;
767
768 uap->im &= ~UART011_RXIM;
Russell King9f25bc52015-11-03 14:51:13 +0000769 pl011_write(uap->im, uap, REG_IMSC);
Linus Walleijead76f32011-02-24 13:21:08 +0100770
771 return 0;
772}
773
774/*
775 * This is called when either the DMA job is complete, or
776 * the FIFO timeout interrupt occurred. This must be called
777 * with the port spinlock uap->port.lock held.
778 */
779static void pl011_dma_rx_chars(struct uart_amba_port *uap,
780 u32 pending, bool use_buf_b,
781 bool readfifo)
782{
Jiri Slaby05c7cd32013-01-03 15:53:04 +0100783 struct tty_port *port = &uap->port.state->port;
Linus Walleijead76f32011-02-24 13:21:08 +0100784 struct pl011_sgbuf *sgbuf = use_buf_b ?
785 &uap->dmarx.sgbuf_b : &uap->dmarx.sgbuf_a;
Linus Walleijead76f32011-02-24 13:21:08 +0100786 int dma_count = 0;
787 u32 fifotaken = 0; /* only used for vdbg() */
788
Chanho Mincb06ff12013-03-27 18:38:11 +0900789 struct pl011_dmarx_data *dmarx = &uap->dmarx;
790 int dmataken = 0;
791
792 if (uap->dmarx.poll_rate) {
793 /* The data can be taken by polling */
794 dmataken = sgbuf->sg.length - dmarx->last_residue;
795 /* Recalculate the pending size */
796 if (pending >= dmataken)
797 pending -= dmataken;
798 }
799
800 /* Pick the remain data from the DMA */
Linus Walleijead76f32011-02-24 13:21:08 +0100801 if (pending) {
Linus Walleijead76f32011-02-24 13:21:08 +0100802
803 /*
804 * First take all chars in the DMA pipe, then look in the FIFO.
805 * Note that tty_insert_flip_buf() tries to take as many chars
806 * as it can.
807 */
Chanho Mincb06ff12013-03-27 18:38:11 +0900808 dma_count = tty_insert_flip_string(port, sgbuf->buf + dmataken,
809 pending);
Linus Walleijead76f32011-02-24 13:21:08 +0100810
811 uap->port.icount.rx += dma_count;
812 if (dma_count < pending)
813 dev_warn(uap->port.dev,
814 "couldn't insert all characters (TTY is full?)\n");
815 }
816
Chanho Mincb06ff12013-03-27 18:38:11 +0900817 /* Reset the last_residue for Rx DMA poll */
818 if (uap->dmarx.poll_rate)
819 dmarx->last_residue = sgbuf->sg.length;
820
Linus Walleijead76f32011-02-24 13:21:08 +0100821 /*
822 * Only continue with trying to read the FIFO if all DMA chars have
823 * been taken first.
824 */
825 if (dma_count == pending && readfifo) {
826 /* Clear any error flags */
Russell King75836332015-11-03 14:50:58 +0000827 pl011_write(UART011_OEIS | UART011_BEIS | UART011_PEIS |
Russell King9f25bc52015-11-03 14:51:13 +0000828 UART011_FEIS, uap, REG_ICR);
Linus Walleijead76f32011-02-24 13:21:08 +0100829
830 /*
831 * If we read all the DMA'd characters, and we had an
Linus Walleij29772c42011-02-24 13:21:36 +0100832 * incomplete buffer, that could be due to an rx error, or
833 * maybe we just timed out. Read any pending chars and check
834 * the error status.
835 *
836 * Error conditions will only occur in the FIFO, these will
837 * trigger an immediate interrupt and stop the DMA job, so we
838 * will always find the error in the FIFO, never in the DMA
839 * buffer.
Linus Walleijead76f32011-02-24 13:21:08 +0100840 */
Linus Walleij29772c42011-02-24 13:21:36 +0100841 fifotaken = pl011_fifo_to_tty(uap);
Linus Walleijead76f32011-02-24 13:21:08 +0100842 }
843
844 spin_unlock(&uap->port.lock);
845 dev_vdbg(uap->port.dev,
846 "Took %d chars from DMA buffer and %d chars from the FIFO\n",
847 dma_count, fifotaken);
Jiri Slaby2e124b42013-01-03 15:53:06 +0100848 tty_flip_buffer_push(port);
Linus Walleijead76f32011-02-24 13:21:08 +0100849 spin_lock(&uap->port.lock);
850}
851
852static void pl011_dma_rx_irq(struct uart_amba_port *uap)
853{
854 struct pl011_dmarx_data *dmarx = &uap->dmarx;
855 struct dma_chan *rxchan = dmarx->chan;
856 struct pl011_sgbuf *sgbuf = dmarx->use_buf_b ?
857 &dmarx->sgbuf_b : &dmarx->sgbuf_a;
858 size_t pending;
859 struct dma_tx_state state;
860 enum dma_status dmastat;
861
862 /*
863 * Pause the transfer so we can trust the current counter,
864 * do this before we pause the PL011 block, else we may
865 * overflow the FIFO.
866 */
867 if (dmaengine_pause(rxchan))
868 dev_err(uap->port.dev, "unable to pause DMA transfer\n");
869 dmastat = rxchan->device->device_tx_status(rxchan,
870 dmarx->cookie, &state);
871 if (dmastat != DMA_PAUSED)
872 dev_err(uap->port.dev, "unable to pause DMA transfer\n");
873
874 /* Disable RX DMA - incoming data will wait in the FIFO */
875 uap->dmacr &= ~UART011_RXDMAE;
Russell King9f25bc52015-11-03 14:51:13 +0000876 pl011_write(uap->dmacr, uap, REG_DMACR);
Linus Walleijead76f32011-02-24 13:21:08 +0100877 uap->dmarx.running = false;
878
879 pending = sgbuf->sg.length - state.residue;
880 BUG_ON(pending > PL011_DMA_BUFFER_SIZE);
881 /* Then we terminate the transfer - we now know our residue */
882 dmaengine_terminate_all(rxchan);
883
884 /*
885 * This will take the chars we have so far and insert
886 * into the framework.
887 */
888 pl011_dma_rx_chars(uap, pending, dmarx->use_buf_b, true);
889
890 /* Switch buffer & re-trigger DMA job */
891 dmarx->use_buf_b = !dmarx->use_buf_b;
892 if (pl011_dma_rx_trigger_dma(uap)) {
893 dev_dbg(uap->port.dev, "could not retrigger RX DMA job "
894 "fall back to interrupt mode\n");
895 uap->im |= UART011_RXIM;
Russell King9f25bc52015-11-03 14:51:13 +0000896 pl011_write(uap->im, uap, REG_IMSC);
Linus Walleijead76f32011-02-24 13:21:08 +0100897 }
898}
899
900static void pl011_dma_rx_callback(void *data)
901{
902 struct uart_amba_port *uap = data;
903 struct pl011_dmarx_data *dmarx = &uap->dmarx;
Chanho Min6dc01aa2012-02-20 10:24:40 +0900904 struct dma_chan *rxchan = dmarx->chan;
Linus Walleijead76f32011-02-24 13:21:08 +0100905 bool lastbuf = dmarx->use_buf_b;
Chanho Min6dc01aa2012-02-20 10:24:40 +0900906 struct pl011_sgbuf *sgbuf = dmarx->use_buf_b ?
907 &dmarx->sgbuf_b : &dmarx->sgbuf_a;
908 size_t pending;
909 struct dma_tx_state state;
Linus Walleijead76f32011-02-24 13:21:08 +0100910 int ret;
911
912 /*
913 * This completion interrupt occurs typically when the
914 * RX buffer is totally stuffed but no timeout has yet
915 * occurred. When that happens, we just want the RX
916 * routine to flush out the secondary DMA buffer while
917 * we immediately trigger the next DMA job.
918 */
919 spin_lock_irq(&uap->port.lock);
Chanho Min6dc01aa2012-02-20 10:24:40 +0900920 /*
921 * Rx data can be taken by the UART interrupts during
922 * the DMA irq handler. So we check the residue here.
923 */
924 rxchan->device->device_tx_status(rxchan, dmarx->cookie, &state);
925 pending = sgbuf->sg.length - state.residue;
926 BUG_ON(pending > PL011_DMA_BUFFER_SIZE);
927 /* Then we terminate the transfer - we now know our residue */
928 dmaengine_terminate_all(rxchan);
929
Linus Walleijead76f32011-02-24 13:21:08 +0100930 uap->dmarx.running = false;
931 dmarx->use_buf_b = !lastbuf;
932 ret = pl011_dma_rx_trigger_dma(uap);
933
Chanho Min6dc01aa2012-02-20 10:24:40 +0900934 pl011_dma_rx_chars(uap, pending, lastbuf, false);
Linus Walleijead76f32011-02-24 13:21:08 +0100935 spin_unlock_irq(&uap->port.lock);
936 /*
937 * Do this check after we picked the DMA chars so we don't
938 * get some IRQ immediately from RX.
939 */
940 if (ret) {
941 dev_dbg(uap->port.dev, "could not retrigger RX DMA job "
942 "fall back to interrupt mode\n");
943 uap->im |= UART011_RXIM;
Russell King9f25bc52015-11-03 14:51:13 +0000944 pl011_write(uap->im, uap, REG_IMSC);
Linus Walleijead76f32011-02-24 13:21:08 +0100945 }
946}
947
948/*
949 * Stop accepting received characters, when we're shutting down or
950 * suspending this port.
951 * Locking: called with port lock held and IRQs disabled.
952 */
953static inline void pl011_dma_rx_stop(struct uart_amba_port *uap)
954{
955 /* FIXME. Just disable the DMA enable */
956 uap->dmacr &= ~UART011_RXDMAE;
Russell King9f25bc52015-11-03 14:51:13 +0000957 pl011_write(uap->dmacr, uap, REG_DMACR);
Linus Walleijead76f32011-02-24 13:21:08 +0100958}
Russell King68b65f72010-12-22 17:24:39 +0000959
Chanho Mincb06ff12013-03-27 18:38:11 +0900960/*
961 * Timer handler for Rx DMA polling.
962 * Every polling, It checks the residue in the dma buffer and transfer
963 * data to the tty. Also, last_residue is updated for the next polling.
964 */
965static void pl011_dma_rx_poll(unsigned long args)
966{
967 struct uart_amba_port *uap = (struct uart_amba_port *)args;
968 struct tty_port *port = &uap->port.state->port;
969 struct pl011_dmarx_data *dmarx = &uap->dmarx;
970 struct dma_chan *rxchan = uap->dmarx.chan;
971 unsigned long flags = 0;
972 unsigned int dmataken = 0;
973 unsigned int size = 0;
974 struct pl011_sgbuf *sgbuf;
975 int dma_count;
976 struct dma_tx_state state;
977
978 sgbuf = dmarx->use_buf_b ? &uap->dmarx.sgbuf_b : &uap->dmarx.sgbuf_a;
979 rxchan->device->device_tx_status(rxchan, dmarx->cookie, &state);
980 if (likely(state.residue < dmarx->last_residue)) {
981 dmataken = sgbuf->sg.length - dmarx->last_residue;
982 size = dmarx->last_residue - state.residue;
983 dma_count = tty_insert_flip_string(port, sgbuf->buf + dmataken,
984 size);
985 if (dma_count == size)
986 dmarx->last_residue = state.residue;
987 dmarx->last_jiffies = jiffies;
988 }
989 tty_flip_buffer_push(port);
990
991 /*
992 * If no data is received in poll_timeout, the driver will fall back
993 * to interrupt mode. We will retrigger DMA at the first interrupt.
994 */
995 if (jiffies_to_msecs(jiffies - dmarx->last_jiffies)
996 > uap->dmarx.poll_timeout) {
997
998 spin_lock_irqsave(&uap->port.lock, flags);
999 pl011_dma_rx_stop(uap);
Guennadi Liakhovetskic25a1ad2013-12-10 14:54:47 +01001000 uap->im |= UART011_RXIM;
Russell King9f25bc52015-11-03 14:51:13 +00001001 pl011_write(uap->im, uap, REG_IMSC);
Chanho Mincb06ff12013-03-27 18:38:11 +09001002 spin_unlock_irqrestore(&uap->port.lock, flags);
1003
1004 uap->dmarx.running = false;
1005 dmaengine_terminate_all(rxchan);
1006 del_timer(&uap->dmarx.timer);
1007 } else {
1008 mod_timer(&uap->dmarx.timer,
1009 jiffies + msecs_to_jiffies(uap->dmarx.poll_rate));
1010 }
1011}
1012
Russell King68b65f72010-12-22 17:24:39 +00001013static void pl011_dma_startup(struct uart_amba_port *uap)
1014{
Linus Walleijead76f32011-02-24 13:21:08 +01001015 int ret;
1016
Jorge Ramirez-Ortiz1c9be312015-03-06 13:05:40 -05001017 if (!uap->dma_probed)
1018 pl011_dma_probe(uap);
1019
Russell King68b65f72010-12-22 17:24:39 +00001020 if (!uap->dmatx.chan)
1021 return;
1022
Andrew Jackson4c0be452014-11-07 14:14:35 +00001023 uap->dmatx.buf = kmalloc(PL011_DMA_BUFFER_SIZE, GFP_KERNEL | __GFP_DMA);
Russell King68b65f72010-12-22 17:24:39 +00001024 if (!uap->dmatx.buf) {
1025 dev_err(uap->port.dev, "no memory for DMA TX buffer\n");
1026 uap->port.fifosize = uap->fifosize;
1027 return;
1028 }
1029
1030 sg_init_one(&uap->dmatx.sg, uap->dmatx.buf, PL011_DMA_BUFFER_SIZE);
1031
1032 /* The DMA buffer is now the FIFO the TTY subsystem can use */
1033 uap->port.fifosize = PL011_DMA_BUFFER_SIZE;
Linus Walleijead76f32011-02-24 13:21:08 +01001034 uap->using_tx_dma = true;
Russell King68b65f72010-12-22 17:24:39 +00001035
Linus Walleijead76f32011-02-24 13:21:08 +01001036 if (!uap->dmarx.chan)
1037 goto skip_rx;
1038
1039 /* Allocate and map DMA RX buffers */
1040 ret = pl011_sgbuf_init(uap->dmarx.chan, &uap->dmarx.sgbuf_a,
1041 DMA_FROM_DEVICE);
1042 if (ret) {
1043 dev_err(uap->port.dev, "failed to init DMA %s: %d\n",
1044 "RX buffer A", ret);
1045 goto skip_rx;
1046 }
1047
1048 ret = pl011_sgbuf_init(uap->dmarx.chan, &uap->dmarx.sgbuf_b,
1049 DMA_FROM_DEVICE);
1050 if (ret) {
1051 dev_err(uap->port.dev, "failed to init DMA %s: %d\n",
1052 "RX buffer B", ret);
1053 pl011_sgbuf_free(uap->dmarx.chan, &uap->dmarx.sgbuf_a,
1054 DMA_FROM_DEVICE);
1055 goto skip_rx;
1056 }
1057
1058 uap->using_rx_dma = true;
1059
1060skip_rx:
Russell King68b65f72010-12-22 17:24:39 +00001061 /* Turn on DMA error (RX/TX will be enabled on demand) */
1062 uap->dmacr |= UART011_DMAONERR;
Russell King9f25bc52015-11-03 14:51:13 +00001063 pl011_write(uap->dmacr, uap, REG_DMACR);
Russell King38d62432010-12-22 17:59:16 +00001064
1065 /*
1066 * ST Micro variants has some specific dma burst threshold
1067 * compensation. Set this to 16 bytes, so burst will only
1068 * be issued above/below 16 bytes.
1069 */
1070 if (uap->vendor->dma_threshold)
Russell King75836332015-11-03 14:50:58 +00001071 pl011_write(ST_UART011_DMAWM_RX_16 | ST_UART011_DMAWM_TX_16,
Russell King9f25bc52015-11-03 14:51:13 +00001072 uap, REG_ST_DMAWM);
Linus Walleijead76f32011-02-24 13:21:08 +01001073
1074 if (uap->using_rx_dma) {
1075 if (pl011_dma_rx_trigger_dma(uap))
1076 dev_dbg(uap->port.dev, "could not trigger initial "
1077 "RX DMA job, fall back to interrupt mode\n");
Chanho Mincb06ff12013-03-27 18:38:11 +09001078 if (uap->dmarx.poll_rate) {
1079 init_timer(&(uap->dmarx.timer));
1080 uap->dmarx.timer.function = pl011_dma_rx_poll;
1081 uap->dmarx.timer.data = (unsigned long)uap;
1082 mod_timer(&uap->dmarx.timer,
1083 jiffies +
1084 msecs_to_jiffies(uap->dmarx.poll_rate));
1085 uap->dmarx.last_residue = PL011_DMA_BUFFER_SIZE;
1086 uap->dmarx.last_jiffies = jiffies;
1087 }
Linus Walleijead76f32011-02-24 13:21:08 +01001088 }
Russell King68b65f72010-12-22 17:24:39 +00001089}
1090
1091static void pl011_dma_shutdown(struct uart_amba_port *uap)
1092{
Linus Walleijead76f32011-02-24 13:21:08 +01001093 if (!(uap->using_tx_dma || uap->using_rx_dma))
Russell King68b65f72010-12-22 17:24:39 +00001094 return;
1095
1096 /* Disable RX and TX DMA */
Russell King9f25bc52015-11-03 14:51:13 +00001097 while (pl011_read(uap, REG_FR) & UART01x_FR_BUSY)
Russell King68b65f72010-12-22 17:24:39 +00001098 barrier();
1099
1100 spin_lock_irq(&uap->port.lock);
1101 uap->dmacr &= ~(UART011_DMAONERR | UART011_RXDMAE | UART011_TXDMAE);
Russell King9f25bc52015-11-03 14:51:13 +00001102 pl011_write(uap->dmacr, uap, REG_DMACR);
Russell King68b65f72010-12-22 17:24:39 +00001103 spin_unlock_irq(&uap->port.lock);
1104
Linus Walleijead76f32011-02-24 13:21:08 +01001105 if (uap->using_tx_dma) {
1106 /* In theory, this should already be done by pl011_dma_flush_buffer */
1107 dmaengine_terminate_all(uap->dmatx.chan);
1108 if (uap->dmatx.queued) {
1109 dma_unmap_sg(uap->dmatx.chan->device->dev, &uap->dmatx.sg, 1,
1110 DMA_TO_DEVICE);
1111 uap->dmatx.queued = false;
1112 }
1113
1114 kfree(uap->dmatx.buf);
1115 uap->using_tx_dma = false;
Russell King68b65f72010-12-22 17:24:39 +00001116 }
1117
Linus Walleijead76f32011-02-24 13:21:08 +01001118 if (uap->using_rx_dma) {
1119 dmaengine_terminate_all(uap->dmarx.chan);
1120 /* Clean up the RX DMA */
1121 pl011_sgbuf_free(uap->dmarx.chan, &uap->dmarx.sgbuf_a, DMA_FROM_DEVICE);
1122 pl011_sgbuf_free(uap->dmarx.chan, &uap->dmarx.sgbuf_b, DMA_FROM_DEVICE);
Chanho Mincb06ff12013-03-27 18:38:11 +09001123 if (uap->dmarx.poll_rate)
1124 del_timer_sync(&uap->dmarx.timer);
Linus Walleijead76f32011-02-24 13:21:08 +01001125 uap->using_rx_dma = false;
1126 }
Russell King68b65f72010-12-22 17:24:39 +00001127}
1128
Linus Walleijead76f32011-02-24 13:21:08 +01001129static inline bool pl011_dma_rx_available(struct uart_amba_port *uap)
1130{
1131 return uap->using_rx_dma;
1132}
1133
1134static inline bool pl011_dma_rx_running(struct uart_amba_port *uap)
1135{
1136 return uap->using_rx_dma && uap->dmarx.running;
1137}
1138
Russell King68b65f72010-12-22 17:24:39 +00001139#else
1140/* Blank functions if the DMA engine is not available */
Jorge Ramirez-Ortiz1c9be312015-03-06 13:05:40 -05001141static inline void pl011_dma_probe(struct uart_amba_port *uap)
Russell King68b65f72010-12-22 17:24:39 +00001142{
1143}
1144
1145static inline void pl011_dma_remove(struct uart_amba_port *uap)
1146{
1147}
1148
1149static inline void pl011_dma_startup(struct uart_amba_port *uap)
1150{
1151}
1152
1153static inline void pl011_dma_shutdown(struct uart_amba_port *uap)
1154{
1155}
1156
1157static inline bool pl011_dma_tx_irq(struct uart_amba_port *uap)
1158{
1159 return false;
1160}
1161
1162static inline void pl011_dma_tx_stop(struct uart_amba_port *uap)
1163{
1164}
1165
1166static inline bool pl011_dma_tx_start(struct uart_amba_port *uap)
1167{
1168 return false;
1169}
1170
Linus Walleijead76f32011-02-24 13:21:08 +01001171static inline void pl011_dma_rx_irq(struct uart_amba_port *uap)
1172{
1173}
1174
1175static inline void pl011_dma_rx_stop(struct uart_amba_port *uap)
1176{
1177}
1178
1179static inline int pl011_dma_rx_trigger_dma(struct uart_amba_port *uap)
1180{
1181 return -EIO;
1182}
1183
1184static inline bool pl011_dma_rx_available(struct uart_amba_port *uap)
1185{
1186 return false;
1187}
1188
1189static inline bool pl011_dma_rx_running(struct uart_amba_port *uap)
1190{
1191 return false;
1192}
1193
Russell King68b65f72010-12-22 17:24:39 +00001194#define pl011_dma_flush_buffer NULL
1195#endif
1196
Russell Kingb129a8c2005-08-31 10:12:14 +01001197static void pl011_stop_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001199 struct uart_amba_port *uap =
1200 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201
1202 uap->im &= ~UART011_TXIM;
Russell King9f25bc52015-11-03 14:51:13 +00001203 pl011_write(uap->im, uap, REG_IMSC);
Russell King68b65f72010-12-22 17:24:39 +00001204 pl011_dma_tx_stop(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205}
1206
Dave Martin1e84d222015-04-27 16:49:05 +01001207static void pl011_tx_chars(struct uart_amba_port *uap, bool from_irq);
Dave Martin734745c2015-03-04 12:27:33 +00001208
1209/* Start TX with programmed I/O only (no DMA) */
1210static void pl011_start_tx_pio(struct uart_amba_port *uap)
1211{
1212 uap->im |= UART011_TXIM;
Russell King9f25bc52015-11-03 14:51:13 +00001213 pl011_write(uap->im, uap, REG_IMSC);
Dave Martin1e84d222015-04-27 16:49:05 +01001214 pl011_tx_chars(uap, false);
Dave Martin734745c2015-03-04 12:27:33 +00001215}
1216
Russell Kingb129a8c2005-08-31 10:12:14 +01001217static void pl011_start_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001219 struct uart_amba_port *uap =
1220 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221
Dave Martin734745c2015-03-04 12:27:33 +00001222 if (!pl011_dma_tx_start(uap))
1223 pl011_start_tx_pio(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224}
1225
1226static void pl011_stop_rx(struct uart_port *port)
1227{
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_RXIM|UART011_RTIM|UART011_FEIM|
1232 UART011_PEIM|UART011_BEIM|UART011_OEIM);
Russell King9f25bc52015-11-03 14:51:13 +00001233 pl011_write(uap->im, uap, REG_IMSC);
Linus Walleijead76f32011-02-24 13:21:08 +01001234
1235 pl011_dma_rx_stop(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236}
1237
1238static void pl011_enable_ms(struct uart_port *port)
1239{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001240 struct uart_amba_port *uap =
1241 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242
1243 uap->im |= UART011_RIMIM|UART011_CTSMIM|UART011_DCDMIM|UART011_DSRMIM;
Russell King9f25bc52015-11-03 14:51:13 +00001244 pl011_write(uap->im, uap, REG_IMSC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245}
1246
David Howells7d12e782006-10-05 14:55:46 +01001247static void pl011_rx_chars(struct uart_amba_port *uap)
Fabio Estevamb83286b2013-08-09 17:58:51 -03001248__releases(&uap->port.lock)
1249__acquires(&uap->port.lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250{
Linus Walleij29772c42011-02-24 13:21:36 +01001251 pl011_fifo_to_tty(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252
Thomas Gleixner2389b272007-05-29 21:53:50 +01001253 spin_unlock(&uap->port.lock);
Jiri Slaby2e124b42013-01-03 15:53:06 +01001254 tty_flip_buffer_push(&uap->port.state->port);
Linus Walleijead76f32011-02-24 13:21:08 +01001255 /*
1256 * If we were temporarily out of DMA mode for a while,
1257 * attempt to switch back to DMA mode again.
1258 */
1259 if (pl011_dma_rx_available(uap)) {
1260 if (pl011_dma_rx_trigger_dma(uap)) {
1261 dev_dbg(uap->port.dev, "could not trigger RX DMA job "
1262 "fall back to interrupt mode again\n");
1263 uap->im |= UART011_RXIM;
Russell King9f25bc52015-11-03 14:51:13 +00001264 pl011_write(uap->im, uap, REG_IMSC);
Chanho Mincb06ff12013-03-27 18:38:11 +09001265 } else {
Chanho Min89fa28d2013-04-03 11:10:37 +09001266#ifdef CONFIG_DMA_ENGINE
Chanho Mincb06ff12013-03-27 18:38:11 +09001267 /* Start Rx DMA poll */
1268 if (uap->dmarx.poll_rate) {
1269 uap->dmarx.last_jiffies = jiffies;
1270 uap->dmarx.last_residue = PL011_DMA_BUFFER_SIZE;
1271 mod_timer(&uap->dmarx.timer,
1272 jiffies +
1273 msecs_to_jiffies(uap->dmarx.poll_rate));
1274 }
Chanho Min89fa28d2013-04-03 11:10:37 +09001275#endif
Chanho Mincb06ff12013-03-27 18:38:11 +09001276 }
Linus Walleijead76f32011-02-24 13:21:08 +01001277 }
Thomas Gleixner2389b272007-05-29 21:53:50 +01001278 spin_lock(&uap->port.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279}
1280
Dave Martin1e84d222015-04-27 16:49:05 +01001281static bool pl011_tx_char(struct uart_amba_port *uap, unsigned char c,
1282 bool from_irq)
Dave Martin734745c2015-03-04 12:27:33 +00001283{
Dave Martin1e84d222015-04-27 16:49:05 +01001284 if (unlikely(!from_irq) &&
Russell King9f25bc52015-11-03 14:51:13 +00001285 pl011_read(uap, REG_FR) & UART01x_FR_TXFF)
Dave Martin1e84d222015-04-27 16:49:05 +01001286 return false; /* unable to transmit character */
1287
Russell King9f25bc52015-11-03 14:51:13 +00001288 pl011_write(c, uap, REG_DR);
Dave Martin734745c2015-03-04 12:27:33 +00001289 uap->port.icount.tx++;
1290
Dave Martin1e84d222015-04-27 16:49:05 +01001291 return true;
Dave Martin734745c2015-03-04 12:27:33 +00001292}
1293
Dave Martin1e84d222015-04-27 16:49:05 +01001294static void pl011_tx_chars(struct uart_amba_port *uap, bool from_irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295{
Alan Coxebd2c8f2009-09-19 13:13:28 -07001296 struct circ_buf *xmit = &uap->port.state->xmit;
Dave Martin1e84d222015-04-27 16:49:05 +01001297 int count = uap->fifosize >> 1;
Dave Martin734745c2015-03-04 12:27:33 +00001298
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 if (uap->port.x_char) {
Dave Martin1e84d222015-04-27 16:49:05 +01001300 if (!pl011_tx_char(uap, uap->port.x_char, from_irq))
1301 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 uap->port.x_char = 0;
Dave Martin734745c2015-03-04 12:27:33 +00001303 --count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 }
1305 if (uart_circ_empty(xmit) || uart_tx_stopped(&uap->port)) {
Russell Kingb129a8c2005-08-31 10:12:14 +01001306 pl011_stop_tx(&uap->port);
Dave Martin1e84d222015-04-27 16:49:05 +01001307 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 }
1309
Russell King68b65f72010-12-22 17:24:39 +00001310 /* If we are using DMA mode, try to send some characters. */
1311 if (pl011_dma_tx_irq(uap))
Dave Martin1e84d222015-04-27 16:49:05 +01001312 return;
Russell King68b65f72010-12-22 17:24:39 +00001313
Dave Martin1e84d222015-04-27 16:49:05 +01001314 do {
1315 if (likely(from_irq) && count-- == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 break;
Dave Martin1e84d222015-04-27 16:49:05 +01001317
1318 if (!pl011_tx_char(uap, xmit->buf[xmit->tail], from_irq))
1319 break;
1320
1321 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
1322 } while (!uart_circ_empty(xmit));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323
1324 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
1325 uart_write_wakeup(&uap->port);
1326
Dave Martin1e84d222015-04-27 16:49:05 +01001327 if (uart_circ_empty(xmit))
Russell Kingb129a8c2005-08-31 10:12:14 +01001328 pl011_stop_tx(&uap->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329}
1330
1331static void pl011_modem_status(struct uart_amba_port *uap)
1332{
1333 unsigned int status, delta;
1334
Russell King9f25bc52015-11-03 14:51:13 +00001335 status = pl011_read(uap, REG_FR) & UART01x_FR_MODEM_ANY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336
1337 delta = status ^ uap->old_status;
1338 uap->old_status = status;
1339
1340 if (!delta)
1341 return;
1342
1343 if (delta & UART01x_FR_DCD)
1344 uart_handle_dcd_change(&uap->port, status & UART01x_FR_DCD);
1345
Greg Kroah-Hartman062a68a2015-09-04 09:11:24 -07001346 if (delta & UART01x_FR_DSR)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 uap->port.icount.dsr++;
1348
Greg Kroah-Hartman062a68a2015-09-04 09:11:24 -07001349 if (delta & UART01x_FR_CTS)
1350 uart_handle_cts_change(&uap->port, status & UART01x_FR_CTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351
Alan Coxbdc04e32009-09-19 13:13:31 -07001352 wake_up_interruptible(&uap->port.state->port.delta_msr_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353}
1354
Andre Przywara9c4ef4b2015-05-21 17:26:20 +01001355static void check_apply_cts_event_workaround(struct uart_amba_port *uap)
1356{
1357 unsigned int dummy_read;
1358
1359 if (!uap->vendor->cts_event_workaround)
1360 return;
1361
1362 /* workaround to make sure that all bits are unlocked.. */
Russell King9f25bc52015-11-03 14:51:13 +00001363 pl011_write(0x00, uap, REG_ICR);
Andre Przywara9c4ef4b2015-05-21 17:26:20 +01001364
1365 /*
1366 * WA: introduce 26ns(1 uart clk) delay before W1C;
1367 * single apb access will incur 2 pclk(133.12Mhz) delay,
1368 * so add 2 dummy reads
1369 */
Russell King9f25bc52015-11-03 14:51:13 +00001370 dummy_read = pl011_read(uap, REG_ICR);
1371 dummy_read = pl011_read(uap, REG_ICR);
Andre Przywara9c4ef4b2015-05-21 17:26:20 +01001372}
1373
David Howells7d12e782006-10-05 14:55:46 +01001374static irqreturn_t pl011_int(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375{
1376 struct uart_amba_port *uap = dev_id;
Russell King963cc982010-12-22 17:16:09 +00001377 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 unsigned int status, pass_counter = AMBA_ISR_PASS_LIMIT;
Andre Przywara075167e2015-05-21 17:26:19 +01001379 u16 imsc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 int handled = 0;
1381
Russell King963cc982010-12-22 17:16:09 +00001382 spin_lock_irqsave(&uap->port.lock, flags);
Russell King9f25bc52015-11-03 14:51:13 +00001383 imsc = pl011_read(uap, REG_IMSC);
1384 status = pl011_read(uap, REG_RIS) & imsc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 if (status) {
1386 do {
Andre Przywara9c4ef4b2015-05-21 17:26:20 +01001387 check_apply_cts_event_workaround(uap);
Greg Kroah-Hartmanf11c9842015-09-04 09:13:39 -07001388
Russell King75836332015-11-03 14:50:58 +00001389 pl011_write(status & ~(UART011_TXIS|UART011_RTIS|
1390 UART011_RXIS),
Russell King9f25bc52015-11-03 14:51:13 +00001391 uap, REG_ICR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392
Linus Walleijead76f32011-02-24 13:21:08 +01001393 if (status & (UART011_RTIS|UART011_RXIS)) {
1394 if (pl011_dma_rx_running(uap))
1395 pl011_dma_rx_irq(uap);
1396 else
1397 pl011_rx_chars(uap);
1398 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 if (status & (UART011_DSRMIS|UART011_DCDMIS|
1400 UART011_CTSMIS|UART011_RIMIS))
1401 pl011_modem_status(uap);
Dave Martin1e84d222015-04-27 16:49:05 +01001402 if (status & UART011_TXIS)
1403 pl011_tx_chars(uap, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404
Rajanikanth H.V4fd06902012-03-26 11:17:02 +02001405 if (pass_counter-- == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 break;
1407
Russell King9f25bc52015-11-03 14:51:13 +00001408 status = pl011_read(uap, REG_RIS) & imsc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 } while (status != 0);
1410 handled = 1;
1411 }
1412
Russell King963cc982010-12-22 17:16:09 +00001413 spin_unlock_irqrestore(&uap->port.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414
1415 return IRQ_RETVAL(handled);
1416}
1417
Linus Walleije643f872012-06-17 15:44:19 +02001418static unsigned int pl011_tx_empty(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001420 struct uart_amba_port *uap =
1421 container_of(port, struct uart_amba_port, port);
Russell King9f25bc52015-11-03 14:51:13 +00001422 unsigned int status = pl011_read(uap, REG_FR);
Greg Kroah-Hartman062a68a2015-09-04 09:11:24 -07001423 return status & (UART01x_FR_BUSY|UART01x_FR_TXFF) ? 0 : TIOCSER_TEMT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424}
1425
Linus Walleije643f872012-06-17 15:44:19 +02001426static unsigned int pl011_get_mctrl(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001428 struct uart_amba_port *uap =
1429 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 unsigned int result = 0;
Russell King9f25bc52015-11-03 14:51:13 +00001431 unsigned int status = pl011_read(uap, REG_FR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432
Jiri Slaby5159f402007-10-18 23:40:31 -07001433#define TIOCMBIT(uartbit, tiocmbit) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 if (status & uartbit) \
1435 result |= tiocmbit
1436
Jiri Slaby5159f402007-10-18 23:40:31 -07001437 TIOCMBIT(UART01x_FR_DCD, TIOCM_CAR);
Greg Kroah-Hartman062a68a2015-09-04 09:11:24 -07001438 TIOCMBIT(UART01x_FR_DSR, TIOCM_DSR);
1439 TIOCMBIT(UART01x_FR_CTS, TIOCM_CTS);
1440 TIOCMBIT(UART011_FR_RI, TIOCM_RNG);
Jiri Slaby5159f402007-10-18 23:40:31 -07001441#undef TIOCMBIT
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 return result;
1443}
1444
1445static void pl011_set_mctrl(struct uart_port *port, unsigned int mctrl)
1446{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001447 struct uart_amba_port *uap =
1448 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 unsigned int cr;
1450
Russell King9f25bc52015-11-03 14:51:13 +00001451 cr = pl011_read(uap, REG_CR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452
Jiri Slaby5159f402007-10-18 23:40:31 -07001453#define TIOCMBIT(tiocmbit, uartbit) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 if (mctrl & tiocmbit) \
1455 cr |= uartbit; \
1456 else \
1457 cr &= ~uartbit
1458
Jiri Slaby5159f402007-10-18 23:40:31 -07001459 TIOCMBIT(TIOCM_RTS, UART011_CR_RTS);
1460 TIOCMBIT(TIOCM_DTR, UART011_CR_DTR);
1461 TIOCMBIT(TIOCM_OUT1, UART011_CR_OUT1);
1462 TIOCMBIT(TIOCM_OUT2, UART011_CR_OUT2);
1463 TIOCMBIT(TIOCM_LOOP, UART011_CR_LBE);
Rabin Vincent3b438162010-02-12 06:43:11 +01001464
1465 if (uap->autorts) {
1466 /* We need to disable auto-RTS if we want to turn RTS off */
1467 TIOCMBIT(TIOCM_RTS, UART011_CR_RTSEN);
1468 }
Jiri Slaby5159f402007-10-18 23:40:31 -07001469#undef TIOCMBIT
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470
Russell King9f25bc52015-11-03 14:51:13 +00001471 pl011_write(cr, uap, REG_CR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472}
1473
1474static void pl011_break_ctl(struct uart_port *port, int break_state)
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 long flags;
1479 unsigned int lcr_h;
1480
1481 spin_lock_irqsave(&uap->port.lock, flags);
Russell Kingb2a4e242015-11-03 14:51:03 +00001482 lcr_h = pl011_read(uap, uap->lcrh_tx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 if (break_state == -1)
1484 lcr_h |= UART01x_LCRH_BRK;
1485 else
1486 lcr_h &= ~UART01x_LCRH_BRK;
Russell Kingb2a4e242015-11-03 14:51:03 +00001487 pl011_write(lcr_h, uap, uap->lcrh_tx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 spin_unlock_irqrestore(&uap->port.lock, flags);
1489}
1490
Jason Wessel84b5ae12008-02-20 13:33:39 -06001491#ifdef CONFIG_CONSOLE_POLL
Anton Vorontsov5c8124a2012-09-24 14:27:55 -07001492
1493static void pl011_quiesce_irqs(struct uart_port *port)
1494{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001495 struct uart_amba_port *uap =
1496 container_of(port, struct uart_amba_port, port);
Anton Vorontsov5c8124a2012-09-24 14:27:55 -07001497
Russell King9f25bc52015-11-03 14:51:13 +00001498 pl011_write(pl011_read(uap, REG_MIS), uap, REG_ICR);
Anton Vorontsov5c8124a2012-09-24 14:27:55 -07001499 /*
1500 * There is no way to clear TXIM as this is "ready to transmit IRQ", so
1501 * we simply mask it. start_tx() will unmask it.
1502 *
1503 * Note we can race with start_tx(), and if the race happens, the
1504 * polling user might get another interrupt just after we clear it.
1505 * But it should be OK and can happen even w/o the race, e.g.
1506 * controller immediately got some new data and raised the IRQ.
1507 *
1508 * And whoever uses polling routines assumes that it manages the device
1509 * (including tx queue), so we're also fine with start_tx()'s caller
1510 * side.
1511 */
Russell King9f25bc52015-11-03 14:51:13 +00001512 pl011_write(pl011_read(uap, REG_IMSC) & ~UART011_TXIM, uap,
1513 REG_IMSC);
Anton Vorontsov5c8124a2012-09-24 14:27:55 -07001514}
1515
Linus Walleije643f872012-06-17 15:44:19 +02001516static int pl011_get_poll_char(struct uart_port *port)
Jason Wessel84b5ae12008-02-20 13:33:39 -06001517{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001518 struct uart_amba_port *uap =
1519 container_of(port, struct uart_amba_port, port);
Jason Wessel84b5ae12008-02-20 13:33:39 -06001520 unsigned int status;
1521
Anton Vorontsov5c8124a2012-09-24 14:27:55 -07001522 /*
1523 * The caller might need IRQs lowered, e.g. if used with KDB NMI
1524 * debugger.
1525 */
1526 pl011_quiesce_irqs(port);
1527
Russell King9f25bc52015-11-03 14:51:13 +00001528 status = pl011_read(uap, REG_FR);
Jason Wesself5316b42010-05-20 21:04:22 -05001529 if (status & UART01x_FR_RXFE)
1530 return NO_POLL_CHAR;
Jason Wessel84b5ae12008-02-20 13:33:39 -06001531
Russell King9f25bc52015-11-03 14:51:13 +00001532 return pl011_read(uap, REG_DR);
Jason Wessel84b5ae12008-02-20 13:33:39 -06001533}
1534
Linus Walleije643f872012-06-17 15:44:19 +02001535static void pl011_put_poll_char(struct uart_port *port,
Jason Wessel84b5ae12008-02-20 13:33:39 -06001536 unsigned char ch)
1537{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001538 struct uart_amba_port *uap =
1539 container_of(port, struct uart_amba_port, port);
Jason Wessel84b5ae12008-02-20 13:33:39 -06001540
Russell King9f25bc52015-11-03 14:51:13 +00001541 while (pl011_read(uap, REG_FR) & UART01x_FR_TXFF)
Jason Wessel84b5ae12008-02-20 13:33:39 -06001542 barrier();
1543
Russell King9f25bc52015-11-03 14:51:13 +00001544 pl011_write(ch, uap, REG_DR);
Jason Wessel84b5ae12008-02-20 13:33:39 -06001545}
1546
1547#endif /* CONFIG_CONSOLE_POLL */
1548
Anton Vorontsovb3564c22012-09-24 14:27:54 -07001549static int pl011_hwinit(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001551 struct uart_amba_port *uap =
1552 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 int retval;
1554
Linus Walleij78d80c52012-05-23 21:18:46 +02001555 /* Optionaly enable pins to be muxed in and configured */
Linus Walleij2b996fc2013-06-05 15:36:42 +02001556 pinctrl_pm_select_default_state(port->dev);
Linus Walleij78d80c52012-05-23 21:18:46 +02001557
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 /*
1559 * Try to enable the clock producer.
1560 */
Julia Lawall1c4c4392012-08-26 18:01:01 +02001561 retval = clk_prepare_enable(uap->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 if (retval)
Tushar Behera7f6d9422014-06-26 15:35:35 +05301563 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564
1565 uap->port.uartclk = clk_get_rate(uap->clk);
1566
Linus Walleij9b96fba2012-03-13 13:27:23 +01001567 /* Clear pending error and receive interrupts */
Russell King75836332015-11-03 14:50:58 +00001568 pl011_write(UART011_OEIS | UART011_BEIS | UART011_PEIS |
1569 UART011_FEIS | UART011_RTIS | UART011_RXIS,
Russell King9f25bc52015-11-03 14:51:13 +00001570 uap, REG_ICR);
Linus Walleij9b96fba2012-03-13 13:27:23 +01001571
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 /*
Anton Vorontsovb3564c22012-09-24 14:27:54 -07001573 * Save interrupts enable mask, and enable RX interrupts in case if
1574 * the interrupt is used for NMI entry.
1575 */
Russell King9f25bc52015-11-03 14:51:13 +00001576 uap->im = pl011_read(uap, REG_IMSC);
1577 pl011_write(UART011_RTIM | UART011_RXIM, uap, REG_IMSC);
Anton Vorontsovb3564c22012-09-24 14:27:54 -07001578
Jingoo Han574de552013-07-30 17:06:57 +09001579 if (dev_get_platdata(uap->port.dev)) {
Anton Vorontsovb3564c22012-09-24 14:27:54 -07001580 struct amba_pl011_data *plat;
1581
Jingoo Han574de552013-07-30 17:06:57 +09001582 plat = dev_get_platdata(uap->port.dev);
Anton Vorontsovb3564c22012-09-24 14:27:54 -07001583 if (plat->init)
1584 plat->init();
1585 }
1586 return 0;
Anton Vorontsovb3564c22012-09-24 14:27:54 -07001587}
1588
Russell King7fe9a5a2015-11-03 14:51:08 +00001589static bool pl011_split_lcrh(const struct uart_amba_port *uap)
1590{
Russell King9f25bc52015-11-03 14:51:13 +00001591 return pl011_reg_to_offset(uap, uap->lcrh_rx) !=
1592 pl011_reg_to_offset(uap, uap->lcrh_tx);
Russell King7fe9a5a2015-11-03 14:51:08 +00001593}
1594
Jon Medhurstb60f2f62013-12-10 10:18:59 +00001595static void pl011_write_lcr_h(struct uart_amba_port *uap, unsigned int lcr_h)
1596{
Russell Kingb2a4e242015-11-03 14:51:03 +00001597 pl011_write(lcr_h, uap, uap->lcrh_rx);
Russell King7fe9a5a2015-11-03 14:51:08 +00001598 if (pl011_split_lcrh(uap)) {
Jon Medhurstb60f2f62013-12-10 10:18:59 +00001599 int i;
1600 /*
1601 * Wait 10 PCLKs before writing LCRH_TX register,
1602 * to get this delay write read only register 10 times
1603 */
1604 for (i = 0; i < 10; ++i)
Russell King9f25bc52015-11-03 14:51:13 +00001605 pl011_write(0xff, uap, REG_MIS);
Russell Kingb2a4e242015-11-03 14:51:03 +00001606 pl011_write(lcr_h, uap, uap->lcrh_tx);
Jon Medhurstb60f2f62013-12-10 10:18:59 +00001607 }
1608}
1609
Andre Przywara867b8e82015-05-21 17:26:15 +01001610static int pl011_allocate_irq(struct uart_amba_port *uap)
1611{
Russell King9f25bc52015-11-03 14:51:13 +00001612 pl011_write(uap->im, uap, REG_IMSC);
Andre Przywara867b8e82015-05-21 17:26:15 +01001613
1614 return request_irq(uap->port.irq, pl011_int, 0, "uart-pl011", uap);
1615}
1616
1617/*
1618 * Enable interrupts, only timeouts when using DMA
1619 * if initial RX DMA job failed, start in interrupt mode
1620 * as well.
1621 */
1622static void pl011_enable_interrupts(struct uart_amba_port *uap)
1623{
1624 spin_lock_irq(&uap->port.lock);
1625
1626 /* Clear out any spuriously appearing RX interrupts */
Russell King9f25bc52015-11-03 14:51:13 +00001627 pl011_write(UART011_RTIS | UART011_RXIS, uap, REG_ICR);
Andre Przywara867b8e82015-05-21 17:26:15 +01001628 uap->im = UART011_RTIM;
1629 if (!pl011_dma_rx_running(uap))
1630 uap->im |= UART011_RXIM;
Russell King9f25bc52015-11-03 14:51:13 +00001631 pl011_write(uap->im, uap, REG_IMSC);
Andre Przywara867b8e82015-05-21 17:26:15 +01001632 spin_unlock_irq(&uap->port.lock);
1633}
1634
Anton Vorontsovb3564c22012-09-24 14:27:54 -07001635static int pl011_startup(struct uart_port *port)
1636{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001637 struct uart_amba_port *uap =
1638 container_of(port, struct uart_amba_port, port);
Dave Martin734745c2015-03-04 12:27:33 +00001639 unsigned int cr;
Anton Vorontsovb3564c22012-09-24 14:27:54 -07001640 int retval;
1641
1642 retval = pl011_hwinit(port);
1643 if (retval)
1644 goto clk_dis;
1645
Andre Przywara867b8e82015-05-21 17:26:15 +01001646 retval = pl011_allocate_irq(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 if (retval)
1648 goto clk_dis;
1649
Russell King9f25bc52015-11-03 14:51:13 +00001650 pl011_write(uap->vendor->ifls, uap, REG_IFLS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651
Jon Medhurstfe433902013-12-10 10:18:58 +00001652 spin_lock_irq(&uap->port.lock);
1653
Shreshtha Kumar Sahud8d8ffa2012-01-18 15:53:59 +05301654 /* restore RTS and DTR */
1655 cr = uap->old_cr & (UART011_CR_RTS | UART011_CR_DTR);
1656 cr |= UART01x_CR_UARTEN | UART011_CR_RXE | UART011_CR_TXE;
Russell King9f25bc52015-11-03 14:51:13 +00001657 pl011_write(cr, uap, REG_CR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658
Jon Medhurstfe433902013-12-10 10:18:58 +00001659 spin_unlock_irq(&uap->port.lock);
1660
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 /*
1662 * initialise the old status of the modem signals
1663 */
Russell King9f25bc52015-11-03 14:51:13 +00001664 uap->old_status = pl011_read(uap, REG_FR) & UART01x_FR_MODEM_ANY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665
Russell King68b65f72010-12-22 17:24:39 +00001666 /* Startup DMA */
1667 pl011_dma_startup(uap);
1668
Andre Przywara867b8e82015-05-21 17:26:15 +01001669 pl011_enable_interrupts(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670
1671 return 0;
1672
1673 clk_dis:
Julia Lawall1c4c4392012-08-26 18:01:01 +02001674 clk_disable_unprepare(uap->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 return retval;
1676}
1677
Andre Przywara0dd1e242015-05-21 17:26:23 +01001678static int sbsa_uart_startup(struct uart_port *port)
1679{
1680 struct uart_amba_port *uap =
1681 container_of(port, struct uart_amba_port, port);
1682 int retval;
1683
1684 retval = pl011_hwinit(port);
1685 if (retval)
1686 return retval;
1687
1688 retval = pl011_allocate_irq(uap);
1689 if (retval)
1690 return retval;
1691
1692 /* The SBSA UART does not support any modem status lines. */
1693 uap->old_status = 0;
1694
1695 pl011_enable_interrupts(uap);
1696
1697 return 0;
1698}
1699
Linus Walleijec489aa2010-06-02 08:13:52 +01001700static void pl011_shutdown_channel(struct uart_amba_port *uap,
1701 unsigned int lcrh)
1702{
Greg Kroah-Hartmanf11c9842015-09-04 09:13:39 -07001703 unsigned long val;
Linus Walleijec489aa2010-06-02 08:13:52 +01001704
Russell Kingb2a4e242015-11-03 14:51:03 +00001705 val = pl011_read(uap, lcrh);
Greg Kroah-Hartmanf11c9842015-09-04 09:13:39 -07001706 val &= ~(UART01x_LCRH_BRK | UART01x_LCRH_FEN);
Russell Kingb2a4e242015-11-03 14:51:03 +00001707 pl011_write(val, uap, lcrh);
Linus Walleijec489aa2010-06-02 08:13:52 +01001708}
1709
Andre Przywara95166a32015-05-21 17:26:16 +01001710/*
1711 * disable the port. It should not disable RTS and DTR.
1712 * Also RTS and DTR state should be preserved to restore
1713 * it during startup().
1714 */
1715static void pl011_disable_uart(struct uart_amba_port *uap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716{
Shreshtha Kumar Sahud8d8ffa2012-01-18 15:53:59 +05301717 unsigned int cr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718
Rabin Vincent3b438162010-02-12 06:43:11 +01001719 uap->autorts = false;
Jon Medhurstfe433902013-12-10 10:18:58 +00001720 spin_lock_irq(&uap->port.lock);
Russell King9f25bc52015-11-03 14:51:13 +00001721 cr = pl011_read(uap, REG_CR);
Shreshtha Kumar Sahud8d8ffa2012-01-18 15:53:59 +05301722 uap->old_cr = cr;
1723 cr &= UART011_CR_RTS | UART011_CR_DTR;
1724 cr |= UART01x_CR_UARTEN | UART011_CR_TXE;
Russell King9f25bc52015-11-03 14:51:13 +00001725 pl011_write(cr, uap, REG_CR);
Jon Medhurstfe433902013-12-10 10:18:58 +00001726 spin_unlock_irq(&uap->port.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727
1728 /*
1729 * disable break condition and fifos
1730 */
Linus Walleijec489aa2010-06-02 08:13:52 +01001731 pl011_shutdown_channel(uap, uap->lcrh_rx);
Russell King7fe9a5a2015-11-03 14:51:08 +00001732 if (pl011_split_lcrh(uap))
Linus Walleijec489aa2010-06-02 08:13:52 +01001733 pl011_shutdown_channel(uap, uap->lcrh_tx);
Andre Przywara95166a32015-05-21 17:26:16 +01001734}
1735
1736static void pl011_disable_interrupts(struct uart_amba_port *uap)
1737{
1738 spin_lock_irq(&uap->port.lock);
1739
1740 /* mask all interrupts and clear all pending ones */
1741 uap->im = 0;
Russell King9f25bc52015-11-03 14:51:13 +00001742 pl011_write(uap->im, uap, REG_IMSC);
1743 pl011_write(0xffff, uap, REG_ICR);
Andre Przywara95166a32015-05-21 17:26:16 +01001744
1745 spin_unlock_irq(&uap->port.lock);
1746}
1747
1748static void pl011_shutdown(struct uart_port *port)
1749{
1750 struct uart_amba_port *uap =
1751 container_of(port, struct uart_amba_port, port);
1752
1753 pl011_disable_interrupts(uap);
1754
1755 pl011_dma_shutdown(uap);
1756
1757 free_irq(uap->port.irq, uap);
1758
1759 pl011_disable_uart(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760
1761 /*
1762 * Shut down the clock producer
1763 */
Julia Lawall1c4c4392012-08-26 18:01:01 +02001764 clk_disable_unprepare(uap->clk);
Linus Walleij78d80c52012-05-23 21:18:46 +02001765 /* Optionally let pins go into sleep states */
Linus Walleij2b996fc2013-06-05 15:36:42 +02001766 pinctrl_pm_select_sleep_state(port->dev);
Shreshtha Kumar Sahuc16d51a2011-06-13 10:11:33 +02001767
Jingoo Han574de552013-07-30 17:06:57 +09001768 if (dev_get_platdata(uap->port.dev)) {
Shreshtha Kumar Sahuc16d51a2011-06-13 10:11:33 +02001769 struct amba_pl011_data *plat;
1770
Jingoo Han574de552013-07-30 17:06:57 +09001771 plat = dev_get_platdata(uap->port.dev);
Shreshtha Kumar Sahuc16d51a2011-06-13 10:11:33 +02001772 if (plat->exit)
1773 plat->exit();
1774 }
1775
Peter Hurley36f339d2014-11-06 09:06:12 -05001776 if (uap->port.ops->flush_buffer)
1777 uap->port.ops->flush_buffer(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778}
1779
Andre Przywara0dd1e242015-05-21 17:26:23 +01001780static void sbsa_uart_shutdown(struct uart_port *port)
1781{
1782 struct uart_amba_port *uap =
1783 container_of(port, struct uart_amba_port, port);
1784
1785 pl011_disable_interrupts(uap);
1786
1787 free_irq(uap->port.irq, uap);
1788
1789 if (uap->port.ops->flush_buffer)
1790 uap->port.ops->flush_buffer(port);
1791}
1792
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793static void
Andre Przywaraef5a9352015-05-21 17:26:17 +01001794pl011_setup_status_masks(struct uart_port *port, struct ktermios *termios)
1795{
1796 port->read_status_mask = UART011_DR_OE | 255;
1797 if (termios->c_iflag & INPCK)
1798 port->read_status_mask |= UART011_DR_FE | UART011_DR_PE;
1799 if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
1800 port->read_status_mask |= UART011_DR_BE;
1801
1802 /*
1803 * Characters to ignore
1804 */
1805 port->ignore_status_mask = 0;
1806 if (termios->c_iflag & IGNPAR)
1807 port->ignore_status_mask |= UART011_DR_FE | UART011_DR_PE;
1808 if (termios->c_iflag & IGNBRK) {
1809 port->ignore_status_mask |= UART011_DR_BE;
1810 /*
1811 * If we're ignoring parity and break indicators,
1812 * ignore overruns too (for real raw support).
1813 */
1814 if (termios->c_iflag & IGNPAR)
1815 port->ignore_status_mask |= UART011_DR_OE;
1816 }
1817
1818 /*
1819 * Ignore all characters if CREAD is not set.
1820 */
1821 if ((termios->c_cflag & CREAD) == 0)
1822 port->ignore_status_mask |= UART_DUMMY_DR_RX;
1823}
1824
1825static void
Alan Cox606d0992006-12-08 02:38:45 -08001826pl011_set_termios(struct uart_port *port, struct ktermios *termios,
1827 struct ktermios *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001829 struct uart_amba_port *uap =
1830 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 unsigned int lcr_h, old_cr;
1832 unsigned long flags;
Russell Kingc19f12b2010-12-22 17:48:26 +00001833 unsigned int baud, quot, clkdiv;
1834
1835 if (uap->vendor->oversampling)
1836 clkdiv = 8;
1837 else
1838 clkdiv = 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839
1840 /*
1841 * Ask the core to calculate the divisor for us.
1842 */
Linus Walleijac3e3fb2010-06-02 20:40:22 +01001843 baud = uart_get_baud_rate(port, termios, old, 0,
Russell Kingc19f12b2010-12-22 17:48:26 +00001844 port->uartclk / clkdiv);
Chanho Min89fa28d2013-04-03 11:10:37 +09001845#ifdef CONFIG_DMA_ENGINE
Chanho Mincb06ff12013-03-27 18:38:11 +09001846 /*
1847 * Adjust RX DMA polling rate with baud rate if not specified.
1848 */
1849 if (uap->dmarx.auto_poll_rate)
1850 uap->dmarx.poll_rate = DIV_ROUND_UP(10000000, baud);
Chanho Min89fa28d2013-04-03 11:10:37 +09001851#endif
Linus Walleijac3e3fb2010-06-02 20:40:22 +01001852
1853 if (baud > port->uartclk/16)
1854 quot = DIV_ROUND_CLOSEST(port->uartclk * 8, baud);
1855 else
1856 quot = DIV_ROUND_CLOSEST(port->uartclk * 4, baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857
1858 switch (termios->c_cflag & CSIZE) {
1859 case CS5:
1860 lcr_h = UART01x_LCRH_WLEN_5;
1861 break;
1862 case CS6:
1863 lcr_h = UART01x_LCRH_WLEN_6;
1864 break;
1865 case CS7:
1866 lcr_h = UART01x_LCRH_WLEN_7;
1867 break;
1868 default: // CS8
1869 lcr_h = UART01x_LCRH_WLEN_8;
1870 break;
1871 }
1872 if (termios->c_cflag & CSTOPB)
1873 lcr_h |= UART01x_LCRH_STP2;
1874 if (termios->c_cflag & PARENB) {
1875 lcr_h |= UART01x_LCRH_PEN;
1876 if (!(termios->c_cflag & PARODD))
1877 lcr_h |= UART01x_LCRH_EPS;
1878 }
Russell Kingffca2b12010-12-22 17:13:05 +00001879 if (uap->fifosize > 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 lcr_h |= UART01x_LCRH_FEN;
1881
1882 spin_lock_irqsave(&port->lock, flags);
1883
1884 /*
1885 * Update the per-port timeout.
1886 */
1887 uart_update_timeout(port, termios->c_cflag, baud);
1888
Andre Przywaraef5a9352015-05-21 17:26:17 +01001889 pl011_setup_status_masks(port, termios);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890
1891 if (UART_ENABLE_MS(port, termios->c_cflag))
1892 pl011_enable_ms(port);
1893
1894 /* first, disable everything */
Russell King9f25bc52015-11-03 14:51:13 +00001895 old_cr = pl011_read(uap, REG_CR);
1896 pl011_write(0, uap, REG_CR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897
Rabin Vincent3b438162010-02-12 06:43:11 +01001898 if (termios->c_cflag & CRTSCTS) {
1899 if (old_cr & UART011_CR_RTS)
1900 old_cr |= UART011_CR_RTSEN;
1901
1902 old_cr |= UART011_CR_CTSEN;
1903 uap->autorts = true;
1904 } else {
1905 old_cr &= ~(UART011_CR_CTSEN | UART011_CR_RTSEN);
1906 uap->autorts = false;
1907 }
1908
Russell Kingc19f12b2010-12-22 17:48:26 +00001909 if (uap->vendor->oversampling) {
1910 if (baud > port->uartclk / 16)
Linus Walleijac3e3fb2010-06-02 20:40:22 +01001911 old_cr |= ST_UART011_CR_OVSFACT;
1912 else
1913 old_cr &= ~ST_UART011_CR_OVSFACT;
1914 }
1915
Linus Walleijc5dd5532012-09-26 17:21:36 +02001916 /*
1917 * Workaround for the ST Micro oversampling variants to
1918 * increase the bitrate slightly, by lowering the divisor,
1919 * to avoid delayed sampling of start bit at high speeds,
1920 * else we see data corruption.
1921 */
1922 if (uap->vendor->oversampling) {
1923 if ((baud >= 3000000) && (baud < 3250000) && (quot > 1))
1924 quot -= 1;
1925 else if ((baud > 3250000) && (quot > 2))
1926 quot -= 2;
1927 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 /* Set baud rate */
Russell King9f25bc52015-11-03 14:51:13 +00001929 pl011_write(quot & 0x3f, uap, REG_FBRD);
1930 pl011_write(quot >> 6, uap, REG_IBRD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931
1932 /*
1933 * ----------v----------v----------v----------v-----
Linus Walleijc5dd5532012-09-26 17:21:36 +02001934 * NOTE: lcrh_tx and lcrh_rx MUST BE WRITTEN AFTER
Russell King9f25bc52015-11-03 14:51:13 +00001935 * REG_FBRD & REG_IBRD.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 * ----------^----------^----------^----------^-----
1937 */
Jon Medhurstb60f2f62013-12-10 10:18:59 +00001938 pl011_write_lcr_h(uap, lcr_h);
Russell King9f25bc52015-11-03 14:51:13 +00001939 pl011_write(old_cr, uap, REG_CR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940
1941 spin_unlock_irqrestore(&port->lock, flags);
1942}
1943
Andre Przywara0dd1e242015-05-21 17:26:23 +01001944static void
1945sbsa_uart_set_termios(struct uart_port *port, struct ktermios *termios,
1946 struct ktermios *old)
1947{
1948 struct uart_amba_port *uap =
1949 container_of(port, struct uart_amba_port, port);
1950 unsigned long flags;
1951
1952 tty_termios_encode_baud_rate(termios, uap->fixed_baud, uap->fixed_baud);
1953
1954 /* The SBSA UART only supports 8n1 without hardware flow control. */
1955 termios->c_cflag &= ~(CSIZE | CSTOPB | PARENB | PARODD);
1956 termios->c_cflag &= ~(CMSPAR | CRTSCTS);
1957 termios->c_cflag |= CS8 | CLOCAL;
1958
1959 spin_lock_irqsave(&port->lock, flags);
1960 uart_update_timeout(port, CS8, uap->fixed_baud);
1961 pl011_setup_status_masks(port, termios);
1962 spin_unlock_irqrestore(&port->lock, flags);
1963}
1964
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965static const char *pl011_type(struct uart_port *port)
1966{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001967 struct uart_amba_port *uap =
1968 container_of(port, struct uart_amba_port, port);
Russell Kinge8a7ba82010-12-28 09:16:54 +00001969 return uap->port.type == PORT_AMBA ? uap->type : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970}
1971
1972/*
1973 * Release the memory region(s) being used by 'port'
1974 */
Linus Walleije643f872012-06-17 15:44:19 +02001975static void pl011_release_port(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976{
1977 release_mem_region(port->mapbase, SZ_4K);
1978}
1979
1980/*
1981 * Request the memory region(s) being used by 'port'
1982 */
Linus Walleije643f872012-06-17 15:44:19 +02001983static int pl011_request_port(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984{
1985 return request_mem_region(port->mapbase, SZ_4K, "uart-pl011")
1986 != NULL ? 0 : -EBUSY;
1987}
1988
1989/*
1990 * Configure/autoconfigure the port.
1991 */
Linus Walleije643f872012-06-17 15:44:19 +02001992static void pl011_config_port(struct uart_port *port, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993{
1994 if (flags & UART_CONFIG_TYPE) {
1995 port->type = PORT_AMBA;
Linus Walleije643f872012-06-17 15:44:19 +02001996 pl011_request_port(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 }
1998}
1999
2000/*
2001 * verify the new serial_struct (for TIOCSSERIAL).
2002 */
Linus Walleije643f872012-06-17 15:44:19 +02002003static int pl011_verify_port(struct uart_port *port, struct serial_struct *ser)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004{
2005 int ret = 0;
2006 if (ser->type != PORT_UNKNOWN && ser->type != PORT_AMBA)
2007 ret = -EINVAL;
Yinghai Lua62c4132008-08-19 20:49:55 -07002008 if (ser->irq < 0 || ser->irq >= nr_irqs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 ret = -EINVAL;
2010 if (ser->baud_base < 9600)
2011 ret = -EINVAL;
2012 return ret;
2013}
2014
2015static struct uart_ops amba_pl011_pops = {
Linus Walleije643f872012-06-17 15:44:19 +02002016 .tx_empty = pl011_tx_empty,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 .set_mctrl = pl011_set_mctrl,
Linus Walleije643f872012-06-17 15:44:19 +02002018 .get_mctrl = pl011_get_mctrl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 .stop_tx = pl011_stop_tx,
2020 .start_tx = pl011_start_tx,
2021 .stop_rx = pl011_stop_rx,
2022 .enable_ms = pl011_enable_ms,
2023 .break_ctl = pl011_break_ctl,
2024 .startup = pl011_startup,
2025 .shutdown = pl011_shutdown,
Russell King68b65f72010-12-22 17:24:39 +00002026 .flush_buffer = pl011_dma_flush_buffer,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 .set_termios = pl011_set_termios,
2028 .type = pl011_type,
Linus Walleije643f872012-06-17 15:44:19 +02002029 .release_port = pl011_release_port,
2030 .request_port = pl011_request_port,
2031 .config_port = pl011_config_port,
2032 .verify_port = pl011_verify_port,
Jason Wessel84b5ae12008-02-20 13:33:39 -06002033#ifdef CONFIG_CONSOLE_POLL
Anton Vorontsovb3564c22012-09-24 14:27:54 -07002034 .poll_init = pl011_hwinit,
Linus Walleije643f872012-06-17 15:44:19 +02002035 .poll_get_char = pl011_get_poll_char,
2036 .poll_put_char = pl011_put_poll_char,
Jason Wessel84b5ae12008-02-20 13:33:39 -06002037#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038};
2039
Andre Przywara0dd1e242015-05-21 17:26:23 +01002040static void sbsa_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
2041{
2042}
2043
2044static unsigned int sbsa_uart_get_mctrl(struct uart_port *port)
2045{
2046 return 0;
2047}
2048
2049static const struct uart_ops sbsa_uart_pops = {
2050 .tx_empty = pl011_tx_empty,
2051 .set_mctrl = sbsa_uart_set_mctrl,
2052 .get_mctrl = sbsa_uart_get_mctrl,
2053 .stop_tx = pl011_stop_tx,
2054 .start_tx = pl011_start_tx,
2055 .stop_rx = pl011_stop_rx,
2056 .startup = sbsa_uart_startup,
2057 .shutdown = sbsa_uart_shutdown,
2058 .set_termios = sbsa_uart_set_termios,
2059 .type = pl011_type,
2060 .release_port = pl011_release_port,
2061 .request_port = pl011_request_port,
2062 .config_port = pl011_config_port,
2063 .verify_port = pl011_verify_port,
2064#ifdef CONFIG_CONSOLE_POLL
2065 .poll_init = pl011_hwinit,
2066 .poll_get_char = pl011_get_poll_char,
2067 .poll_put_char = pl011_put_poll_char,
2068#endif
2069};
2070
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071static struct uart_amba_port *amba_ports[UART_NR];
2072
2073#ifdef CONFIG_SERIAL_AMBA_PL011_CONSOLE
2074
Russell Kingd3587882006-03-20 20:00:09 +00002075static void pl011_console_putchar(struct uart_port *port, int ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076{
Daniel Thompsona5820c22014-09-03 12:51:55 +01002077 struct uart_amba_port *uap =
2078 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079
Russell King9f25bc52015-11-03 14:51:13 +00002080 while (pl011_read(uap, REG_FR) & UART01x_FR_TXFF)
Russell Kingd3587882006-03-20 20:00:09 +00002081 barrier();
Russell King9f25bc52015-11-03 14:51:13 +00002082 pl011_write(ch, uap, REG_DR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083}
2084
2085static void
2086pl011_console_write(struct console *co, const char *s, unsigned int count)
2087{
2088 struct uart_amba_port *uap = amba_ports[co->index];
Andre Przywara71eec482015-05-21 17:26:21 +01002089 unsigned int status, old_cr = 0, new_cr;
Rabin Vincentef605fd2012-01-17 11:52:28 +01002090 unsigned long flags;
2091 int locked = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092
2093 clk_enable(uap->clk);
2094
Rabin Vincentef605fd2012-01-17 11:52:28 +01002095 local_irq_save(flags);
2096 if (uap->port.sysrq)
2097 locked = 0;
2098 else if (oops_in_progress)
2099 locked = spin_trylock(&uap->port.lock);
2100 else
2101 spin_lock(&uap->port.lock);
2102
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 /*
2104 * First save the CR then disable the interrupts
2105 */
Andre Przywara71eec482015-05-21 17:26:21 +01002106 if (!uap->vendor->always_enabled) {
Russell King9f25bc52015-11-03 14:51:13 +00002107 old_cr = pl011_read(uap, REG_CR);
Andre Przywara71eec482015-05-21 17:26:21 +01002108 new_cr = old_cr & ~UART011_CR_CTSEN;
2109 new_cr |= UART01x_CR_UARTEN | UART011_CR_TXE;
Russell King9f25bc52015-11-03 14:51:13 +00002110 pl011_write(new_cr, uap, REG_CR);
Andre Przywara71eec482015-05-21 17:26:21 +01002111 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112
Russell Kingd3587882006-03-20 20:00:09 +00002113 uart_console_write(&uap->port, s, count, pl011_console_putchar);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114
2115 /*
2116 * Finally, wait for transmitter to become empty
2117 * and restore the TCR
2118 */
2119 do {
Russell King9f25bc52015-11-03 14:51:13 +00002120 status = pl011_read(uap, REG_FR);
Greg Kroah-Hartman062a68a2015-09-04 09:11:24 -07002121 } while (status & UART01x_FR_BUSY);
Andre Przywara71eec482015-05-21 17:26:21 +01002122 if (!uap->vendor->always_enabled)
Russell King9f25bc52015-11-03 14:51:13 +00002123 pl011_write(old_cr, uap, REG_CR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124
Rabin Vincentef605fd2012-01-17 11:52:28 +01002125 if (locked)
2126 spin_unlock(&uap->port.lock);
2127 local_irq_restore(flags);
2128
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 clk_disable(uap->clk);
2130}
2131
2132static void __init
2133pl011_console_get_options(struct uart_amba_port *uap, int *baud,
2134 int *parity, int *bits)
2135{
Russell King9f25bc52015-11-03 14:51:13 +00002136 if (pl011_read(uap, REG_CR) & UART01x_CR_UARTEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 unsigned int lcr_h, ibrd, fbrd;
2138
Russell Kingb2a4e242015-11-03 14:51:03 +00002139 lcr_h = pl011_read(uap, uap->lcrh_tx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140
2141 *parity = 'n';
2142 if (lcr_h & UART01x_LCRH_PEN) {
2143 if (lcr_h & UART01x_LCRH_EPS)
2144 *parity = 'e';
2145 else
2146 *parity = 'o';
2147 }
2148
2149 if ((lcr_h & 0x60) == UART01x_LCRH_WLEN_7)
2150 *bits = 7;
2151 else
2152 *bits = 8;
2153
Russell King9f25bc52015-11-03 14:51:13 +00002154 ibrd = pl011_read(uap, REG_IBRD);
2155 fbrd = pl011_read(uap, REG_FBRD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156
2157 *baud = uap->port.uartclk * 4 / (64 * ibrd + fbrd);
Linus Walleijac3e3fb2010-06-02 20:40:22 +01002158
Russell Kingc19f12b2010-12-22 17:48:26 +00002159 if (uap->vendor->oversampling) {
Russell King9f25bc52015-11-03 14:51:13 +00002160 if (pl011_read(uap, REG_CR)
Linus Walleijac3e3fb2010-06-02 20:40:22 +01002161 & ST_UART011_CR_OVSFACT)
2162 *baud *= 2;
2163 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 }
2165}
2166
2167static int __init pl011_console_setup(struct console *co, char *options)
2168{
2169 struct uart_amba_port *uap;
2170 int baud = 38400;
2171 int bits = 8;
2172 int parity = 'n';
2173 int flow = 'n';
Russell King4b4851c2011-09-22 11:35:30 +01002174 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175
2176 /*
2177 * Check whether an invalid uart number has been specified, and
2178 * if so, search for the first available port that does have
2179 * console support.
2180 */
2181 if (co->index >= UART_NR)
2182 co->index = 0;
2183 uap = amba_ports[co->index];
Russell Kingd28122a2007-01-22 18:59:42 +00002184 if (!uap)
2185 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186
Linus Walleij78d80c52012-05-23 21:18:46 +02002187 /* Allow pins to be muxed in and configured */
Linus Walleij2b996fc2013-06-05 15:36:42 +02002188 pinctrl_pm_select_default_state(uap->port.dev);
Linus Walleij78d80c52012-05-23 21:18:46 +02002189
Russell King4b4851c2011-09-22 11:35:30 +01002190 ret = clk_prepare(uap->clk);
2191 if (ret)
2192 return ret;
2193
Jingoo Han574de552013-07-30 17:06:57 +09002194 if (dev_get_platdata(uap->port.dev)) {
Shreshtha Kumar Sahuc16d51a2011-06-13 10:11:33 +02002195 struct amba_pl011_data *plat;
2196
Jingoo Han574de552013-07-30 17:06:57 +09002197 plat = dev_get_platdata(uap->port.dev);
Shreshtha Kumar Sahuc16d51a2011-06-13 10:11:33 +02002198 if (plat->init)
2199 plat->init();
2200 }
2201
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 uap->port.uartclk = clk_get_rate(uap->clk);
2203
Andre Przywaracefc2d12015-05-21 17:26:22 +01002204 if (uap->vendor->fixed_options) {
2205 baud = uap->fixed_baud;
2206 } else {
2207 if (options)
2208 uart_parse_options(options,
2209 &baud, &parity, &bits, &flow);
2210 else
2211 pl011_console_get_options(uap, &baud, &parity, &bits);
2212 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213
2214 return uart_set_options(&uap->port, co, baud, parity, bits, flow);
2215}
2216
Vincent Sanders2d934862005-09-14 22:36:03 +01002217static struct uart_driver amba_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218static struct console amba_console = {
2219 .name = "ttyAMA",
2220 .write = pl011_console_write,
2221 .device = uart_console_device,
2222 .setup = pl011_console_setup,
2223 .flags = CON_PRINTBUFFER,
2224 .index = -1,
2225 .data = &amba_reg,
2226};
2227
2228#define AMBA_CONSOLE (&amba_console)
Rob Herring0d3c6732014-04-18 17:19:57 -05002229
2230static void pl011_putc(struct uart_port *port, int c)
2231{
Russell King9f25bc52015-11-03 14:51:13 +00002232 while (readl(port->membase + REG_FR) & UART01x_FR_TXFF)
Rob Herring0d3c6732014-04-18 17:19:57 -05002233 ;
Russell King9f25bc52015-11-03 14:51:13 +00002234 writeb(c, port->membase + REG_DR);
2235 while (readl(port->membase + REG_FR) & UART01x_FR_BUSY)
Rob Herring0d3c6732014-04-18 17:19:57 -05002236 ;
2237}
2238
2239static void pl011_early_write(struct console *con, const char *s, unsigned n)
2240{
2241 struct earlycon_device *dev = con->data;
2242
2243 uart_console_write(&dev->port, s, n, pl011_putc);
2244}
2245
2246static int __init pl011_early_console_setup(struct earlycon_device *device,
2247 const char *opt)
2248{
2249 if (!device->port.membase)
2250 return -ENODEV;
2251
2252 device->con->write = pl011_early_write;
2253 return 0;
2254}
2255EARLYCON_DECLARE(pl011, pl011_early_console_setup);
Rob Herring45e0f0f2014-03-27 08:08:03 -05002256OF_EARLYCON_DECLARE(pl011, "arm,pl011", pl011_early_console_setup);
Rob Herring0d3c6732014-04-18 17:19:57 -05002257
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258#else
2259#define AMBA_CONSOLE NULL
2260#endif
2261
2262static struct uart_driver amba_reg = {
2263 .owner = THIS_MODULE,
2264 .driver_name = "ttyAMA",
2265 .dev_name = "ttyAMA",
2266 .major = SERIAL_AMBA_MAJOR,
2267 .minor = SERIAL_AMBA_MINOR,
2268 .nr = UART_NR,
2269 .cons = AMBA_CONSOLE,
2270};
2271
Matthew Leach32614aa2012-08-28 16:41:28 +01002272static int pl011_probe_dt_alias(int index, struct device *dev)
2273{
2274 struct device_node *np;
2275 static bool seen_dev_with_alias = false;
2276 static bool seen_dev_without_alias = false;
2277 int ret = index;
2278
2279 if (!IS_ENABLED(CONFIG_OF))
2280 return ret;
2281
2282 np = dev->of_node;
2283 if (!np)
2284 return ret;
2285
2286 ret = of_alias_get_id(np, "serial");
2287 if (IS_ERR_VALUE(ret)) {
2288 seen_dev_without_alias = true;
2289 ret = index;
2290 } else {
2291 seen_dev_with_alias = true;
2292 if (ret >= ARRAY_SIZE(amba_ports) || amba_ports[ret] != NULL) {
2293 dev_warn(dev, "requested serial port %d not available.\n", ret);
2294 ret = index;
2295 }
2296 }
2297
2298 if (seen_dev_with_alias && seen_dev_without_alias)
2299 dev_warn(dev, "aliased and non-aliased serial devices found in device tree. Serial port enumeration may be unpredictable.\n");
2300
2301 return ret;
2302}
2303
Andre Przywara49bb3c82015-05-21 17:26:14 +01002304/* unregisters the driver also if no more ports are left */
2305static void pl011_unregister_port(struct uart_amba_port *uap)
2306{
2307 int i;
2308 bool busy = false;
2309
2310 for (i = 0; i < ARRAY_SIZE(amba_ports); i++) {
2311 if (amba_ports[i] == uap)
2312 amba_ports[i] = NULL;
2313 else if (amba_ports[i])
2314 busy = true;
2315 }
2316 pl011_dma_remove(uap);
2317 if (!busy)
2318 uart_unregister_driver(&amba_reg);
2319}
2320
Andre Przywara3873e2d2015-05-21 17:26:18 +01002321static int pl011_find_free_port(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322{
Andre Przywara3873e2d2015-05-21 17:26:18 +01002323 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324
2325 for (i = 0; i < ARRAY_SIZE(amba_ports); i++)
2326 if (amba_ports[i] == NULL)
Andre Przywara3873e2d2015-05-21 17:26:18 +01002327 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328
Andre Przywara3873e2d2015-05-21 17:26:18 +01002329 return -EBUSY;
2330}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331
Andre Przywara3873e2d2015-05-21 17:26:18 +01002332static int pl011_setup_port(struct device *dev, struct uart_amba_port *uap,
2333 struct resource *mmiobase, int index)
2334{
2335 void __iomem *base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336
Andre Przywara3873e2d2015-05-21 17:26:18 +01002337 base = devm_ioremap_resource(dev, mmiobase);
Krzysztof Kozlowski97a60ea2015-07-09 22:21:41 +09002338 if (IS_ERR(base))
2339 return PTR_ERR(base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340
Andre Przywara3873e2d2015-05-21 17:26:18 +01002341 index = pl011_probe_dt_alias(index, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342
Shreshtha Kumar Sahud8d8ffa2012-01-18 15:53:59 +05302343 uap->old_cr = 0;
Andre Przywara3873e2d2015-05-21 17:26:18 +01002344 uap->port.dev = dev;
2345 uap->port.mapbase = mmiobase->start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 uap->port.membase = base;
2347 uap->port.iotype = UPIO_MEM;
Russell Kingffca2b12010-12-22 17:13:05 +00002348 uap->port.fifosize = uap->fifosize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 uap->port.flags = UPF_BOOT_AUTOCONF;
Andre Przywara3873e2d2015-05-21 17:26:18 +01002350 uap->port.line = index;
2351
2352 amba_ports[index] = uap;
2353
2354 return 0;
2355}
2356
2357static int pl011_register_port(struct uart_amba_port *uap)
2358{
2359 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360
Linus Walleijc3d8b762012-03-21 20:15:18 +01002361 /* Ensure interrupts from this UART are masked and cleared */
Russell King9f25bc52015-11-03 14:51:13 +00002362 pl011_write(0, uap, REG_IMSC);
2363 pl011_write(0xffff, uap, REG_ICR);
Linus Walleijc3d8b762012-03-21 20:15:18 +01002364
Tushar Beheraef2889f2014-01-20 14:32:35 +05302365 if (!amba_reg.state) {
2366 ret = uart_register_driver(&amba_reg);
2367 if (ret < 0) {
Andre Przywara3873e2d2015-05-21 17:26:18 +01002368 dev_err(uap->port.dev,
Jorge Ramirez-Ortiz1c9be312015-03-06 13:05:40 -05002369 "Failed to register AMBA-PL011 driver\n");
Tushar Beheraef2889f2014-01-20 14:32:35 +05302370 return ret;
2371 }
2372 }
2373
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 ret = uart_add_one_port(&amba_reg, &uap->port);
Andre Przywara49bb3c82015-05-21 17:26:14 +01002375 if (ret)
2376 pl011_unregister_port(uap);
Tushar Behera7f6d9422014-06-26 15:35:35 +05302377
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 return ret;
2379}
2380
Andre Przywara3873e2d2015-05-21 17:26:18 +01002381static int pl011_probe(struct amba_device *dev, const struct amba_id *id)
2382{
2383 struct uart_amba_port *uap;
2384 struct vendor_data *vendor = id->data;
2385 int portnr, ret;
2386
2387 portnr = pl011_find_free_port();
2388 if (portnr < 0)
2389 return portnr;
2390
2391 uap = devm_kzalloc(&dev->dev, sizeof(struct uart_amba_port),
2392 GFP_KERNEL);
2393 if (!uap)
2394 return -ENOMEM;
2395
2396 uap->clk = devm_clk_get(&dev->dev, NULL);
2397 if (IS_ERR(uap->clk))
2398 return PTR_ERR(uap->clk);
2399
2400 uap->vendor = vendor;
2401 uap->lcrh_rx = vendor->lcrh_rx;
2402 uap->lcrh_tx = vendor->lcrh_tx;
2403 uap->fifosize = vendor->get_fifosize(dev);
2404 uap->port.irq = dev->irq[0];
2405 uap->port.ops = &amba_pl011_pops;
2406
2407 snprintf(uap->type, sizeof(uap->type), "PL011 rev%u", amba_rev(dev));
2408
2409 ret = pl011_setup_port(&dev->dev, uap, &dev->res, portnr);
2410 if (ret)
2411 return ret;
2412
2413 amba_set_drvdata(dev, uap);
2414
2415 return pl011_register_port(uap);
2416}
2417
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418static int pl011_remove(struct amba_device *dev)
2419{
2420 struct uart_amba_port *uap = amba_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 uart_remove_one_port(&amba_reg, &uap->port);
Andre Przywara49bb3c82015-05-21 17:26:14 +01002423 pl011_unregister_port(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424 return 0;
2425}
2426
Ulf Hanssond0ce8502013-12-03 11:04:28 +01002427#ifdef CONFIG_PM_SLEEP
2428static int pl011_suspend(struct device *dev)
Leo Chenb736b892009-07-28 23:43:33 +01002429{
Ulf Hanssond0ce8502013-12-03 11:04:28 +01002430 struct uart_amba_port *uap = dev_get_drvdata(dev);
Leo Chenb736b892009-07-28 23:43:33 +01002431
2432 if (!uap)
2433 return -EINVAL;
2434
2435 return uart_suspend_port(&amba_reg, &uap->port);
2436}
2437
Ulf Hanssond0ce8502013-12-03 11:04:28 +01002438static int pl011_resume(struct device *dev)
Leo Chenb736b892009-07-28 23:43:33 +01002439{
Ulf Hanssond0ce8502013-12-03 11:04:28 +01002440 struct uart_amba_port *uap = dev_get_drvdata(dev);
Leo Chenb736b892009-07-28 23:43:33 +01002441
2442 if (!uap)
2443 return -EINVAL;
2444
2445 return uart_resume_port(&amba_reg, &uap->port);
2446}
2447#endif
2448
Ulf Hanssond0ce8502013-12-03 11:04:28 +01002449static SIMPLE_DEV_PM_OPS(pl011_dev_pm_ops, pl011_suspend, pl011_resume);
2450
Andre Przywara0dd1e242015-05-21 17:26:23 +01002451static int sbsa_uart_probe(struct platform_device *pdev)
2452{
2453 struct uart_amba_port *uap;
2454 struct resource *r;
2455 int portnr, ret;
2456 int baudrate;
2457
2458 /*
2459 * Check the mandatory baud rate parameter in the DT node early
2460 * so that we can easily exit with the error.
2461 */
2462 if (pdev->dev.of_node) {
2463 struct device_node *np = pdev->dev.of_node;
2464
2465 ret = of_property_read_u32(np, "current-speed", &baudrate);
2466 if (ret)
2467 return ret;
2468 } else {
2469 baudrate = 115200;
2470 }
2471
2472 portnr = pl011_find_free_port();
2473 if (portnr < 0)
2474 return portnr;
2475
2476 uap = devm_kzalloc(&pdev->dev, sizeof(struct uart_amba_port),
2477 GFP_KERNEL);
2478 if (!uap)
2479 return -ENOMEM;
2480
2481 uap->vendor = &vendor_sbsa;
2482 uap->fifosize = 32;
2483 uap->port.irq = platform_get_irq(pdev, 0);
2484 uap->port.ops = &sbsa_uart_pops;
2485 uap->fixed_baud = baudrate;
2486
2487 snprintf(uap->type, sizeof(uap->type), "SBSA");
2488
2489 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2490
2491 ret = pl011_setup_port(&pdev->dev, uap, r, portnr);
2492 if (ret)
2493 return ret;
2494
2495 platform_set_drvdata(pdev, uap);
2496
2497 return pl011_register_port(uap);
2498}
2499
2500static int sbsa_uart_remove(struct platform_device *pdev)
2501{
2502 struct uart_amba_port *uap = platform_get_drvdata(pdev);
2503
2504 uart_remove_one_port(&amba_reg, &uap->port);
2505 pl011_unregister_port(uap);
2506 return 0;
2507}
2508
2509static const struct of_device_id sbsa_uart_of_match[] = {
2510 { .compatible = "arm,sbsa-uart", },
2511 {},
2512};
2513MODULE_DEVICE_TABLE(of, sbsa_uart_of_match);
2514
Graeme Gregory3db9ab02015-05-21 17:26:24 +01002515static const struct acpi_device_id sbsa_uart_acpi_match[] = {
2516 { "ARMH0011", 0 },
2517 {},
2518};
2519MODULE_DEVICE_TABLE(acpi, sbsa_uart_acpi_match);
2520
Andre Przywara0dd1e242015-05-21 17:26:23 +01002521static struct platform_driver arm_sbsa_uart_platform_driver = {
2522 .probe = sbsa_uart_probe,
2523 .remove = sbsa_uart_remove,
2524 .driver = {
2525 .name = "sbsa-uart",
2526 .of_match_table = of_match_ptr(sbsa_uart_of_match),
Graeme Gregory3db9ab02015-05-21 17:26:24 +01002527 .acpi_match_table = ACPI_PTR(sbsa_uart_acpi_match),
Andre Przywara0dd1e242015-05-21 17:26:23 +01002528 },
2529};
2530
Russell King2c39c9e2010-07-27 08:50:16 +01002531static struct amba_id pl011_ids[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532 {
2533 .id = 0x00041011,
2534 .mask = 0x000fffff,
Alessandro Rubini5926a292009-06-04 17:43:04 +01002535 .data = &vendor_arm,
2536 },
2537 {
2538 .id = 0x00380802,
2539 .mask = 0x00ffffff,
2540 .data = &vendor_st,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541 },
2542 { 0, 0 },
2543};
2544
Dave Martin60f7a332011-10-05 15:15:22 +01002545MODULE_DEVICE_TABLE(amba, pl011_ids);
2546
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547static struct amba_driver pl011_driver = {
2548 .drv = {
2549 .name = "uart-pl011",
Ulf Hanssond0ce8502013-12-03 11:04:28 +01002550 .pm = &pl011_dev_pm_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551 },
2552 .id_table = pl011_ids,
2553 .probe = pl011_probe,
2554 .remove = pl011_remove,
2555};
2556
2557static int __init pl011_init(void)
2558{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559 printk(KERN_INFO "Serial: AMBA PL011 UART driver\n");
2560
Andre Przywara0dd1e242015-05-21 17:26:23 +01002561 if (platform_driver_register(&arm_sbsa_uart_platform_driver))
2562 pr_warn("could not register SBSA UART platform driver\n");
Greg Kroah-Hartman062a68a2015-09-04 09:11:24 -07002563 return amba_driver_register(&pl011_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564}
2565
2566static void __exit pl011_exit(void)
2567{
Andre Przywara0dd1e242015-05-21 17:26:23 +01002568 platform_driver_unregister(&arm_sbsa_uart_platform_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 amba_driver_unregister(&pl011_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570}
2571
Alessandro Rubini4dd9e742009-05-05 05:54:13 +01002572/*
2573 * While this can be a module, if builtin it's most likely the console
2574 * So let's leave module_exit but move module_init to an earlier place
2575 */
2576arch_initcall(pl011_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577module_exit(pl011_exit);
2578
2579MODULE_AUTHOR("ARM Ltd/Deep Blue Solutions Ltd");
2580MODULE_DESCRIPTION("ARM AMBA serial port driver");
2581MODULE_LICENSE("GPL");