2 * Copyright (c) 2013-2016, Linux Foundation. All rights reserved.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 and
6 * only version 2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
15 #include <linux/time.h>
17 #include <linux/platform_device.h>
18 #include <linux/phy/phy.h>
19 #include <linux/reset-controller.h>
22 #include "ufshcd-pltfrm.h"
26 #include "ufs_quirks.h"
27 #define UFS_QCOM_DEFAULT_DBG_PRINT_EN \
28 (UFS_QCOM_DBG_PRINT_REGS_EN | UFS_QCOM_DBG_PRINT_TEST_BUS_EN)
46 static struct ufs_qcom_host *ufs_qcom_hosts[MAX_UFS_QCOM_HOSTS];
48 static int ufs_qcom_set_bus_vote(struct ufs_qcom_host *host, int vote);
49 static void ufs_qcom_get_default_testbus_cfg(struct ufs_qcom_host *host);
50 static int ufs_qcom_set_dme_vs_core_clk_ctrl_clear_div(struct ufs_hba *hba,
53 static struct ufs_qcom_host *rcdev_to_ufs_host(struct reset_controller_dev *rcd)
55 return container_of(rcd, struct ufs_qcom_host, rcdev);
58 static void ufs_qcom_dump_regs_wrapper(struct ufs_hba *hba, int offset, int len,
59 const char *prefix, void *priv)
61 ufshcd_dump_regs(hba, offset, len * 4, prefix);
64 static int ufs_qcom_get_connected_tx_lanes(struct ufs_hba *hba, u32 *tx_lanes)
68 err = ufshcd_dme_get(hba,
69 UIC_ARG_MIB(PA_CONNECTEDTXDATALANES), tx_lanes);
71 dev_err(hba->dev, "%s: couldn't read PA_CONNECTEDTXDATALANES %d\n",
77 static int ufs_qcom_host_clk_get(struct device *dev,
78 const char *name, struct clk **clk_out, bool optional)
83 clk = devm_clk_get(dev, name);
91 if (optional && err == -ENOENT) {
96 if (err != -EPROBE_DEFER)
97 dev_err(dev, "failed to get %s err %d\n", name, err);
102 static int ufs_qcom_host_clk_enable(struct device *dev,
103 const char *name, struct clk *clk)
107 err = clk_prepare_enable(clk);
109 dev_err(dev, "%s: %s enable failed %d\n", __func__, name, err);
114 static void ufs_qcom_disable_lane_clks(struct ufs_qcom_host *host)
116 if (!host->is_lane_clks_enabled)
119 clk_disable_unprepare(host->tx_l1_sync_clk);
120 clk_disable_unprepare(host->tx_l0_sync_clk);
121 clk_disable_unprepare(host->rx_l1_sync_clk);
122 clk_disable_unprepare(host->rx_l0_sync_clk);
124 host->is_lane_clks_enabled = false;
127 static int ufs_qcom_enable_lane_clks(struct ufs_qcom_host *host)
130 struct device *dev = host->hba->dev;
132 if (host->is_lane_clks_enabled)
135 err = ufs_qcom_host_clk_enable(dev, "rx_lane0_sync_clk",
136 host->rx_l0_sync_clk);
140 err = ufs_qcom_host_clk_enable(dev, "tx_lane0_sync_clk",
141 host->tx_l0_sync_clk);
145 err = ufs_qcom_host_clk_enable(dev, "rx_lane1_sync_clk",
146 host->rx_l1_sync_clk);
150 err = ufs_qcom_host_clk_enable(dev, "tx_lane1_sync_clk",
151 host->tx_l1_sync_clk);
155 host->is_lane_clks_enabled = true;
159 clk_disable_unprepare(host->rx_l1_sync_clk);
161 clk_disable_unprepare(host->tx_l0_sync_clk);
163 clk_disable_unprepare(host->rx_l0_sync_clk);
168 static int ufs_qcom_init_lane_clks(struct ufs_qcom_host *host)
171 struct device *dev = host->hba->dev;
173 err = ufs_qcom_host_clk_get(dev, "rx_lane0_sync_clk",
174 &host->rx_l0_sync_clk, false);
178 err = ufs_qcom_host_clk_get(dev, "tx_lane0_sync_clk",
179 &host->tx_l0_sync_clk, false);
183 /* In case of single lane per direction, don't read lane1 clocks */
184 if (host->hba->lanes_per_direction > 1) {
185 err = ufs_qcom_host_clk_get(dev, "rx_lane1_sync_clk",
186 &host->rx_l1_sync_clk, false);
190 err = ufs_qcom_host_clk_get(dev, "tx_lane1_sync_clk",
191 &host->tx_l1_sync_clk, true);
197 static int ufs_qcom_link_startup_post_change(struct ufs_hba *hba)
201 return ufs_qcom_get_connected_tx_lanes(hba, &tx_lanes);
204 static int ufs_qcom_check_hibern8(struct ufs_hba *hba)
208 unsigned long timeout = jiffies + msecs_to_jiffies(HBRN8_POLL_TOUT_MS);
211 err = ufshcd_dme_get(hba,
212 UIC_ARG_MIB_SEL(MPHY_TX_FSM_STATE,
213 UIC_ARG_MPHY_TX_GEN_SEL_INDEX(0)),
215 if (err || tx_fsm_val == TX_FSM_HIBERN8)
218 /* sleep for max. 200us */
219 usleep_range(100, 200);
220 } while (time_before(jiffies, timeout));
223 * we might have scheduled out for long during polling so
224 * check the state again.
226 if (time_after(jiffies, timeout))
227 err = ufshcd_dme_get(hba,
228 UIC_ARG_MIB_SEL(MPHY_TX_FSM_STATE,
229 UIC_ARG_MPHY_TX_GEN_SEL_INDEX(0)),
233 dev_err(hba->dev, "%s: unable to get TX_FSM_STATE, err %d\n",
235 } else if (tx_fsm_val != TX_FSM_HIBERN8) {
237 dev_err(hba->dev, "%s: invalid TX_FSM_STATE = %d\n",
244 static void ufs_qcom_select_unipro_mode(struct ufs_qcom_host *host)
246 ufshcd_rmwl(host->hba, QUNIPRO_SEL,
247 ufs_qcom_cap_qunipro(host) ? QUNIPRO_SEL : 0,
249 /* make sure above configuration is applied before we return */
253 static int ufs_qcom_power_up_sequence(struct ufs_hba *hba)
255 struct ufs_qcom_host *host = ufshcd_get_variant(hba);
256 struct phy *phy = host->generic_phy;
258 bool is_rate_B = (UFS_QCOM_LIMIT_HS_RATE == PA_HS_MODE_B)
262 phy_set_mode(phy, PHY_MODE_UFS_HS_B);
264 /* phy initialization - calibrate the phy */
267 dev_err(hba->dev, "%s: phy init failed, ret = %d\n",
272 /* power on phy - start serdes and phy's power and clocks */
273 ret = phy_power_on(phy);
275 dev_err(hba->dev, "%s: phy power on failed, ret = %d\n",
277 goto out_disable_phy;
280 ufs_qcom_select_unipro_mode(host);
291 * The UTP controller has a number of internal clock gating cells (CGCs).
292 * Internal hardware sub-modules within the UTP controller control the CGCs.
293 * Hardware CGCs disable the clock to inactivate UTP sub-modules not involved
294 * in a specific operation, UTP controller CGCs are by default disabled and
295 * this function enables them (after every UFS link startup) to save some power
298 static void ufs_qcom_enable_hw_clk_gating(struct ufs_hba *hba)
301 ufshcd_readl(hba, REG_UFS_CFG2) | REG_UFS_CFG2_CGC_EN_ALL,
304 /* Ensure that HW clock gating is enabled before next operations */
308 static int ufs_qcom_hce_enable_notify(struct ufs_hba *hba,
309 enum ufs_notify_change_status status)
311 struct ufs_qcom_host *host = ufshcd_get_variant(hba);
316 ufs_qcom_power_up_sequence(hba);
318 * The PHY PLL output is the source of tx/rx lane symbol
319 * clocks, hence, enable the lane clocks only after PHY
322 err = ufs_qcom_enable_lane_clks(host);
325 /* check if UFS PHY moved from DISABLED to HIBERN8 */
326 err = ufs_qcom_check_hibern8(hba);
327 ufs_qcom_enable_hw_clk_gating(hba);
331 dev_err(hba->dev, "%s: invalid status %d\n", __func__, status);
339 * Returns zero for success and non-zero in case of a failure
341 static int ufs_qcom_cfg_timers(struct ufs_hba *hba, u32 gear,
342 u32 hs, u32 rate, bool update_link_startup_timer)
345 struct ufs_qcom_host *host = ufshcd_get_variant(hba);
346 struct ufs_clk_info *clki;
347 u32 core_clk_period_in_ns;
348 u32 tx_clk_cycles_per_us = 0;
349 unsigned long core_clk_rate = 0;
350 u32 core_clk_cycles_per_us = 0;
352 static u32 pwm_fr_table[][2] = {
359 static u32 hs_fr_table_rA[][2] = {
365 static u32 hs_fr_table_rB[][2] = {
372 * The Qunipro controller does not use following registers:
373 * SYS1CLK_1US_REG, TX_SYMBOL_CLK_1US_REG, CLK_NS_REG &
374 * UFS_REG_PA_LINK_STARTUP_TIMER
375 * But UTP controller uses SYS1CLK_1US_REG register for Interrupt
378 if (ufs_qcom_cap_qunipro(host) && !ufshcd_is_intr_aggr_allowed(hba))
382 dev_err(hba->dev, "%s: invalid gear = %d\n", __func__, gear);
386 list_for_each_entry(clki, &hba->clk_list_head, list) {
387 if (!strcmp(clki->name, "core_clk"))
388 core_clk_rate = clk_get_rate(clki->clk);
391 /* If frequency is smaller than 1MHz, set to 1MHz */
392 if (core_clk_rate < DEFAULT_CLK_RATE_HZ)
393 core_clk_rate = DEFAULT_CLK_RATE_HZ;
395 core_clk_cycles_per_us = core_clk_rate / USEC_PER_SEC;
396 if (ufshcd_readl(hba, REG_UFS_SYS1CLK_1US) != core_clk_cycles_per_us) {
397 ufshcd_writel(hba, core_clk_cycles_per_us, REG_UFS_SYS1CLK_1US);
399 * make sure above write gets applied before we return from
405 if (ufs_qcom_cap_qunipro(host))
408 core_clk_period_in_ns = NSEC_PER_SEC / core_clk_rate;
409 core_clk_period_in_ns <<= OFFSET_CLK_NS_REG;
410 core_clk_period_in_ns &= MASK_CLK_NS_REG;
415 if (rate == PA_HS_MODE_A) {
416 if (gear > ARRAY_SIZE(hs_fr_table_rA)) {
418 "%s: index %d exceeds table size %zu\n",
420 ARRAY_SIZE(hs_fr_table_rA));
423 tx_clk_cycles_per_us = hs_fr_table_rA[gear-1][1];
424 } else if (rate == PA_HS_MODE_B) {
425 if (gear > ARRAY_SIZE(hs_fr_table_rB)) {
427 "%s: index %d exceeds table size %zu\n",
429 ARRAY_SIZE(hs_fr_table_rB));
432 tx_clk_cycles_per_us = hs_fr_table_rB[gear-1][1];
434 dev_err(hba->dev, "%s: invalid rate = %d\n",
441 if (gear > ARRAY_SIZE(pwm_fr_table)) {
443 "%s: index %d exceeds table size %zu\n",
445 ARRAY_SIZE(pwm_fr_table));
448 tx_clk_cycles_per_us = pwm_fr_table[gear-1][1];
452 dev_err(hba->dev, "%s: invalid mode = %d\n", __func__, hs);
456 if (ufshcd_readl(hba, REG_UFS_TX_SYMBOL_CLK_NS_US) !=
457 (core_clk_period_in_ns | tx_clk_cycles_per_us)) {
458 /* this register 2 fields shall be written at once */
459 ufshcd_writel(hba, core_clk_period_in_ns | tx_clk_cycles_per_us,
460 REG_UFS_TX_SYMBOL_CLK_NS_US);
462 * make sure above write gets applied before we return from
468 if (update_link_startup_timer) {
469 ufshcd_writel(hba, ((core_clk_rate / MSEC_PER_SEC) * 100),
470 REG_UFS_PA_LINK_STARTUP_TIMER);
472 * make sure that this configuration is applied before
485 static int ufs_qcom_link_startup_notify(struct ufs_hba *hba,
486 enum ufs_notify_change_status status)
489 struct ufs_qcom_host *host = ufshcd_get_variant(hba);
493 if (ufs_qcom_cfg_timers(hba, UFS_PWM_G1, SLOWAUTO_MODE,
495 dev_err(hba->dev, "%s: ufs_qcom_cfg_timers() failed\n",
501 if (ufs_qcom_cap_qunipro(host))
503 * set unipro core clock cycles to 150 & clear clock
506 err = ufs_qcom_set_dme_vs_core_clk_ctrl_clear_div(hba,
510 * Some UFS devices (and may be host) have issues if LCC is
511 * enabled. So we are setting PA_Local_TX_LCC_Enable to 0
512 * before link startup which will make sure that both host
513 * and device TX LCC are disabled once link startup is
516 if (ufshcd_get_local_unipro_ver(hba) != UFS_UNIPRO_VER_1_41)
517 err = ufshcd_dme_set(hba,
518 UIC_ARG_MIB(PA_LOCAL_TX_LCC_ENABLE),
523 ufs_qcom_link_startup_post_change(hba);
533 static int ufs_qcom_suspend(struct ufs_hba *hba, enum ufs_pm_op pm_op)
535 struct ufs_qcom_host *host = ufshcd_get_variant(hba);
536 struct phy *phy = host->generic_phy;
539 if (ufs_qcom_is_link_off(hba)) {
541 * Disable the tx/rx lane symbol clocks before PHY is
542 * powered down as the PLL source should be disabled
543 * after downstream clocks are disabled.
545 ufs_qcom_disable_lane_clks(host);
548 } else if (!ufs_qcom_is_link_active(hba)) {
549 ufs_qcom_disable_lane_clks(host);
555 static int ufs_qcom_resume(struct ufs_hba *hba, enum ufs_pm_op pm_op)
557 struct ufs_qcom_host *host = ufshcd_get_variant(hba);
558 struct phy *phy = host->generic_phy;
561 if (ufs_qcom_is_link_off(hba)) {
562 err = phy_power_on(phy);
564 dev_err(hba->dev, "%s: failed PHY power on: %d\n",
569 err = ufs_qcom_enable_lane_clks(host);
573 } else if (!ufs_qcom_is_link_active(hba)) {
574 err = ufs_qcom_enable_lane_clks(host);
579 hba->is_sys_suspended = false;
583 struct ufs_qcom_dev_params {
584 u32 pwm_rx_gear; /* pwm rx gear to work in */
585 u32 pwm_tx_gear; /* pwm tx gear to work in */
586 u32 hs_rx_gear; /* hs rx gear to work in */
587 u32 hs_tx_gear; /* hs tx gear to work in */
588 u32 rx_lanes; /* number of rx lanes */
589 u32 tx_lanes; /* number of tx lanes */
590 u32 rx_pwr_pwm; /* rx pwm working pwr */
591 u32 tx_pwr_pwm; /* tx pwm working pwr */
592 u32 rx_pwr_hs; /* rx hs working pwr */
593 u32 tx_pwr_hs; /* tx hs working pwr */
594 u32 hs_rate; /* rate A/B to work in HS */
595 u32 desired_working_mode;
598 static int ufs_qcom_get_pwr_dev_param(struct ufs_qcom_dev_params *qcom_param,
599 struct ufs_pa_layer_attr *dev_max,
600 struct ufs_pa_layer_attr *agreed_pwr)
604 bool is_dev_sup_hs = false;
605 bool is_qcom_max_hs = false;
607 if (dev_max->pwr_rx == FAST_MODE)
608 is_dev_sup_hs = true;
610 if (qcom_param->desired_working_mode == FAST) {
611 is_qcom_max_hs = true;
612 min_qcom_gear = min_t(u32, qcom_param->hs_rx_gear,
613 qcom_param->hs_tx_gear);
615 min_qcom_gear = min_t(u32, qcom_param->pwm_rx_gear,
616 qcom_param->pwm_tx_gear);
620 * device doesn't support HS but qcom_param->desired_working_mode is
621 * HS, thus device and qcom_param don't agree
623 if (!is_dev_sup_hs && is_qcom_max_hs) {
624 pr_err("%s: failed to agree on power mode (device doesn't support HS but requested power is HS)\n",
627 } else if (is_dev_sup_hs && is_qcom_max_hs) {
629 * since device supports HS, it supports FAST_MODE.
630 * since qcom_param->desired_working_mode is also HS
631 * then final decision (FAST/FASTAUTO) is done according
632 * to qcom_params as it is the restricting factor
634 agreed_pwr->pwr_rx = agreed_pwr->pwr_tx =
635 qcom_param->rx_pwr_hs;
638 * here qcom_param->desired_working_mode is PWM.
639 * it doesn't matter whether device supports HS or PWM,
640 * in both cases qcom_param->desired_working_mode will
643 agreed_pwr->pwr_rx = agreed_pwr->pwr_tx =
644 qcom_param->rx_pwr_pwm;
648 * we would like tx to work in the minimum number of lanes
649 * between device capability and vendor preferences.
650 * the same decision will be made for rx
652 agreed_pwr->lane_tx = min_t(u32, dev_max->lane_tx,
653 qcom_param->tx_lanes);
654 agreed_pwr->lane_rx = min_t(u32, dev_max->lane_rx,
655 qcom_param->rx_lanes);
657 /* device maximum gear is the minimum between device rx and tx gears */
658 min_dev_gear = min_t(u32, dev_max->gear_rx, dev_max->gear_tx);
661 * if both device capabilities and vendor pre-defined preferences are
662 * both HS or both PWM then set the minimum gear to be the chosen
664 * if one is PWM and one is HS then the one that is PWM get to decide
665 * what is the gear, as it is the one that also decided previously what
666 * pwr the device will be configured to.
668 if ((is_dev_sup_hs && is_qcom_max_hs) ||
669 (!is_dev_sup_hs && !is_qcom_max_hs))
670 agreed_pwr->gear_rx = agreed_pwr->gear_tx =
671 min_t(u32, min_dev_gear, min_qcom_gear);
672 else if (!is_dev_sup_hs)
673 agreed_pwr->gear_rx = agreed_pwr->gear_tx = min_dev_gear;
675 agreed_pwr->gear_rx = agreed_pwr->gear_tx = min_qcom_gear;
677 agreed_pwr->hs_rate = qcom_param->hs_rate;
681 #ifdef CONFIG_MSM_BUS_SCALING
682 static int ufs_qcom_get_bus_vote(struct ufs_qcom_host *host,
683 const char *speed_mode)
685 struct device *dev = host->hba->dev;
686 struct device_node *np = dev->of_node;
688 const char *key = "qcom,bus-vector-names";
695 if (host->bus_vote.is_max_bw_needed && !!strcmp(speed_mode, "MIN"))
696 err = of_property_match_string(np, key, "MAX");
698 err = of_property_match_string(np, key, speed_mode);
702 dev_err(dev, "%s: Invalid %s mode %d\n",
703 __func__, speed_mode, err);
707 static void ufs_qcom_get_speed_mode(struct ufs_pa_layer_attr *p, char *result)
709 int gear = max_t(u32, p->gear_rx, p->gear_tx);
710 int lanes = max_t(u32, p->lane_rx, p->lane_tx);
713 /* default to PWM Gear 1, Lane 1 if power mode is not initialized */
720 if (!p->pwr_rx && !p->pwr_tx) {
722 snprintf(result, BUS_VECTOR_NAME_LEN, "MIN");
723 } else if (p->pwr_rx == FAST_MODE || p->pwr_rx == FASTAUTO_MODE ||
724 p->pwr_tx == FAST_MODE || p->pwr_tx == FASTAUTO_MODE) {
726 snprintf(result, BUS_VECTOR_NAME_LEN, "%s_R%s_G%d_L%d", "HS",
727 p->hs_rate == PA_HS_MODE_B ? "B" : "A", gear, lanes);
730 snprintf(result, BUS_VECTOR_NAME_LEN, "%s_G%d_L%d",
735 static int ufs_qcom_set_bus_vote(struct ufs_qcom_host *host, int vote)
739 if (vote != host->bus_vote.curr_vote) {
740 err = msm_bus_scale_client_update_request(
741 host->bus_vote.client_handle, vote);
743 dev_err(host->hba->dev,
744 "%s: msm_bus_scale_client_update_request() failed: bus_client_handle=0x%x, vote=%d, err=%d\n",
745 __func__, host->bus_vote.client_handle,
750 host->bus_vote.curr_vote = vote;
756 static int ufs_qcom_update_bus_bw_vote(struct ufs_qcom_host *host)
760 char mode[BUS_VECTOR_NAME_LEN];
762 ufs_qcom_get_speed_mode(&host->dev_req_params, mode);
764 vote = ufs_qcom_get_bus_vote(host, mode);
766 err = ufs_qcom_set_bus_vote(host, vote);
771 dev_err(host->hba->dev, "%s: failed %d\n", __func__, err);
773 host->bus_vote.saved_vote = vote;
778 show_ufs_to_mem_max_bus_bw(struct device *dev, struct device_attribute *attr,
781 struct ufs_hba *hba = dev_get_drvdata(dev);
782 struct ufs_qcom_host *host = ufshcd_get_variant(hba);
784 return snprintf(buf, PAGE_SIZE, "%u\n",
785 host->bus_vote.is_max_bw_needed);
789 store_ufs_to_mem_max_bus_bw(struct device *dev, struct device_attribute *attr,
790 const char *buf, size_t count)
792 struct ufs_hba *hba = dev_get_drvdata(dev);
793 struct ufs_qcom_host *host = ufshcd_get_variant(hba);
796 if (!kstrtou32(buf, 0, &value)) {
797 host->bus_vote.is_max_bw_needed = !!value;
798 ufs_qcom_update_bus_bw_vote(host);
804 static int ufs_qcom_bus_register(struct ufs_qcom_host *host)
807 struct msm_bus_scale_pdata *bus_pdata;
808 struct device *dev = host->hba->dev;
809 struct platform_device *pdev = to_platform_device(dev);
810 struct device_node *np = dev->of_node;
812 bus_pdata = msm_bus_cl_get_pdata(pdev);
814 dev_err(dev, "%s: failed to get bus vectors\n", __func__);
819 err = of_property_count_strings(np, "qcom,bus-vector-names");
820 if (err < 0 || err != bus_pdata->num_usecases) {
821 dev_err(dev, "%s: qcom,bus-vector-names not specified correctly %d\n",
826 host->bus_vote.client_handle = msm_bus_scale_register_client(bus_pdata);
827 if (!host->bus_vote.client_handle) {
828 dev_err(dev, "%s: msm_bus_scale_register_client failed\n",
834 /* cache the vote index for minimum and maximum bandwidth */
835 host->bus_vote.min_bw_vote = ufs_qcom_get_bus_vote(host, "MIN");
836 host->bus_vote.max_bw_vote = ufs_qcom_get_bus_vote(host, "MAX");
838 host->bus_vote.max_bus_bw.show = show_ufs_to_mem_max_bus_bw;
839 host->bus_vote.max_bus_bw.store = store_ufs_to_mem_max_bus_bw;
840 sysfs_attr_init(&host->bus_vote.max_bus_bw.attr);
841 host->bus_vote.max_bus_bw.attr.name = "max_bus_bw";
842 host->bus_vote.max_bus_bw.attr.mode = S_IRUGO | S_IWUSR;
843 err = device_create_file(dev, &host->bus_vote.max_bus_bw);
847 #else /* CONFIG_MSM_BUS_SCALING */
848 static int ufs_qcom_update_bus_bw_vote(struct ufs_qcom_host *host)
853 static int ufs_qcom_set_bus_vote(struct ufs_qcom_host *host, int vote)
858 static int ufs_qcom_bus_register(struct ufs_qcom_host *host)
862 #endif /* CONFIG_MSM_BUS_SCALING */
864 static void ufs_qcom_dev_ref_clk_ctrl(struct ufs_qcom_host *host, bool enable)
866 if (host->dev_ref_clk_ctrl_mmio &&
867 (enable ^ host->is_dev_ref_clk_enabled)) {
868 u32 temp = readl_relaxed(host->dev_ref_clk_ctrl_mmio);
871 temp |= host->dev_ref_clk_en_mask;
873 temp &= ~host->dev_ref_clk_en_mask;
876 * If we are here to disable this clock it might be immediately
877 * after entering into hibern8 in which case we need to make
878 * sure that device ref_clk is active at least 1us after the
884 writel_relaxed(temp, host->dev_ref_clk_ctrl_mmio);
886 /* ensure that ref_clk is enabled/disabled before we return */
890 * If we call hibern8 exit after this, we need to make sure that
891 * device ref_clk is stable for at least 1us before the hibern8
897 host->is_dev_ref_clk_enabled = enable;
901 static int ufs_qcom_pwr_change_notify(struct ufs_hba *hba,
902 enum ufs_notify_change_status status,
903 struct ufs_pa_layer_attr *dev_max_params,
904 struct ufs_pa_layer_attr *dev_req_params)
907 struct ufs_qcom_host *host = ufshcd_get_variant(hba);
908 struct ufs_qcom_dev_params ufs_qcom_cap;
911 if (!dev_req_params) {
912 pr_err("%s: incoming dev_req_params is NULL\n", __func__);
919 ufs_qcom_cap.tx_lanes = UFS_QCOM_LIMIT_NUM_LANES_TX;
920 ufs_qcom_cap.rx_lanes = UFS_QCOM_LIMIT_NUM_LANES_RX;
921 ufs_qcom_cap.hs_rx_gear = UFS_QCOM_LIMIT_HSGEAR_RX;
922 ufs_qcom_cap.hs_tx_gear = UFS_QCOM_LIMIT_HSGEAR_TX;
923 ufs_qcom_cap.pwm_rx_gear = UFS_QCOM_LIMIT_PWMGEAR_RX;
924 ufs_qcom_cap.pwm_tx_gear = UFS_QCOM_LIMIT_PWMGEAR_TX;
925 ufs_qcom_cap.rx_pwr_pwm = UFS_QCOM_LIMIT_RX_PWR_PWM;
926 ufs_qcom_cap.tx_pwr_pwm = UFS_QCOM_LIMIT_TX_PWR_PWM;
927 ufs_qcom_cap.rx_pwr_hs = UFS_QCOM_LIMIT_RX_PWR_HS;
928 ufs_qcom_cap.tx_pwr_hs = UFS_QCOM_LIMIT_TX_PWR_HS;
929 ufs_qcom_cap.hs_rate = UFS_QCOM_LIMIT_HS_RATE;
930 ufs_qcom_cap.desired_working_mode =
931 UFS_QCOM_LIMIT_DESIRED_MODE;
933 if (host->hw_ver.major == 0x1) {
935 * HS-G3 operations may not reliably work on legacy QCOM
936 * UFS host controller hardware even though capability
937 * exchange during link startup phase may end up
938 * negotiating maximum supported gear as G3.
939 * Hence downgrade the maximum supported gear to HS-G2.
941 if (ufs_qcom_cap.hs_tx_gear > UFS_HS_G2)
942 ufs_qcom_cap.hs_tx_gear = UFS_HS_G2;
943 if (ufs_qcom_cap.hs_rx_gear > UFS_HS_G2)
944 ufs_qcom_cap.hs_rx_gear = UFS_HS_G2;
947 ret = ufs_qcom_get_pwr_dev_param(&ufs_qcom_cap,
951 pr_err("%s: failed to determine capabilities\n",
956 /* enable the device ref clock before changing to HS mode */
957 if (!ufshcd_is_hs_mode(&hba->pwr_info) &&
958 ufshcd_is_hs_mode(dev_req_params))
959 ufs_qcom_dev_ref_clk_ctrl(host, true);
962 if (ufs_qcom_cfg_timers(hba, dev_req_params->gear_rx,
963 dev_req_params->pwr_rx,
964 dev_req_params->hs_rate, false)) {
965 dev_err(hba->dev, "%s: ufs_qcom_cfg_timers() failed\n",
968 * we return error code at the end of the routine,
969 * but continue to configure UFS_PHY_TX_LANE_ENABLE
970 * and bus voting as usual
975 val = ~(MAX_U32 << dev_req_params->lane_tx);
977 /* cache the power mode parameters to use internally */
978 memcpy(&host->dev_req_params,
979 dev_req_params, sizeof(*dev_req_params));
980 ufs_qcom_update_bus_bw_vote(host);
982 /* disable the device ref clock if entered PWM mode */
983 if (ufshcd_is_hs_mode(&hba->pwr_info) &&
984 !ufshcd_is_hs_mode(dev_req_params))
985 ufs_qcom_dev_ref_clk_ctrl(host, false);
995 static int ufs_qcom_quirk_host_pa_saveconfigtime(struct ufs_hba *hba)
998 u32 pa_vs_config_reg1;
1000 err = ufshcd_dme_get(hba, UIC_ARG_MIB(PA_VS_CONFIG_REG1),
1001 &pa_vs_config_reg1);
1005 /* Allow extension of MSB bits of PA_SaveConfigTime attribute */
1006 err = ufshcd_dme_set(hba, UIC_ARG_MIB(PA_VS_CONFIG_REG1),
1007 (pa_vs_config_reg1 | (1 << 12)));
1013 static int ufs_qcom_apply_dev_quirks(struct ufs_hba *hba)
1017 if (hba->dev_quirks & UFS_DEVICE_QUIRK_HOST_PA_SAVECONFIGTIME)
1018 err = ufs_qcom_quirk_host_pa_saveconfigtime(hba);
1023 static u32 ufs_qcom_get_ufs_hci_version(struct ufs_hba *hba)
1025 struct ufs_qcom_host *host = ufshcd_get_variant(hba);
1027 if (host->hw_ver.major == 0x1)
1028 return UFSHCI_VERSION_11;
1030 return UFSHCI_VERSION_20;
1034 * ufs_qcom_advertise_quirks - advertise the known QCOM UFS controller quirks
1035 * @hba: host controller instance
1037 * QCOM UFS host controller might have some non standard behaviours (quirks)
1038 * than what is specified by UFSHCI specification. Advertise all such
1039 * quirks to standard UFS host controller driver so standard takes them into
1042 static void ufs_qcom_advertise_quirks(struct ufs_hba *hba)
1044 struct ufs_qcom_host *host = ufshcd_get_variant(hba);
1046 if (host->hw_ver.major == 0x01) {
1047 hba->quirks |= UFSHCD_QUIRK_DELAY_BEFORE_DME_CMDS
1048 | UFSHCD_QUIRK_BROKEN_PA_RXHSUNTERMCAP
1049 | UFSHCD_QUIRK_DME_PEER_ACCESS_AUTO_MODE;
1051 if (host->hw_ver.minor == 0x0001 && host->hw_ver.step == 0x0001)
1052 hba->quirks |= UFSHCD_QUIRK_BROKEN_INTR_AGGR;
1054 hba->quirks |= UFSHCD_QUIRK_BROKEN_LCC;
1057 if (host->hw_ver.major == 0x2) {
1058 hba->quirks |= UFSHCD_QUIRK_BROKEN_UFS_HCI_VERSION;
1060 if (!ufs_qcom_cap_qunipro(host))
1061 /* Legacy UniPro mode still need following quirks */
1062 hba->quirks |= (UFSHCD_QUIRK_DELAY_BEFORE_DME_CMDS
1063 | UFSHCD_QUIRK_DME_PEER_ACCESS_AUTO_MODE
1064 | UFSHCD_QUIRK_BROKEN_PA_RXHSUNTERMCAP);
1068 static void ufs_qcom_set_caps(struct ufs_hba *hba)
1070 struct ufs_qcom_host *host = ufshcd_get_variant(hba);
1072 hba->caps |= UFSHCD_CAP_CLK_GATING | UFSHCD_CAP_HIBERN8_WITH_CLK_GATING;
1073 hba->caps |= UFSHCD_CAP_CLK_SCALING;
1074 hba->caps |= UFSHCD_CAP_AUTO_BKOPS_SUSPEND;
1076 if (host->hw_ver.major >= 0x2) {
1077 host->caps = UFS_QCOM_CAP_QUNIPRO |
1078 UFS_QCOM_CAP_RETAIN_SEC_CFG_AFTER_PWR_COLLAPSE;
1083 * ufs_qcom_setup_clocks - enables/disable clocks
1084 * @hba: host controller instance
1085 * @on: If true, enable clocks else disable them.
1086 * @status: PRE_CHANGE or POST_CHANGE notify
1088 * Returns 0 on success, non-zero on failure.
1090 static int ufs_qcom_setup_clocks(struct ufs_hba *hba, bool on,
1091 enum ufs_notify_change_status status)
1093 struct ufs_qcom_host *host = ufshcd_get_variant(hba);
1098 * In case ufs_qcom_init() is not yet done, simply ignore.
1099 * This ufs_qcom_setup_clocks() shall be called from
1100 * ufs_qcom_init() after init is done.
1105 if (on && (status == POST_CHANGE)) {
1106 /* enable the device ref clock for HS mode*/
1107 if (ufshcd_is_hs_mode(&hba->pwr_info))
1108 ufs_qcom_dev_ref_clk_ctrl(host, true);
1109 vote = host->bus_vote.saved_vote;
1110 if (vote == host->bus_vote.min_bw_vote)
1111 ufs_qcom_update_bus_bw_vote(host);
1113 } else if (!on && (status == PRE_CHANGE)) {
1114 if (!ufs_qcom_is_link_active(hba)) {
1115 /* disable device ref_clk */
1116 ufs_qcom_dev_ref_clk_ctrl(host, false);
1119 vote = host->bus_vote.min_bw_vote;
1122 err = ufs_qcom_set_bus_vote(host, vote);
1124 dev_err(hba->dev, "%s: set bus vote failed %d\n",
1131 ufs_qcom_reset_assert(struct reset_controller_dev *rcdev, unsigned long id)
1133 struct ufs_qcom_host *host = rcdev_to_ufs_host(rcdev);
1135 /* Currently this code only knows about a single reset. */
1137 ufs_qcom_assert_reset(host->hba);
1138 /* provide 1ms delay to let the reset pulse propagate. */
1139 usleep_range(1000, 1100);
1144 ufs_qcom_reset_deassert(struct reset_controller_dev *rcdev, unsigned long id)
1146 struct ufs_qcom_host *host = rcdev_to_ufs_host(rcdev);
1148 /* Currently this code only knows about a single reset. */
1150 ufs_qcom_deassert_reset(host->hba);
1153 * after reset deassertion, phy will need all ref clocks,
1154 * voltage, current to settle down before starting serdes.
1156 usleep_range(1000, 1100);
1160 static const struct reset_control_ops ufs_qcom_reset_ops = {
1161 .assert = ufs_qcom_reset_assert,
1162 .deassert = ufs_qcom_reset_deassert,
1165 #define ANDROID_BOOT_DEV_MAX 30
1166 static char android_boot_dev[ANDROID_BOOT_DEV_MAX];
1169 static int __init get_android_boot_dev(char *str)
1171 strlcpy(android_boot_dev, str, ANDROID_BOOT_DEV_MAX);
1174 __setup("androidboot.bootdevice=", get_android_boot_dev);
1178 * ufs_qcom_init - bind phy with controller
1179 * @hba: host controller instance
1181 * Binds PHY with controller and powers up PHY enabling clocks
1184 * Returns -EPROBE_DEFER if binding fails, returns negative error
1185 * on phy power up failure and returns zero on success.
1187 static int ufs_qcom_init(struct ufs_hba *hba)
1190 struct device *dev = hba->dev;
1191 struct platform_device *pdev = to_platform_device(dev);
1192 struct ufs_qcom_host *host;
1193 struct resource *res;
1195 if (strlen(android_boot_dev) && strcmp(android_boot_dev, dev_name(dev)))
1198 host = devm_kzalloc(dev, sizeof(*host), GFP_KERNEL);
1201 dev_err(dev, "%s: no memory for qcom ufs host\n", __func__);
1205 /* Make a two way bind between the qcom host and the hba */
1207 ufshcd_set_variant(hba, host);
1209 /* Fire up the reset controller. Failure here is non-fatal. */
1210 host->rcdev.of_node = dev->of_node;
1211 host->rcdev.ops = &ufs_qcom_reset_ops;
1212 host->rcdev.owner = dev->driver->owner;
1213 host->rcdev.nr_resets = 1;
1214 err = devm_reset_controller_register(dev, &host->rcdev);
1216 dev_warn(dev, "Failed to register reset controller\n");
1221 * voting/devoting device ref_clk source is time consuming hence
1222 * skip devoting it during aggressive clock gating. This clock
1223 * will still be gated off during runtime suspend.
1225 host->generic_phy = devm_phy_get(dev, "ufsphy");
1227 if (host->generic_phy == ERR_PTR(-EPROBE_DEFER)) {
1229 * UFS driver might be probed before the phy driver does.
1230 * In that case we would like to return EPROBE_DEFER code.
1232 err = -EPROBE_DEFER;
1233 dev_warn(dev, "%s: required phy device. hasn't probed yet. err = %d\n",
1235 goto out_variant_clear;
1236 } else if (IS_ERR(host->generic_phy)) {
1237 err = PTR_ERR(host->generic_phy);
1238 dev_err(dev, "%s: PHY get failed %d\n", __func__, err);
1239 goto out_variant_clear;
1242 err = ufs_qcom_bus_register(host);
1244 goto out_variant_clear;
1246 ufs_qcom_get_controller_revision(hba, &host->hw_ver.major,
1247 &host->hw_ver.minor, &host->hw_ver.step);
1250 * for newer controllers, device reference clock control bit has
1251 * moved inside UFS controller register address space itself.
1253 if (host->hw_ver.major >= 0x02) {
1254 host->dev_ref_clk_ctrl_mmio = hba->mmio_base + REG_UFS_CFG1;
1255 host->dev_ref_clk_en_mask = BIT(26);
1257 /* "dev_ref_clk_ctrl_mem" is optional resource */
1258 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1260 host->dev_ref_clk_ctrl_mmio =
1261 devm_ioremap_resource(dev, res);
1262 if (IS_ERR(host->dev_ref_clk_ctrl_mmio)) {
1264 "%s: could not map dev_ref_clk_ctrl_mmio, err %ld\n",
1266 PTR_ERR(host->dev_ref_clk_ctrl_mmio));
1267 host->dev_ref_clk_ctrl_mmio = NULL;
1269 host->dev_ref_clk_en_mask = BIT(5);
1273 err = ufs_qcom_init_lane_clks(host);
1275 goto out_variant_clear;
1277 ufs_qcom_set_caps(hba);
1278 ufs_qcom_advertise_quirks(hba);
1280 ufs_qcom_setup_clocks(hba, true, POST_CHANGE);
1282 if (hba->dev->id < MAX_UFS_QCOM_HOSTS)
1283 ufs_qcom_hosts[hba->dev->id] = host;
1285 host->dbg_print_en |= UFS_QCOM_DEFAULT_DBG_PRINT_EN;
1286 ufs_qcom_get_default_testbus_cfg(host);
1287 err = ufs_qcom_testbus_config(host);
1289 dev_warn(dev, "%s: failed to configure the testbus %d\n",
1297 ufshcd_set_variant(hba, NULL);
1302 static void ufs_qcom_exit(struct ufs_hba *hba)
1304 struct ufs_qcom_host *host = ufshcd_get_variant(hba);
1306 ufs_qcom_disable_lane_clks(host);
1307 phy_power_off(host->generic_phy);
1308 phy_exit(host->generic_phy);
1311 static int ufs_qcom_set_dme_vs_core_clk_ctrl_clear_div(struct ufs_hba *hba,
1315 u32 core_clk_ctrl_reg;
1317 if (clk_cycles > DME_VS_CORE_CLK_CTRL_MAX_CORE_CLK_1US_CYCLES_MASK)
1320 err = ufshcd_dme_get(hba,
1321 UIC_ARG_MIB(DME_VS_CORE_CLK_CTRL),
1322 &core_clk_ctrl_reg);
1326 core_clk_ctrl_reg &= ~DME_VS_CORE_CLK_CTRL_MAX_CORE_CLK_1US_CYCLES_MASK;
1327 core_clk_ctrl_reg |= clk_cycles;
1329 /* Clear CORE_CLK_DIV_EN */
1330 core_clk_ctrl_reg &= ~DME_VS_CORE_CLK_CTRL_CORE_CLK_DIV_EN_BIT;
1332 err = ufshcd_dme_set(hba,
1333 UIC_ARG_MIB(DME_VS_CORE_CLK_CTRL),
1339 static int ufs_qcom_clk_scale_up_pre_change(struct ufs_hba *hba)
1341 /* nothing to do as of now */
1345 static int ufs_qcom_clk_scale_up_post_change(struct ufs_hba *hba)
1347 struct ufs_qcom_host *host = ufshcd_get_variant(hba);
1349 if (!ufs_qcom_cap_qunipro(host))
1352 /* set unipro core clock cycles to 150 and clear clock divider */
1353 return ufs_qcom_set_dme_vs_core_clk_ctrl_clear_div(hba, 150);
1356 static int ufs_qcom_clk_scale_down_pre_change(struct ufs_hba *hba)
1358 struct ufs_qcom_host *host = ufshcd_get_variant(hba);
1360 u32 core_clk_ctrl_reg;
1362 if (!ufs_qcom_cap_qunipro(host))
1365 err = ufshcd_dme_get(hba,
1366 UIC_ARG_MIB(DME_VS_CORE_CLK_CTRL),
1367 &core_clk_ctrl_reg);
1369 /* make sure CORE_CLK_DIV_EN is cleared */
1371 (core_clk_ctrl_reg & DME_VS_CORE_CLK_CTRL_CORE_CLK_DIV_EN_BIT)) {
1372 core_clk_ctrl_reg &= ~DME_VS_CORE_CLK_CTRL_CORE_CLK_DIV_EN_BIT;
1373 err = ufshcd_dme_set(hba,
1374 UIC_ARG_MIB(DME_VS_CORE_CLK_CTRL),
1381 static int ufs_qcom_clk_scale_down_post_change(struct ufs_hba *hba)
1383 struct ufs_qcom_host *host = ufshcd_get_variant(hba);
1385 if (!ufs_qcom_cap_qunipro(host))
1388 /* set unipro core clock cycles to 75 and clear clock divider */
1389 return ufs_qcom_set_dme_vs_core_clk_ctrl_clear_div(hba, 75);
1392 static int ufs_qcom_clk_scale_notify(struct ufs_hba *hba,
1393 bool scale_up, enum ufs_notify_change_status status)
1395 struct ufs_qcom_host *host = ufshcd_get_variant(hba);
1396 struct ufs_pa_layer_attr *dev_req_params = &host->dev_req_params;
1399 if (status == PRE_CHANGE) {
1401 err = ufs_qcom_clk_scale_up_pre_change(hba);
1403 err = ufs_qcom_clk_scale_down_pre_change(hba);
1406 err = ufs_qcom_clk_scale_up_post_change(hba);
1408 err = ufs_qcom_clk_scale_down_post_change(hba);
1410 if (err || !dev_req_params)
1413 ufs_qcom_cfg_timers(hba,
1414 dev_req_params->gear_rx,
1415 dev_req_params->pwr_rx,
1416 dev_req_params->hs_rate,
1418 ufs_qcom_update_bus_bw_vote(host);
1425 static void ufs_qcom_print_hw_debug_reg_all(struct ufs_hba *hba,
1426 void *priv, void (*print_fn)(struct ufs_hba *hba,
1427 int offset, int num_regs, const char *str, void *priv))
1430 struct ufs_qcom_host *host;
1432 if (unlikely(!hba)) {
1433 pr_err("%s: hba is NULL\n", __func__);
1436 if (unlikely(!print_fn)) {
1437 dev_err(hba->dev, "%s: print_fn is NULL\n", __func__);
1441 host = ufshcd_get_variant(hba);
1442 if (!(host->dbg_print_en & UFS_QCOM_DBG_PRINT_REGS_EN))
1445 reg = ufs_qcom_get_debug_reg_offset(host, UFS_UFS_DBG_RD_REG_OCSC);
1446 print_fn(hba, reg, 44, "UFS_UFS_DBG_RD_REG_OCSC ", priv);
1448 reg = ufshcd_readl(hba, REG_UFS_CFG1);
1449 reg |= UTP_DBG_RAMS_EN;
1450 ufshcd_writel(hba, reg, REG_UFS_CFG1);
1452 reg = ufs_qcom_get_debug_reg_offset(host, UFS_UFS_DBG_RD_EDTL_RAM);
1453 print_fn(hba, reg, 32, "UFS_UFS_DBG_RD_EDTL_RAM ", priv);
1455 reg = ufs_qcom_get_debug_reg_offset(host, UFS_UFS_DBG_RD_DESC_RAM);
1456 print_fn(hba, reg, 128, "UFS_UFS_DBG_RD_DESC_RAM ", priv);
1458 reg = ufs_qcom_get_debug_reg_offset(host, UFS_UFS_DBG_RD_PRDT_RAM);
1459 print_fn(hba, reg, 64, "UFS_UFS_DBG_RD_PRDT_RAM ", priv);
1461 /* clear bit 17 - UTP_DBG_RAMS_EN */
1462 ufshcd_rmwl(hba, UTP_DBG_RAMS_EN, 0, REG_UFS_CFG1);
1464 reg = ufs_qcom_get_debug_reg_offset(host, UFS_DBG_RD_REG_UAWM);
1465 print_fn(hba, reg, 4, "UFS_DBG_RD_REG_UAWM ", priv);
1467 reg = ufs_qcom_get_debug_reg_offset(host, UFS_DBG_RD_REG_UARM);
1468 print_fn(hba, reg, 4, "UFS_DBG_RD_REG_UARM ", priv);
1470 reg = ufs_qcom_get_debug_reg_offset(host, UFS_DBG_RD_REG_TXUC);
1471 print_fn(hba, reg, 48, "UFS_DBG_RD_REG_TXUC ", priv);
1473 reg = ufs_qcom_get_debug_reg_offset(host, UFS_DBG_RD_REG_RXUC);
1474 print_fn(hba, reg, 27, "UFS_DBG_RD_REG_RXUC ", priv);
1476 reg = ufs_qcom_get_debug_reg_offset(host, UFS_DBG_RD_REG_DFC);
1477 print_fn(hba, reg, 19, "UFS_DBG_RD_REG_DFC ", priv);
1479 reg = ufs_qcom_get_debug_reg_offset(host, UFS_DBG_RD_REG_TRLUT);
1480 print_fn(hba, reg, 34, "UFS_DBG_RD_REG_TRLUT ", priv);
1482 reg = ufs_qcom_get_debug_reg_offset(host, UFS_DBG_RD_REG_TMRLUT);
1483 print_fn(hba, reg, 9, "UFS_DBG_RD_REG_TMRLUT ", priv);
1486 static void ufs_qcom_enable_test_bus(struct ufs_qcom_host *host)
1488 if (host->dbg_print_en & UFS_QCOM_DBG_PRINT_TEST_BUS_EN) {
1489 ufshcd_rmwl(host->hba, UFS_REG_TEST_BUS_EN,
1490 UFS_REG_TEST_BUS_EN, REG_UFS_CFG1);
1491 ufshcd_rmwl(host->hba, TEST_BUS_EN, TEST_BUS_EN, REG_UFS_CFG1);
1493 ufshcd_rmwl(host->hba, UFS_REG_TEST_BUS_EN, 0, REG_UFS_CFG1);
1494 ufshcd_rmwl(host->hba, TEST_BUS_EN, 0, REG_UFS_CFG1);
1498 static void ufs_qcom_get_default_testbus_cfg(struct ufs_qcom_host *host)
1500 /* provide a legal default configuration */
1501 host->testbus.select_major = TSTBUS_UNIPRO;
1502 host->testbus.select_minor = 37;
1505 static bool ufs_qcom_testbus_cfg_is_ok(struct ufs_qcom_host *host)
1507 if (host->testbus.select_major >= TSTBUS_MAX) {
1508 dev_err(host->hba->dev,
1509 "%s: UFS_CFG1[TEST_BUS_SEL} may not equal 0x%05X\n",
1510 __func__, host->testbus.select_major);
1517 int ufs_qcom_testbus_config(struct ufs_qcom_host *host)
1521 u32 mask = TEST_BUS_SUB_SEL_MASK;
1526 if (!ufs_qcom_testbus_cfg_is_ok(host))
1529 switch (host->testbus.select_major) {
1531 reg = UFS_TEST_BUS_CTRL_0;
1535 reg = UFS_TEST_BUS_CTRL_0;
1539 reg = UFS_TEST_BUS_CTRL_0;
1543 reg = UFS_TEST_BUS_CTRL_0;
1547 reg = UFS_TEST_BUS_CTRL_1;
1551 reg = UFS_TEST_BUS_CTRL_1;
1555 reg = UFS_TEST_BUS_CTRL_1;
1559 reg = UFS_TEST_BUS_CTRL_1;
1562 case TSTBUS_WRAPPER:
1563 reg = UFS_TEST_BUS_CTRL_2;
1566 case TSTBUS_COMBINED:
1567 reg = UFS_TEST_BUS_CTRL_2;
1570 case TSTBUS_UTP_HCI:
1571 reg = UFS_TEST_BUS_CTRL_2;
1575 reg = UFS_UNIPRO_CFG;
1580 * No need for a default case, since
1581 * ufs_qcom_testbus_cfg_is_ok() checks that the configuration
1587 pm_runtime_get_sync(host->hba->dev);
1588 ufshcd_hold(host->hba, false);
1589 ufshcd_rmwl(host->hba, TEST_BUS_SEL,
1590 (u32)host->testbus.select_major << 19,
1592 ufshcd_rmwl(host->hba, mask,
1593 (u32)host->testbus.select_minor << offset,
1595 ufs_qcom_enable_test_bus(host);
1597 * Make sure the test bus configuration is
1598 * committed before returning.
1601 ufshcd_release(host->hba);
1602 pm_runtime_put_sync(host->hba->dev);
1607 static void ufs_qcom_testbus_read(struct ufs_hba *hba)
1609 ufshcd_dump_regs(hba, UFS_TEST_BUS, 4, "UFS_TEST_BUS ");
1612 static void ufs_qcom_print_unipro_testbus(struct ufs_hba *hba)
1614 struct ufs_qcom_host *host = ufshcd_get_variant(hba);
1615 u32 *testbus = NULL;
1616 int i, nminor = 256, testbus_len = nminor * sizeof(u32);
1618 testbus = kmalloc(testbus_len, GFP_KERNEL);
1622 host->testbus.select_major = TSTBUS_UNIPRO;
1623 for (i = 0; i < nminor; i++) {
1624 host->testbus.select_minor = i;
1625 ufs_qcom_testbus_config(host);
1626 testbus[i] = ufshcd_readl(hba, UFS_TEST_BUS);
1628 print_hex_dump(KERN_ERR, "UNIPRO_TEST_BUS ", DUMP_PREFIX_OFFSET,
1629 16, 4, testbus, testbus_len, false);
1633 static void ufs_qcom_dump_dbg_regs(struct ufs_hba *hba)
1635 ufshcd_dump_regs(hba, REG_UFS_SYS1CLK_1US, 16 * 4,
1636 "HCI Vendor Specific Registers ");
1638 /* sleep a bit intermittently as we are dumping too much data */
1639 ufs_qcom_print_hw_debug_reg_all(hba, NULL, ufs_qcom_dump_regs_wrapper);
1640 usleep_range(1000, 1100);
1641 ufs_qcom_testbus_read(hba);
1642 usleep_range(1000, 1100);
1643 ufs_qcom_print_unipro_testbus(hba);
1644 usleep_range(1000, 1100);
1648 * struct ufs_hba_qcom_vops - UFS QCOM specific variant operations
1650 * The variant operations configure the necessary controller and PHY
1651 * handshake during initialization.
1653 static struct ufs_hba_variant_ops ufs_hba_qcom_vops = {
1655 .init = ufs_qcom_init,
1656 .exit = ufs_qcom_exit,
1657 .get_ufs_hci_version = ufs_qcom_get_ufs_hci_version,
1658 .clk_scale_notify = ufs_qcom_clk_scale_notify,
1659 .setup_clocks = ufs_qcom_setup_clocks,
1660 .hce_enable_notify = ufs_qcom_hce_enable_notify,
1661 .link_startup_notify = ufs_qcom_link_startup_notify,
1662 .pwr_change_notify = ufs_qcom_pwr_change_notify,
1663 .apply_dev_quirks = ufs_qcom_apply_dev_quirks,
1664 .suspend = ufs_qcom_suspend,
1665 .resume = ufs_qcom_resume,
1666 .dbg_register_dump = ufs_qcom_dump_dbg_regs,
1670 * ufs_qcom_probe - probe routine of the driver
1671 * @pdev: pointer to Platform device handle
1673 * Return zero for success and non-zero for failure
1675 static int ufs_qcom_probe(struct platform_device *pdev)
1678 struct device *dev = &pdev->dev;
1680 /* Perform generic probe */
1681 err = ufshcd_pltfrm_init(pdev, &ufs_hba_qcom_vops);
1683 dev_err(dev, "ufshcd_pltfrm_init() failed %d\n", err);
1689 * ufs_qcom_remove - set driver_data of the device to NULL
1690 * @pdev: pointer to platform device handle
1694 static int ufs_qcom_remove(struct platform_device *pdev)
1696 struct ufs_hba *hba = platform_get_drvdata(pdev);
1698 pm_runtime_get_sync(&(pdev)->dev);
1703 static const struct of_device_id ufs_qcom_of_match[] = {
1704 { .compatible = "qcom,ufshc"},
1707 MODULE_DEVICE_TABLE(of, ufs_qcom_of_match);
1709 static const struct dev_pm_ops ufs_qcom_pm_ops = {
1710 .suspend = ufshcd_pltfrm_suspend,
1711 .resume = ufshcd_pltfrm_resume,
1712 .runtime_suspend = ufshcd_pltfrm_runtime_suspend,
1713 .runtime_resume = ufshcd_pltfrm_runtime_resume,
1714 .runtime_idle = ufshcd_pltfrm_runtime_idle,
1717 static struct platform_driver ufs_qcom_pltform = {
1718 .probe = ufs_qcom_probe,
1719 .remove = ufs_qcom_remove,
1720 .shutdown = ufshcd_pltfrm_shutdown,
1722 .name = "ufshcd-qcom",
1723 .pm = &ufs_qcom_pm_ops,
1724 .of_match_table = of_match_ptr(ufs_qcom_of_match),
1727 module_platform_driver(ufs_qcom_pltform);
1729 MODULE_LICENSE("GPL v2");