1 // SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
2 // Copyright(c) 2015-17 Intel Corporation.
3 
4 /*
5  * Cadence SoundWire Master module
6  * Used by Master driver
7  */
8 
9 #include <linux/delay.h>
10 #include <linux/device.h>
11 #include <linux/debugfs.h>
12 #include <linux/interrupt.h>
13 #include <linux/io.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>
22 #include "bus.h"
23 #include "cadence_master.h"
24 
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");
28 
29 #define CDNS_MCP_CONFIG				0x0
30 #define CDNS_MCP_CONFIG_BUS_REL			BIT(6)
31 
32 #define CDNS_IP_MCP_CONFIG			0x0 /* IP offset added at run-time */
33 
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
41 
42 #define CDNS_MCP_CONTROL			0x4
43 
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)
48 
49 #define CDNS_IP_MCP_CONTROL			0x4  /* IP offset added at run-time */
50 
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)
56 
57 #define CDNS_IP_MCP_CMDCTRL			0x8 /* IP offset added at run-time */
58 
59 #define CDNS_IP_MCP_CMDCTRL_INSERT_PARITY_ERR	BIT(2)
60 
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)
66 
67 #define CDNS_MCP_CONFIG_UPDATE			0x18
68 #define CDNS_MCP_CONFIG_UPDATE_BIT		BIT(0)
69 
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)
76 
77 #define CDNS_MCP_STAT				0x40
78 
79 #define CDNS_MCP_STAT_ACTIVE_BANK		BIT(20)
80 #define CDNS_MCP_STAT_CLK_STOP			BIT(16)
81 
82 #define CDNS_MCP_INTSTAT			0x44
83 #define CDNS_MCP_INTMASK			0x48
84 
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)
104 
105 #define CDNS_MCP_INTSET				0x4C
106 
107 #define CDNS_MCP_SLAVE_STAT			0x50
108 #define CDNS_MCP_SLAVE_STAT_MASK		GENMASK(1, 0)
109 
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
118 
119 #define CDNS_MCP_SLAVE_INTMASK0			0x5C
120 #define CDNS_MCP_SLAVE_INTMASK1			0x60
121 
122 #define CDNS_MCP_SLAVE_INTMASK0_MASK		GENMASK(31, 0)
123 #define CDNS_MCP_SLAVE_INTMASK1_MASK		GENMASK(15, 0)
124 
125 #define CDNS_MCP_PORT_INTSTAT			0x64
126 #define CDNS_MCP_PDI_STAT			0x6C
127 
128 #define CDNS_MCP_FIFOLEVEL			0x78
129 #define CDNS_MCP_FIFOSTAT			0x7C
130 #define CDNS_MCP_RX_FIFO_AVAIL			GENMASK(5, 0)
131 
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
137 
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)
143 
144 #define CDNS_MCP_CMD_READ			2
145 #define CDNS_MCP_CMD_WRITE			3
146 
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)
150 
151 #define CDNS_DP_SIZE				128
152 
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))
159 
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))
166 
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)
172 
173 #define CDNS_DPN_SAMPLE_CTRL_SI			GENMASK(15, 0)
174 
175 #define CDNS_DPN_OFFSET_CTRL_1			GENMASK(7, 0)
176 #define CDNS_DPN_OFFSET_CTRL_2			GENMASK(15, 8)
177 
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)
181 
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)
186 
187 #define CDNS_PORT_OFFSET			0x80
188 
189 #define CDNS_PDI_CONFIG(n)			(0x1100 + (n) * 16)
190 
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)
194 
195 /* Driver defaults */
196 #define CDNS_TX_TIMEOUT				500
197 
198 #define CDNS_SCP_RX_FIFOLEVEL			0x2
199 
200 /*
201  * register accessor helpers
202  */
cdns_readl(struct sdw_cdns * cdns,int offset)203 static inline u32 cdns_readl(struct sdw_cdns *cdns, int offset)
204 {
205 	return readl(cdns->registers + offset);
206 }
207 
cdns_writel(struct sdw_cdns * cdns,int offset,u32 value)208 static inline void cdns_writel(struct sdw_cdns *cdns, int offset, u32 value)
209 {
210 	writel(value, cdns->registers + offset);
211 }
212 
cdns_ip_readl(struct sdw_cdns * cdns,int offset)213 static inline u32 cdns_ip_readl(struct sdw_cdns *cdns, int offset)
214 {
215 	return cdns_readl(cdns, cdns->ip_offset + offset);
216 }
217 
cdns_ip_writel(struct sdw_cdns * cdns,int offset,u32 value)218 static inline void cdns_ip_writel(struct sdw_cdns *cdns, int offset, u32 value)
219 {
220 	return cdns_writel(cdns, cdns->ip_offset + offset, value);
221 }
222 
cdns_updatel(struct sdw_cdns * cdns,int offset,u32 mask,u32 val)223 static inline void cdns_updatel(struct sdw_cdns *cdns,
224 				int offset, u32 mask, u32 val)
225 {
226 	u32 tmp;
227 
228 	tmp = cdns_readl(cdns, offset);
229 	tmp = (tmp & ~mask) | val;
230 	cdns_writel(cdns, offset, tmp);
231 }
232 
cdns_ip_updatel(struct sdw_cdns * cdns,int offset,u32 mask,u32 val)233 static inline void cdns_ip_updatel(struct sdw_cdns *cdns,
234 				   int offset, u32 mask, u32 val)
235 {
236 	cdns_updatel(cdns, cdns->ip_offset + offset, mask, val);
237 }
238 
cdns_set_wait(struct sdw_cdns * cdns,int offset,u32 mask,u32 value)239 static int cdns_set_wait(struct sdw_cdns *cdns, int offset, u32 mask, u32 value)
240 {
241 	int timeout = 10;
242 	u32 reg_read;
243 
244 	/* Wait for bit to be set */
245 	do {
246 		reg_read = readl(cdns->registers + offset);
247 		if ((reg_read & mask) == value)
248 			return 0;
249 
250 		timeout--;
251 		usleep_range(50, 100);
252 	} while (timeout != 0);
253 
254 	return -ETIMEDOUT;
255 }
256 
cdns_clear_bit(struct sdw_cdns * cdns,int offset,u32 value)257 static int cdns_clear_bit(struct sdw_cdns *cdns, int offset, u32 value)
258 {
259 	writel(value, cdns->registers + offset);
260 
261 	/* Wait for bit to be self cleared */
262 	return cdns_set_wait(cdns, offset, value, 0);
263 }
264 
265 /*
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
268  */
cdns_config_update(struct sdw_cdns * cdns)269 static int cdns_config_update(struct sdw_cdns *cdns)
270 {
271 	int ret;
272 
273 	if (sdw_cdns_is_clock_stop(cdns)) {
274 		dev_err(cdns->dev, "Cannot program MCP_CONFIG_UPDATE in ClockStopMode\n");
275 		return -EINVAL;
276 	}
277 
278 	ret = cdns_clear_bit(cdns, CDNS_MCP_CONFIG_UPDATE,
279 			     CDNS_MCP_CONFIG_UPDATE_BIT);
280 	if (ret < 0)
281 		dev_err(cdns->dev, "Config update timedout\n");
282 
283 	return ret;
284 }
285 
286 /**
287  * sdw_cdns_config_update() - Update configurations
288  * @cdns: Cadence instance
289  */
sdw_cdns_config_update(struct sdw_cdns * cdns)290 void sdw_cdns_config_update(struct sdw_cdns *cdns)
291 {
292 	/* commit changes */
293 	cdns_writel(cdns, CDNS_MCP_CONFIG_UPDATE, CDNS_MCP_CONFIG_UPDATE_BIT);
294 }
295 EXPORT_SYMBOL(sdw_cdns_config_update);
296 
297 /**
298  * sdw_cdns_config_update_set_wait() - wait until configuration update bit is self-cleared
299  * @cdns: Cadence instance
300  */
sdw_cdns_config_update_set_wait(struct sdw_cdns * cdns)301 int sdw_cdns_config_update_set_wait(struct sdw_cdns *cdns)
302 {
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);
306 }
307 EXPORT_SYMBOL(sdw_cdns_config_update_set_wait);
308 
309 /*
310  * debugfs
311  */
312 #ifdef CONFIG_DEBUG_FS
313 
314 #define RD_BUF (2 * PAGE_SIZE)
315 
cdns_sprintf(struct sdw_cdns * cdns,char * buf,size_t pos,unsigned int reg)316 static ssize_t cdns_sprintf(struct sdw_cdns *cdns,
317 			    char *buf, size_t pos, unsigned int reg)
318 {
319 	return scnprintf(buf + pos, RD_BUF - pos,
320 			 "%4x\t%8x\n", reg, cdns_readl(cdns, reg));
321 }
322 
cdns_reg_show(struct seq_file * s,void * data)323 static int cdns_reg_show(struct seq_file *s, void *data)
324 {
325 	struct sdw_cdns *cdns = s->private;
326 	char *buf;
327 	ssize_t ret;
328 	int num_ports;
329 	int i, j;
330 
331 	buf = kzalloc(RD_BUF, GFP_KERNEL);
332 	if (!buf)
333 		return -ENOMEM;
334 
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);
340 
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);
346 
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);
353 
354 	ret += scnprintf(buf + ret, RD_BUF - ret,
355 			 "\nDPn B0 Registers\n");
356 
357 	num_ports = cdns->num_ports;
358 
359 	for (i = 0; i < num_ports; i++) {
360 		ret += scnprintf(buf + ret, RD_BUF - ret,
361 				 "\nDP-%d\n", i);
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);
365 	}
366 
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,
371 				 "\nDP-%d\n", i);
372 
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);
376 	}
377 
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);
383 
384 	ret += scnprintf(buf + ret, RD_BUF - ret,
385 			 "\nPDIn Config Registers\n");
386 
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));
390 
391 	seq_printf(s, "%s", buf);
392 	kfree(buf);
393 
394 	return 0;
395 }
396 DEFINE_SHOW_ATTRIBUTE(cdns_reg);
397 
cdns_hw_reset(void * data,u64 value)398 static int cdns_hw_reset(void *data, u64 value)
399 {
400 	struct sdw_cdns *cdns = data;
401 	int ret;
402 
403 	if (value != 1)
404 		return -EINVAL;
405 
406 	/* Userspace changed the hardware state behind the kernel's back */
407 	add_taint(TAINT_USER, LOCKDEP_STILL_OK);
408 
409 	ret = sdw_cdns_exit_reset(cdns);
410 
411 	dev_dbg(cdns->dev, "link hw_reset done: %d\n", ret);
412 
413 	return ret;
414 }
415 
416 DEFINE_DEBUGFS_ATTRIBUTE(cdns_hw_reset_fops, NULL, cdns_hw_reset, "%llu\n");
417 
cdns_parity_error_injection(void * data,u64 value)418 static int cdns_parity_error_injection(void *data, u64 value)
419 {
420 	struct sdw_cdns *cdns = data;
421 	struct sdw_bus *bus;
422 	int ret;
423 
424 	if (value != 1)
425 		return -EINVAL;
426 
427 	bus = &cdns->bus;
428 
429 	/*
430 	 * Resume Master device. If this results in a bus reset, the
431 	 * Slave devices will re-attach and be re-enumerated.
432 	 */
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",
437 				    __func__, ret);
438 		return ret;
439 	}
440 
441 	/*
442 	 * wait long enough for Slave(s) to be in steady state. This
443 	 * does not need to be super precise.
444 	 */
445 	msleep(200);
446 
447 	/*
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
450 	 */
451 	mutex_lock(&bus->bus_lock);
452 
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);
457 
458 	/* commit changes */
459 	ret = cdns_clear_bit(cdns, CDNS_MCP_CONFIG_UPDATE, CDNS_MCP_CONFIG_UPDATE_BIT);
460 	if (ret < 0)
461 		goto unlock;
462 
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);
466 
467 	/* program hardware to disable parity error */
468 	cdns_ip_updatel(cdns, CDNS_IP_MCP_CMDCTRL,
469 			CDNS_IP_MCP_CMDCTRL_INSERT_PARITY_ERR,
470 			0);
471 
472 	/* commit changes */
473 	ret = cdns_clear_bit(cdns, CDNS_MCP_CONFIG_UPDATE, CDNS_MCP_CONFIG_UPDATE_BIT);
474 	if (ret < 0)
475 		goto unlock;
476 
477 	/* Userspace changed the hardware state behind the kernel's back */
478 	add_taint(TAINT_USER, LOCKDEP_STILL_OK);
479 
480 unlock:
481 	/* Continue bus operation with parity error injection disabled */
482 	mutex_unlock(&bus->bus_lock);
483 
484 	/*
485 	 * allow Master device to enter pm_runtime suspend. This may
486 	 * also result in Slave devices suspending.
487 	 */
488 	pm_runtime_mark_last_busy(bus->dev);
489 	pm_runtime_put_autosuspend(bus->dev);
490 
491 	return 0;
492 }
493 
494 DEFINE_DEBUGFS_ATTRIBUTE(cdns_parity_error_fops, NULL,
495 			 cdns_parity_error_injection, "%llu\n");
496 
cdns_set_pdi_loopback_source(void * data,u64 value)497 static int cdns_set_pdi_loopback_source(void *data, u64 value)
498 {
499 	struct sdw_cdns *cdns = data;
500 	unsigned int pdi_out_num = cdns->pcm.num_bd + cdns->pcm.num_out;
501 
502 	if (value > pdi_out_num)
503 		return -EINVAL;
504 
505 	/* Userspace changed the hardware state behind the kernel's back */
506 	add_taint(TAINT_USER, LOCKDEP_STILL_OK);
507 
508 	cdns->pdi_loopback_source = value;
509 
510 	return 0;
511 }
512 DEFINE_DEBUGFS_ATTRIBUTE(cdns_pdi_loopback_source_fops, NULL, cdns_set_pdi_loopback_source, "%llu\n");
513 
cdns_set_pdi_loopback_target(void * data,u64 value)514 static int cdns_set_pdi_loopback_target(void *data, u64 value)
515 {
516 	struct sdw_cdns *cdns = data;
517 	unsigned int pdi_in_num = cdns->pcm.num_bd + cdns->pcm.num_in;
518 
519 	if (value > pdi_in_num)
520 		return -EINVAL;
521 
522 	/* Userspace changed the hardware state behind the kernel's back */
523 	add_taint(TAINT_USER, LOCKDEP_STILL_OK);
524 
525 	cdns->pdi_loopback_target = value;
526 
527 	return 0;
528 }
529 DEFINE_DEBUGFS_ATTRIBUTE(cdns_pdi_loopback_target_fops, NULL, cdns_set_pdi_loopback_target, "%llu\n");
530 
531 /**
532  * sdw_cdns_debugfs_init() - Cadence debugfs init
533  * @cdns: Cadence instance
534  * @root: debugfs root
535  */
sdw_cdns_debugfs_init(struct sdw_cdns * cdns,struct dentry * root)536 void sdw_cdns_debugfs_init(struct sdw_cdns *cdns, struct dentry *root)
537 {
538 	debugfs_create_file("cdns-registers", 0400, root, cdns, &cdns_reg_fops);
539 
540 	debugfs_create_file("cdns-hw-reset", 0200, root, cdns,
541 			    &cdns_hw_reset_fops);
542 
543 	debugfs_create_file("cdns-parity-error-injection", 0200, root, cdns,
544 			    &cdns_parity_error_fops);
545 
546 	cdns->pdi_loopback_source = -1;
547 	cdns->pdi_loopback_target = -1;
548 
549 	debugfs_create_file("cdns-pdi-loopback-source", 0200, root, cdns,
550 			    &cdns_pdi_loopback_source_fops);
551 
552 	debugfs_create_file("cdns-pdi-loopback-target", 0200, root, cdns,
553 			    &cdns_pdi_loopback_target_fops);
554 
555 }
556 EXPORT_SYMBOL_GPL(sdw_cdns_debugfs_init);
557 
558 #endif /* CONFIG_DEBUG_FS */
559 
560 /*
561  * IO Calls
562  */
563 static enum sdw_command_response
cdns_fill_msg_resp(struct sdw_cdns * cdns,struct sdw_msg * msg,int count,int offset)564 cdns_fill_msg_resp(struct sdw_cdns *cdns,
565 		   struct sdw_msg *msg, int count, int offset)
566 {
567 	int nack = 0, no_ack = 0;
568 	int i;
569 
570 	/* check message response */
571 	for (i = 0; i < count; i++) {
572 		if (!(cdns->response_buf[i] & CDNS_MCP_RESP_ACK)) {
573 			no_ack = 1;
574 			dev_vdbg(cdns->dev, "Msg Ack not received, cmd %d\n", i);
575 		}
576 		if (cdns->response_buf[i] & CDNS_MCP_RESP_NACK) {
577 			nack = 1;
578 			dev_err_ratelimited(cdns->dev, "Msg NACK received, cmd %d\n", i);
579 		}
580 	}
581 
582 	if (nack) {
583 		dev_err_ratelimited(cdns->dev, "Msg NACKed for Slave %d\n", msg->dev_num);
584 		return SDW_CMD_FAIL;
585 	}
586 
587 	if (no_ack) {
588 		dev_dbg_ratelimited(cdns->dev, "Msg ignored for Slave %d\n", msg->dev_num);
589 		return SDW_CMD_IGNORED;
590 	}
591 
592 	if (msg->flags == SDW_MSG_FLAG_READ) {
593 		/* fill response */
594 		for (i = 0; i < count; i++)
595 			msg->buf[i + offset] = FIELD_GET(CDNS_MCP_RESP_RDATA,
596 							 cdns->response_buf[i]);
597 	}
598 
599 	return SDW_CMD_OK;
600 }
601 
cdns_read_response(struct sdw_cdns * cdns)602 static void cdns_read_response(struct sdw_cdns *cdns)
603 {
604 	u32 num_resp, cmd_base;
605 	int i;
606 
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);
609 
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);
615 	}
616 
617 	cmd_base = CDNS_IP_MCP_CMD_BASE;
618 
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;
622 	}
623 }
624 
625 static enum sdw_command_response
_cdns_xfer_msg(struct sdw_cdns * cdns,struct sdw_msg * msg,int cmd,int offset,int count,bool defer)626 _cdns_xfer_msg(struct sdw_cdns *cdns, struct sdw_msg *msg, int cmd,
627 	       int offset, int count, bool defer)
628 {
629 	unsigned long time;
630 	u32 base, i, data;
631 	u16 addr;
632 
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;
637 	}
638 
639 	base = CDNS_IP_MCP_CMD_BASE;
640 	addr = msg->addr + offset;
641 
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);
646 		addr++;
647 
648 		if (msg->flags == SDW_MSG_FLAG_WRITE)
649 			data |= msg->buf[i + offset];
650 
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;
654 	}
655 
656 	if (defer)
657 		return SDW_CMD_OK;
658 
659 	/* wait for timeout or response */
660 	time = wait_for_completion_timeout(&cdns->tx_complete,
661 					   msecs_to_jiffies(CDNS_TX_TIMEOUT));
662 	if (!time) {
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);
665 		msg->len = 0;
666 
667 		/* Drain anything in the RX_FIFO */
668 		cdns_read_response(cdns);
669 
670 		return SDW_CMD_TIMEOUT;
671 	}
672 
673 	return cdns_fill_msg_resp(cdns, msg, count, offset);
674 }
675 
676 static enum sdw_command_response
cdns_program_scp_addr(struct sdw_cdns * cdns,struct sdw_msg * msg)677 cdns_program_scp_addr(struct sdw_cdns *cdns, struct sdw_msg *msg)
678 {
679 	int nack = 0, no_ack = 0;
680 	unsigned long time;
681 	u32 data[2], base;
682 	int i;
683 
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;
688 	}
689 
690 	data[0] = FIELD_PREP(CDNS_MCP_CMD_DEV_ADDR, msg->dev_num);
691 	data[0] |= FIELD_PREP(CDNS_MCP_CMD_COMMAND, 0x3);
692 	data[1] = data[0];
693 
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);
696 
697 	data[0] |= msg->addr_page1;
698 	data[1] |= msg->addr_page2;
699 
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]);
704 
705 	time = wait_for_completion_timeout(&cdns->tx_complete,
706 					   msecs_to_jiffies(CDNS_TX_TIMEOUT));
707 	if (!time) {
708 		dev_err(cdns->dev, "SCP Msg trf timed out\n");
709 		msg->len = 0;
710 		return SDW_CMD_TIMEOUT;
711 	}
712 
713 	/* check response the writes */
714 	for (i = 0; i < 2; i++) {
715 		if (!(cdns->response_buf[i] & CDNS_MCP_RESP_ACK)) {
716 			no_ack = 1;
717 			dev_err(cdns->dev, "Program SCP Ack not received\n");
718 			if (cdns->response_buf[i] & CDNS_MCP_RESP_NACK) {
719 				nack = 1;
720 				dev_err(cdns->dev, "Program SCP NACK received\n");
721 			}
722 		}
723 	}
724 
725 	/* For NACK, NO ack, don't return err if we are in Broadcast mode */
726 	if (nack) {
727 		dev_err_ratelimited(cdns->dev,
728 				    "SCP_addrpage NACKed for Slave %d\n", msg->dev_num);
729 		return SDW_CMD_FAIL;
730 	}
731 
732 	if (no_ack) {
733 		dev_dbg_ratelimited(cdns->dev,
734 				    "SCP_addrpage ignored for Slave %d\n", msg->dev_num);
735 		return SDW_CMD_IGNORED;
736 	}
737 
738 	return SDW_CMD_OK;
739 }
740 
cdns_prep_msg(struct sdw_cdns * cdns,struct sdw_msg * msg,int * cmd)741 static int cdns_prep_msg(struct sdw_cdns *cdns, struct sdw_msg *msg, int *cmd)
742 {
743 	int ret;
744 
745 	if (msg->page) {
746 		ret = cdns_program_scp_addr(cdns, msg);
747 		if (ret) {
748 			msg->len = 0;
749 			return ret;
750 		}
751 	}
752 
753 	switch (msg->flags) {
754 	case SDW_MSG_FLAG_READ:
755 		*cmd = CDNS_MCP_CMD_READ;
756 		break;
757 
758 	case SDW_MSG_FLAG_WRITE:
759 		*cmd = CDNS_MCP_CMD_WRITE;
760 		break;
761 
762 	default:
763 		dev_err(cdns->dev, "Invalid msg cmd: %d\n", msg->flags);
764 		return -EINVAL;
765 	}
766 
767 	return 0;
768 }
769 
770 enum sdw_command_response
cdns_xfer_msg(struct sdw_bus * bus,struct sdw_msg * msg)771 cdns_xfer_msg(struct sdw_bus *bus, struct sdw_msg *msg)
772 {
773 	struct sdw_cdns *cdns = bus_to_cdns(bus);
774 	int cmd = 0, ret, i;
775 
776 	ret = cdns_prep_msg(cdns, msg, &cmd);
777 	if (ret)
778 		return SDW_CMD_FAIL_OTHER;
779 
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)
784 			return ret;
785 	}
786 
787 	if (!(msg->len % CDNS_MCP_CMD_LEN))
788 		return SDW_CMD_OK;
789 
790 	return _cdns_xfer_msg(cdns, msg, cmd, i * CDNS_MCP_CMD_LEN,
791 			      msg->len % CDNS_MCP_CMD_LEN, false);
792 }
793 EXPORT_SYMBOL(cdns_xfer_msg);
794 
795 enum sdw_command_response
cdns_xfer_msg_defer(struct sdw_bus * bus)796 cdns_xfer_msg_defer(struct sdw_bus *bus)
797 {
798 	struct sdw_cdns *cdns = bus_to_cdns(bus);
799 	struct sdw_defer *defer = &bus->defer_msg;
800 	struct sdw_msg *msg = defer->msg;
801 	int cmd = 0, ret;
802 
803 	/* for defer only 1 message is supported */
804 	if (msg->len > 1)
805 		return -ENOTSUPP;
806 
807 	ret = cdns_prep_msg(cdns, msg, &cmd);
808 	if (ret)
809 		return SDW_CMD_FAIL_OTHER;
810 
811 	return _cdns_xfer_msg(cdns, msg, cmd, 0, msg->len, true);
812 }
813 EXPORT_SYMBOL(cdns_xfer_msg_defer);
814 
cdns_read_ping_status(struct sdw_bus * bus)815 u32 cdns_read_ping_status(struct sdw_bus *bus)
816 {
817 	struct sdw_cdns *cdns = bus_to_cdns(bus);
818 
819 	return cdns_readl(cdns, CDNS_MCP_SLAVE_STAT);
820 }
821 EXPORT_SYMBOL(cdns_read_ping_status);
822 
823 /*
824  * IRQ handling
825  */
826 
cdns_update_slave_status(struct sdw_cdns * cdns,u64 slave_intstat)827 static int cdns_update_slave_status(struct sdw_cdns *cdns,
828 				    u64 slave_intstat)
829 {
830 	enum sdw_slave_status status[SDW_MAX_DEVICES + 1];
831 	bool is_slave = false;
832 	u32 mask;
833 	u32 val;
834 	int i, set_status;
835 
836 	memset(status, 0, sizeof(status));
837 
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;
841 
842 		set_status = 0;
843 
844 		if (mask) {
845 			is_slave = true;
846 
847 			if (mask & CDNS_MCP_SLAVE_INTSTAT_RESERVED) {
848 				status[i] = SDW_SLAVE_RESERVED;
849 				set_status++;
850 			}
851 
852 			if (mask & CDNS_MCP_SLAVE_INTSTAT_ATTACHED) {
853 				status[i] = SDW_SLAVE_ATTACHED;
854 				set_status++;
855 			}
856 
857 			if (mask & CDNS_MCP_SLAVE_INTSTAT_ALERT) {
858 				status[i] = SDW_SLAVE_ALERT;
859 				set_status++;
860 			}
861 
862 			if (mask & CDNS_MCP_SLAVE_INTSTAT_NPRESENT) {
863 				status[i] = SDW_SLAVE_UNATTACHED;
864 				set_status++;
865 			}
866 		}
867 
868 		/*
869 		 * check that there was a single reported Slave status and when
870 		 * there is not use the latest status extracted from PING commands
871 		 */
872 		if (set_status != 1) {
873 			val = cdns_readl(cdns, CDNS_MCP_SLAVE_STAT);
874 			val >>= (i * 2);
875 
876 			switch (val & 0x3) {
877 			case 0:
878 				status[i] = SDW_SLAVE_UNATTACHED;
879 				break;
880 			case 1:
881 				status[i] = SDW_SLAVE_ATTACHED;
882 				break;
883 			case 2:
884 				status[i] = SDW_SLAVE_ALERT;
885 				break;
886 			case 3:
887 			default:
888 				status[i] = SDW_SLAVE_RESERVED;
889 				break;
890 			}
891 		}
892 	}
893 
894 	if (is_slave) {
895 		int ret;
896 
897 		mutex_lock(&cdns->status_update_lock);
898 		ret = sdw_handle_slave_status(&cdns->bus, status);
899 		mutex_unlock(&cdns->status_update_lock);
900 		return ret;
901 	}
902 
903 	return 0;
904 }
905 
906 /**
907  * sdw_cdns_irq() - Cadence interrupt handler
908  * @irq: irq number
909  * @dev_id: irq context
910  */
sdw_cdns_irq(int irq,void * dev_id)911 irqreturn_t sdw_cdns_irq(int irq, void *dev_id)
912 {
913 	struct sdw_cdns *cdns = dev_id;
914 	u32 int_status;
915 
916 	/* Check if the link is up */
917 	if (!cdns->link_up)
918 		return IRQ_NONE;
919 
920 	int_status = cdns_readl(cdns, CDNS_MCP_INTSTAT);
921 
922 	/* check for reserved values read as zero */
923 	if (int_status & CDNS_MCP_INT_RESERVED)
924 		return IRQ_NONE;
925 
926 	if (!(int_status & CDNS_MCP_INT_IRQ))
927 		return IRQ_NONE;
928 
929 	if (int_status & CDNS_MCP_INT_RX_WL) {
930 		struct sdw_bus *bus = &cdns->bus;
931 		struct sdw_defer *defer = &bus->defer_msg;
932 
933 		cdns_read_response(cdns);
934 
935 		if (defer && defer->msg) {
936 			cdns_fill_msg_resp(cdns, defer->msg,
937 					   defer->length, 0);
938 			complete(&defer->complete);
939 		} else {
940 			complete(&cdns->tx_complete);
941 		}
942 	}
943 
944 	if (int_status & CDNS_MCP_INT_PARITY) {
945 		/* Parity error detected by Master */
946 		dev_err_ratelimited(cdns->dev, "Parity error\n");
947 	}
948 
949 	if (int_status & CDNS_MCP_INT_CTRL_CLASH) {
950 		/* Slave is driving bit slot during control word */
951 		dev_err_ratelimited(cdns->dev, "Bus clash for control word\n");
952 	}
953 
954 	if (int_status & CDNS_MCP_INT_DATA_CLASH) {
955 		/*
956 		 * Multiple slaves trying to drive bit slot, or issue with
957 		 * ownership of data bits or Slave gone bonkers
958 		 */
959 		dev_err_ratelimited(cdns->dev, "Bus clash for data word\n");
960 	}
961 
962 	if (cdns->bus.params.m_data_mode != SDW_PORT_DATA_MODE_NORMAL &&
963 	    int_status & CDNS_MCP_INT_DPINT) {
964 		u32 port_intstat;
965 
966 		/* just log which ports report an error */
967 		port_intstat = cdns_readl(cdns, CDNS_MCP_PORT_INTSTAT);
968 		dev_err_ratelimited(cdns->dev, "DP interrupt: PortIntStat %8x\n",
969 				    port_intstat);
970 
971 		/* clear status w/ write1 */
972 		cdns_writel(cdns, CDNS_MCP_PORT_INTSTAT, port_intstat);
973 	}
974 
975 	if (int_status & CDNS_MCP_INT_SLAVE_MASK) {
976 		/* Mask the Slave interrupt and wake thread */
977 		cdns_updatel(cdns, CDNS_MCP_INTMASK,
978 			     CDNS_MCP_INT_SLAVE_MASK, 0);
979 
980 		int_status &= ~CDNS_MCP_INT_SLAVE_MASK;
981 
982 		/*
983 		 * Deal with possible race condition between interrupt
984 		 * handling and disabling interrupts on suspend.
985 		 *
986 		 * If the master is in the process of disabling
987 		 * interrupts, don't schedule a workqueue
988 		 */
989 		if (cdns->interrupt_enabled)
990 			schedule_work(&cdns->work);
991 	}
992 
993 	cdns_writel(cdns, CDNS_MCP_INTSTAT, int_status);
994 	return IRQ_HANDLED;
995 }
996 EXPORT_SYMBOL(sdw_cdns_irq);
997 
cdns_check_attached_status_dwork(struct work_struct * work)998 static void cdns_check_attached_status_dwork(struct work_struct *work)
999 {
1000 	struct sdw_cdns *cdns =
1001 		container_of(work, struct sdw_cdns, attach_dwork.work);
1002 	enum sdw_slave_status status[SDW_MAX_DEVICES + 1];
1003 	u32 val;
1004 	int ret;
1005 	int i;
1006 
1007 	val = cdns_readl(cdns, CDNS_MCP_SLAVE_STAT);
1008 
1009 	for (i = 0; i <= SDW_MAX_DEVICES; i++) {
1010 		status[i] = val & 0x3;
1011 		if (status[i])
1012 			dev_dbg(cdns->dev, "Peripheral %d status: %d\n", i, status[i]);
1013 		val >>= 2;
1014 	}
1015 
1016 	mutex_lock(&cdns->status_update_lock);
1017 	ret = sdw_handle_slave_status(&cdns->bus, status);
1018 	mutex_unlock(&cdns->status_update_lock);
1019 	if (ret < 0)
1020 		dev_err(cdns->dev, "%s: sdw_handle_slave_status failed: %d\n", __func__, ret);
1021 }
1022 
1023 /**
1024  * cdns_update_slave_status_work - update slave status in a work since we will need to handle
1025  * other interrupts eg. CDNS_MCP_INT_RX_WL during the update slave
1026  * process.
1027  * @work: cdns worker thread
1028  */
cdns_update_slave_status_work(struct work_struct * work)1029 static void cdns_update_slave_status_work(struct work_struct *work)
1030 {
1031 	struct sdw_cdns *cdns =
1032 		container_of(work, struct sdw_cdns, work);
1033 	u32 slave0, slave1;
1034 	u64 slave_intstat;
1035 	u32 device0_status;
1036 	int retry_count = 0;
1037 
1038 	/*
1039 	 * Clear main interrupt first so we don't lose any assertions
1040 	 * that happen during this function.
1041 	 */
1042 	cdns_writel(cdns, CDNS_MCP_INTSTAT, CDNS_MCP_INT_SLAVE_MASK);
1043 
1044 	slave0 = cdns_readl(cdns, CDNS_MCP_SLAVE_INTSTAT0);
1045 	slave1 = cdns_readl(cdns, CDNS_MCP_SLAVE_INTSTAT1);
1046 
1047 	/*
1048 	 * Clear the bits before handling so we don't lose any
1049 	 * bits that re-assert.
1050 	 */
1051 	cdns_writel(cdns, CDNS_MCP_SLAVE_INTSTAT0, slave0);
1052 	cdns_writel(cdns, CDNS_MCP_SLAVE_INTSTAT1, slave1);
1053 
1054 	/* combine the two status */
1055 	slave_intstat = ((u64)slave1 << 32) | slave0;
1056 
1057 	dev_dbg_ratelimited(cdns->dev, "Slave status change: 0x%llx\n", slave_intstat);
1058 
1059 update_status:
1060 	cdns_update_slave_status(cdns, slave_intstat);
1061 
1062 	/*
1063 	 * When there is more than one peripheral per link, it's
1064 	 * possible that a deviceB becomes attached after we deal with
1065 	 * the attachment of deviceA. Since the hardware does a
1066 	 * logical AND, the attachment of the second device does not
1067 	 * change the status seen by the driver.
1068 	 *
1069 	 * In that case, clearing the registers above would result in
1070 	 * the deviceB never being detected - until a change of status
1071 	 * is observed on the bus.
1072 	 *
1073 	 * To avoid this race condition, re-check if any device0 needs
1074 	 * attention with PING commands. There is no need to check for
1075 	 * ALERTS since they are not allowed until a non-zero
1076 	 * device_number is assigned.
1077 	 *
1078 	 * Do not clear the INTSTAT0/1. While looping to enumerate devices on
1079 	 * #0 there could be status changes on other devices - these must
1080 	 * be kept in the INTSTAT so they can be handled when all #0 devices
1081 	 * have been handled.
1082 	 */
1083 
1084 	device0_status = cdns_readl(cdns, CDNS_MCP_SLAVE_STAT);
1085 	device0_status &= 3;
1086 
1087 	if (device0_status == SDW_SLAVE_ATTACHED) {
1088 		if (retry_count++ < SDW_MAX_DEVICES) {
1089 			dev_dbg_ratelimited(cdns->dev,
1090 					    "Device0 detected after clearing status, iteration %d\n",
1091 					    retry_count);
1092 			slave_intstat = CDNS_MCP_SLAVE_INTSTAT_ATTACHED;
1093 			goto update_status;
1094 		} else {
1095 			dev_err_ratelimited(cdns->dev,
1096 					    "Device0 detected after %d iterations\n",
1097 					    retry_count);
1098 		}
1099 	}
1100 
1101 	/* unmask Slave interrupt now */
1102 	cdns_updatel(cdns, CDNS_MCP_INTMASK,
1103 		     CDNS_MCP_INT_SLAVE_MASK, CDNS_MCP_INT_SLAVE_MASK);
1104 
1105 }
1106 
1107 /* paranoia check to make sure self-cleared bits are indeed cleared */
sdw_cdns_check_self_clearing_bits(struct sdw_cdns * cdns,const char * string,bool initial_delay,int reset_iterations)1108 void sdw_cdns_check_self_clearing_bits(struct sdw_cdns *cdns, const char *string,
1109 				       bool initial_delay, int reset_iterations)
1110 {
1111 	u32 ip_mcp_control;
1112 	u32 mcp_control;
1113 	u32 mcp_config_update;
1114 	int i;
1115 
1116 	if (initial_delay)
1117 		usleep_range(1000, 1500);
1118 
1119 	ip_mcp_control = cdns_ip_readl(cdns, CDNS_IP_MCP_CONTROL);
1120 
1121 	/* the following bits should be cleared immediately */
1122 	if (ip_mcp_control & CDNS_IP_MCP_CONTROL_SW_RST)
1123 		dev_err(cdns->dev, "%s failed: IP_MCP_CONTROL_SW_RST is not cleared\n", string);
1124 
1125 	mcp_control = cdns_readl(cdns, CDNS_MCP_CONTROL);
1126 
1127 	/* the following bits should be cleared immediately */
1128 	if (mcp_control & CDNS_MCP_CONTROL_CMD_RST)
1129 		dev_err(cdns->dev, "%s failed: MCP_CONTROL_CMD_RST is not cleared\n", string);
1130 	if (mcp_control & CDNS_MCP_CONTROL_SOFT_RST)
1131 		dev_err(cdns->dev, "%s failed: MCP_CONTROL_SOFT_RST is not cleared\n", string);
1132 	if (mcp_control & CDNS_MCP_CONTROL_CLK_STOP_CLR)
1133 		dev_err(cdns->dev, "%s failed: MCP_CONTROL_CLK_STOP_CLR is not cleared\n", string);
1134 
1135 	mcp_config_update = cdns_readl(cdns, CDNS_MCP_CONFIG_UPDATE);
1136 	if (mcp_config_update & CDNS_MCP_CONFIG_UPDATE_BIT)
1137 		dev_err(cdns->dev, "%s failed: MCP_CONFIG_UPDATE_BIT is not cleared\n", string);
1138 
1139 	i = 0;
1140 	while (mcp_control & CDNS_MCP_CONTROL_HW_RST) {
1141 		if (i == reset_iterations) {
1142 			dev_err(cdns->dev, "%s failed: MCP_CONTROL_HW_RST is not cleared\n", string);
1143 			break;
1144 		}
1145 
1146 		dev_dbg(cdns->dev, "%s: MCP_CONTROL_HW_RST is not cleared at iteration %d\n", string, i);
1147 		i++;
1148 
1149 		usleep_range(1000, 1500);
1150 		mcp_control = cdns_readl(cdns, CDNS_MCP_CONTROL);
1151 	}
1152 
1153 }
1154 EXPORT_SYMBOL(sdw_cdns_check_self_clearing_bits);
1155 
1156 /*
1157  * init routines
1158  */
1159 
1160 /**
1161  * sdw_cdns_exit_reset() - Program reset parameters and start bus operations
1162  * @cdns: Cadence instance
1163  */
sdw_cdns_exit_reset(struct sdw_cdns * cdns)1164 int sdw_cdns_exit_reset(struct sdw_cdns *cdns)
1165 {
1166 	/* keep reset delay unchanged to 4096 cycles */
1167 
1168 	/* use hardware generated reset */
1169 	cdns_updatel(cdns, CDNS_MCP_CONTROL,
1170 		     CDNS_MCP_CONTROL_HW_RST,
1171 		     CDNS_MCP_CONTROL_HW_RST);
1172 
1173 	/* commit changes */
1174 	return cdns_config_update(cdns);
1175 }
1176 EXPORT_SYMBOL(sdw_cdns_exit_reset);
1177 
1178 /**
1179  * cdns_enable_slave_interrupts() - Enable SDW slave interrupts
1180  * @cdns: Cadence instance
1181  * @state: boolean for true/false
1182  */
cdns_enable_slave_interrupts(struct sdw_cdns * cdns,bool state)1183 static void cdns_enable_slave_interrupts(struct sdw_cdns *cdns, bool state)
1184 {
1185 	u32 mask;
1186 
1187 	mask = cdns_readl(cdns, CDNS_MCP_INTMASK);
1188 	if (state)
1189 		mask |= CDNS_MCP_INT_SLAVE_MASK;
1190 	else
1191 		mask &= ~CDNS_MCP_INT_SLAVE_MASK;
1192 
1193 	cdns_writel(cdns, CDNS_MCP_INTMASK, mask);
1194 }
1195 
1196 /**
1197  * sdw_cdns_enable_interrupt() - Enable SDW interrupts
1198  * @cdns: Cadence instance
1199  * @state: True if we are trying to enable interrupt.
1200  */
sdw_cdns_enable_interrupt(struct sdw_cdns * cdns,bool state)1201 int sdw_cdns_enable_interrupt(struct sdw_cdns *cdns, bool state)
1202 {
1203 	u32 slave_intmask0 = 0;
1204 	u32 slave_intmask1 = 0;
1205 	u32 mask = 0;
1206 
1207 	if (!state)
1208 		goto update_masks;
1209 
1210 	slave_intmask0 = CDNS_MCP_SLAVE_INTMASK0_MASK;
1211 	slave_intmask1 = CDNS_MCP_SLAVE_INTMASK1_MASK;
1212 
1213 	/* enable detection of all slave state changes */
1214 	mask = CDNS_MCP_INT_SLAVE_MASK;
1215 
1216 	/* enable detection of bus issues */
1217 	mask |= CDNS_MCP_INT_CTRL_CLASH | CDNS_MCP_INT_DATA_CLASH |
1218 		CDNS_MCP_INT_PARITY;
1219 
1220 	/* port interrupt limited to test modes for now */
1221 	if (cdns->bus.params.m_data_mode != SDW_PORT_DATA_MODE_NORMAL)
1222 		mask |= CDNS_MCP_INT_DPINT;
1223 
1224 	/* enable detection of RX fifo level */
1225 	mask |= CDNS_MCP_INT_RX_WL;
1226 
1227 	/*
1228 	 * CDNS_MCP_INT_IRQ needs to be set otherwise all previous
1229 	 * settings are irrelevant
1230 	 */
1231 	mask |= CDNS_MCP_INT_IRQ;
1232 
1233 	if (interrupt_mask) /* parameter override */
1234 		mask = interrupt_mask;
1235 
1236 update_masks:
1237 	/* clear slave interrupt status before enabling interrupt */
1238 	if (state) {
1239 		u32 slave_state;
1240 
1241 		slave_state = cdns_readl(cdns, CDNS_MCP_SLAVE_INTSTAT0);
1242 		cdns_writel(cdns, CDNS_MCP_SLAVE_INTSTAT0, slave_state);
1243 		slave_state = cdns_readl(cdns, CDNS_MCP_SLAVE_INTSTAT1);
1244 		cdns_writel(cdns, CDNS_MCP_SLAVE_INTSTAT1, slave_state);
1245 	}
1246 	cdns->interrupt_enabled = state;
1247 
1248 	/*
1249 	 * Complete any on-going status updates before updating masks,
1250 	 * and cancel queued status updates.
1251 	 *
1252 	 * There could be a race with a new interrupt thrown before
1253 	 * the 3 mask updates below are complete, so in the interrupt
1254 	 * we use the 'interrupt_enabled' status to prevent new work
1255 	 * from being queued.
1256 	 */
1257 	if (!state)
1258 		cancel_work_sync(&cdns->work);
1259 
1260 	cdns_writel(cdns, CDNS_MCP_SLAVE_INTMASK0, slave_intmask0);
1261 	cdns_writel(cdns, CDNS_MCP_SLAVE_INTMASK1, slave_intmask1);
1262 	cdns_writel(cdns, CDNS_MCP_INTMASK, mask);
1263 
1264 	return 0;
1265 }
1266 EXPORT_SYMBOL(sdw_cdns_enable_interrupt);
1267 
cdns_allocate_pdi(struct sdw_cdns * cdns,struct sdw_cdns_pdi ** stream,u32 num,u32 pdi_offset)1268 static int cdns_allocate_pdi(struct sdw_cdns *cdns,
1269 			     struct sdw_cdns_pdi **stream,
1270 			     u32 num, u32 pdi_offset)
1271 {
1272 	struct sdw_cdns_pdi *pdi;
1273 	int i;
1274 
1275 	if (!num)
1276 		return 0;
1277 
1278 	pdi = devm_kcalloc(cdns->dev, num, sizeof(*pdi), GFP_KERNEL);
1279 	if (!pdi)
1280 		return -ENOMEM;
1281 
1282 	for (i = 0; i < num; i++) {
1283 		pdi[i].num = i + pdi_offset;
1284 	}
1285 
1286 	*stream = pdi;
1287 	return 0;
1288 }
1289 
1290 /**
1291  * sdw_cdns_pdi_init() - PDI initialization routine
1292  *
1293  * @cdns: Cadence instance
1294  * @config: Stream configurations
1295  */
sdw_cdns_pdi_init(struct sdw_cdns * cdns,struct sdw_cdns_stream_config config)1296 int sdw_cdns_pdi_init(struct sdw_cdns *cdns,
1297 		      struct sdw_cdns_stream_config config)
1298 {
1299 	struct sdw_cdns_streams *stream;
1300 	int offset;
1301 	int ret;
1302 
1303 	cdns->pcm.num_bd = config.pcm_bd;
1304 	cdns->pcm.num_in = config.pcm_in;
1305 	cdns->pcm.num_out = config.pcm_out;
1306 
1307 	/* Allocate PDIs for PCMs */
1308 	stream = &cdns->pcm;
1309 
1310 	/* we allocate PDI0 and PDI1 which are used for Bulk */
1311 	offset = 0;
1312 
1313 	ret = cdns_allocate_pdi(cdns, &stream->bd,
1314 				stream->num_bd, offset);
1315 	if (ret)
1316 		return ret;
1317 
1318 	offset += stream->num_bd;
1319 
1320 	ret = cdns_allocate_pdi(cdns, &stream->in,
1321 				stream->num_in, offset);
1322 	if (ret)
1323 		return ret;
1324 
1325 	offset += stream->num_in;
1326 
1327 	ret = cdns_allocate_pdi(cdns, &stream->out,
1328 				stream->num_out, offset);
1329 	if (ret)
1330 		return ret;
1331 
1332 	/* Update total number of PCM PDIs */
1333 	stream->num_pdi = stream->num_bd + stream->num_in + stream->num_out;
1334 	cdns->num_ports = stream->num_pdi;
1335 
1336 	return 0;
1337 }
1338 EXPORT_SYMBOL(sdw_cdns_pdi_init);
1339 
cdns_set_initial_frame_shape(int n_rows,int n_cols)1340 static u32 cdns_set_initial_frame_shape(int n_rows, int n_cols)
1341 {
1342 	u32 val;
1343 	int c;
1344 	int r;
1345 
1346 	r = sdw_find_row_index(n_rows);
1347 	c = sdw_find_col_index(n_cols);
1348 
1349 	val = FIELD_PREP(CDNS_MCP_FRAME_SHAPE_ROW_MASK, r);
1350 	val |= FIELD_PREP(CDNS_MCP_FRAME_SHAPE_COL_MASK, c);
1351 
1352 	return val;
1353 }
1354 
cdns_init_clock_ctrl(struct sdw_cdns * cdns)1355 static void cdns_init_clock_ctrl(struct sdw_cdns *cdns)
1356 {
1357 	struct sdw_bus *bus = &cdns->bus;
1358 	struct sdw_master_prop *prop = &bus->prop;
1359 	u32 val;
1360 	u32 ssp_interval;
1361 	int divider;
1362 
1363 	/* Set clock divider */
1364 	divider	= (prop->mclk_freq / prop->max_clk_freq) - 1;
1365 
1366 	cdns_updatel(cdns, CDNS_MCP_CLK_CTRL0,
1367 		     CDNS_MCP_CLK_MCLKD_MASK, divider);
1368 	cdns_updatel(cdns, CDNS_MCP_CLK_CTRL1,
1369 		     CDNS_MCP_CLK_MCLKD_MASK, divider);
1370 
1371 	/*
1372 	 * Frame shape changes after initialization have to be done
1373 	 * with the bank switch mechanism
1374 	 */
1375 	val = cdns_set_initial_frame_shape(prop->default_row,
1376 					   prop->default_col);
1377 	cdns_writel(cdns, CDNS_MCP_FRAME_SHAPE_INIT, val);
1378 
1379 	/* Set SSP interval to default value */
1380 	ssp_interval = prop->default_frame_rate / SDW_CADENCE_GSYNC_HZ;
1381 	cdns_writel(cdns, CDNS_MCP_SSP_CTRL0, ssp_interval);
1382 	cdns_writel(cdns, CDNS_MCP_SSP_CTRL1, ssp_interval);
1383 }
1384 
1385 /**
1386  * sdw_cdns_init() - Cadence initialization
1387  * @cdns: Cadence instance
1388  */
sdw_cdns_init(struct sdw_cdns * cdns)1389 int sdw_cdns_init(struct sdw_cdns *cdns)
1390 {
1391 	u32 val;
1392 
1393 	cdns_init_clock_ctrl(cdns);
1394 
1395 	sdw_cdns_check_self_clearing_bits(cdns, __func__, false, 0);
1396 
1397 	/* reset msg_count to default value of FIFOLEVEL */
1398 	cdns->msg_count = cdns_readl(cdns, CDNS_MCP_FIFOLEVEL);
1399 
1400 	/* flush command FIFOs */
1401 	cdns_updatel(cdns, CDNS_MCP_CONTROL, CDNS_MCP_CONTROL_CMD_RST,
1402 		     CDNS_MCP_CONTROL_CMD_RST);
1403 
1404 	/* Set cmd accept mode */
1405 	cdns_ip_updatel(cdns, CDNS_IP_MCP_CONTROL, CDNS_IP_MCP_CONTROL_CMD_ACCEPT,
1406 			CDNS_IP_MCP_CONTROL_CMD_ACCEPT);
1407 
1408 	/* Configure mcp config */
1409 	val = cdns_readl(cdns, CDNS_MCP_CONFIG);
1410 
1411 	/* Disable auto bus release */
1412 	val &= ~CDNS_MCP_CONFIG_BUS_REL;
1413 
1414 	cdns_writel(cdns, CDNS_MCP_CONFIG, val);
1415 
1416 	/* Configure IP mcp config */
1417 	val = cdns_ip_readl(cdns, CDNS_IP_MCP_CONFIG);
1418 
1419 	/* enable bus operations with clock and data */
1420 	val &= ~CDNS_IP_MCP_CONFIG_OP;
1421 	val |= CDNS_IP_MCP_CONFIG_OP_NORMAL;
1422 
1423 	/* Set cmd mode for Tx and Rx cmds */
1424 	val &= ~CDNS_IP_MCP_CONFIG_CMD;
1425 
1426 	/* Disable sniffer mode */
1427 	val &= ~CDNS_IP_MCP_CONFIG_SNIFFER;
1428 
1429 	if (cdns->bus.multi_link)
1430 		/* Set Multi-master mode to take gsync into account */
1431 		val |= CDNS_IP_MCP_CONFIG_MMASTER;
1432 
1433 	/* leave frame delay to hardware default of 0x1F */
1434 
1435 	/* leave command retry to hardware default of 0 */
1436 
1437 	cdns_ip_writel(cdns, CDNS_IP_MCP_CONFIG, val);
1438 
1439 	/* changes will be committed later */
1440 	return 0;
1441 }
1442 EXPORT_SYMBOL(sdw_cdns_init);
1443 
cdns_bus_conf(struct sdw_bus * bus,struct sdw_bus_params * params)1444 int cdns_bus_conf(struct sdw_bus *bus, struct sdw_bus_params *params)
1445 {
1446 	struct sdw_master_prop *prop = &bus->prop;
1447 	struct sdw_cdns *cdns = bus_to_cdns(bus);
1448 	int mcp_clkctrl_off;
1449 	int divider;
1450 
1451 	if (!params->curr_dr_freq) {
1452 		dev_err(cdns->dev, "NULL curr_dr_freq\n");
1453 		return -EINVAL;
1454 	}
1455 
1456 	divider	= prop->mclk_freq * SDW_DOUBLE_RATE_FACTOR /
1457 		params->curr_dr_freq;
1458 	divider--; /* divider is 1/(N+1) */
1459 
1460 	if (params->next_bank)
1461 		mcp_clkctrl_off = CDNS_MCP_CLK_CTRL1;
1462 	else
1463 		mcp_clkctrl_off = CDNS_MCP_CLK_CTRL0;
1464 
1465 	cdns_updatel(cdns, mcp_clkctrl_off, CDNS_MCP_CLK_MCLKD_MASK, divider);
1466 
1467 	return 0;
1468 }
1469 EXPORT_SYMBOL(cdns_bus_conf);
1470 
cdns_port_params(struct sdw_bus * bus,struct sdw_port_params * p_params,unsigned int bank)1471 static int cdns_port_params(struct sdw_bus *bus,
1472 			    struct sdw_port_params *p_params, unsigned int bank)
1473 {
1474 	struct sdw_cdns *cdns = bus_to_cdns(bus);
1475 	int dpn_config_off_source;
1476 	int dpn_config_off_target;
1477 	int target_num = p_params->num;
1478 	int source_num = p_params->num;
1479 	bool override = false;
1480 	int dpn_config;
1481 
1482 	if (target_num == cdns->pdi_loopback_target &&
1483 	    cdns->pdi_loopback_source != -1) {
1484 		source_num = cdns->pdi_loopback_source;
1485 		override = true;
1486 	}
1487 
1488 	if (bank) {
1489 		dpn_config_off_source = CDNS_DPN_B1_CONFIG(source_num);
1490 		dpn_config_off_target = CDNS_DPN_B1_CONFIG(target_num);
1491 	} else {
1492 		dpn_config_off_source = CDNS_DPN_B0_CONFIG(source_num);
1493 		dpn_config_off_target = CDNS_DPN_B0_CONFIG(target_num);
1494 	}
1495 
1496 	dpn_config = cdns_readl(cdns, dpn_config_off_source);
1497 
1498 	/* use port params if there is no loopback, otherwise use source as is */
1499 	if (!override) {
1500 		u32p_replace_bits(&dpn_config, p_params->bps - 1, CDNS_DPN_CONFIG_WL);
1501 		u32p_replace_bits(&dpn_config, p_params->flow_mode, CDNS_DPN_CONFIG_PORT_FLOW);
1502 		u32p_replace_bits(&dpn_config, p_params->data_mode, CDNS_DPN_CONFIG_PORT_DAT);
1503 	}
1504 
1505 	cdns_writel(cdns, dpn_config_off_target, dpn_config);
1506 
1507 	return 0;
1508 }
1509 
cdns_transport_params(struct sdw_bus * bus,struct sdw_transport_params * t_params,enum sdw_reg_bank bank)1510 static int cdns_transport_params(struct sdw_bus *bus,
1511 				 struct sdw_transport_params *t_params,
1512 				 enum sdw_reg_bank bank)
1513 {
1514 	struct sdw_cdns *cdns = bus_to_cdns(bus);
1515 	int dpn_config;
1516 	int dpn_config_off_source;
1517 	int dpn_config_off_target;
1518 	int dpn_hctrl;
1519 	int dpn_hctrl_off_source;
1520 	int dpn_hctrl_off_target;
1521 	int dpn_offsetctrl;
1522 	int dpn_offsetctrl_off_source;
1523 	int dpn_offsetctrl_off_target;
1524 	int dpn_samplectrl;
1525 	int dpn_samplectrl_off_source;
1526 	int dpn_samplectrl_off_target;
1527 	int source_num = t_params->port_num;
1528 	int target_num = t_params->port_num;
1529 	bool override = false;
1530 
1531 	if (target_num == cdns->pdi_loopback_target &&
1532 	    cdns->pdi_loopback_source != -1) {
1533 		source_num = cdns->pdi_loopback_source;
1534 		override = true;
1535 	}
1536 
1537 	/*
1538 	 * Note: Only full data port is supported on the Master side for
1539 	 * both PCM and PDM ports.
1540 	 */
1541 
1542 	if (bank) {
1543 		dpn_config_off_source = CDNS_DPN_B1_CONFIG(source_num);
1544 		dpn_hctrl_off_source = CDNS_DPN_B1_HCTRL(source_num);
1545 		dpn_offsetctrl_off_source = CDNS_DPN_B1_OFFSET_CTRL(source_num);
1546 		dpn_samplectrl_off_source = CDNS_DPN_B1_SAMPLE_CTRL(source_num);
1547 
1548 		dpn_config_off_target = CDNS_DPN_B1_CONFIG(target_num);
1549 		dpn_hctrl_off_target = CDNS_DPN_B1_HCTRL(target_num);
1550 		dpn_offsetctrl_off_target = CDNS_DPN_B1_OFFSET_CTRL(target_num);
1551 		dpn_samplectrl_off_target = CDNS_DPN_B1_SAMPLE_CTRL(target_num);
1552 
1553 	} else {
1554 		dpn_config_off_source = CDNS_DPN_B0_CONFIG(source_num);
1555 		dpn_hctrl_off_source = CDNS_DPN_B0_HCTRL(source_num);
1556 		dpn_offsetctrl_off_source = CDNS_DPN_B0_OFFSET_CTRL(source_num);
1557 		dpn_samplectrl_off_source = CDNS_DPN_B0_SAMPLE_CTRL(source_num);
1558 
1559 		dpn_config_off_target = CDNS_DPN_B0_CONFIG(target_num);
1560 		dpn_hctrl_off_target = CDNS_DPN_B0_HCTRL(target_num);
1561 		dpn_offsetctrl_off_target = CDNS_DPN_B0_OFFSET_CTRL(target_num);
1562 		dpn_samplectrl_off_target = CDNS_DPN_B0_SAMPLE_CTRL(target_num);
1563 	}
1564 
1565 	dpn_config = cdns_readl(cdns, dpn_config_off_source);
1566 	if (!override) {
1567 		u32p_replace_bits(&dpn_config, t_params->blk_grp_ctrl, CDNS_DPN_CONFIG_BGC);
1568 		u32p_replace_bits(&dpn_config, t_params->blk_pkg_mode, CDNS_DPN_CONFIG_BPM);
1569 	}
1570 	cdns_writel(cdns, dpn_config_off_target, dpn_config);
1571 
1572 	if (!override) {
1573 		dpn_offsetctrl = 0;
1574 		u32p_replace_bits(&dpn_offsetctrl, t_params->offset1, CDNS_DPN_OFFSET_CTRL_1);
1575 		u32p_replace_bits(&dpn_offsetctrl, t_params->offset2, CDNS_DPN_OFFSET_CTRL_2);
1576 	} else {
1577 		dpn_offsetctrl = cdns_readl(cdns, dpn_offsetctrl_off_source);
1578 	}
1579 	cdns_writel(cdns, dpn_offsetctrl_off_target,  dpn_offsetctrl);
1580 
1581 	if (!override) {
1582 		dpn_hctrl = 0;
1583 		u32p_replace_bits(&dpn_hctrl, t_params->hstart, CDNS_DPN_HCTRL_HSTART);
1584 		u32p_replace_bits(&dpn_hctrl, t_params->hstop, CDNS_DPN_HCTRL_HSTOP);
1585 		u32p_replace_bits(&dpn_hctrl, t_params->lane_ctrl, CDNS_DPN_HCTRL_LCTRL);
1586 	} else {
1587 		dpn_hctrl = cdns_readl(cdns, dpn_hctrl_off_source);
1588 	}
1589 	cdns_writel(cdns, dpn_hctrl_off_target, dpn_hctrl);
1590 
1591 	if (!override)
1592 		dpn_samplectrl = t_params->sample_interval - 1;
1593 	else
1594 		dpn_samplectrl = cdns_readl(cdns, dpn_samplectrl_off_source);
1595 	cdns_writel(cdns, dpn_samplectrl_off_target, dpn_samplectrl);
1596 
1597 	return 0;
1598 }
1599 
cdns_port_enable(struct sdw_bus * bus,struct sdw_enable_ch * enable_ch,unsigned int bank)1600 static int cdns_port_enable(struct sdw_bus *bus,
1601 			    struct sdw_enable_ch *enable_ch, unsigned int bank)
1602 {
1603 	struct sdw_cdns *cdns = bus_to_cdns(bus);
1604 	int dpn_chnen_off, ch_mask;
1605 
1606 	if (bank)
1607 		dpn_chnen_off = CDNS_DPN_B1_CH_EN(enable_ch->port_num);
1608 	else
1609 		dpn_chnen_off = CDNS_DPN_B0_CH_EN(enable_ch->port_num);
1610 
1611 	ch_mask = enable_ch->ch_mask * enable_ch->enable;
1612 	cdns_writel(cdns, dpn_chnen_off, ch_mask);
1613 
1614 	return 0;
1615 }
1616 
1617 static const struct sdw_master_port_ops cdns_port_ops = {
1618 	.dpn_set_port_params = cdns_port_params,
1619 	.dpn_set_port_transport_params = cdns_transport_params,
1620 	.dpn_port_enable_ch = cdns_port_enable,
1621 };
1622 
1623 /**
1624  * sdw_cdns_is_clock_stop: Check clock status
1625  *
1626  * @cdns: Cadence instance
1627  */
sdw_cdns_is_clock_stop(struct sdw_cdns * cdns)1628 bool sdw_cdns_is_clock_stop(struct sdw_cdns *cdns)
1629 {
1630 	return !!(cdns_readl(cdns, CDNS_MCP_STAT) & CDNS_MCP_STAT_CLK_STOP);
1631 }
1632 EXPORT_SYMBOL(sdw_cdns_is_clock_stop);
1633 
1634 /**
1635  * sdw_cdns_clock_stop: Cadence clock stop configuration routine
1636  *
1637  * @cdns: Cadence instance
1638  * @block_wake: prevent wakes if required by the platform
1639  */
sdw_cdns_clock_stop(struct sdw_cdns * cdns,bool block_wake)1640 int sdw_cdns_clock_stop(struct sdw_cdns *cdns, bool block_wake)
1641 {
1642 	bool slave_present = false;
1643 	struct sdw_slave *slave;
1644 	int ret;
1645 
1646 	sdw_cdns_check_self_clearing_bits(cdns, __func__, false, 0);
1647 
1648 	/* Check suspend status */
1649 	if (sdw_cdns_is_clock_stop(cdns)) {
1650 		dev_dbg(cdns->dev, "Clock is already stopped\n");
1651 		return 0;
1652 	}
1653 
1654 	/*
1655 	 * Before entering clock stop we mask the Slave
1656 	 * interrupts. This helps avoid having to deal with e.g. a
1657 	 * Slave becoming UNATTACHED while the clock is being stopped
1658 	 */
1659 	cdns_enable_slave_interrupts(cdns, false);
1660 
1661 	/*
1662 	 * For specific platforms, it is required to be able to put
1663 	 * master into a state in which it ignores wake-up trials
1664 	 * in clock stop state
1665 	 */
1666 	if (block_wake)
1667 		cdns_ip_updatel(cdns, CDNS_IP_MCP_CONTROL,
1668 				CDNS_IP_MCP_CONTROL_BLOCK_WAKEUP,
1669 				CDNS_IP_MCP_CONTROL_BLOCK_WAKEUP);
1670 
1671 	list_for_each_entry(slave, &cdns->bus.slaves, node) {
1672 		if (slave->status == SDW_SLAVE_ATTACHED ||
1673 		    slave->status == SDW_SLAVE_ALERT) {
1674 			slave_present = true;
1675 			break;
1676 		}
1677 	}
1678 
1679 	/* commit changes */
1680 	ret = cdns_config_update(cdns);
1681 	if (ret < 0) {
1682 		dev_err(cdns->dev, "%s: config_update failed\n", __func__);
1683 		return ret;
1684 	}
1685 
1686 	/* Prepare slaves for clock stop */
1687 	if (slave_present) {
1688 		ret = sdw_bus_prep_clk_stop(&cdns->bus);
1689 		if (ret < 0 && ret != -ENODATA) {
1690 			dev_err(cdns->dev, "prepare clock stop failed %d\n", ret);
1691 			return ret;
1692 		}
1693 	}
1694 
1695 	/*
1696 	 * Enter clock stop mode and only report errors if there are
1697 	 * Slave devices present (ALERT or ATTACHED)
1698 	 */
1699 	ret = sdw_bus_clk_stop(&cdns->bus);
1700 	if (ret < 0 && slave_present && ret != -ENODATA) {
1701 		dev_err(cdns->dev, "bus clock stop failed %d\n", ret);
1702 		return ret;
1703 	}
1704 
1705 	ret = cdns_set_wait(cdns, CDNS_MCP_STAT,
1706 			    CDNS_MCP_STAT_CLK_STOP,
1707 			    CDNS_MCP_STAT_CLK_STOP);
1708 	if (ret < 0)
1709 		dev_err(cdns->dev, "Clock stop failed %d\n", ret);
1710 
1711 	return ret;
1712 }
1713 EXPORT_SYMBOL(sdw_cdns_clock_stop);
1714 
1715 /**
1716  * sdw_cdns_clock_restart: Cadence PM clock restart configuration routine
1717  *
1718  * @cdns: Cadence instance
1719  * @bus_reset: context may be lost while in low power modes and the bus
1720  * may require a Severe Reset and re-enumeration after a wake.
1721  */
sdw_cdns_clock_restart(struct sdw_cdns * cdns,bool bus_reset)1722 int sdw_cdns_clock_restart(struct sdw_cdns *cdns, bool bus_reset)
1723 {
1724 	int ret;
1725 
1726 	/* unmask Slave interrupts that were masked when stopping the clock */
1727 	cdns_enable_slave_interrupts(cdns, true);
1728 
1729 	ret = cdns_clear_bit(cdns, CDNS_MCP_CONTROL,
1730 			     CDNS_MCP_CONTROL_CLK_STOP_CLR);
1731 	if (ret < 0) {
1732 		dev_err(cdns->dev, "Couldn't exit from clock stop\n");
1733 		return ret;
1734 	}
1735 
1736 	ret = cdns_set_wait(cdns, CDNS_MCP_STAT, CDNS_MCP_STAT_CLK_STOP, 0);
1737 	if (ret < 0) {
1738 		dev_err(cdns->dev, "clock stop exit failed %d\n", ret);
1739 		return ret;
1740 	}
1741 
1742 	cdns_ip_updatel(cdns, CDNS_IP_MCP_CONTROL,
1743 			CDNS_IP_MCP_CONTROL_BLOCK_WAKEUP, 0);
1744 
1745 	cdns_ip_updatel(cdns, CDNS_IP_MCP_CONTROL, CDNS_IP_MCP_CONTROL_CMD_ACCEPT,
1746 			CDNS_IP_MCP_CONTROL_CMD_ACCEPT);
1747 
1748 	if (!bus_reset) {
1749 
1750 		/* enable bus operations with clock and data */
1751 		cdns_ip_updatel(cdns, CDNS_IP_MCP_CONFIG,
1752 				CDNS_IP_MCP_CONFIG_OP,
1753 				CDNS_IP_MCP_CONFIG_OP_NORMAL);
1754 
1755 		ret = cdns_config_update(cdns);
1756 		if (ret < 0) {
1757 			dev_err(cdns->dev, "%s: config_update failed\n", __func__);
1758 			return ret;
1759 		}
1760 
1761 		ret = sdw_bus_exit_clk_stop(&cdns->bus);
1762 		if (ret < 0)
1763 			dev_err(cdns->dev, "bus failed to exit clock stop %d\n", ret);
1764 	}
1765 
1766 	return ret;
1767 }
1768 EXPORT_SYMBOL(sdw_cdns_clock_restart);
1769 
1770 /**
1771  * sdw_cdns_probe() - Cadence probe routine
1772  * @cdns: Cadence instance
1773  */
sdw_cdns_probe(struct sdw_cdns * cdns)1774 int sdw_cdns_probe(struct sdw_cdns *cdns)
1775 {
1776 	init_completion(&cdns->tx_complete);
1777 	cdns->bus.port_ops = &cdns_port_ops;
1778 
1779 	mutex_init(&cdns->status_update_lock);
1780 
1781 	INIT_WORK(&cdns->work, cdns_update_slave_status_work);
1782 	INIT_DELAYED_WORK(&cdns->attach_dwork, cdns_check_attached_status_dwork);
1783 
1784 	return 0;
1785 }
1786 EXPORT_SYMBOL(sdw_cdns_probe);
1787 
cdns_set_sdw_stream(struct snd_soc_dai * dai,void * stream,int direction)1788 int cdns_set_sdw_stream(struct snd_soc_dai *dai,
1789 			void *stream, int direction)
1790 {
1791 	struct sdw_cdns *cdns = snd_soc_dai_get_drvdata(dai);
1792 	struct sdw_cdns_dai_runtime *dai_runtime;
1793 
1794 	dai_runtime = cdns->dai_runtime_array[dai->id];
1795 
1796 	if (stream) {
1797 		/* first paranoia check */
1798 		if (dai_runtime) {
1799 			dev_err(dai->dev,
1800 				"dai_runtime already allocated for dai %s\n",
1801 				dai->name);
1802 			return -EINVAL;
1803 		}
1804 
1805 		/* allocate and set dai_runtime info */
1806 		dai_runtime = kzalloc(sizeof(*dai_runtime), GFP_KERNEL);
1807 		if (!dai_runtime)
1808 			return -ENOMEM;
1809 
1810 		dai_runtime->stream_type = SDW_STREAM_PCM;
1811 
1812 		dai_runtime->bus = &cdns->bus;
1813 		dai_runtime->link_id = cdns->instance;
1814 
1815 		dai_runtime->stream = stream;
1816 		dai_runtime->direction = direction;
1817 
1818 		cdns->dai_runtime_array[dai->id] = dai_runtime;
1819 	} else {
1820 		/* second paranoia check */
1821 		if (!dai_runtime) {
1822 			dev_err(dai->dev,
1823 				"dai_runtime not allocated for dai %s\n",
1824 				dai->name);
1825 			return -EINVAL;
1826 		}
1827 
1828 		/* for NULL stream we release allocated dai_runtime */
1829 		kfree(dai_runtime);
1830 		cdns->dai_runtime_array[dai->id] = NULL;
1831 	}
1832 	return 0;
1833 }
1834 EXPORT_SYMBOL(cdns_set_sdw_stream);
1835 
1836 /**
1837  * cdns_find_pdi() - Find a free PDI
1838  *
1839  * @cdns: Cadence instance
1840  * @offset: Starting offset
1841  * @num: Number of PDIs
1842  * @pdi: PDI instances
1843  * @dai_id: DAI id
1844  *
1845  * Find a PDI for a given PDI array. The PDI num and dai_id are
1846  * expected to match, return NULL otherwise.
1847  */
cdns_find_pdi(struct sdw_cdns * cdns,unsigned int offset,unsigned int num,struct sdw_cdns_pdi * pdi,int dai_id)1848 static struct sdw_cdns_pdi *cdns_find_pdi(struct sdw_cdns *cdns,
1849 					  unsigned int offset,
1850 					  unsigned int num,
1851 					  struct sdw_cdns_pdi *pdi,
1852 					  int dai_id)
1853 {
1854 	int i;
1855 
1856 	for (i = offset; i < offset + num; i++)
1857 		if (pdi[i].num == dai_id)
1858 			return &pdi[i];
1859 
1860 	return NULL;
1861 }
1862 
1863 /**
1864  * sdw_cdns_config_stream: Configure a stream
1865  *
1866  * @cdns: Cadence instance
1867  * @ch: Channel count
1868  * @dir: Data direction
1869  * @pdi: PDI to be used
1870  */
sdw_cdns_config_stream(struct sdw_cdns * cdns,u32 ch,u32 dir,struct sdw_cdns_pdi * pdi)1871 void sdw_cdns_config_stream(struct sdw_cdns *cdns,
1872 			    u32 ch, u32 dir, struct sdw_cdns_pdi *pdi)
1873 {
1874 	u32 offset, val = 0;
1875 
1876 	if (dir == SDW_DATA_DIR_RX) {
1877 		val = CDNS_PORTCTRL_DIRN;
1878 
1879 		if (cdns->bus.params.m_data_mode != SDW_PORT_DATA_MODE_NORMAL)
1880 			val |= CDNS_PORTCTRL_TEST_FAILED;
1881 	}
1882 	offset = CDNS_PORTCTRL + pdi->num * CDNS_PORT_OFFSET;
1883 	cdns_updatel(cdns, offset,
1884 		     CDNS_PORTCTRL_DIRN | CDNS_PORTCTRL_TEST_FAILED,
1885 		     val);
1886 
1887 	val = pdi->num;
1888 	val |= CDNS_PDI_CONFIG_SOFT_RESET;
1889 	val |= FIELD_PREP(CDNS_PDI_CONFIG_CHANNEL, (1 << ch) - 1);
1890 	cdns_writel(cdns, CDNS_PDI_CONFIG(pdi->num), val);
1891 }
1892 EXPORT_SYMBOL(sdw_cdns_config_stream);
1893 
1894 /**
1895  * sdw_cdns_alloc_pdi() - Allocate a PDI
1896  *
1897  * @cdns: Cadence instance
1898  * @stream: Stream to be allocated
1899  * @ch: Channel count
1900  * @dir: Data direction
1901  * @dai_id: DAI id
1902  */
sdw_cdns_alloc_pdi(struct sdw_cdns * cdns,struct sdw_cdns_streams * stream,u32 ch,u32 dir,int dai_id)1903 struct sdw_cdns_pdi *sdw_cdns_alloc_pdi(struct sdw_cdns *cdns,
1904 					struct sdw_cdns_streams *stream,
1905 					u32 ch, u32 dir, int dai_id)
1906 {
1907 	struct sdw_cdns_pdi *pdi = NULL;
1908 
1909 	if (dir == SDW_DATA_DIR_RX)
1910 		pdi = cdns_find_pdi(cdns, 0, stream->num_in, stream->in,
1911 				    dai_id);
1912 	else
1913 		pdi = cdns_find_pdi(cdns, 0, stream->num_out, stream->out,
1914 				    dai_id);
1915 
1916 	/* check if we found a PDI, else find in bi-directional */
1917 	if (!pdi)
1918 		pdi = cdns_find_pdi(cdns, 0, stream->num_bd, stream->bd,
1919 				    dai_id);
1920 
1921 	if (pdi) {
1922 		pdi->l_ch_num = 0;
1923 		pdi->h_ch_num = ch - 1;
1924 		pdi->dir = dir;
1925 		pdi->ch_count = ch;
1926 	}
1927 
1928 	return pdi;
1929 }
1930 EXPORT_SYMBOL(sdw_cdns_alloc_pdi);
1931 
1932 MODULE_LICENSE("Dual BSD/GPL");
1933 MODULE_DESCRIPTION("Cadence Soundwire Library");
1934