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