1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2013-2016, Linux Foundation. All rights reserved.
6 #include <linux/acpi.h>
7 #include <linux/time.h>
9 #include <linux/platform_device.h>
10 #include <linux/phy/phy.h>
11 #include <linux/gpio/consumer.h>
12 #include <linux/reset-controller.h>
15 #include "ufshcd-pltfrm.h"
19 #include "ufs_quirks.h"
20 #define UFS_QCOM_DEFAULT_DBG_PRINT_EN \
21 (UFS_QCOM_DBG_PRINT_REGS_EN | UFS_QCOM_DBG_PRINT_TEST_BUS_EN)
39 static struct ufs_qcom_host
*ufs_qcom_hosts
[MAX_UFS_QCOM_HOSTS
];
41 static int ufs_qcom_set_bus_vote(struct ufs_qcom_host
*host
, int vote
);
42 static void ufs_qcom_get_default_testbus_cfg(struct ufs_qcom_host
*host
);
43 static int ufs_qcom_set_dme_vs_core_clk_ctrl_clear_div(struct ufs_hba
*hba
,
46 static struct ufs_qcom_host
*rcdev_to_ufs_host(struct reset_controller_dev
*rcd
)
48 return container_of(rcd
, struct ufs_qcom_host
, rcdev
);
51 static void ufs_qcom_dump_regs_wrapper(struct ufs_hba
*hba
, int offset
, int len
,
52 const char *prefix
, void *priv
)
54 ufshcd_dump_regs(hba
, offset
, len
* 4, prefix
);
57 static int ufs_qcom_get_connected_tx_lanes(struct ufs_hba
*hba
, u32
*tx_lanes
)
61 err
= ufshcd_dme_get(hba
,
62 UIC_ARG_MIB(PA_CONNECTEDTXDATALANES
), tx_lanes
);
64 dev_err(hba
->dev
, "%s: couldn't read PA_CONNECTEDTXDATALANES %d\n",
70 static int ufs_qcom_host_clk_get(struct device
*dev
,
71 const char *name
, struct clk
**clk_out
, bool optional
)
76 clk
= devm_clk_get(dev
, name
);
84 if (optional
&& err
== -ENOENT
) {
89 if (err
!= -EPROBE_DEFER
)
90 dev_err(dev
, "failed to get %s err %d\n", name
, err
);
95 static int ufs_qcom_host_clk_enable(struct device
*dev
,
96 const char *name
, struct clk
*clk
)
100 err
= clk_prepare_enable(clk
);
102 dev_err(dev
, "%s: %s enable failed %d\n", __func__
, name
, err
);
107 static void ufs_qcom_disable_lane_clks(struct ufs_qcom_host
*host
)
109 if (!host
->is_lane_clks_enabled
)
112 clk_disable_unprepare(host
->tx_l1_sync_clk
);
113 clk_disable_unprepare(host
->tx_l0_sync_clk
);
114 clk_disable_unprepare(host
->rx_l1_sync_clk
);
115 clk_disable_unprepare(host
->rx_l0_sync_clk
);
117 host
->is_lane_clks_enabled
= false;
120 static int ufs_qcom_enable_lane_clks(struct ufs_qcom_host
*host
)
123 struct device
*dev
= host
->hba
->dev
;
125 if (host
->is_lane_clks_enabled
)
128 err
= ufs_qcom_host_clk_enable(dev
, "rx_lane0_sync_clk",
129 host
->rx_l0_sync_clk
);
133 err
= ufs_qcom_host_clk_enable(dev
, "tx_lane0_sync_clk",
134 host
->tx_l0_sync_clk
);
138 err
= ufs_qcom_host_clk_enable(dev
, "rx_lane1_sync_clk",
139 host
->rx_l1_sync_clk
);
143 err
= ufs_qcom_host_clk_enable(dev
, "tx_lane1_sync_clk",
144 host
->tx_l1_sync_clk
);
148 host
->is_lane_clks_enabled
= true;
152 clk_disable_unprepare(host
->rx_l1_sync_clk
);
154 clk_disable_unprepare(host
->tx_l0_sync_clk
);
156 clk_disable_unprepare(host
->rx_l0_sync_clk
);
161 static int ufs_qcom_init_lane_clks(struct ufs_qcom_host
*host
)
164 struct device
*dev
= host
->hba
->dev
;
166 if (has_acpi_companion(dev
))
169 err
= ufs_qcom_host_clk_get(dev
, "rx_lane0_sync_clk",
170 &host
->rx_l0_sync_clk
, false);
174 err
= ufs_qcom_host_clk_get(dev
, "tx_lane0_sync_clk",
175 &host
->tx_l0_sync_clk
, false);
179 /* In case of single lane per direction, don't read lane1 clocks */
180 if (host
->hba
->lanes_per_direction
> 1) {
181 err
= ufs_qcom_host_clk_get(dev
, "rx_lane1_sync_clk",
182 &host
->rx_l1_sync_clk
, false);
186 err
= ufs_qcom_host_clk_get(dev
, "tx_lane1_sync_clk",
187 &host
->tx_l1_sync_clk
, true);
193 static int ufs_qcom_link_startup_post_change(struct ufs_hba
*hba
)
197 return ufs_qcom_get_connected_tx_lanes(hba
, &tx_lanes
);
200 static int ufs_qcom_check_hibern8(struct ufs_hba
*hba
)
204 unsigned long timeout
= jiffies
+ msecs_to_jiffies(HBRN8_POLL_TOUT_MS
);
207 err
= ufshcd_dme_get(hba
,
208 UIC_ARG_MIB_SEL(MPHY_TX_FSM_STATE
,
209 UIC_ARG_MPHY_TX_GEN_SEL_INDEX(0)),
211 if (err
|| tx_fsm_val
== TX_FSM_HIBERN8
)
214 /* sleep for max. 200us */
215 usleep_range(100, 200);
216 } while (time_before(jiffies
, timeout
));
219 * we might have scheduled out for long during polling so
220 * check the state again.
222 if (time_after(jiffies
, timeout
))
223 err
= ufshcd_dme_get(hba
,
224 UIC_ARG_MIB_SEL(MPHY_TX_FSM_STATE
,
225 UIC_ARG_MPHY_TX_GEN_SEL_INDEX(0)),
229 dev_err(hba
->dev
, "%s: unable to get TX_FSM_STATE, err %d\n",
231 } else if (tx_fsm_val
!= TX_FSM_HIBERN8
) {
233 dev_err(hba
->dev
, "%s: invalid TX_FSM_STATE = %d\n",
240 static void ufs_qcom_select_unipro_mode(struct ufs_qcom_host
*host
)
242 ufshcd_rmwl(host
->hba
, QUNIPRO_SEL
,
243 ufs_qcom_cap_qunipro(host
) ? QUNIPRO_SEL
: 0,
245 /* make sure above configuration is applied before we return */
250 * ufs_qcom_host_reset - reset host controller and PHY
252 static int ufs_qcom_host_reset(struct ufs_hba
*hba
)
255 struct ufs_qcom_host
*host
= ufshcd_get_variant(hba
);
257 if (!host
->core_reset
) {
258 dev_warn(hba
->dev
, "%s: reset control not set\n", __func__
);
262 ret
= reset_control_assert(host
->core_reset
);
264 dev_err(hba
->dev
, "%s: core_reset assert failed, err = %d\n",
270 * The hardware requirement for delay between assert/deassert
271 * is at least 3-4 sleep clock (32.7KHz) cycles, which comes to
272 * ~125us (4/32768). To be on the safe side add 200us delay.
274 usleep_range(200, 210);
276 ret
= reset_control_deassert(host
->core_reset
);
278 dev_err(hba
->dev
, "%s: core_reset deassert failed, err = %d\n",
281 usleep_range(1000, 1100);
287 static int ufs_qcom_power_up_sequence(struct ufs_hba
*hba
)
289 struct ufs_qcom_host
*host
= ufshcd_get_variant(hba
);
290 struct phy
*phy
= host
->generic_phy
;
292 bool is_rate_B
= (UFS_QCOM_LIMIT_HS_RATE
== PA_HS_MODE_B
)
295 /* Reset UFS Host Controller and PHY */
296 ret
= ufs_qcom_host_reset(hba
);
298 dev_warn(hba
->dev
, "%s: host reset returned %d\n",
302 phy_set_mode(phy
, PHY_MODE_UFS_HS_B
);
304 /* phy initialization - calibrate the phy */
307 dev_err(hba
->dev
, "%s: phy init failed, ret = %d\n",
312 /* power on phy - start serdes and phy's power and clocks */
313 ret
= phy_power_on(phy
);
315 dev_err(hba
->dev
, "%s: phy power on failed, ret = %d\n",
317 goto out_disable_phy
;
320 ufs_qcom_select_unipro_mode(host
);
331 * The UTP controller has a number of internal clock gating cells (CGCs).
332 * Internal hardware sub-modules within the UTP controller control the CGCs.
333 * Hardware CGCs disable the clock to inactivate UTP sub-modules not involved
334 * in a specific operation, UTP controller CGCs are by default disabled and
335 * this function enables them (after every UFS link startup) to save some power
338 static void ufs_qcom_enable_hw_clk_gating(struct ufs_hba
*hba
)
341 ufshcd_readl(hba
, REG_UFS_CFG2
) | REG_UFS_CFG2_CGC_EN_ALL
,
344 /* Ensure that HW clock gating is enabled before next operations */
348 static int ufs_qcom_hce_enable_notify(struct ufs_hba
*hba
,
349 enum ufs_notify_change_status status
)
351 struct ufs_qcom_host
*host
= ufshcd_get_variant(hba
);
356 ufs_qcom_power_up_sequence(hba
);
358 * The PHY PLL output is the source of tx/rx lane symbol
359 * clocks, hence, enable the lane clocks only after PHY
362 err
= ufs_qcom_enable_lane_clks(host
);
365 /* check if UFS PHY moved from DISABLED to HIBERN8 */
366 err
= ufs_qcom_check_hibern8(hba
);
367 ufs_qcom_enable_hw_clk_gating(hba
);
371 dev_err(hba
->dev
, "%s: invalid status %d\n", __func__
, status
);
379 * Returns zero for success and non-zero in case of a failure
381 static int ufs_qcom_cfg_timers(struct ufs_hba
*hba
, u32 gear
,
382 u32 hs
, u32 rate
, bool update_link_startup_timer
)
385 struct ufs_qcom_host
*host
= ufshcd_get_variant(hba
);
386 struct ufs_clk_info
*clki
;
387 u32 core_clk_period_in_ns
;
388 u32 tx_clk_cycles_per_us
= 0;
389 unsigned long core_clk_rate
= 0;
390 u32 core_clk_cycles_per_us
= 0;
392 static u32 pwm_fr_table
[][2] = {
399 static u32 hs_fr_table_rA
[][2] = {
405 static u32 hs_fr_table_rB
[][2] = {
412 * The Qunipro controller does not use following registers:
413 * SYS1CLK_1US_REG, TX_SYMBOL_CLK_1US_REG, CLK_NS_REG &
414 * UFS_REG_PA_LINK_STARTUP_TIMER
415 * But UTP controller uses SYS1CLK_1US_REG register for Interrupt
418 if (ufs_qcom_cap_qunipro(host
) && !ufshcd_is_intr_aggr_allowed(hba
))
422 dev_err(hba
->dev
, "%s: invalid gear = %d\n", __func__
, gear
);
426 list_for_each_entry(clki
, &hba
->clk_list_head
, list
) {
427 if (!strcmp(clki
->name
, "core_clk"))
428 core_clk_rate
= clk_get_rate(clki
->clk
);
431 /* If frequency is smaller than 1MHz, set to 1MHz */
432 if (core_clk_rate
< DEFAULT_CLK_RATE_HZ
)
433 core_clk_rate
= DEFAULT_CLK_RATE_HZ
;
435 core_clk_cycles_per_us
= core_clk_rate
/ USEC_PER_SEC
;
436 if (ufshcd_readl(hba
, REG_UFS_SYS1CLK_1US
) != core_clk_cycles_per_us
) {
437 ufshcd_writel(hba
, core_clk_cycles_per_us
, REG_UFS_SYS1CLK_1US
);
439 * make sure above write gets applied before we return from
445 if (ufs_qcom_cap_qunipro(host
))
448 core_clk_period_in_ns
= NSEC_PER_SEC
/ core_clk_rate
;
449 core_clk_period_in_ns
<<= OFFSET_CLK_NS_REG
;
450 core_clk_period_in_ns
&= MASK_CLK_NS_REG
;
455 if (rate
== PA_HS_MODE_A
) {
456 if (gear
> ARRAY_SIZE(hs_fr_table_rA
)) {
458 "%s: index %d exceeds table size %zu\n",
460 ARRAY_SIZE(hs_fr_table_rA
));
463 tx_clk_cycles_per_us
= hs_fr_table_rA
[gear
-1][1];
464 } else if (rate
== PA_HS_MODE_B
) {
465 if (gear
> ARRAY_SIZE(hs_fr_table_rB
)) {
467 "%s: index %d exceeds table size %zu\n",
469 ARRAY_SIZE(hs_fr_table_rB
));
472 tx_clk_cycles_per_us
= hs_fr_table_rB
[gear
-1][1];
474 dev_err(hba
->dev
, "%s: invalid rate = %d\n",
481 if (gear
> ARRAY_SIZE(pwm_fr_table
)) {
483 "%s: index %d exceeds table size %zu\n",
485 ARRAY_SIZE(pwm_fr_table
));
488 tx_clk_cycles_per_us
= pwm_fr_table
[gear
-1][1];
492 dev_err(hba
->dev
, "%s: invalid mode = %d\n", __func__
, hs
);
496 if (ufshcd_readl(hba
, REG_UFS_TX_SYMBOL_CLK_NS_US
) !=
497 (core_clk_period_in_ns
| tx_clk_cycles_per_us
)) {
498 /* this register 2 fields shall be written at once */
499 ufshcd_writel(hba
, core_clk_period_in_ns
| tx_clk_cycles_per_us
,
500 REG_UFS_TX_SYMBOL_CLK_NS_US
);
502 * make sure above write gets applied before we return from
508 if (update_link_startup_timer
) {
509 ufshcd_writel(hba
, ((core_clk_rate
/ MSEC_PER_SEC
) * 100),
510 REG_UFS_PA_LINK_STARTUP_TIMER
);
512 * make sure that this configuration is applied before
525 static int ufs_qcom_link_startup_notify(struct ufs_hba
*hba
,
526 enum ufs_notify_change_status status
)
529 struct ufs_qcom_host
*host
= ufshcd_get_variant(hba
);
533 if (ufs_qcom_cfg_timers(hba
, UFS_PWM_G1
, SLOWAUTO_MODE
,
535 dev_err(hba
->dev
, "%s: ufs_qcom_cfg_timers() failed\n",
541 if (ufs_qcom_cap_qunipro(host
))
543 * set unipro core clock cycles to 150 & clear clock
546 err
= ufs_qcom_set_dme_vs_core_clk_ctrl_clear_div(hba
,
550 * Some UFS devices (and may be host) have issues if LCC is
551 * enabled. So we are setting PA_Local_TX_LCC_Enable to 0
552 * before link startup which will make sure that both host
553 * and device TX LCC are disabled once link startup is
556 if (ufshcd_get_local_unipro_ver(hba
) != UFS_UNIPRO_VER_1_41
)
557 err
= ufshcd_dme_set(hba
,
558 UIC_ARG_MIB(PA_LOCAL_TX_LCC_ENABLE
),
563 ufs_qcom_link_startup_post_change(hba
);
573 static int ufs_qcom_suspend(struct ufs_hba
*hba
, enum ufs_pm_op pm_op
)
575 struct ufs_qcom_host
*host
= ufshcd_get_variant(hba
);
576 struct phy
*phy
= host
->generic_phy
;
579 if (ufs_qcom_is_link_off(hba
)) {
581 * Disable the tx/rx lane symbol clocks before PHY is
582 * powered down as the PLL source should be disabled
583 * after downstream clocks are disabled.
585 ufs_qcom_disable_lane_clks(host
);
588 } else if (!ufs_qcom_is_link_active(hba
)) {
589 ufs_qcom_disable_lane_clks(host
);
595 static int ufs_qcom_resume(struct ufs_hba
*hba
, enum ufs_pm_op pm_op
)
597 struct ufs_qcom_host
*host
= ufshcd_get_variant(hba
);
598 struct phy
*phy
= host
->generic_phy
;
601 if (ufs_qcom_is_link_off(hba
)) {
602 err
= phy_power_on(phy
);
604 dev_err(hba
->dev
, "%s: failed PHY power on: %d\n",
609 err
= ufs_qcom_enable_lane_clks(host
);
613 } else if (!ufs_qcom_is_link_active(hba
)) {
614 err
= ufs_qcom_enable_lane_clks(host
);
619 hba
->is_sys_suspended
= false;
623 #ifdef CONFIG_MSM_BUS_SCALING
624 static int ufs_qcom_get_bus_vote(struct ufs_qcom_host
*host
,
625 const char *speed_mode
)
627 struct device
*dev
= host
->hba
->dev
;
628 struct device_node
*np
= dev
->of_node
;
630 const char *key
= "qcom,bus-vector-names";
637 if (host
->bus_vote
.is_max_bw_needed
&& !!strcmp(speed_mode
, "MIN"))
638 err
= of_property_match_string(np
, key
, "MAX");
640 err
= of_property_match_string(np
, key
, speed_mode
);
644 dev_err(dev
, "%s: Invalid %s mode %d\n",
645 __func__
, speed_mode
, err
);
649 static void ufs_qcom_get_speed_mode(struct ufs_pa_layer_attr
*p
, char *result
)
651 int gear
= max_t(u32
, p
->gear_rx
, p
->gear_tx
);
652 int lanes
= max_t(u32
, p
->lane_rx
, p
->lane_tx
);
655 /* default to PWM Gear 1, Lane 1 if power mode is not initialized */
662 if (!p
->pwr_rx
&& !p
->pwr_tx
) {
664 snprintf(result
, BUS_VECTOR_NAME_LEN
, "MIN");
665 } else if (p
->pwr_rx
== FAST_MODE
|| p
->pwr_rx
== FASTAUTO_MODE
||
666 p
->pwr_tx
== FAST_MODE
|| p
->pwr_tx
== FASTAUTO_MODE
) {
668 snprintf(result
, BUS_VECTOR_NAME_LEN
, "%s_R%s_G%d_L%d", "HS",
669 p
->hs_rate
== PA_HS_MODE_B
? "B" : "A", gear
, lanes
);
672 snprintf(result
, BUS_VECTOR_NAME_LEN
, "%s_G%d_L%d",
677 static int ufs_qcom_set_bus_vote(struct ufs_qcom_host
*host
, int vote
)
681 if (vote
!= host
->bus_vote
.curr_vote
) {
682 err
= msm_bus_scale_client_update_request(
683 host
->bus_vote
.client_handle
, vote
);
685 dev_err(host
->hba
->dev
,
686 "%s: msm_bus_scale_client_update_request() failed: bus_client_handle=0x%x, vote=%d, err=%d\n",
687 __func__
, host
->bus_vote
.client_handle
,
692 host
->bus_vote
.curr_vote
= vote
;
698 static int ufs_qcom_update_bus_bw_vote(struct ufs_qcom_host
*host
)
702 char mode
[BUS_VECTOR_NAME_LEN
];
704 ufs_qcom_get_speed_mode(&host
->dev_req_params
, mode
);
706 vote
= ufs_qcom_get_bus_vote(host
, mode
);
708 err
= ufs_qcom_set_bus_vote(host
, vote
);
713 dev_err(host
->hba
->dev
, "%s: failed %d\n", __func__
, err
);
715 host
->bus_vote
.saved_vote
= vote
;
720 show_ufs_to_mem_max_bus_bw(struct device
*dev
, struct device_attribute
*attr
,
723 struct ufs_hba
*hba
= dev_get_drvdata(dev
);
724 struct ufs_qcom_host
*host
= ufshcd_get_variant(hba
);
726 return snprintf(buf
, PAGE_SIZE
, "%u\n",
727 host
->bus_vote
.is_max_bw_needed
);
731 store_ufs_to_mem_max_bus_bw(struct device
*dev
, struct device_attribute
*attr
,
732 const char *buf
, size_t count
)
734 struct ufs_hba
*hba
= dev_get_drvdata(dev
);
735 struct ufs_qcom_host
*host
= ufshcd_get_variant(hba
);
738 if (!kstrtou32(buf
, 0, &value
)) {
739 host
->bus_vote
.is_max_bw_needed
= !!value
;
740 ufs_qcom_update_bus_bw_vote(host
);
746 static int ufs_qcom_bus_register(struct ufs_qcom_host
*host
)
749 struct msm_bus_scale_pdata
*bus_pdata
;
750 struct device
*dev
= host
->hba
->dev
;
751 struct platform_device
*pdev
= to_platform_device(dev
);
752 struct device_node
*np
= dev
->of_node
;
754 bus_pdata
= msm_bus_cl_get_pdata(pdev
);
756 dev_err(dev
, "%s: failed to get bus vectors\n", __func__
);
761 err
= of_property_count_strings(np
, "qcom,bus-vector-names");
762 if (err
< 0 || err
!= bus_pdata
->num_usecases
) {
763 dev_err(dev
, "%s: qcom,bus-vector-names not specified correctly %d\n",
768 host
->bus_vote
.client_handle
= msm_bus_scale_register_client(bus_pdata
);
769 if (!host
->bus_vote
.client_handle
) {
770 dev_err(dev
, "%s: msm_bus_scale_register_client failed\n",
776 /* cache the vote index for minimum and maximum bandwidth */
777 host
->bus_vote
.min_bw_vote
= ufs_qcom_get_bus_vote(host
, "MIN");
778 host
->bus_vote
.max_bw_vote
= ufs_qcom_get_bus_vote(host
, "MAX");
780 host
->bus_vote
.max_bus_bw
.show
= show_ufs_to_mem_max_bus_bw
;
781 host
->bus_vote
.max_bus_bw
.store
= store_ufs_to_mem_max_bus_bw
;
782 sysfs_attr_init(&host
->bus_vote
.max_bus_bw
.attr
);
783 host
->bus_vote
.max_bus_bw
.attr
.name
= "max_bus_bw";
784 host
->bus_vote
.max_bus_bw
.attr
.mode
= S_IRUGO
| S_IWUSR
;
785 err
= device_create_file(dev
, &host
->bus_vote
.max_bus_bw
);
789 #else /* CONFIG_MSM_BUS_SCALING */
790 static int ufs_qcom_update_bus_bw_vote(struct ufs_qcom_host
*host
)
795 static int ufs_qcom_set_bus_vote(struct ufs_qcom_host
*host
, int vote
)
800 static int ufs_qcom_bus_register(struct ufs_qcom_host
*host
)
804 #endif /* CONFIG_MSM_BUS_SCALING */
806 static void ufs_qcom_dev_ref_clk_ctrl(struct ufs_qcom_host
*host
, bool enable
)
808 if (host
->dev_ref_clk_ctrl_mmio
&&
809 (enable
^ host
->is_dev_ref_clk_enabled
)) {
810 u32 temp
= readl_relaxed(host
->dev_ref_clk_ctrl_mmio
);
813 temp
|= host
->dev_ref_clk_en_mask
;
815 temp
&= ~host
->dev_ref_clk_en_mask
;
818 * If we are here to disable this clock it might be immediately
819 * after entering into hibern8 in which case we need to make
820 * sure that device ref_clk is active at least 1us after the
826 writel_relaxed(temp
, host
->dev_ref_clk_ctrl_mmio
);
828 /* ensure that ref_clk is enabled/disabled before we return */
832 * If we call hibern8 exit after this, we need to make sure that
833 * device ref_clk is stable for at least 1us before the hibern8
839 host
->is_dev_ref_clk_enabled
= enable
;
843 static int ufs_qcom_pwr_change_notify(struct ufs_hba
*hba
,
844 enum ufs_notify_change_status status
,
845 struct ufs_pa_layer_attr
*dev_max_params
,
846 struct ufs_pa_layer_attr
*dev_req_params
)
848 struct ufs_qcom_host
*host
= ufshcd_get_variant(hba
);
849 struct ufs_dev_params ufs_qcom_cap
;
852 if (!dev_req_params
) {
853 pr_err("%s: incoming dev_req_params is NULL\n", __func__
);
860 ufs_qcom_cap
.tx_lanes
= UFS_QCOM_LIMIT_NUM_LANES_TX
;
861 ufs_qcom_cap
.rx_lanes
= UFS_QCOM_LIMIT_NUM_LANES_RX
;
862 ufs_qcom_cap
.hs_rx_gear
= UFS_QCOM_LIMIT_HSGEAR_RX
;
863 ufs_qcom_cap
.hs_tx_gear
= UFS_QCOM_LIMIT_HSGEAR_TX
;
864 ufs_qcom_cap
.pwm_rx_gear
= UFS_QCOM_LIMIT_PWMGEAR_RX
;
865 ufs_qcom_cap
.pwm_tx_gear
= UFS_QCOM_LIMIT_PWMGEAR_TX
;
866 ufs_qcom_cap
.rx_pwr_pwm
= UFS_QCOM_LIMIT_RX_PWR_PWM
;
867 ufs_qcom_cap
.tx_pwr_pwm
= UFS_QCOM_LIMIT_TX_PWR_PWM
;
868 ufs_qcom_cap
.rx_pwr_hs
= UFS_QCOM_LIMIT_RX_PWR_HS
;
869 ufs_qcom_cap
.tx_pwr_hs
= UFS_QCOM_LIMIT_TX_PWR_HS
;
870 ufs_qcom_cap
.hs_rate
= UFS_QCOM_LIMIT_HS_RATE
;
871 ufs_qcom_cap
.desired_working_mode
=
872 UFS_QCOM_LIMIT_DESIRED_MODE
;
874 if (host
->hw_ver
.major
== 0x1) {
876 * HS-G3 operations may not reliably work on legacy QCOM
877 * UFS host controller hardware even though capability
878 * exchange during link startup phase may end up
879 * negotiating maximum supported gear as G3.
880 * Hence downgrade the maximum supported gear to HS-G2.
882 if (ufs_qcom_cap
.hs_tx_gear
> UFS_HS_G2
)
883 ufs_qcom_cap
.hs_tx_gear
= UFS_HS_G2
;
884 if (ufs_qcom_cap
.hs_rx_gear
> UFS_HS_G2
)
885 ufs_qcom_cap
.hs_rx_gear
= UFS_HS_G2
;
888 ret
= ufshcd_get_pwr_dev_param(&ufs_qcom_cap
,
892 pr_err("%s: failed to determine capabilities\n",
897 /* enable the device ref clock before changing to HS mode */
898 if (!ufshcd_is_hs_mode(&hba
->pwr_info
) &&
899 ufshcd_is_hs_mode(dev_req_params
))
900 ufs_qcom_dev_ref_clk_ctrl(host
, true);
903 if (ufs_qcom_cfg_timers(hba
, dev_req_params
->gear_rx
,
904 dev_req_params
->pwr_rx
,
905 dev_req_params
->hs_rate
, false)) {
906 dev_err(hba
->dev
, "%s: ufs_qcom_cfg_timers() failed\n",
909 * we return error code at the end of the routine,
910 * but continue to configure UFS_PHY_TX_LANE_ENABLE
911 * and bus voting as usual
916 /* cache the power mode parameters to use internally */
917 memcpy(&host
->dev_req_params
,
918 dev_req_params
, sizeof(*dev_req_params
));
919 ufs_qcom_update_bus_bw_vote(host
);
921 /* disable the device ref clock if entered PWM mode */
922 if (ufshcd_is_hs_mode(&hba
->pwr_info
) &&
923 !ufshcd_is_hs_mode(dev_req_params
))
924 ufs_qcom_dev_ref_clk_ctrl(host
, false);
934 static int ufs_qcom_quirk_host_pa_saveconfigtime(struct ufs_hba
*hba
)
937 u32 pa_vs_config_reg1
;
939 err
= ufshcd_dme_get(hba
, UIC_ARG_MIB(PA_VS_CONFIG_REG1
),
944 /* Allow extension of MSB bits of PA_SaveConfigTime attribute */
945 err
= ufshcd_dme_set(hba
, UIC_ARG_MIB(PA_VS_CONFIG_REG1
),
946 (pa_vs_config_reg1
| (1 << 12)));
952 static int ufs_qcom_apply_dev_quirks(struct ufs_hba
*hba
)
956 if (hba
->dev_quirks
& UFS_DEVICE_QUIRK_HOST_PA_SAVECONFIGTIME
)
957 err
= ufs_qcom_quirk_host_pa_saveconfigtime(hba
);
962 static u32
ufs_qcom_get_ufs_hci_version(struct ufs_hba
*hba
)
964 struct ufs_qcom_host
*host
= ufshcd_get_variant(hba
);
966 if (host
->hw_ver
.major
== 0x1)
967 return UFSHCI_VERSION_11
;
969 return UFSHCI_VERSION_20
;
973 * ufs_qcom_advertise_quirks - advertise the known QCOM UFS controller quirks
974 * @hba: host controller instance
976 * QCOM UFS host controller might have some non standard behaviours (quirks)
977 * than what is specified by UFSHCI specification. Advertise all such
978 * quirks to standard UFS host controller driver so standard takes them into
981 static void ufs_qcom_advertise_quirks(struct ufs_hba
*hba
)
983 struct ufs_qcom_host
*host
= ufshcd_get_variant(hba
);
985 if (host
->hw_ver
.major
== 0x01) {
986 hba
->quirks
|= UFSHCD_QUIRK_DELAY_BEFORE_DME_CMDS
987 | UFSHCD_QUIRK_BROKEN_PA_RXHSUNTERMCAP
988 | UFSHCD_QUIRK_DME_PEER_ACCESS_AUTO_MODE
;
990 if (host
->hw_ver
.minor
== 0x0001 && host
->hw_ver
.step
== 0x0001)
991 hba
->quirks
|= UFSHCD_QUIRK_BROKEN_INTR_AGGR
;
993 hba
->quirks
|= UFSHCD_QUIRK_BROKEN_LCC
;
996 if (host
->hw_ver
.major
== 0x2) {
997 hba
->quirks
|= UFSHCD_QUIRK_BROKEN_UFS_HCI_VERSION
;
999 if (!ufs_qcom_cap_qunipro(host
))
1000 /* Legacy UniPro mode still need following quirks */
1001 hba
->quirks
|= (UFSHCD_QUIRK_DELAY_BEFORE_DME_CMDS
1002 | UFSHCD_QUIRK_DME_PEER_ACCESS_AUTO_MODE
1003 | UFSHCD_QUIRK_BROKEN_PA_RXHSUNTERMCAP
);
1007 static void ufs_qcom_set_caps(struct ufs_hba
*hba
)
1009 struct ufs_qcom_host
*host
= ufshcd_get_variant(hba
);
1011 hba
->caps
|= UFSHCD_CAP_CLK_GATING
| UFSHCD_CAP_HIBERN8_WITH_CLK_GATING
;
1012 hba
->caps
|= UFSHCD_CAP_CLK_SCALING
;
1013 hba
->caps
|= UFSHCD_CAP_AUTO_BKOPS_SUSPEND
;
1015 if (host
->hw_ver
.major
>= 0x2) {
1016 host
->caps
= UFS_QCOM_CAP_QUNIPRO
|
1017 UFS_QCOM_CAP_RETAIN_SEC_CFG_AFTER_PWR_COLLAPSE
;
1022 * ufs_qcom_setup_clocks - enables/disable clocks
1023 * @hba: host controller instance
1024 * @on: If true, enable clocks else disable them.
1025 * @status: PRE_CHANGE or POST_CHANGE notify
1027 * Returns 0 on success, non-zero on failure.
1029 static int ufs_qcom_setup_clocks(struct ufs_hba
*hba
, bool on
,
1030 enum ufs_notify_change_status status
)
1032 struct ufs_qcom_host
*host
= ufshcd_get_variant(hba
);
1037 * In case ufs_qcom_init() is not yet done, simply ignore.
1038 * This ufs_qcom_setup_clocks() shall be called from
1039 * ufs_qcom_init() after init is done.
1044 if (on
&& (status
== POST_CHANGE
)) {
1045 /* enable the device ref clock for HS mode*/
1046 if (ufshcd_is_hs_mode(&hba
->pwr_info
))
1047 ufs_qcom_dev_ref_clk_ctrl(host
, true);
1048 vote
= host
->bus_vote
.saved_vote
;
1049 if (vote
== host
->bus_vote
.min_bw_vote
)
1050 ufs_qcom_update_bus_bw_vote(host
);
1052 } else if (!on
&& (status
== PRE_CHANGE
)) {
1053 if (!ufs_qcom_is_link_active(hba
)) {
1054 /* disable device ref_clk */
1055 ufs_qcom_dev_ref_clk_ctrl(host
, false);
1058 vote
= host
->bus_vote
.min_bw_vote
;
1061 err
= ufs_qcom_set_bus_vote(host
, vote
);
1063 dev_err(hba
->dev
, "%s: set bus vote failed %d\n",
1070 ufs_qcom_reset_assert(struct reset_controller_dev
*rcdev
, unsigned long id
)
1072 struct ufs_qcom_host
*host
= rcdev_to_ufs_host(rcdev
);
1074 /* Currently this code only knows about a single reset. */
1076 ufs_qcom_assert_reset(host
->hba
);
1077 /* provide 1ms delay to let the reset pulse propagate. */
1078 usleep_range(1000, 1100);
1083 ufs_qcom_reset_deassert(struct reset_controller_dev
*rcdev
, unsigned long id
)
1085 struct ufs_qcom_host
*host
= rcdev_to_ufs_host(rcdev
);
1087 /* Currently this code only knows about a single reset. */
1089 ufs_qcom_deassert_reset(host
->hba
);
1092 * after reset deassertion, phy will need all ref clocks,
1093 * voltage, current to settle down before starting serdes.
1095 usleep_range(1000, 1100);
1099 static const struct reset_control_ops ufs_qcom_reset_ops
= {
1100 .assert = ufs_qcom_reset_assert
,
1101 .deassert
= ufs_qcom_reset_deassert
,
1104 #define ANDROID_BOOT_DEV_MAX 30
1105 static char android_boot_dev
[ANDROID_BOOT_DEV_MAX
];
1108 static int __init
get_android_boot_dev(char *str
)
1110 strlcpy(android_boot_dev
, str
, ANDROID_BOOT_DEV_MAX
);
1113 __setup("androidboot.bootdevice=", get_android_boot_dev
);
1117 * ufs_qcom_init - bind phy with controller
1118 * @hba: host controller instance
1120 * Binds PHY with controller and powers up PHY enabling clocks
1123 * Returns -EPROBE_DEFER if binding fails, returns negative error
1124 * on phy power up failure and returns zero on success.
1126 static int ufs_qcom_init(struct ufs_hba
*hba
)
1129 struct device
*dev
= hba
->dev
;
1130 struct platform_device
*pdev
= to_platform_device(dev
);
1131 struct ufs_qcom_host
*host
;
1132 struct resource
*res
;
1134 if (strlen(android_boot_dev
) && strcmp(android_boot_dev
, dev_name(dev
)))
1137 host
= devm_kzalloc(dev
, sizeof(*host
), GFP_KERNEL
);
1140 dev_err(dev
, "%s: no memory for qcom ufs host\n", __func__
);
1144 /* Make a two way bind between the qcom host and the hba */
1146 ufshcd_set_variant(hba
, host
);
1148 /* Setup the reset control of HCI */
1149 host
->core_reset
= devm_reset_control_get(hba
->dev
, "rst");
1150 if (IS_ERR(host
->core_reset
)) {
1151 err
= PTR_ERR(host
->core_reset
);
1152 dev_warn(dev
, "Failed to get reset control %d\n", err
);
1153 host
->core_reset
= NULL
;
1157 /* Fire up the reset controller. Failure here is non-fatal. */
1158 host
->rcdev
.of_node
= dev
->of_node
;
1159 host
->rcdev
.ops
= &ufs_qcom_reset_ops
;
1160 host
->rcdev
.owner
= dev
->driver
->owner
;
1161 host
->rcdev
.nr_resets
= 1;
1162 err
= devm_reset_controller_register(dev
, &host
->rcdev
);
1164 dev_warn(dev
, "Failed to register reset controller\n");
1169 * voting/devoting device ref_clk source is time consuming hence
1170 * skip devoting it during aggressive clock gating. This clock
1171 * will still be gated off during runtime suspend.
1173 host
->generic_phy
= devm_phy_get(dev
, "ufsphy");
1175 if (host
->generic_phy
== ERR_PTR(-EPROBE_DEFER
)) {
1177 * UFS driver might be probed before the phy driver does.
1178 * In that case we would like to return EPROBE_DEFER code.
1180 err
= -EPROBE_DEFER
;
1181 dev_warn(dev
, "%s: required phy device. hasn't probed yet. err = %d\n",
1183 goto out_variant_clear
;
1184 } else if (IS_ERR(host
->generic_phy
)) {
1185 if (has_acpi_companion(dev
)) {
1186 host
->generic_phy
= NULL
;
1188 err
= PTR_ERR(host
->generic_phy
);
1189 dev_err(dev
, "%s: PHY get failed %d\n", __func__
, err
);
1190 goto out_variant_clear
;
1194 host
->device_reset
= devm_gpiod_get_optional(dev
, "reset",
1196 if (IS_ERR(host
->device_reset
)) {
1197 err
= PTR_ERR(host
->device_reset
);
1198 if (err
!= -EPROBE_DEFER
)
1199 dev_err(dev
, "failed to acquire reset gpio: %d\n", err
);
1200 goto out_variant_clear
;
1203 err
= ufs_qcom_bus_register(host
);
1205 goto out_variant_clear
;
1207 ufs_qcom_get_controller_revision(hba
, &host
->hw_ver
.major
,
1208 &host
->hw_ver
.minor
, &host
->hw_ver
.step
);
1211 * for newer controllers, device reference clock control bit has
1212 * moved inside UFS controller register address space itself.
1214 if (host
->hw_ver
.major
>= 0x02) {
1215 host
->dev_ref_clk_ctrl_mmio
= hba
->mmio_base
+ REG_UFS_CFG1
;
1216 host
->dev_ref_clk_en_mask
= BIT(26);
1218 /* "dev_ref_clk_ctrl_mem" is optional resource */
1219 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 1);
1221 host
->dev_ref_clk_ctrl_mmio
=
1222 devm_ioremap_resource(dev
, res
);
1223 if (IS_ERR(host
->dev_ref_clk_ctrl_mmio
)) {
1225 "%s: could not map dev_ref_clk_ctrl_mmio, err %ld\n",
1227 PTR_ERR(host
->dev_ref_clk_ctrl_mmio
));
1228 host
->dev_ref_clk_ctrl_mmio
= NULL
;
1230 host
->dev_ref_clk_en_mask
= BIT(5);
1234 err
= ufs_qcom_init_lane_clks(host
);
1236 goto out_variant_clear
;
1238 ufs_qcom_set_caps(hba
);
1239 ufs_qcom_advertise_quirks(hba
);
1241 ufs_qcom_setup_clocks(hba
, true, POST_CHANGE
);
1243 if (hba
->dev
->id
< MAX_UFS_QCOM_HOSTS
)
1244 ufs_qcom_hosts
[hba
->dev
->id
] = host
;
1246 host
->dbg_print_en
|= UFS_QCOM_DEFAULT_DBG_PRINT_EN
;
1247 ufs_qcom_get_default_testbus_cfg(host
);
1248 err
= ufs_qcom_testbus_config(host
);
1250 dev_warn(dev
, "%s: failed to configure the testbus %d\n",
1258 ufshcd_set_variant(hba
, NULL
);
1263 static void ufs_qcom_exit(struct ufs_hba
*hba
)
1265 struct ufs_qcom_host
*host
= ufshcd_get_variant(hba
);
1267 ufs_qcom_disable_lane_clks(host
);
1268 phy_power_off(host
->generic_phy
);
1269 phy_exit(host
->generic_phy
);
1272 static int ufs_qcom_set_dme_vs_core_clk_ctrl_clear_div(struct ufs_hba
*hba
,
1276 u32 core_clk_ctrl_reg
;
1278 if (clk_cycles
> DME_VS_CORE_CLK_CTRL_MAX_CORE_CLK_1US_CYCLES_MASK
)
1281 err
= ufshcd_dme_get(hba
,
1282 UIC_ARG_MIB(DME_VS_CORE_CLK_CTRL
),
1283 &core_clk_ctrl_reg
);
1287 core_clk_ctrl_reg
&= ~DME_VS_CORE_CLK_CTRL_MAX_CORE_CLK_1US_CYCLES_MASK
;
1288 core_clk_ctrl_reg
|= clk_cycles
;
1290 /* Clear CORE_CLK_DIV_EN */
1291 core_clk_ctrl_reg
&= ~DME_VS_CORE_CLK_CTRL_CORE_CLK_DIV_EN_BIT
;
1293 err
= ufshcd_dme_set(hba
,
1294 UIC_ARG_MIB(DME_VS_CORE_CLK_CTRL
),
1300 static int ufs_qcom_clk_scale_up_pre_change(struct ufs_hba
*hba
)
1302 /* nothing to do as of now */
1306 static int ufs_qcom_clk_scale_up_post_change(struct ufs_hba
*hba
)
1308 struct ufs_qcom_host
*host
= ufshcd_get_variant(hba
);
1310 if (!ufs_qcom_cap_qunipro(host
))
1313 /* set unipro core clock cycles to 150 and clear clock divider */
1314 return ufs_qcom_set_dme_vs_core_clk_ctrl_clear_div(hba
, 150);
1317 static int ufs_qcom_clk_scale_down_pre_change(struct ufs_hba
*hba
)
1319 struct ufs_qcom_host
*host
= ufshcd_get_variant(hba
);
1321 u32 core_clk_ctrl_reg
;
1323 if (!ufs_qcom_cap_qunipro(host
))
1326 err
= ufshcd_dme_get(hba
,
1327 UIC_ARG_MIB(DME_VS_CORE_CLK_CTRL
),
1328 &core_clk_ctrl_reg
);
1330 /* make sure CORE_CLK_DIV_EN is cleared */
1332 (core_clk_ctrl_reg
& DME_VS_CORE_CLK_CTRL_CORE_CLK_DIV_EN_BIT
)) {
1333 core_clk_ctrl_reg
&= ~DME_VS_CORE_CLK_CTRL_CORE_CLK_DIV_EN_BIT
;
1334 err
= ufshcd_dme_set(hba
,
1335 UIC_ARG_MIB(DME_VS_CORE_CLK_CTRL
),
1342 static int ufs_qcom_clk_scale_down_post_change(struct ufs_hba
*hba
)
1344 struct ufs_qcom_host
*host
= ufshcd_get_variant(hba
);
1346 if (!ufs_qcom_cap_qunipro(host
))
1349 /* set unipro core clock cycles to 75 and clear clock divider */
1350 return ufs_qcom_set_dme_vs_core_clk_ctrl_clear_div(hba
, 75);
1353 static int ufs_qcom_clk_scale_notify(struct ufs_hba
*hba
,
1354 bool scale_up
, enum ufs_notify_change_status status
)
1356 struct ufs_qcom_host
*host
= ufshcd_get_variant(hba
);
1357 struct ufs_pa_layer_attr
*dev_req_params
= &host
->dev_req_params
;
1360 if (status
== PRE_CHANGE
) {
1362 err
= ufs_qcom_clk_scale_up_pre_change(hba
);
1364 err
= ufs_qcom_clk_scale_down_pre_change(hba
);
1367 err
= ufs_qcom_clk_scale_up_post_change(hba
);
1369 err
= ufs_qcom_clk_scale_down_post_change(hba
);
1371 if (err
|| !dev_req_params
)
1374 ufs_qcom_cfg_timers(hba
,
1375 dev_req_params
->gear_rx
,
1376 dev_req_params
->pwr_rx
,
1377 dev_req_params
->hs_rate
,
1379 ufs_qcom_update_bus_bw_vote(host
);
1386 static void ufs_qcom_print_hw_debug_reg_all(struct ufs_hba
*hba
,
1387 void *priv
, void (*print_fn
)(struct ufs_hba
*hba
,
1388 int offset
, int num_regs
, const char *str
, void *priv
))
1391 struct ufs_qcom_host
*host
;
1393 if (unlikely(!hba
)) {
1394 pr_err("%s: hba is NULL\n", __func__
);
1397 if (unlikely(!print_fn
)) {
1398 dev_err(hba
->dev
, "%s: print_fn is NULL\n", __func__
);
1402 host
= ufshcd_get_variant(hba
);
1403 if (!(host
->dbg_print_en
& UFS_QCOM_DBG_PRINT_REGS_EN
))
1406 reg
= ufs_qcom_get_debug_reg_offset(host
, UFS_UFS_DBG_RD_REG_OCSC
);
1407 print_fn(hba
, reg
, 44, "UFS_UFS_DBG_RD_REG_OCSC ", priv
);
1409 reg
= ufshcd_readl(hba
, REG_UFS_CFG1
);
1410 reg
|= UTP_DBG_RAMS_EN
;
1411 ufshcd_writel(hba
, reg
, REG_UFS_CFG1
);
1413 reg
= ufs_qcom_get_debug_reg_offset(host
, UFS_UFS_DBG_RD_EDTL_RAM
);
1414 print_fn(hba
, reg
, 32, "UFS_UFS_DBG_RD_EDTL_RAM ", priv
);
1416 reg
= ufs_qcom_get_debug_reg_offset(host
, UFS_UFS_DBG_RD_DESC_RAM
);
1417 print_fn(hba
, reg
, 128, "UFS_UFS_DBG_RD_DESC_RAM ", priv
);
1419 reg
= ufs_qcom_get_debug_reg_offset(host
, UFS_UFS_DBG_RD_PRDT_RAM
);
1420 print_fn(hba
, reg
, 64, "UFS_UFS_DBG_RD_PRDT_RAM ", priv
);
1422 /* clear bit 17 - UTP_DBG_RAMS_EN */
1423 ufshcd_rmwl(hba
, UTP_DBG_RAMS_EN
, 0, REG_UFS_CFG1
);
1425 reg
= ufs_qcom_get_debug_reg_offset(host
, UFS_DBG_RD_REG_UAWM
);
1426 print_fn(hba
, reg
, 4, "UFS_DBG_RD_REG_UAWM ", priv
);
1428 reg
= ufs_qcom_get_debug_reg_offset(host
, UFS_DBG_RD_REG_UARM
);
1429 print_fn(hba
, reg
, 4, "UFS_DBG_RD_REG_UARM ", priv
);
1431 reg
= ufs_qcom_get_debug_reg_offset(host
, UFS_DBG_RD_REG_TXUC
);
1432 print_fn(hba
, reg
, 48, "UFS_DBG_RD_REG_TXUC ", priv
);
1434 reg
= ufs_qcom_get_debug_reg_offset(host
, UFS_DBG_RD_REG_RXUC
);
1435 print_fn(hba
, reg
, 27, "UFS_DBG_RD_REG_RXUC ", priv
);
1437 reg
= ufs_qcom_get_debug_reg_offset(host
, UFS_DBG_RD_REG_DFC
);
1438 print_fn(hba
, reg
, 19, "UFS_DBG_RD_REG_DFC ", priv
);
1440 reg
= ufs_qcom_get_debug_reg_offset(host
, UFS_DBG_RD_REG_TRLUT
);
1441 print_fn(hba
, reg
, 34, "UFS_DBG_RD_REG_TRLUT ", priv
);
1443 reg
= ufs_qcom_get_debug_reg_offset(host
, UFS_DBG_RD_REG_TMRLUT
);
1444 print_fn(hba
, reg
, 9, "UFS_DBG_RD_REG_TMRLUT ", priv
);
1447 static void ufs_qcom_enable_test_bus(struct ufs_qcom_host
*host
)
1449 if (host
->dbg_print_en
& UFS_QCOM_DBG_PRINT_TEST_BUS_EN
) {
1450 ufshcd_rmwl(host
->hba
, UFS_REG_TEST_BUS_EN
,
1451 UFS_REG_TEST_BUS_EN
, REG_UFS_CFG1
);
1452 ufshcd_rmwl(host
->hba
, TEST_BUS_EN
, TEST_BUS_EN
, REG_UFS_CFG1
);
1454 ufshcd_rmwl(host
->hba
, UFS_REG_TEST_BUS_EN
, 0, REG_UFS_CFG1
);
1455 ufshcd_rmwl(host
->hba
, TEST_BUS_EN
, 0, REG_UFS_CFG1
);
1459 static void ufs_qcom_get_default_testbus_cfg(struct ufs_qcom_host
*host
)
1461 /* provide a legal default configuration */
1462 host
->testbus
.select_major
= TSTBUS_UNIPRO
;
1463 host
->testbus
.select_minor
= 37;
1466 static bool ufs_qcom_testbus_cfg_is_ok(struct ufs_qcom_host
*host
)
1468 if (host
->testbus
.select_major
>= TSTBUS_MAX
) {
1469 dev_err(host
->hba
->dev
,
1470 "%s: UFS_CFG1[TEST_BUS_SEL} may not equal 0x%05X\n",
1471 __func__
, host
->testbus
.select_major
);
1478 int ufs_qcom_testbus_config(struct ufs_qcom_host
*host
)
1482 u32 mask
= TEST_BUS_SUB_SEL_MASK
;
1487 if (!ufs_qcom_testbus_cfg_is_ok(host
))
1490 switch (host
->testbus
.select_major
) {
1492 reg
= UFS_TEST_BUS_CTRL_0
;
1496 reg
= UFS_TEST_BUS_CTRL_0
;
1500 reg
= UFS_TEST_BUS_CTRL_0
;
1504 reg
= UFS_TEST_BUS_CTRL_0
;
1508 reg
= UFS_TEST_BUS_CTRL_1
;
1512 reg
= UFS_TEST_BUS_CTRL_1
;
1516 reg
= UFS_TEST_BUS_CTRL_1
;
1520 reg
= UFS_TEST_BUS_CTRL_1
;
1523 case TSTBUS_WRAPPER
:
1524 reg
= UFS_TEST_BUS_CTRL_2
;
1527 case TSTBUS_COMBINED
:
1528 reg
= UFS_TEST_BUS_CTRL_2
;
1531 case TSTBUS_UTP_HCI
:
1532 reg
= UFS_TEST_BUS_CTRL_2
;
1536 reg
= UFS_UNIPRO_CFG
;
1541 * No need for a default case, since
1542 * ufs_qcom_testbus_cfg_is_ok() checks that the configuration
1548 pm_runtime_get_sync(host
->hba
->dev
);
1549 ufshcd_hold(host
->hba
, false);
1550 ufshcd_rmwl(host
->hba
, TEST_BUS_SEL
,
1551 (u32
)host
->testbus
.select_major
<< 19,
1553 ufshcd_rmwl(host
->hba
, mask
,
1554 (u32
)host
->testbus
.select_minor
<< offset
,
1556 ufs_qcom_enable_test_bus(host
);
1558 * Make sure the test bus configuration is
1559 * committed before returning.
1562 ufshcd_release(host
->hba
);
1563 pm_runtime_put_sync(host
->hba
->dev
);
1568 static void ufs_qcom_testbus_read(struct ufs_hba
*hba
)
1570 ufshcd_dump_regs(hba
, UFS_TEST_BUS
, 4, "UFS_TEST_BUS ");
1573 static void ufs_qcom_print_unipro_testbus(struct ufs_hba
*hba
)
1575 struct ufs_qcom_host
*host
= ufshcd_get_variant(hba
);
1576 u32
*testbus
= NULL
;
1577 int i
, nminor
= 256, testbus_len
= nminor
* sizeof(u32
);
1579 testbus
= kmalloc(testbus_len
, GFP_KERNEL
);
1583 host
->testbus
.select_major
= TSTBUS_UNIPRO
;
1584 for (i
= 0; i
< nminor
; i
++) {
1585 host
->testbus
.select_minor
= i
;
1586 ufs_qcom_testbus_config(host
);
1587 testbus
[i
] = ufshcd_readl(hba
, UFS_TEST_BUS
);
1589 print_hex_dump(KERN_ERR
, "UNIPRO_TEST_BUS ", DUMP_PREFIX_OFFSET
,
1590 16, 4, testbus
, testbus_len
, false);
1594 static void ufs_qcom_dump_dbg_regs(struct ufs_hba
*hba
)
1596 ufshcd_dump_regs(hba
, REG_UFS_SYS1CLK_1US
, 16 * 4,
1597 "HCI Vendor Specific Registers ");
1599 /* sleep a bit intermittently as we are dumping too much data */
1600 ufs_qcom_print_hw_debug_reg_all(hba
, NULL
, ufs_qcom_dump_regs_wrapper
);
1601 usleep_range(1000, 1100);
1602 ufs_qcom_testbus_read(hba
);
1603 usleep_range(1000, 1100);
1604 ufs_qcom_print_unipro_testbus(hba
);
1605 usleep_range(1000, 1100);
1609 * ufs_qcom_device_reset() - toggle the (optional) device reset line
1610 * @hba: per-adapter instance
1612 * Toggles the (optional) reset line to reset the attached device.
1614 static void ufs_qcom_device_reset(struct ufs_hba
*hba
)
1616 struct ufs_qcom_host
*host
= ufshcd_get_variant(hba
);
1618 /* reset gpio is optional */
1619 if (!host
->device_reset
)
1623 * The UFS device shall detect reset pulses of 1us, sleep for 10us to
1624 * be on the safe side.
1626 gpiod_set_value_cansleep(host
->device_reset
, 1);
1627 usleep_range(10, 15);
1629 gpiod_set_value_cansleep(host
->device_reset
, 0);
1630 usleep_range(10, 15);
1634 * struct ufs_hba_qcom_vops - UFS QCOM specific variant operations
1636 * The variant operations configure the necessary controller and PHY
1637 * handshake during initialization.
1639 static const struct ufs_hba_variant_ops ufs_hba_qcom_vops
= {
1641 .init
= ufs_qcom_init
,
1642 .exit
= ufs_qcom_exit
,
1643 .get_ufs_hci_version
= ufs_qcom_get_ufs_hci_version
,
1644 .clk_scale_notify
= ufs_qcom_clk_scale_notify
,
1645 .setup_clocks
= ufs_qcom_setup_clocks
,
1646 .hce_enable_notify
= ufs_qcom_hce_enable_notify
,
1647 .link_startup_notify
= ufs_qcom_link_startup_notify
,
1648 .pwr_change_notify
= ufs_qcom_pwr_change_notify
,
1649 .apply_dev_quirks
= ufs_qcom_apply_dev_quirks
,
1650 .suspend
= ufs_qcom_suspend
,
1651 .resume
= ufs_qcom_resume
,
1652 .dbg_register_dump
= ufs_qcom_dump_dbg_regs
,
1653 .device_reset
= ufs_qcom_device_reset
,
1657 * ufs_qcom_probe - probe routine of the driver
1658 * @pdev: pointer to Platform device handle
1660 * Return zero for success and non-zero for failure
1662 static int ufs_qcom_probe(struct platform_device
*pdev
)
1665 struct device
*dev
= &pdev
->dev
;
1667 /* Perform generic probe */
1668 err
= ufshcd_pltfrm_init(pdev
, &ufs_hba_qcom_vops
);
1670 dev_err(dev
, "ufshcd_pltfrm_init() failed %d\n", err
);
1676 * ufs_qcom_remove - set driver_data of the device to NULL
1677 * @pdev: pointer to platform device handle
1681 static int ufs_qcom_remove(struct platform_device
*pdev
)
1683 struct ufs_hba
*hba
= platform_get_drvdata(pdev
);
1685 pm_runtime_get_sync(&(pdev
)->dev
);
1690 static const struct of_device_id ufs_qcom_of_match
[] = {
1691 { .compatible
= "qcom,ufshc"},
1694 MODULE_DEVICE_TABLE(of
, ufs_qcom_of_match
);
1697 static const struct acpi_device_id ufs_qcom_acpi_match
[] = {
1701 MODULE_DEVICE_TABLE(acpi
, ufs_qcom_acpi_match
);
1704 static const struct dev_pm_ops ufs_qcom_pm_ops
= {
1705 .suspend
= ufshcd_pltfrm_suspend
,
1706 .resume
= ufshcd_pltfrm_resume
,
1707 .runtime_suspend
= ufshcd_pltfrm_runtime_suspend
,
1708 .runtime_resume
= ufshcd_pltfrm_runtime_resume
,
1709 .runtime_idle
= ufshcd_pltfrm_runtime_idle
,
1712 static struct platform_driver ufs_qcom_pltform
= {
1713 .probe
= ufs_qcom_probe
,
1714 .remove
= ufs_qcom_remove
,
1715 .shutdown
= ufshcd_pltfrm_shutdown
,
1717 .name
= "ufshcd-qcom",
1718 .pm
= &ufs_qcom_pm_ops
,
1719 .of_match_table
= of_match_ptr(ufs_qcom_of_match
),
1720 .acpi_match_table
= ACPI_PTR(ufs_qcom_acpi_match
),
1723 module_platform_driver(ufs_qcom_pltform
);
1725 MODULE_LICENSE("GPL v2");