Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017, The Linux foundation. All rights reserved. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License version 2 and |
| 6 | * only version 2 as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | */ |
| 13 | |
| 14 | #include <linux/bitmap.h> |
| 15 | #include <linux/bitops.h> |
| 16 | #include <linux/debugfs.h> |
| 17 | #include <linux/delay.h> |
| 18 | #include <linux/console.h> |
| 19 | #include <linux/io.h> |
Girish Mahadevan | 7115f4e | 2017-03-15 15:18:34 -0600 | [diff] [blame] | 20 | #include <linux/ipc_logging.h> |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 21 | #include <linux/module.h> |
| 22 | #include <linux/of.h> |
| 23 | #include <linux/of_device.h> |
| 24 | #include <linux/platform_device.h> |
Karthikeyan Ramasubramanian | 9d88c72 | 2017-04-06 16:04:39 -0600 | [diff] [blame] | 25 | #include <linux/pm_runtime.h> |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 26 | #include <linux/qcom-geni-se.h> |
| 27 | #include <linux/serial.h> |
| 28 | #include <linux/serial_core.h> |
Karthikeyan Ramasubramanian | 8ec770cf | 2017-04-20 15:29:09 -0600 | [diff] [blame] | 29 | #include <linux/slab.h> |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 30 | #include <linux/tty.h> |
| 31 | #include <linux/tty_flip.h> |
| 32 | |
| 33 | /* UART specific GENI registers */ |
| 34 | #define SE_UART_LOOPBACK_CFG (0x22C) |
| 35 | #define SE_UART_TX_TRANS_CFG (0x25C) |
| 36 | #define SE_UART_TX_WORD_LEN (0x268) |
| 37 | #define SE_UART_TX_STOP_BIT_LEN (0x26C) |
| 38 | #define SE_UART_TX_TRANS_LEN (0x270) |
| 39 | #define SE_UART_RX_TRANS_CFG (0x280) |
| 40 | #define SE_UART_RX_WORD_LEN (0x28C) |
| 41 | #define SE_UART_RX_STALE_CNT (0x294) |
| 42 | #define SE_UART_TX_PARITY_CFG (0x2A4) |
| 43 | #define SE_UART_RX_PARITY_CFG (0x2A8) |
Girish Mahadevan | 7115f4e | 2017-03-15 15:18:34 -0600 | [diff] [blame] | 44 | #define SE_UART_MANUAL_RFR (0x2AC) |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 45 | |
| 46 | /* SE_UART_LOOPBACK_CFG */ |
| 47 | #define NO_LOOPBACK (0) |
| 48 | #define TX_RX_LOOPBACK (0x1) |
| 49 | #define CTS_RFR_LOOPBACK (0x2) |
| 50 | #define CTSRFR_TXRX_LOOPBACK (0x3) |
| 51 | |
| 52 | /* SE_UART_TRANS_CFG */ |
| 53 | #define UART_TX_PAR_EN (BIT(0)) |
| 54 | #define UART_CTS_MASK (BIT(1)) |
| 55 | |
| 56 | /* SE_UART_TX_WORD_LEN */ |
| 57 | #define TX_WORD_LEN_MSK (GENMASK(9, 0)) |
| 58 | |
| 59 | /* SE_UART_TX_STOP_BIT_LEN */ |
| 60 | #define TX_STOP_BIT_LEN_MSK (GENMASK(23, 0)) |
| 61 | #define TX_STOP_BIT_LEN_1 (0) |
| 62 | #define TX_STOP_BIT_LEN_1_5 (1) |
| 63 | #define TX_STOP_BIT_LEN_2 (2) |
| 64 | |
| 65 | /* SE_UART_TX_TRANS_LEN */ |
| 66 | #define TX_TRANS_LEN_MSK (GENMASK(23, 0)) |
| 67 | |
| 68 | /* SE_UART_RX_TRANS_CFG */ |
| 69 | #define UART_RX_INS_STATUS_BIT (BIT(2)) |
| 70 | #define UART_RX_PAR_EN (BIT(3)) |
| 71 | |
| 72 | /* SE_UART_RX_WORD_LEN */ |
| 73 | #define RX_WORD_LEN_MASK (GENMASK(9, 0)) |
| 74 | |
| 75 | /* SE_UART_RX_STALE_CNT */ |
| 76 | #define RX_STALE_CNT (GENMASK(23, 0)) |
| 77 | |
| 78 | /* SE_UART_TX_PARITY_CFG/RX_PARITY_CFG */ |
| 79 | #define PAR_CALC_EN (BIT(0)) |
| 80 | #define PAR_MODE_MSK (GENMASK(2, 1)) |
| 81 | #define PAR_MODE_SHFT (1) |
| 82 | #define PAR_EVEN (0x00) |
| 83 | #define PAR_ODD (0x01) |
| 84 | #define PAR_SPACE (0x10) |
| 85 | #define PAR_MARK (0x11) |
| 86 | |
Girish Mahadevan | 7115f4e | 2017-03-15 15:18:34 -0600 | [diff] [blame] | 87 | /* SE_UART_MANUAL_RFR register fields */ |
| 88 | #define UART_MANUAL_RFR_EN (BIT(31)) |
| 89 | #define UART_RFR_NOT_READY (BIT(1)) |
| 90 | #define UART_RFR_READY (BIT(0)) |
| 91 | |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 92 | /* UART M_CMD OP codes */ |
| 93 | #define UART_START_TX (0x1) |
| 94 | #define UART_START_BREAK (0x4) |
| 95 | #define UART_STOP_BREAK (0x5) |
| 96 | /* UART S_CMD OP codes */ |
| 97 | #define UART_START_READ (0x1) |
| 98 | #define UART_PARAM (0x1) |
| 99 | |
Girish Mahadevan | c2b9252 | 2017-08-17 22:41:32 -0600 | [diff] [blame] | 100 | /* UART DMA Rx GP_IRQ_BITS */ |
Karthikeyan Ramasubramanian | e67fd2e | 2017-08-30 17:16:58 -0600 | [diff] [blame] | 101 | #define UART_DMA_RX_PARITY_ERR BIT(5) |
| 102 | #define UART_DMA_RX_ERRS (GENMASK(5, 6)) |
| 103 | #define UART_DMA_RX_BREAK (GENMASK(7, 8)) |
Girish Mahadevan | c2b9252 | 2017-08-17 22:41:32 -0600 | [diff] [blame] | 104 | |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 105 | #define UART_OVERSAMPLING (32) |
| 106 | #define STALE_TIMEOUT (16) |
Girish Mahadevan | b1ab172 | 2017-04-27 16:39:11 -0600 | [diff] [blame] | 107 | #define DEFAULT_BITS_PER_CHAR (10) |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 108 | #define GENI_UART_NR_PORTS (15) |
Girish Mahadevan | 7115f4e | 2017-03-15 15:18:34 -0600 | [diff] [blame] | 109 | #define GENI_UART_CONS_PORTS (1) |
Girish Mahadevan | bd9b44f | 2017-04-11 13:11:10 -0600 | [diff] [blame] | 110 | #define DEF_FIFO_DEPTH_WORDS (16) |
Girish Mahadevan | b1ab172 | 2017-04-27 16:39:11 -0600 | [diff] [blame] | 111 | #define DEF_TX_WM (2) |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 112 | #define DEF_FIFO_WIDTH_BITS (32) |
Girish Mahadevan | 3e694cc | 2017-04-19 16:50:03 -0600 | [diff] [blame] | 113 | #define UART_CORE2X_VOTE (10000) |
Girish Mahadevan | 3e694cc | 2017-04-19 16:50:03 -0600 | [diff] [blame] | 114 | |
Girish Mahadevan | 7115f4e | 2017-03-15 15:18:34 -0600 | [diff] [blame] | 115 | #define WAKEBYTE_TIMEOUT_MSEC (2000) |
Girish Mahadevan | 736892d | 2017-07-14 15:20:58 -0600 | [diff] [blame] | 116 | #define WAIT_XFER_MAX_ITER (50) |
| 117 | #define WAIT_XFER_MAX_TIMEOUT_US (10000) |
| 118 | #define WAIT_XFER_MIN_TIMEOUT_US (9000) |
| 119 | #define IPC_LOG_PWR_PAGES (6) |
Girish Mahadevan | 51fc9d4 | 2017-10-12 18:37:52 -0600 | [diff] [blame] | 120 | #define IPC_LOG_MISC_PAGES (10) |
Girish Mahadevan | 736892d | 2017-07-14 15:20:58 -0600 | [diff] [blame] | 121 | #define IPC_LOG_TX_RX_PAGES (8) |
Girish Mahadevan | 7115f4e | 2017-03-15 15:18:34 -0600 | [diff] [blame] | 122 | #define DATA_BYTES_PER_LINE (32) |
| 123 | |
| 124 | #define IPC_LOG_MSG(ctx, x...) do { \ |
| 125 | if (ctx) \ |
| 126 | ipc_log_string(ctx, x); \ |
| 127 | } while (0) |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 128 | |
Karthikeyan Ramasubramanian | b6be8c2 | 2017-08-23 16:34:00 -0600 | [diff] [blame] | 129 | #define DMA_RX_BUF_SIZE (2048) |
Karthikeyan Ramasubramanian | 55a1c6d | 2017-11-14 13:10:46 -0700 | [diff] [blame] | 130 | #define UART_CONSOLE_RX_WM (2) |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 131 | struct msm_geni_serial_port { |
| 132 | struct uart_port uport; |
| 133 | char name[20]; |
| 134 | unsigned int tx_fifo_depth; |
| 135 | unsigned int tx_fifo_width; |
| 136 | unsigned int rx_fifo_depth; |
| 137 | unsigned int tx_wm; |
| 138 | unsigned int rx_wm; |
| 139 | unsigned int rx_rfr; |
| 140 | int xfer_mode; |
| 141 | struct dentry *dbg; |
| 142 | bool port_setup; |
| 143 | unsigned int *rx_fifo; |
| 144 | int (*handle_rx)(struct uart_port *uport, |
| 145 | unsigned int rx_fifo_wc, |
| 146 | unsigned int rx_last_byte_valid, |
Karthikeyan Ramasubramanian | e67fd2e | 2017-08-30 17:16:58 -0600 | [diff] [blame] | 147 | unsigned int rx_last, |
| 148 | bool drop_rx); |
Karthikeyan Ramasubramanian | 0d578b7 | 2017-04-26 10:44:02 -0600 | [diff] [blame] | 149 | struct device *wrapper_dev; |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 150 | struct se_geni_rsc serial_rsc; |
Karthikeyan Ramasubramanian | 8ec770cf | 2017-04-20 15:29:09 -0600 | [diff] [blame] | 151 | dma_addr_t tx_dma; |
| 152 | unsigned int xmit_size; |
| 153 | void *rx_buf; |
| 154 | dma_addr_t rx_dma; |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 155 | int loopback; |
Girish Mahadevan | 7115f4e | 2017-03-15 15:18:34 -0600 | [diff] [blame] | 156 | int wakeup_irq; |
| 157 | unsigned char wakeup_byte; |
| 158 | struct wakeup_source geni_wake; |
| 159 | void *ipc_log_tx; |
| 160 | void *ipc_log_rx; |
| 161 | void *ipc_log_pwr; |
| 162 | void *ipc_log_misc; |
| 163 | unsigned int cur_baud; |
Girish Mahadevan | a4ed038 | 2017-05-12 11:25:30 -0600 | [diff] [blame] | 164 | int ioctl_count; |
Girish Mahadevan | 736892d | 2017-07-14 15:20:58 -0600 | [diff] [blame] | 165 | int edge_count; |
Girish Mahadevan | 6d97cbc | 2017-11-07 15:28:27 -0700 | [diff] [blame] | 166 | bool manual_flow; |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 167 | }; |
| 168 | |
| 169 | static const struct uart_ops msm_geni_serial_pops; |
| 170 | static struct uart_driver msm_geni_console_driver; |
| 171 | static struct uart_driver msm_geni_serial_hs_driver; |
| 172 | static int handle_rx_console(struct uart_port *uport, |
| 173 | unsigned int rx_fifo_wc, |
| 174 | unsigned int rx_last_byte_valid, |
Karthikeyan Ramasubramanian | e67fd2e | 2017-08-30 17:16:58 -0600 | [diff] [blame] | 175 | unsigned int rx_last, |
| 176 | bool drop_rx); |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 177 | static int handle_rx_hs(struct uart_port *uport, |
| 178 | unsigned int rx_fifo_wc, |
| 179 | unsigned int rx_last_byte_valid, |
Karthikeyan Ramasubramanian | e67fd2e | 2017-08-30 17:16:58 -0600 | [diff] [blame] | 180 | unsigned int rx_last, |
| 181 | bool drop_rx); |
Girish Mahadevan | b1ab172 | 2017-04-27 16:39:11 -0600 | [diff] [blame] | 182 | static unsigned int msm_geni_serial_tx_empty(struct uart_port *port); |
Girish Mahadevan | 7115f4e | 2017-03-15 15:18:34 -0600 | [diff] [blame] | 183 | static int msm_geni_serial_power_on(struct uart_port *uport); |
| 184 | static void msm_geni_serial_power_off(struct uart_port *uport); |
Girish Mahadevan | a4ed038 | 2017-05-12 11:25:30 -0600 | [diff] [blame] | 185 | static int msm_geni_serial_poll_bit(struct uart_port *uport, |
| 186 | int offset, int bit_field, bool set); |
Girish Mahadevan | eecdd97 | 2017-08-22 17:58:08 -0600 | [diff] [blame] | 187 | static void msm_geni_serial_stop_rx(struct uart_port *uport); |
Girish Mahadevan | 5db3df7 | 2017-10-18 11:02:56 -0600 | [diff] [blame] | 188 | static int msm_geni_serial_runtime_resume(struct device *dev); |
| 189 | static int msm_geni_serial_runtime_suspend(struct device *dev); |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 190 | |
| 191 | static atomic_t uart_line_id = ATOMIC_INIT(0); |
| 192 | |
| 193 | #define GET_DEV_PORT(uport) \ |
| 194 | container_of(uport, struct msm_geni_serial_port, uport) |
| 195 | |
Girish Mahadevan | 7115f4e | 2017-03-15 15:18:34 -0600 | [diff] [blame] | 196 | static struct msm_geni_serial_port msm_geni_console_port; |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 197 | static struct msm_geni_serial_port msm_geni_serial_ports[GENI_UART_NR_PORTS]; |
| 198 | |
| 199 | static void msm_geni_serial_config_port(struct uart_port *uport, int cfg_flags) |
| 200 | { |
| 201 | if (cfg_flags & UART_CONFIG_TYPE) |
| 202 | uport->type = PORT_MSM; |
| 203 | } |
| 204 | |
| 205 | static ssize_t msm_geni_serial_loopback_show(struct device *dev, |
| 206 | struct device_attribute *attr, char *buf) |
| 207 | { |
| 208 | struct platform_device *pdev = to_platform_device(dev); |
| 209 | struct msm_geni_serial_port *port = platform_get_drvdata(pdev); |
| 210 | |
| 211 | return snprintf(buf, sizeof(int), "%d\n", port->loopback); |
| 212 | } |
| 213 | |
| 214 | static ssize_t msm_geni_serial_loopback_store(struct device *dev, |
| 215 | struct device_attribute *attr, const char *buf, |
| 216 | size_t size) |
| 217 | { |
| 218 | struct platform_device *pdev = to_platform_device(dev); |
| 219 | struct msm_geni_serial_port *port = platform_get_drvdata(pdev); |
| 220 | |
| 221 | if (kstrtoint(buf, 0, &port->loopback)) { |
| 222 | dev_err(dev, "Invalid input\n"); |
| 223 | return -EINVAL; |
| 224 | } |
| 225 | return size; |
| 226 | } |
| 227 | |
| 228 | static DEVICE_ATTR(loopback, 0644, msm_geni_serial_loopback_show, |
| 229 | msm_geni_serial_loopback_store); |
| 230 | |
Girish Mahadevan | 7115f4e | 2017-03-15 15:18:34 -0600 | [diff] [blame] | 231 | static void dump_ipc(void *ipc_ctx, char *prefix, char *string, |
| 232 | u64 addr, int size) |
| 233 | |
| 234 | { |
| 235 | char buf[DATA_BYTES_PER_LINE * 2]; |
| 236 | int len = 0; |
| 237 | |
| 238 | if (!ipc_ctx) |
| 239 | return; |
| 240 | len = min(size, DATA_BYTES_PER_LINE); |
| 241 | hex_dump_to_buffer(string, len, DATA_BYTES_PER_LINE, 1, buf, |
| 242 | sizeof(buf), false); |
| 243 | ipc_log_string(ipc_ctx, "%s[0x%.10x:%d] : %s", prefix, |
| 244 | (unsigned int)addr, size, buf); |
| 245 | } |
| 246 | |
Girish Mahadevan | 51fc9d4 | 2017-10-12 18:37:52 -0600 | [diff] [blame] | 247 | static bool device_pending_suspend(struct uart_port *uport) |
| 248 | { |
| 249 | int usage_count = atomic_read(&uport->dev->power.usage_count); |
| 250 | |
Girish Mahadevan | 5db3df7 | 2017-10-18 11:02:56 -0600 | [diff] [blame] | 251 | return (pm_runtime_status_suspended(uport->dev) || !usage_count); |
Girish Mahadevan | 51fc9d4 | 2017-10-12 18:37:52 -0600 | [diff] [blame] | 252 | } |
| 253 | |
Girish Mahadevan | 736892d | 2017-07-14 15:20:58 -0600 | [diff] [blame] | 254 | static bool check_transfers_inflight(struct uart_port *uport) |
Girish Mahadevan | 7115f4e | 2017-03-15 15:18:34 -0600 | [diff] [blame] | 255 | { |
Girish Mahadevan | 736892d | 2017-07-14 15:20:58 -0600 | [diff] [blame] | 256 | bool xfer_on = false; |
| 257 | bool tx_active = false; |
Girish Mahadevan | 51fc9d4 | 2017-10-12 18:37:52 -0600 | [diff] [blame] | 258 | bool tx_fifo_status = false; |
Girish Mahadevan | 736892d | 2017-07-14 15:20:58 -0600 | [diff] [blame] | 259 | bool m_cmd_active = false; |
| 260 | bool rx_active = false; |
| 261 | u32 rx_fifo_status = 0; |
Girish Mahadevan | 51fc9d4 | 2017-10-12 18:37:52 -0600 | [diff] [blame] | 262 | struct msm_geni_serial_port *port = GET_DEV_PORT(uport); |
Girish Mahadevan | 736892d | 2017-07-14 15:20:58 -0600 | [diff] [blame] | 263 | u32 geni_status = geni_read_reg_nolog(uport->membase, |
| 264 | SE_GENI_STATUS); |
Girish Mahadevan | 51fc9d4 | 2017-10-12 18:37:52 -0600 | [diff] [blame] | 265 | struct circ_buf *xmit = &uport->state->xmit; |
| 266 | |
Girish Mahadevan | 736892d | 2017-07-14 15:20:58 -0600 | [diff] [blame] | 267 | /* Possible stop tx is called multiple times. */ |
| 268 | m_cmd_active = geni_status & M_GENI_CMD_ACTIVE; |
Girish Mahadevan | 6d97cbc | 2017-11-07 15:28:27 -0700 | [diff] [blame] | 269 | if (port->xfer_mode == SE_DMA) { |
Girish Mahadevan | 51fc9d4 | 2017-10-12 18:37:52 -0600 | [diff] [blame] | 270 | tx_fifo_status = port->tx_dma ? 1 : 0; |
Girish Mahadevan | 6d97cbc | 2017-11-07 15:28:27 -0700 | [diff] [blame] | 271 | rx_fifo_status = |
| 272 | geni_read_reg_nolog(uport->membase, SE_DMA_RX_LEN_IN); |
| 273 | } else { |
Girish Mahadevan | 51fc9d4 | 2017-10-12 18:37:52 -0600 | [diff] [blame] | 274 | tx_fifo_status = geni_read_reg_nolog(uport->membase, |
| 275 | SE_GENI_TX_FIFO_STATUS); |
Girish Mahadevan | 6d97cbc | 2017-11-07 15:28:27 -0700 | [diff] [blame] | 276 | rx_fifo_status = geni_read_reg_nolog(uport->membase, |
Girish Mahadevan | 736892d | 2017-07-14 15:20:58 -0600 | [diff] [blame] | 277 | SE_GENI_RX_FIFO_STATUS); |
Girish Mahadevan | 6d97cbc | 2017-11-07 15:28:27 -0700 | [diff] [blame] | 278 | } |
| 279 | tx_active = m_cmd_active || tx_fifo_status; |
| 280 | rx_active = rx_fifo_status ? true : false; |
Girish Mahadevan | 736892d | 2017-07-14 15:20:58 -0600 | [diff] [blame] | 281 | |
Girish Mahadevan | 51fc9d4 | 2017-10-12 18:37:52 -0600 | [diff] [blame] | 282 | if (rx_active || tx_active || !uart_circ_empty(xmit)) |
Girish Mahadevan | 736892d | 2017-07-14 15:20:58 -0600 | [diff] [blame] | 283 | xfer_on = true; |
| 284 | |
| 285 | return xfer_on; |
| 286 | } |
| 287 | |
| 288 | static void wait_for_transfers_inflight(struct uart_port *uport) |
| 289 | { |
| 290 | int iter = 0; |
| 291 | struct msm_geni_serial_port *port = GET_DEV_PORT(uport); |
| 292 | |
| 293 | while (iter < WAIT_XFER_MAX_ITER) { |
| 294 | if (check_transfers_inflight(uport)) { |
| 295 | usleep_range(WAIT_XFER_MIN_TIMEOUT_US, |
| 296 | WAIT_XFER_MAX_TIMEOUT_US); |
| 297 | iter++; |
| 298 | } else { |
| 299 | break; |
| 300 | } |
| 301 | } |
| 302 | if (check_transfers_inflight(uport)) { |
| 303 | u32 geni_status = geni_read_reg_nolog(uport->membase, |
| 304 | SE_GENI_STATUS); |
| 305 | u32 geni_ios = geni_read_reg_nolog(uport->membase, SE_GENI_IOS); |
| 306 | u32 rx_fifo_status = geni_read_reg_nolog(uport->membase, |
| 307 | SE_GENI_RX_FIFO_STATUS); |
Girish Mahadevan | 6d97cbc | 2017-11-07 15:28:27 -0700 | [diff] [blame] | 308 | u32 rx_dma = |
| 309 | geni_read_reg_nolog(uport->membase, SE_DMA_RX_LEN_IN); |
Girish Mahadevan | 736892d | 2017-07-14 15:20:58 -0600 | [diff] [blame] | 310 | |
| 311 | IPC_LOG_MSG(port->ipc_log_misc, |
Girish Mahadevan | 6d97cbc | 2017-11-07 15:28:27 -0700 | [diff] [blame] | 312 | "%s IOS 0x%x geni status 0x%x rx: fifo 0x%x dma 0x%x\n", |
| 313 | __func__, geni_ios, geni_status, rx_fifo_status, rx_dma); |
Girish Mahadevan | 736892d | 2017-07-14 15:20:58 -0600 | [diff] [blame] | 314 | } |
Girish Mahadevan | 7115f4e | 2017-03-15 15:18:34 -0600 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | static int vote_clock_on(struct uart_port *uport) |
| 318 | { |
Girish Mahadevan | 7115f4e | 2017-03-15 15:18:34 -0600 | [diff] [blame] | 319 | struct msm_geni_serial_port *port = GET_DEV_PORT(uport); |
Girish Mahadevan | aa1bb7b | 2017-10-20 14:35:32 -0600 | [diff] [blame] | 320 | int usage_count = atomic_read(&uport->dev->power.usage_count); |
Girish Mahadevan | 736892d | 2017-07-14 15:20:58 -0600 | [diff] [blame] | 321 | int ret = 0; |
Girish Mahadevan | 7115f4e | 2017-03-15 15:18:34 -0600 | [diff] [blame] | 322 | |
Girish Mahadevan | 7115f4e | 2017-03-15 15:18:34 -0600 | [diff] [blame] | 323 | ret = msm_geni_serial_power_on(uport); |
| 324 | if (ret) { |
| 325 | dev_err(uport->dev, "Failed to vote clock on\n"); |
| 326 | return ret; |
| 327 | } |
Girish Mahadevan | a4ed038 | 2017-05-12 11:25:30 -0600 | [diff] [blame] | 328 | port->ioctl_count++; |
Girish Mahadevan | aa1bb7b | 2017-10-20 14:35:32 -0600 | [diff] [blame] | 329 | IPC_LOG_MSG(port->ipc_log_pwr, "%s%s ioctl %d usage_count %d\n", |
| 330 | __func__, current->comm, port->ioctl_count, usage_count); |
Girish Mahadevan | 7115f4e | 2017-03-15 15:18:34 -0600 | [diff] [blame] | 331 | return 0; |
| 332 | } |
| 333 | |
| 334 | static int vote_clock_off(struct uart_port *uport) |
| 335 | { |
| 336 | struct msm_geni_serial_port *port = GET_DEV_PORT(uport); |
Girish Mahadevan | aa1bb7b | 2017-10-20 14:35:32 -0600 | [diff] [blame] | 337 | int usage_count = atomic_read(&uport->dev->power.usage_count); |
Girish Mahadevan | 7115f4e | 2017-03-15 15:18:34 -0600 | [diff] [blame] | 338 | |
| 339 | if (!pm_runtime_enabled(uport->dev)) { |
| 340 | dev_err(uport->dev, "RPM not available.Can't enable clocks\n"); |
Girish Mahadevan | 736892d | 2017-07-14 15:20:58 -0600 | [diff] [blame] | 341 | return -EPERM; |
Girish Mahadevan | 7115f4e | 2017-03-15 15:18:34 -0600 | [diff] [blame] | 342 | } |
Girish Mahadevan | a4ed038 | 2017-05-12 11:25:30 -0600 | [diff] [blame] | 343 | if (!port->ioctl_count) { |
| 344 | dev_warn(uport->dev, "%s:Imbalanced vote off ioctl %d\n", |
Girish Mahadevan | 736892d | 2017-07-14 15:20:58 -0600 | [diff] [blame] | 345 | __func__, port->ioctl_count); |
Girish Mahadevan | a4ed038 | 2017-05-12 11:25:30 -0600 | [diff] [blame] | 346 | IPC_LOG_MSG(port->ipc_log_pwr, |
Girish Mahadevan | 736892d | 2017-07-14 15:20:58 -0600 | [diff] [blame] | 347 | "%s:Imbalanced vote_off from userspace. %d", |
| 348 | __func__, port->ioctl_count); |
| 349 | return -EPERM; |
Girish Mahadevan | a4ed038 | 2017-05-12 11:25:30 -0600 | [diff] [blame] | 350 | } |
Girish Mahadevan | 51fc9d4 | 2017-10-12 18:37:52 -0600 | [diff] [blame] | 351 | wait_for_transfers_inflight(uport); |
Girish Mahadevan | a4ed038 | 2017-05-12 11:25:30 -0600 | [diff] [blame] | 352 | port->ioctl_count--; |
Girish Mahadevan | 7115f4e | 2017-03-15 15:18:34 -0600 | [diff] [blame] | 353 | msm_geni_serial_power_off(uport); |
Girish Mahadevan | aa1bb7b | 2017-10-20 14:35:32 -0600 | [diff] [blame] | 354 | IPC_LOG_MSG(port->ipc_log_pwr, "%s%s ioctl %d usage_count %d\n", |
| 355 | __func__, current->comm, port->ioctl_count, usage_count); |
Girish Mahadevan | 7115f4e | 2017-03-15 15:18:34 -0600 | [diff] [blame] | 356 | return 0; |
| 357 | }; |
| 358 | |
| 359 | static int msm_geni_serial_ioctl(struct uart_port *uport, unsigned int cmd, |
| 360 | unsigned long arg) |
| 361 | { |
| 362 | int ret = -ENOIOCTLCMD; |
| 363 | |
| 364 | switch (cmd) { |
| 365 | case TIOCPMGET: { |
| 366 | ret = vote_clock_on(uport); |
| 367 | break; |
| 368 | } |
| 369 | case TIOCPMPUT: { |
| 370 | ret = vote_clock_off(uport); |
| 371 | break; |
| 372 | } |
| 373 | case TIOCPMACT: { |
| 374 | ret = !pm_runtime_status_suspended(uport->dev); |
| 375 | break; |
| 376 | } |
| 377 | default: |
| 378 | break; |
| 379 | } |
| 380 | return ret; |
| 381 | } |
| 382 | |
| 383 | static void msm_geni_serial_break_ctl(struct uart_port *uport, int ctl) |
| 384 | { |
Girish Mahadevan | 51fc9d4 | 2017-10-12 18:37:52 -0600 | [diff] [blame] | 385 | struct msm_geni_serial_port *port = GET_DEV_PORT(uport); |
| 386 | |
| 387 | if (!uart_console(uport) && device_pending_suspend(uport)) { |
| 388 | IPC_LOG_MSG(port->ipc_log_misc, |
| 389 | "%s.Device is suspended.\n", __func__); |
Girish Mahadevan | 7115f4e | 2017-03-15 15:18:34 -0600 | [diff] [blame] | 390 | return; |
Girish Mahadevan | 51fc9d4 | 2017-10-12 18:37:52 -0600 | [diff] [blame] | 391 | } |
Girish Mahadevan | 7115f4e | 2017-03-15 15:18:34 -0600 | [diff] [blame] | 392 | |
| 393 | if (ctl) { |
Girish Mahadevan | 736892d | 2017-07-14 15:20:58 -0600 | [diff] [blame] | 394 | wait_for_transfers_inflight(uport); |
Girish Mahadevan | 7115f4e | 2017-03-15 15:18:34 -0600 | [diff] [blame] | 395 | geni_setup_m_cmd(uport->membase, UART_START_BREAK, 0); |
| 396 | } else { |
| 397 | geni_setup_m_cmd(uport->membase, UART_STOP_BREAK, 0); |
| 398 | } |
| 399 | /* Ensure break start/stop command is setup before returning.*/ |
| 400 | mb(); |
| 401 | } |
| 402 | |
Karthikeyan Ramasubramanian | f5fb743 | 2017-05-24 21:59:55 -0600 | [diff] [blame] | 403 | static unsigned int msm_geni_cons_get_mctrl(struct uart_port *uport) |
| 404 | { |
| 405 | return TIOCM_DSR | TIOCM_CAR | TIOCM_CTS; |
| 406 | } |
| 407 | |
Girish Mahadevan | 7115f4e | 2017-03-15 15:18:34 -0600 | [diff] [blame] | 408 | static unsigned int msm_geni_serial_get_mctrl(struct uart_port *uport) |
| 409 | { |
| 410 | u32 geni_ios = 0; |
| 411 | unsigned int mctrl = TIOCM_DSR | TIOCM_CAR; |
| 412 | |
Girish Mahadevan | 6d97cbc | 2017-11-07 15:28:27 -0700 | [diff] [blame] | 413 | if (device_pending_suspend(uport)) |
Girish Mahadevan | 7115f4e | 2017-03-15 15:18:34 -0600 | [diff] [blame] | 414 | return TIOCM_DSR | TIOCM_CAR | TIOCM_CTS; |
| 415 | |
| 416 | geni_ios = geni_read_reg_nolog(uport->membase, SE_GENI_IOS); |
| 417 | if (!(geni_ios & IO2_DATA_IN)) |
| 418 | mctrl |= TIOCM_CTS; |
| 419 | |
| 420 | return mctrl; |
| 421 | } |
| 422 | |
| 423 | static void msm_geni_cons_set_mctrl(struct uart_port *uport, |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 424 | unsigned int mctrl) |
| 425 | { |
| 426 | } |
| 427 | |
Girish Mahadevan | 7115f4e | 2017-03-15 15:18:34 -0600 | [diff] [blame] | 428 | static void msm_geni_serial_set_mctrl(struct uart_port *uport, |
| 429 | unsigned int mctrl) |
| 430 | { |
| 431 | u32 uart_manual_rfr = 0; |
Girish Mahadevan | 51fc9d4 | 2017-10-12 18:37:52 -0600 | [diff] [blame] | 432 | struct msm_geni_serial_port *port = GET_DEV_PORT(uport); |
Girish Mahadevan | 7115f4e | 2017-03-15 15:18:34 -0600 | [diff] [blame] | 433 | |
Girish Mahadevan | 51fc9d4 | 2017-10-12 18:37:52 -0600 | [diff] [blame] | 434 | if (device_pending_suspend(uport)) { |
| 435 | IPC_LOG_MSG(port->ipc_log_misc, |
| 436 | "%s.Device is suspended.\n", __func__); |
Girish Mahadevan | 7115f4e | 2017-03-15 15:18:34 -0600 | [diff] [blame] | 437 | return; |
Girish Mahadevan | 51fc9d4 | 2017-10-12 18:37:52 -0600 | [diff] [blame] | 438 | } |
Girish Mahadevan | 6d97cbc | 2017-11-07 15:28:27 -0700 | [diff] [blame] | 439 | if (!(mctrl & TIOCM_RTS)) { |
Girish Mahadevan | 7115f4e | 2017-03-15 15:18:34 -0600 | [diff] [blame] | 440 | uart_manual_rfr |= (UART_MANUAL_RFR_EN | UART_RFR_NOT_READY); |
Girish Mahadevan | 6d97cbc | 2017-11-07 15:28:27 -0700 | [diff] [blame] | 441 | port->manual_flow = true; |
| 442 | } else { |
| 443 | port->manual_flow = false; |
| 444 | } |
Girish Mahadevan | 7115f4e | 2017-03-15 15:18:34 -0600 | [diff] [blame] | 445 | geni_write_reg_nolog(uart_manual_rfr, uport->membase, |
| 446 | SE_UART_MANUAL_RFR); |
| 447 | /* Write to flow control must complete before return to client*/ |
| 448 | mb(); |
| 449 | } |
| 450 | |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 451 | static const char *msm_geni_serial_get_type(struct uart_port *uport) |
| 452 | { |
| 453 | return "MSM"; |
| 454 | } |
| 455 | |
Girish Mahadevan | 7115f4e | 2017-03-15 15:18:34 -0600 | [diff] [blame] | 456 | static struct msm_geni_serial_port *get_port_from_line(int line, |
| 457 | bool is_console) |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 458 | { |
Girish Mahadevan | 7115f4e | 2017-03-15 15:18:34 -0600 | [diff] [blame] | 459 | struct msm_geni_serial_port *port = NULL; |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 460 | |
Girish Mahadevan | 7115f4e | 2017-03-15 15:18:34 -0600 | [diff] [blame] | 461 | if (is_console) { |
| 462 | if ((line < 0) || (line >= GENI_UART_CONS_PORTS)) |
| 463 | port = ERR_PTR(-ENXIO); |
| 464 | port = &msm_geni_console_port; |
| 465 | } else { |
| 466 | if ((line < 0) || (line >= GENI_UART_NR_PORTS)) |
| 467 | return ERR_PTR(-ENXIO); |
| 468 | port = &msm_geni_serial_ports[line]; |
| 469 | } |
| 470 | |
| 471 | return port; |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 472 | } |
| 473 | |
| 474 | static int msm_geni_serial_power_on(struct uart_port *uport) |
| 475 | { |
| 476 | int ret = 0; |
Girish Mahadevan | 736892d | 2017-07-14 15:20:58 -0600 | [diff] [blame] | 477 | struct msm_geni_serial_port *port = GET_DEV_PORT(uport); |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 478 | |
Girish Mahadevan | 5db3df7 | 2017-10-18 11:02:56 -0600 | [diff] [blame] | 479 | if (!pm_runtime_enabled(uport->dev)) { |
| 480 | if (pm_runtime_status_suspended(uport->dev)) { |
| 481 | struct uart_state *state = uport->state; |
| 482 | struct tty_port *tport = &state->port; |
| 483 | int lock = mutex_trylock(&tport->mutex); |
| 484 | |
| 485 | IPC_LOG_MSG(port->ipc_log_pwr, |
| 486 | "%s:Manual resume\n", __func__); |
| 487 | pm_runtime_disable(uport->dev); |
| 488 | ret = msm_geni_serial_runtime_resume(uport->dev); |
| 489 | if (ret) { |
| 490 | IPC_LOG_MSG(port->ipc_log_pwr, |
| 491 | "%s:Manual RPM CB failed %d\n", |
| 492 | __func__, ret); |
| 493 | } else { |
| 494 | pm_runtime_get_noresume(uport->dev); |
| 495 | pm_runtime_set_active(uport->dev); |
Karthikeyan Ramasubramanian | 0525e57 | 2017-11-30 16:33:43 -0700 | [diff] [blame] | 496 | enable_irq(uport->irq); |
Girish Mahadevan | 5db3df7 | 2017-10-18 11:02:56 -0600 | [diff] [blame] | 497 | } |
| 498 | pm_runtime_enable(uport->dev); |
| 499 | if (lock) |
| 500 | mutex_unlock(&tport->mutex); |
| 501 | } |
| 502 | } else { |
| 503 | ret = pm_runtime_get_sync(uport->dev); |
| 504 | if (ret < 0) { |
| 505 | IPC_LOG_MSG(port->ipc_log_pwr, "%s Err\n", __func__); |
| 506 | WARN_ON_ONCE(1); |
| 507 | pm_runtime_put_noidle(uport->dev); |
| 508 | pm_runtime_set_suspended(uport->dev); |
| 509 | return ret; |
| 510 | } |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 511 | } |
Girish Mahadevan | 7115f4e | 2017-03-15 15:18:34 -0600 | [diff] [blame] | 512 | return 0; |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 513 | } |
| 514 | |
| 515 | static void msm_geni_serial_power_off(struct uart_port *uport) |
| 516 | { |
Girish Mahadevan | 51fc9d4 | 2017-10-12 18:37:52 -0600 | [diff] [blame] | 517 | struct msm_geni_serial_port *port = GET_DEV_PORT(uport); |
| 518 | int usage_count = atomic_read(&uport->dev->power.usage_count); |
| 519 | |
| 520 | if (!usage_count) { |
| 521 | IPC_LOG_MSG(port->ipc_log_pwr, "%s: Usage Count is already 0\n", |
| 522 | __func__); |
| 523 | return; |
| 524 | } |
Girish Mahadevan | c2b9252 | 2017-08-17 22:41:32 -0600 | [diff] [blame] | 525 | pm_runtime_mark_last_busy(uport->dev); |
| 526 | pm_runtime_put_autosuspend(uport->dev); |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 527 | } |
| 528 | |
| 529 | static int msm_geni_serial_poll_bit(struct uart_port *uport, |
Girish Mahadevan | 9149f83 | 2017-04-18 11:10:51 -0600 | [diff] [blame] | 530 | int offset, int bit_field, bool set) |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 531 | { |
| 532 | int iter = 0; |
| 533 | unsigned int reg; |
| 534 | bool met = false; |
Girish Mahadevan | 7115f4e | 2017-03-15 15:18:34 -0600 | [diff] [blame] | 535 | struct msm_geni_serial_port *port = NULL; |
Girish Mahadevan | 9149f83 | 2017-04-18 11:10:51 -0600 | [diff] [blame] | 536 | bool cond = false; |
Girish Mahadevan | 7115f4e | 2017-03-15 15:18:34 -0600 | [diff] [blame] | 537 | unsigned int baud = 115200; |
| 538 | unsigned int fifo_bits = DEF_FIFO_DEPTH_WORDS * DEF_FIFO_WIDTH_BITS; |
Girish Mahadevan | a4ed038 | 2017-05-12 11:25:30 -0600 | [diff] [blame] | 539 | unsigned long total_iter = 1000; |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 540 | |
Girish Mahadevan | 7115f4e | 2017-03-15 15:18:34 -0600 | [diff] [blame] | 541 | |
Girish Mahadevan | a4ed038 | 2017-05-12 11:25:30 -0600 | [diff] [blame] | 542 | if (uport->private_data && !uart_console(uport)) { |
Girish Mahadevan | 7115f4e | 2017-03-15 15:18:34 -0600 | [diff] [blame] | 543 | port = GET_DEV_PORT(uport); |
| 544 | baud = (port->cur_baud ? port->cur_baud : 115200); |
| 545 | fifo_bits = port->tx_fifo_depth * port->tx_fifo_width; |
Girish Mahadevan | a4ed038 | 2017-05-12 11:25:30 -0600 | [diff] [blame] | 546 | /* |
| 547 | * Total polling iterations based on FIFO worth of bytes to be |
| 548 | * sent at current baud .Add a little fluff to the wait. |
| 549 | */ |
Girish Mahadevan | 6d97cbc | 2017-11-07 15:28:27 -0700 | [diff] [blame] | 550 | total_iter = ((fifo_bits * USEC_PER_SEC) / baud) / 10; |
Girish Mahadevan | a4ed038 | 2017-05-12 11:25:30 -0600 | [diff] [blame] | 551 | total_iter += 50; |
Girish Mahadevan | 7115f4e | 2017-03-15 15:18:34 -0600 | [diff] [blame] | 552 | } |
Girish Mahadevan | 7115f4e | 2017-03-15 15:18:34 -0600 | [diff] [blame] | 553 | |
| 554 | while (iter < total_iter) { |
Girish Mahadevan | bd9b44f | 2017-04-11 13:11:10 -0600 | [diff] [blame] | 555 | reg = geni_read_reg_nolog(uport->membase, offset); |
Girish Mahadevan | 9149f83 | 2017-04-18 11:10:51 -0600 | [diff] [blame] | 556 | cond = reg & bit_field; |
| 557 | if (cond == set) { |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 558 | met = true; |
| 559 | break; |
| 560 | } |
| 561 | udelay(10); |
| 562 | iter++; |
| 563 | } |
| 564 | return met; |
| 565 | } |
| 566 | |
| 567 | static void msm_geni_serial_setup_tx(struct uart_port *uport, |
Girish Mahadevan | b1ab172 | 2017-04-27 16:39:11 -0600 | [diff] [blame] | 568 | unsigned int xmit_size) |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 569 | { |
Girish Mahadevan | 9149f83 | 2017-04-18 11:10:51 -0600 | [diff] [blame] | 570 | u32 m_cmd = 0; |
| 571 | |
Girish Mahadevan | bd9b44f | 2017-04-11 13:11:10 -0600 | [diff] [blame] | 572 | geni_write_reg_nolog(xmit_size, uport->membase, SE_UART_TX_TRANS_LEN); |
Girish Mahadevan | 9149f83 | 2017-04-18 11:10:51 -0600 | [diff] [blame] | 573 | m_cmd |= (UART_START_TX << M_OPCODE_SHFT); |
| 574 | geni_write_reg_nolog(m_cmd, uport->membase, SE_GENI_M_CMD0); |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 575 | /* |
| 576 | * Writes to enable the primary sequencer should go through before |
| 577 | * exiting this function. |
| 578 | */ |
| 579 | mb(); |
| 580 | } |
| 581 | |
| 582 | static void msm_geni_serial_poll_cancel_tx(struct uart_port *uport) |
| 583 | { |
| 584 | int done = 0; |
| 585 | unsigned int irq_clear = M_CMD_DONE_EN; |
| 586 | |
| 587 | done = msm_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS, |
Girish Mahadevan | 9149f83 | 2017-04-18 11:10:51 -0600 | [diff] [blame] | 588 | M_CMD_DONE_EN, true); |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 589 | if (!done) { |
Girish Mahadevan | a4ed038 | 2017-05-12 11:25:30 -0600 | [diff] [blame] | 590 | geni_write_reg_nolog(M_GENI_CMD_ABORT, uport->membase, |
| 591 | SE_GENI_M_CMD_CTRL_REG); |
| 592 | irq_clear |= M_CMD_ABORT_EN; |
| 593 | msm_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS, |
Girish Mahadevan | 9149f83 | 2017-04-18 11:10:51 -0600 | [diff] [blame] | 594 | M_CMD_ABORT_EN, true); |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 595 | } |
Girish Mahadevan | bd9b44f | 2017-04-11 13:11:10 -0600 | [diff] [blame] | 596 | geni_write_reg_nolog(irq_clear, uport->membase, SE_GENI_M_IRQ_CLEAR); |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 597 | } |
| 598 | |
Girish Mahadevan | 9149f83 | 2017-04-18 11:10:51 -0600 | [diff] [blame] | 599 | static void msm_geni_serial_abort_rx(struct uart_port *uport) |
Girish Mahadevan | 24f5659 | 2017-04-15 17:35:05 -0600 | [diff] [blame] | 600 | { |
Girish Mahadevan | 24f5659 | 2017-04-15 17:35:05 -0600 | [diff] [blame] | 601 | unsigned int irq_clear = S_CMD_DONE_EN; |
| 602 | |
Girish Mahadevan | 9149f83 | 2017-04-18 11:10:51 -0600 | [diff] [blame] | 603 | geni_abort_s_cmd(uport->membase); |
| 604 | /* Ensure this goes through before polling. */ |
| 605 | mb(); |
| 606 | irq_clear |= S_CMD_ABORT_EN; |
| 607 | msm_geni_serial_poll_bit(uport, SE_GENI_S_CMD_CTRL_REG, |
| 608 | S_GENI_CMD_ABORT, false); |
Girish Mahadevan | 24f5659 | 2017-04-15 17:35:05 -0600 | [diff] [blame] | 609 | geni_write_reg_nolog(irq_clear, uport->membase, SE_GENI_S_IRQ_CLEAR); |
Girish Mahadevan | eecdd97 | 2017-08-22 17:58:08 -0600 | [diff] [blame] | 610 | geni_write_reg(FORCE_DEFAULT, uport->membase, GENI_FORCE_DEFAULT_REG); |
Girish Mahadevan | 24f5659 | 2017-04-15 17:35:05 -0600 | [diff] [blame] | 611 | } |
Girish Mahadevan | 9149f83 | 2017-04-18 11:10:51 -0600 | [diff] [blame] | 612 | |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 613 | #ifdef CONFIG_CONSOLE_POLL |
| 614 | static int msm_geni_serial_get_char(struct uart_port *uport) |
| 615 | { |
| 616 | unsigned int rx_fifo; |
| 617 | unsigned int m_irq_status; |
| 618 | unsigned int s_irq_status; |
| 619 | |
| 620 | if (!(msm_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS, |
Karthikeyan Ramasubramanian | 56351b6 | 2017-04-21 15:11:03 -0600 | [diff] [blame] | 621 | M_SEC_IRQ_EN, true))) |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 622 | return -ENXIO; |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 623 | |
Girish Mahadevan | bd9b44f | 2017-04-11 13:11:10 -0600 | [diff] [blame] | 624 | m_irq_status = geni_read_reg_nolog(uport->membase, |
| 625 | SE_GENI_M_IRQ_STATUS); |
| 626 | s_irq_status = geni_read_reg_nolog(uport->membase, |
| 627 | SE_GENI_S_IRQ_STATUS); |
Girish Mahadevan | 7115f4e | 2017-03-15 15:18:34 -0600 | [diff] [blame] | 628 | geni_write_reg_nolog(m_irq_status, uport->membase, |
| 629 | SE_GENI_M_IRQ_CLEAR); |
| 630 | geni_write_reg_nolog(s_irq_status, uport->membase, |
| 631 | SE_GENI_S_IRQ_CLEAR); |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 632 | |
| 633 | if (!(msm_geni_serial_poll_bit(uport, SE_GENI_RX_FIFO_STATUS, |
Karthikeyan Ramasubramanian | 56351b6 | 2017-04-21 15:11:03 -0600 | [diff] [blame] | 634 | RX_FIFO_WC_MSK, true))) |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 635 | return -ENXIO; |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 636 | |
| 637 | /* |
| 638 | * Read the Rx FIFO only after clearing the interrupt registers and |
| 639 | * getting valid RX fifo status. |
| 640 | */ |
| 641 | mb(); |
Girish Mahadevan | bd9b44f | 2017-04-11 13:11:10 -0600 | [diff] [blame] | 642 | rx_fifo = geni_read_reg_nolog(uport->membase, SE_GENI_RX_FIFOn); |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 643 | rx_fifo &= 0xFF; |
| 644 | return rx_fifo; |
| 645 | } |
| 646 | |
| 647 | static void msm_geni_serial_poll_put_char(struct uart_port *uport, |
| 648 | unsigned char c) |
| 649 | { |
| 650 | int b = (int) c; |
| 651 | struct msm_geni_serial_port *port = GET_DEV_PORT(uport); |
| 652 | |
Girish Mahadevan | bd9b44f | 2017-04-11 13:11:10 -0600 | [diff] [blame] | 653 | geni_write_reg_nolog(port->tx_wm, uport->membase, |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 654 | SE_GENI_TX_WATERMARK_REG); |
| 655 | msm_geni_serial_setup_tx(uport, 1); |
| 656 | if (!msm_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS, |
Girish Mahadevan | 9149f83 | 2017-04-18 11:10:51 -0600 | [diff] [blame] | 657 | M_TX_FIFO_WATERMARK_EN, true)) |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 658 | WARN_ON(1); |
Girish Mahadevan | bd9b44f | 2017-04-11 13:11:10 -0600 | [diff] [blame] | 659 | geni_write_reg_nolog(b, uport->membase, SE_GENI_TX_FIFOn); |
| 660 | geni_write_reg_nolog(M_TX_FIFO_WATERMARK_EN, uport->membase, |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 661 | SE_GENI_M_IRQ_CLEAR); |
| 662 | /* |
| 663 | * Ensure FIFO write goes through before polling for status but. |
| 664 | */ |
| 665 | mb(); |
| 666 | msm_geni_serial_poll_cancel_tx(uport); |
| 667 | } |
| 668 | #endif |
| 669 | |
Girish Mahadevan | f08b110 | 2017-04-02 19:27:28 -0600 | [diff] [blame] | 670 | #if defined(CONFIG_SERIAL_CORE_CONSOLE) || defined(CONFIG_CONSOLE_POLL) |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 671 | static void msm_geni_serial_wr_char(struct uart_port *uport, int ch) |
| 672 | { |
Girish Mahadevan | bd9b44f | 2017-04-11 13:11:10 -0600 | [diff] [blame] | 673 | geni_write_reg_nolog(ch, uport->membase, SE_GENI_TX_FIFOn); |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 674 | /* |
| 675 | * Ensure FIFO write clear goes through before |
| 676 | * next iteration. |
| 677 | */ |
| 678 | mb(); |
| 679 | |
| 680 | } |
| 681 | |
| 682 | static void |
| 683 | __msm_geni_serial_console_write(struct uart_port *uport, const char *s, |
| 684 | unsigned int count) |
| 685 | { |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 686 | int new_line = 0; |
| 687 | int i; |
| 688 | int bytes_to_send = count; |
Girish Mahadevan | b1ab172 | 2017-04-27 16:39:11 -0600 | [diff] [blame] | 689 | int fifo_depth = DEF_FIFO_DEPTH_WORDS; |
| 690 | int tx_wm = DEF_TX_WM; |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 691 | |
| 692 | for (i = 0; i < count; i++) { |
| 693 | if (s[i] == '\n') |
| 694 | new_line++; |
| 695 | } |
| 696 | |
| 697 | bytes_to_send += new_line; |
Girish Mahadevan | b1ab172 | 2017-04-27 16:39:11 -0600 | [diff] [blame] | 698 | geni_write_reg_nolog(tx_wm, uport->membase, |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 699 | SE_GENI_TX_WATERMARK_REG); |
| 700 | msm_geni_serial_setup_tx(uport, bytes_to_send); |
| 701 | i = 0; |
| 702 | while (i < count) { |
| 703 | u32 chars_to_write = 0; |
Girish Mahadevan | b1ab172 | 2017-04-27 16:39:11 -0600 | [diff] [blame] | 704 | u32 avail_fifo_bytes = (fifo_depth - tx_wm); |
Girish Mahadevan | 24f5659 | 2017-04-15 17:35:05 -0600 | [diff] [blame] | 705 | /* |
| 706 | * If the WM bit never set, then the Tx state machine is not |
| 707 | * in a valid state, so break, cancel/abort any existing |
| 708 | * command. Unfortunately the current data being written is |
| 709 | * lost. |
| 710 | */ |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 711 | while (!msm_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS, |
Girish Mahadevan | 9149f83 | 2017-04-18 11:10:51 -0600 | [diff] [blame] | 712 | M_TX_FIFO_WATERMARK_EN, true)) |
Girish Mahadevan | 24f5659 | 2017-04-15 17:35:05 -0600 | [diff] [blame] | 713 | break; |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 714 | chars_to_write = min((unsigned int)(count - i), |
| 715 | avail_fifo_bytes); |
| 716 | if ((chars_to_write << 1) > avail_fifo_bytes) |
| 717 | chars_to_write = (avail_fifo_bytes >> 1); |
| 718 | uart_console_write(uport, (s + i), chars_to_write, |
| 719 | msm_geni_serial_wr_char); |
Girish Mahadevan | bd9b44f | 2017-04-11 13:11:10 -0600 | [diff] [blame] | 720 | geni_write_reg_nolog(M_TX_FIFO_WATERMARK_EN, uport->membase, |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 721 | SE_GENI_M_IRQ_CLEAR); |
Girish Mahadevan | bd9b44f | 2017-04-11 13:11:10 -0600 | [diff] [blame] | 722 | /* Ensure this goes through before polling for WM IRQ again.*/ |
| 723 | mb(); |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 724 | i += chars_to_write; |
| 725 | } |
| 726 | msm_geni_serial_poll_cancel_tx(uport); |
| 727 | } |
| 728 | |
| 729 | static void msm_geni_serial_console_write(struct console *co, const char *s, |
| 730 | unsigned int count) |
| 731 | { |
| 732 | struct uart_port *uport; |
| 733 | struct msm_geni_serial_port *port; |
Karthikeyan Ramasubramanian | f5fb743 | 2017-05-24 21:59:55 -0600 | [diff] [blame] | 734 | int locked = 1; |
| 735 | unsigned long flags; |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 736 | |
| 737 | WARN_ON(co->index < 0 || co->index >= GENI_UART_NR_PORTS); |
| 738 | |
Girish Mahadevan | 7115f4e | 2017-03-15 15:18:34 -0600 | [diff] [blame] | 739 | port = get_port_from_line(co->index, true); |
Karthikeyan Ramasubramanian | 56351b6 | 2017-04-21 15:11:03 -0600 | [diff] [blame] | 740 | if (IS_ERR_OR_NULL(port)) |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 741 | return; |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 742 | |
| 743 | uport = &port->uport; |
Karthikeyan Ramasubramanian | f5fb743 | 2017-05-24 21:59:55 -0600 | [diff] [blame] | 744 | if (oops_in_progress) |
| 745 | locked = spin_trylock_irqsave(&uport->lock, flags); |
| 746 | else |
| 747 | spin_lock_irqsave(&uport->lock, flags); |
| 748 | |
| 749 | if (locked) { |
| 750 | __msm_geni_serial_console_write(uport, s, count); |
| 751 | spin_unlock_irqrestore(&uport->lock, flags); |
| 752 | } |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 753 | } |
| 754 | |
Girish Mahadevan | f08b110 | 2017-04-02 19:27:28 -0600 | [diff] [blame] | 755 | static int handle_rx_console(struct uart_port *uport, |
| 756 | unsigned int rx_fifo_wc, |
| 757 | unsigned int rx_last_byte_valid, |
Karthikeyan Ramasubramanian | e67fd2e | 2017-08-30 17:16:58 -0600 | [diff] [blame] | 758 | unsigned int rx_last, |
| 759 | bool drop_rx) |
Girish Mahadevan | f08b110 | 2017-04-02 19:27:28 -0600 | [diff] [blame] | 760 | { |
| 761 | int i, c; |
| 762 | unsigned char *rx_char; |
| 763 | struct tty_port *tport; |
| 764 | struct msm_geni_serial_port *msm_port = GET_DEV_PORT(uport); |
| 765 | |
| 766 | tport = &uport->state->port; |
Girish Mahadevan | f08b110 | 2017-04-02 19:27:28 -0600 | [diff] [blame] | 767 | for (i = 0; i < rx_fifo_wc; i++) { |
| 768 | int bytes = 4; |
| 769 | |
| 770 | *(msm_port->rx_fifo) = |
Girish Mahadevan | bd9b44f | 2017-04-11 13:11:10 -0600 | [diff] [blame] | 771 | geni_read_reg_nolog(uport->membase, SE_GENI_RX_FIFOn); |
Karthikeyan Ramasubramanian | e67fd2e | 2017-08-30 17:16:58 -0600 | [diff] [blame] | 772 | if (drop_rx) |
| 773 | continue; |
Girish Mahadevan | f08b110 | 2017-04-02 19:27:28 -0600 | [diff] [blame] | 774 | rx_char = (unsigned char *)msm_port->rx_fifo; |
| 775 | |
| 776 | if (i == (rx_fifo_wc - 1)) { |
| 777 | if (rx_last && rx_last_byte_valid) |
| 778 | bytes = rx_last_byte_valid; |
| 779 | } |
| 780 | for (c = 0; c < bytes; c++) { |
| 781 | char flag = TTY_NORMAL; |
| 782 | int sysrq; |
| 783 | |
| 784 | uport->icount.rx++; |
| 785 | sysrq = uart_handle_sysrq_char(uport, rx_char[c]); |
| 786 | if (!sysrq) |
| 787 | tty_insert_flip_char(tport, rx_char[c], flag); |
| 788 | } |
| 789 | } |
Karthikeyan Ramasubramanian | e67fd2e | 2017-08-30 17:16:58 -0600 | [diff] [blame] | 790 | if (!drop_rx) |
| 791 | tty_flip_buffer_push(tport); |
Girish Mahadevan | f08b110 | 2017-04-02 19:27:28 -0600 | [diff] [blame] | 792 | return 0; |
| 793 | } |
| 794 | #else |
| 795 | static int handle_rx_console(struct uart_port *uport, |
| 796 | unsigned int rx_fifo_wc, |
| 797 | unsigned int rx_last_byte_valid, |
Karthikeyan Ramasubramanian | e67fd2e | 2017-08-30 17:16:58 -0600 | [diff] [blame] | 798 | unsigned int rx_last, |
| 799 | bool drop_rx) |
Girish Mahadevan | f08b110 | 2017-04-02 19:27:28 -0600 | [diff] [blame] | 800 | { |
| 801 | return -EPERM; |
| 802 | } |
| 803 | |
| 804 | #endif /* (CONFIG_SERIAL_CORE_CONSOLE) || defined(CONFIG_CONSOLE_POLL)) */ |
| 805 | |
Karthikeyan Ramasubramanian | 8ec770cf | 2017-04-20 15:29:09 -0600 | [diff] [blame] | 806 | static int msm_geni_serial_prep_dma_tx(struct uart_port *uport) |
| 807 | { |
| 808 | struct msm_geni_serial_port *msm_port = GET_DEV_PORT(uport); |
| 809 | struct circ_buf *xmit = &uport->state->xmit; |
| 810 | unsigned int xmit_size; |
| 811 | int ret = 0; |
| 812 | |
| 813 | xmit_size = uart_circ_chars_pending(xmit); |
| 814 | if (xmit_size < WAKEUP_CHARS) |
| 815 | uart_write_wakeup(uport); |
| 816 | |
| 817 | if (xmit_size > (UART_XMIT_SIZE - xmit->tail)) |
| 818 | xmit_size = UART_XMIT_SIZE - xmit->tail; |
| 819 | |
| 820 | if (!xmit_size) |
| 821 | return ret; |
| 822 | |
| 823 | dump_ipc(msm_port->ipc_log_tx, "DMA Tx", |
| 824 | (char *)&xmit->buf[xmit->tail], 0, xmit_size); |
| 825 | msm_geni_serial_setup_tx(uport, xmit_size); |
| 826 | ret = geni_se_tx_dma_prep(msm_port->wrapper_dev, uport->membase, |
| 827 | &xmit->buf[xmit->tail], xmit_size, &msm_port->tx_dma); |
| 828 | if (!ret) { |
| 829 | msm_port->xmit_size = xmit_size; |
| 830 | } else { |
| 831 | geni_write_reg_nolog(0, uport->membase, |
| 832 | SE_UART_TX_TRANS_LEN); |
| 833 | geni_cancel_m_cmd(uport->membase); |
| 834 | if (!msm_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS, |
| 835 | M_CMD_CANCEL_EN, true)) { |
| 836 | geni_abort_m_cmd(uport->membase); |
| 837 | msm_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS, |
| 838 | M_CMD_ABORT_EN, true); |
| 839 | geni_write_reg_nolog(M_CMD_ABORT_EN, uport->membase, |
| 840 | SE_GENI_M_IRQ_CLEAR); |
| 841 | } |
| 842 | geni_write_reg_nolog(M_CMD_CANCEL_EN, uport->membase, |
| 843 | SE_GENI_M_IRQ_CLEAR); |
| 844 | IPC_LOG_MSG(msm_port->ipc_log_tx, "%s: DMA map failure %d\n", |
| 845 | __func__, ret); |
| 846 | msm_port->tx_dma = (dma_addr_t)NULL; |
| 847 | msm_port->xmit_size = 0; |
| 848 | } |
| 849 | return ret; |
| 850 | } |
| 851 | |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 852 | static void msm_geni_serial_start_tx(struct uart_port *uport) |
| 853 | { |
| 854 | unsigned int geni_m_irq_en; |
Girish Mahadevan | 7115f4e | 2017-03-15 15:18:34 -0600 | [diff] [blame] | 855 | struct msm_geni_serial_port *msm_port = GET_DEV_PORT(uport); |
Karthikeyan Ramasubramanian | 61074fa | 2017-05-24 00:18:34 -0600 | [diff] [blame] | 856 | unsigned int geni_status; |
Karthikeyan Ramasubramanian | 8ec770cf | 2017-04-20 15:29:09 -0600 | [diff] [blame] | 857 | unsigned int geni_ios; |
Girish Mahadevan | b1ab172 | 2017-04-27 16:39:11 -0600 | [diff] [blame] | 858 | |
Girish Mahadevan | 51fc9d4 | 2017-10-12 18:37:52 -0600 | [diff] [blame] | 859 | if (!uart_console(uport) && !pm_runtime_active(uport->dev)) { |
Girish Mahadevan | 736892d | 2017-07-14 15:20:58 -0600 | [diff] [blame] | 860 | IPC_LOG_MSG(msm_port->ipc_log_misc, |
Girish Mahadevan | 51fc9d4 | 2017-10-12 18:37:52 -0600 | [diff] [blame] | 861 | "%s.Putting in async RPM vote\n", __func__); |
| 862 | pm_runtime_get(uport->dev); |
| 863 | goto exit_start_tx; |
Girish Mahadevan | 7115f4e | 2017-03-15 15:18:34 -0600 | [diff] [blame] | 864 | } |
| 865 | |
Girish Mahadevan | aa1bb7b | 2017-10-20 14:35:32 -0600 | [diff] [blame] | 866 | if (!uart_console(uport)) { |
| 867 | IPC_LOG_MSG(msm_port->ipc_log_misc, |
| 868 | "%s.Power on.\n", __func__); |
Girish Mahadevan | 51fc9d4 | 2017-10-12 18:37:52 -0600 | [diff] [blame] | 869 | pm_runtime_get(uport->dev); |
Girish Mahadevan | aa1bb7b | 2017-10-20 14:35:32 -0600 | [diff] [blame] | 870 | } |
Girish Mahadevan | 51fc9d4 | 2017-10-12 18:37:52 -0600 | [diff] [blame] | 871 | |
Karthikeyan Ramasubramanian | 8ec770cf | 2017-04-20 15:29:09 -0600 | [diff] [blame] | 872 | if (msm_port->xfer_mode == FIFO_MODE) { |
| 873 | geni_status = geni_read_reg_nolog(uport->membase, |
| 874 | SE_GENI_STATUS); |
| 875 | if (geni_status & M_GENI_CMD_ACTIVE) |
| 876 | goto check_flow_ctrl; |
Karthikeyan Ramasubramanian | 61074fa | 2017-05-24 00:18:34 -0600 | [diff] [blame] | 877 | |
Karthikeyan Ramasubramanian | 8ec770cf | 2017-04-20 15:29:09 -0600 | [diff] [blame] | 878 | if (!msm_geni_serial_tx_empty(uport)) |
| 879 | goto check_flow_ctrl; |
Karthikeyan Ramasubramanian | 61074fa | 2017-05-24 00:18:34 -0600 | [diff] [blame] | 880 | |
Karthikeyan Ramasubramanian | 8ec770cf | 2017-04-20 15:29:09 -0600 | [diff] [blame] | 881 | geni_m_irq_en = geni_read_reg_nolog(uport->membase, |
| 882 | SE_GENI_M_IRQ_EN); |
| 883 | geni_m_irq_en |= (M_TX_FIFO_WATERMARK_EN | M_CMD_DONE_EN); |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 884 | |
Karthikeyan Ramasubramanian | 8ec770cf | 2017-04-20 15:29:09 -0600 | [diff] [blame] | 885 | geni_write_reg_nolog(msm_port->tx_wm, uport->membase, |
Girish Mahadevan | bd9b44f | 2017-04-11 13:11:10 -0600 | [diff] [blame] | 886 | SE_GENI_TX_WATERMARK_REG); |
Karthikeyan Ramasubramanian | 8ec770cf | 2017-04-20 15:29:09 -0600 | [diff] [blame] | 887 | geni_write_reg_nolog(geni_m_irq_en, uport->membase, |
| 888 | SE_GENI_M_IRQ_EN); |
| 889 | /* Geni command setup should complete before returning.*/ |
| 890 | mb(); |
| 891 | } else if (msm_port->xfer_mode == SE_DMA) { |
| 892 | if (msm_port->tx_dma) |
| 893 | goto check_flow_ctrl; |
| 894 | |
| 895 | msm_geni_serial_prep_dma_tx(uport); |
| 896 | } |
| 897 | IPC_LOG_MSG(msm_port->ipc_log_misc, "%s\n", __func__); |
| 898 | return; |
| 899 | check_flow_ctrl: |
| 900 | geni_ios = geni_read_reg_nolog(uport->membase, SE_GENI_IOS); |
| 901 | if (!(geni_ios & IO2_DATA_IN)) |
| 902 | IPC_LOG_MSG(msm_port->ipc_log_misc, "%s: ios: 0x%08x\n", |
| 903 | __func__, geni_ios); |
Girish Mahadevan | 51fc9d4 | 2017-10-12 18:37:52 -0600 | [diff] [blame] | 904 | exit_start_tx: |
| 905 | if (!uart_console(uport)) |
| 906 | msm_geni_serial_power_off(uport); |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 907 | } |
| 908 | |
Karthikeyan Ramasubramanian | b6be8c2 | 2017-08-23 16:34:00 -0600 | [diff] [blame] | 909 | static void msm_geni_serial_tx_fsm_rst(struct uart_port *uport) |
| 910 | { |
| 911 | unsigned int tx_irq_en; |
| 912 | int done = 0; |
| 913 | int tries = 0; |
| 914 | |
| 915 | tx_irq_en = geni_read_reg_nolog(uport->membase, SE_DMA_TX_IRQ_EN); |
| 916 | geni_write_reg_nolog(0, uport->membase, SE_DMA_TX_IRQ_EN_SET); |
| 917 | geni_write_reg_nolog(1, uport->membase, SE_DMA_TX_FSM_RST); |
| 918 | do { |
| 919 | done = msm_geni_serial_poll_bit(uport, SE_DMA_TX_IRQ_STAT, |
| 920 | TX_RESET_DONE, true); |
| 921 | tries++; |
| 922 | } while (!done && tries < 5); |
| 923 | geni_write_reg_nolog(TX_DMA_DONE | TX_RESET_DONE, uport->membase, |
| 924 | SE_DMA_TX_IRQ_CLR); |
| 925 | geni_write_reg_nolog(tx_irq_en, uport->membase, SE_DMA_TX_IRQ_EN_SET); |
| 926 | } |
| 927 | |
Girish Mahadevan | 6d97cbc | 2017-11-07 15:28:27 -0700 | [diff] [blame] | 928 | static void stop_tx_sequencer(struct uart_port *uport) |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 929 | { |
| 930 | unsigned int geni_m_irq_en; |
| 931 | unsigned int geni_status; |
Girish Mahadevan | 7115f4e | 2017-03-15 15:18:34 -0600 | [diff] [blame] | 932 | struct msm_geni_serial_port *port = GET_DEV_PORT(uport); |
| 933 | |
Girish Mahadevan | bd9b44f | 2017-04-11 13:11:10 -0600 | [diff] [blame] | 934 | geni_m_irq_en = geni_read_reg_nolog(uport->membase, SE_GENI_M_IRQ_EN); |
Karthikeyan Ramasubramanian | 8ec770cf | 2017-04-20 15:29:09 -0600 | [diff] [blame] | 935 | geni_m_irq_en &= ~M_CMD_DONE_EN; |
| 936 | if (port->xfer_mode == FIFO_MODE) { |
| 937 | geni_m_irq_en &= ~M_TX_FIFO_WATERMARK_EN; |
| 938 | geni_write_reg_nolog(0, uport->membase, |
| 939 | SE_GENI_TX_WATERMARK_REG); |
| 940 | } else if (port->xfer_mode == SE_DMA) { |
| 941 | if (port->tx_dma) { |
Karthikeyan Ramasubramanian | b6be8c2 | 2017-08-23 16:34:00 -0600 | [diff] [blame] | 942 | msm_geni_serial_tx_fsm_rst(uport); |
Karthikeyan Ramasubramanian | 8ec770cf | 2017-04-20 15:29:09 -0600 | [diff] [blame] | 943 | geni_se_tx_dma_unprep(port->wrapper_dev, port->tx_dma, |
| 944 | port->xmit_size); |
| 945 | port->tx_dma = (dma_addr_t)NULL; |
| 946 | } |
| 947 | } |
| 948 | port->xmit_size = 0; |
Girish Mahadevan | bd9b44f | 2017-04-11 13:11:10 -0600 | [diff] [blame] | 949 | geni_write_reg_nolog(geni_m_irq_en, uport->membase, SE_GENI_M_IRQ_EN); |
Girish Mahadevan | bd9b44f | 2017-04-11 13:11:10 -0600 | [diff] [blame] | 950 | geni_status = geni_read_reg_nolog(uport->membase, |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 951 | SE_GENI_STATUS); |
| 952 | /* Possible stop tx is called multiple times. */ |
| 953 | if (!(geni_status & M_GENI_CMD_ACTIVE)) |
| 954 | return; |
| 955 | |
| 956 | geni_cancel_m_cmd(uport->membase); |
| 957 | if (!msm_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS, |
Girish Mahadevan | 9149f83 | 2017-04-18 11:10:51 -0600 | [diff] [blame] | 958 | M_CMD_CANCEL_EN, true)) { |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 959 | geni_abort_m_cmd(uport->membase); |
| 960 | msm_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS, |
Girish Mahadevan | 9149f83 | 2017-04-18 11:10:51 -0600 | [diff] [blame] | 961 | M_CMD_ABORT_EN, true); |
Girish Mahadevan | bd9b44f | 2017-04-11 13:11:10 -0600 | [diff] [blame] | 962 | geni_write_reg_nolog(M_CMD_ABORT_EN, uport->membase, |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 963 | SE_GENI_M_IRQ_CLEAR); |
| 964 | } |
Girish Mahadevan | bd9b44f | 2017-04-11 13:11:10 -0600 | [diff] [blame] | 965 | geni_write_reg_nolog(M_CMD_CANCEL_EN, uport, SE_GENI_M_IRQ_CLEAR); |
Girish Mahadevan | 7115f4e | 2017-03-15 15:18:34 -0600 | [diff] [blame] | 966 | IPC_LOG_MSG(port->ipc_log_misc, "%s\n", __func__); |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 967 | } |
| 968 | |
Girish Mahadevan | 6d97cbc | 2017-11-07 15:28:27 -0700 | [diff] [blame] | 969 | static void msm_geni_serial_stop_tx(struct uart_port *uport) |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 970 | { |
Girish Mahadevan | 7115f4e | 2017-03-15 15:18:34 -0600 | [diff] [blame] | 971 | struct msm_geni_serial_port *port = GET_DEV_PORT(uport); |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 972 | |
Girish Mahadevan | 51fc9d4 | 2017-10-12 18:37:52 -0600 | [diff] [blame] | 973 | if (!uart_console(uport) && device_pending_suspend(uport)) { |
Girish Mahadevan | 736892d | 2017-07-14 15:20:58 -0600 | [diff] [blame] | 974 | dev_err(uport->dev, "%s.Device is suspended.\n", __func__); |
| 975 | IPC_LOG_MSG(port->ipc_log_misc, |
| 976 | "%s.Device is suspended.\n", __func__); |
Girish Mahadevan | 7115f4e | 2017-03-15 15:18:34 -0600 | [diff] [blame] | 977 | return; |
Girish Mahadevan | 736892d | 2017-07-14 15:20:58 -0600 | [diff] [blame] | 978 | } |
Girish Mahadevan | 6d97cbc | 2017-11-07 15:28:27 -0700 | [diff] [blame] | 979 | stop_tx_sequencer(uport); |
| 980 | } |
| 981 | |
| 982 | static void start_rx_sequencer(struct uart_port *uport) |
| 983 | { |
| 984 | unsigned int geni_s_irq_en; |
| 985 | unsigned int geni_m_irq_en; |
| 986 | unsigned int geni_status; |
| 987 | struct msm_geni_serial_port *port = GET_DEV_PORT(uport); |
| 988 | int ret; |
Girish Mahadevan | 7115f4e | 2017-03-15 15:18:34 -0600 | [diff] [blame] | 989 | |
| 990 | geni_status = geni_read_reg_nolog(uport->membase, SE_GENI_STATUS); |
| 991 | if (geni_status & S_GENI_CMD_ACTIVE) |
Girish Mahadevan | eecdd97 | 2017-08-22 17:58:08 -0600 | [diff] [blame] | 992 | msm_geni_serial_stop_rx(uport); |
| 993 | |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 994 | geni_setup_s_cmd(uport->membase, UART_START_READ, 0); |
Karthikeyan Ramasubramanian | 8ec770cf | 2017-04-20 15:29:09 -0600 | [diff] [blame] | 995 | |
| 996 | if (port->xfer_mode == FIFO_MODE) { |
| 997 | geni_s_irq_en = geni_read_reg_nolog(uport->membase, |
| 998 | SE_GENI_S_IRQ_EN); |
| 999 | geni_m_irq_en = geni_read_reg_nolog(uport->membase, |
| 1000 | SE_GENI_M_IRQ_EN); |
| 1001 | |
| 1002 | geni_s_irq_en |= S_RX_FIFO_WATERMARK_EN | S_RX_FIFO_LAST_EN; |
| 1003 | geni_m_irq_en |= M_RX_FIFO_WATERMARK_EN | M_RX_FIFO_LAST_EN; |
| 1004 | |
| 1005 | geni_write_reg_nolog(geni_s_irq_en, uport->membase, |
| 1006 | SE_GENI_S_IRQ_EN); |
| 1007 | geni_write_reg_nolog(geni_m_irq_en, uport->membase, |
| 1008 | SE_GENI_M_IRQ_EN); |
| 1009 | } else if (port->xfer_mode == SE_DMA) { |
Karthikeyan Ramasubramanian | 8ec770cf | 2017-04-20 15:29:09 -0600 | [diff] [blame] | 1010 | ret = geni_se_rx_dma_prep(port->wrapper_dev, uport->membase, |
| 1011 | port->rx_buf, DMA_RX_BUF_SIZE, &port->rx_dma); |
| 1012 | if (ret) { |
| 1013 | dev_err(uport->dev, "%s: RX Prep dma failed %d\n", |
| 1014 | __func__, ret); |
Girish Mahadevan | eecdd97 | 2017-08-22 17:58:08 -0600 | [diff] [blame] | 1015 | msm_geni_serial_stop_rx(uport); |
Girish Mahadevan | 6d97cbc | 2017-11-07 15:28:27 -0700 | [diff] [blame] | 1016 | goto exit_start_rx_sequencer; |
Karthikeyan Ramasubramanian | 8ec770cf | 2017-04-20 15:29:09 -0600 | [diff] [blame] | 1017 | } |
| 1018 | } |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 1019 | /* |
| 1020 | * Ensure the writes to the secondary sequencer and interrupt enables |
| 1021 | * go through. |
| 1022 | */ |
| 1023 | mb(); |
Girish Mahadevan | c2b9252 | 2017-08-17 22:41:32 -0600 | [diff] [blame] | 1024 | geni_status = geni_read_reg_nolog(uport->membase, SE_GENI_STATUS); |
Girish Mahadevan | 6d97cbc | 2017-11-07 15:28:27 -0700 | [diff] [blame] | 1025 | exit_start_rx_sequencer: |
Girish Mahadevan | c2b9252 | 2017-08-17 22:41:32 -0600 | [diff] [blame] | 1026 | IPC_LOG_MSG(port->ipc_log_misc, "%s 0x%x\n", __func__, geni_status); |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 1027 | } |
| 1028 | |
Girish Mahadevan | 6d97cbc | 2017-11-07 15:28:27 -0700 | [diff] [blame] | 1029 | static void msm_geni_serial_start_rx(struct uart_port *uport) |
| 1030 | { |
| 1031 | struct msm_geni_serial_port *port = GET_DEV_PORT(uport); |
| 1032 | |
| 1033 | if (!uart_console(uport) && device_pending_suspend(uport)) { |
| 1034 | dev_err(uport->dev, "%s.Device is suspended.\n", __func__); |
| 1035 | IPC_LOG_MSG(port->ipc_log_misc, |
| 1036 | "%s.Device is suspended.\n", __func__); |
| 1037 | return; |
| 1038 | } |
| 1039 | start_rx_sequencer(&port->uport); |
| 1040 | } |
| 1041 | |
| 1042 | |
Karthikeyan Ramasubramanian | b6be8c2 | 2017-08-23 16:34:00 -0600 | [diff] [blame] | 1043 | static void msm_geni_serial_rx_fsm_rst(struct uart_port *uport) |
| 1044 | { |
| 1045 | unsigned int rx_irq_en; |
| 1046 | int done = 0; |
| 1047 | int tries = 0; |
| 1048 | |
| 1049 | rx_irq_en = geni_read_reg_nolog(uport->membase, SE_DMA_RX_IRQ_EN); |
| 1050 | geni_write_reg_nolog(0, uport->membase, SE_DMA_RX_IRQ_EN_SET); |
| 1051 | geni_write_reg_nolog(1, uport->membase, SE_DMA_RX_FSM_RST); |
| 1052 | do { |
| 1053 | done = msm_geni_serial_poll_bit(uport, SE_DMA_RX_IRQ_STAT, |
| 1054 | RX_RESET_DONE, true); |
| 1055 | tries++; |
| 1056 | } while (!done && tries < 5); |
| 1057 | geni_write_reg_nolog(RX_DMA_DONE | RX_RESET_DONE, uport->membase, |
| 1058 | SE_DMA_RX_IRQ_CLR); |
| 1059 | geni_write_reg_nolog(rx_irq_en, uport->membase, SE_DMA_RX_IRQ_EN_SET); |
| 1060 | } |
| 1061 | |
Girish Mahadevan | 6d97cbc | 2017-11-07 15:28:27 -0700 | [diff] [blame] | 1062 | static void stop_rx_sequencer(struct uart_port *uport) |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 1063 | { |
| 1064 | unsigned int geni_s_irq_en; |
| 1065 | unsigned int geni_m_irq_en; |
| 1066 | unsigned int geni_status; |
Girish Mahadevan | 736892d | 2017-07-14 15:20:58 -0600 | [diff] [blame] | 1067 | struct msm_geni_serial_port *port = GET_DEV_PORT(uport); |
Girish Mahadevan | eecdd97 | 2017-08-22 17:58:08 -0600 | [diff] [blame] | 1068 | u32 irq_clear = S_CMD_DONE_EN; |
Girish Mahadevan | 6d97cbc | 2017-11-07 15:28:27 -0700 | [diff] [blame] | 1069 | bool done; |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 1070 | |
Girish Mahadevan | c2b9252 | 2017-08-17 22:41:32 -0600 | [diff] [blame] | 1071 | IPC_LOG_MSG(port->ipc_log_misc, "%s\n", __func__); |
Karthikeyan Ramasubramanian | 8ec770cf | 2017-04-20 15:29:09 -0600 | [diff] [blame] | 1072 | if (port->xfer_mode == FIFO_MODE) { |
| 1073 | geni_s_irq_en = geni_read_reg_nolog(uport->membase, |
| 1074 | SE_GENI_S_IRQ_EN); |
| 1075 | geni_m_irq_en = geni_read_reg_nolog(uport->membase, |
| 1076 | SE_GENI_M_IRQ_EN); |
| 1077 | geni_s_irq_en &= ~(S_RX_FIFO_WATERMARK_EN | S_RX_FIFO_LAST_EN); |
| 1078 | geni_m_irq_en &= ~(M_RX_FIFO_WATERMARK_EN | M_RX_FIFO_LAST_EN); |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 1079 | |
Karthikeyan Ramasubramanian | 8ec770cf | 2017-04-20 15:29:09 -0600 | [diff] [blame] | 1080 | geni_write_reg_nolog(geni_s_irq_en, uport->membase, |
| 1081 | SE_GENI_S_IRQ_EN); |
| 1082 | geni_write_reg_nolog(geni_m_irq_en, uport->membase, |
| 1083 | SE_GENI_M_IRQ_EN); |
Karthikeyan Ramasubramanian | 8ec770cf | 2017-04-20 15:29:09 -0600 | [diff] [blame] | 1084 | } |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 1085 | |
Girish Mahadevan | bd9b44f | 2017-04-11 13:11:10 -0600 | [diff] [blame] | 1086 | geni_status = geni_read_reg_nolog(uport->membase, SE_GENI_STATUS); |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 1087 | /* Possible stop rx is called multiple times. */ |
| 1088 | if (!(geni_status & S_GENI_CMD_ACTIVE)) |
Girish Mahadevan | 6d97cbc | 2017-11-07 15:28:27 -0700 | [diff] [blame] | 1089 | goto exit_rx_seq; |
Girish Mahadevan | eecdd97 | 2017-08-22 17:58:08 -0600 | [diff] [blame] | 1090 | geni_cancel_s_cmd(uport->membase); |
| 1091 | /* |
| 1092 | * Ensure that the cancel goes through before polling for the |
| 1093 | * cancel control bit. |
| 1094 | */ |
| 1095 | mb(); |
Girish Mahadevan | 6d97cbc | 2017-11-07 15:28:27 -0700 | [diff] [blame] | 1096 | done = msm_geni_serial_poll_bit(uport, SE_GENI_S_CMD_CTRL_REG, |
Girish Mahadevan | eecdd97 | 2017-08-22 17:58:08 -0600 | [diff] [blame] | 1097 | S_GENI_CMD_CANCEL, false); |
Girish Mahadevan | 6d97cbc | 2017-11-07 15:28:27 -0700 | [diff] [blame] | 1098 | geni_status = geni_read_reg_nolog(uport->membase, SE_GENI_STATUS); |
| 1099 | if (!done) |
| 1100 | IPC_LOG_MSG(port->ipc_log_misc, "%s Cancel fail 0x%x\n", |
| 1101 | __func__, geni_status); |
| 1102 | |
Girish Mahadevan | eecdd97 | 2017-08-22 17:58:08 -0600 | [diff] [blame] | 1103 | geni_write_reg_nolog(irq_clear, uport->membase, SE_GENI_S_IRQ_CLEAR); |
| 1104 | if ((geni_status & S_GENI_CMD_ACTIVE)) |
| 1105 | msm_geni_serial_abort_rx(uport); |
Girish Mahadevan | 6d97cbc | 2017-11-07 15:28:27 -0700 | [diff] [blame] | 1106 | exit_rx_seq: |
| 1107 | if (port->xfer_mode == SE_DMA && port->rx_dma) { |
| 1108 | msm_geni_serial_rx_fsm_rst(uport); |
| 1109 | geni_se_rx_dma_unprep(port->wrapper_dev, port->rx_dma, |
| 1110 | DMA_RX_BUF_SIZE); |
| 1111 | port->rx_dma = (dma_addr_t)NULL; |
| 1112 | } |
| 1113 | } |
| 1114 | |
| 1115 | static void msm_geni_serial_stop_rx(struct uart_port *uport) |
| 1116 | { |
| 1117 | struct msm_geni_serial_port *port = GET_DEV_PORT(uport); |
| 1118 | |
| 1119 | if (!uart_console(uport) && device_pending_suspend(uport)) { |
| 1120 | IPC_LOG_MSG(port->ipc_log_misc, |
| 1121 | "%s.Device is suspended.\n", __func__); |
| 1122 | return; |
| 1123 | } |
| 1124 | stop_rx_sequencer(uport); |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 1125 | } |
| 1126 | |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 1127 | static int handle_rx_hs(struct uart_port *uport, |
| 1128 | unsigned int rx_fifo_wc, |
| 1129 | unsigned int rx_last_byte_valid, |
Karthikeyan Ramasubramanian | e67fd2e | 2017-08-30 17:16:58 -0600 | [diff] [blame] | 1130 | unsigned int rx_last, |
| 1131 | bool drop_rx) |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 1132 | { |
| 1133 | unsigned char *rx_char; |
| 1134 | struct tty_port *tport; |
| 1135 | struct msm_geni_serial_port *msm_port = GET_DEV_PORT(uport); |
| 1136 | int ret; |
| 1137 | int rx_bytes = 0; |
| 1138 | |
| 1139 | rx_bytes = (msm_port->tx_fifo_width * (rx_fifo_wc - 1)) >> 3; |
| 1140 | rx_bytes += ((rx_last && rx_last_byte_valid) ? |
| 1141 | rx_last_byte_valid : msm_port->tx_fifo_width >> 3); |
| 1142 | |
| 1143 | tport = &uport->state->port; |
| 1144 | ioread32_rep((uport->membase + SE_GENI_RX_FIFOn), msm_port->rx_fifo, |
| 1145 | rx_fifo_wc); |
Karthikeyan Ramasubramanian | e67fd2e | 2017-08-30 17:16:58 -0600 | [diff] [blame] | 1146 | if (drop_rx) |
| 1147 | return 0; |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 1148 | |
| 1149 | rx_char = (unsigned char *)msm_port->rx_fifo; |
| 1150 | ret = tty_insert_flip_string(tport, rx_char, rx_bytes); |
| 1151 | if (ret != rx_bytes) { |
| 1152 | dev_err(uport->dev, "%s: ret %d rx_bytes %d\n", __func__, |
| 1153 | ret, rx_bytes); |
| 1154 | WARN_ON(1); |
| 1155 | } |
| 1156 | uport->icount.rx += ret; |
| 1157 | tty_flip_buffer_push(tport); |
Girish Mahadevan | 7115f4e | 2017-03-15 15:18:34 -0600 | [diff] [blame] | 1158 | dump_ipc(msm_port->ipc_log_rx, "Rx", (char *)msm_port->rx_fifo, 0, |
| 1159 | rx_bytes); |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 1160 | return ret; |
| 1161 | } |
| 1162 | |
Karthikeyan Ramasubramanian | e67fd2e | 2017-08-30 17:16:58 -0600 | [diff] [blame] | 1163 | static int msm_geni_serial_handle_rx(struct uart_port *uport, bool drop_rx) |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 1164 | { |
| 1165 | int ret = 0; |
| 1166 | unsigned int rx_fifo_status; |
| 1167 | unsigned int rx_fifo_wc = 0; |
| 1168 | unsigned int rx_last_byte_valid = 0; |
| 1169 | unsigned int rx_last = 0; |
| 1170 | struct tty_port *tport; |
Girish Mahadevan | 7115f4e | 2017-03-15 15:18:34 -0600 | [diff] [blame] | 1171 | struct msm_geni_serial_port *port = GET_DEV_PORT(uport); |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 1172 | |
| 1173 | tport = &uport->state->port; |
Girish Mahadevan | bd9b44f | 2017-04-11 13:11:10 -0600 | [diff] [blame] | 1174 | rx_fifo_status = geni_read_reg_nolog(uport->membase, |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 1175 | SE_GENI_RX_FIFO_STATUS); |
| 1176 | rx_fifo_wc = rx_fifo_status & RX_FIFO_WC_MSK; |
| 1177 | rx_last_byte_valid = ((rx_fifo_status & RX_LAST_BYTE_VALID_MSK) >> |
| 1178 | RX_LAST_BYTE_VALID_SHFT); |
| 1179 | rx_last = rx_fifo_status & RX_LAST; |
| 1180 | if (rx_fifo_wc) |
Girish Mahadevan | 7115f4e | 2017-03-15 15:18:34 -0600 | [diff] [blame] | 1181 | port->handle_rx(uport, rx_fifo_wc, rx_last_byte_valid, |
Karthikeyan Ramasubramanian | e67fd2e | 2017-08-30 17:16:58 -0600 | [diff] [blame] | 1182 | rx_last, drop_rx); |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 1183 | return ret; |
| 1184 | } |
| 1185 | |
| 1186 | static int msm_geni_serial_handle_tx(struct uart_port *uport) |
| 1187 | { |
| 1188 | int ret = 0; |
| 1189 | struct msm_geni_serial_port *msm_port = GET_DEV_PORT(uport); |
| 1190 | struct circ_buf *xmit = &uport->state->xmit; |
| 1191 | unsigned int avail_fifo_bytes = 0; |
| 1192 | unsigned int bytes_remaining = 0; |
| 1193 | int i = 0; |
| 1194 | unsigned int tx_fifo_status; |
| 1195 | unsigned int xmit_size; |
Girish Mahadevan | b1ab172 | 2017-04-27 16:39:11 -0600 | [diff] [blame] | 1196 | unsigned int fifo_width_bytes = |
| 1197 | (uart_console(uport) ? 1 : (msm_port->tx_fifo_width >> 3)); |
Karthikeyan Ramasubramanian | 61074fa | 2017-05-24 00:18:34 -0600 | [diff] [blame] | 1198 | unsigned int geni_m_irq_en; |
Karthikeyan Ramasubramanian | 967b6fb | 2017-10-25 13:59:28 -0600 | [diff] [blame] | 1199 | int temp_tail = 0; |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 1200 | |
Karthikeyan Ramasubramanian | 967b6fb | 2017-10-25 13:59:28 -0600 | [diff] [blame] | 1201 | xmit_size = uart_circ_chars_pending(xmit); |
Girish Mahadevan | bd9b44f | 2017-04-11 13:11:10 -0600 | [diff] [blame] | 1202 | tx_fifo_status = geni_read_reg_nolog(uport->membase, |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 1203 | SE_GENI_TX_FIFO_STATUS); |
Karthikeyan Ramasubramanian | 967b6fb | 2017-10-25 13:59:28 -0600 | [diff] [blame] | 1204 | /* Both FIFO and framework buffer are drained */ |
| 1205 | if ((xmit_size == msm_port->xmit_size) && !tx_fifo_status) { |
Girish Mahadevan | 51fc9d4 | 2017-10-12 18:37:52 -0600 | [diff] [blame] | 1206 | /* |
| 1207 | * This will balance out the power vote put in during start_tx |
| 1208 | * allowing the device to suspend. |
| 1209 | */ |
| 1210 | if (!uart_console(uport)) { |
| 1211 | IPC_LOG_MSG(msm_port->ipc_log_misc, |
| 1212 | "%s.Power Off.\n", __func__); |
| 1213 | msm_geni_serial_power_off(uport); |
| 1214 | } |
Karthikeyan Ramasubramanian | 967b6fb | 2017-10-25 13:59:28 -0600 | [diff] [blame] | 1215 | msm_port->xmit_size = 0; |
| 1216 | uart_circ_clear(xmit); |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 1217 | msm_geni_serial_stop_tx(uport); |
| 1218 | goto exit_handle_tx; |
| 1219 | } |
Karthikeyan Ramasubramanian | 967b6fb | 2017-10-25 13:59:28 -0600 | [diff] [blame] | 1220 | xmit_size -= msm_port->xmit_size; |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 1221 | |
Karthikeyan Ramasubramanian | 61074fa | 2017-05-24 00:18:34 -0600 | [diff] [blame] | 1222 | if (!uart_console(uport)) { |
| 1223 | geni_m_irq_en = geni_read_reg_nolog(uport->membase, |
| 1224 | SE_GENI_M_IRQ_EN); |
| 1225 | geni_m_irq_en &= ~(M_TX_FIFO_WATERMARK_EN); |
| 1226 | geni_write_reg_nolog(0, uport->membase, |
| 1227 | SE_GENI_TX_WATERMARK_REG); |
| 1228 | geni_write_reg_nolog(geni_m_irq_en, uport->membase, |
| 1229 | SE_GENI_M_IRQ_EN); |
| 1230 | } |
| 1231 | |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 1232 | avail_fifo_bytes = (msm_port->tx_fifo_depth - msm_port->tx_wm) * |
| 1233 | fifo_width_bytes; |
Karthikeyan Ramasubramanian | 967b6fb | 2017-10-25 13:59:28 -0600 | [diff] [blame] | 1234 | temp_tail = (xmit->tail + msm_port->xmit_size) & (UART_XMIT_SIZE - 1); |
| 1235 | if (xmit_size > (UART_XMIT_SIZE - temp_tail)) |
| 1236 | xmit_size = (UART_XMIT_SIZE - temp_tail); |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 1237 | if (xmit_size > avail_fifo_bytes) |
| 1238 | xmit_size = avail_fifo_bytes; |
| 1239 | |
| 1240 | if (!xmit_size) |
| 1241 | goto exit_handle_tx; |
| 1242 | |
| 1243 | msm_geni_serial_setup_tx(uport, xmit_size); |
| 1244 | |
| 1245 | bytes_remaining = xmit_size; |
Karthikeyan Ramasubramanian | 967b6fb | 2017-10-25 13:59:28 -0600 | [diff] [blame] | 1246 | dump_ipc(msm_port->ipc_log_tx, "Tx", (char *)&xmit->buf[temp_tail], 0, |
Girish Mahadevan | 7115f4e | 2017-03-15 15:18:34 -0600 | [diff] [blame] | 1247 | xmit_size); |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 1248 | while (i < xmit_size) { |
| 1249 | unsigned int tx_bytes; |
| 1250 | unsigned int buf = 0; |
| 1251 | int c; |
| 1252 | |
| 1253 | tx_bytes = ((bytes_remaining < fifo_width_bytes) ? |
| 1254 | bytes_remaining : fifo_width_bytes); |
| 1255 | |
| 1256 | for (c = 0; c < tx_bytes ; c++) |
Karthikeyan Ramasubramanian | 8ec770cf | 2017-04-20 15:29:09 -0600 | [diff] [blame] | 1257 | buf |= (xmit->buf[temp_tail + c] << (c * 8)); |
Girish Mahadevan | bd9b44f | 2017-04-11 13:11:10 -0600 | [diff] [blame] | 1258 | geni_write_reg_nolog(buf, uport->membase, SE_GENI_TX_FIFOn); |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 1259 | i += tx_bytes; |
Karthikeyan Ramasubramanian | 967b6fb | 2017-10-25 13:59:28 -0600 | [diff] [blame] | 1260 | temp_tail = (temp_tail + tx_bytes) & (UART_XMIT_SIZE - 1); |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 1261 | uport->icount.tx += tx_bytes; |
| 1262 | bytes_remaining -= tx_bytes; |
| 1263 | /* Ensure FIFO write goes through */ |
| 1264 | wmb(); |
| 1265 | } |
Karthikeyan Ramasubramanian | 967b6fb | 2017-10-25 13:59:28 -0600 | [diff] [blame] | 1266 | if (uart_console(uport)) |
Karthikeyan Ramasubramanian | 40cdf08 | 2017-08-28 13:18:00 -0600 | [diff] [blame] | 1267 | msm_geni_serial_poll_cancel_tx(uport); |
Karthikeyan Ramasubramanian | 967b6fb | 2017-10-25 13:59:28 -0600 | [diff] [blame] | 1268 | msm_port->xmit_size += xmit_size; |
Karthikeyan Ramasubramanian | e67fd2e | 2017-08-30 17:16:58 -0600 | [diff] [blame] | 1269 | exit_handle_tx: |
Girish Mahadevan | 51fc9d4 | 2017-10-12 18:37:52 -0600 | [diff] [blame] | 1270 | uart_write_wakeup(uport); |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 1271 | return ret; |
| 1272 | } |
| 1273 | |
Karthikeyan Ramasubramanian | e67fd2e | 2017-08-30 17:16:58 -0600 | [diff] [blame] | 1274 | static int msm_geni_serial_handle_dma_rx(struct uart_port *uport, bool drop_rx) |
Karthikeyan Ramasubramanian | 8ec770cf | 2017-04-20 15:29:09 -0600 | [diff] [blame] | 1275 | { |
| 1276 | struct msm_geni_serial_port *msm_port = GET_DEV_PORT(uport); |
| 1277 | unsigned int rx_bytes = 0; |
| 1278 | struct tty_port *tport; |
| 1279 | int ret; |
Karthikeyan Ramasubramanian | 83eb13d | 2017-08-14 12:40:44 -0600 | [diff] [blame] | 1280 | unsigned int geni_status; |
| 1281 | |
| 1282 | geni_status = geni_read_reg_nolog(uport->membase, SE_GENI_STATUS); |
| 1283 | /* Possible stop rx is called */ |
| 1284 | if (!(geni_status & S_GENI_CMD_ACTIVE)) |
| 1285 | return 0; |
Karthikeyan Ramasubramanian | 8ec770cf | 2017-04-20 15:29:09 -0600 | [diff] [blame] | 1286 | |
| 1287 | geni_se_rx_dma_unprep(msm_port->wrapper_dev, msm_port->rx_dma, |
| 1288 | DMA_RX_BUF_SIZE); |
| 1289 | rx_bytes = geni_read_reg_nolog(uport->membase, SE_DMA_RX_LEN_IN); |
Karthikeyan Ramasubramanian | b6be8c2 | 2017-08-23 16:34:00 -0600 | [diff] [blame] | 1290 | if (unlikely(!msm_port->rx_buf)) { |
| 1291 | IPC_LOG_MSG(msm_port->ipc_log_rx, "%s: NULL Rx_buf\n", |
| 1292 | __func__); |
Karthikeyan Ramasubramanian | 83eb13d | 2017-08-14 12:40:44 -0600 | [diff] [blame] | 1293 | return 0; |
| 1294 | } |
Karthikeyan Ramasubramanian | b6be8c2 | 2017-08-23 16:34:00 -0600 | [diff] [blame] | 1295 | if (unlikely(!rx_bytes)) { |
| 1296 | IPC_LOG_MSG(msm_port->ipc_log_rx, "%s: Size %d\n", |
| 1297 | __func__, rx_bytes); |
| 1298 | goto exit_handle_dma_rx; |
| 1299 | } |
Karthikeyan Ramasubramanian | e67fd2e | 2017-08-30 17:16:58 -0600 | [diff] [blame] | 1300 | if (drop_rx) |
| 1301 | goto exit_handle_dma_rx; |
Karthikeyan Ramasubramanian | 8ec770cf | 2017-04-20 15:29:09 -0600 | [diff] [blame] | 1302 | |
| 1303 | tport = &uport->state->port; |
| 1304 | ret = tty_insert_flip_string(tport, (unsigned char *)(msm_port->rx_buf), |
| 1305 | rx_bytes); |
| 1306 | if (ret != rx_bytes) { |
| 1307 | dev_err(uport->dev, "%s: ret %d rx_bytes %d\n", __func__, |
| 1308 | ret, rx_bytes); |
| 1309 | WARN_ON(1); |
| 1310 | } |
| 1311 | uport->icount.rx += ret; |
| 1312 | tty_flip_buffer_push(tport); |
| 1313 | dump_ipc(msm_port->ipc_log_rx, "DMA Rx", (char *)msm_port->rx_buf, 0, |
| 1314 | rx_bytes); |
Karthikeyan Ramasubramanian | b6be8c2 | 2017-08-23 16:34:00 -0600 | [diff] [blame] | 1315 | exit_handle_dma_rx: |
Karthikeyan Ramasubramanian | 8ec770cf | 2017-04-20 15:29:09 -0600 | [diff] [blame] | 1316 | ret = geni_se_rx_dma_prep(msm_port->wrapper_dev, uport->membase, |
| 1317 | msm_port->rx_buf, DMA_RX_BUF_SIZE, &msm_port->rx_dma); |
| 1318 | if (ret) |
| 1319 | IPC_LOG_MSG(msm_port->ipc_log_rx, "%s: %d\n", __func__, ret); |
| 1320 | return ret; |
| 1321 | } |
| 1322 | |
| 1323 | static int msm_geni_serial_handle_dma_tx(struct uart_port *uport) |
| 1324 | { |
| 1325 | struct msm_geni_serial_port *msm_port = GET_DEV_PORT(uport); |
| 1326 | struct circ_buf *xmit = &uport->state->xmit; |
| 1327 | |
| 1328 | xmit->tail = (xmit->tail + msm_port->xmit_size) & (UART_XMIT_SIZE - 1); |
| 1329 | geni_se_tx_dma_unprep(msm_port->wrapper_dev, msm_port->tx_dma, |
| 1330 | msm_port->xmit_size); |
| 1331 | uport->icount.tx += msm_port->xmit_size; |
| 1332 | msm_port->tx_dma = (dma_addr_t)NULL; |
| 1333 | msm_port->xmit_size = 0; |
| 1334 | |
| 1335 | if (!uart_circ_empty(xmit)) |
| 1336 | msm_geni_serial_prep_dma_tx(uport); |
Girish Mahadevan | 51fc9d4 | 2017-10-12 18:37:52 -0600 | [diff] [blame] | 1337 | else { |
| 1338 | /* |
| 1339 | * This will balance out the power vote put in during start_tx |
| 1340 | * allowing the device to suspend. |
| 1341 | */ |
| 1342 | if (!uart_console(uport)) { |
| 1343 | IPC_LOG_MSG(msm_port->ipc_log_misc, |
| 1344 | "%s.Power Off.\n", __func__); |
| 1345 | msm_geni_serial_power_off(uport); |
| 1346 | } |
Karthikeyan Ramasubramanian | 8ec770cf | 2017-04-20 15:29:09 -0600 | [diff] [blame] | 1347 | uart_write_wakeup(uport); |
Girish Mahadevan | 51fc9d4 | 2017-10-12 18:37:52 -0600 | [diff] [blame] | 1348 | } |
Karthikeyan Ramasubramanian | 8ec770cf | 2017-04-20 15:29:09 -0600 | [diff] [blame] | 1349 | return 0; |
| 1350 | } |
| 1351 | |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 1352 | static irqreturn_t msm_geni_serial_isr(int isr, void *dev) |
| 1353 | { |
| 1354 | unsigned int m_irq_status; |
| 1355 | unsigned int s_irq_status; |
Karthikeyan Ramasubramanian | 8ec770cf | 2017-04-20 15:29:09 -0600 | [diff] [blame] | 1356 | unsigned int dma; |
| 1357 | unsigned int dma_tx_status; |
| 1358 | unsigned int dma_rx_status; |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 1359 | struct uart_port *uport = dev; |
| 1360 | unsigned long flags; |
Karthikeyan Ramasubramanian | 61074fa | 2017-05-24 00:18:34 -0600 | [diff] [blame] | 1361 | unsigned int m_irq_en; |
Girish Mahadevan | 736892d | 2017-07-14 15:20:58 -0600 | [diff] [blame] | 1362 | struct msm_geni_serial_port *msm_port = GET_DEV_PORT(uport); |
Karthikeyan Ramasubramanian | 55a1c6d | 2017-11-14 13:10:46 -0700 | [diff] [blame] | 1363 | struct tty_port *tport = &uport->state->port; |
Karthikeyan Ramasubramanian | e67fd2e | 2017-08-30 17:16:58 -0600 | [diff] [blame] | 1364 | bool drop_rx = false; |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 1365 | |
| 1366 | spin_lock_irqsave(&uport->lock, flags); |
Girish Mahadevan | 7115f4e | 2017-03-15 15:18:34 -0600 | [diff] [blame] | 1367 | if (uart_console(uport) && uport->suspended) |
| 1368 | goto exit_geni_serial_isr; |
Girish Mahadevan | 5db3df7 | 2017-10-18 11:02:56 -0600 | [diff] [blame] | 1369 | if (!uart_console(uport) && pm_runtime_status_suspended(uport->dev)) { |
Girish Mahadevan | 736892d | 2017-07-14 15:20:58 -0600 | [diff] [blame] | 1370 | dev_err(uport->dev, "%s.Device is suspended.\n", __func__); |
| 1371 | IPC_LOG_MSG(msm_port->ipc_log_misc, |
| 1372 | "%s.Device is suspended.\n", __func__); |
Girish Mahadevan | 7115f4e | 2017-03-15 15:18:34 -0600 | [diff] [blame] | 1373 | goto exit_geni_serial_isr; |
Girish Mahadevan | 736892d | 2017-07-14 15:20:58 -0600 | [diff] [blame] | 1374 | } |
Girish Mahadevan | bd9b44f | 2017-04-11 13:11:10 -0600 | [diff] [blame] | 1375 | m_irq_status = geni_read_reg_nolog(uport->membase, |
Girish Mahadevan | 7115f4e | 2017-03-15 15:18:34 -0600 | [diff] [blame] | 1376 | SE_GENI_M_IRQ_STATUS); |
Girish Mahadevan | bd9b44f | 2017-04-11 13:11:10 -0600 | [diff] [blame] | 1377 | s_irq_status = geni_read_reg_nolog(uport->membase, |
Girish Mahadevan | 7115f4e | 2017-03-15 15:18:34 -0600 | [diff] [blame] | 1378 | SE_GENI_S_IRQ_STATUS); |
Karthikeyan Ramasubramanian | 61074fa | 2017-05-24 00:18:34 -0600 | [diff] [blame] | 1379 | m_irq_en = geni_read_reg_nolog(uport->membase, SE_GENI_M_IRQ_EN); |
Karthikeyan Ramasubramanian | 8ec770cf | 2017-04-20 15:29:09 -0600 | [diff] [blame] | 1380 | dma = geni_read_reg_nolog(uport->membase, SE_GENI_DMA_MODE_EN); |
| 1381 | dma_tx_status = geni_read_reg_nolog(uport->membase, SE_DMA_TX_IRQ_STAT); |
| 1382 | dma_rx_status = geni_read_reg_nolog(uport->membase, SE_DMA_RX_IRQ_STAT); |
| 1383 | |
| 1384 | geni_write_reg_nolog(m_irq_status, uport->membase, SE_GENI_M_IRQ_CLEAR); |
| 1385 | geni_write_reg_nolog(s_irq_status, uport->membase, SE_GENI_S_IRQ_CLEAR); |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 1386 | |
| 1387 | if ((m_irq_status & M_ILLEGAL_CMD_EN)) { |
| 1388 | WARN_ON(1); |
| 1389 | goto exit_geni_serial_isr; |
| 1390 | } |
| 1391 | |
Karthikeyan Ramasubramanian | 20cf4fa | 2017-09-18 17:07:58 -0600 | [diff] [blame] | 1392 | if (s_irq_status & S_RX_FIFO_WR_ERR_EN) { |
Karthikeyan Ramasubramanian | 55a1c6d | 2017-11-14 13:10:46 -0700 | [diff] [blame] | 1393 | uport->icount.overrun++; |
| 1394 | tty_insert_flip_char(tport, 0, TTY_OVERRUN); |
Karthikeyan Ramasubramanian | 20cf4fa | 2017-09-18 17:07:58 -0600 | [diff] [blame] | 1395 | IPC_LOG_MSG(msm_port->ipc_log_misc, |
| 1396 | "%s.sirq 0x%x buf_overrun:%d\n", |
| 1397 | __func__, s_irq_status, uport->icount.buf_overrun); |
| 1398 | } |
| 1399 | |
Karthikeyan Ramasubramanian | 8ec770cf | 2017-04-20 15:29:09 -0600 | [diff] [blame] | 1400 | if (!dma) { |
Karthikeyan Ramasubramanian | 8ec770cf | 2017-04-20 15:29:09 -0600 | [diff] [blame] | 1401 | if ((m_irq_status & m_irq_en) & |
| 1402 | (M_TX_FIFO_WATERMARK_EN | M_CMD_DONE_EN)) |
| 1403 | msm_geni_serial_handle_tx(uport); |
Karthikeyan Ramasubramanian | e67fd2e | 2017-08-30 17:16:58 -0600 | [diff] [blame] | 1404 | |
| 1405 | if ((s_irq_status & S_GP_IRQ_0_EN) || |
| 1406 | (s_irq_status & S_GP_IRQ_1_EN)) { |
| 1407 | if (s_irq_status & S_GP_IRQ_0_EN) |
| 1408 | uport->icount.parity++; |
| 1409 | IPC_LOG_MSG(msm_port->ipc_log_misc, |
| 1410 | "%s.sirq 0x%x parity:%d\n", |
| 1411 | __func__, s_irq_status, uport->icount.parity); |
| 1412 | drop_rx = true; |
| 1413 | } else if ((s_irq_status & S_GP_IRQ_2_EN) || |
| 1414 | (s_irq_status & S_GP_IRQ_3_EN)) { |
| 1415 | uport->icount.brk++; |
| 1416 | IPC_LOG_MSG(msm_port->ipc_log_misc, |
| 1417 | "%s.sirq 0x%x break:%d\n", |
| 1418 | __func__, s_irq_status, uport->icount.brk); |
| 1419 | } |
| 1420 | |
| 1421 | if ((s_irq_status & S_RX_FIFO_WATERMARK_EN) || |
| 1422 | (s_irq_status & S_RX_FIFO_LAST_EN)) |
| 1423 | msm_geni_serial_handle_rx(uport, drop_rx); |
Karthikeyan Ramasubramanian | 8ec770cf | 2017-04-20 15:29:09 -0600 | [diff] [blame] | 1424 | } else { |
| 1425 | if (dma_tx_status) { |
| 1426 | geni_write_reg_nolog(dma_tx_status, uport->membase, |
| 1427 | SE_DMA_TX_IRQ_CLR); |
| 1428 | if (dma_tx_status & TX_DMA_DONE) |
| 1429 | msm_geni_serial_handle_dma_tx(uport); |
| 1430 | } |
| 1431 | |
| 1432 | if (dma_rx_status) { |
| 1433 | geni_write_reg_nolog(dma_rx_status, uport->membase, |
| 1434 | SE_DMA_RX_IRQ_CLR); |
Girish Mahadevan | c2b9252 | 2017-08-17 22:41:32 -0600 | [diff] [blame] | 1435 | if (dma_rx_status & RX_RESET_DONE) { |
| 1436 | IPC_LOG_MSG(msm_port->ipc_log_misc, |
| 1437 | "%s.Reset done. 0x%x.\n", |
| 1438 | __func__, dma_rx_status); |
| 1439 | goto exit_geni_serial_isr; |
| 1440 | } |
| 1441 | if (dma_rx_status & UART_DMA_RX_ERRS) { |
Karthikeyan Ramasubramanian | e67fd2e | 2017-08-30 17:16:58 -0600 | [diff] [blame] | 1442 | if (dma_rx_status & UART_DMA_RX_PARITY_ERR) |
| 1443 | uport->icount.parity++; |
Girish Mahadevan | c2b9252 | 2017-08-17 22:41:32 -0600 | [diff] [blame] | 1444 | IPC_LOG_MSG(msm_port->ipc_log_misc, |
Karthikeyan Ramasubramanian | e67fd2e | 2017-08-30 17:16:58 -0600 | [diff] [blame] | 1445 | "%s.Rx Errors. 0x%x parity:%d\n", |
| 1446 | __func__, dma_rx_status, |
| 1447 | uport->icount.parity); |
| 1448 | drop_rx = true; |
| 1449 | } else if (dma_rx_status & UART_DMA_RX_BREAK) { |
| 1450 | uport->icount.brk++; |
| 1451 | IPC_LOG_MSG(msm_port->ipc_log_misc, |
| 1452 | "%s.Rx Errors. 0x%x break:%d\n", |
| 1453 | __func__, dma_rx_status, |
| 1454 | uport->icount.brk); |
Girish Mahadevan | c2b9252 | 2017-08-17 22:41:32 -0600 | [diff] [blame] | 1455 | } |
Karthikeyan Ramasubramanian | 8ec770cf | 2017-04-20 15:29:09 -0600 | [diff] [blame] | 1456 | if (dma_rx_status & RX_DMA_DONE) |
Karthikeyan Ramasubramanian | e67fd2e | 2017-08-30 17:16:58 -0600 | [diff] [blame] | 1457 | msm_geni_serial_handle_dma_rx(uport, drop_rx); |
Karthikeyan Ramasubramanian | 8ec770cf | 2017-04-20 15:29:09 -0600 | [diff] [blame] | 1458 | } |
| 1459 | } |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 1460 | |
| 1461 | exit_geni_serial_isr: |
| 1462 | spin_unlock_irqrestore(&uport->lock, flags); |
| 1463 | return IRQ_HANDLED; |
| 1464 | } |
| 1465 | |
Girish Mahadevan | 7115f4e | 2017-03-15 15:18:34 -0600 | [diff] [blame] | 1466 | static irqreturn_t msm_geni_wakeup_isr(int isr, void *dev) |
| 1467 | { |
| 1468 | struct uart_port *uport = dev; |
| 1469 | struct msm_geni_serial_port *port = GET_DEV_PORT(uport); |
| 1470 | struct tty_struct *tty; |
| 1471 | unsigned long flags; |
| 1472 | |
| 1473 | spin_lock_irqsave(&uport->lock, flags); |
Girish Mahadevan | 736892d | 2017-07-14 15:20:58 -0600 | [diff] [blame] | 1474 | IPC_LOG_MSG(port->ipc_log_rx, "%s: Edge-Count %d\n", __func__, |
| 1475 | port->edge_count); |
| 1476 | if (port->wakeup_byte && (port->edge_count == 2)) { |
Girish Mahadevan | 7115f4e | 2017-03-15 15:18:34 -0600 | [diff] [blame] | 1477 | tty = uport->state->port.tty; |
| 1478 | tty_insert_flip_char(tty->port, port->wakeup_byte, TTY_NORMAL); |
| 1479 | IPC_LOG_MSG(port->ipc_log_rx, "%s: Inject 0x%x\n", |
| 1480 | __func__, port->wakeup_byte); |
Girish Mahadevan | 736892d | 2017-07-14 15:20:58 -0600 | [diff] [blame] | 1481 | port->edge_count = 0; |
Girish Mahadevan | 7115f4e | 2017-03-15 15:18:34 -0600 | [diff] [blame] | 1482 | tty_flip_buffer_push(tty->port); |
Girish Mahadevan | 736892d | 2017-07-14 15:20:58 -0600 | [diff] [blame] | 1483 | __pm_wakeup_event(&port->geni_wake, WAKEBYTE_TIMEOUT_MSEC); |
| 1484 | } else if (port->edge_count < 2) { |
| 1485 | port->edge_count++; |
Girish Mahadevan | 7115f4e | 2017-03-15 15:18:34 -0600 | [diff] [blame] | 1486 | } |
Girish Mahadevan | 7115f4e | 2017-03-15 15:18:34 -0600 | [diff] [blame] | 1487 | spin_unlock_irqrestore(&uport->lock, flags); |
| 1488 | return IRQ_HANDLED; |
| 1489 | } |
| 1490 | |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 1491 | static int get_tx_fifo_size(struct msm_geni_serial_port *port) |
| 1492 | { |
| 1493 | struct uart_port *uport; |
| 1494 | |
| 1495 | if (!port) |
| 1496 | return -ENODEV; |
| 1497 | |
| 1498 | uport = &port->uport; |
| 1499 | port->tx_fifo_depth = get_tx_fifo_depth(uport->membase); |
| 1500 | if (!port->tx_fifo_depth) { |
| 1501 | dev_err(uport->dev, "%s:Invalid TX FIFO depth read\n", |
| 1502 | __func__); |
| 1503 | return -ENXIO; |
| 1504 | } |
| 1505 | |
| 1506 | port->tx_fifo_width = get_tx_fifo_width(uport->membase); |
| 1507 | if (!port->tx_fifo_width) { |
| 1508 | dev_err(uport->dev, "%s:Invalid TX FIFO width read\n", |
Karthikeyan Ramasubramanian | 56351b6 | 2017-04-21 15:11:03 -0600 | [diff] [blame] | 1509 | __func__); |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 1510 | return -ENXIO; |
| 1511 | } |
| 1512 | |
| 1513 | port->rx_fifo_depth = get_rx_fifo_depth(uport->membase); |
| 1514 | if (!port->rx_fifo_depth) { |
| 1515 | dev_err(uport->dev, "%s:Invalid RX FIFO depth read\n", |
| 1516 | __func__); |
| 1517 | return -ENXIO; |
| 1518 | } |
| 1519 | |
| 1520 | uport->fifosize = |
| 1521 | ((port->tx_fifo_depth * port->tx_fifo_width) >> 3); |
| 1522 | return 0; |
| 1523 | } |
| 1524 | |
| 1525 | static void set_rfr_wm(struct msm_geni_serial_port *port) |
| 1526 | { |
| 1527 | /* |
| 1528 | * Set RFR (Flow off) to FIFO_DEPTH - 2. |
| 1529 | * RX WM level at 50% RX_FIFO_DEPTH. |
| 1530 | * TX WM level at 10% TX_FIFO_DEPTH. |
| 1531 | */ |
| 1532 | port->rx_rfr = port->rx_fifo_depth - 2; |
Karthikeyan Ramasubramanian | 55a1c6d | 2017-11-14 13:10:46 -0700 | [diff] [blame] | 1533 | if (!uart_console(&port->uport)) |
| 1534 | port->rx_wm = port->rx_fifo_depth >> 1; |
| 1535 | else |
| 1536 | port->rx_wm = UART_CONSOLE_RX_WM; |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 1537 | port->tx_wm = 2; |
| 1538 | } |
| 1539 | |
| 1540 | static void msm_geni_serial_shutdown(struct uart_port *uport) |
| 1541 | { |
| 1542 | struct msm_geni_serial_port *msm_port = GET_DEV_PORT(uport); |
Karthikeyan Ramasubramanian | f5fb743 | 2017-05-24 21:59:55 -0600 | [diff] [blame] | 1543 | unsigned long flags; |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 1544 | |
Karthikeyan Ramasubramanian | f5fb743 | 2017-05-24 21:59:55 -0600 | [diff] [blame] | 1545 | /* Stop the console before stopping the current tx */ |
Girish Mahadevan | 736892d | 2017-07-14 15:20:58 -0600 | [diff] [blame] | 1546 | if (uart_console(uport)) { |
Karthikeyan Ramasubramanian | f5fb743 | 2017-05-24 21:59:55 -0600 | [diff] [blame] | 1547 | console_stop(uport->cons); |
Girish Mahadevan | 736892d | 2017-07-14 15:20:58 -0600 | [diff] [blame] | 1548 | } else { |
| 1549 | msm_geni_serial_power_on(uport); |
| 1550 | wait_for_transfers_inflight(uport); |
| 1551 | } |
Karthikeyan Ramasubramanian | f5fb743 | 2017-05-24 21:59:55 -0600 | [diff] [blame] | 1552 | |
Karthikeyan Ramasubramanian | 83eb13d | 2017-08-14 12:40:44 -0600 | [diff] [blame] | 1553 | disable_irq(uport->irq); |
Karthikeyan Ramasubramanian | 53157c5 | 2017-08-11 17:06:24 -0600 | [diff] [blame] | 1554 | free_irq(uport->irq, uport); |
Karthikeyan Ramasubramanian | f5fb743 | 2017-05-24 21:59:55 -0600 | [diff] [blame] | 1555 | spin_lock_irqsave(&uport->lock, flags); |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 1556 | msm_geni_serial_stop_tx(uport); |
| 1557 | msm_geni_serial_stop_rx(uport); |
Karthikeyan Ramasubramanian | f5fb743 | 2017-05-24 21:59:55 -0600 | [diff] [blame] | 1558 | spin_unlock_irqrestore(&uport->lock, flags); |
| 1559 | |
Karthikeyan Ramasubramanian | 22bdbaf | 2017-09-22 11:38:37 -0600 | [diff] [blame] | 1560 | if (!uart_console(uport)) { |
Girish Mahadevan | aa1bb7b | 2017-10-20 14:35:32 -0600 | [diff] [blame] | 1561 | if (msm_port->ioctl_count) { |
| 1562 | int i; |
| 1563 | |
| 1564 | for (i = 0; i < msm_port->ioctl_count; i++) { |
| 1565 | IPC_LOG_MSG(msm_port->ipc_log_pwr, |
| 1566 | "%s IOCTL vote present. Forcing off\n", |
| 1567 | __func__); |
| 1568 | msm_geni_serial_power_off(uport); |
| 1569 | } |
| 1570 | msm_port->ioctl_count = 0; |
| 1571 | } |
Girish Mahadevan | 736892d | 2017-07-14 15:20:58 -0600 | [diff] [blame] | 1572 | msm_geni_serial_power_off(uport); |
Girish Mahadevan | 7115f4e | 2017-03-15 15:18:34 -0600 | [diff] [blame] | 1573 | if (msm_port->wakeup_irq > 0) { |
Girish Mahadevan | 736892d | 2017-07-14 15:20:58 -0600 | [diff] [blame] | 1574 | irq_set_irq_wake(msm_port->wakeup_irq, 0); |
Girish Mahadevan | 7115f4e | 2017-03-15 15:18:34 -0600 | [diff] [blame] | 1575 | disable_irq(msm_port->wakeup_irq); |
Karthikeyan Ramasubramanian | 53157c5 | 2017-08-11 17:06:24 -0600 | [diff] [blame] | 1576 | free_irq(msm_port->wakeup_irq, uport); |
Girish Mahadevan | 7115f4e | 2017-03-15 15:18:34 -0600 | [diff] [blame] | 1577 | } |
Girish Mahadevan | 7115f4e | 2017-03-15 15:18:34 -0600 | [diff] [blame] | 1578 | } |
| 1579 | IPC_LOG_MSG(msm_port->ipc_log_misc, "%s\n", __func__); |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 1580 | } |
| 1581 | |
| 1582 | static int msm_geni_serial_port_setup(struct uart_port *uport) |
| 1583 | { |
| 1584 | int ret = 0; |
| 1585 | struct msm_geni_serial_port *msm_port = GET_DEV_PORT(uport); |
Girish Mahadevan | b1ab172 | 2017-04-27 16:39:11 -0600 | [diff] [blame] | 1586 | unsigned long cfg0, cfg1; |
Girish Mahadevan | c2b9252 | 2017-08-17 22:41:32 -0600 | [diff] [blame] | 1587 | unsigned int rxstale = DEFAULT_BITS_PER_CHAR * STALE_TIMEOUT; |
Girish Mahadevan | b1ab172 | 2017-04-27 16:39:11 -0600 | [diff] [blame] | 1588 | |
Girish Mahadevan | 7115f4e | 2017-03-15 15:18:34 -0600 | [diff] [blame] | 1589 | set_rfr_wm(msm_port); |
Girish Mahadevan | c2b9252 | 2017-08-17 22:41:32 -0600 | [diff] [blame] | 1590 | geni_write_reg_nolog(rxstale, uport->membase, SE_UART_RX_STALE_CNT); |
Girish Mahadevan | b1ab172 | 2017-04-27 16:39:11 -0600 | [diff] [blame] | 1591 | if (!uart_console(uport)) { |
| 1592 | /* For now only assume FIFO mode. */ |
Karthikeyan Ramasubramanian | 8ec770cf | 2017-04-20 15:29:09 -0600 | [diff] [blame] | 1593 | msm_port->xfer_mode = SE_DMA; |
Girish Mahadevan | b1ab172 | 2017-04-27 16:39:11 -0600 | [diff] [blame] | 1594 | se_get_packing_config(8, 4, false, &cfg0, &cfg1); |
| 1595 | geni_write_reg_nolog(cfg0, uport->membase, |
| 1596 | SE_GENI_TX_PACKING_CFG0); |
| 1597 | geni_write_reg_nolog(cfg1, uport->membase, |
| 1598 | SE_GENI_TX_PACKING_CFG1); |
Girish Mahadevan | c2b9252 | 2017-08-17 22:41:32 -0600 | [diff] [blame] | 1599 | geni_write_reg_nolog(cfg0, uport->membase, |
| 1600 | SE_GENI_RX_PACKING_CFG0); |
| 1601 | geni_write_reg_nolog(cfg1, uport->membase, |
| 1602 | SE_GENI_RX_PACKING_CFG1); |
Karthikeyan Ramasubramanian | c8b095c | 2017-05-24 00:09:01 -0600 | [diff] [blame] | 1603 | msm_port->handle_rx = handle_rx_hs; |
| 1604 | msm_port->rx_fifo = devm_kzalloc(uport->dev, |
| 1605 | sizeof(msm_port->rx_fifo_depth * sizeof(u32)), |
| 1606 | GFP_KERNEL); |
| 1607 | if (!msm_port->rx_fifo) { |
| 1608 | ret = -ENOMEM; |
| 1609 | goto exit_portsetup; |
| 1610 | } |
Karthikeyan Ramasubramanian | b6be8c2 | 2017-08-23 16:34:00 -0600 | [diff] [blame] | 1611 | |
| 1612 | msm_port->rx_buf = devm_kzalloc(uport->dev, DMA_RX_BUF_SIZE, |
| 1613 | GFP_KERNEL); |
| 1614 | if (!msm_port->rx_buf) { |
| 1615 | kfree(msm_port->rx_fifo); |
| 1616 | msm_port->rx_fifo = NULL; |
| 1617 | ret = -ENOMEM; |
| 1618 | goto exit_portsetup; |
| 1619 | } |
Girish Mahadevan | a4ed038 | 2017-05-12 11:25:30 -0600 | [diff] [blame] | 1620 | } else { |
| 1621 | /* |
| 1622 | * Make an unconditional cancel on the main sequencer to reset |
| 1623 | * it else we could end up in data loss scenarios. |
| 1624 | */ |
| 1625 | msm_port->xfer_mode = FIFO_MODE; |
| 1626 | msm_geni_serial_poll_cancel_tx(uport); |
| 1627 | se_get_packing_config(8, 1, false, &cfg0, &cfg1); |
| 1628 | geni_write_reg_nolog(cfg0, uport->membase, |
| 1629 | SE_GENI_TX_PACKING_CFG0); |
| 1630 | geni_write_reg_nolog(cfg1, uport->membase, |
| 1631 | SE_GENI_TX_PACKING_CFG1); |
Girish Mahadevan | c2b9252 | 2017-08-17 22:41:32 -0600 | [diff] [blame] | 1632 | se_get_packing_config(8, 4, false, &cfg0, &cfg1); |
| 1633 | geni_write_reg_nolog(cfg0, uport->membase, |
| 1634 | SE_GENI_RX_PACKING_CFG0); |
| 1635 | geni_write_reg_nolog(cfg1, uport->membase, |
| 1636 | SE_GENI_RX_PACKING_CFG1); |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 1637 | } |
Girish Mahadevan | a4ed038 | 2017-05-12 11:25:30 -0600 | [diff] [blame] | 1638 | ret = geni_se_init(uport->membase, msm_port->rx_wm, msm_port->rx_rfr); |
| 1639 | if (ret) { |
| 1640 | dev_err(uport->dev, "%s: Fail\n", __func__); |
| 1641 | goto exit_portsetup; |
| 1642 | } |
| 1643 | |
| 1644 | ret = geni_se_select_mode(uport->membase, msm_port->xfer_mode); |
| 1645 | if (ret) |
| 1646 | goto exit_portsetup; |
Karthikeyan Ramasubramanian | 0d578b7 | 2017-04-26 10:44:02 -0600 | [diff] [blame] | 1647 | |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 1648 | msm_port->port_setup = true; |
| 1649 | /* |
| 1650 | * Ensure Port setup related IO completes before returning to |
| 1651 | * framework. |
| 1652 | */ |
| 1653 | mb(); |
| 1654 | exit_portsetup: |
| 1655 | return ret; |
| 1656 | } |
| 1657 | |
| 1658 | static int msm_geni_serial_startup(struct uart_port *uport) |
| 1659 | { |
| 1660 | int ret = 0; |
| 1661 | struct msm_geni_serial_port *msm_port = GET_DEV_PORT(uport); |
| 1662 | |
| 1663 | scnprintf(msm_port->name, sizeof(msm_port->name), "msm_serial_geni%d", |
| 1664 | uport->line); |
| 1665 | |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 1666 | if (likely(!uart_console(uport))) { |
| 1667 | ret = msm_geni_serial_power_on(&msm_port->uport); |
Girish Mahadevan | 736892d | 2017-07-14 15:20:58 -0600 | [diff] [blame] | 1668 | if (ret) { |
| 1669 | dev_err(uport->dev, "%s:Failed to power on %d\n", |
| 1670 | __func__, ret); |
| 1671 | return ret; |
| 1672 | } |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 1673 | } |
| 1674 | |
Girish Mahadevan | 7115f4e | 2017-03-15 15:18:34 -0600 | [diff] [blame] | 1675 | if (unlikely(get_se_proto(uport->membase) != UART)) { |
| 1676 | dev_err(uport->dev, "%s: Invalid FW %d loaded.\n", |
| 1677 | __func__, get_se_proto(uport->membase)); |
Girish Mahadevan | a4ed038 | 2017-05-12 11:25:30 -0600 | [diff] [blame] | 1678 | ret = -ENXIO; |
Girish Mahadevan | a4ed038 | 2017-05-12 11:25:30 -0600 | [diff] [blame] | 1679 | goto exit_startup; |
Girish Mahadevan | 7115f4e | 2017-03-15 15:18:34 -0600 | [diff] [blame] | 1680 | } |
| 1681 | |
Karthikeyan Ramasubramanian | c8b095c | 2017-05-24 00:09:01 -0600 | [diff] [blame] | 1682 | get_tx_fifo_size(msm_port); |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 1683 | if (!msm_port->port_setup) { |
| 1684 | if (msm_geni_serial_port_setup(uport)) |
| 1685 | goto exit_startup; |
| 1686 | } |
| 1687 | |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 1688 | /* |
| 1689 | * Ensure that all the port configuration writes complete |
| 1690 | * before returning to the framework. |
| 1691 | */ |
| 1692 | mb(); |
Girish Mahadevan | 33661b8 | 2017-05-16 18:59:11 -0600 | [diff] [blame] | 1693 | ret = request_irq(uport->irq, msm_geni_serial_isr, IRQF_TRIGGER_HIGH, |
Karthikeyan Ramasubramanian | 53157c5 | 2017-08-11 17:06:24 -0600 | [diff] [blame] | 1694 | msm_port->name, uport); |
Girish Mahadevan | 33661b8 | 2017-05-16 18:59:11 -0600 | [diff] [blame] | 1695 | if (unlikely(ret)) { |
| 1696 | dev_err(uport->dev, "%s: Failed to get IRQ ret %d\n", |
| 1697 | __func__, ret); |
| 1698 | goto exit_startup; |
| 1699 | } |
| 1700 | |
| 1701 | if (msm_port->wakeup_irq > 0) { |
Girish Mahadevan | 736892d | 2017-07-14 15:20:58 -0600 | [diff] [blame] | 1702 | ret = request_irq(msm_port->wakeup_irq, msm_geni_wakeup_isr, |
Girish Mahadevan | 33661b8 | 2017-05-16 18:59:11 -0600 | [diff] [blame] | 1703 | IRQF_TRIGGER_FALLING | IRQF_ONESHOT, |
| 1704 | "hs_uart_wakeup", uport); |
| 1705 | if (unlikely(ret)) { |
| 1706 | dev_err(uport->dev, "%s:Failed to get WakeIRQ ret%d\n", |
| 1707 | __func__, ret); |
| 1708 | goto exit_startup; |
| 1709 | } |
| 1710 | disable_irq(msm_port->wakeup_irq); |
Girish Mahadevan | 736892d | 2017-07-14 15:20:58 -0600 | [diff] [blame] | 1711 | ret = irq_set_irq_wake(msm_port->wakeup_irq, 1); |
| 1712 | if (unlikely(ret)) { |
| 1713 | dev_err(uport->dev, "%s:Failed to set IRQ wake:%d\n", |
| 1714 | __func__, ret); |
| 1715 | goto exit_startup; |
| 1716 | } |
Girish Mahadevan | 33661b8 | 2017-05-16 18:59:11 -0600 | [diff] [blame] | 1717 | } |
Girish Mahadevan | 7115f4e | 2017-03-15 15:18:34 -0600 | [diff] [blame] | 1718 | IPC_LOG_MSG(msm_port->ipc_log_misc, "%s\n", __func__); |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 1719 | exit_startup: |
Girish Mahadevan | 736892d | 2017-07-14 15:20:58 -0600 | [diff] [blame] | 1720 | if (likely(!uart_console(uport))) |
| 1721 | msm_geni_serial_power_off(&msm_port->uport); |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 1722 | return ret; |
| 1723 | } |
| 1724 | |
Girish Mahadevan | 7115f4e | 2017-03-15 15:18:34 -0600 | [diff] [blame] | 1725 | static int get_clk_cfg(unsigned long clk_freq, unsigned long *ser_clk) |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 1726 | { |
Girish Mahadevan | 7115f4e | 2017-03-15 15:18:34 -0600 | [diff] [blame] | 1727 | unsigned long root_freq[] = {7372800, 14745600, 19200000, 29491200, |
| 1728 | 32000000, 48000000, 64000000, 80000000, 96000000, 100000000}; |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 1729 | int i; |
| 1730 | int match = -1; |
| 1731 | |
| 1732 | for (i = 0; i < ARRAY_SIZE(root_freq); i++) { |
| 1733 | if (clk_freq > root_freq[i]) |
| 1734 | continue; |
| 1735 | |
| 1736 | if (!(root_freq[i] % clk_freq)) { |
| 1737 | match = i; |
| 1738 | break; |
| 1739 | } |
| 1740 | } |
| 1741 | if (match != -1) |
| 1742 | *ser_clk = root_freq[match]; |
Girish Mahadevan | 7115f4e | 2017-03-15 15:18:34 -0600 | [diff] [blame] | 1743 | else |
| 1744 | pr_err("clk_freq %ld\n", clk_freq); |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 1745 | return match; |
| 1746 | } |
| 1747 | |
| 1748 | static void geni_serial_write_term_regs(struct uart_port *uport, u32 loopback, |
| 1749 | u32 tx_trans_cfg, u32 tx_parity_cfg, u32 rx_trans_cfg, |
| 1750 | u32 rx_parity_cfg, u32 bits_per_char, u32 stop_bit_len, |
Girish Mahadevan | b1ab172 | 2017-04-27 16:39:11 -0600 | [diff] [blame] | 1751 | u32 s_clk_cfg) |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 1752 | { |
Girish Mahadevan | bd9b44f | 2017-04-11 13:11:10 -0600 | [diff] [blame] | 1753 | geni_write_reg_nolog(loopback, uport->membase, SE_UART_LOOPBACK_CFG); |
| 1754 | geni_write_reg_nolog(tx_trans_cfg, uport->membase, |
| 1755 | SE_UART_TX_TRANS_CFG); |
| 1756 | geni_write_reg_nolog(tx_parity_cfg, uport->membase, |
| 1757 | SE_UART_TX_PARITY_CFG); |
| 1758 | geni_write_reg_nolog(rx_trans_cfg, uport->membase, |
| 1759 | SE_UART_RX_TRANS_CFG); |
| 1760 | geni_write_reg_nolog(rx_parity_cfg, uport->membase, |
| 1761 | SE_UART_RX_PARITY_CFG); |
| 1762 | geni_write_reg_nolog(bits_per_char, uport->membase, |
| 1763 | SE_UART_TX_WORD_LEN); |
| 1764 | geni_write_reg_nolog(bits_per_char, uport->membase, |
| 1765 | SE_UART_RX_WORD_LEN); |
| 1766 | geni_write_reg_nolog(stop_bit_len, uport->membase, |
| 1767 | SE_UART_TX_STOP_BIT_LEN); |
Girish Mahadevan | bd9b44f | 2017-04-11 13:11:10 -0600 | [diff] [blame] | 1768 | geni_write_reg_nolog(s_clk_cfg, uport->membase, GENI_SER_M_CLK_CFG); |
| 1769 | geni_write_reg_nolog(s_clk_cfg, uport->membase, GENI_SER_S_CLK_CFG); |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 1770 | } |
| 1771 | |
| 1772 | static int get_clk_div_rate(unsigned int baud, unsigned long *desired_clk_rate) |
| 1773 | { |
| 1774 | unsigned long ser_clk; |
| 1775 | int dfs_index; |
| 1776 | int clk_div = 0; |
| 1777 | |
| 1778 | *desired_clk_rate = baud * UART_OVERSAMPLING; |
Girish Mahadevan | 7115f4e | 2017-03-15 15:18:34 -0600 | [diff] [blame] | 1779 | dfs_index = get_clk_cfg(*desired_clk_rate, &ser_clk); |
| 1780 | if (dfs_index < 0) { |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 1781 | pr_err("%s: Can't find matching DFS entry for baud %d\n", |
| 1782 | __func__, baud); |
| 1783 | clk_div = -EINVAL; |
| 1784 | goto exit_get_clk_div_rate; |
| 1785 | } |
| 1786 | |
| 1787 | clk_div = ser_clk / *desired_clk_rate; |
Girish Mahadevan | bd9b44f | 2017-04-11 13:11:10 -0600 | [diff] [blame] | 1788 | *desired_clk_rate = ser_clk; |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 1789 | exit_get_clk_div_rate: |
| 1790 | return clk_div; |
| 1791 | } |
| 1792 | |
| 1793 | static void msm_geni_serial_set_termios(struct uart_port *uport, |
| 1794 | struct ktermios *termios, struct ktermios *old) |
| 1795 | { |
| 1796 | unsigned int baud; |
| 1797 | unsigned int bits_per_char = 0; |
| 1798 | unsigned int tx_trans_cfg; |
| 1799 | unsigned int tx_parity_cfg; |
| 1800 | unsigned int rx_trans_cfg; |
| 1801 | unsigned int rx_parity_cfg; |
| 1802 | unsigned int stop_bit_len; |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 1803 | unsigned int clk_div; |
Girish Mahadevan | 18a9fb0 | 2017-03-29 11:26:06 -0600 | [diff] [blame] | 1804 | unsigned long ser_clk_cfg = 0; |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 1805 | struct msm_geni_serial_port *port = GET_DEV_PORT(uport); |
| 1806 | unsigned long clk_rate; |
| 1807 | |
Girish Mahadevan | eecdd97 | 2017-08-22 17:58:08 -0600 | [diff] [blame] | 1808 | if (!uart_console(uport)) { |
| 1809 | int ret = msm_geni_serial_power_on(uport); |
| 1810 | |
| 1811 | if (ret) { |
| 1812 | IPC_LOG_MSG(port->ipc_log_misc, |
| 1813 | "%s: Failed to vote clock on:%d\n", |
| 1814 | __func__, ret); |
| 1815 | return; |
| 1816 | } |
Girish Mahadevan | 736892d | 2017-07-14 15:20:58 -0600 | [diff] [blame] | 1817 | } |
Girish Mahadevan | eecdd97 | 2017-08-22 17:58:08 -0600 | [diff] [blame] | 1818 | msm_geni_serial_stop_rx(uport); |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 1819 | /* baud rate */ |
| 1820 | baud = uart_get_baud_rate(uport, termios, old, 300, 4000000); |
Girish Mahadevan | 7115f4e | 2017-03-15 15:18:34 -0600 | [diff] [blame] | 1821 | port->cur_baud = baud; |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 1822 | clk_div = get_clk_div_rate(baud, &clk_rate); |
| 1823 | if (clk_div <= 0) |
| 1824 | goto exit_set_termios; |
| 1825 | |
| 1826 | uport->uartclk = clk_rate; |
| 1827 | clk_set_rate(port->serial_rsc.se_clk, clk_rate); |
| 1828 | ser_clk_cfg |= SER_CLK_EN; |
| 1829 | ser_clk_cfg |= (clk_div << CLK_DIV_SHFT); |
| 1830 | |
| 1831 | /* parity */ |
Girish Mahadevan | bd9b44f | 2017-04-11 13:11:10 -0600 | [diff] [blame] | 1832 | tx_trans_cfg = geni_read_reg_nolog(uport->membase, |
| 1833 | SE_UART_TX_TRANS_CFG); |
| 1834 | tx_parity_cfg = geni_read_reg_nolog(uport->membase, |
| 1835 | SE_UART_TX_PARITY_CFG); |
| 1836 | rx_trans_cfg = geni_read_reg_nolog(uport->membase, |
| 1837 | SE_UART_RX_TRANS_CFG); |
| 1838 | rx_parity_cfg = geni_read_reg_nolog(uport->membase, |
| 1839 | SE_UART_RX_PARITY_CFG); |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 1840 | if (termios->c_cflag & PARENB) { |
| 1841 | tx_trans_cfg |= UART_TX_PAR_EN; |
| 1842 | rx_trans_cfg |= UART_RX_PAR_EN; |
| 1843 | tx_parity_cfg |= PAR_CALC_EN; |
| 1844 | rx_parity_cfg |= PAR_CALC_EN; |
| 1845 | if (termios->c_cflag & PARODD) { |
| 1846 | tx_parity_cfg |= PAR_ODD; |
| 1847 | rx_parity_cfg |= PAR_ODD; |
| 1848 | } else if (termios->c_cflag & CMSPAR) { |
| 1849 | tx_parity_cfg |= PAR_SPACE; |
| 1850 | rx_parity_cfg |= PAR_SPACE; |
| 1851 | } else { |
| 1852 | tx_parity_cfg |= PAR_EVEN; |
| 1853 | rx_parity_cfg |= PAR_EVEN; |
| 1854 | } |
| 1855 | } else { |
| 1856 | tx_trans_cfg &= ~UART_TX_PAR_EN; |
| 1857 | rx_trans_cfg &= ~UART_RX_PAR_EN; |
| 1858 | tx_parity_cfg &= ~PAR_CALC_EN; |
| 1859 | rx_parity_cfg &= ~PAR_CALC_EN; |
| 1860 | } |
| 1861 | |
| 1862 | /* bits per char */ |
| 1863 | switch (termios->c_cflag & CSIZE) { |
| 1864 | case CS5: |
| 1865 | bits_per_char = 5; |
| 1866 | break; |
| 1867 | case CS6: |
| 1868 | bits_per_char = 6; |
| 1869 | break; |
| 1870 | case CS7: |
| 1871 | bits_per_char = 7; |
| 1872 | break; |
| 1873 | case CS8: |
| 1874 | default: |
| 1875 | bits_per_char = 8; |
| 1876 | break; |
| 1877 | } |
| 1878 | |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 1879 | |
| 1880 | /* stop bits */ |
| 1881 | if (termios->c_cflag & CSTOPB) |
| 1882 | stop_bit_len = TX_STOP_BIT_LEN_2; |
| 1883 | else |
| 1884 | stop_bit_len = TX_STOP_BIT_LEN_1; |
| 1885 | |
| 1886 | /* flow control, clear the CTS_MASK bit if using flow control. */ |
| 1887 | if (termios->c_cflag & CRTSCTS) |
| 1888 | tx_trans_cfg &= ~UART_CTS_MASK; |
| 1889 | else |
| 1890 | tx_trans_cfg |= UART_CTS_MASK; |
| 1891 | /* status bits to ignore */ |
| 1892 | |
Karthikeyan Ramasubramanian | 61074fa | 2017-05-24 00:18:34 -0600 | [diff] [blame] | 1893 | if (likely(baud)) |
| 1894 | uart_update_timeout(uport, termios->c_cflag, baud); |
| 1895 | |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 1896 | geni_serial_write_term_regs(uport, port->loopback, tx_trans_cfg, |
| 1897 | tx_parity_cfg, rx_trans_cfg, rx_parity_cfg, bits_per_char, |
Girish Mahadevan | b1ab172 | 2017-04-27 16:39:11 -0600 | [diff] [blame] | 1898 | stop_bit_len, ser_clk_cfg); |
Girish Mahadevan | 7115f4e | 2017-03-15 15:18:34 -0600 | [diff] [blame] | 1899 | IPC_LOG_MSG(port->ipc_log_misc, "%s: baud %d\n", __func__, baud); |
| 1900 | IPC_LOG_MSG(port->ipc_log_misc, "Tx: trans_cfg%d parity %d\n", |
| 1901 | tx_trans_cfg, tx_parity_cfg); |
| 1902 | IPC_LOG_MSG(port->ipc_log_misc, "Rx: trans_cfg%d parity %d", |
| 1903 | rx_trans_cfg, rx_parity_cfg); |
| 1904 | IPC_LOG_MSG(port->ipc_log_misc, "BitsChar%d stop bit%d\n", |
| 1905 | bits_per_char, stop_bit_len); |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 1906 | exit_set_termios: |
Girish Mahadevan | eecdd97 | 2017-08-22 17:58:08 -0600 | [diff] [blame] | 1907 | msm_geni_serial_start_rx(uport); |
| 1908 | if (!uart_console(uport)) |
| 1909 | msm_geni_serial_power_off(uport); |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 1910 | return; |
| 1911 | |
| 1912 | } |
| 1913 | |
Girish Mahadevan | 7115f4e | 2017-03-15 15:18:34 -0600 | [diff] [blame] | 1914 | static unsigned int msm_geni_serial_tx_empty(struct uart_port *uport) |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 1915 | { |
| 1916 | unsigned int tx_fifo_status; |
| 1917 | unsigned int is_tx_empty = 1; |
Girish Mahadevan | 736892d | 2017-07-14 15:20:58 -0600 | [diff] [blame] | 1918 | struct msm_geni_serial_port *port = GET_DEV_PORT(uport); |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 1919 | |
Girish Mahadevan | aa1bb7b | 2017-10-20 14:35:32 -0600 | [diff] [blame] | 1920 | if (!uart_console(uport) && device_pending_suspend(uport)) |
Girish Mahadevan | 6d97cbc | 2017-11-07 15:28:27 -0700 | [diff] [blame] | 1921 | return 1; |
Karthikeyan Ramasubramanian | 8ec770cf | 2017-04-20 15:29:09 -0600 | [diff] [blame] | 1922 | |
| 1923 | if (port->xfer_mode == SE_DMA) |
| 1924 | tx_fifo_status = port->tx_dma ? 1 : 0; |
| 1925 | else |
| 1926 | tx_fifo_status = geni_read_reg_nolog(uport->membase, |
| 1927 | SE_GENI_TX_FIFO_STATUS); |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 1928 | if (tx_fifo_status) |
| 1929 | is_tx_empty = 0; |
| 1930 | |
| 1931 | return is_tx_empty; |
| 1932 | } |
| 1933 | |
Karthikeyan Ramasubramanian | 8ec770cf | 2017-04-20 15:29:09 -0600 | [diff] [blame] | 1934 | static ssize_t msm_geni_serial_xfer_mode_show(struct device *dev, |
| 1935 | struct device_attribute *attr, char *buf) |
| 1936 | { |
| 1937 | struct platform_device *pdev = to_platform_device(dev); |
| 1938 | struct msm_geni_serial_port *port = platform_get_drvdata(pdev); |
| 1939 | ssize_t ret = 0; |
| 1940 | |
| 1941 | if (port->xfer_mode == FIFO_MODE) |
| 1942 | ret = snprintf(buf, sizeof("FIFO\n"), "FIFO\n"); |
| 1943 | else if (port->xfer_mode == SE_DMA) |
| 1944 | ret = snprintf(buf, sizeof("SE_DMA\n"), "SE_DMA\n"); |
| 1945 | |
| 1946 | return ret; |
| 1947 | } |
| 1948 | |
| 1949 | static ssize_t msm_geni_serial_xfer_mode_store(struct device *dev, |
| 1950 | struct device_attribute *attr, const char *buf, |
| 1951 | size_t size) |
| 1952 | { |
| 1953 | struct platform_device *pdev = to_platform_device(dev); |
| 1954 | struct msm_geni_serial_port *port = platform_get_drvdata(pdev); |
| 1955 | struct uart_port *uport = &port->uport; |
| 1956 | int xfer_mode = port->xfer_mode; |
| 1957 | unsigned long flags; |
| 1958 | |
| 1959 | if (uart_console(uport)) |
| 1960 | return -EOPNOTSUPP; |
| 1961 | |
| 1962 | if (strnstr(buf, "FIFO", strlen("FIFO"))) { |
| 1963 | xfer_mode = FIFO_MODE; |
| 1964 | } else if (strnstr(buf, "SE_DMA", strlen("SE_DMA"))) { |
| 1965 | xfer_mode = SE_DMA; |
| 1966 | } else { |
| 1967 | dev_err(dev, "%s: Invalid input %s\n", __func__, buf); |
| 1968 | return -EINVAL; |
| 1969 | } |
| 1970 | |
| 1971 | if (xfer_mode == port->xfer_mode) |
| 1972 | return size; |
| 1973 | |
| 1974 | msm_geni_serial_power_on(uport); |
| 1975 | spin_lock_irqsave(&uport->lock, flags); |
| 1976 | msm_geni_serial_stop_tx(uport); |
| 1977 | msm_geni_serial_stop_rx(uport); |
| 1978 | port->xfer_mode = xfer_mode; |
| 1979 | geni_se_select_mode(uport->membase, port->xfer_mode); |
| 1980 | spin_unlock_irqrestore(&uport->lock, flags); |
| 1981 | msm_geni_serial_start_rx(uport); |
| 1982 | msm_geni_serial_power_off(uport); |
| 1983 | |
| 1984 | return size; |
| 1985 | } |
| 1986 | |
| 1987 | static DEVICE_ATTR(xfer_mode, 0644, msm_geni_serial_xfer_mode_show, |
| 1988 | msm_geni_serial_xfer_mode_store); |
| 1989 | |
Girish Mahadevan | f08b110 | 2017-04-02 19:27:28 -0600 | [diff] [blame] | 1990 | #if defined(CONFIG_SERIAL_CORE_CONSOLE) || defined(CONFIG_CONSOLE_POLL) |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 1991 | static int __init msm_geni_console_setup(struct console *co, char *options) |
| 1992 | { |
| 1993 | struct uart_port *uport; |
| 1994 | struct msm_geni_serial_port *dev_port; |
| 1995 | int baud = 115200; |
| 1996 | int bits = 8; |
| 1997 | int parity = 'n'; |
| 1998 | int flow = 'n'; |
| 1999 | int ret = 0; |
| 2000 | |
| 2001 | if (unlikely(co->index >= GENI_UART_NR_PORTS || co->index < 0)) |
| 2002 | return -ENXIO; |
| 2003 | |
Girish Mahadevan | 7115f4e | 2017-03-15 15:18:34 -0600 | [diff] [blame] | 2004 | dev_port = get_port_from_line(co->index, true); |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 2005 | if (IS_ERR_OR_NULL(dev_port)) { |
| 2006 | ret = PTR_ERR(dev_port); |
| 2007 | pr_err("Invalid line %d(%d)\n", co->index, ret); |
| 2008 | return ret; |
| 2009 | } |
| 2010 | |
| 2011 | uport = &dev_port->uport; |
| 2012 | |
| 2013 | if (unlikely(!uport->membase)) |
| 2014 | return -ENXIO; |
| 2015 | |
| 2016 | if (se_geni_resources_on(&dev_port->serial_rsc)) |
| 2017 | WARN_ON(1); |
| 2018 | |
| 2019 | if (unlikely(get_se_proto(uport->membase) != UART)) { |
| 2020 | se_geni_resources_off(&dev_port->serial_rsc); |
| 2021 | return -ENXIO; |
| 2022 | } |
| 2023 | |
Girish Mahadevan | c2b9252 | 2017-08-17 22:41:32 -0600 | [diff] [blame] | 2024 | if (!dev_port->port_setup) { |
| 2025 | msm_geni_serial_stop_rx(uport); |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 2026 | msm_geni_serial_port_setup(uport); |
Girish Mahadevan | c2b9252 | 2017-08-17 22:41:32 -0600 | [diff] [blame] | 2027 | } |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 2028 | |
| 2029 | if (options) |
| 2030 | uart_parse_options(options, &baud, &parity, &bits, &flow); |
| 2031 | |
| 2032 | return uart_set_options(uport, co, baud, parity, bits, flow); |
| 2033 | } |
| 2034 | |
Girish Mahadevan | f08b110 | 2017-04-02 19:27:28 -0600 | [diff] [blame] | 2035 | static void |
| 2036 | msm_geni_serial_early_console_write(struct console *con, const char *s, |
| 2037 | unsigned int n) |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 2038 | { |
Girish Mahadevan | f08b110 | 2017-04-02 19:27:28 -0600 | [diff] [blame] | 2039 | struct earlycon_device *dev = con->data; |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 2040 | |
Girish Mahadevan | f08b110 | 2017-04-02 19:27:28 -0600 | [diff] [blame] | 2041 | __msm_geni_serial_console_write(&dev->port, s, n); |
| 2042 | } |
| 2043 | |
| 2044 | static int __init |
| 2045 | msm_geni_serial_earlycon_setup(struct earlycon_device *dev, |
| 2046 | const char *opt) |
| 2047 | { |
| 2048 | struct uart_port *uport = &dev->port; |
| 2049 | int ret = 0; |
Girish Mahadevan | f08b110 | 2017-04-02 19:27:28 -0600 | [diff] [blame] | 2050 | u32 tx_trans_cfg = 0; |
| 2051 | u32 tx_parity_cfg = 0; |
| 2052 | u32 rx_trans_cfg = 0; |
| 2053 | u32 rx_parity_cfg = 0; |
| 2054 | u32 stop_bit = 0; |
| 2055 | u32 rx_stale = 0; |
| 2056 | u32 bits_per_char = 0; |
| 2057 | u32 s_clk_cfg = 0; |
| 2058 | u32 baud = 115200; |
| 2059 | u32 clk_div; |
| 2060 | unsigned long clk_rate; |
Girish Mahadevan | b1ab172 | 2017-04-27 16:39:11 -0600 | [diff] [blame] | 2061 | unsigned long cfg0, cfg1; |
Girish Mahadevan | f08b110 | 2017-04-02 19:27:28 -0600 | [diff] [blame] | 2062 | |
| 2063 | if (!uport->membase) { |
| 2064 | ret = -ENOMEM; |
| 2065 | goto exit_geni_serial_earlyconsetup; |
| 2066 | } |
| 2067 | |
| 2068 | if (get_se_proto(uport->membase) != UART) { |
| 2069 | ret = -ENXIO; |
| 2070 | goto exit_geni_serial_earlyconsetup; |
| 2071 | } |
| 2072 | |
Girish Mahadevan | f08b110 | 2017-04-02 19:27:28 -0600 | [diff] [blame] | 2073 | /* |
| 2074 | * Ignore Flow control. |
| 2075 | * Disable Tx Parity. |
| 2076 | * Don't check Parity during Rx. |
| 2077 | * Disable Rx Parity. |
| 2078 | * n = 8. |
| 2079 | * Stop bit = 0. |
| 2080 | * Stale timeout in bit-time (3 chars worth). |
| 2081 | */ |
| 2082 | tx_trans_cfg |= UART_CTS_MASK; |
| 2083 | tx_parity_cfg = 0; |
| 2084 | rx_trans_cfg = 0; |
| 2085 | rx_parity_cfg = 0; |
| 2086 | bits_per_char = 0x8; |
| 2087 | stop_bit = 0; |
| 2088 | rx_stale = 0x18; |
| 2089 | clk_div = get_clk_div_rate(baud, &clk_rate); |
| 2090 | if (clk_div <= 0) { |
| 2091 | ret = -EINVAL; |
| 2092 | goto exit_geni_serial_earlyconsetup; |
| 2093 | } |
| 2094 | |
| 2095 | s_clk_cfg |= SER_CLK_EN; |
| 2096 | s_clk_cfg |= (clk_div << CLK_DIV_SHFT); |
| 2097 | |
Girish Mahadevan | 24f5659 | 2017-04-15 17:35:05 -0600 | [diff] [blame] | 2098 | /* |
| 2099 | * Make an unconditional cancel on the main sequencer to reset |
| 2100 | * it else we could end up in data loss scenarios. |
| 2101 | */ |
| 2102 | msm_geni_serial_poll_cancel_tx(uport); |
Girish Mahadevan | a4ed038 | 2017-05-12 11:25:30 -0600 | [diff] [blame] | 2103 | msm_geni_serial_abort_rx(uport); |
Girish Mahadevan | 7115f4e | 2017-03-15 15:18:34 -0600 | [diff] [blame] | 2104 | se_get_packing_config(8, 1, false, &cfg0, &cfg1); |
Girish Mahadevan | a4ed038 | 2017-05-12 11:25:30 -0600 | [diff] [blame] | 2105 | geni_se_init(uport->membase, (DEF_FIFO_DEPTH_WORDS >> 1), |
| 2106 | (DEF_FIFO_DEPTH_WORDS - 2)); |
| 2107 | geni_se_select_mode(uport->membase, FIFO_MODE); |
Girish Mahadevan | 7115f4e | 2017-03-15 15:18:34 -0600 | [diff] [blame] | 2108 | geni_write_reg_nolog(cfg0, uport->membase, SE_GENI_TX_PACKING_CFG0); |
| 2109 | geni_write_reg_nolog(cfg1, uport->membase, SE_GENI_TX_PACKING_CFG1); |
| 2110 | geni_write_reg_nolog(tx_trans_cfg, uport->membase, |
| 2111 | SE_UART_TX_TRANS_CFG); |
| 2112 | geni_write_reg_nolog(tx_parity_cfg, uport->membase, |
| 2113 | SE_UART_TX_PARITY_CFG); |
| 2114 | geni_write_reg_nolog(rx_trans_cfg, uport->membase, |
| 2115 | SE_UART_RX_TRANS_CFG); |
| 2116 | geni_write_reg_nolog(rx_parity_cfg, uport->membase, |
| 2117 | SE_UART_RX_PARITY_CFG); |
| 2118 | geni_write_reg_nolog(bits_per_char, uport->membase, |
| 2119 | SE_UART_TX_WORD_LEN); |
| 2120 | geni_write_reg_nolog(bits_per_char, uport->membase, |
| 2121 | SE_UART_RX_WORD_LEN); |
| 2122 | geni_write_reg_nolog(stop_bit, uport->membase, SE_UART_TX_STOP_BIT_LEN); |
| 2123 | geni_write_reg_nolog(s_clk_cfg, uport->membase, GENI_SER_M_CLK_CFG); |
| 2124 | geni_write_reg_nolog(s_clk_cfg, uport->membase, GENI_SER_S_CLK_CFG); |
Girish Mahadevan | f08b110 | 2017-04-02 19:27:28 -0600 | [diff] [blame] | 2125 | |
| 2126 | dev->con->write = msm_geni_serial_early_console_write; |
| 2127 | dev->con->setup = NULL; |
| 2128 | /* |
| 2129 | * Ensure that the early console setup completes before |
| 2130 | * returning. |
| 2131 | */ |
| 2132 | mb(); |
| 2133 | exit_geni_serial_earlyconsetup: |
| 2134 | return ret; |
| 2135 | } |
| 2136 | OF_EARLYCON_DECLARE(msm_geni_serial, "qcom,msm-geni-uart", |
| 2137 | msm_geni_serial_earlycon_setup); |
| 2138 | |
| 2139 | static int console_register(struct uart_driver *drv) |
| 2140 | { |
| 2141 | return uart_register_driver(drv); |
| 2142 | } |
| 2143 | static void console_unregister(struct uart_driver *drv) |
| 2144 | { |
| 2145 | uart_unregister_driver(drv); |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 2146 | } |
| 2147 | |
| 2148 | static struct console cons_ops = { |
| 2149 | .name = "ttyMSM", |
| 2150 | .write = msm_geni_serial_console_write, |
| 2151 | .device = uart_console_device, |
| 2152 | .setup = msm_geni_console_setup, |
| 2153 | .flags = CON_PRINTBUFFER, |
| 2154 | .index = -1, |
| 2155 | .data = &msm_geni_console_driver, |
| 2156 | }; |
| 2157 | |
Girish Mahadevan | f08b110 | 2017-04-02 19:27:28 -0600 | [diff] [blame] | 2158 | static struct uart_driver msm_geni_console_driver = { |
| 2159 | .owner = THIS_MODULE, |
| 2160 | .driver_name = "msm_geni_console", |
| 2161 | .dev_name = "ttyMSM", |
| 2162 | .nr = GENI_UART_NR_PORTS, |
| 2163 | .cons = &cons_ops, |
| 2164 | }; |
| 2165 | #else |
| 2166 | static int console_register(struct uart_driver *drv) |
| 2167 | { |
| 2168 | return 0; |
| 2169 | } |
| 2170 | |
| 2171 | static void console_unregister(struct uart_driver *drv) |
| 2172 | { |
| 2173 | } |
| 2174 | #endif /* defined(CONFIG_SERIAL_CORE_CONSOLE) || defined(CONFIG_CONSOLE_POLL) */ |
| 2175 | |
Girish Mahadevan | efb8f6e | 2017-10-11 11:15:02 -0600 | [diff] [blame] | 2176 | static void msm_geni_serial_debug_init(struct uart_port *uport, bool console) |
Girish Mahadevan | f08b110 | 2017-04-02 19:27:28 -0600 | [diff] [blame] | 2177 | { |
| 2178 | struct msm_geni_serial_port *msm_port = GET_DEV_PORT(uport); |
| 2179 | |
| 2180 | msm_port->dbg = debugfs_create_dir(dev_name(uport->dev), NULL); |
| 2181 | if (IS_ERR_OR_NULL(msm_port->dbg)) |
| 2182 | dev_err(uport->dev, "Failed to create dbg dir\n"); |
Girish Mahadevan | efb8f6e | 2017-10-11 11:15:02 -0600 | [diff] [blame] | 2183 | |
| 2184 | if (!console) { |
| 2185 | char name[30]; |
| 2186 | |
| 2187 | memset(name, 0, sizeof(name)); |
| 2188 | if (!msm_port->ipc_log_rx) { |
| 2189 | scnprintf(name, sizeof(name), "%s%s", |
| 2190 | dev_name(uport->dev), "_rx"); |
| 2191 | msm_port->ipc_log_rx = ipc_log_context_create( |
| 2192 | IPC_LOG_TX_RX_PAGES, name, 0); |
| 2193 | if (!msm_port->ipc_log_rx) |
| 2194 | dev_info(uport->dev, "Err in Rx IPC Log\n"); |
| 2195 | } |
| 2196 | memset(name, 0, sizeof(name)); |
| 2197 | if (!msm_port->ipc_log_tx) { |
| 2198 | scnprintf(name, sizeof(name), "%s%s", |
| 2199 | dev_name(uport->dev), "_tx"); |
| 2200 | msm_port->ipc_log_tx = ipc_log_context_create( |
| 2201 | IPC_LOG_TX_RX_PAGES, name, 0); |
| 2202 | if (!msm_port->ipc_log_tx) |
| 2203 | dev_info(uport->dev, "Err in Tx IPC Log\n"); |
| 2204 | } |
| 2205 | memset(name, 0, sizeof(name)); |
| 2206 | if (!msm_port->ipc_log_pwr) { |
| 2207 | scnprintf(name, sizeof(name), "%s%s", |
| 2208 | dev_name(uport->dev), "_pwr"); |
| 2209 | msm_port->ipc_log_pwr = ipc_log_context_create( |
| 2210 | IPC_LOG_PWR_PAGES, name, 0); |
| 2211 | if (!msm_port->ipc_log_pwr) |
| 2212 | dev_info(uport->dev, "Err in Pwr IPC Log\n"); |
| 2213 | } |
| 2214 | memset(name, 0, sizeof(name)); |
| 2215 | if (!msm_port->ipc_log_misc) { |
| 2216 | scnprintf(name, sizeof(name), "%s%s", |
| 2217 | dev_name(uport->dev), "_misc"); |
| 2218 | msm_port->ipc_log_misc = ipc_log_context_create( |
| 2219 | IPC_LOG_MISC_PAGES, name, 0); |
| 2220 | if (!msm_port->ipc_log_misc) |
| 2221 | dev_info(uport->dev, "Err in Misc IPC Log\n"); |
| 2222 | } |
Girish Mahadevan | efb8f6e | 2017-10-11 11:15:02 -0600 | [diff] [blame] | 2223 | } |
Girish Mahadevan | f08b110 | 2017-04-02 19:27:28 -0600 | [diff] [blame] | 2224 | } |
| 2225 | |
Karthikeyan Ramasubramanian | 22bdbaf | 2017-09-22 11:38:37 -0600 | [diff] [blame] | 2226 | static void msm_geni_serial_cons_pm(struct uart_port *uport, |
| 2227 | unsigned int new_state, unsigned int old_state) |
| 2228 | { |
| 2229 | struct msm_geni_serial_port *msm_port = GET_DEV_PORT(uport); |
| 2230 | |
| 2231 | if (unlikely(!uart_console(uport))) |
| 2232 | return; |
| 2233 | |
| 2234 | if (new_state == UART_PM_STATE_ON && old_state == UART_PM_STATE_OFF) |
| 2235 | se_geni_resources_on(&msm_port->serial_rsc); |
| 2236 | else if (new_state == UART_PM_STATE_OFF && |
| 2237 | old_state == UART_PM_STATE_ON) |
| 2238 | se_geni_resources_off(&msm_port->serial_rsc); |
| 2239 | } |
| 2240 | |
Girish Mahadevan | 7115f4e | 2017-03-15 15:18:34 -0600 | [diff] [blame] | 2241 | static const struct uart_ops msm_geni_console_pops = { |
| 2242 | .tx_empty = msm_geni_serial_tx_empty, |
| 2243 | .stop_tx = msm_geni_serial_stop_tx, |
| 2244 | .start_tx = msm_geni_serial_start_tx, |
| 2245 | .stop_rx = msm_geni_serial_stop_rx, |
| 2246 | .set_termios = msm_geni_serial_set_termios, |
| 2247 | .startup = msm_geni_serial_startup, |
| 2248 | .config_port = msm_geni_serial_config_port, |
| 2249 | .shutdown = msm_geni_serial_shutdown, |
| 2250 | .type = msm_geni_serial_get_type, |
| 2251 | .set_mctrl = msm_geni_cons_set_mctrl, |
Karthikeyan Ramasubramanian | f5fb743 | 2017-05-24 21:59:55 -0600 | [diff] [blame] | 2252 | .get_mctrl = msm_geni_cons_get_mctrl, |
Girish Mahadevan | 7115f4e | 2017-03-15 15:18:34 -0600 | [diff] [blame] | 2253 | #ifdef CONFIG_CONSOLE_POLL |
| 2254 | .poll_get_char = msm_geni_serial_get_char, |
| 2255 | .poll_put_char = msm_geni_serial_poll_put_char, |
| 2256 | #endif |
Karthikeyan Ramasubramanian | 22bdbaf | 2017-09-22 11:38:37 -0600 | [diff] [blame] | 2257 | .pm = msm_geni_serial_cons_pm, |
Girish Mahadevan | 7115f4e | 2017-03-15 15:18:34 -0600 | [diff] [blame] | 2258 | }; |
| 2259 | |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 2260 | static const struct uart_ops msm_geni_serial_pops = { |
| 2261 | .tx_empty = msm_geni_serial_tx_empty, |
| 2262 | .stop_tx = msm_geni_serial_stop_tx, |
| 2263 | .start_tx = msm_geni_serial_start_tx, |
| 2264 | .stop_rx = msm_geni_serial_stop_rx, |
| 2265 | .set_termios = msm_geni_serial_set_termios, |
| 2266 | .startup = msm_geni_serial_startup, |
| 2267 | .config_port = msm_geni_serial_config_port, |
| 2268 | .shutdown = msm_geni_serial_shutdown, |
| 2269 | .type = msm_geni_serial_get_type, |
| 2270 | .set_mctrl = msm_geni_serial_set_mctrl, |
Girish Mahadevan | 7115f4e | 2017-03-15 15:18:34 -0600 | [diff] [blame] | 2271 | .get_mctrl = msm_geni_serial_get_mctrl, |
| 2272 | .break_ctl = msm_geni_serial_break_ctl, |
| 2273 | .flush_buffer = NULL, |
| 2274 | .ioctl = msm_geni_serial_ioctl, |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 2275 | }; |
| 2276 | |
| 2277 | static const struct of_device_id msm_geni_device_tbl[] = { |
Girish Mahadevan | f08b110 | 2017-04-02 19:27:28 -0600 | [diff] [blame] | 2278 | #if defined(CONFIG_SERIAL_CORE_CONSOLE) || defined(CONFIG_CONSOLE_POLL) |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 2279 | { .compatible = "qcom,msm-geni-console", |
| 2280 | .data = (void *)&msm_geni_console_driver}, |
Girish Mahadevan | f08b110 | 2017-04-02 19:27:28 -0600 | [diff] [blame] | 2281 | #endif |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 2282 | { .compatible = "qcom,msm-geni-serial-hs", |
| 2283 | .data = (void *)&msm_geni_serial_hs_driver}, |
| 2284 | {}, |
| 2285 | }; |
| 2286 | |
| 2287 | static int msm_geni_serial_probe(struct platform_device *pdev) |
| 2288 | { |
| 2289 | int ret = 0; |
| 2290 | int line; |
| 2291 | struct msm_geni_serial_port *dev_port; |
| 2292 | struct uart_port *uport; |
| 2293 | struct resource *res; |
| 2294 | struct uart_driver *drv; |
| 2295 | const struct of_device_id *id; |
Girish Mahadevan | 7115f4e | 2017-03-15 15:18:34 -0600 | [diff] [blame] | 2296 | bool is_console = false; |
Karthikeyan Ramasubramanian | 0d578b7 | 2017-04-26 10:44:02 -0600 | [diff] [blame] | 2297 | struct platform_device *wrapper_pdev; |
| 2298 | struct device_node *wrapper_ph_node; |
Girish Mahadevan | 736892d | 2017-07-14 15:20:58 -0600 | [diff] [blame] | 2299 | u32 wake_char = 0; |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 2300 | |
| 2301 | id = of_match_device(msm_geni_device_tbl, &pdev->dev); |
| 2302 | if (id) { |
| 2303 | dev_dbg(&pdev->dev, "%s: %s\n", __func__, id->compatible); |
| 2304 | drv = (struct uart_driver *)id->data; |
| 2305 | } else { |
| 2306 | dev_err(&pdev->dev, "%s: No matching device found", __func__); |
| 2307 | return -ENODEV; |
| 2308 | } |
| 2309 | |
Girish Mahadevan | 7115f4e | 2017-03-15 15:18:34 -0600 | [diff] [blame] | 2310 | if (pdev->dev.of_node) { |
| 2311 | if (drv->cons) |
| 2312 | line = of_alias_get_id(pdev->dev.of_node, "serial"); |
| 2313 | else |
| 2314 | line = of_alias_get_id(pdev->dev.of_node, "hsuart"); |
| 2315 | } else { |
| 2316 | line = pdev->id; |
| 2317 | } |
| 2318 | |
| 2319 | if (line < 0) |
| 2320 | line = atomic_inc_return(&uart_line_id) - 1; |
| 2321 | |
| 2322 | if ((line < 0) || (line >= GENI_UART_NR_PORTS)) |
| 2323 | return -ENXIO; |
| 2324 | is_console = (drv->cons ? true : false); |
| 2325 | dev_port = get_port_from_line(line, is_console); |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 2326 | if (IS_ERR_OR_NULL(dev_port)) { |
| 2327 | ret = PTR_ERR(dev_port); |
| 2328 | dev_err(&pdev->dev, "Invalid line %d(%d)\n", |
| 2329 | line, ret); |
| 2330 | goto exit_geni_serial_probe; |
| 2331 | } |
| 2332 | |
| 2333 | uport = &dev_port->uport; |
| 2334 | |
| 2335 | /* Don't allow 2 drivers to access the same port */ |
| 2336 | if (uport->private_data) { |
| 2337 | ret = -ENODEV; |
| 2338 | goto exit_geni_serial_probe; |
| 2339 | } |
| 2340 | |
| 2341 | uport->dev = &pdev->dev; |
Girish Mahadevan | 3e694cc | 2017-04-19 16:50:03 -0600 | [diff] [blame] | 2342 | |
Karthikeyan Ramasubramanian | 0d578b7 | 2017-04-26 10:44:02 -0600 | [diff] [blame] | 2343 | wrapper_ph_node = of_parse_phandle(pdev->dev.of_node, |
| 2344 | "qcom,wrapper-core", 0); |
| 2345 | if (IS_ERR_OR_NULL(wrapper_ph_node)) { |
| 2346 | ret = PTR_ERR(wrapper_ph_node); |
| 2347 | goto exit_geni_serial_probe; |
Girish Mahadevan | 3e694cc | 2017-04-19 16:50:03 -0600 | [diff] [blame] | 2348 | } |
Karthikeyan Ramasubramanian | 0d578b7 | 2017-04-26 10:44:02 -0600 | [diff] [blame] | 2349 | wrapper_pdev = of_find_device_by_node(wrapper_ph_node); |
| 2350 | of_node_put(wrapper_ph_node); |
| 2351 | if (IS_ERR_OR_NULL(wrapper_pdev)) { |
| 2352 | ret = PTR_ERR(wrapper_pdev); |
| 2353 | goto exit_geni_serial_probe; |
| 2354 | } |
| 2355 | dev_port->wrapper_dev = &wrapper_pdev->dev; |
| 2356 | dev_port->serial_rsc.wrapper_dev = &wrapper_pdev->dev; |
| 2357 | ret = geni_se_resources_init(&dev_port->serial_rsc, UART_CORE2X_VOTE, |
| 2358 | (DEFAULT_SE_CLK * DEFAULT_BUS_WIDTH)); |
| 2359 | if (ret) |
| 2360 | goto exit_geni_serial_probe; |
Girish Mahadevan | 3e694cc | 2017-04-19 16:50:03 -0600 | [diff] [blame] | 2361 | |
Girish Mahadevan | 736892d | 2017-07-14 15:20:58 -0600 | [diff] [blame] | 2362 | if (of_property_read_u32(pdev->dev.of_node, "qcom,wakeup-byte", |
| 2363 | &wake_char)) { |
| 2364 | dev_dbg(&pdev->dev, "No Wakeup byte specified\n"); |
| 2365 | } else { |
| 2366 | dev_port->wakeup_byte = (u8)wake_char; |
| 2367 | dev_info(&pdev->dev, "Wakeup byte 0x%x\n", |
| 2368 | dev_port->wakeup_byte); |
| 2369 | } |
Girish Mahadevan | 7115f4e | 2017-03-15 15:18:34 -0600 | [diff] [blame] | 2370 | |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 2371 | dev_port->serial_rsc.se_clk = devm_clk_get(&pdev->dev, "se-clk"); |
| 2372 | if (IS_ERR(dev_port->serial_rsc.se_clk)) { |
| 2373 | ret = PTR_ERR(dev_port->serial_rsc.se_clk); |
| 2374 | dev_err(&pdev->dev, "Err getting SE Core clk %d\n", ret); |
| 2375 | goto exit_geni_serial_probe; |
| 2376 | } |
| 2377 | |
| 2378 | dev_port->serial_rsc.m_ahb_clk = devm_clk_get(&pdev->dev, "m-ahb"); |
| 2379 | if (IS_ERR(dev_port->serial_rsc.m_ahb_clk)) { |
| 2380 | ret = PTR_ERR(dev_port->serial_rsc.m_ahb_clk); |
| 2381 | dev_err(&pdev->dev, "Err getting M AHB clk %d\n", ret); |
| 2382 | goto exit_geni_serial_probe; |
| 2383 | } |
| 2384 | |
| 2385 | dev_port->serial_rsc.s_ahb_clk = devm_clk_get(&pdev->dev, "s-ahb"); |
| 2386 | if (IS_ERR(dev_port->serial_rsc.s_ahb_clk)) { |
| 2387 | ret = PTR_ERR(dev_port->serial_rsc.s_ahb_clk); |
| 2388 | dev_err(&pdev->dev, "Err getting S AHB clk %d\n", ret); |
| 2389 | goto exit_geni_serial_probe; |
| 2390 | } |
| 2391 | |
| 2392 | res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "se_phys"); |
| 2393 | if (!res) { |
| 2394 | ret = -ENXIO; |
| 2395 | dev_err(&pdev->dev, "Err getting IO region\n"); |
| 2396 | goto exit_geni_serial_probe; |
| 2397 | } |
| 2398 | |
| 2399 | uport->mapbase = res->start; |
| 2400 | uport->membase = devm_ioremap(&pdev->dev, res->start, |
| 2401 | resource_size(res)); |
| 2402 | if (!uport->membase) { |
| 2403 | ret = -ENOMEM; |
| 2404 | dev_err(&pdev->dev, "Err IO mapping serial iomem"); |
| 2405 | goto exit_geni_serial_probe; |
| 2406 | } |
| 2407 | |
Girish Mahadevan | 6d97cbc | 2017-11-07 15:28:27 -0700 | [diff] [blame] | 2408 | /* Optional to use the Rx pin as wakeup irq */ |
| 2409 | dev_port->wakeup_irq = platform_get_irq(pdev, 1); |
| 2410 | if ((dev_port->wakeup_irq < 0 && !is_console)) |
| 2411 | dev_info(&pdev->dev, "No wakeup IRQ configured\n"); |
| 2412 | |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 2413 | dev_port->serial_rsc.geni_pinctrl = devm_pinctrl_get(&pdev->dev); |
| 2414 | if (IS_ERR_OR_NULL(dev_port->serial_rsc.geni_pinctrl)) { |
| 2415 | dev_err(&pdev->dev, "No pinctrl config specified!\n"); |
| 2416 | ret = PTR_ERR(dev_port->serial_rsc.geni_pinctrl); |
| 2417 | goto exit_geni_serial_probe; |
| 2418 | } |
| 2419 | dev_port->serial_rsc.geni_gpio_active = |
| 2420 | pinctrl_lookup_state(dev_port->serial_rsc.geni_pinctrl, |
| 2421 | PINCTRL_DEFAULT); |
| 2422 | if (IS_ERR_OR_NULL(dev_port->serial_rsc.geni_gpio_active)) { |
| 2423 | dev_err(&pdev->dev, "No default config specified!\n"); |
| 2424 | ret = PTR_ERR(dev_port->serial_rsc.geni_gpio_active); |
| 2425 | goto exit_geni_serial_probe; |
| 2426 | } |
Girish Mahadevan | 6d97cbc | 2017-11-07 15:28:27 -0700 | [diff] [blame] | 2427 | |
| 2428 | /* |
| 2429 | * For clients who setup an Inband wakeup, leave the GPIO pins |
| 2430 | * always connected to the core, else move the pins to their |
| 2431 | * defined "sleep" state. |
| 2432 | */ |
| 2433 | if (dev_port->wakeup_irq > 0) { |
| 2434 | dev_port->serial_rsc.geni_gpio_sleep = |
| 2435 | dev_port->serial_rsc.geni_gpio_active; |
| 2436 | } else { |
| 2437 | dev_port->serial_rsc.geni_gpio_sleep = |
| 2438 | pinctrl_lookup_state(dev_port->serial_rsc.geni_pinctrl, |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 2439 | PINCTRL_SLEEP); |
Girish Mahadevan | 6d97cbc | 2017-11-07 15:28:27 -0700 | [diff] [blame] | 2440 | if (IS_ERR_OR_NULL(dev_port->serial_rsc.geni_gpio_sleep)) { |
| 2441 | dev_err(&pdev->dev, "No sleep config specified!\n"); |
| 2442 | ret = PTR_ERR(dev_port->serial_rsc.geni_gpio_sleep); |
| 2443 | goto exit_geni_serial_probe; |
| 2444 | } |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 2445 | } |
| 2446 | |
Girish Mahadevan | 7115f4e | 2017-03-15 15:18:34 -0600 | [diff] [blame] | 2447 | wakeup_source_init(&dev_port->geni_wake, dev_name(&pdev->dev)); |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 2448 | dev_port->tx_fifo_depth = DEF_FIFO_DEPTH_WORDS; |
| 2449 | dev_port->rx_fifo_depth = DEF_FIFO_DEPTH_WORDS; |
| 2450 | dev_port->tx_fifo_width = DEF_FIFO_WIDTH_BITS; |
| 2451 | uport->fifosize = |
| 2452 | ((dev_port->tx_fifo_depth * dev_port->tx_fifo_width) >> 3); |
| 2453 | |
| 2454 | uport->irq = platform_get_irq(pdev, 0); |
| 2455 | if (uport->irq < 0) { |
| 2456 | ret = uport->irq; |
| 2457 | dev_err(&pdev->dev, "Failed to get IRQ %d\n", ret); |
| 2458 | goto exit_geni_serial_probe; |
| 2459 | } |
| 2460 | |
| 2461 | uport->private_data = (void *)drv; |
| 2462 | platform_set_drvdata(pdev, dev_port); |
Girish Mahadevan | 7115f4e | 2017-03-15 15:18:34 -0600 | [diff] [blame] | 2463 | if (is_console) { |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 2464 | dev_port->handle_rx = handle_rx_console; |
| 2465 | dev_port->rx_fifo = devm_kzalloc(uport->dev, sizeof(u32), |
| 2466 | GFP_KERNEL); |
| 2467 | } else { |
Girish Mahadevan | 33661b8 | 2017-05-16 18:59:11 -0600 | [diff] [blame] | 2468 | pm_runtime_set_suspended(&pdev->dev); |
Girish Mahadevan | c2b9252 | 2017-08-17 22:41:32 -0600 | [diff] [blame] | 2469 | pm_runtime_set_autosuspend_delay(&pdev->dev, 150); |
| 2470 | pm_runtime_use_autosuspend(&pdev->dev); |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 2471 | pm_runtime_enable(&pdev->dev); |
| 2472 | } |
| 2473 | |
| 2474 | dev_info(&pdev->dev, "Serial port%d added.FifoSize %d is_console%d\n", |
Girish Mahadevan | 7115f4e | 2017-03-15 15:18:34 -0600 | [diff] [blame] | 2475 | line, uport->fifosize, is_console); |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 2476 | device_create_file(uport->dev, &dev_attr_loopback); |
Karthikeyan Ramasubramanian | 8ec770cf | 2017-04-20 15:29:09 -0600 | [diff] [blame] | 2477 | device_create_file(uport->dev, &dev_attr_xfer_mode); |
Girish Mahadevan | efb8f6e | 2017-10-11 11:15:02 -0600 | [diff] [blame] | 2478 | msm_geni_serial_debug_init(uport, is_console); |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 2479 | dev_port->port_setup = false; |
| 2480 | return uart_add_one_port(drv, uport); |
| 2481 | |
| 2482 | exit_geni_serial_probe: |
| 2483 | return ret; |
| 2484 | } |
| 2485 | |
| 2486 | static int msm_geni_serial_remove(struct platform_device *pdev) |
| 2487 | { |
| 2488 | struct msm_geni_serial_port *port = platform_get_drvdata(pdev); |
| 2489 | struct uart_driver *drv = |
| 2490 | (struct uart_driver *)port->uport.private_data; |
| 2491 | |
Girish Mahadevan | 7115f4e | 2017-03-15 15:18:34 -0600 | [diff] [blame] | 2492 | wakeup_source_trash(&port->geni_wake); |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 2493 | uart_remove_one_port(drv, &port->uport); |
| 2494 | return 0; |
| 2495 | } |
| 2496 | |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 2497 | |
| 2498 | #ifdef CONFIG_PM |
| 2499 | static int msm_geni_serial_runtime_suspend(struct device *dev) |
| 2500 | { |
| 2501 | struct platform_device *pdev = to_platform_device(dev); |
| 2502 | struct msm_geni_serial_port *port = platform_get_drvdata(pdev); |
Girish Mahadevan | 7115f4e | 2017-03-15 15:18:34 -0600 | [diff] [blame] | 2503 | int ret = 0; |
Girish Mahadevan | 6d97cbc | 2017-11-07 15:28:27 -0700 | [diff] [blame] | 2504 | u32 uart_manual_rfr = 0; |
| 2505 | u32 geni_status = geni_read_reg_nolog(port->uport.membase, |
| 2506 | SE_GENI_STATUS); |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 2507 | |
Girish Mahadevan | 736892d | 2017-07-14 15:20:58 -0600 | [diff] [blame] | 2508 | wait_for_transfers_inflight(&port->uport); |
Girish Mahadevan | 6d97cbc | 2017-11-07 15:28:27 -0700 | [diff] [blame] | 2509 | /* |
| 2510 | * Disable Interrupt |
| 2511 | * Manual RFR On. |
| 2512 | * Stop Rx. |
| 2513 | * Resources off |
| 2514 | */ |
Girish Mahadevan | 51fc9d4 | 2017-10-12 18:37:52 -0600 | [diff] [blame] | 2515 | disable_irq(port->uport.irq); |
Girish Mahadevan | 6d97cbc | 2017-11-07 15:28:27 -0700 | [diff] [blame] | 2516 | /* |
| 2517 | * If the clients haven't done a manual flow on/off then go ahead and |
| 2518 | * set this to manual flow on. |
| 2519 | */ |
| 2520 | if (!port->manual_flow) { |
| 2521 | uart_manual_rfr |= (UART_MANUAL_RFR_EN | UART_RFR_READY); |
| 2522 | geni_write_reg_nolog(uart_manual_rfr, port->uport.membase, |
| 2523 | SE_UART_MANUAL_RFR); |
| 2524 | /* |
| 2525 | * Ensure that the manual flow on writes go through before |
| 2526 | * doing a stop_rx else we could end up flowing off the peer. |
| 2527 | */ |
| 2528 | mb(); |
| 2529 | } |
| 2530 | stop_rx_sequencer(&port->uport); |
| 2531 | if ((geni_status & M_GENI_CMD_ACTIVE)) |
| 2532 | stop_tx_sequencer(&port->uport); |
Girish Mahadevan | 7115f4e | 2017-03-15 15:18:34 -0600 | [diff] [blame] | 2533 | ret = se_geni_resources_off(&port->serial_rsc); |
| 2534 | if (ret) { |
| 2535 | dev_err(dev, "%s: Error ret %d\n", __func__, ret); |
| 2536 | goto exit_runtime_suspend; |
| 2537 | } |
Girish Mahadevan | 736892d | 2017-07-14 15:20:58 -0600 | [diff] [blame] | 2538 | if (port->wakeup_irq > 0) { |
Girish Mahadevan | 736892d | 2017-07-14 15:20:58 -0600 | [diff] [blame] | 2539 | port->edge_count = 0; |
Girish Mahadevan | 7115f4e | 2017-03-15 15:18:34 -0600 | [diff] [blame] | 2540 | enable_irq(port->wakeup_irq); |
Girish Mahadevan | 736892d | 2017-07-14 15:20:58 -0600 | [diff] [blame] | 2541 | } |
Girish Mahadevan | a4ed038 | 2017-05-12 11:25:30 -0600 | [diff] [blame] | 2542 | IPC_LOG_MSG(port->ipc_log_pwr, "%s:\n", __func__); |
Girish Mahadevan | 736892d | 2017-07-14 15:20:58 -0600 | [diff] [blame] | 2543 | __pm_relax(&port->geni_wake); |
Girish Mahadevan | 7115f4e | 2017-03-15 15:18:34 -0600 | [diff] [blame] | 2544 | exit_runtime_suspend: |
| 2545 | return ret; |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 2546 | } |
| 2547 | |
| 2548 | static int msm_geni_serial_runtime_resume(struct device *dev) |
| 2549 | { |
| 2550 | struct platform_device *pdev = to_platform_device(dev); |
| 2551 | struct msm_geni_serial_port *port = platform_get_drvdata(pdev); |
Girish Mahadevan | 7115f4e | 2017-03-15 15:18:34 -0600 | [diff] [blame] | 2552 | int ret = 0; |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 2553 | |
Girish Mahadevan | 736892d | 2017-07-14 15:20:58 -0600 | [diff] [blame] | 2554 | /* |
| 2555 | * Do an unconditional relax followed by a stay awake in case the |
| 2556 | * wake source is activated by the wakeup isr. |
| 2557 | */ |
| 2558 | __pm_relax(&port->geni_wake); |
| 2559 | __pm_stay_awake(&port->geni_wake); |
Girish Mahadevan | 7115f4e | 2017-03-15 15:18:34 -0600 | [diff] [blame] | 2560 | if (port->wakeup_irq > 0) |
| 2561 | disable_irq(port->wakeup_irq); |
Girish Mahadevan | 6d97cbc | 2017-11-07 15:28:27 -0700 | [diff] [blame] | 2562 | /* |
| 2563 | * Resources On. |
| 2564 | * Start Rx. |
| 2565 | * Auto RFR. |
| 2566 | * Enable IRQ. |
| 2567 | */ |
Girish Mahadevan | 7115f4e | 2017-03-15 15:18:34 -0600 | [diff] [blame] | 2568 | ret = se_geni_resources_on(&port->serial_rsc); |
| 2569 | if (ret) { |
| 2570 | dev_err(dev, "%s: Error ret %d\n", __func__, ret); |
Girish Mahadevan | 736892d | 2017-07-14 15:20:58 -0600 | [diff] [blame] | 2571 | __pm_relax(&port->geni_wake); |
Girish Mahadevan | 7115f4e | 2017-03-15 15:18:34 -0600 | [diff] [blame] | 2572 | goto exit_runtime_resume; |
| 2573 | } |
Girish Mahadevan | 6d97cbc | 2017-11-07 15:28:27 -0700 | [diff] [blame] | 2574 | start_rx_sequencer(&port->uport); |
| 2575 | if (!port->manual_flow) |
| 2576 | geni_write_reg_nolog(0, port->uport.membase, |
| 2577 | SE_UART_MANUAL_RFR); |
| 2578 | /* Ensure that the Rx is running before enabling interrupts */ |
| 2579 | mb(); |
Karthikeyan Ramasubramanian | 0525e57 | 2017-11-30 16:33:43 -0700 | [diff] [blame] | 2580 | if (pm_runtime_enabled(dev)) |
| 2581 | enable_irq(port->uport.irq); |
Girish Mahadevan | a4ed038 | 2017-05-12 11:25:30 -0600 | [diff] [blame] | 2582 | IPC_LOG_MSG(port->ipc_log_pwr, "%s:\n", __func__); |
Girish Mahadevan | 7115f4e | 2017-03-15 15:18:34 -0600 | [diff] [blame] | 2583 | exit_runtime_resume: |
| 2584 | return ret; |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 2585 | } |
| 2586 | |
| 2587 | static int msm_geni_serial_sys_suspend_noirq(struct device *dev) |
| 2588 | { |
| 2589 | struct platform_device *pdev = to_platform_device(dev); |
| 2590 | struct msm_geni_serial_port *port = platform_get_drvdata(pdev); |
| 2591 | struct uart_port *uport = &port->uport; |
| 2592 | |
| 2593 | if (uart_console(uport)) { |
| 2594 | uart_suspend_port((struct uart_driver *)uport->private_data, |
| 2595 | uport); |
| 2596 | } else { |
Girish Mahadevan | 736892d | 2017-07-14 15:20:58 -0600 | [diff] [blame] | 2597 | struct uart_state *state = uport->state; |
| 2598 | struct tty_port *tty_port = &state->port; |
| 2599 | |
| 2600 | mutex_lock(&tty_port->mutex); |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 2601 | if (!pm_runtime_status_suspended(dev)) { |
Girish Mahadevan | 736892d | 2017-07-14 15:20:58 -0600 | [diff] [blame] | 2602 | dev_err(dev, "%s:Active userspace vote; ioctl_cnt %d\n", |
| 2603 | __func__, port->ioctl_count); |
| 2604 | IPC_LOG_MSG(port->ipc_log_pwr, |
| 2605 | "%s:Active userspace vote; ioctl_cnt %d\n", |
| 2606 | __func__, port->ioctl_count); |
| 2607 | mutex_unlock(&tty_port->mutex); |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 2608 | return -EBUSY; |
| 2609 | } |
Girish Mahadevan | 736892d | 2017-07-14 15:20:58 -0600 | [diff] [blame] | 2610 | mutex_unlock(&tty_port->mutex); |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 2611 | } |
| 2612 | return 0; |
| 2613 | } |
| 2614 | |
| 2615 | static int msm_geni_serial_sys_resume_noirq(struct device *dev) |
| 2616 | { |
| 2617 | struct platform_device *pdev = to_platform_device(dev); |
| 2618 | struct msm_geni_serial_port *port = platform_get_drvdata(pdev); |
| 2619 | struct uart_port *uport = &port->uport; |
| 2620 | |
Karthikeyan Ramasubramanian | 29d76c2 | 2017-07-19 10:55:49 -0600 | [diff] [blame] | 2621 | if (uart_console(uport) && |
| 2622 | console_suspend_enabled && uport->suspended) { |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 2623 | uart_resume_port((struct uart_driver *)uport->private_data, |
| 2624 | uport); |
Karthikeyan Ramasubramanian | 53157c5 | 2017-08-11 17:06:24 -0600 | [diff] [blame] | 2625 | disable_irq(uport->irq); |
Girish Mahadevan | b1ab172 | 2017-04-27 16:39:11 -0600 | [diff] [blame] | 2626 | } |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 2627 | return 0; |
| 2628 | } |
| 2629 | #else |
| 2630 | static int msm_geni_serial_runtime_suspend(struct device *dev) |
| 2631 | { |
| 2632 | return 0; |
| 2633 | } |
| 2634 | |
| 2635 | static int msm_geni_serial_runtime_resume(struct device *dev) |
| 2636 | { |
| 2637 | return 0; |
| 2638 | } |
| 2639 | |
| 2640 | static int msm_geni_serial_sys_suspend_noirq(struct device *dev) |
| 2641 | { |
| 2642 | return 0; |
| 2643 | } |
| 2644 | |
| 2645 | static int msm_geni_serial_sys_resume_noirq(struct device *dev) |
| 2646 | { |
| 2647 | return 0; |
| 2648 | } |
| 2649 | #endif |
| 2650 | |
| 2651 | static const struct dev_pm_ops msm_geni_serial_pm_ops = { |
| 2652 | .runtime_suspend = msm_geni_serial_runtime_suspend, |
| 2653 | .runtime_resume = msm_geni_serial_runtime_resume, |
| 2654 | .suspend_noirq = msm_geni_serial_sys_suspend_noirq, |
| 2655 | .resume_noirq = msm_geni_serial_sys_resume_noirq, |
| 2656 | }; |
| 2657 | |
| 2658 | static const struct of_device_id msm_geni_serial_match_table[] = { |
| 2659 | { .compatible = "qcom,msm-geni-uart"}, |
| 2660 | {}, |
| 2661 | }; |
| 2662 | |
| 2663 | static struct platform_driver msm_geni_serial_platform_driver = { |
| 2664 | .remove = msm_geni_serial_remove, |
| 2665 | .probe = msm_geni_serial_probe, |
| 2666 | .driver = { |
| 2667 | .name = "msm_geni_serial", |
| 2668 | .of_match_table = msm_geni_serial_match_table, |
| 2669 | .pm = &msm_geni_serial_pm_ops, |
| 2670 | }, |
| 2671 | }; |
| 2672 | |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 2673 | |
| 2674 | static struct uart_driver msm_geni_serial_hs_driver = { |
| 2675 | .owner = THIS_MODULE, |
| 2676 | .driver_name = "msm_geni_serial_hs", |
| 2677 | .dev_name = "ttyHS", |
| 2678 | .nr = GENI_UART_NR_PORTS, |
| 2679 | }; |
| 2680 | |
| 2681 | static int __init msm_geni_serial_init(void) |
| 2682 | { |
| 2683 | int ret = 0; |
| 2684 | int i; |
| 2685 | |
| 2686 | for (i = 0; i < GENI_UART_NR_PORTS; i++) { |
| 2687 | msm_geni_serial_ports[i].uport.iotype = UPIO_MEM; |
| 2688 | msm_geni_serial_ports[i].uport.ops = &msm_geni_serial_pops; |
| 2689 | msm_geni_serial_ports[i].uport.flags = UPF_BOOT_AUTOCONF; |
| 2690 | msm_geni_serial_ports[i].uport.line = i; |
| 2691 | } |
| 2692 | |
Girish Mahadevan | 7115f4e | 2017-03-15 15:18:34 -0600 | [diff] [blame] | 2693 | for (i = 0; i < GENI_UART_CONS_PORTS; i++) { |
| 2694 | msm_geni_console_port.uport.iotype = UPIO_MEM; |
| 2695 | msm_geni_console_port.uport.ops = &msm_geni_console_pops; |
| 2696 | msm_geni_console_port.uport.flags = UPF_BOOT_AUTOCONF; |
| 2697 | msm_geni_console_port.uport.line = i; |
| 2698 | } |
| 2699 | |
Girish Mahadevan | f08b110 | 2017-04-02 19:27:28 -0600 | [diff] [blame] | 2700 | ret = console_register(&msm_geni_console_driver); |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 2701 | if (ret) |
| 2702 | return ret; |
| 2703 | |
| 2704 | ret = uart_register_driver(&msm_geni_serial_hs_driver); |
| 2705 | if (ret) { |
| 2706 | uart_unregister_driver(&msm_geni_console_driver); |
| 2707 | return ret; |
| 2708 | } |
| 2709 | |
| 2710 | ret = platform_driver_register(&msm_geni_serial_platform_driver); |
| 2711 | if (ret) { |
Girish Mahadevan | f08b110 | 2017-04-02 19:27:28 -0600 | [diff] [blame] | 2712 | console_unregister(&msm_geni_console_driver); |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 2713 | uart_unregister_driver(&msm_geni_serial_hs_driver); |
| 2714 | return ret; |
| 2715 | } |
| 2716 | |
| 2717 | pr_info("%s: Driver initialized", __func__); |
| 2718 | return ret; |
| 2719 | } |
| 2720 | module_init(msm_geni_serial_init); |
| 2721 | |
| 2722 | static void __exit msm_geni_serial_exit(void) |
| 2723 | { |
| 2724 | platform_driver_unregister(&msm_geni_serial_platform_driver); |
| 2725 | uart_unregister_driver(&msm_geni_serial_hs_driver); |
Girish Mahadevan | f08b110 | 2017-04-02 19:27:28 -0600 | [diff] [blame] | 2726 | console_unregister(&msm_geni_console_driver); |
Girish Mahadevan | ebeed35 | 2016-11-23 10:59:29 -0700 | [diff] [blame] | 2727 | } |
| 2728 | module_exit(msm_geni_serial_exit); |
| 2729 | |
| 2730 | MODULE_DESCRIPTION("Serial driver for GENI based QTI serial cores"); |
| 2731 | MODULE_LICENSE("GPL v2"); |
| 2732 | MODULE_ALIAS("tty:msm_geni_geni_serial"); |