blob: 017443d092c151fde4b781721ee171bb945e87ce [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 Nie09dcc7d2015-07-31 15:49:18 +0800252static bool is_implemented(struct uart_amba_port *uap, unsigned int reg)
253{
254 return uap->reg_lut[reg] != (u16)~0;
255}
256
Jun Nie7b753f32015-07-31 15:49:16 +0800257static unsigned int pl011_readw(struct uart_amba_port *uap, int index)
258{
259 WARN_ON(index > REG_NR);
Jun Nie2c096a92015-07-31 15:49:17 +0800260 return readw_relaxed(uap->port.membase + uap->reg_lut[index]);
Jun Nie7b753f32015-07-31 15:49:16 +0800261}
262
263static void pl011_writew(struct uart_amba_port *uap, int val, int index)
264{
265 WARN_ON(index > REG_NR);
Jun Nie2c096a92015-07-31 15:49:17 +0800266 writew_relaxed(val, uap->port.membase + uap->reg_lut[index]);
Jun Nie7b753f32015-07-31 15:49:16 +0800267}
268
269static void pl011_writeb(struct uart_amba_port *uap, u8 val, int index)
270{
271 WARN_ON(index > REG_NR);
Jun Nie2c096a92015-07-31 15:49:17 +0800272 writeb_relaxed(val, uap->port.membase + uap->reg_lut[index]);
Jun Nie7b753f32015-07-31 15:49:16 +0800273}
274
Russell King68b65f72010-12-22 17:24:39 +0000275/*
Linus Walleij29772c42011-02-24 13:21:36 +0100276 * Reads up to 256 characters from the FIFO or until it's empty and
277 * inserts them into the TTY layer. Returns the number of characters
278 * read from the FIFO.
279 */
280static int pl011_fifo_to_tty(struct uart_amba_port *uap)
281{
282 u16 status, ch;
283 unsigned int flag, max_count = 256;
284 int fifotaken = 0;
285
286 while (max_count--) {
Jun Nie7b753f32015-07-31 15:49:16 +0800287 status = pl011_readw(uap, REG_FR);
Linus Walleij29772c42011-02-24 13:21:36 +0100288 if (status & UART01x_FR_RXFE)
289 break;
290
291 /* Take chars from the FIFO and update status */
Jun Nie7b753f32015-07-31 15:49:16 +0800292 ch = pl011_readw(uap, REG_DR) |
Linus Walleij29772c42011-02-24 13:21:36 +0100293 UART_DUMMY_DR_RX;
294 flag = TTY_NORMAL;
295 uap->port.icount.rx++;
296 fifotaken++;
297
298 if (unlikely(ch & UART_DR_ERROR)) {
299 if (ch & UART011_DR_BE) {
300 ch &= ~(UART011_DR_FE | UART011_DR_PE);
301 uap->port.icount.brk++;
302 if (uart_handle_break(&uap->port))
303 continue;
304 } else if (ch & UART011_DR_PE)
305 uap->port.icount.parity++;
306 else if (ch & UART011_DR_FE)
307 uap->port.icount.frame++;
308 if (ch & UART011_DR_OE)
309 uap->port.icount.overrun++;
310
311 ch &= uap->port.read_status_mask;
312
313 if (ch & UART011_DR_BE)
314 flag = TTY_BREAK;
315 else if (ch & UART011_DR_PE)
316 flag = TTY_PARITY;
317 else if (ch & UART011_DR_FE)
318 flag = TTY_FRAME;
319 }
320
321 if (uart_handle_sysrq_char(&uap->port, ch & 255))
322 continue;
323
324 uart_insert_char(&uap->port, ch, UART011_DR_OE, ch, flag);
325 }
326
327 return fifotaken;
328}
329
330
331/*
Russell King68b65f72010-12-22 17:24:39 +0000332 * All the DMA operation mode stuff goes inside this ifdef.
333 * This assumes that you have a generic DMA device interface,
334 * no custom DMA interfaces are supported.
335 */
336#ifdef CONFIG_DMA_ENGINE
337
338#define PL011_DMA_BUFFER_SIZE PAGE_SIZE
339
Linus Walleijead76f32011-02-24 13:21:08 +0100340static int pl011_sgbuf_init(struct dma_chan *chan, struct pl011_sgbuf *sg,
341 enum dma_data_direction dir)
342{
Chanho Mincb06ff12013-03-27 18:38:11 +0900343 dma_addr_t dma_addr;
344
345 sg->buf = dma_alloc_coherent(chan->device->dev,
346 PL011_DMA_BUFFER_SIZE, &dma_addr, GFP_KERNEL);
Linus Walleijead76f32011-02-24 13:21:08 +0100347 if (!sg->buf)
348 return -ENOMEM;
349
Chanho Mincb06ff12013-03-27 18:38:11 +0900350 sg_init_table(&sg->sg, 1);
351 sg_set_page(&sg->sg, phys_to_page(dma_addr),
352 PL011_DMA_BUFFER_SIZE, offset_in_page(dma_addr));
353 sg_dma_address(&sg->sg) = dma_addr;
Andrew Jacksonc64be922014-11-07 14:14:43 +0000354 sg_dma_len(&sg->sg) = PL011_DMA_BUFFER_SIZE;
Linus Walleijead76f32011-02-24 13:21:08 +0100355
Linus Walleijead76f32011-02-24 13:21:08 +0100356 return 0;
357}
358
359static void pl011_sgbuf_free(struct dma_chan *chan, struct pl011_sgbuf *sg,
360 enum dma_data_direction dir)
361{
362 if (sg->buf) {
Chanho Mincb06ff12013-03-27 18:38:11 +0900363 dma_free_coherent(chan->device->dev,
364 PL011_DMA_BUFFER_SIZE, sg->buf,
365 sg_dma_address(&sg->sg));
Linus Walleijead76f32011-02-24 13:21:08 +0100366 }
367}
368
Jorge Ramirez-Ortiz1c9be312015-03-06 13:05:40 -0500369static void pl011_dma_probe(struct uart_amba_port *uap)
Russell King68b65f72010-12-22 17:24:39 +0000370{
371 /* DMA is the sole user of the platform data right now */
Jingoo Han574de552013-07-30 17:06:57 +0900372 struct amba_pl011_data *plat = dev_get_platdata(uap->port.dev);
Jorge Ramirez-Ortiz1c9be312015-03-06 13:05:40 -0500373 struct device *dev = uap->port.dev;
Russell King68b65f72010-12-22 17:24:39 +0000374 struct dma_slave_config tx_conf = {
Jun Nie2c096a92015-07-31 15:49:17 +0800375 .dst_addr = uap->port.mapbase + uap->reg_lut[REG_DR],
Russell King68b65f72010-12-22 17:24:39 +0000376 .dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE,
Vinod Koula485df42011-10-14 10:47:38 +0530377 .direction = DMA_MEM_TO_DEV,
Russell King68b65f72010-12-22 17:24:39 +0000378 .dst_maxburst = uap->fifosize >> 1,
Viresh Kumar258aea72012-02-01 16:12:19 +0530379 .device_fc = false,
Russell King68b65f72010-12-22 17:24:39 +0000380 };
381 struct dma_chan *chan;
382 dma_cap_mask_t mask;
383
Jorge Ramirez-Ortiz1c9be312015-03-06 13:05:40 -0500384 uap->dma_probed = true;
385 chan = dma_request_slave_channel_reason(dev, "tx");
386 if (IS_ERR(chan)) {
387 if (PTR_ERR(chan) == -EPROBE_DEFER) {
Jorge Ramirez-Ortiz1c9be312015-03-06 13:05:40 -0500388 uap->dma_probed = false;
389 return;
390 }
Russell King68b65f72010-12-22 17:24:39 +0000391
Arnd Bergmann787b0c12013-01-28 16:24:37 +0000392 /* We need platform data */
393 if (!plat || !plat->dma_filter) {
394 dev_info(uap->port.dev, "no DMA platform data\n");
395 return;
396 }
397
398 /* Try to acquire a generic DMA engine slave TX channel */
399 dma_cap_zero(mask);
400 dma_cap_set(DMA_SLAVE, mask);
401
402 chan = dma_request_channel(mask, plat->dma_filter,
403 plat->dma_tx_param);
404 if (!chan) {
405 dev_err(uap->port.dev, "no TX DMA channel!\n");
406 return;
407 }
Russell King68b65f72010-12-22 17:24:39 +0000408 }
409
410 dmaengine_slave_config(chan, &tx_conf);
411 uap->dmatx.chan = chan;
412
413 dev_info(uap->port.dev, "DMA channel TX %s\n",
414 dma_chan_name(uap->dmatx.chan));
Linus Walleijead76f32011-02-24 13:21:08 +0100415
416 /* Optionally make use of an RX channel as well */
Arnd Bergmann787b0c12013-01-28 16:24:37 +0000417 chan = dma_request_slave_channel(dev, "rx");
Rob Herring0d3c6732014-04-18 17:19:57 -0500418
Arnd Bergmann787b0c12013-01-28 16:24:37 +0000419 if (!chan && plat->dma_rx_param) {
420 chan = dma_request_channel(mask, plat->dma_filter, plat->dma_rx_param);
421
422 if (!chan) {
423 dev_err(uap->port.dev, "no RX DMA channel!\n");
424 return;
425 }
426 }
427
428 if (chan) {
Linus Walleijead76f32011-02-24 13:21:08 +0100429 struct dma_slave_config rx_conf = {
Jun Nie2c096a92015-07-31 15:49:17 +0800430 .src_addr = uap->port.mapbase + uap->reg_lut[REG_DR],
Linus Walleijead76f32011-02-24 13:21:08 +0100431 .src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE,
Vinod Koula485df42011-10-14 10:47:38 +0530432 .direction = DMA_DEV_TO_MEM,
Guennadi Liakhovetskib2aeb772014-04-12 19:47:17 +0200433 .src_maxburst = uap->fifosize >> 2,
Viresh Kumar258aea72012-02-01 16:12:19 +0530434 .device_fc = false,
Linus Walleijead76f32011-02-24 13:21:08 +0100435 };
Andrew Jackson2d3b7d62014-11-07 14:14:47 +0000436 struct dma_slave_caps caps;
Linus Walleijead76f32011-02-24 13:21:08 +0100437
Andrew Jackson2d3b7d62014-11-07 14:14:47 +0000438 /*
439 * Some DMA controllers provide information on their capabilities.
440 * If the controller does, check for suitable residue processing
441 * otherwise assime all is well.
442 */
443 if (0 == dma_get_slave_caps(chan, &caps)) {
444 if (caps.residue_granularity ==
445 DMA_RESIDUE_GRANULARITY_DESCRIPTOR) {
446 dma_release_channel(chan);
447 dev_info(uap->port.dev,
448 "RX DMA disabled - no residue processing\n");
449 return;
450 }
451 }
Linus Walleijead76f32011-02-24 13:21:08 +0100452 dmaengine_slave_config(chan, &rx_conf);
453 uap->dmarx.chan = chan;
454
Andrew Jackson98267d32014-11-07 14:14:23 +0000455 uap->dmarx.auto_poll_rate = false;
Greg Kroah-Hartman8f898bf2013-12-17 09:33:18 -0800456 if (plat && plat->dma_rx_poll_enable) {
Chanho Mincb06ff12013-03-27 18:38:11 +0900457 /* Set poll rate if specified. */
458 if (plat->dma_rx_poll_rate) {
459 uap->dmarx.auto_poll_rate = false;
460 uap->dmarx.poll_rate = plat->dma_rx_poll_rate;
461 } else {
462 /*
463 * 100 ms defaults to poll rate if not
464 * specified. This will be adjusted with
465 * the baud rate at set_termios.
466 */
467 uap->dmarx.auto_poll_rate = true;
468 uap->dmarx.poll_rate = 100;
469 }
470 /* 3 secs defaults poll_timeout if not specified. */
471 if (plat->dma_rx_poll_timeout)
472 uap->dmarx.poll_timeout =
473 plat->dma_rx_poll_timeout;
474 else
475 uap->dmarx.poll_timeout = 3000;
Andrew Jackson98267d32014-11-07 14:14:23 +0000476 } else if (!plat && dev->of_node) {
477 uap->dmarx.auto_poll_rate = of_property_read_bool(
478 dev->of_node, "auto-poll");
479 if (uap->dmarx.auto_poll_rate) {
480 u32 x;
Chanho Mincb06ff12013-03-27 18:38:11 +0900481
Andrew Jackson98267d32014-11-07 14:14:23 +0000482 if (0 == of_property_read_u32(dev->of_node,
483 "poll-rate-ms", &x))
484 uap->dmarx.poll_rate = x;
485 else
486 uap->dmarx.poll_rate = 100;
487 if (0 == of_property_read_u32(dev->of_node,
488 "poll-timeout-ms", &x))
489 uap->dmarx.poll_timeout = x;
490 else
491 uap->dmarx.poll_timeout = 3000;
492 }
493 }
Linus Walleijead76f32011-02-24 13:21:08 +0100494 dev_info(uap->port.dev, "DMA channel RX %s\n",
495 dma_chan_name(uap->dmarx.chan));
496 }
Russell King68b65f72010-12-22 17:24:39 +0000497}
498
Russell King68b65f72010-12-22 17:24:39 +0000499static void pl011_dma_remove(struct uart_amba_port *uap)
500{
Russell King68b65f72010-12-22 17:24:39 +0000501 if (uap->dmatx.chan)
502 dma_release_channel(uap->dmatx.chan);
Linus Walleijead76f32011-02-24 13:21:08 +0100503 if (uap->dmarx.chan)
504 dma_release_channel(uap->dmarx.chan);
Russell King68b65f72010-12-22 17:24:39 +0000505}
506
Dave Martin734745c2015-03-04 12:27:33 +0000507/* Forward declare these for the refill routine */
Russell King68b65f72010-12-22 17:24:39 +0000508static int pl011_dma_tx_refill(struct uart_amba_port *uap);
Dave Martin734745c2015-03-04 12:27:33 +0000509static void pl011_start_tx_pio(struct uart_amba_port *uap);
Russell King68b65f72010-12-22 17:24:39 +0000510
511/*
512 * The current DMA TX buffer has been sent.
513 * Try to queue up another DMA buffer.
514 */
515static void pl011_dma_tx_callback(void *data)
516{
517 struct uart_amba_port *uap = data;
518 struct pl011_dmatx_data *dmatx = &uap->dmatx;
519 unsigned long flags;
520 u16 dmacr;
521
522 spin_lock_irqsave(&uap->port.lock, flags);
523 if (uap->dmatx.queued)
524 dma_unmap_sg(dmatx->chan->device->dev, &dmatx->sg, 1,
525 DMA_TO_DEVICE);
526
527 dmacr = uap->dmacr;
528 uap->dmacr = dmacr & ~UART011_TXDMAE;
Jun Nie7b753f32015-07-31 15:49:16 +0800529 pl011_writew(uap, uap->dmacr, REG_DMACR);
Russell King68b65f72010-12-22 17:24:39 +0000530
531 /*
532 * If TX DMA was disabled, it means that we've stopped the DMA for
533 * some reason (eg, XOFF received, or we want to send an X-char.)
534 *
535 * Note: we need to be careful here of a potential race between DMA
536 * and the rest of the driver - if the driver disables TX DMA while
537 * a TX buffer completing, we must update the tx queued status to
538 * get further refills (hence we check dmacr).
539 */
540 if (!(dmacr & UART011_TXDMAE) || uart_tx_stopped(&uap->port) ||
541 uart_circ_empty(&uap->port.state->xmit)) {
542 uap->dmatx.queued = false;
543 spin_unlock_irqrestore(&uap->port.lock, flags);
544 return;
545 }
546
Dave Martin734745c2015-03-04 12:27:33 +0000547 if (pl011_dma_tx_refill(uap) <= 0)
Russell King68b65f72010-12-22 17:24:39 +0000548 /*
549 * We didn't queue a DMA buffer for some reason, but we
550 * have data pending to be sent. Re-enable the TX IRQ.
551 */
Dave Martin734745c2015-03-04 12:27:33 +0000552 pl011_start_tx_pio(uap);
553
Russell King68b65f72010-12-22 17:24:39 +0000554 spin_unlock_irqrestore(&uap->port.lock, flags);
555}
556
557/*
558 * Try to refill the TX DMA buffer.
559 * Locking: called with port lock held and IRQs disabled.
560 * Returns:
561 * 1 if we queued up a TX DMA buffer.
562 * 0 if we didn't want to handle this by DMA
563 * <0 on error
564 */
565static int pl011_dma_tx_refill(struct uart_amba_port *uap)
566{
567 struct pl011_dmatx_data *dmatx = &uap->dmatx;
568 struct dma_chan *chan = dmatx->chan;
569 struct dma_device *dma_dev = chan->device;
570 struct dma_async_tx_descriptor *desc;
571 struct circ_buf *xmit = &uap->port.state->xmit;
572 unsigned int count;
573
574 /*
575 * Try to avoid the overhead involved in using DMA if the
576 * transaction fits in the first half of the FIFO, by using
577 * the standard interrupt handling. This ensures that we
578 * issue a uart_write_wakeup() at the appropriate time.
579 */
580 count = uart_circ_chars_pending(xmit);
581 if (count < (uap->fifosize >> 1)) {
582 uap->dmatx.queued = false;
583 return 0;
584 }
585
586 /*
587 * Bodge: don't send the last character by DMA, as this
588 * will prevent XON from notifying us to restart DMA.
589 */
590 count -= 1;
591
592 /* Else proceed to copy the TX chars to the DMA buffer and fire DMA */
593 if (count > PL011_DMA_BUFFER_SIZE)
594 count = PL011_DMA_BUFFER_SIZE;
595
596 if (xmit->tail < xmit->head)
597 memcpy(&dmatx->buf[0], &xmit->buf[xmit->tail], count);
598 else {
599 size_t first = UART_XMIT_SIZE - xmit->tail;
Andrew Jacksone2a545a2014-11-07 14:14:39 +0000600 size_t second;
601
602 if (first > count)
603 first = count;
604 second = count - first;
Russell King68b65f72010-12-22 17:24:39 +0000605
606 memcpy(&dmatx->buf[0], &xmit->buf[xmit->tail], first);
607 if (second)
608 memcpy(&dmatx->buf[first], &xmit->buf[0], second);
609 }
610
611 dmatx->sg.length = count;
612
613 if (dma_map_sg(dma_dev->dev, &dmatx->sg, 1, DMA_TO_DEVICE) != 1) {
614 uap->dmatx.queued = false;
615 dev_dbg(uap->port.dev, "unable to map TX DMA\n");
616 return -EBUSY;
617 }
618
Alexandre Bounine16052822012-03-08 16:11:18 -0500619 desc = dmaengine_prep_slave_sg(chan, &dmatx->sg, 1, DMA_MEM_TO_DEV,
Russell King68b65f72010-12-22 17:24:39 +0000620 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
621 if (!desc) {
622 dma_unmap_sg(dma_dev->dev, &dmatx->sg, 1, DMA_TO_DEVICE);
623 uap->dmatx.queued = false;
624 /*
625 * If DMA cannot be used right now, we complete this
626 * transaction via IRQ and let the TTY layer retry.
627 */
628 dev_dbg(uap->port.dev, "TX DMA busy\n");
629 return -EBUSY;
630 }
631
632 /* Some data to go along to the callback */
633 desc->callback = pl011_dma_tx_callback;
634 desc->callback_param = uap;
635
636 /* All errors should happen at prepare time */
637 dmaengine_submit(desc);
638
639 /* Fire the DMA transaction */
640 dma_dev->device_issue_pending(chan);
641
642 uap->dmacr |= UART011_TXDMAE;
Jun Nie7b753f32015-07-31 15:49:16 +0800643 pl011_writew(uap, uap->dmacr, REG_DMACR);
Russell King68b65f72010-12-22 17:24:39 +0000644 uap->dmatx.queued = true;
645
646 /*
647 * Now we know that DMA will fire, so advance the ring buffer
648 * with the stuff we just dispatched.
649 */
650 xmit->tail = (xmit->tail + count) & (UART_XMIT_SIZE - 1);
651 uap->port.icount.tx += count;
652
653 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
654 uart_write_wakeup(&uap->port);
655
656 return 1;
657}
658
659/*
660 * We received a transmit interrupt without a pending X-char but with
661 * pending characters.
662 * Locking: called with port lock held and IRQs disabled.
663 * Returns:
664 * false if we want to use PIO to transmit
665 * true if we queued a DMA buffer
666 */
667static bool pl011_dma_tx_irq(struct uart_amba_port *uap)
668{
Linus Walleijead76f32011-02-24 13:21:08 +0100669 if (!uap->using_tx_dma)
Russell King68b65f72010-12-22 17:24:39 +0000670 return false;
671
672 /*
673 * If we already have a TX buffer queued, but received a
674 * TX interrupt, it will be because we've just sent an X-char.
675 * Ensure the TX DMA is enabled and the TX IRQ is disabled.
676 */
677 if (uap->dmatx.queued) {
678 uap->dmacr |= UART011_TXDMAE;
Jun Nie7b753f32015-07-31 15:49:16 +0800679 pl011_writew(uap, uap->dmacr, REG_DMACR);
Russell King68b65f72010-12-22 17:24:39 +0000680 uap->im &= ~UART011_TXIM;
Jun Nie7b753f32015-07-31 15:49:16 +0800681 pl011_writew(uap, uap->im, REG_IMSC);
Russell King68b65f72010-12-22 17:24:39 +0000682 return true;
683 }
684
685 /*
686 * We don't have a TX buffer queued, so try to queue one.
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300687 * If we successfully queued a buffer, mask the TX IRQ.
Russell King68b65f72010-12-22 17:24:39 +0000688 */
689 if (pl011_dma_tx_refill(uap) > 0) {
690 uap->im &= ~UART011_TXIM;
Jun Nie7b753f32015-07-31 15:49:16 +0800691 pl011_writew(uap, uap->im, REG_IMSC);
Russell King68b65f72010-12-22 17:24:39 +0000692 return true;
693 }
694 return false;
695}
696
697/*
698 * Stop the DMA transmit (eg, due to received XOFF).
699 * Locking: called with port lock held and IRQs disabled.
700 */
701static inline void pl011_dma_tx_stop(struct uart_amba_port *uap)
702{
703 if (uap->dmatx.queued) {
704 uap->dmacr &= ~UART011_TXDMAE;
Jun Nie7b753f32015-07-31 15:49:16 +0800705 pl011_writew(uap, uap->dmacr, REG_DMACR);
Russell King68b65f72010-12-22 17:24:39 +0000706 }
707}
708
709/*
710 * Try to start a DMA transmit, or in the case of an XON/OFF
711 * character queued for send, try to get that character out ASAP.
712 * Locking: called with port lock held and IRQs disabled.
713 * Returns:
714 * false if we want the TX IRQ to be enabled
715 * true if we have a buffer queued
716 */
717static inline bool pl011_dma_tx_start(struct uart_amba_port *uap)
718{
719 u16 dmacr;
720
Linus Walleijead76f32011-02-24 13:21:08 +0100721 if (!uap->using_tx_dma)
Russell King68b65f72010-12-22 17:24:39 +0000722 return false;
723
724 if (!uap->port.x_char) {
725 /* no X-char, try to push chars out in DMA mode */
726 bool ret = true;
727
728 if (!uap->dmatx.queued) {
729 if (pl011_dma_tx_refill(uap) > 0) {
730 uap->im &= ~UART011_TXIM;
Jun Nie7b753f32015-07-31 15:49:16 +0800731 pl011_writew(uap, uap->im, REG_IMSC);
Dave Martin734745c2015-03-04 12:27:33 +0000732 } else
Russell King68b65f72010-12-22 17:24:39 +0000733 ret = false;
Russell King68b65f72010-12-22 17:24:39 +0000734 } else if (!(uap->dmacr & UART011_TXDMAE)) {
735 uap->dmacr |= UART011_TXDMAE;
Jun Nie7b753f32015-07-31 15:49:16 +0800736 pl011_writew(uap, uap->dmacr, REG_DMACR);
Russell King68b65f72010-12-22 17:24:39 +0000737 }
738 return ret;
739 }
740
741 /*
742 * We have an X-char to send. Disable DMA to prevent it loading
743 * the TX fifo, and then see if we can stuff it into the FIFO.
744 */
745 dmacr = uap->dmacr;
746 uap->dmacr &= ~UART011_TXDMAE;
Jun Nie7b753f32015-07-31 15:49:16 +0800747 pl011_writew(uap, uap->dmacr, REG_DMACR);
Russell King68b65f72010-12-22 17:24:39 +0000748
Jun Nie7b753f32015-07-31 15:49:16 +0800749 if (pl011_readw(uap, REG_FR) & UART01x_FR_TXFF) {
Russell King68b65f72010-12-22 17:24:39 +0000750 /*
751 * No space in the FIFO, so enable the transmit interrupt
752 * so we know when there is space. Note that once we've
753 * loaded the character, we should just re-enable DMA.
754 */
755 return false;
756 }
757
Jun Nie7b753f32015-07-31 15:49:16 +0800758 pl011_writew(uap, uap->port.x_char, REG_DR);
Russell King68b65f72010-12-22 17:24:39 +0000759 uap->port.icount.tx++;
760 uap->port.x_char = 0;
761
762 /* Success - restore the DMA state */
763 uap->dmacr = dmacr;
Jun Nie7b753f32015-07-31 15:49:16 +0800764 pl011_writew(uap, dmacr, REG_DMACR);
Russell King68b65f72010-12-22 17:24:39 +0000765
766 return true;
767}
768
769/*
770 * Flush the transmit buffer.
771 * Locking: called with port lock held and IRQs disabled.
772 */
773static void pl011_dma_flush_buffer(struct uart_port *port)
Fabio Estevamb83286b2013-08-09 17:58:51 -0300774__releases(&uap->port.lock)
775__acquires(&uap->port.lock)
Russell King68b65f72010-12-22 17:24:39 +0000776{
Daniel Thompsona5820c22014-09-03 12:51:55 +0100777 struct uart_amba_port *uap =
778 container_of(port, struct uart_amba_port, port);
Russell King68b65f72010-12-22 17:24:39 +0000779
Linus Walleijead76f32011-02-24 13:21:08 +0100780 if (!uap->using_tx_dma)
Russell King68b65f72010-12-22 17:24:39 +0000781 return;
782
783 /* Avoid deadlock with the DMA engine callback */
784 spin_unlock(&uap->port.lock);
785 dmaengine_terminate_all(uap->dmatx.chan);
786 spin_lock(&uap->port.lock);
787 if (uap->dmatx.queued) {
788 dma_unmap_sg(uap->dmatx.chan->device->dev, &uap->dmatx.sg, 1,
789 DMA_TO_DEVICE);
790 uap->dmatx.queued = false;
791 uap->dmacr &= ~UART011_TXDMAE;
Jun Nie7b753f32015-07-31 15:49:16 +0800792 pl011_writew(uap, uap->dmacr, REG_DMACR);
Russell King68b65f72010-12-22 17:24:39 +0000793 }
794}
795
Linus Walleijead76f32011-02-24 13:21:08 +0100796static void pl011_dma_rx_callback(void *data);
797
798static int pl011_dma_rx_trigger_dma(struct uart_amba_port *uap)
799{
800 struct dma_chan *rxchan = uap->dmarx.chan;
Linus Walleijead76f32011-02-24 13:21:08 +0100801 struct pl011_dmarx_data *dmarx = &uap->dmarx;
802 struct dma_async_tx_descriptor *desc;
803 struct pl011_sgbuf *sgbuf;
804
805 if (!rxchan)
806 return -EIO;
807
808 /* Start the RX DMA job */
809 sgbuf = uap->dmarx.use_buf_b ?
810 &uap->dmarx.sgbuf_b : &uap->dmarx.sgbuf_a;
Alexandre Bounine16052822012-03-08 16:11:18 -0500811 desc = dmaengine_prep_slave_sg(rxchan, &sgbuf->sg, 1,
Vinod Koula485df42011-10-14 10:47:38 +0530812 DMA_DEV_TO_MEM,
Linus Walleijead76f32011-02-24 13:21:08 +0100813 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
814 /*
815 * If the DMA engine is busy and cannot prepare a
816 * channel, no big deal, the driver will fall back
817 * to interrupt mode as a result of this error code.
818 */
819 if (!desc) {
820 uap->dmarx.running = false;
821 dmaengine_terminate_all(rxchan);
822 return -EBUSY;
823 }
824
825 /* Some data to go along to the callback */
826 desc->callback = pl011_dma_rx_callback;
827 desc->callback_param = uap;
828 dmarx->cookie = dmaengine_submit(desc);
829 dma_async_issue_pending(rxchan);
830
831 uap->dmacr |= UART011_RXDMAE;
Jun Nie7b753f32015-07-31 15:49:16 +0800832 pl011_writew(uap, uap->dmacr, REG_DMACR);
Linus Walleijead76f32011-02-24 13:21:08 +0100833 uap->dmarx.running = true;
834
835 uap->im &= ~UART011_RXIM;
Jun Nie7b753f32015-07-31 15:49:16 +0800836 pl011_writew(uap, uap->im, REG_IMSC);
Linus Walleijead76f32011-02-24 13:21:08 +0100837
838 return 0;
839}
840
841/*
842 * This is called when either the DMA job is complete, or
843 * the FIFO timeout interrupt occurred. This must be called
844 * with the port spinlock uap->port.lock held.
845 */
846static void pl011_dma_rx_chars(struct uart_amba_port *uap,
847 u32 pending, bool use_buf_b,
848 bool readfifo)
849{
Jiri Slaby05c7cd32013-01-03 15:53:04 +0100850 struct tty_port *port = &uap->port.state->port;
Linus Walleijead76f32011-02-24 13:21:08 +0100851 struct pl011_sgbuf *sgbuf = use_buf_b ?
852 &uap->dmarx.sgbuf_b : &uap->dmarx.sgbuf_a;
Linus Walleijead76f32011-02-24 13:21:08 +0100853 int dma_count = 0;
854 u32 fifotaken = 0; /* only used for vdbg() */
855
Chanho Mincb06ff12013-03-27 18:38:11 +0900856 struct pl011_dmarx_data *dmarx = &uap->dmarx;
857 int dmataken = 0;
858
859 if (uap->dmarx.poll_rate) {
860 /* The data can be taken by polling */
861 dmataken = sgbuf->sg.length - dmarx->last_residue;
862 /* Recalculate the pending size */
863 if (pending >= dmataken)
864 pending -= dmataken;
865 }
866
867 /* Pick the remain data from the DMA */
Linus Walleijead76f32011-02-24 13:21:08 +0100868 if (pending) {
Linus Walleijead76f32011-02-24 13:21:08 +0100869
870 /*
871 * First take all chars in the DMA pipe, then look in the FIFO.
872 * Note that tty_insert_flip_buf() tries to take as many chars
873 * as it can.
874 */
Chanho Mincb06ff12013-03-27 18:38:11 +0900875 dma_count = tty_insert_flip_string(port, sgbuf->buf + dmataken,
876 pending);
Linus Walleijead76f32011-02-24 13:21:08 +0100877
878 uap->port.icount.rx += dma_count;
879 if (dma_count < pending)
880 dev_warn(uap->port.dev,
881 "couldn't insert all characters (TTY is full?)\n");
882 }
883
Chanho Mincb06ff12013-03-27 18:38:11 +0900884 /* Reset the last_residue for Rx DMA poll */
885 if (uap->dmarx.poll_rate)
886 dmarx->last_residue = sgbuf->sg.length;
887
Linus Walleijead76f32011-02-24 13:21:08 +0100888 /*
889 * Only continue with trying to read the FIFO if all DMA chars have
890 * been taken first.
891 */
892 if (dma_count == pending && readfifo) {
893 /* Clear any error flags */
Jun Nie7b753f32015-07-31 15:49:16 +0800894 pl011_writew(uap,
895 UART011_OEIS | UART011_BEIS | UART011_PEIS
896 | UART011_FEIS, REG_ICR);
Linus Walleijead76f32011-02-24 13:21:08 +0100897
898 /*
899 * If we read all the DMA'd characters, and we had an
Linus Walleij29772c42011-02-24 13:21:36 +0100900 * incomplete buffer, that could be due to an rx error, or
901 * maybe we just timed out. Read any pending chars and check
902 * the error status.
903 *
904 * Error conditions will only occur in the FIFO, these will
905 * trigger an immediate interrupt and stop the DMA job, so we
906 * will always find the error in the FIFO, never in the DMA
907 * buffer.
Linus Walleijead76f32011-02-24 13:21:08 +0100908 */
Linus Walleij29772c42011-02-24 13:21:36 +0100909 fifotaken = pl011_fifo_to_tty(uap);
Linus Walleijead76f32011-02-24 13:21:08 +0100910 }
911
912 spin_unlock(&uap->port.lock);
913 dev_vdbg(uap->port.dev,
914 "Took %d chars from DMA buffer and %d chars from the FIFO\n",
915 dma_count, fifotaken);
Jiri Slaby2e124b42013-01-03 15:53:06 +0100916 tty_flip_buffer_push(port);
Linus Walleijead76f32011-02-24 13:21:08 +0100917 spin_lock(&uap->port.lock);
918}
919
920static void pl011_dma_rx_irq(struct uart_amba_port *uap)
921{
922 struct pl011_dmarx_data *dmarx = &uap->dmarx;
923 struct dma_chan *rxchan = dmarx->chan;
924 struct pl011_sgbuf *sgbuf = dmarx->use_buf_b ?
925 &dmarx->sgbuf_b : &dmarx->sgbuf_a;
926 size_t pending;
927 struct dma_tx_state state;
928 enum dma_status dmastat;
929
930 /*
931 * Pause the transfer so we can trust the current counter,
932 * do this before we pause the PL011 block, else we may
933 * overflow the FIFO.
934 */
935 if (dmaengine_pause(rxchan))
936 dev_err(uap->port.dev, "unable to pause DMA transfer\n");
937 dmastat = rxchan->device->device_tx_status(rxchan,
938 dmarx->cookie, &state);
939 if (dmastat != DMA_PAUSED)
940 dev_err(uap->port.dev, "unable to pause DMA transfer\n");
941
942 /* Disable RX DMA - incoming data will wait in the FIFO */
943 uap->dmacr &= ~UART011_RXDMAE;
Jun Nie7b753f32015-07-31 15:49:16 +0800944 pl011_writew(uap, uap->dmacr, REG_DMACR);
Linus Walleijead76f32011-02-24 13:21:08 +0100945 uap->dmarx.running = false;
946
947 pending = sgbuf->sg.length - state.residue;
948 BUG_ON(pending > PL011_DMA_BUFFER_SIZE);
949 /* Then we terminate the transfer - we now know our residue */
950 dmaengine_terminate_all(rxchan);
951
952 /*
953 * This will take the chars we have so far and insert
954 * into the framework.
955 */
956 pl011_dma_rx_chars(uap, pending, dmarx->use_buf_b, true);
957
958 /* Switch buffer & re-trigger DMA job */
959 dmarx->use_buf_b = !dmarx->use_buf_b;
960 if (pl011_dma_rx_trigger_dma(uap)) {
961 dev_dbg(uap->port.dev, "could not retrigger RX DMA job "
962 "fall back to interrupt mode\n");
963 uap->im |= UART011_RXIM;
Jun Nie7b753f32015-07-31 15:49:16 +0800964 pl011_writew(uap, uap->im, REG_IMSC);
Linus Walleijead76f32011-02-24 13:21:08 +0100965 }
966}
967
968static void pl011_dma_rx_callback(void *data)
969{
970 struct uart_amba_port *uap = data;
971 struct pl011_dmarx_data *dmarx = &uap->dmarx;
Chanho Min6dc01aa2012-02-20 10:24:40 +0900972 struct dma_chan *rxchan = dmarx->chan;
Linus Walleijead76f32011-02-24 13:21:08 +0100973 bool lastbuf = dmarx->use_buf_b;
Chanho Min6dc01aa2012-02-20 10:24:40 +0900974 struct pl011_sgbuf *sgbuf = dmarx->use_buf_b ?
975 &dmarx->sgbuf_b : &dmarx->sgbuf_a;
976 size_t pending;
977 struct dma_tx_state state;
Linus Walleijead76f32011-02-24 13:21:08 +0100978 int ret;
979
980 /*
981 * This completion interrupt occurs typically when the
982 * RX buffer is totally stuffed but no timeout has yet
983 * occurred. When that happens, we just want the RX
984 * routine to flush out the secondary DMA buffer while
985 * we immediately trigger the next DMA job.
986 */
987 spin_lock_irq(&uap->port.lock);
Chanho Min6dc01aa2012-02-20 10:24:40 +0900988 /*
989 * Rx data can be taken by the UART interrupts during
990 * the DMA irq handler. So we check the residue here.
991 */
992 rxchan->device->device_tx_status(rxchan, dmarx->cookie, &state);
993 pending = sgbuf->sg.length - state.residue;
994 BUG_ON(pending > PL011_DMA_BUFFER_SIZE);
995 /* Then we terminate the transfer - we now know our residue */
996 dmaengine_terminate_all(rxchan);
997
Linus Walleijead76f32011-02-24 13:21:08 +0100998 uap->dmarx.running = false;
999 dmarx->use_buf_b = !lastbuf;
1000 ret = pl011_dma_rx_trigger_dma(uap);
1001
Chanho Min6dc01aa2012-02-20 10:24:40 +09001002 pl011_dma_rx_chars(uap, pending, lastbuf, false);
Linus Walleijead76f32011-02-24 13:21:08 +01001003 spin_unlock_irq(&uap->port.lock);
1004 /*
1005 * Do this check after we picked the DMA chars so we don't
1006 * get some IRQ immediately from RX.
1007 */
1008 if (ret) {
1009 dev_dbg(uap->port.dev, "could not retrigger RX DMA job "
1010 "fall back to interrupt mode\n");
1011 uap->im |= UART011_RXIM;
Jun Nie7b753f32015-07-31 15:49:16 +08001012 pl011_writew(uap, uap->im, REG_IMSC);
Linus Walleijead76f32011-02-24 13:21:08 +01001013 }
1014}
1015
1016/*
1017 * Stop accepting received characters, when we're shutting down or
1018 * suspending this port.
1019 * Locking: called with port lock held and IRQs disabled.
1020 */
1021static inline void pl011_dma_rx_stop(struct uart_amba_port *uap)
1022{
1023 /* FIXME. Just disable the DMA enable */
1024 uap->dmacr &= ~UART011_RXDMAE;
Jun Nie7b753f32015-07-31 15:49:16 +08001025 pl011_writew(uap, uap->dmacr, REG_DMACR);
Linus Walleijead76f32011-02-24 13:21:08 +01001026}
Russell King68b65f72010-12-22 17:24:39 +00001027
Chanho Mincb06ff12013-03-27 18:38:11 +09001028/*
1029 * Timer handler for Rx DMA polling.
1030 * Every polling, It checks the residue in the dma buffer and transfer
1031 * data to the tty. Also, last_residue is updated for the next polling.
1032 */
1033static void pl011_dma_rx_poll(unsigned long args)
1034{
1035 struct uart_amba_port *uap = (struct uart_amba_port *)args;
1036 struct tty_port *port = &uap->port.state->port;
1037 struct pl011_dmarx_data *dmarx = &uap->dmarx;
1038 struct dma_chan *rxchan = uap->dmarx.chan;
1039 unsigned long flags = 0;
1040 unsigned int dmataken = 0;
1041 unsigned int size = 0;
1042 struct pl011_sgbuf *sgbuf;
1043 int dma_count;
1044 struct dma_tx_state state;
1045
1046 sgbuf = dmarx->use_buf_b ? &uap->dmarx.sgbuf_b : &uap->dmarx.sgbuf_a;
1047 rxchan->device->device_tx_status(rxchan, dmarx->cookie, &state);
1048 if (likely(state.residue < dmarx->last_residue)) {
1049 dmataken = sgbuf->sg.length - dmarx->last_residue;
1050 size = dmarx->last_residue - state.residue;
1051 dma_count = tty_insert_flip_string(port, sgbuf->buf + dmataken,
1052 size);
1053 if (dma_count == size)
1054 dmarx->last_residue = state.residue;
1055 dmarx->last_jiffies = jiffies;
1056 }
1057 tty_flip_buffer_push(port);
1058
1059 /*
1060 * If no data is received in poll_timeout, the driver will fall back
1061 * to interrupt mode. We will retrigger DMA at the first interrupt.
1062 */
1063 if (jiffies_to_msecs(jiffies - dmarx->last_jiffies)
1064 > uap->dmarx.poll_timeout) {
1065
1066 spin_lock_irqsave(&uap->port.lock, flags);
1067 pl011_dma_rx_stop(uap);
Guennadi Liakhovetskic25a1ad2013-12-10 14:54:47 +01001068 uap->im |= UART011_RXIM;
Jun Nie7b753f32015-07-31 15:49:16 +08001069 pl011_writew(uap, uap->im, REG_IMSC);
Chanho Mincb06ff12013-03-27 18:38:11 +09001070 spin_unlock_irqrestore(&uap->port.lock, flags);
1071
1072 uap->dmarx.running = false;
1073 dmaengine_terminate_all(rxchan);
1074 del_timer(&uap->dmarx.timer);
1075 } else {
1076 mod_timer(&uap->dmarx.timer,
1077 jiffies + msecs_to_jiffies(uap->dmarx.poll_rate));
1078 }
1079}
1080
Russell King68b65f72010-12-22 17:24:39 +00001081static void pl011_dma_startup(struct uart_amba_port *uap)
1082{
Linus Walleijead76f32011-02-24 13:21:08 +01001083 int ret;
1084
Jorge Ramirez-Ortiz1c9be312015-03-06 13:05:40 -05001085 if (!uap->dma_probed)
1086 pl011_dma_probe(uap);
1087
Russell King68b65f72010-12-22 17:24:39 +00001088 if (!uap->dmatx.chan)
1089 return;
1090
Andrew Jackson4c0be452014-11-07 14:14:35 +00001091 uap->dmatx.buf = kmalloc(PL011_DMA_BUFFER_SIZE, GFP_KERNEL | __GFP_DMA);
Russell King68b65f72010-12-22 17:24:39 +00001092 if (!uap->dmatx.buf) {
1093 dev_err(uap->port.dev, "no memory for DMA TX buffer\n");
1094 uap->port.fifosize = uap->fifosize;
1095 return;
1096 }
1097
1098 sg_init_one(&uap->dmatx.sg, uap->dmatx.buf, PL011_DMA_BUFFER_SIZE);
1099
1100 /* The DMA buffer is now the FIFO the TTY subsystem can use */
1101 uap->port.fifosize = PL011_DMA_BUFFER_SIZE;
Linus Walleijead76f32011-02-24 13:21:08 +01001102 uap->using_tx_dma = true;
Russell King68b65f72010-12-22 17:24:39 +00001103
Linus Walleijead76f32011-02-24 13:21:08 +01001104 if (!uap->dmarx.chan)
1105 goto skip_rx;
1106
1107 /* Allocate and map DMA RX buffers */
1108 ret = pl011_sgbuf_init(uap->dmarx.chan, &uap->dmarx.sgbuf_a,
1109 DMA_FROM_DEVICE);
1110 if (ret) {
1111 dev_err(uap->port.dev, "failed to init DMA %s: %d\n",
1112 "RX buffer A", ret);
1113 goto skip_rx;
1114 }
1115
1116 ret = pl011_sgbuf_init(uap->dmarx.chan, &uap->dmarx.sgbuf_b,
1117 DMA_FROM_DEVICE);
1118 if (ret) {
1119 dev_err(uap->port.dev, "failed to init DMA %s: %d\n",
1120 "RX buffer B", ret);
1121 pl011_sgbuf_free(uap->dmarx.chan, &uap->dmarx.sgbuf_a,
1122 DMA_FROM_DEVICE);
1123 goto skip_rx;
1124 }
1125
1126 uap->using_rx_dma = true;
1127
1128skip_rx:
Russell King68b65f72010-12-22 17:24:39 +00001129 /* Turn on DMA error (RX/TX will be enabled on demand) */
1130 uap->dmacr |= UART011_DMAONERR;
Jun Nie7b753f32015-07-31 15:49:16 +08001131 pl011_writew(uap, uap->dmacr, REG_DMACR);
Russell King38d62432010-12-22 17:59:16 +00001132
1133 /*
1134 * ST Micro variants has some specific dma burst threshold
1135 * compensation. Set this to 16 bytes, so burst will only
1136 * be issued above/below 16 bytes.
1137 */
1138 if (uap->vendor->dma_threshold)
Jun Nie7b753f32015-07-31 15:49:16 +08001139 pl011_writew(uap,
1140 ST_UART011_DMAWM_RX_16 | ST_UART011_DMAWM_TX_16,
1141 REG_ST_DMAWM);
Linus Walleijead76f32011-02-24 13:21:08 +01001142
1143 if (uap->using_rx_dma) {
1144 if (pl011_dma_rx_trigger_dma(uap))
1145 dev_dbg(uap->port.dev, "could not trigger initial "
1146 "RX DMA job, fall back to interrupt mode\n");
Chanho Mincb06ff12013-03-27 18:38:11 +09001147 if (uap->dmarx.poll_rate) {
1148 init_timer(&(uap->dmarx.timer));
1149 uap->dmarx.timer.function = pl011_dma_rx_poll;
1150 uap->dmarx.timer.data = (unsigned long)uap;
1151 mod_timer(&uap->dmarx.timer,
1152 jiffies +
1153 msecs_to_jiffies(uap->dmarx.poll_rate));
1154 uap->dmarx.last_residue = PL011_DMA_BUFFER_SIZE;
1155 uap->dmarx.last_jiffies = jiffies;
1156 }
Linus Walleijead76f32011-02-24 13:21:08 +01001157 }
Russell King68b65f72010-12-22 17:24:39 +00001158}
1159
1160static void pl011_dma_shutdown(struct uart_amba_port *uap)
1161{
Linus Walleijead76f32011-02-24 13:21:08 +01001162 if (!(uap->using_tx_dma || uap->using_rx_dma))
Russell King68b65f72010-12-22 17:24:39 +00001163 return;
1164
1165 /* Disable RX and TX DMA */
Greg Kroah-Hartman062a68a2015-09-04 09:11:24 -07001166 while (pl011_readw(uap, REG_FR) & UART01x_FR_BUSY)
Russell King68b65f72010-12-22 17:24:39 +00001167 barrier();
1168
1169 spin_lock_irq(&uap->port.lock);
1170 uap->dmacr &= ~(UART011_DMAONERR | UART011_RXDMAE | UART011_TXDMAE);
Jun Nie7b753f32015-07-31 15:49:16 +08001171 pl011_writew(uap, uap->dmacr, REG_DMACR);
Russell King68b65f72010-12-22 17:24:39 +00001172 spin_unlock_irq(&uap->port.lock);
1173
Linus Walleijead76f32011-02-24 13:21:08 +01001174 if (uap->using_tx_dma) {
1175 /* In theory, this should already be done by pl011_dma_flush_buffer */
1176 dmaengine_terminate_all(uap->dmatx.chan);
1177 if (uap->dmatx.queued) {
1178 dma_unmap_sg(uap->dmatx.chan->device->dev, &uap->dmatx.sg, 1,
1179 DMA_TO_DEVICE);
1180 uap->dmatx.queued = false;
1181 }
1182
1183 kfree(uap->dmatx.buf);
1184 uap->using_tx_dma = false;
Russell King68b65f72010-12-22 17:24:39 +00001185 }
1186
Linus Walleijead76f32011-02-24 13:21:08 +01001187 if (uap->using_rx_dma) {
1188 dmaengine_terminate_all(uap->dmarx.chan);
1189 /* Clean up the RX DMA */
1190 pl011_sgbuf_free(uap->dmarx.chan, &uap->dmarx.sgbuf_a, DMA_FROM_DEVICE);
1191 pl011_sgbuf_free(uap->dmarx.chan, &uap->dmarx.sgbuf_b, DMA_FROM_DEVICE);
Chanho Mincb06ff12013-03-27 18:38:11 +09001192 if (uap->dmarx.poll_rate)
1193 del_timer_sync(&uap->dmarx.timer);
Linus Walleijead76f32011-02-24 13:21:08 +01001194 uap->using_rx_dma = false;
1195 }
Russell King68b65f72010-12-22 17:24:39 +00001196}
1197
Linus Walleijead76f32011-02-24 13:21:08 +01001198static inline bool pl011_dma_rx_available(struct uart_amba_port *uap)
1199{
1200 return uap->using_rx_dma;
1201}
1202
1203static inline bool pl011_dma_rx_running(struct uart_amba_port *uap)
1204{
1205 return uap->using_rx_dma && uap->dmarx.running;
1206}
1207
Russell King68b65f72010-12-22 17:24:39 +00001208#else
1209/* Blank functions if the DMA engine is not available */
Jorge Ramirez-Ortiz1c9be312015-03-06 13:05:40 -05001210static inline void pl011_dma_probe(struct uart_amba_port *uap)
Russell King68b65f72010-12-22 17:24:39 +00001211{
1212}
1213
1214static inline void pl011_dma_remove(struct uart_amba_port *uap)
1215{
1216}
1217
1218static inline void pl011_dma_startup(struct uart_amba_port *uap)
1219{
1220}
1221
1222static inline void pl011_dma_shutdown(struct uart_amba_port *uap)
1223{
1224}
1225
1226static inline bool pl011_dma_tx_irq(struct uart_amba_port *uap)
1227{
1228 return false;
1229}
1230
1231static inline void pl011_dma_tx_stop(struct uart_amba_port *uap)
1232{
1233}
1234
1235static inline bool pl011_dma_tx_start(struct uart_amba_port *uap)
1236{
1237 return false;
1238}
1239
Linus Walleijead76f32011-02-24 13:21:08 +01001240static inline void pl011_dma_rx_irq(struct uart_amba_port *uap)
1241{
1242}
1243
1244static inline void pl011_dma_rx_stop(struct uart_amba_port *uap)
1245{
1246}
1247
1248static inline int pl011_dma_rx_trigger_dma(struct uart_amba_port *uap)
1249{
1250 return -EIO;
1251}
1252
1253static inline bool pl011_dma_rx_available(struct uart_amba_port *uap)
1254{
1255 return false;
1256}
1257
1258static inline bool pl011_dma_rx_running(struct uart_amba_port *uap)
1259{
1260 return false;
1261}
1262
Russell King68b65f72010-12-22 17:24:39 +00001263#define pl011_dma_flush_buffer NULL
1264#endif
1265
Russell Kingb129a8c2005-08-31 10:12:14 +01001266static void pl011_stop_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001268 struct uart_amba_port *uap =
1269 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270
1271 uap->im &= ~UART011_TXIM;
Jun Nie7b753f32015-07-31 15:49:16 +08001272 pl011_writew(uap, uap->im, REG_IMSC);
Russell King68b65f72010-12-22 17:24:39 +00001273 pl011_dma_tx_stop(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274}
1275
Dave Martin1e84d222015-04-27 16:49:05 +01001276static void pl011_tx_chars(struct uart_amba_port *uap, bool from_irq);
Dave Martin734745c2015-03-04 12:27:33 +00001277
1278/* Start TX with programmed I/O only (no DMA) */
1279static void pl011_start_tx_pio(struct uart_amba_port *uap)
1280{
1281 uap->im |= UART011_TXIM;
Jun Nie7b753f32015-07-31 15:49:16 +08001282 pl011_writew(uap, uap->im, REG_IMSC);
Dave Martin1e84d222015-04-27 16:49:05 +01001283 pl011_tx_chars(uap, false);
Dave Martin734745c2015-03-04 12:27:33 +00001284}
1285
Russell Kingb129a8c2005-08-31 10:12:14 +01001286static void pl011_start_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001288 struct uart_amba_port *uap =
1289 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290
Dave Martin734745c2015-03-04 12:27:33 +00001291 if (!pl011_dma_tx_start(uap))
1292 pl011_start_tx_pio(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293}
1294
1295static void pl011_stop_rx(struct uart_port *port)
1296{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001297 struct uart_amba_port *uap =
1298 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299
1300 uap->im &= ~(UART011_RXIM|UART011_RTIM|UART011_FEIM|
1301 UART011_PEIM|UART011_BEIM|UART011_OEIM);
Jun Nie7b753f32015-07-31 15:49:16 +08001302 pl011_writew(uap, uap->im, REG_IMSC);
Linus Walleijead76f32011-02-24 13:21:08 +01001303
1304 pl011_dma_rx_stop(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305}
1306
1307static void pl011_enable_ms(struct uart_port *port)
1308{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001309 struct uart_amba_port *uap =
1310 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311
1312 uap->im |= UART011_RIMIM|UART011_CTSMIM|UART011_DCDMIM|UART011_DSRMIM;
Jun Nie7b753f32015-07-31 15:49:16 +08001313 pl011_writew(uap, uap->im, REG_IMSC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314}
1315
David Howells7d12e782006-10-05 14:55:46 +01001316static void pl011_rx_chars(struct uart_amba_port *uap)
Fabio Estevamb83286b2013-08-09 17:58:51 -03001317__releases(&uap->port.lock)
1318__acquires(&uap->port.lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319{
Linus Walleij29772c42011-02-24 13:21:36 +01001320 pl011_fifo_to_tty(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321
Thomas Gleixner2389b272007-05-29 21:53:50 +01001322 spin_unlock(&uap->port.lock);
Jiri Slaby2e124b42013-01-03 15:53:06 +01001323 tty_flip_buffer_push(&uap->port.state->port);
Linus Walleijead76f32011-02-24 13:21:08 +01001324 /*
1325 * If we were temporarily out of DMA mode for a while,
1326 * attempt to switch back to DMA mode again.
1327 */
1328 if (pl011_dma_rx_available(uap)) {
1329 if (pl011_dma_rx_trigger_dma(uap)) {
1330 dev_dbg(uap->port.dev, "could not trigger RX DMA job "
1331 "fall back to interrupt mode again\n");
1332 uap->im |= UART011_RXIM;
Jun Nie7b753f32015-07-31 15:49:16 +08001333 pl011_writew(uap, uap->im, REG_IMSC);
Chanho Mincb06ff12013-03-27 18:38:11 +09001334 } else {
Chanho Min89fa28d2013-04-03 11:10:37 +09001335#ifdef CONFIG_DMA_ENGINE
Chanho Mincb06ff12013-03-27 18:38:11 +09001336 /* Start Rx DMA poll */
1337 if (uap->dmarx.poll_rate) {
1338 uap->dmarx.last_jiffies = jiffies;
1339 uap->dmarx.last_residue = PL011_DMA_BUFFER_SIZE;
1340 mod_timer(&uap->dmarx.timer,
1341 jiffies +
1342 msecs_to_jiffies(uap->dmarx.poll_rate));
1343 }
Chanho Min89fa28d2013-04-03 11:10:37 +09001344#endif
Chanho Mincb06ff12013-03-27 18:38:11 +09001345 }
Linus Walleijead76f32011-02-24 13:21:08 +01001346 }
Thomas Gleixner2389b272007-05-29 21:53:50 +01001347 spin_lock(&uap->port.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348}
1349
Dave Martin1e84d222015-04-27 16:49:05 +01001350static bool pl011_tx_char(struct uart_amba_port *uap, unsigned char c,
1351 bool from_irq)
Dave Martin734745c2015-03-04 12:27:33 +00001352{
Dave Martin1e84d222015-04-27 16:49:05 +01001353 if (unlikely(!from_irq) &&
Jun Nie7b753f32015-07-31 15:49:16 +08001354 pl011_readw(uap, REG_FR) & UART01x_FR_TXFF)
Dave Martin1e84d222015-04-27 16:49:05 +01001355 return false; /* unable to transmit character */
1356
Jun Nie7b753f32015-07-31 15:49:16 +08001357 pl011_writew(uap, c, REG_DR);
Dave Martin734745c2015-03-04 12:27:33 +00001358 uap->port.icount.tx++;
1359
Dave Martin1e84d222015-04-27 16:49:05 +01001360 return true;
Dave Martin734745c2015-03-04 12:27:33 +00001361}
1362
Dave Martin1e84d222015-04-27 16:49:05 +01001363static void pl011_tx_chars(struct uart_amba_port *uap, bool from_irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364{
Alan Coxebd2c8f2009-09-19 13:13:28 -07001365 struct circ_buf *xmit = &uap->port.state->xmit;
Dave Martin1e84d222015-04-27 16:49:05 +01001366 int count = uap->fifosize >> 1;
Dave Martin734745c2015-03-04 12:27:33 +00001367
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 if (uap->port.x_char) {
Dave Martin1e84d222015-04-27 16:49:05 +01001369 if (!pl011_tx_char(uap, uap->port.x_char, from_irq))
1370 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 uap->port.x_char = 0;
Dave Martin734745c2015-03-04 12:27:33 +00001372 --count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 }
1374 if (uart_circ_empty(xmit) || uart_tx_stopped(&uap->port)) {
Russell Kingb129a8c2005-08-31 10:12:14 +01001375 pl011_stop_tx(&uap->port);
Dave Martin1e84d222015-04-27 16:49:05 +01001376 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 }
1378
Russell King68b65f72010-12-22 17:24:39 +00001379 /* If we are using DMA mode, try to send some characters. */
1380 if (pl011_dma_tx_irq(uap))
Dave Martin1e84d222015-04-27 16:49:05 +01001381 return;
Russell King68b65f72010-12-22 17:24:39 +00001382
Dave Martin1e84d222015-04-27 16:49:05 +01001383 do {
1384 if (likely(from_irq) && count-- == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 break;
Dave Martin1e84d222015-04-27 16:49:05 +01001386
1387 if (!pl011_tx_char(uap, xmit->buf[xmit->tail], from_irq))
1388 break;
1389
1390 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
1391 } while (!uart_circ_empty(xmit));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392
1393 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
1394 uart_write_wakeup(&uap->port);
1395
Dave Martin1e84d222015-04-27 16:49:05 +01001396 if (uart_circ_empty(xmit))
Russell Kingb129a8c2005-08-31 10:12:14 +01001397 pl011_stop_tx(&uap->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398}
1399
1400static void pl011_modem_status(struct uart_amba_port *uap)
1401{
1402 unsigned int status, delta;
1403
Jun Nie7b753f32015-07-31 15:49:16 +08001404 status = pl011_readw(uap, REG_FR) & UART01x_FR_MODEM_ANY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405
1406 delta = status ^ uap->old_status;
1407 uap->old_status = status;
1408
1409 if (!delta)
1410 return;
1411
1412 if (delta & UART01x_FR_DCD)
1413 uart_handle_dcd_change(&uap->port, status & UART01x_FR_DCD);
1414
Greg Kroah-Hartman062a68a2015-09-04 09:11:24 -07001415 if (delta & UART01x_FR_DSR)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 uap->port.icount.dsr++;
1417
Greg Kroah-Hartman062a68a2015-09-04 09:11:24 -07001418 if (delta & UART01x_FR_CTS)
1419 uart_handle_cts_change(&uap->port, status & UART01x_FR_CTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420
Alan Coxbdc04e32009-09-19 13:13:31 -07001421 wake_up_interruptible(&uap->port.state->port.delta_msr_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422}
1423
Andre Przywara9c4ef4b2015-05-21 17:26:20 +01001424static void check_apply_cts_event_workaround(struct uart_amba_port *uap)
1425{
1426 unsigned int dummy_read;
1427
1428 if (!uap->vendor->cts_event_workaround)
1429 return;
1430
1431 /* workaround to make sure that all bits are unlocked.. */
Jun Nie7b753f32015-07-31 15:49:16 +08001432 pl011_writew(uap, 0x00, REG_ICR);
Andre Przywara9c4ef4b2015-05-21 17:26:20 +01001433
1434 /*
1435 * WA: introduce 26ns(1 uart clk) delay before W1C;
1436 * single apb access will incur 2 pclk(133.12Mhz) delay,
1437 * so add 2 dummy reads
1438 */
Jun Nie7b753f32015-07-31 15:49:16 +08001439 dummy_read = pl011_readw(uap, REG_ICR);
1440 dummy_read = pl011_readw(uap, REG_ICR);
Andre Przywara9c4ef4b2015-05-21 17:26:20 +01001441}
1442
David Howells7d12e782006-10-05 14:55:46 +01001443static irqreturn_t pl011_int(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444{
1445 struct uart_amba_port *uap = dev_id;
Russell King963cc982010-12-22 17:16:09 +00001446 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 unsigned int status, pass_counter = AMBA_ISR_PASS_LIMIT;
Andre Przywara075167e2015-05-21 17:26:19 +01001448 u16 imsc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 int handled = 0;
1450
Russell King963cc982010-12-22 17:16:09 +00001451 spin_lock_irqsave(&uap->port.lock, flags);
Jun Nie7b753f32015-07-31 15:49:16 +08001452 imsc = pl011_readw(uap, REG_IMSC);
1453 status = pl011_readw(uap, REG_RIS) & imsc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 if (status) {
1455 do {
Andre Przywara9c4ef4b2015-05-21 17:26:20 +01001456 check_apply_cts_event_workaround(uap);
Jun Nie7b753f32015-07-31 15:49:16 +08001457 pl011_writew(uap, status & ~(UART011_TXIS|UART011_RTIS|
1458 UART011_RXIS), REG_ICR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459
Linus Walleijead76f32011-02-24 13:21:08 +01001460 if (status & (UART011_RTIS|UART011_RXIS)) {
1461 if (pl011_dma_rx_running(uap))
1462 pl011_dma_rx_irq(uap);
1463 else
1464 pl011_rx_chars(uap);
1465 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 if (status & (UART011_DSRMIS|UART011_DCDMIS|
1467 UART011_CTSMIS|UART011_RIMIS))
1468 pl011_modem_status(uap);
Dave Martin1e84d222015-04-27 16:49:05 +01001469 if (status & UART011_TXIS)
1470 pl011_tx_chars(uap, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471
Rajanikanth H.V4fd06902012-03-26 11:17:02 +02001472 if (pass_counter-- == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 break;
1474
Jun Nie7b753f32015-07-31 15:49:16 +08001475 status = pl011_readw(uap, REG_RIS) & imsc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 } while (status != 0);
1477 handled = 1;
1478 }
1479
Russell King963cc982010-12-22 17:16:09 +00001480 spin_unlock_irqrestore(&uap->port.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481
1482 return IRQ_RETVAL(handled);
1483}
1484
Linus Walleije643f872012-06-17 15:44:19 +02001485static unsigned int pl011_tx_empty(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001487 struct uart_amba_port *uap =
1488 container_of(port, struct uart_amba_port, port);
Jun Nie7b753f32015-07-31 15:49:16 +08001489 unsigned int status = pl011_readw(uap, REG_FR);
Greg Kroah-Hartman062a68a2015-09-04 09:11:24 -07001490 return status & (UART01x_FR_BUSY|UART01x_FR_TXFF) ? 0 : TIOCSER_TEMT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491}
1492
Linus Walleije643f872012-06-17 15:44:19 +02001493static unsigned int pl011_get_mctrl(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001495 struct uart_amba_port *uap =
1496 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 unsigned int result = 0;
Jun Nie7b753f32015-07-31 15:49:16 +08001498 unsigned int status = pl011_readw(uap, REG_FR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499
Jiri Slaby5159f402007-10-18 23:40:31 -07001500#define TIOCMBIT(uartbit, tiocmbit) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 if (status & uartbit) \
1502 result |= tiocmbit
1503
Jiri Slaby5159f402007-10-18 23:40:31 -07001504 TIOCMBIT(UART01x_FR_DCD, TIOCM_CAR);
Greg Kroah-Hartman062a68a2015-09-04 09:11:24 -07001505 TIOCMBIT(UART01x_FR_DSR, TIOCM_DSR);
1506 TIOCMBIT(UART01x_FR_CTS, TIOCM_CTS);
1507 TIOCMBIT(UART011_FR_RI, TIOCM_RNG);
Jiri Slaby5159f402007-10-18 23:40:31 -07001508#undef TIOCMBIT
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 return result;
1510}
1511
1512static void pl011_set_mctrl(struct uart_port *port, unsigned int mctrl)
1513{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001514 struct uart_amba_port *uap =
1515 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 unsigned int cr;
1517
Jun Nie7b753f32015-07-31 15:49:16 +08001518 cr = pl011_readw(uap, REG_CR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519
Jiri Slaby5159f402007-10-18 23:40:31 -07001520#define TIOCMBIT(tiocmbit, uartbit) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 if (mctrl & tiocmbit) \
1522 cr |= uartbit; \
1523 else \
1524 cr &= ~uartbit
1525
Jiri Slaby5159f402007-10-18 23:40:31 -07001526 TIOCMBIT(TIOCM_RTS, UART011_CR_RTS);
1527 TIOCMBIT(TIOCM_DTR, UART011_CR_DTR);
1528 TIOCMBIT(TIOCM_OUT1, UART011_CR_OUT1);
1529 TIOCMBIT(TIOCM_OUT2, UART011_CR_OUT2);
1530 TIOCMBIT(TIOCM_LOOP, UART011_CR_LBE);
Rabin Vincent3b438162010-02-12 06:43:11 +01001531
1532 if (uap->autorts) {
1533 /* We need to disable auto-RTS if we want to turn RTS off */
1534 TIOCMBIT(TIOCM_RTS, UART011_CR_RTSEN);
1535 }
Jiri Slaby5159f402007-10-18 23:40:31 -07001536#undef TIOCMBIT
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537
Jun Nie7b753f32015-07-31 15:49:16 +08001538 pl011_writew(uap, cr, REG_CR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539}
1540
1541static void pl011_break_ctl(struct uart_port *port, int break_state)
1542{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001543 struct uart_amba_port *uap =
1544 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 unsigned long flags;
1546 unsigned int lcr_h;
1547
1548 spin_lock_irqsave(&uap->port.lock, flags);
Jun Nie7b753f32015-07-31 15:49:16 +08001549 lcr_h = pl011_readw(uap, uap->lcrh_tx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 if (break_state == -1)
1551 lcr_h |= UART01x_LCRH_BRK;
1552 else
1553 lcr_h &= ~UART01x_LCRH_BRK;
Jun Nie7b753f32015-07-31 15:49:16 +08001554 pl011_writew(uap, lcr_h, uap->lcrh_tx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 spin_unlock_irqrestore(&uap->port.lock, flags);
1556}
1557
Jason Wessel84b5ae12008-02-20 13:33:39 -06001558#ifdef CONFIG_CONSOLE_POLL
Anton Vorontsov5c8124a2012-09-24 14:27:55 -07001559
1560static void pl011_quiesce_irqs(struct uart_port *port)
1561{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001562 struct uart_amba_port *uap =
1563 container_of(port, struct uart_amba_port, port);
Anton Vorontsov5c8124a2012-09-24 14:27:55 -07001564
Jun Nie7b753f32015-07-31 15:49:16 +08001565 pl011_writew(uap, pl011_readw(uap, REG_MIS), REG_ICR);
Anton Vorontsov5c8124a2012-09-24 14:27:55 -07001566 /*
1567 * There is no way to clear TXIM as this is "ready to transmit IRQ", so
1568 * we simply mask it. start_tx() will unmask it.
1569 *
1570 * Note we can race with start_tx(), and if the race happens, the
1571 * polling user might get another interrupt just after we clear it.
1572 * But it should be OK and can happen even w/o the race, e.g.
1573 * controller immediately got some new data and raised the IRQ.
1574 *
1575 * And whoever uses polling routines assumes that it manages the device
1576 * (including tx queue), so we're also fine with start_tx()'s caller
1577 * side.
1578 */
Jun Nie7b753f32015-07-31 15:49:16 +08001579 pl011_writew(uap, pl011_readw(uap, REG_IMSC) & ~UART011_TXIM, REG_IMSC);
Anton Vorontsov5c8124a2012-09-24 14:27:55 -07001580}
1581
Linus Walleije643f872012-06-17 15:44:19 +02001582static int pl011_get_poll_char(struct uart_port *port)
Jason Wessel84b5ae12008-02-20 13:33:39 -06001583{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001584 struct uart_amba_port *uap =
1585 container_of(port, struct uart_amba_port, port);
Jason Wessel84b5ae12008-02-20 13:33:39 -06001586 unsigned int status;
1587
Anton Vorontsov5c8124a2012-09-24 14:27:55 -07001588 /*
1589 * The caller might need IRQs lowered, e.g. if used with KDB NMI
1590 * debugger.
1591 */
1592 pl011_quiesce_irqs(port);
1593
Jun Nie7b753f32015-07-31 15:49:16 +08001594 status = pl011_readw(uap, REG_FR);
Jason Wesself5316b42010-05-20 21:04:22 -05001595 if (status & UART01x_FR_RXFE)
1596 return NO_POLL_CHAR;
Jason Wessel84b5ae12008-02-20 13:33:39 -06001597
Jun Nie7b753f32015-07-31 15:49:16 +08001598 return pl011_readw(uap, REG_DR);
Jason Wessel84b5ae12008-02-20 13:33:39 -06001599}
1600
Linus Walleije643f872012-06-17 15:44:19 +02001601static void pl011_put_poll_char(struct uart_port *port,
Jason Wessel84b5ae12008-02-20 13:33:39 -06001602 unsigned char ch)
1603{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001604 struct uart_amba_port *uap =
1605 container_of(port, struct uart_amba_port, port);
Jason Wessel84b5ae12008-02-20 13:33:39 -06001606
Jun Nie7b753f32015-07-31 15:49:16 +08001607 while (pl011_readw(uap, REG_FR) & UART01x_FR_TXFF)
Jason Wessel84b5ae12008-02-20 13:33:39 -06001608 barrier();
1609
Jun Nie7b753f32015-07-31 15:49:16 +08001610 pl011_writew(uap, ch, REG_DR);
Jason Wessel84b5ae12008-02-20 13:33:39 -06001611}
1612
1613#endif /* CONFIG_CONSOLE_POLL */
1614
Anton Vorontsovb3564c22012-09-24 14:27:54 -07001615static int pl011_hwinit(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001617 struct uart_amba_port *uap =
1618 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 int retval;
1620
Linus Walleij78d80c52012-05-23 21:18:46 +02001621 /* Optionaly enable pins to be muxed in and configured */
Linus Walleij2b996fc2013-06-05 15:36:42 +02001622 pinctrl_pm_select_default_state(port->dev);
Linus Walleij78d80c52012-05-23 21:18:46 +02001623
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 /*
1625 * Try to enable the clock producer.
1626 */
Julia Lawall1c4c4392012-08-26 18:01:01 +02001627 retval = clk_prepare_enable(uap->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 if (retval)
Tushar Behera7f6d9422014-06-26 15:35:35 +05301629 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630
1631 uap->port.uartclk = clk_get_rate(uap->clk);
1632
Linus Walleij9b96fba2012-03-13 13:27:23 +01001633 /* Clear pending error and receive interrupts */
Jun Nie7b753f32015-07-31 15:49:16 +08001634 pl011_writew(uap, UART011_OEIS | UART011_BEIS | UART011_PEIS |
1635 UART011_FEIS | UART011_RTIS | UART011_RXIS, REG_ICR);
Linus Walleij9b96fba2012-03-13 13:27:23 +01001636
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 /*
Anton Vorontsovb3564c22012-09-24 14:27:54 -07001638 * Save interrupts enable mask, and enable RX interrupts in case if
1639 * the interrupt is used for NMI entry.
1640 */
Jun Nie7b753f32015-07-31 15:49:16 +08001641 uap->im = pl011_readw(uap, REG_IMSC);
1642 pl011_writew(uap, UART011_RTIM | UART011_RXIM, REG_IMSC);
Anton Vorontsovb3564c22012-09-24 14:27:54 -07001643
Jingoo Han574de552013-07-30 17:06:57 +09001644 if (dev_get_platdata(uap->port.dev)) {
Anton Vorontsovb3564c22012-09-24 14:27:54 -07001645 struct amba_pl011_data *plat;
1646
Jingoo Han574de552013-07-30 17:06:57 +09001647 plat = dev_get_platdata(uap->port.dev);
Anton Vorontsovb3564c22012-09-24 14:27:54 -07001648 if (plat->init)
1649 plat->init();
1650 }
1651 return 0;
Anton Vorontsovb3564c22012-09-24 14:27:54 -07001652}
1653
Jon Medhurstb60f2f62013-12-10 10:18:59 +00001654static void pl011_write_lcr_h(struct uart_amba_port *uap, unsigned int lcr_h)
1655{
Jun Nie7b753f32015-07-31 15:49:16 +08001656 pl011_writew(uap, lcr_h, uap->lcrh_rx);
Jun Nie09dcc7d2015-07-31 15:49:18 +08001657 if (is_implemented(uap, REG_ST_LCRH_RX)) {
Jon Medhurstb60f2f62013-12-10 10:18:59 +00001658 int i;
1659 /*
1660 * Wait 10 PCLKs before writing LCRH_TX register,
1661 * to get this delay write read only register 10 times
1662 */
1663 for (i = 0; i < 10; ++i)
Jun Nie7b753f32015-07-31 15:49:16 +08001664 pl011_writew(uap, 0xff, REG_MIS);
1665 pl011_writew(uap, lcr_h, uap->lcrh_tx);
Jon Medhurstb60f2f62013-12-10 10:18:59 +00001666 }
1667}
1668
Andre Przywara867b8e82015-05-21 17:26:15 +01001669static int pl011_allocate_irq(struct uart_amba_port *uap)
1670{
Jun Nie7b753f32015-07-31 15:49:16 +08001671 pl011_writew(uap, uap->im, REG_IMSC);
Andre Przywara867b8e82015-05-21 17:26:15 +01001672
1673 return request_irq(uap->port.irq, pl011_int, 0, "uart-pl011", uap);
1674}
1675
1676/*
1677 * Enable interrupts, only timeouts when using DMA
1678 * if initial RX DMA job failed, start in interrupt mode
1679 * as well.
1680 */
1681static void pl011_enable_interrupts(struct uart_amba_port *uap)
1682{
1683 spin_lock_irq(&uap->port.lock);
1684
1685 /* Clear out any spuriously appearing RX interrupts */
Jun Nie7b753f32015-07-31 15:49:16 +08001686 pl011_writew(uap, UART011_RTIS | UART011_RXIS, REG_ICR);
Andre Przywara867b8e82015-05-21 17:26:15 +01001687 uap->im = UART011_RTIM;
1688 if (!pl011_dma_rx_running(uap))
1689 uap->im |= UART011_RXIM;
Jun Nie7b753f32015-07-31 15:49:16 +08001690 pl011_writew(uap, uap->im, REG_IMSC);
Andre Przywara867b8e82015-05-21 17:26:15 +01001691 spin_unlock_irq(&uap->port.lock);
1692}
1693
Anton Vorontsovb3564c22012-09-24 14:27:54 -07001694static int pl011_startup(struct uart_port *port)
1695{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001696 struct uart_amba_port *uap =
1697 container_of(port, struct uart_amba_port, port);
Dave Martin734745c2015-03-04 12:27:33 +00001698 unsigned int cr;
Anton Vorontsovb3564c22012-09-24 14:27:54 -07001699 int retval;
1700
1701 retval = pl011_hwinit(port);
1702 if (retval)
1703 goto clk_dis;
1704
Andre Przywara867b8e82015-05-21 17:26:15 +01001705 retval = pl011_allocate_irq(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 if (retval)
1707 goto clk_dis;
1708
Jun Nie7b753f32015-07-31 15:49:16 +08001709 pl011_writew(uap, uap->vendor->ifls, REG_IFLS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710
Jon Medhurstfe433902013-12-10 10:18:58 +00001711 spin_lock_irq(&uap->port.lock);
1712
Shreshtha Kumar Sahud8d8ffa2012-01-18 15:53:59 +05301713 /* restore RTS and DTR */
1714 cr = uap->old_cr & (UART011_CR_RTS | UART011_CR_DTR);
1715 cr |= UART01x_CR_UARTEN | UART011_CR_RXE | UART011_CR_TXE;
Jun Nie7b753f32015-07-31 15:49:16 +08001716 pl011_writew(uap, cr, REG_CR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717
Jon Medhurstfe433902013-12-10 10:18:58 +00001718 spin_unlock_irq(&uap->port.lock);
1719
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 /*
1721 * initialise the old status of the modem signals
1722 */
Greg Kroah-Hartman062a68a2015-09-04 09:11:24 -07001723 uap->old_status = pl011_readw(uap, REG_FR) &
1724 UART01x_FR_MODEM_ANY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725
Russell King68b65f72010-12-22 17:24:39 +00001726 /* Startup DMA */
1727 pl011_dma_startup(uap);
1728
Andre Przywara867b8e82015-05-21 17:26:15 +01001729 pl011_enable_interrupts(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730
1731 return 0;
1732
1733 clk_dis:
Julia Lawall1c4c4392012-08-26 18:01:01 +02001734 clk_disable_unprepare(uap->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 return retval;
1736}
1737
Andre Przywara0dd1e242015-05-21 17:26:23 +01001738static int sbsa_uart_startup(struct uart_port *port)
1739{
1740 struct uart_amba_port *uap =
1741 container_of(port, struct uart_amba_port, port);
1742 int retval;
1743
1744 retval = pl011_hwinit(port);
1745 if (retval)
1746 return retval;
1747
1748 retval = pl011_allocate_irq(uap);
1749 if (retval)
1750 return retval;
1751
1752 /* The SBSA UART does not support any modem status lines. */
1753 uap->old_status = 0;
1754
1755 pl011_enable_interrupts(uap);
1756
1757 return 0;
1758}
1759
Linus Walleijec489aa2010-06-02 08:13:52 +01001760static void pl011_shutdown_channel(struct uart_amba_port *uap,
1761 unsigned int lcrh)
1762{
Jun Nie7b753f32015-07-31 15:49:16 +08001763 unsigned long val;
Linus Walleijec489aa2010-06-02 08:13:52 +01001764
Jun Nie7b753f32015-07-31 15:49:16 +08001765 val = pl011_readw(uap, lcrh);
1766 val &= ~(UART01x_LCRH_BRK | UART01x_LCRH_FEN);
1767 pl011_writew(uap, val, lcrh);
Linus Walleijec489aa2010-06-02 08:13:52 +01001768}
1769
Andre Przywara95166a32015-05-21 17:26:16 +01001770/*
1771 * disable the port. It should not disable RTS and DTR.
1772 * Also RTS and DTR state should be preserved to restore
1773 * it during startup().
1774 */
1775static void pl011_disable_uart(struct uart_amba_port *uap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776{
Shreshtha Kumar Sahud8d8ffa2012-01-18 15:53:59 +05301777 unsigned int cr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778
Rabin Vincent3b438162010-02-12 06:43:11 +01001779 uap->autorts = false;
Jon Medhurstfe433902013-12-10 10:18:58 +00001780 spin_lock_irq(&uap->port.lock);
Jun Nie7b753f32015-07-31 15:49:16 +08001781 cr = pl011_readw(uap, REG_CR);
Shreshtha Kumar Sahud8d8ffa2012-01-18 15:53:59 +05301782 uap->old_cr = cr;
1783 cr &= UART011_CR_RTS | UART011_CR_DTR;
1784 cr |= UART01x_CR_UARTEN | UART011_CR_TXE;
Jun Nie7b753f32015-07-31 15:49:16 +08001785 pl011_writew(uap, cr, REG_CR);
Jon Medhurstfe433902013-12-10 10:18:58 +00001786 spin_unlock_irq(&uap->port.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787
1788 /*
1789 * disable break condition and fifos
1790 */
Linus Walleijec489aa2010-06-02 08:13:52 +01001791 pl011_shutdown_channel(uap, uap->lcrh_rx);
Jun Nie09dcc7d2015-07-31 15:49:18 +08001792 if (is_implemented(uap, REG_ST_LCRH_RX))
Linus Walleijec489aa2010-06-02 08:13:52 +01001793 pl011_shutdown_channel(uap, uap->lcrh_tx);
Andre Przywara95166a32015-05-21 17:26:16 +01001794}
1795
1796static void pl011_disable_interrupts(struct uart_amba_port *uap)
1797{
1798 spin_lock_irq(&uap->port.lock);
1799
1800 /* mask all interrupts and clear all pending ones */
1801 uap->im = 0;
Jun Nie7b753f32015-07-31 15:49:16 +08001802 pl011_writew(uap, uap->im, REG_IMSC);
Greg Kroah-Hartman062a68a2015-09-04 09:11:24 -07001803 pl011_writew(0xffff, REG_ICR);
Andre Przywara95166a32015-05-21 17:26:16 +01001804
1805 spin_unlock_irq(&uap->port.lock);
1806}
1807
1808static void pl011_shutdown(struct uart_port *port)
1809{
1810 struct uart_amba_port *uap =
1811 container_of(port, struct uart_amba_port, port);
1812
1813 pl011_disable_interrupts(uap);
1814
1815 pl011_dma_shutdown(uap);
1816
1817 free_irq(uap->port.irq, uap);
1818
1819 pl011_disable_uart(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820
1821 /*
1822 * Shut down the clock producer
1823 */
Julia Lawall1c4c4392012-08-26 18:01:01 +02001824 clk_disable_unprepare(uap->clk);
Linus Walleij78d80c52012-05-23 21:18:46 +02001825 /* Optionally let pins go into sleep states */
Linus Walleij2b996fc2013-06-05 15:36:42 +02001826 pinctrl_pm_select_sleep_state(port->dev);
Shreshtha Kumar Sahuc16d51a2011-06-13 10:11:33 +02001827
Jingoo Han574de552013-07-30 17:06:57 +09001828 if (dev_get_platdata(uap->port.dev)) {
Shreshtha Kumar Sahuc16d51a2011-06-13 10:11:33 +02001829 struct amba_pl011_data *plat;
1830
Jingoo Han574de552013-07-30 17:06:57 +09001831 plat = dev_get_platdata(uap->port.dev);
Shreshtha Kumar Sahuc16d51a2011-06-13 10:11:33 +02001832 if (plat->exit)
1833 plat->exit();
1834 }
1835
Peter Hurley36f339d2014-11-06 09:06:12 -05001836 if (uap->port.ops->flush_buffer)
1837 uap->port.ops->flush_buffer(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838}
1839
Andre Przywara0dd1e242015-05-21 17:26:23 +01001840static void sbsa_uart_shutdown(struct uart_port *port)
1841{
1842 struct uart_amba_port *uap =
1843 container_of(port, struct uart_amba_port, port);
1844
1845 pl011_disable_interrupts(uap);
1846
1847 free_irq(uap->port.irq, uap);
1848
1849 if (uap->port.ops->flush_buffer)
1850 uap->port.ops->flush_buffer(port);
1851}
1852
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853static void
Andre Przywaraef5a9352015-05-21 17:26:17 +01001854pl011_setup_status_masks(struct uart_port *port, struct ktermios *termios)
1855{
1856 port->read_status_mask = UART011_DR_OE | 255;
1857 if (termios->c_iflag & INPCK)
1858 port->read_status_mask |= UART011_DR_FE | UART011_DR_PE;
1859 if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
1860 port->read_status_mask |= UART011_DR_BE;
1861
1862 /*
1863 * Characters to ignore
1864 */
1865 port->ignore_status_mask = 0;
1866 if (termios->c_iflag & IGNPAR)
1867 port->ignore_status_mask |= UART011_DR_FE | UART011_DR_PE;
1868 if (termios->c_iflag & IGNBRK) {
1869 port->ignore_status_mask |= UART011_DR_BE;
1870 /*
1871 * If we're ignoring parity and break indicators,
1872 * ignore overruns too (for real raw support).
1873 */
1874 if (termios->c_iflag & IGNPAR)
1875 port->ignore_status_mask |= UART011_DR_OE;
1876 }
1877
1878 /*
1879 * Ignore all characters if CREAD is not set.
1880 */
1881 if ((termios->c_cflag & CREAD) == 0)
1882 port->ignore_status_mask |= UART_DUMMY_DR_RX;
1883}
1884
1885static void
Alan Cox606d0992006-12-08 02:38:45 -08001886pl011_set_termios(struct uart_port *port, struct ktermios *termios,
1887 struct ktermios *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001889 struct uart_amba_port *uap =
1890 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 unsigned int lcr_h, old_cr;
1892 unsigned long flags;
Russell Kingc19f12b2010-12-22 17:48:26 +00001893 unsigned int baud, quot, clkdiv;
1894
1895 if (uap->vendor->oversampling)
1896 clkdiv = 8;
1897 else
1898 clkdiv = 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899
1900 /*
1901 * Ask the core to calculate the divisor for us.
1902 */
Linus Walleijac3e3fb2010-06-02 20:40:22 +01001903 baud = uart_get_baud_rate(port, termios, old, 0,
Russell Kingc19f12b2010-12-22 17:48:26 +00001904 port->uartclk / clkdiv);
Chanho Min89fa28d2013-04-03 11:10:37 +09001905#ifdef CONFIG_DMA_ENGINE
Chanho Mincb06ff12013-03-27 18:38:11 +09001906 /*
1907 * Adjust RX DMA polling rate with baud rate if not specified.
1908 */
1909 if (uap->dmarx.auto_poll_rate)
1910 uap->dmarx.poll_rate = DIV_ROUND_UP(10000000, baud);
Chanho Min89fa28d2013-04-03 11:10:37 +09001911#endif
Linus Walleijac3e3fb2010-06-02 20:40:22 +01001912
1913 if (baud > port->uartclk/16)
1914 quot = DIV_ROUND_CLOSEST(port->uartclk * 8, baud);
1915 else
1916 quot = DIV_ROUND_CLOSEST(port->uartclk * 4, baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917
1918 switch (termios->c_cflag & CSIZE) {
1919 case CS5:
1920 lcr_h = UART01x_LCRH_WLEN_5;
1921 break;
1922 case CS6:
1923 lcr_h = UART01x_LCRH_WLEN_6;
1924 break;
1925 case CS7:
1926 lcr_h = UART01x_LCRH_WLEN_7;
1927 break;
1928 default: // CS8
1929 lcr_h = UART01x_LCRH_WLEN_8;
1930 break;
1931 }
1932 if (termios->c_cflag & CSTOPB)
1933 lcr_h |= UART01x_LCRH_STP2;
1934 if (termios->c_cflag & PARENB) {
1935 lcr_h |= UART01x_LCRH_PEN;
1936 if (!(termios->c_cflag & PARODD))
1937 lcr_h |= UART01x_LCRH_EPS;
1938 }
Russell Kingffca2b12010-12-22 17:13:05 +00001939 if (uap->fifosize > 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 lcr_h |= UART01x_LCRH_FEN;
1941
1942 spin_lock_irqsave(&port->lock, flags);
1943
1944 /*
1945 * Update the per-port timeout.
1946 */
1947 uart_update_timeout(port, termios->c_cflag, baud);
1948
Andre Przywaraef5a9352015-05-21 17:26:17 +01001949 pl011_setup_status_masks(port, termios);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950
1951 if (UART_ENABLE_MS(port, termios->c_cflag))
1952 pl011_enable_ms(port);
1953
1954 /* first, disable everything */
Jun Nie7b753f32015-07-31 15:49:16 +08001955 old_cr = pl011_readw(uap, REG_CR);
1956 pl011_writew(uap, 0, REG_CR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957
Rabin Vincent3b438162010-02-12 06:43:11 +01001958 if (termios->c_cflag & CRTSCTS) {
1959 if (old_cr & UART011_CR_RTS)
1960 old_cr |= UART011_CR_RTSEN;
1961
1962 old_cr |= UART011_CR_CTSEN;
1963 uap->autorts = true;
1964 } else {
1965 old_cr &= ~(UART011_CR_CTSEN | UART011_CR_RTSEN);
1966 uap->autorts = false;
1967 }
1968
Russell Kingc19f12b2010-12-22 17:48:26 +00001969 if (uap->vendor->oversampling) {
1970 if (baud > port->uartclk / 16)
Linus Walleijac3e3fb2010-06-02 20:40:22 +01001971 old_cr |= ST_UART011_CR_OVSFACT;
1972 else
1973 old_cr &= ~ST_UART011_CR_OVSFACT;
1974 }
1975
Linus Walleijc5dd5532012-09-26 17:21:36 +02001976 /*
1977 * Workaround for the ST Micro oversampling variants to
1978 * increase the bitrate slightly, by lowering the divisor,
1979 * to avoid delayed sampling of start bit at high speeds,
1980 * else we see data corruption.
1981 */
1982 if (uap->vendor->oversampling) {
1983 if ((baud >= 3000000) && (baud < 3250000) && (quot > 1))
1984 quot -= 1;
1985 else if ((baud > 3250000) && (quot > 2))
1986 quot -= 2;
1987 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 /* Set baud rate */
Jun Nie7b753f32015-07-31 15:49:16 +08001989 pl011_writew(uap, quot & 0x3f, REG_FBRD);
1990 pl011_writew(uap, quot >> 6, REG_IBRD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991
1992 /*
1993 * ----------v----------v----------v----------v-----
Linus Walleijc5dd5532012-09-26 17:21:36 +02001994 * NOTE: lcrh_tx and lcrh_rx MUST BE WRITTEN AFTER
Jun Nie534e14e2015-07-31 15:49:15 +08001995 * REG_FBRD & REG_IBRD.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 * ----------^----------^----------^----------^-----
1997 */
Jon Medhurstb60f2f62013-12-10 10:18:59 +00001998 pl011_write_lcr_h(uap, lcr_h);
Jun Nie7b753f32015-07-31 15:49:16 +08001999 pl011_writew(uap, old_cr, REG_CR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000
2001 spin_unlock_irqrestore(&port->lock, flags);
2002}
2003
Andre Przywara0dd1e242015-05-21 17:26:23 +01002004static void
2005sbsa_uart_set_termios(struct uart_port *port, struct ktermios *termios,
2006 struct ktermios *old)
2007{
2008 struct uart_amba_port *uap =
2009 container_of(port, struct uart_amba_port, port);
2010 unsigned long flags;
2011
2012 tty_termios_encode_baud_rate(termios, uap->fixed_baud, uap->fixed_baud);
2013
2014 /* The SBSA UART only supports 8n1 without hardware flow control. */
2015 termios->c_cflag &= ~(CSIZE | CSTOPB | PARENB | PARODD);
2016 termios->c_cflag &= ~(CMSPAR | CRTSCTS);
2017 termios->c_cflag |= CS8 | CLOCAL;
2018
2019 spin_lock_irqsave(&port->lock, flags);
2020 uart_update_timeout(port, CS8, uap->fixed_baud);
2021 pl011_setup_status_masks(port, termios);
2022 spin_unlock_irqrestore(&port->lock, flags);
2023}
2024
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025static const char *pl011_type(struct uart_port *port)
2026{
Daniel Thompsona5820c22014-09-03 12:51:55 +01002027 struct uart_amba_port *uap =
2028 container_of(port, struct uart_amba_port, port);
Russell Kinge8a7ba82010-12-28 09:16:54 +00002029 return uap->port.type == PORT_AMBA ? uap->type : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030}
2031
2032/*
2033 * Release the memory region(s) being used by 'port'
2034 */
Linus Walleije643f872012-06-17 15:44:19 +02002035static void pl011_release_port(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036{
2037 release_mem_region(port->mapbase, SZ_4K);
2038}
2039
2040/*
2041 * Request the memory region(s) being used by 'port'
2042 */
Linus Walleije643f872012-06-17 15:44:19 +02002043static int pl011_request_port(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044{
2045 return request_mem_region(port->mapbase, SZ_4K, "uart-pl011")
2046 != NULL ? 0 : -EBUSY;
2047}
2048
2049/*
2050 * Configure/autoconfigure the port.
2051 */
Linus Walleije643f872012-06-17 15:44:19 +02002052static void pl011_config_port(struct uart_port *port, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053{
2054 if (flags & UART_CONFIG_TYPE) {
2055 port->type = PORT_AMBA;
Linus Walleije643f872012-06-17 15:44:19 +02002056 pl011_request_port(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 }
2058}
2059
2060/*
2061 * verify the new serial_struct (for TIOCSSERIAL).
2062 */
Linus Walleije643f872012-06-17 15:44:19 +02002063static int pl011_verify_port(struct uart_port *port, struct serial_struct *ser)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064{
2065 int ret = 0;
2066 if (ser->type != PORT_UNKNOWN && ser->type != PORT_AMBA)
2067 ret = -EINVAL;
Yinghai Lua62c4132008-08-19 20:49:55 -07002068 if (ser->irq < 0 || ser->irq >= nr_irqs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 ret = -EINVAL;
2070 if (ser->baud_base < 9600)
2071 ret = -EINVAL;
2072 return ret;
2073}
2074
2075static struct uart_ops amba_pl011_pops = {
Linus Walleije643f872012-06-17 15:44:19 +02002076 .tx_empty = pl011_tx_empty,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 .set_mctrl = pl011_set_mctrl,
Linus Walleije643f872012-06-17 15:44:19 +02002078 .get_mctrl = pl011_get_mctrl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 .stop_tx = pl011_stop_tx,
2080 .start_tx = pl011_start_tx,
2081 .stop_rx = pl011_stop_rx,
2082 .enable_ms = pl011_enable_ms,
2083 .break_ctl = pl011_break_ctl,
2084 .startup = pl011_startup,
2085 .shutdown = pl011_shutdown,
Russell King68b65f72010-12-22 17:24:39 +00002086 .flush_buffer = pl011_dma_flush_buffer,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 .set_termios = pl011_set_termios,
2088 .type = pl011_type,
Linus Walleije643f872012-06-17 15:44:19 +02002089 .release_port = pl011_release_port,
2090 .request_port = pl011_request_port,
2091 .config_port = pl011_config_port,
2092 .verify_port = pl011_verify_port,
Jason Wessel84b5ae12008-02-20 13:33:39 -06002093#ifdef CONFIG_CONSOLE_POLL
Anton Vorontsovb3564c22012-09-24 14:27:54 -07002094 .poll_init = pl011_hwinit,
Linus Walleije643f872012-06-17 15:44:19 +02002095 .poll_get_char = pl011_get_poll_char,
2096 .poll_put_char = pl011_put_poll_char,
Jason Wessel84b5ae12008-02-20 13:33:39 -06002097#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098};
2099
Andre Przywara0dd1e242015-05-21 17:26:23 +01002100static void sbsa_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
2101{
2102}
2103
2104static unsigned int sbsa_uart_get_mctrl(struct uart_port *port)
2105{
2106 return 0;
2107}
2108
2109static const struct uart_ops sbsa_uart_pops = {
2110 .tx_empty = pl011_tx_empty,
2111 .set_mctrl = sbsa_uart_set_mctrl,
2112 .get_mctrl = sbsa_uart_get_mctrl,
2113 .stop_tx = pl011_stop_tx,
2114 .start_tx = pl011_start_tx,
2115 .stop_rx = pl011_stop_rx,
2116 .startup = sbsa_uart_startup,
2117 .shutdown = sbsa_uart_shutdown,
2118 .set_termios = sbsa_uart_set_termios,
2119 .type = pl011_type,
2120 .release_port = pl011_release_port,
2121 .request_port = pl011_request_port,
2122 .config_port = pl011_config_port,
2123 .verify_port = pl011_verify_port,
2124#ifdef CONFIG_CONSOLE_POLL
2125 .poll_init = pl011_hwinit,
2126 .poll_get_char = pl011_get_poll_char,
2127 .poll_put_char = pl011_put_poll_char,
2128#endif
2129};
2130
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131static struct uart_amba_port *amba_ports[UART_NR];
2132
2133#ifdef CONFIG_SERIAL_AMBA_PL011_CONSOLE
2134
Russell Kingd3587882006-03-20 20:00:09 +00002135static void pl011_console_putchar(struct uart_port *port, int ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136{
Daniel Thompsona5820c22014-09-03 12:51:55 +01002137 struct uart_amba_port *uap =
2138 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139
Jun Nie7b753f32015-07-31 15:49:16 +08002140 while (pl011_readw(uap, REG_FR) & UART01x_FR_TXFF)
Russell Kingd3587882006-03-20 20:00:09 +00002141 barrier();
Jun Nie7b753f32015-07-31 15:49:16 +08002142 pl011_writew(uap, ch, REG_DR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143}
2144
2145static void
2146pl011_console_write(struct console *co, const char *s, unsigned int count)
2147{
2148 struct uart_amba_port *uap = amba_ports[co->index];
Andre Przywara71eec482015-05-21 17:26:21 +01002149 unsigned int status, old_cr = 0, new_cr;
Rabin Vincentef605fd2012-01-17 11:52:28 +01002150 unsigned long flags;
2151 int locked = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152
2153 clk_enable(uap->clk);
2154
Rabin Vincentef605fd2012-01-17 11:52:28 +01002155 local_irq_save(flags);
2156 if (uap->port.sysrq)
2157 locked = 0;
2158 else if (oops_in_progress)
2159 locked = spin_trylock(&uap->port.lock);
2160 else
2161 spin_lock(&uap->port.lock);
2162
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 /*
2164 * First save the CR then disable the interrupts
2165 */
Andre Przywara71eec482015-05-21 17:26:21 +01002166 if (!uap->vendor->always_enabled) {
Jun Nie7b753f32015-07-31 15:49:16 +08002167 old_cr = pl011_readw(uap, REG_CR);
Andre Przywara71eec482015-05-21 17:26:21 +01002168 new_cr = old_cr & ~UART011_CR_CTSEN;
2169 new_cr |= UART01x_CR_UARTEN | UART011_CR_TXE;
Jun Nie7b753f32015-07-31 15:49:16 +08002170 pl011_writew(uap, new_cr, REG_CR);
Andre Przywara71eec482015-05-21 17:26:21 +01002171 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172
Russell Kingd3587882006-03-20 20:00:09 +00002173 uart_console_write(&uap->port, s, count, pl011_console_putchar);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174
2175 /*
2176 * Finally, wait for transmitter to become empty
2177 * and restore the TCR
2178 */
2179 do {
Jun Nie7b753f32015-07-31 15:49:16 +08002180 status = pl011_readw(uap, REG_FR);
Greg Kroah-Hartman062a68a2015-09-04 09:11:24 -07002181 } while (status & UART01x_FR_BUSY);
Andre Przywara71eec482015-05-21 17:26:21 +01002182 if (!uap->vendor->always_enabled)
Jun Nie7b753f32015-07-31 15:49:16 +08002183 pl011_writew(uap, old_cr, REG_CR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184
Rabin Vincentef605fd2012-01-17 11:52:28 +01002185 if (locked)
2186 spin_unlock(&uap->port.lock);
2187 local_irq_restore(flags);
2188
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 clk_disable(uap->clk);
2190}
2191
2192static void __init
2193pl011_console_get_options(struct uart_amba_port *uap, int *baud,
2194 int *parity, int *bits)
2195{
Jun Nie7b753f32015-07-31 15:49:16 +08002196 if (pl011_readw(uap, REG_CR) & UART01x_CR_UARTEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 unsigned int lcr_h, ibrd, fbrd;
2198
Jun Nie7b753f32015-07-31 15:49:16 +08002199 lcr_h = pl011_readw(uap, uap->lcrh_tx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200
2201 *parity = 'n';
2202 if (lcr_h & UART01x_LCRH_PEN) {
2203 if (lcr_h & UART01x_LCRH_EPS)
2204 *parity = 'e';
2205 else
2206 *parity = 'o';
2207 }
2208
2209 if ((lcr_h & 0x60) == UART01x_LCRH_WLEN_7)
2210 *bits = 7;
2211 else
2212 *bits = 8;
2213
Jun Nie7b753f32015-07-31 15:49:16 +08002214 ibrd = pl011_readw(uap, REG_IBRD);
2215 fbrd = pl011_readw(uap, REG_FBRD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216
2217 *baud = uap->port.uartclk * 4 / (64 * ibrd + fbrd);
Linus Walleijac3e3fb2010-06-02 20:40:22 +01002218
Russell Kingc19f12b2010-12-22 17:48:26 +00002219 if (uap->vendor->oversampling) {
Jun Nie7b753f32015-07-31 15:49:16 +08002220 if (pl011_readw(uap, REG_CR)
Linus Walleijac3e3fb2010-06-02 20:40:22 +01002221 & ST_UART011_CR_OVSFACT)
2222 *baud *= 2;
2223 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 }
2225}
2226
2227static int __init pl011_console_setup(struct console *co, char *options)
2228{
2229 struct uart_amba_port *uap;
2230 int baud = 38400;
2231 int bits = 8;
2232 int parity = 'n';
2233 int flow = 'n';
Russell King4b4851c2011-09-22 11:35:30 +01002234 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235
2236 /*
2237 * Check whether an invalid uart number has been specified, and
2238 * if so, search for the first available port that does have
2239 * console support.
2240 */
2241 if (co->index >= UART_NR)
2242 co->index = 0;
2243 uap = amba_ports[co->index];
Russell Kingd28122a2007-01-22 18:59:42 +00002244 if (!uap)
2245 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246
Linus Walleij78d80c52012-05-23 21:18:46 +02002247 /* Allow pins to be muxed in and configured */
Linus Walleij2b996fc2013-06-05 15:36:42 +02002248 pinctrl_pm_select_default_state(uap->port.dev);
Linus Walleij78d80c52012-05-23 21:18:46 +02002249
Russell King4b4851c2011-09-22 11:35:30 +01002250 ret = clk_prepare(uap->clk);
2251 if (ret)
2252 return ret;
2253
Jingoo Han574de552013-07-30 17:06:57 +09002254 if (dev_get_platdata(uap->port.dev)) {
Shreshtha Kumar Sahuc16d51a2011-06-13 10:11:33 +02002255 struct amba_pl011_data *plat;
2256
Jingoo Han574de552013-07-30 17:06:57 +09002257 plat = dev_get_platdata(uap->port.dev);
Shreshtha Kumar Sahuc16d51a2011-06-13 10:11:33 +02002258 if (plat->init)
2259 plat->init();
2260 }
2261
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 uap->port.uartclk = clk_get_rate(uap->clk);
2263
Andre Przywaracefc2d12015-05-21 17:26:22 +01002264 if (uap->vendor->fixed_options) {
2265 baud = uap->fixed_baud;
2266 } else {
2267 if (options)
2268 uart_parse_options(options,
2269 &baud, &parity, &bits, &flow);
2270 else
2271 pl011_console_get_options(uap, &baud, &parity, &bits);
2272 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273
2274 return uart_set_options(&uap->port, co, baud, parity, bits, flow);
2275}
2276
Vincent Sanders2d934862005-09-14 22:36:03 +01002277static struct uart_driver amba_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278static struct console amba_console = {
2279 .name = "ttyAMA",
2280 .write = pl011_console_write,
2281 .device = uart_console_device,
2282 .setup = pl011_console_setup,
2283 .flags = CON_PRINTBUFFER,
2284 .index = -1,
2285 .data = &amba_reg,
2286};
2287
2288#define AMBA_CONSOLE (&amba_console)
Rob Herring0d3c6732014-04-18 17:19:57 -05002289
2290static void pl011_putc(struct uart_port *port, int c)
2291{
Jun Nie7b753f32015-07-31 15:49:16 +08002292 struct uart_amba_port *uap =
2293 container_of(port, struct uart_amba_port, port);
2294
2295 while (pl011_readw(uap, REG_FR) & UART01x_FR_TXFF)
Rob Herring0d3c6732014-04-18 17:19:57 -05002296 ;
Jun Nie7b753f32015-07-31 15:49:16 +08002297 pl011_writeb(uap, c, REG_DR);
Greg Kroah-Hartman062a68a2015-09-04 09:11:24 -07002298 while (pl011_readw(uap, REG_FR) & UART01x_FR_BUSY)
Rob Herring0d3c6732014-04-18 17:19:57 -05002299 ;
2300}
2301
2302static void pl011_early_write(struct console *con, const char *s, unsigned n)
2303{
2304 struct earlycon_device *dev = con->data;
2305
2306 uart_console_write(&dev->port, s, n, pl011_putc);
2307}
2308
2309static int __init pl011_early_console_setup(struct earlycon_device *device,
2310 const char *opt)
2311{
2312 if (!device->port.membase)
2313 return -ENODEV;
2314
2315 device->con->write = pl011_early_write;
2316 return 0;
2317}
2318EARLYCON_DECLARE(pl011, pl011_early_console_setup);
Rob Herring45e0f0f2014-03-27 08:08:03 -05002319OF_EARLYCON_DECLARE(pl011, "arm,pl011", pl011_early_console_setup);
Rob Herring0d3c6732014-04-18 17:19:57 -05002320
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321#else
2322#define AMBA_CONSOLE NULL
2323#endif
2324
2325static struct uart_driver amba_reg = {
2326 .owner = THIS_MODULE,
2327 .driver_name = "ttyAMA",
2328 .dev_name = "ttyAMA",
2329 .major = SERIAL_AMBA_MAJOR,
2330 .minor = SERIAL_AMBA_MINOR,
2331 .nr = UART_NR,
2332 .cons = AMBA_CONSOLE,
2333};
2334
Matthew Leach32614aa2012-08-28 16:41:28 +01002335static int pl011_probe_dt_alias(int index, struct device *dev)
2336{
2337 struct device_node *np;
2338 static bool seen_dev_with_alias = false;
2339 static bool seen_dev_without_alias = false;
2340 int ret = index;
2341
2342 if (!IS_ENABLED(CONFIG_OF))
2343 return ret;
2344
2345 np = dev->of_node;
2346 if (!np)
2347 return ret;
2348
2349 ret = of_alias_get_id(np, "serial");
2350 if (IS_ERR_VALUE(ret)) {
2351 seen_dev_without_alias = true;
2352 ret = index;
2353 } else {
2354 seen_dev_with_alias = true;
2355 if (ret >= ARRAY_SIZE(amba_ports) || amba_ports[ret] != NULL) {
2356 dev_warn(dev, "requested serial port %d not available.\n", ret);
2357 ret = index;
2358 }
2359 }
2360
2361 if (seen_dev_with_alias && seen_dev_without_alias)
2362 dev_warn(dev, "aliased and non-aliased serial devices found in device tree. Serial port enumeration may be unpredictable.\n");
2363
2364 return ret;
2365}
2366
Andre Przywara49bb3c82015-05-21 17:26:14 +01002367/* unregisters the driver also if no more ports are left */
2368static void pl011_unregister_port(struct uart_amba_port *uap)
2369{
2370 int i;
2371 bool busy = false;
2372
2373 for (i = 0; i < ARRAY_SIZE(amba_ports); i++) {
2374 if (amba_ports[i] == uap)
2375 amba_ports[i] = NULL;
2376 else if (amba_ports[i])
2377 busy = true;
2378 }
2379 pl011_dma_remove(uap);
2380 if (!busy)
2381 uart_unregister_driver(&amba_reg);
2382}
2383
Andre Przywara3873e2d2015-05-21 17:26:18 +01002384static int pl011_find_free_port(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385{
Andre Przywara3873e2d2015-05-21 17:26:18 +01002386 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387
2388 for (i = 0; i < ARRAY_SIZE(amba_ports); i++)
2389 if (amba_ports[i] == NULL)
Andre Przywara3873e2d2015-05-21 17:26:18 +01002390 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391
Andre Przywara3873e2d2015-05-21 17:26:18 +01002392 return -EBUSY;
2393}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394
Andre Przywara3873e2d2015-05-21 17:26:18 +01002395static int pl011_setup_port(struct device *dev, struct uart_amba_port *uap,
2396 struct resource *mmiobase, int index)
2397{
2398 void __iomem *base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399
Andre Przywara3873e2d2015-05-21 17:26:18 +01002400 base = devm_ioremap_resource(dev, mmiobase);
Krzysztof Kozlowski97a60ea2015-07-09 22:21:41 +09002401 if (IS_ERR(base))
2402 return PTR_ERR(base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403
Andre Przywara3873e2d2015-05-21 17:26:18 +01002404 index = pl011_probe_dt_alias(index, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405
Shreshtha Kumar Sahud8d8ffa2012-01-18 15:53:59 +05302406 uap->old_cr = 0;
Andre Przywara3873e2d2015-05-21 17:26:18 +01002407 uap->port.dev = dev;
2408 uap->port.mapbase = mmiobase->start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 uap->port.membase = base;
2410 uap->port.iotype = UPIO_MEM;
Russell Kingffca2b12010-12-22 17:13:05 +00002411 uap->port.fifosize = uap->fifosize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 uap->port.flags = UPF_BOOT_AUTOCONF;
Andre Przywara3873e2d2015-05-21 17:26:18 +01002413 uap->port.line = index;
2414
2415 amba_ports[index] = uap;
2416
2417 return 0;
2418}
2419
2420static int pl011_register_port(struct uart_amba_port *uap)
2421{
2422 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423
Linus Walleijc3d8b762012-03-21 20:15:18 +01002424 /* Ensure interrupts from this UART are masked and cleared */
Jun Nie7b753f32015-07-31 15:49:16 +08002425 pl011_writew(uap, 0, REG_IMSC);
2426 pl011_writew(uap, 0xffff, REG_ICR);
Linus Walleijc3d8b762012-03-21 20:15:18 +01002427
Tushar Beheraef2889f2014-01-20 14:32:35 +05302428 if (!amba_reg.state) {
2429 ret = uart_register_driver(&amba_reg);
2430 if (ret < 0) {
Andre Przywara3873e2d2015-05-21 17:26:18 +01002431 dev_err(uap->port.dev,
Jorge Ramirez-Ortiz1c9be312015-03-06 13:05:40 -05002432 "Failed to register AMBA-PL011 driver\n");
Tushar Beheraef2889f2014-01-20 14:32:35 +05302433 return ret;
2434 }
2435 }
2436
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 ret = uart_add_one_port(&amba_reg, &uap->port);
Andre Przywara49bb3c82015-05-21 17:26:14 +01002438 if (ret)
2439 pl011_unregister_port(uap);
Tushar Behera7f6d9422014-06-26 15:35:35 +05302440
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 return ret;
2442}
2443
Andre Przywara3873e2d2015-05-21 17:26:18 +01002444static int pl011_probe(struct amba_device *dev, const struct amba_id *id)
2445{
2446 struct uart_amba_port *uap;
2447 struct vendor_data *vendor = id->data;
2448 int portnr, ret;
2449
2450 portnr = pl011_find_free_port();
2451 if (portnr < 0)
2452 return portnr;
2453
2454 uap = devm_kzalloc(&dev->dev, sizeof(struct uart_amba_port),
2455 GFP_KERNEL);
2456 if (!uap)
2457 return -ENOMEM;
2458
2459 uap->clk = devm_clk_get(&dev->dev, NULL);
2460 if (IS_ERR(uap->clk))
2461 return PTR_ERR(uap->clk);
2462
2463 uap->vendor = vendor;
Jun Nie2c096a92015-07-31 15:49:17 +08002464 uap->reg_lut = vendor->reg_lut;
Andre Przywara3873e2d2015-05-21 17:26:18 +01002465 uap->lcrh_rx = vendor->lcrh_rx;
2466 uap->lcrh_tx = vendor->lcrh_tx;
2467 uap->fifosize = vendor->get_fifosize(dev);
2468 uap->port.irq = dev->irq[0];
2469 uap->port.ops = &amba_pl011_pops;
2470
2471 snprintf(uap->type, sizeof(uap->type), "PL011 rev%u", amba_rev(dev));
2472
2473 ret = pl011_setup_port(&dev->dev, uap, &dev->res, portnr);
2474 if (ret)
2475 return ret;
2476
2477 amba_set_drvdata(dev, uap);
2478
2479 return pl011_register_port(uap);
2480}
2481
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482static int pl011_remove(struct amba_device *dev)
2483{
2484 struct uart_amba_port *uap = amba_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 uart_remove_one_port(&amba_reg, &uap->port);
Andre Przywara49bb3c82015-05-21 17:26:14 +01002487 pl011_unregister_port(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 return 0;
2489}
2490
Ulf Hanssond0ce8502013-12-03 11:04:28 +01002491#ifdef CONFIG_PM_SLEEP
2492static int pl011_suspend(struct device *dev)
Leo Chenb736b892009-07-28 23:43:33 +01002493{
Ulf Hanssond0ce8502013-12-03 11:04:28 +01002494 struct uart_amba_port *uap = dev_get_drvdata(dev);
Leo Chenb736b892009-07-28 23:43:33 +01002495
2496 if (!uap)
2497 return -EINVAL;
2498
2499 return uart_suspend_port(&amba_reg, &uap->port);
2500}
2501
Ulf Hanssond0ce8502013-12-03 11:04:28 +01002502static int pl011_resume(struct device *dev)
Leo Chenb736b892009-07-28 23:43:33 +01002503{
Ulf Hanssond0ce8502013-12-03 11:04:28 +01002504 struct uart_amba_port *uap = dev_get_drvdata(dev);
Leo Chenb736b892009-07-28 23:43:33 +01002505
2506 if (!uap)
2507 return -EINVAL;
2508
2509 return uart_resume_port(&amba_reg, &uap->port);
2510}
2511#endif
2512
Ulf Hanssond0ce8502013-12-03 11:04:28 +01002513static SIMPLE_DEV_PM_OPS(pl011_dev_pm_ops, pl011_suspend, pl011_resume);
2514
Andre Przywara0dd1e242015-05-21 17:26:23 +01002515static int sbsa_uart_probe(struct platform_device *pdev)
2516{
2517 struct uart_amba_port *uap;
2518 struct resource *r;
2519 int portnr, ret;
2520 int baudrate;
2521
2522 /*
2523 * Check the mandatory baud rate parameter in the DT node early
2524 * so that we can easily exit with the error.
2525 */
2526 if (pdev->dev.of_node) {
2527 struct device_node *np = pdev->dev.of_node;
2528
2529 ret = of_property_read_u32(np, "current-speed", &baudrate);
2530 if (ret)
2531 return ret;
2532 } else {
2533 baudrate = 115200;
2534 }
2535
2536 portnr = pl011_find_free_port();
2537 if (portnr < 0)
2538 return portnr;
2539
2540 uap = devm_kzalloc(&pdev->dev, sizeof(struct uart_amba_port),
2541 GFP_KERNEL);
2542 if (!uap)
2543 return -ENOMEM;
2544
2545 uap->vendor = &vendor_sbsa;
Jun Nie2c096a92015-07-31 15:49:17 +08002546 uap->reg_lut = vendor_sbsa.reg_lut;
Andre Przywara0dd1e242015-05-21 17:26:23 +01002547 uap->fifosize = 32;
2548 uap->port.irq = platform_get_irq(pdev, 0);
2549 uap->port.ops = &sbsa_uart_pops;
2550 uap->fixed_baud = baudrate;
2551
2552 snprintf(uap->type, sizeof(uap->type), "SBSA");
2553
2554 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2555
2556 ret = pl011_setup_port(&pdev->dev, uap, r, portnr);
2557 if (ret)
2558 return ret;
2559
2560 platform_set_drvdata(pdev, uap);
2561
2562 return pl011_register_port(uap);
2563}
2564
2565static int sbsa_uart_remove(struct platform_device *pdev)
2566{
2567 struct uart_amba_port *uap = platform_get_drvdata(pdev);
2568
2569 uart_remove_one_port(&amba_reg, &uap->port);
2570 pl011_unregister_port(uap);
2571 return 0;
2572}
2573
2574static const struct of_device_id sbsa_uart_of_match[] = {
2575 { .compatible = "arm,sbsa-uart", },
2576 {},
2577};
2578MODULE_DEVICE_TABLE(of, sbsa_uart_of_match);
2579
Graeme Gregory3db9ab02015-05-21 17:26:24 +01002580static const struct acpi_device_id sbsa_uart_acpi_match[] = {
2581 { "ARMH0011", 0 },
2582 {},
2583};
2584MODULE_DEVICE_TABLE(acpi, sbsa_uart_acpi_match);
2585
Andre Przywara0dd1e242015-05-21 17:26:23 +01002586static struct platform_driver arm_sbsa_uart_platform_driver = {
2587 .probe = sbsa_uart_probe,
2588 .remove = sbsa_uart_remove,
2589 .driver = {
2590 .name = "sbsa-uart",
2591 .of_match_table = of_match_ptr(sbsa_uart_of_match),
Graeme Gregory3db9ab02015-05-21 17:26:24 +01002592 .acpi_match_table = ACPI_PTR(sbsa_uart_acpi_match),
Andre Przywara0dd1e242015-05-21 17:26:23 +01002593 },
2594};
2595
Russell King2c39c9e2010-07-27 08:50:16 +01002596static struct amba_id pl011_ids[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597 {
2598 .id = 0x00041011,
2599 .mask = 0x000fffff,
Alessandro Rubini5926a292009-06-04 17:43:04 +01002600 .data = &vendor_arm,
2601 },
2602 {
2603 .id = 0x00380802,
2604 .mask = 0x00ffffff,
2605 .data = &vendor_st,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 },
2607 { 0, 0 },
2608};
2609
Dave Martin60f7a332011-10-05 15:15:22 +01002610MODULE_DEVICE_TABLE(amba, pl011_ids);
2611
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612static struct amba_driver pl011_driver = {
2613 .drv = {
2614 .name = "uart-pl011",
Ulf Hanssond0ce8502013-12-03 11:04:28 +01002615 .pm = &pl011_dev_pm_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616 },
2617 .id_table = pl011_ids,
2618 .probe = pl011_probe,
2619 .remove = pl011_remove,
2620};
2621
2622static int __init pl011_init(void)
2623{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624 printk(KERN_INFO "Serial: AMBA PL011 UART driver\n");
2625
Andre Przywara0dd1e242015-05-21 17:26:23 +01002626 if (platform_driver_register(&arm_sbsa_uart_platform_driver))
2627 pr_warn("could not register SBSA UART platform driver\n");
Greg Kroah-Hartman062a68a2015-09-04 09:11:24 -07002628 return amba_driver_register(&pl011_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629}
2630
2631static void __exit pl011_exit(void)
2632{
Andre Przywara0dd1e242015-05-21 17:26:23 +01002633 platform_driver_unregister(&arm_sbsa_uart_platform_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634 amba_driver_unregister(&pl011_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635}
2636
Alessandro Rubini4dd9e742009-05-05 05:54:13 +01002637/*
2638 * While this can be a module, if builtin it's most likely the console
2639 * So let's leave module_exit but move module_init to an earlier place
2640 */
2641arch_initcall(pl011_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642module_exit(pl011_exit);
2643
2644MODULE_AUTHOR("ARM Ltd/Deep Blue Solutions Ltd");
2645MODULE_DESCRIPTION("ARM AMBA serial port driver");
2646MODULE_LICENSE("GPL");