blob: a0ec21b64d4225cf9da1aae09f97d0334143b1cb [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) {
450 dev_err(cdns->dev, "IO transfer timed out\n");
451 msg->len = 0;
452 return SDW_CMD_TIMEOUT;
453 }
454
455 return cdns_fill_msg_resp(cdns, msg, count, offset);
456}
457
Pierre-Louis Bossartbbb63812019-05-01 10:57:41 -0500458static enum sdw_command_response
459cdns_program_scp_addr(struct sdw_cdns *cdns, struct sdw_msg *msg)
Sanyog Kale956baa12017-12-14 11:19:42 +0530460{
461 int nack = 0, no_ack = 0;
462 unsigned long time;
463 u32 data[2], base;
464 int i;
465
466 /* Program the watermark level for RX FIFO */
467 if (cdns->msg_count != CDNS_SCP_RX_FIFOLEVEL) {
468 cdns_writel(cdns, CDNS_MCP_FIFOLEVEL, CDNS_SCP_RX_FIFOLEVEL);
469 cdns->msg_count = CDNS_SCP_RX_FIFOLEVEL;
470 }
471
472 data[0] = msg->dev_num << SDW_REG_SHIFT(CDNS_MCP_CMD_DEV_ADDR);
473 data[0] |= 0x3 << SDW_REG_SHIFT(CDNS_MCP_CMD_COMMAND);
474 data[1] = data[0];
475
476 data[0] |= SDW_SCP_ADDRPAGE1 << SDW_REG_SHIFT(CDNS_MCP_CMD_REG_ADDR_L);
477 data[1] |= SDW_SCP_ADDRPAGE2 << SDW_REG_SHIFT(CDNS_MCP_CMD_REG_ADDR_L);
478
479 data[0] |= msg->addr_page1;
480 data[1] |= msg->addr_page2;
481
482 base = CDNS_MCP_CMD_BASE;
483 cdns_writel(cdns, base, data[0]);
484 base += CDNS_MCP_CMD_WORD_LEN;
485 cdns_writel(cdns, base, data[1]);
486
487 time = wait_for_completion_timeout(&cdns->tx_complete,
Pierre-Louis Bossartbbb63812019-05-01 10:57:41 -0500488 msecs_to_jiffies(CDNS_TX_TIMEOUT));
Sanyog Kale956baa12017-12-14 11:19:42 +0530489 if (!time) {
490 dev_err(cdns->dev, "SCP Msg trf timed out\n");
491 msg->len = 0;
492 return SDW_CMD_TIMEOUT;
493 }
494
495 /* check response the writes */
496 for (i = 0; i < 2; i++) {
497 if (!(cdns->response_buf[i] & CDNS_MCP_RESP_ACK)) {
498 no_ack = 1;
Pierre-Louis Bossart17ed5be2019-05-01 10:57:45 -0500499 dev_err(cdns->dev, "Program SCP Ack not received\n");
Sanyog Kale956baa12017-12-14 11:19:42 +0530500 if (cdns->response_buf[i] & CDNS_MCP_RESP_NACK) {
501 nack = 1;
Pierre-Louis Bossart17ed5be2019-05-01 10:57:45 -0500502 dev_err(cdns->dev, "Program SCP NACK received\n");
Sanyog Kale956baa12017-12-14 11:19:42 +0530503 }
504 }
505 }
506
507 /* For NACK, NO ack, don't return err if we are in Broadcast mode */
508 if (nack) {
Pierre-Louis Bossarteb7df4c2019-05-22 14:47:26 -0500509 dev_err_ratelimited(cdns->dev,
510 "SCP_addrpage NACKed for Slave %d\n", msg->dev_num);
Sanyog Kale956baa12017-12-14 11:19:42 +0530511 return SDW_CMD_FAIL;
512 } else if (no_ack) {
Pierre-Louis Bossarteb7df4c2019-05-22 14:47:26 -0500513 dev_dbg_ratelimited(cdns->dev,
514 "SCP_addrpage ignored for Slave %d\n", msg->dev_num);
Sanyog Kale956baa12017-12-14 11:19:42 +0530515 return SDW_CMD_IGNORED;
516 }
517
518 return SDW_CMD_OK;
519}
520
521static int cdns_prep_msg(struct sdw_cdns *cdns, struct sdw_msg *msg, int *cmd)
522{
523 int ret;
524
525 if (msg->page) {
526 ret = cdns_program_scp_addr(cdns, msg);
527 if (ret) {
528 msg->len = 0;
529 return ret;
530 }
531 }
532
533 switch (msg->flags) {
534 case SDW_MSG_FLAG_READ:
535 *cmd = CDNS_MCP_CMD_READ;
536 break;
537
538 case SDW_MSG_FLAG_WRITE:
539 *cmd = CDNS_MCP_CMD_WRITE;
540 break;
541
542 default:
543 dev_err(cdns->dev, "Invalid msg cmd: %d\n", msg->flags);
544 return -EINVAL;
545 }
546
547 return 0;
548}
549
Shreyas NCc91605f2018-04-26 18:38:43 +0530550enum sdw_command_response
Sanyog Kale956baa12017-12-14 11:19:42 +0530551cdns_xfer_msg(struct sdw_bus *bus, struct sdw_msg *msg)
552{
553 struct sdw_cdns *cdns = bus_to_cdns(bus);
554 int cmd = 0, ret, i;
555
556 ret = cdns_prep_msg(cdns, msg, &cmd);
557 if (ret)
558 return SDW_CMD_FAIL_OTHER;
559
560 for (i = 0; i < msg->len / CDNS_MCP_CMD_LEN; i++) {
561 ret = _cdns_xfer_msg(cdns, msg, cmd, i * CDNS_MCP_CMD_LEN,
Pierre-Louis Bossartbbb63812019-05-01 10:57:41 -0500562 CDNS_MCP_CMD_LEN, false);
Sanyog Kale956baa12017-12-14 11:19:42 +0530563 if (ret < 0)
564 goto exit;
565 }
566
567 if (!(msg->len % CDNS_MCP_CMD_LEN))
568 goto exit;
569
570 ret = _cdns_xfer_msg(cdns, msg, cmd, i * CDNS_MCP_CMD_LEN,
Pierre-Louis Bossartbbb63812019-05-01 10:57:41 -0500571 msg->len % CDNS_MCP_CMD_LEN, false);
Sanyog Kale956baa12017-12-14 11:19:42 +0530572
573exit:
574 return ret;
575}
Shreyas NCc91605f2018-04-26 18:38:43 +0530576EXPORT_SYMBOL(cdns_xfer_msg);
Sanyog Kale956baa12017-12-14 11:19:42 +0530577
Shreyas NCc91605f2018-04-26 18:38:43 +0530578enum sdw_command_response
Sanyog Kale956baa12017-12-14 11:19:42 +0530579cdns_xfer_msg_defer(struct sdw_bus *bus,
Pierre-Louis Bossartbbb63812019-05-01 10:57:41 -0500580 struct sdw_msg *msg, struct sdw_defer *defer)
Sanyog Kale956baa12017-12-14 11:19:42 +0530581{
582 struct sdw_cdns *cdns = bus_to_cdns(bus);
583 int cmd = 0, ret;
584
585 /* for defer only 1 message is supported */
586 if (msg->len > 1)
587 return -ENOTSUPP;
588
589 ret = cdns_prep_msg(cdns, msg, &cmd);
590 if (ret)
591 return SDW_CMD_FAIL_OTHER;
592
593 cdns->defer = defer;
594 cdns->defer->length = msg->len;
595
596 return _cdns_xfer_msg(cdns, msg, cmd, 0, msg->len, true);
597}
Shreyas NCc91605f2018-04-26 18:38:43 +0530598EXPORT_SYMBOL(cdns_xfer_msg_defer);
Sanyog Kale956baa12017-12-14 11:19:42 +0530599
Shreyas NCc91605f2018-04-26 18:38:43 +0530600enum sdw_command_response
Sanyog Kale956baa12017-12-14 11:19:42 +0530601cdns_reset_page_addr(struct sdw_bus *bus, unsigned int dev_num)
602{
603 struct sdw_cdns *cdns = bus_to_cdns(bus);
604 struct sdw_msg msg;
605
606 /* Create dummy message with valid device number */
607 memset(&msg, 0, sizeof(msg));
608 msg.dev_num = dev_num;
609
610 return cdns_program_scp_addr(cdns, &msg);
611}
Shreyas NCc91605f2018-04-26 18:38:43 +0530612EXPORT_SYMBOL(cdns_reset_page_addr);
Sanyog Kale956baa12017-12-14 11:19:42 +0530613
614/*
Vinod Koul2f52a512017-12-14 11:19:41 +0530615 * IRQ handling
616 */
617
Sanyog Kale956baa12017-12-14 11:19:42 +0530618static void cdns_read_response(struct sdw_cdns *cdns)
619{
620 u32 num_resp, cmd_base;
621 int i;
622
623 num_resp = cdns_readl(cdns, CDNS_MCP_FIFOSTAT);
624 num_resp &= CDNS_MCP_RX_FIFO_AVAIL;
625
626 cmd_base = CDNS_MCP_CMD_BASE;
627
628 for (i = 0; i < num_resp; i++) {
629 cdns->response_buf[i] = cdns_readl(cdns, cmd_base);
630 cmd_base += CDNS_MCP_CMD_WORD_LEN;
631 }
632}
633
Vinod Koul2f52a512017-12-14 11:19:41 +0530634static int cdns_update_slave_status(struct sdw_cdns *cdns,
Pierre-Louis Bossartbbb63812019-05-01 10:57:41 -0500635 u32 slave0, u32 slave1)
Vinod Koul2f52a512017-12-14 11:19:41 +0530636{
637 enum sdw_slave_status status[SDW_MAX_DEVICES + 1];
638 bool is_slave = false;
Pierre-Louis Bossarta78b32d2019-05-22 14:47:27 -0500639 u64 slave;
640 u32 mask;
Vinod Koul2f52a512017-12-14 11:19:41 +0530641 int i, set_status;
642
643 /* combine the two status */
644 slave = ((u64)slave1 << 32) | slave0;
645 memset(status, 0, sizeof(status));
646
647 for (i = 0; i <= SDW_MAX_DEVICES; i++) {
648 mask = (slave >> (i * CDNS_MCP_SLAVE_STATUS_NUM)) &
649 CDNS_MCP_SLAVE_STATUS_BITS;
650 if (!mask)
651 continue;
652
653 is_slave = true;
654 set_status = 0;
655
656 if (mask & CDNS_MCP_SLAVE_INTSTAT_RESERVED) {
657 status[i] = SDW_SLAVE_RESERVED;
658 set_status++;
659 }
660
661 if (mask & CDNS_MCP_SLAVE_INTSTAT_ATTACHED) {
662 status[i] = SDW_SLAVE_ATTACHED;
663 set_status++;
664 }
665
666 if (mask & CDNS_MCP_SLAVE_INTSTAT_ALERT) {
667 status[i] = SDW_SLAVE_ALERT;
668 set_status++;
669 }
670
671 if (mask & CDNS_MCP_SLAVE_INTSTAT_NPRESENT) {
672 status[i] = SDW_SLAVE_UNATTACHED;
673 set_status++;
674 }
675
676 /* first check if Slave reported multiple status */
677 if (set_status > 1) {
Pierre-Louis Bossarteb7df4c2019-05-22 14:47:26 -0500678 dev_warn_ratelimited(cdns->dev,
679 "Slave reported multiple Status: %d\n",
Pierre-Louis Bossarta78b32d2019-05-22 14:47:27 -0500680 mask);
Vinod Koul2f52a512017-12-14 11:19:41 +0530681 /*
682 * TODO: we need to reread the status here by
683 * issuing a PING cmd
684 */
685 }
686 }
687
688 if (is_slave)
689 return sdw_handle_slave_status(&cdns->bus, status);
690
691 return 0;
692}
693
694/**
695 * sdw_cdns_irq() - Cadence interrupt handler
696 * @irq: irq number
697 * @dev_id: irq context
698 */
699irqreturn_t sdw_cdns_irq(int irq, void *dev_id)
700{
701 struct sdw_cdns *cdns = dev_id;
702 u32 int_status;
703 int ret = IRQ_HANDLED;
704
705 /* Check if the link is up */
706 if (!cdns->link_up)
707 return IRQ_NONE;
708
709 int_status = cdns_readl(cdns, CDNS_MCP_INTSTAT);
710
Pierre-Louis Bossarta2cff9e2020-01-10 15:57:26 -0600711 /* check for reserved values read as zero */
712 if (int_status & CDNS_MCP_INT_RESERVED)
713 return IRQ_NONE;
714
Vinod Koul2f52a512017-12-14 11:19:41 +0530715 if (!(int_status & CDNS_MCP_INT_IRQ))
716 return IRQ_NONE;
717
Sanyog Kale956baa12017-12-14 11:19:42 +0530718 if (int_status & CDNS_MCP_INT_RX_WL) {
719 cdns_read_response(cdns);
720
721 if (cdns->defer) {
722 cdns_fill_msg_resp(cdns, cdns->defer->msg,
Pierre-Louis Bossartbbb63812019-05-01 10:57:41 -0500723 cdns->defer->length, 0);
Sanyog Kale956baa12017-12-14 11:19:42 +0530724 complete(&cdns->defer->complete);
725 cdns->defer = NULL;
Pierre-Louis Bossartf6e20962019-05-01 10:57:42 -0500726 } else {
Sanyog Kale956baa12017-12-14 11:19:42 +0530727 complete(&cdns->tx_complete);
Pierre-Louis Bossartf6e20962019-05-01 10:57:42 -0500728 }
Sanyog Kale956baa12017-12-14 11:19:42 +0530729 }
730
Pierre-Louis Bossart9b5884a2019-08-05 19:55:08 -0500731 if (int_status & CDNS_MCP_INT_PARITY) {
732 /* Parity error detected by Master */
733 dev_err_ratelimited(cdns->dev, "Parity error\n");
734 }
735
Vinod Koul2f52a512017-12-14 11:19:41 +0530736 if (int_status & CDNS_MCP_INT_CTRL_CLASH) {
Vinod Koul2f52a512017-12-14 11:19:41 +0530737 /* Slave is driving bit slot during control word */
738 dev_err_ratelimited(cdns->dev, "Bus clash for control word\n");
Vinod Koul2f52a512017-12-14 11:19:41 +0530739 }
740
741 if (int_status & CDNS_MCP_INT_DATA_CLASH) {
742 /*
743 * Multiple slaves trying to drive bit slot, or issue with
744 * ownership of data bits or Slave gone bonkers
745 */
746 dev_err_ratelimited(cdns->dev, "Bus clash for data word\n");
Vinod Koul2f52a512017-12-14 11:19:41 +0530747 }
748
749 if (int_status & CDNS_MCP_INT_SLAVE_MASK) {
750 /* Mask the Slave interrupt and wake thread */
751 cdns_updatel(cdns, CDNS_MCP_INTMASK,
Pierre-Louis Bossartbbb63812019-05-01 10:57:41 -0500752 CDNS_MCP_INT_SLAVE_MASK, 0);
Vinod Koul2f52a512017-12-14 11:19:41 +0530753
754 int_status &= ~CDNS_MCP_INT_SLAVE_MASK;
755 ret = IRQ_WAKE_THREAD;
756 }
757
758 cdns_writel(cdns, CDNS_MCP_INTSTAT, int_status);
759 return ret;
760}
761EXPORT_SYMBOL(sdw_cdns_irq);
762
763/**
764 * sdw_cdns_thread() - Cadence irq thread handler
765 * @irq: irq number
766 * @dev_id: irq context
767 */
768irqreturn_t sdw_cdns_thread(int irq, void *dev_id)
769{
770 struct sdw_cdns *cdns = dev_id;
771 u32 slave0, slave1;
772
Pierre-Louis Bossarteb7df4c2019-05-22 14:47:26 -0500773 dev_dbg_ratelimited(cdns->dev, "Slave status change\n");
Vinod Koul2f52a512017-12-14 11:19:41 +0530774
775 slave0 = cdns_readl(cdns, CDNS_MCP_SLAVE_INTSTAT0);
776 slave1 = cdns_readl(cdns, CDNS_MCP_SLAVE_INTSTAT1);
777
778 cdns_update_slave_status(cdns, slave0, slave1);
779 cdns_writel(cdns, CDNS_MCP_SLAVE_INTSTAT0, slave0);
780 cdns_writel(cdns, CDNS_MCP_SLAVE_INTSTAT1, slave1);
781
782 /* clear and unmask Slave interrupt now */
783 cdns_writel(cdns, CDNS_MCP_INTSTAT, CDNS_MCP_INT_SLAVE_MASK);
784 cdns_updatel(cdns, CDNS_MCP_INTMASK,
Pierre-Louis Bossartbbb63812019-05-01 10:57:41 -0500785 CDNS_MCP_INT_SLAVE_MASK, CDNS_MCP_INT_SLAVE_MASK);
Vinod Koul2f52a512017-12-14 11:19:41 +0530786
787 return IRQ_HANDLED;
788}
789EXPORT_SYMBOL(sdw_cdns_thread);
790
791/*
792 * init routines
793 */
Pierre-Louis Bossart49ea07d2019-10-22 18:54:44 -0500794
795/**
796 * sdw_cdns_exit_reset() - Program reset parameters and start bus operations
797 * @cdns: Cadence instance
798 */
799int sdw_cdns_exit_reset(struct sdw_cdns *cdns)
800{
801 /* program maximum length reset to be safe */
802 cdns_updatel(cdns, CDNS_MCP_CONTROL,
803 CDNS_MCP_CONTROL_RST_DELAY,
804 CDNS_MCP_CONTROL_RST_DELAY);
805
806 /* use hardware generated reset */
807 cdns_updatel(cdns, CDNS_MCP_CONTROL,
808 CDNS_MCP_CONTROL_HW_RST,
809 CDNS_MCP_CONTROL_HW_RST);
810
811 /* enable bus operations with clock and data */
812 cdns_updatel(cdns, CDNS_MCP_CONFIG,
813 CDNS_MCP_CONFIG_OP,
814 CDNS_MCP_CONFIG_OP_NORMAL);
815
816 /* commit changes */
817 return cdns_update_config(cdns);
818}
819EXPORT_SYMBOL(sdw_cdns_exit_reset);
820
821/**
822 * sdw_cdns_enable_interrupt() - Enable SDW interrupts and update config
823 * @cdns: Cadence instance
824 */
Pierre-Louis Bossart9e3d47f2019-10-22 18:54:47 -0500825int sdw_cdns_enable_interrupt(struct sdw_cdns *cdns, bool state)
Sanyog Kale956baa12017-12-14 11:19:42 +0530826{
Pierre-Louis Bossart9e3d47f2019-10-22 18:54:47 -0500827 u32 slave_intmask0 = 0;
828 u32 slave_intmask1 = 0;
829 u32 mask = 0;
Sanyog Kale956baa12017-12-14 11:19:42 +0530830
Pierre-Louis Bossart9e3d47f2019-10-22 18:54:47 -0500831 if (!state)
832 goto update_masks;
833
834 slave_intmask0 = CDNS_MCP_SLAVE_INTMASK0_MASK;
835 slave_intmask1 = CDNS_MCP_SLAVE_INTMASK1_MASK;
Sanyog Kale956baa12017-12-14 11:19:42 +0530836
Pierre-Louis Bossart9b5884a2019-08-05 19:55:08 -0500837 /* enable detection of all slave state changes */
838 mask = CDNS_MCP_INT_SLAVE_MASK;
839
840 /* enable detection of bus issues */
841 mask |= CDNS_MCP_INT_CTRL_CLASH | CDNS_MCP_INT_DATA_CLASH |
842 CDNS_MCP_INT_PARITY;
843
844 /* no detection of port interrupts for now */
845
846 /* enable detection of RX fifo level */
847 mask |= CDNS_MCP_INT_RX_WL;
848
849 /*
850 * CDNS_MCP_INT_IRQ needs to be set otherwise all previous
851 * settings are irrelevant
852 */
853 mask |= CDNS_MCP_INT_IRQ;
Sanyog Kale956baa12017-12-14 11:19:42 +0530854
Pierre-Louis Bossart04592dc2019-08-05 19:55:21 -0500855 if (interrupt_mask) /* parameter override */
856 mask = interrupt_mask;
Sanyog Kale956baa12017-12-14 11:19:42 +0530857
Pierre-Louis Bossart9e3d47f2019-10-22 18:54:47 -0500858update_masks:
859 cdns_writel(cdns, CDNS_MCP_SLAVE_INTMASK0, slave_intmask0);
860 cdns_writel(cdns, CDNS_MCP_SLAVE_INTMASK1, slave_intmask1);
Sanyog Kale956baa12017-12-14 11:19:42 +0530861 cdns_writel(cdns, CDNS_MCP_INTMASK, mask);
862
Pierre-Louis Bossart49ea07d2019-10-22 18:54:44 -0500863 /* commit changes */
864 return cdns_update_config(cdns);
Sanyog Kale956baa12017-12-14 11:19:42 +0530865}
866EXPORT_SYMBOL(sdw_cdns_enable_interrupt);
Vinod Koul2f52a512017-12-14 11:19:41 +0530867
Vinod Koul07abeff2018-04-26 18:38:48 +0530868static int cdns_allocate_pdi(struct sdw_cdns *cdns,
Pierre-Louis Bossartbbb63812019-05-01 10:57:41 -0500869 struct sdw_cdns_pdi **stream,
870 u32 num, u32 pdi_offset)
Vinod Koul07abeff2018-04-26 18:38:48 +0530871{
872 struct sdw_cdns_pdi *pdi;
873 int i;
874
875 if (!num)
876 return 0;
877
878 pdi = devm_kcalloc(cdns->dev, num, sizeof(*pdi), GFP_KERNEL);
879 if (!pdi)
880 return -ENOMEM;
881
882 for (i = 0; i < num; i++) {
883 pdi[i].num = i + pdi_offset;
Vinod Koul07abeff2018-04-26 18:38:48 +0530884 }
885
886 *stream = pdi;
887 return 0;
888}
889
890/**
891 * sdw_cdns_pdi_init() - PDI initialization routine
892 *
893 * @cdns: Cadence instance
894 * @config: Stream configurations
895 */
896int sdw_cdns_pdi_init(struct sdw_cdns *cdns,
Pierre-Louis Bossartbbb63812019-05-01 10:57:41 -0500897 struct sdw_cdns_stream_config config)
Vinod Koul07abeff2018-04-26 18:38:48 +0530898{
899 struct sdw_cdns_streams *stream;
Pierre-Louis Bossart57a34792019-09-16 14:23:46 -0500900 int offset;
901 int ret;
Vinod Koul07abeff2018-04-26 18:38:48 +0530902
903 cdns->pcm.num_bd = config.pcm_bd;
904 cdns->pcm.num_in = config.pcm_in;
905 cdns->pcm.num_out = config.pcm_out;
906 cdns->pdm.num_bd = config.pdm_bd;
907 cdns->pdm.num_in = config.pdm_in;
908 cdns->pdm.num_out = config.pdm_out;
909
910 /* Allocate PDIs for PCMs */
911 stream = &cdns->pcm;
912
Pierre-Louis Bossart807c15b2019-09-16 14:23:47 -0500913 /* we allocate PDI0 and PDI1 which are used for Bulk */
914 offset = 0;
Vinod Koul07abeff2018-04-26 18:38:48 +0530915
916 ret = cdns_allocate_pdi(cdns, &stream->bd,
917 stream->num_bd, offset);
918 if (ret)
919 return ret;
920
921 offset += stream->num_bd;
922
923 ret = cdns_allocate_pdi(cdns, &stream->in,
924 stream->num_in, offset);
925 if (ret)
926 return ret;
927
928 offset += stream->num_in;
929
930 ret = cdns_allocate_pdi(cdns, &stream->out,
931 stream->num_out, offset);
932 if (ret)
933 return ret;
934
935 /* Update total number of PCM PDIs */
936 stream->num_pdi = stream->num_bd + stream->num_in + stream->num_out;
937 cdns->num_ports = stream->num_pdi;
938
939 /* Allocate PDIs for PDMs */
940 stream = &cdns->pdm;
Vinod Koul07abeff2018-04-26 18:38:48 +0530941 ret = cdns_allocate_pdi(cdns, &stream->bd,
942 stream->num_bd, offset);
943 if (ret)
944 return ret;
945
946 offset += stream->num_bd;
947
948 ret = cdns_allocate_pdi(cdns, &stream->in,
949 stream->num_in, offset);
950 if (ret)
951 return ret;
952
953 offset += stream->num_in;
954
955 ret = cdns_allocate_pdi(cdns, &stream->out,
956 stream->num_out, offset);
Pierre-Louis Bossart807c15b2019-09-16 14:23:47 -0500957
958 offset += stream->num_out;
959
Vinod Koul07abeff2018-04-26 18:38:48 +0530960 if (ret)
961 return ret;
962
963 /* Update total number of PDM PDIs */
964 stream->num_pdi = stream->num_bd + stream->num_in + stream->num_out;
965 cdns->num_ports += stream->num_pdi;
966
Vinod Koul07abeff2018-04-26 18:38:48 +0530967 return 0;
968}
969EXPORT_SYMBOL(sdw_cdns_pdi_init);
970
Pierre-Louis Bossart05be59a2019-08-05 19:55:11 -0500971static u32 cdns_set_initial_frame_shape(int n_rows, int n_cols)
972{
973 u32 val;
974 int c;
975 int r;
976
977 r = sdw_find_row_index(n_rows);
978 c = sdw_find_col_index(n_cols) & CDNS_MCP_FRAME_SHAPE_COL_MASK;
979
980 val = (r << CDNS_MCP_FRAME_SHAPE_ROW_OFFSET) | c;
981
982 return val;
983}
984
Vinod Koul2f52a512017-12-14 11:19:41 +0530985/**
986 * sdw_cdns_init() - Cadence initialization
987 * @cdns: Cadence instance
988 */
Pierre-Louis Bossart3ccb8552019-10-22 18:54:48 -0500989int sdw_cdns_init(struct sdw_cdns *cdns, bool clock_stop_exit)
Vinod Koul2f52a512017-12-14 11:19:41 +0530990{
Pierre-Louis Bossart38598722019-08-05 19:55:17 -0500991 struct sdw_bus *bus = &cdns->bus;
992 struct sdw_master_prop *prop = &bus->prop;
Vinod Koul2f52a512017-12-14 11:19:41 +0530993 u32 val;
Pierre-Louis Bossart38598722019-08-05 19:55:17 -0500994 int divider;
Vinod Koul2f52a512017-12-14 11:19:41 +0530995 int ret;
996
Pierre-Louis Bossart3ccb8552019-10-22 18:54:48 -0500997 if (clock_stop_exit) {
998 ret = cdns_clear_bit(cdns, CDNS_MCP_CONTROL,
999 CDNS_MCP_CONTROL_CLK_STOP_CLR);
1000 if (ret < 0) {
1001 dev_err(cdns->dev, "Couldn't exit from clock stop\n");
1002 return ret;
1003 }
Vinod Koul2f52a512017-12-14 11:19:41 +05301004 }
1005
1006 /* Set clock divider */
Pierre-Louis Bossart38598722019-08-05 19:55:17 -05001007 divider = (prop->mclk_freq / prop->max_clk_freq) - 1;
Vinod Koul2f52a512017-12-14 11:19:41 +05301008
Rander Wanga50954e2019-08-05 19:55:18 -05001009 cdns_updatel(cdns, CDNS_MCP_CLK_CTRL0,
1010 CDNS_MCP_CLK_MCLKD_MASK, divider);
1011 cdns_updatel(cdns, CDNS_MCP_CLK_CTRL1,
1012 CDNS_MCP_CLK_MCLKD_MASK, divider);
Vinod Koul2f52a512017-12-14 11:19:41 +05301013
Pierre-Louis Bossart05be59a2019-08-05 19:55:11 -05001014 /*
1015 * Frame shape changes after initialization have to be done
1016 * with the bank switch mechanism
1017 */
1018 val = cdns_set_initial_frame_shape(prop->default_row,
1019 prop->default_col);
1020 cdns_writel(cdns, CDNS_MCP_FRAME_SHAPE_INIT, val);
Vinod Koul2f52a512017-12-14 11:19:41 +05301021
1022 /* Set SSP interval to default value */
1023 cdns_writel(cdns, CDNS_MCP_SSP_CTRL0, CDNS_DEFAULT_SSP_INTERVAL);
1024 cdns_writel(cdns, CDNS_MCP_SSP_CTRL1, CDNS_DEFAULT_SSP_INTERVAL);
1025
Pierre-Louis Bossart49ea07d2019-10-22 18:54:44 -05001026 /* flush command FIFOs */
1027 cdns_updatel(cdns, CDNS_MCP_CONTROL, CDNS_MCP_CONTROL_CMD_RST,
1028 CDNS_MCP_CONTROL_CMD_RST);
1029
Vinod Koul2f52a512017-12-14 11:19:41 +05301030 /* Set cmd accept mode */
1031 cdns_updatel(cdns, CDNS_MCP_CONTROL, CDNS_MCP_CONTROL_CMD_ACCEPT,
Pierre-Louis Bossartbbb63812019-05-01 10:57:41 -05001032 CDNS_MCP_CONTROL_CMD_ACCEPT);
Vinod Koul2f52a512017-12-14 11:19:41 +05301033
1034 /* Configure mcp config */
1035 val = cdns_readl(cdns, CDNS_MCP_CONFIG);
1036
1037 /* Set Max cmd retry to 15 */
1038 val |= CDNS_MCP_CONFIG_MCMD_RETRY;
1039
1040 /* Set frame delay between PREQ and ping frame to 15 frames */
1041 val |= 0xF << SDW_REG_SHIFT(CDNS_MCP_CONFIG_MPREQ_DELAY);
1042
1043 /* Disable auto bus release */
1044 val &= ~CDNS_MCP_CONFIG_BUS_REL;
1045
1046 /* Disable sniffer mode */
1047 val &= ~CDNS_MCP_CONFIG_SNIFFER;
1048
1049 /* Set cmd mode for Tx and Rx cmds */
1050 val &= ~CDNS_MCP_CONFIG_CMD;
1051
Vinod Koul2f52a512017-12-14 11:19:41 +05301052 cdns_writel(cdns, CDNS_MCP_CONFIG, val);
1053
Pierre-Louis Bossart49ea07d2019-10-22 18:54:44 -05001054 /* commit changes */
1055 return cdns_update_config(cdns);
Vinod Koul2f52a512017-12-14 11:19:41 +05301056}
1057EXPORT_SYMBOL(sdw_cdns_init);
1058
Vinod Koul07abeff2018-04-26 18:38:48 +05301059int cdns_bus_conf(struct sdw_bus *bus, struct sdw_bus_params *params)
1060{
Pierre-Louis Bossart38598722019-08-05 19:55:17 -05001061 struct sdw_master_prop *prop = &bus->prop;
Vinod Koul07abeff2018-04-26 18:38:48 +05301062 struct sdw_cdns *cdns = bus_to_cdns(bus);
Rander Wanga50954e2019-08-05 19:55:18 -05001063 int mcp_clkctrl_off;
Vinod Koul07abeff2018-04-26 18:38:48 +05301064 int divider;
1065
1066 if (!params->curr_dr_freq) {
Pierre-Louis Bossart17ed5be2019-05-01 10:57:45 -05001067 dev_err(cdns->dev, "NULL curr_dr_freq\n");
Vinod Koul07abeff2018-04-26 18:38:48 +05301068 return -EINVAL;
1069 }
1070
Pierre-Louis Bossart38598722019-08-05 19:55:17 -05001071 divider = prop->mclk_freq * SDW_DOUBLE_RATE_FACTOR /
1072 params->curr_dr_freq;
1073 divider--; /* divider is 1/(N+1) */
Vinod Koul07abeff2018-04-26 18:38:48 +05301074
1075 if (params->next_bank)
1076 mcp_clkctrl_off = CDNS_MCP_CLK_CTRL1;
1077 else
1078 mcp_clkctrl_off = CDNS_MCP_CLK_CTRL0;
1079
Rander Wanga50954e2019-08-05 19:55:18 -05001080 cdns_updatel(cdns, mcp_clkctrl_off, CDNS_MCP_CLK_MCLKD_MASK, divider);
Vinod Koul07abeff2018-04-26 18:38:48 +05301081
1082 return 0;
1083}
1084EXPORT_SYMBOL(cdns_bus_conf);
1085
1086static int cdns_port_params(struct sdw_bus *bus,
Pierre-Louis Bossartbbb63812019-05-01 10:57:41 -05001087 struct sdw_port_params *p_params, unsigned int bank)
Vinod Koul07abeff2018-04-26 18:38:48 +05301088{
1089 struct sdw_cdns *cdns = bus_to_cdns(bus);
1090 int dpn_config = 0, dpn_config_off;
1091
1092 if (bank)
1093 dpn_config_off = CDNS_DPN_B1_CONFIG(p_params->num);
1094 else
1095 dpn_config_off = CDNS_DPN_B0_CONFIG(p_params->num);
1096
1097 dpn_config = cdns_readl(cdns, dpn_config_off);
1098
1099 dpn_config |= ((p_params->bps - 1) <<
1100 SDW_REG_SHIFT(CDNS_DPN_CONFIG_WL));
1101 dpn_config |= (p_params->flow_mode <<
1102 SDW_REG_SHIFT(CDNS_DPN_CONFIG_PORT_FLOW));
1103 dpn_config |= (p_params->data_mode <<
1104 SDW_REG_SHIFT(CDNS_DPN_CONFIG_PORT_DAT));
1105
1106 cdns_writel(cdns, dpn_config_off, dpn_config);
1107
1108 return 0;
1109}
1110
1111static int cdns_transport_params(struct sdw_bus *bus,
Pierre-Louis Bossartbbb63812019-05-01 10:57:41 -05001112 struct sdw_transport_params *t_params,
1113 enum sdw_reg_bank bank)
Vinod Koul07abeff2018-04-26 18:38:48 +05301114{
1115 struct sdw_cdns *cdns = bus_to_cdns(bus);
1116 int dpn_offsetctrl = 0, dpn_offsetctrl_off;
1117 int dpn_config = 0, dpn_config_off;
1118 int dpn_hctrl = 0, dpn_hctrl_off;
1119 int num = t_params->port_num;
1120 int dpn_samplectrl_off;
1121
1122 /*
1123 * Note: Only full data port is supported on the Master side for
1124 * both PCM and PDM ports.
1125 */
1126
1127 if (bank) {
1128 dpn_config_off = CDNS_DPN_B1_CONFIG(num);
1129 dpn_samplectrl_off = CDNS_DPN_B1_SAMPLE_CTRL(num);
1130 dpn_hctrl_off = CDNS_DPN_B1_HCTRL(num);
1131 dpn_offsetctrl_off = CDNS_DPN_B1_OFFSET_CTRL(num);
1132 } else {
1133 dpn_config_off = CDNS_DPN_B0_CONFIG(num);
1134 dpn_samplectrl_off = CDNS_DPN_B0_SAMPLE_CTRL(num);
1135 dpn_hctrl_off = CDNS_DPN_B0_HCTRL(num);
1136 dpn_offsetctrl_off = CDNS_DPN_B0_OFFSET_CTRL(num);
1137 }
1138
1139 dpn_config = cdns_readl(cdns, dpn_config_off);
1140
1141 dpn_config |= (t_params->blk_grp_ctrl <<
1142 SDW_REG_SHIFT(CDNS_DPN_CONFIG_BGC));
1143 dpn_config |= (t_params->blk_pkg_mode <<
1144 SDW_REG_SHIFT(CDNS_DPN_CONFIG_BPM));
1145 cdns_writel(cdns, dpn_config_off, dpn_config);
1146
1147 dpn_offsetctrl |= (t_params->offset1 <<
1148 SDW_REG_SHIFT(CDNS_DPN_OFFSET_CTRL_1));
1149 dpn_offsetctrl |= (t_params->offset2 <<
1150 SDW_REG_SHIFT(CDNS_DPN_OFFSET_CTRL_2));
1151 cdns_writel(cdns, dpn_offsetctrl_off, dpn_offsetctrl);
1152
1153 dpn_hctrl |= (t_params->hstart <<
1154 SDW_REG_SHIFT(CDNS_DPN_HCTRL_HSTART));
1155 dpn_hctrl |= (t_params->hstop << SDW_REG_SHIFT(CDNS_DPN_HCTRL_HSTOP));
1156 dpn_hctrl |= (t_params->lane_ctrl <<
1157 SDW_REG_SHIFT(CDNS_DPN_HCTRL_LCTRL));
1158
1159 cdns_writel(cdns, dpn_hctrl_off, dpn_hctrl);
1160 cdns_writel(cdns, dpn_samplectrl_off, (t_params->sample_interval - 1));
1161
1162 return 0;
1163}
1164
1165static int cdns_port_enable(struct sdw_bus *bus,
Pierre-Louis Bossartbbb63812019-05-01 10:57:41 -05001166 struct sdw_enable_ch *enable_ch, unsigned int bank)
Vinod Koul07abeff2018-04-26 18:38:48 +05301167{
1168 struct sdw_cdns *cdns = bus_to_cdns(bus);
1169 int dpn_chnen_off, ch_mask;
1170
1171 if (bank)
1172 dpn_chnen_off = CDNS_DPN_B1_CH_EN(enable_ch->port_num);
1173 else
1174 dpn_chnen_off = CDNS_DPN_B0_CH_EN(enable_ch->port_num);
1175
1176 ch_mask = enable_ch->ch_mask * enable_ch->enable;
1177 cdns_writel(cdns, dpn_chnen_off, ch_mask);
1178
1179 return 0;
1180}
1181
1182static const struct sdw_master_port_ops cdns_port_ops = {
1183 .dpn_set_port_params = cdns_port_params,
1184 .dpn_set_port_transport_params = cdns_transport_params,
1185 .dpn_port_enable_ch = cdns_port_enable,
1186};
1187
Sanyog Kale956baa12017-12-14 11:19:42 +05301188/**
1189 * sdw_cdns_probe() - Cadence probe routine
1190 * @cdns: Cadence instance
1191 */
1192int sdw_cdns_probe(struct sdw_cdns *cdns)
1193{
1194 init_completion(&cdns->tx_complete);
Vinod Koul07abeff2018-04-26 18:38:48 +05301195 cdns->bus.port_ops = &cdns_port_ops;
Sanyog Kale956baa12017-12-14 11:19:42 +05301196
1197 return 0;
1198}
1199EXPORT_SYMBOL(sdw_cdns_probe);
1200
Vinod Koul5d6b3c82018-04-26 18:38:53 +05301201int cdns_set_sdw_stream(struct snd_soc_dai *dai,
Pierre-Louis Bossartbbb63812019-05-01 10:57:41 -05001202 void *stream, bool pcm, int direction)
Vinod Koul5d6b3c82018-04-26 18:38:53 +05301203{
1204 struct sdw_cdns *cdns = snd_soc_dai_get_drvdata(dai);
1205 struct sdw_cdns_dma_data *dma;
1206
1207 dma = kzalloc(sizeof(*dma), GFP_KERNEL);
1208 if (!dma)
1209 return -ENOMEM;
1210
1211 if (pcm)
1212 dma->stream_type = SDW_STREAM_PCM;
1213 else
1214 dma->stream_type = SDW_STREAM_PDM;
1215
1216 dma->bus = &cdns->bus;
1217 dma->link_id = cdns->instance;
1218
1219 dma->stream = stream;
1220
1221 if (direction == SNDRV_PCM_STREAM_PLAYBACK)
1222 dai->playback_dma_data = dma;
1223 else
1224 dai->capture_dma_data = dma;
1225
1226 return 0;
1227}
1228EXPORT_SYMBOL(cdns_set_sdw_stream);
1229
1230/**
1231 * cdns_find_pdi() - Find a free PDI
1232 *
1233 * @cdns: Cadence instance
1234 * @num: Number of PDIs
1235 * @pdi: PDI instances
1236 *
Bard Liao1b53385e2019-09-16 14:23:48 -05001237 * Find a PDI for a given PDI array. The PDI num and dai_id are
1238 * expected to match, return NULL otherwise.
Vinod Koul5d6b3c82018-04-26 18:38:53 +05301239 */
1240static struct sdw_cdns_pdi *cdns_find_pdi(struct sdw_cdns *cdns,
Pierre-Louis Bossart807c15b2019-09-16 14:23:47 -05001241 unsigned int offset,
Pierre-Louis Bossartbbb63812019-05-01 10:57:41 -05001242 unsigned int num,
Bard Liao1b53385e2019-09-16 14:23:48 -05001243 struct sdw_cdns_pdi *pdi,
1244 int dai_id)
Vinod Koul5d6b3c82018-04-26 18:38:53 +05301245{
1246 int i;
1247
Bard Liao1b53385e2019-09-16 14:23:48 -05001248 for (i = offset; i < offset + num; i++)
1249 if (pdi[i].num == dai_id)
1250 return &pdi[i];
Vinod Koul5d6b3c82018-04-26 18:38:53 +05301251
1252 return NULL;
1253}
1254
1255/**
1256 * sdw_cdns_config_stream: Configure a stream
1257 *
1258 * @cdns: Cadence instance
Vinod Koul5d6b3c82018-04-26 18:38:53 +05301259 * @ch: Channel count
1260 * @dir: Data direction
1261 * @pdi: PDI to be used
1262 */
1263void sdw_cdns_config_stream(struct sdw_cdns *cdns,
Pierre-Louis Bossartbbb63812019-05-01 10:57:41 -05001264 u32 ch, u32 dir, struct sdw_cdns_pdi *pdi)
Vinod Koul5d6b3c82018-04-26 18:38:53 +05301265{
1266 u32 offset, val = 0;
1267
1268 if (dir == SDW_DATA_DIR_RX)
1269 val = CDNS_PORTCTRL_DIRN;
1270
Pierre-Louis Bossart57a34792019-09-16 14:23:46 -05001271 offset = CDNS_PORTCTRL + pdi->num * CDNS_PORT_OFFSET;
Vinod Koul5d6b3c82018-04-26 18:38:53 +05301272 cdns_updatel(cdns, offset, CDNS_PORTCTRL_DIRN, val);
1273
Pierre-Louis Bossart57a34792019-09-16 14:23:46 -05001274 val = pdi->num;
Vinod Koul5d6b3c82018-04-26 18:38:53 +05301275 val |= ((1 << ch) - 1) << SDW_REG_SHIFT(CDNS_PDI_CONFIG_CHANNEL);
1276 cdns_writel(cdns, CDNS_PDI_CONFIG(pdi->num), val);
1277}
1278EXPORT_SYMBOL(sdw_cdns_config_stream);
1279
1280/**
Pierre-Louis Bossart57a34792019-09-16 14:23:46 -05001281 * sdw_cdns_alloc_pdi() - Allocate a PDI
Vinod Koul5d6b3c82018-04-26 18:38:53 +05301282 *
1283 * @cdns: Cadence instance
1284 * @stream: Stream to be allocated
1285 * @ch: Channel count
1286 * @dir: Data direction
1287 */
Pierre-Louis Bossart57a34792019-09-16 14:23:46 -05001288struct sdw_cdns_pdi *sdw_cdns_alloc_pdi(struct sdw_cdns *cdns,
1289 struct sdw_cdns_streams *stream,
Bard Liao1b53385e2019-09-16 14:23:48 -05001290 u32 ch, u32 dir, int dai_id)
Vinod Koul5d6b3c82018-04-26 18:38:53 +05301291{
1292 struct sdw_cdns_pdi *pdi = NULL;
1293
1294 if (dir == SDW_DATA_DIR_RX)
Bard Liao1b53385e2019-09-16 14:23:48 -05001295 pdi = cdns_find_pdi(cdns, 0, stream->num_in, stream->in,
1296 dai_id);
Vinod Koul5d6b3c82018-04-26 18:38:53 +05301297 else
Bard Liao1b53385e2019-09-16 14:23:48 -05001298 pdi = cdns_find_pdi(cdns, 0, stream->num_out, stream->out,
1299 dai_id);
Vinod Koul5d6b3c82018-04-26 18:38:53 +05301300
1301 /* check if we found a PDI, else find in bi-directional */
1302 if (!pdi)
Bard Liao1b53385e2019-09-16 14:23:48 -05001303 pdi = cdns_find_pdi(cdns, 2, stream->num_bd, stream->bd,
1304 dai_id);
Vinod Koul5d6b3c82018-04-26 18:38:53 +05301305
Pierre-Louis Bossart57a34792019-09-16 14:23:46 -05001306 if (pdi) {
1307 pdi->l_ch_num = 0;
1308 pdi->h_ch_num = ch - 1;
1309 pdi->dir = dir;
1310 pdi->ch_count = ch;
1311 }
Vinod Koul5d6b3c82018-04-26 18:38:53 +05301312
Pierre-Louis Bossart57a34792019-09-16 14:23:46 -05001313 return pdi;
Vinod Koul5d6b3c82018-04-26 18:38:53 +05301314}
Pierre-Louis Bossart57a34792019-09-16 14:23:46 -05001315EXPORT_SYMBOL(sdw_cdns_alloc_pdi);
Vinod Koul5d6b3c82018-04-26 18:38:53 +05301316
Vinod Koul2f52a512017-12-14 11:19:41 +05301317MODULE_LICENSE("Dual BSD/GPL");
1318MODULE_DESCRIPTION("Cadence Soundwire Library");