blob: 3914ad0c2c26fccab03857ff8ae1593da54e260b [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
Russell King9f25bc52015-11-03 14:51:13 +000063#include "amba-pl011.h"
64
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#define UART_NR 14
66
67#define SERIAL_AMBA_MAJOR 204
68#define SERIAL_AMBA_MINOR 64
69#define SERIAL_AMBA_NR UART_NR
70
71#define AMBA_ISR_PASS_LIMIT 256
72
Russell Kingb63d4f02005-11-19 11:10:35 +000073#define UART_DR_ERROR (UART011_DR_OE|UART011_DR_BE|UART011_DR_PE|UART011_DR_FE)
74#define UART_DUMMY_DR_RX (1 << 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
Russell Kingdebb7f62015-11-16 17:40:26 +000076static u16 pl011_std_offsets[REG_ARRAY_SIZE] = {
77 [REG_DR] = UART01x_DR,
Russell Kingdebb7f62015-11-16 17:40:26 +000078 [REG_FR] = UART01x_FR,
Russell Kinge4df9a82015-11-16 17:40:41 +000079 [REG_LCRH_RX] = UART011_LCRH,
80 [REG_LCRH_TX] = UART011_LCRH,
Russell Kingdebb7f62015-11-16 17:40:26 +000081 [REG_IBRD] = UART011_IBRD,
82 [REG_FBRD] = UART011_FBRD,
Russell Kingdebb7f62015-11-16 17:40:26 +000083 [REG_CR] = UART011_CR,
84 [REG_IFLS] = UART011_IFLS,
85 [REG_IMSC] = UART011_IMSC,
86 [REG_RIS] = UART011_RIS,
87 [REG_MIS] = UART011_MIS,
88 [REG_ICR] = UART011_ICR,
89 [REG_DMACR] = UART011_DMACR,
Russell Kingdebb7f62015-11-16 17:40:26 +000090};
91
Alessandro Rubini5926a292009-06-04 17:43:04 +010092/* There is by now at least one vendor with differing details, so handle it */
93struct vendor_data {
Russell King439403b2015-11-16 17:40:31 +000094 const u16 *reg_offset;
Alessandro Rubini5926a292009-06-04 17:43:04 +010095 unsigned int ifls;
Shawn Guo0e125a52016-07-08 17:00:39 +080096 unsigned int fr_busy;
97 unsigned int fr_dsr;
98 unsigned int fr_cts;
99 unsigned int fr_ri;
Russell King84c3e032015-11-16 17:40:52 +0000100 bool access_32b;
Linus Walleijac3e3fb2010-06-02 20:40:22 +0100101 bool oversampling;
Russell King38d62432010-12-22 17:59:16 +0000102 bool dma_threshold;
Rajanikanth H.V4fd06902012-03-26 11:17:02 +0200103 bool cts_event_workaround;
Andre Przywara71eec482015-05-21 17:26:21 +0100104 bool always_enabled;
Andre Przywaracefc2d12015-05-21 17:26:22 +0100105 bool fixed_options;
Jongsung Kim78506f22013-04-15 14:45:25 +0900106
Jongsung Kimea336402013-05-10 18:05:35 +0900107 unsigned int (*get_fifosize)(struct amba_device *dev);
Alessandro Rubini5926a292009-06-04 17:43:04 +0100108};
109
Jongsung Kimea336402013-05-10 18:05:35 +0900110static unsigned int get_fifosize_arm(struct amba_device *dev)
Jongsung Kim78506f22013-04-15 14:45:25 +0900111{
Jongsung Kimea336402013-05-10 18:05:35 +0900112 return amba_rev(dev) < 3 ? 16 : 32;
Jongsung Kim78506f22013-04-15 14:45:25 +0900113}
114
Alessandro Rubini5926a292009-06-04 17:43:04 +0100115static struct vendor_data vendor_arm = {
Russell King439403b2015-11-16 17:40:31 +0000116 .reg_offset = pl011_std_offsets,
Alessandro Rubini5926a292009-06-04 17:43:04 +0100117 .ifls = UART011_IFLS_RX4_8|UART011_IFLS_TX4_8,
Shawn Guo0e125a52016-07-08 17:00:39 +0800118 .fr_busy = UART01x_FR_BUSY,
119 .fr_dsr = UART01x_FR_DSR,
120 .fr_cts = UART01x_FR_CTS,
121 .fr_ri = UART011_FR_RI,
Linus Walleijac3e3fb2010-06-02 20:40:22 +0100122 .oversampling = false,
Russell King38d62432010-12-22 17:59:16 +0000123 .dma_threshold = false,
Rajanikanth H.V4fd06902012-03-26 11:17:02 +0200124 .cts_event_workaround = false,
Andre Przywara71eec482015-05-21 17:26:21 +0100125 .always_enabled = false,
Andre Przywaracefc2d12015-05-21 17:26:22 +0100126 .fixed_options = false,
Jongsung Kim78506f22013-04-15 14:45:25 +0900127 .get_fifosize = get_fifosize_arm,
Alessandro Rubini5926a292009-06-04 17:43:04 +0100128};
129
Andre Przywara0dd1e242015-05-21 17:26:23 +0100130static struct vendor_data vendor_sbsa = {
Russell King439403b2015-11-16 17:40:31 +0000131 .reg_offset = pl011_std_offsets,
Shawn Guo0e125a52016-07-08 17:00:39 +0800132 .fr_busy = UART01x_FR_BUSY,
133 .fr_dsr = UART01x_FR_DSR,
134 .fr_cts = UART01x_FR_CTS,
135 .fr_ri = UART011_FR_RI,
Christopher Covington1aabf522016-04-01 17:23:58 -0400136 .access_32b = true,
Andre Przywara0dd1e242015-05-21 17:26:23 +0100137 .oversampling = false,
138 .dma_threshold = false,
139 .cts_event_workaround = false,
140 .always_enabled = true,
141 .fixed_options = true,
142};
143
Russell Kingbf69ff82015-11-16 17:40:36 +0000144static u16 pl011_st_offsets[REG_ARRAY_SIZE] = {
145 [REG_DR] = UART01x_DR,
146 [REG_ST_DMAWM] = ST_UART011_DMAWM,
147 [REG_ST_TIMEOUT] = ST_UART011_TIMEOUT,
148 [REG_FR] = UART01x_FR,
Russell Kinge4df9a82015-11-16 17:40:41 +0000149 [REG_LCRH_RX] = ST_UART011_LCRH_RX,
150 [REG_LCRH_TX] = ST_UART011_LCRH_TX,
Russell Kingbf69ff82015-11-16 17:40:36 +0000151 [REG_IBRD] = UART011_IBRD,
152 [REG_FBRD] = UART011_FBRD,
Russell Kingbf69ff82015-11-16 17:40:36 +0000153 [REG_CR] = UART011_CR,
154 [REG_IFLS] = UART011_IFLS,
155 [REG_IMSC] = UART011_IMSC,
156 [REG_RIS] = UART011_RIS,
157 [REG_MIS] = UART011_MIS,
158 [REG_ICR] = UART011_ICR,
159 [REG_DMACR] = UART011_DMACR,
160 [REG_ST_XFCR] = ST_UART011_XFCR,
161 [REG_ST_XON1] = ST_UART011_XON1,
162 [REG_ST_XON2] = ST_UART011_XON2,
163 [REG_ST_XOFF1] = ST_UART011_XOFF1,
164 [REG_ST_XOFF2] = ST_UART011_XOFF2,
165 [REG_ST_ITCR] = ST_UART011_ITCR,
166 [REG_ST_ITIP] = ST_UART011_ITIP,
167 [REG_ST_ABCR] = ST_UART011_ABCR,
168 [REG_ST_ABIMSC] = ST_UART011_ABIMSC,
169};
170
Jongsung Kimea336402013-05-10 18:05:35 +0900171static unsigned int get_fifosize_st(struct amba_device *dev)
Jongsung Kim78506f22013-04-15 14:45:25 +0900172{
173 return 64;
174}
175
Alessandro Rubini5926a292009-06-04 17:43:04 +0100176static struct vendor_data vendor_st = {
Russell Kingbf69ff82015-11-16 17:40:36 +0000177 .reg_offset = pl011_st_offsets,
Alessandro Rubini5926a292009-06-04 17:43:04 +0100178 .ifls = UART011_IFLS_RX_HALF|UART011_IFLS_TX_HALF,
Shawn Guo0e125a52016-07-08 17:00:39 +0800179 .fr_busy = UART01x_FR_BUSY,
180 .fr_dsr = UART01x_FR_DSR,
181 .fr_cts = UART01x_FR_CTS,
182 .fr_ri = UART011_FR_RI,
Linus Walleijac3e3fb2010-06-02 20:40:22 +0100183 .oversampling = true,
Russell King38d62432010-12-22 17:59:16 +0000184 .dma_threshold = true,
Rajanikanth H.V4fd06902012-03-26 11:17:02 +0200185 .cts_event_workaround = true,
Andre Przywara71eec482015-05-21 17:26:21 +0100186 .always_enabled = false,
Andre Przywaracefc2d12015-05-21 17:26:22 +0100187 .fixed_options = false,
Jongsung Kim78506f22013-04-15 14:45:25 +0900188 .get_fifosize = get_fifosize_st,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189};
190
Russell King7ec75872015-11-16 17:40:57 +0000191static const u16 pl011_zte_offsets[REG_ARRAY_SIZE] = {
192 [REG_DR] = ZX_UART011_DR,
193 [REG_FR] = ZX_UART011_FR,
194 [REG_LCRH_RX] = ZX_UART011_LCRH,
195 [REG_LCRH_TX] = ZX_UART011_LCRH,
196 [REG_IBRD] = ZX_UART011_IBRD,
197 [REG_FBRD] = ZX_UART011_FBRD,
198 [REG_CR] = ZX_UART011_CR,
199 [REG_IFLS] = ZX_UART011_IFLS,
200 [REG_IMSC] = ZX_UART011_IMSC,
201 [REG_RIS] = ZX_UART011_RIS,
202 [REG_MIS] = ZX_UART011_MIS,
203 [REG_ICR] = ZX_UART011_ICR,
204 [REG_DMACR] = ZX_UART011_DMACR,
205};
206
Arnd Bergmannff52a9a2016-01-20 11:40:03 +0100207static struct vendor_data vendor_zte __maybe_unused = {
Russell King7ec75872015-11-16 17:40:57 +0000208 .reg_offset = pl011_zte_offsets,
209 .access_32b = true,
210 .ifls = UART011_IFLS_RX4_8|UART011_IFLS_TX4_8,
Shawn Guo0e125a52016-07-08 17:00:39 +0800211 .fr_busy = ZX_UART01x_FR_BUSY,
212 .fr_dsr = ZX_UART01x_FR_DSR,
213 .fr_cts = ZX_UART01x_FR_CTS,
214 .fr_ri = ZX_UART011_FR_RI,
Russell King7ec75872015-11-16 17:40:57 +0000215 .get_fifosize = get_fifosize_arm,
216};
217
Russell King68b65f72010-12-22 17:24:39 +0000218/* Deals with DMA transactions */
Linus Walleijead76f32011-02-24 13:21:08 +0100219
220struct pl011_sgbuf {
221 struct scatterlist sg;
222 char *buf;
223};
224
225struct pl011_dmarx_data {
226 struct dma_chan *chan;
227 struct completion complete;
228 bool use_buf_b;
229 struct pl011_sgbuf sgbuf_a;
230 struct pl011_sgbuf sgbuf_b;
231 dma_cookie_t cookie;
232 bool running;
Chanho Mincb06ff12013-03-27 18:38:11 +0900233 struct timer_list timer;
234 unsigned int last_residue;
235 unsigned long last_jiffies;
236 bool auto_poll_rate;
237 unsigned int poll_rate;
238 unsigned int poll_timeout;
Linus Walleijead76f32011-02-24 13:21:08 +0100239};
240
Russell King68b65f72010-12-22 17:24:39 +0000241struct pl011_dmatx_data {
242 struct dma_chan *chan;
243 struct scatterlist sg;
244 char *buf;
245 bool queued;
246};
247
Russell Kingc19f12b2010-12-22 17:48:26 +0000248/*
249 * We wrap our port structure around the generic uart_port.
250 */
251struct uart_amba_port {
252 struct uart_port port;
Russell Kingdebb7f62015-11-16 17:40:26 +0000253 const u16 *reg_offset;
Russell Kingc19f12b2010-12-22 17:48:26 +0000254 struct clk *clk;
255 const struct vendor_data *vendor;
Russell King68b65f72010-12-22 17:24:39 +0000256 unsigned int dmacr; /* dma control reg */
Russell Kingc19f12b2010-12-22 17:48:26 +0000257 unsigned int im; /* interrupt mask */
258 unsigned int old_status;
Russell Kingffca2b12010-12-22 17:13:05 +0000259 unsigned int fifosize; /* vendor-specific */
Shreshtha Kumar Sahud8d8ffa2012-01-18 15:53:59 +0530260 unsigned int old_cr; /* state during shutdown */
Russell Kingc19f12b2010-12-22 17:48:26 +0000261 bool autorts;
Andre Przywaracefc2d12015-05-21 17:26:22 +0100262 unsigned int fixed_baud; /* vendor-set fixed baud rate */
Russell Kingc19f12b2010-12-22 17:48:26 +0000263 char type[12];
Russell King68b65f72010-12-22 17:24:39 +0000264#ifdef CONFIG_DMA_ENGINE
265 /* DMA stuff */
Linus Walleijead76f32011-02-24 13:21:08 +0100266 bool using_tx_dma;
267 bool using_rx_dma;
268 struct pl011_dmarx_data dmarx;
Russell King68b65f72010-12-22 17:24:39 +0000269 struct pl011_dmatx_data dmatx;
Jorge Ramirez-Ortiz1c9be312015-03-06 13:05:40 -0500270 bool dma_probed;
Russell King68b65f72010-12-22 17:24:39 +0000271#endif
Russell Kingc19f12b2010-12-22 17:48:26 +0000272};
273
Russell King9f25bc52015-11-03 14:51:13 +0000274static unsigned int pl011_reg_to_offset(const struct uart_amba_port *uap,
275 unsigned int reg)
276{
Russell Kingdebb7f62015-11-16 17:40:26 +0000277 return uap->reg_offset[reg];
Russell King9f25bc52015-11-03 14:51:13 +0000278}
279
Russell Kingb2a4e242015-11-03 14:51:03 +0000280static unsigned int pl011_read(const struct uart_amba_port *uap,
281 unsigned int reg)
Russell King75836332015-11-03 14:50:58 +0000282{
Russell King84c3e032015-11-16 17:40:52 +0000283 void __iomem *addr = uap->port.membase + pl011_reg_to_offset(uap, reg);
284
Timur Tabi3b78fae2016-01-04 15:37:42 -0600285 return (uap->port.iotype == UPIO_MEM32) ?
286 readl_relaxed(addr) : readw_relaxed(addr);
Russell King75836332015-11-03 14:50:58 +0000287}
288
Russell Kingb2a4e242015-11-03 14:51:03 +0000289static void pl011_write(unsigned int val, const struct uart_amba_port *uap,
290 unsigned int reg)
Russell King75836332015-11-03 14:50:58 +0000291{
Russell King84c3e032015-11-16 17:40:52 +0000292 void __iomem *addr = uap->port.membase + pl011_reg_to_offset(uap, reg);
293
Timur Tabi3b78fae2016-01-04 15:37:42 -0600294 if (uap->port.iotype == UPIO_MEM32)
Russell Kingf5ce6ed2015-11-16 17:41:02 +0000295 writel_relaxed(val, addr);
Russell King84c3e032015-11-16 17:40:52 +0000296 else
Russell Kingf5ce6ed2015-11-16 17:41:02 +0000297 writew_relaxed(val, addr);
Russell King75836332015-11-03 14:50:58 +0000298}
299
Russell King68b65f72010-12-22 17:24:39 +0000300/*
Linus Walleij29772c42011-02-24 13:21:36 +0100301 * Reads up to 256 characters from the FIFO or until it's empty and
302 * inserts them into the TTY layer. Returns the number of characters
303 * read from the FIFO.
304 */
305static int pl011_fifo_to_tty(struct uart_amba_port *uap)
306{
Timur Tabi71a5cd82015-10-07 15:27:16 -0500307 u16 status;
308 unsigned int ch, flag, max_count = 256;
Linus Walleij29772c42011-02-24 13:21:36 +0100309 int fifotaken = 0;
310
311 while (max_count--) {
Russell King9f25bc52015-11-03 14:51:13 +0000312 status = pl011_read(uap, REG_FR);
Linus Walleij29772c42011-02-24 13:21:36 +0100313 if (status & UART01x_FR_RXFE)
314 break;
315
316 /* Take chars from the FIFO and update status */
Russell King9f25bc52015-11-03 14:51:13 +0000317 ch = pl011_read(uap, REG_DR) | UART_DUMMY_DR_RX;
Linus Walleij29772c42011-02-24 13:21:36 +0100318 flag = TTY_NORMAL;
319 uap->port.icount.rx++;
320 fifotaken++;
321
322 if (unlikely(ch & UART_DR_ERROR)) {
323 if (ch & UART011_DR_BE) {
324 ch &= ~(UART011_DR_FE | UART011_DR_PE);
325 uap->port.icount.brk++;
326 if (uart_handle_break(&uap->port))
327 continue;
328 } else if (ch & UART011_DR_PE)
329 uap->port.icount.parity++;
330 else if (ch & UART011_DR_FE)
331 uap->port.icount.frame++;
332 if (ch & UART011_DR_OE)
333 uap->port.icount.overrun++;
334
335 ch &= uap->port.read_status_mask;
336
337 if (ch & UART011_DR_BE)
338 flag = TTY_BREAK;
339 else if (ch & UART011_DR_PE)
340 flag = TTY_PARITY;
341 else if (ch & UART011_DR_FE)
342 flag = TTY_FRAME;
343 }
344
345 if (uart_handle_sysrq_char(&uap->port, ch & 255))
346 continue;
347
348 uart_insert_char(&uap->port, ch, UART011_DR_OE, ch, flag);
349 }
350
351 return fifotaken;
352}
353
354
355/*
Russell King68b65f72010-12-22 17:24:39 +0000356 * All the DMA operation mode stuff goes inside this ifdef.
357 * This assumes that you have a generic DMA device interface,
358 * no custom DMA interfaces are supported.
359 */
360#ifdef CONFIG_DMA_ENGINE
361
362#define PL011_DMA_BUFFER_SIZE PAGE_SIZE
363
Linus Walleijead76f32011-02-24 13:21:08 +0100364static int pl011_sgbuf_init(struct dma_chan *chan, struct pl011_sgbuf *sg,
365 enum dma_data_direction dir)
366{
Chanho Mincb06ff12013-03-27 18:38:11 +0900367 dma_addr_t dma_addr;
368
369 sg->buf = dma_alloc_coherent(chan->device->dev,
370 PL011_DMA_BUFFER_SIZE, &dma_addr, GFP_KERNEL);
Linus Walleijead76f32011-02-24 13:21:08 +0100371 if (!sg->buf)
372 return -ENOMEM;
373
Chanho Mincb06ff12013-03-27 18:38:11 +0900374 sg_init_table(&sg->sg, 1);
375 sg_set_page(&sg->sg, phys_to_page(dma_addr),
376 PL011_DMA_BUFFER_SIZE, offset_in_page(dma_addr));
377 sg_dma_address(&sg->sg) = dma_addr;
Andrew Jacksonc64be922014-11-07 14:14:43 +0000378 sg_dma_len(&sg->sg) = PL011_DMA_BUFFER_SIZE;
Linus Walleijead76f32011-02-24 13:21:08 +0100379
Linus Walleijead76f32011-02-24 13:21:08 +0100380 return 0;
381}
382
383static void pl011_sgbuf_free(struct dma_chan *chan, struct pl011_sgbuf *sg,
384 enum dma_data_direction dir)
385{
386 if (sg->buf) {
Chanho Mincb06ff12013-03-27 18:38:11 +0900387 dma_free_coherent(chan->device->dev,
388 PL011_DMA_BUFFER_SIZE, sg->buf,
389 sg_dma_address(&sg->sg));
Linus Walleijead76f32011-02-24 13:21:08 +0100390 }
391}
392
Jorge Ramirez-Ortiz1c9be312015-03-06 13:05:40 -0500393static void pl011_dma_probe(struct uart_amba_port *uap)
Russell King68b65f72010-12-22 17:24:39 +0000394{
395 /* DMA is the sole user of the platform data right now */
Jingoo Han574de552013-07-30 17:06:57 +0900396 struct amba_pl011_data *plat = dev_get_platdata(uap->port.dev);
Jorge Ramirez-Ortiz1c9be312015-03-06 13:05:40 -0500397 struct device *dev = uap->port.dev;
Russell King68b65f72010-12-22 17:24:39 +0000398 struct dma_slave_config tx_conf = {
Russell King9f25bc52015-11-03 14:51:13 +0000399 .dst_addr = uap->port.mapbase +
400 pl011_reg_to_offset(uap, REG_DR),
Russell King68b65f72010-12-22 17:24:39 +0000401 .dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE,
Vinod Koula485df42011-10-14 10:47:38 +0530402 .direction = DMA_MEM_TO_DEV,
Russell King68b65f72010-12-22 17:24:39 +0000403 .dst_maxburst = uap->fifosize >> 1,
Viresh Kumar258aea72012-02-01 16:12:19 +0530404 .device_fc = false,
Russell King68b65f72010-12-22 17:24:39 +0000405 };
406 struct dma_chan *chan;
407 dma_cap_mask_t mask;
408
Jorge Ramirez-Ortiz1c9be312015-03-06 13:05:40 -0500409 uap->dma_probed = true;
410 chan = dma_request_slave_channel_reason(dev, "tx");
411 if (IS_ERR(chan)) {
412 if (PTR_ERR(chan) == -EPROBE_DEFER) {
Jorge Ramirez-Ortiz1c9be312015-03-06 13:05:40 -0500413 uap->dma_probed = false;
414 return;
415 }
Russell King68b65f72010-12-22 17:24:39 +0000416
Arnd Bergmann787b0c12013-01-28 16:24:37 +0000417 /* We need platform data */
418 if (!plat || !plat->dma_filter) {
419 dev_info(uap->port.dev, "no DMA platform data\n");
420 return;
421 }
422
423 /* Try to acquire a generic DMA engine slave TX channel */
424 dma_cap_zero(mask);
425 dma_cap_set(DMA_SLAVE, mask);
426
427 chan = dma_request_channel(mask, plat->dma_filter,
428 plat->dma_tx_param);
429 if (!chan) {
430 dev_err(uap->port.dev, "no TX DMA channel!\n");
431 return;
432 }
Russell King68b65f72010-12-22 17:24:39 +0000433 }
434
435 dmaengine_slave_config(chan, &tx_conf);
436 uap->dmatx.chan = chan;
437
438 dev_info(uap->port.dev, "DMA channel TX %s\n",
439 dma_chan_name(uap->dmatx.chan));
Linus Walleijead76f32011-02-24 13:21:08 +0100440
441 /* Optionally make use of an RX channel as well */
Arnd Bergmann787b0c12013-01-28 16:24:37 +0000442 chan = dma_request_slave_channel(dev, "rx");
Rob Herring0d3c6732014-04-18 17:19:57 -0500443
Robin Murphyd9e105c2016-03-03 16:35:35 +0000444 if (!chan && plat && plat->dma_rx_param) {
Arnd Bergmann787b0c12013-01-28 16:24:37 +0000445 chan = dma_request_channel(mask, plat->dma_filter, plat->dma_rx_param);
446
447 if (!chan) {
448 dev_err(uap->port.dev, "no RX DMA channel!\n");
449 return;
450 }
451 }
452
453 if (chan) {
Linus Walleijead76f32011-02-24 13:21:08 +0100454 struct dma_slave_config rx_conf = {
Russell King9f25bc52015-11-03 14:51:13 +0000455 .src_addr = uap->port.mapbase +
456 pl011_reg_to_offset(uap, REG_DR),
Linus Walleijead76f32011-02-24 13:21:08 +0100457 .src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE,
Vinod Koula485df42011-10-14 10:47:38 +0530458 .direction = DMA_DEV_TO_MEM,
Guennadi Liakhovetskib2aeb772014-04-12 19:47:17 +0200459 .src_maxburst = uap->fifosize >> 2,
Viresh Kumar258aea72012-02-01 16:12:19 +0530460 .device_fc = false,
Linus Walleijead76f32011-02-24 13:21:08 +0100461 };
Andrew Jackson2d3b7d62014-11-07 14:14:47 +0000462 struct dma_slave_caps caps;
Linus Walleijead76f32011-02-24 13:21:08 +0100463
Andrew Jackson2d3b7d62014-11-07 14:14:47 +0000464 /*
465 * Some DMA controllers provide information on their capabilities.
466 * If the controller does, check for suitable residue processing
467 * otherwise assime all is well.
468 */
469 if (0 == dma_get_slave_caps(chan, &caps)) {
470 if (caps.residue_granularity ==
471 DMA_RESIDUE_GRANULARITY_DESCRIPTOR) {
472 dma_release_channel(chan);
473 dev_info(uap->port.dev,
474 "RX DMA disabled - no residue processing\n");
475 return;
476 }
477 }
Linus Walleijead76f32011-02-24 13:21:08 +0100478 dmaengine_slave_config(chan, &rx_conf);
479 uap->dmarx.chan = chan;
480
Andrew Jackson98267d32014-11-07 14:14:23 +0000481 uap->dmarx.auto_poll_rate = false;
Greg Kroah-Hartman8f898bf2013-12-17 09:33:18 -0800482 if (plat && plat->dma_rx_poll_enable) {
Chanho Mincb06ff12013-03-27 18:38:11 +0900483 /* Set poll rate if specified. */
484 if (plat->dma_rx_poll_rate) {
485 uap->dmarx.auto_poll_rate = false;
486 uap->dmarx.poll_rate = plat->dma_rx_poll_rate;
487 } else {
488 /*
489 * 100 ms defaults to poll rate if not
490 * specified. This will be adjusted with
491 * the baud rate at set_termios.
492 */
493 uap->dmarx.auto_poll_rate = true;
494 uap->dmarx.poll_rate = 100;
495 }
496 /* 3 secs defaults poll_timeout if not specified. */
497 if (plat->dma_rx_poll_timeout)
498 uap->dmarx.poll_timeout =
499 plat->dma_rx_poll_timeout;
500 else
501 uap->dmarx.poll_timeout = 3000;
Andrew Jackson98267d32014-11-07 14:14:23 +0000502 } else if (!plat && dev->of_node) {
503 uap->dmarx.auto_poll_rate = of_property_read_bool(
504 dev->of_node, "auto-poll");
505 if (uap->dmarx.auto_poll_rate) {
506 u32 x;
Chanho Mincb06ff12013-03-27 18:38:11 +0900507
Andrew Jackson98267d32014-11-07 14:14:23 +0000508 if (0 == of_property_read_u32(dev->of_node,
509 "poll-rate-ms", &x))
510 uap->dmarx.poll_rate = x;
511 else
512 uap->dmarx.poll_rate = 100;
513 if (0 == of_property_read_u32(dev->of_node,
514 "poll-timeout-ms", &x))
515 uap->dmarx.poll_timeout = x;
516 else
517 uap->dmarx.poll_timeout = 3000;
518 }
519 }
Linus Walleijead76f32011-02-24 13:21:08 +0100520 dev_info(uap->port.dev, "DMA channel RX %s\n",
521 dma_chan_name(uap->dmarx.chan));
522 }
Russell King68b65f72010-12-22 17:24:39 +0000523}
524
Russell King68b65f72010-12-22 17:24:39 +0000525static void pl011_dma_remove(struct uart_amba_port *uap)
526{
Russell King68b65f72010-12-22 17:24:39 +0000527 if (uap->dmatx.chan)
528 dma_release_channel(uap->dmatx.chan);
Linus Walleijead76f32011-02-24 13:21:08 +0100529 if (uap->dmarx.chan)
530 dma_release_channel(uap->dmarx.chan);
Russell King68b65f72010-12-22 17:24:39 +0000531}
532
Dave Martin734745c2015-03-04 12:27:33 +0000533/* Forward declare these for the refill routine */
Russell King68b65f72010-12-22 17:24:39 +0000534static int pl011_dma_tx_refill(struct uart_amba_port *uap);
Dave Martin734745c2015-03-04 12:27:33 +0000535static void pl011_start_tx_pio(struct uart_amba_port *uap);
Russell King68b65f72010-12-22 17:24:39 +0000536
537/*
538 * The current DMA TX buffer has been sent.
539 * Try to queue up another DMA buffer.
540 */
541static void pl011_dma_tx_callback(void *data)
542{
543 struct uart_amba_port *uap = data;
544 struct pl011_dmatx_data *dmatx = &uap->dmatx;
545 unsigned long flags;
546 u16 dmacr;
547
548 spin_lock_irqsave(&uap->port.lock, flags);
549 if (uap->dmatx.queued)
550 dma_unmap_sg(dmatx->chan->device->dev, &dmatx->sg, 1,
551 DMA_TO_DEVICE);
552
553 dmacr = uap->dmacr;
554 uap->dmacr = dmacr & ~UART011_TXDMAE;
Russell King9f25bc52015-11-03 14:51:13 +0000555 pl011_write(uap->dmacr, uap, REG_DMACR);
Russell King68b65f72010-12-22 17:24:39 +0000556
557 /*
558 * If TX DMA was disabled, it means that we've stopped the DMA for
559 * some reason (eg, XOFF received, or we want to send an X-char.)
560 *
561 * Note: we need to be careful here of a potential race between DMA
562 * and the rest of the driver - if the driver disables TX DMA while
563 * a TX buffer completing, we must update the tx queued status to
564 * get further refills (hence we check dmacr).
565 */
566 if (!(dmacr & UART011_TXDMAE) || uart_tx_stopped(&uap->port) ||
567 uart_circ_empty(&uap->port.state->xmit)) {
568 uap->dmatx.queued = false;
569 spin_unlock_irqrestore(&uap->port.lock, flags);
570 return;
571 }
572
Dave Martin734745c2015-03-04 12:27:33 +0000573 if (pl011_dma_tx_refill(uap) <= 0)
Russell King68b65f72010-12-22 17:24:39 +0000574 /*
575 * We didn't queue a DMA buffer for some reason, but we
576 * have data pending to be sent. Re-enable the TX IRQ.
577 */
Dave Martin734745c2015-03-04 12:27:33 +0000578 pl011_start_tx_pio(uap);
579
Russell King68b65f72010-12-22 17:24:39 +0000580 spin_unlock_irqrestore(&uap->port.lock, flags);
581}
582
583/*
584 * Try to refill the TX DMA buffer.
585 * Locking: called with port lock held and IRQs disabled.
586 * Returns:
587 * 1 if we queued up a TX DMA buffer.
588 * 0 if we didn't want to handle this by DMA
589 * <0 on error
590 */
591static int pl011_dma_tx_refill(struct uart_amba_port *uap)
592{
593 struct pl011_dmatx_data *dmatx = &uap->dmatx;
594 struct dma_chan *chan = dmatx->chan;
595 struct dma_device *dma_dev = chan->device;
596 struct dma_async_tx_descriptor *desc;
597 struct circ_buf *xmit = &uap->port.state->xmit;
598 unsigned int count;
599
600 /*
601 * Try to avoid the overhead involved in using DMA if the
602 * transaction fits in the first half of the FIFO, by using
603 * the standard interrupt handling. This ensures that we
604 * issue a uart_write_wakeup() at the appropriate time.
605 */
606 count = uart_circ_chars_pending(xmit);
607 if (count < (uap->fifosize >> 1)) {
608 uap->dmatx.queued = false;
609 return 0;
610 }
611
612 /*
613 * Bodge: don't send the last character by DMA, as this
614 * will prevent XON from notifying us to restart DMA.
615 */
616 count -= 1;
617
618 /* Else proceed to copy the TX chars to the DMA buffer and fire DMA */
619 if (count > PL011_DMA_BUFFER_SIZE)
620 count = PL011_DMA_BUFFER_SIZE;
621
622 if (xmit->tail < xmit->head)
623 memcpy(&dmatx->buf[0], &xmit->buf[xmit->tail], count);
624 else {
625 size_t first = UART_XMIT_SIZE - xmit->tail;
Andrew Jacksone2a545a2014-11-07 14:14:39 +0000626 size_t second;
627
628 if (first > count)
629 first = count;
630 second = count - first;
Russell King68b65f72010-12-22 17:24:39 +0000631
632 memcpy(&dmatx->buf[0], &xmit->buf[xmit->tail], first);
633 if (second)
634 memcpy(&dmatx->buf[first], &xmit->buf[0], second);
635 }
636
637 dmatx->sg.length = count;
638
639 if (dma_map_sg(dma_dev->dev, &dmatx->sg, 1, DMA_TO_DEVICE) != 1) {
640 uap->dmatx.queued = false;
641 dev_dbg(uap->port.dev, "unable to map TX DMA\n");
642 return -EBUSY;
643 }
644
Alexandre Bounine16052822012-03-08 16:11:18 -0500645 desc = dmaengine_prep_slave_sg(chan, &dmatx->sg, 1, DMA_MEM_TO_DEV,
Russell King68b65f72010-12-22 17:24:39 +0000646 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
647 if (!desc) {
648 dma_unmap_sg(dma_dev->dev, &dmatx->sg, 1, DMA_TO_DEVICE);
649 uap->dmatx.queued = false;
650 /*
651 * If DMA cannot be used right now, we complete this
652 * transaction via IRQ and let the TTY layer retry.
653 */
654 dev_dbg(uap->port.dev, "TX DMA busy\n");
655 return -EBUSY;
656 }
657
658 /* Some data to go along to the callback */
659 desc->callback = pl011_dma_tx_callback;
660 desc->callback_param = uap;
661
662 /* All errors should happen at prepare time */
663 dmaengine_submit(desc);
664
665 /* Fire the DMA transaction */
666 dma_dev->device_issue_pending(chan);
667
668 uap->dmacr |= UART011_TXDMAE;
Russell King9f25bc52015-11-03 14:51:13 +0000669 pl011_write(uap->dmacr, uap, REG_DMACR);
Russell King68b65f72010-12-22 17:24:39 +0000670 uap->dmatx.queued = true;
671
672 /*
673 * Now we know that DMA will fire, so advance the ring buffer
674 * with the stuff we just dispatched.
675 */
676 xmit->tail = (xmit->tail + count) & (UART_XMIT_SIZE - 1);
677 uap->port.icount.tx += count;
678
679 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
680 uart_write_wakeup(&uap->port);
681
682 return 1;
683}
684
685/*
686 * We received a transmit interrupt without a pending X-char but with
687 * pending characters.
688 * Locking: called with port lock held and IRQs disabled.
689 * Returns:
690 * false if we want to use PIO to transmit
691 * true if we queued a DMA buffer
692 */
693static bool pl011_dma_tx_irq(struct uart_amba_port *uap)
694{
Linus Walleijead76f32011-02-24 13:21:08 +0100695 if (!uap->using_tx_dma)
Russell King68b65f72010-12-22 17:24:39 +0000696 return false;
697
698 /*
699 * If we already have a TX buffer queued, but received a
700 * TX interrupt, it will be because we've just sent an X-char.
701 * Ensure the TX DMA is enabled and the TX IRQ is disabled.
702 */
703 if (uap->dmatx.queued) {
704 uap->dmacr |= UART011_TXDMAE;
Russell King9f25bc52015-11-03 14:51:13 +0000705 pl011_write(uap->dmacr, uap, REG_DMACR);
Russell King68b65f72010-12-22 17:24:39 +0000706 uap->im &= ~UART011_TXIM;
Russell King9f25bc52015-11-03 14:51:13 +0000707 pl011_write(uap->im, uap, REG_IMSC);
Russell King68b65f72010-12-22 17:24:39 +0000708 return true;
709 }
710
711 /*
712 * We don't have a TX buffer queued, so try to queue one.
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300713 * If we successfully queued a buffer, mask the TX IRQ.
Russell King68b65f72010-12-22 17:24:39 +0000714 */
715 if (pl011_dma_tx_refill(uap) > 0) {
716 uap->im &= ~UART011_TXIM;
Russell King9f25bc52015-11-03 14:51:13 +0000717 pl011_write(uap->im, uap, REG_IMSC);
Russell King68b65f72010-12-22 17:24:39 +0000718 return true;
719 }
720 return false;
721}
722
723/*
724 * Stop the DMA transmit (eg, due to received XOFF).
725 * Locking: called with port lock held and IRQs disabled.
726 */
727static inline void pl011_dma_tx_stop(struct uart_amba_port *uap)
728{
729 if (uap->dmatx.queued) {
730 uap->dmacr &= ~UART011_TXDMAE;
Russell King9f25bc52015-11-03 14:51:13 +0000731 pl011_write(uap->dmacr, uap, REG_DMACR);
Russell King68b65f72010-12-22 17:24:39 +0000732 }
733}
734
735/*
736 * Try to start a DMA transmit, or in the case of an XON/OFF
737 * character queued for send, try to get that character out ASAP.
738 * Locking: called with port lock held and IRQs disabled.
739 * Returns:
740 * false if we want the TX IRQ to be enabled
741 * true if we have a buffer queued
742 */
743static inline bool pl011_dma_tx_start(struct uart_amba_port *uap)
744{
745 u16 dmacr;
746
Linus Walleijead76f32011-02-24 13:21:08 +0100747 if (!uap->using_tx_dma)
Russell King68b65f72010-12-22 17:24:39 +0000748 return false;
749
750 if (!uap->port.x_char) {
751 /* no X-char, try to push chars out in DMA mode */
752 bool ret = true;
753
754 if (!uap->dmatx.queued) {
755 if (pl011_dma_tx_refill(uap) > 0) {
756 uap->im &= ~UART011_TXIM;
Russell King9f25bc52015-11-03 14:51:13 +0000757 pl011_write(uap->im, uap, REG_IMSC);
Dave Martin734745c2015-03-04 12:27:33 +0000758 } else
Russell King68b65f72010-12-22 17:24:39 +0000759 ret = false;
Russell King68b65f72010-12-22 17:24:39 +0000760 } else if (!(uap->dmacr & UART011_TXDMAE)) {
761 uap->dmacr |= UART011_TXDMAE;
Russell King9f25bc52015-11-03 14:51:13 +0000762 pl011_write(uap->dmacr, uap, REG_DMACR);
Russell King68b65f72010-12-22 17:24:39 +0000763 }
764 return ret;
765 }
766
767 /*
768 * We have an X-char to send. Disable DMA to prevent it loading
769 * the TX fifo, and then see if we can stuff it into the FIFO.
770 */
771 dmacr = uap->dmacr;
772 uap->dmacr &= ~UART011_TXDMAE;
Russell King9f25bc52015-11-03 14:51:13 +0000773 pl011_write(uap->dmacr, uap, REG_DMACR);
Russell King68b65f72010-12-22 17:24:39 +0000774
Russell King9f25bc52015-11-03 14:51:13 +0000775 if (pl011_read(uap, REG_FR) & UART01x_FR_TXFF) {
Russell King68b65f72010-12-22 17:24:39 +0000776 /*
777 * No space in the FIFO, so enable the transmit interrupt
778 * so we know when there is space. Note that once we've
779 * loaded the character, we should just re-enable DMA.
780 */
781 return false;
782 }
783
Russell King9f25bc52015-11-03 14:51:13 +0000784 pl011_write(uap->port.x_char, uap, REG_DR);
Russell King68b65f72010-12-22 17:24:39 +0000785 uap->port.icount.tx++;
786 uap->port.x_char = 0;
787
788 /* Success - restore the DMA state */
789 uap->dmacr = dmacr;
Russell King9f25bc52015-11-03 14:51:13 +0000790 pl011_write(dmacr, uap, REG_DMACR);
Russell King68b65f72010-12-22 17:24:39 +0000791
792 return true;
793}
794
795/*
796 * Flush the transmit buffer.
797 * Locking: called with port lock held and IRQs disabled.
798 */
799static void pl011_dma_flush_buffer(struct uart_port *port)
Fabio Estevamb83286b2013-08-09 17:58:51 -0300800__releases(&uap->port.lock)
801__acquires(&uap->port.lock)
Russell King68b65f72010-12-22 17:24:39 +0000802{
Daniel Thompsona5820c22014-09-03 12:51:55 +0100803 struct uart_amba_port *uap =
804 container_of(port, struct uart_amba_port, port);
Russell King68b65f72010-12-22 17:24:39 +0000805
Linus Walleijead76f32011-02-24 13:21:08 +0100806 if (!uap->using_tx_dma)
Russell King68b65f72010-12-22 17:24:39 +0000807 return;
808
809 /* Avoid deadlock with the DMA engine callback */
810 spin_unlock(&uap->port.lock);
811 dmaengine_terminate_all(uap->dmatx.chan);
812 spin_lock(&uap->port.lock);
813 if (uap->dmatx.queued) {
814 dma_unmap_sg(uap->dmatx.chan->device->dev, &uap->dmatx.sg, 1,
815 DMA_TO_DEVICE);
816 uap->dmatx.queued = false;
817 uap->dmacr &= ~UART011_TXDMAE;
Russell King9f25bc52015-11-03 14:51:13 +0000818 pl011_write(uap->dmacr, uap, REG_DMACR);
Russell King68b65f72010-12-22 17:24:39 +0000819 }
820}
821
Linus Walleijead76f32011-02-24 13:21:08 +0100822static void pl011_dma_rx_callback(void *data);
823
824static int pl011_dma_rx_trigger_dma(struct uart_amba_port *uap)
825{
826 struct dma_chan *rxchan = uap->dmarx.chan;
Linus Walleijead76f32011-02-24 13:21:08 +0100827 struct pl011_dmarx_data *dmarx = &uap->dmarx;
828 struct dma_async_tx_descriptor *desc;
829 struct pl011_sgbuf *sgbuf;
830
831 if (!rxchan)
832 return -EIO;
833
834 /* Start the RX DMA job */
835 sgbuf = uap->dmarx.use_buf_b ?
836 &uap->dmarx.sgbuf_b : &uap->dmarx.sgbuf_a;
Alexandre Bounine16052822012-03-08 16:11:18 -0500837 desc = dmaengine_prep_slave_sg(rxchan, &sgbuf->sg, 1,
Vinod Koula485df42011-10-14 10:47:38 +0530838 DMA_DEV_TO_MEM,
Linus Walleijead76f32011-02-24 13:21:08 +0100839 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
840 /*
841 * If the DMA engine is busy and cannot prepare a
842 * channel, no big deal, the driver will fall back
843 * to interrupt mode as a result of this error code.
844 */
845 if (!desc) {
846 uap->dmarx.running = false;
847 dmaengine_terminate_all(rxchan);
848 return -EBUSY;
849 }
850
851 /* Some data to go along to the callback */
852 desc->callback = pl011_dma_rx_callback;
853 desc->callback_param = uap;
854 dmarx->cookie = dmaengine_submit(desc);
855 dma_async_issue_pending(rxchan);
856
857 uap->dmacr |= UART011_RXDMAE;
Russell King9f25bc52015-11-03 14:51:13 +0000858 pl011_write(uap->dmacr, uap, REG_DMACR);
Linus Walleijead76f32011-02-24 13:21:08 +0100859 uap->dmarx.running = true;
860
861 uap->im &= ~UART011_RXIM;
Russell King9f25bc52015-11-03 14:51:13 +0000862 pl011_write(uap->im, uap, REG_IMSC);
Linus Walleijead76f32011-02-24 13:21:08 +0100863
864 return 0;
865}
866
867/*
868 * This is called when either the DMA job is complete, or
869 * the FIFO timeout interrupt occurred. This must be called
870 * with the port spinlock uap->port.lock held.
871 */
872static void pl011_dma_rx_chars(struct uart_amba_port *uap,
873 u32 pending, bool use_buf_b,
874 bool readfifo)
875{
Jiri Slaby05c7cd32013-01-03 15:53:04 +0100876 struct tty_port *port = &uap->port.state->port;
Linus Walleijead76f32011-02-24 13:21:08 +0100877 struct pl011_sgbuf *sgbuf = use_buf_b ?
878 &uap->dmarx.sgbuf_b : &uap->dmarx.sgbuf_a;
Linus Walleijead76f32011-02-24 13:21:08 +0100879 int dma_count = 0;
880 u32 fifotaken = 0; /* only used for vdbg() */
881
Chanho Mincb06ff12013-03-27 18:38:11 +0900882 struct pl011_dmarx_data *dmarx = &uap->dmarx;
883 int dmataken = 0;
884
885 if (uap->dmarx.poll_rate) {
886 /* The data can be taken by polling */
887 dmataken = sgbuf->sg.length - dmarx->last_residue;
888 /* Recalculate the pending size */
889 if (pending >= dmataken)
890 pending -= dmataken;
891 }
892
893 /* Pick the remain data from the DMA */
Linus Walleijead76f32011-02-24 13:21:08 +0100894 if (pending) {
Linus Walleijead76f32011-02-24 13:21:08 +0100895
896 /*
897 * First take all chars in the DMA pipe, then look in the FIFO.
898 * Note that tty_insert_flip_buf() tries to take as many chars
899 * as it can.
900 */
Chanho Mincb06ff12013-03-27 18:38:11 +0900901 dma_count = tty_insert_flip_string(port, sgbuf->buf + dmataken,
902 pending);
Linus Walleijead76f32011-02-24 13:21:08 +0100903
904 uap->port.icount.rx += dma_count;
905 if (dma_count < pending)
906 dev_warn(uap->port.dev,
907 "couldn't insert all characters (TTY is full?)\n");
908 }
909
Chanho Mincb06ff12013-03-27 18:38:11 +0900910 /* Reset the last_residue for Rx DMA poll */
911 if (uap->dmarx.poll_rate)
912 dmarx->last_residue = sgbuf->sg.length;
913
Linus Walleijead76f32011-02-24 13:21:08 +0100914 /*
915 * Only continue with trying to read the FIFO if all DMA chars have
916 * been taken first.
917 */
918 if (dma_count == pending && readfifo) {
919 /* Clear any error flags */
Russell King75836332015-11-03 14:50:58 +0000920 pl011_write(UART011_OEIS | UART011_BEIS | UART011_PEIS |
Russell King9f25bc52015-11-03 14:51:13 +0000921 UART011_FEIS, uap, REG_ICR);
Linus Walleijead76f32011-02-24 13:21:08 +0100922
923 /*
924 * If we read all the DMA'd characters, and we had an
Linus Walleij29772c42011-02-24 13:21:36 +0100925 * incomplete buffer, that could be due to an rx error, or
926 * maybe we just timed out. Read any pending chars and check
927 * the error status.
928 *
929 * Error conditions will only occur in the FIFO, these will
930 * trigger an immediate interrupt and stop the DMA job, so we
931 * will always find the error in the FIFO, never in the DMA
932 * buffer.
Linus Walleijead76f32011-02-24 13:21:08 +0100933 */
Linus Walleij29772c42011-02-24 13:21:36 +0100934 fifotaken = pl011_fifo_to_tty(uap);
Linus Walleijead76f32011-02-24 13:21:08 +0100935 }
936
937 spin_unlock(&uap->port.lock);
938 dev_vdbg(uap->port.dev,
939 "Took %d chars from DMA buffer and %d chars from the FIFO\n",
940 dma_count, fifotaken);
Jiri Slaby2e124b42013-01-03 15:53:06 +0100941 tty_flip_buffer_push(port);
Linus Walleijead76f32011-02-24 13:21:08 +0100942 spin_lock(&uap->port.lock);
943}
944
945static void pl011_dma_rx_irq(struct uart_amba_port *uap)
946{
947 struct pl011_dmarx_data *dmarx = &uap->dmarx;
948 struct dma_chan *rxchan = dmarx->chan;
949 struct pl011_sgbuf *sgbuf = dmarx->use_buf_b ?
950 &dmarx->sgbuf_b : &dmarx->sgbuf_a;
951 size_t pending;
952 struct dma_tx_state state;
953 enum dma_status dmastat;
954
955 /*
956 * Pause the transfer so we can trust the current counter,
957 * do this before we pause the PL011 block, else we may
958 * overflow the FIFO.
959 */
960 if (dmaengine_pause(rxchan))
961 dev_err(uap->port.dev, "unable to pause DMA transfer\n");
962 dmastat = rxchan->device->device_tx_status(rxchan,
963 dmarx->cookie, &state);
964 if (dmastat != DMA_PAUSED)
965 dev_err(uap->port.dev, "unable to pause DMA transfer\n");
966
967 /* Disable RX DMA - incoming data will wait in the FIFO */
968 uap->dmacr &= ~UART011_RXDMAE;
Russell King9f25bc52015-11-03 14:51:13 +0000969 pl011_write(uap->dmacr, uap, REG_DMACR);
Linus Walleijead76f32011-02-24 13:21:08 +0100970 uap->dmarx.running = false;
971
972 pending = sgbuf->sg.length - state.residue;
973 BUG_ON(pending > PL011_DMA_BUFFER_SIZE);
974 /* Then we terminate the transfer - we now know our residue */
975 dmaengine_terminate_all(rxchan);
976
977 /*
978 * This will take the chars we have so far and insert
979 * into the framework.
980 */
981 pl011_dma_rx_chars(uap, pending, dmarx->use_buf_b, true);
982
983 /* Switch buffer & re-trigger DMA job */
984 dmarx->use_buf_b = !dmarx->use_buf_b;
985 if (pl011_dma_rx_trigger_dma(uap)) {
986 dev_dbg(uap->port.dev, "could not retrigger RX DMA job "
987 "fall back to interrupt mode\n");
988 uap->im |= UART011_RXIM;
Russell King9f25bc52015-11-03 14:51:13 +0000989 pl011_write(uap->im, uap, REG_IMSC);
Linus Walleijead76f32011-02-24 13:21:08 +0100990 }
991}
992
993static void pl011_dma_rx_callback(void *data)
994{
995 struct uart_amba_port *uap = data;
996 struct pl011_dmarx_data *dmarx = &uap->dmarx;
Chanho Min6dc01aa2012-02-20 10:24:40 +0900997 struct dma_chan *rxchan = dmarx->chan;
Linus Walleijead76f32011-02-24 13:21:08 +0100998 bool lastbuf = dmarx->use_buf_b;
Chanho Min6dc01aa2012-02-20 10:24:40 +0900999 struct pl011_sgbuf *sgbuf = dmarx->use_buf_b ?
1000 &dmarx->sgbuf_b : &dmarx->sgbuf_a;
1001 size_t pending;
1002 struct dma_tx_state state;
Linus Walleijead76f32011-02-24 13:21:08 +01001003 int ret;
1004
1005 /*
1006 * This completion interrupt occurs typically when the
1007 * RX buffer is totally stuffed but no timeout has yet
1008 * occurred. When that happens, we just want the RX
1009 * routine to flush out the secondary DMA buffer while
1010 * we immediately trigger the next DMA job.
1011 */
1012 spin_lock_irq(&uap->port.lock);
Chanho Min6dc01aa2012-02-20 10:24:40 +09001013 /*
1014 * Rx data can be taken by the UART interrupts during
1015 * the DMA irq handler. So we check the residue here.
1016 */
1017 rxchan->device->device_tx_status(rxchan, dmarx->cookie, &state);
1018 pending = sgbuf->sg.length - state.residue;
1019 BUG_ON(pending > PL011_DMA_BUFFER_SIZE);
1020 /* Then we terminate the transfer - we now know our residue */
1021 dmaengine_terminate_all(rxchan);
1022
Linus Walleijead76f32011-02-24 13:21:08 +01001023 uap->dmarx.running = false;
1024 dmarx->use_buf_b = !lastbuf;
1025 ret = pl011_dma_rx_trigger_dma(uap);
1026
Chanho Min6dc01aa2012-02-20 10:24:40 +09001027 pl011_dma_rx_chars(uap, pending, lastbuf, false);
Linus Walleijead76f32011-02-24 13:21:08 +01001028 spin_unlock_irq(&uap->port.lock);
1029 /*
1030 * Do this check after we picked the DMA chars so we don't
1031 * get some IRQ immediately from RX.
1032 */
1033 if (ret) {
1034 dev_dbg(uap->port.dev, "could not retrigger RX DMA job "
1035 "fall back to interrupt mode\n");
1036 uap->im |= UART011_RXIM;
Russell King9f25bc52015-11-03 14:51:13 +00001037 pl011_write(uap->im, uap, REG_IMSC);
Linus Walleijead76f32011-02-24 13:21:08 +01001038 }
1039}
1040
1041/*
1042 * Stop accepting received characters, when we're shutting down or
1043 * suspending this port.
1044 * Locking: called with port lock held and IRQs disabled.
1045 */
1046static inline void pl011_dma_rx_stop(struct uart_amba_port *uap)
1047{
1048 /* FIXME. Just disable the DMA enable */
1049 uap->dmacr &= ~UART011_RXDMAE;
Russell King9f25bc52015-11-03 14:51:13 +00001050 pl011_write(uap->dmacr, uap, REG_DMACR);
Linus Walleijead76f32011-02-24 13:21:08 +01001051}
Russell King68b65f72010-12-22 17:24:39 +00001052
Chanho Mincb06ff12013-03-27 18:38:11 +09001053/*
1054 * Timer handler for Rx DMA polling.
1055 * Every polling, It checks the residue in the dma buffer and transfer
1056 * data to the tty. Also, last_residue is updated for the next polling.
1057 */
1058static void pl011_dma_rx_poll(unsigned long args)
1059{
1060 struct uart_amba_port *uap = (struct uart_amba_port *)args;
1061 struct tty_port *port = &uap->port.state->port;
1062 struct pl011_dmarx_data *dmarx = &uap->dmarx;
1063 struct dma_chan *rxchan = uap->dmarx.chan;
1064 unsigned long flags = 0;
1065 unsigned int dmataken = 0;
1066 unsigned int size = 0;
1067 struct pl011_sgbuf *sgbuf;
1068 int dma_count;
1069 struct dma_tx_state state;
1070
1071 sgbuf = dmarx->use_buf_b ? &uap->dmarx.sgbuf_b : &uap->dmarx.sgbuf_a;
1072 rxchan->device->device_tx_status(rxchan, dmarx->cookie, &state);
1073 if (likely(state.residue < dmarx->last_residue)) {
1074 dmataken = sgbuf->sg.length - dmarx->last_residue;
1075 size = dmarx->last_residue - state.residue;
1076 dma_count = tty_insert_flip_string(port, sgbuf->buf + dmataken,
1077 size);
1078 if (dma_count == size)
1079 dmarx->last_residue = state.residue;
1080 dmarx->last_jiffies = jiffies;
1081 }
1082 tty_flip_buffer_push(port);
1083
1084 /*
1085 * If no data is received in poll_timeout, the driver will fall back
1086 * to interrupt mode. We will retrigger DMA at the first interrupt.
1087 */
1088 if (jiffies_to_msecs(jiffies - dmarx->last_jiffies)
1089 > uap->dmarx.poll_timeout) {
1090
1091 spin_lock_irqsave(&uap->port.lock, flags);
1092 pl011_dma_rx_stop(uap);
Guennadi Liakhovetskic25a1ad2013-12-10 14:54:47 +01001093 uap->im |= UART011_RXIM;
Russell King9f25bc52015-11-03 14:51:13 +00001094 pl011_write(uap->im, uap, REG_IMSC);
Chanho Mincb06ff12013-03-27 18:38:11 +09001095 spin_unlock_irqrestore(&uap->port.lock, flags);
1096
1097 uap->dmarx.running = false;
1098 dmaengine_terminate_all(rxchan);
1099 del_timer(&uap->dmarx.timer);
1100 } else {
1101 mod_timer(&uap->dmarx.timer,
1102 jiffies + msecs_to_jiffies(uap->dmarx.poll_rate));
1103 }
1104}
1105
Russell King68b65f72010-12-22 17:24:39 +00001106static void pl011_dma_startup(struct uart_amba_port *uap)
1107{
Linus Walleijead76f32011-02-24 13:21:08 +01001108 int ret;
1109
Jorge Ramirez-Ortiz1c9be312015-03-06 13:05:40 -05001110 if (!uap->dma_probed)
1111 pl011_dma_probe(uap);
1112
Russell King68b65f72010-12-22 17:24:39 +00001113 if (!uap->dmatx.chan)
1114 return;
1115
Andrew Jackson4c0be452014-11-07 14:14:35 +00001116 uap->dmatx.buf = kmalloc(PL011_DMA_BUFFER_SIZE, GFP_KERNEL | __GFP_DMA);
Russell King68b65f72010-12-22 17:24:39 +00001117 if (!uap->dmatx.buf) {
1118 dev_err(uap->port.dev, "no memory for DMA TX buffer\n");
1119 uap->port.fifosize = uap->fifosize;
1120 return;
1121 }
1122
1123 sg_init_one(&uap->dmatx.sg, uap->dmatx.buf, PL011_DMA_BUFFER_SIZE);
1124
1125 /* The DMA buffer is now the FIFO the TTY subsystem can use */
1126 uap->port.fifosize = PL011_DMA_BUFFER_SIZE;
Linus Walleijead76f32011-02-24 13:21:08 +01001127 uap->using_tx_dma = true;
Russell King68b65f72010-12-22 17:24:39 +00001128
Linus Walleijead76f32011-02-24 13:21:08 +01001129 if (!uap->dmarx.chan)
1130 goto skip_rx;
1131
1132 /* Allocate and map DMA RX buffers */
1133 ret = pl011_sgbuf_init(uap->dmarx.chan, &uap->dmarx.sgbuf_a,
1134 DMA_FROM_DEVICE);
1135 if (ret) {
1136 dev_err(uap->port.dev, "failed to init DMA %s: %d\n",
1137 "RX buffer A", ret);
1138 goto skip_rx;
1139 }
1140
1141 ret = pl011_sgbuf_init(uap->dmarx.chan, &uap->dmarx.sgbuf_b,
1142 DMA_FROM_DEVICE);
1143 if (ret) {
1144 dev_err(uap->port.dev, "failed to init DMA %s: %d\n",
1145 "RX buffer B", ret);
1146 pl011_sgbuf_free(uap->dmarx.chan, &uap->dmarx.sgbuf_a,
1147 DMA_FROM_DEVICE);
1148 goto skip_rx;
1149 }
1150
1151 uap->using_rx_dma = true;
1152
1153skip_rx:
Russell King68b65f72010-12-22 17:24:39 +00001154 /* Turn on DMA error (RX/TX will be enabled on demand) */
1155 uap->dmacr |= UART011_DMAONERR;
Russell King9f25bc52015-11-03 14:51:13 +00001156 pl011_write(uap->dmacr, uap, REG_DMACR);
Russell King38d62432010-12-22 17:59:16 +00001157
1158 /*
1159 * ST Micro variants has some specific dma burst threshold
1160 * compensation. Set this to 16 bytes, so burst will only
1161 * be issued above/below 16 bytes.
1162 */
1163 if (uap->vendor->dma_threshold)
Russell King75836332015-11-03 14:50:58 +00001164 pl011_write(ST_UART011_DMAWM_RX_16 | ST_UART011_DMAWM_TX_16,
Russell King9f25bc52015-11-03 14:51:13 +00001165 uap, REG_ST_DMAWM);
Linus Walleijead76f32011-02-24 13:21:08 +01001166
1167 if (uap->using_rx_dma) {
1168 if (pl011_dma_rx_trigger_dma(uap))
1169 dev_dbg(uap->port.dev, "could not trigger initial "
1170 "RX DMA job, fall back to interrupt mode\n");
Chanho Mincb06ff12013-03-27 18:38:11 +09001171 if (uap->dmarx.poll_rate) {
1172 init_timer(&(uap->dmarx.timer));
1173 uap->dmarx.timer.function = pl011_dma_rx_poll;
1174 uap->dmarx.timer.data = (unsigned long)uap;
1175 mod_timer(&uap->dmarx.timer,
1176 jiffies +
1177 msecs_to_jiffies(uap->dmarx.poll_rate));
1178 uap->dmarx.last_residue = PL011_DMA_BUFFER_SIZE;
1179 uap->dmarx.last_jiffies = jiffies;
1180 }
Linus Walleijead76f32011-02-24 13:21:08 +01001181 }
Russell King68b65f72010-12-22 17:24:39 +00001182}
1183
1184static void pl011_dma_shutdown(struct uart_amba_port *uap)
1185{
Linus Walleijead76f32011-02-24 13:21:08 +01001186 if (!(uap->using_tx_dma || uap->using_rx_dma))
Russell King68b65f72010-12-22 17:24:39 +00001187 return;
1188
1189 /* Disable RX and TX DMA */
Shawn Guo0e125a52016-07-08 17:00:39 +08001190 while (pl011_read(uap, REG_FR) & uap->vendor->fr_busy)
Timur Tabi2f2fd082016-01-15 14:32:20 -06001191 cpu_relax();
Russell King68b65f72010-12-22 17:24:39 +00001192
1193 spin_lock_irq(&uap->port.lock);
1194 uap->dmacr &= ~(UART011_DMAONERR | UART011_RXDMAE | UART011_TXDMAE);
Russell King9f25bc52015-11-03 14:51:13 +00001195 pl011_write(uap->dmacr, uap, REG_DMACR);
Russell King68b65f72010-12-22 17:24:39 +00001196 spin_unlock_irq(&uap->port.lock);
1197
Linus Walleijead76f32011-02-24 13:21:08 +01001198 if (uap->using_tx_dma) {
1199 /* In theory, this should already be done by pl011_dma_flush_buffer */
1200 dmaengine_terminate_all(uap->dmatx.chan);
1201 if (uap->dmatx.queued) {
1202 dma_unmap_sg(uap->dmatx.chan->device->dev, &uap->dmatx.sg, 1,
1203 DMA_TO_DEVICE);
1204 uap->dmatx.queued = false;
1205 }
1206
1207 kfree(uap->dmatx.buf);
1208 uap->using_tx_dma = false;
Russell King68b65f72010-12-22 17:24:39 +00001209 }
1210
Linus Walleijead76f32011-02-24 13:21:08 +01001211 if (uap->using_rx_dma) {
1212 dmaengine_terminate_all(uap->dmarx.chan);
1213 /* Clean up the RX DMA */
1214 pl011_sgbuf_free(uap->dmarx.chan, &uap->dmarx.sgbuf_a, DMA_FROM_DEVICE);
1215 pl011_sgbuf_free(uap->dmarx.chan, &uap->dmarx.sgbuf_b, DMA_FROM_DEVICE);
Chanho Mincb06ff12013-03-27 18:38:11 +09001216 if (uap->dmarx.poll_rate)
1217 del_timer_sync(&uap->dmarx.timer);
Linus Walleijead76f32011-02-24 13:21:08 +01001218 uap->using_rx_dma = false;
1219 }
Russell King68b65f72010-12-22 17:24:39 +00001220}
1221
Linus Walleijead76f32011-02-24 13:21:08 +01001222static inline bool pl011_dma_rx_available(struct uart_amba_port *uap)
1223{
1224 return uap->using_rx_dma;
1225}
1226
1227static inline bool pl011_dma_rx_running(struct uart_amba_port *uap)
1228{
1229 return uap->using_rx_dma && uap->dmarx.running;
1230}
1231
Russell King68b65f72010-12-22 17:24:39 +00001232#else
1233/* Blank functions if the DMA engine is not available */
Jorge Ramirez-Ortiz1c9be312015-03-06 13:05:40 -05001234static inline void pl011_dma_probe(struct uart_amba_port *uap)
Russell King68b65f72010-12-22 17:24:39 +00001235{
1236}
1237
1238static inline void pl011_dma_remove(struct uart_amba_port *uap)
1239{
1240}
1241
1242static inline void pl011_dma_startup(struct uart_amba_port *uap)
1243{
1244}
1245
1246static inline void pl011_dma_shutdown(struct uart_amba_port *uap)
1247{
1248}
1249
1250static inline bool pl011_dma_tx_irq(struct uart_amba_port *uap)
1251{
1252 return false;
1253}
1254
1255static inline void pl011_dma_tx_stop(struct uart_amba_port *uap)
1256{
1257}
1258
1259static inline bool pl011_dma_tx_start(struct uart_amba_port *uap)
1260{
1261 return false;
1262}
1263
Linus Walleijead76f32011-02-24 13:21:08 +01001264static inline void pl011_dma_rx_irq(struct uart_amba_port *uap)
1265{
1266}
1267
1268static inline void pl011_dma_rx_stop(struct uart_amba_port *uap)
1269{
1270}
1271
1272static inline int pl011_dma_rx_trigger_dma(struct uart_amba_port *uap)
1273{
1274 return -EIO;
1275}
1276
1277static inline bool pl011_dma_rx_available(struct uart_amba_port *uap)
1278{
1279 return false;
1280}
1281
1282static inline bool pl011_dma_rx_running(struct uart_amba_port *uap)
1283{
1284 return false;
1285}
1286
Russell King68b65f72010-12-22 17:24:39 +00001287#define pl011_dma_flush_buffer NULL
1288#endif
1289
Russell Kingb129a8c2005-08-31 10:12:14 +01001290static void pl011_stop_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001292 struct uart_amba_port *uap =
1293 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294
1295 uap->im &= ~UART011_TXIM;
Russell King9f25bc52015-11-03 14:51:13 +00001296 pl011_write(uap->im, uap, REG_IMSC);
Russell King68b65f72010-12-22 17:24:39 +00001297 pl011_dma_tx_stop(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298}
1299
Dave Martin1e84d222015-04-27 16:49:05 +01001300static void pl011_tx_chars(struct uart_amba_port *uap, bool from_irq);
Dave Martin734745c2015-03-04 12:27:33 +00001301
1302/* Start TX with programmed I/O only (no DMA) */
1303static void pl011_start_tx_pio(struct uart_amba_port *uap)
1304{
1305 uap->im |= UART011_TXIM;
Russell King9f25bc52015-11-03 14:51:13 +00001306 pl011_write(uap->im, uap, REG_IMSC);
Dave Martin1e84d222015-04-27 16:49:05 +01001307 pl011_tx_chars(uap, false);
Dave Martin734745c2015-03-04 12:27:33 +00001308}
1309
Russell Kingb129a8c2005-08-31 10:12:14 +01001310static void pl011_start_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001312 struct uart_amba_port *uap =
1313 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314
Dave Martin734745c2015-03-04 12:27:33 +00001315 if (!pl011_dma_tx_start(uap))
1316 pl011_start_tx_pio(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317}
1318
1319static void pl011_stop_rx(struct uart_port *port)
1320{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001321 struct uart_amba_port *uap =
1322 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323
1324 uap->im &= ~(UART011_RXIM|UART011_RTIM|UART011_FEIM|
1325 UART011_PEIM|UART011_BEIM|UART011_OEIM);
Russell King9f25bc52015-11-03 14:51:13 +00001326 pl011_write(uap->im, uap, REG_IMSC);
Linus Walleijead76f32011-02-24 13:21:08 +01001327
1328 pl011_dma_rx_stop(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329}
1330
1331static void pl011_enable_ms(struct uart_port *port)
1332{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001333 struct uart_amba_port *uap =
1334 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335
1336 uap->im |= UART011_RIMIM|UART011_CTSMIM|UART011_DCDMIM|UART011_DSRMIM;
Russell King9f25bc52015-11-03 14:51:13 +00001337 pl011_write(uap->im, uap, REG_IMSC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338}
1339
David Howells7d12e782006-10-05 14:55:46 +01001340static void pl011_rx_chars(struct uart_amba_port *uap)
Fabio Estevamb83286b2013-08-09 17:58:51 -03001341__releases(&uap->port.lock)
1342__acquires(&uap->port.lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343{
Linus Walleij29772c42011-02-24 13:21:36 +01001344 pl011_fifo_to_tty(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345
Thomas Gleixner2389b272007-05-29 21:53:50 +01001346 spin_unlock(&uap->port.lock);
Jiri Slaby2e124b42013-01-03 15:53:06 +01001347 tty_flip_buffer_push(&uap->port.state->port);
Linus Walleijead76f32011-02-24 13:21:08 +01001348 /*
1349 * If we were temporarily out of DMA mode for a while,
1350 * attempt to switch back to DMA mode again.
1351 */
1352 if (pl011_dma_rx_available(uap)) {
1353 if (pl011_dma_rx_trigger_dma(uap)) {
1354 dev_dbg(uap->port.dev, "could not trigger RX DMA job "
1355 "fall back to interrupt mode again\n");
1356 uap->im |= UART011_RXIM;
Russell King9f25bc52015-11-03 14:51:13 +00001357 pl011_write(uap->im, uap, REG_IMSC);
Chanho Mincb06ff12013-03-27 18:38:11 +09001358 } else {
Chanho Min89fa28d2013-04-03 11:10:37 +09001359#ifdef CONFIG_DMA_ENGINE
Chanho Mincb06ff12013-03-27 18:38:11 +09001360 /* Start Rx DMA poll */
1361 if (uap->dmarx.poll_rate) {
1362 uap->dmarx.last_jiffies = jiffies;
1363 uap->dmarx.last_residue = PL011_DMA_BUFFER_SIZE;
1364 mod_timer(&uap->dmarx.timer,
1365 jiffies +
1366 msecs_to_jiffies(uap->dmarx.poll_rate));
1367 }
Chanho Min89fa28d2013-04-03 11:10:37 +09001368#endif
Chanho Mincb06ff12013-03-27 18:38:11 +09001369 }
Linus Walleijead76f32011-02-24 13:21:08 +01001370 }
Thomas Gleixner2389b272007-05-29 21:53:50 +01001371 spin_lock(&uap->port.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372}
1373
Dave Martin1e84d222015-04-27 16:49:05 +01001374static bool pl011_tx_char(struct uart_amba_port *uap, unsigned char c,
1375 bool from_irq)
Dave Martin734745c2015-03-04 12:27:33 +00001376{
Dave Martin1e84d222015-04-27 16:49:05 +01001377 if (unlikely(!from_irq) &&
Russell King9f25bc52015-11-03 14:51:13 +00001378 pl011_read(uap, REG_FR) & UART01x_FR_TXFF)
Dave Martin1e84d222015-04-27 16:49:05 +01001379 return false; /* unable to transmit character */
1380
Russell King9f25bc52015-11-03 14:51:13 +00001381 pl011_write(c, uap, REG_DR);
Dave Martin734745c2015-03-04 12:27:33 +00001382 uap->port.icount.tx++;
1383
Dave Martin1e84d222015-04-27 16:49:05 +01001384 return true;
Dave Martin734745c2015-03-04 12:27:33 +00001385}
1386
Dave Martin1e84d222015-04-27 16:49:05 +01001387static void pl011_tx_chars(struct uart_amba_port *uap, bool from_irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388{
Alan Coxebd2c8f2009-09-19 13:13:28 -07001389 struct circ_buf *xmit = &uap->port.state->xmit;
Dave Martin1e84d222015-04-27 16:49:05 +01001390 int count = uap->fifosize >> 1;
Dave Martin734745c2015-03-04 12:27:33 +00001391
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 if (uap->port.x_char) {
Dave Martin1e84d222015-04-27 16:49:05 +01001393 if (!pl011_tx_char(uap, uap->port.x_char, from_irq))
1394 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 uap->port.x_char = 0;
Dave Martin734745c2015-03-04 12:27:33 +00001396 --count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 }
1398 if (uart_circ_empty(xmit) || uart_tx_stopped(&uap->port)) {
Russell Kingb129a8c2005-08-31 10:12:14 +01001399 pl011_stop_tx(&uap->port);
Dave Martin1e84d222015-04-27 16:49:05 +01001400 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 }
1402
Russell King68b65f72010-12-22 17:24:39 +00001403 /* If we are using DMA mode, try to send some characters. */
1404 if (pl011_dma_tx_irq(uap))
Dave Martin1e84d222015-04-27 16:49:05 +01001405 return;
Russell King68b65f72010-12-22 17:24:39 +00001406
Dave Martin1e84d222015-04-27 16:49:05 +01001407 do {
1408 if (likely(from_irq) && count-- == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 break;
Dave Martin1e84d222015-04-27 16:49:05 +01001410
1411 if (!pl011_tx_char(uap, xmit->buf[xmit->tail], from_irq))
1412 break;
1413
1414 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
1415 } while (!uart_circ_empty(xmit));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416
1417 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
1418 uart_write_wakeup(&uap->port);
1419
Dave Martin1e84d222015-04-27 16:49:05 +01001420 if (uart_circ_empty(xmit))
Russell Kingb129a8c2005-08-31 10:12:14 +01001421 pl011_stop_tx(&uap->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422}
1423
1424static void pl011_modem_status(struct uart_amba_port *uap)
1425{
1426 unsigned int status, delta;
1427
Russell King9f25bc52015-11-03 14:51:13 +00001428 status = pl011_read(uap, REG_FR) & UART01x_FR_MODEM_ANY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429
1430 delta = status ^ uap->old_status;
1431 uap->old_status = status;
1432
1433 if (!delta)
1434 return;
1435
1436 if (delta & UART01x_FR_DCD)
1437 uart_handle_dcd_change(&uap->port, status & UART01x_FR_DCD);
1438
Shawn Guo0e125a52016-07-08 17:00:39 +08001439 if (delta & uap->vendor->fr_dsr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 uap->port.icount.dsr++;
1441
Shawn Guo0e125a52016-07-08 17:00:39 +08001442 if (delta & uap->vendor->fr_cts)
1443 uart_handle_cts_change(&uap->port,
1444 status & uap->vendor->fr_cts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445
Alan Coxbdc04e32009-09-19 13:13:31 -07001446 wake_up_interruptible(&uap->port.state->port.delta_msr_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447}
1448
Andre Przywara9c4ef4b2015-05-21 17:26:20 +01001449static void check_apply_cts_event_workaround(struct uart_amba_port *uap)
1450{
1451 unsigned int dummy_read;
1452
1453 if (!uap->vendor->cts_event_workaround)
1454 return;
1455
1456 /* workaround to make sure that all bits are unlocked.. */
Russell King9f25bc52015-11-03 14:51:13 +00001457 pl011_write(0x00, uap, REG_ICR);
Andre Przywara9c4ef4b2015-05-21 17:26:20 +01001458
1459 /*
1460 * WA: introduce 26ns(1 uart clk) delay before W1C;
1461 * single apb access will incur 2 pclk(133.12Mhz) delay,
1462 * so add 2 dummy reads
1463 */
Russell King9f25bc52015-11-03 14:51:13 +00001464 dummy_read = pl011_read(uap, REG_ICR);
1465 dummy_read = pl011_read(uap, REG_ICR);
Andre Przywara9c4ef4b2015-05-21 17:26:20 +01001466}
1467
David Howells7d12e782006-10-05 14:55:46 +01001468static irqreturn_t pl011_int(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469{
1470 struct uart_amba_port *uap = dev_id;
Russell King963cc982010-12-22 17:16:09 +00001471 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 unsigned int status, pass_counter = AMBA_ISR_PASS_LIMIT;
Andre Przywara075167e2015-05-21 17:26:19 +01001473 u16 imsc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 int handled = 0;
1475
Russell King963cc982010-12-22 17:16:09 +00001476 spin_lock_irqsave(&uap->port.lock, flags);
Russell King9f25bc52015-11-03 14:51:13 +00001477 imsc = pl011_read(uap, REG_IMSC);
1478 status = pl011_read(uap, REG_RIS) & imsc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 if (status) {
1480 do {
Andre Przywara9c4ef4b2015-05-21 17:26:20 +01001481 check_apply_cts_event_workaround(uap);
Greg Kroah-Hartmanf11c9842015-09-04 09:13:39 -07001482
Russell King75836332015-11-03 14:50:58 +00001483 pl011_write(status & ~(UART011_TXIS|UART011_RTIS|
1484 UART011_RXIS),
Russell King9f25bc52015-11-03 14:51:13 +00001485 uap, REG_ICR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486
Linus Walleijead76f32011-02-24 13:21:08 +01001487 if (status & (UART011_RTIS|UART011_RXIS)) {
1488 if (pl011_dma_rx_running(uap))
1489 pl011_dma_rx_irq(uap);
1490 else
1491 pl011_rx_chars(uap);
1492 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 if (status & (UART011_DSRMIS|UART011_DCDMIS|
1494 UART011_CTSMIS|UART011_RIMIS))
1495 pl011_modem_status(uap);
Dave Martin1e84d222015-04-27 16:49:05 +01001496 if (status & UART011_TXIS)
1497 pl011_tx_chars(uap, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498
Rajanikanth H.V4fd06902012-03-26 11:17:02 +02001499 if (pass_counter-- == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 break;
1501
Russell King9f25bc52015-11-03 14:51:13 +00001502 status = pl011_read(uap, REG_RIS) & imsc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 } while (status != 0);
1504 handled = 1;
1505 }
1506
Russell King963cc982010-12-22 17:16:09 +00001507 spin_unlock_irqrestore(&uap->port.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508
1509 return IRQ_RETVAL(handled);
1510}
1511
Linus Walleije643f872012-06-17 15:44:19 +02001512static unsigned int pl011_tx_empty(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001514 struct uart_amba_port *uap =
1515 container_of(port, struct uart_amba_port, port);
Russell King9f25bc52015-11-03 14:51:13 +00001516 unsigned int status = pl011_read(uap, REG_FR);
Shawn Guo0e125a52016-07-08 17:00:39 +08001517 return status & (uap->vendor->fr_busy | UART01x_FR_TXFF) ?
1518 0 : TIOCSER_TEMT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519}
1520
Linus Walleije643f872012-06-17 15:44:19 +02001521static unsigned int pl011_get_mctrl(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001523 struct uart_amba_port *uap =
1524 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 unsigned int result = 0;
Russell King9f25bc52015-11-03 14:51:13 +00001526 unsigned int status = pl011_read(uap, REG_FR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527
Jiri Slaby5159f402007-10-18 23:40:31 -07001528#define TIOCMBIT(uartbit, tiocmbit) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 if (status & uartbit) \
1530 result |= tiocmbit
1531
Jiri Slaby5159f402007-10-18 23:40:31 -07001532 TIOCMBIT(UART01x_FR_DCD, TIOCM_CAR);
Shawn Guo0e125a52016-07-08 17:00:39 +08001533 TIOCMBIT(uap->vendor->fr_dsr, TIOCM_DSR);
1534 TIOCMBIT(uap->vendor->fr_cts, TIOCM_CTS);
1535 TIOCMBIT(uap->vendor->fr_ri, TIOCM_RNG);
Jiri Slaby5159f402007-10-18 23:40:31 -07001536#undef TIOCMBIT
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 return result;
1538}
1539
1540static void pl011_set_mctrl(struct uart_port *port, unsigned int mctrl)
1541{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001542 struct uart_amba_port *uap =
1543 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 unsigned int cr;
1545
Russell King9f25bc52015-11-03 14:51:13 +00001546 cr = pl011_read(uap, REG_CR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547
Jiri Slaby5159f402007-10-18 23:40:31 -07001548#define TIOCMBIT(tiocmbit, uartbit) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 if (mctrl & tiocmbit) \
1550 cr |= uartbit; \
1551 else \
1552 cr &= ~uartbit
1553
Jiri Slaby5159f402007-10-18 23:40:31 -07001554 TIOCMBIT(TIOCM_RTS, UART011_CR_RTS);
1555 TIOCMBIT(TIOCM_DTR, UART011_CR_DTR);
1556 TIOCMBIT(TIOCM_OUT1, UART011_CR_OUT1);
1557 TIOCMBIT(TIOCM_OUT2, UART011_CR_OUT2);
1558 TIOCMBIT(TIOCM_LOOP, UART011_CR_LBE);
Rabin Vincent3b438162010-02-12 06:43:11 +01001559
1560 if (uap->autorts) {
1561 /* We need to disable auto-RTS if we want to turn RTS off */
1562 TIOCMBIT(TIOCM_RTS, UART011_CR_RTSEN);
1563 }
Jiri Slaby5159f402007-10-18 23:40:31 -07001564#undef TIOCMBIT
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565
Russell King9f25bc52015-11-03 14:51:13 +00001566 pl011_write(cr, uap, REG_CR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567}
1568
1569static void pl011_break_ctl(struct uart_port *port, int break_state)
1570{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001571 struct uart_amba_port *uap =
1572 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 unsigned long flags;
1574 unsigned int lcr_h;
1575
1576 spin_lock_irqsave(&uap->port.lock, flags);
Russell Kinge4df9a82015-11-16 17:40:41 +00001577 lcr_h = pl011_read(uap, REG_LCRH_TX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 if (break_state == -1)
1579 lcr_h |= UART01x_LCRH_BRK;
1580 else
1581 lcr_h &= ~UART01x_LCRH_BRK;
Russell Kinge4df9a82015-11-16 17:40:41 +00001582 pl011_write(lcr_h, uap, REG_LCRH_TX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 spin_unlock_irqrestore(&uap->port.lock, flags);
1584}
1585
Jason Wessel84b5ae12008-02-20 13:33:39 -06001586#ifdef CONFIG_CONSOLE_POLL
Anton Vorontsov5c8124a2012-09-24 14:27:55 -07001587
1588static void pl011_quiesce_irqs(struct uart_port *port)
1589{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001590 struct uart_amba_port *uap =
1591 container_of(port, struct uart_amba_port, port);
Anton Vorontsov5c8124a2012-09-24 14:27:55 -07001592
Russell King9f25bc52015-11-03 14:51:13 +00001593 pl011_write(pl011_read(uap, REG_MIS), uap, REG_ICR);
Anton Vorontsov5c8124a2012-09-24 14:27:55 -07001594 /*
1595 * There is no way to clear TXIM as this is "ready to transmit IRQ", so
1596 * we simply mask it. start_tx() will unmask it.
1597 *
1598 * Note we can race with start_tx(), and if the race happens, the
1599 * polling user might get another interrupt just after we clear it.
1600 * But it should be OK and can happen even w/o the race, e.g.
1601 * controller immediately got some new data and raised the IRQ.
1602 *
1603 * And whoever uses polling routines assumes that it manages the device
1604 * (including tx queue), so we're also fine with start_tx()'s caller
1605 * side.
1606 */
Russell King9f25bc52015-11-03 14:51:13 +00001607 pl011_write(pl011_read(uap, REG_IMSC) & ~UART011_TXIM, uap,
1608 REG_IMSC);
Anton Vorontsov5c8124a2012-09-24 14:27:55 -07001609}
1610
Linus Walleije643f872012-06-17 15:44:19 +02001611static int pl011_get_poll_char(struct uart_port *port)
Jason Wessel84b5ae12008-02-20 13:33:39 -06001612{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001613 struct uart_amba_port *uap =
1614 container_of(port, struct uart_amba_port, port);
Jason Wessel84b5ae12008-02-20 13:33:39 -06001615 unsigned int status;
1616
Anton Vorontsov5c8124a2012-09-24 14:27:55 -07001617 /*
1618 * The caller might need IRQs lowered, e.g. if used with KDB NMI
1619 * debugger.
1620 */
1621 pl011_quiesce_irqs(port);
1622
Russell King9f25bc52015-11-03 14:51:13 +00001623 status = pl011_read(uap, REG_FR);
Jason Wesself5316b42010-05-20 21:04:22 -05001624 if (status & UART01x_FR_RXFE)
1625 return NO_POLL_CHAR;
Jason Wessel84b5ae12008-02-20 13:33:39 -06001626
Russell King9f25bc52015-11-03 14:51:13 +00001627 return pl011_read(uap, REG_DR);
Jason Wessel84b5ae12008-02-20 13:33:39 -06001628}
1629
Linus Walleije643f872012-06-17 15:44:19 +02001630static void pl011_put_poll_char(struct uart_port *port,
Jason Wessel84b5ae12008-02-20 13:33:39 -06001631 unsigned char ch)
1632{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001633 struct uart_amba_port *uap =
1634 container_of(port, struct uart_amba_port, port);
Jason Wessel84b5ae12008-02-20 13:33:39 -06001635
Russell King9f25bc52015-11-03 14:51:13 +00001636 while (pl011_read(uap, REG_FR) & UART01x_FR_TXFF)
Timur Tabi2f2fd082016-01-15 14:32:20 -06001637 cpu_relax();
Jason Wessel84b5ae12008-02-20 13:33:39 -06001638
Russell King9f25bc52015-11-03 14:51:13 +00001639 pl011_write(ch, uap, REG_DR);
Jason Wessel84b5ae12008-02-20 13:33:39 -06001640}
1641
1642#endif /* CONFIG_CONSOLE_POLL */
1643
Anton Vorontsovb3564c22012-09-24 14:27:54 -07001644static int pl011_hwinit(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001646 struct uart_amba_port *uap =
1647 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 int retval;
1649
Linus Walleij78d80c52012-05-23 21:18:46 +02001650 /* Optionaly enable pins to be muxed in and configured */
Linus Walleij2b996fc2013-06-05 15:36:42 +02001651 pinctrl_pm_select_default_state(port->dev);
Linus Walleij78d80c52012-05-23 21:18:46 +02001652
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 /*
1654 * Try to enable the clock producer.
1655 */
Julia Lawall1c4c4392012-08-26 18:01:01 +02001656 retval = clk_prepare_enable(uap->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 if (retval)
Tushar Behera7f6d9422014-06-26 15:35:35 +05301658 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659
1660 uap->port.uartclk = clk_get_rate(uap->clk);
1661
Linus Walleij9b96fba2012-03-13 13:27:23 +01001662 /* Clear pending error and receive interrupts */
Russell King75836332015-11-03 14:50:58 +00001663 pl011_write(UART011_OEIS | UART011_BEIS | UART011_PEIS |
1664 UART011_FEIS | UART011_RTIS | UART011_RXIS,
Russell King9f25bc52015-11-03 14:51:13 +00001665 uap, REG_ICR);
Linus Walleij9b96fba2012-03-13 13:27:23 +01001666
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 /*
Anton Vorontsovb3564c22012-09-24 14:27:54 -07001668 * Save interrupts enable mask, and enable RX interrupts in case if
1669 * the interrupt is used for NMI entry.
1670 */
Russell King9f25bc52015-11-03 14:51:13 +00001671 uap->im = pl011_read(uap, REG_IMSC);
1672 pl011_write(UART011_RTIM | UART011_RXIM, uap, REG_IMSC);
Anton Vorontsovb3564c22012-09-24 14:27:54 -07001673
Jingoo Han574de552013-07-30 17:06:57 +09001674 if (dev_get_platdata(uap->port.dev)) {
Anton Vorontsovb3564c22012-09-24 14:27:54 -07001675 struct amba_pl011_data *plat;
1676
Jingoo Han574de552013-07-30 17:06:57 +09001677 plat = dev_get_platdata(uap->port.dev);
Anton Vorontsovb3564c22012-09-24 14:27:54 -07001678 if (plat->init)
1679 plat->init();
1680 }
1681 return 0;
Anton Vorontsovb3564c22012-09-24 14:27:54 -07001682}
1683
Russell King7fe9a5a2015-11-03 14:51:08 +00001684static bool pl011_split_lcrh(const struct uart_amba_port *uap)
1685{
Russell Kinge4df9a82015-11-16 17:40:41 +00001686 return pl011_reg_to_offset(uap, REG_LCRH_RX) !=
1687 pl011_reg_to_offset(uap, REG_LCRH_TX);
Russell King7fe9a5a2015-11-03 14:51:08 +00001688}
1689
Jon Medhurstb60f2f62013-12-10 10:18:59 +00001690static void pl011_write_lcr_h(struct uart_amba_port *uap, unsigned int lcr_h)
1691{
Russell Kinge4df9a82015-11-16 17:40:41 +00001692 pl011_write(lcr_h, uap, REG_LCRH_RX);
Russell King7fe9a5a2015-11-03 14:51:08 +00001693 if (pl011_split_lcrh(uap)) {
Jon Medhurstb60f2f62013-12-10 10:18:59 +00001694 int i;
1695 /*
1696 * Wait 10 PCLKs before writing LCRH_TX register,
1697 * to get this delay write read only register 10 times
1698 */
1699 for (i = 0; i < 10; ++i)
Russell King9f25bc52015-11-03 14:51:13 +00001700 pl011_write(0xff, uap, REG_MIS);
Russell Kinge4df9a82015-11-16 17:40:41 +00001701 pl011_write(lcr_h, uap, REG_LCRH_TX);
Jon Medhurstb60f2f62013-12-10 10:18:59 +00001702 }
1703}
1704
Andre Przywara867b8e82015-05-21 17:26:15 +01001705static int pl011_allocate_irq(struct uart_amba_port *uap)
1706{
Russell King9f25bc52015-11-03 14:51:13 +00001707 pl011_write(uap->im, uap, REG_IMSC);
Andre Przywara867b8e82015-05-21 17:26:15 +01001708
1709 return request_irq(uap->port.irq, pl011_int, 0, "uart-pl011", uap);
1710}
1711
1712/*
1713 * Enable interrupts, only timeouts when using DMA
1714 * if initial RX DMA job failed, start in interrupt mode
1715 * as well.
1716 */
1717static void pl011_enable_interrupts(struct uart_amba_port *uap)
1718{
1719 spin_lock_irq(&uap->port.lock);
1720
1721 /* Clear out any spuriously appearing RX interrupts */
Russell King9f25bc52015-11-03 14:51:13 +00001722 pl011_write(UART011_RTIS | UART011_RXIS, uap, REG_ICR);
Andre Przywara867b8e82015-05-21 17:26:15 +01001723 uap->im = UART011_RTIM;
1724 if (!pl011_dma_rx_running(uap))
1725 uap->im |= UART011_RXIM;
Russell King9f25bc52015-11-03 14:51:13 +00001726 pl011_write(uap->im, uap, REG_IMSC);
Andre Przywara867b8e82015-05-21 17:26:15 +01001727 spin_unlock_irq(&uap->port.lock);
1728}
1729
Anton Vorontsovb3564c22012-09-24 14:27:54 -07001730static int pl011_startup(struct uart_port *port)
1731{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001732 struct uart_amba_port *uap =
1733 container_of(port, struct uart_amba_port, port);
Dave Martin734745c2015-03-04 12:27:33 +00001734 unsigned int cr;
Anton Vorontsovb3564c22012-09-24 14:27:54 -07001735 int retval;
1736
1737 retval = pl011_hwinit(port);
1738 if (retval)
1739 goto clk_dis;
1740
Andre Przywara867b8e82015-05-21 17:26:15 +01001741 retval = pl011_allocate_irq(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 if (retval)
1743 goto clk_dis;
1744
Russell King9f25bc52015-11-03 14:51:13 +00001745 pl011_write(uap->vendor->ifls, uap, REG_IFLS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746
Jon Medhurstfe433902013-12-10 10:18:58 +00001747 spin_lock_irq(&uap->port.lock);
1748
Shreshtha Kumar Sahud8d8ffa2012-01-18 15:53:59 +05301749 /* restore RTS and DTR */
1750 cr = uap->old_cr & (UART011_CR_RTS | UART011_CR_DTR);
1751 cr |= UART01x_CR_UARTEN | UART011_CR_RXE | UART011_CR_TXE;
Russell King9f25bc52015-11-03 14:51:13 +00001752 pl011_write(cr, uap, REG_CR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753
Jon Medhurstfe433902013-12-10 10:18:58 +00001754 spin_unlock_irq(&uap->port.lock);
1755
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 /*
1757 * initialise the old status of the modem signals
1758 */
Russell King9f25bc52015-11-03 14:51:13 +00001759 uap->old_status = pl011_read(uap, REG_FR) & UART01x_FR_MODEM_ANY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760
Russell King68b65f72010-12-22 17:24:39 +00001761 /* Startup DMA */
1762 pl011_dma_startup(uap);
1763
Andre Przywara867b8e82015-05-21 17:26:15 +01001764 pl011_enable_interrupts(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765
1766 return 0;
1767
1768 clk_dis:
Julia Lawall1c4c4392012-08-26 18:01:01 +02001769 clk_disable_unprepare(uap->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 return retval;
1771}
1772
Andre Przywara0dd1e242015-05-21 17:26:23 +01001773static int sbsa_uart_startup(struct uart_port *port)
1774{
1775 struct uart_amba_port *uap =
1776 container_of(port, struct uart_amba_port, port);
1777 int retval;
1778
1779 retval = pl011_hwinit(port);
1780 if (retval)
1781 return retval;
1782
1783 retval = pl011_allocate_irq(uap);
1784 if (retval)
1785 return retval;
1786
1787 /* The SBSA UART does not support any modem status lines. */
1788 uap->old_status = 0;
1789
1790 pl011_enable_interrupts(uap);
1791
1792 return 0;
1793}
1794
Linus Walleijec489aa2010-06-02 08:13:52 +01001795static void pl011_shutdown_channel(struct uart_amba_port *uap,
1796 unsigned int lcrh)
1797{
Greg Kroah-Hartmanf11c9842015-09-04 09:13:39 -07001798 unsigned long val;
Linus Walleijec489aa2010-06-02 08:13:52 +01001799
Russell Kingb2a4e242015-11-03 14:51:03 +00001800 val = pl011_read(uap, lcrh);
Greg Kroah-Hartmanf11c9842015-09-04 09:13:39 -07001801 val &= ~(UART01x_LCRH_BRK | UART01x_LCRH_FEN);
Russell Kingb2a4e242015-11-03 14:51:03 +00001802 pl011_write(val, uap, lcrh);
Linus Walleijec489aa2010-06-02 08:13:52 +01001803}
1804
Andre Przywara95166a32015-05-21 17:26:16 +01001805/*
1806 * disable the port. It should not disable RTS and DTR.
1807 * Also RTS and DTR state should be preserved to restore
1808 * it during startup().
1809 */
1810static void pl011_disable_uart(struct uart_amba_port *uap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811{
Shreshtha Kumar Sahud8d8ffa2012-01-18 15:53:59 +05301812 unsigned int cr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813
Rabin Vincent3b438162010-02-12 06:43:11 +01001814 uap->autorts = false;
Jon Medhurstfe433902013-12-10 10:18:58 +00001815 spin_lock_irq(&uap->port.lock);
Russell King9f25bc52015-11-03 14:51:13 +00001816 cr = pl011_read(uap, REG_CR);
Shreshtha Kumar Sahud8d8ffa2012-01-18 15:53:59 +05301817 uap->old_cr = cr;
1818 cr &= UART011_CR_RTS | UART011_CR_DTR;
1819 cr |= UART01x_CR_UARTEN | UART011_CR_TXE;
Russell King9f25bc52015-11-03 14:51:13 +00001820 pl011_write(cr, uap, REG_CR);
Jon Medhurstfe433902013-12-10 10:18:58 +00001821 spin_unlock_irq(&uap->port.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822
1823 /*
1824 * disable break condition and fifos
1825 */
Russell Kinge4df9a82015-11-16 17:40:41 +00001826 pl011_shutdown_channel(uap, REG_LCRH_RX);
Russell King7fe9a5a2015-11-03 14:51:08 +00001827 if (pl011_split_lcrh(uap))
Russell Kinge4df9a82015-11-16 17:40:41 +00001828 pl011_shutdown_channel(uap, REG_LCRH_TX);
Andre Przywara95166a32015-05-21 17:26:16 +01001829}
1830
1831static void pl011_disable_interrupts(struct uart_amba_port *uap)
1832{
1833 spin_lock_irq(&uap->port.lock);
1834
1835 /* mask all interrupts and clear all pending ones */
1836 uap->im = 0;
Russell King9f25bc52015-11-03 14:51:13 +00001837 pl011_write(uap->im, uap, REG_IMSC);
1838 pl011_write(0xffff, uap, REG_ICR);
Andre Przywara95166a32015-05-21 17:26:16 +01001839
1840 spin_unlock_irq(&uap->port.lock);
1841}
1842
1843static void pl011_shutdown(struct uart_port *port)
1844{
1845 struct uart_amba_port *uap =
1846 container_of(port, struct uart_amba_port, port);
1847
1848 pl011_disable_interrupts(uap);
1849
1850 pl011_dma_shutdown(uap);
1851
1852 free_irq(uap->port.irq, uap);
1853
1854 pl011_disable_uart(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855
1856 /*
1857 * Shut down the clock producer
1858 */
Julia Lawall1c4c4392012-08-26 18:01:01 +02001859 clk_disable_unprepare(uap->clk);
Linus Walleij78d80c52012-05-23 21:18:46 +02001860 /* Optionally let pins go into sleep states */
Linus Walleij2b996fc2013-06-05 15:36:42 +02001861 pinctrl_pm_select_sleep_state(port->dev);
Shreshtha Kumar Sahuc16d51a2011-06-13 10:11:33 +02001862
Jingoo Han574de552013-07-30 17:06:57 +09001863 if (dev_get_platdata(uap->port.dev)) {
Shreshtha Kumar Sahuc16d51a2011-06-13 10:11:33 +02001864 struct amba_pl011_data *plat;
1865
Jingoo Han574de552013-07-30 17:06:57 +09001866 plat = dev_get_platdata(uap->port.dev);
Shreshtha Kumar Sahuc16d51a2011-06-13 10:11:33 +02001867 if (plat->exit)
1868 plat->exit();
1869 }
1870
Peter Hurley36f339d2014-11-06 09:06:12 -05001871 if (uap->port.ops->flush_buffer)
1872 uap->port.ops->flush_buffer(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873}
1874
Andre Przywara0dd1e242015-05-21 17:26:23 +01001875static void sbsa_uart_shutdown(struct uart_port *port)
1876{
1877 struct uart_amba_port *uap =
1878 container_of(port, struct uart_amba_port, port);
1879
1880 pl011_disable_interrupts(uap);
1881
1882 free_irq(uap->port.irq, uap);
1883
1884 if (uap->port.ops->flush_buffer)
1885 uap->port.ops->flush_buffer(port);
1886}
1887
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888static void
Andre Przywaraef5a9352015-05-21 17:26:17 +01001889pl011_setup_status_masks(struct uart_port *port, struct ktermios *termios)
1890{
1891 port->read_status_mask = UART011_DR_OE | 255;
1892 if (termios->c_iflag & INPCK)
1893 port->read_status_mask |= UART011_DR_FE | UART011_DR_PE;
1894 if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
1895 port->read_status_mask |= UART011_DR_BE;
1896
1897 /*
1898 * Characters to ignore
1899 */
1900 port->ignore_status_mask = 0;
1901 if (termios->c_iflag & IGNPAR)
1902 port->ignore_status_mask |= UART011_DR_FE | UART011_DR_PE;
1903 if (termios->c_iflag & IGNBRK) {
1904 port->ignore_status_mask |= UART011_DR_BE;
1905 /*
1906 * If we're ignoring parity and break indicators,
1907 * ignore overruns too (for real raw support).
1908 */
1909 if (termios->c_iflag & IGNPAR)
1910 port->ignore_status_mask |= UART011_DR_OE;
1911 }
1912
1913 /*
1914 * Ignore all characters if CREAD is not set.
1915 */
1916 if ((termios->c_cflag & CREAD) == 0)
1917 port->ignore_status_mask |= UART_DUMMY_DR_RX;
1918}
1919
1920static void
Alan Cox606d0992006-12-08 02:38:45 -08001921pl011_set_termios(struct uart_port *port, struct ktermios *termios,
1922 struct ktermios *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923{
Daniel Thompsona5820c22014-09-03 12:51:55 +01001924 struct uart_amba_port *uap =
1925 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 unsigned int lcr_h, old_cr;
1927 unsigned long flags;
Russell Kingc19f12b2010-12-22 17:48:26 +00001928 unsigned int baud, quot, clkdiv;
1929
1930 if (uap->vendor->oversampling)
1931 clkdiv = 8;
1932 else
1933 clkdiv = 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934
1935 /*
1936 * Ask the core to calculate the divisor for us.
1937 */
Linus Walleijac3e3fb2010-06-02 20:40:22 +01001938 baud = uart_get_baud_rate(port, termios, old, 0,
Russell Kingc19f12b2010-12-22 17:48:26 +00001939 port->uartclk / clkdiv);
Chanho Min89fa28d2013-04-03 11:10:37 +09001940#ifdef CONFIG_DMA_ENGINE
Chanho Mincb06ff12013-03-27 18:38:11 +09001941 /*
1942 * Adjust RX DMA polling rate with baud rate if not specified.
1943 */
1944 if (uap->dmarx.auto_poll_rate)
1945 uap->dmarx.poll_rate = DIV_ROUND_UP(10000000, baud);
Chanho Min89fa28d2013-04-03 11:10:37 +09001946#endif
Linus Walleijac3e3fb2010-06-02 20:40:22 +01001947
1948 if (baud > port->uartclk/16)
1949 quot = DIV_ROUND_CLOSEST(port->uartclk * 8, baud);
1950 else
1951 quot = DIV_ROUND_CLOSEST(port->uartclk * 4, baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952
1953 switch (termios->c_cflag & CSIZE) {
1954 case CS5:
1955 lcr_h = UART01x_LCRH_WLEN_5;
1956 break;
1957 case CS6:
1958 lcr_h = UART01x_LCRH_WLEN_6;
1959 break;
1960 case CS7:
1961 lcr_h = UART01x_LCRH_WLEN_7;
1962 break;
1963 default: // CS8
1964 lcr_h = UART01x_LCRH_WLEN_8;
1965 break;
1966 }
1967 if (termios->c_cflag & CSTOPB)
1968 lcr_h |= UART01x_LCRH_STP2;
1969 if (termios->c_cflag & PARENB) {
1970 lcr_h |= UART01x_LCRH_PEN;
1971 if (!(termios->c_cflag & PARODD))
1972 lcr_h |= UART01x_LCRH_EPS;
Ed Spiridonovbb700022016-03-04 08:11:53 +03001973 if (termios->c_cflag & CMSPAR)
1974 lcr_h |= UART011_LCRH_SPS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 }
Russell Kingffca2b12010-12-22 17:13:05 +00001976 if (uap->fifosize > 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 lcr_h |= UART01x_LCRH_FEN;
1978
1979 spin_lock_irqsave(&port->lock, flags);
1980
1981 /*
1982 * Update the per-port timeout.
1983 */
1984 uart_update_timeout(port, termios->c_cflag, baud);
1985
Andre Przywaraef5a9352015-05-21 17:26:17 +01001986 pl011_setup_status_masks(port, termios);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987
1988 if (UART_ENABLE_MS(port, termios->c_cflag))
1989 pl011_enable_ms(port);
1990
1991 /* first, disable everything */
Russell King9f25bc52015-11-03 14:51:13 +00001992 old_cr = pl011_read(uap, REG_CR);
1993 pl011_write(0, uap, REG_CR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994
Rabin Vincent3b438162010-02-12 06:43:11 +01001995 if (termios->c_cflag & CRTSCTS) {
1996 if (old_cr & UART011_CR_RTS)
1997 old_cr |= UART011_CR_RTSEN;
1998
1999 old_cr |= UART011_CR_CTSEN;
2000 uap->autorts = true;
2001 } else {
2002 old_cr &= ~(UART011_CR_CTSEN | UART011_CR_RTSEN);
2003 uap->autorts = false;
2004 }
2005
Russell Kingc19f12b2010-12-22 17:48:26 +00002006 if (uap->vendor->oversampling) {
2007 if (baud > port->uartclk / 16)
Linus Walleijac3e3fb2010-06-02 20:40:22 +01002008 old_cr |= ST_UART011_CR_OVSFACT;
2009 else
2010 old_cr &= ~ST_UART011_CR_OVSFACT;
2011 }
2012
Linus Walleijc5dd5532012-09-26 17:21:36 +02002013 /*
2014 * Workaround for the ST Micro oversampling variants to
2015 * increase the bitrate slightly, by lowering the divisor,
2016 * to avoid delayed sampling of start bit at high speeds,
2017 * else we see data corruption.
2018 */
2019 if (uap->vendor->oversampling) {
2020 if ((baud >= 3000000) && (baud < 3250000) && (quot > 1))
2021 quot -= 1;
2022 else if ((baud > 3250000) && (quot > 2))
2023 quot -= 2;
2024 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 /* Set baud rate */
Russell King9f25bc52015-11-03 14:51:13 +00002026 pl011_write(quot & 0x3f, uap, REG_FBRD);
2027 pl011_write(quot >> 6, uap, REG_IBRD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028
2029 /*
2030 * ----------v----------v----------v----------v-----
Russell Kinge4df9a82015-11-16 17:40:41 +00002031 * NOTE: REG_LCRH_TX and REG_LCRH_RX MUST BE WRITTEN AFTER
Russell King9f25bc52015-11-03 14:51:13 +00002032 * REG_FBRD & REG_IBRD.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 * ----------^----------^----------^----------^-----
2034 */
Jon Medhurstb60f2f62013-12-10 10:18:59 +00002035 pl011_write_lcr_h(uap, lcr_h);
Russell King9f25bc52015-11-03 14:51:13 +00002036 pl011_write(old_cr, uap, REG_CR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037
2038 spin_unlock_irqrestore(&port->lock, flags);
2039}
2040
Andre Przywara0dd1e242015-05-21 17:26:23 +01002041static void
2042sbsa_uart_set_termios(struct uart_port *port, struct ktermios *termios,
2043 struct ktermios *old)
2044{
2045 struct uart_amba_port *uap =
2046 container_of(port, struct uart_amba_port, port);
2047 unsigned long flags;
2048
2049 tty_termios_encode_baud_rate(termios, uap->fixed_baud, uap->fixed_baud);
2050
2051 /* The SBSA UART only supports 8n1 without hardware flow control. */
2052 termios->c_cflag &= ~(CSIZE | CSTOPB | PARENB | PARODD);
2053 termios->c_cflag &= ~(CMSPAR | CRTSCTS);
2054 termios->c_cflag |= CS8 | CLOCAL;
2055
2056 spin_lock_irqsave(&port->lock, flags);
2057 uart_update_timeout(port, CS8, uap->fixed_baud);
2058 pl011_setup_status_masks(port, termios);
2059 spin_unlock_irqrestore(&port->lock, flags);
2060}
2061
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062static const char *pl011_type(struct uart_port *port)
2063{
Daniel Thompsona5820c22014-09-03 12:51:55 +01002064 struct uart_amba_port *uap =
2065 container_of(port, struct uart_amba_port, port);
Russell Kinge8a7ba82010-12-28 09:16:54 +00002066 return uap->port.type == PORT_AMBA ? uap->type : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067}
2068
2069/*
2070 * Release the memory region(s) being used by 'port'
2071 */
Linus Walleije643f872012-06-17 15:44:19 +02002072static void pl011_release_port(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073{
2074 release_mem_region(port->mapbase, SZ_4K);
2075}
2076
2077/*
2078 * Request the memory region(s) being used by 'port'
2079 */
Linus Walleije643f872012-06-17 15:44:19 +02002080static int pl011_request_port(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081{
2082 return request_mem_region(port->mapbase, SZ_4K, "uart-pl011")
2083 != NULL ? 0 : -EBUSY;
2084}
2085
2086/*
2087 * Configure/autoconfigure the port.
2088 */
Linus Walleije643f872012-06-17 15:44:19 +02002089static void pl011_config_port(struct uart_port *port, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090{
2091 if (flags & UART_CONFIG_TYPE) {
2092 port->type = PORT_AMBA;
Linus Walleije643f872012-06-17 15:44:19 +02002093 pl011_request_port(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 }
2095}
2096
2097/*
2098 * verify the new serial_struct (for TIOCSSERIAL).
2099 */
Linus Walleije643f872012-06-17 15:44:19 +02002100static int pl011_verify_port(struct uart_port *port, struct serial_struct *ser)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101{
2102 int ret = 0;
2103 if (ser->type != PORT_UNKNOWN && ser->type != PORT_AMBA)
2104 ret = -EINVAL;
Yinghai Lua62c4132008-08-19 20:49:55 -07002105 if (ser->irq < 0 || ser->irq >= nr_irqs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 ret = -EINVAL;
2107 if (ser->baud_base < 9600)
2108 ret = -EINVAL;
2109 return ret;
2110}
2111
2112static struct uart_ops amba_pl011_pops = {
Linus Walleije643f872012-06-17 15:44:19 +02002113 .tx_empty = pl011_tx_empty,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 .set_mctrl = pl011_set_mctrl,
Linus Walleije643f872012-06-17 15:44:19 +02002115 .get_mctrl = pl011_get_mctrl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 .stop_tx = pl011_stop_tx,
2117 .start_tx = pl011_start_tx,
2118 .stop_rx = pl011_stop_rx,
2119 .enable_ms = pl011_enable_ms,
2120 .break_ctl = pl011_break_ctl,
2121 .startup = pl011_startup,
2122 .shutdown = pl011_shutdown,
Russell King68b65f72010-12-22 17:24:39 +00002123 .flush_buffer = pl011_dma_flush_buffer,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 .set_termios = pl011_set_termios,
2125 .type = pl011_type,
Linus Walleije643f872012-06-17 15:44:19 +02002126 .release_port = pl011_release_port,
2127 .request_port = pl011_request_port,
2128 .config_port = pl011_config_port,
2129 .verify_port = pl011_verify_port,
Jason Wessel84b5ae12008-02-20 13:33:39 -06002130#ifdef CONFIG_CONSOLE_POLL
Anton Vorontsovb3564c22012-09-24 14:27:54 -07002131 .poll_init = pl011_hwinit,
Linus Walleije643f872012-06-17 15:44:19 +02002132 .poll_get_char = pl011_get_poll_char,
2133 .poll_put_char = pl011_put_poll_char,
Jason Wessel84b5ae12008-02-20 13:33:39 -06002134#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135};
2136
Andre Przywara0dd1e242015-05-21 17:26:23 +01002137static void sbsa_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
2138{
2139}
2140
2141static unsigned int sbsa_uart_get_mctrl(struct uart_port *port)
2142{
2143 return 0;
2144}
2145
2146static const struct uart_ops sbsa_uart_pops = {
2147 .tx_empty = pl011_tx_empty,
2148 .set_mctrl = sbsa_uart_set_mctrl,
2149 .get_mctrl = sbsa_uart_get_mctrl,
2150 .stop_tx = pl011_stop_tx,
2151 .start_tx = pl011_start_tx,
2152 .stop_rx = pl011_stop_rx,
2153 .startup = sbsa_uart_startup,
2154 .shutdown = sbsa_uart_shutdown,
2155 .set_termios = sbsa_uart_set_termios,
2156 .type = pl011_type,
2157 .release_port = pl011_release_port,
2158 .request_port = pl011_request_port,
2159 .config_port = pl011_config_port,
2160 .verify_port = pl011_verify_port,
2161#ifdef CONFIG_CONSOLE_POLL
2162 .poll_init = pl011_hwinit,
2163 .poll_get_char = pl011_get_poll_char,
2164 .poll_put_char = pl011_put_poll_char,
2165#endif
2166};
2167
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168static struct uart_amba_port *amba_ports[UART_NR];
2169
2170#ifdef CONFIG_SERIAL_AMBA_PL011_CONSOLE
2171
Russell Kingd3587882006-03-20 20:00:09 +00002172static void pl011_console_putchar(struct uart_port *port, int ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173{
Daniel Thompsona5820c22014-09-03 12:51:55 +01002174 struct uart_amba_port *uap =
2175 container_of(port, struct uart_amba_port, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176
Russell King9f25bc52015-11-03 14:51:13 +00002177 while (pl011_read(uap, REG_FR) & UART01x_FR_TXFF)
Timur Tabi2f2fd082016-01-15 14:32:20 -06002178 cpu_relax();
Russell King9f25bc52015-11-03 14:51:13 +00002179 pl011_write(ch, uap, REG_DR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180}
2181
2182static void
2183pl011_console_write(struct console *co, const char *s, unsigned int count)
2184{
2185 struct uart_amba_port *uap = amba_ports[co->index];
Timur Tabi2f2fd082016-01-15 14:32:20 -06002186 unsigned int old_cr = 0, new_cr;
Rabin Vincentef605fd2012-01-17 11:52:28 +01002187 unsigned long flags;
2188 int locked = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189
2190 clk_enable(uap->clk);
2191
Rabin Vincentef605fd2012-01-17 11:52:28 +01002192 local_irq_save(flags);
2193 if (uap->port.sysrq)
2194 locked = 0;
2195 else if (oops_in_progress)
2196 locked = spin_trylock(&uap->port.lock);
2197 else
2198 spin_lock(&uap->port.lock);
2199
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 /*
2201 * First save the CR then disable the interrupts
2202 */
Andre Przywara71eec482015-05-21 17:26:21 +01002203 if (!uap->vendor->always_enabled) {
Russell King9f25bc52015-11-03 14:51:13 +00002204 old_cr = pl011_read(uap, REG_CR);
Andre Przywara71eec482015-05-21 17:26:21 +01002205 new_cr = old_cr & ~UART011_CR_CTSEN;
2206 new_cr |= UART01x_CR_UARTEN | UART011_CR_TXE;
Russell King9f25bc52015-11-03 14:51:13 +00002207 pl011_write(new_cr, uap, REG_CR);
Andre Przywara71eec482015-05-21 17:26:21 +01002208 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209
Russell Kingd3587882006-03-20 20:00:09 +00002210 uart_console_write(&uap->port, s, count, pl011_console_putchar);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211
2212 /*
2213 * Finally, wait for transmitter to become empty
2214 * and restore the TCR
2215 */
Shawn Guo0e125a52016-07-08 17:00:39 +08002216 while (pl011_read(uap, REG_FR) & uap->vendor->fr_busy)
Timur Tabi2f2fd082016-01-15 14:32:20 -06002217 cpu_relax();
Andre Przywara71eec482015-05-21 17:26:21 +01002218 if (!uap->vendor->always_enabled)
Russell King9f25bc52015-11-03 14:51:13 +00002219 pl011_write(old_cr, uap, REG_CR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220
Rabin Vincentef605fd2012-01-17 11:52:28 +01002221 if (locked)
2222 spin_unlock(&uap->port.lock);
2223 local_irq_restore(flags);
2224
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 clk_disable(uap->clk);
2226}
2227
2228static void __init
2229pl011_console_get_options(struct uart_amba_port *uap, int *baud,
2230 int *parity, int *bits)
2231{
Russell King9f25bc52015-11-03 14:51:13 +00002232 if (pl011_read(uap, REG_CR) & UART01x_CR_UARTEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 unsigned int lcr_h, ibrd, fbrd;
2234
Russell Kinge4df9a82015-11-16 17:40:41 +00002235 lcr_h = pl011_read(uap, REG_LCRH_TX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236
2237 *parity = 'n';
2238 if (lcr_h & UART01x_LCRH_PEN) {
2239 if (lcr_h & UART01x_LCRH_EPS)
2240 *parity = 'e';
2241 else
2242 *parity = 'o';
2243 }
2244
2245 if ((lcr_h & 0x60) == UART01x_LCRH_WLEN_7)
2246 *bits = 7;
2247 else
2248 *bits = 8;
2249
Russell King9f25bc52015-11-03 14:51:13 +00002250 ibrd = pl011_read(uap, REG_IBRD);
2251 fbrd = pl011_read(uap, REG_FBRD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252
2253 *baud = uap->port.uartclk * 4 / (64 * ibrd + fbrd);
Linus Walleijac3e3fb2010-06-02 20:40:22 +01002254
Russell Kingc19f12b2010-12-22 17:48:26 +00002255 if (uap->vendor->oversampling) {
Russell King9f25bc52015-11-03 14:51:13 +00002256 if (pl011_read(uap, REG_CR)
Linus Walleijac3e3fb2010-06-02 20:40:22 +01002257 & ST_UART011_CR_OVSFACT)
2258 *baud *= 2;
2259 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 }
2261}
2262
2263static int __init pl011_console_setup(struct console *co, char *options)
2264{
2265 struct uart_amba_port *uap;
2266 int baud = 38400;
2267 int bits = 8;
2268 int parity = 'n';
2269 int flow = 'n';
Russell King4b4851c2011-09-22 11:35:30 +01002270 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271
2272 /*
2273 * Check whether an invalid uart number has been specified, and
2274 * if so, search for the first available port that does have
2275 * console support.
2276 */
2277 if (co->index >= UART_NR)
2278 co->index = 0;
2279 uap = amba_ports[co->index];
Russell Kingd28122a2007-01-22 18:59:42 +00002280 if (!uap)
2281 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282
Linus Walleij78d80c52012-05-23 21:18:46 +02002283 /* Allow pins to be muxed in and configured */
Linus Walleij2b996fc2013-06-05 15:36:42 +02002284 pinctrl_pm_select_default_state(uap->port.dev);
Linus Walleij78d80c52012-05-23 21:18:46 +02002285
Russell King4b4851c2011-09-22 11:35:30 +01002286 ret = clk_prepare(uap->clk);
2287 if (ret)
2288 return ret;
2289
Jingoo Han574de552013-07-30 17:06:57 +09002290 if (dev_get_platdata(uap->port.dev)) {
Shreshtha Kumar Sahuc16d51a2011-06-13 10:11:33 +02002291 struct amba_pl011_data *plat;
2292
Jingoo Han574de552013-07-30 17:06:57 +09002293 plat = dev_get_platdata(uap->port.dev);
Shreshtha Kumar Sahuc16d51a2011-06-13 10:11:33 +02002294 if (plat->init)
2295 plat->init();
2296 }
2297
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 uap->port.uartclk = clk_get_rate(uap->clk);
2299
Andre Przywaracefc2d12015-05-21 17:26:22 +01002300 if (uap->vendor->fixed_options) {
2301 baud = uap->fixed_baud;
2302 } else {
2303 if (options)
2304 uart_parse_options(options,
2305 &baud, &parity, &bits, &flow);
2306 else
2307 pl011_console_get_options(uap, &baud, &parity, &bits);
2308 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309
2310 return uart_set_options(&uap->port, co, baud, parity, bits, flow);
2311}
2312
Vincent Sanders2d934862005-09-14 22:36:03 +01002313static struct uart_driver amba_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314static struct console amba_console = {
2315 .name = "ttyAMA",
2316 .write = pl011_console_write,
2317 .device = uart_console_device,
2318 .setup = pl011_console_setup,
2319 .flags = CON_PRINTBUFFER,
2320 .index = -1,
2321 .data = &amba_reg,
2322};
2323
2324#define AMBA_CONSOLE (&amba_console)
Rob Herring0d3c6732014-04-18 17:19:57 -05002325
2326static void pl011_putc(struct uart_port *port, int c)
2327{
Shawn Guo0e125a52016-07-08 17:00:39 +08002328 struct uart_amba_port *uap =
2329 container_of(port, struct uart_amba_port, port);
2330
Russell Kingcdf091c2016-01-04 15:37:41 -06002331 while (readl(port->membase + UART01x_FR) & UART01x_FR_TXFF)
Timur Tabi2f2fd082016-01-15 14:32:20 -06002332 cpu_relax();
Timur Tabi3b78fae2016-01-04 15:37:42 -06002333 if (port->iotype == UPIO_MEM32)
2334 writel(c, port->membase + UART01x_DR);
2335 else
2336 writeb(c, port->membase + UART01x_DR);
Shawn Guo0e125a52016-07-08 17:00:39 +08002337 while (readl(port->membase + UART01x_FR) & uap->vendor->fr_busy)
Timur Tabi2f2fd082016-01-15 14:32:20 -06002338 cpu_relax();
Rob Herring0d3c6732014-04-18 17:19:57 -05002339}
2340
2341static void pl011_early_write(struct console *con, const char *s, unsigned n)
2342{
2343 struct earlycon_device *dev = con->data;
2344
2345 uart_console_write(&dev->port, s, n, pl011_putc);
2346}
2347
2348static int __init pl011_early_console_setup(struct earlycon_device *device,
2349 const char *opt)
2350{
2351 if (!device->port.membase)
2352 return -ENODEV;
2353
2354 device->con->write = pl011_early_write;
2355 return 0;
2356}
Rob Herring45e0f0f2014-03-27 08:08:03 -05002357OF_EARLYCON_DECLARE(pl011, "arm,pl011", pl011_early_console_setup);
Rob Herring0d3c6732014-04-18 17:19:57 -05002358
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359#else
2360#define AMBA_CONSOLE NULL
2361#endif
2362
2363static struct uart_driver amba_reg = {
2364 .owner = THIS_MODULE,
2365 .driver_name = "ttyAMA",
2366 .dev_name = "ttyAMA",
2367 .major = SERIAL_AMBA_MAJOR,
2368 .minor = SERIAL_AMBA_MINOR,
2369 .nr = UART_NR,
2370 .cons = AMBA_CONSOLE,
2371};
2372
Matthew Leach32614aa2012-08-28 16:41:28 +01002373static int pl011_probe_dt_alias(int index, struct device *dev)
2374{
2375 struct device_node *np;
2376 static bool seen_dev_with_alias = false;
2377 static bool seen_dev_without_alias = false;
2378 int ret = index;
2379
2380 if (!IS_ENABLED(CONFIG_OF))
2381 return ret;
2382
2383 np = dev->of_node;
2384 if (!np)
2385 return ret;
2386
2387 ret = of_alias_get_id(np, "serial");
Arnd Bergmann287980e2016-05-27 23:23:25 +02002388 if (ret < 0) {
Matthew Leach32614aa2012-08-28 16:41:28 +01002389 seen_dev_without_alias = true;
2390 ret = index;
2391 } else {
2392 seen_dev_with_alias = true;
2393 if (ret >= ARRAY_SIZE(amba_ports) || amba_ports[ret] != NULL) {
2394 dev_warn(dev, "requested serial port %d not available.\n", ret);
2395 ret = index;
2396 }
2397 }
2398
2399 if (seen_dev_with_alias && seen_dev_without_alias)
2400 dev_warn(dev, "aliased and non-aliased serial devices found in device tree. Serial port enumeration may be unpredictable.\n");
2401
2402 return ret;
2403}
2404
Andre Przywara49bb3c82015-05-21 17:26:14 +01002405/* unregisters the driver also if no more ports are left */
2406static void pl011_unregister_port(struct uart_amba_port *uap)
2407{
2408 int i;
2409 bool busy = false;
2410
2411 for (i = 0; i < ARRAY_SIZE(amba_ports); i++) {
2412 if (amba_ports[i] == uap)
2413 amba_ports[i] = NULL;
2414 else if (amba_ports[i])
2415 busy = true;
2416 }
2417 pl011_dma_remove(uap);
2418 if (!busy)
2419 uart_unregister_driver(&amba_reg);
2420}
2421
Andre Przywara3873e2d2015-05-21 17:26:18 +01002422static int pl011_find_free_port(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423{
Andre Przywara3873e2d2015-05-21 17:26:18 +01002424 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425
2426 for (i = 0; i < ARRAY_SIZE(amba_ports); i++)
2427 if (amba_ports[i] == NULL)
Andre Przywara3873e2d2015-05-21 17:26:18 +01002428 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429
Andre Przywara3873e2d2015-05-21 17:26:18 +01002430 return -EBUSY;
2431}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432
Andre Przywara3873e2d2015-05-21 17:26:18 +01002433static int pl011_setup_port(struct device *dev, struct uart_amba_port *uap,
2434 struct resource *mmiobase, int index)
2435{
2436 void __iomem *base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437
Andre Przywara3873e2d2015-05-21 17:26:18 +01002438 base = devm_ioremap_resource(dev, mmiobase);
Krzysztof Kozlowski97a60ea2015-07-09 22:21:41 +09002439 if (IS_ERR(base))
2440 return PTR_ERR(base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441
Andre Przywara3873e2d2015-05-21 17:26:18 +01002442 index = pl011_probe_dt_alias(index, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443
Shreshtha Kumar Sahud8d8ffa2012-01-18 15:53:59 +05302444 uap->old_cr = 0;
Andre Przywara3873e2d2015-05-21 17:26:18 +01002445 uap->port.dev = dev;
2446 uap->port.mapbase = mmiobase->start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 uap->port.membase = base;
Russell Kingffca2b12010-12-22 17:13:05 +00002448 uap->port.fifosize = uap->fifosize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 uap->port.flags = UPF_BOOT_AUTOCONF;
Andre Przywara3873e2d2015-05-21 17:26:18 +01002450 uap->port.line = index;
2451
2452 amba_ports[index] = uap;
2453
2454 return 0;
2455}
2456
2457static int pl011_register_port(struct uart_amba_port *uap)
2458{
2459 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460
Linus Walleijc3d8b762012-03-21 20:15:18 +01002461 /* Ensure interrupts from this UART are masked and cleared */
Russell King9f25bc52015-11-03 14:51:13 +00002462 pl011_write(0, uap, REG_IMSC);
2463 pl011_write(0xffff, uap, REG_ICR);
Linus Walleijc3d8b762012-03-21 20:15:18 +01002464
Tushar Beheraef2889f2014-01-20 14:32:35 +05302465 if (!amba_reg.state) {
2466 ret = uart_register_driver(&amba_reg);
2467 if (ret < 0) {
Andre Przywara3873e2d2015-05-21 17:26:18 +01002468 dev_err(uap->port.dev,
Jorge Ramirez-Ortiz1c9be312015-03-06 13:05:40 -05002469 "Failed to register AMBA-PL011 driver\n");
Tushar Beheraef2889f2014-01-20 14:32:35 +05302470 return ret;
2471 }
2472 }
2473
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 ret = uart_add_one_port(&amba_reg, &uap->port);
Andre Przywara49bb3c82015-05-21 17:26:14 +01002475 if (ret)
2476 pl011_unregister_port(uap);
Tushar Behera7f6d9422014-06-26 15:35:35 +05302477
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 return ret;
2479}
2480
Andre Przywara3873e2d2015-05-21 17:26:18 +01002481static int pl011_probe(struct amba_device *dev, const struct amba_id *id)
2482{
2483 struct uart_amba_port *uap;
2484 struct vendor_data *vendor = id->data;
2485 int portnr, ret;
2486
2487 portnr = pl011_find_free_port();
2488 if (portnr < 0)
2489 return portnr;
2490
2491 uap = devm_kzalloc(&dev->dev, sizeof(struct uart_amba_port),
2492 GFP_KERNEL);
2493 if (!uap)
2494 return -ENOMEM;
2495
2496 uap->clk = devm_clk_get(&dev->dev, NULL);
2497 if (IS_ERR(uap->clk))
2498 return PTR_ERR(uap->clk);
2499
Russell King439403b2015-11-16 17:40:31 +00002500 uap->reg_offset = vendor->reg_offset;
Andre Przywara3873e2d2015-05-21 17:26:18 +01002501 uap->vendor = vendor;
Andre Przywara3873e2d2015-05-21 17:26:18 +01002502 uap->fifosize = vendor->get_fifosize(dev);
Timur Tabi3b78fae2016-01-04 15:37:42 -06002503 uap->port.iotype = vendor->access_32b ? UPIO_MEM32 : UPIO_MEM;
Andre Przywara3873e2d2015-05-21 17:26:18 +01002504 uap->port.irq = dev->irq[0];
2505 uap->port.ops = &amba_pl011_pops;
2506
2507 snprintf(uap->type, sizeof(uap->type), "PL011 rev%u", amba_rev(dev));
2508
2509 ret = pl011_setup_port(&dev->dev, uap, &dev->res, portnr);
2510 if (ret)
2511 return ret;
2512
2513 amba_set_drvdata(dev, uap);
2514
2515 return pl011_register_port(uap);
2516}
2517
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518static int pl011_remove(struct amba_device *dev)
2519{
2520 struct uart_amba_port *uap = amba_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522 uart_remove_one_port(&amba_reg, &uap->port);
Andre Przywara49bb3c82015-05-21 17:26:14 +01002523 pl011_unregister_port(uap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524 return 0;
2525}
2526
Ulf Hanssond0ce8502013-12-03 11:04:28 +01002527#ifdef CONFIG_PM_SLEEP
2528static int pl011_suspend(struct device *dev)
Leo Chenb736b892009-07-28 23:43:33 +01002529{
Ulf Hanssond0ce8502013-12-03 11:04:28 +01002530 struct uart_amba_port *uap = dev_get_drvdata(dev);
Leo Chenb736b892009-07-28 23:43:33 +01002531
2532 if (!uap)
2533 return -EINVAL;
2534
2535 return uart_suspend_port(&amba_reg, &uap->port);
2536}
2537
Ulf Hanssond0ce8502013-12-03 11:04:28 +01002538static int pl011_resume(struct device *dev)
Leo Chenb736b892009-07-28 23:43:33 +01002539{
Ulf Hanssond0ce8502013-12-03 11:04:28 +01002540 struct uart_amba_port *uap = dev_get_drvdata(dev);
Leo Chenb736b892009-07-28 23:43:33 +01002541
2542 if (!uap)
2543 return -EINVAL;
2544
2545 return uart_resume_port(&amba_reg, &uap->port);
2546}
2547#endif
2548
Ulf Hanssond0ce8502013-12-03 11:04:28 +01002549static SIMPLE_DEV_PM_OPS(pl011_dev_pm_ops, pl011_suspend, pl011_resume);
2550
Andre Przywara0dd1e242015-05-21 17:26:23 +01002551static int sbsa_uart_probe(struct platform_device *pdev)
2552{
2553 struct uart_amba_port *uap;
2554 struct resource *r;
2555 int portnr, ret;
2556 int baudrate;
2557
2558 /*
2559 * Check the mandatory baud rate parameter in the DT node early
2560 * so that we can easily exit with the error.
2561 */
2562 if (pdev->dev.of_node) {
2563 struct device_node *np = pdev->dev.of_node;
2564
2565 ret = of_property_read_u32(np, "current-speed", &baudrate);
2566 if (ret)
2567 return ret;
2568 } else {
2569 baudrate = 115200;
2570 }
2571
2572 portnr = pl011_find_free_port();
2573 if (portnr < 0)
2574 return portnr;
2575
2576 uap = devm_kzalloc(&pdev->dev, sizeof(struct uart_amba_port),
2577 GFP_KERNEL);
2578 if (!uap)
2579 return -ENOMEM;
2580
Jiri Slaby394a9e22016-05-09 09:23:35 +02002581 ret = platform_get_irq(pdev, 0);
2582 if (ret < 0) {
2583 dev_err(&pdev->dev, "cannot obtain irq\n");
2584 return ret;
2585 }
2586 uap->port.irq = ret;
2587
Russell King439403b2015-11-16 17:40:31 +00002588 uap->reg_offset = vendor_sbsa.reg_offset;
Andre Przywara0dd1e242015-05-21 17:26:23 +01002589 uap->vendor = &vendor_sbsa;
2590 uap->fifosize = 32;
Timur Tabi3b78fae2016-01-04 15:37:42 -06002591 uap->port.iotype = vendor_sbsa.access_32b ? UPIO_MEM32 : UPIO_MEM;
Andre Przywara0dd1e242015-05-21 17:26:23 +01002592 uap->port.ops = &sbsa_uart_pops;
2593 uap->fixed_baud = baudrate;
2594
2595 snprintf(uap->type, sizeof(uap->type), "SBSA");
2596
2597 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2598
2599 ret = pl011_setup_port(&pdev->dev, uap, r, portnr);
2600 if (ret)
2601 return ret;
2602
2603 platform_set_drvdata(pdev, uap);
2604
2605 return pl011_register_port(uap);
2606}
2607
2608static int sbsa_uart_remove(struct platform_device *pdev)
2609{
2610 struct uart_amba_port *uap = platform_get_drvdata(pdev);
2611
2612 uart_remove_one_port(&amba_reg, &uap->port);
2613 pl011_unregister_port(uap);
2614 return 0;
2615}
2616
2617static const struct of_device_id sbsa_uart_of_match[] = {
2618 { .compatible = "arm,sbsa-uart", },
2619 {},
2620};
2621MODULE_DEVICE_TABLE(of, sbsa_uart_of_match);
2622
Graeme Gregory3db9ab02015-05-21 17:26:24 +01002623static const struct acpi_device_id sbsa_uart_acpi_match[] = {
2624 { "ARMH0011", 0 },
2625 {},
2626};
2627MODULE_DEVICE_TABLE(acpi, sbsa_uart_acpi_match);
2628
Andre Przywara0dd1e242015-05-21 17:26:23 +01002629static struct platform_driver arm_sbsa_uart_platform_driver = {
2630 .probe = sbsa_uart_probe,
2631 .remove = sbsa_uart_remove,
2632 .driver = {
2633 .name = "sbsa-uart",
2634 .of_match_table = of_match_ptr(sbsa_uart_of_match),
Graeme Gregory3db9ab02015-05-21 17:26:24 +01002635 .acpi_match_table = ACPI_PTR(sbsa_uart_acpi_match),
Andre Przywara0dd1e242015-05-21 17:26:23 +01002636 },
2637};
2638
Russell King2c39c9e2010-07-27 08:50:16 +01002639static struct amba_id pl011_ids[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640 {
2641 .id = 0x00041011,
2642 .mask = 0x000fffff,
Alessandro Rubini5926a292009-06-04 17:43:04 +01002643 .data = &vendor_arm,
2644 },
2645 {
2646 .id = 0x00380802,
2647 .mask = 0x00ffffff,
2648 .data = &vendor_st,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649 },
2650 { 0, 0 },
2651};
2652
Dave Martin60f7a332011-10-05 15:15:22 +01002653MODULE_DEVICE_TABLE(amba, pl011_ids);
2654
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655static struct amba_driver pl011_driver = {
2656 .drv = {
2657 .name = "uart-pl011",
Ulf Hanssond0ce8502013-12-03 11:04:28 +01002658 .pm = &pl011_dev_pm_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659 },
2660 .id_table = pl011_ids,
2661 .probe = pl011_probe,
2662 .remove = pl011_remove,
2663};
2664
2665static int __init pl011_init(void)
2666{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667 printk(KERN_INFO "Serial: AMBA PL011 UART driver\n");
2668
Andre Przywara0dd1e242015-05-21 17:26:23 +01002669 if (platform_driver_register(&arm_sbsa_uart_platform_driver))
2670 pr_warn("could not register SBSA UART platform driver\n");
Greg Kroah-Hartman062a68a2015-09-04 09:11:24 -07002671 return amba_driver_register(&pl011_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672}
2673
2674static void __exit pl011_exit(void)
2675{
Andre Przywara0dd1e242015-05-21 17:26:23 +01002676 platform_driver_unregister(&arm_sbsa_uart_platform_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677 amba_driver_unregister(&pl011_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678}
2679
Alessandro Rubini4dd9e742009-05-05 05:54:13 +01002680/*
2681 * While this can be a module, if builtin it's most likely the console
2682 * So let's leave module_exit but move module_init to an earlier place
2683 */
2684arch_initcall(pl011_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685module_exit(pl011_exit);
2686
2687MODULE_AUTHOR("ARM Ltd/Deep Blue Solutions Ltd");
2688MODULE_DESCRIPTION("ARM AMBA serial port driver");
2689MODULE_LICENSE("GPL");