2 * Copyright (c) 2013-2015, 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>
20 #include <linux/phy/phy-qcom-ufs.h>
22 #include "ufshcd-pltfrm.h"
27 static struct ufs_qcom_host *ufs_qcom_hosts[MAX_UFS_QCOM_HOSTS];
29 static void ufs_qcom_get_speed_mode(struct ufs_pa_layer_attr *p, char *result);
30 static int ufs_qcom_get_bus_vote(struct ufs_qcom_host *host,
31 const char *speed_mode);
32 static int ufs_qcom_set_bus_vote(struct ufs_qcom_host *host, int vote);
34 static int ufs_qcom_get_connected_tx_lanes(struct ufs_hba *hba, u32 *tx_lanes)
38 err = ufshcd_dme_get(hba,
39 UIC_ARG_MIB(PA_CONNECTEDTXDATALANES), tx_lanes);
41 dev_err(hba->dev, "%s: couldn't read PA_CONNECTEDTXDATALANES %d\n",
47 static int ufs_qcom_host_clk_get(struct device *dev,
48 const char *name, struct clk **clk_out)
53 clk = devm_clk_get(dev, name);
56 dev_err(dev, "%s: failed to get %s err %d",
65 static int ufs_qcom_host_clk_enable(struct device *dev,
66 const char *name, struct clk *clk)
70 err = clk_prepare_enable(clk);
72 dev_err(dev, "%s: %s enable failed %d\n", __func__, name, err);
77 static void ufs_qcom_disable_lane_clks(struct ufs_qcom_host *host)
79 if (!host->is_lane_clks_enabled)
82 clk_disable_unprepare(host->tx_l1_sync_clk);
83 clk_disable_unprepare(host->tx_l0_sync_clk);
84 clk_disable_unprepare(host->rx_l1_sync_clk);
85 clk_disable_unprepare(host->rx_l0_sync_clk);
87 host->is_lane_clks_enabled = false;
90 static int ufs_qcom_enable_lane_clks(struct ufs_qcom_host *host)
93 struct device *dev = host->hba->dev;
95 if (host->is_lane_clks_enabled)
98 err = ufs_qcom_host_clk_enable(dev, "rx_lane0_sync_clk",
99 host->rx_l0_sync_clk);
103 err = ufs_qcom_host_clk_enable(dev, "tx_lane0_sync_clk",
104 host->tx_l0_sync_clk);
108 err = ufs_qcom_host_clk_enable(dev, "rx_lane1_sync_clk",
109 host->rx_l1_sync_clk);
113 err = ufs_qcom_host_clk_enable(dev, "tx_lane1_sync_clk",
114 host->tx_l1_sync_clk);
118 host->is_lane_clks_enabled = true;
122 clk_disable_unprepare(host->rx_l1_sync_clk);
124 clk_disable_unprepare(host->tx_l0_sync_clk);
126 clk_disable_unprepare(host->rx_l0_sync_clk);
131 static int ufs_qcom_init_lane_clks(struct ufs_qcom_host *host)
134 struct device *dev = host->hba->dev;
136 err = ufs_qcom_host_clk_get(dev,
137 "rx_lane0_sync_clk", &host->rx_l0_sync_clk);
141 err = ufs_qcom_host_clk_get(dev,
142 "tx_lane0_sync_clk", &host->tx_l0_sync_clk);
146 err = ufs_qcom_host_clk_get(dev, "rx_lane1_sync_clk",
147 &host->rx_l1_sync_clk);
151 err = ufs_qcom_host_clk_get(dev, "tx_lane1_sync_clk",
152 &host->tx_l1_sync_clk);
157 static int ufs_qcom_link_startup_post_change(struct ufs_hba *hba)
159 struct ufs_qcom_host *host = ufshcd_get_variant(hba);
160 struct phy *phy = host->generic_phy;
164 err = ufs_qcom_get_connected_tx_lanes(hba, &tx_lanes);
168 err = ufs_qcom_phy_set_tx_lane_enable(phy, tx_lanes);
170 dev_err(hba->dev, "%s: ufs_qcom_phy_set_tx_lane_enable failed\n",
177 static int ufs_qcom_check_hibern8(struct ufs_hba *hba)
181 unsigned long timeout = jiffies + msecs_to_jiffies(HBRN8_POLL_TOUT_MS);
184 err = ufshcd_dme_get(hba,
185 UIC_ARG_MIB(MPHY_TX_FSM_STATE), &tx_fsm_val);
186 if (err || tx_fsm_val == TX_FSM_HIBERN8)
189 /* sleep for max. 200us */
190 usleep_range(100, 200);
191 } while (time_before(jiffies, timeout));
194 * we might have scheduled out for long during polling so
195 * check the state again.
197 if (time_after(jiffies, timeout))
198 err = ufshcd_dme_get(hba,
199 UIC_ARG_MIB(MPHY_TX_FSM_STATE), &tx_fsm_val);
202 dev_err(hba->dev, "%s: unable to get TX_FSM_STATE, err %d\n",
204 } else if (tx_fsm_val != TX_FSM_HIBERN8) {
206 dev_err(hba->dev, "%s: invalid TX_FSM_STATE = %d\n",
213 static int ufs_qcom_power_up_sequence(struct ufs_hba *hba)
215 struct ufs_qcom_host *host = ufshcd_get_variant(hba);
216 struct phy *phy = host->generic_phy;
218 bool is_rate_B = (UFS_QCOM_LIMIT_HS_RATE == PA_HS_MODE_B)
221 /* Assert PHY reset and apply PHY calibration values */
222 ufs_qcom_assert_reset(hba);
223 /* provide 1ms delay to let the reset pulse propagate */
224 usleep_range(1000, 1100);
226 ret = ufs_qcom_phy_calibrate_phy(phy, is_rate_B);
228 dev_err(hba->dev, "%s: ufs_qcom_phy_calibrate_phy() failed, ret = %d\n",
233 /* De-assert PHY reset and start serdes */
234 ufs_qcom_deassert_reset(hba);
237 * after reset deassertion, phy will need all ref clocks,
238 * voltage, current to settle down before starting serdes.
240 usleep_range(1000, 1100);
241 ret = ufs_qcom_phy_start_serdes(phy);
243 dev_err(hba->dev, "%s: ufs_qcom_phy_start_serdes() failed, ret = %d\n",
248 ret = ufs_qcom_phy_is_pcs_ready(phy);
250 dev_err(hba->dev, "%s: is_physical_coding_sublayer_ready() failed, ret = %d\n",
258 * The UTP controller has a number of internal clock gating cells (CGCs).
259 * Internal hardware sub-modules within the UTP controller control the CGCs.
260 * Hardware CGCs disable the clock to inactivate UTP sub-modules not involved
261 * in a specific operation, UTP controller CGCs are by default disabled and
262 * this function enables them (after every UFS link startup) to save some power
265 static void ufs_qcom_enable_hw_clk_gating(struct ufs_hba *hba)
268 ufshcd_readl(hba, REG_UFS_CFG2) | REG_UFS_CFG2_CGC_EN_ALL,
271 /* Ensure that HW clock gating is enabled before next operations */
275 static int ufs_qcom_hce_enable_notify(struct ufs_hba *hba, bool status)
277 struct ufs_qcom_host *host = ufshcd_get_variant(hba);
282 ufs_qcom_power_up_sequence(hba);
284 * The PHY PLL output is the source of tx/rx lane symbol
285 * clocks, hence, enable the lane clocks only after PHY
288 err = ufs_qcom_enable_lane_clks(host);
291 /* check if UFS PHY moved from DISABLED to HIBERN8 */
292 err = ufs_qcom_check_hibern8(hba);
293 ufs_qcom_enable_hw_clk_gating(hba);
297 dev_err(hba->dev, "%s: invalid status %d\n", __func__, status);
305 * Returns non-zero for success (which rate of core_clk) and 0
306 * in case of a failure
309 ufs_qcom_cfg_timers(struct ufs_hba *hba, u32 gear, u32 hs, u32 rate)
311 struct ufs_qcom_host *host = ufshcd_get_variant(hba);
312 struct ufs_clk_info *clki;
313 u32 core_clk_period_in_ns;
314 u32 tx_clk_cycles_per_us = 0;
315 unsigned long core_clk_rate = 0;
316 u32 core_clk_cycles_per_us = 0;
318 static u32 pwm_fr_table[][2] = {
325 static u32 hs_fr_table_rA[][2] = {
330 static u32 hs_fr_table_rB[][2] = {
336 * The Qunipro controller does not use following registers:
337 * SYS1CLK_1US_REG, TX_SYMBOL_CLK_1US_REG, CLK_NS_REG &
338 * UFS_REG_PA_LINK_STARTUP_TIMER
339 * But UTP controller uses SYS1CLK_1US_REG register for Interrupt
342 if (ufs_qcom_cap_qunipro(host) && !ufshcd_is_intr_aggr_allowed(hba))
346 dev_err(hba->dev, "%s: invalid gear = %d\n", __func__, gear);
350 list_for_each_entry(clki, &hba->clk_list_head, list) {
351 if (!strcmp(clki->name, "core_clk"))
352 core_clk_rate = clk_get_rate(clki->clk);
355 /* If frequency is smaller than 1MHz, set to 1MHz */
356 if (core_clk_rate < DEFAULT_CLK_RATE_HZ)
357 core_clk_rate = DEFAULT_CLK_RATE_HZ;
359 core_clk_cycles_per_us = core_clk_rate / USEC_PER_SEC;
360 ufshcd_writel(hba, core_clk_cycles_per_us, REG_UFS_SYS1CLK_1US);
362 core_clk_period_in_ns = NSEC_PER_SEC / core_clk_rate;
363 core_clk_period_in_ns <<= OFFSET_CLK_NS_REG;
364 core_clk_period_in_ns &= MASK_CLK_NS_REG;
369 if (rate == PA_HS_MODE_A) {
370 if (gear > ARRAY_SIZE(hs_fr_table_rA)) {
372 "%s: index %d exceeds table size %zu\n",
374 ARRAY_SIZE(hs_fr_table_rA));
377 tx_clk_cycles_per_us = hs_fr_table_rA[gear-1][1];
378 } else if (rate == PA_HS_MODE_B) {
379 if (gear > ARRAY_SIZE(hs_fr_table_rB)) {
381 "%s: index %d exceeds table size %zu\n",
383 ARRAY_SIZE(hs_fr_table_rB));
386 tx_clk_cycles_per_us = hs_fr_table_rB[gear-1][1];
388 dev_err(hba->dev, "%s: invalid rate = %d\n",
395 if (gear > ARRAY_SIZE(pwm_fr_table)) {
397 "%s: index %d exceeds table size %zu\n",
399 ARRAY_SIZE(pwm_fr_table));
402 tx_clk_cycles_per_us = pwm_fr_table[gear-1][1];
406 dev_err(hba->dev, "%s: invalid mode = %d\n", __func__, hs);
410 /* this register 2 fields shall be written at once */
411 ufshcd_writel(hba, core_clk_period_in_ns | tx_clk_cycles_per_us,
412 REG_UFS_TX_SYMBOL_CLK_NS_US);
418 return core_clk_rate;
421 static int ufs_qcom_link_startup_notify(struct ufs_hba *hba, bool status)
423 unsigned long core_clk_rate = 0;
424 u32 core_clk_cycles_per_100ms;
428 core_clk_rate = ufs_qcom_cfg_timers(hba, UFS_PWM_G1,
430 if (!core_clk_rate) {
431 dev_err(hba->dev, "%s: ufs_qcom_cfg_timers() failed\n",
435 core_clk_cycles_per_100ms =
436 (core_clk_rate / MSEC_PER_SEC) * 100;
437 ufshcd_writel(hba, core_clk_cycles_per_100ms,
438 REG_UFS_PA_LINK_STARTUP_TIMER);
441 ufs_qcom_link_startup_post_change(hba);
450 static int ufs_qcom_suspend(struct ufs_hba *hba, enum ufs_pm_op pm_op)
452 struct ufs_qcom_host *host = ufshcd_get_variant(hba);
453 struct phy *phy = host->generic_phy;
456 if (ufs_qcom_is_link_off(hba)) {
458 * Disable the tx/rx lane symbol clocks before PHY is
459 * powered down as the PLL source should be disabled
460 * after downstream clocks are disabled.
462 ufs_qcom_disable_lane_clks(host);
465 /* Assert PHY soft reset */
466 ufs_qcom_assert_reset(hba);
471 * If UniPro link is not active, PHY ref_clk, main PHY analog power
472 * rail and low noise analog power rail for PLL can be switched off.
474 if (!ufs_qcom_is_link_active(hba))
481 static int ufs_qcom_resume(struct ufs_hba *hba, enum ufs_pm_op pm_op)
483 struct ufs_qcom_host *host = ufshcd_get_variant(hba);
484 struct phy *phy = host->generic_phy;
487 err = phy_power_on(phy);
489 dev_err(hba->dev, "%s: failed enabling regs, err = %d\n",
494 hba->is_sys_suspended = false;
500 struct ufs_qcom_dev_params {
501 u32 pwm_rx_gear; /* pwm rx gear to work in */
502 u32 pwm_tx_gear; /* pwm tx gear to work in */
503 u32 hs_rx_gear; /* hs rx gear to work in */
504 u32 hs_tx_gear; /* hs tx gear to work in */
505 u32 rx_lanes; /* number of rx lanes */
506 u32 tx_lanes; /* number of tx lanes */
507 u32 rx_pwr_pwm; /* rx pwm working pwr */
508 u32 tx_pwr_pwm; /* tx pwm working pwr */
509 u32 rx_pwr_hs; /* rx hs working pwr */
510 u32 tx_pwr_hs; /* tx hs working pwr */
511 u32 hs_rate; /* rate A/B to work in HS */
512 u32 desired_working_mode;
515 static int ufs_qcom_get_pwr_dev_param(struct ufs_qcom_dev_params *qcom_param,
516 struct ufs_pa_layer_attr *dev_max,
517 struct ufs_pa_layer_attr *agreed_pwr)
521 bool is_dev_sup_hs = false;
522 bool is_qcom_max_hs = false;
524 if (dev_max->pwr_rx == FAST_MODE)
525 is_dev_sup_hs = true;
527 if (qcom_param->desired_working_mode == FAST) {
528 is_qcom_max_hs = true;
529 min_qcom_gear = min_t(u32, qcom_param->hs_rx_gear,
530 qcom_param->hs_tx_gear);
532 min_qcom_gear = min_t(u32, qcom_param->pwm_rx_gear,
533 qcom_param->pwm_tx_gear);
537 * device doesn't support HS but qcom_param->desired_working_mode is
538 * HS, thus device and qcom_param don't agree
540 if (!is_dev_sup_hs && is_qcom_max_hs) {
541 pr_err("%s: failed to agree on power mode (device doesn't support HS but requested power is HS)\n",
544 } else if (is_dev_sup_hs && is_qcom_max_hs) {
546 * since device supports HS, it supports FAST_MODE.
547 * since qcom_param->desired_working_mode is also HS
548 * then final decision (FAST/FASTAUTO) is done according
549 * to qcom_params as it is the restricting factor
551 agreed_pwr->pwr_rx = agreed_pwr->pwr_tx =
552 qcom_param->rx_pwr_hs;
555 * here qcom_param->desired_working_mode is PWM.
556 * it doesn't matter whether device supports HS or PWM,
557 * in both cases qcom_param->desired_working_mode will
560 agreed_pwr->pwr_rx = agreed_pwr->pwr_tx =
561 qcom_param->rx_pwr_pwm;
565 * we would like tx to work in the minimum number of lanes
566 * between device capability and vendor preferences.
567 * the same decision will be made for rx
569 agreed_pwr->lane_tx = min_t(u32, dev_max->lane_tx,
570 qcom_param->tx_lanes);
571 agreed_pwr->lane_rx = min_t(u32, dev_max->lane_rx,
572 qcom_param->rx_lanes);
574 /* device maximum gear is the minimum between device rx and tx gears */
575 min_dev_gear = min_t(u32, dev_max->gear_rx, dev_max->gear_tx);
578 * if both device capabilities and vendor pre-defined preferences are
579 * both HS or both PWM then set the minimum gear to be the chosen
581 * if one is PWM and one is HS then the one that is PWM get to decide
582 * what is the gear, as it is the one that also decided previously what
583 * pwr the device will be configured to.
585 if ((is_dev_sup_hs && is_qcom_max_hs) ||
586 (!is_dev_sup_hs && !is_qcom_max_hs))
587 agreed_pwr->gear_rx = agreed_pwr->gear_tx =
588 min_t(u32, min_dev_gear, min_qcom_gear);
589 else if (!is_dev_sup_hs)
590 agreed_pwr->gear_rx = agreed_pwr->gear_tx = min_dev_gear;
592 agreed_pwr->gear_rx = agreed_pwr->gear_tx = min_qcom_gear;
594 agreed_pwr->hs_rate = qcom_param->hs_rate;
598 static int ufs_qcom_update_bus_bw_vote(struct ufs_qcom_host *host)
602 char mode[BUS_VECTOR_NAME_LEN];
604 ufs_qcom_get_speed_mode(&host->dev_req_params, mode);
606 vote = ufs_qcom_get_bus_vote(host, mode);
608 err = ufs_qcom_set_bus_vote(host, vote);
613 dev_err(host->hba->dev, "%s: failed %d\n", __func__, err);
615 host->bus_vote.saved_vote = vote;
619 static int ufs_qcom_pwr_change_notify(struct ufs_hba *hba,
621 struct ufs_pa_layer_attr *dev_max_params,
622 struct ufs_pa_layer_attr *dev_req_params)
625 struct ufs_qcom_host *host = ufshcd_get_variant(hba);
626 struct phy *phy = host->generic_phy;
627 struct ufs_qcom_dev_params ufs_qcom_cap;
631 if (!dev_req_params) {
632 pr_err("%s: incoming dev_req_params is NULL\n", __func__);
639 ufs_qcom_cap.tx_lanes = UFS_QCOM_LIMIT_NUM_LANES_TX;
640 ufs_qcom_cap.rx_lanes = UFS_QCOM_LIMIT_NUM_LANES_RX;
641 ufs_qcom_cap.hs_rx_gear = UFS_QCOM_LIMIT_HSGEAR_RX;
642 ufs_qcom_cap.hs_tx_gear = UFS_QCOM_LIMIT_HSGEAR_TX;
643 ufs_qcom_cap.pwm_rx_gear = UFS_QCOM_LIMIT_PWMGEAR_RX;
644 ufs_qcom_cap.pwm_tx_gear = UFS_QCOM_LIMIT_PWMGEAR_TX;
645 ufs_qcom_cap.rx_pwr_pwm = UFS_QCOM_LIMIT_RX_PWR_PWM;
646 ufs_qcom_cap.tx_pwr_pwm = UFS_QCOM_LIMIT_TX_PWR_PWM;
647 ufs_qcom_cap.rx_pwr_hs = UFS_QCOM_LIMIT_RX_PWR_HS;
648 ufs_qcom_cap.tx_pwr_hs = UFS_QCOM_LIMIT_TX_PWR_HS;
649 ufs_qcom_cap.hs_rate = UFS_QCOM_LIMIT_HS_RATE;
650 ufs_qcom_cap.desired_working_mode =
651 UFS_QCOM_LIMIT_DESIRED_MODE;
653 ret = ufs_qcom_get_pwr_dev_param(&ufs_qcom_cap,
657 pr_err("%s: failed to determine capabilities\n",
664 if (!ufs_qcom_cfg_timers(hba, dev_req_params->gear_rx,
665 dev_req_params->pwr_rx,
666 dev_req_params->hs_rate)) {
667 dev_err(hba->dev, "%s: ufs_qcom_cfg_timers() failed\n",
670 * we return error code at the end of the routine,
671 * but continue to configure UFS_PHY_TX_LANE_ENABLE
672 * and bus voting as usual
677 val = ~(MAX_U32 << dev_req_params->lane_tx);
678 res = ufs_qcom_phy_set_tx_lane_enable(phy, val);
680 dev_err(hba->dev, "%s: ufs_qcom_phy_set_tx_lane_enable() failed res = %d\n",
685 /* cache the power mode parameters to use internally */
686 memcpy(&host->dev_req_params,
687 dev_req_params, sizeof(*dev_req_params));
688 ufs_qcom_update_bus_bw_vote(host);
698 static u32 ufs_qcom_get_ufs_hci_version(struct ufs_hba *hba)
700 struct ufs_qcom_host *host = ufshcd_get_variant(hba);
702 if (host->hw_ver.major == 0x1)
703 return UFSHCI_VERSION_11;
705 return UFSHCI_VERSION_20;
709 * ufs_qcom_advertise_quirks - advertise the known QCOM UFS controller quirks
710 * @hba: host controller instance
712 * QCOM UFS host controller might have some non standard behaviours (quirks)
713 * than what is specified by UFSHCI specification. Advertise all such
714 * quirks to standard UFS host controller driver so standard takes them into
717 static void ufs_qcom_advertise_quirks(struct ufs_hba *hba)
719 struct ufs_qcom_host *host = ufshcd_get_variant(hba);
721 if (host->hw_ver.major == 0x01) {
722 hba->quirks |= UFSHCD_QUIRK_DELAY_BEFORE_DME_CMDS
723 | UFSHCD_QUIRK_BROKEN_PA_RXHSUNTERMCAP
724 | UFSHCD_QUIRK_DME_PEER_ACCESS_AUTO_MODE;
726 if (host->hw_ver.minor == 0x0001 && host->hw_ver.step == 0x0001)
727 hba->quirks |= UFSHCD_QUIRK_BROKEN_INTR_AGGR;
730 if (host->hw_ver.major >= 0x2) {
731 hba->quirks |= UFSHCD_QUIRK_BROKEN_LCC;
732 hba->quirks |= UFSHCD_QUIRK_BROKEN_UFS_HCI_VERSION;
734 if (!ufs_qcom_cap_qunipro(host))
735 /* Legacy UniPro mode still need following quirks */
736 hba->quirks |= (UFSHCD_QUIRK_DELAY_BEFORE_DME_CMDS
737 | UFSHCD_QUIRK_DME_PEER_ACCESS_AUTO_MODE
738 | UFSHCD_QUIRK_BROKEN_PA_RXHSUNTERMCAP);
742 static void ufs_qcom_set_caps(struct ufs_hba *hba)
744 struct ufs_qcom_host *host = ufshcd_get_variant(hba);
746 if (host->hw_ver.major >= 0x2)
747 host->caps = UFS_QCOM_CAP_QUNIPRO;
750 static int ufs_qcom_get_bus_vote(struct ufs_qcom_host *host,
751 const char *speed_mode)
753 struct device *dev = host->hba->dev;
754 struct device_node *np = dev->of_node;
756 const char *key = "qcom,bus-vector-names";
763 if (host->bus_vote.is_max_bw_needed && !!strcmp(speed_mode, "MIN"))
764 err = of_property_match_string(np, key, "MAX");
766 err = of_property_match_string(np, key, speed_mode);
770 dev_err(dev, "%s: Invalid %s mode %d\n",
771 __func__, speed_mode, err);
775 static int ufs_qcom_set_bus_vote(struct ufs_qcom_host *host, int vote)
779 if (vote != host->bus_vote.curr_vote)
780 host->bus_vote.curr_vote = vote;
785 static void ufs_qcom_get_speed_mode(struct ufs_pa_layer_attr *p, char *result)
787 int gear = max_t(u32, p->gear_rx, p->gear_tx);
788 int lanes = max_t(u32, p->lane_rx, p->lane_tx);
791 /* default to PWM Gear 1, Lane 1 if power mode is not initialized */
798 if (!p->pwr_rx && !p->pwr_tx) {
800 snprintf(result, BUS_VECTOR_NAME_LEN, "MIN");
801 } else if (p->pwr_rx == FAST_MODE || p->pwr_rx == FASTAUTO_MODE ||
802 p->pwr_tx == FAST_MODE || p->pwr_tx == FASTAUTO_MODE) {
804 snprintf(result, BUS_VECTOR_NAME_LEN, "%s_R%s_G%d_L%d", "HS",
805 p->hs_rate == PA_HS_MODE_B ? "B" : "A", gear, lanes);
808 snprintf(result, BUS_VECTOR_NAME_LEN, "%s_G%d_L%d",
813 static int ufs_qcom_setup_clocks(struct ufs_hba *hba, bool on)
815 struct ufs_qcom_host *host = ufshcd_get_variant(hba);
820 * In case ufs_qcom_init() is not yet done, simply ignore.
821 * This ufs_qcom_setup_clocks() shall be called from
822 * ufs_qcom_init() after init is done.
828 err = ufs_qcom_phy_enable_iface_clk(host->generic_phy);
832 err = ufs_qcom_phy_enable_ref_clk(host->generic_phy);
834 dev_err(hba->dev, "%s enable phy ref clock failed, err=%d\n",
836 ufs_qcom_phy_disable_iface_clk(host->generic_phy);
839 /* enable the device ref clock */
840 ufs_qcom_phy_enable_dev_ref_clk(host->generic_phy);
841 vote = host->bus_vote.saved_vote;
842 if (vote == host->bus_vote.min_bw_vote)
843 ufs_qcom_update_bus_bw_vote(host);
845 /* M-PHY RMMI interface clocks can be turned off */
846 ufs_qcom_phy_disable_iface_clk(host->generic_phy);
847 if (!ufs_qcom_is_link_active(hba)) {
848 /* turn off UFS local PHY ref_clk */
849 ufs_qcom_phy_disable_ref_clk(host->generic_phy);
850 /* disable device ref_clk */
851 ufs_qcom_phy_disable_dev_ref_clk(host->generic_phy);
853 vote = host->bus_vote.min_bw_vote;
856 err = ufs_qcom_set_bus_vote(host, vote);
858 dev_err(hba->dev, "%s: set bus vote failed %d\n",
866 show_ufs_to_mem_max_bus_bw(struct device *dev, struct device_attribute *attr,
869 struct ufs_hba *hba = dev_get_drvdata(dev);
870 struct ufs_qcom_host *host = ufshcd_get_variant(hba);
872 return snprintf(buf, PAGE_SIZE, "%u\n",
873 host->bus_vote.is_max_bw_needed);
877 store_ufs_to_mem_max_bus_bw(struct device *dev, struct device_attribute *attr,
878 const char *buf, size_t count)
880 struct ufs_hba *hba = dev_get_drvdata(dev);
881 struct ufs_qcom_host *host = ufshcd_get_variant(hba);
884 if (!kstrtou32(buf, 0, &value)) {
885 host->bus_vote.is_max_bw_needed = !!value;
886 ufs_qcom_update_bus_bw_vote(host);
892 static int ufs_qcom_bus_register(struct ufs_qcom_host *host)
895 struct device *dev = host->hba->dev;
896 struct device_node *np = dev->of_node;
898 err = of_property_count_strings(np, "qcom,bus-vector-names");
900 dev_err(dev, "%s: qcom,bus-vector-names not specified correctly %d\n",
905 /* cache the vote index for minimum and maximum bandwidth */
906 host->bus_vote.min_bw_vote = ufs_qcom_get_bus_vote(host, "MIN");
907 host->bus_vote.max_bw_vote = ufs_qcom_get_bus_vote(host, "MAX");
909 host->bus_vote.max_bus_bw.show = show_ufs_to_mem_max_bus_bw;
910 host->bus_vote.max_bus_bw.store = store_ufs_to_mem_max_bus_bw;
911 sysfs_attr_init(&host->bus_vote.max_bus_bw.attr);
912 host->bus_vote.max_bus_bw.attr.name = "max_bus_bw";
913 host->bus_vote.max_bus_bw.attr.mode = S_IRUGO | S_IWUSR;
914 err = device_create_file(dev, &host->bus_vote.max_bus_bw);
919 #define ANDROID_BOOT_DEV_MAX 30
920 static char android_boot_dev[ANDROID_BOOT_DEV_MAX];
923 static int __init get_android_boot_dev(char *str)
925 strlcpy(android_boot_dev, str, ANDROID_BOOT_DEV_MAX);
928 __setup("androidboot.bootdevice=", get_android_boot_dev);
932 * ufs_qcom_init - bind phy with controller
933 * @hba: host controller instance
935 * Binds PHY with controller and powers up PHY enabling clocks
938 * Returns -EPROBE_DEFER if binding fails, returns negative error
939 * on phy power up failure and returns zero on success.
941 static int ufs_qcom_init(struct ufs_hba *hba)
944 struct device *dev = hba->dev;
945 struct ufs_qcom_host *host;
947 if (strlen(android_boot_dev) && strcmp(android_boot_dev, dev_name(dev)))
950 host = devm_kzalloc(dev, sizeof(*host), GFP_KERNEL);
953 dev_err(dev, "%s: no memory for qcom ufs host\n", __func__);
958 ufshcd_set_variant(hba, host);
960 host->generic_phy = devm_phy_get(dev, "ufsphy");
962 if (IS_ERR(host->generic_phy)) {
963 err = PTR_ERR(host->generic_phy);
964 dev_err(dev, "%s: PHY get failed %d\n", __func__, err);
968 err = ufs_qcom_bus_register(host);
972 ufs_qcom_get_controller_revision(hba, &host->hw_ver.major,
973 &host->hw_ver.minor, &host->hw_ver.step);
975 /* update phy revision information before calling phy_init() */
976 ufs_qcom_phy_save_controller_version(host->generic_phy,
977 host->hw_ver.major, host->hw_ver.minor, host->hw_ver.step);
979 phy_init(host->generic_phy);
980 err = phy_power_on(host->generic_phy);
982 goto out_unregister_bus;
984 err = ufs_qcom_init_lane_clks(host);
986 goto out_disable_phy;
988 ufs_qcom_set_caps(hba);
989 ufs_qcom_advertise_quirks(hba);
991 hba->caps |= UFSHCD_CAP_CLK_GATING | UFSHCD_CAP_CLK_SCALING;
992 hba->caps |= UFSHCD_CAP_AUTO_BKOPS_SUSPEND;
994 ufs_qcom_setup_clocks(hba, true);
996 if (hba->dev->id < MAX_UFS_QCOM_HOSTS)
997 ufs_qcom_hosts[hba->dev->id] = host;
1002 phy_power_off(host->generic_phy);
1004 phy_exit(host->generic_phy);
1006 devm_kfree(dev, host);
1007 ufshcd_set_variant(hba, NULL);
1012 static void ufs_qcom_exit(struct ufs_hba *hba)
1014 struct ufs_qcom_host *host = ufshcd_get_variant(hba);
1016 ufs_qcom_disable_lane_clks(host);
1017 phy_power_off(host->generic_phy);
1021 void ufs_qcom_clk_scale_notify(struct ufs_hba *hba)
1023 struct ufs_qcom_host *host = ufshcd_get_variant(hba);
1024 struct ufs_pa_layer_attr *dev_req_params = &host->dev_req_params;
1026 if (!dev_req_params)
1029 ufs_qcom_cfg_timers(hba, dev_req_params->gear_rx,
1030 dev_req_params->pwr_rx,
1031 dev_req_params->hs_rate);
1035 * struct ufs_hba_qcom_vops - UFS QCOM specific variant operations
1037 * The variant operations configure the necessary controller and PHY
1038 * handshake during initialization.
1040 static struct ufs_hba_variant_ops ufs_hba_qcom_vops = {
1042 .init = ufs_qcom_init,
1043 .exit = ufs_qcom_exit,
1044 .get_ufs_hci_version = ufs_qcom_get_ufs_hci_version,
1045 .clk_scale_notify = ufs_qcom_clk_scale_notify,
1046 .setup_clocks = ufs_qcom_setup_clocks,
1047 .hce_enable_notify = ufs_qcom_hce_enable_notify,
1048 .link_startup_notify = ufs_qcom_link_startup_notify,
1049 .pwr_change_notify = ufs_qcom_pwr_change_notify,
1050 .suspend = ufs_qcom_suspend,
1051 .resume = ufs_qcom_resume,
1055 * ufs_qcom_probe - probe routine of the driver
1056 * @pdev: pointer to Platform device handle
1058 * Return zero for success and non-zero for failure
1060 static int ufs_qcom_probe(struct platform_device *pdev)
1063 struct device *dev = &pdev->dev;
1065 /* Perform generic probe */
1066 err = ufshcd_pltfrm_init(pdev, &ufs_hba_qcom_vops);
1068 dev_err(dev, "ufshcd_pltfrm_init() failed %d\n", err);
1074 * ufs_qcom_remove - set driver_data of the device to NULL
1075 * @pdev: pointer to platform device handle
1079 static int ufs_qcom_remove(struct platform_device *pdev)
1081 struct ufs_hba *hba = platform_get_drvdata(pdev);
1083 pm_runtime_get_sync(&(pdev)->dev);
1088 static const struct of_device_id ufs_qcom_of_match[] = {
1089 { .compatible = "qcom,ufshc"},
1093 static const struct dev_pm_ops ufs_qcom_pm_ops = {
1094 .suspend = ufshcd_pltfrm_suspend,
1095 .resume = ufshcd_pltfrm_resume,
1096 .runtime_suspend = ufshcd_pltfrm_runtime_suspend,
1097 .runtime_resume = ufshcd_pltfrm_runtime_resume,
1098 .runtime_idle = ufshcd_pltfrm_runtime_idle,
1101 static struct platform_driver ufs_qcom_pltform = {
1102 .probe = ufs_qcom_probe,
1103 .remove = ufs_qcom_remove,
1104 .shutdown = ufshcd_pltfrm_shutdown,
1106 .name = "ufshcd-qcom",
1107 .pm = &ufs_qcom_pm_ops,
1108 .of_match_table = of_match_ptr(ufs_qcom_of_match),
1111 module_platform_driver(ufs_qcom_pltform);
1113 MODULE_LICENSE("GPL v2");