Greg Kroah-Hartman | e3b3d0f | 2017-11-06 18:11:51 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Robert Love | 04896a7 | 2009-06-22 18:43:11 +0100 | [diff] [blame] | 2 | /* |
Jovi Zhang | 99edb3d | 2011-03-30 05:30:41 -0400 | [diff] [blame] | 3 | * Driver for msm7k serial device and console |
Robert Love | 04896a7 | 2009-06-22 18:43:11 +0100 | [diff] [blame] | 4 | * |
| 5 | * Copyright (C) 2007 Google, Inc. |
| 6 | * Author: Robert Love <rlove@google.com> |
Stepan Moskovchenko | ec8f29e | 2010-12-21 12:38:05 -0800 | [diff] [blame] | 7 | * Copyright (c) 2011, Code Aurora Forum. All rights reserved. |
Robert Love | 04896a7 | 2009-06-22 18:43:11 +0100 | [diff] [blame] | 8 | */ |
| 9 | |
Stephen Boyd | e4276ff | 2016-05-11 18:02:28 -0700 | [diff] [blame] | 10 | #include <linux/kernel.h> |
David Brown | cfdad2a | 2011-08-04 01:55:24 -0700 | [diff] [blame] | 11 | #include <linux/atomic.h> |
Ivan T. Ivanov | 3a878c4 | 2015-09-30 15:27:01 +0300 | [diff] [blame] | 12 | #include <linux/dma-mapping.h> |
| 13 | #include <linux/dmaengine.h> |
Robert Love | 04896a7 | 2009-06-22 18:43:11 +0100 | [diff] [blame] | 14 | #include <linux/module.h> |
| 15 | #include <linux/io.h> |
| 16 | #include <linux/ioport.h> |
Stephen Boyd | e4276ff | 2016-05-11 18:02:28 -0700 | [diff] [blame] | 17 | #include <linux/interrupt.h> |
Robert Love | 04896a7 | 2009-06-22 18:43:11 +0100 | [diff] [blame] | 18 | #include <linux/init.h> |
| 19 | #include <linux/console.h> |
| 20 | #include <linux/tty.h> |
| 21 | #include <linux/tty_flip.h> |
| 22 | #include <linux/serial_core.h> |
Ivan T. Ivanov | 9969394 | 2015-09-30 15:27:02 +0300 | [diff] [blame] | 23 | #include <linux/slab.h> |
Robert Love | 04896a7 | 2009-06-22 18:43:11 +0100 | [diff] [blame] | 24 | #include <linux/clk.h> |
| 25 | #include <linux/platform_device.h> |
Stepan Moskovchenko | ec8f29e | 2010-12-21 12:38:05 -0800 | [diff] [blame] | 26 | #include <linux/delay.h> |
David Brown | cfdad2a | 2011-08-04 01:55:24 -0700 | [diff] [blame] | 27 | #include <linux/of.h> |
| 28 | #include <linux/of_device.h> |
Stephen Boyd | e4276ff | 2016-05-11 18:02:28 -0700 | [diff] [blame] | 29 | #include <linux/wait.h> |
Robert Love | 04896a7 | 2009-06-22 18:43:11 +0100 | [diff] [blame] | 30 | |
Stephen Boyd | 3217374 | 2016-05-11 18:02:26 -0700 | [diff] [blame] | 31 | #define UART_MR1 0x0000 |
Robert Love | 04896a7 | 2009-06-22 18:43:11 +0100 | [diff] [blame] | 32 | |
Stephen Boyd | 3217374 | 2016-05-11 18:02:26 -0700 | [diff] [blame] | 33 | #define UART_MR1_AUTO_RFR_LEVEL0 0x3F |
| 34 | #define UART_MR1_AUTO_RFR_LEVEL1 0x3FF00 |
| 35 | #define UART_DM_MR1_AUTO_RFR_LEVEL1 0xFFFFFF00 |
| 36 | #define UART_MR1_RX_RDY_CTL BIT(7) |
| 37 | #define UART_MR1_CTS_CTL BIT(6) |
| 38 | |
| 39 | #define UART_MR2 0x0004 |
| 40 | #define UART_MR2_ERROR_MODE BIT(6) |
| 41 | #define UART_MR2_BITS_PER_CHAR 0x30 |
| 42 | #define UART_MR2_BITS_PER_CHAR_5 (0x0 << 4) |
| 43 | #define UART_MR2_BITS_PER_CHAR_6 (0x1 << 4) |
| 44 | #define UART_MR2_BITS_PER_CHAR_7 (0x2 << 4) |
| 45 | #define UART_MR2_BITS_PER_CHAR_8 (0x3 << 4) |
| 46 | #define UART_MR2_STOP_BIT_LEN_ONE (0x1 << 2) |
| 47 | #define UART_MR2_STOP_BIT_LEN_TWO (0x3 << 2) |
| 48 | #define UART_MR2_PARITY_MODE_NONE 0x0 |
| 49 | #define UART_MR2_PARITY_MODE_ODD 0x1 |
| 50 | #define UART_MR2_PARITY_MODE_EVEN 0x2 |
| 51 | #define UART_MR2_PARITY_MODE_SPACE 0x3 |
| 52 | #define UART_MR2_PARITY_MODE 0x3 |
| 53 | |
| 54 | #define UART_CSR 0x0008 |
| 55 | |
| 56 | #define UART_TF 0x000C |
| 57 | #define UARTDM_TF 0x0070 |
| 58 | |
| 59 | #define UART_CR 0x0010 |
| 60 | #define UART_CR_CMD_NULL (0 << 4) |
| 61 | #define UART_CR_CMD_RESET_RX (1 << 4) |
| 62 | #define UART_CR_CMD_RESET_TX (2 << 4) |
| 63 | #define UART_CR_CMD_RESET_ERR (3 << 4) |
| 64 | #define UART_CR_CMD_RESET_BREAK_INT (4 << 4) |
| 65 | #define UART_CR_CMD_START_BREAK (5 << 4) |
| 66 | #define UART_CR_CMD_STOP_BREAK (6 << 4) |
| 67 | #define UART_CR_CMD_RESET_CTS (7 << 4) |
| 68 | #define UART_CR_CMD_RESET_STALE_INT (8 << 4) |
| 69 | #define UART_CR_CMD_PACKET_MODE (9 << 4) |
| 70 | #define UART_CR_CMD_MODE_RESET (12 << 4) |
| 71 | #define UART_CR_CMD_SET_RFR (13 << 4) |
| 72 | #define UART_CR_CMD_RESET_RFR (14 << 4) |
| 73 | #define UART_CR_CMD_PROTECTION_EN (16 << 4) |
| 74 | #define UART_CR_CMD_STALE_EVENT_DISABLE (6 << 8) |
| 75 | #define UART_CR_CMD_STALE_EVENT_ENABLE (80 << 4) |
| 76 | #define UART_CR_CMD_FORCE_STALE (4 << 8) |
| 77 | #define UART_CR_CMD_RESET_TX_READY (3 << 8) |
| 78 | #define UART_CR_TX_DISABLE BIT(3) |
| 79 | #define UART_CR_TX_ENABLE BIT(2) |
| 80 | #define UART_CR_RX_DISABLE BIT(1) |
| 81 | #define UART_CR_RX_ENABLE BIT(0) |
| 82 | #define UART_CR_CMD_RESET_RXBREAK_START ((1 << 11) | (2 << 4)) |
| 83 | |
| 84 | #define UART_IMR 0x0014 |
| 85 | #define UART_IMR_TXLEV BIT(0) |
| 86 | #define UART_IMR_RXSTALE BIT(3) |
| 87 | #define UART_IMR_RXLEV BIT(4) |
| 88 | #define UART_IMR_DELTA_CTS BIT(5) |
| 89 | #define UART_IMR_CURRENT_CTS BIT(6) |
| 90 | #define UART_IMR_RXBREAK_START BIT(10) |
| 91 | |
| 92 | #define UART_IPR_RXSTALE_LAST 0x20 |
| 93 | #define UART_IPR_STALE_LSB 0x1F |
| 94 | #define UART_IPR_STALE_TIMEOUT_MSB 0x3FF80 |
| 95 | #define UART_DM_IPR_STALE_TIMEOUT_MSB 0xFFFFFF80 |
| 96 | |
| 97 | #define UART_IPR 0x0018 |
| 98 | #define UART_TFWR 0x001C |
| 99 | #define UART_RFWR 0x0020 |
| 100 | #define UART_HCR 0x0024 |
| 101 | |
| 102 | #define UART_MREG 0x0028 |
| 103 | #define UART_NREG 0x002C |
| 104 | #define UART_DREG 0x0030 |
| 105 | #define UART_MNDREG 0x0034 |
| 106 | #define UART_IRDA 0x0038 |
| 107 | #define UART_MISR_MODE 0x0040 |
| 108 | #define UART_MISR_RESET 0x0044 |
| 109 | #define UART_MISR_EXPORT 0x0048 |
| 110 | #define UART_MISR_VAL 0x004C |
| 111 | #define UART_TEST_CTRL 0x0050 |
| 112 | |
| 113 | #define UART_SR 0x0008 |
| 114 | #define UART_SR_HUNT_CHAR BIT(7) |
| 115 | #define UART_SR_RX_BREAK BIT(6) |
| 116 | #define UART_SR_PAR_FRAME_ERR BIT(5) |
| 117 | #define UART_SR_OVERRUN BIT(4) |
| 118 | #define UART_SR_TX_EMPTY BIT(3) |
| 119 | #define UART_SR_TX_READY BIT(2) |
| 120 | #define UART_SR_RX_FULL BIT(1) |
| 121 | #define UART_SR_RX_READY BIT(0) |
| 122 | |
| 123 | #define UART_RF 0x000C |
| 124 | #define UARTDM_RF 0x0070 |
| 125 | #define UART_MISR 0x0010 |
| 126 | #define UART_ISR 0x0014 |
| 127 | #define UART_ISR_TX_READY BIT(7) |
| 128 | |
| 129 | #define UARTDM_RXFS 0x50 |
| 130 | #define UARTDM_RXFS_BUF_SHIFT 0x7 |
| 131 | #define UARTDM_RXFS_BUF_MASK 0x7 |
| 132 | |
| 133 | #define UARTDM_DMEN 0x3C |
| 134 | #define UARTDM_DMEN_RX_SC_ENABLE BIT(5) |
| 135 | #define UARTDM_DMEN_TX_SC_ENABLE BIT(4) |
| 136 | |
| 137 | #define UARTDM_DMEN_TX_BAM_ENABLE BIT(2) /* UARTDM_1P4 */ |
| 138 | #define UARTDM_DMEN_TX_DM_ENABLE BIT(0) /* < UARTDM_1P4 */ |
| 139 | |
| 140 | #define UARTDM_DMEN_RX_BAM_ENABLE BIT(3) /* UARTDM_1P4 */ |
| 141 | #define UARTDM_DMEN_RX_DM_ENABLE BIT(1) /* < UARTDM_1P4 */ |
| 142 | |
| 143 | #define UARTDM_DMRX 0x34 |
| 144 | #define UARTDM_NCF_TX 0x40 |
| 145 | #define UARTDM_RX_TOTAL_SNAP 0x38 |
| 146 | |
| 147 | #define UARTDM_BURST_SIZE 16 /* in bytes */ |
| 148 | #define UARTDM_TX_AIGN(x) ((x) & ~0x3) /* valid for > 1p3 */ |
| 149 | #define UARTDM_TX_MAX 256 /* in bytes, valid for <= 1p3 */ |
| 150 | #define UARTDM_RX_SIZE (UART_XMIT_SIZE / 4) |
Ivan T. Ivanov | 3a878c4 | 2015-09-30 15:27:01 +0300 | [diff] [blame] | 151 | |
Stephen Boyd | f7e54d7 | 2014-01-14 12:34:55 -0800 | [diff] [blame] | 152 | enum { |
| 153 | UARTDM_1P1 = 1, |
| 154 | UARTDM_1P2, |
| 155 | UARTDM_1P3, |
| 156 | UARTDM_1P4, |
| 157 | }; |
| 158 | |
Ivan T. Ivanov | 3a878c4 | 2015-09-30 15:27:01 +0300 | [diff] [blame] | 159 | struct msm_dma { |
| 160 | struct dma_chan *chan; |
| 161 | enum dma_data_direction dir; |
| 162 | dma_addr_t phys; |
| 163 | unsigned char *virt; |
| 164 | dma_cookie_t cookie; |
| 165 | u32 enable_bit; |
| 166 | unsigned int count; |
| 167 | struct dma_async_tx_descriptor *desc; |
| 168 | }; |
| 169 | |
Robert Love | 04896a7 | 2009-06-22 18:43:11 +0100 | [diff] [blame] | 170 | struct msm_port { |
| 171 | struct uart_port uart; |
| 172 | char name[16]; |
| 173 | struct clk *clk; |
Stepan Moskovchenko | ec8f29e | 2010-12-21 12:38:05 -0800 | [diff] [blame] | 174 | struct clk *pclk; |
Robert Love | 04896a7 | 2009-06-22 18:43:11 +0100 | [diff] [blame] | 175 | unsigned int imr; |
Stepan Moskovchenko | ec8f29e | 2010-12-21 12:38:05 -0800 | [diff] [blame] | 176 | int is_uartdm; |
| 177 | unsigned int old_snap_state; |
Stephen Boyd | 0896d4d | 2014-10-29 11:14:38 -0700 | [diff] [blame] | 178 | bool break_detected; |
Ivan T. Ivanov | 3a878c4 | 2015-09-30 15:27:01 +0300 | [diff] [blame] | 179 | struct msm_dma tx_dma; |
Ivan T. Ivanov | 9969394 | 2015-09-30 15:27:02 +0300 | [diff] [blame] | 180 | struct msm_dma rx_dma; |
Robert Love | 04896a7 | 2009-06-22 18:43:11 +0100 | [diff] [blame] | 181 | }; |
| 182 | |
Stephen Boyd | 3217374 | 2016-05-11 18:02:26 -0700 | [diff] [blame] | 183 | #define UART_TO_MSM(uart_port) container_of(uart_port, struct msm_port, uart) |
| 184 | |
| 185 | static |
| 186 | void msm_write(struct uart_port *port, unsigned int val, unsigned int off) |
| 187 | { |
| 188 | writel_relaxed(val, port->membase + off); |
| 189 | } |
| 190 | |
| 191 | static |
| 192 | unsigned int msm_read(struct uart_port *port, unsigned int off) |
| 193 | { |
| 194 | return readl_relaxed(port->membase + off); |
| 195 | } |
| 196 | |
| 197 | /* |
| 198 | * Setup the MND registers to use the TCXO clock. |
| 199 | */ |
| 200 | static void msm_serial_set_mnd_regs_tcxo(struct uart_port *port) |
| 201 | { |
| 202 | msm_write(port, 0x06, UART_MREG); |
| 203 | msm_write(port, 0xF1, UART_NREG); |
| 204 | msm_write(port, 0x0F, UART_DREG); |
| 205 | msm_write(port, 0x1A, UART_MNDREG); |
| 206 | port->uartclk = 1843200; |
| 207 | } |
| 208 | |
| 209 | /* |
| 210 | * Setup the MND registers to use the TCXO clock divided by 4. |
| 211 | */ |
| 212 | static void msm_serial_set_mnd_regs_tcxoby4(struct uart_port *port) |
| 213 | { |
| 214 | msm_write(port, 0x18, UART_MREG); |
| 215 | msm_write(port, 0xF6, UART_NREG); |
| 216 | msm_write(port, 0x0F, UART_DREG); |
| 217 | msm_write(port, 0x0A, UART_MNDREG); |
| 218 | port->uartclk = 1843200; |
| 219 | } |
| 220 | |
| 221 | static void msm_serial_set_mnd_regs(struct uart_port *port) |
| 222 | { |
Stephen Boyd | 2a31f09 | 2016-05-11 18:02:27 -0700 | [diff] [blame] | 223 | struct msm_port *msm_port = UART_TO_MSM(port); |
| 224 | |
| 225 | /* |
| 226 | * These registers don't exist so we change the clk input rate |
| 227 | * on uartdm hardware instead |
| 228 | */ |
| 229 | if (msm_port->is_uartdm) |
| 230 | return; |
| 231 | |
Stephen Boyd | 3217374 | 2016-05-11 18:02:26 -0700 | [diff] [blame] | 232 | if (port->uartclk == 19200000) |
| 233 | msm_serial_set_mnd_regs_tcxo(port); |
| 234 | else if (port->uartclk == 4800000) |
| 235 | msm_serial_set_mnd_regs_tcxoby4(port); |
| 236 | } |
| 237 | |
Ivan T. Ivanov | 3a878c4 | 2015-09-30 15:27:01 +0300 | [diff] [blame] | 238 | static void msm_handle_tx(struct uart_port *port); |
Ivan T. Ivanov | 9969394 | 2015-09-30 15:27:02 +0300 | [diff] [blame] | 239 | static void msm_start_rx_dma(struct msm_port *msm_port); |
Ivan T. Ivanov | 3a878c4 | 2015-09-30 15:27:01 +0300 | [diff] [blame] | 240 | |
Ben Dooks | 9a3f5bf | 2016-06-07 19:11:52 +0100 | [diff] [blame] | 241 | static void msm_stop_dma(struct uart_port *port, struct msm_dma *dma) |
Ivan T. Ivanov | 3a878c4 | 2015-09-30 15:27:01 +0300 | [diff] [blame] | 242 | { |
| 243 | struct device *dev = port->dev; |
| 244 | unsigned int mapped; |
| 245 | u32 val; |
| 246 | |
| 247 | mapped = dma->count; |
| 248 | dma->count = 0; |
| 249 | |
| 250 | dmaengine_terminate_all(dma->chan); |
| 251 | |
| 252 | /* |
| 253 | * DMA Stall happens if enqueue and flush command happens concurrently. |
| 254 | * For example before changing the baud rate/protocol configuration and |
| 255 | * sending flush command to ADM, disable the channel of UARTDM. |
| 256 | * Note: should not reset the receiver here immediately as it is not |
| 257 | * suggested to do disable/reset or reset/disable at the same time. |
| 258 | */ |
| 259 | val = msm_read(port, UARTDM_DMEN); |
| 260 | val &= ~dma->enable_bit; |
| 261 | msm_write(port, val, UARTDM_DMEN); |
| 262 | |
| 263 | if (mapped) |
| 264 | dma_unmap_single(dev, dma->phys, mapped, dma->dir); |
| 265 | } |
| 266 | |
| 267 | static void msm_release_dma(struct msm_port *msm_port) |
| 268 | { |
| 269 | struct msm_dma *dma; |
| 270 | |
| 271 | dma = &msm_port->tx_dma; |
| 272 | if (dma->chan) { |
| 273 | msm_stop_dma(&msm_port->uart, dma); |
| 274 | dma_release_channel(dma->chan); |
| 275 | } |
| 276 | |
| 277 | memset(dma, 0, sizeof(*dma)); |
Ivan T. Ivanov | 9969394 | 2015-09-30 15:27:02 +0300 | [diff] [blame] | 278 | |
| 279 | dma = &msm_port->rx_dma; |
| 280 | if (dma->chan) { |
| 281 | msm_stop_dma(&msm_port->uart, dma); |
| 282 | dma_release_channel(dma->chan); |
| 283 | kfree(dma->virt); |
| 284 | } |
| 285 | |
| 286 | memset(dma, 0, sizeof(*dma)); |
Ivan T. Ivanov | 3a878c4 | 2015-09-30 15:27:01 +0300 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | static void msm_request_tx_dma(struct msm_port *msm_port, resource_size_t base) |
| 290 | { |
| 291 | struct device *dev = msm_port->uart.dev; |
| 292 | struct dma_slave_config conf; |
| 293 | struct msm_dma *dma; |
| 294 | u32 crci = 0; |
| 295 | int ret; |
| 296 | |
| 297 | dma = &msm_port->tx_dma; |
| 298 | |
| 299 | /* allocate DMA resources, if available */ |
Peter Ujfalusi | 19b6ecf | 2019-11-13 11:46:17 +0200 | [diff] [blame] | 300 | dma->chan = dma_request_chan(dev, "tx"); |
Ivan T. Ivanov | 3a878c4 | 2015-09-30 15:27:01 +0300 | [diff] [blame] | 301 | if (IS_ERR(dma->chan)) |
| 302 | goto no_tx; |
| 303 | |
| 304 | of_property_read_u32(dev->of_node, "qcom,tx-crci", &crci); |
| 305 | |
| 306 | memset(&conf, 0, sizeof(conf)); |
| 307 | conf.direction = DMA_MEM_TO_DEV; |
| 308 | conf.device_fc = true; |
| 309 | conf.dst_addr = base + UARTDM_TF; |
| 310 | conf.dst_maxburst = UARTDM_BURST_SIZE; |
| 311 | conf.slave_id = crci; |
| 312 | |
| 313 | ret = dmaengine_slave_config(dma->chan, &conf); |
| 314 | if (ret) |
| 315 | goto rel_tx; |
| 316 | |
| 317 | dma->dir = DMA_TO_DEVICE; |
| 318 | |
| 319 | if (msm_port->is_uartdm < UARTDM_1P4) |
| 320 | dma->enable_bit = UARTDM_DMEN_TX_DM_ENABLE; |
| 321 | else |
| 322 | dma->enable_bit = UARTDM_DMEN_TX_BAM_ENABLE; |
| 323 | |
| 324 | return; |
| 325 | |
| 326 | rel_tx: |
| 327 | dma_release_channel(dma->chan); |
| 328 | no_tx: |
| 329 | memset(dma, 0, sizeof(*dma)); |
| 330 | } |
| 331 | |
Ivan T. Ivanov | 9969394 | 2015-09-30 15:27:02 +0300 | [diff] [blame] | 332 | static void msm_request_rx_dma(struct msm_port *msm_port, resource_size_t base) |
| 333 | { |
| 334 | struct device *dev = msm_port->uart.dev; |
| 335 | struct dma_slave_config conf; |
| 336 | struct msm_dma *dma; |
| 337 | u32 crci = 0; |
| 338 | int ret; |
| 339 | |
| 340 | dma = &msm_port->rx_dma; |
| 341 | |
| 342 | /* allocate DMA resources, if available */ |
Peter Ujfalusi | 19b6ecf | 2019-11-13 11:46:17 +0200 | [diff] [blame] | 343 | dma->chan = dma_request_chan(dev, "rx"); |
Ivan T. Ivanov | 9969394 | 2015-09-30 15:27:02 +0300 | [diff] [blame] | 344 | if (IS_ERR(dma->chan)) |
| 345 | goto no_rx; |
| 346 | |
| 347 | of_property_read_u32(dev->of_node, "qcom,rx-crci", &crci); |
| 348 | |
| 349 | dma->virt = kzalloc(UARTDM_RX_SIZE, GFP_KERNEL); |
| 350 | if (!dma->virt) |
| 351 | goto rel_rx; |
| 352 | |
| 353 | memset(&conf, 0, sizeof(conf)); |
| 354 | conf.direction = DMA_DEV_TO_MEM; |
| 355 | conf.device_fc = true; |
| 356 | conf.src_addr = base + UARTDM_RF; |
| 357 | conf.src_maxburst = UARTDM_BURST_SIZE; |
| 358 | conf.slave_id = crci; |
| 359 | |
| 360 | ret = dmaengine_slave_config(dma->chan, &conf); |
| 361 | if (ret) |
| 362 | goto err; |
| 363 | |
| 364 | dma->dir = DMA_FROM_DEVICE; |
| 365 | |
| 366 | if (msm_port->is_uartdm < UARTDM_1P4) |
| 367 | dma->enable_bit = UARTDM_DMEN_RX_DM_ENABLE; |
| 368 | else |
| 369 | dma->enable_bit = UARTDM_DMEN_RX_BAM_ENABLE; |
| 370 | |
| 371 | return; |
| 372 | err: |
| 373 | kfree(dma->virt); |
| 374 | rel_rx: |
| 375 | dma_release_channel(dma->chan); |
| 376 | no_rx: |
| 377 | memset(dma, 0, sizeof(*dma)); |
| 378 | } |
| 379 | |
Ivan T. Ivanov | 558abdb | 2015-09-30 15:27:00 +0300 | [diff] [blame] | 380 | static inline void msm_wait_for_xmitr(struct uart_port *port) |
Stepan Moskovchenko | ec8f29e | 2010-12-21 12:38:05 -0800 | [diff] [blame] | 381 | { |
Jorge Ramirez-Ortiz | ba3684f | 2019-06-10 19:23:08 +0200 | [diff] [blame] | 382 | unsigned int timeout = 500000; |
| 383 | |
Stephen Boyd | 4a5662d | 2013-07-24 11:37:28 -0700 | [diff] [blame] | 384 | while (!(msm_read(port, UART_SR) & UART_SR_TX_EMPTY)) { |
| 385 | if (msm_read(port, UART_ISR) & UART_ISR_TX_READY) |
| 386 | break; |
| 387 | udelay(1); |
Jorge Ramirez-Ortiz | ba3684f | 2019-06-10 19:23:08 +0200 | [diff] [blame] | 388 | if (!timeout--) |
| 389 | break; |
Stephen Boyd | 4a5662d | 2013-07-24 11:37:28 -0700 | [diff] [blame] | 390 | } |
| 391 | msm_write(port, UART_CR_CMD_RESET_TX_READY, UART_CR); |
Stepan Moskovchenko | ec8f29e | 2010-12-21 12:38:05 -0800 | [diff] [blame] | 392 | } |
| 393 | |
Robert Love | 04896a7 | 2009-06-22 18:43:11 +0100 | [diff] [blame] | 394 | static void msm_stop_tx(struct uart_port *port) |
| 395 | { |
| 396 | struct msm_port *msm_port = UART_TO_MSM(port); |
| 397 | |
| 398 | msm_port->imr &= ~UART_IMR_TXLEV; |
| 399 | msm_write(port, msm_port->imr, UART_IMR); |
| 400 | } |
| 401 | |
| 402 | static void msm_start_tx(struct uart_port *port) |
| 403 | { |
| 404 | struct msm_port *msm_port = UART_TO_MSM(port); |
Ivan T. Ivanov | 3a878c4 | 2015-09-30 15:27:01 +0300 | [diff] [blame] | 405 | struct msm_dma *dma = &msm_port->tx_dma; |
| 406 | |
| 407 | /* Already started in DMA mode */ |
| 408 | if (dma->count) |
| 409 | return; |
Robert Love | 04896a7 | 2009-06-22 18:43:11 +0100 | [diff] [blame] | 410 | |
| 411 | msm_port->imr |= UART_IMR_TXLEV; |
| 412 | msm_write(port, msm_port->imr, UART_IMR); |
| 413 | } |
| 414 | |
Ivan T. Ivanov | 3a878c4 | 2015-09-30 15:27:01 +0300 | [diff] [blame] | 415 | static void msm_reset_dm_count(struct uart_port *port, int count) |
| 416 | { |
| 417 | msm_wait_for_xmitr(port); |
| 418 | msm_write(port, count, UARTDM_NCF_TX); |
| 419 | msm_read(port, UARTDM_NCF_TX); |
| 420 | } |
| 421 | |
| 422 | static void msm_complete_tx_dma(void *args) |
| 423 | { |
| 424 | struct msm_port *msm_port = args; |
| 425 | struct uart_port *port = &msm_port->uart; |
| 426 | struct circ_buf *xmit = &port->state->xmit; |
| 427 | struct msm_dma *dma = &msm_port->tx_dma; |
| 428 | struct dma_tx_state state; |
Ivan T. Ivanov | 3a878c4 | 2015-09-30 15:27:01 +0300 | [diff] [blame] | 429 | unsigned long flags; |
| 430 | unsigned int count; |
| 431 | u32 val; |
| 432 | |
| 433 | spin_lock_irqsave(&port->lock, flags); |
| 434 | |
| 435 | /* Already stopped */ |
| 436 | if (!dma->count) |
| 437 | goto done; |
| 438 | |
Lee Jones | fe98992 | 2020-11-04 19:35:33 +0000 | [diff] [blame] | 439 | dmaengine_tx_status(dma->chan, dma->cookie, &state); |
Ivan T. Ivanov | 3a878c4 | 2015-09-30 15:27:01 +0300 | [diff] [blame] | 440 | |
| 441 | dma_unmap_single(port->dev, dma->phys, dma->count, dma->dir); |
| 442 | |
| 443 | val = msm_read(port, UARTDM_DMEN); |
| 444 | val &= ~dma->enable_bit; |
| 445 | msm_write(port, val, UARTDM_DMEN); |
| 446 | |
| 447 | if (msm_port->is_uartdm > UARTDM_1P3) { |
| 448 | msm_write(port, UART_CR_CMD_RESET_TX, UART_CR); |
| 449 | msm_write(port, UART_CR_TX_ENABLE, UART_CR); |
| 450 | } |
| 451 | |
| 452 | count = dma->count - state.residue; |
| 453 | port->icount.tx += count; |
| 454 | dma->count = 0; |
| 455 | |
| 456 | xmit->tail += count; |
| 457 | xmit->tail &= UART_XMIT_SIZE - 1; |
| 458 | |
| 459 | /* Restore "Tx FIFO below watermark" interrupt */ |
| 460 | msm_port->imr |= UART_IMR_TXLEV; |
| 461 | msm_write(port, msm_port->imr, UART_IMR); |
| 462 | |
| 463 | if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS) |
| 464 | uart_write_wakeup(port); |
| 465 | |
| 466 | msm_handle_tx(port); |
| 467 | done: |
| 468 | spin_unlock_irqrestore(&port->lock, flags); |
| 469 | } |
| 470 | |
| 471 | static int msm_handle_tx_dma(struct msm_port *msm_port, unsigned int count) |
| 472 | { |
| 473 | struct circ_buf *xmit = &msm_port->uart.state->xmit; |
| 474 | struct uart_port *port = &msm_port->uart; |
| 475 | struct msm_dma *dma = &msm_port->tx_dma; |
| 476 | void *cpu_addr; |
| 477 | int ret; |
| 478 | u32 val; |
| 479 | |
| 480 | cpu_addr = &xmit->buf[xmit->tail]; |
| 481 | |
| 482 | dma->phys = dma_map_single(port->dev, cpu_addr, count, dma->dir); |
| 483 | ret = dma_mapping_error(port->dev, dma->phys); |
| 484 | if (ret) |
| 485 | return ret; |
| 486 | |
| 487 | dma->desc = dmaengine_prep_slave_single(dma->chan, dma->phys, |
| 488 | count, DMA_MEM_TO_DEV, |
| 489 | DMA_PREP_INTERRUPT | |
| 490 | DMA_PREP_FENCE); |
| 491 | if (!dma->desc) { |
| 492 | ret = -EIO; |
| 493 | goto unmap; |
| 494 | } |
| 495 | |
| 496 | dma->desc->callback = msm_complete_tx_dma; |
| 497 | dma->desc->callback_param = msm_port; |
| 498 | |
| 499 | dma->cookie = dmaengine_submit(dma->desc); |
| 500 | ret = dma_submit_error(dma->cookie); |
| 501 | if (ret) |
| 502 | goto unmap; |
| 503 | |
| 504 | /* |
| 505 | * Using DMA complete for Tx FIFO reload, no need for |
| 506 | * "Tx FIFO below watermark" one, disable it |
| 507 | */ |
| 508 | msm_port->imr &= ~UART_IMR_TXLEV; |
| 509 | msm_write(port, msm_port->imr, UART_IMR); |
| 510 | |
| 511 | dma->count = count; |
| 512 | |
| 513 | val = msm_read(port, UARTDM_DMEN); |
| 514 | val |= dma->enable_bit; |
| 515 | |
| 516 | if (msm_port->is_uartdm < UARTDM_1P4) |
| 517 | msm_write(port, val, UARTDM_DMEN); |
| 518 | |
| 519 | msm_reset_dm_count(port, count); |
| 520 | |
| 521 | if (msm_port->is_uartdm > UARTDM_1P3) |
| 522 | msm_write(port, val, UARTDM_DMEN); |
| 523 | |
| 524 | dma_async_issue_pending(dma->chan); |
| 525 | return 0; |
| 526 | unmap: |
| 527 | dma_unmap_single(port->dev, dma->phys, count, dma->dir); |
| 528 | return ret; |
| 529 | } |
Ivan T. Ivanov | 9969394 | 2015-09-30 15:27:02 +0300 | [diff] [blame] | 530 | |
| 531 | static void msm_complete_rx_dma(void *args) |
| 532 | { |
| 533 | struct msm_port *msm_port = args; |
| 534 | struct uart_port *port = &msm_port->uart; |
| 535 | struct tty_port *tport = &port->state->port; |
| 536 | struct msm_dma *dma = &msm_port->rx_dma; |
| 537 | int count = 0, i, sysrq; |
| 538 | unsigned long flags; |
| 539 | u32 val; |
| 540 | |
| 541 | spin_lock_irqsave(&port->lock, flags); |
| 542 | |
| 543 | /* Already stopped */ |
| 544 | if (!dma->count) |
| 545 | goto done; |
| 546 | |
| 547 | val = msm_read(port, UARTDM_DMEN); |
| 548 | val &= ~dma->enable_bit; |
| 549 | msm_write(port, val, UARTDM_DMEN); |
| 550 | |
Ivan T. Ivanov | 9969394 | 2015-09-30 15:27:02 +0300 | [diff] [blame] | 551 | if (msm_read(port, UART_SR) & UART_SR_OVERRUN) { |
| 552 | port->icount.overrun++; |
| 553 | tty_insert_flip_char(tport, 0, TTY_OVERRUN); |
| 554 | msm_write(port, UART_CR_CMD_RESET_ERR, UART_CR); |
| 555 | } |
| 556 | |
| 557 | count = msm_read(port, UARTDM_RX_TOTAL_SNAP); |
| 558 | |
| 559 | port->icount.rx += count; |
| 560 | |
| 561 | dma->count = 0; |
| 562 | |
| 563 | dma_unmap_single(port->dev, dma->phys, UARTDM_RX_SIZE, dma->dir); |
| 564 | |
| 565 | for (i = 0; i < count; i++) { |
| 566 | char flag = TTY_NORMAL; |
| 567 | |
| 568 | if (msm_port->break_detected && dma->virt[i] == 0) { |
| 569 | port->icount.brk++; |
| 570 | flag = TTY_BREAK; |
| 571 | msm_port->break_detected = false; |
| 572 | if (uart_handle_break(port)) |
| 573 | continue; |
| 574 | } |
| 575 | |
| 576 | if (!(port->read_status_mask & UART_SR_RX_BREAK)) |
| 577 | flag = TTY_NORMAL; |
| 578 | |
| 579 | spin_unlock_irqrestore(&port->lock, flags); |
| 580 | sysrq = uart_handle_sysrq_char(port, dma->virt[i]); |
| 581 | spin_lock_irqsave(&port->lock, flags); |
| 582 | if (!sysrq) |
| 583 | tty_insert_flip_char(tport, dma->virt[i], flag); |
| 584 | } |
| 585 | |
| 586 | msm_start_rx_dma(msm_port); |
| 587 | done: |
| 588 | spin_unlock_irqrestore(&port->lock, flags); |
| 589 | |
| 590 | if (count) |
| 591 | tty_flip_buffer_push(tport); |
| 592 | } |
| 593 | |
| 594 | static void msm_start_rx_dma(struct msm_port *msm_port) |
| 595 | { |
| 596 | struct msm_dma *dma = &msm_port->rx_dma; |
| 597 | struct uart_port *uart = &msm_port->uart; |
| 598 | u32 val; |
| 599 | int ret; |
| 600 | |
Sven Eckelmann | 7492ffc | 2021-11-13 13:10:50 +0100 | [diff] [blame] | 601 | if (IS_ENABLED(CONFIG_CONSOLE_POLL)) |
| 602 | return; |
| 603 | |
Ivan T. Ivanov | 9969394 | 2015-09-30 15:27:02 +0300 | [diff] [blame] | 604 | if (!dma->chan) |
| 605 | return; |
| 606 | |
| 607 | dma->phys = dma_map_single(uart->dev, dma->virt, |
| 608 | UARTDM_RX_SIZE, dma->dir); |
| 609 | ret = dma_mapping_error(uart->dev, dma->phys); |
| 610 | if (ret) |
Loic Poulain | 76460fb | 2020-01-10 09:58:04 +0100 | [diff] [blame] | 611 | goto sw_mode; |
Ivan T. Ivanov | 9969394 | 2015-09-30 15:27:02 +0300 | [diff] [blame] | 612 | |
| 613 | dma->desc = dmaengine_prep_slave_single(dma->chan, dma->phys, |
| 614 | UARTDM_RX_SIZE, DMA_DEV_TO_MEM, |
| 615 | DMA_PREP_INTERRUPT); |
| 616 | if (!dma->desc) |
| 617 | goto unmap; |
| 618 | |
| 619 | dma->desc->callback = msm_complete_rx_dma; |
| 620 | dma->desc->callback_param = msm_port; |
| 621 | |
| 622 | dma->cookie = dmaengine_submit(dma->desc); |
| 623 | ret = dma_submit_error(dma->cookie); |
| 624 | if (ret) |
| 625 | goto unmap; |
| 626 | /* |
| 627 | * Using DMA for FIFO off-load, no need for "Rx FIFO over |
| 628 | * watermark" or "stale" interrupts, disable them |
| 629 | */ |
| 630 | msm_port->imr &= ~(UART_IMR_RXLEV | UART_IMR_RXSTALE); |
| 631 | |
| 632 | /* |
| 633 | * Well, when DMA is ADM3 engine(implied by <= UARTDM v1.3), |
| 634 | * we need RXSTALE to flush input DMA fifo to memory |
| 635 | */ |
| 636 | if (msm_port->is_uartdm < UARTDM_1P4) |
| 637 | msm_port->imr |= UART_IMR_RXSTALE; |
| 638 | |
| 639 | msm_write(uart, msm_port->imr, UART_IMR); |
| 640 | |
| 641 | dma->count = UARTDM_RX_SIZE; |
| 642 | |
| 643 | dma_async_issue_pending(dma->chan); |
| 644 | |
| 645 | msm_write(uart, UART_CR_CMD_RESET_STALE_INT, UART_CR); |
| 646 | msm_write(uart, UART_CR_CMD_STALE_EVENT_ENABLE, UART_CR); |
| 647 | |
| 648 | val = msm_read(uart, UARTDM_DMEN); |
| 649 | val |= dma->enable_bit; |
| 650 | |
| 651 | if (msm_port->is_uartdm < UARTDM_1P4) |
| 652 | msm_write(uart, val, UARTDM_DMEN); |
| 653 | |
| 654 | msm_write(uart, UARTDM_RX_SIZE, UARTDM_DMRX); |
| 655 | |
| 656 | if (msm_port->is_uartdm > UARTDM_1P3) |
| 657 | msm_write(uart, val, UARTDM_DMEN); |
| 658 | |
| 659 | return; |
| 660 | unmap: |
| 661 | dma_unmap_single(uart->dev, dma->phys, UARTDM_RX_SIZE, dma->dir); |
Loic Poulain | 76460fb | 2020-01-10 09:58:04 +0100 | [diff] [blame] | 662 | |
| 663 | sw_mode: |
| 664 | /* |
| 665 | * Switch from DMA to SW/FIFO mode. After clearing Rx BAM (UARTDM_DMEN), |
| 666 | * receiver must be reset. |
| 667 | */ |
| 668 | msm_write(uart, UART_CR_CMD_RESET_RX, UART_CR); |
| 669 | msm_write(uart, UART_CR_RX_ENABLE, UART_CR); |
| 670 | |
| 671 | msm_write(uart, UART_CR_CMD_RESET_STALE_INT, UART_CR); |
| 672 | msm_write(uart, 0xFFFFFF, UARTDM_DMRX); |
| 673 | msm_write(uart, UART_CR_CMD_STALE_EVENT_ENABLE, UART_CR); |
| 674 | |
| 675 | /* Re-enable RX interrupts */ |
| 676 | msm_port->imr |= (UART_IMR_RXLEV | UART_IMR_RXSTALE); |
| 677 | msm_write(uart, msm_port->imr, UART_IMR); |
Ivan T. Ivanov | 9969394 | 2015-09-30 15:27:02 +0300 | [diff] [blame] | 678 | } |
| 679 | |
Robert Love | 04896a7 | 2009-06-22 18:43:11 +0100 | [diff] [blame] | 680 | static void msm_stop_rx(struct uart_port *port) |
| 681 | { |
| 682 | struct msm_port *msm_port = UART_TO_MSM(port); |
Ivan T. Ivanov | 9969394 | 2015-09-30 15:27:02 +0300 | [diff] [blame] | 683 | struct msm_dma *dma = &msm_port->rx_dma; |
Robert Love | 04896a7 | 2009-06-22 18:43:11 +0100 | [diff] [blame] | 684 | |
| 685 | msm_port->imr &= ~(UART_IMR_RXLEV | UART_IMR_RXSTALE); |
| 686 | msm_write(port, msm_port->imr, UART_IMR); |
Ivan T. Ivanov | 9969394 | 2015-09-30 15:27:02 +0300 | [diff] [blame] | 687 | |
| 688 | if (dma->chan) |
| 689 | msm_stop_dma(port, dma); |
Robert Love | 04896a7 | 2009-06-22 18:43:11 +0100 | [diff] [blame] | 690 | } |
| 691 | |
| 692 | static void msm_enable_ms(struct uart_port *port) |
| 693 | { |
| 694 | struct msm_port *msm_port = UART_TO_MSM(port); |
| 695 | |
| 696 | msm_port->imr |= UART_IMR_DELTA_CTS; |
| 697 | msm_write(port, msm_port->imr, UART_IMR); |
| 698 | } |
| 699 | |
Ivan T. Ivanov | 558abdb | 2015-09-30 15:27:00 +0300 | [diff] [blame] | 700 | static void msm_handle_rx_dm(struct uart_port *port, unsigned int misr) |
Johan Hovold | ca45b5c | 2020-07-23 14:33:27 +0200 | [diff] [blame] | 701 | __must_hold(&port->lock) |
Stepan Moskovchenko | ec8f29e | 2010-12-21 12:38:05 -0800 | [diff] [blame] | 702 | { |
Jiri Slaby | 92a19f9 | 2013-01-03 15:53:03 +0100 | [diff] [blame] | 703 | struct tty_port *tport = &port->state->port; |
Stepan Moskovchenko | ec8f29e | 2010-12-21 12:38:05 -0800 | [diff] [blame] | 704 | unsigned int sr; |
| 705 | int count = 0; |
| 706 | struct msm_port *msm_port = UART_TO_MSM(port); |
| 707 | |
| 708 | if ((msm_read(port, UART_SR) & UART_SR_OVERRUN)) { |
| 709 | port->icount.overrun++; |
Jiri Slaby | 92a19f9 | 2013-01-03 15:53:03 +0100 | [diff] [blame] | 710 | tty_insert_flip_char(tport, 0, TTY_OVERRUN); |
Stepan Moskovchenko | ec8f29e | 2010-12-21 12:38:05 -0800 | [diff] [blame] | 711 | msm_write(port, UART_CR_CMD_RESET_ERR, UART_CR); |
| 712 | } |
| 713 | |
| 714 | if (misr & UART_IMR_RXSTALE) { |
| 715 | count = msm_read(port, UARTDM_RX_TOTAL_SNAP) - |
| 716 | msm_port->old_snap_state; |
| 717 | msm_port->old_snap_state = 0; |
| 718 | } else { |
| 719 | count = 4 * (msm_read(port, UART_RFWR)); |
| 720 | msm_port->old_snap_state += count; |
| 721 | } |
| 722 | |
| 723 | /* TODO: Precise error reporting */ |
| 724 | |
| 725 | port->icount.rx += count; |
| 726 | |
| 727 | while (count > 0) { |
Stephen Boyd | 6825242 | 2014-06-30 14:54:01 -0700 | [diff] [blame] | 728 | unsigned char buf[4]; |
Stephen Boyd | 0896d4d | 2014-10-29 11:14:38 -0700 | [diff] [blame] | 729 | int sysrq, r_count, i; |
Stepan Moskovchenko | ec8f29e | 2010-12-21 12:38:05 -0800 | [diff] [blame] | 730 | |
| 731 | sr = msm_read(port, UART_SR); |
| 732 | if ((sr & UART_SR_RX_READY) == 0) { |
| 733 | msm_port->old_snap_state -= count; |
| 734 | break; |
| 735 | } |
Stepan Moskovchenko | ec8f29e | 2010-12-21 12:38:05 -0800 | [diff] [blame] | 736 | |
Stephen Boyd | 0896d4d | 2014-10-29 11:14:38 -0700 | [diff] [blame] | 737 | ioread32_rep(port->membase + UARTDM_RF, buf, 1); |
| 738 | r_count = min_t(int, count, sizeof(buf)); |
| 739 | |
| 740 | for (i = 0; i < r_count; i++) { |
| 741 | char flag = TTY_NORMAL; |
| 742 | |
| 743 | if (msm_port->break_detected && buf[i] == 0) { |
| 744 | port->icount.brk++; |
| 745 | flag = TTY_BREAK; |
| 746 | msm_port->break_detected = false; |
| 747 | if (uart_handle_break(port)) |
| 748 | continue; |
| 749 | } |
| 750 | |
| 751 | if (!(port->read_status_mask & UART_SR_RX_BREAK)) |
| 752 | flag = TTY_NORMAL; |
| 753 | |
| 754 | spin_unlock(&port->lock); |
| 755 | sysrq = uart_handle_sysrq_char(port, buf[i]); |
| 756 | spin_lock(&port->lock); |
| 757 | if (!sysrq) |
| 758 | tty_insert_flip_char(tport, buf[i], flag); |
| 759 | } |
| 760 | count -= r_count; |
Stepan Moskovchenko | ec8f29e | 2010-12-21 12:38:05 -0800 | [diff] [blame] | 761 | } |
| 762 | |
Jiri Slaby | 2e124b4 | 2013-01-03 15:53:06 +0100 | [diff] [blame] | 763 | tty_flip_buffer_push(tport); |
Viresh Kumar | f77232d | 2013-08-19 20:14:20 +0530 | [diff] [blame] | 764 | |
Stepan Moskovchenko | ec8f29e | 2010-12-21 12:38:05 -0800 | [diff] [blame] | 765 | if (misr & (UART_IMR_RXSTALE)) |
| 766 | msm_write(port, UART_CR_CMD_RESET_STALE_INT, UART_CR); |
| 767 | msm_write(port, 0xFFFFFF, UARTDM_DMRX); |
| 768 | msm_write(port, UART_CR_CMD_STALE_EVENT_ENABLE, UART_CR); |
Ivan T. Ivanov | 9969394 | 2015-09-30 15:27:02 +0300 | [diff] [blame] | 769 | |
| 770 | /* Try to use DMA */ |
| 771 | msm_start_rx_dma(msm_port); |
Stepan Moskovchenko | ec8f29e | 2010-12-21 12:38:05 -0800 | [diff] [blame] | 772 | } |
| 773 | |
Ivan T. Ivanov | 558abdb | 2015-09-30 15:27:00 +0300 | [diff] [blame] | 774 | static void msm_handle_rx(struct uart_port *port) |
Johan Hovold | ca45b5c | 2020-07-23 14:33:27 +0200 | [diff] [blame] | 775 | __must_hold(&port->lock) |
Robert Love | 04896a7 | 2009-06-22 18:43:11 +0100 | [diff] [blame] | 776 | { |
Jiri Slaby | 92a19f9 | 2013-01-03 15:53:03 +0100 | [diff] [blame] | 777 | struct tty_port *tport = &port->state->port; |
Robert Love | 04896a7 | 2009-06-22 18:43:11 +0100 | [diff] [blame] | 778 | unsigned int sr; |
| 779 | |
| 780 | /* |
| 781 | * Handle overrun. My understanding of the hardware is that overrun |
| 782 | * is not tied to the RX buffer, so we handle the case out of band. |
| 783 | */ |
| 784 | if ((msm_read(port, UART_SR) & UART_SR_OVERRUN)) { |
| 785 | port->icount.overrun++; |
Jiri Slaby | 92a19f9 | 2013-01-03 15:53:03 +0100 | [diff] [blame] | 786 | tty_insert_flip_char(tport, 0, TTY_OVERRUN); |
Robert Love | 04896a7 | 2009-06-22 18:43:11 +0100 | [diff] [blame] | 787 | msm_write(port, UART_CR_CMD_RESET_ERR, UART_CR); |
| 788 | } |
| 789 | |
| 790 | /* and now the main RX loop */ |
| 791 | while ((sr = msm_read(port, UART_SR)) & UART_SR_RX_READY) { |
| 792 | unsigned int c; |
| 793 | char flag = TTY_NORMAL; |
Stephen Boyd | 660beb0e | 2014-10-29 11:14:37 -0700 | [diff] [blame] | 794 | int sysrq; |
Robert Love | 04896a7 | 2009-06-22 18:43:11 +0100 | [diff] [blame] | 795 | |
| 796 | c = msm_read(port, UART_RF); |
| 797 | |
| 798 | if (sr & UART_SR_RX_BREAK) { |
| 799 | port->icount.brk++; |
| 800 | if (uart_handle_break(port)) |
| 801 | continue; |
| 802 | } else if (sr & UART_SR_PAR_FRAME_ERR) { |
| 803 | port->icount.frame++; |
| 804 | } else { |
| 805 | port->icount.rx++; |
| 806 | } |
| 807 | |
| 808 | /* Mask conditions we're ignorning. */ |
| 809 | sr &= port->read_status_mask; |
| 810 | |
Kiran Padwal | ddea392 | 2014-08-05 13:21:59 +0530 | [diff] [blame] | 811 | if (sr & UART_SR_RX_BREAK) |
Robert Love | 04896a7 | 2009-06-22 18:43:11 +0100 | [diff] [blame] | 812 | flag = TTY_BREAK; |
Kiran Padwal | ddea392 | 2014-08-05 13:21:59 +0530 | [diff] [blame] | 813 | else if (sr & UART_SR_PAR_FRAME_ERR) |
Robert Love | 04896a7 | 2009-06-22 18:43:11 +0100 | [diff] [blame] | 814 | flag = TTY_FRAME; |
Robert Love | 04896a7 | 2009-06-22 18:43:11 +0100 | [diff] [blame] | 815 | |
Stephen Boyd | 660beb0e | 2014-10-29 11:14:37 -0700 | [diff] [blame] | 816 | spin_unlock(&port->lock); |
| 817 | sysrq = uart_handle_sysrq_char(port, c); |
| 818 | spin_lock(&port->lock); |
| 819 | if (!sysrq) |
Jiri Slaby | 92a19f9 | 2013-01-03 15:53:03 +0100 | [diff] [blame] | 820 | tty_insert_flip_char(tport, c, flag); |
Robert Love | 04896a7 | 2009-06-22 18:43:11 +0100 | [diff] [blame] | 821 | } |
| 822 | |
Jiri Slaby | 2e124b4 | 2013-01-03 15:53:06 +0100 | [diff] [blame] | 823 | tty_flip_buffer_push(tport); |
Robert Love | 04896a7 | 2009-06-22 18:43:11 +0100 | [diff] [blame] | 824 | } |
| 825 | |
Ivan T. Ivanov | 3a878c4 | 2015-09-30 15:27:01 +0300 | [diff] [blame] | 826 | static void msm_handle_tx_pio(struct uart_port *port, unsigned int tx_count) |
Robert Love | 04896a7 | 2009-06-22 18:43:11 +0100 | [diff] [blame] | 827 | { |
Alan Cox | ebd2c8f | 2009-09-19 13:13:28 -0700 | [diff] [blame] | 828 | struct circ_buf *xmit = &port->state->xmit; |
Robert Love | 04896a7 | 2009-06-22 18:43:11 +0100 | [diff] [blame] | 829 | struct msm_port *msm_port = UART_TO_MSM(port); |
Ivan T. Ivanov | 3a878c4 | 2015-09-30 15:27:01 +0300 | [diff] [blame] | 830 | unsigned int num_chars; |
Stephen Boyd | 17fae28 | 2013-07-24 11:37:31 -0700 | [diff] [blame] | 831 | unsigned int tf_pointer = 0; |
Stephen Boyd | 6825242 | 2014-06-30 14:54:01 -0700 | [diff] [blame] | 832 | void __iomem *tf; |
| 833 | |
| 834 | if (msm_port->is_uartdm) |
| 835 | tf = port->membase + UARTDM_TF; |
| 836 | else |
| 837 | tf = port->membase + UART_TF; |
Stephen Boyd | 17fae28 | 2013-07-24 11:37:31 -0700 | [diff] [blame] | 838 | |
Ivan T. Ivanov | 3a878c4 | 2015-09-30 15:27:01 +0300 | [diff] [blame] | 839 | if (tx_count && msm_port->is_uartdm) |
Ivan T. Ivanov | 558abdb | 2015-09-30 15:27:00 +0300 | [diff] [blame] | 840 | msm_reset_dm_count(port, tx_count); |
Robert Love | 04896a7 | 2009-06-22 18:43:11 +0100 | [diff] [blame] | 841 | |
Stephen Boyd | 17fae28 | 2013-07-24 11:37:31 -0700 | [diff] [blame] | 842 | while (tf_pointer < tx_count) { |
| 843 | int i; |
| 844 | char buf[4] = { 0 }; |
Stepan Moskovchenko | ec8f29e | 2010-12-21 12:38:05 -0800 | [diff] [blame] | 845 | |
Stephen Boyd | 17fae28 | 2013-07-24 11:37:31 -0700 | [diff] [blame] | 846 | if (!(msm_read(port, UART_SR) & UART_SR_TX_READY)) |
Robert Love | 04896a7 | 2009-06-22 18:43:11 +0100 | [diff] [blame] | 847 | break; |
Robert Love | 04896a7 | 2009-06-22 18:43:11 +0100 | [diff] [blame] | 848 | |
Stepan Moskovchenko | ec8f29e | 2010-12-21 12:38:05 -0800 | [diff] [blame] | 849 | if (msm_port->is_uartdm) |
Jingoo Han | 4f749f2 | 2013-08-08 17:38:20 +0900 | [diff] [blame] | 850 | num_chars = min(tx_count - tf_pointer, |
| 851 | (unsigned int)sizeof(buf)); |
Stephen Boyd | 17fae28 | 2013-07-24 11:37:31 -0700 | [diff] [blame] | 852 | else |
| 853 | num_chars = 1; |
Robert Love | 04896a7 | 2009-06-22 18:43:11 +0100 | [diff] [blame] | 854 | |
Stephen Boyd | 17fae28 | 2013-07-24 11:37:31 -0700 | [diff] [blame] | 855 | for (i = 0; i < num_chars; i++) { |
| 856 | buf[i] = xmit->buf[xmit->tail + i]; |
| 857 | port->icount.tx++; |
| 858 | } |
| 859 | |
Stephen Boyd | 6825242 | 2014-06-30 14:54:01 -0700 | [diff] [blame] | 860 | iowrite32_rep(tf, buf, 1); |
Stephen Boyd | 17fae28 | 2013-07-24 11:37:31 -0700 | [diff] [blame] | 861 | xmit->tail = (xmit->tail + num_chars) & (UART_XMIT_SIZE - 1); |
| 862 | tf_pointer += num_chars; |
Robert Love | 04896a7 | 2009-06-22 18:43:11 +0100 | [diff] [blame] | 863 | } |
| 864 | |
Stephen Boyd | 17fae28 | 2013-07-24 11:37:31 -0700 | [diff] [blame] | 865 | /* disable tx interrupts if nothing more to send */ |
| 866 | if (uart_circ_empty(xmit)) |
| 867 | msm_stop_tx(port); |
| 868 | |
Robert Love | 04896a7 | 2009-06-22 18:43:11 +0100 | [diff] [blame] | 869 | if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS) |
| 870 | uart_write_wakeup(port); |
| 871 | } |
| 872 | |
Ivan T. Ivanov | 3a878c4 | 2015-09-30 15:27:01 +0300 | [diff] [blame] | 873 | static void msm_handle_tx(struct uart_port *port) |
| 874 | { |
| 875 | struct msm_port *msm_port = UART_TO_MSM(port); |
| 876 | struct circ_buf *xmit = &msm_port->uart.state->xmit; |
| 877 | struct msm_dma *dma = &msm_port->tx_dma; |
| 878 | unsigned int pio_count, dma_count, dma_min; |
Jorge Ramirez-Ortiz | 61c0e37 | 2019-05-20 20:38:48 +0200 | [diff] [blame] | 879 | char buf[4] = { 0 }; |
Ivan T. Ivanov | 3a878c4 | 2015-09-30 15:27:01 +0300 | [diff] [blame] | 880 | void __iomem *tf; |
| 881 | int err = 0; |
| 882 | |
| 883 | if (port->x_char) { |
| 884 | if (msm_port->is_uartdm) |
| 885 | tf = port->membase + UARTDM_TF; |
| 886 | else |
| 887 | tf = port->membase + UART_TF; |
| 888 | |
Jorge Ramirez-Ortiz | 61c0e37 | 2019-05-20 20:38:48 +0200 | [diff] [blame] | 889 | buf[0] = port->x_char; |
| 890 | |
Ivan T. Ivanov | 3a878c4 | 2015-09-30 15:27:01 +0300 | [diff] [blame] | 891 | if (msm_port->is_uartdm) |
| 892 | msm_reset_dm_count(port, 1); |
| 893 | |
Jorge Ramirez-Ortiz | 61c0e37 | 2019-05-20 20:38:48 +0200 | [diff] [blame] | 894 | iowrite32_rep(tf, buf, 1); |
Ivan T. Ivanov | 3a878c4 | 2015-09-30 15:27:01 +0300 | [diff] [blame] | 895 | port->icount.tx++; |
| 896 | port->x_char = 0; |
| 897 | return; |
| 898 | } |
| 899 | |
| 900 | if (uart_circ_empty(xmit) || uart_tx_stopped(port)) { |
| 901 | msm_stop_tx(port); |
| 902 | return; |
| 903 | } |
| 904 | |
Bjorn Andersson | 30acf54 | 2016-06-02 17:48:28 -0700 | [diff] [blame] | 905 | pio_count = CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE); |
Ivan T. Ivanov | 3a878c4 | 2015-09-30 15:27:01 +0300 | [diff] [blame] | 906 | dma_count = CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE); |
| 907 | |
| 908 | dma_min = 1; /* Always DMA */ |
| 909 | if (msm_port->is_uartdm > UARTDM_1P3) { |
| 910 | dma_count = UARTDM_TX_AIGN(dma_count); |
| 911 | dma_min = UARTDM_BURST_SIZE; |
| 912 | } else { |
| 913 | if (dma_count > UARTDM_TX_MAX) |
| 914 | dma_count = UARTDM_TX_MAX; |
| 915 | } |
| 916 | |
| 917 | if (pio_count > port->fifosize) |
| 918 | pio_count = port->fifosize; |
| 919 | |
| 920 | if (!dma->chan || dma_count < dma_min) |
| 921 | msm_handle_tx_pio(port, pio_count); |
| 922 | else |
| 923 | err = msm_handle_tx_dma(msm_port, dma_count); |
| 924 | |
| 925 | if (err) /* fall back to PIO mode */ |
| 926 | msm_handle_tx_pio(port, pio_count); |
| 927 | } |
| 928 | |
Ivan T. Ivanov | 558abdb | 2015-09-30 15:27:00 +0300 | [diff] [blame] | 929 | static void msm_handle_delta_cts(struct uart_port *port) |
Robert Love | 04896a7 | 2009-06-22 18:43:11 +0100 | [diff] [blame] | 930 | { |
| 931 | msm_write(port, UART_CR_CMD_RESET_CTS, UART_CR); |
| 932 | port->icount.cts++; |
Alan Cox | bdc04e3 | 2009-09-19 13:13:31 -0700 | [diff] [blame] | 933 | wake_up_interruptible(&port->state->port.delta_msr_wait); |
Robert Love | 04896a7 | 2009-06-22 18:43:11 +0100 | [diff] [blame] | 934 | } |
| 935 | |
Ivan T. Ivanov | 558abdb | 2015-09-30 15:27:00 +0300 | [diff] [blame] | 936 | static irqreturn_t msm_uart_irq(int irq, void *dev_id) |
Robert Love | 04896a7 | 2009-06-22 18:43:11 +0100 | [diff] [blame] | 937 | { |
| 938 | struct uart_port *port = dev_id; |
| 939 | struct msm_port *msm_port = UART_TO_MSM(port); |
Ivan T. Ivanov | 9969394 | 2015-09-30 15:27:02 +0300 | [diff] [blame] | 940 | struct msm_dma *dma = &msm_port->rx_dma; |
Ivan T. Ivanov | 3a878c4 | 2015-09-30 15:27:01 +0300 | [diff] [blame] | 941 | unsigned long flags; |
Robert Love | 04896a7 | 2009-06-22 18:43:11 +0100 | [diff] [blame] | 942 | unsigned int misr; |
Ivan T. Ivanov | 9969394 | 2015-09-30 15:27:02 +0300 | [diff] [blame] | 943 | u32 val; |
Robert Love | 04896a7 | 2009-06-22 18:43:11 +0100 | [diff] [blame] | 944 | |
Ivan T. Ivanov | 3a878c4 | 2015-09-30 15:27:01 +0300 | [diff] [blame] | 945 | spin_lock_irqsave(&port->lock, flags); |
Robert Love | 04896a7 | 2009-06-22 18:43:11 +0100 | [diff] [blame] | 946 | misr = msm_read(port, UART_MISR); |
| 947 | msm_write(port, 0, UART_IMR); /* disable interrupt */ |
| 948 | |
Stephen Boyd | 0896d4d | 2014-10-29 11:14:38 -0700 | [diff] [blame] | 949 | if (misr & UART_IMR_RXBREAK_START) { |
| 950 | msm_port->break_detected = true; |
| 951 | msm_write(port, UART_CR_CMD_RESET_RXBREAK_START, UART_CR); |
| 952 | } |
| 953 | |
Stepan Moskovchenko | ec8f29e | 2010-12-21 12:38:05 -0800 | [diff] [blame] | 954 | if (misr & (UART_IMR_RXLEV | UART_IMR_RXSTALE)) { |
Ivan T. Ivanov | 9969394 | 2015-09-30 15:27:02 +0300 | [diff] [blame] | 955 | if (dma->count) { |
| 956 | val = UART_CR_CMD_STALE_EVENT_DISABLE; |
| 957 | msm_write(port, val, UART_CR); |
| 958 | val = UART_CR_CMD_RESET_STALE_INT; |
| 959 | msm_write(port, val, UART_CR); |
| 960 | /* |
| 961 | * Flush DMA input fifo to memory, this will also |
| 962 | * trigger DMA RX completion |
| 963 | */ |
| 964 | dmaengine_terminate_all(dma->chan); |
| 965 | } else if (msm_port->is_uartdm) { |
Ivan T. Ivanov | 558abdb | 2015-09-30 15:27:00 +0300 | [diff] [blame] | 966 | msm_handle_rx_dm(port, misr); |
Ivan T. Ivanov | 9969394 | 2015-09-30 15:27:02 +0300 | [diff] [blame] | 967 | } else { |
Ivan T. Ivanov | 558abdb | 2015-09-30 15:27:00 +0300 | [diff] [blame] | 968 | msm_handle_rx(port); |
Ivan T. Ivanov | 9969394 | 2015-09-30 15:27:02 +0300 | [diff] [blame] | 969 | } |
Stepan Moskovchenko | ec8f29e | 2010-12-21 12:38:05 -0800 | [diff] [blame] | 970 | } |
Robert Love | 04896a7 | 2009-06-22 18:43:11 +0100 | [diff] [blame] | 971 | if (misr & UART_IMR_TXLEV) |
Ivan T. Ivanov | 558abdb | 2015-09-30 15:27:00 +0300 | [diff] [blame] | 972 | msm_handle_tx(port); |
Robert Love | 04896a7 | 2009-06-22 18:43:11 +0100 | [diff] [blame] | 973 | if (misr & UART_IMR_DELTA_CTS) |
Ivan T. Ivanov | 558abdb | 2015-09-30 15:27:00 +0300 | [diff] [blame] | 974 | msm_handle_delta_cts(port); |
Robert Love | 04896a7 | 2009-06-22 18:43:11 +0100 | [diff] [blame] | 975 | |
| 976 | msm_write(port, msm_port->imr, UART_IMR); /* restore interrupt */ |
Ivan T. Ivanov | 3a878c4 | 2015-09-30 15:27:01 +0300 | [diff] [blame] | 977 | spin_unlock_irqrestore(&port->lock, flags); |
Robert Love | 04896a7 | 2009-06-22 18:43:11 +0100 | [diff] [blame] | 978 | |
| 979 | return IRQ_HANDLED; |
| 980 | } |
| 981 | |
| 982 | static unsigned int msm_tx_empty(struct uart_port *port) |
| 983 | { |
| 984 | return (msm_read(port, UART_SR) & UART_SR_TX_EMPTY) ? TIOCSER_TEMT : 0; |
| 985 | } |
| 986 | |
| 987 | static unsigned int msm_get_mctrl(struct uart_port *port) |
| 988 | { |
| 989 | return TIOCM_CAR | TIOCM_CTS | TIOCM_DSR | TIOCM_RTS; |
| 990 | } |
| 991 | |
Stepan Moskovchenko | ec8f29e | 2010-12-21 12:38:05 -0800 | [diff] [blame] | 992 | static void msm_reset(struct uart_port *port) |
| 993 | { |
Stephen Boyd | f7e54d7 | 2014-01-14 12:34:55 -0800 | [diff] [blame] | 994 | struct msm_port *msm_port = UART_TO_MSM(port); |
Jeffrey Hugo | b027ce2 | 2019-10-21 08:46:16 -0700 | [diff] [blame] | 995 | unsigned int mr; |
Stephen Boyd | f7e54d7 | 2014-01-14 12:34:55 -0800 | [diff] [blame] | 996 | |
Stepan Moskovchenko | ec8f29e | 2010-12-21 12:38:05 -0800 | [diff] [blame] | 997 | /* reset everything */ |
| 998 | msm_write(port, UART_CR_CMD_RESET_RX, UART_CR); |
| 999 | msm_write(port, UART_CR_CMD_RESET_TX, UART_CR); |
| 1000 | msm_write(port, UART_CR_CMD_RESET_ERR, UART_CR); |
| 1001 | msm_write(port, UART_CR_CMD_RESET_BREAK_INT, UART_CR); |
| 1002 | msm_write(port, UART_CR_CMD_RESET_CTS, UART_CR); |
Jeffrey Hugo | b027ce2 | 2019-10-21 08:46:16 -0700 | [diff] [blame] | 1003 | msm_write(port, UART_CR_CMD_RESET_RFR, UART_CR); |
| 1004 | mr = msm_read(port, UART_MR1); |
| 1005 | mr &= ~UART_MR1_RX_RDY_CTL; |
| 1006 | msm_write(port, mr, UART_MR1); |
Stephen Boyd | f7e54d7 | 2014-01-14 12:34:55 -0800 | [diff] [blame] | 1007 | |
| 1008 | /* Disable DM modes */ |
| 1009 | if (msm_port->is_uartdm) |
| 1010 | msm_write(port, 0, UARTDM_DMEN); |
Stepan Moskovchenko | ec8f29e | 2010-12-21 12:38:05 -0800 | [diff] [blame] | 1011 | } |
| 1012 | |
Stephen Boyd | f8fb952 | 2013-07-24 11:37:29 -0700 | [diff] [blame] | 1013 | static void msm_set_mctrl(struct uart_port *port, unsigned int mctrl) |
Robert Love | 04896a7 | 2009-06-22 18:43:11 +0100 | [diff] [blame] | 1014 | { |
| 1015 | unsigned int mr; |
Kiran Padwal | e919cef | 2014-08-05 13:22:00 +0530 | [diff] [blame] | 1016 | |
Robert Love | 04896a7 | 2009-06-22 18:43:11 +0100 | [diff] [blame] | 1017 | mr = msm_read(port, UART_MR1); |
| 1018 | |
| 1019 | if (!(mctrl & TIOCM_RTS)) { |
| 1020 | mr &= ~UART_MR1_RX_RDY_CTL; |
| 1021 | msm_write(port, mr, UART_MR1); |
| 1022 | msm_write(port, UART_CR_CMD_RESET_RFR, UART_CR); |
| 1023 | } else { |
| 1024 | mr |= UART_MR1_RX_RDY_CTL; |
| 1025 | msm_write(port, mr, UART_MR1); |
| 1026 | } |
| 1027 | } |
| 1028 | |
| 1029 | static void msm_break_ctl(struct uart_port *port, int break_ctl) |
| 1030 | { |
| 1031 | if (break_ctl) |
| 1032 | msm_write(port, UART_CR_CMD_START_BREAK, UART_CR); |
| 1033 | else |
| 1034 | msm_write(port, UART_CR_CMD_STOP_BREAK, UART_CR); |
| 1035 | } |
| 1036 | |
Stephen Boyd | 6909dad | 2013-07-24 11:37:30 -0700 | [diff] [blame] | 1037 | struct msm_baud_map { |
| 1038 | u16 divisor; |
| 1039 | u8 code; |
| 1040 | u8 rxstale; |
| 1041 | }; |
| 1042 | |
| 1043 | static const struct msm_baud_map * |
Stephen Boyd | 98952bf | 2016-03-25 14:35:49 -0700 | [diff] [blame] | 1044 | msm_find_best_baud(struct uart_port *port, unsigned int baud, |
| 1045 | unsigned long *rate) |
Stephen Boyd | 6909dad | 2013-07-24 11:37:30 -0700 | [diff] [blame] | 1046 | { |
Stephen Boyd | 98952bf | 2016-03-25 14:35:49 -0700 | [diff] [blame] | 1047 | struct msm_port *msm_port = UART_TO_MSM(port); |
| 1048 | unsigned int divisor, result; |
| 1049 | unsigned long target, old, best_rate = 0, diff, best_diff = ULONG_MAX; |
| 1050 | const struct msm_baud_map *entry, *end, *best; |
Stephen Boyd | 6909dad | 2013-07-24 11:37:30 -0700 | [diff] [blame] | 1051 | static const struct msm_baud_map table[] = { |
Stephen Boyd | 6909dad | 2013-07-24 11:37:30 -0700 | [diff] [blame] | 1052 | { 1, 0xff, 31 }, |
Stephen Boyd | 98952bf | 2016-03-25 14:35:49 -0700 | [diff] [blame] | 1053 | { 2, 0xee, 16 }, |
| 1054 | { 3, 0xdd, 8 }, |
| 1055 | { 4, 0xcc, 6 }, |
| 1056 | { 6, 0xbb, 6 }, |
| 1057 | { 8, 0xaa, 6 }, |
| 1058 | { 12, 0x99, 6 }, |
| 1059 | { 16, 0x88, 1 }, |
| 1060 | { 24, 0x77, 1 }, |
| 1061 | { 32, 0x66, 1 }, |
| 1062 | { 48, 0x55, 1 }, |
| 1063 | { 96, 0x44, 1 }, |
| 1064 | { 192, 0x33, 1 }, |
| 1065 | { 384, 0x22, 1 }, |
| 1066 | { 768, 0x11, 1 }, |
| 1067 | { 1536, 0x00, 1 }, |
Stephen Boyd | 6909dad | 2013-07-24 11:37:30 -0700 | [diff] [blame] | 1068 | }; |
| 1069 | |
Stephen Boyd | 98952bf | 2016-03-25 14:35:49 -0700 | [diff] [blame] | 1070 | best = table; /* Default to smallest divider */ |
| 1071 | target = clk_round_rate(msm_port->clk, 16 * baud); |
| 1072 | divisor = DIV_ROUND_CLOSEST(target, 16 * baud); |
Stephen Boyd | 6909dad | 2013-07-24 11:37:30 -0700 | [diff] [blame] | 1073 | |
Stephen Boyd | 98952bf | 2016-03-25 14:35:49 -0700 | [diff] [blame] | 1074 | end = table + ARRAY_SIZE(table); |
| 1075 | entry = table; |
| 1076 | while (entry < end) { |
| 1077 | if (entry->divisor <= divisor) { |
| 1078 | result = target / entry->divisor / 16; |
| 1079 | diff = abs(result - baud); |
Stephen Boyd | 6909dad | 2013-07-24 11:37:30 -0700 | [diff] [blame] | 1080 | |
Stephen Boyd | 98952bf | 2016-03-25 14:35:49 -0700 | [diff] [blame] | 1081 | /* Keep track of best entry */ |
| 1082 | if (diff < best_diff) { |
| 1083 | best_diff = diff; |
| 1084 | best = entry; |
| 1085 | best_rate = target; |
| 1086 | } |
| 1087 | |
| 1088 | if (result == baud) |
| 1089 | break; |
| 1090 | } else if (entry->divisor > divisor) { |
| 1091 | old = target; |
| 1092 | target = clk_round_rate(msm_port->clk, old + 1); |
| 1093 | /* |
| 1094 | * The rate didn't get any faster so we can't do |
| 1095 | * better at dividing it down |
| 1096 | */ |
| 1097 | if (target == old) |
| 1098 | break; |
| 1099 | |
| 1100 | /* Start the divisor search over at this new rate */ |
| 1101 | entry = table; |
| 1102 | divisor = DIV_ROUND_CLOSEST(target, 16 * baud); |
| 1103 | continue; |
| 1104 | } |
| 1105 | entry++; |
| 1106 | } |
| 1107 | |
| 1108 | *rate = best_rate; |
| 1109 | return best; |
Stephen Boyd | 6909dad | 2013-07-24 11:37:30 -0700 | [diff] [blame] | 1110 | } |
| 1111 | |
Ivan T. Ivanov | 850b37a | 2015-09-30 15:27:03 +0300 | [diff] [blame] | 1112 | static int msm_set_baud_rate(struct uart_port *port, unsigned int baud, |
| 1113 | unsigned long *saved_flags) |
Robert Love | 04896a7 | 2009-06-22 18:43:11 +0100 | [diff] [blame] | 1114 | { |
Pramod Gurav | 12b9b9f | 2015-09-30 15:26:58 +0300 | [diff] [blame] | 1115 | unsigned int rxstale, watermark, mask; |
Stepan Moskovchenko | ec8f29e | 2010-12-21 12:38:05 -0800 | [diff] [blame] | 1116 | struct msm_port *msm_port = UART_TO_MSM(port); |
Stephen Boyd | 6909dad | 2013-07-24 11:37:30 -0700 | [diff] [blame] | 1117 | const struct msm_baud_map *entry; |
Stephen Boyd | 98952bf | 2016-03-25 14:35:49 -0700 | [diff] [blame] | 1118 | unsigned long flags, rate; |
Ivan T. Ivanov | 850b37a | 2015-09-30 15:27:03 +0300 | [diff] [blame] | 1119 | |
| 1120 | flags = *saved_flags; |
| 1121 | spin_unlock_irqrestore(&port->lock, flags); |
| 1122 | |
Stephen Boyd | 98952bf | 2016-03-25 14:35:49 -0700 | [diff] [blame] | 1123 | entry = msm_find_best_baud(port, baud, &rate); |
| 1124 | clk_set_rate(msm_port->clk, rate); |
| 1125 | baud = rate / 16 / entry->divisor; |
Ivan T. Ivanov | 850b37a | 2015-09-30 15:27:03 +0300 | [diff] [blame] | 1126 | |
| 1127 | spin_lock_irqsave(&port->lock, flags); |
| 1128 | *saved_flags = flags; |
Stephen Boyd | 98952bf | 2016-03-25 14:35:49 -0700 | [diff] [blame] | 1129 | port->uartclk = rate; |
| 1130 | |
| 1131 | msm_write(port, entry->code, UART_CSR); |
Ivan T. Ivanov | 850b37a | 2015-09-30 15:27:03 +0300 | [diff] [blame] | 1132 | |
Robert Love | 04896a7 | 2009-06-22 18:43:11 +0100 | [diff] [blame] | 1133 | /* RX stale watermark */ |
Stephen Boyd | 6909dad | 2013-07-24 11:37:30 -0700 | [diff] [blame] | 1134 | rxstale = entry->rxstale; |
Robert Love | 04896a7 | 2009-06-22 18:43:11 +0100 | [diff] [blame] | 1135 | watermark = UART_IPR_STALE_LSB & rxstale; |
Pramod Gurav | 12b9b9f | 2015-09-30 15:26:58 +0300 | [diff] [blame] | 1136 | if (msm_port->is_uartdm) { |
| 1137 | mask = UART_DM_IPR_STALE_TIMEOUT_MSB; |
| 1138 | } else { |
| 1139 | watermark |= UART_IPR_RXSTALE_LAST; |
| 1140 | mask = UART_IPR_STALE_TIMEOUT_MSB; |
| 1141 | } |
| 1142 | |
| 1143 | watermark |= mask & (rxstale << 2); |
| 1144 | |
Robert Love | 04896a7 | 2009-06-22 18:43:11 +0100 | [diff] [blame] | 1145 | msm_write(port, watermark, UART_IPR); |
| 1146 | |
| 1147 | /* set RX watermark */ |
| 1148 | watermark = (port->fifosize * 3) / 4; |
| 1149 | msm_write(port, watermark, UART_RFWR); |
| 1150 | |
| 1151 | /* set TX watermark */ |
| 1152 | msm_write(port, 10, UART_TFWR); |
Alan Cox | 44da59e | 2009-06-22 18:43:18 +0100 | [diff] [blame] | 1153 | |
Stephen Boyd | a12f1b4 | 2014-10-29 18:47:01 -0700 | [diff] [blame] | 1154 | msm_write(port, UART_CR_CMD_PROTECTION_EN, UART_CR); |
| 1155 | msm_reset(port); |
| 1156 | |
| 1157 | /* Enable RX and TX */ |
| 1158 | msm_write(port, UART_CR_TX_ENABLE | UART_CR_RX_ENABLE, UART_CR); |
| 1159 | |
| 1160 | /* turn on RX and CTS interrupts */ |
| 1161 | msm_port->imr = UART_IMR_RXLEV | UART_IMR_RXSTALE | |
| 1162 | UART_IMR_CURRENT_CTS | UART_IMR_RXBREAK_START; |
| 1163 | |
| 1164 | msm_write(port, msm_port->imr, UART_IMR); |
| 1165 | |
Stepan Moskovchenko | ec8f29e | 2010-12-21 12:38:05 -0800 | [diff] [blame] | 1166 | if (msm_port->is_uartdm) { |
| 1167 | msm_write(port, UART_CR_CMD_RESET_STALE_INT, UART_CR); |
| 1168 | msm_write(port, 0xFFFFFF, UARTDM_DMRX); |
| 1169 | msm_write(port, UART_CR_CMD_STALE_EVENT_ENABLE, UART_CR); |
| 1170 | } |
| 1171 | |
Alan Cox | 44da59e | 2009-06-22 18:43:18 +0100 | [diff] [blame] | 1172 | return baud; |
Robert Love | 04896a7 | 2009-06-22 18:43:11 +0100 | [diff] [blame] | 1173 | } |
| 1174 | |
Robert Love | 04896a7 | 2009-06-22 18:43:11 +0100 | [diff] [blame] | 1175 | static void msm_init_clock(struct uart_port *port) |
| 1176 | { |
| 1177 | struct msm_port *msm_port = UART_TO_MSM(port); |
| 1178 | |
Stephen Boyd | f98cf83 | 2013-06-17 10:43:08 -0700 | [diff] [blame] | 1179 | clk_prepare_enable(msm_port->clk); |
Stephen Boyd | bfaddb7 | 2013-08-20 23:48:02 -0700 | [diff] [blame] | 1180 | clk_prepare_enable(msm_port->pclk); |
Abhijeet Dharmapurikar | 18c79d7 | 2010-05-20 15:20:23 -0700 | [diff] [blame] | 1181 | msm_serial_set_mnd_regs(port); |
Robert Love | 04896a7 | 2009-06-22 18:43:11 +0100 | [diff] [blame] | 1182 | } |
| 1183 | |
| 1184 | static int msm_startup(struct uart_port *port) |
| 1185 | { |
| 1186 | struct msm_port *msm_port = UART_TO_MSM(port); |
Pramod Gurav | 12b9b9f | 2015-09-30 15:26:58 +0300 | [diff] [blame] | 1187 | unsigned int data, rfr_level, mask; |
Robert Love | 04896a7 | 2009-06-22 18:43:11 +0100 | [diff] [blame] | 1188 | int ret; |
| 1189 | |
| 1190 | snprintf(msm_port->name, sizeof(msm_port->name), |
| 1191 | "msm_serial%d", port->line); |
| 1192 | |
Robert Love | 04896a7 | 2009-06-22 18:43:11 +0100 | [diff] [blame] | 1193 | msm_init_clock(port); |
| 1194 | |
| 1195 | if (likely(port->fifosize > 12)) |
| 1196 | rfr_level = port->fifosize - 12; |
| 1197 | else |
| 1198 | rfr_level = port->fifosize; |
| 1199 | |
| 1200 | /* set automatic RFR level */ |
| 1201 | data = msm_read(port, UART_MR1); |
Pramod Gurav | 12b9b9f | 2015-09-30 15:26:58 +0300 | [diff] [blame] | 1202 | |
| 1203 | if (msm_port->is_uartdm) |
| 1204 | mask = UART_DM_MR1_AUTO_RFR_LEVEL1; |
| 1205 | else |
| 1206 | mask = UART_MR1_AUTO_RFR_LEVEL1; |
| 1207 | |
| 1208 | data &= ~mask; |
Robert Love | 04896a7 | 2009-06-22 18:43:11 +0100 | [diff] [blame] | 1209 | data &= ~UART_MR1_AUTO_RFR_LEVEL0; |
Pramod Gurav | 12b9b9f | 2015-09-30 15:26:58 +0300 | [diff] [blame] | 1210 | data |= mask & (rfr_level << 2); |
Robert Love | 04896a7 | 2009-06-22 18:43:11 +0100 | [diff] [blame] | 1211 | data |= UART_MR1_AUTO_RFR_LEVEL0 & rfr_level; |
| 1212 | msm_write(port, data, UART_MR1); |
Pramod Gurav | 12b9b9f | 2015-09-30 15:26:58 +0300 | [diff] [blame] | 1213 | |
Ivan T. Ivanov | 9969394 | 2015-09-30 15:27:02 +0300 | [diff] [blame] | 1214 | if (msm_port->is_uartdm) { |
Ivan T. Ivanov | 3a878c4 | 2015-09-30 15:27:01 +0300 | [diff] [blame] | 1215 | msm_request_tx_dma(msm_port, msm_port->uart.mapbase); |
Ivan T. Ivanov | 9969394 | 2015-09-30 15:27:02 +0300 | [diff] [blame] | 1216 | msm_request_rx_dma(msm_port, msm_port->uart.mapbase); |
| 1217 | } |
Ivan T. Ivanov | 3a878c4 | 2015-09-30 15:27:01 +0300 | [diff] [blame] | 1218 | |
Neeraj Upadhyay | 4d691f7 | 2017-08-07 11:51:25 +0530 | [diff] [blame] | 1219 | ret = request_irq(port->irq, msm_uart_irq, IRQF_TRIGGER_HIGH, |
| 1220 | msm_port->name, port); |
| 1221 | if (unlikely(ret)) |
| 1222 | goto err_irq; |
| 1223 | |
Robert Love | 04896a7 | 2009-06-22 18:43:11 +0100 | [diff] [blame] | 1224 | return 0; |
Neeraj Upadhyay | 4d691f7 | 2017-08-07 11:51:25 +0530 | [diff] [blame] | 1225 | |
| 1226 | err_irq: |
| 1227 | if (msm_port->is_uartdm) |
| 1228 | msm_release_dma(msm_port); |
| 1229 | |
| 1230 | clk_disable_unprepare(msm_port->pclk); |
| 1231 | clk_disable_unprepare(msm_port->clk); |
| 1232 | |
| 1233 | return ret; |
Robert Love | 04896a7 | 2009-06-22 18:43:11 +0100 | [diff] [blame] | 1234 | } |
| 1235 | |
| 1236 | static void msm_shutdown(struct uart_port *port) |
| 1237 | { |
| 1238 | struct msm_port *msm_port = UART_TO_MSM(port); |
| 1239 | |
| 1240 | msm_port->imr = 0; |
| 1241 | msm_write(port, 0, UART_IMR); /* disable interrupts */ |
| 1242 | |
Ivan T. Ivanov | 3a878c4 | 2015-09-30 15:27:01 +0300 | [diff] [blame] | 1243 | if (msm_port->is_uartdm) |
| 1244 | msm_release_dma(msm_port); |
| 1245 | |
Stephen Boyd | f98cf83 | 2013-06-17 10:43:08 -0700 | [diff] [blame] | 1246 | clk_disable_unprepare(msm_port->clk); |
Robert Love | 04896a7 | 2009-06-22 18:43:11 +0100 | [diff] [blame] | 1247 | |
| 1248 | free_irq(port->irq, port); |
| 1249 | } |
| 1250 | |
| 1251 | static void msm_set_termios(struct uart_port *port, struct ktermios *termios, |
| 1252 | struct ktermios *old) |
| 1253 | { |
Ivan T. Ivanov | 9969394 | 2015-09-30 15:27:02 +0300 | [diff] [blame] | 1254 | struct msm_port *msm_port = UART_TO_MSM(port); |
| 1255 | struct msm_dma *dma = &msm_port->rx_dma; |
Robert Love | 04896a7 | 2009-06-22 18:43:11 +0100 | [diff] [blame] | 1256 | unsigned long flags; |
| 1257 | unsigned int baud, mr; |
| 1258 | |
| 1259 | spin_lock_irqsave(&port->lock, flags); |
| 1260 | |
Ivan T. Ivanov | 9969394 | 2015-09-30 15:27:02 +0300 | [diff] [blame] | 1261 | if (dma->chan) /* Terminate if any */ |
| 1262 | msm_stop_dma(port, dma); |
| 1263 | |
Robert Love | 04896a7 | 2009-06-22 18:43:11 +0100 | [diff] [blame] | 1264 | /* calculate and set baud rate */ |
Ivan T. Ivanov | 850b37a | 2015-09-30 15:27:03 +0300 | [diff] [blame] | 1265 | baud = uart_get_baud_rate(port, termios, old, 300, 4000000); |
| 1266 | baud = msm_set_baud_rate(port, baud, &flags); |
Alan Cox | 44da59e | 2009-06-22 18:43:18 +0100 | [diff] [blame] | 1267 | if (tty_termios_baud_rate(termios)) |
| 1268 | tty_termios_encode_baud_rate(termios, baud, baud); |
Stepan Moskovchenko | ec8f29e | 2010-12-21 12:38:05 -0800 | [diff] [blame] | 1269 | |
Robert Love | 04896a7 | 2009-06-22 18:43:11 +0100 | [diff] [blame] | 1270 | /* calculate parity */ |
| 1271 | mr = msm_read(port, UART_MR2); |
| 1272 | mr &= ~UART_MR2_PARITY_MODE; |
| 1273 | if (termios->c_cflag & PARENB) { |
| 1274 | if (termios->c_cflag & PARODD) |
| 1275 | mr |= UART_MR2_PARITY_MODE_ODD; |
| 1276 | else if (termios->c_cflag & CMSPAR) |
| 1277 | mr |= UART_MR2_PARITY_MODE_SPACE; |
| 1278 | else |
| 1279 | mr |= UART_MR2_PARITY_MODE_EVEN; |
| 1280 | } |
| 1281 | |
| 1282 | /* calculate bits per char */ |
| 1283 | mr &= ~UART_MR2_BITS_PER_CHAR; |
| 1284 | switch (termios->c_cflag & CSIZE) { |
| 1285 | case CS5: |
| 1286 | mr |= UART_MR2_BITS_PER_CHAR_5; |
| 1287 | break; |
| 1288 | case CS6: |
| 1289 | mr |= UART_MR2_BITS_PER_CHAR_6; |
| 1290 | break; |
| 1291 | case CS7: |
| 1292 | mr |= UART_MR2_BITS_PER_CHAR_7; |
| 1293 | break; |
| 1294 | case CS8: |
| 1295 | default: |
| 1296 | mr |= UART_MR2_BITS_PER_CHAR_8; |
| 1297 | break; |
| 1298 | } |
| 1299 | |
| 1300 | /* calculate stop bits */ |
| 1301 | mr &= ~(UART_MR2_STOP_BIT_LEN_ONE | UART_MR2_STOP_BIT_LEN_TWO); |
| 1302 | if (termios->c_cflag & CSTOPB) |
| 1303 | mr |= UART_MR2_STOP_BIT_LEN_TWO; |
| 1304 | else |
| 1305 | mr |= UART_MR2_STOP_BIT_LEN_ONE; |
| 1306 | |
| 1307 | /* set parity, bits per char, and stop bit */ |
| 1308 | msm_write(port, mr, UART_MR2); |
| 1309 | |
| 1310 | /* calculate and set hardware flow control */ |
| 1311 | mr = msm_read(port, UART_MR1); |
| 1312 | mr &= ~(UART_MR1_CTS_CTL | UART_MR1_RX_RDY_CTL); |
| 1313 | if (termios->c_cflag & CRTSCTS) { |
| 1314 | mr |= UART_MR1_CTS_CTL; |
| 1315 | mr |= UART_MR1_RX_RDY_CTL; |
| 1316 | } |
| 1317 | msm_write(port, mr, UART_MR1); |
| 1318 | |
| 1319 | /* Configure status bits to ignore based on termio flags. */ |
| 1320 | port->read_status_mask = 0; |
| 1321 | if (termios->c_iflag & INPCK) |
| 1322 | port->read_status_mask |= UART_SR_PAR_FRAME_ERR; |
Peter Hurley | ef8b9dd | 2014-06-16 08:10:41 -0400 | [diff] [blame] | 1323 | if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK)) |
Robert Love | 04896a7 | 2009-06-22 18:43:11 +0100 | [diff] [blame] | 1324 | port->read_status_mask |= UART_SR_RX_BREAK; |
| 1325 | |
| 1326 | uart_update_timeout(port, termios->c_cflag, baud); |
| 1327 | |
Ivan T. Ivanov | 9969394 | 2015-09-30 15:27:02 +0300 | [diff] [blame] | 1328 | /* Try to use DMA */ |
| 1329 | msm_start_rx_dma(msm_port); |
| 1330 | |
Robert Love | 04896a7 | 2009-06-22 18:43:11 +0100 | [diff] [blame] | 1331 | spin_unlock_irqrestore(&port->lock, flags); |
| 1332 | } |
| 1333 | |
| 1334 | static const char *msm_type(struct uart_port *port) |
| 1335 | { |
| 1336 | return "MSM"; |
| 1337 | } |
| 1338 | |
| 1339 | static void msm_release_port(struct uart_port *port) |
| 1340 | { |
| 1341 | struct platform_device *pdev = to_platform_device(port->dev); |
Stepan Moskovchenko | ec8f29e | 2010-12-21 12:38:05 -0800 | [diff] [blame] | 1342 | struct resource *uart_resource; |
Robert Love | 04896a7 | 2009-06-22 18:43:11 +0100 | [diff] [blame] | 1343 | resource_size_t size; |
| 1344 | |
Stepan Moskovchenko | ec8f29e | 2010-12-21 12:38:05 -0800 | [diff] [blame] | 1345 | uart_resource = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 1346 | if (unlikely(!uart_resource)) |
Robert Love | 04896a7 | 2009-06-22 18:43:11 +0100 | [diff] [blame] | 1347 | return; |
Stepan Moskovchenko | ec8f29e | 2010-12-21 12:38:05 -0800 | [diff] [blame] | 1348 | size = resource_size(uart_resource); |
Robert Love | 04896a7 | 2009-06-22 18:43:11 +0100 | [diff] [blame] | 1349 | |
| 1350 | release_mem_region(port->mapbase, size); |
| 1351 | iounmap(port->membase); |
| 1352 | port->membase = NULL; |
| 1353 | } |
| 1354 | |
| 1355 | static int msm_request_port(struct uart_port *port) |
| 1356 | { |
| 1357 | struct platform_device *pdev = to_platform_device(port->dev); |
Stepan Moskovchenko | ec8f29e | 2010-12-21 12:38:05 -0800 | [diff] [blame] | 1358 | struct resource *uart_resource; |
Robert Love | 04896a7 | 2009-06-22 18:43:11 +0100 | [diff] [blame] | 1359 | resource_size_t size; |
Stepan Moskovchenko | ec8f29e | 2010-12-21 12:38:05 -0800 | [diff] [blame] | 1360 | int ret; |
Robert Love | 04896a7 | 2009-06-22 18:43:11 +0100 | [diff] [blame] | 1361 | |
David Brown | 886a451 | 2011-08-02 09:02:49 -0700 | [diff] [blame] | 1362 | uart_resource = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
Stepan Moskovchenko | ec8f29e | 2010-12-21 12:38:05 -0800 | [diff] [blame] | 1363 | if (unlikely(!uart_resource)) |
Robert Love | 04896a7 | 2009-06-22 18:43:11 +0100 | [diff] [blame] | 1364 | return -ENXIO; |
Robert Love | 04896a7 | 2009-06-22 18:43:11 +0100 | [diff] [blame] | 1365 | |
Stepan Moskovchenko | ec8f29e | 2010-12-21 12:38:05 -0800 | [diff] [blame] | 1366 | size = resource_size(uart_resource); |
| 1367 | |
| 1368 | if (!request_mem_region(port->mapbase, size, "msm_serial")) |
Robert Love | 04896a7 | 2009-06-22 18:43:11 +0100 | [diff] [blame] | 1369 | return -EBUSY; |
| 1370 | |
| 1371 | port->membase = ioremap(port->mapbase, size); |
| 1372 | if (!port->membase) { |
Stepan Moskovchenko | ec8f29e | 2010-12-21 12:38:05 -0800 | [diff] [blame] | 1373 | ret = -EBUSY; |
| 1374 | goto fail_release_port; |
| 1375 | } |
| 1376 | |
Robert Love | 04896a7 | 2009-06-22 18:43:11 +0100 | [diff] [blame] | 1377 | return 0; |
Stepan Moskovchenko | ec8f29e | 2010-12-21 12:38:05 -0800 | [diff] [blame] | 1378 | |
Stepan Moskovchenko | ec8f29e | 2010-12-21 12:38:05 -0800 | [diff] [blame] | 1379 | fail_release_port: |
| 1380 | release_mem_region(port->mapbase, size); |
| 1381 | return ret; |
Robert Love | 04896a7 | 2009-06-22 18:43:11 +0100 | [diff] [blame] | 1382 | } |
| 1383 | |
| 1384 | static void msm_config_port(struct uart_port *port, int flags) |
| 1385 | { |
Stepan Moskovchenko | ec8f29e | 2010-12-21 12:38:05 -0800 | [diff] [blame] | 1386 | int ret; |
Kiran Padwal | e919cef | 2014-08-05 13:22:00 +0530 | [diff] [blame] | 1387 | |
Robert Love | 04896a7 | 2009-06-22 18:43:11 +0100 | [diff] [blame] | 1388 | if (flags & UART_CONFIG_TYPE) { |
| 1389 | port->type = PORT_MSM; |
Stepan Moskovchenko | ec8f29e | 2010-12-21 12:38:05 -0800 | [diff] [blame] | 1390 | ret = msm_request_port(port); |
| 1391 | if (ret) |
| 1392 | return; |
Robert Love | 04896a7 | 2009-06-22 18:43:11 +0100 | [diff] [blame] | 1393 | } |
| 1394 | } |
| 1395 | |
| 1396 | static int msm_verify_port(struct uart_port *port, struct serial_struct *ser) |
| 1397 | { |
| 1398 | if (unlikely(ser->type != PORT_UNKNOWN && ser->type != PORT_MSM)) |
| 1399 | return -EINVAL; |
| 1400 | if (unlikely(port->irq != ser->irq)) |
| 1401 | return -EINVAL; |
| 1402 | return 0; |
| 1403 | } |
| 1404 | |
| 1405 | static void msm_power(struct uart_port *port, unsigned int state, |
| 1406 | unsigned int oldstate) |
| 1407 | { |
| 1408 | struct msm_port *msm_port = UART_TO_MSM(port); |
| 1409 | |
| 1410 | switch (state) { |
| 1411 | case 0: |
Stephen Boyd | f98cf83 | 2013-06-17 10:43:08 -0700 | [diff] [blame] | 1412 | clk_prepare_enable(msm_port->clk); |
Stephen Boyd | bfaddb7 | 2013-08-20 23:48:02 -0700 | [diff] [blame] | 1413 | clk_prepare_enable(msm_port->pclk); |
Robert Love | 04896a7 | 2009-06-22 18:43:11 +0100 | [diff] [blame] | 1414 | break; |
| 1415 | case 3: |
Stephen Boyd | f98cf83 | 2013-06-17 10:43:08 -0700 | [diff] [blame] | 1416 | clk_disable_unprepare(msm_port->clk); |
Stephen Boyd | bfaddb7 | 2013-08-20 23:48:02 -0700 | [diff] [blame] | 1417 | clk_disable_unprepare(msm_port->pclk); |
Robert Love | 04896a7 | 2009-06-22 18:43:11 +0100 | [diff] [blame] | 1418 | break; |
| 1419 | default: |
Kiran Padwal | 6a7cfe4 | 2014-08-05 13:22:01 +0530 | [diff] [blame] | 1420 | pr_err("msm_serial: Unknown PM state %d\n", state); |
Robert Love | 04896a7 | 2009-06-22 18:43:11 +0100 | [diff] [blame] | 1421 | } |
| 1422 | } |
| 1423 | |
Stephen Boyd | f7e54d7 | 2014-01-14 12:34:55 -0800 | [diff] [blame] | 1424 | #ifdef CONFIG_CONSOLE_POLL |
Stephen Boyd | f7e54d7 | 2014-01-14 12:34:55 -0800 | [diff] [blame] | 1425 | static int msm_poll_get_char_single(struct uart_port *port) |
| 1426 | { |
| 1427 | struct msm_port *msm_port = UART_TO_MSM(port); |
| 1428 | unsigned int rf_reg = msm_port->is_uartdm ? UARTDM_RF : UART_RF; |
| 1429 | |
| 1430 | if (!(msm_read(port, UART_SR) & UART_SR_RX_READY)) |
| 1431 | return NO_POLL_CHAR; |
Kiran Padwal | 6f47abc | 2014-08-05 13:22:02 +0530 | [diff] [blame] | 1432 | |
| 1433 | return msm_read(port, rf_reg) & 0xff; |
Stephen Boyd | f7e54d7 | 2014-01-14 12:34:55 -0800 | [diff] [blame] | 1434 | } |
| 1435 | |
Stephen Boyd | 8b37439 | 2014-08-05 18:37:24 -0700 | [diff] [blame] | 1436 | static int msm_poll_get_char_dm(struct uart_port *port) |
Stephen Boyd | f7e54d7 | 2014-01-14 12:34:55 -0800 | [diff] [blame] | 1437 | { |
| 1438 | int c; |
| 1439 | static u32 slop; |
| 1440 | static int count; |
| 1441 | unsigned char *sp = (unsigned char *)&slop; |
| 1442 | |
| 1443 | /* Check if a previous read had more than one char */ |
| 1444 | if (count) { |
| 1445 | c = sp[sizeof(slop) - count]; |
| 1446 | count--; |
| 1447 | /* Or if FIFO is empty */ |
| 1448 | } else if (!(msm_read(port, UART_SR) & UART_SR_RX_READY)) { |
| 1449 | /* |
| 1450 | * If RX packing buffer has less than a word, force stale to |
| 1451 | * push contents into RX FIFO |
| 1452 | */ |
| 1453 | count = msm_read(port, UARTDM_RXFS); |
| 1454 | count = (count >> UARTDM_RXFS_BUF_SHIFT) & UARTDM_RXFS_BUF_MASK; |
| 1455 | if (count) { |
| 1456 | msm_write(port, UART_CR_CMD_FORCE_STALE, UART_CR); |
| 1457 | slop = msm_read(port, UARTDM_RF); |
| 1458 | c = sp[0]; |
| 1459 | count--; |
Stephen Boyd | 8b37439 | 2014-08-05 18:37:24 -0700 | [diff] [blame] | 1460 | msm_write(port, UART_CR_CMD_RESET_STALE_INT, UART_CR); |
| 1461 | msm_write(port, 0xFFFFFF, UARTDM_DMRX); |
| 1462 | msm_write(port, UART_CR_CMD_STALE_EVENT_ENABLE, |
| 1463 | UART_CR); |
Stephen Boyd | f7e54d7 | 2014-01-14 12:34:55 -0800 | [diff] [blame] | 1464 | } else { |
| 1465 | c = NO_POLL_CHAR; |
| 1466 | } |
| 1467 | /* FIFO has a word */ |
| 1468 | } else { |
| 1469 | slop = msm_read(port, UARTDM_RF); |
| 1470 | c = sp[0]; |
| 1471 | count = sizeof(slop) - 1; |
| 1472 | } |
| 1473 | |
| 1474 | return c; |
| 1475 | } |
| 1476 | |
| 1477 | static int msm_poll_get_char(struct uart_port *port) |
| 1478 | { |
| 1479 | u32 imr; |
| 1480 | int c; |
| 1481 | struct msm_port *msm_port = UART_TO_MSM(port); |
| 1482 | |
| 1483 | /* Disable all interrupts */ |
| 1484 | imr = msm_read(port, UART_IMR); |
| 1485 | msm_write(port, 0, UART_IMR); |
| 1486 | |
Stephen Boyd | 8b37439 | 2014-08-05 18:37:24 -0700 | [diff] [blame] | 1487 | if (msm_port->is_uartdm) |
| 1488 | c = msm_poll_get_char_dm(port); |
Stephen Boyd | f7e54d7 | 2014-01-14 12:34:55 -0800 | [diff] [blame] | 1489 | else |
| 1490 | c = msm_poll_get_char_single(port); |
| 1491 | |
| 1492 | /* Enable interrupts */ |
| 1493 | msm_write(port, imr, UART_IMR); |
| 1494 | |
| 1495 | return c; |
| 1496 | } |
| 1497 | |
| 1498 | static void msm_poll_put_char(struct uart_port *port, unsigned char c) |
| 1499 | { |
| 1500 | u32 imr; |
| 1501 | struct msm_port *msm_port = UART_TO_MSM(port); |
| 1502 | |
| 1503 | /* Disable all interrupts */ |
| 1504 | imr = msm_read(port, UART_IMR); |
| 1505 | msm_write(port, 0, UART_IMR); |
| 1506 | |
| 1507 | if (msm_port->is_uartdm) |
Ivan T. Ivanov | 558abdb | 2015-09-30 15:27:00 +0300 | [diff] [blame] | 1508 | msm_reset_dm_count(port, 1); |
Stephen Boyd | f7e54d7 | 2014-01-14 12:34:55 -0800 | [diff] [blame] | 1509 | |
| 1510 | /* Wait until FIFO is empty */ |
| 1511 | while (!(msm_read(port, UART_SR) & UART_SR_TX_READY)) |
| 1512 | cpu_relax(); |
| 1513 | |
| 1514 | /* Write a character */ |
| 1515 | msm_write(port, c, msm_port->is_uartdm ? UARTDM_TF : UART_TF); |
| 1516 | |
| 1517 | /* Wait until FIFO is empty */ |
| 1518 | while (!(msm_read(port, UART_SR) & UART_SR_TX_READY)) |
| 1519 | cpu_relax(); |
| 1520 | |
| 1521 | /* Enable interrupts */ |
| 1522 | msm_write(port, imr, UART_IMR); |
Stephen Boyd | f7e54d7 | 2014-01-14 12:34:55 -0800 | [diff] [blame] | 1523 | } |
| 1524 | #endif |
| 1525 | |
Rikard Falkeborn | 0d66442 | 2020-11-05 00:51:34 +0100 | [diff] [blame] | 1526 | static const struct uart_ops msm_uart_pops = { |
Robert Love | 04896a7 | 2009-06-22 18:43:11 +0100 | [diff] [blame] | 1527 | .tx_empty = msm_tx_empty, |
| 1528 | .set_mctrl = msm_set_mctrl, |
| 1529 | .get_mctrl = msm_get_mctrl, |
| 1530 | .stop_tx = msm_stop_tx, |
| 1531 | .start_tx = msm_start_tx, |
| 1532 | .stop_rx = msm_stop_rx, |
| 1533 | .enable_ms = msm_enable_ms, |
| 1534 | .break_ctl = msm_break_ctl, |
| 1535 | .startup = msm_startup, |
| 1536 | .shutdown = msm_shutdown, |
| 1537 | .set_termios = msm_set_termios, |
| 1538 | .type = msm_type, |
| 1539 | .release_port = msm_release_port, |
| 1540 | .request_port = msm_request_port, |
| 1541 | .config_port = msm_config_port, |
| 1542 | .verify_port = msm_verify_port, |
| 1543 | .pm = msm_power, |
Stephen Boyd | f7e54d7 | 2014-01-14 12:34:55 -0800 | [diff] [blame] | 1544 | #ifdef CONFIG_CONSOLE_POLL |
Stephen Boyd | f7e54d7 | 2014-01-14 12:34:55 -0800 | [diff] [blame] | 1545 | .poll_get_char = msm_poll_get_char, |
| 1546 | .poll_put_char = msm_poll_put_char, |
| 1547 | #endif |
Robert Love | 04896a7 | 2009-06-22 18:43:11 +0100 | [diff] [blame] | 1548 | }; |
| 1549 | |
| 1550 | static struct msm_port msm_uart_ports[] = { |
| 1551 | { |
| 1552 | .uart = { |
| 1553 | .iotype = UPIO_MEM, |
| 1554 | .ops = &msm_uart_pops, |
| 1555 | .flags = UPF_BOOT_AUTOCONF, |
Stepan Moskovchenko | ec8f29e | 2010-12-21 12:38:05 -0800 | [diff] [blame] | 1556 | .fifosize = 64, |
Robert Love | 04896a7 | 2009-06-22 18:43:11 +0100 | [diff] [blame] | 1557 | .line = 0, |
| 1558 | }, |
| 1559 | }, |
| 1560 | { |
| 1561 | .uart = { |
| 1562 | .iotype = UPIO_MEM, |
| 1563 | .ops = &msm_uart_pops, |
| 1564 | .flags = UPF_BOOT_AUTOCONF, |
Stepan Moskovchenko | ec8f29e | 2010-12-21 12:38:05 -0800 | [diff] [blame] | 1565 | .fifosize = 64, |
Robert Love | 04896a7 | 2009-06-22 18:43:11 +0100 | [diff] [blame] | 1566 | .line = 1, |
| 1567 | }, |
| 1568 | }, |
| 1569 | { |
| 1570 | .uart = { |
| 1571 | .iotype = UPIO_MEM, |
| 1572 | .ops = &msm_uart_pops, |
| 1573 | .flags = UPF_BOOT_AUTOCONF, |
| 1574 | .fifosize = 64, |
| 1575 | .line = 2, |
| 1576 | }, |
| 1577 | }, |
| 1578 | }; |
| 1579 | |
| 1580 | #define UART_NR ARRAY_SIZE(msm_uart_ports) |
| 1581 | |
Ivan T. Ivanov | 558abdb | 2015-09-30 15:27:00 +0300 | [diff] [blame] | 1582 | static inline struct uart_port *msm_get_port_from_line(unsigned int line) |
Robert Love | 04896a7 | 2009-06-22 18:43:11 +0100 | [diff] [blame] | 1583 | { |
| 1584 | return &msm_uart_ports[line].uart; |
| 1585 | } |
| 1586 | |
| 1587 | #ifdef CONFIG_SERIAL_MSM_CONSOLE |
Stephen Boyd | 0efe729 | 2014-09-15 17:22:51 -0700 | [diff] [blame] | 1588 | static void __msm_console_write(struct uart_port *port, const char *s, |
| 1589 | unsigned int count, bool is_uartdm) |
Robert Love | 04896a7 | 2009-06-22 18:43:11 +0100 | [diff] [blame] | 1590 | { |
Stephen Boyd | a3957e8 | 2013-08-20 23:48:06 -0700 | [diff] [blame] | 1591 | int i; |
Stephen Boyd | a3957e8 | 2013-08-20 23:48:06 -0700 | [diff] [blame] | 1592 | int num_newlines = 0; |
| 1593 | bool replaced = false; |
Stephen Boyd | 6825242 | 2014-06-30 14:54:01 -0700 | [diff] [blame] | 1594 | void __iomem *tf; |
Leo Yan | 0e4f7f9 | 2019-11-27 22:15:43 +0800 | [diff] [blame] | 1595 | int locked = 1; |
Robert Love | 04896a7 | 2009-06-22 18:43:11 +0100 | [diff] [blame] | 1596 | |
Stephen Boyd | 0efe729 | 2014-09-15 17:22:51 -0700 | [diff] [blame] | 1597 | if (is_uartdm) |
Stephen Boyd | 6825242 | 2014-06-30 14:54:01 -0700 | [diff] [blame] | 1598 | tf = port->membase + UARTDM_TF; |
| 1599 | else |
| 1600 | tf = port->membase + UART_TF; |
| 1601 | |
Stephen Boyd | a3957e8 | 2013-08-20 23:48:06 -0700 | [diff] [blame] | 1602 | /* Account for newlines that will get a carriage return added */ |
| 1603 | for (i = 0; i < count; i++) |
| 1604 | if (s[i] == '\n') |
| 1605 | num_newlines++; |
| 1606 | count += num_newlines; |
| 1607 | |
Leo Yan | 0e4f7f9 | 2019-11-27 22:15:43 +0800 | [diff] [blame] | 1608 | if (port->sysrq) |
| 1609 | locked = 0; |
| 1610 | else if (oops_in_progress) |
| 1611 | locked = spin_trylock(&port->lock); |
| 1612 | else |
| 1613 | spin_lock(&port->lock); |
| 1614 | |
Stephen Boyd | 0efe729 | 2014-09-15 17:22:51 -0700 | [diff] [blame] | 1615 | if (is_uartdm) |
Ivan T. Ivanov | 558abdb | 2015-09-30 15:27:00 +0300 | [diff] [blame] | 1616 | msm_reset_dm_count(port, count); |
Stephen Boyd | a3957e8 | 2013-08-20 23:48:06 -0700 | [diff] [blame] | 1617 | |
| 1618 | i = 0; |
| 1619 | while (i < count) { |
| 1620 | int j; |
| 1621 | unsigned int num_chars; |
| 1622 | char buf[4] = { 0 }; |
Stephen Boyd | a3957e8 | 2013-08-20 23:48:06 -0700 | [diff] [blame] | 1623 | |
Stephen Boyd | 0efe729 | 2014-09-15 17:22:51 -0700 | [diff] [blame] | 1624 | if (is_uartdm) |
Stephen Boyd | a3957e8 | 2013-08-20 23:48:06 -0700 | [diff] [blame] | 1625 | num_chars = min(count - i, (unsigned int)sizeof(buf)); |
| 1626 | else |
| 1627 | num_chars = 1; |
| 1628 | |
| 1629 | for (j = 0; j < num_chars; j++) { |
| 1630 | char c = *s; |
| 1631 | |
| 1632 | if (c == '\n' && !replaced) { |
| 1633 | buf[j] = '\r'; |
| 1634 | j++; |
| 1635 | replaced = true; |
| 1636 | } |
| 1637 | if (j < num_chars) { |
| 1638 | buf[j] = c; |
| 1639 | s++; |
| 1640 | replaced = false; |
| 1641 | } |
| 1642 | } |
| 1643 | |
| 1644 | while (!(msm_read(port, UART_SR) & UART_SR_TX_READY)) |
| 1645 | cpu_relax(); |
| 1646 | |
Stephen Boyd | 6825242 | 2014-06-30 14:54:01 -0700 | [diff] [blame] | 1647 | iowrite32_rep(tf, buf, 1); |
Stephen Boyd | a3957e8 | 2013-08-20 23:48:06 -0700 | [diff] [blame] | 1648 | i += num_chars; |
| 1649 | } |
Leo Yan | 0e4f7f9 | 2019-11-27 22:15:43 +0800 | [diff] [blame] | 1650 | |
| 1651 | if (locked) |
| 1652 | spin_unlock(&port->lock); |
Robert Love | 04896a7 | 2009-06-22 18:43:11 +0100 | [diff] [blame] | 1653 | } |
| 1654 | |
Stephen Boyd | 0efe729 | 2014-09-15 17:22:51 -0700 | [diff] [blame] | 1655 | static void msm_console_write(struct console *co, const char *s, |
| 1656 | unsigned int count) |
| 1657 | { |
| 1658 | struct uart_port *port; |
| 1659 | struct msm_port *msm_port; |
| 1660 | |
| 1661 | BUG_ON(co->index < 0 || co->index >= UART_NR); |
| 1662 | |
Ivan T. Ivanov | 558abdb | 2015-09-30 15:27:00 +0300 | [diff] [blame] | 1663 | port = msm_get_port_from_line(co->index); |
Stephen Boyd | 0efe729 | 2014-09-15 17:22:51 -0700 | [diff] [blame] | 1664 | msm_port = UART_TO_MSM(port); |
| 1665 | |
| 1666 | __msm_console_write(port, s, count, msm_port->is_uartdm); |
| 1667 | } |
| 1668 | |
Jeffrey Hugo | be24c27 | 2019-02-16 22:05:52 -0700 | [diff] [blame] | 1669 | static int msm_console_setup(struct console *co, char *options) |
Robert Love | 04896a7 | 2009-06-22 18:43:11 +0100 | [diff] [blame] | 1670 | { |
| 1671 | struct uart_port *port; |
Pramod Gurav | 4daba33 | 2015-01-12 19:15:32 +0530 | [diff] [blame] | 1672 | int baud = 115200; |
| 1673 | int bits = 8; |
| 1674 | int parity = 'n'; |
| 1675 | int flow = 'n'; |
Robert Love | 04896a7 | 2009-06-22 18:43:11 +0100 | [diff] [blame] | 1676 | |
| 1677 | if (unlikely(co->index >= UART_NR || co->index < 0)) |
| 1678 | return -ENXIO; |
| 1679 | |
Ivan T. Ivanov | 558abdb | 2015-09-30 15:27:00 +0300 | [diff] [blame] | 1680 | port = msm_get_port_from_line(co->index); |
Robert Love | 04896a7 | 2009-06-22 18:43:11 +0100 | [diff] [blame] | 1681 | |
| 1682 | if (unlikely(!port->membase)) |
| 1683 | return -ENXIO; |
| 1684 | |
Robert Love | 04896a7 | 2009-06-22 18:43:11 +0100 | [diff] [blame] | 1685 | msm_init_clock(port); |
| 1686 | |
| 1687 | if (options) |
| 1688 | uart_parse_options(options, &baud, &parity, &bits, &flow); |
| 1689 | |
Kiran Padwal | 6a7cfe4 | 2014-08-05 13:22:01 +0530 | [diff] [blame] | 1690 | pr_info("msm_serial: console setup on port #%d\n", port->line); |
Robert Love | 04896a7 | 2009-06-22 18:43:11 +0100 | [diff] [blame] | 1691 | |
| 1692 | return uart_set_options(port, co, baud, parity, bits, flow); |
| 1693 | } |
| 1694 | |
Stephen Boyd | 0efe729 | 2014-09-15 17:22:51 -0700 | [diff] [blame] | 1695 | static void |
| 1696 | msm_serial_early_write(struct console *con, const char *s, unsigned n) |
| 1697 | { |
| 1698 | struct earlycon_device *dev = con->data; |
| 1699 | |
| 1700 | __msm_console_write(&dev->port, s, n, false); |
| 1701 | } |
| 1702 | |
| 1703 | static int __init |
| 1704 | msm_serial_early_console_setup(struct earlycon_device *device, const char *opt) |
| 1705 | { |
| 1706 | if (!device->port.membase) |
| 1707 | return -ENODEV; |
| 1708 | |
| 1709 | device->con->write = msm_serial_early_write; |
| 1710 | return 0; |
| 1711 | } |
Stephen Boyd | 0efe729 | 2014-09-15 17:22:51 -0700 | [diff] [blame] | 1712 | OF_EARLYCON_DECLARE(msm_serial, "qcom,msm-uart", |
| 1713 | msm_serial_early_console_setup); |
| 1714 | |
| 1715 | static void |
| 1716 | msm_serial_early_write_dm(struct console *con, const char *s, unsigned n) |
| 1717 | { |
| 1718 | struct earlycon_device *dev = con->data; |
| 1719 | |
| 1720 | __msm_console_write(&dev->port, s, n, true); |
| 1721 | } |
| 1722 | |
| 1723 | static int __init |
| 1724 | msm_serial_early_console_setup_dm(struct earlycon_device *device, |
| 1725 | const char *opt) |
| 1726 | { |
| 1727 | if (!device->port.membase) |
| 1728 | return -ENODEV; |
| 1729 | |
| 1730 | device->con->write = msm_serial_early_write_dm; |
| 1731 | return 0; |
| 1732 | } |
Stephen Boyd | 0efe729 | 2014-09-15 17:22:51 -0700 | [diff] [blame] | 1733 | OF_EARLYCON_DECLARE(msm_serial_dm, "qcom,msm-uartdm", |
| 1734 | msm_serial_early_console_setup_dm); |
| 1735 | |
Robert Love | 04896a7 | 2009-06-22 18:43:11 +0100 | [diff] [blame] | 1736 | static struct uart_driver msm_uart_driver; |
| 1737 | |
| 1738 | static struct console msm_console = { |
| 1739 | .name = "ttyMSM", |
| 1740 | .write = msm_console_write, |
| 1741 | .device = uart_console_device, |
| 1742 | .setup = msm_console_setup, |
| 1743 | .flags = CON_PRINTBUFFER, |
| 1744 | .index = -1, |
| 1745 | .data = &msm_uart_driver, |
| 1746 | }; |
| 1747 | |
| 1748 | #define MSM_CONSOLE (&msm_console) |
| 1749 | |
| 1750 | #else |
| 1751 | #define MSM_CONSOLE NULL |
| 1752 | #endif |
| 1753 | |
| 1754 | static struct uart_driver msm_uart_driver = { |
| 1755 | .owner = THIS_MODULE, |
| 1756 | .driver_name = "msm_serial", |
| 1757 | .dev_name = "ttyMSM", |
| 1758 | .nr = UART_NR, |
| 1759 | .cons = MSM_CONSOLE, |
| 1760 | }; |
| 1761 | |
David Brown | cfdad2a | 2011-08-04 01:55:24 -0700 | [diff] [blame] | 1762 | static atomic_t msm_uart_next_id = ATOMIC_INIT(0); |
| 1763 | |
Stephen Boyd | c3b5d3b | 2013-08-20 23:48:04 -0700 | [diff] [blame] | 1764 | static const struct of_device_id msm_uartdm_table[] = { |
Stephen Boyd | f7e54d7 | 2014-01-14 12:34:55 -0800 | [diff] [blame] | 1765 | { .compatible = "qcom,msm-uartdm-v1.1", .data = (void *)UARTDM_1P1 }, |
| 1766 | { .compatible = "qcom,msm-uartdm-v1.2", .data = (void *)UARTDM_1P2 }, |
| 1767 | { .compatible = "qcom,msm-uartdm-v1.3", .data = (void *)UARTDM_1P3 }, |
| 1768 | { .compatible = "qcom,msm-uartdm-v1.4", .data = (void *)UARTDM_1P4 }, |
Stephen Boyd | c3b5d3b | 2013-08-20 23:48:04 -0700 | [diff] [blame] | 1769 | { } |
| 1770 | }; |
| 1771 | |
Kumar Gala | 4cc2946 | 2014-06-03 15:13:22 -0500 | [diff] [blame] | 1772 | static int msm_serial_probe(struct platform_device *pdev) |
Robert Love | 04896a7 | 2009-06-22 18:43:11 +0100 | [diff] [blame] | 1773 | { |
| 1774 | struct msm_port *msm_port; |
| 1775 | struct resource *resource; |
| 1776 | struct uart_port *port; |
Stephen Boyd | f7e54d7 | 2014-01-14 12:34:55 -0800 | [diff] [blame] | 1777 | const struct of_device_id *id; |
Stephen Boyd | 97f7547 | 2014-10-22 17:33:01 -0700 | [diff] [blame] | 1778 | int irq, line; |
Robert Love | 04896a7 | 2009-06-22 18:43:11 +0100 | [diff] [blame] | 1779 | |
Stephen Boyd | 97f7547 | 2014-10-22 17:33:01 -0700 | [diff] [blame] | 1780 | if (pdev->dev.of_node) |
| 1781 | line = of_alias_get_id(pdev->dev.of_node, "serial"); |
| 1782 | else |
| 1783 | line = pdev->id; |
| 1784 | |
Stephen Boyd | 7920408 | 2014-11-14 10:39:21 -0800 | [diff] [blame] | 1785 | if (line < 0) |
| 1786 | line = atomic_inc_return(&msm_uart_next_id) - 1; |
| 1787 | |
Stephen Boyd | 97f7547 | 2014-10-22 17:33:01 -0700 | [diff] [blame] | 1788 | if (unlikely(line < 0 || line >= UART_NR)) |
Robert Love | 04896a7 | 2009-06-22 18:43:11 +0100 | [diff] [blame] | 1789 | return -ENXIO; |
| 1790 | |
Stephen Boyd | 97f7547 | 2014-10-22 17:33:01 -0700 | [diff] [blame] | 1791 | dev_info(&pdev->dev, "msm_serial: detected port #%d\n", line); |
Robert Love | 04896a7 | 2009-06-22 18:43:11 +0100 | [diff] [blame] | 1792 | |
Ivan T. Ivanov | 558abdb | 2015-09-30 15:27:00 +0300 | [diff] [blame] | 1793 | port = msm_get_port_from_line(line); |
Robert Love | 04896a7 | 2009-06-22 18:43:11 +0100 | [diff] [blame] | 1794 | port->dev = &pdev->dev; |
| 1795 | msm_port = UART_TO_MSM(port); |
| 1796 | |
Stephen Boyd | f7e54d7 | 2014-01-14 12:34:55 -0800 | [diff] [blame] | 1797 | id = of_match_device(msm_uartdm_table, &pdev->dev); |
| 1798 | if (id) |
| 1799 | msm_port->is_uartdm = (unsigned long)id->data; |
Stepan Moskovchenko | ec8f29e | 2010-12-21 12:38:05 -0800 | [diff] [blame] | 1800 | else |
| 1801 | msm_port->is_uartdm = 0; |
| 1802 | |
Stephen Boyd | bfaddb7 | 2013-08-20 23:48:02 -0700 | [diff] [blame] | 1803 | msm_port->clk = devm_clk_get(&pdev->dev, "core"); |
Stephen Boyd | 519b371 | 2013-06-17 10:43:09 -0700 | [diff] [blame] | 1804 | if (IS_ERR(msm_port->clk)) |
| 1805 | return PTR_ERR(msm_port->clk); |
Stepan Moskovchenko | ec8f29e | 2010-12-21 12:38:05 -0800 | [diff] [blame] | 1806 | |
Stephen Boyd | 519b371 | 2013-06-17 10:43:09 -0700 | [diff] [blame] | 1807 | if (msm_port->is_uartdm) { |
Stephen Boyd | bfaddb7 | 2013-08-20 23:48:02 -0700 | [diff] [blame] | 1808 | msm_port->pclk = devm_clk_get(&pdev->dev, "iface"); |
Stephen Boyd | 519b371 | 2013-06-17 10:43:09 -0700 | [diff] [blame] | 1809 | if (IS_ERR(msm_port->pclk)) |
| 1810 | return PTR_ERR(msm_port->pclk); |
Stephen Boyd | 519b371 | 2013-06-17 10:43:09 -0700 | [diff] [blame] | 1811 | } |
Stepan Moskovchenko | ec8f29e | 2010-12-21 12:38:05 -0800 | [diff] [blame] | 1812 | |
Robert Love | 04896a7 | 2009-06-22 18:43:11 +0100 | [diff] [blame] | 1813 | port->uartclk = clk_get_rate(msm_port->clk); |
Kiran Padwal | 6a7cfe4 | 2014-08-05 13:22:01 +0530 | [diff] [blame] | 1814 | dev_info(&pdev->dev, "uartclk = %d\n", port->uartclk); |
Robert Love | 04896a7 | 2009-06-22 18:43:11 +0100 | [diff] [blame] | 1815 | |
David Brown | 886a451 | 2011-08-02 09:02:49 -0700 | [diff] [blame] | 1816 | resource = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
Robert Love | 04896a7 | 2009-06-22 18:43:11 +0100 | [diff] [blame] | 1817 | if (unlikely(!resource)) |
| 1818 | return -ENXIO; |
| 1819 | port->mapbase = resource->start; |
| 1820 | |
Roel Kluin | 1e09175 | 2009-12-21 16:26:49 -0800 | [diff] [blame] | 1821 | irq = platform_get_irq(pdev, 0); |
| 1822 | if (unlikely(irq < 0)) |
Robert Love | 04896a7 | 2009-06-22 18:43:11 +0100 | [diff] [blame] | 1823 | return -ENXIO; |
Roel Kluin | 1e09175 | 2009-12-21 16:26:49 -0800 | [diff] [blame] | 1824 | port->irq = irq; |
Dmitry Safonov | 804ca1d | 2019-12-13 00:06:24 +0000 | [diff] [blame] | 1825 | port->has_sysrq = IS_ENABLED(CONFIG_SERIAL_MSM_CONSOLE); |
Robert Love | 04896a7 | 2009-06-22 18:43:11 +0100 | [diff] [blame] | 1826 | |
| 1827 | platform_set_drvdata(pdev, port); |
| 1828 | |
| 1829 | return uart_add_one_port(&msm_uart_driver, port); |
| 1830 | } |
| 1831 | |
Bill Pemberton | ae8d8a1 | 2012-11-19 13:26:18 -0500 | [diff] [blame] | 1832 | static int msm_serial_remove(struct platform_device *pdev) |
Robert Love | 04896a7 | 2009-06-22 18:43:11 +0100 | [diff] [blame] | 1833 | { |
Stephen Boyd | 519b371 | 2013-06-17 10:43:09 -0700 | [diff] [blame] | 1834 | struct uart_port *port = platform_get_drvdata(pdev); |
Robert Love | 04896a7 | 2009-06-22 18:43:11 +0100 | [diff] [blame] | 1835 | |
Stephen Boyd | 519b371 | 2013-06-17 10:43:09 -0700 | [diff] [blame] | 1836 | uart_remove_one_port(&msm_uart_driver, port); |
Robert Love | 04896a7 | 2009-06-22 18:43:11 +0100 | [diff] [blame] | 1837 | |
| 1838 | return 0; |
| 1839 | } |
| 1840 | |
Kiran Padwal | af30053 | 2014-07-23 15:56:26 +0530 | [diff] [blame] | 1841 | static const struct of_device_id msm_match_table[] = { |
David Brown | cfdad2a | 2011-08-04 01:55:24 -0700 | [diff] [blame] | 1842 | { .compatible = "qcom,msm-uart" }, |
Stephen Boyd | c3b5d3b | 2013-08-20 23:48:04 -0700 | [diff] [blame] | 1843 | { .compatible = "qcom,msm-uartdm" }, |
David Brown | cfdad2a | 2011-08-04 01:55:24 -0700 | [diff] [blame] | 1844 | {} |
| 1845 | }; |
Javier Martinez Canillas | abe81f3 | 2017-01-02 11:57:20 -0300 | [diff] [blame] | 1846 | MODULE_DEVICE_TABLE(of, msm_match_table); |
David Brown | cfdad2a | 2011-08-04 01:55:24 -0700 | [diff] [blame] | 1847 | |
Pascal Huerst | 2ccdb55 | 2018-04-25 17:22:07 +0200 | [diff] [blame] | 1848 | static int __maybe_unused msm_serial_suspend(struct device *dev) |
Pascal Huerst | 7a91e38 | 2018-04-11 13:50:45 +0200 | [diff] [blame] | 1849 | { |
| 1850 | struct msm_port *port = dev_get_drvdata(dev); |
| 1851 | |
| 1852 | uart_suspend_port(&msm_uart_driver, &port->uart); |
| 1853 | |
| 1854 | return 0; |
| 1855 | } |
| 1856 | |
Pascal Huerst | 2ccdb55 | 2018-04-25 17:22:07 +0200 | [diff] [blame] | 1857 | static int __maybe_unused msm_serial_resume(struct device *dev) |
Pascal Huerst | 7a91e38 | 2018-04-11 13:50:45 +0200 | [diff] [blame] | 1858 | { |
| 1859 | struct msm_port *port = dev_get_drvdata(dev); |
| 1860 | |
| 1861 | uart_resume_port(&msm_uart_driver, &port->uart); |
| 1862 | |
| 1863 | return 0; |
| 1864 | } |
Pascal Huerst | 7a91e38 | 2018-04-11 13:50:45 +0200 | [diff] [blame] | 1865 | |
| 1866 | static const struct dev_pm_ops msm_serial_dev_pm_ops = { |
| 1867 | SET_SYSTEM_SLEEP_PM_OPS(msm_serial_suspend, msm_serial_resume) |
| 1868 | }; |
| 1869 | |
Robert Love | 04896a7 | 2009-06-22 18:43:11 +0100 | [diff] [blame] | 1870 | static struct platform_driver msm_platform_driver = { |
Robert Love | 04896a7 | 2009-06-22 18:43:11 +0100 | [diff] [blame] | 1871 | .remove = msm_serial_remove, |
Andy Gross | 31964ff | 2014-04-24 11:31:22 -0500 | [diff] [blame] | 1872 | .probe = msm_serial_probe, |
Robert Love | 04896a7 | 2009-06-22 18:43:11 +0100 | [diff] [blame] | 1873 | .driver = { |
| 1874 | .name = "msm_serial", |
Pascal Huerst | 7a91e38 | 2018-04-11 13:50:45 +0200 | [diff] [blame] | 1875 | .pm = &msm_serial_dev_pm_ops, |
David Brown | cfdad2a | 2011-08-04 01:55:24 -0700 | [diff] [blame] | 1876 | .of_match_table = msm_match_table, |
Robert Love | 04896a7 | 2009-06-22 18:43:11 +0100 | [diff] [blame] | 1877 | }, |
| 1878 | }; |
| 1879 | |
| 1880 | static int __init msm_serial_init(void) |
| 1881 | { |
| 1882 | int ret; |
| 1883 | |
| 1884 | ret = uart_register_driver(&msm_uart_driver); |
| 1885 | if (unlikely(ret)) |
| 1886 | return ret; |
| 1887 | |
Andy Gross | 31964ff | 2014-04-24 11:31:22 -0500 | [diff] [blame] | 1888 | ret = platform_driver_register(&msm_platform_driver); |
Robert Love | 04896a7 | 2009-06-22 18:43:11 +0100 | [diff] [blame] | 1889 | if (unlikely(ret)) |
| 1890 | uart_unregister_driver(&msm_uart_driver); |
| 1891 | |
Kiran Padwal | 6a7cfe4 | 2014-08-05 13:22:01 +0530 | [diff] [blame] | 1892 | pr_info("msm_serial: driver initialized\n"); |
Robert Love | 04896a7 | 2009-06-22 18:43:11 +0100 | [diff] [blame] | 1893 | |
| 1894 | return ret; |
| 1895 | } |
| 1896 | |
| 1897 | static void __exit msm_serial_exit(void) |
| 1898 | { |
Robert Love | 04896a7 | 2009-06-22 18:43:11 +0100 | [diff] [blame] | 1899 | platform_driver_unregister(&msm_platform_driver); |
| 1900 | uart_unregister_driver(&msm_uart_driver); |
| 1901 | } |
| 1902 | |
| 1903 | module_init(msm_serial_init); |
| 1904 | module_exit(msm_serial_exit); |
| 1905 | |
| 1906 | MODULE_AUTHOR("Robert Love <rlove@google.com>"); |
| 1907 | MODULE_DESCRIPTION("Driver for msm7x serial device"); |
| 1908 | MODULE_LICENSE("GPL"); |