blob: 7b428e7652c597cf5e7e0357f3ed93680e24a7f0 [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>
Dave Martin734745c2015-03-04 12:27:33 +000061#include <linux/workqueue.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
63#define UART_NR 14
64
65#define SERIAL_AMBA_MAJOR 204
66#define SERIAL_AMBA_MINOR 64
67#define SERIAL_AMBA_NR UART_NR
68
69#define AMBA_ISR_PASS_LIMIT 256
70
Russell Kingb63d4f02005-11-19 11:10:35 +000071#define UART_DR_ERROR (UART011_DR_OE|UART011_DR_BE|UART011_DR_PE|UART011_DR_FE)
72#define UART_DUMMY_DR_RX (1 << 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
Alessandro Rubini5926a292009-06-04 17:43:04 +010074/* There is by now at least one vendor with differing details, so handle it */
75struct vendor_data {
76 unsigned int ifls;
Linus Walleijec489aa2010-06-02 08:13:52 +010077 unsigned int lcrh_tx;
78 unsigned int lcrh_rx;
Linus Walleijac3e3fb2010-06-02 20:40:22 +010079 bool oversampling;
Russell King38d62432010-12-22 17:59:16 +000080 bool dma_threshold;
Rajanikanth H.V4fd06902012-03-26 11:17:02 +020081 bool cts_event_workaround;
Jongsung Kim78506f22013-04-15 14:45:25 +090082
Jongsung Kimea336402013-05-10 18:05:35 +090083 unsigned int (*get_fifosize)(struct amba_device *dev);
Alessandro Rubini5926a292009-06-04 17:43:04 +010084};
85
Jongsung Kimea336402013-05-10 18:05:35 +090086static unsigned int get_fifosize_arm(struct amba_device *dev)
Jongsung Kim78506f22013-04-15 14:45:25 +090087{
Jongsung Kimea336402013-05-10 18:05:35 +090088 return amba_rev(dev) < 3 ? 16 : 32;
Jongsung Kim78506f22013-04-15 14:45:25 +090089}
90
Alessandro Rubini5926a292009-06-04 17:43:04 +010091static struct vendor_data vendor_arm = {
92 .ifls = UART011_IFLS_RX4_8|UART011_IFLS_TX4_8,
Linus Walleijec489aa2010-06-02 08:13:52 +010093 .lcrh_tx = UART011_LCRH,
94 .lcrh_rx = UART011_LCRH,
Linus Walleijac3e3fb2010-06-02 20:40:22 +010095 .oversampling = false,
Russell King38d62432010-12-22 17:59:16 +000096 .dma_threshold = false,
Rajanikanth H.V4fd06902012-03-26 11:17:02 +020097 .cts_event_workaround = false,
Jongsung Kim78506f22013-04-15 14:45:25 +090098 .get_fifosize = get_fifosize_arm,
Alessandro Rubini5926a292009-06-04 17:43:04 +010099};
100
Jongsung Kimea336402013-05-10 18:05:35 +0900101static unsigned int get_fifosize_st(struct amba_device *dev)
Jongsung Kim78506f22013-04-15 14:45:25 +0900102{
103 return 64;
104}
105
Alessandro Rubini5926a292009-06-04 17:43:04 +0100106static struct vendor_data vendor_st = {
107 .ifls = UART011_IFLS_RX_HALF|UART011_IFLS_TX_HALF,
Linus Walleijec489aa2010-06-02 08:13:52 +0100108 .lcrh_tx = ST_UART011_LCRH_TX,
109 .lcrh_rx = ST_UART011_LCRH_RX,
Linus Walleijac3e3fb2010-06-02 20:40:22 +0100110 .oversampling = true,
Russell King38d62432010-12-22 17:59:16 +0000111 .dma_threshold = true,
Rajanikanth H.V4fd06902012-03-26 11:17:02 +0200112 .cts_event_workaround = true,
Jongsung Kim78506f22013-04-15 14:45:25 +0900113 .get_fifosize = get_fifosize_st,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114};
115
Russell King68b65f72010-12-22 17:24:39 +0000116/* Deals with DMA transactions */
Linus Walleijead76f32011-02-24 13:21:08 +0100117
118struct pl011_sgbuf {
119 struct scatterlist sg;
120 char *buf;
121};
122
123struct pl011_dmarx_data {
124 struct dma_chan *chan;
125 struct completion complete;
126 bool use_buf_b;
127 struct pl011_sgbuf sgbuf_a;
128 struct pl011_sgbuf sgbuf_b;
129 dma_cookie_t cookie;
130 bool running;
Chanho Mincb06ff12013-03-27 18:38:11 +0900131 struct timer_list timer;
132 unsigned int last_residue;
133 unsigned long last_jiffies;
134 bool auto_poll_rate;
135 unsigned int poll_rate;
136 unsigned int poll_timeout;
Linus Walleijead76f32011-02-24 13:21:08 +0100137};
138
Russell King68b65f72010-12-22 17:24:39 +0000139struct pl011_dmatx_data {
140 struct dma_chan *chan;
141 struct scatterlist sg;
142 char *buf;
143 bool queued;
144};
145
Russell Kingc19f12b2010-12-22 17:48:26 +0000146/*
147 * We wrap our port structure around the generic uart_port.
148 */
149struct uart_amba_port {
150 struct uart_port port;
151 struct clk *clk;
152 const struct vendor_data *vendor;
Russell King68b65f72010-12-22 17:24:39 +0000153 unsigned int dmacr; /* dma control reg */
Russell Kingc19f12b2010-12-22 17:48:26 +0000154 unsigned int im; /* interrupt mask */
155 unsigned int old_status;
Russell Kingffca2b12010-12-22 17:13:05 +0000156 unsigned int fifosize; /* vendor-specific */
Russell Kingc19f12b2010-12-22 17:48:26 +0000157 unsigned int lcrh_tx; /* vendor-specific */
158 unsigned int lcrh_rx; /* vendor-specific */
Shreshtha Kumar Sahud8d8ffa2012-01-18 15:53:59 +0530159 unsigned int old_cr; /* state during shutdown */
Dave Martin734745c2015-03-04 12:27:33 +0000160 struct delayed_work tx_softirq_work;
Russell Kingc19f12b2010-12-22 17:48:26 +0000161 bool autorts;
Dave Martin734745c2015-03-04 12:27:33 +0000162 unsigned int tx_irq_seen; /* 0=none, 1=1, 2=2 or more */
Russell Kingc19f12b2010-12-22 17:48:26 +0000163 char type[12];
Russell King68b65f72010-12-22 17:24:39 +0000164#ifdef CONFIG_DMA_ENGINE
165 /* DMA stuff */
Linus Walleijead76f32011-02-24 13:21:08 +0100166 bool using_tx_dma;
167 bool using_rx_dma;
168 struct pl011_dmarx_data dmarx;
Russell King68b65f72010-12-22 17:24:39 +0000169 struct pl011_dmatx_data dmatx;
Jorge Ramirez-Ortiz1c9be312015-03-06 13:05:40 -0500170 bool dma_probed;
Russell King68b65f72010-12-22 17:24:39 +0000171#endif
Russell Kingc19f12b2010-12-22 17:48:26 +0000172};
173
Russell King68b65f72010-12-22 17:24:39 +0000174/*
Linus Walleij29772c42011-02-24 13:21:36 +0100175 * Reads up to 256 characters from the FIFO or until it's empty and
176 * inserts them into the TTY layer. Returns the number of characters
177 * read from the FIFO.
178 */
179static int pl011_fifo_to_tty(struct uart_amba_port *uap)
180{
181 u16 status, ch;
182 unsigned int flag, max_count = 256;
183 int fifotaken = 0;
184
185 while (max_count--) {
186 status = readw(uap->port.membase + UART01x_FR);
187 if (status & UART01x_FR_RXFE)
188 break;
189
190 /* Take chars from the FIFO and update status */
191 ch = readw(uap->port.membase + UART01x_DR) |
192 UART_DUMMY_DR_RX;
193 flag = TTY_NORMAL;
194 uap->port.icount.rx++;
195 fifotaken++;
196
197 if (unlikely(ch & UART_DR_ERROR)) {
198 if (ch & UART011_DR_BE) {
199 ch &= ~(UART011_DR_FE | UART011_DR_PE);
200 uap->port.icount.brk++;
201 if (uart_handle_break(&uap->port))
202 continue;
203 } else if (ch & UART011_DR_PE)
204 uap->port.icount.parity++;
205 else if (ch & UART011_DR_FE)
206 uap->port.icount.frame++;
207 if (ch & UART011_DR_OE)
208 uap->port.icount.overrun++;
209
210 ch &= uap->port.read_status_mask;
211
212 if (ch & UART011_DR_BE)
213 flag = TTY_BREAK;
214 else if (ch & UART011_DR_PE)
215 flag = TTY_PARITY;
216 else if (ch & UART011_DR_FE)
217 flag = TTY_FRAME;
218 }
219
220 if (uart_handle_sysrq_char(&uap->port, ch & 255))
221 continue;
222
223 uart_insert_char(&uap->port, ch, UART011_DR_OE, ch, flag);
224 }
225
226 return fifotaken;
227}
228
229
230/*
Russell King68b65f72010-12-22 17:24:39 +0000231 * All the DMA operation mode stuff goes inside this ifdef.
232 * This assumes that you have a generic DMA device interface,
233 * no custom DMA interfaces are supported.
234 */
235#ifdef CONFIG_DMA_ENGINE
236
237#define PL011_DMA_BUFFER_SIZE PAGE_SIZE
238
Linus Walleijead76f32011-02-24 13:21:08 +0100239static int pl011_sgbuf_init(struct dma_chan *chan, struct pl011_sgbuf *sg,
240 enum dma_data_direction dir)
241{
Chanho Mincb06ff12013-03-27 18:38:11 +0900242 dma_addr_t dma_addr;
243
244 sg->buf = dma_alloc_coherent(chan->device->dev,
245 PL011_DMA_BUFFER_SIZE, &dma_addr, GFP_KERNEL);
Linus Walleijead76f32011-02-24 13:21:08 +0100246 if (!sg->buf)
247 return -ENOMEM;
248
Chanho Mincb06ff12013-03-27 18:38:11 +0900249 sg_init_table(&sg->sg, 1);
250 sg_set_page(&sg->sg, phys_to_page(dma_addr),
251 PL011_DMA_BUFFER_SIZE, offset_in_page(dma_addr));
252 sg_dma_address(&sg->sg) = dma_addr;
Andrew Jacksonc64be922014-11-07 14:14:43 +0000253 sg_dma_len(&sg->sg) = PL011_DMA_BUFFER_SIZE;
Linus Walleijead76f32011-02-24 13:21:08 +0100254
Linus Walleijead76f32011-02-24 13:21:08 +0100255 return 0;
256}
257
258static void pl011_sgbuf_free(struct dma_chan *chan, struct pl011_sgbuf *sg,
259 enum dma_data_direction dir)
260{
261 if (sg->buf) {
Chanho Mincb06ff12013-03-27 18:38:11 +0900262 dma_free_coherent(chan->device->dev,
263 PL011_DMA_BUFFER_SIZE, sg->buf,
264 sg_dma_address(&sg->sg));
Linus Walleijead76f32011-02-24 13:21:08 +0100265 }
266}
267
Jorge Ramirez-Ortiz1c9be312015-03-06 13:05:40 -0500268static void pl011_dma_probe(struct uart_amba_port *uap)
Russell King68b65f72010-12-22 17:24:39 +0000269{
270 /* DMA is the sole user of the platform data right now */
Jingoo Han574de552013-07-30 17:06:57 +0900271 struct amba_pl011_data *plat = dev_get_platdata(uap->port.dev);
Jorge Ramirez-Ortiz1c9be312015-03-06 13:05:40 -0500272 struct device *dev = uap->port.dev;
Russell King68b65f72010-12-22 17:24:39 +0000273 struct dma_slave_config tx_conf = {
274 .dst_addr = uap->port.mapbase + UART01x_DR,
275 .dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE,
Vinod Koula485df42011-10-14 10:47:38 +0530276 .direction = DMA_MEM_TO_DEV,
Russell King68b65f72010-12-22 17:24:39 +0000277 .dst_maxburst = uap->fifosize >> 1,
Viresh Kumar258aea72012-02-01 16:12:19 +0530278 .device_fc = false,
Russell King68b65f72010-12-22 17:24:39 +0000279 };
280 struct dma_chan *chan;
281 dma_cap_mask_t mask;
282
Jorge Ramirez-Ortiz1c9be312015-03-06 13:05:40 -0500283 uap->dma_probed = true;
284 chan = dma_request_slave_channel_reason(dev, "tx");
285 if (IS_ERR(chan)) {
286 if (PTR_ERR(chan) == -EPROBE_DEFER) {
287 dev_info(uap->port.dev, "DMA driver not ready\n");
288 uap->dma_probed = false;
289 return;
290 }
Russell King68b65f72010-12-22 17:24:39 +0000291
Arnd Bergmann787b0c12013-01-28 16:24:37 +0000292 /* We need platform data */
293 if (!plat || !plat->dma_filter) {
294 dev_info(uap->port.dev, "no DMA platform data\n");
295 return;
296 }
297
298 /* Try to acquire a generic DMA engine slave TX channel */
299 dma_cap_zero(mask);
300 dma_cap_set(DMA_SLAVE, mask);
301
302 chan = dma_request_channel(mask, plat->dma_filter,
303 plat->dma_tx_param);
304 if (!chan) {
305 dev_err(uap->port.dev, "no TX DMA channel!\n");
306 return;
307 }
Russell King68b65f72010-12-22 17:24:39 +0000308 }
309
310 dmaengine_slave_config(chan, &tx_conf);
311 uap->dmatx.chan = chan;
312
313 dev_info(uap->port.dev, "DMA channel TX %s\n",
314 dma_chan_name(uap->dmatx.chan));
Linus Walleijead76f32011-02-24 13:21:08 +0100315
316 /* Optionally make use of an RX channel as well */
Arnd Bergmann787b0c12013-01-28 16:24:37 +0000317 chan = dma_request_slave_channel(dev, "rx");
Rob Herring0d3c6732014-04-18 17:19:57 -0500318
Arnd Bergmann787b0c12013-01-28 16:24:37 +0000319 if (!chan && plat->dma_rx_param) {
320 chan = dma_request_channel(mask, plat->dma_filter, plat->dma_rx_param);
321
322 if (!chan) {
323 dev_err(uap->port.dev, "no RX DMA channel!\n");
324 return;
325 }
326 }
327
328 if (chan) {
Linus Walleijead76f32011-02-24 13:21:08 +0100329 struct dma_slave_config rx_conf = {
330 .src_addr = uap->port.mapbase + UART01x_DR,
331 .src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE,
Vinod Koula485df42011-10-14 10:47:38 +0530332 .direction = DMA_DEV_TO_MEM,
Guennadi Liakhovetskib2aeb772014-04-12 19:47:17 +0200333 .src_maxburst = uap->fifosize >> 2,
Viresh Kumar258aea72012-02-01 16:12:19 +0530334 .device_fc = false,
Linus Walleijead76f32011-02-24 13:21:08 +0100335 };
Andrew Jackson2d3b7d62014-11-07 14:14:47 +0000336 struct dma_slave_caps caps;
Linus Walleijead76f32011-02-24 13:21:08 +0100337
Andrew Jackson2d3b7d62014-11-07 14:14:47 +0000338 /*
339 * Some DMA controllers provide information on their capabilities.
340 * If the controller does, check for suitable residue processing
341 * otherwise assime all is well.
342 */
343 if (0 == dma_get_slave_caps(chan, &caps)) {
344 if (caps.residue_granularity ==
345 DMA_RESIDUE_GRANULARITY_DESCRIPTOR) {
346 dma_release_channel(chan);
347 dev_info(uap->port.dev,
348 "RX DMA disabled - no residue processing\n");
349 return;
350 }
351 }
Linus Walleijead76f32011-02-24 13:21:08 +0100352 dmaengine_slave_config(chan, &rx_conf);
353 uap->dmarx.chan = chan;
354
Andrew Jackson98267d32014-11-07 14:14:23 +0000355 uap->dmarx.auto_poll_rate = false;
Greg Kroah-Hartman8f898bf2013-12-17 09:33:18 -0800356 if (plat && plat->dma_rx_poll_enable) {
Chanho Mincb06ff12013-03-27 18:38:11 +0900357 /* Set poll rate if specified. */
358 if (plat->dma_rx_poll_rate) {
359 uap->dmarx.auto_poll_rate = false;
360 uap->dmarx.poll_rate = plat->dma_rx_poll_rate;
361 } else {
362 /*
363 * 100 ms defaults to poll rate if not
364 * specified. This will be adjusted with
365 * the baud rate at set_termios.
366 */
367 uap->dmarx.auto_poll_rate = true;
368 uap->dmarx.poll_rate = 100;
369 }
370 /* 3 secs defaults poll_timeout if not specified. */
371 if (plat->dma_rx_poll_timeout)
372 uap->dmarx.poll_timeout =
373 plat->dma_rx_poll_timeout;
374 else
375 uap->dmarx.poll_timeout = 3000;
Andrew Jackson98267d32014-11-07 14:14:23 +0000376 } else if (!plat && dev->of_node) {
377 uap->dmarx.auto_poll_rate = of_property_read_bool(
378 dev->of_node, "auto-poll");
379 if (uap->dmarx.auto_poll_rate) {
380 u32 x;
Chanho Mincb06ff12013-03-27 18:38:11 +0900381
Andrew Jackson98267d32014-11-07 14:14:23 +0000382 if (0 == of_property_read_u32(dev->of_node,
383 "poll-rate-ms", &x))
384 uap->dmarx.poll_rate = x;
385 else
386 uap->dmarx.poll_rate = 100;
387 if (0 == of_property_read_u32(dev->of_node,
388 "poll-timeout-ms", &x))
389 uap->dmarx.poll_timeout = x;
390 else
391 uap->dmarx.poll_timeout = 3000;
392 }
393 }
Linus Walleijead76f32011-02-24 13:21:08 +0100394 dev_info(uap->port.dev, "DMA channel RX %s\n",
395 dma_chan_name(uap->dmarx.chan));
396 }
Russell King68b65f72010-12-22 17:24:39 +0000397}
398
Russell King68b65f72010-12-22 17:24:39 +0000399static void pl011_dma_remove(struct uart_amba_port *uap)
400{
Russell King68b65f72010-12-22 17:24:39 +0000401 if (uap->dmatx.chan)
402 dma_release_channel(uap->dmatx.chan);
Linus Walleijead76f32011-02-24 13:21:08 +0100403 if (uap->dmarx.chan)
404 dma_release_channel(uap->dmarx.chan);
Russell King68b65f72010-12-22 17:24:39 +0000405}
406
Dave Martin734745c2015-03-04 12:27:33 +0000407/* Forward declare these for the refill routine */
Russell King68b65f72010-12-22 17:24:39 +0000408static int pl011_dma_tx_refill(struct uart_amba_port *uap);
Dave Martin734745c2015-03-04 12:27:33 +0000409static void pl011_start_tx_pio(struct uart_amba_port *uap);
Russell King68b65f72010-12-22 17:24:39 +0000410
411/*
412 * The current DMA TX buffer has been sent.
413 * Try to queue up another DMA buffer.
414 */
415static void pl011_dma_tx_callback(void *data)
416{
417 struct uart_amba_port *uap = data;
418 struct pl011_dmatx_data *dmatx = &uap->dmatx;
419 unsigned long flags;
420 u16 dmacr;
421
422 spin_lock_irqsave(&uap->port.lock, flags);
423 if (uap->dmatx.queued)
424 dma_unmap_sg(dmatx->chan->device->dev, &dmatx->sg, 1,
425 DMA_TO_DEVICE);
426
427 dmacr = uap->dmacr;
428 uap->dmacr = dmacr & ~UART011_TXDMAE;
429 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
430
431 /*
432 * If TX DMA was disabled, it means that we've stopped the DMA for
433 * some reason (eg, XOFF received, or we want to send an X-char.)
434 *
435 * Note: we need to be careful here of a potential race between DMA
436 * and the rest of the driver - if the driver disables TX DMA while
437 * a TX buffer completing, we must update the tx queued status to
438 * get further refills (hence we check dmacr).
439 */
440 if (!(dmacr & UART011_TXDMAE) || uart_tx_stopped(&uap->port) ||
441 uart_circ_empty(&uap->port.state->xmit)) {
442 uap->dmatx.queued = false;
443 spin_unlock_irqrestore(&uap->port.lock, flags);
444 return;
445 }
446
Dave Martin734745c2015-03-04 12:27:33 +0000447 if (pl011_dma_tx_refill(uap) <= 0)
Russell King68b65f72010-12-22 17:24:39 +0000448 /*
449 * We didn't queue a DMA buffer for some reason, but we
450 * have data pending to be sent. Re-enable the TX IRQ.
451 */
Dave Martin734745c2015-03-04 12:27:33 +0000452 pl011_start_tx_pio(uap);
453
Russell King68b65f72010-12-22 17:24:39 +0000454 spin_unlock_irqrestore(&uap->port.lock, flags);
455}
456
457/*
458 * Try to refill the TX DMA buffer.
459 * Locking: called with port lock held and IRQs disabled.
460 * Returns:
461 * 1 if we queued up a TX DMA buffer.
462 * 0 if we didn't want to handle this by DMA
463 * <0 on error
464 */
465static int pl011_dma_tx_refill(struct uart_amba_port *uap)
466{
467 struct pl011_dmatx_data *dmatx = &uap->dmatx;
468 struct dma_chan *chan = dmatx->chan;
469 struct dma_device *dma_dev = chan->device;
470 struct dma_async_tx_descriptor *desc;
471 struct circ_buf *xmit = &uap->port.state->xmit;
472 unsigned int count;
473
474 /*
475 * Try to avoid the overhead involved in using DMA if the
476 * transaction fits in the first half of the FIFO, by using
477 * the standard interrupt handling. This ensures that we
478 * issue a uart_write_wakeup() at the appropriate time.
479 */
480 count = uart_circ_chars_pending(xmit);
481 if (count < (uap->fifosize >> 1)) {
482 uap->dmatx.queued = false;
483 return 0;
484 }
485
486 /*
487 * Bodge: don't send the last character by DMA, as this
488 * will prevent XON from notifying us to restart DMA.
489 */
490 count -= 1;
491
492 /* Else proceed to copy the TX chars to the DMA buffer and fire DMA */
493 if (count > PL011_DMA_BUFFER_SIZE)
494 count = PL011_DMA_BUFFER_SIZE;
495
496 if (xmit->tail < xmit->head)
497 memcpy(&dmatx->buf[0], &xmit->buf[xmit->tail], count);
498 else {
499 size_t first = UART_XMIT_SIZE - xmit->tail;
Andrew Jacksone2a545a2014-11-07 14:14:39 +0000500 size_t second;
501
502 if (first > count)
503 first = count;
504 second = count - first;
Russell King68b65f72010-12-22 17:24:39 +0000505
506 memcpy(&dmatx->buf[0], &xmit->buf[xmit->tail], first);
507 if (second)
508 memcpy(&dmatx->buf[first], &xmit->buf[0], second);
509 }
510
511 dmatx->sg.length = count;
512
513 if (dma_map_sg(dma_dev->dev, &dmatx->sg, 1, DMA_TO_DEVICE) != 1) {
514 uap->dmatx.queued = false;
515 dev_dbg(uap->port.dev, "unable to map TX DMA\n");
516 return -EBUSY;
517 }
518
Alexandre Bounine16052822012-03-08 16:11:18 -0500519 desc = dmaengine_prep_slave_sg(chan, &dmatx->sg, 1, DMA_MEM_TO_DEV,
Russell King68b65f72010-12-22 17:24:39 +0000520 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
521 if (!desc) {
522 dma_unmap_sg(dma_dev->dev, &dmatx->sg, 1, DMA_TO_DEVICE);
523 uap->dmatx.queued = false;
524 /*
525 * If DMA cannot be used right now, we complete this
526 * transaction via IRQ and let the TTY layer retry.
527 */
528 dev_dbg(uap->port.dev, "TX DMA busy\n");
529 return -EBUSY;
530 }
531
532 /* Some data to go along to the callback */
533 desc->callback = pl011_dma_tx_callback;
534 desc->callback_param = uap;
535
536 /* All errors should happen at prepare time */
537 dmaengine_submit(desc);
538
539 /* Fire the DMA transaction */
540 dma_dev->device_issue_pending(chan);
541
542 uap->dmacr |= UART011_TXDMAE;
543 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
544 uap->dmatx.queued = true;
545
546 /*
547 * Now we know that DMA will fire, so advance the ring buffer
548 * with the stuff we just dispatched.
549 */
550 xmit->tail = (xmit->tail + count) & (UART_XMIT_SIZE - 1);
551 uap->port.icount.tx += count;
552
553 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
554 uart_write_wakeup(&uap->port);
555
556 return 1;
557}
558
559/*
560 * We received a transmit interrupt without a pending X-char but with
561 * pending characters.
562 * Locking: called with port lock held and IRQs disabled.
563 * Returns:
564 * false if we want to use PIO to transmit
565 * true if we queued a DMA buffer
566 */
567static bool pl011_dma_tx_irq(struct uart_amba_port *uap)
568{
Linus Walleijead76f32011-02-24 13:21:08 +0100569 if (!uap->using_tx_dma)
Russell King68b65f72010-12-22 17:24:39 +0000570 return false;
571
572 /*
573 * If we already have a TX buffer queued, but received a
574 * TX interrupt, it will be because we've just sent an X-char.
575 * Ensure the TX DMA is enabled and the TX IRQ is disabled.
576 */
577 if (uap->dmatx.queued) {
578 uap->dmacr |= UART011_TXDMAE;
579 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
580 uap->im &= ~UART011_TXIM;
581 writew(uap->im, uap->port.membase + UART011_IMSC);
582 return true;
583 }
584
585 /*
586 * We don't have a TX buffer queued, so try to queue one.
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300587 * If we successfully queued a buffer, mask the TX IRQ.
Russell King68b65f72010-12-22 17:24:39 +0000588 */
589 if (pl011_dma_tx_refill(uap) > 0) {
590 uap->im &= ~UART011_TXIM;
591 writew(uap->im, uap->port.membase + UART011_IMSC);
592 return true;
593 }
594 return false;
595}
596
597/*
598 * Stop the DMA transmit (eg, due to received XOFF).
599 * Locking: called with port lock held and IRQs disabled.
600 */
601static inline void pl011_dma_tx_stop(struct uart_amba_port *uap)
602{
603 if (uap->dmatx.queued) {
604 uap->dmacr &= ~UART011_TXDMAE;
605 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
606 }
607}
608
609/*
610 * Try to start a DMA transmit, or in the case of an XON/OFF
611 * character queued for send, try to get that character out ASAP.
612 * Locking: called with port lock held and IRQs disabled.
613 * Returns:
614 * false if we want the TX IRQ to be enabled
615 * true if we have a buffer queued
616 */
617static inline bool pl011_dma_tx_start(struct uart_amba_port *uap)
618{
619 u16 dmacr;
620
Linus Walleijead76f32011-02-24 13:21:08 +0100621 if (!uap->using_tx_dma)
Russell King68b65f72010-12-22 17:24:39 +0000622 return false;
623
624 if (!uap->port.x_char) {
625 /* no X-char, try to push chars out in DMA mode */
626 bool ret = true;
627
628 if (!uap->dmatx.queued) {
629 if (pl011_dma_tx_refill(uap) > 0) {
630 uap->im &= ~UART011_TXIM;
Dave Martin734745c2015-03-04 12:27:33 +0000631 writew(uap->im, uap->port.membase +
632 UART011_IMSC);
633 } else
Russell King68b65f72010-12-22 17:24:39 +0000634 ret = false;
Russell King68b65f72010-12-22 17:24:39 +0000635 } else if (!(uap->dmacr & UART011_TXDMAE)) {
636 uap->dmacr |= UART011_TXDMAE;
637 writew(uap->dmacr,
638 uap->port.membase + UART011_DMACR);
639 }
640 return ret;
641 }
642
643 /*
644 * We have an X-char to send. Disable DMA to prevent it loading
645 * the TX fifo, and then see if we can stuff it into the FIFO.
646 */
647 dmacr = uap->dmacr;
648 uap->dmacr &= ~UART011_TXDMAE;
649 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
650
651 if (readw(uap->port.membase + UART01x_FR) & UART01x_FR_TXFF) {
652 /*
653 * No space in the FIFO, so enable the transmit interrupt
654 * so we know when there is space. Note that once we've
655 * loaded the character, we should just re-enable DMA.
656 */
657 return false;
658 }
659
660 writew(uap->port.x_char, uap->port.membase + UART01x_DR);
661 uap->port.icount.tx++;
662 uap->port.x_char = 0;
663
664 /* Success - restore the DMA state */
665 uap->dmacr = dmacr;
666 writew(dmacr, uap->port.membase + UART011_DMACR);
667
668 return true;
669}
670
671/*
672 * Flush the transmit buffer.
673 * Locking: called with port lock held and IRQs disabled.
674 */
675static void pl011_dma_flush_buffer(struct uart_port *port)
Fabio Estevamb83286b2013-08-09 17:58:51 -0300676__releases(&uap->port.lock)
677__acquires(&uap->port.lock)
Russell King68b65f72010-12-22 17:24:39 +0000678{
Daniel Thompsona5820c22014-09-03 12:51:55 +0100679 struct uart_amba_port *uap =
680 container_of(port, struct uart_amba_port, port);
Russell King68b65f72010-12-22 17:24:39 +0000681
Linus Walleijead76f32011-02-24 13:21:08 +0100682 if (!uap->using_tx_dma)
Russell King68b65f72010-12-22 17:24:39 +0000683 return;
684
685 /* Avoid deadlock with the DMA engine callback */
686 spin_unlock(&uap->port.lock);
687 dmaengine_terminate_all(uap->dmatx.chan);
688 spin_lock(&uap->port.lock);
689 if (uap->dmatx.queued) {
690 dma_unmap_sg(uap->dmatx.chan->device->dev, &uap->dmatx.sg, 1,
691 DMA_TO_DEVICE);
692 uap->dmatx.queued = false;
693 uap->dmacr &= ~UART011_TXDMAE;
694 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
695 }
696}
697
Linus Walleijead76f32011-02-24 13:21:08 +0100698static void pl011_dma_rx_callback(void *data);
699
700static int pl011_dma_rx_trigger_dma(struct uart_amba_port *uap)
701{
702 struct dma_chan *rxchan = uap->dmarx.chan;
Linus Walleijead76f32011-02-24 13:21:08 +0100703 struct pl011_dmarx_data *dmarx = &uap->dmarx;
704 struct dma_async_tx_descriptor *desc;
705 struct pl011_sgbuf *sgbuf;
706
707 if (!rxchan)
708 return -EIO;
709
710 /* Start the RX DMA job */
711 sgbuf = uap->dmarx.use_buf_b ?
712 &uap->dmarx.sgbuf_b : &uap->dmarx.sgbuf_a;
Alexandre Bounine16052822012-03-08 16:11:18 -0500713 desc = dmaengine_prep_slave_sg(rxchan, &sgbuf->sg, 1,
Vinod Koula485df42011-10-14 10:47:38 +0530714 DMA_DEV_TO_MEM,
Linus Walleijead76f32011-02-24 13:21:08 +0100715 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
716 /*
717 * If the DMA engine is busy and cannot prepare a
718 * channel, no big deal, the driver will fall back
719 * to interrupt mode as a result of this error code.
720 */
721 if (!desc) {
722 uap->dmarx.running = false;
723 dmaengine_terminate_all(rxchan);
724 return -EBUSY;
725 }
726
727 /* Some data to go along to the callback */
728 desc->callback = pl011_dma_rx_callback;
729 desc->callback_param = uap;
730 dmarx->cookie = dmaengine_submit(desc);
731 dma_async_issue_pending(rxchan);
732
733 uap->dmacr |= UART011_RXDMAE;
734 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
735 uap->dmarx.running = true;
736
737 uap->im &= ~UART011_RXIM;
738 writew(uap->im, uap->port.membase + UART011_IMSC);
739
740 return 0;
741}
742
743/*
744 * This is called when either the DMA job is complete, or
745 * the FIFO timeout interrupt occurred. This must be called
746 * with the port spinlock uap->port.lock held.
747 */
748static void pl011_dma_rx_chars(struct uart_amba_port *uap,
749 u32 pending, bool use_buf_b,
750 bool readfifo)
751{
Jiri Slaby05c7cd32013-01-03 15:53:04 +0100752 struct tty_port *port = &uap->port.state->port;
Linus Walleijead76f32011-02-24 13:21:08 +0100753 struct pl011_sgbuf *sgbuf = use_buf_b ?
754 &uap->dmarx.sgbuf_b : &uap->dmarx.sgbuf_a;
Linus Walleijead76f32011-02-24 13:21:08 +0100755 int dma_count = 0;
756 u32 fifotaken = 0; /* only used for vdbg() */
757
Chanho Mincb06ff12013-03-27 18:38:11 +0900758 struct pl011_dmarx_data *dmarx = &uap->dmarx;
759 int dmataken = 0;
760
761 if (uap->dmarx.poll_rate) {
762 /* The data can be taken by polling */
763 dmataken = sgbuf->sg.length - dmarx->last_residue;
764 /* Recalculate the pending size */
765 if (pending >= dmataken)
766 pending -= dmataken;
767 }
768
769 /* Pick the remain data from the DMA */
Linus Walleijead76f32011-02-24 13:21:08 +0100770 if (pending) {
Linus Walleijead76f32011-02-24 13:21:08 +0100771
772 /*
773 * First take all chars in the DMA pipe, then look in the FIFO.
774 * Note that tty_insert_flip_buf() tries to take as many chars
775 * as it can.
776 */
Chanho Mincb06ff12013-03-27 18:38:11 +0900777 dma_count = tty_insert_flip_string(port, sgbuf->buf + dmataken,
778 pending);
Linus Walleijead76f32011-02-24 13:21:08 +0100779
780 uap->port.icount.rx += dma_count;
781 if (dma_count < pending)
782 dev_warn(uap->port.dev,
783 "couldn't insert all characters (TTY is full?)\n");
784 }
785
Chanho Mincb06ff12013-03-27 18:38:11 +0900786 /* Reset the last_residue for Rx DMA poll */
787 if (uap->dmarx.poll_rate)
788 dmarx->last_residue = sgbuf->sg.length;
789
Linus Walleijead76f32011-02-24 13:21:08 +0100790 /*
791 * Only continue with trying to read the FIFO if all DMA chars have
792 * been taken first.
793 */
794 if (dma_count == pending && readfifo) {
795 /* Clear any error flags */
796 writew(UART011_OEIS | UART011_BEIS | UART011_PEIS | UART011_FEIS,
797 uap->port.membase + UART011_ICR);
798
799 /*
800 * If we read all the DMA'd characters, and we had an
Linus Walleij29772c42011-02-24 13:21:36 +0100801 * incomplete buffer, that could be due to an rx error, or
802 * maybe we just timed out. Read any pending chars and check
803 * the error status.
804 *
805 * Error conditions will only occur in the FIFO, these will
806 * trigger an immediate interrupt and stop the DMA job, so we
807 * will always find the error in the FIFO, never in the DMA
808 * buffer.
Linus Walleijead76f32011-02-24 13:21:08 +0100809 */
Linus Walleij29772c42011-02-24 13:21:36 +0100810 fifotaken = pl011_fifo_to_tty(uap);
Linus Walleijead76f32011-02-24 13:21:08 +0100811 }
812
813 spin_unlock(&uap->port.lock);
814 dev_vdbg(uap->port.dev,
815 "Took %d chars from DMA buffer and %d chars from the FIFO\n",
816 dma_count, fifotaken);
Jiri Slaby2e124b42013-01-03 15:53:06 +0100817 tty_flip_buffer_push(port);
Linus Walleijead76f32011-02-24 13:21:08 +0100818 spin_lock(&uap->port.lock);
819}
820
821static void pl011_dma_rx_irq(struct uart_amba_port *uap)
822{
823 struct pl011_dmarx_data *dmarx = &uap->dmarx;
824 struct dma_chan *rxchan = dmarx->chan;
825 struct pl011_sgbuf *sgbuf = dmarx->use_buf_b ?
826 &dmarx->sgbuf_b : &dmarx->sgbuf_a;
827 size_t pending;
828 struct dma_tx_state state;
829 enum dma_status dmastat;
830
831 /*
832 * Pause the transfer so we can trust the current counter,
833 * do this before we pause the PL011 block, else we may
834 * overflow the FIFO.
835 */
836 if (dmaengine_pause(rxchan))
837 dev_err(uap->port.dev, "unable to pause DMA transfer\n");
838 dmastat = rxchan->device->device_tx_status(rxchan,
839 dmarx->cookie, &state);
840 if (dmastat != DMA_PAUSED)
841 dev_err(uap->port.dev, "unable to pause DMA transfer\n");
842
843 /* Disable RX DMA - incoming data will wait in the FIFO */
844 uap->dmacr &= ~UART011_RXDMAE;
845 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
846 uap->dmarx.running = false;
847
848 pending = sgbuf->sg.length - state.residue;
849 BUG_ON(pending > PL011_DMA_BUFFER_SIZE);
850 /* Then we terminate the transfer - we now know our residue */
851 dmaengine_terminate_all(rxchan);
852
853 /*
854 * This will take the chars we have so far and insert
855 * into the framework.
856 */
857 pl011_dma_rx_chars(uap, pending, dmarx->use_buf_b, true);
858
859 /* Switch buffer & re-trigger DMA job */
860 dmarx->use_buf_b = !dmarx->use_buf_b;
861 if (pl011_dma_rx_trigger_dma(uap)) {
862 dev_dbg(uap->port.dev, "could not retrigger RX DMA job "
863 "fall back to interrupt mode\n");
864 uap->im |= UART011_RXIM;
865 writew(uap->im, uap->port.membase + UART011_IMSC);
866 }
867}
868
869static void pl011_dma_rx_callback(void *data)
870{
871 struct uart_amba_port *uap = data;
872 struct pl011_dmarx_data *dmarx = &uap->dmarx;
Chanho Min6dc01aa2012-02-20 10:24:40 +0900873 struct dma_chan *rxchan = dmarx->chan;
Linus Walleijead76f32011-02-24 13:21:08 +0100874 bool lastbuf = dmarx->use_buf_b;
Chanho Min6dc01aa2012-02-20 10:24:40 +0900875 struct pl011_sgbuf *sgbuf = dmarx->use_buf_b ?
876 &dmarx->sgbuf_b : &dmarx->sgbuf_a;
877 size_t pending;
878 struct dma_tx_state state;
Linus Walleijead76f32011-02-24 13:21:08 +0100879 int ret;
880
881 /*
882 * This completion interrupt occurs typically when the
883 * RX buffer is totally stuffed but no timeout has yet
884 * occurred. When that happens, we just want the RX
885 * routine to flush out the secondary DMA buffer while
886 * we immediately trigger the next DMA job.
887 */
888 spin_lock_irq(&uap->port.lock);
Chanho Min6dc01aa2012-02-20 10:24:40 +0900889 /*
890 * Rx data can be taken by the UART interrupts during
891 * the DMA irq handler. So we check the residue here.
892 */
893 rxchan->device->device_tx_status(rxchan, dmarx->cookie, &state);
894 pending = sgbuf->sg.length - state.residue;
895 BUG_ON(pending > PL011_DMA_BUFFER_SIZE);
896 /* Then we terminate the transfer - we now know our residue */
897 dmaengine_terminate_all(rxchan);
898
Linus Walleijead76f32011-02-24 13:21:08 +0100899 uap->dmarx.running = false;
900 dmarx->use_buf_b = !lastbuf;
901 ret = pl011_dma_rx_trigger_dma(uap);
902
Chanho Min6dc01aa2012-02-20 10:24:40 +0900903 pl011_dma_rx_chars(uap, pending, lastbuf, false);
Linus Walleijead76f32011-02-24 13:21:08 +0100904 spin_unlock_irq(&uap->port.lock);
905 /*
906 * Do this check after we picked the DMA chars so we don't
907 * get some IRQ immediately from RX.
908 */
909 if (ret) {
910 dev_dbg(uap->port.dev, "could not retrigger RX DMA job "
911 "fall back to interrupt mode\n");
912 uap->im |= UART011_RXIM;
913 writew(uap->im, uap->port.membase + UART011_IMSC);
914 }
915}
916
917/*
918 * Stop accepting received characters, when we're shutting down or
919 * suspending this port.
920 * Locking: called with port lock held and IRQs disabled.
921 */
922static inline void pl011_dma_rx_stop(struct uart_amba_port *uap)
923{
924 /* FIXME. Just disable the DMA enable */
925 uap->dmacr &= ~UART011_RXDMAE;
926 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
927}
Russell King68b65f72010-12-22 17:24:39 +0000928
Chanho Mincb06ff12013-03-27 18:38:11 +0900929/*
930 * Timer handler for Rx DMA polling.
931 * Every polling, It checks the residue in the dma buffer and transfer
932 * data to the tty. Also, last_residue is updated for the next polling.
933 */
934static void pl011_dma_rx_poll(unsigned long args)
935{
936 struct uart_amba_port *uap = (struct uart_amba_port *)args;
937 struct tty_port *port = &uap->port.state->port;
938 struct pl011_dmarx_data *dmarx = &uap->dmarx;
939 struct dma_chan *rxchan = uap->dmarx.chan;
940 unsigned long flags = 0;
941 unsigned int dmataken = 0;
942 unsigned int size = 0;
943 struct pl011_sgbuf *sgbuf;
944 int dma_count;
945 struct dma_tx_state state;
946
947 sgbuf = dmarx->use_buf_b ? &uap->dmarx.sgbuf_b : &uap->dmarx.sgbuf_a;
948 rxchan->device->device_tx_status(rxchan, dmarx->cookie, &state);
949 if (likely(state.residue < dmarx->last_residue)) {
950 dmataken = sgbuf->sg.length - dmarx->last_residue;
951 size = dmarx->last_residue - state.residue;
952 dma_count = tty_insert_flip_string(port, sgbuf->buf + dmataken,
953 size);
954 if (dma_count == size)
955 dmarx->last_residue = state.residue;
956 dmarx->last_jiffies = jiffies;
957 }
958 tty_flip_buffer_push(port);
959
960 /*
961 * If no data is received in poll_timeout, the driver will fall back
962 * to interrupt mode. We will retrigger DMA at the first interrupt.
963 */
964 if (jiffies_to_msecs(jiffies - dmarx->last_jiffies)
965 > uap->dmarx.poll_timeout) {
966
967 spin_lock_irqsave(&uap->port.lock, flags);
968 pl011_dma_rx_stop(uap);
Guennadi Liakhovetskic25a1ad2013-12-10 14:54:47 +0100969 uap->im |= UART011_RXIM;
970 writew(uap->im, uap->port.membase + UART011_IMSC);
Chanho Mincb06ff12013-03-27 18:38:11 +0900971 spin_unlock_irqrestore(&uap->port.lock, flags);
972
973 uap->dmarx.running = false;
974 dmaengine_terminate_all(rxchan);
975 del_timer(&uap->dmarx.timer);
976 } else {
977 mod_timer(&uap->dmarx.timer,
978 jiffies + msecs_to_jiffies(uap->dmarx.poll_rate));
979 }
980}
981
Russell King68b65f72010-12-22 17:24:39 +0000982static void pl011_dma_startup(struct uart_amba_port *uap)
983{
Linus Walleijead76f32011-02-24 13:21:08 +0100984 int ret;
985
Jorge Ramirez-Ortiz1c9be312015-03-06 13:05:40 -0500986 if (!uap->dma_probed)
987 pl011_dma_probe(uap);
988
Russell King68b65f72010-12-22 17:24:39 +0000989 if (!uap->dmatx.chan)
990 return;
991
Andrew Jackson4c0be452014-11-07 14:14:35 +0000992 uap->dmatx.buf = kmalloc(PL011_DMA_BUFFER_SIZE, GFP_KERNEL | __GFP_DMA);
Russell King68b65f72010-12-22 17:24:39 +0000993 if (!uap->dmatx.buf) {
994 dev_err(uap->port.dev, "no memory for DMA TX buffer\n");
995 uap->port.fifosize = uap->fifosize;
996 return;
997 }
998
999 sg_init_one(&uap->dmatx.sg, uap->dmatx.buf, PL011_DMA_BUFFER_SIZE);
1000
1001 /* The DMA buffer is now the FIFO the TTY subsystem can use */
1002 uap->port.fifosize = PL011_DMA_BUFFER_SIZE;
Linus Walleijead76f32011-02-24 13:21:08 +01001003 uap->using_tx_dma = true;
Russell King68b65f72010-12-22 17:24:39 +00001004
Linus Walleijead76f32011-02-24 13:21:08 +01001005 if (!uap->dmarx.chan)
1006 goto skip_rx;
1007
1008 /* Allocate and map DMA RX buffers */
1009 ret = pl011_sgbuf_init(uap->dmarx.chan, &uap->dmarx.sgbuf_a,
1010 DMA_FROM_DEVICE);
1011 if (ret) {
1012 dev_err(uap->port.dev, "failed to init DMA %s: %d\n",
1013 "RX buffer A", ret);
1014 goto skip_rx;
1015 }
1016
1017 ret = pl011_sgbuf_init(uap->dmarx.chan, &uap->dmarx.sgbuf_b,
1018 DMA_FROM_DEVICE);
1019 if (ret) {
1020 dev_err(uap->port.dev, "failed to init DMA %s: %d\n",
1021 "RX buffer B", ret);
1022 pl011_sgbuf_free(uap->dmarx.chan, &uap->dmarx.sgbuf_a,
1023 DMA_FROM_DEVICE);
1024 goto skip_rx;
1025 }
1026
1027 uap->using_rx_dma = true;
1028
1029skip_rx:
Russell King68b65f72010-12-22 17:24:39 +00001030 /* Turn on DMA error (RX/TX will be enabled on demand) */
1031 uap->dmacr |= UART011_DMAONERR;
1032 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
Russell King38d62432010-12-22 17:59:16 +00001033
1034 /*
1035 * ST Micro variants has some specific dma burst threshold
1036 * compensation. Set this to 16 bytes, so burst will only
1037 * be issued above/below 16 bytes.
1038 */
1039 if (uap->vendor->dma_threshold)
1040 writew(ST_UART011_DMAWM_RX_16 | ST_UART011_DMAWM_TX_16,
1041 uap->port.membase + ST_UART011_DMAWM);
Linus Walleijead76f32011-02-24 13:21:08 +01001042
1043 if (uap->using_rx_dma) {
1044 if (pl011_dma_rx_trigger_dma(uap))
1045 dev_dbg(uap->port.dev, "could not trigger initial "
1046 "RX DMA job, fall back to interrupt mode\n");
Chanho Mincb06ff12013-03-27 18:38:11 +09001047 if (uap->dmarx.poll_rate) {
1048 init_timer(&(uap->dmarx.timer));
1049 uap->dmarx.timer.function = pl011_dma_rx_poll;
1050 uap->dmarx.timer.data = (unsigned long)uap;
1051 mod_timer(&uap->dmarx.timer,
1052 jiffies +
1053 msecs_to_jiffies(uap->dmarx.poll_rate));
1054 uap->dmarx.last_residue = PL011_DMA_BUFFER_SIZE;
1055 uap->dmarx.last_jiffies = jiffies;
1056 }
Linus Walleijead76f32011-02-24 13:21:08 +01001057 }
Russell King68b65f72010-12-22 17:24:39 +00001058}
1059
1060static void pl011_dma_shutdown(struct uart_amba_port *uap)
1061{
Linus Walleijead76f32011-02-24 13:21:08 +01001062 if (!(uap->using_tx_dma || uap->using_rx_dma))
Russell King68b65f72010-12-22 17:24:39 +00001063 return;
1064
1065 /* Disable RX and TX DMA */
1066 while (readw(uap->port.membase + UART01x_FR) & UART01x_FR_BUSY)
1067 barrier();
1068
1069 spin_lock_irq(&uap->port.lock);
1070 uap->dmacr &= ~(UART011_DMAONERR | UART011_RXDMAE | UART011_TXDMAE);
1071 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
1072 spin_unlock_irq(&uap->port.lock);
1073
Linus Walleijead76f32011-02-24 13:21:08 +01001074 if (uap->using_tx_dma) {
1075 /* In theory, this should already be done by pl011_dma_flush_buffer */
1076 dmaengine_terminate_all(uap->dmatx.chan);
1077 if (uap->dmatx.queued) {
1078 dma_unmap_sg(uap->dmatx.chan->device->dev, &uap->dmatx.sg, 1,
1079 DMA_TO_DEVICE);
1080 uap->dmatx.queued = false;
1081 }
1082
1083 kfree(uap->dmatx.buf);
1084 uap->using_tx_dma = false;
Russell King68b65f72010-12-22 17:24:39 +00001085 }
1086
Linus Walleijead76f32011-02-24 13:21:08 +01001087 if (uap->using_rx_dma) {
1088 dmaengine_terminate_all(uap->dmarx.chan);
1089 /* Clean up the RX DMA */
1090 pl011_sgbuf_free(uap->dmarx.chan, &uap->dmarx.sgbuf_a, DMA_FROM_DEVICE);
1091 pl011_sgbuf_free(uap->dmarx.chan, &uap->dmarx.sgbuf_b, DMA_FROM_DEVICE);
Chanho Mincb06ff12013-03-27 18:38:11 +09001092 if (uap->dmarx.poll_rate)
1093 del_timer_sync(&uap->dmarx.timer);
Linus Walleijead76f32011-02-24 13:21:08 +01001094 uap->using_rx_dma = false;
1095 }
Russell King68b65f72010-12-22 17:24:39 +00001096}
1097
Linus Walleijead76f32011-02-24 13:21:08 +01001098static inline bool pl011_dma_rx_available(struct uart_amba_port *uap)
1099{
1100 return uap->using_rx_dma;
1101}
1102
1103static inline bool pl011_dma_rx_running(struct uart_amba_port *uap)
1104{
1105 return uap->using_rx_dma && uap->dmarx.running;
1106}
1107
Russell King68b65f72010-12-22 17:24:39 +00001108#else
1109/* Blank functions if the DMA engine is not available */
Jorge Ramirez-Ortiz1c9be312015-03-06 13:05:40 -05001110static inline void pl011_dma_probe(struct uart_amba_port *uap)
Russell King68b65f72010-12-22 17:24:39 +00001111{
1112}
1113
1114static inline void pl011_dma_remove(struct uart_amba_port *uap)
1115{
1116}
1117
1118static inline void pl011_dma_startup(struct uart_amba_port *uap)
1119{
1120}
1121
1122static inline void pl011_dma_shutdown(struct uart_amba_port *uap)
1123{
1124}
1125
1126static inline bool pl011_dma_tx_irq(struct uart_amba_port *uap)
1127{
1128 return false;
1129}
1130
1131static inline void pl011_dma_tx_stop(struct uart_amba_port *uap)
1132{
1133}
1134
1135static inline bool pl011_dma_tx_start(struct uart_amba_port *uap)
1136{
1137 return false;
1138}
1139
Linus Walleijead76f32011-02-24 13:21:08 +01001140static inline void pl011_dma_rx_irq(struct uart_amba_port *uap)
1141{
1142}
1143
1144static inline void pl011_dma_rx_stop(struct uart_amba_port *uap)
1145{
1146}
1147
1148static inline int pl011_dma_rx_trigger_dma(struct uart_amba_port *uap)
1149{
1150 return -EIO;
1151}
1152
1153static inline bool pl011_dma_rx_available(struct uart_amba_port *uap)
1154{
1155 return false;
1156}
1157
1158static inline bool pl011_dma_rx_running(struct uart_amba_port *uap)
1159{
1160 return false;
1161}
1162
Russell King68b65f72010-12-22 17:24:39 +00001163#define pl011_dma_flush_buffer NULL
1164#endif
1165
Russell Kingb129a8c2005-08-31 10:12:14 +01001166static void pl011_stop_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001168 struct uart_amba_port *uap =
1169 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170
1171 uap->im &= ~UART011_TXIM;
1172 writew(uap->im, uap->port.membase + UART011_IMSC);
Russell King68b65f72010-12-22 17:24:39 +00001173 pl011_dma_tx_stop(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174}
1175
Dave Martin734745c2015-03-04 12:27:33 +00001176static bool pl011_tx_chars(struct uart_amba_port *uap);
1177
1178/* Start TX with programmed I/O only (no DMA) */
1179static void pl011_start_tx_pio(struct uart_amba_port *uap)
1180{
1181 uap->im |= UART011_TXIM;
1182 writew(uap->im, uap->port.membase + UART011_IMSC);
1183 if (!uap->tx_irq_seen)
1184 pl011_tx_chars(uap);
1185}
1186
Russell Kingb129a8c2005-08-31 10:12:14 +01001187static void pl011_start_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001189 struct uart_amba_port *uap =
1190 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191
Dave Martin734745c2015-03-04 12:27:33 +00001192 if (!pl011_dma_tx_start(uap))
1193 pl011_start_tx_pio(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194}
1195
1196static void pl011_stop_rx(struct uart_port *port)
1197{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001198 struct uart_amba_port *uap =
1199 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200
1201 uap->im &= ~(UART011_RXIM|UART011_RTIM|UART011_FEIM|
1202 UART011_PEIM|UART011_BEIM|UART011_OEIM);
1203 writew(uap->im, uap->port.membase + UART011_IMSC);
Linus Walleijead76f32011-02-24 13:21:08 +01001204
1205 pl011_dma_rx_stop(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206}
1207
1208static void pl011_enable_ms(struct uart_port *port)
1209{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001210 struct uart_amba_port *uap =
1211 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212
1213 uap->im |= UART011_RIMIM|UART011_CTSMIM|UART011_DCDMIM|UART011_DSRMIM;
1214 writew(uap->im, uap->port.membase + UART011_IMSC);
1215}
1216
David Howells7d12e782006-10-05 14:55:46 +01001217static void pl011_rx_chars(struct uart_amba_port *uap)
Fabio Estevamb83286b2013-08-09 17:58:51 -03001218__releases(&uap->port.lock)
1219__acquires(&uap->port.lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220{
Linus Walleij29772c42011-02-24 13:21:36 +01001221 pl011_fifo_to_tty(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222
Thomas Gleixner2389b272007-05-29 21:53:50 +01001223 spin_unlock(&uap->port.lock);
Jiri Slaby2e124b42013-01-03 15:53:06 +01001224 tty_flip_buffer_push(&uap->port.state->port);
Linus Walleijead76f32011-02-24 13:21:08 +01001225 /*
1226 * If we were temporarily out of DMA mode for a while,
1227 * attempt to switch back to DMA mode again.
1228 */
1229 if (pl011_dma_rx_available(uap)) {
1230 if (pl011_dma_rx_trigger_dma(uap)) {
1231 dev_dbg(uap->port.dev, "could not trigger RX DMA job "
1232 "fall back to interrupt mode again\n");
1233 uap->im |= UART011_RXIM;
Guennadi Liakhovetski30ae5852013-12-10 14:54:42 +01001234 writew(uap->im, uap->port.membase + UART011_IMSC);
Chanho Mincb06ff12013-03-27 18:38:11 +09001235 } else {
Chanho Min89fa28d2013-04-03 11:10:37 +09001236#ifdef CONFIG_DMA_ENGINE
Chanho Mincb06ff12013-03-27 18:38:11 +09001237 /* Start Rx DMA poll */
1238 if (uap->dmarx.poll_rate) {
1239 uap->dmarx.last_jiffies = jiffies;
1240 uap->dmarx.last_residue = PL011_DMA_BUFFER_SIZE;
1241 mod_timer(&uap->dmarx.timer,
1242 jiffies +
1243 msecs_to_jiffies(uap->dmarx.poll_rate));
1244 }
Chanho Min89fa28d2013-04-03 11:10:37 +09001245#endif
Chanho Mincb06ff12013-03-27 18:38:11 +09001246 }
Linus Walleijead76f32011-02-24 13:21:08 +01001247 }
Thomas Gleixner2389b272007-05-29 21:53:50 +01001248 spin_lock(&uap->port.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249}
1250
Dave Martin734745c2015-03-04 12:27:33 +00001251/*
1252 * Transmit a character
1253 * There must be at least one free entry in the TX FIFO to accept the char.
1254 *
1255 * Returns true if the FIFO might have space in it afterwards;
1256 * returns false if the FIFO definitely became full.
1257 */
1258static bool pl011_tx_char(struct uart_amba_port *uap, unsigned char c)
1259{
1260 writew(c, uap->port.membase + UART01x_DR);
1261 uap->port.icount.tx++;
1262
1263 if (likely(uap->tx_irq_seen > 1))
1264 return true;
1265
1266 return !(readw(uap->port.membase + UART01x_FR) & UART01x_FR_TXFF);
1267}
1268
1269static bool pl011_tx_chars(struct uart_amba_port *uap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270{
Alan Coxebd2c8f2009-09-19 13:13:28 -07001271 struct circ_buf *xmit = &uap->port.state->xmit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 int count;
1273
Dave Martin734745c2015-03-04 12:27:33 +00001274 if (unlikely(uap->tx_irq_seen < 2))
1275 /*
1276 * Initial FIFO fill level unknown: we must check TXFF
1277 * after each write, so just try to fill up the FIFO.
1278 */
1279 count = uap->fifosize;
1280 else /* tx_irq_seen >= 2 */
1281 /*
1282 * FIFO initially at least half-empty, so we can simply
1283 * write half the FIFO without polling TXFF.
1284
1285 * Note: the *first* TX IRQ can still race with
1286 * pl011_start_tx_pio(), which can result in the FIFO
1287 * being fuller than expected in that case.
1288 */
1289 count = uap->fifosize >> 1;
1290
1291 /*
1292 * If the FIFO is full we're guaranteed a TX IRQ at some later point,
1293 * and can't transmit immediately in any case:
1294 */
1295 if (unlikely(uap->tx_irq_seen < 2 &&
1296 readw(uap->port.membase + UART01x_FR) & UART01x_FR_TXFF))
1297 return false;
1298
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 if (uap->port.x_char) {
Dave Martin734745c2015-03-04 12:27:33 +00001300 pl011_tx_char(uap, uap->port.x_char);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 uap->port.x_char = 0;
Dave Martin734745c2015-03-04 12:27:33 +00001302 --count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 }
1304 if (uart_circ_empty(xmit) || uart_tx_stopped(&uap->port)) {
Russell Kingb129a8c2005-08-31 10:12:14 +01001305 pl011_stop_tx(&uap->port);
Dave Martin734745c2015-03-04 12:27:33 +00001306 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 }
1308
Russell King68b65f72010-12-22 17:24:39 +00001309 /* If we are using DMA mode, try to send some characters. */
1310 if (pl011_dma_tx_irq(uap))
Dave Martin734745c2015-03-04 12:27:33 +00001311 goto done;
Russell King68b65f72010-12-22 17:24:39 +00001312
Dave Martin734745c2015-03-04 12:27:33 +00001313 while (count-- > 0 && pl011_tx_char(uap, xmit->buf[xmit->tail])) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 if (uart_circ_empty(xmit))
1316 break;
Dave Martin734745c2015-03-04 12:27:33 +00001317 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318
1319 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
1320 uart_write_wakeup(&uap->port);
1321
Dave Martin734745c2015-03-04 12:27:33 +00001322 if (uart_circ_empty(xmit)) {
Russell Kingb129a8c2005-08-31 10:12:14 +01001323 pl011_stop_tx(&uap->port);
Dave Martin734745c2015-03-04 12:27:33 +00001324 goto done;
1325 }
1326
1327 if (unlikely(!uap->tx_irq_seen))
1328 schedule_delayed_work(&uap->tx_softirq_work, uap->port.timeout);
1329
1330done:
1331 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332}
1333
1334static void pl011_modem_status(struct uart_amba_port *uap)
1335{
1336 unsigned int status, delta;
1337
1338 status = readw(uap->port.membase + UART01x_FR) & UART01x_FR_MODEM_ANY;
1339
1340 delta = status ^ uap->old_status;
1341 uap->old_status = status;
1342
1343 if (!delta)
1344 return;
1345
1346 if (delta & UART01x_FR_DCD)
1347 uart_handle_dcd_change(&uap->port, status & UART01x_FR_DCD);
1348
1349 if (delta & UART01x_FR_DSR)
1350 uap->port.icount.dsr++;
1351
1352 if (delta & UART01x_FR_CTS)
1353 uart_handle_cts_change(&uap->port, status & UART01x_FR_CTS);
1354
Alan Coxbdc04e32009-09-19 13:13:31 -07001355 wake_up_interruptible(&uap->port.state->port.delta_msr_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356}
1357
Dave Martin734745c2015-03-04 12:27:33 +00001358static void pl011_tx_softirq(struct work_struct *work)
1359{
1360 struct delayed_work *dwork = to_delayed_work(work);
1361 struct uart_amba_port *uap =
1362 container_of(dwork, struct uart_amba_port, tx_softirq_work);
1363
1364 spin_lock(&uap->port.lock);
1365 while (pl011_tx_chars(uap)) ;
1366 spin_unlock(&uap->port.lock);
1367}
1368
1369static void pl011_tx_irq_seen(struct uart_amba_port *uap)
1370{
1371 if (likely(uap->tx_irq_seen > 1))
1372 return;
1373
1374 uap->tx_irq_seen++;
1375 if (uap->tx_irq_seen < 2)
1376 /* first TX IRQ */
1377 cancel_delayed_work(&uap->tx_softirq_work);
1378}
1379
David Howells7d12e782006-10-05 14:55:46 +01001380static irqreturn_t pl011_int(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381{
1382 struct uart_amba_port *uap = dev_id;
Russell King963cc982010-12-22 17:16:09 +00001383 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 unsigned int status, pass_counter = AMBA_ISR_PASS_LIMIT;
1385 int handled = 0;
Rajanikanth H.V4fd06902012-03-26 11:17:02 +02001386 unsigned int dummy_read;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387
Russell King963cc982010-12-22 17:16:09 +00001388 spin_lock_irqsave(&uap->port.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 status = readw(uap->port.membase + UART011_MIS);
1390 if (status) {
1391 do {
Rajanikanth H.V4fd06902012-03-26 11:17:02 +02001392 if (uap->vendor->cts_event_workaround) {
1393 /* workaround to make sure that all bits are unlocked.. */
1394 writew(0x00, uap->port.membase + UART011_ICR);
1395
1396 /*
1397 * WA: introduce 26ns(1 uart clk) delay before W1C;
1398 * single apb access will incur 2 pclk(133.12Mhz) delay,
1399 * so add 2 dummy reads
1400 */
1401 dummy_read = readw(uap->port.membase + UART011_ICR);
1402 dummy_read = readw(uap->port.membase + UART011_ICR);
1403 }
1404
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 writew(status & ~(UART011_TXIS|UART011_RTIS|
1406 UART011_RXIS),
1407 uap->port.membase + UART011_ICR);
1408
Linus Walleijead76f32011-02-24 13:21:08 +01001409 if (status & (UART011_RTIS|UART011_RXIS)) {
1410 if (pl011_dma_rx_running(uap))
1411 pl011_dma_rx_irq(uap);
1412 else
1413 pl011_rx_chars(uap);
1414 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 if (status & (UART011_DSRMIS|UART011_DCDMIS|
1416 UART011_CTSMIS|UART011_RIMIS))
1417 pl011_modem_status(uap);
Dave Martin734745c2015-03-04 12:27:33 +00001418 if (status & UART011_TXIS) {
1419 pl011_tx_irq_seen(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 pl011_tx_chars(uap);
Dave Martin734745c2015-03-04 12:27:33 +00001421 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422
Rajanikanth H.V4fd06902012-03-26 11:17:02 +02001423 if (pass_counter-- == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 break;
1425
1426 status = readw(uap->port.membase + UART011_MIS);
1427 } while (status != 0);
1428 handled = 1;
1429 }
1430
Russell King963cc982010-12-22 17:16:09 +00001431 spin_unlock_irqrestore(&uap->port.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432
1433 return IRQ_RETVAL(handled);
1434}
1435
Linus Walleije643f872012-06-17 15:44:19 +02001436static unsigned int pl011_tx_empty(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001438 struct uart_amba_port *uap =
1439 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 unsigned int status = readw(uap->port.membase + UART01x_FR);
1441 return status & (UART01x_FR_BUSY|UART01x_FR_TXFF) ? 0 : TIOCSER_TEMT;
1442}
1443
Linus Walleije643f872012-06-17 15:44:19 +02001444static unsigned int pl011_get_mctrl(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001446 struct uart_amba_port *uap =
1447 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 unsigned int result = 0;
1449 unsigned int status = readw(uap->port.membase + UART01x_FR);
1450
Jiri Slaby5159f402007-10-18 23:40:31 -07001451#define TIOCMBIT(uartbit, tiocmbit) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 if (status & uartbit) \
1453 result |= tiocmbit
1454
Jiri Slaby5159f402007-10-18 23:40:31 -07001455 TIOCMBIT(UART01x_FR_DCD, TIOCM_CAR);
1456 TIOCMBIT(UART01x_FR_DSR, TIOCM_DSR);
1457 TIOCMBIT(UART01x_FR_CTS, TIOCM_CTS);
1458 TIOCMBIT(UART011_FR_RI, TIOCM_RNG);
1459#undef TIOCMBIT
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 return result;
1461}
1462
1463static void pl011_set_mctrl(struct uart_port *port, unsigned int mctrl)
1464{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001465 struct uart_amba_port *uap =
1466 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 unsigned int cr;
1468
1469 cr = readw(uap->port.membase + UART011_CR);
1470
Jiri Slaby5159f402007-10-18 23:40:31 -07001471#define TIOCMBIT(tiocmbit, uartbit) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 if (mctrl & tiocmbit) \
1473 cr |= uartbit; \
1474 else \
1475 cr &= ~uartbit
1476
Jiri Slaby5159f402007-10-18 23:40:31 -07001477 TIOCMBIT(TIOCM_RTS, UART011_CR_RTS);
1478 TIOCMBIT(TIOCM_DTR, UART011_CR_DTR);
1479 TIOCMBIT(TIOCM_OUT1, UART011_CR_OUT1);
1480 TIOCMBIT(TIOCM_OUT2, UART011_CR_OUT2);
1481 TIOCMBIT(TIOCM_LOOP, UART011_CR_LBE);
Rabin Vincent3b438162010-02-12 06:43:11 +01001482
1483 if (uap->autorts) {
1484 /* We need to disable auto-RTS if we want to turn RTS off */
1485 TIOCMBIT(TIOCM_RTS, UART011_CR_RTSEN);
1486 }
Jiri Slaby5159f402007-10-18 23:40:31 -07001487#undef TIOCMBIT
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488
1489 writew(cr, uap->port.membase + UART011_CR);
1490}
1491
1492static void pl011_break_ctl(struct uart_port *port, int break_state)
1493{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001494 struct uart_amba_port *uap =
1495 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 unsigned long flags;
1497 unsigned int lcr_h;
1498
1499 spin_lock_irqsave(&uap->port.lock, flags);
Linus Walleijec489aa2010-06-02 08:13:52 +01001500 lcr_h = readw(uap->port.membase + uap->lcrh_tx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 if (break_state == -1)
1502 lcr_h |= UART01x_LCRH_BRK;
1503 else
1504 lcr_h &= ~UART01x_LCRH_BRK;
Linus Walleijec489aa2010-06-02 08:13:52 +01001505 writew(lcr_h, uap->port.membase + uap->lcrh_tx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 spin_unlock_irqrestore(&uap->port.lock, flags);
1507}
1508
Jason Wessel84b5ae12008-02-20 13:33:39 -06001509#ifdef CONFIG_CONSOLE_POLL
Anton Vorontsov5c8124a2012-09-24 14:27:55 -07001510
1511static void pl011_quiesce_irqs(struct uart_port *port)
1512{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001513 struct uart_amba_port *uap =
1514 container_of(port, struct uart_amba_port, port);
Anton Vorontsov5c8124a2012-09-24 14:27:55 -07001515 unsigned char __iomem *regs = uap->port.membase;
1516
1517 writew(readw(regs + UART011_MIS), regs + UART011_ICR);
1518 /*
1519 * There is no way to clear TXIM as this is "ready to transmit IRQ", so
1520 * we simply mask it. start_tx() will unmask it.
1521 *
1522 * Note we can race with start_tx(), and if the race happens, the
1523 * polling user might get another interrupt just after we clear it.
1524 * But it should be OK and can happen even w/o the race, e.g.
1525 * controller immediately got some new data and raised the IRQ.
1526 *
1527 * And whoever uses polling routines assumes that it manages the device
1528 * (including tx queue), so we're also fine with start_tx()'s caller
1529 * side.
1530 */
1531 writew(readw(regs + UART011_IMSC) & ~UART011_TXIM, regs + UART011_IMSC);
1532}
1533
Linus Walleije643f872012-06-17 15:44:19 +02001534static int pl011_get_poll_char(struct uart_port *port)
Jason Wessel84b5ae12008-02-20 13:33:39 -06001535{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001536 struct uart_amba_port *uap =
1537 container_of(port, struct uart_amba_port, port);
Jason Wessel84b5ae12008-02-20 13:33:39 -06001538 unsigned int status;
1539
Anton Vorontsov5c8124a2012-09-24 14:27:55 -07001540 /*
1541 * The caller might need IRQs lowered, e.g. if used with KDB NMI
1542 * debugger.
1543 */
1544 pl011_quiesce_irqs(port);
1545
Jason Wesself5316b42010-05-20 21:04:22 -05001546 status = readw(uap->port.membase + UART01x_FR);
1547 if (status & UART01x_FR_RXFE)
1548 return NO_POLL_CHAR;
Jason Wessel84b5ae12008-02-20 13:33:39 -06001549
1550 return readw(uap->port.membase + UART01x_DR);
1551}
1552
Linus Walleije643f872012-06-17 15:44:19 +02001553static void pl011_put_poll_char(struct uart_port *port,
Jason Wessel84b5ae12008-02-20 13:33:39 -06001554 unsigned char ch)
1555{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001556 struct uart_amba_port *uap =
1557 container_of(port, struct uart_amba_port, port);
Jason Wessel84b5ae12008-02-20 13:33:39 -06001558
1559 while (readw(uap->port.membase + UART01x_FR) & UART01x_FR_TXFF)
1560 barrier();
1561
1562 writew(ch, uap->port.membase + UART01x_DR);
1563}
1564
1565#endif /* CONFIG_CONSOLE_POLL */
1566
Anton Vorontsovb3564c22012-09-24 14:27:54 -07001567static int pl011_hwinit(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001569 struct uart_amba_port *uap =
1570 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 int retval;
1572
Linus Walleij78d80c52012-05-23 21:18:46 +02001573 /* Optionaly enable pins to be muxed in and configured */
Linus Walleij2b996fc2013-06-05 15:36:42 +02001574 pinctrl_pm_select_default_state(port->dev);
Linus Walleij78d80c52012-05-23 21:18:46 +02001575
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 /*
1577 * Try to enable the clock producer.
1578 */
Julia Lawall1c4c4392012-08-26 18:01:01 +02001579 retval = clk_prepare_enable(uap->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 if (retval)
Tushar Behera7f6d9422014-06-26 15:35:35 +05301581 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582
1583 uap->port.uartclk = clk_get_rate(uap->clk);
1584
Linus Walleij9b96fba2012-03-13 13:27:23 +01001585 /* Clear pending error and receive interrupts */
1586 writew(UART011_OEIS | UART011_BEIS | UART011_PEIS | UART011_FEIS |
1587 UART011_RTIS | UART011_RXIS, uap->port.membase + UART011_ICR);
1588
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 /*
Anton Vorontsovb3564c22012-09-24 14:27:54 -07001590 * Save interrupts enable mask, and enable RX interrupts in case if
1591 * the interrupt is used for NMI entry.
1592 */
1593 uap->im = readw(uap->port.membase + UART011_IMSC);
1594 writew(UART011_RTIM | UART011_RXIM, uap->port.membase + UART011_IMSC);
1595
Jingoo Han574de552013-07-30 17:06:57 +09001596 if (dev_get_platdata(uap->port.dev)) {
Anton Vorontsovb3564c22012-09-24 14:27:54 -07001597 struct amba_pl011_data *plat;
1598
Jingoo Han574de552013-07-30 17:06:57 +09001599 plat = dev_get_platdata(uap->port.dev);
Anton Vorontsovb3564c22012-09-24 14:27:54 -07001600 if (plat->init)
1601 plat->init();
1602 }
1603 return 0;
Anton Vorontsovb3564c22012-09-24 14:27:54 -07001604}
1605
Jon Medhurstb60f2f62013-12-10 10:18:59 +00001606static void pl011_write_lcr_h(struct uart_amba_port *uap, unsigned int lcr_h)
1607{
1608 writew(lcr_h, uap->port.membase + uap->lcrh_rx);
1609 if (uap->lcrh_rx != uap->lcrh_tx) {
1610 int i;
1611 /*
1612 * Wait 10 PCLKs before writing LCRH_TX register,
1613 * to get this delay write read only register 10 times
1614 */
1615 for (i = 0; i < 10; ++i)
1616 writew(0xff, uap->port.membase + UART011_MIS);
1617 writew(lcr_h, uap->port.membase + uap->lcrh_tx);
1618 }
1619}
1620
Anton Vorontsovb3564c22012-09-24 14:27:54 -07001621static int pl011_startup(struct uart_port *port)
1622{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001623 struct uart_amba_port *uap =
1624 container_of(port, struct uart_amba_port, port);
Dave Martin734745c2015-03-04 12:27:33 +00001625 unsigned int cr;
Anton Vorontsovb3564c22012-09-24 14:27:54 -07001626 int retval;
1627
1628 retval = pl011_hwinit(port);
1629 if (retval)
1630 goto clk_dis;
1631
1632 writew(uap->im, uap->port.membase + UART011_IMSC);
1633
1634 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 * Allocate the IRQ
1636 */
1637 retval = request_irq(uap->port.irq, pl011_int, 0, "uart-pl011", uap);
1638 if (retval)
1639 goto clk_dis;
1640
Russell Kingc19f12b2010-12-22 17:48:26 +00001641 writew(uap->vendor->ifls, uap->port.membase + UART011_IFLS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642
Jon Medhurstfe433902013-12-10 10:18:58 +00001643 spin_lock_irq(&uap->port.lock);
1644
Shreshtha Kumar Sahud8d8ffa2012-01-18 15:53:59 +05301645 /* restore RTS and DTR */
1646 cr = uap->old_cr & (UART011_CR_RTS | UART011_CR_DTR);
1647 cr |= UART01x_CR_UARTEN | UART011_CR_RXE | UART011_CR_TXE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 writew(cr, uap->port.membase + UART011_CR);
1649
Jon Medhurstfe433902013-12-10 10:18:58 +00001650 spin_unlock_irq(&uap->port.lock);
1651
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 /*
1653 * initialise the old status of the modem signals
1654 */
1655 uap->old_status = readw(uap->port.membase + UART01x_FR) & UART01x_FR_MODEM_ANY;
1656
Russell King68b65f72010-12-22 17:24:39 +00001657 /* Startup DMA */
1658 pl011_dma_startup(uap);
1659
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 /*
Linus Walleijead76f32011-02-24 13:21:08 +01001661 * Finally, enable interrupts, only timeouts when using DMA
1662 * if initial RX DMA job failed, start in interrupt mode
1663 * as well.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 */
1665 spin_lock_irq(&uap->port.lock);
Linus Walleij9b96fba2012-03-13 13:27:23 +01001666 /* Clear out any spuriously appearing RX interrupts */
1667 writew(UART011_RTIS | UART011_RXIS,
1668 uap->port.membase + UART011_ICR);
Linus Walleijead76f32011-02-24 13:21:08 +01001669 uap->im = UART011_RTIM;
1670 if (!pl011_dma_rx_running(uap))
1671 uap->im |= UART011_RXIM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 writew(uap->im, uap->port.membase + UART011_IMSC);
1673 spin_unlock_irq(&uap->port.lock);
1674
1675 return 0;
1676
1677 clk_dis:
Julia Lawall1c4c4392012-08-26 18:01:01 +02001678 clk_disable_unprepare(uap->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 return retval;
1680}
1681
Linus Walleijec489aa2010-06-02 08:13:52 +01001682static void pl011_shutdown_channel(struct uart_amba_port *uap,
1683 unsigned int lcrh)
1684{
1685 unsigned long val;
1686
1687 val = readw(uap->port.membase + lcrh);
1688 val &= ~(UART01x_LCRH_BRK | UART01x_LCRH_FEN);
1689 writew(val, uap->port.membase + lcrh);
1690}
1691
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692static void pl011_shutdown(struct uart_port *port)
1693{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001694 struct uart_amba_port *uap =
1695 container_of(port, struct uart_amba_port, port);
Shreshtha Kumar Sahud8d8ffa2012-01-18 15:53:59 +05301696 unsigned int cr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697
Dave Martin734745c2015-03-04 12:27:33 +00001698 cancel_delayed_work_sync(&uap->tx_softirq_work);
1699
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 /*
1701 * disable all interrupts
1702 */
1703 spin_lock_irq(&uap->port.lock);
1704 uap->im = 0;
1705 writew(uap->im, uap->port.membase + UART011_IMSC);
Dave Martinf2ee6df2015-03-04 12:27:34 +00001706 writew(0xffff & ~UART011_TXIS, uap->port.membase + UART011_ICR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 spin_unlock_irq(&uap->port.lock);
1708
Russell King68b65f72010-12-22 17:24:39 +00001709 pl011_dma_shutdown(uap);
1710
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 /*
1712 * Free the interrupt
1713 */
1714 free_irq(uap->port.irq, uap);
1715
1716 /*
1717 * disable the port
Shreshtha Kumar Sahud8d8ffa2012-01-18 15:53:59 +05301718 * disable the port. It should not disable RTS and DTR.
1719 * Also RTS and DTR state should be preserved to restore
1720 * it during startup().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 */
Rabin Vincent3b438162010-02-12 06:43:11 +01001722 uap->autorts = false;
Jon Medhurstfe433902013-12-10 10:18:58 +00001723 spin_lock_irq(&uap->port.lock);
Shreshtha Kumar Sahud8d8ffa2012-01-18 15:53:59 +05301724 cr = readw(uap->port.membase + UART011_CR);
1725 uap->old_cr = cr;
1726 cr &= UART011_CR_RTS | UART011_CR_DTR;
1727 cr |= UART01x_CR_UARTEN | UART011_CR_TXE;
1728 writew(cr, uap->port.membase + UART011_CR);
Jon Medhurstfe433902013-12-10 10:18:58 +00001729 spin_unlock_irq(&uap->port.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730
1731 /*
1732 * disable break condition and fifos
1733 */
Linus Walleijec489aa2010-06-02 08:13:52 +01001734 pl011_shutdown_channel(uap, uap->lcrh_rx);
1735 if (uap->lcrh_rx != uap->lcrh_tx)
1736 pl011_shutdown_channel(uap, uap->lcrh_tx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737
1738 /*
1739 * Shut down the clock producer
1740 */
Julia Lawall1c4c4392012-08-26 18:01:01 +02001741 clk_disable_unprepare(uap->clk);
Linus Walleij78d80c52012-05-23 21:18:46 +02001742 /* Optionally let pins go into sleep states */
Linus Walleij2b996fc2013-06-05 15:36:42 +02001743 pinctrl_pm_select_sleep_state(port->dev);
Shreshtha Kumar Sahuc16d51a2011-06-13 10:11:33 +02001744
Jingoo Han574de552013-07-30 17:06:57 +09001745 if (dev_get_platdata(uap->port.dev)) {
Shreshtha Kumar Sahuc16d51a2011-06-13 10:11:33 +02001746 struct amba_pl011_data *plat;
1747
Jingoo Han574de552013-07-30 17:06:57 +09001748 plat = dev_get_platdata(uap->port.dev);
Shreshtha Kumar Sahuc16d51a2011-06-13 10:11:33 +02001749 if (plat->exit)
1750 plat->exit();
1751 }
1752
Peter Hurley36f339d2014-11-06 09:06:12 -05001753 if (uap->port.ops->flush_buffer)
1754 uap->port.ops->flush_buffer(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755}
1756
1757static void
Alan Cox606d0992006-12-08 02:38:45 -08001758pl011_set_termios(struct uart_port *port, struct ktermios *termios,
1759 struct ktermios *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001761 struct uart_amba_port *uap =
1762 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 unsigned int lcr_h, old_cr;
1764 unsigned long flags;
Russell Kingc19f12b2010-12-22 17:48:26 +00001765 unsigned int baud, quot, clkdiv;
1766
1767 if (uap->vendor->oversampling)
1768 clkdiv = 8;
1769 else
1770 clkdiv = 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771
1772 /*
1773 * Ask the core to calculate the divisor for us.
1774 */
Linus Walleijac3e3fb2010-06-02 20:40:22 +01001775 baud = uart_get_baud_rate(port, termios, old, 0,
Russell Kingc19f12b2010-12-22 17:48:26 +00001776 port->uartclk / clkdiv);
Chanho Min89fa28d2013-04-03 11:10:37 +09001777#ifdef CONFIG_DMA_ENGINE
Chanho Mincb06ff12013-03-27 18:38:11 +09001778 /*
1779 * Adjust RX DMA polling rate with baud rate if not specified.
1780 */
1781 if (uap->dmarx.auto_poll_rate)
1782 uap->dmarx.poll_rate = DIV_ROUND_UP(10000000, baud);
Chanho Min89fa28d2013-04-03 11:10:37 +09001783#endif
Linus Walleijac3e3fb2010-06-02 20:40:22 +01001784
1785 if (baud > port->uartclk/16)
1786 quot = DIV_ROUND_CLOSEST(port->uartclk * 8, baud);
1787 else
1788 quot = DIV_ROUND_CLOSEST(port->uartclk * 4, baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789
1790 switch (termios->c_cflag & CSIZE) {
1791 case CS5:
1792 lcr_h = UART01x_LCRH_WLEN_5;
1793 break;
1794 case CS6:
1795 lcr_h = UART01x_LCRH_WLEN_6;
1796 break;
1797 case CS7:
1798 lcr_h = UART01x_LCRH_WLEN_7;
1799 break;
1800 default: // CS8
1801 lcr_h = UART01x_LCRH_WLEN_8;
1802 break;
1803 }
1804 if (termios->c_cflag & CSTOPB)
1805 lcr_h |= UART01x_LCRH_STP2;
1806 if (termios->c_cflag & PARENB) {
1807 lcr_h |= UART01x_LCRH_PEN;
1808 if (!(termios->c_cflag & PARODD))
1809 lcr_h |= UART01x_LCRH_EPS;
1810 }
Russell Kingffca2b12010-12-22 17:13:05 +00001811 if (uap->fifosize > 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 lcr_h |= UART01x_LCRH_FEN;
1813
1814 spin_lock_irqsave(&port->lock, flags);
1815
1816 /*
1817 * Update the per-port timeout.
1818 */
1819 uart_update_timeout(port, termios->c_cflag, baud);
1820
Russell Kingb63d4f02005-11-19 11:10:35 +00001821 port->read_status_mask = UART011_DR_OE | 255;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 if (termios->c_iflag & INPCK)
Russell Kingb63d4f02005-11-19 11:10:35 +00001823 port->read_status_mask |= UART011_DR_FE | UART011_DR_PE;
Peter Hurleyef8b9dd2014-06-16 08:10:41 -04001824 if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
Russell Kingb63d4f02005-11-19 11:10:35 +00001825 port->read_status_mask |= UART011_DR_BE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826
1827 /*
1828 * Characters to ignore
1829 */
1830 port->ignore_status_mask = 0;
1831 if (termios->c_iflag & IGNPAR)
Russell Kingb63d4f02005-11-19 11:10:35 +00001832 port->ignore_status_mask |= UART011_DR_FE | UART011_DR_PE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 if (termios->c_iflag & IGNBRK) {
Russell Kingb63d4f02005-11-19 11:10:35 +00001834 port->ignore_status_mask |= UART011_DR_BE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 /*
1836 * If we're ignoring parity and break indicators,
1837 * ignore overruns too (for real raw support).
1838 */
1839 if (termios->c_iflag & IGNPAR)
Russell Kingb63d4f02005-11-19 11:10:35 +00001840 port->ignore_status_mask |= UART011_DR_OE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 }
1842
1843 /*
1844 * Ignore all characters if CREAD is not set.
1845 */
1846 if ((termios->c_cflag & CREAD) == 0)
Russell Kingb63d4f02005-11-19 11:10:35 +00001847 port->ignore_status_mask |= UART_DUMMY_DR_RX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848
1849 if (UART_ENABLE_MS(port, termios->c_cflag))
1850 pl011_enable_ms(port);
1851
1852 /* first, disable everything */
1853 old_cr = readw(port->membase + UART011_CR);
1854 writew(0, port->membase + UART011_CR);
1855
Rabin Vincent3b438162010-02-12 06:43:11 +01001856 if (termios->c_cflag & CRTSCTS) {
1857 if (old_cr & UART011_CR_RTS)
1858 old_cr |= UART011_CR_RTSEN;
1859
1860 old_cr |= UART011_CR_CTSEN;
1861 uap->autorts = true;
1862 } else {
1863 old_cr &= ~(UART011_CR_CTSEN | UART011_CR_RTSEN);
1864 uap->autorts = false;
1865 }
1866
Russell Kingc19f12b2010-12-22 17:48:26 +00001867 if (uap->vendor->oversampling) {
1868 if (baud > port->uartclk / 16)
Linus Walleijac3e3fb2010-06-02 20:40:22 +01001869 old_cr |= ST_UART011_CR_OVSFACT;
1870 else
1871 old_cr &= ~ST_UART011_CR_OVSFACT;
1872 }
1873
Linus Walleijc5dd5532012-09-26 17:21:36 +02001874 /*
1875 * Workaround for the ST Micro oversampling variants to
1876 * increase the bitrate slightly, by lowering the divisor,
1877 * to avoid delayed sampling of start bit at high speeds,
1878 * else we see data corruption.
1879 */
1880 if (uap->vendor->oversampling) {
1881 if ((baud >= 3000000) && (baud < 3250000) && (quot > 1))
1882 quot -= 1;
1883 else if ((baud > 3250000) && (quot > 2))
1884 quot -= 2;
1885 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 /* Set baud rate */
1887 writew(quot & 0x3f, port->membase + UART011_FBRD);
1888 writew(quot >> 6, port->membase + UART011_IBRD);
1889
1890 /*
1891 * ----------v----------v----------v----------v-----
Linus Walleijc5dd5532012-09-26 17:21:36 +02001892 * NOTE: lcrh_tx and lcrh_rx MUST BE WRITTEN AFTER
1893 * UART011_FBRD & UART011_IBRD.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 * ----------^----------^----------^----------^-----
1895 */
Jon Medhurstb60f2f62013-12-10 10:18:59 +00001896 pl011_write_lcr_h(uap, lcr_h);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 writew(old_cr, port->membase + UART011_CR);
1898
1899 spin_unlock_irqrestore(&port->lock, flags);
1900}
1901
1902static const char *pl011_type(struct uart_port *port)
1903{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001904 struct uart_amba_port *uap =
1905 container_of(port, struct uart_amba_port, port);
Russell Kinge8a7ba82010-12-28 09:16:54 +00001906 return uap->port.type == PORT_AMBA ? uap->type : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907}
1908
1909/*
1910 * Release the memory region(s) being used by 'port'
1911 */
Linus Walleije643f872012-06-17 15:44:19 +02001912static void pl011_release_port(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913{
1914 release_mem_region(port->mapbase, SZ_4K);
1915}
1916
1917/*
1918 * Request the memory region(s) being used by 'port'
1919 */
Linus Walleije643f872012-06-17 15:44:19 +02001920static int pl011_request_port(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921{
1922 return request_mem_region(port->mapbase, SZ_4K, "uart-pl011")
1923 != NULL ? 0 : -EBUSY;
1924}
1925
1926/*
1927 * Configure/autoconfigure the port.
1928 */
Linus Walleije643f872012-06-17 15:44:19 +02001929static void pl011_config_port(struct uart_port *port, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930{
1931 if (flags & UART_CONFIG_TYPE) {
1932 port->type = PORT_AMBA;
Linus Walleije643f872012-06-17 15:44:19 +02001933 pl011_request_port(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 }
1935}
1936
1937/*
1938 * verify the new serial_struct (for TIOCSSERIAL).
1939 */
Linus Walleije643f872012-06-17 15:44:19 +02001940static int pl011_verify_port(struct uart_port *port, struct serial_struct *ser)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941{
1942 int ret = 0;
1943 if (ser->type != PORT_UNKNOWN && ser->type != PORT_AMBA)
1944 ret = -EINVAL;
Yinghai Lua62c4132008-08-19 20:49:55 -07001945 if (ser->irq < 0 || ser->irq >= nr_irqs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 ret = -EINVAL;
1947 if (ser->baud_base < 9600)
1948 ret = -EINVAL;
1949 return ret;
1950}
1951
1952static struct uart_ops amba_pl011_pops = {
Linus Walleije643f872012-06-17 15:44:19 +02001953 .tx_empty = pl011_tx_empty,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 .set_mctrl = pl011_set_mctrl,
Linus Walleije643f872012-06-17 15:44:19 +02001955 .get_mctrl = pl011_get_mctrl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 .stop_tx = pl011_stop_tx,
1957 .start_tx = pl011_start_tx,
1958 .stop_rx = pl011_stop_rx,
1959 .enable_ms = pl011_enable_ms,
1960 .break_ctl = pl011_break_ctl,
1961 .startup = pl011_startup,
1962 .shutdown = pl011_shutdown,
Russell King68b65f72010-12-22 17:24:39 +00001963 .flush_buffer = pl011_dma_flush_buffer,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 .set_termios = pl011_set_termios,
1965 .type = pl011_type,
Linus Walleije643f872012-06-17 15:44:19 +02001966 .release_port = pl011_release_port,
1967 .request_port = pl011_request_port,
1968 .config_port = pl011_config_port,
1969 .verify_port = pl011_verify_port,
Jason Wessel84b5ae12008-02-20 13:33:39 -06001970#ifdef CONFIG_CONSOLE_POLL
Anton Vorontsovb3564c22012-09-24 14:27:54 -07001971 .poll_init = pl011_hwinit,
Linus Walleije643f872012-06-17 15:44:19 +02001972 .poll_get_char = pl011_get_poll_char,
1973 .poll_put_char = pl011_put_poll_char,
Jason Wessel84b5ae12008-02-20 13:33:39 -06001974#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975};
1976
1977static struct uart_amba_port *amba_ports[UART_NR];
1978
1979#ifdef CONFIG_SERIAL_AMBA_PL011_CONSOLE
1980
Russell Kingd3587882006-03-20 20:00:09 +00001981static void pl011_console_putchar(struct uart_port *port, int ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001983 struct uart_amba_port *uap =
1984 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985
Russell Kingd3587882006-03-20 20:00:09 +00001986 while (readw(uap->port.membase + UART01x_FR) & UART01x_FR_TXFF)
1987 barrier();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 writew(ch, uap->port.membase + UART01x_DR);
1989}
1990
1991static void
1992pl011_console_write(struct console *co, const char *s, unsigned int count)
1993{
1994 struct uart_amba_port *uap = amba_ports[co->index];
1995 unsigned int status, old_cr, new_cr;
Rabin Vincentef605fd2012-01-17 11:52:28 +01001996 unsigned long flags;
1997 int locked = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998
1999 clk_enable(uap->clk);
2000
Rabin Vincentef605fd2012-01-17 11:52:28 +01002001 local_irq_save(flags);
2002 if (uap->port.sysrq)
2003 locked = 0;
2004 else if (oops_in_progress)
2005 locked = spin_trylock(&uap->port.lock);
2006 else
2007 spin_lock(&uap->port.lock);
2008
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 /*
2010 * First save the CR then disable the interrupts
2011 */
2012 old_cr = readw(uap->port.membase + UART011_CR);
2013 new_cr = old_cr & ~UART011_CR_CTSEN;
2014 new_cr |= UART01x_CR_UARTEN | UART011_CR_TXE;
2015 writew(new_cr, uap->port.membase + UART011_CR);
2016
Russell Kingd3587882006-03-20 20:00:09 +00002017 uart_console_write(&uap->port, s, count, pl011_console_putchar);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018
2019 /*
2020 * Finally, wait for transmitter to become empty
2021 * and restore the TCR
2022 */
2023 do {
2024 status = readw(uap->port.membase + UART01x_FR);
2025 } while (status & UART01x_FR_BUSY);
2026 writew(old_cr, uap->port.membase + UART011_CR);
2027
Rabin Vincentef605fd2012-01-17 11:52:28 +01002028 if (locked)
2029 spin_unlock(&uap->port.lock);
2030 local_irq_restore(flags);
2031
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 clk_disable(uap->clk);
2033}
2034
2035static void __init
2036pl011_console_get_options(struct uart_amba_port *uap, int *baud,
2037 int *parity, int *bits)
2038{
2039 if (readw(uap->port.membase + UART011_CR) & UART01x_CR_UARTEN) {
2040 unsigned int lcr_h, ibrd, fbrd;
2041
Linus Walleijec489aa2010-06-02 08:13:52 +01002042 lcr_h = readw(uap->port.membase + uap->lcrh_tx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043
2044 *parity = 'n';
2045 if (lcr_h & UART01x_LCRH_PEN) {
2046 if (lcr_h & UART01x_LCRH_EPS)
2047 *parity = 'e';
2048 else
2049 *parity = 'o';
2050 }
2051
2052 if ((lcr_h & 0x60) == UART01x_LCRH_WLEN_7)
2053 *bits = 7;
2054 else
2055 *bits = 8;
2056
2057 ibrd = readw(uap->port.membase + UART011_IBRD);
2058 fbrd = readw(uap->port.membase + UART011_FBRD);
2059
2060 *baud = uap->port.uartclk * 4 / (64 * ibrd + fbrd);
Linus Walleijac3e3fb2010-06-02 20:40:22 +01002061
Russell Kingc19f12b2010-12-22 17:48:26 +00002062 if (uap->vendor->oversampling) {
Linus Walleijac3e3fb2010-06-02 20:40:22 +01002063 if (readw(uap->port.membase + UART011_CR)
2064 & ST_UART011_CR_OVSFACT)
2065 *baud *= 2;
2066 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 }
2068}
2069
2070static int __init pl011_console_setup(struct console *co, char *options)
2071{
2072 struct uart_amba_port *uap;
2073 int baud = 38400;
2074 int bits = 8;
2075 int parity = 'n';
2076 int flow = 'n';
Russell King4b4851c2011-09-22 11:35:30 +01002077 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078
2079 /*
2080 * Check whether an invalid uart number has been specified, and
2081 * if so, search for the first available port that does have
2082 * console support.
2083 */
2084 if (co->index >= UART_NR)
2085 co->index = 0;
2086 uap = amba_ports[co->index];
Russell Kingd28122a2007-01-22 18:59:42 +00002087 if (!uap)
2088 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089
Linus Walleij78d80c52012-05-23 21:18:46 +02002090 /* Allow pins to be muxed in and configured */
Linus Walleij2b996fc2013-06-05 15:36:42 +02002091 pinctrl_pm_select_default_state(uap->port.dev);
Linus Walleij78d80c52012-05-23 21:18:46 +02002092
Russell King4b4851c2011-09-22 11:35:30 +01002093 ret = clk_prepare(uap->clk);
2094 if (ret)
2095 return ret;
2096
Jingoo Han574de552013-07-30 17:06:57 +09002097 if (dev_get_platdata(uap->port.dev)) {
Shreshtha Kumar Sahuc16d51a2011-06-13 10:11:33 +02002098 struct amba_pl011_data *plat;
2099
Jingoo Han574de552013-07-30 17:06:57 +09002100 plat = dev_get_platdata(uap->port.dev);
Shreshtha Kumar Sahuc16d51a2011-06-13 10:11:33 +02002101 if (plat->init)
2102 plat->init();
2103 }
2104
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 uap->port.uartclk = clk_get_rate(uap->clk);
2106
2107 if (options)
2108 uart_parse_options(options, &baud, &parity, &bits, &flow);
2109 else
2110 pl011_console_get_options(uap, &baud, &parity, &bits);
2111
2112 return uart_set_options(&uap->port, co, baud, parity, bits, flow);
2113}
2114
Vincent Sanders2d934862005-09-14 22:36:03 +01002115static struct uart_driver amba_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116static struct console amba_console = {
2117 .name = "ttyAMA",
2118 .write = pl011_console_write,
2119 .device = uart_console_device,
2120 .setup = pl011_console_setup,
2121 .flags = CON_PRINTBUFFER,
2122 .index = -1,
2123 .data = &amba_reg,
2124};
2125
2126#define AMBA_CONSOLE (&amba_console)
Rob Herring0d3c6732014-04-18 17:19:57 -05002127
2128static void pl011_putc(struct uart_port *port, int c)
2129{
2130 while (readl(port->membase + UART01x_FR) & UART01x_FR_TXFF)
2131 ;
2132 writeb(c, port->membase + UART01x_DR);
2133 while (readl(port->membase + UART01x_FR) & UART01x_FR_BUSY)
2134 ;
2135}
2136
2137static void pl011_early_write(struct console *con, const char *s, unsigned n)
2138{
2139 struct earlycon_device *dev = con->data;
2140
2141 uart_console_write(&dev->port, s, n, pl011_putc);
2142}
2143
2144static int __init pl011_early_console_setup(struct earlycon_device *device,
2145 const char *opt)
2146{
2147 if (!device->port.membase)
2148 return -ENODEV;
2149
2150 device->con->write = pl011_early_write;
2151 return 0;
2152}
2153EARLYCON_DECLARE(pl011, pl011_early_console_setup);
Rob Herring45e0f0f2014-03-27 08:08:03 -05002154OF_EARLYCON_DECLARE(pl011, "arm,pl011", pl011_early_console_setup);
Rob Herring0d3c6732014-04-18 17:19:57 -05002155
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156#else
2157#define AMBA_CONSOLE NULL
2158#endif
2159
2160static struct uart_driver amba_reg = {
2161 .owner = THIS_MODULE,
2162 .driver_name = "ttyAMA",
2163 .dev_name = "ttyAMA",
2164 .major = SERIAL_AMBA_MAJOR,
2165 .minor = SERIAL_AMBA_MINOR,
2166 .nr = UART_NR,
2167 .cons = AMBA_CONSOLE,
2168};
2169
Matthew Leach32614aa2012-08-28 16:41:28 +01002170static int pl011_probe_dt_alias(int index, struct device *dev)
2171{
2172 struct device_node *np;
2173 static bool seen_dev_with_alias = false;
2174 static bool seen_dev_without_alias = false;
2175 int ret = index;
2176
2177 if (!IS_ENABLED(CONFIG_OF))
2178 return ret;
2179
2180 np = dev->of_node;
2181 if (!np)
2182 return ret;
2183
2184 ret = of_alias_get_id(np, "serial");
2185 if (IS_ERR_VALUE(ret)) {
2186 seen_dev_without_alias = true;
2187 ret = index;
2188 } else {
2189 seen_dev_with_alias = true;
2190 if (ret >= ARRAY_SIZE(amba_ports) || amba_ports[ret] != NULL) {
2191 dev_warn(dev, "requested serial port %d not available.\n", ret);
2192 ret = index;
2193 }
2194 }
2195
2196 if (seen_dev_with_alias && seen_dev_without_alias)
2197 dev_warn(dev, "aliased and non-aliased serial devices found in device tree. Serial port enumeration may be unpredictable.\n");
2198
2199 return ret;
2200}
2201
Russell Kingaa25afa2011-02-19 15:55:00 +00002202static int pl011_probe(struct amba_device *dev, const struct amba_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203{
2204 struct uart_amba_port *uap;
Alessandro Rubini5926a292009-06-04 17:43:04 +01002205 struct vendor_data *vendor = id->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 void __iomem *base;
2207 int i, ret;
2208
2209 for (i = 0; i < ARRAY_SIZE(amba_ports); i++)
2210 if (amba_ports[i] == NULL)
2211 break;
2212
Tushar Behera7f6d9422014-06-26 15:35:35 +05302213 if (i == ARRAY_SIZE(amba_ports))
2214 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215
Linus Walleijde609582012-10-15 13:36:01 +02002216 uap = devm_kzalloc(&dev->dev, sizeof(struct uart_amba_port),
2217 GFP_KERNEL);
Tushar Behera7f6d9422014-06-26 15:35:35 +05302218 if (uap == NULL)
2219 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220
Matthew Leach32614aa2012-08-28 16:41:28 +01002221 i = pl011_probe_dt_alias(i, &dev->dev);
2222
Linus Walleijde609582012-10-15 13:36:01 +02002223 base = devm_ioremap(&dev->dev, dev->res.start,
2224 resource_size(&dev->res));
Tushar Behera7f6d9422014-06-26 15:35:35 +05302225 if (!base)
2226 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227
Linus Walleijde609582012-10-15 13:36:01 +02002228 uap->clk = devm_clk_get(&dev->dev, NULL);
Tushar Behera7f6d9422014-06-26 15:35:35 +05302229 if (IS_ERR(uap->clk))
2230 return PTR_ERR(uap->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231
Russell Kingc19f12b2010-12-22 17:48:26 +00002232 uap->vendor = vendor;
Linus Walleijec489aa2010-06-02 08:13:52 +01002233 uap->lcrh_rx = vendor->lcrh_rx;
2234 uap->lcrh_tx = vendor->lcrh_tx;
Shreshtha Kumar Sahud8d8ffa2012-01-18 15:53:59 +05302235 uap->old_cr = 0;
Jongsung Kimea336402013-05-10 18:05:35 +09002236 uap->fifosize = vendor->get_fifosize(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 uap->port.dev = &dev->dev;
2238 uap->port.mapbase = dev->res.start;
2239 uap->port.membase = base;
2240 uap->port.iotype = UPIO_MEM;
2241 uap->port.irq = dev->irq[0];
Russell Kingffca2b12010-12-22 17:13:05 +00002242 uap->port.fifosize = uap->fifosize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 uap->port.ops = &amba_pl011_pops;
2244 uap->port.flags = UPF_BOOT_AUTOCONF;
2245 uap->port.line = i;
Dave Martin734745c2015-03-04 12:27:33 +00002246 INIT_DELAYED_WORK(&uap->tx_softirq_work, pl011_tx_softirq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247
Linus Walleijc3d8b762012-03-21 20:15:18 +01002248 /* Ensure interrupts from this UART are masked and cleared */
2249 writew(0, uap->port.membase + UART011_IMSC);
2250 writew(0xffff, uap->port.membase + UART011_ICR);
2251
Russell Kinge8a7ba82010-12-28 09:16:54 +00002252 snprintf(uap->type, sizeof(uap->type), "PL011 rev%u", amba_rev(dev));
2253
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 amba_ports[i] = uap;
2255
2256 amba_set_drvdata(dev, uap);
Tushar Beheraef2889f2014-01-20 14:32:35 +05302257
2258 if (!amba_reg.state) {
2259 ret = uart_register_driver(&amba_reg);
2260 if (ret < 0) {
Jorge Ramirez-Ortiz1c9be312015-03-06 13:05:40 -05002261 dev_err(&dev->dev,
2262 "Failed to register AMBA-PL011 driver\n");
Tushar Beheraef2889f2014-01-20 14:32:35 +05302263 return ret;
2264 }
2265 }
2266
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 ret = uart_add_one_port(&amba_reg, &uap->port);
2268 if (ret) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 amba_ports[i] = NULL;
Tushar Beheraef2889f2014-01-20 14:32:35 +05302270 uart_unregister_driver(&amba_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 }
Tushar Behera7f6d9422014-06-26 15:35:35 +05302272
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 return ret;
2274}
2275
2276static int pl011_remove(struct amba_device *dev)
2277{
2278 struct uart_amba_port *uap = amba_get_drvdata(dev);
Guennadi Liakhovetski1e7da052014-04-05 16:31:08 +02002279 bool busy = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 int i;
2281
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 uart_remove_one_port(&amba_reg, &uap->port);
2283
2284 for (i = 0; i < ARRAY_SIZE(amba_ports); i++)
2285 if (amba_ports[i] == uap)
2286 amba_ports[i] = NULL;
Guennadi Liakhovetski1e7da052014-04-05 16:31:08 +02002287 else if (amba_ports[i])
2288 busy = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289
Russell King68b65f72010-12-22 17:24:39 +00002290 pl011_dma_remove(uap);
Guennadi Liakhovetski1e7da052014-04-05 16:31:08 +02002291 if (!busy)
2292 uart_unregister_driver(&amba_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 return 0;
2294}
2295
Ulf Hanssond0ce8502013-12-03 11:04:28 +01002296#ifdef CONFIG_PM_SLEEP
2297static int pl011_suspend(struct device *dev)
Leo Chenb736b892009-07-28 23:43:33 +01002298{
Ulf Hanssond0ce8502013-12-03 11:04:28 +01002299 struct uart_amba_port *uap = dev_get_drvdata(dev);
Leo Chenb736b892009-07-28 23:43:33 +01002300
2301 if (!uap)
2302 return -EINVAL;
2303
2304 return uart_suspend_port(&amba_reg, &uap->port);
2305}
2306
Ulf Hanssond0ce8502013-12-03 11:04:28 +01002307static int pl011_resume(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_resume_port(&amba_reg, &uap->port);
2315}
2316#endif
2317
Ulf Hanssond0ce8502013-12-03 11:04:28 +01002318static SIMPLE_DEV_PM_OPS(pl011_dev_pm_ops, pl011_suspend, pl011_resume);
2319
Russell King2c39c9e2010-07-27 08:50:16 +01002320static struct amba_id pl011_ids[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321 {
2322 .id = 0x00041011,
2323 .mask = 0x000fffff,
Alessandro Rubini5926a292009-06-04 17:43:04 +01002324 .data = &vendor_arm,
2325 },
2326 {
2327 .id = 0x00380802,
2328 .mask = 0x00ffffff,
2329 .data = &vendor_st,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 },
2331 { 0, 0 },
2332};
2333
Dave Martin60f7a332011-10-05 15:15:22 +01002334MODULE_DEVICE_TABLE(amba, pl011_ids);
2335
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336static struct amba_driver pl011_driver = {
2337 .drv = {
2338 .name = "uart-pl011",
Ulf Hanssond0ce8502013-12-03 11:04:28 +01002339 .pm = &pl011_dev_pm_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 },
2341 .id_table = pl011_ids,
2342 .probe = pl011_probe,
2343 .remove = pl011_remove,
2344};
2345
2346static int __init pl011_init(void)
2347{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 printk(KERN_INFO "Serial: AMBA PL011 UART driver\n");
2349
Tushar Beheraef2889f2014-01-20 14:32:35 +05302350 return amba_driver_register(&pl011_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351}
2352
2353static void __exit pl011_exit(void)
2354{
2355 amba_driver_unregister(&pl011_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356}
2357
Alessandro Rubini4dd9e742009-05-05 05:54:13 +01002358/*
2359 * While this can be a module, if builtin it's most likely the console
2360 * So let's leave module_exit but move module_init to an earlier place
2361 */
2362arch_initcall(pl011_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363module_exit(pl011_exit);
2364
2365MODULE_AUTHOR("ARM Ltd/Deep Blue Solutions Ltd");
2366MODULE_DESCRIPTION("ARM AMBA serial port driver");
2367MODULE_LICENSE("GPL");