blob: 192dac10f0c2c0d326b297e09cf2d517abc79186 [file] [log] [blame]
Vinod Koul2f52a512017-12-14 11:19:41 +05301// 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>
Pierre-Louis Bossartaa850662019-08-21 13:58:19 -050011#include <linux/debugfs.h>
Vinod Koul2f52a512017-12-14 11:19:41 +053012#include <linux/interrupt.h>
Jan Kotas18de65d2019-04-04 09:12:21 +010013#include <linux/io.h>
Vinod Koul2f52a512017-12-14 11:19:41 +053014#include <linux/module.h>
15#include <linux/mod_devicetable.h>
Pierre-Louis Bossart32d2a892020-09-08 21:45:21 +080016#include <linux/pm_runtime.h>
Vinod Koul2f52a512017-12-14 11:19:41 +053017#include <linux/soundwire/sdw_registers.h>
18#include <linux/soundwire/sdw.h>
Vinod Koul5d6b3c82018-04-26 18:38:53 +053019#include <sound/pcm_params.h>
20#include <sound/soc.h>
Bard Liao4a98a6b2020-07-16 23:09:45 +080021#include <linux/workqueue.h>
Vinod Koul2f52a512017-12-14 11:19:41 +053022#include "bus.h"
23#include "cadence_master.h"
24
Pierre-Louis Bossart04592dc2019-08-05 19:55:21 -050025static int interrupt_mask;
26module_param_named(cnds_mcp_int_mask, interrupt_mask, int, 0444);
27MODULE_PARM_DESC(cdns_mcp_int_mask, "Cadence MCP IntMask");
28
Vinod Koul2f52a512017-12-14 11:19:41 +053029#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
Vinod Koul2f52a512017-12-14 11:19:41 +053053#define CDNS_MCP_CMDCTRL 0x8
Pierre-Louis Bossart32d2a892020-09-08 21:45:21 +080054
55#define CDNS_MCP_CMDCTRL_INSERT_PARITY_ERR BIT(2)
56
Vinod Koul2f52a512017-12-14 11:19:41 +053057#define CDNS_MCP_SSPSTAT 0xC
58#define CDNS_MCP_FRAME_SHAPE 0x10
59#define CDNS_MCP_FRAME_SHAPE_INIT 0x14
Pierre-Louis Bossart05be59a2019-08-05 19:55:11 -050060#define CDNS_MCP_FRAME_SHAPE_COL_MASK GENMASK(2, 0)
Vinod Koul3cf25d62020-09-03 17:15:01 +053061#define CDNS_MCP_FRAME_SHAPE_ROW_MASK GENMASK(7, 3)
Vinod Koul2f52a512017-12-14 11:19:41 +053062
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
Rander Wanga50954e2019-08-05 19:55:18 -050071#define CDNS_MCP_CLK_MCLKD_MASK GENMASK(7, 0)
Vinod Koul2f52a512017-12-14 11:19:41 +053072
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)
Pierre-Louis Bossarta2cff9e2020-01-10 15:57:26 -060082#define CDNS_MCP_INT_RESERVED1 GENMASK(30, 17)
Vinod Koul2f52a512017-12-14 11:19:41 +053083#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)
Pierre-Louis Bossart9b5884a2019-08-05 19:55:08 -050092#define CDNS_MCP_INT_PARITY BIT(8)
Vinod Koul2f52a512017-12-14 11:19:41 +053093#define CDNS_MCP_INT_CMD_ERR BIT(7)
Pierre-Louis Bossarta2cff9e2020-01-10 15:57:26 -060094#define CDNS_MCP_INT_RESERVED2 GENMASK(6, 4)
Pierre-Louis Bossart9b5884a2019-08-05 19:55:08 -050095#define CDNS_MCP_INT_RX_NE BIT(3)
Vinod Koul2f52a512017-12-14 11:19:41 +053096#define CDNS_MCP_INT_RX_WL BIT(2)
97#define CDNS_MCP_INT_TXE BIT(1)
Pierre-Louis Bossart9b5884a2019-08-05 19:55:08 -050098#define CDNS_MCP_INT_TXF BIT(0)
Pierre-Louis Bossarta2cff9e2020-01-10 15:57:26 -060099#define CDNS_MCP_INT_RESERVED (CDNS_MCP_INT_RESERVED1 | CDNS_MCP_INT_RESERVED2)
Vinod Koul2f52a512017-12-14 11:19:41 +0530100
101#define CDNS_MCP_INTSET 0x4C
102
Pierre-Louis Bossartb07dd9b2019-07-25 18:40:05 -0500103#define CDNS_MCP_SLAVE_STAT 0x50
104#define CDNS_MCP_SLAVE_STAT_MASK GENMASK(1, 0)
Vinod Koul2f52a512017-12-14 11:19:41 +0530105
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
Pierre-Louis Bossart664b1652019-07-25 18:40:06 -0500118#define CDNS_MCP_SLAVE_INTMASK0_MASK GENMASK(31, 0)
119#define CDNS_MCP_SLAVE_INTMASK1_MASK GENMASK(15, 0)
Vinod Koul2f52a512017-12-14 11:19:41 +0530120
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)
Vinod Koul3cf25d62020-09-03 17:15:01 +0530136#define CDNS_MCP_CMD_REG_ADDR GENMASK(23, 8)
Vinod Koul2f52a512017-12-14 11:19:41 +0530137#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
Pierre-Louis Bossart9e4e6012020-09-21 03:32:07 +0800178#define CDNS_PORTCTRL_TEST_FAILED BIT(1)
Vinod Koul2f52a512017-12-14 11:19:41 +0530179#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 */
Pierre-Louis Bossart0cff9912021-01-15 14:16:51 +0800191#define CDNS_TX_TIMEOUT 500
Vinod Koul2f52a512017-12-14 11:19:41 +0530192
Vinod Koul2f52a512017-12-14 11:19:41 +0530193#define CDNS_SCP_RX_FIFOLEVEL 0x2
194
195/*
196 * register accessor helpers
197 */
198static inline u32 cdns_readl(struct sdw_cdns *cdns, int offset)
199{
200 return readl(cdns->registers + offset);
201}
202
203static inline void cdns_writel(struct sdw_cdns *cdns, int offset, u32 value)
204{
205 writel(value, cdns->registers + offset);
206}
207
208static 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
Rander Wang10325042020-03-17 11:33:17 -0500218static 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
Pierre-Louis Bossart12632452020-03-17 11:33:20 -0500236static 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
Vinod Koul2f52a512017-12-14 11:19:41 +0530244/*
Pierre-Louis Bossart49ea07d2019-10-22 18:54:44 -0500245 * 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 */
Pierre-Louis Bossartce1acf02020-03-17 11:33:13 -0500248static int cdns_config_update(struct sdw_cdns *cdns)
Pierre-Louis Bossart49ea07d2019-10-22 18:54:44 -0500249{
250 int ret;
251
Pierre-Louis Bossart9bc87cc2020-03-17 11:33:16 -0500252 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
Pierre-Louis Bossart49ea07d2019-10-22 18:54:44 -0500257 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/*
Pierre-Louis Bossartaa850662019-08-21 13:58:19 -0500266 * debugfs
267 */
268#ifdef CONFIG_DEBUG_FS
269
270#define RD_BUF (2 * PAGE_SIZE)
271
272static 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
279static 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
Pierre-Louis Bossart807c15b2019-09-16 14:23:47 -0500313 num_ports = cdns->num_ports;
Pierre-Louis Bossartaa850662019-08-21 13:58:19 -0500314
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}
352DEFINE_SHOW_ATTRIBUTE(cdns_reg);
353
Pierre-Louis Bossart675d4c92019-10-22 18:54:45 -0500354static 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
372DEFINE_DEBUGFS_ATTRIBUTE(cdns_hw_reset_fops, NULL, cdns_hw_reset, "%llu\n");
373
Pierre-Louis Bossart32d2a892020-09-08 21:45:21 +0800374static 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_get_sync(bus->dev);
390 if (ret < 0 && ret != -EACCES) {
391 dev_err_ratelimited(cdns->dev,
392 "pm_runtime_get_sync failed in %s, ret %d\n",
393 __func__, ret);
394 pm_runtime_put_noidle(bus->dev);
395 return ret;
396 }
397
398 /*
399 * wait long enough for Slave(s) to be in steady state. This
400 * does not need to be super precise.
401 */
402 msleep(200);
403
404 /*
405 * Take the bus lock here to make sure that any bus transactions
406 * will be queued while we inject a parity error on a dummy read
407 */
408 mutex_lock(&bus->bus_lock);
409
410 /* program hardware to inject parity error */
411 cdns_updatel(cdns, CDNS_MCP_CMDCTRL,
412 CDNS_MCP_CMDCTRL_INSERT_PARITY_ERR,
413 CDNS_MCP_CMDCTRL_INSERT_PARITY_ERR);
414
415 /* commit changes */
416 cdns_updatel(cdns, CDNS_MCP_CONFIG_UPDATE,
417 CDNS_MCP_CONFIG_UPDATE_BIT,
418 CDNS_MCP_CONFIG_UPDATE_BIT);
419
420 /* do a broadcast dummy read to avoid bus clashes */
421 ret = sdw_bread_no_pm_unlocked(&cdns->bus, 0xf, SDW_SCP_DEVID_0);
422 dev_info(cdns->dev, "parity error injection, read: %d\n", ret);
423
424 /* program hardware to disable parity error */
425 cdns_updatel(cdns, CDNS_MCP_CMDCTRL,
426 CDNS_MCP_CMDCTRL_INSERT_PARITY_ERR,
427 0);
428
429 /* commit changes */
430 cdns_updatel(cdns, CDNS_MCP_CONFIG_UPDATE,
431 CDNS_MCP_CONFIG_UPDATE_BIT,
432 CDNS_MCP_CONFIG_UPDATE_BIT);
433
434 /* Continue bus operation with parity error injection disabled */
435 mutex_unlock(&bus->bus_lock);
436
437 /* Userspace changed the hardware state behind the kernel's back */
438 add_taint(TAINT_USER, LOCKDEP_STILL_OK);
439
440 /*
441 * allow Master device to enter pm_runtime suspend. This may
442 * also result in Slave devices suspending.
443 */
444 pm_runtime_mark_last_busy(bus->dev);
445 pm_runtime_put_autosuspend(bus->dev);
446
447 return 0;
448}
449
450DEFINE_DEBUGFS_ATTRIBUTE(cdns_parity_error_fops, NULL,
451 cdns_parity_error_injection, "%llu\n");
452
Pierre-Louis Bossartaa850662019-08-21 13:58:19 -0500453/**
454 * sdw_cdns_debugfs_init() - Cadence debugfs init
455 * @cdns: Cadence instance
456 * @root: debugfs root
457 */
458void sdw_cdns_debugfs_init(struct sdw_cdns *cdns, struct dentry *root)
459{
460 debugfs_create_file("cdns-registers", 0400, root, cdns, &cdns_reg_fops);
Pierre-Louis Bossart675d4c92019-10-22 18:54:45 -0500461
462 debugfs_create_file("cdns-hw-reset", 0200, root, cdns,
463 &cdns_hw_reset_fops);
Pierre-Louis Bossart32d2a892020-09-08 21:45:21 +0800464
465 debugfs_create_file("cdns-parity-error-injection", 0200, root, cdns,
466 &cdns_parity_error_fops);
Pierre-Louis Bossartaa850662019-08-21 13:58:19 -0500467}
468EXPORT_SYMBOL_GPL(sdw_cdns_debugfs_init);
469
470#endif /* CONFIG_DEBUG_FS */
471
472/*
Sanyog Kale956baa12017-12-14 11:19:42 +0530473 * IO Calls
474 */
Pierre-Louis Bossartbbb63812019-05-01 10:57:41 -0500475static enum sdw_command_response
476cdns_fill_msg_resp(struct sdw_cdns *cdns,
477 struct sdw_msg *msg, int count, int offset)
Sanyog Kale956baa12017-12-14 11:19:42 +0530478{
479 int nack = 0, no_ack = 0;
480 int i;
481
482 /* check message response */
483 for (i = 0; i < count; i++) {
484 if (!(cdns->response_buf[i] & CDNS_MCP_RESP_ACK)) {
485 no_ack = 1;
Pierre-Louis Bossart9a0c7982021-01-15 13:37:38 +0800486 dev_vdbg(cdns->dev, "Msg Ack not received, cmd %d\n", i);
Pierre-Louis Bossartdb9d9f92021-01-15 13:37:37 +0800487 }
488 if (cdns->response_buf[i] & CDNS_MCP_RESP_NACK) {
489 nack = 1;
Pierre-Louis Bossart9a0c7982021-01-15 13:37:38 +0800490 dev_err_ratelimited(cdns->dev, "Msg NACK received, cmd %d\n", i);
Sanyog Kale956baa12017-12-14 11:19:42 +0530491 }
492 }
493
494 if (nack) {
Pierre-Louis Bossarteb7df4c2019-05-22 14:47:26 -0500495 dev_err_ratelimited(cdns->dev, "Msg NACKed for Slave %d\n", msg->dev_num);
Sanyog Kale956baa12017-12-14 11:19:42 +0530496 return SDW_CMD_FAIL;
Guennadi Liakhovetski6f7219f2020-05-08 02:30:46 +0200497 }
498
499 if (no_ack) {
Pierre-Louis Bossarteb7df4c2019-05-22 14:47:26 -0500500 dev_dbg_ratelimited(cdns->dev, "Msg ignored for Slave %d\n", msg->dev_num);
Sanyog Kale956baa12017-12-14 11:19:42 +0530501 return SDW_CMD_IGNORED;
502 }
503
504 /* fill response */
505 for (i = 0; i < count; i++)
Vinod Koul3cf25d62020-09-03 17:15:01 +0530506 msg->buf[i + offset] = FIELD_GET(CDNS_MCP_RESP_RDATA, cdns->response_buf[i]);
Sanyog Kale956baa12017-12-14 11:19:42 +0530507
508 return SDW_CMD_OK;
509}
510
511static enum sdw_command_response
512_cdns_xfer_msg(struct sdw_cdns *cdns, struct sdw_msg *msg, int cmd,
Pierre-Louis Bossartbbb63812019-05-01 10:57:41 -0500513 int offset, int count, bool defer)
Sanyog Kale956baa12017-12-14 11:19:42 +0530514{
515 unsigned long time;
516 u32 base, i, data;
517 u16 addr;
518
519 /* Program the watermark level for RX FIFO */
520 if (cdns->msg_count != count) {
521 cdns_writel(cdns, CDNS_MCP_FIFOLEVEL, count);
522 cdns->msg_count = count;
523 }
524
525 base = CDNS_MCP_CMD_BASE;
526 addr = msg->addr;
527
528 for (i = 0; i < count; i++) {
Vinod Koul3cf25d62020-09-03 17:15:01 +0530529 data = FIELD_PREP(CDNS_MCP_CMD_DEV_ADDR, msg->dev_num);
530 data |= FIELD_PREP(CDNS_MCP_CMD_COMMAND, cmd);
531 data |= FIELD_PREP(CDNS_MCP_CMD_REG_ADDR, addr);
532 addr++;
Sanyog Kale956baa12017-12-14 11:19:42 +0530533
534 if (msg->flags == SDW_MSG_FLAG_WRITE)
535 data |= msg->buf[i + offset];
536
Vinod Koul3cf25d62020-09-03 17:15:01 +0530537 data |= FIELD_PREP(CDNS_MCP_CMD_SSP_TAG, msg->ssp_sync);
Sanyog Kale956baa12017-12-14 11:19:42 +0530538 cdns_writel(cdns, base, data);
539 base += CDNS_MCP_CMD_WORD_LEN;
540 }
541
542 if (defer)
543 return SDW_CMD_OK;
544
545 /* wait for timeout or response */
546 time = wait_for_completion_timeout(&cdns->tx_complete,
Pierre-Louis Bossartbbb63812019-05-01 10:57:41 -0500547 msecs_to_jiffies(CDNS_TX_TIMEOUT));
Sanyog Kale956baa12017-12-14 11:19:42 +0530548 if (!time) {
Pierre-Louis Bossart53ee9572020-01-10 15:57:28 -0600549 dev_err(cdns->dev, "IO transfer timed out, cmd %d device %d addr %x len %d\n",
550 cmd, msg->dev_num, msg->addr, msg->len);
Sanyog Kale956baa12017-12-14 11:19:42 +0530551 msg->len = 0;
552 return SDW_CMD_TIMEOUT;
553 }
554
555 return cdns_fill_msg_resp(cdns, msg, count, offset);
556}
557
Pierre-Louis Bossartbbb63812019-05-01 10:57:41 -0500558static enum sdw_command_response
559cdns_program_scp_addr(struct sdw_cdns *cdns, struct sdw_msg *msg)
Sanyog Kale956baa12017-12-14 11:19:42 +0530560{
561 int nack = 0, no_ack = 0;
562 unsigned long time;
563 u32 data[2], base;
564 int i;
565
566 /* Program the watermark level for RX FIFO */
567 if (cdns->msg_count != CDNS_SCP_RX_FIFOLEVEL) {
568 cdns_writel(cdns, CDNS_MCP_FIFOLEVEL, CDNS_SCP_RX_FIFOLEVEL);
569 cdns->msg_count = CDNS_SCP_RX_FIFOLEVEL;
570 }
571
Vinod Koul3cf25d62020-09-03 17:15:01 +0530572 data[0] = FIELD_PREP(CDNS_MCP_CMD_DEV_ADDR, msg->dev_num);
573 data[0] |= FIELD_PREP(CDNS_MCP_CMD_COMMAND, 0x3);
Sanyog Kale956baa12017-12-14 11:19:42 +0530574 data[1] = data[0];
575
Vinod Koul3cf25d62020-09-03 17:15:01 +0530576 data[0] |= FIELD_PREP(CDNS_MCP_CMD_REG_ADDR, SDW_SCP_ADDRPAGE1);
577 data[1] |= FIELD_PREP(CDNS_MCP_CMD_REG_ADDR, SDW_SCP_ADDRPAGE2);
Sanyog Kale956baa12017-12-14 11:19:42 +0530578
579 data[0] |= msg->addr_page1;
580 data[1] |= msg->addr_page2;
581
582 base = CDNS_MCP_CMD_BASE;
583 cdns_writel(cdns, base, data[0]);
584 base += CDNS_MCP_CMD_WORD_LEN;
585 cdns_writel(cdns, base, data[1]);
586
587 time = wait_for_completion_timeout(&cdns->tx_complete,
Pierre-Louis Bossartbbb63812019-05-01 10:57:41 -0500588 msecs_to_jiffies(CDNS_TX_TIMEOUT));
Sanyog Kale956baa12017-12-14 11:19:42 +0530589 if (!time) {
590 dev_err(cdns->dev, "SCP Msg trf timed out\n");
591 msg->len = 0;
592 return SDW_CMD_TIMEOUT;
593 }
594
595 /* check response the writes */
596 for (i = 0; i < 2; i++) {
597 if (!(cdns->response_buf[i] & CDNS_MCP_RESP_ACK)) {
598 no_ack = 1;
Pierre-Louis Bossart17ed5be2019-05-01 10:57:45 -0500599 dev_err(cdns->dev, "Program SCP Ack not received\n");
Sanyog Kale956baa12017-12-14 11:19:42 +0530600 if (cdns->response_buf[i] & CDNS_MCP_RESP_NACK) {
601 nack = 1;
Pierre-Louis Bossart17ed5be2019-05-01 10:57:45 -0500602 dev_err(cdns->dev, "Program SCP NACK received\n");
Sanyog Kale956baa12017-12-14 11:19:42 +0530603 }
604 }
605 }
606
607 /* For NACK, NO ack, don't return err if we are in Broadcast mode */
608 if (nack) {
Pierre-Louis Bossarteb7df4c2019-05-22 14:47:26 -0500609 dev_err_ratelimited(cdns->dev,
610 "SCP_addrpage NACKed for Slave %d\n", msg->dev_num);
Sanyog Kale956baa12017-12-14 11:19:42 +0530611 return SDW_CMD_FAIL;
Guennadi Liakhovetski6f7219f2020-05-08 02:30:46 +0200612 }
613
614 if (no_ack) {
Pierre-Louis Bossarteb7df4c2019-05-22 14:47:26 -0500615 dev_dbg_ratelimited(cdns->dev,
616 "SCP_addrpage ignored for Slave %d\n", msg->dev_num);
Sanyog Kale956baa12017-12-14 11:19:42 +0530617 return SDW_CMD_IGNORED;
618 }
619
620 return SDW_CMD_OK;
621}
622
623static int cdns_prep_msg(struct sdw_cdns *cdns, struct sdw_msg *msg, int *cmd)
624{
625 int ret;
626
627 if (msg->page) {
628 ret = cdns_program_scp_addr(cdns, msg);
629 if (ret) {
630 msg->len = 0;
631 return ret;
632 }
633 }
634
635 switch (msg->flags) {
636 case SDW_MSG_FLAG_READ:
637 *cmd = CDNS_MCP_CMD_READ;
638 break;
639
640 case SDW_MSG_FLAG_WRITE:
641 *cmd = CDNS_MCP_CMD_WRITE;
642 break;
643
644 default:
645 dev_err(cdns->dev, "Invalid msg cmd: %d\n", msg->flags);
646 return -EINVAL;
647 }
648
649 return 0;
650}
651
Shreyas NCc91605f2018-04-26 18:38:43 +0530652enum sdw_command_response
Sanyog Kale956baa12017-12-14 11:19:42 +0530653cdns_xfer_msg(struct sdw_bus *bus, struct sdw_msg *msg)
654{
655 struct sdw_cdns *cdns = bus_to_cdns(bus);
656 int cmd = 0, ret, i;
657
658 ret = cdns_prep_msg(cdns, msg, &cmd);
659 if (ret)
660 return SDW_CMD_FAIL_OTHER;
661
662 for (i = 0; i < msg->len / CDNS_MCP_CMD_LEN; i++) {
663 ret = _cdns_xfer_msg(cdns, msg, cmd, i * CDNS_MCP_CMD_LEN,
Pierre-Louis Bossartbbb63812019-05-01 10:57:41 -0500664 CDNS_MCP_CMD_LEN, false);
Sanyog Kale956baa12017-12-14 11:19:42 +0530665 if (ret < 0)
666 goto exit;
667 }
668
669 if (!(msg->len % CDNS_MCP_CMD_LEN))
670 goto exit;
671
672 ret = _cdns_xfer_msg(cdns, msg, cmd, i * CDNS_MCP_CMD_LEN,
Pierre-Louis Bossartbbb63812019-05-01 10:57:41 -0500673 msg->len % CDNS_MCP_CMD_LEN, false);
Sanyog Kale956baa12017-12-14 11:19:42 +0530674
675exit:
676 return ret;
677}
Shreyas NCc91605f2018-04-26 18:38:43 +0530678EXPORT_SYMBOL(cdns_xfer_msg);
Sanyog Kale956baa12017-12-14 11:19:42 +0530679
Shreyas NCc91605f2018-04-26 18:38:43 +0530680enum sdw_command_response
Sanyog Kale956baa12017-12-14 11:19:42 +0530681cdns_xfer_msg_defer(struct sdw_bus *bus,
Pierre-Louis Bossartbbb63812019-05-01 10:57:41 -0500682 struct sdw_msg *msg, struct sdw_defer *defer)
Sanyog Kale956baa12017-12-14 11:19:42 +0530683{
684 struct sdw_cdns *cdns = bus_to_cdns(bus);
685 int cmd = 0, ret;
686
687 /* for defer only 1 message is supported */
688 if (msg->len > 1)
689 return -ENOTSUPP;
690
691 ret = cdns_prep_msg(cdns, msg, &cmd);
692 if (ret)
693 return SDW_CMD_FAIL_OTHER;
694
695 cdns->defer = defer;
696 cdns->defer->length = msg->len;
697
698 return _cdns_xfer_msg(cdns, msg, cmd, 0, msg->len, true);
699}
Shreyas NCc91605f2018-04-26 18:38:43 +0530700EXPORT_SYMBOL(cdns_xfer_msg_defer);
Sanyog Kale956baa12017-12-14 11:19:42 +0530701
Shreyas NCc91605f2018-04-26 18:38:43 +0530702enum sdw_command_response
Sanyog Kale956baa12017-12-14 11:19:42 +0530703cdns_reset_page_addr(struct sdw_bus *bus, unsigned int dev_num)
704{
705 struct sdw_cdns *cdns = bus_to_cdns(bus);
706 struct sdw_msg msg;
707
708 /* Create dummy message with valid device number */
709 memset(&msg, 0, sizeof(msg));
710 msg.dev_num = dev_num;
711
712 return cdns_program_scp_addr(cdns, &msg);
713}
Shreyas NCc91605f2018-04-26 18:38:43 +0530714EXPORT_SYMBOL(cdns_reset_page_addr);
Sanyog Kale956baa12017-12-14 11:19:42 +0530715
716/*
Vinod Koul2f52a512017-12-14 11:19:41 +0530717 * IRQ handling
718 */
719
Sanyog Kale956baa12017-12-14 11:19:42 +0530720static void cdns_read_response(struct sdw_cdns *cdns)
721{
722 u32 num_resp, cmd_base;
723 int i;
724
725 num_resp = cdns_readl(cdns, CDNS_MCP_FIFOSTAT);
726 num_resp &= CDNS_MCP_RX_FIFO_AVAIL;
727
728 cmd_base = CDNS_MCP_CMD_BASE;
729
730 for (i = 0; i < num_resp; i++) {
731 cdns->response_buf[i] = cdns_readl(cdns, cmd_base);
732 cmd_base += CDNS_MCP_CMD_WORD_LEN;
733 }
734}
735
Vinod Koul2f52a512017-12-14 11:19:41 +0530736static int cdns_update_slave_status(struct sdw_cdns *cdns,
Pierre-Louis Bossart6f206832021-01-15 13:37:35 +0800737 u64 slave_intstat)
Vinod Koul2f52a512017-12-14 11:19:41 +0530738{
739 enum sdw_slave_status status[SDW_MAX_DEVICES + 1];
740 bool is_slave = false;
Pierre-Louis Bossarta78b32d2019-05-22 14:47:27 -0500741 u32 mask;
Vinod Koul2f52a512017-12-14 11:19:41 +0530742 int i, set_status;
743
Vinod Koul2f52a512017-12-14 11:19:41 +0530744 memset(status, 0, sizeof(status));
745
746 for (i = 0; i <= SDW_MAX_DEVICES; i++) {
Pierre-Louis Bossart6f206832021-01-15 13:37:35 +0800747 mask = (slave_intstat >> (i * CDNS_MCP_SLAVE_STATUS_NUM)) &
748 CDNS_MCP_SLAVE_STATUS_BITS;
Vinod Koul2f52a512017-12-14 11:19:41 +0530749 if (!mask)
750 continue;
751
752 is_slave = true;
753 set_status = 0;
754
755 if (mask & CDNS_MCP_SLAVE_INTSTAT_RESERVED) {
756 status[i] = SDW_SLAVE_RESERVED;
757 set_status++;
758 }
759
760 if (mask & CDNS_MCP_SLAVE_INTSTAT_ATTACHED) {
761 status[i] = SDW_SLAVE_ATTACHED;
762 set_status++;
763 }
764
765 if (mask & CDNS_MCP_SLAVE_INTSTAT_ALERT) {
766 status[i] = SDW_SLAVE_ALERT;
767 set_status++;
768 }
769
770 if (mask & CDNS_MCP_SLAVE_INTSTAT_NPRESENT) {
771 status[i] = SDW_SLAVE_UNATTACHED;
772 set_status++;
773 }
774
775 /* first check if Slave reported multiple status */
776 if (set_status > 1) {
Pierre-Louis Bossart7181b1d2020-01-10 15:57:30 -0600777 u32 val;
778
Pierre-Louis Bossarteb7df4c2019-05-22 14:47:26 -0500779 dev_warn_ratelimited(cdns->dev,
Pierre-Louis Bossart7181b1d2020-01-10 15:57:30 -0600780 "Slave %d reported multiple Status: %d\n",
781 i, mask);
782
783 /* check latest status extracted from PING commands */
784 val = cdns_readl(cdns, CDNS_MCP_SLAVE_STAT);
785 val >>= (i * 2);
786
787 switch (val & 0x3) {
788 case 0:
789 status[i] = SDW_SLAVE_UNATTACHED;
790 break;
791 case 1:
792 status[i] = SDW_SLAVE_ATTACHED;
793 break;
794 case 2:
795 status[i] = SDW_SLAVE_ALERT;
796 break;
797 case 3:
798 default:
799 status[i] = SDW_SLAVE_RESERVED;
800 break;
801 }
802
803 dev_warn_ratelimited(cdns->dev,
804 "Slave %d status updated to %d\n",
805 i, status[i]);
806
Vinod Koul2f52a512017-12-14 11:19:41 +0530807 }
808 }
809
810 if (is_slave)
811 return sdw_handle_slave_status(&cdns->bus, status);
812
813 return 0;
814}
815
816/**
817 * sdw_cdns_irq() - Cadence interrupt handler
818 * @irq: irq number
819 * @dev_id: irq context
820 */
821irqreturn_t sdw_cdns_irq(int irq, void *dev_id)
822{
823 struct sdw_cdns *cdns = dev_id;
824 u32 int_status;
825 int ret = IRQ_HANDLED;
826
827 /* Check if the link is up */
828 if (!cdns->link_up)
829 return IRQ_NONE;
830
831 int_status = cdns_readl(cdns, CDNS_MCP_INTSTAT);
832
Pierre-Louis Bossarta2cff9e2020-01-10 15:57:26 -0600833 /* check for reserved values read as zero */
834 if (int_status & CDNS_MCP_INT_RESERVED)
835 return IRQ_NONE;
836
Vinod Koul2f52a512017-12-14 11:19:41 +0530837 if (!(int_status & CDNS_MCP_INT_IRQ))
838 return IRQ_NONE;
839
Sanyog Kale956baa12017-12-14 11:19:42 +0530840 if (int_status & CDNS_MCP_INT_RX_WL) {
841 cdns_read_response(cdns);
842
843 if (cdns->defer) {
844 cdns_fill_msg_resp(cdns, cdns->defer->msg,
Pierre-Louis Bossartbbb63812019-05-01 10:57:41 -0500845 cdns->defer->length, 0);
Sanyog Kale956baa12017-12-14 11:19:42 +0530846 complete(&cdns->defer->complete);
847 cdns->defer = NULL;
Pierre-Louis Bossartf6e20962019-05-01 10:57:42 -0500848 } else {
Sanyog Kale956baa12017-12-14 11:19:42 +0530849 complete(&cdns->tx_complete);
Pierre-Louis Bossartf6e20962019-05-01 10:57:42 -0500850 }
Sanyog Kale956baa12017-12-14 11:19:42 +0530851 }
852
Pierre-Louis Bossart9b5884a2019-08-05 19:55:08 -0500853 if (int_status & CDNS_MCP_INT_PARITY) {
854 /* Parity error detected by Master */
855 dev_err_ratelimited(cdns->dev, "Parity error\n");
856 }
857
Vinod Koul2f52a512017-12-14 11:19:41 +0530858 if (int_status & CDNS_MCP_INT_CTRL_CLASH) {
Vinod Koul2f52a512017-12-14 11:19:41 +0530859 /* Slave is driving bit slot during control word */
860 dev_err_ratelimited(cdns->dev, "Bus clash for control word\n");
Vinod Koul2f52a512017-12-14 11:19:41 +0530861 }
862
863 if (int_status & CDNS_MCP_INT_DATA_CLASH) {
864 /*
865 * Multiple slaves trying to drive bit slot, or issue with
866 * ownership of data bits or Slave gone bonkers
867 */
868 dev_err_ratelimited(cdns->dev, "Bus clash for data word\n");
Vinod Koul2f52a512017-12-14 11:19:41 +0530869 }
870
Pierre-Louis Bossart9e4e6012020-09-21 03:32:07 +0800871 if (cdns->bus.params.m_data_mode != SDW_PORT_DATA_MODE_NORMAL &&
872 int_status & CDNS_MCP_INT_DPINT) {
873 u32 port_intstat;
874
875 /* just log which ports report an error */
876 port_intstat = cdns_readl(cdns, CDNS_MCP_PORT_INTSTAT);
877 dev_err_ratelimited(cdns->dev, "DP interrupt: PortIntStat %8x\n",
878 port_intstat);
879
880 /* clear status w/ write1 */
881 cdns_writel(cdns, CDNS_MCP_PORT_INTSTAT, port_intstat);
882 }
883
Vinod Koul2f52a512017-12-14 11:19:41 +0530884 if (int_status & CDNS_MCP_INT_SLAVE_MASK) {
885 /* Mask the Slave interrupt and wake thread */
886 cdns_updatel(cdns, CDNS_MCP_INTMASK,
Pierre-Louis Bossartbbb63812019-05-01 10:57:41 -0500887 CDNS_MCP_INT_SLAVE_MASK, 0);
Vinod Koul2f52a512017-12-14 11:19:41 +0530888
889 int_status &= ~CDNS_MCP_INT_SLAVE_MASK;
Pierre-Louis Bossartd2068da2020-08-18 06:23:40 +0800890
891 /*
892 * Deal with possible race condition between interrupt
893 * handling and disabling interrupts on suspend.
894 *
895 * If the master is in the process of disabling
896 * interrupts, don't schedule a workqueue
897 */
898 if (cdns->interrupt_enabled)
899 schedule_work(&cdns->work);
Vinod Koul2f52a512017-12-14 11:19:41 +0530900 }
901
902 cdns_writel(cdns, CDNS_MCP_INTSTAT, int_status);
903 return ret;
904}
905EXPORT_SYMBOL(sdw_cdns_irq);
906
907/**
Pierre-Louis Bossartb76f3fb2021-03-01 11:47:14 -0600908 * cdns_update_slave_status_work - update slave status in a work since we will need to handle
Bard Liao4a98a6b2020-07-16 23:09:45 +0800909 * other interrupts eg. CDNS_MCP_INT_RX_WL during the update slave
910 * process.
911 * @work: cdns worker thread
Vinod Koul2f52a512017-12-14 11:19:41 +0530912 */
Bard Liao4a98a6b2020-07-16 23:09:45 +0800913static void cdns_update_slave_status_work(struct work_struct *work)
Vinod Koul2f52a512017-12-14 11:19:41 +0530914{
Bard Liao4a98a6b2020-07-16 23:09:45 +0800915 struct sdw_cdns *cdns =
916 container_of(work, struct sdw_cdns, work);
Vinod Koul2f52a512017-12-14 11:19:41 +0530917 u32 slave0, slave1;
Pierre-Louis Bossart6f206832021-01-15 13:37:35 +0800918 u64 slave_intstat;
Vinod Koul2f52a512017-12-14 11:19:41 +0530919
920 slave0 = cdns_readl(cdns, CDNS_MCP_SLAVE_INTSTAT0);
921 slave1 = cdns_readl(cdns, CDNS_MCP_SLAVE_INTSTAT1);
922
Pierre-Louis Bossart6f206832021-01-15 13:37:35 +0800923 /* combine the two status */
924 slave_intstat = ((u64)slave1 << 32) | slave0;
925
926 dev_dbg_ratelimited(cdns->dev, "Slave status change: 0x%llx\n", slave_intstat);
927
928 cdns_update_slave_status(cdns, slave_intstat);
Vinod Koul2f52a512017-12-14 11:19:41 +0530929 cdns_writel(cdns, CDNS_MCP_SLAVE_INTSTAT0, slave0);
930 cdns_writel(cdns, CDNS_MCP_SLAVE_INTSTAT1, slave1);
931
932 /* clear and unmask Slave interrupt now */
933 cdns_writel(cdns, CDNS_MCP_INTSTAT, CDNS_MCP_INT_SLAVE_MASK);
934 cdns_updatel(cdns, CDNS_MCP_INTMASK,
Pierre-Louis Bossartbbb63812019-05-01 10:57:41 -0500935 CDNS_MCP_INT_SLAVE_MASK, CDNS_MCP_INT_SLAVE_MASK);
Vinod Koul2f52a512017-12-14 11:19:41 +0530936
Vinod Koul2f52a512017-12-14 11:19:41 +0530937}
Vinod Koul2f52a512017-12-14 11:19:41 +0530938
939/*
940 * init routines
941 */
Pierre-Louis Bossart49ea07d2019-10-22 18:54:44 -0500942
943/**
944 * sdw_cdns_exit_reset() - Program reset parameters and start bus operations
945 * @cdns: Cadence instance
946 */
947int sdw_cdns_exit_reset(struct sdw_cdns *cdns)
948{
949 /* program maximum length reset to be safe */
950 cdns_updatel(cdns, CDNS_MCP_CONTROL,
951 CDNS_MCP_CONTROL_RST_DELAY,
952 CDNS_MCP_CONTROL_RST_DELAY);
953
954 /* use hardware generated reset */
955 cdns_updatel(cdns, CDNS_MCP_CONTROL,
956 CDNS_MCP_CONTROL_HW_RST,
957 CDNS_MCP_CONTROL_HW_RST);
958
Pierre-Louis Bossart49ea07d2019-10-22 18:54:44 -0500959 /* commit changes */
Pierre-Louis Bossart2c800e32020-03-17 11:33:28 -0500960 cdns_updatel(cdns, CDNS_MCP_CONFIG_UPDATE,
961 CDNS_MCP_CONFIG_UPDATE_BIT,
962 CDNS_MCP_CONFIG_UPDATE_BIT);
963
964 /* don't wait here */
965 return 0;
966
Pierre-Louis Bossart49ea07d2019-10-22 18:54:44 -0500967}
968EXPORT_SYMBOL(sdw_cdns_exit_reset);
969
970/**
Pierre-Louis Bossartb76f3fb2021-03-01 11:47:14 -0600971 * cdns_enable_slave_interrupts() - Enable SDW slave interrupts
Pierre-Louis Bossartaf4cc912020-03-17 11:33:19 -0500972 * @cdns: Cadence instance
973 * @state: boolean for true/false
974 */
975static void cdns_enable_slave_interrupts(struct sdw_cdns *cdns, bool state)
976{
977 u32 mask;
978
979 mask = cdns_readl(cdns, CDNS_MCP_INTMASK);
980 if (state)
981 mask |= CDNS_MCP_INT_SLAVE_MASK;
982 else
983 mask &= ~CDNS_MCP_INT_SLAVE_MASK;
984
985 cdns_writel(cdns, CDNS_MCP_INTMASK, mask);
986}
987
988/**
Rander Wangae478d62020-01-10 15:57:29 -0600989 * sdw_cdns_enable_interrupt() - Enable SDW interrupts
Pierre-Louis Bossart49ea07d2019-10-22 18:54:44 -0500990 * @cdns: Cadence instance
Pierre-Louis Bossart550f9052020-01-14 17:31:24 -0600991 * @state: True if we are trying to enable interrupt.
Pierre-Louis Bossart49ea07d2019-10-22 18:54:44 -0500992 */
Pierre-Louis Bossart9e3d47f2019-10-22 18:54:47 -0500993int sdw_cdns_enable_interrupt(struct sdw_cdns *cdns, bool state)
Sanyog Kale956baa12017-12-14 11:19:42 +0530994{
Pierre-Louis Bossart9e3d47f2019-10-22 18:54:47 -0500995 u32 slave_intmask0 = 0;
996 u32 slave_intmask1 = 0;
997 u32 mask = 0;
Sanyog Kale956baa12017-12-14 11:19:42 +0530998
Pierre-Louis Bossart9e3d47f2019-10-22 18:54:47 -0500999 if (!state)
1000 goto update_masks;
1001
1002 slave_intmask0 = CDNS_MCP_SLAVE_INTMASK0_MASK;
1003 slave_intmask1 = CDNS_MCP_SLAVE_INTMASK1_MASK;
Sanyog Kale956baa12017-12-14 11:19:42 +05301004
Pierre-Louis Bossart9b5884a2019-08-05 19:55:08 -05001005 /* enable detection of all slave state changes */
1006 mask = CDNS_MCP_INT_SLAVE_MASK;
1007
1008 /* enable detection of bus issues */
1009 mask |= CDNS_MCP_INT_CTRL_CLASH | CDNS_MCP_INT_DATA_CLASH |
1010 CDNS_MCP_INT_PARITY;
1011
Pierre-Louis Bossart9e4e6012020-09-21 03:32:07 +08001012 /* port interrupt limited to test modes for now */
1013 if (cdns->bus.params.m_data_mode != SDW_PORT_DATA_MODE_NORMAL)
1014 mask |= CDNS_MCP_INT_DPINT;
Pierre-Louis Bossart9b5884a2019-08-05 19:55:08 -05001015
1016 /* enable detection of RX fifo level */
1017 mask |= CDNS_MCP_INT_RX_WL;
1018
1019 /*
1020 * CDNS_MCP_INT_IRQ needs to be set otherwise all previous
1021 * settings are irrelevant
1022 */
1023 mask |= CDNS_MCP_INT_IRQ;
Sanyog Kale956baa12017-12-14 11:19:42 +05301024
Pierre-Louis Bossart04592dc2019-08-05 19:55:21 -05001025 if (interrupt_mask) /* parameter override */
1026 mask = interrupt_mask;
Sanyog Kale956baa12017-12-14 11:19:42 +05301027
Pierre-Louis Bossart9e3d47f2019-10-22 18:54:47 -05001028update_masks:
Rander Wang5ebb0942020-01-10 15:57:27 -06001029 /* clear slave interrupt status before enabling interrupt */
1030 if (state) {
1031 u32 slave_state;
1032
1033 slave_state = cdns_readl(cdns, CDNS_MCP_SLAVE_INTSTAT0);
1034 cdns_writel(cdns, CDNS_MCP_SLAVE_INTSTAT0, slave_state);
1035 slave_state = cdns_readl(cdns, CDNS_MCP_SLAVE_INTSTAT1);
1036 cdns_writel(cdns, CDNS_MCP_SLAVE_INTSTAT1, slave_state);
1037 }
Pierre-Louis Bossartd2068da2020-08-18 06:23:40 +08001038 cdns->interrupt_enabled = state;
1039
1040 /*
1041 * Complete any on-going status updates before updating masks,
1042 * and cancel queued status updates.
1043 *
1044 * There could be a race with a new interrupt thrown before
1045 * the 3 mask updates below are complete, so in the interrupt
1046 * we use the 'interrupt_enabled' status to prevent new work
1047 * from being queued.
1048 */
1049 if (!state)
1050 cancel_work_sync(&cdns->work);
Rander Wang5ebb0942020-01-10 15:57:27 -06001051
Pierre-Louis Bossart9e3d47f2019-10-22 18:54:47 -05001052 cdns_writel(cdns, CDNS_MCP_SLAVE_INTMASK0, slave_intmask0);
1053 cdns_writel(cdns, CDNS_MCP_SLAVE_INTMASK1, slave_intmask1);
Sanyog Kale956baa12017-12-14 11:19:42 +05301054 cdns_writel(cdns, CDNS_MCP_INTMASK, mask);
1055
Rander Wangae478d62020-01-10 15:57:29 -06001056 return 0;
Sanyog Kale956baa12017-12-14 11:19:42 +05301057}
1058EXPORT_SYMBOL(sdw_cdns_enable_interrupt);
Vinod Koul2f52a512017-12-14 11:19:41 +05301059
Vinod Koul07abeff2018-04-26 18:38:48 +05301060static int cdns_allocate_pdi(struct sdw_cdns *cdns,
Pierre-Louis Bossartbbb63812019-05-01 10:57:41 -05001061 struct sdw_cdns_pdi **stream,
1062 u32 num, u32 pdi_offset)
Vinod Koul07abeff2018-04-26 18:38:48 +05301063{
1064 struct sdw_cdns_pdi *pdi;
1065 int i;
1066
1067 if (!num)
1068 return 0;
1069
1070 pdi = devm_kcalloc(cdns->dev, num, sizeof(*pdi), GFP_KERNEL);
1071 if (!pdi)
1072 return -ENOMEM;
1073
1074 for (i = 0; i < num; i++) {
1075 pdi[i].num = i + pdi_offset;
Vinod Koul07abeff2018-04-26 18:38:48 +05301076 }
1077
1078 *stream = pdi;
1079 return 0;
1080}
1081
1082/**
1083 * sdw_cdns_pdi_init() - PDI initialization routine
1084 *
1085 * @cdns: Cadence instance
1086 * @config: Stream configurations
1087 */
1088int sdw_cdns_pdi_init(struct sdw_cdns *cdns,
Pierre-Louis Bossartbbb63812019-05-01 10:57:41 -05001089 struct sdw_cdns_stream_config config)
Vinod Koul07abeff2018-04-26 18:38:48 +05301090{
1091 struct sdw_cdns_streams *stream;
Pierre-Louis Bossart57a34792019-09-16 14:23:46 -05001092 int offset;
1093 int ret;
Vinod Koul07abeff2018-04-26 18:38:48 +05301094
1095 cdns->pcm.num_bd = config.pcm_bd;
1096 cdns->pcm.num_in = config.pcm_in;
1097 cdns->pcm.num_out = config.pcm_out;
1098 cdns->pdm.num_bd = config.pdm_bd;
1099 cdns->pdm.num_in = config.pdm_in;
1100 cdns->pdm.num_out = config.pdm_out;
1101
1102 /* Allocate PDIs for PCMs */
1103 stream = &cdns->pcm;
1104
Pierre-Louis Bossart807c15b2019-09-16 14:23:47 -05001105 /* we allocate PDI0 and PDI1 which are used for Bulk */
1106 offset = 0;
Vinod Koul07abeff2018-04-26 18:38:48 +05301107
1108 ret = cdns_allocate_pdi(cdns, &stream->bd,
1109 stream->num_bd, offset);
1110 if (ret)
1111 return ret;
1112
1113 offset += stream->num_bd;
1114
1115 ret = cdns_allocate_pdi(cdns, &stream->in,
1116 stream->num_in, offset);
1117 if (ret)
1118 return ret;
1119
1120 offset += stream->num_in;
1121
1122 ret = cdns_allocate_pdi(cdns, &stream->out,
1123 stream->num_out, offset);
1124 if (ret)
1125 return ret;
1126
1127 /* Update total number of PCM PDIs */
1128 stream->num_pdi = stream->num_bd + stream->num_in + stream->num_out;
1129 cdns->num_ports = stream->num_pdi;
1130
1131 /* Allocate PDIs for PDMs */
1132 stream = &cdns->pdm;
Vinod Koul07abeff2018-04-26 18:38:48 +05301133 ret = cdns_allocate_pdi(cdns, &stream->bd,
1134 stream->num_bd, offset);
1135 if (ret)
1136 return ret;
1137
1138 offset += stream->num_bd;
1139
1140 ret = cdns_allocate_pdi(cdns, &stream->in,
1141 stream->num_in, offset);
1142 if (ret)
1143 return ret;
1144
1145 offset += stream->num_in;
1146
1147 ret = cdns_allocate_pdi(cdns, &stream->out,
1148 stream->num_out, offset);
Pierre-Louis Bossart807c15b2019-09-16 14:23:47 -05001149
Vinod Koul07abeff2018-04-26 18:38:48 +05301150 if (ret)
1151 return ret;
1152
1153 /* Update total number of PDM PDIs */
1154 stream->num_pdi = stream->num_bd + stream->num_in + stream->num_out;
1155 cdns->num_ports += stream->num_pdi;
1156
Vinod Koul07abeff2018-04-26 18:38:48 +05301157 return 0;
1158}
1159EXPORT_SYMBOL(sdw_cdns_pdi_init);
1160
Pierre-Louis Bossart05be59a2019-08-05 19:55:11 -05001161static u32 cdns_set_initial_frame_shape(int n_rows, int n_cols)
1162{
1163 u32 val;
1164 int c;
1165 int r;
1166
1167 r = sdw_find_row_index(n_rows);
Vinod Koul3cf25d62020-09-03 17:15:01 +05301168 c = sdw_find_col_index(n_cols);
Pierre-Louis Bossart05be59a2019-08-05 19:55:11 -05001169
Vinod Koul3cf25d62020-09-03 17:15:01 +05301170 val = FIELD_PREP(CDNS_MCP_FRAME_SHAPE_ROW_MASK, r);
1171 val |= FIELD_PREP(CDNS_MCP_FRAME_SHAPE_COL_MASK, c);
Pierre-Louis Bossart05be59a2019-08-05 19:55:11 -05001172
1173 return val;
1174}
1175
Pierre-Louis Bossart0cdcded2020-03-17 11:33:21 -05001176static void cdns_init_clock_ctrl(struct sdw_cdns *cdns)
Vinod Koul2f52a512017-12-14 11:19:41 +05301177{
Pierre-Louis Bossart38598722019-08-05 19:55:17 -05001178 struct sdw_bus *bus = &cdns->bus;
1179 struct sdw_master_prop *prop = &bus->prop;
Vinod Koul2f52a512017-12-14 11:19:41 +05301180 u32 val;
Pierre-Louis Bossart1dd6a172020-03-17 11:33:22 -05001181 u32 ssp_interval;
Pierre-Louis Bossart38598722019-08-05 19:55:17 -05001182 int divider;
Vinod Koul2f52a512017-12-14 11:19:41 +05301183
1184 /* Set clock divider */
Pierre-Louis Bossart38598722019-08-05 19:55:17 -05001185 divider = (prop->mclk_freq / prop->max_clk_freq) - 1;
Vinod Koul2f52a512017-12-14 11:19:41 +05301186
Rander Wanga50954e2019-08-05 19:55:18 -05001187 cdns_updatel(cdns, CDNS_MCP_CLK_CTRL0,
1188 CDNS_MCP_CLK_MCLKD_MASK, divider);
1189 cdns_updatel(cdns, CDNS_MCP_CLK_CTRL1,
1190 CDNS_MCP_CLK_MCLKD_MASK, divider);
Vinod Koul2f52a512017-12-14 11:19:41 +05301191
Pierre-Louis Bossart05be59a2019-08-05 19:55:11 -05001192 /*
1193 * Frame shape changes after initialization have to be done
1194 * with the bank switch mechanism
1195 */
1196 val = cdns_set_initial_frame_shape(prop->default_row,
1197 prop->default_col);
1198 cdns_writel(cdns, CDNS_MCP_FRAME_SHAPE_INIT, val);
Vinod Koul2f52a512017-12-14 11:19:41 +05301199
1200 /* Set SSP interval to default value */
Pierre-Louis Bossart1dd6a172020-03-17 11:33:22 -05001201 ssp_interval = prop->default_frame_rate / SDW_CADENCE_GSYNC_HZ;
1202 cdns_writel(cdns, CDNS_MCP_SSP_CTRL0, ssp_interval);
1203 cdns_writel(cdns, CDNS_MCP_SSP_CTRL1, ssp_interval);
Pierre-Louis Bossart0cdcded2020-03-17 11:33:21 -05001204}
1205
1206/**
1207 * sdw_cdns_init() - Cadence initialization
1208 * @cdns: Cadence instance
1209 */
1210int sdw_cdns_init(struct sdw_cdns *cdns)
1211{
1212 u32 val;
Pierre-Louis Bossart0cdcded2020-03-17 11:33:21 -05001213
1214 cdns_init_clock_ctrl(cdns);
Vinod Koul2f52a512017-12-14 11:19:41 +05301215
Rander Wang0d667d02020-03-17 11:33:18 -05001216 /* reset msg_count to default value of FIFOLEVEL */
1217 cdns->msg_count = cdns_readl(cdns, CDNS_MCP_FIFOLEVEL);
1218
Pierre-Louis Bossart49ea07d2019-10-22 18:54:44 -05001219 /* flush command FIFOs */
1220 cdns_updatel(cdns, CDNS_MCP_CONTROL, CDNS_MCP_CONTROL_CMD_RST,
1221 CDNS_MCP_CONTROL_CMD_RST);
1222
Vinod Koul2f52a512017-12-14 11:19:41 +05301223 /* Set cmd accept mode */
1224 cdns_updatel(cdns, CDNS_MCP_CONTROL, CDNS_MCP_CONTROL_CMD_ACCEPT,
Pierre-Louis Bossartbbb63812019-05-01 10:57:41 -05001225 CDNS_MCP_CONTROL_CMD_ACCEPT);
Vinod Koul2f52a512017-12-14 11:19:41 +05301226
1227 /* Configure mcp config */
1228 val = cdns_readl(cdns, CDNS_MCP_CONFIG);
1229
Pierre-Louis Bossart5c8f0f682020-03-17 11:33:24 -05001230 /* enable bus operations with clock and data */
1231 val &= ~CDNS_MCP_CONFIG_OP;
1232 val |= CDNS_MCP_CONFIG_OP_NORMAL;
1233
Pierre-Louis Bossartb62e76c2020-03-17 11:33:23 -05001234 /* Set cmd mode for Tx and Rx cmds */
1235 val &= ~CDNS_MCP_CONFIG_CMD;
Vinod Koul2f52a512017-12-14 11:19:41 +05301236
1237 /* Disable sniffer mode */
1238 val &= ~CDNS_MCP_CONFIG_SNIFFER;
1239
Pierre-Louis Bossartb62e76c2020-03-17 11:33:23 -05001240 /* Disable auto bus release */
1241 val &= ~CDNS_MCP_CONFIG_BUS_REL;
1242
Pierre-Louis Bossart2c800e32020-03-17 11:33:28 -05001243 if (cdns->bus.multi_link)
1244 /* Set Multi-master mode to take gsync into account */
1245 val |= CDNS_MCP_CONFIG_MMASTER;
Pierre-Louis Bossartb62e76c2020-03-17 11:33:23 -05001246
Pierre-Louis Bossart91080112020-03-17 11:33:25 -05001247 /* leave frame delay to hardware default of 0x1F */
Pierre-Louis Bossartb62e76c2020-03-17 11:33:23 -05001248
Pierre-Louis Bossartad473db2020-03-17 11:33:26 -05001249 /* leave command retry to hardware default of 0 */
Vinod Koul2f52a512017-12-14 11:19:41 +05301250
Vinod Koul2f52a512017-12-14 11:19:41 +05301251 cdns_writel(cdns, CDNS_MCP_CONFIG, val);
1252
Pierre-Louis Bossartb17350e2020-03-17 11:33:27 -05001253 /* changes will be committed later */
1254 return 0;
Vinod Koul2f52a512017-12-14 11:19:41 +05301255}
1256EXPORT_SYMBOL(sdw_cdns_init);
1257
Vinod Koul07abeff2018-04-26 18:38:48 +05301258int cdns_bus_conf(struct sdw_bus *bus, struct sdw_bus_params *params)
1259{
Pierre-Louis Bossart38598722019-08-05 19:55:17 -05001260 struct sdw_master_prop *prop = &bus->prop;
Vinod Koul07abeff2018-04-26 18:38:48 +05301261 struct sdw_cdns *cdns = bus_to_cdns(bus);
Rander Wanga50954e2019-08-05 19:55:18 -05001262 int mcp_clkctrl_off;
Vinod Koul07abeff2018-04-26 18:38:48 +05301263 int divider;
1264
1265 if (!params->curr_dr_freq) {
Pierre-Louis Bossart17ed5be2019-05-01 10:57:45 -05001266 dev_err(cdns->dev, "NULL curr_dr_freq\n");
Vinod Koul07abeff2018-04-26 18:38:48 +05301267 return -EINVAL;
1268 }
1269
Pierre-Louis Bossart38598722019-08-05 19:55:17 -05001270 divider = prop->mclk_freq * SDW_DOUBLE_RATE_FACTOR /
1271 params->curr_dr_freq;
1272 divider--; /* divider is 1/(N+1) */
Vinod Koul07abeff2018-04-26 18:38:48 +05301273
1274 if (params->next_bank)
1275 mcp_clkctrl_off = CDNS_MCP_CLK_CTRL1;
1276 else
1277 mcp_clkctrl_off = CDNS_MCP_CLK_CTRL0;
1278
Rander Wanga50954e2019-08-05 19:55:18 -05001279 cdns_updatel(cdns, mcp_clkctrl_off, CDNS_MCP_CLK_MCLKD_MASK, divider);
Vinod Koul07abeff2018-04-26 18:38:48 +05301280
1281 return 0;
1282}
1283EXPORT_SYMBOL(cdns_bus_conf);
1284
1285static int cdns_port_params(struct sdw_bus *bus,
Pierre-Louis Bossartbbb63812019-05-01 10:57:41 -05001286 struct sdw_port_params *p_params, unsigned int bank)
Vinod Koul07abeff2018-04-26 18:38:48 +05301287{
1288 struct sdw_cdns *cdns = bus_to_cdns(bus);
1289 int dpn_config = 0, dpn_config_off;
1290
1291 if (bank)
1292 dpn_config_off = CDNS_DPN_B1_CONFIG(p_params->num);
1293 else
1294 dpn_config_off = CDNS_DPN_B0_CONFIG(p_params->num);
1295
1296 dpn_config = cdns_readl(cdns, dpn_config_off);
1297
Vinod Koul714db042020-09-17 17:31:45 +05301298 u32p_replace_bits(&dpn_config, (p_params->bps - 1), CDNS_DPN_CONFIG_WL);
1299 u32p_replace_bits(&dpn_config, p_params->flow_mode, CDNS_DPN_CONFIG_PORT_FLOW);
1300 u32p_replace_bits(&dpn_config, p_params->data_mode, CDNS_DPN_CONFIG_PORT_DAT);
Vinod Koul07abeff2018-04-26 18:38:48 +05301301
1302 cdns_writel(cdns, dpn_config_off, dpn_config);
1303
1304 return 0;
1305}
1306
1307static int cdns_transport_params(struct sdw_bus *bus,
Pierre-Louis Bossartbbb63812019-05-01 10:57:41 -05001308 struct sdw_transport_params *t_params,
1309 enum sdw_reg_bank bank)
Vinod Koul07abeff2018-04-26 18:38:48 +05301310{
1311 struct sdw_cdns *cdns = bus_to_cdns(bus);
1312 int dpn_offsetctrl = 0, dpn_offsetctrl_off;
1313 int dpn_config = 0, dpn_config_off;
1314 int dpn_hctrl = 0, dpn_hctrl_off;
1315 int num = t_params->port_num;
1316 int dpn_samplectrl_off;
1317
1318 /*
1319 * Note: Only full data port is supported on the Master side for
1320 * both PCM and PDM ports.
1321 */
1322
1323 if (bank) {
1324 dpn_config_off = CDNS_DPN_B1_CONFIG(num);
1325 dpn_samplectrl_off = CDNS_DPN_B1_SAMPLE_CTRL(num);
1326 dpn_hctrl_off = CDNS_DPN_B1_HCTRL(num);
1327 dpn_offsetctrl_off = CDNS_DPN_B1_OFFSET_CTRL(num);
1328 } else {
1329 dpn_config_off = CDNS_DPN_B0_CONFIG(num);
1330 dpn_samplectrl_off = CDNS_DPN_B0_SAMPLE_CTRL(num);
1331 dpn_hctrl_off = CDNS_DPN_B0_HCTRL(num);
1332 dpn_offsetctrl_off = CDNS_DPN_B0_OFFSET_CTRL(num);
1333 }
1334
1335 dpn_config = cdns_readl(cdns, dpn_config_off);
Vinod Koul714db042020-09-17 17:31:45 +05301336 u32p_replace_bits(&dpn_config, t_params->blk_grp_ctrl, CDNS_DPN_CONFIG_BGC);
1337 u32p_replace_bits(&dpn_config, t_params->blk_pkg_mode, CDNS_DPN_CONFIG_BPM);
Vinod Koul07abeff2018-04-26 18:38:48 +05301338 cdns_writel(cdns, dpn_config_off, dpn_config);
1339
Vinod Koul714db042020-09-17 17:31:45 +05301340 u32p_replace_bits(&dpn_offsetctrl, t_params->offset1, CDNS_DPN_OFFSET_CTRL_1);
1341 u32p_replace_bits(&dpn_offsetctrl, t_params->offset2, CDNS_DPN_OFFSET_CTRL_2);
Vinod Koul07abeff2018-04-26 18:38:48 +05301342 cdns_writel(cdns, dpn_offsetctrl_off, dpn_offsetctrl);
1343
Vinod Koul714db042020-09-17 17:31:45 +05301344 u32p_replace_bits(&dpn_hctrl, t_params->hstart, CDNS_DPN_HCTRL_HSTART);
1345 u32p_replace_bits(&dpn_hctrl, t_params->hstop, CDNS_DPN_HCTRL_HSTOP);
1346 u32p_replace_bits(&dpn_hctrl, t_params->lane_ctrl, CDNS_DPN_HCTRL_LCTRL);
Vinod Koul07abeff2018-04-26 18:38:48 +05301347
1348 cdns_writel(cdns, dpn_hctrl_off, dpn_hctrl);
1349 cdns_writel(cdns, dpn_samplectrl_off, (t_params->sample_interval - 1));
1350
1351 return 0;
1352}
1353
1354static int cdns_port_enable(struct sdw_bus *bus,
Pierre-Louis Bossartbbb63812019-05-01 10:57:41 -05001355 struct sdw_enable_ch *enable_ch, unsigned int bank)
Vinod Koul07abeff2018-04-26 18:38:48 +05301356{
1357 struct sdw_cdns *cdns = bus_to_cdns(bus);
1358 int dpn_chnen_off, ch_mask;
1359
1360 if (bank)
1361 dpn_chnen_off = CDNS_DPN_B1_CH_EN(enable_ch->port_num);
1362 else
1363 dpn_chnen_off = CDNS_DPN_B0_CH_EN(enable_ch->port_num);
1364
1365 ch_mask = enable_ch->ch_mask * enable_ch->enable;
1366 cdns_writel(cdns, dpn_chnen_off, ch_mask);
1367
1368 return 0;
1369}
1370
1371static const struct sdw_master_port_ops cdns_port_ops = {
1372 .dpn_set_port_params = cdns_port_params,
1373 .dpn_set_port_transport_params = cdns_transport_params,
1374 .dpn_port_enable_ch = cdns_port_enable,
1375};
1376
Sanyog Kale956baa12017-12-14 11:19:42 +05301377/**
Rander Wang5a885c52020-03-17 11:33:15 -05001378 * sdw_cdns_is_clock_stop: Check clock status
1379 *
1380 * @cdns: Cadence instance
1381 */
1382bool sdw_cdns_is_clock_stop(struct sdw_cdns *cdns)
1383{
1384 return !!(cdns_readl(cdns, CDNS_MCP_STAT) & CDNS_MCP_STAT_CLK_STOP);
1385}
1386EXPORT_SYMBOL(sdw_cdns_is_clock_stop);
1387
1388/**
Rander Wang10325042020-03-17 11:33:17 -05001389 * sdw_cdns_clock_stop: Cadence clock stop configuration routine
1390 *
1391 * @cdns: Cadence instance
1392 * @block_wake: prevent wakes if required by the platform
1393 */
1394int sdw_cdns_clock_stop(struct sdw_cdns *cdns, bool block_wake)
1395{
1396 bool slave_present = false;
1397 struct sdw_slave *slave;
1398 int ret;
1399
1400 /* Check suspend status */
1401 if (sdw_cdns_is_clock_stop(cdns)) {
1402 dev_dbg(cdns->dev, "Clock is already stopped\n");
1403 return 0;
1404 }
1405
1406 /*
Pierre-Louis Bossartaf4cc912020-03-17 11:33:19 -05001407 * Before entering clock stop we mask the Slave
1408 * interrupts. This helps avoid having to deal with e.g. a
1409 * Slave becoming UNATTACHED while the clock is being stopped
1410 */
1411 cdns_enable_slave_interrupts(cdns, false);
1412
1413 /*
Rander Wang10325042020-03-17 11:33:17 -05001414 * For specific platforms, it is required to be able to put
1415 * master into a state in which it ignores wake-up trials
1416 * in clock stop state
1417 */
1418 if (block_wake)
1419 cdns_updatel(cdns, CDNS_MCP_CONTROL,
1420 CDNS_MCP_CONTROL_BLOCK_WAKEUP,
1421 CDNS_MCP_CONTROL_BLOCK_WAKEUP);
1422
1423 list_for_each_entry(slave, &cdns->bus.slaves, node) {
1424 if (slave->status == SDW_SLAVE_ATTACHED ||
1425 slave->status == SDW_SLAVE_ALERT) {
1426 slave_present = true;
1427 break;
1428 }
1429 }
1430
1431 /*
1432 * This CMD_ACCEPT should be used when there are no devices
1433 * attached on the link when entering clock stop mode. If this is
1434 * not set and there is a broadcast write then the command ignored
1435 * will be treated as a failure
1436 */
1437 if (!slave_present)
1438 cdns_updatel(cdns, CDNS_MCP_CONTROL,
1439 CDNS_MCP_CONTROL_CMD_ACCEPT,
1440 CDNS_MCP_CONTROL_CMD_ACCEPT);
1441 else
1442 cdns_updatel(cdns, CDNS_MCP_CONTROL,
1443 CDNS_MCP_CONTROL_CMD_ACCEPT, 0);
1444
1445 /* commit changes */
1446 ret = cdns_config_update(cdns);
1447 if (ret < 0) {
1448 dev_err(cdns->dev, "%s: config_update failed\n", __func__);
1449 return ret;
1450 }
1451
1452 /* Prepare slaves for clock stop */
Pierre-Louis Bossart58ef9352021-03-23 09:37:07 +08001453 if (slave_present) {
1454 ret = sdw_bus_prep_clk_stop(&cdns->bus);
1455 if (ret < 0 && ret != -ENODATA) {
1456 dev_err(cdns->dev, "prepare clock stop failed %d\n", ret);
1457 return ret;
1458 }
Rander Wang10325042020-03-17 11:33:17 -05001459 }
1460
1461 /*
1462 * Enter clock stop mode and only report errors if there are
1463 * Slave devices present (ALERT or ATTACHED)
1464 */
1465 ret = sdw_bus_clk_stop(&cdns->bus);
1466 if (ret < 0 && slave_present && ret != -ENODATA) {
Pierre-Louis Bossart7dbdcd62021-03-23 08:58:53 +08001467 dev_err(cdns->dev, "bus clock stop failed %d\n", ret);
Rander Wang10325042020-03-17 11:33:17 -05001468 return ret;
1469 }
1470
1471 ret = cdns_set_wait(cdns, CDNS_MCP_STAT,
1472 CDNS_MCP_STAT_CLK_STOP,
1473 CDNS_MCP_STAT_CLK_STOP);
1474 if (ret < 0)
1475 dev_err(cdns->dev, "Clock stop failed %d\n", ret);
1476
1477 return ret;
1478}
1479EXPORT_SYMBOL(sdw_cdns_clock_stop);
1480
1481/**
1482 * sdw_cdns_clock_restart: Cadence PM clock restart configuration routine
1483 *
1484 * @cdns: Cadence instance
1485 * @bus_reset: context may be lost while in low power modes and the bus
1486 * may require a Severe Reset and re-enumeration after a wake.
1487 */
1488int sdw_cdns_clock_restart(struct sdw_cdns *cdns, bool bus_reset)
1489{
1490 int ret;
1491
Pierre-Louis Bossartaf4cc912020-03-17 11:33:19 -05001492 /* unmask Slave interrupts that were masked when stopping the clock */
1493 cdns_enable_slave_interrupts(cdns, true);
1494
Rander Wang10325042020-03-17 11:33:17 -05001495 ret = cdns_clear_bit(cdns, CDNS_MCP_CONTROL,
1496 CDNS_MCP_CONTROL_CLK_STOP_CLR);
1497 if (ret < 0) {
1498 dev_err(cdns->dev, "Couldn't exit from clock stop\n");
1499 return ret;
1500 }
1501
1502 ret = cdns_set_wait(cdns, CDNS_MCP_STAT, CDNS_MCP_STAT_CLK_STOP, 0);
1503 if (ret < 0) {
1504 dev_err(cdns->dev, "clock stop exit failed %d\n", ret);
1505 return ret;
1506 }
1507
1508 cdns_updatel(cdns, CDNS_MCP_CONTROL,
1509 CDNS_MCP_CONTROL_BLOCK_WAKEUP, 0);
1510
1511 /*
1512 * clear CMD_ACCEPT so that the command ignored
1513 * will be treated as a failure during a broadcast write
1514 */
1515 cdns_updatel(cdns, CDNS_MCP_CONTROL, CDNS_MCP_CONTROL_CMD_ACCEPT, 0);
1516
1517 if (!bus_reset) {
1518
1519 /* enable bus operations with clock and data */
1520 cdns_updatel(cdns, CDNS_MCP_CONFIG,
1521 CDNS_MCP_CONFIG_OP,
1522 CDNS_MCP_CONFIG_OP_NORMAL);
1523
1524 ret = cdns_config_update(cdns);
1525 if (ret < 0) {
1526 dev_err(cdns->dev, "%s: config_update failed\n", __func__);
1527 return ret;
1528 }
1529
1530 ret = sdw_bus_exit_clk_stop(&cdns->bus);
1531 if (ret < 0)
1532 dev_err(cdns->dev, "bus failed to exit clock stop %d\n", ret);
1533 }
1534
1535 return ret;
1536}
1537EXPORT_SYMBOL(sdw_cdns_clock_restart);
1538
1539/**
Sanyog Kale956baa12017-12-14 11:19:42 +05301540 * sdw_cdns_probe() - Cadence probe routine
1541 * @cdns: Cadence instance
1542 */
1543int sdw_cdns_probe(struct sdw_cdns *cdns)
1544{
1545 init_completion(&cdns->tx_complete);
Vinod Koul07abeff2018-04-26 18:38:48 +05301546 cdns->bus.port_ops = &cdns_port_ops;
Sanyog Kale956baa12017-12-14 11:19:42 +05301547
Bard Liao4a98a6b2020-07-16 23:09:45 +08001548 INIT_WORK(&cdns->work, cdns_update_slave_status_work);
Sanyog Kale956baa12017-12-14 11:19:42 +05301549 return 0;
1550}
1551EXPORT_SYMBOL(sdw_cdns_probe);
1552
Vinod Koul5d6b3c82018-04-26 18:38:53 +05301553int cdns_set_sdw_stream(struct snd_soc_dai *dai,
Pierre-Louis Bossartbbb63812019-05-01 10:57:41 -05001554 void *stream, bool pcm, int direction)
Vinod Koul5d6b3c82018-04-26 18:38:53 +05301555{
1556 struct sdw_cdns *cdns = snd_soc_dai_get_drvdata(dai);
1557 struct sdw_cdns_dma_data *dma;
1558
Pierre-Louis Bossartb5e9e682020-07-01 02:43:55 +08001559 if (stream) {
1560 /* first paranoia check */
1561 if (direction == SNDRV_PCM_STREAM_PLAYBACK)
1562 dma = dai->playback_dma_data;
1563 else
1564 dma = dai->capture_dma_data;
Vinod Koul5d6b3c82018-04-26 18:38:53 +05301565
Pierre-Louis Bossartb5e9e682020-07-01 02:43:55 +08001566 if (dma) {
1567 dev_err(dai->dev,
1568 "dma_data already allocated for dai %s\n",
1569 dai->name);
1570 return -EINVAL;
1571 }
Vinod Koul5d6b3c82018-04-26 18:38:53 +05301572
Pierre-Louis Bossartb5e9e682020-07-01 02:43:55 +08001573 /* allocate and set dma info */
1574 dma = kzalloc(sizeof(*dma), GFP_KERNEL);
1575 if (!dma)
1576 return -ENOMEM;
Vinod Koul5d6b3c82018-04-26 18:38:53 +05301577
Pierre-Louis Bossartb5e9e682020-07-01 02:43:55 +08001578 if (pcm)
1579 dma->stream_type = SDW_STREAM_PCM;
1580 else
1581 dma->stream_type = SDW_STREAM_PDM;
Vinod Koul5d6b3c82018-04-26 18:38:53 +05301582
Pierre-Louis Bossartb5e9e682020-07-01 02:43:55 +08001583 dma->bus = &cdns->bus;
1584 dma->link_id = cdns->instance;
Vinod Koul5d6b3c82018-04-26 18:38:53 +05301585
Pierre-Louis Bossartb5e9e682020-07-01 02:43:55 +08001586 dma->stream = stream;
1587
1588 if (direction == SNDRV_PCM_STREAM_PLAYBACK)
1589 dai->playback_dma_data = dma;
1590 else
1591 dai->capture_dma_data = dma;
1592 } else {
1593 /* for NULL stream we release allocated dma_data */
1594 if (direction == SNDRV_PCM_STREAM_PLAYBACK) {
1595 kfree(dai->playback_dma_data);
1596 dai->playback_dma_data = NULL;
1597 } else {
1598 kfree(dai->capture_dma_data);
1599 dai->capture_dma_data = NULL;
1600 }
1601 }
Vinod Koul5d6b3c82018-04-26 18:38:53 +05301602 return 0;
1603}
1604EXPORT_SYMBOL(cdns_set_sdw_stream);
1605
1606/**
1607 * cdns_find_pdi() - Find a free PDI
1608 *
1609 * @cdns: Cadence instance
Pierre-Louis Bossart39737a32020-01-13 15:10:24 -06001610 * @offset: Starting offset
Vinod Koul5d6b3c82018-04-26 18:38:53 +05301611 * @num: Number of PDIs
1612 * @pdi: PDI instances
Pierre-Louis Bossart39737a32020-01-13 15:10:24 -06001613 * @dai_id: DAI id
Vinod Koul5d6b3c82018-04-26 18:38:53 +05301614 *
Bard Liao1b53385e2019-09-16 14:23:48 -05001615 * Find a PDI for a given PDI array. The PDI num and dai_id are
1616 * expected to match, return NULL otherwise.
Vinod Koul5d6b3c82018-04-26 18:38:53 +05301617 */
1618static struct sdw_cdns_pdi *cdns_find_pdi(struct sdw_cdns *cdns,
Pierre-Louis Bossart807c15b2019-09-16 14:23:47 -05001619 unsigned int offset,
Pierre-Louis Bossartbbb63812019-05-01 10:57:41 -05001620 unsigned int num,
Bard Liao1b53385e2019-09-16 14:23:48 -05001621 struct sdw_cdns_pdi *pdi,
1622 int dai_id)
Vinod Koul5d6b3c82018-04-26 18:38:53 +05301623{
1624 int i;
1625
Bard Liao1b53385e2019-09-16 14:23:48 -05001626 for (i = offset; i < offset + num; i++)
1627 if (pdi[i].num == dai_id)
1628 return &pdi[i];
Vinod Koul5d6b3c82018-04-26 18:38:53 +05301629
1630 return NULL;
1631}
1632
1633/**
1634 * sdw_cdns_config_stream: Configure a stream
1635 *
1636 * @cdns: Cadence instance
Vinod Koul5d6b3c82018-04-26 18:38:53 +05301637 * @ch: Channel count
1638 * @dir: Data direction
1639 * @pdi: PDI to be used
1640 */
1641void sdw_cdns_config_stream(struct sdw_cdns *cdns,
Pierre-Louis Bossartbbb63812019-05-01 10:57:41 -05001642 u32 ch, u32 dir, struct sdw_cdns_pdi *pdi)
Vinod Koul5d6b3c82018-04-26 18:38:53 +05301643{
1644 u32 offset, val = 0;
1645
Pierre-Louis Bossart9e4e6012020-09-21 03:32:07 +08001646 if (dir == SDW_DATA_DIR_RX) {
Vinod Koul5d6b3c82018-04-26 18:38:53 +05301647 val = CDNS_PORTCTRL_DIRN;
1648
Pierre-Louis Bossart9e4e6012020-09-21 03:32:07 +08001649 if (cdns->bus.params.m_data_mode != SDW_PORT_DATA_MODE_NORMAL)
1650 val |= CDNS_PORTCTRL_TEST_FAILED;
1651 }
Pierre-Louis Bossart57a34792019-09-16 14:23:46 -05001652 offset = CDNS_PORTCTRL + pdi->num * CDNS_PORT_OFFSET;
Pierre-Louis Bossart9e4e6012020-09-21 03:32:07 +08001653 cdns_updatel(cdns, offset,
1654 CDNS_PORTCTRL_DIRN | CDNS_PORTCTRL_TEST_FAILED,
1655 val);
Vinod Koul5d6b3c82018-04-26 18:38:53 +05301656
Pierre-Louis Bossart57a34792019-09-16 14:23:46 -05001657 val = pdi->num;
randerwangb468a782020-03-17 11:33:29 -05001658 val |= CDNS_PDI_CONFIG_SOFT_RESET;
Vinod Koul3cf25d62020-09-03 17:15:01 +05301659 val |= FIELD_PREP(CDNS_PDI_CONFIG_CHANNEL, (1 << ch) - 1);
Vinod Koul5d6b3c82018-04-26 18:38:53 +05301660 cdns_writel(cdns, CDNS_PDI_CONFIG(pdi->num), val);
1661}
1662EXPORT_SYMBOL(sdw_cdns_config_stream);
1663
1664/**
Pierre-Louis Bossart57a34792019-09-16 14:23:46 -05001665 * sdw_cdns_alloc_pdi() - Allocate a PDI
Vinod Koul5d6b3c82018-04-26 18:38:53 +05301666 *
1667 * @cdns: Cadence instance
1668 * @stream: Stream to be allocated
1669 * @ch: Channel count
1670 * @dir: Data direction
Pierre-Louis Bossart39737a32020-01-13 15:10:24 -06001671 * @dai_id: DAI id
Vinod Koul5d6b3c82018-04-26 18:38:53 +05301672 */
Pierre-Louis Bossart57a34792019-09-16 14:23:46 -05001673struct sdw_cdns_pdi *sdw_cdns_alloc_pdi(struct sdw_cdns *cdns,
1674 struct sdw_cdns_streams *stream,
Bard Liao1b53385e2019-09-16 14:23:48 -05001675 u32 ch, u32 dir, int dai_id)
Vinod Koul5d6b3c82018-04-26 18:38:53 +05301676{
1677 struct sdw_cdns_pdi *pdi = NULL;
1678
1679 if (dir == SDW_DATA_DIR_RX)
Bard Liao1b53385e2019-09-16 14:23:48 -05001680 pdi = cdns_find_pdi(cdns, 0, stream->num_in, stream->in,
1681 dai_id);
Vinod Koul5d6b3c82018-04-26 18:38:53 +05301682 else
Bard Liao1b53385e2019-09-16 14:23:48 -05001683 pdi = cdns_find_pdi(cdns, 0, stream->num_out, stream->out,
1684 dai_id);
Vinod Koul5d6b3c82018-04-26 18:38:53 +05301685
1686 /* check if we found a PDI, else find in bi-directional */
1687 if (!pdi)
Bard Liao1b53385e2019-09-16 14:23:48 -05001688 pdi = cdns_find_pdi(cdns, 2, stream->num_bd, stream->bd,
1689 dai_id);
Vinod Koul5d6b3c82018-04-26 18:38:53 +05301690
Pierre-Louis Bossart57a34792019-09-16 14:23:46 -05001691 if (pdi) {
1692 pdi->l_ch_num = 0;
1693 pdi->h_ch_num = ch - 1;
1694 pdi->dir = dir;
1695 pdi->ch_count = ch;
1696 }
Vinod Koul5d6b3c82018-04-26 18:38:53 +05301697
Pierre-Louis Bossart57a34792019-09-16 14:23:46 -05001698 return pdi;
Vinod Koul5d6b3c82018-04-26 18:38:53 +05301699}
Pierre-Louis Bossart57a34792019-09-16 14:23:46 -05001700EXPORT_SYMBOL(sdw_cdns_alloc_pdi);
Vinod Koul5d6b3c82018-04-26 18:38:53 +05301701
Vinod Koul2f52a512017-12-14 11:19:41 +05301702MODULE_LICENSE("Dual BSD/GPL");
1703MODULE_DESCRIPTION("Cadence Soundwire Library");