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) { |
| 450 | dev_err(cdns->dev, "IO transfer timed out\n"); |
| 451 | msg->len = 0; |
| 452 | return SDW_CMD_TIMEOUT; |
| 453 | } |
| 454 | |
| 455 | return cdns_fill_msg_resp(cdns, msg, count, offset); |
| 456 | } |
| 457 | |
Pierre-Louis Bossart | bbb6381 | 2019-05-01 10:57:41 -0500 | [diff] [blame] | 458 | static enum sdw_command_response |
| 459 | cdns_program_scp_addr(struct sdw_cdns *cdns, struct sdw_msg *msg) |
Sanyog Kale | 956baa1 | 2017-12-14 11:19:42 +0530 | [diff] [blame] | 460 | { |
| 461 | int nack = 0, no_ack = 0; |
| 462 | unsigned long time; |
| 463 | u32 data[2], base; |
| 464 | int i; |
| 465 | |
| 466 | /* Program the watermark level for RX FIFO */ |
| 467 | if (cdns->msg_count != CDNS_SCP_RX_FIFOLEVEL) { |
| 468 | cdns_writel(cdns, CDNS_MCP_FIFOLEVEL, CDNS_SCP_RX_FIFOLEVEL); |
| 469 | cdns->msg_count = CDNS_SCP_RX_FIFOLEVEL; |
| 470 | } |
| 471 | |
| 472 | data[0] = msg->dev_num << SDW_REG_SHIFT(CDNS_MCP_CMD_DEV_ADDR); |
| 473 | data[0] |= 0x3 << SDW_REG_SHIFT(CDNS_MCP_CMD_COMMAND); |
| 474 | data[1] = data[0]; |
| 475 | |
| 476 | data[0] |= SDW_SCP_ADDRPAGE1 << SDW_REG_SHIFT(CDNS_MCP_CMD_REG_ADDR_L); |
| 477 | data[1] |= SDW_SCP_ADDRPAGE2 << SDW_REG_SHIFT(CDNS_MCP_CMD_REG_ADDR_L); |
| 478 | |
| 479 | data[0] |= msg->addr_page1; |
| 480 | data[1] |= msg->addr_page2; |
| 481 | |
| 482 | base = CDNS_MCP_CMD_BASE; |
| 483 | cdns_writel(cdns, base, data[0]); |
| 484 | base += CDNS_MCP_CMD_WORD_LEN; |
| 485 | cdns_writel(cdns, base, data[1]); |
| 486 | |
| 487 | time = wait_for_completion_timeout(&cdns->tx_complete, |
Pierre-Louis Bossart | bbb6381 | 2019-05-01 10:57:41 -0500 | [diff] [blame] | 488 | msecs_to_jiffies(CDNS_TX_TIMEOUT)); |
Sanyog Kale | 956baa1 | 2017-12-14 11:19:42 +0530 | [diff] [blame] | 489 | if (!time) { |
| 490 | dev_err(cdns->dev, "SCP Msg trf timed out\n"); |
| 491 | msg->len = 0; |
| 492 | return SDW_CMD_TIMEOUT; |
| 493 | } |
| 494 | |
| 495 | /* check response the writes */ |
| 496 | for (i = 0; i < 2; i++) { |
| 497 | if (!(cdns->response_buf[i] & CDNS_MCP_RESP_ACK)) { |
| 498 | no_ack = 1; |
Pierre-Louis Bossart | 17ed5be | 2019-05-01 10:57:45 -0500 | [diff] [blame] | 499 | dev_err(cdns->dev, "Program SCP Ack not received\n"); |
Sanyog Kale | 956baa1 | 2017-12-14 11:19:42 +0530 | [diff] [blame] | 500 | if (cdns->response_buf[i] & CDNS_MCP_RESP_NACK) { |
| 501 | nack = 1; |
Pierre-Louis Bossart | 17ed5be | 2019-05-01 10:57:45 -0500 | [diff] [blame] | 502 | dev_err(cdns->dev, "Program SCP NACK received\n"); |
Sanyog Kale | 956baa1 | 2017-12-14 11:19:42 +0530 | [diff] [blame] | 503 | } |
| 504 | } |
| 505 | } |
| 506 | |
| 507 | /* For NACK, NO ack, don't return err if we are in Broadcast mode */ |
| 508 | if (nack) { |
Pierre-Louis Bossart | eb7df4c | 2019-05-22 14:47:26 -0500 | [diff] [blame] | 509 | dev_err_ratelimited(cdns->dev, |
| 510 | "SCP_addrpage NACKed for Slave %d\n", msg->dev_num); |
Sanyog Kale | 956baa1 | 2017-12-14 11:19:42 +0530 | [diff] [blame] | 511 | return SDW_CMD_FAIL; |
| 512 | } else if (no_ack) { |
Pierre-Louis Bossart | eb7df4c | 2019-05-22 14:47:26 -0500 | [diff] [blame] | 513 | dev_dbg_ratelimited(cdns->dev, |
| 514 | "SCP_addrpage ignored for Slave %d\n", msg->dev_num); |
Sanyog Kale | 956baa1 | 2017-12-14 11:19:42 +0530 | [diff] [blame] | 515 | return SDW_CMD_IGNORED; |
| 516 | } |
| 517 | |
| 518 | return SDW_CMD_OK; |
| 519 | } |
| 520 | |
| 521 | static int cdns_prep_msg(struct sdw_cdns *cdns, struct sdw_msg *msg, int *cmd) |
| 522 | { |
| 523 | int ret; |
| 524 | |
| 525 | if (msg->page) { |
| 526 | ret = cdns_program_scp_addr(cdns, msg); |
| 527 | if (ret) { |
| 528 | msg->len = 0; |
| 529 | return ret; |
| 530 | } |
| 531 | } |
| 532 | |
| 533 | switch (msg->flags) { |
| 534 | case SDW_MSG_FLAG_READ: |
| 535 | *cmd = CDNS_MCP_CMD_READ; |
| 536 | break; |
| 537 | |
| 538 | case SDW_MSG_FLAG_WRITE: |
| 539 | *cmd = CDNS_MCP_CMD_WRITE; |
| 540 | break; |
| 541 | |
| 542 | default: |
| 543 | dev_err(cdns->dev, "Invalid msg cmd: %d\n", msg->flags); |
| 544 | return -EINVAL; |
| 545 | } |
| 546 | |
| 547 | return 0; |
| 548 | } |
| 549 | |
Shreyas NC | c91605f | 2018-04-26 18:38:43 +0530 | [diff] [blame] | 550 | enum sdw_command_response |
Sanyog Kale | 956baa1 | 2017-12-14 11:19:42 +0530 | [diff] [blame] | 551 | cdns_xfer_msg(struct sdw_bus *bus, struct sdw_msg *msg) |
| 552 | { |
| 553 | struct sdw_cdns *cdns = bus_to_cdns(bus); |
| 554 | int cmd = 0, ret, i; |
| 555 | |
| 556 | ret = cdns_prep_msg(cdns, msg, &cmd); |
| 557 | if (ret) |
| 558 | return SDW_CMD_FAIL_OTHER; |
| 559 | |
| 560 | for (i = 0; i < msg->len / CDNS_MCP_CMD_LEN; i++) { |
| 561 | 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] | 562 | CDNS_MCP_CMD_LEN, false); |
Sanyog Kale | 956baa1 | 2017-12-14 11:19:42 +0530 | [diff] [blame] | 563 | if (ret < 0) |
| 564 | goto exit; |
| 565 | } |
| 566 | |
| 567 | if (!(msg->len % CDNS_MCP_CMD_LEN)) |
| 568 | goto exit; |
| 569 | |
| 570 | 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] | 571 | msg->len % CDNS_MCP_CMD_LEN, false); |
Sanyog Kale | 956baa1 | 2017-12-14 11:19:42 +0530 | [diff] [blame] | 572 | |
| 573 | exit: |
| 574 | return ret; |
| 575 | } |
Shreyas NC | c91605f | 2018-04-26 18:38:43 +0530 | [diff] [blame] | 576 | EXPORT_SYMBOL(cdns_xfer_msg); |
Sanyog Kale | 956baa1 | 2017-12-14 11:19:42 +0530 | [diff] [blame] | 577 | |
Shreyas NC | c91605f | 2018-04-26 18:38:43 +0530 | [diff] [blame] | 578 | enum sdw_command_response |
Sanyog Kale | 956baa1 | 2017-12-14 11:19:42 +0530 | [diff] [blame] | 579 | cdns_xfer_msg_defer(struct sdw_bus *bus, |
Pierre-Louis Bossart | bbb6381 | 2019-05-01 10:57:41 -0500 | [diff] [blame] | 580 | struct sdw_msg *msg, struct sdw_defer *defer) |
Sanyog Kale | 956baa1 | 2017-12-14 11:19:42 +0530 | [diff] [blame] | 581 | { |
| 582 | struct sdw_cdns *cdns = bus_to_cdns(bus); |
| 583 | int cmd = 0, ret; |
| 584 | |
| 585 | /* for defer only 1 message is supported */ |
| 586 | if (msg->len > 1) |
| 587 | return -ENOTSUPP; |
| 588 | |
| 589 | ret = cdns_prep_msg(cdns, msg, &cmd); |
| 590 | if (ret) |
| 591 | return SDW_CMD_FAIL_OTHER; |
| 592 | |
| 593 | cdns->defer = defer; |
| 594 | cdns->defer->length = msg->len; |
| 595 | |
| 596 | return _cdns_xfer_msg(cdns, msg, cmd, 0, msg->len, true); |
| 597 | } |
Shreyas NC | c91605f | 2018-04-26 18:38:43 +0530 | [diff] [blame] | 598 | EXPORT_SYMBOL(cdns_xfer_msg_defer); |
Sanyog Kale | 956baa1 | 2017-12-14 11:19:42 +0530 | [diff] [blame] | 599 | |
Shreyas NC | c91605f | 2018-04-26 18:38:43 +0530 | [diff] [blame] | 600 | enum sdw_command_response |
Sanyog Kale | 956baa1 | 2017-12-14 11:19:42 +0530 | [diff] [blame] | 601 | cdns_reset_page_addr(struct sdw_bus *bus, unsigned int dev_num) |
| 602 | { |
| 603 | struct sdw_cdns *cdns = bus_to_cdns(bus); |
| 604 | struct sdw_msg msg; |
| 605 | |
| 606 | /* Create dummy message with valid device number */ |
| 607 | memset(&msg, 0, sizeof(msg)); |
| 608 | msg.dev_num = dev_num; |
| 609 | |
| 610 | return cdns_program_scp_addr(cdns, &msg); |
| 611 | } |
Shreyas NC | c91605f | 2018-04-26 18:38:43 +0530 | [diff] [blame] | 612 | EXPORT_SYMBOL(cdns_reset_page_addr); |
Sanyog Kale | 956baa1 | 2017-12-14 11:19:42 +0530 | [diff] [blame] | 613 | |
| 614 | /* |
Vinod Koul | 2f52a51 | 2017-12-14 11:19:41 +0530 | [diff] [blame] | 615 | * IRQ handling |
| 616 | */ |
| 617 | |
Sanyog Kale | 956baa1 | 2017-12-14 11:19:42 +0530 | [diff] [blame] | 618 | static void cdns_read_response(struct sdw_cdns *cdns) |
| 619 | { |
| 620 | u32 num_resp, cmd_base; |
| 621 | int i; |
| 622 | |
| 623 | num_resp = cdns_readl(cdns, CDNS_MCP_FIFOSTAT); |
| 624 | num_resp &= CDNS_MCP_RX_FIFO_AVAIL; |
| 625 | |
| 626 | cmd_base = CDNS_MCP_CMD_BASE; |
| 627 | |
| 628 | for (i = 0; i < num_resp; i++) { |
| 629 | cdns->response_buf[i] = cdns_readl(cdns, cmd_base); |
| 630 | cmd_base += CDNS_MCP_CMD_WORD_LEN; |
| 631 | } |
| 632 | } |
| 633 | |
Vinod Koul | 2f52a51 | 2017-12-14 11:19:41 +0530 | [diff] [blame] | 634 | static int cdns_update_slave_status(struct sdw_cdns *cdns, |
Pierre-Louis Bossart | bbb6381 | 2019-05-01 10:57:41 -0500 | [diff] [blame] | 635 | u32 slave0, u32 slave1) |
Vinod Koul | 2f52a51 | 2017-12-14 11:19:41 +0530 | [diff] [blame] | 636 | { |
| 637 | enum sdw_slave_status status[SDW_MAX_DEVICES + 1]; |
| 638 | bool is_slave = false; |
Pierre-Louis Bossart | a78b32d | 2019-05-22 14:47:27 -0500 | [diff] [blame] | 639 | u64 slave; |
| 640 | u32 mask; |
Vinod Koul | 2f52a51 | 2017-12-14 11:19:41 +0530 | [diff] [blame] | 641 | int i, set_status; |
| 642 | |
| 643 | /* combine the two status */ |
| 644 | slave = ((u64)slave1 << 32) | slave0; |
| 645 | memset(status, 0, sizeof(status)); |
| 646 | |
| 647 | for (i = 0; i <= SDW_MAX_DEVICES; i++) { |
| 648 | mask = (slave >> (i * CDNS_MCP_SLAVE_STATUS_NUM)) & |
| 649 | CDNS_MCP_SLAVE_STATUS_BITS; |
| 650 | if (!mask) |
| 651 | continue; |
| 652 | |
| 653 | is_slave = true; |
| 654 | set_status = 0; |
| 655 | |
| 656 | if (mask & CDNS_MCP_SLAVE_INTSTAT_RESERVED) { |
| 657 | status[i] = SDW_SLAVE_RESERVED; |
| 658 | set_status++; |
| 659 | } |
| 660 | |
| 661 | if (mask & CDNS_MCP_SLAVE_INTSTAT_ATTACHED) { |
| 662 | status[i] = SDW_SLAVE_ATTACHED; |
| 663 | set_status++; |
| 664 | } |
| 665 | |
| 666 | if (mask & CDNS_MCP_SLAVE_INTSTAT_ALERT) { |
| 667 | status[i] = SDW_SLAVE_ALERT; |
| 668 | set_status++; |
| 669 | } |
| 670 | |
| 671 | if (mask & CDNS_MCP_SLAVE_INTSTAT_NPRESENT) { |
| 672 | status[i] = SDW_SLAVE_UNATTACHED; |
| 673 | set_status++; |
| 674 | } |
| 675 | |
| 676 | /* first check if Slave reported multiple status */ |
| 677 | if (set_status > 1) { |
Pierre-Louis Bossart | eb7df4c | 2019-05-22 14:47:26 -0500 | [diff] [blame] | 678 | dev_warn_ratelimited(cdns->dev, |
| 679 | "Slave reported multiple Status: %d\n", |
Pierre-Louis Bossart | a78b32d | 2019-05-22 14:47:27 -0500 | [diff] [blame] | 680 | mask); |
Vinod Koul | 2f52a51 | 2017-12-14 11:19:41 +0530 | [diff] [blame] | 681 | /* |
| 682 | * TODO: we need to reread the status here by |
| 683 | * issuing a PING cmd |
| 684 | */ |
| 685 | } |
| 686 | } |
| 687 | |
| 688 | if (is_slave) |
| 689 | return sdw_handle_slave_status(&cdns->bus, status); |
| 690 | |
| 691 | return 0; |
| 692 | } |
| 693 | |
| 694 | /** |
| 695 | * sdw_cdns_irq() - Cadence interrupt handler |
| 696 | * @irq: irq number |
| 697 | * @dev_id: irq context |
| 698 | */ |
| 699 | irqreturn_t sdw_cdns_irq(int irq, void *dev_id) |
| 700 | { |
| 701 | struct sdw_cdns *cdns = dev_id; |
| 702 | u32 int_status; |
| 703 | int ret = IRQ_HANDLED; |
| 704 | |
| 705 | /* Check if the link is up */ |
| 706 | if (!cdns->link_up) |
| 707 | return IRQ_NONE; |
| 708 | |
| 709 | int_status = cdns_readl(cdns, CDNS_MCP_INTSTAT); |
| 710 | |
Pierre-Louis Bossart | a2cff9e | 2020-01-10 15:57:26 -0600 | [diff] [blame^] | 711 | /* check for reserved values read as zero */ |
| 712 | if (int_status & CDNS_MCP_INT_RESERVED) |
| 713 | return IRQ_NONE; |
| 714 | |
Vinod Koul | 2f52a51 | 2017-12-14 11:19:41 +0530 | [diff] [blame] | 715 | if (!(int_status & CDNS_MCP_INT_IRQ)) |
| 716 | return IRQ_NONE; |
| 717 | |
Sanyog Kale | 956baa1 | 2017-12-14 11:19:42 +0530 | [diff] [blame] | 718 | if (int_status & CDNS_MCP_INT_RX_WL) { |
| 719 | cdns_read_response(cdns); |
| 720 | |
| 721 | if (cdns->defer) { |
| 722 | cdns_fill_msg_resp(cdns, cdns->defer->msg, |
Pierre-Louis Bossart | bbb6381 | 2019-05-01 10:57:41 -0500 | [diff] [blame] | 723 | cdns->defer->length, 0); |
Sanyog Kale | 956baa1 | 2017-12-14 11:19:42 +0530 | [diff] [blame] | 724 | complete(&cdns->defer->complete); |
| 725 | cdns->defer = NULL; |
Pierre-Louis Bossart | f6e2096 | 2019-05-01 10:57:42 -0500 | [diff] [blame] | 726 | } else { |
Sanyog Kale | 956baa1 | 2017-12-14 11:19:42 +0530 | [diff] [blame] | 727 | complete(&cdns->tx_complete); |
Pierre-Louis Bossart | f6e2096 | 2019-05-01 10:57:42 -0500 | [diff] [blame] | 728 | } |
Sanyog Kale | 956baa1 | 2017-12-14 11:19:42 +0530 | [diff] [blame] | 729 | } |
| 730 | |
Pierre-Louis Bossart | 9b5884a | 2019-08-05 19:55:08 -0500 | [diff] [blame] | 731 | if (int_status & CDNS_MCP_INT_PARITY) { |
| 732 | /* Parity error detected by Master */ |
| 733 | dev_err_ratelimited(cdns->dev, "Parity error\n"); |
| 734 | } |
| 735 | |
Vinod Koul | 2f52a51 | 2017-12-14 11:19:41 +0530 | [diff] [blame] | 736 | if (int_status & CDNS_MCP_INT_CTRL_CLASH) { |
Vinod Koul | 2f52a51 | 2017-12-14 11:19:41 +0530 | [diff] [blame] | 737 | /* Slave is driving bit slot during control word */ |
| 738 | dev_err_ratelimited(cdns->dev, "Bus clash for control word\n"); |
Vinod Koul | 2f52a51 | 2017-12-14 11:19:41 +0530 | [diff] [blame] | 739 | } |
| 740 | |
| 741 | if (int_status & CDNS_MCP_INT_DATA_CLASH) { |
| 742 | /* |
| 743 | * Multiple slaves trying to drive bit slot, or issue with |
| 744 | * ownership of data bits or Slave gone bonkers |
| 745 | */ |
| 746 | dev_err_ratelimited(cdns->dev, "Bus clash for data word\n"); |
Vinod Koul | 2f52a51 | 2017-12-14 11:19:41 +0530 | [diff] [blame] | 747 | } |
| 748 | |
| 749 | if (int_status & CDNS_MCP_INT_SLAVE_MASK) { |
| 750 | /* Mask the Slave interrupt and wake thread */ |
| 751 | cdns_updatel(cdns, CDNS_MCP_INTMASK, |
Pierre-Louis Bossart | bbb6381 | 2019-05-01 10:57:41 -0500 | [diff] [blame] | 752 | CDNS_MCP_INT_SLAVE_MASK, 0); |
Vinod Koul | 2f52a51 | 2017-12-14 11:19:41 +0530 | [diff] [blame] | 753 | |
| 754 | int_status &= ~CDNS_MCP_INT_SLAVE_MASK; |
| 755 | ret = IRQ_WAKE_THREAD; |
| 756 | } |
| 757 | |
| 758 | cdns_writel(cdns, CDNS_MCP_INTSTAT, int_status); |
| 759 | return ret; |
| 760 | } |
| 761 | EXPORT_SYMBOL(sdw_cdns_irq); |
| 762 | |
| 763 | /** |
| 764 | * sdw_cdns_thread() - Cadence irq thread handler |
| 765 | * @irq: irq number |
| 766 | * @dev_id: irq context |
| 767 | */ |
| 768 | irqreturn_t sdw_cdns_thread(int irq, void *dev_id) |
| 769 | { |
| 770 | struct sdw_cdns *cdns = dev_id; |
| 771 | u32 slave0, slave1; |
| 772 | |
Pierre-Louis Bossart | eb7df4c | 2019-05-22 14:47:26 -0500 | [diff] [blame] | 773 | dev_dbg_ratelimited(cdns->dev, "Slave status change\n"); |
Vinod Koul | 2f52a51 | 2017-12-14 11:19:41 +0530 | [diff] [blame] | 774 | |
| 775 | slave0 = cdns_readl(cdns, CDNS_MCP_SLAVE_INTSTAT0); |
| 776 | slave1 = cdns_readl(cdns, CDNS_MCP_SLAVE_INTSTAT1); |
| 777 | |
| 778 | cdns_update_slave_status(cdns, slave0, slave1); |
| 779 | cdns_writel(cdns, CDNS_MCP_SLAVE_INTSTAT0, slave0); |
| 780 | cdns_writel(cdns, CDNS_MCP_SLAVE_INTSTAT1, slave1); |
| 781 | |
| 782 | /* clear and unmask Slave interrupt now */ |
| 783 | cdns_writel(cdns, CDNS_MCP_INTSTAT, CDNS_MCP_INT_SLAVE_MASK); |
| 784 | cdns_updatel(cdns, CDNS_MCP_INTMASK, |
Pierre-Louis Bossart | bbb6381 | 2019-05-01 10:57:41 -0500 | [diff] [blame] | 785 | CDNS_MCP_INT_SLAVE_MASK, CDNS_MCP_INT_SLAVE_MASK); |
Vinod Koul | 2f52a51 | 2017-12-14 11:19:41 +0530 | [diff] [blame] | 786 | |
| 787 | return IRQ_HANDLED; |
| 788 | } |
| 789 | EXPORT_SYMBOL(sdw_cdns_thread); |
| 790 | |
| 791 | /* |
| 792 | * init routines |
| 793 | */ |
Pierre-Louis Bossart | 49ea07d | 2019-10-22 18:54:44 -0500 | [diff] [blame] | 794 | |
| 795 | /** |
| 796 | * sdw_cdns_exit_reset() - Program reset parameters and start bus operations |
| 797 | * @cdns: Cadence instance |
| 798 | */ |
| 799 | int sdw_cdns_exit_reset(struct sdw_cdns *cdns) |
| 800 | { |
| 801 | /* program maximum length reset to be safe */ |
| 802 | cdns_updatel(cdns, CDNS_MCP_CONTROL, |
| 803 | CDNS_MCP_CONTROL_RST_DELAY, |
| 804 | CDNS_MCP_CONTROL_RST_DELAY); |
| 805 | |
| 806 | /* use hardware generated reset */ |
| 807 | cdns_updatel(cdns, CDNS_MCP_CONTROL, |
| 808 | CDNS_MCP_CONTROL_HW_RST, |
| 809 | CDNS_MCP_CONTROL_HW_RST); |
| 810 | |
| 811 | /* enable bus operations with clock and data */ |
| 812 | cdns_updatel(cdns, CDNS_MCP_CONFIG, |
| 813 | CDNS_MCP_CONFIG_OP, |
| 814 | CDNS_MCP_CONFIG_OP_NORMAL); |
| 815 | |
| 816 | /* commit changes */ |
| 817 | return cdns_update_config(cdns); |
| 818 | } |
| 819 | EXPORT_SYMBOL(sdw_cdns_exit_reset); |
| 820 | |
| 821 | /** |
| 822 | * sdw_cdns_enable_interrupt() - Enable SDW interrupts and update config |
| 823 | * @cdns: Cadence instance |
| 824 | */ |
Pierre-Louis Bossart | 9e3d47f | 2019-10-22 18:54:47 -0500 | [diff] [blame] | 825 | int sdw_cdns_enable_interrupt(struct sdw_cdns *cdns, bool state) |
Sanyog Kale | 956baa1 | 2017-12-14 11:19:42 +0530 | [diff] [blame] | 826 | { |
Pierre-Louis Bossart | 9e3d47f | 2019-10-22 18:54:47 -0500 | [diff] [blame] | 827 | u32 slave_intmask0 = 0; |
| 828 | u32 slave_intmask1 = 0; |
| 829 | u32 mask = 0; |
Sanyog Kale | 956baa1 | 2017-12-14 11:19:42 +0530 | [diff] [blame] | 830 | |
Pierre-Louis Bossart | 9e3d47f | 2019-10-22 18:54:47 -0500 | [diff] [blame] | 831 | if (!state) |
| 832 | goto update_masks; |
| 833 | |
| 834 | slave_intmask0 = CDNS_MCP_SLAVE_INTMASK0_MASK; |
| 835 | slave_intmask1 = CDNS_MCP_SLAVE_INTMASK1_MASK; |
Sanyog Kale | 956baa1 | 2017-12-14 11:19:42 +0530 | [diff] [blame] | 836 | |
Pierre-Louis Bossart | 9b5884a | 2019-08-05 19:55:08 -0500 | [diff] [blame] | 837 | /* enable detection of all slave state changes */ |
| 838 | mask = CDNS_MCP_INT_SLAVE_MASK; |
| 839 | |
| 840 | /* enable detection of bus issues */ |
| 841 | mask |= CDNS_MCP_INT_CTRL_CLASH | CDNS_MCP_INT_DATA_CLASH | |
| 842 | CDNS_MCP_INT_PARITY; |
| 843 | |
| 844 | /* no detection of port interrupts for now */ |
| 845 | |
| 846 | /* enable detection of RX fifo level */ |
| 847 | mask |= CDNS_MCP_INT_RX_WL; |
| 848 | |
| 849 | /* |
| 850 | * CDNS_MCP_INT_IRQ needs to be set otherwise all previous |
| 851 | * settings are irrelevant |
| 852 | */ |
| 853 | mask |= CDNS_MCP_INT_IRQ; |
Sanyog Kale | 956baa1 | 2017-12-14 11:19:42 +0530 | [diff] [blame] | 854 | |
Pierre-Louis Bossart | 04592dc | 2019-08-05 19:55:21 -0500 | [diff] [blame] | 855 | if (interrupt_mask) /* parameter override */ |
| 856 | mask = interrupt_mask; |
Sanyog Kale | 956baa1 | 2017-12-14 11:19:42 +0530 | [diff] [blame] | 857 | |
Pierre-Louis Bossart | 9e3d47f | 2019-10-22 18:54:47 -0500 | [diff] [blame] | 858 | update_masks: |
| 859 | cdns_writel(cdns, CDNS_MCP_SLAVE_INTMASK0, slave_intmask0); |
| 860 | cdns_writel(cdns, CDNS_MCP_SLAVE_INTMASK1, slave_intmask1); |
Sanyog Kale | 956baa1 | 2017-12-14 11:19:42 +0530 | [diff] [blame] | 861 | cdns_writel(cdns, CDNS_MCP_INTMASK, mask); |
| 862 | |
Pierre-Louis Bossart | 49ea07d | 2019-10-22 18:54:44 -0500 | [diff] [blame] | 863 | /* commit changes */ |
| 864 | return cdns_update_config(cdns); |
Sanyog Kale | 956baa1 | 2017-12-14 11:19:42 +0530 | [diff] [blame] | 865 | } |
| 866 | EXPORT_SYMBOL(sdw_cdns_enable_interrupt); |
Vinod Koul | 2f52a51 | 2017-12-14 11:19:41 +0530 | [diff] [blame] | 867 | |
Vinod Koul | 07abeff | 2018-04-26 18:38:48 +0530 | [diff] [blame] | 868 | static int cdns_allocate_pdi(struct sdw_cdns *cdns, |
Pierre-Louis Bossart | bbb6381 | 2019-05-01 10:57:41 -0500 | [diff] [blame] | 869 | struct sdw_cdns_pdi **stream, |
| 870 | u32 num, u32 pdi_offset) |
Vinod Koul | 07abeff | 2018-04-26 18:38:48 +0530 | [diff] [blame] | 871 | { |
| 872 | struct sdw_cdns_pdi *pdi; |
| 873 | int i; |
| 874 | |
| 875 | if (!num) |
| 876 | return 0; |
| 877 | |
| 878 | pdi = devm_kcalloc(cdns->dev, num, sizeof(*pdi), GFP_KERNEL); |
| 879 | if (!pdi) |
| 880 | return -ENOMEM; |
| 881 | |
| 882 | for (i = 0; i < num; i++) { |
| 883 | pdi[i].num = i + pdi_offset; |
Vinod Koul | 07abeff | 2018-04-26 18:38:48 +0530 | [diff] [blame] | 884 | } |
| 885 | |
| 886 | *stream = pdi; |
| 887 | return 0; |
| 888 | } |
| 889 | |
| 890 | /** |
| 891 | * sdw_cdns_pdi_init() - PDI initialization routine |
| 892 | * |
| 893 | * @cdns: Cadence instance |
| 894 | * @config: Stream configurations |
| 895 | */ |
| 896 | int sdw_cdns_pdi_init(struct sdw_cdns *cdns, |
Pierre-Louis Bossart | bbb6381 | 2019-05-01 10:57:41 -0500 | [diff] [blame] | 897 | struct sdw_cdns_stream_config config) |
Vinod Koul | 07abeff | 2018-04-26 18:38:48 +0530 | [diff] [blame] | 898 | { |
| 899 | struct sdw_cdns_streams *stream; |
Pierre-Louis Bossart | 57a3479 | 2019-09-16 14:23:46 -0500 | [diff] [blame] | 900 | int offset; |
| 901 | int ret; |
Vinod Koul | 07abeff | 2018-04-26 18:38:48 +0530 | [diff] [blame] | 902 | |
| 903 | cdns->pcm.num_bd = config.pcm_bd; |
| 904 | cdns->pcm.num_in = config.pcm_in; |
| 905 | cdns->pcm.num_out = config.pcm_out; |
| 906 | cdns->pdm.num_bd = config.pdm_bd; |
| 907 | cdns->pdm.num_in = config.pdm_in; |
| 908 | cdns->pdm.num_out = config.pdm_out; |
| 909 | |
| 910 | /* Allocate PDIs for PCMs */ |
| 911 | stream = &cdns->pcm; |
| 912 | |
Pierre-Louis Bossart | 807c15b | 2019-09-16 14:23:47 -0500 | [diff] [blame] | 913 | /* we allocate PDI0 and PDI1 which are used for Bulk */ |
| 914 | offset = 0; |
Vinod Koul | 07abeff | 2018-04-26 18:38:48 +0530 | [diff] [blame] | 915 | |
| 916 | ret = cdns_allocate_pdi(cdns, &stream->bd, |
| 917 | stream->num_bd, offset); |
| 918 | if (ret) |
| 919 | return ret; |
| 920 | |
| 921 | offset += stream->num_bd; |
| 922 | |
| 923 | ret = cdns_allocate_pdi(cdns, &stream->in, |
| 924 | stream->num_in, offset); |
| 925 | if (ret) |
| 926 | return ret; |
| 927 | |
| 928 | offset += stream->num_in; |
| 929 | |
| 930 | ret = cdns_allocate_pdi(cdns, &stream->out, |
| 931 | stream->num_out, offset); |
| 932 | if (ret) |
| 933 | return ret; |
| 934 | |
| 935 | /* Update total number of PCM PDIs */ |
| 936 | stream->num_pdi = stream->num_bd + stream->num_in + stream->num_out; |
| 937 | cdns->num_ports = stream->num_pdi; |
| 938 | |
| 939 | /* Allocate PDIs for PDMs */ |
| 940 | stream = &cdns->pdm; |
Vinod Koul | 07abeff | 2018-04-26 18:38:48 +0530 | [diff] [blame] | 941 | ret = cdns_allocate_pdi(cdns, &stream->bd, |
| 942 | stream->num_bd, offset); |
| 943 | if (ret) |
| 944 | return ret; |
| 945 | |
| 946 | offset += stream->num_bd; |
| 947 | |
| 948 | ret = cdns_allocate_pdi(cdns, &stream->in, |
| 949 | stream->num_in, offset); |
| 950 | if (ret) |
| 951 | return ret; |
| 952 | |
| 953 | offset += stream->num_in; |
| 954 | |
| 955 | ret = cdns_allocate_pdi(cdns, &stream->out, |
| 956 | stream->num_out, offset); |
Pierre-Louis Bossart | 807c15b | 2019-09-16 14:23:47 -0500 | [diff] [blame] | 957 | |
| 958 | offset += stream->num_out; |
| 959 | |
Vinod Koul | 07abeff | 2018-04-26 18:38:48 +0530 | [diff] [blame] | 960 | if (ret) |
| 961 | return ret; |
| 962 | |
| 963 | /* Update total number of PDM PDIs */ |
| 964 | stream->num_pdi = stream->num_bd + stream->num_in + stream->num_out; |
| 965 | cdns->num_ports += stream->num_pdi; |
| 966 | |
Vinod Koul | 07abeff | 2018-04-26 18:38:48 +0530 | [diff] [blame] | 967 | return 0; |
| 968 | } |
| 969 | EXPORT_SYMBOL(sdw_cdns_pdi_init); |
| 970 | |
Pierre-Louis Bossart | 05be59a | 2019-08-05 19:55:11 -0500 | [diff] [blame] | 971 | static u32 cdns_set_initial_frame_shape(int n_rows, int n_cols) |
| 972 | { |
| 973 | u32 val; |
| 974 | int c; |
| 975 | int r; |
| 976 | |
| 977 | r = sdw_find_row_index(n_rows); |
| 978 | c = sdw_find_col_index(n_cols) & CDNS_MCP_FRAME_SHAPE_COL_MASK; |
| 979 | |
| 980 | val = (r << CDNS_MCP_FRAME_SHAPE_ROW_OFFSET) | c; |
| 981 | |
| 982 | return val; |
| 983 | } |
| 984 | |
Vinod Koul | 2f52a51 | 2017-12-14 11:19:41 +0530 | [diff] [blame] | 985 | /** |
| 986 | * sdw_cdns_init() - Cadence initialization |
| 987 | * @cdns: Cadence instance |
| 988 | */ |
Pierre-Louis Bossart | 3ccb855 | 2019-10-22 18:54:48 -0500 | [diff] [blame] | 989 | int sdw_cdns_init(struct sdw_cdns *cdns, bool clock_stop_exit) |
Vinod Koul | 2f52a51 | 2017-12-14 11:19:41 +0530 | [diff] [blame] | 990 | { |
Pierre-Louis Bossart | 3859872 | 2019-08-05 19:55:17 -0500 | [diff] [blame] | 991 | struct sdw_bus *bus = &cdns->bus; |
| 992 | struct sdw_master_prop *prop = &bus->prop; |
Vinod Koul | 2f52a51 | 2017-12-14 11:19:41 +0530 | [diff] [blame] | 993 | u32 val; |
Pierre-Louis Bossart | 3859872 | 2019-08-05 19:55:17 -0500 | [diff] [blame] | 994 | int divider; |
Vinod Koul | 2f52a51 | 2017-12-14 11:19:41 +0530 | [diff] [blame] | 995 | int ret; |
| 996 | |
Pierre-Louis Bossart | 3ccb855 | 2019-10-22 18:54:48 -0500 | [diff] [blame] | 997 | if (clock_stop_exit) { |
| 998 | ret = cdns_clear_bit(cdns, CDNS_MCP_CONTROL, |
| 999 | CDNS_MCP_CONTROL_CLK_STOP_CLR); |
| 1000 | if (ret < 0) { |
| 1001 | dev_err(cdns->dev, "Couldn't exit from clock stop\n"); |
| 1002 | return ret; |
| 1003 | } |
Vinod Koul | 2f52a51 | 2017-12-14 11:19:41 +0530 | [diff] [blame] | 1004 | } |
| 1005 | |
| 1006 | /* Set clock divider */ |
Pierre-Louis Bossart | 3859872 | 2019-08-05 19:55:17 -0500 | [diff] [blame] | 1007 | divider = (prop->mclk_freq / prop->max_clk_freq) - 1; |
Vinod Koul | 2f52a51 | 2017-12-14 11:19:41 +0530 | [diff] [blame] | 1008 | |
Rander Wang | a50954e | 2019-08-05 19:55:18 -0500 | [diff] [blame] | 1009 | cdns_updatel(cdns, CDNS_MCP_CLK_CTRL0, |
| 1010 | CDNS_MCP_CLK_MCLKD_MASK, divider); |
| 1011 | cdns_updatel(cdns, CDNS_MCP_CLK_CTRL1, |
| 1012 | CDNS_MCP_CLK_MCLKD_MASK, divider); |
Vinod Koul | 2f52a51 | 2017-12-14 11:19:41 +0530 | [diff] [blame] | 1013 | |
Pierre-Louis Bossart | 05be59a | 2019-08-05 19:55:11 -0500 | [diff] [blame] | 1014 | /* |
| 1015 | * Frame shape changes after initialization have to be done |
| 1016 | * with the bank switch mechanism |
| 1017 | */ |
| 1018 | val = cdns_set_initial_frame_shape(prop->default_row, |
| 1019 | prop->default_col); |
| 1020 | cdns_writel(cdns, CDNS_MCP_FRAME_SHAPE_INIT, val); |
Vinod Koul | 2f52a51 | 2017-12-14 11:19:41 +0530 | [diff] [blame] | 1021 | |
| 1022 | /* Set SSP interval to default value */ |
| 1023 | cdns_writel(cdns, CDNS_MCP_SSP_CTRL0, CDNS_DEFAULT_SSP_INTERVAL); |
| 1024 | cdns_writel(cdns, CDNS_MCP_SSP_CTRL1, CDNS_DEFAULT_SSP_INTERVAL); |
| 1025 | |
Pierre-Louis Bossart | 49ea07d | 2019-10-22 18:54:44 -0500 | [diff] [blame] | 1026 | /* flush command FIFOs */ |
| 1027 | cdns_updatel(cdns, CDNS_MCP_CONTROL, CDNS_MCP_CONTROL_CMD_RST, |
| 1028 | CDNS_MCP_CONTROL_CMD_RST); |
| 1029 | |
Vinod Koul | 2f52a51 | 2017-12-14 11:19:41 +0530 | [diff] [blame] | 1030 | /* Set cmd accept mode */ |
| 1031 | cdns_updatel(cdns, CDNS_MCP_CONTROL, CDNS_MCP_CONTROL_CMD_ACCEPT, |
Pierre-Louis Bossart | bbb6381 | 2019-05-01 10:57:41 -0500 | [diff] [blame] | 1032 | CDNS_MCP_CONTROL_CMD_ACCEPT); |
Vinod Koul | 2f52a51 | 2017-12-14 11:19:41 +0530 | [diff] [blame] | 1033 | |
| 1034 | /* Configure mcp config */ |
| 1035 | val = cdns_readl(cdns, CDNS_MCP_CONFIG); |
| 1036 | |
| 1037 | /* Set Max cmd retry to 15 */ |
| 1038 | val |= CDNS_MCP_CONFIG_MCMD_RETRY; |
| 1039 | |
| 1040 | /* Set frame delay between PREQ and ping frame to 15 frames */ |
| 1041 | val |= 0xF << SDW_REG_SHIFT(CDNS_MCP_CONFIG_MPREQ_DELAY); |
| 1042 | |
| 1043 | /* Disable auto bus release */ |
| 1044 | val &= ~CDNS_MCP_CONFIG_BUS_REL; |
| 1045 | |
| 1046 | /* Disable sniffer mode */ |
| 1047 | val &= ~CDNS_MCP_CONFIG_SNIFFER; |
| 1048 | |
| 1049 | /* Set cmd mode for Tx and Rx cmds */ |
| 1050 | val &= ~CDNS_MCP_CONFIG_CMD; |
| 1051 | |
Vinod Koul | 2f52a51 | 2017-12-14 11:19:41 +0530 | [diff] [blame] | 1052 | cdns_writel(cdns, CDNS_MCP_CONFIG, val); |
| 1053 | |
Pierre-Louis Bossart | 49ea07d | 2019-10-22 18:54:44 -0500 | [diff] [blame] | 1054 | /* commit changes */ |
| 1055 | return cdns_update_config(cdns); |
Vinod Koul | 2f52a51 | 2017-12-14 11:19:41 +0530 | [diff] [blame] | 1056 | } |
| 1057 | EXPORT_SYMBOL(sdw_cdns_init); |
| 1058 | |
Vinod Koul | 07abeff | 2018-04-26 18:38:48 +0530 | [diff] [blame] | 1059 | int cdns_bus_conf(struct sdw_bus *bus, struct sdw_bus_params *params) |
| 1060 | { |
Pierre-Louis Bossart | 3859872 | 2019-08-05 19:55:17 -0500 | [diff] [blame] | 1061 | struct sdw_master_prop *prop = &bus->prop; |
Vinod Koul | 07abeff | 2018-04-26 18:38:48 +0530 | [diff] [blame] | 1062 | struct sdw_cdns *cdns = bus_to_cdns(bus); |
Rander Wang | a50954e | 2019-08-05 19:55:18 -0500 | [diff] [blame] | 1063 | int mcp_clkctrl_off; |
Vinod Koul | 07abeff | 2018-04-26 18:38:48 +0530 | [diff] [blame] | 1064 | int divider; |
| 1065 | |
| 1066 | if (!params->curr_dr_freq) { |
Pierre-Louis Bossart | 17ed5be | 2019-05-01 10:57:45 -0500 | [diff] [blame] | 1067 | dev_err(cdns->dev, "NULL curr_dr_freq\n"); |
Vinod Koul | 07abeff | 2018-04-26 18:38:48 +0530 | [diff] [blame] | 1068 | return -EINVAL; |
| 1069 | } |
| 1070 | |
Pierre-Louis Bossart | 3859872 | 2019-08-05 19:55:17 -0500 | [diff] [blame] | 1071 | divider = prop->mclk_freq * SDW_DOUBLE_RATE_FACTOR / |
| 1072 | params->curr_dr_freq; |
| 1073 | divider--; /* divider is 1/(N+1) */ |
Vinod Koul | 07abeff | 2018-04-26 18:38:48 +0530 | [diff] [blame] | 1074 | |
| 1075 | if (params->next_bank) |
| 1076 | mcp_clkctrl_off = CDNS_MCP_CLK_CTRL1; |
| 1077 | else |
| 1078 | mcp_clkctrl_off = CDNS_MCP_CLK_CTRL0; |
| 1079 | |
Rander Wang | a50954e | 2019-08-05 19:55:18 -0500 | [diff] [blame] | 1080 | cdns_updatel(cdns, mcp_clkctrl_off, CDNS_MCP_CLK_MCLKD_MASK, divider); |
Vinod Koul | 07abeff | 2018-04-26 18:38:48 +0530 | [diff] [blame] | 1081 | |
| 1082 | return 0; |
| 1083 | } |
| 1084 | EXPORT_SYMBOL(cdns_bus_conf); |
| 1085 | |
| 1086 | static int cdns_port_params(struct sdw_bus *bus, |
Pierre-Louis Bossart | bbb6381 | 2019-05-01 10:57:41 -0500 | [diff] [blame] | 1087 | struct sdw_port_params *p_params, unsigned int bank) |
Vinod Koul | 07abeff | 2018-04-26 18:38:48 +0530 | [diff] [blame] | 1088 | { |
| 1089 | struct sdw_cdns *cdns = bus_to_cdns(bus); |
| 1090 | int dpn_config = 0, dpn_config_off; |
| 1091 | |
| 1092 | if (bank) |
| 1093 | dpn_config_off = CDNS_DPN_B1_CONFIG(p_params->num); |
| 1094 | else |
| 1095 | dpn_config_off = CDNS_DPN_B0_CONFIG(p_params->num); |
| 1096 | |
| 1097 | dpn_config = cdns_readl(cdns, dpn_config_off); |
| 1098 | |
| 1099 | dpn_config |= ((p_params->bps - 1) << |
| 1100 | SDW_REG_SHIFT(CDNS_DPN_CONFIG_WL)); |
| 1101 | dpn_config |= (p_params->flow_mode << |
| 1102 | SDW_REG_SHIFT(CDNS_DPN_CONFIG_PORT_FLOW)); |
| 1103 | dpn_config |= (p_params->data_mode << |
| 1104 | SDW_REG_SHIFT(CDNS_DPN_CONFIG_PORT_DAT)); |
| 1105 | |
| 1106 | cdns_writel(cdns, dpn_config_off, dpn_config); |
| 1107 | |
| 1108 | return 0; |
| 1109 | } |
| 1110 | |
| 1111 | static int cdns_transport_params(struct sdw_bus *bus, |
Pierre-Louis Bossart | bbb6381 | 2019-05-01 10:57:41 -0500 | [diff] [blame] | 1112 | struct sdw_transport_params *t_params, |
| 1113 | enum sdw_reg_bank bank) |
Vinod Koul | 07abeff | 2018-04-26 18:38:48 +0530 | [diff] [blame] | 1114 | { |
| 1115 | struct sdw_cdns *cdns = bus_to_cdns(bus); |
| 1116 | int dpn_offsetctrl = 0, dpn_offsetctrl_off; |
| 1117 | int dpn_config = 0, dpn_config_off; |
| 1118 | int dpn_hctrl = 0, dpn_hctrl_off; |
| 1119 | int num = t_params->port_num; |
| 1120 | int dpn_samplectrl_off; |
| 1121 | |
| 1122 | /* |
| 1123 | * Note: Only full data port is supported on the Master side for |
| 1124 | * both PCM and PDM ports. |
| 1125 | */ |
| 1126 | |
| 1127 | if (bank) { |
| 1128 | dpn_config_off = CDNS_DPN_B1_CONFIG(num); |
| 1129 | dpn_samplectrl_off = CDNS_DPN_B1_SAMPLE_CTRL(num); |
| 1130 | dpn_hctrl_off = CDNS_DPN_B1_HCTRL(num); |
| 1131 | dpn_offsetctrl_off = CDNS_DPN_B1_OFFSET_CTRL(num); |
| 1132 | } else { |
| 1133 | dpn_config_off = CDNS_DPN_B0_CONFIG(num); |
| 1134 | dpn_samplectrl_off = CDNS_DPN_B0_SAMPLE_CTRL(num); |
| 1135 | dpn_hctrl_off = CDNS_DPN_B0_HCTRL(num); |
| 1136 | dpn_offsetctrl_off = CDNS_DPN_B0_OFFSET_CTRL(num); |
| 1137 | } |
| 1138 | |
| 1139 | dpn_config = cdns_readl(cdns, dpn_config_off); |
| 1140 | |
| 1141 | dpn_config |= (t_params->blk_grp_ctrl << |
| 1142 | SDW_REG_SHIFT(CDNS_DPN_CONFIG_BGC)); |
| 1143 | dpn_config |= (t_params->blk_pkg_mode << |
| 1144 | SDW_REG_SHIFT(CDNS_DPN_CONFIG_BPM)); |
| 1145 | cdns_writel(cdns, dpn_config_off, dpn_config); |
| 1146 | |
| 1147 | dpn_offsetctrl |= (t_params->offset1 << |
| 1148 | SDW_REG_SHIFT(CDNS_DPN_OFFSET_CTRL_1)); |
| 1149 | dpn_offsetctrl |= (t_params->offset2 << |
| 1150 | SDW_REG_SHIFT(CDNS_DPN_OFFSET_CTRL_2)); |
| 1151 | cdns_writel(cdns, dpn_offsetctrl_off, dpn_offsetctrl); |
| 1152 | |
| 1153 | dpn_hctrl |= (t_params->hstart << |
| 1154 | SDW_REG_SHIFT(CDNS_DPN_HCTRL_HSTART)); |
| 1155 | dpn_hctrl |= (t_params->hstop << SDW_REG_SHIFT(CDNS_DPN_HCTRL_HSTOP)); |
| 1156 | dpn_hctrl |= (t_params->lane_ctrl << |
| 1157 | SDW_REG_SHIFT(CDNS_DPN_HCTRL_LCTRL)); |
| 1158 | |
| 1159 | cdns_writel(cdns, dpn_hctrl_off, dpn_hctrl); |
| 1160 | cdns_writel(cdns, dpn_samplectrl_off, (t_params->sample_interval - 1)); |
| 1161 | |
| 1162 | return 0; |
| 1163 | } |
| 1164 | |
| 1165 | static int cdns_port_enable(struct sdw_bus *bus, |
Pierre-Louis Bossart | bbb6381 | 2019-05-01 10:57:41 -0500 | [diff] [blame] | 1166 | struct sdw_enable_ch *enable_ch, unsigned int bank) |
Vinod Koul | 07abeff | 2018-04-26 18:38:48 +0530 | [diff] [blame] | 1167 | { |
| 1168 | struct sdw_cdns *cdns = bus_to_cdns(bus); |
| 1169 | int dpn_chnen_off, ch_mask; |
| 1170 | |
| 1171 | if (bank) |
| 1172 | dpn_chnen_off = CDNS_DPN_B1_CH_EN(enable_ch->port_num); |
| 1173 | else |
| 1174 | dpn_chnen_off = CDNS_DPN_B0_CH_EN(enable_ch->port_num); |
| 1175 | |
| 1176 | ch_mask = enable_ch->ch_mask * enable_ch->enable; |
| 1177 | cdns_writel(cdns, dpn_chnen_off, ch_mask); |
| 1178 | |
| 1179 | return 0; |
| 1180 | } |
| 1181 | |
| 1182 | static const struct sdw_master_port_ops cdns_port_ops = { |
| 1183 | .dpn_set_port_params = cdns_port_params, |
| 1184 | .dpn_set_port_transport_params = cdns_transport_params, |
| 1185 | .dpn_port_enable_ch = cdns_port_enable, |
| 1186 | }; |
| 1187 | |
Sanyog Kale | 956baa1 | 2017-12-14 11:19:42 +0530 | [diff] [blame] | 1188 | /** |
| 1189 | * sdw_cdns_probe() - Cadence probe routine |
| 1190 | * @cdns: Cadence instance |
| 1191 | */ |
| 1192 | int sdw_cdns_probe(struct sdw_cdns *cdns) |
| 1193 | { |
| 1194 | init_completion(&cdns->tx_complete); |
Vinod Koul | 07abeff | 2018-04-26 18:38:48 +0530 | [diff] [blame] | 1195 | cdns->bus.port_ops = &cdns_port_ops; |
Sanyog Kale | 956baa1 | 2017-12-14 11:19:42 +0530 | [diff] [blame] | 1196 | |
| 1197 | return 0; |
| 1198 | } |
| 1199 | EXPORT_SYMBOL(sdw_cdns_probe); |
| 1200 | |
Vinod Koul | 5d6b3c8 | 2018-04-26 18:38:53 +0530 | [diff] [blame] | 1201 | int cdns_set_sdw_stream(struct snd_soc_dai *dai, |
Pierre-Louis Bossart | bbb6381 | 2019-05-01 10:57:41 -0500 | [diff] [blame] | 1202 | void *stream, bool pcm, int direction) |
Vinod Koul | 5d6b3c8 | 2018-04-26 18:38:53 +0530 | [diff] [blame] | 1203 | { |
| 1204 | struct sdw_cdns *cdns = snd_soc_dai_get_drvdata(dai); |
| 1205 | struct sdw_cdns_dma_data *dma; |
| 1206 | |
| 1207 | dma = kzalloc(sizeof(*dma), GFP_KERNEL); |
| 1208 | if (!dma) |
| 1209 | return -ENOMEM; |
| 1210 | |
| 1211 | if (pcm) |
| 1212 | dma->stream_type = SDW_STREAM_PCM; |
| 1213 | else |
| 1214 | dma->stream_type = SDW_STREAM_PDM; |
| 1215 | |
| 1216 | dma->bus = &cdns->bus; |
| 1217 | dma->link_id = cdns->instance; |
| 1218 | |
| 1219 | dma->stream = stream; |
| 1220 | |
| 1221 | if (direction == SNDRV_PCM_STREAM_PLAYBACK) |
| 1222 | dai->playback_dma_data = dma; |
| 1223 | else |
| 1224 | dai->capture_dma_data = dma; |
| 1225 | |
| 1226 | return 0; |
| 1227 | } |
| 1228 | EXPORT_SYMBOL(cdns_set_sdw_stream); |
| 1229 | |
| 1230 | /** |
| 1231 | * cdns_find_pdi() - Find a free PDI |
| 1232 | * |
| 1233 | * @cdns: Cadence instance |
| 1234 | * @num: Number of PDIs |
| 1235 | * @pdi: PDI instances |
| 1236 | * |
Bard Liao | 1b53385e | 2019-09-16 14:23:48 -0500 | [diff] [blame] | 1237 | * Find a PDI for a given PDI array. The PDI num and dai_id are |
| 1238 | * expected to match, return NULL otherwise. |
Vinod Koul | 5d6b3c8 | 2018-04-26 18:38:53 +0530 | [diff] [blame] | 1239 | */ |
| 1240 | 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] | 1241 | unsigned int offset, |
Pierre-Louis Bossart | bbb6381 | 2019-05-01 10:57:41 -0500 | [diff] [blame] | 1242 | unsigned int num, |
Bard Liao | 1b53385e | 2019-09-16 14:23:48 -0500 | [diff] [blame] | 1243 | struct sdw_cdns_pdi *pdi, |
| 1244 | int dai_id) |
Vinod Koul | 5d6b3c8 | 2018-04-26 18:38:53 +0530 | [diff] [blame] | 1245 | { |
| 1246 | int i; |
| 1247 | |
Bard Liao | 1b53385e | 2019-09-16 14:23:48 -0500 | [diff] [blame] | 1248 | for (i = offset; i < offset + num; i++) |
| 1249 | if (pdi[i].num == dai_id) |
| 1250 | return &pdi[i]; |
Vinod Koul | 5d6b3c8 | 2018-04-26 18:38:53 +0530 | [diff] [blame] | 1251 | |
| 1252 | return NULL; |
| 1253 | } |
| 1254 | |
| 1255 | /** |
| 1256 | * sdw_cdns_config_stream: Configure a stream |
| 1257 | * |
| 1258 | * @cdns: Cadence instance |
Vinod Koul | 5d6b3c8 | 2018-04-26 18:38:53 +0530 | [diff] [blame] | 1259 | * @ch: Channel count |
| 1260 | * @dir: Data direction |
| 1261 | * @pdi: PDI to be used |
| 1262 | */ |
| 1263 | void sdw_cdns_config_stream(struct sdw_cdns *cdns, |
Pierre-Louis Bossart | bbb6381 | 2019-05-01 10:57:41 -0500 | [diff] [blame] | 1264 | u32 ch, u32 dir, struct sdw_cdns_pdi *pdi) |
Vinod Koul | 5d6b3c8 | 2018-04-26 18:38:53 +0530 | [diff] [blame] | 1265 | { |
| 1266 | u32 offset, val = 0; |
| 1267 | |
| 1268 | if (dir == SDW_DATA_DIR_RX) |
| 1269 | val = CDNS_PORTCTRL_DIRN; |
| 1270 | |
Pierre-Louis Bossart | 57a3479 | 2019-09-16 14:23:46 -0500 | [diff] [blame] | 1271 | offset = CDNS_PORTCTRL + pdi->num * CDNS_PORT_OFFSET; |
Vinod Koul | 5d6b3c8 | 2018-04-26 18:38:53 +0530 | [diff] [blame] | 1272 | cdns_updatel(cdns, offset, CDNS_PORTCTRL_DIRN, val); |
| 1273 | |
Pierre-Louis Bossart | 57a3479 | 2019-09-16 14:23:46 -0500 | [diff] [blame] | 1274 | val = pdi->num; |
Vinod Koul | 5d6b3c8 | 2018-04-26 18:38:53 +0530 | [diff] [blame] | 1275 | val |= ((1 << ch) - 1) << SDW_REG_SHIFT(CDNS_PDI_CONFIG_CHANNEL); |
| 1276 | cdns_writel(cdns, CDNS_PDI_CONFIG(pdi->num), val); |
| 1277 | } |
| 1278 | EXPORT_SYMBOL(sdw_cdns_config_stream); |
| 1279 | |
| 1280 | /** |
Pierre-Louis Bossart | 57a3479 | 2019-09-16 14:23:46 -0500 | [diff] [blame] | 1281 | * sdw_cdns_alloc_pdi() - Allocate a PDI |
Vinod Koul | 5d6b3c8 | 2018-04-26 18:38:53 +0530 | [diff] [blame] | 1282 | * |
| 1283 | * @cdns: Cadence instance |
| 1284 | * @stream: Stream to be allocated |
| 1285 | * @ch: Channel count |
| 1286 | * @dir: Data direction |
| 1287 | */ |
Pierre-Louis Bossart | 57a3479 | 2019-09-16 14:23:46 -0500 | [diff] [blame] | 1288 | struct sdw_cdns_pdi *sdw_cdns_alloc_pdi(struct sdw_cdns *cdns, |
| 1289 | struct sdw_cdns_streams *stream, |
Bard Liao | 1b53385e | 2019-09-16 14:23:48 -0500 | [diff] [blame] | 1290 | u32 ch, u32 dir, int dai_id) |
Vinod Koul | 5d6b3c8 | 2018-04-26 18:38:53 +0530 | [diff] [blame] | 1291 | { |
| 1292 | struct sdw_cdns_pdi *pdi = NULL; |
| 1293 | |
| 1294 | if (dir == SDW_DATA_DIR_RX) |
Bard Liao | 1b53385e | 2019-09-16 14:23:48 -0500 | [diff] [blame] | 1295 | pdi = cdns_find_pdi(cdns, 0, stream->num_in, stream->in, |
| 1296 | dai_id); |
Vinod Koul | 5d6b3c8 | 2018-04-26 18:38:53 +0530 | [diff] [blame] | 1297 | else |
Bard Liao | 1b53385e | 2019-09-16 14:23:48 -0500 | [diff] [blame] | 1298 | pdi = cdns_find_pdi(cdns, 0, stream->num_out, stream->out, |
| 1299 | dai_id); |
Vinod Koul | 5d6b3c8 | 2018-04-26 18:38:53 +0530 | [diff] [blame] | 1300 | |
| 1301 | /* check if we found a PDI, else find in bi-directional */ |
| 1302 | if (!pdi) |
Bard Liao | 1b53385e | 2019-09-16 14:23:48 -0500 | [diff] [blame] | 1303 | pdi = cdns_find_pdi(cdns, 2, stream->num_bd, stream->bd, |
| 1304 | dai_id); |
Vinod Koul | 5d6b3c8 | 2018-04-26 18:38:53 +0530 | [diff] [blame] | 1305 | |
Pierre-Louis Bossart | 57a3479 | 2019-09-16 14:23:46 -0500 | [diff] [blame] | 1306 | if (pdi) { |
| 1307 | pdi->l_ch_num = 0; |
| 1308 | pdi->h_ch_num = ch - 1; |
| 1309 | pdi->dir = dir; |
| 1310 | pdi->ch_count = ch; |
| 1311 | } |
Vinod Koul | 5d6b3c8 | 2018-04-26 18:38:53 +0530 | [diff] [blame] | 1312 | |
Pierre-Louis Bossart | 57a3479 | 2019-09-16 14:23:46 -0500 | [diff] [blame] | 1313 | return pdi; |
Vinod Koul | 5d6b3c8 | 2018-04-26 18:38:53 +0530 | [diff] [blame] | 1314 | } |
Pierre-Louis Bossart | 57a3479 | 2019-09-16 14:23:46 -0500 | [diff] [blame] | 1315 | EXPORT_SYMBOL(sdw_cdns_alloc_pdi); |
Vinod Koul | 5d6b3c8 | 2018-04-26 18:38:53 +0530 | [diff] [blame] | 1316 | |
Vinod Koul | 2f52a51 | 2017-12-14 11:19:41 +0530 | [diff] [blame] | 1317 | MODULE_LICENSE("Dual BSD/GPL"); |
| 1318 | MODULE_DESCRIPTION("Cadence Soundwire Library"); |