blob: 3c4f602eecd2b601985b62124c33d84617ac7425 [file] [log] [blame]
Yaniv Gardi81c0fc52015-01-15 16:32:37 +02001/*
Yaniv Gardi54b879b2016-03-10 17:37:05 +02002 * Copyright (c) 2013-2016, Linux Foundation. All rights reserved.
Yaniv Gardi81c0fc52015-01-15 16:32:37 +02003 *
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.
7 *
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.
12 *
13 */
14
15#include <linux/time.h>
16#include <linux/of.h>
17#include <linux/platform_device.h>
18#include <linux/phy/phy.h>
Yaniv Gardi81c0fc52015-01-15 16:32:37 +020019#include <linux/phy/phy-qcom-ufs.h>
Yaniv Gardi4b9ad0b2016-03-10 17:37:19 +020020
Yaniv Gardi81c0fc52015-01-15 16:32:37 +020021#include "ufshcd.h"
Yaniv Gardi47555a52015-10-28 13:15:49 +020022#include "ufshcd-pltfrm.h"
Yaniv Gardi81c0fc52015-01-15 16:32:37 +020023#include "unipro.h"
24#include "ufs-qcom.h"
25#include "ufshci.h"
Yaniv Gardi6e3fd442015-10-28 13:15:50 +020026#define UFS_QCOM_DEFAULT_DBG_PRINT_EN \
27 (UFS_QCOM_DBG_PRINT_REGS_EN | UFS_QCOM_DBG_PRINT_TEST_BUS_EN)
28
29enum {
30 TSTBUS_UAWM,
31 TSTBUS_UARM,
32 TSTBUS_TXUC,
33 TSTBUS_RXUC,
34 TSTBUS_DFC,
35 TSTBUS_TRLUT,
36 TSTBUS_TMRLUT,
37 TSTBUS_OCSC,
38 TSTBUS_UTP_HCI,
39 TSTBUS_COMBINED,
40 TSTBUS_WRAPPER,
41 TSTBUS_UNIPRO,
42 TSTBUS_MAX,
43};
Yaniv Gardi81c0fc52015-01-15 16:32:37 +020044
45static struct ufs_qcom_host *ufs_qcom_hosts[MAX_UFS_QCOM_HOSTS];
46
Yaniv Gardi81c0fc52015-01-15 16:32:37 +020047static int ufs_qcom_set_bus_vote(struct ufs_qcom_host *host, int vote);
Yaniv Gardi6e3fd442015-10-28 13:15:50 +020048static void ufs_qcom_get_default_testbus_cfg(struct ufs_qcom_host *host);
Yaniv Gardif06fcc72015-10-28 13:15:51 +020049static int ufs_qcom_set_dme_vs_core_clk_ctrl_clear_div(struct ufs_hba *hba,
50 u32 clk_cycles);
51
Yaniv Gardi6e3fd442015-10-28 13:15:50 +020052static void ufs_qcom_dump_regs(struct ufs_hba *hba, int offset, int len,
53 char *prefix)
54{
55 print_hex_dump(KERN_ERR, prefix,
56 len > 4 ? DUMP_PREFIX_OFFSET : DUMP_PREFIX_NONE,
57 16, 4, (void __force *)hba->mmio_base + offset,
58 len * 4, false);
59}
Yaniv Gardi81c0fc52015-01-15 16:32:37 +020060
Yaniv Gardieba5ed32016-03-10 17:37:21 +020061static void ufs_qcom_dump_regs_wrapper(struct ufs_hba *hba, int offset, int len,
62 char *prefix, void *priv)
63{
64 ufs_qcom_dump_regs(hba, offset, len, prefix);
65}
66
Yaniv Gardi81c0fc52015-01-15 16:32:37 +020067static int ufs_qcom_get_connected_tx_lanes(struct ufs_hba *hba, u32 *tx_lanes)
68{
69 int err = 0;
70
71 err = ufshcd_dme_get(hba,
72 UIC_ARG_MIB(PA_CONNECTEDTXDATALANES), tx_lanes);
73 if (err)
74 dev_err(hba->dev, "%s: couldn't read PA_CONNECTEDTXDATALANES %d\n",
75 __func__, err);
76
77 return err;
78}
79
80static int ufs_qcom_host_clk_get(struct device *dev,
81 const char *name, struct clk **clk_out)
82{
83 struct clk *clk;
84 int err = 0;
85
86 clk = devm_clk_get(dev, name);
87 if (IS_ERR(clk)) {
88 err = PTR_ERR(clk);
89 dev_err(dev, "%s: failed to get %s err %d",
90 __func__, name, err);
91 } else {
92 *clk_out = clk;
93 }
94
95 return err;
96}
97
98static int ufs_qcom_host_clk_enable(struct device *dev,
99 const char *name, struct clk *clk)
100{
101 int err = 0;
102
103 err = clk_prepare_enable(clk);
104 if (err)
105 dev_err(dev, "%s: %s enable failed %d\n", __func__, name, err);
106
107 return err;
108}
109
110static void ufs_qcom_disable_lane_clks(struct ufs_qcom_host *host)
111{
112 if (!host->is_lane_clks_enabled)
113 return;
114
Yaniv Gardi4b9ad0b2016-03-10 17:37:19 +0200115 if (host->hba->lanes_per_direction > 1)
116 clk_disable_unprepare(host->tx_l1_sync_clk);
Yaniv Gardi81c0fc52015-01-15 16:32:37 +0200117 clk_disable_unprepare(host->tx_l0_sync_clk);
Yaniv Gardi4b9ad0b2016-03-10 17:37:19 +0200118 if (host->hba->lanes_per_direction > 1)
119 clk_disable_unprepare(host->rx_l1_sync_clk);
Yaniv Gardi81c0fc52015-01-15 16:32:37 +0200120 clk_disable_unprepare(host->rx_l0_sync_clk);
121
122 host->is_lane_clks_enabled = false;
123}
124
125static int ufs_qcom_enable_lane_clks(struct ufs_qcom_host *host)
126{
127 int err = 0;
128 struct device *dev = host->hba->dev;
129
130 if (host->is_lane_clks_enabled)
131 return 0;
132
133 err = ufs_qcom_host_clk_enable(dev, "rx_lane0_sync_clk",
134 host->rx_l0_sync_clk);
135 if (err)
136 goto out;
137
138 err = ufs_qcom_host_clk_enable(dev, "tx_lane0_sync_clk",
139 host->tx_l0_sync_clk);
140 if (err)
141 goto disable_rx_l0;
142
Yaniv Gardi54b879b2016-03-10 17:37:05 +0200143 if (host->hba->lanes_per_direction > 1) {
144 err = ufs_qcom_host_clk_enable(dev, "rx_lane1_sync_clk",
145 host->rx_l1_sync_clk);
146 if (err)
147 goto disable_tx_l0;
Yaniv Gardi81c0fc52015-01-15 16:32:37 +0200148
Yaniv Gardi54b879b2016-03-10 17:37:05 +0200149 err = ufs_qcom_host_clk_enable(dev, "tx_lane1_sync_clk",
150 host->tx_l1_sync_clk);
151 if (err)
152 goto disable_rx_l1;
153 }
Yaniv Gardi81c0fc52015-01-15 16:32:37 +0200154
155 host->is_lane_clks_enabled = true;
156 goto out;
157
158disable_rx_l1:
Yaniv Gardi54b879b2016-03-10 17:37:05 +0200159 if (host->hba->lanes_per_direction > 1)
160 clk_disable_unprepare(host->rx_l1_sync_clk);
Yaniv Gardi81c0fc52015-01-15 16:32:37 +0200161disable_tx_l0:
162 clk_disable_unprepare(host->tx_l0_sync_clk);
163disable_rx_l0:
164 clk_disable_unprepare(host->rx_l0_sync_clk);
165out:
166 return err;
167}
168
169static int ufs_qcom_init_lane_clks(struct ufs_qcom_host *host)
170{
171 int err = 0;
172 struct device *dev = host->hba->dev;
173
174 err = ufs_qcom_host_clk_get(dev,
175 "rx_lane0_sync_clk", &host->rx_l0_sync_clk);
176 if (err)
177 goto out;
178
179 err = ufs_qcom_host_clk_get(dev,
180 "tx_lane0_sync_clk", &host->tx_l0_sync_clk);
181 if (err)
182 goto out;
183
Yaniv Gardi54b879b2016-03-10 17:37:05 +0200184 /* In case of single lane per direction, don't read lane1 clocks */
185 if (host->hba->lanes_per_direction > 1) {
186 err = ufs_qcom_host_clk_get(dev, "rx_lane1_sync_clk",
187 &host->rx_l1_sync_clk);
188 if (err)
189 goto out;
Yaniv Gardi81c0fc52015-01-15 16:32:37 +0200190
Yaniv Gardi54b879b2016-03-10 17:37:05 +0200191 err = ufs_qcom_host_clk_get(dev, "tx_lane1_sync_clk",
192 &host->tx_l1_sync_clk);
193 }
Yaniv Gardi81c0fc52015-01-15 16:32:37 +0200194out:
195 return err;
196}
197
198static int ufs_qcom_link_startup_post_change(struct ufs_hba *hba)
199{
Yaniv Gardi1ce58982015-10-28 13:15:47 +0200200 struct ufs_qcom_host *host = ufshcd_get_variant(hba);
Yaniv Gardi81c0fc52015-01-15 16:32:37 +0200201 struct phy *phy = host->generic_phy;
202 u32 tx_lanes;
203 int err = 0;
204
205 err = ufs_qcom_get_connected_tx_lanes(hba, &tx_lanes);
206 if (err)
207 goto out;
208
209 err = ufs_qcom_phy_set_tx_lane_enable(phy, tx_lanes);
210 if (err)
211 dev_err(hba->dev, "%s: ufs_qcom_phy_set_tx_lane_enable failed\n",
212 __func__);
213
214out:
215 return err;
216}
217
218static int ufs_qcom_check_hibern8(struct ufs_hba *hba)
219{
220 int err;
221 u32 tx_fsm_val = 0;
222 unsigned long timeout = jiffies + msecs_to_jiffies(HBRN8_POLL_TOUT_MS);
223
224 do {
225 err = ufshcd_dme_get(hba,
Yaniv Gardif06fcc72015-10-28 13:15:51 +0200226 UIC_ARG_MIB_SEL(MPHY_TX_FSM_STATE,
227 UIC_ARG_MPHY_TX_GEN_SEL_INDEX(0)),
228 &tx_fsm_val);
Yaniv Gardi81c0fc52015-01-15 16:32:37 +0200229 if (err || tx_fsm_val == TX_FSM_HIBERN8)
230 break;
231
232 /* sleep for max. 200us */
233 usleep_range(100, 200);
234 } while (time_before(jiffies, timeout));
235
236 /*
237 * we might have scheduled out for long during polling so
238 * check the state again.
239 */
240 if (time_after(jiffies, timeout))
241 err = ufshcd_dme_get(hba,
Yaniv Gardif06fcc72015-10-28 13:15:51 +0200242 UIC_ARG_MIB_SEL(MPHY_TX_FSM_STATE,
243 UIC_ARG_MPHY_TX_GEN_SEL_INDEX(0)),
244 &tx_fsm_val);
Yaniv Gardi81c0fc52015-01-15 16:32:37 +0200245
246 if (err) {
247 dev_err(hba->dev, "%s: unable to get TX_FSM_STATE, err %d\n",
248 __func__, err);
249 } else if (tx_fsm_val != TX_FSM_HIBERN8) {
250 err = tx_fsm_val;
251 dev_err(hba->dev, "%s: invalid TX_FSM_STATE = %d\n",
252 __func__, err);
253 }
254
255 return err;
256}
257
Yaniv Gardif06fcc72015-10-28 13:15:51 +0200258static void ufs_qcom_select_unipro_mode(struct ufs_qcom_host *host)
259{
260 ufshcd_rmwl(host->hba, QUNIPRO_SEL,
261 ufs_qcom_cap_qunipro(host) ? QUNIPRO_SEL : 0,
262 REG_UFS_CFG1);
263 /* make sure above configuration is applied before we return */
264 mb();
265}
266
Yaniv Gardi81c0fc52015-01-15 16:32:37 +0200267static int ufs_qcom_power_up_sequence(struct ufs_hba *hba)
268{
Yaniv Gardi1ce58982015-10-28 13:15:47 +0200269 struct ufs_qcom_host *host = ufshcd_get_variant(hba);
Yaniv Gardi81c0fc52015-01-15 16:32:37 +0200270 struct phy *phy = host->generic_phy;
271 int ret = 0;
Yaniv Gardi81c0fc52015-01-15 16:32:37 +0200272 bool is_rate_B = (UFS_QCOM_LIMIT_HS_RATE == PA_HS_MODE_B)
273 ? true : false;
274
275 /* Assert PHY reset and apply PHY calibration values */
276 ufs_qcom_assert_reset(hba);
277 /* provide 1ms delay to let the reset pulse propagate */
278 usleep_range(1000, 1100);
279
Yaniv Gardi81c0fc52015-01-15 16:32:37 +0200280 ret = ufs_qcom_phy_calibrate_phy(phy, is_rate_B);
Yaniv Gardif06fcc72015-10-28 13:15:51 +0200281
Yaniv Gardi81c0fc52015-01-15 16:32:37 +0200282 if (ret) {
Yaniv Gardi4b9ad0b2016-03-10 17:37:19 +0200283 dev_err(hba->dev, "%s: ufs_qcom_phy_calibrate_phy() failed, ret = %d\n",
284 __func__, ret);
Yaniv Gardi81c0fc52015-01-15 16:32:37 +0200285 goto out;
286 }
287
288 /* De-assert PHY reset and start serdes */
289 ufs_qcom_deassert_reset(hba);
290
291 /*
292 * after reset deassertion, phy will need all ref clocks,
293 * voltage, current to settle down before starting serdes.
294 */
295 usleep_range(1000, 1100);
296 ret = ufs_qcom_phy_start_serdes(phy);
297 if (ret) {
298 dev_err(hba->dev, "%s: ufs_qcom_phy_start_serdes() failed, ret = %d\n",
299 __func__, ret);
300 goto out;
301 }
302
303 ret = ufs_qcom_phy_is_pcs_ready(phy);
304 if (ret)
Yaniv Gardif06fcc72015-10-28 13:15:51 +0200305 dev_err(hba->dev,
306 "%s: is_physical_coding_sublayer_ready() failed, ret = %d\n",
Yaniv Gardi81c0fc52015-01-15 16:32:37 +0200307 __func__, ret);
308
Yaniv Gardif06fcc72015-10-28 13:15:51 +0200309 ufs_qcom_select_unipro_mode(host);
310
Yaniv Gardi81c0fc52015-01-15 16:32:37 +0200311out:
312 return ret;
313}
314
315/*
316 * The UTP controller has a number of internal clock gating cells (CGCs).
317 * Internal hardware sub-modules within the UTP controller control the CGCs.
318 * Hardware CGCs disable the clock to inactivate UTP sub-modules not involved
319 * in a specific operation, UTP controller CGCs are by default disabled and
320 * this function enables them (after every UFS link startup) to save some power
321 * leakage.
322 */
323static void ufs_qcom_enable_hw_clk_gating(struct ufs_hba *hba)
324{
325 ufshcd_writel(hba,
326 ufshcd_readl(hba, REG_UFS_CFG2) | REG_UFS_CFG2_CGC_EN_ALL,
327 REG_UFS_CFG2);
328
329 /* Ensure that HW clock gating is enabled before next operations */
330 mb();
331}
332
Yaniv Gardif06fcc72015-10-28 13:15:51 +0200333static int ufs_qcom_hce_enable_notify(struct ufs_hba *hba,
334 enum ufs_notify_change_status status)
Yaniv Gardi81c0fc52015-01-15 16:32:37 +0200335{
Yaniv Gardi1ce58982015-10-28 13:15:47 +0200336 struct ufs_qcom_host *host = ufshcd_get_variant(hba);
Yaniv Gardi81c0fc52015-01-15 16:32:37 +0200337 int err = 0;
338
339 switch (status) {
340 case PRE_CHANGE:
341 ufs_qcom_power_up_sequence(hba);
342 /*
343 * The PHY PLL output is the source of tx/rx lane symbol
344 * clocks, hence, enable the lane clocks only after PHY
345 * is initialized.
346 */
347 err = ufs_qcom_enable_lane_clks(host);
348 break;
349 case POST_CHANGE:
350 /* check if UFS PHY moved from DISABLED to HIBERN8 */
351 err = ufs_qcom_check_hibern8(hba);
352 ufs_qcom_enable_hw_clk_gating(hba);
353
354 break;
355 default:
356 dev_err(hba->dev, "%s: invalid status %d\n", __func__, status);
357 err = -EINVAL;
358 break;
359 }
360 return err;
361}
362
363/**
Yaniv Gardif06fcc72015-10-28 13:15:51 +0200364 * Returns zero for success and non-zero in case of a failure
Yaniv Gardi81c0fc52015-01-15 16:32:37 +0200365 */
Yaniv Gardif06fcc72015-10-28 13:15:51 +0200366static int ufs_qcom_cfg_timers(struct ufs_hba *hba, u32 gear,
367 u32 hs, u32 rate, bool update_link_startup_timer)
Yaniv Gardi81c0fc52015-01-15 16:32:37 +0200368{
Yaniv Gardif06fcc72015-10-28 13:15:51 +0200369 int ret = 0;
Yaniv Gardi1ce58982015-10-28 13:15:47 +0200370 struct ufs_qcom_host *host = ufshcd_get_variant(hba);
Yaniv Gardi81c0fc52015-01-15 16:32:37 +0200371 struct ufs_clk_info *clki;
372 u32 core_clk_period_in_ns;
373 u32 tx_clk_cycles_per_us = 0;
374 unsigned long core_clk_rate = 0;
375 u32 core_clk_cycles_per_us = 0;
376
377 static u32 pwm_fr_table[][2] = {
378 {UFS_PWM_G1, 0x1},
379 {UFS_PWM_G2, 0x1},
380 {UFS_PWM_G3, 0x1},
381 {UFS_PWM_G4, 0x1},
382 };
383
384 static u32 hs_fr_table_rA[][2] = {
385 {UFS_HS_G1, 0x1F},
386 {UFS_HS_G2, 0x3e},
Yaniv Gardif06fcc72015-10-28 13:15:51 +0200387 {UFS_HS_G3, 0x7D},
Yaniv Gardi81c0fc52015-01-15 16:32:37 +0200388 };
389
390 static u32 hs_fr_table_rB[][2] = {
391 {UFS_HS_G1, 0x24},
392 {UFS_HS_G2, 0x49},
Yaniv Gardif06fcc72015-10-28 13:15:51 +0200393 {UFS_HS_G3, 0x92},
Yaniv Gardi81c0fc52015-01-15 16:32:37 +0200394 };
395
Yaniv Gardi81c7e062015-05-17 18:54:58 +0300396 /*
397 * The Qunipro controller does not use following registers:
398 * SYS1CLK_1US_REG, TX_SYMBOL_CLK_1US_REG, CLK_NS_REG &
399 * UFS_REG_PA_LINK_STARTUP_TIMER
400 * But UTP controller uses SYS1CLK_1US_REG register for Interrupt
401 * Aggregation logic.
402 */
403 if (ufs_qcom_cap_qunipro(host) && !ufshcd_is_intr_aggr_allowed(hba))
404 goto out;
405
Yaniv Gardi81c0fc52015-01-15 16:32:37 +0200406 if (gear == 0) {
407 dev_err(hba->dev, "%s: invalid gear = %d\n", __func__, gear);
408 goto out_error;
409 }
410
411 list_for_each_entry(clki, &hba->clk_list_head, list) {
412 if (!strcmp(clki->name, "core_clk"))
413 core_clk_rate = clk_get_rate(clki->clk);
414 }
415
416 /* If frequency is smaller than 1MHz, set to 1MHz */
417 if (core_clk_rate < DEFAULT_CLK_RATE_HZ)
418 core_clk_rate = DEFAULT_CLK_RATE_HZ;
419
420 core_clk_cycles_per_us = core_clk_rate / USEC_PER_SEC;
Yaniv Gardif06fcc72015-10-28 13:15:51 +0200421 if (ufshcd_readl(hba, REG_UFS_SYS1CLK_1US) != core_clk_cycles_per_us) {
422 ufshcd_writel(hba, core_clk_cycles_per_us, REG_UFS_SYS1CLK_1US);
423 /*
424 * make sure above write gets applied before we return from
425 * this function.
426 */
427 mb();
428 }
429
430 if (ufs_qcom_cap_qunipro(host))
431 goto out;
Yaniv Gardi81c0fc52015-01-15 16:32:37 +0200432
433 core_clk_period_in_ns = NSEC_PER_SEC / core_clk_rate;
434 core_clk_period_in_ns <<= OFFSET_CLK_NS_REG;
435 core_clk_period_in_ns &= MASK_CLK_NS_REG;
436
437 switch (hs) {
438 case FASTAUTO_MODE:
439 case FAST_MODE:
440 if (rate == PA_HS_MODE_A) {
441 if (gear > ARRAY_SIZE(hs_fr_table_rA)) {
442 dev_err(hba->dev,
443 "%s: index %d exceeds table size %zu\n",
444 __func__, gear,
445 ARRAY_SIZE(hs_fr_table_rA));
446 goto out_error;
447 }
448 tx_clk_cycles_per_us = hs_fr_table_rA[gear-1][1];
449 } else if (rate == PA_HS_MODE_B) {
450 if (gear > ARRAY_SIZE(hs_fr_table_rB)) {
451 dev_err(hba->dev,
452 "%s: index %d exceeds table size %zu\n",
453 __func__, gear,
454 ARRAY_SIZE(hs_fr_table_rB));
455 goto out_error;
456 }
457 tx_clk_cycles_per_us = hs_fr_table_rB[gear-1][1];
458 } else {
459 dev_err(hba->dev, "%s: invalid rate = %d\n",
460 __func__, rate);
461 goto out_error;
462 }
463 break;
464 case SLOWAUTO_MODE:
465 case SLOW_MODE:
466 if (gear > ARRAY_SIZE(pwm_fr_table)) {
467 dev_err(hba->dev,
468 "%s: index %d exceeds table size %zu\n",
469 __func__, gear,
470 ARRAY_SIZE(pwm_fr_table));
471 goto out_error;
472 }
473 tx_clk_cycles_per_us = pwm_fr_table[gear-1][1];
474 break;
475 case UNCHANGED:
476 default:
477 dev_err(hba->dev, "%s: invalid mode = %d\n", __func__, hs);
478 goto out_error;
479 }
480
Yaniv Gardif06fcc72015-10-28 13:15:51 +0200481 if (ufshcd_readl(hba, REG_UFS_TX_SYMBOL_CLK_NS_US) !=
482 (core_clk_period_in_ns | tx_clk_cycles_per_us)) {
483 /* this register 2 fields shall be written at once */
484 ufshcd_writel(hba, core_clk_period_in_ns | tx_clk_cycles_per_us,
485 REG_UFS_TX_SYMBOL_CLK_NS_US);
486 /*
487 * make sure above write gets applied before we return from
488 * this function.
489 */
490 mb();
491 }
492
493 if (update_link_startup_timer) {
494 ufshcd_writel(hba, ((core_clk_rate / MSEC_PER_SEC) * 100),
495 REG_UFS_PA_LINK_STARTUP_TIMER);
496 /*
497 * make sure that this configuration is applied before
498 * we return
499 */
500 mb();
501 }
Yaniv Gardi81c0fc52015-01-15 16:32:37 +0200502 goto out;
503
504out_error:
Yaniv Gardif06fcc72015-10-28 13:15:51 +0200505 ret = -EINVAL;
Yaniv Gardi81c0fc52015-01-15 16:32:37 +0200506out:
Yaniv Gardif06fcc72015-10-28 13:15:51 +0200507 return ret;
Yaniv Gardi81c0fc52015-01-15 16:32:37 +0200508}
509
Yaniv Gardif06fcc72015-10-28 13:15:51 +0200510static int ufs_qcom_link_startup_notify(struct ufs_hba *hba,
511 enum ufs_notify_change_status status)
Yaniv Gardi81c0fc52015-01-15 16:32:37 +0200512{
Yaniv Gardif06fcc72015-10-28 13:15:51 +0200513 int err = 0;
514 struct ufs_qcom_host *host = ufshcd_get_variant(hba);
Yaniv Gardi81c0fc52015-01-15 16:32:37 +0200515
516 switch (status) {
517 case PRE_CHANGE:
Yaniv Gardif06fcc72015-10-28 13:15:51 +0200518 if (ufs_qcom_cfg_timers(hba, UFS_PWM_G1, SLOWAUTO_MODE,
519 0, true)) {
Yaniv Gardi81c0fc52015-01-15 16:32:37 +0200520 dev_err(hba->dev, "%s: ufs_qcom_cfg_timers() failed\n",
521 __func__);
Yaniv Gardif06fcc72015-10-28 13:15:51 +0200522 err = -EINVAL;
523 goto out;
Yaniv Gardi81c0fc52015-01-15 16:32:37 +0200524 }
Yaniv Gardif06fcc72015-10-28 13:15:51 +0200525
526 if (ufs_qcom_cap_qunipro(host))
527 /*
528 * set unipro core clock cycles to 150 & clear clock
529 * divider
530 */
531 err = ufs_qcom_set_dme_vs_core_clk_ctrl_clear_div(hba,
532 150);
533
Yaniv Gardi4b9ad0b2016-03-10 17:37:19 +0200534 /*
535 * Some UFS devices (and may be host) have issues if LCC is
536 * enabled. So we are setting PA_Local_TX_LCC_Enable to 0
537 * before link startup which will make sure that both host
538 * and device TX LCC are disabled once link startup is
539 * completed.
540 */
541 if (ufshcd_get_local_unipro_ver(hba) != UFS_UNIPRO_VER_1_41)
542 err = ufshcd_dme_set(hba,
543 UIC_ARG_MIB(PA_LOCAL_TX_LCC_ENABLE),
544 0);
545
Yaniv Gardi81c0fc52015-01-15 16:32:37 +0200546 break;
547 case POST_CHANGE:
548 ufs_qcom_link_startup_post_change(hba);
549 break;
550 default:
551 break;
552 }
553
Yaniv Gardif06fcc72015-10-28 13:15:51 +0200554out:
555 return err;
Yaniv Gardi81c0fc52015-01-15 16:32:37 +0200556}
557
558static int ufs_qcom_suspend(struct ufs_hba *hba, enum ufs_pm_op pm_op)
559{
Yaniv Gardi1ce58982015-10-28 13:15:47 +0200560 struct ufs_qcom_host *host = ufshcd_get_variant(hba);
Yaniv Gardi81c0fc52015-01-15 16:32:37 +0200561 struct phy *phy = host->generic_phy;
562 int ret = 0;
563
564 if (ufs_qcom_is_link_off(hba)) {
565 /*
566 * Disable the tx/rx lane symbol clocks before PHY is
567 * powered down as the PLL source should be disabled
568 * after downstream clocks are disabled.
569 */
570 ufs_qcom_disable_lane_clks(host);
571 phy_power_off(phy);
572
573 /* Assert PHY soft reset */
574 ufs_qcom_assert_reset(hba);
575 goto out;
576 }
577
578 /*
579 * If UniPro link is not active, PHY ref_clk, main PHY analog power
580 * rail and low noise analog power rail for PLL can be switched off.
581 */
Yaniv Gardif06fcc72015-10-28 13:15:51 +0200582 if (!ufs_qcom_is_link_active(hba)) {
583 ufs_qcom_disable_lane_clks(host);
Yaniv Gardi81c0fc52015-01-15 16:32:37 +0200584 phy_power_off(phy);
Yaniv Gardif06fcc72015-10-28 13:15:51 +0200585 }
Yaniv Gardi81c0fc52015-01-15 16:32:37 +0200586
587out:
588 return ret;
589}
590
591static int ufs_qcom_resume(struct ufs_hba *hba, enum ufs_pm_op pm_op)
592{
Yaniv Gardi1ce58982015-10-28 13:15:47 +0200593 struct ufs_qcom_host *host = ufshcd_get_variant(hba);
Yaniv Gardi81c0fc52015-01-15 16:32:37 +0200594 struct phy *phy = host->generic_phy;
595 int err;
596
597 err = phy_power_on(phy);
598 if (err) {
599 dev_err(hba->dev, "%s: failed enabling regs, err = %d\n",
600 __func__, err);
601 goto out;
602 }
603
Yaniv Gardif06fcc72015-10-28 13:15:51 +0200604 err = ufs_qcom_enable_lane_clks(host);
605 if (err)
606 goto out;
607
Yaniv Gardi81c0fc52015-01-15 16:32:37 +0200608 hba->is_sys_suspended = false;
609
610out:
611 return err;
612}
613
614struct ufs_qcom_dev_params {
615 u32 pwm_rx_gear; /* pwm rx gear to work in */
616 u32 pwm_tx_gear; /* pwm tx gear to work in */
617 u32 hs_rx_gear; /* hs rx gear to work in */
618 u32 hs_tx_gear; /* hs tx gear to work in */
619 u32 rx_lanes; /* number of rx lanes */
620 u32 tx_lanes; /* number of tx lanes */
621 u32 rx_pwr_pwm; /* rx pwm working pwr */
622 u32 tx_pwr_pwm; /* tx pwm working pwr */
623 u32 rx_pwr_hs; /* rx hs working pwr */
624 u32 tx_pwr_hs; /* tx hs working pwr */
625 u32 hs_rate; /* rate A/B to work in HS */
626 u32 desired_working_mode;
627};
628
629static int ufs_qcom_get_pwr_dev_param(struct ufs_qcom_dev_params *qcom_param,
630 struct ufs_pa_layer_attr *dev_max,
631 struct ufs_pa_layer_attr *agreed_pwr)
632{
633 int min_qcom_gear;
634 int min_dev_gear;
635 bool is_dev_sup_hs = false;
636 bool is_qcom_max_hs = false;
637
638 if (dev_max->pwr_rx == FAST_MODE)
639 is_dev_sup_hs = true;
640
641 if (qcom_param->desired_working_mode == FAST) {
642 is_qcom_max_hs = true;
643 min_qcom_gear = min_t(u32, qcom_param->hs_rx_gear,
644 qcom_param->hs_tx_gear);
645 } else {
646 min_qcom_gear = min_t(u32, qcom_param->pwm_rx_gear,
647 qcom_param->pwm_tx_gear);
648 }
649
650 /*
651 * device doesn't support HS but qcom_param->desired_working_mode is
652 * HS, thus device and qcom_param don't agree
653 */
654 if (!is_dev_sup_hs && is_qcom_max_hs) {
655 pr_err("%s: failed to agree on power mode (device doesn't support HS but requested power is HS)\n",
656 __func__);
657 return -ENOTSUPP;
658 } else if (is_dev_sup_hs && is_qcom_max_hs) {
659 /*
660 * since device supports HS, it supports FAST_MODE.
661 * since qcom_param->desired_working_mode is also HS
662 * then final decision (FAST/FASTAUTO) is done according
663 * to qcom_params as it is the restricting factor
664 */
665 agreed_pwr->pwr_rx = agreed_pwr->pwr_tx =
666 qcom_param->rx_pwr_hs;
667 } else {
668 /*
669 * here qcom_param->desired_working_mode is PWM.
670 * it doesn't matter whether device supports HS or PWM,
671 * in both cases qcom_param->desired_working_mode will
672 * determine the mode
673 */
674 agreed_pwr->pwr_rx = agreed_pwr->pwr_tx =
675 qcom_param->rx_pwr_pwm;
676 }
677
678 /*
679 * we would like tx to work in the minimum number of lanes
680 * between device capability and vendor preferences.
681 * the same decision will be made for rx
682 */
683 agreed_pwr->lane_tx = min_t(u32, dev_max->lane_tx,
684 qcom_param->tx_lanes);
685 agreed_pwr->lane_rx = min_t(u32, dev_max->lane_rx,
686 qcom_param->rx_lanes);
687
688 /* device maximum gear is the minimum between device rx and tx gears */
689 min_dev_gear = min_t(u32, dev_max->gear_rx, dev_max->gear_tx);
690
691 /*
692 * if both device capabilities and vendor pre-defined preferences are
693 * both HS or both PWM then set the minimum gear to be the chosen
694 * working gear.
695 * if one is PWM and one is HS then the one that is PWM get to decide
696 * what is the gear, as it is the one that also decided previously what
697 * pwr the device will be configured to.
698 */
699 if ((is_dev_sup_hs && is_qcom_max_hs) ||
700 (!is_dev_sup_hs && !is_qcom_max_hs))
701 agreed_pwr->gear_rx = agreed_pwr->gear_tx =
702 min_t(u32, min_dev_gear, min_qcom_gear);
703 else if (!is_dev_sup_hs)
704 agreed_pwr->gear_rx = agreed_pwr->gear_tx = min_dev_gear;
705 else
706 agreed_pwr->gear_rx = agreed_pwr->gear_tx = min_qcom_gear;
707
708 agreed_pwr->hs_rate = qcom_param->hs_rate;
709 return 0;
710}
711
Yaniv Gardif06fcc72015-10-28 13:15:51 +0200712#ifdef CONFIG_MSM_BUS_SCALING
713static int ufs_qcom_get_bus_vote(struct ufs_qcom_host *host,
714 const char *speed_mode)
715{
716 struct device *dev = host->hba->dev;
717 struct device_node *np = dev->of_node;
718 int err;
719 const char *key = "qcom,bus-vector-names";
720
721 if (!speed_mode) {
722 err = -EINVAL;
723 goto out;
724 }
725
726 if (host->bus_vote.is_max_bw_needed && !!strcmp(speed_mode, "MIN"))
727 err = of_property_match_string(np, key, "MAX");
728 else
729 err = of_property_match_string(np, key, speed_mode);
730
731out:
732 if (err < 0)
733 dev_err(dev, "%s: Invalid %s mode %d\n",
734 __func__, speed_mode, err);
735 return err;
736}
737
738static void ufs_qcom_get_speed_mode(struct ufs_pa_layer_attr *p, char *result)
739{
740 int gear = max_t(u32, p->gear_rx, p->gear_tx);
741 int lanes = max_t(u32, p->lane_rx, p->lane_tx);
742 int pwr;
743
744 /* default to PWM Gear 1, Lane 1 if power mode is not initialized */
745 if (!gear)
746 gear = 1;
747
748 if (!lanes)
749 lanes = 1;
750
751 if (!p->pwr_rx && !p->pwr_tx) {
752 pwr = SLOWAUTO_MODE;
753 snprintf(result, BUS_VECTOR_NAME_LEN, "MIN");
754 } else if (p->pwr_rx == FAST_MODE || p->pwr_rx == FASTAUTO_MODE ||
755 p->pwr_tx == FAST_MODE || p->pwr_tx == FASTAUTO_MODE) {
756 pwr = FAST_MODE;
757 snprintf(result, BUS_VECTOR_NAME_LEN, "%s_R%s_G%d_L%d", "HS",
758 p->hs_rate == PA_HS_MODE_B ? "B" : "A", gear, lanes);
759 } else {
760 pwr = SLOW_MODE;
761 snprintf(result, BUS_VECTOR_NAME_LEN, "%s_G%d_L%d",
762 "PWM", gear, lanes);
763 }
764}
765
766static int ufs_qcom_set_bus_vote(struct ufs_qcom_host *host, int vote)
767{
768 int err = 0;
769
770 if (vote != host->bus_vote.curr_vote) {
771 err = msm_bus_scale_client_update_request(
772 host->bus_vote.client_handle, vote);
773 if (err) {
774 dev_err(host->hba->dev,
775 "%s: msm_bus_scale_client_update_request() failed: bus_client_handle=0x%x, vote=%d, err=%d\n",
776 __func__, host->bus_vote.client_handle,
777 vote, err);
778 goto out;
779 }
780
781 host->bus_vote.curr_vote = vote;
782 }
783out:
784 return err;
785}
786
Yaniv Gardi81c0fc52015-01-15 16:32:37 +0200787static int ufs_qcom_update_bus_bw_vote(struct ufs_qcom_host *host)
788{
789 int vote;
790 int err = 0;
791 char mode[BUS_VECTOR_NAME_LEN];
792
793 ufs_qcom_get_speed_mode(&host->dev_req_params, mode);
794
795 vote = ufs_qcom_get_bus_vote(host, mode);
796 if (vote >= 0)
797 err = ufs_qcom_set_bus_vote(host, vote);
798 else
799 err = vote;
800
801 if (err)
802 dev_err(host->hba->dev, "%s: failed %d\n", __func__, err);
803 else
804 host->bus_vote.saved_vote = vote;
805 return err;
806}
807
Yaniv Gardif06fcc72015-10-28 13:15:51 +0200808static ssize_t
809show_ufs_to_mem_max_bus_bw(struct device *dev, struct device_attribute *attr,
810 char *buf)
811{
812 struct ufs_hba *hba = dev_get_drvdata(dev);
813 struct ufs_qcom_host *host = ufshcd_get_variant(hba);
814
815 return snprintf(buf, PAGE_SIZE, "%u\n",
816 host->bus_vote.is_max_bw_needed);
817}
818
819static ssize_t
820store_ufs_to_mem_max_bus_bw(struct device *dev, struct device_attribute *attr,
821 const char *buf, size_t count)
822{
823 struct ufs_hba *hba = dev_get_drvdata(dev);
824 struct ufs_qcom_host *host = ufshcd_get_variant(hba);
825 uint32_t value;
826
827 if (!kstrtou32(buf, 0, &value)) {
828 host->bus_vote.is_max_bw_needed = !!value;
829 ufs_qcom_update_bus_bw_vote(host);
830 }
831
832 return count;
833}
834
835static int ufs_qcom_bus_register(struct ufs_qcom_host *host)
836{
837 int err;
838 struct msm_bus_scale_pdata *bus_pdata;
839 struct device *dev = host->hba->dev;
840 struct platform_device *pdev = to_platform_device(dev);
841 struct device_node *np = dev->of_node;
842
843 bus_pdata = msm_bus_cl_get_pdata(pdev);
844 if (!bus_pdata) {
845 dev_err(dev, "%s: failed to get bus vectors\n", __func__);
846 err = -ENODATA;
847 goto out;
848 }
849
850 err = of_property_count_strings(np, "qcom,bus-vector-names");
851 if (err < 0 || err != bus_pdata->num_usecases) {
852 dev_err(dev, "%s: qcom,bus-vector-names not specified correctly %d\n",
853 __func__, err);
854 goto out;
855 }
856
857 host->bus_vote.client_handle = msm_bus_scale_register_client(bus_pdata);
858 if (!host->bus_vote.client_handle) {
859 dev_err(dev, "%s: msm_bus_scale_register_client failed\n",
860 __func__);
861 err = -EFAULT;
862 goto out;
863 }
864
865 /* cache the vote index for minimum and maximum bandwidth */
866 host->bus_vote.min_bw_vote = ufs_qcom_get_bus_vote(host, "MIN");
867 host->bus_vote.max_bw_vote = ufs_qcom_get_bus_vote(host, "MAX");
868
869 host->bus_vote.max_bus_bw.show = show_ufs_to_mem_max_bus_bw;
870 host->bus_vote.max_bus_bw.store = store_ufs_to_mem_max_bus_bw;
871 sysfs_attr_init(&host->bus_vote.max_bus_bw.attr);
872 host->bus_vote.max_bus_bw.attr.name = "max_bus_bw";
873 host->bus_vote.max_bus_bw.attr.mode = S_IRUGO | S_IWUSR;
874 err = device_create_file(dev, &host->bus_vote.max_bus_bw);
875out:
876 return err;
877}
878#else /* CONFIG_MSM_BUS_SCALING */
879static int ufs_qcom_update_bus_bw_vote(struct ufs_qcom_host *host)
880{
881 return 0;
882}
883
884static int ufs_qcom_set_bus_vote(struct ufs_qcom_host *host, int vote)
885{
886 return 0;
887}
888
889static int ufs_qcom_bus_register(struct ufs_qcom_host *host)
890{
891 return 0;
892}
893#endif /* CONFIG_MSM_BUS_SCALING */
894
895static void ufs_qcom_dev_ref_clk_ctrl(struct ufs_qcom_host *host, bool enable)
896{
897 if (host->dev_ref_clk_ctrl_mmio &&
898 (enable ^ host->is_dev_ref_clk_enabled)) {
899 u32 temp = readl_relaxed(host->dev_ref_clk_ctrl_mmio);
900
901 if (enable)
902 temp |= host->dev_ref_clk_en_mask;
903 else
904 temp &= ~host->dev_ref_clk_en_mask;
905
906 /*
907 * If we are here to disable this clock it might be immediately
908 * after entering into hibern8 in which case we need to make
909 * sure that device ref_clk is active at least 1us after the
910 * hibern8 enter.
911 */
912 if (!enable)
913 udelay(1);
914
915 writel_relaxed(temp, host->dev_ref_clk_ctrl_mmio);
916
917 /* ensure that ref_clk is enabled/disabled before we return */
918 wmb();
919
920 /*
921 * If we call hibern8 exit after this, we need to make sure that
922 * device ref_clk is stable for at least 1us before the hibern8
923 * exit command.
924 */
925 if (enable)
926 udelay(1);
927
928 host->is_dev_ref_clk_enabled = enable;
929 }
930}
931
Yaniv Gardi81c0fc52015-01-15 16:32:37 +0200932static int ufs_qcom_pwr_change_notify(struct ufs_hba *hba,
Yaniv Gardif06fcc72015-10-28 13:15:51 +0200933 enum ufs_notify_change_status status,
Yaniv Gardi81c0fc52015-01-15 16:32:37 +0200934 struct ufs_pa_layer_attr *dev_max_params,
935 struct ufs_pa_layer_attr *dev_req_params)
936{
937 u32 val;
Yaniv Gardi1ce58982015-10-28 13:15:47 +0200938 struct ufs_qcom_host *host = ufshcd_get_variant(hba);
Yaniv Gardi81c0fc52015-01-15 16:32:37 +0200939 struct phy *phy = host->generic_phy;
940 struct ufs_qcom_dev_params ufs_qcom_cap;
941 int ret = 0;
942 int res = 0;
943
944 if (!dev_req_params) {
945 pr_err("%s: incoming dev_req_params is NULL\n", __func__);
946 ret = -EINVAL;
947 goto out;
948 }
949
950 switch (status) {
951 case PRE_CHANGE:
952 ufs_qcom_cap.tx_lanes = UFS_QCOM_LIMIT_NUM_LANES_TX;
953 ufs_qcom_cap.rx_lanes = UFS_QCOM_LIMIT_NUM_LANES_RX;
954 ufs_qcom_cap.hs_rx_gear = UFS_QCOM_LIMIT_HSGEAR_RX;
955 ufs_qcom_cap.hs_tx_gear = UFS_QCOM_LIMIT_HSGEAR_TX;
956 ufs_qcom_cap.pwm_rx_gear = UFS_QCOM_LIMIT_PWMGEAR_RX;
957 ufs_qcom_cap.pwm_tx_gear = UFS_QCOM_LIMIT_PWMGEAR_TX;
958 ufs_qcom_cap.rx_pwr_pwm = UFS_QCOM_LIMIT_RX_PWR_PWM;
959 ufs_qcom_cap.tx_pwr_pwm = UFS_QCOM_LIMIT_TX_PWR_PWM;
960 ufs_qcom_cap.rx_pwr_hs = UFS_QCOM_LIMIT_RX_PWR_HS;
961 ufs_qcom_cap.tx_pwr_hs = UFS_QCOM_LIMIT_TX_PWR_HS;
962 ufs_qcom_cap.hs_rate = UFS_QCOM_LIMIT_HS_RATE;
963 ufs_qcom_cap.desired_working_mode =
964 UFS_QCOM_LIMIT_DESIRED_MODE;
965
Yaniv Gardif06fcc72015-10-28 13:15:51 +0200966 if (host->hw_ver.major == 0x1) {
967 /*
968 * HS-G3 operations may not reliably work on legacy QCOM
969 * UFS host controller hardware even though capability
970 * exchange during link startup phase may end up
971 * negotiating maximum supported gear as G3.
972 * Hence downgrade the maximum supported gear to HS-G2.
973 */
974 if (ufs_qcom_cap.hs_tx_gear > UFS_HS_G2)
975 ufs_qcom_cap.hs_tx_gear = UFS_HS_G2;
976 if (ufs_qcom_cap.hs_rx_gear > UFS_HS_G2)
977 ufs_qcom_cap.hs_rx_gear = UFS_HS_G2;
978 }
979
Yaniv Gardi81c0fc52015-01-15 16:32:37 +0200980 ret = ufs_qcom_get_pwr_dev_param(&ufs_qcom_cap,
981 dev_max_params,
982 dev_req_params);
983 if (ret) {
984 pr_err("%s: failed to determine capabilities\n",
985 __func__);
986 goto out;
987 }
988
Yaniv Gardif37aabc2016-03-10 17:37:20 +0200989 /* enable the device ref clock before changing to HS mode */
990 if (!ufshcd_is_hs_mode(&hba->pwr_info) &&
991 ufshcd_is_hs_mode(dev_req_params))
992 ufs_qcom_dev_ref_clk_ctrl(host, true);
Yaniv Gardi81c0fc52015-01-15 16:32:37 +0200993 break;
994 case POST_CHANGE:
Yaniv Gardif06fcc72015-10-28 13:15:51 +0200995 if (ufs_qcom_cfg_timers(hba, dev_req_params->gear_rx,
Yaniv Gardi81c0fc52015-01-15 16:32:37 +0200996 dev_req_params->pwr_rx,
Yaniv Gardif06fcc72015-10-28 13:15:51 +0200997 dev_req_params->hs_rate, false)) {
Yaniv Gardi81c0fc52015-01-15 16:32:37 +0200998 dev_err(hba->dev, "%s: ufs_qcom_cfg_timers() failed\n",
999 __func__);
1000 /*
1001 * we return error code at the end of the routine,
1002 * but continue to configure UFS_PHY_TX_LANE_ENABLE
1003 * and bus voting as usual
1004 */
1005 ret = -EINVAL;
1006 }
1007
1008 val = ~(MAX_U32 << dev_req_params->lane_tx);
1009 res = ufs_qcom_phy_set_tx_lane_enable(phy, val);
1010 if (res) {
1011 dev_err(hba->dev, "%s: ufs_qcom_phy_set_tx_lane_enable() failed res = %d\n",
1012 __func__, res);
1013 ret = res;
1014 }
1015
1016 /* cache the power mode parameters to use internally */
1017 memcpy(&host->dev_req_params,
1018 dev_req_params, sizeof(*dev_req_params));
1019 ufs_qcom_update_bus_bw_vote(host);
Yaniv Gardif37aabc2016-03-10 17:37:20 +02001020
1021 /* disable the device ref clock if entered PWM mode */
1022 if (ufshcd_is_hs_mode(&hba->pwr_info) &&
1023 !ufshcd_is_hs_mode(dev_req_params))
1024 ufs_qcom_dev_ref_clk_ctrl(host, false);
Yaniv Gardi81c0fc52015-01-15 16:32:37 +02001025 break;
1026 default:
1027 ret = -EINVAL;
1028 break;
1029 }
1030out:
1031 return ret;
1032}
1033
Yaniv Gardiae977582015-05-17 18:55:06 +03001034static u32 ufs_qcom_get_ufs_hci_version(struct ufs_hba *hba)
1035{
Yaniv Gardi1ce58982015-10-28 13:15:47 +02001036 struct ufs_qcom_host *host = ufshcd_get_variant(hba);
Yaniv Gardiae977582015-05-17 18:55:06 +03001037
1038 if (host->hw_ver.major == 0x1)
1039 return UFSHCI_VERSION_11;
1040 else
1041 return UFSHCI_VERSION_20;
1042}
1043
Yaniv Gardi81c0fc52015-01-15 16:32:37 +02001044/**
1045 * ufs_qcom_advertise_quirks - advertise the known QCOM UFS controller quirks
1046 * @hba: host controller instance
1047 *
1048 * QCOM UFS host controller might have some non standard behaviours (quirks)
1049 * than what is specified by UFSHCI specification. Advertise all such
1050 * quirks to standard UFS host controller driver so standard takes them into
1051 * account.
1052 */
1053static void ufs_qcom_advertise_quirks(struct ufs_hba *hba)
1054{
Yaniv Gardi1ce58982015-10-28 13:15:47 +02001055 struct ufs_qcom_host *host = ufshcd_get_variant(hba);
Yaniv Gardi81c0fc52015-01-15 16:32:37 +02001056
Yaniv Gardi81c7e062015-05-17 18:54:58 +03001057 if (host->hw_ver.major == 0x01) {
Yaniv Gardi81637432015-05-17 18:55:02 +03001058 hba->quirks |= UFSHCD_QUIRK_DELAY_BEFORE_DME_CMDS
Yaniv Gardi2c0cc2e2015-05-17 18:55:04 +03001059 | UFSHCD_QUIRK_BROKEN_PA_RXHSUNTERMCAP
1060 | UFSHCD_QUIRK_DME_PEER_ACCESS_AUTO_MODE;
Yaniv Gardi81c0fc52015-01-15 16:32:37 +02001061
Yaniv Gardi81c7e062015-05-17 18:54:58 +03001062 if (host->hw_ver.minor == 0x0001 && host->hw_ver.step == 0x0001)
1063 hba->quirks |= UFSHCD_QUIRK_BROKEN_INTR_AGGR;
Yaniv Gardif06fcc72015-10-28 13:15:51 +02001064
1065 hba->quirks |= UFSHCD_QUIRK_BROKEN_LCC;
Yaniv Gardi81c7e062015-05-17 18:54:58 +03001066 }
1067
Yaniv Gardicad2e032015-03-31 17:37:14 +03001068 if (host->hw_ver.major >= 0x2) {
Yaniv Gardiae977582015-05-17 18:55:06 +03001069 hba->quirks |= UFSHCD_QUIRK_BROKEN_UFS_HCI_VERSION;
Yaniv Gardi2f018372015-05-17 18:55:00 +03001070
Yaniv Gardicad2e032015-03-31 17:37:14 +03001071 if (!ufs_qcom_cap_qunipro(host))
1072 /* Legacy UniPro mode still need following quirks */
Yaniv Gardi81637432015-05-17 18:55:02 +03001073 hba->quirks |= (UFSHCD_QUIRK_DELAY_BEFORE_DME_CMDS
Yaniv Gardi2c0cc2e2015-05-17 18:55:04 +03001074 | UFSHCD_QUIRK_DME_PEER_ACCESS_AUTO_MODE
Yaniv Gardi81637432015-05-17 18:55:02 +03001075 | UFSHCD_QUIRK_BROKEN_PA_RXHSUNTERMCAP);
Yaniv Gardicad2e032015-03-31 17:37:14 +03001076 }
1077}
1078
1079static void ufs_qcom_set_caps(struct ufs_hba *hba)
1080{
Yaniv Gardi1ce58982015-10-28 13:15:47 +02001081 struct ufs_qcom_host *host = ufshcd_get_variant(hba);
Yaniv Gardicad2e032015-03-31 17:37:14 +03001082
Yaniv Gardif06fcc72015-10-28 13:15:51 +02001083 hba->caps |= UFSHCD_CAP_CLK_GATING | UFSHCD_CAP_HIBERN8_WITH_CLK_GATING;
1084 hba->caps |= UFSHCD_CAP_CLK_SCALING;
1085 hba->caps |= UFSHCD_CAP_AUTO_BKOPS_SUSPEND;
Yaniv Gardi81c0fc52015-01-15 16:32:37 +02001086
Yaniv Gardif06fcc72015-10-28 13:15:51 +02001087 if (host->hw_ver.major >= 0x2) {
1088 host->caps = UFS_QCOM_CAP_QUNIPRO |
1089 UFS_QCOM_CAP_RETAIN_SEC_CFG_AFTER_PWR_COLLAPSE;
Yaniv Gardi81c0fc52015-01-15 16:32:37 +02001090 }
1091}
1092
Yaniv Gardif06fcc72015-10-28 13:15:51 +02001093/**
1094 * ufs_qcom_setup_clocks - enables/disable clocks
1095 * @hba: host controller instance
1096 * @on: If true, enable clocks else disable them.
Subhash Jadavani1e879e82016-10-06 21:48:22 -07001097 * @status: PRE_CHANGE or POST_CHANGE notify
Yaniv Gardif06fcc72015-10-28 13:15:51 +02001098 *
1099 * Returns 0 on success, non-zero on failure.
1100 */
Subhash Jadavani1e879e82016-10-06 21:48:22 -07001101static int ufs_qcom_setup_clocks(struct ufs_hba *hba, bool on,
1102 enum ufs_notify_change_status status)
Yaniv Gardi81c0fc52015-01-15 16:32:37 +02001103{
Yaniv Gardi1ce58982015-10-28 13:15:47 +02001104 struct ufs_qcom_host *host = ufshcd_get_variant(hba);
Yaniv Gardif06fcc72015-10-28 13:15:51 +02001105 int err;
Yaniv Gardi81c0fc52015-01-15 16:32:37 +02001106 int vote = 0;
1107
1108 /*
1109 * In case ufs_qcom_init() is not yet done, simply ignore.
1110 * This ufs_qcom_setup_clocks() shall be called from
1111 * ufs_qcom_init() after init is done.
1112 */
1113 if (!host)
1114 return 0;
1115
Subhash Jadavani1e879e82016-10-06 21:48:22 -07001116 if (on && (status == POST_CHANGE)) {
Yaniv Gardi81c0fc52015-01-15 16:32:37 +02001117 err = ufs_qcom_phy_enable_iface_clk(host->generic_phy);
1118 if (err)
1119 goto out;
1120
1121 err = ufs_qcom_phy_enable_ref_clk(host->generic_phy);
1122 if (err) {
1123 dev_err(hba->dev, "%s enable phy ref clock failed, err=%d\n",
1124 __func__, err);
1125 ufs_qcom_phy_disable_iface_clk(host->generic_phy);
1126 goto out;
1127 }
Yaniv Gardif37aabc2016-03-10 17:37:20 +02001128 /* enable the device ref clock for HS mode*/
1129 if (ufshcd_is_hs_mode(&hba->pwr_info))
1130 ufs_qcom_dev_ref_clk_ctrl(host, true);
Yaniv Gardi81c0fc52015-01-15 16:32:37 +02001131 vote = host->bus_vote.saved_vote;
1132 if (vote == host->bus_vote.min_bw_vote)
1133 ufs_qcom_update_bus_bw_vote(host);
Yaniv Gardif06fcc72015-10-28 13:15:51 +02001134
Subhash Jadavani1e879e82016-10-06 21:48:22 -07001135 } else if (!on && (status == PRE_CHANGE)) {
Yaniv Gardif06fcc72015-10-28 13:15:51 +02001136
Yaniv Gardi81c0fc52015-01-15 16:32:37 +02001137 /* M-PHY RMMI interface clocks can be turned off */
1138 ufs_qcom_phy_disable_iface_clk(host->generic_phy);
Yaniv Gardif06fcc72015-10-28 13:15:51 +02001139 if (!ufs_qcom_is_link_active(hba))
Yaniv Gardi81c0fc52015-01-15 16:32:37 +02001140 /* disable device ref_clk */
Yaniv Gardif06fcc72015-10-28 13:15:51 +02001141 ufs_qcom_dev_ref_clk_ctrl(host, false);
1142
Yaniv Gardi81c0fc52015-01-15 16:32:37 +02001143 vote = host->bus_vote.min_bw_vote;
1144 }
1145
1146 err = ufs_qcom_set_bus_vote(host, vote);
1147 if (err)
1148 dev_err(hba->dev, "%s: set bus vote failed %d\n",
1149 __func__, err);
1150
1151out:
1152 return err;
1153}
1154
Yaniv Gardi81c0fc52015-01-15 16:32:37 +02001155#define ANDROID_BOOT_DEV_MAX 30
1156static char android_boot_dev[ANDROID_BOOT_DEV_MAX];
Yaniv Gardifb819ee2015-10-28 13:15:45 +02001157
1158#ifndef MODULE
1159static int __init get_android_boot_dev(char *str)
Yaniv Gardi81c0fc52015-01-15 16:32:37 +02001160{
1161 strlcpy(android_boot_dev, str, ANDROID_BOOT_DEV_MAX);
1162 return 1;
1163}
1164__setup("androidboot.bootdevice=", get_android_boot_dev);
Yaniv Gardifb819ee2015-10-28 13:15:45 +02001165#endif
Yaniv Gardi81c0fc52015-01-15 16:32:37 +02001166
1167/**
1168 * ufs_qcom_init - bind phy with controller
1169 * @hba: host controller instance
1170 *
1171 * Binds PHY with controller and powers up PHY enabling clocks
1172 * and regulators.
1173 *
1174 * Returns -EPROBE_DEFER if binding fails, returns negative error
1175 * on phy power up failure and returns zero on success.
1176 */
1177static int ufs_qcom_init(struct ufs_hba *hba)
1178{
1179 int err;
1180 struct device *dev = hba->dev;
Yaniv Gardif06fcc72015-10-28 13:15:51 +02001181 struct platform_device *pdev = to_platform_device(dev);
Yaniv Gardi81c0fc52015-01-15 16:32:37 +02001182 struct ufs_qcom_host *host;
Yaniv Gardif06fcc72015-10-28 13:15:51 +02001183 struct resource *res;
Yaniv Gardi81c0fc52015-01-15 16:32:37 +02001184
1185 if (strlen(android_boot_dev) && strcmp(android_boot_dev, dev_name(dev)))
1186 return -ENODEV;
1187
1188 host = devm_kzalloc(dev, sizeof(*host), GFP_KERNEL);
1189 if (!host) {
1190 err = -ENOMEM;
1191 dev_err(dev, "%s: no memory for qcom ufs host\n", __func__);
1192 goto out;
1193 }
1194
Yaniv Gardif06fcc72015-10-28 13:15:51 +02001195 /* Make a two way bind between the qcom host and the hba */
Yaniv Gardi81c0fc52015-01-15 16:32:37 +02001196 host->hba = hba;
Yaniv Gardi1ce58982015-10-28 13:15:47 +02001197 ufshcd_set_variant(hba, host);
Yaniv Gardi81c0fc52015-01-15 16:32:37 +02001198
Yaniv Gardif06fcc72015-10-28 13:15:51 +02001199 /*
1200 * voting/devoting device ref_clk source is time consuming hence
1201 * skip devoting it during aggressive clock gating. This clock
1202 * will still be gated off during runtime suspend.
1203 */
Yaniv Gardi81c0fc52015-01-15 16:32:37 +02001204 host->generic_phy = devm_phy_get(dev, "ufsphy");
1205
1206 if (IS_ERR(host->generic_phy)) {
1207 err = PTR_ERR(host->generic_phy);
1208 dev_err(dev, "%s: PHY get failed %d\n", __func__, err);
1209 goto out;
1210 }
1211
1212 err = ufs_qcom_bus_register(host);
1213 if (err)
1214 goto out_host_free;
1215
Yaniv Gardibfdbe8b2015-03-31 17:37:13 +03001216 ufs_qcom_get_controller_revision(hba, &host->hw_ver.major,
1217 &host->hw_ver.minor, &host->hw_ver.step);
1218
Yaniv Gardif06fcc72015-10-28 13:15:51 +02001219 /*
1220 * for newer controllers, device reference clock control bit has
1221 * moved inside UFS controller register address space itself.
1222 */
1223 if (host->hw_ver.major >= 0x02) {
1224 host->dev_ref_clk_ctrl_mmio = hba->mmio_base + REG_UFS_CFG1;
1225 host->dev_ref_clk_en_mask = BIT(26);
1226 } else {
1227 /* "dev_ref_clk_ctrl_mem" is optional resource */
1228 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1229 if (res) {
1230 host->dev_ref_clk_ctrl_mmio =
1231 devm_ioremap_resource(dev, res);
1232 if (IS_ERR(host->dev_ref_clk_ctrl_mmio)) {
1233 dev_warn(dev,
1234 "%s: could not map dev_ref_clk_ctrl_mmio, err %ld\n",
1235 __func__,
1236 PTR_ERR(host->dev_ref_clk_ctrl_mmio));
1237 host->dev_ref_clk_ctrl_mmio = NULL;
1238 }
1239 host->dev_ref_clk_en_mask = BIT(5);
1240 }
1241 }
1242
Yaniv Gardibfdbe8b2015-03-31 17:37:13 +03001243 /* update phy revision information before calling phy_init() */
1244 ufs_qcom_phy_save_controller_version(host->generic_phy,
1245 host->hw_ver.major, host->hw_ver.minor, host->hw_ver.step);
1246
Yaniv Gardi81c0fc52015-01-15 16:32:37 +02001247 phy_init(host->generic_phy);
1248 err = phy_power_on(host->generic_phy);
1249 if (err)
1250 goto out_unregister_bus;
1251
1252 err = ufs_qcom_init_lane_clks(host);
1253 if (err)
1254 goto out_disable_phy;
1255
Yaniv Gardicad2e032015-03-31 17:37:14 +03001256 ufs_qcom_set_caps(hba);
Yaniv Gardi81c0fc52015-01-15 16:32:37 +02001257 ufs_qcom_advertise_quirks(hba);
1258
Subhash Jadavani1e879e82016-10-06 21:48:22 -07001259 ufs_qcom_setup_clocks(hba, true, POST_CHANGE);
Yaniv Gardi81c0fc52015-01-15 16:32:37 +02001260
1261 if (hba->dev->id < MAX_UFS_QCOM_HOSTS)
1262 ufs_qcom_hosts[hba->dev->id] = host;
1263
Yaniv Gardi6e3fd442015-10-28 13:15:50 +02001264 host->dbg_print_en |= UFS_QCOM_DEFAULT_DBG_PRINT_EN;
1265 ufs_qcom_get_default_testbus_cfg(host);
1266 err = ufs_qcom_testbus_config(host);
1267 if (err) {
1268 dev_warn(dev, "%s: failed to configure the testbus %d\n",
1269 __func__, err);
1270 err = 0;
1271 }
1272
Yaniv Gardi81c0fc52015-01-15 16:32:37 +02001273 goto out;
1274
1275out_disable_phy:
1276 phy_power_off(host->generic_phy);
1277out_unregister_bus:
1278 phy_exit(host->generic_phy);
1279out_host_free:
1280 devm_kfree(dev, host);
Yaniv Gardi1ce58982015-10-28 13:15:47 +02001281 ufshcd_set_variant(hba, NULL);
Yaniv Gardi81c0fc52015-01-15 16:32:37 +02001282out:
1283 return err;
1284}
1285
1286static void ufs_qcom_exit(struct ufs_hba *hba)
1287{
Yaniv Gardi1ce58982015-10-28 13:15:47 +02001288 struct ufs_qcom_host *host = ufshcd_get_variant(hba);
Yaniv Gardi81c0fc52015-01-15 16:32:37 +02001289
1290 ufs_qcom_disable_lane_clks(host);
1291 phy_power_off(host->generic_phy);
1292}
1293
Yaniv Gardif06fcc72015-10-28 13:15:51 +02001294static int ufs_qcom_set_dme_vs_core_clk_ctrl_clear_div(struct ufs_hba *hba,
1295 u32 clk_cycles)
1296{
1297 int err;
1298 u32 core_clk_ctrl_reg;
1299
1300 if (clk_cycles > DME_VS_CORE_CLK_CTRL_MAX_CORE_CLK_1US_CYCLES_MASK)
1301 return -EINVAL;
1302
1303 err = ufshcd_dme_get(hba,
1304 UIC_ARG_MIB(DME_VS_CORE_CLK_CTRL),
1305 &core_clk_ctrl_reg);
1306 if (err)
1307 goto out;
1308
1309 core_clk_ctrl_reg &= ~DME_VS_CORE_CLK_CTRL_MAX_CORE_CLK_1US_CYCLES_MASK;
1310 core_clk_ctrl_reg |= clk_cycles;
1311
1312 /* Clear CORE_CLK_DIV_EN */
1313 core_clk_ctrl_reg &= ~DME_VS_CORE_CLK_CTRL_CORE_CLK_DIV_EN_BIT;
1314
1315 err = ufshcd_dme_set(hba,
1316 UIC_ARG_MIB(DME_VS_CORE_CLK_CTRL),
1317 core_clk_ctrl_reg);
1318out:
1319 return err;
1320}
1321
1322static int ufs_qcom_clk_scale_up_pre_change(struct ufs_hba *hba)
1323{
1324 /* nothing to do as of now */
1325 return 0;
1326}
1327
1328static int ufs_qcom_clk_scale_up_post_change(struct ufs_hba *hba)
1329{
1330 struct ufs_qcom_host *host = ufshcd_get_variant(hba);
1331
1332 if (!ufs_qcom_cap_qunipro(host))
1333 return 0;
1334
1335 /* set unipro core clock cycles to 150 and clear clock divider */
1336 return ufs_qcom_set_dme_vs_core_clk_ctrl_clear_div(hba, 150);
1337}
1338
1339static int ufs_qcom_clk_scale_down_pre_change(struct ufs_hba *hba)
1340{
1341 struct ufs_qcom_host *host = ufshcd_get_variant(hba);
1342 int err;
1343 u32 core_clk_ctrl_reg;
1344
1345 if (!ufs_qcom_cap_qunipro(host))
1346 return 0;
1347
1348 err = ufshcd_dme_get(hba,
1349 UIC_ARG_MIB(DME_VS_CORE_CLK_CTRL),
1350 &core_clk_ctrl_reg);
1351
1352 /* make sure CORE_CLK_DIV_EN is cleared */
1353 if (!err &&
1354 (core_clk_ctrl_reg & DME_VS_CORE_CLK_CTRL_CORE_CLK_DIV_EN_BIT)) {
1355 core_clk_ctrl_reg &= ~DME_VS_CORE_CLK_CTRL_CORE_CLK_DIV_EN_BIT;
1356 err = ufshcd_dme_set(hba,
1357 UIC_ARG_MIB(DME_VS_CORE_CLK_CTRL),
1358 core_clk_ctrl_reg);
1359 }
1360
1361 return err;
1362}
1363
1364static int ufs_qcom_clk_scale_down_post_change(struct ufs_hba *hba)
1365{
1366 struct ufs_qcom_host *host = ufshcd_get_variant(hba);
1367
1368 if (!ufs_qcom_cap_qunipro(host))
1369 return 0;
1370
1371 /* set unipro core clock cycles to 75 and clear clock divider */
1372 return ufs_qcom_set_dme_vs_core_clk_ctrl_clear_div(hba, 75);
1373}
1374
1375static int ufs_qcom_clk_scale_notify(struct ufs_hba *hba,
1376 bool scale_up, enum ufs_notify_change_status status)
Yaniv Gardi81c0fc52015-01-15 16:32:37 +02001377{
Yaniv Gardi1ce58982015-10-28 13:15:47 +02001378 struct ufs_qcom_host *host = ufshcd_get_variant(hba);
Yaniv Gardi81c0fc52015-01-15 16:32:37 +02001379 struct ufs_pa_layer_attr *dev_req_params = &host->dev_req_params;
Yaniv Gardif06fcc72015-10-28 13:15:51 +02001380 int err = 0;
Yaniv Gardi81c0fc52015-01-15 16:32:37 +02001381
Yaniv Gardif06fcc72015-10-28 13:15:51 +02001382 if (status == PRE_CHANGE) {
1383 if (scale_up)
1384 err = ufs_qcom_clk_scale_up_pre_change(hba);
1385 else
1386 err = ufs_qcom_clk_scale_down_pre_change(hba);
1387 } else {
1388 if (scale_up)
1389 err = ufs_qcom_clk_scale_up_post_change(hba);
1390 else
1391 err = ufs_qcom_clk_scale_down_post_change(hba);
1392
1393 if (err || !dev_req_params)
1394 goto out;
1395
1396 ufs_qcom_cfg_timers(hba,
1397 dev_req_params->gear_rx,
1398 dev_req_params->pwr_rx,
1399 dev_req_params->hs_rate,
1400 false);
1401 ufs_qcom_update_bus_bw_vote(host);
1402 }
1403
1404out:
1405 return err;
Yaniv Gardi81c0fc52015-01-15 16:32:37 +02001406}
1407
Yaniv Gardieba5ed32016-03-10 17:37:21 +02001408static void ufs_qcom_print_hw_debug_reg_all(struct ufs_hba *hba,
1409 void *priv, void (*print_fn)(struct ufs_hba *hba,
1410 int offset, int num_regs, char *str, void *priv))
1411{
1412 u32 reg;
1413 struct ufs_qcom_host *host;
1414
1415 if (unlikely(!hba)) {
1416 pr_err("%s: hba is NULL\n", __func__);
1417 return;
1418 }
1419 if (unlikely(!print_fn)) {
1420 dev_err(hba->dev, "%s: print_fn is NULL\n", __func__);
1421 return;
1422 }
1423
1424 host = ufshcd_get_variant(hba);
1425 if (!(host->dbg_print_en & UFS_QCOM_DBG_PRINT_REGS_EN))
1426 return;
1427
1428 reg = ufs_qcom_get_debug_reg_offset(host, UFS_UFS_DBG_RD_REG_OCSC);
1429 print_fn(hba, reg, 44, "UFS_UFS_DBG_RD_REG_OCSC ", priv);
1430
1431 reg = ufshcd_readl(hba, REG_UFS_CFG1);
1432 reg |= UFS_BIT(17);
1433 ufshcd_writel(hba, reg, REG_UFS_CFG1);
1434
1435 reg = ufs_qcom_get_debug_reg_offset(host, UFS_UFS_DBG_RD_EDTL_RAM);
1436 print_fn(hba, reg, 32, "UFS_UFS_DBG_RD_EDTL_RAM ", priv);
1437
1438 reg = ufs_qcom_get_debug_reg_offset(host, UFS_UFS_DBG_RD_DESC_RAM);
1439 print_fn(hba, reg, 128, "UFS_UFS_DBG_RD_DESC_RAM ", priv);
1440
1441 reg = ufs_qcom_get_debug_reg_offset(host, UFS_UFS_DBG_RD_PRDT_RAM);
1442 print_fn(hba, reg, 64, "UFS_UFS_DBG_RD_PRDT_RAM ", priv);
1443
1444 ufshcd_writel(hba, (reg & ~UFS_BIT(17)), REG_UFS_CFG1);
1445
1446 reg = ufs_qcom_get_debug_reg_offset(host, UFS_DBG_RD_REG_UAWM);
1447 print_fn(hba, reg, 4, "UFS_DBG_RD_REG_UAWM ", priv);
1448
1449 reg = ufs_qcom_get_debug_reg_offset(host, UFS_DBG_RD_REG_UARM);
1450 print_fn(hba, reg, 4, "UFS_DBG_RD_REG_UARM ", priv);
1451
1452 reg = ufs_qcom_get_debug_reg_offset(host, UFS_DBG_RD_REG_TXUC);
1453 print_fn(hba, reg, 48, "UFS_DBG_RD_REG_TXUC ", priv);
1454
1455 reg = ufs_qcom_get_debug_reg_offset(host, UFS_DBG_RD_REG_RXUC);
1456 print_fn(hba, reg, 27, "UFS_DBG_RD_REG_RXUC ", priv);
1457
1458 reg = ufs_qcom_get_debug_reg_offset(host, UFS_DBG_RD_REG_DFC);
1459 print_fn(hba, reg, 19, "UFS_DBG_RD_REG_DFC ", priv);
1460
1461 reg = ufs_qcom_get_debug_reg_offset(host, UFS_DBG_RD_REG_TRLUT);
1462 print_fn(hba, reg, 34, "UFS_DBG_RD_REG_TRLUT ", priv);
1463
1464 reg = ufs_qcom_get_debug_reg_offset(host, UFS_DBG_RD_REG_TMRLUT);
1465 print_fn(hba, reg, 9, "UFS_DBG_RD_REG_TMRLUT ", priv);
1466}
1467
1468static void ufs_qcom_enable_test_bus(struct ufs_qcom_host *host)
1469{
1470 if (host->dbg_print_en & UFS_QCOM_DBG_PRINT_TEST_BUS_EN)
1471 ufshcd_rmwl(host->hba, TEST_BUS_EN, TEST_BUS_EN, REG_UFS_CFG1);
1472 else
1473 ufshcd_rmwl(host->hba, TEST_BUS_EN, 0, REG_UFS_CFG1);
1474}
1475
Yaniv Gardi6e3fd442015-10-28 13:15:50 +02001476static void ufs_qcom_get_default_testbus_cfg(struct ufs_qcom_host *host)
1477{
1478 /* provide a legal default configuration */
1479 host->testbus.select_major = TSTBUS_UAWM;
1480 host->testbus.select_minor = 1;
1481}
1482
1483static bool ufs_qcom_testbus_cfg_is_ok(struct ufs_qcom_host *host)
1484{
1485 if (host->testbus.select_major >= TSTBUS_MAX) {
1486 dev_err(host->hba->dev,
1487 "%s: UFS_CFG1[TEST_BUS_SEL} may not equal 0x%05X\n",
1488 __func__, host->testbus.select_major);
1489 return false;
1490 }
1491
1492 /*
1493 * Not performing check for each individual select_major
1494 * mappings of select_minor, since there is no harm in
1495 * configuring a non-existent select_minor
1496 */
1497 if (host->testbus.select_minor > 0x1F) {
1498 dev_err(host->hba->dev,
1499 "%s: 0x%05X is not a legal testbus option\n",
1500 __func__, host->testbus.select_minor);
1501 return false;
1502 }
1503
1504 return true;
1505}
1506
1507int ufs_qcom_testbus_config(struct ufs_qcom_host *host)
1508{
1509 int reg;
1510 int offset;
1511 u32 mask = TEST_BUS_SUB_SEL_MASK;
1512
1513 if (!host)
1514 return -EINVAL;
1515
1516 if (!ufs_qcom_testbus_cfg_is_ok(host))
1517 return -EPERM;
1518
1519 switch (host->testbus.select_major) {
1520 case TSTBUS_UAWM:
1521 reg = UFS_TEST_BUS_CTRL_0;
1522 offset = 24;
1523 break;
1524 case TSTBUS_UARM:
1525 reg = UFS_TEST_BUS_CTRL_0;
1526 offset = 16;
1527 break;
1528 case TSTBUS_TXUC:
1529 reg = UFS_TEST_BUS_CTRL_0;
1530 offset = 8;
1531 break;
1532 case TSTBUS_RXUC:
1533 reg = UFS_TEST_BUS_CTRL_0;
1534 offset = 0;
1535 break;
1536 case TSTBUS_DFC:
1537 reg = UFS_TEST_BUS_CTRL_1;
1538 offset = 24;
1539 break;
1540 case TSTBUS_TRLUT:
1541 reg = UFS_TEST_BUS_CTRL_1;
1542 offset = 16;
1543 break;
1544 case TSTBUS_TMRLUT:
1545 reg = UFS_TEST_BUS_CTRL_1;
1546 offset = 8;
1547 break;
1548 case TSTBUS_OCSC:
1549 reg = UFS_TEST_BUS_CTRL_1;
1550 offset = 0;
1551 break;
1552 case TSTBUS_WRAPPER:
1553 reg = UFS_TEST_BUS_CTRL_2;
1554 offset = 16;
1555 break;
1556 case TSTBUS_COMBINED:
1557 reg = UFS_TEST_BUS_CTRL_2;
1558 offset = 8;
1559 break;
1560 case TSTBUS_UTP_HCI:
1561 reg = UFS_TEST_BUS_CTRL_2;
1562 offset = 0;
1563 break;
1564 case TSTBUS_UNIPRO:
1565 reg = UFS_UNIPRO_CFG;
1566 offset = 1;
1567 break;
1568 /*
1569 * No need for a default case, since
1570 * ufs_qcom_testbus_cfg_is_ok() checks that the configuration
1571 * is legal
1572 */
1573 }
1574 mask <<= offset;
1575
1576 pm_runtime_get_sync(host->hba->dev);
1577 ufshcd_hold(host->hba, false);
1578 ufshcd_rmwl(host->hba, TEST_BUS_SEL,
1579 (u32)host->testbus.select_major << 19,
1580 REG_UFS_CFG1);
1581 ufshcd_rmwl(host->hba, mask,
1582 (u32)host->testbus.select_minor << offset,
1583 reg);
Yaniv Gardieba5ed32016-03-10 17:37:21 +02001584 ufs_qcom_enable_test_bus(host);
Yaniv Gardi6e3fd442015-10-28 13:15:50 +02001585 ufshcd_release(host->hba);
1586 pm_runtime_put_sync(host->hba->dev);
1587
1588 return 0;
1589}
1590
1591static void ufs_qcom_testbus_read(struct ufs_hba *hba)
1592{
1593 ufs_qcom_dump_regs(hba, UFS_TEST_BUS, 1, "UFS_TEST_BUS ");
1594}
1595
1596static void ufs_qcom_dump_dbg_regs(struct ufs_hba *hba)
1597{
1598 ufs_qcom_dump_regs(hba, REG_UFS_SYS1CLK_1US, 16,
1599 "HCI Vendor Specific Registers ");
1600
Yaniv Gardieba5ed32016-03-10 17:37:21 +02001601 ufs_qcom_print_hw_debug_reg_all(hba, NULL, ufs_qcom_dump_regs_wrapper);
Yaniv Gardi6e3fd442015-10-28 13:15:50 +02001602 ufs_qcom_testbus_read(hba);
1603}
Yaniv Gardieba5ed32016-03-10 17:37:21 +02001604
Yaniv Gardi81c0fc52015-01-15 16:32:37 +02001605/**
1606 * struct ufs_hba_qcom_vops - UFS QCOM specific variant operations
1607 *
1608 * The variant operations configure the necessary controller and PHY
1609 * handshake during initialization.
1610 */
Yaniv Gardi47555a52015-10-28 13:15:49 +02001611static struct ufs_hba_variant_ops ufs_hba_qcom_vops = {
Yaniv Gardi81c0fc52015-01-15 16:32:37 +02001612 .name = "qcom",
1613 .init = ufs_qcom_init,
1614 .exit = ufs_qcom_exit,
Yaniv Gardiae977582015-05-17 18:55:06 +03001615 .get_ufs_hci_version = ufs_qcom_get_ufs_hci_version,
Yaniv Gardi81c0fc52015-01-15 16:32:37 +02001616 .clk_scale_notify = ufs_qcom_clk_scale_notify,
1617 .setup_clocks = ufs_qcom_setup_clocks,
1618 .hce_enable_notify = ufs_qcom_hce_enable_notify,
1619 .link_startup_notify = ufs_qcom_link_startup_notify,
1620 .pwr_change_notify = ufs_qcom_pwr_change_notify,
1621 .suspend = ufs_qcom_suspend,
1622 .resume = ufs_qcom_resume,
Yaniv Gardi6e3fd442015-10-28 13:15:50 +02001623 .dbg_register_dump = ufs_qcom_dump_dbg_regs,
Yaniv Gardi81c0fc52015-01-15 16:32:37 +02001624};
Yaniv Gardifb819ee2015-10-28 13:15:45 +02001625
Yaniv Gardi47555a52015-10-28 13:15:49 +02001626/**
1627 * ufs_qcom_probe - probe routine of the driver
1628 * @pdev: pointer to Platform device handle
1629 *
1630 * Return zero for success and non-zero for failure
1631 */
1632static int ufs_qcom_probe(struct platform_device *pdev)
1633{
1634 int err;
1635 struct device *dev = &pdev->dev;
1636
1637 /* Perform generic probe */
1638 err = ufshcd_pltfrm_init(pdev, &ufs_hba_qcom_vops);
1639 if (err)
1640 dev_err(dev, "ufshcd_pltfrm_init() failed %d\n", err);
1641
1642 return err;
1643}
1644
1645/**
1646 * ufs_qcom_remove - set driver_data of the device to NULL
1647 * @pdev: pointer to platform device handle
1648 *
Yaniv Gardi4b9ad0b2016-03-10 17:37:19 +02001649 * Always returns 0
Yaniv Gardi47555a52015-10-28 13:15:49 +02001650 */
1651static int ufs_qcom_remove(struct platform_device *pdev)
1652{
1653 struct ufs_hba *hba = platform_get_drvdata(pdev);
1654
1655 pm_runtime_get_sync(&(pdev)->dev);
1656 ufshcd_remove(hba);
1657 return 0;
1658}
1659
1660static const struct of_device_id ufs_qcom_of_match[] = {
1661 { .compatible = "qcom,ufshc"},
1662 {},
1663};
1664
1665static const struct dev_pm_ops ufs_qcom_pm_ops = {
1666 .suspend = ufshcd_pltfrm_suspend,
1667 .resume = ufshcd_pltfrm_resume,
1668 .runtime_suspend = ufshcd_pltfrm_runtime_suspend,
1669 .runtime_resume = ufshcd_pltfrm_runtime_resume,
1670 .runtime_idle = ufshcd_pltfrm_runtime_idle,
1671};
1672
1673static struct platform_driver ufs_qcom_pltform = {
1674 .probe = ufs_qcom_probe,
1675 .remove = ufs_qcom_remove,
1676 .shutdown = ufshcd_pltfrm_shutdown,
1677 .driver = {
1678 .name = "ufshcd-qcom",
1679 .pm = &ufs_qcom_pm_ops,
1680 .of_match_table = of_match_ptr(ufs_qcom_of_match),
1681 },
1682};
1683module_platform_driver(ufs_qcom_pltform);
1684
Yaniv Gardifb819ee2015-10-28 13:15:45 +02001685MODULE_LICENSE("GPL v2");