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