1 // SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
2 // Copyright(c) 2015-17 Intel Corporation.
5 * Cadence SoundWire Master module
6 * Used by Master driver
9 #include <linux/delay.h>
10 #include <linux/device.h>
11 #include <linux/debugfs.h>
12 #include <linux/interrupt.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>
23 #include "cadence_master.h"
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");
29 #define CDNS_MCP_CONFIG 0x0
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
41 #define CDNS_MCP_CONTROL 0x4
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)
53 #define CDNS_MCP_CMDCTRL 0x8
55 #define CDNS_MCP_CMDCTRL_INSERT_PARITY_ERR BIT(2)
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)
63 #define CDNS_MCP_CONFIG_UPDATE 0x18
64 #define CDNS_MCP_CONFIG_UPDATE_BIT BIT(0)
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)
73 #define CDNS_MCP_STAT 0x40
75 #define CDNS_MCP_STAT_ACTIVE_BANK BIT(20)
76 #define CDNS_MCP_STAT_CLK_STOP BIT(16)
78 #define CDNS_MCP_INTSTAT 0x44
79 #define CDNS_MCP_INTMASK 0x48
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)
101 #define CDNS_MCP_INTSET 0x4C
103 #define CDNS_MCP_SLAVE_STAT 0x50
104 #define CDNS_MCP_SLAVE_STAT_MASK GENMASK(1, 0)
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
115 #define CDNS_MCP_SLAVE_INTMASK0 0x5C
116 #define CDNS_MCP_SLAVE_INTMASK1 0x60
118 #define CDNS_MCP_SLAVE_INTMASK0_MASK GENMASK(31, 0)
119 #define CDNS_MCP_SLAVE_INTMASK1_MASK GENMASK(15, 0)
121 #define CDNS_MCP_PORT_INTSTAT 0x64
122 #define CDNS_MCP_PDI_STAT 0x6C
124 #define CDNS_MCP_FIFOLEVEL 0x78
125 #define CDNS_MCP_FIFOSTAT 0x7C
126 #define CDNS_MCP_RX_FIFO_AVAIL GENMASK(5, 0)
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
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)
139 #define CDNS_MCP_CMD_READ 2
140 #define CDNS_MCP_CMD_WRITE 3
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)
146 #define CDNS_DP_SIZE 128
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))
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))
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)
168 #define CDNS_DPN_SAMPLE_CTRL_SI GENMASK(15, 0)
170 #define CDNS_DPN_OFFSET_CTRL_1 GENMASK(7, 0)
171 #define CDNS_DPN_OFFSET_CTRL_2 GENMASK(15, 8)
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)
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)
182 #define CDNS_PORT_OFFSET 0x80
184 #define CDNS_PDI_CONFIG(n) (0x1100 + (n) * 16)
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)
190 /* Driver defaults */
191 #define CDNS_TX_TIMEOUT 500
193 #define CDNS_SCP_RX_FIFOLEVEL 0x2
196 * register accessor helpers
198 static inline u32 cdns_readl(struct sdw_cdns *cdns, int offset)
200 return readl(cdns->registers + offset);
203 static inline void cdns_writel(struct sdw_cdns *cdns, int offset, u32 value)
205 writel(value, cdns->registers + offset);
208 static inline void cdns_updatel(struct sdw_cdns *cdns,
209 int offset, u32 mask, u32 val)
213 tmp = cdns_readl(cdns, offset);
214 tmp = (tmp & ~mask) | val;
215 cdns_writel(cdns, offset, tmp);
218 static int cdns_set_wait(struct sdw_cdns *cdns, int offset, u32 mask, u32 value)
223 /* Wait for bit to be set */
225 reg_read = readl(cdns->registers + offset);
226 if ((reg_read & mask) == value)
230 usleep_range(50, 100);
231 } while (timeout != 0);
236 static int cdns_clear_bit(struct sdw_cdns *cdns, int offset, u32 value)
238 writel(value, cdns->registers + offset);
240 /* Wait for bit to be self cleared */
241 return cdns_set_wait(cdns, offset, value, 0);
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
248 static int cdns_config_update(struct sdw_cdns *cdns)
252 if (sdw_cdns_is_clock_stop(cdns)) {
253 dev_err(cdns->dev, "Cannot program MCP_CONFIG_UPDATE in ClockStopMode\n");
257 ret = cdns_clear_bit(cdns, CDNS_MCP_CONFIG_UPDATE,
258 CDNS_MCP_CONFIG_UPDATE_BIT);
260 dev_err(cdns->dev, "Config update timedout\n");
268 #ifdef CONFIG_DEBUG_FS
270 #define RD_BUF (2 * PAGE_SIZE)
272 static ssize_t cdns_sprintf(struct sdw_cdns *cdns,
273 char *buf, size_t pos, unsigned int reg)
275 return scnprintf(buf + pos, RD_BUF - pos,
276 "%4x\t%8x\n", reg, cdns_readl(cdns, reg));
279 static int cdns_reg_show(struct seq_file *s, void *data)
281 struct sdw_cdns *cdns = s->private;
287 buf = kzalloc(RD_BUF, GFP_KERNEL);
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);
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);
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);
310 ret += scnprintf(buf + ret, RD_BUF - ret,
311 "\nDPn B0 Registers\n");
313 num_ports = cdns->num_ports;
315 for (i = 0; i < num_ports; i++) {
316 ret += scnprintf(buf + ret, RD_BUF - ret,
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);
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,
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);
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);
340 ret += scnprintf(buf + ret, RD_BUF - ret,
341 "\nPDIn Config Registers\n");
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));
347 seq_printf(s, "%s", buf);
352 DEFINE_SHOW_ATTRIBUTE(cdns_reg);
354 static int cdns_hw_reset(void *data, u64 value)
356 struct sdw_cdns *cdns = data;
362 /* Userspace changed the hardware state behind the kernel's back */
363 add_taint(TAINT_USER, LOCKDEP_STILL_OK);
365 ret = sdw_cdns_exit_reset(cdns);
367 dev_dbg(cdns->dev, "link hw_reset done: %d\n", ret);
372 DEFINE_DEBUGFS_ATTRIBUTE(cdns_hw_reset_fops, NULL, cdns_hw_reset, "%llu\n");
374 static int cdns_parity_error_injection(void *data, u64 value)
376 struct sdw_cdns *cdns = data;
386 * Resume Master device. If this results in a bus reset, the
387 * Slave devices will re-attach and be re-enumerated.
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",
398 * wait long enough for Slave(s) to be in steady state. This
399 * does not need to be super precise.
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
407 mutex_lock(&bus->bus_lock);
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);
415 cdns_updatel(cdns, CDNS_MCP_CONFIG_UPDATE,
416 CDNS_MCP_CONFIG_UPDATE_BIT,
417 CDNS_MCP_CONFIG_UPDATE_BIT);
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);
423 /* program hardware to disable parity error */
424 cdns_updatel(cdns, CDNS_MCP_CMDCTRL,
425 CDNS_MCP_CMDCTRL_INSERT_PARITY_ERR,
429 cdns_updatel(cdns, CDNS_MCP_CONFIG_UPDATE,
430 CDNS_MCP_CONFIG_UPDATE_BIT,
431 CDNS_MCP_CONFIG_UPDATE_BIT);
433 /* Continue bus operation with parity error injection disabled */
434 mutex_unlock(&bus->bus_lock);
436 /* Userspace changed the hardware state behind the kernel's back */
437 add_taint(TAINT_USER, LOCKDEP_STILL_OK);
440 * allow Master device to enter pm_runtime suspend. This may
441 * also result in Slave devices suspending.
443 pm_runtime_mark_last_busy(bus->dev);
444 pm_runtime_put_autosuspend(bus->dev);
449 DEFINE_DEBUGFS_ATTRIBUTE(cdns_parity_error_fops, NULL,
450 cdns_parity_error_injection, "%llu\n");
452 static int cdns_set_pdi_loopback_source(void *data, u64 value)
454 struct sdw_cdns *cdns = data;
455 unsigned int pdi_out_num = cdns->pcm.num_bd + cdns->pcm.num_out;
457 if (value > pdi_out_num)
460 /* Userspace changed the hardware state behind the kernel's back */
461 add_taint(TAINT_USER, LOCKDEP_STILL_OK);
463 cdns->pdi_loopback_source = value;
467 DEFINE_DEBUGFS_ATTRIBUTE(cdns_pdi_loopback_source_fops, NULL, cdns_set_pdi_loopback_source, "%llu\n");
469 static int cdns_set_pdi_loopback_target(void *data, u64 value)
471 struct sdw_cdns *cdns = data;
472 unsigned int pdi_in_num = cdns->pcm.num_bd + cdns->pcm.num_in;
474 if (value > pdi_in_num)
477 /* Userspace changed the hardware state behind the kernel's back */
478 add_taint(TAINT_USER, LOCKDEP_STILL_OK);
480 cdns->pdi_loopback_target = value;
484 DEFINE_DEBUGFS_ATTRIBUTE(cdns_pdi_loopback_target_fops, NULL, cdns_set_pdi_loopback_target, "%llu\n");
487 * sdw_cdns_debugfs_init() - Cadence debugfs init
488 * @cdns: Cadence instance
489 * @root: debugfs root
491 void sdw_cdns_debugfs_init(struct sdw_cdns *cdns, struct dentry *root)
493 debugfs_create_file("cdns-registers", 0400, root, cdns, &cdns_reg_fops);
495 debugfs_create_file("cdns-hw-reset", 0200, root, cdns,
496 &cdns_hw_reset_fops);
498 debugfs_create_file("cdns-parity-error-injection", 0200, root, cdns,
499 &cdns_parity_error_fops);
501 cdns->pdi_loopback_source = -1;
502 cdns->pdi_loopback_target = -1;
504 debugfs_create_file("cdns-pdi-loopback-source", 0200, root, cdns,
505 &cdns_pdi_loopback_source_fops);
507 debugfs_create_file("cdns-pdi-loopback-target", 0200, root, cdns,
508 &cdns_pdi_loopback_target_fops);
511 EXPORT_SYMBOL_GPL(sdw_cdns_debugfs_init);
513 #endif /* CONFIG_DEBUG_FS */
518 static enum sdw_command_response
519 cdns_fill_msg_resp(struct sdw_cdns *cdns,
520 struct sdw_msg *msg, int count, int offset)
522 int nack = 0, no_ack = 0;
525 /* check message response */
526 for (i = 0; i < count; i++) {
527 if (!(cdns->response_buf[i] & CDNS_MCP_RESP_ACK)) {
529 dev_vdbg(cdns->dev, "Msg Ack not received, cmd %d\n", i);
531 if (cdns->response_buf[i] & CDNS_MCP_RESP_NACK) {
533 dev_err_ratelimited(cdns->dev, "Msg NACK received, cmd %d\n", i);
538 dev_err_ratelimited(cdns->dev, "Msg NACKed for Slave %d\n", msg->dev_num);
543 dev_dbg_ratelimited(cdns->dev, "Msg ignored for Slave %d\n", msg->dev_num);
544 return SDW_CMD_IGNORED;
548 for (i = 0; i < count; i++)
549 msg->buf[i + offset] = FIELD_GET(CDNS_MCP_RESP_RDATA, cdns->response_buf[i]);
554 static enum sdw_command_response
555 _cdns_xfer_msg(struct sdw_cdns *cdns, struct sdw_msg *msg, int cmd,
556 int offset, int count, bool defer)
562 /* Program the watermark level for RX FIFO */
563 if (cdns->msg_count != count) {
564 cdns_writel(cdns, CDNS_MCP_FIFOLEVEL, count);
565 cdns->msg_count = count;
568 base = CDNS_MCP_CMD_BASE;
571 for (i = 0; i < count; i++) {
572 data = FIELD_PREP(CDNS_MCP_CMD_DEV_ADDR, msg->dev_num);
573 data |= FIELD_PREP(CDNS_MCP_CMD_COMMAND, cmd);
574 data |= FIELD_PREP(CDNS_MCP_CMD_REG_ADDR, addr);
577 if (msg->flags == SDW_MSG_FLAG_WRITE)
578 data |= msg->buf[i + offset];
580 data |= FIELD_PREP(CDNS_MCP_CMD_SSP_TAG, msg->ssp_sync);
581 cdns_writel(cdns, base, data);
582 base += CDNS_MCP_CMD_WORD_LEN;
588 /* wait for timeout or response */
589 time = wait_for_completion_timeout(&cdns->tx_complete,
590 msecs_to_jiffies(CDNS_TX_TIMEOUT));
592 dev_err(cdns->dev, "IO transfer timed out, cmd %d device %d addr %x len %d\n",
593 cmd, msg->dev_num, msg->addr, msg->len);
595 return SDW_CMD_TIMEOUT;
598 return cdns_fill_msg_resp(cdns, msg, count, offset);
601 static enum sdw_command_response
602 cdns_program_scp_addr(struct sdw_cdns *cdns, struct sdw_msg *msg)
604 int nack = 0, no_ack = 0;
609 /* Program the watermark level for RX FIFO */
610 if (cdns->msg_count != CDNS_SCP_RX_FIFOLEVEL) {
611 cdns_writel(cdns, CDNS_MCP_FIFOLEVEL, CDNS_SCP_RX_FIFOLEVEL);
612 cdns->msg_count = CDNS_SCP_RX_FIFOLEVEL;
615 data[0] = FIELD_PREP(CDNS_MCP_CMD_DEV_ADDR, msg->dev_num);
616 data[0] |= FIELD_PREP(CDNS_MCP_CMD_COMMAND, 0x3);
619 data[0] |= FIELD_PREP(CDNS_MCP_CMD_REG_ADDR, SDW_SCP_ADDRPAGE1);
620 data[1] |= FIELD_PREP(CDNS_MCP_CMD_REG_ADDR, SDW_SCP_ADDRPAGE2);
622 data[0] |= msg->addr_page1;
623 data[1] |= msg->addr_page2;
625 base = CDNS_MCP_CMD_BASE;
626 cdns_writel(cdns, base, data[0]);
627 base += CDNS_MCP_CMD_WORD_LEN;
628 cdns_writel(cdns, base, data[1]);
630 time = wait_for_completion_timeout(&cdns->tx_complete,
631 msecs_to_jiffies(CDNS_TX_TIMEOUT));
633 dev_err(cdns->dev, "SCP Msg trf timed out\n");
635 return SDW_CMD_TIMEOUT;
638 /* check response the writes */
639 for (i = 0; i < 2; i++) {
640 if (!(cdns->response_buf[i] & CDNS_MCP_RESP_ACK)) {
642 dev_err(cdns->dev, "Program SCP Ack not received\n");
643 if (cdns->response_buf[i] & CDNS_MCP_RESP_NACK) {
645 dev_err(cdns->dev, "Program SCP NACK received\n");
650 /* For NACK, NO ack, don't return err if we are in Broadcast mode */
652 dev_err_ratelimited(cdns->dev,
653 "SCP_addrpage NACKed for Slave %d\n", msg->dev_num);
658 dev_dbg_ratelimited(cdns->dev,
659 "SCP_addrpage ignored for Slave %d\n", msg->dev_num);
660 return SDW_CMD_IGNORED;
666 static int cdns_prep_msg(struct sdw_cdns *cdns, struct sdw_msg *msg, int *cmd)
671 ret = cdns_program_scp_addr(cdns, msg);
678 switch (msg->flags) {
679 case SDW_MSG_FLAG_READ:
680 *cmd = CDNS_MCP_CMD_READ;
683 case SDW_MSG_FLAG_WRITE:
684 *cmd = CDNS_MCP_CMD_WRITE;
688 dev_err(cdns->dev, "Invalid msg cmd: %d\n", msg->flags);
695 enum sdw_command_response
696 cdns_xfer_msg(struct sdw_bus *bus, struct sdw_msg *msg)
698 struct sdw_cdns *cdns = bus_to_cdns(bus);
701 ret = cdns_prep_msg(cdns, msg, &cmd);
703 return SDW_CMD_FAIL_OTHER;
705 for (i = 0; i < msg->len / CDNS_MCP_CMD_LEN; i++) {
706 ret = _cdns_xfer_msg(cdns, msg, cmd, i * CDNS_MCP_CMD_LEN,
707 CDNS_MCP_CMD_LEN, false);
712 if (!(msg->len % CDNS_MCP_CMD_LEN))
715 ret = _cdns_xfer_msg(cdns, msg, cmd, i * CDNS_MCP_CMD_LEN,
716 msg->len % CDNS_MCP_CMD_LEN, false);
721 EXPORT_SYMBOL(cdns_xfer_msg);
723 enum sdw_command_response
724 cdns_xfer_msg_defer(struct sdw_bus *bus,
725 struct sdw_msg *msg, struct sdw_defer *defer)
727 struct sdw_cdns *cdns = bus_to_cdns(bus);
730 /* for defer only 1 message is supported */
734 ret = cdns_prep_msg(cdns, msg, &cmd);
736 return SDW_CMD_FAIL_OTHER;
739 cdns->defer->length = msg->len;
741 return _cdns_xfer_msg(cdns, msg, cmd, 0, msg->len, true);
743 EXPORT_SYMBOL(cdns_xfer_msg_defer);
745 enum sdw_command_response
746 cdns_reset_page_addr(struct sdw_bus *bus, unsigned int dev_num)
748 struct sdw_cdns *cdns = bus_to_cdns(bus);
751 /* Create dummy message with valid device number */
752 memset(&msg, 0, sizeof(msg));
753 msg.dev_num = dev_num;
755 return cdns_program_scp_addr(cdns, &msg);
757 EXPORT_SYMBOL(cdns_reset_page_addr);
763 static void cdns_read_response(struct sdw_cdns *cdns)
765 u32 num_resp, cmd_base;
768 num_resp = cdns_readl(cdns, CDNS_MCP_FIFOSTAT);
769 num_resp &= CDNS_MCP_RX_FIFO_AVAIL;
771 cmd_base = CDNS_MCP_CMD_BASE;
773 for (i = 0; i < num_resp; i++) {
774 cdns->response_buf[i] = cdns_readl(cdns, cmd_base);
775 cmd_base += CDNS_MCP_CMD_WORD_LEN;
779 static int cdns_update_slave_status(struct sdw_cdns *cdns,
782 enum sdw_slave_status status[SDW_MAX_DEVICES + 1];
783 bool is_slave = false;
787 memset(status, 0, sizeof(status));
789 for (i = 0; i <= SDW_MAX_DEVICES; i++) {
790 mask = (slave_intstat >> (i * CDNS_MCP_SLAVE_STATUS_NUM)) &
791 CDNS_MCP_SLAVE_STATUS_BITS;
798 if (mask & CDNS_MCP_SLAVE_INTSTAT_RESERVED) {
799 status[i] = SDW_SLAVE_RESERVED;
803 if (mask & CDNS_MCP_SLAVE_INTSTAT_ATTACHED) {
804 status[i] = SDW_SLAVE_ATTACHED;
808 if (mask & CDNS_MCP_SLAVE_INTSTAT_ALERT) {
809 status[i] = SDW_SLAVE_ALERT;
813 if (mask & CDNS_MCP_SLAVE_INTSTAT_NPRESENT) {
814 status[i] = SDW_SLAVE_UNATTACHED;
818 /* first check if Slave reported multiple status */
819 if (set_status > 1) {
822 dev_warn_ratelimited(cdns->dev,
823 "Slave %d reported multiple Status: %d\n",
826 /* check latest status extracted from PING commands */
827 val = cdns_readl(cdns, CDNS_MCP_SLAVE_STAT);
832 status[i] = SDW_SLAVE_UNATTACHED;
835 status[i] = SDW_SLAVE_ATTACHED;
838 status[i] = SDW_SLAVE_ALERT;
842 status[i] = SDW_SLAVE_RESERVED;
846 dev_warn_ratelimited(cdns->dev,
847 "Slave %d status updated to %d\n",
854 return sdw_handle_slave_status(&cdns->bus, status);
860 * sdw_cdns_irq() - Cadence interrupt handler
862 * @dev_id: irq context
864 irqreturn_t sdw_cdns_irq(int irq, void *dev_id)
866 struct sdw_cdns *cdns = dev_id;
869 /* Check if the link is up */
873 int_status = cdns_readl(cdns, CDNS_MCP_INTSTAT);
875 /* check for reserved values read as zero */
876 if (int_status & CDNS_MCP_INT_RESERVED)
879 if (!(int_status & CDNS_MCP_INT_IRQ))
882 if (int_status & CDNS_MCP_INT_RX_WL) {
883 cdns_read_response(cdns);
886 cdns_fill_msg_resp(cdns, cdns->defer->msg,
887 cdns->defer->length, 0);
888 complete(&cdns->defer->complete);
891 complete(&cdns->tx_complete);
895 if (int_status & CDNS_MCP_INT_PARITY) {
896 /* Parity error detected by Master */
897 dev_err_ratelimited(cdns->dev, "Parity error\n");
900 if (int_status & CDNS_MCP_INT_CTRL_CLASH) {
901 /* Slave is driving bit slot during control word */
902 dev_err_ratelimited(cdns->dev, "Bus clash for control word\n");
905 if (int_status & CDNS_MCP_INT_DATA_CLASH) {
907 * Multiple slaves trying to drive bit slot, or issue with
908 * ownership of data bits or Slave gone bonkers
910 dev_err_ratelimited(cdns->dev, "Bus clash for data word\n");
913 if (cdns->bus.params.m_data_mode != SDW_PORT_DATA_MODE_NORMAL &&
914 int_status & CDNS_MCP_INT_DPINT) {
917 /* just log which ports report an error */
918 port_intstat = cdns_readl(cdns, CDNS_MCP_PORT_INTSTAT);
919 dev_err_ratelimited(cdns->dev, "DP interrupt: PortIntStat %8x\n",
922 /* clear status w/ write1 */
923 cdns_writel(cdns, CDNS_MCP_PORT_INTSTAT, port_intstat);
926 if (int_status & CDNS_MCP_INT_SLAVE_MASK) {
927 /* Mask the Slave interrupt and wake thread */
928 cdns_updatel(cdns, CDNS_MCP_INTMASK,
929 CDNS_MCP_INT_SLAVE_MASK, 0);
931 int_status &= ~CDNS_MCP_INT_SLAVE_MASK;
934 * Deal with possible race condition between interrupt
935 * handling and disabling interrupts on suspend.
937 * If the master is in the process of disabling
938 * interrupts, don't schedule a workqueue
940 if (cdns->interrupt_enabled)
941 schedule_work(&cdns->work);
944 cdns_writel(cdns, CDNS_MCP_INTSTAT, int_status);
947 EXPORT_SYMBOL(sdw_cdns_irq);
950 * cdns_update_slave_status_work - update slave status in a work since we will need to handle
951 * other interrupts eg. CDNS_MCP_INT_RX_WL during the update slave
953 * @work: cdns worker thread
955 static void cdns_update_slave_status_work(struct work_struct *work)
957 struct sdw_cdns *cdns =
958 container_of(work, struct sdw_cdns, work);
964 slave0 = cdns_readl(cdns, CDNS_MCP_SLAVE_INTSTAT0);
965 slave1 = cdns_readl(cdns, CDNS_MCP_SLAVE_INTSTAT1);
967 /* combine the two status */
968 slave_intstat = ((u64)slave1 << 32) | slave0;
970 dev_dbg_ratelimited(cdns->dev, "Slave status change: 0x%llx\n", slave_intstat);
973 cdns_update_slave_status(cdns, slave_intstat);
974 cdns_writel(cdns, CDNS_MCP_SLAVE_INTSTAT0, slave0);
975 cdns_writel(cdns, CDNS_MCP_SLAVE_INTSTAT1, slave1);
978 * When there is more than one peripheral per link, it's
979 * possible that a deviceB becomes attached after we deal with
980 * the attachment of deviceA. Since the hardware does a
981 * logical AND, the attachment of the second device does not
982 * change the status seen by the driver.
984 * In that case, clearing the registers above would result in
985 * the deviceB never being detected - until a change of status
986 * is observed on the bus.
988 * To avoid this race condition, re-check if any device0 needs
989 * attention with PING commands. There is no need to check for
990 * ALERTS since they are not allowed until a non-zero
991 * device_number is assigned.
994 device0_status = cdns_readl(cdns, CDNS_MCP_SLAVE_STAT);
997 if (device0_status == SDW_SLAVE_ATTACHED) {
998 if (retry_count++ < SDW_MAX_DEVICES) {
999 dev_dbg_ratelimited(cdns->dev,
1000 "Device0 detected after clearing status, iteration %d\n",
1002 slave_intstat = CDNS_MCP_SLAVE_INTSTAT_ATTACHED;
1005 dev_err_ratelimited(cdns->dev,
1006 "Device0 detected after %d iterations\n",
1011 /* clear and unmask Slave interrupt now */
1012 cdns_writel(cdns, CDNS_MCP_INTSTAT, CDNS_MCP_INT_SLAVE_MASK);
1013 cdns_updatel(cdns, CDNS_MCP_INTMASK,
1014 CDNS_MCP_INT_SLAVE_MASK, CDNS_MCP_INT_SLAVE_MASK);
1018 /* paranoia check to make sure self-cleared bits are indeed cleared */
1019 void sdw_cdns_check_self_clearing_bits(struct sdw_cdns *cdns, const char *string,
1020 bool initial_delay, int reset_iterations)
1023 u32 mcp_config_update;
1027 usleep_range(1000, 1500);
1029 mcp_control = cdns_readl(cdns, CDNS_MCP_CONTROL);
1031 /* the following bits should be cleared immediately */
1032 if (mcp_control & CDNS_MCP_CONTROL_CMD_RST)
1033 dev_err(cdns->dev, "%s failed: MCP_CONTROL_CMD_RST is not cleared\n", string);
1034 if (mcp_control & CDNS_MCP_CONTROL_SOFT_RST)
1035 dev_err(cdns->dev, "%s failed: MCP_CONTROL_SOFT_RST is not cleared\n", string);
1036 if (mcp_control & CDNS_MCP_CONTROL_SW_RST)
1037 dev_err(cdns->dev, "%s failed: MCP_CONTROL_SW_RST is not cleared\n", string);
1038 if (mcp_control & CDNS_MCP_CONTROL_CLK_STOP_CLR)
1039 dev_err(cdns->dev, "%s failed: MCP_CONTROL_CLK_STOP_CLR is not cleared\n", string);
1040 mcp_config_update = cdns_readl(cdns, CDNS_MCP_CONFIG_UPDATE);
1041 if (mcp_config_update & CDNS_MCP_CONFIG_UPDATE_BIT)
1042 dev_err(cdns->dev, "%s failed: MCP_CONFIG_UPDATE_BIT is not cleared\n", string);
1045 while (mcp_control & CDNS_MCP_CONTROL_HW_RST) {
1046 if (i == reset_iterations) {
1047 dev_err(cdns->dev, "%s failed: MCP_CONTROL_HW_RST is not cleared\n", string);
1051 dev_dbg(cdns->dev, "%s: MCP_CONTROL_HW_RST is not cleared at iteration %d\n", string, i);
1054 usleep_range(1000, 1500);
1055 mcp_control = cdns_readl(cdns, CDNS_MCP_CONTROL);
1059 EXPORT_SYMBOL(sdw_cdns_check_self_clearing_bits);
1066 * sdw_cdns_exit_reset() - Program reset parameters and start bus operations
1067 * @cdns: Cadence instance
1069 int sdw_cdns_exit_reset(struct sdw_cdns *cdns)
1071 /* keep reset delay unchanged to 4096 cycles */
1073 /* use hardware generated reset */
1074 cdns_updatel(cdns, CDNS_MCP_CONTROL,
1075 CDNS_MCP_CONTROL_HW_RST,
1076 CDNS_MCP_CONTROL_HW_RST);
1078 /* commit changes */
1079 cdns_updatel(cdns, CDNS_MCP_CONFIG_UPDATE,
1080 CDNS_MCP_CONFIG_UPDATE_BIT,
1081 CDNS_MCP_CONFIG_UPDATE_BIT);
1083 /* don't wait here */
1087 EXPORT_SYMBOL(sdw_cdns_exit_reset);
1090 * cdns_enable_slave_interrupts() - Enable SDW slave interrupts
1091 * @cdns: Cadence instance
1092 * @state: boolean for true/false
1094 static void cdns_enable_slave_interrupts(struct sdw_cdns *cdns, bool state)
1098 mask = cdns_readl(cdns, CDNS_MCP_INTMASK);
1100 mask |= CDNS_MCP_INT_SLAVE_MASK;
1102 mask &= ~CDNS_MCP_INT_SLAVE_MASK;
1104 cdns_writel(cdns, CDNS_MCP_INTMASK, mask);
1108 * sdw_cdns_enable_interrupt() - Enable SDW interrupts
1109 * @cdns: Cadence instance
1110 * @state: True if we are trying to enable interrupt.
1112 int sdw_cdns_enable_interrupt(struct sdw_cdns *cdns, bool state)
1114 u32 slave_intmask0 = 0;
1115 u32 slave_intmask1 = 0;
1121 slave_intmask0 = CDNS_MCP_SLAVE_INTMASK0_MASK;
1122 slave_intmask1 = CDNS_MCP_SLAVE_INTMASK1_MASK;
1124 /* enable detection of all slave state changes */
1125 mask = CDNS_MCP_INT_SLAVE_MASK;
1127 /* enable detection of bus issues */
1128 mask |= CDNS_MCP_INT_CTRL_CLASH | CDNS_MCP_INT_DATA_CLASH |
1129 CDNS_MCP_INT_PARITY;
1131 /* port interrupt limited to test modes for now */
1132 if (cdns->bus.params.m_data_mode != SDW_PORT_DATA_MODE_NORMAL)
1133 mask |= CDNS_MCP_INT_DPINT;
1135 /* enable detection of RX fifo level */
1136 mask |= CDNS_MCP_INT_RX_WL;
1139 * CDNS_MCP_INT_IRQ needs to be set otherwise all previous
1140 * settings are irrelevant
1142 mask |= CDNS_MCP_INT_IRQ;
1144 if (interrupt_mask) /* parameter override */
1145 mask = interrupt_mask;
1148 /* clear slave interrupt status before enabling interrupt */
1152 slave_state = cdns_readl(cdns, CDNS_MCP_SLAVE_INTSTAT0);
1153 cdns_writel(cdns, CDNS_MCP_SLAVE_INTSTAT0, slave_state);
1154 slave_state = cdns_readl(cdns, CDNS_MCP_SLAVE_INTSTAT1);
1155 cdns_writel(cdns, CDNS_MCP_SLAVE_INTSTAT1, slave_state);
1157 cdns->interrupt_enabled = state;
1160 * Complete any on-going status updates before updating masks,
1161 * and cancel queued status updates.
1163 * There could be a race with a new interrupt thrown before
1164 * the 3 mask updates below are complete, so in the interrupt
1165 * we use the 'interrupt_enabled' status to prevent new work
1166 * from being queued.
1169 cancel_work_sync(&cdns->work);
1171 cdns_writel(cdns, CDNS_MCP_SLAVE_INTMASK0, slave_intmask0);
1172 cdns_writel(cdns, CDNS_MCP_SLAVE_INTMASK1, slave_intmask1);
1173 cdns_writel(cdns, CDNS_MCP_INTMASK, mask);
1177 EXPORT_SYMBOL(sdw_cdns_enable_interrupt);
1179 static int cdns_allocate_pdi(struct sdw_cdns *cdns,
1180 struct sdw_cdns_pdi **stream,
1181 u32 num, u32 pdi_offset)
1183 struct sdw_cdns_pdi *pdi;
1189 pdi = devm_kcalloc(cdns->dev, num, sizeof(*pdi), GFP_KERNEL);
1193 for (i = 0; i < num; i++) {
1194 pdi[i].num = i + pdi_offset;
1202 * sdw_cdns_pdi_init() - PDI initialization routine
1204 * @cdns: Cadence instance
1205 * @config: Stream configurations
1207 int sdw_cdns_pdi_init(struct sdw_cdns *cdns,
1208 struct sdw_cdns_stream_config config)
1210 struct sdw_cdns_streams *stream;
1214 cdns->pcm.num_bd = config.pcm_bd;
1215 cdns->pcm.num_in = config.pcm_in;
1216 cdns->pcm.num_out = config.pcm_out;
1218 /* Allocate PDIs for PCMs */
1219 stream = &cdns->pcm;
1221 /* we allocate PDI0 and PDI1 which are used for Bulk */
1224 ret = cdns_allocate_pdi(cdns, &stream->bd,
1225 stream->num_bd, offset);
1229 offset += stream->num_bd;
1231 ret = cdns_allocate_pdi(cdns, &stream->in,
1232 stream->num_in, offset);
1236 offset += stream->num_in;
1238 ret = cdns_allocate_pdi(cdns, &stream->out,
1239 stream->num_out, offset);
1243 /* Update total number of PCM PDIs */
1244 stream->num_pdi = stream->num_bd + stream->num_in + stream->num_out;
1245 cdns->num_ports = stream->num_pdi;
1249 EXPORT_SYMBOL(sdw_cdns_pdi_init);
1251 static u32 cdns_set_initial_frame_shape(int n_rows, int n_cols)
1257 r = sdw_find_row_index(n_rows);
1258 c = sdw_find_col_index(n_cols);
1260 val = FIELD_PREP(CDNS_MCP_FRAME_SHAPE_ROW_MASK, r);
1261 val |= FIELD_PREP(CDNS_MCP_FRAME_SHAPE_COL_MASK, c);
1266 static void cdns_init_clock_ctrl(struct sdw_cdns *cdns)
1268 struct sdw_bus *bus = &cdns->bus;
1269 struct sdw_master_prop *prop = &bus->prop;
1274 /* Set clock divider */
1275 divider = (prop->mclk_freq / prop->max_clk_freq) - 1;
1277 cdns_updatel(cdns, CDNS_MCP_CLK_CTRL0,
1278 CDNS_MCP_CLK_MCLKD_MASK, divider);
1279 cdns_updatel(cdns, CDNS_MCP_CLK_CTRL1,
1280 CDNS_MCP_CLK_MCLKD_MASK, divider);
1283 * Frame shape changes after initialization have to be done
1284 * with the bank switch mechanism
1286 val = cdns_set_initial_frame_shape(prop->default_row,
1288 cdns_writel(cdns, CDNS_MCP_FRAME_SHAPE_INIT, val);
1290 /* Set SSP interval to default value */
1291 ssp_interval = prop->default_frame_rate / SDW_CADENCE_GSYNC_HZ;
1292 cdns_writel(cdns, CDNS_MCP_SSP_CTRL0, ssp_interval);
1293 cdns_writel(cdns, CDNS_MCP_SSP_CTRL1, ssp_interval);
1297 * sdw_cdns_init() - Cadence initialization
1298 * @cdns: Cadence instance
1300 int sdw_cdns_init(struct sdw_cdns *cdns)
1304 cdns_init_clock_ctrl(cdns);
1306 sdw_cdns_check_self_clearing_bits(cdns, __func__, false, 0);
1308 /* reset msg_count to default value of FIFOLEVEL */
1309 cdns->msg_count = cdns_readl(cdns, CDNS_MCP_FIFOLEVEL);
1311 /* flush command FIFOs */
1312 cdns_updatel(cdns, CDNS_MCP_CONTROL, CDNS_MCP_CONTROL_CMD_RST,
1313 CDNS_MCP_CONTROL_CMD_RST);
1315 /* Set cmd accept mode */
1316 cdns_updatel(cdns, CDNS_MCP_CONTROL, CDNS_MCP_CONTROL_CMD_ACCEPT,
1317 CDNS_MCP_CONTROL_CMD_ACCEPT);
1319 /* Configure mcp config */
1320 val = cdns_readl(cdns, CDNS_MCP_CONFIG);
1322 /* enable bus operations with clock and data */
1323 val &= ~CDNS_MCP_CONFIG_OP;
1324 val |= CDNS_MCP_CONFIG_OP_NORMAL;
1326 /* Set cmd mode for Tx and Rx cmds */
1327 val &= ~CDNS_MCP_CONFIG_CMD;
1329 /* Disable sniffer mode */
1330 val &= ~CDNS_MCP_CONFIG_SNIFFER;
1332 /* Disable auto bus release */
1333 val &= ~CDNS_MCP_CONFIG_BUS_REL;
1335 if (cdns->bus.multi_link)
1336 /* Set Multi-master mode to take gsync into account */
1337 val |= CDNS_MCP_CONFIG_MMASTER;
1339 /* leave frame delay to hardware default of 0x1F */
1341 /* leave command retry to hardware default of 0 */
1343 cdns_writel(cdns, CDNS_MCP_CONFIG, val);
1345 /* changes will be committed later */
1348 EXPORT_SYMBOL(sdw_cdns_init);
1350 int cdns_bus_conf(struct sdw_bus *bus, struct sdw_bus_params *params)
1352 struct sdw_master_prop *prop = &bus->prop;
1353 struct sdw_cdns *cdns = bus_to_cdns(bus);
1354 int mcp_clkctrl_off;
1357 if (!params->curr_dr_freq) {
1358 dev_err(cdns->dev, "NULL curr_dr_freq\n");
1362 divider = prop->mclk_freq * SDW_DOUBLE_RATE_FACTOR /
1363 params->curr_dr_freq;
1364 divider--; /* divider is 1/(N+1) */
1366 if (params->next_bank)
1367 mcp_clkctrl_off = CDNS_MCP_CLK_CTRL1;
1369 mcp_clkctrl_off = CDNS_MCP_CLK_CTRL0;
1371 cdns_updatel(cdns, mcp_clkctrl_off, CDNS_MCP_CLK_MCLKD_MASK, divider);
1375 EXPORT_SYMBOL(cdns_bus_conf);
1377 static int cdns_port_params(struct sdw_bus *bus,
1378 struct sdw_port_params *p_params, unsigned int bank)
1380 struct sdw_cdns *cdns = bus_to_cdns(bus);
1381 int dpn_config_off_source;
1382 int dpn_config_off_target;
1383 int target_num = p_params->num;
1384 int source_num = p_params->num;
1385 bool override = false;
1388 if (target_num == cdns->pdi_loopback_target &&
1389 cdns->pdi_loopback_source != -1) {
1390 source_num = cdns->pdi_loopback_source;
1395 dpn_config_off_source = CDNS_DPN_B1_CONFIG(source_num);
1396 dpn_config_off_target = CDNS_DPN_B1_CONFIG(target_num);
1398 dpn_config_off_source = CDNS_DPN_B0_CONFIG(source_num);
1399 dpn_config_off_target = CDNS_DPN_B0_CONFIG(target_num);
1402 dpn_config = cdns_readl(cdns, dpn_config_off_source);
1404 /* use port params if there is no loopback, otherwise use source as is */
1406 u32p_replace_bits(&dpn_config, p_params->bps - 1, CDNS_DPN_CONFIG_WL);
1407 u32p_replace_bits(&dpn_config, p_params->flow_mode, CDNS_DPN_CONFIG_PORT_FLOW);
1408 u32p_replace_bits(&dpn_config, p_params->data_mode, CDNS_DPN_CONFIG_PORT_DAT);
1411 cdns_writel(cdns, dpn_config_off_target, dpn_config);
1416 static int cdns_transport_params(struct sdw_bus *bus,
1417 struct sdw_transport_params *t_params,
1418 enum sdw_reg_bank bank)
1420 struct sdw_cdns *cdns = bus_to_cdns(bus);
1422 int dpn_config_off_source;
1423 int dpn_config_off_target;
1425 int dpn_hctrl_off_source;
1426 int dpn_hctrl_off_target;
1428 int dpn_offsetctrl_off_source;
1429 int dpn_offsetctrl_off_target;
1431 int dpn_samplectrl_off_source;
1432 int dpn_samplectrl_off_target;
1433 int source_num = t_params->port_num;
1434 int target_num = t_params->port_num;
1435 bool override = false;
1437 if (target_num == cdns->pdi_loopback_target &&
1438 cdns->pdi_loopback_source != -1) {
1439 source_num = cdns->pdi_loopback_source;
1444 * Note: Only full data port is supported on the Master side for
1445 * both PCM and PDM ports.
1449 dpn_config_off_source = CDNS_DPN_B1_CONFIG(source_num);
1450 dpn_hctrl_off_source = CDNS_DPN_B1_HCTRL(source_num);
1451 dpn_offsetctrl_off_source = CDNS_DPN_B1_OFFSET_CTRL(source_num);
1452 dpn_samplectrl_off_source = CDNS_DPN_B1_SAMPLE_CTRL(source_num);
1454 dpn_config_off_target = CDNS_DPN_B1_CONFIG(target_num);
1455 dpn_hctrl_off_target = CDNS_DPN_B1_HCTRL(target_num);
1456 dpn_offsetctrl_off_target = CDNS_DPN_B1_OFFSET_CTRL(target_num);
1457 dpn_samplectrl_off_target = CDNS_DPN_B1_SAMPLE_CTRL(target_num);
1460 dpn_config_off_source = CDNS_DPN_B0_CONFIG(source_num);
1461 dpn_hctrl_off_source = CDNS_DPN_B0_HCTRL(source_num);
1462 dpn_offsetctrl_off_source = CDNS_DPN_B0_OFFSET_CTRL(source_num);
1463 dpn_samplectrl_off_source = CDNS_DPN_B0_SAMPLE_CTRL(source_num);
1465 dpn_config_off_target = CDNS_DPN_B0_CONFIG(target_num);
1466 dpn_hctrl_off_target = CDNS_DPN_B0_HCTRL(target_num);
1467 dpn_offsetctrl_off_target = CDNS_DPN_B0_OFFSET_CTRL(target_num);
1468 dpn_samplectrl_off_target = CDNS_DPN_B0_SAMPLE_CTRL(target_num);
1471 dpn_config = cdns_readl(cdns, dpn_config_off_source);
1473 u32p_replace_bits(&dpn_config, t_params->blk_grp_ctrl, CDNS_DPN_CONFIG_BGC);
1474 u32p_replace_bits(&dpn_config, t_params->blk_pkg_mode, CDNS_DPN_CONFIG_BPM);
1476 cdns_writel(cdns, dpn_config_off_target, dpn_config);
1480 u32p_replace_bits(&dpn_offsetctrl, t_params->offset1, CDNS_DPN_OFFSET_CTRL_1);
1481 u32p_replace_bits(&dpn_offsetctrl, t_params->offset2, CDNS_DPN_OFFSET_CTRL_2);
1483 dpn_offsetctrl = cdns_readl(cdns, dpn_offsetctrl_off_source);
1485 cdns_writel(cdns, dpn_offsetctrl_off_target, dpn_offsetctrl);
1489 u32p_replace_bits(&dpn_hctrl, t_params->hstart, CDNS_DPN_HCTRL_HSTART);
1490 u32p_replace_bits(&dpn_hctrl, t_params->hstop, CDNS_DPN_HCTRL_HSTOP);
1491 u32p_replace_bits(&dpn_hctrl, t_params->lane_ctrl, CDNS_DPN_HCTRL_LCTRL);
1493 dpn_hctrl = cdns_readl(cdns, dpn_hctrl_off_source);
1495 cdns_writel(cdns, dpn_hctrl_off_target, dpn_hctrl);
1498 dpn_samplectrl = t_params->sample_interval - 1;
1500 dpn_samplectrl = cdns_readl(cdns, dpn_samplectrl_off_source);
1501 cdns_writel(cdns, dpn_samplectrl_off_target, dpn_samplectrl);
1506 static int cdns_port_enable(struct sdw_bus *bus,
1507 struct sdw_enable_ch *enable_ch, unsigned int bank)
1509 struct sdw_cdns *cdns = bus_to_cdns(bus);
1510 int dpn_chnen_off, ch_mask;
1513 dpn_chnen_off = CDNS_DPN_B1_CH_EN(enable_ch->port_num);
1515 dpn_chnen_off = CDNS_DPN_B0_CH_EN(enable_ch->port_num);
1517 ch_mask = enable_ch->ch_mask * enable_ch->enable;
1518 cdns_writel(cdns, dpn_chnen_off, ch_mask);
1523 static const struct sdw_master_port_ops cdns_port_ops = {
1524 .dpn_set_port_params = cdns_port_params,
1525 .dpn_set_port_transport_params = cdns_transport_params,
1526 .dpn_port_enable_ch = cdns_port_enable,
1530 * sdw_cdns_is_clock_stop: Check clock status
1532 * @cdns: Cadence instance
1534 bool sdw_cdns_is_clock_stop(struct sdw_cdns *cdns)
1536 return !!(cdns_readl(cdns, CDNS_MCP_STAT) & CDNS_MCP_STAT_CLK_STOP);
1538 EXPORT_SYMBOL(sdw_cdns_is_clock_stop);
1541 * sdw_cdns_clock_stop: Cadence clock stop configuration routine
1543 * @cdns: Cadence instance
1544 * @block_wake: prevent wakes if required by the platform
1546 int sdw_cdns_clock_stop(struct sdw_cdns *cdns, bool block_wake)
1548 bool slave_present = false;
1549 struct sdw_slave *slave;
1552 sdw_cdns_check_self_clearing_bits(cdns, __func__, false, 0);
1554 /* Check suspend status */
1555 if (sdw_cdns_is_clock_stop(cdns)) {
1556 dev_dbg(cdns->dev, "Clock is already stopped\n");
1561 * Before entering clock stop we mask the Slave
1562 * interrupts. This helps avoid having to deal with e.g. a
1563 * Slave becoming UNATTACHED while the clock is being stopped
1565 cdns_enable_slave_interrupts(cdns, false);
1568 * For specific platforms, it is required to be able to put
1569 * master into a state in which it ignores wake-up trials
1570 * in clock stop state
1573 cdns_updatel(cdns, CDNS_MCP_CONTROL,
1574 CDNS_MCP_CONTROL_BLOCK_WAKEUP,
1575 CDNS_MCP_CONTROL_BLOCK_WAKEUP);
1577 list_for_each_entry(slave, &cdns->bus.slaves, node) {
1578 if (slave->status == SDW_SLAVE_ATTACHED ||
1579 slave->status == SDW_SLAVE_ALERT) {
1580 slave_present = true;
1585 /* commit changes */
1586 ret = cdns_config_update(cdns);
1588 dev_err(cdns->dev, "%s: config_update failed\n", __func__);
1592 /* Prepare slaves for clock stop */
1593 if (slave_present) {
1594 ret = sdw_bus_prep_clk_stop(&cdns->bus);
1595 if (ret < 0 && ret != -ENODATA) {
1596 dev_err(cdns->dev, "prepare clock stop failed %d\n", ret);
1602 * Enter clock stop mode and only report errors if there are
1603 * Slave devices present (ALERT or ATTACHED)
1605 ret = sdw_bus_clk_stop(&cdns->bus);
1606 if (ret < 0 && slave_present && ret != -ENODATA) {
1607 dev_err(cdns->dev, "bus clock stop failed %d\n", ret);
1611 ret = cdns_set_wait(cdns, CDNS_MCP_STAT,
1612 CDNS_MCP_STAT_CLK_STOP,
1613 CDNS_MCP_STAT_CLK_STOP);
1615 dev_err(cdns->dev, "Clock stop failed %d\n", ret);
1619 EXPORT_SYMBOL(sdw_cdns_clock_stop);
1622 * sdw_cdns_clock_restart: Cadence PM clock restart configuration routine
1624 * @cdns: Cadence instance
1625 * @bus_reset: context may be lost while in low power modes and the bus
1626 * may require a Severe Reset and re-enumeration after a wake.
1628 int sdw_cdns_clock_restart(struct sdw_cdns *cdns, bool bus_reset)
1632 /* unmask Slave interrupts that were masked when stopping the clock */
1633 cdns_enable_slave_interrupts(cdns, true);
1635 ret = cdns_clear_bit(cdns, CDNS_MCP_CONTROL,
1636 CDNS_MCP_CONTROL_CLK_STOP_CLR);
1638 dev_err(cdns->dev, "Couldn't exit from clock stop\n");
1642 ret = cdns_set_wait(cdns, CDNS_MCP_STAT, CDNS_MCP_STAT_CLK_STOP, 0);
1644 dev_err(cdns->dev, "clock stop exit failed %d\n", ret);
1648 cdns_updatel(cdns, CDNS_MCP_CONTROL,
1649 CDNS_MCP_CONTROL_BLOCK_WAKEUP, 0);
1651 cdns_updatel(cdns, CDNS_MCP_CONTROL, CDNS_MCP_CONTROL_CMD_ACCEPT,
1652 CDNS_MCP_CONTROL_CMD_ACCEPT);
1656 /* enable bus operations with clock and data */
1657 cdns_updatel(cdns, CDNS_MCP_CONFIG,
1659 CDNS_MCP_CONFIG_OP_NORMAL);
1661 ret = cdns_config_update(cdns);
1663 dev_err(cdns->dev, "%s: config_update failed\n", __func__);
1667 ret = sdw_bus_exit_clk_stop(&cdns->bus);
1669 dev_err(cdns->dev, "bus failed to exit clock stop %d\n", ret);
1674 EXPORT_SYMBOL(sdw_cdns_clock_restart);
1677 * sdw_cdns_probe() - Cadence probe routine
1678 * @cdns: Cadence instance
1680 int sdw_cdns_probe(struct sdw_cdns *cdns)
1682 init_completion(&cdns->tx_complete);
1683 cdns->bus.port_ops = &cdns_port_ops;
1685 INIT_WORK(&cdns->work, cdns_update_slave_status_work);
1688 EXPORT_SYMBOL(sdw_cdns_probe);
1690 int cdns_set_sdw_stream(struct snd_soc_dai *dai,
1691 void *stream, int direction)
1693 struct sdw_cdns *cdns = snd_soc_dai_get_drvdata(dai);
1694 struct sdw_cdns_dma_data *dma;
1697 /* first paranoia check */
1698 if (direction == SNDRV_PCM_STREAM_PLAYBACK)
1699 dma = dai->playback_dma_data;
1701 dma = dai->capture_dma_data;
1705 "dma_data already allocated for dai %s\n",
1710 /* allocate and set dma info */
1711 dma = kzalloc(sizeof(*dma), GFP_KERNEL);
1715 dma->stream_type = SDW_STREAM_PCM;
1717 dma->bus = &cdns->bus;
1718 dma->link_id = cdns->instance;
1720 dma->stream = stream;
1722 if (direction == SNDRV_PCM_STREAM_PLAYBACK)
1723 dai->playback_dma_data = dma;
1725 dai->capture_dma_data = dma;
1727 /* for NULL stream we release allocated dma_data */
1728 if (direction == SNDRV_PCM_STREAM_PLAYBACK) {
1729 kfree(dai->playback_dma_data);
1730 dai->playback_dma_data = NULL;
1732 kfree(dai->capture_dma_data);
1733 dai->capture_dma_data = NULL;
1738 EXPORT_SYMBOL(cdns_set_sdw_stream);
1741 * cdns_find_pdi() - Find a free PDI
1743 * @cdns: Cadence instance
1744 * @offset: Starting offset
1745 * @num: Number of PDIs
1746 * @pdi: PDI instances
1749 * Find a PDI for a given PDI array. The PDI num and dai_id are
1750 * expected to match, return NULL otherwise.
1752 static struct sdw_cdns_pdi *cdns_find_pdi(struct sdw_cdns *cdns,
1753 unsigned int offset,
1755 struct sdw_cdns_pdi *pdi,
1760 for (i = offset; i < offset + num; i++)
1761 if (pdi[i].num == dai_id)
1768 * sdw_cdns_config_stream: Configure a stream
1770 * @cdns: Cadence instance
1771 * @ch: Channel count
1772 * @dir: Data direction
1773 * @pdi: PDI to be used
1775 void sdw_cdns_config_stream(struct sdw_cdns *cdns,
1776 u32 ch, u32 dir, struct sdw_cdns_pdi *pdi)
1778 u32 offset, val = 0;
1780 if (dir == SDW_DATA_DIR_RX) {
1781 val = CDNS_PORTCTRL_DIRN;
1783 if (cdns->bus.params.m_data_mode != SDW_PORT_DATA_MODE_NORMAL)
1784 val |= CDNS_PORTCTRL_TEST_FAILED;
1786 offset = CDNS_PORTCTRL + pdi->num * CDNS_PORT_OFFSET;
1787 cdns_updatel(cdns, offset,
1788 CDNS_PORTCTRL_DIRN | CDNS_PORTCTRL_TEST_FAILED,
1792 val |= CDNS_PDI_CONFIG_SOFT_RESET;
1793 val |= FIELD_PREP(CDNS_PDI_CONFIG_CHANNEL, (1 << ch) - 1);
1794 cdns_writel(cdns, CDNS_PDI_CONFIG(pdi->num), val);
1796 EXPORT_SYMBOL(sdw_cdns_config_stream);
1799 * sdw_cdns_alloc_pdi() - Allocate a PDI
1801 * @cdns: Cadence instance
1802 * @stream: Stream to be allocated
1803 * @ch: Channel count
1804 * @dir: Data direction
1807 struct sdw_cdns_pdi *sdw_cdns_alloc_pdi(struct sdw_cdns *cdns,
1808 struct sdw_cdns_streams *stream,
1809 u32 ch, u32 dir, int dai_id)
1811 struct sdw_cdns_pdi *pdi = NULL;
1813 if (dir == SDW_DATA_DIR_RX)
1814 pdi = cdns_find_pdi(cdns, 0, stream->num_in, stream->in,
1817 pdi = cdns_find_pdi(cdns, 0, stream->num_out, stream->out,
1820 /* check if we found a PDI, else find in bi-directional */
1822 pdi = cdns_find_pdi(cdns, 2, stream->num_bd, stream->bd,
1827 pdi->h_ch_num = ch - 1;
1834 EXPORT_SYMBOL(sdw_cdns_alloc_pdi);
1836 MODULE_LICENSE("Dual BSD/GPL");
1837 MODULE_DESCRIPTION("Cadence Soundwire Library");