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");
287 * sdw_cdns_config_update() - Update configurations
288 * @cdns: Cadence instance
290 void sdw_cdns_config_update(struct sdw_cdns *cdns)
293 cdns_writel(cdns, CDNS_MCP_CONFIG_UPDATE, CDNS_MCP_CONFIG_UPDATE_BIT);
295 EXPORT_SYMBOL(sdw_cdns_config_update);
298 * sdw_cdns_config_update_set_wait() - wait until configuration update bit is self-cleared
299 * @cdns: Cadence instance
301 int sdw_cdns_config_update_set_wait(struct sdw_cdns *cdns)
303 /* the hardware recommendation is to wait at least 300us */
304 return cdns_set_wait(cdns, CDNS_MCP_CONFIG_UPDATE,
305 CDNS_MCP_CONFIG_UPDATE_BIT, 0);
307 EXPORT_SYMBOL(sdw_cdns_config_update_set_wait);
312 #ifdef CONFIG_DEBUG_FS
314 #define RD_BUF (2 * PAGE_SIZE)
316 static ssize_t cdns_sprintf(struct sdw_cdns *cdns,
317 char *buf, size_t pos, unsigned int reg)
319 return scnprintf(buf + pos, RD_BUF - pos,
320 "%4x\t%8x\n", reg, cdns_readl(cdns, reg));
323 static int cdns_reg_show(struct seq_file *s, void *data)
325 struct sdw_cdns *cdns = s->private;
331 buf = kzalloc(RD_BUF, GFP_KERNEL);
335 ret = scnprintf(buf, RD_BUF, "Register Value\n");
336 ret += scnprintf(buf + ret, RD_BUF - ret, "\nMCP Registers\n");
337 /* 8 MCP registers */
338 for (i = CDNS_MCP_CONFIG; i <= CDNS_MCP_PHYCTRL; i += sizeof(u32))
339 ret += cdns_sprintf(cdns, buf, ret, i);
341 ret += scnprintf(buf + ret, RD_BUF - ret,
342 "\nStatus & Intr Registers\n");
343 /* 13 Status & Intr registers (offsets 0x70 and 0x74 not defined) */
344 for (i = CDNS_MCP_STAT; i <= CDNS_MCP_FIFOSTAT; i += sizeof(u32))
345 ret += cdns_sprintf(cdns, buf, ret, i);
347 ret += scnprintf(buf + ret, RD_BUF - ret,
348 "\nSSP & Clk ctrl Registers\n");
349 ret += cdns_sprintf(cdns, buf, ret, CDNS_MCP_SSP_CTRL0);
350 ret += cdns_sprintf(cdns, buf, ret, CDNS_MCP_SSP_CTRL1);
351 ret += cdns_sprintf(cdns, buf, ret, CDNS_MCP_CLK_CTRL0);
352 ret += cdns_sprintf(cdns, buf, ret, CDNS_MCP_CLK_CTRL1);
354 ret += scnprintf(buf + ret, RD_BUF - ret,
355 "\nDPn B0 Registers\n");
357 num_ports = cdns->num_ports;
359 for (i = 0; i < num_ports; i++) {
360 ret += scnprintf(buf + ret, RD_BUF - ret,
362 for (j = CDNS_DPN_B0_CONFIG(i);
363 j < CDNS_DPN_B0_ASYNC_CTRL(i); j += sizeof(u32))
364 ret += cdns_sprintf(cdns, buf, ret, j);
367 ret += scnprintf(buf + ret, RD_BUF - ret,
368 "\nDPn B1 Registers\n");
369 for (i = 0; i < num_ports; i++) {
370 ret += scnprintf(buf + ret, RD_BUF - ret,
373 for (j = CDNS_DPN_B1_CONFIG(i);
374 j < CDNS_DPN_B1_ASYNC_CTRL(i); j += sizeof(u32))
375 ret += cdns_sprintf(cdns, buf, ret, j);
378 ret += scnprintf(buf + ret, RD_BUF - ret,
379 "\nDPn Control Registers\n");
380 for (i = 0; i < num_ports; i++)
381 ret += cdns_sprintf(cdns, buf, ret,
382 CDNS_PORTCTRL + i * CDNS_PORT_OFFSET);
384 ret += scnprintf(buf + ret, RD_BUF - ret,
385 "\nPDIn Config Registers\n");
387 /* number of PDI and ports is interchangeable */
388 for (i = 0; i < num_ports; i++)
389 ret += cdns_sprintf(cdns, buf, ret, CDNS_PDI_CONFIG(i));
391 seq_printf(s, "%s", buf);
396 DEFINE_SHOW_ATTRIBUTE(cdns_reg);
398 static int cdns_hw_reset(void *data, u64 value)
400 struct sdw_cdns *cdns = data;
406 /* Userspace changed the hardware state behind the kernel's back */
407 add_taint(TAINT_USER, LOCKDEP_STILL_OK);
409 ret = sdw_cdns_exit_reset(cdns);
411 dev_dbg(cdns->dev, "link hw_reset done: %d\n", ret);
416 DEFINE_DEBUGFS_ATTRIBUTE(cdns_hw_reset_fops, NULL, cdns_hw_reset, "%llu\n");
418 static int cdns_parity_error_injection(void *data, u64 value)
420 struct sdw_cdns *cdns = data;
430 * Resume Master device. If this results in a bus reset, the
431 * Slave devices will re-attach and be re-enumerated.
433 ret = pm_runtime_resume_and_get(bus->dev);
434 if (ret < 0 && ret != -EACCES) {
435 dev_err_ratelimited(cdns->dev,
436 "pm_runtime_resume_and_get failed in %s, ret %d\n",
442 * wait long enough for Slave(s) to be in steady state. This
443 * does not need to be super precise.
448 * Take the bus lock here to make sure that any bus transactions
449 * will be queued while we inject a parity error on a dummy read
451 mutex_lock(&bus->bus_lock);
453 /* program hardware to inject parity error */
454 cdns_ip_updatel(cdns, CDNS_IP_MCP_CMDCTRL,
455 CDNS_IP_MCP_CMDCTRL_INSERT_PARITY_ERR,
456 CDNS_IP_MCP_CMDCTRL_INSERT_PARITY_ERR);
459 ret = cdns_clear_bit(cdns, CDNS_MCP_CONFIG_UPDATE, CDNS_MCP_CONFIG_UPDATE_BIT);
463 /* do a broadcast dummy read to avoid bus clashes */
464 ret = sdw_bread_no_pm_unlocked(&cdns->bus, 0xf, SDW_SCP_DEVID_0);
465 dev_info(cdns->dev, "parity error injection, read: %d\n", ret);
467 /* program hardware to disable parity error */
468 cdns_ip_updatel(cdns, CDNS_IP_MCP_CMDCTRL,
469 CDNS_IP_MCP_CMDCTRL_INSERT_PARITY_ERR,
473 ret = cdns_clear_bit(cdns, CDNS_MCP_CONFIG_UPDATE, CDNS_MCP_CONFIG_UPDATE_BIT);
477 /* Userspace changed the hardware state behind the kernel's back */
478 add_taint(TAINT_USER, LOCKDEP_STILL_OK);
481 /* Continue bus operation with parity error injection disabled */
482 mutex_unlock(&bus->bus_lock);
485 * allow Master device to enter pm_runtime suspend. This may
486 * also result in Slave devices suspending.
488 pm_runtime_mark_last_busy(bus->dev);
489 pm_runtime_put_autosuspend(bus->dev);
494 DEFINE_DEBUGFS_ATTRIBUTE(cdns_parity_error_fops, NULL,
495 cdns_parity_error_injection, "%llu\n");
497 static int cdns_set_pdi_loopback_source(void *data, u64 value)
499 struct sdw_cdns *cdns = data;
500 unsigned int pdi_out_num = cdns->pcm.num_bd + cdns->pcm.num_out;
502 if (value > pdi_out_num)
505 /* Userspace changed the hardware state behind the kernel's back */
506 add_taint(TAINT_USER, LOCKDEP_STILL_OK);
508 cdns->pdi_loopback_source = value;
512 DEFINE_DEBUGFS_ATTRIBUTE(cdns_pdi_loopback_source_fops, NULL, cdns_set_pdi_loopback_source, "%llu\n");
514 static int cdns_set_pdi_loopback_target(void *data, u64 value)
516 struct sdw_cdns *cdns = data;
517 unsigned int pdi_in_num = cdns->pcm.num_bd + cdns->pcm.num_in;
519 if (value > pdi_in_num)
522 /* Userspace changed the hardware state behind the kernel's back */
523 add_taint(TAINT_USER, LOCKDEP_STILL_OK);
525 cdns->pdi_loopback_target = value;
529 DEFINE_DEBUGFS_ATTRIBUTE(cdns_pdi_loopback_target_fops, NULL, cdns_set_pdi_loopback_target, "%llu\n");
532 * sdw_cdns_debugfs_init() - Cadence debugfs init
533 * @cdns: Cadence instance
534 * @root: debugfs root
536 void sdw_cdns_debugfs_init(struct sdw_cdns *cdns, struct dentry *root)
538 debugfs_create_file("cdns-registers", 0400, root, cdns, &cdns_reg_fops);
540 debugfs_create_file("cdns-hw-reset", 0200, root, cdns,
541 &cdns_hw_reset_fops);
543 debugfs_create_file("cdns-parity-error-injection", 0200, root, cdns,
544 &cdns_parity_error_fops);
546 cdns->pdi_loopback_source = -1;
547 cdns->pdi_loopback_target = -1;
549 debugfs_create_file("cdns-pdi-loopback-source", 0200, root, cdns,
550 &cdns_pdi_loopback_source_fops);
552 debugfs_create_file("cdns-pdi-loopback-target", 0200, root, cdns,
553 &cdns_pdi_loopback_target_fops);
556 EXPORT_SYMBOL_GPL(sdw_cdns_debugfs_init);
558 #endif /* CONFIG_DEBUG_FS */
563 static enum sdw_command_response
564 cdns_fill_msg_resp(struct sdw_cdns *cdns,
565 struct sdw_msg *msg, int count, int offset)
567 int nack = 0, no_ack = 0;
570 /* check message response */
571 for (i = 0; i < count; i++) {
572 if (!(cdns->response_buf[i] & CDNS_MCP_RESP_ACK)) {
574 dev_vdbg(cdns->dev, "Msg Ack not received, cmd %d\n", i);
576 if (cdns->response_buf[i] & CDNS_MCP_RESP_NACK) {
578 dev_err_ratelimited(cdns->dev, "Msg NACK received, cmd %d\n", i);
583 dev_err_ratelimited(cdns->dev, "Msg NACKed for Slave %d\n", msg->dev_num);
588 dev_dbg_ratelimited(cdns->dev, "Msg ignored for Slave %d\n", msg->dev_num);
589 return SDW_CMD_IGNORED;
592 if (msg->flags == SDW_MSG_FLAG_READ) {
594 for (i = 0; i < count; i++)
595 msg->buf[i + offset] = FIELD_GET(CDNS_MCP_RESP_RDATA,
596 cdns->response_buf[i]);
602 static void cdns_read_response(struct sdw_cdns *cdns)
604 u32 num_resp, cmd_base;
607 /* RX_FIFO_AVAIL can be 2 entries more than the FIFO size */
608 BUILD_BUG_ON(ARRAY_SIZE(cdns->response_buf) < CDNS_MCP_CMD_LEN + 2);
610 num_resp = cdns_readl(cdns, CDNS_MCP_FIFOSTAT);
611 num_resp &= CDNS_MCP_RX_FIFO_AVAIL;
612 if (num_resp > ARRAY_SIZE(cdns->response_buf)) {
613 dev_warn(cdns->dev, "RX AVAIL %d too long\n", num_resp);
614 num_resp = ARRAY_SIZE(cdns->response_buf);
617 cmd_base = CDNS_IP_MCP_CMD_BASE;
619 for (i = 0; i < num_resp; i++) {
620 cdns->response_buf[i] = cdns_ip_readl(cdns, cmd_base);
621 cmd_base += CDNS_MCP_CMD_WORD_LEN;
625 static enum sdw_command_response
626 _cdns_xfer_msg(struct sdw_cdns *cdns, struct sdw_msg *msg, int cmd,
627 int offset, int count, bool defer)
633 /* Program the watermark level for RX FIFO */
634 if (cdns->msg_count != count) {
635 cdns_writel(cdns, CDNS_MCP_FIFOLEVEL, count);
636 cdns->msg_count = count;
639 base = CDNS_IP_MCP_CMD_BASE;
640 addr = msg->addr + offset;
642 for (i = 0; i < count; i++) {
643 data = FIELD_PREP(CDNS_MCP_CMD_DEV_ADDR, msg->dev_num);
644 data |= FIELD_PREP(CDNS_MCP_CMD_COMMAND, cmd);
645 data |= FIELD_PREP(CDNS_MCP_CMD_REG_ADDR, addr);
648 if (msg->flags == SDW_MSG_FLAG_WRITE)
649 data |= msg->buf[i + offset];
651 data |= FIELD_PREP(CDNS_MCP_CMD_SSP_TAG, msg->ssp_sync);
652 cdns_ip_writel(cdns, base, data);
653 base += CDNS_MCP_CMD_WORD_LEN;
659 /* wait for timeout or response */
660 time = wait_for_completion_timeout(&cdns->tx_complete,
661 msecs_to_jiffies(CDNS_TX_TIMEOUT));
663 dev_err(cdns->dev, "IO transfer timed out, cmd %d device %d addr %x len %d\n",
664 cmd, msg->dev_num, msg->addr, msg->len);
667 /* Drain anything in the RX_FIFO */
668 cdns_read_response(cdns);
670 return SDW_CMD_TIMEOUT;
673 return cdns_fill_msg_resp(cdns, msg, count, offset);
676 static enum sdw_command_response
677 cdns_program_scp_addr(struct sdw_cdns *cdns, struct sdw_msg *msg)
679 int nack = 0, no_ack = 0;
684 /* Program the watermark level for RX FIFO */
685 if (cdns->msg_count != CDNS_SCP_RX_FIFOLEVEL) {
686 cdns_writel(cdns, CDNS_MCP_FIFOLEVEL, CDNS_SCP_RX_FIFOLEVEL);
687 cdns->msg_count = CDNS_SCP_RX_FIFOLEVEL;
690 data[0] = FIELD_PREP(CDNS_MCP_CMD_DEV_ADDR, msg->dev_num);
691 data[0] |= FIELD_PREP(CDNS_MCP_CMD_COMMAND, 0x3);
694 data[0] |= FIELD_PREP(CDNS_MCP_CMD_REG_ADDR, SDW_SCP_ADDRPAGE1);
695 data[1] |= FIELD_PREP(CDNS_MCP_CMD_REG_ADDR, SDW_SCP_ADDRPAGE2);
697 data[0] |= msg->addr_page1;
698 data[1] |= msg->addr_page2;
700 base = CDNS_IP_MCP_CMD_BASE;
701 cdns_ip_writel(cdns, base, data[0]);
702 base += CDNS_MCP_CMD_WORD_LEN;
703 cdns_ip_writel(cdns, base, data[1]);
705 time = wait_for_completion_timeout(&cdns->tx_complete,
706 msecs_to_jiffies(CDNS_TX_TIMEOUT));
708 dev_err(cdns->dev, "SCP Msg trf timed out\n");
710 return SDW_CMD_TIMEOUT;
713 /* check response the writes */
714 for (i = 0; i < 2; i++) {
715 if (!(cdns->response_buf[i] & CDNS_MCP_RESP_ACK)) {
717 dev_err(cdns->dev, "Program SCP Ack not received\n");
718 if (cdns->response_buf[i] & CDNS_MCP_RESP_NACK) {
720 dev_err(cdns->dev, "Program SCP NACK received\n");
725 /* For NACK, NO ack, don't return err if we are in Broadcast mode */
727 dev_err_ratelimited(cdns->dev,
728 "SCP_addrpage NACKed for Slave %d\n", msg->dev_num);
733 dev_dbg_ratelimited(cdns->dev,
734 "SCP_addrpage ignored for Slave %d\n", msg->dev_num);
735 return SDW_CMD_IGNORED;
741 static int cdns_prep_msg(struct sdw_cdns *cdns, struct sdw_msg *msg, int *cmd)
746 ret = cdns_program_scp_addr(cdns, msg);
753 switch (msg->flags) {
754 case SDW_MSG_FLAG_READ:
755 *cmd = CDNS_MCP_CMD_READ;
758 case SDW_MSG_FLAG_WRITE:
759 *cmd = CDNS_MCP_CMD_WRITE;
763 dev_err(cdns->dev, "Invalid msg cmd: %d\n", msg->flags);
770 enum sdw_command_response
771 cdns_xfer_msg(struct sdw_bus *bus, struct sdw_msg *msg)
773 struct sdw_cdns *cdns = bus_to_cdns(bus);
776 ret = cdns_prep_msg(cdns, msg, &cmd);
778 return SDW_CMD_FAIL_OTHER;
780 for (i = 0; i < msg->len / CDNS_MCP_CMD_LEN; i++) {
781 ret = _cdns_xfer_msg(cdns, msg, cmd, i * CDNS_MCP_CMD_LEN,
782 CDNS_MCP_CMD_LEN, false);
783 if (ret != SDW_CMD_OK)
787 if (!(msg->len % CDNS_MCP_CMD_LEN))
790 return _cdns_xfer_msg(cdns, msg, cmd, i * CDNS_MCP_CMD_LEN,
791 msg->len % CDNS_MCP_CMD_LEN, false);
793 EXPORT_SYMBOL(cdns_xfer_msg);
795 enum sdw_command_response
796 cdns_xfer_msg_defer(struct sdw_bus *bus)
798 struct sdw_cdns *cdns = bus_to_cdns(bus);
799 struct sdw_defer *defer = &bus->defer_msg;
800 struct sdw_msg *msg = defer->msg;
803 /* for defer only 1 message is supported */
807 ret = cdns_prep_msg(cdns, msg, &cmd);
809 return SDW_CMD_FAIL_OTHER;
811 return _cdns_xfer_msg(cdns, msg, cmd, 0, msg->len, true);
813 EXPORT_SYMBOL(cdns_xfer_msg_defer);
815 u32 cdns_read_ping_status(struct sdw_bus *bus)
817 struct sdw_cdns *cdns = bus_to_cdns(bus);
819 return cdns_readl(cdns, CDNS_MCP_SLAVE_STAT);
821 EXPORT_SYMBOL(cdns_read_ping_status);
827 static int cdns_update_slave_status(struct sdw_cdns *cdns,
830 enum sdw_slave_status status[SDW_MAX_DEVICES + 1];
831 bool is_slave = false;
836 memset(status, 0, sizeof(status));
838 for (i = 0; i <= SDW_MAX_DEVICES; i++) {
839 mask = (slave_intstat >> (i * CDNS_MCP_SLAVE_STATUS_NUM)) &
840 CDNS_MCP_SLAVE_STATUS_BITS;
847 if (mask & CDNS_MCP_SLAVE_INTSTAT_RESERVED) {
848 status[i] = SDW_SLAVE_RESERVED;
852 if (mask & CDNS_MCP_SLAVE_INTSTAT_ATTACHED) {
853 status[i] = SDW_SLAVE_ATTACHED;
857 if (mask & CDNS_MCP_SLAVE_INTSTAT_ALERT) {
858 status[i] = SDW_SLAVE_ALERT;
862 if (mask & CDNS_MCP_SLAVE_INTSTAT_NPRESENT) {
863 status[i] = SDW_SLAVE_UNATTACHED;
869 * check that there was a single reported Slave status and when
870 * there is not use the latest status extracted from PING commands
872 if (set_status != 1) {
873 val = cdns_readl(cdns, CDNS_MCP_SLAVE_STAT);
878 status[i] = SDW_SLAVE_UNATTACHED;
881 status[i] = SDW_SLAVE_ATTACHED;
884 status[i] = SDW_SLAVE_ALERT;
888 status[i] = SDW_SLAVE_RESERVED;
895 return sdw_handle_slave_status(&cdns->bus, status);
901 * sdw_cdns_irq() - Cadence interrupt handler
903 * @dev_id: irq context
905 irqreturn_t sdw_cdns_irq(int irq, void *dev_id)
907 struct sdw_cdns *cdns = dev_id;
910 /* Check if the link is up */
914 int_status = cdns_readl(cdns, CDNS_MCP_INTSTAT);
916 /* check for reserved values read as zero */
917 if (int_status & CDNS_MCP_INT_RESERVED)
920 if (!(int_status & CDNS_MCP_INT_IRQ))
923 if (int_status & CDNS_MCP_INT_RX_WL) {
924 struct sdw_bus *bus = &cdns->bus;
925 struct sdw_defer *defer = &bus->defer_msg;
927 cdns_read_response(cdns);
929 if (defer && defer->msg) {
930 cdns_fill_msg_resp(cdns, defer->msg,
932 complete(&defer->complete);
934 complete(&cdns->tx_complete);
938 if (int_status & CDNS_MCP_INT_PARITY) {
939 /* Parity error detected by Master */
940 dev_err_ratelimited(cdns->dev, "Parity error\n");
943 if (int_status & CDNS_MCP_INT_CTRL_CLASH) {
944 /* Slave is driving bit slot during control word */
945 dev_err_ratelimited(cdns->dev, "Bus clash for control word\n");
948 if (int_status & CDNS_MCP_INT_DATA_CLASH) {
950 * Multiple slaves trying to drive bit slot, or issue with
951 * ownership of data bits or Slave gone bonkers
953 dev_err_ratelimited(cdns->dev, "Bus clash for data word\n");
956 if (cdns->bus.params.m_data_mode != SDW_PORT_DATA_MODE_NORMAL &&
957 int_status & CDNS_MCP_INT_DPINT) {
960 /* just log which ports report an error */
961 port_intstat = cdns_readl(cdns, CDNS_MCP_PORT_INTSTAT);
962 dev_err_ratelimited(cdns->dev, "DP interrupt: PortIntStat %8x\n",
965 /* clear status w/ write1 */
966 cdns_writel(cdns, CDNS_MCP_PORT_INTSTAT, port_intstat);
969 if (int_status & CDNS_MCP_INT_SLAVE_MASK) {
970 /* Mask the Slave interrupt and wake thread */
971 cdns_updatel(cdns, CDNS_MCP_INTMASK,
972 CDNS_MCP_INT_SLAVE_MASK, 0);
974 int_status &= ~CDNS_MCP_INT_SLAVE_MASK;
977 * Deal with possible race condition between interrupt
978 * handling and disabling interrupts on suspend.
980 * If the master is in the process of disabling
981 * interrupts, don't schedule a workqueue
983 if (cdns->interrupt_enabled)
984 schedule_work(&cdns->work);
987 cdns_writel(cdns, CDNS_MCP_INTSTAT, int_status);
990 EXPORT_SYMBOL(sdw_cdns_irq);
993 * cdns_update_slave_status_work - update slave status in a work since we will need to handle
994 * other interrupts eg. CDNS_MCP_INT_RX_WL during the update slave
996 * @work: cdns worker thread
998 static void cdns_update_slave_status_work(struct work_struct *work)
1000 struct sdw_cdns *cdns =
1001 container_of(work, struct sdw_cdns, work);
1005 int retry_count = 0;
1008 * Clear main interrupt first so we don't lose any assertions
1009 * that happen during this function.
1011 cdns_writel(cdns, CDNS_MCP_INTSTAT, CDNS_MCP_INT_SLAVE_MASK);
1013 slave0 = cdns_readl(cdns, CDNS_MCP_SLAVE_INTSTAT0);
1014 slave1 = cdns_readl(cdns, CDNS_MCP_SLAVE_INTSTAT1);
1017 * Clear the bits before handling so we don't lose any
1018 * bits that re-assert.
1020 cdns_writel(cdns, CDNS_MCP_SLAVE_INTSTAT0, slave0);
1021 cdns_writel(cdns, CDNS_MCP_SLAVE_INTSTAT1, slave1);
1023 /* combine the two status */
1024 slave_intstat = ((u64)slave1 << 32) | slave0;
1026 dev_dbg_ratelimited(cdns->dev, "Slave status change: 0x%llx\n", slave_intstat);
1029 cdns_update_slave_status(cdns, slave_intstat);
1032 * When there is more than one peripheral per link, it's
1033 * possible that a deviceB becomes attached after we deal with
1034 * the attachment of deviceA. Since the hardware does a
1035 * logical AND, the attachment of the second device does not
1036 * change the status seen by the driver.
1038 * In that case, clearing the registers above would result in
1039 * the deviceB never being detected - until a change of status
1040 * is observed on the bus.
1042 * To avoid this race condition, re-check if any device0 needs
1043 * attention with PING commands. There is no need to check for
1044 * ALERTS since they are not allowed until a non-zero
1045 * device_number is assigned.
1047 * Do not clear the INTSTAT0/1. While looping to enumerate devices on
1048 * #0 there could be status changes on other devices - these must
1049 * be kept in the INTSTAT so they can be handled when all #0 devices
1050 * have been handled.
1053 device0_status = cdns_readl(cdns, CDNS_MCP_SLAVE_STAT);
1054 device0_status &= 3;
1056 if (device0_status == SDW_SLAVE_ATTACHED) {
1057 if (retry_count++ < SDW_MAX_DEVICES) {
1058 dev_dbg_ratelimited(cdns->dev,
1059 "Device0 detected after clearing status, iteration %d\n",
1061 slave_intstat = CDNS_MCP_SLAVE_INTSTAT_ATTACHED;
1064 dev_err_ratelimited(cdns->dev,
1065 "Device0 detected after %d iterations\n",
1070 /* unmask Slave interrupt now */
1071 cdns_updatel(cdns, CDNS_MCP_INTMASK,
1072 CDNS_MCP_INT_SLAVE_MASK, CDNS_MCP_INT_SLAVE_MASK);
1076 /* paranoia check to make sure self-cleared bits are indeed cleared */
1077 void sdw_cdns_check_self_clearing_bits(struct sdw_cdns *cdns, const char *string,
1078 bool initial_delay, int reset_iterations)
1082 u32 mcp_config_update;
1086 usleep_range(1000, 1500);
1088 ip_mcp_control = cdns_ip_readl(cdns, CDNS_IP_MCP_CONTROL);
1090 /* the following bits should be cleared immediately */
1091 if (ip_mcp_control & CDNS_IP_MCP_CONTROL_SW_RST)
1092 dev_err(cdns->dev, "%s failed: IP_MCP_CONTROL_SW_RST is not cleared\n", string);
1094 mcp_control = cdns_readl(cdns, CDNS_MCP_CONTROL);
1096 /* the following bits should be cleared immediately */
1097 if (mcp_control & CDNS_MCP_CONTROL_CMD_RST)
1098 dev_err(cdns->dev, "%s failed: MCP_CONTROL_CMD_RST is not cleared\n", string);
1099 if (mcp_control & CDNS_MCP_CONTROL_SOFT_RST)
1100 dev_err(cdns->dev, "%s failed: MCP_CONTROL_SOFT_RST is not cleared\n", string);
1101 if (mcp_control & CDNS_MCP_CONTROL_CLK_STOP_CLR)
1102 dev_err(cdns->dev, "%s failed: MCP_CONTROL_CLK_STOP_CLR is not cleared\n", string);
1104 mcp_config_update = cdns_readl(cdns, CDNS_MCP_CONFIG_UPDATE);
1105 if (mcp_config_update & CDNS_MCP_CONFIG_UPDATE_BIT)
1106 dev_err(cdns->dev, "%s failed: MCP_CONFIG_UPDATE_BIT is not cleared\n", string);
1109 while (mcp_control & CDNS_MCP_CONTROL_HW_RST) {
1110 if (i == reset_iterations) {
1111 dev_err(cdns->dev, "%s failed: MCP_CONTROL_HW_RST is not cleared\n", string);
1115 dev_dbg(cdns->dev, "%s: MCP_CONTROL_HW_RST is not cleared at iteration %d\n", string, i);
1118 usleep_range(1000, 1500);
1119 mcp_control = cdns_readl(cdns, CDNS_MCP_CONTROL);
1123 EXPORT_SYMBOL(sdw_cdns_check_self_clearing_bits);
1130 * sdw_cdns_exit_reset() - Program reset parameters and start bus operations
1131 * @cdns: Cadence instance
1133 int sdw_cdns_exit_reset(struct sdw_cdns *cdns)
1135 /* keep reset delay unchanged to 4096 cycles */
1137 /* use hardware generated reset */
1138 cdns_updatel(cdns, CDNS_MCP_CONTROL,
1139 CDNS_MCP_CONTROL_HW_RST,
1140 CDNS_MCP_CONTROL_HW_RST);
1142 /* commit changes */
1143 return cdns_config_update(cdns);
1145 EXPORT_SYMBOL(sdw_cdns_exit_reset);
1148 * cdns_enable_slave_interrupts() - Enable SDW slave interrupts
1149 * @cdns: Cadence instance
1150 * @state: boolean for true/false
1152 static void cdns_enable_slave_interrupts(struct sdw_cdns *cdns, bool state)
1156 mask = cdns_readl(cdns, CDNS_MCP_INTMASK);
1158 mask |= CDNS_MCP_INT_SLAVE_MASK;
1160 mask &= ~CDNS_MCP_INT_SLAVE_MASK;
1162 cdns_writel(cdns, CDNS_MCP_INTMASK, mask);
1166 * sdw_cdns_enable_interrupt() - Enable SDW interrupts
1167 * @cdns: Cadence instance
1168 * @state: True if we are trying to enable interrupt.
1170 int sdw_cdns_enable_interrupt(struct sdw_cdns *cdns, bool state)
1172 u32 slave_intmask0 = 0;
1173 u32 slave_intmask1 = 0;
1179 slave_intmask0 = CDNS_MCP_SLAVE_INTMASK0_MASK;
1180 slave_intmask1 = CDNS_MCP_SLAVE_INTMASK1_MASK;
1182 /* enable detection of all slave state changes */
1183 mask = CDNS_MCP_INT_SLAVE_MASK;
1185 /* enable detection of bus issues */
1186 mask |= CDNS_MCP_INT_CTRL_CLASH | CDNS_MCP_INT_DATA_CLASH |
1187 CDNS_MCP_INT_PARITY;
1189 /* port interrupt limited to test modes for now */
1190 if (cdns->bus.params.m_data_mode != SDW_PORT_DATA_MODE_NORMAL)
1191 mask |= CDNS_MCP_INT_DPINT;
1193 /* enable detection of RX fifo level */
1194 mask |= CDNS_MCP_INT_RX_WL;
1197 * CDNS_MCP_INT_IRQ needs to be set otherwise all previous
1198 * settings are irrelevant
1200 mask |= CDNS_MCP_INT_IRQ;
1202 if (interrupt_mask) /* parameter override */
1203 mask = interrupt_mask;
1206 /* clear slave interrupt status before enabling interrupt */
1210 slave_state = cdns_readl(cdns, CDNS_MCP_SLAVE_INTSTAT0);
1211 cdns_writel(cdns, CDNS_MCP_SLAVE_INTSTAT0, slave_state);
1212 slave_state = cdns_readl(cdns, CDNS_MCP_SLAVE_INTSTAT1);
1213 cdns_writel(cdns, CDNS_MCP_SLAVE_INTSTAT1, slave_state);
1215 cdns->interrupt_enabled = state;
1218 * Complete any on-going status updates before updating masks,
1219 * and cancel queued status updates.
1221 * There could be a race with a new interrupt thrown before
1222 * the 3 mask updates below are complete, so in the interrupt
1223 * we use the 'interrupt_enabled' status to prevent new work
1224 * from being queued.
1227 cancel_work_sync(&cdns->work);
1229 cdns_writel(cdns, CDNS_MCP_SLAVE_INTMASK0, slave_intmask0);
1230 cdns_writel(cdns, CDNS_MCP_SLAVE_INTMASK1, slave_intmask1);
1231 cdns_writel(cdns, CDNS_MCP_INTMASK, mask);
1235 EXPORT_SYMBOL(sdw_cdns_enable_interrupt);
1237 static int cdns_allocate_pdi(struct sdw_cdns *cdns,
1238 struct sdw_cdns_pdi **stream,
1239 u32 num, u32 pdi_offset)
1241 struct sdw_cdns_pdi *pdi;
1247 pdi = devm_kcalloc(cdns->dev, num, sizeof(*pdi), GFP_KERNEL);
1251 for (i = 0; i < num; i++) {
1252 pdi[i].num = i + pdi_offset;
1260 * sdw_cdns_pdi_init() - PDI initialization routine
1262 * @cdns: Cadence instance
1263 * @config: Stream configurations
1265 int sdw_cdns_pdi_init(struct sdw_cdns *cdns,
1266 struct sdw_cdns_stream_config config)
1268 struct sdw_cdns_streams *stream;
1272 cdns->pcm.num_bd = config.pcm_bd;
1273 cdns->pcm.num_in = config.pcm_in;
1274 cdns->pcm.num_out = config.pcm_out;
1276 /* Allocate PDIs for PCMs */
1277 stream = &cdns->pcm;
1279 /* we allocate PDI0 and PDI1 which are used for Bulk */
1282 ret = cdns_allocate_pdi(cdns, &stream->bd,
1283 stream->num_bd, offset);
1287 offset += stream->num_bd;
1289 ret = cdns_allocate_pdi(cdns, &stream->in,
1290 stream->num_in, offset);
1294 offset += stream->num_in;
1296 ret = cdns_allocate_pdi(cdns, &stream->out,
1297 stream->num_out, offset);
1301 /* Update total number of PCM PDIs */
1302 stream->num_pdi = stream->num_bd + stream->num_in + stream->num_out;
1303 cdns->num_ports = stream->num_pdi;
1307 EXPORT_SYMBOL(sdw_cdns_pdi_init);
1309 static u32 cdns_set_initial_frame_shape(int n_rows, int n_cols)
1315 r = sdw_find_row_index(n_rows);
1316 c = sdw_find_col_index(n_cols);
1318 val = FIELD_PREP(CDNS_MCP_FRAME_SHAPE_ROW_MASK, r);
1319 val |= FIELD_PREP(CDNS_MCP_FRAME_SHAPE_COL_MASK, c);
1324 static void cdns_init_clock_ctrl(struct sdw_cdns *cdns)
1326 struct sdw_bus *bus = &cdns->bus;
1327 struct sdw_master_prop *prop = &bus->prop;
1332 /* Set clock divider */
1333 divider = (prop->mclk_freq / prop->max_clk_freq) - 1;
1335 cdns_updatel(cdns, CDNS_MCP_CLK_CTRL0,
1336 CDNS_MCP_CLK_MCLKD_MASK, divider);
1337 cdns_updatel(cdns, CDNS_MCP_CLK_CTRL1,
1338 CDNS_MCP_CLK_MCLKD_MASK, divider);
1341 * Frame shape changes after initialization have to be done
1342 * with the bank switch mechanism
1344 val = cdns_set_initial_frame_shape(prop->default_row,
1346 cdns_writel(cdns, CDNS_MCP_FRAME_SHAPE_INIT, val);
1348 /* Set SSP interval to default value */
1349 ssp_interval = prop->default_frame_rate / SDW_CADENCE_GSYNC_HZ;
1350 cdns_writel(cdns, CDNS_MCP_SSP_CTRL0, ssp_interval);
1351 cdns_writel(cdns, CDNS_MCP_SSP_CTRL1, ssp_interval);
1355 * sdw_cdns_init() - Cadence initialization
1356 * @cdns: Cadence instance
1358 int sdw_cdns_init(struct sdw_cdns *cdns)
1362 cdns_init_clock_ctrl(cdns);
1364 sdw_cdns_check_self_clearing_bits(cdns, __func__, false, 0);
1366 /* reset msg_count to default value of FIFOLEVEL */
1367 cdns->msg_count = cdns_readl(cdns, CDNS_MCP_FIFOLEVEL);
1369 /* flush command FIFOs */
1370 cdns_updatel(cdns, CDNS_MCP_CONTROL, CDNS_MCP_CONTROL_CMD_RST,
1371 CDNS_MCP_CONTROL_CMD_RST);
1373 /* Set cmd accept mode */
1374 cdns_ip_updatel(cdns, CDNS_IP_MCP_CONTROL, CDNS_IP_MCP_CONTROL_CMD_ACCEPT,
1375 CDNS_IP_MCP_CONTROL_CMD_ACCEPT);
1377 /* Configure mcp config */
1378 val = cdns_readl(cdns, CDNS_MCP_CONFIG);
1380 /* Disable auto bus release */
1381 val &= ~CDNS_MCP_CONFIG_BUS_REL;
1383 cdns_writel(cdns, CDNS_MCP_CONFIG, val);
1385 /* Configure IP mcp config */
1386 val = cdns_ip_readl(cdns, CDNS_IP_MCP_CONFIG);
1388 /* enable bus operations with clock and data */
1389 val &= ~CDNS_IP_MCP_CONFIG_OP;
1390 val |= CDNS_IP_MCP_CONFIG_OP_NORMAL;
1392 /* Set cmd mode for Tx and Rx cmds */
1393 val &= ~CDNS_IP_MCP_CONFIG_CMD;
1395 /* Disable sniffer mode */
1396 val &= ~CDNS_IP_MCP_CONFIG_SNIFFER;
1398 if (cdns->bus.multi_link)
1399 /* Set Multi-master mode to take gsync into account */
1400 val |= CDNS_IP_MCP_CONFIG_MMASTER;
1402 /* leave frame delay to hardware default of 0x1F */
1404 /* leave command retry to hardware default of 0 */
1406 cdns_ip_writel(cdns, CDNS_IP_MCP_CONFIG, val);
1408 /* changes will be committed later */
1411 EXPORT_SYMBOL(sdw_cdns_init);
1413 int cdns_bus_conf(struct sdw_bus *bus, struct sdw_bus_params *params)
1415 struct sdw_master_prop *prop = &bus->prop;
1416 struct sdw_cdns *cdns = bus_to_cdns(bus);
1417 int mcp_clkctrl_off;
1420 if (!params->curr_dr_freq) {
1421 dev_err(cdns->dev, "NULL curr_dr_freq\n");
1425 divider = prop->mclk_freq * SDW_DOUBLE_RATE_FACTOR /
1426 params->curr_dr_freq;
1427 divider--; /* divider is 1/(N+1) */
1429 if (params->next_bank)
1430 mcp_clkctrl_off = CDNS_MCP_CLK_CTRL1;
1432 mcp_clkctrl_off = CDNS_MCP_CLK_CTRL0;
1434 cdns_updatel(cdns, mcp_clkctrl_off, CDNS_MCP_CLK_MCLKD_MASK, divider);
1438 EXPORT_SYMBOL(cdns_bus_conf);
1440 static int cdns_port_params(struct sdw_bus *bus,
1441 struct sdw_port_params *p_params, unsigned int bank)
1443 struct sdw_cdns *cdns = bus_to_cdns(bus);
1444 int dpn_config_off_source;
1445 int dpn_config_off_target;
1446 int target_num = p_params->num;
1447 int source_num = p_params->num;
1448 bool override = false;
1451 if (target_num == cdns->pdi_loopback_target &&
1452 cdns->pdi_loopback_source != -1) {
1453 source_num = cdns->pdi_loopback_source;
1458 dpn_config_off_source = CDNS_DPN_B1_CONFIG(source_num);
1459 dpn_config_off_target = CDNS_DPN_B1_CONFIG(target_num);
1461 dpn_config_off_source = CDNS_DPN_B0_CONFIG(source_num);
1462 dpn_config_off_target = CDNS_DPN_B0_CONFIG(target_num);
1465 dpn_config = cdns_readl(cdns, dpn_config_off_source);
1467 /* use port params if there is no loopback, otherwise use source as is */
1469 u32p_replace_bits(&dpn_config, p_params->bps - 1, CDNS_DPN_CONFIG_WL);
1470 u32p_replace_bits(&dpn_config, p_params->flow_mode, CDNS_DPN_CONFIG_PORT_FLOW);
1471 u32p_replace_bits(&dpn_config, p_params->data_mode, CDNS_DPN_CONFIG_PORT_DAT);
1474 cdns_writel(cdns, dpn_config_off_target, dpn_config);
1479 static int cdns_transport_params(struct sdw_bus *bus,
1480 struct sdw_transport_params *t_params,
1481 enum sdw_reg_bank bank)
1483 struct sdw_cdns *cdns = bus_to_cdns(bus);
1485 int dpn_config_off_source;
1486 int dpn_config_off_target;
1488 int dpn_hctrl_off_source;
1489 int dpn_hctrl_off_target;
1491 int dpn_offsetctrl_off_source;
1492 int dpn_offsetctrl_off_target;
1494 int dpn_samplectrl_off_source;
1495 int dpn_samplectrl_off_target;
1496 int source_num = t_params->port_num;
1497 int target_num = t_params->port_num;
1498 bool override = false;
1500 if (target_num == cdns->pdi_loopback_target &&
1501 cdns->pdi_loopback_source != -1) {
1502 source_num = cdns->pdi_loopback_source;
1507 * Note: Only full data port is supported on the Master side for
1508 * both PCM and PDM ports.
1512 dpn_config_off_source = CDNS_DPN_B1_CONFIG(source_num);
1513 dpn_hctrl_off_source = CDNS_DPN_B1_HCTRL(source_num);
1514 dpn_offsetctrl_off_source = CDNS_DPN_B1_OFFSET_CTRL(source_num);
1515 dpn_samplectrl_off_source = CDNS_DPN_B1_SAMPLE_CTRL(source_num);
1517 dpn_config_off_target = CDNS_DPN_B1_CONFIG(target_num);
1518 dpn_hctrl_off_target = CDNS_DPN_B1_HCTRL(target_num);
1519 dpn_offsetctrl_off_target = CDNS_DPN_B1_OFFSET_CTRL(target_num);
1520 dpn_samplectrl_off_target = CDNS_DPN_B1_SAMPLE_CTRL(target_num);
1523 dpn_config_off_source = CDNS_DPN_B0_CONFIG(source_num);
1524 dpn_hctrl_off_source = CDNS_DPN_B0_HCTRL(source_num);
1525 dpn_offsetctrl_off_source = CDNS_DPN_B0_OFFSET_CTRL(source_num);
1526 dpn_samplectrl_off_source = CDNS_DPN_B0_SAMPLE_CTRL(source_num);
1528 dpn_config_off_target = CDNS_DPN_B0_CONFIG(target_num);
1529 dpn_hctrl_off_target = CDNS_DPN_B0_HCTRL(target_num);
1530 dpn_offsetctrl_off_target = CDNS_DPN_B0_OFFSET_CTRL(target_num);
1531 dpn_samplectrl_off_target = CDNS_DPN_B0_SAMPLE_CTRL(target_num);
1534 dpn_config = cdns_readl(cdns, dpn_config_off_source);
1536 u32p_replace_bits(&dpn_config, t_params->blk_grp_ctrl, CDNS_DPN_CONFIG_BGC);
1537 u32p_replace_bits(&dpn_config, t_params->blk_pkg_mode, CDNS_DPN_CONFIG_BPM);
1539 cdns_writel(cdns, dpn_config_off_target, dpn_config);
1543 u32p_replace_bits(&dpn_offsetctrl, t_params->offset1, CDNS_DPN_OFFSET_CTRL_1);
1544 u32p_replace_bits(&dpn_offsetctrl, t_params->offset2, CDNS_DPN_OFFSET_CTRL_2);
1546 dpn_offsetctrl = cdns_readl(cdns, dpn_offsetctrl_off_source);
1548 cdns_writel(cdns, dpn_offsetctrl_off_target, dpn_offsetctrl);
1552 u32p_replace_bits(&dpn_hctrl, t_params->hstart, CDNS_DPN_HCTRL_HSTART);
1553 u32p_replace_bits(&dpn_hctrl, t_params->hstop, CDNS_DPN_HCTRL_HSTOP);
1554 u32p_replace_bits(&dpn_hctrl, t_params->lane_ctrl, CDNS_DPN_HCTRL_LCTRL);
1556 dpn_hctrl = cdns_readl(cdns, dpn_hctrl_off_source);
1558 cdns_writel(cdns, dpn_hctrl_off_target, dpn_hctrl);
1561 dpn_samplectrl = t_params->sample_interval - 1;
1563 dpn_samplectrl = cdns_readl(cdns, dpn_samplectrl_off_source);
1564 cdns_writel(cdns, dpn_samplectrl_off_target, dpn_samplectrl);
1569 static int cdns_port_enable(struct sdw_bus *bus,
1570 struct sdw_enable_ch *enable_ch, unsigned int bank)
1572 struct sdw_cdns *cdns = bus_to_cdns(bus);
1573 int dpn_chnen_off, ch_mask;
1576 dpn_chnen_off = CDNS_DPN_B1_CH_EN(enable_ch->port_num);
1578 dpn_chnen_off = CDNS_DPN_B0_CH_EN(enable_ch->port_num);
1580 ch_mask = enable_ch->ch_mask * enable_ch->enable;
1581 cdns_writel(cdns, dpn_chnen_off, ch_mask);
1586 static const struct sdw_master_port_ops cdns_port_ops = {
1587 .dpn_set_port_params = cdns_port_params,
1588 .dpn_set_port_transport_params = cdns_transport_params,
1589 .dpn_port_enable_ch = cdns_port_enable,
1593 * sdw_cdns_is_clock_stop: Check clock status
1595 * @cdns: Cadence instance
1597 bool sdw_cdns_is_clock_stop(struct sdw_cdns *cdns)
1599 return !!(cdns_readl(cdns, CDNS_MCP_STAT) & CDNS_MCP_STAT_CLK_STOP);
1601 EXPORT_SYMBOL(sdw_cdns_is_clock_stop);
1604 * sdw_cdns_clock_stop: Cadence clock stop configuration routine
1606 * @cdns: Cadence instance
1607 * @block_wake: prevent wakes if required by the platform
1609 int sdw_cdns_clock_stop(struct sdw_cdns *cdns, bool block_wake)
1611 bool slave_present = false;
1612 struct sdw_slave *slave;
1615 sdw_cdns_check_self_clearing_bits(cdns, __func__, false, 0);
1617 /* Check suspend status */
1618 if (sdw_cdns_is_clock_stop(cdns)) {
1619 dev_dbg(cdns->dev, "Clock is already stopped\n");
1624 * Before entering clock stop we mask the Slave
1625 * interrupts. This helps avoid having to deal with e.g. a
1626 * Slave becoming UNATTACHED while the clock is being stopped
1628 cdns_enable_slave_interrupts(cdns, false);
1631 * For specific platforms, it is required to be able to put
1632 * master into a state in which it ignores wake-up trials
1633 * in clock stop state
1636 cdns_ip_updatel(cdns, CDNS_IP_MCP_CONTROL,
1637 CDNS_IP_MCP_CONTROL_BLOCK_WAKEUP,
1638 CDNS_IP_MCP_CONTROL_BLOCK_WAKEUP);
1640 list_for_each_entry(slave, &cdns->bus.slaves, node) {
1641 if (slave->status == SDW_SLAVE_ATTACHED ||
1642 slave->status == SDW_SLAVE_ALERT) {
1643 slave_present = true;
1648 /* commit changes */
1649 ret = cdns_config_update(cdns);
1651 dev_err(cdns->dev, "%s: config_update failed\n", __func__);
1655 /* Prepare slaves for clock stop */
1656 if (slave_present) {
1657 ret = sdw_bus_prep_clk_stop(&cdns->bus);
1658 if (ret < 0 && ret != -ENODATA) {
1659 dev_err(cdns->dev, "prepare clock stop failed %d\n", ret);
1665 * Enter clock stop mode and only report errors if there are
1666 * Slave devices present (ALERT or ATTACHED)
1668 ret = sdw_bus_clk_stop(&cdns->bus);
1669 if (ret < 0 && slave_present && ret != -ENODATA) {
1670 dev_err(cdns->dev, "bus clock stop failed %d\n", ret);
1674 ret = cdns_set_wait(cdns, CDNS_MCP_STAT,
1675 CDNS_MCP_STAT_CLK_STOP,
1676 CDNS_MCP_STAT_CLK_STOP);
1678 dev_err(cdns->dev, "Clock stop failed %d\n", ret);
1682 EXPORT_SYMBOL(sdw_cdns_clock_stop);
1685 * sdw_cdns_clock_restart: Cadence PM clock restart configuration routine
1687 * @cdns: Cadence instance
1688 * @bus_reset: context may be lost while in low power modes and the bus
1689 * may require a Severe Reset and re-enumeration after a wake.
1691 int sdw_cdns_clock_restart(struct sdw_cdns *cdns, bool bus_reset)
1695 /* unmask Slave interrupts that were masked when stopping the clock */
1696 cdns_enable_slave_interrupts(cdns, true);
1698 ret = cdns_clear_bit(cdns, CDNS_MCP_CONTROL,
1699 CDNS_MCP_CONTROL_CLK_STOP_CLR);
1701 dev_err(cdns->dev, "Couldn't exit from clock stop\n");
1705 ret = cdns_set_wait(cdns, CDNS_MCP_STAT, CDNS_MCP_STAT_CLK_STOP, 0);
1707 dev_err(cdns->dev, "clock stop exit failed %d\n", ret);
1711 cdns_ip_updatel(cdns, CDNS_IP_MCP_CONTROL,
1712 CDNS_IP_MCP_CONTROL_BLOCK_WAKEUP, 0);
1714 cdns_ip_updatel(cdns, CDNS_IP_MCP_CONTROL, CDNS_IP_MCP_CONTROL_CMD_ACCEPT,
1715 CDNS_IP_MCP_CONTROL_CMD_ACCEPT);
1719 /* enable bus operations with clock and data */
1720 cdns_ip_updatel(cdns, CDNS_IP_MCP_CONFIG,
1721 CDNS_IP_MCP_CONFIG_OP,
1722 CDNS_IP_MCP_CONFIG_OP_NORMAL);
1724 ret = cdns_config_update(cdns);
1726 dev_err(cdns->dev, "%s: config_update failed\n", __func__);
1730 ret = sdw_bus_exit_clk_stop(&cdns->bus);
1732 dev_err(cdns->dev, "bus failed to exit clock stop %d\n", ret);
1737 EXPORT_SYMBOL(sdw_cdns_clock_restart);
1740 * sdw_cdns_probe() - Cadence probe routine
1741 * @cdns: Cadence instance
1743 int sdw_cdns_probe(struct sdw_cdns *cdns)
1745 init_completion(&cdns->tx_complete);
1746 cdns->bus.port_ops = &cdns_port_ops;
1748 INIT_WORK(&cdns->work, cdns_update_slave_status_work);
1751 EXPORT_SYMBOL(sdw_cdns_probe);
1753 int cdns_set_sdw_stream(struct snd_soc_dai *dai,
1754 void *stream, int direction)
1756 struct sdw_cdns *cdns = snd_soc_dai_get_drvdata(dai);
1757 struct sdw_cdns_dai_runtime *dai_runtime;
1759 dai_runtime = cdns->dai_runtime_array[dai->id];
1762 /* first paranoia check */
1765 "dai_runtime already allocated for dai %s\n",
1770 /* allocate and set dai_runtime info */
1771 dai_runtime = kzalloc(sizeof(*dai_runtime), GFP_KERNEL);
1775 dai_runtime->stream_type = SDW_STREAM_PCM;
1777 dai_runtime->bus = &cdns->bus;
1778 dai_runtime->link_id = cdns->instance;
1780 dai_runtime->stream = stream;
1781 dai_runtime->direction = direction;
1783 cdns->dai_runtime_array[dai->id] = dai_runtime;
1785 /* second paranoia check */
1788 "dai_runtime not allocated for dai %s\n",
1793 /* for NULL stream we release allocated dai_runtime */
1795 cdns->dai_runtime_array[dai->id] = NULL;
1799 EXPORT_SYMBOL(cdns_set_sdw_stream);
1802 * cdns_find_pdi() - Find a free PDI
1804 * @cdns: Cadence instance
1805 * @offset: Starting offset
1806 * @num: Number of PDIs
1807 * @pdi: PDI instances
1810 * Find a PDI for a given PDI array. The PDI num and dai_id are
1811 * expected to match, return NULL otherwise.
1813 static struct sdw_cdns_pdi *cdns_find_pdi(struct sdw_cdns *cdns,
1814 unsigned int offset,
1816 struct sdw_cdns_pdi *pdi,
1821 for (i = offset; i < offset + num; i++)
1822 if (pdi[i].num == dai_id)
1829 * sdw_cdns_config_stream: Configure a stream
1831 * @cdns: Cadence instance
1832 * @ch: Channel count
1833 * @dir: Data direction
1834 * @pdi: PDI to be used
1836 void sdw_cdns_config_stream(struct sdw_cdns *cdns,
1837 u32 ch, u32 dir, struct sdw_cdns_pdi *pdi)
1839 u32 offset, val = 0;
1841 if (dir == SDW_DATA_DIR_RX) {
1842 val = CDNS_PORTCTRL_DIRN;
1844 if (cdns->bus.params.m_data_mode != SDW_PORT_DATA_MODE_NORMAL)
1845 val |= CDNS_PORTCTRL_TEST_FAILED;
1847 offset = CDNS_PORTCTRL + pdi->num * CDNS_PORT_OFFSET;
1848 cdns_updatel(cdns, offset,
1849 CDNS_PORTCTRL_DIRN | CDNS_PORTCTRL_TEST_FAILED,
1853 val |= CDNS_PDI_CONFIG_SOFT_RESET;
1854 val |= FIELD_PREP(CDNS_PDI_CONFIG_CHANNEL, (1 << ch) - 1);
1855 cdns_writel(cdns, CDNS_PDI_CONFIG(pdi->num), val);
1857 EXPORT_SYMBOL(sdw_cdns_config_stream);
1860 * sdw_cdns_alloc_pdi() - Allocate a PDI
1862 * @cdns: Cadence instance
1863 * @stream: Stream to be allocated
1864 * @ch: Channel count
1865 * @dir: Data direction
1868 struct sdw_cdns_pdi *sdw_cdns_alloc_pdi(struct sdw_cdns *cdns,
1869 struct sdw_cdns_streams *stream,
1870 u32 ch, u32 dir, int dai_id)
1872 struct sdw_cdns_pdi *pdi = NULL;
1874 if (dir == SDW_DATA_DIR_RX)
1875 pdi = cdns_find_pdi(cdns, 0, stream->num_in, stream->in,
1878 pdi = cdns_find_pdi(cdns, 0, stream->num_out, stream->out,
1881 /* check if we found a PDI, else find in bi-directional */
1883 pdi = cdns_find_pdi(cdns, 2, stream->num_bd, stream->bd,
1888 pdi->h_ch_num = ch - 1;
1895 EXPORT_SYMBOL(sdw_cdns_alloc_pdi);
1897 MODULE_LICENSE("Dual BSD/GPL");
1898 MODULE_DESCRIPTION("Cadence Soundwire Library");