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