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