blob: e1f3bd5afad6d0c78eaa840fcae2034a6099a0d0 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Driver for AMBA serial ports
3 *
4 * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
5 *
6 * Copyright 1999 ARM Limited
7 * Copyright (C) 2000 Deep Blue Solutions Ltd.
Russell King68b65f72010-12-22 17:24:39 +00008 * Copyright (C) 2010 ST-Ericsson SA
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 * This is a generic driver for ARM AMBA-type serial ports. They
25 * have a lot of 16550-like features, but are not register compatible.
26 * Note that although they do have CTS, DCD and DSR inputs, they do
27 * not have an RI input, nor do they have DTR or RTS outputs. If
28 * required, these have to be supplied via some other means (eg, GPIO)
29 * and hooked into this driver.
30 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Chanho Mincb06ff12013-03-27 18:38:11 +090032
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#if defined(CONFIG_SERIAL_AMBA_PL011_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
34#define SUPPORT_SYSRQ
35#endif
36
37#include <linux/module.h>
38#include <linux/ioport.h>
39#include <linux/init.h>
40#include <linux/console.h>
41#include <linux/sysrq.h>
42#include <linux/device.h>
43#include <linux/tty.h>
44#include <linux/tty_flip.h>
45#include <linux/serial_core.h>
46#include <linux/serial.h>
Russell Kinga62c80e2006-01-07 13:52:45 +000047#include <linux/amba/bus.h>
48#include <linux/amba/serial.h>
Russell Kingf8ce2542006-01-07 16:15:52 +000049#include <linux/clk.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090050#include <linux/slab.h>
Russell King68b65f72010-12-22 17:24:39 +000051#include <linux/dmaengine.h>
52#include <linux/dma-mapping.h>
53#include <linux/scatterlist.h>
Shreshtha Kumar Sahuc16d51a2011-06-13 10:11:33 +020054#include <linux/delay.h>
Viresh Kumar258aea72012-02-01 16:12:19 +053055#include <linux/types.h>
Matthew Leach32614aa2012-08-28 16:41:28 +010056#include <linux/of.h>
57#include <linux/of_device.h>
Shawn Guo258e0552012-05-06 22:53:35 +080058#include <linux/pinctrl/consumer.h>
Alessandro Rubinicb707062012-06-24 12:46:37 +010059#include <linux/sizes.h>
Linus Walleijde609582012-10-15 13:36:01 +020060#include <linux/io.h>
Graeme Gregory3db9ab02015-05-21 17:26:24 +010061#include <linux/acpi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
63#define UART_NR 14
64
65#define SERIAL_AMBA_MAJOR 204
66#define SERIAL_AMBA_MINOR 64
67#define SERIAL_AMBA_NR UART_NR
68
69#define AMBA_ISR_PASS_LIMIT 256
70
Russell Kingb63d4f02005-11-19 11:10:35 +000071#define UART_DR_ERROR (UART011_DR_OE|UART011_DR_BE|UART011_DR_PE|UART011_DR_FE)
72#define UART_DUMMY_DR_RX (1 << 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
Alessandro Rubini5926a292009-06-04 17:43:04 +010074/* There is by now at least one vendor with differing details, so handle it */
75struct vendor_data {
76 unsigned int ifls;
Linus Walleijec489aa2010-06-02 08:13:52 +010077 unsigned int lcrh_tx;
78 unsigned int lcrh_rx;
Jun Nie2c096a92015-07-31 15:49:17 +080079 u16 *reg_lut;
Linus Walleijac3e3fb2010-06-02 20:40:22 +010080 bool oversampling;
Russell King38d62432010-12-22 17:59:16 +000081 bool dma_threshold;
Rajanikanth H.V4fd06902012-03-26 11:17:02 +020082 bool cts_event_workaround;
Andre Przywara71eec482015-05-21 17:26:21 +010083 bool always_enabled;
Andre Przywaracefc2d12015-05-21 17:26:22 +010084 bool fixed_options;
Jongsung Kim78506f22013-04-15 14:45:25 +090085
Jongsung Kimea336402013-05-10 18:05:35 +090086 unsigned int (*get_fifosize)(struct amba_device *dev);
Alessandro Rubini5926a292009-06-04 17:43:04 +010087};
88
Jun Nie7b753f32015-07-31 15:49:16 +080089/* Max address offset of register in use is 0x48 */
90#define REG_NR (0x48 >> 2)
91#define IDX(x) (x >> 2)
Jun Nie534e14e2015-07-31 15:49:15 +080092enum reg_idx {
Jun Nie7b753f32015-07-31 15:49:16 +080093 REG_DR = IDX(UART01x_DR),
94 REG_RSR = IDX(UART01x_RSR),
95 REG_ST_DMAWM = IDX(ST_UART011_DMAWM),
96 REG_FR = IDX(UART01x_FR),
97 REG_ST_LCRH_RX = IDX(ST_UART011_LCRH_RX),
98 REG_ILPR = IDX(UART01x_ILPR),
99 REG_IBRD = IDX(UART011_IBRD),
100 REG_FBRD = IDX(UART011_FBRD),
101 REG_LCRH = IDX(UART011_LCRH),
102 REG_CR = IDX(UART011_CR),
103 REG_IFLS = IDX(UART011_IFLS),
104 REG_IMSC = IDX(UART011_IMSC),
105 REG_RIS = IDX(UART011_RIS),
106 REG_MIS = IDX(UART011_MIS),
107 REG_ICR = IDX(UART011_ICR),
108 REG_DMACR = IDX(UART011_DMACR),
Jun Nie534e14e2015-07-31 15:49:15 +0800109};
110
Jun Nie2c096a92015-07-31 15:49:17 +0800111static u16 arm_reg[] = {
112 [REG_DR] = UART01x_DR,
113 [REG_RSR] = UART01x_RSR,
114 [REG_ST_DMAWM] = ~0,
115 [REG_FR] = UART01x_FR,
116 [REG_ST_LCRH_RX] = ~0,
117 [REG_ILPR] = UART01x_ILPR,
118 [REG_IBRD] = UART011_IBRD,
119 [REG_FBRD] = UART011_FBRD,
120 [REG_LCRH] = UART011_LCRH,
121 [REG_CR] = UART011_CR,
122 [REG_IFLS] = UART011_IFLS,
123 [REG_IMSC] = UART011_IMSC,
124 [REG_RIS] = UART011_RIS,
125 [REG_MIS] = UART011_MIS,
126 [REG_ICR] = UART011_ICR,
127 [REG_DMACR] = UART011_DMACR,
128};
129
Jongsung Kimea336402013-05-10 18:05:35 +0900130static unsigned int get_fifosize_arm(struct amba_device *dev)
Jongsung Kim78506f22013-04-15 14:45:25 +0900131{
Jongsung Kimea336402013-05-10 18:05:35 +0900132 return amba_rev(dev) < 3 ? 16 : 32;
Jongsung Kim78506f22013-04-15 14:45:25 +0900133}
134
Alessandro Rubini5926a292009-06-04 17:43:04 +0100135static struct vendor_data vendor_arm = {
136 .ifls = UART011_IFLS_RX4_8|UART011_IFLS_TX4_8,
Jun Nie534e14e2015-07-31 15:49:15 +0800137 .lcrh_tx = REG_LCRH,
138 .lcrh_rx = REG_LCRH,
Jun Nie2c096a92015-07-31 15:49:17 +0800139 .reg_lut = arm_reg,
Linus Walleijac3e3fb2010-06-02 20:40:22 +0100140 .oversampling = false,
Russell King38d62432010-12-22 17:59:16 +0000141 .dma_threshold = false,
Rajanikanth H.V4fd06902012-03-26 11:17:02 +0200142 .cts_event_workaround = false,
Andre Przywara71eec482015-05-21 17:26:21 +0100143 .always_enabled = false,
Andre Przywaracefc2d12015-05-21 17:26:22 +0100144 .fixed_options = false,
Jongsung Kim78506f22013-04-15 14:45:25 +0900145 .get_fifosize = get_fifosize_arm,
Alessandro Rubini5926a292009-06-04 17:43:04 +0100146};
147
Andre Przywara0dd1e242015-05-21 17:26:23 +0100148static struct vendor_data vendor_sbsa = {
Jun Nie2c096a92015-07-31 15:49:17 +0800149 .reg_lut = arm_reg,
Andre Przywara0dd1e242015-05-21 17:26:23 +0100150 .oversampling = false,
151 .dma_threshold = false,
152 .cts_event_workaround = false,
153 .always_enabled = true,
154 .fixed_options = true,
155};
156
Jun Nie2c096a92015-07-31 15:49:17 +0800157static u16 st_reg[] = {
158 [REG_DR] = UART01x_DR,
159 [REG_RSR] = UART01x_RSR,
160 [REG_ST_DMAWM] = ST_UART011_DMAWM,
161 [REG_FR] = UART01x_FR,
162 [REG_ST_LCRH_RX] = ST_UART011_LCRH_RX,
163 [REG_ILPR] = UART01x_ILPR,
164 [REG_IBRD] = UART011_IBRD,
165 [REG_FBRD] = UART011_FBRD,
166 [REG_LCRH] = UART011_LCRH,
167 [REG_CR] = UART011_CR,
168 [REG_IFLS] = UART011_IFLS,
169 [REG_IMSC] = UART011_IMSC,
170 [REG_RIS] = UART011_RIS,
171 [REG_MIS] = UART011_MIS,
172 [REG_ICR] = UART011_ICR,
173 [REG_DMACR] = UART011_DMACR,
174};
175
Jongsung Kimea336402013-05-10 18:05:35 +0900176static unsigned int get_fifosize_st(struct amba_device *dev)
Jongsung Kim78506f22013-04-15 14:45:25 +0900177{
178 return 64;
179}
180
Alessandro Rubini5926a292009-06-04 17:43:04 +0100181static struct vendor_data vendor_st = {
182 .ifls = UART011_IFLS_RX_HALF|UART011_IFLS_TX_HALF,
Jun Nie534e14e2015-07-31 15:49:15 +0800183 .lcrh_tx = REG_LCRH,
184 .lcrh_rx = REG_ST_LCRH_RX,
Jun Nie2c096a92015-07-31 15:49:17 +0800185 .reg_lut = st_reg,
Linus Walleijac3e3fb2010-06-02 20:40:22 +0100186 .oversampling = true,
Russell King38d62432010-12-22 17:59:16 +0000187 .dma_threshold = true,
Rajanikanth H.V4fd06902012-03-26 11:17:02 +0200188 .cts_event_workaround = true,
Andre Przywara71eec482015-05-21 17:26:21 +0100189 .always_enabled = false,
Andre Przywaracefc2d12015-05-21 17:26:22 +0100190 .fixed_options = false,
Jongsung Kim78506f22013-04-15 14:45:25 +0900191 .get_fifosize = get_fifosize_st,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192};
193
Russell King68b65f72010-12-22 17:24:39 +0000194/* Deals with DMA transactions */
Linus Walleijead76f32011-02-24 13:21:08 +0100195
196struct pl011_sgbuf {
197 struct scatterlist sg;
198 char *buf;
199};
200
201struct pl011_dmarx_data {
202 struct dma_chan *chan;
203 struct completion complete;
204 bool use_buf_b;
205 struct pl011_sgbuf sgbuf_a;
206 struct pl011_sgbuf sgbuf_b;
207 dma_cookie_t cookie;
208 bool running;
Chanho Mincb06ff12013-03-27 18:38:11 +0900209 struct timer_list timer;
210 unsigned int last_residue;
211 unsigned long last_jiffies;
212 bool auto_poll_rate;
213 unsigned int poll_rate;
214 unsigned int poll_timeout;
Linus Walleijead76f32011-02-24 13:21:08 +0100215};
216
Russell King68b65f72010-12-22 17:24:39 +0000217struct pl011_dmatx_data {
218 struct dma_chan *chan;
219 struct scatterlist sg;
220 char *buf;
221 bool queued;
222};
223
Russell Kingc19f12b2010-12-22 17:48:26 +0000224/*
225 * We wrap our port structure around the generic uart_port.
226 */
227struct uart_amba_port {
228 struct uart_port port;
229 struct clk *clk;
230 const struct vendor_data *vendor;
Jun Nie2c096a92015-07-31 15:49:17 +0800231 u16 *reg_lut;
Russell King68b65f72010-12-22 17:24:39 +0000232 unsigned int dmacr; /* dma control reg */
Russell Kingc19f12b2010-12-22 17:48:26 +0000233 unsigned int im; /* interrupt mask */
234 unsigned int old_status;
Russell Kingffca2b12010-12-22 17:13:05 +0000235 unsigned int fifosize; /* vendor-specific */
Russell Kingc19f12b2010-12-22 17:48:26 +0000236 unsigned int lcrh_tx; /* vendor-specific */
237 unsigned int lcrh_rx; /* vendor-specific */
Shreshtha Kumar Sahud8d8ffa2012-01-18 15:53:59 +0530238 unsigned int old_cr; /* state during shutdown */
Russell Kingc19f12b2010-12-22 17:48:26 +0000239 bool autorts;
Andre Przywaracefc2d12015-05-21 17:26:22 +0100240 unsigned int fixed_baud; /* vendor-set fixed baud rate */
Russell Kingc19f12b2010-12-22 17:48:26 +0000241 char type[12];
Russell King68b65f72010-12-22 17:24:39 +0000242#ifdef CONFIG_DMA_ENGINE
243 /* DMA stuff */
Linus Walleijead76f32011-02-24 13:21:08 +0100244 bool using_tx_dma;
245 bool using_rx_dma;
246 struct pl011_dmarx_data dmarx;
Russell King68b65f72010-12-22 17:24:39 +0000247 struct pl011_dmatx_data dmatx;
Jorge Ramirez-Ortiz1c9be312015-03-06 13:05:40 -0500248 bool dma_probed;
Russell King68b65f72010-12-22 17:24:39 +0000249#endif
Russell Kingc19f12b2010-12-22 17:48:26 +0000250};
251
Jun Nie7b753f32015-07-31 15:49:16 +0800252static unsigned int pl011_readw(struct uart_amba_port *uap, int index)
253{
254 WARN_ON(index > REG_NR);
Jun Nie2c096a92015-07-31 15:49:17 +0800255 return readw_relaxed(uap->port.membase + uap->reg_lut[index]);
Jun Nie7b753f32015-07-31 15:49:16 +0800256}
257
258static void pl011_writew(struct uart_amba_port *uap, int val, int index)
259{
260 WARN_ON(index > REG_NR);
Jun Nie2c096a92015-07-31 15:49:17 +0800261 writew_relaxed(val, uap->port.membase + uap->reg_lut[index]);
Jun Nie7b753f32015-07-31 15:49:16 +0800262}
263
264static void pl011_writeb(struct uart_amba_port *uap, u8 val, int index)
265{
266 WARN_ON(index > REG_NR);
Jun Nie2c096a92015-07-31 15:49:17 +0800267 writeb_relaxed(val, uap->port.membase + uap->reg_lut[index]);
Jun Nie7b753f32015-07-31 15:49:16 +0800268}
269
Russell King68b65f72010-12-22 17:24:39 +0000270/*
Linus Walleij29772c42011-02-24 13:21:36 +0100271 * Reads up to 256 characters from the FIFO or until it's empty and
272 * inserts them into the TTY layer. Returns the number of characters
273 * read from the FIFO.
274 */
275static int pl011_fifo_to_tty(struct uart_amba_port *uap)
276{
277 u16 status, ch;
278 unsigned int flag, max_count = 256;
279 int fifotaken = 0;
280
281 while (max_count--) {
Jun Nie7b753f32015-07-31 15:49:16 +0800282 status = pl011_readw(uap, REG_FR);
Linus Walleij29772c42011-02-24 13:21:36 +0100283 if (status & UART01x_FR_RXFE)
284 break;
285
286 /* Take chars from the FIFO and update status */
Jun Nie7b753f32015-07-31 15:49:16 +0800287 ch = pl011_readw(uap, REG_DR) |
Linus Walleij29772c42011-02-24 13:21:36 +0100288 UART_DUMMY_DR_RX;
289 flag = TTY_NORMAL;
290 uap->port.icount.rx++;
291 fifotaken++;
292
293 if (unlikely(ch & UART_DR_ERROR)) {
294 if (ch & UART011_DR_BE) {
295 ch &= ~(UART011_DR_FE | UART011_DR_PE);
296 uap->port.icount.brk++;
297 if (uart_handle_break(&uap->port))
298 continue;
299 } else if (ch & UART011_DR_PE)
300 uap->port.icount.parity++;
301 else if (ch & UART011_DR_FE)
302 uap->port.icount.frame++;
303 if (ch & UART011_DR_OE)
304 uap->port.icount.overrun++;
305
306 ch &= uap->port.read_status_mask;
307
308 if (ch & UART011_DR_BE)
309 flag = TTY_BREAK;
310 else if (ch & UART011_DR_PE)
311 flag = TTY_PARITY;
312 else if (ch & UART011_DR_FE)
313 flag = TTY_FRAME;
314 }
315
316 if (uart_handle_sysrq_char(&uap->port, ch & 255))
317 continue;
318
319 uart_insert_char(&uap->port, ch, UART011_DR_OE, ch, flag);
320 }
321
322 return fifotaken;
323}
324
325
326/*
Russell King68b65f72010-12-22 17:24:39 +0000327 * All the DMA operation mode stuff goes inside this ifdef.
328 * This assumes that you have a generic DMA device interface,
329 * no custom DMA interfaces are supported.
330 */
331#ifdef CONFIG_DMA_ENGINE
332
333#define PL011_DMA_BUFFER_SIZE PAGE_SIZE
334
Linus Walleijead76f32011-02-24 13:21:08 +0100335static int pl011_sgbuf_init(struct dma_chan *chan, struct pl011_sgbuf *sg,
336 enum dma_data_direction dir)
337{
Chanho Mincb06ff12013-03-27 18:38:11 +0900338 dma_addr_t dma_addr;
339
340 sg->buf = dma_alloc_coherent(chan->device->dev,
341 PL011_DMA_BUFFER_SIZE, &dma_addr, GFP_KERNEL);
Linus Walleijead76f32011-02-24 13:21:08 +0100342 if (!sg->buf)
343 return -ENOMEM;
344
Chanho Mincb06ff12013-03-27 18:38:11 +0900345 sg_init_table(&sg->sg, 1);
346 sg_set_page(&sg->sg, phys_to_page(dma_addr),
347 PL011_DMA_BUFFER_SIZE, offset_in_page(dma_addr));
348 sg_dma_address(&sg->sg) = dma_addr;
Andrew Jacksonc64be922014-11-07 14:14:43 +0000349 sg_dma_len(&sg->sg) = PL011_DMA_BUFFER_SIZE;
Linus Walleijead76f32011-02-24 13:21:08 +0100350
Linus Walleijead76f32011-02-24 13:21:08 +0100351 return 0;
352}
353
354static void pl011_sgbuf_free(struct dma_chan *chan, struct pl011_sgbuf *sg,
355 enum dma_data_direction dir)
356{
357 if (sg->buf) {
Chanho Mincb06ff12013-03-27 18:38:11 +0900358 dma_free_coherent(chan->device->dev,
359 PL011_DMA_BUFFER_SIZE, sg->buf,
360 sg_dma_address(&sg->sg));
Linus Walleijead76f32011-02-24 13:21:08 +0100361 }
362}
363
Jorge Ramirez-Ortiz1c9be312015-03-06 13:05:40 -0500364static void pl011_dma_probe(struct uart_amba_port *uap)
Russell King68b65f72010-12-22 17:24:39 +0000365{
366 /* DMA is the sole user of the platform data right now */
Jingoo Han574de552013-07-30 17:06:57 +0900367 struct amba_pl011_data *plat = dev_get_platdata(uap->port.dev);
Jorge Ramirez-Ortiz1c9be312015-03-06 13:05:40 -0500368 struct device *dev = uap->port.dev;
Russell King68b65f72010-12-22 17:24:39 +0000369 struct dma_slave_config tx_conf = {
Jun Nie2c096a92015-07-31 15:49:17 +0800370 .dst_addr = uap->port.mapbase + uap->reg_lut[REG_DR],
Russell King68b65f72010-12-22 17:24:39 +0000371 .dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE,
Vinod Koula485df42011-10-14 10:47:38 +0530372 .direction = DMA_MEM_TO_DEV,
Russell King68b65f72010-12-22 17:24:39 +0000373 .dst_maxburst = uap->fifosize >> 1,
Viresh Kumar258aea72012-02-01 16:12:19 +0530374 .device_fc = false,
Russell King68b65f72010-12-22 17:24:39 +0000375 };
376 struct dma_chan *chan;
377 dma_cap_mask_t mask;
378
Jorge Ramirez-Ortiz1c9be312015-03-06 13:05:40 -0500379 uap->dma_probed = true;
380 chan = dma_request_slave_channel_reason(dev, "tx");
381 if (IS_ERR(chan)) {
382 if (PTR_ERR(chan) == -EPROBE_DEFER) {
Jorge Ramirez-Ortiz1c9be312015-03-06 13:05:40 -0500383 uap->dma_probed = false;
384 return;
385 }
Russell King68b65f72010-12-22 17:24:39 +0000386
Arnd Bergmann787b0c12013-01-28 16:24:37 +0000387 /* We need platform data */
388 if (!plat || !plat->dma_filter) {
389 dev_info(uap->port.dev, "no DMA platform data\n");
390 return;
391 }
392
393 /* Try to acquire a generic DMA engine slave TX channel */
394 dma_cap_zero(mask);
395 dma_cap_set(DMA_SLAVE, mask);
396
397 chan = dma_request_channel(mask, plat->dma_filter,
398 plat->dma_tx_param);
399 if (!chan) {
400 dev_err(uap->port.dev, "no TX DMA channel!\n");
401 return;
402 }
Russell King68b65f72010-12-22 17:24:39 +0000403 }
404
405 dmaengine_slave_config(chan, &tx_conf);
406 uap->dmatx.chan = chan;
407
408 dev_info(uap->port.dev, "DMA channel TX %s\n",
409 dma_chan_name(uap->dmatx.chan));
Linus Walleijead76f32011-02-24 13:21:08 +0100410
411 /* Optionally make use of an RX channel as well */
Arnd Bergmann787b0c12013-01-28 16:24:37 +0000412 chan = dma_request_slave_channel(dev, "rx");
Rob Herring0d3c6732014-04-18 17:19:57 -0500413
Arnd Bergmann787b0c12013-01-28 16:24:37 +0000414 if (!chan && plat->dma_rx_param) {
415 chan = dma_request_channel(mask, plat->dma_filter, plat->dma_rx_param);
416
417 if (!chan) {
418 dev_err(uap->port.dev, "no RX DMA channel!\n");
419 return;
420 }
421 }
422
423 if (chan) {
Linus Walleijead76f32011-02-24 13:21:08 +0100424 struct dma_slave_config rx_conf = {
Jun Nie2c096a92015-07-31 15:49:17 +0800425 .src_addr = uap->port.mapbase + uap->reg_lut[REG_DR],
Linus Walleijead76f32011-02-24 13:21:08 +0100426 .src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE,
Vinod Koula485df42011-10-14 10:47:38 +0530427 .direction = DMA_DEV_TO_MEM,
Guennadi Liakhovetskib2aeb772014-04-12 19:47:17 +0200428 .src_maxburst = uap->fifosize >> 2,
Viresh Kumar258aea72012-02-01 16:12:19 +0530429 .device_fc = false,
Linus Walleijead76f32011-02-24 13:21:08 +0100430 };
Andrew Jackson2d3b7d62014-11-07 14:14:47 +0000431 struct dma_slave_caps caps;
Linus Walleijead76f32011-02-24 13:21:08 +0100432
Andrew Jackson2d3b7d62014-11-07 14:14:47 +0000433 /*
434 * Some DMA controllers provide information on their capabilities.
435 * If the controller does, check for suitable residue processing
436 * otherwise assime all is well.
437 */
438 if (0 == dma_get_slave_caps(chan, &caps)) {
439 if (caps.residue_granularity ==
440 DMA_RESIDUE_GRANULARITY_DESCRIPTOR) {
441 dma_release_channel(chan);
442 dev_info(uap->port.dev,
443 "RX DMA disabled - no residue processing\n");
444 return;
445 }
446 }
Linus Walleijead76f32011-02-24 13:21:08 +0100447 dmaengine_slave_config(chan, &rx_conf);
448 uap->dmarx.chan = chan;
449
Andrew Jackson98267d32014-11-07 14:14:23 +0000450 uap->dmarx.auto_poll_rate = false;
Greg Kroah-Hartman8f898bf2013-12-17 09:33:18 -0800451 if (plat && plat->dma_rx_poll_enable) {
Chanho Mincb06ff12013-03-27 18:38:11 +0900452 /* Set poll rate if specified. */
453 if (plat->dma_rx_poll_rate) {
454 uap->dmarx.auto_poll_rate = false;
455 uap->dmarx.poll_rate = plat->dma_rx_poll_rate;
456 } else {
457 /*
458 * 100 ms defaults to poll rate if not
459 * specified. This will be adjusted with
460 * the baud rate at set_termios.
461 */
462 uap->dmarx.auto_poll_rate = true;
463 uap->dmarx.poll_rate = 100;
464 }
465 /* 3 secs defaults poll_timeout if not specified. */
466 if (plat->dma_rx_poll_timeout)
467 uap->dmarx.poll_timeout =
468 plat->dma_rx_poll_timeout;
469 else
470 uap->dmarx.poll_timeout = 3000;
Andrew Jackson98267d32014-11-07 14:14:23 +0000471 } else if (!plat && dev->of_node) {
472 uap->dmarx.auto_poll_rate = of_property_read_bool(
473 dev->of_node, "auto-poll");
474 if (uap->dmarx.auto_poll_rate) {
475 u32 x;
Chanho Mincb06ff12013-03-27 18:38:11 +0900476
Andrew Jackson98267d32014-11-07 14:14:23 +0000477 if (0 == of_property_read_u32(dev->of_node,
478 "poll-rate-ms", &x))
479 uap->dmarx.poll_rate = x;
480 else
481 uap->dmarx.poll_rate = 100;
482 if (0 == of_property_read_u32(dev->of_node,
483 "poll-timeout-ms", &x))
484 uap->dmarx.poll_timeout = x;
485 else
486 uap->dmarx.poll_timeout = 3000;
487 }
488 }
Linus Walleijead76f32011-02-24 13:21:08 +0100489 dev_info(uap->port.dev, "DMA channel RX %s\n",
490 dma_chan_name(uap->dmarx.chan));
491 }
Russell King68b65f72010-12-22 17:24:39 +0000492}
493
Russell King68b65f72010-12-22 17:24:39 +0000494static void pl011_dma_remove(struct uart_amba_port *uap)
495{
Russell King68b65f72010-12-22 17:24:39 +0000496 if (uap->dmatx.chan)
497 dma_release_channel(uap->dmatx.chan);
Linus Walleijead76f32011-02-24 13:21:08 +0100498 if (uap->dmarx.chan)
499 dma_release_channel(uap->dmarx.chan);
Russell King68b65f72010-12-22 17:24:39 +0000500}
501
Dave Martin734745c2015-03-04 12:27:33 +0000502/* Forward declare these for the refill routine */
Russell King68b65f72010-12-22 17:24:39 +0000503static int pl011_dma_tx_refill(struct uart_amba_port *uap);
Dave Martin734745c2015-03-04 12:27:33 +0000504static void pl011_start_tx_pio(struct uart_amba_port *uap);
Russell King68b65f72010-12-22 17:24:39 +0000505
506/*
507 * The current DMA TX buffer has been sent.
508 * Try to queue up another DMA buffer.
509 */
510static void pl011_dma_tx_callback(void *data)
511{
512 struct uart_amba_port *uap = data;
513 struct pl011_dmatx_data *dmatx = &uap->dmatx;
514 unsigned long flags;
515 u16 dmacr;
516
517 spin_lock_irqsave(&uap->port.lock, flags);
518 if (uap->dmatx.queued)
519 dma_unmap_sg(dmatx->chan->device->dev, &dmatx->sg, 1,
520 DMA_TO_DEVICE);
521
522 dmacr = uap->dmacr;
523 uap->dmacr = dmacr & ~UART011_TXDMAE;
Jun Nie7b753f32015-07-31 15:49:16 +0800524 pl011_writew(uap, uap->dmacr, REG_DMACR);
Russell King68b65f72010-12-22 17:24:39 +0000525
526 /*
527 * If TX DMA was disabled, it means that we've stopped the DMA for
528 * some reason (eg, XOFF received, or we want to send an X-char.)
529 *
530 * Note: we need to be careful here of a potential race between DMA
531 * and the rest of the driver - if the driver disables TX DMA while
532 * a TX buffer completing, we must update the tx queued status to
533 * get further refills (hence we check dmacr).
534 */
535 if (!(dmacr & UART011_TXDMAE) || uart_tx_stopped(&uap->port) ||
536 uart_circ_empty(&uap->port.state->xmit)) {
537 uap->dmatx.queued = false;
538 spin_unlock_irqrestore(&uap->port.lock, flags);
539 return;
540 }
541
Dave Martin734745c2015-03-04 12:27:33 +0000542 if (pl011_dma_tx_refill(uap) <= 0)
Russell King68b65f72010-12-22 17:24:39 +0000543 /*
544 * We didn't queue a DMA buffer for some reason, but we
545 * have data pending to be sent. Re-enable the TX IRQ.
546 */
Dave Martin734745c2015-03-04 12:27:33 +0000547 pl011_start_tx_pio(uap);
548
Russell King68b65f72010-12-22 17:24:39 +0000549 spin_unlock_irqrestore(&uap->port.lock, flags);
550}
551
552/*
553 * Try to refill the TX DMA buffer.
554 * Locking: called with port lock held and IRQs disabled.
555 * Returns:
556 * 1 if we queued up a TX DMA buffer.
557 * 0 if we didn't want to handle this by DMA
558 * <0 on error
559 */
560static int pl011_dma_tx_refill(struct uart_amba_port *uap)
561{
562 struct pl011_dmatx_data *dmatx = &uap->dmatx;
563 struct dma_chan *chan = dmatx->chan;
564 struct dma_device *dma_dev = chan->device;
565 struct dma_async_tx_descriptor *desc;
566 struct circ_buf *xmit = &uap->port.state->xmit;
567 unsigned int count;
568
569 /*
570 * Try to avoid the overhead involved in using DMA if the
571 * transaction fits in the first half of the FIFO, by using
572 * the standard interrupt handling. This ensures that we
573 * issue a uart_write_wakeup() at the appropriate time.
574 */
575 count = uart_circ_chars_pending(xmit);
576 if (count < (uap->fifosize >> 1)) {
577 uap->dmatx.queued = false;
578 return 0;
579 }
580
581 /*
582 * Bodge: don't send the last character by DMA, as this
583 * will prevent XON from notifying us to restart DMA.
584 */
585 count -= 1;
586
587 /* Else proceed to copy the TX chars to the DMA buffer and fire DMA */
588 if (count > PL011_DMA_BUFFER_SIZE)
589 count = PL011_DMA_BUFFER_SIZE;
590
591 if (xmit->tail < xmit->head)
592 memcpy(&dmatx->buf[0], &xmit->buf[xmit->tail], count);
593 else {
594 size_t first = UART_XMIT_SIZE - xmit->tail;
Andrew Jacksone2a545a2014-11-07 14:14:39 +0000595 size_t second;
596
597 if (first > count)
598 first = count;
599 second = count - first;
Russell King68b65f72010-12-22 17:24:39 +0000600
601 memcpy(&dmatx->buf[0], &xmit->buf[xmit->tail], first);
602 if (second)
603 memcpy(&dmatx->buf[first], &xmit->buf[0], second);
604 }
605
606 dmatx->sg.length = count;
607
608 if (dma_map_sg(dma_dev->dev, &dmatx->sg, 1, DMA_TO_DEVICE) != 1) {
609 uap->dmatx.queued = false;
610 dev_dbg(uap->port.dev, "unable to map TX DMA\n");
611 return -EBUSY;
612 }
613
Alexandre Bounine16052822012-03-08 16:11:18 -0500614 desc = dmaengine_prep_slave_sg(chan, &dmatx->sg, 1, DMA_MEM_TO_DEV,
Russell King68b65f72010-12-22 17:24:39 +0000615 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
616 if (!desc) {
617 dma_unmap_sg(dma_dev->dev, &dmatx->sg, 1, DMA_TO_DEVICE);
618 uap->dmatx.queued = false;
619 /*
620 * If DMA cannot be used right now, we complete this
621 * transaction via IRQ and let the TTY layer retry.
622 */
623 dev_dbg(uap->port.dev, "TX DMA busy\n");
624 return -EBUSY;
625 }
626
627 /* Some data to go along to the callback */
628 desc->callback = pl011_dma_tx_callback;
629 desc->callback_param = uap;
630
631 /* All errors should happen at prepare time */
632 dmaengine_submit(desc);
633
634 /* Fire the DMA transaction */
635 dma_dev->device_issue_pending(chan);
636
637 uap->dmacr |= UART011_TXDMAE;
Jun Nie7b753f32015-07-31 15:49:16 +0800638 pl011_writew(uap, uap->dmacr, REG_DMACR);
Russell King68b65f72010-12-22 17:24:39 +0000639 uap->dmatx.queued = true;
640
641 /*
642 * Now we know that DMA will fire, so advance the ring buffer
643 * with the stuff we just dispatched.
644 */
645 xmit->tail = (xmit->tail + count) & (UART_XMIT_SIZE - 1);
646 uap->port.icount.tx += count;
647
648 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
649 uart_write_wakeup(&uap->port);
650
651 return 1;
652}
653
654/*
655 * We received a transmit interrupt without a pending X-char but with
656 * pending characters.
657 * Locking: called with port lock held and IRQs disabled.
658 * Returns:
659 * false if we want to use PIO to transmit
660 * true if we queued a DMA buffer
661 */
662static bool pl011_dma_tx_irq(struct uart_amba_port *uap)
663{
Linus Walleijead76f32011-02-24 13:21:08 +0100664 if (!uap->using_tx_dma)
Russell King68b65f72010-12-22 17:24:39 +0000665 return false;
666
667 /*
668 * If we already have a TX buffer queued, but received a
669 * TX interrupt, it will be because we've just sent an X-char.
670 * Ensure the TX DMA is enabled and the TX IRQ is disabled.
671 */
672 if (uap->dmatx.queued) {
673 uap->dmacr |= UART011_TXDMAE;
Jun Nie7b753f32015-07-31 15:49:16 +0800674 pl011_writew(uap, uap->dmacr, REG_DMACR);
Russell King68b65f72010-12-22 17:24:39 +0000675 uap->im &= ~UART011_TXIM;
Jun Nie7b753f32015-07-31 15:49:16 +0800676 pl011_writew(uap, uap->im, REG_IMSC);
Russell King68b65f72010-12-22 17:24:39 +0000677 return true;
678 }
679
680 /*
681 * We don't have a TX buffer queued, so try to queue one.
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300682 * If we successfully queued a buffer, mask the TX IRQ.
Russell King68b65f72010-12-22 17:24:39 +0000683 */
684 if (pl011_dma_tx_refill(uap) > 0) {
685 uap->im &= ~UART011_TXIM;
Jun Nie7b753f32015-07-31 15:49:16 +0800686 pl011_writew(uap, uap->im, REG_IMSC);
Russell King68b65f72010-12-22 17:24:39 +0000687 return true;
688 }
689 return false;
690}
691
692/*
693 * Stop the DMA transmit (eg, due to received XOFF).
694 * Locking: called with port lock held and IRQs disabled.
695 */
696static inline void pl011_dma_tx_stop(struct uart_amba_port *uap)
697{
698 if (uap->dmatx.queued) {
699 uap->dmacr &= ~UART011_TXDMAE;
Jun Nie7b753f32015-07-31 15:49:16 +0800700 pl011_writew(uap, uap->dmacr, REG_DMACR);
Russell King68b65f72010-12-22 17:24:39 +0000701 }
702}
703
704/*
705 * Try to start a DMA transmit, or in the case of an XON/OFF
706 * character queued for send, try to get that character out ASAP.
707 * Locking: called with port lock held and IRQs disabled.
708 * Returns:
709 * false if we want the TX IRQ to be enabled
710 * true if we have a buffer queued
711 */
712static inline bool pl011_dma_tx_start(struct uart_amba_port *uap)
713{
714 u16 dmacr;
715
Linus Walleijead76f32011-02-24 13:21:08 +0100716 if (!uap->using_tx_dma)
Russell King68b65f72010-12-22 17:24:39 +0000717 return false;
718
719 if (!uap->port.x_char) {
720 /* no X-char, try to push chars out in DMA mode */
721 bool ret = true;
722
723 if (!uap->dmatx.queued) {
724 if (pl011_dma_tx_refill(uap) > 0) {
725 uap->im &= ~UART011_TXIM;
Jun Nie7b753f32015-07-31 15:49:16 +0800726 pl011_writew(uap, uap->im, REG_IMSC);
Dave Martin734745c2015-03-04 12:27:33 +0000727 } else
Russell King68b65f72010-12-22 17:24:39 +0000728 ret = false;
Russell King68b65f72010-12-22 17:24:39 +0000729 } else if (!(uap->dmacr & UART011_TXDMAE)) {
730 uap->dmacr |= UART011_TXDMAE;
Jun Nie7b753f32015-07-31 15:49:16 +0800731 pl011_writew(uap, uap->dmacr, REG_DMACR);
Russell King68b65f72010-12-22 17:24:39 +0000732 }
733 return ret;
734 }
735
736 /*
737 * We have an X-char to send. Disable DMA to prevent it loading
738 * the TX fifo, and then see if we can stuff it into the FIFO.
739 */
740 dmacr = uap->dmacr;
741 uap->dmacr &= ~UART011_TXDMAE;
Jun Nie7b753f32015-07-31 15:49:16 +0800742 pl011_writew(uap, uap->dmacr, REG_DMACR);
Russell King68b65f72010-12-22 17:24:39 +0000743
Jun Nie7b753f32015-07-31 15:49:16 +0800744 if (pl011_readw(uap, REG_FR) & UART01x_FR_TXFF) {
Russell King68b65f72010-12-22 17:24:39 +0000745 /*
746 * No space in the FIFO, so enable the transmit interrupt
747 * so we know when there is space. Note that once we've
748 * loaded the character, we should just re-enable DMA.
749 */
750 return false;
751 }
752
Jun Nie7b753f32015-07-31 15:49:16 +0800753 pl011_writew(uap, uap->port.x_char, REG_DR);
Russell King68b65f72010-12-22 17:24:39 +0000754 uap->port.icount.tx++;
755 uap->port.x_char = 0;
756
757 /* Success - restore the DMA state */
758 uap->dmacr = dmacr;
Jun Nie7b753f32015-07-31 15:49:16 +0800759 pl011_writew(uap, dmacr, REG_DMACR);
Russell King68b65f72010-12-22 17:24:39 +0000760
761 return true;
762}
763
764/*
765 * Flush the transmit buffer.
766 * Locking: called with port lock held and IRQs disabled.
767 */
768static void pl011_dma_flush_buffer(struct uart_port *port)
Fabio Estevamb83286b2013-08-09 17:58:51 -0300769__releases(&uap->port.lock)
770__acquires(&uap->port.lock)
Russell King68b65f72010-12-22 17:24:39 +0000771{
Daniel Thompsona5820c22014-09-03 12:51:55 +0100772 struct uart_amba_port *uap =
773 container_of(port, struct uart_amba_port, port);
Russell King68b65f72010-12-22 17:24:39 +0000774
Linus Walleijead76f32011-02-24 13:21:08 +0100775 if (!uap->using_tx_dma)
Russell King68b65f72010-12-22 17:24:39 +0000776 return;
777
778 /* Avoid deadlock with the DMA engine callback */
779 spin_unlock(&uap->port.lock);
780 dmaengine_terminate_all(uap->dmatx.chan);
781 spin_lock(&uap->port.lock);
782 if (uap->dmatx.queued) {
783 dma_unmap_sg(uap->dmatx.chan->device->dev, &uap->dmatx.sg, 1,
784 DMA_TO_DEVICE);
785 uap->dmatx.queued = false;
786 uap->dmacr &= ~UART011_TXDMAE;
Jun Nie7b753f32015-07-31 15:49:16 +0800787 pl011_writew(uap, uap->dmacr, REG_DMACR);
Russell King68b65f72010-12-22 17:24:39 +0000788 }
789}
790
Linus Walleijead76f32011-02-24 13:21:08 +0100791static void pl011_dma_rx_callback(void *data);
792
793static int pl011_dma_rx_trigger_dma(struct uart_amba_port *uap)
794{
795 struct dma_chan *rxchan = uap->dmarx.chan;
Linus Walleijead76f32011-02-24 13:21:08 +0100796 struct pl011_dmarx_data *dmarx = &uap->dmarx;
797 struct dma_async_tx_descriptor *desc;
798 struct pl011_sgbuf *sgbuf;
799
800 if (!rxchan)
801 return -EIO;
802
803 /* Start the RX DMA job */
804 sgbuf = uap->dmarx.use_buf_b ?
805 &uap->dmarx.sgbuf_b : &uap->dmarx.sgbuf_a;
Alexandre Bounine16052822012-03-08 16:11:18 -0500806 desc = dmaengine_prep_slave_sg(rxchan, &sgbuf->sg, 1,
Vinod Koula485df42011-10-14 10:47:38 +0530807 DMA_DEV_TO_MEM,
Linus Walleijead76f32011-02-24 13:21:08 +0100808 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
809 /*
810 * If the DMA engine is busy and cannot prepare a
811 * channel, no big deal, the driver will fall back
812 * to interrupt mode as a result of this error code.
813 */
814 if (!desc) {
815 uap->dmarx.running = false;
816 dmaengine_terminate_all(rxchan);
817 return -EBUSY;
818 }
819
820 /* Some data to go along to the callback */
821 desc->callback = pl011_dma_rx_callback;
822 desc->callback_param = uap;
823 dmarx->cookie = dmaengine_submit(desc);
824 dma_async_issue_pending(rxchan);
825
826 uap->dmacr |= UART011_RXDMAE;
Jun Nie7b753f32015-07-31 15:49:16 +0800827 pl011_writew(uap, uap->dmacr, REG_DMACR);
Linus Walleijead76f32011-02-24 13:21:08 +0100828 uap->dmarx.running = true;
829
830 uap->im &= ~UART011_RXIM;
Jun Nie7b753f32015-07-31 15:49:16 +0800831 pl011_writew(uap, uap->im, REG_IMSC);
Linus Walleijead76f32011-02-24 13:21:08 +0100832
833 return 0;
834}
835
836/*
837 * This is called when either the DMA job is complete, or
838 * the FIFO timeout interrupt occurred. This must be called
839 * with the port spinlock uap->port.lock held.
840 */
841static void pl011_dma_rx_chars(struct uart_amba_port *uap,
842 u32 pending, bool use_buf_b,
843 bool readfifo)
844{
Jiri Slaby05c7cd32013-01-03 15:53:04 +0100845 struct tty_port *port = &uap->port.state->port;
Linus Walleijead76f32011-02-24 13:21:08 +0100846 struct pl011_sgbuf *sgbuf = use_buf_b ?
847 &uap->dmarx.sgbuf_b : &uap->dmarx.sgbuf_a;
Linus Walleijead76f32011-02-24 13:21:08 +0100848 int dma_count = 0;
849 u32 fifotaken = 0; /* only used for vdbg() */
850
Chanho Mincb06ff12013-03-27 18:38:11 +0900851 struct pl011_dmarx_data *dmarx = &uap->dmarx;
852 int dmataken = 0;
853
854 if (uap->dmarx.poll_rate) {
855 /* The data can be taken by polling */
856 dmataken = sgbuf->sg.length - dmarx->last_residue;
857 /* Recalculate the pending size */
858 if (pending >= dmataken)
859 pending -= dmataken;
860 }
861
862 /* Pick the remain data from the DMA */
Linus Walleijead76f32011-02-24 13:21:08 +0100863 if (pending) {
Linus Walleijead76f32011-02-24 13:21:08 +0100864
865 /*
866 * First take all chars in the DMA pipe, then look in the FIFO.
867 * Note that tty_insert_flip_buf() tries to take as many chars
868 * as it can.
869 */
Chanho Mincb06ff12013-03-27 18:38:11 +0900870 dma_count = tty_insert_flip_string(port, sgbuf->buf + dmataken,
871 pending);
Linus Walleijead76f32011-02-24 13:21:08 +0100872
873 uap->port.icount.rx += dma_count;
874 if (dma_count < pending)
875 dev_warn(uap->port.dev,
876 "couldn't insert all characters (TTY is full?)\n");
877 }
878
Chanho Mincb06ff12013-03-27 18:38:11 +0900879 /* Reset the last_residue for Rx DMA poll */
880 if (uap->dmarx.poll_rate)
881 dmarx->last_residue = sgbuf->sg.length;
882
Linus Walleijead76f32011-02-24 13:21:08 +0100883 /*
884 * Only continue with trying to read the FIFO if all DMA chars have
885 * been taken first.
886 */
887 if (dma_count == pending && readfifo) {
888 /* Clear any error flags */
Jun Nie7b753f32015-07-31 15:49:16 +0800889 pl011_writew(uap,
890 UART011_OEIS | UART011_BEIS | UART011_PEIS
891 | UART011_FEIS, REG_ICR);
Linus Walleijead76f32011-02-24 13:21:08 +0100892
893 /*
894 * If we read all the DMA'd characters, and we had an
Linus Walleij29772c42011-02-24 13:21:36 +0100895 * incomplete buffer, that could be due to an rx error, or
896 * maybe we just timed out. Read any pending chars and check
897 * the error status.
898 *
899 * Error conditions will only occur in the FIFO, these will
900 * trigger an immediate interrupt and stop the DMA job, so we
901 * will always find the error in the FIFO, never in the DMA
902 * buffer.
Linus Walleijead76f32011-02-24 13:21:08 +0100903 */
Linus Walleij29772c42011-02-24 13:21:36 +0100904 fifotaken = pl011_fifo_to_tty(uap);
Linus Walleijead76f32011-02-24 13:21:08 +0100905 }
906
907 spin_unlock(&uap->port.lock);
908 dev_vdbg(uap->port.dev,
909 "Took %d chars from DMA buffer and %d chars from the FIFO\n",
910 dma_count, fifotaken);
Jiri Slaby2e124b42013-01-03 15:53:06 +0100911 tty_flip_buffer_push(port);
Linus Walleijead76f32011-02-24 13:21:08 +0100912 spin_lock(&uap->port.lock);
913}
914
915static void pl011_dma_rx_irq(struct uart_amba_port *uap)
916{
917 struct pl011_dmarx_data *dmarx = &uap->dmarx;
918 struct dma_chan *rxchan = dmarx->chan;
919 struct pl011_sgbuf *sgbuf = dmarx->use_buf_b ?
920 &dmarx->sgbuf_b : &dmarx->sgbuf_a;
921 size_t pending;
922 struct dma_tx_state state;
923 enum dma_status dmastat;
924
925 /*
926 * Pause the transfer so we can trust the current counter,
927 * do this before we pause the PL011 block, else we may
928 * overflow the FIFO.
929 */
930 if (dmaengine_pause(rxchan))
931 dev_err(uap->port.dev, "unable to pause DMA transfer\n");
932 dmastat = rxchan->device->device_tx_status(rxchan,
933 dmarx->cookie, &state);
934 if (dmastat != DMA_PAUSED)
935 dev_err(uap->port.dev, "unable to pause DMA transfer\n");
936
937 /* Disable RX DMA - incoming data will wait in the FIFO */
938 uap->dmacr &= ~UART011_RXDMAE;
Jun Nie7b753f32015-07-31 15:49:16 +0800939 pl011_writew(uap, uap->dmacr, REG_DMACR);
Linus Walleijead76f32011-02-24 13:21:08 +0100940 uap->dmarx.running = false;
941
942 pending = sgbuf->sg.length - state.residue;
943 BUG_ON(pending > PL011_DMA_BUFFER_SIZE);
944 /* Then we terminate the transfer - we now know our residue */
945 dmaengine_terminate_all(rxchan);
946
947 /*
948 * This will take the chars we have so far and insert
949 * into the framework.
950 */
951 pl011_dma_rx_chars(uap, pending, dmarx->use_buf_b, true);
952
953 /* Switch buffer & re-trigger DMA job */
954 dmarx->use_buf_b = !dmarx->use_buf_b;
955 if (pl011_dma_rx_trigger_dma(uap)) {
956 dev_dbg(uap->port.dev, "could not retrigger RX DMA job "
957 "fall back to interrupt mode\n");
958 uap->im |= UART011_RXIM;
Jun Nie7b753f32015-07-31 15:49:16 +0800959 pl011_writew(uap, uap->im, REG_IMSC);
Linus Walleijead76f32011-02-24 13:21:08 +0100960 }
961}
962
963static void pl011_dma_rx_callback(void *data)
964{
965 struct uart_amba_port *uap = data;
966 struct pl011_dmarx_data *dmarx = &uap->dmarx;
Chanho Min6dc01aa2012-02-20 10:24:40 +0900967 struct dma_chan *rxchan = dmarx->chan;
Linus Walleijead76f32011-02-24 13:21:08 +0100968 bool lastbuf = dmarx->use_buf_b;
Chanho Min6dc01aa2012-02-20 10:24:40 +0900969 struct pl011_sgbuf *sgbuf = dmarx->use_buf_b ?
970 &dmarx->sgbuf_b : &dmarx->sgbuf_a;
971 size_t pending;
972 struct dma_tx_state state;
Linus Walleijead76f32011-02-24 13:21:08 +0100973 int ret;
974
975 /*
976 * This completion interrupt occurs typically when the
977 * RX buffer is totally stuffed but no timeout has yet
978 * occurred. When that happens, we just want the RX
979 * routine to flush out the secondary DMA buffer while
980 * we immediately trigger the next DMA job.
981 */
982 spin_lock_irq(&uap->port.lock);
Chanho Min6dc01aa2012-02-20 10:24:40 +0900983 /*
984 * Rx data can be taken by the UART interrupts during
985 * the DMA irq handler. So we check the residue here.
986 */
987 rxchan->device->device_tx_status(rxchan, dmarx->cookie, &state);
988 pending = sgbuf->sg.length - state.residue;
989 BUG_ON(pending > PL011_DMA_BUFFER_SIZE);
990 /* Then we terminate the transfer - we now know our residue */
991 dmaengine_terminate_all(rxchan);
992
Linus Walleijead76f32011-02-24 13:21:08 +0100993 uap->dmarx.running = false;
994 dmarx->use_buf_b = !lastbuf;
995 ret = pl011_dma_rx_trigger_dma(uap);
996
Chanho Min6dc01aa2012-02-20 10:24:40 +0900997 pl011_dma_rx_chars(uap, pending, lastbuf, false);
Linus Walleijead76f32011-02-24 13:21:08 +0100998 spin_unlock_irq(&uap->port.lock);
999 /*
1000 * Do this check after we picked the DMA chars so we don't
1001 * get some IRQ immediately from RX.
1002 */
1003 if (ret) {
1004 dev_dbg(uap->port.dev, "could not retrigger RX DMA job "
1005 "fall back to interrupt mode\n");
1006 uap->im |= UART011_RXIM;
Jun Nie7b753f32015-07-31 15:49:16 +08001007 pl011_writew(uap, uap->im, REG_IMSC);
Linus Walleijead76f32011-02-24 13:21:08 +01001008 }
1009}
1010
1011/*
1012 * Stop accepting received characters, when we're shutting down or
1013 * suspending this port.
1014 * Locking: called with port lock held and IRQs disabled.
1015 */
1016static inline void pl011_dma_rx_stop(struct uart_amba_port *uap)
1017{
1018 /* FIXME. Just disable the DMA enable */
1019 uap->dmacr &= ~UART011_RXDMAE;
Jun Nie7b753f32015-07-31 15:49:16 +08001020 pl011_writew(uap, uap->dmacr, REG_DMACR);
Linus Walleijead76f32011-02-24 13:21:08 +01001021}
Russell King68b65f72010-12-22 17:24:39 +00001022
Chanho Mincb06ff12013-03-27 18:38:11 +09001023/*
1024 * Timer handler for Rx DMA polling.
1025 * Every polling, It checks the residue in the dma buffer and transfer
1026 * data to the tty. Also, last_residue is updated for the next polling.
1027 */
1028static void pl011_dma_rx_poll(unsigned long args)
1029{
1030 struct uart_amba_port *uap = (struct uart_amba_port *)args;
1031 struct tty_port *port = &uap->port.state->port;
1032 struct pl011_dmarx_data *dmarx = &uap->dmarx;
1033 struct dma_chan *rxchan = uap->dmarx.chan;
1034 unsigned long flags = 0;
1035 unsigned int dmataken = 0;
1036 unsigned int size = 0;
1037 struct pl011_sgbuf *sgbuf;
1038 int dma_count;
1039 struct dma_tx_state state;
1040
1041 sgbuf = dmarx->use_buf_b ? &uap->dmarx.sgbuf_b : &uap->dmarx.sgbuf_a;
1042 rxchan->device->device_tx_status(rxchan, dmarx->cookie, &state);
1043 if (likely(state.residue < dmarx->last_residue)) {
1044 dmataken = sgbuf->sg.length - dmarx->last_residue;
1045 size = dmarx->last_residue - state.residue;
1046 dma_count = tty_insert_flip_string(port, sgbuf->buf + dmataken,
1047 size);
1048 if (dma_count == size)
1049 dmarx->last_residue = state.residue;
1050 dmarx->last_jiffies = jiffies;
1051 }
1052 tty_flip_buffer_push(port);
1053
1054 /*
1055 * If no data is received in poll_timeout, the driver will fall back
1056 * to interrupt mode. We will retrigger DMA at the first interrupt.
1057 */
1058 if (jiffies_to_msecs(jiffies - dmarx->last_jiffies)
1059 > uap->dmarx.poll_timeout) {
1060
1061 spin_lock_irqsave(&uap->port.lock, flags);
1062 pl011_dma_rx_stop(uap);
Guennadi Liakhovetskic25a1ad2013-12-10 14:54:47 +01001063 uap->im |= UART011_RXIM;
Jun Nie7b753f32015-07-31 15:49:16 +08001064 pl011_writew(uap, uap->im, REG_IMSC);
Chanho Mincb06ff12013-03-27 18:38:11 +09001065 spin_unlock_irqrestore(&uap->port.lock, flags);
1066
1067 uap->dmarx.running = false;
1068 dmaengine_terminate_all(rxchan);
1069 del_timer(&uap->dmarx.timer);
1070 } else {
1071 mod_timer(&uap->dmarx.timer,
1072 jiffies + msecs_to_jiffies(uap->dmarx.poll_rate));
1073 }
1074}
1075
Russell King68b65f72010-12-22 17:24:39 +00001076static void pl011_dma_startup(struct uart_amba_port *uap)
1077{
Linus Walleijead76f32011-02-24 13:21:08 +01001078 int ret;
1079
Jorge Ramirez-Ortiz1c9be312015-03-06 13:05:40 -05001080 if (!uap->dma_probed)
1081 pl011_dma_probe(uap);
1082
Russell King68b65f72010-12-22 17:24:39 +00001083 if (!uap->dmatx.chan)
1084 return;
1085
Andrew Jackson4c0be452014-11-07 14:14:35 +00001086 uap->dmatx.buf = kmalloc(PL011_DMA_BUFFER_SIZE, GFP_KERNEL | __GFP_DMA);
Russell King68b65f72010-12-22 17:24:39 +00001087 if (!uap->dmatx.buf) {
1088 dev_err(uap->port.dev, "no memory for DMA TX buffer\n");
1089 uap->port.fifosize = uap->fifosize;
1090 return;
1091 }
1092
1093 sg_init_one(&uap->dmatx.sg, uap->dmatx.buf, PL011_DMA_BUFFER_SIZE);
1094
1095 /* The DMA buffer is now the FIFO the TTY subsystem can use */
1096 uap->port.fifosize = PL011_DMA_BUFFER_SIZE;
Linus Walleijead76f32011-02-24 13:21:08 +01001097 uap->using_tx_dma = true;
Russell King68b65f72010-12-22 17:24:39 +00001098
Linus Walleijead76f32011-02-24 13:21:08 +01001099 if (!uap->dmarx.chan)
1100 goto skip_rx;
1101
1102 /* Allocate and map DMA RX buffers */
1103 ret = pl011_sgbuf_init(uap->dmarx.chan, &uap->dmarx.sgbuf_a,
1104 DMA_FROM_DEVICE);
1105 if (ret) {
1106 dev_err(uap->port.dev, "failed to init DMA %s: %d\n",
1107 "RX buffer A", ret);
1108 goto skip_rx;
1109 }
1110
1111 ret = pl011_sgbuf_init(uap->dmarx.chan, &uap->dmarx.sgbuf_b,
1112 DMA_FROM_DEVICE);
1113 if (ret) {
1114 dev_err(uap->port.dev, "failed to init DMA %s: %d\n",
1115 "RX buffer B", ret);
1116 pl011_sgbuf_free(uap->dmarx.chan, &uap->dmarx.sgbuf_a,
1117 DMA_FROM_DEVICE);
1118 goto skip_rx;
1119 }
1120
1121 uap->using_rx_dma = true;
1122
1123skip_rx:
Russell King68b65f72010-12-22 17:24:39 +00001124 /* Turn on DMA error (RX/TX will be enabled on demand) */
1125 uap->dmacr |= UART011_DMAONERR;
Jun Nie7b753f32015-07-31 15:49:16 +08001126 pl011_writew(uap, uap->dmacr, REG_DMACR);
Russell King38d62432010-12-22 17:59:16 +00001127
1128 /*
1129 * ST Micro variants has some specific dma burst threshold
1130 * compensation. Set this to 16 bytes, so burst will only
1131 * be issued above/below 16 bytes.
1132 */
1133 if (uap->vendor->dma_threshold)
Jun Nie7b753f32015-07-31 15:49:16 +08001134 pl011_writew(uap,
1135 ST_UART011_DMAWM_RX_16 | ST_UART011_DMAWM_TX_16,
1136 REG_ST_DMAWM);
Linus Walleijead76f32011-02-24 13:21:08 +01001137
1138 if (uap->using_rx_dma) {
1139 if (pl011_dma_rx_trigger_dma(uap))
1140 dev_dbg(uap->port.dev, "could not trigger initial "
1141 "RX DMA job, fall back to interrupt mode\n");
Chanho Mincb06ff12013-03-27 18:38:11 +09001142 if (uap->dmarx.poll_rate) {
1143 init_timer(&(uap->dmarx.timer));
1144 uap->dmarx.timer.function = pl011_dma_rx_poll;
1145 uap->dmarx.timer.data = (unsigned long)uap;
1146 mod_timer(&uap->dmarx.timer,
1147 jiffies +
1148 msecs_to_jiffies(uap->dmarx.poll_rate));
1149 uap->dmarx.last_residue = PL011_DMA_BUFFER_SIZE;
1150 uap->dmarx.last_jiffies = jiffies;
1151 }
Linus Walleijead76f32011-02-24 13:21:08 +01001152 }
Russell King68b65f72010-12-22 17:24:39 +00001153}
1154
1155static void pl011_dma_shutdown(struct uart_amba_port *uap)
1156{
Linus Walleijead76f32011-02-24 13:21:08 +01001157 if (!(uap->using_tx_dma || uap->using_rx_dma))
Russell King68b65f72010-12-22 17:24:39 +00001158 return;
1159
1160 /* Disable RX and TX DMA */
Jun Nie7b753f32015-07-31 15:49:16 +08001161 while (pl011_readw(uap, REG_FR) & UART01x_FR_BUSY)
Russell King68b65f72010-12-22 17:24:39 +00001162 barrier();
1163
1164 spin_lock_irq(&uap->port.lock);
1165 uap->dmacr &= ~(UART011_DMAONERR | UART011_RXDMAE | UART011_TXDMAE);
Jun Nie7b753f32015-07-31 15:49:16 +08001166 pl011_writew(uap, uap->dmacr, REG_DMACR);
Russell King68b65f72010-12-22 17:24:39 +00001167 spin_unlock_irq(&uap->port.lock);
1168
Linus Walleijead76f32011-02-24 13:21:08 +01001169 if (uap->using_tx_dma) {
1170 /* In theory, this should already be done by pl011_dma_flush_buffer */
1171 dmaengine_terminate_all(uap->dmatx.chan);
1172 if (uap->dmatx.queued) {
1173 dma_unmap_sg(uap->dmatx.chan->device->dev, &uap->dmatx.sg, 1,
1174 DMA_TO_DEVICE);
1175 uap->dmatx.queued = false;
1176 }
1177
1178 kfree(uap->dmatx.buf);
1179 uap->using_tx_dma = false;
Russell King68b65f72010-12-22 17:24:39 +00001180 }
1181
Linus Walleijead76f32011-02-24 13:21:08 +01001182 if (uap->using_rx_dma) {
1183 dmaengine_terminate_all(uap->dmarx.chan);
1184 /* Clean up the RX DMA */
1185 pl011_sgbuf_free(uap->dmarx.chan, &uap->dmarx.sgbuf_a, DMA_FROM_DEVICE);
1186 pl011_sgbuf_free(uap->dmarx.chan, &uap->dmarx.sgbuf_b, DMA_FROM_DEVICE);
Chanho Mincb06ff12013-03-27 18:38:11 +09001187 if (uap->dmarx.poll_rate)
1188 del_timer_sync(&uap->dmarx.timer);
Linus Walleijead76f32011-02-24 13:21:08 +01001189 uap->using_rx_dma = false;
1190 }
Russell King68b65f72010-12-22 17:24:39 +00001191}
1192
Linus Walleijead76f32011-02-24 13:21:08 +01001193static inline bool pl011_dma_rx_available(struct uart_amba_port *uap)
1194{
1195 return uap->using_rx_dma;
1196}
1197
1198static inline bool pl011_dma_rx_running(struct uart_amba_port *uap)
1199{
1200 return uap->using_rx_dma && uap->dmarx.running;
1201}
1202
Russell King68b65f72010-12-22 17:24:39 +00001203#else
1204/* Blank functions if the DMA engine is not available */
Jorge Ramirez-Ortiz1c9be312015-03-06 13:05:40 -05001205static inline void pl011_dma_probe(struct uart_amba_port *uap)
Russell King68b65f72010-12-22 17:24:39 +00001206{
1207}
1208
1209static inline void pl011_dma_remove(struct uart_amba_port *uap)
1210{
1211}
1212
1213static inline void pl011_dma_startup(struct uart_amba_port *uap)
1214{
1215}
1216
1217static inline void pl011_dma_shutdown(struct uart_amba_port *uap)
1218{
1219}
1220
1221static inline bool pl011_dma_tx_irq(struct uart_amba_port *uap)
1222{
1223 return false;
1224}
1225
1226static inline void pl011_dma_tx_stop(struct uart_amba_port *uap)
1227{
1228}
1229
1230static inline bool pl011_dma_tx_start(struct uart_amba_port *uap)
1231{
1232 return false;
1233}
1234
Linus Walleijead76f32011-02-24 13:21:08 +01001235static inline void pl011_dma_rx_irq(struct uart_amba_port *uap)
1236{
1237}
1238
1239static inline void pl011_dma_rx_stop(struct uart_amba_port *uap)
1240{
1241}
1242
1243static inline int pl011_dma_rx_trigger_dma(struct uart_amba_port *uap)
1244{
1245 return -EIO;
1246}
1247
1248static inline bool pl011_dma_rx_available(struct uart_amba_port *uap)
1249{
1250 return false;
1251}
1252
1253static inline bool pl011_dma_rx_running(struct uart_amba_port *uap)
1254{
1255 return false;
1256}
1257
Russell King68b65f72010-12-22 17:24:39 +00001258#define pl011_dma_flush_buffer NULL
1259#endif
1260
Russell Kingb129a8c2005-08-31 10:12:14 +01001261static void pl011_stop_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001263 struct uart_amba_port *uap =
1264 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265
1266 uap->im &= ~UART011_TXIM;
Jun Nie7b753f32015-07-31 15:49:16 +08001267 pl011_writew(uap, uap->im, REG_IMSC);
Russell King68b65f72010-12-22 17:24:39 +00001268 pl011_dma_tx_stop(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269}
1270
Dave Martin1e84d222015-04-27 16:49:05 +01001271static void pl011_tx_chars(struct uart_amba_port *uap, bool from_irq);
Dave Martin734745c2015-03-04 12:27:33 +00001272
1273/* Start TX with programmed I/O only (no DMA) */
1274static void pl011_start_tx_pio(struct uart_amba_port *uap)
1275{
1276 uap->im |= UART011_TXIM;
Jun Nie7b753f32015-07-31 15:49:16 +08001277 pl011_writew(uap, uap->im, REG_IMSC);
Dave Martin1e84d222015-04-27 16:49:05 +01001278 pl011_tx_chars(uap, false);
Dave Martin734745c2015-03-04 12:27:33 +00001279}
1280
Russell Kingb129a8c2005-08-31 10:12:14 +01001281static void pl011_start_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001283 struct uart_amba_port *uap =
1284 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285
Dave Martin734745c2015-03-04 12:27:33 +00001286 if (!pl011_dma_tx_start(uap))
1287 pl011_start_tx_pio(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288}
1289
1290static void pl011_stop_rx(struct uart_port *port)
1291{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001292 struct uart_amba_port *uap =
1293 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294
1295 uap->im &= ~(UART011_RXIM|UART011_RTIM|UART011_FEIM|
1296 UART011_PEIM|UART011_BEIM|UART011_OEIM);
Jun Nie7b753f32015-07-31 15:49:16 +08001297 pl011_writew(uap, uap->im, REG_IMSC);
Linus Walleijead76f32011-02-24 13:21:08 +01001298
1299 pl011_dma_rx_stop(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300}
1301
1302static void pl011_enable_ms(struct uart_port *port)
1303{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001304 struct uart_amba_port *uap =
1305 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306
1307 uap->im |= UART011_RIMIM|UART011_CTSMIM|UART011_DCDMIM|UART011_DSRMIM;
Jun Nie7b753f32015-07-31 15:49:16 +08001308 pl011_writew(uap, uap->im, REG_IMSC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309}
1310
David Howells7d12e782006-10-05 14:55:46 +01001311static void pl011_rx_chars(struct uart_amba_port *uap)
Fabio Estevamb83286b2013-08-09 17:58:51 -03001312__releases(&uap->port.lock)
1313__acquires(&uap->port.lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314{
Linus Walleij29772c42011-02-24 13:21:36 +01001315 pl011_fifo_to_tty(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316
Thomas Gleixner2389b272007-05-29 21:53:50 +01001317 spin_unlock(&uap->port.lock);
Jiri Slaby2e124b42013-01-03 15:53:06 +01001318 tty_flip_buffer_push(&uap->port.state->port);
Linus Walleijead76f32011-02-24 13:21:08 +01001319 /*
1320 * If we were temporarily out of DMA mode for a while,
1321 * attempt to switch back to DMA mode again.
1322 */
1323 if (pl011_dma_rx_available(uap)) {
1324 if (pl011_dma_rx_trigger_dma(uap)) {
1325 dev_dbg(uap->port.dev, "could not trigger RX DMA job "
1326 "fall back to interrupt mode again\n");
1327 uap->im |= UART011_RXIM;
Jun Nie7b753f32015-07-31 15:49:16 +08001328 pl011_writew(uap, uap->im, REG_IMSC);
Chanho Mincb06ff12013-03-27 18:38:11 +09001329 } else {
Chanho Min89fa28d2013-04-03 11:10:37 +09001330#ifdef CONFIG_DMA_ENGINE
Chanho Mincb06ff12013-03-27 18:38:11 +09001331 /* Start Rx DMA poll */
1332 if (uap->dmarx.poll_rate) {
1333 uap->dmarx.last_jiffies = jiffies;
1334 uap->dmarx.last_residue = PL011_DMA_BUFFER_SIZE;
1335 mod_timer(&uap->dmarx.timer,
1336 jiffies +
1337 msecs_to_jiffies(uap->dmarx.poll_rate));
1338 }
Chanho Min89fa28d2013-04-03 11:10:37 +09001339#endif
Chanho Mincb06ff12013-03-27 18:38:11 +09001340 }
Linus Walleijead76f32011-02-24 13:21:08 +01001341 }
Thomas Gleixner2389b272007-05-29 21:53:50 +01001342 spin_lock(&uap->port.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343}
1344
Dave Martin1e84d222015-04-27 16:49:05 +01001345static bool pl011_tx_char(struct uart_amba_port *uap, unsigned char c,
1346 bool from_irq)
Dave Martin734745c2015-03-04 12:27:33 +00001347{
Dave Martin1e84d222015-04-27 16:49:05 +01001348 if (unlikely(!from_irq) &&
Jun Nie7b753f32015-07-31 15:49:16 +08001349 pl011_readw(uap, REG_FR) & UART01x_FR_TXFF)
Dave Martin1e84d222015-04-27 16:49:05 +01001350 return false; /* unable to transmit character */
1351
Jun Nie7b753f32015-07-31 15:49:16 +08001352 pl011_writew(uap, c, REG_DR);
Dave Martin734745c2015-03-04 12:27:33 +00001353 uap->port.icount.tx++;
1354
Dave Martin1e84d222015-04-27 16:49:05 +01001355 return true;
Dave Martin734745c2015-03-04 12:27:33 +00001356}
1357
Dave Martin1e84d222015-04-27 16:49:05 +01001358static void pl011_tx_chars(struct uart_amba_port *uap, bool from_irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359{
Alan Coxebd2c8f2009-09-19 13:13:28 -07001360 struct circ_buf *xmit = &uap->port.state->xmit;
Dave Martin1e84d222015-04-27 16:49:05 +01001361 int count = uap->fifosize >> 1;
Dave Martin734745c2015-03-04 12:27:33 +00001362
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 if (uap->port.x_char) {
Dave Martin1e84d222015-04-27 16:49:05 +01001364 if (!pl011_tx_char(uap, uap->port.x_char, from_irq))
1365 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 uap->port.x_char = 0;
Dave Martin734745c2015-03-04 12:27:33 +00001367 --count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 }
1369 if (uart_circ_empty(xmit) || uart_tx_stopped(&uap->port)) {
Russell Kingb129a8c2005-08-31 10:12:14 +01001370 pl011_stop_tx(&uap->port);
Dave Martin1e84d222015-04-27 16:49:05 +01001371 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 }
1373
Russell King68b65f72010-12-22 17:24:39 +00001374 /* If we are using DMA mode, try to send some characters. */
1375 if (pl011_dma_tx_irq(uap))
Dave Martin1e84d222015-04-27 16:49:05 +01001376 return;
Russell King68b65f72010-12-22 17:24:39 +00001377
Dave Martin1e84d222015-04-27 16:49:05 +01001378 do {
1379 if (likely(from_irq) && count-- == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 break;
Dave Martin1e84d222015-04-27 16:49:05 +01001381
1382 if (!pl011_tx_char(uap, xmit->buf[xmit->tail], from_irq))
1383 break;
1384
1385 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
1386 } while (!uart_circ_empty(xmit));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387
1388 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
1389 uart_write_wakeup(&uap->port);
1390
Dave Martin1e84d222015-04-27 16:49:05 +01001391 if (uart_circ_empty(xmit))
Russell Kingb129a8c2005-08-31 10:12:14 +01001392 pl011_stop_tx(&uap->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393}
1394
1395static void pl011_modem_status(struct uart_amba_port *uap)
1396{
1397 unsigned int status, delta;
1398
Jun Nie7b753f32015-07-31 15:49:16 +08001399 status = pl011_readw(uap, REG_FR) & UART01x_FR_MODEM_ANY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400
1401 delta = status ^ uap->old_status;
1402 uap->old_status = status;
1403
1404 if (!delta)
1405 return;
1406
1407 if (delta & UART01x_FR_DCD)
1408 uart_handle_dcd_change(&uap->port, status & UART01x_FR_DCD);
1409
1410 if (delta & UART01x_FR_DSR)
1411 uap->port.icount.dsr++;
1412
1413 if (delta & UART01x_FR_CTS)
1414 uart_handle_cts_change(&uap->port, status & UART01x_FR_CTS);
1415
Alan Coxbdc04e32009-09-19 13:13:31 -07001416 wake_up_interruptible(&uap->port.state->port.delta_msr_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417}
1418
Andre Przywara9c4ef4b2015-05-21 17:26:20 +01001419static void check_apply_cts_event_workaround(struct uart_amba_port *uap)
1420{
1421 unsigned int dummy_read;
1422
1423 if (!uap->vendor->cts_event_workaround)
1424 return;
1425
1426 /* workaround to make sure that all bits are unlocked.. */
Jun Nie7b753f32015-07-31 15:49:16 +08001427 pl011_writew(uap, 0x00, REG_ICR);
Andre Przywara9c4ef4b2015-05-21 17:26:20 +01001428
1429 /*
1430 * WA: introduce 26ns(1 uart clk) delay before W1C;
1431 * single apb access will incur 2 pclk(133.12Mhz) delay,
1432 * so add 2 dummy reads
1433 */
Jun Nie7b753f32015-07-31 15:49:16 +08001434 dummy_read = pl011_readw(uap, REG_ICR);
1435 dummy_read = pl011_readw(uap, REG_ICR);
Andre Przywara9c4ef4b2015-05-21 17:26:20 +01001436}
1437
David Howells7d12e782006-10-05 14:55:46 +01001438static irqreturn_t pl011_int(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439{
1440 struct uart_amba_port *uap = dev_id;
Russell King963cc982010-12-22 17:16:09 +00001441 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 unsigned int status, pass_counter = AMBA_ISR_PASS_LIMIT;
Andre Przywara075167e2015-05-21 17:26:19 +01001443 u16 imsc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 int handled = 0;
1445
Russell King963cc982010-12-22 17:16:09 +00001446 spin_lock_irqsave(&uap->port.lock, flags);
Jun Nie7b753f32015-07-31 15:49:16 +08001447 imsc = pl011_readw(uap, REG_IMSC);
1448 status = pl011_readw(uap, REG_RIS) & imsc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 if (status) {
1450 do {
Andre Przywara9c4ef4b2015-05-21 17:26:20 +01001451 check_apply_cts_event_workaround(uap);
Jun Nie7b753f32015-07-31 15:49:16 +08001452 pl011_writew(uap, status & ~(UART011_TXIS|UART011_RTIS|
1453 UART011_RXIS), REG_ICR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454
Linus Walleijead76f32011-02-24 13:21:08 +01001455 if (status & (UART011_RTIS|UART011_RXIS)) {
1456 if (pl011_dma_rx_running(uap))
1457 pl011_dma_rx_irq(uap);
1458 else
1459 pl011_rx_chars(uap);
1460 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 if (status & (UART011_DSRMIS|UART011_DCDMIS|
1462 UART011_CTSMIS|UART011_RIMIS))
1463 pl011_modem_status(uap);
Dave Martin1e84d222015-04-27 16:49:05 +01001464 if (status & UART011_TXIS)
1465 pl011_tx_chars(uap, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466
Rajanikanth H.V4fd06902012-03-26 11:17:02 +02001467 if (pass_counter-- == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 break;
1469
Jun Nie7b753f32015-07-31 15:49:16 +08001470 status = pl011_readw(uap, REG_RIS) & imsc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 } while (status != 0);
1472 handled = 1;
1473 }
1474
Russell King963cc982010-12-22 17:16:09 +00001475 spin_unlock_irqrestore(&uap->port.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476
1477 return IRQ_RETVAL(handled);
1478}
1479
Linus Walleije643f872012-06-17 15:44:19 +02001480static unsigned int pl011_tx_empty(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001482 struct uart_amba_port *uap =
1483 container_of(port, struct uart_amba_port, port);
Jun Nie7b753f32015-07-31 15:49:16 +08001484 unsigned int status = pl011_readw(uap, REG_FR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 return status & (UART01x_FR_BUSY|UART01x_FR_TXFF) ? 0 : TIOCSER_TEMT;
1486}
1487
Linus Walleije643f872012-06-17 15:44:19 +02001488static unsigned int pl011_get_mctrl(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001490 struct uart_amba_port *uap =
1491 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 unsigned int result = 0;
Jun Nie7b753f32015-07-31 15:49:16 +08001493 unsigned int status = pl011_readw(uap, REG_FR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494
Jiri Slaby5159f402007-10-18 23:40:31 -07001495#define TIOCMBIT(uartbit, tiocmbit) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 if (status & uartbit) \
1497 result |= tiocmbit
1498
Jiri Slaby5159f402007-10-18 23:40:31 -07001499 TIOCMBIT(UART01x_FR_DCD, TIOCM_CAR);
1500 TIOCMBIT(UART01x_FR_DSR, TIOCM_DSR);
1501 TIOCMBIT(UART01x_FR_CTS, TIOCM_CTS);
1502 TIOCMBIT(UART011_FR_RI, TIOCM_RNG);
1503#undef TIOCMBIT
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 return result;
1505}
1506
1507static void pl011_set_mctrl(struct uart_port *port, unsigned int mctrl)
1508{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001509 struct uart_amba_port *uap =
1510 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 unsigned int cr;
1512
Jun Nie7b753f32015-07-31 15:49:16 +08001513 cr = pl011_readw(uap, REG_CR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514
Jiri Slaby5159f402007-10-18 23:40:31 -07001515#define TIOCMBIT(tiocmbit, uartbit) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 if (mctrl & tiocmbit) \
1517 cr |= uartbit; \
1518 else \
1519 cr &= ~uartbit
1520
Jiri Slaby5159f402007-10-18 23:40:31 -07001521 TIOCMBIT(TIOCM_RTS, UART011_CR_RTS);
1522 TIOCMBIT(TIOCM_DTR, UART011_CR_DTR);
1523 TIOCMBIT(TIOCM_OUT1, UART011_CR_OUT1);
1524 TIOCMBIT(TIOCM_OUT2, UART011_CR_OUT2);
1525 TIOCMBIT(TIOCM_LOOP, UART011_CR_LBE);
Rabin Vincent3b438162010-02-12 06:43:11 +01001526
1527 if (uap->autorts) {
1528 /* We need to disable auto-RTS if we want to turn RTS off */
1529 TIOCMBIT(TIOCM_RTS, UART011_CR_RTSEN);
1530 }
Jiri Slaby5159f402007-10-18 23:40:31 -07001531#undef TIOCMBIT
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532
Jun Nie7b753f32015-07-31 15:49:16 +08001533 pl011_writew(uap, cr, REG_CR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534}
1535
1536static void pl011_break_ctl(struct uart_port *port, int break_state)
1537{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001538 struct uart_amba_port *uap =
1539 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 unsigned long flags;
1541 unsigned int lcr_h;
1542
1543 spin_lock_irqsave(&uap->port.lock, flags);
Jun Nie7b753f32015-07-31 15:49:16 +08001544 lcr_h = pl011_readw(uap, uap->lcrh_tx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 if (break_state == -1)
1546 lcr_h |= UART01x_LCRH_BRK;
1547 else
1548 lcr_h &= ~UART01x_LCRH_BRK;
Jun Nie7b753f32015-07-31 15:49:16 +08001549 pl011_writew(uap, lcr_h, uap->lcrh_tx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 spin_unlock_irqrestore(&uap->port.lock, flags);
1551}
1552
Jason Wessel84b5ae12008-02-20 13:33:39 -06001553#ifdef CONFIG_CONSOLE_POLL
Anton Vorontsov5c8124a2012-09-24 14:27:55 -07001554
1555static void pl011_quiesce_irqs(struct uart_port *port)
1556{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001557 struct uart_amba_port *uap =
1558 container_of(port, struct uart_amba_port, port);
Anton Vorontsov5c8124a2012-09-24 14:27:55 -07001559
Jun Nie7b753f32015-07-31 15:49:16 +08001560 pl011_writew(uap, pl011_readw(uap, REG_MIS), REG_ICR);
Anton Vorontsov5c8124a2012-09-24 14:27:55 -07001561 /*
1562 * There is no way to clear TXIM as this is "ready to transmit IRQ", so
1563 * we simply mask it. start_tx() will unmask it.
1564 *
1565 * Note we can race with start_tx(), and if the race happens, the
1566 * polling user might get another interrupt just after we clear it.
1567 * But it should be OK and can happen even w/o the race, e.g.
1568 * controller immediately got some new data and raised the IRQ.
1569 *
1570 * And whoever uses polling routines assumes that it manages the device
1571 * (including tx queue), so we're also fine with start_tx()'s caller
1572 * side.
1573 */
Jun Nie7b753f32015-07-31 15:49:16 +08001574 pl011_writew(uap, pl011_readw(uap, REG_IMSC) & ~UART011_TXIM, REG_IMSC);
Anton Vorontsov5c8124a2012-09-24 14:27:55 -07001575}
1576
Linus Walleije643f872012-06-17 15:44:19 +02001577static int pl011_get_poll_char(struct uart_port *port)
Jason Wessel84b5ae12008-02-20 13:33:39 -06001578{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001579 struct uart_amba_port *uap =
1580 container_of(port, struct uart_amba_port, port);
Jason Wessel84b5ae12008-02-20 13:33:39 -06001581 unsigned int status;
1582
Anton Vorontsov5c8124a2012-09-24 14:27:55 -07001583 /*
1584 * The caller might need IRQs lowered, e.g. if used with KDB NMI
1585 * debugger.
1586 */
1587 pl011_quiesce_irqs(port);
1588
Jun Nie7b753f32015-07-31 15:49:16 +08001589 status = pl011_readw(uap, REG_FR);
Jason Wesself5316b42010-05-20 21:04:22 -05001590 if (status & UART01x_FR_RXFE)
1591 return NO_POLL_CHAR;
Jason Wessel84b5ae12008-02-20 13:33:39 -06001592
Jun Nie7b753f32015-07-31 15:49:16 +08001593 return pl011_readw(uap, REG_DR);
Jason Wessel84b5ae12008-02-20 13:33:39 -06001594}
1595
Linus Walleije643f872012-06-17 15:44:19 +02001596static void pl011_put_poll_char(struct uart_port *port,
Jason Wessel84b5ae12008-02-20 13:33:39 -06001597 unsigned char ch)
1598{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001599 struct uart_amba_port *uap =
1600 container_of(port, struct uart_amba_port, port);
Jason Wessel84b5ae12008-02-20 13:33:39 -06001601
Jun Nie7b753f32015-07-31 15:49:16 +08001602 while (pl011_readw(uap, REG_FR) & UART01x_FR_TXFF)
Jason Wessel84b5ae12008-02-20 13:33:39 -06001603 barrier();
1604
Jun Nie7b753f32015-07-31 15:49:16 +08001605 pl011_writew(uap, ch, REG_DR);
Jason Wessel84b5ae12008-02-20 13:33:39 -06001606}
1607
1608#endif /* CONFIG_CONSOLE_POLL */
1609
Anton Vorontsovb3564c22012-09-24 14:27:54 -07001610static int pl011_hwinit(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001612 struct uart_amba_port *uap =
1613 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 int retval;
1615
Linus Walleij78d80c52012-05-23 21:18:46 +02001616 /* Optionaly enable pins to be muxed in and configured */
Linus Walleij2b996fc2013-06-05 15:36:42 +02001617 pinctrl_pm_select_default_state(port->dev);
Linus Walleij78d80c52012-05-23 21:18:46 +02001618
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 /*
1620 * Try to enable the clock producer.
1621 */
Julia Lawall1c4c4392012-08-26 18:01:01 +02001622 retval = clk_prepare_enable(uap->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 if (retval)
Tushar Behera7f6d9422014-06-26 15:35:35 +05301624 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625
1626 uap->port.uartclk = clk_get_rate(uap->clk);
1627
Linus Walleij9b96fba2012-03-13 13:27:23 +01001628 /* Clear pending error and receive interrupts */
Jun Nie7b753f32015-07-31 15:49:16 +08001629 pl011_writew(uap, UART011_OEIS | UART011_BEIS | UART011_PEIS |
1630 UART011_FEIS | UART011_RTIS | UART011_RXIS, REG_ICR);
Linus Walleij9b96fba2012-03-13 13:27:23 +01001631
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 /*
Anton Vorontsovb3564c22012-09-24 14:27:54 -07001633 * Save interrupts enable mask, and enable RX interrupts in case if
1634 * the interrupt is used for NMI entry.
1635 */
Jun Nie7b753f32015-07-31 15:49:16 +08001636 uap->im = pl011_readw(uap, REG_IMSC);
1637 pl011_writew(uap, UART011_RTIM | UART011_RXIM, REG_IMSC);
Anton Vorontsovb3564c22012-09-24 14:27:54 -07001638
Jingoo Han574de552013-07-30 17:06:57 +09001639 if (dev_get_platdata(uap->port.dev)) {
Anton Vorontsovb3564c22012-09-24 14:27:54 -07001640 struct amba_pl011_data *plat;
1641
Jingoo Han574de552013-07-30 17:06:57 +09001642 plat = dev_get_platdata(uap->port.dev);
Anton Vorontsovb3564c22012-09-24 14:27:54 -07001643 if (plat->init)
1644 plat->init();
1645 }
1646 return 0;
Anton Vorontsovb3564c22012-09-24 14:27:54 -07001647}
1648
Jon Medhurstb60f2f62013-12-10 10:18:59 +00001649static void pl011_write_lcr_h(struct uart_amba_port *uap, unsigned int lcr_h)
1650{
Jun Nie7b753f32015-07-31 15:49:16 +08001651 pl011_writew(uap, lcr_h, uap->lcrh_rx);
Jon Medhurstb60f2f62013-12-10 10:18:59 +00001652 if (uap->lcrh_rx != uap->lcrh_tx) {
1653 int i;
1654 /*
1655 * Wait 10 PCLKs before writing LCRH_TX register,
1656 * to get this delay write read only register 10 times
1657 */
1658 for (i = 0; i < 10; ++i)
Jun Nie7b753f32015-07-31 15:49:16 +08001659 pl011_writew(uap, 0xff, REG_MIS);
1660 pl011_writew(uap, lcr_h, uap->lcrh_tx);
Jon Medhurstb60f2f62013-12-10 10:18:59 +00001661 }
1662}
1663
Andre Przywara867b8e82015-05-21 17:26:15 +01001664static int pl011_allocate_irq(struct uart_amba_port *uap)
1665{
Jun Nie7b753f32015-07-31 15:49:16 +08001666 pl011_writew(uap, uap->im, REG_IMSC);
Andre Przywara867b8e82015-05-21 17:26:15 +01001667
1668 return request_irq(uap->port.irq, pl011_int, 0, "uart-pl011", uap);
1669}
1670
1671/*
1672 * Enable interrupts, only timeouts when using DMA
1673 * if initial RX DMA job failed, start in interrupt mode
1674 * as well.
1675 */
1676static void pl011_enable_interrupts(struct uart_amba_port *uap)
1677{
1678 spin_lock_irq(&uap->port.lock);
1679
1680 /* Clear out any spuriously appearing RX interrupts */
Jun Nie7b753f32015-07-31 15:49:16 +08001681 pl011_writew(uap, UART011_RTIS | UART011_RXIS, REG_ICR);
Andre Przywara867b8e82015-05-21 17:26:15 +01001682 uap->im = UART011_RTIM;
1683 if (!pl011_dma_rx_running(uap))
1684 uap->im |= UART011_RXIM;
Jun Nie7b753f32015-07-31 15:49:16 +08001685 pl011_writew(uap, uap->im, REG_IMSC);
Andre Przywara867b8e82015-05-21 17:26:15 +01001686 spin_unlock_irq(&uap->port.lock);
1687}
1688
Anton Vorontsovb3564c22012-09-24 14:27:54 -07001689static int pl011_startup(struct uart_port *port)
1690{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001691 struct uart_amba_port *uap =
1692 container_of(port, struct uart_amba_port, port);
Dave Martin734745c2015-03-04 12:27:33 +00001693 unsigned int cr;
Anton Vorontsovb3564c22012-09-24 14:27:54 -07001694 int retval;
1695
1696 retval = pl011_hwinit(port);
1697 if (retval)
1698 goto clk_dis;
1699
Andre Przywara867b8e82015-05-21 17:26:15 +01001700 retval = pl011_allocate_irq(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 if (retval)
1702 goto clk_dis;
1703
Jun Nie7b753f32015-07-31 15:49:16 +08001704 pl011_writew(uap, uap->vendor->ifls, REG_IFLS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705
Jon Medhurstfe433902013-12-10 10:18:58 +00001706 spin_lock_irq(&uap->port.lock);
1707
Shreshtha Kumar Sahud8d8ffa2012-01-18 15:53:59 +05301708 /* restore RTS and DTR */
1709 cr = uap->old_cr & (UART011_CR_RTS | UART011_CR_DTR);
1710 cr |= UART01x_CR_UARTEN | UART011_CR_RXE | UART011_CR_TXE;
Jun Nie7b753f32015-07-31 15:49:16 +08001711 pl011_writew(uap, cr, REG_CR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712
Jon Medhurstfe433902013-12-10 10:18:58 +00001713 spin_unlock_irq(&uap->port.lock);
1714
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 /*
1716 * initialise the old status of the modem signals
1717 */
Jun Nie7b753f32015-07-31 15:49:16 +08001718 uap->old_status = pl011_readw(uap, REG_FR) &
Jun Nie534e14e2015-07-31 15:49:15 +08001719 UART01x_FR_MODEM_ANY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720
Russell King68b65f72010-12-22 17:24:39 +00001721 /* Startup DMA */
1722 pl011_dma_startup(uap);
1723
Andre Przywara867b8e82015-05-21 17:26:15 +01001724 pl011_enable_interrupts(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725
1726 return 0;
1727
1728 clk_dis:
Julia Lawall1c4c4392012-08-26 18:01:01 +02001729 clk_disable_unprepare(uap->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 return retval;
1731}
1732
Andre Przywara0dd1e242015-05-21 17:26:23 +01001733static int sbsa_uart_startup(struct uart_port *port)
1734{
1735 struct uart_amba_port *uap =
1736 container_of(port, struct uart_amba_port, port);
1737 int retval;
1738
1739 retval = pl011_hwinit(port);
1740 if (retval)
1741 return retval;
1742
1743 retval = pl011_allocate_irq(uap);
1744 if (retval)
1745 return retval;
1746
1747 /* The SBSA UART does not support any modem status lines. */
1748 uap->old_status = 0;
1749
1750 pl011_enable_interrupts(uap);
1751
1752 return 0;
1753}
1754
Linus Walleijec489aa2010-06-02 08:13:52 +01001755static void pl011_shutdown_channel(struct uart_amba_port *uap,
1756 unsigned int lcrh)
1757{
Jun Nie7b753f32015-07-31 15:49:16 +08001758 unsigned long val;
Linus Walleijec489aa2010-06-02 08:13:52 +01001759
Jun Nie7b753f32015-07-31 15:49:16 +08001760 val = pl011_readw(uap, lcrh);
1761 val &= ~(UART01x_LCRH_BRK | UART01x_LCRH_FEN);
1762 pl011_writew(uap, val, lcrh);
Linus Walleijec489aa2010-06-02 08:13:52 +01001763}
1764
Andre Przywara95166a32015-05-21 17:26:16 +01001765/*
1766 * disable the port. It should not disable RTS and DTR.
1767 * Also RTS and DTR state should be preserved to restore
1768 * it during startup().
1769 */
1770static void pl011_disable_uart(struct uart_amba_port *uap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771{
Shreshtha Kumar Sahud8d8ffa2012-01-18 15:53:59 +05301772 unsigned int cr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773
Rabin Vincent3b438162010-02-12 06:43:11 +01001774 uap->autorts = false;
Jon Medhurstfe433902013-12-10 10:18:58 +00001775 spin_lock_irq(&uap->port.lock);
Jun Nie7b753f32015-07-31 15:49:16 +08001776 cr = pl011_readw(uap, REG_CR);
Shreshtha Kumar Sahud8d8ffa2012-01-18 15:53:59 +05301777 uap->old_cr = cr;
1778 cr &= UART011_CR_RTS | UART011_CR_DTR;
1779 cr |= UART01x_CR_UARTEN | UART011_CR_TXE;
Jun Nie7b753f32015-07-31 15:49:16 +08001780 pl011_writew(uap, cr, REG_CR);
Jon Medhurstfe433902013-12-10 10:18:58 +00001781 spin_unlock_irq(&uap->port.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782
1783 /*
1784 * disable break condition and fifos
1785 */
Linus Walleijec489aa2010-06-02 08:13:52 +01001786 pl011_shutdown_channel(uap, uap->lcrh_rx);
1787 if (uap->lcrh_rx != uap->lcrh_tx)
1788 pl011_shutdown_channel(uap, uap->lcrh_tx);
Andre Przywara95166a32015-05-21 17:26:16 +01001789}
1790
1791static void pl011_disable_interrupts(struct uart_amba_port *uap)
1792{
1793 spin_lock_irq(&uap->port.lock);
1794
1795 /* mask all interrupts and clear all pending ones */
1796 uap->im = 0;
Jun Nie7b753f32015-07-31 15:49:16 +08001797 pl011_writew(uap, uap->im, REG_IMSC);
1798 pl011_writew(0xffff, REG_ICR);
Andre Przywara95166a32015-05-21 17:26:16 +01001799
1800 spin_unlock_irq(&uap->port.lock);
1801}
1802
1803static void pl011_shutdown(struct uart_port *port)
1804{
1805 struct uart_amba_port *uap =
1806 container_of(port, struct uart_amba_port, port);
1807
1808 pl011_disable_interrupts(uap);
1809
1810 pl011_dma_shutdown(uap);
1811
1812 free_irq(uap->port.irq, uap);
1813
1814 pl011_disable_uart(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815
1816 /*
1817 * Shut down the clock producer
1818 */
Julia Lawall1c4c4392012-08-26 18:01:01 +02001819 clk_disable_unprepare(uap->clk);
Linus Walleij78d80c52012-05-23 21:18:46 +02001820 /* Optionally let pins go into sleep states */
Linus Walleij2b996fc2013-06-05 15:36:42 +02001821 pinctrl_pm_select_sleep_state(port->dev);
Shreshtha Kumar Sahuc16d51a2011-06-13 10:11:33 +02001822
Jingoo Han574de552013-07-30 17:06:57 +09001823 if (dev_get_platdata(uap->port.dev)) {
Shreshtha Kumar Sahuc16d51a2011-06-13 10:11:33 +02001824 struct amba_pl011_data *plat;
1825
Jingoo Han574de552013-07-30 17:06:57 +09001826 plat = dev_get_platdata(uap->port.dev);
Shreshtha Kumar Sahuc16d51a2011-06-13 10:11:33 +02001827 if (plat->exit)
1828 plat->exit();
1829 }
1830
Peter Hurley36f339d2014-11-06 09:06:12 -05001831 if (uap->port.ops->flush_buffer)
1832 uap->port.ops->flush_buffer(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833}
1834
Andre Przywara0dd1e242015-05-21 17:26:23 +01001835static void sbsa_uart_shutdown(struct uart_port *port)
1836{
1837 struct uart_amba_port *uap =
1838 container_of(port, struct uart_amba_port, port);
1839
1840 pl011_disable_interrupts(uap);
1841
1842 free_irq(uap->port.irq, uap);
1843
1844 if (uap->port.ops->flush_buffer)
1845 uap->port.ops->flush_buffer(port);
1846}
1847
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848static void
Andre Przywaraef5a9352015-05-21 17:26:17 +01001849pl011_setup_status_masks(struct uart_port *port, struct ktermios *termios)
1850{
1851 port->read_status_mask = UART011_DR_OE | 255;
1852 if (termios->c_iflag & INPCK)
1853 port->read_status_mask |= UART011_DR_FE | UART011_DR_PE;
1854 if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
1855 port->read_status_mask |= UART011_DR_BE;
1856
1857 /*
1858 * Characters to ignore
1859 */
1860 port->ignore_status_mask = 0;
1861 if (termios->c_iflag & IGNPAR)
1862 port->ignore_status_mask |= UART011_DR_FE | UART011_DR_PE;
1863 if (termios->c_iflag & IGNBRK) {
1864 port->ignore_status_mask |= UART011_DR_BE;
1865 /*
1866 * If we're ignoring parity and break indicators,
1867 * ignore overruns too (for real raw support).
1868 */
1869 if (termios->c_iflag & IGNPAR)
1870 port->ignore_status_mask |= UART011_DR_OE;
1871 }
1872
1873 /*
1874 * Ignore all characters if CREAD is not set.
1875 */
1876 if ((termios->c_cflag & CREAD) == 0)
1877 port->ignore_status_mask |= UART_DUMMY_DR_RX;
1878}
1879
1880static void
Alan Cox606d0992006-12-08 02:38:45 -08001881pl011_set_termios(struct uart_port *port, struct ktermios *termios,
1882 struct ktermios *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001884 struct uart_amba_port *uap =
1885 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 unsigned int lcr_h, old_cr;
1887 unsigned long flags;
Russell Kingc19f12b2010-12-22 17:48:26 +00001888 unsigned int baud, quot, clkdiv;
1889
1890 if (uap->vendor->oversampling)
1891 clkdiv = 8;
1892 else
1893 clkdiv = 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894
1895 /*
1896 * Ask the core to calculate the divisor for us.
1897 */
Linus Walleijac3e3fb2010-06-02 20:40:22 +01001898 baud = uart_get_baud_rate(port, termios, old, 0,
Russell Kingc19f12b2010-12-22 17:48:26 +00001899 port->uartclk / clkdiv);
Chanho Min89fa28d2013-04-03 11:10:37 +09001900#ifdef CONFIG_DMA_ENGINE
Chanho Mincb06ff12013-03-27 18:38:11 +09001901 /*
1902 * Adjust RX DMA polling rate with baud rate if not specified.
1903 */
1904 if (uap->dmarx.auto_poll_rate)
1905 uap->dmarx.poll_rate = DIV_ROUND_UP(10000000, baud);
Chanho Min89fa28d2013-04-03 11:10:37 +09001906#endif
Linus Walleijac3e3fb2010-06-02 20:40:22 +01001907
1908 if (baud > port->uartclk/16)
1909 quot = DIV_ROUND_CLOSEST(port->uartclk * 8, baud);
1910 else
1911 quot = DIV_ROUND_CLOSEST(port->uartclk * 4, baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912
1913 switch (termios->c_cflag & CSIZE) {
1914 case CS5:
1915 lcr_h = UART01x_LCRH_WLEN_5;
1916 break;
1917 case CS6:
1918 lcr_h = UART01x_LCRH_WLEN_6;
1919 break;
1920 case CS7:
1921 lcr_h = UART01x_LCRH_WLEN_7;
1922 break;
1923 default: // CS8
1924 lcr_h = UART01x_LCRH_WLEN_8;
1925 break;
1926 }
1927 if (termios->c_cflag & CSTOPB)
1928 lcr_h |= UART01x_LCRH_STP2;
1929 if (termios->c_cflag & PARENB) {
1930 lcr_h |= UART01x_LCRH_PEN;
1931 if (!(termios->c_cflag & PARODD))
1932 lcr_h |= UART01x_LCRH_EPS;
1933 }
Russell Kingffca2b12010-12-22 17:13:05 +00001934 if (uap->fifosize > 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 lcr_h |= UART01x_LCRH_FEN;
1936
1937 spin_lock_irqsave(&port->lock, flags);
1938
1939 /*
1940 * Update the per-port timeout.
1941 */
1942 uart_update_timeout(port, termios->c_cflag, baud);
1943
Andre Przywaraef5a9352015-05-21 17:26:17 +01001944 pl011_setup_status_masks(port, termios);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945
1946 if (UART_ENABLE_MS(port, termios->c_cflag))
1947 pl011_enable_ms(port);
1948
1949 /* first, disable everything */
Jun Nie7b753f32015-07-31 15:49:16 +08001950 old_cr = pl011_readw(uap, REG_CR);
1951 pl011_writew(uap, 0, REG_CR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952
Rabin Vincent3b438162010-02-12 06:43:11 +01001953 if (termios->c_cflag & CRTSCTS) {
1954 if (old_cr & UART011_CR_RTS)
1955 old_cr |= UART011_CR_RTSEN;
1956
1957 old_cr |= UART011_CR_CTSEN;
1958 uap->autorts = true;
1959 } else {
1960 old_cr &= ~(UART011_CR_CTSEN | UART011_CR_RTSEN);
1961 uap->autorts = false;
1962 }
1963
Russell Kingc19f12b2010-12-22 17:48:26 +00001964 if (uap->vendor->oversampling) {
1965 if (baud > port->uartclk / 16)
Linus Walleijac3e3fb2010-06-02 20:40:22 +01001966 old_cr |= ST_UART011_CR_OVSFACT;
1967 else
1968 old_cr &= ~ST_UART011_CR_OVSFACT;
1969 }
1970
Linus Walleijc5dd5532012-09-26 17:21:36 +02001971 /*
1972 * Workaround for the ST Micro oversampling variants to
1973 * increase the bitrate slightly, by lowering the divisor,
1974 * to avoid delayed sampling of start bit at high speeds,
1975 * else we see data corruption.
1976 */
1977 if (uap->vendor->oversampling) {
1978 if ((baud >= 3000000) && (baud < 3250000) && (quot > 1))
1979 quot -= 1;
1980 else if ((baud > 3250000) && (quot > 2))
1981 quot -= 2;
1982 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 /* Set baud rate */
Jun Nie7b753f32015-07-31 15:49:16 +08001984 pl011_writew(uap, quot & 0x3f, REG_FBRD);
1985 pl011_writew(uap, quot >> 6, REG_IBRD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986
1987 /*
1988 * ----------v----------v----------v----------v-----
Linus Walleijc5dd5532012-09-26 17:21:36 +02001989 * NOTE: lcrh_tx and lcrh_rx MUST BE WRITTEN AFTER
Jun Nie534e14e2015-07-31 15:49:15 +08001990 * REG_FBRD & REG_IBRD.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 * ----------^----------^----------^----------^-----
1992 */
Jon Medhurstb60f2f62013-12-10 10:18:59 +00001993 pl011_write_lcr_h(uap, lcr_h);
Jun Nie7b753f32015-07-31 15:49:16 +08001994 pl011_writew(uap, old_cr, REG_CR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995
1996 spin_unlock_irqrestore(&port->lock, flags);
1997}
1998
Andre Przywara0dd1e242015-05-21 17:26:23 +01001999static void
2000sbsa_uart_set_termios(struct uart_port *port, struct ktermios *termios,
2001 struct ktermios *old)
2002{
2003 struct uart_amba_port *uap =
2004 container_of(port, struct uart_amba_port, port);
2005 unsigned long flags;
2006
2007 tty_termios_encode_baud_rate(termios, uap->fixed_baud, uap->fixed_baud);
2008
2009 /* The SBSA UART only supports 8n1 without hardware flow control. */
2010 termios->c_cflag &= ~(CSIZE | CSTOPB | PARENB | PARODD);
2011 termios->c_cflag &= ~(CMSPAR | CRTSCTS);
2012 termios->c_cflag |= CS8 | CLOCAL;
2013
2014 spin_lock_irqsave(&port->lock, flags);
2015 uart_update_timeout(port, CS8, uap->fixed_baud);
2016 pl011_setup_status_masks(port, termios);
2017 spin_unlock_irqrestore(&port->lock, flags);
2018}
2019
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020static const char *pl011_type(struct uart_port *port)
2021{
Daniel Thompsona5820c22014-09-03 12:51:55 +01002022 struct uart_amba_port *uap =
2023 container_of(port, struct uart_amba_port, port);
Russell Kinge8a7ba82010-12-28 09:16:54 +00002024 return uap->port.type == PORT_AMBA ? uap->type : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025}
2026
2027/*
2028 * Release the memory region(s) being used by 'port'
2029 */
Linus Walleije643f872012-06-17 15:44:19 +02002030static void pl011_release_port(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031{
2032 release_mem_region(port->mapbase, SZ_4K);
2033}
2034
2035/*
2036 * Request the memory region(s) being used by 'port'
2037 */
Linus Walleije643f872012-06-17 15:44:19 +02002038static int pl011_request_port(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039{
2040 return request_mem_region(port->mapbase, SZ_4K, "uart-pl011")
2041 != NULL ? 0 : -EBUSY;
2042}
2043
2044/*
2045 * Configure/autoconfigure the port.
2046 */
Linus Walleije643f872012-06-17 15:44:19 +02002047static void pl011_config_port(struct uart_port *port, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048{
2049 if (flags & UART_CONFIG_TYPE) {
2050 port->type = PORT_AMBA;
Linus Walleije643f872012-06-17 15:44:19 +02002051 pl011_request_port(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 }
2053}
2054
2055/*
2056 * verify the new serial_struct (for TIOCSSERIAL).
2057 */
Linus Walleije643f872012-06-17 15:44:19 +02002058static int pl011_verify_port(struct uart_port *port, struct serial_struct *ser)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059{
2060 int ret = 0;
2061 if (ser->type != PORT_UNKNOWN && ser->type != PORT_AMBA)
2062 ret = -EINVAL;
Yinghai Lua62c4132008-08-19 20:49:55 -07002063 if (ser->irq < 0 || ser->irq >= nr_irqs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 ret = -EINVAL;
2065 if (ser->baud_base < 9600)
2066 ret = -EINVAL;
2067 return ret;
2068}
2069
2070static struct uart_ops amba_pl011_pops = {
Linus Walleije643f872012-06-17 15:44:19 +02002071 .tx_empty = pl011_tx_empty,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 .set_mctrl = pl011_set_mctrl,
Linus Walleije643f872012-06-17 15:44:19 +02002073 .get_mctrl = pl011_get_mctrl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 .stop_tx = pl011_stop_tx,
2075 .start_tx = pl011_start_tx,
2076 .stop_rx = pl011_stop_rx,
2077 .enable_ms = pl011_enable_ms,
2078 .break_ctl = pl011_break_ctl,
2079 .startup = pl011_startup,
2080 .shutdown = pl011_shutdown,
Russell King68b65f72010-12-22 17:24:39 +00002081 .flush_buffer = pl011_dma_flush_buffer,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 .set_termios = pl011_set_termios,
2083 .type = pl011_type,
Linus Walleije643f872012-06-17 15:44:19 +02002084 .release_port = pl011_release_port,
2085 .request_port = pl011_request_port,
2086 .config_port = pl011_config_port,
2087 .verify_port = pl011_verify_port,
Jason Wessel84b5ae12008-02-20 13:33:39 -06002088#ifdef CONFIG_CONSOLE_POLL
Anton Vorontsovb3564c22012-09-24 14:27:54 -07002089 .poll_init = pl011_hwinit,
Linus Walleije643f872012-06-17 15:44:19 +02002090 .poll_get_char = pl011_get_poll_char,
2091 .poll_put_char = pl011_put_poll_char,
Jason Wessel84b5ae12008-02-20 13:33:39 -06002092#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093};
2094
Andre Przywara0dd1e242015-05-21 17:26:23 +01002095static void sbsa_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
2096{
2097}
2098
2099static unsigned int sbsa_uart_get_mctrl(struct uart_port *port)
2100{
2101 return 0;
2102}
2103
2104static const struct uart_ops sbsa_uart_pops = {
2105 .tx_empty = pl011_tx_empty,
2106 .set_mctrl = sbsa_uart_set_mctrl,
2107 .get_mctrl = sbsa_uart_get_mctrl,
2108 .stop_tx = pl011_stop_tx,
2109 .start_tx = pl011_start_tx,
2110 .stop_rx = pl011_stop_rx,
2111 .startup = sbsa_uart_startup,
2112 .shutdown = sbsa_uart_shutdown,
2113 .set_termios = sbsa_uart_set_termios,
2114 .type = pl011_type,
2115 .release_port = pl011_release_port,
2116 .request_port = pl011_request_port,
2117 .config_port = pl011_config_port,
2118 .verify_port = pl011_verify_port,
2119#ifdef CONFIG_CONSOLE_POLL
2120 .poll_init = pl011_hwinit,
2121 .poll_get_char = pl011_get_poll_char,
2122 .poll_put_char = pl011_put_poll_char,
2123#endif
2124};
2125
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126static struct uart_amba_port *amba_ports[UART_NR];
2127
2128#ifdef CONFIG_SERIAL_AMBA_PL011_CONSOLE
2129
Russell Kingd3587882006-03-20 20:00:09 +00002130static void pl011_console_putchar(struct uart_port *port, int ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131{
Daniel Thompsona5820c22014-09-03 12:51:55 +01002132 struct uart_amba_port *uap =
2133 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134
Jun Nie7b753f32015-07-31 15:49:16 +08002135 while (pl011_readw(uap, REG_FR) & UART01x_FR_TXFF)
Russell Kingd3587882006-03-20 20:00:09 +00002136 barrier();
Jun Nie7b753f32015-07-31 15:49:16 +08002137 pl011_writew(uap, ch, REG_DR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138}
2139
2140static void
2141pl011_console_write(struct console *co, const char *s, unsigned int count)
2142{
2143 struct uart_amba_port *uap = amba_ports[co->index];
Andre Przywara71eec482015-05-21 17:26:21 +01002144 unsigned int status, old_cr = 0, new_cr;
Rabin Vincentef605fd2012-01-17 11:52:28 +01002145 unsigned long flags;
2146 int locked = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147
2148 clk_enable(uap->clk);
2149
Rabin Vincentef605fd2012-01-17 11:52:28 +01002150 local_irq_save(flags);
2151 if (uap->port.sysrq)
2152 locked = 0;
2153 else if (oops_in_progress)
2154 locked = spin_trylock(&uap->port.lock);
2155 else
2156 spin_lock(&uap->port.lock);
2157
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 /*
2159 * First save the CR then disable the interrupts
2160 */
Andre Przywara71eec482015-05-21 17:26:21 +01002161 if (!uap->vendor->always_enabled) {
Jun Nie7b753f32015-07-31 15:49:16 +08002162 old_cr = pl011_readw(uap, REG_CR);
Andre Przywara71eec482015-05-21 17:26:21 +01002163 new_cr = old_cr & ~UART011_CR_CTSEN;
2164 new_cr |= UART01x_CR_UARTEN | UART011_CR_TXE;
Jun Nie7b753f32015-07-31 15:49:16 +08002165 pl011_writew(uap, new_cr, REG_CR);
Andre Przywara71eec482015-05-21 17:26:21 +01002166 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167
Russell Kingd3587882006-03-20 20:00:09 +00002168 uart_console_write(&uap->port, s, count, pl011_console_putchar);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169
2170 /*
2171 * Finally, wait for transmitter to become empty
2172 * and restore the TCR
2173 */
2174 do {
Jun Nie7b753f32015-07-31 15:49:16 +08002175 status = pl011_readw(uap, REG_FR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 } while (status & UART01x_FR_BUSY);
Andre Przywara71eec482015-05-21 17:26:21 +01002177 if (!uap->vendor->always_enabled)
Jun Nie7b753f32015-07-31 15:49:16 +08002178 pl011_writew(uap, old_cr, REG_CR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179
Rabin Vincentef605fd2012-01-17 11:52:28 +01002180 if (locked)
2181 spin_unlock(&uap->port.lock);
2182 local_irq_restore(flags);
2183
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 clk_disable(uap->clk);
2185}
2186
2187static void __init
2188pl011_console_get_options(struct uart_amba_port *uap, int *baud,
2189 int *parity, int *bits)
2190{
Jun Nie7b753f32015-07-31 15:49:16 +08002191 if (pl011_readw(uap, REG_CR) & UART01x_CR_UARTEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 unsigned int lcr_h, ibrd, fbrd;
2193
Jun Nie7b753f32015-07-31 15:49:16 +08002194 lcr_h = pl011_readw(uap, uap->lcrh_tx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195
2196 *parity = 'n';
2197 if (lcr_h & UART01x_LCRH_PEN) {
2198 if (lcr_h & UART01x_LCRH_EPS)
2199 *parity = 'e';
2200 else
2201 *parity = 'o';
2202 }
2203
2204 if ((lcr_h & 0x60) == UART01x_LCRH_WLEN_7)
2205 *bits = 7;
2206 else
2207 *bits = 8;
2208
Jun Nie7b753f32015-07-31 15:49:16 +08002209 ibrd = pl011_readw(uap, REG_IBRD);
2210 fbrd = pl011_readw(uap, REG_FBRD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211
2212 *baud = uap->port.uartclk * 4 / (64 * ibrd + fbrd);
Linus Walleijac3e3fb2010-06-02 20:40:22 +01002213
Russell Kingc19f12b2010-12-22 17:48:26 +00002214 if (uap->vendor->oversampling) {
Jun Nie7b753f32015-07-31 15:49:16 +08002215 if (pl011_readw(uap, REG_CR)
Linus Walleijac3e3fb2010-06-02 20:40:22 +01002216 & ST_UART011_CR_OVSFACT)
2217 *baud *= 2;
2218 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 }
2220}
2221
2222static int __init pl011_console_setup(struct console *co, char *options)
2223{
2224 struct uart_amba_port *uap;
2225 int baud = 38400;
2226 int bits = 8;
2227 int parity = 'n';
2228 int flow = 'n';
Russell King4b4851c2011-09-22 11:35:30 +01002229 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230
2231 /*
2232 * Check whether an invalid uart number has been specified, and
2233 * if so, search for the first available port that does have
2234 * console support.
2235 */
2236 if (co->index >= UART_NR)
2237 co->index = 0;
2238 uap = amba_ports[co->index];
Russell Kingd28122a2007-01-22 18:59:42 +00002239 if (!uap)
2240 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241
Linus Walleij78d80c52012-05-23 21:18:46 +02002242 /* Allow pins to be muxed in and configured */
Linus Walleij2b996fc2013-06-05 15:36:42 +02002243 pinctrl_pm_select_default_state(uap->port.dev);
Linus Walleij78d80c52012-05-23 21:18:46 +02002244
Russell King4b4851c2011-09-22 11:35:30 +01002245 ret = clk_prepare(uap->clk);
2246 if (ret)
2247 return ret;
2248
Jingoo Han574de552013-07-30 17:06:57 +09002249 if (dev_get_platdata(uap->port.dev)) {
Shreshtha Kumar Sahuc16d51a2011-06-13 10:11:33 +02002250 struct amba_pl011_data *plat;
2251
Jingoo Han574de552013-07-30 17:06:57 +09002252 plat = dev_get_platdata(uap->port.dev);
Shreshtha Kumar Sahuc16d51a2011-06-13 10:11:33 +02002253 if (plat->init)
2254 plat->init();
2255 }
2256
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 uap->port.uartclk = clk_get_rate(uap->clk);
2258
Andre Przywaracefc2d12015-05-21 17:26:22 +01002259 if (uap->vendor->fixed_options) {
2260 baud = uap->fixed_baud;
2261 } else {
2262 if (options)
2263 uart_parse_options(options,
2264 &baud, &parity, &bits, &flow);
2265 else
2266 pl011_console_get_options(uap, &baud, &parity, &bits);
2267 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268
2269 return uart_set_options(&uap->port, co, baud, parity, bits, flow);
2270}
2271
Vincent Sanders2d934862005-09-14 22:36:03 +01002272static struct uart_driver amba_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273static struct console amba_console = {
2274 .name = "ttyAMA",
2275 .write = pl011_console_write,
2276 .device = uart_console_device,
2277 .setup = pl011_console_setup,
2278 .flags = CON_PRINTBUFFER,
2279 .index = -1,
2280 .data = &amba_reg,
2281};
2282
2283#define AMBA_CONSOLE (&amba_console)
Rob Herring0d3c6732014-04-18 17:19:57 -05002284
2285static void pl011_putc(struct uart_port *port, int c)
2286{
Jun Nie7b753f32015-07-31 15:49:16 +08002287 struct uart_amba_port *uap =
2288 container_of(port, struct uart_amba_port, port);
2289
2290 while (pl011_readw(uap, REG_FR) & UART01x_FR_TXFF)
Rob Herring0d3c6732014-04-18 17:19:57 -05002291 ;
Jun Nie7b753f32015-07-31 15:49:16 +08002292 pl011_writeb(uap, c, REG_DR);
2293 while (pl011_readw(uap, REG_FR) & UART01x_FR_BUSY)
Rob Herring0d3c6732014-04-18 17:19:57 -05002294 ;
2295}
2296
2297static void pl011_early_write(struct console *con, const char *s, unsigned n)
2298{
2299 struct earlycon_device *dev = con->data;
2300
2301 uart_console_write(&dev->port, s, n, pl011_putc);
2302}
2303
2304static int __init pl011_early_console_setup(struct earlycon_device *device,
2305 const char *opt)
2306{
2307 if (!device->port.membase)
2308 return -ENODEV;
2309
2310 device->con->write = pl011_early_write;
2311 return 0;
2312}
2313EARLYCON_DECLARE(pl011, pl011_early_console_setup);
Rob Herring45e0f0f2014-03-27 08:08:03 -05002314OF_EARLYCON_DECLARE(pl011, "arm,pl011", pl011_early_console_setup);
Rob Herring0d3c6732014-04-18 17:19:57 -05002315
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316#else
2317#define AMBA_CONSOLE NULL
2318#endif
2319
2320static struct uart_driver amba_reg = {
2321 .owner = THIS_MODULE,
2322 .driver_name = "ttyAMA",
2323 .dev_name = "ttyAMA",
2324 .major = SERIAL_AMBA_MAJOR,
2325 .minor = SERIAL_AMBA_MINOR,
2326 .nr = UART_NR,
2327 .cons = AMBA_CONSOLE,
2328};
2329
Matthew Leach32614aa2012-08-28 16:41:28 +01002330static int pl011_probe_dt_alias(int index, struct device *dev)
2331{
2332 struct device_node *np;
2333 static bool seen_dev_with_alias = false;
2334 static bool seen_dev_without_alias = false;
2335 int ret = index;
2336
2337 if (!IS_ENABLED(CONFIG_OF))
2338 return ret;
2339
2340 np = dev->of_node;
2341 if (!np)
2342 return ret;
2343
2344 ret = of_alias_get_id(np, "serial");
2345 if (IS_ERR_VALUE(ret)) {
2346 seen_dev_without_alias = true;
2347 ret = index;
2348 } else {
2349 seen_dev_with_alias = true;
2350 if (ret >= ARRAY_SIZE(amba_ports) || amba_ports[ret] != NULL) {
2351 dev_warn(dev, "requested serial port %d not available.\n", ret);
2352 ret = index;
2353 }
2354 }
2355
2356 if (seen_dev_with_alias && seen_dev_without_alias)
2357 dev_warn(dev, "aliased and non-aliased serial devices found in device tree. Serial port enumeration may be unpredictable.\n");
2358
2359 return ret;
2360}
2361
Andre Przywara49bb3c82015-05-21 17:26:14 +01002362/* unregisters the driver also if no more ports are left */
2363static void pl011_unregister_port(struct uart_amba_port *uap)
2364{
2365 int i;
2366 bool busy = false;
2367
2368 for (i = 0; i < ARRAY_SIZE(amba_ports); i++) {
2369 if (amba_ports[i] == uap)
2370 amba_ports[i] = NULL;
2371 else if (amba_ports[i])
2372 busy = true;
2373 }
2374 pl011_dma_remove(uap);
2375 if (!busy)
2376 uart_unregister_driver(&amba_reg);
2377}
2378
Andre Przywara3873e2d2015-05-21 17:26:18 +01002379static int pl011_find_free_port(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380{
Andre Przywara3873e2d2015-05-21 17:26:18 +01002381 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382
2383 for (i = 0; i < ARRAY_SIZE(amba_ports); i++)
2384 if (amba_ports[i] == NULL)
Andre Przywara3873e2d2015-05-21 17:26:18 +01002385 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386
Andre Przywara3873e2d2015-05-21 17:26:18 +01002387 return -EBUSY;
2388}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389
Andre Przywara3873e2d2015-05-21 17:26:18 +01002390static int pl011_setup_port(struct device *dev, struct uart_amba_port *uap,
2391 struct resource *mmiobase, int index)
2392{
2393 void __iomem *base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394
Andre Przywara3873e2d2015-05-21 17:26:18 +01002395 base = devm_ioremap_resource(dev, mmiobase);
Krzysztof Kozlowski97a60ea2015-07-09 22:21:41 +09002396 if (IS_ERR(base))
2397 return PTR_ERR(base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398
Andre Przywara3873e2d2015-05-21 17:26:18 +01002399 index = pl011_probe_dt_alias(index, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400
Shreshtha Kumar Sahud8d8ffa2012-01-18 15:53:59 +05302401 uap->old_cr = 0;
Andre Przywara3873e2d2015-05-21 17:26:18 +01002402 uap->port.dev = dev;
2403 uap->port.mapbase = mmiobase->start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 uap->port.membase = base;
2405 uap->port.iotype = UPIO_MEM;
Russell Kingffca2b12010-12-22 17:13:05 +00002406 uap->port.fifosize = uap->fifosize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407 uap->port.flags = UPF_BOOT_AUTOCONF;
Andre Przywara3873e2d2015-05-21 17:26:18 +01002408 uap->port.line = index;
2409
2410 amba_ports[index] = uap;
2411
2412 return 0;
2413}
2414
2415static int pl011_register_port(struct uart_amba_port *uap)
2416{
2417 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418
Linus Walleijc3d8b762012-03-21 20:15:18 +01002419 /* Ensure interrupts from this UART are masked and cleared */
Jun Nie7b753f32015-07-31 15:49:16 +08002420 pl011_writew(uap, 0, REG_IMSC);
2421 pl011_writew(uap, 0xffff, REG_ICR);
Linus Walleijc3d8b762012-03-21 20:15:18 +01002422
Tushar Beheraef2889f2014-01-20 14:32:35 +05302423 if (!amba_reg.state) {
2424 ret = uart_register_driver(&amba_reg);
2425 if (ret < 0) {
Andre Przywara3873e2d2015-05-21 17:26:18 +01002426 dev_err(uap->port.dev,
Jorge Ramirez-Ortiz1c9be312015-03-06 13:05:40 -05002427 "Failed to register AMBA-PL011 driver\n");
Tushar Beheraef2889f2014-01-20 14:32:35 +05302428 return ret;
2429 }
2430 }
2431
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 ret = uart_add_one_port(&amba_reg, &uap->port);
Andre Przywara49bb3c82015-05-21 17:26:14 +01002433 if (ret)
2434 pl011_unregister_port(uap);
Tushar Behera7f6d9422014-06-26 15:35:35 +05302435
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 return ret;
2437}
2438
Andre Przywara3873e2d2015-05-21 17:26:18 +01002439static int pl011_probe(struct amba_device *dev, const struct amba_id *id)
2440{
2441 struct uart_amba_port *uap;
2442 struct vendor_data *vendor = id->data;
2443 int portnr, ret;
2444
2445 portnr = pl011_find_free_port();
2446 if (portnr < 0)
2447 return portnr;
2448
2449 uap = devm_kzalloc(&dev->dev, sizeof(struct uart_amba_port),
2450 GFP_KERNEL);
2451 if (!uap)
2452 return -ENOMEM;
2453
2454 uap->clk = devm_clk_get(&dev->dev, NULL);
2455 if (IS_ERR(uap->clk))
2456 return PTR_ERR(uap->clk);
2457
2458 uap->vendor = vendor;
Jun Nie2c096a92015-07-31 15:49:17 +08002459 uap->reg_lut = vendor->reg_lut;
Andre Przywara3873e2d2015-05-21 17:26:18 +01002460 uap->lcrh_rx = vendor->lcrh_rx;
2461 uap->lcrh_tx = vendor->lcrh_tx;
2462 uap->fifosize = vendor->get_fifosize(dev);
2463 uap->port.irq = dev->irq[0];
2464 uap->port.ops = &amba_pl011_pops;
2465
2466 snprintf(uap->type, sizeof(uap->type), "PL011 rev%u", amba_rev(dev));
2467
2468 ret = pl011_setup_port(&dev->dev, uap, &dev->res, portnr);
2469 if (ret)
2470 return ret;
2471
2472 amba_set_drvdata(dev, uap);
2473
2474 return pl011_register_port(uap);
2475}
2476
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477static int pl011_remove(struct amba_device *dev)
2478{
2479 struct uart_amba_port *uap = amba_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 uart_remove_one_port(&amba_reg, &uap->port);
Andre Przywara49bb3c82015-05-21 17:26:14 +01002482 pl011_unregister_port(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483 return 0;
2484}
2485
Ulf Hanssond0ce8502013-12-03 11:04:28 +01002486#ifdef CONFIG_PM_SLEEP
2487static int pl011_suspend(struct device *dev)
Leo Chenb736b892009-07-28 23:43:33 +01002488{
Ulf Hanssond0ce8502013-12-03 11:04:28 +01002489 struct uart_amba_port *uap = dev_get_drvdata(dev);
Leo Chenb736b892009-07-28 23:43:33 +01002490
2491 if (!uap)
2492 return -EINVAL;
2493
2494 return uart_suspend_port(&amba_reg, &uap->port);
2495}
2496
Ulf Hanssond0ce8502013-12-03 11:04:28 +01002497static int pl011_resume(struct device *dev)
Leo Chenb736b892009-07-28 23:43:33 +01002498{
Ulf Hanssond0ce8502013-12-03 11:04:28 +01002499 struct uart_amba_port *uap = dev_get_drvdata(dev);
Leo Chenb736b892009-07-28 23:43:33 +01002500
2501 if (!uap)
2502 return -EINVAL;
2503
2504 return uart_resume_port(&amba_reg, &uap->port);
2505}
2506#endif
2507
Ulf Hanssond0ce8502013-12-03 11:04:28 +01002508static SIMPLE_DEV_PM_OPS(pl011_dev_pm_ops, pl011_suspend, pl011_resume);
2509
Andre Przywara0dd1e242015-05-21 17:26:23 +01002510static int sbsa_uart_probe(struct platform_device *pdev)
2511{
2512 struct uart_amba_port *uap;
2513 struct resource *r;
2514 int portnr, ret;
2515 int baudrate;
2516
2517 /*
2518 * Check the mandatory baud rate parameter in the DT node early
2519 * so that we can easily exit with the error.
2520 */
2521 if (pdev->dev.of_node) {
2522 struct device_node *np = pdev->dev.of_node;
2523
2524 ret = of_property_read_u32(np, "current-speed", &baudrate);
2525 if (ret)
2526 return ret;
2527 } else {
2528 baudrate = 115200;
2529 }
2530
2531 portnr = pl011_find_free_port();
2532 if (portnr < 0)
2533 return portnr;
2534
2535 uap = devm_kzalloc(&pdev->dev, sizeof(struct uart_amba_port),
2536 GFP_KERNEL);
2537 if (!uap)
2538 return -ENOMEM;
2539
2540 uap->vendor = &vendor_sbsa;
Jun Nie2c096a92015-07-31 15:49:17 +08002541 uap->reg_lut = vendor_sbsa.reg_lut;
Andre Przywara0dd1e242015-05-21 17:26:23 +01002542 uap->fifosize = 32;
2543 uap->port.irq = platform_get_irq(pdev, 0);
2544 uap->port.ops = &sbsa_uart_pops;
2545 uap->fixed_baud = baudrate;
2546
2547 snprintf(uap->type, sizeof(uap->type), "SBSA");
2548
2549 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2550
2551 ret = pl011_setup_port(&pdev->dev, uap, r, portnr);
2552 if (ret)
2553 return ret;
2554
2555 platform_set_drvdata(pdev, uap);
2556
2557 return pl011_register_port(uap);
2558}
2559
2560static int sbsa_uart_remove(struct platform_device *pdev)
2561{
2562 struct uart_amba_port *uap = platform_get_drvdata(pdev);
2563
2564 uart_remove_one_port(&amba_reg, &uap->port);
2565 pl011_unregister_port(uap);
2566 return 0;
2567}
2568
2569static const struct of_device_id sbsa_uart_of_match[] = {
2570 { .compatible = "arm,sbsa-uart", },
2571 {},
2572};
2573MODULE_DEVICE_TABLE(of, sbsa_uart_of_match);
2574
Graeme Gregory3db9ab02015-05-21 17:26:24 +01002575static const struct acpi_device_id sbsa_uart_acpi_match[] = {
2576 { "ARMH0011", 0 },
2577 {},
2578};
2579MODULE_DEVICE_TABLE(acpi, sbsa_uart_acpi_match);
2580
Andre Przywara0dd1e242015-05-21 17:26:23 +01002581static struct platform_driver arm_sbsa_uart_platform_driver = {
2582 .probe = sbsa_uart_probe,
2583 .remove = sbsa_uart_remove,
2584 .driver = {
2585 .name = "sbsa-uart",
2586 .of_match_table = of_match_ptr(sbsa_uart_of_match),
Graeme Gregory3db9ab02015-05-21 17:26:24 +01002587 .acpi_match_table = ACPI_PTR(sbsa_uart_acpi_match),
Andre Przywara0dd1e242015-05-21 17:26:23 +01002588 },
2589};
2590
Russell King2c39c9e2010-07-27 08:50:16 +01002591static struct amba_id pl011_ids[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592 {
2593 .id = 0x00041011,
2594 .mask = 0x000fffff,
Alessandro Rubini5926a292009-06-04 17:43:04 +01002595 .data = &vendor_arm,
2596 },
2597 {
2598 .id = 0x00380802,
2599 .mask = 0x00ffffff,
2600 .data = &vendor_st,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601 },
2602 { 0, 0 },
2603};
2604
Dave Martin60f7a332011-10-05 15:15:22 +01002605MODULE_DEVICE_TABLE(amba, pl011_ids);
2606
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607static struct amba_driver pl011_driver = {
2608 .drv = {
2609 .name = "uart-pl011",
Ulf Hanssond0ce8502013-12-03 11:04:28 +01002610 .pm = &pl011_dev_pm_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611 },
2612 .id_table = pl011_ids,
2613 .probe = pl011_probe,
2614 .remove = pl011_remove,
2615};
2616
2617static int __init pl011_init(void)
2618{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619 printk(KERN_INFO "Serial: AMBA PL011 UART driver\n");
2620
Andre Przywara0dd1e242015-05-21 17:26:23 +01002621 if (platform_driver_register(&arm_sbsa_uart_platform_driver))
2622 pr_warn("could not register SBSA UART platform driver\n");
Tushar Beheraef2889f2014-01-20 14:32:35 +05302623 return amba_driver_register(&pl011_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624}
2625
2626static void __exit pl011_exit(void)
2627{
Andre Przywara0dd1e242015-05-21 17:26:23 +01002628 platform_driver_unregister(&arm_sbsa_uart_platform_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629 amba_driver_unregister(&pl011_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630}
2631
Alessandro Rubini4dd9e742009-05-05 05:54:13 +01002632/*
2633 * While this can be a module, if builtin it's most likely the console
2634 * So let's leave module_exit but move module_init to an earlier place
2635 */
2636arch_initcall(pl011_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637module_exit(pl011_exit);
2638
2639MODULE_AUTHOR("ARM Ltd/Deep Blue Solutions Ltd");
2640MODULE_DESCRIPTION("ARM AMBA serial port driver");
2641MODULE_LICENSE("GPL");