blob: 42e7aad8d01dae3999ccaf8628da07e437141772 [file] [log] [blame]
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07001/* Copyright (c) 2013-2016, The Linux Foundation. All rights reserved.
Yaniv Gardi81c0fc52015-01-15 16:32:37 +02002 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 */
13
14#ifndef UFS_QCOM_H_
15#define UFS_QCOM_H_
16
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -070017#include <linux/phy/phy.h>
18#include <linux/pm_qos.h>
19#include "ufshcd.h"
20
Subhash Jadavania889db02016-12-09 10:24:58 -080021#define MAX_UFS_QCOM_HOSTS 2
Yaniv Gardi81c0fc52015-01-15 16:32:37 +020022#define MAX_U32 (~(u32)0)
23#define MPHY_TX_FSM_STATE 0x41
24#define TX_FSM_HIBERN8 0x1
25#define HBRN8_POLL_TOUT_MS 100
26#define DEFAULT_CLK_RATE_HZ 1000000
27#define BUS_VECTOR_NAME_LEN 32
28
29#define UFS_HW_VER_MAJOR_SHFT (28)
30#define UFS_HW_VER_MAJOR_MASK (0x000F << UFS_HW_VER_MAJOR_SHFT)
31#define UFS_HW_VER_MINOR_SHFT (16)
32#define UFS_HW_VER_MINOR_MASK (0x0FFF << UFS_HW_VER_MINOR_SHFT)
33#define UFS_HW_VER_STEP_SHFT (0)
34#define UFS_HW_VER_STEP_MASK (0xFFFF << UFS_HW_VER_STEP_SHFT)
35
36/* vendor specific pre-defined parameters */
37#define SLOW 1
38#define FAST 2
39
40#define UFS_QCOM_LIMIT_NUM_LANES_RX 2
41#define UFS_QCOM_LIMIT_NUM_LANES_TX 2
Yaniv Gardif06fcc72015-10-28 13:15:51 +020042#define UFS_QCOM_LIMIT_HSGEAR_RX UFS_HS_G3
43#define UFS_QCOM_LIMIT_HSGEAR_TX UFS_HS_G3
Yaniv Gardi81c0fc52015-01-15 16:32:37 +020044#define UFS_QCOM_LIMIT_PWMGEAR_RX UFS_PWM_G4
45#define UFS_QCOM_LIMIT_PWMGEAR_TX UFS_PWM_G4
46#define UFS_QCOM_LIMIT_RX_PWR_PWM SLOW_MODE
47#define UFS_QCOM_LIMIT_TX_PWR_PWM SLOW_MODE
48#define UFS_QCOM_LIMIT_RX_PWR_HS FAST_MODE
49#define UFS_QCOM_LIMIT_TX_PWR_HS FAST_MODE
50#define UFS_QCOM_LIMIT_HS_RATE PA_HS_MODE_B
51#define UFS_QCOM_LIMIT_DESIRED_MODE FAST
52
53/* QCOM UFS host controller vendor specific registers */
54enum {
55 REG_UFS_SYS1CLK_1US = 0xC0,
56 REG_UFS_TX_SYMBOL_CLK_NS_US = 0xC4,
57 REG_UFS_LOCAL_PORT_ID_REG = 0xC8,
58 REG_UFS_PA_ERR_CODE = 0xCC,
59 REG_UFS_RETRY_TIMER_REG = 0xD0,
60 REG_UFS_PA_LINK_STARTUP_TIMER = 0xD8,
61 REG_UFS_CFG1 = 0xDC,
62 REG_UFS_CFG2 = 0xE0,
63 REG_UFS_HW_VERSION = 0xE4,
64
Yaniv Gardi6e3fd442015-10-28 13:15:50 +020065 UFS_TEST_BUS = 0xE8,
66 UFS_TEST_BUS_CTRL_0 = 0xEC,
67 UFS_TEST_BUS_CTRL_1 = 0xF0,
68 UFS_TEST_BUS_CTRL_2 = 0xF4,
69 UFS_UNIPRO_CFG = 0xF8,
70
Yaniv Gardif06fcc72015-10-28 13:15:51 +020071 /*
72 * QCOM UFS host controller vendor specific registers
73 * added in HW Version 3.0.0
74 */
75 UFS_AH8_CFG = 0xFC,
Yaniv Gardi6e3fd442015-10-28 13:15:50 +020076};
77
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -070078
Yaniv Gardi6e3fd442015-10-28 13:15:50 +020079/* QCOM UFS host controller vendor specific debug registers */
80enum {
Yaniv Gardi81c0fc52015-01-15 16:32:37 +020081 UFS_DBG_RD_REG_UAWM = 0x100,
82 UFS_DBG_RD_REG_UARM = 0x200,
83 UFS_DBG_RD_REG_TXUC = 0x300,
84 UFS_DBG_RD_REG_RXUC = 0x400,
85 UFS_DBG_RD_REG_DFC = 0x500,
86 UFS_DBG_RD_REG_TRLUT = 0x600,
87 UFS_DBG_RD_REG_TMRLUT = 0x700,
88 UFS_UFS_DBG_RD_REG_OCSC = 0x800,
89
90 UFS_UFS_DBG_RD_DESC_RAM = 0x1500,
91 UFS_UFS_DBG_RD_PRDT_RAM = 0x1700,
92 UFS_UFS_DBG_RD_RESP_RAM = 0x1800,
93 UFS_UFS_DBG_RD_EDTL_RAM = 0x1900,
94};
95
Yaniv Gardif06fcc72015-10-28 13:15:51 +020096#define UFS_CNTLR_2_x_x_VEN_REGS_OFFSET(x) (0x000 + x)
97#define UFS_CNTLR_3_x_x_VEN_REGS_OFFSET(x) (0x400 + x)
98
99/* bit definitions for REG_UFS_CFG1 register */
100#define QUNIPRO_SEL UFS_BIT(0)
Yaniv Gardi6e3fd442015-10-28 13:15:50 +0200101#define TEST_BUS_EN BIT(18)
102#define TEST_BUS_SEL GENMASK(22, 19)
103
Yaniv Gardi81c0fc52015-01-15 16:32:37 +0200104/* bit definitions for REG_UFS_CFG2 register */
105#define UAWM_HW_CGC_EN (1 << 0)
106#define UARM_HW_CGC_EN (1 << 1)
107#define TXUC_HW_CGC_EN (1 << 2)
108#define RXUC_HW_CGC_EN (1 << 3)
109#define DFC_HW_CGC_EN (1 << 4)
110#define TRLUT_HW_CGC_EN (1 << 5)
111#define TMRLUT_HW_CGC_EN (1 << 6)
112#define OCSC_HW_CGC_EN (1 << 7)
113
Yaniv Gardi6e3fd442015-10-28 13:15:50 +0200114/* bit definition for UFS_UFS_TEST_BUS_CTRL_n */
115#define TEST_BUS_SUB_SEL_MASK 0x1F /* All XXX_SEL fields are 5 bits wide */
116
Yaniv Gardi81c0fc52015-01-15 16:32:37 +0200117#define REG_UFS_CFG2_CGC_EN_ALL (UAWM_HW_CGC_EN | UARM_HW_CGC_EN |\
118 TXUC_HW_CGC_EN | RXUC_HW_CGC_EN |\
119 DFC_HW_CGC_EN | TRLUT_HW_CGC_EN |\
120 TMRLUT_HW_CGC_EN | OCSC_HW_CGC_EN)
121
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -0700122/* bit definitions for UFS_AH8_CFG register */
123#define CC_UFS_HCLK_REQ_EN BIT(1)
124#define CC_UFS_SYS_CLK_REQ_EN BIT(2)
125#define CC_UFS_ICE_CORE_CLK_REQ_EN BIT(3)
126#define CC_UFS_UNIPRO_CORE_CLK_REQ_EN BIT(4)
127#define CC_UFS_AUXCLK_REQ_EN BIT(5)
128
129#define UFS_HW_CLK_CTRL_EN (CC_UFS_SYS_CLK_REQ_EN |\
130 CC_UFS_ICE_CORE_CLK_REQ_EN |\
131 CC_UFS_UNIPRO_CORE_CLK_REQ_EN |\
132 CC_UFS_AUXCLK_REQ_EN)
Yaniv Gardi81c0fc52015-01-15 16:32:37 +0200133/* bit offset */
134enum {
135 OFFSET_UFS_PHY_SOFT_RESET = 1,
136 OFFSET_CLK_NS_REG = 10,
137};
138
139/* bit masks */
140enum {
141 MASK_UFS_PHY_SOFT_RESET = 0x2,
142 MASK_TX_SYMBOL_CLK_1US_REG = 0x3FF,
143 MASK_CLK_NS_REG = 0xFFFC00,
144};
145
146enum ufs_qcom_phy_init_type {
147 UFS_PHY_INIT_FULL,
148 UFS_PHY_INIT_CFG_RESTORE,
149};
150
Yaniv Gardi6e3fd442015-10-28 13:15:50 +0200151/* QCOM UFS debug print bit mask */
152#define UFS_QCOM_DBG_PRINT_REGS_EN BIT(0)
153#define UFS_QCOM_DBG_PRINT_ICE_REGS_EN BIT(1)
154#define UFS_QCOM_DBG_PRINT_TEST_BUS_EN BIT(2)
155
156#define UFS_QCOM_DBG_PRINT_ALL \
157 (UFS_QCOM_DBG_PRINT_REGS_EN | UFS_QCOM_DBG_PRINT_ICE_REGS_EN | \
158 UFS_QCOM_DBG_PRINT_TEST_BUS_EN)
159
Yaniv Gardif06fcc72015-10-28 13:15:51 +0200160/* QUniPro Vendor specific attributes */
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -0700161#define PA_VS_CONFIG_REG1 0x9000
162#define SAVECONFIGTIME_MODE_MASK 0x6000
163
164#define PA_VS_CLK_CFG_REG 0x9004
165#define PA_VS_CLK_CFG_REG_MASK 0x1FF
166
167#define DL_VS_CLK_CFG 0xA00B
168#define DL_VS_CLK_CFG_MASK 0x3FF
169
Yaniv Gardif06fcc72015-10-28 13:15:51 +0200170#define DME_VS_CORE_CLK_CTRL 0xD002
171/* bit and mask definitions for DME_VS_CORE_CLK_CTRL attribute */
Yaniv Gardif06fcc72015-10-28 13:15:51 +0200172#define DME_VS_CORE_CLK_CTRL_MAX_CORE_CLK_1US_CYCLES_MASK 0xFF
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -0700173#define DME_VS_CORE_CLK_CTRL_CORE_CLK_DIV_EN_BIT BIT(8)
174#define DME_VS_CORE_CLK_CTRL_DME_HW_CGC_EN BIT(9)
Yaniv Gardif06fcc72015-10-28 13:15:51 +0200175
Yaniv Gardi81c0fc52015-01-15 16:32:37 +0200176static inline void
177ufs_qcom_get_controller_revision(struct ufs_hba *hba,
178 u8 *major, u16 *minor, u16 *step)
179{
180 u32 ver = ufshcd_readl(hba, REG_UFS_HW_VERSION);
181
182 *major = (ver & UFS_HW_VER_MAJOR_MASK) >> UFS_HW_VER_MAJOR_SHFT;
183 *minor = (ver & UFS_HW_VER_MINOR_MASK) >> UFS_HW_VER_MINOR_SHFT;
184 *step = (ver & UFS_HW_VER_STEP_MASK) >> UFS_HW_VER_STEP_SHFT;
185};
186
187static inline void ufs_qcom_assert_reset(struct ufs_hba *hba)
188{
189 ufshcd_rmwl(hba, MASK_UFS_PHY_SOFT_RESET,
190 1 << OFFSET_UFS_PHY_SOFT_RESET, REG_UFS_CFG1);
191
192 /*
193 * Make sure assertion of ufs phy reset is written to
194 * register before returning
195 */
196 mb();
197}
198
199static inline void ufs_qcom_deassert_reset(struct ufs_hba *hba)
200{
201 ufshcd_rmwl(hba, MASK_UFS_PHY_SOFT_RESET,
202 0 << OFFSET_UFS_PHY_SOFT_RESET, REG_UFS_CFG1);
203
204 /*
205 * Make sure de-assertion of ufs phy reset is written to
206 * register before returning
207 */
208 mb();
209}
210
211struct ufs_qcom_bus_vote {
212 uint32_t client_handle;
213 uint32_t curr_vote;
214 int min_bw_vote;
215 int max_bw_vote;
216 int saved_vote;
217 bool is_max_bw_needed;
218 struct device_attribute max_bus_bw;
219};
220
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -0700221/**
222 * struct ufs_qcom_ice_data - ICE related information
223 * @vops: pointer to variant operations of ICE
224 * @async_done: completion for supporting ICE's driver asynchronous nature
225 * @pdev: pointer to the proper ICE platform device
226 * @state: UFS-ICE interface's internal state (see
227 * ufs-qcom-ice.h for possible internal states)
228 * @quirks: UFS-ICE interface related quirks
229 * @crypto_engine_err: crypto engine errors
230 */
231struct ufs_qcom_ice_data {
232 struct qcom_ice_variant_ops *vops;
233 struct platform_device *pdev;
234 int state;
235
236 u16 quirks;
237
238 bool crypto_engine_err;
239};
240
Yaniv Gardibfdbe8b2015-03-31 17:37:13 +0300241/* Host controller hardware version: major.minor.step */
242struct ufs_hw_version {
243 u16 step;
244 u16 minor;
245 u8 major;
246};
Yaniv Gardicad2e032015-03-31 17:37:14 +0300247
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -0700248#ifdef CONFIG_DEBUG_FS
249struct qcom_debugfs_files {
250 struct dentry *debugfs_root;
251 struct dentry *dbg_print_en;
252 struct dentry *testbus;
253 struct dentry *testbus_en;
254 struct dentry *testbus_cfg;
255 struct dentry *testbus_bus;
256 struct dentry *dbg_regs;
257 struct dentry *pm_qos;
258};
259#endif
260
Yaniv Gardi6e3fd442015-10-28 13:15:50 +0200261struct ufs_qcom_testbus {
262 u8 select_major;
263 u8 select_minor;
264};
265
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -0700266/* PM QoS voting state */
267enum ufs_qcom_pm_qos_state {
268 PM_QOS_UNVOTED,
269 PM_QOS_VOTED,
270 PM_QOS_REQ_VOTE,
271 PM_QOS_REQ_UNVOTE,
272};
273
274/**
275 * struct ufs_qcom_pm_qos_cpu_group - data related to cluster PM QoS voting
276 * logic
277 * @req: request object for PM QoS
278 * @vote_work: work object for voting procedure
279 * @unvote_work: work object for un-voting procedure
280 * @host: back pointer to the main structure
281 * @state: voting state machine current state
282 * @latency_us: requested latency value used for cluster voting, in
283 * microseconds
284 * @mask: cpu mask defined for this cluster
285 * @active_reqs: number of active requests on this cluster
286 */
287struct ufs_qcom_pm_qos_cpu_group {
288 struct pm_qos_request req;
289 struct work_struct vote_work;
290 struct work_struct unvote_work;
291 struct ufs_qcom_host *host;
292 enum ufs_qcom_pm_qos_state state;
293 s32 latency_us;
294 cpumask_t mask;
295 int active_reqs;
296};
297
298/**
299 * struct ufs_qcom_pm_qos - data related to PM QoS voting logic
300 * @groups: PM QoS cpu group state array
301 * @enable_attr: sysfs attribute to enable/disable PM QoS voting logic
302 * @latency_attr: sysfs attribute to set latency value
303 * @workq: single threaded workqueue to run PM QoS voting/unvoting
304 * @num_clusters: number of clusters defined
305 * @default_cpu: cpu to use for voting for request not specifying a cpu
306 * @is_enabled: flag specifying whether voting logic is enabled
307 */
308struct ufs_qcom_pm_qos {
309 struct ufs_qcom_pm_qos_cpu_group *groups;
310 struct device_attribute enable_attr;
311 struct device_attribute latency_attr;
312 struct workqueue_struct *workq;
313 int num_groups;
314 int default_cpu;
315 bool is_enabled;
316};
317
Yaniv Gardi6e3fd442015-10-28 13:15:50 +0200318struct ufs_qcom_host {
Yaniv Gardicad2e032015-03-31 17:37:14 +0300319 /*
320 * Set this capability if host controller supports the QUniPro mode
321 * and if driver wants the Host controller to operate in QUniPro mode.
322 * Note: By default this capability will be kept enabled if host
323 * controller supports the QUniPro mode.
324 */
325 #define UFS_QCOM_CAP_QUNIPRO UFS_BIT(0)
Yaniv Gardif06fcc72015-10-28 13:15:51 +0200326
327 /*
328 * Set this capability if host controller can retain the secure
329 * configuration even after UFS controller core power collapse.
330 */
331 #define UFS_QCOM_CAP_RETAIN_SEC_CFG_AFTER_PWR_COLLAPSE UFS_BIT(1)
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -0700332
333 /*
334 * Set this capability if host controller supports Qunipro internal
335 * clock gating.
336 */
337 #define UFS_QCOM_CAP_QUNIPRO_CLK_GATING UFS_BIT(2)
338
339 /*
340 * Set this capability if host controller supports SVS2 frequencies.
341 */
342 #define UFS_QCOM_CAP_SVS2 UFS_BIT(3)
Yaniv Gardicad2e032015-03-31 17:37:14 +0300343 u32 caps;
344
Yaniv Gardi81c0fc52015-01-15 16:32:37 +0200345 struct phy *generic_phy;
346 struct ufs_hba *hba;
347 struct ufs_qcom_bus_vote bus_vote;
348 struct ufs_pa_layer_attr dev_req_params;
349 struct clk *rx_l0_sync_clk;
350 struct clk *tx_l0_sync_clk;
351 struct clk *rx_l1_sync_clk;
352 struct clk *tx_l1_sync_clk;
Yaniv Gardibfdbe8b2015-03-31 17:37:13 +0300353
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -0700354 /* PM Quality-of-Service (QoS) data */
355 struct ufs_qcom_pm_qos pm_qos;
356
357 bool disable_lpm;
358 bool is_lane_clks_enabled;
359 bool sec_cfg_updated;
360 struct ufs_qcom_ice_data ice;
Yaniv Gardif06fcc72015-10-28 13:15:51 +0200361 void __iomem *dev_ref_clk_ctrl_mmio;
362 bool is_dev_ref_clk_enabled;
Yaniv Gardibfdbe8b2015-03-31 17:37:13 +0300363 struct ufs_hw_version hw_ver;
Yaniv Gardif06fcc72015-10-28 13:15:51 +0200364 u32 dev_ref_clk_en_mask;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -0700365#ifdef CONFIG_DEBUG_FS
366 struct qcom_debugfs_files debugfs_files;
367#endif
Yaniv Gardi6e3fd442015-10-28 13:15:50 +0200368 /* Bitmask for enabling debug prints */
369 u32 dbg_print_en;
370 struct ufs_qcom_testbus testbus;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -0700371
372 struct work_struct ice_cfg_work;
373 struct request *req_pending;
Yaniv Gardi81c0fc52015-01-15 16:32:37 +0200374};
375
Yaniv Gardieba5ed32016-03-10 17:37:21 +0200376static inline u32
377ufs_qcom_get_debug_reg_offset(struct ufs_qcom_host *host, u32 reg)
378{
379 if (host->hw_ver.major <= 0x02)
380 return UFS_CNTLR_2_x_x_VEN_REGS_OFFSET(reg);
381
382 return UFS_CNTLR_3_x_x_VEN_REGS_OFFSET(reg);
383};
384
Yaniv Gardi81c0fc52015-01-15 16:32:37 +0200385#define ufs_qcom_is_link_off(hba) ufshcd_is_link_off(hba)
386#define ufs_qcom_is_link_active(hba) ufshcd_is_link_active(hba)
387#define ufs_qcom_is_link_hibern8(hba) ufshcd_is_link_hibern8(hba)
388
Yaniv Gardi6e3fd442015-10-28 13:15:50 +0200389int ufs_qcom_testbus_config(struct ufs_qcom_host *host);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -0700390void ufs_qcom_print_hw_debug_reg_all(struct ufs_hba *hba, void *priv,
391 void (*print_fn)(struct ufs_hba *hba, int offset, int num_regs,
392 char *str, void *priv));
Yaniv Gardi6e3fd442015-10-28 13:15:50 +0200393
Yaniv Gardicad2e032015-03-31 17:37:14 +0300394static inline bool ufs_qcom_cap_qunipro(struct ufs_qcom_host *host)
395{
396 if (host->caps & UFS_QCOM_CAP_QUNIPRO)
397 return true;
398 else
399 return false;
400}
401
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -0700402static inline bool ufs_qcom_cap_qunipro_clk_gating(struct ufs_qcom_host *host)
403{
404 return !!(host->caps & UFS_QCOM_CAP_QUNIPRO_CLK_GATING);
405}
406
407static inline bool ufs_qcom_cap_svs2(struct ufs_qcom_host *host)
408{
409 return !!(host->caps & UFS_QCOM_CAP_SVS2);
410}
411
Yaniv Gardi81c0fc52015-01-15 16:32:37 +0200412#endif /* UFS_QCOM_H_ */