blob: 2af09ab153b6db628c09bf248219a65a0e40387d [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;
Jun Nie8cd90e52015-07-31 15:49:19 +080077 unsigned int fr_busy;
78 unsigned int fr_dsr;
79 unsigned int fr_cts;
80 unsigned int fr_ri;
Linus Walleijec489aa2010-06-02 08:13:52 +010081 unsigned int lcrh_tx;
82 unsigned int lcrh_rx;
Jun Nie2c096a92015-07-31 15:49:17 +080083 u16 *reg_lut;
Linus Walleijac3e3fb2010-06-02 20:40:22 +010084 bool oversampling;
Russell King38d62432010-12-22 17:59:16 +000085 bool dma_threshold;
Rajanikanth H.V4fd06902012-03-26 11:17:02 +020086 bool cts_event_workaround;
Andre Przywara71eec482015-05-21 17:26:21 +010087 bool always_enabled;
Andre Przywaracefc2d12015-05-21 17:26:22 +010088 bool fixed_options;
Jongsung Kim78506f22013-04-15 14:45:25 +090089
Jongsung Kimea336402013-05-10 18:05:35 +090090 unsigned int (*get_fifosize)(struct amba_device *dev);
Alessandro Rubini5926a292009-06-04 17:43:04 +010091};
92
Jun Nie7b753f32015-07-31 15:49:16 +080093/* Max address offset of register in use is 0x48 */
94#define REG_NR (0x48 >> 2)
95#define IDX(x) (x >> 2)
Jun Nie534e14e2015-07-31 15:49:15 +080096enum reg_idx {
Jun Nie7b753f32015-07-31 15:49:16 +080097 REG_DR = IDX(UART01x_DR),
98 REG_RSR = IDX(UART01x_RSR),
99 REG_ST_DMAWM = IDX(ST_UART011_DMAWM),
100 REG_FR = IDX(UART01x_FR),
101 REG_ST_LCRH_RX = IDX(ST_UART011_LCRH_RX),
102 REG_ILPR = IDX(UART01x_ILPR),
103 REG_IBRD = IDX(UART011_IBRD),
104 REG_FBRD = IDX(UART011_FBRD),
105 REG_LCRH = IDX(UART011_LCRH),
106 REG_CR = IDX(UART011_CR),
107 REG_IFLS = IDX(UART011_IFLS),
108 REG_IMSC = IDX(UART011_IMSC),
109 REG_RIS = IDX(UART011_RIS),
110 REG_MIS = IDX(UART011_MIS),
111 REG_ICR = IDX(UART011_ICR),
112 REG_DMACR = IDX(UART011_DMACR),
Jun Nie534e14e2015-07-31 15:49:15 +0800113};
114
Jun Nie2c096a92015-07-31 15:49:17 +0800115static u16 arm_reg[] = {
116 [REG_DR] = UART01x_DR,
117 [REG_RSR] = UART01x_RSR,
118 [REG_ST_DMAWM] = ~0,
119 [REG_FR] = UART01x_FR,
120 [REG_ST_LCRH_RX] = ~0,
121 [REG_ILPR] = UART01x_ILPR,
122 [REG_IBRD] = UART011_IBRD,
123 [REG_FBRD] = UART011_FBRD,
124 [REG_LCRH] = UART011_LCRH,
125 [REG_CR] = UART011_CR,
126 [REG_IFLS] = UART011_IFLS,
127 [REG_IMSC] = UART011_IMSC,
128 [REG_RIS] = UART011_RIS,
129 [REG_MIS] = UART011_MIS,
130 [REG_ICR] = UART011_ICR,
131 [REG_DMACR] = UART011_DMACR,
132};
133
Jun Nie8cd90e52015-07-31 15:49:19 +0800134#ifdef CONFIG_ARM_AMBA
Jongsung Kimea336402013-05-10 18:05:35 +0900135static unsigned int get_fifosize_arm(struct amba_device *dev)
Jongsung Kim78506f22013-04-15 14:45:25 +0900136{
Jongsung Kimea336402013-05-10 18:05:35 +0900137 return amba_rev(dev) < 3 ? 16 : 32;
Jongsung Kim78506f22013-04-15 14:45:25 +0900138}
139
Alessandro Rubini5926a292009-06-04 17:43:04 +0100140static struct vendor_data vendor_arm = {
141 .ifls = UART011_IFLS_RX4_8|UART011_IFLS_TX4_8,
Jun Nie8cd90e52015-07-31 15:49:19 +0800142 .fr_busy = UART01x_FR_BUSY,
143 .fr_dsr = UART01x_FR_DSR,
144 .fr_cts = UART01x_FR_CTS,
145 .fr_ri = UART011_FR_RI,
Jun Nie534e14e2015-07-31 15:49:15 +0800146 .lcrh_tx = REG_LCRH,
147 .lcrh_rx = REG_LCRH,
Jun Nie2c096a92015-07-31 15:49:17 +0800148 .reg_lut = arm_reg,
Linus Walleijac3e3fb2010-06-02 20:40:22 +0100149 .oversampling = false,
Russell King38d62432010-12-22 17:59:16 +0000150 .dma_threshold = false,
Rajanikanth H.V4fd06902012-03-26 11:17:02 +0200151 .cts_event_workaround = false,
Andre Przywara71eec482015-05-21 17:26:21 +0100152 .always_enabled = false,
Andre Przywaracefc2d12015-05-21 17:26:22 +0100153 .fixed_options = false,
Jongsung Kim78506f22013-04-15 14:45:25 +0900154 .get_fifosize = get_fifosize_arm,
Alessandro Rubini5926a292009-06-04 17:43:04 +0100155};
Jun Nie8cd90e52015-07-31 15:49:19 +0800156#endif
Alessandro Rubini5926a292009-06-04 17:43:04 +0100157
Andre Przywara0dd1e242015-05-21 17:26:23 +0100158static struct vendor_data vendor_sbsa = {
Jun Nie8cd90e52015-07-31 15:49:19 +0800159 .fr_busy = UART01x_FR_BUSY,
160 .fr_dsr = UART01x_FR_DSR,
161 .fr_cts = UART01x_FR_CTS,
162 .fr_ri = UART011_FR_RI,
Jun Nie2c096a92015-07-31 15:49:17 +0800163 .reg_lut = arm_reg,
Andre Przywara0dd1e242015-05-21 17:26:23 +0100164 .oversampling = false,
165 .dma_threshold = false,
166 .cts_event_workaround = false,
167 .always_enabled = true,
168 .fixed_options = true,
169};
170
Jun Nie8cd90e52015-07-31 15:49:19 +0800171#ifdef CONFIG_ARM_AMBA
Jun Nie2c096a92015-07-31 15:49:17 +0800172static u16 st_reg[] = {
173 [REG_DR] = UART01x_DR,
174 [REG_RSR] = UART01x_RSR,
175 [REG_ST_DMAWM] = ST_UART011_DMAWM,
176 [REG_FR] = UART01x_FR,
177 [REG_ST_LCRH_RX] = ST_UART011_LCRH_RX,
178 [REG_ILPR] = UART01x_ILPR,
179 [REG_IBRD] = UART011_IBRD,
180 [REG_FBRD] = UART011_FBRD,
181 [REG_LCRH] = UART011_LCRH,
182 [REG_CR] = UART011_CR,
183 [REG_IFLS] = UART011_IFLS,
184 [REG_IMSC] = UART011_IMSC,
185 [REG_RIS] = UART011_RIS,
186 [REG_MIS] = UART011_MIS,
187 [REG_ICR] = UART011_ICR,
188 [REG_DMACR] = UART011_DMACR,
189};
190
Jongsung Kimea336402013-05-10 18:05:35 +0900191static unsigned int get_fifosize_st(struct amba_device *dev)
Jongsung Kim78506f22013-04-15 14:45:25 +0900192{
193 return 64;
194}
195
Alessandro Rubini5926a292009-06-04 17:43:04 +0100196static struct vendor_data vendor_st = {
197 .ifls = UART011_IFLS_RX_HALF|UART011_IFLS_TX_HALF,
Jun Nie8cd90e52015-07-31 15:49:19 +0800198 .fr_busy = UART01x_FR_BUSY,
199 .fr_dsr = UART01x_FR_DSR,
200 .fr_cts = UART01x_FR_CTS,
201 .fr_ri = UART011_FR_RI,
Jun Nie534e14e2015-07-31 15:49:15 +0800202 .lcrh_tx = REG_LCRH,
203 .lcrh_rx = REG_ST_LCRH_RX,
Jun Nie2c096a92015-07-31 15:49:17 +0800204 .reg_lut = st_reg,
Linus Walleijac3e3fb2010-06-02 20:40:22 +0100205 .oversampling = true,
Russell King38d62432010-12-22 17:59:16 +0000206 .dma_threshold = true,
Rajanikanth H.V4fd06902012-03-26 11:17:02 +0200207 .cts_event_workaround = true,
Andre Przywara71eec482015-05-21 17:26:21 +0100208 .always_enabled = false,
Andre Przywaracefc2d12015-05-21 17:26:22 +0100209 .fixed_options = false,
Jongsung Kim78506f22013-04-15 14:45:25 +0900210 .get_fifosize = get_fifosize_st,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211};
Jun Nie8cd90e52015-07-31 15:49:19 +0800212#endif
213
214#ifdef CONFIG_SOC_ZX296702
215static u16 zte_reg[] = {
216 [REG_DR] = ZX_UART01x_DR,
217 [REG_RSR] = UART01x_RSR,
218 [REG_ST_DMAWM] = ST_UART011_DMAWM,
219 [REG_FR] = ZX_UART01x_FR,
220 [REG_ST_LCRH_RX] = ST_UART011_LCRH_RX,
221 [REG_ILPR] = UART01x_ILPR,
222 [REG_IBRD] = UART011_IBRD,
223 [REG_FBRD] = UART011_FBRD,
224 [REG_LCRH] = ZX_UART011_LCRH_TX,
225 [REG_CR] = ZX_UART011_CR,
226 [REG_IFLS] = ZX_UART011_IFLS,
227 [REG_IMSC] = ZX_UART011_IMSC,
228 [REG_RIS] = ZX_UART011_RIS,
229 [REG_MIS] = ZX_UART011_MIS,
230 [REG_ICR] = ZX_UART011_ICR,
231 [REG_DMACR] = ZX_UART011_DMACR,
232};
233
234static struct vendor_data vendor_zte = {
235 .ifls = UART011_IFLS_RX4_8|UART011_IFLS_TX4_8,
236 .fr_busy = ZX_UART01x_FR_BUSY,
237 .fr_dsr = ZX_UART01x_FR_DSR,
238 .fr_cts = ZX_UART01x_FR_CTS,
239 .fr_ri = ZX_UART011_FR_RI,
240 .lcrh_tx = REG_LCRH,
241 .lcrh_rx = REG_ST_LCRH_RX,
242 .reg_lut = zte_reg,
243 .oversampling = false,
244 .dma_threshold = false,
245 .cts_event_workaround = false,
246 .fixed_options = false,
247};
248#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
Russell King68b65f72010-12-22 17:24:39 +0000250/* Deals with DMA transactions */
Linus Walleijead76f322011-02-24 13:21:08 +0100251
252struct pl011_sgbuf {
253 struct scatterlist sg;
254 char *buf;
255};
256
257struct pl011_dmarx_data {
258 struct dma_chan *chan;
259 struct completion complete;
260 bool use_buf_b;
261 struct pl011_sgbuf sgbuf_a;
262 struct pl011_sgbuf sgbuf_b;
263 dma_cookie_t cookie;
264 bool running;
Chanho Mincb06ff12013-03-27 18:38:11 +0900265 struct timer_list timer;
266 unsigned int last_residue;
267 unsigned long last_jiffies;
268 bool auto_poll_rate;
269 unsigned int poll_rate;
270 unsigned int poll_timeout;
Linus Walleijead76f322011-02-24 13:21:08 +0100271};
272
Russell King68b65f72010-12-22 17:24:39 +0000273struct pl011_dmatx_data {
274 struct dma_chan *chan;
275 struct scatterlist sg;
276 char *buf;
277 bool queued;
278};
279
Russell Kingc19f12b2010-12-22 17:48:26 +0000280/*
281 * We wrap our port structure around the generic uart_port.
282 */
283struct uart_amba_port {
284 struct uart_port port;
285 struct clk *clk;
286 const struct vendor_data *vendor;
Jun Nie2c096a92015-07-31 15:49:17 +0800287 u16 *reg_lut;
Russell King68b65f72010-12-22 17:24:39 +0000288 unsigned int dmacr; /* dma control reg */
Russell Kingc19f12b2010-12-22 17:48:26 +0000289 unsigned int im; /* interrupt mask */
290 unsigned int old_status;
Russell Kingffca2b12010-12-22 17:13:05 +0000291 unsigned int fifosize; /* vendor-specific */
Jun Nie8cd90e52015-07-31 15:49:19 +0800292 unsigned int fr_busy; /* vendor-specific */
293 unsigned int fr_dsr; /* vendor-specific */
294 unsigned int fr_cts; /* vendor-specific */
295 unsigned int fr_ri; /* vendor-specific */
Russell Kingc19f12b2010-12-22 17:48:26 +0000296 unsigned int lcrh_tx; /* vendor-specific */
297 unsigned int lcrh_rx; /* vendor-specific */
Shreshtha Kumar Sahud8d8ffa2012-01-18 15:53:59 +0530298 unsigned int old_cr; /* state during shutdown */
Russell Kingc19f12b2010-12-22 17:48:26 +0000299 bool autorts;
Andre Przywaracefc2d12015-05-21 17:26:22 +0100300 unsigned int fixed_baud; /* vendor-set fixed baud rate */
Russell Kingc19f12b2010-12-22 17:48:26 +0000301 char type[12];
Russell King68b65f72010-12-22 17:24:39 +0000302#ifdef CONFIG_DMA_ENGINE
303 /* DMA stuff */
Linus Walleijead76f322011-02-24 13:21:08 +0100304 bool using_tx_dma;
305 bool using_rx_dma;
306 struct pl011_dmarx_data dmarx;
Russell King68b65f72010-12-22 17:24:39 +0000307 struct pl011_dmatx_data dmatx;
Jorge Ramirez-Ortiz1c9be312015-03-06 13:05:40 -0500308 bool dma_probed;
Russell King68b65f72010-12-22 17:24:39 +0000309#endif
Russell Kingc19f12b2010-12-22 17:48:26 +0000310};
311
Jun Nie09dcc7d2015-07-31 15:49:18 +0800312static bool is_implemented(struct uart_amba_port *uap, unsigned int reg)
313{
314 return uap->reg_lut[reg] != (u16)~0;
315}
316
Jun Nie7b753f32015-07-31 15:49:16 +0800317static unsigned int pl011_readw(struct uart_amba_port *uap, int index)
318{
319 WARN_ON(index > REG_NR);
Jun Nie2c096a92015-07-31 15:49:17 +0800320 return readw_relaxed(uap->port.membase + uap->reg_lut[index]);
Jun Nie7b753f32015-07-31 15:49:16 +0800321}
322
323static void pl011_writew(struct uart_amba_port *uap, int val, int index)
324{
325 WARN_ON(index > REG_NR);
Jun Nie2c096a92015-07-31 15:49:17 +0800326 writew_relaxed(val, uap->port.membase + uap->reg_lut[index]);
Jun Nie7b753f32015-07-31 15:49:16 +0800327}
328
329static void pl011_writeb(struct uart_amba_port *uap, u8 val, int index)
330{
331 WARN_ON(index > REG_NR);
Jun Nie2c096a92015-07-31 15:49:17 +0800332 writeb_relaxed(val, uap->port.membase + uap->reg_lut[index]);
Jun Nie7b753f32015-07-31 15:49:16 +0800333}
334
Russell King68b65f72010-12-22 17:24:39 +0000335/*
Linus Walleij29772c42011-02-24 13:21:36 +0100336 * Reads up to 256 characters from the FIFO or until it's empty and
337 * inserts them into the TTY layer. Returns the number of characters
338 * read from the FIFO.
339 */
340static int pl011_fifo_to_tty(struct uart_amba_port *uap)
341{
342 u16 status, ch;
343 unsigned int flag, max_count = 256;
344 int fifotaken = 0;
345
346 while (max_count--) {
Jun Nie7b753f32015-07-31 15:49:16 +0800347 status = pl011_readw(uap, REG_FR);
Linus Walleij29772c42011-02-24 13:21:36 +0100348 if (status & UART01x_FR_RXFE)
349 break;
350
351 /* Take chars from the FIFO and update status */
Jun Nie7b753f32015-07-31 15:49:16 +0800352 ch = pl011_readw(uap, REG_DR) |
Linus Walleij29772c42011-02-24 13:21:36 +0100353 UART_DUMMY_DR_RX;
354 flag = TTY_NORMAL;
355 uap->port.icount.rx++;
356 fifotaken++;
357
358 if (unlikely(ch & UART_DR_ERROR)) {
359 if (ch & UART011_DR_BE) {
360 ch &= ~(UART011_DR_FE | UART011_DR_PE);
361 uap->port.icount.brk++;
362 if (uart_handle_break(&uap->port))
363 continue;
364 } else if (ch & UART011_DR_PE)
365 uap->port.icount.parity++;
366 else if (ch & UART011_DR_FE)
367 uap->port.icount.frame++;
368 if (ch & UART011_DR_OE)
369 uap->port.icount.overrun++;
370
371 ch &= uap->port.read_status_mask;
372
373 if (ch & UART011_DR_BE)
374 flag = TTY_BREAK;
375 else if (ch & UART011_DR_PE)
376 flag = TTY_PARITY;
377 else if (ch & UART011_DR_FE)
378 flag = TTY_FRAME;
379 }
380
381 if (uart_handle_sysrq_char(&uap->port, ch & 255))
382 continue;
383
384 uart_insert_char(&uap->port, ch, UART011_DR_OE, ch, flag);
385 }
386
387 return fifotaken;
388}
389
390
391/*
Russell King68b65f72010-12-22 17:24:39 +0000392 * All the DMA operation mode stuff goes inside this ifdef.
393 * This assumes that you have a generic DMA device interface,
394 * no custom DMA interfaces are supported.
395 */
396#ifdef CONFIG_DMA_ENGINE
397
398#define PL011_DMA_BUFFER_SIZE PAGE_SIZE
399
Linus Walleijead76f322011-02-24 13:21:08 +0100400static int pl011_sgbuf_init(struct dma_chan *chan, struct pl011_sgbuf *sg,
401 enum dma_data_direction dir)
402{
Chanho Mincb06ff12013-03-27 18:38:11 +0900403 dma_addr_t dma_addr;
404
405 sg->buf = dma_alloc_coherent(chan->device->dev,
406 PL011_DMA_BUFFER_SIZE, &dma_addr, GFP_KERNEL);
Linus Walleijead76f322011-02-24 13:21:08 +0100407 if (!sg->buf)
408 return -ENOMEM;
409
Chanho Mincb06ff12013-03-27 18:38:11 +0900410 sg_init_table(&sg->sg, 1);
411 sg_set_page(&sg->sg, phys_to_page(dma_addr),
412 PL011_DMA_BUFFER_SIZE, offset_in_page(dma_addr));
413 sg_dma_address(&sg->sg) = dma_addr;
Andrew Jacksonc64be922014-11-07 14:14:43 +0000414 sg_dma_len(&sg->sg) = PL011_DMA_BUFFER_SIZE;
Linus Walleijead76f322011-02-24 13:21:08 +0100415
Linus Walleijead76f322011-02-24 13:21:08 +0100416 return 0;
417}
418
419static void pl011_sgbuf_free(struct dma_chan *chan, struct pl011_sgbuf *sg,
420 enum dma_data_direction dir)
421{
422 if (sg->buf) {
Chanho Mincb06ff12013-03-27 18:38:11 +0900423 dma_free_coherent(chan->device->dev,
424 PL011_DMA_BUFFER_SIZE, sg->buf,
425 sg_dma_address(&sg->sg));
Linus Walleijead76f322011-02-24 13:21:08 +0100426 }
427}
428
Jorge Ramirez-Ortiz1c9be312015-03-06 13:05:40 -0500429static void pl011_dma_probe(struct uart_amba_port *uap)
Russell King68b65f72010-12-22 17:24:39 +0000430{
431 /* DMA is the sole user of the platform data right now */
Jingoo Han574de552013-07-30 17:06:57 +0900432 struct amba_pl011_data *plat = dev_get_platdata(uap->port.dev);
Jorge Ramirez-Ortiz1c9be312015-03-06 13:05:40 -0500433 struct device *dev = uap->port.dev;
Russell King68b65f72010-12-22 17:24:39 +0000434 struct dma_slave_config tx_conf = {
Jun Nie2c096a92015-07-31 15:49:17 +0800435 .dst_addr = uap->port.mapbase + uap->reg_lut[REG_DR],
Russell King68b65f72010-12-22 17:24:39 +0000436 .dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE,
Vinod Koula485df42011-10-14 10:47:38 +0530437 .direction = DMA_MEM_TO_DEV,
Russell King68b65f72010-12-22 17:24:39 +0000438 .dst_maxburst = uap->fifosize >> 1,
Viresh Kumar258aea72012-02-01 16:12:19 +0530439 .device_fc = false,
Russell King68b65f72010-12-22 17:24:39 +0000440 };
441 struct dma_chan *chan;
442 dma_cap_mask_t mask;
443
Jorge Ramirez-Ortiz1c9be312015-03-06 13:05:40 -0500444 uap->dma_probed = true;
445 chan = dma_request_slave_channel_reason(dev, "tx");
446 if (IS_ERR(chan)) {
447 if (PTR_ERR(chan) == -EPROBE_DEFER) {
Jorge Ramirez-Ortiz1c9be312015-03-06 13:05:40 -0500448 uap->dma_probed = false;
449 return;
450 }
Russell King68b65f72010-12-22 17:24:39 +0000451
Arnd Bergmann787b0c12013-01-28 16:24:37 +0000452 /* We need platform data */
453 if (!plat || !plat->dma_filter) {
454 dev_info(uap->port.dev, "no DMA platform data\n");
455 return;
456 }
457
458 /* Try to acquire a generic DMA engine slave TX channel */
459 dma_cap_zero(mask);
460 dma_cap_set(DMA_SLAVE, mask);
461
462 chan = dma_request_channel(mask, plat->dma_filter,
463 plat->dma_tx_param);
464 if (!chan) {
465 dev_err(uap->port.dev, "no TX DMA channel!\n");
466 return;
467 }
Russell King68b65f72010-12-22 17:24:39 +0000468 }
469
470 dmaengine_slave_config(chan, &tx_conf);
471 uap->dmatx.chan = chan;
472
473 dev_info(uap->port.dev, "DMA channel TX %s\n",
474 dma_chan_name(uap->dmatx.chan));
Linus Walleijead76f322011-02-24 13:21:08 +0100475
476 /* Optionally make use of an RX channel as well */
Arnd Bergmann787b0c12013-01-28 16:24:37 +0000477 chan = dma_request_slave_channel(dev, "rx");
Rob Herring0d3c6732014-04-18 17:19:57 -0500478
Arnd Bergmann787b0c12013-01-28 16:24:37 +0000479 if (!chan && plat->dma_rx_param) {
480 chan = dma_request_channel(mask, plat->dma_filter, plat->dma_rx_param);
481
482 if (!chan) {
483 dev_err(uap->port.dev, "no RX DMA channel!\n");
484 return;
485 }
486 }
487
488 if (chan) {
Linus Walleijead76f322011-02-24 13:21:08 +0100489 struct dma_slave_config rx_conf = {
Jun Nie2c096a92015-07-31 15:49:17 +0800490 .src_addr = uap->port.mapbase + uap->reg_lut[REG_DR],
Linus Walleijead76f322011-02-24 13:21:08 +0100491 .src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE,
Vinod Koula485df42011-10-14 10:47:38 +0530492 .direction = DMA_DEV_TO_MEM,
Guennadi Liakhovetskib2aeb772014-04-12 19:47:17 +0200493 .src_maxburst = uap->fifosize >> 2,
Viresh Kumar258aea72012-02-01 16:12:19 +0530494 .device_fc = false,
Linus Walleijead76f322011-02-24 13:21:08 +0100495 };
Andrew Jackson2d3b7d62014-11-07 14:14:47 +0000496 struct dma_slave_caps caps;
Linus Walleijead76f322011-02-24 13:21:08 +0100497
Andrew Jackson2d3b7d62014-11-07 14:14:47 +0000498 /*
499 * Some DMA controllers provide information on their capabilities.
500 * If the controller does, check for suitable residue processing
501 * otherwise assime all is well.
502 */
503 if (0 == dma_get_slave_caps(chan, &caps)) {
504 if (caps.residue_granularity ==
505 DMA_RESIDUE_GRANULARITY_DESCRIPTOR) {
506 dma_release_channel(chan);
507 dev_info(uap->port.dev,
508 "RX DMA disabled - no residue processing\n");
509 return;
510 }
511 }
Linus Walleijead76f322011-02-24 13:21:08 +0100512 dmaengine_slave_config(chan, &rx_conf);
513 uap->dmarx.chan = chan;
514
Andrew Jackson98267d32014-11-07 14:14:23 +0000515 uap->dmarx.auto_poll_rate = false;
Greg Kroah-Hartman8f898bf2013-12-17 09:33:18 -0800516 if (plat && plat->dma_rx_poll_enable) {
Chanho Mincb06ff12013-03-27 18:38:11 +0900517 /* Set poll rate if specified. */
518 if (plat->dma_rx_poll_rate) {
519 uap->dmarx.auto_poll_rate = false;
520 uap->dmarx.poll_rate = plat->dma_rx_poll_rate;
521 } else {
522 /*
523 * 100 ms defaults to poll rate if not
524 * specified. This will be adjusted with
525 * the baud rate at set_termios.
526 */
527 uap->dmarx.auto_poll_rate = true;
528 uap->dmarx.poll_rate = 100;
529 }
530 /* 3 secs defaults poll_timeout if not specified. */
531 if (plat->dma_rx_poll_timeout)
532 uap->dmarx.poll_timeout =
533 plat->dma_rx_poll_timeout;
534 else
535 uap->dmarx.poll_timeout = 3000;
Andrew Jackson98267d32014-11-07 14:14:23 +0000536 } else if (!plat && dev->of_node) {
537 uap->dmarx.auto_poll_rate = of_property_read_bool(
538 dev->of_node, "auto-poll");
539 if (uap->dmarx.auto_poll_rate) {
540 u32 x;
Chanho Mincb06ff12013-03-27 18:38:11 +0900541
Andrew Jackson98267d32014-11-07 14:14:23 +0000542 if (0 == of_property_read_u32(dev->of_node,
543 "poll-rate-ms", &x))
544 uap->dmarx.poll_rate = x;
545 else
546 uap->dmarx.poll_rate = 100;
547 if (0 == of_property_read_u32(dev->of_node,
548 "poll-timeout-ms", &x))
549 uap->dmarx.poll_timeout = x;
550 else
551 uap->dmarx.poll_timeout = 3000;
552 }
553 }
Linus Walleijead76f322011-02-24 13:21:08 +0100554 dev_info(uap->port.dev, "DMA channel RX %s\n",
555 dma_chan_name(uap->dmarx.chan));
556 }
Russell King68b65f72010-12-22 17:24:39 +0000557}
558
Russell King68b65f72010-12-22 17:24:39 +0000559static void pl011_dma_remove(struct uart_amba_port *uap)
560{
Russell King68b65f72010-12-22 17:24:39 +0000561 if (uap->dmatx.chan)
562 dma_release_channel(uap->dmatx.chan);
Linus Walleijead76f322011-02-24 13:21:08 +0100563 if (uap->dmarx.chan)
564 dma_release_channel(uap->dmarx.chan);
Russell King68b65f72010-12-22 17:24:39 +0000565}
566
Dave Martin734745c2015-03-04 12:27:33 +0000567/* Forward declare these for the refill routine */
Russell King68b65f72010-12-22 17:24:39 +0000568static int pl011_dma_tx_refill(struct uart_amba_port *uap);
Dave Martin734745c2015-03-04 12:27:33 +0000569static void pl011_start_tx_pio(struct uart_amba_port *uap);
Russell King68b65f72010-12-22 17:24:39 +0000570
571/*
572 * The current DMA TX buffer has been sent.
573 * Try to queue up another DMA buffer.
574 */
575static void pl011_dma_tx_callback(void *data)
576{
577 struct uart_amba_port *uap = data;
578 struct pl011_dmatx_data *dmatx = &uap->dmatx;
579 unsigned long flags;
580 u16 dmacr;
581
582 spin_lock_irqsave(&uap->port.lock, flags);
583 if (uap->dmatx.queued)
584 dma_unmap_sg(dmatx->chan->device->dev, &dmatx->sg, 1,
585 DMA_TO_DEVICE);
586
587 dmacr = uap->dmacr;
588 uap->dmacr = dmacr & ~UART011_TXDMAE;
Jun Nie7b753f32015-07-31 15:49:16 +0800589 pl011_writew(uap, uap->dmacr, REG_DMACR);
Russell King68b65f72010-12-22 17:24:39 +0000590
591 /*
592 * If TX DMA was disabled, it means that we've stopped the DMA for
593 * some reason (eg, XOFF received, or we want to send an X-char.)
594 *
595 * Note: we need to be careful here of a potential race between DMA
596 * and the rest of the driver - if the driver disables TX DMA while
597 * a TX buffer completing, we must update the tx queued status to
598 * get further refills (hence we check dmacr).
599 */
600 if (!(dmacr & UART011_TXDMAE) || uart_tx_stopped(&uap->port) ||
601 uart_circ_empty(&uap->port.state->xmit)) {
602 uap->dmatx.queued = false;
603 spin_unlock_irqrestore(&uap->port.lock, flags);
604 return;
605 }
606
Dave Martin734745c2015-03-04 12:27:33 +0000607 if (pl011_dma_tx_refill(uap) <= 0)
Russell King68b65f72010-12-22 17:24:39 +0000608 /*
609 * We didn't queue a DMA buffer for some reason, but we
610 * have data pending to be sent. Re-enable the TX IRQ.
611 */
Dave Martin734745c2015-03-04 12:27:33 +0000612 pl011_start_tx_pio(uap);
613
Russell King68b65f72010-12-22 17:24:39 +0000614 spin_unlock_irqrestore(&uap->port.lock, flags);
615}
616
617/*
618 * Try to refill the TX DMA buffer.
619 * Locking: called with port lock held and IRQs disabled.
620 * Returns:
621 * 1 if we queued up a TX DMA buffer.
622 * 0 if we didn't want to handle this by DMA
623 * <0 on error
624 */
625static int pl011_dma_tx_refill(struct uart_amba_port *uap)
626{
627 struct pl011_dmatx_data *dmatx = &uap->dmatx;
628 struct dma_chan *chan = dmatx->chan;
629 struct dma_device *dma_dev = chan->device;
630 struct dma_async_tx_descriptor *desc;
631 struct circ_buf *xmit = &uap->port.state->xmit;
632 unsigned int count;
633
634 /*
635 * Try to avoid the overhead involved in using DMA if the
636 * transaction fits in the first half of the FIFO, by using
637 * the standard interrupt handling. This ensures that we
638 * issue a uart_write_wakeup() at the appropriate time.
639 */
640 count = uart_circ_chars_pending(xmit);
641 if (count < (uap->fifosize >> 1)) {
642 uap->dmatx.queued = false;
643 return 0;
644 }
645
646 /*
647 * Bodge: don't send the last character by DMA, as this
648 * will prevent XON from notifying us to restart DMA.
649 */
650 count -= 1;
651
652 /* Else proceed to copy the TX chars to the DMA buffer and fire DMA */
653 if (count > PL011_DMA_BUFFER_SIZE)
654 count = PL011_DMA_BUFFER_SIZE;
655
656 if (xmit->tail < xmit->head)
657 memcpy(&dmatx->buf[0], &xmit->buf[xmit->tail], count);
658 else {
659 size_t first = UART_XMIT_SIZE - xmit->tail;
Andrew Jacksone2a545a2014-11-07 14:14:39 +0000660 size_t second;
661
662 if (first > count)
663 first = count;
664 second = count - first;
Russell King68b65f72010-12-22 17:24:39 +0000665
666 memcpy(&dmatx->buf[0], &xmit->buf[xmit->tail], first);
667 if (second)
668 memcpy(&dmatx->buf[first], &xmit->buf[0], second);
669 }
670
671 dmatx->sg.length = count;
672
673 if (dma_map_sg(dma_dev->dev, &dmatx->sg, 1, DMA_TO_DEVICE) != 1) {
674 uap->dmatx.queued = false;
675 dev_dbg(uap->port.dev, "unable to map TX DMA\n");
676 return -EBUSY;
677 }
678
Alexandre Bounine16052822012-03-08 16:11:18 -0500679 desc = dmaengine_prep_slave_sg(chan, &dmatx->sg, 1, DMA_MEM_TO_DEV,
Russell King68b65f72010-12-22 17:24:39 +0000680 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
681 if (!desc) {
682 dma_unmap_sg(dma_dev->dev, &dmatx->sg, 1, DMA_TO_DEVICE);
683 uap->dmatx.queued = false;
684 /*
685 * If DMA cannot be used right now, we complete this
686 * transaction via IRQ and let the TTY layer retry.
687 */
688 dev_dbg(uap->port.dev, "TX DMA busy\n");
689 return -EBUSY;
690 }
691
692 /* Some data to go along to the callback */
693 desc->callback = pl011_dma_tx_callback;
694 desc->callback_param = uap;
695
696 /* All errors should happen at prepare time */
697 dmaengine_submit(desc);
698
699 /* Fire the DMA transaction */
700 dma_dev->device_issue_pending(chan);
701
702 uap->dmacr |= UART011_TXDMAE;
Jun Nie7b753f32015-07-31 15:49:16 +0800703 pl011_writew(uap, uap->dmacr, REG_DMACR);
Russell King68b65f72010-12-22 17:24:39 +0000704 uap->dmatx.queued = true;
705
706 /*
707 * Now we know that DMA will fire, so advance the ring buffer
708 * with the stuff we just dispatched.
709 */
710 xmit->tail = (xmit->tail + count) & (UART_XMIT_SIZE - 1);
711 uap->port.icount.tx += count;
712
713 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
714 uart_write_wakeup(&uap->port);
715
716 return 1;
717}
718
719/*
720 * We received a transmit interrupt without a pending X-char but with
721 * pending characters.
722 * Locking: called with port lock held and IRQs disabled.
723 * Returns:
724 * false if we want to use PIO to transmit
725 * true if we queued a DMA buffer
726 */
727static bool pl011_dma_tx_irq(struct uart_amba_port *uap)
728{
Linus Walleijead76f322011-02-24 13:21:08 +0100729 if (!uap->using_tx_dma)
Russell King68b65f72010-12-22 17:24:39 +0000730 return false;
731
732 /*
733 * If we already have a TX buffer queued, but received a
734 * TX interrupt, it will be because we've just sent an X-char.
735 * Ensure the TX DMA is enabled and the TX IRQ is disabled.
736 */
737 if (uap->dmatx.queued) {
738 uap->dmacr |= UART011_TXDMAE;
Jun Nie7b753f32015-07-31 15:49:16 +0800739 pl011_writew(uap, uap->dmacr, REG_DMACR);
Russell King68b65f72010-12-22 17:24:39 +0000740 uap->im &= ~UART011_TXIM;
Jun Nie7b753f32015-07-31 15:49:16 +0800741 pl011_writew(uap, uap->im, REG_IMSC);
Russell King68b65f72010-12-22 17:24:39 +0000742 return true;
743 }
744
745 /*
746 * We don't have a TX buffer queued, so try to queue one.
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300747 * If we successfully queued a buffer, mask the TX IRQ.
Russell King68b65f72010-12-22 17:24:39 +0000748 */
749 if (pl011_dma_tx_refill(uap) > 0) {
750 uap->im &= ~UART011_TXIM;
Jun Nie7b753f32015-07-31 15:49:16 +0800751 pl011_writew(uap, uap->im, REG_IMSC);
Russell King68b65f72010-12-22 17:24:39 +0000752 return true;
753 }
754 return false;
755}
756
757/*
758 * Stop the DMA transmit (eg, due to received XOFF).
759 * Locking: called with port lock held and IRQs disabled.
760 */
761static inline void pl011_dma_tx_stop(struct uart_amba_port *uap)
762{
763 if (uap->dmatx.queued) {
764 uap->dmacr &= ~UART011_TXDMAE;
Jun Nie7b753f32015-07-31 15:49:16 +0800765 pl011_writew(uap, uap->dmacr, REG_DMACR);
Russell King68b65f72010-12-22 17:24:39 +0000766 }
767}
768
769/*
770 * Try to start a DMA transmit, or in the case of an XON/OFF
771 * character queued for send, try to get that character out ASAP.
772 * Locking: called with port lock held and IRQs disabled.
773 * Returns:
774 * false if we want the TX IRQ to be enabled
775 * true if we have a buffer queued
776 */
777static inline bool pl011_dma_tx_start(struct uart_amba_port *uap)
778{
779 u16 dmacr;
780
Linus Walleijead76f322011-02-24 13:21:08 +0100781 if (!uap->using_tx_dma)
Russell King68b65f72010-12-22 17:24:39 +0000782 return false;
783
784 if (!uap->port.x_char) {
785 /* no X-char, try to push chars out in DMA mode */
786 bool ret = true;
787
788 if (!uap->dmatx.queued) {
789 if (pl011_dma_tx_refill(uap) > 0) {
790 uap->im &= ~UART011_TXIM;
Jun Nie7b753f32015-07-31 15:49:16 +0800791 pl011_writew(uap, uap->im, REG_IMSC);
Dave Martin734745c2015-03-04 12:27:33 +0000792 } else
Russell King68b65f72010-12-22 17:24:39 +0000793 ret = false;
Russell King68b65f72010-12-22 17:24:39 +0000794 } else if (!(uap->dmacr & UART011_TXDMAE)) {
795 uap->dmacr |= UART011_TXDMAE;
Jun Nie7b753f32015-07-31 15:49:16 +0800796 pl011_writew(uap, uap->dmacr, REG_DMACR);
Russell King68b65f72010-12-22 17:24:39 +0000797 }
798 return ret;
799 }
800
801 /*
802 * We have an X-char to send. Disable DMA to prevent it loading
803 * the TX fifo, and then see if we can stuff it into the FIFO.
804 */
805 dmacr = uap->dmacr;
806 uap->dmacr &= ~UART011_TXDMAE;
Jun Nie7b753f32015-07-31 15:49:16 +0800807 pl011_writew(uap, uap->dmacr, REG_DMACR);
Russell King68b65f72010-12-22 17:24:39 +0000808
Jun Nie7b753f32015-07-31 15:49:16 +0800809 if (pl011_readw(uap, REG_FR) & UART01x_FR_TXFF) {
Russell King68b65f72010-12-22 17:24:39 +0000810 /*
811 * No space in the FIFO, so enable the transmit interrupt
812 * so we know when there is space. Note that once we've
813 * loaded the character, we should just re-enable DMA.
814 */
815 return false;
816 }
817
Jun Nie7b753f32015-07-31 15:49:16 +0800818 pl011_writew(uap, uap->port.x_char, REG_DR);
Russell King68b65f72010-12-22 17:24:39 +0000819 uap->port.icount.tx++;
820 uap->port.x_char = 0;
821
822 /* Success - restore the DMA state */
823 uap->dmacr = dmacr;
Jun Nie7b753f32015-07-31 15:49:16 +0800824 pl011_writew(uap, dmacr, REG_DMACR);
Russell King68b65f72010-12-22 17:24:39 +0000825
826 return true;
827}
828
829/*
830 * Flush the transmit buffer.
831 * Locking: called with port lock held and IRQs disabled.
832 */
833static void pl011_dma_flush_buffer(struct uart_port *port)
Fabio Estevamb83286b2013-08-09 17:58:51 -0300834__releases(&uap->port.lock)
835__acquires(&uap->port.lock)
Russell King68b65f72010-12-22 17:24:39 +0000836{
Daniel Thompsona5820c22014-09-03 12:51:55 +0100837 struct uart_amba_port *uap =
838 container_of(port, struct uart_amba_port, port);
Russell King68b65f72010-12-22 17:24:39 +0000839
Linus Walleijead76f322011-02-24 13:21:08 +0100840 if (!uap->using_tx_dma)
Russell King68b65f72010-12-22 17:24:39 +0000841 return;
842
843 /* Avoid deadlock with the DMA engine callback */
844 spin_unlock(&uap->port.lock);
845 dmaengine_terminate_all(uap->dmatx.chan);
846 spin_lock(&uap->port.lock);
847 if (uap->dmatx.queued) {
848 dma_unmap_sg(uap->dmatx.chan->device->dev, &uap->dmatx.sg, 1,
849 DMA_TO_DEVICE);
850 uap->dmatx.queued = false;
851 uap->dmacr &= ~UART011_TXDMAE;
Jun Nie7b753f32015-07-31 15:49:16 +0800852 pl011_writew(uap, uap->dmacr, REG_DMACR);
Russell King68b65f72010-12-22 17:24:39 +0000853 }
854}
855
Linus Walleijead76f322011-02-24 13:21:08 +0100856static void pl011_dma_rx_callback(void *data);
857
858static int pl011_dma_rx_trigger_dma(struct uart_amba_port *uap)
859{
860 struct dma_chan *rxchan = uap->dmarx.chan;
Linus Walleijead76f322011-02-24 13:21:08 +0100861 struct pl011_dmarx_data *dmarx = &uap->dmarx;
862 struct dma_async_tx_descriptor *desc;
863 struct pl011_sgbuf *sgbuf;
864
865 if (!rxchan)
866 return -EIO;
867
868 /* Start the RX DMA job */
869 sgbuf = uap->dmarx.use_buf_b ?
870 &uap->dmarx.sgbuf_b : &uap->dmarx.sgbuf_a;
Alexandre Bounine16052822012-03-08 16:11:18 -0500871 desc = dmaengine_prep_slave_sg(rxchan, &sgbuf->sg, 1,
Vinod Koula485df42011-10-14 10:47:38 +0530872 DMA_DEV_TO_MEM,
Linus Walleijead76f322011-02-24 13:21:08 +0100873 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
874 /*
875 * If the DMA engine is busy and cannot prepare a
876 * channel, no big deal, the driver will fall back
877 * to interrupt mode as a result of this error code.
878 */
879 if (!desc) {
880 uap->dmarx.running = false;
881 dmaengine_terminate_all(rxchan);
882 return -EBUSY;
883 }
884
885 /* Some data to go along to the callback */
886 desc->callback = pl011_dma_rx_callback;
887 desc->callback_param = uap;
888 dmarx->cookie = dmaengine_submit(desc);
889 dma_async_issue_pending(rxchan);
890
891 uap->dmacr |= UART011_RXDMAE;
Jun Nie7b753f32015-07-31 15:49:16 +0800892 pl011_writew(uap, uap->dmacr, REG_DMACR);
Linus Walleijead76f322011-02-24 13:21:08 +0100893 uap->dmarx.running = true;
894
895 uap->im &= ~UART011_RXIM;
Jun Nie7b753f32015-07-31 15:49:16 +0800896 pl011_writew(uap, uap->im, REG_IMSC);
Linus Walleijead76f322011-02-24 13:21:08 +0100897
898 return 0;
899}
900
901/*
902 * This is called when either the DMA job is complete, or
903 * the FIFO timeout interrupt occurred. This must be called
904 * with the port spinlock uap->port.lock held.
905 */
906static void pl011_dma_rx_chars(struct uart_amba_port *uap,
907 u32 pending, bool use_buf_b,
908 bool readfifo)
909{
Jiri Slaby05c7cd32013-01-03 15:53:04 +0100910 struct tty_port *port = &uap->port.state->port;
Linus Walleijead76f322011-02-24 13:21:08 +0100911 struct pl011_sgbuf *sgbuf = use_buf_b ?
912 &uap->dmarx.sgbuf_b : &uap->dmarx.sgbuf_a;
Linus Walleijead76f322011-02-24 13:21:08 +0100913 int dma_count = 0;
914 u32 fifotaken = 0; /* only used for vdbg() */
915
Chanho Mincb06ff12013-03-27 18:38:11 +0900916 struct pl011_dmarx_data *dmarx = &uap->dmarx;
917 int dmataken = 0;
918
919 if (uap->dmarx.poll_rate) {
920 /* The data can be taken by polling */
921 dmataken = sgbuf->sg.length - dmarx->last_residue;
922 /* Recalculate the pending size */
923 if (pending >= dmataken)
924 pending -= dmataken;
925 }
926
927 /* Pick the remain data from the DMA */
Linus Walleijead76f322011-02-24 13:21:08 +0100928 if (pending) {
Linus Walleijead76f322011-02-24 13:21:08 +0100929
930 /*
931 * First take all chars in the DMA pipe, then look in the FIFO.
932 * Note that tty_insert_flip_buf() tries to take as many chars
933 * as it can.
934 */
Chanho Mincb06ff12013-03-27 18:38:11 +0900935 dma_count = tty_insert_flip_string(port, sgbuf->buf + dmataken,
936 pending);
Linus Walleijead76f322011-02-24 13:21:08 +0100937
938 uap->port.icount.rx += dma_count;
939 if (dma_count < pending)
940 dev_warn(uap->port.dev,
941 "couldn't insert all characters (TTY is full?)\n");
942 }
943
Chanho Mincb06ff12013-03-27 18:38:11 +0900944 /* Reset the last_residue for Rx DMA poll */
945 if (uap->dmarx.poll_rate)
946 dmarx->last_residue = sgbuf->sg.length;
947
Linus Walleijead76f322011-02-24 13:21:08 +0100948 /*
949 * Only continue with trying to read the FIFO if all DMA chars have
950 * been taken first.
951 */
952 if (dma_count == pending && readfifo) {
953 /* Clear any error flags */
Jun Nie7b753f32015-07-31 15:49:16 +0800954 pl011_writew(uap,
955 UART011_OEIS | UART011_BEIS | UART011_PEIS
956 | UART011_FEIS, REG_ICR);
Linus Walleijead76f322011-02-24 13:21:08 +0100957
958 /*
959 * If we read all the DMA'd characters, and we had an
Linus Walleij29772c42011-02-24 13:21:36 +0100960 * incomplete buffer, that could be due to an rx error, or
961 * maybe we just timed out. Read any pending chars and check
962 * the error status.
963 *
964 * Error conditions will only occur in the FIFO, these will
965 * trigger an immediate interrupt and stop the DMA job, so we
966 * will always find the error in the FIFO, never in the DMA
967 * buffer.
Linus Walleijead76f322011-02-24 13:21:08 +0100968 */
Linus Walleij29772c42011-02-24 13:21:36 +0100969 fifotaken = pl011_fifo_to_tty(uap);
Linus Walleijead76f322011-02-24 13:21:08 +0100970 }
971
972 spin_unlock(&uap->port.lock);
973 dev_vdbg(uap->port.dev,
974 "Took %d chars from DMA buffer and %d chars from the FIFO\n",
975 dma_count, fifotaken);
Jiri Slaby2e124b42013-01-03 15:53:06 +0100976 tty_flip_buffer_push(port);
Linus Walleijead76f322011-02-24 13:21:08 +0100977 spin_lock(&uap->port.lock);
978}
979
980static void pl011_dma_rx_irq(struct uart_amba_port *uap)
981{
982 struct pl011_dmarx_data *dmarx = &uap->dmarx;
983 struct dma_chan *rxchan = dmarx->chan;
984 struct pl011_sgbuf *sgbuf = dmarx->use_buf_b ?
985 &dmarx->sgbuf_b : &dmarx->sgbuf_a;
986 size_t pending;
987 struct dma_tx_state state;
988 enum dma_status dmastat;
989
990 /*
991 * Pause the transfer so we can trust the current counter,
992 * do this before we pause the PL011 block, else we may
993 * overflow the FIFO.
994 */
995 if (dmaengine_pause(rxchan))
996 dev_err(uap->port.dev, "unable to pause DMA transfer\n");
997 dmastat = rxchan->device->device_tx_status(rxchan,
998 dmarx->cookie, &state);
999 if (dmastat != DMA_PAUSED)
1000 dev_err(uap->port.dev, "unable to pause DMA transfer\n");
1001
1002 /* Disable RX DMA - incoming data will wait in the FIFO */
1003 uap->dmacr &= ~UART011_RXDMAE;
Jun Nie7b753f32015-07-31 15:49:16 +08001004 pl011_writew(uap, uap->dmacr, REG_DMACR);
Linus Walleijead76f322011-02-24 13:21:08 +01001005 uap->dmarx.running = false;
1006
1007 pending = sgbuf->sg.length - state.residue;
1008 BUG_ON(pending > PL011_DMA_BUFFER_SIZE);
1009 /* Then we terminate the transfer - we now know our residue */
1010 dmaengine_terminate_all(rxchan);
1011
1012 /*
1013 * This will take the chars we have so far and insert
1014 * into the framework.
1015 */
1016 pl011_dma_rx_chars(uap, pending, dmarx->use_buf_b, true);
1017
1018 /* Switch buffer & re-trigger DMA job */
1019 dmarx->use_buf_b = !dmarx->use_buf_b;
1020 if (pl011_dma_rx_trigger_dma(uap)) {
1021 dev_dbg(uap->port.dev, "could not retrigger RX DMA job "
1022 "fall back to interrupt mode\n");
1023 uap->im |= UART011_RXIM;
Jun Nie7b753f32015-07-31 15:49:16 +08001024 pl011_writew(uap, uap->im, REG_IMSC);
Linus Walleijead76f322011-02-24 13:21:08 +01001025 }
1026}
1027
1028static void pl011_dma_rx_callback(void *data)
1029{
1030 struct uart_amba_port *uap = data;
1031 struct pl011_dmarx_data *dmarx = &uap->dmarx;
Chanho Min6dc01aa2012-02-20 10:24:40 +09001032 struct dma_chan *rxchan = dmarx->chan;
Linus Walleijead76f322011-02-24 13:21:08 +01001033 bool lastbuf = dmarx->use_buf_b;
Chanho Min6dc01aa2012-02-20 10:24:40 +09001034 struct pl011_sgbuf *sgbuf = dmarx->use_buf_b ?
1035 &dmarx->sgbuf_b : &dmarx->sgbuf_a;
1036 size_t pending;
1037 struct dma_tx_state state;
Linus Walleijead76f322011-02-24 13:21:08 +01001038 int ret;
1039
1040 /*
1041 * This completion interrupt occurs typically when the
1042 * RX buffer is totally stuffed but no timeout has yet
1043 * occurred. When that happens, we just want the RX
1044 * routine to flush out the secondary DMA buffer while
1045 * we immediately trigger the next DMA job.
1046 */
1047 spin_lock_irq(&uap->port.lock);
Chanho Min6dc01aa2012-02-20 10:24:40 +09001048 /*
1049 * Rx data can be taken by the UART interrupts during
1050 * the DMA irq handler. So we check the residue here.
1051 */
1052 rxchan->device->device_tx_status(rxchan, dmarx->cookie, &state);
1053 pending = sgbuf->sg.length - state.residue;
1054 BUG_ON(pending > PL011_DMA_BUFFER_SIZE);
1055 /* Then we terminate the transfer - we now know our residue */
1056 dmaengine_terminate_all(rxchan);
1057
Linus Walleijead76f322011-02-24 13:21:08 +01001058 uap->dmarx.running = false;
1059 dmarx->use_buf_b = !lastbuf;
1060 ret = pl011_dma_rx_trigger_dma(uap);
1061
Chanho Min6dc01aa2012-02-20 10:24:40 +09001062 pl011_dma_rx_chars(uap, pending, lastbuf, false);
Linus Walleijead76f322011-02-24 13:21:08 +01001063 spin_unlock_irq(&uap->port.lock);
1064 /*
1065 * Do this check after we picked the DMA chars so we don't
1066 * get some IRQ immediately from RX.
1067 */
1068 if (ret) {
1069 dev_dbg(uap->port.dev, "could not retrigger RX DMA job "
1070 "fall back to interrupt mode\n");
1071 uap->im |= UART011_RXIM;
Jun Nie7b753f32015-07-31 15:49:16 +08001072 pl011_writew(uap, uap->im, REG_IMSC);
Linus Walleijead76f322011-02-24 13:21:08 +01001073 }
1074}
1075
1076/*
1077 * Stop accepting received characters, when we're shutting down or
1078 * suspending this port.
1079 * Locking: called with port lock held and IRQs disabled.
1080 */
1081static inline void pl011_dma_rx_stop(struct uart_amba_port *uap)
1082{
1083 /* FIXME. Just disable the DMA enable */
1084 uap->dmacr &= ~UART011_RXDMAE;
Jun Nie7b753f32015-07-31 15:49:16 +08001085 pl011_writew(uap, uap->dmacr, REG_DMACR);
Linus Walleijead76f322011-02-24 13:21:08 +01001086}
Russell King68b65f72010-12-22 17:24:39 +00001087
Chanho Mincb06ff12013-03-27 18:38:11 +09001088/*
1089 * Timer handler for Rx DMA polling.
1090 * Every polling, It checks the residue in the dma buffer and transfer
1091 * data to the tty. Also, last_residue is updated for the next polling.
1092 */
1093static void pl011_dma_rx_poll(unsigned long args)
1094{
1095 struct uart_amba_port *uap = (struct uart_amba_port *)args;
1096 struct tty_port *port = &uap->port.state->port;
1097 struct pl011_dmarx_data *dmarx = &uap->dmarx;
1098 struct dma_chan *rxchan = uap->dmarx.chan;
1099 unsigned long flags = 0;
1100 unsigned int dmataken = 0;
1101 unsigned int size = 0;
1102 struct pl011_sgbuf *sgbuf;
1103 int dma_count;
1104 struct dma_tx_state state;
1105
1106 sgbuf = dmarx->use_buf_b ? &uap->dmarx.sgbuf_b : &uap->dmarx.sgbuf_a;
1107 rxchan->device->device_tx_status(rxchan, dmarx->cookie, &state);
1108 if (likely(state.residue < dmarx->last_residue)) {
1109 dmataken = sgbuf->sg.length - dmarx->last_residue;
1110 size = dmarx->last_residue - state.residue;
1111 dma_count = tty_insert_flip_string(port, sgbuf->buf + dmataken,
1112 size);
1113 if (dma_count == size)
1114 dmarx->last_residue = state.residue;
1115 dmarx->last_jiffies = jiffies;
1116 }
1117 tty_flip_buffer_push(port);
1118
1119 /*
1120 * If no data is received in poll_timeout, the driver will fall back
1121 * to interrupt mode. We will retrigger DMA at the first interrupt.
1122 */
1123 if (jiffies_to_msecs(jiffies - dmarx->last_jiffies)
1124 > uap->dmarx.poll_timeout) {
1125
1126 spin_lock_irqsave(&uap->port.lock, flags);
1127 pl011_dma_rx_stop(uap);
Guennadi Liakhovetskic25a1ad2013-12-10 14:54:47 +01001128 uap->im |= UART011_RXIM;
Jun Nie7b753f32015-07-31 15:49:16 +08001129 pl011_writew(uap, uap->im, REG_IMSC);
Chanho Mincb06ff12013-03-27 18:38:11 +09001130 spin_unlock_irqrestore(&uap->port.lock, flags);
1131
1132 uap->dmarx.running = false;
1133 dmaengine_terminate_all(rxchan);
1134 del_timer(&uap->dmarx.timer);
1135 } else {
1136 mod_timer(&uap->dmarx.timer,
1137 jiffies + msecs_to_jiffies(uap->dmarx.poll_rate));
1138 }
1139}
1140
Russell King68b65f72010-12-22 17:24:39 +00001141static void pl011_dma_startup(struct uart_amba_port *uap)
1142{
Linus Walleijead76f322011-02-24 13:21:08 +01001143 int ret;
1144
Jorge Ramirez-Ortiz1c9be312015-03-06 13:05:40 -05001145 if (!uap->dma_probed)
1146 pl011_dma_probe(uap);
1147
Russell King68b65f72010-12-22 17:24:39 +00001148 if (!uap->dmatx.chan)
1149 return;
1150
Andrew Jackson4c0be452014-11-07 14:14:35 +00001151 uap->dmatx.buf = kmalloc(PL011_DMA_BUFFER_SIZE, GFP_KERNEL | __GFP_DMA);
Russell King68b65f72010-12-22 17:24:39 +00001152 if (!uap->dmatx.buf) {
1153 dev_err(uap->port.dev, "no memory for DMA TX buffer\n");
1154 uap->port.fifosize = uap->fifosize;
1155 return;
1156 }
1157
1158 sg_init_one(&uap->dmatx.sg, uap->dmatx.buf, PL011_DMA_BUFFER_SIZE);
1159
1160 /* The DMA buffer is now the FIFO the TTY subsystem can use */
1161 uap->port.fifosize = PL011_DMA_BUFFER_SIZE;
Linus Walleijead76f322011-02-24 13:21:08 +01001162 uap->using_tx_dma = true;
Russell King68b65f72010-12-22 17:24:39 +00001163
Linus Walleijead76f322011-02-24 13:21:08 +01001164 if (!uap->dmarx.chan)
1165 goto skip_rx;
1166
1167 /* Allocate and map DMA RX buffers */
1168 ret = pl011_sgbuf_init(uap->dmarx.chan, &uap->dmarx.sgbuf_a,
1169 DMA_FROM_DEVICE);
1170 if (ret) {
1171 dev_err(uap->port.dev, "failed to init DMA %s: %d\n",
1172 "RX buffer A", ret);
1173 goto skip_rx;
1174 }
1175
1176 ret = pl011_sgbuf_init(uap->dmarx.chan, &uap->dmarx.sgbuf_b,
1177 DMA_FROM_DEVICE);
1178 if (ret) {
1179 dev_err(uap->port.dev, "failed to init DMA %s: %d\n",
1180 "RX buffer B", ret);
1181 pl011_sgbuf_free(uap->dmarx.chan, &uap->dmarx.sgbuf_a,
1182 DMA_FROM_DEVICE);
1183 goto skip_rx;
1184 }
1185
1186 uap->using_rx_dma = true;
1187
1188skip_rx:
Russell King68b65f72010-12-22 17:24:39 +00001189 /* Turn on DMA error (RX/TX will be enabled on demand) */
1190 uap->dmacr |= UART011_DMAONERR;
Jun Nie7b753f32015-07-31 15:49:16 +08001191 pl011_writew(uap, uap->dmacr, REG_DMACR);
Russell King38d62432010-12-22 17:59:16 +00001192
1193 /*
1194 * ST Micro variants has some specific dma burst threshold
1195 * compensation. Set this to 16 bytes, so burst will only
1196 * be issued above/below 16 bytes.
1197 */
1198 if (uap->vendor->dma_threshold)
Jun Nie7b753f32015-07-31 15:49:16 +08001199 pl011_writew(uap,
1200 ST_UART011_DMAWM_RX_16 | ST_UART011_DMAWM_TX_16,
1201 REG_ST_DMAWM);
Linus Walleijead76f322011-02-24 13:21:08 +01001202
1203 if (uap->using_rx_dma) {
1204 if (pl011_dma_rx_trigger_dma(uap))
1205 dev_dbg(uap->port.dev, "could not trigger initial "
1206 "RX DMA job, fall back to interrupt mode\n");
Chanho Mincb06ff12013-03-27 18:38:11 +09001207 if (uap->dmarx.poll_rate) {
1208 init_timer(&(uap->dmarx.timer));
1209 uap->dmarx.timer.function = pl011_dma_rx_poll;
1210 uap->dmarx.timer.data = (unsigned long)uap;
1211 mod_timer(&uap->dmarx.timer,
1212 jiffies +
1213 msecs_to_jiffies(uap->dmarx.poll_rate));
1214 uap->dmarx.last_residue = PL011_DMA_BUFFER_SIZE;
1215 uap->dmarx.last_jiffies = jiffies;
1216 }
Linus Walleijead76f322011-02-24 13:21:08 +01001217 }
Russell King68b65f72010-12-22 17:24:39 +00001218}
1219
1220static void pl011_dma_shutdown(struct uart_amba_port *uap)
1221{
Linus Walleijead76f322011-02-24 13:21:08 +01001222 if (!(uap->using_tx_dma || uap->using_rx_dma))
Russell King68b65f72010-12-22 17:24:39 +00001223 return;
1224
1225 /* Disable RX and TX DMA */
Jun Nie8cd90e52015-07-31 15:49:19 +08001226 while (pl011_readw(uap, REG_FR) & uap->fr_busy)
Russell King68b65f72010-12-22 17:24:39 +00001227 barrier();
1228
1229 spin_lock_irq(&uap->port.lock);
1230 uap->dmacr &= ~(UART011_DMAONERR | UART011_RXDMAE | UART011_TXDMAE);
Jun Nie7b753f32015-07-31 15:49:16 +08001231 pl011_writew(uap, uap->dmacr, REG_DMACR);
Russell King68b65f72010-12-22 17:24:39 +00001232 spin_unlock_irq(&uap->port.lock);
1233
Linus Walleijead76f322011-02-24 13:21:08 +01001234 if (uap->using_tx_dma) {
1235 /* In theory, this should already be done by pl011_dma_flush_buffer */
1236 dmaengine_terminate_all(uap->dmatx.chan);
1237 if (uap->dmatx.queued) {
1238 dma_unmap_sg(uap->dmatx.chan->device->dev, &uap->dmatx.sg, 1,
1239 DMA_TO_DEVICE);
1240 uap->dmatx.queued = false;
1241 }
1242
1243 kfree(uap->dmatx.buf);
1244 uap->using_tx_dma = false;
Russell King68b65f72010-12-22 17:24:39 +00001245 }
1246
Linus Walleijead76f322011-02-24 13:21:08 +01001247 if (uap->using_rx_dma) {
1248 dmaengine_terminate_all(uap->dmarx.chan);
1249 /* Clean up the RX DMA */
1250 pl011_sgbuf_free(uap->dmarx.chan, &uap->dmarx.sgbuf_a, DMA_FROM_DEVICE);
1251 pl011_sgbuf_free(uap->dmarx.chan, &uap->dmarx.sgbuf_b, DMA_FROM_DEVICE);
Chanho Mincb06ff12013-03-27 18:38:11 +09001252 if (uap->dmarx.poll_rate)
1253 del_timer_sync(&uap->dmarx.timer);
Linus Walleijead76f322011-02-24 13:21:08 +01001254 uap->using_rx_dma = false;
1255 }
Russell King68b65f72010-12-22 17:24:39 +00001256}
1257
Linus Walleijead76f322011-02-24 13:21:08 +01001258static inline bool pl011_dma_rx_available(struct uart_amba_port *uap)
1259{
1260 return uap->using_rx_dma;
1261}
1262
1263static inline bool pl011_dma_rx_running(struct uart_amba_port *uap)
1264{
1265 return uap->using_rx_dma && uap->dmarx.running;
1266}
1267
Russell King68b65f72010-12-22 17:24:39 +00001268#else
1269/* Blank functions if the DMA engine is not available */
Jorge Ramirez-Ortiz1c9be312015-03-06 13:05:40 -05001270static inline void pl011_dma_probe(struct uart_amba_port *uap)
Russell King68b65f72010-12-22 17:24:39 +00001271{
1272}
1273
1274static inline void pl011_dma_remove(struct uart_amba_port *uap)
1275{
1276}
1277
1278static inline void pl011_dma_startup(struct uart_amba_port *uap)
1279{
1280}
1281
1282static inline void pl011_dma_shutdown(struct uart_amba_port *uap)
1283{
1284}
1285
1286static inline bool pl011_dma_tx_irq(struct uart_amba_port *uap)
1287{
1288 return false;
1289}
1290
1291static inline void pl011_dma_tx_stop(struct uart_amba_port *uap)
1292{
1293}
1294
1295static inline bool pl011_dma_tx_start(struct uart_amba_port *uap)
1296{
1297 return false;
1298}
1299
Linus Walleijead76f322011-02-24 13:21:08 +01001300static inline void pl011_dma_rx_irq(struct uart_amba_port *uap)
1301{
1302}
1303
1304static inline void pl011_dma_rx_stop(struct uart_amba_port *uap)
1305{
1306}
1307
1308static inline int pl011_dma_rx_trigger_dma(struct uart_amba_port *uap)
1309{
1310 return -EIO;
1311}
1312
1313static inline bool pl011_dma_rx_available(struct uart_amba_port *uap)
1314{
1315 return false;
1316}
1317
1318static inline bool pl011_dma_rx_running(struct uart_amba_port *uap)
1319{
1320 return false;
1321}
1322
Russell King68b65f72010-12-22 17:24:39 +00001323#define pl011_dma_flush_buffer NULL
1324#endif
1325
Russell Kingb129a8c2005-08-31 10:12:14 +01001326static void pl011_stop_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001328 struct uart_amba_port *uap =
1329 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330
1331 uap->im &= ~UART011_TXIM;
Jun Nie7b753f32015-07-31 15:49:16 +08001332 pl011_writew(uap, uap->im, REG_IMSC);
Russell King68b65f72010-12-22 17:24:39 +00001333 pl011_dma_tx_stop(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334}
1335
Dave Martin1e84d222015-04-27 16:49:05 +01001336static void pl011_tx_chars(struct uart_amba_port *uap, bool from_irq);
Dave Martin734745c2015-03-04 12:27:33 +00001337
1338/* Start TX with programmed I/O only (no DMA) */
1339static void pl011_start_tx_pio(struct uart_amba_port *uap)
1340{
1341 uap->im |= UART011_TXIM;
Jun Nie7b753f32015-07-31 15:49:16 +08001342 pl011_writew(uap, uap->im, REG_IMSC);
Dave Martin1e84d222015-04-27 16:49:05 +01001343 pl011_tx_chars(uap, false);
Dave Martin734745c2015-03-04 12:27:33 +00001344}
1345
Russell Kingb129a8c2005-08-31 10:12:14 +01001346static void pl011_start_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001348 struct uart_amba_port *uap =
1349 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350
Dave Martin734745c2015-03-04 12:27:33 +00001351 if (!pl011_dma_tx_start(uap))
1352 pl011_start_tx_pio(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353}
1354
1355static void pl011_stop_rx(struct uart_port *port)
1356{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001357 struct uart_amba_port *uap =
1358 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359
1360 uap->im &= ~(UART011_RXIM|UART011_RTIM|UART011_FEIM|
1361 UART011_PEIM|UART011_BEIM|UART011_OEIM);
Jun Nie7b753f32015-07-31 15:49:16 +08001362 pl011_writew(uap, uap->im, REG_IMSC);
Linus Walleijead76f322011-02-24 13:21:08 +01001363
1364 pl011_dma_rx_stop(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365}
1366
1367static void pl011_enable_ms(struct uart_port *port)
1368{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001369 struct uart_amba_port *uap =
1370 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371
1372 uap->im |= UART011_RIMIM|UART011_CTSMIM|UART011_DCDMIM|UART011_DSRMIM;
Jun Nie7b753f32015-07-31 15:49:16 +08001373 pl011_writew(uap, uap->im, REG_IMSC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374}
1375
David Howells7d12e782006-10-05 14:55:46 +01001376static void pl011_rx_chars(struct uart_amba_port *uap)
Fabio Estevamb83286b2013-08-09 17:58:51 -03001377__releases(&uap->port.lock)
1378__acquires(&uap->port.lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379{
Linus Walleij29772c42011-02-24 13:21:36 +01001380 pl011_fifo_to_tty(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381
Thomas Gleixner2389b272007-05-29 21:53:50 +01001382 spin_unlock(&uap->port.lock);
Jiri Slaby2e124b42013-01-03 15:53:06 +01001383 tty_flip_buffer_push(&uap->port.state->port);
Linus Walleijead76f322011-02-24 13:21:08 +01001384 /*
1385 * If we were temporarily out of DMA mode for a while,
1386 * attempt to switch back to DMA mode again.
1387 */
1388 if (pl011_dma_rx_available(uap)) {
1389 if (pl011_dma_rx_trigger_dma(uap)) {
1390 dev_dbg(uap->port.dev, "could not trigger RX DMA job "
1391 "fall back to interrupt mode again\n");
1392 uap->im |= UART011_RXIM;
Jun Nie7b753f32015-07-31 15:49:16 +08001393 pl011_writew(uap, uap->im, REG_IMSC);
Chanho Mincb06ff12013-03-27 18:38:11 +09001394 } else {
Chanho Min89fa28d2013-04-03 11:10:37 +09001395#ifdef CONFIG_DMA_ENGINE
Chanho Mincb06ff12013-03-27 18:38:11 +09001396 /* Start Rx DMA poll */
1397 if (uap->dmarx.poll_rate) {
1398 uap->dmarx.last_jiffies = jiffies;
1399 uap->dmarx.last_residue = PL011_DMA_BUFFER_SIZE;
1400 mod_timer(&uap->dmarx.timer,
1401 jiffies +
1402 msecs_to_jiffies(uap->dmarx.poll_rate));
1403 }
Chanho Min89fa28d2013-04-03 11:10:37 +09001404#endif
Chanho Mincb06ff12013-03-27 18:38:11 +09001405 }
Linus Walleijead76f322011-02-24 13:21:08 +01001406 }
Thomas Gleixner2389b272007-05-29 21:53:50 +01001407 spin_lock(&uap->port.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408}
1409
Dave Martin1e84d222015-04-27 16:49:05 +01001410static bool pl011_tx_char(struct uart_amba_port *uap, unsigned char c,
1411 bool from_irq)
Dave Martin734745c2015-03-04 12:27:33 +00001412{
Dave Martin1e84d222015-04-27 16:49:05 +01001413 if (unlikely(!from_irq) &&
Jun Nie7b753f32015-07-31 15:49:16 +08001414 pl011_readw(uap, REG_FR) & UART01x_FR_TXFF)
Dave Martin1e84d222015-04-27 16:49:05 +01001415 return false; /* unable to transmit character */
1416
Jun Nie7b753f32015-07-31 15:49:16 +08001417 pl011_writew(uap, c, REG_DR);
Dave Martin734745c2015-03-04 12:27:33 +00001418 uap->port.icount.tx++;
1419
Dave Martin1e84d222015-04-27 16:49:05 +01001420 return true;
Dave Martin734745c2015-03-04 12:27:33 +00001421}
1422
Dave Martin1e84d222015-04-27 16:49:05 +01001423static void pl011_tx_chars(struct uart_amba_port *uap, bool from_irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424{
Alan Coxebd2c8f2009-09-19 13:13:28 -07001425 struct circ_buf *xmit = &uap->port.state->xmit;
Dave Martin1e84d222015-04-27 16:49:05 +01001426 int count = uap->fifosize >> 1;
Dave Martin734745c2015-03-04 12:27:33 +00001427
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 if (uap->port.x_char) {
Dave Martin1e84d222015-04-27 16:49:05 +01001429 if (!pl011_tx_char(uap, uap->port.x_char, from_irq))
1430 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 uap->port.x_char = 0;
Dave Martin734745c2015-03-04 12:27:33 +00001432 --count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 }
1434 if (uart_circ_empty(xmit) || uart_tx_stopped(&uap->port)) {
Russell Kingb129a8c2005-08-31 10:12:14 +01001435 pl011_stop_tx(&uap->port);
Dave Martin1e84d222015-04-27 16:49:05 +01001436 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 }
1438
Russell King68b65f72010-12-22 17:24:39 +00001439 /* If we are using DMA mode, try to send some characters. */
1440 if (pl011_dma_tx_irq(uap))
Dave Martin1e84d222015-04-27 16:49:05 +01001441 return;
Russell King68b65f72010-12-22 17:24:39 +00001442
Dave Martin1e84d222015-04-27 16:49:05 +01001443 do {
1444 if (likely(from_irq) && count-- == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 break;
Dave Martin1e84d222015-04-27 16:49:05 +01001446
1447 if (!pl011_tx_char(uap, xmit->buf[xmit->tail], from_irq))
1448 break;
1449
1450 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
1451 } while (!uart_circ_empty(xmit));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452
1453 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
1454 uart_write_wakeup(&uap->port);
1455
Dave Martin1e84d222015-04-27 16:49:05 +01001456 if (uart_circ_empty(xmit))
Russell Kingb129a8c2005-08-31 10:12:14 +01001457 pl011_stop_tx(&uap->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458}
1459
1460static void pl011_modem_status(struct uart_amba_port *uap)
1461{
1462 unsigned int status, delta;
1463
Jun Nie7b753f32015-07-31 15:49:16 +08001464 status = pl011_readw(uap, REG_FR) & UART01x_FR_MODEM_ANY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465
1466 delta = status ^ uap->old_status;
1467 uap->old_status = status;
1468
1469 if (!delta)
1470 return;
1471
1472 if (delta & UART01x_FR_DCD)
1473 uart_handle_dcd_change(&uap->port, status & UART01x_FR_DCD);
1474
Jun Nie8cd90e52015-07-31 15:49:19 +08001475 if (delta & uap->fr_dsr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 uap->port.icount.dsr++;
1477
Jun Nie8cd90e52015-07-31 15:49:19 +08001478 if (delta & uap->fr_cts)
1479 uart_handle_cts_change(&uap->port, status & uap->fr_cts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480
Alan Coxbdc04e32009-09-19 13:13:31 -07001481 wake_up_interruptible(&uap->port.state->port.delta_msr_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482}
1483
Andre Przywara9c4ef4b2015-05-21 17:26:20 +01001484static void check_apply_cts_event_workaround(struct uart_amba_port *uap)
1485{
1486 unsigned int dummy_read;
1487
1488 if (!uap->vendor->cts_event_workaround)
1489 return;
1490
1491 /* workaround to make sure that all bits are unlocked.. */
Jun Nie7b753f32015-07-31 15:49:16 +08001492 pl011_writew(uap, 0x00, REG_ICR);
Andre Przywara9c4ef4b2015-05-21 17:26:20 +01001493
1494 /*
1495 * WA: introduce 26ns(1 uart clk) delay before W1C;
1496 * single apb access will incur 2 pclk(133.12Mhz) delay,
1497 * so add 2 dummy reads
1498 */
Jun Nie7b753f32015-07-31 15:49:16 +08001499 dummy_read = pl011_readw(uap, REG_ICR);
1500 dummy_read = pl011_readw(uap, REG_ICR);
Andre Przywara9c4ef4b2015-05-21 17:26:20 +01001501}
1502
David Howells7d12e782006-10-05 14:55:46 +01001503static irqreturn_t pl011_int(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504{
1505 struct uart_amba_port *uap = dev_id;
Russell King963cc982010-12-22 17:16:09 +00001506 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 unsigned int status, pass_counter = AMBA_ISR_PASS_LIMIT;
Andre Przywara075167e2015-05-21 17:26:19 +01001508 u16 imsc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 int handled = 0;
1510
Russell King963cc982010-12-22 17:16:09 +00001511 spin_lock_irqsave(&uap->port.lock, flags);
Jun Nie7b753f32015-07-31 15:49:16 +08001512 imsc = pl011_readw(uap, REG_IMSC);
1513 status = pl011_readw(uap, REG_RIS) & imsc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 if (status) {
1515 do {
Andre Przywara9c4ef4b2015-05-21 17:26:20 +01001516 check_apply_cts_event_workaround(uap);
Jun Nie7b753f32015-07-31 15:49:16 +08001517 pl011_writew(uap, status & ~(UART011_TXIS|UART011_RTIS|
1518 UART011_RXIS), REG_ICR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519
Linus Walleijead76f322011-02-24 13:21:08 +01001520 if (status & (UART011_RTIS|UART011_RXIS)) {
1521 if (pl011_dma_rx_running(uap))
1522 pl011_dma_rx_irq(uap);
1523 else
1524 pl011_rx_chars(uap);
1525 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 if (status & (UART011_DSRMIS|UART011_DCDMIS|
1527 UART011_CTSMIS|UART011_RIMIS))
1528 pl011_modem_status(uap);
Dave Martin1e84d222015-04-27 16:49:05 +01001529 if (status & UART011_TXIS)
1530 pl011_tx_chars(uap, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531
Rajanikanth H.V4fd06902012-03-26 11:17:02 +02001532 if (pass_counter-- == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 break;
1534
Jun Nie7b753f32015-07-31 15:49:16 +08001535 status = pl011_readw(uap, REG_RIS) & imsc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 } while (status != 0);
1537 handled = 1;
1538 }
1539
Russell King963cc982010-12-22 17:16:09 +00001540 spin_unlock_irqrestore(&uap->port.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541
1542 return IRQ_RETVAL(handled);
1543}
1544
Linus Walleije643f872012-06-17 15:44:19 +02001545static unsigned int pl011_tx_empty(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001547 struct uart_amba_port *uap =
1548 container_of(port, struct uart_amba_port, port);
Jun Nie7b753f32015-07-31 15:49:16 +08001549 unsigned int status = pl011_readw(uap, REG_FR);
Jun Nie8cd90e52015-07-31 15:49:19 +08001550 return status & (uap->fr_busy|UART01x_FR_TXFF) ? 0 : TIOCSER_TEMT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551}
1552
Linus Walleije643f872012-06-17 15:44:19 +02001553static unsigned int pl011_get_mctrl(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001555 struct uart_amba_port *uap =
1556 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 unsigned int result = 0;
Jun Nie7b753f32015-07-31 15:49:16 +08001558 unsigned int status = pl011_readw(uap, REG_FR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559
Jiri Slaby5159f402007-10-18 23:40:31 -07001560#define TIOCMBIT(uartbit, tiocmbit) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 if (status & uartbit) \
1562 result |= tiocmbit
1563
Jiri Slaby5159f402007-10-18 23:40:31 -07001564 TIOCMBIT(UART01x_FR_DCD, TIOCM_CAR);
Jun Nie8cd90e52015-07-31 15:49:19 +08001565 TIOCMBIT(uap->fr_dsr, TIOCM_DSR);
1566 TIOCMBIT(uap->fr_cts, TIOCM_CTS);
1567 TIOCMBIT(uap->fr_ri, TIOCM_RNG);
Jiri Slaby5159f402007-10-18 23:40:31 -07001568#undef TIOCMBIT
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 return result;
1570}
1571
1572static void pl011_set_mctrl(struct uart_port *port, unsigned int mctrl)
1573{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001574 struct uart_amba_port *uap =
1575 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 unsigned int cr;
1577
Jun Nie7b753f32015-07-31 15:49:16 +08001578 cr = pl011_readw(uap, REG_CR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579
Jiri Slaby5159f402007-10-18 23:40:31 -07001580#define TIOCMBIT(tiocmbit, uartbit) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 if (mctrl & tiocmbit) \
1582 cr |= uartbit; \
1583 else \
1584 cr &= ~uartbit
1585
Jiri Slaby5159f402007-10-18 23:40:31 -07001586 TIOCMBIT(TIOCM_RTS, UART011_CR_RTS);
1587 TIOCMBIT(TIOCM_DTR, UART011_CR_DTR);
1588 TIOCMBIT(TIOCM_OUT1, UART011_CR_OUT1);
1589 TIOCMBIT(TIOCM_OUT2, UART011_CR_OUT2);
1590 TIOCMBIT(TIOCM_LOOP, UART011_CR_LBE);
Rabin Vincent3b438162010-02-12 06:43:11 +01001591
1592 if (uap->autorts) {
1593 /* We need to disable auto-RTS if we want to turn RTS off */
1594 TIOCMBIT(TIOCM_RTS, UART011_CR_RTSEN);
1595 }
Jiri Slaby5159f402007-10-18 23:40:31 -07001596#undef TIOCMBIT
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597
Jun Nie7b753f32015-07-31 15:49:16 +08001598 pl011_writew(uap, cr, REG_CR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599}
1600
1601static void pl011_break_ctl(struct uart_port *port, int break_state)
1602{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001603 struct uart_amba_port *uap =
1604 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 unsigned long flags;
1606 unsigned int lcr_h;
1607
1608 spin_lock_irqsave(&uap->port.lock, flags);
Jun Nie7b753f32015-07-31 15:49:16 +08001609 lcr_h = pl011_readw(uap, uap->lcrh_tx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 if (break_state == -1)
1611 lcr_h |= UART01x_LCRH_BRK;
1612 else
1613 lcr_h &= ~UART01x_LCRH_BRK;
Jun Nie7b753f32015-07-31 15:49:16 +08001614 pl011_writew(uap, lcr_h, uap->lcrh_tx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 spin_unlock_irqrestore(&uap->port.lock, flags);
1616}
1617
Jason Wessel84b5ae12008-02-20 13:33:39 -06001618#ifdef CONFIG_CONSOLE_POLL
Anton Vorontsov5c8124a2012-09-24 14:27:55 -07001619
1620static void pl011_quiesce_irqs(struct uart_port *port)
1621{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001622 struct uart_amba_port *uap =
1623 container_of(port, struct uart_amba_port, port);
Anton Vorontsov5c8124a2012-09-24 14:27:55 -07001624
Jun Nie7b753f32015-07-31 15:49:16 +08001625 pl011_writew(uap, pl011_readw(uap, REG_MIS), REG_ICR);
Anton Vorontsov5c8124a2012-09-24 14:27:55 -07001626 /*
1627 * There is no way to clear TXIM as this is "ready to transmit IRQ", so
1628 * we simply mask it. start_tx() will unmask it.
1629 *
1630 * Note we can race with start_tx(), and if the race happens, the
1631 * polling user might get another interrupt just after we clear it.
1632 * But it should be OK and can happen even w/o the race, e.g.
1633 * controller immediately got some new data and raised the IRQ.
1634 *
1635 * And whoever uses polling routines assumes that it manages the device
1636 * (including tx queue), so we're also fine with start_tx()'s caller
1637 * side.
1638 */
Jun Nie7b753f32015-07-31 15:49:16 +08001639 pl011_writew(uap, pl011_readw(uap, REG_IMSC) & ~UART011_TXIM, REG_IMSC);
Anton Vorontsov5c8124a2012-09-24 14:27:55 -07001640}
1641
Linus Walleije643f872012-06-17 15:44:19 +02001642static int pl011_get_poll_char(struct uart_port *port)
Jason Wessel84b5ae12008-02-20 13:33:39 -06001643{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001644 struct uart_amba_port *uap =
1645 container_of(port, struct uart_amba_port, port);
Jason Wessel84b5ae12008-02-20 13:33:39 -06001646 unsigned int status;
1647
Anton Vorontsov5c8124a2012-09-24 14:27:55 -07001648 /*
1649 * The caller might need IRQs lowered, e.g. if used with KDB NMI
1650 * debugger.
1651 */
1652 pl011_quiesce_irqs(port);
1653
Jun Nie7b753f32015-07-31 15:49:16 +08001654 status = pl011_readw(uap, REG_FR);
Jason Wesself5316b42010-05-20 21:04:22 -05001655 if (status & UART01x_FR_RXFE)
1656 return NO_POLL_CHAR;
Jason Wessel84b5ae12008-02-20 13:33:39 -06001657
Jun Nie7b753f32015-07-31 15:49:16 +08001658 return pl011_readw(uap, REG_DR);
Jason Wessel84b5ae12008-02-20 13:33:39 -06001659}
1660
Linus Walleije643f872012-06-17 15:44:19 +02001661static void pl011_put_poll_char(struct uart_port *port,
Jason Wessel84b5ae12008-02-20 13:33:39 -06001662 unsigned char ch)
1663{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001664 struct uart_amba_port *uap =
1665 container_of(port, struct uart_amba_port, port);
Jason Wessel84b5ae12008-02-20 13:33:39 -06001666
Jun Nie7b753f32015-07-31 15:49:16 +08001667 while (pl011_readw(uap, REG_FR) & UART01x_FR_TXFF)
Jason Wessel84b5ae12008-02-20 13:33:39 -06001668 barrier();
1669
Jun Nie7b753f32015-07-31 15:49:16 +08001670 pl011_writew(uap, ch, REG_DR);
Jason Wessel84b5ae12008-02-20 13:33:39 -06001671}
1672
1673#endif /* CONFIG_CONSOLE_POLL */
1674
Anton Vorontsovb3564c22012-09-24 14:27:54 -07001675static int pl011_hwinit(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001677 struct uart_amba_port *uap =
1678 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 int retval;
1680
Linus Walleij78d80c52012-05-23 21:18:46 +02001681 /* Optionaly enable pins to be muxed in and configured */
Linus Walleij2b996fc2013-06-05 15:36:42 +02001682 pinctrl_pm_select_default_state(port->dev);
Linus Walleij78d80c52012-05-23 21:18:46 +02001683
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 /*
1685 * Try to enable the clock producer.
1686 */
Julia Lawall1c4c4392012-08-26 18:01:01 +02001687 retval = clk_prepare_enable(uap->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 if (retval)
Tushar Behera7f6d9422014-06-26 15:35:35 +05301689 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690
1691 uap->port.uartclk = clk_get_rate(uap->clk);
1692
Linus Walleij9b96fba2012-03-13 13:27:23 +01001693 /* Clear pending error and receive interrupts */
Jun Nie7b753f32015-07-31 15:49:16 +08001694 pl011_writew(uap, UART011_OEIS | UART011_BEIS | UART011_PEIS |
1695 UART011_FEIS | UART011_RTIS | UART011_RXIS, REG_ICR);
Linus Walleij9b96fba2012-03-13 13:27:23 +01001696
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 /*
Anton Vorontsovb3564c22012-09-24 14:27:54 -07001698 * Save interrupts enable mask, and enable RX interrupts in case if
1699 * the interrupt is used for NMI entry.
1700 */
Jun Nie7b753f32015-07-31 15:49:16 +08001701 uap->im = pl011_readw(uap, REG_IMSC);
1702 pl011_writew(uap, UART011_RTIM | UART011_RXIM, REG_IMSC);
Anton Vorontsovb3564c22012-09-24 14:27:54 -07001703
Jingoo Han574de552013-07-30 17:06:57 +09001704 if (dev_get_platdata(uap->port.dev)) {
Anton Vorontsovb3564c22012-09-24 14:27:54 -07001705 struct amba_pl011_data *plat;
1706
Jingoo Han574de552013-07-30 17:06:57 +09001707 plat = dev_get_platdata(uap->port.dev);
Anton Vorontsovb3564c22012-09-24 14:27:54 -07001708 if (plat->init)
1709 plat->init();
1710 }
1711 return 0;
Anton Vorontsovb3564c22012-09-24 14:27:54 -07001712}
1713
Jon Medhurstb60f2f62013-12-10 10:18:59 +00001714static void pl011_write_lcr_h(struct uart_amba_port *uap, unsigned int lcr_h)
1715{
Jun Nie7b753f32015-07-31 15:49:16 +08001716 pl011_writew(uap, lcr_h, uap->lcrh_rx);
Jun Nie09dcc7d2015-07-31 15:49:18 +08001717 if (is_implemented(uap, REG_ST_LCRH_RX)) {
Jon Medhurstb60f2f62013-12-10 10:18:59 +00001718 int i;
1719 /*
1720 * Wait 10 PCLKs before writing LCRH_TX register,
1721 * to get this delay write read only register 10 times
1722 */
1723 for (i = 0; i < 10; ++i)
Jun Nie7b753f32015-07-31 15:49:16 +08001724 pl011_writew(uap, 0xff, REG_MIS);
1725 pl011_writew(uap, lcr_h, uap->lcrh_tx);
Jon Medhurstb60f2f62013-12-10 10:18:59 +00001726 }
1727}
1728
Andre Przywara867b8e82015-05-21 17:26:15 +01001729static int pl011_allocate_irq(struct uart_amba_port *uap)
1730{
Jun Nie7b753f32015-07-31 15:49:16 +08001731 pl011_writew(uap, uap->im, REG_IMSC);
Andre Przywara867b8e82015-05-21 17:26:15 +01001732
1733 return request_irq(uap->port.irq, pl011_int, 0, "uart-pl011", uap);
1734}
1735
1736/*
1737 * Enable interrupts, only timeouts when using DMA
1738 * if initial RX DMA job failed, start in interrupt mode
1739 * as well.
1740 */
1741static void pl011_enable_interrupts(struct uart_amba_port *uap)
1742{
1743 spin_lock_irq(&uap->port.lock);
1744
1745 /* Clear out any spuriously appearing RX interrupts */
Jun Nie7b753f32015-07-31 15:49:16 +08001746 pl011_writew(uap, UART011_RTIS | UART011_RXIS, REG_ICR);
Andre Przywara867b8e82015-05-21 17:26:15 +01001747 uap->im = UART011_RTIM;
1748 if (!pl011_dma_rx_running(uap))
1749 uap->im |= UART011_RXIM;
Jun Nie7b753f32015-07-31 15:49:16 +08001750 pl011_writew(uap, uap->im, REG_IMSC);
Andre Przywara867b8e82015-05-21 17:26:15 +01001751 spin_unlock_irq(&uap->port.lock);
1752}
1753
Anton Vorontsovb3564c22012-09-24 14:27:54 -07001754static int pl011_startup(struct uart_port *port)
1755{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001756 struct uart_amba_port *uap =
1757 container_of(port, struct uart_amba_port, port);
Dave Martin734745c2015-03-04 12:27:33 +00001758 unsigned int cr;
Anton Vorontsovb3564c22012-09-24 14:27:54 -07001759 int retval;
1760
1761 retval = pl011_hwinit(port);
1762 if (retval)
1763 goto clk_dis;
1764
Andre Przywara867b8e82015-05-21 17:26:15 +01001765 retval = pl011_allocate_irq(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 if (retval)
1767 goto clk_dis;
1768
Jun Nie7b753f32015-07-31 15:49:16 +08001769 pl011_writew(uap, uap->vendor->ifls, REG_IFLS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770
Jon Medhurstfe433902013-12-10 10:18:58 +00001771 spin_lock_irq(&uap->port.lock);
1772
Shreshtha Kumar Sahud8d8ffa2012-01-18 15:53:59 +05301773 /* restore RTS and DTR */
1774 cr = uap->old_cr & (UART011_CR_RTS | UART011_CR_DTR);
1775 cr |= UART01x_CR_UARTEN | UART011_CR_RXE | UART011_CR_TXE;
Jun Nie7b753f32015-07-31 15:49:16 +08001776 pl011_writew(uap, cr, REG_CR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777
Jon Medhurstfe433902013-12-10 10:18:58 +00001778 spin_unlock_irq(&uap->port.lock);
1779
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 /*
1781 * initialise the old status of the modem signals
1782 */
Jun Nie8cd90e52015-07-31 15:49:19 +08001783 uap->old_status = pl011_readw(uap, REG_FR) & UART01x_FR_MODEM_ANY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784
Russell King68b65f72010-12-22 17:24:39 +00001785 /* Startup DMA */
1786 pl011_dma_startup(uap);
1787
Andre Przywara867b8e82015-05-21 17:26:15 +01001788 pl011_enable_interrupts(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789
1790 return 0;
1791
1792 clk_dis:
Julia Lawall1c4c4392012-08-26 18:01:01 +02001793 clk_disable_unprepare(uap->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 return retval;
1795}
1796
Andre Przywara0dd1e242015-05-21 17:26:23 +01001797static int sbsa_uart_startup(struct uart_port *port)
1798{
1799 struct uart_amba_port *uap =
1800 container_of(port, struct uart_amba_port, port);
1801 int retval;
1802
1803 retval = pl011_hwinit(port);
1804 if (retval)
1805 return retval;
1806
1807 retval = pl011_allocate_irq(uap);
1808 if (retval)
1809 return retval;
1810
1811 /* The SBSA UART does not support any modem status lines. */
1812 uap->old_status = 0;
1813
1814 pl011_enable_interrupts(uap);
1815
1816 return 0;
1817}
1818
Linus Walleijec489aa2010-06-02 08:13:52 +01001819static void pl011_shutdown_channel(struct uart_amba_port *uap,
1820 unsigned int lcrh)
1821{
Jun Nie7b753f32015-07-31 15:49:16 +08001822 unsigned long val;
Linus Walleijec489aa2010-06-02 08:13:52 +01001823
Jun Nie7b753f32015-07-31 15:49:16 +08001824 val = pl011_readw(uap, lcrh);
1825 val &= ~(UART01x_LCRH_BRK | UART01x_LCRH_FEN);
1826 pl011_writew(uap, val, lcrh);
Linus Walleijec489aa2010-06-02 08:13:52 +01001827}
1828
Andre Przywara95166a32015-05-21 17:26:16 +01001829/*
1830 * disable the port. It should not disable RTS and DTR.
1831 * Also RTS and DTR state should be preserved to restore
1832 * it during startup().
1833 */
1834static void pl011_disable_uart(struct uart_amba_port *uap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835{
Shreshtha Kumar Sahud8d8ffa2012-01-18 15:53:59 +05301836 unsigned int cr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837
Rabin Vincent3b438162010-02-12 06:43:11 +01001838 uap->autorts = false;
Jon Medhurstfe433902013-12-10 10:18:58 +00001839 spin_lock_irq(&uap->port.lock);
Jun Nie7b753f32015-07-31 15:49:16 +08001840 cr = pl011_readw(uap, REG_CR);
Shreshtha Kumar Sahud8d8ffa2012-01-18 15:53:59 +05301841 uap->old_cr = cr;
1842 cr &= UART011_CR_RTS | UART011_CR_DTR;
1843 cr |= UART01x_CR_UARTEN | UART011_CR_TXE;
Jun Nie7b753f32015-07-31 15:49:16 +08001844 pl011_writew(uap, cr, REG_CR);
Jon Medhurstfe433902013-12-10 10:18:58 +00001845 spin_unlock_irq(&uap->port.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846
1847 /*
1848 * disable break condition and fifos
1849 */
Linus Walleijec489aa2010-06-02 08:13:52 +01001850 pl011_shutdown_channel(uap, uap->lcrh_rx);
Jun Nie09dcc7d2015-07-31 15:49:18 +08001851 if (is_implemented(uap, REG_ST_LCRH_RX))
Linus Walleijec489aa2010-06-02 08:13:52 +01001852 pl011_shutdown_channel(uap, uap->lcrh_tx);
Andre Przywara95166a32015-05-21 17:26:16 +01001853}
1854
1855static void pl011_disable_interrupts(struct uart_amba_port *uap)
1856{
1857 spin_lock_irq(&uap->port.lock);
1858
1859 /* mask all interrupts and clear all pending ones */
1860 uap->im = 0;
Jun Nie7b753f32015-07-31 15:49:16 +08001861 pl011_writew(uap, uap->im, REG_IMSC);
Jun Nie8cd90e52015-07-31 15:49:19 +08001862 pl011_writew(uap, 0xffff, REG_ICR);
Andre Przywara95166a32015-05-21 17:26:16 +01001863
1864 spin_unlock_irq(&uap->port.lock);
1865}
1866
1867static void pl011_shutdown(struct uart_port *port)
1868{
1869 struct uart_amba_port *uap =
1870 container_of(port, struct uart_amba_port, port);
1871
1872 pl011_disable_interrupts(uap);
1873
1874 pl011_dma_shutdown(uap);
1875
1876 free_irq(uap->port.irq, uap);
1877
1878 pl011_disable_uart(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879
1880 /*
1881 * Shut down the clock producer
1882 */
Julia Lawall1c4c4392012-08-26 18:01:01 +02001883 clk_disable_unprepare(uap->clk);
Linus Walleij78d80c52012-05-23 21:18:46 +02001884 /* Optionally let pins go into sleep states */
Linus Walleij2b996fc2013-06-05 15:36:42 +02001885 pinctrl_pm_select_sleep_state(port->dev);
Shreshtha Kumar Sahuc16d51a2011-06-13 10:11:33 +02001886
Jingoo Han574de552013-07-30 17:06:57 +09001887 if (dev_get_platdata(uap->port.dev)) {
Shreshtha Kumar Sahuc16d51a2011-06-13 10:11:33 +02001888 struct amba_pl011_data *plat;
1889
Jingoo Han574de552013-07-30 17:06:57 +09001890 plat = dev_get_platdata(uap->port.dev);
Shreshtha Kumar Sahuc16d51a2011-06-13 10:11:33 +02001891 if (plat->exit)
1892 plat->exit();
1893 }
1894
Peter Hurley36f339d2014-11-06 09:06:12 -05001895 if (uap->port.ops->flush_buffer)
1896 uap->port.ops->flush_buffer(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897}
1898
Andre Przywara0dd1e242015-05-21 17:26:23 +01001899static void sbsa_uart_shutdown(struct uart_port *port)
1900{
1901 struct uart_amba_port *uap =
1902 container_of(port, struct uart_amba_port, port);
1903
1904 pl011_disable_interrupts(uap);
1905
1906 free_irq(uap->port.irq, uap);
1907
1908 if (uap->port.ops->flush_buffer)
1909 uap->port.ops->flush_buffer(port);
1910}
1911
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912static void
Andre Przywaraef5a9352015-05-21 17:26:17 +01001913pl011_setup_status_masks(struct uart_port *port, struct ktermios *termios)
1914{
1915 port->read_status_mask = UART011_DR_OE | 255;
1916 if (termios->c_iflag & INPCK)
1917 port->read_status_mask |= UART011_DR_FE | UART011_DR_PE;
1918 if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
1919 port->read_status_mask |= UART011_DR_BE;
1920
1921 /*
1922 * Characters to ignore
1923 */
1924 port->ignore_status_mask = 0;
1925 if (termios->c_iflag & IGNPAR)
1926 port->ignore_status_mask |= UART011_DR_FE | UART011_DR_PE;
1927 if (termios->c_iflag & IGNBRK) {
1928 port->ignore_status_mask |= UART011_DR_BE;
1929 /*
1930 * If we're ignoring parity and break indicators,
1931 * ignore overruns too (for real raw support).
1932 */
1933 if (termios->c_iflag & IGNPAR)
1934 port->ignore_status_mask |= UART011_DR_OE;
1935 }
1936
1937 /*
1938 * Ignore all characters if CREAD is not set.
1939 */
1940 if ((termios->c_cflag & CREAD) == 0)
1941 port->ignore_status_mask |= UART_DUMMY_DR_RX;
1942}
1943
1944static void
Alan Cox606d0992006-12-08 02:38:45 -08001945pl011_set_termios(struct uart_port *port, struct ktermios *termios,
1946 struct ktermios *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001948 struct uart_amba_port *uap =
1949 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 unsigned int lcr_h, old_cr;
1951 unsigned long flags;
Russell Kingc19f12b2010-12-22 17:48:26 +00001952 unsigned int baud, quot, clkdiv;
1953
1954 if (uap->vendor->oversampling)
1955 clkdiv = 8;
1956 else
1957 clkdiv = 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958
1959 /*
1960 * Ask the core to calculate the divisor for us.
1961 */
Linus Walleijac3e3fb2010-06-02 20:40:22 +01001962 baud = uart_get_baud_rate(port, termios, old, 0,
Russell Kingc19f12b2010-12-22 17:48:26 +00001963 port->uartclk / clkdiv);
Chanho Min89fa28d2013-04-03 11:10:37 +09001964#ifdef CONFIG_DMA_ENGINE
Chanho Mincb06ff12013-03-27 18:38:11 +09001965 /*
1966 * Adjust RX DMA polling rate with baud rate if not specified.
1967 */
1968 if (uap->dmarx.auto_poll_rate)
1969 uap->dmarx.poll_rate = DIV_ROUND_UP(10000000, baud);
Chanho Min89fa28d2013-04-03 11:10:37 +09001970#endif
Linus Walleijac3e3fb2010-06-02 20:40:22 +01001971
1972 if (baud > port->uartclk/16)
1973 quot = DIV_ROUND_CLOSEST(port->uartclk * 8, baud);
1974 else
1975 quot = DIV_ROUND_CLOSEST(port->uartclk * 4, baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976
1977 switch (termios->c_cflag & CSIZE) {
1978 case CS5:
1979 lcr_h = UART01x_LCRH_WLEN_5;
1980 break;
1981 case CS6:
1982 lcr_h = UART01x_LCRH_WLEN_6;
1983 break;
1984 case CS7:
1985 lcr_h = UART01x_LCRH_WLEN_7;
1986 break;
1987 default: // CS8
1988 lcr_h = UART01x_LCRH_WLEN_8;
1989 break;
1990 }
1991 if (termios->c_cflag & CSTOPB)
1992 lcr_h |= UART01x_LCRH_STP2;
1993 if (termios->c_cflag & PARENB) {
1994 lcr_h |= UART01x_LCRH_PEN;
1995 if (!(termios->c_cflag & PARODD))
1996 lcr_h |= UART01x_LCRH_EPS;
1997 }
Russell Kingffca2b12010-12-22 17:13:05 +00001998 if (uap->fifosize > 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 lcr_h |= UART01x_LCRH_FEN;
2000
2001 spin_lock_irqsave(&port->lock, flags);
2002
2003 /*
2004 * Update the per-port timeout.
2005 */
2006 uart_update_timeout(port, termios->c_cflag, baud);
2007
Andre Przywaraef5a9352015-05-21 17:26:17 +01002008 pl011_setup_status_masks(port, termios);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009
2010 if (UART_ENABLE_MS(port, termios->c_cflag))
2011 pl011_enable_ms(port);
2012
2013 /* first, disable everything */
Jun Nie7b753f32015-07-31 15:49:16 +08002014 old_cr = pl011_readw(uap, REG_CR);
2015 pl011_writew(uap, 0, REG_CR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016
Rabin Vincent3b438162010-02-12 06:43:11 +01002017 if (termios->c_cflag & CRTSCTS) {
2018 if (old_cr & UART011_CR_RTS)
2019 old_cr |= UART011_CR_RTSEN;
2020
2021 old_cr |= UART011_CR_CTSEN;
2022 uap->autorts = true;
2023 } else {
2024 old_cr &= ~(UART011_CR_CTSEN | UART011_CR_RTSEN);
2025 uap->autorts = false;
2026 }
2027
Russell Kingc19f12b2010-12-22 17:48:26 +00002028 if (uap->vendor->oversampling) {
2029 if (baud > port->uartclk / 16)
Linus Walleijac3e3fb2010-06-02 20:40:22 +01002030 old_cr |= ST_UART011_CR_OVSFACT;
2031 else
2032 old_cr &= ~ST_UART011_CR_OVSFACT;
2033 }
2034
Linus Walleijc5dd5532012-09-26 17:21:36 +02002035 /*
2036 * Workaround for the ST Micro oversampling variants to
2037 * increase the bitrate slightly, by lowering the divisor,
2038 * to avoid delayed sampling of start bit at high speeds,
2039 * else we see data corruption.
2040 */
2041 if (uap->vendor->oversampling) {
2042 if ((baud >= 3000000) && (baud < 3250000) && (quot > 1))
2043 quot -= 1;
2044 else if ((baud > 3250000) && (quot > 2))
2045 quot -= 2;
2046 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 /* Set baud rate */
Jun Nie7b753f32015-07-31 15:49:16 +08002048 pl011_writew(uap, quot & 0x3f, REG_FBRD);
2049 pl011_writew(uap, quot >> 6, REG_IBRD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050
2051 /*
2052 * ----------v----------v----------v----------v-----
Linus Walleijc5dd5532012-09-26 17:21:36 +02002053 * NOTE: lcrh_tx and lcrh_rx MUST BE WRITTEN AFTER
Jun Nie534e14e2015-07-31 15:49:15 +08002054 * REG_FBRD & REG_IBRD.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 * ----------^----------^----------^----------^-----
2056 */
Jon Medhurstb60f2f62013-12-10 10:18:59 +00002057 pl011_write_lcr_h(uap, lcr_h);
Jun Nie7b753f32015-07-31 15:49:16 +08002058 pl011_writew(uap, old_cr, REG_CR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059
2060 spin_unlock_irqrestore(&port->lock, flags);
2061}
2062
Andre Przywara0dd1e242015-05-21 17:26:23 +01002063static void
2064sbsa_uart_set_termios(struct uart_port *port, struct ktermios *termios,
2065 struct ktermios *old)
2066{
2067 struct uart_amba_port *uap =
2068 container_of(port, struct uart_amba_port, port);
2069 unsigned long flags;
2070
2071 tty_termios_encode_baud_rate(termios, uap->fixed_baud, uap->fixed_baud);
2072
2073 /* The SBSA UART only supports 8n1 without hardware flow control. */
2074 termios->c_cflag &= ~(CSIZE | CSTOPB | PARENB | PARODD);
2075 termios->c_cflag &= ~(CMSPAR | CRTSCTS);
2076 termios->c_cflag |= CS8 | CLOCAL;
2077
2078 spin_lock_irqsave(&port->lock, flags);
2079 uart_update_timeout(port, CS8, uap->fixed_baud);
2080 pl011_setup_status_masks(port, termios);
2081 spin_unlock_irqrestore(&port->lock, flags);
2082}
2083
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084static const char *pl011_type(struct uart_port *port)
2085{
Daniel Thompsona5820c22014-09-03 12:51:55 +01002086 struct uart_amba_port *uap =
2087 container_of(port, struct uart_amba_port, port);
Russell Kinge8a7ba82010-12-28 09:16:54 +00002088 return uap->port.type == PORT_AMBA ? uap->type : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089}
2090
2091/*
2092 * Release the memory region(s) being used by 'port'
2093 */
Linus Walleije643f872012-06-17 15:44:19 +02002094static void pl011_release_port(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095{
2096 release_mem_region(port->mapbase, SZ_4K);
2097}
2098
2099/*
2100 * Request the memory region(s) being used by 'port'
2101 */
Linus Walleije643f872012-06-17 15:44:19 +02002102static int pl011_request_port(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103{
2104 return request_mem_region(port->mapbase, SZ_4K, "uart-pl011")
2105 != NULL ? 0 : -EBUSY;
2106}
2107
2108/*
2109 * Configure/autoconfigure the port.
2110 */
Linus Walleije643f872012-06-17 15:44:19 +02002111static void pl011_config_port(struct uart_port *port, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112{
2113 if (flags & UART_CONFIG_TYPE) {
2114 port->type = PORT_AMBA;
Linus Walleije643f872012-06-17 15:44:19 +02002115 pl011_request_port(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 }
2117}
2118
2119/*
2120 * verify the new serial_struct (for TIOCSSERIAL).
2121 */
Linus Walleije643f872012-06-17 15:44:19 +02002122static int pl011_verify_port(struct uart_port *port, struct serial_struct *ser)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123{
2124 int ret = 0;
2125 if (ser->type != PORT_UNKNOWN && ser->type != PORT_AMBA)
2126 ret = -EINVAL;
Yinghai Lua62c4132008-08-19 20:49:55 -07002127 if (ser->irq < 0 || ser->irq >= nr_irqs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 ret = -EINVAL;
2129 if (ser->baud_base < 9600)
2130 ret = -EINVAL;
2131 return ret;
2132}
2133
2134static struct uart_ops amba_pl011_pops = {
Linus Walleije643f872012-06-17 15:44:19 +02002135 .tx_empty = pl011_tx_empty,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 .set_mctrl = pl011_set_mctrl,
Linus Walleije643f872012-06-17 15:44:19 +02002137 .get_mctrl = pl011_get_mctrl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 .stop_tx = pl011_stop_tx,
2139 .start_tx = pl011_start_tx,
2140 .stop_rx = pl011_stop_rx,
2141 .enable_ms = pl011_enable_ms,
2142 .break_ctl = pl011_break_ctl,
2143 .startup = pl011_startup,
2144 .shutdown = pl011_shutdown,
Russell King68b65f72010-12-22 17:24:39 +00002145 .flush_buffer = pl011_dma_flush_buffer,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 .set_termios = pl011_set_termios,
2147 .type = pl011_type,
Linus Walleije643f872012-06-17 15:44:19 +02002148 .release_port = pl011_release_port,
2149 .request_port = pl011_request_port,
2150 .config_port = pl011_config_port,
2151 .verify_port = pl011_verify_port,
Jason Wessel84b5ae12008-02-20 13:33:39 -06002152#ifdef CONFIG_CONSOLE_POLL
Anton Vorontsovb3564c22012-09-24 14:27:54 -07002153 .poll_init = pl011_hwinit,
Linus Walleije643f872012-06-17 15:44:19 +02002154 .poll_get_char = pl011_get_poll_char,
2155 .poll_put_char = pl011_put_poll_char,
Jason Wessel84b5ae12008-02-20 13:33:39 -06002156#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157};
2158
Andre Przywara0dd1e242015-05-21 17:26:23 +01002159static void sbsa_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
2160{
2161}
2162
2163static unsigned int sbsa_uart_get_mctrl(struct uart_port *port)
2164{
2165 return 0;
2166}
2167
2168static const struct uart_ops sbsa_uart_pops = {
2169 .tx_empty = pl011_tx_empty,
2170 .set_mctrl = sbsa_uart_set_mctrl,
2171 .get_mctrl = sbsa_uart_get_mctrl,
2172 .stop_tx = pl011_stop_tx,
2173 .start_tx = pl011_start_tx,
2174 .stop_rx = pl011_stop_rx,
2175 .startup = sbsa_uart_startup,
2176 .shutdown = sbsa_uart_shutdown,
2177 .set_termios = sbsa_uart_set_termios,
2178 .type = pl011_type,
2179 .release_port = pl011_release_port,
2180 .request_port = pl011_request_port,
2181 .config_port = pl011_config_port,
2182 .verify_port = pl011_verify_port,
2183#ifdef CONFIG_CONSOLE_POLL
2184 .poll_init = pl011_hwinit,
2185 .poll_get_char = pl011_get_poll_char,
2186 .poll_put_char = pl011_put_poll_char,
2187#endif
2188};
2189
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190static struct uart_amba_port *amba_ports[UART_NR];
2191
2192#ifdef CONFIG_SERIAL_AMBA_PL011_CONSOLE
2193
Russell Kingd3587882006-03-20 20:00:09 +00002194static void pl011_console_putchar(struct uart_port *port, int ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195{
Daniel Thompsona5820c22014-09-03 12:51:55 +01002196 struct uart_amba_port *uap =
2197 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198
Jun Nie7b753f32015-07-31 15:49:16 +08002199 while (pl011_readw(uap, REG_FR) & UART01x_FR_TXFF)
Russell Kingd3587882006-03-20 20:00:09 +00002200 barrier();
Jun Nie7b753f32015-07-31 15:49:16 +08002201 pl011_writew(uap, ch, REG_DR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202}
2203
2204static void
2205pl011_console_write(struct console *co, const char *s, unsigned int count)
2206{
2207 struct uart_amba_port *uap = amba_ports[co->index];
Andre Przywara71eec482015-05-21 17:26:21 +01002208 unsigned int status, old_cr = 0, new_cr;
Rabin Vincentef605fd2012-01-17 11:52:28 +01002209 unsigned long flags;
2210 int locked = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211
2212 clk_enable(uap->clk);
2213
Rabin Vincentef605fd2012-01-17 11:52:28 +01002214 local_irq_save(flags);
2215 if (uap->port.sysrq)
2216 locked = 0;
2217 else if (oops_in_progress)
2218 locked = spin_trylock(&uap->port.lock);
2219 else
2220 spin_lock(&uap->port.lock);
2221
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 /*
2223 * First save the CR then disable the interrupts
2224 */
Andre Przywara71eec482015-05-21 17:26:21 +01002225 if (!uap->vendor->always_enabled) {
Jun Nie7b753f32015-07-31 15:49:16 +08002226 old_cr = pl011_readw(uap, REG_CR);
Andre Przywara71eec482015-05-21 17:26:21 +01002227 new_cr = old_cr & ~UART011_CR_CTSEN;
2228 new_cr |= UART01x_CR_UARTEN | UART011_CR_TXE;
Jun Nie7b753f32015-07-31 15:49:16 +08002229 pl011_writew(uap, new_cr, REG_CR);
Andre Przywara71eec482015-05-21 17:26:21 +01002230 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231
Russell Kingd3587882006-03-20 20:00:09 +00002232 uart_console_write(&uap->port, s, count, pl011_console_putchar);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233
2234 /*
2235 * Finally, wait for transmitter to become empty
2236 * and restore the TCR
2237 */
2238 do {
Jun Nie7b753f32015-07-31 15:49:16 +08002239 status = pl011_readw(uap, REG_FR);
Jun Nie8cd90e52015-07-31 15:49:19 +08002240 } while (status & uap->fr_busy);
Andre Przywara71eec482015-05-21 17:26:21 +01002241 if (!uap->vendor->always_enabled)
Jun Nie7b753f32015-07-31 15:49:16 +08002242 pl011_writew(uap, old_cr, REG_CR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243
Rabin Vincentef605fd2012-01-17 11:52:28 +01002244 if (locked)
2245 spin_unlock(&uap->port.lock);
2246 local_irq_restore(flags);
2247
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 clk_disable(uap->clk);
2249}
2250
2251static void __init
2252pl011_console_get_options(struct uart_amba_port *uap, int *baud,
2253 int *parity, int *bits)
2254{
Jun Nie7b753f32015-07-31 15:49:16 +08002255 if (pl011_readw(uap, REG_CR) & UART01x_CR_UARTEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 unsigned int lcr_h, ibrd, fbrd;
2257
Jun Nie7b753f32015-07-31 15:49:16 +08002258 lcr_h = pl011_readw(uap, uap->lcrh_tx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259
2260 *parity = 'n';
2261 if (lcr_h & UART01x_LCRH_PEN) {
2262 if (lcr_h & UART01x_LCRH_EPS)
2263 *parity = 'e';
2264 else
2265 *parity = 'o';
2266 }
2267
2268 if ((lcr_h & 0x60) == UART01x_LCRH_WLEN_7)
2269 *bits = 7;
2270 else
2271 *bits = 8;
2272
Jun Nie7b753f32015-07-31 15:49:16 +08002273 ibrd = pl011_readw(uap, REG_IBRD);
2274 fbrd = pl011_readw(uap, REG_FBRD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275
2276 *baud = uap->port.uartclk * 4 / (64 * ibrd + fbrd);
Linus Walleijac3e3fb2010-06-02 20:40:22 +01002277
Russell Kingc19f12b2010-12-22 17:48:26 +00002278 if (uap->vendor->oversampling) {
Jun Nie7b753f32015-07-31 15:49:16 +08002279 if (pl011_readw(uap, REG_CR)
Linus Walleijac3e3fb2010-06-02 20:40:22 +01002280 & ST_UART011_CR_OVSFACT)
2281 *baud *= 2;
2282 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 }
2284}
2285
2286static int __init pl011_console_setup(struct console *co, char *options)
2287{
2288 struct uart_amba_port *uap;
2289 int baud = 38400;
2290 int bits = 8;
2291 int parity = 'n';
2292 int flow = 'n';
Russell King4b4851c2011-09-22 11:35:30 +01002293 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294
2295 /*
2296 * Check whether an invalid uart number has been specified, and
2297 * if so, search for the first available port that does have
2298 * console support.
2299 */
2300 if (co->index >= UART_NR)
2301 co->index = 0;
2302 uap = amba_ports[co->index];
Russell Kingd28122a2007-01-22 18:59:42 +00002303 if (!uap)
2304 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305
Linus Walleij78d80c52012-05-23 21:18:46 +02002306 /* Allow pins to be muxed in and configured */
Linus Walleij2b996fc2013-06-05 15:36:42 +02002307 pinctrl_pm_select_default_state(uap->port.dev);
Linus Walleij78d80c52012-05-23 21:18:46 +02002308
Russell King4b4851c2011-09-22 11:35:30 +01002309 ret = clk_prepare(uap->clk);
2310 if (ret)
2311 return ret;
2312
Jingoo Han574de552013-07-30 17:06:57 +09002313 if (dev_get_platdata(uap->port.dev)) {
Shreshtha Kumar Sahuc16d51a2011-06-13 10:11:33 +02002314 struct amba_pl011_data *plat;
2315
Jingoo Han574de552013-07-30 17:06:57 +09002316 plat = dev_get_platdata(uap->port.dev);
Shreshtha Kumar Sahuc16d51a2011-06-13 10:11:33 +02002317 if (plat->init)
2318 plat->init();
2319 }
2320
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321 uap->port.uartclk = clk_get_rate(uap->clk);
2322
Andre Przywaracefc2d12015-05-21 17:26:22 +01002323 if (uap->vendor->fixed_options) {
2324 baud = uap->fixed_baud;
2325 } else {
2326 if (options)
2327 uart_parse_options(options,
2328 &baud, &parity, &bits, &flow);
2329 else
2330 pl011_console_get_options(uap, &baud, &parity, &bits);
2331 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332
2333 return uart_set_options(&uap->port, co, baud, parity, bits, flow);
2334}
2335
Vincent Sanders2d934862005-09-14 22:36:03 +01002336static struct uart_driver amba_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337static struct console amba_console = {
2338 .name = "ttyAMA",
2339 .write = pl011_console_write,
2340 .device = uart_console_device,
2341 .setup = pl011_console_setup,
2342 .flags = CON_PRINTBUFFER,
2343 .index = -1,
2344 .data = &amba_reg,
2345};
2346
2347#define AMBA_CONSOLE (&amba_console)
Rob Herring0d3c6732014-04-18 17:19:57 -05002348
2349static void pl011_putc(struct uart_port *port, int c)
2350{
Jun Nie7b753f32015-07-31 15:49:16 +08002351 struct uart_amba_port *uap =
2352 container_of(port, struct uart_amba_port, port);
2353
2354 while (pl011_readw(uap, REG_FR) & UART01x_FR_TXFF)
Rob Herring0d3c6732014-04-18 17:19:57 -05002355 ;
Jun Nie7b753f32015-07-31 15:49:16 +08002356 pl011_writeb(uap, c, REG_DR);
Jun Nie8cd90e52015-07-31 15:49:19 +08002357 while (pl011_readw(uap, REG_FR) & uap->fr_busy)
Rob Herring0d3c6732014-04-18 17:19:57 -05002358 ;
2359}
2360
2361static void pl011_early_write(struct console *con, const char *s, unsigned n)
2362{
2363 struct earlycon_device *dev = con->data;
2364
2365 uart_console_write(&dev->port, s, n, pl011_putc);
2366}
2367
2368static int __init pl011_early_console_setup(struct earlycon_device *device,
2369 const char *opt)
2370{
2371 if (!device->port.membase)
2372 return -ENODEV;
2373
2374 device->con->write = pl011_early_write;
2375 return 0;
2376}
2377EARLYCON_DECLARE(pl011, pl011_early_console_setup);
Rob Herring45e0f0f2014-03-27 08:08:03 -05002378OF_EARLYCON_DECLARE(pl011, "arm,pl011", pl011_early_console_setup);
Rob Herring0d3c6732014-04-18 17:19:57 -05002379
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380#else
2381#define AMBA_CONSOLE NULL
2382#endif
2383
2384static struct uart_driver amba_reg = {
2385 .owner = THIS_MODULE,
2386 .driver_name = "ttyAMA",
2387 .dev_name = "ttyAMA",
2388 .major = SERIAL_AMBA_MAJOR,
2389 .minor = SERIAL_AMBA_MINOR,
2390 .nr = UART_NR,
2391 .cons = AMBA_CONSOLE,
2392};
2393
Matthew Leach32614aa2012-08-28 16:41:28 +01002394static int pl011_probe_dt_alias(int index, struct device *dev)
2395{
2396 struct device_node *np;
2397 static bool seen_dev_with_alias = false;
2398 static bool seen_dev_without_alias = false;
2399 int ret = index;
2400
2401 if (!IS_ENABLED(CONFIG_OF))
2402 return ret;
2403
2404 np = dev->of_node;
2405 if (!np)
2406 return ret;
2407
2408 ret = of_alias_get_id(np, "serial");
2409 if (IS_ERR_VALUE(ret)) {
2410 seen_dev_without_alias = true;
2411 ret = index;
2412 } else {
2413 seen_dev_with_alias = true;
2414 if (ret >= ARRAY_SIZE(amba_ports) || amba_ports[ret] != NULL) {
2415 dev_warn(dev, "requested serial port %d not available.\n", ret);
2416 ret = index;
2417 }
2418 }
2419
2420 if (seen_dev_with_alias && seen_dev_without_alias)
2421 dev_warn(dev, "aliased and non-aliased serial devices found in device tree. Serial port enumeration may be unpredictable.\n");
2422
2423 return ret;
2424}
2425
Andre Przywara49bb3c82015-05-21 17:26:14 +01002426/* unregisters the driver also if no more ports are left */
2427static void pl011_unregister_port(struct uart_amba_port *uap)
2428{
2429 int i;
2430 bool busy = false;
2431
2432 for (i = 0; i < ARRAY_SIZE(amba_ports); i++) {
2433 if (amba_ports[i] == uap)
2434 amba_ports[i] = NULL;
2435 else if (amba_ports[i])
2436 busy = true;
2437 }
2438 pl011_dma_remove(uap);
2439 if (!busy)
2440 uart_unregister_driver(&amba_reg);
2441}
2442
Andre Przywara3873e2d2015-05-21 17:26:18 +01002443static int pl011_find_free_port(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444{
Andre Przywara3873e2d2015-05-21 17:26:18 +01002445 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446
2447 for (i = 0; i < ARRAY_SIZE(amba_ports); i++)
2448 if (amba_ports[i] == NULL)
Andre Przywara3873e2d2015-05-21 17:26:18 +01002449 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450
Andre Przywara3873e2d2015-05-21 17:26:18 +01002451 return -EBUSY;
2452}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453
Andre Przywara3873e2d2015-05-21 17:26:18 +01002454static int pl011_setup_port(struct device *dev, struct uart_amba_port *uap,
2455 struct resource *mmiobase, int index)
2456{
2457 void __iomem *base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458
Andre Przywara3873e2d2015-05-21 17:26:18 +01002459 base = devm_ioremap_resource(dev, mmiobase);
Krzysztof Kozlowski97a60ea2015-07-09 22:21:41 +09002460 if (IS_ERR(base))
2461 return PTR_ERR(base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462
Andre Przywara3873e2d2015-05-21 17:26:18 +01002463 index = pl011_probe_dt_alias(index, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464
Shreshtha Kumar Sahud8d8ffa2012-01-18 15:53:59 +05302465 uap->old_cr = 0;
Andre Przywara3873e2d2015-05-21 17:26:18 +01002466 uap->port.dev = dev;
2467 uap->port.mapbase = mmiobase->start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 uap->port.membase = base;
2469 uap->port.iotype = UPIO_MEM;
Russell Kingffca2b12010-12-22 17:13:05 +00002470 uap->port.fifosize = uap->fifosize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 uap->port.flags = UPF_BOOT_AUTOCONF;
Andre Przywara3873e2d2015-05-21 17:26:18 +01002472 uap->port.line = index;
2473
2474 amba_ports[index] = uap;
2475
2476 return 0;
2477}
2478
2479static int pl011_register_port(struct uart_amba_port *uap)
2480{
2481 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482
Linus Walleijc3d8b762012-03-21 20:15:18 +01002483 /* Ensure interrupts from this UART are masked and cleared */
Jun Nie7b753f32015-07-31 15:49:16 +08002484 pl011_writew(uap, 0, REG_IMSC);
2485 pl011_writew(uap, 0xffff, REG_ICR);
Linus Walleijc3d8b762012-03-21 20:15:18 +01002486
Tushar Beheraef2889f2014-01-20 14:32:35 +05302487 if (!amba_reg.state) {
2488 ret = uart_register_driver(&amba_reg);
2489 if (ret < 0) {
Andre Przywara3873e2d2015-05-21 17:26:18 +01002490 dev_err(uap->port.dev,
Jorge Ramirez-Ortiz1c9be312015-03-06 13:05:40 -05002491 "Failed to register AMBA-PL011 driver\n");
Tushar Beheraef2889f2014-01-20 14:32:35 +05302492 return ret;
2493 }
2494 }
2495
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496 ret = uart_add_one_port(&amba_reg, &uap->port);
Andre Przywara49bb3c82015-05-21 17:26:14 +01002497 if (ret)
2498 pl011_unregister_port(uap);
Tushar Behera7f6d9422014-06-26 15:35:35 +05302499
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 return ret;
2501}
2502
Jun Nie8cd90e52015-07-31 15:49:19 +08002503#ifdef CONFIG_ARM_AMBA
Andre Przywara3873e2d2015-05-21 17:26:18 +01002504static int pl011_probe(struct amba_device *dev, const struct amba_id *id)
2505{
2506 struct uart_amba_port *uap;
2507 struct vendor_data *vendor = id->data;
2508 int portnr, ret;
2509
2510 portnr = pl011_find_free_port();
2511 if (portnr < 0)
2512 return portnr;
2513
2514 uap = devm_kzalloc(&dev->dev, sizeof(struct uart_amba_port),
2515 GFP_KERNEL);
2516 if (!uap)
2517 return -ENOMEM;
2518
2519 uap->clk = devm_clk_get(&dev->dev, NULL);
2520 if (IS_ERR(uap->clk))
2521 return PTR_ERR(uap->clk);
2522
2523 uap->vendor = vendor;
Jun Nie2c096a92015-07-31 15:49:17 +08002524 uap->reg_lut = vendor->reg_lut;
Andre Przywara3873e2d2015-05-21 17:26:18 +01002525 uap->lcrh_rx = vendor->lcrh_rx;
2526 uap->lcrh_tx = vendor->lcrh_tx;
Jun Nie8cd90e52015-07-31 15:49:19 +08002527 uap->fr_busy = vendor->fr_busy;
2528 uap->fr_dsr = vendor->fr_dsr;
2529 uap->fr_cts = vendor->fr_cts;
2530 uap->fr_ri = vendor->fr_ri;
Andre Przywara3873e2d2015-05-21 17:26:18 +01002531 uap->fifosize = vendor->get_fifosize(dev);
2532 uap->port.irq = dev->irq[0];
2533 uap->port.ops = &amba_pl011_pops;
2534
2535 snprintf(uap->type, sizeof(uap->type), "PL011 rev%u", amba_rev(dev));
2536
2537 ret = pl011_setup_port(&dev->dev, uap, &dev->res, portnr);
2538 if (ret)
2539 return ret;
2540
2541 amba_set_drvdata(dev, uap);
2542
2543 return pl011_register_port(uap);
2544}
2545
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546static int pl011_remove(struct amba_device *dev)
2547{
2548 struct uart_amba_port *uap = amba_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550 uart_remove_one_port(&amba_reg, &uap->port);
Andre Przywara49bb3c82015-05-21 17:26:14 +01002551 pl011_unregister_port(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552 return 0;
2553}
Jun Nie8cd90e52015-07-31 15:49:19 +08002554#endif
2555
2556#ifdef CONFIG_SOC_ZX296702
2557static int zx_uart_probe(struct platform_device *pdev)
2558{
2559 struct uart_amba_port *uap;
2560 struct vendor_data *vendor = &vendor_zte;
2561 struct resource *res;
2562 int portnr, ret;
2563
2564 portnr = pl011_find_free_port();
2565 if (portnr < 0)
2566 return portnr;
2567
2568 uap = devm_kzalloc(&pdev->dev, sizeof(struct uart_amba_port),
2569 GFP_KERNEL);
2570 if (!uap) {
2571 ret = -ENOMEM;
2572 goto out;
2573 }
2574
2575 uap->clk = devm_clk_get(&pdev->dev, NULL);
2576 if (IS_ERR(uap->clk)) {
2577 ret = PTR_ERR(uap->clk);
2578 goto out;
2579 }
2580
2581 uap->vendor = vendor;
2582 uap->reg_lut = vendor->reg_lut;
2583 uap->lcrh_rx = vendor->lcrh_rx;
2584 uap->lcrh_tx = vendor->lcrh_tx;
2585 uap->fr_busy = vendor->fr_busy;
2586 uap->fr_dsr = vendor->fr_dsr;
2587 uap->fr_cts = vendor->fr_cts;
2588 uap->fr_ri = vendor->fr_ri;
2589 uap->fifosize = 16;
2590 uap->port.irq = platform_get_irq(pdev, 0);
2591 uap->port.ops = &amba_pl011_pops;
2592
2593 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2594
2595 ret = pl011_setup_port(&pdev->dev, uap, res, portnr);
2596 if (ret)
2597 return ret;
2598
2599 platform_set_drvdata(pdev, uap);
2600
2601 return pl011_register_port(uap);
2602out:
2603 return ret;
2604}
2605
2606static int zx_uart_remove(struct platform_device *pdev)
2607{
2608 struct uart_amba_port *uap = platform_get_drvdata(pdev);
2609
2610 uart_remove_one_port(&amba_reg, &uap->port);
2611 pl011_unregister_port(uap);
2612 return 0;
2613}
2614#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615
Ulf Hanssond0ce8502013-12-03 11:04:28 +01002616#ifdef CONFIG_PM_SLEEP
2617static int pl011_suspend(struct device *dev)
Leo Chenb736b892009-07-28 23:43:33 +01002618{
Ulf Hanssond0ce8502013-12-03 11:04:28 +01002619 struct uart_amba_port *uap = dev_get_drvdata(dev);
Leo Chenb736b892009-07-28 23:43:33 +01002620
2621 if (!uap)
2622 return -EINVAL;
2623
2624 return uart_suspend_port(&amba_reg, &uap->port);
2625}
2626
Ulf Hanssond0ce8502013-12-03 11:04:28 +01002627static int pl011_resume(struct device *dev)
Leo Chenb736b892009-07-28 23:43:33 +01002628{
Ulf Hanssond0ce8502013-12-03 11:04:28 +01002629 struct uart_amba_port *uap = dev_get_drvdata(dev);
Leo Chenb736b892009-07-28 23:43:33 +01002630
2631 if (!uap)
2632 return -EINVAL;
2633
2634 return uart_resume_port(&amba_reg, &uap->port);
2635}
2636#endif
2637
Ulf Hanssond0ce8502013-12-03 11:04:28 +01002638static SIMPLE_DEV_PM_OPS(pl011_dev_pm_ops, pl011_suspend, pl011_resume);
2639
Andre Przywara0dd1e242015-05-21 17:26:23 +01002640static int sbsa_uart_probe(struct platform_device *pdev)
2641{
2642 struct uart_amba_port *uap;
2643 struct resource *r;
2644 int portnr, ret;
2645 int baudrate;
2646
2647 /*
2648 * Check the mandatory baud rate parameter in the DT node early
2649 * so that we can easily exit with the error.
2650 */
2651 if (pdev->dev.of_node) {
2652 struct device_node *np = pdev->dev.of_node;
2653
2654 ret = of_property_read_u32(np, "current-speed", &baudrate);
2655 if (ret)
2656 return ret;
2657 } else {
2658 baudrate = 115200;
2659 }
2660
2661 portnr = pl011_find_free_port();
2662 if (portnr < 0)
2663 return portnr;
2664
2665 uap = devm_kzalloc(&pdev->dev, sizeof(struct uart_amba_port),
2666 GFP_KERNEL);
2667 if (!uap)
2668 return -ENOMEM;
2669
2670 uap->vendor = &vendor_sbsa;
Jun Nie2c096a92015-07-31 15:49:17 +08002671 uap->reg_lut = vendor_sbsa.reg_lut;
Jun Nie8cd90e52015-07-31 15:49:19 +08002672 uap->fr_busy = vendor_sbsa.fr_busy;
2673 uap->fr_dsr = vendor_sbsa.fr_dsr;
2674 uap->fr_cts = vendor_sbsa.fr_cts;
2675 uap->fr_ri = vendor_sbsa.fr_ri;
Andre Przywara0dd1e242015-05-21 17:26:23 +01002676 uap->fifosize = 32;
2677 uap->port.irq = platform_get_irq(pdev, 0);
2678 uap->port.ops = &sbsa_uart_pops;
2679 uap->fixed_baud = baudrate;
2680
2681 snprintf(uap->type, sizeof(uap->type), "SBSA");
2682
2683 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2684
2685 ret = pl011_setup_port(&pdev->dev, uap, r, portnr);
2686 if (ret)
2687 return ret;
2688
2689 platform_set_drvdata(pdev, uap);
2690
2691 return pl011_register_port(uap);
2692}
2693
2694static int sbsa_uart_remove(struct platform_device *pdev)
2695{
2696 struct uart_amba_port *uap = platform_get_drvdata(pdev);
2697
2698 uart_remove_one_port(&amba_reg, &uap->port);
2699 pl011_unregister_port(uap);
2700 return 0;
2701}
2702
2703static const struct of_device_id sbsa_uart_of_match[] = {
2704 { .compatible = "arm,sbsa-uart", },
2705 {},
2706};
2707MODULE_DEVICE_TABLE(of, sbsa_uart_of_match);
2708
Graeme Gregory3db9ab02015-05-21 17:26:24 +01002709static const struct acpi_device_id sbsa_uart_acpi_match[] = {
2710 { "ARMH0011", 0 },
2711 {},
2712};
2713MODULE_DEVICE_TABLE(acpi, sbsa_uart_acpi_match);
2714
Andre Przywara0dd1e242015-05-21 17:26:23 +01002715static struct platform_driver arm_sbsa_uart_platform_driver = {
2716 .probe = sbsa_uart_probe,
2717 .remove = sbsa_uart_remove,
2718 .driver = {
2719 .name = "sbsa-uart",
2720 .of_match_table = of_match_ptr(sbsa_uart_of_match),
Graeme Gregory3db9ab02015-05-21 17:26:24 +01002721 .acpi_match_table = ACPI_PTR(sbsa_uart_acpi_match),
Andre Przywara0dd1e242015-05-21 17:26:23 +01002722 },
2723};
2724
Jun Nie8cd90e52015-07-31 15:49:19 +08002725#ifdef CONFIG_ARM_AMBA
Russell King2c39c9e2010-07-27 08:50:16 +01002726static struct amba_id pl011_ids[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727 {
2728 .id = 0x00041011,
2729 .mask = 0x000fffff,
Alessandro Rubini5926a292009-06-04 17:43:04 +01002730 .data = &vendor_arm,
2731 },
2732 {
2733 .id = 0x00380802,
2734 .mask = 0x00ffffff,
2735 .data = &vendor_st,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736 },
2737 { 0, 0 },
2738};
2739
Dave Martin60f7a332011-10-05 15:15:22 +01002740MODULE_DEVICE_TABLE(amba, pl011_ids);
2741
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742static struct amba_driver pl011_driver = {
2743 .drv = {
2744 .name = "uart-pl011",
Ulf Hanssond0ce8502013-12-03 11:04:28 +01002745 .pm = &pl011_dev_pm_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746 },
2747 .id_table = pl011_ids,
2748 .probe = pl011_probe,
2749 .remove = pl011_remove,
2750};
Jun Nie8cd90e52015-07-31 15:49:19 +08002751#endif
2752
2753#ifdef CONFIG_SOC_ZX296702
2754static const struct of_device_id zx_uart_dt_ids[] = {
2755 { .compatible = "zte,zx296702-uart", },
2756 { /* sentinel */ }
2757};
2758MODULE_DEVICE_TABLE(of, zx_uart_dt_ids);
2759
2760static struct platform_driver zx_uart_driver = {
2761 .driver = {
2762 .name = "zx-uart",
2763 .owner = THIS_MODULE,
2764 .pm = &pl011_dev_pm_ops,
2765 .of_match_table = zx_uart_dt_ids,
2766 },
2767 .probe = zx_uart_probe,
2768 .remove = zx_uart_remove,
2769};
2770#endif
2771
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772
2773static int __init pl011_init(void)
2774{
Jun Nie8cd90e52015-07-31 15:49:19 +08002775 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776 printk(KERN_INFO "Serial: AMBA PL011 UART driver\n");
2777
Andre Przywara0dd1e242015-05-21 17:26:23 +01002778 if (platform_driver_register(&arm_sbsa_uart_platform_driver))
2779 pr_warn("could not register SBSA UART platform driver\n");
Jun Nie8cd90e52015-07-31 15:49:19 +08002780
2781#ifdef CONFIG_SOC_ZX296702
2782 ret = platform_driver_register(&zx_uart_driver);
2783 if (ret)
2784 pr_warn("could not register ZX UART platform driver\n");
2785#endif
2786
2787#ifdef CONFIG_ARM_AMBA
2788 ret = amba_driver_register(&pl011_driver);
2789#endif
2790 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791}
2792
2793static void __exit pl011_exit(void)
2794{
Andre Przywara0dd1e242015-05-21 17:26:23 +01002795 platform_driver_unregister(&arm_sbsa_uart_platform_driver);
Jun Nie8cd90e52015-07-31 15:49:19 +08002796#ifdef CONFIG_SOC_ZX296702
2797 platform_driver_unregister(&zx_uart_driver);
2798#endif
2799#ifdef CONFIG_ARM_AMBA
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800 amba_driver_unregister(&pl011_driver);
Jun Nie8cd90e52015-07-31 15:49:19 +08002801#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802}
2803
Alessandro Rubini4dd9e742009-05-05 05:54:13 +01002804/*
2805 * While this can be a module, if builtin it's most likely the console
2806 * So let's leave module_exit but move module_init to an earlier place
2807 */
2808arch_initcall(pl011_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809module_exit(pl011_exit);
2810
2811MODULE_AUTHOR("ARM Ltd/Deep Blue Solutions Ltd");
2812MODULE_DESCRIPTION("ARM AMBA serial port driver");
2813MODULE_LICENSE("GPL");