1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Universal Flash Storage Host controller PCI glue driver
5 * Copyright (C) 2011-2013 Samsung India Software Operations
8 * Santosh Yaraganavi <santosh.sy@samsung.com>
9 * Vinayak Holikatti <h.vinayak@samsung.com>
12 #include <ufs/ufshcd.h>
13 #include <linux/delay.h>
14 #include <linux/module.h>
15 #include <linux/pci.h>
16 #include <linux/pm_runtime.h>
17 #include <linux/pm_qos.h>
18 #include <linux/debugfs.h>
19 #include <linux/uuid.h>
20 #include <linux/acpi.h>
21 #include <linux/gpio/consumer.h>
24 void (*late_init)(struct ufs_hba *hba);
27 enum intel_ufs_dsm_func_id {
33 struct ufs_host ufs_host;
37 struct dentry *debugfs_root;
38 struct gpio_desc *reset_gpio;
41 static const guid_t intel_dsm_guid =
42 GUID_INIT(0x1A4832A0, 0x7D03, 0x43CA,
43 0xB0, 0x20, 0xF6, 0xDC, 0xD1, 0x2A, 0x19, 0x50);
45 static bool __intel_dsm_supported(struct intel_host *host,
46 enum intel_ufs_dsm_func_id fn)
48 return fn < 32 && fn >= 0 && (host->dsm_fns & (1u << fn));
51 #define INTEL_DSM_SUPPORTED(host, name) \
52 __intel_dsm_supported(host, INTEL_DSM_##name)
54 static int __intel_dsm(struct intel_host *intel_host, struct device *dev,
55 unsigned int fn, u32 *result)
57 union acpi_object *obj;
61 obj = acpi_evaluate_dsm_typed(ACPI_HANDLE(dev), &intel_dsm_guid, 0, fn, NULL,
66 if (obj->buffer.length < 1) {
71 len = min_t(size_t, obj->buffer.length, 4);
74 memcpy(result, obj->buffer.pointer, len);
81 static int intel_dsm(struct intel_host *intel_host, struct device *dev,
82 unsigned int fn, u32 *result)
84 if (!__intel_dsm_supported(intel_host, fn))
87 return __intel_dsm(intel_host, dev, fn, result);
90 static void intel_dsm_init(struct intel_host *intel_host, struct device *dev)
94 err = __intel_dsm(intel_host, dev, INTEL_DSM_FNS, &intel_host->dsm_fns);
95 dev_dbg(dev, "DSM fns %#x, error %d\n", intel_host->dsm_fns, err);
98 static int ufs_intel_hce_enable_notify(struct ufs_hba *hba,
99 enum ufs_notify_change_status status)
101 /* Cannot enable ICE until after HC enable */
102 if (status == POST_CHANGE && hba->caps & UFSHCD_CAP_CRYPTO) {
103 u32 hce = ufshcd_readl(hba, REG_CONTROLLER_ENABLE);
105 hce |= CRYPTO_GENERAL_ENABLE;
106 ufshcd_writel(hba, hce, REG_CONTROLLER_ENABLE);
112 static int ufs_intel_disable_lcc(struct ufs_hba *hba)
114 u32 attr = UIC_ARG_MIB(PA_LOCAL_TX_LCC_ENABLE);
117 ufshcd_dme_get(hba, attr, &lcc_enable);
119 ufshcd_disable_host_tx_lcc(hba);
124 static int ufs_intel_link_startup_notify(struct ufs_hba *hba,
125 enum ufs_notify_change_status status)
131 err = ufs_intel_disable_lcc(hba);
142 static int ufs_intel_set_lanes(struct ufs_hba *hba, u32 lanes)
144 struct ufs_pa_layer_attr pwr_info = hba->pwr_info;
147 pwr_info.lane_rx = lanes;
148 pwr_info.lane_tx = lanes;
149 ret = ufshcd_config_pwr_mode(hba, &pwr_info);
151 dev_err(hba->dev, "%s: Setting %u lanes, err = %d\n",
152 __func__, lanes, ret);
156 static int ufs_intel_lkf_pwr_change_notify(struct ufs_hba *hba,
157 enum ufs_notify_change_status status,
158 struct ufs_pa_layer_attr *dev_max_params,
159 struct ufs_pa_layer_attr *dev_req_params)
165 if (ufshcd_is_hs_mode(dev_max_params) &&
166 (hba->pwr_info.lane_rx != 2 || hba->pwr_info.lane_tx != 2))
167 ufs_intel_set_lanes(hba, 2);
168 memcpy(dev_req_params, dev_max_params, sizeof(*dev_req_params));
171 if (ufshcd_is_hs_mode(dev_req_params)) {
172 u32 peer_granularity;
174 usleep_range(1000, 1250);
175 err = ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_GRANULARITY),
186 static int ufs_intel_lkf_apply_dev_quirks(struct ufs_hba *hba)
188 u32 granularity, peer_granularity;
189 u32 pa_tactivate, peer_pa_tactivate;
192 ret = ufshcd_dme_get(hba, UIC_ARG_MIB(PA_GRANULARITY), &granularity);
196 ret = ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_GRANULARITY), &peer_granularity);
200 ret = ufshcd_dme_get(hba, UIC_ARG_MIB(PA_TACTIVATE), &pa_tactivate);
204 ret = ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_TACTIVATE), &peer_pa_tactivate);
208 if (granularity == peer_granularity) {
209 u32 new_peer_pa_tactivate = pa_tactivate + 2;
211 ret = ufshcd_dme_peer_set(hba, UIC_ARG_MIB(PA_TACTIVATE), new_peer_pa_tactivate);
217 #define INTEL_ACTIVELTR 0x804
218 #define INTEL_IDLELTR 0x808
220 #define INTEL_LTR_REQ BIT(15)
221 #define INTEL_LTR_SCALE_MASK GENMASK(11, 10)
222 #define INTEL_LTR_SCALE_1US (2 << 10)
223 #define INTEL_LTR_SCALE_32US (3 << 10)
224 #define INTEL_LTR_VALUE_MASK GENMASK(9, 0)
226 static void intel_cache_ltr(struct ufs_hba *hba)
228 struct intel_host *host = ufshcd_get_variant(hba);
230 host->active_ltr = readl(hba->mmio_base + INTEL_ACTIVELTR);
231 host->idle_ltr = readl(hba->mmio_base + INTEL_IDLELTR);
234 static void intel_ltr_set(struct device *dev, s32 val)
236 struct ufs_hba *hba = dev_get_drvdata(dev);
237 struct intel_host *host = ufshcd_get_variant(hba);
240 pm_runtime_get_sync(dev);
243 * Program latency tolerance (LTR) accordingly what has been asked
244 * by the PM QoS layer or disable it in case we were passed
245 * negative value or PM_QOS_LATENCY_ANY.
247 ltr = readl(hba->mmio_base + INTEL_ACTIVELTR);
249 if (val == PM_QOS_LATENCY_ANY || val < 0) {
250 ltr &= ~INTEL_LTR_REQ;
252 ltr |= INTEL_LTR_REQ;
253 ltr &= ~INTEL_LTR_SCALE_MASK;
254 ltr &= ~INTEL_LTR_VALUE_MASK;
256 if (val > INTEL_LTR_VALUE_MASK) {
258 if (val > INTEL_LTR_VALUE_MASK)
259 val = INTEL_LTR_VALUE_MASK;
260 ltr |= INTEL_LTR_SCALE_32US | val;
262 ltr |= INTEL_LTR_SCALE_1US | val;
266 if (ltr == host->active_ltr)
269 writel(ltr, hba->mmio_base + INTEL_ACTIVELTR);
270 writel(ltr, hba->mmio_base + INTEL_IDLELTR);
272 /* Cache the values into intel_host structure */
273 intel_cache_ltr(hba);
278 static void intel_ltr_expose(struct device *dev)
280 dev->power.set_latency_tolerance = intel_ltr_set;
281 dev_pm_qos_expose_latency_tolerance(dev);
284 static void intel_ltr_hide(struct device *dev)
286 dev_pm_qos_hide_latency_tolerance(dev);
287 dev->power.set_latency_tolerance = NULL;
290 static void intel_add_debugfs(struct ufs_hba *hba)
292 struct dentry *dir = debugfs_create_dir(dev_name(hba->dev), NULL);
293 struct intel_host *host = ufshcd_get_variant(hba);
295 intel_cache_ltr(hba);
297 host->debugfs_root = dir;
298 debugfs_create_x32("active_ltr", 0444, dir, &host->active_ltr);
299 debugfs_create_x32("idle_ltr", 0444, dir, &host->idle_ltr);
302 static void intel_remove_debugfs(struct ufs_hba *hba)
304 struct intel_host *host = ufshcd_get_variant(hba);
306 debugfs_remove_recursive(host->debugfs_root);
309 static int ufs_intel_device_reset(struct ufs_hba *hba)
311 struct intel_host *host = ufshcd_get_variant(hba);
313 if (INTEL_DSM_SUPPORTED(host, RESET)) {
317 err = intel_dsm(host, hba->dev, INTEL_DSM_RESET, &result);
321 dev_err(hba->dev, "%s: DSM error %d result %u\n",
322 __func__, err, result);
326 if (!host->reset_gpio)
329 gpiod_set_value_cansleep(host->reset_gpio, 1);
330 usleep_range(10, 15);
332 gpiod_set_value_cansleep(host->reset_gpio, 0);
333 usleep_range(10, 15);
338 static struct gpio_desc *ufs_intel_get_reset_gpio(struct device *dev)
340 /* GPIO in _DSD has active low setting */
341 return devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_LOW);
344 static int ufs_intel_common_init(struct ufs_hba *hba)
346 struct intel_host *host;
348 hba->caps |= UFSHCD_CAP_RPM_AUTOSUSPEND;
350 host = devm_kzalloc(hba->dev, sizeof(*host), GFP_KERNEL);
353 ufshcd_set_variant(hba, host);
354 intel_dsm_init(host, hba->dev);
355 if (INTEL_DSM_SUPPORTED(host, RESET)) {
356 if (hba->vops->device_reset)
357 hba->caps |= UFSHCD_CAP_DEEPSLEEP;
359 if (hba->vops->device_reset)
360 host->reset_gpio = ufs_intel_get_reset_gpio(hba->dev);
361 if (IS_ERR(host->reset_gpio)) {
362 dev_err(hba->dev, "%s: failed to get reset GPIO, error %ld\n",
363 __func__, PTR_ERR(host->reset_gpio));
364 host->reset_gpio = NULL;
366 if (host->reset_gpio) {
367 gpiod_set_value_cansleep(host->reset_gpio, 0);
368 hba->caps |= UFSHCD_CAP_DEEPSLEEP;
371 intel_ltr_expose(hba->dev);
372 intel_add_debugfs(hba);
376 static void ufs_intel_common_exit(struct ufs_hba *hba)
378 intel_remove_debugfs(hba);
379 intel_ltr_hide(hba->dev);
382 static int ufs_intel_resume(struct ufs_hba *hba, enum ufs_pm_op op)
384 if (ufshcd_is_link_hibern8(hba)) {
385 int ret = ufshcd_uic_hibern8_exit(hba);
388 ufshcd_set_link_active(hba);
390 dev_err(hba->dev, "%s: hibern8 exit failed %d\n",
393 * Force reset and restore. Any other actions can lead
394 * to an unrecoverable state.
396 ufshcd_set_link_off(hba);
403 static int ufs_intel_ehl_init(struct ufs_hba *hba)
405 hba->quirks |= UFSHCD_QUIRK_BROKEN_AUTO_HIBERN8;
406 return ufs_intel_common_init(hba);
409 static void ufs_intel_lkf_late_init(struct ufs_hba *hba)
411 /* LKF always needs a full reset, so set PM accordingly */
412 if (hba->caps & UFSHCD_CAP_DEEPSLEEP) {
413 hba->spm_lvl = UFS_PM_LVL_6;
414 hba->rpm_lvl = UFS_PM_LVL_6;
416 hba->spm_lvl = UFS_PM_LVL_5;
417 hba->rpm_lvl = UFS_PM_LVL_5;
421 static int ufs_intel_lkf_init(struct ufs_hba *hba)
423 struct ufs_host *ufs_host;
426 hba->nop_out_timeout = 200;
427 hba->quirks |= UFSHCD_QUIRK_BROKEN_AUTO_HIBERN8;
428 hba->caps |= UFSHCD_CAP_CRYPTO;
429 err = ufs_intel_common_init(hba);
430 ufs_host = ufshcd_get_variant(hba);
431 ufs_host->late_init = ufs_intel_lkf_late_init;
435 static int ufs_intel_adl_init(struct ufs_hba *hba)
437 hba->nop_out_timeout = 200;
438 hba->quirks |= UFSHCD_QUIRK_BROKEN_AUTO_HIBERN8;
439 hba->caps |= UFSHCD_CAP_WB_EN;
440 return ufs_intel_common_init(hba);
443 static int ufs_intel_mtl_init(struct ufs_hba *hba)
445 hba->caps |= UFSHCD_CAP_CRYPTO | UFSHCD_CAP_WB_EN;
446 return ufs_intel_common_init(hba);
449 static struct ufs_hba_variant_ops ufs_intel_cnl_hba_vops = {
451 .init = ufs_intel_common_init,
452 .exit = ufs_intel_common_exit,
453 .link_startup_notify = ufs_intel_link_startup_notify,
454 .resume = ufs_intel_resume,
457 static struct ufs_hba_variant_ops ufs_intel_ehl_hba_vops = {
459 .init = ufs_intel_ehl_init,
460 .exit = ufs_intel_common_exit,
461 .link_startup_notify = ufs_intel_link_startup_notify,
462 .resume = ufs_intel_resume,
465 static struct ufs_hba_variant_ops ufs_intel_lkf_hba_vops = {
467 .init = ufs_intel_lkf_init,
468 .exit = ufs_intel_common_exit,
469 .hce_enable_notify = ufs_intel_hce_enable_notify,
470 .link_startup_notify = ufs_intel_link_startup_notify,
471 .pwr_change_notify = ufs_intel_lkf_pwr_change_notify,
472 .apply_dev_quirks = ufs_intel_lkf_apply_dev_quirks,
473 .resume = ufs_intel_resume,
474 .device_reset = ufs_intel_device_reset,
477 static struct ufs_hba_variant_ops ufs_intel_adl_hba_vops = {
479 .init = ufs_intel_adl_init,
480 .exit = ufs_intel_common_exit,
481 .link_startup_notify = ufs_intel_link_startup_notify,
482 .resume = ufs_intel_resume,
483 .device_reset = ufs_intel_device_reset,
486 static struct ufs_hba_variant_ops ufs_intel_mtl_hba_vops = {
488 .init = ufs_intel_mtl_init,
489 .exit = ufs_intel_common_exit,
490 .hce_enable_notify = ufs_intel_hce_enable_notify,
491 .link_startup_notify = ufs_intel_link_startup_notify,
492 .resume = ufs_intel_resume,
493 .device_reset = ufs_intel_device_reset,
496 #ifdef CONFIG_PM_SLEEP
497 static int ufshcd_pci_restore(struct device *dev)
499 struct ufs_hba *hba = dev_get_drvdata(dev);
501 /* Force a full reset and restore */
502 ufshcd_set_link_off(hba);
504 return ufshcd_system_resume(dev);
509 * ufshcd_pci_remove - de-allocate PCI/SCSI host and host memory space
510 * data structure memory
511 * @pdev: pointer to PCI handle
513 static void ufshcd_pci_remove(struct pci_dev *pdev)
515 struct ufs_hba *hba = pci_get_drvdata(pdev);
517 pm_runtime_forbid(&pdev->dev);
518 pm_runtime_get_noresume(&pdev->dev);
520 ufshcd_dealloc_host(hba);
524 * ufshcd_pci_probe - probe routine of the driver
525 * @pdev: pointer to PCI device handle
528 * Return: 0 on success, non-zero value on failure.
531 ufshcd_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
533 struct ufs_host *ufs_host;
535 void __iomem *mmio_base;
538 err = pcim_enable_device(pdev);
540 dev_err(&pdev->dev, "pcim_enable_device failed\n");
544 pci_set_master(pdev);
546 err = pcim_iomap_regions(pdev, 1 << 0, UFSHCD);
548 dev_err(&pdev->dev, "request and iomap failed\n");
552 mmio_base = pcim_iomap_table(pdev)[0];
554 err = ufshcd_alloc_host(&pdev->dev, &hba);
556 dev_err(&pdev->dev, "Allocation failed\n");
560 hba->vops = (struct ufs_hba_variant_ops *)id->driver_data;
562 err = ufshcd_init(hba, mmio_base, pdev->irq);
564 dev_err(&pdev->dev, "Initialization failed\n");
565 ufshcd_dealloc_host(hba);
569 ufs_host = ufshcd_get_variant(hba);
570 if (ufs_host && ufs_host->late_init)
571 ufs_host->late_init(hba);
573 pm_runtime_put_noidle(&pdev->dev);
574 pm_runtime_allow(&pdev->dev);
579 static const struct dev_pm_ops ufshcd_pci_pm_ops = {
580 SET_RUNTIME_PM_OPS(ufshcd_runtime_suspend, ufshcd_runtime_resume, NULL)
581 #ifdef CONFIG_PM_SLEEP
582 .suspend = ufshcd_system_suspend,
583 .resume = ufshcd_system_resume,
584 .freeze = ufshcd_system_suspend,
585 .thaw = ufshcd_system_resume,
586 .poweroff = ufshcd_system_suspend,
587 .restore = ufshcd_pci_restore,
588 .prepare = ufshcd_suspend_prepare,
589 .complete = ufshcd_resume_complete,
593 static const struct pci_device_id ufshcd_pci_tbl[] = {
594 { PCI_VENDOR_ID_REDHAT, 0x0013, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
595 { PCI_VENDOR_ID_SAMSUNG, 0xC00C, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
596 { PCI_VDEVICE(INTEL, 0x9DFA), (kernel_ulong_t)&ufs_intel_cnl_hba_vops },
597 { PCI_VDEVICE(INTEL, 0x4B41), (kernel_ulong_t)&ufs_intel_ehl_hba_vops },
598 { PCI_VDEVICE(INTEL, 0x4B43), (kernel_ulong_t)&ufs_intel_ehl_hba_vops },
599 { PCI_VDEVICE(INTEL, 0x98FA), (kernel_ulong_t)&ufs_intel_lkf_hba_vops },
600 { PCI_VDEVICE(INTEL, 0x51FF), (kernel_ulong_t)&ufs_intel_adl_hba_vops },
601 { PCI_VDEVICE(INTEL, 0x54FF), (kernel_ulong_t)&ufs_intel_adl_hba_vops },
602 { PCI_VDEVICE(INTEL, 0x7E47), (kernel_ulong_t)&ufs_intel_mtl_hba_vops },
603 { PCI_VDEVICE(INTEL, 0xA847), (kernel_ulong_t)&ufs_intel_mtl_hba_vops },
604 { PCI_VDEVICE(INTEL, 0x7747), (kernel_ulong_t)&ufs_intel_mtl_hba_vops },
605 { } /* terminate list */
608 MODULE_DEVICE_TABLE(pci, ufshcd_pci_tbl);
610 static struct pci_driver ufshcd_pci_driver = {
612 .id_table = ufshcd_pci_tbl,
613 .probe = ufshcd_pci_probe,
614 .remove = ufshcd_pci_remove,
616 .pm = &ufshcd_pci_pm_ops
620 module_pci_driver(ufshcd_pci_driver);
622 MODULE_AUTHOR("Santosh Yaragnavi <santosh.sy@samsung.com>");
623 MODULE_AUTHOR("Vinayak Holikatti <h.vinayak@samsung.com>");
624 MODULE_DESCRIPTION("UFS host controller PCI glue driver");
625 MODULE_LICENSE("GPL");