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