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> 11 #include <linux/debugfs.h> 12 #include <linux/interrupt.h> 13 #include <linux/io.h> 14 #include <linux/module.h> 15 #include <linux/mod_devicetable.h> 16 #include <linux/pm_runtime.h> 17 #include <linux/soundwire/sdw_registers.h> 18 #include <linux/soundwire/sdw.h> 19 #include <sound/pcm_params.h> 20 #include <sound/soc.h> 21 #include <linux/workqueue.h> 22 #include "bus.h" 23 #include "cadence_master.h" 24 25 static int interrupt_mask; 26 module_param_named(cnds_mcp_int_mask, interrupt_mask, int, 0444); 27 MODULE_PARM_DESC(cdns_mcp_int_mask, "Cadence MCP IntMask"); 28 29 #define CDNS_MCP_CONFIG 0x0 30 #define CDNS_MCP_CONFIG_BUS_REL BIT(6) 31 32 #define CDNS_IP_MCP_CONFIG 0x0 /* IP offset added at run-time */ 33 34 #define CDNS_IP_MCP_CONFIG_MCMD_RETRY GENMASK(27, 24) 35 #define CDNS_IP_MCP_CONFIG_MPREQ_DELAY GENMASK(20, 16) 36 #define CDNS_IP_MCP_CONFIG_MMASTER BIT(7) 37 #define CDNS_IP_MCP_CONFIG_SNIFFER BIT(5) 38 #define CDNS_IP_MCP_CONFIG_CMD BIT(3) 39 #define CDNS_IP_MCP_CONFIG_OP GENMASK(2, 0) 40 #define CDNS_IP_MCP_CONFIG_OP_NORMAL 0 41 42 #define CDNS_MCP_CONTROL 0x4 43 44 #define CDNS_MCP_CONTROL_CMD_RST BIT(7) 45 #define CDNS_MCP_CONTROL_SOFT_RST BIT(6) 46 #define CDNS_MCP_CONTROL_HW_RST BIT(4) 47 #define CDNS_MCP_CONTROL_CLK_STOP_CLR BIT(2) 48 49 #define CDNS_IP_MCP_CONTROL 0x4 /* IP offset added at run-time */ 50 51 #define CDNS_IP_MCP_CONTROL_RST_DELAY GENMASK(10, 8) 52 #define CDNS_IP_MCP_CONTROL_SW_RST BIT(5) 53 #define CDNS_IP_MCP_CONTROL_CLK_PAUSE BIT(3) 54 #define CDNS_IP_MCP_CONTROL_CMD_ACCEPT BIT(1) 55 #define CDNS_IP_MCP_CONTROL_BLOCK_WAKEUP BIT(0) 56 57 #define CDNS_IP_MCP_CMDCTRL 0x8 /* IP offset added at run-time */ 58 59 #define CDNS_IP_MCP_CMDCTRL_INSERT_PARITY_ERR BIT(2) 60 61 #define CDNS_MCP_SSPSTAT 0xC 62 #define CDNS_MCP_FRAME_SHAPE 0x10 63 #define CDNS_MCP_FRAME_SHAPE_INIT 0x14 64 #define CDNS_MCP_FRAME_SHAPE_COL_MASK GENMASK(2, 0) 65 #define CDNS_MCP_FRAME_SHAPE_ROW_MASK GENMASK(7, 3) 66 67 #define CDNS_MCP_CONFIG_UPDATE 0x18 68 #define CDNS_MCP_CONFIG_UPDATE_BIT BIT(0) 69 70 #define CDNS_MCP_PHYCTRL 0x1C 71 #define CDNS_MCP_SSP_CTRL0 0x20 72 #define CDNS_MCP_SSP_CTRL1 0x28 73 #define CDNS_MCP_CLK_CTRL0 0x30 74 #define CDNS_MCP_CLK_CTRL1 0x38 75 #define CDNS_MCP_CLK_MCLKD_MASK GENMASK(7, 0) 76 77 #define CDNS_MCP_STAT 0x40 78 79 #define CDNS_MCP_STAT_ACTIVE_BANK BIT(20) 80 #define CDNS_MCP_STAT_CLK_STOP BIT(16) 81 82 #define CDNS_MCP_INTSTAT 0x44 83 #define CDNS_MCP_INTMASK 0x48 84 85 #define CDNS_MCP_INT_IRQ BIT(31) 86 #define CDNS_MCP_INT_RESERVED1 GENMASK(30, 17) 87 #define CDNS_MCP_INT_WAKEUP BIT(16) 88 #define CDNS_MCP_INT_SLAVE_RSVD BIT(15) 89 #define CDNS_MCP_INT_SLAVE_ALERT BIT(14) 90 #define CDNS_MCP_INT_SLAVE_ATTACH BIT(13) 91 #define CDNS_MCP_INT_SLAVE_NATTACH BIT(12) 92 #define CDNS_MCP_INT_SLAVE_MASK GENMASK(15, 12) 93 #define CDNS_MCP_INT_DPINT BIT(11) 94 #define CDNS_MCP_INT_CTRL_CLASH BIT(10) 95 #define CDNS_MCP_INT_DATA_CLASH BIT(9) 96 #define CDNS_MCP_INT_PARITY BIT(8) 97 #define CDNS_MCP_INT_CMD_ERR BIT(7) 98 #define CDNS_MCP_INT_RESERVED2 GENMASK(6, 4) 99 #define CDNS_MCP_INT_RX_NE BIT(3) 100 #define CDNS_MCP_INT_RX_WL BIT(2) 101 #define CDNS_MCP_INT_TXE BIT(1) 102 #define CDNS_MCP_INT_TXF BIT(0) 103 #define CDNS_MCP_INT_RESERVED (CDNS_MCP_INT_RESERVED1 | CDNS_MCP_INT_RESERVED2) 104 105 #define CDNS_MCP_INTSET 0x4C 106 107 #define CDNS_MCP_SLAVE_STAT 0x50 108 #define CDNS_MCP_SLAVE_STAT_MASK GENMASK(1, 0) 109 110 #define CDNS_MCP_SLAVE_INTSTAT0 0x54 111 #define CDNS_MCP_SLAVE_INTSTAT1 0x58 112 #define CDNS_MCP_SLAVE_INTSTAT_NPRESENT BIT(0) 113 #define CDNS_MCP_SLAVE_INTSTAT_ATTACHED BIT(1) 114 #define CDNS_MCP_SLAVE_INTSTAT_ALERT BIT(2) 115 #define CDNS_MCP_SLAVE_INTSTAT_RESERVED BIT(3) 116 #define CDNS_MCP_SLAVE_STATUS_BITS GENMASK(3, 0) 117 #define CDNS_MCP_SLAVE_STATUS_NUM 4 118 119 #define CDNS_MCP_SLAVE_INTMASK0 0x5C 120 #define CDNS_MCP_SLAVE_INTMASK1 0x60 121 122 #define CDNS_MCP_SLAVE_INTMASK0_MASK GENMASK(31, 0) 123 #define CDNS_MCP_SLAVE_INTMASK1_MASK GENMASK(15, 0) 124 125 #define CDNS_MCP_PORT_INTSTAT 0x64 126 #define CDNS_MCP_PDI_STAT 0x6C 127 128 #define CDNS_MCP_FIFOLEVEL 0x78 129 #define CDNS_MCP_FIFOSTAT 0x7C 130 #define CDNS_MCP_RX_FIFO_AVAIL GENMASK(5, 0) 131 132 #define CDNS_IP_MCP_CMD_BASE 0x80 /* IP offset added at run-time */ 133 #define CDNS_IP_MCP_RESP_BASE 0x80 /* IP offset added at run-time */ 134 /* FIFO can hold 8 commands */ 135 #define CDNS_MCP_CMD_LEN 8 136 #define CDNS_MCP_CMD_WORD_LEN 0x4 137 138 #define CDNS_MCP_CMD_SSP_TAG BIT(31) 139 #define CDNS_MCP_CMD_COMMAND GENMASK(30, 28) 140 #define CDNS_MCP_CMD_DEV_ADDR GENMASK(27, 24) 141 #define CDNS_MCP_CMD_REG_ADDR GENMASK(23, 8) 142 #define CDNS_MCP_CMD_REG_DATA GENMASK(7, 0) 143 144 #define CDNS_MCP_CMD_READ 2 145 #define CDNS_MCP_CMD_WRITE 3 146 147 #define CDNS_MCP_RESP_RDATA GENMASK(15, 8) 148 #define CDNS_MCP_RESP_ACK BIT(0) 149 #define CDNS_MCP_RESP_NACK BIT(1) 150 151 #define CDNS_DP_SIZE 128 152 153 #define CDNS_DPN_B0_CONFIG(n) (0x100 + CDNS_DP_SIZE * (n)) 154 #define CDNS_DPN_B0_CH_EN(n) (0x104 + CDNS_DP_SIZE * (n)) 155 #define CDNS_DPN_B0_SAMPLE_CTRL(n) (0x108 + CDNS_DP_SIZE * (n)) 156 #define CDNS_DPN_B0_OFFSET_CTRL(n) (0x10C + CDNS_DP_SIZE * (n)) 157 #define CDNS_DPN_B0_HCTRL(n) (0x110 + CDNS_DP_SIZE * (n)) 158 #define CDNS_DPN_B0_ASYNC_CTRL(n) (0x114 + CDNS_DP_SIZE * (n)) 159 160 #define CDNS_DPN_B1_CONFIG(n) (0x118 + CDNS_DP_SIZE * (n)) 161 #define CDNS_DPN_B1_CH_EN(n) (0x11C + CDNS_DP_SIZE * (n)) 162 #define CDNS_DPN_B1_SAMPLE_CTRL(n) (0x120 + CDNS_DP_SIZE * (n)) 163 #define CDNS_DPN_B1_OFFSET_CTRL(n) (0x124 + CDNS_DP_SIZE * (n)) 164 #define CDNS_DPN_B1_HCTRL(n) (0x128 + CDNS_DP_SIZE * (n)) 165 #define CDNS_DPN_B1_ASYNC_CTRL(n) (0x12C + CDNS_DP_SIZE * (n)) 166 167 #define CDNS_DPN_CONFIG_BPM BIT(18) 168 #define CDNS_DPN_CONFIG_BGC GENMASK(17, 16) 169 #define CDNS_DPN_CONFIG_WL GENMASK(12, 8) 170 #define CDNS_DPN_CONFIG_PORT_DAT GENMASK(3, 2) 171 #define CDNS_DPN_CONFIG_PORT_FLOW GENMASK(1, 0) 172 173 #define CDNS_DPN_SAMPLE_CTRL_SI GENMASK(15, 0) 174 175 #define CDNS_DPN_OFFSET_CTRL_1 GENMASK(7, 0) 176 #define CDNS_DPN_OFFSET_CTRL_2 GENMASK(15, 8) 177 178 #define CDNS_DPN_HCTRL_HSTOP GENMASK(3, 0) 179 #define CDNS_DPN_HCTRL_HSTART GENMASK(7, 4) 180 #define CDNS_DPN_HCTRL_LCTRL GENMASK(10, 8) 181 182 #define CDNS_PORTCTRL 0x130 183 #define CDNS_PORTCTRL_TEST_FAILED BIT(1) 184 #define CDNS_PORTCTRL_DIRN BIT(7) 185 #define CDNS_PORTCTRL_BANK_INVERT BIT(8) 186 187 #define CDNS_PORT_OFFSET 0x80 188 189 #define CDNS_PDI_CONFIG(n) (0x1100 + (n) * 16) 190 191 #define CDNS_PDI_CONFIG_SOFT_RESET BIT(24) 192 #define CDNS_PDI_CONFIG_CHANNEL GENMASK(15, 8) 193 #define CDNS_PDI_CONFIG_PORT GENMASK(4, 0) 194 195 /* Driver defaults */ 196 #define CDNS_TX_TIMEOUT 500 197 198 #define CDNS_SCP_RX_FIFOLEVEL 0x2 199 200 /* 201 * register accessor helpers 202 */ 203 static inline u32 cdns_readl(struct sdw_cdns *cdns, int offset) 204 { 205 return readl(cdns->registers + offset); 206 } 207 208 static inline void cdns_writel(struct sdw_cdns *cdns, int offset, u32 value) 209 { 210 writel(value, cdns->registers + offset); 211 } 212 213 static inline u32 cdns_ip_readl(struct sdw_cdns *cdns, int offset) 214 { 215 return cdns_readl(cdns, cdns->ip_offset + offset); 216 } 217 218 static inline void cdns_ip_writel(struct sdw_cdns *cdns, int offset, u32 value) 219 { 220 return cdns_writel(cdns, cdns->ip_offset + offset, value); 221 } 222 223 static inline void cdns_updatel(struct sdw_cdns *cdns, 224 int offset, u32 mask, u32 val) 225 { 226 u32 tmp; 227 228 tmp = cdns_readl(cdns, offset); 229 tmp = (tmp & ~mask) | val; 230 cdns_writel(cdns, offset, tmp); 231 } 232 233 static inline void cdns_ip_updatel(struct sdw_cdns *cdns, 234 int offset, u32 mask, u32 val) 235 { 236 cdns_updatel(cdns, cdns->ip_offset + offset, mask, val); 237 } 238 239 static int cdns_set_wait(struct sdw_cdns *cdns, int offset, u32 mask, u32 value) 240 { 241 int timeout = 10; 242 u32 reg_read; 243 244 /* Wait for bit to be set */ 245 do { 246 reg_read = readl(cdns->registers + offset); 247 if ((reg_read & mask) == value) 248 return 0; 249 250 timeout--; 251 usleep_range(50, 100); 252 } while (timeout != 0); 253 254 return -ETIMEDOUT; 255 } 256 257 static int cdns_clear_bit(struct sdw_cdns *cdns, int offset, u32 value) 258 { 259 writel(value, cdns->registers + offset); 260 261 /* Wait for bit to be self cleared */ 262 return cdns_set_wait(cdns, offset, value, 0); 263 } 264 265 /* 266 * all changes to the MCP_CONFIG, MCP_CONTROL, MCP_CMDCTRL and MCP_PHYCTRL 267 * need to be confirmed with a write to MCP_CONFIG_UPDATE 268 */ 269 static int cdns_config_update(struct sdw_cdns *cdns) 270 { 271 int ret; 272 273 if (sdw_cdns_is_clock_stop(cdns)) { 274 dev_err(cdns->dev, "Cannot program MCP_CONFIG_UPDATE in ClockStopMode\n"); 275 return -EINVAL; 276 } 277 278 ret = cdns_clear_bit(cdns, CDNS_MCP_CONFIG_UPDATE, 279 CDNS_MCP_CONFIG_UPDATE_BIT); 280 if (ret < 0) 281 dev_err(cdns->dev, "Config update timedout\n"); 282 283 return ret; 284 } 285 286 /** 287 * sdw_cdns_config_update() - Update configurations 288 * @cdns: Cadence instance 289 */ 290 void sdw_cdns_config_update(struct sdw_cdns *cdns) 291 { 292 /* commit changes */ 293 cdns_writel(cdns, CDNS_MCP_CONFIG_UPDATE, CDNS_MCP_CONFIG_UPDATE_BIT); 294 } 295 EXPORT_SYMBOL(sdw_cdns_config_update); 296 297 /** 298 * sdw_cdns_config_update_set_wait() - wait until configuration update bit is self-cleared 299 * @cdns: Cadence instance 300 */ 301 int sdw_cdns_config_update_set_wait(struct sdw_cdns *cdns) 302 { 303 /* the hardware recommendation is to wait at least 300us */ 304 return cdns_set_wait(cdns, CDNS_MCP_CONFIG_UPDATE, 305 CDNS_MCP_CONFIG_UPDATE_BIT, 0); 306 } 307 EXPORT_SYMBOL(sdw_cdns_config_update_set_wait); 308 309 /* 310 * debugfs 311 */ 312 #ifdef CONFIG_DEBUG_FS 313 314 #define RD_BUF (2 * PAGE_SIZE) 315 316 static ssize_t cdns_sprintf(struct sdw_cdns *cdns, 317 char *buf, size_t pos, unsigned int reg) 318 { 319 return scnprintf(buf + pos, RD_BUF - pos, 320 "%4x\t%8x\n", reg, cdns_readl(cdns, reg)); 321 } 322 323 static int cdns_reg_show(struct seq_file *s, void *data) 324 { 325 struct sdw_cdns *cdns = s->private; 326 char *buf; 327 ssize_t ret; 328 int num_ports; 329 int i, j; 330 331 buf = kzalloc(RD_BUF, GFP_KERNEL); 332 if (!buf) 333 return -ENOMEM; 334 335 ret = scnprintf(buf, RD_BUF, "Register Value\n"); 336 ret += scnprintf(buf + ret, RD_BUF - ret, "\nMCP Registers\n"); 337 /* 8 MCP registers */ 338 for (i = CDNS_MCP_CONFIG; i <= CDNS_MCP_PHYCTRL; i += sizeof(u32)) 339 ret += cdns_sprintf(cdns, buf, ret, i); 340 341 ret += scnprintf(buf + ret, RD_BUF - ret, 342 "\nStatus & Intr Registers\n"); 343 /* 13 Status & Intr registers (offsets 0x70 and 0x74 not defined) */ 344 for (i = CDNS_MCP_STAT; i <= CDNS_MCP_FIFOSTAT; i += sizeof(u32)) 345 ret += cdns_sprintf(cdns, buf, ret, i); 346 347 ret += scnprintf(buf + ret, RD_BUF - ret, 348 "\nSSP & Clk ctrl Registers\n"); 349 ret += cdns_sprintf(cdns, buf, ret, CDNS_MCP_SSP_CTRL0); 350 ret += cdns_sprintf(cdns, buf, ret, CDNS_MCP_SSP_CTRL1); 351 ret += cdns_sprintf(cdns, buf, ret, CDNS_MCP_CLK_CTRL0); 352 ret += cdns_sprintf(cdns, buf, ret, CDNS_MCP_CLK_CTRL1); 353 354 ret += scnprintf(buf + ret, RD_BUF - ret, 355 "\nDPn B0 Registers\n"); 356 357 num_ports = cdns->num_ports; 358 359 for (i = 0; i < num_ports; i++) { 360 ret += scnprintf(buf + ret, RD_BUF - ret, 361 "\nDP-%d\n", i); 362 for (j = CDNS_DPN_B0_CONFIG(i); 363 j < CDNS_DPN_B0_ASYNC_CTRL(i); j += sizeof(u32)) 364 ret += cdns_sprintf(cdns, buf, ret, j); 365 } 366 367 ret += scnprintf(buf + ret, RD_BUF - ret, 368 "\nDPn B1 Registers\n"); 369 for (i = 0; i < num_ports; i++) { 370 ret += scnprintf(buf + ret, RD_BUF - ret, 371 "\nDP-%d\n", i); 372 373 for (j = CDNS_DPN_B1_CONFIG(i); 374 j < CDNS_DPN_B1_ASYNC_CTRL(i); j += sizeof(u32)) 375 ret += cdns_sprintf(cdns, buf, ret, j); 376 } 377 378 ret += scnprintf(buf + ret, RD_BUF - ret, 379 "\nDPn Control Registers\n"); 380 for (i = 0; i < num_ports; i++) 381 ret += cdns_sprintf(cdns, buf, ret, 382 CDNS_PORTCTRL + i * CDNS_PORT_OFFSET); 383 384 ret += scnprintf(buf + ret, RD_BUF - ret, 385 "\nPDIn Config Registers\n"); 386 387 /* number of PDI and ports is interchangeable */ 388 for (i = 0; i < num_ports; i++) 389 ret += cdns_sprintf(cdns, buf, ret, CDNS_PDI_CONFIG(i)); 390 391 seq_printf(s, "%s", buf); 392 kfree(buf); 393 394 return 0; 395 } 396 DEFINE_SHOW_ATTRIBUTE(cdns_reg); 397 398 static int cdns_hw_reset(void *data, u64 value) 399 { 400 struct sdw_cdns *cdns = data; 401 int ret; 402 403 if (value != 1) 404 return -EINVAL; 405 406 /* Userspace changed the hardware state behind the kernel's back */ 407 add_taint(TAINT_USER, LOCKDEP_STILL_OK); 408 409 ret = sdw_cdns_exit_reset(cdns); 410 411 dev_dbg(cdns->dev, "link hw_reset done: %d\n", ret); 412 413 return ret; 414 } 415 416 DEFINE_DEBUGFS_ATTRIBUTE(cdns_hw_reset_fops, NULL, cdns_hw_reset, "%llu\n"); 417 418 static int cdns_parity_error_injection(void *data, u64 value) 419 { 420 struct sdw_cdns *cdns = data; 421 struct sdw_bus *bus; 422 int ret; 423 424 if (value != 1) 425 return -EINVAL; 426 427 bus = &cdns->bus; 428 429 /* 430 * Resume Master device. If this results in a bus reset, the 431 * Slave devices will re-attach and be re-enumerated. 432 */ 433 ret = pm_runtime_resume_and_get(bus->dev); 434 if (ret < 0 && ret != -EACCES) { 435 dev_err_ratelimited(cdns->dev, 436 "pm_runtime_resume_and_get failed in %s, ret %d\n", 437 __func__, ret); 438 return ret; 439 } 440 441 /* 442 * wait long enough for Slave(s) to be in steady state. This 443 * does not need to be super precise. 444 */ 445 msleep(200); 446 447 /* 448 * Take the bus lock here to make sure that any bus transactions 449 * will be queued while we inject a parity error on a dummy read 450 */ 451 mutex_lock(&bus->bus_lock); 452 453 /* program hardware to inject parity error */ 454 cdns_ip_updatel(cdns, CDNS_IP_MCP_CMDCTRL, 455 CDNS_IP_MCP_CMDCTRL_INSERT_PARITY_ERR, 456 CDNS_IP_MCP_CMDCTRL_INSERT_PARITY_ERR); 457 458 /* commit changes */ 459 ret = cdns_clear_bit(cdns, CDNS_MCP_CONFIG_UPDATE, CDNS_MCP_CONFIG_UPDATE_BIT); 460 if (ret < 0) 461 goto unlock; 462 463 /* do a broadcast dummy read to avoid bus clashes */ 464 ret = sdw_bread_no_pm_unlocked(&cdns->bus, 0xf, SDW_SCP_DEVID_0); 465 dev_info(cdns->dev, "parity error injection, read: %d\n", ret); 466 467 /* program hardware to disable parity error */ 468 cdns_ip_updatel(cdns, CDNS_IP_MCP_CMDCTRL, 469 CDNS_IP_MCP_CMDCTRL_INSERT_PARITY_ERR, 470 0); 471 472 /* commit changes */ 473 ret = cdns_clear_bit(cdns, CDNS_MCP_CONFIG_UPDATE, CDNS_MCP_CONFIG_UPDATE_BIT); 474 if (ret < 0) 475 goto unlock; 476 477 /* Userspace changed the hardware state behind the kernel's back */ 478 add_taint(TAINT_USER, LOCKDEP_STILL_OK); 479 480 unlock: 481 /* Continue bus operation with parity error injection disabled */ 482 mutex_unlock(&bus->bus_lock); 483 484 /* 485 * allow Master device to enter pm_runtime suspend. This may 486 * also result in Slave devices suspending. 487 */ 488 pm_runtime_mark_last_busy(bus->dev); 489 pm_runtime_put_autosuspend(bus->dev); 490 491 return 0; 492 } 493 494 DEFINE_DEBUGFS_ATTRIBUTE(cdns_parity_error_fops, NULL, 495 cdns_parity_error_injection, "%llu\n"); 496 497 static int cdns_set_pdi_loopback_source(void *data, u64 value) 498 { 499 struct sdw_cdns *cdns = data; 500 unsigned int pdi_out_num = cdns->pcm.num_bd + cdns->pcm.num_out; 501 502 if (value > pdi_out_num) 503 return -EINVAL; 504 505 /* Userspace changed the hardware state behind the kernel's back */ 506 add_taint(TAINT_USER, LOCKDEP_STILL_OK); 507 508 cdns->pdi_loopback_source = value; 509 510 return 0; 511 } 512 DEFINE_DEBUGFS_ATTRIBUTE(cdns_pdi_loopback_source_fops, NULL, cdns_set_pdi_loopback_source, "%llu\n"); 513 514 static int cdns_set_pdi_loopback_target(void *data, u64 value) 515 { 516 struct sdw_cdns *cdns = data; 517 unsigned int pdi_in_num = cdns->pcm.num_bd + cdns->pcm.num_in; 518 519 if (value > pdi_in_num) 520 return -EINVAL; 521 522 /* Userspace changed the hardware state behind the kernel's back */ 523 add_taint(TAINT_USER, LOCKDEP_STILL_OK); 524 525 cdns->pdi_loopback_target = value; 526 527 return 0; 528 } 529 DEFINE_DEBUGFS_ATTRIBUTE(cdns_pdi_loopback_target_fops, NULL, cdns_set_pdi_loopback_target, "%llu\n"); 530 531 /** 532 * sdw_cdns_debugfs_init() - Cadence debugfs init 533 * @cdns: Cadence instance 534 * @root: debugfs root 535 */ 536 void sdw_cdns_debugfs_init(struct sdw_cdns *cdns, struct dentry *root) 537 { 538 debugfs_create_file("cdns-registers", 0400, root, cdns, &cdns_reg_fops); 539 540 debugfs_create_file("cdns-hw-reset", 0200, root, cdns, 541 &cdns_hw_reset_fops); 542 543 debugfs_create_file("cdns-parity-error-injection", 0200, root, cdns, 544 &cdns_parity_error_fops); 545 546 cdns->pdi_loopback_source = -1; 547 cdns->pdi_loopback_target = -1; 548 549 debugfs_create_file("cdns-pdi-loopback-source", 0200, root, cdns, 550 &cdns_pdi_loopback_source_fops); 551 552 debugfs_create_file("cdns-pdi-loopback-target", 0200, root, cdns, 553 &cdns_pdi_loopback_target_fops); 554 555 } 556 EXPORT_SYMBOL_GPL(sdw_cdns_debugfs_init); 557 558 #endif /* CONFIG_DEBUG_FS */ 559 560 /* 561 * IO Calls 562 */ 563 static enum sdw_command_response 564 cdns_fill_msg_resp(struct sdw_cdns *cdns, 565 struct sdw_msg *msg, int count, int offset) 566 { 567 int nack = 0, no_ack = 0; 568 int i; 569 570 /* check message response */ 571 for (i = 0; i < count; i++) { 572 if (!(cdns->response_buf[i] & CDNS_MCP_RESP_ACK)) { 573 no_ack = 1; 574 dev_vdbg(cdns->dev, "Msg Ack not received, cmd %d\n", i); 575 } 576 if (cdns->response_buf[i] & CDNS_MCP_RESP_NACK) { 577 nack = 1; 578 dev_err_ratelimited(cdns->dev, "Msg NACK received, cmd %d\n", i); 579 } 580 } 581 582 if (nack) { 583 dev_err_ratelimited(cdns->dev, "Msg NACKed for Slave %d\n", msg->dev_num); 584 return SDW_CMD_FAIL; 585 } 586 587 if (no_ack) { 588 dev_dbg_ratelimited(cdns->dev, "Msg ignored for Slave %d\n", msg->dev_num); 589 return SDW_CMD_IGNORED; 590 } 591 592 if (msg->flags == SDW_MSG_FLAG_READ) { 593 /* fill response */ 594 for (i = 0; i < count; i++) 595 msg->buf[i + offset] = FIELD_GET(CDNS_MCP_RESP_RDATA, 596 cdns->response_buf[i]); 597 } 598 599 return SDW_CMD_OK; 600 } 601 602 static void cdns_read_response(struct sdw_cdns *cdns) 603 { 604 u32 num_resp, cmd_base; 605 int i; 606 607 /* RX_FIFO_AVAIL can be 2 entries more than the FIFO size */ 608 BUILD_BUG_ON(ARRAY_SIZE(cdns->response_buf) < CDNS_MCP_CMD_LEN + 2); 609 610 num_resp = cdns_readl(cdns, CDNS_MCP_FIFOSTAT); 611 num_resp &= CDNS_MCP_RX_FIFO_AVAIL; 612 if (num_resp > ARRAY_SIZE(cdns->response_buf)) { 613 dev_warn(cdns->dev, "RX AVAIL %d too long\n", num_resp); 614 num_resp = ARRAY_SIZE(cdns->response_buf); 615 } 616 617 cmd_base = CDNS_IP_MCP_CMD_BASE; 618 619 for (i = 0; i < num_resp; i++) { 620 cdns->response_buf[i] = cdns_ip_readl(cdns, cmd_base); 621 cmd_base += CDNS_MCP_CMD_WORD_LEN; 622 } 623 } 624 625 static enum sdw_command_response 626 _cdns_xfer_msg(struct sdw_cdns *cdns, struct sdw_msg *msg, int cmd, 627 int offset, int count, bool defer) 628 { 629 unsigned long time; 630 u32 base, i, data; 631 u16 addr; 632 633 /* Program the watermark level for RX FIFO */ 634 if (cdns->msg_count != count) { 635 cdns_writel(cdns, CDNS_MCP_FIFOLEVEL, count); 636 cdns->msg_count = count; 637 } 638 639 base = CDNS_IP_MCP_CMD_BASE; 640 addr = msg->addr + offset; 641 642 for (i = 0; i < count; i++) { 643 data = FIELD_PREP(CDNS_MCP_CMD_DEV_ADDR, msg->dev_num); 644 data |= FIELD_PREP(CDNS_MCP_CMD_COMMAND, cmd); 645 data |= FIELD_PREP(CDNS_MCP_CMD_REG_ADDR, addr); 646 addr++; 647 648 if (msg->flags == SDW_MSG_FLAG_WRITE) 649 data |= msg->buf[i + offset]; 650 651 data |= FIELD_PREP(CDNS_MCP_CMD_SSP_TAG, msg->ssp_sync); 652 cdns_ip_writel(cdns, base, data); 653 base += CDNS_MCP_CMD_WORD_LEN; 654 } 655 656 if (defer) 657 return SDW_CMD_OK; 658 659 /* wait for timeout or response */ 660 time = wait_for_completion_timeout(&cdns->tx_complete, 661 msecs_to_jiffies(CDNS_TX_TIMEOUT)); 662 if (!time) { 663 dev_err(cdns->dev, "IO transfer timed out, cmd %d device %d addr %x len %d\n", 664 cmd, msg->dev_num, msg->addr, msg->len); 665 msg->len = 0; 666 667 /* Drain anything in the RX_FIFO */ 668 cdns_read_response(cdns); 669 670 return SDW_CMD_TIMEOUT; 671 } 672 673 return cdns_fill_msg_resp(cdns, msg, count, offset); 674 } 675 676 static enum sdw_command_response 677 cdns_program_scp_addr(struct sdw_cdns *cdns, struct sdw_msg *msg) 678 { 679 int nack = 0, no_ack = 0; 680 unsigned long time; 681 u32 data[2], base; 682 int i; 683 684 /* Program the watermark level for RX FIFO */ 685 if (cdns->msg_count != CDNS_SCP_RX_FIFOLEVEL) { 686 cdns_writel(cdns, CDNS_MCP_FIFOLEVEL, CDNS_SCP_RX_FIFOLEVEL); 687 cdns->msg_count = CDNS_SCP_RX_FIFOLEVEL; 688 } 689 690 data[0] = FIELD_PREP(CDNS_MCP_CMD_DEV_ADDR, msg->dev_num); 691 data[0] |= FIELD_PREP(CDNS_MCP_CMD_COMMAND, 0x3); 692 data[1] = data[0]; 693 694 data[0] |= FIELD_PREP(CDNS_MCP_CMD_REG_ADDR, SDW_SCP_ADDRPAGE1); 695 data[1] |= FIELD_PREP(CDNS_MCP_CMD_REG_ADDR, SDW_SCP_ADDRPAGE2); 696 697 data[0] |= msg->addr_page1; 698 data[1] |= msg->addr_page2; 699 700 base = CDNS_IP_MCP_CMD_BASE; 701 cdns_ip_writel(cdns, base, data[0]); 702 base += CDNS_MCP_CMD_WORD_LEN; 703 cdns_ip_writel(cdns, base, data[1]); 704 705 time = wait_for_completion_timeout(&cdns->tx_complete, 706 msecs_to_jiffies(CDNS_TX_TIMEOUT)); 707 if (!time) { 708 dev_err(cdns->dev, "SCP Msg trf timed out\n"); 709 msg->len = 0; 710 return SDW_CMD_TIMEOUT; 711 } 712 713 /* check response the writes */ 714 for (i = 0; i < 2; i++) { 715 if (!(cdns->response_buf[i] & CDNS_MCP_RESP_ACK)) { 716 no_ack = 1; 717 dev_err(cdns->dev, "Program SCP Ack not received\n"); 718 if (cdns->response_buf[i] & CDNS_MCP_RESP_NACK) { 719 nack = 1; 720 dev_err(cdns->dev, "Program SCP NACK received\n"); 721 } 722 } 723 } 724 725 /* For NACK, NO ack, don't return err if we are in Broadcast mode */ 726 if (nack) { 727 dev_err_ratelimited(cdns->dev, 728 "SCP_addrpage NACKed for Slave %d\n", msg->dev_num); 729 return SDW_CMD_FAIL; 730 } 731 732 if (no_ack) { 733 dev_dbg_ratelimited(cdns->dev, 734 "SCP_addrpage ignored for Slave %d\n", msg->dev_num); 735 return SDW_CMD_IGNORED; 736 } 737 738 return SDW_CMD_OK; 739 } 740 741 static int cdns_prep_msg(struct sdw_cdns *cdns, struct sdw_msg *msg, int *cmd) 742 { 743 int ret; 744 745 if (msg->page) { 746 ret = cdns_program_scp_addr(cdns, msg); 747 if (ret) { 748 msg->len = 0; 749 return ret; 750 } 751 } 752 753 switch (msg->flags) { 754 case SDW_MSG_FLAG_READ: 755 *cmd = CDNS_MCP_CMD_READ; 756 break; 757 758 case SDW_MSG_FLAG_WRITE: 759 *cmd = CDNS_MCP_CMD_WRITE; 760 break; 761 762 default: 763 dev_err(cdns->dev, "Invalid msg cmd: %d\n", msg->flags); 764 return -EINVAL; 765 } 766 767 return 0; 768 } 769 770 enum sdw_command_response 771 cdns_xfer_msg(struct sdw_bus *bus, struct sdw_msg *msg) 772 { 773 struct sdw_cdns *cdns = bus_to_cdns(bus); 774 int cmd = 0, ret, i; 775 776 ret = cdns_prep_msg(cdns, msg, &cmd); 777 if (ret) 778 return SDW_CMD_FAIL_OTHER; 779 780 for (i = 0; i < msg->len / CDNS_MCP_CMD_LEN; i++) { 781 ret = _cdns_xfer_msg(cdns, msg, cmd, i * CDNS_MCP_CMD_LEN, 782 CDNS_MCP_CMD_LEN, false); 783 if (ret != SDW_CMD_OK) 784 return ret; 785 } 786 787 if (!(msg->len % CDNS_MCP_CMD_LEN)) 788 return SDW_CMD_OK; 789 790 return _cdns_xfer_msg(cdns, msg, cmd, i * CDNS_MCP_CMD_LEN, 791 msg->len % CDNS_MCP_CMD_LEN, false); 792 } 793 EXPORT_SYMBOL(cdns_xfer_msg); 794 795 enum sdw_command_response 796 cdns_xfer_msg_defer(struct sdw_bus *bus) 797 { 798 struct sdw_cdns *cdns = bus_to_cdns(bus); 799 struct sdw_defer *defer = &bus->defer_msg; 800 struct sdw_msg *msg = defer->msg; 801 int cmd = 0, ret; 802 803 /* for defer only 1 message is supported */ 804 if (msg->len > 1) 805 return -ENOTSUPP; 806 807 ret = cdns_prep_msg(cdns, msg, &cmd); 808 if (ret) 809 return SDW_CMD_FAIL_OTHER; 810 811 return _cdns_xfer_msg(cdns, msg, cmd, 0, msg->len, true); 812 } 813 EXPORT_SYMBOL(cdns_xfer_msg_defer); 814 815 u32 cdns_read_ping_status(struct sdw_bus *bus) 816 { 817 struct sdw_cdns *cdns = bus_to_cdns(bus); 818 819 return cdns_readl(cdns, CDNS_MCP_SLAVE_STAT); 820 } 821 EXPORT_SYMBOL(cdns_read_ping_status); 822 823 /* 824 * IRQ handling 825 */ 826 827 static int cdns_update_slave_status(struct sdw_cdns *cdns, 828 u64 slave_intstat) 829 { 830 enum sdw_slave_status status[SDW_MAX_DEVICES + 1]; 831 bool is_slave = false; 832 u32 mask; 833 u32 val; 834 int i, set_status; 835 836 memset(status, 0, sizeof(status)); 837 838 for (i = 0; i <= SDW_MAX_DEVICES; i++) { 839 mask = (slave_intstat >> (i * CDNS_MCP_SLAVE_STATUS_NUM)) & 840 CDNS_MCP_SLAVE_STATUS_BITS; 841 842 set_status = 0; 843 844 if (mask) { 845 is_slave = true; 846 847 if (mask & CDNS_MCP_SLAVE_INTSTAT_RESERVED) { 848 status[i] = SDW_SLAVE_RESERVED; 849 set_status++; 850 } 851 852 if (mask & CDNS_MCP_SLAVE_INTSTAT_ATTACHED) { 853 status[i] = SDW_SLAVE_ATTACHED; 854 set_status++; 855 } 856 857 if (mask & CDNS_MCP_SLAVE_INTSTAT_ALERT) { 858 status[i] = SDW_SLAVE_ALERT; 859 set_status++; 860 } 861 862 if (mask & CDNS_MCP_SLAVE_INTSTAT_NPRESENT) { 863 status[i] = SDW_SLAVE_UNATTACHED; 864 set_status++; 865 } 866 } 867 868 /* 869 * check that there was a single reported Slave status and when 870 * there is not use the latest status extracted from PING commands 871 */ 872 if (set_status != 1) { 873 val = cdns_readl(cdns, CDNS_MCP_SLAVE_STAT); 874 val >>= (i * 2); 875 876 switch (val & 0x3) { 877 case 0: 878 status[i] = SDW_SLAVE_UNATTACHED; 879 break; 880 case 1: 881 status[i] = SDW_SLAVE_ATTACHED; 882 break; 883 case 2: 884 status[i] = SDW_SLAVE_ALERT; 885 break; 886 case 3: 887 default: 888 status[i] = SDW_SLAVE_RESERVED; 889 break; 890 } 891 } 892 } 893 894 if (is_slave) 895 return sdw_handle_slave_status(&cdns->bus, status); 896 897 return 0; 898 } 899 900 /** 901 * sdw_cdns_irq() - Cadence interrupt handler 902 * @irq: irq number 903 * @dev_id: irq context 904 */ 905 irqreturn_t sdw_cdns_irq(int irq, void *dev_id) 906 { 907 struct sdw_cdns *cdns = dev_id; 908 u32 int_status; 909 910 /* Check if the link is up */ 911 if (!cdns->link_up) 912 return IRQ_NONE; 913 914 int_status = cdns_readl(cdns, CDNS_MCP_INTSTAT); 915 916 /* check for reserved values read as zero */ 917 if (int_status & CDNS_MCP_INT_RESERVED) 918 return IRQ_NONE; 919 920 if (!(int_status & CDNS_MCP_INT_IRQ)) 921 return IRQ_NONE; 922 923 if (int_status & CDNS_MCP_INT_RX_WL) { 924 struct sdw_bus *bus = &cdns->bus; 925 struct sdw_defer *defer = &bus->defer_msg; 926 927 cdns_read_response(cdns); 928 929 if (defer && defer->msg) { 930 cdns_fill_msg_resp(cdns, defer->msg, 931 defer->length, 0); 932 complete(&defer->complete); 933 } else { 934 complete(&cdns->tx_complete); 935 } 936 } 937 938 if (int_status & CDNS_MCP_INT_PARITY) { 939 /* Parity error detected by Master */ 940 dev_err_ratelimited(cdns->dev, "Parity error\n"); 941 } 942 943 if (int_status & CDNS_MCP_INT_CTRL_CLASH) { 944 /* Slave is driving bit slot during control word */ 945 dev_err_ratelimited(cdns->dev, "Bus clash for control word\n"); 946 } 947 948 if (int_status & CDNS_MCP_INT_DATA_CLASH) { 949 /* 950 * Multiple slaves trying to drive bit slot, or issue with 951 * ownership of data bits or Slave gone bonkers 952 */ 953 dev_err_ratelimited(cdns->dev, "Bus clash for data word\n"); 954 } 955 956 if (cdns->bus.params.m_data_mode != SDW_PORT_DATA_MODE_NORMAL && 957 int_status & CDNS_MCP_INT_DPINT) { 958 u32 port_intstat; 959 960 /* just log which ports report an error */ 961 port_intstat = cdns_readl(cdns, CDNS_MCP_PORT_INTSTAT); 962 dev_err_ratelimited(cdns->dev, "DP interrupt: PortIntStat %8x\n", 963 port_intstat); 964 965 /* clear status w/ write1 */ 966 cdns_writel(cdns, CDNS_MCP_PORT_INTSTAT, port_intstat); 967 } 968 969 if (int_status & CDNS_MCP_INT_SLAVE_MASK) { 970 /* Mask the Slave interrupt and wake thread */ 971 cdns_updatel(cdns, CDNS_MCP_INTMASK, 972 CDNS_MCP_INT_SLAVE_MASK, 0); 973 974 int_status &= ~CDNS_MCP_INT_SLAVE_MASK; 975 976 /* 977 * Deal with possible race condition between interrupt 978 * handling and disabling interrupts on suspend. 979 * 980 * If the master is in the process of disabling 981 * interrupts, don't schedule a workqueue 982 */ 983 if (cdns->interrupt_enabled) 984 schedule_work(&cdns->work); 985 } 986 987 cdns_writel(cdns, CDNS_MCP_INTSTAT, int_status); 988 return IRQ_HANDLED; 989 } 990 EXPORT_SYMBOL(sdw_cdns_irq); 991 992 /** 993 * cdns_update_slave_status_work - update slave status in a work since we will need to handle 994 * other interrupts eg. CDNS_MCP_INT_RX_WL during the update slave 995 * process. 996 * @work: cdns worker thread 997 */ 998 static void cdns_update_slave_status_work(struct work_struct *work) 999 { 1000 struct sdw_cdns *cdns = 1001 container_of(work, struct sdw_cdns, work); 1002 u32 slave0, slave1; 1003 u64 slave_intstat; 1004 u32 device0_status; 1005 int retry_count = 0; 1006 1007 /* 1008 * Clear main interrupt first so we don't lose any assertions 1009 * that happen during this function. 1010 */ 1011 cdns_writel(cdns, CDNS_MCP_INTSTAT, CDNS_MCP_INT_SLAVE_MASK); 1012 1013 slave0 = cdns_readl(cdns, CDNS_MCP_SLAVE_INTSTAT0); 1014 slave1 = cdns_readl(cdns, CDNS_MCP_SLAVE_INTSTAT1); 1015 1016 /* 1017 * Clear the bits before handling so we don't lose any 1018 * bits that re-assert. 1019 */ 1020 cdns_writel(cdns, CDNS_MCP_SLAVE_INTSTAT0, slave0); 1021 cdns_writel(cdns, CDNS_MCP_SLAVE_INTSTAT1, slave1); 1022 1023 /* combine the two status */ 1024 slave_intstat = ((u64)slave1 << 32) | slave0; 1025 1026 dev_dbg_ratelimited(cdns->dev, "Slave status change: 0x%llx\n", slave_intstat); 1027 1028 update_status: 1029 cdns_update_slave_status(cdns, slave_intstat); 1030 1031 /* 1032 * When there is more than one peripheral per link, it's 1033 * possible that a deviceB becomes attached after we deal with 1034 * the attachment of deviceA. Since the hardware does a 1035 * logical AND, the attachment of the second device does not 1036 * change the status seen by the driver. 1037 * 1038 * In that case, clearing the registers above would result in 1039 * the deviceB never being detected - until a change of status 1040 * is observed on the bus. 1041 * 1042 * To avoid this race condition, re-check if any device0 needs 1043 * attention with PING commands. There is no need to check for 1044 * ALERTS since they are not allowed until a non-zero 1045 * device_number is assigned. 1046 * 1047 * Do not clear the INTSTAT0/1. While looping to enumerate devices on 1048 * #0 there could be status changes on other devices - these must 1049 * be kept in the INTSTAT so they can be handled when all #0 devices 1050 * have been handled. 1051 */ 1052 1053 device0_status = cdns_readl(cdns, CDNS_MCP_SLAVE_STAT); 1054 device0_status &= 3; 1055 1056 if (device0_status == SDW_SLAVE_ATTACHED) { 1057 if (retry_count++ < SDW_MAX_DEVICES) { 1058 dev_dbg_ratelimited(cdns->dev, 1059 "Device0 detected after clearing status, iteration %d\n", 1060 retry_count); 1061 slave_intstat = CDNS_MCP_SLAVE_INTSTAT_ATTACHED; 1062 goto update_status; 1063 } else { 1064 dev_err_ratelimited(cdns->dev, 1065 "Device0 detected after %d iterations\n", 1066 retry_count); 1067 } 1068 } 1069 1070 /* unmask Slave interrupt now */ 1071 cdns_updatel(cdns, CDNS_MCP_INTMASK, 1072 CDNS_MCP_INT_SLAVE_MASK, CDNS_MCP_INT_SLAVE_MASK); 1073 1074 } 1075 1076 /* paranoia check to make sure self-cleared bits are indeed cleared */ 1077 void sdw_cdns_check_self_clearing_bits(struct sdw_cdns *cdns, const char *string, 1078 bool initial_delay, int reset_iterations) 1079 { 1080 u32 ip_mcp_control; 1081 u32 mcp_control; 1082 u32 mcp_config_update; 1083 int i; 1084 1085 if (initial_delay) 1086 usleep_range(1000, 1500); 1087 1088 ip_mcp_control = cdns_ip_readl(cdns, CDNS_IP_MCP_CONTROL); 1089 1090 /* the following bits should be cleared immediately */ 1091 if (ip_mcp_control & CDNS_IP_MCP_CONTROL_SW_RST) 1092 dev_err(cdns->dev, "%s failed: IP_MCP_CONTROL_SW_RST is not cleared\n", string); 1093 1094 mcp_control = cdns_readl(cdns, CDNS_MCP_CONTROL); 1095 1096 /* the following bits should be cleared immediately */ 1097 if (mcp_control & CDNS_MCP_CONTROL_CMD_RST) 1098 dev_err(cdns->dev, "%s failed: MCP_CONTROL_CMD_RST is not cleared\n", string); 1099 if (mcp_control & CDNS_MCP_CONTROL_SOFT_RST) 1100 dev_err(cdns->dev, "%s failed: MCP_CONTROL_SOFT_RST is not cleared\n", string); 1101 if (mcp_control & CDNS_MCP_CONTROL_CLK_STOP_CLR) 1102 dev_err(cdns->dev, "%s failed: MCP_CONTROL_CLK_STOP_CLR is not cleared\n", string); 1103 1104 mcp_config_update = cdns_readl(cdns, CDNS_MCP_CONFIG_UPDATE); 1105 if (mcp_config_update & CDNS_MCP_CONFIG_UPDATE_BIT) 1106 dev_err(cdns->dev, "%s failed: MCP_CONFIG_UPDATE_BIT is not cleared\n", string); 1107 1108 i = 0; 1109 while (mcp_control & CDNS_MCP_CONTROL_HW_RST) { 1110 if (i == reset_iterations) { 1111 dev_err(cdns->dev, "%s failed: MCP_CONTROL_HW_RST is not cleared\n", string); 1112 break; 1113 } 1114 1115 dev_dbg(cdns->dev, "%s: MCP_CONTROL_HW_RST is not cleared at iteration %d\n", string, i); 1116 i++; 1117 1118 usleep_range(1000, 1500); 1119 mcp_control = cdns_readl(cdns, CDNS_MCP_CONTROL); 1120 } 1121 1122 } 1123 EXPORT_SYMBOL(sdw_cdns_check_self_clearing_bits); 1124 1125 /* 1126 * init routines 1127 */ 1128 1129 /** 1130 * sdw_cdns_exit_reset() - Program reset parameters and start bus operations 1131 * @cdns: Cadence instance 1132 */ 1133 int sdw_cdns_exit_reset(struct sdw_cdns *cdns) 1134 { 1135 /* keep reset delay unchanged to 4096 cycles */ 1136 1137 /* use hardware generated reset */ 1138 cdns_updatel(cdns, CDNS_MCP_CONTROL, 1139 CDNS_MCP_CONTROL_HW_RST, 1140 CDNS_MCP_CONTROL_HW_RST); 1141 1142 /* commit changes */ 1143 return cdns_config_update(cdns); 1144 } 1145 EXPORT_SYMBOL(sdw_cdns_exit_reset); 1146 1147 /** 1148 * cdns_enable_slave_interrupts() - Enable SDW slave interrupts 1149 * @cdns: Cadence instance 1150 * @state: boolean for true/false 1151 */ 1152 static void cdns_enable_slave_interrupts(struct sdw_cdns *cdns, bool state) 1153 { 1154 u32 mask; 1155 1156 mask = cdns_readl(cdns, CDNS_MCP_INTMASK); 1157 if (state) 1158 mask |= CDNS_MCP_INT_SLAVE_MASK; 1159 else 1160 mask &= ~CDNS_MCP_INT_SLAVE_MASK; 1161 1162 cdns_writel(cdns, CDNS_MCP_INTMASK, mask); 1163 } 1164 1165 /** 1166 * sdw_cdns_enable_interrupt() - Enable SDW interrupts 1167 * @cdns: Cadence instance 1168 * @state: True if we are trying to enable interrupt. 1169 */ 1170 int sdw_cdns_enable_interrupt(struct sdw_cdns *cdns, bool state) 1171 { 1172 u32 slave_intmask0 = 0; 1173 u32 slave_intmask1 = 0; 1174 u32 mask = 0; 1175 1176 if (!state) 1177 goto update_masks; 1178 1179 slave_intmask0 = CDNS_MCP_SLAVE_INTMASK0_MASK; 1180 slave_intmask1 = CDNS_MCP_SLAVE_INTMASK1_MASK; 1181 1182 /* enable detection of all slave state changes */ 1183 mask = CDNS_MCP_INT_SLAVE_MASK; 1184 1185 /* enable detection of bus issues */ 1186 mask |= CDNS_MCP_INT_CTRL_CLASH | CDNS_MCP_INT_DATA_CLASH | 1187 CDNS_MCP_INT_PARITY; 1188 1189 /* port interrupt limited to test modes for now */ 1190 if (cdns->bus.params.m_data_mode != SDW_PORT_DATA_MODE_NORMAL) 1191 mask |= CDNS_MCP_INT_DPINT; 1192 1193 /* enable detection of RX fifo level */ 1194 mask |= CDNS_MCP_INT_RX_WL; 1195 1196 /* 1197 * CDNS_MCP_INT_IRQ needs to be set otherwise all previous 1198 * settings are irrelevant 1199 */ 1200 mask |= CDNS_MCP_INT_IRQ; 1201 1202 if (interrupt_mask) /* parameter override */ 1203 mask = interrupt_mask; 1204 1205 update_masks: 1206 /* clear slave interrupt status before enabling interrupt */ 1207 if (state) { 1208 u32 slave_state; 1209 1210 slave_state = cdns_readl(cdns, CDNS_MCP_SLAVE_INTSTAT0); 1211 cdns_writel(cdns, CDNS_MCP_SLAVE_INTSTAT0, slave_state); 1212 slave_state = cdns_readl(cdns, CDNS_MCP_SLAVE_INTSTAT1); 1213 cdns_writel(cdns, CDNS_MCP_SLAVE_INTSTAT1, slave_state); 1214 } 1215 cdns->interrupt_enabled = state; 1216 1217 /* 1218 * Complete any on-going status updates before updating masks, 1219 * and cancel queued status updates. 1220 * 1221 * There could be a race with a new interrupt thrown before 1222 * the 3 mask updates below are complete, so in the interrupt 1223 * we use the 'interrupt_enabled' status to prevent new work 1224 * from being queued. 1225 */ 1226 if (!state) 1227 cancel_work_sync(&cdns->work); 1228 1229 cdns_writel(cdns, CDNS_MCP_SLAVE_INTMASK0, slave_intmask0); 1230 cdns_writel(cdns, CDNS_MCP_SLAVE_INTMASK1, slave_intmask1); 1231 cdns_writel(cdns, CDNS_MCP_INTMASK, mask); 1232 1233 return 0; 1234 } 1235 EXPORT_SYMBOL(sdw_cdns_enable_interrupt); 1236 1237 static int cdns_allocate_pdi(struct sdw_cdns *cdns, 1238 struct sdw_cdns_pdi **stream, 1239 u32 num) 1240 { 1241 struct sdw_cdns_pdi *pdi; 1242 int i; 1243 1244 if (!num) 1245 return 0; 1246 1247 pdi = devm_kcalloc(cdns->dev, num, sizeof(*pdi), GFP_KERNEL); 1248 if (!pdi) 1249 return -ENOMEM; 1250 1251 for (i = 0; i < num; i++) { 1252 pdi[i].num = i; 1253 } 1254 1255 *stream = pdi; 1256 return 0; 1257 } 1258 1259 /** 1260 * sdw_cdns_pdi_init() - PDI initialization routine 1261 * 1262 * @cdns: Cadence instance 1263 * @config: Stream configurations 1264 */ 1265 int sdw_cdns_pdi_init(struct sdw_cdns *cdns, 1266 struct sdw_cdns_stream_config config) 1267 { 1268 struct sdw_cdns_streams *stream; 1269 int ret; 1270 1271 cdns->pcm.num_bd = config.pcm_bd; 1272 cdns->pcm.num_in = config.pcm_in; 1273 cdns->pcm.num_out = config.pcm_out; 1274 1275 /* Allocate PDIs for PCMs */ 1276 stream = &cdns->pcm; 1277 1278 /* we allocate PDI0 and PDI1 which are used for Bulk */ 1279 ret = cdns_allocate_pdi(cdns, &stream->bd, stream->num_bd); 1280 if (ret) 1281 return ret; 1282 1283 ret = cdns_allocate_pdi(cdns, &stream->in, stream->num_in); 1284 if (ret) 1285 return ret; 1286 1287 ret = cdns_allocate_pdi(cdns, &stream->out, stream->num_out); 1288 if (ret) 1289 return ret; 1290 1291 /* Update total number of PCM PDIs */ 1292 stream->num_pdi = stream->num_bd + stream->num_in + stream->num_out; 1293 cdns->num_ports = stream->num_pdi; 1294 1295 return 0; 1296 } 1297 EXPORT_SYMBOL(sdw_cdns_pdi_init); 1298 1299 static u32 cdns_set_initial_frame_shape(int n_rows, int n_cols) 1300 { 1301 u32 val; 1302 int c; 1303 int r; 1304 1305 r = sdw_find_row_index(n_rows); 1306 c = sdw_find_col_index(n_cols); 1307 1308 val = FIELD_PREP(CDNS_MCP_FRAME_SHAPE_ROW_MASK, r); 1309 val |= FIELD_PREP(CDNS_MCP_FRAME_SHAPE_COL_MASK, c); 1310 1311 return val; 1312 } 1313 1314 static void cdns_init_clock_ctrl(struct sdw_cdns *cdns) 1315 { 1316 struct sdw_bus *bus = &cdns->bus; 1317 struct sdw_master_prop *prop = &bus->prop; 1318 u32 val; 1319 u32 ssp_interval; 1320 int divider; 1321 1322 dev_dbg(cdns->dev, "mclk %d max %d row %d col %d\n", 1323 prop->mclk_freq, 1324 prop->max_clk_freq, 1325 prop->default_row, 1326 prop->default_col); 1327 1328 /* Set clock divider */ 1329 divider = (prop->mclk_freq / prop->max_clk_freq) - 1; 1330 1331 cdns_updatel(cdns, CDNS_MCP_CLK_CTRL0, 1332 CDNS_MCP_CLK_MCLKD_MASK, divider); 1333 cdns_updatel(cdns, CDNS_MCP_CLK_CTRL1, 1334 CDNS_MCP_CLK_MCLKD_MASK, divider); 1335 1336 /* 1337 * Frame shape changes after initialization have to be done 1338 * with the bank switch mechanism 1339 */ 1340 val = cdns_set_initial_frame_shape(prop->default_row, 1341 prop->default_col); 1342 cdns_writel(cdns, CDNS_MCP_FRAME_SHAPE_INIT, val); 1343 1344 /* Set SSP interval to default value */ 1345 ssp_interval = prop->default_frame_rate / SDW_CADENCE_GSYNC_HZ; 1346 cdns_writel(cdns, CDNS_MCP_SSP_CTRL0, ssp_interval); 1347 cdns_writel(cdns, CDNS_MCP_SSP_CTRL1, ssp_interval); 1348 } 1349 1350 /** 1351 * sdw_cdns_init() - Cadence initialization 1352 * @cdns: Cadence instance 1353 */ 1354 int sdw_cdns_init(struct sdw_cdns *cdns) 1355 { 1356 u32 val; 1357 1358 cdns_init_clock_ctrl(cdns); 1359 1360 sdw_cdns_check_self_clearing_bits(cdns, __func__, false, 0); 1361 1362 /* reset msg_count to default value of FIFOLEVEL */ 1363 cdns->msg_count = cdns_readl(cdns, CDNS_MCP_FIFOLEVEL); 1364 1365 /* flush command FIFOs */ 1366 cdns_updatel(cdns, CDNS_MCP_CONTROL, CDNS_MCP_CONTROL_CMD_RST, 1367 CDNS_MCP_CONTROL_CMD_RST); 1368 1369 /* Set cmd accept mode */ 1370 cdns_ip_updatel(cdns, CDNS_IP_MCP_CONTROL, CDNS_IP_MCP_CONTROL_CMD_ACCEPT, 1371 CDNS_IP_MCP_CONTROL_CMD_ACCEPT); 1372 1373 /* Configure mcp config */ 1374 val = cdns_readl(cdns, CDNS_MCP_CONFIG); 1375 1376 /* Disable auto bus release */ 1377 val &= ~CDNS_MCP_CONFIG_BUS_REL; 1378 1379 cdns_writel(cdns, CDNS_MCP_CONFIG, val); 1380 1381 /* Configure IP mcp config */ 1382 val = cdns_ip_readl(cdns, CDNS_IP_MCP_CONFIG); 1383 1384 /* enable bus operations with clock and data */ 1385 val &= ~CDNS_IP_MCP_CONFIG_OP; 1386 val |= CDNS_IP_MCP_CONFIG_OP_NORMAL; 1387 1388 /* Set cmd mode for Tx and Rx cmds */ 1389 val &= ~CDNS_IP_MCP_CONFIG_CMD; 1390 1391 /* Disable sniffer mode */ 1392 val &= ~CDNS_IP_MCP_CONFIG_SNIFFER; 1393 1394 if (cdns->bus.multi_link) 1395 /* Set Multi-master mode to take gsync into account */ 1396 val |= CDNS_IP_MCP_CONFIG_MMASTER; 1397 1398 /* leave frame delay to hardware default of 0x1F */ 1399 1400 /* leave command retry to hardware default of 0 */ 1401 1402 cdns_ip_writel(cdns, CDNS_IP_MCP_CONFIG, val); 1403 1404 /* changes will be committed later */ 1405 return 0; 1406 } 1407 EXPORT_SYMBOL(sdw_cdns_init); 1408 1409 int cdns_bus_conf(struct sdw_bus *bus, struct sdw_bus_params *params) 1410 { 1411 struct sdw_master_prop *prop = &bus->prop; 1412 struct sdw_cdns *cdns = bus_to_cdns(bus); 1413 int mcp_clkctrl_off; 1414 int divider; 1415 1416 if (!params->curr_dr_freq) { 1417 dev_err(cdns->dev, "NULL curr_dr_freq\n"); 1418 return -EINVAL; 1419 } 1420 1421 divider = prop->mclk_freq * SDW_DOUBLE_RATE_FACTOR / 1422 params->curr_dr_freq; 1423 divider--; /* divider is 1/(N+1) */ 1424 1425 if (params->next_bank) 1426 mcp_clkctrl_off = CDNS_MCP_CLK_CTRL1; 1427 else 1428 mcp_clkctrl_off = CDNS_MCP_CLK_CTRL0; 1429 1430 cdns_updatel(cdns, mcp_clkctrl_off, CDNS_MCP_CLK_MCLKD_MASK, divider); 1431 1432 return 0; 1433 } 1434 EXPORT_SYMBOL(cdns_bus_conf); 1435 1436 static int cdns_port_params(struct sdw_bus *bus, 1437 struct sdw_port_params *p_params, unsigned int bank) 1438 { 1439 struct sdw_cdns *cdns = bus_to_cdns(bus); 1440 int dpn_config_off_source; 1441 int dpn_config_off_target; 1442 int target_num = p_params->num; 1443 int source_num = p_params->num; 1444 bool override = false; 1445 int dpn_config; 1446 1447 if (target_num == cdns->pdi_loopback_target && 1448 cdns->pdi_loopback_source != -1) { 1449 source_num = cdns->pdi_loopback_source; 1450 override = true; 1451 } 1452 1453 if (bank) { 1454 dpn_config_off_source = CDNS_DPN_B1_CONFIG(source_num); 1455 dpn_config_off_target = CDNS_DPN_B1_CONFIG(target_num); 1456 } else { 1457 dpn_config_off_source = CDNS_DPN_B0_CONFIG(source_num); 1458 dpn_config_off_target = CDNS_DPN_B0_CONFIG(target_num); 1459 } 1460 1461 dpn_config = cdns_readl(cdns, dpn_config_off_source); 1462 1463 /* use port params if there is no loopback, otherwise use source as is */ 1464 if (!override) { 1465 u32p_replace_bits(&dpn_config, p_params->bps - 1, CDNS_DPN_CONFIG_WL); 1466 u32p_replace_bits(&dpn_config, p_params->flow_mode, CDNS_DPN_CONFIG_PORT_FLOW); 1467 u32p_replace_bits(&dpn_config, p_params->data_mode, CDNS_DPN_CONFIG_PORT_DAT); 1468 } 1469 1470 cdns_writel(cdns, dpn_config_off_target, dpn_config); 1471 1472 return 0; 1473 } 1474 1475 static int cdns_transport_params(struct sdw_bus *bus, 1476 struct sdw_transport_params *t_params, 1477 enum sdw_reg_bank bank) 1478 { 1479 struct sdw_cdns *cdns = bus_to_cdns(bus); 1480 int dpn_config; 1481 int dpn_config_off_source; 1482 int dpn_config_off_target; 1483 int dpn_hctrl; 1484 int dpn_hctrl_off_source; 1485 int dpn_hctrl_off_target; 1486 int dpn_offsetctrl; 1487 int dpn_offsetctrl_off_source; 1488 int dpn_offsetctrl_off_target; 1489 int dpn_samplectrl; 1490 int dpn_samplectrl_off_source; 1491 int dpn_samplectrl_off_target; 1492 int source_num = t_params->port_num; 1493 int target_num = t_params->port_num; 1494 bool override = false; 1495 1496 if (target_num == cdns->pdi_loopback_target && 1497 cdns->pdi_loopback_source != -1) { 1498 source_num = cdns->pdi_loopback_source; 1499 override = true; 1500 } 1501 1502 /* 1503 * Note: Only full data port is supported on the Master side for 1504 * both PCM and PDM ports. 1505 */ 1506 1507 if (bank) { 1508 dpn_config_off_source = CDNS_DPN_B1_CONFIG(source_num); 1509 dpn_hctrl_off_source = CDNS_DPN_B1_HCTRL(source_num); 1510 dpn_offsetctrl_off_source = CDNS_DPN_B1_OFFSET_CTRL(source_num); 1511 dpn_samplectrl_off_source = CDNS_DPN_B1_SAMPLE_CTRL(source_num); 1512 1513 dpn_config_off_target = CDNS_DPN_B1_CONFIG(target_num); 1514 dpn_hctrl_off_target = CDNS_DPN_B1_HCTRL(target_num); 1515 dpn_offsetctrl_off_target = CDNS_DPN_B1_OFFSET_CTRL(target_num); 1516 dpn_samplectrl_off_target = CDNS_DPN_B1_SAMPLE_CTRL(target_num); 1517 1518 } else { 1519 dpn_config_off_source = CDNS_DPN_B0_CONFIG(source_num); 1520 dpn_hctrl_off_source = CDNS_DPN_B0_HCTRL(source_num); 1521 dpn_offsetctrl_off_source = CDNS_DPN_B0_OFFSET_CTRL(source_num); 1522 dpn_samplectrl_off_source = CDNS_DPN_B0_SAMPLE_CTRL(source_num); 1523 1524 dpn_config_off_target = CDNS_DPN_B0_CONFIG(target_num); 1525 dpn_hctrl_off_target = CDNS_DPN_B0_HCTRL(target_num); 1526 dpn_offsetctrl_off_target = CDNS_DPN_B0_OFFSET_CTRL(target_num); 1527 dpn_samplectrl_off_target = CDNS_DPN_B0_SAMPLE_CTRL(target_num); 1528 } 1529 1530 dpn_config = cdns_readl(cdns, dpn_config_off_source); 1531 if (!override) { 1532 u32p_replace_bits(&dpn_config, t_params->blk_grp_ctrl, CDNS_DPN_CONFIG_BGC); 1533 u32p_replace_bits(&dpn_config, t_params->blk_pkg_mode, CDNS_DPN_CONFIG_BPM); 1534 } 1535 cdns_writel(cdns, dpn_config_off_target, dpn_config); 1536 1537 if (!override) { 1538 dpn_offsetctrl = 0; 1539 u32p_replace_bits(&dpn_offsetctrl, t_params->offset1, CDNS_DPN_OFFSET_CTRL_1); 1540 u32p_replace_bits(&dpn_offsetctrl, t_params->offset2, CDNS_DPN_OFFSET_CTRL_2); 1541 } else { 1542 dpn_offsetctrl = cdns_readl(cdns, dpn_offsetctrl_off_source); 1543 } 1544 cdns_writel(cdns, dpn_offsetctrl_off_target, dpn_offsetctrl); 1545 1546 if (!override) { 1547 dpn_hctrl = 0; 1548 u32p_replace_bits(&dpn_hctrl, t_params->hstart, CDNS_DPN_HCTRL_HSTART); 1549 u32p_replace_bits(&dpn_hctrl, t_params->hstop, CDNS_DPN_HCTRL_HSTOP); 1550 u32p_replace_bits(&dpn_hctrl, t_params->lane_ctrl, CDNS_DPN_HCTRL_LCTRL); 1551 } else { 1552 dpn_hctrl = cdns_readl(cdns, dpn_hctrl_off_source); 1553 } 1554 cdns_writel(cdns, dpn_hctrl_off_target, dpn_hctrl); 1555 1556 if (!override) 1557 dpn_samplectrl = t_params->sample_interval - 1; 1558 else 1559 dpn_samplectrl = cdns_readl(cdns, dpn_samplectrl_off_source); 1560 cdns_writel(cdns, dpn_samplectrl_off_target, dpn_samplectrl); 1561 1562 return 0; 1563 } 1564 1565 static int cdns_port_enable(struct sdw_bus *bus, 1566 struct sdw_enable_ch *enable_ch, unsigned int bank) 1567 { 1568 struct sdw_cdns *cdns = bus_to_cdns(bus); 1569 int dpn_chnen_off, ch_mask; 1570 1571 if (bank) 1572 dpn_chnen_off = CDNS_DPN_B1_CH_EN(enable_ch->port_num); 1573 else 1574 dpn_chnen_off = CDNS_DPN_B0_CH_EN(enable_ch->port_num); 1575 1576 ch_mask = enable_ch->ch_mask * enable_ch->enable; 1577 cdns_writel(cdns, dpn_chnen_off, ch_mask); 1578 1579 return 0; 1580 } 1581 1582 static const struct sdw_master_port_ops cdns_port_ops = { 1583 .dpn_set_port_params = cdns_port_params, 1584 .dpn_set_port_transport_params = cdns_transport_params, 1585 .dpn_port_enable_ch = cdns_port_enable, 1586 }; 1587 1588 /** 1589 * sdw_cdns_is_clock_stop: Check clock status 1590 * 1591 * @cdns: Cadence instance 1592 */ 1593 bool sdw_cdns_is_clock_stop(struct sdw_cdns *cdns) 1594 { 1595 return !!(cdns_readl(cdns, CDNS_MCP_STAT) & CDNS_MCP_STAT_CLK_STOP); 1596 } 1597 EXPORT_SYMBOL(sdw_cdns_is_clock_stop); 1598 1599 /** 1600 * sdw_cdns_clock_stop: Cadence clock stop configuration routine 1601 * 1602 * @cdns: Cadence instance 1603 * @block_wake: prevent wakes if required by the platform 1604 */ 1605 int sdw_cdns_clock_stop(struct sdw_cdns *cdns, bool block_wake) 1606 { 1607 bool slave_present = false; 1608 struct sdw_slave *slave; 1609 int ret; 1610 1611 sdw_cdns_check_self_clearing_bits(cdns, __func__, false, 0); 1612 1613 /* Check suspend status */ 1614 if (sdw_cdns_is_clock_stop(cdns)) { 1615 dev_dbg(cdns->dev, "Clock is already stopped\n"); 1616 return 0; 1617 } 1618 1619 /* 1620 * Before entering clock stop we mask the Slave 1621 * interrupts. This helps avoid having to deal with e.g. a 1622 * Slave becoming UNATTACHED while the clock is being stopped 1623 */ 1624 cdns_enable_slave_interrupts(cdns, false); 1625 1626 /* 1627 * For specific platforms, it is required to be able to put 1628 * master into a state in which it ignores wake-up trials 1629 * in clock stop state 1630 */ 1631 if (block_wake) 1632 cdns_ip_updatel(cdns, CDNS_IP_MCP_CONTROL, 1633 CDNS_IP_MCP_CONTROL_BLOCK_WAKEUP, 1634 CDNS_IP_MCP_CONTROL_BLOCK_WAKEUP); 1635 1636 list_for_each_entry(slave, &cdns->bus.slaves, node) { 1637 if (slave->status == SDW_SLAVE_ATTACHED || 1638 slave->status == SDW_SLAVE_ALERT) { 1639 slave_present = true; 1640 break; 1641 } 1642 } 1643 1644 /* commit changes */ 1645 ret = cdns_config_update(cdns); 1646 if (ret < 0) { 1647 dev_err(cdns->dev, "%s: config_update failed\n", __func__); 1648 return ret; 1649 } 1650 1651 /* Prepare slaves for clock stop */ 1652 if (slave_present) { 1653 ret = sdw_bus_prep_clk_stop(&cdns->bus); 1654 if (ret < 0 && ret != -ENODATA) { 1655 dev_err(cdns->dev, "prepare clock stop failed %d\n", ret); 1656 return ret; 1657 } 1658 } 1659 1660 /* 1661 * Enter clock stop mode and only report errors if there are 1662 * Slave devices present (ALERT or ATTACHED) 1663 */ 1664 ret = sdw_bus_clk_stop(&cdns->bus); 1665 if (ret < 0 && slave_present && ret != -ENODATA) { 1666 dev_err(cdns->dev, "bus clock stop failed %d\n", ret); 1667 return ret; 1668 } 1669 1670 ret = cdns_set_wait(cdns, CDNS_MCP_STAT, 1671 CDNS_MCP_STAT_CLK_STOP, 1672 CDNS_MCP_STAT_CLK_STOP); 1673 if (ret < 0) 1674 dev_err(cdns->dev, "Clock stop failed %d\n", ret); 1675 1676 return ret; 1677 } 1678 EXPORT_SYMBOL(sdw_cdns_clock_stop); 1679 1680 /** 1681 * sdw_cdns_clock_restart: Cadence PM clock restart configuration routine 1682 * 1683 * @cdns: Cadence instance 1684 * @bus_reset: context may be lost while in low power modes and the bus 1685 * may require a Severe Reset and re-enumeration after a wake. 1686 */ 1687 int sdw_cdns_clock_restart(struct sdw_cdns *cdns, bool bus_reset) 1688 { 1689 int ret; 1690 1691 /* unmask Slave interrupts that were masked when stopping the clock */ 1692 cdns_enable_slave_interrupts(cdns, true); 1693 1694 ret = cdns_clear_bit(cdns, CDNS_MCP_CONTROL, 1695 CDNS_MCP_CONTROL_CLK_STOP_CLR); 1696 if (ret < 0) { 1697 dev_err(cdns->dev, "Couldn't exit from clock stop\n"); 1698 return ret; 1699 } 1700 1701 ret = cdns_set_wait(cdns, CDNS_MCP_STAT, CDNS_MCP_STAT_CLK_STOP, 0); 1702 if (ret < 0) { 1703 dev_err(cdns->dev, "clock stop exit failed %d\n", ret); 1704 return ret; 1705 } 1706 1707 cdns_ip_updatel(cdns, CDNS_IP_MCP_CONTROL, 1708 CDNS_IP_MCP_CONTROL_BLOCK_WAKEUP, 0); 1709 1710 cdns_ip_updatel(cdns, CDNS_IP_MCP_CONTROL, CDNS_IP_MCP_CONTROL_CMD_ACCEPT, 1711 CDNS_IP_MCP_CONTROL_CMD_ACCEPT); 1712 1713 if (!bus_reset) { 1714 1715 /* enable bus operations with clock and data */ 1716 cdns_ip_updatel(cdns, CDNS_IP_MCP_CONFIG, 1717 CDNS_IP_MCP_CONFIG_OP, 1718 CDNS_IP_MCP_CONFIG_OP_NORMAL); 1719 1720 ret = cdns_config_update(cdns); 1721 if (ret < 0) { 1722 dev_err(cdns->dev, "%s: config_update failed\n", __func__); 1723 return ret; 1724 } 1725 1726 ret = sdw_bus_exit_clk_stop(&cdns->bus); 1727 if (ret < 0) 1728 dev_err(cdns->dev, "bus failed to exit clock stop %d\n", ret); 1729 } 1730 1731 return ret; 1732 } 1733 EXPORT_SYMBOL(sdw_cdns_clock_restart); 1734 1735 /** 1736 * sdw_cdns_probe() - Cadence probe routine 1737 * @cdns: Cadence instance 1738 */ 1739 int sdw_cdns_probe(struct sdw_cdns *cdns) 1740 { 1741 init_completion(&cdns->tx_complete); 1742 cdns->bus.port_ops = &cdns_port_ops; 1743 1744 INIT_WORK(&cdns->work, cdns_update_slave_status_work); 1745 return 0; 1746 } 1747 EXPORT_SYMBOL(sdw_cdns_probe); 1748 1749 int cdns_set_sdw_stream(struct snd_soc_dai *dai, 1750 void *stream, int direction) 1751 { 1752 struct sdw_cdns *cdns = snd_soc_dai_get_drvdata(dai); 1753 struct sdw_cdns_dai_runtime *dai_runtime; 1754 1755 dai_runtime = cdns->dai_runtime_array[dai->id]; 1756 1757 if (stream) { 1758 /* first paranoia check */ 1759 if (dai_runtime) { 1760 dev_err(dai->dev, 1761 "dai_runtime already allocated for dai %s\n", 1762 dai->name); 1763 return -EINVAL; 1764 } 1765 1766 /* allocate and set dai_runtime info */ 1767 dai_runtime = kzalloc(sizeof(*dai_runtime), GFP_KERNEL); 1768 if (!dai_runtime) 1769 return -ENOMEM; 1770 1771 dai_runtime->stream_type = SDW_STREAM_PCM; 1772 1773 dai_runtime->bus = &cdns->bus; 1774 dai_runtime->link_id = cdns->instance; 1775 1776 dai_runtime->stream = stream; 1777 dai_runtime->direction = direction; 1778 1779 cdns->dai_runtime_array[dai->id] = dai_runtime; 1780 } else { 1781 /* second paranoia check */ 1782 if (!dai_runtime) { 1783 dev_err(dai->dev, 1784 "dai_runtime not allocated for dai %s\n", 1785 dai->name); 1786 return -EINVAL; 1787 } 1788 1789 /* for NULL stream we release allocated dai_runtime */ 1790 kfree(dai_runtime); 1791 cdns->dai_runtime_array[dai->id] = NULL; 1792 } 1793 return 0; 1794 } 1795 EXPORT_SYMBOL(cdns_set_sdw_stream); 1796 1797 /** 1798 * cdns_find_pdi() - Find a free PDI 1799 * 1800 * @cdns: Cadence instance 1801 * @num: Number of PDIs 1802 * @pdi: PDI instances 1803 * @dai_id: DAI id 1804 * 1805 * Find a PDI for a given PDI array. The PDI num and dai_id are 1806 * expected to match, return NULL otherwise. 1807 */ 1808 static struct sdw_cdns_pdi *cdns_find_pdi(struct sdw_cdns *cdns, 1809 unsigned int num, 1810 struct sdw_cdns_pdi *pdi, 1811 int dai_id) 1812 { 1813 int i; 1814 1815 for (i = 0; i < num; i++) 1816 if (pdi[i].num == dai_id) 1817 return &pdi[i]; 1818 1819 return NULL; 1820 } 1821 1822 /** 1823 * sdw_cdns_config_stream: Configure a stream 1824 * 1825 * @cdns: Cadence instance 1826 * @ch: Channel count 1827 * @dir: Data direction 1828 * @pdi: PDI to be used 1829 */ 1830 void sdw_cdns_config_stream(struct sdw_cdns *cdns, 1831 u32 ch, u32 dir, struct sdw_cdns_pdi *pdi) 1832 { 1833 u32 offset, val = 0; 1834 1835 if (dir == SDW_DATA_DIR_RX) { 1836 val = CDNS_PORTCTRL_DIRN; 1837 1838 if (cdns->bus.params.m_data_mode != SDW_PORT_DATA_MODE_NORMAL) 1839 val |= CDNS_PORTCTRL_TEST_FAILED; 1840 } 1841 offset = CDNS_PORTCTRL + pdi->num * CDNS_PORT_OFFSET; 1842 cdns_updatel(cdns, offset, 1843 CDNS_PORTCTRL_DIRN | CDNS_PORTCTRL_TEST_FAILED, 1844 val); 1845 1846 val = pdi->num; 1847 val |= CDNS_PDI_CONFIG_SOFT_RESET; 1848 val |= FIELD_PREP(CDNS_PDI_CONFIG_CHANNEL, (1 << ch) - 1); 1849 cdns_writel(cdns, CDNS_PDI_CONFIG(pdi->num), val); 1850 } 1851 EXPORT_SYMBOL(sdw_cdns_config_stream); 1852 1853 /** 1854 * sdw_cdns_alloc_pdi() - Allocate a PDI 1855 * 1856 * @cdns: Cadence instance 1857 * @stream: Stream to be allocated 1858 * @ch: Channel count 1859 * @dir: Data direction 1860 * @dai_id: DAI id 1861 */ 1862 struct sdw_cdns_pdi *sdw_cdns_alloc_pdi(struct sdw_cdns *cdns, 1863 struct sdw_cdns_streams *stream, 1864 u32 ch, u32 dir, int dai_id) 1865 { 1866 struct sdw_cdns_pdi *pdi = NULL; 1867 1868 if (dir == SDW_DATA_DIR_RX) 1869 pdi = cdns_find_pdi(cdns, stream->num_in, stream->in, 1870 dai_id); 1871 else 1872 pdi = cdns_find_pdi(cdns, stream->num_out, stream->out, 1873 dai_id); 1874 1875 /* check if we found a PDI, else find in bi-directional */ 1876 if (!pdi) 1877 pdi = cdns_find_pdi(cdns, stream->num_bd, stream->bd, 1878 dai_id); 1879 1880 if (pdi) { 1881 pdi->l_ch_num = 0; 1882 pdi->h_ch_num = ch - 1; 1883 pdi->dir = dir; 1884 pdi->ch_count = ch; 1885 } 1886 1887 return pdi; 1888 } 1889 EXPORT_SYMBOL(sdw_cdns_alloc_pdi); 1890 1891 MODULE_LICENSE("Dual BSD/GPL"); 1892 MODULE_DESCRIPTION("Cadence Soundwire Library"); 1893