18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (C) 2018 Cadence Design Systems Inc.
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Author: Boris Brezillon <boris.brezillon@bootlin.com>
68c2ecf20Sopenharmony_ci */
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci#include <linux/bitops.h>
98c2ecf20Sopenharmony_ci#include <linux/clk.h>
108c2ecf20Sopenharmony_ci#include <linux/err.h>
118c2ecf20Sopenharmony_ci#include <linux/errno.h>
128c2ecf20Sopenharmony_ci#include <linux/i3c/master.h>
138c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
148c2ecf20Sopenharmony_ci#include <linux/io.h>
158c2ecf20Sopenharmony_ci#include <linux/iopoll.h>
168c2ecf20Sopenharmony_ci#include <linux/ioport.h>
178c2ecf20Sopenharmony_ci#include <linux/kernel.h>
188c2ecf20Sopenharmony_ci#include <linux/list.h>
198c2ecf20Sopenharmony_ci#include <linux/module.h>
208c2ecf20Sopenharmony_ci#include <linux/of.h>
218c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
228c2ecf20Sopenharmony_ci#include <linux/slab.h>
238c2ecf20Sopenharmony_ci#include <linux/spinlock.h>
248c2ecf20Sopenharmony_ci#include <linux/workqueue.h>
258c2ecf20Sopenharmony_ci#include <linux/of_device.h>
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_ci#define DEV_ID				0x0
288c2ecf20Sopenharmony_ci#define DEV_ID_I3C_MASTER		0x5034
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci#define CONF_STATUS0			0x4
318c2ecf20Sopenharmony_ci#define CONF_STATUS0_CMDR_DEPTH(x)	(4 << (((x) & GENMASK(31, 29)) >> 29))
328c2ecf20Sopenharmony_ci#define CONF_STATUS0_ECC_CHK		BIT(28)
338c2ecf20Sopenharmony_ci#define CONF_STATUS0_INTEG_CHK		BIT(27)
348c2ecf20Sopenharmony_ci#define CONF_STATUS0_CSR_DAP_CHK	BIT(26)
358c2ecf20Sopenharmony_ci#define CONF_STATUS0_TRANS_TOUT_CHK	BIT(25)
368c2ecf20Sopenharmony_ci#define CONF_STATUS0_PROT_FAULTS_CHK	BIT(24)
378c2ecf20Sopenharmony_ci#define CONF_STATUS0_GPO_NUM(x)		(((x) & GENMASK(23, 16)) >> 16)
388c2ecf20Sopenharmony_ci#define CONF_STATUS0_GPI_NUM(x)		(((x) & GENMASK(15, 8)) >> 8)
398c2ecf20Sopenharmony_ci#define CONF_STATUS0_IBIR_DEPTH(x)	(4 << (((x) & GENMASK(7, 6)) >> 7))
408c2ecf20Sopenharmony_ci#define CONF_STATUS0_SUPPORTS_DDR	BIT(5)
418c2ecf20Sopenharmony_ci#define CONF_STATUS0_SEC_MASTER		BIT(4)
428c2ecf20Sopenharmony_ci#define CONF_STATUS0_DEVS_NUM(x)	((x) & GENMASK(3, 0))
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ci#define CONF_STATUS1			0x8
458c2ecf20Sopenharmony_ci#define CONF_STATUS1_IBI_HW_RES(x)	((((x) & GENMASK(31, 28)) >> 28) + 1)
468c2ecf20Sopenharmony_ci#define CONF_STATUS1_CMD_DEPTH(x)	(4 << (((x) & GENMASK(27, 26)) >> 26))
478c2ecf20Sopenharmony_ci#define CONF_STATUS1_SLVDDR_RX_DEPTH(x)	(8 << (((x) & GENMASK(25, 21)) >> 21))
488c2ecf20Sopenharmony_ci#define CONF_STATUS1_SLVDDR_TX_DEPTH(x)	(8 << (((x) & GENMASK(20, 16)) >> 16))
498c2ecf20Sopenharmony_ci#define CONF_STATUS1_IBI_DEPTH(x)	(2 << (((x) & GENMASK(12, 10)) >> 10))
508c2ecf20Sopenharmony_ci#define CONF_STATUS1_RX_DEPTH(x)	(8 << (((x) & GENMASK(9, 5)) >> 5))
518c2ecf20Sopenharmony_ci#define CONF_STATUS1_TX_DEPTH(x)	(8 << ((x) & GENMASK(4, 0)))
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ci#define REV_ID				0xc
548c2ecf20Sopenharmony_ci#define REV_ID_VID(id)			(((id) & GENMASK(31, 20)) >> 20)
558c2ecf20Sopenharmony_ci#define REV_ID_PID(id)			(((id) & GENMASK(19, 8)) >> 8)
568c2ecf20Sopenharmony_ci#define REV_ID_REV_MAJOR(id)		(((id) & GENMASK(7, 4)) >> 4)
578c2ecf20Sopenharmony_ci#define REV_ID_REV_MINOR(id)		((id) & GENMASK(3, 0))
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_ci#define CTRL				0x10
608c2ecf20Sopenharmony_ci#define CTRL_DEV_EN			BIT(31)
618c2ecf20Sopenharmony_ci#define CTRL_HALT_EN			BIT(30)
628c2ecf20Sopenharmony_ci#define CTRL_MCS			BIT(29)
638c2ecf20Sopenharmony_ci#define CTRL_MCS_EN			BIT(28)
648c2ecf20Sopenharmony_ci#define CTRL_THD_DELAY(x)		(((x) << 24) & GENMASK(25, 24))
658c2ecf20Sopenharmony_ci#define CTRL_HJ_DISEC			BIT(8)
668c2ecf20Sopenharmony_ci#define CTRL_MST_ACK			BIT(7)
678c2ecf20Sopenharmony_ci#define CTRL_HJ_ACK			BIT(6)
688c2ecf20Sopenharmony_ci#define CTRL_HJ_INIT			BIT(5)
698c2ecf20Sopenharmony_ci#define CTRL_MST_INIT			BIT(4)
708c2ecf20Sopenharmony_ci#define CTRL_AHDR_OPT			BIT(3)
718c2ecf20Sopenharmony_ci#define CTRL_PURE_BUS_MODE		0
728c2ecf20Sopenharmony_ci#define CTRL_MIXED_FAST_BUS_MODE	2
738c2ecf20Sopenharmony_ci#define CTRL_MIXED_SLOW_BUS_MODE	3
748c2ecf20Sopenharmony_ci#define CTRL_BUS_MODE_MASK		GENMASK(1, 0)
758c2ecf20Sopenharmony_ci#define THD_DELAY_MAX			3
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_ci#define PRESCL_CTRL0			0x14
788c2ecf20Sopenharmony_ci#define PRESCL_CTRL0_I2C(x)		((x) << 16)
798c2ecf20Sopenharmony_ci#define PRESCL_CTRL0_I3C(x)		(x)
808c2ecf20Sopenharmony_ci#define PRESCL_CTRL0_I3C_MAX		GENMASK(9, 0)
818c2ecf20Sopenharmony_ci#define PRESCL_CTRL0_I2C_MAX		GENMASK(15, 0)
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci#define PRESCL_CTRL1			0x18
848c2ecf20Sopenharmony_ci#define PRESCL_CTRL1_PP_LOW_MASK	GENMASK(15, 8)
858c2ecf20Sopenharmony_ci#define PRESCL_CTRL1_PP_LOW(x)		((x) << 8)
868c2ecf20Sopenharmony_ci#define PRESCL_CTRL1_OD_LOW_MASK	GENMASK(7, 0)
878c2ecf20Sopenharmony_ci#define PRESCL_CTRL1_OD_LOW(x)		(x)
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_ci#define MST_IER				0x20
908c2ecf20Sopenharmony_ci#define MST_IDR				0x24
918c2ecf20Sopenharmony_ci#define MST_IMR				0x28
928c2ecf20Sopenharmony_ci#define MST_ICR				0x2c
938c2ecf20Sopenharmony_ci#define MST_ISR				0x30
948c2ecf20Sopenharmony_ci#define MST_INT_HALTED			BIT(18)
958c2ecf20Sopenharmony_ci#define MST_INT_MR_DONE			BIT(17)
968c2ecf20Sopenharmony_ci#define MST_INT_IMM_COMP		BIT(16)
978c2ecf20Sopenharmony_ci#define MST_INT_TX_THR			BIT(15)
988c2ecf20Sopenharmony_ci#define MST_INT_TX_OVF			BIT(14)
998c2ecf20Sopenharmony_ci#define MST_INT_IBID_THR		BIT(12)
1008c2ecf20Sopenharmony_ci#define MST_INT_IBID_UNF		BIT(11)
1018c2ecf20Sopenharmony_ci#define MST_INT_IBIR_THR		BIT(10)
1028c2ecf20Sopenharmony_ci#define MST_INT_IBIR_UNF		BIT(9)
1038c2ecf20Sopenharmony_ci#define MST_INT_IBIR_OVF		BIT(8)
1048c2ecf20Sopenharmony_ci#define MST_INT_RX_THR			BIT(7)
1058c2ecf20Sopenharmony_ci#define MST_INT_RX_UNF			BIT(6)
1068c2ecf20Sopenharmony_ci#define MST_INT_CMDD_EMP		BIT(5)
1078c2ecf20Sopenharmony_ci#define MST_INT_CMDD_THR		BIT(4)
1088c2ecf20Sopenharmony_ci#define MST_INT_CMDD_OVF		BIT(3)
1098c2ecf20Sopenharmony_ci#define MST_INT_CMDR_THR		BIT(2)
1108c2ecf20Sopenharmony_ci#define MST_INT_CMDR_UNF		BIT(1)
1118c2ecf20Sopenharmony_ci#define MST_INT_CMDR_OVF		BIT(0)
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ci#define MST_STATUS0			0x34
1148c2ecf20Sopenharmony_ci#define MST_STATUS0_IDLE		BIT(18)
1158c2ecf20Sopenharmony_ci#define MST_STATUS0_HALTED		BIT(17)
1168c2ecf20Sopenharmony_ci#define MST_STATUS0_MASTER_MODE		BIT(16)
1178c2ecf20Sopenharmony_ci#define MST_STATUS0_TX_FULL		BIT(13)
1188c2ecf20Sopenharmony_ci#define MST_STATUS0_IBID_FULL		BIT(12)
1198c2ecf20Sopenharmony_ci#define MST_STATUS0_IBIR_FULL		BIT(11)
1208c2ecf20Sopenharmony_ci#define MST_STATUS0_RX_FULL		BIT(10)
1218c2ecf20Sopenharmony_ci#define MST_STATUS0_CMDD_FULL		BIT(9)
1228c2ecf20Sopenharmony_ci#define MST_STATUS0_CMDR_FULL		BIT(8)
1238c2ecf20Sopenharmony_ci#define MST_STATUS0_TX_EMP		BIT(5)
1248c2ecf20Sopenharmony_ci#define MST_STATUS0_IBID_EMP		BIT(4)
1258c2ecf20Sopenharmony_ci#define MST_STATUS0_IBIR_EMP		BIT(3)
1268c2ecf20Sopenharmony_ci#define MST_STATUS0_RX_EMP		BIT(2)
1278c2ecf20Sopenharmony_ci#define MST_STATUS0_CMDD_EMP		BIT(1)
1288c2ecf20Sopenharmony_ci#define MST_STATUS0_CMDR_EMP		BIT(0)
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_ci#define CMDR				0x38
1318c2ecf20Sopenharmony_ci#define CMDR_NO_ERROR			0
1328c2ecf20Sopenharmony_ci#define CMDR_DDR_PREAMBLE_ERROR		1
1338c2ecf20Sopenharmony_ci#define CMDR_DDR_PARITY_ERROR		2
1348c2ecf20Sopenharmony_ci#define CMDR_DDR_RX_FIFO_OVF		3
1358c2ecf20Sopenharmony_ci#define CMDR_DDR_TX_FIFO_UNF		4
1368c2ecf20Sopenharmony_ci#define CMDR_M0_ERROR			5
1378c2ecf20Sopenharmony_ci#define CMDR_M1_ERROR			6
1388c2ecf20Sopenharmony_ci#define CMDR_M2_ERROR			7
1398c2ecf20Sopenharmony_ci#define CMDR_MST_ABORT			8
1408c2ecf20Sopenharmony_ci#define CMDR_NACK_RESP			9
1418c2ecf20Sopenharmony_ci#define CMDR_INVALID_DA			10
1428c2ecf20Sopenharmony_ci#define CMDR_DDR_DROPPED		11
1438c2ecf20Sopenharmony_ci#define CMDR_ERROR(x)			(((x) & GENMASK(27, 24)) >> 24)
1448c2ecf20Sopenharmony_ci#define CMDR_XFER_BYTES(x)		(((x) & GENMASK(19, 8)) >> 8)
1458c2ecf20Sopenharmony_ci#define CMDR_CMDID_HJACK_DISEC		0xfe
1468c2ecf20Sopenharmony_ci#define CMDR_CMDID_HJACK_ENTDAA		0xff
1478c2ecf20Sopenharmony_ci#define CMDR_CMDID(x)			((x) & GENMASK(7, 0))
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_ci#define IBIR				0x3c
1508c2ecf20Sopenharmony_ci#define IBIR_ACKED			BIT(12)
1518c2ecf20Sopenharmony_ci#define IBIR_SLVID(x)			(((x) & GENMASK(11, 8)) >> 8)
1528c2ecf20Sopenharmony_ci#define IBIR_ERROR			BIT(7)
1538c2ecf20Sopenharmony_ci#define IBIR_XFER_BYTES(x)		(((x) & GENMASK(6, 2)) >> 2)
1548c2ecf20Sopenharmony_ci#define IBIR_TYPE_IBI			0
1558c2ecf20Sopenharmony_ci#define IBIR_TYPE_HJ			1
1568c2ecf20Sopenharmony_ci#define IBIR_TYPE_MR			2
1578c2ecf20Sopenharmony_ci#define IBIR_TYPE(x)			((x) & GENMASK(1, 0))
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_ci#define SLV_IER				0x40
1608c2ecf20Sopenharmony_ci#define SLV_IDR				0x44
1618c2ecf20Sopenharmony_ci#define SLV_IMR				0x48
1628c2ecf20Sopenharmony_ci#define SLV_ICR				0x4c
1638c2ecf20Sopenharmony_ci#define SLV_ISR				0x50
1648c2ecf20Sopenharmony_ci#define SLV_INT_TM			BIT(20)
1658c2ecf20Sopenharmony_ci#define SLV_INT_ERROR			BIT(19)
1668c2ecf20Sopenharmony_ci#define SLV_INT_EVENT_UP		BIT(18)
1678c2ecf20Sopenharmony_ci#define SLV_INT_HJ_DONE			BIT(17)
1688c2ecf20Sopenharmony_ci#define SLV_INT_MR_DONE			BIT(16)
1698c2ecf20Sopenharmony_ci#define SLV_INT_DA_UPD			BIT(15)
1708c2ecf20Sopenharmony_ci#define SLV_INT_SDR_FAIL		BIT(14)
1718c2ecf20Sopenharmony_ci#define SLV_INT_DDR_FAIL		BIT(13)
1728c2ecf20Sopenharmony_ci#define SLV_INT_M_RD_ABORT		BIT(12)
1738c2ecf20Sopenharmony_ci#define SLV_INT_DDR_RX_THR		BIT(11)
1748c2ecf20Sopenharmony_ci#define SLV_INT_DDR_TX_THR		BIT(10)
1758c2ecf20Sopenharmony_ci#define SLV_INT_SDR_RX_THR		BIT(9)
1768c2ecf20Sopenharmony_ci#define SLV_INT_SDR_TX_THR		BIT(8)
1778c2ecf20Sopenharmony_ci#define SLV_INT_DDR_RX_UNF		BIT(7)
1788c2ecf20Sopenharmony_ci#define SLV_INT_DDR_TX_OVF		BIT(6)
1798c2ecf20Sopenharmony_ci#define SLV_INT_SDR_RX_UNF		BIT(5)
1808c2ecf20Sopenharmony_ci#define SLV_INT_SDR_TX_OVF		BIT(4)
1818c2ecf20Sopenharmony_ci#define SLV_INT_DDR_RD_COMP		BIT(3)
1828c2ecf20Sopenharmony_ci#define SLV_INT_DDR_WR_COMP		BIT(2)
1838c2ecf20Sopenharmony_ci#define SLV_INT_SDR_RD_COMP		BIT(1)
1848c2ecf20Sopenharmony_ci#define SLV_INT_SDR_WR_COMP		BIT(0)
1858c2ecf20Sopenharmony_ci
1868c2ecf20Sopenharmony_ci#define SLV_STATUS0			0x54
1878c2ecf20Sopenharmony_ci#define SLV_STATUS0_REG_ADDR(s)		(((s) & GENMASK(23, 16)) >> 16)
1888c2ecf20Sopenharmony_ci#define SLV_STATUS0_XFRD_BYTES(s)	((s) & GENMASK(15, 0))
1898c2ecf20Sopenharmony_ci
1908c2ecf20Sopenharmony_ci#define SLV_STATUS1			0x58
1918c2ecf20Sopenharmony_ci#define SLV_STATUS1_AS(s)		(((s) & GENMASK(21, 20)) >> 20)
1928c2ecf20Sopenharmony_ci#define SLV_STATUS1_VEN_TM		BIT(19)
1938c2ecf20Sopenharmony_ci#define SLV_STATUS1_HJ_DIS		BIT(18)
1948c2ecf20Sopenharmony_ci#define SLV_STATUS1_MR_DIS		BIT(17)
1958c2ecf20Sopenharmony_ci#define SLV_STATUS1_PROT_ERR		BIT(16)
1968c2ecf20Sopenharmony_ci#define SLV_STATUS1_DA(s)		(((s) & GENMASK(15, 9)) >> 9)
1978c2ecf20Sopenharmony_ci#define SLV_STATUS1_HAS_DA		BIT(8)
1988c2ecf20Sopenharmony_ci#define SLV_STATUS1_DDR_RX_FULL		BIT(7)
1998c2ecf20Sopenharmony_ci#define SLV_STATUS1_DDR_TX_FULL		BIT(6)
2008c2ecf20Sopenharmony_ci#define SLV_STATUS1_DDR_RX_EMPTY	BIT(5)
2018c2ecf20Sopenharmony_ci#define SLV_STATUS1_DDR_TX_EMPTY	BIT(4)
2028c2ecf20Sopenharmony_ci#define SLV_STATUS1_SDR_RX_FULL		BIT(3)
2038c2ecf20Sopenharmony_ci#define SLV_STATUS1_SDR_TX_FULL		BIT(2)
2048c2ecf20Sopenharmony_ci#define SLV_STATUS1_SDR_RX_EMPTY	BIT(1)
2058c2ecf20Sopenharmony_ci#define SLV_STATUS1_SDR_TX_EMPTY	BIT(0)
2068c2ecf20Sopenharmony_ci
2078c2ecf20Sopenharmony_ci#define CMD0_FIFO			0x60
2088c2ecf20Sopenharmony_ci#define CMD0_FIFO_IS_DDR		BIT(31)
2098c2ecf20Sopenharmony_ci#define CMD0_FIFO_IS_CCC		BIT(30)
2108c2ecf20Sopenharmony_ci#define CMD0_FIFO_BCH			BIT(29)
2118c2ecf20Sopenharmony_ci#define XMIT_BURST_STATIC_SUBADDR	0
2128c2ecf20Sopenharmony_ci#define XMIT_SINGLE_INC_SUBADDR		1
2138c2ecf20Sopenharmony_ci#define XMIT_SINGLE_STATIC_SUBADDR	2
2148c2ecf20Sopenharmony_ci#define XMIT_BURST_WITHOUT_SUBADDR	3
2158c2ecf20Sopenharmony_ci#define CMD0_FIFO_PRIV_XMIT_MODE(m)	((m) << 27)
2168c2ecf20Sopenharmony_ci#define CMD0_FIFO_SBCA			BIT(26)
2178c2ecf20Sopenharmony_ci#define CMD0_FIFO_RSBC			BIT(25)
2188c2ecf20Sopenharmony_ci#define CMD0_FIFO_IS_10B		BIT(24)
2198c2ecf20Sopenharmony_ci#define CMD0_FIFO_PL_LEN(l)		((l) << 12)
2208c2ecf20Sopenharmony_ci#define CMD0_FIFO_PL_LEN_MAX		4095
2218c2ecf20Sopenharmony_ci#define CMD0_FIFO_DEV_ADDR(a)		((a) << 1)
2228c2ecf20Sopenharmony_ci#define CMD0_FIFO_RNW			BIT(0)
2238c2ecf20Sopenharmony_ci
2248c2ecf20Sopenharmony_ci#define CMD1_FIFO			0x64
2258c2ecf20Sopenharmony_ci#define CMD1_FIFO_CMDID(id)		((id) << 24)
2268c2ecf20Sopenharmony_ci#define CMD1_FIFO_CSRADDR(a)		(a)
2278c2ecf20Sopenharmony_ci#define CMD1_FIFO_CCC(id)		(id)
2288c2ecf20Sopenharmony_ci
2298c2ecf20Sopenharmony_ci#define TX_FIFO				0x68
2308c2ecf20Sopenharmony_ci
2318c2ecf20Sopenharmony_ci#define IMD_CMD0			0x70
2328c2ecf20Sopenharmony_ci#define IMD_CMD0_PL_LEN(l)		((l) << 12)
2338c2ecf20Sopenharmony_ci#define IMD_CMD0_DEV_ADDR(a)		((a) << 1)
2348c2ecf20Sopenharmony_ci#define IMD_CMD0_RNW			BIT(0)
2358c2ecf20Sopenharmony_ci
2368c2ecf20Sopenharmony_ci#define IMD_CMD1			0x74
2378c2ecf20Sopenharmony_ci#define IMD_CMD1_CCC(id)		(id)
2388c2ecf20Sopenharmony_ci
2398c2ecf20Sopenharmony_ci#define IMD_DATA			0x78
2408c2ecf20Sopenharmony_ci#define RX_FIFO				0x80
2418c2ecf20Sopenharmony_ci#define IBI_DATA_FIFO			0x84
2428c2ecf20Sopenharmony_ci#define SLV_DDR_TX_FIFO			0x88
2438c2ecf20Sopenharmony_ci#define SLV_DDR_RX_FIFO			0x8c
2448c2ecf20Sopenharmony_ci
2458c2ecf20Sopenharmony_ci#define CMD_IBI_THR_CTRL		0x90
2468c2ecf20Sopenharmony_ci#define IBIR_THR(t)			((t) << 24)
2478c2ecf20Sopenharmony_ci#define CMDR_THR(t)			((t) << 16)
2488c2ecf20Sopenharmony_ci#define IBI_THR(t)			((t) << 8)
2498c2ecf20Sopenharmony_ci#define CMD_THR(t)			(t)
2508c2ecf20Sopenharmony_ci
2518c2ecf20Sopenharmony_ci#define TX_RX_THR_CTRL			0x94
2528c2ecf20Sopenharmony_ci#define RX_THR(t)			((t) << 16)
2538c2ecf20Sopenharmony_ci#define TX_THR(t)			(t)
2548c2ecf20Sopenharmony_ci
2558c2ecf20Sopenharmony_ci#define SLV_DDR_TX_RX_THR_CTRL		0x98
2568c2ecf20Sopenharmony_ci#define SLV_DDR_RX_THR(t)		((t) << 16)
2578c2ecf20Sopenharmony_ci#define SLV_DDR_TX_THR(t)		(t)
2588c2ecf20Sopenharmony_ci
2598c2ecf20Sopenharmony_ci#define FLUSH_CTRL			0x9c
2608c2ecf20Sopenharmony_ci#define FLUSH_IBI_RESP			BIT(23)
2618c2ecf20Sopenharmony_ci#define FLUSH_CMD_RESP			BIT(22)
2628c2ecf20Sopenharmony_ci#define FLUSH_SLV_DDR_RX_FIFO		BIT(22)
2638c2ecf20Sopenharmony_ci#define FLUSH_SLV_DDR_TX_FIFO		BIT(21)
2648c2ecf20Sopenharmony_ci#define FLUSH_IMM_FIFO			BIT(20)
2658c2ecf20Sopenharmony_ci#define FLUSH_IBI_FIFO			BIT(19)
2668c2ecf20Sopenharmony_ci#define FLUSH_RX_FIFO			BIT(18)
2678c2ecf20Sopenharmony_ci#define FLUSH_TX_FIFO			BIT(17)
2688c2ecf20Sopenharmony_ci#define FLUSH_CMD_FIFO			BIT(16)
2698c2ecf20Sopenharmony_ci
2708c2ecf20Sopenharmony_ci#define TTO_PRESCL_CTRL0		0xb0
2718c2ecf20Sopenharmony_ci#define TTO_PRESCL_CTRL0_DIVB(x)	((x) << 16)
2728c2ecf20Sopenharmony_ci#define TTO_PRESCL_CTRL0_DIVA(x)	(x)
2738c2ecf20Sopenharmony_ci
2748c2ecf20Sopenharmony_ci#define TTO_PRESCL_CTRL1		0xb4
2758c2ecf20Sopenharmony_ci#define TTO_PRESCL_CTRL1_DIVB(x)	((x) << 16)
2768c2ecf20Sopenharmony_ci#define TTO_PRESCL_CTRL1_DIVA(x)	(x)
2778c2ecf20Sopenharmony_ci
2788c2ecf20Sopenharmony_ci#define DEVS_CTRL			0xb8
2798c2ecf20Sopenharmony_ci#define DEVS_CTRL_DEV_CLR_SHIFT		16
2808c2ecf20Sopenharmony_ci#define DEVS_CTRL_DEV_CLR_ALL		GENMASK(31, 16)
2818c2ecf20Sopenharmony_ci#define DEVS_CTRL_DEV_CLR(dev)		BIT(16 + (dev))
2828c2ecf20Sopenharmony_ci#define DEVS_CTRL_DEV_ACTIVE(dev)	BIT(dev)
2838c2ecf20Sopenharmony_ci#define DEVS_CTRL_DEVS_ACTIVE_MASK	GENMASK(15, 0)
2848c2ecf20Sopenharmony_ci#define MAX_DEVS			16
2858c2ecf20Sopenharmony_ci
2868c2ecf20Sopenharmony_ci#define DEV_ID_RR0(d)			(0xc0 + ((d) * 0x10))
2878c2ecf20Sopenharmony_ci#define DEV_ID_RR0_LVR_EXT_ADDR		BIT(11)
2888c2ecf20Sopenharmony_ci#define DEV_ID_RR0_HDR_CAP		BIT(10)
2898c2ecf20Sopenharmony_ci#define DEV_ID_RR0_IS_I3C		BIT(9)
2908c2ecf20Sopenharmony_ci#define DEV_ID_RR0_DEV_ADDR_MASK	(GENMASK(6, 0) | GENMASK(15, 13))
2918c2ecf20Sopenharmony_ci#define DEV_ID_RR0_SET_DEV_ADDR(a)	(((a) & GENMASK(6, 0)) |	\
2928c2ecf20Sopenharmony_ci					 (((a) & GENMASK(9, 7)) << 6))
2938c2ecf20Sopenharmony_ci#define DEV_ID_RR0_GET_DEV_ADDR(x)	((((x) >> 1) & GENMASK(6, 0)) |	\
2948c2ecf20Sopenharmony_ci					 (((x) >> 6) & GENMASK(9, 7)))
2958c2ecf20Sopenharmony_ci
2968c2ecf20Sopenharmony_ci#define DEV_ID_RR1(d)			(0xc4 + ((d) * 0x10))
2978c2ecf20Sopenharmony_ci#define DEV_ID_RR1_PID_MSB(pid)		(pid)
2988c2ecf20Sopenharmony_ci
2998c2ecf20Sopenharmony_ci#define DEV_ID_RR2(d)			(0xc8 + ((d) * 0x10))
3008c2ecf20Sopenharmony_ci#define DEV_ID_RR2_PID_LSB(pid)		((pid) << 16)
3018c2ecf20Sopenharmony_ci#define DEV_ID_RR2_BCR(bcr)		((bcr) << 8)
3028c2ecf20Sopenharmony_ci#define DEV_ID_RR2_DCR(dcr)		(dcr)
3038c2ecf20Sopenharmony_ci#define DEV_ID_RR2_LVR(lvr)		(lvr)
3048c2ecf20Sopenharmony_ci
3058c2ecf20Sopenharmony_ci#define SIR_MAP(x)			(0x180 + ((x) * 4))
3068c2ecf20Sopenharmony_ci#define SIR_MAP_DEV_REG(d)		SIR_MAP((d) / 2)
3078c2ecf20Sopenharmony_ci#define SIR_MAP_DEV_SHIFT(d, fs)	((fs) + (((d) % 2) ? 16 : 0))
3088c2ecf20Sopenharmony_ci#define SIR_MAP_DEV_CONF_MASK(d)	(GENMASK(15, 0) << (((d) % 2) ? 16 : 0))
3098c2ecf20Sopenharmony_ci#define SIR_MAP_DEV_CONF(d, c)		((c) << (((d) % 2) ? 16 : 0))
3108c2ecf20Sopenharmony_ci#define DEV_ROLE_SLAVE			0
3118c2ecf20Sopenharmony_ci#define DEV_ROLE_MASTER			1
3128c2ecf20Sopenharmony_ci#define SIR_MAP_DEV_ROLE(role)		((role) << 14)
3138c2ecf20Sopenharmony_ci#define SIR_MAP_DEV_SLOW		BIT(13)
3148c2ecf20Sopenharmony_ci#define SIR_MAP_DEV_PL(l)		((l) << 8)
3158c2ecf20Sopenharmony_ci#define SIR_MAP_PL_MAX			GENMASK(4, 0)
3168c2ecf20Sopenharmony_ci#define SIR_MAP_DEV_DA(a)		((a) << 1)
3178c2ecf20Sopenharmony_ci#define SIR_MAP_DEV_ACK			BIT(0)
3188c2ecf20Sopenharmony_ci
3198c2ecf20Sopenharmony_ci#define GPIR_WORD(x)			(0x200 + ((x) * 4))
3208c2ecf20Sopenharmony_ci#define GPI_REG(val, id)		\
3218c2ecf20Sopenharmony_ci	(((val) >> (((id) % 4) * 8)) & GENMASK(7, 0))
3228c2ecf20Sopenharmony_ci
3238c2ecf20Sopenharmony_ci#define GPOR_WORD(x)			(0x220 + ((x) * 4))
3248c2ecf20Sopenharmony_ci#define GPO_REG(val, id)		\
3258c2ecf20Sopenharmony_ci	(((val) >> (((id) % 4) * 8)) & GENMASK(7, 0))
3268c2ecf20Sopenharmony_ci
3278c2ecf20Sopenharmony_ci#define ASF_INT_STATUS			0x300
3288c2ecf20Sopenharmony_ci#define ASF_INT_RAW_STATUS		0x304
3298c2ecf20Sopenharmony_ci#define ASF_INT_MASK			0x308
3308c2ecf20Sopenharmony_ci#define ASF_INT_TEST			0x30c
3318c2ecf20Sopenharmony_ci#define ASF_INT_FATAL_SELECT		0x310
3328c2ecf20Sopenharmony_ci#define ASF_INTEGRITY_ERR		BIT(6)
3338c2ecf20Sopenharmony_ci#define ASF_PROTOCOL_ERR		BIT(5)
3348c2ecf20Sopenharmony_ci#define ASF_TRANS_TIMEOUT_ERR		BIT(4)
3358c2ecf20Sopenharmony_ci#define ASF_CSR_ERR			BIT(3)
3368c2ecf20Sopenharmony_ci#define ASF_DAP_ERR			BIT(2)
3378c2ecf20Sopenharmony_ci#define ASF_SRAM_UNCORR_ERR		BIT(1)
3388c2ecf20Sopenharmony_ci#define ASF_SRAM_CORR_ERR		BIT(0)
3398c2ecf20Sopenharmony_ci
3408c2ecf20Sopenharmony_ci#define ASF_SRAM_CORR_FAULT_STATUS	0x320
3418c2ecf20Sopenharmony_ci#define ASF_SRAM_UNCORR_FAULT_STATUS	0x324
3428c2ecf20Sopenharmony_ci#define ASF_SRAM_CORR_FAULT_INSTANCE(x)	((x) >> 24)
3438c2ecf20Sopenharmony_ci#define ASF_SRAM_CORR_FAULT_ADDR(x)	((x) & GENMASK(23, 0))
3448c2ecf20Sopenharmony_ci
3458c2ecf20Sopenharmony_ci#define ASF_SRAM_FAULT_STATS		0x328
3468c2ecf20Sopenharmony_ci#define ASF_SRAM_FAULT_UNCORR_STATS(x)	((x) >> 16)
3478c2ecf20Sopenharmony_ci#define ASF_SRAM_FAULT_CORR_STATS(x)	((x) & GENMASK(15, 0))
3488c2ecf20Sopenharmony_ci
3498c2ecf20Sopenharmony_ci#define ASF_TRANS_TOUT_CTRL		0x330
3508c2ecf20Sopenharmony_ci#define ASF_TRANS_TOUT_EN		BIT(31)
3518c2ecf20Sopenharmony_ci#define ASF_TRANS_TOUT_VAL(x)	(x)
3528c2ecf20Sopenharmony_ci
3538c2ecf20Sopenharmony_ci#define ASF_TRANS_TOUT_FAULT_MASK	0x334
3548c2ecf20Sopenharmony_ci#define ASF_TRANS_TOUT_FAULT_STATUS	0x338
3558c2ecf20Sopenharmony_ci#define ASF_TRANS_TOUT_FAULT_APB	BIT(3)
3568c2ecf20Sopenharmony_ci#define ASF_TRANS_TOUT_FAULT_SCL_LOW	BIT(2)
3578c2ecf20Sopenharmony_ci#define ASF_TRANS_TOUT_FAULT_SCL_HIGH	BIT(1)
3588c2ecf20Sopenharmony_ci#define ASF_TRANS_TOUT_FAULT_FSCL_HIGH	BIT(0)
3598c2ecf20Sopenharmony_ci
3608c2ecf20Sopenharmony_ci#define ASF_PROTO_FAULT_MASK		0x340
3618c2ecf20Sopenharmony_ci#define ASF_PROTO_FAULT_STATUS		0x344
3628c2ecf20Sopenharmony_ci#define ASF_PROTO_FAULT_SLVSDR_RD_ABORT	BIT(31)
3638c2ecf20Sopenharmony_ci#define ASF_PROTO_FAULT_SLVDDR_FAIL	BIT(30)
3648c2ecf20Sopenharmony_ci#define ASF_PROTO_FAULT_S(x)		BIT(16 + (x))
3658c2ecf20Sopenharmony_ci#define ASF_PROTO_FAULT_MSTSDR_RD_ABORT	BIT(15)
3668c2ecf20Sopenharmony_ci#define ASF_PROTO_FAULT_MSTDDR_FAIL	BIT(14)
3678c2ecf20Sopenharmony_ci#define ASF_PROTO_FAULT_M(x)		BIT(x)
3688c2ecf20Sopenharmony_ci
3698c2ecf20Sopenharmony_cistruct cdns_i3c_master_caps {
3708c2ecf20Sopenharmony_ci	u32 cmdfifodepth;
3718c2ecf20Sopenharmony_ci	u32 cmdrfifodepth;
3728c2ecf20Sopenharmony_ci	u32 txfifodepth;
3738c2ecf20Sopenharmony_ci	u32 rxfifodepth;
3748c2ecf20Sopenharmony_ci	u32 ibirfifodepth;
3758c2ecf20Sopenharmony_ci};
3768c2ecf20Sopenharmony_ci
3778c2ecf20Sopenharmony_cistruct cdns_i3c_cmd {
3788c2ecf20Sopenharmony_ci	u32 cmd0;
3798c2ecf20Sopenharmony_ci	u32 cmd1;
3808c2ecf20Sopenharmony_ci	u32 tx_len;
3818c2ecf20Sopenharmony_ci	const void *tx_buf;
3828c2ecf20Sopenharmony_ci	u32 rx_len;
3838c2ecf20Sopenharmony_ci	void *rx_buf;
3848c2ecf20Sopenharmony_ci	u32 error;
3858c2ecf20Sopenharmony_ci};
3868c2ecf20Sopenharmony_ci
3878c2ecf20Sopenharmony_cistruct cdns_i3c_xfer {
3888c2ecf20Sopenharmony_ci	struct list_head node;
3898c2ecf20Sopenharmony_ci	struct completion comp;
3908c2ecf20Sopenharmony_ci	int ret;
3918c2ecf20Sopenharmony_ci	unsigned int ncmds;
3928c2ecf20Sopenharmony_ci	struct cdns_i3c_cmd cmds[];
3938c2ecf20Sopenharmony_ci};
3948c2ecf20Sopenharmony_ci
3958c2ecf20Sopenharmony_cistruct cdns_i3c_data {
3968c2ecf20Sopenharmony_ci	u8 thd_delay_ns;
3978c2ecf20Sopenharmony_ci};
3988c2ecf20Sopenharmony_ci
3998c2ecf20Sopenharmony_cistruct cdns_i3c_master {
4008c2ecf20Sopenharmony_ci	struct work_struct hj_work;
4018c2ecf20Sopenharmony_ci	struct i3c_master_controller base;
4028c2ecf20Sopenharmony_ci	u32 free_rr_slots;
4038c2ecf20Sopenharmony_ci	unsigned int maxdevs;
4048c2ecf20Sopenharmony_ci	struct {
4058c2ecf20Sopenharmony_ci		unsigned int num_slots;
4068c2ecf20Sopenharmony_ci		struct i3c_dev_desc **slots;
4078c2ecf20Sopenharmony_ci		spinlock_t lock;
4088c2ecf20Sopenharmony_ci	} ibi;
4098c2ecf20Sopenharmony_ci	struct {
4108c2ecf20Sopenharmony_ci		struct list_head list;
4118c2ecf20Sopenharmony_ci		struct cdns_i3c_xfer *cur;
4128c2ecf20Sopenharmony_ci		spinlock_t lock;
4138c2ecf20Sopenharmony_ci	} xferqueue;
4148c2ecf20Sopenharmony_ci	void __iomem *regs;
4158c2ecf20Sopenharmony_ci	struct clk *sysclk;
4168c2ecf20Sopenharmony_ci	struct clk *pclk;
4178c2ecf20Sopenharmony_ci	struct cdns_i3c_master_caps caps;
4188c2ecf20Sopenharmony_ci	unsigned long i3c_scl_lim;
4198c2ecf20Sopenharmony_ci	const struct cdns_i3c_data *devdata;
4208c2ecf20Sopenharmony_ci};
4218c2ecf20Sopenharmony_ci
4228c2ecf20Sopenharmony_cistatic inline struct cdns_i3c_master *
4238c2ecf20Sopenharmony_cito_cdns_i3c_master(struct i3c_master_controller *master)
4248c2ecf20Sopenharmony_ci{
4258c2ecf20Sopenharmony_ci	return container_of(master, struct cdns_i3c_master, base);
4268c2ecf20Sopenharmony_ci}
4278c2ecf20Sopenharmony_ci
4288c2ecf20Sopenharmony_cistatic void cdns_i3c_master_wr_to_tx_fifo(struct cdns_i3c_master *master,
4298c2ecf20Sopenharmony_ci					  const u8 *bytes, int nbytes)
4308c2ecf20Sopenharmony_ci{
4318c2ecf20Sopenharmony_ci	writesl(master->regs + TX_FIFO, bytes, nbytes / 4);
4328c2ecf20Sopenharmony_ci	if (nbytes & 3) {
4338c2ecf20Sopenharmony_ci		u32 tmp = 0;
4348c2ecf20Sopenharmony_ci
4358c2ecf20Sopenharmony_ci		memcpy(&tmp, bytes + (nbytes & ~3), nbytes & 3);
4368c2ecf20Sopenharmony_ci		writesl(master->regs + TX_FIFO, &tmp, 1);
4378c2ecf20Sopenharmony_ci	}
4388c2ecf20Sopenharmony_ci}
4398c2ecf20Sopenharmony_ci
4408c2ecf20Sopenharmony_cistatic void cdns_i3c_master_rd_from_rx_fifo(struct cdns_i3c_master *master,
4418c2ecf20Sopenharmony_ci					    u8 *bytes, int nbytes)
4428c2ecf20Sopenharmony_ci{
4438c2ecf20Sopenharmony_ci	readsl(master->regs + RX_FIFO, bytes, nbytes / 4);
4448c2ecf20Sopenharmony_ci	if (nbytes & 3) {
4458c2ecf20Sopenharmony_ci		u32 tmp;
4468c2ecf20Sopenharmony_ci
4478c2ecf20Sopenharmony_ci		readsl(master->regs + RX_FIFO, &tmp, 1);
4488c2ecf20Sopenharmony_ci		memcpy(bytes + (nbytes & ~3), &tmp, nbytes & 3);
4498c2ecf20Sopenharmony_ci	}
4508c2ecf20Sopenharmony_ci}
4518c2ecf20Sopenharmony_ci
4528c2ecf20Sopenharmony_cistatic bool cdns_i3c_master_supports_ccc_cmd(struct i3c_master_controller *m,
4538c2ecf20Sopenharmony_ci					     const struct i3c_ccc_cmd *cmd)
4548c2ecf20Sopenharmony_ci{
4558c2ecf20Sopenharmony_ci	if (cmd->ndests > 1)
4568c2ecf20Sopenharmony_ci		return false;
4578c2ecf20Sopenharmony_ci
4588c2ecf20Sopenharmony_ci	switch (cmd->id) {
4598c2ecf20Sopenharmony_ci	case I3C_CCC_ENEC(true):
4608c2ecf20Sopenharmony_ci	case I3C_CCC_ENEC(false):
4618c2ecf20Sopenharmony_ci	case I3C_CCC_DISEC(true):
4628c2ecf20Sopenharmony_ci	case I3C_CCC_DISEC(false):
4638c2ecf20Sopenharmony_ci	case I3C_CCC_ENTAS(0, true):
4648c2ecf20Sopenharmony_ci	case I3C_CCC_ENTAS(0, false):
4658c2ecf20Sopenharmony_ci	case I3C_CCC_RSTDAA(true):
4668c2ecf20Sopenharmony_ci	case I3C_CCC_RSTDAA(false):
4678c2ecf20Sopenharmony_ci	case I3C_CCC_ENTDAA:
4688c2ecf20Sopenharmony_ci	case I3C_CCC_SETMWL(true):
4698c2ecf20Sopenharmony_ci	case I3C_CCC_SETMWL(false):
4708c2ecf20Sopenharmony_ci	case I3C_CCC_SETMRL(true):
4718c2ecf20Sopenharmony_ci	case I3C_CCC_SETMRL(false):
4728c2ecf20Sopenharmony_ci	case I3C_CCC_DEFSLVS:
4738c2ecf20Sopenharmony_ci	case I3C_CCC_ENTHDR(0):
4748c2ecf20Sopenharmony_ci	case I3C_CCC_SETDASA:
4758c2ecf20Sopenharmony_ci	case I3C_CCC_SETNEWDA:
4768c2ecf20Sopenharmony_ci	case I3C_CCC_GETMWL:
4778c2ecf20Sopenharmony_ci	case I3C_CCC_GETMRL:
4788c2ecf20Sopenharmony_ci	case I3C_CCC_GETPID:
4798c2ecf20Sopenharmony_ci	case I3C_CCC_GETBCR:
4808c2ecf20Sopenharmony_ci	case I3C_CCC_GETDCR:
4818c2ecf20Sopenharmony_ci	case I3C_CCC_GETSTATUS:
4828c2ecf20Sopenharmony_ci	case I3C_CCC_GETACCMST:
4838c2ecf20Sopenharmony_ci	case I3C_CCC_GETMXDS:
4848c2ecf20Sopenharmony_ci	case I3C_CCC_GETHDRCAP:
4858c2ecf20Sopenharmony_ci		return true;
4868c2ecf20Sopenharmony_ci	default:
4878c2ecf20Sopenharmony_ci		break;
4888c2ecf20Sopenharmony_ci	}
4898c2ecf20Sopenharmony_ci
4908c2ecf20Sopenharmony_ci	return false;
4918c2ecf20Sopenharmony_ci}
4928c2ecf20Sopenharmony_ci
4938c2ecf20Sopenharmony_cistatic int cdns_i3c_master_disable(struct cdns_i3c_master *master)
4948c2ecf20Sopenharmony_ci{
4958c2ecf20Sopenharmony_ci	u32 status;
4968c2ecf20Sopenharmony_ci
4978c2ecf20Sopenharmony_ci	writel(readl(master->regs + CTRL) & ~CTRL_DEV_EN, master->regs + CTRL);
4988c2ecf20Sopenharmony_ci
4998c2ecf20Sopenharmony_ci	return readl_poll_timeout(master->regs + MST_STATUS0, status,
5008c2ecf20Sopenharmony_ci				  status & MST_STATUS0_IDLE, 10, 1000000);
5018c2ecf20Sopenharmony_ci}
5028c2ecf20Sopenharmony_ci
5038c2ecf20Sopenharmony_cistatic void cdns_i3c_master_enable(struct cdns_i3c_master *master)
5048c2ecf20Sopenharmony_ci{
5058c2ecf20Sopenharmony_ci	writel(readl(master->regs + CTRL) | CTRL_DEV_EN, master->regs + CTRL);
5068c2ecf20Sopenharmony_ci}
5078c2ecf20Sopenharmony_ci
5088c2ecf20Sopenharmony_cistatic struct cdns_i3c_xfer *
5098c2ecf20Sopenharmony_cicdns_i3c_master_alloc_xfer(struct cdns_i3c_master *master, unsigned int ncmds)
5108c2ecf20Sopenharmony_ci{
5118c2ecf20Sopenharmony_ci	struct cdns_i3c_xfer *xfer;
5128c2ecf20Sopenharmony_ci
5138c2ecf20Sopenharmony_ci	xfer = kzalloc(struct_size(xfer, cmds, ncmds), GFP_KERNEL);
5148c2ecf20Sopenharmony_ci	if (!xfer)
5158c2ecf20Sopenharmony_ci		return NULL;
5168c2ecf20Sopenharmony_ci
5178c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&xfer->node);
5188c2ecf20Sopenharmony_ci	xfer->ncmds = ncmds;
5198c2ecf20Sopenharmony_ci	xfer->ret = -ETIMEDOUT;
5208c2ecf20Sopenharmony_ci
5218c2ecf20Sopenharmony_ci	return xfer;
5228c2ecf20Sopenharmony_ci}
5238c2ecf20Sopenharmony_ci
5248c2ecf20Sopenharmony_cistatic void cdns_i3c_master_free_xfer(struct cdns_i3c_xfer *xfer)
5258c2ecf20Sopenharmony_ci{
5268c2ecf20Sopenharmony_ci	kfree(xfer);
5278c2ecf20Sopenharmony_ci}
5288c2ecf20Sopenharmony_ci
5298c2ecf20Sopenharmony_cistatic void cdns_i3c_master_start_xfer_locked(struct cdns_i3c_master *master)
5308c2ecf20Sopenharmony_ci{
5318c2ecf20Sopenharmony_ci	struct cdns_i3c_xfer *xfer = master->xferqueue.cur;
5328c2ecf20Sopenharmony_ci	unsigned int i;
5338c2ecf20Sopenharmony_ci
5348c2ecf20Sopenharmony_ci	if (!xfer)
5358c2ecf20Sopenharmony_ci		return;
5368c2ecf20Sopenharmony_ci
5378c2ecf20Sopenharmony_ci	writel(MST_INT_CMDD_EMP, master->regs + MST_ICR);
5388c2ecf20Sopenharmony_ci	for (i = 0; i < xfer->ncmds; i++) {
5398c2ecf20Sopenharmony_ci		struct cdns_i3c_cmd *cmd = &xfer->cmds[i];
5408c2ecf20Sopenharmony_ci
5418c2ecf20Sopenharmony_ci		cdns_i3c_master_wr_to_tx_fifo(master, cmd->tx_buf,
5428c2ecf20Sopenharmony_ci					      cmd->tx_len);
5438c2ecf20Sopenharmony_ci	}
5448c2ecf20Sopenharmony_ci
5458c2ecf20Sopenharmony_ci	for (i = 0; i < xfer->ncmds; i++) {
5468c2ecf20Sopenharmony_ci		struct cdns_i3c_cmd *cmd = &xfer->cmds[i];
5478c2ecf20Sopenharmony_ci
5488c2ecf20Sopenharmony_ci		writel(cmd->cmd1 | CMD1_FIFO_CMDID(i),
5498c2ecf20Sopenharmony_ci		       master->regs + CMD1_FIFO);
5508c2ecf20Sopenharmony_ci		writel(cmd->cmd0, master->regs + CMD0_FIFO);
5518c2ecf20Sopenharmony_ci	}
5528c2ecf20Sopenharmony_ci
5538c2ecf20Sopenharmony_ci	writel(readl(master->regs + CTRL) | CTRL_MCS,
5548c2ecf20Sopenharmony_ci	       master->regs + CTRL);
5558c2ecf20Sopenharmony_ci	writel(MST_INT_CMDD_EMP, master->regs + MST_IER);
5568c2ecf20Sopenharmony_ci}
5578c2ecf20Sopenharmony_ci
5588c2ecf20Sopenharmony_cistatic void cdns_i3c_master_end_xfer_locked(struct cdns_i3c_master *master,
5598c2ecf20Sopenharmony_ci					    u32 isr)
5608c2ecf20Sopenharmony_ci{
5618c2ecf20Sopenharmony_ci	struct cdns_i3c_xfer *xfer = master->xferqueue.cur;
5628c2ecf20Sopenharmony_ci	int i, ret = 0;
5638c2ecf20Sopenharmony_ci	u32 status0;
5648c2ecf20Sopenharmony_ci
5658c2ecf20Sopenharmony_ci	if (!xfer)
5668c2ecf20Sopenharmony_ci		return;
5678c2ecf20Sopenharmony_ci
5688c2ecf20Sopenharmony_ci	if (!(isr & MST_INT_CMDD_EMP))
5698c2ecf20Sopenharmony_ci		return;
5708c2ecf20Sopenharmony_ci
5718c2ecf20Sopenharmony_ci	writel(MST_INT_CMDD_EMP, master->regs + MST_IDR);
5728c2ecf20Sopenharmony_ci
5738c2ecf20Sopenharmony_ci	for (status0 = readl(master->regs + MST_STATUS0);
5748c2ecf20Sopenharmony_ci	     !(status0 & MST_STATUS0_CMDR_EMP);
5758c2ecf20Sopenharmony_ci	     status0 = readl(master->regs + MST_STATUS0)) {
5768c2ecf20Sopenharmony_ci		struct cdns_i3c_cmd *cmd;
5778c2ecf20Sopenharmony_ci		u32 cmdr, rx_len, id;
5788c2ecf20Sopenharmony_ci
5798c2ecf20Sopenharmony_ci		cmdr = readl(master->regs + CMDR);
5808c2ecf20Sopenharmony_ci		id = CMDR_CMDID(cmdr);
5818c2ecf20Sopenharmony_ci		if (id == CMDR_CMDID_HJACK_DISEC ||
5828c2ecf20Sopenharmony_ci		    id == CMDR_CMDID_HJACK_ENTDAA ||
5838c2ecf20Sopenharmony_ci		    WARN_ON(id >= xfer->ncmds))
5848c2ecf20Sopenharmony_ci			continue;
5858c2ecf20Sopenharmony_ci
5868c2ecf20Sopenharmony_ci		cmd = &xfer->cmds[CMDR_CMDID(cmdr)];
5878c2ecf20Sopenharmony_ci		rx_len = min_t(u32, CMDR_XFER_BYTES(cmdr), cmd->rx_len);
5888c2ecf20Sopenharmony_ci		cdns_i3c_master_rd_from_rx_fifo(master, cmd->rx_buf, rx_len);
5898c2ecf20Sopenharmony_ci		cmd->error = CMDR_ERROR(cmdr);
5908c2ecf20Sopenharmony_ci	}
5918c2ecf20Sopenharmony_ci
5928c2ecf20Sopenharmony_ci	for (i = 0; i < xfer->ncmds; i++) {
5938c2ecf20Sopenharmony_ci		switch (xfer->cmds[i].error) {
5948c2ecf20Sopenharmony_ci		case CMDR_NO_ERROR:
5958c2ecf20Sopenharmony_ci			break;
5968c2ecf20Sopenharmony_ci
5978c2ecf20Sopenharmony_ci		case CMDR_DDR_PREAMBLE_ERROR:
5988c2ecf20Sopenharmony_ci		case CMDR_DDR_PARITY_ERROR:
5998c2ecf20Sopenharmony_ci		case CMDR_M0_ERROR:
6008c2ecf20Sopenharmony_ci		case CMDR_M1_ERROR:
6018c2ecf20Sopenharmony_ci		case CMDR_M2_ERROR:
6028c2ecf20Sopenharmony_ci		case CMDR_MST_ABORT:
6038c2ecf20Sopenharmony_ci		case CMDR_NACK_RESP:
6048c2ecf20Sopenharmony_ci		case CMDR_DDR_DROPPED:
6058c2ecf20Sopenharmony_ci			ret = -EIO;
6068c2ecf20Sopenharmony_ci			break;
6078c2ecf20Sopenharmony_ci
6088c2ecf20Sopenharmony_ci		case CMDR_DDR_RX_FIFO_OVF:
6098c2ecf20Sopenharmony_ci		case CMDR_DDR_TX_FIFO_UNF:
6108c2ecf20Sopenharmony_ci			ret = -ENOSPC;
6118c2ecf20Sopenharmony_ci			break;
6128c2ecf20Sopenharmony_ci
6138c2ecf20Sopenharmony_ci		case CMDR_INVALID_DA:
6148c2ecf20Sopenharmony_ci		default:
6158c2ecf20Sopenharmony_ci			ret = -EINVAL;
6168c2ecf20Sopenharmony_ci			break;
6178c2ecf20Sopenharmony_ci		}
6188c2ecf20Sopenharmony_ci	}
6198c2ecf20Sopenharmony_ci
6208c2ecf20Sopenharmony_ci	xfer->ret = ret;
6218c2ecf20Sopenharmony_ci	complete(&xfer->comp);
6228c2ecf20Sopenharmony_ci
6238c2ecf20Sopenharmony_ci	xfer = list_first_entry_or_null(&master->xferqueue.list,
6248c2ecf20Sopenharmony_ci					struct cdns_i3c_xfer, node);
6258c2ecf20Sopenharmony_ci	if (xfer)
6268c2ecf20Sopenharmony_ci		list_del_init(&xfer->node);
6278c2ecf20Sopenharmony_ci
6288c2ecf20Sopenharmony_ci	master->xferqueue.cur = xfer;
6298c2ecf20Sopenharmony_ci	cdns_i3c_master_start_xfer_locked(master);
6308c2ecf20Sopenharmony_ci}
6318c2ecf20Sopenharmony_ci
6328c2ecf20Sopenharmony_cistatic void cdns_i3c_master_queue_xfer(struct cdns_i3c_master *master,
6338c2ecf20Sopenharmony_ci				       struct cdns_i3c_xfer *xfer)
6348c2ecf20Sopenharmony_ci{
6358c2ecf20Sopenharmony_ci	unsigned long flags;
6368c2ecf20Sopenharmony_ci
6378c2ecf20Sopenharmony_ci	init_completion(&xfer->comp);
6388c2ecf20Sopenharmony_ci	spin_lock_irqsave(&master->xferqueue.lock, flags);
6398c2ecf20Sopenharmony_ci	if (master->xferqueue.cur) {
6408c2ecf20Sopenharmony_ci		list_add_tail(&xfer->node, &master->xferqueue.list);
6418c2ecf20Sopenharmony_ci	} else {
6428c2ecf20Sopenharmony_ci		master->xferqueue.cur = xfer;
6438c2ecf20Sopenharmony_ci		cdns_i3c_master_start_xfer_locked(master);
6448c2ecf20Sopenharmony_ci	}
6458c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&master->xferqueue.lock, flags);
6468c2ecf20Sopenharmony_ci}
6478c2ecf20Sopenharmony_ci
6488c2ecf20Sopenharmony_cistatic void cdns_i3c_master_unqueue_xfer(struct cdns_i3c_master *master,
6498c2ecf20Sopenharmony_ci					 struct cdns_i3c_xfer *xfer)
6508c2ecf20Sopenharmony_ci{
6518c2ecf20Sopenharmony_ci	unsigned long flags;
6528c2ecf20Sopenharmony_ci
6538c2ecf20Sopenharmony_ci	spin_lock_irqsave(&master->xferqueue.lock, flags);
6548c2ecf20Sopenharmony_ci	if (master->xferqueue.cur == xfer) {
6558c2ecf20Sopenharmony_ci		u32 status;
6568c2ecf20Sopenharmony_ci
6578c2ecf20Sopenharmony_ci		writel(readl(master->regs + CTRL) & ~CTRL_DEV_EN,
6588c2ecf20Sopenharmony_ci		       master->regs + CTRL);
6598c2ecf20Sopenharmony_ci		readl_poll_timeout_atomic(master->regs + MST_STATUS0, status,
6608c2ecf20Sopenharmony_ci					  status & MST_STATUS0_IDLE, 10,
6618c2ecf20Sopenharmony_ci					  1000000);
6628c2ecf20Sopenharmony_ci		master->xferqueue.cur = NULL;
6638c2ecf20Sopenharmony_ci		writel(FLUSH_RX_FIFO | FLUSH_TX_FIFO | FLUSH_CMD_FIFO |
6648c2ecf20Sopenharmony_ci		       FLUSH_CMD_RESP,
6658c2ecf20Sopenharmony_ci		       master->regs + FLUSH_CTRL);
6668c2ecf20Sopenharmony_ci		writel(MST_INT_CMDD_EMP, master->regs + MST_IDR);
6678c2ecf20Sopenharmony_ci		writel(readl(master->regs + CTRL) | CTRL_DEV_EN,
6688c2ecf20Sopenharmony_ci		       master->regs + CTRL);
6698c2ecf20Sopenharmony_ci	} else {
6708c2ecf20Sopenharmony_ci		list_del_init(&xfer->node);
6718c2ecf20Sopenharmony_ci	}
6728c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&master->xferqueue.lock, flags);
6738c2ecf20Sopenharmony_ci}
6748c2ecf20Sopenharmony_ci
6758c2ecf20Sopenharmony_cistatic enum i3c_error_code cdns_i3c_cmd_get_err(struct cdns_i3c_cmd *cmd)
6768c2ecf20Sopenharmony_ci{
6778c2ecf20Sopenharmony_ci	switch (cmd->error) {
6788c2ecf20Sopenharmony_ci	case CMDR_M0_ERROR:
6798c2ecf20Sopenharmony_ci		return I3C_ERROR_M0;
6808c2ecf20Sopenharmony_ci
6818c2ecf20Sopenharmony_ci	case CMDR_M1_ERROR:
6828c2ecf20Sopenharmony_ci		return I3C_ERROR_M1;
6838c2ecf20Sopenharmony_ci
6848c2ecf20Sopenharmony_ci	case CMDR_M2_ERROR:
6858c2ecf20Sopenharmony_ci	case CMDR_NACK_RESP:
6868c2ecf20Sopenharmony_ci		return I3C_ERROR_M2;
6878c2ecf20Sopenharmony_ci
6888c2ecf20Sopenharmony_ci	default:
6898c2ecf20Sopenharmony_ci		break;
6908c2ecf20Sopenharmony_ci	}
6918c2ecf20Sopenharmony_ci
6928c2ecf20Sopenharmony_ci	return I3C_ERROR_UNKNOWN;
6938c2ecf20Sopenharmony_ci}
6948c2ecf20Sopenharmony_ci
6958c2ecf20Sopenharmony_cistatic int cdns_i3c_master_send_ccc_cmd(struct i3c_master_controller *m,
6968c2ecf20Sopenharmony_ci					struct i3c_ccc_cmd *cmd)
6978c2ecf20Sopenharmony_ci{
6988c2ecf20Sopenharmony_ci	struct cdns_i3c_master *master = to_cdns_i3c_master(m);
6998c2ecf20Sopenharmony_ci	struct cdns_i3c_xfer *xfer;
7008c2ecf20Sopenharmony_ci	struct cdns_i3c_cmd *ccmd;
7018c2ecf20Sopenharmony_ci	int ret;
7028c2ecf20Sopenharmony_ci
7038c2ecf20Sopenharmony_ci	xfer = cdns_i3c_master_alloc_xfer(master, 1);
7048c2ecf20Sopenharmony_ci	if (!xfer)
7058c2ecf20Sopenharmony_ci		return -ENOMEM;
7068c2ecf20Sopenharmony_ci
7078c2ecf20Sopenharmony_ci	ccmd = xfer->cmds;
7088c2ecf20Sopenharmony_ci	ccmd->cmd1 = CMD1_FIFO_CCC(cmd->id);
7098c2ecf20Sopenharmony_ci	ccmd->cmd0 = CMD0_FIFO_IS_CCC |
7108c2ecf20Sopenharmony_ci		     CMD0_FIFO_PL_LEN(cmd->dests[0].payload.len);
7118c2ecf20Sopenharmony_ci
7128c2ecf20Sopenharmony_ci	if (cmd->id & I3C_CCC_DIRECT)
7138c2ecf20Sopenharmony_ci		ccmd->cmd0 |= CMD0_FIFO_DEV_ADDR(cmd->dests[0].addr);
7148c2ecf20Sopenharmony_ci
7158c2ecf20Sopenharmony_ci	if (cmd->rnw) {
7168c2ecf20Sopenharmony_ci		ccmd->cmd0 |= CMD0_FIFO_RNW;
7178c2ecf20Sopenharmony_ci		ccmd->rx_buf = cmd->dests[0].payload.data;
7188c2ecf20Sopenharmony_ci		ccmd->rx_len = cmd->dests[0].payload.len;
7198c2ecf20Sopenharmony_ci	} else {
7208c2ecf20Sopenharmony_ci		ccmd->tx_buf = cmd->dests[0].payload.data;
7218c2ecf20Sopenharmony_ci		ccmd->tx_len = cmd->dests[0].payload.len;
7228c2ecf20Sopenharmony_ci	}
7238c2ecf20Sopenharmony_ci
7248c2ecf20Sopenharmony_ci	cdns_i3c_master_queue_xfer(master, xfer);
7258c2ecf20Sopenharmony_ci	if (!wait_for_completion_timeout(&xfer->comp, msecs_to_jiffies(1000)))
7268c2ecf20Sopenharmony_ci		cdns_i3c_master_unqueue_xfer(master, xfer);
7278c2ecf20Sopenharmony_ci
7288c2ecf20Sopenharmony_ci	ret = xfer->ret;
7298c2ecf20Sopenharmony_ci	cmd->err = cdns_i3c_cmd_get_err(&xfer->cmds[0]);
7308c2ecf20Sopenharmony_ci	cdns_i3c_master_free_xfer(xfer);
7318c2ecf20Sopenharmony_ci
7328c2ecf20Sopenharmony_ci	return ret;
7338c2ecf20Sopenharmony_ci}
7348c2ecf20Sopenharmony_ci
7358c2ecf20Sopenharmony_cistatic int cdns_i3c_master_priv_xfers(struct i3c_dev_desc *dev,
7368c2ecf20Sopenharmony_ci				      struct i3c_priv_xfer *xfers,
7378c2ecf20Sopenharmony_ci				      int nxfers)
7388c2ecf20Sopenharmony_ci{
7398c2ecf20Sopenharmony_ci	struct i3c_master_controller *m = i3c_dev_get_master(dev);
7408c2ecf20Sopenharmony_ci	struct cdns_i3c_master *master = to_cdns_i3c_master(m);
7418c2ecf20Sopenharmony_ci	int txslots = 0, rxslots = 0, i, ret;
7428c2ecf20Sopenharmony_ci	struct cdns_i3c_xfer *cdns_xfer;
7438c2ecf20Sopenharmony_ci
7448c2ecf20Sopenharmony_ci	for (i = 0; i < nxfers; i++) {
7458c2ecf20Sopenharmony_ci		if (xfers[i].len > CMD0_FIFO_PL_LEN_MAX)
7468c2ecf20Sopenharmony_ci			return -ENOTSUPP;
7478c2ecf20Sopenharmony_ci	}
7488c2ecf20Sopenharmony_ci
7498c2ecf20Sopenharmony_ci	if (!nxfers)
7508c2ecf20Sopenharmony_ci		return 0;
7518c2ecf20Sopenharmony_ci
7528c2ecf20Sopenharmony_ci	if (nxfers > master->caps.cmdfifodepth ||
7538c2ecf20Sopenharmony_ci	    nxfers > master->caps.cmdrfifodepth)
7548c2ecf20Sopenharmony_ci		return -ENOTSUPP;
7558c2ecf20Sopenharmony_ci
7568c2ecf20Sopenharmony_ci	/*
7578c2ecf20Sopenharmony_ci	 * First make sure that all transactions (block of transfers separated
7588c2ecf20Sopenharmony_ci	 * by a STOP marker) fit in the FIFOs.
7598c2ecf20Sopenharmony_ci	 */
7608c2ecf20Sopenharmony_ci	for (i = 0; i < nxfers; i++) {
7618c2ecf20Sopenharmony_ci		if (xfers[i].rnw)
7628c2ecf20Sopenharmony_ci			rxslots += DIV_ROUND_UP(xfers[i].len, 4);
7638c2ecf20Sopenharmony_ci		else
7648c2ecf20Sopenharmony_ci			txslots += DIV_ROUND_UP(xfers[i].len, 4);
7658c2ecf20Sopenharmony_ci	}
7668c2ecf20Sopenharmony_ci
7678c2ecf20Sopenharmony_ci	if (rxslots > master->caps.rxfifodepth ||
7688c2ecf20Sopenharmony_ci	    txslots > master->caps.txfifodepth)
7698c2ecf20Sopenharmony_ci		return -ENOTSUPP;
7708c2ecf20Sopenharmony_ci
7718c2ecf20Sopenharmony_ci	cdns_xfer = cdns_i3c_master_alloc_xfer(master, nxfers);
7728c2ecf20Sopenharmony_ci	if (!cdns_xfer)
7738c2ecf20Sopenharmony_ci		return -ENOMEM;
7748c2ecf20Sopenharmony_ci
7758c2ecf20Sopenharmony_ci	for (i = 0; i < nxfers; i++) {
7768c2ecf20Sopenharmony_ci		struct cdns_i3c_cmd *ccmd = &cdns_xfer->cmds[i];
7778c2ecf20Sopenharmony_ci		u32 pl_len = xfers[i].len;
7788c2ecf20Sopenharmony_ci
7798c2ecf20Sopenharmony_ci		ccmd->cmd0 = CMD0_FIFO_DEV_ADDR(dev->info.dyn_addr) |
7808c2ecf20Sopenharmony_ci			CMD0_FIFO_PRIV_XMIT_MODE(XMIT_BURST_WITHOUT_SUBADDR);
7818c2ecf20Sopenharmony_ci
7828c2ecf20Sopenharmony_ci		if (xfers[i].rnw) {
7838c2ecf20Sopenharmony_ci			ccmd->cmd0 |= CMD0_FIFO_RNW;
7848c2ecf20Sopenharmony_ci			ccmd->rx_buf = xfers[i].data.in;
7858c2ecf20Sopenharmony_ci			ccmd->rx_len = xfers[i].len;
7868c2ecf20Sopenharmony_ci			pl_len++;
7878c2ecf20Sopenharmony_ci		} else {
7888c2ecf20Sopenharmony_ci			ccmd->tx_buf = xfers[i].data.out;
7898c2ecf20Sopenharmony_ci			ccmd->tx_len = xfers[i].len;
7908c2ecf20Sopenharmony_ci		}
7918c2ecf20Sopenharmony_ci
7928c2ecf20Sopenharmony_ci		ccmd->cmd0 |= CMD0_FIFO_PL_LEN(pl_len);
7938c2ecf20Sopenharmony_ci
7948c2ecf20Sopenharmony_ci		if (i < nxfers - 1)
7958c2ecf20Sopenharmony_ci			ccmd->cmd0 |= CMD0_FIFO_RSBC;
7968c2ecf20Sopenharmony_ci
7978c2ecf20Sopenharmony_ci		if (!i)
7988c2ecf20Sopenharmony_ci			ccmd->cmd0 |= CMD0_FIFO_BCH;
7998c2ecf20Sopenharmony_ci	}
8008c2ecf20Sopenharmony_ci
8018c2ecf20Sopenharmony_ci	cdns_i3c_master_queue_xfer(master, cdns_xfer);
8028c2ecf20Sopenharmony_ci	if (!wait_for_completion_timeout(&cdns_xfer->comp,
8038c2ecf20Sopenharmony_ci					 msecs_to_jiffies(1000)))
8048c2ecf20Sopenharmony_ci		cdns_i3c_master_unqueue_xfer(master, cdns_xfer);
8058c2ecf20Sopenharmony_ci
8068c2ecf20Sopenharmony_ci	ret = cdns_xfer->ret;
8078c2ecf20Sopenharmony_ci
8088c2ecf20Sopenharmony_ci	for (i = 0; i < nxfers; i++)
8098c2ecf20Sopenharmony_ci		xfers[i].err = cdns_i3c_cmd_get_err(&cdns_xfer->cmds[i]);
8108c2ecf20Sopenharmony_ci
8118c2ecf20Sopenharmony_ci	cdns_i3c_master_free_xfer(cdns_xfer);
8128c2ecf20Sopenharmony_ci
8138c2ecf20Sopenharmony_ci	return ret;
8148c2ecf20Sopenharmony_ci}
8158c2ecf20Sopenharmony_ci
8168c2ecf20Sopenharmony_cistatic int cdns_i3c_master_i2c_xfers(struct i2c_dev_desc *dev,
8178c2ecf20Sopenharmony_ci				     const struct i2c_msg *xfers, int nxfers)
8188c2ecf20Sopenharmony_ci{
8198c2ecf20Sopenharmony_ci	struct i3c_master_controller *m = i2c_dev_get_master(dev);
8208c2ecf20Sopenharmony_ci	struct cdns_i3c_master *master = to_cdns_i3c_master(m);
8218c2ecf20Sopenharmony_ci	unsigned int nrxwords = 0, ntxwords = 0;
8228c2ecf20Sopenharmony_ci	struct cdns_i3c_xfer *xfer;
8238c2ecf20Sopenharmony_ci	int i, ret = 0;
8248c2ecf20Sopenharmony_ci
8258c2ecf20Sopenharmony_ci	if (nxfers > master->caps.cmdfifodepth)
8268c2ecf20Sopenharmony_ci		return -ENOTSUPP;
8278c2ecf20Sopenharmony_ci
8288c2ecf20Sopenharmony_ci	for (i = 0; i < nxfers; i++) {
8298c2ecf20Sopenharmony_ci		if (xfers[i].len > CMD0_FIFO_PL_LEN_MAX)
8308c2ecf20Sopenharmony_ci			return -ENOTSUPP;
8318c2ecf20Sopenharmony_ci
8328c2ecf20Sopenharmony_ci		if (xfers[i].flags & I2C_M_RD)
8338c2ecf20Sopenharmony_ci			nrxwords += DIV_ROUND_UP(xfers[i].len, 4);
8348c2ecf20Sopenharmony_ci		else
8358c2ecf20Sopenharmony_ci			ntxwords += DIV_ROUND_UP(xfers[i].len, 4);
8368c2ecf20Sopenharmony_ci	}
8378c2ecf20Sopenharmony_ci
8388c2ecf20Sopenharmony_ci	if (ntxwords > master->caps.txfifodepth ||
8398c2ecf20Sopenharmony_ci	    nrxwords > master->caps.rxfifodepth)
8408c2ecf20Sopenharmony_ci		return -ENOTSUPP;
8418c2ecf20Sopenharmony_ci
8428c2ecf20Sopenharmony_ci	xfer = cdns_i3c_master_alloc_xfer(master, nxfers);
8438c2ecf20Sopenharmony_ci	if (!xfer)
8448c2ecf20Sopenharmony_ci		return -ENOMEM;
8458c2ecf20Sopenharmony_ci
8468c2ecf20Sopenharmony_ci	for (i = 0; i < nxfers; i++) {
8478c2ecf20Sopenharmony_ci		struct cdns_i3c_cmd *ccmd = &xfer->cmds[i];
8488c2ecf20Sopenharmony_ci
8498c2ecf20Sopenharmony_ci		ccmd->cmd0 = CMD0_FIFO_DEV_ADDR(xfers[i].addr) |
8508c2ecf20Sopenharmony_ci			CMD0_FIFO_PL_LEN(xfers[i].len) |
8518c2ecf20Sopenharmony_ci			CMD0_FIFO_PRIV_XMIT_MODE(XMIT_BURST_WITHOUT_SUBADDR);
8528c2ecf20Sopenharmony_ci
8538c2ecf20Sopenharmony_ci		if (xfers[i].flags & I2C_M_TEN)
8548c2ecf20Sopenharmony_ci			ccmd->cmd0 |= CMD0_FIFO_IS_10B;
8558c2ecf20Sopenharmony_ci
8568c2ecf20Sopenharmony_ci		if (xfers[i].flags & I2C_M_RD) {
8578c2ecf20Sopenharmony_ci			ccmd->cmd0 |= CMD0_FIFO_RNW;
8588c2ecf20Sopenharmony_ci			ccmd->rx_buf = xfers[i].buf;
8598c2ecf20Sopenharmony_ci			ccmd->rx_len = xfers[i].len;
8608c2ecf20Sopenharmony_ci		} else {
8618c2ecf20Sopenharmony_ci			ccmd->tx_buf = xfers[i].buf;
8628c2ecf20Sopenharmony_ci			ccmd->tx_len = xfers[i].len;
8638c2ecf20Sopenharmony_ci		}
8648c2ecf20Sopenharmony_ci	}
8658c2ecf20Sopenharmony_ci
8668c2ecf20Sopenharmony_ci	cdns_i3c_master_queue_xfer(master, xfer);
8678c2ecf20Sopenharmony_ci	if (!wait_for_completion_timeout(&xfer->comp, msecs_to_jiffies(1000)))
8688c2ecf20Sopenharmony_ci		cdns_i3c_master_unqueue_xfer(master, xfer);
8698c2ecf20Sopenharmony_ci
8708c2ecf20Sopenharmony_ci	ret = xfer->ret;
8718c2ecf20Sopenharmony_ci	cdns_i3c_master_free_xfer(xfer);
8728c2ecf20Sopenharmony_ci
8738c2ecf20Sopenharmony_ci	return ret;
8748c2ecf20Sopenharmony_ci}
8758c2ecf20Sopenharmony_ci
8768c2ecf20Sopenharmony_cistruct cdns_i3c_i2c_dev_data {
8778c2ecf20Sopenharmony_ci	u16 id;
8788c2ecf20Sopenharmony_ci	s16 ibi;
8798c2ecf20Sopenharmony_ci	struct i3c_generic_ibi_pool *ibi_pool;
8808c2ecf20Sopenharmony_ci};
8818c2ecf20Sopenharmony_ci
8828c2ecf20Sopenharmony_cistatic u32 prepare_rr0_dev_address(u32 addr)
8838c2ecf20Sopenharmony_ci{
8848c2ecf20Sopenharmony_ci	u32 ret = (addr << 1) & 0xff;
8858c2ecf20Sopenharmony_ci
8868c2ecf20Sopenharmony_ci	/* RR0[7:1] = addr[6:0] */
8878c2ecf20Sopenharmony_ci	ret |= (addr & GENMASK(6, 0)) << 1;
8888c2ecf20Sopenharmony_ci
8898c2ecf20Sopenharmony_ci	/* RR0[15:13] = addr[9:7] */
8908c2ecf20Sopenharmony_ci	ret |= (addr & GENMASK(9, 7)) << 6;
8918c2ecf20Sopenharmony_ci
8928c2ecf20Sopenharmony_ci	/* RR0[0] = ~XOR(addr[6:0]) */
8938c2ecf20Sopenharmony_ci	if (!(hweight8(addr & 0x7f) & 1))
8948c2ecf20Sopenharmony_ci		ret |= 1;
8958c2ecf20Sopenharmony_ci
8968c2ecf20Sopenharmony_ci	return ret;
8978c2ecf20Sopenharmony_ci}
8988c2ecf20Sopenharmony_ci
8998c2ecf20Sopenharmony_cistatic void cdns_i3c_master_upd_i3c_addr(struct i3c_dev_desc *dev)
9008c2ecf20Sopenharmony_ci{
9018c2ecf20Sopenharmony_ci	struct i3c_master_controller *m = i3c_dev_get_master(dev);
9028c2ecf20Sopenharmony_ci	struct cdns_i3c_master *master = to_cdns_i3c_master(m);
9038c2ecf20Sopenharmony_ci	struct cdns_i3c_i2c_dev_data *data = i3c_dev_get_master_data(dev);
9048c2ecf20Sopenharmony_ci	u32 rr;
9058c2ecf20Sopenharmony_ci
9068c2ecf20Sopenharmony_ci	rr = prepare_rr0_dev_address(dev->info.dyn_addr ?
9078c2ecf20Sopenharmony_ci				     dev->info.dyn_addr :
9088c2ecf20Sopenharmony_ci				     dev->info.static_addr);
9098c2ecf20Sopenharmony_ci	writel(DEV_ID_RR0_IS_I3C | rr, master->regs + DEV_ID_RR0(data->id));
9108c2ecf20Sopenharmony_ci}
9118c2ecf20Sopenharmony_ci
9128c2ecf20Sopenharmony_cistatic int cdns_i3c_master_get_rr_slot(struct cdns_i3c_master *master,
9138c2ecf20Sopenharmony_ci				       u8 dyn_addr)
9148c2ecf20Sopenharmony_ci{
9158c2ecf20Sopenharmony_ci	unsigned long activedevs;
9168c2ecf20Sopenharmony_ci	u32 rr;
9178c2ecf20Sopenharmony_ci	int i;
9188c2ecf20Sopenharmony_ci
9198c2ecf20Sopenharmony_ci	if (!dyn_addr) {
9208c2ecf20Sopenharmony_ci		if (!master->free_rr_slots)
9218c2ecf20Sopenharmony_ci			return -ENOSPC;
9228c2ecf20Sopenharmony_ci
9238c2ecf20Sopenharmony_ci		return ffs(master->free_rr_slots) - 1;
9248c2ecf20Sopenharmony_ci	}
9258c2ecf20Sopenharmony_ci
9268c2ecf20Sopenharmony_ci	activedevs = readl(master->regs + DEVS_CTRL) & DEVS_CTRL_DEVS_ACTIVE_MASK;
9278c2ecf20Sopenharmony_ci	activedevs &= ~BIT(0);
9288c2ecf20Sopenharmony_ci
9298c2ecf20Sopenharmony_ci	for_each_set_bit(i, &activedevs, master->maxdevs + 1) {
9308c2ecf20Sopenharmony_ci		rr = readl(master->regs + DEV_ID_RR0(i));
9318c2ecf20Sopenharmony_ci		if (!(rr & DEV_ID_RR0_IS_I3C) ||
9328c2ecf20Sopenharmony_ci		    DEV_ID_RR0_GET_DEV_ADDR(rr) != dyn_addr)
9338c2ecf20Sopenharmony_ci			continue;
9348c2ecf20Sopenharmony_ci
9358c2ecf20Sopenharmony_ci		return i;
9368c2ecf20Sopenharmony_ci	}
9378c2ecf20Sopenharmony_ci
9388c2ecf20Sopenharmony_ci	return -EINVAL;
9398c2ecf20Sopenharmony_ci}
9408c2ecf20Sopenharmony_ci
9418c2ecf20Sopenharmony_cistatic int cdns_i3c_master_reattach_i3c_dev(struct i3c_dev_desc *dev,
9428c2ecf20Sopenharmony_ci					    u8 old_dyn_addr)
9438c2ecf20Sopenharmony_ci{
9448c2ecf20Sopenharmony_ci	cdns_i3c_master_upd_i3c_addr(dev);
9458c2ecf20Sopenharmony_ci
9468c2ecf20Sopenharmony_ci	return 0;
9478c2ecf20Sopenharmony_ci}
9488c2ecf20Sopenharmony_ci
9498c2ecf20Sopenharmony_cistatic int cdns_i3c_master_attach_i3c_dev(struct i3c_dev_desc *dev)
9508c2ecf20Sopenharmony_ci{
9518c2ecf20Sopenharmony_ci	struct i3c_master_controller *m = i3c_dev_get_master(dev);
9528c2ecf20Sopenharmony_ci	struct cdns_i3c_master *master = to_cdns_i3c_master(m);
9538c2ecf20Sopenharmony_ci	struct cdns_i3c_i2c_dev_data *data;
9548c2ecf20Sopenharmony_ci	int slot;
9558c2ecf20Sopenharmony_ci
9568c2ecf20Sopenharmony_ci	data = kzalloc(sizeof(*data), GFP_KERNEL);
9578c2ecf20Sopenharmony_ci	if (!data)
9588c2ecf20Sopenharmony_ci		return -ENOMEM;
9598c2ecf20Sopenharmony_ci
9608c2ecf20Sopenharmony_ci	slot = cdns_i3c_master_get_rr_slot(master, dev->info.dyn_addr);
9618c2ecf20Sopenharmony_ci	if (slot < 0) {
9628c2ecf20Sopenharmony_ci		kfree(data);
9638c2ecf20Sopenharmony_ci		return slot;
9648c2ecf20Sopenharmony_ci	}
9658c2ecf20Sopenharmony_ci
9668c2ecf20Sopenharmony_ci	data->ibi = -1;
9678c2ecf20Sopenharmony_ci	data->id = slot;
9688c2ecf20Sopenharmony_ci	i3c_dev_set_master_data(dev, data);
9698c2ecf20Sopenharmony_ci	master->free_rr_slots &= ~BIT(slot);
9708c2ecf20Sopenharmony_ci
9718c2ecf20Sopenharmony_ci	if (!dev->info.dyn_addr) {
9728c2ecf20Sopenharmony_ci		cdns_i3c_master_upd_i3c_addr(dev);
9738c2ecf20Sopenharmony_ci		writel(readl(master->regs + DEVS_CTRL) |
9748c2ecf20Sopenharmony_ci		       DEVS_CTRL_DEV_ACTIVE(data->id),
9758c2ecf20Sopenharmony_ci		       master->regs + DEVS_CTRL);
9768c2ecf20Sopenharmony_ci	}
9778c2ecf20Sopenharmony_ci
9788c2ecf20Sopenharmony_ci	return 0;
9798c2ecf20Sopenharmony_ci}
9808c2ecf20Sopenharmony_ci
9818c2ecf20Sopenharmony_cistatic void cdns_i3c_master_detach_i3c_dev(struct i3c_dev_desc *dev)
9828c2ecf20Sopenharmony_ci{
9838c2ecf20Sopenharmony_ci	struct i3c_master_controller *m = i3c_dev_get_master(dev);
9848c2ecf20Sopenharmony_ci	struct cdns_i3c_master *master = to_cdns_i3c_master(m);
9858c2ecf20Sopenharmony_ci	struct cdns_i3c_i2c_dev_data *data = i3c_dev_get_master_data(dev);
9868c2ecf20Sopenharmony_ci
9878c2ecf20Sopenharmony_ci	writel(readl(master->regs + DEVS_CTRL) |
9888c2ecf20Sopenharmony_ci	       DEVS_CTRL_DEV_CLR(data->id),
9898c2ecf20Sopenharmony_ci	       master->regs + DEVS_CTRL);
9908c2ecf20Sopenharmony_ci
9918c2ecf20Sopenharmony_ci	i3c_dev_set_master_data(dev, NULL);
9928c2ecf20Sopenharmony_ci	master->free_rr_slots |= BIT(data->id);
9938c2ecf20Sopenharmony_ci	kfree(data);
9948c2ecf20Sopenharmony_ci}
9958c2ecf20Sopenharmony_ci
9968c2ecf20Sopenharmony_cistatic int cdns_i3c_master_attach_i2c_dev(struct i2c_dev_desc *dev)
9978c2ecf20Sopenharmony_ci{
9988c2ecf20Sopenharmony_ci	struct i3c_master_controller *m = i2c_dev_get_master(dev);
9998c2ecf20Sopenharmony_ci	struct cdns_i3c_master *master = to_cdns_i3c_master(m);
10008c2ecf20Sopenharmony_ci	struct cdns_i3c_i2c_dev_data *data;
10018c2ecf20Sopenharmony_ci	int slot;
10028c2ecf20Sopenharmony_ci
10038c2ecf20Sopenharmony_ci	slot = cdns_i3c_master_get_rr_slot(master, 0);
10048c2ecf20Sopenharmony_ci	if (slot < 0)
10058c2ecf20Sopenharmony_ci		return slot;
10068c2ecf20Sopenharmony_ci
10078c2ecf20Sopenharmony_ci	data = kzalloc(sizeof(*data), GFP_KERNEL);
10088c2ecf20Sopenharmony_ci	if (!data)
10098c2ecf20Sopenharmony_ci		return -ENOMEM;
10108c2ecf20Sopenharmony_ci
10118c2ecf20Sopenharmony_ci	data->id = slot;
10128c2ecf20Sopenharmony_ci	master->free_rr_slots &= ~BIT(slot);
10138c2ecf20Sopenharmony_ci	i2c_dev_set_master_data(dev, data);
10148c2ecf20Sopenharmony_ci
10158c2ecf20Sopenharmony_ci	writel(prepare_rr0_dev_address(dev->addr),
10168c2ecf20Sopenharmony_ci	       master->regs + DEV_ID_RR0(data->id));
10178c2ecf20Sopenharmony_ci	writel(dev->lvr, master->regs + DEV_ID_RR2(data->id));
10188c2ecf20Sopenharmony_ci	writel(readl(master->regs + DEVS_CTRL) |
10198c2ecf20Sopenharmony_ci	       DEVS_CTRL_DEV_ACTIVE(data->id),
10208c2ecf20Sopenharmony_ci	       master->regs + DEVS_CTRL);
10218c2ecf20Sopenharmony_ci
10228c2ecf20Sopenharmony_ci	return 0;
10238c2ecf20Sopenharmony_ci}
10248c2ecf20Sopenharmony_ci
10258c2ecf20Sopenharmony_cistatic void cdns_i3c_master_detach_i2c_dev(struct i2c_dev_desc *dev)
10268c2ecf20Sopenharmony_ci{
10278c2ecf20Sopenharmony_ci	struct i3c_master_controller *m = i2c_dev_get_master(dev);
10288c2ecf20Sopenharmony_ci	struct cdns_i3c_master *master = to_cdns_i3c_master(m);
10298c2ecf20Sopenharmony_ci	struct cdns_i3c_i2c_dev_data *data = i2c_dev_get_master_data(dev);
10308c2ecf20Sopenharmony_ci
10318c2ecf20Sopenharmony_ci	writel(readl(master->regs + DEVS_CTRL) |
10328c2ecf20Sopenharmony_ci	       DEVS_CTRL_DEV_CLR(data->id),
10338c2ecf20Sopenharmony_ci	       master->regs + DEVS_CTRL);
10348c2ecf20Sopenharmony_ci	master->free_rr_slots |= BIT(data->id);
10358c2ecf20Sopenharmony_ci
10368c2ecf20Sopenharmony_ci	i2c_dev_set_master_data(dev, NULL);
10378c2ecf20Sopenharmony_ci	kfree(data);
10388c2ecf20Sopenharmony_ci}
10398c2ecf20Sopenharmony_ci
10408c2ecf20Sopenharmony_cistatic void cdns_i3c_master_bus_cleanup(struct i3c_master_controller *m)
10418c2ecf20Sopenharmony_ci{
10428c2ecf20Sopenharmony_ci	struct cdns_i3c_master *master = to_cdns_i3c_master(m);
10438c2ecf20Sopenharmony_ci
10448c2ecf20Sopenharmony_ci	cdns_i3c_master_disable(master);
10458c2ecf20Sopenharmony_ci}
10468c2ecf20Sopenharmony_ci
10478c2ecf20Sopenharmony_cistatic void cdns_i3c_master_dev_rr_to_info(struct cdns_i3c_master *master,
10488c2ecf20Sopenharmony_ci					   unsigned int slot,
10498c2ecf20Sopenharmony_ci					   struct i3c_device_info *info)
10508c2ecf20Sopenharmony_ci{
10518c2ecf20Sopenharmony_ci	u32 rr;
10528c2ecf20Sopenharmony_ci
10538c2ecf20Sopenharmony_ci	memset(info, 0, sizeof(*info));
10548c2ecf20Sopenharmony_ci	rr = readl(master->regs + DEV_ID_RR0(slot));
10558c2ecf20Sopenharmony_ci	info->dyn_addr = DEV_ID_RR0_GET_DEV_ADDR(rr);
10568c2ecf20Sopenharmony_ci	rr = readl(master->regs + DEV_ID_RR2(slot));
10578c2ecf20Sopenharmony_ci	info->dcr = rr;
10588c2ecf20Sopenharmony_ci	info->bcr = rr >> 8;
10598c2ecf20Sopenharmony_ci	info->pid = rr >> 16;
10608c2ecf20Sopenharmony_ci	info->pid |= (u64)readl(master->regs + DEV_ID_RR1(slot)) << 16;
10618c2ecf20Sopenharmony_ci}
10628c2ecf20Sopenharmony_ci
10638c2ecf20Sopenharmony_cistatic void cdns_i3c_master_upd_i3c_scl_lim(struct cdns_i3c_master *master)
10648c2ecf20Sopenharmony_ci{
10658c2ecf20Sopenharmony_ci	struct i3c_master_controller *m = &master->base;
10668c2ecf20Sopenharmony_ci	unsigned long i3c_lim_period, pres_step, ncycles;
10678c2ecf20Sopenharmony_ci	struct i3c_bus *bus = i3c_master_get_bus(m);
10688c2ecf20Sopenharmony_ci	unsigned long new_i3c_scl_lim = 0;
10698c2ecf20Sopenharmony_ci	struct i3c_dev_desc *dev;
10708c2ecf20Sopenharmony_ci	u32 prescl1, ctrl;
10718c2ecf20Sopenharmony_ci
10728c2ecf20Sopenharmony_ci	i3c_bus_for_each_i3cdev(bus, dev) {
10738c2ecf20Sopenharmony_ci		unsigned long max_fscl;
10748c2ecf20Sopenharmony_ci
10758c2ecf20Sopenharmony_ci		max_fscl = max(I3C_CCC_MAX_SDR_FSCL(dev->info.max_read_ds),
10768c2ecf20Sopenharmony_ci			       I3C_CCC_MAX_SDR_FSCL(dev->info.max_write_ds));
10778c2ecf20Sopenharmony_ci		switch (max_fscl) {
10788c2ecf20Sopenharmony_ci		case I3C_SDR1_FSCL_8MHZ:
10798c2ecf20Sopenharmony_ci			max_fscl = 8000000;
10808c2ecf20Sopenharmony_ci			break;
10818c2ecf20Sopenharmony_ci		case I3C_SDR2_FSCL_6MHZ:
10828c2ecf20Sopenharmony_ci			max_fscl = 6000000;
10838c2ecf20Sopenharmony_ci			break;
10848c2ecf20Sopenharmony_ci		case I3C_SDR3_FSCL_4MHZ:
10858c2ecf20Sopenharmony_ci			max_fscl = 4000000;
10868c2ecf20Sopenharmony_ci			break;
10878c2ecf20Sopenharmony_ci		case I3C_SDR4_FSCL_2MHZ:
10888c2ecf20Sopenharmony_ci			max_fscl = 2000000;
10898c2ecf20Sopenharmony_ci			break;
10908c2ecf20Sopenharmony_ci		case I3C_SDR0_FSCL_MAX:
10918c2ecf20Sopenharmony_ci		default:
10928c2ecf20Sopenharmony_ci			max_fscl = 0;
10938c2ecf20Sopenharmony_ci			break;
10948c2ecf20Sopenharmony_ci		}
10958c2ecf20Sopenharmony_ci
10968c2ecf20Sopenharmony_ci		if (max_fscl &&
10978c2ecf20Sopenharmony_ci		    (new_i3c_scl_lim > max_fscl || !new_i3c_scl_lim))
10988c2ecf20Sopenharmony_ci			new_i3c_scl_lim = max_fscl;
10998c2ecf20Sopenharmony_ci	}
11008c2ecf20Sopenharmony_ci
11018c2ecf20Sopenharmony_ci	/* Only update PRESCL_CTRL1 if the I3C SCL limitation has changed. */
11028c2ecf20Sopenharmony_ci	if (new_i3c_scl_lim == master->i3c_scl_lim)
11038c2ecf20Sopenharmony_ci		return;
11048c2ecf20Sopenharmony_ci	master->i3c_scl_lim = new_i3c_scl_lim;
11058c2ecf20Sopenharmony_ci	if (!new_i3c_scl_lim)
11068c2ecf20Sopenharmony_ci		return;
11078c2ecf20Sopenharmony_ci	pres_step = 1000000000UL / (bus->scl_rate.i3c * 4);
11088c2ecf20Sopenharmony_ci
11098c2ecf20Sopenharmony_ci	/* Configure PP_LOW to meet I3C slave limitations. */
11108c2ecf20Sopenharmony_ci	prescl1 = readl(master->regs + PRESCL_CTRL1) &
11118c2ecf20Sopenharmony_ci		  ~PRESCL_CTRL1_PP_LOW_MASK;
11128c2ecf20Sopenharmony_ci	ctrl = readl(master->regs + CTRL);
11138c2ecf20Sopenharmony_ci
11148c2ecf20Sopenharmony_ci	i3c_lim_period = DIV_ROUND_UP(1000000000, master->i3c_scl_lim);
11158c2ecf20Sopenharmony_ci	ncycles = DIV_ROUND_UP(i3c_lim_period, pres_step);
11168c2ecf20Sopenharmony_ci	if (ncycles < 4)
11178c2ecf20Sopenharmony_ci		ncycles = 0;
11188c2ecf20Sopenharmony_ci	else
11198c2ecf20Sopenharmony_ci		ncycles -= 4;
11208c2ecf20Sopenharmony_ci
11218c2ecf20Sopenharmony_ci	prescl1 |= PRESCL_CTRL1_PP_LOW(ncycles);
11228c2ecf20Sopenharmony_ci
11238c2ecf20Sopenharmony_ci	/* Disable I3C master before updating PRESCL_CTRL1. */
11248c2ecf20Sopenharmony_ci	if (ctrl & CTRL_DEV_EN)
11258c2ecf20Sopenharmony_ci		cdns_i3c_master_disable(master);
11268c2ecf20Sopenharmony_ci
11278c2ecf20Sopenharmony_ci	writel(prescl1, master->regs + PRESCL_CTRL1);
11288c2ecf20Sopenharmony_ci
11298c2ecf20Sopenharmony_ci	if (ctrl & CTRL_DEV_EN)
11308c2ecf20Sopenharmony_ci		cdns_i3c_master_enable(master);
11318c2ecf20Sopenharmony_ci}
11328c2ecf20Sopenharmony_ci
11338c2ecf20Sopenharmony_cistatic int cdns_i3c_master_do_daa(struct i3c_master_controller *m)
11348c2ecf20Sopenharmony_ci{
11358c2ecf20Sopenharmony_ci	struct cdns_i3c_master *master = to_cdns_i3c_master(m);
11368c2ecf20Sopenharmony_ci	unsigned long olddevs, newdevs;
11378c2ecf20Sopenharmony_ci	int ret, slot;
11388c2ecf20Sopenharmony_ci	u8 addrs[MAX_DEVS] = { };
11398c2ecf20Sopenharmony_ci	u8 last_addr = 0;
11408c2ecf20Sopenharmony_ci
11418c2ecf20Sopenharmony_ci	olddevs = readl(master->regs + DEVS_CTRL) & DEVS_CTRL_DEVS_ACTIVE_MASK;
11428c2ecf20Sopenharmony_ci	olddevs |= BIT(0);
11438c2ecf20Sopenharmony_ci
11448c2ecf20Sopenharmony_ci	/* Prepare RR slots before launching DAA. */
11458c2ecf20Sopenharmony_ci	for_each_clear_bit(slot, &olddevs, master->maxdevs + 1) {
11468c2ecf20Sopenharmony_ci		ret = i3c_master_get_free_addr(m, last_addr + 1);
11478c2ecf20Sopenharmony_ci		if (ret < 0)
11488c2ecf20Sopenharmony_ci			return -ENOSPC;
11498c2ecf20Sopenharmony_ci
11508c2ecf20Sopenharmony_ci		last_addr = ret;
11518c2ecf20Sopenharmony_ci		addrs[slot] = last_addr;
11528c2ecf20Sopenharmony_ci		writel(prepare_rr0_dev_address(last_addr) | DEV_ID_RR0_IS_I3C,
11538c2ecf20Sopenharmony_ci		       master->regs + DEV_ID_RR0(slot));
11548c2ecf20Sopenharmony_ci		writel(0, master->regs + DEV_ID_RR1(slot));
11558c2ecf20Sopenharmony_ci		writel(0, master->regs + DEV_ID_RR2(slot));
11568c2ecf20Sopenharmony_ci	}
11578c2ecf20Sopenharmony_ci
11588c2ecf20Sopenharmony_ci	ret = i3c_master_entdaa_locked(&master->base);
11598c2ecf20Sopenharmony_ci	if (ret && ret != I3C_ERROR_M2)
11608c2ecf20Sopenharmony_ci		return ret;
11618c2ecf20Sopenharmony_ci
11628c2ecf20Sopenharmony_ci	newdevs = readl(master->regs + DEVS_CTRL) & DEVS_CTRL_DEVS_ACTIVE_MASK;
11638c2ecf20Sopenharmony_ci	newdevs &= ~olddevs;
11648c2ecf20Sopenharmony_ci
11658c2ecf20Sopenharmony_ci	/*
11668c2ecf20Sopenharmony_ci	 * Clear all retaining registers filled during DAA. We already
11678c2ecf20Sopenharmony_ci	 * have the addressed assigned to them in the addrs array.
11688c2ecf20Sopenharmony_ci	 */
11698c2ecf20Sopenharmony_ci	for_each_set_bit(slot, &newdevs, master->maxdevs + 1)
11708c2ecf20Sopenharmony_ci		i3c_master_add_i3c_dev_locked(m, addrs[slot]);
11718c2ecf20Sopenharmony_ci
11728c2ecf20Sopenharmony_ci	/*
11738c2ecf20Sopenharmony_ci	 * Clear slots that ended up not being used. Can be caused by I3C
11748c2ecf20Sopenharmony_ci	 * device creation failure or when the I3C device was already known
11758c2ecf20Sopenharmony_ci	 * by the system but with a different address (in this case the device
11768c2ecf20Sopenharmony_ci	 * already has a slot and does not need a new one).
11778c2ecf20Sopenharmony_ci	 */
11788c2ecf20Sopenharmony_ci	writel(readl(master->regs + DEVS_CTRL) |
11798c2ecf20Sopenharmony_ci	       master->free_rr_slots << DEVS_CTRL_DEV_CLR_SHIFT,
11808c2ecf20Sopenharmony_ci	       master->regs + DEVS_CTRL);
11818c2ecf20Sopenharmony_ci
11828c2ecf20Sopenharmony_ci	i3c_master_defslvs_locked(&master->base);
11838c2ecf20Sopenharmony_ci
11848c2ecf20Sopenharmony_ci	cdns_i3c_master_upd_i3c_scl_lim(master);
11858c2ecf20Sopenharmony_ci
11868c2ecf20Sopenharmony_ci	/* Unmask Hot-Join and Mastership request interrupts. */
11878c2ecf20Sopenharmony_ci	i3c_master_enec_locked(m, I3C_BROADCAST_ADDR,
11888c2ecf20Sopenharmony_ci			       I3C_CCC_EVENT_HJ | I3C_CCC_EVENT_MR);
11898c2ecf20Sopenharmony_ci
11908c2ecf20Sopenharmony_ci	return 0;
11918c2ecf20Sopenharmony_ci}
11928c2ecf20Sopenharmony_ci
11938c2ecf20Sopenharmony_cistatic u8 cdns_i3c_master_calculate_thd_delay(struct cdns_i3c_master *master)
11948c2ecf20Sopenharmony_ci{
11958c2ecf20Sopenharmony_ci	unsigned long sysclk_rate = clk_get_rate(master->sysclk);
11968c2ecf20Sopenharmony_ci	u8 thd_delay = DIV_ROUND_UP(master->devdata->thd_delay_ns,
11978c2ecf20Sopenharmony_ci				    (NSEC_PER_SEC / sysclk_rate));
11988c2ecf20Sopenharmony_ci
11998c2ecf20Sopenharmony_ci	/* Every value greater than 3 is not valid. */
12008c2ecf20Sopenharmony_ci	if (thd_delay > THD_DELAY_MAX)
12018c2ecf20Sopenharmony_ci		thd_delay = THD_DELAY_MAX;
12028c2ecf20Sopenharmony_ci
12038c2ecf20Sopenharmony_ci	/* CTLR_THD_DEL value is encoded. */
12048c2ecf20Sopenharmony_ci	return (THD_DELAY_MAX - thd_delay);
12058c2ecf20Sopenharmony_ci}
12068c2ecf20Sopenharmony_ci
12078c2ecf20Sopenharmony_cistatic int cdns_i3c_master_bus_init(struct i3c_master_controller *m)
12088c2ecf20Sopenharmony_ci{
12098c2ecf20Sopenharmony_ci	struct cdns_i3c_master *master = to_cdns_i3c_master(m);
12108c2ecf20Sopenharmony_ci	unsigned long pres_step, sysclk_rate, max_i2cfreq;
12118c2ecf20Sopenharmony_ci	struct i3c_bus *bus = i3c_master_get_bus(m);
12128c2ecf20Sopenharmony_ci	u32 ctrl, prescl0, prescl1, pres, low;
12138c2ecf20Sopenharmony_ci	struct i3c_device_info info = { };
12148c2ecf20Sopenharmony_ci	int ret, ncycles;
12158c2ecf20Sopenharmony_ci
12168c2ecf20Sopenharmony_ci	switch (bus->mode) {
12178c2ecf20Sopenharmony_ci	case I3C_BUS_MODE_PURE:
12188c2ecf20Sopenharmony_ci		ctrl = CTRL_PURE_BUS_MODE;
12198c2ecf20Sopenharmony_ci		break;
12208c2ecf20Sopenharmony_ci
12218c2ecf20Sopenharmony_ci	case I3C_BUS_MODE_MIXED_FAST:
12228c2ecf20Sopenharmony_ci		ctrl = CTRL_MIXED_FAST_BUS_MODE;
12238c2ecf20Sopenharmony_ci		break;
12248c2ecf20Sopenharmony_ci
12258c2ecf20Sopenharmony_ci	case I3C_BUS_MODE_MIXED_SLOW:
12268c2ecf20Sopenharmony_ci		ctrl = CTRL_MIXED_SLOW_BUS_MODE;
12278c2ecf20Sopenharmony_ci		break;
12288c2ecf20Sopenharmony_ci
12298c2ecf20Sopenharmony_ci	default:
12308c2ecf20Sopenharmony_ci		return -EINVAL;
12318c2ecf20Sopenharmony_ci	}
12328c2ecf20Sopenharmony_ci
12338c2ecf20Sopenharmony_ci	sysclk_rate = clk_get_rate(master->sysclk);
12348c2ecf20Sopenharmony_ci	if (!sysclk_rate)
12358c2ecf20Sopenharmony_ci		return -EINVAL;
12368c2ecf20Sopenharmony_ci
12378c2ecf20Sopenharmony_ci	pres = DIV_ROUND_UP(sysclk_rate, (bus->scl_rate.i3c * 4)) - 1;
12388c2ecf20Sopenharmony_ci	if (pres > PRESCL_CTRL0_I3C_MAX)
12398c2ecf20Sopenharmony_ci		return -ERANGE;
12408c2ecf20Sopenharmony_ci
12418c2ecf20Sopenharmony_ci	bus->scl_rate.i3c = sysclk_rate / ((pres + 1) * 4);
12428c2ecf20Sopenharmony_ci
12438c2ecf20Sopenharmony_ci	prescl0 = PRESCL_CTRL0_I3C(pres);
12448c2ecf20Sopenharmony_ci
12458c2ecf20Sopenharmony_ci	low = ((I3C_BUS_TLOW_OD_MIN_NS * sysclk_rate) / (pres + 1)) - 2;
12468c2ecf20Sopenharmony_ci	prescl1 = PRESCL_CTRL1_OD_LOW(low);
12478c2ecf20Sopenharmony_ci
12488c2ecf20Sopenharmony_ci	max_i2cfreq = bus->scl_rate.i2c;
12498c2ecf20Sopenharmony_ci
12508c2ecf20Sopenharmony_ci	pres = (sysclk_rate / (max_i2cfreq * 5)) - 1;
12518c2ecf20Sopenharmony_ci	if (pres > PRESCL_CTRL0_I2C_MAX)
12528c2ecf20Sopenharmony_ci		return -ERANGE;
12538c2ecf20Sopenharmony_ci
12548c2ecf20Sopenharmony_ci	bus->scl_rate.i2c = sysclk_rate / ((pres + 1) * 5);
12558c2ecf20Sopenharmony_ci
12568c2ecf20Sopenharmony_ci	prescl0 |= PRESCL_CTRL0_I2C(pres);
12578c2ecf20Sopenharmony_ci	writel(prescl0, master->regs + PRESCL_CTRL0);
12588c2ecf20Sopenharmony_ci
12598c2ecf20Sopenharmony_ci	/* Calculate OD and PP low. */
12608c2ecf20Sopenharmony_ci	pres_step = 1000000000 / (bus->scl_rate.i3c * 4);
12618c2ecf20Sopenharmony_ci	ncycles = DIV_ROUND_UP(I3C_BUS_TLOW_OD_MIN_NS, pres_step) - 2;
12628c2ecf20Sopenharmony_ci	if (ncycles < 0)
12638c2ecf20Sopenharmony_ci		ncycles = 0;
12648c2ecf20Sopenharmony_ci	prescl1 = PRESCL_CTRL1_OD_LOW(ncycles);
12658c2ecf20Sopenharmony_ci	writel(prescl1, master->regs + PRESCL_CTRL1);
12668c2ecf20Sopenharmony_ci
12678c2ecf20Sopenharmony_ci	/* Get an address for the master. */
12688c2ecf20Sopenharmony_ci	ret = i3c_master_get_free_addr(m, 0);
12698c2ecf20Sopenharmony_ci	if (ret < 0)
12708c2ecf20Sopenharmony_ci		return ret;
12718c2ecf20Sopenharmony_ci
12728c2ecf20Sopenharmony_ci	writel(prepare_rr0_dev_address(ret) | DEV_ID_RR0_IS_I3C,
12738c2ecf20Sopenharmony_ci	       master->regs + DEV_ID_RR0(0));
12748c2ecf20Sopenharmony_ci
12758c2ecf20Sopenharmony_ci	cdns_i3c_master_dev_rr_to_info(master, 0, &info);
12768c2ecf20Sopenharmony_ci	if (info.bcr & I3C_BCR_HDR_CAP)
12778c2ecf20Sopenharmony_ci		info.hdr_cap = I3C_CCC_HDR_MODE(I3C_HDR_DDR);
12788c2ecf20Sopenharmony_ci
12798c2ecf20Sopenharmony_ci	ret = i3c_master_set_info(&master->base, &info);
12808c2ecf20Sopenharmony_ci	if (ret)
12818c2ecf20Sopenharmony_ci		return ret;
12828c2ecf20Sopenharmony_ci
12838c2ecf20Sopenharmony_ci	/*
12848c2ecf20Sopenharmony_ci	 * Enable Hot-Join, and, when a Hot-Join request happens, disable all
12858c2ecf20Sopenharmony_ci	 * events coming from this device.
12868c2ecf20Sopenharmony_ci	 *
12878c2ecf20Sopenharmony_ci	 * We will issue ENTDAA afterwards from the threaded IRQ handler.
12888c2ecf20Sopenharmony_ci	 */
12898c2ecf20Sopenharmony_ci	ctrl |= CTRL_HJ_ACK | CTRL_HJ_DISEC | CTRL_HALT_EN | CTRL_MCS_EN;
12908c2ecf20Sopenharmony_ci
12918c2ecf20Sopenharmony_ci	/*
12928c2ecf20Sopenharmony_ci	 * Configure data hold delay based on device-specific data.
12938c2ecf20Sopenharmony_ci	 *
12948c2ecf20Sopenharmony_ci	 * MIPI I3C Specification 1.0 defines non-zero minimal tHD_PP timing on
12958c2ecf20Sopenharmony_ci	 * master output. This setting allows to meet this timing on master's
12968c2ecf20Sopenharmony_ci	 * SoC outputs, regardless of PCB balancing.
12978c2ecf20Sopenharmony_ci	 */
12988c2ecf20Sopenharmony_ci	ctrl |= CTRL_THD_DELAY(cdns_i3c_master_calculate_thd_delay(master));
12998c2ecf20Sopenharmony_ci	writel(ctrl, master->regs + CTRL);
13008c2ecf20Sopenharmony_ci
13018c2ecf20Sopenharmony_ci	cdns_i3c_master_enable(master);
13028c2ecf20Sopenharmony_ci
13038c2ecf20Sopenharmony_ci	return 0;
13048c2ecf20Sopenharmony_ci}
13058c2ecf20Sopenharmony_ci
13068c2ecf20Sopenharmony_cistatic void cdns_i3c_master_handle_ibi(struct cdns_i3c_master *master,
13078c2ecf20Sopenharmony_ci				       u32 ibir)
13088c2ecf20Sopenharmony_ci{
13098c2ecf20Sopenharmony_ci	struct cdns_i3c_i2c_dev_data *data;
13108c2ecf20Sopenharmony_ci	bool data_consumed = false;
13118c2ecf20Sopenharmony_ci	struct i3c_ibi_slot *slot;
13128c2ecf20Sopenharmony_ci	u32 id = IBIR_SLVID(ibir);
13138c2ecf20Sopenharmony_ci	struct i3c_dev_desc *dev;
13148c2ecf20Sopenharmony_ci	size_t nbytes;
13158c2ecf20Sopenharmony_ci	u8 *buf;
13168c2ecf20Sopenharmony_ci
13178c2ecf20Sopenharmony_ci	/*
13188c2ecf20Sopenharmony_ci	 * FIXME: maybe we should report the FIFO OVF errors to the upper
13198c2ecf20Sopenharmony_ci	 * layer.
13208c2ecf20Sopenharmony_ci	 */
13218c2ecf20Sopenharmony_ci	if (id >= master->ibi.num_slots || (ibir & IBIR_ERROR))
13228c2ecf20Sopenharmony_ci		goto out;
13238c2ecf20Sopenharmony_ci
13248c2ecf20Sopenharmony_ci	dev = master->ibi.slots[id];
13258c2ecf20Sopenharmony_ci	spin_lock(&master->ibi.lock);
13268c2ecf20Sopenharmony_ci
13278c2ecf20Sopenharmony_ci	data = i3c_dev_get_master_data(dev);
13288c2ecf20Sopenharmony_ci	slot = i3c_generic_ibi_get_free_slot(data->ibi_pool);
13298c2ecf20Sopenharmony_ci	if (!slot)
13308c2ecf20Sopenharmony_ci		goto out_unlock;
13318c2ecf20Sopenharmony_ci
13328c2ecf20Sopenharmony_ci	buf = slot->data;
13338c2ecf20Sopenharmony_ci
13348c2ecf20Sopenharmony_ci	nbytes = IBIR_XFER_BYTES(ibir);
13358c2ecf20Sopenharmony_ci	readsl(master->regs + IBI_DATA_FIFO, buf, nbytes / 4);
13368c2ecf20Sopenharmony_ci	if (nbytes % 3) {
13378c2ecf20Sopenharmony_ci		u32 tmp = __raw_readl(master->regs + IBI_DATA_FIFO);
13388c2ecf20Sopenharmony_ci
13398c2ecf20Sopenharmony_ci		memcpy(buf + (nbytes & ~3), &tmp, nbytes & 3);
13408c2ecf20Sopenharmony_ci	}
13418c2ecf20Sopenharmony_ci
13428c2ecf20Sopenharmony_ci	slot->len = min_t(unsigned int, IBIR_XFER_BYTES(ibir),
13438c2ecf20Sopenharmony_ci			  dev->ibi->max_payload_len);
13448c2ecf20Sopenharmony_ci	i3c_master_queue_ibi(dev, slot);
13458c2ecf20Sopenharmony_ci	data_consumed = true;
13468c2ecf20Sopenharmony_ci
13478c2ecf20Sopenharmony_ciout_unlock:
13488c2ecf20Sopenharmony_ci	spin_unlock(&master->ibi.lock);
13498c2ecf20Sopenharmony_ci
13508c2ecf20Sopenharmony_ciout:
13518c2ecf20Sopenharmony_ci	/* Consume data from the FIFO if it's not been done already. */
13528c2ecf20Sopenharmony_ci	if (!data_consumed) {
13538c2ecf20Sopenharmony_ci		int i;
13548c2ecf20Sopenharmony_ci
13558c2ecf20Sopenharmony_ci		for (i = 0; i < IBIR_XFER_BYTES(ibir); i += 4)
13568c2ecf20Sopenharmony_ci			readl(master->regs + IBI_DATA_FIFO);
13578c2ecf20Sopenharmony_ci	}
13588c2ecf20Sopenharmony_ci}
13598c2ecf20Sopenharmony_ci
13608c2ecf20Sopenharmony_cistatic void cnds_i3c_master_demux_ibis(struct cdns_i3c_master *master)
13618c2ecf20Sopenharmony_ci{
13628c2ecf20Sopenharmony_ci	u32 status0;
13638c2ecf20Sopenharmony_ci
13648c2ecf20Sopenharmony_ci	writel(MST_INT_IBIR_THR, master->regs + MST_ICR);
13658c2ecf20Sopenharmony_ci
13668c2ecf20Sopenharmony_ci	for (status0 = readl(master->regs + MST_STATUS0);
13678c2ecf20Sopenharmony_ci	     !(status0 & MST_STATUS0_IBIR_EMP);
13688c2ecf20Sopenharmony_ci	     status0 = readl(master->regs + MST_STATUS0)) {
13698c2ecf20Sopenharmony_ci		u32 ibir = readl(master->regs + IBIR);
13708c2ecf20Sopenharmony_ci
13718c2ecf20Sopenharmony_ci		switch (IBIR_TYPE(ibir)) {
13728c2ecf20Sopenharmony_ci		case IBIR_TYPE_IBI:
13738c2ecf20Sopenharmony_ci			cdns_i3c_master_handle_ibi(master, ibir);
13748c2ecf20Sopenharmony_ci			break;
13758c2ecf20Sopenharmony_ci
13768c2ecf20Sopenharmony_ci		case IBIR_TYPE_HJ:
13778c2ecf20Sopenharmony_ci			WARN_ON(IBIR_XFER_BYTES(ibir) || (ibir & IBIR_ERROR));
13788c2ecf20Sopenharmony_ci			queue_work(master->base.wq, &master->hj_work);
13798c2ecf20Sopenharmony_ci			break;
13808c2ecf20Sopenharmony_ci
13818c2ecf20Sopenharmony_ci		case IBIR_TYPE_MR:
13828c2ecf20Sopenharmony_ci			WARN_ON(IBIR_XFER_BYTES(ibir) || (ibir & IBIR_ERROR));
13838c2ecf20Sopenharmony_ci		default:
13848c2ecf20Sopenharmony_ci			break;
13858c2ecf20Sopenharmony_ci		}
13868c2ecf20Sopenharmony_ci	}
13878c2ecf20Sopenharmony_ci}
13888c2ecf20Sopenharmony_ci
13898c2ecf20Sopenharmony_cistatic irqreturn_t cdns_i3c_master_interrupt(int irq, void *data)
13908c2ecf20Sopenharmony_ci{
13918c2ecf20Sopenharmony_ci	struct cdns_i3c_master *master = data;
13928c2ecf20Sopenharmony_ci	u32 status;
13938c2ecf20Sopenharmony_ci
13948c2ecf20Sopenharmony_ci	status = readl(master->regs + MST_ISR);
13958c2ecf20Sopenharmony_ci	if (!(status & readl(master->regs + MST_IMR)))
13968c2ecf20Sopenharmony_ci		return IRQ_NONE;
13978c2ecf20Sopenharmony_ci
13988c2ecf20Sopenharmony_ci	spin_lock(&master->xferqueue.lock);
13998c2ecf20Sopenharmony_ci	cdns_i3c_master_end_xfer_locked(master, status);
14008c2ecf20Sopenharmony_ci	spin_unlock(&master->xferqueue.lock);
14018c2ecf20Sopenharmony_ci
14028c2ecf20Sopenharmony_ci	if (status & MST_INT_IBIR_THR)
14038c2ecf20Sopenharmony_ci		cnds_i3c_master_demux_ibis(master);
14048c2ecf20Sopenharmony_ci
14058c2ecf20Sopenharmony_ci	return IRQ_HANDLED;
14068c2ecf20Sopenharmony_ci}
14078c2ecf20Sopenharmony_ci
14088c2ecf20Sopenharmony_cistatic int cdns_i3c_master_disable_ibi(struct i3c_dev_desc *dev)
14098c2ecf20Sopenharmony_ci{
14108c2ecf20Sopenharmony_ci	struct i3c_master_controller *m = i3c_dev_get_master(dev);
14118c2ecf20Sopenharmony_ci	struct cdns_i3c_master *master = to_cdns_i3c_master(m);
14128c2ecf20Sopenharmony_ci	struct cdns_i3c_i2c_dev_data *data = i3c_dev_get_master_data(dev);
14138c2ecf20Sopenharmony_ci	unsigned long flags;
14148c2ecf20Sopenharmony_ci	u32 sirmap;
14158c2ecf20Sopenharmony_ci	int ret;
14168c2ecf20Sopenharmony_ci
14178c2ecf20Sopenharmony_ci	ret = i3c_master_disec_locked(m, dev->info.dyn_addr,
14188c2ecf20Sopenharmony_ci				      I3C_CCC_EVENT_SIR);
14198c2ecf20Sopenharmony_ci	if (ret)
14208c2ecf20Sopenharmony_ci		return ret;
14218c2ecf20Sopenharmony_ci
14228c2ecf20Sopenharmony_ci	spin_lock_irqsave(&master->ibi.lock, flags);
14238c2ecf20Sopenharmony_ci	sirmap = readl(master->regs + SIR_MAP_DEV_REG(data->ibi));
14248c2ecf20Sopenharmony_ci	sirmap &= ~SIR_MAP_DEV_CONF_MASK(data->ibi);
14258c2ecf20Sopenharmony_ci	sirmap |= SIR_MAP_DEV_CONF(data->ibi,
14268c2ecf20Sopenharmony_ci				   SIR_MAP_DEV_DA(I3C_BROADCAST_ADDR));
14278c2ecf20Sopenharmony_ci	writel(sirmap, master->regs + SIR_MAP_DEV_REG(data->ibi));
14288c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&master->ibi.lock, flags);
14298c2ecf20Sopenharmony_ci
14308c2ecf20Sopenharmony_ci	return ret;
14318c2ecf20Sopenharmony_ci}
14328c2ecf20Sopenharmony_ci
14338c2ecf20Sopenharmony_cistatic int cdns_i3c_master_enable_ibi(struct i3c_dev_desc *dev)
14348c2ecf20Sopenharmony_ci{
14358c2ecf20Sopenharmony_ci	struct i3c_master_controller *m = i3c_dev_get_master(dev);
14368c2ecf20Sopenharmony_ci	struct cdns_i3c_master *master = to_cdns_i3c_master(m);
14378c2ecf20Sopenharmony_ci	struct cdns_i3c_i2c_dev_data *data = i3c_dev_get_master_data(dev);
14388c2ecf20Sopenharmony_ci	unsigned long flags;
14398c2ecf20Sopenharmony_ci	u32 sircfg, sirmap;
14408c2ecf20Sopenharmony_ci	int ret;
14418c2ecf20Sopenharmony_ci
14428c2ecf20Sopenharmony_ci	spin_lock_irqsave(&master->ibi.lock, flags);
14438c2ecf20Sopenharmony_ci	sirmap = readl(master->regs + SIR_MAP_DEV_REG(data->ibi));
14448c2ecf20Sopenharmony_ci	sirmap &= ~SIR_MAP_DEV_CONF_MASK(data->ibi);
14458c2ecf20Sopenharmony_ci	sircfg = SIR_MAP_DEV_ROLE(dev->info.bcr >> 6) |
14468c2ecf20Sopenharmony_ci		 SIR_MAP_DEV_DA(dev->info.dyn_addr) |
14478c2ecf20Sopenharmony_ci		 SIR_MAP_DEV_PL(dev->info.max_ibi_len) |
14488c2ecf20Sopenharmony_ci		 SIR_MAP_DEV_ACK;
14498c2ecf20Sopenharmony_ci
14508c2ecf20Sopenharmony_ci	if (dev->info.bcr & I3C_BCR_MAX_DATA_SPEED_LIM)
14518c2ecf20Sopenharmony_ci		sircfg |= SIR_MAP_DEV_SLOW;
14528c2ecf20Sopenharmony_ci
14538c2ecf20Sopenharmony_ci	sirmap |= SIR_MAP_DEV_CONF(data->ibi, sircfg);
14548c2ecf20Sopenharmony_ci	writel(sirmap, master->regs + SIR_MAP_DEV_REG(data->ibi));
14558c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&master->ibi.lock, flags);
14568c2ecf20Sopenharmony_ci
14578c2ecf20Sopenharmony_ci	ret = i3c_master_enec_locked(m, dev->info.dyn_addr,
14588c2ecf20Sopenharmony_ci				     I3C_CCC_EVENT_SIR);
14598c2ecf20Sopenharmony_ci	if (ret) {
14608c2ecf20Sopenharmony_ci		spin_lock_irqsave(&master->ibi.lock, flags);
14618c2ecf20Sopenharmony_ci		sirmap = readl(master->regs + SIR_MAP_DEV_REG(data->ibi));
14628c2ecf20Sopenharmony_ci		sirmap &= ~SIR_MAP_DEV_CONF_MASK(data->ibi);
14638c2ecf20Sopenharmony_ci		sirmap |= SIR_MAP_DEV_CONF(data->ibi,
14648c2ecf20Sopenharmony_ci					   SIR_MAP_DEV_DA(I3C_BROADCAST_ADDR));
14658c2ecf20Sopenharmony_ci		writel(sirmap, master->regs + SIR_MAP_DEV_REG(data->ibi));
14668c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&master->ibi.lock, flags);
14678c2ecf20Sopenharmony_ci	}
14688c2ecf20Sopenharmony_ci
14698c2ecf20Sopenharmony_ci	return ret;
14708c2ecf20Sopenharmony_ci}
14718c2ecf20Sopenharmony_ci
14728c2ecf20Sopenharmony_cistatic int cdns_i3c_master_request_ibi(struct i3c_dev_desc *dev,
14738c2ecf20Sopenharmony_ci				       const struct i3c_ibi_setup *req)
14748c2ecf20Sopenharmony_ci{
14758c2ecf20Sopenharmony_ci	struct i3c_master_controller *m = i3c_dev_get_master(dev);
14768c2ecf20Sopenharmony_ci	struct cdns_i3c_master *master = to_cdns_i3c_master(m);
14778c2ecf20Sopenharmony_ci	struct cdns_i3c_i2c_dev_data *data = i3c_dev_get_master_data(dev);
14788c2ecf20Sopenharmony_ci	unsigned long flags;
14798c2ecf20Sopenharmony_ci	unsigned int i;
14808c2ecf20Sopenharmony_ci
14818c2ecf20Sopenharmony_ci	data->ibi_pool = i3c_generic_ibi_alloc_pool(dev, req);
14828c2ecf20Sopenharmony_ci	if (IS_ERR(data->ibi_pool))
14838c2ecf20Sopenharmony_ci		return PTR_ERR(data->ibi_pool);
14848c2ecf20Sopenharmony_ci
14858c2ecf20Sopenharmony_ci	spin_lock_irqsave(&master->ibi.lock, flags);
14868c2ecf20Sopenharmony_ci	for (i = 0; i < master->ibi.num_slots; i++) {
14878c2ecf20Sopenharmony_ci		if (!master->ibi.slots[i]) {
14888c2ecf20Sopenharmony_ci			data->ibi = i;
14898c2ecf20Sopenharmony_ci			master->ibi.slots[i] = dev;
14908c2ecf20Sopenharmony_ci			break;
14918c2ecf20Sopenharmony_ci		}
14928c2ecf20Sopenharmony_ci	}
14938c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&master->ibi.lock, flags);
14948c2ecf20Sopenharmony_ci
14958c2ecf20Sopenharmony_ci	if (i < master->ibi.num_slots)
14968c2ecf20Sopenharmony_ci		return 0;
14978c2ecf20Sopenharmony_ci
14988c2ecf20Sopenharmony_ci	i3c_generic_ibi_free_pool(data->ibi_pool);
14998c2ecf20Sopenharmony_ci	data->ibi_pool = NULL;
15008c2ecf20Sopenharmony_ci
15018c2ecf20Sopenharmony_ci	return -ENOSPC;
15028c2ecf20Sopenharmony_ci}
15038c2ecf20Sopenharmony_ci
15048c2ecf20Sopenharmony_cistatic void cdns_i3c_master_free_ibi(struct i3c_dev_desc *dev)
15058c2ecf20Sopenharmony_ci{
15068c2ecf20Sopenharmony_ci	struct i3c_master_controller *m = i3c_dev_get_master(dev);
15078c2ecf20Sopenharmony_ci	struct cdns_i3c_master *master = to_cdns_i3c_master(m);
15088c2ecf20Sopenharmony_ci	struct cdns_i3c_i2c_dev_data *data = i3c_dev_get_master_data(dev);
15098c2ecf20Sopenharmony_ci	unsigned long flags;
15108c2ecf20Sopenharmony_ci
15118c2ecf20Sopenharmony_ci	spin_lock_irqsave(&master->ibi.lock, flags);
15128c2ecf20Sopenharmony_ci	master->ibi.slots[data->ibi] = NULL;
15138c2ecf20Sopenharmony_ci	data->ibi = -1;
15148c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&master->ibi.lock, flags);
15158c2ecf20Sopenharmony_ci
15168c2ecf20Sopenharmony_ci	i3c_generic_ibi_free_pool(data->ibi_pool);
15178c2ecf20Sopenharmony_ci}
15188c2ecf20Sopenharmony_ci
15198c2ecf20Sopenharmony_cistatic void cdns_i3c_master_recycle_ibi_slot(struct i3c_dev_desc *dev,
15208c2ecf20Sopenharmony_ci					     struct i3c_ibi_slot *slot)
15218c2ecf20Sopenharmony_ci{
15228c2ecf20Sopenharmony_ci	struct cdns_i3c_i2c_dev_data *data = i3c_dev_get_master_data(dev);
15238c2ecf20Sopenharmony_ci
15248c2ecf20Sopenharmony_ci	i3c_generic_ibi_recycle_slot(data->ibi_pool, slot);
15258c2ecf20Sopenharmony_ci}
15268c2ecf20Sopenharmony_ci
15278c2ecf20Sopenharmony_cistatic const struct i3c_master_controller_ops cdns_i3c_master_ops = {
15288c2ecf20Sopenharmony_ci	.bus_init = cdns_i3c_master_bus_init,
15298c2ecf20Sopenharmony_ci	.bus_cleanup = cdns_i3c_master_bus_cleanup,
15308c2ecf20Sopenharmony_ci	.do_daa = cdns_i3c_master_do_daa,
15318c2ecf20Sopenharmony_ci	.attach_i3c_dev = cdns_i3c_master_attach_i3c_dev,
15328c2ecf20Sopenharmony_ci	.reattach_i3c_dev = cdns_i3c_master_reattach_i3c_dev,
15338c2ecf20Sopenharmony_ci	.detach_i3c_dev = cdns_i3c_master_detach_i3c_dev,
15348c2ecf20Sopenharmony_ci	.attach_i2c_dev = cdns_i3c_master_attach_i2c_dev,
15358c2ecf20Sopenharmony_ci	.detach_i2c_dev = cdns_i3c_master_detach_i2c_dev,
15368c2ecf20Sopenharmony_ci	.supports_ccc_cmd = cdns_i3c_master_supports_ccc_cmd,
15378c2ecf20Sopenharmony_ci	.send_ccc_cmd = cdns_i3c_master_send_ccc_cmd,
15388c2ecf20Sopenharmony_ci	.priv_xfers = cdns_i3c_master_priv_xfers,
15398c2ecf20Sopenharmony_ci	.i2c_xfers = cdns_i3c_master_i2c_xfers,
15408c2ecf20Sopenharmony_ci	.enable_ibi = cdns_i3c_master_enable_ibi,
15418c2ecf20Sopenharmony_ci	.disable_ibi = cdns_i3c_master_disable_ibi,
15428c2ecf20Sopenharmony_ci	.request_ibi = cdns_i3c_master_request_ibi,
15438c2ecf20Sopenharmony_ci	.free_ibi = cdns_i3c_master_free_ibi,
15448c2ecf20Sopenharmony_ci	.recycle_ibi_slot = cdns_i3c_master_recycle_ibi_slot,
15458c2ecf20Sopenharmony_ci};
15468c2ecf20Sopenharmony_ci
15478c2ecf20Sopenharmony_cistatic void cdns_i3c_master_hj(struct work_struct *work)
15488c2ecf20Sopenharmony_ci{
15498c2ecf20Sopenharmony_ci	struct cdns_i3c_master *master = container_of(work,
15508c2ecf20Sopenharmony_ci						      struct cdns_i3c_master,
15518c2ecf20Sopenharmony_ci						      hj_work);
15528c2ecf20Sopenharmony_ci
15538c2ecf20Sopenharmony_ci	i3c_master_do_daa(&master->base);
15548c2ecf20Sopenharmony_ci}
15558c2ecf20Sopenharmony_ci
15568c2ecf20Sopenharmony_cistatic struct cdns_i3c_data cdns_i3c_devdata = {
15578c2ecf20Sopenharmony_ci	.thd_delay_ns = 10,
15588c2ecf20Sopenharmony_ci};
15598c2ecf20Sopenharmony_ci
15608c2ecf20Sopenharmony_cistatic const struct of_device_id cdns_i3c_master_of_ids[] = {
15618c2ecf20Sopenharmony_ci	{ .compatible = "cdns,i3c-master", .data = &cdns_i3c_devdata },
15628c2ecf20Sopenharmony_ci	{ /* sentinel */ },
15638c2ecf20Sopenharmony_ci};
15648c2ecf20Sopenharmony_ci
15658c2ecf20Sopenharmony_cistatic int cdns_i3c_master_probe(struct platform_device *pdev)
15668c2ecf20Sopenharmony_ci{
15678c2ecf20Sopenharmony_ci	struct cdns_i3c_master *master;
15688c2ecf20Sopenharmony_ci	int ret, irq;
15698c2ecf20Sopenharmony_ci	u32 val;
15708c2ecf20Sopenharmony_ci
15718c2ecf20Sopenharmony_ci	master = devm_kzalloc(&pdev->dev, sizeof(*master), GFP_KERNEL);
15728c2ecf20Sopenharmony_ci	if (!master)
15738c2ecf20Sopenharmony_ci		return -ENOMEM;
15748c2ecf20Sopenharmony_ci
15758c2ecf20Sopenharmony_ci	master->devdata = of_device_get_match_data(&pdev->dev);
15768c2ecf20Sopenharmony_ci	if (!master->devdata)
15778c2ecf20Sopenharmony_ci		return -EINVAL;
15788c2ecf20Sopenharmony_ci
15798c2ecf20Sopenharmony_ci	master->regs = devm_platform_ioremap_resource(pdev, 0);
15808c2ecf20Sopenharmony_ci	if (IS_ERR(master->regs))
15818c2ecf20Sopenharmony_ci		return PTR_ERR(master->regs);
15828c2ecf20Sopenharmony_ci
15838c2ecf20Sopenharmony_ci	master->pclk = devm_clk_get(&pdev->dev, "pclk");
15848c2ecf20Sopenharmony_ci	if (IS_ERR(master->pclk))
15858c2ecf20Sopenharmony_ci		return PTR_ERR(master->pclk);
15868c2ecf20Sopenharmony_ci
15878c2ecf20Sopenharmony_ci	master->sysclk = devm_clk_get(&pdev->dev, "sysclk");
15888c2ecf20Sopenharmony_ci	if (IS_ERR(master->sysclk))
15898c2ecf20Sopenharmony_ci		return PTR_ERR(master->sysclk);
15908c2ecf20Sopenharmony_ci
15918c2ecf20Sopenharmony_ci	irq = platform_get_irq(pdev, 0);
15928c2ecf20Sopenharmony_ci	if (irq < 0)
15938c2ecf20Sopenharmony_ci		return irq;
15948c2ecf20Sopenharmony_ci
15958c2ecf20Sopenharmony_ci	ret = clk_prepare_enable(master->pclk);
15968c2ecf20Sopenharmony_ci	if (ret)
15978c2ecf20Sopenharmony_ci		return ret;
15988c2ecf20Sopenharmony_ci
15998c2ecf20Sopenharmony_ci	ret = clk_prepare_enable(master->sysclk);
16008c2ecf20Sopenharmony_ci	if (ret)
16018c2ecf20Sopenharmony_ci		goto err_disable_pclk;
16028c2ecf20Sopenharmony_ci
16038c2ecf20Sopenharmony_ci	if (readl(master->regs + DEV_ID) != DEV_ID_I3C_MASTER) {
16048c2ecf20Sopenharmony_ci		ret = -EINVAL;
16058c2ecf20Sopenharmony_ci		goto err_disable_sysclk;
16068c2ecf20Sopenharmony_ci	}
16078c2ecf20Sopenharmony_ci
16088c2ecf20Sopenharmony_ci	spin_lock_init(&master->xferqueue.lock);
16098c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&master->xferqueue.list);
16108c2ecf20Sopenharmony_ci
16118c2ecf20Sopenharmony_ci	INIT_WORK(&master->hj_work, cdns_i3c_master_hj);
16128c2ecf20Sopenharmony_ci	writel(0xffffffff, master->regs + MST_IDR);
16138c2ecf20Sopenharmony_ci	writel(0xffffffff, master->regs + SLV_IDR);
16148c2ecf20Sopenharmony_ci	ret = devm_request_irq(&pdev->dev, irq, cdns_i3c_master_interrupt, 0,
16158c2ecf20Sopenharmony_ci			       dev_name(&pdev->dev), master);
16168c2ecf20Sopenharmony_ci	if (ret)
16178c2ecf20Sopenharmony_ci		goto err_disable_sysclk;
16188c2ecf20Sopenharmony_ci
16198c2ecf20Sopenharmony_ci	platform_set_drvdata(pdev, master);
16208c2ecf20Sopenharmony_ci
16218c2ecf20Sopenharmony_ci	val = readl(master->regs + CONF_STATUS0);
16228c2ecf20Sopenharmony_ci
16238c2ecf20Sopenharmony_ci	/* Device ID0 is reserved to describe this master. */
16248c2ecf20Sopenharmony_ci	master->maxdevs = CONF_STATUS0_DEVS_NUM(val);
16258c2ecf20Sopenharmony_ci	master->free_rr_slots = GENMASK(master->maxdevs, 1);
16268c2ecf20Sopenharmony_ci	master->caps.ibirfifodepth = CONF_STATUS0_IBIR_DEPTH(val);
16278c2ecf20Sopenharmony_ci	master->caps.cmdrfifodepth = CONF_STATUS0_CMDR_DEPTH(val);
16288c2ecf20Sopenharmony_ci
16298c2ecf20Sopenharmony_ci	val = readl(master->regs + CONF_STATUS1);
16308c2ecf20Sopenharmony_ci	master->caps.cmdfifodepth = CONF_STATUS1_CMD_DEPTH(val);
16318c2ecf20Sopenharmony_ci	master->caps.rxfifodepth = CONF_STATUS1_RX_DEPTH(val);
16328c2ecf20Sopenharmony_ci	master->caps.txfifodepth = CONF_STATUS1_TX_DEPTH(val);
16338c2ecf20Sopenharmony_ci
16348c2ecf20Sopenharmony_ci	spin_lock_init(&master->ibi.lock);
16358c2ecf20Sopenharmony_ci	master->ibi.num_slots = CONF_STATUS1_IBI_HW_RES(val);
16368c2ecf20Sopenharmony_ci	master->ibi.slots = devm_kcalloc(&pdev->dev, master->ibi.num_slots,
16378c2ecf20Sopenharmony_ci					 sizeof(*master->ibi.slots),
16388c2ecf20Sopenharmony_ci					 GFP_KERNEL);
16398c2ecf20Sopenharmony_ci	if (!master->ibi.slots) {
16408c2ecf20Sopenharmony_ci		ret = -ENOMEM;
16418c2ecf20Sopenharmony_ci		goto err_disable_sysclk;
16428c2ecf20Sopenharmony_ci	}
16438c2ecf20Sopenharmony_ci
16448c2ecf20Sopenharmony_ci	writel(IBIR_THR(1), master->regs + CMD_IBI_THR_CTRL);
16458c2ecf20Sopenharmony_ci	writel(MST_INT_IBIR_THR, master->regs + MST_IER);
16468c2ecf20Sopenharmony_ci	writel(DEVS_CTRL_DEV_CLR_ALL, master->regs + DEVS_CTRL);
16478c2ecf20Sopenharmony_ci
16488c2ecf20Sopenharmony_ci	ret = i3c_master_register(&master->base, &pdev->dev,
16498c2ecf20Sopenharmony_ci				  &cdns_i3c_master_ops, false);
16508c2ecf20Sopenharmony_ci	if (ret)
16518c2ecf20Sopenharmony_ci		goto err_disable_sysclk;
16528c2ecf20Sopenharmony_ci
16538c2ecf20Sopenharmony_ci	return 0;
16548c2ecf20Sopenharmony_ci
16558c2ecf20Sopenharmony_cierr_disable_sysclk:
16568c2ecf20Sopenharmony_ci	clk_disable_unprepare(master->sysclk);
16578c2ecf20Sopenharmony_ci
16588c2ecf20Sopenharmony_cierr_disable_pclk:
16598c2ecf20Sopenharmony_ci	clk_disable_unprepare(master->pclk);
16608c2ecf20Sopenharmony_ci
16618c2ecf20Sopenharmony_ci	return ret;
16628c2ecf20Sopenharmony_ci}
16638c2ecf20Sopenharmony_ci
16648c2ecf20Sopenharmony_cistatic int cdns_i3c_master_remove(struct platform_device *pdev)
16658c2ecf20Sopenharmony_ci{
16668c2ecf20Sopenharmony_ci	struct cdns_i3c_master *master = platform_get_drvdata(pdev);
16678c2ecf20Sopenharmony_ci	int ret;
16688c2ecf20Sopenharmony_ci
16698c2ecf20Sopenharmony_ci	ret = i3c_master_unregister(&master->base);
16708c2ecf20Sopenharmony_ci	if (ret)
16718c2ecf20Sopenharmony_ci		return ret;
16728c2ecf20Sopenharmony_ci
16738c2ecf20Sopenharmony_ci	clk_disable_unprepare(master->sysclk);
16748c2ecf20Sopenharmony_ci	clk_disable_unprepare(master->pclk);
16758c2ecf20Sopenharmony_ci
16768c2ecf20Sopenharmony_ci	return 0;
16778c2ecf20Sopenharmony_ci}
16788c2ecf20Sopenharmony_ci
16798c2ecf20Sopenharmony_cistatic struct platform_driver cdns_i3c_master = {
16808c2ecf20Sopenharmony_ci	.probe = cdns_i3c_master_probe,
16818c2ecf20Sopenharmony_ci	.remove = cdns_i3c_master_remove,
16828c2ecf20Sopenharmony_ci	.driver = {
16838c2ecf20Sopenharmony_ci		.name = "cdns-i3c-master",
16848c2ecf20Sopenharmony_ci		.of_match_table = cdns_i3c_master_of_ids,
16858c2ecf20Sopenharmony_ci	},
16868c2ecf20Sopenharmony_ci};
16878c2ecf20Sopenharmony_cimodule_platform_driver(cdns_i3c_master);
16888c2ecf20Sopenharmony_ci
16898c2ecf20Sopenharmony_ciMODULE_AUTHOR("Boris Brezillon <boris.brezillon@bootlin.com>");
16908c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Cadence I3C master driver");
16918c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2");
16928c2ecf20Sopenharmony_ciMODULE_ALIAS("platform:cdns-i3c-master");
1693