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 #include <linux/of.h>
9 #include <linux/platform_device.h>
10 #include <linux/phy/phy.h>
11 #include <linux/gpio/consumer.h>
12 #include <linux/reset-controller.h>
13 #include <linux/devfreq.h>
14
15 #include "ufshcd.h"
16 #include "ufshcd-pltfrm.h"
17 #include "unipro.h"
18 #include "ufs-qcom.h"
19 #include "ufshci.h"
20 #include "ufs_quirks.h"
21 #define UFS_QCOM_DEFAULT_DBG_PRINT_EN \
22 (UFS_QCOM_DBG_PRINT_REGS_EN | UFS_QCOM_DBG_PRINT_TEST_BUS_EN)
23
24 enum {
25 TSTBUS_UAWM,
26 TSTBUS_UARM,
27 TSTBUS_TXUC,
28 TSTBUS_RXUC,
29 TSTBUS_DFC,
30 TSTBUS_TRLUT,
31 TSTBUS_TMRLUT,
32 TSTBUS_OCSC,
33 TSTBUS_UTP_HCI,
34 TSTBUS_COMBINED,
35 TSTBUS_WRAPPER,
36 TSTBUS_UNIPRO,
37 TSTBUS_MAX,
38 };
39
40 static struct ufs_qcom_host *ufs_qcom_hosts[MAX_UFS_QCOM_HOSTS];
41
42 static void ufs_qcom_get_default_testbus_cfg(struct ufs_qcom_host *host);
43 static int ufs_qcom_set_dme_vs_core_clk_ctrl_clear_div(struct ufs_hba *hba,
44 u32 clk_cycles);
45
rcdev_to_ufs_host(struct reset_controller_dev * rcd)46 static struct ufs_qcom_host *rcdev_to_ufs_host(struct reset_controller_dev *rcd)
47 {
48 return container_of(rcd, struct ufs_qcom_host, rcdev);
49 }
50
ufs_qcom_dump_regs_wrapper(struct ufs_hba * hba,int offset,int len,const char * prefix,void * priv)51 static void ufs_qcom_dump_regs_wrapper(struct ufs_hba *hba, int offset, int len,
52 const char *prefix, void *priv)
53 {
54 ufshcd_dump_regs(hba, offset, len * 4, prefix);
55 }
56
ufs_qcom_get_connected_tx_lanes(struct ufs_hba * hba,u32 * tx_lanes)57 static int ufs_qcom_get_connected_tx_lanes(struct ufs_hba *hba, u32 *tx_lanes)
58 {
59 int err = 0;
60
61 err = ufshcd_dme_get(hba,
62 UIC_ARG_MIB(PA_CONNECTEDTXDATALANES), tx_lanes);
63 if (err)
64 dev_err(hba->dev, "%s: couldn't read PA_CONNECTEDTXDATALANES %d\n",
65 __func__, err);
66
67 return err;
68 }
69
ufs_qcom_host_clk_get(struct device * dev,const char * name,struct clk ** clk_out,bool optional)70 static int ufs_qcom_host_clk_get(struct device *dev,
71 const char *name, struct clk **clk_out, bool optional)
72 {
73 struct clk *clk;
74 int err = 0;
75
76 clk = devm_clk_get(dev, name);
77 if (!IS_ERR(clk)) {
78 *clk_out = clk;
79 return 0;
80 }
81
82 err = PTR_ERR(clk);
83
84 if (optional && err == -ENOENT) {
85 *clk_out = NULL;
86 return 0;
87 }
88
89 if (err != -EPROBE_DEFER)
90 dev_err(dev, "failed to get %s err %d\n", name, err);
91
92 return err;
93 }
94
ufs_qcom_host_clk_enable(struct device * dev,const char * name,struct clk * clk)95 static int ufs_qcom_host_clk_enable(struct device *dev,
96 const char *name, struct clk *clk)
97 {
98 int err = 0;
99
100 err = clk_prepare_enable(clk);
101 if (err)
102 dev_err(dev, "%s: %s enable failed %d\n", __func__, name, err);
103
104 return err;
105 }
106
ufs_qcom_disable_lane_clks(struct ufs_qcom_host * host)107 static void ufs_qcom_disable_lane_clks(struct ufs_qcom_host *host)
108 {
109 if (!host->is_lane_clks_enabled)
110 return;
111
112 clk_disable_unprepare(host->tx_l1_sync_clk);
113 clk_disable_unprepare(host->tx_l0_sync_clk);
114 clk_disable_unprepare(host->rx_l1_sync_clk);
115 clk_disable_unprepare(host->rx_l0_sync_clk);
116
117 host->is_lane_clks_enabled = false;
118 }
119
ufs_qcom_enable_lane_clks(struct ufs_qcom_host * host)120 static int ufs_qcom_enable_lane_clks(struct ufs_qcom_host *host)
121 {
122 int err = 0;
123 struct device *dev = host->hba->dev;
124
125 if (host->is_lane_clks_enabled)
126 return 0;
127
128 err = ufs_qcom_host_clk_enable(dev, "rx_lane0_sync_clk",
129 host->rx_l0_sync_clk);
130 if (err)
131 goto out;
132
133 err = ufs_qcom_host_clk_enable(dev, "tx_lane0_sync_clk",
134 host->tx_l0_sync_clk);
135 if (err)
136 goto disable_rx_l0;
137
138 err = ufs_qcom_host_clk_enable(dev, "rx_lane1_sync_clk",
139 host->rx_l1_sync_clk);
140 if (err)
141 goto disable_tx_l0;
142
143 err = ufs_qcom_host_clk_enable(dev, "tx_lane1_sync_clk",
144 host->tx_l1_sync_clk);
145 if (err)
146 goto disable_rx_l1;
147
148 host->is_lane_clks_enabled = true;
149 goto out;
150
151 disable_rx_l1:
152 clk_disable_unprepare(host->rx_l1_sync_clk);
153 disable_tx_l0:
154 clk_disable_unprepare(host->tx_l0_sync_clk);
155 disable_rx_l0:
156 clk_disable_unprepare(host->rx_l0_sync_clk);
157 out:
158 return err;
159 }
160
ufs_qcom_init_lane_clks(struct ufs_qcom_host * host)161 static int ufs_qcom_init_lane_clks(struct ufs_qcom_host *host)
162 {
163 int err = 0;
164 struct device *dev = host->hba->dev;
165
166 if (has_acpi_companion(dev))
167 return 0;
168
169 err = ufs_qcom_host_clk_get(dev, "rx_lane0_sync_clk",
170 &host->rx_l0_sync_clk, false);
171 if (err)
172 goto out;
173
174 err = ufs_qcom_host_clk_get(dev, "tx_lane0_sync_clk",
175 &host->tx_l0_sync_clk, false);
176 if (err)
177 goto out;
178
179 /* In case of single lane per direction, don't read lane1 clocks */
180 if (host->hba->lanes_per_direction > 1) {
181 err = ufs_qcom_host_clk_get(dev, "rx_lane1_sync_clk",
182 &host->rx_l1_sync_clk, false);
183 if (err)
184 goto out;
185
186 err = ufs_qcom_host_clk_get(dev, "tx_lane1_sync_clk",
187 &host->tx_l1_sync_clk, true);
188 }
189 out:
190 return err;
191 }
192
ufs_qcom_link_startup_post_change(struct ufs_hba * hba)193 static int ufs_qcom_link_startup_post_change(struct ufs_hba *hba)
194 {
195 u32 tx_lanes;
196
197 return ufs_qcom_get_connected_tx_lanes(hba, &tx_lanes);
198 }
199
ufs_qcom_check_hibern8(struct ufs_hba * hba)200 static int ufs_qcom_check_hibern8(struct ufs_hba *hba)
201 {
202 int err;
203 u32 tx_fsm_val = 0;
204 unsigned long timeout = jiffies + msecs_to_jiffies(HBRN8_POLL_TOUT_MS);
205
206 do {
207 err = ufshcd_dme_get(hba,
208 UIC_ARG_MIB_SEL(MPHY_TX_FSM_STATE,
209 UIC_ARG_MPHY_TX_GEN_SEL_INDEX(0)),
210 &tx_fsm_val);
211 if (err || tx_fsm_val == TX_FSM_HIBERN8)
212 break;
213
214 /* sleep for max. 200us */
215 usleep_range(100, 200);
216 } while (time_before(jiffies, timeout));
217
218 /*
219 * we might have scheduled out for long during polling so
220 * check the state again.
221 */
222 if (time_after(jiffies, timeout))
223 err = ufshcd_dme_get(hba,
224 UIC_ARG_MIB_SEL(MPHY_TX_FSM_STATE,
225 UIC_ARG_MPHY_TX_GEN_SEL_INDEX(0)),
226 &tx_fsm_val);
227
228 if (err) {
229 dev_err(hba->dev, "%s: unable to get TX_FSM_STATE, err %d\n",
230 __func__, err);
231 } else if (tx_fsm_val != TX_FSM_HIBERN8) {
232 err = tx_fsm_val;
233 dev_err(hba->dev, "%s: invalid TX_FSM_STATE = %d\n",
234 __func__, err);
235 }
236
237 return err;
238 }
239
ufs_qcom_select_unipro_mode(struct ufs_qcom_host * host)240 static void ufs_qcom_select_unipro_mode(struct ufs_qcom_host *host)
241 {
242 ufshcd_rmwl(host->hba, QUNIPRO_SEL,
243 ufs_qcom_cap_qunipro(host) ? QUNIPRO_SEL : 0,
244 REG_UFS_CFG1);
245 /* make sure above configuration is applied before we return */
246 mb();
247 }
248
249 /*
250 * ufs_qcom_host_reset - reset host controller and PHY
251 */
ufs_qcom_host_reset(struct ufs_hba * hba)252 static int ufs_qcom_host_reset(struct ufs_hba *hba)
253 {
254 int ret = 0;
255 struct ufs_qcom_host *host = ufshcd_get_variant(hba);
256 bool reenable_intr = false;
257
258 if (!host->core_reset) {
259 dev_warn(hba->dev, "%s: reset control not set\n", __func__);
260 goto out;
261 }
262
263 reenable_intr = hba->is_irq_enabled;
264 disable_irq(hba->irq);
265 hba->is_irq_enabled = false;
266
267 ret = reset_control_assert(host->core_reset);
268 if (ret) {
269 dev_err(hba->dev, "%s: core_reset assert failed, err = %d\n",
270 __func__, ret);
271 goto out;
272 }
273
274 /*
275 * The hardware requirement for delay between assert/deassert
276 * is at least 3-4 sleep clock (32.7KHz) cycles, which comes to
277 * ~125us (4/32768). To be on the safe side add 200us delay.
278 */
279 usleep_range(200, 210);
280
281 ret = reset_control_deassert(host->core_reset);
282 if (ret)
283 dev_err(hba->dev, "%s: core_reset deassert failed, err = %d\n",
284 __func__, ret);
285
286 usleep_range(1000, 1100);
287
288 if (reenable_intr) {
289 enable_irq(hba->irq);
290 hba->is_irq_enabled = true;
291 }
292
293 out:
294 return ret;
295 }
296
ufs_qcom_power_up_sequence(struct ufs_hba * hba)297 static int ufs_qcom_power_up_sequence(struct ufs_hba *hba)
298 {
299 struct ufs_qcom_host *host = ufshcd_get_variant(hba);
300 struct phy *phy = host->generic_phy;
301 int ret = 0;
302 bool is_rate_B = (UFS_QCOM_LIMIT_HS_RATE == PA_HS_MODE_B)
303 ? true : false;
304
305 /* Reset UFS Host Controller and PHY */
306 ret = ufs_qcom_host_reset(hba);
307 if (ret)
308 dev_warn(hba->dev, "%s: host reset returned %d\n",
309 __func__, ret);
310
311 if (is_rate_B)
312 phy_set_mode(phy, PHY_MODE_UFS_HS_B);
313
314 /* phy initialization - calibrate the phy */
315 ret = phy_init(phy);
316 if (ret) {
317 dev_err(hba->dev, "%s: phy init failed, ret = %d\n",
318 __func__, ret);
319 goto out;
320 }
321
322 /* power on phy - start serdes and phy's power and clocks */
323 ret = phy_power_on(phy);
324 if (ret) {
325 dev_err(hba->dev, "%s: phy power on failed, ret = %d\n",
326 __func__, ret);
327 goto out_disable_phy;
328 }
329
330 ufs_qcom_select_unipro_mode(host);
331
332 return 0;
333
334 out_disable_phy:
335 phy_exit(phy);
336 out:
337 return ret;
338 }
339
340 /*
341 * The UTP controller has a number of internal clock gating cells (CGCs).
342 * Internal hardware sub-modules within the UTP controller control the CGCs.
343 * Hardware CGCs disable the clock to inactivate UTP sub-modules not involved
344 * in a specific operation, UTP controller CGCs are by default disabled and
345 * this function enables them (after every UFS link startup) to save some power
346 * leakage.
347 */
ufs_qcom_enable_hw_clk_gating(struct ufs_hba * hba)348 static void ufs_qcom_enable_hw_clk_gating(struct ufs_hba *hba)
349 {
350 ufshcd_writel(hba,
351 ufshcd_readl(hba, REG_UFS_CFG2) | REG_UFS_CFG2_CGC_EN_ALL,
352 REG_UFS_CFG2);
353
354 /* Ensure that HW clock gating is enabled before next operations */
355 mb();
356 }
357
ufs_qcom_hce_enable_notify(struct ufs_hba * hba,enum ufs_notify_change_status status)358 static int ufs_qcom_hce_enable_notify(struct ufs_hba *hba,
359 enum ufs_notify_change_status status)
360 {
361 struct ufs_qcom_host *host = ufshcd_get_variant(hba);
362 int err = 0;
363
364 switch (status) {
365 case PRE_CHANGE:
366 ufs_qcom_power_up_sequence(hba);
367 /*
368 * The PHY PLL output is the source of tx/rx lane symbol
369 * clocks, hence, enable the lane clocks only after PHY
370 * is initialized.
371 */
372 err = ufs_qcom_enable_lane_clks(host);
373 break;
374 case POST_CHANGE:
375 /* check if UFS PHY moved from DISABLED to HIBERN8 */
376 err = ufs_qcom_check_hibern8(hba);
377 ufs_qcom_enable_hw_clk_gating(hba);
378 ufs_qcom_ice_enable(host);
379 break;
380 default:
381 dev_err(hba->dev, "%s: invalid status %d\n", __func__, status);
382 err = -EINVAL;
383 break;
384 }
385 return err;
386 }
387
388 /*
389 * Returns zero for success and non-zero in case of a failure
390 */
ufs_qcom_cfg_timers(struct ufs_hba * hba,u32 gear,u32 hs,u32 rate,bool update_link_startup_timer)391 static int ufs_qcom_cfg_timers(struct ufs_hba *hba, u32 gear,
392 u32 hs, u32 rate, bool update_link_startup_timer)
393 {
394 int ret = 0;
395 struct ufs_qcom_host *host = ufshcd_get_variant(hba);
396 struct ufs_clk_info *clki;
397 u32 core_clk_period_in_ns;
398 u32 tx_clk_cycles_per_us = 0;
399 unsigned long core_clk_rate = 0;
400 u32 core_clk_cycles_per_us = 0;
401
402 static u32 pwm_fr_table[][2] = {
403 {UFS_PWM_G1, 0x1},
404 {UFS_PWM_G2, 0x1},
405 {UFS_PWM_G3, 0x1},
406 {UFS_PWM_G4, 0x1},
407 };
408
409 static u32 hs_fr_table_rA[][2] = {
410 {UFS_HS_G1, 0x1F},
411 {UFS_HS_G2, 0x3e},
412 {UFS_HS_G3, 0x7D},
413 };
414
415 static u32 hs_fr_table_rB[][2] = {
416 {UFS_HS_G1, 0x24},
417 {UFS_HS_G2, 0x49},
418 {UFS_HS_G3, 0x92},
419 };
420
421 /*
422 * The Qunipro controller does not use following registers:
423 * SYS1CLK_1US_REG, TX_SYMBOL_CLK_1US_REG, CLK_NS_REG &
424 * UFS_REG_PA_LINK_STARTUP_TIMER
425 * But UTP controller uses SYS1CLK_1US_REG register for Interrupt
426 * Aggregation logic.
427 */
428 if (ufs_qcom_cap_qunipro(host) && !ufshcd_is_intr_aggr_allowed(hba))
429 goto out;
430
431 if (gear == 0) {
432 dev_err(hba->dev, "%s: invalid gear = %d\n", __func__, gear);
433 goto out_error;
434 }
435
436 list_for_each_entry(clki, &hba->clk_list_head, list) {
437 if (!strcmp(clki->name, "core_clk"))
438 core_clk_rate = clk_get_rate(clki->clk);
439 }
440
441 /* If frequency is smaller than 1MHz, set to 1MHz */
442 if (core_clk_rate < DEFAULT_CLK_RATE_HZ)
443 core_clk_rate = DEFAULT_CLK_RATE_HZ;
444
445 core_clk_cycles_per_us = core_clk_rate / USEC_PER_SEC;
446 if (ufshcd_readl(hba, REG_UFS_SYS1CLK_1US) != core_clk_cycles_per_us) {
447 ufshcd_writel(hba, core_clk_cycles_per_us, REG_UFS_SYS1CLK_1US);
448 /*
449 * make sure above write gets applied before we return from
450 * this function.
451 */
452 mb();
453 }
454
455 if (ufs_qcom_cap_qunipro(host))
456 goto out;
457
458 core_clk_period_in_ns = NSEC_PER_SEC / core_clk_rate;
459 core_clk_period_in_ns <<= OFFSET_CLK_NS_REG;
460 core_clk_period_in_ns &= MASK_CLK_NS_REG;
461
462 switch (hs) {
463 case FASTAUTO_MODE:
464 case FAST_MODE:
465 if (rate == PA_HS_MODE_A) {
466 if (gear > ARRAY_SIZE(hs_fr_table_rA)) {
467 dev_err(hba->dev,
468 "%s: index %d exceeds table size %zu\n",
469 __func__, gear,
470 ARRAY_SIZE(hs_fr_table_rA));
471 goto out_error;
472 }
473 tx_clk_cycles_per_us = hs_fr_table_rA[gear-1][1];
474 } else if (rate == PA_HS_MODE_B) {
475 if (gear > ARRAY_SIZE(hs_fr_table_rB)) {
476 dev_err(hba->dev,
477 "%s: index %d exceeds table size %zu\n",
478 __func__, gear,
479 ARRAY_SIZE(hs_fr_table_rB));
480 goto out_error;
481 }
482 tx_clk_cycles_per_us = hs_fr_table_rB[gear-1][1];
483 } else {
484 dev_err(hba->dev, "%s: invalid rate = %d\n",
485 __func__, rate);
486 goto out_error;
487 }
488 break;
489 case SLOWAUTO_MODE:
490 case SLOW_MODE:
491 if (gear > ARRAY_SIZE(pwm_fr_table)) {
492 dev_err(hba->dev,
493 "%s: index %d exceeds table size %zu\n",
494 __func__, gear,
495 ARRAY_SIZE(pwm_fr_table));
496 goto out_error;
497 }
498 tx_clk_cycles_per_us = pwm_fr_table[gear-1][1];
499 break;
500 case UNCHANGED:
501 default:
502 dev_err(hba->dev, "%s: invalid mode = %d\n", __func__, hs);
503 goto out_error;
504 }
505
506 if (ufshcd_readl(hba, REG_UFS_TX_SYMBOL_CLK_NS_US) !=
507 (core_clk_period_in_ns | tx_clk_cycles_per_us)) {
508 /* this register 2 fields shall be written at once */
509 ufshcd_writel(hba, core_clk_period_in_ns | tx_clk_cycles_per_us,
510 REG_UFS_TX_SYMBOL_CLK_NS_US);
511 /*
512 * make sure above write gets applied before we return from
513 * this function.
514 */
515 mb();
516 }
517
518 if (update_link_startup_timer) {
519 ufshcd_writel(hba, ((core_clk_rate / MSEC_PER_SEC) * 100),
520 REG_UFS_PA_LINK_STARTUP_TIMER);
521 /*
522 * make sure that this configuration is applied before
523 * we return
524 */
525 mb();
526 }
527 goto out;
528
529 out_error:
530 ret = -EINVAL;
531 out:
532 return ret;
533 }
534
ufs_qcom_link_startup_notify(struct ufs_hba * hba,enum ufs_notify_change_status status)535 static int ufs_qcom_link_startup_notify(struct ufs_hba *hba,
536 enum ufs_notify_change_status status)
537 {
538 int err = 0;
539 struct ufs_qcom_host *host = ufshcd_get_variant(hba);
540
541 switch (status) {
542 case PRE_CHANGE:
543 if (ufs_qcom_cfg_timers(hba, UFS_PWM_G1, SLOWAUTO_MODE,
544 0, true)) {
545 dev_err(hba->dev, "%s: ufs_qcom_cfg_timers() failed\n",
546 __func__);
547 err = -EINVAL;
548 goto out;
549 }
550
551 if (ufs_qcom_cap_qunipro(host))
552 /*
553 * set unipro core clock cycles to 150 & clear clock
554 * divider
555 */
556 err = ufs_qcom_set_dme_vs_core_clk_ctrl_clear_div(hba,
557 150);
558
559 /*
560 * Some UFS devices (and may be host) have issues if LCC is
561 * enabled. So we are setting PA_Local_TX_LCC_Enable to 0
562 * before link startup which will make sure that both host
563 * and device TX LCC are disabled once link startup is
564 * completed.
565 */
566 if (ufshcd_get_local_unipro_ver(hba) != UFS_UNIPRO_VER_1_41)
567 err = ufshcd_disable_host_tx_lcc(hba);
568
569 break;
570 case POST_CHANGE:
571 ufs_qcom_link_startup_post_change(hba);
572 break;
573 default:
574 break;
575 }
576
577 out:
578 return err;
579 }
580
ufs_qcom_device_reset_ctrl(struct ufs_hba * hba,bool asserted)581 static void ufs_qcom_device_reset_ctrl(struct ufs_hba *hba, bool asserted)
582 {
583 struct ufs_qcom_host *host = ufshcd_get_variant(hba);
584
585 /* reset gpio is optional */
586 if (!host->device_reset)
587 return;
588
589 gpiod_set_value_cansleep(host->device_reset, asserted);
590 }
591
ufs_qcom_suspend(struct ufs_hba * hba,enum ufs_pm_op pm_op)592 static int ufs_qcom_suspend(struct ufs_hba *hba, enum ufs_pm_op pm_op)
593 {
594 struct ufs_qcom_host *host = ufshcd_get_variant(hba);
595 struct phy *phy = host->generic_phy;
596
597 if (ufs_qcom_is_link_off(hba)) {
598 /*
599 * Disable the tx/rx lane symbol clocks before PHY is
600 * powered down as the PLL source should be disabled
601 * after downstream clocks are disabled.
602 */
603 ufs_qcom_disable_lane_clks(host);
604 phy_power_off(phy);
605
606 /* reset the connected UFS device during power down */
607 ufs_qcom_device_reset_ctrl(hba, true);
608
609 } else if (!ufs_qcom_is_link_active(hba)) {
610 ufs_qcom_disable_lane_clks(host);
611 }
612
613 return 0;
614 }
615
ufs_qcom_resume(struct ufs_hba * hba,enum ufs_pm_op pm_op)616 static int ufs_qcom_resume(struct ufs_hba *hba, enum ufs_pm_op pm_op)
617 {
618 struct ufs_qcom_host *host = ufshcd_get_variant(hba);
619 struct phy *phy = host->generic_phy;
620 int err;
621
622 if (ufs_qcom_is_link_off(hba)) {
623 err = phy_power_on(phy);
624 if (err) {
625 dev_err(hba->dev, "%s: failed PHY power on: %d\n",
626 __func__, err);
627 return err;
628 }
629
630 err = ufs_qcom_enable_lane_clks(host);
631 if (err)
632 return err;
633
634 } else if (!ufs_qcom_is_link_active(hba)) {
635 err = ufs_qcom_enable_lane_clks(host);
636 if (err)
637 return err;
638 }
639
640 return ufs_qcom_ice_resume(host);
641 }
642
ufs_qcom_dev_ref_clk_ctrl(struct ufs_qcom_host * host,bool enable)643 static void ufs_qcom_dev_ref_clk_ctrl(struct ufs_qcom_host *host, bool enable)
644 {
645 if (host->dev_ref_clk_ctrl_mmio &&
646 (enable ^ host->is_dev_ref_clk_enabled)) {
647 u32 temp = readl_relaxed(host->dev_ref_clk_ctrl_mmio);
648
649 if (enable)
650 temp |= host->dev_ref_clk_en_mask;
651 else
652 temp &= ~host->dev_ref_clk_en_mask;
653
654 /*
655 * If we are here to disable this clock it might be immediately
656 * after entering into hibern8 in which case we need to make
657 * sure that device ref_clk is active for specific time after
658 * hibern8 enter.
659 */
660 if (!enable) {
661 unsigned long gating_wait;
662
663 gating_wait = host->hba->dev_info.clk_gating_wait_us;
664 if (!gating_wait) {
665 udelay(1);
666 } else {
667 /*
668 * bRefClkGatingWaitTime defines the minimum
669 * time for which the reference clock is
670 * required by device during transition from
671 * HS-MODE to LS-MODE or HIBERN8 state. Give it
672 * more delay to be on the safe side.
673 */
674 gating_wait += 10;
675 usleep_range(gating_wait, gating_wait + 10);
676 }
677 }
678
679 writel_relaxed(temp, host->dev_ref_clk_ctrl_mmio);
680
681 /*
682 * Make sure the write to ref_clk reaches the destination and
683 * not stored in a Write Buffer (WB).
684 */
685 readl(host->dev_ref_clk_ctrl_mmio);
686
687 /*
688 * If we call hibern8 exit after this, we need to make sure that
689 * device ref_clk is stable for at least 1us before the hibern8
690 * exit command.
691 */
692 if (enable)
693 udelay(1);
694
695 host->is_dev_ref_clk_enabled = enable;
696 }
697 }
698
ufs_qcom_pwr_change_notify(struct ufs_hba * hba,enum ufs_notify_change_status status,struct ufs_pa_layer_attr * dev_max_params,struct ufs_pa_layer_attr * dev_req_params)699 static int ufs_qcom_pwr_change_notify(struct ufs_hba *hba,
700 enum ufs_notify_change_status status,
701 struct ufs_pa_layer_attr *dev_max_params,
702 struct ufs_pa_layer_attr *dev_req_params)
703 {
704 struct ufs_qcom_host *host = ufshcd_get_variant(hba);
705 struct ufs_dev_params ufs_qcom_cap;
706 int ret = 0;
707
708 if (!dev_req_params) {
709 pr_err("%s: incoming dev_req_params is NULL\n", __func__);
710 ret = -EINVAL;
711 goto out;
712 }
713
714 switch (status) {
715 case PRE_CHANGE:
716 ufs_qcom_cap.tx_lanes = UFS_QCOM_LIMIT_NUM_LANES_TX;
717 ufs_qcom_cap.rx_lanes = UFS_QCOM_LIMIT_NUM_LANES_RX;
718 ufs_qcom_cap.hs_rx_gear = UFS_QCOM_LIMIT_HSGEAR_RX;
719 ufs_qcom_cap.hs_tx_gear = UFS_QCOM_LIMIT_HSGEAR_TX;
720 ufs_qcom_cap.pwm_rx_gear = UFS_QCOM_LIMIT_PWMGEAR_RX;
721 ufs_qcom_cap.pwm_tx_gear = UFS_QCOM_LIMIT_PWMGEAR_TX;
722 ufs_qcom_cap.rx_pwr_pwm = UFS_QCOM_LIMIT_RX_PWR_PWM;
723 ufs_qcom_cap.tx_pwr_pwm = UFS_QCOM_LIMIT_TX_PWR_PWM;
724 ufs_qcom_cap.rx_pwr_hs = UFS_QCOM_LIMIT_RX_PWR_HS;
725 ufs_qcom_cap.tx_pwr_hs = UFS_QCOM_LIMIT_TX_PWR_HS;
726 ufs_qcom_cap.hs_rate = UFS_QCOM_LIMIT_HS_RATE;
727 ufs_qcom_cap.desired_working_mode =
728 UFS_QCOM_LIMIT_DESIRED_MODE;
729
730 if (host->hw_ver.major == 0x1) {
731 /*
732 * HS-G3 operations may not reliably work on legacy QCOM
733 * UFS host controller hardware even though capability
734 * exchange during link startup phase may end up
735 * negotiating maximum supported gear as G3.
736 * Hence downgrade the maximum supported gear to HS-G2.
737 */
738 if (ufs_qcom_cap.hs_tx_gear > UFS_HS_G2)
739 ufs_qcom_cap.hs_tx_gear = UFS_HS_G2;
740 if (ufs_qcom_cap.hs_rx_gear > UFS_HS_G2)
741 ufs_qcom_cap.hs_rx_gear = UFS_HS_G2;
742 }
743
744 ret = ufshcd_get_pwr_dev_param(&ufs_qcom_cap,
745 dev_max_params,
746 dev_req_params);
747 if (ret) {
748 pr_err("%s: failed to determine capabilities\n",
749 __func__);
750 goto out;
751 }
752
753 /* enable the device ref clock before changing to HS mode */
754 if (!ufshcd_is_hs_mode(&hba->pwr_info) &&
755 ufshcd_is_hs_mode(dev_req_params))
756 ufs_qcom_dev_ref_clk_ctrl(host, true);
757
758 if (host->hw_ver.major >= 0x4) {
759 if (dev_req_params->gear_tx == UFS_HS_G4) {
760 /* INITIAL ADAPT */
761 ufshcd_dme_set(hba,
762 UIC_ARG_MIB(PA_TXHSADAPTTYPE),
763 PA_INITIAL_ADAPT);
764 } else {
765 /* NO ADAPT */
766 ufshcd_dme_set(hba,
767 UIC_ARG_MIB(PA_TXHSADAPTTYPE),
768 PA_NO_ADAPT);
769 }
770 }
771 break;
772 case POST_CHANGE:
773 if (ufs_qcom_cfg_timers(hba, dev_req_params->gear_rx,
774 dev_req_params->pwr_rx,
775 dev_req_params->hs_rate, false)) {
776 dev_err(hba->dev, "%s: ufs_qcom_cfg_timers() failed\n",
777 __func__);
778 /*
779 * we return error code at the end of the routine,
780 * but continue to configure UFS_PHY_TX_LANE_ENABLE
781 * and bus voting as usual
782 */
783 ret = -EINVAL;
784 }
785
786 /* cache the power mode parameters to use internally */
787 memcpy(&host->dev_req_params,
788 dev_req_params, sizeof(*dev_req_params));
789
790 /* disable the device ref clock if entered PWM mode */
791 if (ufshcd_is_hs_mode(&hba->pwr_info) &&
792 !ufshcd_is_hs_mode(dev_req_params))
793 ufs_qcom_dev_ref_clk_ctrl(host, false);
794 break;
795 default:
796 ret = -EINVAL;
797 break;
798 }
799 out:
800 return ret;
801 }
802
ufs_qcom_quirk_host_pa_saveconfigtime(struct ufs_hba * hba)803 static int ufs_qcom_quirk_host_pa_saveconfigtime(struct ufs_hba *hba)
804 {
805 int err;
806 u32 pa_vs_config_reg1;
807
808 err = ufshcd_dme_get(hba, UIC_ARG_MIB(PA_VS_CONFIG_REG1),
809 &pa_vs_config_reg1);
810 if (err)
811 goto out;
812
813 /* Allow extension of MSB bits of PA_SaveConfigTime attribute */
814 err = ufshcd_dme_set(hba, UIC_ARG_MIB(PA_VS_CONFIG_REG1),
815 (pa_vs_config_reg1 | (1 << 12)));
816
817 out:
818 return err;
819 }
820
ufs_qcom_apply_dev_quirks(struct ufs_hba * hba)821 static int ufs_qcom_apply_dev_quirks(struct ufs_hba *hba)
822 {
823 int err = 0;
824
825 if (hba->dev_quirks & UFS_DEVICE_QUIRK_HOST_PA_SAVECONFIGTIME)
826 err = ufs_qcom_quirk_host_pa_saveconfigtime(hba);
827
828 if (hba->dev_info.wmanufacturerid == UFS_VENDOR_WDC)
829 hba->dev_quirks |= UFS_DEVICE_QUIRK_HOST_PA_TACTIVATE;
830
831 return err;
832 }
833
ufs_qcom_get_ufs_hci_version(struct ufs_hba * hba)834 static u32 ufs_qcom_get_ufs_hci_version(struct ufs_hba *hba)
835 {
836 struct ufs_qcom_host *host = ufshcd_get_variant(hba);
837
838 if (host->hw_ver.major == 0x1)
839 return ufshci_version(1, 1);
840 else
841 return ufshci_version(2, 0);
842 }
843
844 /**
845 * ufs_qcom_advertise_quirks - advertise the known QCOM UFS controller quirks
846 * @hba: host controller instance
847 *
848 * QCOM UFS host controller might have some non standard behaviours (quirks)
849 * than what is specified by UFSHCI specification. Advertise all such
850 * quirks to standard UFS host controller driver so standard takes them into
851 * account.
852 */
ufs_qcom_advertise_quirks(struct ufs_hba * hba)853 static void ufs_qcom_advertise_quirks(struct ufs_hba *hba)
854 {
855 struct ufs_qcom_host *host = ufshcd_get_variant(hba);
856
857 if (host->hw_ver.major == 0x01) {
858 hba->quirks |= UFSHCD_QUIRK_DELAY_BEFORE_DME_CMDS
859 | UFSHCD_QUIRK_BROKEN_PA_RXHSUNTERMCAP
860 | UFSHCD_QUIRK_DME_PEER_ACCESS_AUTO_MODE;
861
862 if (host->hw_ver.minor == 0x0001 && host->hw_ver.step == 0x0001)
863 hba->quirks |= UFSHCD_QUIRK_BROKEN_INTR_AGGR;
864
865 hba->quirks |= UFSHCD_QUIRK_BROKEN_LCC;
866 }
867
868 if (host->hw_ver.major == 0x2) {
869 hba->quirks |= UFSHCD_QUIRK_BROKEN_UFS_HCI_VERSION;
870
871 if (!ufs_qcom_cap_qunipro(host))
872 /* Legacy UniPro mode still need following quirks */
873 hba->quirks |= (UFSHCD_QUIRK_DELAY_BEFORE_DME_CMDS
874 | UFSHCD_QUIRK_DME_PEER_ACCESS_AUTO_MODE
875 | UFSHCD_QUIRK_BROKEN_PA_RXHSUNTERMCAP);
876 }
877 }
878
ufs_qcom_set_caps(struct ufs_hba * hba)879 static void ufs_qcom_set_caps(struct ufs_hba *hba)
880 {
881 struct ufs_qcom_host *host = ufshcd_get_variant(hba);
882
883 hba->caps |= UFSHCD_CAP_CLK_GATING | UFSHCD_CAP_HIBERN8_WITH_CLK_GATING;
884 hba->caps |= UFSHCD_CAP_CLK_SCALING;
885 hba->caps |= UFSHCD_CAP_AUTO_BKOPS_SUSPEND;
886 hba->caps |= UFSHCD_CAP_WB_EN;
887 hba->caps |= UFSHCD_CAP_CRYPTO;
888
889 if (host->hw_ver.major >= 0x2) {
890 host->caps = UFS_QCOM_CAP_QUNIPRO |
891 UFS_QCOM_CAP_RETAIN_SEC_CFG_AFTER_PWR_COLLAPSE;
892 }
893 }
894
895 /**
896 * ufs_qcom_setup_clocks - enables/disable clocks
897 * @hba: host controller instance
898 * @on: If true, enable clocks else disable them.
899 * @status: PRE_CHANGE or POST_CHANGE notify
900 *
901 * Returns 0 on success, non-zero on failure.
902 */
ufs_qcom_setup_clocks(struct ufs_hba * hba,bool on,enum ufs_notify_change_status status)903 static int ufs_qcom_setup_clocks(struct ufs_hba *hba, bool on,
904 enum ufs_notify_change_status status)
905 {
906 struct ufs_qcom_host *host = ufshcd_get_variant(hba);
907 int err = 0;
908
909 /*
910 * In case ufs_qcom_init() is not yet done, simply ignore.
911 * This ufs_qcom_setup_clocks() shall be called from
912 * ufs_qcom_init() after init is done.
913 */
914 if (!host)
915 return 0;
916
917 switch (status) {
918 case PRE_CHANGE:
919 if (!on) {
920 if (!ufs_qcom_is_link_active(hba)) {
921 /* disable device ref_clk */
922 ufs_qcom_dev_ref_clk_ctrl(host, false);
923 }
924 }
925 break;
926 case POST_CHANGE:
927 if (on) {
928 /* enable the device ref clock for HS mode*/
929 if (ufshcd_is_hs_mode(&hba->pwr_info))
930 ufs_qcom_dev_ref_clk_ctrl(host, true);
931 }
932 break;
933 }
934
935 return err;
936 }
937
938 static int
ufs_qcom_reset_assert(struct reset_controller_dev * rcdev,unsigned long id)939 ufs_qcom_reset_assert(struct reset_controller_dev *rcdev, unsigned long id)
940 {
941 struct ufs_qcom_host *host = rcdev_to_ufs_host(rcdev);
942
943 /* Currently this code only knows about a single reset. */
944 WARN_ON(id);
945 ufs_qcom_assert_reset(host->hba);
946 /* provide 1ms delay to let the reset pulse propagate. */
947 usleep_range(1000, 1100);
948 return 0;
949 }
950
951 static int
ufs_qcom_reset_deassert(struct reset_controller_dev * rcdev,unsigned long id)952 ufs_qcom_reset_deassert(struct reset_controller_dev *rcdev, unsigned long id)
953 {
954 struct ufs_qcom_host *host = rcdev_to_ufs_host(rcdev);
955
956 /* Currently this code only knows about a single reset. */
957 WARN_ON(id);
958 ufs_qcom_deassert_reset(host->hba);
959
960 /*
961 * after reset deassertion, phy will need all ref clocks,
962 * voltage, current to settle down before starting serdes.
963 */
964 usleep_range(1000, 1100);
965 return 0;
966 }
967
968 static const struct reset_control_ops ufs_qcom_reset_ops = {
969 .assert = ufs_qcom_reset_assert,
970 .deassert = ufs_qcom_reset_deassert,
971 };
972
973 #define ANDROID_BOOT_DEV_MAX 30
974 static char android_boot_dev[ANDROID_BOOT_DEV_MAX];
975
976 #ifndef MODULE
get_android_boot_dev(char * str)977 static int __init get_android_boot_dev(char *str)
978 {
979 strlcpy(android_boot_dev, str, ANDROID_BOOT_DEV_MAX);
980 return 1;
981 }
982 __setup("androidboot.bootdevice=", get_android_boot_dev);
983 #endif
984
985 /**
986 * ufs_qcom_init - bind phy with controller
987 * @hba: host controller instance
988 *
989 * Binds PHY with controller and powers up PHY enabling clocks
990 * and regulators.
991 *
992 * Returns -EPROBE_DEFER if binding fails, returns negative error
993 * on phy power up failure and returns zero on success.
994 */
ufs_qcom_init(struct ufs_hba * hba)995 static int ufs_qcom_init(struct ufs_hba *hba)
996 {
997 int err;
998 struct device *dev = hba->dev;
999 struct platform_device *pdev = to_platform_device(dev);
1000 struct ufs_qcom_host *host;
1001 struct resource *res;
1002
1003 if (strlen(android_boot_dev) && strcmp(android_boot_dev, dev_name(dev)))
1004 return -ENODEV;
1005
1006 host = devm_kzalloc(dev, sizeof(*host), GFP_KERNEL);
1007 if (!host) {
1008 err = -ENOMEM;
1009 dev_err(dev, "%s: no memory for qcom ufs host\n", __func__);
1010 goto out;
1011 }
1012
1013 /* Make a two way bind between the qcom host and the hba */
1014 host->hba = hba;
1015 ufshcd_set_variant(hba, host);
1016
1017 /* Setup the reset control of HCI */
1018 host->core_reset = devm_reset_control_get(hba->dev, "rst");
1019 if (IS_ERR(host->core_reset)) {
1020 err = PTR_ERR(host->core_reset);
1021 dev_warn(dev, "Failed to get reset control %d\n", err);
1022 host->core_reset = NULL;
1023 err = 0;
1024 }
1025
1026 /* Fire up the reset controller. Failure here is non-fatal. */
1027 host->rcdev.of_node = dev->of_node;
1028 host->rcdev.ops = &ufs_qcom_reset_ops;
1029 host->rcdev.owner = dev->driver->owner;
1030 host->rcdev.nr_resets = 1;
1031 err = devm_reset_controller_register(dev, &host->rcdev);
1032 if (err) {
1033 dev_warn(dev, "Failed to register reset controller\n");
1034 err = 0;
1035 }
1036
1037 /*
1038 * voting/devoting device ref_clk source is time consuming hence
1039 * skip devoting it during aggressive clock gating. This clock
1040 * will still be gated off during runtime suspend.
1041 */
1042 host->generic_phy = devm_phy_get(dev, "ufsphy");
1043
1044 if (host->generic_phy == ERR_PTR(-EPROBE_DEFER)) {
1045 /*
1046 * UFS driver might be probed before the phy driver does.
1047 * In that case we would like to return EPROBE_DEFER code.
1048 */
1049 err = -EPROBE_DEFER;
1050 dev_warn(dev, "%s: required phy device. hasn't probed yet. err = %d\n",
1051 __func__, err);
1052 goto out_variant_clear;
1053 } else if (IS_ERR(host->generic_phy)) {
1054 if (has_acpi_companion(dev)) {
1055 host->generic_phy = NULL;
1056 } else {
1057 err = PTR_ERR(host->generic_phy);
1058 dev_err(dev, "%s: PHY get failed %d\n", __func__, err);
1059 goto out_variant_clear;
1060 }
1061 }
1062
1063 host->device_reset = devm_gpiod_get_optional(dev, "reset",
1064 GPIOD_OUT_HIGH);
1065 if (IS_ERR(host->device_reset)) {
1066 err = PTR_ERR(host->device_reset);
1067 if (err != -EPROBE_DEFER)
1068 dev_err(dev, "failed to acquire reset gpio: %d\n", err);
1069 goto out_variant_clear;
1070 }
1071
1072 ufs_qcom_get_controller_revision(hba, &host->hw_ver.major,
1073 &host->hw_ver.minor, &host->hw_ver.step);
1074
1075 /*
1076 * for newer controllers, device reference clock control bit has
1077 * moved inside UFS controller register address space itself.
1078 */
1079 if (host->hw_ver.major >= 0x02) {
1080 host->dev_ref_clk_ctrl_mmio = hba->mmio_base + REG_UFS_CFG1;
1081 host->dev_ref_clk_en_mask = BIT(26);
1082 } else {
1083 /* "dev_ref_clk_ctrl_mem" is optional resource */
1084 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
1085 "dev_ref_clk_ctrl_mem");
1086 if (res) {
1087 host->dev_ref_clk_ctrl_mmio =
1088 devm_ioremap_resource(dev, res);
1089 if (IS_ERR(host->dev_ref_clk_ctrl_mmio)) {
1090 dev_warn(dev,
1091 "%s: could not map dev_ref_clk_ctrl_mmio, err %ld\n",
1092 __func__,
1093 PTR_ERR(host->dev_ref_clk_ctrl_mmio));
1094 host->dev_ref_clk_ctrl_mmio = NULL;
1095 }
1096 host->dev_ref_clk_en_mask = BIT(5);
1097 }
1098 }
1099
1100 err = ufs_qcom_init_lane_clks(host);
1101 if (err)
1102 goto out_variant_clear;
1103
1104 ufs_qcom_set_caps(hba);
1105 ufs_qcom_advertise_quirks(hba);
1106
1107 err = ufs_qcom_ice_init(host);
1108 if (err)
1109 goto out_variant_clear;
1110
1111 ufs_qcom_setup_clocks(hba, true, POST_CHANGE);
1112
1113 if (hba->dev->id < MAX_UFS_QCOM_HOSTS)
1114 ufs_qcom_hosts[hba->dev->id] = host;
1115
1116 host->dbg_print_en |= UFS_QCOM_DEFAULT_DBG_PRINT_EN;
1117 ufs_qcom_get_default_testbus_cfg(host);
1118 err = ufs_qcom_testbus_config(host);
1119 if (err) {
1120 dev_warn(dev, "%s: failed to configure the testbus %d\n",
1121 __func__, err);
1122 err = 0;
1123 }
1124
1125 goto out;
1126
1127 out_variant_clear:
1128 ufshcd_set_variant(hba, NULL);
1129 out:
1130 return err;
1131 }
1132
ufs_qcom_exit(struct ufs_hba * hba)1133 static void ufs_qcom_exit(struct ufs_hba *hba)
1134 {
1135 struct ufs_qcom_host *host = ufshcd_get_variant(hba);
1136
1137 ufs_qcom_disable_lane_clks(host);
1138 phy_power_off(host->generic_phy);
1139 phy_exit(host->generic_phy);
1140 }
1141
ufs_qcom_set_dme_vs_core_clk_ctrl_clear_div(struct ufs_hba * hba,u32 clk_cycles)1142 static int ufs_qcom_set_dme_vs_core_clk_ctrl_clear_div(struct ufs_hba *hba,
1143 u32 clk_cycles)
1144 {
1145 int err;
1146 u32 core_clk_ctrl_reg;
1147
1148 if (clk_cycles > DME_VS_CORE_CLK_CTRL_MAX_CORE_CLK_1US_CYCLES_MASK)
1149 return -EINVAL;
1150
1151 err = ufshcd_dme_get(hba,
1152 UIC_ARG_MIB(DME_VS_CORE_CLK_CTRL),
1153 &core_clk_ctrl_reg);
1154 if (err)
1155 goto out;
1156
1157 core_clk_ctrl_reg &= ~DME_VS_CORE_CLK_CTRL_MAX_CORE_CLK_1US_CYCLES_MASK;
1158 core_clk_ctrl_reg |= clk_cycles;
1159
1160 /* Clear CORE_CLK_DIV_EN */
1161 core_clk_ctrl_reg &= ~DME_VS_CORE_CLK_CTRL_CORE_CLK_DIV_EN_BIT;
1162
1163 err = ufshcd_dme_set(hba,
1164 UIC_ARG_MIB(DME_VS_CORE_CLK_CTRL),
1165 core_clk_ctrl_reg);
1166 out:
1167 return err;
1168 }
1169
ufs_qcom_clk_scale_up_pre_change(struct ufs_hba * hba)1170 static int ufs_qcom_clk_scale_up_pre_change(struct ufs_hba *hba)
1171 {
1172 /* nothing to do as of now */
1173 return 0;
1174 }
1175
ufs_qcom_clk_scale_up_post_change(struct ufs_hba * hba)1176 static int ufs_qcom_clk_scale_up_post_change(struct ufs_hba *hba)
1177 {
1178 struct ufs_qcom_host *host = ufshcd_get_variant(hba);
1179
1180 if (!ufs_qcom_cap_qunipro(host))
1181 return 0;
1182
1183 /* set unipro core clock cycles to 150 and clear clock divider */
1184 return ufs_qcom_set_dme_vs_core_clk_ctrl_clear_div(hba, 150);
1185 }
1186
ufs_qcom_clk_scale_down_pre_change(struct ufs_hba * hba)1187 static int ufs_qcom_clk_scale_down_pre_change(struct ufs_hba *hba)
1188 {
1189 struct ufs_qcom_host *host = ufshcd_get_variant(hba);
1190 int err;
1191 u32 core_clk_ctrl_reg;
1192
1193 if (!ufs_qcom_cap_qunipro(host))
1194 return 0;
1195
1196 err = ufshcd_dme_get(hba,
1197 UIC_ARG_MIB(DME_VS_CORE_CLK_CTRL),
1198 &core_clk_ctrl_reg);
1199
1200 /* make sure CORE_CLK_DIV_EN is cleared */
1201 if (!err &&
1202 (core_clk_ctrl_reg & DME_VS_CORE_CLK_CTRL_CORE_CLK_DIV_EN_BIT)) {
1203 core_clk_ctrl_reg &= ~DME_VS_CORE_CLK_CTRL_CORE_CLK_DIV_EN_BIT;
1204 err = ufshcd_dme_set(hba,
1205 UIC_ARG_MIB(DME_VS_CORE_CLK_CTRL),
1206 core_clk_ctrl_reg);
1207 }
1208
1209 return err;
1210 }
1211
ufs_qcom_clk_scale_down_post_change(struct ufs_hba * hba)1212 static int ufs_qcom_clk_scale_down_post_change(struct ufs_hba *hba)
1213 {
1214 struct ufs_qcom_host *host = ufshcd_get_variant(hba);
1215
1216 if (!ufs_qcom_cap_qunipro(host))
1217 return 0;
1218
1219 /* set unipro core clock cycles to 75 and clear clock divider */
1220 return ufs_qcom_set_dme_vs_core_clk_ctrl_clear_div(hba, 75);
1221 }
1222
ufs_qcom_clk_scale_notify(struct ufs_hba * hba,bool scale_up,enum ufs_notify_change_status status)1223 static int ufs_qcom_clk_scale_notify(struct ufs_hba *hba,
1224 bool scale_up, enum ufs_notify_change_status status)
1225 {
1226 struct ufs_qcom_host *host = ufshcd_get_variant(hba);
1227 struct ufs_pa_layer_attr *dev_req_params = &host->dev_req_params;
1228 int err = 0;
1229
1230 if (status == PRE_CHANGE) {
1231 err = ufshcd_uic_hibern8_enter(hba);
1232 if (err)
1233 return err;
1234 if (scale_up)
1235 err = ufs_qcom_clk_scale_up_pre_change(hba);
1236 else
1237 err = ufs_qcom_clk_scale_down_pre_change(hba);
1238 if (err)
1239 ufshcd_uic_hibern8_exit(hba);
1240
1241 } else {
1242 if (scale_up)
1243 err = ufs_qcom_clk_scale_up_post_change(hba);
1244 else
1245 err = ufs_qcom_clk_scale_down_post_change(hba);
1246
1247
1248 if (err || !dev_req_params) {
1249 ufshcd_uic_hibern8_exit(hba);
1250 goto out;
1251 }
1252
1253 ufs_qcom_cfg_timers(hba,
1254 dev_req_params->gear_rx,
1255 dev_req_params->pwr_rx,
1256 dev_req_params->hs_rate,
1257 false);
1258 ufshcd_uic_hibern8_exit(hba);
1259 }
1260
1261 out:
1262 return err;
1263 }
1264
ufs_qcom_print_hw_debug_reg_all(struct ufs_hba * hba,void * priv,void (* print_fn)(struct ufs_hba * hba,int offset,int num_regs,const char * str,void * priv))1265 static void ufs_qcom_print_hw_debug_reg_all(struct ufs_hba *hba,
1266 void *priv, void (*print_fn)(struct ufs_hba *hba,
1267 int offset, int num_regs, const char *str, void *priv))
1268 {
1269 u32 reg;
1270 struct ufs_qcom_host *host;
1271
1272 if (unlikely(!hba)) {
1273 pr_err("%s: hba is NULL\n", __func__);
1274 return;
1275 }
1276 if (unlikely(!print_fn)) {
1277 dev_err(hba->dev, "%s: print_fn is NULL\n", __func__);
1278 return;
1279 }
1280
1281 host = ufshcd_get_variant(hba);
1282 if (!(host->dbg_print_en & UFS_QCOM_DBG_PRINT_REGS_EN))
1283 return;
1284
1285 reg = ufs_qcom_get_debug_reg_offset(host, UFS_UFS_DBG_RD_REG_OCSC);
1286 print_fn(hba, reg, 44, "UFS_UFS_DBG_RD_REG_OCSC ", priv);
1287
1288 reg = ufshcd_readl(hba, REG_UFS_CFG1);
1289 reg |= UTP_DBG_RAMS_EN;
1290 ufshcd_writel(hba, reg, REG_UFS_CFG1);
1291
1292 reg = ufs_qcom_get_debug_reg_offset(host, UFS_UFS_DBG_RD_EDTL_RAM);
1293 print_fn(hba, reg, 32, "UFS_UFS_DBG_RD_EDTL_RAM ", priv);
1294
1295 reg = ufs_qcom_get_debug_reg_offset(host, UFS_UFS_DBG_RD_DESC_RAM);
1296 print_fn(hba, reg, 128, "UFS_UFS_DBG_RD_DESC_RAM ", priv);
1297
1298 reg = ufs_qcom_get_debug_reg_offset(host, UFS_UFS_DBG_RD_PRDT_RAM);
1299 print_fn(hba, reg, 64, "UFS_UFS_DBG_RD_PRDT_RAM ", priv);
1300
1301 /* clear bit 17 - UTP_DBG_RAMS_EN */
1302 ufshcd_rmwl(hba, UTP_DBG_RAMS_EN, 0, REG_UFS_CFG1);
1303
1304 reg = ufs_qcom_get_debug_reg_offset(host, UFS_DBG_RD_REG_UAWM);
1305 print_fn(hba, reg, 4, "UFS_DBG_RD_REG_UAWM ", priv);
1306
1307 reg = ufs_qcom_get_debug_reg_offset(host, UFS_DBG_RD_REG_UARM);
1308 print_fn(hba, reg, 4, "UFS_DBG_RD_REG_UARM ", priv);
1309
1310 reg = ufs_qcom_get_debug_reg_offset(host, UFS_DBG_RD_REG_TXUC);
1311 print_fn(hba, reg, 48, "UFS_DBG_RD_REG_TXUC ", priv);
1312
1313 reg = ufs_qcom_get_debug_reg_offset(host, UFS_DBG_RD_REG_RXUC);
1314 print_fn(hba, reg, 27, "UFS_DBG_RD_REG_RXUC ", priv);
1315
1316 reg = ufs_qcom_get_debug_reg_offset(host, UFS_DBG_RD_REG_DFC);
1317 print_fn(hba, reg, 19, "UFS_DBG_RD_REG_DFC ", priv);
1318
1319 reg = ufs_qcom_get_debug_reg_offset(host, UFS_DBG_RD_REG_TRLUT);
1320 print_fn(hba, reg, 34, "UFS_DBG_RD_REG_TRLUT ", priv);
1321
1322 reg = ufs_qcom_get_debug_reg_offset(host, UFS_DBG_RD_REG_TMRLUT);
1323 print_fn(hba, reg, 9, "UFS_DBG_RD_REG_TMRLUT ", priv);
1324 }
1325
ufs_qcom_enable_test_bus(struct ufs_qcom_host * host)1326 static void ufs_qcom_enable_test_bus(struct ufs_qcom_host *host)
1327 {
1328 if (host->dbg_print_en & UFS_QCOM_DBG_PRINT_TEST_BUS_EN) {
1329 ufshcd_rmwl(host->hba, UFS_REG_TEST_BUS_EN,
1330 UFS_REG_TEST_BUS_EN, REG_UFS_CFG1);
1331 ufshcd_rmwl(host->hba, TEST_BUS_EN, TEST_BUS_EN, REG_UFS_CFG1);
1332 } else {
1333 ufshcd_rmwl(host->hba, UFS_REG_TEST_BUS_EN, 0, REG_UFS_CFG1);
1334 ufshcd_rmwl(host->hba, TEST_BUS_EN, 0, REG_UFS_CFG1);
1335 }
1336 }
1337
ufs_qcom_get_default_testbus_cfg(struct ufs_qcom_host * host)1338 static void ufs_qcom_get_default_testbus_cfg(struct ufs_qcom_host *host)
1339 {
1340 /* provide a legal default configuration */
1341 host->testbus.select_major = TSTBUS_UNIPRO;
1342 host->testbus.select_minor = 37;
1343 }
1344
ufs_qcom_testbus_cfg_is_ok(struct ufs_qcom_host * host)1345 static bool ufs_qcom_testbus_cfg_is_ok(struct ufs_qcom_host *host)
1346 {
1347 if (host->testbus.select_major >= TSTBUS_MAX) {
1348 dev_err(host->hba->dev,
1349 "%s: UFS_CFG1[TEST_BUS_SEL} may not equal 0x%05X\n",
1350 __func__, host->testbus.select_major);
1351 return false;
1352 }
1353
1354 return true;
1355 }
1356
ufs_qcom_testbus_config(struct ufs_qcom_host * host)1357 int ufs_qcom_testbus_config(struct ufs_qcom_host *host)
1358 {
1359 int reg;
1360 int offset;
1361 u32 mask = TEST_BUS_SUB_SEL_MASK;
1362
1363 if (!host)
1364 return -EINVAL;
1365
1366 if (!ufs_qcom_testbus_cfg_is_ok(host))
1367 return -EPERM;
1368
1369 switch (host->testbus.select_major) {
1370 case TSTBUS_UAWM:
1371 reg = UFS_TEST_BUS_CTRL_0;
1372 offset = 24;
1373 break;
1374 case TSTBUS_UARM:
1375 reg = UFS_TEST_BUS_CTRL_0;
1376 offset = 16;
1377 break;
1378 case TSTBUS_TXUC:
1379 reg = UFS_TEST_BUS_CTRL_0;
1380 offset = 8;
1381 break;
1382 case TSTBUS_RXUC:
1383 reg = UFS_TEST_BUS_CTRL_0;
1384 offset = 0;
1385 break;
1386 case TSTBUS_DFC:
1387 reg = UFS_TEST_BUS_CTRL_1;
1388 offset = 24;
1389 break;
1390 case TSTBUS_TRLUT:
1391 reg = UFS_TEST_BUS_CTRL_1;
1392 offset = 16;
1393 break;
1394 case TSTBUS_TMRLUT:
1395 reg = UFS_TEST_BUS_CTRL_1;
1396 offset = 8;
1397 break;
1398 case TSTBUS_OCSC:
1399 reg = UFS_TEST_BUS_CTRL_1;
1400 offset = 0;
1401 break;
1402 case TSTBUS_WRAPPER:
1403 reg = UFS_TEST_BUS_CTRL_2;
1404 offset = 16;
1405 break;
1406 case TSTBUS_COMBINED:
1407 reg = UFS_TEST_BUS_CTRL_2;
1408 offset = 8;
1409 break;
1410 case TSTBUS_UTP_HCI:
1411 reg = UFS_TEST_BUS_CTRL_2;
1412 offset = 0;
1413 break;
1414 case TSTBUS_UNIPRO:
1415 reg = UFS_UNIPRO_CFG;
1416 offset = 20;
1417 mask = 0xFFF;
1418 break;
1419 /*
1420 * No need for a default case, since
1421 * ufs_qcom_testbus_cfg_is_ok() checks that the configuration
1422 * is legal
1423 */
1424 }
1425 mask <<= offset;
1426 ufshcd_rmwl(host->hba, TEST_BUS_SEL,
1427 (u32)host->testbus.select_major << 19,
1428 REG_UFS_CFG1);
1429 ufshcd_rmwl(host->hba, mask,
1430 (u32)host->testbus.select_minor << offset,
1431 reg);
1432 ufs_qcom_enable_test_bus(host);
1433 /*
1434 * Make sure the test bus configuration is
1435 * committed before returning.
1436 */
1437 mb();
1438
1439 return 0;
1440 }
1441
ufs_qcom_dump_dbg_regs(struct ufs_hba * hba)1442 static void ufs_qcom_dump_dbg_regs(struct ufs_hba *hba)
1443 {
1444 ufshcd_dump_regs(hba, REG_UFS_SYS1CLK_1US, 16 * 4,
1445 "HCI Vendor Specific Registers ");
1446
1447 ufs_qcom_print_hw_debug_reg_all(hba, NULL, ufs_qcom_dump_regs_wrapper);
1448 }
1449
1450 /**
1451 * ufs_qcom_device_reset() - toggle the (optional) device reset line
1452 * @hba: per-adapter instance
1453 *
1454 * Toggles the (optional) reset line to reset the attached device.
1455 */
ufs_qcom_device_reset(struct ufs_hba * hba)1456 static int ufs_qcom_device_reset(struct ufs_hba *hba)
1457 {
1458 struct ufs_qcom_host *host = ufshcd_get_variant(hba);
1459
1460 /* reset gpio is optional */
1461 if (!host->device_reset)
1462 return -EOPNOTSUPP;
1463
1464 /*
1465 * The UFS device shall detect reset pulses of 1us, sleep for 10us to
1466 * be on the safe side.
1467 */
1468 ufs_qcom_device_reset_ctrl(hba, true);
1469 usleep_range(10, 15);
1470
1471 ufs_qcom_device_reset_ctrl(hba, false);
1472 usleep_range(10, 15);
1473
1474 return 0;
1475 }
1476
1477 #if IS_ENABLED(CONFIG_DEVFREQ_GOV_SIMPLE_ONDEMAND)
ufs_qcom_config_scaling_param(struct ufs_hba * hba,struct devfreq_dev_profile * p,void * data)1478 static void ufs_qcom_config_scaling_param(struct ufs_hba *hba,
1479 struct devfreq_dev_profile *p,
1480 void *data)
1481 {
1482 static struct devfreq_simple_ondemand_data *d;
1483
1484 if (!data)
1485 return;
1486
1487 d = (struct devfreq_simple_ondemand_data *)data;
1488 p->polling_ms = 60;
1489 d->upthreshold = 70;
1490 d->downdifferential = 5;
1491 }
1492 #else
ufs_qcom_config_scaling_param(struct ufs_hba * hba,struct devfreq_dev_profile * p,void * data)1493 static void ufs_qcom_config_scaling_param(struct ufs_hba *hba,
1494 struct devfreq_dev_profile *p,
1495 void *data)
1496 {
1497 }
1498 #endif
1499
1500 /*
1501 * struct ufs_hba_qcom_vops - UFS QCOM specific variant operations
1502 *
1503 * The variant operations configure the necessary controller and PHY
1504 * handshake during initialization.
1505 */
1506 static const struct ufs_hba_variant_ops ufs_hba_qcom_vops = {
1507 .name = "qcom",
1508 .init = ufs_qcom_init,
1509 .exit = ufs_qcom_exit,
1510 .get_ufs_hci_version = ufs_qcom_get_ufs_hci_version,
1511 .clk_scale_notify = ufs_qcom_clk_scale_notify,
1512 .setup_clocks = ufs_qcom_setup_clocks,
1513 .hce_enable_notify = ufs_qcom_hce_enable_notify,
1514 .link_startup_notify = ufs_qcom_link_startup_notify,
1515 .pwr_change_notify = ufs_qcom_pwr_change_notify,
1516 .apply_dev_quirks = ufs_qcom_apply_dev_quirks,
1517 .suspend = ufs_qcom_suspend,
1518 .resume = ufs_qcom_resume,
1519 .dbg_register_dump = ufs_qcom_dump_dbg_regs,
1520 .device_reset = ufs_qcom_device_reset,
1521 .config_scaling_param = ufs_qcom_config_scaling_param,
1522 .program_key = ufs_qcom_ice_program_key,
1523 };
1524
1525 /**
1526 * ufs_qcom_probe - probe routine of the driver
1527 * @pdev: pointer to Platform device handle
1528 *
1529 * Return zero for success and non-zero for failure
1530 */
ufs_qcom_probe(struct platform_device * pdev)1531 static int ufs_qcom_probe(struct platform_device *pdev)
1532 {
1533 int err;
1534 struct device *dev = &pdev->dev;
1535
1536 /* Perform generic probe */
1537 err = ufshcd_pltfrm_init(pdev, &ufs_hba_qcom_vops);
1538 if (err)
1539 dev_err(dev, "ufshcd_pltfrm_init() failed %d\n", err);
1540
1541 return err;
1542 }
1543
1544 /**
1545 * ufs_qcom_remove - set driver_data of the device to NULL
1546 * @pdev: pointer to platform device handle
1547 *
1548 * Always returns 0
1549 */
ufs_qcom_remove(struct platform_device * pdev)1550 static int ufs_qcom_remove(struct platform_device *pdev)
1551 {
1552 struct ufs_hba *hba = platform_get_drvdata(pdev);
1553
1554 pm_runtime_get_sync(&(pdev)->dev);
1555 ufshcd_remove(hba);
1556 return 0;
1557 }
1558
1559 static const struct of_device_id ufs_qcom_of_match[] = {
1560 { .compatible = "qcom,ufshc"},
1561 {},
1562 };
1563 MODULE_DEVICE_TABLE(of, ufs_qcom_of_match);
1564
1565 #ifdef CONFIG_ACPI
1566 static const struct acpi_device_id ufs_qcom_acpi_match[] = {
1567 { "QCOM24A5" },
1568 { },
1569 };
1570 MODULE_DEVICE_TABLE(acpi, ufs_qcom_acpi_match);
1571 #endif
1572
1573 static const struct dev_pm_ops ufs_qcom_pm_ops = {
1574 .suspend = ufshcd_pltfrm_suspend,
1575 .resume = ufshcd_pltfrm_resume,
1576 .runtime_suspend = ufshcd_pltfrm_runtime_suspend,
1577 .runtime_resume = ufshcd_pltfrm_runtime_resume,
1578 .runtime_idle = ufshcd_pltfrm_runtime_idle,
1579 };
1580
1581 static struct platform_driver ufs_qcom_pltform = {
1582 .probe = ufs_qcom_probe,
1583 .remove = ufs_qcom_remove,
1584 .shutdown = ufshcd_pltfrm_shutdown,
1585 .driver = {
1586 .name = "ufshcd-qcom",
1587 .pm = &ufs_qcom_pm_ops,
1588 .of_match_table = of_match_ptr(ufs_qcom_of_match),
1589 .acpi_match_table = ACPI_PTR(ufs_qcom_acpi_match),
1590 },
1591 };
1592 module_platform_driver(ufs_qcom_pltform);
1593
1594 MODULE_LICENSE("GPL v2");
1595