Vinod Koul | 2f52a51 | 2017-12-14 11:19:41 +0530 | [diff] [blame] | 1 | // SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause) |
| 2 | // Copyright(c) 2015-17 Intel Corporation. |
| 3 | |
| 4 | /* |
| 5 | * Cadence SoundWire Master module |
| 6 | * Used by Master driver |
| 7 | */ |
| 8 | |
| 9 | #include <linux/delay.h> |
| 10 | #include <linux/device.h> |
Pierre-Louis Bossart | aa85066 | 2019-08-21 13:58:19 -0500 | [diff] [blame] | 11 | #include <linux/debugfs.h> |
Vinod Koul | 2f52a51 | 2017-12-14 11:19:41 +0530 | [diff] [blame] | 12 | #include <linux/interrupt.h> |
Jan Kotas | 18de65d | 2019-04-04 09:12:21 +0100 | [diff] [blame] | 13 | #include <linux/io.h> |
Vinod Koul | 2f52a51 | 2017-12-14 11:19:41 +0530 | [diff] [blame] | 14 | #include <linux/module.h> |
| 15 | #include <linux/mod_devicetable.h> |
| 16 | #include <linux/soundwire/sdw_registers.h> |
| 17 | #include <linux/soundwire/sdw.h> |
Vinod Koul | 5d6b3c8 | 2018-04-26 18:38:53 +0530 | [diff] [blame] | 18 | #include <sound/pcm_params.h> |
| 19 | #include <sound/soc.h> |
Vinod Koul | 2f52a51 | 2017-12-14 11:19:41 +0530 | [diff] [blame] | 20 | #include "bus.h" |
| 21 | #include "cadence_master.h" |
| 22 | |
Pierre-Louis Bossart | 04592dc | 2019-08-05 19:55:21 -0500 | [diff] [blame] | 23 | static int interrupt_mask; |
| 24 | module_param_named(cnds_mcp_int_mask, interrupt_mask, int, 0444); |
| 25 | MODULE_PARM_DESC(cdns_mcp_int_mask, "Cadence MCP IntMask"); |
| 26 | |
Vinod Koul | 2f52a51 | 2017-12-14 11:19:41 +0530 | [diff] [blame] | 27 | #define CDNS_MCP_CONFIG 0x0 |
| 28 | |
| 29 | #define CDNS_MCP_CONFIG_MCMD_RETRY GENMASK(27, 24) |
| 30 | #define CDNS_MCP_CONFIG_MPREQ_DELAY GENMASK(20, 16) |
| 31 | #define CDNS_MCP_CONFIG_MMASTER BIT(7) |
| 32 | #define CDNS_MCP_CONFIG_BUS_REL BIT(6) |
| 33 | #define CDNS_MCP_CONFIG_SNIFFER BIT(5) |
| 34 | #define CDNS_MCP_CONFIG_SSPMOD BIT(4) |
| 35 | #define CDNS_MCP_CONFIG_CMD BIT(3) |
| 36 | #define CDNS_MCP_CONFIG_OP GENMASK(2, 0) |
| 37 | #define CDNS_MCP_CONFIG_OP_NORMAL 0 |
| 38 | |
| 39 | #define CDNS_MCP_CONTROL 0x4 |
| 40 | |
| 41 | #define CDNS_MCP_CONTROL_RST_DELAY GENMASK(10, 8) |
| 42 | #define CDNS_MCP_CONTROL_CMD_RST BIT(7) |
| 43 | #define CDNS_MCP_CONTROL_SOFT_RST BIT(6) |
| 44 | #define CDNS_MCP_CONTROL_SW_RST BIT(5) |
| 45 | #define CDNS_MCP_CONTROL_HW_RST BIT(4) |
| 46 | #define CDNS_MCP_CONTROL_CLK_PAUSE BIT(3) |
| 47 | #define CDNS_MCP_CONTROL_CLK_STOP_CLR BIT(2) |
| 48 | #define CDNS_MCP_CONTROL_CMD_ACCEPT BIT(1) |
| 49 | #define CDNS_MCP_CONTROL_BLOCK_WAKEUP BIT(0) |
| 50 | |
Vinod Koul | 2f52a51 | 2017-12-14 11:19:41 +0530 | [diff] [blame] | 51 | #define CDNS_MCP_CMDCTRL 0x8 |
| 52 | #define CDNS_MCP_SSPSTAT 0xC |
| 53 | #define CDNS_MCP_FRAME_SHAPE 0x10 |
| 54 | #define CDNS_MCP_FRAME_SHAPE_INIT 0x14 |
Pierre-Louis Bossart | 05be59a | 2019-08-05 19:55:11 -0500 | [diff] [blame] | 55 | #define CDNS_MCP_FRAME_SHAPE_COL_MASK GENMASK(2, 0) |
| 56 | #define CDNS_MCP_FRAME_SHAPE_ROW_OFFSET 3 |
Vinod Koul | 2f52a51 | 2017-12-14 11:19:41 +0530 | [diff] [blame] | 57 | |
| 58 | #define CDNS_MCP_CONFIG_UPDATE 0x18 |
| 59 | #define CDNS_MCP_CONFIG_UPDATE_BIT BIT(0) |
| 60 | |
| 61 | #define CDNS_MCP_PHYCTRL 0x1C |
| 62 | #define CDNS_MCP_SSP_CTRL0 0x20 |
| 63 | #define CDNS_MCP_SSP_CTRL1 0x28 |
| 64 | #define CDNS_MCP_CLK_CTRL0 0x30 |
| 65 | #define CDNS_MCP_CLK_CTRL1 0x38 |
Rander Wang | a50954e | 2019-08-05 19:55:18 -0500 | [diff] [blame] | 66 | #define CDNS_MCP_CLK_MCLKD_MASK GENMASK(7, 0) |
Vinod Koul | 2f52a51 | 2017-12-14 11:19:41 +0530 | [diff] [blame] | 67 | |
| 68 | #define CDNS_MCP_STAT 0x40 |
| 69 | |
| 70 | #define CDNS_MCP_STAT_ACTIVE_BANK BIT(20) |
| 71 | #define CDNS_MCP_STAT_CLK_STOP BIT(16) |
| 72 | |
| 73 | #define CDNS_MCP_INTSTAT 0x44 |
| 74 | #define CDNS_MCP_INTMASK 0x48 |
| 75 | |
| 76 | #define CDNS_MCP_INT_IRQ BIT(31) |
Pierre-Louis Bossart | a2cff9e | 2020-01-10 15:57:26 -0600 | [diff] [blame] | 77 | #define CDNS_MCP_INT_RESERVED1 GENMASK(30, 17) |
Vinod Koul | 2f52a51 | 2017-12-14 11:19:41 +0530 | [diff] [blame] | 78 | #define CDNS_MCP_INT_WAKEUP BIT(16) |
| 79 | #define CDNS_MCP_INT_SLAVE_RSVD BIT(15) |
| 80 | #define CDNS_MCP_INT_SLAVE_ALERT BIT(14) |
| 81 | #define CDNS_MCP_INT_SLAVE_ATTACH BIT(13) |
| 82 | #define CDNS_MCP_INT_SLAVE_NATTACH BIT(12) |
| 83 | #define CDNS_MCP_INT_SLAVE_MASK GENMASK(15, 12) |
| 84 | #define CDNS_MCP_INT_DPINT BIT(11) |
| 85 | #define CDNS_MCP_INT_CTRL_CLASH BIT(10) |
| 86 | #define CDNS_MCP_INT_DATA_CLASH BIT(9) |
Pierre-Louis Bossart | 9b5884a | 2019-08-05 19:55:08 -0500 | [diff] [blame] | 87 | #define CDNS_MCP_INT_PARITY BIT(8) |
Vinod Koul | 2f52a51 | 2017-12-14 11:19:41 +0530 | [diff] [blame] | 88 | #define CDNS_MCP_INT_CMD_ERR BIT(7) |
Pierre-Louis Bossart | a2cff9e | 2020-01-10 15:57:26 -0600 | [diff] [blame] | 89 | #define CDNS_MCP_INT_RESERVED2 GENMASK(6, 4) |
Pierre-Louis Bossart | 9b5884a | 2019-08-05 19:55:08 -0500 | [diff] [blame] | 90 | #define CDNS_MCP_INT_RX_NE BIT(3) |
Vinod Koul | 2f52a51 | 2017-12-14 11:19:41 +0530 | [diff] [blame] | 91 | #define CDNS_MCP_INT_RX_WL BIT(2) |
| 92 | #define CDNS_MCP_INT_TXE BIT(1) |
Pierre-Louis Bossart | 9b5884a | 2019-08-05 19:55:08 -0500 | [diff] [blame] | 93 | #define CDNS_MCP_INT_TXF BIT(0) |
Pierre-Louis Bossart | a2cff9e | 2020-01-10 15:57:26 -0600 | [diff] [blame] | 94 | #define CDNS_MCP_INT_RESERVED (CDNS_MCP_INT_RESERVED1 | CDNS_MCP_INT_RESERVED2) |
Vinod Koul | 2f52a51 | 2017-12-14 11:19:41 +0530 | [diff] [blame] | 95 | |
| 96 | #define CDNS_MCP_INTSET 0x4C |
| 97 | |
Pierre-Louis Bossart | b07dd9b | 2019-07-25 18:40:05 -0500 | [diff] [blame] | 98 | #define CDNS_MCP_SLAVE_STAT 0x50 |
| 99 | #define CDNS_MCP_SLAVE_STAT_MASK GENMASK(1, 0) |
Vinod Koul | 2f52a51 | 2017-12-14 11:19:41 +0530 | [diff] [blame] | 100 | |
| 101 | #define CDNS_MCP_SLAVE_INTSTAT0 0x54 |
| 102 | #define CDNS_MCP_SLAVE_INTSTAT1 0x58 |
| 103 | #define CDNS_MCP_SLAVE_INTSTAT_NPRESENT BIT(0) |
| 104 | #define CDNS_MCP_SLAVE_INTSTAT_ATTACHED BIT(1) |
| 105 | #define CDNS_MCP_SLAVE_INTSTAT_ALERT BIT(2) |
| 106 | #define CDNS_MCP_SLAVE_INTSTAT_RESERVED BIT(3) |
| 107 | #define CDNS_MCP_SLAVE_STATUS_BITS GENMASK(3, 0) |
| 108 | #define CDNS_MCP_SLAVE_STATUS_NUM 4 |
| 109 | |
| 110 | #define CDNS_MCP_SLAVE_INTMASK0 0x5C |
| 111 | #define CDNS_MCP_SLAVE_INTMASK1 0x60 |
| 112 | |
Pierre-Louis Bossart | 664b165 | 2019-07-25 18:40:06 -0500 | [diff] [blame] | 113 | #define CDNS_MCP_SLAVE_INTMASK0_MASK GENMASK(31, 0) |
| 114 | #define CDNS_MCP_SLAVE_INTMASK1_MASK GENMASK(15, 0) |
Vinod Koul | 2f52a51 | 2017-12-14 11:19:41 +0530 | [diff] [blame] | 115 | |
| 116 | #define CDNS_MCP_PORT_INTSTAT 0x64 |
| 117 | #define CDNS_MCP_PDI_STAT 0x6C |
| 118 | |
| 119 | #define CDNS_MCP_FIFOLEVEL 0x78 |
| 120 | #define CDNS_MCP_FIFOSTAT 0x7C |
| 121 | #define CDNS_MCP_RX_FIFO_AVAIL GENMASK(5, 0) |
| 122 | |
| 123 | #define CDNS_MCP_CMD_BASE 0x80 |
| 124 | #define CDNS_MCP_RESP_BASE 0x80 |
| 125 | #define CDNS_MCP_CMD_LEN 0x20 |
| 126 | #define CDNS_MCP_CMD_WORD_LEN 0x4 |
| 127 | |
| 128 | #define CDNS_MCP_CMD_SSP_TAG BIT(31) |
| 129 | #define CDNS_MCP_CMD_COMMAND GENMASK(30, 28) |
| 130 | #define CDNS_MCP_CMD_DEV_ADDR GENMASK(27, 24) |
| 131 | #define CDNS_MCP_CMD_REG_ADDR_H GENMASK(23, 16) |
| 132 | #define CDNS_MCP_CMD_REG_ADDR_L GENMASK(15, 8) |
| 133 | #define CDNS_MCP_CMD_REG_DATA GENMASK(7, 0) |
| 134 | |
| 135 | #define CDNS_MCP_CMD_READ 2 |
| 136 | #define CDNS_MCP_CMD_WRITE 3 |
| 137 | |
| 138 | #define CDNS_MCP_RESP_RDATA GENMASK(15, 8) |
| 139 | #define CDNS_MCP_RESP_ACK BIT(0) |
| 140 | #define CDNS_MCP_RESP_NACK BIT(1) |
| 141 | |
| 142 | #define CDNS_DP_SIZE 128 |
| 143 | |
| 144 | #define CDNS_DPN_B0_CONFIG(n) (0x100 + CDNS_DP_SIZE * (n)) |
| 145 | #define CDNS_DPN_B0_CH_EN(n) (0x104 + CDNS_DP_SIZE * (n)) |
| 146 | #define CDNS_DPN_B0_SAMPLE_CTRL(n) (0x108 + CDNS_DP_SIZE * (n)) |
| 147 | #define CDNS_DPN_B0_OFFSET_CTRL(n) (0x10C + CDNS_DP_SIZE * (n)) |
| 148 | #define CDNS_DPN_B0_HCTRL(n) (0x110 + CDNS_DP_SIZE * (n)) |
| 149 | #define CDNS_DPN_B0_ASYNC_CTRL(n) (0x114 + CDNS_DP_SIZE * (n)) |
| 150 | |
| 151 | #define CDNS_DPN_B1_CONFIG(n) (0x118 + CDNS_DP_SIZE * (n)) |
| 152 | #define CDNS_DPN_B1_CH_EN(n) (0x11C + CDNS_DP_SIZE * (n)) |
| 153 | #define CDNS_DPN_B1_SAMPLE_CTRL(n) (0x120 + CDNS_DP_SIZE * (n)) |
| 154 | #define CDNS_DPN_B1_OFFSET_CTRL(n) (0x124 + CDNS_DP_SIZE * (n)) |
| 155 | #define CDNS_DPN_B1_HCTRL(n) (0x128 + CDNS_DP_SIZE * (n)) |
| 156 | #define CDNS_DPN_B1_ASYNC_CTRL(n) (0x12C + CDNS_DP_SIZE * (n)) |
| 157 | |
| 158 | #define CDNS_DPN_CONFIG_BPM BIT(18) |
| 159 | #define CDNS_DPN_CONFIG_BGC GENMASK(17, 16) |
| 160 | #define CDNS_DPN_CONFIG_WL GENMASK(12, 8) |
| 161 | #define CDNS_DPN_CONFIG_PORT_DAT GENMASK(3, 2) |
| 162 | #define CDNS_DPN_CONFIG_PORT_FLOW GENMASK(1, 0) |
| 163 | |
| 164 | #define CDNS_DPN_SAMPLE_CTRL_SI GENMASK(15, 0) |
| 165 | |
| 166 | #define CDNS_DPN_OFFSET_CTRL_1 GENMASK(7, 0) |
| 167 | #define CDNS_DPN_OFFSET_CTRL_2 GENMASK(15, 8) |
| 168 | |
| 169 | #define CDNS_DPN_HCTRL_HSTOP GENMASK(3, 0) |
| 170 | #define CDNS_DPN_HCTRL_HSTART GENMASK(7, 4) |
| 171 | #define CDNS_DPN_HCTRL_LCTRL GENMASK(10, 8) |
| 172 | |
| 173 | #define CDNS_PORTCTRL 0x130 |
| 174 | #define CDNS_PORTCTRL_DIRN BIT(7) |
| 175 | #define CDNS_PORTCTRL_BANK_INVERT BIT(8) |
| 176 | |
| 177 | #define CDNS_PORT_OFFSET 0x80 |
| 178 | |
| 179 | #define CDNS_PDI_CONFIG(n) (0x1100 + (n) * 16) |
| 180 | |
| 181 | #define CDNS_PDI_CONFIG_SOFT_RESET BIT(24) |
| 182 | #define CDNS_PDI_CONFIG_CHANNEL GENMASK(15, 8) |
| 183 | #define CDNS_PDI_CONFIG_PORT GENMASK(4, 0) |
| 184 | |
| 185 | /* Driver defaults */ |
Vinod Koul | 2f52a51 | 2017-12-14 11:19:41 +0530 | [diff] [blame] | 186 | #define CDNS_DEFAULT_SSP_INTERVAL 0x18 |
| 187 | #define CDNS_TX_TIMEOUT 2000 |
| 188 | |
Vinod Koul | 2f52a51 | 2017-12-14 11:19:41 +0530 | [diff] [blame] | 189 | #define CDNS_SCP_RX_FIFOLEVEL 0x2 |
| 190 | |
| 191 | /* |
| 192 | * register accessor helpers |
| 193 | */ |
| 194 | static inline u32 cdns_readl(struct sdw_cdns *cdns, int offset) |
| 195 | { |
| 196 | return readl(cdns->registers + offset); |
| 197 | } |
| 198 | |
| 199 | static inline void cdns_writel(struct sdw_cdns *cdns, int offset, u32 value) |
| 200 | { |
| 201 | writel(value, cdns->registers + offset); |
| 202 | } |
| 203 | |
| 204 | static inline void cdns_updatel(struct sdw_cdns *cdns, |
| 205 | int offset, u32 mask, u32 val) |
| 206 | { |
| 207 | u32 tmp; |
| 208 | |
| 209 | tmp = cdns_readl(cdns, offset); |
| 210 | tmp = (tmp & ~mask) | val; |
| 211 | cdns_writel(cdns, offset, tmp); |
| 212 | } |
| 213 | |
| 214 | static int cdns_clear_bit(struct sdw_cdns *cdns, int offset, u32 value) |
| 215 | { |
| 216 | int timeout = 10; |
| 217 | u32 reg_read; |
| 218 | |
| 219 | writel(value, cdns->registers + offset); |
| 220 | |
| 221 | /* Wait for bit to be self cleared */ |
| 222 | do { |
| 223 | reg_read = readl(cdns->registers + offset); |
| 224 | if ((reg_read & value) == 0) |
| 225 | return 0; |
| 226 | |
| 227 | timeout--; |
| 228 | udelay(50); |
| 229 | } while (timeout != 0); |
| 230 | |
| 231 | return -EAGAIN; |
| 232 | } |
| 233 | |
| 234 | /* |
Pierre-Louis Bossart | 49ea07d | 2019-10-22 18:54:44 -0500 | [diff] [blame] | 235 | * all changes to the MCP_CONFIG, MCP_CONTROL, MCP_CMDCTRL and MCP_PHYCTRL |
| 236 | * need to be confirmed with a write to MCP_CONFIG_UPDATE |
| 237 | */ |
| 238 | static int cdns_update_config(struct sdw_cdns *cdns) |
| 239 | { |
| 240 | int ret; |
| 241 | |
| 242 | ret = cdns_clear_bit(cdns, CDNS_MCP_CONFIG_UPDATE, |
| 243 | CDNS_MCP_CONFIG_UPDATE_BIT); |
| 244 | if (ret < 0) |
| 245 | dev_err(cdns->dev, "Config update timedout\n"); |
| 246 | |
| 247 | return ret; |
| 248 | } |
| 249 | |
| 250 | /* |
Pierre-Louis Bossart | aa85066 | 2019-08-21 13:58:19 -0500 | [diff] [blame] | 251 | * debugfs |
| 252 | */ |
| 253 | #ifdef CONFIG_DEBUG_FS |
| 254 | |
| 255 | #define RD_BUF (2 * PAGE_SIZE) |
| 256 | |
| 257 | static ssize_t cdns_sprintf(struct sdw_cdns *cdns, |
| 258 | char *buf, size_t pos, unsigned int reg) |
| 259 | { |
| 260 | return scnprintf(buf + pos, RD_BUF - pos, |
| 261 | "%4x\t%8x\n", reg, cdns_readl(cdns, reg)); |
| 262 | } |
| 263 | |
| 264 | static int cdns_reg_show(struct seq_file *s, void *data) |
| 265 | { |
| 266 | struct sdw_cdns *cdns = s->private; |
| 267 | char *buf; |
| 268 | ssize_t ret; |
| 269 | int num_ports; |
| 270 | int i, j; |
| 271 | |
| 272 | buf = kzalloc(RD_BUF, GFP_KERNEL); |
| 273 | if (!buf) |
| 274 | return -ENOMEM; |
| 275 | |
| 276 | ret = scnprintf(buf, RD_BUF, "Register Value\n"); |
| 277 | ret += scnprintf(buf + ret, RD_BUF - ret, "\nMCP Registers\n"); |
| 278 | /* 8 MCP registers */ |
| 279 | for (i = CDNS_MCP_CONFIG; i <= CDNS_MCP_PHYCTRL; i += sizeof(u32)) |
| 280 | ret += cdns_sprintf(cdns, buf, ret, i); |
| 281 | |
| 282 | ret += scnprintf(buf + ret, RD_BUF - ret, |
| 283 | "\nStatus & Intr Registers\n"); |
| 284 | /* 13 Status & Intr registers (offsets 0x70 and 0x74 not defined) */ |
| 285 | for (i = CDNS_MCP_STAT; i <= CDNS_MCP_FIFOSTAT; i += sizeof(u32)) |
| 286 | ret += cdns_sprintf(cdns, buf, ret, i); |
| 287 | |
| 288 | ret += scnprintf(buf + ret, RD_BUF - ret, |
| 289 | "\nSSP & Clk ctrl Registers\n"); |
| 290 | ret += cdns_sprintf(cdns, buf, ret, CDNS_MCP_SSP_CTRL0); |
| 291 | ret += cdns_sprintf(cdns, buf, ret, CDNS_MCP_SSP_CTRL1); |
| 292 | ret += cdns_sprintf(cdns, buf, ret, CDNS_MCP_CLK_CTRL0); |
| 293 | ret += cdns_sprintf(cdns, buf, ret, CDNS_MCP_CLK_CTRL1); |
| 294 | |
| 295 | ret += scnprintf(buf + ret, RD_BUF - ret, |
| 296 | "\nDPn B0 Registers\n"); |
| 297 | |
Pierre-Louis Bossart | 807c15b | 2019-09-16 14:23:47 -0500 | [diff] [blame] | 298 | num_ports = cdns->num_ports; |
Pierre-Louis Bossart | aa85066 | 2019-08-21 13:58:19 -0500 | [diff] [blame] | 299 | |
| 300 | for (i = 0; i < num_ports; i++) { |
| 301 | ret += scnprintf(buf + ret, RD_BUF - ret, |
| 302 | "\nDP-%d\n", i); |
| 303 | for (j = CDNS_DPN_B0_CONFIG(i); |
| 304 | j < CDNS_DPN_B0_ASYNC_CTRL(i); j += sizeof(u32)) |
| 305 | ret += cdns_sprintf(cdns, buf, ret, j); |
| 306 | } |
| 307 | |
| 308 | ret += scnprintf(buf + ret, RD_BUF - ret, |
| 309 | "\nDPn B1 Registers\n"); |
| 310 | for (i = 0; i < num_ports; i++) { |
| 311 | ret += scnprintf(buf + ret, RD_BUF - ret, |
| 312 | "\nDP-%d\n", i); |
| 313 | |
| 314 | for (j = CDNS_DPN_B1_CONFIG(i); |
| 315 | j < CDNS_DPN_B1_ASYNC_CTRL(i); j += sizeof(u32)) |
| 316 | ret += cdns_sprintf(cdns, buf, ret, j); |
| 317 | } |
| 318 | |
| 319 | ret += scnprintf(buf + ret, RD_BUF - ret, |
| 320 | "\nDPn Control Registers\n"); |
| 321 | for (i = 0; i < num_ports; i++) |
| 322 | ret += cdns_sprintf(cdns, buf, ret, |
| 323 | CDNS_PORTCTRL + i * CDNS_PORT_OFFSET); |
| 324 | |
| 325 | ret += scnprintf(buf + ret, RD_BUF - ret, |
| 326 | "\nPDIn Config Registers\n"); |
| 327 | |
| 328 | /* number of PDI and ports is interchangeable */ |
| 329 | for (i = 0; i < num_ports; i++) |
| 330 | ret += cdns_sprintf(cdns, buf, ret, CDNS_PDI_CONFIG(i)); |
| 331 | |
| 332 | seq_printf(s, "%s", buf); |
| 333 | kfree(buf); |
| 334 | |
| 335 | return 0; |
| 336 | } |
| 337 | DEFINE_SHOW_ATTRIBUTE(cdns_reg); |
| 338 | |
Pierre-Louis Bossart | 675d4c9 | 2019-10-22 18:54:45 -0500 | [diff] [blame] | 339 | static int cdns_hw_reset(void *data, u64 value) |
| 340 | { |
| 341 | struct sdw_cdns *cdns = data; |
| 342 | int ret; |
| 343 | |
| 344 | if (value != 1) |
| 345 | return -EINVAL; |
| 346 | |
| 347 | /* Userspace changed the hardware state behind the kernel's back */ |
| 348 | add_taint(TAINT_USER, LOCKDEP_STILL_OK); |
| 349 | |
| 350 | ret = sdw_cdns_exit_reset(cdns); |
| 351 | |
| 352 | dev_dbg(cdns->dev, "link hw_reset done: %d\n", ret); |
| 353 | |
| 354 | return ret; |
| 355 | } |
| 356 | |
| 357 | DEFINE_DEBUGFS_ATTRIBUTE(cdns_hw_reset_fops, NULL, cdns_hw_reset, "%llu\n"); |
| 358 | |
Pierre-Louis Bossart | aa85066 | 2019-08-21 13:58:19 -0500 | [diff] [blame] | 359 | /** |
| 360 | * sdw_cdns_debugfs_init() - Cadence debugfs init |
| 361 | * @cdns: Cadence instance |
| 362 | * @root: debugfs root |
| 363 | */ |
| 364 | void sdw_cdns_debugfs_init(struct sdw_cdns *cdns, struct dentry *root) |
| 365 | { |
| 366 | debugfs_create_file("cdns-registers", 0400, root, cdns, &cdns_reg_fops); |
Pierre-Louis Bossart | 675d4c9 | 2019-10-22 18:54:45 -0500 | [diff] [blame] | 367 | |
| 368 | debugfs_create_file("cdns-hw-reset", 0200, root, cdns, |
| 369 | &cdns_hw_reset_fops); |
Pierre-Louis Bossart | aa85066 | 2019-08-21 13:58:19 -0500 | [diff] [blame] | 370 | } |
| 371 | EXPORT_SYMBOL_GPL(sdw_cdns_debugfs_init); |
| 372 | |
| 373 | #endif /* CONFIG_DEBUG_FS */ |
| 374 | |
| 375 | /* |
Sanyog Kale | 956baa1 | 2017-12-14 11:19:42 +0530 | [diff] [blame] | 376 | * IO Calls |
| 377 | */ |
Pierre-Louis Bossart | bbb6381 | 2019-05-01 10:57:41 -0500 | [diff] [blame] | 378 | static enum sdw_command_response |
| 379 | cdns_fill_msg_resp(struct sdw_cdns *cdns, |
| 380 | struct sdw_msg *msg, int count, int offset) |
Sanyog Kale | 956baa1 | 2017-12-14 11:19:42 +0530 | [diff] [blame] | 381 | { |
| 382 | int nack = 0, no_ack = 0; |
| 383 | int i; |
| 384 | |
| 385 | /* check message response */ |
| 386 | for (i = 0; i < count; i++) { |
| 387 | if (!(cdns->response_buf[i] & CDNS_MCP_RESP_ACK)) { |
| 388 | no_ack = 1; |
Pierre-Louis Bossart | eb7df4c | 2019-05-22 14:47:26 -0500 | [diff] [blame] | 389 | dev_dbg_ratelimited(cdns->dev, "Msg Ack not received\n"); |
Sanyog Kale | 956baa1 | 2017-12-14 11:19:42 +0530 | [diff] [blame] | 390 | if (cdns->response_buf[i] & CDNS_MCP_RESP_NACK) { |
| 391 | nack = 1; |
Pierre-Louis Bossart | eb7df4c | 2019-05-22 14:47:26 -0500 | [diff] [blame] | 392 | dev_err_ratelimited(cdns->dev, "Msg NACK received\n"); |
Sanyog Kale | 956baa1 | 2017-12-14 11:19:42 +0530 | [diff] [blame] | 393 | } |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | if (nack) { |
Pierre-Louis Bossart | eb7df4c | 2019-05-22 14:47:26 -0500 | [diff] [blame] | 398 | dev_err_ratelimited(cdns->dev, "Msg NACKed for Slave %d\n", msg->dev_num); |
Sanyog Kale | 956baa1 | 2017-12-14 11:19:42 +0530 | [diff] [blame] | 399 | return SDW_CMD_FAIL; |
| 400 | } else if (no_ack) { |
Pierre-Louis Bossart | eb7df4c | 2019-05-22 14:47:26 -0500 | [diff] [blame] | 401 | dev_dbg_ratelimited(cdns->dev, "Msg ignored for Slave %d\n", msg->dev_num); |
Sanyog Kale | 956baa1 | 2017-12-14 11:19:42 +0530 | [diff] [blame] | 402 | return SDW_CMD_IGNORED; |
| 403 | } |
| 404 | |
| 405 | /* fill response */ |
| 406 | for (i = 0; i < count; i++) |
| 407 | msg->buf[i + offset] = cdns->response_buf[i] >> |
| 408 | SDW_REG_SHIFT(CDNS_MCP_RESP_RDATA); |
| 409 | |
| 410 | return SDW_CMD_OK; |
| 411 | } |
| 412 | |
| 413 | static enum sdw_command_response |
| 414 | _cdns_xfer_msg(struct sdw_cdns *cdns, struct sdw_msg *msg, int cmd, |
Pierre-Louis Bossart | bbb6381 | 2019-05-01 10:57:41 -0500 | [diff] [blame] | 415 | int offset, int count, bool defer) |
Sanyog Kale | 956baa1 | 2017-12-14 11:19:42 +0530 | [diff] [blame] | 416 | { |
| 417 | unsigned long time; |
| 418 | u32 base, i, data; |
| 419 | u16 addr; |
| 420 | |
| 421 | /* Program the watermark level for RX FIFO */ |
| 422 | if (cdns->msg_count != count) { |
| 423 | cdns_writel(cdns, CDNS_MCP_FIFOLEVEL, count); |
| 424 | cdns->msg_count = count; |
| 425 | } |
| 426 | |
| 427 | base = CDNS_MCP_CMD_BASE; |
| 428 | addr = msg->addr; |
| 429 | |
| 430 | for (i = 0; i < count; i++) { |
| 431 | data = msg->dev_num << SDW_REG_SHIFT(CDNS_MCP_CMD_DEV_ADDR); |
| 432 | data |= cmd << SDW_REG_SHIFT(CDNS_MCP_CMD_COMMAND); |
| 433 | data |= addr++ << SDW_REG_SHIFT(CDNS_MCP_CMD_REG_ADDR_L); |
| 434 | |
| 435 | if (msg->flags == SDW_MSG_FLAG_WRITE) |
| 436 | data |= msg->buf[i + offset]; |
| 437 | |
| 438 | data |= msg->ssp_sync << SDW_REG_SHIFT(CDNS_MCP_CMD_SSP_TAG); |
| 439 | cdns_writel(cdns, base, data); |
| 440 | base += CDNS_MCP_CMD_WORD_LEN; |
| 441 | } |
| 442 | |
| 443 | if (defer) |
| 444 | return SDW_CMD_OK; |
| 445 | |
| 446 | /* wait for timeout or response */ |
| 447 | time = wait_for_completion_timeout(&cdns->tx_complete, |
Pierre-Louis Bossart | bbb6381 | 2019-05-01 10:57:41 -0500 | [diff] [blame] | 448 | msecs_to_jiffies(CDNS_TX_TIMEOUT)); |
Sanyog Kale | 956baa1 | 2017-12-14 11:19:42 +0530 | [diff] [blame] | 449 | if (!time) { |
Pierre-Louis Bossart | 53ee957 | 2020-01-10 15:57:28 -0600 | [diff] [blame^] | 450 | dev_err(cdns->dev, "IO transfer timed out, cmd %d device %d addr %x len %d\n", |
| 451 | cmd, msg->dev_num, msg->addr, msg->len); |
Sanyog Kale | 956baa1 | 2017-12-14 11:19:42 +0530 | [diff] [blame] | 452 | msg->len = 0; |
| 453 | return SDW_CMD_TIMEOUT; |
| 454 | } |
| 455 | |
| 456 | return cdns_fill_msg_resp(cdns, msg, count, offset); |
| 457 | } |
| 458 | |
Pierre-Louis Bossart | bbb6381 | 2019-05-01 10:57:41 -0500 | [diff] [blame] | 459 | static enum sdw_command_response |
| 460 | cdns_program_scp_addr(struct sdw_cdns *cdns, struct sdw_msg *msg) |
Sanyog Kale | 956baa1 | 2017-12-14 11:19:42 +0530 | [diff] [blame] | 461 | { |
| 462 | int nack = 0, no_ack = 0; |
| 463 | unsigned long time; |
| 464 | u32 data[2], base; |
| 465 | int i; |
| 466 | |
| 467 | /* Program the watermark level for RX FIFO */ |
| 468 | if (cdns->msg_count != CDNS_SCP_RX_FIFOLEVEL) { |
| 469 | cdns_writel(cdns, CDNS_MCP_FIFOLEVEL, CDNS_SCP_RX_FIFOLEVEL); |
| 470 | cdns->msg_count = CDNS_SCP_RX_FIFOLEVEL; |
| 471 | } |
| 472 | |
| 473 | data[0] = msg->dev_num << SDW_REG_SHIFT(CDNS_MCP_CMD_DEV_ADDR); |
| 474 | data[0] |= 0x3 << SDW_REG_SHIFT(CDNS_MCP_CMD_COMMAND); |
| 475 | data[1] = data[0]; |
| 476 | |
| 477 | data[0] |= SDW_SCP_ADDRPAGE1 << SDW_REG_SHIFT(CDNS_MCP_CMD_REG_ADDR_L); |
| 478 | data[1] |= SDW_SCP_ADDRPAGE2 << SDW_REG_SHIFT(CDNS_MCP_CMD_REG_ADDR_L); |
| 479 | |
| 480 | data[0] |= msg->addr_page1; |
| 481 | data[1] |= msg->addr_page2; |
| 482 | |
| 483 | base = CDNS_MCP_CMD_BASE; |
| 484 | cdns_writel(cdns, base, data[0]); |
| 485 | base += CDNS_MCP_CMD_WORD_LEN; |
| 486 | cdns_writel(cdns, base, data[1]); |
| 487 | |
| 488 | time = wait_for_completion_timeout(&cdns->tx_complete, |
Pierre-Louis Bossart | bbb6381 | 2019-05-01 10:57:41 -0500 | [diff] [blame] | 489 | msecs_to_jiffies(CDNS_TX_TIMEOUT)); |
Sanyog Kale | 956baa1 | 2017-12-14 11:19:42 +0530 | [diff] [blame] | 490 | if (!time) { |
| 491 | dev_err(cdns->dev, "SCP Msg trf timed out\n"); |
| 492 | msg->len = 0; |
| 493 | return SDW_CMD_TIMEOUT; |
| 494 | } |
| 495 | |
| 496 | /* check response the writes */ |
| 497 | for (i = 0; i < 2; i++) { |
| 498 | if (!(cdns->response_buf[i] & CDNS_MCP_RESP_ACK)) { |
| 499 | no_ack = 1; |
Pierre-Louis Bossart | 17ed5be | 2019-05-01 10:57:45 -0500 | [diff] [blame] | 500 | dev_err(cdns->dev, "Program SCP Ack not received\n"); |
Sanyog Kale | 956baa1 | 2017-12-14 11:19:42 +0530 | [diff] [blame] | 501 | if (cdns->response_buf[i] & CDNS_MCP_RESP_NACK) { |
| 502 | nack = 1; |
Pierre-Louis Bossart | 17ed5be | 2019-05-01 10:57:45 -0500 | [diff] [blame] | 503 | dev_err(cdns->dev, "Program SCP NACK received\n"); |
Sanyog Kale | 956baa1 | 2017-12-14 11:19:42 +0530 | [diff] [blame] | 504 | } |
| 505 | } |
| 506 | } |
| 507 | |
| 508 | /* For NACK, NO ack, don't return err if we are in Broadcast mode */ |
| 509 | if (nack) { |
Pierre-Louis Bossart | eb7df4c | 2019-05-22 14:47:26 -0500 | [diff] [blame] | 510 | dev_err_ratelimited(cdns->dev, |
| 511 | "SCP_addrpage NACKed for Slave %d\n", msg->dev_num); |
Sanyog Kale | 956baa1 | 2017-12-14 11:19:42 +0530 | [diff] [blame] | 512 | return SDW_CMD_FAIL; |
| 513 | } else if (no_ack) { |
Pierre-Louis Bossart | eb7df4c | 2019-05-22 14:47:26 -0500 | [diff] [blame] | 514 | dev_dbg_ratelimited(cdns->dev, |
| 515 | "SCP_addrpage ignored for Slave %d\n", msg->dev_num); |
Sanyog Kale | 956baa1 | 2017-12-14 11:19:42 +0530 | [diff] [blame] | 516 | return SDW_CMD_IGNORED; |
| 517 | } |
| 518 | |
| 519 | return SDW_CMD_OK; |
| 520 | } |
| 521 | |
| 522 | static int cdns_prep_msg(struct sdw_cdns *cdns, struct sdw_msg *msg, int *cmd) |
| 523 | { |
| 524 | int ret; |
| 525 | |
| 526 | if (msg->page) { |
| 527 | ret = cdns_program_scp_addr(cdns, msg); |
| 528 | if (ret) { |
| 529 | msg->len = 0; |
| 530 | return ret; |
| 531 | } |
| 532 | } |
| 533 | |
| 534 | switch (msg->flags) { |
| 535 | case SDW_MSG_FLAG_READ: |
| 536 | *cmd = CDNS_MCP_CMD_READ; |
| 537 | break; |
| 538 | |
| 539 | case SDW_MSG_FLAG_WRITE: |
| 540 | *cmd = CDNS_MCP_CMD_WRITE; |
| 541 | break; |
| 542 | |
| 543 | default: |
| 544 | dev_err(cdns->dev, "Invalid msg cmd: %d\n", msg->flags); |
| 545 | return -EINVAL; |
| 546 | } |
| 547 | |
| 548 | return 0; |
| 549 | } |
| 550 | |
Shreyas NC | c91605f | 2018-04-26 18:38:43 +0530 | [diff] [blame] | 551 | enum sdw_command_response |
Sanyog Kale | 956baa1 | 2017-12-14 11:19:42 +0530 | [diff] [blame] | 552 | cdns_xfer_msg(struct sdw_bus *bus, struct sdw_msg *msg) |
| 553 | { |
| 554 | struct sdw_cdns *cdns = bus_to_cdns(bus); |
| 555 | int cmd = 0, ret, i; |
| 556 | |
| 557 | ret = cdns_prep_msg(cdns, msg, &cmd); |
| 558 | if (ret) |
| 559 | return SDW_CMD_FAIL_OTHER; |
| 560 | |
| 561 | for (i = 0; i < msg->len / CDNS_MCP_CMD_LEN; i++) { |
| 562 | ret = _cdns_xfer_msg(cdns, msg, cmd, i * CDNS_MCP_CMD_LEN, |
Pierre-Louis Bossart | bbb6381 | 2019-05-01 10:57:41 -0500 | [diff] [blame] | 563 | CDNS_MCP_CMD_LEN, false); |
Sanyog Kale | 956baa1 | 2017-12-14 11:19:42 +0530 | [diff] [blame] | 564 | if (ret < 0) |
| 565 | goto exit; |
| 566 | } |
| 567 | |
| 568 | if (!(msg->len % CDNS_MCP_CMD_LEN)) |
| 569 | goto exit; |
| 570 | |
| 571 | ret = _cdns_xfer_msg(cdns, msg, cmd, i * CDNS_MCP_CMD_LEN, |
Pierre-Louis Bossart | bbb6381 | 2019-05-01 10:57:41 -0500 | [diff] [blame] | 572 | msg->len % CDNS_MCP_CMD_LEN, false); |
Sanyog Kale | 956baa1 | 2017-12-14 11:19:42 +0530 | [diff] [blame] | 573 | |
| 574 | exit: |
| 575 | return ret; |
| 576 | } |
Shreyas NC | c91605f | 2018-04-26 18:38:43 +0530 | [diff] [blame] | 577 | EXPORT_SYMBOL(cdns_xfer_msg); |
Sanyog Kale | 956baa1 | 2017-12-14 11:19:42 +0530 | [diff] [blame] | 578 | |
Shreyas NC | c91605f | 2018-04-26 18:38:43 +0530 | [diff] [blame] | 579 | enum sdw_command_response |
Sanyog Kale | 956baa1 | 2017-12-14 11:19:42 +0530 | [diff] [blame] | 580 | cdns_xfer_msg_defer(struct sdw_bus *bus, |
Pierre-Louis Bossart | bbb6381 | 2019-05-01 10:57:41 -0500 | [diff] [blame] | 581 | struct sdw_msg *msg, struct sdw_defer *defer) |
Sanyog Kale | 956baa1 | 2017-12-14 11:19:42 +0530 | [diff] [blame] | 582 | { |
| 583 | struct sdw_cdns *cdns = bus_to_cdns(bus); |
| 584 | int cmd = 0, ret; |
| 585 | |
| 586 | /* for defer only 1 message is supported */ |
| 587 | if (msg->len > 1) |
| 588 | return -ENOTSUPP; |
| 589 | |
| 590 | ret = cdns_prep_msg(cdns, msg, &cmd); |
| 591 | if (ret) |
| 592 | return SDW_CMD_FAIL_OTHER; |
| 593 | |
| 594 | cdns->defer = defer; |
| 595 | cdns->defer->length = msg->len; |
| 596 | |
| 597 | return _cdns_xfer_msg(cdns, msg, cmd, 0, msg->len, true); |
| 598 | } |
Shreyas NC | c91605f | 2018-04-26 18:38:43 +0530 | [diff] [blame] | 599 | EXPORT_SYMBOL(cdns_xfer_msg_defer); |
Sanyog Kale | 956baa1 | 2017-12-14 11:19:42 +0530 | [diff] [blame] | 600 | |
Shreyas NC | c91605f | 2018-04-26 18:38:43 +0530 | [diff] [blame] | 601 | enum sdw_command_response |
Sanyog Kale | 956baa1 | 2017-12-14 11:19:42 +0530 | [diff] [blame] | 602 | cdns_reset_page_addr(struct sdw_bus *bus, unsigned int dev_num) |
| 603 | { |
| 604 | struct sdw_cdns *cdns = bus_to_cdns(bus); |
| 605 | struct sdw_msg msg; |
| 606 | |
| 607 | /* Create dummy message with valid device number */ |
| 608 | memset(&msg, 0, sizeof(msg)); |
| 609 | msg.dev_num = dev_num; |
| 610 | |
| 611 | return cdns_program_scp_addr(cdns, &msg); |
| 612 | } |
Shreyas NC | c91605f | 2018-04-26 18:38:43 +0530 | [diff] [blame] | 613 | EXPORT_SYMBOL(cdns_reset_page_addr); |
Sanyog Kale | 956baa1 | 2017-12-14 11:19:42 +0530 | [diff] [blame] | 614 | |
| 615 | /* |
Vinod Koul | 2f52a51 | 2017-12-14 11:19:41 +0530 | [diff] [blame] | 616 | * IRQ handling |
| 617 | */ |
| 618 | |
Sanyog Kale | 956baa1 | 2017-12-14 11:19:42 +0530 | [diff] [blame] | 619 | static void cdns_read_response(struct sdw_cdns *cdns) |
| 620 | { |
| 621 | u32 num_resp, cmd_base; |
| 622 | int i; |
| 623 | |
| 624 | num_resp = cdns_readl(cdns, CDNS_MCP_FIFOSTAT); |
| 625 | num_resp &= CDNS_MCP_RX_FIFO_AVAIL; |
| 626 | |
| 627 | cmd_base = CDNS_MCP_CMD_BASE; |
| 628 | |
| 629 | for (i = 0; i < num_resp; i++) { |
| 630 | cdns->response_buf[i] = cdns_readl(cdns, cmd_base); |
| 631 | cmd_base += CDNS_MCP_CMD_WORD_LEN; |
| 632 | } |
| 633 | } |
| 634 | |
Vinod Koul | 2f52a51 | 2017-12-14 11:19:41 +0530 | [diff] [blame] | 635 | static int cdns_update_slave_status(struct sdw_cdns *cdns, |
Pierre-Louis Bossart | bbb6381 | 2019-05-01 10:57:41 -0500 | [diff] [blame] | 636 | u32 slave0, u32 slave1) |
Vinod Koul | 2f52a51 | 2017-12-14 11:19:41 +0530 | [diff] [blame] | 637 | { |
| 638 | enum sdw_slave_status status[SDW_MAX_DEVICES + 1]; |
| 639 | bool is_slave = false; |
Pierre-Louis Bossart | a78b32d | 2019-05-22 14:47:27 -0500 | [diff] [blame] | 640 | u64 slave; |
| 641 | u32 mask; |
Vinod Koul | 2f52a51 | 2017-12-14 11:19:41 +0530 | [diff] [blame] | 642 | int i, set_status; |
| 643 | |
| 644 | /* combine the two status */ |
| 645 | slave = ((u64)slave1 << 32) | slave0; |
| 646 | memset(status, 0, sizeof(status)); |
| 647 | |
| 648 | for (i = 0; i <= SDW_MAX_DEVICES; i++) { |
| 649 | mask = (slave >> (i * CDNS_MCP_SLAVE_STATUS_NUM)) & |
| 650 | CDNS_MCP_SLAVE_STATUS_BITS; |
| 651 | if (!mask) |
| 652 | continue; |
| 653 | |
| 654 | is_slave = true; |
| 655 | set_status = 0; |
| 656 | |
| 657 | if (mask & CDNS_MCP_SLAVE_INTSTAT_RESERVED) { |
| 658 | status[i] = SDW_SLAVE_RESERVED; |
| 659 | set_status++; |
| 660 | } |
| 661 | |
| 662 | if (mask & CDNS_MCP_SLAVE_INTSTAT_ATTACHED) { |
| 663 | status[i] = SDW_SLAVE_ATTACHED; |
| 664 | set_status++; |
| 665 | } |
| 666 | |
| 667 | if (mask & CDNS_MCP_SLAVE_INTSTAT_ALERT) { |
| 668 | status[i] = SDW_SLAVE_ALERT; |
| 669 | set_status++; |
| 670 | } |
| 671 | |
| 672 | if (mask & CDNS_MCP_SLAVE_INTSTAT_NPRESENT) { |
| 673 | status[i] = SDW_SLAVE_UNATTACHED; |
| 674 | set_status++; |
| 675 | } |
| 676 | |
| 677 | /* first check if Slave reported multiple status */ |
| 678 | if (set_status > 1) { |
Pierre-Louis Bossart | eb7df4c | 2019-05-22 14:47:26 -0500 | [diff] [blame] | 679 | dev_warn_ratelimited(cdns->dev, |
| 680 | "Slave reported multiple Status: %d\n", |
Pierre-Louis Bossart | a78b32d | 2019-05-22 14:47:27 -0500 | [diff] [blame] | 681 | mask); |
Vinod Koul | 2f52a51 | 2017-12-14 11:19:41 +0530 | [diff] [blame] | 682 | /* |
| 683 | * TODO: we need to reread the status here by |
| 684 | * issuing a PING cmd |
| 685 | */ |
| 686 | } |
| 687 | } |
| 688 | |
| 689 | if (is_slave) |
| 690 | return sdw_handle_slave_status(&cdns->bus, status); |
| 691 | |
| 692 | return 0; |
| 693 | } |
| 694 | |
| 695 | /** |
| 696 | * sdw_cdns_irq() - Cadence interrupt handler |
| 697 | * @irq: irq number |
| 698 | * @dev_id: irq context |
| 699 | */ |
| 700 | irqreturn_t sdw_cdns_irq(int irq, void *dev_id) |
| 701 | { |
| 702 | struct sdw_cdns *cdns = dev_id; |
| 703 | u32 int_status; |
| 704 | int ret = IRQ_HANDLED; |
| 705 | |
| 706 | /* Check if the link is up */ |
| 707 | if (!cdns->link_up) |
| 708 | return IRQ_NONE; |
| 709 | |
| 710 | int_status = cdns_readl(cdns, CDNS_MCP_INTSTAT); |
| 711 | |
Pierre-Louis Bossart | a2cff9e | 2020-01-10 15:57:26 -0600 | [diff] [blame] | 712 | /* check for reserved values read as zero */ |
| 713 | if (int_status & CDNS_MCP_INT_RESERVED) |
| 714 | return IRQ_NONE; |
| 715 | |
Vinod Koul | 2f52a51 | 2017-12-14 11:19:41 +0530 | [diff] [blame] | 716 | if (!(int_status & CDNS_MCP_INT_IRQ)) |
| 717 | return IRQ_NONE; |
| 718 | |
Sanyog Kale | 956baa1 | 2017-12-14 11:19:42 +0530 | [diff] [blame] | 719 | if (int_status & CDNS_MCP_INT_RX_WL) { |
| 720 | cdns_read_response(cdns); |
| 721 | |
| 722 | if (cdns->defer) { |
| 723 | cdns_fill_msg_resp(cdns, cdns->defer->msg, |
Pierre-Louis Bossart | bbb6381 | 2019-05-01 10:57:41 -0500 | [diff] [blame] | 724 | cdns->defer->length, 0); |
Sanyog Kale | 956baa1 | 2017-12-14 11:19:42 +0530 | [diff] [blame] | 725 | complete(&cdns->defer->complete); |
| 726 | cdns->defer = NULL; |
Pierre-Louis Bossart | f6e2096 | 2019-05-01 10:57:42 -0500 | [diff] [blame] | 727 | } else { |
Sanyog Kale | 956baa1 | 2017-12-14 11:19:42 +0530 | [diff] [blame] | 728 | complete(&cdns->tx_complete); |
Pierre-Louis Bossart | f6e2096 | 2019-05-01 10:57:42 -0500 | [diff] [blame] | 729 | } |
Sanyog Kale | 956baa1 | 2017-12-14 11:19:42 +0530 | [diff] [blame] | 730 | } |
| 731 | |
Pierre-Louis Bossart | 9b5884a | 2019-08-05 19:55:08 -0500 | [diff] [blame] | 732 | if (int_status & CDNS_MCP_INT_PARITY) { |
| 733 | /* Parity error detected by Master */ |
| 734 | dev_err_ratelimited(cdns->dev, "Parity error\n"); |
| 735 | } |
| 736 | |
Vinod Koul | 2f52a51 | 2017-12-14 11:19:41 +0530 | [diff] [blame] | 737 | if (int_status & CDNS_MCP_INT_CTRL_CLASH) { |
Vinod Koul | 2f52a51 | 2017-12-14 11:19:41 +0530 | [diff] [blame] | 738 | /* Slave is driving bit slot during control word */ |
| 739 | dev_err_ratelimited(cdns->dev, "Bus clash for control word\n"); |
Vinod Koul | 2f52a51 | 2017-12-14 11:19:41 +0530 | [diff] [blame] | 740 | } |
| 741 | |
| 742 | if (int_status & CDNS_MCP_INT_DATA_CLASH) { |
| 743 | /* |
| 744 | * Multiple slaves trying to drive bit slot, or issue with |
| 745 | * ownership of data bits or Slave gone bonkers |
| 746 | */ |
| 747 | dev_err_ratelimited(cdns->dev, "Bus clash for data word\n"); |
Vinod Koul | 2f52a51 | 2017-12-14 11:19:41 +0530 | [diff] [blame] | 748 | } |
| 749 | |
| 750 | if (int_status & CDNS_MCP_INT_SLAVE_MASK) { |
| 751 | /* Mask the Slave interrupt and wake thread */ |
| 752 | cdns_updatel(cdns, CDNS_MCP_INTMASK, |
Pierre-Louis Bossart | bbb6381 | 2019-05-01 10:57:41 -0500 | [diff] [blame] | 753 | CDNS_MCP_INT_SLAVE_MASK, 0); |
Vinod Koul | 2f52a51 | 2017-12-14 11:19:41 +0530 | [diff] [blame] | 754 | |
| 755 | int_status &= ~CDNS_MCP_INT_SLAVE_MASK; |
| 756 | ret = IRQ_WAKE_THREAD; |
| 757 | } |
| 758 | |
| 759 | cdns_writel(cdns, CDNS_MCP_INTSTAT, int_status); |
| 760 | return ret; |
| 761 | } |
| 762 | EXPORT_SYMBOL(sdw_cdns_irq); |
| 763 | |
| 764 | /** |
| 765 | * sdw_cdns_thread() - Cadence irq thread handler |
| 766 | * @irq: irq number |
| 767 | * @dev_id: irq context |
| 768 | */ |
| 769 | irqreturn_t sdw_cdns_thread(int irq, void *dev_id) |
| 770 | { |
| 771 | struct sdw_cdns *cdns = dev_id; |
| 772 | u32 slave0, slave1; |
| 773 | |
Pierre-Louis Bossart | eb7df4c | 2019-05-22 14:47:26 -0500 | [diff] [blame] | 774 | dev_dbg_ratelimited(cdns->dev, "Slave status change\n"); |
Vinod Koul | 2f52a51 | 2017-12-14 11:19:41 +0530 | [diff] [blame] | 775 | |
| 776 | slave0 = cdns_readl(cdns, CDNS_MCP_SLAVE_INTSTAT0); |
| 777 | slave1 = cdns_readl(cdns, CDNS_MCP_SLAVE_INTSTAT1); |
| 778 | |
| 779 | cdns_update_slave_status(cdns, slave0, slave1); |
| 780 | cdns_writel(cdns, CDNS_MCP_SLAVE_INTSTAT0, slave0); |
| 781 | cdns_writel(cdns, CDNS_MCP_SLAVE_INTSTAT1, slave1); |
| 782 | |
| 783 | /* clear and unmask Slave interrupt now */ |
| 784 | cdns_writel(cdns, CDNS_MCP_INTSTAT, CDNS_MCP_INT_SLAVE_MASK); |
| 785 | cdns_updatel(cdns, CDNS_MCP_INTMASK, |
Pierre-Louis Bossart | bbb6381 | 2019-05-01 10:57:41 -0500 | [diff] [blame] | 786 | CDNS_MCP_INT_SLAVE_MASK, CDNS_MCP_INT_SLAVE_MASK); |
Vinod Koul | 2f52a51 | 2017-12-14 11:19:41 +0530 | [diff] [blame] | 787 | |
| 788 | return IRQ_HANDLED; |
| 789 | } |
| 790 | EXPORT_SYMBOL(sdw_cdns_thread); |
| 791 | |
| 792 | /* |
| 793 | * init routines |
| 794 | */ |
Pierre-Louis Bossart | 49ea07d | 2019-10-22 18:54:44 -0500 | [diff] [blame] | 795 | |
| 796 | /** |
| 797 | * sdw_cdns_exit_reset() - Program reset parameters and start bus operations |
| 798 | * @cdns: Cadence instance |
| 799 | */ |
| 800 | int sdw_cdns_exit_reset(struct sdw_cdns *cdns) |
| 801 | { |
| 802 | /* program maximum length reset to be safe */ |
| 803 | cdns_updatel(cdns, CDNS_MCP_CONTROL, |
| 804 | CDNS_MCP_CONTROL_RST_DELAY, |
| 805 | CDNS_MCP_CONTROL_RST_DELAY); |
| 806 | |
| 807 | /* use hardware generated reset */ |
| 808 | cdns_updatel(cdns, CDNS_MCP_CONTROL, |
| 809 | CDNS_MCP_CONTROL_HW_RST, |
| 810 | CDNS_MCP_CONTROL_HW_RST); |
| 811 | |
| 812 | /* enable bus operations with clock and data */ |
| 813 | cdns_updatel(cdns, CDNS_MCP_CONFIG, |
| 814 | CDNS_MCP_CONFIG_OP, |
| 815 | CDNS_MCP_CONFIG_OP_NORMAL); |
| 816 | |
| 817 | /* commit changes */ |
| 818 | return cdns_update_config(cdns); |
| 819 | } |
| 820 | EXPORT_SYMBOL(sdw_cdns_exit_reset); |
| 821 | |
| 822 | /** |
| 823 | * sdw_cdns_enable_interrupt() - Enable SDW interrupts and update config |
| 824 | * @cdns: Cadence instance |
| 825 | */ |
Pierre-Louis Bossart | 9e3d47f | 2019-10-22 18:54:47 -0500 | [diff] [blame] | 826 | int sdw_cdns_enable_interrupt(struct sdw_cdns *cdns, bool state) |
Sanyog Kale | 956baa1 | 2017-12-14 11:19:42 +0530 | [diff] [blame] | 827 | { |
Pierre-Louis Bossart | 9e3d47f | 2019-10-22 18:54:47 -0500 | [diff] [blame] | 828 | u32 slave_intmask0 = 0; |
| 829 | u32 slave_intmask1 = 0; |
| 830 | u32 mask = 0; |
Sanyog Kale | 956baa1 | 2017-12-14 11:19:42 +0530 | [diff] [blame] | 831 | |
Pierre-Louis Bossart | 9e3d47f | 2019-10-22 18:54:47 -0500 | [diff] [blame] | 832 | if (!state) |
| 833 | goto update_masks; |
| 834 | |
| 835 | slave_intmask0 = CDNS_MCP_SLAVE_INTMASK0_MASK; |
| 836 | slave_intmask1 = CDNS_MCP_SLAVE_INTMASK1_MASK; |
Sanyog Kale | 956baa1 | 2017-12-14 11:19:42 +0530 | [diff] [blame] | 837 | |
Pierre-Louis Bossart | 9b5884a | 2019-08-05 19:55:08 -0500 | [diff] [blame] | 838 | /* enable detection of all slave state changes */ |
| 839 | mask = CDNS_MCP_INT_SLAVE_MASK; |
| 840 | |
| 841 | /* enable detection of bus issues */ |
| 842 | mask |= CDNS_MCP_INT_CTRL_CLASH | CDNS_MCP_INT_DATA_CLASH | |
| 843 | CDNS_MCP_INT_PARITY; |
| 844 | |
| 845 | /* no detection of port interrupts for now */ |
| 846 | |
| 847 | /* enable detection of RX fifo level */ |
| 848 | mask |= CDNS_MCP_INT_RX_WL; |
| 849 | |
| 850 | /* |
| 851 | * CDNS_MCP_INT_IRQ needs to be set otherwise all previous |
| 852 | * settings are irrelevant |
| 853 | */ |
| 854 | mask |= CDNS_MCP_INT_IRQ; |
Sanyog Kale | 956baa1 | 2017-12-14 11:19:42 +0530 | [diff] [blame] | 855 | |
Pierre-Louis Bossart | 04592dc | 2019-08-05 19:55:21 -0500 | [diff] [blame] | 856 | if (interrupt_mask) /* parameter override */ |
| 857 | mask = interrupt_mask; |
Sanyog Kale | 956baa1 | 2017-12-14 11:19:42 +0530 | [diff] [blame] | 858 | |
Pierre-Louis Bossart | 9e3d47f | 2019-10-22 18:54:47 -0500 | [diff] [blame] | 859 | update_masks: |
Rander Wang | 5ebb094 | 2020-01-10 15:57:27 -0600 | [diff] [blame] | 860 | /* clear slave interrupt status before enabling interrupt */ |
| 861 | if (state) { |
| 862 | u32 slave_state; |
| 863 | |
| 864 | slave_state = cdns_readl(cdns, CDNS_MCP_SLAVE_INTSTAT0); |
| 865 | cdns_writel(cdns, CDNS_MCP_SLAVE_INTSTAT0, slave_state); |
| 866 | slave_state = cdns_readl(cdns, CDNS_MCP_SLAVE_INTSTAT1); |
| 867 | cdns_writel(cdns, CDNS_MCP_SLAVE_INTSTAT1, slave_state); |
| 868 | } |
| 869 | |
Pierre-Louis Bossart | 9e3d47f | 2019-10-22 18:54:47 -0500 | [diff] [blame] | 870 | cdns_writel(cdns, CDNS_MCP_SLAVE_INTMASK0, slave_intmask0); |
| 871 | cdns_writel(cdns, CDNS_MCP_SLAVE_INTMASK1, slave_intmask1); |
Sanyog Kale | 956baa1 | 2017-12-14 11:19:42 +0530 | [diff] [blame] | 872 | cdns_writel(cdns, CDNS_MCP_INTMASK, mask); |
| 873 | |
Pierre-Louis Bossart | 49ea07d | 2019-10-22 18:54:44 -0500 | [diff] [blame] | 874 | /* commit changes */ |
| 875 | return cdns_update_config(cdns); |
Sanyog Kale | 956baa1 | 2017-12-14 11:19:42 +0530 | [diff] [blame] | 876 | } |
| 877 | EXPORT_SYMBOL(sdw_cdns_enable_interrupt); |
Vinod Koul | 2f52a51 | 2017-12-14 11:19:41 +0530 | [diff] [blame] | 878 | |
Vinod Koul | 07abeff | 2018-04-26 18:38:48 +0530 | [diff] [blame] | 879 | static int cdns_allocate_pdi(struct sdw_cdns *cdns, |
Pierre-Louis Bossart | bbb6381 | 2019-05-01 10:57:41 -0500 | [diff] [blame] | 880 | struct sdw_cdns_pdi **stream, |
| 881 | u32 num, u32 pdi_offset) |
Vinod Koul | 07abeff | 2018-04-26 18:38:48 +0530 | [diff] [blame] | 882 | { |
| 883 | struct sdw_cdns_pdi *pdi; |
| 884 | int i; |
| 885 | |
| 886 | if (!num) |
| 887 | return 0; |
| 888 | |
| 889 | pdi = devm_kcalloc(cdns->dev, num, sizeof(*pdi), GFP_KERNEL); |
| 890 | if (!pdi) |
| 891 | return -ENOMEM; |
| 892 | |
| 893 | for (i = 0; i < num; i++) { |
| 894 | pdi[i].num = i + pdi_offset; |
Vinod Koul | 07abeff | 2018-04-26 18:38:48 +0530 | [diff] [blame] | 895 | } |
| 896 | |
| 897 | *stream = pdi; |
| 898 | return 0; |
| 899 | } |
| 900 | |
| 901 | /** |
| 902 | * sdw_cdns_pdi_init() - PDI initialization routine |
| 903 | * |
| 904 | * @cdns: Cadence instance |
| 905 | * @config: Stream configurations |
| 906 | */ |
| 907 | int sdw_cdns_pdi_init(struct sdw_cdns *cdns, |
Pierre-Louis Bossart | bbb6381 | 2019-05-01 10:57:41 -0500 | [diff] [blame] | 908 | struct sdw_cdns_stream_config config) |
Vinod Koul | 07abeff | 2018-04-26 18:38:48 +0530 | [diff] [blame] | 909 | { |
| 910 | struct sdw_cdns_streams *stream; |
Pierre-Louis Bossart | 57a3479 | 2019-09-16 14:23:46 -0500 | [diff] [blame] | 911 | int offset; |
| 912 | int ret; |
Vinod Koul | 07abeff | 2018-04-26 18:38:48 +0530 | [diff] [blame] | 913 | |
| 914 | cdns->pcm.num_bd = config.pcm_bd; |
| 915 | cdns->pcm.num_in = config.pcm_in; |
| 916 | cdns->pcm.num_out = config.pcm_out; |
| 917 | cdns->pdm.num_bd = config.pdm_bd; |
| 918 | cdns->pdm.num_in = config.pdm_in; |
| 919 | cdns->pdm.num_out = config.pdm_out; |
| 920 | |
| 921 | /* Allocate PDIs for PCMs */ |
| 922 | stream = &cdns->pcm; |
| 923 | |
Pierre-Louis Bossart | 807c15b | 2019-09-16 14:23:47 -0500 | [diff] [blame] | 924 | /* we allocate PDI0 and PDI1 which are used for Bulk */ |
| 925 | offset = 0; |
Vinod Koul | 07abeff | 2018-04-26 18:38:48 +0530 | [diff] [blame] | 926 | |
| 927 | ret = cdns_allocate_pdi(cdns, &stream->bd, |
| 928 | stream->num_bd, offset); |
| 929 | if (ret) |
| 930 | return ret; |
| 931 | |
| 932 | offset += stream->num_bd; |
| 933 | |
| 934 | ret = cdns_allocate_pdi(cdns, &stream->in, |
| 935 | stream->num_in, offset); |
| 936 | if (ret) |
| 937 | return ret; |
| 938 | |
| 939 | offset += stream->num_in; |
| 940 | |
| 941 | ret = cdns_allocate_pdi(cdns, &stream->out, |
| 942 | stream->num_out, offset); |
| 943 | if (ret) |
| 944 | return ret; |
| 945 | |
| 946 | /* Update total number of PCM PDIs */ |
| 947 | stream->num_pdi = stream->num_bd + stream->num_in + stream->num_out; |
| 948 | cdns->num_ports = stream->num_pdi; |
| 949 | |
| 950 | /* Allocate PDIs for PDMs */ |
| 951 | stream = &cdns->pdm; |
Vinod Koul | 07abeff | 2018-04-26 18:38:48 +0530 | [diff] [blame] | 952 | ret = cdns_allocate_pdi(cdns, &stream->bd, |
| 953 | stream->num_bd, offset); |
| 954 | if (ret) |
| 955 | return ret; |
| 956 | |
| 957 | offset += stream->num_bd; |
| 958 | |
| 959 | ret = cdns_allocate_pdi(cdns, &stream->in, |
| 960 | stream->num_in, offset); |
| 961 | if (ret) |
| 962 | return ret; |
| 963 | |
| 964 | offset += stream->num_in; |
| 965 | |
| 966 | ret = cdns_allocate_pdi(cdns, &stream->out, |
| 967 | stream->num_out, offset); |
Pierre-Louis Bossart | 807c15b | 2019-09-16 14:23:47 -0500 | [diff] [blame] | 968 | |
| 969 | offset += stream->num_out; |
| 970 | |
Vinod Koul | 07abeff | 2018-04-26 18:38:48 +0530 | [diff] [blame] | 971 | if (ret) |
| 972 | return ret; |
| 973 | |
| 974 | /* Update total number of PDM PDIs */ |
| 975 | stream->num_pdi = stream->num_bd + stream->num_in + stream->num_out; |
| 976 | cdns->num_ports += stream->num_pdi; |
| 977 | |
Vinod Koul | 07abeff | 2018-04-26 18:38:48 +0530 | [diff] [blame] | 978 | return 0; |
| 979 | } |
| 980 | EXPORT_SYMBOL(sdw_cdns_pdi_init); |
| 981 | |
Pierre-Louis Bossart | 05be59a | 2019-08-05 19:55:11 -0500 | [diff] [blame] | 982 | static u32 cdns_set_initial_frame_shape(int n_rows, int n_cols) |
| 983 | { |
| 984 | u32 val; |
| 985 | int c; |
| 986 | int r; |
| 987 | |
| 988 | r = sdw_find_row_index(n_rows); |
| 989 | c = sdw_find_col_index(n_cols) & CDNS_MCP_FRAME_SHAPE_COL_MASK; |
| 990 | |
| 991 | val = (r << CDNS_MCP_FRAME_SHAPE_ROW_OFFSET) | c; |
| 992 | |
| 993 | return val; |
| 994 | } |
| 995 | |
Vinod Koul | 2f52a51 | 2017-12-14 11:19:41 +0530 | [diff] [blame] | 996 | /** |
| 997 | * sdw_cdns_init() - Cadence initialization |
| 998 | * @cdns: Cadence instance |
| 999 | */ |
Pierre-Louis Bossart | 3ccb855 | 2019-10-22 18:54:48 -0500 | [diff] [blame] | 1000 | int sdw_cdns_init(struct sdw_cdns *cdns, bool clock_stop_exit) |
Vinod Koul | 2f52a51 | 2017-12-14 11:19:41 +0530 | [diff] [blame] | 1001 | { |
Pierre-Louis Bossart | 3859872 | 2019-08-05 19:55:17 -0500 | [diff] [blame] | 1002 | struct sdw_bus *bus = &cdns->bus; |
| 1003 | struct sdw_master_prop *prop = &bus->prop; |
Vinod Koul | 2f52a51 | 2017-12-14 11:19:41 +0530 | [diff] [blame] | 1004 | u32 val; |
Pierre-Louis Bossart | 3859872 | 2019-08-05 19:55:17 -0500 | [diff] [blame] | 1005 | int divider; |
Vinod Koul | 2f52a51 | 2017-12-14 11:19:41 +0530 | [diff] [blame] | 1006 | int ret; |
| 1007 | |
Pierre-Louis Bossart | 3ccb855 | 2019-10-22 18:54:48 -0500 | [diff] [blame] | 1008 | if (clock_stop_exit) { |
| 1009 | ret = cdns_clear_bit(cdns, CDNS_MCP_CONTROL, |
| 1010 | CDNS_MCP_CONTROL_CLK_STOP_CLR); |
| 1011 | if (ret < 0) { |
| 1012 | dev_err(cdns->dev, "Couldn't exit from clock stop\n"); |
| 1013 | return ret; |
| 1014 | } |
Vinod Koul | 2f52a51 | 2017-12-14 11:19:41 +0530 | [diff] [blame] | 1015 | } |
| 1016 | |
| 1017 | /* Set clock divider */ |
Pierre-Louis Bossart | 3859872 | 2019-08-05 19:55:17 -0500 | [diff] [blame] | 1018 | divider = (prop->mclk_freq / prop->max_clk_freq) - 1; |
Vinod Koul | 2f52a51 | 2017-12-14 11:19:41 +0530 | [diff] [blame] | 1019 | |
Rander Wang | a50954e | 2019-08-05 19:55:18 -0500 | [diff] [blame] | 1020 | cdns_updatel(cdns, CDNS_MCP_CLK_CTRL0, |
| 1021 | CDNS_MCP_CLK_MCLKD_MASK, divider); |
| 1022 | cdns_updatel(cdns, CDNS_MCP_CLK_CTRL1, |
| 1023 | CDNS_MCP_CLK_MCLKD_MASK, divider); |
Vinod Koul | 2f52a51 | 2017-12-14 11:19:41 +0530 | [diff] [blame] | 1024 | |
Pierre-Louis Bossart | 05be59a | 2019-08-05 19:55:11 -0500 | [diff] [blame] | 1025 | /* |
| 1026 | * Frame shape changes after initialization have to be done |
| 1027 | * with the bank switch mechanism |
| 1028 | */ |
| 1029 | val = cdns_set_initial_frame_shape(prop->default_row, |
| 1030 | prop->default_col); |
| 1031 | cdns_writel(cdns, CDNS_MCP_FRAME_SHAPE_INIT, val); |
Vinod Koul | 2f52a51 | 2017-12-14 11:19:41 +0530 | [diff] [blame] | 1032 | |
| 1033 | /* Set SSP interval to default value */ |
| 1034 | cdns_writel(cdns, CDNS_MCP_SSP_CTRL0, CDNS_DEFAULT_SSP_INTERVAL); |
| 1035 | cdns_writel(cdns, CDNS_MCP_SSP_CTRL1, CDNS_DEFAULT_SSP_INTERVAL); |
| 1036 | |
Pierre-Louis Bossart | 49ea07d | 2019-10-22 18:54:44 -0500 | [diff] [blame] | 1037 | /* flush command FIFOs */ |
| 1038 | cdns_updatel(cdns, CDNS_MCP_CONTROL, CDNS_MCP_CONTROL_CMD_RST, |
| 1039 | CDNS_MCP_CONTROL_CMD_RST); |
| 1040 | |
Vinod Koul | 2f52a51 | 2017-12-14 11:19:41 +0530 | [diff] [blame] | 1041 | /* Set cmd accept mode */ |
| 1042 | cdns_updatel(cdns, CDNS_MCP_CONTROL, CDNS_MCP_CONTROL_CMD_ACCEPT, |
Pierre-Louis Bossart | bbb6381 | 2019-05-01 10:57:41 -0500 | [diff] [blame] | 1043 | CDNS_MCP_CONTROL_CMD_ACCEPT); |
Vinod Koul | 2f52a51 | 2017-12-14 11:19:41 +0530 | [diff] [blame] | 1044 | |
| 1045 | /* Configure mcp config */ |
| 1046 | val = cdns_readl(cdns, CDNS_MCP_CONFIG); |
| 1047 | |
| 1048 | /* Set Max cmd retry to 15 */ |
| 1049 | val |= CDNS_MCP_CONFIG_MCMD_RETRY; |
| 1050 | |
| 1051 | /* Set frame delay between PREQ and ping frame to 15 frames */ |
| 1052 | val |= 0xF << SDW_REG_SHIFT(CDNS_MCP_CONFIG_MPREQ_DELAY); |
| 1053 | |
| 1054 | /* Disable auto bus release */ |
| 1055 | val &= ~CDNS_MCP_CONFIG_BUS_REL; |
| 1056 | |
| 1057 | /* Disable sniffer mode */ |
| 1058 | val &= ~CDNS_MCP_CONFIG_SNIFFER; |
| 1059 | |
| 1060 | /* Set cmd mode for Tx and Rx cmds */ |
| 1061 | val &= ~CDNS_MCP_CONFIG_CMD; |
| 1062 | |
Vinod Koul | 2f52a51 | 2017-12-14 11:19:41 +0530 | [diff] [blame] | 1063 | cdns_writel(cdns, CDNS_MCP_CONFIG, val); |
| 1064 | |
Pierre-Louis Bossart | 49ea07d | 2019-10-22 18:54:44 -0500 | [diff] [blame] | 1065 | /* commit changes */ |
| 1066 | return cdns_update_config(cdns); |
Vinod Koul | 2f52a51 | 2017-12-14 11:19:41 +0530 | [diff] [blame] | 1067 | } |
| 1068 | EXPORT_SYMBOL(sdw_cdns_init); |
| 1069 | |
Vinod Koul | 07abeff | 2018-04-26 18:38:48 +0530 | [diff] [blame] | 1070 | int cdns_bus_conf(struct sdw_bus *bus, struct sdw_bus_params *params) |
| 1071 | { |
Pierre-Louis Bossart | 3859872 | 2019-08-05 19:55:17 -0500 | [diff] [blame] | 1072 | struct sdw_master_prop *prop = &bus->prop; |
Vinod Koul | 07abeff | 2018-04-26 18:38:48 +0530 | [diff] [blame] | 1073 | struct sdw_cdns *cdns = bus_to_cdns(bus); |
Rander Wang | a50954e | 2019-08-05 19:55:18 -0500 | [diff] [blame] | 1074 | int mcp_clkctrl_off; |
Vinod Koul | 07abeff | 2018-04-26 18:38:48 +0530 | [diff] [blame] | 1075 | int divider; |
| 1076 | |
| 1077 | if (!params->curr_dr_freq) { |
Pierre-Louis Bossart | 17ed5be | 2019-05-01 10:57:45 -0500 | [diff] [blame] | 1078 | dev_err(cdns->dev, "NULL curr_dr_freq\n"); |
Vinod Koul | 07abeff | 2018-04-26 18:38:48 +0530 | [diff] [blame] | 1079 | return -EINVAL; |
| 1080 | } |
| 1081 | |
Pierre-Louis Bossart | 3859872 | 2019-08-05 19:55:17 -0500 | [diff] [blame] | 1082 | divider = prop->mclk_freq * SDW_DOUBLE_RATE_FACTOR / |
| 1083 | params->curr_dr_freq; |
| 1084 | divider--; /* divider is 1/(N+1) */ |
Vinod Koul | 07abeff | 2018-04-26 18:38:48 +0530 | [diff] [blame] | 1085 | |
| 1086 | if (params->next_bank) |
| 1087 | mcp_clkctrl_off = CDNS_MCP_CLK_CTRL1; |
| 1088 | else |
| 1089 | mcp_clkctrl_off = CDNS_MCP_CLK_CTRL0; |
| 1090 | |
Rander Wang | a50954e | 2019-08-05 19:55:18 -0500 | [diff] [blame] | 1091 | cdns_updatel(cdns, mcp_clkctrl_off, CDNS_MCP_CLK_MCLKD_MASK, divider); |
Vinod Koul | 07abeff | 2018-04-26 18:38:48 +0530 | [diff] [blame] | 1092 | |
| 1093 | return 0; |
| 1094 | } |
| 1095 | EXPORT_SYMBOL(cdns_bus_conf); |
| 1096 | |
| 1097 | static int cdns_port_params(struct sdw_bus *bus, |
Pierre-Louis Bossart | bbb6381 | 2019-05-01 10:57:41 -0500 | [diff] [blame] | 1098 | struct sdw_port_params *p_params, unsigned int bank) |
Vinod Koul | 07abeff | 2018-04-26 18:38:48 +0530 | [diff] [blame] | 1099 | { |
| 1100 | struct sdw_cdns *cdns = bus_to_cdns(bus); |
| 1101 | int dpn_config = 0, dpn_config_off; |
| 1102 | |
| 1103 | if (bank) |
| 1104 | dpn_config_off = CDNS_DPN_B1_CONFIG(p_params->num); |
| 1105 | else |
| 1106 | dpn_config_off = CDNS_DPN_B0_CONFIG(p_params->num); |
| 1107 | |
| 1108 | dpn_config = cdns_readl(cdns, dpn_config_off); |
| 1109 | |
| 1110 | dpn_config |= ((p_params->bps - 1) << |
| 1111 | SDW_REG_SHIFT(CDNS_DPN_CONFIG_WL)); |
| 1112 | dpn_config |= (p_params->flow_mode << |
| 1113 | SDW_REG_SHIFT(CDNS_DPN_CONFIG_PORT_FLOW)); |
| 1114 | dpn_config |= (p_params->data_mode << |
| 1115 | SDW_REG_SHIFT(CDNS_DPN_CONFIG_PORT_DAT)); |
| 1116 | |
| 1117 | cdns_writel(cdns, dpn_config_off, dpn_config); |
| 1118 | |
| 1119 | return 0; |
| 1120 | } |
| 1121 | |
| 1122 | static int cdns_transport_params(struct sdw_bus *bus, |
Pierre-Louis Bossart | bbb6381 | 2019-05-01 10:57:41 -0500 | [diff] [blame] | 1123 | struct sdw_transport_params *t_params, |
| 1124 | enum sdw_reg_bank bank) |
Vinod Koul | 07abeff | 2018-04-26 18:38:48 +0530 | [diff] [blame] | 1125 | { |
| 1126 | struct sdw_cdns *cdns = bus_to_cdns(bus); |
| 1127 | int dpn_offsetctrl = 0, dpn_offsetctrl_off; |
| 1128 | int dpn_config = 0, dpn_config_off; |
| 1129 | int dpn_hctrl = 0, dpn_hctrl_off; |
| 1130 | int num = t_params->port_num; |
| 1131 | int dpn_samplectrl_off; |
| 1132 | |
| 1133 | /* |
| 1134 | * Note: Only full data port is supported on the Master side for |
| 1135 | * both PCM and PDM ports. |
| 1136 | */ |
| 1137 | |
| 1138 | if (bank) { |
| 1139 | dpn_config_off = CDNS_DPN_B1_CONFIG(num); |
| 1140 | dpn_samplectrl_off = CDNS_DPN_B1_SAMPLE_CTRL(num); |
| 1141 | dpn_hctrl_off = CDNS_DPN_B1_HCTRL(num); |
| 1142 | dpn_offsetctrl_off = CDNS_DPN_B1_OFFSET_CTRL(num); |
| 1143 | } else { |
| 1144 | dpn_config_off = CDNS_DPN_B0_CONFIG(num); |
| 1145 | dpn_samplectrl_off = CDNS_DPN_B0_SAMPLE_CTRL(num); |
| 1146 | dpn_hctrl_off = CDNS_DPN_B0_HCTRL(num); |
| 1147 | dpn_offsetctrl_off = CDNS_DPN_B0_OFFSET_CTRL(num); |
| 1148 | } |
| 1149 | |
| 1150 | dpn_config = cdns_readl(cdns, dpn_config_off); |
| 1151 | |
| 1152 | dpn_config |= (t_params->blk_grp_ctrl << |
| 1153 | SDW_REG_SHIFT(CDNS_DPN_CONFIG_BGC)); |
| 1154 | dpn_config |= (t_params->blk_pkg_mode << |
| 1155 | SDW_REG_SHIFT(CDNS_DPN_CONFIG_BPM)); |
| 1156 | cdns_writel(cdns, dpn_config_off, dpn_config); |
| 1157 | |
| 1158 | dpn_offsetctrl |= (t_params->offset1 << |
| 1159 | SDW_REG_SHIFT(CDNS_DPN_OFFSET_CTRL_1)); |
| 1160 | dpn_offsetctrl |= (t_params->offset2 << |
| 1161 | SDW_REG_SHIFT(CDNS_DPN_OFFSET_CTRL_2)); |
| 1162 | cdns_writel(cdns, dpn_offsetctrl_off, dpn_offsetctrl); |
| 1163 | |
| 1164 | dpn_hctrl |= (t_params->hstart << |
| 1165 | SDW_REG_SHIFT(CDNS_DPN_HCTRL_HSTART)); |
| 1166 | dpn_hctrl |= (t_params->hstop << SDW_REG_SHIFT(CDNS_DPN_HCTRL_HSTOP)); |
| 1167 | dpn_hctrl |= (t_params->lane_ctrl << |
| 1168 | SDW_REG_SHIFT(CDNS_DPN_HCTRL_LCTRL)); |
| 1169 | |
| 1170 | cdns_writel(cdns, dpn_hctrl_off, dpn_hctrl); |
| 1171 | cdns_writel(cdns, dpn_samplectrl_off, (t_params->sample_interval - 1)); |
| 1172 | |
| 1173 | return 0; |
| 1174 | } |
| 1175 | |
| 1176 | static int cdns_port_enable(struct sdw_bus *bus, |
Pierre-Louis Bossart | bbb6381 | 2019-05-01 10:57:41 -0500 | [diff] [blame] | 1177 | struct sdw_enable_ch *enable_ch, unsigned int bank) |
Vinod Koul | 07abeff | 2018-04-26 18:38:48 +0530 | [diff] [blame] | 1178 | { |
| 1179 | struct sdw_cdns *cdns = bus_to_cdns(bus); |
| 1180 | int dpn_chnen_off, ch_mask; |
| 1181 | |
| 1182 | if (bank) |
| 1183 | dpn_chnen_off = CDNS_DPN_B1_CH_EN(enable_ch->port_num); |
| 1184 | else |
| 1185 | dpn_chnen_off = CDNS_DPN_B0_CH_EN(enable_ch->port_num); |
| 1186 | |
| 1187 | ch_mask = enable_ch->ch_mask * enable_ch->enable; |
| 1188 | cdns_writel(cdns, dpn_chnen_off, ch_mask); |
| 1189 | |
| 1190 | return 0; |
| 1191 | } |
| 1192 | |
| 1193 | static const struct sdw_master_port_ops cdns_port_ops = { |
| 1194 | .dpn_set_port_params = cdns_port_params, |
| 1195 | .dpn_set_port_transport_params = cdns_transport_params, |
| 1196 | .dpn_port_enable_ch = cdns_port_enable, |
| 1197 | }; |
| 1198 | |
Sanyog Kale | 956baa1 | 2017-12-14 11:19:42 +0530 | [diff] [blame] | 1199 | /** |
| 1200 | * sdw_cdns_probe() - Cadence probe routine |
| 1201 | * @cdns: Cadence instance |
| 1202 | */ |
| 1203 | int sdw_cdns_probe(struct sdw_cdns *cdns) |
| 1204 | { |
| 1205 | init_completion(&cdns->tx_complete); |
Vinod Koul | 07abeff | 2018-04-26 18:38:48 +0530 | [diff] [blame] | 1206 | cdns->bus.port_ops = &cdns_port_ops; |
Sanyog Kale | 956baa1 | 2017-12-14 11:19:42 +0530 | [diff] [blame] | 1207 | |
| 1208 | return 0; |
| 1209 | } |
| 1210 | EXPORT_SYMBOL(sdw_cdns_probe); |
| 1211 | |
Vinod Koul | 5d6b3c8 | 2018-04-26 18:38:53 +0530 | [diff] [blame] | 1212 | int cdns_set_sdw_stream(struct snd_soc_dai *dai, |
Pierre-Louis Bossart | bbb6381 | 2019-05-01 10:57:41 -0500 | [diff] [blame] | 1213 | void *stream, bool pcm, int direction) |
Vinod Koul | 5d6b3c8 | 2018-04-26 18:38:53 +0530 | [diff] [blame] | 1214 | { |
| 1215 | struct sdw_cdns *cdns = snd_soc_dai_get_drvdata(dai); |
| 1216 | struct sdw_cdns_dma_data *dma; |
| 1217 | |
| 1218 | dma = kzalloc(sizeof(*dma), GFP_KERNEL); |
| 1219 | if (!dma) |
| 1220 | return -ENOMEM; |
| 1221 | |
| 1222 | if (pcm) |
| 1223 | dma->stream_type = SDW_STREAM_PCM; |
| 1224 | else |
| 1225 | dma->stream_type = SDW_STREAM_PDM; |
| 1226 | |
| 1227 | dma->bus = &cdns->bus; |
| 1228 | dma->link_id = cdns->instance; |
| 1229 | |
| 1230 | dma->stream = stream; |
| 1231 | |
| 1232 | if (direction == SNDRV_PCM_STREAM_PLAYBACK) |
| 1233 | dai->playback_dma_data = dma; |
| 1234 | else |
| 1235 | dai->capture_dma_data = dma; |
| 1236 | |
| 1237 | return 0; |
| 1238 | } |
| 1239 | EXPORT_SYMBOL(cdns_set_sdw_stream); |
| 1240 | |
| 1241 | /** |
| 1242 | * cdns_find_pdi() - Find a free PDI |
| 1243 | * |
| 1244 | * @cdns: Cadence instance |
| 1245 | * @num: Number of PDIs |
| 1246 | * @pdi: PDI instances |
| 1247 | * |
Bard Liao | 1b53385e | 2019-09-16 14:23:48 -0500 | [diff] [blame] | 1248 | * Find a PDI for a given PDI array. The PDI num and dai_id are |
| 1249 | * expected to match, return NULL otherwise. |
Vinod Koul | 5d6b3c8 | 2018-04-26 18:38:53 +0530 | [diff] [blame] | 1250 | */ |
| 1251 | static struct sdw_cdns_pdi *cdns_find_pdi(struct sdw_cdns *cdns, |
Pierre-Louis Bossart | 807c15b | 2019-09-16 14:23:47 -0500 | [diff] [blame] | 1252 | unsigned int offset, |
Pierre-Louis Bossart | bbb6381 | 2019-05-01 10:57:41 -0500 | [diff] [blame] | 1253 | unsigned int num, |
Bard Liao | 1b53385e | 2019-09-16 14:23:48 -0500 | [diff] [blame] | 1254 | struct sdw_cdns_pdi *pdi, |
| 1255 | int dai_id) |
Vinod Koul | 5d6b3c8 | 2018-04-26 18:38:53 +0530 | [diff] [blame] | 1256 | { |
| 1257 | int i; |
| 1258 | |
Bard Liao | 1b53385e | 2019-09-16 14:23:48 -0500 | [diff] [blame] | 1259 | for (i = offset; i < offset + num; i++) |
| 1260 | if (pdi[i].num == dai_id) |
| 1261 | return &pdi[i]; |
Vinod Koul | 5d6b3c8 | 2018-04-26 18:38:53 +0530 | [diff] [blame] | 1262 | |
| 1263 | return NULL; |
| 1264 | } |
| 1265 | |
| 1266 | /** |
| 1267 | * sdw_cdns_config_stream: Configure a stream |
| 1268 | * |
| 1269 | * @cdns: Cadence instance |
Vinod Koul | 5d6b3c8 | 2018-04-26 18:38:53 +0530 | [diff] [blame] | 1270 | * @ch: Channel count |
| 1271 | * @dir: Data direction |
| 1272 | * @pdi: PDI to be used |
| 1273 | */ |
| 1274 | void sdw_cdns_config_stream(struct sdw_cdns *cdns, |
Pierre-Louis Bossart | bbb6381 | 2019-05-01 10:57:41 -0500 | [diff] [blame] | 1275 | u32 ch, u32 dir, struct sdw_cdns_pdi *pdi) |
Vinod Koul | 5d6b3c8 | 2018-04-26 18:38:53 +0530 | [diff] [blame] | 1276 | { |
| 1277 | u32 offset, val = 0; |
| 1278 | |
| 1279 | if (dir == SDW_DATA_DIR_RX) |
| 1280 | val = CDNS_PORTCTRL_DIRN; |
| 1281 | |
Pierre-Louis Bossart | 57a3479 | 2019-09-16 14:23:46 -0500 | [diff] [blame] | 1282 | offset = CDNS_PORTCTRL + pdi->num * CDNS_PORT_OFFSET; |
Vinod Koul | 5d6b3c8 | 2018-04-26 18:38:53 +0530 | [diff] [blame] | 1283 | cdns_updatel(cdns, offset, CDNS_PORTCTRL_DIRN, val); |
| 1284 | |
Pierre-Louis Bossart | 57a3479 | 2019-09-16 14:23:46 -0500 | [diff] [blame] | 1285 | val = pdi->num; |
Vinod Koul | 5d6b3c8 | 2018-04-26 18:38:53 +0530 | [diff] [blame] | 1286 | val |= ((1 << ch) - 1) << SDW_REG_SHIFT(CDNS_PDI_CONFIG_CHANNEL); |
| 1287 | cdns_writel(cdns, CDNS_PDI_CONFIG(pdi->num), val); |
| 1288 | } |
| 1289 | EXPORT_SYMBOL(sdw_cdns_config_stream); |
| 1290 | |
| 1291 | /** |
Pierre-Louis Bossart | 57a3479 | 2019-09-16 14:23:46 -0500 | [diff] [blame] | 1292 | * sdw_cdns_alloc_pdi() - Allocate a PDI |
Vinod Koul | 5d6b3c8 | 2018-04-26 18:38:53 +0530 | [diff] [blame] | 1293 | * |
| 1294 | * @cdns: Cadence instance |
| 1295 | * @stream: Stream to be allocated |
| 1296 | * @ch: Channel count |
| 1297 | * @dir: Data direction |
| 1298 | */ |
Pierre-Louis Bossart | 57a3479 | 2019-09-16 14:23:46 -0500 | [diff] [blame] | 1299 | struct sdw_cdns_pdi *sdw_cdns_alloc_pdi(struct sdw_cdns *cdns, |
| 1300 | struct sdw_cdns_streams *stream, |
Bard Liao | 1b53385e | 2019-09-16 14:23:48 -0500 | [diff] [blame] | 1301 | u32 ch, u32 dir, int dai_id) |
Vinod Koul | 5d6b3c8 | 2018-04-26 18:38:53 +0530 | [diff] [blame] | 1302 | { |
| 1303 | struct sdw_cdns_pdi *pdi = NULL; |
| 1304 | |
| 1305 | if (dir == SDW_DATA_DIR_RX) |
Bard Liao | 1b53385e | 2019-09-16 14:23:48 -0500 | [diff] [blame] | 1306 | pdi = cdns_find_pdi(cdns, 0, stream->num_in, stream->in, |
| 1307 | dai_id); |
Vinod Koul | 5d6b3c8 | 2018-04-26 18:38:53 +0530 | [diff] [blame] | 1308 | else |
Bard Liao | 1b53385e | 2019-09-16 14:23:48 -0500 | [diff] [blame] | 1309 | pdi = cdns_find_pdi(cdns, 0, stream->num_out, stream->out, |
| 1310 | dai_id); |
Vinod Koul | 5d6b3c8 | 2018-04-26 18:38:53 +0530 | [diff] [blame] | 1311 | |
| 1312 | /* check if we found a PDI, else find in bi-directional */ |
| 1313 | if (!pdi) |
Bard Liao | 1b53385e | 2019-09-16 14:23:48 -0500 | [diff] [blame] | 1314 | pdi = cdns_find_pdi(cdns, 2, stream->num_bd, stream->bd, |
| 1315 | dai_id); |
Vinod Koul | 5d6b3c8 | 2018-04-26 18:38:53 +0530 | [diff] [blame] | 1316 | |
Pierre-Louis Bossart | 57a3479 | 2019-09-16 14:23:46 -0500 | [diff] [blame] | 1317 | if (pdi) { |
| 1318 | pdi->l_ch_num = 0; |
| 1319 | pdi->h_ch_num = ch - 1; |
| 1320 | pdi->dir = dir; |
| 1321 | pdi->ch_count = ch; |
| 1322 | } |
Vinod Koul | 5d6b3c8 | 2018-04-26 18:38:53 +0530 | [diff] [blame] | 1323 | |
Pierre-Louis Bossart | 57a3479 | 2019-09-16 14:23:46 -0500 | [diff] [blame] | 1324 | return pdi; |
Vinod Koul | 5d6b3c8 | 2018-04-26 18:38:53 +0530 | [diff] [blame] | 1325 | } |
Pierre-Louis Bossart | 57a3479 | 2019-09-16 14:23:46 -0500 | [diff] [blame] | 1326 | EXPORT_SYMBOL(sdw_cdns_alloc_pdi); |
Vinod Koul | 5d6b3c8 | 2018-04-26 18:38:53 +0530 | [diff] [blame] | 1327 | |
Vinod Koul | 2f52a51 | 2017-12-14 11:19:41 +0530 | [diff] [blame] | 1328 | MODULE_LICENSE("Dual BSD/GPL"); |
| 1329 | MODULE_DESCRIPTION("Cadence Soundwire Library"); |