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/cleanup.h>
10 #include <linux/delay.h>
11 #include <linux/device.h>
12 #include <linux/debugfs.h>
13 #include <linux/interrupt.h>
15 #include <linux/module.h>
16 #include <linux/mod_devicetable.h>
17 #include <linux/pm_runtime.h>
18 #include <linux/soundwire/sdw_registers.h>
19 #include <linux/soundwire/sdw.h>
20 #include <sound/pcm_params.h>
21 #include <sound/soc.h>
22 #include <linux/workqueue.h>
24 #include "cadence_master.h"
26 static int interrupt_mask
;
27 module_param_named(cnds_mcp_int_mask
, interrupt_mask
, int, 0444);
28 MODULE_PARM_DESC(cdns_mcp_int_mask
, "Cadence MCP IntMask");
30 #define CDNS_MCP_CONFIG 0x0
31 #define CDNS_MCP_CONFIG_BUS_REL BIT(6)
33 #define CDNS_IP_MCP_CONFIG 0x0 /* IP offset added at run-time */
35 #define CDNS_IP_MCP_CONFIG_MCMD_RETRY GENMASK(27, 24)
36 #define CDNS_IP_MCP_CONFIG_MPREQ_DELAY GENMASK(20, 16)
37 #define CDNS_IP_MCP_CONFIG_MMASTER BIT(7)
38 #define CDNS_IP_MCP_CONFIG_SNIFFER BIT(5)
39 #define CDNS_IP_MCP_CONFIG_CMD BIT(3)
40 #define CDNS_IP_MCP_CONFIG_OP GENMASK(2, 0)
41 #define CDNS_IP_MCP_CONFIG_OP_NORMAL 0
43 #define CDNS_MCP_CONTROL 0x4
45 #define CDNS_MCP_CONTROL_CMD_RST BIT(7)
46 #define CDNS_MCP_CONTROL_SOFT_RST BIT(6)
47 #define CDNS_MCP_CONTROL_HW_RST BIT(4)
48 #define CDNS_MCP_CONTROL_CLK_STOP_CLR BIT(2)
50 #define CDNS_IP_MCP_CONTROL 0x4 /* IP offset added at run-time */
52 #define CDNS_IP_MCP_CONTROL_RST_DELAY GENMASK(10, 8)
53 #define CDNS_IP_MCP_CONTROL_SW_RST BIT(5)
54 #define CDNS_IP_MCP_CONTROL_CLK_PAUSE BIT(3)
55 #define CDNS_IP_MCP_CONTROL_CMD_ACCEPT BIT(1)
56 #define CDNS_IP_MCP_CONTROL_BLOCK_WAKEUP BIT(0)
58 #define CDNS_IP_MCP_CMDCTRL 0x8 /* IP offset added at run-time */
60 #define CDNS_IP_MCP_CMDCTRL_INSERT_PARITY_ERR BIT(2)
62 #define CDNS_MCP_SSPSTAT 0xC
63 #define CDNS_MCP_FRAME_SHAPE 0x10
64 #define CDNS_MCP_FRAME_SHAPE_INIT 0x14
65 #define CDNS_MCP_FRAME_SHAPE_COL_MASK GENMASK(2, 0)
66 #define CDNS_MCP_FRAME_SHAPE_ROW_MASK GENMASK(7, 3)
68 #define CDNS_MCP_CONFIG_UPDATE 0x18
69 #define CDNS_MCP_CONFIG_UPDATE_BIT BIT(0)
71 #define CDNS_MCP_PHYCTRL 0x1C
72 #define CDNS_MCP_SSP_CTRL0 0x20
73 #define CDNS_MCP_SSP_CTRL1 0x28
74 #define CDNS_MCP_CLK_CTRL0 0x30
75 #define CDNS_MCP_CLK_CTRL1 0x38
76 #define CDNS_MCP_CLK_MCLKD_MASK GENMASK(7, 0)
78 #define CDNS_MCP_STAT 0x40
80 #define CDNS_MCP_STAT_ACTIVE_BANK BIT(20)
81 #define CDNS_MCP_STAT_CLK_STOP BIT(16)
83 #define CDNS_MCP_INTSTAT 0x44
84 #define CDNS_MCP_INTMASK 0x48
86 #define CDNS_MCP_INT_IRQ BIT(31)
87 #define CDNS_MCP_INT_RESERVED1 GENMASK(30, 17)
88 #define CDNS_MCP_INT_WAKEUP BIT(16)
89 #define CDNS_MCP_INT_SLAVE_RSVD BIT(15)
90 #define CDNS_MCP_INT_SLAVE_ALERT BIT(14)
91 #define CDNS_MCP_INT_SLAVE_ATTACH BIT(13)
92 #define CDNS_MCP_INT_SLAVE_NATTACH BIT(12)
93 #define CDNS_MCP_INT_SLAVE_MASK GENMASK(15, 12)
94 #define CDNS_MCP_INT_DPINT BIT(11)
95 #define CDNS_MCP_INT_CTRL_CLASH BIT(10)
96 #define CDNS_MCP_INT_DATA_CLASH BIT(9)
97 #define CDNS_MCP_INT_PARITY BIT(8)
98 #define CDNS_MCP_INT_CMD_ERR BIT(7)
99 #define CDNS_MCP_INT_RESERVED2 GENMASK(6, 4)
100 #define CDNS_MCP_INT_RX_NE BIT(3)
101 #define CDNS_MCP_INT_RX_WL BIT(2)
102 #define CDNS_MCP_INT_TXE BIT(1)
103 #define CDNS_MCP_INT_TXF BIT(0)
104 #define CDNS_MCP_INT_RESERVED (CDNS_MCP_INT_RESERVED1 | CDNS_MCP_INT_RESERVED2)
106 #define CDNS_MCP_INTSET 0x4C
108 #define CDNS_MCP_SLAVE_STAT 0x50
109 #define CDNS_MCP_SLAVE_STAT_MASK GENMASK(1, 0)
111 #define CDNS_MCP_SLAVE_INTSTAT0 0x54
112 #define CDNS_MCP_SLAVE_INTSTAT1 0x58
113 #define CDNS_MCP_SLAVE_INTSTAT_NPRESENT BIT(0)
114 #define CDNS_MCP_SLAVE_INTSTAT_ATTACHED BIT(1)
115 #define CDNS_MCP_SLAVE_INTSTAT_ALERT BIT(2)
116 #define CDNS_MCP_SLAVE_INTSTAT_RESERVED BIT(3)
117 #define CDNS_MCP_SLAVE_STATUS_BITS GENMASK(3, 0)
118 #define CDNS_MCP_SLAVE_STATUS_NUM 4
120 #define CDNS_MCP_SLAVE_INTMASK0 0x5C
121 #define CDNS_MCP_SLAVE_INTMASK1 0x60
123 #define CDNS_MCP_SLAVE_INTMASK0_MASK GENMASK(31, 0)
124 #define CDNS_MCP_SLAVE_INTMASK1_MASK GENMASK(15, 0)
126 #define CDNS_MCP_PORT_INTSTAT 0x64
127 #define CDNS_MCP_PDI_STAT 0x6C
129 #define CDNS_MCP_FIFOLEVEL 0x78
130 #define CDNS_MCP_FIFOSTAT 0x7C
131 #define CDNS_MCP_RX_FIFO_AVAIL GENMASK(5, 0)
133 #define CDNS_IP_MCP_CMD_BASE 0x80 /* IP offset added at run-time */
134 #define CDNS_IP_MCP_RESP_BASE 0x80 /* IP offset added at run-time */
135 /* FIFO can hold 8 commands */
136 #define CDNS_MCP_CMD_LEN 8
137 #define CDNS_MCP_CMD_WORD_LEN 0x4
139 #define CDNS_MCP_CMD_SSP_TAG BIT(31)
140 #define CDNS_MCP_CMD_COMMAND GENMASK(30, 28)
141 #define CDNS_MCP_CMD_DEV_ADDR GENMASK(27, 24)
142 #define CDNS_MCP_CMD_REG_ADDR GENMASK(23, 8)
143 #define CDNS_MCP_CMD_REG_DATA GENMASK(7, 0)
145 #define CDNS_MCP_CMD_READ 2
146 #define CDNS_MCP_CMD_WRITE 3
148 #define CDNS_MCP_RESP_RDATA GENMASK(15, 8)
149 #define CDNS_MCP_RESP_ACK BIT(0)
150 #define CDNS_MCP_RESP_NACK BIT(1)
152 #define CDNS_DP_SIZE 128
154 #define CDNS_DPN_B0_CONFIG(n) (0x100 + CDNS_DP_SIZE * (n))
155 #define CDNS_DPN_B0_CH_EN(n) (0x104 + CDNS_DP_SIZE * (n))
156 #define CDNS_DPN_B0_SAMPLE_CTRL(n) (0x108 + CDNS_DP_SIZE * (n))
157 #define CDNS_DPN_B0_OFFSET_CTRL(n) (0x10C + CDNS_DP_SIZE * (n))
158 #define CDNS_DPN_B0_HCTRL(n) (0x110 + CDNS_DP_SIZE * (n))
159 #define CDNS_DPN_B0_ASYNC_CTRL(n) (0x114 + CDNS_DP_SIZE * (n))
161 #define CDNS_DPN_B1_CONFIG(n) (0x118 + CDNS_DP_SIZE * (n))
162 #define CDNS_DPN_B1_CH_EN(n) (0x11C + CDNS_DP_SIZE * (n))
163 #define CDNS_DPN_B1_SAMPLE_CTRL(n) (0x120 + CDNS_DP_SIZE * (n))
164 #define CDNS_DPN_B1_OFFSET_CTRL(n) (0x124 + CDNS_DP_SIZE * (n))
165 #define CDNS_DPN_B1_HCTRL(n) (0x128 + CDNS_DP_SIZE * (n))
166 #define CDNS_DPN_B1_ASYNC_CTRL(n) (0x12C + CDNS_DP_SIZE * (n))
168 #define CDNS_DPN_CONFIG_BPM BIT(18)
169 #define CDNS_DPN_CONFIG_BGC GENMASK(17, 16)
170 #define CDNS_DPN_CONFIG_WL GENMASK(12, 8)
171 #define CDNS_DPN_CONFIG_PORT_DAT GENMASK(3, 2)
172 #define CDNS_DPN_CONFIG_PORT_FLOW GENMASK(1, 0)
174 #define CDNS_DPN_SAMPLE_CTRL_SI GENMASK(15, 0)
176 #define CDNS_DPN_OFFSET_CTRL_1 GENMASK(7, 0)
177 #define CDNS_DPN_OFFSET_CTRL_2 GENMASK(15, 8)
179 #define CDNS_DPN_HCTRL_HSTOP GENMASK(3, 0)
180 #define CDNS_DPN_HCTRL_HSTART GENMASK(7, 4)
181 #define CDNS_DPN_HCTRL_LCTRL GENMASK(10, 8)
183 #define CDNS_PORTCTRL 0x130
184 #define CDNS_PORTCTRL_TEST_FAILED BIT(1)
185 #define CDNS_PORTCTRL_DIRN BIT(7)
186 #define CDNS_PORTCTRL_BANK_INVERT BIT(8)
188 #define CDNS_PORT_OFFSET 0x80
190 #define CDNS_PDI_CONFIG(n) (0x1100 + (n) * 16)
192 #define CDNS_PDI_CONFIG_SOFT_RESET BIT(24)
193 #define CDNS_PDI_CONFIG_CHANNEL GENMASK(15, 8)
194 #define CDNS_PDI_CONFIG_PORT GENMASK(4, 0)
196 /* Driver defaults */
197 #define CDNS_TX_TIMEOUT 500
199 #define CDNS_SCP_RX_FIFOLEVEL 0x2
202 * register accessor helpers
204 static inline u32
cdns_readl(struct sdw_cdns
*cdns
, int offset
)
206 return readl(cdns
->registers
+ offset
);
209 static inline void cdns_writel(struct sdw_cdns
*cdns
, int offset
, u32 value
)
211 writel(value
, cdns
->registers
+ offset
);
214 static inline u32
cdns_ip_readl(struct sdw_cdns
*cdns
, int offset
)
216 return cdns_readl(cdns
, cdns
->ip_offset
+ offset
);
219 static inline void cdns_ip_writel(struct sdw_cdns
*cdns
, int offset
, u32 value
)
221 return cdns_writel(cdns
, cdns
->ip_offset
+ offset
, value
);
224 static inline void cdns_updatel(struct sdw_cdns
*cdns
,
225 int offset
, u32 mask
, u32 val
)
229 tmp
= cdns_readl(cdns
, offset
);
230 tmp
= (tmp
& ~mask
) | val
;
231 cdns_writel(cdns
, offset
, tmp
);
234 static inline void cdns_ip_updatel(struct sdw_cdns
*cdns
,
235 int offset
, u32 mask
, u32 val
)
237 cdns_updatel(cdns
, cdns
->ip_offset
+ offset
, mask
, val
);
240 static int cdns_set_wait(struct sdw_cdns
*cdns
, int offset
, u32 mask
, u32 value
)
245 /* Wait for bit to be set */
247 reg_read
= readl(cdns
->registers
+ offset
);
248 if ((reg_read
& mask
) == value
)
252 usleep_range(50, 100);
253 } while (timeout
!= 0);
258 static int cdns_clear_bit(struct sdw_cdns
*cdns
, int offset
, u32 value
)
260 writel(value
, cdns
->registers
+ offset
);
262 /* Wait for bit to be self cleared */
263 return cdns_set_wait(cdns
, offset
, value
, 0);
267 * all changes to the MCP_CONFIG, MCP_CONTROL, MCP_CMDCTRL and MCP_PHYCTRL
268 * need to be confirmed with a write to MCP_CONFIG_UPDATE
270 static int cdns_config_update(struct sdw_cdns
*cdns
)
274 if (sdw_cdns_is_clock_stop(cdns
)) {
275 dev_err(cdns
->dev
, "Cannot program MCP_CONFIG_UPDATE in ClockStopMode\n");
279 ret
= cdns_clear_bit(cdns
, CDNS_MCP_CONFIG_UPDATE
,
280 CDNS_MCP_CONFIG_UPDATE_BIT
);
282 dev_err(cdns
->dev
, "Config update timedout\n");
288 * sdw_cdns_config_update() - Update configurations
289 * @cdns: Cadence instance
291 void sdw_cdns_config_update(struct sdw_cdns
*cdns
)
294 cdns_writel(cdns
, CDNS_MCP_CONFIG_UPDATE
, CDNS_MCP_CONFIG_UPDATE_BIT
);
296 EXPORT_SYMBOL(sdw_cdns_config_update
);
299 * sdw_cdns_config_update_set_wait() - wait until configuration update bit is self-cleared
300 * @cdns: Cadence instance
302 int sdw_cdns_config_update_set_wait(struct sdw_cdns
*cdns
)
304 /* the hardware recommendation is to wait at least 300us */
305 return cdns_set_wait(cdns
, CDNS_MCP_CONFIG_UPDATE
,
306 CDNS_MCP_CONFIG_UPDATE_BIT
, 0);
308 EXPORT_SYMBOL(sdw_cdns_config_update_set_wait
);
313 #ifdef CONFIG_DEBUG_FS
315 #define RD_BUF (2 * PAGE_SIZE)
317 static ssize_t
cdns_sprintf(struct sdw_cdns
*cdns
,
318 char *buf
, size_t pos
, unsigned int reg
)
320 return scnprintf(buf
+ pos
, RD_BUF
- pos
,
321 "%4x\t%8x\n", reg
, cdns_readl(cdns
, reg
));
324 static int cdns_reg_show(struct seq_file
*s
, void *data
)
326 struct sdw_cdns
*cdns
= s
->private;
331 char *buf
__free(kfree
) = 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
);
395 DEFINE_SHOW_ATTRIBUTE(cdns_reg
);
397 static int cdns_hw_reset(void *data
, u64 value
)
399 struct sdw_cdns
*cdns
= data
;
405 /* Userspace changed the hardware state behind the kernel's back */
406 add_taint(TAINT_USER
, LOCKDEP_STILL_OK
);
408 ret
= sdw_cdns_exit_reset(cdns
);
410 dev_dbg(cdns
->dev
, "link hw_reset done: %d\n", ret
);
415 DEFINE_DEBUGFS_ATTRIBUTE(cdns_hw_reset_fops
, NULL
, cdns_hw_reset
, "%llu\n");
417 static int cdns_parity_error_injection(void *data
, u64 value
)
419 struct sdw_cdns
*cdns
= data
;
429 * Resume Master device. If this results in a bus reset, the
430 * Slave devices will re-attach and be re-enumerated.
432 ret
= pm_runtime_resume_and_get(bus
->dev
);
433 if (ret
< 0 && ret
!= -EACCES
) {
434 dev_err_ratelimited(cdns
->dev
,
435 "pm_runtime_resume_and_get failed in %s, ret %d\n",
441 * wait long enough for Slave(s) to be in steady state. This
442 * does not need to be super precise.
447 * Take the bus lock here to make sure that any bus transactions
448 * will be queued while we inject a parity error on a dummy read
450 mutex_lock(&bus
->bus_lock
);
452 /* program hardware to inject parity error */
453 cdns_ip_updatel(cdns
, CDNS_IP_MCP_CMDCTRL
,
454 CDNS_IP_MCP_CMDCTRL_INSERT_PARITY_ERR
,
455 CDNS_IP_MCP_CMDCTRL_INSERT_PARITY_ERR
);
458 ret
= cdns_clear_bit(cdns
, CDNS_MCP_CONFIG_UPDATE
, CDNS_MCP_CONFIG_UPDATE_BIT
);
462 /* do a broadcast dummy read to avoid bus clashes */
463 ret
= sdw_bread_no_pm_unlocked(&cdns
->bus
, 0xf, SDW_SCP_DEVID_0
);
464 dev_info(cdns
->dev
, "parity error injection, read: %d\n", ret
);
466 /* program hardware to disable parity error */
467 cdns_ip_updatel(cdns
, CDNS_IP_MCP_CMDCTRL
,
468 CDNS_IP_MCP_CMDCTRL_INSERT_PARITY_ERR
,
472 ret
= cdns_clear_bit(cdns
, CDNS_MCP_CONFIG_UPDATE
, CDNS_MCP_CONFIG_UPDATE_BIT
);
476 /* Userspace changed the hardware state behind the kernel's back */
477 add_taint(TAINT_USER
, LOCKDEP_STILL_OK
);
480 /* Continue bus operation with parity error injection disabled */
481 mutex_unlock(&bus
->bus_lock
);
484 * allow Master device to enter pm_runtime suspend. This may
485 * also result in Slave devices suspending.
487 pm_runtime_mark_last_busy(bus
->dev
);
488 pm_runtime_put_autosuspend(bus
->dev
);
493 DEFINE_DEBUGFS_ATTRIBUTE(cdns_parity_error_fops
, NULL
,
494 cdns_parity_error_injection
, "%llu\n");
496 static int cdns_set_pdi_loopback_source(void *data
, u64 value
)
498 struct sdw_cdns
*cdns
= data
;
499 unsigned int pdi_out_num
= cdns
->pcm
.num_bd
+ cdns
->pcm
.num_out
;
501 if (value
> pdi_out_num
)
504 /* Userspace changed the hardware state behind the kernel's back */
505 add_taint(TAINT_USER
, LOCKDEP_STILL_OK
);
507 cdns
->pdi_loopback_source
= value
;
511 DEFINE_DEBUGFS_ATTRIBUTE(cdns_pdi_loopback_source_fops
, NULL
, cdns_set_pdi_loopback_source
, "%llu\n");
513 static int cdns_set_pdi_loopback_target(void *data
, u64 value
)
515 struct sdw_cdns
*cdns
= data
;
516 unsigned int pdi_in_num
= cdns
->pcm
.num_bd
+ cdns
->pcm
.num_in
;
518 if (value
> pdi_in_num
)
521 /* Userspace changed the hardware state behind the kernel's back */
522 add_taint(TAINT_USER
, LOCKDEP_STILL_OK
);
524 cdns
->pdi_loopback_target
= value
;
528 DEFINE_DEBUGFS_ATTRIBUTE(cdns_pdi_loopback_target_fops
, NULL
, cdns_set_pdi_loopback_target
, "%llu\n");
531 * sdw_cdns_debugfs_init() - Cadence debugfs init
532 * @cdns: Cadence instance
533 * @root: debugfs root
535 void sdw_cdns_debugfs_init(struct sdw_cdns
*cdns
, struct dentry
*root
)
537 debugfs_create_file("cdns-registers", 0400, root
, cdns
, &cdns_reg_fops
);
539 debugfs_create_file("cdns-hw-reset", 0200, root
, cdns
,
540 &cdns_hw_reset_fops
);
542 debugfs_create_file("cdns-parity-error-injection", 0200, root
, cdns
,
543 &cdns_parity_error_fops
);
545 cdns
->pdi_loopback_source
= -1;
546 cdns
->pdi_loopback_target
= -1;
548 debugfs_create_file("cdns-pdi-loopback-source", 0200, root
, cdns
,
549 &cdns_pdi_loopback_source_fops
);
551 debugfs_create_file("cdns-pdi-loopback-target", 0200, root
, cdns
,
552 &cdns_pdi_loopback_target_fops
);
555 EXPORT_SYMBOL_GPL(sdw_cdns_debugfs_init
);
557 #endif /* CONFIG_DEBUG_FS */
562 static enum sdw_command_response
563 cdns_fill_msg_resp(struct sdw_cdns
*cdns
,
564 struct sdw_msg
*msg
, int count
, int offset
)
566 int nack
= 0, no_ack
= 0;
569 /* check message response */
570 for (i
= 0; i
< count
; i
++) {
571 if (!(cdns
->response_buf
[i
] & CDNS_MCP_RESP_ACK
)) {
573 dev_vdbg(cdns
->dev
, "Msg Ack not received, cmd %d\n", i
);
575 if (cdns
->response_buf
[i
] & CDNS_MCP_RESP_NACK
) {
577 dev_err_ratelimited(cdns
->dev
, "Msg NACK received, cmd %d\n", i
);
582 dev_err_ratelimited(cdns
->dev
, "Msg NACKed for Slave %d\n", msg
->dev_num
);
587 dev_dbg_ratelimited(cdns
->dev
, "Msg ignored for Slave %d\n", msg
->dev_num
);
588 return SDW_CMD_IGNORED
;
591 if (msg
->flags
== SDW_MSG_FLAG_READ
) {
593 for (i
= 0; i
< count
; i
++)
594 msg
->buf
[i
+ offset
] = FIELD_GET(CDNS_MCP_RESP_RDATA
,
595 cdns
->response_buf
[i
]);
601 static void cdns_read_response(struct sdw_cdns
*cdns
)
603 u32 num_resp
, cmd_base
;
606 /* RX_FIFO_AVAIL can be 2 entries more than the FIFO size */
607 BUILD_BUG_ON(ARRAY_SIZE(cdns
->response_buf
) < CDNS_MCP_CMD_LEN
+ 2);
609 num_resp
= cdns_readl(cdns
, CDNS_MCP_FIFOSTAT
);
610 num_resp
&= CDNS_MCP_RX_FIFO_AVAIL
;
611 if (num_resp
> ARRAY_SIZE(cdns
->response_buf
)) {
612 dev_warn(cdns
->dev
, "RX AVAIL %d too long\n", num_resp
);
613 num_resp
= ARRAY_SIZE(cdns
->response_buf
);
616 cmd_base
= CDNS_IP_MCP_CMD_BASE
;
618 for (i
= 0; i
< num_resp
; i
++) {
619 cdns
->response_buf
[i
] = cdns_ip_readl(cdns
, cmd_base
);
620 cmd_base
+= CDNS_MCP_CMD_WORD_LEN
;
624 static enum sdw_command_response
625 _cdns_xfer_msg(struct sdw_cdns
*cdns
, struct sdw_msg
*msg
, int cmd
,
626 int offset
, int count
, bool defer
)
632 /* Program the watermark level for RX FIFO */
633 if (cdns
->msg_count
!= count
) {
634 cdns_writel(cdns
, CDNS_MCP_FIFOLEVEL
, count
);
635 cdns
->msg_count
= count
;
638 base
= CDNS_IP_MCP_CMD_BASE
;
639 addr
= msg
->addr
+ offset
;
641 for (i
= 0; i
< count
; i
++) {
642 data
= FIELD_PREP(CDNS_MCP_CMD_DEV_ADDR
, msg
->dev_num
);
643 data
|= FIELD_PREP(CDNS_MCP_CMD_COMMAND
, cmd
);
644 data
|= FIELD_PREP(CDNS_MCP_CMD_REG_ADDR
, addr
);
647 if (msg
->flags
== SDW_MSG_FLAG_WRITE
)
648 data
|= msg
->buf
[i
+ offset
];
650 data
|= FIELD_PREP(CDNS_MCP_CMD_SSP_TAG
, msg
->ssp_sync
);
651 cdns_ip_writel(cdns
, base
, data
);
652 base
+= CDNS_MCP_CMD_WORD_LEN
;
658 /* wait for timeout or response */
659 time
= wait_for_completion_timeout(&cdns
->tx_complete
,
660 msecs_to_jiffies(CDNS_TX_TIMEOUT
));
662 dev_err(cdns
->dev
, "IO transfer timed out, cmd %d device %d addr %x len %d\n",
663 cmd
, msg
->dev_num
, msg
->addr
, msg
->len
);
666 /* Drain anything in the RX_FIFO */
667 cdns_read_response(cdns
);
669 return SDW_CMD_TIMEOUT
;
672 return cdns_fill_msg_resp(cdns
, msg
, count
, offset
);
675 static enum sdw_command_response
676 cdns_program_scp_addr(struct sdw_cdns
*cdns
, struct sdw_msg
*msg
)
678 int nack
= 0, no_ack
= 0;
683 /* Program the watermark level for RX FIFO */
684 if (cdns
->msg_count
!= CDNS_SCP_RX_FIFOLEVEL
) {
685 cdns_writel(cdns
, CDNS_MCP_FIFOLEVEL
, CDNS_SCP_RX_FIFOLEVEL
);
686 cdns
->msg_count
= CDNS_SCP_RX_FIFOLEVEL
;
689 data
[0] = FIELD_PREP(CDNS_MCP_CMD_DEV_ADDR
, msg
->dev_num
);
690 data
[0] |= FIELD_PREP(CDNS_MCP_CMD_COMMAND
, 0x3);
693 data
[0] |= FIELD_PREP(CDNS_MCP_CMD_REG_ADDR
, SDW_SCP_ADDRPAGE1
);
694 data
[1] |= FIELD_PREP(CDNS_MCP_CMD_REG_ADDR
, SDW_SCP_ADDRPAGE2
);
696 data
[0] |= msg
->addr_page1
;
697 data
[1] |= msg
->addr_page2
;
699 base
= CDNS_IP_MCP_CMD_BASE
;
700 cdns_ip_writel(cdns
, base
, data
[0]);
701 base
+= CDNS_MCP_CMD_WORD_LEN
;
702 cdns_ip_writel(cdns
, base
, data
[1]);
704 time
= wait_for_completion_timeout(&cdns
->tx_complete
,
705 msecs_to_jiffies(CDNS_TX_TIMEOUT
));
707 dev_err(cdns
->dev
, "SCP Msg trf timed out\n");
709 return SDW_CMD_TIMEOUT
;
712 /* check response the writes */
713 for (i
= 0; i
< 2; i
++) {
714 if (!(cdns
->response_buf
[i
] & CDNS_MCP_RESP_ACK
)) {
716 dev_err(cdns
->dev
, "Program SCP Ack not received\n");
717 if (cdns
->response_buf
[i
] & CDNS_MCP_RESP_NACK
) {
719 dev_err(cdns
->dev
, "Program SCP NACK received\n");
724 /* For NACK, NO ack, don't return err if we are in Broadcast mode */
726 dev_err_ratelimited(cdns
->dev
,
727 "SCP_addrpage NACKed for Slave %d\n", msg
->dev_num
);
732 dev_dbg_ratelimited(cdns
->dev
,
733 "SCP_addrpage ignored for Slave %d\n", msg
->dev_num
);
734 return SDW_CMD_IGNORED
;
740 static int cdns_prep_msg(struct sdw_cdns
*cdns
, struct sdw_msg
*msg
, int *cmd
)
745 ret
= cdns_program_scp_addr(cdns
, msg
);
752 switch (msg
->flags
) {
753 case SDW_MSG_FLAG_READ
:
754 *cmd
= CDNS_MCP_CMD_READ
;
757 case SDW_MSG_FLAG_WRITE
:
758 *cmd
= CDNS_MCP_CMD_WRITE
;
762 dev_err(cdns
->dev
, "Invalid msg cmd: %d\n", msg
->flags
);
769 enum sdw_command_response
770 cdns_xfer_msg(struct sdw_bus
*bus
, struct sdw_msg
*msg
)
772 struct sdw_cdns
*cdns
= bus_to_cdns(bus
);
775 ret
= cdns_prep_msg(cdns
, msg
, &cmd
);
777 return SDW_CMD_FAIL_OTHER
;
779 for (i
= 0; i
< msg
->len
/ CDNS_MCP_CMD_LEN
; i
++) {
780 ret
= _cdns_xfer_msg(cdns
, msg
, cmd
, i
* CDNS_MCP_CMD_LEN
,
781 CDNS_MCP_CMD_LEN
, false);
782 if (ret
!= SDW_CMD_OK
)
786 if (!(msg
->len
% CDNS_MCP_CMD_LEN
))
789 return _cdns_xfer_msg(cdns
, msg
, cmd
, i
* CDNS_MCP_CMD_LEN
,
790 msg
->len
% CDNS_MCP_CMD_LEN
, false);
792 EXPORT_SYMBOL(cdns_xfer_msg
);
794 enum sdw_command_response
795 cdns_xfer_msg_defer(struct sdw_bus
*bus
)
797 struct sdw_cdns
*cdns
= bus_to_cdns(bus
);
798 struct sdw_defer
*defer
= &bus
->defer_msg
;
799 struct sdw_msg
*msg
= defer
->msg
;
802 /* for defer only 1 message is supported */
806 ret
= cdns_prep_msg(cdns
, msg
, &cmd
);
808 return SDW_CMD_FAIL_OTHER
;
810 return _cdns_xfer_msg(cdns
, msg
, cmd
, 0, msg
->len
, true);
812 EXPORT_SYMBOL(cdns_xfer_msg_defer
);
814 u32
cdns_read_ping_status(struct sdw_bus
*bus
)
816 struct sdw_cdns
*cdns
= bus_to_cdns(bus
);
818 return cdns_readl(cdns
, CDNS_MCP_SLAVE_STAT
);
820 EXPORT_SYMBOL(cdns_read_ping_status
);
826 static int cdns_update_slave_status(struct sdw_cdns
*cdns
,
829 enum sdw_slave_status status
[SDW_MAX_DEVICES
+ 1];
830 bool is_slave
= false;
835 memset(status
, 0, sizeof(status
));
837 for (i
= 0; i
<= SDW_MAX_DEVICES
; i
++) {
838 mask
= (slave_intstat
>> (i
* CDNS_MCP_SLAVE_STATUS_NUM
)) &
839 CDNS_MCP_SLAVE_STATUS_BITS
;
846 if (mask
& CDNS_MCP_SLAVE_INTSTAT_RESERVED
) {
847 status
[i
] = SDW_SLAVE_RESERVED
;
851 if (mask
& CDNS_MCP_SLAVE_INTSTAT_ATTACHED
) {
852 status
[i
] = SDW_SLAVE_ATTACHED
;
856 if (mask
& CDNS_MCP_SLAVE_INTSTAT_ALERT
) {
857 status
[i
] = SDW_SLAVE_ALERT
;
861 if (mask
& CDNS_MCP_SLAVE_INTSTAT_NPRESENT
) {
862 status
[i
] = SDW_SLAVE_UNATTACHED
;
868 * check that there was a single reported Slave status and when
869 * there is not use the latest status extracted from PING commands
871 if (set_status
!= 1) {
872 val
= cdns_readl(cdns
, CDNS_MCP_SLAVE_STAT
);
877 status
[i
] = SDW_SLAVE_UNATTACHED
;
880 status
[i
] = SDW_SLAVE_ATTACHED
;
883 status
[i
] = SDW_SLAVE_ALERT
;
887 status
[i
] = SDW_SLAVE_RESERVED
;
896 mutex_lock(&cdns
->status_update_lock
);
897 ret
= sdw_handle_slave_status(&cdns
->bus
, status
);
898 mutex_unlock(&cdns
->status_update_lock
);
906 * sdw_cdns_irq() - Cadence interrupt handler
908 * @dev_id: irq context
910 irqreturn_t
sdw_cdns_irq(int irq
, void *dev_id
)
912 struct sdw_cdns
*cdns
= dev_id
;
915 /* Check if the link is up */
919 int_status
= cdns_readl(cdns
, CDNS_MCP_INTSTAT
);
921 /* check for reserved values read as zero */
922 if (int_status
& CDNS_MCP_INT_RESERVED
)
925 if (!(int_status
& CDNS_MCP_INT_IRQ
))
928 if (int_status
& CDNS_MCP_INT_RX_WL
) {
929 struct sdw_bus
*bus
= &cdns
->bus
;
930 struct sdw_defer
*defer
= &bus
->defer_msg
;
932 cdns_read_response(cdns
);
934 if (defer
&& defer
->msg
) {
935 cdns_fill_msg_resp(cdns
, defer
->msg
,
937 complete(&defer
->complete
);
939 complete(&cdns
->tx_complete
);
943 if (int_status
& CDNS_MCP_INT_PARITY
) {
944 /* Parity error detected by Master */
945 dev_err_ratelimited(cdns
->dev
, "Parity error\n");
948 if (int_status
& CDNS_MCP_INT_CTRL_CLASH
) {
949 /* Slave is driving bit slot during control word */
950 dev_err_ratelimited(cdns
->dev
, "Bus clash for control word\n");
953 if (int_status
& CDNS_MCP_INT_DATA_CLASH
) {
955 * Multiple slaves trying to drive bit slot, or issue with
956 * ownership of data bits or Slave gone bonkers
958 dev_err_ratelimited(cdns
->dev
, "Bus clash for data word\n");
961 if (cdns
->bus
.params
.m_data_mode
!= SDW_PORT_DATA_MODE_NORMAL
&&
962 int_status
& CDNS_MCP_INT_DPINT
) {
965 /* just log which ports report an error */
966 port_intstat
= cdns_readl(cdns
, CDNS_MCP_PORT_INTSTAT
);
967 dev_err_ratelimited(cdns
->dev
, "DP interrupt: PortIntStat %8x\n",
970 /* clear status w/ write1 */
971 cdns_writel(cdns
, CDNS_MCP_PORT_INTSTAT
, port_intstat
);
974 if (int_status
& CDNS_MCP_INT_SLAVE_MASK
) {
975 /* Mask the Slave interrupt and wake thread */
976 cdns_updatel(cdns
, CDNS_MCP_INTMASK
,
977 CDNS_MCP_INT_SLAVE_MASK
, 0);
979 int_status
&= ~CDNS_MCP_INT_SLAVE_MASK
;
982 * Deal with possible race condition between interrupt
983 * handling and disabling interrupts on suspend.
985 * If the master is in the process of disabling
986 * interrupts, don't schedule a workqueue
988 if (cdns
->interrupt_enabled
)
989 schedule_work(&cdns
->work
);
992 cdns_writel(cdns
, CDNS_MCP_INTSTAT
, int_status
);
995 EXPORT_SYMBOL(sdw_cdns_irq
);
997 static void cdns_check_attached_status_dwork(struct work_struct
*work
)
999 struct sdw_cdns
*cdns
=
1000 container_of(work
, struct sdw_cdns
, attach_dwork
.work
);
1001 enum sdw_slave_status status
[SDW_MAX_DEVICES
+ 1];
1006 val
= cdns_readl(cdns
, CDNS_MCP_SLAVE_STAT
);
1008 for (i
= 0; i
<= SDW_MAX_DEVICES
; i
++) {
1009 status
[i
] = val
& 0x3;
1011 dev_dbg(cdns
->dev
, "Peripheral %d status: %d\n", i
, status
[i
]);
1015 mutex_lock(&cdns
->status_update_lock
);
1016 ret
= sdw_handle_slave_status(&cdns
->bus
, status
);
1017 mutex_unlock(&cdns
->status_update_lock
);
1019 dev_err(cdns
->dev
, "%s: sdw_handle_slave_status failed: %d\n", __func__
, ret
);
1023 * cdns_update_slave_status_work - update slave status in a work since we will need to handle
1024 * other interrupts eg. CDNS_MCP_INT_RX_WL during the update slave
1026 * @work: cdns worker thread
1028 static void cdns_update_slave_status_work(struct work_struct
*work
)
1030 struct sdw_cdns
*cdns
=
1031 container_of(work
, struct sdw_cdns
, work
);
1035 int retry_count
= 0;
1038 * Clear main interrupt first so we don't lose any assertions
1039 * that happen during this function.
1041 cdns_writel(cdns
, CDNS_MCP_INTSTAT
, CDNS_MCP_INT_SLAVE_MASK
);
1043 slave0
= cdns_readl(cdns
, CDNS_MCP_SLAVE_INTSTAT0
);
1044 slave1
= cdns_readl(cdns
, CDNS_MCP_SLAVE_INTSTAT1
);
1047 * Clear the bits before handling so we don't lose any
1048 * bits that re-assert.
1050 cdns_writel(cdns
, CDNS_MCP_SLAVE_INTSTAT0
, slave0
);
1051 cdns_writel(cdns
, CDNS_MCP_SLAVE_INTSTAT1
, slave1
);
1053 /* combine the two status */
1054 slave_intstat
= ((u64
)slave1
<< 32) | slave0
;
1056 dev_dbg_ratelimited(cdns
->dev
, "Slave status change: 0x%llx\n", slave_intstat
);
1059 cdns_update_slave_status(cdns
, slave_intstat
);
1062 * When there is more than one peripheral per link, it's
1063 * possible that a deviceB becomes attached after we deal with
1064 * the attachment of deviceA. Since the hardware does a
1065 * logical AND, the attachment of the second device does not
1066 * change the status seen by the driver.
1068 * In that case, clearing the registers above would result in
1069 * the deviceB never being detected - until a change of status
1070 * is observed on the bus.
1072 * To avoid this race condition, re-check if any device0 needs
1073 * attention with PING commands. There is no need to check for
1074 * ALERTS since they are not allowed until a non-zero
1075 * device_number is assigned.
1077 * Do not clear the INTSTAT0/1. While looping to enumerate devices on
1078 * #0 there could be status changes on other devices - these must
1079 * be kept in the INTSTAT so they can be handled when all #0 devices
1080 * have been handled.
1083 device0_status
= cdns_readl(cdns
, CDNS_MCP_SLAVE_STAT
);
1084 device0_status
&= 3;
1086 if (device0_status
== SDW_SLAVE_ATTACHED
) {
1087 if (retry_count
++ < SDW_MAX_DEVICES
) {
1088 dev_dbg_ratelimited(cdns
->dev
,
1089 "Device0 detected after clearing status, iteration %d\n",
1091 slave_intstat
= CDNS_MCP_SLAVE_INTSTAT_ATTACHED
;
1094 dev_err_ratelimited(cdns
->dev
,
1095 "Device0 detected after %d iterations\n",
1100 /* unmask Slave interrupt now */
1101 cdns_updatel(cdns
, CDNS_MCP_INTMASK
,
1102 CDNS_MCP_INT_SLAVE_MASK
, CDNS_MCP_INT_SLAVE_MASK
);
1106 /* paranoia check to make sure self-cleared bits are indeed cleared */
1107 void sdw_cdns_check_self_clearing_bits(struct sdw_cdns
*cdns
, const char *string
,
1108 bool initial_delay
, int reset_iterations
)
1112 u32 mcp_config_update
;
1116 usleep_range(1000, 1500);
1118 ip_mcp_control
= cdns_ip_readl(cdns
, CDNS_IP_MCP_CONTROL
);
1120 /* the following bits should be cleared immediately */
1121 if (ip_mcp_control
& CDNS_IP_MCP_CONTROL_SW_RST
)
1122 dev_err(cdns
->dev
, "%s failed: IP_MCP_CONTROL_SW_RST is not cleared\n", string
);
1124 mcp_control
= cdns_readl(cdns
, CDNS_MCP_CONTROL
);
1126 /* the following bits should be cleared immediately */
1127 if (mcp_control
& CDNS_MCP_CONTROL_CMD_RST
)
1128 dev_err(cdns
->dev
, "%s failed: MCP_CONTROL_CMD_RST is not cleared\n", string
);
1129 if (mcp_control
& CDNS_MCP_CONTROL_SOFT_RST
)
1130 dev_err(cdns
->dev
, "%s failed: MCP_CONTROL_SOFT_RST is not cleared\n", string
);
1131 if (mcp_control
& CDNS_MCP_CONTROL_CLK_STOP_CLR
)
1132 dev_err(cdns
->dev
, "%s failed: MCP_CONTROL_CLK_STOP_CLR is not cleared\n", string
);
1134 mcp_config_update
= cdns_readl(cdns
, CDNS_MCP_CONFIG_UPDATE
);
1135 if (mcp_config_update
& CDNS_MCP_CONFIG_UPDATE_BIT
)
1136 dev_err(cdns
->dev
, "%s failed: MCP_CONFIG_UPDATE_BIT is not cleared\n", string
);
1139 while (mcp_control
& CDNS_MCP_CONTROL_HW_RST
) {
1140 if (i
== reset_iterations
) {
1141 dev_err(cdns
->dev
, "%s failed: MCP_CONTROL_HW_RST is not cleared\n", string
);
1145 dev_dbg(cdns
->dev
, "%s: MCP_CONTROL_HW_RST is not cleared at iteration %d\n", string
, i
);
1148 usleep_range(1000, 1500);
1149 mcp_control
= cdns_readl(cdns
, CDNS_MCP_CONTROL
);
1153 EXPORT_SYMBOL(sdw_cdns_check_self_clearing_bits
);
1160 * sdw_cdns_exit_reset() - Program reset parameters and start bus operations
1161 * @cdns: Cadence instance
1163 int sdw_cdns_exit_reset(struct sdw_cdns
*cdns
)
1165 /* keep reset delay unchanged to 4096 cycles */
1167 /* use hardware generated reset */
1168 cdns_updatel(cdns
, CDNS_MCP_CONTROL
,
1169 CDNS_MCP_CONTROL_HW_RST
,
1170 CDNS_MCP_CONTROL_HW_RST
);
1172 /* commit changes */
1173 return cdns_config_update(cdns
);
1175 EXPORT_SYMBOL(sdw_cdns_exit_reset
);
1178 * cdns_enable_slave_interrupts() - Enable SDW slave interrupts
1179 * @cdns: Cadence instance
1180 * @state: boolean for true/false
1182 static void cdns_enable_slave_interrupts(struct sdw_cdns
*cdns
, bool state
)
1186 mask
= cdns_readl(cdns
, CDNS_MCP_INTMASK
);
1188 mask
|= CDNS_MCP_INT_SLAVE_MASK
;
1190 mask
&= ~CDNS_MCP_INT_SLAVE_MASK
;
1192 cdns_writel(cdns
, CDNS_MCP_INTMASK
, mask
);
1196 * sdw_cdns_enable_interrupt() - Enable SDW interrupts
1197 * @cdns: Cadence instance
1198 * @state: True if we are trying to enable interrupt.
1200 int sdw_cdns_enable_interrupt(struct sdw_cdns
*cdns
, bool state
)
1202 u32 slave_intmask0
= 0;
1203 u32 slave_intmask1
= 0;
1209 slave_intmask0
= CDNS_MCP_SLAVE_INTMASK0_MASK
;
1210 slave_intmask1
= CDNS_MCP_SLAVE_INTMASK1_MASK
;
1212 /* enable detection of all slave state changes */
1213 mask
= CDNS_MCP_INT_SLAVE_MASK
;
1215 /* enable detection of bus issues */
1216 mask
|= CDNS_MCP_INT_CTRL_CLASH
| CDNS_MCP_INT_DATA_CLASH
|
1217 CDNS_MCP_INT_PARITY
;
1219 /* port interrupt limited to test modes for now */
1220 if (cdns
->bus
.params
.m_data_mode
!= SDW_PORT_DATA_MODE_NORMAL
)
1221 mask
|= CDNS_MCP_INT_DPINT
;
1223 /* enable detection of RX fifo level */
1224 mask
|= CDNS_MCP_INT_RX_WL
;
1227 * CDNS_MCP_INT_IRQ needs to be set otherwise all previous
1228 * settings are irrelevant
1230 mask
|= CDNS_MCP_INT_IRQ
;
1232 if (interrupt_mask
) /* parameter override */
1233 mask
= interrupt_mask
;
1236 /* clear slave interrupt status before enabling interrupt */
1240 slave_state
= cdns_readl(cdns
, CDNS_MCP_SLAVE_INTSTAT0
);
1241 cdns_writel(cdns
, CDNS_MCP_SLAVE_INTSTAT0
, slave_state
);
1242 slave_state
= cdns_readl(cdns
, CDNS_MCP_SLAVE_INTSTAT1
);
1243 cdns_writel(cdns
, CDNS_MCP_SLAVE_INTSTAT1
, slave_state
);
1245 cdns
->interrupt_enabled
= state
;
1248 * Complete any on-going status updates before updating masks,
1249 * and cancel queued status updates.
1251 * There could be a race with a new interrupt thrown before
1252 * the 3 mask updates below are complete, so in the interrupt
1253 * we use the 'interrupt_enabled' status to prevent new work
1254 * from being queued.
1257 cancel_work_sync(&cdns
->work
);
1259 cdns_writel(cdns
, CDNS_MCP_SLAVE_INTMASK0
, slave_intmask0
);
1260 cdns_writel(cdns
, CDNS_MCP_SLAVE_INTMASK1
, slave_intmask1
);
1261 cdns_writel(cdns
, CDNS_MCP_INTMASK
, mask
);
1265 EXPORT_SYMBOL(sdw_cdns_enable_interrupt
);
1267 static int cdns_allocate_pdi(struct sdw_cdns
*cdns
,
1268 struct sdw_cdns_pdi
**stream
,
1271 struct sdw_cdns_pdi
*pdi
;
1277 pdi
= devm_kcalloc(cdns
->dev
, num
, sizeof(*pdi
), GFP_KERNEL
);
1281 for (i
= 0; i
< num
; i
++) {
1290 * sdw_cdns_pdi_init() - PDI initialization routine
1292 * @cdns: Cadence instance
1293 * @config: Stream configurations
1295 int sdw_cdns_pdi_init(struct sdw_cdns
*cdns
,
1296 struct sdw_cdns_stream_config config
)
1298 struct sdw_cdns_streams
*stream
;
1301 cdns
->pcm
.num_bd
= config
.pcm_bd
;
1302 cdns
->pcm
.num_in
= config
.pcm_in
;
1303 cdns
->pcm
.num_out
= config
.pcm_out
;
1305 /* Allocate PDIs for PCMs */
1306 stream
= &cdns
->pcm
;
1308 /* we allocate PDI0 and PDI1 which are used for Bulk */
1309 ret
= cdns_allocate_pdi(cdns
, &stream
->bd
, stream
->num_bd
);
1313 ret
= cdns_allocate_pdi(cdns
, &stream
->in
, stream
->num_in
);
1317 ret
= cdns_allocate_pdi(cdns
, &stream
->out
, stream
->num_out
);
1321 /* Update total number of PCM PDIs */
1322 stream
->num_pdi
= stream
->num_bd
+ stream
->num_in
+ stream
->num_out
;
1323 cdns
->num_ports
= stream
->num_pdi
;
1327 EXPORT_SYMBOL(sdw_cdns_pdi_init
);
1329 static u32
cdns_set_initial_frame_shape(int n_rows
, int n_cols
)
1335 r
= sdw_find_row_index(n_rows
);
1336 c
= sdw_find_col_index(n_cols
);
1338 val
= FIELD_PREP(CDNS_MCP_FRAME_SHAPE_ROW_MASK
, r
);
1339 val
|= FIELD_PREP(CDNS_MCP_FRAME_SHAPE_COL_MASK
, c
);
1344 static void cdns_init_clock_ctrl(struct sdw_cdns
*cdns
)
1346 struct sdw_bus
*bus
= &cdns
->bus
;
1347 struct sdw_master_prop
*prop
= &bus
->prop
;
1352 dev_dbg(cdns
->dev
, "mclk %d max %d row %d col %d\n",
1358 /* Set clock divider */
1359 divider
= (prop
->mclk_freq
/ prop
->max_clk_freq
) - 1;
1361 cdns_updatel(cdns
, CDNS_MCP_CLK_CTRL0
,
1362 CDNS_MCP_CLK_MCLKD_MASK
, divider
);
1363 cdns_updatel(cdns
, CDNS_MCP_CLK_CTRL1
,
1364 CDNS_MCP_CLK_MCLKD_MASK
, divider
);
1367 * Frame shape changes after initialization have to be done
1368 * with the bank switch mechanism
1370 val
= cdns_set_initial_frame_shape(prop
->default_row
,
1372 cdns_writel(cdns
, CDNS_MCP_FRAME_SHAPE_INIT
, val
);
1374 /* Set SSP interval to default value */
1375 ssp_interval
= prop
->default_frame_rate
/ SDW_CADENCE_GSYNC_HZ
;
1376 cdns_writel(cdns
, CDNS_MCP_SSP_CTRL0
, ssp_interval
);
1377 cdns_writel(cdns
, CDNS_MCP_SSP_CTRL1
, ssp_interval
);
1381 * sdw_cdns_init() - Cadence initialization
1382 * @cdns: Cadence instance
1384 int sdw_cdns_init(struct sdw_cdns
*cdns
)
1388 cdns_init_clock_ctrl(cdns
);
1390 sdw_cdns_check_self_clearing_bits(cdns
, __func__
, false, 0);
1392 /* reset msg_count to default value of FIFOLEVEL */
1393 cdns
->msg_count
= cdns_readl(cdns
, CDNS_MCP_FIFOLEVEL
);
1395 /* flush command FIFOs */
1396 cdns_updatel(cdns
, CDNS_MCP_CONTROL
, CDNS_MCP_CONTROL_CMD_RST
,
1397 CDNS_MCP_CONTROL_CMD_RST
);
1399 /* Set cmd accept mode */
1400 cdns_ip_updatel(cdns
, CDNS_IP_MCP_CONTROL
, CDNS_IP_MCP_CONTROL_CMD_ACCEPT
,
1401 CDNS_IP_MCP_CONTROL_CMD_ACCEPT
);
1403 /* Configure mcp config */
1404 val
= cdns_readl(cdns
, CDNS_MCP_CONFIG
);
1406 /* Disable auto bus release */
1407 val
&= ~CDNS_MCP_CONFIG_BUS_REL
;
1409 cdns_writel(cdns
, CDNS_MCP_CONFIG
, val
);
1411 /* Configure IP mcp config */
1412 val
= cdns_ip_readl(cdns
, CDNS_IP_MCP_CONFIG
);
1414 /* enable bus operations with clock and data */
1415 val
&= ~CDNS_IP_MCP_CONFIG_OP
;
1416 val
|= CDNS_IP_MCP_CONFIG_OP_NORMAL
;
1418 /* Set cmd mode for Tx and Rx cmds */
1419 val
&= ~CDNS_IP_MCP_CONFIG_CMD
;
1421 /* Disable sniffer mode */
1422 val
&= ~CDNS_IP_MCP_CONFIG_SNIFFER
;
1424 if (cdns
->bus
.multi_link
)
1425 /* Set Multi-master mode to take gsync into account */
1426 val
|= CDNS_IP_MCP_CONFIG_MMASTER
;
1428 /* leave frame delay to hardware default of 0x1F */
1430 /* leave command retry to hardware default of 0 */
1432 cdns_ip_writel(cdns
, CDNS_IP_MCP_CONFIG
, val
);
1434 /* changes will be committed later */
1437 EXPORT_SYMBOL(sdw_cdns_init
);
1439 int cdns_bus_conf(struct sdw_bus
*bus
, struct sdw_bus_params
*params
)
1441 struct sdw_master_prop
*prop
= &bus
->prop
;
1442 struct sdw_cdns
*cdns
= bus_to_cdns(bus
);
1443 int mcp_clkctrl_off
;
1446 if (!params
->curr_dr_freq
) {
1447 dev_err(cdns
->dev
, "NULL curr_dr_freq\n");
1451 divider
= prop
->mclk_freq
* SDW_DOUBLE_RATE_FACTOR
/
1452 params
->curr_dr_freq
;
1453 divider
--; /* divider is 1/(N+1) */
1455 if (params
->next_bank
)
1456 mcp_clkctrl_off
= CDNS_MCP_CLK_CTRL1
;
1458 mcp_clkctrl_off
= CDNS_MCP_CLK_CTRL0
;
1460 cdns_updatel(cdns
, mcp_clkctrl_off
, CDNS_MCP_CLK_MCLKD_MASK
, divider
);
1464 EXPORT_SYMBOL(cdns_bus_conf
);
1466 static int cdns_port_params(struct sdw_bus
*bus
,
1467 struct sdw_port_params
*p_params
, unsigned int bank
)
1469 struct sdw_cdns
*cdns
= bus_to_cdns(bus
);
1470 int dpn_config_off_source
;
1471 int dpn_config_off_target
;
1472 int target_num
= p_params
->num
;
1473 int source_num
= p_params
->num
;
1474 bool override
= false;
1477 if (target_num
== cdns
->pdi_loopback_target
&&
1478 cdns
->pdi_loopback_source
!= -1) {
1479 source_num
= cdns
->pdi_loopback_source
;
1484 dpn_config_off_source
= CDNS_DPN_B1_CONFIG(source_num
);
1485 dpn_config_off_target
= CDNS_DPN_B1_CONFIG(target_num
);
1487 dpn_config_off_source
= CDNS_DPN_B0_CONFIG(source_num
);
1488 dpn_config_off_target
= CDNS_DPN_B0_CONFIG(target_num
);
1491 dpn_config
= cdns_readl(cdns
, dpn_config_off_source
);
1493 /* use port params if there is no loopback, otherwise use source as is */
1495 u32p_replace_bits(&dpn_config
, p_params
->bps
- 1, CDNS_DPN_CONFIG_WL
);
1496 u32p_replace_bits(&dpn_config
, p_params
->flow_mode
, CDNS_DPN_CONFIG_PORT_FLOW
);
1497 u32p_replace_bits(&dpn_config
, p_params
->data_mode
, CDNS_DPN_CONFIG_PORT_DAT
);
1500 cdns_writel(cdns
, dpn_config_off_target
, dpn_config
);
1505 static int cdns_transport_params(struct sdw_bus
*bus
,
1506 struct sdw_transport_params
*t_params
,
1507 enum sdw_reg_bank bank
)
1509 struct sdw_cdns
*cdns
= bus_to_cdns(bus
);
1511 int dpn_config_off_source
;
1512 int dpn_config_off_target
;
1514 int dpn_hctrl_off_source
;
1515 int dpn_hctrl_off_target
;
1517 int dpn_offsetctrl_off_source
;
1518 int dpn_offsetctrl_off_target
;
1520 int dpn_samplectrl_off_source
;
1521 int dpn_samplectrl_off_target
;
1522 int source_num
= t_params
->port_num
;
1523 int target_num
= t_params
->port_num
;
1524 bool override
= false;
1526 if (target_num
== cdns
->pdi_loopback_target
&&
1527 cdns
->pdi_loopback_source
!= -1) {
1528 source_num
= cdns
->pdi_loopback_source
;
1533 * Note: Only full data port is supported on the Master side for
1534 * both PCM and PDM ports.
1538 dpn_config_off_source
= CDNS_DPN_B1_CONFIG(source_num
);
1539 dpn_hctrl_off_source
= CDNS_DPN_B1_HCTRL(source_num
);
1540 dpn_offsetctrl_off_source
= CDNS_DPN_B1_OFFSET_CTRL(source_num
);
1541 dpn_samplectrl_off_source
= CDNS_DPN_B1_SAMPLE_CTRL(source_num
);
1543 dpn_config_off_target
= CDNS_DPN_B1_CONFIG(target_num
);
1544 dpn_hctrl_off_target
= CDNS_DPN_B1_HCTRL(target_num
);
1545 dpn_offsetctrl_off_target
= CDNS_DPN_B1_OFFSET_CTRL(target_num
);
1546 dpn_samplectrl_off_target
= CDNS_DPN_B1_SAMPLE_CTRL(target_num
);
1549 dpn_config_off_source
= CDNS_DPN_B0_CONFIG(source_num
);
1550 dpn_hctrl_off_source
= CDNS_DPN_B0_HCTRL(source_num
);
1551 dpn_offsetctrl_off_source
= CDNS_DPN_B0_OFFSET_CTRL(source_num
);
1552 dpn_samplectrl_off_source
= CDNS_DPN_B0_SAMPLE_CTRL(source_num
);
1554 dpn_config_off_target
= CDNS_DPN_B0_CONFIG(target_num
);
1555 dpn_hctrl_off_target
= CDNS_DPN_B0_HCTRL(target_num
);
1556 dpn_offsetctrl_off_target
= CDNS_DPN_B0_OFFSET_CTRL(target_num
);
1557 dpn_samplectrl_off_target
= CDNS_DPN_B0_SAMPLE_CTRL(target_num
);
1560 dpn_config
= cdns_readl(cdns
, dpn_config_off_source
);
1562 u32p_replace_bits(&dpn_config
, t_params
->blk_grp_ctrl
, CDNS_DPN_CONFIG_BGC
);
1563 u32p_replace_bits(&dpn_config
, t_params
->blk_pkg_mode
, CDNS_DPN_CONFIG_BPM
);
1565 cdns_writel(cdns
, dpn_config_off_target
, dpn_config
);
1569 u32p_replace_bits(&dpn_offsetctrl
, t_params
->offset1
, CDNS_DPN_OFFSET_CTRL_1
);
1570 u32p_replace_bits(&dpn_offsetctrl
, t_params
->offset2
, CDNS_DPN_OFFSET_CTRL_2
);
1572 dpn_offsetctrl
= cdns_readl(cdns
, dpn_offsetctrl_off_source
);
1574 cdns_writel(cdns
, dpn_offsetctrl_off_target
, dpn_offsetctrl
);
1578 u32p_replace_bits(&dpn_hctrl
, t_params
->hstart
, CDNS_DPN_HCTRL_HSTART
);
1579 u32p_replace_bits(&dpn_hctrl
, t_params
->hstop
, CDNS_DPN_HCTRL_HSTOP
);
1580 u32p_replace_bits(&dpn_hctrl
, t_params
->lane_ctrl
, CDNS_DPN_HCTRL_LCTRL
);
1582 dpn_hctrl
= cdns_readl(cdns
, dpn_hctrl_off_source
);
1584 cdns_writel(cdns
, dpn_hctrl_off_target
, dpn_hctrl
);
1587 dpn_samplectrl
= t_params
->sample_interval
- 1;
1589 dpn_samplectrl
= cdns_readl(cdns
, dpn_samplectrl_off_source
);
1590 cdns_writel(cdns
, dpn_samplectrl_off_target
, dpn_samplectrl
);
1595 static int cdns_port_enable(struct sdw_bus
*bus
,
1596 struct sdw_enable_ch
*enable_ch
, unsigned int bank
)
1598 struct sdw_cdns
*cdns
= bus_to_cdns(bus
);
1599 int dpn_chnen_off
, ch_mask
;
1602 dpn_chnen_off
= CDNS_DPN_B1_CH_EN(enable_ch
->port_num
);
1604 dpn_chnen_off
= CDNS_DPN_B0_CH_EN(enable_ch
->port_num
);
1606 ch_mask
= enable_ch
->ch_mask
* enable_ch
->enable
;
1607 cdns_writel(cdns
, dpn_chnen_off
, ch_mask
);
1612 static const struct sdw_master_port_ops cdns_port_ops
= {
1613 .dpn_set_port_params
= cdns_port_params
,
1614 .dpn_set_port_transport_params
= cdns_transport_params
,
1615 .dpn_port_enable_ch
= cdns_port_enable
,
1619 * sdw_cdns_is_clock_stop: Check clock status
1621 * @cdns: Cadence instance
1623 bool sdw_cdns_is_clock_stop(struct sdw_cdns
*cdns
)
1625 return !!(cdns_readl(cdns
, CDNS_MCP_STAT
) & CDNS_MCP_STAT_CLK_STOP
);
1627 EXPORT_SYMBOL(sdw_cdns_is_clock_stop
);
1630 * sdw_cdns_clock_stop: Cadence clock stop configuration routine
1632 * @cdns: Cadence instance
1633 * @block_wake: prevent wakes if required by the platform
1635 int sdw_cdns_clock_stop(struct sdw_cdns
*cdns
, bool block_wake
)
1637 bool slave_present
= false;
1638 struct sdw_slave
*slave
;
1641 sdw_cdns_check_self_clearing_bits(cdns
, __func__
, false, 0);
1643 /* Check suspend status */
1644 if (sdw_cdns_is_clock_stop(cdns
)) {
1645 dev_dbg(cdns
->dev
, "Clock is already stopped\n");
1650 * Before entering clock stop we mask the Slave
1651 * interrupts. This helps avoid having to deal with e.g. a
1652 * Slave becoming UNATTACHED while the clock is being stopped
1654 cdns_enable_slave_interrupts(cdns
, false);
1657 * For specific platforms, it is required to be able to put
1658 * master into a state in which it ignores wake-up trials
1659 * in clock stop state
1662 cdns_ip_updatel(cdns
, CDNS_IP_MCP_CONTROL
,
1663 CDNS_IP_MCP_CONTROL_BLOCK_WAKEUP
,
1664 CDNS_IP_MCP_CONTROL_BLOCK_WAKEUP
);
1666 list_for_each_entry(slave
, &cdns
->bus
.slaves
, node
) {
1667 if (slave
->status
== SDW_SLAVE_ATTACHED
||
1668 slave
->status
== SDW_SLAVE_ALERT
) {
1669 slave_present
= true;
1674 /* commit changes */
1675 ret
= cdns_config_update(cdns
);
1677 dev_err(cdns
->dev
, "%s: config_update failed\n", __func__
);
1681 /* Prepare slaves for clock stop */
1682 if (slave_present
) {
1683 ret
= sdw_bus_prep_clk_stop(&cdns
->bus
);
1684 if (ret
< 0 && ret
!= -ENODATA
) {
1685 dev_err(cdns
->dev
, "prepare clock stop failed %d\n", ret
);
1691 * Enter clock stop mode and only report errors if there are
1692 * Slave devices present (ALERT or ATTACHED)
1694 ret
= sdw_bus_clk_stop(&cdns
->bus
);
1695 if (ret
< 0 && slave_present
&& ret
!= -ENODATA
) {
1696 dev_err(cdns
->dev
, "bus clock stop failed %d\n", ret
);
1700 ret
= cdns_set_wait(cdns
, CDNS_MCP_STAT
,
1701 CDNS_MCP_STAT_CLK_STOP
,
1702 CDNS_MCP_STAT_CLK_STOP
);
1704 dev_err(cdns
->dev
, "Clock stop failed %d\n", ret
);
1708 EXPORT_SYMBOL(sdw_cdns_clock_stop
);
1711 * sdw_cdns_clock_restart: Cadence PM clock restart configuration routine
1713 * @cdns: Cadence instance
1714 * @bus_reset: context may be lost while in low power modes and the bus
1715 * may require a Severe Reset and re-enumeration after a wake.
1717 int sdw_cdns_clock_restart(struct sdw_cdns
*cdns
, bool bus_reset
)
1721 /* unmask Slave interrupts that were masked when stopping the clock */
1722 cdns_enable_slave_interrupts(cdns
, true);
1724 ret
= cdns_clear_bit(cdns
, CDNS_MCP_CONTROL
,
1725 CDNS_MCP_CONTROL_CLK_STOP_CLR
);
1727 dev_err(cdns
->dev
, "Couldn't exit from clock stop\n");
1731 ret
= cdns_set_wait(cdns
, CDNS_MCP_STAT
, CDNS_MCP_STAT_CLK_STOP
, 0);
1733 dev_err(cdns
->dev
, "clock stop exit failed %d\n", ret
);
1737 cdns_ip_updatel(cdns
, CDNS_IP_MCP_CONTROL
,
1738 CDNS_IP_MCP_CONTROL_BLOCK_WAKEUP
, 0);
1740 cdns_ip_updatel(cdns
, CDNS_IP_MCP_CONTROL
, CDNS_IP_MCP_CONTROL_CMD_ACCEPT
,
1741 CDNS_IP_MCP_CONTROL_CMD_ACCEPT
);
1745 /* enable bus operations with clock and data */
1746 cdns_ip_updatel(cdns
, CDNS_IP_MCP_CONFIG
,
1747 CDNS_IP_MCP_CONFIG_OP
,
1748 CDNS_IP_MCP_CONFIG_OP_NORMAL
);
1750 ret
= cdns_config_update(cdns
);
1752 dev_err(cdns
->dev
, "%s: config_update failed\n", __func__
);
1756 ret
= sdw_bus_exit_clk_stop(&cdns
->bus
);
1758 dev_err(cdns
->dev
, "bus failed to exit clock stop %d\n", ret
);
1763 EXPORT_SYMBOL(sdw_cdns_clock_restart
);
1766 * sdw_cdns_probe() - Cadence probe routine
1767 * @cdns: Cadence instance
1769 int sdw_cdns_probe(struct sdw_cdns
*cdns
)
1771 init_completion(&cdns
->tx_complete
);
1772 cdns
->bus
.port_ops
= &cdns_port_ops
;
1774 mutex_init(&cdns
->status_update_lock
);
1776 INIT_WORK(&cdns
->work
, cdns_update_slave_status_work
);
1777 INIT_DELAYED_WORK(&cdns
->attach_dwork
, cdns_check_attached_status_dwork
);
1781 EXPORT_SYMBOL(sdw_cdns_probe
);
1783 int cdns_set_sdw_stream(struct snd_soc_dai
*dai
,
1784 void *stream
, int direction
)
1786 struct sdw_cdns
*cdns
= snd_soc_dai_get_drvdata(dai
);
1787 struct sdw_cdns_dai_runtime
*dai_runtime
;
1789 dai_runtime
= cdns
->dai_runtime_array
[dai
->id
];
1792 /* first paranoia check */
1795 "dai_runtime already allocated for dai %s\n",
1800 /* allocate and set dai_runtime info */
1801 dai_runtime
= kzalloc(sizeof(*dai_runtime
), GFP_KERNEL
);
1805 dai_runtime
->stream_type
= SDW_STREAM_PCM
;
1807 dai_runtime
->bus
= &cdns
->bus
;
1808 dai_runtime
->link_id
= cdns
->instance
;
1810 dai_runtime
->stream
= stream
;
1811 dai_runtime
->direction
= direction
;
1813 cdns
->dai_runtime_array
[dai
->id
] = dai_runtime
;
1815 /* second paranoia check */
1818 "dai_runtime not allocated for dai %s\n",
1823 /* for NULL stream we release allocated dai_runtime */
1825 cdns
->dai_runtime_array
[dai
->id
] = NULL
;
1829 EXPORT_SYMBOL(cdns_set_sdw_stream
);
1832 * cdns_find_pdi() - Find a free PDI
1834 * @cdns: Cadence instance
1835 * @num: Number of PDIs
1836 * @pdi: PDI instances
1839 * Find a PDI for a given PDI array. The PDI num and dai_id are
1840 * expected to match, return NULL otherwise.
1842 static struct sdw_cdns_pdi
*cdns_find_pdi(struct sdw_cdns
*cdns
,
1844 struct sdw_cdns_pdi
*pdi
,
1849 for (i
= 0; i
< num
; i
++)
1850 if (pdi
[i
].num
== dai_id
)
1857 * sdw_cdns_config_stream: Configure a stream
1859 * @cdns: Cadence instance
1860 * @ch: Channel count
1861 * @dir: Data direction
1862 * @pdi: PDI to be used
1864 void sdw_cdns_config_stream(struct sdw_cdns
*cdns
,
1865 u32 ch
, u32 dir
, struct sdw_cdns_pdi
*pdi
)
1867 u32 offset
, val
= 0;
1869 if (dir
== SDW_DATA_DIR_RX
) {
1870 val
= CDNS_PORTCTRL_DIRN
;
1872 if (cdns
->bus
.params
.m_data_mode
!= SDW_PORT_DATA_MODE_NORMAL
)
1873 val
|= CDNS_PORTCTRL_TEST_FAILED
;
1875 offset
= CDNS_PORTCTRL
+ pdi
->num
* CDNS_PORT_OFFSET
;
1876 cdns_updatel(cdns
, offset
,
1877 CDNS_PORTCTRL_DIRN
| CDNS_PORTCTRL_TEST_FAILED
,
1881 val
|= CDNS_PDI_CONFIG_SOFT_RESET
;
1882 val
|= FIELD_PREP(CDNS_PDI_CONFIG_CHANNEL
, (1 << ch
) - 1);
1883 cdns_writel(cdns
, CDNS_PDI_CONFIG(pdi
->num
), val
);
1885 EXPORT_SYMBOL(sdw_cdns_config_stream
);
1888 * sdw_cdns_alloc_pdi() - Allocate a PDI
1890 * @cdns: Cadence instance
1891 * @stream: Stream to be allocated
1892 * @ch: Channel count
1893 * @dir: Data direction
1896 struct sdw_cdns_pdi
*sdw_cdns_alloc_pdi(struct sdw_cdns
*cdns
,
1897 struct sdw_cdns_streams
*stream
,
1898 u32 ch
, u32 dir
, int dai_id
)
1900 struct sdw_cdns_pdi
*pdi
= NULL
;
1902 if (dir
== SDW_DATA_DIR_RX
)
1903 pdi
= cdns_find_pdi(cdns
, stream
->num_in
, stream
->in
,
1906 pdi
= cdns_find_pdi(cdns
, stream
->num_out
, stream
->out
,
1909 /* check if we found a PDI, else find in bi-directional */
1911 pdi
= cdns_find_pdi(cdns
, stream
->num_bd
, stream
->bd
,
1916 pdi
->h_ch_num
= ch
- 1;
1923 EXPORT_SYMBOL(sdw_cdns_alloc_pdi
);
1925 MODULE_LICENSE("Dual BSD/GPL");
1926 MODULE_DESCRIPTION("Cadence Soundwire Library");