1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (C) 2019 MediaTek Inc.
4 * Authors:
5 * Stanley Chu <stanley.chu@mediatek.com>
6 * Peter Wang <peter.wang@mediatek.com>
7 */
8
9 #include <linux/arm-smccc.h>
10 #include <linux/bitfield.h>
11 #include <linux/of.h>
12 #include <linux/of_address.h>
13 #include <linux/of_device.h>
14 #include <linux/phy/phy.h>
15 #include <linux/platform_device.h>
16 #include <linux/regulator/consumer.h>
17 #include <linux/reset.h>
18 #include <linux/soc/mediatek/mtk_sip_svc.h>
19
20 #include "ufshcd.h"
21 #include "ufshcd-crypto.h"
22 #include "ufshcd-pltfrm.h"
23 #include "ufs_quirks.h"
24 #include "unipro.h"
25 #include "ufs-mediatek.h"
26
27 #define CREATE_TRACE_POINTS
28 #include "ufs-mediatek-trace.h"
29
30 #define ufs_mtk_smc(cmd, val, res) \
31 arm_smccc_smc(MTK_SIP_UFS_CONTROL, \
32 cmd, val, 0, 0, 0, 0, 0, &(res))
33
34 #define ufs_mtk_va09_pwr_ctrl(res, on) \
35 ufs_mtk_smc(UFS_MTK_SIP_VA09_PWR_CTRL, on, res)
36
37 #define ufs_mtk_crypto_ctrl(res, enable) \
38 ufs_mtk_smc(UFS_MTK_SIP_CRYPTO_CTRL, enable, res)
39
40 #define ufs_mtk_ref_clk_notify(on, res) \
41 ufs_mtk_smc(UFS_MTK_SIP_REF_CLK_NOTIFICATION, on, res)
42
43 #define ufs_mtk_device_reset_ctrl(high, res) \
44 ufs_mtk_smc(UFS_MTK_SIP_DEVICE_RESET, high, res)
45
46 static struct ufs_dev_fix ufs_mtk_dev_fixups[] = {
47 UFS_FIX(UFS_VENDOR_MICRON, UFS_ANY_MODEL,
48 UFS_DEVICE_QUIRK_DELAY_AFTER_LPM),
49 UFS_FIX(UFS_VENDOR_SKHYNIX, "H9HQ21AFAMZDAR",
50 UFS_DEVICE_QUIRK_SUPPORT_EXTENDED_FEATURES),
51 END_FIX
52 };
53
54 static const struct of_device_id ufs_mtk_of_match[] = {
55 { .compatible = "mediatek,mt8183-ufshci" },
56 {},
57 };
58
ufs_mtk_is_boost_crypt_enabled(struct ufs_hba * hba)59 static bool ufs_mtk_is_boost_crypt_enabled(struct ufs_hba *hba)
60 {
61 struct ufs_mtk_host *host = ufshcd_get_variant(hba);
62
63 return !!(host->caps & UFS_MTK_CAP_BOOST_CRYPT_ENGINE);
64 }
65
ufs_mtk_is_va09_supported(struct ufs_hba * hba)66 static bool ufs_mtk_is_va09_supported(struct ufs_hba *hba)
67 {
68 struct ufs_mtk_host *host = ufshcd_get_variant(hba);
69
70 return !!(host->caps & UFS_MTK_CAP_VA09_PWR_CTRL);
71 }
72
ufs_mtk_is_broken_vcc(struct ufs_hba * hba)73 static bool ufs_mtk_is_broken_vcc(struct ufs_hba *hba)
74 {
75 struct ufs_mtk_host *host = ufshcd_get_variant(hba);
76
77 return !!(host->caps & UFS_MTK_CAP_BROKEN_VCC);
78 }
79
ufs_mtk_cfg_unipro_cg(struct ufs_hba * hba,bool enable)80 static void ufs_mtk_cfg_unipro_cg(struct ufs_hba *hba, bool enable)
81 {
82 u32 tmp;
83
84 if (enable) {
85 ufshcd_dme_get(hba,
86 UIC_ARG_MIB(VS_SAVEPOWERCONTROL), &tmp);
87 tmp = tmp |
88 (1 << RX_SYMBOL_CLK_GATE_EN) |
89 (1 << SYS_CLK_GATE_EN) |
90 (1 << TX_CLK_GATE_EN);
91 ufshcd_dme_set(hba,
92 UIC_ARG_MIB(VS_SAVEPOWERCONTROL), tmp);
93
94 ufshcd_dme_get(hba,
95 UIC_ARG_MIB(VS_DEBUGCLOCKENABLE), &tmp);
96 tmp = tmp & ~(1 << TX_SYMBOL_CLK_REQ_FORCE);
97 ufshcd_dme_set(hba,
98 UIC_ARG_MIB(VS_DEBUGCLOCKENABLE), tmp);
99 } else {
100 ufshcd_dme_get(hba,
101 UIC_ARG_MIB(VS_SAVEPOWERCONTROL), &tmp);
102 tmp = tmp & ~((1 << RX_SYMBOL_CLK_GATE_EN) |
103 (1 << SYS_CLK_GATE_EN) |
104 (1 << TX_CLK_GATE_EN));
105 ufshcd_dme_set(hba,
106 UIC_ARG_MIB(VS_SAVEPOWERCONTROL), tmp);
107
108 ufshcd_dme_get(hba,
109 UIC_ARG_MIB(VS_DEBUGCLOCKENABLE), &tmp);
110 tmp = tmp | (1 << TX_SYMBOL_CLK_REQ_FORCE);
111 ufshcd_dme_set(hba,
112 UIC_ARG_MIB(VS_DEBUGCLOCKENABLE), tmp);
113 }
114 }
115
ufs_mtk_crypto_enable(struct ufs_hba * hba)116 static void ufs_mtk_crypto_enable(struct ufs_hba *hba)
117 {
118 struct arm_smccc_res res;
119
120 ufs_mtk_crypto_ctrl(res, 1);
121 if (res.a0) {
122 dev_info(hba->dev, "%s: crypto enable failed, err: %lu\n",
123 __func__, res.a0);
124 hba->caps &= ~UFSHCD_CAP_CRYPTO;
125 }
126 }
127
ufs_mtk_host_reset(struct ufs_hba * hba)128 static void ufs_mtk_host_reset(struct ufs_hba *hba)
129 {
130 struct ufs_mtk_host *host = ufshcd_get_variant(hba);
131
132 reset_control_assert(host->hci_reset);
133 reset_control_assert(host->crypto_reset);
134 reset_control_assert(host->unipro_reset);
135
136 usleep_range(100, 110);
137
138 reset_control_deassert(host->unipro_reset);
139 reset_control_deassert(host->crypto_reset);
140 reset_control_deassert(host->hci_reset);
141 }
142
ufs_mtk_init_reset_control(struct ufs_hba * hba,struct reset_control ** rc,char * str)143 static void ufs_mtk_init_reset_control(struct ufs_hba *hba,
144 struct reset_control **rc,
145 char *str)
146 {
147 *rc = devm_reset_control_get(hba->dev, str);
148 if (IS_ERR(*rc)) {
149 dev_info(hba->dev, "Failed to get reset control %s: %ld\n",
150 str, PTR_ERR(*rc));
151 *rc = NULL;
152 }
153 }
154
ufs_mtk_init_reset(struct ufs_hba * hba)155 static void ufs_mtk_init_reset(struct ufs_hba *hba)
156 {
157 struct ufs_mtk_host *host = ufshcd_get_variant(hba);
158
159 ufs_mtk_init_reset_control(hba, &host->hci_reset,
160 "hci_rst");
161 ufs_mtk_init_reset_control(hba, &host->unipro_reset,
162 "unipro_rst");
163 ufs_mtk_init_reset_control(hba, &host->crypto_reset,
164 "crypto_rst");
165 }
166
ufs_mtk_hce_enable_notify(struct ufs_hba * hba,enum ufs_notify_change_status status)167 static int ufs_mtk_hce_enable_notify(struct ufs_hba *hba,
168 enum ufs_notify_change_status status)
169 {
170 struct ufs_mtk_host *host = ufshcd_get_variant(hba);
171 unsigned long flags;
172
173 if (status == PRE_CHANGE) {
174 if (host->unipro_lpm) {
175 hba->vps->hba_enable_delay_us = 0;
176 } else {
177 hba->vps->hba_enable_delay_us = 600;
178 ufs_mtk_host_reset(hba);
179 }
180
181 if (hba->caps & UFSHCD_CAP_CRYPTO)
182 ufs_mtk_crypto_enable(hba);
183
184 if (host->caps & UFS_MTK_CAP_DISABLE_AH8) {
185 spin_lock_irqsave(hba->host->host_lock, flags);
186 ufshcd_writel(hba, 0,
187 REG_AUTO_HIBERNATE_IDLE_TIMER);
188 spin_unlock_irqrestore(hba->host->host_lock,
189 flags);
190
191 hba->capabilities &= ~MASK_AUTO_HIBERN8_SUPPORT;
192 hba->ahit = 0;
193 }
194 }
195
196 return 0;
197 }
198
ufs_mtk_bind_mphy(struct ufs_hba * hba)199 static int ufs_mtk_bind_mphy(struct ufs_hba *hba)
200 {
201 struct ufs_mtk_host *host = ufshcd_get_variant(hba);
202 struct device *dev = hba->dev;
203 struct device_node *np = dev->of_node;
204 int err = 0;
205
206 host->mphy = devm_of_phy_get_by_index(dev, np, 0);
207
208 if (host->mphy == ERR_PTR(-EPROBE_DEFER)) {
209 /*
210 * UFS driver might be probed before the phy driver does.
211 * In that case we would like to return EPROBE_DEFER code.
212 */
213 err = -EPROBE_DEFER;
214 dev_info(dev,
215 "%s: required phy hasn't probed yet. err = %d\n",
216 __func__, err);
217 } else if (IS_ERR(host->mphy)) {
218 err = PTR_ERR(host->mphy);
219 if (err != -ENODEV) {
220 dev_info(dev, "%s: PHY get failed %d\n", __func__,
221 err);
222 }
223 }
224
225 if (err)
226 host->mphy = NULL;
227 /*
228 * Allow unbound mphy because not every platform needs specific
229 * mphy control.
230 */
231 if (err == -ENODEV)
232 err = 0;
233
234 return err;
235 }
236
ufs_mtk_setup_ref_clk(struct ufs_hba * hba,bool on)237 static int ufs_mtk_setup_ref_clk(struct ufs_hba *hba, bool on)
238 {
239 struct ufs_mtk_host *host = ufshcd_get_variant(hba);
240 struct arm_smccc_res res;
241 ktime_t timeout, time_checked;
242 u32 value;
243
244 if (host->ref_clk_enabled == on)
245 return 0;
246
247 if (on) {
248 ufs_mtk_ref_clk_notify(on, res);
249 ufshcd_delay_us(host->ref_clk_ungating_wait_us, 10);
250 ufshcd_writel(hba, REFCLK_REQUEST, REG_UFS_REFCLK_CTRL);
251 } else {
252 ufshcd_writel(hba, REFCLK_RELEASE, REG_UFS_REFCLK_CTRL);
253 }
254
255 /* Wait for ack */
256 timeout = ktime_add_us(ktime_get(), REFCLK_REQ_TIMEOUT_US);
257 do {
258 time_checked = ktime_get();
259 value = ufshcd_readl(hba, REG_UFS_REFCLK_CTRL);
260
261 /* Wait until ack bit equals to req bit */
262 if (((value & REFCLK_ACK) >> 1) == (value & REFCLK_REQUEST))
263 goto out;
264
265 usleep_range(100, 200);
266 } while (ktime_before(time_checked, timeout));
267
268 dev_err(hba->dev, "missing ack of refclk req, reg: 0x%x\n", value);
269
270 ufs_mtk_ref_clk_notify(host->ref_clk_enabled, res);
271
272 return -ETIMEDOUT;
273
274 out:
275 host->ref_clk_enabled = on;
276 if (!on) {
277 ufshcd_delay_us(host->ref_clk_gating_wait_us, 10);
278 ufs_mtk_ref_clk_notify(on, res);
279 }
280
281 return 0;
282 }
283
ufs_mtk_setup_ref_clk_wait_us(struct ufs_hba * hba,u16 gating_us,u16 ungating_us)284 static void ufs_mtk_setup_ref_clk_wait_us(struct ufs_hba *hba,
285 u16 gating_us, u16 ungating_us)
286 {
287 struct ufs_mtk_host *host = ufshcd_get_variant(hba);
288
289 if (hba->dev_info.clk_gating_wait_us) {
290 host->ref_clk_gating_wait_us =
291 hba->dev_info.clk_gating_wait_us;
292 } else {
293 host->ref_clk_gating_wait_us = gating_us;
294 }
295
296 host->ref_clk_ungating_wait_us = ungating_us;
297 }
298
ufs_mtk_wait_link_state(struct ufs_hba * hba,u32 state,unsigned long max_wait_ms)299 static int ufs_mtk_wait_link_state(struct ufs_hba *hba, u32 state,
300 unsigned long max_wait_ms)
301 {
302 ktime_t timeout, time_checked;
303 u32 val;
304
305 timeout = ktime_add_ms(ktime_get(), max_wait_ms);
306 do {
307 time_checked = ktime_get();
308 ufshcd_writel(hba, 0x20, REG_UFS_DEBUG_SEL);
309 val = ufshcd_readl(hba, REG_UFS_PROBE);
310 val = val >> 28;
311
312 if (val == state)
313 return 0;
314
315 /* Sleep for max. 200us */
316 usleep_range(100, 200);
317 } while (ktime_before(time_checked, timeout));
318
319 if (val == state)
320 return 0;
321
322 return -ETIMEDOUT;
323 }
324
ufs_mtk_mphy_power_on(struct ufs_hba * hba,bool on)325 static int ufs_mtk_mphy_power_on(struct ufs_hba *hba, bool on)
326 {
327 struct ufs_mtk_host *host = ufshcd_get_variant(hba);
328 struct phy *mphy = host->mphy;
329 struct arm_smccc_res res;
330 int ret = 0;
331
332 if (!mphy || !(on ^ host->mphy_powered_on))
333 return 0;
334
335 if (on) {
336 if (ufs_mtk_is_va09_supported(hba)) {
337 ret = regulator_enable(host->reg_va09);
338 if (ret < 0)
339 goto out;
340 /* wait 200 us to stablize VA09 */
341 usleep_range(200, 210);
342 ufs_mtk_va09_pwr_ctrl(res, 1);
343 }
344 phy_power_on(mphy);
345 } else {
346 phy_power_off(mphy);
347 if (ufs_mtk_is_va09_supported(hba)) {
348 ufs_mtk_va09_pwr_ctrl(res, 0);
349 ret = regulator_disable(host->reg_va09);
350 if (ret < 0)
351 goto out;
352 }
353 }
354 out:
355 if (ret) {
356 dev_info(hba->dev,
357 "failed to %s va09: %d\n",
358 on ? "enable" : "disable",
359 ret);
360 } else {
361 host->mphy_powered_on = on;
362 }
363
364 return ret;
365 }
366
ufs_mtk_get_host_clk(struct device * dev,const char * name,struct clk ** clk_out)367 static int ufs_mtk_get_host_clk(struct device *dev, const char *name,
368 struct clk **clk_out)
369 {
370 struct clk *clk;
371 int err = 0;
372
373 clk = devm_clk_get(dev, name);
374 if (IS_ERR(clk))
375 err = PTR_ERR(clk);
376 else
377 *clk_out = clk;
378
379 return err;
380 }
381
ufs_mtk_boost_crypt(struct ufs_hba * hba,bool boost)382 static void ufs_mtk_boost_crypt(struct ufs_hba *hba, bool boost)
383 {
384 struct ufs_mtk_host *host = ufshcd_get_variant(hba);
385 struct ufs_mtk_crypt_cfg *cfg;
386 struct regulator *reg;
387 int volt, ret;
388
389 if (!ufs_mtk_is_boost_crypt_enabled(hba))
390 return;
391
392 cfg = host->crypt;
393 volt = cfg->vcore_volt;
394 reg = cfg->reg_vcore;
395
396 ret = clk_prepare_enable(cfg->clk_crypt_mux);
397 if (ret) {
398 dev_info(hba->dev, "clk_prepare_enable(): %d\n",
399 ret);
400 return;
401 }
402
403 if (boost) {
404 ret = regulator_set_voltage(reg, volt, INT_MAX);
405 if (ret) {
406 dev_info(hba->dev,
407 "failed to set vcore to %d\n", volt);
408 goto out;
409 }
410
411 ret = clk_set_parent(cfg->clk_crypt_mux,
412 cfg->clk_crypt_perf);
413 if (ret) {
414 dev_info(hba->dev,
415 "failed to set clk_crypt_perf\n");
416 regulator_set_voltage(reg, 0, INT_MAX);
417 goto out;
418 }
419 } else {
420 ret = clk_set_parent(cfg->clk_crypt_mux,
421 cfg->clk_crypt_lp);
422 if (ret) {
423 dev_info(hba->dev,
424 "failed to set clk_crypt_lp\n");
425 goto out;
426 }
427
428 ret = regulator_set_voltage(reg, 0, INT_MAX);
429 if (ret) {
430 dev_info(hba->dev,
431 "failed to set vcore to MIN\n");
432 }
433 }
434 out:
435 clk_disable_unprepare(cfg->clk_crypt_mux);
436 }
437
ufs_mtk_init_host_clk(struct ufs_hba * hba,const char * name,struct clk ** clk)438 static int ufs_mtk_init_host_clk(struct ufs_hba *hba, const char *name,
439 struct clk **clk)
440 {
441 int ret;
442
443 ret = ufs_mtk_get_host_clk(hba->dev, name, clk);
444 if (ret) {
445 dev_info(hba->dev, "%s: failed to get %s: %d", __func__,
446 name, ret);
447 }
448
449 return ret;
450 }
451
ufs_mtk_init_boost_crypt(struct ufs_hba * hba)452 static void ufs_mtk_init_boost_crypt(struct ufs_hba *hba)
453 {
454 struct ufs_mtk_host *host = ufshcd_get_variant(hba);
455 struct ufs_mtk_crypt_cfg *cfg;
456 struct device *dev = hba->dev;
457 struct regulator *reg;
458 u32 volt;
459
460 host->crypt = devm_kzalloc(dev, sizeof(*(host->crypt)),
461 GFP_KERNEL);
462 if (!host->crypt)
463 goto disable_caps;
464
465 reg = devm_regulator_get_optional(dev, "dvfsrc-vcore");
466 if (IS_ERR(reg)) {
467 dev_info(dev, "failed to get dvfsrc-vcore: %ld",
468 PTR_ERR(reg));
469 goto disable_caps;
470 }
471
472 if (of_property_read_u32(dev->of_node, "boost-crypt-vcore-min",
473 &volt)) {
474 dev_info(dev, "failed to get boost-crypt-vcore-min");
475 goto disable_caps;
476 }
477
478 cfg = host->crypt;
479 if (ufs_mtk_init_host_clk(hba, "crypt_mux",
480 &cfg->clk_crypt_mux))
481 goto disable_caps;
482
483 if (ufs_mtk_init_host_clk(hba, "crypt_lp",
484 &cfg->clk_crypt_lp))
485 goto disable_caps;
486
487 if (ufs_mtk_init_host_clk(hba, "crypt_perf",
488 &cfg->clk_crypt_perf))
489 goto disable_caps;
490
491 cfg->reg_vcore = reg;
492 cfg->vcore_volt = volt;
493 host->caps |= UFS_MTK_CAP_BOOST_CRYPT_ENGINE;
494
495 disable_caps:
496 return;
497 }
498
ufs_mtk_init_va09_pwr_ctrl(struct ufs_hba * hba)499 static void ufs_mtk_init_va09_pwr_ctrl(struct ufs_hba *hba)
500 {
501 struct ufs_mtk_host *host = ufshcd_get_variant(hba);
502
503 host->reg_va09 = regulator_get(hba->dev, "va09");
504 if (IS_ERR(host->reg_va09))
505 dev_info(hba->dev, "failed to get va09");
506 else
507 host->caps |= UFS_MTK_CAP_VA09_PWR_CTRL;
508 }
509
ufs_mtk_init_host_caps(struct ufs_hba * hba)510 static void ufs_mtk_init_host_caps(struct ufs_hba *hba)
511 {
512 struct ufs_mtk_host *host = ufshcd_get_variant(hba);
513 struct device_node *np = hba->dev->of_node;
514
515 if (of_property_read_bool(np, "mediatek,ufs-boost-crypt"))
516 ufs_mtk_init_boost_crypt(hba);
517
518 if (of_property_read_bool(np, "mediatek,ufs-support-va09"))
519 ufs_mtk_init_va09_pwr_ctrl(hba);
520
521 if (of_property_read_bool(np, "mediatek,ufs-disable-ah8"))
522 host->caps |= UFS_MTK_CAP_DISABLE_AH8;
523
524 if (of_property_read_bool(np, "mediatek,ufs-broken-vcc"))
525 host->caps |= UFS_MTK_CAP_BROKEN_VCC;
526
527 dev_info(hba->dev, "caps: 0x%x", host->caps);
528 }
529
530 /**
531 * ufs_mtk_setup_clocks - enables/disable clocks
532 * @hba: host controller instance
533 * @on: If true, enable clocks else disable them.
534 * @status: PRE_CHANGE or POST_CHANGE notify
535 *
536 * Returns 0 on success, non-zero on failure.
537 */
ufs_mtk_setup_clocks(struct ufs_hba * hba,bool on,enum ufs_notify_change_status status)538 static int ufs_mtk_setup_clocks(struct ufs_hba *hba, bool on,
539 enum ufs_notify_change_status status)
540 {
541 struct ufs_mtk_host *host = ufshcd_get_variant(hba);
542 bool clk_pwr_off = false;
543 int ret = 0;
544
545 /*
546 * In case ufs_mtk_init() is not yet done, simply ignore.
547 * This ufs_mtk_setup_clocks() shall be called from
548 * ufs_mtk_init() after init is done.
549 */
550 if (!host)
551 return 0;
552
553 if (!on && status == PRE_CHANGE) {
554 if (ufshcd_is_link_off(hba)) {
555 clk_pwr_off = true;
556 } else if (ufshcd_is_link_hibern8(hba) ||
557 (!ufshcd_can_hibern8_during_gating(hba) &&
558 ufshcd_is_auto_hibern8_enabled(hba))) {
559 /*
560 * Gate ref-clk and poweroff mphy if link state is in
561 * OFF or Hibern8 by either Auto-Hibern8 or
562 * ufshcd_link_state_transition().
563 */
564 ret = ufs_mtk_wait_link_state(hba,
565 VS_LINK_HIBERN8,
566 15);
567 if (!ret)
568 clk_pwr_off = true;
569 }
570
571 if (clk_pwr_off) {
572 ufs_mtk_boost_crypt(hba, on);
573 ufs_mtk_setup_ref_clk(hba, on);
574 phy_power_off(host->mphy);
575 }
576 } else if (on && status == POST_CHANGE) {
577 phy_power_on(host->mphy);
578 ufs_mtk_setup_ref_clk(hba, on);
579 ufs_mtk_boost_crypt(hba, on);
580 }
581
582 return ret;
583 }
584
ufs_mtk_get_controller_version(struct ufs_hba * hba)585 static void ufs_mtk_get_controller_version(struct ufs_hba *hba)
586 {
587 struct ufs_mtk_host *host = ufshcd_get_variant(hba);
588 int ret, ver = 0;
589
590 if (host->hw_ver.major)
591 return;
592
593 /* Set default (minimum) version anyway */
594 host->hw_ver.major = 2;
595
596 ret = ufshcd_dme_get(hba, UIC_ARG_MIB(PA_LOCALVERINFO), &ver);
597 if (!ret) {
598 if (ver >= UFS_UNIPRO_VER_1_8) {
599 host->hw_ver.major = 3;
600 /*
601 * Fix HCI version for some platforms with
602 * incorrect version
603 */
604 if (hba->ufs_version < ufshci_version(3, 0))
605 hba->ufs_version = ufshci_version(3, 0);
606 }
607 }
608 }
609
ufs_mtk_get_ufs_hci_version(struct ufs_hba * hba)610 static u32 ufs_mtk_get_ufs_hci_version(struct ufs_hba *hba)
611 {
612 return hba->ufs_version;
613 }
614
615 /**
616 * ufs_mtk_init - find other essential mmio bases
617 * @hba: host controller instance
618 *
619 * Binds PHY with controller and powers up PHY enabling clocks
620 * and regulators.
621 *
622 * Returns -EPROBE_DEFER if binding fails, returns negative error
623 * on phy power up failure and returns zero on success.
624 */
ufs_mtk_init(struct ufs_hba * hba)625 static int ufs_mtk_init(struct ufs_hba *hba)
626 {
627 const struct of_device_id *id;
628 struct device *dev = hba->dev;
629 struct ufs_mtk_host *host;
630 int err = 0;
631
632 host = devm_kzalloc(dev, sizeof(*host), GFP_KERNEL);
633 if (!host) {
634 err = -ENOMEM;
635 dev_info(dev, "%s: no memory for mtk ufs host\n", __func__);
636 goto out;
637 }
638
639 host->hba = hba;
640 ufshcd_set_variant(hba, host);
641
642 id = of_match_device(ufs_mtk_of_match, dev);
643 if (!id) {
644 err = -EINVAL;
645 goto out;
646 }
647
648 /* Initialize host capability */
649 ufs_mtk_init_host_caps(hba);
650
651 err = ufs_mtk_bind_mphy(hba);
652 if (err)
653 goto out_variant_clear;
654
655 ufs_mtk_init_reset(hba);
656
657 /* Enable runtime autosuspend */
658 hba->caps |= UFSHCD_CAP_RPM_AUTOSUSPEND;
659
660 /* Enable clock-gating */
661 hba->caps |= UFSHCD_CAP_CLK_GATING;
662
663 /* Enable inline encryption */
664 hba->caps |= UFSHCD_CAP_CRYPTO;
665
666 /* Enable WriteBooster */
667 hba->caps |= UFSHCD_CAP_WB_EN;
668 hba->quirks |= UFSHCI_QUIRK_SKIP_MANUAL_WB_FLUSH_CTRL;
669 hba->vps->wb_flush_threshold = UFS_WB_BUF_REMAIN_PERCENT(80);
670
671 if (host->caps & UFS_MTK_CAP_DISABLE_AH8)
672 hba->caps |= UFSHCD_CAP_HIBERN8_WITH_CLK_GATING;
673
674 /*
675 * ufshcd_vops_init() is invoked after
676 * ufshcd_setup_clock(true) in ufshcd_hba_init() thus
677 * phy clock setup is skipped.
678 *
679 * Enable phy clocks specifically here.
680 */
681 ufs_mtk_mphy_power_on(hba, true);
682 ufs_mtk_setup_clocks(hba, true, POST_CHANGE);
683
684 goto out;
685
686 out_variant_clear:
687 ufshcd_set_variant(hba, NULL);
688 out:
689 return err;
690 }
691
ufs_mtk_pre_pwr_change(struct ufs_hba * hba,struct ufs_pa_layer_attr * dev_max_params,struct ufs_pa_layer_attr * dev_req_params)692 static int ufs_mtk_pre_pwr_change(struct ufs_hba *hba,
693 struct ufs_pa_layer_attr *dev_max_params,
694 struct ufs_pa_layer_attr *dev_req_params)
695 {
696 struct ufs_mtk_host *host = ufshcd_get_variant(hba);
697 struct ufs_dev_params host_cap;
698 u32 adapt_val;
699 int ret;
700
701 host_cap.tx_lanes = UFS_MTK_LIMIT_NUM_LANES_TX;
702 host_cap.rx_lanes = UFS_MTK_LIMIT_NUM_LANES_RX;
703 host_cap.hs_rx_gear = UFS_MTK_LIMIT_HSGEAR_RX;
704 host_cap.hs_tx_gear = UFS_MTK_LIMIT_HSGEAR_TX;
705 host_cap.pwm_rx_gear = UFS_MTK_LIMIT_PWMGEAR_RX;
706 host_cap.pwm_tx_gear = UFS_MTK_LIMIT_PWMGEAR_TX;
707 host_cap.rx_pwr_pwm = UFS_MTK_LIMIT_RX_PWR_PWM;
708 host_cap.tx_pwr_pwm = UFS_MTK_LIMIT_TX_PWR_PWM;
709 host_cap.rx_pwr_hs = UFS_MTK_LIMIT_RX_PWR_HS;
710 host_cap.tx_pwr_hs = UFS_MTK_LIMIT_TX_PWR_HS;
711 host_cap.hs_rate = UFS_MTK_LIMIT_HS_RATE;
712 host_cap.desired_working_mode =
713 UFS_MTK_LIMIT_DESIRED_MODE;
714
715 ret = ufshcd_get_pwr_dev_param(&host_cap,
716 dev_max_params,
717 dev_req_params);
718 if (ret) {
719 pr_info("%s: failed to determine capabilities\n",
720 __func__);
721 }
722
723 if (host->hw_ver.major >= 3) {
724 if (dev_req_params->gear_tx == UFS_HS_G4)
725 adapt_val = PA_INITIAL_ADAPT;
726 else
727 adapt_val = PA_NO_ADAPT;
728 ufshcd_dme_set(hba,
729 UIC_ARG_MIB(PA_TXHSADAPTTYPE),
730 adapt_val);
731 }
732
733 return ret;
734 }
735
ufs_mtk_pwr_change_notify(struct ufs_hba * hba,enum ufs_notify_change_status stage,struct ufs_pa_layer_attr * dev_max_params,struct ufs_pa_layer_attr * dev_req_params)736 static int ufs_mtk_pwr_change_notify(struct ufs_hba *hba,
737 enum ufs_notify_change_status stage,
738 struct ufs_pa_layer_attr *dev_max_params,
739 struct ufs_pa_layer_attr *dev_req_params)
740 {
741 int ret = 0;
742
743 switch (stage) {
744 case PRE_CHANGE:
745 ret = ufs_mtk_pre_pwr_change(hba, dev_max_params,
746 dev_req_params);
747 break;
748 case POST_CHANGE:
749 break;
750 default:
751 ret = -EINVAL;
752 break;
753 }
754
755 return ret;
756 }
757
ufs_mtk_unipro_set_pm(struct ufs_hba * hba,bool lpm)758 static int ufs_mtk_unipro_set_pm(struct ufs_hba *hba, bool lpm)
759 {
760 int ret;
761 struct ufs_mtk_host *host = ufshcd_get_variant(hba);
762
763 ret = ufshcd_dme_set(hba,
764 UIC_ARG_MIB_SEL(VS_UNIPROPOWERDOWNCONTROL, 0),
765 lpm);
766 if (!ret || !lpm) {
767 /*
768 * Forcibly set as non-LPM mode if UIC commands is failed
769 * to use default hba_enable_delay_us value for re-enabling
770 * the host.
771 */
772 host->unipro_lpm = lpm;
773 }
774
775 return ret;
776 }
777
ufs_mtk_pre_link(struct ufs_hba * hba)778 static int ufs_mtk_pre_link(struct ufs_hba *hba)
779 {
780 int ret;
781 u32 tmp;
782
783 ufs_mtk_get_controller_version(hba);
784
785 ret = ufs_mtk_unipro_set_pm(hba, false);
786 if (ret)
787 return ret;
788
789 /*
790 * Setting PA_Local_TX_LCC_Enable to 0 before link startup
791 * to make sure that both host and device TX LCC are disabled
792 * once link startup is completed.
793 */
794 ret = ufshcd_disable_host_tx_lcc(hba);
795 if (ret)
796 return ret;
797
798 /* disable deep stall */
799 ret = ufshcd_dme_get(hba, UIC_ARG_MIB(VS_SAVEPOWERCONTROL), &tmp);
800 if (ret)
801 return ret;
802
803 tmp &= ~(1 << 6);
804
805 ret = ufshcd_dme_set(hba, UIC_ARG_MIB(VS_SAVEPOWERCONTROL), tmp);
806
807 return ret;
808 }
809
ufs_mtk_setup_clk_gating(struct ufs_hba * hba)810 static void ufs_mtk_setup_clk_gating(struct ufs_hba *hba)
811 {
812 unsigned long flags;
813 u32 ah_ms;
814
815 if (ufshcd_is_clkgating_allowed(hba)) {
816 if (ufshcd_is_auto_hibern8_supported(hba) && hba->ahit)
817 ah_ms = FIELD_GET(UFSHCI_AHIBERN8_TIMER_MASK,
818 hba->ahit);
819 else
820 ah_ms = 10;
821 spin_lock_irqsave(hba->host->host_lock, flags);
822 hba->clk_gating.delay_ms = ah_ms + 5;
823 spin_unlock_irqrestore(hba->host->host_lock, flags);
824 }
825 }
826
ufs_mtk_post_link(struct ufs_hba * hba)827 static int ufs_mtk_post_link(struct ufs_hba *hba)
828 {
829 /* enable unipro clock gating feature */
830 ufs_mtk_cfg_unipro_cg(hba, true);
831
832 /* configure auto-hibern8 timer to 10ms */
833 if (ufshcd_is_auto_hibern8_supported(hba)) {
834 ufshcd_auto_hibern8_update(hba,
835 FIELD_PREP(UFSHCI_AHIBERN8_TIMER_MASK, 10) |
836 FIELD_PREP(UFSHCI_AHIBERN8_SCALE_MASK, 3));
837 }
838
839 ufs_mtk_setup_clk_gating(hba);
840
841 return 0;
842 }
843
ufs_mtk_link_startup_notify(struct ufs_hba * hba,enum ufs_notify_change_status stage)844 static int ufs_mtk_link_startup_notify(struct ufs_hba *hba,
845 enum ufs_notify_change_status stage)
846 {
847 int ret = 0;
848
849 switch (stage) {
850 case PRE_CHANGE:
851 ret = ufs_mtk_pre_link(hba);
852 break;
853 case POST_CHANGE:
854 ret = ufs_mtk_post_link(hba);
855 break;
856 default:
857 ret = -EINVAL;
858 break;
859 }
860
861 return ret;
862 }
863
ufs_mtk_device_reset(struct ufs_hba * hba)864 static int ufs_mtk_device_reset(struct ufs_hba *hba)
865 {
866 struct arm_smccc_res res;
867
868 /* disable hba before device reset */
869 ufshcd_hba_stop(hba);
870
871 ufs_mtk_device_reset_ctrl(0, res);
872
873 /*
874 * The reset signal is active low. UFS devices shall detect
875 * more than or equal to 1us of positive or negative RST_n
876 * pulse width.
877 *
878 * To be on safe side, keep the reset low for at least 10us.
879 */
880 usleep_range(10, 15);
881
882 ufs_mtk_device_reset_ctrl(1, res);
883
884 /* Some devices may need time to respond to rst_n */
885 usleep_range(10000, 15000);
886
887 dev_info(hba->dev, "device reset done\n");
888
889 return 0;
890 }
891
ufs_mtk_link_set_hpm(struct ufs_hba * hba)892 static int ufs_mtk_link_set_hpm(struct ufs_hba *hba)
893 {
894 int err;
895
896 err = ufshcd_hba_enable(hba);
897 if (err)
898 return err;
899
900 err = ufs_mtk_unipro_set_pm(hba, false);
901 if (err)
902 return err;
903
904 err = ufshcd_uic_hibern8_exit(hba);
905 if (!err)
906 ufshcd_set_link_active(hba);
907 else
908 return err;
909
910 err = ufshcd_make_hba_operational(hba);
911 if (err)
912 return err;
913
914 return 0;
915 }
916
ufs_mtk_link_set_lpm(struct ufs_hba * hba)917 static int ufs_mtk_link_set_lpm(struct ufs_hba *hba)
918 {
919 int err;
920
921 err = ufs_mtk_unipro_set_pm(hba, true);
922 if (err) {
923 /* Resume UniPro state for following error recovery */
924 ufs_mtk_unipro_set_pm(hba, false);
925 return err;
926 }
927
928 return 0;
929 }
930
ufs_mtk_vreg_set_lpm(struct ufs_hba * hba,bool lpm)931 static void ufs_mtk_vreg_set_lpm(struct ufs_hba *hba, bool lpm)
932 {
933 if (!hba->vreg_info.vccq2 || !hba->vreg_info.vcc)
934 return;
935
936 if (lpm && !hba->vreg_info.vcc->enabled)
937 regulator_set_mode(hba->vreg_info.vccq2->reg,
938 REGULATOR_MODE_IDLE);
939 else if (!lpm)
940 regulator_set_mode(hba->vreg_info.vccq2->reg,
941 REGULATOR_MODE_NORMAL);
942 }
943
ufs_mtk_suspend(struct ufs_hba * hba,enum ufs_pm_op pm_op)944 static int ufs_mtk_suspend(struct ufs_hba *hba, enum ufs_pm_op pm_op)
945 {
946 int err;
947 struct arm_smccc_res res;
948
949 if (ufshcd_is_link_hibern8(hba)) {
950 err = ufs_mtk_link_set_lpm(hba);
951 if (err)
952 goto fail;
953 }
954
955 if (!ufshcd_is_link_active(hba)) {
956 /*
957 * Make sure no error will be returned to prevent
958 * ufshcd_suspend() re-enabling regulators while vreg is still
959 * in low-power mode.
960 */
961 ufs_mtk_vreg_set_lpm(hba, true);
962 err = ufs_mtk_mphy_power_on(hba, false);
963 if (err)
964 goto fail;
965 }
966
967 if (ufshcd_is_link_off(hba))
968 ufs_mtk_device_reset_ctrl(0, res);
969
970 return 0;
971 fail:
972 /*
973 * Set link as off state enforcedly to trigger
974 * ufshcd_host_reset_and_restore() in ufshcd_suspend()
975 * for completed host reset.
976 */
977 ufshcd_set_link_off(hba);
978 return -EAGAIN;
979 }
980
ufs_mtk_resume(struct ufs_hba * hba,enum ufs_pm_op pm_op)981 static int ufs_mtk_resume(struct ufs_hba *hba, enum ufs_pm_op pm_op)
982 {
983 int err;
984
985 err = ufs_mtk_mphy_power_on(hba, true);
986 if (err)
987 goto fail;
988
989 ufs_mtk_vreg_set_lpm(hba, false);
990
991 if (ufshcd_is_link_hibern8(hba)) {
992 err = ufs_mtk_link_set_hpm(hba);
993 if (err)
994 goto fail;
995 }
996
997 return 0;
998 fail:
999 return ufshcd_link_recovery(hba);
1000 }
1001
ufs_mtk_dbg_register_dump(struct ufs_hba * hba)1002 static void ufs_mtk_dbg_register_dump(struct ufs_hba *hba)
1003 {
1004 ufshcd_dump_regs(hba, REG_UFS_REFCLK_CTRL, 0x4, "Ref-Clk Ctrl ");
1005
1006 ufshcd_dump_regs(hba, REG_UFS_EXTREG, 0x4, "Ext Reg ");
1007
1008 ufshcd_dump_regs(hba, REG_UFS_MPHYCTRL,
1009 REG_UFS_REJECT_MON - REG_UFS_MPHYCTRL + 4,
1010 "MPHY Ctrl ");
1011
1012 /* Direct debugging information to REG_MTK_PROBE */
1013 ufshcd_writel(hba, 0x20, REG_UFS_DEBUG_SEL);
1014 ufshcd_dump_regs(hba, REG_UFS_PROBE, 0x4, "Debug Probe ");
1015 }
1016
ufs_mtk_apply_dev_quirks(struct ufs_hba * hba)1017 static int ufs_mtk_apply_dev_quirks(struct ufs_hba *hba)
1018 {
1019 struct ufs_dev_info *dev_info = &hba->dev_info;
1020 u16 mid = dev_info->wmanufacturerid;
1021
1022 if (mid == UFS_VENDOR_SAMSUNG)
1023 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TACTIVATE), 6);
1024
1025 /*
1026 * Decide waiting time before gating reference clock and
1027 * after ungating reference clock according to vendors'
1028 * requirements.
1029 */
1030 if (mid == UFS_VENDOR_SAMSUNG)
1031 ufs_mtk_setup_ref_clk_wait_us(hba, 1, 1);
1032 else if (mid == UFS_VENDOR_SKHYNIX)
1033 ufs_mtk_setup_ref_clk_wait_us(hba, 30, 30);
1034 else if (mid == UFS_VENDOR_TOSHIBA)
1035 ufs_mtk_setup_ref_clk_wait_us(hba, 100, 32);
1036
1037 return 0;
1038 }
1039
ufs_mtk_fixup_dev_quirks(struct ufs_hba * hba)1040 static void ufs_mtk_fixup_dev_quirks(struct ufs_hba *hba)
1041 {
1042 ufshcd_fixup_dev_quirks(hba, ufs_mtk_dev_fixups);
1043
1044 if (ufs_mtk_is_broken_vcc(hba) && hba->vreg_info.vcc &&
1045 (hba->dev_quirks & UFS_DEVICE_QUIRK_DELAY_AFTER_LPM)) {
1046 hba->vreg_info.vcc->always_on = true;
1047 /*
1048 * VCC will be kept always-on thus we don't
1049 * need any delay during regulator operations
1050 */
1051 hba->dev_quirks &= ~(UFS_DEVICE_QUIRK_DELAY_BEFORE_LPM |
1052 UFS_DEVICE_QUIRK_DELAY_AFTER_LPM);
1053 }
1054 }
1055
ufs_mtk_event_notify(struct ufs_hba * hba,enum ufs_event_type evt,void * data)1056 static void ufs_mtk_event_notify(struct ufs_hba *hba,
1057 enum ufs_event_type evt, void *data)
1058 {
1059 unsigned int val = *(u32 *)data;
1060
1061 trace_ufs_mtk_event(evt, val);
1062 }
1063
1064 /*
1065 * struct ufs_hba_mtk_vops - UFS MTK specific variant operations
1066 *
1067 * The variant operations configure the necessary controller and PHY
1068 * handshake during initialization.
1069 */
1070 static const struct ufs_hba_variant_ops ufs_hba_mtk_vops = {
1071 .name = "mediatek.ufshci",
1072 .init = ufs_mtk_init,
1073 .get_ufs_hci_version = ufs_mtk_get_ufs_hci_version,
1074 .setup_clocks = ufs_mtk_setup_clocks,
1075 .hce_enable_notify = ufs_mtk_hce_enable_notify,
1076 .link_startup_notify = ufs_mtk_link_startup_notify,
1077 .pwr_change_notify = ufs_mtk_pwr_change_notify,
1078 .apply_dev_quirks = ufs_mtk_apply_dev_quirks,
1079 .fixup_dev_quirks = ufs_mtk_fixup_dev_quirks,
1080 .suspend = ufs_mtk_suspend,
1081 .resume = ufs_mtk_resume,
1082 .dbg_register_dump = ufs_mtk_dbg_register_dump,
1083 .device_reset = ufs_mtk_device_reset,
1084 .event_notify = ufs_mtk_event_notify,
1085 };
1086
1087 /**
1088 * ufs_mtk_probe - probe routine of the driver
1089 * @pdev: pointer to Platform device handle
1090 *
1091 * Return zero for success and non-zero for failure
1092 */
ufs_mtk_probe(struct platform_device * pdev)1093 static int ufs_mtk_probe(struct platform_device *pdev)
1094 {
1095 int err;
1096 struct device *dev = &pdev->dev;
1097 struct device_node *reset_node;
1098 struct platform_device *reset_pdev;
1099 struct device_link *link;
1100
1101 reset_node = of_find_compatible_node(NULL, NULL,
1102 "ti,syscon-reset");
1103 if (!reset_node) {
1104 dev_notice(dev, "find ti,syscon-reset fail\n");
1105 goto skip_reset;
1106 }
1107 reset_pdev = of_find_device_by_node(reset_node);
1108 if (!reset_pdev) {
1109 dev_notice(dev, "find reset_pdev fail\n");
1110 goto skip_reset;
1111 }
1112 link = device_link_add(dev, &reset_pdev->dev,
1113 DL_FLAG_AUTOPROBE_CONSUMER);
1114 if (!link) {
1115 dev_notice(dev, "add reset device_link fail\n");
1116 goto skip_reset;
1117 }
1118 /* supplier is not probed */
1119 if (link->status == DL_STATE_DORMANT) {
1120 err = -EPROBE_DEFER;
1121 goto out;
1122 }
1123
1124 skip_reset:
1125 /* perform generic probe */
1126 err = ufshcd_pltfrm_init(pdev, &ufs_hba_mtk_vops);
1127
1128 out:
1129 if (err)
1130 dev_info(dev, "probe failed %d\n", err);
1131
1132 of_node_put(reset_node);
1133 return err;
1134 }
1135
1136 /**
1137 * ufs_mtk_remove - set driver_data of the device to NULL
1138 * @pdev: pointer to platform device handle
1139 *
1140 * Always return 0
1141 */
ufs_mtk_remove(struct platform_device * pdev)1142 static int ufs_mtk_remove(struct platform_device *pdev)
1143 {
1144 struct ufs_hba *hba = platform_get_drvdata(pdev);
1145
1146 pm_runtime_get_sync(&(pdev)->dev);
1147 ufshcd_remove(hba);
1148 return 0;
1149 }
1150
1151 static const struct dev_pm_ops ufs_mtk_pm_ops = {
1152 .suspend = ufshcd_pltfrm_suspend,
1153 .resume = ufshcd_pltfrm_resume,
1154 .runtime_suspend = ufshcd_pltfrm_runtime_suspend,
1155 .runtime_resume = ufshcd_pltfrm_runtime_resume,
1156 .runtime_idle = ufshcd_pltfrm_runtime_idle,
1157 };
1158
1159 static struct platform_driver ufs_mtk_pltform = {
1160 .probe = ufs_mtk_probe,
1161 .remove = ufs_mtk_remove,
1162 .shutdown = ufshcd_pltfrm_shutdown,
1163 .driver = {
1164 .name = "ufshcd-mtk",
1165 .pm = &ufs_mtk_pm_ops,
1166 .of_match_table = ufs_mtk_of_match,
1167 },
1168 };
1169
1170 MODULE_AUTHOR("Stanley Chu <stanley.chu@mediatek.com>");
1171 MODULE_AUTHOR("Peter Wang <peter.wang@mediatek.com>");
1172 MODULE_DESCRIPTION("MediaTek UFS Host Driver");
1173 MODULE_LICENSE("GPL v2");
1174
1175 module_platform_driver(ufs_mtk_pltform);
1176