Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | * 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 King | 68b65f7 | 2010-12-22 17:24:39 +0000 | [diff] [blame] | 8 | * Copyright (C) 2010 ST-Ericsson SA |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | * |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | * 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | |
Chanho Min | cb06ff1 | 2013-03-27 18:38:11 +0900 | [diff] [blame] | 32 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | #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 King | a62c80e | 2006-01-07 13:52:45 +0000 | [diff] [blame] | 47 | #include <linux/amba/bus.h> |
| 48 | #include <linux/amba/serial.h> |
Russell King | f8ce254 | 2006-01-07 16:15:52 +0000 | [diff] [blame] | 49 | #include <linux/clk.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 50 | #include <linux/slab.h> |
Russell King | 68b65f7 | 2010-12-22 17:24:39 +0000 | [diff] [blame] | 51 | #include <linux/dmaengine.h> |
| 52 | #include <linux/dma-mapping.h> |
| 53 | #include <linux/scatterlist.h> |
Shreshtha Kumar Sahu | c16d51a | 2011-06-13 10:11:33 +0200 | [diff] [blame] | 54 | #include <linux/delay.h> |
Viresh Kumar | 258aea7 | 2012-02-01 16:12:19 +0530 | [diff] [blame] | 55 | #include <linux/types.h> |
Matthew Leach | 32614aa | 2012-08-28 16:41:28 +0100 | [diff] [blame] | 56 | #include <linux/of.h> |
| 57 | #include <linux/of_device.h> |
Shawn Guo | 258e055 | 2012-05-06 22:53:35 +0800 | [diff] [blame] | 58 | #include <linux/pinctrl/consumer.h> |
Alessandro Rubini | cb70706 | 2012-06-24 12:46:37 +0100 | [diff] [blame] | 59 | #include <linux/sizes.h> |
Linus Walleij | de60958 | 2012-10-15 13:36:01 +0200 | [diff] [blame] | 60 | #include <linux/io.h> |
Graeme Gregory | 3db9ab0 | 2015-05-21 17:26:24 +0100 | [diff] [blame] | 61 | #include <linux/acpi.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | |
| 63 | #define UART_NR 14 |
| 64 | |
| 65 | #define SERIAL_AMBA_MAJOR 204 |
| 66 | #define SERIAL_AMBA_MINOR 64 |
| 67 | #define SERIAL_AMBA_NR UART_NR |
| 68 | |
| 69 | #define AMBA_ISR_PASS_LIMIT 256 |
| 70 | |
Russell King | b63d4f0 | 2005-11-19 11:10:35 +0000 | [diff] [blame] | 71 | #define UART_DR_ERROR (UART011_DR_OE|UART011_DR_BE|UART011_DR_PE|UART011_DR_FE) |
| 72 | #define UART_DUMMY_DR_RX (1 << 16) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | |
Alessandro Rubini | 5926a29 | 2009-06-04 17:43:04 +0100 | [diff] [blame] | 74 | /* There is by now at least one vendor with differing details, so handle it */ |
| 75 | struct vendor_data { |
| 76 | unsigned int ifls; |
Linus Walleij | ec489aa | 2010-06-02 08:13:52 +0100 | [diff] [blame] | 77 | unsigned int lcrh_tx; |
| 78 | unsigned int lcrh_rx; |
Linus Walleij | ac3e3fb | 2010-06-02 20:40:22 +0100 | [diff] [blame] | 79 | bool oversampling; |
Russell King | 38d6243 | 2010-12-22 17:59:16 +0000 | [diff] [blame] | 80 | bool dma_threshold; |
Rajanikanth H.V | 4fd0690 | 2012-03-26 11:17:02 +0200 | [diff] [blame] | 81 | bool cts_event_workaround; |
Andre Przywara | 71eec48 | 2015-05-21 17:26:21 +0100 | [diff] [blame] | 82 | bool always_enabled; |
Andre Przywara | cefc2d1 | 2015-05-21 17:26:22 +0100 | [diff] [blame] | 83 | bool fixed_options; |
Jongsung Kim | 78506f2 | 2013-04-15 14:45:25 +0900 | [diff] [blame] | 84 | |
Jongsung Kim | ea33640 | 2013-05-10 18:05:35 +0900 | [diff] [blame] | 85 | unsigned int (*get_fifosize)(struct amba_device *dev); |
Alessandro Rubini | 5926a29 | 2009-06-04 17:43:04 +0100 | [diff] [blame] | 86 | }; |
| 87 | |
Jongsung Kim | ea33640 | 2013-05-10 18:05:35 +0900 | [diff] [blame] | 88 | static unsigned int get_fifosize_arm(struct amba_device *dev) |
Jongsung Kim | 78506f2 | 2013-04-15 14:45:25 +0900 | [diff] [blame] | 89 | { |
Jongsung Kim | ea33640 | 2013-05-10 18:05:35 +0900 | [diff] [blame] | 90 | return amba_rev(dev) < 3 ? 16 : 32; |
Jongsung Kim | 78506f2 | 2013-04-15 14:45:25 +0900 | [diff] [blame] | 91 | } |
| 92 | |
Alessandro Rubini | 5926a29 | 2009-06-04 17:43:04 +0100 | [diff] [blame] | 93 | static struct vendor_data vendor_arm = { |
| 94 | .ifls = UART011_IFLS_RX4_8|UART011_IFLS_TX4_8, |
Greg Kroah-Hartman | 0de6cfb | 2015-09-04 09:13:56 -0700 | [diff] [blame] | 95 | .lcrh_tx = UART011_LCRH, |
| 96 | .lcrh_rx = UART011_LCRH, |
Linus Walleij | ac3e3fb | 2010-06-02 20:40:22 +0100 | [diff] [blame] | 97 | .oversampling = false, |
Russell King | 38d6243 | 2010-12-22 17:59:16 +0000 | [diff] [blame] | 98 | .dma_threshold = false, |
Rajanikanth H.V | 4fd0690 | 2012-03-26 11:17:02 +0200 | [diff] [blame] | 99 | .cts_event_workaround = false, |
Andre Przywara | 71eec48 | 2015-05-21 17:26:21 +0100 | [diff] [blame] | 100 | .always_enabled = false, |
Andre Przywara | cefc2d1 | 2015-05-21 17:26:22 +0100 | [diff] [blame] | 101 | .fixed_options = false, |
Jongsung Kim | 78506f2 | 2013-04-15 14:45:25 +0900 | [diff] [blame] | 102 | .get_fifosize = get_fifosize_arm, |
Alessandro Rubini | 5926a29 | 2009-06-04 17:43:04 +0100 | [diff] [blame] | 103 | }; |
| 104 | |
Andre Przywara | 0dd1e24 | 2015-05-21 17:26:23 +0100 | [diff] [blame] | 105 | static struct vendor_data vendor_sbsa = { |
| 106 | .oversampling = false, |
| 107 | .dma_threshold = false, |
| 108 | .cts_event_workaround = false, |
| 109 | .always_enabled = true, |
| 110 | .fixed_options = true, |
| 111 | }; |
| 112 | |
Jongsung Kim | ea33640 | 2013-05-10 18:05:35 +0900 | [diff] [blame] | 113 | static unsigned int get_fifosize_st(struct amba_device *dev) |
Jongsung Kim | 78506f2 | 2013-04-15 14:45:25 +0900 | [diff] [blame] | 114 | { |
| 115 | return 64; |
| 116 | } |
| 117 | |
Alessandro Rubini | 5926a29 | 2009-06-04 17:43:04 +0100 | [diff] [blame] | 118 | static struct vendor_data vendor_st = { |
| 119 | .ifls = UART011_IFLS_RX_HALF|UART011_IFLS_TX_HALF, |
Greg Kroah-Hartman | 0de6cfb | 2015-09-04 09:13:56 -0700 | [diff] [blame] | 120 | .lcrh_tx = ST_UART011_LCRH_TX, |
| 121 | .lcrh_rx = ST_UART011_LCRH_RX, |
Linus Walleij | ac3e3fb | 2010-06-02 20:40:22 +0100 | [diff] [blame] | 122 | .oversampling = true, |
Russell King | 38d6243 | 2010-12-22 17:59:16 +0000 | [diff] [blame] | 123 | .dma_threshold = true, |
Rajanikanth H.V | 4fd0690 | 2012-03-26 11:17:02 +0200 | [diff] [blame] | 124 | .cts_event_workaround = true, |
Andre Przywara | 71eec48 | 2015-05-21 17:26:21 +0100 | [diff] [blame] | 125 | .always_enabled = false, |
Andre Przywara | cefc2d1 | 2015-05-21 17:26:22 +0100 | [diff] [blame] | 126 | .fixed_options = false, |
Jongsung Kim | 78506f2 | 2013-04-15 14:45:25 +0900 | [diff] [blame] | 127 | .get_fifosize = get_fifosize_st, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | }; |
| 129 | |
Russell King | 68b65f7 | 2010-12-22 17:24:39 +0000 | [diff] [blame] | 130 | /* Deals with DMA transactions */ |
Linus Walleij | ead76f32 | 2011-02-24 13:21:08 +0100 | [diff] [blame] | 131 | |
| 132 | struct pl011_sgbuf { |
| 133 | struct scatterlist sg; |
| 134 | char *buf; |
| 135 | }; |
| 136 | |
| 137 | struct pl011_dmarx_data { |
| 138 | struct dma_chan *chan; |
| 139 | struct completion complete; |
| 140 | bool use_buf_b; |
| 141 | struct pl011_sgbuf sgbuf_a; |
| 142 | struct pl011_sgbuf sgbuf_b; |
| 143 | dma_cookie_t cookie; |
| 144 | bool running; |
Chanho Min | cb06ff1 | 2013-03-27 18:38:11 +0900 | [diff] [blame] | 145 | struct timer_list timer; |
| 146 | unsigned int last_residue; |
| 147 | unsigned long last_jiffies; |
| 148 | bool auto_poll_rate; |
| 149 | unsigned int poll_rate; |
| 150 | unsigned int poll_timeout; |
Linus Walleij | ead76f32 | 2011-02-24 13:21:08 +0100 | [diff] [blame] | 151 | }; |
| 152 | |
Russell King | 68b65f7 | 2010-12-22 17:24:39 +0000 | [diff] [blame] | 153 | struct pl011_dmatx_data { |
| 154 | struct dma_chan *chan; |
| 155 | struct scatterlist sg; |
| 156 | char *buf; |
| 157 | bool queued; |
| 158 | }; |
| 159 | |
Russell King | c19f12b | 2010-12-22 17:48:26 +0000 | [diff] [blame] | 160 | /* |
| 161 | * We wrap our port structure around the generic uart_port. |
| 162 | */ |
| 163 | struct uart_amba_port { |
| 164 | struct uart_port port; |
| 165 | struct clk *clk; |
| 166 | const struct vendor_data *vendor; |
Russell King | 68b65f7 | 2010-12-22 17:24:39 +0000 | [diff] [blame] | 167 | unsigned int dmacr; /* dma control reg */ |
Russell King | c19f12b | 2010-12-22 17:48:26 +0000 | [diff] [blame] | 168 | unsigned int im; /* interrupt mask */ |
| 169 | unsigned int old_status; |
Russell King | ffca2b1 | 2010-12-22 17:13:05 +0000 | [diff] [blame] | 170 | unsigned int fifosize; /* vendor-specific */ |
Russell King | c19f12b | 2010-12-22 17:48:26 +0000 | [diff] [blame] | 171 | unsigned int lcrh_tx; /* vendor-specific */ |
| 172 | unsigned int lcrh_rx; /* vendor-specific */ |
Shreshtha Kumar Sahu | d8d8ffa | 2012-01-18 15:53:59 +0530 | [diff] [blame] | 173 | unsigned int old_cr; /* state during shutdown */ |
Russell King | c19f12b | 2010-12-22 17:48:26 +0000 | [diff] [blame] | 174 | bool autorts; |
Andre Przywara | cefc2d1 | 2015-05-21 17:26:22 +0100 | [diff] [blame] | 175 | unsigned int fixed_baud; /* vendor-set fixed baud rate */ |
Russell King | c19f12b | 2010-12-22 17:48:26 +0000 | [diff] [blame] | 176 | char type[12]; |
Russell King | 68b65f7 | 2010-12-22 17:24:39 +0000 | [diff] [blame] | 177 | #ifdef CONFIG_DMA_ENGINE |
| 178 | /* DMA stuff */ |
Linus Walleij | ead76f32 | 2011-02-24 13:21:08 +0100 | [diff] [blame] | 179 | bool using_tx_dma; |
| 180 | bool using_rx_dma; |
| 181 | struct pl011_dmarx_data dmarx; |
Russell King | 68b65f7 | 2010-12-22 17:24:39 +0000 | [diff] [blame] | 182 | struct pl011_dmatx_data dmatx; |
Jorge Ramirez-Ortiz | 1c9be31 | 2015-03-06 13:05:40 -0500 | [diff] [blame] | 183 | bool dma_probed; |
Russell King | 68b65f7 | 2010-12-22 17:24:39 +0000 | [diff] [blame] | 184 | #endif |
Russell King | c19f12b | 2010-12-22 17:48:26 +0000 | [diff] [blame] | 185 | }; |
| 186 | |
Russell King | 7583633 | 2015-11-03 14:50:58 +0000 | [diff] [blame^] | 187 | static unsigned int pl011_read(void __iomem *base, unsigned int reg) |
| 188 | { |
| 189 | return readw(base + reg); |
| 190 | } |
| 191 | |
| 192 | static void pl011_write(unsigned int val, void __iomem *base, unsigned int reg) |
| 193 | { |
| 194 | writew(val, base + reg); |
| 195 | } |
| 196 | |
Russell King | 68b65f7 | 2010-12-22 17:24:39 +0000 | [diff] [blame] | 197 | /* |
Linus Walleij | 29772c4 | 2011-02-24 13:21:36 +0100 | [diff] [blame] | 198 | * Reads up to 256 characters from the FIFO or until it's empty and |
| 199 | * inserts them into the TTY layer. Returns the number of characters |
| 200 | * read from the FIFO. |
| 201 | */ |
| 202 | static int pl011_fifo_to_tty(struct uart_amba_port *uap) |
| 203 | { |
Timur Tabi | 71a5cd8 | 2015-10-07 15:27:16 -0500 | [diff] [blame] | 204 | u16 status; |
| 205 | unsigned int ch, flag, max_count = 256; |
Linus Walleij | 29772c4 | 2011-02-24 13:21:36 +0100 | [diff] [blame] | 206 | int fifotaken = 0; |
| 207 | |
| 208 | while (max_count--) { |
Russell King | 7583633 | 2015-11-03 14:50:58 +0000 | [diff] [blame^] | 209 | status = pl011_read(uap->port.membase, UART01x_FR); |
Linus Walleij | 29772c4 | 2011-02-24 13:21:36 +0100 | [diff] [blame] | 210 | if (status & UART01x_FR_RXFE) |
| 211 | break; |
| 212 | |
| 213 | /* Take chars from the FIFO and update status */ |
Russell King | 7583633 | 2015-11-03 14:50:58 +0000 | [diff] [blame^] | 214 | ch = pl011_read(uap->port.membase, UART01x_DR) | |
Linus Walleij | 29772c4 | 2011-02-24 13:21:36 +0100 | [diff] [blame] | 215 | UART_DUMMY_DR_RX; |
| 216 | flag = TTY_NORMAL; |
| 217 | uap->port.icount.rx++; |
| 218 | fifotaken++; |
| 219 | |
| 220 | if (unlikely(ch & UART_DR_ERROR)) { |
| 221 | if (ch & UART011_DR_BE) { |
| 222 | ch &= ~(UART011_DR_FE | UART011_DR_PE); |
| 223 | uap->port.icount.brk++; |
| 224 | if (uart_handle_break(&uap->port)) |
| 225 | continue; |
| 226 | } else if (ch & UART011_DR_PE) |
| 227 | uap->port.icount.parity++; |
| 228 | else if (ch & UART011_DR_FE) |
| 229 | uap->port.icount.frame++; |
| 230 | if (ch & UART011_DR_OE) |
| 231 | uap->port.icount.overrun++; |
| 232 | |
| 233 | ch &= uap->port.read_status_mask; |
| 234 | |
| 235 | if (ch & UART011_DR_BE) |
| 236 | flag = TTY_BREAK; |
| 237 | else if (ch & UART011_DR_PE) |
| 238 | flag = TTY_PARITY; |
| 239 | else if (ch & UART011_DR_FE) |
| 240 | flag = TTY_FRAME; |
| 241 | } |
| 242 | |
| 243 | if (uart_handle_sysrq_char(&uap->port, ch & 255)) |
| 244 | continue; |
| 245 | |
| 246 | uart_insert_char(&uap->port, ch, UART011_DR_OE, ch, flag); |
| 247 | } |
| 248 | |
| 249 | return fifotaken; |
| 250 | } |
| 251 | |
| 252 | |
| 253 | /* |
Russell King | 68b65f7 | 2010-12-22 17:24:39 +0000 | [diff] [blame] | 254 | * All the DMA operation mode stuff goes inside this ifdef. |
| 255 | * This assumes that you have a generic DMA device interface, |
| 256 | * no custom DMA interfaces are supported. |
| 257 | */ |
| 258 | #ifdef CONFIG_DMA_ENGINE |
| 259 | |
| 260 | #define PL011_DMA_BUFFER_SIZE PAGE_SIZE |
| 261 | |
Linus Walleij | ead76f32 | 2011-02-24 13:21:08 +0100 | [diff] [blame] | 262 | static int pl011_sgbuf_init(struct dma_chan *chan, struct pl011_sgbuf *sg, |
| 263 | enum dma_data_direction dir) |
| 264 | { |
Chanho Min | cb06ff1 | 2013-03-27 18:38:11 +0900 | [diff] [blame] | 265 | dma_addr_t dma_addr; |
| 266 | |
| 267 | sg->buf = dma_alloc_coherent(chan->device->dev, |
| 268 | PL011_DMA_BUFFER_SIZE, &dma_addr, GFP_KERNEL); |
Linus Walleij | ead76f32 | 2011-02-24 13:21:08 +0100 | [diff] [blame] | 269 | if (!sg->buf) |
| 270 | return -ENOMEM; |
| 271 | |
Chanho Min | cb06ff1 | 2013-03-27 18:38:11 +0900 | [diff] [blame] | 272 | sg_init_table(&sg->sg, 1); |
| 273 | sg_set_page(&sg->sg, phys_to_page(dma_addr), |
| 274 | PL011_DMA_BUFFER_SIZE, offset_in_page(dma_addr)); |
| 275 | sg_dma_address(&sg->sg) = dma_addr; |
Andrew Jackson | c64be92 | 2014-11-07 14:14:43 +0000 | [diff] [blame] | 276 | sg_dma_len(&sg->sg) = PL011_DMA_BUFFER_SIZE; |
Linus Walleij | ead76f32 | 2011-02-24 13:21:08 +0100 | [diff] [blame] | 277 | |
Linus Walleij | ead76f32 | 2011-02-24 13:21:08 +0100 | [diff] [blame] | 278 | return 0; |
| 279 | } |
| 280 | |
| 281 | static void pl011_sgbuf_free(struct dma_chan *chan, struct pl011_sgbuf *sg, |
| 282 | enum dma_data_direction dir) |
| 283 | { |
| 284 | if (sg->buf) { |
Chanho Min | cb06ff1 | 2013-03-27 18:38:11 +0900 | [diff] [blame] | 285 | dma_free_coherent(chan->device->dev, |
| 286 | PL011_DMA_BUFFER_SIZE, sg->buf, |
| 287 | sg_dma_address(&sg->sg)); |
Linus Walleij | ead76f32 | 2011-02-24 13:21:08 +0100 | [diff] [blame] | 288 | } |
| 289 | } |
| 290 | |
Jorge Ramirez-Ortiz | 1c9be31 | 2015-03-06 13:05:40 -0500 | [diff] [blame] | 291 | static void pl011_dma_probe(struct uart_amba_port *uap) |
Russell King | 68b65f7 | 2010-12-22 17:24:39 +0000 | [diff] [blame] | 292 | { |
| 293 | /* DMA is the sole user of the platform data right now */ |
Jingoo Han | 574de55 | 2013-07-30 17:06:57 +0900 | [diff] [blame] | 294 | struct amba_pl011_data *plat = dev_get_platdata(uap->port.dev); |
Jorge Ramirez-Ortiz | 1c9be31 | 2015-03-06 13:05:40 -0500 | [diff] [blame] | 295 | struct device *dev = uap->port.dev; |
Russell King | 68b65f7 | 2010-12-22 17:24:39 +0000 | [diff] [blame] | 296 | struct dma_slave_config tx_conf = { |
Greg Kroah-Hartman | 0de6cfb | 2015-09-04 09:13:56 -0700 | [diff] [blame] | 297 | .dst_addr = uap->port.mapbase + UART01x_DR, |
Russell King | 68b65f7 | 2010-12-22 17:24:39 +0000 | [diff] [blame] | 298 | .dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE, |
Vinod Koul | a485df4 | 2011-10-14 10:47:38 +0530 | [diff] [blame] | 299 | .direction = DMA_MEM_TO_DEV, |
Russell King | 68b65f7 | 2010-12-22 17:24:39 +0000 | [diff] [blame] | 300 | .dst_maxburst = uap->fifosize >> 1, |
Viresh Kumar | 258aea7 | 2012-02-01 16:12:19 +0530 | [diff] [blame] | 301 | .device_fc = false, |
Russell King | 68b65f7 | 2010-12-22 17:24:39 +0000 | [diff] [blame] | 302 | }; |
| 303 | struct dma_chan *chan; |
| 304 | dma_cap_mask_t mask; |
| 305 | |
Jorge Ramirez-Ortiz | 1c9be31 | 2015-03-06 13:05:40 -0500 | [diff] [blame] | 306 | uap->dma_probed = true; |
| 307 | chan = dma_request_slave_channel_reason(dev, "tx"); |
| 308 | if (IS_ERR(chan)) { |
| 309 | if (PTR_ERR(chan) == -EPROBE_DEFER) { |
Jorge Ramirez-Ortiz | 1c9be31 | 2015-03-06 13:05:40 -0500 | [diff] [blame] | 310 | uap->dma_probed = false; |
| 311 | return; |
| 312 | } |
Russell King | 68b65f7 | 2010-12-22 17:24:39 +0000 | [diff] [blame] | 313 | |
Arnd Bergmann | 787b0c1 | 2013-01-28 16:24:37 +0000 | [diff] [blame] | 314 | /* We need platform data */ |
| 315 | if (!plat || !plat->dma_filter) { |
| 316 | dev_info(uap->port.dev, "no DMA platform data\n"); |
| 317 | return; |
| 318 | } |
| 319 | |
| 320 | /* Try to acquire a generic DMA engine slave TX channel */ |
| 321 | dma_cap_zero(mask); |
| 322 | dma_cap_set(DMA_SLAVE, mask); |
| 323 | |
| 324 | chan = dma_request_channel(mask, plat->dma_filter, |
| 325 | plat->dma_tx_param); |
| 326 | if (!chan) { |
| 327 | dev_err(uap->port.dev, "no TX DMA channel!\n"); |
| 328 | return; |
| 329 | } |
Russell King | 68b65f7 | 2010-12-22 17:24:39 +0000 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | dmaengine_slave_config(chan, &tx_conf); |
| 333 | uap->dmatx.chan = chan; |
| 334 | |
| 335 | dev_info(uap->port.dev, "DMA channel TX %s\n", |
| 336 | dma_chan_name(uap->dmatx.chan)); |
Linus Walleij | ead76f32 | 2011-02-24 13:21:08 +0100 | [diff] [blame] | 337 | |
| 338 | /* Optionally make use of an RX channel as well */ |
Arnd Bergmann | 787b0c1 | 2013-01-28 16:24:37 +0000 | [diff] [blame] | 339 | chan = dma_request_slave_channel(dev, "rx"); |
Rob Herring | 0d3c673 | 2014-04-18 17:19:57 -0500 | [diff] [blame] | 340 | |
Arnd Bergmann | 787b0c1 | 2013-01-28 16:24:37 +0000 | [diff] [blame] | 341 | if (!chan && plat->dma_rx_param) { |
| 342 | chan = dma_request_channel(mask, plat->dma_filter, plat->dma_rx_param); |
| 343 | |
| 344 | if (!chan) { |
| 345 | dev_err(uap->port.dev, "no RX DMA channel!\n"); |
| 346 | return; |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | if (chan) { |
Linus Walleij | ead76f32 | 2011-02-24 13:21:08 +0100 | [diff] [blame] | 351 | struct dma_slave_config rx_conf = { |
Greg Kroah-Hartman | 0de6cfb | 2015-09-04 09:13:56 -0700 | [diff] [blame] | 352 | .src_addr = uap->port.mapbase + UART01x_DR, |
Linus Walleij | ead76f32 | 2011-02-24 13:21:08 +0100 | [diff] [blame] | 353 | .src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE, |
Vinod Koul | a485df4 | 2011-10-14 10:47:38 +0530 | [diff] [blame] | 354 | .direction = DMA_DEV_TO_MEM, |
Guennadi Liakhovetski | b2aeb77 | 2014-04-12 19:47:17 +0200 | [diff] [blame] | 355 | .src_maxburst = uap->fifosize >> 2, |
Viresh Kumar | 258aea7 | 2012-02-01 16:12:19 +0530 | [diff] [blame] | 356 | .device_fc = false, |
Linus Walleij | ead76f32 | 2011-02-24 13:21:08 +0100 | [diff] [blame] | 357 | }; |
Andrew Jackson | 2d3b7d6 | 2014-11-07 14:14:47 +0000 | [diff] [blame] | 358 | struct dma_slave_caps caps; |
Linus Walleij | ead76f32 | 2011-02-24 13:21:08 +0100 | [diff] [blame] | 359 | |
Andrew Jackson | 2d3b7d6 | 2014-11-07 14:14:47 +0000 | [diff] [blame] | 360 | /* |
| 361 | * Some DMA controllers provide information on their capabilities. |
| 362 | * If the controller does, check for suitable residue processing |
| 363 | * otherwise assime all is well. |
| 364 | */ |
| 365 | if (0 == dma_get_slave_caps(chan, &caps)) { |
| 366 | if (caps.residue_granularity == |
| 367 | DMA_RESIDUE_GRANULARITY_DESCRIPTOR) { |
| 368 | dma_release_channel(chan); |
| 369 | dev_info(uap->port.dev, |
| 370 | "RX DMA disabled - no residue processing\n"); |
| 371 | return; |
| 372 | } |
| 373 | } |
Linus Walleij | ead76f32 | 2011-02-24 13:21:08 +0100 | [diff] [blame] | 374 | dmaengine_slave_config(chan, &rx_conf); |
| 375 | uap->dmarx.chan = chan; |
| 376 | |
Andrew Jackson | 98267d3 | 2014-11-07 14:14:23 +0000 | [diff] [blame] | 377 | uap->dmarx.auto_poll_rate = false; |
Greg Kroah-Hartman | 8f898bf | 2013-12-17 09:33:18 -0800 | [diff] [blame] | 378 | if (plat && plat->dma_rx_poll_enable) { |
Chanho Min | cb06ff1 | 2013-03-27 18:38:11 +0900 | [diff] [blame] | 379 | /* Set poll rate if specified. */ |
| 380 | if (plat->dma_rx_poll_rate) { |
| 381 | uap->dmarx.auto_poll_rate = false; |
| 382 | uap->dmarx.poll_rate = plat->dma_rx_poll_rate; |
| 383 | } else { |
| 384 | /* |
| 385 | * 100 ms defaults to poll rate if not |
| 386 | * specified. This will be adjusted with |
| 387 | * the baud rate at set_termios. |
| 388 | */ |
| 389 | uap->dmarx.auto_poll_rate = true; |
| 390 | uap->dmarx.poll_rate = 100; |
| 391 | } |
| 392 | /* 3 secs defaults poll_timeout if not specified. */ |
| 393 | if (plat->dma_rx_poll_timeout) |
| 394 | uap->dmarx.poll_timeout = |
| 395 | plat->dma_rx_poll_timeout; |
| 396 | else |
| 397 | uap->dmarx.poll_timeout = 3000; |
Andrew Jackson | 98267d3 | 2014-11-07 14:14:23 +0000 | [diff] [blame] | 398 | } else if (!plat && dev->of_node) { |
| 399 | uap->dmarx.auto_poll_rate = of_property_read_bool( |
| 400 | dev->of_node, "auto-poll"); |
| 401 | if (uap->dmarx.auto_poll_rate) { |
| 402 | u32 x; |
Chanho Min | cb06ff1 | 2013-03-27 18:38:11 +0900 | [diff] [blame] | 403 | |
Andrew Jackson | 98267d3 | 2014-11-07 14:14:23 +0000 | [diff] [blame] | 404 | if (0 == of_property_read_u32(dev->of_node, |
| 405 | "poll-rate-ms", &x)) |
| 406 | uap->dmarx.poll_rate = x; |
| 407 | else |
| 408 | uap->dmarx.poll_rate = 100; |
| 409 | if (0 == of_property_read_u32(dev->of_node, |
| 410 | "poll-timeout-ms", &x)) |
| 411 | uap->dmarx.poll_timeout = x; |
| 412 | else |
| 413 | uap->dmarx.poll_timeout = 3000; |
| 414 | } |
| 415 | } |
Linus Walleij | ead76f32 | 2011-02-24 13:21:08 +0100 | [diff] [blame] | 416 | dev_info(uap->port.dev, "DMA channel RX %s\n", |
| 417 | dma_chan_name(uap->dmarx.chan)); |
| 418 | } |
Russell King | 68b65f7 | 2010-12-22 17:24:39 +0000 | [diff] [blame] | 419 | } |
| 420 | |
Russell King | 68b65f7 | 2010-12-22 17:24:39 +0000 | [diff] [blame] | 421 | static void pl011_dma_remove(struct uart_amba_port *uap) |
| 422 | { |
Russell King | 68b65f7 | 2010-12-22 17:24:39 +0000 | [diff] [blame] | 423 | if (uap->dmatx.chan) |
| 424 | dma_release_channel(uap->dmatx.chan); |
Linus Walleij | ead76f32 | 2011-02-24 13:21:08 +0100 | [diff] [blame] | 425 | if (uap->dmarx.chan) |
| 426 | dma_release_channel(uap->dmarx.chan); |
Russell King | 68b65f7 | 2010-12-22 17:24:39 +0000 | [diff] [blame] | 427 | } |
| 428 | |
Dave Martin | 734745c | 2015-03-04 12:27:33 +0000 | [diff] [blame] | 429 | /* Forward declare these for the refill routine */ |
Russell King | 68b65f7 | 2010-12-22 17:24:39 +0000 | [diff] [blame] | 430 | static int pl011_dma_tx_refill(struct uart_amba_port *uap); |
Dave Martin | 734745c | 2015-03-04 12:27:33 +0000 | [diff] [blame] | 431 | static void pl011_start_tx_pio(struct uart_amba_port *uap); |
Russell King | 68b65f7 | 2010-12-22 17:24:39 +0000 | [diff] [blame] | 432 | |
| 433 | /* |
| 434 | * The current DMA TX buffer has been sent. |
| 435 | * Try to queue up another DMA buffer. |
| 436 | */ |
| 437 | static void pl011_dma_tx_callback(void *data) |
| 438 | { |
| 439 | struct uart_amba_port *uap = data; |
| 440 | struct pl011_dmatx_data *dmatx = &uap->dmatx; |
| 441 | unsigned long flags; |
| 442 | u16 dmacr; |
| 443 | |
| 444 | spin_lock_irqsave(&uap->port.lock, flags); |
| 445 | if (uap->dmatx.queued) |
| 446 | dma_unmap_sg(dmatx->chan->device->dev, &dmatx->sg, 1, |
| 447 | DMA_TO_DEVICE); |
| 448 | |
| 449 | dmacr = uap->dmacr; |
| 450 | uap->dmacr = dmacr & ~UART011_TXDMAE; |
Russell King | 7583633 | 2015-11-03 14:50:58 +0000 | [diff] [blame^] | 451 | pl011_write(uap->dmacr, uap->port.membase, UART011_DMACR); |
Russell King | 68b65f7 | 2010-12-22 17:24:39 +0000 | [diff] [blame] | 452 | |
| 453 | /* |
| 454 | * If TX DMA was disabled, it means that we've stopped the DMA for |
| 455 | * some reason (eg, XOFF received, or we want to send an X-char.) |
| 456 | * |
| 457 | * Note: we need to be careful here of a potential race between DMA |
| 458 | * and the rest of the driver - if the driver disables TX DMA while |
| 459 | * a TX buffer completing, we must update the tx queued status to |
| 460 | * get further refills (hence we check dmacr). |
| 461 | */ |
| 462 | if (!(dmacr & UART011_TXDMAE) || uart_tx_stopped(&uap->port) || |
| 463 | uart_circ_empty(&uap->port.state->xmit)) { |
| 464 | uap->dmatx.queued = false; |
| 465 | spin_unlock_irqrestore(&uap->port.lock, flags); |
| 466 | return; |
| 467 | } |
| 468 | |
Dave Martin | 734745c | 2015-03-04 12:27:33 +0000 | [diff] [blame] | 469 | if (pl011_dma_tx_refill(uap) <= 0) |
Russell King | 68b65f7 | 2010-12-22 17:24:39 +0000 | [diff] [blame] | 470 | /* |
| 471 | * We didn't queue a DMA buffer for some reason, but we |
| 472 | * have data pending to be sent. Re-enable the TX IRQ. |
| 473 | */ |
Dave Martin | 734745c | 2015-03-04 12:27:33 +0000 | [diff] [blame] | 474 | pl011_start_tx_pio(uap); |
| 475 | |
Russell King | 68b65f7 | 2010-12-22 17:24:39 +0000 | [diff] [blame] | 476 | spin_unlock_irqrestore(&uap->port.lock, flags); |
| 477 | } |
| 478 | |
| 479 | /* |
| 480 | * Try to refill the TX DMA buffer. |
| 481 | * Locking: called with port lock held and IRQs disabled. |
| 482 | * Returns: |
| 483 | * 1 if we queued up a TX DMA buffer. |
| 484 | * 0 if we didn't want to handle this by DMA |
| 485 | * <0 on error |
| 486 | */ |
| 487 | static int pl011_dma_tx_refill(struct uart_amba_port *uap) |
| 488 | { |
| 489 | struct pl011_dmatx_data *dmatx = &uap->dmatx; |
| 490 | struct dma_chan *chan = dmatx->chan; |
| 491 | struct dma_device *dma_dev = chan->device; |
| 492 | struct dma_async_tx_descriptor *desc; |
| 493 | struct circ_buf *xmit = &uap->port.state->xmit; |
| 494 | unsigned int count; |
| 495 | |
| 496 | /* |
| 497 | * Try to avoid the overhead involved in using DMA if the |
| 498 | * transaction fits in the first half of the FIFO, by using |
| 499 | * the standard interrupt handling. This ensures that we |
| 500 | * issue a uart_write_wakeup() at the appropriate time. |
| 501 | */ |
| 502 | count = uart_circ_chars_pending(xmit); |
| 503 | if (count < (uap->fifosize >> 1)) { |
| 504 | uap->dmatx.queued = false; |
| 505 | return 0; |
| 506 | } |
| 507 | |
| 508 | /* |
| 509 | * Bodge: don't send the last character by DMA, as this |
| 510 | * will prevent XON from notifying us to restart DMA. |
| 511 | */ |
| 512 | count -= 1; |
| 513 | |
| 514 | /* Else proceed to copy the TX chars to the DMA buffer and fire DMA */ |
| 515 | if (count > PL011_DMA_BUFFER_SIZE) |
| 516 | count = PL011_DMA_BUFFER_SIZE; |
| 517 | |
| 518 | if (xmit->tail < xmit->head) |
| 519 | memcpy(&dmatx->buf[0], &xmit->buf[xmit->tail], count); |
| 520 | else { |
| 521 | size_t first = UART_XMIT_SIZE - xmit->tail; |
Andrew Jackson | e2a545a | 2014-11-07 14:14:39 +0000 | [diff] [blame] | 522 | size_t second; |
| 523 | |
| 524 | if (first > count) |
| 525 | first = count; |
| 526 | second = count - first; |
Russell King | 68b65f7 | 2010-12-22 17:24:39 +0000 | [diff] [blame] | 527 | |
| 528 | memcpy(&dmatx->buf[0], &xmit->buf[xmit->tail], first); |
| 529 | if (second) |
| 530 | memcpy(&dmatx->buf[first], &xmit->buf[0], second); |
| 531 | } |
| 532 | |
| 533 | dmatx->sg.length = count; |
| 534 | |
| 535 | if (dma_map_sg(dma_dev->dev, &dmatx->sg, 1, DMA_TO_DEVICE) != 1) { |
| 536 | uap->dmatx.queued = false; |
| 537 | dev_dbg(uap->port.dev, "unable to map TX DMA\n"); |
| 538 | return -EBUSY; |
| 539 | } |
| 540 | |
Alexandre Bounine | 1605282 | 2012-03-08 16:11:18 -0500 | [diff] [blame] | 541 | desc = dmaengine_prep_slave_sg(chan, &dmatx->sg, 1, DMA_MEM_TO_DEV, |
Russell King | 68b65f7 | 2010-12-22 17:24:39 +0000 | [diff] [blame] | 542 | DMA_PREP_INTERRUPT | DMA_CTRL_ACK); |
| 543 | if (!desc) { |
| 544 | dma_unmap_sg(dma_dev->dev, &dmatx->sg, 1, DMA_TO_DEVICE); |
| 545 | uap->dmatx.queued = false; |
| 546 | /* |
| 547 | * If DMA cannot be used right now, we complete this |
| 548 | * transaction via IRQ and let the TTY layer retry. |
| 549 | */ |
| 550 | dev_dbg(uap->port.dev, "TX DMA busy\n"); |
| 551 | return -EBUSY; |
| 552 | } |
| 553 | |
| 554 | /* Some data to go along to the callback */ |
| 555 | desc->callback = pl011_dma_tx_callback; |
| 556 | desc->callback_param = uap; |
| 557 | |
| 558 | /* All errors should happen at prepare time */ |
| 559 | dmaengine_submit(desc); |
| 560 | |
| 561 | /* Fire the DMA transaction */ |
| 562 | dma_dev->device_issue_pending(chan); |
| 563 | |
| 564 | uap->dmacr |= UART011_TXDMAE; |
Russell King | 7583633 | 2015-11-03 14:50:58 +0000 | [diff] [blame^] | 565 | pl011_write(uap->dmacr, uap->port.membase, UART011_DMACR); |
Russell King | 68b65f7 | 2010-12-22 17:24:39 +0000 | [diff] [blame] | 566 | uap->dmatx.queued = true; |
| 567 | |
| 568 | /* |
| 569 | * Now we know that DMA will fire, so advance the ring buffer |
| 570 | * with the stuff we just dispatched. |
| 571 | */ |
| 572 | xmit->tail = (xmit->tail + count) & (UART_XMIT_SIZE - 1); |
| 573 | uap->port.icount.tx += count; |
| 574 | |
| 575 | if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS) |
| 576 | uart_write_wakeup(&uap->port); |
| 577 | |
| 578 | return 1; |
| 579 | } |
| 580 | |
| 581 | /* |
| 582 | * We received a transmit interrupt without a pending X-char but with |
| 583 | * pending characters. |
| 584 | * Locking: called with port lock held and IRQs disabled. |
| 585 | * Returns: |
| 586 | * false if we want to use PIO to transmit |
| 587 | * true if we queued a DMA buffer |
| 588 | */ |
| 589 | static bool pl011_dma_tx_irq(struct uart_amba_port *uap) |
| 590 | { |
Linus Walleij | ead76f32 | 2011-02-24 13:21:08 +0100 | [diff] [blame] | 591 | if (!uap->using_tx_dma) |
Russell King | 68b65f7 | 2010-12-22 17:24:39 +0000 | [diff] [blame] | 592 | return false; |
| 593 | |
| 594 | /* |
| 595 | * If we already have a TX buffer queued, but received a |
| 596 | * TX interrupt, it will be because we've just sent an X-char. |
| 597 | * Ensure the TX DMA is enabled and the TX IRQ is disabled. |
| 598 | */ |
| 599 | if (uap->dmatx.queued) { |
| 600 | uap->dmacr |= UART011_TXDMAE; |
Russell King | 7583633 | 2015-11-03 14:50:58 +0000 | [diff] [blame^] | 601 | pl011_write(uap->dmacr, uap->port.membase, UART011_DMACR); |
Russell King | 68b65f7 | 2010-12-22 17:24:39 +0000 | [diff] [blame] | 602 | uap->im &= ~UART011_TXIM; |
Russell King | 7583633 | 2015-11-03 14:50:58 +0000 | [diff] [blame^] | 603 | pl011_write(uap->im, uap->port.membase, UART011_IMSC); |
Russell King | 68b65f7 | 2010-12-22 17:24:39 +0000 | [diff] [blame] | 604 | return true; |
| 605 | } |
| 606 | |
| 607 | /* |
| 608 | * We don't have a TX buffer queued, so try to queue one. |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 609 | * If we successfully queued a buffer, mask the TX IRQ. |
Russell King | 68b65f7 | 2010-12-22 17:24:39 +0000 | [diff] [blame] | 610 | */ |
| 611 | if (pl011_dma_tx_refill(uap) > 0) { |
| 612 | uap->im &= ~UART011_TXIM; |
Russell King | 7583633 | 2015-11-03 14:50:58 +0000 | [diff] [blame^] | 613 | pl011_write(uap->im, uap->port.membase, UART011_IMSC); |
Russell King | 68b65f7 | 2010-12-22 17:24:39 +0000 | [diff] [blame] | 614 | return true; |
| 615 | } |
| 616 | return false; |
| 617 | } |
| 618 | |
| 619 | /* |
| 620 | * Stop the DMA transmit (eg, due to received XOFF). |
| 621 | * Locking: called with port lock held and IRQs disabled. |
| 622 | */ |
| 623 | static inline void pl011_dma_tx_stop(struct uart_amba_port *uap) |
| 624 | { |
| 625 | if (uap->dmatx.queued) { |
| 626 | uap->dmacr &= ~UART011_TXDMAE; |
Russell King | 7583633 | 2015-11-03 14:50:58 +0000 | [diff] [blame^] | 627 | pl011_write(uap->dmacr, uap->port.membase, UART011_DMACR); |
Russell King | 68b65f7 | 2010-12-22 17:24:39 +0000 | [diff] [blame] | 628 | } |
| 629 | } |
| 630 | |
| 631 | /* |
| 632 | * Try to start a DMA transmit, or in the case of an XON/OFF |
| 633 | * character queued for send, try to get that character out ASAP. |
| 634 | * Locking: called with port lock held and IRQs disabled. |
| 635 | * Returns: |
| 636 | * false if we want the TX IRQ to be enabled |
| 637 | * true if we have a buffer queued |
| 638 | */ |
| 639 | static inline bool pl011_dma_tx_start(struct uart_amba_port *uap) |
| 640 | { |
| 641 | u16 dmacr; |
| 642 | |
Linus Walleij | ead76f32 | 2011-02-24 13:21:08 +0100 | [diff] [blame] | 643 | if (!uap->using_tx_dma) |
Russell King | 68b65f7 | 2010-12-22 17:24:39 +0000 | [diff] [blame] | 644 | return false; |
| 645 | |
| 646 | if (!uap->port.x_char) { |
| 647 | /* no X-char, try to push chars out in DMA mode */ |
| 648 | bool ret = true; |
| 649 | |
| 650 | if (!uap->dmatx.queued) { |
| 651 | if (pl011_dma_tx_refill(uap) > 0) { |
| 652 | uap->im &= ~UART011_TXIM; |
Russell King | 7583633 | 2015-11-03 14:50:58 +0000 | [diff] [blame^] | 653 | pl011_write(uap->im, uap->port.membase, |
| 654 | UART011_IMSC); |
Dave Martin | 734745c | 2015-03-04 12:27:33 +0000 | [diff] [blame] | 655 | } else |
Russell King | 68b65f7 | 2010-12-22 17:24:39 +0000 | [diff] [blame] | 656 | ret = false; |
Russell King | 68b65f7 | 2010-12-22 17:24:39 +0000 | [diff] [blame] | 657 | } else if (!(uap->dmacr & UART011_TXDMAE)) { |
| 658 | uap->dmacr |= UART011_TXDMAE; |
Russell King | 7583633 | 2015-11-03 14:50:58 +0000 | [diff] [blame^] | 659 | pl011_write(uap->dmacr, uap->port.membase, |
| 660 | UART011_DMACR); |
Russell King | 68b65f7 | 2010-12-22 17:24:39 +0000 | [diff] [blame] | 661 | } |
| 662 | return ret; |
| 663 | } |
| 664 | |
| 665 | /* |
| 666 | * We have an X-char to send. Disable DMA to prevent it loading |
| 667 | * the TX fifo, and then see if we can stuff it into the FIFO. |
| 668 | */ |
| 669 | dmacr = uap->dmacr; |
| 670 | uap->dmacr &= ~UART011_TXDMAE; |
Russell King | 7583633 | 2015-11-03 14:50:58 +0000 | [diff] [blame^] | 671 | pl011_write(uap->dmacr, uap->port.membase, UART011_DMACR); |
Russell King | 68b65f7 | 2010-12-22 17:24:39 +0000 | [diff] [blame] | 672 | |
Russell King | 7583633 | 2015-11-03 14:50:58 +0000 | [diff] [blame^] | 673 | if (pl011_read(uap->port.membase, UART01x_FR) & UART01x_FR_TXFF) { |
Russell King | 68b65f7 | 2010-12-22 17:24:39 +0000 | [diff] [blame] | 674 | /* |
| 675 | * No space in the FIFO, so enable the transmit interrupt |
| 676 | * so we know when there is space. Note that once we've |
| 677 | * loaded the character, we should just re-enable DMA. |
| 678 | */ |
| 679 | return false; |
| 680 | } |
| 681 | |
Russell King | 7583633 | 2015-11-03 14:50:58 +0000 | [diff] [blame^] | 682 | pl011_write(uap->port.x_char, uap->port.membase, UART01x_DR); |
Russell King | 68b65f7 | 2010-12-22 17:24:39 +0000 | [diff] [blame] | 683 | uap->port.icount.tx++; |
| 684 | uap->port.x_char = 0; |
| 685 | |
| 686 | /* Success - restore the DMA state */ |
| 687 | uap->dmacr = dmacr; |
Russell King | 7583633 | 2015-11-03 14:50:58 +0000 | [diff] [blame^] | 688 | pl011_write(dmacr, uap->port.membase, UART011_DMACR); |
Russell King | 68b65f7 | 2010-12-22 17:24:39 +0000 | [diff] [blame] | 689 | |
| 690 | return true; |
| 691 | } |
| 692 | |
| 693 | /* |
| 694 | * Flush the transmit buffer. |
| 695 | * Locking: called with port lock held and IRQs disabled. |
| 696 | */ |
| 697 | static void pl011_dma_flush_buffer(struct uart_port *port) |
Fabio Estevam | b83286b | 2013-08-09 17:58:51 -0300 | [diff] [blame] | 698 | __releases(&uap->port.lock) |
| 699 | __acquires(&uap->port.lock) |
Russell King | 68b65f7 | 2010-12-22 17:24:39 +0000 | [diff] [blame] | 700 | { |
Daniel Thompson | a5820c2 | 2014-09-03 12:51:55 +0100 | [diff] [blame] | 701 | struct uart_amba_port *uap = |
| 702 | container_of(port, struct uart_amba_port, port); |
Russell King | 68b65f7 | 2010-12-22 17:24:39 +0000 | [diff] [blame] | 703 | |
Linus Walleij | ead76f32 | 2011-02-24 13:21:08 +0100 | [diff] [blame] | 704 | if (!uap->using_tx_dma) |
Russell King | 68b65f7 | 2010-12-22 17:24:39 +0000 | [diff] [blame] | 705 | return; |
| 706 | |
| 707 | /* Avoid deadlock with the DMA engine callback */ |
| 708 | spin_unlock(&uap->port.lock); |
| 709 | dmaengine_terminate_all(uap->dmatx.chan); |
| 710 | spin_lock(&uap->port.lock); |
| 711 | if (uap->dmatx.queued) { |
| 712 | dma_unmap_sg(uap->dmatx.chan->device->dev, &uap->dmatx.sg, 1, |
| 713 | DMA_TO_DEVICE); |
| 714 | uap->dmatx.queued = false; |
| 715 | uap->dmacr &= ~UART011_TXDMAE; |
Russell King | 7583633 | 2015-11-03 14:50:58 +0000 | [diff] [blame^] | 716 | pl011_write(uap->dmacr, uap->port.membase, UART011_DMACR); |
Russell King | 68b65f7 | 2010-12-22 17:24:39 +0000 | [diff] [blame] | 717 | } |
| 718 | } |
| 719 | |
Linus Walleij | ead76f32 | 2011-02-24 13:21:08 +0100 | [diff] [blame] | 720 | static void pl011_dma_rx_callback(void *data); |
| 721 | |
| 722 | static int pl011_dma_rx_trigger_dma(struct uart_amba_port *uap) |
| 723 | { |
| 724 | struct dma_chan *rxchan = uap->dmarx.chan; |
Linus Walleij | ead76f32 | 2011-02-24 13:21:08 +0100 | [diff] [blame] | 725 | struct pl011_dmarx_data *dmarx = &uap->dmarx; |
| 726 | struct dma_async_tx_descriptor *desc; |
| 727 | struct pl011_sgbuf *sgbuf; |
| 728 | |
| 729 | if (!rxchan) |
| 730 | return -EIO; |
| 731 | |
| 732 | /* Start the RX DMA job */ |
| 733 | sgbuf = uap->dmarx.use_buf_b ? |
| 734 | &uap->dmarx.sgbuf_b : &uap->dmarx.sgbuf_a; |
Alexandre Bounine | 1605282 | 2012-03-08 16:11:18 -0500 | [diff] [blame] | 735 | desc = dmaengine_prep_slave_sg(rxchan, &sgbuf->sg, 1, |
Vinod Koul | a485df4 | 2011-10-14 10:47:38 +0530 | [diff] [blame] | 736 | DMA_DEV_TO_MEM, |
Linus Walleij | ead76f32 | 2011-02-24 13:21:08 +0100 | [diff] [blame] | 737 | DMA_PREP_INTERRUPT | DMA_CTRL_ACK); |
| 738 | /* |
| 739 | * If the DMA engine is busy and cannot prepare a |
| 740 | * channel, no big deal, the driver will fall back |
| 741 | * to interrupt mode as a result of this error code. |
| 742 | */ |
| 743 | if (!desc) { |
| 744 | uap->dmarx.running = false; |
| 745 | dmaengine_terminate_all(rxchan); |
| 746 | return -EBUSY; |
| 747 | } |
| 748 | |
| 749 | /* Some data to go along to the callback */ |
| 750 | desc->callback = pl011_dma_rx_callback; |
| 751 | desc->callback_param = uap; |
| 752 | dmarx->cookie = dmaengine_submit(desc); |
| 753 | dma_async_issue_pending(rxchan); |
| 754 | |
| 755 | uap->dmacr |= UART011_RXDMAE; |
Russell King | 7583633 | 2015-11-03 14:50:58 +0000 | [diff] [blame^] | 756 | pl011_write(uap->dmacr, uap->port.membase, UART011_DMACR); |
Linus Walleij | ead76f32 | 2011-02-24 13:21:08 +0100 | [diff] [blame] | 757 | uap->dmarx.running = true; |
| 758 | |
| 759 | uap->im &= ~UART011_RXIM; |
Russell King | 7583633 | 2015-11-03 14:50:58 +0000 | [diff] [blame^] | 760 | pl011_write(uap->im, uap->port.membase, UART011_IMSC); |
Linus Walleij | ead76f32 | 2011-02-24 13:21:08 +0100 | [diff] [blame] | 761 | |
| 762 | return 0; |
| 763 | } |
| 764 | |
| 765 | /* |
| 766 | * This is called when either the DMA job is complete, or |
| 767 | * the FIFO timeout interrupt occurred. This must be called |
| 768 | * with the port spinlock uap->port.lock held. |
| 769 | */ |
| 770 | static void pl011_dma_rx_chars(struct uart_amba_port *uap, |
| 771 | u32 pending, bool use_buf_b, |
| 772 | bool readfifo) |
| 773 | { |
Jiri Slaby | 05c7cd3 | 2013-01-03 15:53:04 +0100 | [diff] [blame] | 774 | struct tty_port *port = &uap->port.state->port; |
Linus Walleij | ead76f32 | 2011-02-24 13:21:08 +0100 | [diff] [blame] | 775 | struct pl011_sgbuf *sgbuf = use_buf_b ? |
| 776 | &uap->dmarx.sgbuf_b : &uap->dmarx.sgbuf_a; |
Linus Walleij | ead76f32 | 2011-02-24 13:21:08 +0100 | [diff] [blame] | 777 | int dma_count = 0; |
| 778 | u32 fifotaken = 0; /* only used for vdbg() */ |
| 779 | |
Chanho Min | cb06ff1 | 2013-03-27 18:38:11 +0900 | [diff] [blame] | 780 | struct pl011_dmarx_data *dmarx = &uap->dmarx; |
| 781 | int dmataken = 0; |
| 782 | |
| 783 | if (uap->dmarx.poll_rate) { |
| 784 | /* The data can be taken by polling */ |
| 785 | dmataken = sgbuf->sg.length - dmarx->last_residue; |
| 786 | /* Recalculate the pending size */ |
| 787 | if (pending >= dmataken) |
| 788 | pending -= dmataken; |
| 789 | } |
| 790 | |
| 791 | /* Pick the remain data from the DMA */ |
Linus Walleij | ead76f32 | 2011-02-24 13:21:08 +0100 | [diff] [blame] | 792 | if (pending) { |
Linus Walleij | ead76f32 | 2011-02-24 13:21:08 +0100 | [diff] [blame] | 793 | |
| 794 | /* |
| 795 | * First take all chars in the DMA pipe, then look in the FIFO. |
| 796 | * Note that tty_insert_flip_buf() tries to take as many chars |
| 797 | * as it can. |
| 798 | */ |
Chanho Min | cb06ff1 | 2013-03-27 18:38:11 +0900 | [diff] [blame] | 799 | dma_count = tty_insert_flip_string(port, sgbuf->buf + dmataken, |
| 800 | pending); |
Linus Walleij | ead76f32 | 2011-02-24 13:21:08 +0100 | [diff] [blame] | 801 | |
| 802 | uap->port.icount.rx += dma_count; |
| 803 | if (dma_count < pending) |
| 804 | dev_warn(uap->port.dev, |
| 805 | "couldn't insert all characters (TTY is full?)\n"); |
| 806 | } |
| 807 | |
Chanho Min | cb06ff1 | 2013-03-27 18:38:11 +0900 | [diff] [blame] | 808 | /* Reset the last_residue for Rx DMA poll */ |
| 809 | if (uap->dmarx.poll_rate) |
| 810 | dmarx->last_residue = sgbuf->sg.length; |
| 811 | |
Linus Walleij | ead76f32 | 2011-02-24 13:21:08 +0100 | [diff] [blame] | 812 | /* |
| 813 | * Only continue with trying to read the FIFO if all DMA chars have |
| 814 | * been taken first. |
| 815 | */ |
| 816 | if (dma_count == pending && readfifo) { |
| 817 | /* Clear any error flags */ |
Russell King | 7583633 | 2015-11-03 14:50:58 +0000 | [diff] [blame^] | 818 | pl011_write(UART011_OEIS | UART011_BEIS | UART011_PEIS | |
| 819 | UART011_FEIS, uap->port.membase, UART011_ICR); |
Linus Walleij | ead76f32 | 2011-02-24 13:21:08 +0100 | [diff] [blame] | 820 | |
| 821 | /* |
| 822 | * If we read all the DMA'd characters, and we had an |
Linus Walleij | 29772c4 | 2011-02-24 13:21:36 +0100 | [diff] [blame] | 823 | * incomplete buffer, that could be due to an rx error, or |
| 824 | * maybe we just timed out. Read any pending chars and check |
| 825 | * the error status. |
| 826 | * |
| 827 | * Error conditions will only occur in the FIFO, these will |
| 828 | * trigger an immediate interrupt and stop the DMA job, so we |
| 829 | * will always find the error in the FIFO, never in the DMA |
| 830 | * buffer. |
Linus Walleij | ead76f32 | 2011-02-24 13:21:08 +0100 | [diff] [blame] | 831 | */ |
Linus Walleij | 29772c4 | 2011-02-24 13:21:36 +0100 | [diff] [blame] | 832 | fifotaken = pl011_fifo_to_tty(uap); |
Linus Walleij | ead76f32 | 2011-02-24 13:21:08 +0100 | [diff] [blame] | 833 | } |
| 834 | |
| 835 | spin_unlock(&uap->port.lock); |
| 836 | dev_vdbg(uap->port.dev, |
| 837 | "Took %d chars from DMA buffer and %d chars from the FIFO\n", |
| 838 | dma_count, fifotaken); |
Jiri Slaby | 2e124b4 | 2013-01-03 15:53:06 +0100 | [diff] [blame] | 839 | tty_flip_buffer_push(port); |
Linus Walleij | ead76f32 | 2011-02-24 13:21:08 +0100 | [diff] [blame] | 840 | spin_lock(&uap->port.lock); |
| 841 | } |
| 842 | |
| 843 | static void pl011_dma_rx_irq(struct uart_amba_port *uap) |
| 844 | { |
| 845 | struct pl011_dmarx_data *dmarx = &uap->dmarx; |
| 846 | struct dma_chan *rxchan = dmarx->chan; |
| 847 | struct pl011_sgbuf *sgbuf = dmarx->use_buf_b ? |
| 848 | &dmarx->sgbuf_b : &dmarx->sgbuf_a; |
| 849 | size_t pending; |
| 850 | struct dma_tx_state state; |
| 851 | enum dma_status dmastat; |
| 852 | |
| 853 | /* |
| 854 | * Pause the transfer so we can trust the current counter, |
| 855 | * do this before we pause the PL011 block, else we may |
| 856 | * overflow the FIFO. |
| 857 | */ |
| 858 | if (dmaengine_pause(rxchan)) |
| 859 | dev_err(uap->port.dev, "unable to pause DMA transfer\n"); |
| 860 | dmastat = rxchan->device->device_tx_status(rxchan, |
| 861 | dmarx->cookie, &state); |
| 862 | if (dmastat != DMA_PAUSED) |
| 863 | dev_err(uap->port.dev, "unable to pause DMA transfer\n"); |
| 864 | |
| 865 | /* Disable RX DMA - incoming data will wait in the FIFO */ |
| 866 | uap->dmacr &= ~UART011_RXDMAE; |
Russell King | 7583633 | 2015-11-03 14:50:58 +0000 | [diff] [blame^] | 867 | pl011_write(uap->dmacr, uap->port.membase, UART011_DMACR); |
Linus Walleij | ead76f32 | 2011-02-24 13:21:08 +0100 | [diff] [blame] | 868 | uap->dmarx.running = false; |
| 869 | |
| 870 | pending = sgbuf->sg.length - state.residue; |
| 871 | BUG_ON(pending > PL011_DMA_BUFFER_SIZE); |
| 872 | /* Then we terminate the transfer - we now know our residue */ |
| 873 | dmaengine_terminate_all(rxchan); |
| 874 | |
| 875 | /* |
| 876 | * This will take the chars we have so far and insert |
| 877 | * into the framework. |
| 878 | */ |
| 879 | pl011_dma_rx_chars(uap, pending, dmarx->use_buf_b, true); |
| 880 | |
| 881 | /* Switch buffer & re-trigger DMA job */ |
| 882 | dmarx->use_buf_b = !dmarx->use_buf_b; |
| 883 | if (pl011_dma_rx_trigger_dma(uap)) { |
| 884 | dev_dbg(uap->port.dev, "could not retrigger RX DMA job " |
| 885 | "fall back to interrupt mode\n"); |
| 886 | uap->im |= UART011_RXIM; |
Russell King | 7583633 | 2015-11-03 14:50:58 +0000 | [diff] [blame^] | 887 | pl011_write(uap->im, uap->port.membase, UART011_IMSC); |
Linus Walleij | ead76f32 | 2011-02-24 13:21:08 +0100 | [diff] [blame] | 888 | } |
| 889 | } |
| 890 | |
| 891 | static void pl011_dma_rx_callback(void *data) |
| 892 | { |
| 893 | struct uart_amba_port *uap = data; |
| 894 | struct pl011_dmarx_data *dmarx = &uap->dmarx; |
Chanho Min | 6dc01aa | 2012-02-20 10:24:40 +0900 | [diff] [blame] | 895 | struct dma_chan *rxchan = dmarx->chan; |
Linus Walleij | ead76f32 | 2011-02-24 13:21:08 +0100 | [diff] [blame] | 896 | bool lastbuf = dmarx->use_buf_b; |
Chanho Min | 6dc01aa | 2012-02-20 10:24:40 +0900 | [diff] [blame] | 897 | struct pl011_sgbuf *sgbuf = dmarx->use_buf_b ? |
| 898 | &dmarx->sgbuf_b : &dmarx->sgbuf_a; |
| 899 | size_t pending; |
| 900 | struct dma_tx_state state; |
Linus Walleij | ead76f32 | 2011-02-24 13:21:08 +0100 | [diff] [blame] | 901 | int ret; |
| 902 | |
| 903 | /* |
| 904 | * This completion interrupt occurs typically when the |
| 905 | * RX buffer is totally stuffed but no timeout has yet |
| 906 | * occurred. When that happens, we just want the RX |
| 907 | * routine to flush out the secondary DMA buffer while |
| 908 | * we immediately trigger the next DMA job. |
| 909 | */ |
| 910 | spin_lock_irq(&uap->port.lock); |
Chanho Min | 6dc01aa | 2012-02-20 10:24:40 +0900 | [diff] [blame] | 911 | /* |
| 912 | * Rx data can be taken by the UART interrupts during |
| 913 | * the DMA irq handler. So we check the residue here. |
| 914 | */ |
| 915 | rxchan->device->device_tx_status(rxchan, dmarx->cookie, &state); |
| 916 | pending = sgbuf->sg.length - state.residue; |
| 917 | BUG_ON(pending > PL011_DMA_BUFFER_SIZE); |
| 918 | /* Then we terminate the transfer - we now know our residue */ |
| 919 | dmaengine_terminate_all(rxchan); |
| 920 | |
Linus Walleij | ead76f32 | 2011-02-24 13:21:08 +0100 | [diff] [blame] | 921 | uap->dmarx.running = false; |
| 922 | dmarx->use_buf_b = !lastbuf; |
| 923 | ret = pl011_dma_rx_trigger_dma(uap); |
| 924 | |
Chanho Min | 6dc01aa | 2012-02-20 10:24:40 +0900 | [diff] [blame] | 925 | pl011_dma_rx_chars(uap, pending, lastbuf, false); |
Linus Walleij | ead76f32 | 2011-02-24 13:21:08 +0100 | [diff] [blame] | 926 | spin_unlock_irq(&uap->port.lock); |
| 927 | /* |
| 928 | * Do this check after we picked the DMA chars so we don't |
| 929 | * get some IRQ immediately from RX. |
| 930 | */ |
| 931 | if (ret) { |
| 932 | dev_dbg(uap->port.dev, "could not retrigger RX DMA job " |
| 933 | "fall back to interrupt mode\n"); |
| 934 | uap->im |= UART011_RXIM; |
Russell King | 7583633 | 2015-11-03 14:50:58 +0000 | [diff] [blame^] | 935 | pl011_write(uap->im, uap->port.membase, UART011_IMSC); |
Linus Walleij | ead76f32 | 2011-02-24 13:21:08 +0100 | [diff] [blame] | 936 | } |
| 937 | } |
| 938 | |
| 939 | /* |
| 940 | * Stop accepting received characters, when we're shutting down or |
| 941 | * suspending this port. |
| 942 | * Locking: called with port lock held and IRQs disabled. |
| 943 | */ |
| 944 | static inline void pl011_dma_rx_stop(struct uart_amba_port *uap) |
| 945 | { |
| 946 | /* FIXME. Just disable the DMA enable */ |
| 947 | uap->dmacr &= ~UART011_RXDMAE; |
Russell King | 7583633 | 2015-11-03 14:50:58 +0000 | [diff] [blame^] | 948 | pl011_write(uap->dmacr, uap->port.membase, UART011_DMACR); |
Linus Walleij | ead76f32 | 2011-02-24 13:21:08 +0100 | [diff] [blame] | 949 | } |
Russell King | 68b65f7 | 2010-12-22 17:24:39 +0000 | [diff] [blame] | 950 | |
Chanho Min | cb06ff1 | 2013-03-27 18:38:11 +0900 | [diff] [blame] | 951 | /* |
| 952 | * Timer handler for Rx DMA polling. |
| 953 | * Every polling, It checks the residue in the dma buffer and transfer |
| 954 | * data to the tty. Also, last_residue is updated for the next polling. |
| 955 | */ |
| 956 | static void pl011_dma_rx_poll(unsigned long args) |
| 957 | { |
| 958 | struct uart_amba_port *uap = (struct uart_amba_port *)args; |
| 959 | struct tty_port *port = &uap->port.state->port; |
| 960 | struct pl011_dmarx_data *dmarx = &uap->dmarx; |
| 961 | struct dma_chan *rxchan = uap->dmarx.chan; |
| 962 | unsigned long flags = 0; |
| 963 | unsigned int dmataken = 0; |
| 964 | unsigned int size = 0; |
| 965 | struct pl011_sgbuf *sgbuf; |
| 966 | int dma_count; |
| 967 | struct dma_tx_state state; |
| 968 | |
| 969 | sgbuf = dmarx->use_buf_b ? &uap->dmarx.sgbuf_b : &uap->dmarx.sgbuf_a; |
| 970 | rxchan->device->device_tx_status(rxchan, dmarx->cookie, &state); |
| 971 | if (likely(state.residue < dmarx->last_residue)) { |
| 972 | dmataken = sgbuf->sg.length - dmarx->last_residue; |
| 973 | size = dmarx->last_residue - state.residue; |
| 974 | dma_count = tty_insert_flip_string(port, sgbuf->buf + dmataken, |
| 975 | size); |
| 976 | if (dma_count == size) |
| 977 | dmarx->last_residue = state.residue; |
| 978 | dmarx->last_jiffies = jiffies; |
| 979 | } |
| 980 | tty_flip_buffer_push(port); |
| 981 | |
| 982 | /* |
| 983 | * If no data is received in poll_timeout, the driver will fall back |
| 984 | * to interrupt mode. We will retrigger DMA at the first interrupt. |
| 985 | */ |
| 986 | if (jiffies_to_msecs(jiffies - dmarx->last_jiffies) |
| 987 | > uap->dmarx.poll_timeout) { |
| 988 | |
| 989 | spin_lock_irqsave(&uap->port.lock, flags); |
| 990 | pl011_dma_rx_stop(uap); |
Guennadi Liakhovetski | c25a1ad | 2013-12-10 14:54:47 +0100 | [diff] [blame] | 991 | uap->im |= UART011_RXIM; |
Russell King | 7583633 | 2015-11-03 14:50:58 +0000 | [diff] [blame^] | 992 | pl011_write(uap->im, uap->port.membase, UART011_IMSC); |
Chanho Min | cb06ff1 | 2013-03-27 18:38:11 +0900 | [diff] [blame] | 993 | spin_unlock_irqrestore(&uap->port.lock, flags); |
| 994 | |
| 995 | uap->dmarx.running = false; |
| 996 | dmaengine_terminate_all(rxchan); |
| 997 | del_timer(&uap->dmarx.timer); |
| 998 | } else { |
| 999 | mod_timer(&uap->dmarx.timer, |
| 1000 | jiffies + msecs_to_jiffies(uap->dmarx.poll_rate)); |
| 1001 | } |
| 1002 | } |
| 1003 | |
Russell King | 68b65f7 | 2010-12-22 17:24:39 +0000 | [diff] [blame] | 1004 | static void pl011_dma_startup(struct uart_amba_port *uap) |
| 1005 | { |
Linus Walleij | ead76f32 | 2011-02-24 13:21:08 +0100 | [diff] [blame] | 1006 | int ret; |
| 1007 | |
Jorge Ramirez-Ortiz | 1c9be31 | 2015-03-06 13:05:40 -0500 | [diff] [blame] | 1008 | if (!uap->dma_probed) |
| 1009 | pl011_dma_probe(uap); |
| 1010 | |
Russell King | 68b65f7 | 2010-12-22 17:24:39 +0000 | [diff] [blame] | 1011 | if (!uap->dmatx.chan) |
| 1012 | return; |
| 1013 | |
Andrew Jackson | 4c0be45 | 2014-11-07 14:14:35 +0000 | [diff] [blame] | 1014 | uap->dmatx.buf = kmalloc(PL011_DMA_BUFFER_SIZE, GFP_KERNEL | __GFP_DMA); |
Russell King | 68b65f7 | 2010-12-22 17:24:39 +0000 | [diff] [blame] | 1015 | if (!uap->dmatx.buf) { |
| 1016 | dev_err(uap->port.dev, "no memory for DMA TX buffer\n"); |
| 1017 | uap->port.fifosize = uap->fifosize; |
| 1018 | return; |
| 1019 | } |
| 1020 | |
| 1021 | sg_init_one(&uap->dmatx.sg, uap->dmatx.buf, PL011_DMA_BUFFER_SIZE); |
| 1022 | |
| 1023 | /* The DMA buffer is now the FIFO the TTY subsystem can use */ |
| 1024 | uap->port.fifosize = PL011_DMA_BUFFER_SIZE; |
Linus Walleij | ead76f32 | 2011-02-24 13:21:08 +0100 | [diff] [blame] | 1025 | uap->using_tx_dma = true; |
Russell King | 68b65f7 | 2010-12-22 17:24:39 +0000 | [diff] [blame] | 1026 | |
Linus Walleij | ead76f32 | 2011-02-24 13:21:08 +0100 | [diff] [blame] | 1027 | if (!uap->dmarx.chan) |
| 1028 | goto skip_rx; |
| 1029 | |
| 1030 | /* Allocate and map DMA RX buffers */ |
| 1031 | ret = pl011_sgbuf_init(uap->dmarx.chan, &uap->dmarx.sgbuf_a, |
| 1032 | DMA_FROM_DEVICE); |
| 1033 | if (ret) { |
| 1034 | dev_err(uap->port.dev, "failed to init DMA %s: %d\n", |
| 1035 | "RX buffer A", ret); |
| 1036 | goto skip_rx; |
| 1037 | } |
| 1038 | |
| 1039 | ret = pl011_sgbuf_init(uap->dmarx.chan, &uap->dmarx.sgbuf_b, |
| 1040 | DMA_FROM_DEVICE); |
| 1041 | if (ret) { |
| 1042 | dev_err(uap->port.dev, "failed to init DMA %s: %d\n", |
| 1043 | "RX buffer B", ret); |
| 1044 | pl011_sgbuf_free(uap->dmarx.chan, &uap->dmarx.sgbuf_a, |
| 1045 | DMA_FROM_DEVICE); |
| 1046 | goto skip_rx; |
| 1047 | } |
| 1048 | |
| 1049 | uap->using_rx_dma = true; |
| 1050 | |
| 1051 | skip_rx: |
Russell King | 68b65f7 | 2010-12-22 17:24:39 +0000 | [diff] [blame] | 1052 | /* Turn on DMA error (RX/TX will be enabled on demand) */ |
| 1053 | uap->dmacr |= UART011_DMAONERR; |
Russell King | 7583633 | 2015-11-03 14:50:58 +0000 | [diff] [blame^] | 1054 | pl011_write(uap->dmacr, uap->port.membase, UART011_DMACR); |
Russell King | 38d6243 | 2010-12-22 17:59:16 +0000 | [diff] [blame] | 1055 | |
| 1056 | /* |
| 1057 | * ST Micro variants has some specific dma burst threshold |
| 1058 | * compensation. Set this to 16 bytes, so burst will only |
| 1059 | * be issued above/below 16 bytes. |
| 1060 | */ |
| 1061 | if (uap->vendor->dma_threshold) |
Russell King | 7583633 | 2015-11-03 14:50:58 +0000 | [diff] [blame^] | 1062 | pl011_write(ST_UART011_DMAWM_RX_16 | ST_UART011_DMAWM_TX_16, |
| 1063 | uap->port.membase, ST_UART011_DMAWM); |
Linus Walleij | ead76f32 | 2011-02-24 13:21:08 +0100 | [diff] [blame] | 1064 | |
| 1065 | if (uap->using_rx_dma) { |
| 1066 | if (pl011_dma_rx_trigger_dma(uap)) |
| 1067 | dev_dbg(uap->port.dev, "could not trigger initial " |
| 1068 | "RX DMA job, fall back to interrupt mode\n"); |
Chanho Min | cb06ff1 | 2013-03-27 18:38:11 +0900 | [diff] [blame] | 1069 | if (uap->dmarx.poll_rate) { |
| 1070 | init_timer(&(uap->dmarx.timer)); |
| 1071 | uap->dmarx.timer.function = pl011_dma_rx_poll; |
| 1072 | uap->dmarx.timer.data = (unsigned long)uap; |
| 1073 | mod_timer(&uap->dmarx.timer, |
| 1074 | jiffies + |
| 1075 | msecs_to_jiffies(uap->dmarx.poll_rate)); |
| 1076 | uap->dmarx.last_residue = PL011_DMA_BUFFER_SIZE; |
| 1077 | uap->dmarx.last_jiffies = jiffies; |
| 1078 | } |
Linus Walleij | ead76f32 | 2011-02-24 13:21:08 +0100 | [diff] [blame] | 1079 | } |
Russell King | 68b65f7 | 2010-12-22 17:24:39 +0000 | [diff] [blame] | 1080 | } |
| 1081 | |
| 1082 | static void pl011_dma_shutdown(struct uart_amba_port *uap) |
| 1083 | { |
Linus Walleij | ead76f32 | 2011-02-24 13:21:08 +0100 | [diff] [blame] | 1084 | if (!(uap->using_tx_dma || uap->using_rx_dma)) |
Russell King | 68b65f7 | 2010-12-22 17:24:39 +0000 | [diff] [blame] | 1085 | return; |
| 1086 | |
| 1087 | /* Disable RX and TX DMA */ |
Russell King | 7583633 | 2015-11-03 14:50:58 +0000 | [diff] [blame^] | 1088 | while (pl011_read(uap->port.membase, UART01x_FR) & UART01x_FR_BUSY) |
Russell King | 68b65f7 | 2010-12-22 17:24:39 +0000 | [diff] [blame] | 1089 | barrier(); |
| 1090 | |
| 1091 | spin_lock_irq(&uap->port.lock); |
| 1092 | uap->dmacr &= ~(UART011_DMAONERR | UART011_RXDMAE | UART011_TXDMAE); |
Russell King | 7583633 | 2015-11-03 14:50:58 +0000 | [diff] [blame^] | 1093 | pl011_write(uap->dmacr, uap->port.membase, UART011_DMACR); |
Russell King | 68b65f7 | 2010-12-22 17:24:39 +0000 | [diff] [blame] | 1094 | spin_unlock_irq(&uap->port.lock); |
| 1095 | |
Linus Walleij | ead76f32 | 2011-02-24 13:21:08 +0100 | [diff] [blame] | 1096 | if (uap->using_tx_dma) { |
| 1097 | /* In theory, this should already be done by pl011_dma_flush_buffer */ |
| 1098 | dmaengine_terminate_all(uap->dmatx.chan); |
| 1099 | if (uap->dmatx.queued) { |
| 1100 | dma_unmap_sg(uap->dmatx.chan->device->dev, &uap->dmatx.sg, 1, |
| 1101 | DMA_TO_DEVICE); |
| 1102 | uap->dmatx.queued = false; |
| 1103 | } |
| 1104 | |
| 1105 | kfree(uap->dmatx.buf); |
| 1106 | uap->using_tx_dma = false; |
Russell King | 68b65f7 | 2010-12-22 17:24:39 +0000 | [diff] [blame] | 1107 | } |
| 1108 | |
Linus Walleij | ead76f32 | 2011-02-24 13:21:08 +0100 | [diff] [blame] | 1109 | if (uap->using_rx_dma) { |
| 1110 | dmaengine_terminate_all(uap->dmarx.chan); |
| 1111 | /* Clean up the RX DMA */ |
| 1112 | pl011_sgbuf_free(uap->dmarx.chan, &uap->dmarx.sgbuf_a, DMA_FROM_DEVICE); |
| 1113 | pl011_sgbuf_free(uap->dmarx.chan, &uap->dmarx.sgbuf_b, DMA_FROM_DEVICE); |
Chanho Min | cb06ff1 | 2013-03-27 18:38:11 +0900 | [diff] [blame] | 1114 | if (uap->dmarx.poll_rate) |
| 1115 | del_timer_sync(&uap->dmarx.timer); |
Linus Walleij | ead76f32 | 2011-02-24 13:21:08 +0100 | [diff] [blame] | 1116 | uap->using_rx_dma = false; |
| 1117 | } |
Russell King | 68b65f7 | 2010-12-22 17:24:39 +0000 | [diff] [blame] | 1118 | } |
| 1119 | |
Linus Walleij | ead76f32 | 2011-02-24 13:21:08 +0100 | [diff] [blame] | 1120 | static inline bool pl011_dma_rx_available(struct uart_amba_port *uap) |
| 1121 | { |
| 1122 | return uap->using_rx_dma; |
| 1123 | } |
| 1124 | |
| 1125 | static inline bool pl011_dma_rx_running(struct uart_amba_port *uap) |
| 1126 | { |
| 1127 | return uap->using_rx_dma && uap->dmarx.running; |
| 1128 | } |
| 1129 | |
Russell King | 68b65f7 | 2010-12-22 17:24:39 +0000 | [diff] [blame] | 1130 | #else |
| 1131 | /* Blank functions if the DMA engine is not available */ |
Jorge Ramirez-Ortiz | 1c9be31 | 2015-03-06 13:05:40 -0500 | [diff] [blame] | 1132 | static inline void pl011_dma_probe(struct uart_amba_port *uap) |
Russell King | 68b65f7 | 2010-12-22 17:24:39 +0000 | [diff] [blame] | 1133 | { |
| 1134 | } |
| 1135 | |
| 1136 | static inline void pl011_dma_remove(struct uart_amba_port *uap) |
| 1137 | { |
| 1138 | } |
| 1139 | |
| 1140 | static inline void pl011_dma_startup(struct uart_amba_port *uap) |
| 1141 | { |
| 1142 | } |
| 1143 | |
| 1144 | static inline void pl011_dma_shutdown(struct uart_amba_port *uap) |
| 1145 | { |
| 1146 | } |
| 1147 | |
| 1148 | static inline bool pl011_dma_tx_irq(struct uart_amba_port *uap) |
| 1149 | { |
| 1150 | return false; |
| 1151 | } |
| 1152 | |
| 1153 | static inline void pl011_dma_tx_stop(struct uart_amba_port *uap) |
| 1154 | { |
| 1155 | } |
| 1156 | |
| 1157 | static inline bool pl011_dma_tx_start(struct uart_amba_port *uap) |
| 1158 | { |
| 1159 | return false; |
| 1160 | } |
| 1161 | |
Linus Walleij | ead76f32 | 2011-02-24 13:21:08 +0100 | [diff] [blame] | 1162 | static inline void pl011_dma_rx_irq(struct uart_amba_port *uap) |
| 1163 | { |
| 1164 | } |
| 1165 | |
| 1166 | static inline void pl011_dma_rx_stop(struct uart_amba_port *uap) |
| 1167 | { |
| 1168 | } |
| 1169 | |
| 1170 | static inline int pl011_dma_rx_trigger_dma(struct uart_amba_port *uap) |
| 1171 | { |
| 1172 | return -EIO; |
| 1173 | } |
| 1174 | |
| 1175 | static inline bool pl011_dma_rx_available(struct uart_amba_port *uap) |
| 1176 | { |
| 1177 | return false; |
| 1178 | } |
| 1179 | |
| 1180 | static inline bool pl011_dma_rx_running(struct uart_amba_port *uap) |
| 1181 | { |
| 1182 | return false; |
| 1183 | } |
| 1184 | |
Russell King | 68b65f7 | 2010-12-22 17:24:39 +0000 | [diff] [blame] | 1185 | #define pl011_dma_flush_buffer NULL |
| 1186 | #endif |
| 1187 | |
Russell King | b129a8c | 2005-08-31 10:12:14 +0100 | [diff] [blame] | 1188 | static void pl011_stop_tx(struct uart_port *port) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1189 | { |
Daniel Thompson | a5820c2 | 2014-09-03 12:51:55 +0100 | [diff] [blame] | 1190 | struct uart_amba_port *uap = |
| 1191 | container_of(port, struct uart_amba_port, port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1192 | |
| 1193 | uap->im &= ~UART011_TXIM; |
Russell King | 7583633 | 2015-11-03 14:50:58 +0000 | [diff] [blame^] | 1194 | pl011_write(uap->im, uap->port.membase, UART011_IMSC); |
Russell King | 68b65f7 | 2010-12-22 17:24:39 +0000 | [diff] [blame] | 1195 | pl011_dma_tx_stop(uap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1196 | } |
| 1197 | |
Dave Martin | 1e84d22 | 2015-04-27 16:49:05 +0100 | [diff] [blame] | 1198 | static void pl011_tx_chars(struct uart_amba_port *uap, bool from_irq); |
Dave Martin | 734745c | 2015-03-04 12:27:33 +0000 | [diff] [blame] | 1199 | |
| 1200 | /* Start TX with programmed I/O only (no DMA) */ |
| 1201 | static void pl011_start_tx_pio(struct uart_amba_port *uap) |
| 1202 | { |
| 1203 | uap->im |= UART011_TXIM; |
Russell King | 7583633 | 2015-11-03 14:50:58 +0000 | [diff] [blame^] | 1204 | pl011_write(uap->im, uap->port.membase, UART011_IMSC); |
Dave Martin | 1e84d22 | 2015-04-27 16:49:05 +0100 | [diff] [blame] | 1205 | pl011_tx_chars(uap, false); |
Dave Martin | 734745c | 2015-03-04 12:27:33 +0000 | [diff] [blame] | 1206 | } |
| 1207 | |
Russell King | b129a8c | 2005-08-31 10:12:14 +0100 | [diff] [blame] | 1208 | static void pl011_start_tx(struct uart_port *port) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1209 | { |
Daniel Thompson | a5820c2 | 2014-09-03 12:51:55 +0100 | [diff] [blame] | 1210 | struct uart_amba_port *uap = |
| 1211 | container_of(port, struct uart_amba_port, port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1212 | |
Dave Martin | 734745c | 2015-03-04 12:27:33 +0000 | [diff] [blame] | 1213 | if (!pl011_dma_tx_start(uap)) |
| 1214 | pl011_start_tx_pio(uap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1215 | } |
| 1216 | |
| 1217 | static void pl011_stop_rx(struct uart_port *port) |
| 1218 | { |
Daniel Thompson | a5820c2 | 2014-09-03 12:51:55 +0100 | [diff] [blame] | 1219 | struct uart_amba_port *uap = |
| 1220 | container_of(port, struct uart_amba_port, port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1221 | |
| 1222 | uap->im &= ~(UART011_RXIM|UART011_RTIM|UART011_FEIM| |
| 1223 | UART011_PEIM|UART011_BEIM|UART011_OEIM); |
Russell King | 7583633 | 2015-11-03 14:50:58 +0000 | [diff] [blame^] | 1224 | pl011_write(uap->im, uap->port.membase, UART011_IMSC); |
Linus Walleij | ead76f32 | 2011-02-24 13:21:08 +0100 | [diff] [blame] | 1225 | |
| 1226 | pl011_dma_rx_stop(uap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1227 | } |
| 1228 | |
| 1229 | static void pl011_enable_ms(struct uart_port *port) |
| 1230 | { |
Daniel Thompson | a5820c2 | 2014-09-03 12:51:55 +0100 | [diff] [blame] | 1231 | struct uart_amba_port *uap = |
| 1232 | container_of(port, struct uart_amba_port, port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1233 | |
| 1234 | uap->im |= UART011_RIMIM|UART011_CTSMIM|UART011_DCDMIM|UART011_DSRMIM; |
Russell King | 7583633 | 2015-11-03 14:50:58 +0000 | [diff] [blame^] | 1235 | pl011_write(uap->im, uap->port.membase, UART011_IMSC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1236 | } |
| 1237 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 1238 | static void pl011_rx_chars(struct uart_amba_port *uap) |
Fabio Estevam | b83286b | 2013-08-09 17:58:51 -0300 | [diff] [blame] | 1239 | __releases(&uap->port.lock) |
| 1240 | __acquires(&uap->port.lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1241 | { |
Linus Walleij | 29772c4 | 2011-02-24 13:21:36 +0100 | [diff] [blame] | 1242 | pl011_fifo_to_tty(uap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1243 | |
Thomas Gleixner | 2389b27 | 2007-05-29 21:53:50 +0100 | [diff] [blame] | 1244 | spin_unlock(&uap->port.lock); |
Jiri Slaby | 2e124b4 | 2013-01-03 15:53:06 +0100 | [diff] [blame] | 1245 | tty_flip_buffer_push(&uap->port.state->port); |
Linus Walleij | ead76f32 | 2011-02-24 13:21:08 +0100 | [diff] [blame] | 1246 | /* |
| 1247 | * If we were temporarily out of DMA mode for a while, |
| 1248 | * attempt to switch back to DMA mode again. |
| 1249 | */ |
| 1250 | if (pl011_dma_rx_available(uap)) { |
| 1251 | if (pl011_dma_rx_trigger_dma(uap)) { |
| 1252 | dev_dbg(uap->port.dev, "could not trigger RX DMA job " |
| 1253 | "fall back to interrupt mode again\n"); |
| 1254 | uap->im |= UART011_RXIM; |
Russell King | 7583633 | 2015-11-03 14:50:58 +0000 | [diff] [blame^] | 1255 | pl011_write(uap->im, uap->port.membase, UART011_IMSC); |
Chanho Min | cb06ff1 | 2013-03-27 18:38:11 +0900 | [diff] [blame] | 1256 | } else { |
Chanho Min | 89fa28d | 2013-04-03 11:10:37 +0900 | [diff] [blame] | 1257 | #ifdef CONFIG_DMA_ENGINE |
Chanho Min | cb06ff1 | 2013-03-27 18:38:11 +0900 | [diff] [blame] | 1258 | /* Start Rx DMA poll */ |
| 1259 | if (uap->dmarx.poll_rate) { |
| 1260 | uap->dmarx.last_jiffies = jiffies; |
| 1261 | uap->dmarx.last_residue = PL011_DMA_BUFFER_SIZE; |
| 1262 | mod_timer(&uap->dmarx.timer, |
| 1263 | jiffies + |
| 1264 | msecs_to_jiffies(uap->dmarx.poll_rate)); |
| 1265 | } |
Chanho Min | 89fa28d | 2013-04-03 11:10:37 +0900 | [diff] [blame] | 1266 | #endif |
Chanho Min | cb06ff1 | 2013-03-27 18:38:11 +0900 | [diff] [blame] | 1267 | } |
Linus Walleij | ead76f32 | 2011-02-24 13:21:08 +0100 | [diff] [blame] | 1268 | } |
Thomas Gleixner | 2389b27 | 2007-05-29 21:53:50 +0100 | [diff] [blame] | 1269 | spin_lock(&uap->port.lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1270 | } |
| 1271 | |
Dave Martin | 1e84d22 | 2015-04-27 16:49:05 +0100 | [diff] [blame] | 1272 | static bool pl011_tx_char(struct uart_amba_port *uap, unsigned char c, |
| 1273 | bool from_irq) |
Dave Martin | 734745c | 2015-03-04 12:27:33 +0000 | [diff] [blame] | 1274 | { |
Dave Martin | 1e84d22 | 2015-04-27 16:49:05 +0100 | [diff] [blame] | 1275 | if (unlikely(!from_irq) && |
Russell King | 7583633 | 2015-11-03 14:50:58 +0000 | [diff] [blame^] | 1276 | pl011_read(uap->port.membase, UART01x_FR) & UART01x_FR_TXFF) |
Dave Martin | 1e84d22 | 2015-04-27 16:49:05 +0100 | [diff] [blame] | 1277 | return false; /* unable to transmit character */ |
| 1278 | |
Russell King | 7583633 | 2015-11-03 14:50:58 +0000 | [diff] [blame^] | 1279 | pl011_write(c, uap->port.membase, UART01x_DR); |
Dave Martin | 734745c | 2015-03-04 12:27:33 +0000 | [diff] [blame] | 1280 | uap->port.icount.tx++; |
| 1281 | |
Dave Martin | 1e84d22 | 2015-04-27 16:49:05 +0100 | [diff] [blame] | 1282 | return true; |
Dave Martin | 734745c | 2015-03-04 12:27:33 +0000 | [diff] [blame] | 1283 | } |
| 1284 | |
Dave Martin | 1e84d22 | 2015-04-27 16:49:05 +0100 | [diff] [blame] | 1285 | static void pl011_tx_chars(struct uart_amba_port *uap, bool from_irq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1286 | { |
Alan Cox | ebd2c8f | 2009-09-19 13:13:28 -0700 | [diff] [blame] | 1287 | struct circ_buf *xmit = &uap->port.state->xmit; |
Dave Martin | 1e84d22 | 2015-04-27 16:49:05 +0100 | [diff] [blame] | 1288 | int count = uap->fifosize >> 1; |
Dave Martin | 734745c | 2015-03-04 12:27:33 +0000 | [diff] [blame] | 1289 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1290 | if (uap->port.x_char) { |
Dave Martin | 1e84d22 | 2015-04-27 16:49:05 +0100 | [diff] [blame] | 1291 | if (!pl011_tx_char(uap, uap->port.x_char, from_irq)) |
| 1292 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1293 | uap->port.x_char = 0; |
Dave Martin | 734745c | 2015-03-04 12:27:33 +0000 | [diff] [blame] | 1294 | --count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1295 | } |
| 1296 | if (uart_circ_empty(xmit) || uart_tx_stopped(&uap->port)) { |
Russell King | b129a8c | 2005-08-31 10:12:14 +0100 | [diff] [blame] | 1297 | pl011_stop_tx(&uap->port); |
Dave Martin | 1e84d22 | 2015-04-27 16:49:05 +0100 | [diff] [blame] | 1298 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1299 | } |
| 1300 | |
Russell King | 68b65f7 | 2010-12-22 17:24:39 +0000 | [diff] [blame] | 1301 | /* If we are using DMA mode, try to send some characters. */ |
| 1302 | if (pl011_dma_tx_irq(uap)) |
Dave Martin | 1e84d22 | 2015-04-27 16:49:05 +0100 | [diff] [blame] | 1303 | return; |
Russell King | 68b65f7 | 2010-12-22 17:24:39 +0000 | [diff] [blame] | 1304 | |
Dave Martin | 1e84d22 | 2015-04-27 16:49:05 +0100 | [diff] [blame] | 1305 | do { |
| 1306 | if (likely(from_irq) && count-- == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1307 | break; |
Dave Martin | 1e84d22 | 2015-04-27 16:49:05 +0100 | [diff] [blame] | 1308 | |
| 1309 | if (!pl011_tx_char(uap, xmit->buf[xmit->tail], from_irq)) |
| 1310 | break; |
| 1311 | |
| 1312 | xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1); |
| 1313 | } while (!uart_circ_empty(xmit)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1314 | |
| 1315 | if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS) |
| 1316 | uart_write_wakeup(&uap->port); |
| 1317 | |
Dave Martin | 1e84d22 | 2015-04-27 16:49:05 +0100 | [diff] [blame] | 1318 | if (uart_circ_empty(xmit)) |
Russell King | b129a8c | 2005-08-31 10:12:14 +0100 | [diff] [blame] | 1319 | pl011_stop_tx(&uap->port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1320 | } |
| 1321 | |
| 1322 | static void pl011_modem_status(struct uart_amba_port *uap) |
| 1323 | { |
| 1324 | unsigned int status, delta; |
| 1325 | |
Russell King | 7583633 | 2015-11-03 14:50:58 +0000 | [diff] [blame^] | 1326 | status = pl011_read(uap->port.membase, UART01x_FR); |
| 1327 | status &= UART01x_FR_MODEM_ANY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1328 | |
| 1329 | delta = status ^ uap->old_status; |
| 1330 | uap->old_status = status; |
| 1331 | |
| 1332 | if (!delta) |
| 1333 | return; |
| 1334 | |
| 1335 | if (delta & UART01x_FR_DCD) |
| 1336 | uart_handle_dcd_change(&uap->port, status & UART01x_FR_DCD); |
| 1337 | |
Greg Kroah-Hartman | 062a68a | 2015-09-04 09:11:24 -0700 | [diff] [blame] | 1338 | if (delta & UART01x_FR_DSR) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1339 | uap->port.icount.dsr++; |
| 1340 | |
Greg Kroah-Hartman | 062a68a | 2015-09-04 09:11:24 -0700 | [diff] [blame] | 1341 | if (delta & UART01x_FR_CTS) |
| 1342 | uart_handle_cts_change(&uap->port, status & UART01x_FR_CTS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1343 | |
Alan Cox | bdc04e3 | 2009-09-19 13:13:31 -0700 | [diff] [blame] | 1344 | wake_up_interruptible(&uap->port.state->port.delta_msr_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1345 | } |
| 1346 | |
Andre Przywara | 9c4ef4b | 2015-05-21 17:26:20 +0100 | [diff] [blame] | 1347 | static void check_apply_cts_event_workaround(struct uart_amba_port *uap) |
| 1348 | { |
| 1349 | unsigned int dummy_read; |
| 1350 | |
| 1351 | if (!uap->vendor->cts_event_workaround) |
| 1352 | return; |
| 1353 | |
| 1354 | /* workaround to make sure that all bits are unlocked.. */ |
Russell King | 7583633 | 2015-11-03 14:50:58 +0000 | [diff] [blame^] | 1355 | pl011_write(0x00, uap->port.membase, UART011_ICR); |
Andre Przywara | 9c4ef4b | 2015-05-21 17:26:20 +0100 | [diff] [blame] | 1356 | |
| 1357 | /* |
| 1358 | * WA: introduce 26ns(1 uart clk) delay before W1C; |
| 1359 | * single apb access will incur 2 pclk(133.12Mhz) delay, |
| 1360 | * so add 2 dummy reads |
| 1361 | */ |
Russell King | 7583633 | 2015-11-03 14:50:58 +0000 | [diff] [blame^] | 1362 | dummy_read = pl011_read(uap->port.membase, UART011_ICR); |
| 1363 | dummy_read = pl011_read(uap->port.membase, UART011_ICR); |
Andre Przywara | 9c4ef4b | 2015-05-21 17:26:20 +0100 | [diff] [blame] | 1364 | } |
| 1365 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 1366 | static irqreturn_t pl011_int(int irq, void *dev_id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1367 | { |
| 1368 | struct uart_amba_port *uap = dev_id; |
Russell King | 963cc98 | 2010-12-22 17:16:09 +0000 | [diff] [blame] | 1369 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1370 | unsigned int status, pass_counter = AMBA_ISR_PASS_LIMIT; |
Andre Przywara | 075167e | 2015-05-21 17:26:19 +0100 | [diff] [blame] | 1371 | u16 imsc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1372 | int handled = 0; |
| 1373 | |
Russell King | 963cc98 | 2010-12-22 17:16:09 +0000 | [diff] [blame] | 1374 | spin_lock_irqsave(&uap->port.lock, flags); |
Russell King | 7583633 | 2015-11-03 14:50:58 +0000 | [diff] [blame^] | 1375 | imsc = pl011_read(uap->port.membase, UART011_IMSC); |
| 1376 | status = pl011_read(uap->port.membase, UART011_RIS) & imsc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1377 | if (status) { |
| 1378 | do { |
Andre Przywara | 9c4ef4b | 2015-05-21 17:26:20 +0100 | [diff] [blame] | 1379 | check_apply_cts_event_workaround(uap); |
Greg Kroah-Hartman | f11c984 | 2015-09-04 09:13:39 -0700 | [diff] [blame] | 1380 | |
Russell King | 7583633 | 2015-11-03 14:50:58 +0000 | [diff] [blame^] | 1381 | pl011_write(status & ~(UART011_TXIS|UART011_RTIS| |
| 1382 | UART011_RXIS), |
| 1383 | uap->port.membase, UART011_ICR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1384 | |
Linus Walleij | ead76f32 | 2011-02-24 13:21:08 +0100 | [diff] [blame] | 1385 | if (status & (UART011_RTIS|UART011_RXIS)) { |
| 1386 | if (pl011_dma_rx_running(uap)) |
| 1387 | pl011_dma_rx_irq(uap); |
| 1388 | else |
| 1389 | pl011_rx_chars(uap); |
| 1390 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1391 | if (status & (UART011_DSRMIS|UART011_DCDMIS| |
| 1392 | UART011_CTSMIS|UART011_RIMIS)) |
| 1393 | pl011_modem_status(uap); |
Dave Martin | 1e84d22 | 2015-04-27 16:49:05 +0100 | [diff] [blame] | 1394 | if (status & UART011_TXIS) |
| 1395 | pl011_tx_chars(uap, true); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1396 | |
Rajanikanth H.V | 4fd0690 | 2012-03-26 11:17:02 +0200 | [diff] [blame] | 1397 | if (pass_counter-- == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1398 | break; |
| 1399 | |
Russell King | 7583633 | 2015-11-03 14:50:58 +0000 | [diff] [blame^] | 1400 | status = pl011_read(uap->port.membase, UART011_RIS) & imsc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1401 | } while (status != 0); |
| 1402 | handled = 1; |
| 1403 | } |
| 1404 | |
Russell King | 963cc98 | 2010-12-22 17:16:09 +0000 | [diff] [blame] | 1405 | spin_unlock_irqrestore(&uap->port.lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1406 | |
| 1407 | return IRQ_RETVAL(handled); |
| 1408 | } |
| 1409 | |
Linus Walleij | e643f87 | 2012-06-17 15:44:19 +0200 | [diff] [blame] | 1410 | static unsigned int pl011_tx_empty(struct uart_port *port) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1411 | { |
Daniel Thompson | a5820c2 | 2014-09-03 12:51:55 +0100 | [diff] [blame] | 1412 | struct uart_amba_port *uap = |
| 1413 | container_of(port, struct uart_amba_port, port); |
Russell King | 7583633 | 2015-11-03 14:50:58 +0000 | [diff] [blame^] | 1414 | unsigned int status = pl011_read(uap->port.membase, UART01x_FR); |
Greg Kroah-Hartman | 062a68a | 2015-09-04 09:11:24 -0700 | [diff] [blame] | 1415 | return status & (UART01x_FR_BUSY|UART01x_FR_TXFF) ? 0 : TIOCSER_TEMT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1416 | } |
| 1417 | |
Linus Walleij | e643f87 | 2012-06-17 15:44:19 +0200 | [diff] [blame] | 1418 | static unsigned int pl011_get_mctrl(struct uart_port *port) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1419 | { |
Daniel Thompson | a5820c2 | 2014-09-03 12:51:55 +0100 | [diff] [blame] | 1420 | struct uart_amba_port *uap = |
| 1421 | container_of(port, struct uart_amba_port, port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1422 | unsigned int result = 0; |
Russell King | 7583633 | 2015-11-03 14:50:58 +0000 | [diff] [blame^] | 1423 | unsigned int status = pl011_read(uap->port.membase, UART01x_FR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1424 | |
Jiri Slaby | 5159f40 | 2007-10-18 23:40:31 -0700 | [diff] [blame] | 1425 | #define TIOCMBIT(uartbit, tiocmbit) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1426 | if (status & uartbit) \ |
| 1427 | result |= tiocmbit |
| 1428 | |
Jiri Slaby | 5159f40 | 2007-10-18 23:40:31 -0700 | [diff] [blame] | 1429 | TIOCMBIT(UART01x_FR_DCD, TIOCM_CAR); |
Greg Kroah-Hartman | 062a68a | 2015-09-04 09:11:24 -0700 | [diff] [blame] | 1430 | TIOCMBIT(UART01x_FR_DSR, TIOCM_DSR); |
| 1431 | TIOCMBIT(UART01x_FR_CTS, TIOCM_CTS); |
| 1432 | TIOCMBIT(UART011_FR_RI, TIOCM_RNG); |
Jiri Slaby | 5159f40 | 2007-10-18 23:40:31 -0700 | [diff] [blame] | 1433 | #undef TIOCMBIT |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1434 | return result; |
| 1435 | } |
| 1436 | |
| 1437 | static void pl011_set_mctrl(struct uart_port *port, unsigned int mctrl) |
| 1438 | { |
Daniel Thompson | a5820c2 | 2014-09-03 12:51:55 +0100 | [diff] [blame] | 1439 | struct uart_amba_port *uap = |
| 1440 | container_of(port, struct uart_amba_port, port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1441 | unsigned int cr; |
| 1442 | |
Russell King | 7583633 | 2015-11-03 14:50:58 +0000 | [diff] [blame^] | 1443 | cr = pl011_read(uap->port.membase, UART011_CR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1444 | |
Jiri Slaby | 5159f40 | 2007-10-18 23:40:31 -0700 | [diff] [blame] | 1445 | #define TIOCMBIT(tiocmbit, uartbit) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1446 | if (mctrl & tiocmbit) \ |
| 1447 | cr |= uartbit; \ |
| 1448 | else \ |
| 1449 | cr &= ~uartbit |
| 1450 | |
Jiri Slaby | 5159f40 | 2007-10-18 23:40:31 -0700 | [diff] [blame] | 1451 | TIOCMBIT(TIOCM_RTS, UART011_CR_RTS); |
| 1452 | TIOCMBIT(TIOCM_DTR, UART011_CR_DTR); |
| 1453 | TIOCMBIT(TIOCM_OUT1, UART011_CR_OUT1); |
| 1454 | TIOCMBIT(TIOCM_OUT2, UART011_CR_OUT2); |
| 1455 | TIOCMBIT(TIOCM_LOOP, UART011_CR_LBE); |
Rabin Vincent | 3b43816 | 2010-02-12 06:43:11 +0100 | [diff] [blame] | 1456 | |
| 1457 | if (uap->autorts) { |
| 1458 | /* We need to disable auto-RTS if we want to turn RTS off */ |
| 1459 | TIOCMBIT(TIOCM_RTS, UART011_CR_RTSEN); |
| 1460 | } |
Jiri Slaby | 5159f40 | 2007-10-18 23:40:31 -0700 | [diff] [blame] | 1461 | #undef TIOCMBIT |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1462 | |
Russell King | 7583633 | 2015-11-03 14:50:58 +0000 | [diff] [blame^] | 1463 | pl011_write(cr, uap->port.membase, UART011_CR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1464 | } |
| 1465 | |
| 1466 | static void pl011_break_ctl(struct uart_port *port, int break_state) |
| 1467 | { |
Daniel Thompson | a5820c2 | 2014-09-03 12:51:55 +0100 | [diff] [blame] | 1468 | struct uart_amba_port *uap = |
| 1469 | container_of(port, struct uart_amba_port, port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1470 | unsigned long flags; |
| 1471 | unsigned int lcr_h; |
| 1472 | |
| 1473 | spin_lock_irqsave(&uap->port.lock, flags); |
Russell King | 7583633 | 2015-11-03 14:50:58 +0000 | [diff] [blame^] | 1474 | lcr_h = pl011_read(uap->port.membase, uap->lcrh_tx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1475 | if (break_state == -1) |
| 1476 | lcr_h |= UART01x_LCRH_BRK; |
| 1477 | else |
| 1478 | lcr_h &= ~UART01x_LCRH_BRK; |
Russell King | 7583633 | 2015-11-03 14:50:58 +0000 | [diff] [blame^] | 1479 | pl011_write(lcr_h, uap->port.membase, uap->lcrh_tx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1480 | spin_unlock_irqrestore(&uap->port.lock, flags); |
| 1481 | } |
| 1482 | |
Jason Wessel | 84b5ae1 | 2008-02-20 13:33:39 -0600 | [diff] [blame] | 1483 | #ifdef CONFIG_CONSOLE_POLL |
Anton Vorontsov | 5c8124a | 2012-09-24 14:27:55 -0700 | [diff] [blame] | 1484 | |
| 1485 | static void pl011_quiesce_irqs(struct uart_port *port) |
| 1486 | { |
Daniel Thompson | a5820c2 | 2014-09-03 12:51:55 +0100 | [diff] [blame] | 1487 | struct uart_amba_port *uap = |
| 1488 | container_of(port, struct uart_amba_port, port); |
Greg Kroah-Hartman | f11c984 | 2015-09-04 09:13:39 -0700 | [diff] [blame] | 1489 | unsigned char __iomem *regs = uap->port.membase; |
Anton Vorontsov | 5c8124a | 2012-09-24 14:27:55 -0700 | [diff] [blame] | 1490 | |
Russell King | 7583633 | 2015-11-03 14:50:58 +0000 | [diff] [blame^] | 1491 | pl011_write(pl011_read(regs, UART011_MIS), regs, UART011_ICR); |
Anton Vorontsov | 5c8124a | 2012-09-24 14:27:55 -0700 | [diff] [blame] | 1492 | /* |
| 1493 | * There is no way to clear TXIM as this is "ready to transmit IRQ", so |
| 1494 | * we simply mask it. start_tx() will unmask it. |
| 1495 | * |
| 1496 | * Note we can race with start_tx(), and if the race happens, the |
| 1497 | * polling user might get another interrupt just after we clear it. |
| 1498 | * But it should be OK and can happen even w/o the race, e.g. |
| 1499 | * controller immediately got some new data and raised the IRQ. |
| 1500 | * |
| 1501 | * And whoever uses polling routines assumes that it manages the device |
| 1502 | * (including tx queue), so we're also fine with start_tx()'s caller |
| 1503 | * side. |
| 1504 | */ |
Russell King | 7583633 | 2015-11-03 14:50:58 +0000 | [diff] [blame^] | 1505 | pl011_write(pl011_read(regs, UART011_IMSC) & ~UART011_TXIM, |
| 1506 | regs, UART011_IMSC); |
Anton Vorontsov | 5c8124a | 2012-09-24 14:27:55 -0700 | [diff] [blame] | 1507 | } |
| 1508 | |
Linus Walleij | e643f87 | 2012-06-17 15:44:19 +0200 | [diff] [blame] | 1509 | static int pl011_get_poll_char(struct uart_port *port) |
Jason Wessel | 84b5ae1 | 2008-02-20 13:33:39 -0600 | [diff] [blame] | 1510 | { |
Daniel Thompson | a5820c2 | 2014-09-03 12:51:55 +0100 | [diff] [blame] | 1511 | struct uart_amba_port *uap = |
| 1512 | container_of(port, struct uart_amba_port, port); |
Jason Wessel | 84b5ae1 | 2008-02-20 13:33:39 -0600 | [diff] [blame] | 1513 | unsigned int status; |
| 1514 | |
Anton Vorontsov | 5c8124a | 2012-09-24 14:27:55 -0700 | [diff] [blame] | 1515 | /* |
| 1516 | * The caller might need IRQs lowered, e.g. if used with KDB NMI |
| 1517 | * debugger. |
| 1518 | */ |
| 1519 | pl011_quiesce_irqs(port); |
| 1520 | |
Russell King | 7583633 | 2015-11-03 14:50:58 +0000 | [diff] [blame^] | 1521 | status = pl011_read(uap->port.membase, UART01x_FR); |
Jason Wessel | f5316b4 | 2010-05-20 21:04:22 -0500 | [diff] [blame] | 1522 | if (status & UART01x_FR_RXFE) |
| 1523 | return NO_POLL_CHAR; |
Jason Wessel | 84b5ae1 | 2008-02-20 13:33:39 -0600 | [diff] [blame] | 1524 | |
Russell King | 7583633 | 2015-11-03 14:50:58 +0000 | [diff] [blame^] | 1525 | return pl011_read(uap->port.membase, UART01x_DR); |
Jason Wessel | 84b5ae1 | 2008-02-20 13:33:39 -0600 | [diff] [blame] | 1526 | } |
| 1527 | |
Linus Walleij | e643f87 | 2012-06-17 15:44:19 +0200 | [diff] [blame] | 1528 | static void pl011_put_poll_char(struct uart_port *port, |
Jason Wessel | 84b5ae1 | 2008-02-20 13:33:39 -0600 | [diff] [blame] | 1529 | unsigned char ch) |
| 1530 | { |
Daniel Thompson | a5820c2 | 2014-09-03 12:51:55 +0100 | [diff] [blame] | 1531 | struct uart_amba_port *uap = |
| 1532 | container_of(port, struct uart_amba_port, port); |
Jason Wessel | 84b5ae1 | 2008-02-20 13:33:39 -0600 | [diff] [blame] | 1533 | |
Russell King | 7583633 | 2015-11-03 14:50:58 +0000 | [diff] [blame^] | 1534 | while (pl011_read(uap->port.membase, UART01x_FR) & UART01x_FR_TXFF) |
Jason Wessel | 84b5ae1 | 2008-02-20 13:33:39 -0600 | [diff] [blame] | 1535 | barrier(); |
| 1536 | |
Russell King | 7583633 | 2015-11-03 14:50:58 +0000 | [diff] [blame^] | 1537 | pl011_write(ch, uap->port.membase, UART01x_DR); |
Jason Wessel | 84b5ae1 | 2008-02-20 13:33:39 -0600 | [diff] [blame] | 1538 | } |
| 1539 | |
| 1540 | #endif /* CONFIG_CONSOLE_POLL */ |
| 1541 | |
Anton Vorontsov | b3564c2 | 2012-09-24 14:27:54 -0700 | [diff] [blame] | 1542 | static int pl011_hwinit(struct uart_port *port) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1543 | { |
Daniel Thompson | a5820c2 | 2014-09-03 12:51:55 +0100 | [diff] [blame] | 1544 | struct uart_amba_port *uap = |
| 1545 | container_of(port, struct uart_amba_port, port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1546 | int retval; |
| 1547 | |
Linus Walleij | 78d80c5 | 2012-05-23 21:18:46 +0200 | [diff] [blame] | 1548 | /* Optionaly enable pins to be muxed in and configured */ |
Linus Walleij | 2b996fc | 2013-06-05 15:36:42 +0200 | [diff] [blame] | 1549 | pinctrl_pm_select_default_state(port->dev); |
Linus Walleij | 78d80c5 | 2012-05-23 21:18:46 +0200 | [diff] [blame] | 1550 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1551 | /* |
| 1552 | * Try to enable the clock producer. |
| 1553 | */ |
Julia Lawall | 1c4c439 | 2012-08-26 18:01:01 +0200 | [diff] [blame] | 1554 | retval = clk_prepare_enable(uap->clk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1555 | if (retval) |
Tushar Behera | 7f6d942 | 2014-06-26 15:35:35 +0530 | [diff] [blame] | 1556 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1557 | |
| 1558 | uap->port.uartclk = clk_get_rate(uap->clk); |
| 1559 | |
Linus Walleij | 9b96fba | 2012-03-13 13:27:23 +0100 | [diff] [blame] | 1560 | /* Clear pending error and receive interrupts */ |
Russell King | 7583633 | 2015-11-03 14:50:58 +0000 | [diff] [blame^] | 1561 | pl011_write(UART011_OEIS | UART011_BEIS | UART011_PEIS | |
| 1562 | UART011_FEIS | UART011_RTIS | UART011_RXIS, |
| 1563 | uap->port.membase, UART011_ICR); |
Linus Walleij | 9b96fba | 2012-03-13 13:27:23 +0100 | [diff] [blame] | 1564 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1565 | /* |
Anton Vorontsov | b3564c2 | 2012-09-24 14:27:54 -0700 | [diff] [blame] | 1566 | * Save interrupts enable mask, and enable RX interrupts in case if |
| 1567 | * the interrupt is used for NMI entry. |
| 1568 | */ |
Russell King | 7583633 | 2015-11-03 14:50:58 +0000 | [diff] [blame^] | 1569 | uap->im = pl011_read(uap->port.membase, UART011_IMSC); |
| 1570 | pl011_write(UART011_RTIM | UART011_RXIM, uap->port.membase, |
| 1571 | UART011_IMSC); |
Anton Vorontsov | b3564c2 | 2012-09-24 14:27:54 -0700 | [diff] [blame] | 1572 | |
Jingoo Han | 574de55 | 2013-07-30 17:06:57 +0900 | [diff] [blame] | 1573 | if (dev_get_platdata(uap->port.dev)) { |
Anton Vorontsov | b3564c2 | 2012-09-24 14:27:54 -0700 | [diff] [blame] | 1574 | struct amba_pl011_data *plat; |
| 1575 | |
Jingoo Han | 574de55 | 2013-07-30 17:06:57 +0900 | [diff] [blame] | 1576 | plat = dev_get_platdata(uap->port.dev); |
Anton Vorontsov | b3564c2 | 2012-09-24 14:27:54 -0700 | [diff] [blame] | 1577 | if (plat->init) |
| 1578 | plat->init(); |
| 1579 | } |
| 1580 | return 0; |
Anton Vorontsov | b3564c2 | 2012-09-24 14:27:54 -0700 | [diff] [blame] | 1581 | } |
| 1582 | |
Jon Medhurst | b60f2f6 | 2013-12-10 10:18:59 +0000 | [diff] [blame] | 1583 | static void pl011_write_lcr_h(struct uart_amba_port *uap, unsigned int lcr_h) |
| 1584 | { |
Russell King | 7583633 | 2015-11-03 14:50:58 +0000 | [diff] [blame^] | 1585 | pl011_write(lcr_h, uap->port.membase, uap->lcrh_rx); |
Greg Kroah-Hartman | 8e50254 | 2015-09-04 09:12:03 -0700 | [diff] [blame] | 1586 | if (uap->lcrh_rx != uap->lcrh_tx) { |
Jon Medhurst | b60f2f6 | 2013-12-10 10:18:59 +0000 | [diff] [blame] | 1587 | int i; |
| 1588 | /* |
| 1589 | * Wait 10 PCLKs before writing LCRH_TX register, |
| 1590 | * to get this delay write read only register 10 times |
| 1591 | */ |
| 1592 | for (i = 0; i < 10; ++i) |
Russell King | 7583633 | 2015-11-03 14:50:58 +0000 | [diff] [blame^] | 1593 | pl011_write(0xff, uap->port.membase, UART011_MIS); |
| 1594 | pl011_write(lcr_h, uap->port.membase, uap->lcrh_tx); |
Jon Medhurst | b60f2f6 | 2013-12-10 10:18:59 +0000 | [diff] [blame] | 1595 | } |
| 1596 | } |
| 1597 | |
Andre Przywara | 867b8e8 | 2015-05-21 17:26:15 +0100 | [diff] [blame] | 1598 | static int pl011_allocate_irq(struct uart_amba_port *uap) |
| 1599 | { |
Russell King | 7583633 | 2015-11-03 14:50:58 +0000 | [diff] [blame^] | 1600 | pl011_write(uap->im, uap->port.membase, UART011_IMSC); |
Andre Przywara | 867b8e8 | 2015-05-21 17:26:15 +0100 | [diff] [blame] | 1601 | |
| 1602 | return request_irq(uap->port.irq, pl011_int, 0, "uart-pl011", uap); |
| 1603 | } |
| 1604 | |
| 1605 | /* |
| 1606 | * Enable interrupts, only timeouts when using DMA |
| 1607 | * if initial RX DMA job failed, start in interrupt mode |
| 1608 | * as well. |
| 1609 | */ |
| 1610 | static void pl011_enable_interrupts(struct uart_amba_port *uap) |
| 1611 | { |
| 1612 | spin_lock_irq(&uap->port.lock); |
| 1613 | |
| 1614 | /* Clear out any spuriously appearing RX interrupts */ |
Russell King | 7583633 | 2015-11-03 14:50:58 +0000 | [diff] [blame^] | 1615 | pl011_write(UART011_RTIS | UART011_RXIS, uap->port.membase, |
| 1616 | UART011_ICR); |
Andre Przywara | 867b8e8 | 2015-05-21 17:26:15 +0100 | [diff] [blame] | 1617 | uap->im = UART011_RTIM; |
| 1618 | if (!pl011_dma_rx_running(uap)) |
| 1619 | uap->im |= UART011_RXIM; |
Russell King | 7583633 | 2015-11-03 14:50:58 +0000 | [diff] [blame^] | 1620 | pl011_write(uap->im, uap->port.membase, UART011_IMSC); |
Andre Przywara | 867b8e8 | 2015-05-21 17:26:15 +0100 | [diff] [blame] | 1621 | spin_unlock_irq(&uap->port.lock); |
| 1622 | } |
| 1623 | |
Anton Vorontsov | b3564c2 | 2012-09-24 14:27:54 -0700 | [diff] [blame] | 1624 | static int pl011_startup(struct uart_port *port) |
| 1625 | { |
Daniel Thompson | a5820c2 | 2014-09-03 12:51:55 +0100 | [diff] [blame] | 1626 | struct uart_amba_port *uap = |
| 1627 | container_of(port, struct uart_amba_port, port); |
Dave Martin | 734745c | 2015-03-04 12:27:33 +0000 | [diff] [blame] | 1628 | unsigned int cr; |
Anton Vorontsov | b3564c2 | 2012-09-24 14:27:54 -0700 | [diff] [blame] | 1629 | int retval; |
| 1630 | |
| 1631 | retval = pl011_hwinit(port); |
| 1632 | if (retval) |
| 1633 | goto clk_dis; |
| 1634 | |
Andre Przywara | 867b8e8 | 2015-05-21 17:26:15 +0100 | [diff] [blame] | 1635 | retval = pl011_allocate_irq(uap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1636 | if (retval) |
| 1637 | goto clk_dis; |
| 1638 | |
Russell King | 7583633 | 2015-11-03 14:50:58 +0000 | [diff] [blame^] | 1639 | pl011_write(uap->vendor->ifls, uap->port.membase, UART011_IFLS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1640 | |
Jon Medhurst | fe43390 | 2013-12-10 10:18:58 +0000 | [diff] [blame] | 1641 | spin_lock_irq(&uap->port.lock); |
| 1642 | |
Shreshtha Kumar Sahu | d8d8ffa | 2012-01-18 15:53:59 +0530 | [diff] [blame] | 1643 | /* restore RTS and DTR */ |
| 1644 | cr = uap->old_cr & (UART011_CR_RTS | UART011_CR_DTR); |
| 1645 | cr |= UART01x_CR_UARTEN | UART011_CR_RXE | UART011_CR_TXE; |
Russell King | 7583633 | 2015-11-03 14:50:58 +0000 | [diff] [blame^] | 1646 | pl011_write(cr, uap->port.membase, UART011_CR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1647 | |
Jon Medhurst | fe43390 | 2013-12-10 10:18:58 +0000 | [diff] [blame] | 1648 | spin_unlock_irq(&uap->port.lock); |
| 1649 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1650 | /* |
| 1651 | * initialise the old status of the modem signals |
| 1652 | */ |
Russell King | 7583633 | 2015-11-03 14:50:58 +0000 | [diff] [blame^] | 1653 | uap->old_status = pl011_read(uap->port.membase, UART01x_FR) & UART01x_FR_MODEM_ANY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1654 | |
Russell King | 68b65f7 | 2010-12-22 17:24:39 +0000 | [diff] [blame] | 1655 | /* Startup DMA */ |
| 1656 | pl011_dma_startup(uap); |
| 1657 | |
Andre Przywara | 867b8e8 | 2015-05-21 17:26:15 +0100 | [diff] [blame] | 1658 | pl011_enable_interrupts(uap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1659 | |
| 1660 | return 0; |
| 1661 | |
| 1662 | clk_dis: |
Julia Lawall | 1c4c439 | 2012-08-26 18:01:01 +0200 | [diff] [blame] | 1663 | clk_disable_unprepare(uap->clk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1664 | return retval; |
| 1665 | } |
| 1666 | |
Andre Przywara | 0dd1e24 | 2015-05-21 17:26:23 +0100 | [diff] [blame] | 1667 | static int sbsa_uart_startup(struct uart_port *port) |
| 1668 | { |
| 1669 | struct uart_amba_port *uap = |
| 1670 | container_of(port, struct uart_amba_port, port); |
| 1671 | int retval; |
| 1672 | |
| 1673 | retval = pl011_hwinit(port); |
| 1674 | if (retval) |
| 1675 | return retval; |
| 1676 | |
| 1677 | retval = pl011_allocate_irq(uap); |
| 1678 | if (retval) |
| 1679 | return retval; |
| 1680 | |
| 1681 | /* The SBSA UART does not support any modem status lines. */ |
| 1682 | uap->old_status = 0; |
| 1683 | |
| 1684 | pl011_enable_interrupts(uap); |
| 1685 | |
| 1686 | return 0; |
| 1687 | } |
| 1688 | |
Linus Walleij | ec489aa | 2010-06-02 08:13:52 +0100 | [diff] [blame] | 1689 | static void pl011_shutdown_channel(struct uart_amba_port *uap, |
| 1690 | unsigned int lcrh) |
| 1691 | { |
Greg Kroah-Hartman | f11c984 | 2015-09-04 09:13:39 -0700 | [diff] [blame] | 1692 | unsigned long val; |
Linus Walleij | ec489aa | 2010-06-02 08:13:52 +0100 | [diff] [blame] | 1693 | |
Russell King | 7583633 | 2015-11-03 14:50:58 +0000 | [diff] [blame^] | 1694 | val = pl011_read(uap->port.membase, lcrh); |
Greg Kroah-Hartman | f11c984 | 2015-09-04 09:13:39 -0700 | [diff] [blame] | 1695 | val &= ~(UART01x_LCRH_BRK | UART01x_LCRH_FEN); |
Russell King | 7583633 | 2015-11-03 14:50:58 +0000 | [diff] [blame^] | 1696 | pl011_write(val, uap->port.membase, lcrh); |
Linus Walleij | ec489aa | 2010-06-02 08:13:52 +0100 | [diff] [blame] | 1697 | } |
| 1698 | |
Andre Przywara | 95166a3 | 2015-05-21 17:26:16 +0100 | [diff] [blame] | 1699 | /* |
| 1700 | * disable the port. It should not disable RTS and DTR. |
| 1701 | * Also RTS and DTR state should be preserved to restore |
| 1702 | * it during startup(). |
| 1703 | */ |
| 1704 | static void pl011_disable_uart(struct uart_amba_port *uap) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1705 | { |
Shreshtha Kumar Sahu | d8d8ffa | 2012-01-18 15:53:59 +0530 | [diff] [blame] | 1706 | unsigned int cr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1707 | |
Rabin Vincent | 3b43816 | 2010-02-12 06:43:11 +0100 | [diff] [blame] | 1708 | uap->autorts = false; |
Jon Medhurst | fe43390 | 2013-12-10 10:18:58 +0000 | [diff] [blame] | 1709 | spin_lock_irq(&uap->port.lock); |
Russell King | 7583633 | 2015-11-03 14:50:58 +0000 | [diff] [blame^] | 1710 | cr = pl011_read(uap->port.membase, UART011_CR); |
Shreshtha Kumar Sahu | d8d8ffa | 2012-01-18 15:53:59 +0530 | [diff] [blame] | 1711 | uap->old_cr = cr; |
| 1712 | cr &= UART011_CR_RTS | UART011_CR_DTR; |
| 1713 | cr |= UART01x_CR_UARTEN | UART011_CR_TXE; |
Russell King | 7583633 | 2015-11-03 14:50:58 +0000 | [diff] [blame^] | 1714 | pl011_write(cr, uap->port.membase, UART011_CR); |
Jon Medhurst | fe43390 | 2013-12-10 10:18:58 +0000 | [diff] [blame] | 1715 | spin_unlock_irq(&uap->port.lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1716 | |
| 1717 | /* |
| 1718 | * disable break condition and fifos |
| 1719 | */ |
Linus Walleij | ec489aa | 2010-06-02 08:13:52 +0100 | [diff] [blame] | 1720 | pl011_shutdown_channel(uap, uap->lcrh_rx); |
Greg Kroah-Hartman | 8e50254 | 2015-09-04 09:12:03 -0700 | [diff] [blame] | 1721 | if (uap->lcrh_rx != uap->lcrh_tx) |
Linus Walleij | ec489aa | 2010-06-02 08:13:52 +0100 | [diff] [blame] | 1722 | pl011_shutdown_channel(uap, uap->lcrh_tx); |
Andre Przywara | 95166a3 | 2015-05-21 17:26:16 +0100 | [diff] [blame] | 1723 | } |
| 1724 | |
| 1725 | static void pl011_disable_interrupts(struct uart_amba_port *uap) |
| 1726 | { |
| 1727 | spin_lock_irq(&uap->port.lock); |
| 1728 | |
| 1729 | /* mask all interrupts and clear all pending ones */ |
| 1730 | uap->im = 0; |
Russell King | 7583633 | 2015-11-03 14:50:58 +0000 | [diff] [blame^] | 1731 | pl011_write(uap->im, uap->port.membase, UART011_IMSC); |
| 1732 | pl011_write(0xffff, uap->port.membase, UART011_ICR); |
Andre Przywara | 95166a3 | 2015-05-21 17:26:16 +0100 | [diff] [blame] | 1733 | |
| 1734 | spin_unlock_irq(&uap->port.lock); |
| 1735 | } |
| 1736 | |
| 1737 | static void pl011_shutdown(struct uart_port *port) |
| 1738 | { |
| 1739 | struct uart_amba_port *uap = |
| 1740 | container_of(port, struct uart_amba_port, port); |
| 1741 | |
| 1742 | pl011_disable_interrupts(uap); |
| 1743 | |
| 1744 | pl011_dma_shutdown(uap); |
| 1745 | |
| 1746 | free_irq(uap->port.irq, uap); |
| 1747 | |
| 1748 | pl011_disable_uart(uap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1749 | |
| 1750 | /* |
| 1751 | * Shut down the clock producer |
| 1752 | */ |
Julia Lawall | 1c4c439 | 2012-08-26 18:01:01 +0200 | [diff] [blame] | 1753 | clk_disable_unprepare(uap->clk); |
Linus Walleij | 78d80c5 | 2012-05-23 21:18:46 +0200 | [diff] [blame] | 1754 | /* Optionally let pins go into sleep states */ |
Linus Walleij | 2b996fc | 2013-06-05 15:36:42 +0200 | [diff] [blame] | 1755 | pinctrl_pm_select_sleep_state(port->dev); |
Shreshtha Kumar Sahu | c16d51a | 2011-06-13 10:11:33 +0200 | [diff] [blame] | 1756 | |
Jingoo Han | 574de55 | 2013-07-30 17:06:57 +0900 | [diff] [blame] | 1757 | if (dev_get_platdata(uap->port.dev)) { |
Shreshtha Kumar Sahu | c16d51a | 2011-06-13 10:11:33 +0200 | [diff] [blame] | 1758 | struct amba_pl011_data *plat; |
| 1759 | |
Jingoo Han | 574de55 | 2013-07-30 17:06:57 +0900 | [diff] [blame] | 1760 | plat = dev_get_platdata(uap->port.dev); |
Shreshtha Kumar Sahu | c16d51a | 2011-06-13 10:11:33 +0200 | [diff] [blame] | 1761 | if (plat->exit) |
| 1762 | plat->exit(); |
| 1763 | } |
| 1764 | |
Peter Hurley | 36f339d | 2014-11-06 09:06:12 -0500 | [diff] [blame] | 1765 | if (uap->port.ops->flush_buffer) |
| 1766 | uap->port.ops->flush_buffer(port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1767 | } |
| 1768 | |
Andre Przywara | 0dd1e24 | 2015-05-21 17:26:23 +0100 | [diff] [blame] | 1769 | static void sbsa_uart_shutdown(struct uart_port *port) |
| 1770 | { |
| 1771 | struct uart_amba_port *uap = |
| 1772 | container_of(port, struct uart_amba_port, port); |
| 1773 | |
| 1774 | pl011_disable_interrupts(uap); |
| 1775 | |
| 1776 | free_irq(uap->port.irq, uap); |
| 1777 | |
| 1778 | if (uap->port.ops->flush_buffer) |
| 1779 | uap->port.ops->flush_buffer(port); |
| 1780 | } |
| 1781 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1782 | static void |
Andre Przywara | ef5a935 | 2015-05-21 17:26:17 +0100 | [diff] [blame] | 1783 | pl011_setup_status_masks(struct uart_port *port, struct ktermios *termios) |
| 1784 | { |
| 1785 | port->read_status_mask = UART011_DR_OE | 255; |
| 1786 | if (termios->c_iflag & INPCK) |
| 1787 | port->read_status_mask |= UART011_DR_FE | UART011_DR_PE; |
| 1788 | if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK)) |
| 1789 | port->read_status_mask |= UART011_DR_BE; |
| 1790 | |
| 1791 | /* |
| 1792 | * Characters to ignore |
| 1793 | */ |
| 1794 | port->ignore_status_mask = 0; |
| 1795 | if (termios->c_iflag & IGNPAR) |
| 1796 | port->ignore_status_mask |= UART011_DR_FE | UART011_DR_PE; |
| 1797 | if (termios->c_iflag & IGNBRK) { |
| 1798 | port->ignore_status_mask |= UART011_DR_BE; |
| 1799 | /* |
| 1800 | * If we're ignoring parity and break indicators, |
| 1801 | * ignore overruns too (for real raw support). |
| 1802 | */ |
| 1803 | if (termios->c_iflag & IGNPAR) |
| 1804 | port->ignore_status_mask |= UART011_DR_OE; |
| 1805 | } |
| 1806 | |
| 1807 | /* |
| 1808 | * Ignore all characters if CREAD is not set. |
| 1809 | */ |
| 1810 | if ((termios->c_cflag & CREAD) == 0) |
| 1811 | port->ignore_status_mask |= UART_DUMMY_DR_RX; |
| 1812 | } |
| 1813 | |
| 1814 | static void |
Alan Cox | 606d099 | 2006-12-08 02:38:45 -0800 | [diff] [blame] | 1815 | pl011_set_termios(struct uart_port *port, struct ktermios *termios, |
| 1816 | struct ktermios *old) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1817 | { |
Daniel Thompson | a5820c2 | 2014-09-03 12:51:55 +0100 | [diff] [blame] | 1818 | struct uart_amba_port *uap = |
| 1819 | container_of(port, struct uart_amba_port, port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1820 | unsigned int lcr_h, old_cr; |
| 1821 | unsigned long flags; |
Russell King | c19f12b | 2010-12-22 17:48:26 +0000 | [diff] [blame] | 1822 | unsigned int baud, quot, clkdiv; |
| 1823 | |
| 1824 | if (uap->vendor->oversampling) |
| 1825 | clkdiv = 8; |
| 1826 | else |
| 1827 | clkdiv = 16; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1828 | |
| 1829 | /* |
| 1830 | * Ask the core to calculate the divisor for us. |
| 1831 | */ |
Linus Walleij | ac3e3fb | 2010-06-02 20:40:22 +0100 | [diff] [blame] | 1832 | baud = uart_get_baud_rate(port, termios, old, 0, |
Russell King | c19f12b | 2010-12-22 17:48:26 +0000 | [diff] [blame] | 1833 | port->uartclk / clkdiv); |
Chanho Min | 89fa28d | 2013-04-03 11:10:37 +0900 | [diff] [blame] | 1834 | #ifdef CONFIG_DMA_ENGINE |
Chanho Min | cb06ff1 | 2013-03-27 18:38:11 +0900 | [diff] [blame] | 1835 | /* |
| 1836 | * Adjust RX DMA polling rate with baud rate if not specified. |
| 1837 | */ |
| 1838 | if (uap->dmarx.auto_poll_rate) |
| 1839 | uap->dmarx.poll_rate = DIV_ROUND_UP(10000000, baud); |
Chanho Min | 89fa28d | 2013-04-03 11:10:37 +0900 | [diff] [blame] | 1840 | #endif |
Linus Walleij | ac3e3fb | 2010-06-02 20:40:22 +0100 | [diff] [blame] | 1841 | |
| 1842 | if (baud > port->uartclk/16) |
| 1843 | quot = DIV_ROUND_CLOSEST(port->uartclk * 8, baud); |
| 1844 | else |
| 1845 | quot = DIV_ROUND_CLOSEST(port->uartclk * 4, baud); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1846 | |
| 1847 | switch (termios->c_cflag & CSIZE) { |
| 1848 | case CS5: |
| 1849 | lcr_h = UART01x_LCRH_WLEN_5; |
| 1850 | break; |
| 1851 | case CS6: |
| 1852 | lcr_h = UART01x_LCRH_WLEN_6; |
| 1853 | break; |
| 1854 | case CS7: |
| 1855 | lcr_h = UART01x_LCRH_WLEN_7; |
| 1856 | break; |
| 1857 | default: // CS8 |
| 1858 | lcr_h = UART01x_LCRH_WLEN_8; |
| 1859 | break; |
| 1860 | } |
| 1861 | if (termios->c_cflag & CSTOPB) |
| 1862 | lcr_h |= UART01x_LCRH_STP2; |
| 1863 | if (termios->c_cflag & PARENB) { |
| 1864 | lcr_h |= UART01x_LCRH_PEN; |
| 1865 | if (!(termios->c_cflag & PARODD)) |
| 1866 | lcr_h |= UART01x_LCRH_EPS; |
| 1867 | } |
Russell King | ffca2b1 | 2010-12-22 17:13:05 +0000 | [diff] [blame] | 1868 | if (uap->fifosize > 1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1869 | lcr_h |= UART01x_LCRH_FEN; |
| 1870 | |
| 1871 | spin_lock_irqsave(&port->lock, flags); |
| 1872 | |
| 1873 | /* |
| 1874 | * Update the per-port timeout. |
| 1875 | */ |
| 1876 | uart_update_timeout(port, termios->c_cflag, baud); |
| 1877 | |
Andre Przywara | ef5a935 | 2015-05-21 17:26:17 +0100 | [diff] [blame] | 1878 | pl011_setup_status_masks(port, termios); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1879 | |
| 1880 | if (UART_ENABLE_MS(port, termios->c_cflag)) |
| 1881 | pl011_enable_ms(port); |
| 1882 | |
| 1883 | /* first, disable everything */ |
Russell King | 7583633 | 2015-11-03 14:50:58 +0000 | [diff] [blame^] | 1884 | old_cr = pl011_read(port->membase, UART011_CR); |
| 1885 | pl011_write(0, port->membase, UART011_CR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1886 | |
Rabin Vincent | 3b43816 | 2010-02-12 06:43:11 +0100 | [diff] [blame] | 1887 | if (termios->c_cflag & CRTSCTS) { |
| 1888 | if (old_cr & UART011_CR_RTS) |
| 1889 | old_cr |= UART011_CR_RTSEN; |
| 1890 | |
| 1891 | old_cr |= UART011_CR_CTSEN; |
| 1892 | uap->autorts = true; |
| 1893 | } else { |
| 1894 | old_cr &= ~(UART011_CR_CTSEN | UART011_CR_RTSEN); |
| 1895 | uap->autorts = false; |
| 1896 | } |
| 1897 | |
Russell King | c19f12b | 2010-12-22 17:48:26 +0000 | [diff] [blame] | 1898 | if (uap->vendor->oversampling) { |
| 1899 | if (baud > port->uartclk / 16) |
Linus Walleij | ac3e3fb | 2010-06-02 20:40:22 +0100 | [diff] [blame] | 1900 | old_cr |= ST_UART011_CR_OVSFACT; |
| 1901 | else |
| 1902 | old_cr &= ~ST_UART011_CR_OVSFACT; |
| 1903 | } |
| 1904 | |
Linus Walleij | c5dd553 | 2012-09-26 17:21:36 +0200 | [diff] [blame] | 1905 | /* |
| 1906 | * Workaround for the ST Micro oversampling variants to |
| 1907 | * increase the bitrate slightly, by lowering the divisor, |
| 1908 | * to avoid delayed sampling of start bit at high speeds, |
| 1909 | * else we see data corruption. |
| 1910 | */ |
| 1911 | if (uap->vendor->oversampling) { |
| 1912 | if ((baud >= 3000000) && (baud < 3250000) && (quot > 1)) |
| 1913 | quot -= 1; |
| 1914 | else if ((baud > 3250000) && (quot > 2)) |
| 1915 | quot -= 2; |
| 1916 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1917 | /* Set baud rate */ |
Russell King | 7583633 | 2015-11-03 14:50:58 +0000 | [diff] [blame^] | 1918 | pl011_write(quot & 0x3f, port->membase, UART011_FBRD); |
| 1919 | pl011_write(quot >> 6, port->membase, UART011_IBRD); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1920 | |
| 1921 | /* |
| 1922 | * ----------v----------v----------v----------v----- |
Linus Walleij | c5dd553 | 2012-09-26 17:21:36 +0200 | [diff] [blame] | 1923 | * NOTE: lcrh_tx and lcrh_rx MUST BE WRITTEN AFTER |
Greg Kroah-Hartman | 0de6cfb | 2015-09-04 09:13:56 -0700 | [diff] [blame] | 1924 | * UART011_FBRD & UART011_IBRD. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1925 | * ----------^----------^----------^----------^----- |
| 1926 | */ |
Jon Medhurst | b60f2f6 | 2013-12-10 10:18:59 +0000 | [diff] [blame] | 1927 | pl011_write_lcr_h(uap, lcr_h); |
Russell King | 7583633 | 2015-11-03 14:50:58 +0000 | [diff] [blame^] | 1928 | pl011_write(old_cr, port->membase, UART011_CR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1929 | |
| 1930 | spin_unlock_irqrestore(&port->lock, flags); |
| 1931 | } |
| 1932 | |
Andre Przywara | 0dd1e24 | 2015-05-21 17:26:23 +0100 | [diff] [blame] | 1933 | static void |
| 1934 | sbsa_uart_set_termios(struct uart_port *port, struct ktermios *termios, |
| 1935 | struct ktermios *old) |
| 1936 | { |
| 1937 | struct uart_amba_port *uap = |
| 1938 | container_of(port, struct uart_amba_port, port); |
| 1939 | unsigned long flags; |
| 1940 | |
| 1941 | tty_termios_encode_baud_rate(termios, uap->fixed_baud, uap->fixed_baud); |
| 1942 | |
| 1943 | /* The SBSA UART only supports 8n1 without hardware flow control. */ |
| 1944 | termios->c_cflag &= ~(CSIZE | CSTOPB | PARENB | PARODD); |
| 1945 | termios->c_cflag &= ~(CMSPAR | CRTSCTS); |
| 1946 | termios->c_cflag |= CS8 | CLOCAL; |
| 1947 | |
| 1948 | spin_lock_irqsave(&port->lock, flags); |
| 1949 | uart_update_timeout(port, CS8, uap->fixed_baud); |
| 1950 | pl011_setup_status_masks(port, termios); |
| 1951 | spin_unlock_irqrestore(&port->lock, flags); |
| 1952 | } |
| 1953 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1954 | static const char *pl011_type(struct uart_port *port) |
| 1955 | { |
Daniel Thompson | a5820c2 | 2014-09-03 12:51:55 +0100 | [diff] [blame] | 1956 | struct uart_amba_port *uap = |
| 1957 | container_of(port, struct uart_amba_port, port); |
Russell King | e8a7ba8 | 2010-12-28 09:16:54 +0000 | [diff] [blame] | 1958 | return uap->port.type == PORT_AMBA ? uap->type : NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1959 | } |
| 1960 | |
| 1961 | /* |
| 1962 | * Release the memory region(s) being used by 'port' |
| 1963 | */ |
Linus Walleij | e643f87 | 2012-06-17 15:44:19 +0200 | [diff] [blame] | 1964 | static void pl011_release_port(struct uart_port *port) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1965 | { |
| 1966 | release_mem_region(port->mapbase, SZ_4K); |
| 1967 | } |
| 1968 | |
| 1969 | /* |
| 1970 | * Request the memory region(s) being used by 'port' |
| 1971 | */ |
Linus Walleij | e643f87 | 2012-06-17 15:44:19 +0200 | [diff] [blame] | 1972 | static int pl011_request_port(struct uart_port *port) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1973 | { |
| 1974 | return request_mem_region(port->mapbase, SZ_4K, "uart-pl011") |
| 1975 | != NULL ? 0 : -EBUSY; |
| 1976 | } |
| 1977 | |
| 1978 | /* |
| 1979 | * Configure/autoconfigure the port. |
| 1980 | */ |
Linus Walleij | e643f87 | 2012-06-17 15:44:19 +0200 | [diff] [blame] | 1981 | static void pl011_config_port(struct uart_port *port, int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1982 | { |
| 1983 | if (flags & UART_CONFIG_TYPE) { |
| 1984 | port->type = PORT_AMBA; |
Linus Walleij | e643f87 | 2012-06-17 15:44:19 +0200 | [diff] [blame] | 1985 | pl011_request_port(port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1986 | } |
| 1987 | } |
| 1988 | |
| 1989 | /* |
| 1990 | * verify the new serial_struct (for TIOCSSERIAL). |
| 1991 | */ |
Linus Walleij | e643f87 | 2012-06-17 15:44:19 +0200 | [diff] [blame] | 1992 | static int pl011_verify_port(struct uart_port *port, struct serial_struct *ser) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1993 | { |
| 1994 | int ret = 0; |
| 1995 | if (ser->type != PORT_UNKNOWN && ser->type != PORT_AMBA) |
| 1996 | ret = -EINVAL; |
Yinghai Lu | a62c413 | 2008-08-19 20:49:55 -0700 | [diff] [blame] | 1997 | if (ser->irq < 0 || ser->irq >= nr_irqs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1998 | ret = -EINVAL; |
| 1999 | if (ser->baud_base < 9600) |
| 2000 | ret = -EINVAL; |
| 2001 | return ret; |
| 2002 | } |
| 2003 | |
| 2004 | static struct uart_ops amba_pl011_pops = { |
Linus Walleij | e643f87 | 2012-06-17 15:44:19 +0200 | [diff] [blame] | 2005 | .tx_empty = pl011_tx_empty, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2006 | .set_mctrl = pl011_set_mctrl, |
Linus Walleij | e643f87 | 2012-06-17 15:44:19 +0200 | [diff] [blame] | 2007 | .get_mctrl = pl011_get_mctrl, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2008 | .stop_tx = pl011_stop_tx, |
| 2009 | .start_tx = pl011_start_tx, |
| 2010 | .stop_rx = pl011_stop_rx, |
| 2011 | .enable_ms = pl011_enable_ms, |
| 2012 | .break_ctl = pl011_break_ctl, |
| 2013 | .startup = pl011_startup, |
| 2014 | .shutdown = pl011_shutdown, |
Russell King | 68b65f7 | 2010-12-22 17:24:39 +0000 | [diff] [blame] | 2015 | .flush_buffer = pl011_dma_flush_buffer, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2016 | .set_termios = pl011_set_termios, |
| 2017 | .type = pl011_type, |
Linus Walleij | e643f87 | 2012-06-17 15:44:19 +0200 | [diff] [blame] | 2018 | .release_port = pl011_release_port, |
| 2019 | .request_port = pl011_request_port, |
| 2020 | .config_port = pl011_config_port, |
| 2021 | .verify_port = pl011_verify_port, |
Jason Wessel | 84b5ae1 | 2008-02-20 13:33:39 -0600 | [diff] [blame] | 2022 | #ifdef CONFIG_CONSOLE_POLL |
Anton Vorontsov | b3564c2 | 2012-09-24 14:27:54 -0700 | [diff] [blame] | 2023 | .poll_init = pl011_hwinit, |
Linus Walleij | e643f87 | 2012-06-17 15:44:19 +0200 | [diff] [blame] | 2024 | .poll_get_char = pl011_get_poll_char, |
| 2025 | .poll_put_char = pl011_put_poll_char, |
Jason Wessel | 84b5ae1 | 2008-02-20 13:33:39 -0600 | [diff] [blame] | 2026 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2027 | }; |
| 2028 | |
Andre Przywara | 0dd1e24 | 2015-05-21 17:26:23 +0100 | [diff] [blame] | 2029 | static void sbsa_uart_set_mctrl(struct uart_port *port, unsigned int mctrl) |
| 2030 | { |
| 2031 | } |
| 2032 | |
| 2033 | static unsigned int sbsa_uart_get_mctrl(struct uart_port *port) |
| 2034 | { |
| 2035 | return 0; |
| 2036 | } |
| 2037 | |
| 2038 | static const struct uart_ops sbsa_uart_pops = { |
| 2039 | .tx_empty = pl011_tx_empty, |
| 2040 | .set_mctrl = sbsa_uart_set_mctrl, |
| 2041 | .get_mctrl = sbsa_uart_get_mctrl, |
| 2042 | .stop_tx = pl011_stop_tx, |
| 2043 | .start_tx = pl011_start_tx, |
| 2044 | .stop_rx = pl011_stop_rx, |
| 2045 | .startup = sbsa_uart_startup, |
| 2046 | .shutdown = sbsa_uart_shutdown, |
| 2047 | .set_termios = sbsa_uart_set_termios, |
| 2048 | .type = pl011_type, |
| 2049 | .release_port = pl011_release_port, |
| 2050 | .request_port = pl011_request_port, |
| 2051 | .config_port = pl011_config_port, |
| 2052 | .verify_port = pl011_verify_port, |
| 2053 | #ifdef CONFIG_CONSOLE_POLL |
| 2054 | .poll_init = pl011_hwinit, |
| 2055 | .poll_get_char = pl011_get_poll_char, |
| 2056 | .poll_put_char = pl011_put_poll_char, |
| 2057 | #endif |
| 2058 | }; |
| 2059 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2060 | static struct uart_amba_port *amba_ports[UART_NR]; |
| 2061 | |
| 2062 | #ifdef CONFIG_SERIAL_AMBA_PL011_CONSOLE |
| 2063 | |
Russell King | d358788 | 2006-03-20 20:00:09 +0000 | [diff] [blame] | 2064 | static void pl011_console_putchar(struct uart_port *port, int ch) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2065 | { |
Daniel Thompson | a5820c2 | 2014-09-03 12:51:55 +0100 | [diff] [blame] | 2066 | struct uart_amba_port *uap = |
| 2067 | container_of(port, struct uart_amba_port, port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2068 | |
Russell King | 7583633 | 2015-11-03 14:50:58 +0000 | [diff] [blame^] | 2069 | while (pl011_read(uap->port.membase, UART01x_FR) & UART01x_FR_TXFF) |
Russell King | d358788 | 2006-03-20 20:00:09 +0000 | [diff] [blame] | 2070 | barrier(); |
Russell King | 7583633 | 2015-11-03 14:50:58 +0000 | [diff] [blame^] | 2071 | pl011_write(ch, uap->port.membase, UART01x_DR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2072 | } |
| 2073 | |
| 2074 | static void |
| 2075 | pl011_console_write(struct console *co, const char *s, unsigned int count) |
| 2076 | { |
| 2077 | struct uart_amba_port *uap = amba_ports[co->index]; |
Andre Przywara | 71eec48 | 2015-05-21 17:26:21 +0100 | [diff] [blame] | 2078 | unsigned int status, old_cr = 0, new_cr; |
Rabin Vincent | ef605fd | 2012-01-17 11:52:28 +0100 | [diff] [blame] | 2079 | unsigned long flags; |
| 2080 | int locked = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2081 | |
| 2082 | clk_enable(uap->clk); |
| 2083 | |
Rabin Vincent | ef605fd | 2012-01-17 11:52:28 +0100 | [diff] [blame] | 2084 | local_irq_save(flags); |
| 2085 | if (uap->port.sysrq) |
| 2086 | locked = 0; |
| 2087 | else if (oops_in_progress) |
| 2088 | locked = spin_trylock(&uap->port.lock); |
| 2089 | else |
| 2090 | spin_lock(&uap->port.lock); |
| 2091 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2092 | /* |
| 2093 | * First save the CR then disable the interrupts |
| 2094 | */ |
Andre Przywara | 71eec48 | 2015-05-21 17:26:21 +0100 | [diff] [blame] | 2095 | if (!uap->vendor->always_enabled) { |
Russell King | 7583633 | 2015-11-03 14:50:58 +0000 | [diff] [blame^] | 2096 | old_cr = pl011_read(uap->port.membase, UART011_CR); |
Andre Przywara | 71eec48 | 2015-05-21 17:26:21 +0100 | [diff] [blame] | 2097 | new_cr = old_cr & ~UART011_CR_CTSEN; |
| 2098 | new_cr |= UART01x_CR_UARTEN | UART011_CR_TXE; |
Russell King | 7583633 | 2015-11-03 14:50:58 +0000 | [diff] [blame^] | 2099 | pl011_write(new_cr, uap->port.membase, UART011_CR); |
Andre Przywara | 71eec48 | 2015-05-21 17:26:21 +0100 | [diff] [blame] | 2100 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2101 | |
Russell King | d358788 | 2006-03-20 20:00:09 +0000 | [diff] [blame] | 2102 | uart_console_write(&uap->port, s, count, pl011_console_putchar); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2103 | |
| 2104 | /* |
| 2105 | * Finally, wait for transmitter to become empty |
| 2106 | * and restore the TCR |
| 2107 | */ |
| 2108 | do { |
Russell King | 7583633 | 2015-11-03 14:50:58 +0000 | [diff] [blame^] | 2109 | status = pl011_read(uap->port.membase, UART01x_FR); |
Greg Kroah-Hartman | 062a68a | 2015-09-04 09:11:24 -0700 | [diff] [blame] | 2110 | } while (status & UART01x_FR_BUSY); |
Andre Przywara | 71eec48 | 2015-05-21 17:26:21 +0100 | [diff] [blame] | 2111 | if (!uap->vendor->always_enabled) |
Russell King | 7583633 | 2015-11-03 14:50:58 +0000 | [diff] [blame^] | 2112 | pl011_write(old_cr, uap->port.membase, UART011_CR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2113 | |
Rabin Vincent | ef605fd | 2012-01-17 11:52:28 +0100 | [diff] [blame] | 2114 | if (locked) |
| 2115 | spin_unlock(&uap->port.lock); |
| 2116 | local_irq_restore(flags); |
| 2117 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2118 | clk_disable(uap->clk); |
| 2119 | } |
| 2120 | |
| 2121 | static void __init |
| 2122 | pl011_console_get_options(struct uart_amba_port *uap, int *baud, |
| 2123 | int *parity, int *bits) |
| 2124 | { |
Russell King | 7583633 | 2015-11-03 14:50:58 +0000 | [diff] [blame^] | 2125 | if (pl011_read(uap->port.membase, UART011_CR) & UART01x_CR_UARTEN) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2126 | unsigned int lcr_h, ibrd, fbrd; |
| 2127 | |
Russell King | 7583633 | 2015-11-03 14:50:58 +0000 | [diff] [blame^] | 2128 | lcr_h = pl011_read(uap->port.membase, uap->lcrh_tx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2129 | |
| 2130 | *parity = 'n'; |
| 2131 | if (lcr_h & UART01x_LCRH_PEN) { |
| 2132 | if (lcr_h & UART01x_LCRH_EPS) |
| 2133 | *parity = 'e'; |
| 2134 | else |
| 2135 | *parity = 'o'; |
| 2136 | } |
| 2137 | |
| 2138 | if ((lcr_h & 0x60) == UART01x_LCRH_WLEN_7) |
| 2139 | *bits = 7; |
| 2140 | else |
| 2141 | *bits = 8; |
| 2142 | |
Russell King | 7583633 | 2015-11-03 14:50:58 +0000 | [diff] [blame^] | 2143 | ibrd = pl011_read(uap->port.membase, UART011_IBRD); |
| 2144 | fbrd = pl011_read(uap->port.membase, UART011_FBRD); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2145 | |
| 2146 | *baud = uap->port.uartclk * 4 / (64 * ibrd + fbrd); |
Linus Walleij | ac3e3fb | 2010-06-02 20:40:22 +0100 | [diff] [blame] | 2147 | |
Russell King | c19f12b | 2010-12-22 17:48:26 +0000 | [diff] [blame] | 2148 | if (uap->vendor->oversampling) { |
Russell King | 7583633 | 2015-11-03 14:50:58 +0000 | [diff] [blame^] | 2149 | if (pl011_read(uap->port.membase, UART011_CR) |
Linus Walleij | ac3e3fb | 2010-06-02 20:40:22 +0100 | [diff] [blame] | 2150 | & ST_UART011_CR_OVSFACT) |
| 2151 | *baud *= 2; |
| 2152 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2153 | } |
| 2154 | } |
| 2155 | |
| 2156 | static int __init pl011_console_setup(struct console *co, char *options) |
| 2157 | { |
| 2158 | struct uart_amba_port *uap; |
| 2159 | int baud = 38400; |
| 2160 | int bits = 8; |
| 2161 | int parity = 'n'; |
| 2162 | int flow = 'n'; |
Russell King | 4b4851c | 2011-09-22 11:35:30 +0100 | [diff] [blame] | 2163 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2164 | |
| 2165 | /* |
| 2166 | * Check whether an invalid uart number has been specified, and |
| 2167 | * if so, search for the first available port that does have |
| 2168 | * console support. |
| 2169 | */ |
| 2170 | if (co->index >= UART_NR) |
| 2171 | co->index = 0; |
| 2172 | uap = amba_ports[co->index]; |
Russell King | d28122a | 2007-01-22 18:59:42 +0000 | [diff] [blame] | 2173 | if (!uap) |
| 2174 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2175 | |
Linus Walleij | 78d80c5 | 2012-05-23 21:18:46 +0200 | [diff] [blame] | 2176 | /* Allow pins to be muxed in and configured */ |
Linus Walleij | 2b996fc | 2013-06-05 15:36:42 +0200 | [diff] [blame] | 2177 | pinctrl_pm_select_default_state(uap->port.dev); |
Linus Walleij | 78d80c5 | 2012-05-23 21:18:46 +0200 | [diff] [blame] | 2178 | |
Russell King | 4b4851c | 2011-09-22 11:35:30 +0100 | [diff] [blame] | 2179 | ret = clk_prepare(uap->clk); |
| 2180 | if (ret) |
| 2181 | return ret; |
| 2182 | |
Jingoo Han | 574de55 | 2013-07-30 17:06:57 +0900 | [diff] [blame] | 2183 | if (dev_get_platdata(uap->port.dev)) { |
Shreshtha Kumar Sahu | c16d51a | 2011-06-13 10:11:33 +0200 | [diff] [blame] | 2184 | struct amba_pl011_data *plat; |
| 2185 | |
Jingoo Han | 574de55 | 2013-07-30 17:06:57 +0900 | [diff] [blame] | 2186 | plat = dev_get_platdata(uap->port.dev); |
Shreshtha Kumar Sahu | c16d51a | 2011-06-13 10:11:33 +0200 | [diff] [blame] | 2187 | if (plat->init) |
| 2188 | plat->init(); |
| 2189 | } |
| 2190 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2191 | uap->port.uartclk = clk_get_rate(uap->clk); |
| 2192 | |
Andre Przywara | cefc2d1 | 2015-05-21 17:26:22 +0100 | [diff] [blame] | 2193 | if (uap->vendor->fixed_options) { |
| 2194 | baud = uap->fixed_baud; |
| 2195 | } else { |
| 2196 | if (options) |
| 2197 | uart_parse_options(options, |
| 2198 | &baud, &parity, &bits, &flow); |
| 2199 | else |
| 2200 | pl011_console_get_options(uap, &baud, &parity, &bits); |
| 2201 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2202 | |
| 2203 | return uart_set_options(&uap->port, co, baud, parity, bits, flow); |
| 2204 | } |
| 2205 | |
Vincent Sanders | 2d93486 | 2005-09-14 22:36:03 +0100 | [diff] [blame] | 2206 | static struct uart_driver amba_reg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2207 | static struct console amba_console = { |
| 2208 | .name = "ttyAMA", |
| 2209 | .write = pl011_console_write, |
| 2210 | .device = uart_console_device, |
| 2211 | .setup = pl011_console_setup, |
| 2212 | .flags = CON_PRINTBUFFER, |
| 2213 | .index = -1, |
| 2214 | .data = &amba_reg, |
| 2215 | }; |
| 2216 | |
| 2217 | #define AMBA_CONSOLE (&amba_console) |
Rob Herring | 0d3c673 | 2014-04-18 17:19:57 -0500 | [diff] [blame] | 2218 | |
| 2219 | static void pl011_putc(struct uart_port *port, int c) |
| 2220 | { |
Greg Kroah-Hartman | 0de6cfb | 2015-09-04 09:13:56 -0700 | [diff] [blame] | 2221 | while (readl(port->membase + UART01x_FR) & UART01x_FR_TXFF) |
Rob Herring | 0d3c673 | 2014-04-18 17:19:57 -0500 | [diff] [blame] | 2222 | ; |
Greg Kroah-Hartman | 0de6cfb | 2015-09-04 09:13:56 -0700 | [diff] [blame] | 2223 | writeb(c, port->membase + UART01x_DR); |
| 2224 | while (readl(port->membase + UART01x_FR) & UART01x_FR_BUSY) |
Rob Herring | 0d3c673 | 2014-04-18 17:19:57 -0500 | [diff] [blame] | 2225 | ; |
| 2226 | } |
| 2227 | |
| 2228 | static void pl011_early_write(struct console *con, const char *s, unsigned n) |
| 2229 | { |
| 2230 | struct earlycon_device *dev = con->data; |
| 2231 | |
| 2232 | uart_console_write(&dev->port, s, n, pl011_putc); |
| 2233 | } |
| 2234 | |
| 2235 | static int __init pl011_early_console_setup(struct earlycon_device *device, |
| 2236 | const char *opt) |
| 2237 | { |
| 2238 | if (!device->port.membase) |
| 2239 | return -ENODEV; |
| 2240 | |
| 2241 | device->con->write = pl011_early_write; |
| 2242 | return 0; |
| 2243 | } |
| 2244 | EARLYCON_DECLARE(pl011, pl011_early_console_setup); |
Rob Herring | 45e0f0f | 2014-03-27 08:08:03 -0500 | [diff] [blame] | 2245 | OF_EARLYCON_DECLARE(pl011, "arm,pl011", pl011_early_console_setup); |
Rob Herring | 0d3c673 | 2014-04-18 17:19:57 -0500 | [diff] [blame] | 2246 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2247 | #else |
| 2248 | #define AMBA_CONSOLE NULL |
| 2249 | #endif |
| 2250 | |
| 2251 | static struct uart_driver amba_reg = { |
| 2252 | .owner = THIS_MODULE, |
| 2253 | .driver_name = "ttyAMA", |
| 2254 | .dev_name = "ttyAMA", |
| 2255 | .major = SERIAL_AMBA_MAJOR, |
| 2256 | .minor = SERIAL_AMBA_MINOR, |
| 2257 | .nr = UART_NR, |
| 2258 | .cons = AMBA_CONSOLE, |
| 2259 | }; |
| 2260 | |
Matthew Leach | 32614aa | 2012-08-28 16:41:28 +0100 | [diff] [blame] | 2261 | static int pl011_probe_dt_alias(int index, struct device *dev) |
| 2262 | { |
| 2263 | struct device_node *np; |
| 2264 | static bool seen_dev_with_alias = false; |
| 2265 | static bool seen_dev_without_alias = false; |
| 2266 | int ret = index; |
| 2267 | |
| 2268 | if (!IS_ENABLED(CONFIG_OF)) |
| 2269 | return ret; |
| 2270 | |
| 2271 | np = dev->of_node; |
| 2272 | if (!np) |
| 2273 | return ret; |
| 2274 | |
| 2275 | ret = of_alias_get_id(np, "serial"); |
| 2276 | if (IS_ERR_VALUE(ret)) { |
| 2277 | seen_dev_without_alias = true; |
| 2278 | ret = index; |
| 2279 | } else { |
| 2280 | seen_dev_with_alias = true; |
| 2281 | if (ret >= ARRAY_SIZE(amba_ports) || amba_ports[ret] != NULL) { |
| 2282 | dev_warn(dev, "requested serial port %d not available.\n", ret); |
| 2283 | ret = index; |
| 2284 | } |
| 2285 | } |
| 2286 | |
| 2287 | if (seen_dev_with_alias && seen_dev_without_alias) |
| 2288 | dev_warn(dev, "aliased and non-aliased serial devices found in device tree. Serial port enumeration may be unpredictable.\n"); |
| 2289 | |
| 2290 | return ret; |
| 2291 | } |
| 2292 | |
Andre Przywara | 49bb3c8 | 2015-05-21 17:26:14 +0100 | [diff] [blame] | 2293 | /* unregisters the driver also if no more ports are left */ |
| 2294 | static void pl011_unregister_port(struct uart_amba_port *uap) |
| 2295 | { |
| 2296 | int i; |
| 2297 | bool busy = false; |
| 2298 | |
| 2299 | for (i = 0; i < ARRAY_SIZE(amba_ports); i++) { |
| 2300 | if (amba_ports[i] == uap) |
| 2301 | amba_ports[i] = NULL; |
| 2302 | else if (amba_ports[i]) |
| 2303 | busy = true; |
| 2304 | } |
| 2305 | pl011_dma_remove(uap); |
| 2306 | if (!busy) |
| 2307 | uart_unregister_driver(&amba_reg); |
| 2308 | } |
| 2309 | |
Andre Przywara | 3873e2d | 2015-05-21 17:26:18 +0100 | [diff] [blame] | 2310 | static int pl011_find_free_port(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2311 | { |
Andre Przywara | 3873e2d | 2015-05-21 17:26:18 +0100 | [diff] [blame] | 2312 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2313 | |
| 2314 | for (i = 0; i < ARRAY_SIZE(amba_ports); i++) |
| 2315 | if (amba_ports[i] == NULL) |
Andre Przywara | 3873e2d | 2015-05-21 17:26:18 +0100 | [diff] [blame] | 2316 | return i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2317 | |
Andre Przywara | 3873e2d | 2015-05-21 17:26:18 +0100 | [diff] [blame] | 2318 | return -EBUSY; |
| 2319 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2320 | |
Andre Przywara | 3873e2d | 2015-05-21 17:26:18 +0100 | [diff] [blame] | 2321 | static int pl011_setup_port(struct device *dev, struct uart_amba_port *uap, |
| 2322 | struct resource *mmiobase, int index) |
| 2323 | { |
| 2324 | void __iomem *base; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2325 | |
Andre Przywara | 3873e2d | 2015-05-21 17:26:18 +0100 | [diff] [blame] | 2326 | base = devm_ioremap_resource(dev, mmiobase); |
Krzysztof Kozlowski | 97a60ea | 2015-07-09 22:21:41 +0900 | [diff] [blame] | 2327 | if (IS_ERR(base)) |
| 2328 | return PTR_ERR(base); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2329 | |
Andre Przywara | 3873e2d | 2015-05-21 17:26:18 +0100 | [diff] [blame] | 2330 | index = pl011_probe_dt_alias(index, dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2331 | |
Shreshtha Kumar Sahu | d8d8ffa | 2012-01-18 15:53:59 +0530 | [diff] [blame] | 2332 | uap->old_cr = 0; |
Andre Przywara | 3873e2d | 2015-05-21 17:26:18 +0100 | [diff] [blame] | 2333 | uap->port.dev = dev; |
| 2334 | uap->port.mapbase = mmiobase->start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2335 | uap->port.membase = base; |
| 2336 | uap->port.iotype = UPIO_MEM; |
Russell King | ffca2b1 | 2010-12-22 17:13:05 +0000 | [diff] [blame] | 2337 | uap->port.fifosize = uap->fifosize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2338 | uap->port.flags = UPF_BOOT_AUTOCONF; |
Andre Przywara | 3873e2d | 2015-05-21 17:26:18 +0100 | [diff] [blame] | 2339 | uap->port.line = index; |
| 2340 | |
| 2341 | amba_ports[index] = uap; |
| 2342 | |
| 2343 | return 0; |
| 2344 | } |
| 2345 | |
| 2346 | static int pl011_register_port(struct uart_amba_port *uap) |
| 2347 | { |
| 2348 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2349 | |
Linus Walleij | c3d8b76 | 2012-03-21 20:15:18 +0100 | [diff] [blame] | 2350 | /* Ensure interrupts from this UART are masked and cleared */ |
Russell King | 7583633 | 2015-11-03 14:50:58 +0000 | [diff] [blame^] | 2351 | pl011_write(0, uap->port.membase, UART011_IMSC); |
| 2352 | pl011_write(0xffff, uap->port.membase, UART011_ICR); |
Linus Walleij | c3d8b76 | 2012-03-21 20:15:18 +0100 | [diff] [blame] | 2353 | |
Tushar Behera | ef2889f | 2014-01-20 14:32:35 +0530 | [diff] [blame] | 2354 | if (!amba_reg.state) { |
| 2355 | ret = uart_register_driver(&amba_reg); |
| 2356 | if (ret < 0) { |
Andre Przywara | 3873e2d | 2015-05-21 17:26:18 +0100 | [diff] [blame] | 2357 | dev_err(uap->port.dev, |
Jorge Ramirez-Ortiz | 1c9be31 | 2015-03-06 13:05:40 -0500 | [diff] [blame] | 2358 | "Failed to register AMBA-PL011 driver\n"); |
Tushar Behera | ef2889f | 2014-01-20 14:32:35 +0530 | [diff] [blame] | 2359 | return ret; |
| 2360 | } |
| 2361 | } |
| 2362 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2363 | ret = uart_add_one_port(&amba_reg, &uap->port); |
Andre Przywara | 49bb3c8 | 2015-05-21 17:26:14 +0100 | [diff] [blame] | 2364 | if (ret) |
| 2365 | pl011_unregister_port(uap); |
Tushar Behera | 7f6d942 | 2014-06-26 15:35:35 +0530 | [diff] [blame] | 2366 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2367 | return ret; |
| 2368 | } |
| 2369 | |
Andre Przywara | 3873e2d | 2015-05-21 17:26:18 +0100 | [diff] [blame] | 2370 | static int pl011_probe(struct amba_device *dev, const struct amba_id *id) |
| 2371 | { |
| 2372 | struct uart_amba_port *uap; |
| 2373 | struct vendor_data *vendor = id->data; |
| 2374 | int portnr, ret; |
| 2375 | |
| 2376 | portnr = pl011_find_free_port(); |
| 2377 | if (portnr < 0) |
| 2378 | return portnr; |
| 2379 | |
| 2380 | uap = devm_kzalloc(&dev->dev, sizeof(struct uart_amba_port), |
| 2381 | GFP_KERNEL); |
| 2382 | if (!uap) |
| 2383 | return -ENOMEM; |
| 2384 | |
| 2385 | uap->clk = devm_clk_get(&dev->dev, NULL); |
| 2386 | if (IS_ERR(uap->clk)) |
| 2387 | return PTR_ERR(uap->clk); |
| 2388 | |
| 2389 | uap->vendor = vendor; |
| 2390 | uap->lcrh_rx = vendor->lcrh_rx; |
| 2391 | uap->lcrh_tx = vendor->lcrh_tx; |
| 2392 | uap->fifosize = vendor->get_fifosize(dev); |
| 2393 | uap->port.irq = dev->irq[0]; |
| 2394 | uap->port.ops = &amba_pl011_pops; |
| 2395 | |
| 2396 | snprintf(uap->type, sizeof(uap->type), "PL011 rev%u", amba_rev(dev)); |
| 2397 | |
| 2398 | ret = pl011_setup_port(&dev->dev, uap, &dev->res, portnr); |
| 2399 | if (ret) |
| 2400 | return ret; |
| 2401 | |
| 2402 | amba_set_drvdata(dev, uap); |
| 2403 | |
| 2404 | return pl011_register_port(uap); |
| 2405 | } |
| 2406 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2407 | static int pl011_remove(struct amba_device *dev) |
| 2408 | { |
| 2409 | struct uart_amba_port *uap = amba_get_drvdata(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2410 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2411 | uart_remove_one_port(&amba_reg, &uap->port); |
Andre Przywara | 49bb3c8 | 2015-05-21 17:26:14 +0100 | [diff] [blame] | 2412 | pl011_unregister_port(uap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2413 | return 0; |
| 2414 | } |
| 2415 | |
Ulf Hansson | d0ce850 | 2013-12-03 11:04:28 +0100 | [diff] [blame] | 2416 | #ifdef CONFIG_PM_SLEEP |
| 2417 | static int pl011_suspend(struct device *dev) |
Leo Chen | b736b89 | 2009-07-28 23:43:33 +0100 | [diff] [blame] | 2418 | { |
Ulf Hansson | d0ce850 | 2013-12-03 11:04:28 +0100 | [diff] [blame] | 2419 | struct uart_amba_port *uap = dev_get_drvdata(dev); |
Leo Chen | b736b89 | 2009-07-28 23:43:33 +0100 | [diff] [blame] | 2420 | |
| 2421 | if (!uap) |
| 2422 | return -EINVAL; |
| 2423 | |
| 2424 | return uart_suspend_port(&amba_reg, &uap->port); |
| 2425 | } |
| 2426 | |
Ulf Hansson | d0ce850 | 2013-12-03 11:04:28 +0100 | [diff] [blame] | 2427 | static int pl011_resume(struct device *dev) |
Leo Chen | b736b89 | 2009-07-28 23:43:33 +0100 | [diff] [blame] | 2428 | { |
Ulf Hansson | d0ce850 | 2013-12-03 11:04:28 +0100 | [diff] [blame] | 2429 | struct uart_amba_port *uap = dev_get_drvdata(dev); |
Leo Chen | b736b89 | 2009-07-28 23:43:33 +0100 | [diff] [blame] | 2430 | |
| 2431 | if (!uap) |
| 2432 | return -EINVAL; |
| 2433 | |
| 2434 | return uart_resume_port(&amba_reg, &uap->port); |
| 2435 | } |
| 2436 | #endif |
| 2437 | |
Ulf Hansson | d0ce850 | 2013-12-03 11:04:28 +0100 | [diff] [blame] | 2438 | static SIMPLE_DEV_PM_OPS(pl011_dev_pm_ops, pl011_suspend, pl011_resume); |
| 2439 | |
Andre Przywara | 0dd1e24 | 2015-05-21 17:26:23 +0100 | [diff] [blame] | 2440 | static int sbsa_uart_probe(struct platform_device *pdev) |
| 2441 | { |
| 2442 | struct uart_amba_port *uap; |
| 2443 | struct resource *r; |
| 2444 | int portnr, ret; |
| 2445 | int baudrate; |
| 2446 | |
| 2447 | /* |
| 2448 | * Check the mandatory baud rate parameter in the DT node early |
| 2449 | * so that we can easily exit with the error. |
| 2450 | */ |
| 2451 | if (pdev->dev.of_node) { |
| 2452 | struct device_node *np = pdev->dev.of_node; |
| 2453 | |
| 2454 | ret = of_property_read_u32(np, "current-speed", &baudrate); |
| 2455 | if (ret) |
| 2456 | return ret; |
| 2457 | } else { |
| 2458 | baudrate = 115200; |
| 2459 | } |
| 2460 | |
| 2461 | portnr = pl011_find_free_port(); |
| 2462 | if (portnr < 0) |
| 2463 | return portnr; |
| 2464 | |
| 2465 | uap = devm_kzalloc(&pdev->dev, sizeof(struct uart_amba_port), |
| 2466 | GFP_KERNEL); |
| 2467 | if (!uap) |
| 2468 | return -ENOMEM; |
| 2469 | |
| 2470 | uap->vendor = &vendor_sbsa; |
| 2471 | uap->fifosize = 32; |
| 2472 | uap->port.irq = platform_get_irq(pdev, 0); |
| 2473 | uap->port.ops = &sbsa_uart_pops; |
| 2474 | uap->fixed_baud = baudrate; |
| 2475 | |
| 2476 | snprintf(uap->type, sizeof(uap->type), "SBSA"); |
| 2477 | |
| 2478 | r = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 2479 | |
| 2480 | ret = pl011_setup_port(&pdev->dev, uap, r, portnr); |
| 2481 | if (ret) |
| 2482 | return ret; |
| 2483 | |
| 2484 | platform_set_drvdata(pdev, uap); |
| 2485 | |
| 2486 | return pl011_register_port(uap); |
| 2487 | } |
| 2488 | |
| 2489 | static int sbsa_uart_remove(struct platform_device *pdev) |
| 2490 | { |
| 2491 | struct uart_amba_port *uap = platform_get_drvdata(pdev); |
| 2492 | |
| 2493 | uart_remove_one_port(&amba_reg, &uap->port); |
| 2494 | pl011_unregister_port(uap); |
| 2495 | return 0; |
| 2496 | } |
| 2497 | |
| 2498 | static const struct of_device_id sbsa_uart_of_match[] = { |
| 2499 | { .compatible = "arm,sbsa-uart", }, |
| 2500 | {}, |
| 2501 | }; |
| 2502 | MODULE_DEVICE_TABLE(of, sbsa_uart_of_match); |
| 2503 | |
Graeme Gregory | 3db9ab0 | 2015-05-21 17:26:24 +0100 | [diff] [blame] | 2504 | static const struct acpi_device_id sbsa_uart_acpi_match[] = { |
| 2505 | { "ARMH0011", 0 }, |
| 2506 | {}, |
| 2507 | }; |
| 2508 | MODULE_DEVICE_TABLE(acpi, sbsa_uart_acpi_match); |
| 2509 | |
Andre Przywara | 0dd1e24 | 2015-05-21 17:26:23 +0100 | [diff] [blame] | 2510 | static struct platform_driver arm_sbsa_uart_platform_driver = { |
| 2511 | .probe = sbsa_uart_probe, |
| 2512 | .remove = sbsa_uart_remove, |
| 2513 | .driver = { |
| 2514 | .name = "sbsa-uart", |
| 2515 | .of_match_table = of_match_ptr(sbsa_uart_of_match), |
Graeme Gregory | 3db9ab0 | 2015-05-21 17:26:24 +0100 | [diff] [blame] | 2516 | .acpi_match_table = ACPI_PTR(sbsa_uart_acpi_match), |
Andre Przywara | 0dd1e24 | 2015-05-21 17:26:23 +0100 | [diff] [blame] | 2517 | }, |
| 2518 | }; |
| 2519 | |
Russell King | 2c39c9e | 2010-07-27 08:50:16 +0100 | [diff] [blame] | 2520 | static struct amba_id pl011_ids[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2521 | { |
| 2522 | .id = 0x00041011, |
| 2523 | .mask = 0x000fffff, |
Alessandro Rubini | 5926a29 | 2009-06-04 17:43:04 +0100 | [diff] [blame] | 2524 | .data = &vendor_arm, |
| 2525 | }, |
| 2526 | { |
| 2527 | .id = 0x00380802, |
| 2528 | .mask = 0x00ffffff, |
| 2529 | .data = &vendor_st, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2530 | }, |
| 2531 | { 0, 0 }, |
| 2532 | }; |
| 2533 | |
Dave Martin | 60f7a33 | 2011-10-05 15:15:22 +0100 | [diff] [blame] | 2534 | MODULE_DEVICE_TABLE(amba, pl011_ids); |
| 2535 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2536 | static struct amba_driver pl011_driver = { |
| 2537 | .drv = { |
| 2538 | .name = "uart-pl011", |
Ulf Hansson | d0ce850 | 2013-12-03 11:04:28 +0100 | [diff] [blame] | 2539 | .pm = &pl011_dev_pm_ops, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2540 | }, |
| 2541 | .id_table = pl011_ids, |
| 2542 | .probe = pl011_probe, |
| 2543 | .remove = pl011_remove, |
| 2544 | }; |
| 2545 | |
| 2546 | static int __init pl011_init(void) |
| 2547 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2548 | printk(KERN_INFO "Serial: AMBA PL011 UART driver\n"); |
| 2549 | |
Andre Przywara | 0dd1e24 | 2015-05-21 17:26:23 +0100 | [diff] [blame] | 2550 | if (platform_driver_register(&arm_sbsa_uart_platform_driver)) |
| 2551 | pr_warn("could not register SBSA UART platform driver\n"); |
Greg Kroah-Hartman | 062a68a | 2015-09-04 09:11:24 -0700 | [diff] [blame] | 2552 | return amba_driver_register(&pl011_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2553 | } |
| 2554 | |
| 2555 | static void __exit pl011_exit(void) |
| 2556 | { |
Andre Przywara | 0dd1e24 | 2015-05-21 17:26:23 +0100 | [diff] [blame] | 2557 | platform_driver_unregister(&arm_sbsa_uart_platform_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2558 | amba_driver_unregister(&pl011_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2559 | } |
| 2560 | |
Alessandro Rubini | 4dd9e74 | 2009-05-05 05:54:13 +0100 | [diff] [blame] | 2561 | /* |
| 2562 | * While this can be a module, if builtin it's most likely the console |
| 2563 | * So let's leave module_exit but move module_init to an earlier place |
| 2564 | */ |
| 2565 | arch_initcall(pl011_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2566 | module_exit(pl011_exit); |
| 2567 | |
| 2568 | MODULE_AUTHOR("ARM Ltd/Deep Blue Solutions Ltd"); |
| 2569 | MODULE_DESCRIPTION("ARM AMBA serial port driver"); |
| 2570 | MODULE_LICENSE("GPL"); |