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