blob: 6558400f766ffdba4b690029282fb28dcdfa3dae [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
62#define UART_NR 14
63
64#define SERIAL_AMBA_MAJOR 204
65#define SERIAL_AMBA_MINOR 64
66#define SERIAL_AMBA_NR UART_NR
67
68#define AMBA_ISR_PASS_LIMIT 256
69
Russell Kingb63d4f02005-11-19 11:10:35 +000070#define UART_DR_ERROR (UART011_DR_OE|UART011_DR_BE|UART011_DR_PE|UART011_DR_FE)
71#define UART_DUMMY_DR_RX (1 << 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Alessandro Rubini5926a292009-06-04 17:43:04 +010073/* There is by now at least one vendor with differing details, so handle it */
74struct vendor_data {
75 unsigned int ifls;
Linus Walleijec489aa2010-06-02 08:13:52 +010076 unsigned int lcrh_tx;
77 unsigned int lcrh_rx;
Linus Walleijac3e3fb2010-06-02 20:40:22 +010078 bool oversampling;
Russell King38d62432010-12-22 17:59:16 +000079 bool dma_threshold;
Rajanikanth H.V4fd06902012-03-26 11:17:02 +020080 bool cts_event_workaround;
Andre Przywara71eec482015-05-21 17:26:21 +010081 bool always_enabled;
Andre Przywaracefc2d12015-05-21 17:26:22 +010082 bool fixed_options;
Jongsung Kim78506f22013-04-15 14:45:25 +090083
Jongsung Kimea336402013-05-10 18:05:35 +090084 unsigned int (*get_fifosize)(struct amba_device *dev);
Alessandro Rubini5926a292009-06-04 17:43:04 +010085};
86
Jongsung Kimea336402013-05-10 18:05:35 +090087static unsigned int get_fifosize_arm(struct amba_device *dev)
Jongsung Kim78506f22013-04-15 14:45:25 +090088{
Jongsung Kimea336402013-05-10 18:05:35 +090089 return amba_rev(dev) < 3 ? 16 : 32;
Jongsung Kim78506f22013-04-15 14:45:25 +090090}
91
Alessandro Rubini5926a292009-06-04 17:43:04 +010092static struct vendor_data vendor_arm = {
93 .ifls = UART011_IFLS_RX4_8|UART011_IFLS_TX4_8,
Linus Walleijec489aa2010-06-02 08:13:52 +010094 .lcrh_tx = UART011_LCRH,
95 .lcrh_rx = UART011_LCRH,
Linus Walleijac3e3fb2010-06-02 20:40:22 +010096 .oversampling = false,
Russell King38d62432010-12-22 17:59:16 +000097 .dma_threshold = false,
Rajanikanth H.V4fd06902012-03-26 11:17:02 +020098 .cts_event_workaround = false,
Andre Przywara71eec482015-05-21 17:26:21 +010099 .always_enabled = false,
Andre Przywaracefc2d12015-05-21 17:26:22 +0100100 .fixed_options = false,
Jongsung Kim78506f22013-04-15 14:45:25 +0900101 .get_fifosize = get_fifosize_arm,
Alessandro Rubini5926a292009-06-04 17:43:04 +0100102};
103
Jongsung Kimea336402013-05-10 18:05:35 +0900104static unsigned int get_fifosize_st(struct amba_device *dev)
Jongsung Kim78506f22013-04-15 14:45:25 +0900105{
106 return 64;
107}
108
Alessandro Rubini5926a292009-06-04 17:43:04 +0100109static struct vendor_data vendor_st = {
110 .ifls = UART011_IFLS_RX_HALF|UART011_IFLS_TX_HALF,
Linus Walleijec489aa2010-06-02 08:13:52 +0100111 .lcrh_tx = ST_UART011_LCRH_TX,
112 .lcrh_rx = ST_UART011_LCRH_RX,
Linus Walleijac3e3fb2010-06-02 20:40:22 +0100113 .oversampling = true,
Russell King38d62432010-12-22 17:59:16 +0000114 .dma_threshold = true,
Rajanikanth H.V4fd06902012-03-26 11:17:02 +0200115 .cts_event_workaround = true,
Andre Przywara71eec482015-05-21 17:26:21 +0100116 .always_enabled = false,
Andre Przywaracefc2d12015-05-21 17:26:22 +0100117 .fixed_options = false,
Jongsung Kim78506f22013-04-15 14:45:25 +0900118 .get_fifosize = get_fifosize_st,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119};
120
Russell King68b65f72010-12-22 17:24:39 +0000121/* Deals with DMA transactions */
Linus Walleijead76f32011-02-24 13:21:08 +0100122
123struct pl011_sgbuf {
124 struct scatterlist sg;
125 char *buf;
126};
127
128struct pl011_dmarx_data {
129 struct dma_chan *chan;
130 struct completion complete;
131 bool use_buf_b;
132 struct pl011_sgbuf sgbuf_a;
133 struct pl011_sgbuf sgbuf_b;
134 dma_cookie_t cookie;
135 bool running;
Chanho Mincb06ff12013-03-27 18:38:11 +0900136 struct timer_list timer;
137 unsigned int last_residue;
138 unsigned long last_jiffies;
139 bool auto_poll_rate;
140 unsigned int poll_rate;
141 unsigned int poll_timeout;
Linus Walleijead76f32011-02-24 13:21:08 +0100142};
143
Russell King68b65f72010-12-22 17:24:39 +0000144struct pl011_dmatx_data {
145 struct dma_chan *chan;
146 struct scatterlist sg;
147 char *buf;
148 bool queued;
149};
150
Russell Kingc19f12b2010-12-22 17:48:26 +0000151/*
152 * We wrap our port structure around the generic uart_port.
153 */
154struct uart_amba_port {
155 struct uart_port port;
156 struct clk *clk;
157 const struct vendor_data *vendor;
Russell King68b65f72010-12-22 17:24:39 +0000158 unsigned int dmacr; /* dma control reg */
Russell Kingc19f12b2010-12-22 17:48:26 +0000159 unsigned int im; /* interrupt mask */
160 unsigned int old_status;
Russell Kingffca2b12010-12-22 17:13:05 +0000161 unsigned int fifosize; /* vendor-specific */
Russell Kingc19f12b2010-12-22 17:48:26 +0000162 unsigned int lcrh_tx; /* vendor-specific */
163 unsigned int lcrh_rx; /* vendor-specific */
Shreshtha Kumar Sahud8d8ffa2012-01-18 15:53:59 +0530164 unsigned int old_cr; /* state during shutdown */
Russell Kingc19f12b2010-12-22 17:48:26 +0000165 bool autorts;
Andre Przywaracefc2d12015-05-21 17:26:22 +0100166 unsigned int fixed_baud; /* vendor-set fixed baud rate */
Russell Kingc19f12b2010-12-22 17:48:26 +0000167 char type[12];
Russell King68b65f72010-12-22 17:24:39 +0000168#ifdef CONFIG_DMA_ENGINE
169 /* DMA stuff */
Linus Walleijead76f32011-02-24 13:21:08 +0100170 bool using_tx_dma;
171 bool using_rx_dma;
172 struct pl011_dmarx_data dmarx;
Russell King68b65f72010-12-22 17:24:39 +0000173 struct pl011_dmatx_data dmatx;
Jorge Ramirez-Ortiz1c9be312015-03-06 13:05:40 -0500174 bool dma_probed;
Russell King68b65f72010-12-22 17:24:39 +0000175#endif
Russell Kingc19f12b2010-12-22 17:48:26 +0000176};
177
Russell King68b65f72010-12-22 17:24:39 +0000178/*
Linus Walleij29772c42011-02-24 13:21:36 +0100179 * Reads up to 256 characters from the FIFO or until it's empty and
180 * inserts them into the TTY layer. Returns the number of characters
181 * read from the FIFO.
182 */
183static int pl011_fifo_to_tty(struct uart_amba_port *uap)
184{
185 u16 status, ch;
186 unsigned int flag, max_count = 256;
187 int fifotaken = 0;
188
189 while (max_count--) {
190 status = readw(uap->port.membase + UART01x_FR);
191 if (status & UART01x_FR_RXFE)
192 break;
193
194 /* Take chars from the FIFO and update status */
195 ch = readw(uap->port.membase + UART01x_DR) |
196 UART_DUMMY_DR_RX;
197 flag = TTY_NORMAL;
198 uap->port.icount.rx++;
199 fifotaken++;
200
201 if (unlikely(ch & UART_DR_ERROR)) {
202 if (ch & UART011_DR_BE) {
203 ch &= ~(UART011_DR_FE | UART011_DR_PE);
204 uap->port.icount.brk++;
205 if (uart_handle_break(&uap->port))
206 continue;
207 } else if (ch & UART011_DR_PE)
208 uap->port.icount.parity++;
209 else if (ch & UART011_DR_FE)
210 uap->port.icount.frame++;
211 if (ch & UART011_DR_OE)
212 uap->port.icount.overrun++;
213
214 ch &= uap->port.read_status_mask;
215
216 if (ch & UART011_DR_BE)
217 flag = TTY_BREAK;
218 else if (ch & UART011_DR_PE)
219 flag = TTY_PARITY;
220 else if (ch & UART011_DR_FE)
221 flag = TTY_FRAME;
222 }
223
224 if (uart_handle_sysrq_char(&uap->port, ch & 255))
225 continue;
226
227 uart_insert_char(&uap->port, ch, UART011_DR_OE, ch, flag);
228 }
229
230 return fifotaken;
231}
232
233
234/*
Russell King68b65f72010-12-22 17:24:39 +0000235 * All the DMA operation mode stuff goes inside this ifdef.
236 * This assumes that you have a generic DMA device interface,
237 * no custom DMA interfaces are supported.
238 */
239#ifdef CONFIG_DMA_ENGINE
240
241#define PL011_DMA_BUFFER_SIZE PAGE_SIZE
242
Linus Walleijead76f32011-02-24 13:21:08 +0100243static int pl011_sgbuf_init(struct dma_chan *chan, struct pl011_sgbuf *sg,
244 enum dma_data_direction dir)
245{
Chanho Mincb06ff12013-03-27 18:38:11 +0900246 dma_addr_t dma_addr;
247
248 sg->buf = dma_alloc_coherent(chan->device->dev,
249 PL011_DMA_BUFFER_SIZE, &dma_addr, GFP_KERNEL);
Linus Walleijead76f32011-02-24 13:21:08 +0100250 if (!sg->buf)
251 return -ENOMEM;
252
Chanho Mincb06ff12013-03-27 18:38:11 +0900253 sg_init_table(&sg->sg, 1);
254 sg_set_page(&sg->sg, phys_to_page(dma_addr),
255 PL011_DMA_BUFFER_SIZE, offset_in_page(dma_addr));
256 sg_dma_address(&sg->sg) = dma_addr;
Andrew Jacksonc64be922014-11-07 14:14:43 +0000257 sg_dma_len(&sg->sg) = PL011_DMA_BUFFER_SIZE;
Linus Walleijead76f32011-02-24 13:21:08 +0100258
Linus Walleijead76f32011-02-24 13:21:08 +0100259 return 0;
260}
261
262static void pl011_sgbuf_free(struct dma_chan *chan, struct pl011_sgbuf *sg,
263 enum dma_data_direction dir)
264{
265 if (sg->buf) {
Chanho Mincb06ff12013-03-27 18:38:11 +0900266 dma_free_coherent(chan->device->dev,
267 PL011_DMA_BUFFER_SIZE, sg->buf,
268 sg_dma_address(&sg->sg));
Linus Walleijead76f32011-02-24 13:21:08 +0100269 }
270}
271
Jorge Ramirez-Ortiz1c9be312015-03-06 13:05:40 -0500272static void pl011_dma_probe(struct uart_amba_port *uap)
Russell King68b65f72010-12-22 17:24:39 +0000273{
274 /* DMA is the sole user of the platform data right now */
Jingoo Han574de552013-07-30 17:06:57 +0900275 struct amba_pl011_data *plat = dev_get_platdata(uap->port.dev);
Jorge Ramirez-Ortiz1c9be312015-03-06 13:05:40 -0500276 struct device *dev = uap->port.dev;
Russell King68b65f72010-12-22 17:24:39 +0000277 struct dma_slave_config tx_conf = {
278 .dst_addr = uap->port.mapbase + UART01x_DR,
279 .dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE,
Vinod Koula485df42011-10-14 10:47:38 +0530280 .direction = DMA_MEM_TO_DEV,
Russell King68b65f72010-12-22 17:24:39 +0000281 .dst_maxburst = uap->fifosize >> 1,
Viresh Kumar258aea72012-02-01 16:12:19 +0530282 .device_fc = false,
Russell King68b65f72010-12-22 17:24:39 +0000283 };
284 struct dma_chan *chan;
285 dma_cap_mask_t mask;
286
Jorge Ramirez-Ortiz1c9be312015-03-06 13:05:40 -0500287 uap->dma_probed = true;
288 chan = dma_request_slave_channel_reason(dev, "tx");
289 if (IS_ERR(chan)) {
290 if (PTR_ERR(chan) == -EPROBE_DEFER) {
Jorge Ramirez-Ortiz1c9be312015-03-06 13:05:40 -0500291 uap->dma_probed = false;
292 return;
293 }
Russell King68b65f72010-12-22 17:24:39 +0000294
Arnd Bergmann787b0c12013-01-28 16:24:37 +0000295 /* We need platform data */
296 if (!plat || !plat->dma_filter) {
297 dev_info(uap->port.dev, "no DMA platform data\n");
298 return;
299 }
300
301 /* Try to acquire a generic DMA engine slave TX channel */
302 dma_cap_zero(mask);
303 dma_cap_set(DMA_SLAVE, mask);
304
305 chan = dma_request_channel(mask, plat->dma_filter,
306 plat->dma_tx_param);
307 if (!chan) {
308 dev_err(uap->port.dev, "no TX DMA channel!\n");
309 return;
310 }
Russell King68b65f72010-12-22 17:24:39 +0000311 }
312
313 dmaengine_slave_config(chan, &tx_conf);
314 uap->dmatx.chan = chan;
315
316 dev_info(uap->port.dev, "DMA channel TX %s\n",
317 dma_chan_name(uap->dmatx.chan));
Linus Walleijead76f32011-02-24 13:21:08 +0100318
319 /* Optionally make use of an RX channel as well */
Arnd Bergmann787b0c12013-01-28 16:24:37 +0000320 chan = dma_request_slave_channel(dev, "rx");
Rob Herring0d3c6732014-04-18 17:19:57 -0500321
Arnd Bergmann787b0c12013-01-28 16:24:37 +0000322 if (!chan && plat->dma_rx_param) {
323 chan = dma_request_channel(mask, plat->dma_filter, plat->dma_rx_param);
324
325 if (!chan) {
326 dev_err(uap->port.dev, "no RX DMA channel!\n");
327 return;
328 }
329 }
330
331 if (chan) {
Linus Walleijead76f32011-02-24 13:21:08 +0100332 struct dma_slave_config rx_conf = {
333 .src_addr = uap->port.mapbase + UART01x_DR,
334 .src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE,
Vinod Koula485df42011-10-14 10:47:38 +0530335 .direction = DMA_DEV_TO_MEM,
Guennadi Liakhovetskib2aeb772014-04-12 19:47:17 +0200336 .src_maxburst = uap->fifosize >> 2,
Viresh Kumar258aea72012-02-01 16:12:19 +0530337 .device_fc = false,
Linus Walleijead76f32011-02-24 13:21:08 +0100338 };
Andrew Jackson2d3b7d62014-11-07 14:14:47 +0000339 struct dma_slave_caps caps;
Linus Walleijead76f32011-02-24 13:21:08 +0100340
Andrew Jackson2d3b7d62014-11-07 14:14:47 +0000341 /*
342 * Some DMA controllers provide information on their capabilities.
343 * If the controller does, check for suitable residue processing
344 * otherwise assime all is well.
345 */
346 if (0 == dma_get_slave_caps(chan, &caps)) {
347 if (caps.residue_granularity ==
348 DMA_RESIDUE_GRANULARITY_DESCRIPTOR) {
349 dma_release_channel(chan);
350 dev_info(uap->port.dev,
351 "RX DMA disabled - no residue processing\n");
352 return;
353 }
354 }
Linus Walleijead76f32011-02-24 13:21:08 +0100355 dmaengine_slave_config(chan, &rx_conf);
356 uap->dmarx.chan = chan;
357
Andrew Jackson98267d32014-11-07 14:14:23 +0000358 uap->dmarx.auto_poll_rate = false;
Greg Kroah-Hartman8f898bf2013-12-17 09:33:18 -0800359 if (plat && plat->dma_rx_poll_enable) {
Chanho Mincb06ff12013-03-27 18:38:11 +0900360 /* Set poll rate if specified. */
361 if (plat->dma_rx_poll_rate) {
362 uap->dmarx.auto_poll_rate = false;
363 uap->dmarx.poll_rate = plat->dma_rx_poll_rate;
364 } else {
365 /*
366 * 100 ms defaults to poll rate if not
367 * specified. This will be adjusted with
368 * the baud rate at set_termios.
369 */
370 uap->dmarx.auto_poll_rate = true;
371 uap->dmarx.poll_rate = 100;
372 }
373 /* 3 secs defaults poll_timeout if not specified. */
374 if (plat->dma_rx_poll_timeout)
375 uap->dmarx.poll_timeout =
376 plat->dma_rx_poll_timeout;
377 else
378 uap->dmarx.poll_timeout = 3000;
Andrew Jackson98267d32014-11-07 14:14:23 +0000379 } else if (!plat && dev->of_node) {
380 uap->dmarx.auto_poll_rate = of_property_read_bool(
381 dev->of_node, "auto-poll");
382 if (uap->dmarx.auto_poll_rate) {
383 u32 x;
Chanho Mincb06ff12013-03-27 18:38:11 +0900384
Andrew Jackson98267d32014-11-07 14:14:23 +0000385 if (0 == of_property_read_u32(dev->of_node,
386 "poll-rate-ms", &x))
387 uap->dmarx.poll_rate = x;
388 else
389 uap->dmarx.poll_rate = 100;
390 if (0 == of_property_read_u32(dev->of_node,
391 "poll-timeout-ms", &x))
392 uap->dmarx.poll_timeout = x;
393 else
394 uap->dmarx.poll_timeout = 3000;
395 }
396 }
Linus Walleijead76f32011-02-24 13:21:08 +0100397 dev_info(uap->port.dev, "DMA channel RX %s\n",
398 dma_chan_name(uap->dmarx.chan));
399 }
Russell King68b65f72010-12-22 17:24:39 +0000400}
401
Russell King68b65f72010-12-22 17:24:39 +0000402static void pl011_dma_remove(struct uart_amba_port *uap)
403{
Russell King68b65f72010-12-22 17:24:39 +0000404 if (uap->dmatx.chan)
405 dma_release_channel(uap->dmatx.chan);
Linus Walleijead76f32011-02-24 13:21:08 +0100406 if (uap->dmarx.chan)
407 dma_release_channel(uap->dmarx.chan);
Russell King68b65f72010-12-22 17:24:39 +0000408}
409
Dave Martin734745c2015-03-04 12:27:33 +0000410/* Forward declare these for the refill routine */
Russell King68b65f72010-12-22 17:24:39 +0000411static int pl011_dma_tx_refill(struct uart_amba_port *uap);
Dave Martin734745c2015-03-04 12:27:33 +0000412static void pl011_start_tx_pio(struct uart_amba_port *uap);
Russell King68b65f72010-12-22 17:24:39 +0000413
414/*
415 * The current DMA TX buffer has been sent.
416 * Try to queue up another DMA buffer.
417 */
418static void pl011_dma_tx_callback(void *data)
419{
420 struct uart_amba_port *uap = data;
421 struct pl011_dmatx_data *dmatx = &uap->dmatx;
422 unsigned long flags;
423 u16 dmacr;
424
425 spin_lock_irqsave(&uap->port.lock, flags);
426 if (uap->dmatx.queued)
427 dma_unmap_sg(dmatx->chan->device->dev, &dmatx->sg, 1,
428 DMA_TO_DEVICE);
429
430 dmacr = uap->dmacr;
431 uap->dmacr = dmacr & ~UART011_TXDMAE;
432 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
433
434 /*
435 * If TX DMA was disabled, it means that we've stopped the DMA for
436 * some reason (eg, XOFF received, or we want to send an X-char.)
437 *
438 * Note: we need to be careful here of a potential race between DMA
439 * and the rest of the driver - if the driver disables TX DMA while
440 * a TX buffer completing, we must update the tx queued status to
441 * get further refills (hence we check dmacr).
442 */
443 if (!(dmacr & UART011_TXDMAE) || uart_tx_stopped(&uap->port) ||
444 uart_circ_empty(&uap->port.state->xmit)) {
445 uap->dmatx.queued = false;
446 spin_unlock_irqrestore(&uap->port.lock, flags);
447 return;
448 }
449
Dave Martin734745c2015-03-04 12:27:33 +0000450 if (pl011_dma_tx_refill(uap) <= 0)
Russell King68b65f72010-12-22 17:24:39 +0000451 /*
452 * We didn't queue a DMA buffer for some reason, but we
453 * have data pending to be sent. Re-enable the TX IRQ.
454 */
Dave Martin734745c2015-03-04 12:27:33 +0000455 pl011_start_tx_pio(uap);
456
Russell King68b65f72010-12-22 17:24:39 +0000457 spin_unlock_irqrestore(&uap->port.lock, flags);
458}
459
460/*
461 * Try to refill the TX DMA buffer.
462 * Locking: called with port lock held and IRQs disabled.
463 * Returns:
464 * 1 if we queued up a TX DMA buffer.
465 * 0 if we didn't want to handle this by DMA
466 * <0 on error
467 */
468static int pl011_dma_tx_refill(struct uart_amba_port *uap)
469{
470 struct pl011_dmatx_data *dmatx = &uap->dmatx;
471 struct dma_chan *chan = dmatx->chan;
472 struct dma_device *dma_dev = chan->device;
473 struct dma_async_tx_descriptor *desc;
474 struct circ_buf *xmit = &uap->port.state->xmit;
475 unsigned int count;
476
477 /*
478 * Try to avoid the overhead involved in using DMA if the
479 * transaction fits in the first half of the FIFO, by using
480 * the standard interrupt handling. This ensures that we
481 * issue a uart_write_wakeup() at the appropriate time.
482 */
483 count = uart_circ_chars_pending(xmit);
484 if (count < (uap->fifosize >> 1)) {
485 uap->dmatx.queued = false;
486 return 0;
487 }
488
489 /*
490 * Bodge: don't send the last character by DMA, as this
491 * will prevent XON from notifying us to restart DMA.
492 */
493 count -= 1;
494
495 /* Else proceed to copy the TX chars to the DMA buffer and fire DMA */
496 if (count > PL011_DMA_BUFFER_SIZE)
497 count = PL011_DMA_BUFFER_SIZE;
498
499 if (xmit->tail < xmit->head)
500 memcpy(&dmatx->buf[0], &xmit->buf[xmit->tail], count);
501 else {
502 size_t first = UART_XMIT_SIZE - xmit->tail;
Andrew Jacksone2a545a2014-11-07 14:14:39 +0000503 size_t second;
504
505 if (first > count)
506 first = count;
507 second = count - first;
Russell King68b65f72010-12-22 17:24:39 +0000508
509 memcpy(&dmatx->buf[0], &xmit->buf[xmit->tail], first);
510 if (second)
511 memcpy(&dmatx->buf[first], &xmit->buf[0], second);
512 }
513
514 dmatx->sg.length = count;
515
516 if (dma_map_sg(dma_dev->dev, &dmatx->sg, 1, DMA_TO_DEVICE) != 1) {
517 uap->dmatx.queued = false;
518 dev_dbg(uap->port.dev, "unable to map TX DMA\n");
519 return -EBUSY;
520 }
521
Alexandre Bounine16052822012-03-08 16:11:18 -0500522 desc = dmaengine_prep_slave_sg(chan, &dmatx->sg, 1, DMA_MEM_TO_DEV,
Russell King68b65f72010-12-22 17:24:39 +0000523 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
524 if (!desc) {
525 dma_unmap_sg(dma_dev->dev, &dmatx->sg, 1, DMA_TO_DEVICE);
526 uap->dmatx.queued = false;
527 /*
528 * If DMA cannot be used right now, we complete this
529 * transaction via IRQ and let the TTY layer retry.
530 */
531 dev_dbg(uap->port.dev, "TX DMA busy\n");
532 return -EBUSY;
533 }
534
535 /* Some data to go along to the callback */
536 desc->callback = pl011_dma_tx_callback;
537 desc->callback_param = uap;
538
539 /* All errors should happen at prepare time */
540 dmaengine_submit(desc);
541
542 /* Fire the DMA transaction */
543 dma_dev->device_issue_pending(chan);
544
545 uap->dmacr |= UART011_TXDMAE;
546 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
547 uap->dmatx.queued = true;
548
549 /*
550 * Now we know that DMA will fire, so advance the ring buffer
551 * with the stuff we just dispatched.
552 */
553 xmit->tail = (xmit->tail + count) & (UART_XMIT_SIZE - 1);
554 uap->port.icount.tx += count;
555
556 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
557 uart_write_wakeup(&uap->port);
558
559 return 1;
560}
561
562/*
563 * We received a transmit interrupt without a pending X-char but with
564 * pending characters.
565 * Locking: called with port lock held and IRQs disabled.
566 * Returns:
567 * false if we want to use PIO to transmit
568 * true if we queued a DMA buffer
569 */
570static bool pl011_dma_tx_irq(struct uart_amba_port *uap)
571{
Linus Walleijead76f32011-02-24 13:21:08 +0100572 if (!uap->using_tx_dma)
Russell King68b65f72010-12-22 17:24:39 +0000573 return false;
574
575 /*
576 * If we already have a TX buffer queued, but received a
577 * TX interrupt, it will be because we've just sent an X-char.
578 * Ensure the TX DMA is enabled and the TX IRQ is disabled.
579 */
580 if (uap->dmatx.queued) {
581 uap->dmacr |= UART011_TXDMAE;
582 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
583 uap->im &= ~UART011_TXIM;
584 writew(uap->im, uap->port.membase + UART011_IMSC);
585 return true;
586 }
587
588 /*
589 * We don't have a TX buffer queued, so try to queue one.
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300590 * If we successfully queued a buffer, mask the TX IRQ.
Russell King68b65f72010-12-22 17:24:39 +0000591 */
592 if (pl011_dma_tx_refill(uap) > 0) {
593 uap->im &= ~UART011_TXIM;
594 writew(uap->im, uap->port.membase + UART011_IMSC);
595 return true;
596 }
597 return false;
598}
599
600/*
601 * Stop the DMA transmit (eg, due to received XOFF).
602 * Locking: called with port lock held and IRQs disabled.
603 */
604static inline void pl011_dma_tx_stop(struct uart_amba_port *uap)
605{
606 if (uap->dmatx.queued) {
607 uap->dmacr &= ~UART011_TXDMAE;
608 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
609 }
610}
611
612/*
613 * Try to start a DMA transmit, or in the case of an XON/OFF
614 * character queued for send, try to get that character out ASAP.
615 * Locking: called with port lock held and IRQs disabled.
616 * Returns:
617 * false if we want the TX IRQ to be enabled
618 * true if we have a buffer queued
619 */
620static inline bool pl011_dma_tx_start(struct uart_amba_port *uap)
621{
622 u16 dmacr;
623
Linus Walleijead76f32011-02-24 13:21:08 +0100624 if (!uap->using_tx_dma)
Russell King68b65f72010-12-22 17:24:39 +0000625 return false;
626
627 if (!uap->port.x_char) {
628 /* no X-char, try to push chars out in DMA mode */
629 bool ret = true;
630
631 if (!uap->dmatx.queued) {
632 if (pl011_dma_tx_refill(uap) > 0) {
633 uap->im &= ~UART011_TXIM;
Dave Martin734745c2015-03-04 12:27:33 +0000634 writew(uap->im, uap->port.membase +
635 UART011_IMSC);
636 } else
Russell King68b65f72010-12-22 17:24:39 +0000637 ret = false;
Russell King68b65f72010-12-22 17:24:39 +0000638 } else if (!(uap->dmacr & UART011_TXDMAE)) {
639 uap->dmacr |= UART011_TXDMAE;
640 writew(uap->dmacr,
641 uap->port.membase + UART011_DMACR);
642 }
643 return ret;
644 }
645
646 /*
647 * We have an X-char to send. Disable DMA to prevent it loading
648 * the TX fifo, and then see if we can stuff it into the FIFO.
649 */
650 dmacr = uap->dmacr;
651 uap->dmacr &= ~UART011_TXDMAE;
652 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
653
654 if (readw(uap->port.membase + UART01x_FR) & UART01x_FR_TXFF) {
655 /*
656 * No space in the FIFO, so enable the transmit interrupt
657 * so we know when there is space. Note that once we've
658 * loaded the character, we should just re-enable DMA.
659 */
660 return false;
661 }
662
663 writew(uap->port.x_char, uap->port.membase + UART01x_DR);
664 uap->port.icount.tx++;
665 uap->port.x_char = 0;
666
667 /* Success - restore the DMA state */
668 uap->dmacr = dmacr;
669 writew(dmacr, uap->port.membase + UART011_DMACR);
670
671 return true;
672}
673
674/*
675 * Flush the transmit buffer.
676 * Locking: called with port lock held and IRQs disabled.
677 */
678static void pl011_dma_flush_buffer(struct uart_port *port)
Fabio Estevamb83286b2013-08-09 17:58:51 -0300679__releases(&uap->port.lock)
680__acquires(&uap->port.lock)
Russell King68b65f72010-12-22 17:24:39 +0000681{
Daniel Thompsona5820c22014-09-03 12:51:55 +0100682 struct uart_amba_port *uap =
683 container_of(port, struct uart_amba_port, port);
Russell King68b65f72010-12-22 17:24:39 +0000684
Linus Walleijead76f32011-02-24 13:21:08 +0100685 if (!uap->using_tx_dma)
Russell King68b65f72010-12-22 17:24:39 +0000686 return;
687
688 /* Avoid deadlock with the DMA engine callback */
689 spin_unlock(&uap->port.lock);
690 dmaengine_terminate_all(uap->dmatx.chan);
691 spin_lock(&uap->port.lock);
692 if (uap->dmatx.queued) {
693 dma_unmap_sg(uap->dmatx.chan->device->dev, &uap->dmatx.sg, 1,
694 DMA_TO_DEVICE);
695 uap->dmatx.queued = false;
696 uap->dmacr &= ~UART011_TXDMAE;
697 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
698 }
699}
700
Linus Walleijead76f32011-02-24 13:21:08 +0100701static void pl011_dma_rx_callback(void *data);
702
703static int pl011_dma_rx_trigger_dma(struct uart_amba_port *uap)
704{
705 struct dma_chan *rxchan = uap->dmarx.chan;
Linus Walleijead76f32011-02-24 13:21:08 +0100706 struct pl011_dmarx_data *dmarx = &uap->dmarx;
707 struct dma_async_tx_descriptor *desc;
708 struct pl011_sgbuf *sgbuf;
709
710 if (!rxchan)
711 return -EIO;
712
713 /* Start the RX DMA job */
714 sgbuf = uap->dmarx.use_buf_b ?
715 &uap->dmarx.sgbuf_b : &uap->dmarx.sgbuf_a;
Alexandre Bounine16052822012-03-08 16:11:18 -0500716 desc = dmaengine_prep_slave_sg(rxchan, &sgbuf->sg, 1,
Vinod Koula485df42011-10-14 10:47:38 +0530717 DMA_DEV_TO_MEM,
Linus Walleijead76f32011-02-24 13:21:08 +0100718 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
719 /*
720 * If the DMA engine is busy and cannot prepare a
721 * channel, no big deal, the driver will fall back
722 * to interrupt mode as a result of this error code.
723 */
724 if (!desc) {
725 uap->dmarx.running = false;
726 dmaengine_terminate_all(rxchan);
727 return -EBUSY;
728 }
729
730 /* Some data to go along to the callback */
731 desc->callback = pl011_dma_rx_callback;
732 desc->callback_param = uap;
733 dmarx->cookie = dmaengine_submit(desc);
734 dma_async_issue_pending(rxchan);
735
736 uap->dmacr |= UART011_RXDMAE;
737 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
738 uap->dmarx.running = true;
739
740 uap->im &= ~UART011_RXIM;
741 writew(uap->im, uap->port.membase + UART011_IMSC);
742
743 return 0;
744}
745
746/*
747 * This is called when either the DMA job is complete, or
748 * the FIFO timeout interrupt occurred. This must be called
749 * with the port spinlock uap->port.lock held.
750 */
751static void pl011_dma_rx_chars(struct uart_amba_port *uap,
752 u32 pending, bool use_buf_b,
753 bool readfifo)
754{
Jiri Slaby05c7cd32013-01-03 15:53:04 +0100755 struct tty_port *port = &uap->port.state->port;
Linus Walleijead76f32011-02-24 13:21:08 +0100756 struct pl011_sgbuf *sgbuf = use_buf_b ?
757 &uap->dmarx.sgbuf_b : &uap->dmarx.sgbuf_a;
Linus Walleijead76f32011-02-24 13:21:08 +0100758 int dma_count = 0;
759 u32 fifotaken = 0; /* only used for vdbg() */
760
Chanho Mincb06ff12013-03-27 18:38:11 +0900761 struct pl011_dmarx_data *dmarx = &uap->dmarx;
762 int dmataken = 0;
763
764 if (uap->dmarx.poll_rate) {
765 /* The data can be taken by polling */
766 dmataken = sgbuf->sg.length - dmarx->last_residue;
767 /* Recalculate the pending size */
768 if (pending >= dmataken)
769 pending -= dmataken;
770 }
771
772 /* Pick the remain data from the DMA */
Linus Walleijead76f32011-02-24 13:21:08 +0100773 if (pending) {
Linus Walleijead76f32011-02-24 13:21:08 +0100774
775 /*
776 * First take all chars in the DMA pipe, then look in the FIFO.
777 * Note that tty_insert_flip_buf() tries to take as many chars
778 * as it can.
779 */
Chanho Mincb06ff12013-03-27 18:38:11 +0900780 dma_count = tty_insert_flip_string(port, sgbuf->buf + dmataken,
781 pending);
Linus Walleijead76f32011-02-24 13:21:08 +0100782
783 uap->port.icount.rx += dma_count;
784 if (dma_count < pending)
785 dev_warn(uap->port.dev,
786 "couldn't insert all characters (TTY is full?)\n");
787 }
788
Chanho Mincb06ff12013-03-27 18:38:11 +0900789 /* Reset the last_residue for Rx DMA poll */
790 if (uap->dmarx.poll_rate)
791 dmarx->last_residue = sgbuf->sg.length;
792
Linus Walleijead76f32011-02-24 13:21:08 +0100793 /*
794 * Only continue with trying to read the FIFO if all DMA chars have
795 * been taken first.
796 */
797 if (dma_count == pending && readfifo) {
798 /* Clear any error flags */
799 writew(UART011_OEIS | UART011_BEIS | UART011_PEIS | UART011_FEIS,
800 uap->port.membase + UART011_ICR);
801
802 /*
803 * If we read all the DMA'd characters, and we had an
Linus Walleij29772c42011-02-24 13:21:36 +0100804 * incomplete buffer, that could be due to an rx error, or
805 * maybe we just timed out. Read any pending chars and check
806 * the error status.
807 *
808 * Error conditions will only occur in the FIFO, these will
809 * trigger an immediate interrupt and stop the DMA job, so we
810 * will always find the error in the FIFO, never in the DMA
811 * buffer.
Linus Walleijead76f32011-02-24 13:21:08 +0100812 */
Linus Walleij29772c42011-02-24 13:21:36 +0100813 fifotaken = pl011_fifo_to_tty(uap);
Linus Walleijead76f32011-02-24 13:21:08 +0100814 }
815
816 spin_unlock(&uap->port.lock);
817 dev_vdbg(uap->port.dev,
818 "Took %d chars from DMA buffer and %d chars from the FIFO\n",
819 dma_count, fifotaken);
Jiri Slaby2e124b42013-01-03 15:53:06 +0100820 tty_flip_buffer_push(port);
Linus Walleijead76f32011-02-24 13:21:08 +0100821 spin_lock(&uap->port.lock);
822}
823
824static void pl011_dma_rx_irq(struct uart_amba_port *uap)
825{
826 struct pl011_dmarx_data *dmarx = &uap->dmarx;
827 struct dma_chan *rxchan = dmarx->chan;
828 struct pl011_sgbuf *sgbuf = dmarx->use_buf_b ?
829 &dmarx->sgbuf_b : &dmarx->sgbuf_a;
830 size_t pending;
831 struct dma_tx_state state;
832 enum dma_status dmastat;
833
834 /*
835 * Pause the transfer so we can trust the current counter,
836 * do this before we pause the PL011 block, else we may
837 * overflow the FIFO.
838 */
839 if (dmaengine_pause(rxchan))
840 dev_err(uap->port.dev, "unable to pause DMA transfer\n");
841 dmastat = rxchan->device->device_tx_status(rxchan,
842 dmarx->cookie, &state);
843 if (dmastat != DMA_PAUSED)
844 dev_err(uap->port.dev, "unable to pause DMA transfer\n");
845
846 /* Disable RX DMA - incoming data will wait in the FIFO */
847 uap->dmacr &= ~UART011_RXDMAE;
848 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
849 uap->dmarx.running = false;
850
851 pending = sgbuf->sg.length - state.residue;
852 BUG_ON(pending > PL011_DMA_BUFFER_SIZE);
853 /* Then we terminate the transfer - we now know our residue */
854 dmaengine_terminate_all(rxchan);
855
856 /*
857 * This will take the chars we have so far and insert
858 * into the framework.
859 */
860 pl011_dma_rx_chars(uap, pending, dmarx->use_buf_b, true);
861
862 /* Switch buffer & re-trigger DMA job */
863 dmarx->use_buf_b = !dmarx->use_buf_b;
864 if (pl011_dma_rx_trigger_dma(uap)) {
865 dev_dbg(uap->port.dev, "could not retrigger RX DMA job "
866 "fall back to interrupt mode\n");
867 uap->im |= UART011_RXIM;
868 writew(uap->im, uap->port.membase + UART011_IMSC);
869 }
870}
871
872static void pl011_dma_rx_callback(void *data)
873{
874 struct uart_amba_port *uap = data;
875 struct pl011_dmarx_data *dmarx = &uap->dmarx;
Chanho Min6dc01aa2012-02-20 10:24:40 +0900876 struct dma_chan *rxchan = dmarx->chan;
Linus Walleijead76f32011-02-24 13:21:08 +0100877 bool lastbuf = dmarx->use_buf_b;
Chanho Min6dc01aa2012-02-20 10:24:40 +0900878 struct pl011_sgbuf *sgbuf = dmarx->use_buf_b ?
879 &dmarx->sgbuf_b : &dmarx->sgbuf_a;
880 size_t pending;
881 struct dma_tx_state state;
Linus Walleijead76f32011-02-24 13:21:08 +0100882 int ret;
883
884 /*
885 * This completion interrupt occurs typically when the
886 * RX buffer is totally stuffed but no timeout has yet
887 * occurred. When that happens, we just want the RX
888 * routine to flush out the secondary DMA buffer while
889 * we immediately trigger the next DMA job.
890 */
891 spin_lock_irq(&uap->port.lock);
Chanho Min6dc01aa2012-02-20 10:24:40 +0900892 /*
893 * Rx data can be taken by the UART interrupts during
894 * the DMA irq handler. So we check the residue here.
895 */
896 rxchan->device->device_tx_status(rxchan, dmarx->cookie, &state);
897 pending = sgbuf->sg.length - state.residue;
898 BUG_ON(pending > PL011_DMA_BUFFER_SIZE);
899 /* Then we terminate the transfer - we now know our residue */
900 dmaengine_terminate_all(rxchan);
901
Linus Walleijead76f32011-02-24 13:21:08 +0100902 uap->dmarx.running = false;
903 dmarx->use_buf_b = !lastbuf;
904 ret = pl011_dma_rx_trigger_dma(uap);
905
Chanho Min6dc01aa2012-02-20 10:24:40 +0900906 pl011_dma_rx_chars(uap, pending, lastbuf, false);
Linus Walleijead76f32011-02-24 13:21:08 +0100907 spin_unlock_irq(&uap->port.lock);
908 /*
909 * Do this check after we picked the DMA chars so we don't
910 * get some IRQ immediately from RX.
911 */
912 if (ret) {
913 dev_dbg(uap->port.dev, "could not retrigger RX DMA job "
914 "fall back to interrupt mode\n");
915 uap->im |= UART011_RXIM;
916 writew(uap->im, uap->port.membase + UART011_IMSC);
917 }
918}
919
920/*
921 * Stop accepting received characters, when we're shutting down or
922 * suspending this port.
923 * Locking: called with port lock held and IRQs disabled.
924 */
925static inline void pl011_dma_rx_stop(struct uart_amba_port *uap)
926{
927 /* FIXME. Just disable the DMA enable */
928 uap->dmacr &= ~UART011_RXDMAE;
929 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
930}
Russell King68b65f72010-12-22 17:24:39 +0000931
Chanho Mincb06ff12013-03-27 18:38:11 +0900932/*
933 * Timer handler for Rx DMA polling.
934 * Every polling, It checks the residue in the dma buffer and transfer
935 * data to the tty. Also, last_residue is updated for the next polling.
936 */
937static void pl011_dma_rx_poll(unsigned long args)
938{
939 struct uart_amba_port *uap = (struct uart_amba_port *)args;
940 struct tty_port *port = &uap->port.state->port;
941 struct pl011_dmarx_data *dmarx = &uap->dmarx;
942 struct dma_chan *rxchan = uap->dmarx.chan;
943 unsigned long flags = 0;
944 unsigned int dmataken = 0;
945 unsigned int size = 0;
946 struct pl011_sgbuf *sgbuf;
947 int dma_count;
948 struct dma_tx_state state;
949
950 sgbuf = dmarx->use_buf_b ? &uap->dmarx.sgbuf_b : &uap->dmarx.sgbuf_a;
951 rxchan->device->device_tx_status(rxchan, dmarx->cookie, &state);
952 if (likely(state.residue < dmarx->last_residue)) {
953 dmataken = sgbuf->sg.length - dmarx->last_residue;
954 size = dmarx->last_residue - state.residue;
955 dma_count = tty_insert_flip_string(port, sgbuf->buf + dmataken,
956 size);
957 if (dma_count == size)
958 dmarx->last_residue = state.residue;
959 dmarx->last_jiffies = jiffies;
960 }
961 tty_flip_buffer_push(port);
962
963 /*
964 * If no data is received in poll_timeout, the driver will fall back
965 * to interrupt mode. We will retrigger DMA at the first interrupt.
966 */
967 if (jiffies_to_msecs(jiffies - dmarx->last_jiffies)
968 > uap->dmarx.poll_timeout) {
969
970 spin_lock_irqsave(&uap->port.lock, flags);
971 pl011_dma_rx_stop(uap);
Guennadi Liakhovetskic25a1ad2013-12-10 14:54:47 +0100972 uap->im |= UART011_RXIM;
973 writew(uap->im, uap->port.membase + UART011_IMSC);
Chanho Mincb06ff12013-03-27 18:38:11 +0900974 spin_unlock_irqrestore(&uap->port.lock, flags);
975
976 uap->dmarx.running = false;
977 dmaengine_terminate_all(rxchan);
978 del_timer(&uap->dmarx.timer);
979 } else {
980 mod_timer(&uap->dmarx.timer,
981 jiffies + msecs_to_jiffies(uap->dmarx.poll_rate));
982 }
983}
984
Russell King68b65f72010-12-22 17:24:39 +0000985static void pl011_dma_startup(struct uart_amba_port *uap)
986{
Linus Walleijead76f32011-02-24 13:21:08 +0100987 int ret;
988
Jorge Ramirez-Ortiz1c9be312015-03-06 13:05:40 -0500989 if (!uap->dma_probed)
990 pl011_dma_probe(uap);
991
Russell King68b65f72010-12-22 17:24:39 +0000992 if (!uap->dmatx.chan)
993 return;
994
Andrew Jackson4c0be452014-11-07 14:14:35 +0000995 uap->dmatx.buf = kmalloc(PL011_DMA_BUFFER_SIZE, GFP_KERNEL | __GFP_DMA);
Russell King68b65f72010-12-22 17:24:39 +0000996 if (!uap->dmatx.buf) {
997 dev_err(uap->port.dev, "no memory for DMA TX buffer\n");
998 uap->port.fifosize = uap->fifosize;
999 return;
1000 }
1001
1002 sg_init_one(&uap->dmatx.sg, uap->dmatx.buf, PL011_DMA_BUFFER_SIZE);
1003
1004 /* The DMA buffer is now the FIFO the TTY subsystem can use */
1005 uap->port.fifosize = PL011_DMA_BUFFER_SIZE;
Linus Walleijead76f32011-02-24 13:21:08 +01001006 uap->using_tx_dma = true;
Russell King68b65f72010-12-22 17:24:39 +00001007
Linus Walleijead76f32011-02-24 13:21:08 +01001008 if (!uap->dmarx.chan)
1009 goto skip_rx;
1010
1011 /* Allocate and map DMA RX buffers */
1012 ret = pl011_sgbuf_init(uap->dmarx.chan, &uap->dmarx.sgbuf_a,
1013 DMA_FROM_DEVICE);
1014 if (ret) {
1015 dev_err(uap->port.dev, "failed to init DMA %s: %d\n",
1016 "RX buffer A", ret);
1017 goto skip_rx;
1018 }
1019
1020 ret = pl011_sgbuf_init(uap->dmarx.chan, &uap->dmarx.sgbuf_b,
1021 DMA_FROM_DEVICE);
1022 if (ret) {
1023 dev_err(uap->port.dev, "failed to init DMA %s: %d\n",
1024 "RX buffer B", ret);
1025 pl011_sgbuf_free(uap->dmarx.chan, &uap->dmarx.sgbuf_a,
1026 DMA_FROM_DEVICE);
1027 goto skip_rx;
1028 }
1029
1030 uap->using_rx_dma = true;
1031
1032skip_rx:
Russell King68b65f72010-12-22 17:24:39 +00001033 /* Turn on DMA error (RX/TX will be enabled on demand) */
1034 uap->dmacr |= UART011_DMAONERR;
1035 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
Russell King38d62432010-12-22 17:59:16 +00001036
1037 /*
1038 * ST Micro variants has some specific dma burst threshold
1039 * compensation. Set this to 16 bytes, so burst will only
1040 * be issued above/below 16 bytes.
1041 */
1042 if (uap->vendor->dma_threshold)
1043 writew(ST_UART011_DMAWM_RX_16 | ST_UART011_DMAWM_TX_16,
1044 uap->port.membase + ST_UART011_DMAWM);
Linus Walleijead76f32011-02-24 13:21:08 +01001045
1046 if (uap->using_rx_dma) {
1047 if (pl011_dma_rx_trigger_dma(uap))
1048 dev_dbg(uap->port.dev, "could not trigger initial "
1049 "RX DMA job, fall back to interrupt mode\n");
Chanho Mincb06ff12013-03-27 18:38:11 +09001050 if (uap->dmarx.poll_rate) {
1051 init_timer(&(uap->dmarx.timer));
1052 uap->dmarx.timer.function = pl011_dma_rx_poll;
1053 uap->dmarx.timer.data = (unsigned long)uap;
1054 mod_timer(&uap->dmarx.timer,
1055 jiffies +
1056 msecs_to_jiffies(uap->dmarx.poll_rate));
1057 uap->dmarx.last_residue = PL011_DMA_BUFFER_SIZE;
1058 uap->dmarx.last_jiffies = jiffies;
1059 }
Linus Walleijead76f32011-02-24 13:21:08 +01001060 }
Russell King68b65f72010-12-22 17:24:39 +00001061}
1062
1063static void pl011_dma_shutdown(struct uart_amba_port *uap)
1064{
Linus Walleijead76f32011-02-24 13:21:08 +01001065 if (!(uap->using_tx_dma || uap->using_rx_dma))
Russell King68b65f72010-12-22 17:24:39 +00001066 return;
1067
1068 /* Disable RX and TX DMA */
1069 while (readw(uap->port.membase + UART01x_FR) & UART01x_FR_BUSY)
1070 barrier();
1071
1072 spin_lock_irq(&uap->port.lock);
1073 uap->dmacr &= ~(UART011_DMAONERR | UART011_RXDMAE | UART011_TXDMAE);
1074 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
1075 spin_unlock_irq(&uap->port.lock);
1076
Linus Walleijead76f32011-02-24 13:21:08 +01001077 if (uap->using_tx_dma) {
1078 /* In theory, this should already be done by pl011_dma_flush_buffer */
1079 dmaengine_terminate_all(uap->dmatx.chan);
1080 if (uap->dmatx.queued) {
1081 dma_unmap_sg(uap->dmatx.chan->device->dev, &uap->dmatx.sg, 1,
1082 DMA_TO_DEVICE);
1083 uap->dmatx.queued = false;
1084 }
1085
1086 kfree(uap->dmatx.buf);
1087 uap->using_tx_dma = false;
Russell King68b65f72010-12-22 17:24:39 +00001088 }
1089
Linus Walleijead76f32011-02-24 13:21:08 +01001090 if (uap->using_rx_dma) {
1091 dmaengine_terminate_all(uap->dmarx.chan);
1092 /* Clean up the RX DMA */
1093 pl011_sgbuf_free(uap->dmarx.chan, &uap->dmarx.sgbuf_a, DMA_FROM_DEVICE);
1094 pl011_sgbuf_free(uap->dmarx.chan, &uap->dmarx.sgbuf_b, DMA_FROM_DEVICE);
Chanho Mincb06ff12013-03-27 18:38:11 +09001095 if (uap->dmarx.poll_rate)
1096 del_timer_sync(&uap->dmarx.timer);
Linus Walleijead76f32011-02-24 13:21:08 +01001097 uap->using_rx_dma = false;
1098 }
Russell King68b65f72010-12-22 17:24:39 +00001099}
1100
Linus Walleijead76f32011-02-24 13:21:08 +01001101static inline bool pl011_dma_rx_available(struct uart_amba_port *uap)
1102{
1103 return uap->using_rx_dma;
1104}
1105
1106static inline bool pl011_dma_rx_running(struct uart_amba_port *uap)
1107{
1108 return uap->using_rx_dma && uap->dmarx.running;
1109}
1110
Russell King68b65f72010-12-22 17:24:39 +00001111#else
1112/* Blank functions if the DMA engine is not available */
Jorge Ramirez-Ortiz1c9be312015-03-06 13:05:40 -05001113static inline void pl011_dma_probe(struct uart_amba_port *uap)
Russell King68b65f72010-12-22 17:24:39 +00001114{
1115}
1116
1117static inline void pl011_dma_remove(struct uart_amba_port *uap)
1118{
1119}
1120
1121static inline void pl011_dma_startup(struct uart_amba_port *uap)
1122{
1123}
1124
1125static inline void pl011_dma_shutdown(struct uart_amba_port *uap)
1126{
1127}
1128
1129static inline bool pl011_dma_tx_irq(struct uart_amba_port *uap)
1130{
1131 return false;
1132}
1133
1134static inline void pl011_dma_tx_stop(struct uart_amba_port *uap)
1135{
1136}
1137
1138static inline bool pl011_dma_tx_start(struct uart_amba_port *uap)
1139{
1140 return false;
1141}
1142
Linus Walleijead76f32011-02-24 13:21:08 +01001143static inline void pl011_dma_rx_irq(struct uart_amba_port *uap)
1144{
1145}
1146
1147static inline void pl011_dma_rx_stop(struct uart_amba_port *uap)
1148{
1149}
1150
1151static inline int pl011_dma_rx_trigger_dma(struct uart_amba_port *uap)
1152{
1153 return -EIO;
1154}
1155
1156static inline bool pl011_dma_rx_available(struct uart_amba_port *uap)
1157{
1158 return false;
1159}
1160
1161static inline bool pl011_dma_rx_running(struct uart_amba_port *uap)
1162{
1163 return false;
1164}
1165
Russell King68b65f72010-12-22 17:24:39 +00001166#define pl011_dma_flush_buffer NULL
1167#endif
1168
Russell Kingb129a8c2005-08-31 10:12:14 +01001169static void pl011_stop_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001171 struct uart_amba_port *uap =
1172 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173
1174 uap->im &= ~UART011_TXIM;
1175 writew(uap->im, uap->port.membase + UART011_IMSC);
Russell King68b65f72010-12-22 17:24:39 +00001176 pl011_dma_tx_stop(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177}
1178
Dave Martin1e84d222015-04-27 16:49:05 +01001179static void pl011_tx_chars(struct uart_amba_port *uap, bool from_irq);
Dave Martin734745c2015-03-04 12:27:33 +00001180
1181/* Start TX with programmed I/O only (no DMA) */
1182static void pl011_start_tx_pio(struct uart_amba_port *uap)
1183{
1184 uap->im |= UART011_TXIM;
1185 writew(uap->im, uap->port.membase + UART011_IMSC);
Dave Martin1e84d222015-04-27 16:49:05 +01001186 pl011_tx_chars(uap, false);
Dave Martin734745c2015-03-04 12:27:33 +00001187}
1188
Russell Kingb129a8c2005-08-31 10:12:14 +01001189static void pl011_start_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001191 struct uart_amba_port *uap =
1192 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193
Dave Martin734745c2015-03-04 12:27:33 +00001194 if (!pl011_dma_tx_start(uap))
1195 pl011_start_tx_pio(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196}
1197
1198static void pl011_stop_rx(struct uart_port *port)
1199{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001200 struct uart_amba_port *uap =
1201 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202
1203 uap->im &= ~(UART011_RXIM|UART011_RTIM|UART011_FEIM|
1204 UART011_PEIM|UART011_BEIM|UART011_OEIM);
1205 writew(uap->im, uap->port.membase + UART011_IMSC);
Linus Walleijead76f32011-02-24 13:21:08 +01001206
1207 pl011_dma_rx_stop(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208}
1209
1210static void pl011_enable_ms(struct uart_port *port)
1211{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001212 struct uart_amba_port *uap =
1213 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
1215 uap->im |= UART011_RIMIM|UART011_CTSMIM|UART011_DCDMIM|UART011_DSRMIM;
1216 writew(uap->im, uap->port.membase + UART011_IMSC);
1217}
1218
David Howells7d12e782006-10-05 14:55:46 +01001219static void pl011_rx_chars(struct uart_amba_port *uap)
Fabio Estevamb83286b2013-08-09 17:58:51 -03001220__releases(&uap->port.lock)
1221__acquires(&uap->port.lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222{
Linus Walleij29772c42011-02-24 13:21:36 +01001223 pl011_fifo_to_tty(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224
Thomas Gleixner2389b272007-05-29 21:53:50 +01001225 spin_unlock(&uap->port.lock);
Jiri Slaby2e124b42013-01-03 15:53:06 +01001226 tty_flip_buffer_push(&uap->port.state->port);
Linus Walleijead76f32011-02-24 13:21:08 +01001227 /*
1228 * If we were temporarily out of DMA mode for a while,
1229 * attempt to switch back to DMA mode again.
1230 */
1231 if (pl011_dma_rx_available(uap)) {
1232 if (pl011_dma_rx_trigger_dma(uap)) {
1233 dev_dbg(uap->port.dev, "could not trigger RX DMA job "
1234 "fall back to interrupt mode again\n");
1235 uap->im |= UART011_RXIM;
Guennadi Liakhovetski30ae5852013-12-10 14:54:42 +01001236 writew(uap->im, uap->port.membase + UART011_IMSC);
Chanho Mincb06ff12013-03-27 18:38:11 +09001237 } else {
Chanho Min89fa28d2013-04-03 11:10:37 +09001238#ifdef CONFIG_DMA_ENGINE
Chanho Mincb06ff12013-03-27 18:38:11 +09001239 /* Start Rx DMA poll */
1240 if (uap->dmarx.poll_rate) {
1241 uap->dmarx.last_jiffies = jiffies;
1242 uap->dmarx.last_residue = PL011_DMA_BUFFER_SIZE;
1243 mod_timer(&uap->dmarx.timer,
1244 jiffies +
1245 msecs_to_jiffies(uap->dmarx.poll_rate));
1246 }
Chanho Min89fa28d2013-04-03 11:10:37 +09001247#endif
Chanho Mincb06ff12013-03-27 18:38:11 +09001248 }
Linus Walleijead76f32011-02-24 13:21:08 +01001249 }
Thomas Gleixner2389b272007-05-29 21:53:50 +01001250 spin_lock(&uap->port.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251}
1252
Dave Martin1e84d222015-04-27 16:49:05 +01001253static bool pl011_tx_char(struct uart_amba_port *uap, unsigned char c,
1254 bool from_irq)
Dave Martin734745c2015-03-04 12:27:33 +00001255{
Dave Martin1e84d222015-04-27 16:49:05 +01001256 if (unlikely(!from_irq) &&
1257 readw(uap->port.membase + UART01x_FR) & UART01x_FR_TXFF)
1258 return false; /* unable to transmit character */
1259
Dave Martin734745c2015-03-04 12:27:33 +00001260 writew(c, uap->port.membase + UART01x_DR);
1261 uap->port.icount.tx++;
1262
Dave Martin1e84d222015-04-27 16:49:05 +01001263 return true;
Dave Martin734745c2015-03-04 12:27:33 +00001264}
1265
Dave Martin1e84d222015-04-27 16:49:05 +01001266static void pl011_tx_chars(struct uart_amba_port *uap, bool from_irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267{
Alan Coxebd2c8f2009-09-19 13:13:28 -07001268 struct circ_buf *xmit = &uap->port.state->xmit;
Dave Martin1e84d222015-04-27 16:49:05 +01001269 int count = uap->fifosize >> 1;
Dave Martin734745c2015-03-04 12:27:33 +00001270
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 if (uap->port.x_char) {
Dave Martin1e84d222015-04-27 16:49:05 +01001272 if (!pl011_tx_char(uap, uap->port.x_char, from_irq))
1273 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 uap->port.x_char = 0;
Dave Martin734745c2015-03-04 12:27:33 +00001275 --count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 }
1277 if (uart_circ_empty(xmit) || uart_tx_stopped(&uap->port)) {
Russell Kingb129a8c2005-08-31 10:12:14 +01001278 pl011_stop_tx(&uap->port);
Dave Martin1e84d222015-04-27 16:49:05 +01001279 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 }
1281
Russell King68b65f72010-12-22 17:24:39 +00001282 /* If we are using DMA mode, try to send some characters. */
1283 if (pl011_dma_tx_irq(uap))
Dave Martin1e84d222015-04-27 16:49:05 +01001284 return;
Russell King68b65f72010-12-22 17:24:39 +00001285
Dave Martin1e84d222015-04-27 16:49:05 +01001286 do {
1287 if (likely(from_irq) && count-- == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 break;
Dave Martin1e84d222015-04-27 16:49:05 +01001289
1290 if (!pl011_tx_char(uap, xmit->buf[xmit->tail], from_irq))
1291 break;
1292
1293 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
1294 } while (!uart_circ_empty(xmit));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295
1296 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
1297 uart_write_wakeup(&uap->port);
1298
Dave Martin1e84d222015-04-27 16:49:05 +01001299 if (uart_circ_empty(xmit))
Russell Kingb129a8c2005-08-31 10:12:14 +01001300 pl011_stop_tx(&uap->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301}
1302
1303static void pl011_modem_status(struct uart_amba_port *uap)
1304{
1305 unsigned int status, delta;
1306
1307 status = readw(uap->port.membase + UART01x_FR) & UART01x_FR_MODEM_ANY;
1308
1309 delta = status ^ uap->old_status;
1310 uap->old_status = status;
1311
1312 if (!delta)
1313 return;
1314
1315 if (delta & UART01x_FR_DCD)
1316 uart_handle_dcd_change(&uap->port, status & UART01x_FR_DCD);
1317
1318 if (delta & UART01x_FR_DSR)
1319 uap->port.icount.dsr++;
1320
1321 if (delta & UART01x_FR_CTS)
1322 uart_handle_cts_change(&uap->port, status & UART01x_FR_CTS);
1323
Alan Coxbdc04e32009-09-19 13:13:31 -07001324 wake_up_interruptible(&uap->port.state->port.delta_msr_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325}
1326
Andre Przywara9c4ef4b2015-05-21 17:26:20 +01001327static void check_apply_cts_event_workaround(struct uart_amba_port *uap)
1328{
1329 unsigned int dummy_read;
1330
1331 if (!uap->vendor->cts_event_workaround)
1332 return;
1333
1334 /* workaround to make sure that all bits are unlocked.. */
1335 writew(0x00, uap->port.membase + UART011_ICR);
1336
1337 /*
1338 * WA: introduce 26ns(1 uart clk) delay before W1C;
1339 * single apb access will incur 2 pclk(133.12Mhz) delay,
1340 * so add 2 dummy reads
1341 */
1342 dummy_read = readw(uap->port.membase + UART011_ICR);
1343 dummy_read = readw(uap->port.membase + UART011_ICR);
1344}
1345
David Howells7d12e782006-10-05 14:55:46 +01001346static irqreturn_t pl011_int(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347{
1348 struct uart_amba_port *uap = dev_id;
Russell King963cc982010-12-22 17:16:09 +00001349 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 unsigned int status, pass_counter = AMBA_ISR_PASS_LIMIT;
Andre Przywara075167e2015-05-21 17:26:19 +01001351 u16 imsc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 int handled = 0;
1353
Russell King963cc982010-12-22 17:16:09 +00001354 spin_lock_irqsave(&uap->port.lock, flags);
Andre Przywara075167e2015-05-21 17:26:19 +01001355 imsc = readw(uap->port.membase + UART011_IMSC);
1356 status = readw(uap->port.membase + UART011_RIS) & imsc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 if (status) {
1358 do {
Andre Przywara9c4ef4b2015-05-21 17:26:20 +01001359 check_apply_cts_event_workaround(uap);
Rajanikanth H.V4fd06902012-03-26 11:17:02 +02001360
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 writew(status & ~(UART011_TXIS|UART011_RTIS|
1362 UART011_RXIS),
1363 uap->port.membase + UART011_ICR);
1364
Linus Walleijead76f32011-02-24 13:21:08 +01001365 if (status & (UART011_RTIS|UART011_RXIS)) {
1366 if (pl011_dma_rx_running(uap))
1367 pl011_dma_rx_irq(uap);
1368 else
1369 pl011_rx_chars(uap);
1370 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 if (status & (UART011_DSRMIS|UART011_DCDMIS|
1372 UART011_CTSMIS|UART011_RIMIS))
1373 pl011_modem_status(uap);
Dave Martin1e84d222015-04-27 16:49:05 +01001374 if (status & UART011_TXIS)
1375 pl011_tx_chars(uap, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376
Rajanikanth H.V4fd06902012-03-26 11:17:02 +02001377 if (pass_counter-- == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 break;
1379
Andre Przywara075167e2015-05-21 17:26:19 +01001380 status = readw(uap->port.membase + UART011_RIS) & imsc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 } while (status != 0);
1382 handled = 1;
1383 }
1384
Russell King963cc982010-12-22 17:16:09 +00001385 spin_unlock_irqrestore(&uap->port.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386
1387 return IRQ_RETVAL(handled);
1388}
1389
Linus Walleije643f872012-06-17 15:44:19 +02001390static unsigned int pl011_tx_empty(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001392 struct uart_amba_port *uap =
1393 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 unsigned int status = readw(uap->port.membase + UART01x_FR);
1395 return status & (UART01x_FR_BUSY|UART01x_FR_TXFF) ? 0 : TIOCSER_TEMT;
1396}
1397
Linus Walleije643f872012-06-17 15:44:19 +02001398static unsigned int pl011_get_mctrl(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001400 struct uart_amba_port *uap =
1401 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 unsigned int result = 0;
1403 unsigned int status = readw(uap->port.membase + UART01x_FR);
1404
Jiri Slaby5159f402007-10-18 23:40:31 -07001405#define TIOCMBIT(uartbit, tiocmbit) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 if (status & uartbit) \
1407 result |= tiocmbit
1408
Jiri Slaby5159f402007-10-18 23:40:31 -07001409 TIOCMBIT(UART01x_FR_DCD, TIOCM_CAR);
1410 TIOCMBIT(UART01x_FR_DSR, TIOCM_DSR);
1411 TIOCMBIT(UART01x_FR_CTS, TIOCM_CTS);
1412 TIOCMBIT(UART011_FR_RI, TIOCM_RNG);
1413#undef TIOCMBIT
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 return result;
1415}
1416
1417static void pl011_set_mctrl(struct uart_port *port, unsigned int mctrl)
1418{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001419 struct uart_amba_port *uap =
1420 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 unsigned int cr;
1422
1423 cr = readw(uap->port.membase + UART011_CR);
1424
Jiri Slaby5159f402007-10-18 23:40:31 -07001425#define TIOCMBIT(tiocmbit, uartbit) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 if (mctrl & tiocmbit) \
1427 cr |= uartbit; \
1428 else \
1429 cr &= ~uartbit
1430
Jiri Slaby5159f402007-10-18 23:40:31 -07001431 TIOCMBIT(TIOCM_RTS, UART011_CR_RTS);
1432 TIOCMBIT(TIOCM_DTR, UART011_CR_DTR);
1433 TIOCMBIT(TIOCM_OUT1, UART011_CR_OUT1);
1434 TIOCMBIT(TIOCM_OUT2, UART011_CR_OUT2);
1435 TIOCMBIT(TIOCM_LOOP, UART011_CR_LBE);
Rabin Vincent3b438162010-02-12 06:43:11 +01001436
1437 if (uap->autorts) {
1438 /* We need to disable auto-RTS if we want to turn RTS off */
1439 TIOCMBIT(TIOCM_RTS, UART011_CR_RTSEN);
1440 }
Jiri Slaby5159f402007-10-18 23:40:31 -07001441#undef TIOCMBIT
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442
1443 writew(cr, uap->port.membase + UART011_CR);
1444}
1445
1446static void pl011_break_ctl(struct uart_port *port, int break_state)
1447{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001448 struct uart_amba_port *uap =
1449 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 unsigned long flags;
1451 unsigned int lcr_h;
1452
1453 spin_lock_irqsave(&uap->port.lock, flags);
Linus Walleijec489aa2010-06-02 08:13:52 +01001454 lcr_h = readw(uap->port.membase + uap->lcrh_tx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 if (break_state == -1)
1456 lcr_h |= UART01x_LCRH_BRK;
1457 else
1458 lcr_h &= ~UART01x_LCRH_BRK;
Linus Walleijec489aa2010-06-02 08:13:52 +01001459 writew(lcr_h, uap->port.membase + uap->lcrh_tx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 spin_unlock_irqrestore(&uap->port.lock, flags);
1461}
1462
Jason Wessel84b5ae12008-02-20 13:33:39 -06001463#ifdef CONFIG_CONSOLE_POLL
Anton Vorontsov5c8124a2012-09-24 14:27:55 -07001464
1465static void pl011_quiesce_irqs(struct uart_port *port)
1466{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001467 struct uart_amba_port *uap =
1468 container_of(port, struct uart_amba_port, port);
Anton Vorontsov5c8124a2012-09-24 14:27:55 -07001469 unsigned char __iomem *regs = uap->port.membase;
1470
1471 writew(readw(regs + UART011_MIS), regs + UART011_ICR);
1472 /*
1473 * There is no way to clear TXIM as this is "ready to transmit IRQ", so
1474 * we simply mask it. start_tx() will unmask it.
1475 *
1476 * Note we can race with start_tx(), and if the race happens, the
1477 * polling user might get another interrupt just after we clear it.
1478 * But it should be OK and can happen even w/o the race, e.g.
1479 * controller immediately got some new data and raised the IRQ.
1480 *
1481 * And whoever uses polling routines assumes that it manages the device
1482 * (including tx queue), so we're also fine with start_tx()'s caller
1483 * side.
1484 */
1485 writew(readw(regs + UART011_IMSC) & ~UART011_TXIM, regs + UART011_IMSC);
1486}
1487
Linus Walleije643f872012-06-17 15:44:19 +02001488static int pl011_get_poll_char(struct uart_port *port)
Jason Wessel84b5ae12008-02-20 13:33:39 -06001489{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001490 struct uart_amba_port *uap =
1491 container_of(port, struct uart_amba_port, port);
Jason Wessel84b5ae12008-02-20 13:33:39 -06001492 unsigned int status;
1493
Anton Vorontsov5c8124a2012-09-24 14:27:55 -07001494 /*
1495 * The caller might need IRQs lowered, e.g. if used with KDB NMI
1496 * debugger.
1497 */
1498 pl011_quiesce_irqs(port);
1499
Jason Wesself5316b42010-05-20 21:04:22 -05001500 status = readw(uap->port.membase + UART01x_FR);
1501 if (status & UART01x_FR_RXFE)
1502 return NO_POLL_CHAR;
Jason Wessel84b5ae12008-02-20 13:33:39 -06001503
1504 return readw(uap->port.membase + UART01x_DR);
1505}
1506
Linus Walleije643f872012-06-17 15:44:19 +02001507static void pl011_put_poll_char(struct uart_port *port,
Jason Wessel84b5ae12008-02-20 13:33:39 -06001508 unsigned char ch)
1509{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001510 struct uart_amba_port *uap =
1511 container_of(port, struct uart_amba_port, port);
Jason Wessel84b5ae12008-02-20 13:33:39 -06001512
1513 while (readw(uap->port.membase + UART01x_FR) & UART01x_FR_TXFF)
1514 barrier();
1515
1516 writew(ch, uap->port.membase + UART01x_DR);
1517}
1518
1519#endif /* CONFIG_CONSOLE_POLL */
1520
Anton Vorontsovb3564c22012-09-24 14:27:54 -07001521static int pl011_hwinit(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001523 struct uart_amba_port *uap =
1524 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 int retval;
1526
Linus Walleij78d80c52012-05-23 21:18:46 +02001527 /* Optionaly enable pins to be muxed in and configured */
Linus Walleij2b996fc2013-06-05 15:36:42 +02001528 pinctrl_pm_select_default_state(port->dev);
Linus Walleij78d80c52012-05-23 21:18:46 +02001529
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 /*
1531 * Try to enable the clock producer.
1532 */
Julia Lawall1c4c4392012-08-26 18:01:01 +02001533 retval = clk_prepare_enable(uap->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 if (retval)
Tushar Behera7f6d9422014-06-26 15:35:35 +05301535 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536
1537 uap->port.uartclk = clk_get_rate(uap->clk);
1538
Linus Walleij9b96fba2012-03-13 13:27:23 +01001539 /* Clear pending error and receive interrupts */
1540 writew(UART011_OEIS | UART011_BEIS | UART011_PEIS | UART011_FEIS |
1541 UART011_RTIS | UART011_RXIS, uap->port.membase + UART011_ICR);
1542
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 /*
Anton Vorontsovb3564c22012-09-24 14:27:54 -07001544 * Save interrupts enable mask, and enable RX interrupts in case if
1545 * the interrupt is used for NMI entry.
1546 */
1547 uap->im = readw(uap->port.membase + UART011_IMSC);
1548 writew(UART011_RTIM | UART011_RXIM, uap->port.membase + UART011_IMSC);
1549
Jingoo Han574de552013-07-30 17:06:57 +09001550 if (dev_get_platdata(uap->port.dev)) {
Anton Vorontsovb3564c22012-09-24 14:27:54 -07001551 struct amba_pl011_data *plat;
1552
Jingoo Han574de552013-07-30 17:06:57 +09001553 plat = dev_get_platdata(uap->port.dev);
Anton Vorontsovb3564c22012-09-24 14:27:54 -07001554 if (plat->init)
1555 plat->init();
1556 }
1557 return 0;
Anton Vorontsovb3564c22012-09-24 14:27:54 -07001558}
1559
Jon Medhurstb60f2f62013-12-10 10:18:59 +00001560static void pl011_write_lcr_h(struct uart_amba_port *uap, unsigned int lcr_h)
1561{
1562 writew(lcr_h, uap->port.membase + uap->lcrh_rx);
1563 if (uap->lcrh_rx != uap->lcrh_tx) {
1564 int i;
1565 /*
1566 * Wait 10 PCLKs before writing LCRH_TX register,
1567 * to get this delay write read only register 10 times
1568 */
1569 for (i = 0; i < 10; ++i)
1570 writew(0xff, uap->port.membase + UART011_MIS);
1571 writew(lcr_h, uap->port.membase + uap->lcrh_tx);
1572 }
1573}
1574
Andre Przywara867b8e82015-05-21 17:26:15 +01001575static int pl011_allocate_irq(struct uart_amba_port *uap)
1576{
1577 writew(uap->im, uap->port.membase + UART011_IMSC);
1578
1579 return request_irq(uap->port.irq, pl011_int, 0, "uart-pl011", uap);
1580}
1581
1582/*
1583 * Enable interrupts, only timeouts when using DMA
1584 * if initial RX DMA job failed, start in interrupt mode
1585 * as well.
1586 */
1587static void pl011_enable_interrupts(struct uart_amba_port *uap)
1588{
1589 spin_lock_irq(&uap->port.lock);
1590
1591 /* Clear out any spuriously appearing RX interrupts */
1592 writew(UART011_RTIS | UART011_RXIS,
1593 uap->port.membase + UART011_ICR);
1594 uap->im = UART011_RTIM;
1595 if (!pl011_dma_rx_running(uap))
1596 uap->im |= UART011_RXIM;
1597 writew(uap->im, uap->port.membase + UART011_IMSC);
1598 spin_unlock_irq(&uap->port.lock);
1599}
1600
Anton Vorontsovb3564c22012-09-24 14:27:54 -07001601static int pl011_startup(struct uart_port *port)
1602{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001603 struct uart_amba_port *uap =
1604 container_of(port, struct uart_amba_port, port);
Dave Martin734745c2015-03-04 12:27:33 +00001605 unsigned int cr;
Anton Vorontsovb3564c22012-09-24 14:27:54 -07001606 int retval;
1607
1608 retval = pl011_hwinit(port);
1609 if (retval)
1610 goto clk_dis;
1611
Andre Przywara867b8e82015-05-21 17:26:15 +01001612 retval = pl011_allocate_irq(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 if (retval)
1614 goto clk_dis;
1615
Russell Kingc19f12b2010-12-22 17:48:26 +00001616 writew(uap->vendor->ifls, uap->port.membase + UART011_IFLS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617
Jon Medhurstfe433902013-12-10 10:18:58 +00001618 spin_lock_irq(&uap->port.lock);
1619
Shreshtha Kumar Sahud8d8ffa2012-01-18 15:53:59 +05301620 /* restore RTS and DTR */
1621 cr = uap->old_cr & (UART011_CR_RTS | UART011_CR_DTR);
1622 cr |= UART01x_CR_UARTEN | UART011_CR_RXE | UART011_CR_TXE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 writew(cr, uap->port.membase + UART011_CR);
1624
Jon Medhurstfe433902013-12-10 10:18:58 +00001625 spin_unlock_irq(&uap->port.lock);
1626
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 /*
1628 * initialise the old status of the modem signals
1629 */
1630 uap->old_status = readw(uap->port.membase + UART01x_FR) & UART01x_FR_MODEM_ANY;
1631
Russell King68b65f72010-12-22 17:24:39 +00001632 /* Startup DMA */
1633 pl011_dma_startup(uap);
1634
Andre Przywara867b8e82015-05-21 17:26:15 +01001635 pl011_enable_interrupts(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636
1637 return 0;
1638
1639 clk_dis:
Julia Lawall1c4c4392012-08-26 18:01:01 +02001640 clk_disable_unprepare(uap->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 return retval;
1642}
1643
Linus Walleijec489aa2010-06-02 08:13:52 +01001644static void pl011_shutdown_channel(struct uart_amba_port *uap,
1645 unsigned int lcrh)
1646{
1647 unsigned long val;
1648
1649 val = readw(uap->port.membase + lcrh);
1650 val &= ~(UART01x_LCRH_BRK | UART01x_LCRH_FEN);
1651 writew(val, uap->port.membase + lcrh);
1652}
1653
Andre Przywara95166a32015-05-21 17:26:16 +01001654/*
1655 * disable the port. It should not disable RTS and DTR.
1656 * Also RTS and DTR state should be preserved to restore
1657 * it during startup().
1658 */
1659static void pl011_disable_uart(struct uart_amba_port *uap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660{
Shreshtha Kumar Sahud8d8ffa2012-01-18 15:53:59 +05301661 unsigned int cr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662
Rabin Vincent3b438162010-02-12 06:43:11 +01001663 uap->autorts = false;
Jon Medhurstfe433902013-12-10 10:18:58 +00001664 spin_lock_irq(&uap->port.lock);
Shreshtha Kumar Sahud8d8ffa2012-01-18 15:53:59 +05301665 cr = readw(uap->port.membase + UART011_CR);
1666 uap->old_cr = cr;
1667 cr &= UART011_CR_RTS | UART011_CR_DTR;
1668 cr |= UART01x_CR_UARTEN | UART011_CR_TXE;
1669 writew(cr, uap->port.membase + UART011_CR);
Jon Medhurstfe433902013-12-10 10:18:58 +00001670 spin_unlock_irq(&uap->port.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671
1672 /*
1673 * disable break condition and fifos
1674 */
Linus Walleijec489aa2010-06-02 08:13:52 +01001675 pl011_shutdown_channel(uap, uap->lcrh_rx);
1676 if (uap->lcrh_rx != uap->lcrh_tx)
1677 pl011_shutdown_channel(uap, uap->lcrh_tx);
Andre Przywara95166a32015-05-21 17:26:16 +01001678}
1679
1680static void pl011_disable_interrupts(struct uart_amba_port *uap)
1681{
1682 spin_lock_irq(&uap->port.lock);
1683
1684 /* mask all interrupts and clear all pending ones */
1685 uap->im = 0;
1686 writew(uap->im, uap->port.membase + UART011_IMSC);
1687 writew(0xffff, uap->port.membase + UART011_ICR);
1688
1689 spin_unlock_irq(&uap->port.lock);
1690}
1691
1692static void pl011_shutdown(struct uart_port *port)
1693{
1694 struct uart_amba_port *uap =
1695 container_of(port, struct uart_amba_port, port);
1696
1697 pl011_disable_interrupts(uap);
1698
1699 pl011_dma_shutdown(uap);
1700
1701 free_irq(uap->port.irq, uap);
1702
1703 pl011_disable_uart(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704
1705 /*
1706 * Shut down the clock producer
1707 */
Julia Lawall1c4c4392012-08-26 18:01:01 +02001708 clk_disable_unprepare(uap->clk);
Linus Walleij78d80c52012-05-23 21:18:46 +02001709 /* Optionally let pins go into sleep states */
Linus Walleij2b996fc2013-06-05 15:36:42 +02001710 pinctrl_pm_select_sleep_state(port->dev);
Shreshtha Kumar Sahuc16d51a2011-06-13 10:11:33 +02001711
Jingoo Han574de552013-07-30 17:06:57 +09001712 if (dev_get_platdata(uap->port.dev)) {
Shreshtha Kumar Sahuc16d51a2011-06-13 10:11:33 +02001713 struct amba_pl011_data *plat;
1714
Jingoo Han574de552013-07-30 17:06:57 +09001715 plat = dev_get_platdata(uap->port.dev);
Shreshtha Kumar Sahuc16d51a2011-06-13 10:11:33 +02001716 if (plat->exit)
1717 plat->exit();
1718 }
1719
Peter Hurley36f339d2014-11-06 09:06:12 -05001720 if (uap->port.ops->flush_buffer)
1721 uap->port.ops->flush_buffer(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722}
1723
1724static void
Andre Przywaraef5a9352015-05-21 17:26:17 +01001725pl011_setup_status_masks(struct uart_port *port, struct ktermios *termios)
1726{
1727 port->read_status_mask = UART011_DR_OE | 255;
1728 if (termios->c_iflag & INPCK)
1729 port->read_status_mask |= UART011_DR_FE | UART011_DR_PE;
1730 if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
1731 port->read_status_mask |= UART011_DR_BE;
1732
1733 /*
1734 * Characters to ignore
1735 */
1736 port->ignore_status_mask = 0;
1737 if (termios->c_iflag & IGNPAR)
1738 port->ignore_status_mask |= UART011_DR_FE | UART011_DR_PE;
1739 if (termios->c_iflag & IGNBRK) {
1740 port->ignore_status_mask |= UART011_DR_BE;
1741 /*
1742 * If we're ignoring parity and break indicators,
1743 * ignore overruns too (for real raw support).
1744 */
1745 if (termios->c_iflag & IGNPAR)
1746 port->ignore_status_mask |= UART011_DR_OE;
1747 }
1748
1749 /*
1750 * Ignore all characters if CREAD is not set.
1751 */
1752 if ((termios->c_cflag & CREAD) == 0)
1753 port->ignore_status_mask |= UART_DUMMY_DR_RX;
1754}
1755
1756static void
Alan Cox606d0992006-12-08 02:38:45 -08001757pl011_set_termios(struct uart_port *port, struct ktermios *termios,
1758 struct ktermios *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001760 struct uart_amba_port *uap =
1761 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 unsigned int lcr_h, old_cr;
1763 unsigned long flags;
Russell Kingc19f12b2010-12-22 17:48:26 +00001764 unsigned int baud, quot, clkdiv;
1765
1766 if (uap->vendor->oversampling)
1767 clkdiv = 8;
1768 else
1769 clkdiv = 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770
1771 /*
1772 * Ask the core to calculate the divisor for us.
1773 */
Linus Walleijac3e3fb2010-06-02 20:40:22 +01001774 baud = uart_get_baud_rate(port, termios, old, 0,
Russell Kingc19f12b2010-12-22 17:48:26 +00001775 port->uartclk / clkdiv);
Chanho Min89fa28d2013-04-03 11:10:37 +09001776#ifdef CONFIG_DMA_ENGINE
Chanho Mincb06ff12013-03-27 18:38:11 +09001777 /*
1778 * Adjust RX DMA polling rate with baud rate if not specified.
1779 */
1780 if (uap->dmarx.auto_poll_rate)
1781 uap->dmarx.poll_rate = DIV_ROUND_UP(10000000, baud);
Chanho Min89fa28d2013-04-03 11:10:37 +09001782#endif
Linus Walleijac3e3fb2010-06-02 20:40:22 +01001783
1784 if (baud > port->uartclk/16)
1785 quot = DIV_ROUND_CLOSEST(port->uartclk * 8, baud);
1786 else
1787 quot = DIV_ROUND_CLOSEST(port->uartclk * 4, baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788
1789 switch (termios->c_cflag & CSIZE) {
1790 case CS5:
1791 lcr_h = UART01x_LCRH_WLEN_5;
1792 break;
1793 case CS6:
1794 lcr_h = UART01x_LCRH_WLEN_6;
1795 break;
1796 case CS7:
1797 lcr_h = UART01x_LCRH_WLEN_7;
1798 break;
1799 default: // CS8
1800 lcr_h = UART01x_LCRH_WLEN_8;
1801 break;
1802 }
1803 if (termios->c_cflag & CSTOPB)
1804 lcr_h |= UART01x_LCRH_STP2;
1805 if (termios->c_cflag & PARENB) {
1806 lcr_h |= UART01x_LCRH_PEN;
1807 if (!(termios->c_cflag & PARODD))
1808 lcr_h |= UART01x_LCRH_EPS;
1809 }
Russell Kingffca2b12010-12-22 17:13:05 +00001810 if (uap->fifosize > 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 lcr_h |= UART01x_LCRH_FEN;
1812
1813 spin_lock_irqsave(&port->lock, flags);
1814
1815 /*
1816 * Update the per-port timeout.
1817 */
1818 uart_update_timeout(port, termios->c_cflag, baud);
1819
Andre Przywaraef5a9352015-05-21 17:26:17 +01001820 pl011_setup_status_masks(port, termios);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821
1822 if (UART_ENABLE_MS(port, termios->c_cflag))
1823 pl011_enable_ms(port);
1824
1825 /* first, disable everything */
1826 old_cr = readw(port->membase + UART011_CR);
1827 writew(0, port->membase + UART011_CR);
1828
Rabin Vincent3b438162010-02-12 06:43:11 +01001829 if (termios->c_cflag & CRTSCTS) {
1830 if (old_cr & UART011_CR_RTS)
1831 old_cr |= UART011_CR_RTSEN;
1832
1833 old_cr |= UART011_CR_CTSEN;
1834 uap->autorts = true;
1835 } else {
1836 old_cr &= ~(UART011_CR_CTSEN | UART011_CR_RTSEN);
1837 uap->autorts = false;
1838 }
1839
Russell Kingc19f12b2010-12-22 17:48:26 +00001840 if (uap->vendor->oversampling) {
1841 if (baud > port->uartclk / 16)
Linus Walleijac3e3fb2010-06-02 20:40:22 +01001842 old_cr |= ST_UART011_CR_OVSFACT;
1843 else
1844 old_cr &= ~ST_UART011_CR_OVSFACT;
1845 }
1846
Linus Walleijc5dd5532012-09-26 17:21:36 +02001847 /*
1848 * Workaround for the ST Micro oversampling variants to
1849 * increase the bitrate slightly, by lowering the divisor,
1850 * to avoid delayed sampling of start bit at high speeds,
1851 * else we see data corruption.
1852 */
1853 if (uap->vendor->oversampling) {
1854 if ((baud >= 3000000) && (baud < 3250000) && (quot > 1))
1855 quot -= 1;
1856 else if ((baud > 3250000) && (quot > 2))
1857 quot -= 2;
1858 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 /* Set baud rate */
1860 writew(quot & 0x3f, port->membase + UART011_FBRD);
1861 writew(quot >> 6, port->membase + UART011_IBRD);
1862
1863 /*
1864 * ----------v----------v----------v----------v-----
Linus Walleijc5dd5532012-09-26 17:21:36 +02001865 * NOTE: lcrh_tx and lcrh_rx MUST BE WRITTEN AFTER
1866 * UART011_FBRD & UART011_IBRD.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 * ----------^----------^----------^----------^-----
1868 */
Jon Medhurstb60f2f62013-12-10 10:18:59 +00001869 pl011_write_lcr_h(uap, lcr_h);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 writew(old_cr, port->membase + UART011_CR);
1871
1872 spin_unlock_irqrestore(&port->lock, flags);
1873}
1874
1875static const char *pl011_type(struct uart_port *port)
1876{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001877 struct uart_amba_port *uap =
1878 container_of(port, struct uart_amba_port, port);
Russell Kinge8a7ba82010-12-28 09:16:54 +00001879 return uap->port.type == PORT_AMBA ? uap->type : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880}
1881
1882/*
1883 * Release the memory region(s) being used by 'port'
1884 */
Linus Walleije643f872012-06-17 15:44:19 +02001885static void pl011_release_port(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886{
1887 release_mem_region(port->mapbase, SZ_4K);
1888}
1889
1890/*
1891 * Request the memory region(s) being used by 'port'
1892 */
Linus Walleije643f872012-06-17 15:44:19 +02001893static int pl011_request_port(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894{
1895 return request_mem_region(port->mapbase, SZ_4K, "uart-pl011")
1896 != NULL ? 0 : -EBUSY;
1897}
1898
1899/*
1900 * Configure/autoconfigure the port.
1901 */
Linus Walleije643f872012-06-17 15:44:19 +02001902static void pl011_config_port(struct uart_port *port, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903{
1904 if (flags & UART_CONFIG_TYPE) {
1905 port->type = PORT_AMBA;
Linus Walleije643f872012-06-17 15:44:19 +02001906 pl011_request_port(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 }
1908}
1909
1910/*
1911 * verify the new serial_struct (for TIOCSSERIAL).
1912 */
Linus Walleije643f872012-06-17 15:44:19 +02001913static int pl011_verify_port(struct uart_port *port, struct serial_struct *ser)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914{
1915 int ret = 0;
1916 if (ser->type != PORT_UNKNOWN && ser->type != PORT_AMBA)
1917 ret = -EINVAL;
Yinghai Lua62c4132008-08-19 20:49:55 -07001918 if (ser->irq < 0 || ser->irq >= nr_irqs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 ret = -EINVAL;
1920 if (ser->baud_base < 9600)
1921 ret = -EINVAL;
1922 return ret;
1923}
1924
1925static struct uart_ops amba_pl011_pops = {
Linus Walleije643f872012-06-17 15:44:19 +02001926 .tx_empty = pl011_tx_empty,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 .set_mctrl = pl011_set_mctrl,
Linus Walleije643f872012-06-17 15:44:19 +02001928 .get_mctrl = pl011_get_mctrl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 .stop_tx = pl011_stop_tx,
1930 .start_tx = pl011_start_tx,
1931 .stop_rx = pl011_stop_rx,
1932 .enable_ms = pl011_enable_ms,
1933 .break_ctl = pl011_break_ctl,
1934 .startup = pl011_startup,
1935 .shutdown = pl011_shutdown,
Russell King68b65f72010-12-22 17:24:39 +00001936 .flush_buffer = pl011_dma_flush_buffer,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 .set_termios = pl011_set_termios,
1938 .type = pl011_type,
Linus Walleije643f872012-06-17 15:44:19 +02001939 .release_port = pl011_release_port,
1940 .request_port = pl011_request_port,
1941 .config_port = pl011_config_port,
1942 .verify_port = pl011_verify_port,
Jason Wessel84b5ae12008-02-20 13:33:39 -06001943#ifdef CONFIG_CONSOLE_POLL
Anton Vorontsovb3564c22012-09-24 14:27:54 -07001944 .poll_init = pl011_hwinit,
Linus Walleije643f872012-06-17 15:44:19 +02001945 .poll_get_char = pl011_get_poll_char,
1946 .poll_put_char = pl011_put_poll_char,
Jason Wessel84b5ae12008-02-20 13:33:39 -06001947#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948};
1949
1950static struct uart_amba_port *amba_ports[UART_NR];
1951
1952#ifdef CONFIG_SERIAL_AMBA_PL011_CONSOLE
1953
Russell Kingd3587882006-03-20 20:00:09 +00001954static void pl011_console_putchar(struct uart_port *port, int ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001956 struct uart_amba_port *uap =
1957 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958
Russell Kingd3587882006-03-20 20:00:09 +00001959 while (readw(uap->port.membase + UART01x_FR) & UART01x_FR_TXFF)
1960 barrier();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 writew(ch, uap->port.membase + UART01x_DR);
1962}
1963
1964static void
1965pl011_console_write(struct console *co, const char *s, unsigned int count)
1966{
1967 struct uart_amba_port *uap = amba_ports[co->index];
Andre Przywara71eec482015-05-21 17:26:21 +01001968 unsigned int status, old_cr = 0, new_cr;
Rabin Vincentef605fd2012-01-17 11:52:28 +01001969 unsigned long flags;
1970 int locked = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971
1972 clk_enable(uap->clk);
1973
Rabin Vincentef605fd2012-01-17 11:52:28 +01001974 local_irq_save(flags);
1975 if (uap->port.sysrq)
1976 locked = 0;
1977 else if (oops_in_progress)
1978 locked = spin_trylock(&uap->port.lock);
1979 else
1980 spin_lock(&uap->port.lock);
1981
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 /*
1983 * First save the CR then disable the interrupts
1984 */
Andre Przywara71eec482015-05-21 17:26:21 +01001985 if (!uap->vendor->always_enabled) {
1986 old_cr = readw(uap->port.membase + UART011_CR);
1987 new_cr = old_cr & ~UART011_CR_CTSEN;
1988 new_cr |= UART01x_CR_UARTEN | UART011_CR_TXE;
1989 writew(new_cr, uap->port.membase + UART011_CR);
1990 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991
Russell Kingd3587882006-03-20 20:00:09 +00001992 uart_console_write(&uap->port, s, count, pl011_console_putchar);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993
1994 /*
1995 * Finally, wait for transmitter to become empty
1996 * and restore the TCR
1997 */
1998 do {
1999 status = readw(uap->port.membase + UART01x_FR);
2000 } while (status & UART01x_FR_BUSY);
Andre Przywara71eec482015-05-21 17:26:21 +01002001 if (!uap->vendor->always_enabled)
2002 writew(old_cr, uap->port.membase + UART011_CR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003
Rabin Vincentef605fd2012-01-17 11:52:28 +01002004 if (locked)
2005 spin_unlock(&uap->port.lock);
2006 local_irq_restore(flags);
2007
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 clk_disable(uap->clk);
2009}
2010
2011static void __init
2012pl011_console_get_options(struct uart_amba_port *uap, int *baud,
2013 int *parity, int *bits)
2014{
2015 if (readw(uap->port.membase + UART011_CR) & UART01x_CR_UARTEN) {
2016 unsigned int lcr_h, ibrd, fbrd;
2017
Linus Walleijec489aa2010-06-02 08:13:52 +01002018 lcr_h = readw(uap->port.membase + uap->lcrh_tx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019
2020 *parity = 'n';
2021 if (lcr_h & UART01x_LCRH_PEN) {
2022 if (lcr_h & UART01x_LCRH_EPS)
2023 *parity = 'e';
2024 else
2025 *parity = 'o';
2026 }
2027
2028 if ((lcr_h & 0x60) == UART01x_LCRH_WLEN_7)
2029 *bits = 7;
2030 else
2031 *bits = 8;
2032
2033 ibrd = readw(uap->port.membase + UART011_IBRD);
2034 fbrd = readw(uap->port.membase + UART011_FBRD);
2035
2036 *baud = uap->port.uartclk * 4 / (64 * ibrd + fbrd);
Linus Walleijac3e3fb2010-06-02 20:40:22 +01002037
Russell Kingc19f12b2010-12-22 17:48:26 +00002038 if (uap->vendor->oversampling) {
Linus Walleijac3e3fb2010-06-02 20:40:22 +01002039 if (readw(uap->port.membase + UART011_CR)
2040 & ST_UART011_CR_OVSFACT)
2041 *baud *= 2;
2042 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 }
2044}
2045
2046static int __init pl011_console_setup(struct console *co, char *options)
2047{
2048 struct uart_amba_port *uap;
2049 int baud = 38400;
2050 int bits = 8;
2051 int parity = 'n';
2052 int flow = 'n';
Russell King4b4851c2011-09-22 11:35:30 +01002053 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054
2055 /*
2056 * Check whether an invalid uart number has been specified, and
2057 * if so, search for the first available port that does have
2058 * console support.
2059 */
2060 if (co->index >= UART_NR)
2061 co->index = 0;
2062 uap = amba_ports[co->index];
Russell Kingd28122a2007-01-22 18:59:42 +00002063 if (!uap)
2064 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065
Linus Walleij78d80c52012-05-23 21:18:46 +02002066 /* Allow pins to be muxed in and configured */
Linus Walleij2b996fc2013-06-05 15:36:42 +02002067 pinctrl_pm_select_default_state(uap->port.dev);
Linus Walleij78d80c52012-05-23 21:18:46 +02002068
Russell King4b4851c2011-09-22 11:35:30 +01002069 ret = clk_prepare(uap->clk);
2070 if (ret)
2071 return ret;
2072
Jingoo Han574de552013-07-30 17:06:57 +09002073 if (dev_get_platdata(uap->port.dev)) {
Shreshtha Kumar Sahuc16d51a2011-06-13 10:11:33 +02002074 struct amba_pl011_data *plat;
2075
Jingoo Han574de552013-07-30 17:06:57 +09002076 plat = dev_get_platdata(uap->port.dev);
Shreshtha Kumar Sahuc16d51a2011-06-13 10:11:33 +02002077 if (plat->init)
2078 plat->init();
2079 }
2080
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 uap->port.uartclk = clk_get_rate(uap->clk);
2082
Andre Przywaracefc2d12015-05-21 17:26:22 +01002083 if (uap->vendor->fixed_options) {
2084 baud = uap->fixed_baud;
2085 } else {
2086 if (options)
2087 uart_parse_options(options,
2088 &baud, &parity, &bits, &flow);
2089 else
2090 pl011_console_get_options(uap, &baud, &parity, &bits);
2091 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092
2093 return uart_set_options(&uap->port, co, baud, parity, bits, flow);
2094}
2095
Vincent Sanders2d934862005-09-14 22:36:03 +01002096static struct uart_driver amba_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097static struct console amba_console = {
2098 .name = "ttyAMA",
2099 .write = pl011_console_write,
2100 .device = uart_console_device,
2101 .setup = pl011_console_setup,
2102 .flags = CON_PRINTBUFFER,
2103 .index = -1,
2104 .data = &amba_reg,
2105};
2106
2107#define AMBA_CONSOLE (&amba_console)
Rob Herring0d3c6732014-04-18 17:19:57 -05002108
2109static void pl011_putc(struct uart_port *port, int c)
2110{
2111 while (readl(port->membase + UART01x_FR) & UART01x_FR_TXFF)
2112 ;
2113 writeb(c, port->membase + UART01x_DR);
2114 while (readl(port->membase + UART01x_FR) & UART01x_FR_BUSY)
2115 ;
2116}
2117
2118static void pl011_early_write(struct console *con, const char *s, unsigned n)
2119{
2120 struct earlycon_device *dev = con->data;
2121
2122 uart_console_write(&dev->port, s, n, pl011_putc);
2123}
2124
2125static int __init pl011_early_console_setup(struct earlycon_device *device,
2126 const char *opt)
2127{
2128 if (!device->port.membase)
2129 return -ENODEV;
2130
2131 device->con->write = pl011_early_write;
2132 return 0;
2133}
2134EARLYCON_DECLARE(pl011, pl011_early_console_setup);
Rob Herring45e0f0f2014-03-27 08:08:03 -05002135OF_EARLYCON_DECLARE(pl011, "arm,pl011", pl011_early_console_setup);
Rob Herring0d3c6732014-04-18 17:19:57 -05002136
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137#else
2138#define AMBA_CONSOLE NULL
2139#endif
2140
2141static struct uart_driver amba_reg = {
2142 .owner = THIS_MODULE,
2143 .driver_name = "ttyAMA",
2144 .dev_name = "ttyAMA",
2145 .major = SERIAL_AMBA_MAJOR,
2146 .minor = SERIAL_AMBA_MINOR,
2147 .nr = UART_NR,
2148 .cons = AMBA_CONSOLE,
2149};
2150
Matthew Leach32614aa2012-08-28 16:41:28 +01002151static int pl011_probe_dt_alias(int index, struct device *dev)
2152{
2153 struct device_node *np;
2154 static bool seen_dev_with_alias = false;
2155 static bool seen_dev_without_alias = false;
2156 int ret = index;
2157
2158 if (!IS_ENABLED(CONFIG_OF))
2159 return ret;
2160
2161 np = dev->of_node;
2162 if (!np)
2163 return ret;
2164
2165 ret = of_alias_get_id(np, "serial");
2166 if (IS_ERR_VALUE(ret)) {
2167 seen_dev_without_alias = true;
2168 ret = index;
2169 } else {
2170 seen_dev_with_alias = true;
2171 if (ret >= ARRAY_SIZE(amba_ports) || amba_ports[ret] != NULL) {
2172 dev_warn(dev, "requested serial port %d not available.\n", ret);
2173 ret = index;
2174 }
2175 }
2176
2177 if (seen_dev_with_alias && seen_dev_without_alias)
2178 dev_warn(dev, "aliased and non-aliased serial devices found in device tree. Serial port enumeration may be unpredictable.\n");
2179
2180 return ret;
2181}
2182
Andre Przywara49bb3c82015-05-21 17:26:14 +01002183/* unregisters the driver also if no more ports are left */
2184static void pl011_unregister_port(struct uart_amba_port *uap)
2185{
2186 int i;
2187 bool busy = false;
2188
2189 for (i = 0; i < ARRAY_SIZE(amba_ports); i++) {
2190 if (amba_ports[i] == uap)
2191 amba_ports[i] = NULL;
2192 else if (amba_ports[i])
2193 busy = true;
2194 }
2195 pl011_dma_remove(uap);
2196 if (!busy)
2197 uart_unregister_driver(&amba_reg);
2198}
2199
Andre Przywara3873e2d2015-05-21 17:26:18 +01002200static int pl011_find_free_port(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201{
Andre Przywara3873e2d2015-05-21 17:26:18 +01002202 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203
2204 for (i = 0; i < ARRAY_SIZE(amba_ports); i++)
2205 if (amba_ports[i] == NULL)
Andre Przywara3873e2d2015-05-21 17:26:18 +01002206 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207
Andre Przywara3873e2d2015-05-21 17:26:18 +01002208 return -EBUSY;
2209}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210
Andre Przywara3873e2d2015-05-21 17:26:18 +01002211static int pl011_setup_port(struct device *dev, struct uart_amba_port *uap,
2212 struct resource *mmiobase, int index)
2213{
2214 void __iomem *base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215
Andre Przywara3873e2d2015-05-21 17:26:18 +01002216 base = devm_ioremap_resource(dev, mmiobase);
Tushar Behera7f6d9422014-06-26 15:35:35 +05302217 if (!base)
2218 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219
Andre Przywara3873e2d2015-05-21 17:26:18 +01002220 index = pl011_probe_dt_alias(index, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221
Shreshtha Kumar Sahud8d8ffa2012-01-18 15:53:59 +05302222 uap->old_cr = 0;
Andre Przywara3873e2d2015-05-21 17:26:18 +01002223 uap->port.dev = dev;
2224 uap->port.mapbase = mmiobase->start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 uap->port.membase = base;
2226 uap->port.iotype = UPIO_MEM;
Russell Kingffca2b12010-12-22 17:13:05 +00002227 uap->port.fifosize = uap->fifosize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 uap->port.flags = UPF_BOOT_AUTOCONF;
Andre Przywara3873e2d2015-05-21 17:26:18 +01002229 uap->port.line = index;
2230
2231 amba_ports[index] = uap;
2232
2233 return 0;
2234}
2235
2236static int pl011_register_port(struct uart_amba_port *uap)
2237{
2238 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239
Linus Walleijc3d8b762012-03-21 20:15:18 +01002240 /* Ensure interrupts from this UART are masked and cleared */
2241 writew(0, uap->port.membase + UART011_IMSC);
2242 writew(0xffff, uap->port.membase + UART011_ICR);
2243
Tushar Beheraef2889f2014-01-20 14:32:35 +05302244 if (!amba_reg.state) {
2245 ret = uart_register_driver(&amba_reg);
2246 if (ret < 0) {
Andre Przywara3873e2d2015-05-21 17:26:18 +01002247 dev_err(uap->port.dev,
Jorge Ramirez-Ortiz1c9be312015-03-06 13:05:40 -05002248 "Failed to register AMBA-PL011 driver\n");
Tushar Beheraef2889f2014-01-20 14:32:35 +05302249 return ret;
2250 }
2251 }
2252
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 ret = uart_add_one_port(&amba_reg, &uap->port);
Andre Przywara49bb3c82015-05-21 17:26:14 +01002254 if (ret)
2255 pl011_unregister_port(uap);
Tushar Behera7f6d9422014-06-26 15:35:35 +05302256
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 return ret;
2258}
2259
Andre Przywara3873e2d2015-05-21 17:26:18 +01002260static int pl011_probe(struct amba_device *dev, const struct amba_id *id)
2261{
2262 struct uart_amba_port *uap;
2263 struct vendor_data *vendor = id->data;
2264 int portnr, ret;
2265
2266 portnr = pl011_find_free_port();
2267 if (portnr < 0)
2268 return portnr;
2269
2270 uap = devm_kzalloc(&dev->dev, sizeof(struct uart_amba_port),
2271 GFP_KERNEL);
2272 if (!uap)
2273 return -ENOMEM;
2274
2275 uap->clk = devm_clk_get(&dev->dev, NULL);
2276 if (IS_ERR(uap->clk))
2277 return PTR_ERR(uap->clk);
2278
2279 uap->vendor = vendor;
2280 uap->lcrh_rx = vendor->lcrh_rx;
2281 uap->lcrh_tx = vendor->lcrh_tx;
2282 uap->fifosize = vendor->get_fifosize(dev);
2283 uap->port.irq = dev->irq[0];
2284 uap->port.ops = &amba_pl011_pops;
2285
2286 snprintf(uap->type, sizeof(uap->type), "PL011 rev%u", amba_rev(dev));
2287
2288 ret = pl011_setup_port(&dev->dev, uap, &dev->res, portnr);
2289 if (ret)
2290 return ret;
2291
2292 amba_set_drvdata(dev, uap);
2293
2294 return pl011_register_port(uap);
2295}
2296
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297static int pl011_remove(struct amba_device *dev)
2298{
2299 struct uart_amba_port *uap = amba_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 uart_remove_one_port(&amba_reg, &uap->port);
Andre Przywara49bb3c82015-05-21 17:26:14 +01002302 pl011_unregister_port(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 return 0;
2304}
2305
Ulf Hanssond0ce8502013-12-03 11:04:28 +01002306#ifdef CONFIG_PM_SLEEP
2307static int pl011_suspend(struct device *dev)
Leo Chenb736b892009-07-28 23:43:33 +01002308{
Ulf Hanssond0ce8502013-12-03 11:04:28 +01002309 struct uart_amba_port *uap = dev_get_drvdata(dev);
Leo Chenb736b892009-07-28 23:43:33 +01002310
2311 if (!uap)
2312 return -EINVAL;
2313
2314 return uart_suspend_port(&amba_reg, &uap->port);
2315}
2316
Ulf Hanssond0ce8502013-12-03 11:04:28 +01002317static int pl011_resume(struct device *dev)
Leo Chenb736b892009-07-28 23:43:33 +01002318{
Ulf Hanssond0ce8502013-12-03 11:04:28 +01002319 struct uart_amba_port *uap = dev_get_drvdata(dev);
Leo Chenb736b892009-07-28 23:43:33 +01002320
2321 if (!uap)
2322 return -EINVAL;
2323
2324 return uart_resume_port(&amba_reg, &uap->port);
2325}
2326#endif
2327
Ulf Hanssond0ce8502013-12-03 11:04:28 +01002328static SIMPLE_DEV_PM_OPS(pl011_dev_pm_ops, pl011_suspend, pl011_resume);
2329
Russell King2c39c9e2010-07-27 08:50:16 +01002330static struct amba_id pl011_ids[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 {
2332 .id = 0x00041011,
2333 .mask = 0x000fffff,
Alessandro Rubini5926a292009-06-04 17:43:04 +01002334 .data = &vendor_arm,
2335 },
2336 {
2337 .id = 0x00380802,
2338 .mask = 0x00ffffff,
2339 .data = &vendor_st,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 },
2341 { 0, 0 },
2342};
2343
Dave Martin60f7a332011-10-05 15:15:22 +01002344MODULE_DEVICE_TABLE(amba, pl011_ids);
2345
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346static struct amba_driver pl011_driver = {
2347 .drv = {
2348 .name = "uart-pl011",
Ulf Hanssond0ce8502013-12-03 11:04:28 +01002349 .pm = &pl011_dev_pm_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 },
2351 .id_table = pl011_ids,
2352 .probe = pl011_probe,
2353 .remove = pl011_remove,
2354};
2355
2356static int __init pl011_init(void)
2357{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 printk(KERN_INFO "Serial: AMBA PL011 UART driver\n");
2359
Tushar Beheraef2889f2014-01-20 14:32:35 +05302360 return amba_driver_register(&pl011_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361}
2362
2363static void __exit pl011_exit(void)
2364{
2365 amba_driver_unregister(&pl011_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366}
2367
Alessandro Rubini4dd9e742009-05-05 05:54:13 +01002368/*
2369 * While this can be a module, if builtin it's most likely the console
2370 * So let's leave module_exit but move module_init to an earlier place
2371 */
2372arch_initcall(pl011_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373module_exit(pl011_exit);
2374
2375MODULE_AUTHOR("ARM Ltd/Deep Blue Solutions Ltd");
2376MODULE_DESCRIPTION("ARM AMBA serial port driver");
2377MODULE_LICENSE("GPL");