xref: /linux/drivers/ufs/host/ufs-qcom.h (revision 2e3fcbcc3b0eb9b96d2912cdac920f0ae8d1c8f2)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /* Copyright (c) 2013-2015, The Linux Foundation. All rights reserved.
3  */
4 
5 #ifndef UFS_QCOM_H_
6 #define UFS_QCOM_H_
7 
8 #include <linux/reset-controller.h>
9 #include <linux/reset.h>
10 #include <soc/qcom/ice.h>
11 #include <ufs/ufshcd.h>
12 
13 #define MPHY_TX_FSM_STATE       0x41
14 #define TX_FSM_HIBERN8          0x1
15 #define HBRN8_POLL_TOUT_MS      100
16 #define DEFAULT_CLK_RATE_HZ     1000000
17 #define MAX_SUPP_MAC		64
18 #define MAX_ESI_VEC		32
19 
20 #define UFS_HW_VER_MAJOR_MASK	GENMASK(31, 28)
21 #define UFS_HW_VER_MINOR_MASK	GENMASK(27, 16)
22 #define UFS_HW_VER_STEP_MASK	GENMASK(15, 0)
23 #define UFS_DEV_VER_MAJOR_MASK	GENMASK(7, 4)
24 
25 #define UFS_QCOM_LIMIT_HS_RATE		PA_HS_MODE_B
26 
27 /* QCOM UFS host controller vendor specific registers */
28 enum {
29 	REG_UFS_SYS1CLK_1US                 = 0xC0,
30 	REG_UFS_TX_SYMBOL_CLK_NS_US         = 0xC4,
31 	REG_UFS_LOCAL_PORT_ID_REG           = 0xC8,
32 	REG_UFS_PA_ERR_CODE                 = 0xCC,
33 	/* On older UFS revisions, this register is called "RETRY_TIMER_REG" */
34 	REG_UFS_PARAM0                      = 0xD0,
35 	/* On older UFS revisions, this register is called "REG_UFS_PA_LINK_STARTUP_TIMER" */
36 	REG_UFS_CFG0                        = 0xD8,
37 	REG_UFS_CFG1                        = 0xDC,
38 	REG_UFS_CFG2                        = 0xE0,
39 	REG_UFS_HW_VERSION                  = 0xE4,
40 
41 	UFS_TEST_BUS				= 0xE8,
42 	UFS_TEST_BUS_CTRL_0			= 0xEC,
43 	UFS_TEST_BUS_CTRL_1			= 0xF0,
44 	UFS_TEST_BUS_CTRL_2			= 0xF4,
45 	UFS_UNIPRO_CFG				= 0xF8,
46 
47 	/*
48 	 * QCOM UFS host controller vendor specific registers
49 	 * added in HW Version 3.0.0
50 	 */
51 	UFS_AH8_CFG				= 0xFC,
52 
53 	REG_UFS_MEM_ICE_CONFIG			= 0x260C,
54 	REG_UFS_MEM_ICE_NUM_CORE		= 0x2664,
55 
56 	REG_UFS_CFG3				= 0x271C,
57 
58 	REG_UFS_DEBUG_SPARE_CFG			= 0x284C,
59 };
60 
61 /* QCOM UFS host controller vendor specific debug registers */
62 enum {
63 	UFS_DBG_RD_REG_UAWM			= 0x100,
64 	UFS_DBG_RD_REG_UARM			= 0x200,
65 	UFS_DBG_RD_REG_TXUC			= 0x300,
66 	UFS_DBG_RD_REG_RXUC			= 0x400,
67 	UFS_DBG_RD_REG_DFC			= 0x500,
68 	UFS_DBG_RD_REG_TRLUT			= 0x600,
69 	UFS_DBG_RD_REG_TMRLUT			= 0x700,
70 	UFS_UFS_DBG_RD_REG_OCSC			= 0x800,
71 
72 	UFS_UFS_DBG_RD_DESC_RAM			= 0x1500,
73 	UFS_UFS_DBG_RD_PRDT_RAM			= 0x1700,
74 	UFS_UFS_DBG_RD_RESP_RAM			= 0x1800,
75 	UFS_UFS_DBG_RD_EDTL_RAM			= 0x1900,
76 };
77 
78 enum {
79 	UFS_MEM_CQIS_VS		= 0x8,
80 };
81 
82 #define UFS_CNTLR_2_x_x_VEN_REGS_OFFSET(x)	(0x000 + x)
83 #define UFS_CNTLR_3_x_x_VEN_REGS_OFFSET(x)	(0x400 + x)
84 
85 /* bit definitions for REG_UFS_CFG0 register */
86 #define QUNIPRO_G4_SEL		BIT(5)
87 
88 /* bit definitions for REG_UFS_CFG1 register */
89 #define QUNIPRO_SEL		BIT(0)
90 #define UFS_PHY_SOFT_RESET	BIT(1)
91 #define UTP_DBG_RAMS_EN		BIT(17)
92 #define TEST_BUS_EN		BIT(18)
93 #define TEST_BUS_SEL		GENMASK(22, 19)
94 #define UFS_REG_TEST_BUS_EN	BIT(30)
95 
96 /* bit definitions for REG_UFS_CFG2 register */
97 #define UAWM_HW_CGC_EN		BIT(0)
98 #define UARM_HW_CGC_EN		BIT(1)
99 #define TXUC_HW_CGC_EN		BIT(2)
100 #define RXUC_HW_CGC_EN		BIT(3)
101 #define DFC_HW_CGC_EN		BIT(4)
102 #define TRLUT_HW_CGC_EN		BIT(5)
103 #define TMRLUT_HW_CGC_EN	BIT(6)
104 #define OCSC_HW_CGC_EN		BIT(7)
105 
106 /* bit definitions for REG_UFS_CFG3 register */
107 #define ESI_VEC_MASK		GENMASK(22, 12)
108 
109 /* bit definitions for REG_UFS_PARAM0 */
110 #define MAX_HS_GEAR_MASK	GENMASK(6, 4)
111 #define UFS_QCOM_MAX_GEAR(x)	FIELD_GET(MAX_HS_GEAR_MASK, (x))
112 
113 /* bit definition for UFS_UFS_TEST_BUS_CTRL_n */
114 #define TEST_BUS_SUB_SEL_MASK	GENMASK(4, 0)  /* All XXX_SEL fields are 5 bits wide */
115 
116 /* bit definition for UFS Shared ICE config */
117 #define UFS_QCOM_CAP_ICE_CONFIG BIT(0)
118 
119 #define REG_UFS_CFG2_CGC_EN_ALL (UAWM_HW_CGC_EN | UARM_HW_CGC_EN |\
120 				 TXUC_HW_CGC_EN | RXUC_HW_CGC_EN |\
121 				 DFC_HW_CGC_EN | TRLUT_HW_CGC_EN |\
122 				 TMRLUT_HW_CGC_EN | OCSC_HW_CGC_EN)
123 
124 /* QUniPro Vendor specific attributes */
125 #define PA_VS_CONFIG_REG1	0x9000
126 #define DME_VS_CORE_CLK_CTRL	0xD002
127 /* bit and mask definitions for DME_VS_CORE_CLK_CTRL attribute */
128 #define CLK_1US_CYCLES_MASK_V4				GENMASK(27, 16)
129 #define CLK_1US_CYCLES_MASK				GENMASK(7, 0)
130 #define DME_VS_CORE_CLK_CTRL_CORE_CLK_DIV_EN_BIT	BIT(8)
131 #define PA_VS_CORE_CLK_40NS_CYCLES			0x9007
132 #define PA_VS_CORE_CLK_40NS_CYCLES_MASK			GENMASK(6, 0)
133 
134 
135 /* QCOM UFS host controller core clk frequencies */
136 #define UNIPRO_CORE_CLK_FREQ_37_5_MHZ          38
137 #define UNIPRO_CORE_CLK_FREQ_75_MHZ            75
138 #define UNIPRO_CORE_CLK_FREQ_100_MHZ           100
139 #define UNIPRO_CORE_CLK_FREQ_150_MHZ           150
140 #define UNIPRO_CORE_CLK_FREQ_300_MHZ           300
141 #define UNIPRO_CORE_CLK_FREQ_201_5_MHZ         202
142 #define UNIPRO_CORE_CLK_FREQ_403_MHZ           403
143 
144 /* ICE allocator type to share AES engines among TX stream and RX stream */
145 #define ICE_ALLOCATOR_TYPE 2
146 
147 /*
148  * Number of cores allocated for RX stream when Read data block received and
149  * Write data block is not in progress
150  */
151 #define NUM_RX_R1W0 28
152 
153 /*
154  * Number of cores allocated for TX stream when Device asked to send write
155  * data block and Read data block is not in progress
156  */
157 #define NUM_TX_R0W1 28
158 
159 /*
160  * Number of cores allocated for RX stream when Read data block received and
161  * Write data block is in progress
162  * OR
163  * Device asked to send write data block and Read data block is in progress
164  */
165 #define NUM_RX_R1W1 15
166 
167 /*
168  * Number of cores allocated for TX stream (UFS write) when Read data block
169  * received and Write data block is in progress
170  * OR
171  * Device asked to send write data block and Read data block is in progress
172  */
173 #define NUM_TX_R1W1 13
174 
175 static inline void
ufs_qcom_get_controller_revision(struct ufs_hba * hba,u8 * major,u16 * minor,u16 * step)176 ufs_qcom_get_controller_revision(struct ufs_hba *hba,
177 				 u8 *major, u16 *minor, u16 *step)
178 {
179 	u32 ver = ufshcd_readl(hba, REG_UFS_HW_VERSION);
180 
181 	*major = FIELD_GET(UFS_HW_VER_MAJOR_MASK, ver);
182 	*minor = FIELD_GET(UFS_HW_VER_MINOR_MASK, ver);
183 	*step = FIELD_GET(UFS_HW_VER_STEP_MASK, ver);
184 };
185 
ufs_qcom_assert_reset(struct ufs_hba * hba)186 static inline void ufs_qcom_assert_reset(struct ufs_hba *hba)
187 {
188 	ufshcd_rmwl(hba, UFS_PHY_SOFT_RESET, UFS_PHY_SOFT_RESET, REG_UFS_CFG1);
189 
190 	/*
191 	 * Dummy read to ensure the write takes effect before doing any sort
192 	 * of delay
193 	 */
194 	ufshcd_readl(hba, REG_UFS_CFG1);
195 }
196 
ufs_qcom_deassert_reset(struct ufs_hba * hba)197 static inline void ufs_qcom_deassert_reset(struct ufs_hba *hba)
198 {
199 	ufshcd_rmwl(hba, UFS_PHY_SOFT_RESET, 0, REG_UFS_CFG1);
200 
201 	/*
202 	 * Dummy read to ensure the write takes effect before doing any sort
203 	 * of delay
204 	 */
205 	ufshcd_readl(hba, REG_UFS_CFG1);
206 }
207 
208 /* Host controller hardware version: major.minor.step */
209 struct ufs_hw_version {
210 	u16 step;
211 	u16 minor;
212 	u8 major;
213 };
214 
215 struct ufs_qcom_testbus {
216 	u8 select_major;
217 	u8 select_minor;
218 };
219 
220 struct gpio_desc;
221 
222 struct ufs_qcom_host {
223 	struct phy *generic_phy;
224 	struct ufs_hba *hba;
225 	struct ufs_pa_layer_attr dev_req_params;
226 	struct clk_bulk_data *clks;
227 	u32 num_clks;
228 	bool is_lane_clks_enabled;
229 
230 	struct icc_path *icc_ddr;
231 	struct icc_path *icc_cpu;
232 
233 #ifdef CONFIG_SCSI_UFS_CRYPTO
234 	struct qcom_ice *ice;
235 #endif
236 	u32 caps;
237 	void __iomem *dev_ref_clk_ctrl_mmio;
238 	bool is_dev_ref_clk_enabled;
239 	struct ufs_hw_version hw_ver;
240 
241 	u32 dev_ref_clk_en_mask;
242 
243 	struct ufs_qcom_testbus testbus;
244 
245 	/* Reset control of HCI */
246 	struct reset_control *core_reset;
247 	struct reset_controller_dev rcdev;
248 
249 	struct gpio_desc *device_reset;
250 
251 	struct ufs_host_params host_params;
252 	u32 phy_gear;
253 
254 	bool esi_enabled;
255 };
256 
257 struct ufs_qcom_drvdata {
258 	enum ufshcd_quirks quirks;
259 	bool no_phy_retention;
260 };
261 
262 static inline u32
ufs_qcom_get_debug_reg_offset(struct ufs_qcom_host * host,u32 reg)263 ufs_qcom_get_debug_reg_offset(struct ufs_qcom_host *host, u32 reg)
264 {
265 	if (host->hw_ver.major <= 0x02)
266 		return UFS_CNTLR_2_x_x_VEN_REGS_OFFSET(reg);
267 
268 	return UFS_CNTLR_3_x_x_VEN_REGS_OFFSET(reg);
269 };
270 
271 #define ufs_qcom_is_link_off(hba) ufshcd_is_link_off(hba)
272 #define ufs_qcom_is_link_active(hba) ufshcd_is_link_active(hba)
273 #define ufs_qcom_is_link_hibern8(hba) ufshcd_is_link_hibern8(hba)
274 #define ceil(freq, div) ((freq) % (div) == 0 ? ((freq)/(div)) : ((freq)/(div) + 1))
275 
276 int ufs_qcom_testbus_config(struct ufs_qcom_host *host);
277 
278 #endif /* UFS_QCOM_H_ */
279