blob: fd27e986b1dd3437dfd2560ec11efd8def7bf254 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Driver for AMBA serial ports
3 *
4 * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
5 *
6 * Copyright 1999 ARM Limited
7 * Copyright (C) 2000 Deep Blue Solutions Ltd.
Russell King68b65f72010-12-22 17:24:39 +00008 * Copyright (C) 2010 ST-Ericsson SA
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 * This is a generic driver for ARM AMBA-type serial ports. They
25 * have a lot of 16550-like features, but are not register compatible.
26 * Note that although they do have CTS, DCD and DSR inputs, they do
27 * not have an RI input, nor do they have DTR or RTS outputs. If
28 * required, these have to be supplied via some other means (eg, GPIO)
29 * and hooked into this driver.
30 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Chanho Mincb06ff12013-03-27 18:38:11 +090032
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#if defined(CONFIG_SERIAL_AMBA_PL011_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
34#define SUPPORT_SYSRQ
35#endif
36
37#include <linux/module.h>
38#include <linux/ioport.h>
39#include <linux/init.h>
40#include <linux/console.h>
41#include <linux/sysrq.h>
42#include <linux/device.h>
43#include <linux/tty.h>
44#include <linux/tty_flip.h>
45#include <linux/serial_core.h>
46#include <linux/serial.h>
Russell Kinga62c80e2006-01-07 13:52:45 +000047#include <linux/amba/bus.h>
48#include <linux/amba/serial.h>
Russell Kingf8ce2542006-01-07 16:15:52 +000049#include <linux/clk.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090050#include <linux/slab.h>
Russell King68b65f72010-12-22 17:24:39 +000051#include <linux/dmaengine.h>
52#include <linux/dma-mapping.h>
53#include <linux/scatterlist.h>
Shreshtha Kumar Sahuc16d51a2011-06-13 10:11:33 +020054#include <linux/delay.h>
Viresh Kumar258aea72012-02-01 16:12:19 +053055#include <linux/types.h>
Matthew Leach32614aa2012-08-28 16:41:28 +010056#include <linux/of.h>
57#include <linux/of_device.h>
Shawn Guo258e0552012-05-06 22:53:35 +080058#include <linux/pinctrl/consumer.h>
Alessandro Rubinicb707062012-06-24 12:46:37 +010059#include <linux/sizes.h>
Linus Walleijde609582012-10-15 13:36:01 +020060#include <linux/io.h>
Graeme Gregory3db9ab02015-05-21 17:26:24 +010061#include <linux/acpi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
63#define UART_NR 14
64
65#define SERIAL_AMBA_MAJOR 204
66#define SERIAL_AMBA_MINOR 64
67#define SERIAL_AMBA_NR UART_NR
68
69#define AMBA_ISR_PASS_LIMIT 256
70
Russell Kingb63d4f02005-11-19 11:10:35 +000071#define UART_DR_ERROR (UART011_DR_OE|UART011_DR_BE|UART011_DR_PE|UART011_DR_FE)
72#define UART_DUMMY_DR_RX (1 << 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
Alessandro Rubini5926a292009-06-04 17:43:04 +010074/* There is by now at least one vendor with differing details, so handle it */
75struct vendor_data {
76 unsigned int ifls;
Linus Walleijec489aa2010-06-02 08:13:52 +010077 unsigned int lcrh_tx;
78 unsigned int lcrh_rx;
Linus Walleijac3e3fb2010-06-02 20:40:22 +010079 bool oversampling;
Russell King38d62432010-12-22 17:59:16 +000080 bool dma_threshold;
Rajanikanth H.V4fd06902012-03-26 11:17:02 +020081 bool cts_event_workaround;
Andre Przywara71eec482015-05-21 17:26:21 +010082 bool always_enabled;
Andre Przywaracefc2d12015-05-21 17:26:22 +010083 bool fixed_options;
Jongsung Kim78506f22013-04-15 14:45:25 +090084
Jongsung Kimea336402013-05-10 18:05:35 +090085 unsigned int (*get_fifosize)(struct amba_device *dev);
Alessandro Rubini5926a292009-06-04 17:43:04 +010086};
87
Jongsung Kimea336402013-05-10 18:05:35 +090088static unsigned int get_fifosize_arm(struct amba_device *dev)
Jongsung Kim78506f22013-04-15 14:45:25 +090089{
Jongsung Kimea336402013-05-10 18:05:35 +090090 return amba_rev(dev) < 3 ? 16 : 32;
Jongsung Kim78506f22013-04-15 14:45:25 +090091}
92
Alessandro Rubini5926a292009-06-04 17:43:04 +010093static struct vendor_data vendor_arm = {
94 .ifls = UART011_IFLS_RX4_8|UART011_IFLS_TX4_8,
Linus Walleijec489aa2010-06-02 08:13:52 +010095 .lcrh_tx = UART011_LCRH,
96 .lcrh_rx = UART011_LCRH,
Linus Walleijac3e3fb2010-06-02 20:40:22 +010097 .oversampling = false,
Russell King38d62432010-12-22 17:59:16 +000098 .dma_threshold = false,
Rajanikanth H.V4fd06902012-03-26 11:17:02 +020099 .cts_event_workaround = false,
Andre Przywara71eec482015-05-21 17:26:21 +0100100 .always_enabled = false,
Andre Przywaracefc2d12015-05-21 17:26:22 +0100101 .fixed_options = false,
Jongsung Kim78506f22013-04-15 14:45:25 +0900102 .get_fifosize = get_fifosize_arm,
Alessandro Rubini5926a292009-06-04 17:43:04 +0100103};
104
Andre Przywara0dd1e242015-05-21 17:26:23 +0100105static struct vendor_data vendor_sbsa = {
106 .oversampling = false,
107 .dma_threshold = false,
108 .cts_event_workaround = false,
109 .always_enabled = true,
110 .fixed_options = true,
111};
112
Jongsung Kimea336402013-05-10 18:05:35 +0900113static unsigned int get_fifosize_st(struct amba_device *dev)
Jongsung Kim78506f22013-04-15 14:45:25 +0900114{
115 return 64;
116}
117
Alessandro Rubini5926a292009-06-04 17:43:04 +0100118static struct vendor_data vendor_st = {
119 .ifls = UART011_IFLS_RX_HALF|UART011_IFLS_TX_HALF,
Linus Walleijec489aa2010-06-02 08:13:52 +0100120 .lcrh_tx = ST_UART011_LCRH_TX,
121 .lcrh_rx = ST_UART011_LCRH_RX,
Linus Walleijac3e3fb2010-06-02 20:40:22 +0100122 .oversampling = true,
Russell King38d62432010-12-22 17:59:16 +0000123 .dma_threshold = true,
Rajanikanth H.V4fd06902012-03-26 11:17:02 +0200124 .cts_event_workaround = true,
Andre Przywara71eec482015-05-21 17:26:21 +0100125 .always_enabled = false,
Andre Przywaracefc2d12015-05-21 17:26:22 +0100126 .fixed_options = false,
Jongsung Kim78506f22013-04-15 14:45:25 +0900127 .get_fifosize = get_fifosize_st,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128};
129
Russell King68b65f72010-12-22 17:24:39 +0000130/* Deals with DMA transactions */
Linus Walleijead76f32011-02-24 13:21:08 +0100131
132struct pl011_sgbuf {
133 struct scatterlist sg;
134 char *buf;
135};
136
137struct pl011_dmarx_data {
138 struct dma_chan *chan;
139 struct completion complete;
140 bool use_buf_b;
141 struct pl011_sgbuf sgbuf_a;
142 struct pl011_sgbuf sgbuf_b;
143 dma_cookie_t cookie;
144 bool running;
Chanho Mincb06ff12013-03-27 18:38:11 +0900145 struct timer_list timer;
146 unsigned int last_residue;
147 unsigned long last_jiffies;
148 bool auto_poll_rate;
149 unsigned int poll_rate;
150 unsigned int poll_timeout;
Linus Walleijead76f32011-02-24 13:21:08 +0100151};
152
Russell King68b65f72010-12-22 17:24:39 +0000153struct pl011_dmatx_data {
154 struct dma_chan *chan;
155 struct scatterlist sg;
156 char *buf;
157 bool queued;
158};
159
Russell Kingc19f12b2010-12-22 17:48:26 +0000160/*
161 * We wrap our port structure around the generic uart_port.
162 */
163struct uart_amba_port {
164 struct uart_port port;
165 struct clk *clk;
166 const struct vendor_data *vendor;
Russell King68b65f72010-12-22 17:24:39 +0000167 unsigned int dmacr; /* dma control reg */
Russell Kingc19f12b2010-12-22 17:48:26 +0000168 unsigned int im; /* interrupt mask */
169 unsigned int old_status;
Russell Kingffca2b12010-12-22 17:13:05 +0000170 unsigned int fifosize; /* vendor-specific */
Russell Kingc19f12b2010-12-22 17:48:26 +0000171 unsigned int lcrh_tx; /* vendor-specific */
172 unsigned int lcrh_rx; /* vendor-specific */
Shreshtha Kumar Sahud8d8ffa2012-01-18 15:53:59 +0530173 unsigned int old_cr; /* state during shutdown */
Russell Kingc19f12b2010-12-22 17:48:26 +0000174 bool autorts;
Andre Przywaracefc2d12015-05-21 17:26:22 +0100175 unsigned int fixed_baud; /* vendor-set fixed baud rate */
Russell Kingc19f12b2010-12-22 17:48:26 +0000176 char type[12];
Russell King68b65f72010-12-22 17:24:39 +0000177#ifdef CONFIG_DMA_ENGINE
178 /* DMA stuff */
Linus Walleijead76f32011-02-24 13:21:08 +0100179 bool using_tx_dma;
180 bool using_rx_dma;
181 struct pl011_dmarx_data dmarx;
Russell King68b65f72010-12-22 17:24:39 +0000182 struct pl011_dmatx_data dmatx;
Jorge Ramirez-Ortiz1c9be312015-03-06 13:05:40 -0500183 bool dma_probed;
Russell King68b65f72010-12-22 17:24:39 +0000184#endif
Russell Kingc19f12b2010-12-22 17:48:26 +0000185};
186
Russell King68b65f72010-12-22 17:24:39 +0000187/*
Linus Walleij29772c42011-02-24 13:21:36 +0100188 * Reads up to 256 characters from the FIFO or until it's empty and
189 * inserts them into the TTY layer. Returns the number of characters
190 * read from the FIFO.
191 */
192static int pl011_fifo_to_tty(struct uart_amba_port *uap)
193{
194 u16 status, ch;
195 unsigned int flag, max_count = 256;
196 int fifotaken = 0;
197
198 while (max_count--) {
199 status = readw(uap->port.membase + UART01x_FR);
200 if (status & UART01x_FR_RXFE)
201 break;
202
203 /* Take chars from the FIFO and update status */
204 ch = readw(uap->port.membase + UART01x_DR) |
205 UART_DUMMY_DR_RX;
206 flag = TTY_NORMAL;
207 uap->port.icount.rx++;
208 fifotaken++;
209
210 if (unlikely(ch & UART_DR_ERROR)) {
211 if (ch & UART011_DR_BE) {
212 ch &= ~(UART011_DR_FE | UART011_DR_PE);
213 uap->port.icount.brk++;
214 if (uart_handle_break(&uap->port))
215 continue;
216 } else if (ch & UART011_DR_PE)
217 uap->port.icount.parity++;
218 else if (ch & UART011_DR_FE)
219 uap->port.icount.frame++;
220 if (ch & UART011_DR_OE)
221 uap->port.icount.overrun++;
222
223 ch &= uap->port.read_status_mask;
224
225 if (ch & UART011_DR_BE)
226 flag = TTY_BREAK;
227 else if (ch & UART011_DR_PE)
228 flag = TTY_PARITY;
229 else if (ch & UART011_DR_FE)
230 flag = TTY_FRAME;
231 }
232
233 if (uart_handle_sysrq_char(&uap->port, ch & 255))
234 continue;
235
236 uart_insert_char(&uap->port, ch, UART011_DR_OE, ch, flag);
237 }
238
239 return fifotaken;
240}
241
242
243/*
Russell King68b65f72010-12-22 17:24:39 +0000244 * All the DMA operation mode stuff goes inside this ifdef.
245 * This assumes that you have a generic DMA device interface,
246 * no custom DMA interfaces are supported.
247 */
248#ifdef CONFIG_DMA_ENGINE
249
250#define PL011_DMA_BUFFER_SIZE PAGE_SIZE
251
Linus Walleijead76f32011-02-24 13:21:08 +0100252static int pl011_sgbuf_init(struct dma_chan *chan, struct pl011_sgbuf *sg,
253 enum dma_data_direction dir)
254{
Chanho Mincb06ff12013-03-27 18:38:11 +0900255 dma_addr_t dma_addr;
256
257 sg->buf = dma_alloc_coherent(chan->device->dev,
258 PL011_DMA_BUFFER_SIZE, &dma_addr, GFP_KERNEL);
Linus Walleijead76f32011-02-24 13:21:08 +0100259 if (!sg->buf)
260 return -ENOMEM;
261
Chanho Mincb06ff12013-03-27 18:38:11 +0900262 sg_init_table(&sg->sg, 1);
263 sg_set_page(&sg->sg, phys_to_page(dma_addr),
264 PL011_DMA_BUFFER_SIZE, offset_in_page(dma_addr));
265 sg_dma_address(&sg->sg) = dma_addr;
Andrew Jacksonc64be922014-11-07 14:14:43 +0000266 sg_dma_len(&sg->sg) = PL011_DMA_BUFFER_SIZE;
Linus Walleijead76f32011-02-24 13:21:08 +0100267
Linus Walleijead76f32011-02-24 13:21:08 +0100268 return 0;
269}
270
271static void pl011_sgbuf_free(struct dma_chan *chan, struct pl011_sgbuf *sg,
272 enum dma_data_direction dir)
273{
274 if (sg->buf) {
Chanho Mincb06ff12013-03-27 18:38:11 +0900275 dma_free_coherent(chan->device->dev,
276 PL011_DMA_BUFFER_SIZE, sg->buf,
277 sg_dma_address(&sg->sg));
Linus Walleijead76f32011-02-24 13:21:08 +0100278 }
279}
280
Jorge Ramirez-Ortiz1c9be312015-03-06 13:05:40 -0500281static void pl011_dma_probe(struct uart_amba_port *uap)
Russell King68b65f72010-12-22 17:24:39 +0000282{
283 /* DMA is the sole user of the platform data right now */
Jingoo Han574de552013-07-30 17:06:57 +0900284 struct amba_pl011_data *plat = dev_get_platdata(uap->port.dev);
Jorge Ramirez-Ortiz1c9be312015-03-06 13:05:40 -0500285 struct device *dev = uap->port.dev;
Russell King68b65f72010-12-22 17:24:39 +0000286 struct dma_slave_config tx_conf = {
287 .dst_addr = uap->port.mapbase + UART01x_DR,
288 .dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE,
Vinod Koula485df42011-10-14 10:47:38 +0530289 .direction = DMA_MEM_TO_DEV,
Russell King68b65f72010-12-22 17:24:39 +0000290 .dst_maxburst = uap->fifosize >> 1,
Viresh Kumar258aea72012-02-01 16:12:19 +0530291 .device_fc = false,
Russell King68b65f72010-12-22 17:24:39 +0000292 };
293 struct dma_chan *chan;
294 dma_cap_mask_t mask;
295
Jorge Ramirez-Ortiz1c9be312015-03-06 13:05:40 -0500296 uap->dma_probed = true;
297 chan = dma_request_slave_channel_reason(dev, "tx");
298 if (IS_ERR(chan)) {
299 if (PTR_ERR(chan) == -EPROBE_DEFER) {
Jorge Ramirez-Ortiz1c9be312015-03-06 13:05:40 -0500300 uap->dma_probed = false;
301 return;
302 }
Russell King68b65f72010-12-22 17:24:39 +0000303
Arnd Bergmann787b0c12013-01-28 16:24:37 +0000304 /* We need platform data */
305 if (!plat || !plat->dma_filter) {
306 dev_info(uap->port.dev, "no DMA platform data\n");
307 return;
308 }
309
310 /* Try to acquire a generic DMA engine slave TX channel */
311 dma_cap_zero(mask);
312 dma_cap_set(DMA_SLAVE, mask);
313
314 chan = dma_request_channel(mask, plat->dma_filter,
315 plat->dma_tx_param);
316 if (!chan) {
317 dev_err(uap->port.dev, "no TX DMA channel!\n");
318 return;
319 }
Russell King68b65f72010-12-22 17:24:39 +0000320 }
321
322 dmaengine_slave_config(chan, &tx_conf);
323 uap->dmatx.chan = chan;
324
325 dev_info(uap->port.dev, "DMA channel TX %s\n",
326 dma_chan_name(uap->dmatx.chan));
Linus Walleijead76f32011-02-24 13:21:08 +0100327
328 /* Optionally make use of an RX channel as well */
Arnd Bergmann787b0c12013-01-28 16:24:37 +0000329 chan = dma_request_slave_channel(dev, "rx");
Rob Herring0d3c6732014-04-18 17:19:57 -0500330
Arnd Bergmann787b0c12013-01-28 16:24:37 +0000331 if (!chan && plat->dma_rx_param) {
332 chan = dma_request_channel(mask, plat->dma_filter, plat->dma_rx_param);
333
334 if (!chan) {
335 dev_err(uap->port.dev, "no RX DMA channel!\n");
336 return;
337 }
338 }
339
340 if (chan) {
Linus Walleijead76f32011-02-24 13:21:08 +0100341 struct dma_slave_config rx_conf = {
342 .src_addr = uap->port.mapbase + UART01x_DR,
343 .src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE,
Vinod Koula485df42011-10-14 10:47:38 +0530344 .direction = DMA_DEV_TO_MEM,
Guennadi Liakhovetskib2aeb772014-04-12 19:47:17 +0200345 .src_maxburst = uap->fifosize >> 2,
Viresh Kumar258aea72012-02-01 16:12:19 +0530346 .device_fc = false,
Linus Walleijead76f32011-02-24 13:21:08 +0100347 };
Andrew Jackson2d3b7d62014-11-07 14:14:47 +0000348 struct dma_slave_caps caps;
Linus Walleijead76f32011-02-24 13:21:08 +0100349
Andrew Jackson2d3b7d62014-11-07 14:14:47 +0000350 /*
351 * Some DMA controllers provide information on their capabilities.
352 * If the controller does, check for suitable residue processing
353 * otherwise assime all is well.
354 */
355 if (0 == dma_get_slave_caps(chan, &caps)) {
356 if (caps.residue_granularity ==
357 DMA_RESIDUE_GRANULARITY_DESCRIPTOR) {
358 dma_release_channel(chan);
359 dev_info(uap->port.dev,
360 "RX DMA disabled - no residue processing\n");
361 return;
362 }
363 }
Linus Walleijead76f32011-02-24 13:21:08 +0100364 dmaengine_slave_config(chan, &rx_conf);
365 uap->dmarx.chan = chan;
366
Andrew Jackson98267d32014-11-07 14:14:23 +0000367 uap->dmarx.auto_poll_rate = false;
Greg Kroah-Hartman8f898bf2013-12-17 09:33:18 -0800368 if (plat && plat->dma_rx_poll_enable) {
Chanho Mincb06ff12013-03-27 18:38:11 +0900369 /* Set poll rate if specified. */
370 if (plat->dma_rx_poll_rate) {
371 uap->dmarx.auto_poll_rate = false;
372 uap->dmarx.poll_rate = plat->dma_rx_poll_rate;
373 } else {
374 /*
375 * 100 ms defaults to poll rate if not
376 * specified. This will be adjusted with
377 * the baud rate at set_termios.
378 */
379 uap->dmarx.auto_poll_rate = true;
380 uap->dmarx.poll_rate = 100;
381 }
382 /* 3 secs defaults poll_timeout if not specified. */
383 if (plat->dma_rx_poll_timeout)
384 uap->dmarx.poll_timeout =
385 plat->dma_rx_poll_timeout;
386 else
387 uap->dmarx.poll_timeout = 3000;
Andrew Jackson98267d32014-11-07 14:14:23 +0000388 } else if (!plat && dev->of_node) {
389 uap->dmarx.auto_poll_rate = of_property_read_bool(
390 dev->of_node, "auto-poll");
391 if (uap->dmarx.auto_poll_rate) {
392 u32 x;
Chanho Mincb06ff12013-03-27 18:38:11 +0900393
Andrew Jackson98267d32014-11-07 14:14:23 +0000394 if (0 == of_property_read_u32(dev->of_node,
395 "poll-rate-ms", &x))
396 uap->dmarx.poll_rate = x;
397 else
398 uap->dmarx.poll_rate = 100;
399 if (0 == of_property_read_u32(dev->of_node,
400 "poll-timeout-ms", &x))
401 uap->dmarx.poll_timeout = x;
402 else
403 uap->dmarx.poll_timeout = 3000;
404 }
405 }
Linus Walleijead76f32011-02-24 13:21:08 +0100406 dev_info(uap->port.dev, "DMA channel RX %s\n",
407 dma_chan_name(uap->dmarx.chan));
408 }
Russell King68b65f72010-12-22 17:24:39 +0000409}
410
Russell King68b65f72010-12-22 17:24:39 +0000411static void pl011_dma_remove(struct uart_amba_port *uap)
412{
Russell King68b65f72010-12-22 17:24:39 +0000413 if (uap->dmatx.chan)
414 dma_release_channel(uap->dmatx.chan);
Linus Walleijead76f32011-02-24 13:21:08 +0100415 if (uap->dmarx.chan)
416 dma_release_channel(uap->dmarx.chan);
Russell King68b65f72010-12-22 17:24:39 +0000417}
418
Dave Martin734745c2015-03-04 12:27:33 +0000419/* Forward declare these for the refill routine */
Russell King68b65f72010-12-22 17:24:39 +0000420static int pl011_dma_tx_refill(struct uart_amba_port *uap);
Dave Martin734745c2015-03-04 12:27:33 +0000421static void pl011_start_tx_pio(struct uart_amba_port *uap);
Russell King68b65f72010-12-22 17:24:39 +0000422
423/*
424 * The current DMA TX buffer has been sent.
425 * Try to queue up another DMA buffer.
426 */
427static void pl011_dma_tx_callback(void *data)
428{
429 struct uart_amba_port *uap = data;
430 struct pl011_dmatx_data *dmatx = &uap->dmatx;
431 unsigned long flags;
432 u16 dmacr;
433
434 spin_lock_irqsave(&uap->port.lock, flags);
435 if (uap->dmatx.queued)
436 dma_unmap_sg(dmatx->chan->device->dev, &dmatx->sg, 1,
437 DMA_TO_DEVICE);
438
439 dmacr = uap->dmacr;
440 uap->dmacr = dmacr & ~UART011_TXDMAE;
441 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
442
443 /*
444 * If TX DMA was disabled, it means that we've stopped the DMA for
445 * some reason (eg, XOFF received, or we want to send an X-char.)
446 *
447 * Note: we need to be careful here of a potential race between DMA
448 * and the rest of the driver - if the driver disables TX DMA while
449 * a TX buffer completing, we must update the tx queued status to
450 * get further refills (hence we check dmacr).
451 */
452 if (!(dmacr & UART011_TXDMAE) || uart_tx_stopped(&uap->port) ||
453 uart_circ_empty(&uap->port.state->xmit)) {
454 uap->dmatx.queued = false;
455 spin_unlock_irqrestore(&uap->port.lock, flags);
456 return;
457 }
458
Dave Martin734745c2015-03-04 12:27:33 +0000459 if (pl011_dma_tx_refill(uap) <= 0)
Russell King68b65f72010-12-22 17:24:39 +0000460 /*
461 * We didn't queue a DMA buffer for some reason, but we
462 * have data pending to be sent. Re-enable the TX IRQ.
463 */
Dave Martin734745c2015-03-04 12:27:33 +0000464 pl011_start_tx_pio(uap);
465
Russell King68b65f72010-12-22 17:24:39 +0000466 spin_unlock_irqrestore(&uap->port.lock, flags);
467}
468
469/*
470 * Try to refill the TX DMA buffer.
471 * Locking: called with port lock held and IRQs disabled.
472 * Returns:
473 * 1 if we queued up a TX DMA buffer.
474 * 0 if we didn't want to handle this by DMA
475 * <0 on error
476 */
477static int pl011_dma_tx_refill(struct uart_amba_port *uap)
478{
479 struct pl011_dmatx_data *dmatx = &uap->dmatx;
480 struct dma_chan *chan = dmatx->chan;
481 struct dma_device *dma_dev = chan->device;
482 struct dma_async_tx_descriptor *desc;
483 struct circ_buf *xmit = &uap->port.state->xmit;
484 unsigned int count;
485
486 /*
487 * Try to avoid the overhead involved in using DMA if the
488 * transaction fits in the first half of the FIFO, by using
489 * the standard interrupt handling. This ensures that we
490 * issue a uart_write_wakeup() at the appropriate time.
491 */
492 count = uart_circ_chars_pending(xmit);
493 if (count < (uap->fifosize >> 1)) {
494 uap->dmatx.queued = false;
495 return 0;
496 }
497
498 /*
499 * Bodge: don't send the last character by DMA, as this
500 * will prevent XON from notifying us to restart DMA.
501 */
502 count -= 1;
503
504 /* Else proceed to copy the TX chars to the DMA buffer and fire DMA */
505 if (count > PL011_DMA_BUFFER_SIZE)
506 count = PL011_DMA_BUFFER_SIZE;
507
508 if (xmit->tail < xmit->head)
509 memcpy(&dmatx->buf[0], &xmit->buf[xmit->tail], count);
510 else {
511 size_t first = UART_XMIT_SIZE - xmit->tail;
Andrew Jacksone2a545a2014-11-07 14:14:39 +0000512 size_t second;
513
514 if (first > count)
515 first = count;
516 second = count - first;
Russell King68b65f72010-12-22 17:24:39 +0000517
518 memcpy(&dmatx->buf[0], &xmit->buf[xmit->tail], first);
519 if (second)
520 memcpy(&dmatx->buf[first], &xmit->buf[0], second);
521 }
522
523 dmatx->sg.length = count;
524
525 if (dma_map_sg(dma_dev->dev, &dmatx->sg, 1, DMA_TO_DEVICE) != 1) {
526 uap->dmatx.queued = false;
527 dev_dbg(uap->port.dev, "unable to map TX DMA\n");
528 return -EBUSY;
529 }
530
Alexandre Bounine16052822012-03-08 16:11:18 -0500531 desc = dmaengine_prep_slave_sg(chan, &dmatx->sg, 1, DMA_MEM_TO_DEV,
Russell King68b65f72010-12-22 17:24:39 +0000532 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
533 if (!desc) {
534 dma_unmap_sg(dma_dev->dev, &dmatx->sg, 1, DMA_TO_DEVICE);
535 uap->dmatx.queued = false;
536 /*
537 * If DMA cannot be used right now, we complete this
538 * transaction via IRQ and let the TTY layer retry.
539 */
540 dev_dbg(uap->port.dev, "TX DMA busy\n");
541 return -EBUSY;
542 }
543
544 /* Some data to go along to the callback */
545 desc->callback = pl011_dma_tx_callback;
546 desc->callback_param = uap;
547
548 /* All errors should happen at prepare time */
549 dmaengine_submit(desc);
550
551 /* Fire the DMA transaction */
552 dma_dev->device_issue_pending(chan);
553
554 uap->dmacr |= UART011_TXDMAE;
555 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
556 uap->dmatx.queued = true;
557
558 /*
559 * Now we know that DMA will fire, so advance the ring buffer
560 * with the stuff we just dispatched.
561 */
562 xmit->tail = (xmit->tail + count) & (UART_XMIT_SIZE - 1);
563 uap->port.icount.tx += count;
564
565 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
566 uart_write_wakeup(&uap->port);
567
568 return 1;
569}
570
571/*
572 * We received a transmit interrupt without a pending X-char but with
573 * pending characters.
574 * Locking: called with port lock held and IRQs disabled.
575 * Returns:
576 * false if we want to use PIO to transmit
577 * true if we queued a DMA buffer
578 */
579static bool pl011_dma_tx_irq(struct uart_amba_port *uap)
580{
Linus Walleijead76f32011-02-24 13:21:08 +0100581 if (!uap->using_tx_dma)
Russell King68b65f72010-12-22 17:24:39 +0000582 return false;
583
584 /*
585 * If we already have a TX buffer queued, but received a
586 * TX interrupt, it will be because we've just sent an X-char.
587 * Ensure the TX DMA is enabled and the TX IRQ is disabled.
588 */
589 if (uap->dmatx.queued) {
590 uap->dmacr |= UART011_TXDMAE;
591 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
592 uap->im &= ~UART011_TXIM;
593 writew(uap->im, uap->port.membase + UART011_IMSC);
594 return true;
595 }
596
597 /*
598 * We don't have a TX buffer queued, so try to queue one.
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300599 * If we successfully queued a buffer, mask the TX IRQ.
Russell King68b65f72010-12-22 17:24:39 +0000600 */
601 if (pl011_dma_tx_refill(uap) > 0) {
602 uap->im &= ~UART011_TXIM;
603 writew(uap->im, uap->port.membase + UART011_IMSC);
604 return true;
605 }
606 return false;
607}
608
609/*
610 * Stop the DMA transmit (eg, due to received XOFF).
611 * Locking: called with port lock held and IRQs disabled.
612 */
613static inline void pl011_dma_tx_stop(struct uart_amba_port *uap)
614{
615 if (uap->dmatx.queued) {
616 uap->dmacr &= ~UART011_TXDMAE;
617 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
618 }
619}
620
621/*
622 * Try to start a DMA transmit, or in the case of an XON/OFF
623 * character queued for send, try to get that character out ASAP.
624 * Locking: called with port lock held and IRQs disabled.
625 * Returns:
626 * false if we want the TX IRQ to be enabled
627 * true if we have a buffer queued
628 */
629static inline bool pl011_dma_tx_start(struct uart_amba_port *uap)
630{
631 u16 dmacr;
632
Linus Walleijead76f32011-02-24 13:21:08 +0100633 if (!uap->using_tx_dma)
Russell King68b65f72010-12-22 17:24:39 +0000634 return false;
635
636 if (!uap->port.x_char) {
637 /* no X-char, try to push chars out in DMA mode */
638 bool ret = true;
639
640 if (!uap->dmatx.queued) {
641 if (pl011_dma_tx_refill(uap) > 0) {
642 uap->im &= ~UART011_TXIM;
Dave Martin734745c2015-03-04 12:27:33 +0000643 writew(uap->im, uap->port.membase +
644 UART011_IMSC);
645 } else
Russell King68b65f72010-12-22 17:24:39 +0000646 ret = false;
Russell King68b65f72010-12-22 17:24:39 +0000647 } else if (!(uap->dmacr & UART011_TXDMAE)) {
648 uap->dmacr |= UART011_TXDMAE;
649 writew(uap->dmacr,
650 uap->port.membase + UART011_DMACR);
651 }
652 return ret;
653 }
654
655 /*
656 * We have an X-char to send. Disable DMA to prevent it loading
657 * the TX fifo, and then see if we can stuff it into the FIFO.
658 */
659 dmacr = uap->dmacr;
660 uap->dmacr &= ~UART011_TXDMAE;
661 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
662
663 if (readw(uap->port.membase + UART01x_FR) & UART01x_FR_TXFF) {
664 /*
665 * No space in the FIFO, so enable the transmit interrupt
666 * so we know when there is space. Note that once we've
667 * loaded the character, we should just re-enable DMA.
668 */
669 return false;
670 }
671
672 writew(uap->port.x_char, uap->port.membase + UART01x_DR);
673 uap->port.icount.tx++;
674 uap->port.x_char = 0;
675
676 /* Success - restore the DMA state */
677 uap->dmacr = dmacr;
678 writew(dmacr, uap->port.membase + UART011_DMACR);
679
680 return true;
681}
682
683/*
684 * Flush the transmit buffer.
685 * Locking: called with port lock held and IRQs disabled.
686 */
687static void pl011_dma_flush_buffer(struct uart_port *port)
Fabio Estevamb83286b2013-08-09 17:58:51 -0300688__releases(&uap->port.lock)
689__acquires(&uap->port.lock)
Russell King68b65f72010-12-22 17:24:39 +0000690{
Daniel Thompsona5820c22014-09-03 12:51:55 +0100691 struct uart_amba_port *uap =
692 container_of(port, struct uart_amba_port, port);
Russell King68b65f72010-12-22 17:24:39 +0000693
Linus Walleijead76f32011-02-24 13:21:08 +0100694 if (!uap->using_tx_dma)
Russell King68b65f72010-12-22 17:24:39 +0000695 return;
696
697 /* Avoid deadlock with the DMA engine callback */
698 spin_unlock(&uap->port.lock);
699 dmaengine_terminate_all(uap->dmatx.chan);
700 spin_lock(&uap->port.lock);
701 if (uap->dmatx.queued) {
702 dma_unmap_sg(uap->dmatx.chan->device->dev, &uap->dmatx.sg, 1,
703 DMA_TO_DEVICE);
704 uap->dmatx.queued = false;
705 uap->dmacr &= ~UART011_TXDMAE;
706 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
707 }
708}
709
Linus Walleijead76f32011-02-24 13:21:08 +0100710static void pl011_dma_rx_callback(void *data);
711
712static int pl011_dma_rx_trigger_dma(struct uart_amba_port *uap)
713{
714 struct dma_chan *rxchan = uap->dmarx.chan;
Linus Walleijead76f32011-02-24 13:21:08 +0100715 struct pl011_dmarx_data *dmarx = &uap->dmarx;
716 struct dma_async_tx_descriptor *desc;
717 struct pl011_sgbuf *sgbuf;
718
719 if (!rxchan)
720 return -EIO;
721
722 /* Start the RX DMA job */
723 sgbuf = uap->dmarx.use_buf_b ?
724 &uap->dmarx.sgbuf_b : &uap->dmarx.sgbuf_a;
Alexandre Bounine16052822012-03-08 16:11:18 -0500725 desc = dmaengine_prep_slave_sg(rxchan, &sgbuf->sg, 1,
Vinod Koula485df42011-10-14 10:47:38 +0530726 DMA_DEV_TO_MEM,
Linus Walleijead76f32011-02-24 13:21:08 +0100727 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
728 /*
729 * If the DMA engine is busy and cannot prepare a
730 * channel, no big deal, the driver will fall back
731 * to interrupt mode as a result of this error code.
732 */
733 if (!desc) {
734 uap->dmarx.running = false;
735 dmaengine_terminate_all(rxchan);
736 return -EBUSY;
737 }
738
739 /* Some data to go along to the callback */
740 desc->callback = pl011_dma_rx_callback;
741 desc->callback_param = uap;
742 dmarx->cookie = dmaengine_submit(desc);
743 dma_async_issue_pending(rxchan);
744
745 uap->dmacr |= UART011_RXDMAE;
746 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
747 uap->dmarx.running = true;
748
749 uap->im &= ~UART011_RXIM;
750 writew(uap->im, uap->port.membase + UART011_IMSC);
751
752 return 0;
753}
754
755/*
756 * This is called when either the DMA job is complete, or
757 * the FIFO timeout interrupt occurred. This must be called
758 * with the port spinlock uap->port.lock held.
759 */
760static void pl011_dma_rx_chars(struct uart_amba_port *uap,
761 u32 pending, bool use_buf_b,
762 bool readfifo)
763{
Jiri Slaby05c7cd32013-01-03 15:53:04 +0100764 struct tty_port *port = &uap->port.state->port;
Linus Walleijead76f32011-02-24 13:21:08 +0100765 struct pl011_sgbuf *sgbuf = use_buf_b ?
766 &uap->dmarx.sgbuf_b : &uap->dmarx.sgbuf_a;
Linus Walleijead76f32011-02-24 13:21:08 +0100767 int dma_count = 0;
768 u32 fifotaken = 0; /* only used for vdbg() */
769
Chanho Mincb06ff12013-03-27 18:38:11 +0900770 struct pl011_dmarx_data *dmarx = &uap->dmarx;
771 int dmataken = 0;
772
773 if (uap->dmarx.poll_rate) {
774 /* The data can be taken by polling */
775 dmataken = sgbuf->sg.length - dmarx->last_residue;
776 /* Recalculate the pending size */
777 if (pending >= dmataken)
778 pending -= dmataken;
779 }
780
781 /* Pick the remain data from the DMA */
Linus Walleijead76f32011-02-24 13:21:08 +0100782 if (pending) {
Linus Walleijead76f32011-02-24 13:21:08 +0100783
784 /*
785 * First take all chars in the DMA pipe, then look in the FIFO.
786 * Note that tty_insert_flip_buf() tries to take as many chars
787 * as it can.
788 */
Chanho Mincb06ff12013-03-27 18:38:11 +0900789 dma_count = tty_insert_flip_string(port, sgbuf->buf + dmataken,
790 pending);
Linus Walleijead76f32011-02-24 13:21:08 +0100791
792 uap->port.icount.rx += dma_count;
793 if (dma_count < pending)
794 dev_warn(uap->port.dev,
795 "couldn't insert all characters (TTY is full?)\n");
796 }
797
Chanho Mincb06ff12013-03-27 18:38:11 +0900798 /* Reset the last_residue for Rx DMA poll */
799 if (uap->dmarx.poll_rate)
800 dmarx->last_residue = sgbuf->sg.length;
801
Linus Walleijead76f32011-02-24 13:21:08 +0100802 /*
803 * Only continue with trying to read the FIFO if all DMA chars have
804 * been taken first.
805 */
806 if (dma_count == pending && readfifo) {
807 /* Clear any error flags */
808 writew(UART011_OEIS | UART011_BEIS | UART011_PEIS | UART011_FEIS,
809 uap->port.membase + UART011_ICR);
810
811 /*
812 * If we read all the DMA'd characters, and we had an
Linus Walleij29772c42011-02-24 13:21:36 +0100813 * incomplete buffer, that could be due to an rx error, or
814 * maybe we just timed out. Read any pending chars and check
815 * the error status.
816 *
817 * Error conditions will only occur in the FIFO, these will
818 * trigger an immediate interrupt and stop the DMA job, so we
819 * will always find the error in the FIFO, never in the DMA
820 * buffer.
Linus Walleijead76f32011-02-24 13:21:08 +0100821 */
Linus Walleij29772c42011-02-24 13:21:36 +0100822 fifotaken = pl011_fifo_to_tty(uap);
Linus Walleijead76f32011-02-24 13:21:08 +0100823 }
824
825 spin_unlock(&uap->port.lock);
826 dev_vdbg(uap->port.dev,
827 "Took %d chars from DMA buffer and %d chars from the FIFO\n",
828 dma_count, fifotaken);
Jiri Slaby2e124b42013-01-03 15:53:06 +0100829 tty_flip_buffer_push(port);
Linus Walleijead76f32011-02-24 13:21:08 +0100830 spin_lock(&uap->port.lock);
831}
832
833static void pl011_dma_rx_irq(struct uart_amba_port *uap)
834{
835 struct pl011_dmarx_data *dmarx = &uap->dmarx;
836 struct dma_chan *rxchan = dmarx->chan;
837 struct pl011_sgbuf *sgbuf = dmarx->use_buf_b ?
838 &dmarx->sgbuf_b : &dmarx->sgbuf_a;
839 size_t pending;
840 struct dma_tx_state state;
841 enum dma_status dmastat;
842
843 /*
844 * Pause the transfer so we can trust the current counter,
845 * do this before we pause the PL011 block, else we may
846 * overflow the FIFO.
847 */
848 if (dmaengine_pause(rxchan))
849 dev_err(uap->port.dev, "unable to pause DMA transfer\n");
850 dmastat = rxchan->device->device_tx_status(rxchan,
851 dmarx->cookie, &state);
852 if (dmastat != DMA_PAUSED)
853 dev_err(uap->port.dev, "unable to pause DMA transfer\n");
854
855 /* Disable RX DMA - incoming data will wait in the FIFO */
856 uap->dmacr &= ~UART011_RXDMAE;
857 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
858 uap->dmarx.running = false;
859
860 pending = sgbuf->sg.length - state.residue;
861 BUG_ON(pending > PL011_DMA_BUFFER_SIZE);
862 /* Then we terminate the transfer - we now know our residue */
863 dmaengine_terminate_all(rxchan);
864
865 /*
866 * This will take the chars we have so far and insert
867 * into the framework.
868 */
869 pl011_dma_rx_chars(uap, pending, dmarx->use_buf_b, true);
870
871 /* Switch buffer & re-trigger DMA job */
872 dmarx->use_buf_b = !dmarx->use_buf_b;
873 if (pl011_dma_rx_trigger_dma(uap)) {
874 dev_dbg(uap->port.dev, "could not retrigger RX DMA job "
875 "fall back to interrupt mode\n");
876 uap->im |= UART011_RXIM;
877 writew(uap->im, uap->port.membase + UART011_IMSC);
878 }
879}
880
881static void pl011_dma_rx_callback(void *data)
882{
883 struct uart_amba_port *uap = data;
884 struct pl011_dmarx_data *dmarx = &uap->dmarx;
Chanho Min6dc01aa2012-02-20 10:24:40 +0900885 struct dma_chan *rxchan = dmarx->chan;
Linus Walleijead76f32011-02-24 13:21:08 +0100886 bool lastbuf = dmarx->use_buf_b;
Chanho Min6dc01aa2012-02-20 10:24:40 +0900887 struct pl011_sgbuf *sgbuf = dmarx->use_buf_b ?
888 &dmarx->sgbuf_b : &dmarx->sgbuf_a;
889 size_t pending;
890 struct dma_tx_state state;
Linus Walleijead76f32011-02-24 13:21:08 +0100891 int ret;
892
893 /*
894 * This completion interrupt occurs typically when the
895 * RX buffer is totally stuffed but no timeout has yet
896 * occurred. When that happens, we just want the RX
897 * routine to flush out the secondary DMA buffer while
898 * we immediately trigger the next DMA job.
899 */
900 spin_lock_irq(&uap->port.lock);
Chanho Min6dc01aa2012-02-20 10:24:40 +0900901 /*
902 * Rx data can be taken by the UART interrupts during
903 * the DMA irq handler. So we check the residue here.
904 */
905 rxchan->device->device_tx_status(rxchan, dmarx->cookie, &state);
906 pending = sgbuf->sg.length - state.residue;
907 BUG_ON(pending > PL011_DMA_BUFFER_SIZE);
908 /* Then we terminate the transfer - we now know our residue */
909 dmaengine_terminate_all(rxchan);
910
Linus Walleijead76f32011-02-24 13:21:08 +0100911 uap->dmarx.running = false;
912 dmarx->use_buf_b = !lastbuf;
913 ret = pl011_dma_rx_trigger_dma(uap);
914
Chanho Min6dc01aa2012-02-20 10:24:40 +0900915 pl011_dma_rx_chars(uap, pending, lastbuf, false);
Linus Walleijead76f32011-02-24 13:21:08 +0100916 spin_unlock_irq(&uap->port.lock);
917 /*
918 * Do this check after we picked the DMA chars so we don't
919 * get some IRQ immediately from RX.
920 */
921 if (ret) {
922 dev_dbg(uap->port.dev, "could not retrigger RX DMA job "
923 "fall back to interrupt mode\n");
924 uap->im |= UART011_RXIM;
925 writew(uap->im, uap->port.membase + UART011_IMSC);
926 }
927}
928
929/*
930 * Stop accepting received characters, when we're shutting down or
931 * suspending this port.
932 * Locking: called with port lock held and IRQs disabled.
933 */
934static inline void pl011_dma_rx_stop(struct uart_amba_port *uap)
935{
936 /* FIXME. Just disable the DMA enable */
937 uap->dmacr &= ~UART011_RXDMAE;
938 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
939}
Russell King68b65f72010-12-22 17:24:39 +0000940
Chanho Mincb06ff12013-03-27 18:38:11 +0900941/*
942 * Timer handler for Rx DMA polling.
943 * Every polling, It checks the residue in the dma buffer and transfer
944 * data to the tty. Also, last_residue is updated for the next polling.
945 */
946static void pl011_dma_rx_poll(unsigned long args)
947{
948 struct uart_amba_port *uap = (struct uart_amba_port *)args;
949 struct tty_port *port = &uap->port.state->port;
950 struct pl011_dmarx_data *dmarx = &uap->dmarx;
951 struct dma_chan *rxchan = uap->dmarx.chan;
952 unsigned long flags = 0;
953 unsigned int dmataken = 0;
954 unsigned int size = 0;
955 struct pl011_sgbuf *sgbuf;
956 int dma_count;
957 struct dma_tx_state state;
958
959 sgbuf = dmarx->use_buf_b ? &uap->dmarx.sgbuf_b : &uap->dmarx.sgbuf_a;
960 rxchan->device->device_tx_status(rxchan, dmarx->cookie, &state);
961 if (likely(state.residue < dmarx->last_residue)) {
962 dmataken = sgbuf->sg.length - dmarx->last_residue;
963 size = dmarx->last_residue - state.residue;
964 dma_count = tty_insert_flip_string(port, sgbuf->buf + dmataken,
965 size);
966 if (dma_count == size)
967 dmarx->last_residue = state.residue;
968 dmarx->last_jiffies = jiffies;
969 }
970 tty_flip_buffer_push(port);
971
972 /*
973 * If no data is received in poll_timeout, the driver will fall back
974 * to interrupt mode. We will retrigger DMA at the first interrupt.
975 */
976 if (jiffies_to_msecs(jiffies - dmarx->last_jiffies)
977 > uap->dmarx.poll_timeout) {
978
979 spin_lock_irqsave(&uap->port.lock, flags);
980 pl011_dma_rx_stop(uap);
Guennadi Liakhovetskic25a1ad2013-12-10 14:54:47 +0100981 uap->im |= UART011_RXIM;
982 writew(uap->im, uap->port.membase + UART011_IMSC);
Chanho Mincb06ff12013-03-27 18:38:11 +0900983 spin_unlock_irqrestore(&uap->port.lock, flags);
984
985 uap->dmarx.running = false;
986 dmaengine_terminate_all(rxchan);
987 del_timer(&uap->dmarx.timer);
988 } else {
989 mod_timer(&uap->dmarx.timer,
990 jiffies + msecs_to_jiffies(uap->dmarx.poll_rate));
991 }
992}
993
Russell King68b65f72010-12-22 17:24:39 +0000994static void pl011_dma_startup(struct uart_amba_port *uap)
995{
Linus Walleijead76f32011-02-24 13:21:08 +0100996 int ret;
997
Jorge Ramirez-Ortiz1c9be312015-03-06 13:05:40 -0500998 if (!uap->dma_probed)
999 pl011_dma_probe(uap);
1000
Russell King68b65f72010-12-22 17:24:39 +00001001 if (!uap->dmatx.chan)
1002 return;
1003
Andrew Jackson4c0be452014-11-07 14:14:35 +00001004 uap->dmatx.buf = kmalloc(PL011_DMA_BUFFER_SIZE, GFP_KERNEL | __GFP_DMA);
Russell King68b65f72010-12-22 17:24:39 +00001005 if (!uap->dmatx.buf) {
1006 dev_err(uap->port.dev, "no memory for DMA TX buffer\n");
1007 uap->port.fifosize = uap->fifosize;
1008 return;
1009 }
1010
1011 sg_init_one(&uap->dmatx.sg, uap->dmatx.buf, PL011_DMA_BUFFER_SIZE);
1012
1013 /* The DMA buffer is now the FIFO the TTY subsystem can use */
1014 uap->port.fifosize = PL011_DMA_BUFFER_SIZE;
Linus Walleijead76f32011-02-24 13:21:08 +01001015 uap->using_tx_dma = true;
Russell King68b65f72010-12-22 17:24:39 +00001016
Linus Walleijead76f32011-02-24 13:21:08 +01001017 if (!uap->dmarx.chan)
1018 goto skip_rx;
1019
1020 /* Allocate and map DMA RX buffers */
1021 ret = pl011_sgbuf_init(uap->dmarx.chan, &uap->dmarx.sgbuf_a,
1022 DMA_FROM_DEVICE);
1023 if (ret) {
1024 dev_err(uap->port.dev, "failed to init DMA %s: %d\n",
1025 "RX buffer A", ret);
1026 goto skip_rx;
1027 }
1028
1029 ret = pl011_sgbuf_init(uap->dmarx.chan, &uap->dmarx.sgbuf_b,
1030 DMA_FROM_DEVICE);
1031 if (ret) {
1032 dev_err(uap->port.dev, "failed to init DMA %s: %d\n",
1033 "RX buffer B", ret);
1034 pl011_sgbuf_free(uap->dmarx.chan, &uap->dmarx.sgbuf_a,
1035 DMA_FROM_DEVICE);
1036 goto skip_rx;
1037 }
1038
1039 uap->using_rx_dma = true;
1040
1041skip_rx:
Russell King68b65f72010-12-22 17:24:39 +00001042 /* Turn on DMA error (RX/TX will be enabled on demand) */
1043 uap->dmacr |= UART011_DMAONERR;
1044 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
Russell King38d62432010-12-22 17:59:16 +00001045
1046 /*
1047 * ST Micro variants has some specific dma burst threshold
1048 * compensation. Set this to 16 bytes, so burst will only
1049 * be issued above/below 16 bytes.
1050 */
1051 if (uap->vendor->dma_threshold)
1052 writew(ST_UART011_DMAWM_RX_16 | ST_UART011_DMAWM_TX_16,
1053 uap->port.membase + ST_UART011_DMAWM);
Linus Walleijead76f32011-02-24 13:21:08 +01001054
1055 if (uap->using_rx_dma) {
1056 if (pl011_dma_rx_trigger_dma(uap))
1057 dev_dbg(uap->port.dev, "could not trigger initial "
1058 "RX DMA job, fall back to interrupt mode\n");
Chanho Mincb06ff12013-03-27 18:38:11 +09001059 if (uap->dmarx.poll_rate) {
1060 init_timer(&(uap->dmarx.timer));
1061 uap->dmarx.timer.function = pl011_dma_rx_poll;
1062 uap->dmarx.timer.data = (unsigned long)uap;
1063 mod_timer(&uap->dmarx.timer,
1064 jiffies +
1065 msecs_to_jiffies(uap->dmarx.poll_rate));
1066 uap->dmarx.last_residue = PL011_DMA_BUFFER_SIZE;
1067 uap->dmarx.last_jiffies = jiffies;
1068 }
Linus Walleijead76f32011-02-24 13:21:08 +01001069 }
Russell King68b65f72010-12-22 17:24:39 +00001070}
1071
1072static void pl011_dma_shutdown(struct uart_amba_port *uap)
1073{
Linus Walleijead76f32011-02-24 13:21:08 +01001074 if (!(uap->using_tx_dma || uap->using_rx_dma))
Russell King68b65f72010-12-22 17:24:39 +00001075 return;
1076
1077 /* Disable RX and TX DMA */
1078 while (readw(uap->port.membase + UART01x_FR) & UART01x_FR_BUSY)
1079 barrier();
1080
1081 spin_lock_irq(&uap->port.lock);
1082 uap->dmacr &= ~(UART011_DMAONERR | UART011_RXDMAE | UART011_TXDMAE);
1083 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
1084 spin_unlock_irq(&uap->port.lock);
1085
Linus Walleijead76f32011-02-24 13:21:08 +01001086 if (uap->using_tx_dma) {
1087 /* In theory, this should already be done by pl011_dma_flush_buffer */
1088 dmaengine_terminate_all(uap->dmatx.chan);
1089 if (uap->dmatx.queued) {
1090 dma_unmap_sg(uap->dmatx.chan->device->dev, &uap->dmatx.sg, 1,
1091 DMA_TO_DEVICE);
1092 uap->dmatx.queued = false;
1093 }
1094
1095 kfree(uap->dmatx.buf);
1096 uap->using_tx_dma = false;
Russell King68b65f72010-12-22 17:24:39 +00001097 }
1098
Linus Walleijead76f32011-02-24 13:21:08 +01001099 if (uap->using_rx_dma) {
1100 dmaengine_terminate_all(uap->dmarx.chan);
1101 /* Clean up the RX DMA */
1102 pl011_sgbuf_free(uap->dmarx.chan, &uap->dmarx.sgbuf_a, DMA_FROM_DEVICE);
1103 pl011_sgbuf_free(uap->dmarx.chan, &uap->dmarx.sgbuf_b, DMA_FROM_DEVICE);
Chanho Mincb06ff12013-03-27 18:38:11 +09001104 if (uap->dmarx.poll_rate)
1105 del_timer_sync(&uap->dmarx.timer);
Linus Walleijead76f32011-02-24 13:21:08 +01001106 uap->using_rx_dma = false;
1107 }
Russell King68b65f72010-12-22 17:24:39 +00001108}
1109
Linus Walleijead76f32011-02-24 13:21:08 +01001110static inline bool pl011_dma_rx_available(struct uart_amba_port *uap)
1111{
1112 return uap->using_rx_dma;
1113}
1114
1115static inline bool pl011_dma_rx_running(struct uart_amba_port *uap)
1116{
1117 return uap->using_rx_dma && uap->dmarx.running;
1118}
1119
Russell King68b65f72010-12-22 17:24:39 +00001120#else
1121/* Blank functions if the DMA engine is not available */
Jorge Ramirez-Ortiz1c9be312015-03-06 13:05:40 -05001122static inline void pl011_dma_probe(struct uart_amba_port *uap)
Russell King68b65f72010-12-22 17:24:39 +00001123{
1124}
1125
1126static inline void pl011_dma_remove(struct uart_amba_port *uap)
1127{
1128}
1129
1130static inline void pl011_dma_startup(struct uart_amba_port *uap)
1131{
1132}
1133
1134static inline void pl011_dma_shutdown(struct uart_amba_port *uap)
1135{
1136}
1137
1138static inline bool pl011_dma_tx_irq(struct uart_amba_port *uap)
1139{
1140 return false;
1141}
1142
1143static inline void pl011_dma_tx_stop(struct uart_amba_port *uap)
1144{
1145}
1146
1147static inline bool pl011_dma_tx_start(struct uart_amba_port *uap)
1148{
1149 return false;
1150}
1151
Linus Walleijead76f32011-02-24 13:21:08 +01001152static inline void pl011_dma_rx_irq(struct uart_amba_port *uap)
1153{
1154}
1155
1156static inline void pl011_dma_rx_stop(struct uart_amba_port *uap)
1157{
1158}
1159
1160static inline int pl011_dma_rx_trigger_dma(struct uart_amba_port *uap)
1161{
1162 return -EIO;
1163}
1164
1165static inline bool pl011_dma_rx_available(struct uart_amba_port *uap)
1166{
1167 return false;
1168}
1169
1170static inline bool pl011_dma_rx_running(struct uart_amba_port *uap)
1171{
1172 return false;
1173}
1174
Russell King68b65f72010-12-22 17:24:39 +00001175#define pl011_dma_flush_buffer NULL
1176#endif
1177
Russell Kingb129a8c2005-08-31 10:12:14 +01001178static void pl011_stop_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001180 struct uart_amba_port *uap =
1181 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182
1183 uap->im &= ~UART011_TXIM;
1184 writew(uap->im, uap->port.membase + UART011_IMSC);
Russell King68b65f72010-12-22 17:24:39 +00001185 pl011_dma_tx_stop(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186}
1187
Dave Martin1e84d222015-04-27 16:49:05 +01001188static void pl011_tx_chars(struct uart_amba_port *uap, bool from_irq);
Dave Martin734745c2015-03-04 12:27:33 +00001189
1190/* Start TX with programmed I/O only (no DMA) */
1191static void pl011_start_tx_pio(struct uart_amba_port *uap)
1192{
1193 uap->im |= UART011_TXIM;
1194 writew(uap->im, uap->port.membase + UART011_IMSC);
Dave Martin1e84d222015-04-27 16:49:05 +01001195 pl011_tx_chars(uap, false);
Dave Martin734745c2015-03-04 12:27:33 +00001196}
1197
Russell Kingb129a8c2005-08-31 10:12:14 +01001198static void pl011_start_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001200 struct uart_amba_port *uap =
1201 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202
Dave Martin734745c2015-03-04 12:27:33 +00001203 if (!pl011_dma_tx_start(uap))
1204 pl011_start_tx_pio(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205}
1206
1207static void pl011_stop_rx(struct uart_port *port)
1208{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001209 struct uart_amba_port *uap =
1210 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211
1212 uap->im &= ~(UART011_RXIM|UART011_RTIM|UART011_FEIM|
1213 UART011_PEIM|UART011_BEIM|UART011_OEIM);
1214 writew(uap->im, uap->port.membase + UART011_IMSC);
Linus Walleijead76f32011-02-24 13:21:08 +01001215
1216 pl011_dma_rx_stop(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217}
1218
1219static void pl011_enable_ms(struct uart_port *port)
1220{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001221 struct uart_amba_port *uap =
1222 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223
1224 uap->im |= UART011_RIMIM|UART011_CTSMIM|UART011_DCDMIM|UART011_DSRMIM;
1225 writew(uap->im, uap->port.membase + UART011_IMSC);
1226}
1227
David Howells7d12e782006-10-05 14:55:46 +01001228static void pl011_rx_chars(struct uart_amba_port *uap)
Fabio Estevamb83286b2013-08-09 17:58:51 -03001229__releases(&uap->port.lock)
1230__acquires(&uap->port.lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231{
Linus Walleij29772c42011-02-24 13:21:36 +01001232 pl011_fifo_to_tty(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233
Thomas Gleixner2389b272007-05-29 21:53:50 +01001234 spin_unlock(&uap->port.lock);
Jiri Slaby2e124b42013-01-03 15:53:06 +01001235 tty_flip_buffer_push(&uap->port.state->port);
Linus Walleijead76f32011-02-24 13:21:08 +01001236 /*
1237 * If we were temporarily out of DMA mode for a while,
1238 * attempt to switch back to DMA mode again.
1239 */
1240 if (pl011_dma_rx_available(uap)) {
1241 if (pl011_dma_rx_trigger_dma(uap)) {
1242 dev_dbg(uap->port.dev, "could not trigger RX DMA job "
1243 "fall back to interrupt mode again\n");
1244 uap->im |= UART011_RXIM;
Guennadi Liakhovetski30ae5852013-12-10 14:54:42 +01001245 writew(uap->im, uap->port.membase + UART011_IMSC);
Chanho Mincb06ff12013-03-27 18:38:11 +09001246 } else {
Chanho Min89fa28d2013-04-03 11:10:37 +09001247#ifdef CONFIG_DMA_ENGINE
Chanho Mincb06ff12013-03-27 18:38:11 +09001248 /* Start Rx DMA poll */
1249 if (uap->dmarx.poll_rate) {
1250 uap->dmarx.last_jiffies = jiffies;
1251 uap->dmarx.last_residue = PL011_DMA_BUFFER_SIZE;
1252 mod_timer(&uap->dmarx.timer,
1253 jiffies +
1254 msecs_to_jiffies(uap->dmarx.poll_rate));
1255 }
Chanho Min89fa28d2013-04-03 11:10:37 +09001256#endif
Chanho Mincb06ff12013-03-27 18:38:11 +09001257 }
Linus Walleijead76f32011-02-24 13:21:08 +01001258 }
Thomas Gleixner2389b272007-05-29 21:53:50 +01001259 spin_lock(&uap->port.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260}
1261
Dave Martin1e84d222015-04-27 16:49:05 +01001262static bool pl011_tx_char(struct uart_amba_port *uap, unsigned char c,
1263 bool from_irq)
Dave Martin734745c2015-03-04 12:27:33 +00001264{
Dave Martin1e84d222015-04-27 16:49:05 +01001265 if (unlikely(!from_irq) &&
1266 readw(uap->port.membase + UART01x_FR) & UART01x_FR_TXFF)
1267 return false; /* unable to transmit character */
1268
Dave Martin734745c2015-03-04 12:27:33 +00001269 writew(c, uap->port.membase + UART01x_DR);
1270 uap->port.icount.tx++;
1271
Dave Martin1e84d222015-04-27 16:49:05 +01001272 return true;
Dave Martin734745c2015-03-04 12:27:33 +00001273}
1274
Dave Martin1e84d222015-04-27 16:49:05 +01001275static void pl011_tx_chars(struct uart_amba_port *uap, bool from_irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276{
Alan Coxebd2c8f2009-09-19 13:13:28 -07001277 struct circ_buf *xmit = &uap->port.state->xmit;
Dave Martin1e84d222015-04-27 16:49:05 +01001278 int count = uap->fifosize >> 1;
Dave Martin734745c2015-03-04 12:27:33 +00001279
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 if (uap->port.x_char) {
Dave Martin1e84d222015-04-27 16:49:05 +01001281 if (!pl011_tx_char(uap, uap->port.x_char, from_irq))
1282 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 uap->port.x_char = 0;
Dave Martin734745c2015-03-04 12:27:33 +00001284 --count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 }
1286 if (uart_circ_empty(xmit) || uart_tx_stopped(&uap->port)) {
Russell Kingb129a8c2005-08-31 10:12:14 +01001287 pl011_stop_tx(&uap->port);
Dave Martin1e84d222015-04-27 16:49:05 +01001288 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 }
1290
Russell King68b65f72010-12-22 17:24:39 +00001291 /* If we are using DMA mode, try to send some characters. */
1292 if (pl011_dma_tx_irq(uap))
Dave Martin1e84d222015-04-27 16:49:05 +01001293 return;
Russell King68b65f72010-12-22 17:24:39 +00001294
Dave Martin1e84d222015-04-27 16:49:05 +01001295 do {
1296 if (likely(from_irq) && count-- == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 break;
Dave Martin1e84d222015-04-27 16:49:05 +01001298
1299 if (!pl011_tx_char(uap, xmit->buf[xmit->tail], from_irq))
1300 break;
1301
1302 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
1303 } while (!uart_circ_empty(xmit));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304
1305 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
1306 uart_write_wakeup(&uap->port);
1307
Dave Martin1e84d222015-04-27 16:49:05 +01001308 if (uart_circ_empty(xmit))
Russell Kingb129a8c2005-08-31 10:12:14 +01001309 pl011_stop_tx(&uap->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310}
1311
1312static void pl011_modem_status(struct uart_amba_port *uap)
1313{
1314 unsigned int status, delta;
1315
1316 status = readw(uap->port.membase + UART01x_FR) & UART01x_FR_MODEM_ANY;
1317
1318 delta = status ^ uap->old_status;
1319 uap->old_status = status;
1320
1321 if (!delta)
1322 return;
1323
1324 if (delta & UART01x_FR_DCD)
1325 uart_handle_dcd_change(&uap->port, status & UART01x_FR_DCD);
1326
1327 if (delta & UART01x_FR_DSR)
1328 uap->port.icount.dsr++;
1329
1330 if (delta & UART01x_FR_CTS)
1331 uart_handle_cts_change(&uap->port, status & UART01x_FR_CTS);
1332
Alan Coxbdc04e32009-09-19 13:13:31 -07001333 wake_up_interruptible(&uap->port.state->port.delta_msr_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334}
1335
Andre Przywara9c4ef4b2015-05-21 17:26:20 +01001336static void check_apply_cts_event_workaround(struct uart_amba_port *uap)
1337{
1338 unsigned int dummy_read;
1339
1340 if (!uap->vendor->cts_event_workaround)
1341 return;
1342
1343 /* workaround to make sure that all bits are unlocked.. */
1344 writew(0x00, uap->port.membase + UART011_ICR);
1345
1346 /*
1347 * WA: introduce 26ns(1 uart clk) delay before W1C;
1348 * single apb access will incur 2 pclk(133.12Mhz) delay,
1349 * so add 2 dummy reads
1350 */
1351 dummy_read = readw(uap->port.membase + UART011_ICR);
1352 dummy_read = readw(uap->port.membase + UART011_ICR);
1353}
1354
David Howells7d12e782006-10-05 14:55:46 +01001355static irqreturn_t pl011_int(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356{
1357 struct uart_amba_port *uap = dev_id;
Russell King963cc982010-12-22 17:16:09 +00001358 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 unsigned int status, pass_counter = AMBA_ISR_PASS_LIMIT;
Andre Przywara075167e2015-05-21 17:26:19 +01001360 u16 imsc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 int handled = 0;
1362
Russell King963cc982010-12-22 17:16:09 +00001363 spin_lock_irqsave(&uap->port.lock, flags);
Andre Przywara075167e2015-05-21 17:26:19 +01001364 imsc = readw(uap->port.membase + UART011_IMSC);
1365 status = readw(uap->port.membase + UART011_RIS) & imsc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 if (status) {
1367 do {
Andre Przywara9c4ef4b2015-05-21 17:26:20 +01001368 check_apply_cts_event_workaround(uap);
Rajanikanth H.V4fd06902012-03-26 11:17:02 +02001369
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 writew(status & ~(UART011_TXIS|UART011_RTIS|
1371 UART011_RXIS),
1372 uap->port.membase + UART011_ICR);
1373
Linus Walleijead76f32011-02-24 13:21:08 +01001374 if (status & (UART011_RTIS|UART011_RXIS)) {
1375 if (pl011_dma_rx_running(uap))
1376 pl011_dma_rx_irq(uap);
1377 else
1378 pl011_rx_chars(uap);
1379 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 if (status & (UART011_DSRMIS|UART011_DCDMIS|
1381 UART011_CTSMIS|UART011_RIMIS))
1382 pl011_modem_status(uap);
Dave Martin1e84d222015-04-27 16:49:05 +01001383 if (status & UART011_TXIS)
1384 pl011_tx_chars(uap, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385
Rajanikanth H.V4fd06902012-03-26 11:17:02 +02001386 if (pass_counter-- == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 break;
1388
Andre Przywara075167e2015-05-21 17:26:19 +01001389 status = readw(uap->port.membase + UART011_RIS) & imsc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 } while (status != 0);
1391 handled = 1;
1392 }
1393
Russell King963cc982010-12-22 17:16:09 +00001394 spin_unlock_irqrestore(&uap->port.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395
1396 return IRQ_RETVAL(handled);
1397}
1398
Linus Walleije643f872012-06-17 15:44:19 +02001399static unsigned int pl011_tx_empty(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001401 struct uart_amba_port *uap =
1402 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 unsigned int status = readw(uap->port.membase + UART01x_FR);
1404 return status & (UART01x_FR_BUSY|UART01x_FR_TXFF) ? 0 : TIOCSER_TEMT;
1405}
1406
Linus Walleije643f872012-06-17 15:44:19 +02001407static unsigned int pl011_get_mctrl(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001409 struct uart_amba_port *uap =
1410 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 unsigned int result = 0;
1412 unsigned int status = readw(uap->port.membase + UART01x_FR);
1413
Jiri Slaby5159f402007-10-18 23:40:31 -07001414#define TIOCMBIT(uartbit, tiocmbit) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 if (status & uartbit) \
1416 result |= tiocmbit
1417
Jiri Slaby5159f402007-10-18 23:40:31 -07001418 TIOCMBIT(UART01x_FR_DCD, TIOCM_CAR);
1419 TIOCMBIT(UART01x_FR_DSR, TIOCM_DSR);
1420 TIOCMBIT(UART01x_FR_CTS, TIOCM_CTS);
1421 TIOCMBIT(UART011_FR_RI, TIOCM_RNG);
1422#undef TIOCMBIT
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 return result;
1424}
1425
1426static void pl011_set_mctrl(struct uart_port *port, unsigned int mctrl)
1427{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001428 struct uart_amba_port *uap =
1429 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 unsigned int cr;
1431
1432 cr = readw(uap->port.membase + UART011_CR);
1433
Jiri Slaby5159f402007-10-18 23:40:31 -07001434#define TIOCMBIT(tiocmbit, uartbit) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 if (mctrl & tiocmbit) \
1436 cr |= uartbit; \
1437 else \
1438 cr &= ~uartbit
1439
Jiri Slaby5159f402007-10-18 23:40:31 -07001440 TIOCMBIT(TIOCM_RTS, UART011_CR_RTS);
1441 TIOCMBIT(TIOCM_DTR, UART011_CR_DTR);
1442 TIOCMBIT(TIOCM_OUT1, UART011_CR_OUT1);
1443 TIOCMBIT(TIOCM_OUT2, UART011_CR_OUT2);
1444 TIOCMBIT(TIOCM_LOOP, UART011_CR_LBE);
Rabin Vincent3b438162010-02-12 06:43:11 +01001445
1446 if (uap->autorts) {
1447 /* We need to disable auto-RTS if we want to turn RTS off */
1448 TIOCMBIT(TIOCM_RTS, UART011_CR_RTSEN);
1449 }
Jiri Slaby5159f402007-10-18 23:40:31 -07001450#undef TIOCMBIT
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451
1452 writew(cr, uap->port.membase + UART011_CR);
1453}
1454
1455static void pl011_break_ctl(struct uart_port *port, int break_state)
1456{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001457 struct uart_amba_port *uap =
1458 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 unsigned long flags;
1460 unsigned int lcr_h;
1461
1462 spin_lock_irqsave(&uap->port.lock, flags);
Linus Walleijec489aa2010-06-02 08:13:52 +01001463 lcr_h = readw(uap->port.membase + uap->lcrh_tx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 if (break_state == -1)
1465 lcr_h |= UART01x_LCRH_BRK;
1466 else
1467 lcr_h &= ~UART01x_LCRH_BRK;
Linus Walleijec489aa2010-06-02 08:13:52 +01001468 writew(lcr_h, uap->port.membase + uap->lcrh_tx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 spin_unlock_irqrestore(&uap->port.lock, flags);
1470}
1471
Jason Wessel84b5ae12008-02-20 13:33:39 -06001472#ifdef CONFIG_CONSOLE_POLL
Anton Vorontsov5c8124a2012-09-24 14:27:55 -07001473
1474static void pl011_quiesce_irqs(struct uart_port *port)
1475{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001476 struct uart_amba_port *uap =
1477 container_of(port, struct uart_amba_port, port);
Anton Vorontsov5c8124a2012-09-24 14:27:55 -07001478 unsigned char __iomem *regs = uap->port.membase;
1479
1480 writew(readw(regs + UART011_MIS), regs + UART011_ICR);
1481 /*
1482 * There is no way to clear TXIM as this is "ready to transmit IRQ", so
1483 * we simply mask it. start_tx() will unmask it.
1484 *
1485 * Note we can race with start_tx(), and if the race happens, the
1486 * polling user might get another interrupt just after we clear it.
1487 * But it should be OK and can happen even w/o the race, e.g.
1488 * controller immediately got some new data and raised the IRQ.
1489 *
1490 * And whoever uses polling routines assumes that it manages the device
1491 * (including tx queue), so we're also fine with start_tx()'s caller
1492 * side.
1493 */
1494 writew(readw(regs + UART011_IMSC) & ~UART011_TXIM, regs + UART011_IMSC);
1495}
1496
Linus Walleije643f872012-06-17 15:44:19 +02001497static int pl011_get_poll_char(struct uart_port *port)
Jason Wessel84b5ae12008-02-20 13:33:39 -06001498{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001499 struct uart_amba_port *uap =
1500 container_of(port, struct uart_amba_port, port);
Jason Wessel84b5ae12008-02-20 13:33:39 -06001501 unsigned int status;
1502
Anton Vorontsov5c8124a2012-09-24 14:27:55 -07001503 /*
1504 * The caller might need IRQs lowered, e.g. if used with KDB NMI
1505 * debugger.
1506 */
1507 pl011_quiesce_irqs(port);
1508
Jason Wesself5316b42010-05-20 21:04:22 -05001509 status = readw(uap->port.membase + UART01x_FR);
1510 if (status & UART01x_FR_RXFE)
1511 return NO_POLL_CHAR;
Jason Wessel84b5ae12008-02-20 13:33:39 -06001512
1513 return readw(uap->port.membase + UART01x_DR);
1514}
1515
Linus Walleije643f872012-06-17 15:44:19 +02001516static void pl011_put_poll_char(struct uart_port *port,
Jason Wessel84b5ae12008-02-20 13:33:39 -06001517 unsigned char ch)
1518{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001519 struct uart_amba_port *uap =
1520 container_of(port, struct uart_amba_port, port);
Jason Wessel84b5ae12008-02-20 13:33:39 -06001521
1522 while (readw(uap->port.membase + UART01x_FR) & UART01x_FR_TXFF)
1523 barrier();
1524
1525 writew(ch, uap->port.membase + UART01x_DR);
1526}
1527
1528#endif /* CONFIG_CONSOLE_POLL */
1529
Anton Vorontsovb3564c22012-09-24 14:27:54 -07001530static int pl011_hwinit(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001532 struct uart_amba_port *uap =
1533 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 int retval;
1535
Linus Walleij78d80c52012-05-23 21:18:46 +02001536 /* Optionaly enable pins to be muxed in and configured */
Linus Walleij2b996fc2013-06-05 15:36:42 +02001537 pinctrl_pm_select_default_state(port->dev);
Linus Walleij78d80c52012-05-23 21:18:46 +02001538
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 /*
1540 * Try to enable the clock producer.
1541 */
Julia Lawall1c4c4392012-08-26 18:01:01 +02001542 retval = clk_prepare_enable(uap->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 if (retval)
Tushar Behera7f6d9422014-06-26 15:35:35 +05301544 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545
1546 uap->port.uartclk = clk_get_rate(uap->clk);
1547
Linus Walleij9b96fba2012-03-13 13:27:23 +01001548 /* Clear pending error and receive interrupts */
1549 writew(UART011_OEIS | UART011_BEIS | UART011_PEIS | UART011_FEIS |
1550 UART011_RTIS | UART011_RXIS, uap->port.membase + UART011_ICR);
1551
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 /*
Anton Vorontsovb3564c22012-09-24 14:27:54 -07001553 * Save interrupts enable mask, and enable RX interrupts in case if
1554 * the interrupt is used for NMI entry.
1555 */
1556 uap->im = readw(uap->port.membase + UART011_IMSC);
1557 writew(UART011_RTIM | UART011_RXIM, uap->port.membase + UART011_IMSC);
1558
Jingoo Han574de552013-07-30 17:06:57 +09001559 if (dev_get_platdata(uap->port.dev)) {
Anton Vorontsovb3564c22012-09-24 14:27:54 -07001560 struct amba_pl011_data *plat;
1561
Jingoo Han574de552013-07-30 17:06:57 +09001562 plat = dev_get_platdata(uap->port.dev);
Anton Vorontsovb3564c22012-09-24 14:27:54 -07001563 if (plat->init)
1564 plat->init();
1565 }
1566 return 0;
Anton Vorontsovb3564c22012-09-24 14:27:54 -07001567}
1568
Jon Medhurstb60f2f62013-12-10 10:18:59 +00001569static void pl011_write_lcr_h(struct uart_amba_port *uap, unsigned int lcr_h)
1570{
1571 writew(lcr_h, uap->port.membase + uap->lcrh_rx);
1572 if (uap->lcrh_rx != uap->lcrh_tx) {
1573 int i;
1574 /*
1575 * Wait 10 PCLKs before writing LCRH_TX register,
1576 * to get this delay write read only register 10 times
1577 */
1578 for (i = 0; i < 10; ++i)
1579 writew(0xff, uap->port.membase + UART011_MIS);
1580 writew(lcr_h, uap->port.membase + uap->lcrh_tx);
1581 }
1582}
1583
Andre Przywara867b8e82015-05-21 17:26:15 +01001584static int pl011_allocate_irq(struct uart_amba_port *uap)
1585{
1586 writew(uap->im, uap->port.membase + UART011_IMSC);
1587
1588 return request_irq(uap->port.irq, pl011_int, 0, "uart-pl011", uap);
1589}
1590
1591/*
1592 * Enable interrupts, only timeouts when using DMA
1593 * if initial RX DMA job failed, start in interrupt mode
1594 * as well.
1595 */
1596static void pl011_enable_interrupts(struct uart_amba_port *uap)
1597{
1598 spin_lock_irq(&uap->port.lock);
1599
1600 /* Clear out any spuriously appearing RX interrupts */
1601 writew(UART011_RTIS | UART011_RXIS,
1602 uap->port.membase + UART011_ICR);
1603 uap->im = UART011_RTIM;
1604 if (!pl011_dma_rx_running(uap))
1605 uap->im |= UART011_RXIM;
1606 writew(uap->im, uap->port.membase + UART011_IMSC);
1607 spin_unlock_irq(&uap->port.lock);
1608}
1609
Anton Vorontsovb3564c22012-09-24 14:27:54 -07001610static int pl011_startup(struct uart_port *port)
1611{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001612 struct uart_amba_port *uap =
1613 container_of(port, struct uart_amba_port, port);
Dave Martin734745c2015-03-04 12:27:33 +00001614 unsigned int cr;
Anton Vorontsovb3564c22012-09-24 14:27:54 -07001615 int retval;
1616
1617 retval = pl011_hwinit(port);
1618 if (retval)
1619 goto clk_dis;
1620
Andre Przywara867b8e82015-05-21 17:26:15 +01001621 retval = pl011_allocate_irq(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 if (retval)
1623 goto clk_dis;
1624
Russell Kingc19f12b2010-12-22 17:48:26 +00001625 writew(uap->vendor->ifls, uap->port.membase + UART011_IFLS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626
Jon Medhurstfe433902013-12-10 10:18:58 +00001627 spin_lock_irq(&uap->port.lock);
1628
Shreshtha Kumar Sahud8d8ffa2012-01-18 15:53:59 +05301629 /* restore RTS and DTR */
1630 cr = uap->old_cr & (UART011_CR_RTS | UART011_CR_DTR);
1631 cr |= UART01x_CR_UARTEN | UART011_CR_RXE | UART011_CR_TXE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 writew(cr, uap->port.membase + UART011_CR);
1633
Jon Medhurstfe433902013-12-10 10:18:58 +00001634 spin_unlock_irq(&uap->port.lock);
1635
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 /*
1637 * initialise the old status of the modem signals
1638 */
1639 uap->old_status = readw(uap->port.membase + UART01x_FR) & UART01x_FR_MODEM_ANY;
1640
Russell King68b65f72010-12-22 17:24:39 +00001641 /* Startup DMA */
1642 pl011_dma_startup(uap);
1643
Andre Przywara867b8e82015-05-21 17:26:15 +01001644 pl011_enable_interrupts(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645
1646 return 0;
1647
1648 clk_dis:
Julia Lawall1c4c4392012-08-26 18:01:01 +02001649 clk_disable_unprepare(uap->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 return retval;
1651}
1652
Andre Przywara0dd1e242015-05-21 17:26:23 +01001653static int sbsa_uart_startup(struct uart_port *port)
1654{
1655 struct uart_amba_port *uap =
1656 container_of(port, struct uart_amba_port, port);
1657 int retval;
1658
1659 retval = pl011_hwinit(port);
1660 if (retval)
1661 return retval;
1662
1663 retval = pl011_allocate_irq(uap);
1664 if (retval)
1665 return retval;
1666
1667 /* The SBSA UART does not support any modem status lines. */
1668 uap->old_status = 0;
1669
1670 pl011_enable_interrupts(uap);
1671
1672 return 0;
1673}
1674
Linus Walleijec489aa2010-06-02 08:13:52 +01001675static void pl011_shutdown_channel(struct uart_amba_port *uap,
1676 unsigned int lcrh)
1677{
1678 unsigned long val;
1679
1680 val = readw(uap->port.membase + lcrh);
1681 val &= ~(UART01x_LCRH_BRK | UART01x_LCRH_FEN);
1682 writew(val, uap->port.membase + lcrh);
1683}
1684
Andre Przywara95166a32015-05-21 17:26:16 +01001685/*
1686 * disable the port. It should not disable RTS and DTR.
1687 * Also RTS and DTR state should be preserved to restore
1688 * it during startup().
1689 */
1690static void pl011_disable_uart(struct uart_amba_port *uap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691{
Shreshtha Kumar Sahud8d8ffa2012-01-18 15:53:59 +05301692 unsigned int cr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693
Rabin Vincent3b438162010-02-12 06:43:11 +01001694 uap->autorts = false;
Jon Medhurstfe433902013-12-10 10:18:58 +00001695 spin_lock_irq(&uap->port.lock);
Shreshtha Kumar Sahud8d8ffa2012-01-18 15:53:59 +05301696 cr = readw(uap->port.membase + UART011_CR);
1697 uap->old_cr = cr;
1698 cr &= UART011_CR_RTS | UART011_CR_DTR;
1699 cr |= UART01x_CR_UARTEN | UART011_CR_TXE;
1700 writew(cr, uap->port.membase + UART011_CR);
Jon Medhurstfe433902013-12-10 10:18:58 +00001701 spin_unlock_irq(&uap->port.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702
1703 /*
1704 * disable break condition and fifos
1705 */
Linus Walleijec489aa2010-06-02 08:13:52 +01001706 pl011_shutdown_channel(uap, uap->lcrh_rx);
1707 if (uap->lcrh_rx != uap->lcrh_tx)
1708 pl011_shutdown_channel(uap, uap->lcrh_tx);
Andre Przywara95166a32015-05-21 17:26:16 +01001709}
1710
1711static void pl011_disable_interrupts(struct uart_amba_port *uap)
1712{
1713 spin_lock_irq(&uap->port.lock);
1714
1715 /* mask all interrupts and clear all pending ones */
1716 uap->im = 0;
1717 writew(uap->im, uap->port.membase + UART011_IMSC);
1718 writew(0xffff, uap->port.membase + UART011_ICR);
1719
1720 spin_unlock_irq(&uap->port.lock);
1721}
1722
1723static void pl011_shutdown(struct uart_port *port)
1724{
1725 struct uart_amba_port *uap =
1726 container_of(port, struct uart_amba_port, port);
1727
1728 pl011_disable_interrupts(uap);
1729
1730 pl011_dma_shutdown(uap);
1731
1732 free_irq(uap->port.irq, uap);
1733
1734 pl011_disable_uart(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735
1736 /*
1737 * Shut down the clock producer
1738 */
Julia Lawall1c4c4392012-08-26 18:01:01 +02001739 clk_disable_unprepare(uap->clk);
Linus Walleij78d80c52012-05-23 21:18:46 +02001740 /* Optionally let pins go into sleep states */
Linus Walleij2b996fc2013-06-05 15:36:42 +02001741 pinctrl_pm_select_sleep_state(port->dev);
Shreshtha Kumar Sahuc16d51a2011-06-13 10:11:33 +02001742
Jingoo Han574de552013-07-30 17:06:57 +09001743 if (dev_get_platdata(uap->port.dev)) {
Shreshtha Kumar Sahuc16d51a2011-06-13 10:11:33 +02001744 struct amba_pl011_data *plat;
1745
Jingoo Han574de552013-07-30 17:06:57 +09001746 plat = dev_get_platdata(uap->port.dev);
Shreshtha Kumar Sahuc16d51a2011-06-13 10:11:33 +02001747 if (plat->exit)
1748 plat->exit();
1749 }
1750
Peter Hurley36f339d2014-11-06 09:06:12 -05001751 if (uap->port.ops->flush_buffer)
1752 uap->port.ops->flush_buffer(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753}
1754
Andre Przywara0dd1e242015-05-21 17:26:23 +01001755static void sbsa_uart_shutdown(struct uart_port *port)
1756{
1757 struct uart_amba_port *uap =
1758 container_of(port, struct uart_amba_port, port);
1759
1760 pl011_disable_interrupts(uap);
1761
1762 free_irq(uap->port.irq, uap);
1763
1764 if (uap->port.ops->flush_buffer)
1765 uap->port.ops->flush_buffer(port);
1766}
1767
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768static void
Andre Przywaraef5a9352015-05-21 17:26:17 +01001769pl011_setup_status_masks(struct uart_port *port, struct ktermios *termios)
1770{
1771 port->read_status_mask = UART011_DR_OE | 255;
1772 if (termios->c_iflag & INPCK)
1773 port->read_status_mask |= UART011_DR_FE | UART011_DR_PE;
1774 if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
1775 port->read_status_mask |= UART011_DR_BE;
1776
1777 /*
1778 * Characters to ignore
1779 */
1780 port->ignore_status_mask = 0;
1781 if (termios->c_iflag & IGNPAR)
1782 port->ignore_status_mask |= UART011_DR_FE | UART011_DR_PE;
1783 if (termios->c_iflag & IGNBRK) {
1784 port->ignore_status_mask |= UART011_DR_BE;
1785 /*
1786 * If we're ignoring parity and break indicators,
1787 * ignore overruns too (for real raw support).
1788 */
1789 if (termios->c_iflag & IGNPAR)
1790 port->ignore_status_mask |= UART011_DR_OE;
1791 }
1792
1793 /*
1794 * Ignore all characters if CREAD is not set.
1795 */
1796 if ((termios->c_cflag & CREAD) == 0)
1797 port->ignore_status_mask |= UART_DUMMY_DR_RX;
1798}
1799
1800static void
Alan Cox606d0992006-12-08 02:38:45 -08001801pl011_set_termios(struct uart_port *port, struct ktermios *termios,
1802 struct ktermios *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001804 struct uart_amba_port *uap =
1805 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 unsigned int lcr_h, old_cr;
1807 unsigned long flags;
Russell Kingc19f12b2010-12-22 17:48:26 +00001808 unsigned int baud, quot, clkdiv;
1809
1810 if (uap->vendor->oversampling)
1811 clkdiv = 8;
1812 else
1813 clkdiv = 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814
1815 /*
1816 * Ask the core to calculate the divisor for us.
1817 */
Linus Walleijac3e3fb2010-06-02 20:40:22 +01001818 baud = uart_get_baud_rate(port, termios, old, 0,
Russell Kingc19f12b2010-12-22 17:48:26 +00001819 port->uartclk / clkdiv);
Chanho Min89fa28d2013-04-03 11:10:37 +09001820#ifdef CONFIG_DMA_ENGINE
Chanho Mincb06ff12013-03-27 18:38:11 +09001821 /*
1822 * Adjust RX DMA polling rate with baud rate if not specified.
1823 */
1824 if (uap->dmarx.auto_poll_rate)
1825 uap->dmarx.poll_rate = DIV_ROUND_UP(10000000, baud);
Chanho Min89fa28d2013-04-03 11:10:37 +09001826#endif
Linus Walleijac3e3fb2010-06-02 20:40:22 +01001827
1828 if (baud > port->uartclk/16)
1829 quot = DIV_ROUND_CLOSEST(port->uartclk * 8, baud);
1830 else
1831 quot = DIV_ROUND_CLOSEST(port->uartclk * 4, baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832
1833 switch (termios->c_cflag & CSIZE) {
1834 case CS5:
1835 lcr_h = UART01x_LCRH_WLEN_5;
1836 break;
1837 case CS6:
1838 lcr_h = UART01x_LCRH_WLEN_6;
1839 break;
1840 case CS7:
1841 lcr_h = UART01x_LCRH_WLEN_7;
1842 break;
1843 default: // CS8
1844 lcr_h = UART01x_LCRH_WLEN_8;
1845 break;
1846 }
1847 if (termios->c_cflag & CSTOPB)
1848 lcr_h |= UART01x_LCRH_STP2;
1849 if (termios->c_cflag & PARENB) {
1850 lcr_h |= UART01x_LCRH_PEN;
1851 if (!(termios->c_cflag & PARODD))
1852 lcr_h |= UART01x_LCRH_EPS;
1853 }
Russell Kingffca2b12010-12-22 17:13:05 +00001854 if (uap->fifosize > 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 lcr_h |= UART01x_LCRH_FEN;
1856
1857 spin_lock_irqsave(&port->lock, flags);
1858
1859 /*
1860 * Update the per-port timeout.
1861 */
1862 uart_update_timeout(port, termios->c_cflag, baud);
1863
Andre Przywaraef5a9352015-05-21 17:26:17 +01001864 pl011_setup_status_masks(port, termios);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865
1866 if (UART_ENABLE_MS(port, termios->c_cflag))
1867 pl011_enable_ms(port);
1868
1869 /* first, disable everything */
1870 old_cr = readw(port->membase + UART011_CR);
1871 writew(0, port->membase + UART011_CR);
1872
Rabin Vincent3b438162010-02-12 06:43:11 +01001873 if (termios->c_cflag & CRTSCTS) {
1874 if (old_cr & UART011_CR_RTS)
1875 old_cr |= UART011_CR_RTSEN;
1876
1877 old_cr |= UART011_CR_CTSEN;
1878 uap->autorts = true;
1879 } else {
1880 old_cr &= ~(UART011_CR_CTSEN | UART011_CR_RTSEN);
1881 uap->autorts = false;
1882 }
1883
Russell Kingc19f12b2010-12-22 17:48:26 +00001884 if (uap->vendor->oversampling) {
1885 if (baud > port->uartclk / 16)
Linus Walleijac3e3fb2010-06-02 20:40:22 +01001886 old_cr |= ST_UART011_CR_OVSFACT;
1887 else
1888 old_cr &= ~ST_UART011_CR_OVSFACT;
1889 }
1890
Linus Walleijc5dd5532012-09-26 17:21:36 +02001891 /*
1892 * Workaround for the ST Micro oversampling variants to
1893 * increase the bitrate slightly, by lowering the divisor,
1894 * to avoid delayed sampling of start bit at high speeds,
1895 * else we see data corruption.
1896 */
1897 if (uap->vendor->oversampling) {
1898 if ((baud >= 3000000) && (baud < 3250000) && (quot > 1))
1899 quot -= 1;
1900 else if ((baud > 3250000) && (quot > 2))
1901 quot -= 2;
1902 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 /* Set baud rate */
1904 writew(quot & 0x3f, port->membase + UART011_FBRD);
1905 writew(quot >> 6, port->membase + UART011_IBRD);
1906
1907 /*
1908 * ----------v----------v----------v----------v-----
Linus Walleijc5dd5532012-09-26 17:21:36 +02001909 * NOTE: lcrh_tx and lcrh_rx MUST BE WRITTEN AFTER
1910 * UART011_FBRD & UART011_IBRD.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 * ----------^----------^----------^----------^-----
1912 */
Jon Medhurstb60f2f62013-12-10 10:18:59 +00001913 pl011_write_lcr_h(uap, lcr_h);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 writew(old_cr, port->membase + UART011_CR);
1915
1916 spin_unlock_irqrestore(&port->lock, flags);
1917}
1918
Andre Przywara0dd1e242015-05-21 17:26:23 +01001919static void
1920sbsa_uart_set_termios(struct uart_port *port, struct ktermios *termios,
1921 struct ktermios *old)
1922{
1923 struct uart_amba_port *uap =
1924 container_of(port, struct uart_amba_port, port);
1925 unsigned long flags;
1926
1927 tty_termios_encode_baud_rate(termios, uap->fixed_baud, uap->fixed_baud);
1928
1929 /* The SBSA UART only supports 8n1 without hardware flow control. */
1930 termios->c_cflag &= ~(CSIZE | CSTOPB | PARENB | PARODD);
1931 termios->c_cflag &= ~(CMSPAR | CRTSCTS);
1932 termios->c_cflag |= CS8 | CLOCAL;
1933
1934 spin_lock_irqsave(&port->lock, flags);
1935 uart_update_timeout(port, CS8, uap->fixed_baud);
1936 pl011_setup_status_masks(port, termios);
1937 spin_unlock_irqrestore(&port->lock, flags);
1938}
1939
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940static const char *pl011_type(struct uart_port *port)
1941{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001942 struct uart_amba_port *uap =
1943 container_of(port, struct uart_amba_port, port);
Russell Kinge8a7ba82010-12-28 09:16:54 +00001944 return uap->port.type == PORT_AMBA ? uap->type : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945}
1946
1947/*
1948 * Release the memory region(s) being used by 'port'
1949 */
Linus Walleije643f872012-06-17 15:44:19 +02001950static void pl011_release_port(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951{
1952 release_mem_region(port->mapbase, SZ_4K);
1953}
1954
1955/*
1956 * Request the memory region(s) being used by 'port'
1957 */
Linus Walleije643f872012-06-17 15:44:19 +02001958static int pl011_request_port(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959{
1960 return request_mem_region(port->mapbase, SZ_4K, "uart-pl011")
1961 != NULL ? 0 : -EBUSY;
1962}
1963
1964/*
1965 * Configure/autoconfigure the port.
1966 */
Linus Walleije643f872012-06-17 15:44:19 +02001967static void pl011_config_port(struct uart_port *port, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968{
1969 if (flags & UART_CONFIG_TYPE) {
1970 port->type = PORT_AMBA;
Linus Walleije643f872012-06-17 15:44:19 +02001971 pl011_request_port(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 }
1973}
1974
1975/*
1976 * verify the new serial_struct (for TIOCSSERIAL).
1977 */
Linus Walleije643f872012-06-17 15:44:19 +02001978static int pl011_verify_port(struct uart_port *port, struct serial_struct *ser)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979{
1980 int ret = 0;
1981 if (ser->type != PORT_UNKNOWN && ser->type != PORT_AMBA)
1982 ret = -EINVAL;
Yinghai Lua62c4132008-08-19 20:49:55 -07001983 if (ser->irq < 0 || ser->irq >= nr_irqs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 ret = -EINVAL;
1985 if (ser->baud_base < 9600)
1986 ret = -EINVAL;
1987 return ret;
1988}
1989
1990static struct uart_ops amba_pl011_pops = {
Linus Walleije643f872012-06-17 15:44:19 +02001991 .tx_empty = pl011_tx_empty,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 .set_mctrl = pl011_set_mctrl,
Linus Walleije643f872012-06-17 15:44:19 +02001993 .get_mctrl = pl011_get_mctrl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 .stop_tx = pl011_stop_tx,
1995 .start_tx = pl011_start_tx,
1996 .stop_rx = pl011_stop_rx,
1997 .enable_ms = pl011_enable_ms,
1998 .break_ctl = pl011_break_ctl,
1999 .startup = pl011_startup,
2000 .shutdown = pl011_shutdown,
Russell King68b65f72010-12-22 17:24:39 +00002001 .flush_buffer = pl011_dma_flush_buffer,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 .set_termios = pl011_set_termios,
2003 .type = pl011_type,
Linus Walleije643f872012-06-17 15:44:19 +02002004 .release_port = pl011_release_port,
2005 .request_port = pl011_request_port,
2006 .config_port = pl011_config_port,
2007 .verify_port = pl011_verify_port,
Jason Wessel84b5ae12008-02-20 13:33:39 -06002008#ifdef CONFIG_CONSOLE_POLL
Anton Vorontsovb3564c22012-09-24 14:27:54 -07002009 .poll_init = pl011_hwinit,
Linus Walleije643f872012-06-17 15:44:19 +02002010 .poll_get_char = pl011_get_poll_char,
2011 .poll_put_char = pl011_put_poll_char,
Jason Wessel84b5ae12008-02-20 13:33:39 -06002012#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013};
2014
Andre Przywara0dd1e242015-05-21 17:26:23 +01002015static void sbsa_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
2016{
2017}
2018
2019static unsigned int sbsa_uart_get_mctrl(struct uart_port *port)
2020{
2021 return 0;
2022}
2023
2024static const struct uart_ops sbsa_uart_pops = {
2025 .tx_empty = pl011_tx_empty,
2026 .set_mctrl = sbsa_uart_set_mctrl,
2027 .get_mctrl = sbsa_uart_get_mctrl,
2028 .stop_tx = pl011_stop_tx,
2029 .start_tx = pl011_start_tx,
2030 .stop_rx = pl011_stop_rx,
2031 .startup = sbsa_uart_startup,
2032 .shutdown = sbsa_uart_shutdown,
2033 .set_termios = sbsa_uart_set_termios,
2034 .type = pl011_type,
2035 .release_port = pl011_release_port,
2036 .request_port = pl011_request_port,
2037 .config_port = pl011_config_port,
2038 .verify_port = pl011_verify_port,
2039#ifdef CONFIG_CONSOLE_POLL
2040 .poll_init = pl011_hwinit,
2041 .poll_get_char = pl011_get_poll_char,
2042 .poll_put_char = pl011_put_poll_char,
2043#endif
2044};
2045
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046static struct uart_amba_port *amba_ports[UART_NR];
2047
2048#ifdef CONFIG_SERIAL_AMBA_PL011_CONSOLE
2049
Russell Kingd3587882006-03-20 20:00:09 +00002050static void pl011_console_putchar(struct uart_port *port, int ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051{
Daniel Thompsona5820c22014-09-03 12:51:55 +01002052 struct uart_amba_port *uap =
2053 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054
Russell Kingd3587882006-03-20 20:00:09 +00002055 while (readw(uap->port.membase + UART01x_FR) & UART01x_FR_TXFF)
2056 barrier();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 writew(ch, uap->port.membase + UART01x_DR);
2058}
2059
2060static void
2061pl011_console_write(struct console *co, const char *s, unsigned int count)
2062{
2063 struct uart_amba_port *uap = amba_ports[co->index];
Andre Przywara71eec482015-05-21 17:26:21 +01002064 unsigned int status, old_cr = 0, new_cr;
Rabin Vincentef605fd2012-01-17 11:52:28 +01002065 unsigned long flags;
2066 int locked = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067
2068 clk_enable(uap->clk);
2069
Rabin Vincentef605fd2012-01-17 11:52:28 +01002070 local_irq_save(flags);
2071 if (uap->port.sysrq)
2072 locked = 0;
2073 else if (oops_in_progress)
2074 locked = spin_trylock(&uap->port.lock);
2075 else
2076 spin_lock(&uap->port.lock);
2077
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 /*
2079 * First save the CR then disable the interrupts
2080 */
Andre Przywara71eec482015-05-21 17:26:21 +01002081 if (!uap->vendor->always_enabled) {
2082 old_cr = readw(uap->port.membase + UART011_CR);
2083 new_cr = old_cr & ~UART011_CR_CTSEN;
2084 new_cr |= UART01x_CR_UARTEN | UART011_CR_TXE;
2085 writew(new_cr, uap->port.membase + UART011_CR);
2086 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087
Russell Kingd3587882006-03-20 20:00:09 +00002088 uart_console_write(&uap->port, s, count, pl011_console_putchar);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089
2090 /*
2091 * Finally, wait for transmitter to become empty
2092 * and restore the TCR
2093 */
2094 do {
2095 status = readw(uap->port.membase + UART01x_FR);
2096 } while (status & UART01x_FR_BUSY);
Andre Przywara71eec482015-05-21 17:26:21 +01002097 if (!uap->vendor->always_enabled)
2098 writew(old_cr, uap->port.membase + UART011_CR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099
Rabin Vincentef605fd2012-01-17 11:52:28 +01002100 if (locked)
2101 spin_unlock(&uap->port.lock);
2102 local_irq_restore(flags);
2103
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 clk_disable(uap->clk);
2105}
2106
2107static void __init
2108pl011_console_get_options(struct uart_amba_port *uap, int *baud,
2109 int *parity, int *bits)
2110{
2111 if (readw(uap->port.membase + UART011_CR) & UART01x_CR_UARTEN) {
2112 unsigned int lcr_h, ibrd, fbrd;
2113
Linus Walleijec489aa2010-06-02 08:13:52 +01002114 lcr_h = readw(uap->port.membase + uap->lcrh_tx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115
2116 *parity = 'n';
2117 if (lcr_h & UART01x_LCRH_PEN) {
2118 if (lcr_h & UART01x_LCRH_EPS)
2119 *parity = 'e';
2120 else
2121 *parity = 'o';
2122 }
2123
2124 if ((lcr_h & 0x60) == UART01x_LCRH_WLEN_7)
2125 *bits = 7;
2126 else
2127 *bits = 8;
2128
2129 ibrd = readw(uap->port.membase + UART011_IBRD);
2130 fbrd = readw(uap->port.membase + UART011_FBRD);
2131
2132 *baud = uap->port.uartclk * 4 / (64 * ibrd + fbrd);
Linus Walleijac3e3fb2010-06-02 20:40:22 +01002133
Russell Kingc19f12b2010-12-22 17:48:26 +00002134 if (uap->vendor->oversampling) {
Linus Walleijac3e3fb2010-06-02 20:40:22 +01002135 if (readw(uap->port.membase + UART011_CR)
2136 & ST_UART011_CR_OVSFACT)
2137 *baud *= 2;
2138 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 }
2140}
2141
2142static int __init pl011_console_setup(struct console *co, char *options)
2143{
2144 struct uart_amba_port *uap;
2145 int baud = 38400;
2146 int bits = 8;
2147 int parity = 'n';
2148 int flow = 'n';
Russell King4b4851c2011-09-22 11:35:30 +01002149 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150
2151 /*
2152 * Check whether an invalid uart number has been specified, and
2153 * if so, search for the first available port that does have
2154 * console support.
2155 */
2156 if (co->index >= UART_NR)
2157 co->index = 0;
2158 uap = amba_ports[co->index];
Russell Kingd28122a2007-01-22 18:59:42 +00002159 if (!uap)
2160 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161
Linus Walleij78d80c52012-05-23 21:18:46 +02002162 /* Allow pins to be muxed in and configured */
Linus Walleij2b996fc2013-06-05 15:36:42 +02002163 pinctrl_pm_select_default_state(uap->port.dev);
Linus Walleij78d80c52012-05-23 21:18:46 +02002164
Russell King4b4851c2011-09-22 11:35:30 +01002165 ret = clk_prepare(uap->clk);
2166 if (ret)
2167 return ret;
2168
Jingoo Han574de552013-07-30 17:06:57 +09002169 if (dev_get_platdata(uap->port.dev)) {
Shreshtha Kumar Sahuc16d51a2011-06-13 10:11:33 +02002170 struct amba_pl011_data *plat;
2171
Jingoo Han574de552013-07-30 17:06:57 +09002172 plat = dev_get_platdata(uap->port.dev);
Shreshtha Kumar Sahuc16d51a2011-06-13 10:11:33 +02002173 if (plat->init)
2174 plat->init();
2175 }
2176
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 uap->port.uartclk = clk_get_rate(uap->clk);
2178
Andre Przywaracefc2d12015-05-21 17:26:22 +01002179 if (uap->vendor->fixed_options) {
2180 baud = uap->fixed_baud;
2181 } else {
2182 if (options)
2183 uart_parse_options(options,
2184 &baud, &parity, &bits, &flow);
2185 else
2186 pl011_console_get_options(uap, &baud, &parity, &bits);
2187 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188
2189 return uart_set_options(&uap->port, co, baud, parity, bits, flow);
2190}
2191
Vincent Sanders2d934862005-09-14 22:36:03 +01002192static struct uart_driver amba_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193static struct console amba_console = {
2194 .name = "ttyAMA",
2195 .write = pl011_console_write,
2196 .device = uart_console_device,
2197 .setup = pl011_console_setup,
2198 .flags = CON_PRINTBUFFER,
2199 .index = -1,
2200 .data = &amba_reg,
2201};
2202
2203#define AMBA_CONSOLE (&amba_console)
Rob Herring0d3c6732014-04-18 17:19:57 -05002204
2205static void pl011_putc(struct uart_port *port, int c)
2206{
2207 while (readl(port->membase + UART01x_FR) & UART01x_FR_TXFF)
2208 ;
2209 writeb(c, port->membase + UART01x_DR);
2210 while (readl(port->membase + UART01x_FR) & UART01x_FR_BUSY)
2211 ;
2212}
2213
2214static void pl011_early_write(struct console *con, const char *s, unsigned n)
2215{
2216 struct earlycon_device *dev = con->data;
2217
2218 uart_console_write(&dev->port, s, n, pl011_putc);
2219}
2220
2221static int __init pl011_early_console_setup(struct earlycon_device *device,
2222 const char *opt)
2223{
2224 if (!device->port.membase)
2225 return -ENODEV;
2226
2227 device->con->write = pl011_early_write;
2228 return 0;
2229}
2230EARLYCON_DECLARE(pl011, pl011_early_console_setup);
Rob Herring45e0f0f2014-03-27 08:08:03 -05002231OF_EARLYCON_DECLARE(pl011, "arm,pl011", pl011_early_console_setup);
Rob Herring0d3c6732014-04-18 17:19:57 -05002232
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233#else
2234#define AMBA_CONSOLE NULL
2235#endif
2236
2237static struct uart_driver amba_reg = {
2238 .owner = THIS_MODULE,
2239 .driver_name = "ttyAMA",
2240 .dev_name = "ttyAMA",
2241 .major = SERIAL_AMBA_MAJOR,
2242 .minor = SERIAL_AMBA_MINOR,
2243 .nr = UART_NR,
2244 .cons = AMBA_CONSOLE,
2245};
2246
Matthew Leach32614aa2012-08-28 16:41:28 +01002247static int pl011_probe_dt_alias(int index, struct device *dev)
2248{
2249 struct device_node *np;
2250 static bool seen_dev_with_alias = false;
2251 static bool seen_dev_without_alias = false;
2252 int ret = index;
2253
2254 if (!IS_ENABLED(CONFIG_OF))
2255 return ret;
2256
2257 np = dev->of_node;
2258 if (!np)
2259 return ret;
2260
2261 ret = of_alias_get_id(np, "serial");
2262 if (IS_ERR_VALUE(ret)) {
2263 seen_dev_without_alias = true;
2264 ret = index;
2265 } else {
2266 seen_dev_with_alias = true;
2267 if (ret >= ARRAY_SIZE(amba_ports) || amba_ports[ret] != NULL) {
2268 dev_warn(dev, "requested serial port %d not available.\n", ret);
2269 ret = index;
2270 }
2271 }
2272
2273 if (seen_dev_with_alias && seen_dev_without_alias)
2274 dev_warn(dev, "aliased and non-aliased serial devices found in device tree. Serial port enumeration may be unpredictable.\n");
2275
2276 return ret;
2277}
2278
Andre Przywara49bb3c82015-05-21 17:26:14 +01002279/* unregisters the driver also if no more ports are left */
2280static void pl011_unregister_port(struct uart_amba_port *uap)
2281{
2282 int i;
2283 bool busy = false;
2284
2285 for (i = 0; i < ARRAY_SIZE(amba_ports); i++) {
2286 if (amba_ports[i] == uap)
2287 amba_ports[i] = NULL;
2288 else if (amba_ports[i])
2289 busy = true;
2290 }
2291 pl011_dma_remove(uap);
2292 if (!busy)
2293 uart_unregister_driver(&amba_reg);
2294}
2295
Andre Przywara3873e2d2015-05-21 17:26:18 +01002296static int pl011_find_free_port(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297{
Andre Przywara3873e2d2015-05-21 17:26:18 +01002298 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299
2300 for (i = 0; i < ARRAY_SIZE(amba_ports); i++)
2301 if (amba_ports[i] == NULL)
Andre Przywara3873e2d2015-05-21 17:26:18 +01002302 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303
Andre Przywara3873e2d2015-05-21 17:26:18 +01002304 return -EBUSY;
2305}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306
Andre Przywara3873e2d2015-05-21 17:26:18 +01002307static int pl011_setup_port(struct device *dev, struct uart_amba_port *uap,
2308 struct resource *mmiobase, int index)
2309{
2310 void __iomem *base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311
Andre Przywara3873e2d2015-05-21 17:26:18 +01002312 base = devm_ioremap_resource(dev, mmiobase);
Krzysztof Kozlowski97a60ea2015-07-09 22:21:41 +09002313 if (IS_ERR(base))
2314 return PTR_ERR(base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315
Andre Przywara3873e2d2015-05-21 17:26:18 +01002316 index = pl011_probe_dt_alias(index, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317
Shreshtha Kumar Sahud8d8ffa2012-01-18 15:53:59 +05302318 uap->old_cr = 0;
Andre Przywara3873e2d2015-05-21 17:26:18 +01002319 uap->port.dev = dev;
2320 uap->port.mapbase = mmiobase->start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321 uap->port.membase = base;
2322 uap->port.iotype = UPIO_MEM;
Russell Kingffca2b12010-12-22 17:13:05 +00002323 uap->port.fifosize = uap->fifosize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 uap->port.flags = UPF_BOOT_AUTOCONF;
Andre Przywara3873e2d2015-05-21 17:26:18 +01002325 uap->port.line = index;
2326
2327 amba_ports[index] = uap;
2328
2329 return 0;
2330}
2331
2332static int pl011_register_port(struct uart_amba_port *uap)
2333{
2334 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335
Linus Walleijc3d8b762012-03-21 20:15:18 +01002336 /* Ensure interrupts from this UART are masked and cleared */
2337 writew(0, uap->port.membase + UART011_IMSC);
2338 writew(0xffff, uap->port.membase + UART011_ICR);
2339
Tushar Beheraef2889f2014-01-20 14:32:35 +05302340 if (!amba_reg.state) {
2341 ret = uart_register_driver(&amba_reg);
2342 if (ret < 0) {
Andre Przywara3873e2d2015-05-21 17:26:18 +01002343 dev_err(uap->port.dev,
Jorge Ramirez-Ortiz1c9be312015-03-06 13:05:40 -05002344 "Failed to register AMBA-PL011 driver\n");
Tushar Beheraef2889f2014-01-20 14:32:35 +05302345 return ret;
2346 }
2347 }
2348
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 ret = uart_add_one_port(&amba_reg, &uap->port);
Andre Przywara49bb3c82015-05-21 17:26:14 +01002350 if (ret)
2351 pl011_unregister_port(uap);
Tushar Behera7f6d9422014-06-26 15:35:35 +05302352
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 return ret;
2354}
2355
Andre Przywara3873e2d2015-05-21 17:26:18 +01002356static int pl011_probe(struct amba_device *dev, const struct amba_id *id)
2357{
2358 struct uart_amba_port *uap;
2359 struct vendor_data *vendor = id->data;
2360 int portnr, ret;
2361
2362 portnr = pl011_find_free_port();
2363 if (portnr < 0)
2364 return portnr;
2365
2366 uap = devm_kzalloc(&dev->dev, sizeof(struct uart_amba_port),
2367 GFP_KERNEL);
2368 if (!uap)
2369 return -ENOMEM;
2370
2371 uap->clk = devm_clk_get(&dev->dev, NULL);
2372 if (IS_ERR(uap->clk))
2373 return PTR_ERR(uap->clk);
2374
2375 uap->vendor = vendor;
2376 uap->lcrh_rx = vendor->lcrh_rx;
2377 uap->lcrh_tx = vendor->lcrh_tx;
2378 uap->fifosize = vendor->get_fifosize(dev);
2379 uap->port.irq = dev->irq[0];
2380 uap->port.ops = &amba_pl011_pops;
2381
2382 snprintf(uap->type, sizeof(uap->type), "PL011 rev%u", amba_rev(dev));
2383
2384 ret = pl011_setup_port(&dev->dev, uap, &dev->res, portnr);
2385 if (ret)
2386 return ret;
2387
2388 amba_set_drvdata(dev, uap);
2389
2390 return pl011_register_port(uap);
2391}
2392
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393static int pl011_remove(struct amba_device *dev)
2394{
2395 struct uart_amba_port *uap = amba_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 uart_remove_one_port(&amba_reg, &uap->port);
Andre Przywara49bb3c82015-05-21 17:26:14 +01002398 pl011_unregister_port(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 return 0;
2400}
2401
Ulf Hanssond0ce8502013-12-03 11:04:28 +01002402#ifdef CONFIG_PM_SLEEP
2403static int pl011_suspend(struct device *dev)
Leo Chenb736b892009-07-28 23:43:33 +01002404{
Ulf Hanssond0ce8502013-12-03 11:04:28 +01002405 struct uart_amba_port *uap = dev_get_drvdata(dev);
Leo Chenb736b892009-07-28 23:43:33 +01002406
2407 if (!uap)
2408 return -EINVAL;
2409
2410 return uart_suspend_port(&amba_reg, &uap->port);
2411}
2412
Ulf Hanssond0ce8502013-12-03 11:04:28 +01002413static int pl011_resume(struct device *dev)
Leo Chenb736b892009-07-28 23:43:33 +01002414{
Ulf Hanssond0ce8502013-12-03 11:04:28 +01002415 struct uart_amba_port *uap = dev_get_drvdata(dev);
Leo Chenb736b892009-07-28 23:43:33 +01002416
2417 if (!uap)
2418 return -EINVAL;
2419
2420 return uart_resume_port(&amba_reg, &uap->port);
2421}
2422#endif
2423
Ulf Hanssond0ce8502013-12-03 11:04:28 +01002424static SIMPLE_DEV_PM_OPS(pl011_dev_pm_ops, pl011_suspend, pl011_resume);
2425
Andre Przywara0dd1e242015-05-21 17:26:23 +01002426static int sbsa_uart_probe(struct platform_device *pdev)
2427{
2428 struct uart_amba_port *uap;
2429 struct resource *r;
2430 int portnr, ret;
2431 int baudrate;
2432
2433 /*
2434 * Check the mandatory baud rate parameter in the DT node early
2435 * so that we can easily exit with the error.
2436 */
2437 if (pdev->dev.of_node) {
2438 struct device_node *np = pdev->dev.of_node;
2439
2440 ret = of_property_read_u32(np, "current-speed", &baudrate);
2441 if (ret)
2442 return ret;
2443 } else {
2444 baudrate = 115200;
2445 }
2446
2447 portnr = pl011_find_free_port();
2448 if (portnr < 0)
2449 return portnr;
2450
2451 uap = devm_kzalloc(&pdev->dev, sizeof(struct uart_amba_port),
2452 GFP_KERNEL);
2453 if (!uap)
2454 return -ENOMEM;
2455
2456 uap->vendor = &vendor_sbsa;
2457 uap->fifosize = 32;
2458 uap->port.irq = platform_get_irq(pdev, 0);
2459 uap->port.ops = &sbsa_uart_pops;
2460 uap->fixed_baud = baudrate;
2461
2462 snprintf(uap->type, sizeof(uap->type), "SBSA");
2463
2464 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2465
2466 ret = pl011_setup_port(&pdev->dev, uap, r, portnr);
2467 if (ret)
2468 return ret;
2469
2470 platform_set_drvdata(pdev, uap);
2471
2472 return pl011_register_port(uap);
2473}
2474
2475static int sbsa_uart_remove(struct platform_device *pdev)
2476{
2477 struct uart_amba_port *uap = platform_get_drvdata(pdev);
2478
2479 uart_remove_one_port(&amba_reg, &uap->port);
2480 pl011_unregister_port(uap);
2481 return 0;
2482}
2483
2484static const struct of_device_id sbsa_uart_of_match[] = {
2485 { .compatible = "arm,sbsa-uart", },
2486 {},
2487};
2488MODULE_DEVICE_TABLE(of, sbsa_uart_of_match);
2489
Graeme Gregory3db9ab02015-05-21 17:26:24 +01002490static const struct acpi_device_id sbsa_uart_acpi_match[] = {
2491 { "ARMH0011", 0 },
2492 {},
2493};
2494MODULE_DEVICE_TABLE(acpi, sbsa_uart_acpi_match);
2495
Andre Przywara0dd1e242015-05-21 17:26:23 +01002496static struct platform_driver arm_sbsa_uart_platform_driver = {
2497 .probe = sbsa_uart_probe,
2498 .remove = sbsa_uart_remove,
2499 .driver = {
2500 .name = "sbsa-uart",
2501 .of_match_table = of_match_ptr(sbsa_uart_of_match),
Graeme Gregory3db9ab02015-05-21 17:26:24 +01002502 .acpi_match_table = ACPI_PTR(sbsa_uart_acpi_match),
Andre Przywara0dd1e242015-05-21 17:26:23 +01002503 },
2504};
2505
Russell King2c39c9e2010-07-27 08:50:16 +01002506static struct amba_id pl011_ids[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 {
2508 .id = 0x00041011,
2509 .mask = 0x000fffff,
Alessandro Rubini5926a292009-06-04 17:43:04 +01002510 .data = &vendor_arm,
2511 },
2512 {
2513 .id = 0x00380802,
2514 .mask = 0x00ffffff,
2515 .data = &vendor_st,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 },
2517 { 0, 0 },
2518};
2519
Dave Martin60f7a332011-10-05 15:15:22 +01002520MODULE_DEVICE_TABLE(amba, pl011_ids);
2521
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522static struct amba_driver pl011_driver = {
2523 .drv = {
2524 .name = "uart-pl011",
Ulf Hanssond0ce8502013-12-03 11:04:28 +01002525 .pm = &pl011_dev_pm_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526 },
2527 .id_table = pl011_ids,
2528 .probe = pl011_probe,
2529 .remove = pl011_remove,
2530};
2531
2532static int __init pl011_init(void)
2533{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 printk(KERN_INFO "Serial: AMBA PL011 UART driver\n");
2535
Andre Przywara0dd1e242015-05-21 17:26:23 +01002536 if (platform_driver_register(&arm_sbsa_uart_platform_driver))
2537 pr_warn("could not register SBSA UART platform driver\n");
Tushar Beheraef2889f2014-01-20 14:32:35 +05302538 return amba_driver_register(&pl011_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539}
2540
2541static void __exit pl011_exit(void)
2542{
Andre Przywara0dd1e242015-05-21 17:26:23 +01002543 platform_driver_unregister(&arm_sbsa_uart_platform_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544 amba_driver_unregister(&pl011_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545}
2546
Alessandro Rubini4dd9e742009-05-05 05:54:13 +01002547/*
2548 * While this can be a module, if builtin it's most likely the console
2549 * So let's leave module_exit but move module_init to an earlier place
2550 */
2551arch_initcall(pl011_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552module_exit(pl011_exit);
2553
2554MODULE_AUTHOR("ARM Ltd/Deep Blue Solutions Ltd");
2555MODULE_DESCRIPTION("ARM AMBA serial port driver");
2556MODULE_LICENSE("GPL");