blob: c0850f3cc439f0f6e333f2525532618792735769 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Driver for AMBA serial ports
3 *
4 * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
5 *
6 * Copyright 1999 ARM Limited
7 * Copyright (C) 2000 Deep Blue Solutions Ltd.
Russell King68b65f72010-12-22 17:24:39 +00008 * Copyright (C) 2010 ST-Ericsson SA
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 * This is a generic driver for ARM AMBA-type serial ports. They
25 * have a lot of 16550-like features, but are not register compatible.
26 * Note that although they do have CTS, DCD and DSR inputs, they do
27 * not have an RI input, nor do they have DTR or RTS outputs. If
28 * required, these have to be supplied via some other means (eg, GPIO)
29 * and hooked into this driver.
30 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Chanho Mincb06ff12013-03-27 18:38:11 +090032
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#if defined(CONFIG_SERIAL_AMBA_PL011_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
34#define SUPPORT_SYSRQ
35#endif
36
37#include <linux/module.h>
38#include <linux/ioport.h>
39#include <linux/init.h>
40#include <linux/console.h>
41#include <linux/sysrq.h>
42#include <linux/device.h>
43#include <linux/tty.h>
44#include <linux/tty_flip.h>
45#include <linux/serial_core.h>
46#include <linux/serial.h>
Russell Kinga62c80e2006-01-07 13:52:45 +000047#include <linux/amba/bus.h>
48#include <linux/amba/serial.h>
Russell Kingf8ce2542006-01-07 16:15:52 +000049#include <linux/clk.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090050#include <linux/slab.h>
Russell King68b65f72010-12-22 17:24:39 +000051#include <linux/dmaengine.h>
52#include <linux/dma-mapping.h>
53#include <linux/scatterlist.h>
Shreshtha Kumar Sahuc16d51a2011-06-13 10:11:33 +020054#include <linux/delay.h>
Viresh Kumar258aea72012-02-01 16:12:19 +053055#include <linux/types.h>
Matthew Leach32614aa2012-08-28 16:41:28 +010056#include <linux/of.h>
57#include <linux/of_device.h>
Shawn Guo258e0552012-05-06 22:53:35 +080058#include <linux/pinctrl/consumer.h>
Alessandro Rubinicb707062012-06-24 12:46:37 +010059#include <linux/sizes.h>
Linus Walleijde609582012-10-15 13:36:01 +020060#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
62#define UART_NR 14
63
64#define SERIAL_AMBA_MAJOR 204
65#define SERIAL_AMBA_MINOR 64
66#define SERIAL_AMBA_NR UART_NR
67
68#define AMBA_ISR_PASS_LIMIT 256
69
Russell Kingb63d4f02005-11-19 11:10:35 +000070#define UART_DR_ERROR (UART011_DR_OE|UART011_DR_BE|UART011_DR_PE|UART011_DR_FE)
71#define UART_DUMMY_DR_RX (1 << 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Alessandro Rubini5926a292009-06-04 17:43:04 +010073/* There is by now at least one vendor with differing details, so handle it */
74struct vendor_data {
75 unsigned int ifls;
Linus Walleijec489aa2010-06-02 08:13:52 +010076 unsigned int lcrh_tx;
77 unsigned int lcrh_rx;
Linus Walleijac3e3fb2010-06-02 20:40:22 +010078 bool oversampling;
Russell King38d62432010-12-22 17:59:16 +000079 bool dma_threshold;
Rajanikanth H.V4fd06902012-03-26 11:17:02 +020080 bool cts_event_workaround;
Andre Przywara71eec482015-05-21 17:26:21 +010081 bool always_enabled;
Andre Przywaracefc2d12015-05-21 17:26:22 +010082 bool fixed_options;
Jongsung Kim78506f22013-04-15 14:45:25 +090083
Jongsung Kimea336402013-05-10 18:05:35 +090084 unsigned int (*get_fifosize)(struct amba_device *dev);
Alessandro Rubini5926a292009-06-04 17:43:04 +010085};
86
Jongsung Kimea336402013-05-10 18:05:35 +090087static unsigned int get_fifosize_arm(struct amba_device *dev)
Jongsung Kim78506f22013-04-15 14:45:25 +090088{
Jongsung Kimea336402013-05-10 18:05:35 +090089 return amba_rev(dev) < 3 ? 16 : 32;
Jongsung Kim78506f22013-04-15 14:45:25 +090090}
91
Alessandro Rubini5926a292009-06-04 17:43:04 +010092static struct vendor_data vendor_arm = {
93 .ifls = UART011_IFLS_RX4_8|UART011_IFLS_TX4_8,
Linus Walleijec489aa2010-06-02 08:13:52 +010094 .lcrh_tx = UART011_LCRH,
95 .lcrh_rx = UART011_LCRH,
Linus Walleijac3e3fb2010-06-02 20:40:22 +010096 .oversampling = false,
Russell King38d62432010-12-22 17:59:16 +000097 .dma_threshold = false,
Rajanikanth H.V4fd06902012-03-26 11:17:02 +020098 .cts_event_workaround = false,
Andre Przywara71eec482015-05-21 17:26:21 +010099 .always_enabled = false,
Andre Przywaracefc2d12015-05-21 17:26:22 +0100100 .fixed_options = false,
Jongsung Kim78506f22013-04-15 14:45:25 +0900101 .get_fifosize = get_fifosize_arm,
Alessandro Rubini5926a292009-06-04 17:43:04 +0100102};
103
Andre Przywara0dd1e242015-05-21 17:26:23 +0100104static struct vendor_data vendor_sbsa = {
105 .oversampling = false,
106 .dma_threshold = false,
107 .cts_event_workaround = false,
108 .always_enabled = true,
109 .fixed_options = true,
110};
111
Jongsung Kimea336402013-05-10 18:05:35 +0900112static unsigned int get_fifosize_st(struct amba_device *dev)
Jongsung Kim78506f22013-04-15 14:45:25 +0900113{
114 return 64;
115}
116
Alessandro Rubini5926a292009-06-04 17:43:04 +0100117static struct vendor_data vendor_st = {
118 .ifls = UART011_IFLS_RX_HALF|UART011_IFLS_TX_HALF,
Linus Walleijec489aa2010-06-02 08:13:52 +0100119 .lcrh_tx = ST_UART011_LCRH_TX,
120 .lcrh_rx = ST_UART011_LCRH_RX,
Linus Walleijac3e3fb2010-06-02 20:40:22 +0100121 .oversampling = true,
Russell King38d62432010-12-22 17:59:16 +0000122 .dma_threshold = true,
Rajanikanth H.V4fd06902012-03-26 11:17:02 +0200123 .cts_event_workaround = true,
Andre Przywara71eec482015-05-21 17:26:21 +0100124 .always_enabled = false,
Andre Przywaracefc2d12015-05-21 17:26:22 +0100125 .fixed_options = false,
Jongsung Kim78506f22013-04-15 14:45:25 +0900126 .get_fifosize = get_fifosize_st,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127};
128
Russell King68b65f72010-12-22 17:24:39 +0000129/* Deals with DMA transactions */
Linus Walleijead76f32011-02-24 13:21:08 +0100130
131struct pl011_sgbuf {
132 struct scatterlist sg;
133 char *buf;
134};
135
136struct pl011_dmarx_data {
137 struct dma_chan *chan;
138 struct completion complete;
139 bool use_buf_b;
140 struct pl011_sgbuf sgbuf_a;
141 struct pl011_sgbuf sgbuf_b;
142 dma_cookie_t cookie;
143 bool running;
Chanho Mincb06ff12013-03-27 18:38:11 +0900144 struct timer_list timer;
145 unsigned int last_residue;
146 unsigned long last_jiffies;
147 bool auto_poll_rate;
148 unsigned int poll_rate;
149 unsigned int poll_timeout;
Linus Walleijead76f32011-02-24 13:21:08 +0100150};
151
Russell King68b65f72010-12-22 17:24:39 +0000152struct pl011_dmatx_data {
153 struct dma_chan *chan;
154 struct scatterlist sg;
155 char *buf;
156 bool queued;
157};
158
Russell Kingc19f12b2010-12-22 17:48:26 +0000159/*
160 * We wrap our port structure around the generic uart_port.
161 */
162struct uart_amba_port {
163 struct uart_port port;
164 struct clk *clk;
165 const struct vendor_data *vendor;
Russell King68b65f72010-12-22 17:24:39 +0000166 unsigned int dmacr; /* dma control reg */
Russell Kingc19f12b2010-12-22 17:48:26 +0000167 unsigned int im; /* interrupt mask */
168 unsigned int old_status;
Russell Kingffca2b12010-12-22 17:13:05 +0000169 unsigned int fifosize; /* vendor-specific */
Russell Kingc19f12b2010-12-22 17:48:26 +0000170 unsigned int lcrh_tx; /* vendor-specific */
171 unsigned int lcrh_rx; /* vendor-specific */
Shreshtha Kumar Sahud8d8ffa2012-01-18 15:53:59 +0530172 unsigned int old_cr; /* state during shutdown */
Russell Kingc19f12b2010-12-22 17:48:26 +0000173 bool autorts;
Andre Przywaracefc2d12015-05-21 17:26:22 +0100174 unsigned int fixed_baud; /* vendor-set fixed baud rate */
Russell Kingc19f12b2010-12-22 17:48:26 +0000175 char type[12];
Russell King68b65f72010-12-22 17:24:39 +0000176#ifdef CONFIG_DMA_ENGINE
177 /* DMA stuff */
Linus Walleijead76f32011-02-24 13:21:08 +0100178 bool using_tx_dma;
179 bool using_rx_dma;
180 struct pl011_dmarx_data dmarx;
Russell King68b65f72010-12-22 17:24:39 +0000181 struct pl011_dmatx_data dmatx;
Jorge Ramirez-Ortiz1c9be312015-03-06 13:05:40 -0500182 bool dma_probed;
Russell King68b65f72010-12-22 17:24:39 +0000183#endif
Russell Kingc19f12b2010-12-22 17:48:26 +0000184};
185
Russell King68b65f72010-12-22 17:24:39 +0000186/*
Linus Walleij29772c42011-02-24 13:21:36 +0100187 * Reads up to 256 characters from the FIFO or until it's empty and
188 * inserts them into the TTY layer. Returns the number of characters
189 * read from the FIFO.
190 */
191static int pl011_fifo_to_tty(struct uart_amba_port *uap)
192{
193 u16 status, ch;
194 unsigned int flag, max_count = 256;
195 int fifotaken = 0;
196
197 while (max_count--) {
198 status = readw(uap->port.membase + UART01x_FR);
199 if (status & UART01x_FR_RXFE)
200 break;
201
202 /* Take chars from the FIFO and update status */
203 ch = readw(uap->port.membase + UART01x_DR) |
204 UART_DUMMY_DR_RX;
205 flag = TTY_NORMAL;
206 uap->port.icount.rx++;
207 fifotaken++;
208
209 if (unlikely(ch & UART_DR_ERROR)) {
210 if (ch & UART011_DR_BE) {
211 ch &= ~(UART011_DR_FE | UART011_DR_PE);
212 uap->port.icount.brk++;
213 if (uart_handle_break(&uap->port))
214 continue;
215 } else if (ch & UART011_DR_PE)
216 uap->port.icount.parity++;
217 else if (ch & UART011_DR_FE)
218 uap->port.icount.frame++;
219 if (ch & UART011_DR_OE)
220 uap->port.icount.overrun++;
221
222 ch &= uap->port.read_status_mask;
223
224 if (ch & UART011_DR_BE)
225 flag = TTY_BREAK;
226 else if (ch & UART011_DR_PE)
227 flag = TTY_PARITY;
228 else if (ch & UART011_DR_FE)
229 flag = TTY_FRAME;
230 }
231
232 if (uart_handle_sysrq_char(&uap->port, ch & 255))
233 continue;
234
235 uart_insert_char(&uap->port, ch, UART011_DR_OE, ch, flag);
236 }
237
238 return fifotaken;
239}
240
241
242/*
Russell King68b65f72010-12-22 17:24:39 +0000243 * All the DMA operation mode stuff goes inside this ifdef.
244 * This assumes that you have a generic DMA device interface,
245 * no custom DMA interfaces are supported.
246 */
247#ifdef CONFIG_DMA_ENGINE
248
249#define PL011_DMA_BUFFER_SIZE PAGE_SIZE
250
Linus Walleijead76f32011-02-24 13:21:08 +0100251static int pl011_sgbuf_init(struct dma_chan *chan, struct pl011_sgbuf *sg,
252 enum dma_data_direction dir)
253{
Chanho Mincb06ff12013-03-27 18:38:11 +0900254 dma_addr_t dma_addr;
255
256 sg->buf = dma_alloc_coherent(chan->device->dev,
257 PL011_DMA_BUFFER_SIZE, &dma_addr, GFP_KERNEL);
Linus Walleijead76f32011-02-24 13:21:08 +0100258 if (!sg->buf)
259 return -ENOMEM;
260
Chanho Mincb06ff12013-03-27 18:38:11 +0900261 sg_init_table(&sg->sg, 1);
262 sg_set_page(&sg->sg, phys_to_page(dma_addr),
263 PL011_DMA_BUFFER_SIZE, offset_in_page(dma_addr));
264 sg_dma_address(&sg->sg) = dma_addr;
Andrew Jacksonc64be922014-11-07 14:14:43 +0000265 sg_dma_len(&sg->sg) = PL011_DMA_BUFFER_SIZE;
Linus Walleijead76f32011-02-24 13:21:08 +0100266
Linus Walleijead76f32011-02-24 13:21:08 +0100267 return 0;
268}
269
270static void pl011_sgbuf_free(struct dma_chan *chan, struct pl011_sgbuf *sg,
271 enum dma_data_direction dir)
272{
273 if (sg->buf) {
Chanho Mincb06ff12013-03-27 18:38:11 +0900274 dma_free_coherent(chan->device->dev,
275 PL011_DMA_BUFFER_SIZE, sg->buf,
276 sg_dma_address(&sg->sg));
Linus Walleijead76f32011-02-24 13:21:08 +0100277 }
278}
279
Jorge Ramirez-Ortiz1c9be312015-03-06 13:05:40 -0500280static void pl011_dma_probe(struct uart_amba_port *uap)
Russell King68b65f72010-12-22 17:24:39 +0000281{
282 /* DMA is the sole user of the platform data right now */
Jingoo Han574de552013-07-30 17:06:57 +0900283 struct amba_pl011_data *plat = dev_get_platdata(uap->port.dev);
Jorge Ramirez-Ortiz1c9be312015-03-06 13:05:40 -0500284 struct device *dev = uap->port.dev;
Russell King68b65f72010-12-22 17:24:39 +0000285 struct dma_slave_config tx_conf = {
286 .dst_addr = uap->port.mapbase + UART01x_DR,
287 .dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE,
Vinod Koula485df42011-10-14 10:47:38 +0530288 .direction = DMA_MEM_TO_DEV,
Russell King68b65f72010-12-22 17:24:39 +0000289 .dst_maxburst = uap->fifosize >> 1,
Viresh Kumar258aea72012-02-01 16:12:19 +0530290 .device_fc = false,
Russell King68b65f72010-12-22 17:24:39 +0000291 };
292 struct dma_chan *chan;
293 dma_cap_mask_t mask;
294
Jorge Ramirez-Ortiz1c9be312015-03-06 13:05:40 -0500295 uap->dma_probed = true;
296 chan = dma_request_slave_channel_reason(dev, "tx");
297 if (IS_ERR(chan)) {
298 if (PTR_ERR(chan) == -EPROBE_DEFER) {
Jorge Ramirez-Ortiz1c9be312015-03-06 13:05:40 -0500299 uap->dma_probed = false;
300 return;
301 }
Russell King68b65f72010-12-22 17:24:39 +0000302
Arnd Bergmann787b0c12013-01-28 16:24:37 +0000303 /* We need platform data */
304 if (!plat || !plat->dma_filter) {
305 dev_info(uap->port.dev, "no DMA platform data\n");
306 return;
307 }
308
309 /* Try to acquire a generic DMA engine slave TX channel */
310 dma_cap_zero(mask);
311 dma_cap_set(DMA_SLAVE, mask);
312
313 chan = dma_request_channel(mask, plat->dma_filter,
314 plat->dma_tx_param);
315 if (!chan) {
316 dev_err(uap->port.dev, "no TX DMA channel!\n");
317 return;
318 }
Russell King68b65f72010-12-22 17:24:39 +0000319 }
320
321 dmaengine_slave_config(chan, &tx_conf);
322 uap->dmatx.chan = chan;
323
324 dev_info(uap->port.dev, "DMA channel TX %s\n",
325 dma_chan_name(uap->dmatx.chan));
Linus Walleijead76f32011-02-24 13:21:08 +0100326
327 /* Optionally make use of an RX channel as well */
Arnd Bergmann787b0c12013-01-28 16:24:37 +0000328 chan = dma_request_slave_channel(dev, "rx");
Rob Herring0d3c6732014-04-18 17:19:57 -0500329
Arnd Bergmann787b0c12013-01-28 16:24:37 +0000330 if (!chan && plat->dma_rx_param) {
331 chan = dma_request_channel(mask, plat->dma_filter, plat->dma_rx_param);
332
333 if (!chan) {
334 dev_err(uap->port.dev, "no RX DMA channel!\n");
335 return;
336 }
337 }
338
339 if (chan) {
Linus Walleijead76f32011-02-24 13:21:08 +0100340 struct dma_slave_config rx_conf = {
341 .src_addr = uap->port.mapbase + UART01x_DR,
342 .src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE,
Vinod Koula485df42011-10-14 10:47:38 +0530343 .direction = DMA_DEV_TO_MEM,
Guennadi Liakhovetskib2aeb772014-04-12 19:47:17 +0200344 .src_maxburst = uap->fifosize >> 2,
Viresh Kumar258aea72012-02-01 16:12:19 +0530345 .device_fc = false,
Linus Walleijead76f32011-02-24 13:21:08 +0100346 };
Andrew Jackson2d3b7d62014-11-07 14:14:47 +0000347 struct dma_slave_caps caps;
Linus Walleijead76f32011-02-24 13:21:08 +0100348
Andrew Jackson2d3b7d62014-11-07 14:14:47 +0000349 /*
350 * Some DMA controllers provide information on their capabilities.
351 * If the controller does, check for suitable residue processing
352 * otherwise assime all is well.
353 */
354 if (0 == dma_get_slave_caps(chan, &caps)) {
355 if (caps.residue_granularity ==
356 DMA_RESIDUE_GRANULARITY_DESCRIPTOR) {
357 dma_release_channel(chan);
358 dev_info(uap->port.dev,
359 "RX DMA disabled - no residue processing\n");
360 return;
361 }
362 }
Linus Walleijead76f32011-02-24 13:21:08 +0100363 dmaengine_slave_config(chan, &rx_conf);
364 uap->dmarx.chan = chan;
365
Andrew Jackson98267d32014-11-07 14:14:23 +0000366 uap->dmarx.auto_poll_rate = false;
Greg Kroah-Hartman8f898bf2013-12-17 09:33:18 -0800367 if (plat && plat->dma_rx_poll_enable) {
Chanho Mincb06ff12013-03-27 18:38:11 +0900368 /* Set poll rate if specified. */
369 if (plat->dma_rx_poll_rate) {
370 uap->dmarx.auto_poll_rate = false;
371 uap->dmarx.poll_rate = plat->dma_rx_poll_rate;
372 } else {
373 /*
374 * 100 ms defaults to poll rate if not
375 * specified. This will be adjusted with
376 * the baud rate at set_termios.
377 */
378 uap->dmarx.auto_poll_rate = true;
379 uap->dmarx.poll_rate = 100;
380 }
381 /* 3 secs defaults poll_timeout if not specified. */
382 if (plat->dma_rx_poll_timeout)
383 uap->dmarx.poll_timeout =
384 plat->dma_rx_poll_timeout;
385 else
386 uap->dmarx.poll_timeout = 3000;
Andrew Jackson98267d32014-11-07 14:14:23 +0000387 } else if (!plat && dev->of_node) {
388 uap->dmarx.auto_poll_rate = of_property_read_bool(
389 dev->of_node, "auto-poll");
390 if (uap->dmarx.auto_poll_rate) {
391 u32 x;
Chanho Mincb06ff12013-03-27 18:38:11 +0900392
Andrew Jackson98267d32014-11-07 14:14:23 +0000393 if (0 == of_property_read_u32(dev->of_node,
394 "poll-rate-ms", &x))
395 uap->dmarx.poll_rate = x;
396 else
397 uap->dmarx.poll_rate = 100;
398 if (0 == of_property_read_u32(dev->of_node,
399 "poll-timeout-ms", &x))
400 uap->dmarx.poll_timeout = x;
401 else
402 uap->dmarx.poll_timeout = 3000;
403 }
404 }
Linus Walleijead76f32011-02-24 13:21:08 +0100405 dev_info(uap->port.dev, "DMA channel RX %s\n",
406 dma_chan_name(uap->dmarx.chan));
407 }
Russell King68b65f72010-12-22 17:24:39 +0000408}
409
Russell King68b65f72010-12-22 17:24:39 +0000410static void pl011_dma_remove(struct uart_amba_port *uap)
411{
Russell King68b65f72010-12-22 17:24:39 +0000412 if (uap->dmatx.chan)
413 dma_release_channel(uap->dmatx.chan);
Linus Walleijead76f32011-02-24 13:21:08 +0100414 if (uap->dmarx.chan)
415 dma_release_channel(uap->dmarx.chan);
Russell King68b65f72010-12-22 17:24:39 +0000416}
417
Dave Martin734745c2015-03-04 12:27:33 +0000418/* Forward declare these for the refill routine */
Russell King68b65f72010-12-22 17:24:39 +0000419static int pl011_dma_tx_refill(struct uart_amba_port *uap);
Dave Martin734745c2015-03-04 12:27:33 +0000420static void pl011_start_tx_pio(struct uart_amba_port *uap);
Russell King68b65f72010-12-22 17:24:39 +0000421
422/*
423 * The current DMA TX buffer has been sent.
424 * Try to queue up another DMA buffer.
425 */
426static void pl011_dma_tx_callback(void *data)
427{
428 struct uart_amba_port *uap = data;
429 struct pl011_dmatx_data *dmatx = &uap->dmatx;
430 unsigned long flags;
431 u16 dmacr;
432
433 spin_lock_irqsave(&uap->port.lock, flags);
434 if (uap->dmatx.queued)
435 dma_unmap_sg(dmatx->chan->device->dev, &dmatx->sg, 1,
436 DMA_TO_DEVICE);
437
438 dmacr = uap->dmacr;
439 uap->dmacr = dmacr & ~UART011_TXDMAE;
440 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
441
442 /*
443 * If TX DMA was disabled, it means that we've stopped the DMA for
444 * some reason (eg, XOFF received, or we want to send an X-char.)
445 *
446 * Note: we need to be careful here of a potential race between DMA
447 * and the rest of the driver - if the driver disables TX DMA while
448 * a TX buffer completing, we must update the tx queued status to
449 * get further refills (hence we check dmacr).
450 */
451 if (!(dmacr & UART011_TXDMAE) || uart_tx_stopped(&uap->port) ||
452 uart_circ_empty(&uap->port.state->xmit)) {
453 uap->dmatx.queued = false;
454 spin_unlock_irqrestore(&uap->port.lock, flags);
455 return;
456 }
457
Dave Martin734745c2015-03-04 12:27:33 +0000458 if (pl011_dma_tx_refill(uap) <= 0)
Russell King68b65f72010-12-22 17:24:39 +0000459 /*
460 * We didn't queue a DMA buffer for some reason, but we
461 * have data pending to be sent. Re-enable the TX IRQ.
462 */
Dave Martin734745c2015-03-04 12:27:33 +0000463 pl011_start_tx_pio(uap);
464
Russell King68b65f72010-12-22 17:24:39 +0000465 spin_unlock_irqrestore(&uap->port.lock, flags);
466}
467
468/*
469 * Try to refill the TX DMA buffer.
470 * Locking: called with port lock held and IRQs disabled.
471 * Returns:
472 * 1 if we queued up a TX DMA buffer.
473 * 0 if we didn't want to handle this by DMA
474 * <0 on error
475 */
476static int pl011_dma_tx_refill(struct uart_amba_port *uap)
477{
478 struct pl011_dmatx_data *dmatx = &uap->dmatx;
479 struct dma_chan *chan = dmatx->chan;
480 struct dma_device *dma_dev = chan->device;
481 struct dma_async_tx_descriptor *desc;
482 struct circ_buf *xmit = &uap->port.state->xmit;
483 unsigned int count;
484
485 /*
486 * Try to avoid the overhead involved in using DMA if the
487 * transaction fits in the first half of the FIFO, by using
488 * the standard interrupt handling. This ensures that we
489 * issue a uart_write_wakeup() at the appropriate time.
490 */
491 count = uart_circ_chars_pending(xmit);
492 if (count < (uap->fifosize >> 1)) {
493 uap->dmatx.queued = false;
494 return 0;
495 }
496
497 /*
498 * Bodge: don't send the last character by DMA, as this
499 * will prevent XON from notifying us to restart DMA.
500 */
501 count -= 1;
502
503 /* Else proceed to copy the TX chars to the DMA buffer and fire DMA */
504 if (count > PL011_DMA_BUFFER_SIZE)
505 count = PL011_DMA_BUFFER_SIZE;
506
507 if (xmit->tail < xmit->head)
508 memcpy(&dmatx->buf[0], &xmit->buf[xmit->tail], count);
509 else {
510 size_t first = UART_XMIT_SIZE - xmit->tail;
Andrew Jacksone2a545a2014-11-07 14:14:39 +0000511 size_t second;
512
513 if (first > count)
514 first = count;
515 second = count - first;
Russell King68b65f72010-12-22 17:24:39 +0000516
517 memcpy(&dmatx->buf[0], &xmit->buf[xmit->tail], first);
518 if (second)
519 memcpy(&dmatx->buf[first], &xmit->buf[0], second);
520 }
521
522 dmatx->sg.length = count;
523
524 if (dma_map_sg(dma_dev->dev, &dmatx->sg, 1, DMA_TO_DEVICE) != 1) {
525 uap->dmatx.queued = false;
526 dev_dbg(uap->port.dev, "unable to map TX DMA\n");
527 return -EBUSY;
528 }
529
Alexandre Bounine16052822012-03-08 16:11:18 -0500530 desc = dmaengine_prep_slave_sg(chan, &dmatx->sg, 1, DMA_MEM_TO_DEV,
Russell King68b65f72010-12-22 17:24:39 +0000531 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
532 if (!desc) {
533 dma_unmap_sg(dma_dev->dev, &dmatx->sg, 1, DMA_TO_DEVICE);
534 uap->dmatx.queued = false;
535 /*
536 * If DMA cannot be used right now, we complete this
537 * transaction via IRQ and let the TTY layer retry.
538 */
539 dev_dbg(uap->port.dev, "TX DMA busy\n");
540 return -EBUSY;
541 }
542
543 /* Some data to go along to the callback */
544 desc->callback = pl011_dma_tx_callback;
545 desc->callback_param = uap;
546
547 /* All errors should happen at prepare time */
548 dmaengine_submit(desc);
549
550 /* Fire the DMA transaction */
551 dma_dev->device_issue_pending(chan);
552
553 uap->dmacr |= UART011_TXDMAE;
554 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
555 uap->dmatx.queued = true;
556
557 /*
558 * Now we know that DMA will fire, so advance the ring buffer
559 * with the stuff we just dispatched.
560 */
561 xmit->tail = (xmit->tail + count) & (UART_XMIT_SIZE - 1);
562 uap->port.icount.tx += count;
563
564 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
565 uart_write_wakeup(&uap->port);
566
567 return 1;
568}
569
570/*
571 * We received a transmit interrupt without a pending X-char but with
572 * pending characters.
573 * Locking: called with port lock held and IRQs disabled.
574 * Returns:
575 * false if we want to use PIO to transmit
576 * true if we queued a DMA buffer
577 */
578static bool pl011_dma_tx_irq(struct uart_amba_port *uap)
579{
Linus Walleijead76f32011-02-24 13:21:08 +0100580 if (!uap->using_tx_dma)
Russell King68b65f72010-12-22 17:24:39 +0000581 return false;
582
583 /*
584 * If we already have a TX buffer queued, but received a
585 * TX interrupt, it will be because we've just sent an X-char.
586 * Ensure the TX DMA is enabled and the TX IRQ is disabled.
587 */
588 if (uap->dmatx.queued) {
589 uap->dmacr |= UART011_TXDMAE;
590 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
591 uap->im &= ~UART011_TXIM;
592 writew(uap->im, uap->port.membase + UART011_IMSC);
593 return true;
594 }
595
596 /*
597 * We don't have a TX buffer queued, so try to queue one.
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300598 * If we successfully queued a buffer, mask the TX IRQ.
Russell King68b65f72010-12-22 17:24:39 +0000599 */
600 if (pl011_dma_tx_refill(uap) > 0) {
601 uap->im &= ~UART011_TXIM;
602 writew(uap->im, uap->port.membase + UART011_IMSC);
603 return true;
604 }
605 return false;
606}
607
608/*
609 * Stop the DMA transmit (eg, due to received XOFF).
610 * Locking: called with port lock held and IRQs disabled.
611 */
612static inline void pl011_dma_tx_stop(struct uart_amba_port *uap)
613{
614 if (uap->dmatx.queued) {
615 uap->dmacr &= ~UART011_TXDMAE;
616 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
617 }
618}
619
620/*
621 * Try to start a DMA transmit, or in the case of an XON/OFF
622 * character queued for send, try to get that character out ASAP.
623 * Locking: called with port lock held and IRQs disabled.
624 * Returns:
625 * false if we want the TX IRQ to be enabled
626 * true if we have a buffer queued
627 */
628static inline bool pl011_dma_tx_start(struct uart_amba_port *uap)
629{
630 u16 dmacr;
631
Linus Walleijead76f32011-02-24 13:21:08 +0100632 if (!uap->using_tx_dma)
Russell King68b65f72010-12-22 17:24:39 +0000633 return false;
634
635 if (!uap->port.x_char) {
636 /* no X-char, try to push chars out in DMA mode */
637 bool ret = true;
638
639 if (!uap->dmatx.queued) {
640 if (pl011_dma_tx_refill(uap) > 0) {
641 uap->im &= ~UART011_TXIM;
Dave Martin734745c2015-03-04 12:27:33 +0000642 writew(uap->im, uap->port.membase +
643 UART011_IMSC);
644 } else
Russell King68b65f72010-12-22 17:24:39 +0000645 ret = false;
Russell King68b65f72010-12-22 17:24:39 +0000646 } else if (!(uap->dmacr & UART011_TXDMAE)) {
647 uap->dmacr |= UART011_TXDMAE;
648 writew(uap->dmacr,
649 uap->port.membase + UART011_DMACR);
650 }
651 return ret;
652 }
653
654 /*
655 * We have an X-char to send. Disable DMA to prevent it loading
656 * the TX fifo, and then see if we can stuff it into the FIFO.
657 */
658 dmacr = uap->dmacr;
659 uap->dmacr &= ~UART011_TXDMAE;
660 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
661
662 if (readw(uap->port.membase + UART01x_FR) & UART01x_FR_TXFF) {
663 /*
664 * No space in the FIFO, so enable the transmit interrupt
665 * so we know when there is space. Note that once we've
666 * loaded the character, we should just re-enable DMA.
667 */
668 return false;
669 }
670
671 writew(uap->port.x_char, uap->port.membase + UART01x_DR);
672 uap->port.icount.tx++;
673 uap->port.x_char = 0;
674
675 /* Success - restore the DMA state */
676 uap->dmacr = dmacr;
677 writew(dmacr, uap->port.membase + UART011_DMACR);
678
679 return true;
680}
681
682/*
683 * Flush the transmit buffer.
684 * Locking: called with port lock held and IRQs disabled.
685 */
686static void pl011_dma_flush_buffer(struct uart_port *port)
Fabio Estevamb83286b2013-08-09 17:58:51 -0300687__releases(&uap->port.lock)
688__acquires(&uap->port.lock)
Russell King68b65f72010-12-22 17:24:39 +0000689{
Daniel Thompsona5820c22014-09-03 12:51:55 +0100690 struct uart_amba_port *uap =
691 container_of(port, struct uart_amba_port, port);
Russell King68b65f72010-12-22 17:24:39 +0000692
Linus Walleijead76f32011-02-24 13:21:08 +0100693 if (!uap->using_tx_dma)
Russell King68b65f72010-12-22 17:24:39 +0000694 return;
695
696 /* Avoid deadlock with the DMA engine callback */
697 spin_unlock(&uap->port.lock);
698 dmaengine_terminate_all(uap->dmatx.chan);
699 spin_lock(&uap->port.lock);
700 if (uap->dmatx.queued) {
701 dma_unmap_sg(uap->dmatx.chan->device->dev, &uap->dmatx.sg, 1,
702 DMA_TO_DEVICE);
703 uap->dmatx.queued = false;
704 uap->dmacr &= ~UART011_TXDMAE;
705 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
706 }
707}
708
Linus Walleijead76f32011-02-24 13:21:08 +0100709static void pl011_dma_rx_callback(void *data);
710
711static int pl011_dma_rx_trigger_dma(struct uart_amba_port *uap)
712{
713 struct dma_chan *rxchan = uap->dmarx.chan;
Linus Walleijead76f32011-02-24 13:21:08 +0100714 struct pl011_dmarx_data *dmarx = &uap->dmarx;
715 struct dma_async_tx_descriptor *desc;
716 struct pl011_sgbuf *sgbuf;
717
718 if (!rxchan)
719 return -EIO;
720
721 /* Start the RX DMA job */
722 sgbuf = uap->dmarx.use_buf_b ?
723 &uap->dmarx.sgbuf_b : &uap->dmarx.sgbuf_a;
Alexandre Bounine16052822012-03-08 16:11:18 -0500724 desc = dmaengine_prep_slave_sg(rxchan, &sgbuf->sg, 1,
Vinod Koula485df42011-10-14 10:47:38 +0530725 DMA_DEV_TO_MEM,
Linus Walleijead76f32011-02-24 13:21:08 +0100726 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
727 /*
728 * If the DMA engine is busy and cannot prepare a
729 * channel, no big deal, the driver will fall back
730 * to interrupt mode as a result of this error code.
731 */
732 if (!desc) {
733 uap->dmarx.running = false;
734 dmaengine_terminate_all(rxchan);
735 return -EBUSY;
736 }
737
738 /* Some data to go along to the callback */
739 desc->callback = pl011_dma_rx_callback;
740 desc->callback_param = uap;
741 dmarx->cookie = dmaengine_submit(desc);
742 dma_async_issue_pending(rxchan);
743
744 uap->dmacr |= UART011_RXDMAE;
745 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
746 uap->dmarx.running = true;
747
748 uap->im &= ~UART011_RXIM;
749 writew(uap->im, uap->port.membase + UART011_IMSC);
750
751 return 0;
752}
753
754/*
755 * This is called when either the DMA job is complete, or
756 * the FIFO timeout interrupt occurred. This must be called
757 * with the port spinlock uap->port.lock held.
758 */
759static void pl011_dma_rx_chars(struct uart_amba_port *uap,
760 u32 pending, bool use_buf_b,
761 bool readfifo)
762{
Jiri Slaby05c7cd32013-01-03 15:53:04 +0100763 struct tty_port *port = &uap->port.state->port;
Linus Walleijead76f32011-02-24 13:21:08 +0100764 struct pl011_sgbuf *sgbuf = use_buf_b ?
765 &uap->dmarx.sgbuf_b : &uap->dmarx.sgbuf_a;
Linus Walleijead76f32011-02-24 13:21:08 +0100766 int dma_count = 0;
767 u32 fifotaken = 0; /* only used for vdbg() */
768
Chanho Mincb06ff12013-03-27 18:38:11 +0900769 struct pl011_dmarx_data *dmarx = &uap->dmarx;
770 int dmataken = 0;
771
772 if (uap->dmarx.poll_rate) {
773 /* The data can be taken by polling */
774 dmataken = sgbuf->sg.length - dmarx->last_residue;
775 /* Recalculate the pending size */
776 if (pending >= dmataken)
777 pending -= dmataken;
778 }
779
780 /* Pick the remain data from the DMA */
Linus Walleijead76f32011-02-24 13:21:08 +0100781 if (pending) {
Linus Walleijead76f32011-02-24 13:21:08 +0100782
783 /*
784 * First take all chars in the DMA pipe, then look in the FIFO.
785 * Note that tty_insert_flip_buf() tries to take as many chars
786 * as it can.
787 */
Chanho Mincb06ff12013-03-27 18:38:11 +0900788 dma_count = tty_insert_flip_string(port, sgbuf->buf + dmataken,
789 pending);
Linus Walleijead76f32011-02-24 13:21:08 +0100790
791 uap->port.icount.rx += dma_count;
792 if (dma_count < pending)
793 dev_warn(uap->port.dev,
794 "couldn't insert all characters (TTY is full?)\n");
795 }
796
Chanho Mincb06ff12013-03-27 18:38:11 +0900797 /* Reset the last_residue for Rx DMA poll */
798 if (uap->dmarx.poll_rate)
799 dmarx->last_residue = sgbuf->sg.length;
800
Linus Walleijead76f32011-02-24 13:21:08 +0100801 /*
802 * Only continue with trying to read the FIFO if all DMA chars have
803 * been taken first.
804 */
805 if (dma_count == pending && readfifo) {
806 /* Clear any error flags */
807 writew(UART011_OEIS | UART011_BEIS | UART011_PEIS | UART011_FEIS,
808 uap->port.membase + UART011_ICR);
809
810 /*
811 * If we read all the DMA'd characters, and we had an
Linus Walleij29772c42011-02-24 13:21:36 +0100812 * incomplete buffer, that could be due to an rx error, or
813 * maybe we just timed out. Read any pending chars and check
814 * the error status.
815 *
816 * Error conditions will only occur in the FIFO, these will
817 * trigger an immediate interrupt and stop the DMA job, so we
818 * will always find the error in the FIFO, never in the DMA
819 * buffer.
Linus Walleijead76f32011-02-24 13:21:08 +0100820 */
Linus Walleij29772c42011-02-24 13:21:36 +0100821 fifotaken = pl011_fifo_to_tty(uap);
Linus Walleijead76f32011-02-24 13:21:08 +0100822 }
823
824 spin_unlock(&uap->port.lock);
825 dev_vdbg(uap->port.dev,
826 "Took %d chars from DMA buffer and %d chars from the FIFO\n",
827 dma_count, fifotaken);
Jiri Slaby2e124b42013-01-03 15:53:06 +0100828 tty_flip_buffer_push(port);
Linus Walleijead76f32011-02-24 13:21:08 +0100829 spin_lock(&uap->port.lock);
830}
831
832static void pl011_dma_rx_irq(struct uart_amba_port *uap)
833{
834 struct pl011_dmarx_data *dmarx = &uap->dmarx;
835 struct dma_chan *rxchan = dmarx->chan;
836 struct pl011_sgbuf *sgbuf = dmarx->use_buf_b ?
837 &dmarx->sgbuf_b : &dmarx->sgbuf_a;
838 size_t pending;
839 struct dma_tx_state state;
840 enum dma_status dmastat;
841
842 /*
843 * Pause the transfer so we can trust the current counter,
844 * do this before we pause the PL011 block, else we may
845 * overflow the FIFO.
846 */
847 if (dmaengine_pause(rxchan))
848 dev_err(uap->port.dev, "unable to pause DMA transfer\n");
849 dmastat = rxchan->device->device_tx_status(rxchan,
850 dmarx->cookie, &state);
851 if (dmastat != DMA_PAUSED)
852 dev_err(uap->port.dev, "unable to pause DMA transfer\n");
853
854 /* Disable RX DMA - incoming data will wait in the FIFO */
855 uap->dmacr &= ~UART011_RXDMAE;
856 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
857 uap->dmarx.running = false;
858
859 pending = sgbuf->sg.length - state.residue;
860 BUG_ON(pending > PL011_DMA_BUFFER_SIZE);
861 /* Then we terminate the transfer - we now know our residue */
862 dmaengine_terminate_all(rxchan);
863
864 /*
865 * This will take the chars we have so far and insert
866 * into the framework.
867 */
868 pl011_dma_rx_chars(uap, pending, dmarx->use_buf_b, true);
869
870 /* Switch buffer & re-trigger DMA job */
871 dmarx->use_buf_b = !dmarx->use_buf_b;
872 if (pl011_dma_rx_trigger_dma(uap)) {
873 dev_dbg(uap->port.dev, "could not retrigger RX DMA job "
874 "fall back to interrupt mode\n");
875 uap->im |= UART011_RXIM;
876 writew(uap->im, uap->port.membase + UART011_IMSC);
877 }
878}
879
880static void pl011_dma_rx_callback(void *data)
881{
882 struct uart_amba_port *uap = data;
883 struct pl011_dmarx_data *dmarx = &uap->dmarx;
Chanho Min6dc01aa2012-02-20 10:24:40 +0900884 struct dma_chan *rxchan = dmarx->chan;
Linus Walleijead76f32011-02-24 13:21:08 +0100885 bool lastbuf = dmarx->use_buf_b;
Chanho Min6dc01aa2012-02-20 10:24:40 +0900886 struct pl011_sgbuf *sgbuf = dmarx->use_buf_b ?
887 &dmarx->sgbuf_b : &dmarx->sgbuf_a;
888 size_t pending;
889 struct dma_tx_state state;
Linus Walleijead76f32011-02-24 13:21:08 +0100890 int ret;
891
892 /*
893 * This completion interrupt occurs typically when the
894 * RX buffer is totally stuffed but no timeout has yet
895 * occurred. When that happens, we just want the RX
896 * routine to flush out the secondary DMA buffer while
897 * we immediately trigger the next DMA job.
898 */
899 spin_lock_irq(&uap->port.lock);
Chanho Min6dc01aa2012-02-20 10:24:40 +0900900 /*
901 * Rx data can be taken by the UART interrupts during
902 * the DMA irq handler. So we check the residue here.
903 */
904 rxchan->device->device_tx_status(rxchan, dmarx->cookie, &state);
905 pending = sgbuf->sg.length - state.residue;
906 BUG_ON(pending > PL011_DMA_BUFFER_SIZE);
907 /* Then we terminate the transfer - we now know our residue */
908 dmaengine_terminate_all(rxchan);
909
Linus Walleijead76f32011-02-24 13:21:08 +0100910 uap->dmarx.running = false;
911 dmarx->use_buf_b = !lastbuf;
912 ret = pl011_dma_rx_trigger_dma(uap);
913
Chanho Min6dc01aa2012-02-20 10:24:40 +0900914 pl011_dma_rx_chars(uap, pending, lastbuf, false);
Linus Walleijead76f32011-02-24 13:21:08 +0100915 spin_unlock_irq(&uap->port.lock);
916 /*
917 * Do this check after we picked the DMA chars so we don't
918 * get some IRQ immediately from RX.
919 */
920 if (ret) {
921 dev_dbg(uap->port.dev, "could not retrigger RX DMA job "
922 "fall back to interrupt mode\n");
923 uap->im |= UART011_RXIM;
924 writew(uap->im, uap->port.membase + UART011_IMSC);
925 }
926}
927
928/*
929 * Stop accepting received characters, when we're shutting down or
930 * suspending this port.
931 * Locking: called with port lock held and IRQs disabled.
932 */
933static inline void pl011_dma_rx_stop(struct uart_amba_port *uap)
934{
935 /* FIXME. Just disable the DMA enable */
936 uap->dmacr &= ~UART011_RXDMAE;
937 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
938}
Russell King68b65f72010-12-22 17:24:39 +0000939
Chanho Mincb06ff12013-03-27 18:38:11 +0900940/*
941 * Timer handler for Rx DMA polling.
942 * Every polling, It checks the residue in the dma buffer and transfer
943 * data to the tty. Also, last_residue is updated for the next polling.
944 */
945static void pl011_dma_rx_poll(unsigned long args)
946{
947 struct uart_amba_port *uap = (struct uart_amba_port *)args;
948 struct tty_port *port = &uap->port.state->port;
949 struct pl011_dmarx_data *dmarx = &uap->dmarx;
950 struct dma_chan *rxchan = uap->dmarx.chan;
951 unsigned long flags = 0;
952 unsigned int dmataken = 0;
953 unsigned int size = 0;
954 struct pl011_sgbuf *sgbuf;
955 int dma_count;
956 struct dma_tx_state state;
957
958 sgbuf = dmarx->use_buf_b ? &uap->dmarx.sgbuf_b : &uap->dmarx.sgbuf_a;
959 rxchan->device->device_tx_status(rxchan, dmarx->cookie, &state);
960 if (likely(state.residue < dmarx->last_residue)) {
961 dmataken = sgbuf->sg.length - dmarx->last_residue;
962 size = dmarx->last_residue - state.residue;
963 dma_count = tty_insert_flip_string(port, sgbuf->buf + dmataken,
964 size);
965 if (dma_count == size)
966 dmarx->last_residue = state.residue;
967 dmarx->last_jiffies = jiffies;
968 }
969 tty_flip_buffer_push(port);
970
971 /*
972 * If no data is received in poll_timeout, the driver will fall back
973 * to interrupt mode. We will retrigger DMA at the first interrupt.
974 */
975 if (jiffies_to_msecs(jiffies - dmarx->last_jiffies)
976 > uap->dmarx.poll_timeout) {
977
978 spin_lock_irqsave(&uap->port.lock, flags);
979 pl011_dma_rx_stop(uap);
Guennadi Liakhovetskic25a1ad2013-12-10 14:54:47 +0100980 uap->im |= UART011_RXIM;
981 writew(uap->im, uap->port.membase + UART011_IMSC);
Chanho Mincb06ff12013-03-27 18:38:11 +0900982 spin_unlock_irqrestore(&uap->port.lock, flags);
983
984 uap->dmarx.running = false;
985 dmaengine_terminate_all(rxchan);
986 del_timer(&uap->dmarx.timer);
987 } else {
988 mod_timer(&uap->dmarx.timer,
989 jiffies + msecs_to_jiffies(uap->dmarx.poll_rate));
990 }
991}
992
Russell King68b65f72010-12-22 17:24:39 +0000993static void pl011_dma_startup(struct uart_amba_port *uap)
994{
Linus Walleijead76f32011-02-24 13:21:08 +0100995 int ret;
996
Jorge Ramirez-Ortiz1c9be312015-03-06 13:05:40 -0500997 if (!uap->dma_probed)
998 pl011_dma_probe(uap);
999
Russell King68b65f72010-12-22 17:24:39 +00001000 if (!uap->dmatx.chan)
1001 return;
1002
Andrew Jackson4c0be452014-11-07 14:14:35 +00001003 uap->dmatx.buf = kmalloc(PL011_DMA_BUFFER_SIZE, GFP_KERNEL | __GFP_DMA);
Russell King68b65f72010-12-22 17:24:39 +00001004 if (!uap->dmatx.buf) {
1005 dev_err(uap->port.dev, "no memory for DMA TX buffer\n");
1006 uap->port.fifosize = uap->fifosize;
1007 return;
1008 }
1009
1010 sg_init_one(&uap->dmatx.sg, uap->dmatx.buf, PL011_DMA_BUFFER_SIZE);
1011
1012 /* The DMA buffer is now the FIFO the TTY subsystem can use */
1013 uap->port.fifosize = PL011_DMA_BUFFER_SIZE;
Linus Walleijead76f32011-02-24 13:21:08 +01001014 uap->using_tx_dma = true;
Russell King68b65f72010-12-22 17:24:39 +00001015
Linus Walleijead76f32011-02-24 13:21:08 +01001016 if (!uap->dmarx.chan)
1017 goto skip_rx;
1018
1019 /* Allocate and map DMA RX buffers */
1020 ret = pl011_sgbuf_init(uap->dmarx.chan, &uap->dmarx.sgbuf_a,
1021 DMA_FROM_DEVICE);
1022 if (ret) {
1023 dev_err(uap->port.dev, "failed to init DMA %s: %d\n",
1024 "RX buffer A", ret);
1025 goto skip_rx;
1026 }
1027
1028 ret = pl011_sgbuf_init(uap->dmarx.chan, &uap->dmarx.sgbuf_b,
1029 DMA_FROM_DEVICE);
1030 if (ret) {
1031 dev_err(uap->port.dev, "failed to init DMA %s: %d\n",
1032 "RX buffer B", ret);
1033 pl011_sgbuf_free(uap->dmarx.chan, &uap->dmarx.sgbuf_a,
1034 DMA_FROM_DEVICE);
1035 goto skip_rx;
1036 }
1037
1038 uap->using_rx_dma = true;
1039
1040skip_rx:
Russell King68b65f72010-12-22 17:24:39 +00001041 /* Turn on DMA error (RX/TX will be enabled on demand) */
1042 uap->dmacr |= UART011_DMAONERR;
1043 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
Russell King38d62432010-12-22 17:59:16 +00001044
1045 /*
1046 * ST Micro variants has some specific dma burst threshold
1047 * compensation. Set this to 16 bytes, so burst will only
1048 * be issued above/below 16 bytes.
1049 */
1050 if (uap->vendor->dma_threshold)
1051 writew(ST_UART011_DMAWM_RX_16 | ST_UART011_DMAWM_TX_16,
1052 uap->port.membase + ST_UART011_DMAWM);
Linus Walleijead76f32011-02-24 13:21:08 +01001053
1054 if (uap->using_rx_dma) {
1055 if (pl011_dma_rx_trigger_dma(uap))
1056 dev_dbg(uap->port.dev, "could not trigger initial "
1057 "RX DMA job, fall back to interrupt mode\n");
Chanho Mincb06ff12013-03-27 18:38:11 +09001058 if (uap->dmarx.poll_rate) {
1059 init_timer(&(uap->dmarx.timer));
1060 uap->dmarx.timer.function = pl011_dma_rx_poll;
1061 uap->dmarx.timer.data = (unsigned long)uap;
1062 mod_timer(&uap->dmarx.timer,
1063 jiffies +
1064 msecs_to_jiffies(uap->dmarx.poll_rate));
1065 uap->dmarx.last_residue = PL011_DMA_BUFFER_SIZE;
1066 uap->dmarx.last_jiffies = jiffies;
1067 }
Linus Walleijead76f32011-02-24 13:21:08 +01001068 }
Russell King68b65f72010-12-22 17:24:39 +00001069}
1070
1071static void pl011_dma_shutdown(struct uart_amba_port *uap)
1072{
Linus Walleijead76f32011-02-24 13:21:08 +01001073 if (!(uap->using_tx_dma || uap->using_rx_dma))
Russell King68b65f72010-12-22 17:24:39 +00001074 return;
1075
1076 /* Disable RX and TX DMA */
1077 while (readw(uap->port.membase + UART01x_FR) & UART01x_FR_BUSY)
1078 barrier();
1079
1080 spin_lock_irq(&uap->port.lock);
1081 uap->dmacr &= ~(UART011_DMAONERR | UART011_RXDMAE | UART011_TXDMAE);
1082 writew(uap->dmacr, uap->port.membase + UART011_DMACR);
1083 spin_unlock_irq(&uap->port.lock);
1084
Linus Walleijead76f32011-02-24 13:21:08 +01001085 if (uap->using_tx_dma) {
1086 /* In theory, this should already be done by pl011_dma_flush_buffer */
1087 dmaengine_terminate_all(uap->dmatx.chan);
1088 if (uap->dmatx.queued) {
1089 dma_unmap_sg(uap->dmatx.chan->device->dev, &uap->dmatx.sg, 1,
1090 DMA_TO_DEVICE);
1091 uap->dmatx.queued = false;
1092 }
1093
1094 kfree(uap->dmatx.buf);
1095 uap->using_tx_dma = false;
Russell King68b65f72010-12-22 17:24:39 +00001096 }
1097
Linus Walleijead76f32011-02-24 13:21:08 +01001098 if (uap->using_rx_dma) {
1099 dmaengine_terminate_all(uap->dmarx.chan);
1100 /* Clean up the RX DMA */
1101 pl011_sgbuf_free(uap->dmarx.chan, &uap->dmarx.sgbuf_a, DMA_FROM_DEVICE);
1102 pl011_sgbuf_free(uap->dmarx.chan, &uap->dmarx.sgbuf_b, DMA_FROM_DEVICE);
Chanho Mincb06ff12013-03-27 18:38:11 +09001103 if (uap->dmarx.poll_rate)
1104 del_timer_sync(&uap->dmarx.timer);
Linus Walleijead76f32011-02-24 13:21:08 +01001105 uap->using_rx_dma = false;
1106 }
Russell King68b65f72010-12-22 17:24:39 +00001107}
1108
Linus Walleijead76f32011-02-24 13:21:08 +01001109static inline bool pl011_dma_rx_available(struct uart_amba_port *uap)
1110{
1111 return uap->using_rx_dma;
1112}
1113
1114static inline bool pl011_dma_rx_running(struct uart_amba_port *uap)
1115{
1116 return uap->using_rx_dma && uap->dmarx.running;
1117}
1118
Russell King68b65f72010-12-22 17:24:39 +00001119#else
1120/* Blank functions if the DMA engine is not available */
Jorge Ramirez-Ortiz1c9be312015-03-06 13:05:40 -05001121static inline void pl011_dma_probe(struct uart_amba_port *uap)
Russell King68b65f72010-12-22 17:24:39 +00001122{
1123}
1124
1125static inline void pl011_dma_remove(struct uart_amba_port *uap)
1126{
1127}
1128
1129static inline void pl011_dma_startup(struct uart_amba_port *uap)
1130{
1131}
1132
1133static inline void pl011_dma_shutdown(struct uart_amba_port *uap)
1134{
1135}
1136
1137static inline bool pl011_dma_tx_irq(struct uart_amba_port *uap)
1138{
1139 return false;
1140}
1141
1142static inline void pl011_dma_tx_stop(struct uart_amba_port *uap)
1143{
1144}
1145
1146static inline bool pl011_dma_tx_start(struct uart_amba_port *uap)
1147{
1148 return false;
1149}
1150
Linus Walleijead76f32011-02-24 13:21:08 +01001151static inline void pl011_dma_rx_irq(struct uart_amba_port *uap)
1152{
1153}
1154
1155static inline void pl011_dma_rx_stop(struct uart_amba_port *uap)
1156{
1157}
1158
1159static inline int pl011_dma_rx_trigger_dma(struct uart_amba_port *uap)
1160{
1161 return -EIO;
1162}
1163
1164static inline bool pl011_dma_rx_available(struct uart_amba_port *uap)
1165{
1166 return false;
1167}
1168
1169static inline bool pl011_dma_rx_running(struct uart_amba_port *uap)
1170{
1171 return false;
1172}
1173
Russell King68b65f72010-12-22 17:24:39 +00001174#define pl011_dma_flush_buffer NULL
1175#endif
1176
Russell Kingb129a8c2005-08-31 10:12:14 +01001177static void pl011_stop_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001179 struct uart_amba_port *uap =
1180 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181
1182 uap->im &= ~UART011_TXIM;
1183 writew(uap->im, uap->port.membase + UART011_IMSC);
Russell King68b65f72010-12-22 17:24:39 +00001184 pl011_dma_tx_stop(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185}
1186
Dave Martin1e84d222015-04-27 16:49:05 +01001187static void pl011_tx_chars(struct uart_amba_port *uap, bool from_irq);
Dave Martin734745c2015-03-04 12:27:33 +00001188
1189/* Start TX with programmed I/O only (no DMA) */
1190static void pl011_start_tx_pio(struct uart_amba_port *uap)
1191{
1192 uap->im |= UART011_TXIM;
1193 writew(uap->im, uap->port.membase + UART011_IMSC);
Dave Martin1e84d222015-04-27 16:49:05 +01001194 pl011_tx_chars(uap, false);
Dave Martin734745c2015-03-04 12:27:33 +00001195}
1196
Russell Kingb129a8c2005-08-31 10:12:14 +01001197static void pl011_start_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001199 struct uart_amba_port *uap =
1200 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201
Dave Martin734745c2015-03-04 12:27:33 +00001202 if (!pl011_dma_tx_start(uap))
1203 pl011_start_tx_pio(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204}
1205
1206static void pl011_stop_rx(struct uart_port *port)
1207{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001208 struct uart_amba_port *uap =
1209 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210
1211 uap->im &= ~(UART011_RXIM|UART011_RTIM|UART011_FEIM|
1212 UART011_PEIM|UART011_BEIM|UART011_OEIM);
1213 writew(uap->im, uap->port.membase + UART011_IMSC);
Linus Walleijead76f32011-02-24 13:21:08 +01001214
1215 pl011_dma_rx_stop(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216}
1217
1218static void pl011_enable_ms(struct uart_port *port)
1219{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001220 struct uart_amba_port *uap =
1221 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222
1223 uap->im |= UART011_RIMIM|UART011_CTSMIM|UART011_DCDMIM|UART011_DSRMIM;
1224 writew(uap->im, uap->port.membase + UART011_IMSC);
1225}
1226
David Howells7d12e782006-10-05 14:55:46 +01001227static void pl011_rx_chars(struct uart_amba_port *uap)
Fabio Estevamb83286b2013-08-09 17:58:51 -03001228__releases(&uap->port.lock)
1229__acquires(&uap->port.lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230{
Linus Walleij29772c42011-02-24 13:21:36 +01001231 pl011_fifo_to_tty(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232
Thomas Gleixner2389b272007-05-29 21:53:50 +01001233 spin_unlock(&uap->port.lock);
Jiri Slaby2e124b42013-01-03 15:53:06 +01001234 tty_flip_buffer_push(&uap->port.state->port);
Linus Walleijead76f32011-02-24 13:21:08 +01001235 /*
1236 * If we were temporarily out of DMA mode for a while,
1237 * attempt to switch back to DMA mode again.
1238 */
1239 if (pl011_dma_rx_available(uap)) {
1240 if (pl011_dma_rx_trigger_dma(uap)) {
1241 dev_dbg(uap->port.dev, "could not trigger RX DMA job "
1242 "fall back to interrupt mode again\n");
1243 uap->im |= UART011_RXIM;
Guennadi Liakhovetski30ae5852013-12-10 14:54:42 +01001244 writew(uap->im, uap->port.membase + UART011_IMSC);
Chanho Mincb06ff12013-03-27 18:38:11 +09001245 } else {
Chanho Min89fa28d2013-04-03 11:10:37 +09001246#ifdef CONFIG_DMA_ENGINE
Chanho Mincb06ff12013-03-27 18:38:11 +09001247 /* Start Rx DMA poll */
1248 if (uap->dmarx.poll_rate) {
1249 uap->dmarx.last_jiffies = jiffies;
1250 uap->dmarx.last_residue = PL011_DMA_BUFFER_SIZE;
1251 mod_timer(&uap->dmarx.timer,
1252 jiffies +
1253 msecs_to_jiffies(uap->dmarx.poll_rate));
1254 }
Chanho Min89fa28d2013-04-03 11:10:37 +09001255#endif
Chanho Mincb06ff12013-03-27 18:38:11 +09001256 }
Linus Walleijead76f32011-02-24 13:21:08 +01001257 }
Thomas Gleixner2389b272007-05-29 21:53:50 +01001258 spin_lock(&uap->port.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259}
1260
Dave Martin1e84d222015-04-27 16:49:05 +01001261static bool pl011_tx_char(struct uart_amba_port *uap, unsigned char c,
1262 bool from_irq)
Dave Martin734745c2015-03-04 12:27:33 +00001263{
Dave Martin1e84d222015-04-27 16:49:05 +01001264 if (unlikely(!from_irq) &&
1265 readw(uap->port.membase + UART01x_FR) & UART01x_FR_TXFF)
1266 return false; /* unable to transmit character */
1267
Dave Martin734745c2015-03-04 12:27:33 +00001268 writew(c, uap->port.membase + UART01x_DR);
1269 uap->port.icount.tx++;
1270
Dave Martin1e84d222015-04-27 16:49:05 +01001271 return true;
Dave Martin734745c2015-03-04 12:27:33 +00001272}
1273
Dave Martin1e84d222015-04-27 16:49:05 +01001274static void pl011_tx_chars(struct uart_amba_port *uap, bool from_irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275{
Alan Coxebd2c8f2009-09-19 13:13:28 -07001276 struct circ_buf *xmit = &uap->port.state->xmit;
Dave Martin1e84d222015-04-27 16:49:05 +01001277 int count = uap->fifosize >> 1;
Dave Martin734745c2015-03-04 12:27:33 +00001278
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 if (uap->port.x_char) {
Dave Martin1e84d222015-04-27 16:49:05 +01001280 if (!pl011_tx_char(uap, uap->port.x_char, from_irq))
1281 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 uap->port.x_char = 0;
Dave Martin734745c2015-03-04 12:27:33 +00001283 --count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 }
1285 if (uart_circ_empty(xmit) || uart_tx_stopped(&uap->port)) {
Russell Kingb129a8c2005-08-31 10:12:14 +01001286 pl011_stop_tx(&uap->port);
Dave Martin1e84d222015-04-27 16:49:05 +01001287 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 }
1289
Russell King68b65f72010-12-22 17:24:39 +00001290 /* If we are using DMA mode, try to send some characters. */
1291 if (pl011_dma_tx_irq(uap))
Dave Martin1e84d222015-04-27 16:49:05 +01001292 return;
Russell King68b65f72010-12-22 17:24:39 +00001293
Dave Martin1e84d222015-04-27 16:49:05 +01001294 do {
1295 if (likely(from_irq) && count-- == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 break;
Dave Martin1e84d222015-04-27 16:49:05 +01001297
1298 if (!pl011_tx_char(uap, xmit->buf[xmit->tail], from_irq))
1299 break;
1300
1301 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
1302 } while (!uart_circ_empty(xmit));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303
1304 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
1305 uart_write_wakeup(&uap->port);
1306
Dave Martin1e84d222015-04-27 16:49:05 +01001307 if (uart_circ_empty(xmit))
Russell Kingb129a8c2005-08-31 10:12:14 +01001308 pl011_stop_tx(&uap->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309}
1310
1311static void pl011_modem_status(struct uart_amba_port *uap)
1312{
1313 unsigned int status, delta;
1314
1315 status = readw(uap->port.membase + UART01x_FR) & UART01x_FR_MODEM_ANY;
1316
1317 delta = status ^ uap->old_status;
1318 uap->old_status = status;
1319
1320 if (!delta)
1321 return;
1322
1323 if (delta & UART01x_FR_DCD)
1324 uart_handle_dcd_change(&uap->port, status & UART01x_FR_DCD);
1325
1326 if (delta & UART01x_FR_DSR)
1327 uap->port.icount.dsr++;
1328
1329 if (delta & UART01x_FR_CTS)
1330 uart_handle_cts_change(&uap->port, status & UART01x_FR_CTS);
1331
Alan Coxbdc04e32009-09-19 13:13:31 -07001332 wake_up_interruptible(&uap->port.state->port.delta_msr_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333}
1334
Andre Przywara9c4ef4b2015-05-21 17:26:20 +01001335static void check_apply_cts_event_workaround(struct uart_amba_port *uap)
1336{
1337 unsigned int dummy_read;
1338
1339 if (!uap->vendor->cts_event_workaround)
1340 return;
1341
1342 /* workaround to make sure that all bits are unlocked.. */
1343 writew(0x00, uap->port.membase + UART011_ICR);
1344
1345 /*
1346 * WA: introduce 26ns(1 uart clk) delay before W1C;
1347 * single apb access will incur 2 pclk(133.12Mhz) delay,
1348 * so add 2 dummy reads
1349 */
1350 dummy_read = readw(uap->port.membase + UART011_ICR);
1351 dummy_read = readw(uap->port.membase + UART011_ICR);
1352}
1353
David Howells7d12e782006-10-05 14:55:46 +01001354static irqreturn_t pl011_int(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355{
1356 struct uart_amba_port *uap = dev_id;
Russell King963cc982010-12-22 17:16:09 +00001357 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 unsigned int status, pass_counter = AMBA_ISR_PASS_LIMIT;
Andre Przywara075167e2015-05-21 17:26:19 +01001359 u16 imsc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 int handled = 0;
1361
Russell King963cc982010-12-22 17:16:09 +00001362 spin_lock_irqsave(&uap->port.lock, flags);
Andre Przywara075167e2015-05-21 17:26:19 +01001363 imsc = readw(uap->port.membase + UART011_IMSC);
1364 status = readw(uap->port.membase + UART011_RIS) & imsc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 if (status) {
1366 do {
Andre Przywara9c4ef4b2015-05-21 17:26:20 +01001367 check_apply_cts_event_workaround(uap);
Rajanikanth H.V4fd06902012-03-26 11:17:02 +02001368
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 writew(status & ~(UART011_TXIS|UART011_RTIS|
1370 UART011_RXIS),
1371 uap->port.membase + UART011_ICR);
1372
Linus Walleijead76f32011-02-24 13:21:08 +01001373 if (status & (UART011_RTIS|UART011_RXIS)) {
1374 if (pl011_dma_rx_running(uap))
1375 pl011_dma_rx_irq(uap);
1376 else
1377 pl011_rx_chars(uap);
1378 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 if (status & (UART011_DSRMIS|UART011_DCDMIS|
1380 UART011_CTSMIS|UART011_RIMIS))
1381 pl011_modem_status(uap);
Dave Martin1e84d222015-04-27 16:49:05 +01001382 if (status & UART011_TXIS)
1383 pl011_tx_chars(uap, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384
Rajanikanth H.V4fd06902012-03-26 11:17:02 +02001385 if (pass_counter-- == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 break;
1387
Andre Przywara075167e2015-05-21 17:26:19 +01001388 status = readw(uap->port.membase + UART011_RIS) & imsc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 } while (status != 0);
1390 handled = 1;
1391 }
1392
Russell King963cc982010-12-22 17:16:09 +00001393 spin_unlock_irqrestore(&uap->port.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394
1395 return IRQ_RETVAL(handled);
1396}
1397
Linus Walleije643f872012-06-17 15:44:19 +02001398static unsigned int pl011_tx_empty(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001400 struct uart_amba_port *uap =
1401 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 unsigned int status = readw(uap->port.membase + UART01x_FR);
1403 return status & (UART01x_FR_BUSY|UART01x_FR_TXFF) ? 0 : TIOCSER_TEMT;
1404}
1405
Linus Walleije643f872012-06-17 15:44:19 +02001406static unsigned int pl011_get_mctrl(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001408 struct uart_amba_port *uap =
1409 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 unsigned int result = 0;
1411 unsigned int status = readw(uap->port.membase + UART01x_FR);
1412
Jiri Slaby5159f402007-10-18 23:40:31 -07001413#define TIOCMBIT(uartbit, tiocmbit) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 if (status & uartbit) \
1415 result |= tiocmbit
1416
Jiri Slaby5159f402007-10-18 23:40:31 -07001417 TIOCMBIT(UART01x_FR_DCD, TIOCM_CAR);
1418 TIOCMBIT(UART01x_FR_DSR, TIOCM_DSR);
1419 TIOCMBIT(UART01x_FR_CTS, TIOCM_CTS);
1420 TIOCMBIT(UART011_FR_RI, TIOCM_RNG);
1421#undef TIOCMBIT
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 return result;
1423}
1424
1425static void pl011_set_mctrl(struct uart_port *port, unsigned int mctrl)
1426{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001427 struct uart_amba_port *uap =
1428 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 unsigned int cr;
1430
1431 cr = readw(uap->port.membase + UART011_CR);
1432
Jiri Slaby5159f402007-10-18 23:40:31 -07001433#define TIOCMBIT(tiocmbit, uartbit) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 if (mctrl & tiocmbit) \
1435 cr |= uartbit; \
1436 else \
1437 cr &= ~uartbit
1438
Jiri Slaby5159f402007-10-18 23:40:31 -07001439 TIOCMBIT(TIOCM_RTS, UART011_CR_RTS);
1440 TIOCMBIT(TIOCM_DTR, UART011_CR_DTR);
1441 TIOCMBIT(TIOCM_OUT1, UART011_CR_OUT1);
1442 TIOCMBIT(TIOCM_OUT2, UART011_CR_OUT2);
1443 TIOCMBIT(TIOCM_LOOP, UART011_CR_LBE);
Rabin Vincent3b438162010-02-12 06:43:11 +01001444
1445 if (uap->autorts) {
1446 /* We need to disable auto-RTS if we want to turn RTS off */
1447 TIOCMBIT(TIOCM_RTS, UART011_CR_RTSEN);
1448 }
Jiri Slaby5159f402007-10-18 23:40:31 -07001449#undef TIOCMBIT
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450
1451 writew(cr, uap->port.membase + UART011_CR);
1452}
1453
1454static void pl011_break_ctl(struct uart_port *port, int break_state)
1455{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001456 struct uart_amba_port *uap =
1457 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 unsigned long flags;
1459 unsigned int lcr_h;
1460
1461 spin_lock_irqsave(&uap->port.lock, flags);
Linus Walleijec489aa2010-06-02 08:13:52 +01001462 lcr_h = readw(uap->port.membase + uap->lcrh_tx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 if (break_state == -1)
1464 lcr_h |= UART01x_LCRH_BRK;
1465 else
1466 lcr_h &= ~UART01x_LCRH_BRK;
Linus Walleijec489aa2010-06-02 08:13:52 +01001467 writew(lcr_h, uap->port.membase + uap->lcrh_tx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 spin_unlock_irqrestore(&uap->port.lock, flags);
1469}
1470
Jason Wessel84b5ae12008-02-20 13:33:39 -06001471#ifdef CONFIG_CONSOLE_POLL
Anton Vorontsov5c8124a2012-09-24 14:27:55 -07001472
1473static void pl011_quiesce_irqs(struct uart_port *port)
1474{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001475 struct uart_amba_port *uap =
1476 container_of(port, struct uart_amba_port, port);
Anton Vorontsov5c8124a2012-09-24 14:27:55 -07001477 unsigned char __iomem *regs = uap->port.membase;
1478
1479 writew(readw(regs + UART011_MIS), regs + UART011_ICR);
1480 /*
1481 * There is no way to clear TXIM as this is "ready to transmit IRQ", so
1482 * we simply mask it. start_tx() will unmask it.
1483 *
1484 * Note we can race with start_tx(), and if the race happens, the
1485 * polling user might get another interrupt just after we clear it.
1486 * But it should be OK and can happen even w/o the race, e.g.
1487 * controller immediately got some new data and raised the IRQ.
1488 *
1489 * And whoever uses polling routines assumes that it manages the device
1490 * (including tx queue), so we're also fine with start_tx()'s caller
1491 * side.
1492 */
1493 writew(readw(regs + UART011_IMSC) & ~UART011_TXIM, regs + UART011_IMSC);
1494}
1495
Linus Walleije643f872012-06-17 15:44:19 +02001496static int pl011_get_poll_char(struct uart_port *port)
Jason Wessel84b5ae12008-02-20 13:33:39 -06001497{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001498 struct uart_amba_port *uap =
1499 container_of(port, struct uart_amba_port, port);
Jason Wessel84b5ae12008-02-20 13:33:39 -06001500 unsigned int status;
1501
Anton Vorontsov5c8124a2012-09-24 14:27:55 -07001502 /*
1503 * The caller might need IRQs lowered, e.g. if used with KDB NMI
1504 * debugger.
1505 */
1506 pl011_quiesce_irqs(port);
1507
Jason Wesself5316b42010-05-20 21:04:22 -05001508 status = readw(uap->port.membase + UART01x_FR);
1509 if (status & UART01x_FR_RXFE)
1510 return NO_POLL_CHAR;
Jason Wessel84b5ae12008-02-20 13:33:39 -06001511
1512 return readw(uap->port.membase + UART01x_DR);
1513}
1514
Linus Walleije643f872012-06-17 15:44:19 +02001515static void pl011_put_poll_char(struct uart_port *port,
Jason Wessel84b5ae12008-02-20 13:33:39 -06001516 unsigned char ch)
1517{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001518 struct uart_amba_port *uap =
1519 container_of(port, struct uart_amba_port, port);
Jason Wessel84b5ae12008-02-20 13:33:39 -06001520
1521 while (readw(uap->port.membase + UART01x_FR) & UART01x_FR_TXFF)
1522 barrier();
1523
1524 writew(ch, uap->port.membase + UART01x_DR);
1525}
1526
1527#endif /* CONFIG_CONSOLE_POLL */
1528
Anton Vorontsovb3564c22012-09-24 14:27:54 -07001529static int pl011_hwinit(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001531 struct uart_amba_port *uap =
1532 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 int retval;
1534
Linus Walleij78d80c52012-05-23 21:18:46 +02001535 /* Optionaly enable pins to be muxed in and configured */
Linus Walleij2b996fc2013-06-05 15:36:42 +02001536 pinctrl_pm_select_default_state(port->dev);
Linus Walleij78d80c52012-05-23 21:18:46 +02001537
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 /*
1539 * Try to enable the clock producer.
1540 */
Julia Lawall1c4c4392012-08-26 18:01:01 +02001541 retval = clk_prepare_enable(uap->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 if (retval)
Tushar Behera7f6d9422014-06-26 15:35:35 +05301543 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544
1545 uap->port.uartclk = clk_get_rate(uap->clk);
1546
Linus Walleij9b96fba2012-03-13 13:27:23 +01001547 /* Clear pending error and receive interrupts */
1548 writew(UART011_OEIS | UART011_BEIS | UART011_PEIS | UART011_FEIS |
1549 UART011_RTIS | UART011_RXIS, uap->port.membase + UART011_ICR);
1550
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 /*
Anton Vorontsovb3564c22012-09-24 14:27:54 -07001552 * Save interrupts enable mask, and enable RX interrupts in case if
1553 * the interrupt is used for NMI entry.
1554 */
1555 uap->im = readw(uap->port.membase + UART011_IMSC);
1556 writew(UART011_RTIM | UART011_RXIM, uap->port.membase + UART011_IMSC);
1557
Jingoo Han574de552013-07-30 17:06:57 +09001558 if (dev_get_platdata(uap->port.dev)) {
Anton Vorontsovb3564c22012-09-24 14:27:54 -07001559 struct amba_pl011_data *plat;
1560
Jingoo Han574de552013-07-30 17:06:57 +09001561 plat = dev_get_platdata(uap->port.dev);
Anton Vorontsovb3564c22012-09-24 14:27:54 -07001562 if (plat->init)
1563 plat->init();
1564 }
1565 return 0;
Anton Vorontsovb3564c22012-09-24 14:27:54 -07001566}
1567
Jon Medhurstb60f2f62013-12-10 10:18:59 +00001568static void pl011_write_lcr_h(struct uart_amba_port *uap, unsigned int lcr_h)
1569{
1570 writew(lcr_h, uap->port.membase + uap->lcrh_rx);
1571 if (uap->lcrh_rx != uap->lcrh_tx) {
1572 int i;
1573 /*
1574 * Wait 10 PCLKs before writing LCRH_TX register,
1575 * to get this delay write read only register 10 times
1576 */
1577 for (i = 0; i < 10; ++i)
1578 writew(0xff, uap->port.membase + UART011_MIS);
1579 writew(lcr_h, uap->port.membase + uap->lcrh_tx);
1580 }
1581}
1582
Andre Przywara867b8e82015-05-21 17:26:15 +01001583static int pl011_allocate_irq(struct uart_amba_port *uap)
1584{
1585 writew(uap->im, uap->port.membase + UART011_IMSC);
1586
1587 return request_irq(uap->port.irq, pl011_int, 0, "uart-pl011", uap);
1588}
1589
1590/*
1591 * Enable interrupts, only timeouts when using DMA
1592 * if initial RX DMA job failed, start in interrupt mode
1593 * as well.
1594 */
1595static void pl011_enable_interrupts(struct uart_amba_port *uap)
1596{
1597 spin_lock_irq(&uap->port.lock);
1598
1599 /* Clear out any spuriously appearing RX interrupts */
1600 writew(UART011_RTIS | UART011_RXIS,
1601 uap->port.membase + UART011_ICR);
1602 uap->im = UART011_RTIM;
1603 if (!pl011_dma_rx_running(uap))
1604 uap->im |= UART011_RXIM;
1605 writew(uap->im, uap->port.membase + UART011_IMSC);
1606 spin_unlock_irq(&uap->port.lock);
1607}
1608
Anton Vorontsovb3564c22012-09-24 14:27:54 -07001609static int pl011_startup(struct uart_port *port)
1610{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001611 struct uart_amba_port *uap =
1612 container_of(port, struct uart_amba_port, port);
Dave Martin734745c2015-03-04 12:27:33 +00001613 unsigned int cr;
Anton Vorontsovb3564c22012-09-24 14:27:54 -07001614 int retval;
1615
1616 retval = pl011_hwinit(port);
1617 if (retval)
1618 goto clk_dis;
1619
Andre Przywara867b8e82015-05-21 17:26:15 +01001620 retval = pl011_allocate_irq(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 if (retval)
1622 goto clk_dis;
1623
Russell Kingc19f12b2010-12-22 17:48:26 +00001624 writew(uap->vendor->ifls, uap->port.membase + UART011_IFLS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625
Jon Medhurstfe433902013-12-10 10:18:58 +00001626 spin_lock_irq(&uap->port.lock);
1627
Shreshtha Kumar Sahud8d8ffa2012-01-18 15:53:59 +05301628 /* restore RTS and DTR */
1629 cr = uap->old_cr & (UART011_CR_RTS | UART011_CR_DTR);
1630 cr |= UART01x_CR_UARTEN | UART011_CR_RXE | UART011_CR_TXE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 writew(cr, uap->port.membase + UART011_CR);
1632
Jon Medhurstfe433902013-12-10 10:18:58 +00001633 spin_unlock_irq(&uap->port.lock);
1634
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 /*
1636 * initialise the old status of the modem signals
1637 */
1638 uap->old_status = readw(uap->port.membase + UART01x_FR) & UART01x_FR_MODEM_ANY;
1639
Russell King68b65f72010-12-22 17:24:39 +00001640 /* Startup DMA */
1641 pl011_dma_startup(uap);
1642
Andre Przywara867b8e82015-05-21 17:26:15 +01001643 pl011_enable_interrupts(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644
1645 return 0;
1646
1647 clk_dis:
Julia Lawall1c4c4392012-08-26 18:01:01 +02001648 clk_disable_unprepare(uap->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 return retval;
1650}
1651
Andre Przywara0dd1e242015-05-21 17:26:23 +01001652static int sbsa_uart_startup(struct uart_port *port)
1653{
1654 struct uart_amba_port *uap =
1655 container_of(port, struct uart_amba_port, port);
1656 int retval;
1657
1658 retval = pl011_hwinit(port);
1659 if (retval)
1660 return retval;
1661
1662 retval = pl011_allocate_irq(uap);
1663 if (retval)
1664 return retval;
1665
1666 /* The SBSA UART does not support any modem status lines. */
1667 uap->old_status = 0;
1668
1669 pl011_enable_interrupts(uap);
1670
1671 return 0;
1672}
1673
Linus Walleijec489aa2010-06-02 08:13:52 +01001674static void pl011_shutdown_channel(struct uart_amba_port *uap,
1675 unsigned int lcrh)
1676{
1677 unsigned long val;
1678
1679 val = readw(uap->port.membase + lcrh);
1680 val &= ~(UART01x_LCRH_BRK | UART01x_LCRH_FEN);
1681 writew(val, uap->port.membase + lcrh);
1682}
1683
Andre Przywara95166a32015-05-21 17:26:16 +01001684/*
1685 * disable the port. It should not disable RTS and DTR.
1686 * Also RTS and DTR state should be preserved to restore
1687 * it during startup().
1688 */
1689static void pl011_disable_uart(struct uart_amba_port *uap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690{
Shreshtha Kumar Sahud8d8ffa2012-01-18 15:53:59 +05301691 unsigned int cr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692
Rabin Vincent3b438162010-02-12 06:43:11 +01001693 uap->autorts = false;
Jon Medhurstfe433902013-12-10 10:18:58 +00001694 spin_lock_irq(&uap->port.lock);
Shreshtha Kumar Sahud8d8ffa2012-01-18 15:53:59 +05301695 cr = readw(uap->port.membase + UART011_CR);
1696 uap->old_cr = cr;
1697 cr &= UART011_CR_RTS | UART011_CR_DTR;
1698 cr |= UART01x_CR_UARTEN | UART011_CR_TXE;
1699 writew(cr, uap->port.membase + UART011_CR);
Jon Medhurstfe433902013-12-10 10:18:58 +00001700 spin_unlock_irq(&uap->port.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701
1702 /*
1703 * disable break condition and fifos
1704 */
Linus Walleijec489aa2010-06-02 08:13:52 +01001705 pl011_shutdown_channel(uap, uap->lcrh_rx);
1706 if (uap->lcrh_rx != uap->lcrh_tx)
1707 pl011_shutdown_channel(uap, uap->lcrh_tx);
Andre Przywara95166a32015-05-21 17:26:16 +01001708}
1709
1710static void pl011_disable_interrupts(struct uart_amba_port *uap)
1711{
1712 spin_lock_irq(&uap->port.lock);
1713
1714 /* mask all interrupts and clear all pending ones */
1715 uap->im = 0;
1716 writew(uap->im, uap->port.membase + UART011_IMSC);
1717 writew(0xffff, uap->port.membase + UART011_ICR);
1718
1719 spin_unlock_irq(&uap->port.lock);
1720}
1721
1722static void pl011_shutdown(struct uart_port *port)
1723{
1724 struct uart_amba_port *uap =
1725 container_of(port, struct uart_amba_port, port);
1726
1727 pl011_disable_interrupts(uap);
1728
1729 pl011_dma_shutdown(uap);
1730
1731 free_irq(uap->port.irq, uap);
1732
1733 pl011_disable_uart(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734
1735 /*
1736 * Shut down the clock producer
1737 */
Julia Lawall1c4c4392012-08-26 18:01:01 +02001738 clk_disable_unprepare(uap->clk);
Linus Walleij78d80c52012-05-23 21:18:46 +02001739 /* Optionally let pins go into sleep states */
Linus Walleij2b996fc2013-06-05 15:36:42 +02001740 pinctrl_pm_select_sleep_state(port->dev);
Shreshtha Kumar Sahuc16d51a2011-06-13 10:11:33 +02001741
Jingoo Han574de552013-07-30 17:06:57 +09001742 if (dev_get_platdata(uap->port.dev)) {
Shreshtha Kumar Sahuc16d51a2011-06-13 10:11:33 +02001743 struct amba_pl011_data *plat;
1744
Jingoo Han574de552013-07-30 17:06:57 +09001745 plat = dev_get_platdata(uap->port.dev);
Shreshtha Kumar Sahuc16d51a2011-06-13 10:11:33 +02001746 if (plat->exit)
1747 plat->exit();
1748 }
1749
Peter Hurley36f339d2014-11-06 09:06:12 -05001750 if (uap->port.ops->flush_buffer)
1751 uap->port.ops->flush_buffer(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752}
1753
Andre Przywara0dd1e242015-05-21 17:26:23 +01001754static void sbsa_uart_shutdown(struct uart_port *port)
1755{
1756 struct uart_amba_port *uap =
1757 container_of(port, struct uart_amba_port, port);
1758
1759 pl011_disable_interrupts(uap);
1760
1761 free_irq(uap->port.irq, uap);
1762
1763 if (uap->port.ops->flush_buffer)
1764 uap->port.ops->flush_buffer(port);
1765}
1766
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767static void
Andre Przywaraef5a9352015-05-21 17:26:17 +01001768pl011_setup_status_masks(struct uart_port *port, struct ktermios *termios)
1769{
1770 port->read_status_mask = UART011_DR_OE | 255;
1771 if (termios->c_iflag & INPCK)
1772 port->read_status_mask |= UART011_DR_FE | UART011_DR_PE;
1773 if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
1774 port->read_status_mask |= UART011_DR_BE;
1775
1776 /*
1777 * Characters to ignore
1778 */
1779 port->ignore_status_mask = 0;
1780 if (termios->c_iflag & IGNPAR)
1781 port->ignore_status_mask |= UART011_DR_FE | UART011_DR_PE;
1782 if (termios->c_iflag & IGNBRK) {
1783 port->ignore_status_mask |= UART011_DR_BE;
1784 /*
1785 * If we're ignoring parity and break indicators,
1786 * ignore overruns too (for real raw support).
1787 */
1788 if (termios->c_iflag & IGNPAR)
1789 port->ignore_status_mask |= UART011_DR_OE;
1790 }
1791
1792 /*
1793 * Ignore all characters if CREAD is not set.
1794 */
1795 if ((termios->c_cflag & CREAD) == 0)
1796 port->ignore_status_mask |= UART_DUMMY_DR_RX;
1797}
1798
1799static void
Alan Cox606d0992006-12-08 02:38:45 -08001800pl011_set_termios(struct uart_port *port, struct ktermios *termios,
1801 struct ktermios *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001803 struct uart_amba_port *uap =
1804 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 unsigned int lcr_h, old_cr;
1806 unsigned long flags;
Russell Kingc19f12b2010-12-22 17:48:26 +00001807 unsigned int baud, quot, clkdiv;
1808
1809 if (uap->vendor->oversampling)
1810 clkdiv = 8;
1811 else
1812 clkdiv = 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813
1814 /*
1815 * Ask the core to calculate the divisor for us.
1816 */
Linus Walleijac3e3fb2010-06-02 20:40:22 +01001817 baud = uart_get_baud_rate(port, termios, old, 0,
Russell Kingc19f12b2010-12-22 17:48:26 +00001818 port->uartclk / clkdiv);
Chanho Min89fa28d2013-04-03 11:10:37 +09001819#ifdef CONFIG_DMA_ENGINE
Chanho Mincb06ff12013-03-27 18:38:11 +09001820 /*
1821 * Adjust RX DMA polling rate with baud rate if not specified.
1822 */
1823 if (uap->dmarx.auto_poll_rate)
1824 uap->dmarx.poll_rate = DIV_ROUND_UP(10000000, baud);
Chanho Min89fa28d2013-04-03 11:10:37 +09001825#endif
Linus Walleijac3e3fb2010-06-02 20:40:22 +01001826
1827 if (baud > port->uartclk/16)
1828 quot = DIV_ROUND_CLOSEST(port->uartclk * 8, baud);
1829 else
1830 quot = DIV_ROUND_CLOSEST(port->uartclk * 4, baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831
1832 switch (termios->c_cflag & CSIZE) {
1833 case CS5:
1834 lcr_h = UART01x_LCRH_WLEN_5;
1835 break;
1836 case CS6:
1837 lcr_h = UART01x_LCRH_WLEN_6;
1838 break;
1839 case CS7:
1840 lcr_h = UART01x_LCRH_WLEN_7;
1841 break;
1842 default: // CS8
1843 lcr_h = UART01x_LCRH_WLEN_8;
1844 break;
1845 }
1846 if (termios->c_cflag & CSTOPB)
1847 lcr_h |= UART01x_LCRH_STP2;
1848 if (termios->c_cflag & PARENB) {
1849 lcr_h |= UART01x_LCRH_PEN;
1850 if (!(termios->c_cflag & PARODD))
1851 lcr_h |= UART01x_LCRH_EPS;
1852 }
Russell Kingffca2b12010-12-22 17:13:05 +00001853 if (uap->fifosize > 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 lcr_h |= UART01x_LCRH_FEN;
1855
1856 spin_lock_irqsave(&port->lock, flags);
1857
1858 /*
1859 * Update the per-port timeout.
1860 */
1861 uart_update_timeout(port, termios->c_cflag, baud);
1862
Andre Przywaraef5a9352015-05-21 17:26:17 +01001863 pl011_setup_status_masks(port, termios);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864
1865 if (UART_ENABLE_MS(port, termios->c_cflag))
1866 pl011_enable_ms(port);
1867
1868 /* first, disable everything */
1869 old_cr = readw(port->membase + UART011_CR);
1870 writew(0, port->membase + UART011_CR);
1871
Rabin Vincent3b438162010-02-12 06:43:11 +01001872 if (termios->c_cflag & CRTSCTS) {
1873 if (old_cr & UART011_CR_RTS)
1874 old_cr |= UART011_CR_RTSEN;
1875
1876 old_cr |= UART011_CR_CTSEN;
1877 uap->autorts = true;
1878 } else {
1879 old_cr &= ~(UART011_CR_CTSEN | UART011_CR_RTSEN);
1880 uap->autorts = false;
1881 }
1882
Russell Kingc19f12b2010-12-22 17:48:26 +00001883 if (uap->vendor->oversampling) {
1884 if (baud > port->uartclk / 16)
Linus Walleijac3e3fb2010-06-02 20:40:22 +01001885 old_cr |= ST_UART011_CR_OVSFACT;
1886 else
1887 old_cr &= ~ST_UART011_CR_OVSFACT;
1888 }
1889
Linus Walleijc5dd5532012-09-26 17:21:36 +02001890 /*
1891 * Workaround for the ST Micro oversampling variants to
1892 * increase the bitrate slightly, by lowering the divisor,
1893 * to avoid delayed sampling of start bit at high speeds,
1894 * else we see data corruption.
1895 */
1896 if (uap->vendor->oversampling) {
1897 if ((baud >= 3000000) && (baud < 3250000) && (quot > 1))
1898 quot -= 1;
1899 else if ((baud > 3250000) && (quot > 2))
1900 quot -= 2;
1901 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 /* Set baud rate */
1903 writew(quot & 0x3f, port->membase + UART011_FBRD);
1904 writew(quot >> 6, port->membase + UART011_IBRD);
1905
1906 /*
1907 * ----------v----------v----------v----------v-----
Linus Walleijc5dd5532012-09-26 17:21:36 +02001908 * NOTE: lcrh_tx and lcrh_rx MUST BE WRITTEN AFTER
1909 * UART011_FBRD & UART011_IBRD.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 * ----------^----------^----------^----------^-----
1911 */
Jon Medhurstb60f2f62013-12-10 10:18:59 +00001912 pl011_write_lcr_h(uap, lcr_h);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 writew(old_cr, port->membase + UART011_CR);
1914
1915 spin_unlock_irqrestore(&port->lock, flags);
1916}
1917
Andre Przywara0dd1e242015-05-21 17:26:23 +01001918static void
1919sbsa_uart_set_termios(struct uart_port *port, struct ktermios *termios,
1920 struct ktermios *old)
1921{
1922 struct uart_amba_port *uap =
1923 container_of(port, struct uart_amba_port, port);
1924 unsigned long flags;
1925
1926 tty_termios_encode_baud_rate(termios, uap->fixed_baud, uap->fixed_baud);
1927
1928 /* The SBSA UART only supports 8n1 without hardware flow control. */
1929 termios->c_cflag &= ~(CSIZE | CSTOPB | PARENB | PARODD);
1930 termios->c_cflag &= ~(CMSPAR | CRTSCTS);
1931 termios->c_cflag |= CS8 | CLOCAL;
1932
1933 spin_lock_irqsave(&port->lock, flags);
1934 uart_update_timeout(port, CS8, uap->fixed_baud);
1935 pl011_setup_status_masks(port, termios);
1936 spin_unlock_irqrestore(&port->lock, flags);
1937}
1938
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939static const char *pl011_type(struct uart_port *port)
1940{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001941 struct uart_amba_port *uap =
1942 container_of(port, struct uart_amba_port, port);
Russell Kinge8a7ba82010-12-28 09:16:54 +00001943 return uap->port.type == PORT_AMBA ? uap->type : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944}
1945
1946/*
1947 * Release the memory region(s) being used by 'port'
1948 */
Linus Walleije643f872012-06-17 15:44:19 +02001949static void pl011_release_port(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950{
1951 release_mem_region(port->mapbase, SZ_4K);
1952}
1953
1954/*
1955 * Request the memory region(s) being used by 'port'
1956 */
Linus Walleije643f872012-06-17 15:44:19 +02001957static int pl011_request_port(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958{
1959 return request_mem_region(port->mapbase, SZ_4K, "uart-pl011")
1960 != NULL ? 0 : -EBUSY;
1961}
1962
1963/*
1964 * Configure/autoconfigure the port.
1965 */
Linus Walleije643f872012-06-17 15:44:19 +02001966static void pl011_config_port(struct uart_port *port, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967{
1968 if (flags & UART_CONFIG_TYPE) {
1969 port->type = PORT_AMBA;
Linus Walleije643f872012-06-17 15:44:19 +02001970 pl011_request_port(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 }
1972}
1973
1974/*
1975 * verify the new serial_struct (for TIOCSSERIAL).
1976 */
Linus Walleije643f872012-06-17 15:44:19 +02001977static int pl011_verify_port(struct uart_port *port, struct serial_struct *ser)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978{
1979 int ret = 0;
1980 if (ser->type != PORT_UNKNOWN && ser->type != PORT_AMBA)
1981 ret = -EINVAL;
Yinghai Lua62c4132008-08-19 20:49:55 -07001982 if (ser->irq < 0 || ser->irq >= nr_irqs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 ret = -EINVAL;
1984 if (ser->baud_base < 9600)
1985 ret = -EINVAL;
1986 return ret;
1987}
1988
1989static struct uart_ops amba_pl011_pops = {
Linus Walleije643f872012-06-17 15:44:19 +02001990 .tx_empty = pl011_tx_empty,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 .set_mctrl = pl011_set_mctrl,
Linus Walleije643f872012-06-17 15:44:19 +02001992 .get_mctrl = pl011_get_mctrl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 .stop_tx = pl011_stop_tx,
1994 .start_tx = pl011_start_tx,
1995 .stop_rx = pl011_stop_rx,
1996 .enable_ms = pl011_enable_ms,
1997 .break_ctl = pl011_break_ctl,
1998 .startup = pl011_startup,
1999 .shutdown = pl011_shutdown,
Russell King68b65f72010-12-22 17:24:39 +00002000 .flush_buffer = pl011_dma_flush_buffer,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 .set_termios = pl011_set_termios,
2002 .type = pl011_type,
Linus Walleije643f872012-06-17 15:44:19 +02002003 .release_port = pl011_release_port,
2004 .request_port = pl011_request_port,
2005 .config_port = pl011_config_port,
2006 .verify_port = pl011_verify_port,
Jason Wessel84b5ae12008-02-20 13:33:39 -06002007#ifdef CONFIG_CONSOLE_POLL
Anton Vorontsovb3564c22012-09-24 14:27:54 -07002008 .poll_init = pl011_hwinit,
Linus Walleije643f872012-06-17 15:44:19 +02002009 .poll_get_char = pl011_get_poll_char,
2010 .poll_put_char = pl011_put_poll_char,
Jason Wessel84b5ae12008-02-20 13:33:39 -06002011#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012};
2013
Andre Przywara0dd1e242015-05-21 17:26:23 +01002014static void sbsa_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
2015{
2016}
2017
2018static unsigned int sbsa_uart_get_mctrl(struct uart_port *port)
2019{
2020 return 0;
2021}
2022
2023static const struct uart_ops sbsa_uart_pops = {
2024 .tx_empty = pl011_tx_empty,
2025 .set_mctrl = sbsa_uart_set_mctrl,
2026 .get_mctrl = sbsa_uart_get_mctrl,
2027 .stop_tx = pl011_stop_tx,
2028 .start_tx = pl011_start_tx,
2029 .stop_rx = pl011_stop_rx,
2030 .startup = sbsa_uart_startup,
2031 .shutdown = sbsa_uart_shutdown,
2032 .set_termios = sbsa_uart_set_termios,
2033 .type = pl011_type,
2034 .release_port = pl011_release_port,
2035 .request_port = pl011_request_port,
2036 .config_port = pl011_config_port,
2037 .verify_port = pl011_verify_port,
2038#ifdef CONFIG_CONSOLE_POLL
2039 .poll_init = pl011_hwinit,
2040 .poll_get_char = pl011_get_poll_char,
2041 .poll_put_char = pl011_put_poll_char,
2042#endif
2043};
2044
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045static struct uart_amba_port *amba_ports[UART_NR];
2046
2047#ifdef CONFIG_SERIAL_AMBA_PL011_CONSOLE
2048
Russell Kingd3587882006-03-20 20:00:09 +00002049static void pl011_console_putchar(struct uart_port *port, int ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050{
Daniel Thompsona5820c22014-09-03 12:51:55 +01002051 struct uart_amba_port *uap =
2052 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053
Russell Kingd3587882006-03-20 20:00:09 +00002054 while (readw(uap->port.membase + UART01x_FR) & UART01x_FR_TXFF)
2055 barrier();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 writew(ch, uap->port.membase + UART01x_DR);
2057}
2058
2059static void
2060pl011_console_write(struct console *co, const char *s, unsigned int count)
2061{
2062 struct uart_amba_port *uap = amba_ports[co->index];
Andre Przywara71eec482015-05-21 17:26:21 +01002063 unsigned int status, old_cr = 0, new_cr;
Rabin Vincentef605fd2012-01-17 11:52:28 +01002064 unsigned long flags;
2065 int locked = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066
2067 clk_enable(uap->clk);
2068
Rabin Vincentef605fd2012-01-17 11:52:28 +01002069 local_irq_save(flags);
2070 if (uap->port.sysrq)
2071 locked = 0;
2072 else if (oops_in_progress)
2073 locked = spin_trylock(&uap->port.lock);
2074 else
2075 spin_lock(&uap->port.lock);
2076
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 /*
2078 * First save the CR then disable the interrupts
2079 */
Andre Przywara71eec482015-05-21 17:26:21 +01002080 if (!uap->vendor->always_enabled) {
2081 old_cr = readw(uap->port.membase + UART011_CR);
2082 new_cr = old_cr & ~UART011_CR_CTSEN;
2083 new_cr |= UART01x_CR_UARTEN | UART011_CR_TXE;
2084 writew(new_cr, uap->port.membase + UART011_CR);
2085 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086
Russell Kingd3587882006-03-20 20:00:09 +00002087 uart_console_write(&uap->port, s, count, pl011_console_putchar);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088
2089 /*
2090 * Finally, wait for transmitter to become empty
2091 * and restore the TCR
2092 */
2093 do {
2094 status = readw(uap->port.membase + UART01x_FR);
2095 } while (status & UART01x_FR_BUSY);
Andre Przywara71eec482015-05-21 17:26:21 +01002096 if (!uap->vendor->always_enabled)
2097 writew(old_cr, uap->port.membase + UART011_CR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098
Rabin Vincentef605fd2012-01-17 11:52:28 +01002099 if (locked)
2100 spin_unlock(&uap->port.lock);
2101 local_irq_restore(flags);
2102
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 clk_disable(uap->clk);
2104}
2105
2106static void __init
2107pl011_console_get_options(struct uart_amba_port *uap, int *baud,
2108 int *parity, int *bits)
2109{
2110 if (readw(uap->port.membase + UART011_CR) & UART01x_CR_UARTEN) {
2111 unsigned int lcr_h, ibrd, fbrd;
2112
Linus Walleijec489aa2010-06-02 08:13:52 +01002113 lcr_h = readw(uap->port.membase + uap->lcrh_tx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114
2115 *parity = 'n';
2116 if (lcr_h & UART01x_LCRH_PEN) {
2117 if (lcr_h & UART01x_LCRH_EPS)
2118 *parity = 'e';
2119 else
2120 *parity = 'o';
2121 }
2122
2123 if ((lcr_h & 0x60) == UART01x_LCRH_WLEN_7)
2124 *bits = 7;
2125 else
2126 *bits = 8;
2127
2128 ibrd = readw(uap->port.membase + UART011_IBRD);
2129 fbrd = readw(uap->port.membase + UART011_FBRD);
2130
2131 *baud = uap->port.uartclk * 4 / (64 * ibrd + fbrd);
Linus Walleijac3e3fb2010-06-02 20:40:22 +01002132
Russell Kingc19f12b2010-12-22 17:48:26 +00002133 if (uap->vendor->oversampling) {
Linus Walleijac3e3fb2010-06-02 20:40:22 +01002134 if (readw(uap->port.membase + UART011_CR)
2135 & ST_UART011_CR_OVSFACT)
2136 *baud *= 2;
2137 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 }
2139}
2140
2141static int __init pl011_console_setup(struct console *co, char *options)
2142{
2143 struct uart_amba_port *uap;
2144 int baud = 38400;
2145 int bits = 8;
2146 int parity = 'n';
2147 int flow = 'n';
Russell King4b4851c2011-09-22 11:35:30 +01002148 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149
2150 /*
2151 * Check whether an invalid uart number has been specified, and
2152 * if so, search for the first available port that does have
2153 * console support.
2154 */
2155 if (co->index >= UART_NR)
2156 co->index = 0;
2157 uap = amba_ports[co->index];
Russell Kingd28122a2007-01-22 18:59:42 +00002158 if (!uap)
2159 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160
Linus Walleij78d80c52012-05-23 21:18:46 +02002161 /* Allow pins to be muxed in and configured */
Linus Walleij2b996fc2013-06-05 15:36:42 +02002162 pinctrl_pm_select_default_state(uap->port.dev);
Linus Walleij78d80c52012-05-23 21:18:46 +02002163
Russell King4b4851c2011-09-22 11:35:30 +01002164 ret = clk_prepare(uap->clk);
2165 if (ret)
2166 return ret;
2167
Jingoo Han574de552013-07-30 17:06:57 +09002168 if (dev_get_platdata(uap->port.dev)) {
Shreshtha Kumar Sahuc16d51a2011-06-13 10:11:33 +02002169 struct amba_pl011_data *plat;
2170
Jingoo Han574de552013-07-30 17:06:57 +09002171 plat = dev_get_platdata(uap->port.dev);
Shreshtha Kumar Sahuc16d51a2011-06-13 10:11:33 +02002172 if (plat->init)
2173 plat->init();
2174 }
2175
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 uap->port.uartclk = clk_get_rate(uap->clk);
2177
Andre Przywaracefc2d12015-05-21 17:26:22 +01002178 if (uap->vendor->fixed_options) {
2179 baud = uap->fixed_baud;
2180 } else {
2181 if (options)
2182 uart_parse_options(options,
2183 &baud, &parity, &bits, &flow);
2184 else
2185 pl011_console_get_options(uap, &baud, &parity, &bits);
2186 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187
2188 return uart_set_options(&uap->port, co, baud, parity, bits, flow);
2189}
2190
Vincent Sanders2d934862005-09-14 22:36:03 +01002191static struct uart_driver amba_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192static struct console amba_console = {
2193 .name = "ttyAMA",
2194 .write = pl011_console_write,
2195 .device = uart_console_device,
2196 .setup = pl011_console_setup,
2197 .flags = CON_PRINTBUFFER,
2198 .index = -1,
2199 .data = &amba_reg,
2200};
2201
2202#define AMBA_CONSOLE (&amba_console)
Rob Herring0d3c6732014-04-18 17:19:57 -05002203
2204static void pl011_putc(struct uart_port *port, int c)
2205{
2206 while (readl(port->membase + UART01x_FR) & UART01x_FR_TXFF)
2207 ;
2208 writeb(c, port->membase + UART01x_DR);
2209 while (readl(port->membase + UART01x_FR) & UART01x_FR_BUSY)
2210 ;
2211}
2212
2213static void pl011_early_write(struct console *con, const char *s, unsigned n)
2214{
2215 struct earlycon_device *dev = con->data;
2216
2217 uart_console_write(&dev->port, s, n, pl011_putc);
2218}
2219
2220static int __init pl011_early_console_setup(struct earlycon_device *device,
2221 const char *opt)
2222{
2223 if (!device->port.membase)
2224 return -ENODEV;
2225
2226 device->con->write = pl011_early_write;
2227 return 0;
2228}
2229EARLYCON_DECLARE(pl011, pl011_early_console_setup);
Rob Herring45e0f0f2014-03-27 08:08:03 -05002230OF_EARLYCON_DECLARE(pl011, "arm,pl011", pl011_early_console_setup);
Rob Herring0d3c6732014-04-18 17:19:57 -05002231
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232#else
2233#define AMBA_CONSOLE NULL
2234#endif
2235
2236static struct uart_driver amba_reg = {
2237 .owner = THIS_MODULE,
2238 .driver_name = "ttyAMA",
2239 .dev_name = "ttyAMA",
2240 .major = SERIAL_AMBA_MAJOR,
2241 .minor = SERIAL_AMBA_MINOR,
2242 .nr = UART_NR,
2243 .cons = AMBA_CONSOLE,
2244};
2245
Matthew Leach32614aa2012-08-28 16:41:28 +01002246static int pl011_probe_dt_alias(int index, struct device *dev)
2247{
2248 struct device_node *np;
2249 static bool seen_dev_with_alias = false;
2250 static bool seen_dev_without_alias = false;
2251 int ret = index;
2252
2253 if (!IS_ENABLED(CONFIG_OF))
2254 return ret;
2255
2256 np = dev->of_node;
2257 if (!np)
2258 return ret;
2259
2260 ret = of_alias_get_id(np, "serial");
2261 if (IS_ERR_VALUE(ret)) {
2262 seen_dev_without_alias = true;
2263 ret = index;
2264 } else {
2265 seen_dev_with_alias = true;
2266 if (ret >= ARRAY_SIZE(amba_ports) || amba_ports[ret] != NULL) {
2267 dev_warn(dev, "requested serial port %d not available.\n", ret);
2268 ret = index;
2269 }
2270 }
2271
2272 if (seen_dev_with_alias && seen_dev_without_alias)
2273 dev_warn(dev, "aliased and non-aliased serial devices found in device tree. Serial port enumeration may be unpredictable.\n");
2274
2275 return ret;
2276}
2277
Andre Przywara49bb3c82015-05-21 17:26:14 +01002278/* unregisters the driver also if no more ports are left */
2279static void pl011_unregister_port(struct uart_amba_port *uap)
2280{
2281 int i;
2282 bool busy = false;
2283
2284 for (i = 0; i < ARRAY_SIZE(amba_ports); i++) {
2285 if (amba_ports[i] == uap)
2286 amba_ports[i] = NULL;
2287 else if (amba_ports[i])
2288 busy = true;
2289 }
2290 pl011_dma_remove(uap);
2291 if (!busy)
2292 uart_unregister_driver(&amba_reg);
2293}
2294
Andre Przywara3873e2d2015-05-21 17:26:18 +01002295static int pl011_find_free_port(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296{
Andre Przywara3873e2d2015-05-21 17:26:18 +01002297 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298
2299 for (i = 0; i < ARRAY_SIZE(amba_ports); i++)
2300 if (amba_ports[i] == NULL)
Andre Przywara3873e2d2015-05-21 17:26:18 +01002301 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302
Andre Przywara3873e2d2015-05-21 17:26:18 +01002303 return -EBUSY;
2304}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305
Andre Przywara3873e2d2015-05-21 17:26:18 +01002306static int pl011_setup_port(struct device *dev, struct uart_amba_port *uap,
2307 struct resource *mmiobase, int index)
2308{
2309 void __iomem *base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310
Andre Przywara3873e2d2015-05-21 17:26:18 +01002311 base = devm_ioremap_resource(dev, mmiobase);
Tushar Behera7f6d9422014-06-26 15:35:35 +05302312 if (!base)
2313 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314
Andre Przywara3873e2d2015-05-21 17:26:18 +01002315 index = pl011_probe_dt_alias(index, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316
Shreshtha Kumar Sahud8d8ffa2012-01-18 15:53:59 +05302317 uap->old_cr = 0;
Andre Przywara3873e2d2015-05-21 17:26:18 +01002318 uap->port.dev = dev;
2319 uap->port.mapbase = mmiobase->start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 uap->port.membase = base;
2321 uap->port.iotype = UPIO_MEM;
Russell Kingffca2b12010-12-22 17:13:05 +00002322 uap->port.fifosize = uap->fifosize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 uap->port.flags = UPF_BOOT_AUTOCONF;
Andre Przywara3873e2d2015-05-21 17:26:18 +01002324 uap->port.line = index;
2325
2326 amba_ports[index] = uap;
2327
2328 return 0;
2329}
2330
2331static int pl011_register_port(struct uart_amba_port *uap)
2332{
2333 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334
Linus Walleijc3d8b762012-03-21 20:15:18 +01002335 /* Ensure interrupts from this UART are masked and cleared */
2336 writew(0, uap->port.membase + UART011_IMSC);
2337 writew(0xffff, uap->port.membase + UART011_ICR);
2338
Tushar Beheraef2889f2014-01-20 14:32:35 +05302339 if (!amba_reg.state) {
2340 ret = uart_register_driver(&amba_reg);
2341 if (ret < 0) {
Andre Przywara3873e2d2015-05-21 17:26:18 +01002342 dev_err(uap->port.dev,
Jorge Ramirez-Ortiz1c9be312015-03-06 13:05:40 -05002343 "Failed to register AMBA-PL011 driver\n");
Tushar Beheraef2889f2014-01-20 14:32:35 +05302344 return ret;
2345 }
2346 }
2347
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 ret = uart_add_one_port(&amba_reg, &uap->port);
Andre Przywara49bb3c82015-05-21 17:26:14 +01002349 if (ret)
2350 pl011_unregister_port(uap);
Tushar Behera7f6d9422014-06-26 15:35:35 +05302351
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 return ret;
2353}
2354
Andre Przywara3873e2d2015-05-21 17:26:18 +01002355static int pl011_probe(struct amba_device *dev, const struct amba_id *id)
2356{
2357 struct uart_amba_port *uap;
2358 struct vendor_data *vendor = id->data;
2359 int portnr, ret;
2360
2361 portnr = pl011_find_free_port();
2362 if (portnr < 0)
2363 return portnr;
2364
2365 uap = devm_kzalloc(&dev->dev, sizeof(struct uart_amba_port),
2366 GFP_KERNEL);
2367 if (!uap)
2368 return -ENOMEM;
2369
2370 uap->clk = devm_clk_get(&dev->dev, NULL);
2371 if (IS_ERR(uap->clk))
2372 return PTR_ERR(uap->clk);
2373
2374 uap->vendor = vendor;
2375 uap->lcrh_rx = vendor->lcrh_rx;
2376 uap->lcrh_tx = vendor->lcrh_tx;
2377 uap->fifosize = vendor->get_fifosize(dev);
2378 uap->port.irq = dev->irq[0];
2379 uap->port.ops = &amba_pl011_pops;
2380
2381 snprintf(uap->type, sizeof(uap->type), "PL011 rev%u", amba_rev(dev));
2382
2383 ret = pl011_setup_port(&dev->dev, uap, &dev->res, portnr);
2384 if (ret)
2385 return ret;
2386
2387 amba_set_drvdata(dev, uap);
2388
2389 return pl011_register_port(uap);
2390}
2391
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392static int pl011_remove(struct amba_device *dev)
2393{
2394 struct uart_amba_port *uap = amba_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 uart_remove_one_port(&amba_reg, &uap->port);
Andre Przywara49bb3c82015-05-21 17:26:14 +01002397 pl011_unregister_port(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 return 0;
2399}
2400
Ulf Hanssond0ce8502013-12-03 11:04:28 +01002401#ifdef CONFIG_PM_SLEEP
2402static int pl011_suspend(struct device *dev)
Leo Chenb736b892009-07-28 23:43:33 +01002403{
Ulf Hanssond0ce8502013-12-03 11:04:28 +01002404 struct uart_amba_port *uap = dev_get_drvdata(dev);
Leo Chenb736b892009-07-28 23:43:33 +01002405
2406 if (!uap)
2407 return -EINVAL;
2408
2409 return uart_suspend_port(&amba_reg, &uap->port);
2410}
2411
Ulf Hanssond0ce8502013-12-03 11:04:28 +01002412static int pl011_resume(struct device *dev)
Leo Chenb736b892009-07-28 23:43:33 +01002413{
Ulf Hanssond0ce8502013-12-03 11:04:28 +01002414 struct uart_amba_port *uap = dev_get_drvdata(dev);
Leo Chenb736b892009-07-28 23:43:33 +01002415
2416 if (!uap)
2417 return -EINVAL;
2418
2419 return uart_resume_port(&amba_reg, &uap->port);
2420}
2421#endif
2422
Ulf Hanssond0ce8502013-12-03 11:04:28 +01002423static SIMPLE_DEV_PM_OPS(pl011_dev_pm_ops, pl011_suspend, pl011_resume);
2424
Andre Przywara0dd1e242015-05-21 17:26:23 +01002425static int sbsa_uart_probe(struct platform_device *pdev)
2426{
2427 struct uart_amba_port *uap;
2428 struct resource *r;
2429 int portnr, ret;
2430 int baudrate;
2431
2432 /*
2433 * Check the mandatory baud rate parameter in the DT node early
2434 * so that we can easily exit with the error.
2435 */
2436 if (pdev->dev.of_node) {
2437 struct device_node *np = pdev->dev.of_node;
2438
2439 ret = of_property_read_u32(np, "current-speed", &baudrate);
2440 if (ret)
2441 return ret;
2442 } else {
2443 baudrate = 115200;
2444 }
2445
2446 portnr = pl011_find_free_port();
2447 if (portnr < 0)
2448 return portnr;
2449
2450 uap = devm_kzalloc(&pdev->dev, sizeof(struct uart_amba_port),
2451 GFP_KERNEL);
2452 if (!uap)
2453 return -ENOMEM;
2454
2455 uap->vendor = &vendor_sbsa;
2456 uap->fifosize = 32;
2457 uap->port.irq = platform_get_irq(pdev, 0);
2458 uap->port.ops = &sbsa_uart_pops;
2459 uap->fixed_baud = baudrate;
2460
2461 snprintf(uap->type, sizeof(uap->type), "SBSA");
2462
2463 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2464
2465 ret = pl011_setup_port(&pdev->dev, uap, r, portnr);
2466 if (ret)
2467 return ret;
2468
2469 platform_set_drvdata(pdev, uap);
2470
2471 return pl011_register_port(uap);
2472}
2473
2474static int sbsa_uart_remove(struct platform_device *pdev)
2475{
2476 struct uart_amba_port *uap = platform_get_drvdata(pdev);
2477
2478 uart_remove_one_port(&amba_reg, &uap->port);
2479 pl011_unregister_port(uap);
2480 return 0;
2481}
2482
2483static const struct of_device_id sbsa_uart_of_match[] = {
2484 { .compatible = "arm,sbsa-uart", },
2485 {},
2486};
2487MODULE_DEVICE_TABLE(of, sbsa_uart_of_match);
2488
2489static struct platform_driver arm_sbsa_uart_platform_driver = {
2490 .probe = sbsa_uart_probe,
2491 .remove = sbsa_uart_remove,
2492 .driver = {
2493 .name = "sbsa-uart",
2494 .of_match_table = of_match_ptr(sbsa_uart_of_match),
2495 },
2496};
2497
Russell King2c39c9e2010-07-27 08:50:16 +01002498static struct amba_id pl011_ids[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 {
2500 .id = 0x00041011,
2501 .mask = 0x000fffff,
Alessandro Rubini5926a292009-06-04 17:43:04 +01002502 .data = &vendor_arm,
2503 },
2504 {
2505 .id = 0x00380802,
2506 .mask = 0x00ffffff,
2507 .data = &vendor_st,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 },
2509 { 0, 0 },
2510};
2511
Dave Martin60f7a332011-10-05 15:15:22 +01002512MODULE_DEVICE_TABLE(amba, pl011_ids);
2513
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514static struct amba_driver pl011_driver = {
2515 .drv = {
2516 .name = "uart-pl011",
Ulf Hanssond0ce8502013-12-03 11:04:28 +01002517 .pm = &pl011_dev_pm_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518 },
2519 .id_table = pl011_ids,
2520 .probe = pl011_probe,
2521 .remove = pl011_remove,
2522};
2523
2524static int __init pl011_init(void)
2525{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526 printk(KERN_INFO "Serial: AMBA PL011 UART driver\n");
2527
Andre Przywara0dd1e242015-05-21 17:26:23 +01002528 if (platform_driver_register(&arm_sbsa_uart_platform_driver))
2529 pr_warn("could not register SBSA UART platform driver\n");
Tushar Beheraef2889f2014-01-20 14:32:35 +05302530 return amba_driver_register(&pl011_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531}
2532
2533static void __exit pl011_exit(void)
2534{
Andre Przywara0dd1e242015-05-21 17:26:23 +01002535 platform_driver_unregister(&arm_sbsa_uart_platform_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 amba_driver_unregister(&pl011_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537}
2538
Alessandro Rubini4dd9e742009-05-05 05:54:13 +01002539/*
2540 * While this can be a module, if builtin it's most likely the console
2541 * So let's leave module_exit but move module_init to an earlier place
2542 */
2543arch_initcall(pl011_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544module_exit(pl011_exit);
2545
2546MODULE_AUTHOR("ARM Ltd/Deep Blue Solutions Ltd");
2547MODULE_DESCRIPTION("ARM AMBA serial port driver");
2548MODULE_LICENSE("GPL");