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