ufs-qcom.c 36.4 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-only
2
/*
3
 * Copyright (c) 2013-2016, Linux Foundation. All rights reserved.
4 5
 */

6
#include <linux/acpi.h>
7
#include <linux/time.h>
8 9 10
#include <linux/clk.h>
#include <linux/delay.h>
#include <linux/module.h>
11 12 13
#include <linux/of.h>
#include <linux/platform_device.h>
#include <linux/phy/phy.h>
14
#include <linux/gpio/consumer.h>
15
#include <linux/reset-controller.h>
16
#include <linux/devfreq.h>
17

18
#include <ufs/ufshcd.h>
19
#include "ufshcd-pltfrm.h"
20
#include <ufs/unipro.h>
21
#include "ufs-qcom.h"
22 23
#include <ufs/ufshci.h>
#include <ufs/ufs_quirks.h>
24

25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
enum {
	TSTBUS_UAWM,
	TSTBUS_UARM,
	TSTBUS_TXUC,
	TSTBUS_RXUC,
	TSTBUS_DFC,
	TSTBUS_TRLUT,
	TSTBUS_TMRLUT,
	TSTBUS_OCSC,
	TSTBUS_UTP_HCI,
	TSTBUS_COMBINED,
	TSTBUS_WRAPPER,
	TSTBUS_UNIPRO,
	TSTBUS_MAX,
};
40 41 42

static struct ufs_qcom_host *ufs_qcom_hosts[MAX_UFS_QCOM_HOSTS];

43
static void ufs_qcom_get_default_testbus_cfg(struct ufs_qcom_host *host);
44 45 46
static int ufs_qcom_set_dme_vs_core_clk_ctrl_clear_div(struct ufs_hba *hba,
						       u32 clk_cycles);

47 48 49 50 51
static struct ufs_qcom_host *rcdev_to_ufs_host(struct reset_controller_dev *rcd)
{
	return container_of(rcd, struct ufs_qcom_host, rcdev);
}

52
static int ufs_qcom_host_clk_get(struct device *dev,
53
		const char *name, struct clk **clk_out, bool optional)
54 55 56 57 58
{
	struct clk *clk;
	int err = 0;

	clk = devm_clk_get(dev, name);
59
	if (!IS_ERR(clk)) {
60
		*clk_out = clk;
61 62 63 64 65 66 67 68
		return 0;
	}

	err = PTR_ERR(clk);

	if (optional && err == -ENOENT) {
		*clk_out = NULL;
		return 0;
69 70
	}

71 72 73
	if (err != -EPROBE_DEFER)
		dev_err(dev, "failed to get %s err %d\n", name, err);

74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
	return err;
}

static int ufs_qcom_host_clk_enable(struct device *dev,
		const char *name, struct clk *clk)
{
	int err = 0;

	err = clk_prepare_enable(clk);
	if (err)
		dev_err(dev, "%s: %s enable failed %d\n", __func__, name, err);

	return err;
}

static void ufs_qcom_disable_lane_clks(struct ufs_qcom_host *host)
{
	if (!host->is_lane_clks_enabled)
		return;

94
	clk_disable_unprepare(host->tx_l1_sync_clk);
95
	clk_disable_unprepare(host->tx_l0_sync_clk);
96
	clk_disable_unprepare(host->rx_l1_sync_clk);
97 98 99 100 101 102 103
	clk_disable_unprepare(host->rx_l0_sync_clk);

	host->is_lane_clks_enabled = false;
}

static int ufs_qcom_enable_lane_clks(struct ufs_qcom_host *host)
{
104
	int err;
105 106 107 108 109 110 111 112
	struct device *dev = host->hba->dev;

	if (host->is_lane_clks_enabled)
		return 0;

	err = ufs_qcom_host_clk_enable(dev, "rx_lane0_sync_clk",
		host->rx_l0_sync_clk);
	if (err)
113
		return err;
114 115 116 117 118 119

	err = ufs_qcom_host_clk_enable(dev, "tx_lane0_sync_clk",
		host->tx_l0_sync_clk);
	if (err)
		goto disable_rx_l0;

120
	err = ufs_qcom_host_clk_enable(dev, "rx_lane1_sync_clk",
121
			host->rx_l1_sync_clk);
122 123
	if (err)
		goto disable_tx_l0;
124

125
	err = ufs_qcom_host_clk_enable(dev, "tx_lane1_sync_clk",
126
			host->tx_l1_sync_clk);
127 128
	if (err)
		goto disable_rx_l1;
129 130

	host->is_lane_clks_enabled = true;
131 132

	return 0;
133 134

disable_rx_l1:
135
	clk_disable_unprepare(host->rx_l1_sync_clk);
136 137 138 139
disable_tx_l0:
	clk_disable_unprepare(host->tx_l0_sync_clk);
disable_rx_l0:
	clk_disable_unprepare(host->rx_l0_sync_clk);
140

141 142 143 144 145 146 147 148
	return err;
}

static int ufs_qcom_init_lane_clks(struct ufs_qcom_host *host)
{
	int err = 0;
	struct device *dev = host->hba->dev;

149 150 151
	if (has_acpi_companion(dev))
		return 0;

152 153
	err = ufs_qcom_host_clk_get(dev, "rx_lane0_sync_clk",
					&host->rx_l0_sync_clk, false);
154
	if (err)
155
		return err;
156

157 158
	err = ufs_qcom_host_clk_get(dev, "tx_lane0_sync_clk",
					&host->tx_l0_sync_clk, false);
159
	if (err)
160
		return err;
161

162 163 164
	/* In case of single lane per direction, don't read lane1 clocks */
	if (host->hba->lanes_per_direction > 1) {
		err = ufs_qcom_host_clk_get(dev, "rx_lane1_sync_clk",
165
			&host->rx_l1_sync_clk, false);
166
		if (err)
167
			return err;
168

169
		err = ufs_qcom_host_clk_get(dev, "tx_lane1_sync_clk",
170
			&host->tx_l1_sync_clk, true);
171
	}
172 173

	return 0;
174 175 176 177 178 179 180 181 182 183
}

static int ufs_qcom_check_hibern8(struct ufs_hba *hba)
{
	int err;
	u32 tx_fsm_val = 0;
	unsigned long timeout = jiffies + msecs_to_jiffies(HBRN8_POLL_TOUT_MS);

	do {
		err = ufshcd_dme_get(hba,
184 185 186
				UIC_ARG_MIB_SEL(MPHY_TX_FSM_STATE,
					UIC_ARG_MPHY_TX_GEN_SEL_INDEX(0)),
				&tx_fsm_val);
187 188 189 190 191 192 193 194 195 196 197 198 199
		if (err || tx_fsm_val == TX_FSM_HIBERN8)
			break;

		/* sleep for max. 200us */
		usleep_range(100, 200);
	} while (time_before(jiffies, timeout));

	/*
	 * we might have scheduled out for long during polling so
	 * check the state again.
	 */
	if (time_after(jiffies, timeout))
		err = ufshcd_dme_get(hba,
200 201 202
				UIC_ARG_MIB_SEL(MPHY_TX_FSM_STATE,
					UIC_ARG_MPHY_TX_GEN_SEL_INDEX(0)),
				&tx_fsm_val);
203 204 205 206 207 208 209 210 211 212 213 214 215

	if (err) {
		dev_err(hba->dev, "%s: unable to get TX_FSM_STATE, err %d\n",
				__func__, err);
	} else if (tx_fsm_val != TX_FSM_HIBERN8) {
		err = tx_fsm_val;
		dev_err(hba->dev, "%s: invalid TX_FSM_STATE = %d\n",
				__func__, err);
	}

	return err;
}

216 217 218 219 220 221 222 223 224
static void ufs_qcom_select_unipro_mode(struct ufs_qcom_host *host)
{
	ufshcd_rmwl(host->hba, QUNIPRO_SEL,
		   ufs_qcom_cap_qunipro(host) ? QUNIPRO_SEL : 0,
		   REG_UFS_CFG1);
	/* make sure above configuration is applied before we return */
	mb();
}

225
/*
226 227 228 229 230 231
 * ufs_qcom_host_reset - reset host controller and PHY
 */
static int ufs_qcom_host_reset(struct ufs_hba *hba)
{
	int ret = 0;
	struct ufs_qcom_host *host = ufshcd_get_variant(hba);
232
	bool reenable_intr = false;
233 234 235

	if (!host->core_reset) {
		dev_warn(hba->dev, "%s: reset control not set\n", __func__);
236
		return 0;
237 238
	}

239 240 241 242
	reenable_intr = hba->is_irq_enabled;
	disable_irq(hba->irq);
	hba->is_irq_enabled = false;

243 244 245 246
	ret = reset_control_assert(host->core_reset);
	if (ret) {
		dev_err(hba->dev, "%s: core_reset assert failed, err = %d\n",
				 __func__, ret);
247
		return ret;
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
	}

	/*
	 * The hardware requirement for delay between assert/deassert
	 * is at least 3-4 sleep clock (32.7KHz) cycles, which comes to
	 * ~125us (4/32768). To be on the safe side add 200us delay.
	 */
	usleep_range(200, 210);

	ret = reset_control_deassert(host->core_reset);
	if (ret)
		dev_err(hba->dev, "%s: core_reset deassert failed, err = %d\n",
				 __func__, ret);

	usleep_range(1000, 1100);

264 265 266 267 268
	if (reenable_intr) {
		enable_irq(hba->irq);
		hba->is_irq_enabled = true;
	}

269
	return 0;
270 271
}

272 273
static int ufs_qcom_power_up_sequence(struct ufs_hba *hba)
{
274
	struct ufs_qcom_host *host = ufshcd_get_variant(hba);
275
	struct phy *phy = host->generic_phy;
276
	int ret;
277
	bool is_rate_B = UFS_QCOM_LIMIT_HS_RATE == PA_HS_MODE_B;
278

279 280 281 282 283 284
	/* Reset UFS Host Controller and PHY */
	ret = ufs_qcom_host_reset(hba);
	if (ret)
		dev_warn(hba->dev, "%s: host reset returned %d\n",
				  __func__, ret);

285 286 287
	if (is_rate_B)
		phy_set_mode(phy, PHY_MODE_UFS_HS_B);

288 289
	/* phy initialization - calibrate the phy */
	ret = phy_init(phy);
290
	if (ret) {
291
		dev_err(hba->dev, "%s: phy init failed, ret = %d\n",
292
			__func__, ret);
293
		return ret;
294 295
	}

296 297
	/* power on phy - start serdes and phy's power and clocks */
	ret = phy_power_on(phy);
298
	if (ret) {
299
		dev_err(hba->dev, "%s: phy power on failed, ret = %d\n",
300
			__func__, ret);
301
		goto out_disable_phy;
302 303
	}

304 305
	ufs_qcom_select_unipro_mode(host);

306 307 308 309
	return 0;

out_disable_phy:
	phy_exit(phy);
310

311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
	return ret;
}

/*
 * The UTP controller has a number of internal clock gating cells (CGCs).
 * Internal hardware sub-modules within the UTP controller control the CGCs.
 * Hardware CGCs disable the clock to inactivate UTP sub-modules not involved
 * in a specific operation, UTP controller CGCs are by default disabled and
 * this function enables them (after every UFS link startup) to save some power
 * leakage.
 */
static void ufs_qcom_enable_hw_clk_gating(struct ufs_hba *hba)
{
	ufshcd_writel(hba,
		ufshcd_readl(hba, REG_UFS_CFG2) | REG_UFS_CFG2_CGC_EN_ALL,
		REG_UFS_CFG2);

	/* Ensure that HW clock gating is enabled before next operations */
	mb();
}

332 333
static int ufs_qcom_hce_enable_notify(struct ufs_hba *hba,
				      enum ufs_notify_change_status status)
334
{
335
	struct ufs_qcom_host *host = ufshcd_get_variant(hba);
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
	int err = 0;

	switch (status) {
	case PRE_CHANGE:
		ufs_qcom_power_up_sequence(hba);
		/*
		 * The PHY PLL output is the source of tx/rx lane symbol
		 * clocks, hence, enable the lane clocks only after PHY
		 * is initialized.
		 */
		err = ufs_qcom_enable_lane_clks(host);
		break;
	case POST_CHANGE:
		/* check if UFS PHY moved from DISABLED to HIBERN8 */
		err = ufs_qcom_check_hibern8(hba);
		ufs_qcom_enable_hw_clk_gating(hba);
352
		ufs_qcom_ice_enable(host);
353 354 355 356 357 358 359 360 361
		break;
	default:
		dev_err(hba->dev, "%s: invalid status %d\n", __func__, status);
		err = -EINVAL;
		break;
	}
	return err;
}

362
/*
363
 * Returns zero for success and non-zero in case of a failure
364
 */
365 366
static int ufs_qcom_cfg_timers(struct ufs_hba *hba, u32 gear,
			       u32 hs, u32 rate, bool update_link_startup_timer)
367
{
368
	struct ufs_qcom_host *host = ufshcd_get_variant(hba);
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
	struct ufs_clk_info *clki;
	u32 core_clk_period_in_ns;
	u32 tx_clk_cycles_per_us = 0;
	unsigned long core_clk_rate = 0;
	u32 core_clk_cycles_per_us = 0;

	static u32 pwm_fr_table[][2] = {
		{UFS_PWM_G1, 0x1},
		{UFS_PWM_G2, 0x1},
		{UFS_PWM_G3, 0x1},
		{UFS_PWM_G4, 0x1},
	};

	static u32 hs_fr_table_rA[][2] = {
		{UFS_HS_G1, 0x1F},
		{UFS_HS_G2, 0x3e},
385
		{UFS_HS_G3, 0x7D},
386 387 388 389 390
	};

	static u32 hs_fr_table_rB[][2] = {
		{UFS_HS_G1, 0x24},
		{UFS_HS_G2, 0x49},
391
		{UFS_HS_G3, 0x92},
392 393
	};

394 395 396 397 398 399 400 401
	/*
	 * The Qunipro controller does not use following registers:
	 * SYS1CLK_1US_REG, TX_SYMBOL_CLK_1US_REG, CLK_NS_REG &
	 * UFS_REG_PA_LINK_STARTUP_TIMER
	 * But UTP controller uses SYS1CLK_1US_REG register for Interrupt
	 * Aggregation logic.
	*/
	if (ufs_qcom_cap_qunipro(host) && !ufshcd_is_intr_aggr_allowed(hba))
402
		return 0;
403

404 405
	if (gear == 0) {
		dev_err(hba->dev, "%s: invalid gear = %d\n", __func__, gear);
406
		return -EINVAL;
407 408 409 410 411 412 413 414 415 416 417 418
	}

	list_for_each_entry(clki, &hba->clk_list_head, list) {
		if (!strcmp(clki->name, "core_clk"))
			core_clk_rate = clk_get_rate(clki->clk);
	}

	/* If frequency is smaller than 1MHz, set to 1MHz */
	if (core_clk_rate < DEFAULT_CLK_RATE_HZ)
		core_clk_rate = DEFAULT_CLK_RATE_HZ;

	core_clk_cycles_per_us = core_clk_rate / USEC_PER_SEC;
419 420 421 422 423 424 425 426 427 428
	if (ufshcd_readl(hba, REG_UFS_SYS1CLK_1US) != core_clk_cycles_per_us) {
		ufshcd_writel(hba, core_clk_cycles_per_us, REG_UFS_SYS1CLK_1US);
		/*
		 * make sure above write gets applied before we return from
		 * this function.
		 */
		mb();
	}

	if (ufs_qcom_cap_qunipro(host))
429
		return 0;
430 431 432 433 434 435 436 437 438 439 440 441 442 443

	core_clk_period_in_ns = NSEC_PER_SEC / core_clk_rate;
	core_clk_period_in_ns <<= OFFSET_CLK_NS_REG;
	core_clk_period_in_ns &= MASK_CLK_NS_REG;

	switch (hs) {
	case FASTAUTO_MODE:
	case FAST_MODE:
		if (rate == PA_HS_MODE_A) {
			if (gear > ARRAY_SIZE(hs_fr_table_rA)) {
				dev_err(hba->dev,
					"%s: index %d exceeds table size %zu\n",
					__func__, gear,
					ARRAY_SIZE(hs_fr_table_rA));
444
				return -EINVAL;
445 446 447 448 449 450 451 452
			}
			tx_clk_cycles_per_us = hs_fr_table_rA[gear-1][1];
		} else if (rate == PA_HS_MODE_B) {
			if (gear > ARRAY_SIZE(hs_fr_table_rB)) {
				dev_err(hba->dev,
					"%s: index %d exceeds table size %zu\n",
					__func__, gear,
					ARRAY_SIZE(hs_fr_table_rB));
453
				return -EINVAL;
454 455 456 457 458
			}
			tx_clk_cycles_per_us = hs_fr_table_rB[gear-1][1];
		} else {
			dev_err(hba->dev, "%s: invalid rate = %d\n",
				__func__, rate);
459
			return -EINVAL;
460 461 462 463 464 465 466 467 468
		}
		break;
	case SLOWAUTO_MODE:
	case SLOW_MODE:
		if (gear > ARRAY_SIZE(pwm_fr_table)) {
			dev_err(hba->dev,
					"%s: index %d exceeds table size %zu\n",
					__func__, gear,
					ARRAY_SIZE(pwm_fr_table));
469
			return -EINVAL;
470 471 472 473 474 475
		}
		tx_clk_cycles_per_us = pwm_fr_table[gear-1][1];
		break;
	case UNCHANGED:
	default:
		dev_err(hba->dev, "%s: invalid mode = %d\n", __func__, hs);
476
		return -EINVAL;
477 478
	}

479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499
	if (ufshcd_readl(hba, REG_UFS_TX_SYMBOL_CLK_NS_US) !=
	    (core_clk_period_in_ns | tx_clk_cycles_per_us)) {
		/* this register 2 fields shall be written at once */
		ufshcd_writel(hba, core_clk_period_in_ns | tx_clk_cycles_per_us,
			      REG_UFS_TX_SYMBOL_CLK_NS_US);
		/*
		 * make sure above write gets applied before we return from
		 * this function.
		 */
		mb();
	}

	if (update_link_startup_timer) {
		ufshcd_writel(hba, ((core_clk_rate / MSEC_PER_SEC) * 100),
			      REG_UFS_PA_LINK_STARTUP_TIMER);
		/*
		 * make sure that this configuration is applied before
		 * we return
		 */
		mb();
	}
500

501
	return 0;
502 503
}

504 505
static int ufs_qcom_link_startup_notify(struct ufs_hba *hba,
					enum ufs_notify_change_status status)
506
{
507 508
	int err = 0;
	struct ufs_qcom_host *host = ufshcd_get_variant(hba);
509 510 511

	switch (status) {
	case PRE_CHANGE:
512 513
		if (ufs_qcom_cfg_timers(hba, UFS_PWM_G1, SLOWAUTO_MODE,
					0, true)) {
514 515
			dev_err(hba->dev, "%s: ufs_qcom_cfg_timers() failed\n",
				__func__);
516
			return -EINVAL;
517
		}
518 519 520 521 522 523 524 525 526

		if (ufs_qcom_cap_qunipro(host))
			/*
			 * set unipro core clock cycles to 150 & clear clock
			 * divider
			 */
			err = ufs_qcom_set_dme_vs_core_clk_ctrl_clear_div(hba,
									  150);

527 528 529 530 531 532 533 534
		/*
		 * Some UFS devices (and may be host) have issues if LCC is
		 * enabled. So we are setting PA_Local_TX_LCC_Enable to 0
		 * before link startup which will make sure that both host
		 * and device TX LCC are disabled once link startup is
		 * completed.
		 */
		if (ufshcd_get_local_unipro_ver(hba) != UFS_UNIPRO_VER_1_41)
535
			err = ufshcd_disable_host_tx_lcc(hba);
536

537 538 539 540 541
		break;
	default:
		break;
	}

542
	return err;
543 544
}

545 546 547 548 549 550 551 552 553 554 555
static void ufs_qcom_device_reset_ctrl(struct ufs_hba *hba, bool asserted)
{
	struct ufs_qcom_host *host = ufshcd_get_variant(hba);

	/* reset gpio is optional */
	if (!host->device_reset)
		return;

	gpiod_set_value_cansleep(host->device_reset, asserted);
}

556 557
static int ufs_qcom_suspend(struct ufs_hba *hba, enum ufs_pm_op pm_op,
	enum ufs_notify_change_status status)
558
{
559
	struct ufs_qcom_host *host = ufshcd_get_variant(hba);
560 561
	struct phy *phy = host->generic_phy;

562 563 564
	if (status == PRE_CHANGE)
		return 0;

565 566 567 568 569 570 571 572 573
	if (ufs_qcom_is_link_off(hba)) {
		/*
		 * Disable the tx/rx lane symbol clocks before PHY is
		 * powered down as the PLL source should be disabled
		 * after downstream clocks are disabled.
		 */
		ufs_qcom_disable_lane_clks(host);
		phy_power_off(phy);

574 575 576
		/* reset the connected UFS device during power down */
		ufs_qcom_device_reset_ctrl(hba, true);

577
	} else if (!ufs_qcom_is_link_active(hba)) {
578 579
		ufs_qcom_disable_lane_clks(host);
	}
580

581
	return 0;
582 583 584 585
}

static int ufs_qcom_resume(struct ufs_hba *hba, enum ufs_pm_op pm_op)
{
586
	struct ufs_qcom_host *host = ufshcd_get_variant(hba);
587 588 589
	struct phy *phy = host->generic_phy;
	int err;

590 591 592 593 594 595 596
	if (ufs_qcom_is_link_off(hba)) {
		err = phy_power_on(phy);
		if (err) {
			dev_err(hba->dev, "%s: failed PHY power on: %d\n",
				__func__, err);
			return err;
		}
597

598 599 600
		err = ufs_qcom_enable_lane_clks(host);
		if (err)
			return err;
601

602 603 604 605 606
	} else if (!ufs_qcom_is_link_active(hba)) {
		err = ufs_qcom_enable_lane_clks(host);
		if (err)
			return err;
	}
607

608
	return ufs_qcom_ice_resume(host);
609 610
}

611 612 613 614 615 616 617 618 619 620 621 622 623 624
static void ufs_qcom_dev_ref_clk_ctrl(struct ufs_qcom_host *host, bool enable)
{
	if (host->dev_ref_clk_ctrl_mmio &&
	    (enable ^ host->is_dev_ref_clk_enabled)) {
		u32 temp = readl_relaxed(host->dev_ref_clk_ctrl_mmio);

		if (enable)
			temp |= host->dev_ref_clk_en_mask;
		else
			temp &= ~host->dev_ref_clk_en_mask;

		/*
		 * If we are here to disable this clock it might be immediately
		 * after entering into hibern8 in which case we need to make
625
		 * sure that device ref_clk is active for specific time after
626 627
		 * hibern8 enter.
		 */
628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645
		if (!enable) {
			unsigned long gating_wait;

			gating_wait = host->hba->dev_info.clk_gating_wait_us;
			if (!gating_wait) {
				udelay(1);
			} else {
				/*
				 * bRefClkGatingWaitTime defines the minimum
				 * time for which the reference clock is
				 * required by device during transition from
				 * HS-MODE to LS-MODE or HIBERN8 state. Give it
				 * more delay to be on the safe side.
				 */
				gating_wait += 10;
				usleep_range(gating_wait, gating_wait + 10);
			}
		}
646 647 648

		writel_relaxed(temp, host->dev_ref_clk_ctrl_mmio);

649 650 651 652 653
		/*
		 * Make sure the write to ref_clk reaches the destination and
		 * not stored in a Write Buffer (WB).
		 */
		readl(host->dev_ref_clk_ctrl_mmio);
654 655 656 657 658 659 660 661 662 663 664 665 666

		/*
		 * If we call hibern8 exit after this, we need to make sure that
		 * device ref_clk is stable for at least 1us before the hibern8
		 * exit command.
		 */
		if (enable)
			udelay(1);

		host->is_dev_ref_clk_enabled = enable;
	}
}

667
static int ufs_qcom_pwr_change_notify(struct ufs_hba *hba,
668
				enum ufs_notify_change_status status,
669 670 671
				struct ufs_pa_layer_attr *dev_max_params,
				struct ufs_pa_layer_attr *dev_req_params)
{
672
	struct ufs_qcom_host *host = ufshcd_get_variant(hba);
673
	struct ufs_dev_params ufs_qcom_cap;
674 675
	int ret = 0;

676 677 678 679 680
	if (!dev_req_params) {
		pr_err("%s: incoming dev_req_params is NULL\n", __func__);
		return -EINVAL;
	}

681 682
	switch (status) {
	case PRE_CHANGE:
683
		ufshcd_init_pwr_dev_param(&ufs_qcom_cap);
684 685
		ufs_qcom_cap.hs_rate = UFS_QCOM_LIMIT_HS_RATE;

686 687 688 689 690 691 692 693 694 695 696 697 698 699
		if (host->hw_ver.major == 0x1) {
			/*
			 * HS-G3 operations may not reliably work on legacy QCOM
			 * UFS host controller hardware even though capability
			 * exchange during link startup phase may end up
			 * negotiating maximum supported gear as G3.
			 * Hence downgrade the maximum supported gear to HS-G2.
			 */
			if (ufs_qcom_cap.hs_tx_gear > UFS_HS_G2)
				ufs_qcom_cap.hs_tx_gear = UFS_HS_G2;
			if (ufs_qcom_cap.hs_rx_gear > UFS_HS_G2)
				ufs_qcom_cap.hs_rx_gear = UFS_HS_G2;
		}

700 701 702
		ret = ufshcd_get_pwr_dev_param(&ufs_qcom_cap,
					       dev_max_params,
					       dev_req_params);
703
		if (ret) {
704
			dev_err(hba->dev, "%s: failed to determine capabilities\n",
705
					__func__);
706
			return ret;
707 708
		}

709 710 711 712
		/* enable the device ref clock before changing to HS mode */
		if (!ufshcd_is_hs_mode(&hba->pwr_info) &&
			ufshcd_is_hs_mode(dev_req_params))
			ufs_qcom_dev_ref_clk_ctrl(host, true);
713 714

		if (host->hw_ver.major >= 0x4) {
715 716 717
			ufshcd_dme_configure_adapt(hba,
						dev_req_params->gear_tx,
						PA_INITIAL_ADAPT);
718
		}
719 720
		break;
	case POST_CHANGE:
721
		if (ufs_qcom_cfg_timers(hba, dev_req_params->gear_rx,
722
					dev_req_params->pwr_rx,
723
					dev_req_params->hs_rate, false)) {
724 725 726 727 728 729 730 731 732 733 734 735 736
			dev_err(hba->dev, "%s: ufs_qcom_cfg_timers() failed\n",
				__func__);
			/*
			 * we return error code at the end of the routine,
			 * but continue to configure UFS_PHY_TX_LANE_ENABLE
			 * and bus voting as usual
			 */
			ret = -EINVAL;
		}

		/* cache the power mode parameters to use internally */
		memcpy(&host->dev_req_params,
				dev_req_params, sizeof(*dev_req_params));
737 738 739 740 741

		/* disable the device ref clock if entered PWM mode */
		if (ufshcd_is_hs_mode(&hba->pwr_info) &&
			!ufshcd_is_hs_mode(dev_req_params))
			ufs_qcom_dev_ref_clk_ctrl(host, false);
742 743 744 745 746
		break;
	default:
		ret = -EINVAL;
		break;
	}
747

748 749 750
	return ret;
}

751 752 753 754 755 756 757 758
static int ufs_qcom_quirk_host_pa_saveconfigtime(struct ufs_hba *hba)
{
	int err;
	u32 pa_vs_config_reg1;

	err = ufshcd_dme_get(hba, UIC_ARG_MIB(PA_VS_CONFIG_REG1),
			     &pa_vs_config_reg1);
	if (err)
759
		return err;
760 761

	/* Allow extension of MSB bits of PA_SaveConfigTime attribute */
762
	return ufshcd_dme_set(hba, UIC_ARG_MIB(PA_VS_CONFIG_REG1),
763 764 765 766 767 768 769 770 771 772
			    (pa_vs_config_reg1 | (1 << 12)));
}

static int ufs_qcom_apply_dev_quirks(struct ufs_hba *hba)
{
	int err = 0;

	if (hba->dev_quirks & UFS_DEVICE_QUIRK_HOST_PA_SAVECONFIGTIME)
		err = ufs_qcom_quirk_host_pa_saveconfigtime(hba);

773 774 775
	if (hba->dev_info.wmanufacturerid == UFS_VENDOR_WDC)
		hba->dev_quirks |= UFS_DEVICE_QUIRK_HOST_PA_TACTIVATE;

776 777 778
	return err;
}

779 780
static u32 ufs_qcom_get_ufs_hci_version(struct ufs_hba *hba)
{
781
	struct ufs_qcom_host *host = ufshcd_get_variant(hba);
782 783

	if (host->hw_ver.major == 0x1)
784
		return ufshci_version(1, 1);
785
	else
786
		return ufshci_version(2, 0);
787 788
}

789 790 791 792 793 794 795 796 797 798 799
/**
 * ufs_qcom_advertise_quirks - advertise the known QCOM UFS controller quirks
 * @hba: host controller instance
 *
 * QCOM UFS host controller might have some non standard behaviours (quirks)
 * than what is specified by UFSHCI specification. Advertise all such
 * quirks to standard UFS host controller driver so standard takes them into
 * account.
 */
static void ufs_qcom_advertise_quirks(struct ufs_hba *hba)
{
800
	struct ufs_qcom_host *host = ufshcd_get_variant(hba);
801

802
	if (host->hw_ver.major == 0x01) {
803
		hba->quirks |= UFSHCD_QUIRK_DELAY_BEFORE_DME_CMDS
804 805
			    | UFSHCD_QUIRK_BROKEN_PA_RXHSUNTERMCAP
			    | UFSHCD_QUIRK_DME_PEER_ACCESS_AUTO_MODE;
806

807 808
		if (host->hw_ver.minor == 0x0001 && host->hw_ver.step == 0x0001)
			hba->quirks |= UFSHCD_QUIRK_BROKEN_INTR_AGGR;
809 810

		hba->quirks |= UFSHCD_QUIRK_BROKEN_LCC;
811 812
	}

813
	if (host->hw_ver.major == 0x2) {
814
		hba->quirks |= UFSHCD_QUIRK_BROKEN_UFS_HCI_VERSION;
815

816 817
		if (!ufs_qcom_cap_qunipro(host))
			/* Legacy UniPro mode still need following quirks */
818
			hba->quirks |= (UFSHCD_QUIRK_DELAY_BEFORE_DME_CMDS
819
				| UFSHCD_QUIRK_DME_PEER_ACCESS_AUTO_MODE
820
				| UFSHCD_QUIRK_BROKEN_PA_RXHSUNTERMCAP);
821 822 823 824 825
	}
}

static void ufs_qcom_set_caps(struct ufs_hba *hba)
{
826
	struct ufs_qcom_host *host = ufshcd_get_variant(hba);
827

828
	hba->caps |= UFSHCD_CAP_CLK_GATING | UFSHCD_CAP_HIBERN8_WITH_CLK_GATING;
829
	hba->caps |= UFSHCD_CAP_CLK_SCALING | UFSHCD_CAP_WB_WITH_CLK_SCALING;
830
	hba->caps |= UFSHCD_CAP_AUTO_BKOPS_SUSPEND;
831
	hba->caps |= UFSHCD_CAP_WB_EN;
832
	hba->caps |= UFSHCD_CAP_CRYPTO;
833
	hba->caps |= UFSHCD_CAP_AGGR_POWER_COLLAPSE;
834
	hba->caps |= UFSHCD_CAP_RPM_AUTOSUSPEND;
835

836 837 838
	if (host->hw_ver.major >= 0x2) {
		host->caps = UFS_QCOM_CAP_QUNIPRO |
			     UFS_QCOM_CAP_RETAIN_SEC_CFG_AFTER_PWR_COLLAPSE;
839 840 841
	}
}

842 843 844 845
/**
 * ufs_qcom_setup_clocks - enables/disable clocks
 * @hba: host controller instance
 * @on: If true, enable clocks else disable them.
846
 * @status: PRE_CHANGE or POST_CHANGE notify
847 848 849
 *
 * Returns 0 on success, non-zero on failure.
 */
850 851
static int ufs_qcom_setup_clocks(struct ufs_hba *hba, bool on,
				 enum ufs_notify_change_status status)
852
{
853
	struct ufs_qcom_host *host = ufshcd_get_variant(hba);
854 855 856 857 858 859 860 861 862

	/*
	 * In case ufs_qcom_init() is not yet done, simply ignore.
	 * This ufs_qcom_setup_clocks() shall be called from
	 * ufs_qcom_init() after init is done.
	 */
	if (!host)
		return 0;

863 864
	switch (status) {
	case PRE_CHANGE:
865
		if (!on) {
866 867 868 869
			if (!ufs_qcom_is_link_active(hba)) {
				/* disable device ref_clk */
				ufs_qcom_dev_ref_clk_ctrl(host, false);
			}
870
		}
871 872 873 874 875 876 877 878
		break;
	case POST_CHANGE:
		if (on) {
			/* enable the device ref clock for HS mode*/
			if (ufshcd_is_hs_mode(&hba->pwr_info))
				ufs_qcom_dev_ref_clk_ctrl(host, true);
		}
		break;
879 880
	}

881
	return 0;
882 883
}

884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914
static int
ufs_qcom_reset_assert(struct reset_controller_dev *rcdev, unsigned long id)
{
	struct ufs_qcom_host *host = rcdev_to_ufs_host(rcdev);

	ufs_qcom_assert_reset(host->hba);
	/* provide 1ms delay to let the reset pulse propagate. */
	usleep_range(1000, 1100);
	return 0;
}

static int
ufs_qcom_reset_deassert(struct reset_controller_dev *rcdev, unsigned long id)
{
	struct ufs_qcom_host *host = rcdev_to_ufs_host(rcdev);

	ufs_qcom_deassert_reset(host->hba);

	/*
	 * after reset deassertion, phy will need all ref clocks,
	 * voltage, current to settle down before starting serdes.
	 */
	usleep_range(1000, 1100);
	return 0;
}

static const struct reset_control_ops ufs_qcom_reset_ops = {
	.assert = ufs_qcom_reset_assert,
	.deassert = ufs_qcom_reset_deassert,
};

915 916 917 918 919 920 921 922 923 924 925 926 927 928
/**
 * ufs_qcom_init - bind phy with controller
 * @hba: host controller instance
 *
 * Binds PHY with controller and powers up PHY enabling clocks
 * and regulators.
 *
 * Returns -EPROBE_DEFER if binding fails, returns negative error
 * on phy power up failure and returns zero on success.
 */
static int ufs_qcom_init(struct ufs_hba *hba)
{
	int err;
	struct device *dev = hba->dev;
929
	struct platform_device *pdev = to_platform_device(dev);
930
	struct ufs_qcom_host *host;
931
	struct resource *res;
932
	struct ufs_clk_info *clki;
933 934 935 936

	host = devm_kzalloc(dev, sizeof(*host), GFP_KERNEL);
	if (!host) {
		dev_err(dev, "%s: no memory for qcom ufs host\n", __func__);
937
		return -ENOMEM;
938 939
	}

940
	/* Make a two way bind between the qcom host and the hba */
941
	host->hba = hba;
942
	ufshcd_set_variant(hba, host);
943

944 945
	/* Setup the optional reset control of HCI */
	host->core_reset = devm_reset_control_get_optional(hba->dev, "rst");
946
	if (IS_ERR(host->core_reset)) {
947 948 949
		err = dev_err_probe(dev, PTR_ERR(host->core_reset),
				    "Failed to get reset control\n");
		goto out_variant_clear;
950 951
	}

952 953 954 955 956 957
	/* Fire up the reset controller. Failure here is non-fatal. */
	host->rcdev.of_node = dev->of_node;
	host->rcdev.ops = &ufs_qcom_reset_ops;
	host->rcdev.owner = dev->driver->owner;
	host->rcdev.nr_resets = 1;
	err = devm_reset_controller_register(dev, &host->rcdev);
958
	if (err)
959 960
		dev_warn(dev, "Failed to register reset controller\n");

961 962 963 964
	if (!has_acpi_companion(dev)) {
		host->generic_phy = devm_phy_get(dev, "ufsphy");
		if (IS_ERR(host->generic_phy)) {
			err = dev_err_probe(dev, PTR_ERR(host->generic_phy), "Failed to get PHY\n");
965 966
			goto out_variant_clear;
		}
967 968
	}

969 970 971 972 973 974 975 976 977
	host->device_reset = devm_gpiod_get_optional(dev, "reset",
						     GPIOD_OUT_HIGH);
	if (IS_ERR(host->device_reset)) {
		err = PTR_ERR(host->device_reset);
		if (err != -EPROBE_DEFER)
			dev_err(dev, "failed to acquire reset gpio: %d\n", err);
		goto out_variant_clear;
	}

978 979 980
	ufs_qcom_get_controller_revision(hba, &host->hw_ver.major,
		&host->hw_ver.minor, &host->hw_ver.step);

981 982 983 984 985 986 987 988 989
	/*
	 * for newer controllers, device reference clock control bit has
	 * moved inside UFS controller register address space itself.
	 */
	if (host->hw_ver.major >= 0x02) {
		host->dev_ref_clk_ctrl_mmio = hba->mmio_base + REG_UFS_CFG1;
		host->dev_ref_clk_en_mask = BIT(26);
	} else {
		/* "dev_ref_clk_ctrl_mem" is optional resource */
990 991
		res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
						   "dev_ref_clk_ctrl_mem");
992 993 994
		if (res) {
			host->dev_ref_clk_ctrl_mmio =
					devm_ioremap_resource(dev, res);
995
			if (IS_ERR(host->dev_ref_clk_ctrl_mmio))
996 997 998 999 1000
				host->dev_ref_clk_ctrl_mmio = NULL;
			host->dev_ref_clk_en_mask = BIT(5);
		}
	}

1001 1002 1003 1004 1005
	list_for_each_entry(clki, &hba->clk_list_head, list) {
		if (!strcmp(clki->name, "core_clk_unipro"))
			clki->keep_link_active = true;
	}

1006 1007
	err = ufs_qcom_init_lane_clks(host);
	if (err)
1008
		goto out_variant_clear;
1009

1010
	ufs_qcom_set_caps(hba);
1011 1012
	ufs_qcom_advertise_quirks(hba);

1013 1014 1015 1016
	err = ufs_qcom_ice_init(host);
	if (err)
		goto out_variant_clear;

1017
	ufs_qcom_setup_clocks(hba, true, POST_CHANGE);
1018 1019 1020 1021

	if (hba->dev->id < MAX_UFS_QCOM_HOSTS)
		ufs_qcom_hosts[hba->dev->id] = host;

1022 1023
	ufs_qcom_get_default_testbus_cfg(host);
	err = ufs_qcom_testbus_config(host);
1024 1025
	if (err)
		/* Failure is non-fatal */
1026 1027 1028
		dev_warn(dev, "%s: failed to configure the testbus %d\n",
				__func__, err);

1029
	return 0;
1030

1031
out_variant_clear:
1032
	ufshcd_set_variant(hba, NULL);
1033

1034 1035 1036 1037 1038
	return err;
}

static void ufs_qcom_exit(struct ufs_hba *hba)
{
1039
	struct ufs_qcom_host *host = ufshcd_get_variant(hba);
1040 1041 1042

	ufs_qcom_disable_lane_clks(host);
	phy_power_off(host->generic_phy);
1043
	phy_exit(host->generic_phy);
1044 1045
}

1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058
static int ufs_qcom_set_dme_vs_core_clk_ctrl_clear_div(struct ufs_hba *hba,
						       u32 clk_cycles)
{
	int err;
	u32 core_clk_ctrl_reg;

	if (clk_cycles > DME_VS_CORE_CLK_CTRL_MAX_CORE_CLK_1US_CYCLES_MASK)
		return -EINVAL;

	err = ufshcd_dme_get(hba,
			    UIC_ARG_MIB(DME_VS_CORE_CLK_CTRL),
			    &core_clk_ctrl_reg);
	if (err)
1059
		return err;
1060 1061 1062 1063 1064 1065 1066

	core_clk_ctrl_reg &= ~DME_VS_CORE_CLK_CTRL_MAX_CORE_CLK_1US_CYCLES_MASK;
	core_clk_ctrl_reg |= clk_cycles;

	/* Clear CORE_CLK_DIV_EN */
	core_clk_ctrl_reg &= ~DME_VS_CORE_CLK_CTRL_CORE_CLK_DIV_EN_BIT;

1067
	return ufshcd_dme_set(hba,
1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126
			    UIC_ARG_MIB(DME_VS_CORE_CLK_CTRL),
			    core_clk_ctrl_reg);
}

static int ufs_qcom_clk_scale_up_pre_change(struct ufs_hba *hba)
{
	/* nothing to do as of now */
	return 0;
}

static int ufs_qcom_clk_scale_up_post_change(struct ufs_hba *hba)
{
	struct ufs_qcom_host *host = ufshcd_get_variant(hba);

	if (!ufs_qcom_cap_qunipro(host))
		return 0;

	/* set unipro core clock cycles to 150 and clear clock divider */
	return ufs_qcom_set_dme_vs_core_clk_ctrl_clear_div(hba, 150);
}

static int ufs_qcom_clk_scale_down_pre_change(struct ufs_hba *hba)
{
	struct ufs_qcom_host *host = ufshcd_get_variant(hba);
	int err;
	u32 core_clk_ctrl_reg;

	if (!ufs_qcom_cap_qunipro(host))
		return 0;

	err = ufshcd_dme_get(hba,
			    UIC_ARG_MIB(DME_VS_CORE_CLK_CTRL),
			    &core_clk_ctrl_reg);

	/* make sure CORE_CLK_DIV_EN is cleared */
	if (!err &&
	    (core_clk_ctrl_reg & DME_VS_CORE_CLK_CTRL_CORE_CLK_DIV_EN_BIT)) {
		core_clk_ctrl_reg &= ~DME_VS_CORE_CLK_CTRL_CORE_CLK_DIV_EN_BIT;
		err = ufshcd_dme_set(hba,
				    UIC_ARG_MIB(DME_VS_CORE_CLK_CTRL),
				    core_clk_ctrl_reg);
	}

	return err;
}

static int ufs_qcom_clk_scale_down_post_change(struct ufs_hba *hba)
{
	struct ufs_qcom_host *host = ufshcd_get_variant(hba);

	if (!ufs_qcom_cap_qunipro(host))
		return 0;

	/* set unipro core clock cycles to 75 and clear clock divider */
	return ufs_qcom_set_dme_vs_core_clk_ctrl_clear_div(hba, 75);
}

static int ufs_qcom_clk_scale_notify(struct ufs_hba *hba,
		bool scale_up, enum ufs_notify_change_status status)
1127
{
1128
	struct ufs_qcom_host *host = ufshcd_get_variant(hba);
1129
	struct ufs_pa_layer_attr *dev_req_params = &host->dev_req_params;
1130
	int err = 0;
1131

1132
	if (status == PRE_CHANGE) {
1133 1134 1135
		err = ufshcd_uic_hibern8_enter(hba);
		if (err)
			return err;
1136 1137 1138 1139
		if (scale_up)
			err = ufs_qcom_clk_scale_up_pre_change(hba);
		else
			err = ufs_qcom_clk_scale_down_pre_change(hba);
1140 1141 1142
		if (err)
			ufshcd_uic_hibern8_exit(hba);

1143 1144 1145 1146 1147 1148
	} else {
		if (scale_up)
			err = ufs_qcom_clk_scale_up_post_change(hba);
		else
			err = ufs_qcom_clk_scale_down_post_change(hba);

1149 1150 1151

		if (err || !dev_req_params) {
			ufshcd_uic_hibern8_exit(hba);
1152
			return err;
1153
		}
1154 1155 1156 1157 1158 1159

		ufs_qcom_cfg_timers(hba,
				    dev_req_params->gear_rx,
				    dev_req_params->pwr_rx,
				    dev_req_params->hs_rate,
				    false);
1160
		ufshcd_uic_hibern8_exit(hba);
1161 1162
	}

1163
	return 0;
1164 1165
}

1166 1167
static void ufs_qcom_enable_test_bus(struct ufs_qcom_host *host)
{
1168 1169 1170
	ufshcd_rmwl(host->hba, UFS_REG_TEST_BUS_EN,
			UFS_REG_TEST_BUS_EN, REG_UFS_CFG1);
	ufshcd_rmwl(host->hba, TEST_BUS_EN, TEST_BUS_EN, REG_UFS_CFG1);
1171 1172
}

1173 1174 1175
static void ufs_qcom_get_default_testbus_cfg(struct ufs_qcom_host *host)
{
	/* provide a legal default configuration */
1176 1177
	host->testbus.select_major = TSTBUS_UNIPRO;
	host->testbus.select_minor = 37;
1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199
}

static bool ufs_qcom_testbus_cfg_is_ok(struct ufs_qcom_host *host)
{
	if (host->testbus.select_major >= TSTBUS_MAX) {
		dev_err(host->hba->dev,
			"%s: UFS_CFG1[TEST_BUS_SEL} may not equal 0x%05X\n",
			__func__, host->testbus.select_major);
		return false;
	}

	return true;
}

int ufs_qcom_testbus_config(struct ufs_qcom_host *host)
{
	int reg;
	int offset;
	u32 mask = TEST_BUS_SUB_SEL_MASK;

	if (!host)
		return -EINVAL;
1200

1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250
	if (!ufs_qcom_testbus_cfg_is_ok(host))
		return -EPERM;

	switch (host->testbus.select_major) {
	case TSTBUS_UAWM:
		reg = UFS_TEST_BUS_CTRL_0;
		offset = 24;
		break;
	case TSTBUS_UARM:
		reg = UFS_TEST_BUS_CTRL_0;
		offset = 16;
		break;
	case TSTBUS_TXUC:
		reg = UFS_TEST_BUS_CTRL_0;
		offset = 8;
		break;
	case TSTBUS_RXUC:
		reg = UFS_TEST_BUS_CTRL_0;
		offset = 0;
		break;
	case TSTBUS_DFC:
		reg = UFS_TEST_BUS_CTRL_1;
		offset = 24;
		break;
	case TSTBUS_TRLUT:
		reg = UFS_TEST_BUS_CTRL_1;
		offset = 16;
		break;
	case TSTBUS_TMRLUT:
		reg = UFS_TEST_BUS_CTRL_1;
		offset = 8;
		break;
	case TSTBUS_OCSC:
		reg = UFS_TEST_BUS_CTRL_1;
		offset = 0;
		break;
	case TSTBUS_WRAPPER:
		reg = UFS_TEST_BUS_CTRL_2;
		offset = 16;
		break;
	case TSTBUS_COMBINED:
		reg = UFS_TEST_BUS_CTRL_2;
		offset = 8;
		break;
	case TSTBUS_UTP_HCI:
		reg = UFS_TEST_BUS_CTRL_2;
		offset = 0;
		break;
	case TSTBUS_UNIPRO:
		reg = UFS_UNIPRO_CFG;
1251 1252
		offset = 20;
		mask = 0xFFF;
1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266
		break;
	/*
	 * No need for a default case, since
	 * ufs_qcom_testbus_cfg_is_ok() checks that the configuration
	 * is legal
	 */
	}
	mask <<= offset;
	ufshcd_rmwl(host->hba, TEST_BUS_SEL,
		    (u32)host->testbus.select_major << 19,
		    REG_UFS_CFG1);
	ufshcd_rmwl(host->hba, mask,
		    (u32)host->testbus.select_minor << offset,
		    reg);
1267
	ufs_qcom_enable_test_bus(host);
1268 1269 1270 1271 1272
	/*
	 * Make sure the test bus configuration is
	 * committed before returning.
	 */
	mb();
1273 1274

	return 0;
1275 1276
}

1277 1278
static void ufs_qcom_dump_dbg_regs(struct ufs_hba *hba)
{
1279 1280 1281 1282 1283
	u32 reg;
	struct ufs_qcom_host *host;

	host = ufshcd_get_variant(hba);

1284 1285
	ufshcd_dump_regs(hba, REG_UFS_SYS1CLK_1US, 16 * 4,
			 "HCI Vendor Specific Registers ");
1286

1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325
	reg = ufs_qcom_get_debug_reg_offset(host, UFS_UFS_DBG_RD_REG_OCSC);
	ufshcd_dump_regs(hba, reg, 44 * 4, "UFS_UFS_DBG_RD_REG_OCSC ");

	reg = ufshcd_readl(hba, REG_UFS_CFG1);
	reg |= UTP_DBG_RAMS_EN;
	ufshcd_writel(hba, reg, REG_UFS_CFG1);

	reg = ufs_qcom_get_debug_reg_offset(host, UFS_UFS_DBG_RD_EDTL_RAM);
	ufshcd_dump_regs(hba, reg, 32 * 4, "UFS_UFS_DBG_RD_EDTL_RAM ");

	reg = ufs_qcom_get_debug_reg_offset(host, UFS_UFS_DBG_RD_DESC_RAM);
	ufshcd_dump_regs(hba, reg, 128 * 4, "UFS_UFS_DBG_RD_DESC_RAM ");

	reg = ufs_qcom_get_debug_reg_offset(host, UFS_UFS_DBG_RD_PRDT_RAM);
	ufshcd_dump_regs(hba, reg, 64 * 4, "UFS_UFS_DBG_RD_PRDT_RAM ");

	/* clear bit 17 - UTP_DBG_RAMS_EN */
	ufshcd_rmwl(hba, UTP_DBG_RAMS_EN, 0, REG_UFS_CFG1);

	reg = ufs_qcom_get_debug_reg_offset(host, UFS_DBG_RD_REG_UAWM);
	ufshcd_dump_regs(hba, reg, 4 * 4, "UFS_DBG_RD_REG_UAWM ");

	reg = ufs_qcom_get_debug_reg_offset(host, UFS_DBG_RD_REG_UARM);
	ufshcd_dump_regs(hba, reg, 4 * 4, "UFS_DBG_RD_REG_UARM ");

	reg = ufs_qcom_get_debug_reg_offset(host, UFS_DBG_RD_REG_TXUC);
	ufshcd_dump_regs(hba, reg, 48 * 4, "UFS_DBG_RD_REG_TXUC ");

	reg = ufs_qcom_get_debug_reg_offset(host, UFS_DBG_RD_REG_RXUC);
	ufshcd_dump_regs(hba, reg, 27 * 4, "UFS_DBG_RD_REG_RXUC ");

	reg = ufs_qcom_get_debug_reg_offset(host, UFS_DBG_RD_REG_DFC);
	ufshcd_dump_regs(hba, reg, 19 * 4, "UFS_DBG_RD_REG_DFC ");

	reg = ufs_qcom_get_debug_reg_offset(host, UFS_DBG_RD_REG_TRLUT);
	ufshcd_dump_regs(hba, reg, 34 * 4, "UFS_DBG_RD_REG_TRLUT ");

	reg = ufs_qcom_get_debug_reg_offset(host, UFS_DBG_RD_REG_TMRLUT);
	ufshcd_dump_regs(hba, reg, 9 * 4, "UFS_DBG_RD_REG_TMRLUT ");
1326
}
1327

1328 1329 1330 1331 1332 1333
/**
 * ufs_qcom_device_reset() - toggle the (optional) device reset line
 * @hba: per-adapter instance
 *
 * Toggles the (optional) reset line to reset the attached device.
 */
1334
static int ufs_qcom_device_reset(struct ufs_hba *hba)
1335 1336 1337 1338 1339
{
	struct ufs_qcom_host *host = ufshcd_get_variant(hba);

	/* reset gpio is optional */
	if (!host->device_reset)
1340
		return -EOPNOTSUPP;
1341 1342 1343 1344 1345

	/*
	 * The UFS device shall detect reset pulses of 1us, sleep for 10us to
	 * be on the safe side.
	 */
1346
	ufs_qcom_device_reset_ctrl(hba, true);
1347 1348
	usleep_range(10, 15);

1349
	ufs_qcom_device_reset_ctrl(hba, false);
1350
	usleep_range(10, 15);
1351 1352

	return 0;
1353 1354
}

1355 1356
#if IS_ENABLED(CONFIG_DEVFREQ_GOV_SIMPLE_ONDEMAND)
static void ufs_qcom_config_scaling_param(struct ufs_hba *hba,
1357 1358
					struct devfreq_dev_profile *p,
					struct devfreq_simple_ondemand_data *d)
1359 1360 1361 1362 1363 1364 1365
{
	p->polling_ms = 60;
	d->upthreshold = 70;
	d->downdifferential = 5;
}
#else
static void ufs_qcom_config_scaling_param(struct ufs_hba *hba,
1366 1367
		struct devfreq_dev_profile *p,
		struct devfreq_simple_ondemand_data *data)
1368 1369 1370 1371
{
}
#endif

1372
/*
1373 1374 1375 1376 1377
 * struct ufs_hba_qcom_vops - UFS QCOM specific variant operations
 *
 * The variant operations configure the necessary controller and PHY
 * handshake during initialization.
 */
1378
static const struct ufs_hba_variant_ops ufs_hba_qcom_vops = {
1379 1380 1381
	.name                   = "qcom",
	.init                   = ufs_qcom_init,
	.exit                   = ufs_qcom_exit,
1382
	.get_ufs_hci_version	= ufs_qcom_get_ufs_hci_version,
1383 1384 1385 1386 1387
	.clk_scale_notify	= ufs_qcom_clk_scale_notify,
	.setup_clocks           = ufs_qcom_setup_clocks,
	.hce_enable_notify      = ufs_qcom_hce_enable_notify,
	.link_startup_notify    = ufs_qcom_link_startup_notify,
	.pwr_change_notify	= ufs_qcom_pwr_change_notify,
1388
	.apply_dev_quirks	= ufs_qcom_apply_dev_quirks,
1389 1390
	.suspend		= ufs_qcom_suspend,
	.resume			= ufs_qcom_resume,
1391
	.dbg_register_dump	= ufs_qcom_dump_dbg_regs,
1392
	.device_reset		= ufs_qcom_device_reset,
1393
	.config_scaling_param = ufs_qcom_config_scaling_param,
1394
	.program_key		= ufs_qcom_ice_program_key,
1395
};
1396

1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419
/**
 * ufs_qcom_probe - probe routine of the driver
 * @pdev: pointer to Platform device handle
 *
 * Return zero for success and non-zero for failure
 */
static int ufs_qcom_probe(struct platform_device *pdev)
{
	int err;
	struct device *dev = &pdev->dev;

	/* Perform generic probe */
	err = ufshcd_pltfrm_init(pdev, &ufs_hba_qcom_vops);
	if (err)
		dev_err(dev, "ufshcd_pltfrm_init() failed %d\n", err);

	return err;
}

/**
 * ufs_qcom_remove - set driver_data of the device to NULL
 * @pdev: pointer to platform device handle
 *
1420
 * Always returns 0
1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434
 */
static int ufs_qcom_remove(struct platform_device *pdev)
{
	struct ufs_hba *hba =  platform_get_drvdata(pdev);

	pm_runtime_get_sync(&(pdev)->dev);
	ufshcd_remove(hba);
	return 0;
}

static const struct of_device_id ufs_qcom_of_match[] = {
	{ .compatible = "qcom,ufshc"},
	{},
};
1435
MODULE_DEVICE_TABLE(of, ufs_qcom_of_match);
1436

1437 1438 1439 1440 1441 1442 1443 1444
#ifdef CONFIG_ACPI
static const struct acpi_device_id ufs_qcom_acpi_match[] = {
	{ "QCOM24A5" },
	{ },
};
MODULE_DEVICE_TABLE(acpi, ufs_qcom_acpi_match);
#endif

1445
static const struct dev_pm_ops ufs_qcom_pm_ops = {
1446 1447
	SET_SYSTEM_SLEEP_PM_OPS(ufshcd_system_suspend, ufshcd_system_resume)
	SET_RUNTIME_PM_OPS(ufshcd_runtime_suspend, ufshcd_runtime_resume, NULL)
1448 1449
	.prepare	 = ufshcd_suspend_prepare,
	.complete	 = ufshcd_resume_complete,
1450 1451 1452 1453 1454 1455 1456 1457 1458 1459
};

static struct platform_driver ufs_qcom_pltform = {
	.probe	= ufs_qcom_probe,
	.remove	= ufs_qcom_remove,
	.shutdown = ufshcd_pltfrm_shutdown,
	.driver	= {
		.name	= "ufshcd-qcom",
		.pm	= &ufs_qcom_pm_ops,
		.of_match_table = of_match_ptr(ufs_qcom_of_match),
1460
		.acpi_match_table = ACPI_PTR(ufs_qcom_acpi_match),
1461 1462 1463 1464
	},
};
module_platform_driver(ufs_qcom_pltform);

1465
MODULE_LICENSE("GPL v2");