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
30 #define CDNS_MCP_CONFIG_BUS_REL BIT(6)
32 #define CDNS_IP_MCP_CONFIG 0x0 /* IP offset added at run-time */
34 #define CDNS_IP_MCP_CONFIG_MCMD_RETRY GENMASK(27, 24)
35 #define CDNS_IP_MCP_CONFIG_MPREQ_DELAY GENMASK(20, 16)
36 #define CDNS_IP_MCP_CONFIG_MMASTER BIT(7)
37 #define CDNS_IP_MCP_CONFIG_SNIFFER BIT(5)
38 #define CDNS_IP_MCP_CONFIG_CMD BIT(3)
39 #define CDNS_IP_MCP_CONFIG_OP GENMASK(2, 0)
40 #define CDNS_IP_MCP_CONFIG_OP_NORMAL 0
42 #define CDNS_MCP_CONTROL 0x4
44 #define CDNS_MCP_CONTROL_CMD_RST BIT(7)
45 #define CDNS_MCP_CONTROL_SOFT_RST BIT(6)
46 #define CDNS_MCP_CONTROL_HW_RST BIT(4)
47 #define CDNS_MCP_CONTROL_CLK_STOP_CLR BIT(2)
49 #define CDNS_IP_MCP_CONTROL 0x4 /* IP offset added at run-time */
51 #define CDNS_IP_MCP_CONTROL_RST_DELAY GENMASK(10, 8)
52 #define CDNS_IP_MCP_CONTROL_SW_RST BIT(5)
53 #define CDNS_IP_MCP_CONTROL_CLK_PAUSE BIT(3)
54 #define CDNS_IP_MCP_CONTROL_CMD_ACCEPT BIT(1)
55 #define CDNS_IP_MCP_CONTROL_BLOCK_WAKEUP BIT(0)
57 #define CDNS_IP_MCP_CMDCTRL 0x8 /* IP offset added at run-time */
59 #define CDNS_IP_MCP_CMDCTRL_INSERT_PARITY_ERR BIT(2)
61 #define CDNS_MCP_SSPSTAT 0xC
62 #define CDNS_MCP_FRAME_SHAPE 0x10
63 #define CDNS_MCP_FRAME_SHAPE_INIT 0x14
64 #define CDNS_MCP_FRAME_SHAPE_COL_MASK GENMASK(2, 0)
65 #define CDNS_MCP_FRAME_SHAPE_ROW_MASK GENMASK(7, 3)
67 #define CDNS_MCP_CONFIG_UPDATE 0x18
68 #define CDNS_MCP_CONFIG_UPDATE_BIT BIT(0)
70 #define CDNS_MCP_PHYCTRL 0x1C
71 #define CDNS_MCP_SSP_CTRL0 0x20
72 #define CDNS_MCP_SSP_CTRL1 0x28
73 #define CDNS_MCP_CLK_CTRL0 0x30
74 #define CDNS_MCP_CLK_CTRL1 0x38
75 #define CDNS_MCP_CLK_MCLKD_MASK GENMASK(7, 0)
77 #define CDNS_MCP_STAT 0x40
79 #define CDNS_MCP_STAT_ACTIVE_BANK BIT(20)
80 #define CDNS_MCP_STAT_CLK_STOP BIT(16)
82 #define CDNS_MCP_INTSTAT 0x44
83 #define CDNS_MCP_INTMASK 0x48
85 #define CDNS_MCP_INT_IRQ BIT(31)
86 #define CDNS_MCP_INT_RESERVED1 GENMASK(30, 17)
87 #define CDNS_MCP_INT_WAKEUP BIT(16)
88 #define CDNS_MCP_INT_SLAVE_RSVD BIT(15)
89 #define CDNS_MCP_INT_SLAVE_ALERT BIT(14)
90 #define CDNS_MCP_INT_SLAVE_ATTACH BIT(13)
91 #define CDNS_MCP_INT_SLAVE_NATTACH BIT(12)
92 #define CDNS_MCP_INT_SLAVE_MASK GENMASK(15, 12)
93 #define CDNS_MCP_INT_DPINT BIT(11)
94 #define CDNS_MCP_INT_CTRL_CLASH BIT(10)
95 #define CDNS_MCP_INT_DATA_CLASH BIT(9)
96 #define CDNS_MCP_INT_PARITY BIT(8)
97 #define CDNS_MCP_INT_CMD_ERR BIT(7)
98 #define CDNS_MCP_INT_RESERVED2 GENMASK(6, 4)
99 #define CDNS_MCP_INT_RX_NE BIT(3)
100 #define CDNS_MCP_INT_RX_WL BIT(2)
101 #define CDNS_MCP_INT_TXE BIT(1)
102 #define CDNS_MCP_INT_TXF BIT(0)
103 #define CDNS_MCP_INT_RESERVED (CDNS_MCP_INT_RESERVED1 | CDNS_MCP_INT_RESERVED2)
105 #define CDNS_MCP_INTSET 0x4C
107 #define CDNS_MCP_SLAVE_STAT 0x50
108 #define CDNS_MCP_SLAVE_STAT_MASK GENMASK(1, 0)
110 #define CDNS_MCP_SLAVE_INTSTAT0 0x54
111 #define CDNS_MCP_SLAVE_INTSTAT1 0x58
112 #define CDNS_MCP_SLAVE_INTSTAT_NPRESENT BIT(0)
113 #define CDNS_MCP_SLAVE_INTSTAT_ATTACHED BIT(1)
114 #define CDNS_MCP_SLAVE_INTSTAT_ALERT BIT(2)
115 #define CDNS_MCP_SLAVE_INTSTAT_RESERVED BIT(3)
116 #define CDNS_MCP_SLAVE_STATUS_BITS GENMASK(3, 0)
117 #define CDNS_MCP_SLAVE_STATUS_NUM 4
119 #define CDNS_MCP_SLAVE_INTMASK0 0x5C
120 #define CDNS_MCP_SLAVE_INTMASK1 0x60
122 #define CDNS_MCP_SLAVE_INTMASK0_MASK GENMASK(31, 0)
123 #define CDNS_MCP_SLAVE_INTMASK1_MASK GENMASK(15, 0)
125 #define CDNS_MCP_PORT_INTSTAT 0x64
126 #define CDNS_MCP_PDI_STAT 0x6C
128 #define CDNS_MCP_FIFOLEVEL 0x78
129 #define CDNS_MCP_FIFOSTAT 0x7C
130 #define CDNS_MCP_RX_FIFO_AVAIL GENMASK(5, 0)
132 #define CDNS_IP_MCP_CMD_BASE 0x80 /* IP offset added at run-time */
133 #define CDNS_IP_MCP_RESP_BASE 0x80 /* IP offset added at run-time */
134 /* FIFO can hold 8 commands */
135 #define CDNS_MCP_CMD_LEN 8
136 #define CDNS_MCP_CMD_WORD_LEN 0x4
138 #define CDNS_MCP_CMD_SSP_TAG BIT(31)
139 #define CDNS_MCP_CMD_COMMAND GENMASK(30, 28)
140 #define CDNS_MCP_CMD_DEV_ADDR GENMASK(27, 24)
141 #define CDNS_MCP_CMD_REG_ADDR GENMASK(23, 8)
142 #define CDNS_MCP_CMD_REG_DATA GENMASK(7, 0)
144 #define CDNS_MCP_CMD_READ 2
145 #define CDNS_MCP_CMD_WRITE 3
147 #define CDNS_MCP_RESP_RDATA GENMASK(15, 8)
148 #define CDNS_MCP_RESP_ACK BIT(0)
149 #define CDNS_MCP_RESP_NACK BIT(1)
151 #define CDNS_DP_SIZE 128
153 #define CDNS_DPN_B0_CONFIG(n) (0x100 + CDNS_DP_SIZE * (n))
154 #define CDNS_DPN_B0_CH_EN(n) (0x104 + CDNS_DP_SIZE * (n))
155 #define CDNS_DPN_B0_SAMPLE_CTRL(n) (0x108 + CDNS_DP_SIZE * (n))
156 #define CDNS_DPN_B0_OFFSET_CTRL(n) (0x10C + CDNS_DP_SIZE * (n))
157 #define CDNS_DPN_B0_HCTRL(n) (0x110 + CDNS_DP_SIZE * (n))
158 #define CDNS_DPN_B0_ASYNC_CTRL(n) (0x114 + CDNS_DP_SIZE * (n))
160 #define CDNS_DPN_B1_CONFIG(n) (0x118 + CDNS_DP_SIZE * (n))
161 #define CDNS_DPN_B1_CH_EN(n) (0x11C + CDNS_DP_SIZE * (n))
162 #define CDNS_DPN_B1_SAMPLE_CTRL(n) (0x120 + CDNS_DP_SIZE * (n))
163 #define CDNS_DPN_B1_OFFSET_CTRL(n) (0x124 + CDNS_DP_SIZE * (n))
164 #define CDNS_DPN_B1_HCTRL(n) (0x128 + CDNS_DP_SIZE * (n))
165 #define CDNS_DPN_B1_ASYNC_CTRL(n) (0x12C + CDNS_DP_SIZE * (n))
167 #define CDNS_DPN_CONFIG_BPM BIT(18)
168 #define CDNS_DPN_CONFIG_BGC GENMASK(17, 16)
169 #define CDNS_DPN_CONFIG_WL GENMASK(12, 8)
170 #define CDNS_DPN_CONFIG_PORT_DAT GENMASK(3, 2)
171 #define CDNS_DPN_CONFIG_PORT_FLOW GENMASK(1, 0)
173 #define CDNS_DPN_SAMPLE_CTRL_SI GENMASK(15, 0)
175 #define CDNS_DPN_OFFSET_CTRL_1 GENMASK(7, 0)
176 #define CDNS_DPN_OFFSET_CTRL_2 GENMASK(15, 8)
178 #define CDNS_DPN_HCTRL_HSTOP GENMASK(3, 0)
179 #define CDNS_DPN_HCTRL_HSTART GENMASK(7, 4)
180 #define CDNS_DPN_HCTRL_LCTRL GENMASK(10, 8)
182 #define CDNS_PORTCTRL 0x130
183 #define CDNS_PORTCTRL_TEST_FAILED BIT(1)
184 #define CDNS_PORTCTRL_DIRN BIT(7)
185 #define CDNS_PORTCTRL_BANK_INVERT BIT(8)
187 #define CDNS_PORT_OFFSET 0x80
189 #define CDNS_PDI_CONFIG(n) (0x1100 + (n) * 16)
191 #define CDNS_PDI_CONFIG_SOFT_RESET BIT(24)
192 #define CDNS_PDI_CONFIG_CHANNEL GENMASK(15, 8)
193 #define CDNS_PDI_CONFIG_PORT GENMASK(4, 0)
195 /* Driver defaults */
196 #define CDNS_TX_TIMEOUT 500
198 #define CDNS_SCP_RX_FIFOLEVEL 0x2
201 * register accessor helpers
203 static inline u32 cdns_readl(struct sdw_cdns *cdns, int offset)
205 return readl(cdns->registers + offset);
208 static inline void cdns_writel(struct sdw_cdns *cdns, int offset, u32 value)
210 writel(value, cdns->registers + offset);
213 static inline u32 cdns_ip_readl(struct sdw_cdns *cdns, int offset)
215 return cdns_readl(cdns, cdns->ip_offset + offset);
218 static inline void cdns_ip_writel(struct sdw_cdns *cdns, int offset, u32 value)
220 return cdns_writel(cdns, cdns->ip_offset + offset, value);
223 static inline void cdns_updatel(struct sdw_cdns *cdns,
224 int offset, u32 mask, u32 val)
228 tmp = cdns_readl(cdns, offset);
229 tmp = (tmp & ~mask) | val;
230 cdns_writel(cdns, offset, tmp);
233 static inline void cdns_ip_updatel(struct sdw_cdns *cdns,
234 int offset, u32 mask, u32 val)
236 cdns_updatel(cdns, cdns->ip_offset + offset, mask, val);
239 static int cdns_set_wait(struct sdw_cdns *cdns, int offset, u32 mask, u32 value)
244 /* Wait for bit to be set */
246 reg_read = readl(cdns->registers + offset);
247 if ((reg_read & mask) == value)
251 usleep_range(50, 100);
252 } while (timeout != 0);
257 static int cdns_clear_bit(struct sdw_cdns *cdns, int offset, u32 value)
259 writel(value, cdns->registers + offset);
261 /* Wait for bit to be self cleared */
262 return cdns_set_wait(cdns, offset, value, 0);
266 * all changes to the MCP_CONFIG, MCP_CONTROL, MCP_CMDCTRL and MCP_PHYCTRL
267 * need to be confirmed with a write to MCP_CONFIG_UPDATE
269 static int cdns_config_update(struct sdw_cdns *cdns)
273 if (sdw_cdns_is_clock_stop(cdns)) {
274 dev_err(cdns->dev, "Cannot program MCP_CONFIG_UPDATE in ClockStopMode\n");
278 ret = cdns_clear_bit(cdns, CDNS_MCP_CONFIG_UPDATE,
279 CDNS_MCP_CONFIG_UPDATE_BIT);
281 dev_err(cdns->dev, "Config update timedout\n");
289 #ifdef CONFIG_DEBUG_FS
291 #define RD_BUF (2 * PAGE_SIZE)
293 static ssize_t cdns_sprintf(struct sdw_cdns *cdns,
294 char *buf, size_t pos, unsigned int reg)
296 return scnprintf(buf + pos, RD_BUF - pos,
297 "%4x\t%8x\n", reg, cdns_readl(cdns, reg));
300 static int cdns_reg_show(struct seq_file *s, void *data)
302 struct sdw_cdns *cdns = s->private;
308 buf = kzalloc(RD_BUF, GFP_KERNEL);
312 ret = scnprintf(buf, RD_BUF, "Register Value\n");
313 ret += scnprintf(buf + ret, RD_BUF - ret, "\nMCP Registers\n");
314 /* 8 MCP registers */
315 for (i = CDNS_MCP_CONFIG; i <= CDNS_MCP_PHYCTRL; i += sizeof(u32))
316 ret += cdns_sprintf(cdns, buf, ret, i);
318 ret += scnprintf(buf + ret, RD_BUF - ret,
319 "\nStatus & Intr Registers\n");
320 /* 13 Status & Intr registers (offsets 0x70 and 0x74 not defined) */
321 for (i = CDNS_MCP_STAT; i <= CDNS_MCP_FIFOSTAT; i += sizeof(u32))
322 ret += cdns_sprintf(cdns, buf, ret, i);
324 ret += scnprintf(buf + ret, RD_BUF - ret,
325 "\nSSP & Clk ctrl Registers\n");
326 ret += cdns_sprintf(cdns, buf, ret, CDNS_MCP_SSP_CTRL0);
327 ret += cdns_sprintf(cdns, buf, ret, CDNS_MCP_SSP_CTRL1);
328 ret += cdns_sprintf(cdns, buf, ret, CDNS_MCP_CLK_CTRL0);
329 ret += cdns_sprintf(cdns, buf, ret, CDNS_MCP_CLK_CTRL1);
331 ret += scnprintf(buf + ret, RD_BUF - ret,
332 "\nDPn B0 Registers\n");
334 num_ports = cdns->num_ports;
336 for (i = 0; i < num_ports; i++) {
337 ret += scnprintf(buf + ret, RD_BUF - ret,
339 for (j = CDNS_DPN_B0_CONFIG(i);
340 j < CDNS_DPN_B0_ASYNC_CTRL(i); j += sizeof(u32))
341 ret += cdns_sprintf(cdns, buf, ret, j);
344 ret += scnprintf(buf + ret, RD_BUF - ret,
345 "\nDPn B1 Registers\n");
346 for (i = 0; i < num_ports; i++) {
347 ret += scnprintf(buf + ret, RD_BUF - ret,
350 for (j = CDNS_DPN_B1_CONFIG(i);
351 j < CDNS_DPN_B1_ASYNC_CTRL(i); j += sizeof(u32))
352 ret += cdns_sprintf(cdns, buf, ret, j);
355 ret += scnprintf(buf + ret, RD_BUF - ret,
356 "\nDPn Control Registers\n");
357 for (i = 0; i < num_ports; i++)
358 ret += cdns_sprintf(cdns, buf, ret,
359 CDNS_PORTCTRL + i * CDNS_PORT_OFFSET);
361 ret += scnprintf(buf + ret, RD_BUF - ret,
362 "\nPDIn Config Registers\n");
364 /* number of PDI and ports is interchangeable */
365 for (i = 0; i < num_ports; i++)
366 ret += cdns_sprintf(cdns, buf, ret, CDNS_PDI_CONFIG(i));
368 seq_printf(s, "%s", buf);
373 DEFINE_SHOW_ATTRIBUTE(cdns_reg);
375 static int cdns_hw_reset(void *data, u64 value)
377 struct sdw_cdns *cdns = data;
383 /* Userspace changed the hardware state behind the kernel's back */
384 add_taint(TAINT_USER, LOCKDEP_STILL_OK);
386 ret = sdw_cdns_exit_reset(cdns);
388 dev_dbg(cdns->dev, "link hw_reset done: %d\n", ret);
393 DEFINE_DEBUGFS_ATTRIBUTE(cdns_hw_reset_fops, NULL, cdns_hw_reset, "%llu\n");
395 static int cdns_parity_error_injection(void *data, u64 value)
397 struct sdw_cdns *cdns = data;
407 * Resume Master device. If this results in a bus reset, the
408 * Slave devices will re-attach and be re-enumerated.
410 ret = pm_runtime_resume_and_get(bus->dev);
411 if (ret < 0 && ret != -EACCES) {
412 dev_err_ratelimited(cdns->dev,
413 "pm_runtime_resume_and_get failed in %s, ret %d\n",
419 * wait long enough for Slave(s) to be in steady state. This
420 * does not need to be super precise.
425 * Take the bus lock here to make sure that any bus transactions
426 * will be queued while we inject a parity error on a dummy read
428 mutex_lock(&bus->bus_lock);
430 /* program hardware to inject parity error */
431 cdns_ip_updatel(cdns, CDNS_IP_MCP_CMDCTRL,
432 CDNS_IP_MCP_CMDCTRL_INSERT_PARITY_ERR,
433 CDNS_IP_MCP_CMDCTRL_INSERT_PARITY_ERR);
436 cdns_updatel(cdns, CDNS_MCP_CONFIG_UPDATE,
437 CDNS_MCP_CONFIG_UPDATE_BIT,
438 CDNS_MCP_CONFIG_UPDATE_BIT);
440 /* do a broadcast dummy read to avoid bus clashes */
441 ret = sdw_bread_no_pm_unlocked(&cdns->bus, 0xf, SDW_SCP_DEVID_0);
442 dev_info(cdns->dev, "parity error injection, read: %d\n", ret);
444 /* program hardware to disable parity error */
445 cdns_ip_updatel(cdns, CDNS_IP_MCP_CMDCTRL,
446 CDNS_IP_MCP_CMDCTRL_INSERT_PARITY_ERR,
450 cdns_updatel(cdns, CDNS_MCP_CONFIG_UPDATE,
451 CDNS_MCP_CONFIG_UPDATE_BIT,
452 CDNS_MCP_CONFIG_UPDATE_BIT);
454 /* Continue bus operation with parity error injection disabled */
455 mutex_unlock(&bus->bus_lock);
457 /* Userspace changed the hardware state behind the kernel's back */
458 add_taint(TAINT_USER, LOCKDEP_STILL_OK);
461 * allow Master device to enter pm_runtime suspend. This may
462 * also result in Slave devices suspending.
464 pm_runtime_mark_last_busy(bus->dev);
465 pm_runtime_put_autosuspend(bus->dev);
470 DEFINE_DEBUGFS_ATTRIBUTE(cdns_parity_error_fops, NULL,
471 cdns_parity_error_injection, "%llu\n");
473 static int cdns_set_pdi_loopback_source(void *data, u64 value)
475 struct sdw_cdns *cdns = data;
476 unsigned int pdi_out_num = cdns->pcm.num_bd + cdns->pcm.num_out;
478 if (value > pdi_out_num)
481 /* Userspace changed the hardware state behind the kernel's back */
482 add_taint(TAINT_USER, LOCKDEP_STILL_OK);
484 cdns->pdi_loopback_source = value;
488 DEFINE_DEBUGFS_ATTRIBUTE(cdns_pdi_loopback_source_fops, NULL, cdns_set_pdi_loopback_source, "%llu\n");
490 static int cdns_set_pdi_loopback_target(void *data, u64 value)
492 struct sdw_cdns *cdns = data;
493 unsigned int pdi_in_num = cdns->pcm.num_bd + cdns->pcm.num_in;
495 if (value > pdi_in_num)
498 /* Userspace changed the hardware state behind the kernel's back */
499 add_taint(TAINT_USER, LOCKDEP_STILL_OK);
501 cdns->pdi_loopback_target = value;
505 DEFINE_DEBUGFS_ATTRIBUTE(cdns_pdi_loopback_target_fops, NULL, cdns_set_pdi_loopback_target, "%llu\n");
508 * sdw_cdns_debugfs_init() - Cadence debugfs init
509 * @cdns: Cadence instance
510 * @root: debugfs root
512 void sdw_cdns_debugfs_init(struct sdw_cdns *cdns, struct dentry *root)
514 debugfs_create_file("cdns-registers", 0400, root, cdns, &cdns_reg_fops);
516 debugfs_create_file("cdns-hw-reset", 0200, root, cdns,
517 &cdns_hw_reset_fops);
519 debugfs_create_file("cdns-parity-error-injection", 0200, root, cdns,
520 &cdns_parity_error_fops);
522 cdns->pdi_loopback_source = -1;
523 cdns->pdi_loopback_target = -1;
525 debugfs_create_file("cdns-pdi-loopback-source", 0200, root, cdns,
526 &cdns_pdi_loopback_source_fops);
528 debugfs_create_file("cdns-pdi-loopback-target", 0200, root, cdns,
529 &cdns_pdi_loopback_target_fops);
532 EXPORT_SYMBOL_GPL(sdw_cdns_debugfs_init);
534 #endif /* CONFIG_DEBUG_FS */
539 static enum sdw_command_response
540 cdns_fill_msg_resp(struct sdw_cdns *cdns,
541 struct sdw_msg *msg, int count, int offset)
543 int nack = 0, no_ack = 0;
546 /* check message response */
547 for (i = 0; i < count; i++) {
548 if (!(cdns->response_buf[i] & CDNS_MCP_RESP_ACK)) {
550 dev_vdbg(cdns->dev, "Msg Ack not received, cmd %d\n", i);
552 if (cdns->response_buf[i] & CDNS_MCP_RESP_NACK) {
554 dev_err_ratelimited(cdns->dev, "Msg NACK received, cmd %d\n", i);
559 dev_err_ratelimited(cdns->dev, "Msg NACKed for Slave %d\n", msg->dev_num);
564 dev_dbg_ratelimited(cdns->dev, "Msg ignored for Slave %d\n", msg->dev_num);
565 return SDW_CMD_IGNORED;
568 if (msg->flags == SDW_MSG_FLAG_READ) {
570 for (i = 0; i < count; i++)
571 msg->buf[i + offset] = FIELD_GET(CDNS_MCP_RESP_RDATA,
572 cdns->response_buf[i]);
578 static void cdns_read_response(struct sdw_cdns *cdns)
580 u32 num_resp, cmd_base;
583 /* RX_FIFO_AVAIL can be 2 entries more than the FIFO size */
584 BUILD_BUG_ON(ARRAY_SIZE(cdns->response_buf) < CDNS_MCP_CMD_LEN + 2);
586 num_resp = cdns_readl(cdns, CDNS_MCP_FIFOSTAT);
587 num_resp &= CDNS_MCP_RX_FIFO_AVAIL;
588 if (num_resp > ARRAY_SIZE(cdns->response_buf)) {
589 dev_warn(cdns->dev, "RX AVAIL %d too long\n", num_resp);
590 num_resp = ARRAY_SIZE(cdns->response_buf);
593 cmd_base = CDNS_IP_MCP_CMD_BASE;
595 for (i = 0; i < num_resp; i++) {
596 cdns->response_buf[i] = cdns_ip_readl(cdns, cmd_base);
597 cmd_base += CDNS_MCP_CMD_WORD_LEN;
601 static enum sdw_command_response
602 _cdns_xfer_msg(struct sdw_cdns *cdns, struct sdw_msg *msg, int cmd,
603 int offset, int count, bool defer)
609 /* Program the watermark level for RX FIFO */
610 if (cdns->msg_count != count) {
611 cdns_writel(cdns, CDNS_MCP_FIFOLEVEL, count);
612 cdns->msg_count = count;
615 base = CDNS_IP_MCP_CMD_BASE;
616 addr = msg->addr + offset;
618 for (i = 0; i < count; i++) {
619 data = FIELD_PREP(CDNS_MCP_CMD_DEV_ADDR, msg->dev_num);
620 data |= FIELD_PREP(CDNS_MCP_CMD_COMMAND, cmd);
621 data |= FIELD_PREP(CDNS_MCP_CMD_REG_ADDR, addr);
624 if (msg->flags == SDW_MSG_FLAG_WRITE)
625 data |= msg->buf[i + offset];
627 data |= FIELD_PREP(CDNS_MCP_CMD_SSP_TAG, msg->ssp_sync);
628 cdns_ip_writel(cdns, base, data);
629 base += CDNS_MCP_CMD_WORD_LEN;
635 /* wait for timeout or response */
636 time = wait_for_completion_timeout(&cdns->tx_complete,
637 msecs_to_jiffies(CDNS_TX_TIMEOUT));
639 dev_err(cdns->dev, "IO transfer timed out, cmd %d device %d addr %x len %d\n",
640 cmd, msg->dev_num, msg->addr, msg->len);
643 /* Drain anything in the RX_FIFO */
644 cdns_read_response(cdns);
646 return SDW_CMD_TIMEOUT;
649 return cdns_fill_msg_resp(cdns, msg, count, offset);
652 static enum sdw_command_response
653 cdns_program_scp_addr(struct sdw_cdns *cdns, struct sdw_msg *msg)
655 int nack = 0, no_ack = 0;
660 /* Program the watermark level for RX FIFO */
661 if (cdns->msg_count != CDNS_SCP_RX_FIFOLEVEL) {
662 cdns_writel(cdns, CDNS_MCP_FIFOLEVEL, CDNS_SCP_RX_FIFOLEVEL);
663 cdns->msg_count = CDNS_SCP_RX_FIFOLEVEL;
666 data[0] = FIELD_PREP(CDNS_MCP_CMD_DEV_ADDR, msg->dev_num);
667 data[0] |= FIELD_PREP(CDNS_MCP_CMD_COMMAND, 0x3);
670 data[0] |= FIELD_PREP(CDNS_MCP_CMD_REG_ADDR, SDW_SCP_ADDRPAGE1);
671 data[1] |= FIELD_PREP(CDNS_MCP_CMD_REG_ADDR, SDW_SCP_ADDRPAGE2);
673 data[0] |= msg->addr_page1;
674 data[1] |= msg->addr_page2;
676 base = CDNS_IP_MCP_CMD_BASE;
677 cdns_ip_writel(cdns, base, data[0]);
678 base += CDNS_MCP_CMD_WORD_LEN;
679 cdns_ip_writel(cdns, base, data[1]);
681 time = wait_for_completion_timeout(&cdns->tx_complete,
682 msecs_to_jiffies(CDNS_TX_TIMEOUT));
684 dev_err(cdns->dev, "SCP Msg trf timed out\n");
686 return SDW_CMD_TIMEOUT;
689 /* check response the writes */
690 for (i = 0; i < 2; i++) {
691 if (!(cdns->response_buf[i] & CDNS_MCP_RESP_ACK)) {
693 dev_err(cdns->dev, "Program SCP Ack not received\n");
694 if (cdns->response_buf[i] & CDNS_MCP_RESP_NACK) {
696 dev_err(cdns->dev, "Program SCP NACK received\n");
701 /* For NACK, NO ack, don't return err if we are in Broadcast mode */
703 dev_err_ratelimited(cdns->dev,
704 "SCP_addrpage NACKed for Slave %d\n", msg->dev_num);
709 dev_dbg_ratelimited(cdns->dev,
710 "SCP_addrpage ignored for Slave %d\n", msg->dev_num);
711 return SDW_CMD_IGNORED;
717 static int cdns_prep_msg(struct sdw_cdns *cdns, struct sdw_msg *msg, int *cmd)
722 ret = cdns_program_scp_addr(cdns, msg);
729 switch (msg->flags) {
730 case SDW_MSG_FLAG_READ:
731 *cmd = CDNS_MCP_CMD_READ;
734 case SDW_MSG_FLAG_WRITE:
735 *cmd = CDNS_MCP_CMD_WRITE;
739 dev_err(cdns->dev, "Invalid msg cmd: %d\n", msg->flags);
746 enum sdw_command_response
747 cdns_xfer_msg(struct sdw_bus *bus, struct sdw_msg *msg)
749 struct sdw_cdns *cdns = bus_to_cdns(bus);
752 ret = cdns_prep_msg(cdns, msg, &cmd);
754 return SDW_CMD_FAIL_OTHER;
756 for (i = 0; i < msg->len / CDNS_MCP_CMD_LEN; i++) {
757 ret = _cdns_xfer_msg(cdns, msg, cmd, i * CDNS_MCP_CMD_LEN,
758 CDNS_MCP_CMD_LEN, false);
759 if (ret != SDW_CMD_OK)
763 if (!(msg->len % CDNS_MCP_CMD_LEN))
766 return _cdns_xfer_msg(cdns, msg, cmd, i * CDNS_MCP_CMD_LEN,
767 msg->len % CDNS_MCP_CMD_LEN, false);
769 EXPORT_SYMBOL(cdns_xfer_msg);
771 enum sdw_command_response
772 cdns_xfer_msg_defer(struct sdw_bus *bus)
774 struct sdw_cdns *cdns = bus_to_cdns(bus);
775 struct sdw_defer *defer = &bus->defer_msg;
776 struct sdw_msg *msg = defer->msg;
779 /* for defer only 1 message is supported */
783 ret = cdns_prep_msg(cdns, msg, &cmd);
785 return SDW_CMD_FAIL_OTHER;
787 return _cdns_xfer_msg(cdns, msg, cmd, 0, msg->len, true);
789 EXPORT_SYMBOL(cdns_xfer_msg_defer);
791 u32 cdns_read_ping_status(struct sdw_bus *bus)
793 struct sdw_cdns *cdns = bus_to_cdns(bus);
795 return cdns_readl(cdns, CDNS_MCP_SLAVE_STAT);
797 EXPORT_SYMBOL(cdns_read_ping_status);
803 static int cdns_update_slave_status(struct sdw_cdns *cdns,
806 enum sdw_slave_status status[SDW_MAX_DEVICES + 1];
807 bool is_slave = false;
812 memset(status, 0, sizeof(status));
814 for (i = 0; i <= SDW_MAX_DEVICES; i++) {
815 mask = (slave_intstat >> (i * CDNS_MCP_SLAVE_STATUS_NUM)) &
816 CDNS_MCP_SLAVE_STATUS_BITS;
823 if (mask & CDNS_MCP_SLAVE_INTSTAT_RESERVED) {
824 status[i] = SDW_SLAVE_RESERVED;
828 if (mask & CDNS_MCP_SLAVE_INTSTAT_ATTACHED) {
829 status[i] = SDW_SLAVE_ATTACHED;
833 if (mask & CDNS_MCP_SLAVE_INTSTAT_ALERT) {
834 status[i] = SDW_SLAVE_ALERT;
838 if (mask & CDNS_MCP_SLAVE_INTSTAT_NPRESENT) {
839 status[i] = SDW_SLAVE_UNATTACHED;
845 * check that there was a single reported Slave status and when
846 * there is not use the latest status extracted from PING commands
848 if (set_status != 1) {
849 val = cdns_readl(cdns, CDNS_MCP_SLAVE_STAT);
854 status[i] = SDW_SLAVE_UNATTACHED;
857 status[i] = SDW_SLAVE_ATTACHED;
860 status[i] = SDW_SLAVE_ALERT;
864 status[i] = SDW_SLAVE_RESERVED;
871 return sdw_handle_slave_status(&cdns->bus, status);
877 * sdw_cdns_irq() - Cadence interrupt handler
879 * @dev_id: irq context
881 irqreturn_t sdw_cdns_irq(int irq, void *dev_id)
883 struct sdw_cdns *cdns = dev_id;
886 /* Check if the link is up */
890 int_status = cdns_readl(cdns, CDNS_MCP_INTSTAT);
892 /* check for reserved values read as zero */
893 if (int_status & CDNS_MCP_INT_RESERVED)
896 if (!(int_status & CDNS_MCP_INT_IRQ))
899 if (int_status & CDNS_MCP_INT_RX_WL) {
900 struct sdw_bus *bus = &cdns->bus;
901 struct sdw_defer *defer = &bus->defer_msg;
903 cdns_read_response(cdns);
905 if (defer && defer->msg) {
906 cdns_fill_msg_resp(cdns, defer->msg,
908 complete(&defer->complete);
910 complete(&cdns->tx_complete);
914 if (int_status & CDNS_MCP_INT_PARITY) {
915 /* Parity error detected by Master */
916 dev_err_ratelimited(cdns->dev, "Parity error\n");
919 if (int_status & CDNS_MCP_INT_CTRL_CLASH) {
920 /* Slave is driving bit slot during control word */
921 dev_err_ratelimited(cdns->dev, "Bus clash for control word\n");
924 if (int_status & CDNS_MCP_INT_DATA_CLASH) {
926 * Multiple slaves trying to drive bit slot, or issue with
927 * ownership of data bits or Slave gone bonkers
929 dev_err_ratelimited(cdns->dev, "Bus clash for data word\n");
932 if (cdns->bus.params.m_data_mode != SDW_PORT_DATA_MODE_NORMAL &&
933 int_status & CDNS_MCP_INT_DPINT) {
936 /* just log which ports report an error */
937 port_intstat = cdns_readl(cdns, CDNS_MCP_PORT_INTSTAT);
938 dev_err_ratelimited(cdns->dev, "DP interrupt: PortIntStat %8x\n",
941 /* clear status w/ write1 */
942 cdns_writel(cdns, CDNS_MCP_PORT_INTSTAT, port_intstat);
945 if (int_status & CDNS_MCP_INT_SLAVE_MASK) {
946 /* Mask the Slave interrupt and wake thread */
947 cdns_updatel(cdns, CDNS_MCP_INTMASK,
948 CDNS_MCP_INT_SLAVE_MASK, 0);
950 int_status &= ~CDNS_MCP_INT_SLAVE_MASK;
953 * Deal with possible race condition between interrupt
954 * handling and disabling interrupts on suspend.
956 * If the master is in the process of disabling
957 * interrupts, don't schedule a workqueue
959 if (cdns->interrupt_enabled)
960 schedule_work(&cdns->work);
963 cdns_writel(cdns, CDNS_MCP_INTSTAT, int_status);
966 EXPORT_SYMBOL(sdw_cdns_irq);
969 * cdns_update_slave_status_work - update slave status in a work since we will need to handle
970 * other interrupts eg. CDNS_MCP_INT_RX_WL during the update slave
972 * @work: cdns worker thread
974 static void cdns_update_slave_status_work(struct work_struct *work)
976 struct sdw_cdns *cdns =
977 container_of(work, struct sdw_cdns, work);
984 * Clear main interrupt first so we don't lose any assertions
985 * that happen during this function.
987 cdns_writel(cdns, CDNS_MCP_INTSTAT, CDNS_MCP_INT_SLAVE_MASK);
989 slave0 = cdns_readl(cdns, CDNS_MCP_SLAVE_INTSTAT0);
990 slave1 = cdns_readl(cdns, CDNS_MCP_SLAVE_INTSTAT1);
993 * Clear the bits before handling so we don't lose any
994 * bits that re-assert.
996 cdns_writel(cdns, CDNS_MCP_SLAVE_INTSTAT0, slave0);
997 cdns_writel(cdns, CDNS_MCP_SLAVE_INTSTAT1, slave1);
999 /* combine the two status */
1000 slave_intstat = ((u64)slave1 << 32) | slave0;
1002 dev_dbg_ratelimited(cdns->dev, "Slave status change: 0x%llx\n", slave_intstat);
1005 cdns_update_slave_status(cdns, slave_intstat);
1008 * When there is more than one peripheral per link, it's
1009 * possible that a deviceB becomes attached after we deal with
1010 * the attachment of deviceA. Since the hardware does a
1011 * logical AND, the attachment of the second device does not
1012 * change the status seen by the driver.
1014 * In that case, clearing the registers above would result in
1015 * the deviceB never being detected - until a change of status
1016 * is observed on the bus.
1018 * To avoid this race condition, re-check if any device0 needs
1019 * attention with PING commands. There is no need to check for
1020 * ALERTS since they are not allowed until a non-zero
1021 * device_number is assigned.
1023 * Do not clear the INTSTAT0/1. While looping to enumerate devices on
1024 * #0 there could be status changes on other devices - these must
1025 * be kept in the INTSTAT so they can be handled when all #0 devices
1026 * have been handled.
1029 device0_status = cdns_readl(cdns, CDNS_MCP_SLAVE_STAT);
1030 device0_status &= 3;
1032 if (device0_status == SDW_SLAVE_ATTACHED) {
1033 if (retry_count++ < SDW_MAX_DEVICES) {
1034 dev_dbg_ratelimited(cdns->dev,
1035 "Device0 detected after clearing status, iteration %d\n",
1037 slave_intstat = CDNS_MCP_SLAVE_INTSTAT_ATTACHED;
1040 dev_err_ratelimited(cdns->dev,
1041 "Device0 detected after %d iterations\n",
1046 /* unmask Slave interrupt now */
1047 cdns_updatel(cdns, CDNS_MCP_INTMASK,
1048 CDNS_MCP_INT_SLAVE_MASK, CDNS_MCP_INT_SLAVE_MASK);
1052 /* paranoia check to make sure self-cleared bits are indeed cleared */
1053 void sdw_cdns_check_self_clearing_bits(struct sdw_cdns *cdns, const char *string,
1054 bool initial_delay, int reset_iterations)
1058 u32 mcp_config_update;
1062 usleep_range(1000, 1500);
1064 ip_mcp_control = cdns_ip_readl(cdns, CDNS_IP_MCP_CONTROL);
1066 /* the following bits should be cleared immediately */
1067 if (ip_mcp_control & CDNS_IP_MCP_CONTROL_SW_RST)
1068 dev_err(cdns->dev, "%s failed: IP_MCP_CONTROL_SW_RST is not cleared\n", string);
1070 mcp_control = cdns_readl(cdns, CDNS_MCP_CONTROL);
1072 /* the following bits should be cleared immediately */
1073 if (mcp_control & CDNS_MCP_CONTROL_CMD_RST)
1074 dev_err(cdns->dev, "%s failed: MCP_CONTROL_CMD_RST is not cleared\n", string);
1075 if (mcp_control & CDNS_MCP_CONTROL_SOFT_RST)
1076 dev_err(cdns->dev, "%s failed: MCP_CONTROL_SOFT_RST is not cleared\n", string);
1077 if (mcp_control & CDNS_MCP_CONTROL_CLK_STOP_CLR)
1078 dev_err(cdns->dev, "%s failed: MCP_CONTROL_CLK_STOP_CLR is not cleared\n", string);
1080 mcp_config_update = cdns_readl(cdns, CDNS_MCP_CONFIG_UPDATE);
1081 if (mcp_config_update & CDNS_MCP_CONFIG_UPDATE_BIT)
1082 dev_err(cdns->dev, "%s failed: MCP_CONFIG_UPDATE_BIT is not cleared\n", string);
1085 while (mcp_control & CDNS_MCP_CONTROL_HW_RST) {
1086 if (i == reset_iterations) {
1087 dev_err(cdns->dev, "%s failed: MCP_CONTROL_HW_RST is not cleared\n", string);
1091 dev_dbg(cdns->dev, "%s: MCP_CONTROL_HW_RST is not cleared at iteration %d\n", string, i);
1094 usleep_range(1000, 1500);
1095 mcp_control = cdns_readl(cdns, CDNS_MCP_CONTROL);
1099 EXPORT_SYMBOL(sdw_cdns_check_self_clearing_bits);
1106 * sdw_cdns_exit_reset() - Program reset parameters and start bus operations
1107 * @cdns: Cadence instance
1109 int sdw_cdns_exit_reset(struct sdw_cdns *cdns)
1111 /* keep reset delay unchanged to 4096 cycles */
1113 /* use hardware generated reset */
1114 cdns_updatel(cdns, CDNS_MCP_CONTROL,
1115 CDNS_MCP_CONTROL_HW_RST,
1116 CDNS_MCP_CONTROL_HW_RST);
1118 /* commit changes */
1119 cdns_updatel(cdns, CDNS_MCP_CONFIG_UPDATE,
1120 CDNS_MCP_CONFIG_UPDATE_BIT,
1121 CDNS_MCP_CONFIG_UPDATE_BIT);
1123 /* don't wait here */
1127 EXPORT_SYMBOL(sdw_cdns_exit_reset);
1130 * cdns_enable_slave_interrupts() - Enable SDW slave interrupts
1131 * @cdns: Cadence instance
1132 * @state: boolean for true/false
1134 static void cdns_enable_slave_interrupts(struct sdw_cdns *cdns, bool state)
1138 mask = cdns_readl(cdns, CDNS_MCP_INTMASK);
1140 mask |= CDNS_MCP_INT_SLAVE_MASK;
1142 mask &= ~CDNS_MCP_INT_SLAVE_MASK;
1144 cdns_writel(cdns, CDNS_MCP_INTMASK, mask);
1148 * sdw_cdns_enable_interrupt() - Enable SDW interrupts
1149 * @cdns: Cadence instance
1150 * @state: True if we are trying to enable interrupt.
1152 int sdw_cdns_enable_interrupt(struct sdw_cdns *cdns, bool state)
1154 u32 slave_intmask0 = 0;
1155 u32 slave_intmask1 = 0;
1161 slave_intmask0 = CDNS_MCP_SLAVE_INTMASK0_MASK;
1162 slave_intmask1 = CDNS_MCP_SLAVE_INTMASK1_MASK;
1164 /* enable detection of all slave state changes */
1165 mask = CDNS_MCP_INT_SLAVE_MASK;
1167 /* enable detection of bus issues */
1168 mask |= CDNS_MCP_INT_CTRL_CLASH | CDNS_MCP_INT_DATA_CLASH |
1169 CDNS_MCP_INT_PARITY;
1171 /* port interrupt limited to test modes for now */
1172 if (cdns->bus.params.m_data_mode != SDW_PORT_DATA_MODE_NORMAL)
1173 mask |= CDNS_MCP_INT_DPINT;
1175 /* enable detection of RX fifo level */
1176 mask |= CDNS_MCP_INT_RX_WL;
1179 * CDNS_MCP_INT_IRQ needs to be set otherwise all previous
1180 * settings are irrelevant
1182 mask |= CDNS_MCP_INT_IRQ;
1184 if (interrupt_mask) /* parameter override */
1185 mask = interrupt_mask;
1188 /* clear slave interrupt status before enabling interrupt */
1192 slave_state = cdns_readl(cdns, CDNS_MCP_SLAVE_INTSTAT0);
1193 cdns_writel(cdns, CDNS_MCP_SLAVE_INTSTAT0, slave_state);
1194 slave_state = cdns_readl(cdns, CDNS_MCP_SLAVE_INTSTAT1);
1195 cdns_writel(cdns, CDNS_MCP_SLAVE_INTSTAT1, slave_state);
1197 cdns->interrupt_enabled = state;
1200 * Complete any on-going status updates before updating masks,
1201 * and cancel queued status updates.
1203 * There could be a race with a new interrupt thrown before
1204 * the 3 mask updates below are complete, so in the interrupt
1205 * we use the 'interrupt_enabled' status to prevent new work
1206 * from being queued.
1209 cancel_work_sync(&cdns->work);
1211 cdns_writel(cdns, CDNS_MCP_SLAVE_INTMASK0, slave_intmask0);
1212 cdns_writel(cdns, CDNS_MCP_SLAVE_INTMASK1, slave_intmask1);
1213 cdns_writel(cdns, CDNS_MCP_INTMASK, mask);
1217 EXPORT_SYMBOL(sdw_cdns_enable_interrupt);
1219 static int cdns_allocate_pdi(struct sdw_cdns *cdns,
1220 struct sdw_cdns_pdi **stream,
1221 u32 num, u32 pdi_offset)
1223 struct sdw_cdns_pdi *pdi;
1229 pdi = devm_kcalloc(cdns->dev, num, sizeof(*pdi), GFP_KERNEL);
1233 for (i = 0; i < num; i++) {
1234 pdi[i].num = i + pdi_offset;
1242 * sdw_cdns_pdi_init() - PDI initialization routine
1244 * @cdns: Cadence instance
1245 * @config: Stream configurations
1247 int sdw_cdns_pdi_init(struct sdw_cdns *cdns,
1248 struct sdw_cdns_stream_config config)
1250 struct sdw_cdns_streams *stream;
1254 cdns->pcm.num_bd = config.pcm_bd;
1255 cdns->pcm.num_in = config.pcm_in;
1256 cdns->pcm.num_out = config.pcm_out;
1258 /* Allocate PDIs for PCMs */
1259 stream = &cdns->pcm;
1261 /* we allocate PDI0 and PDI1 which are used for Bulk */
1264 ret = cdns_allocate_pdi(cdns, &stream->bd,
1265 stream->num_bd, offset);
1269 offset += stream->num_bd;
1271 ret = cdns_allocate_pdi(cdns, &stream->in,
1272 stream->num_in, offset);
1276 offset += stream->num_in;
1278 ret = cdns_allocate_pdi(cdns, &stream->out,
1279 stream->num_out, offset);
1283 /* Update total number of PCM PDIs */
1284 stream->num_pdi = stream->num_bd + stream->num_in + stream->num_out;
1285 cdns->num_ports = stream->num_pdi;
1289 EXPORT_SYMBOL(sdw_cdns_pdi_init);
1291 static u32 cdns_set_initial_frame_shape(int n_rows, int n_cols)
1297 r = sdw_find_row_index(n_rows);
1298 c = sdw_find_col_index(n_cols);
1300 val = FIELD_PREP(CDNS_MCP_FRAME_SHAPE_ROW_MASK, r);
1301 val |= FIELD_PREP(CDNS_MCP_FRAME_SHAPE_COL_MASK, c);
1306 static void cdns_init_clock_ctrl(struct sdw_cdns *cdns)
1308 struct sdw_bus *bus = &cdns->bus;
1309 struct sdw_master_prop *prop = &bus->prop;
1314 /* Set clock divider */
1315 divider = (prop->mclk_freq / prop->max_clk_freq) - 1;
1317 cdns_updatel(cdns, CDNS_MCP_CLK_CTRL0,
1318 CDNS_MCP_CLK_MCLKD_MASK, divider);
1319 cdns_updatel(cdns, CDNS_MCP_CLK_CTRL1,
1320 CDNS_MCP_CLK_MCLKD_MASK, divider);
1323 * Frame shape changes after initialization have to be done
1324 * with the bank switch mechanism
1326 val = cdns_set_initial_frame_shape(prop->default_row,
1328 cdns_writel(cdns, CDNS_MCP_FRAME_SHAPE_INIT, val);
1330 /* Set SSP interval to default value */
1331 ssp_interval = prop->default_frame_rate / SDW_CADENCE_GSYNC_HZ;
1332 cdns_writel(cdns, CDNS_MCP_SSP_CTRL0, ssp_interval);
1333 cdns_writel(cdns, CDNS_MCP_SSP_CTRL1, ssp_interval);
1337 * sdw_cdns_init() - Cadence initialization
1338 * @cdns: Cadence instance
1340 int sdw_cdns_init(struct sdw_cdns *cdns)
1344 cdns_init_clock_ctrl(cdns);
1346 sdw_cdns_check_self_clearing_bits(cdns, __func__, false, 0);
1348 /* reset msg_count to default value of FIFOLEVEL */
1349 cdns->msg_count = cdns_readl(cdns, CDNS_MCP_FIFOLEVEL);
1351 /* flush command FIFOs */
1352 cdns_updatel(cdns, CDNS_MCP_CONTROL, CDNS_MCP_CONTROL_CMD_RST,
1353 CDNS_MCP_CONTROL_CMD_RST);
1355 /* Set cmd accept mode */
1356 cdns_ip_updatel(cdns, CDNS_IP_MCP_CONTROL, CDNS_IP_MCP_CONTROL_CMD_ACCEPT,
1357 CDNS_IP_MCP_CONTROL_CMD_ACCEPT);
1359 /* Configure mcp config */
1360 val = cdns_readl(cdns, CDNS_MCP_CONFIG);
1362 /* Disable auto bus release */
1363 val &= ~CDNS_MCP_CONFIG_BUS_REL;
1365 cdns_writel(cdns, CDNS_MCP_CONFIG, val);
1367 /* Configure IP mcp config */
1368 val = cdns_ip_readl(cdns, CDNS_IP_MCP_CONFIG);
1370 /* enable bus operations with clock and data */
1371 val &= ~CDNS_IP_MCP_CONFIG_OP;
1372 val |= CDNS_IP_MCP_CONFIG_OP_NORMAL;
1374 /* Set cmd mode for Tx and Rx cmds */
1375 val &= ~CDNS_IP_MCP_CONFIG_CMD;
1377 /* Disable sniffer mode */
1378 val &= ~CDNS_IP_MCP_CONFIG_SNIFFER;
1380 if (cdns->bus.multi_link)
1381 /* Set Multi-master mode to take gsync into account */
1382 val |= CDNS_IP_MCP_CONFIG_MMASTER;
1384 /* leave frame delay to hardware default of 0x1F */
1386 /* leave command retry to hardware default of 0 */
1388 cdns_ip_writel(cdns, CDNS_IP_MCP_CONFIG, val);
1390 /* changes will be committed later */
1393 EXPORT_SYMBOL(sdw_cdns_init);
1395 int cdns_bus_conf(struct sdw_bus *bus, struct sdw_bus_params *params)
1397 struct sdw_master_prop *prop = &bus->prop;
1398 struct sdw_cdns *cdns = bus_to_cdns(bus);
1399 int mcp_clkctrl_off;
1402 if (!params->curr_dr_freq) {
1403 dev_err(cdns->dev, "NULL curr_dr_freq\n");
1407 divider = prop->mclk_freq * SDW_DOUBLE_RATE_FACTOR /
1408 params->curr_dr_freq;
1409 divider--; /* divider is 1/(N+1) */
1411 if (params->next_bank)
1412 mcp_clkctrl_off = CDNS_MCP_CLK_CTRL1;
1414 mcp_clkctrl_off = CDNS_MCP_CLK_CTRL0;
1416 cdns_updatel(cdns, mcp_clkctrl_off, CDNS_MCP_CLK_MCLKD_MASK, divider);
1420 EXPORT_SYMBOL(cdns_bus_conf);
1422 static int cdns_port_params(struct sdw_bus *bus,
1423 struct sdw_port_params *p_params, unsigned int bank)
1425 struct sdw_cdns *cdns = bus_to_cdns(bus);
1426 int dpn_config_off_source;
1427 int dpn_config_off_target;
1428 int target_num = p_params->num;
1429 int source_num = p_params->num;
1430 bool override = false;
1433 if (target_num == cdns->pdi_loopback_target &&
1434 cdns->pdi_loopback_source != -1) {
1435 source_num = cdns->pdi_loopback_source;
1440 dpn_config_off_source = CDNS_DPN_B1_CONFIG(source_num);
1441 dpn_config_off_target = CDNS_DPN_B1_CONFIG(target_num);
1443 dpn_config_off_source = CDNS_DPN_B0_CONFIG(source_num);
1444 dpn_config_off_target = CDNS_DPN_B0_CONFIG(target_num);
1447 dpn_config = cdns_readl(cdns, dpn_config_off_source);
1449 /* use port params if there is no loopback, otherwise use source as is */
1451 u32p_replace_bits(&dpn_config, p_params->bps - 1, CDNS_DPN_CONFIG_WL);
1452 u32p_replace_bits(&dpn_config, p_params->flow_mode, CDNS_DPN_CONFIG_PORT_FLOW);
1453 u32p_replace_bits(&dpn_config, p_params->data_mode, CDNS_DPN_CONFIG_PORT_DAT);
1456 cdns_writel(cdns, dpn_config_off_target, dpn_config);
1461 static int cdns_transport_params(struct sdw_bus *bus,
1462 struct sdw_transport_params *t_params,
1463 enum sdw_reg_bank bank)
1465 struct sdw_cdns *cdns = bus_to_cdns(bus);
1467 int dpn_config_off_source;
1468 int dpn_config_off_target;
1470 int dpn_hctrl_off_source;
1471 int dpn_hctrl_off_target;
1473 int dpn_offsetctrl_off_source;
1474 int dpn_offsetctrl_off_target;
1476 int dpn_samplectrl_off_source;
1477 int dpn_samplectrl_off_target;
1478 int source_num = t_params->port_num;
1479 int target_num = t_params->port_num;
1480 bool override = false;
1482 if (target_num == cdns->pdi_loopback_target &&
1483 cdns->pdi_loopback_source != -1) {
1484 source_num = cdns->pdi_loopback_source;
1489 * Note: Only full data port is supported on the Master side for
1490 * both PCM and PDM ports.
1494 dpn_config_off_source = CDNS_DPN_B1_CONFIG(source_num);
1495 dpn_hctrl_off_source = CDNS_DPN_B1_HCTRL(source_num);
1496 dpn_offsetctrl_off_source = CDNS_DPN_B1_OFFSET_CTRL(source_num);
1497 dpn_samplectrl_off_source = CDNS_DPN_B1_SAMPLE_CTRL(source_num);
1499 dpn_config_off_target = CDNS_DPN_B1_CONFIG(target_num);
1500 dpn_hctrl_off_target = CDNS_DPN_B1_HCTRL(target_num);
1501 dpn_offsetctrl_off_target = CDNS_DPN_B1_OFFSET_CTRL(target_num);
1502 dpn_samplectrl_off_target = CDNS_DPN_B1_SAMPLE_CTRL(target_num);
1505 dpn_config_off_source = CDNS_DPN_B0_CONFIG(source_num);
1506 dpn_hctrl_off_source = CDNS_DPN_B0_HCTRL(source_num);
1507 dpn_offsetctrl_off_source = CDNS_DPN_B0_OFFSET_CTRL(source_num);
1508 dpn_samplectrl_off_source = CDNS_DPN_B0_SAMPLE_CTRL(source_num);
1510 dpn_config_off_target = CDNS_DPN_B0_CONFIG(target_num);
1511 dpn_hctrl_off_target = CDNS_DPN_B0_HCTRL(target_num);
1512 dpn_offsetctrl_off_target = CDNS_DPN_B0_OFFSET_CTRL(target_num);
1513 dpn_samplectrl_off_target = CDNS_DPN_B0_SAMPLE_CTRL(target_num);
1516 dpn_config = cdns_readl(cdns, dpn_config_off_source);
1518 u32p_replace_bits(&dpn_config, t_params->blk_grp_ctrl, CDNS_DPN_CONFIG_BGC);
1519 u32p_replace_bits(&dpn_config, t_params->blk_pkg_mode, CDNS_DPN_CONFIG_BPM);
1521 cdns_writel(cdns, dpn_config_off_target, dpn_config);
1525 u32p_replace_bits(&dpn_offsetctrl, t_params->offset1, CDNS_DPN_OFFSET_CTRL_1);
1526 u32p_replace_bits(&dpn_offsetctrl, t_params->offset2, CDNS_DPN_OFFSET_CTRL_2);
1528 dpn_offsetctrl = cdns_readl(cdns, dpn_offsetctrl_off_source);
1530 cdns_writel(cdns, dpn_offsetctrl_off_target, dpn_offsetctrl);
1534 u32p_replace_bits(&dpn_hctrl, t_params->hstart, CDNS_DPN_HCTRL_HSTART);
1535 u32p_replace_bits(&dpn_hctrl, t_params->hstop, CDNS_DPN_HCTRL_HSTOP);
1536 u32p_replace_bits(&dpn_hctrl, t_params->lane_ctrl, CDNS_DPN_HCTRL_LCTRL);
1538 dpn_hctrl = cdns_readl(cdns, dpn_hctrl_off_source);
1540 cdns_writel(cdns, dpn_hctrl_off_target, dpn_hctrl);
1543 dpn_samplectrl = t_params->sample_interval - 1;
1545 dpn_samplectrl = cdns_readl(cdns, dpn_samplectrl_off_source);
1546 cdns_writel(cdns, dpn_samplectrl_off_target, dpn_samplectrl);
1551 static int cdns_port_enable(struct sdw_bus *bus,
1552 struct sdw_enable_ch *enable_ch, unsigned int bank)
1554 struct sdw_cdns *cdns = bus_to_cdns(bus);
1555 int dpn_chnen_off, ch_mask;
1558 dpn_chnen_off = CDNS_DPN_B1_CH_EN(enable_ch->port_num);
1560 dpn_chnen_off = CDNS_DPN_B0_CH_EN(enable_ch->port_num);
1562 ch_mask = enable_ch->ch_mask * enable_ch->enable;
1563 cdns_writel(cdns, dpn_chnen_off, ch_mask);
1568 static const struct sdw_master_port_ops cdns_port_ops = {
1569 .dpn_set_port_params = cdns_port_params,
1570 .dpn_set_port_transport_params = cdns_transport_params,
1571 .dpn_port_enable_ch = cdns_port_enable,
1575 * sdw_cdns_is_clock_stop: Check clock status
1577 * @cdns: Cadence instance
1579 bool sdw_cdns_is_clock_stop(struct sdw_cdns *cdns)
1581 return !!(cdns_readl(cdns, CDNS_MCP_STAT) & CDNS_MCP_STAT_CLK_STOP);
1583 EXPORT_SYMBOL(sdw_cdns_is_clock_stop);
1586 * sdw_cdns_clock_stop: Cadence clock stop configuration routine
1588 * @cdns: Cadence instance
1589 * @block_wake: prevent wakes if required by the platform
1591 int sdw_cdns_clock_stop(struct sdw_cdns *cdns, bool block_wake)
1593 bool slave_present = false;
1594 struct sdw_slave *slave;
1597 sdw_cdns_check_self_clearing_bits(cdns, __func__, false, 0);
1599 /* Check suspend status */
1600 if (sdw_cdns_is_clock_stop(cdns)) {
1601 dev_dbg(cdns->dev, "Clock is already stopped\n");
1606 * Before entering clock stop we mask the Slave
1607 * interrupts. This helps avoid having to deal with e.g. a
1608 * Slave becoming UNATTACHED while the clock is being stopped
1610 cdns_enable_slave_interrupts(cdns, false);
1613 * For specific platforms, it is required to be able to put
1614 * master into a state in which it ignores wake-up trials
1615 * in clock stop state
1618 cdns_ip_updatel(cdns, CDNS_IP_MCP_CONTROL,
1619 CDNS_IP_MCP_CONTROL_BLOCK_WAKEUP,
1620 CDNS_IP_MCP_CONTROL_BLOCK_WAKEUP);
1622 list_for_each_entry(slave, &cdns->bus.slaves, node) {
1623 if (slave->status == SDW_SLAVE_ATTACHED ||
1624 slave->status == SDW_SLAVE_ALERT) {
1625 slave_present = true;
1630 /* commit changes */
1631 ret = cdns_config_update(cdns);
1633 dev_err(cdns->dev, "%s: config_update failed\n", __func__);
1637 /* Prepare slaves for clock stop */
1638 if (slave_present) {
1639 ret = sdw_bus_prep_clk_stop(&cdns->bus);
1640 if (ret < 0 && ret != -ENODATA) {
1641 dev_err(cdns->dev, "prepare clock stop failed %d\n", ret);
1647 * Enter clock stop mode and only report errors if there are
1648 * Slave devices present (ALERT or ATTACHED)
1650 ret = sdw_bus_clk_stop(&cdns->bus);
1651 if (ret < 0 && slave_present && ret != -ENODATA) {
1652 dev_err(cdns->dev, "bus clock stop failed %d\n", ret);
1656 ret = cdns_set_wait(cdns, CDNS_MCP_STAT,
1657 CDNS_MCP_STAT_CLK_STOP,
1658 CDNS_MCP_STAT_CLK_STOP);
1660 dev_err(cdns->dev, "Clock stop failed %d\n", ret);
1664 EXPORT_SYMBOL(sdw_cdns_clock_stop);
1667 * sdw_cdns_clock_restart: Cadence PM clock restart configuration routine
1669 * @cdns: Cadence instance
1670 * @bus_reset: context may be lost while in low power modes and the bus
1671 * may require a Severe Reset and re-enumeration after a wake.
1673 int sdw_cdns_clock_restart(struct sdw_cdns *cdns, bool bus_reset)
1677 /* unmask Slave interrupts that were masked when stopping the clock */
1678 cdns_enable_slave_interrupts(cdns, true);
1680 ret = cdns_clear_bit(cdns, CDNS_MCP_CONTROL,
1681 CDNS_MCP_CONTROL_CLK_STOP_CLR);
1683 dev_err(cdns->dev, "Couldn't exit from clock stop\n");
1687 ret = cdns_set_wait(cdns, CDNS_MCP_STAT, CDNS_MCP_STAT_CLK_STOP, 0);
1689 dev_err(cdns->dev, "clock stop exit failed %d\n", ret);
1693 cdns_ip_updatel(cdns, CDNS_IP_MCP_CONTROL,
1694 CDNS_IP_MCP_CONTROL_BLOCK_WAKEUP, 0);
1696 cdns_ip_updatel(cdns, CDNS_IP_MCP_CONTROL, CDNS_IP_MCP_CONTROL_CMD_ACCEPT,
1697 CDNS_IP_MCP_CONTROL_CMD_ACCEPT);
1701 /* enable bus operations with clock and data */
1702 cdns_ip_updatel(cdns, CDNS_IP_MCP_CONFIG,
1703 CDNS_IP_MCP_CONFIG_OP,
1704 CDNS_IP_MCP_CONFIG_OP_NORMAL);
1706 ret = cdns_config_update(cdns);
1708 dev_err(cdns->dev, "%s: config_update failed\n", __func__);
1712 ret = sdw_bus_exit_clk_stop(&cdns->bus);
1714 dev_err(cdns->dev, "bus failed to exit clock stop %d\n", ret);
1719 EXPORT_SYMBOL(sdw_cdns_clock_restart);
1722 * sdw_cdns_probe() - Cadence probe routine
1723 * @cdns: Cadence instance
1725 int sdw_cdns_probe(struct sdw_cdns *cdns)
1727 init_completion(&cdns->tx_complete);
1728 cdns->bus.port_ops = &cdns_port_ops;
1730 INIT_WORK(&cdns->work, cdns_update_slave_status_work);
1733 EXPORT_SYMBOL(sdw_cdns_probe);
1735 int cdns_set_sdw_stream(struct snd_soc_dai *dai,
1736 void *stream, int direction)
1738 struct sdw_cdns *cdns = snd_soc_dai_get_drvdata(dai);
1739 struct sdw_cdns_dai_runtime *dai_runtime;
1741 dai_runtime = cdns->dai_runtime_array[dai->id];
1744 /* first paranoia check */
1747 "dai_runtime already allocated for dai %s\n",
1752 /* allocate and set dai_runtime info */
1753 dai_runtime = kzalloc(sizeof(*dai_runtime), GFP_KERNEL);
1757 dai_runtime->stream_type = SDW_STREAM_PCM;
1759 dai_runtime->bus = &cdns->bus;
1760 dai_runtime->link_id = cdns->instance;
1762 dai_runtime->stream = stream;
1763 dai_runtime->direction = direction;
1765 cdns->dai_runtime_array[dai->id] = dai_runtime;
1767 /* second paranoia check */
1770 "dai_runtime not allocated for dai %s\n",
1775 /* for NULL stream we release allocated dai_runtime */
1777 cdns->dai_runtime_array[dai->id] = NULL;
1781 EXPORT_SYMBOL(cdns_set_sdw_stream);
1784 * cdns_find_pdi() - Find a free PDI
1786 * @cdns: Cadence instance
1787 * @offset: Starting offset
1788 * @num: Number of PDIs
1789 * @pdi: PDI instances
1792 * Find a PDI for a given PDI array. The PDI num and dai_id are
1793 * expected to match, return NULL otherwise.
1795 static struct sdw_cdns_pdi *cdns_find_pdi(struct sdw_cdns *cdns,
1796 unsigned int offset,
1798 struct sdw_cdns_pdi *pdi,
1803 for (i = offset; i < offset + num; i++)
1804 if (pdi[i].num == dai_id)
1811 * sdw_cdns_config_stream: Configure a stream
1813 * @cdns: Cadence instance
1814 * @ch: Channel count
1815 * @dir: Data direction
1816 * @pdi: PDI to be used
1818 void sdw_cdns_config_stream(struct sdw_cdns *cdns,
1819 u32 ch, u32 dir, struct sdw_cdns_pdi *pdi)
1821 u32 offset, val = 0;
1823 if (dir == SDW_DATA_DIR_RX) {
1824 val = CDNS_PORTCTRL_DIRN;
1826 if (cdns->bus.params.m_data_mode != SDW_PORT_DATA_MODE_NORMAL)
1827 val |= CDNS_PORTCTRL_TEST_FAILED;
1829 offset = CDNS_PORTCTRL + pdi->num * CDNS_PORT_OFFSET;
1830 cdns_updatel(cdns, offset,
1831 CDNS_PORTCTRL_DIRN | CDNS_PORTCTRL_TEST_FAILED,
1835 val |= CDNS_PDI_CONFIG_SOFT_RESET;
1836 val |= FIELD_PREP(CDNS_PDI_CONFIG_CHANNEL, (1 << ch) - 1);
1837 cdns_writel(cdns, CDNS_PDI_CONFIG(pdi->num), val);
1839 EXPORT_SYMBOL(sdw_cdns_config_stream);
1842 * sdw_cdns_alloc_pdi() - Allocate a PDI
1844 * @cdns: Cadence instance
1845 * @stream: Stream to be allocated
1846 * @ch: Channel count
1847 * @dir: Data direction
1850 struct sdw_cdns_pdi *sdw_cdns_alloc_pdi(struct sdw_cdns *cdns,
1851 struct sdw_cdns_streams *stream,
1852 u32 ch, u32 dir, int dai_id)
1854 struct sdw_cdns_pdi *pdi = NULL;
1856 if (dir == SDW_DATA_DIR_RX)
1857 pdi = cdns_find_pdi(cdns, 0, stream->num_in, stream->in,
1860 pdi = cdns_find_pdi(cdns, 0, stream->num_out, stream->out,
1863 /* check if we found a PDI, else find in bi-directional */
1865 pdi = cdns_find_pdi(cdns, 2, stream->num_bd, stream->bd,
1870 pdi->h_ch_num = ch - 1;
1877 EXPORT_SYMBOL(sdw_cdns_alloc_pdi);
1879 MODULE_LICENSE("Dual BSD/GPL");
1880 MODULE_DESCRIPTION("Cadence Soundwire Library");