d3e9cd3faadfd54fccca99da12c878cc099d6905
[releases.git] / cadence_master.c
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                         2000
192
193 #define CDNS_SCP_RX_FIFOLEVEL                   0x2
194
195 /*
196  * register accessor helpers
197  */
198 static inline u32 cdns_readl(struct sdw_cdns *cdns, int offset)
199 {
200         return readl(cdns->registers + offset);
201 }
202
203 static inline void cdns_writel(struct sdw_cdns *cdns, int offset, u32 value)
204 {
205         writel(value, cdns->registers + offset);
206 }
207
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
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
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  */
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
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
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
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
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_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
450 DEFINE_DEBUGFS_ATTRIBUTE(cdns_parity_error_fops, NULL,
451                          cdns_parity_error_injection, "%llu\n");
452
453 /**
454  * sdw_cdns_debugfs_init() - Cadence debugfs init
455  * @cdns: Cadence instance
456  * @root: debugfs root
457  */
458 void sdw_cdns_debugfs_init(struct sdw_cdns *cdns, struct dentry *root)
459 {
460         debugfs_create_file("cdns-registers", 0400, root, cdns, &cdns_reg_fops);
461
462         debugfs_create_file("cdns-hw-reset", 0200, root, cdns,
463                             &cdns_hw_reset_fops);
464
465         debugfs_create_file("cdns-parity-error-injection", 0200, root, cdns,
466                             &cdns_parity_error_fops);
467 }
468 EXPORT_SYMBOL_GPL(sdw_cdns_debugfs_init);
469
470 #endif /* CONFIG_DEBUG_FS */
471
472 /*
473  * IO Calls
474  */
475 static enum sdw_command_response
476 cdns_fill_msg_resp(struct sdw_cdns *cdns,
477                    struct sdw_msg *msg, int count, int offset)
478 {
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;
486                         dev_dbg_ratelimited(cdns->dev, "Msg Ack not received\n");
487                 }
488                 if (cdns->response_buf[i] & CDNS_MCP_RESP_NACK) {
489                         nack = 1;
490                         dev_err_ratelimited(cdns->dev, "Msg NACK received\n");
491                 }
492         }
493
494         if (nack) {
495                 dev_err_ratelimited(cdns->dev, "Msg NACKed for Slave %d\n", msg->dev_num);
496                 return SDW_CMD_FAIL;
497         }
498
499         if (no_ack) {
500                 dev_dbg_ratelimited(cdns->dev, "Msg ignored for Slave %d\n", msg->dev_num);
501                 return SDW_CMD_IGNORED;
502         }
503
504         if (msg->flags == SDW_MSG_FLAG_READ) {
505                 /* fill response */
506                 for (i = 0; i < count; i++)
507                         msg->buf[i + offset] = FIELD_GET(CDNS_MCP_RESP_RDATA,
508                                                          cdns->response_buf[i]);
509         }
510
511         return SDW_CMD_OK;
512 }
513
514 static void cdns_read_response(struct sdw_cdns *cdns)
515 {
516         u32 num_resp, cmd_base;
517         int i;
518
519         /* RX_FIFO_AVAIL can be 2 entries more than the FIFO size */
520         BUILD_BUG_ON(ARRAY_SIZE(cdns->response_buf) < CDNS_MCP_CMD_LEN + 2);
521
522         num_resp = cdns_readl(cdns, CDNS_MCP_FIFOSTAT);
523         num_resp &= CDNS_MCP_RX_FIFO_AVAIL;
524         if (num_resp > ARRAY_SIZE(cdns->response_buf)) {
525                 dev_warn(cdns->dev, "RX AVAIL %d too long\n", num_resp);
526                 num_resp = ARRAY_SIZE(cdns->response_buf);
527         }
528
529         cmd_base = CDNS_MCP_CMD_BASE;
530
531         for (i = 0; i < num_resp; i++) {
532                 cdns->response_buf[i] = cdns_readl(cdns, cmd_base);
533                 cmd_base += CDNS_MCP_CMD_WORD_LEN;
534         }
535 }
536
537 static enum sdw_command_response
538 _cdns_xfer_msg(struct sdw_cdns *cdns, struct sdw_msg *msg, int cmd,
539                int offset, int count, bool defer)
540 {
541         unsigned long time;
542         u32 base, i, data;
543         u16 addr;
544
545         /* Program the watermark level for RX FIFO */
546         if (cdns->msg_count != count) {
547                 cdns_writel(cdns, CDNS_MCP_FIFOLEVEL, count);
548                 cdns->msg_count = count;
549         }
550
551         base = CDNS_MCP_CMD_BASE;
552         addr = msg->addr;
553
554         for (i = 0; i < count; i++) {
555                 data = FIELD_PREP(CDNS_MCP_CMD_DEV_ADDR, msg->dev_num);
556                 data |= FIELD_PREP(CDNS_MCP_CMD_COMMAND, cmd);
557                 data |= FIELD_PREP(CDNS_MCP_CMD_REG_ADDR, addr);
558                 addr++;
559
560                 if (msg->flags == SDW_MSG_FLAG_WRITE)
561                         data |= msg->buf[i + offset];
562
563                 data |= FIELD_PREP(CDNS_MCP_CMD_SSP_TAG, msg->ssp_sync);
564                 cdns_writel(cdns, base, data);
565                 base += CDNS_MCP_CMD_WORD_LEN;
566         }
567
568         if (defer)
569                 return SDW_CMD_OK;
570
571         /* wait for timeout or response */
572         time = wait_for_completion_timeout(&cdns->tx_complete,
573                                            msecs_to_jiffies(CDNS_TX_TIMEOUT));
574         if (!time) {
575                 dev_err(cdns->dev, "IO transfer timed out, cmd %d device %d addr %x len %d\n",
576                         cmd, msg->dev_num, msg->addr, msg->len);
577                 msg->len = 0;
578
579                 /* Drain anything in the RX_FIFO */
580                 cdns_read_response(cdns);
581
582                 return SDW_CMD_TIMEOUT;
583         }
584
585         return cdns_fill_msg_resp(cdns, msg, count, offset);
586 }
587
588 static enum sdw_command_response
589 cdns_program_scp_addr(struct sdw_cdns *cdns, struct sdw_msg *msg)
590 {
591         int nack = 0, no_ack = 0;
592         unsigned long time;
593         u32 data[2], base;
594         int i;
595
596         /* Program the watermark level for RX FIFO */
597         if (cdns->msg_count != CDNS_SCP_RX_FIFOLEVEL) {
598                 cdns_writel(cdns, CDNS_MCP_FIFOLEVEL, CDNS_SCP_RX_FIFOLEVEL);
599                 cdns->msg_count = CDNS_SCP_RX_FIFOLEVEL;
600         }
601
602         data[0] = FIELD_PREP(CDNS_MCP_CMD_DEV_ADDR, msg->dev_num);
603         data[0] |= FIELD_PREP(CDNS_MCP_CMD_COMMAND, 0x3);
604         data[1] = data[0];
605
606         data[0] |= FIELD_PREP(CDNS_MCP_CMD_REG_ADDR, SDW_SCP_ADDRPAGE1);
607         data[1] |= FIELD_PREP(CDNS_MCP_CMD_REG_ADDR, SDW_SCP_ADDRPAGE2);
608
609         data[0] |= msg->addr_page1;
610         data[1] |= msg->addr_page2;
611
612         base = CDNS_MCP_CMD_BASE;
613         cdns_writel(cdns, base, data[0]);
614         base += CDNS_MCP_CMD_WORD_LEN;
615         cdns_writel(cdns, base, data[1]);
616
617         time = wait_for_completion_timeout(&cdns->tx_complete,
618                                            msecs_to_jiffies(CDNS_TX_TIMEOUT));
619         if (!time) {
620                 dev_err(cdns->dev, "SCP Msg trf timed out\n");
621                 msg->len = 0;
622                 return SDW_CMD_TIMEOUT;
623         }
624
625         /* check response the writes */
626         for (i = 0; i < 2; i++) {
627                 if (!(cdns->response_buf[i] & CDNS_MCP_RESP_ACK)) {
628                         no_ack = 1;
629                         dev_err(cdns->dev, "Program SCP Ack not received\n");
630                         if (cdns->response_buf[i] & CDNS_MCP_RESP_NACK) {
631                                 nack = 1;
632                                 dev_err(cdns->dev, "Program SCP NACK received\n");
633                         }
634                 }
635         }
636
637         /* For NACK, NO ack, don't return err if we are in Broadcast mode */
638         if (nack) {
639                 dev_err_ratelimited(cdns->dev,
640                                     "SCP_addrpage NACKed for Slave %d\n", msg->dev_num);
641                 return SDW_CMD_FAIL;
642         }
643
644         if (no_ack) {
645                 dev_dbg_ratelimited(cdns->dev,
646                                     "SCP_addrpage ignored for Slave %d\n", msg->dev_num);
647                 return SDW_CMD_IGNORED;
648         }
649
650         return SDW_CMD_OK;
651 }
652
653 static int cdns_prep_msg(struct sdw_cdns *cdns, struct sdw_msg *msg, int *cmd)
654 {
655         int ret;
656
657         if (msg->page) {
658                 ret = cdns_program_scp_addr(cdns, msg);
659                 if (ret) {
660                         msg->len = 0;
661                         return ret;
662                 }
663         }
664
665         switch (msg->flags) {
666         case SDW_MSG_FLAG_READ:
667                 *cmd = CDNS_MCP_CMD_READ;
668                 break;
669
670         case SDW_MSG_FLAG_WRITE:
671                 *cmd = CDNS_MCP_CMD_WRITE;
672                 break;
673
674         default:
675                 dev_err(cdns->dev, "Invalid msg cmd: %d\n", msg->flags);
676                 return -EINVAL;
677         }
678
679         return 0;
680 }
681
682 enum sdw_command_response
683 cdns_xfer_msg(struct sdw_bus *bus, struct sdw_msg *msg)
684 {
685         struct sdw_cdns *cdns = bus_to_cdns(bus);
686         int cmd = 0, ret, i;
687
688         ret = cdns_prep_msg(cdns, msg, &cmd);
689         if (ret)
690                 return SDW_CMD_FAIL_OTHER;
691
692         for (i = 0; i < msg->len / CDNS_MCP_CMD_LEN; i++) {
693                 ret = _cdns_xfer_msg(cdns, msg, cmd, i * CDNS_MCP_CMD_LEN,
694                                      CDNS_MCP_CMD_LEN, false);
695                 if (ret < 0)
696                         goto exit;
697         }
698
699         if (!(msg->len % CDNS_MCP_CMD_LEN))
700                 goto exit;
701
702         ret = _cdns_xfer_msg(cdns, msg, cmd, i * CDNS_MCP_CMD_LEN,
703                              msg->len % CDNS_MCP_CMD_LEN, false);
704
705 exit:
706         return ret;
707 }
708 EXPORT_SYMBOL(cdns_xfer_msg);
709
710 enum sdw_command_response
711 cdns_xfer_msg_defer(struct sdw_bus *bus,
712                     struct sdw_msg *msg, struct sdw_defer *defer)
713 {
714         struct sdw_cdns *cdns = bus_to_cdns(bus);
715         int cmd = 0, ret;
716
717         /* for defer only 1 message is supported */
718         if (msg->len > 1)
719                 return -ENOTSUPP;
720
721         ret = cdns_prep_msg(cdns, msg, &cmd);
722         if (ret)
723                 return SDW_CMD_FAIL_OTHER;
724
725         cdns->defer = defer;
726         cdns->defer->length = msg->len;
727
728         return _cdns_xfer_msg(cdns, msg, cmd, 0, msg->len, true);
729 }
730 EXPORT_SYMBOL(cdns_xfer_msg_defer);
731
732 enum sdw_command_response
733 cdns_reset_page_addr(struct sdw_bus *bus, unsigned int dev_num)
734 {
735         struct sdw_cdns *cdns = bus_to_cdns(bus);
736         struct sdw_msg msg;
737
738         /* Create dummy message with valid device number */
739         memset(&msg, 0, sizeof(msg));
740         msg.dev_num = dev_num;
741
742         return cdns_program_scp_addr(cdns, &msg);
743 }
744 EXPORT_SYMBOL(cdns_reset_page_addr);
745
746 /*
747  * IRQ handling
748  */
749
750 static int cdns_update_slave_status(struct sdw_cdns *cdns,
751                                     u32 slave0, u32 slave1)
752 {
753         enum sdw_slave_status status[SDW_MAX_DEVICES + 1];
754         bool is_slave = false;
755         u64 slave;
756         u32 mask;
757         int i, set_status;
758
759         /* combine the two status */
760         slave = ((u64)slave1 << 32) | slave0;
761         memset(status, 0, sizeof(status));
762
763         for (i = 0; i <= SDW_MAX_DEVICES; i++) {
764                 mask = (slave >> (i * CDNS_MCP_SLAVE_STATUS_NUM)) &
765                                 CDNS_MCP_SLAVE_STATUS_BITS;
766                 if (!mask)
767                         continue;
768
769                 is_slave = true;
770                 set_status = 0;
771
772                 if (mask & CDNS_MCP_SLAVE_INTSTAT_RESERVED) {
773                         status[i] = SDW_SLAVE_RESERVED;
774                         set_status++;
775                 }
776
777                 if (mask & CDNS_MCP_SLAVE_INTSTAT_ATTACHED) {
778                         status[i] = SDW_SLAVE_ATTACHED;
779                         set_status++;
780                 }
781
782                 if (mask & CDNS_MCP_SLAVE_INTSTAT_ALERT) {
783                         status[i] = SDW_SLAVE_ALERT;
784                         set_status++;
785                 }
786
787                 if (mask & CDNS_MCP_SLAVE_INTSTAT_NPRESENT) {
788                         status[i] = SDW_SLAVE_UNATTACHED;
789                         set_status++;
790                 }
791
792                 /* first check if Slave reported multiple status */
793                 if (set_status > 1) {
794                         u32 val;
795
796                         dev_warn_ratelimited(cdns->dev,
797                                              "Slave %d reported multiple Status: %d\n",
798                                              i, mask);
799
800                         /* check latest status extracted from PING commands */
801                         val = cdns_readl(cdns, CDNS_MCP_SLAVE_STAT);
802                         val >>= (i * 2);
803
804                         switch (val & 0x3) {
805                         case 0:
806                                 status[i] = SDW_SLAVE_UNATTACHED;
807                                 break;
808                         case 1:
809                                 status[i] = SDW_SLAVE_ATTACHED;
810                                 break;
811                         case 2:
812                                 status[i] = SDW_SLAVE_ALERT;
813                                 break;
814                         case 3:
815                         default:
816                                 status[i] = SDW_SLAVE_RESERVED;
817                                 break;
818                         }
819
820                         dev_warn_ratelimited(cdns->dev,
821                                              "Slave %d status updated to %d\n",
822                                              i, status[i]);
823
824                 }
825         }
826
827         if (is_slave)
828                 return sdw_handle_slave_status(&cdns->bus, status);
829
830         return 0;
831 }
832
833 /**
834  * sdw_cdns_irq() - Cadence interrupt handler
835  * @irq: irq number
836  * @dev_id: irq context
837  */
838 irqreturn_t sdw_cdns_irq(int irq, void *dev_id)
839 {
840         struct sdw_cdns *cdns = dev_id;
841         u32 int_status;
842         int ret = IRQ_HANDLED;
843
844         /* Check if the link is up */
845         if (!cdns->link_up)
846                 return IRQ_NONE;
847
848         int_status = cdns_readl(cdns, CDNS_MCP_INTSTAT);
849
850         /* check for reserved values read as zero */
851         if (int_status & CDNS_MCP_INT_RESERVED)
852                 return IRQ_NONE;
853
854         if (!(int_status & CDNS_MCP_INT_IRQ))
855                 return IRQ_NONE;
856
857         if (int_status & CDNS_MCP_INT_RX_WL) {
858                 cdns_read_response(cdns);
859
860                 if (cdns->defer) {
861                         cdns_fill_msg_resp(cdns, cdns->defer->msg,
862                                            cdns->defer->length, 0);
863                         complete(&cdns->defer->complete);
864                         cdns->defer = NULL;
865                 } else {
866                         complete(&cdns->tx_complete);
867                 }
868         }
869
870         if (int_status & CDNS_MCP_INT_PARITY) {
871                 /* Parity error detected by Master */
872                 dev_err_ratelimited(cdns->dev, "Parity error\n");
873         }
874
875         if (int_status & CDNS_MCP_INT_CTRL_CLASH) {
876                 /* Slave is driving bit slot during control word */
877                 dev_err_ratelimited(cdns->dev, "Bus clash for control word\n");
878         }
879
880         if (int_status & CDNS_MCP_INT_DATA_CLASH) {
881                 /*
882                  * Multiple slaves trying to drive bit slot, or issue with
883                  * ownership of data bits or Slave gone bonkers
884                  */
885                 dev_err_ratelimited(cdns->dev, "Bus clash for data word\n");
886         }
887
888         if (cdns->bus.params.m_data_mode != SDW_PORT_DATA_MODE_NORMAL &&
889             int_status & CDNS_MCP_INT_DPINT) {
890                 u32 port_intstat;
891
892                 /* just log which ports report an error */
893                 port_intstat = cdns_readl(cdns, CDNS_MCP_PORT_INTSTAT);
894                 dev_err_ratelimited(cdns->dev, "DP interrupt: PortIntStat %8x\n",
895                                     port_intstat);
896
897                 /* clear status w/ write1 */
898                 cdns_writel(cdns, CDNS_MCP_PORT_INTSTAT, port_intstat);
899         }
900
901         if (int_status & CDNS_MCP_INT_SLAVE_MASK) {
902                 /* Mask the Slave interrupt and wake thread */
903                 cdns_updatel(cdns, CDNS_MCP_INTMASK,
904                              CDNS_MCP_INT_SLAVE_MASK, 0);
905
906                 int_status &= ~CDNS_MCP_INT_SLAVE_MASK;
907
908                 /*
909                  * Deal with possible race condition between interrupt
910                  * handling and disabling interrupts on suspend.
911                  *
912                  * If the master is in the process of disabling
913                  * interrupts, don't schedule a workqueue
914                  */
915                 if (cdns->interrupt_enabled)
916                         schedule_work(&cdns->work);
917         }
918
919         cdns_writel(cdns, CDNS_MCP_INTSTAT, int_status);
920         return ret;
921 }
922 EXPORT_SYMBOL(sdw_cdns_irq);
923
924 /**
925  * To update slave status in a work since we will need to handle
926  * other interrupts eg. CDNS_MCP_INT_RX_WL during the update slave
927  * process.
928  * @work: cdns worker thread
929  */
930 static void cdns_update_slave_status_work(struct work_struct *work)
931 {
932         struct sdw_cdns *cdns =
933                 container_of(work, struct sdw_cdns, work);
934         u32 slave0, slave1;
935
936         dev_dbg_ratelimited(cdns->dev, "Slave status change\n");
937
938         slave0 = cdns_readl(cdns, CDNS_MCP_SLAVE_INTSTAT0);
939         slave1 = cdns_readl(cdns, CDNS_MCP_SLAVE_INTSTAT1);
940
941         cdns_update_slave_status(cdns, slave0, slave1);
942         cdns_writel(cdns, CDNS_MCP_SLAVE_INTSTAT0, slave0);
943         cdns_writel(cdns, CDNS_MCP_SLAVE_INTSTAT1, slave1);
944
945         /* clear and unmask Slave interrupt now */
946         cdns_writel(cdns, CDNS_MCP_INTSTAT, CDNS_MCP_INT_SLAVE_MASK);
947         cdns_updatel(cdns, CDNS_MCP_INTMASK,
948                      CDNS_MCP_INT_SLAVE_MASK, CDNS_MCP_INT_SLAVE_MASK);
949
950 }
951
952 /*
953  * init routines
954  */
955
956 /**
957  * sdw_cdns_exit_reset() - Program reset parameters and start bus operations
958  * @cdns: Cadence instance
959  */
960 int sdw_cdns_exit_reset(struct sdw_cdns *cdns)
961 {
962         /* program maximum length reset to be safe */
963         cdns_updatel(cdns, CDNS_MCP_CONTROL,
964                      CDNS_MCP_CONTROL_RST_DELAY,
965                      CDNS_MCP_CONTROL_RST_DELAY);
966
967         /* use hardware generated reset */
968         cdns_updatel(cdns, CDNS_MCP_CONTROL,
969                      CDNS_MCP_CONTROL_HW_RST,
970                      CDNS_MCP_CONTROL_HW_RST);
971
972         /* commit changes */
973         cdns_updatel(cdns, CDNS_MCP_CONFIG_UPDATE,
974                      CDNS_MCP_CONFIG_UPDATE_BIT,
975                      CDNS_MCP_CONFIG_UPDATE_BIT);
976
977         /* don't wait here */
978         return 0;
979
980 }
981 EXPORT_SYMBOL(sdw_cdns_exit_reset);
982
983 /**
984  * sdw_cdns_enable_slave_interrupt() - Enable SDW slave interrupts
985  * @cdns: Cadence instance
986  * @state: boolean for true/false
987  */
988 static void cdns_enable_slave_interrupts(struct sdw_cdns *cdns, bool state)
989 {
990         u32 mask;
991
992         mask = cdns_readl(cdns, CDNS_MCP_INTMASK);
993         if (state)
994                 mask |= CDNS_MCP_INT_SLAVE_MASK;
995         else
996                 mask &= ~CDNS_MCP_INT_SLAVE_MASK;
997
998         cdns_writel(cdns, CDNS_MCP_INTMASK, mask);
999 }
1000
1001 /**
1002  * sdw_cdns_enable_interrupt() - Enable SDW interrupts
1003  * @cdns: Cadence instance
1004  * @state: True if we are trying to enable interrupt.
1005  */
1006 int sdw_cdns_enable_interrupt(struct sdw_cdns *cdns, bool state)
1007 {
1008         u32 slave_intmask0 = 0;
1009         u32 slave_intmask1 = 0;
1010         u32 mask = 0;
1011
1012         if (!state)
1013                 goto update_masks;
1014
1015         slave_intmask0 = CDNS_MCP_SLAVE_INTMASK0_MASK;
1016         slave_intmask1 = CDNS_MCP_SLAVE_INTMASK1_MASK;
1017
1018         /* enable detection of all slave state changes */
1019         mask = CDNS_MCP_INT_SLAVE_MASK;
1020
1021         /* enable detection of bus issues */
1022         mask |= CDNS_MCP_INT_CTRL_CLASH | CDNS_MCP_INT_DATA_CLASH |
1023                 CDNS_MCP_INT_PARITY;
1024
1025         /* port interrupt limited to test modes for now */
1026         if (cdns->bus.params.m_data_mode != SDW_PORT_DATA_MODE_NORMAL)
1027                 mask |= CDNS_MCP_INT_DPINT;
1028
1029         /* enable detection of RX fifo level */
1030         mask |= CDNS_MCP_INT_RX_WL;
1031
1032         /*
1033          * CDNS_MCP_INT_IRQ needs to be set otherwise all previous
1034          * settings are irrelevant
1035          */
1036         mask |= CDNS_MCP_INT_IRQ;
1037
1038         if (interrupt_mask) /* parameter override */
1039                 mask = interrupt_mask;
1040
1041 update_masks:
1042         /* clear slave interrupt status before enabling interrupt */
1043         if (state) {
1044                 u32 slave_state;
1045
1046                 slave_state = cdns_readl(cdns, CDNS_MCP_SLAVE_INTSTAT0);
1047                 cdns_writel(cdns, CDNS_MCP_SLAVE_INTSTAT0, slave_state);
1048                 slave_state = cdns_readl(cdns, CDNS_MCP_SLAVE_INTSTAT1);
1049                 cdns_writel(cdns, CDNS_MCP_SLAVE_INTSTAT1, slave_state);
1050         }
1051         cdns->interrupt_enabled = state;
1052
1053         /*
1054          * Complete any on-going status updates before updating masks,
1055          * and cancel queued status updates.
1056          *
1057          * There could be a race with a new interrupt thrown before
1058          * the 3 mask updates below are complete, so in the interrupt
1059          * we use the 'interrupt_enabled' status to prevent new work
1060          * from being queued.
1061          */
1062         if (!state)
1063                 cancel_work_sync(&cdns->work);
1064
1065         cdns_writel(cdns, CDNS_MCP_SLAVE_INTMASK0, slave_intmask0);
1066         cdns_writel(cdns, CDNS_MCP_SLAVE_INTMASK1, slave_intmask1);
1067         cdns_writel(cdns, CDNS_MCP_INTMASK, mask);
1068
1069         return 0;
1070 }
1071 EXPORT_SYMBOL(sdw_cdns_enable_interrupt);
1072
1073 static int cdns_allocate_pdi(struct sdw_cdns *cdns,
1074                              struct sdw_cdns_pdi **stream,
1075                              u32 num, u32 pdi_offset)
1076 {
1077         struct sdw_cdns_pdi *pdi;
1078         int i;
1079
1080         if (!num)
1081                 return 0;
1082
1083         pdi = devm_kcalloc(cdns->dev, num, sizeof(*pdi), GFP_KERNEL);
1084         if (!pdi)
1085                 return -ENOMEM;
1086
1087         for (i = 0; i < num; i++) {
1088                 pdi[i].num = i + pdi_offset;
1089         }
1090
1091         *stream = pdi;
1092         return 0;
1093 }
1094
1095 /**
1096  * sdw_cdns_pdi_init() - PDI initialization routine
1097  *
1098  * @cdns: Cadence instance
1099  * @config: Stream configurations
1100  */
1101 int sdw_cdns_pdi_init(struct sdw_cdns *cdns,
1102                       struct sdw_cdns_stream_config config)
1103 {
1104         struct sdw_cdns_streams *stream;
1105         int offset;
1106         int ret;
1107
1108         cdns->pcm.num_bd = config.pcm_bd;
1109         cdns->pcm.num_in = config.pcm_in;
1110         cdns->pcm.num_out = config.pcm_out;
1111         cdns->pdm.num_bd = config.pdm_bd;
1112         cdns->pdm.num_in = config.pdm_in;
1113         cdns->pdm.num_out = config.pdm_out;
1114
1115         /* Allocate PDIs for PCMs */
1116         stream = &cdns->pcm;
1117
1118         /* we allocate PDI0 and PDI1 which are used for Bulk */
1119         offset = 0;
1120
1121         ret = cdns_allocate_pdi(cdns, &stream->bd,
1122                                 stream->num_bd, offset);
1123         if (ret)
1124                 return ret;
1125
1126         offset += stream->num_bd;
1127
1128         ret = cdns_allocate_pdi(cdns, &stream->in,
1129                                 stream->num_in, offset);
1130         if (ret)
1131                 return ret;
1132
1133         offset += stream->num_in;
1134
1135         ret = cdns_allocate_pdi(cdns, &stream->out,
1136                                 stream->num_out, offset);
1137         if (ret)
1138                 return ret;
1139
1140         /* Update total number of PCM PDIs */
1141         stream->num_pdi = stream->num_bd + stream->num_in + stream->num_out;
1142         cdns->num_ports = stream->num_pdi;
1143
1144         /* Allocate PDIs for PDMs */
1145         stream = &cdns->pdm;
1146         ret = cdns_allocate_pdi(cdns, &stream->bd,
1147                                 stream->num_bd, offset);
1148         if (ret)
1149                 return ret;
1150
1151         offset += stream->num_bd;
1152
1153         ret = cdns_allocate_pdi(cdns, &stream->in,
1154                                 stream->num_in, offset);
1155         if (ret)
1156                 return ret;
1157
1158         offset += stream->num_in;
1159
1160         ret = cdns_allocate_pdi(cdns, &stream->out,
1161                                 stream->num_out, offset);
1162
1163         if (ret)
1164                 return ret;
1165
1166         /* Update total number of PDM PDIs */
1167         stream->num_pdi = stream->num_bd + stream->num_in + stream->num_out;
1168         cdns->num_ports += stream->num_pdi;
1169
1170         return 0;
1171 }
1172 EXPORT_SYMBOL(sdw_cdns_pdi_init);
1173
1174 static u32 cdns_set_initial_frame_shape(int n_rows, int n_cols)
1175 {
1176         u32 val;
1177         int c;
1178         int r;
1179
1180         r = sdw_find_row_index(n_rows);
1181         c = sdw_find_col_index(n_cols);
1182
1183         val = FIELD_PREP(CDNS_MCP_FRAME_SHAPE_ROW_MASK, r);
1184         val |= FIELD_PREP(CDNS_MCP_FRAME_SHAPE_COL_MASK, c);
1185
1186         return val;
1187 }
1188
1189 static void cdns_init_clock_ctrl(struct sdw_cdns *cdns)
1190 {
1191         struct sdw_bus *bus = &cdns->bus;
1192         struct sdw_master_prop *prop = &bus->prop;
1193         u32 val;
1194         u32 ssp_interval;
1195         int divider;
1196
1197         /* Set clock divider */
1198         divider = (prop->mclk_freq / prop->max_clk_freq) - 1;
1199
1200         cdns_updatel(cdns, CDNS_MCP_CLK_CTRL0,
1201                      CDNS_MCP_CLK_MCLKD_MASK, divider);
1202         cdns_updatel(cdns, CDNS_MCP_CLK_CTRL1,
1203                      CDNS_MCP_CLK_MCLKD_MASK, divider);
1204
1205         /*
1206          * Frame shape changes after initialization have to be done
1207          * with the bank switch mechanism
1208          */
1209         val = cdns_set_initial_frame_shape(prop->default_row,
1210                                            prop->default_col);
1211         cdns_writel(cdns, CDNS_MCP_FRAME_SHAPE_INIT, val);
1212
1213         /* Set SSP interval to default value */
1214         ssp_interval = prop->default_frame_rate / SDW_CADENCE_GSYNC_HZ;
1215         cdns_writel(cdns, CDNS_MCP_SSP_CTRL0, ssp_interval);
1216         cdns_writel(cdns, CDNS_MCP_SSP_CTRL1, ssp_interval);
1217 }
1218
1219 /**
1220  * sdw_cdns_init() - Cadence initialization
1221  * @cdns: Cadence instance
1222  */
1223 int sdw_cdns_init(struct sdw_cdns *cdns)
1224 {
1225         u32 val;
1226
1227         cdns_init_clock_ctrl(cdns);
1228
1229         /* reset msg_count to default value of FIFOLEVEL */
1230         cdns->msg_count = cdns_readl(cdns, CDNS_MCP_FIFOLEVEL);
1231
1232         /* flush command FIFOs */
1233         cdns_updatel(cdns, CDNS_MCP_CONTROL, CDNS_MCP_CONTROL_CMD_RST,
1234                      CDNS_MCP_CONTROL_CMD_RST);
1235
1236         /* Set cmd accept mode */
1237         cdns_updatel(cdns, CDNS_MCP_CONTROL, CDNS_MCP_CONTROL_CMD_ACCEPT,
1238                      CDNS_MCP_CONTROL_CMD_ACCEPT);
1239
1240         /* Configure mcp config */
1241         val = cdns_readl(cdns, CDNS_MCP_CONFIG);
1242
1243         /* enable bus operations with clock and data */
1244         val &= ~CDNS_MCP_CONFIG_OP;
1245         val |= CDNS_MCP_CONFIG_OP_NORMAL;
1246
1247         /* Set cmd mode for Tx and Rx cmds */
1248         val &= ~CDNS_MCP_CONFIG_CMD;
1249
1250         /* Disable sniffer mode */
1251         val &= ~CDNS_MCP_CONFIG_SNIFFER;
1252
1253         /* Disable auto bus release */
1254         val &= ~CDNS_MCP_CONFIG_BUS_REL;
1255
1256         if (cdns->bus.multi_link)
1257                 /* Set Multi-master mode to take gsync into account */
1258                 val |= CDNS_MCP_CONFIG_MMASTER;
1259
1260         /* leave frame delay to hardware default of 0x1F */
1261
1262         /* leave command retry to hardware default of 0 */
1263
1264         cdns_writel(cdns, CDNS_MCP_CONFIG, val);
1265
1266         /* changes will be committed later */
1267         return 0;
1268 }
1269 EXPORT_SYMBOL(sdw_cdns_init);
1270
1271 int cdns_bus_conf(struct sdw_bus *bus, struct sdw_bus_params *params)
1272 {
1273         struct sdw_master_prop *prop = &bus->prop;
1274         struct sdw_cdns *cdns = bus_to_cdns(bus);
1275         int mcp_clkctrl_off;
1276         int divider;
1277
1278         if (!params->curr_dr_freq) {
1279                 dev_err(cdns->dev, "NULL curr_dr_freq\n");
1280                 return -EINVAL;
1281         }
1282
1283         divider = prop->mclk_freq * SDW_DOUBLE_RATE_FACTOR /
1284                 params->curr_dr_freq;
1285         divider--; /* divider is 1/(N+1) */
1286
1287         if (params->next_bank)
1288                 mcp_clkctrl_off = CDNS_MCP_CLK_CTRL1;
1289         else
1290                 mcp_clkctrl_off = CDNS_MCP_CLK_CTRL0;
1291
1292         cdns_updatel(cdns, mcp_clkctrl_off, CDNS_MCP_CLK_MCLKD_MASK, divider);
1293
1294         return 0;
1295 }
1296 EXPORT_SYMBOL(cdns_bus_conf);
1297
1298 static int cdns_port_params(struct sdw_bus *bus,
1299                             struct sdw_port_params *p_params, unsigned int bank)
1300 {
1301         struct sdw_cdns *cdns = bus_to_cdns(bus);
1302         int dpn_config = 0, dpn_config_off;
1303
1304         if (bank)
1305                 dpn_config_off = CDNS_DPN_B1_CONFIG(p_params->num);
1306         else
1307                 dpn_config_off = CDNS_DPN_B0_CONFIG(p_params->num);
1308
1309         dpn_config = cdns_readl(cdns, dpn_config_off);
1310
1311         u32p_replace_bits(&dpn_config, (p_params->bps - 1), CDNS_DPN_CONFIG_WL);
1312         u32p_replace_bits(&dpn_config, p_params->flow_mode, CDNS_DPN_CONFIG_PORT_FLOW);
1313         u32p_replace_bits(&dpn_config, p_params->data_mode, CDNS_DPN_CONFIG_PORT_DAT);
1314
1315         cdns_writel(cdns, dpn_config_off, dpn_config);
1316
1317         return 0;
1318 }
1319
1320 static int cdns_transport_params(struct sdw_bus *bus,
1321                                  struct sdw_transport_params *t_params,
1322                                  enum sdw_reg_bank bank)
1323 {
1324         struct sdw_cdns *cdns = bus_to_cdns(bus);
1325         int dpn_offsetctrl = 0, dpn_offsetctrl_off;
1326         int dpn_config = 0, dpn_config_off;
1327         int dpn_hctrl = 0, dpn_hctrl_off;
1328         int num = t_params->port_num;
1329         int dpn_samplectrl_off;
1330
1331         /*
1332          * Note: Only full data port is supported on the Master side for
1333          * both PCM and PDM ports.
1334          */
1335
1336         if (bank) {
1337                 dpn_config_off = CDNS_DPN_B1_CONFIG(num);
1338                 dpn_samplectrl_off = CDNS_DPN_B1_SAMPLE_CTRL(num);
1339                 dpn_hctrl_off = CDNS_DPN_B1_HCTRL(num);
1340                 dpn_offsetctrl_off = CDNS_DPN_B1_OFFSET_CTRL(num);
1341         } else {
1342                 dpn_config_off = CDNS_DPN_B0_CONFIG(num);
1343                 dpn_samplectrl_off = CDNS_DPN_B0_SAMPLE_CTRL(num);
1344                 dpn_hctrl_off = CDNS_DPN_B0_HCTRL(num);
1345                 dpn_offsetctrl_off = CDNS_DPN_B0_OFFSET_CTRL(num);
1346         }
1347
1348         dpn_config = cdns_readl(cdns, dpn_config_off);
1349         u32p_replace_bits(&dpn_config, t_params->blk_grp_ctrl, CDNS_DPN_CONFIG_BGC);
1350         u32p_replace_bits(&dpn_config, t_params->blk_pkg_mode, CDNS_DPN_CONFIG_BPM);
1351         cdns_writel(cdns, dpn_config_off, dpn_config);
1352
1353         u32p_replace_bits(&dpn_offsetctrl, t_params->offset1, CDNS_DPN_OFFSET_CTRL_1);
1354         u32p_replace_bits(&dpn_offsetctrl, t_params->offset2, CDNS_DPN_OFFSET_CTRL_2);
1355         cdns_writel(cdns, dpn_offsetctrl_off,  dpn_offsetctrl);
1356
1357         u32p_replace_bits(&dpn_hctrl, t_params->hstart, CDNS_DPN_HCTRL_HSTART);
1358         u32p_replace_bits(&dpn_hctrl, t_params->hstop, CDNS_DPN_HCTRL_HSTOP);
1359         u32p_replace_bits(&dpn_hctrl, t_params->lane_ctrl, CDNS_DPN_HCTRL_LCTRL);
1360
1361         cdns_writel(cdns, dpn_hctrl_off, dpn_hctrl);
1362         cdns_writel(cdns, dpn_samplectrl_off, (t_params->sample_interval - 1));
1363
1364         return 0;
1365 }
1366
1367 static int cdns_port_enable(struct sdw_bus *bus,
1368                             struct sdw_enable_ch *enable_ch, unsigned int bank)
1369 {
1370         struct sdw_cdns *cdns = bus_to_cdns(bus);
1371         int dpn_chnen_off, ch_mask;
1372
1373         if (bank)
1374                 dpn_chnen_off = CDNS_DPN_B1_CH_EN(enable_ch->port_num);
1375         else
1376                 dpn_chnen_off = CDNS_DPN_B0_CH_EN(enable_ch->port_num);
1377
1378         ch_mask = enable_ch->ch_mask * enable_ch->enable;
1379         cdns_writel(cdns, dpn_chnen_off, ch_mask);
1380
1381         return 0;
1382 }
1383
1384 static const struct sdw_master_port_ops cdns_port_ops = {
1385         .dpn_set_port_params = cdns_port_params,
1386         .dpn_set_port_transport_params = cdns_transport_params,
1387         .dpn_port_enable_ch = cdns_port_enable,
1388 };
1389
1390 /**
1391  * sdw_cdns_is_clock_stop: Check clock status
1392  *
1393  * @cdns: Cadence instance
1394  */
1395 bool sdw_cdns_is_clock_stop(struct sdw_cdns *cdns)
1396 {
1397         return !!(cdns_readl(cdns, CDNS_MCP_STAT) & CDNS_MCP_STAT_CLK_STOP);
1398 }
1399 EXPORT_SYMBOL(sdw_cdns_is_clock_stop);
1400
1401 /**
1402  * sdw_cdns_clock_stop: Cadence clock stop configuration routine
1403  *
1404  * @cdns: Cadence instance
1405  * @block_wake: prevent wakes if required by the platform
1406  */
1407 int sdw_cdns_clock_stop(struct sdw_cdns *cdns, bool block_wake)
1408 {
1409         bool slave_present = false;
1410         struct sdw_slave *slave;
1411         int ret;
1412
1413         /* Check suspend status */
1414         if (sdw_cdns_is_clock_stop(cdns)) {
1415                 dev_dbg(cdns->dev, "Clock is already stopped\n");
1416                 return 0;
1417         }
1418
1419         /*
1420          * Before entering clock stop we mask the Slave
1421          * interrupts. This helps avoid having to deal with e.g. a
1422          * Slave becoming UNATTACHED while the clock is being stopped
1423          */
1424         cdns_enable_slave_interrupts(cdns, false);
1425
1426         /*
1427          * For specific platforms, it is required to be able to put
1428          * master into a state in which it ignores wake-up trials
1429          * in clock stop state
1430          */
1431         if (block_wake)
1432                 cdns_updatel(cdns, CDNS_MCP_CONTROL,
1433                              CDNS_MCP_CONTROL_BLOCK_WAKEUP,
1434                              CDNS_MCP_CONTROL_BLOCK_WAKEUP);
1435
1436         list_for_each_entry(slave, &cdns->bus.slaves, node) {
1437                 if (slave->status == SDW_SLAVE_ATTACHED ||
1438                     slave->status == SDW_SLAVE_ALERT) {
1439                         slave_present = true;
1440                         break;
1441                 }
1442         }
1443
1444         /*
1445          * This CMD_ACCEPT should be used when there are no devices
1446          * attached on the link when entering clock stop mode. If this is
1447          * not set and there is a broadcast write then the command ignored
1448          * will be treated as a failure
1449          */
1450         if (!slave_present)
1451                 cdns_updatel(cdns, CDNS_MCP_CONTROL,
1452                              CDNS_MCP_CONTROL_CMD_ACCEPT,
1453                              CDNS_MCP_CONTROL_CMD_ACCEPT);
1454         else
1455                 cdns_updatel(cdns, CDNS_MCP_CONTROL,
1456                              CDNS_MCP_CONTROL_CMD_ACCEPT, 0);
1457
1458         /* commit changes */
1459         ret = cdns_config_update(cdns);
1460         if (ret < 0) {
1461                 dev_err(cdns->dev, "%s: config_update failed\n", __func__);
1462                 return ret;
1463         }
1464
1465         /* Prepare slaves for clock stop */
1466         if (slave_present) {
1467                 ret = sdw_bus_prep_clk_stop(&cdns->bus);
1468                 if (ret < 0 && ret != -ENODATA) {
1469                         dev_err(cdns->dev, "prepare clock stop failed %d\n", ret);
1470                         return ret;
1471                 }
1472         }
1473
1474         /*
1475          * Enter clock stop mode and only report errors if there are
1476          * Slave devices present (ALERT or ATTACHED)
1477          */
1478         ret = sdw_bus_clk_stop(&cdns->bus);
1479         if (ret < 0 && slave_present && ret != -ENODATA) {
1480                 dev_err(cdns->dev, "bus clock stop failed %d", ret);
1481                 return ret;
1482         }
1483
1484         ret = cdns_set_wait(cdns, CDNS_MCP_STAT,
1485                             CDNS_MCP_STAT_CLK_STOP,
1486                             CDNS_MCP_STAT_CLK_STOP);
1487         if (ret < 0)
1488                 dev_err(cdns->dev, "Clock stop failed %d\n", ret);
1489
1490         return ret;
1491 }
1492 EXPORT_SYMBOL(sdw_cdns_clock_stop);
1493
1494 /**
1495  * sdw_cdns_clock_restart: Cadence PM clock restart configuration routine
1496  *
1497  * @cdns: Cadence instance
1498  * @bus_reset: context may be lost while in low power modes and the bus
1499  * may require a Severe Reset and re-enumeration after a wake.
1500  */
1501 int sdw_cdns_clock_restart(struct sdw_cdns *cdns, bool bus_reset)
1502 {
1503         int ret;
1504
1505         /* unmask Slave interrupts that were masked when stopping the clock */
1506         cdns_enable_slave_interrupts(cdns, true);
1507
1508         ret = cdns_clear_bit(cdns, CDNS_MCP_CONTROL,
1509                              CDNS_MCP_CONTROL_CLK_STOP_CLR);
1510         if (ret < 0) {
1511                 dev_err(cdns->dev, "Couldn't exit from clock stop\n");
1512                 return ret;
1513         }
1514
1515         ret = cdns_set_wait(cdns, CDNS_MCP_STAT, CDNS_MCP_STAT_CLK_STOP, 0);
1516         if (ret < 0) {
1517                 dev_err(cdns->dev, "clock stop exit failed %d\n", ret);
1518                 return ret;
1519         }
1520
1521         cdns_updatel(cdns, CDNS_MCP_CONTROL,
1522                      CDNS_MCP_CONTROL_BLOCK_WAKEUP, 0);
1523
1524         /*
1525          * clear CMD_ACCEPT so that the command ignored
1526          * will be treated as a failure during a broadcast write
1527          */
1528         cdns_updatel(cdns, CDNS_MCP_CONTROL, CDNS_MCP_CONTROL_CMD_ACCEPT, 0);
1529
1530         if (!bus_reset) {
1531
1532                 /* enable bus operations with clock and data */
1533                 cdns_updatel(cdns, CDNS_MCP_CONFIG,
1534                              CDNS_MCP_CONFIG_OP,
1535                              CDNS_MCP_CONFIG_OP_NORMAL);
1536
1537                 ret = cdns_config_update(cdns);
1538                 if (ret < 0) {
1539                         dev_err(cdns->dev, "%s: config_update failed\n", __func__);
1540                         return ret;
1541                 }
1542
1543                 ret = sdw_bus_exit_clk_stop(&cdns->bus);
1544                 if (ret < 0)
1545                         dev_err(cdns->dev, "bus failed to exit clock stop %d\n", ret);
1546         }
1547
1548         return ret;
1549 }
1550 EXPORT_SYMBOL(sdw_cdns_clock_restart);
1551
1552 /**
1553  * sdw_cdns_probe() - Cadence probe routine
1554  * @cdns: Cadence instance
1555  */
1556 int sdw_cdns_probe(struct sdw_cdns *cdns)
1557 {
1558         init_completion(&cdns->tx_complete);
1559         cdns->bus.port_ops = &cdns_port_ops;
1560
1561         INIT_WORK(&cdns->work, cdns_update_slave_status_work);
1562         return 0;
1563 }
1564 EXPORT_SYMBOL(sdw_cdns_probe);
1565
1566 int cdns_set_sdw_stream(struct snd_soc_dai *dai,
1567                         void *stream, bool pcm, int direction)
1568 {
1569         struct sdw_cdns *cdns = snd_soc_dai_get_drvdata(dai);
1570         struct sdw_cdns_dma_data *dma;
1571
1572         if (stream) {
1573                 /* first paranoia check */
1574                 if (direction == SNDRV_PCM_STREAM_PLAYBACK)
1575                         dma = dai->playback_dma_data;
1576                 else
1577                         dma = dai->capture_dma_data;
1578
1579                 if (dma) {
1580                         dev_err(dai->dev,
1581                                 "dma_data already allocated for dai %s\n",
1582                                 dai->name);
1583                         return -EINVAL;
1584                 }
1585
1586                 /* allocate and set dma info */
1587                 dma = kzalloc(sizeof(*dma), GFP_KERNEL);
1588                 if (!dma)
1589                         return -ENOMEM;
1590
1591                 if (pcm)
1592                         dma->stream_type = SDW_STREAM_PCM;
1593                 else
1594                         dma->stream_type = SDW_STREAM_PDM;
1595
1596                 dma->bus = &cdns->bus;
1597                 dma->link_id = cdns->instance;
1598
1599                 dma->stream = stream;
1600
1601                 if (direction == SNDRV_PCM_STREAM_PLAYBACK)
1602                         dai->playback_dma_data = dma;
1603                 else
1604                         dai->capture_dma_data = dma;
1605         } else {
1606                 /* for NULL stream we release allocated dma_data */
1607                 if (direction == SNDRV_PCM_STREAM_PLAYBACK) {
1608                         kfree(dai->playback_dma_data);
1609                         dai->playback_dma_data = NULL;
1610                 } else {
1611                         kfree(dai->capture_dma_data);
1612                         dai->capture_dma_data = NULL;
1613                 }
1614         }
1615         return 0;
1616 }
1617 EXPORT_SYMBOL(cdns_set_sdw_stream);
1618
1619 /**
1620  * cdns_find_pdi() - Find a free PDI
1621  *
1622  * @cdns: Cadence instance
1623  * @offset: Starting offset
1624  * @num: Number of PDIs
1625  * @pdi: PDI instances
1626  * @dai_id: DAI id
1627  *
1628  * Find a PDI for a given PDI array. The PDI num and dai_id are
1629  * expected to match, return NULL otherwise.
1630  */
1631 static struct sdw_cdns_pdi *cdns_find_pdi(struct sdw_cdns *cdns,
1632                                           unsigned int offset,
1633                                           unsigned int num,
1634                                           struct sdw_cdns_pdi *pdi,
1635                                           int dai_id)
1636 {
1637         int i;
1638
1639         for (i = offset; i < offset + num; i++)
1640                 if (pdi[i].num == dai_id)
1641                         return &pdi[i];
1642
1643         return NULL;
1644 }
1645
1646 /**
1647  * sdw_cdns_config_stream: Configure a stream
1648  *
1649  * @cdns: Cadence instance
1650  * @ch: Channel count
1651  * @dir: Data direction
1652  * @pdi: PDI to be used
1653  */
1654 void sdw_cdns_config_stream(struct sdw_cdns *cdns,
1655                             u32 ch, u32 dir, struct sdw_cdns_pdi *pdi)
1656 {
1657         u32 offset, val = 0;
1658
1659         if (dir == SDW_DATA_DIR_RX) {
1660                 val = CDNS_PORTCTRL_DIRN;
1661
1662                 if (cdns->bus.params.m_data_mode != SDW_PORT_DATA_MODE_NORMAL)
1663                         val |= CDNS_PORTCTRL_TEST_FAILED;
1664         }
1665         offset = CDNS_PORTCTRL + pdi->num * CDNS_PORT_OFFSET;
1666         cdns_updatel(cdns, offset,
1667                      CDNS_PORTCTRL_DIRN | CDNS_PORTCTRL_TEST_FAILED,
1668                      val);
1669
1670         val = pdi->num;
1671         val |= CDNS_PDI_CONFIG_SOFT_RESET;
1672         val |= FIELD_PREP(CDNS_PDI_CONFIG_CHANNEL, (1 << ch) - 1);
1673         cdns_writel(cdns, CDNS_PDI_CONFIG(pdi->num), val);
1674 }
1675 EXPORT_SYMBOL(sdw_cdns_config_stream);
1676
1677 /**
1678  * sdw_cdns_alloc_pdi() - Allocate a PDI
1679  *
1680  * @cdns: Cadence instance
1681  * @stream: Stream to be allocated
1682  * @ch: Channel count
1683  * @dir: Data direction
1684  * @dai_id: DAI id
1685  */
1686 struct sdw_cdns_pdi *sdw_cdns_alloc_pdi(struct sdw_cdns *cdns,
1687                                         struct sdw_cdns_streams *stream,
1688                                         u32 ch, u32 dir, int dai_id)
1689 {
1690         struct sdw_cdns_pdi *pdi = NULL;
1691
1692         if (dir == SDW_DATA_DIR_RX)
1693                 pdi = cdns_find_pdi(cdns, 0, stream->num_in, stream->in,
1694                                     dai_id);
1695         else
1696                 pdi = cdns_find_pdi(cdns, 0, stream->num_out, stream->out,
1697                                     dai_id);
1698
1699         /* check if we found a PDI, else find in bi-directional */
1700         if (!pdi)
1701                 pdi = cdns_find_pdi(cdns, 0, stream->num_bd, stream->bd,
1702                                     dai_id);
1703
1704         if (pdi) {
1705                 pdi->l_ch_num = 0;
1706                 pdi->h_ch_num = ch - 1;
1707                 pdi->dir = dir;
1708                 pdi->ch_count = ch;
1709         }
1710
1711         return pdi;
1712 }
1713 EXPORT_SYMBOL(sdw_cdns_alloc_pdi);
1714
1715 MODULE_LICENSE("Dual BSD/GPL");
1716 MODULE_DESCRIPTION("Cadence Soundwire Library");