blob: 6ef2f759310d41f00a5e27a7db2d456fc3f48b44 [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>
16#include <linux/soundwire/sdw_registers.h>
17#include <linux/soundwire/sdw.h>
Vinod Koul5d6b3c82018-04-26 18:38:53 +053018#include <sound/pcm_params.h>
19#include <sound/soc.h>
Vinod Koul2f52a512017-12-14 11:19:41 +053020#include "bus.h"
21#include "cadence_master.h"
22
Pierre-Louis Bossart04592dc2019-08-05 19:55:21 -050023static int interrupt_mask;
24module_param_named(cnds_mcp_int_mask, interrupt_mask, int, 0444);
25MODULE_PARM_DESC(cdns_mcp_int_mask, "Cadence MCP IntMask");
26
Vinod Koul2f52a512017-12-14 11:19:41 +053027#define CDNS_MCP_CONFIG 0x0
28
29#define CDNS_MCP_CONFIG_MCMD_RETRY GENMASK(27, 24)
30#define CDNS_MCP_CONFIG_MPREQ_DELAY GENMASK(20, 16)
31#define CDNS_MCP_CONFIG_MMASTER BIT(7)
32#define CDNS_MCP_CONFIG_BUS_REL BIT(6)
33#define CDNS_MCP_CONFIG_SNIFFER BIT(5)
34#define CDNS_MCP_CONFIG_SSPMOD BIT(4)
35#define CDNS_MCP_CONFIG_CMD BIT(3)
36#define CDNS_MCP_CONFIG_OP GENMASK(2, 0)
37#define CDNS_MCP_CONFIG_OP_NORMAL 0
38
39#define CDNS_MCP_CONTROL 0x4
40
41#define CDNS_MCP_CONTROL_RST_DELAY GENMASK(10, 8)
42#define CDNS_MCP_CONTROL_CMD_RST BIT(7)
43#define CDNS_MCP_CONTROL_SOFT_RST BIT(6)
44#define CDNS_MCP_CONTROL_SW_RST BIT(5)
45#define CDNS_MCP_CONTROL_HW_RST BIT(4)
46#define CDNS_MCP_CONTROL_CLK_PAUSE BIT(3)
47#define CDNS_MCP_CONTROL_CLK_STOP_CLR BIT(2)
48#define CDNS_MCP_CONTROL_CMD_ACCEPT BIT(1)
49#define CDNS_MCP_CONTROL_BLOCK_WAKEUP BIT(0)
50
Vinod Koul2f52a512017-12-14 11:19:41 +053051#define CDNS_MCP_CMDCTRL 0x8
52#define CDNS_MCP_SSPSTAT 0xC
53#define CDNS_MCP_FRAME_SHAPE 0x10
54#define CDNS_MCP_FRAME_SHAPE_INIT 0x14
Pierre-Louis Bossart05be59a2019-08-05 19:55:11 -050055#define CDNS_MCP_FRAME_SHAPE_COL_MASK GENMASK(2, 0)
56#define CDNS_MCP_FRAME_SHAPE_ROW_OFFSET 3
Vinod Koul2f52a512017-12-14 11:19:41 +053057
58#define CDNS_MCP_CONFIG_UPDATE 0x18
59#define CDNS_MCP_CONFIG_UPDATE_BIT BIT(0)
60
61#define CDNS_MCP_PHYCTRL 0x1C
62#define CDNS_MCP_SSP_CTRL0 0x20
63#define CDNS_MCP_SSP_CTRL1 0x28
64#define CDNS_MCP_CLK_CTRL0 0x30
65#define CDNS_MCP_CLK_CTRL1 0x38
Rander Wanga50954e2019-08-05 19:55:18 -050066#define CDNS_MCP_CLK_MCLKD_MASK GENMASK(7, 0)
Vinod Koul2f52a512017-12-14 11:19:41 +053067
68#define CDNS_MCP_STAT 0x40
69
70#define CDNS_MCP_STAT_ACTIVE_BANK BIT(20)
71#define CDNS_MCP_STAT_CLK_STOP BIT(16)
72
73#define CDNS_MCP_INTSTAT 0x44
74#define CDNS_MCP_INTMASK 0x48
75
76#define CDNS_MCP_INT_IRQ BIT(31)
Pierre-Louis Bossarta2cff9e2020-01-10 15:57:26 -060077#define CDNS_MCP_INT_RESERVED1 GENMASK(30, 17)
Vinod Koul2f52a512017-12-14 11:19:41 +053078#define CDNS_MCP_INT_WAKEUP BIT(16)
79#define CDNS_MCP_INT_SLAVE_RSVD BIT(15)
80#define CDNS_MCP_INT_SLAVE_ALERT BIT(14)
81#define CDNS_MCP_INT_SLAVE_ATTACH BIT(13)
82#define CDNS_MCP_INT_SLAVE_NATTACH BIT(12)
83#define CDNS_MCP_INT_SLAVE_MASK GENMASK(15, 12)
84#define CDNS_MCP_INT_DPINT BIT(11)
85#define CDNS_MCP_INT_CTRL_CLASH BIT(10)
86#define CDNS_MCP_INT_DATA_CLASH BIT(9)
Pierre-Louis Bossart9b5884a2019-08-05 19:55:08 -050087#define CDNS_MCP_INT_PARITY BIT(8)
Vinod Koul2f52a512017-12-14 11:19:41 +053088#define CDNS_MCP_INT_CMD_ERR BIT(7)
Pierre-Louis Bossarta2cff9e2020-01-10 15:57:26 -060089#define CDNS_MCP_INT_RESERVED2 GENMASK(6, 4)
Pierre-Louis Bossart9b5884a2019-08-05 19:55:08 -050090#define CDNS_MCP_INT_RX_NE BIT(3)
Vinod Koul2f52a512017-12-14 11:19:41 +053091#define CDNS_MCP_INT_RX_WL BIT(2)
92#define CDNS_MCP_INT_TXE BIT(1)
Pierre-Louis Bossart9b5884a2019-08-05 19:55:08 -050093#define CDNS_MCP_INT_TXF BIT(0)
Pierre-Louis Bossarta2cff9e2020-01-10 15:57:26 -060094#define CDNS_MCP_INT_RESERVED (CDNS_MCP_INT_RESERVED1 | CDNS_MCP_INT_RESERVED2)
Vinod Koul2f52a512017-12-14 11:19:41 +053095
96#define CDNS_MCP_INTSET 0x4C
97
Pierre-Louis Bossartb07dd9b2019-07-25 18:40:05 -050098#define CDNS_MCP_SLAVE_STAT 0x50
99#define CDNS_MCP_SLAVE_STAT_MASK GENMASK(1, 0)
Vinod Koul2f52a512017-12-14 11:19:41 +0530100
101#define CDNS_MCP_SLAVE_INTSTAT0 0x54
102#define CDNS_MCP_SLAVE_INTSTAT1 0x58
103#define CDNS_MCP_SLAVE_INTSTAT_NPRESENT BIT(0)
104#define CDNS_MCP_SLAVE_INTSTAT_ATTACHED BIT(1)
105#define CDNS_MCP_SLAVE_INTSTAT_ALERT BIT(2)
106#define CDNS_MCP_SLAVE_INTSTAT_RESERVED BIT(3)
107#define CDNS_MCP_SLAVE_STATUS_BITS GENMASK(3, 0)
108#define CDNS_MCP_SLAVE_STATUS_NUM 4
109
110#define CDNS_MCP_SLAVE_INTMASK0 0x5C
111#define CDNS_MCP_SLAVE_INTMASK1 0x60
112
Pierre-Louis Bossart664b1652019-07-25 18:40:06 -0500113#define CDNS_MCP_SLAVE_INTMASK0_MASK GENMASK(31, 0)
114#define CDNS_MCP_SLAVE_INTMASK1_MASK GENMASK(15, 0)
Vinod Koul2f52a512017-12-14 11:19:41 +0530115
116#define CDNS_MCP_PORT_INTSTAT 0x64
117#define CDNS_MCP_PDI_STAT 0x6C
118
119#define CDNS_MCP_FIFOLEVEL 0x78
120#define CDNS_MCP_FIFOSTAT 0x7C
121#define CDNS_MCP_RX_FIFO_AVAIL GENMASK(5, 0)
122
123#define CDNS_MCP_CMD_BASE 0x80
124#define CDNS_MCP_RESP_BASE 0x80
125#define CDNS_MCP_CMD_LEN 0x20
126#define CDNS_MCP_CMD_WORD_LEN 0x4
127
128#define CDNS_MCP_CMD_SSP_TAG BIT(31)
129#define CDNS_MCP_CMD_COMMAND GENMASK(30, 28)
130#define CDNS_MCP_CMD_DEV_ADDR GENMASK(27, 24)
131#define CDNS_MCP_CMD_REG_ADDR_H GENMASK(23, 16)
132#define CDNS_MCP_CMD_REG_ADDR_L GENMASK(15, 8)
133#define CDNS_MCP_CMD_REG_DATA GENMASK(7, 0)
134
135#define CDNS_MCP_CMD_READ 2
136#define CDNS_MCP_CMD_WRITE 3
137
138#define CDNS_MCP_RESP_RDATA GENMASK(15, 8)
139#define CDNS_MCP_RESP_ACK BIT(0)
140#define CDNS_MCP_RESP_NACK BIT(1)
141
142#define CDNS_DP_SIZE 128
143
144#define CDNS_DPN_B0_CONFIG(n) (0x100 + CDNS_DP_SIZE * (n))
145#define CDNS_DPN_B0_CH_EN(n) (0x104 + CDNS_DP_SIZE * (n))
146#define CDNS_DPN_B0_SAMPLE_CTRL(n) (0x108 + CDNS_DP_SIZE * (n))
147#define CDNS_DPN_B0_OFFSET_CTRL(n) (0x10C + CDNS_DP_SIZE * (n))
148#define CDNS_DPN_B0_HCTRL(n) (0x110 + CDNS_DP_SIZE * (n))
149#define CDNS_DPN_B0_ASYNC_CTRL(n) (0x114 + CDNS_DP_SIZE * (n))
150
151#define CDNS_DPN_B1_CONFIG(n) (0x118 + CDNS_DP_SIZE * (n))
152#define CDNS_DPN_B1_CH_EN(n) (0x11C + CDNS_DP_SIZE * (n))
153#define CDNS_DPN_B1_SAMPLE_CTRL(n) (0x120 + CDNS_DP_SIZE * (n))
154#define CDNS_DPN_B1_OFFSET_CTRL(n) (0x124 + CDNS_DP_SIZE * (n))
155#define CDNS_DPN_B1_HCTRL(n) (0x128 + CDNS_DP_SIZE * (n))
156#define CDNS_DPN_B1_ASYNC_CTRL(n) (0x12C + CDNS_DP_SIZE * (n))
157
158#define CDNS_DPN_CONFIG_BPM BIT(18)
159#define CDNS_DPN_CONFIG_BGC GENMASK(17, 16)
160#define CDNS_DPN_CONFIG_WL GENMASK(12, 8)
161#define CDNS_DPN_CONFIG_PORT_DAT GENMASK(3, 2)
162#define CDNS_DPN_CONFIG_PORT_FLOW GENMASK(1, 0)
163
164#define CDNS_DPN_SAMPLE_CTRL_SI GENMASK(15, 0)
165
166#define CDNS_DPN_OFFSET_CTRL_1 GENMASK(7, 0)
167#define CDNS_DPN_OFFSET_CTRL_2 GENMASK(15, 8)
168
169#define CDNS_DPN_HCTRL_HSTOP GENMASK(3, 0)
170#define CDNS_DPN_HCTRL_HSTART GENMASK(7, 4)
171#define CDNS_DPN_HCTRL_LCTRL GENMASK(10, 8)
172
173#define CDNS_PORTCTRL 0x130
174#define CDNS_PORTCTRL_DIRN BIT(7)
175#define CDNS_PORTCTRL_BANK_INVERT BIT(8)
176
177#define CDNS_PORT_OFFSET 0x80
178
179#define CDNS_PDI_CONFIG(n) (0x1100 + (n) * 16)
180
181#define CDNS_PDI_CONFIG_SOFT_RESET BIT(24)
182#define CDNS_PDI_CONFIG_CHANNEL GENMASK(15, 8)
183#define CDNS_PDI_CONFIG_PORT GENMASK(4, 0)
184
185/* Driver defaults */
Vinod Koul2f52a512017-12-14 11:19:41 +0530186#define CDNS_DEFAULT_SSP_INTERVAL 0x18
187#define CDNS_TX_TIMEOUT 2000
188
Vinod Koul2f52a512017-12-14 11:19:41 +0530189#define CDNS_SCP_RX_FIFOLEVEL 0x2
190
191/*
192 * register accessor helpers
193 */
194static inline u32 cdns_readl(struct sdw_cdns *cdns, int offset)
195{
196 return readl(cdns->registers + offset);
197}
198
199static inline void cdns_writel(struct sdw_cdns *cdns, int offset, u32 value)
200{
201 writel(value, cdns->registers + offset);
202}
203
204static inline void cdns_updatel(struct sdw_cdns *cdns,
205 int offset, u32 mask, u32 val)
206{
207 u32 tmp;
208
209 tmp = cdns_readl(cdns, offset);
210 tmp = (tmp & ~mask) | val;
211 cdns_writel(cdns, offset, tmp);
212}
213
214static int cdns_clear_bit(struct sdw_cdns *cdns, int offset, u32 value)
215{
216 int timeout = 10;
217 u32 reg_read;
218
219 writel(value, cdns->registers + offset);
220
221 /* Wait for bit to be self cleared */
222 do {
223 reg_read = readl(cdns->registers + offset);
224 if ((reg_read & value) == 0)
225 return 0;
226
227 timeout--;
228 udelay(50);
229 } while (timeout != 0);
230
231 return -EAGAIN;
232}
233
234/*
Pierre-Louis Bossart49ea07d2019-10-22 18:54:44 -0500235 * all changes to the MCP_CONFIG, MCP_CONTROL, MCP_CMDCTRL and MCP_PHYCTRL
236 * need to be confirmed with a write to MCP_CONFIG_UPDATE
237 */
238static int cdns_update_config(struct sdw_cdns *cdns)
239{
240 int ret;
241
242 ret = cdns_clear_bit(cdns, CDNS_MCP_CONFIG_UPDATE,
243 CDNS_MCP_CONFIG_UPDATE_BIT);
244 if (ret < 0)
245 dev_err(cdns->dev, "Config update timedout\n");
246
247 return ret;
248}
249
250/*
Pierre-Louis Bossartaa850662019-08-21 13:58:19 -0500251 * debugfs
252 */
253#ifdef CONFIG_DEBUG_FS
254
255#define RD_BUF (2 * PAGE_SIZE)
256
257static ssize_t cdns_sprintf(struct sdw_cdns *cdns,
258 char *buf, size_t pos, unsigned int reg)
259{
260 return scnprintf(buf + pos, RD_BUF - pos,
261 "%4x\t%8x\n", reg, cdns_readl(cdns, reg));
262}
263
264static int cdns_reg_show(struct seq_file *s, void *data)
265{
266 struct sdw_cdns *cdns = s->private;
267 char *buf;
268 ssize_t ret;
269 int num_ports;
270 int i, j;
271
272 buf = kzalloc(RD_BUF, GFP_KERNEL);
273 if (!buf)
274 return -ENOMEM;
275
276 ret = scnprintf(buf, RD_BUF, "Register Value\n");
277 ret += scnprintf(buf + ret, RD_BUF - ret, "\nMCP Registers\n");
278 /* 8 MCP registers */
279 for (i = CDNS_MCP_CONFIG; i <= CDNS_MCP_PHYCTRL; i += sizeof(u32))
280 ret += cdns_sprintf(cdns, buf, ret, i);
281
282 ret += scnprintf(buf + ret, RD_BUF - ret,
283 "\nStatus & Intr Registers\n");
284 /* 13 Status & Intr registers (offsets 0x70 and 0x74 not defined) */
285 for (i = CDNS_MCP_STAT; i <= CDNS_MCP_FIFOSTAT; i += sizeof(u32))
286 ret += cdns_sprintf(cdns, buf, ret, i);
287
288 ret += scnprintf(buf + ret, RD_BUF - ret,
289 "\nSSP & Clk ctrl Registers\n");
290 ret += cdns_sprintf(cdns, buf, ret, CDNS_MCP_SSP_CTRL0);
291 ret += cdns_sprintf(cdns, buf, ret, CDNS_MCP_SSP_CTRL1);
292 ret += cdns_sprintf(cdns, buf, ret, CDNS_MCP_CLK_CTRL0);
293 ret += cdns_sprintf(cdns, buf, ret, CDNS_MCP_CLK_CTRL1);
294
295 ret += scnprintf(buf + ret, RD_BUF - ret,
296 "\nDPn B0 Registers\n");
297
Pierre-Louis Bossart807c15b2019-09-16 14:23:47 -0500298 num_ports = cdns->num_ports;
Pierre-Louis Bossartaa850662019-08-21 13:58:19 -0500299
300 for (i = 0; i < num_ports; i++) {
301 ret += scnprintf(buf + ret, RD_BUF - ret,
302 "\nDP-%d\n", i);
303 for (j = CDNS_DPN_B0_CONFIG(i);
304 j < CDNS_DPN_B0_ASYNC_CTRL(i); j += sizeof(u32))
305 ret += cdns_sprintf(cdns, buf, ret, j);
306 }
307
308 ret += scnprintf(buf + ret, RD_BUF - ret,
309 "\nDPn B1 Registers\n");
310 for (i = 0; i < num_ports; i++) {
311 ret += scnprintf(buf + ret, RD_BUF - ret,
312 "\nDP-%d\n", i);
313
314 for (j = CDNS_DPN_B1_CONFIG(i);
315 j < CDNS_DPN_B1_ASYNC_CTRL(i); j += sizeof(u32))
316 ret += cdns_sprintf(cdns, buf, ret, j);
317 }
318
319 ret += scnprintf(buf + ret, RD_BUF - ret,
320 "\nDPn Control Registers\n");
321 for (i = 0; i < num_ports; i++)
322 ret += cdns_sprintf(cdns, buf, ret,
323 CDNS_PORTCTRL + i * CDNS_PORT_OFFSET);
324
325 ret += scnprintf(buf + ret, RD_BUF - ret,
326 "\nPDIn Config Registers\n");
327
328 /* number of PDI and ports is interchangeable */
329 for (i = 0; i < num_ports; i++)
330 ret += cdns_sprintf(cdns, buf, ret, CDNS_PDI_CONFIG(i));
331
332 seq_printf(s, "%s", buf);
333 kfree(buf);
334
335 return 0;
336}
337DEFINE_SHOW_ATTRIBUTE(cdns_reg);
338
Pierre-Louis Bossart675d4c92019-10-22 18:54:45 -0500339static int cdns_hw_reset(void *data, u64 value)
340{
341 struct sdw_cdns *cdns = data;
342 int ret;
343
344 if (value != 1)
345 return -EINVAL;
346
347 /* Userspace changed the hardware state behind the kernel's back */
348 add_taint(TAINT_USER, LOCKDEP_STILL_OK);
349
350 ret = sdw_cdns_exit_reset(cdns);
351
352 dev_dbg(cdns->dev, "link hw_reset done: %d\n", ret);
353
354 return ret;
355}
356
357DEFINE_DEBUGFS_ATTRIBUTE(cdns_hw_reset_fops, NULL, cdns_hw_reset, "%llu\n");
358
Pierre-Louis Bossartaa850662019-08-21 13:58:19 -0500359/**
360 * sdw_cdns_debugfs_init() - Cadence debugfs init
361 * @cdns: Cadence instance
362 * @root: debugfs root
363 */
364void sdw_cdns_debugfs_init(struct sdw_cdns *cdns, struct dentry *root)
365{
366 debugfs_create_file("cdns-registers", 0400, root, cdns, &cdns_reg_fops);
Pierre-Louis Bossart675d4c92019-10-22 18:54:45 -0500367
368 debugfs_create_file("cdns-hw-reset", 0200, root, cdns,
369 &cdns_hw_reset_fops);
Pierre-Louis Bossartaa850662019-08-21 13:58:19 -0500370}
371EXPORT_SYMBOL_GPL(sdw_cdns_debugfs_init);
372
373#endif /* CONFIG_DEBUG_FS */
374
375/*
Sanyog Kale956baa12017-12-14 11:19:42 +0530376 * IO Calls
377 */
Pierre-Louis Bossartbbb63812019-05-01 10:57:41 -0500378static enum sdw_command_response
379cdns_fill_msg_resp(struct sdw_cdns *cdns,
380 struct sdw_msg *msg, int count, int offset)
Sanyog Kale956baa12017-12-14 11:19:42 +0530381{
382 int nack = 0, no_ack = 0;
383 int i;
384
385 /* check message response */
386 for (i = 0; i < count; i++) {
387 if (!(cdns->response_buf[i] & CDNS_MCP_RESP_ACK)) {
388 no_ack = 1;
Pierre-Louis Bossarteb7df4c2019-05-22 14:47:26 -0500389 dev_dbg_ratelimited(cdns->dev, "Msg Ack not received\n");
Sanyog Kale956baa12017-12-14 11:19:42 +0530390 if (cdns->response_buf[i] & CDNS_MCP_RESP_NACK) {
391 nack = 1;
Pierre-Louis Bossarteb7df4c2019-05-22 14:47:26 -0500392 dev_err_ratelimited(cdns->dev, "Msg NACK received\n");
Sanyog Kale956baa12017-12-14 11:19:42 +0530393 }
394 }
395 }
396
397 if (nack) {
Pierre-Louis Bossarteb7df4c2019-05-22 14:47:26 -0500398 dev_err_ratelimited(cdns->dev, "Msg NACKed for Slave %d\n", msg->dev_num);
Sanyog Kale956baa12017-12-14 11:19:42 +0530399 return SDW_CMD_FAIL;
400 } else if (no_ack) {
Pierre-Louis Bossarteb7df4c2019-05-22 14:47:26 -0500401 dev_dbg_ratelimited(cdns->dev, "Msg ignored for Slave %d\n", msg->dev_num);
Sanyog Kale956baa12017-12-14 11:19:42 +0530402 return SDW_CMD_IGNORED;
403 }
404
405 /* fill response */
406 for (i = 0; i < count; i++)
407 msg->buf[i + offset] = cdns->response_buf[i] >>
408 SDW_REG_SHIFT(CDNS_MCP_RESP_RDATA);
409
410 return SDW_CMD_OK;
411}
412
413static enum sdw_command_response
414_cdns_xfer_msg(struct sdw_cdns *cdns, struct sdw_msg *msg, int cmd,
Pierre-Louis Bossartbbb63812019-05-01 10:57:41 -0500415 int offset, int count, bool defer)
Sanyog Kale956baa12017-12-14 11:19:42 +0530416{
417 unsigned long time;
418 u32 base, i, data;
419 u16 addr;
420
421 /* Program the watermark level for RX FIFO */
422 if (cdns->msg_count != count) {
423 cdns_writel(cdns, CDNS_MCP_FIFOLEVEL, count);
424 cdns->msg_count = count;
425 }
426
427 base = CDNS_MCP_CMD_BASE;
428 addr = msg->addr;
429
430 for (i = 0; i < count; i++) {
431 data = msg->dev_num << SDW_REG_SHIFT(CDNS_MCP_CMD_DEV_ADDR);
432 data |= cmd << SDW_REG_SHIFT(CDNS_MCP_CMD_COMMAND);
433 data |= addr++ << SDW_REG_SHIFT(CDNS_MCP_CMD_REG_ADDR_L);
434
435 if (msg->flags == SDW_MSG_FLAG_WRITE)
436 data |= msg->buf[i + offset];
437
438 data |= msg->ssp_sync << SDW_REG_SHIFT(CDNS_MCP_CMD_SSP_TAG);
439 cdns_writel(cdns, base, data);
440 base += CDNS_MCP_CMD_WORD_LEN;
441 }
442
443 if (defer)
444 return SDW_CMD_OK;
445
446 /* wait for timeout or response */
447 time = wait_for_completion_timeout(&cdns->tx_complete,
Pierre-Louis Bossartbbb63812019-05-01 10:57:41 -0500448 msecs_to_jiffies(CDNS_TX_TIMEOUT));
Sanyog Kale956baa12017-12-14 11:19:42 +0530449 if (!time) {
Pierre-Louis Bossart53ee9572020-01-10 15:57:28 -0600450 dev_err(cdns->dev, "IO transfer timed out, cmd %d device %d addr %x len %d\n",
451 cmd, msg->dev_num, msg->addr, msg->len);
Sanyog Kale956baa12017-12-14 11:19:42 +0530452 msg->len = 0;
453 return SDW_CMD_TIMEOUT;
454 }
455
456 return cdns_fill_msg_resp(cdns, msg, count, offset);
457}
458
Pierre-Louis Bossartbbb63812019-05-01 10:57:41 -0500459static enum sdw_command_response
460cdns_program_scp_addr(struct sdw_cdns *cdns, struct sdw_msg *msg)
Sanyog Kale956baa12017-12-14 11:19:42 +0530461{
462 int nack = 0, no_ack = 0;
463 unsigned long time;
464 u32 data[2], base;
465 int i;
466
467 /* Program the watermark level for RX FIFO */
468 if (cdns->msg_count != CDNS_SCP_RX_FIFOLEVEL) {
469 cdns_writel(cdns, CDNS_MCP_FIFOLEVEL, CDNS_SCP_RX_FIFOLEVEL);
470 cdns->msg_count = CDNS_SCP_RX_FIFOLEVEL;
471 }
472
473 data[0] = msg->dev_num << SDW_REG_SHIFT(CDNS_MCP_CMD_DEV_ADDR);
474 data[0] |= 0x3 << SDW_REG_SHIFT(CDNS_MCP_CMD_COMMAND);
475 data[1] = data[0];
476
477 data[0] |= SDW_SCP_ADDRPAGE1 << SDW_REG_SHIFT(CDNS_MCP_CMD_REG_ADDR_L);
478 data[1] |= SDW_SCP_ADDRPAGE2 << SDW_REG_SHIFT(CDNS_MCP_CMD_REG_ADDR_L);
479
480 data[0] |= msg->addr_page1;
481 data[1] |= msg->addr_page2;
482
483 base = CDNS_MCP_CMD_BASE;
484 cdns_writel(cdns, base, data[0]);
485 base += CDNS_MCP_CMD_WORD_LEN;
486 cdns_writel(cdns, base, data[1]);
487
488 time = wait_for_completion_timeout(&cdns->tx_complete,
Pierre-Louis Bossartbbb63812019-05-01 10:57:41 -0500489 msecs_to_jiffies(CDNS_TX_TIMEOUT));
Sanyog Kale956baa12017-12-14 11:19:42 +0530490 if (!time) {
491 dev_err(cdns->dev, "SCP Msg trf timed out\n");
492 msg->len = 0;
493 return SDW_CMD_TIMEOUT;
494 }
495
496 /* check response the writes */
497 for (i = 0; i < 2; i++) {
498 if (!(cdns->response_buf[i] & CDNS_MCP_RESP_ACK)) {
499 no_ack = 1;
Pierre-Louis Bossart17ed5be2019-05-01 10:57:45 -0500500 dev_err(cdns->dev, "Program SCP Ack not received\n");
Sanyog Kale956baa12017-12-14 11:19:42 +0530501 if (cdns->response_buf[i] & CDNS_MCP_RESP_NACK) {
502 nack = 1;
Pierre-Louis Bossart17ed5be2019-05-01 10:57:45 -0500503 dev_err(cdns->dev, "Program SCP NACK received\n");
Sanyog Kale956baa12017-12-14 11:19:42 +0530504 }
505 }
506 }
507
508 /* For NACK, NO ack, don't return err if we are in Broadcast mode */
509 if (nack) {
Pierre-Louis Bossarteb7df4c2019-05-22 14:47:26 -0500510 dev_err_ratelimited(cdns->dev,
511 "SCP_addrpage NACKed for Slave %d\n", msg->dev_num);
Sanyog Kale956baa12017-12-14 11:19:42 +0530512 return SDW_CMD_FAIL;
513 } else if (no_ack) {
Pierre-Louis Bossarteb7df4c2019-05-22 14:47:26 -0500514 dev_dbg_ratelimited(cdns->dev,
515 "SCP_addrpage ignored for Slave %d\n", msg->dev_num);
Sanyog Kale956baa12017-12-14 11:19:42 +0530516 return SDW_CMD_IGNORED;
517 }
518
519 return SDW_CMD_OK;
520}
521
522static int cdns_prep_msg(struct sdw_cdns *cdns, struct sdw_msg *msg, int *cmd)
523{
524 int ret;
525
526 if (msg->page) {
527 ret = cdns_program_scp_addr(cdns, msg);
528 if (ret) {
529 msg->len = 0;
530 return ret;
531 }
532 }
533
534 switch (msg->flags) {
535 case SDW_MSG_FLAG_READ:
536 *cmd = CDNS_MCP_CMD_READ;
537 break;
538
539 case SDW_MSG_FLAG_WRITE:
540 *cmd = CDNS_MCP_CMD_WRITE;
541 break;
542
543 default:
544 dev_err(cdns->dev, "Invalid msg cmd: %d\n", msg->flags);
545 return -EINVAL;
546 }
547
548 return 0;
549}
550
Shreyas NCc91605f2018-04-26 18:38:43 +0530551enum sdw_command_response
Sanyog Kale956baa12017-12-14 11:19:42 +0530552cdns_xfer_msg(struct sdw_bus *bus, struct sdw_msg *msg)
553{
554 struct sdw_cdns *cdns = bus_to_cdns(bus);
555 int cmd = 0, ret, i;
556
557 ret = cdns_prep_msg(cdns, msg, &cmd);
558 if (ret)
559 return SDW_CMD_FAIL_OTHER;
560
561 for (i = 0; i < msg->len / CDNS_MCP_CMD_LEN; i++) {
562 ret = _cdns_xfer_msg(cdns, msg, cmd, i * CDNS_MCP_CMD_LEN,
Pierre-Louis Bossartbbb63812019-05-01 10:57:41 -0500563 CDNS_MCP_CMD_LEN, false);
Sanyog Kale956baa12017-12-14 11:19:42 +0530564 if (ret < 0)
565 goto exit;
566 }
567
568 if (!(msg->len % CDNS_MCP_CMD_LEN))
569 goto exit;
570
571 ret = _cdns_xfer_msg(cdns, msg, cmd, i * CDNS_MCP_CMD_LEN,
Pierre-Louis Bossartbbb63812019-05-01 10:57:41 -0500572 msg->len % CDNS_MCP_CMD_LEN, false);
Sanyog Kale956baa12017-12-14 11:19:42 +0530573
574exit:
575 return ret;
576}
Shreyas NCc91605f2018-04-26 18:38:43 +0530577EXPORT_SYMBOL(cdns_xfer_msg);
Sanyog Kale956baa12017-12-14 11:19:42 +0530578
Shreyas NCc91605f2018-04-26 18:38:43 +0530579enum sdw_command_response
Sanyog Kale956baa12017-12-14 11:19:42 +0530580cdns_xfer_msg_defer(struct sdw_bus *bus,
Pierre-Louis Bossartbbb63812019-05-01 10:57:41 -0500581 struct sdw_msg *msg, struct sdw_defer *defer)
Sanyog Kale956baa12017-12-14 11:19:42 +0530582{
583 struct sdw_cdns *cdns = bus_to_cdns(bus);
584 int cmd = 0, ret;
585
586 /* for defer only 1 message is supported */
587 if (msg->len > 1)
588 return -ENOTSUPP;
589
590 ret = cdns_prep_msg(cdns, msg, &cmd);
591 if (ret)
592 return SDW_CMD_FAIL_OTHER;
593
594 cdns->defer = defer;
595 cdns->defer->length = msg->len;
596
597 return _cdns_xfer_msg(cdns, msg, cmd, 0, msg->len, true);
598}
Shreyas NCc91605f2018-04-26 18:38:43 +0530599EXPORT_SYMBOL(cdns_xfer_msg_defer);
Sanyog Kale956baa12017-12-14 11:19:42 +0530600
Shreyas NCc91605f2018-04-26 18:38:43 +0530601enum sdw_command_response
Sanyog Kale956baa12017-12-14 11:19:42 +0530602cdns_reset_page_addr(struct sdw_bus *bus, unsigned int dev_num)
603{
604 struct sdw_cdns *cdns = bus_to_cdns(bus);
605 struct sdw_msg msg;
606
607 /* Create dummy message with valid device number */
608 memset(&msg, 0, sizeof(msg));
609 msg.dev_num = dev_num;
610
611 return cdns_program_scp_addr(cdns, &msg);
612}
Shreyas NCc91605f2018-04-26 18:38:43 +0530613EXPORT_SYMBOL(cdns_reset_page_addr);
Sanyog Kale956baa12017-12-14 11:19:42 +0530614
615/*
Vinod Koul2f52a512017-12-14 11:19:41 +0530616 * IRQ handling
617 */
618
Sanyog Kale956baa12017-12-14 11:19:42 +0530619static void cdns_read_response(struct sdw_cdns *cdns)
620{
621 u32 num_resp, cmd_base;
622 int i;
623
624 num_resp = cdns_readl(cdns, CDNS_MCP_FIFOSTAT);
625 num_resp &= CDNS_MCP_RX_FIFO_AVAIL;
626
627 cmd_base = CDNS_MCP_CMD_BASE;
628
629 for (i = 0; i < num_resp; i++) {
630 cdns->response_buf[i] = cdns_readl(cdns, cmd_base);
631 cmd_base += CDNS_MCP_CMD_WORD_LEN;
632 }
633}
634
Vinod Koul2f52a512017-12-14 11:19:41 +0530635static int cdns_update_slave_status(struct sdw_cdns *cdns,
Pierre-Louis Bossartbbb63812019-05-01 10:57:41 -0500636 u32 slave0, u32 slave1)
Vinod Koul2f52a512017-12-14 11:19:41 +0530637{
638 enum sdw_slave_status status[SDW_MAX_DEVICES + 1];
639 bool is_slave = false;
Pierre-Louis Bossarta78b32d2019-05-22 14:47:27 -0500640 u64 slave;
641 u32 mask;
Vinod Koul2f52a512017-12-14 11:19:41 +0530642 int i, set_status;
643
644 /* combine the two status */
645 slave = ((u64)slave1 << 32) | slave0;
646 memset(status, 0, sizeof(status));
647
648 for (i = 0; i <= SDW_MAX_DEVICES; i++) {
649 mask = (slave >> (i * CDNS_MCP_SLAVE_STATUS_NUM)) &
650 CDNS_MCP_SLAVE_STATUS_BITS;
651 if (!mask)
652 continue;
653
654 is_slave = true;
655 set_status = 0;
656
657 if (mask & CDNS_MCP_SLAVE_INTSTAT_RESERVED) {
658 status[i] = SDW_SLAVE_RESERVED;
659 set_status++;
660 }
661
662 if (mask & CDNS_MCP_SLAVE_INTSTAT_ATTACHED) {
663 status[i] = SDW_SLAVE_ATTACHED;
664 set_status++;
665 }
666
667 if (mask & CDNS_MCP_SLAVE_INTSTAT_ALERT) {
668 status[i] = SDW_SLAVE_ALERT;
669 set_status++;
670 }
671
672 if (mask & CDNS_MCP_SLAVE_INTSTAT_NPRESENT) {
673 status[i] = SDW_SLAVE_UNATTACHED;
674 set_status++;
675 }
676
677 /* first check if Slave reported multiple status */
678 if (set_status > 1) {
Pierre-Louis Bossarteb7df4c2019-05-22 14:47:26 -0500679 dev_warn_ratelimited(cdns->dev,
680 "Slave reported multiple Status: %d\n",
Pierre-Louis Bossarta78b32d2019-05-22 14:47:27 -0500681 mask);
Vinod Koul2f52a512017-12-14 11:19:41 +0530682 /*
683 * TODO: we need to reread the status here by
684 * issuing a PING cmd
685 */
686 }
687 }
688
689 if (is_slave)
690 return sdw_handle_slave_status(&cdns->bus, status);
691
692 return 0;
693}
694
695/**
696 * sdw_cdns_irq() - Cadence interrupt handler
697 * @irq: irq number
698 * @dev_id: irq context
699 */
700irqreturn_t sdw_cdns_irq(int irq, void *dev_id)
701{
702 struct sdw_cdns *cdns = dev_id;
703 u32 int_status;
704 int ret = IRQ_HANDLED;
705
706 /* Check if the link is up */
707 if (!cdns->link_up)
708 return IRQ_NONE;
709
710 int_status = cdns_readl(cdns, CDNS_MCP_INTSTAT);
711
Pierre-Louis Bossarta2cff9e2020-01-10 15:57:26 -0600712 /* check for reserved values read as zero */
713 if (int_status & CDNS_MCP_INT_RESERVED)
714 return IRQ_NONE;
715
Vinod Koul2f52a512017-12-14 11:19:41 +0530716 if (!(int_status & CDNS_MCP_INT_IRQ))
717 return IRQ_NONE;
718
Sanyog Kale956baa12017-12-14 11:19:42 +0530719 if (int_status & CDNS_MCP_INT_RX_WL) {
720 cdns_read_response(cdns);
721
722 if (cdns->defer) {
723 cdns_fill_msg_resp(cdns, cdns->defer->msg,
Pierre-Louis Bossartbbb63812019-05-01 10:57:41 -0500724 cdns->defer->length, 0);
Sanyog Kale956baa12017-12-14 11:19:42 +0530725 complete(&cdns->defer->complete);
726 cdns->defer = NULL;
Pierre-Louis Bossartf6e20962019-05-01 10:57:42 -0500727 } else {
Sanyog Kale956baa12017-12-14 11:19:42 +0530728 complete(&cdns->tx_complete);
Pierre-Louis Bossartf6e20962019-05-01 10:57:42 -0500729 }
Sanyog Kale956baa12017-12-14 11:19:42 +0530730 }
731
Pierre-Louis Bossart9b5884a2019-08-05 19:55:08 -0500732 if (int_status & CDNS_MCP_INT_PARITY) {
733 /* Parity error detected by Master */
734 dev_err_ratelimited(cdns->dev, "Parity error\n");
735 }
736
Vinod Koul2f52a512017-12-14 11:19:41 +0530737 if (int_status & CDNS_MCP_INT_CTRL_CLASH) {
Vinod Koul2f52a512017-12-14 11:19:41 +0530738 /* Slave is driving bit slot during control word */
739 dev_err_ratelimited(cdns->dev, "Bus clash for control word\n");
Vinod Koul2f52a512017-12-14 11:19:41 +0530740 }
741
742 if (int_status & CDNS_MCP_INT_DATA_CLASH) {
743 /*
744 * Multiple slaves trying to drive bit slot, or issue with
745 * ownership of data bits or Slave gone bonkers
746 */
747 dev_err_ratelimited(cdns->dev, "Bus clash for data word\n");
Vinod Koul2f52a512017-12-14 11:19:41 +0530748 }
749
750 if (int_status & CDNS_MCP_INT_SLAVE_MASK) {
751 /* Mask the Slave interrupt and wake thread */
752 cdns_updatel(cdns, CDNS_MCP_INTMASK,
Pierre-Louis Bossartbbb63812019-05-01 10:57:41 -0500753 CDNS_MCP_INT_SLAVE_MASK, 0);
Vinod Koul2f52a512017-12-14 11:19:41 +0530754
755 int_status &= ~CDNS_MCP_INT_SLAVE_MASK;
756 ret = IRQ_WAKE_THREAD;
757 }
758
759 cdns_writel(cdns, CDNS_MCP_INTSTAT, int_status);
760 return ret;
761}
762EXPORT_SYMBOL(sdw_cdns_irq);
763
764/**
765 * sdw_cdns_thread() - Cadence irq thread handler
766 * @irq: irq number
767 * @dev_id: irq context
768 */
769irqreturn_t sdw_cdns_thread(int irq, void *dev_id)
770{
771 struct sdw_cdns *cdns = dev_id;
772 u32 slave0, slave1;
773
Pierre-Louis Bossarteb7df4c2019-05-22 14:47:26 -0500774 dev_dbg_ratelimited(cdns->dev, "Slave status change\n");
Vinod Koul2f52a512017-12-14 11:19:41 +0530775
776 slave0 = cdns_readl(cdns, CDNS_MCP_SLAVE_INTSTAT0);
777 slave1 = cdns_readl(cdns, CDNS_MCP_SLAVE_INTSTAT1);
778
779 cdns_update_slave_status(cdns, slave0, slave1);
780 cdns_writel(cdns, CDNS_MCP_SLAVE_INTSTAT0, slave0);
781 cdns_writel(cdns, CDNS_MCP_SLAVE_INTSTAT1, slave1);
782
783 /* clear and unmask Slave interrupt now */
784 cdns_writel(cdns, CDNS_MCP_INTSTAT, CDNS_MCP_INT_SLAVE_MASK);
785 cdns_updatel(cdns, CDNS_MCP_INTMASK,
Pierre-Louis Bossartbbb63812019-05-01 10:57:41 -0500786 CDNS_MCP_INT_SLAVE_MASK, CDNS_MCP_INT_SLAVE_MASK);
Vinod Koul2f52a512017-12-14 11:19:41 +0530787
788 return IRQ_HANDLED;
789}
790EXPORT_SYMBOL(sdw_cdns_thread);
791
792/*
793 * init routines
794 */
Pierre-Louis Bossart49ea07d2019-10-22 18:54:44 -0500795
796/**
797 * sdw_cdns_exit_reset() - Program reset parameters and start bus operations
798 * @cdns: Cadence instance
799 */
800int sdw_cdns_exit_reset(struct sdw_cdns *cdns)
801{
802 /* program maximum length reset to be safe */
803 cdns_updatel(cdns, CDNS_MCP_CONTROL,
804 CDNS_MCP_CONTROL_RST_DELAY,
805 CDNS_MCP_CONTROL_RST_DELAY);
806
807 /* use hardware generated reset */
808 cdns_updatel(cdns, CDNS_MCP_CONTROL,
809 CDNS_MCP_CONTROL_HW_RST,
810 CDNS_MCP_CONTROL_HW_RST);
811
812 /* enable bus operations with clock and data */
813 cdns_updatel(cdns, CDNS_MCP_CONFIG,
814 CDNS_MCP_CONFIG_OP,
815 CDNS_MCP_CONFIG_OP_NORMAL);
816
817 /* commit changes */
818 return cdns_update_config(cdns);
819}
820EXPORT_SYMBOL(sdw_cdns_exit_reset);
821
822/**
823 * sdw_cdns_enable_interrupt() - Enable SDW interrupts and update config
824 * @cdns: Cadence instance
825 */
Pierre-Louis Bossart9e3d47f2019-10-22 18:54:47 -0500826int sdw_cdns_enable_interrupt(struct sdw_cdns *cdns, bool state)
Sanyog Kale956baa12017-12-14 11:19:42 +0530827{
Pierre-Louis Bossart9e3d47f2019-10-22 18:54:47 -0500828 u32 slave_intmask0 = 0;
829 u32 slave_intmask1 = 0;
830 u32 mask = 0;
Sanyog Kale956baa12017-12-14 11:19:42 +0530831
Pierre-Louis Bossart9e3d47f2019-10-22 18:54:47 -0500832 if (!state)
833 goto update_masks;
834
835 slave_intmask0 = CDNS_MCP_SLAVE_INTMASK0_MASK;
836 slave_intmask1 = CDNS_MCP_SLAVE_INTMASK1_MASK;
Sanyog Kale956baa12017-12-14 11:19:42 +0530837
Pierre-Louis Bossart9b5884a2019-08-05 19:55:08 -0500838 /* enable detection of all slave state changes */
839 mask = CDNS_MCP_INT_SLAVE_MASK;
840
841 /* enable detection of bus issues */
842 mask |= CDNS_MCP_INT_CTRL_CLASH | CDNS_MCP_INT_DATA_CLASH |
843 CDNS_MCP_INT_PARITY;
844
845 /* no detection of port interrupts for now */
846
847 /* enable detection of RX fifo level */
848 mask |= CDNS_MCP_INT_RX_WL;
849
850 /*
851 * CDNS_MCP_INT_IRQ needs to be set otherwise all previous
852 * settings are irrelevant
853 */
854 mask |= CDNS_MCP_INT_IRQ;
Sanyog Kale956baa12017-12-14 11:19:42 +0530855
Pierre-Louis Bossart04592dc2019-08-05 19:55:21 -0500856 if (interrupt_mask) /* parameter override */
857 mask = interrupt_mask;
Sanyog Kale956baa12017-12-14 11:19:42 +0530858
Pierre-Louis Bossart9e3d47f2019-10-22 18:54:47 -0500859update_masks:
Rander Wang5ebb0942020-01-10 15:57:27 -0600860 /* clear slave interrupt status before enabling interrupt */
861 if (state) {
862 u32 slave_state;
863
864 slave_state = cdns_readl(cdns, CDNS_MCP_SLAVE_INTSTAT0);
865 cdns_writel(cdns, CDNS_MCP_SLAVE_INTSTAT0, slave_state);
866 slave_state = cdns_readl(cdns, CDNS_MCP_SLAVE_INTSTAT1);
867 cdns_writel(cdns, CDNS_MCP_SLAVE_INTSTAT1, slave_state);
868 }
869
Pierre-Louis Bossart9e3d47f2019-10-22 18:54:47 -0500870 cdns_writel(cdns, CDNS_MCP_SLAVE_INTMASK0, slave_intmask0);
871 cdns_writel(cdns, CDNS_MCP_SLAVE_INTMASK1, slave_intmask1);
Sanyog Kale956baa12017-12-14 11:19:42 +0530872 cdns_writel(cdns, CDNS_MCP_INTMASK, mask);
873
Pierre-Louis Bossart49ea07d2019-10-22 18:54:44 -0500874 /* commit changes */
875 return cdns_update_config(cdns);
Sanyog Kale956baa12017-12-14 11:19:42 +0530876}
877EXPORT_SYMBOL(sdw_cdns_enable_interrupt);
Vinod Koul2f52a512017-12-14 11:19:41 +0530878
Vinod Koul07abeff2018-04-26 18:38:48 +0530879static int cdns_allocate_pdi(struct sdw_cdns *cdns,
Pierre-Louis Bossartbbb63812019-05-01 10:57:41 -0500880 struct sdw_cdns_pdi **stream,
881 u32 num, u32 pdi_offset)
Vinod Koul07abeff2018-04-26 18:38:48 +0530882{
883 struct sdw_cdns_pdi *pdi;
884 int i;
885
886 if (!num)
887 return 0;
888
889 pdi = devm_kcalloc(cdns->dev, num, sizeof(*pdi), GFP_KERNEL);
890 if (!pdi)
891 return -ENOMEM;
892
893 for (i = 0; i < num; i++) {
894 pdi[i].num = i + pdi_offset;
Vinod Koul07abeff2018-04-26 18:38:48 +0530895 }
896
897 *stream = pdi;
898 return 0;
899}
900
901/**
902 * sdw_cdns_pdi_init() - PDI initialization routine
903 *
904 * @cdns: Cadence instance
905 * @config: Stream configurations
906 */
907int sdw_cdns_pdi_init(struct sdw_cdns *cdns,
Pierre-Louis Bossartbbb63812019-05-01 10:57:41 -0500908 struct sdw_cdns_stream_config config)
Vinod Koul07abeff2018-04-26 18:38:48 +0530909{
910 struct sdw_cdns_streams *stream;
Pierre-Louis Bossart57a34792019-09-16 14:23:46 -0500911 int offset;
912 int ret;
Vinod Koul07abeff2018-04-26 18:38:48 +0530913
914 cdns->pcm.num_bd = config.pcm_bd;
915 cdns->pcm.num_in = config.pcm_in;
916 cdns->pcm.num_out = config.pcm_out;
917 cdns->pdm.num_bd = config.pdm_bd;
918 cdns->pdm.num_in = config.pdm_in;
919 cdns->pdm.num_out = config.pdm_out;
920
921 /* Allocate PDIs for PCMs */
922 stream = &cdns->pcm;
923
Pierre-Louis Bossart807c15b2019-09-16 14:23:47 -0500924 /* we allocate PDI0 and PDI1 which are used for Bulk */
925 offset = 0;
Vinod Koul07abeff2018-04-26 18:38:48 +0530926
927 ret = cdns_allocate_pdi(cdns, &stream->bd,
928 stream->num_bd, offset);
929 if (ret)
930 return ret;
931
932 offset += stream->num_bd;
933
934 ret = cdns_allocate_pdi(cdns, &stream->in,
935 stream->num_in, offset);
936 if (ret)
937 return ret;
938
939 offset += stream->num_in;
940
941 ret = cdns_allocate_pdi(cdns, &stream->out,
942 stream->num_out, offset);
943 if (ret)
944 return ret;
945
946 /* Update total number of PCM PDIs */
947 stream->num_pdi = stream->num_bd + stream->num_in + stream->num_out;
948 cdns->num_ports = stream->num_pdi;
949
950 /* Allocate PDIs for PDMs */
951 stream = &cdns->pdm;
Vinod Koul07abeff2018-04-26 18:38:48 +0530952 ret = cdns_allocate_pdi(cdns, &stream->bd,
953 stream->num_bd, offset);
954 if (ret)
955 return ret;
956
957 offset += stream->num_bd;
958
959 ret = cdns_allocate_pdi(cdns, &stream->in,
960 stream->num_in, offset);
961 if (ret)
962 return ret;
963
964 offset += stream->num_in;
965
966 ret = cdns_allocate_pdi(cdns, &stream->out,
967 stream->num_out, offset);
Pierre-Louis Bossart807c15b2019-09-16 14:23:47 -0500968
969 offset += stream->num_out;
970
Vinod Koul07abeff2018-04-26 18:38:48 +0530971 if (ret)
972 return ret;
973
974 /* Update total number of PDM PDIs */
975 stream->num_pdi = stream->num_bd + stream->num_in + stream->num_out;
976 cdns->num_ports += stream->num_pdi;
977
Vinod Koul07abeff2018-04-26 18:38:48 +0530978 return 0;
979}
980EXPORT_SYMBOL(sdw_cdns_pdi_init);
981
Pierre-Louis Bossart05be59a2019-08-05 19:55:11 -0500982static u32 cdns_set_initial_frame_shape(int n_rows, int n_cols)
983{
984 u32 val;
985 int c;
986 int r;
987
988 r = sdw_find_row_index(n_rows);
989 c = sdw_find_col_index(n_cols) & CDNS_MCP_FRAME_SHAPE_COL_MASK;
990
991 val = (r << CDNS_MCP_FRAME_SHAPE_ROW_OFFSET) | c;
992
993 return val;
994}
995
Vinod Koul2f52a512017-12-14 11:19:41 +0530996/**
997 * sdw_cdns_init() - Cadence initialization
998 * @cdns: Cadence instance
999 */
Pierre-Louis Bossart3ccb8552019-10-22 18:54:48 -05001000int sdw_cdns_init(struct sdw_cdns *cdns, bool clock_stop_exit)
Vinod Koul2f52a512017-12-14 11:19:41 +05301001{
Pierre-Louis Bossart38598722019-08-05 19:55:17 -05001002 struct sdw_bus *bus = &cdns->bus;
1003 struct sdw_master_prop *prop = &bus->prop;
Vinod Koul2f52a512017-12-14 11:19:41 +05301004 u32 val;
Pierre-Louis Bossart38598722019-08-05 19:55:17 -05001005 int divider;
Vinod Koul2f52a512017-12-14 11:19:41 +05301006 int ret;
1007
Pierre-Louis Bossart3ccb8552019-10-22 18:54:48 -05001008 if (clock_stop_exit) {
1009 ret = cdns_clear_bit(cdns, CDNS_MCP_CONTROL,
1010 CDNS_MCP_CONTROL_CLK_STOP_CLR);
1011 if (ret < 0) {
1012 dev_err(cdns->dev, "Couldn't exit from clock stop\n");
1013 return ret;
1014 }
Vinod Koul2f52a512017-12-14 11:19:41 +05301015 }
1016
1017 /* Set clock divider */
Pierre-Louis Bossart38598722019-08-05 19:55:17 -05001018 divider = (prop->mclk_freq / prop->max_clk_freq) - 1;
Vinod Koul2f52a512017-12-14 11:19:41 +05301019
Rander Wanga50954e2019-08-05 19:55:18 -05001020 cdns_updatel(cdns, CDNS_MCP_CLK_CTRL0,
1021 CDNS_MCP_CLK_MCLKD_MASK, divider);
1022 cdns_updatel(cdns, CDNS_MCP_CLK_CTRL1,
1023 CDNS_MCP_CLK_MCLKD_MASK, divider);
Vinod Koul2f52a512017-12-14 11:19:41 +05301024
Pierre-Louis Bossart05be59a2019-08-05 19:55:11 -05001025 /*
1026 * Frame shape changes after initialization have to be done
1027 * with the bank switch mechanism
1028 */
1029 val = cdns_set_initial_frame_shape(prop->default_row,
1030 prop->default_col);
1031 cdns_writel(cdns, CDNS_MCP_FRAME_SHAPE_INIT, val);
Vinod Koul2f52a512017-12-14 11:19:41 +05301032
1033 /* Set SSP interval to default value */
1034 cdns_writel(cdns, CDNS_MCP_SSP_CTRL0, CDNS_DEFAULT_SSP_INTERVAL);
1035 cdns_writel(cdns, CDNS_MCP_SSP_CTRL1, CDNS_DEFAULT_SSP_INTERVAL);
1036
Pierre-Louis Bossart49ea07d2019-10-22 18:54:44 -05001037 /* flush command FIFOs */
1038 cdns_updatel(cdns, CDNS_MCP_CONTROL, CDNS_MCP_CONTROL_CMD_RST,
1039 CDNS_MCP_CONTROL_CMD_RST);
1040
Vinod Koul2f52a512017-12-14 11:19:41 +05301041 /* Set cmd accept mode */
1042 cdns_updatel(cdns, CDNS_MCP_CONTROL, CDNS_MCP_CONTROL_CMD_ACCEPT,
Pierre-Louis Bossartbbb63812019-05-01 10:57:41 -05001043 CDNS_MCP_CONTROL_CMD_ACCEPT);
Vinod Koul2f52a512017-12-14 11:19:41 +05301044
1045 /* Configure mcp config */
1046 val = cdns_readl(cdns, CDNS_MCP_CONFIG);
1047
1048 /* Set Max cmd retry to 15 */
1049 val |= CDNS_MCP_CONFIG_MCMD_RETRY;
1050
1051 /* Set frame delay between PREQ and ping frame to 15 frames */
1052 val |= 0xF << SDW_REG_SHIFT(CDNS_MCP_CONFIG_MPREQ_DELAY);
1053
1054 /* Disable auto bus release */
1055 val &= ~CDNS_MCP_CONFIG_BUS_REL;
1056
1057 /* Disable sniffer mode */
1058 val &= ~CDNS_MCP_CONFIG_SNIFFER;
1059
1060 /* Set cmd mode for Tx and Rx cmds */
1061 val &= ~CDNS_MCP_CONFIG_CMD;
1062
Vinod Koul2f52a512017-12-14 11:19:41 +05301063 cdns_writel(cdns, CDNS_MCP_CONFIG, val);
1064
Pierre-Louis Bossart49ea07d2019-10-22 18:54:44 -05001065 /* commit changes */
1066 return cdns_update_config(cdns);
Vinod Koul2f52a512017-12-14 11:19:41 +05301067}
1068EXPORT_SYMBOL(sdw_cdns_init);
1069
Vinod Koul07abeff2018-04-26 18:38:48 +05301070int cdns_bus_conf(struct sdw_bus *bus, struct sdw_bus_params *params)
1071{
Pierre-Louis Bossart38598722019-08-05 19:55:17 -05001072 struct sdw_master_prop *prop = &bus->prop;
Vinod Koul07abeff2018-04-26 18:38:48 +05301073 struct sdw_cdns *cdns = bus_to_cdns(bus);
Rander Wanga50954e2019-08-05 19:55:18 -05001074 int mcp_clkctrl_off;
Vinod Koul07abeff2018-04-26 18:38:48 +05301075 int divider;
1076
1077 if (!params->curr_dr_freq) {
Pierre-Louis Bossart17ed5be2019-05-01 10:57:45 -05001078 dev_err(cdns->dev, "NULL curr_dr_freq\n");
Vinod Koul07abeff2018-04-26 18:38:48 +05301079 return -EINVAL;
1080 }
1081
Pierre-Louis Bossart38598722019-08-05 19:55:17 -05001082 divider = prop->mclk_freq * SDW_DOUBLE_RATE_FACTOR /
1083 params->curr_dr_freq;
1084 divider--; /* divider is 1/(N+1) */
Vinod Koul07abeff2018-04-26 18:38:48 +05301085
1086 if (params->next_bank)
1087 mcp_clkctrl_off = CDNS_MCP_CLK_CTRL1;
1088 else
1089 mcp_clkctrl_off = CDNS_MCP_CLK_CTRL0;
1090
Rander Wanga50954e2019-08-05 19:55:18 -05001091 cdns_updatel(cdns, mcp_clkctrl_off, CDNS_MCP_CLK_MCLKD_MASK, divider);
Vinod Koul07abeff2018-04-26 18:38:48 +05301092
1093 return 0;
1094}
1095EXPORT_SYMBOL(cdns_bus_conf);
1096
1097static int cdns_port_params(struct sdw_bus *bus,
Pierre-Louis Bossartbbb63812019-05-01 10:57:41 -05001098 struct sdw_port_params *p_params, unsigned int bank)
Vinod Koul07abeff2018-04-26 18:38:48 +05301099{
1100 struct sdw_cdns *cdns = bus_to_cdns(bus);
1101 int dpn_config = 0, dpn_config_off;
1102
1103 if (bank)
1104 dpn_config_off = CDNS_DPN_B1_CONFIG(p_params->num);
1105 else
1106 dpn_config_off = CDNS_DPN_B0_CONFIG(p_params->num);
1107
1108 dpn_config = cdns_readl(cdns, dpn_config_off);
1109
1110 dpn_config |= ((p_params->bps - 1) <<
1111 SDW_REG_SHIFT(CDNS_DPN_CONFIG_WL));
1112 dpn_config |= (p_params->flow_mode <<
1113 SDW_REG_SHIFT(CDNS_DPN_CONFIG_PORT_FLOW));
1114 dpn_config |= (p_params->data_mode <<
1115 SDW_REG_SHIFT(CDNS_DPN_CONFIG_PORT_DAT));
1116
1117 cdns_writel(cdns, dpn_config_off, dpn_config);
1118
1119 return 0;
1120}
1121
1122static int cdns_transport_params(struct sdw_bus *bus,
Pierre-Louis Bossartbbb63812019-05-01 10:57:41 -05001123 struct sdw_transport_params *t_params,
1124 enum sdw_reg_bank bank)
Vinod Koul07abeff2018-04-26 18:38:48 +05301125{
1126 struct sdw_cdns *cdns = bus_to_cdns(bus);
1127 int dpn_offsetctrl = 0, dpn_offsetctrl_off;
1128 int dpn_config = 0, dpn_config_off;
1129 int dpn_hctrl = 0, dpn_hctrl_off;
1130 int num = t_params->port_num;
1131 int dpn_samplectrl_off;
1132
1133 /*
1134 * Note: Only full data port is supported on the Master side for
1135 * both PCM and PDM ports.
1136 */
1137
1138 if (bank) {
1139 dpn_config_off = CDNS_DPN_B1_CONFIG(num);
1140 dpn_samplectrl_off = CDNS_DPN_B1_SAMPLE_CTRL(num);
1141 dpn_hctrl_off = CDNS_DPN_B1_HCTRL(num);
1142 dpn_offsetctrl_off = CDNS_DPN_B1_OFFSET_CTRL(num);
1143 } else {
1144 dpn_config_off = CDNS_DPN_B0_CONFIG(num);
1145 dpn_samplectrl_off = CDNS_DPN_B0_SAMPLE_CTRL(num);
1146 dpn_hctrl_off = CDNS_DPN_B0_HCTRL(num);
1147 dpn_offsetctrl_off = CDNS_DPN_B0_OFFSET_CTRL(num);
1148 }
1149
1150 dpn_config = cdns_readl(cdns, dpn_config_off);
1151
1152 dpn_config |= (t_params->blk_grp_ctrl <<
1153 SDW_REG_SHIFT(CDNS_DPN_CONFIG_BGC));
1154 dpn_config |= (t_params->blk_pkg_mode <<
1155 SDW_REG_SHIFT(CDNS_DPN_CONFIG_BPM));
1156 cdns_writel(cdns, dpn_config_off, dpn_config);
1157
1158 dpn_offsetctrl |= (t_params->offset1 <<
1159 SDW_REG_SHIFT(CDNS_DPN_OFFSET_CTRL_1));
1160 dpn_offsetctrl |= (t_params->offset2 <<
1161 SDW_REG_SHIFT(CDNS_DPN_OFFSET_CTRL_2));
1162 cdns_writel(cdns, dpn_offsetctrl_off, dpn_offsetctrl);
1163
1164 dpn_hctrl |= (t_params->hstart <<
1165 SDW_REG_SHIFT(CDNS_DPN_HCTRL_HSTART));
1166 dpn_hctrl |= (t_params->hstop << SDW_REG_SHIFT(CDNS_DPN_HCTRL_HSTOP));
1167 dpn_hctrl |= (t_params->lane_ctrl <<
1168 SDW_REG_SHIFT(CDNS_DPN_HCTRL_LCTRL));
1169
1170 cdns_writel(cdns, dpn_hctrl_off, dpn_hctrl);
1171 cdns_writel(cdns, dpn_samplectrl_off, (t_params->sample_interval - 1));
1172
1173 return 0;
1174}
1175
1176static int cdns_port_enable(struct sdw_bus *bus,
Pierre-Louis Bossartbbb63812019-05-01 10:57:41 -05001177 struct sdw_enable_ch *enable_ch, unsigned int bank)
Vinod Koul07abeff2018-04-26 18:38:48 +05301178{
1179 struct sdw_cdns *cdns = bus_to_cdns(bus);
1180 int dpn_chnen_off, ch_mask;
1181
1182 if (bank)
1183 dpn_chnen_off = CDNS_DPN_B1_CH_EN(enable_ch->port_num);
1184 else
1185 dpn_chnen_off = CDNS_DPN_B0_CH_EN(enable_ch->port_num);
1186
1187 ch_mask = enable_ch->ch_mask * enable_ch->enable;
1188 cdns_writel(cdns, dpn_chnen_off, ch_mask);
1189
1190 return 0;
1191}
1192
1193static const struct sdw_master_port_ops cdns_port_ops = {
1194 .dpn_set_port_params = cdns_port_params,
1195 .dpn_set_port_transport_params = cdns_transport_params,
1196 .dpn_port_enable_ch = cdns_port_enable,
1197};
1198
Sanyog Kale956baa12017-12-14 11:19:42 +05301199/**
1200 * sdw_cdns_probe() - Cadence probe routine
1201 * @cdns: Cadence instance
1202 */
1203int sdw_cdns_probe(struct sdw_cdns *cdns)
1204{
1205 init_completion(&cdns->tx_complete);
Vinod Koul07abeff2018-04-26 18:38:48 +05301206 cdns->bus.port_ops = &cdns_port_ops;
Sanyog Kale956baa12017-12-14 11:19:42 +05301207
1208 return 0;
1209}
1210EXPORT_SYMBOL(sdw_cdns_probe);
1211
Vinod Koul5d6b3c82018-04-26 18:38:53 +05301212int cdns_set_sdw_stream(struct snd_soc_dai *dai,
Pierre-Louis Bossartbbb63812019-05-01 10:57:41 -05001213 void *stream, bool pcm, int direction)
Vinod Koul5d6b3c82018-04-26 18:38:53 +05301214{
1215 struct sdw_cdns *cdns = snd_soc_dai_get_drvdata(dai);
1216 struct sdw_cdns_dma_data *dma;
1217
1218 dma = kzalloc(sizeof(*dma), GFP_KERNEL);
1219 if (!dma)
1220 return -ENOMEM;
1221
1222 if (pcm)
1223 dma->stream_type = SDW_STREAM_PCM;
1224 else
1225 dma->stream_type = SDW_STREAM_PDM;
1226
1227 dma->bus = &cdns->bus;
1228 dma->link_id = cdns->instance;
1229
1230 dma->stream = stream;
1231
1232 if (direction == SNDRV_PCM_STREAM_PLAYBACK)
1233 dai->playback_dma_data = dma;
1234 else
1235 dai->capture_dma_data = dma;
1236
1237 return 0;
1238}
1239EXPORT_SYMBOL(cdns_set_sdw_stream);
1240
1241/**
1242 * cdns_find_pdi() - Find a free PDI
1243 *
1244 * @cdns: Cadence instance
1245 * @num: Number of PDIs
1246 * @pdi: PDI instances
1247 *
Bard Liao1b53385e2019-09-16 14:23:48 -05001248 * Find a PDI for a given PDI array. The PDI num and dai_id are
1249 * expected to match, return NULL otherwise.
Vinod Koul5d6b3c82018-04-26 18:38:53 +05301250 */
1251static struct sdw_cdns_pdi *cdns_find_pdi(struct sdw_cdns *cdns,
Pierre-Louis Bossart807c15b2019-09-16 14:23:47 -05001252 unsigned int offset,
Pierre-Louis Bossartbbb63812019-05-01 10:57:41 -05001253 unsigned int num,
Bard Liao1b53385e2019-09-16 14:23:48 -05001254 struct sdw_cdns_pdi *pdi,
1255 int dai_id)
Vinod Koul5d6b3c82018-04-26 18:38:53 +05301256{
1257 int i;
1258
Bard Liao1b53385e2019-09-16 14:23:48 -05001259 for (i = offset; i < offset + num; i++)
1260 if (pdi[i].num == dai_id)
1261 return &pdi[i];
Vinod Koul5d6b3c82018-04-26 18:38:53 +05301262
1263 return NULL;
1264}
1265
1266/**
1267 * sdw_cdns_config_stream: Configure a stream
1268 *
1269 * @cdns: Cadence instance
Vinod Koul5d6b3c82018-04-26 18:38:53 +05301270 * @ch: Channel count
1271 * @dir: Data direction
1272 * @pdi: PDI to be used
1273 */
1274void sdw_cdns_config_stream(struct sdw_cdns *cdns,
Pierre-Louis Bossartbbb63812019-05-01 10:57:41 -05001275 u32 ch, u32 dir, struct sdw_cdns_pdi *pdi)
Vinod Koul5d6b3c82018-04-26 18:38:53 +05301276{
1277 u32 offset, val = 0;
1278
1279 if (dir == SDW_DATA_DIR_RX)
1280 val = CDNS_PORTCTRL_DIRN;
1281
Pierre-Louis Bossart57a34792019-09-16 14:23:46 -05001282 offset = CDNS_PORTCTRL + pdi->num * CDNS_PORT_OFFSET;
Vinod Koul5d6b3c82018-04-26 18:38:53 +05301283 cdns_updatel(cdns, offset, CDNS_PORTCTRL_DIRN, val);
1284
Pierre-Louis Bossart57a34792019-09-16 14:23:46 -05001285 val = pdi->num;
Vinod Koul5d6b3c82018-04-26 18:38:53 +05301286 val |= ((1 << ch) - 1) << SDW_REG_SHIFT(CDNS_PDI_CONFIG_CHANNEL);
1287 cdns_writel(cdns, CDNS_PDI_CONFIG(pdi->num), val);
1288}
1289EXPORT_SYMBOL(sdw_cdns_config_stream);
1290
1291/**
Pierre-Louis Bossart57a34792019-09-16 14:23:46 -05001292 * sdw_cdns_alloc_pdi() - Allocate a PDI
Vinod Koul5d6b3c82018-04-26 18:38:53 +05301293 *
1294 * @cdns: Cadence instance
1295 * @stream: Stream to be allocated
1296 * @ch: Channel count
1297 * @dir: Data direction
1298 */
Pierre-Louis Bossart57a34792019-09-16 14:23:46 -05001299struct sdw_cdns_pdi *sdw_cdns_alloc_pdi(struct sdw_cdns *cdns,
1300 struct sdw_cdns_streams *stream,
Bard Liao1b53385e2019-09-16 14:23:48 -05001301 u32 ch, u32 dir, int dai_id)
Vinod Koul5d6b3c82018-04-26 18:38:53 +05301302{
1303 struct sdw_cdns_pdi *pdi = NULL;
1304
1305 if (dir == SDW_DATA_DIR_RX)
Bard Liao1b53385e2019-09-16 14:23:48 -05001306 pdi = cdns_find_pdi(cdns, 0, stream->num_in, stream->in,
1307 dai_id);
Vinod Koul5d6b3c82018-04-26 18:38:53 +05301308 else
Bard Liao1b53385e2019-09-16 14:23:48 -05001309 pdi = cdns_find_pdi(cdns, 0, stream->num_out, stream->out,
1310 dai_id);
Vinod Koul5d6b3c82018-04-26 18:38:53 +05301311
1312 /* check if we found a PDI, else find in bi-directional */
1313 if (!pdi)
Bard Liao1b53385e2019-09-16 14:23:48 -05001314 pdi = cdns_find_pdi(cdns, 2, stream->num_bd, stream->bd,
1315 dai_id);
Vinod Koul5d6b3c82018-04-26 18:38:53 +05301316
Pierre-Louis Bossart57a34792019-09-16 14:23:46 -05001317 if (pdi) {
1318 pdi->l_ch_num = 0;
1319 pdi->h_ch_num = ch - 1;
1320 pdi->dir = dir;
1321 pdi->ch_count = ch;
1322 }
Vinod Koul5d6b3c82018-04-26 18:38:53 +05301323
Pierre-Louis Bossart57a34792019-09-16 14:23:46 -05001324 return pdi;
Vinod Koul5d6b3c82018-04-26 18:38:53 +05301325}
Pierre-Louis Bossart57a34792019-09-16 14:23:46 -05001326EXPORT_SYMBOL(sdw_cdns_alloc_pdi);
Vinod Koul5d6b3c82018-04-26 18:38:53 +05301327
Vinod Koul2f52a512017-12-14 11:19:41 +05301328MODULE_LICENSE("Dual BSD/GPL");
1329MODULE_DESCRIPTION("Cadence Soundwire Library");