|
| 1 | +// SPDX-License-Identifier: GPL-2.0 |
| 2 | +/* |
| 3 | + * Platform UFS Host driver for Cadence controller |
| 4 | + * |
| 5 | + * Copyright (C) 2018 Cadence Design Systems, Inc. |
| 6 | + * |
| 7 | + * Authors: |
| 8 | + * Jan Kotas <jank@cadence.com> |
| 9 | + * |
| 10 | + */ |
| 11 | + |
| 12 | +#include <linux/kernel.h> |
| 13 | +#include <linux/module.h> |
| 14 | +#include <linux/platform_device.h> |
| 15 | +#include <linux/of.h> |
| 16 | +#include <linux/time.h> |
| 17 | + |
| 18 | +#include "ufshcd-pltfrm.h" |
| 19 | + |
| 20 | +#define CDNS_UFS_REG_HCLKDIV 0xFC |
| 21 | + |
| 22 | +/** |
| 23 | + * Sets HCLKDIV register value based on the core_clk |
| 24 | + * @hba: host controller instance |
| 25 | + * |
| 26 | + * Return zero for success and non-zero for failure |
| 27 | + */ |
| 28 | +static int cdns_ufs_set_hclkdiv(struct ufs_hba *hba) |
| 29 | +{ |
| 30 | + struct ufs_clk_info *clki; |
| 31 | + struct list_head *head = &hba->clk_list_head; |
| 32 | + unsigned long core_clk_rate = 0; |
| 33 | + u32 core_clk_div = 0; |
| 34 | + |
| 35 | + if (list_empty(head)) |
| 36 | + return 0; |
| 37 | + |
| 38 | + list_for_each_entry(clki, head, list) { |
| 39 | + if (IS_ERR_OR_NULL(clki->clk)) |
| 40 | + continue; |
| 41 | + if (!strcmp(clki->name, "core_clk")) |
| 42 | + core_clk_rate = clk_get_rate(clki->clk); |
| 43 | + } |
| 44 | + |
| 45 | + if (!core_clk_rate) { |
| 46 | + dev_err(hba->dev, "%s: unable to find core_clk rate\n", |
| 47 | + __func__); |
| 48 | + return -EINVAL; |
| 49 | + } |
| 50 | + |
| 51 | + core_clk_div = core_clk_rate / USEC_PER_SEC; |
| 52 | + |
| 53 | + ufshcd_writel(hba, core_clk_div, CDNS_UFS_REG_HCLKDIV); |
| 54 | + /** |
| 55 | + * Make sure the register was updated, |
| 56 | + * UniPro layer will not work with an incorrect value. |
| 57 | + */ |
| 58 | + mb(); |
| 59 | + |
| 60 | + return 0; |
| 61 | +} |
| 62 | + |
| 63 | +/** |
| 64 | + * Sets clocks used by the controller |
| 65 | + * @hba: host controller instance |
| 66 | + * @on: if true, enable clocks, otherwise disable |
| 67 | + * @status: notify stage (pre, post change) |
| 68 | + * |
| 69 | + * Return zero for success and non-zero for failure |
| 70 | + */ |
| 71 | +static int cdns_ufs_setup_clocks(struct ufs_hba *hba, bool on, |
| 72 | + enum ufs_notify_change_status status) |
| 73 | +{ |
| 74 | + if ((!on) || (status == PRE_CHANGE)) |
| 75 | + return 0; |
| 76 | + |
| 77 | + return cdns_ufs_set_hclkdiv(hba); |
| 78 | +} |
| 79 | + |
| 80 | +static struct ufs_hba_variant_ops cdns_pltfm_hba_vops = { |
| 81 | + .name = "cdns-ufs-pltfm", |
| 82 | + .setup_clocks = cdns_ufs_setup_clocks, |
| 83 | +}; |
| 84 | + |
| 85 | +/** |
| 86 | + * cdns_ufs_pltfrm_probe - probe routine of the driver |
| 87 | + * @pdev: pointer to platform device handle |
| 88 | + * |
| 89 | + * Return zero for success and non-zero for failure |
| 90 | + */ |
| 91 | +static int cdns_ufs_pltfrm_probe(struct platform_device *pdev) |
| 92 | +{ |
| 93 | + int err; |
| 94 | + struct device *dev = &pdev->dev; |
| 95 | + |
| 96 | + /* Perform generic probe */ |
| 97 | + err = ufshcd_pltfrm_init(pdev, &cdns_pltfm_hba_vops); |
| 98 | + if (err) |
| 99 | + dev_err(dev, "ufshcd_pltfrm_init() failed %d\n", err); |
| 100 | + |
| 101 | + return err; |
| 102 | +} |
| 103 | + |
| 104 | +/** |
| 105 | + * cdns_ufs_pltfrm_remove - removes the ufs driver |
| 106 | + * @pdev: pointer to platform device handle |
| 107 | + * |
| 108 | + * Always returns 0 |
| 109 | + */ |
| 110 | +static int cdns_ufs_pltfrm_remove(struct platform_device *pdev) |
| 111 | +{ |
| 112 | + struct ufs_hba *hba = platform_get_drvdata(pdev); |
| 113 | + |
| 114 | + ufshcd_remove(hba); |
| 115 | + return 0; |
| 116 | +} |
| 117 | + |
| 118 | +static const struct of_device_id cdns_ufs_of_match[] = { |
| 119 | + { .compatible = "cdns,ufshc" }, |
| 120 | + {}, |
| 121 | +}; |
| 122 | + |
| 123 | +MODULE_DEVICE_TABLE(of, cdns_ufs_of_match); |
| 124 | + |
| 125 | +static const struct dev_pm_ops cdns_ufs_dev_pm_ops = { |
| 126 | + .suspend = ufshcd_pltfrm_suspend, |
| 127 | + .resume = ufshcd_pltfrm_resume, |
| 128 | + .runtime_suspend = ufshcd_pltfrm_runtime_suspend, |
| 129 | + .runtime_resume = ufshcd_pltfrm_runtime_resume, |
| 130 | + .runtime_idle = ufshcd_pltfrm_runtime_idle, |
| 131 | +}; |
| 132 | + |
| 133 | +static struct platform_driver cdns_ufs_pltfrm_driver = { |
| 134 | + .probe = cdns_ufs_pltfrm_probe, |
| 135 | + .remove = cdns_ufs_pltfrm_remove, |
| 136 | + .driver = { |
| 137 | + .name = "cdns-ufshcd", |
| 138 | + .owner = THIS_MODULE, |
| 139 | + .pm = &cdns_ufs_dev_pm_ops, |
| 140 | + .of_match_table = cdns_ufs_of_match, |
| 141 | + }, |
| 142 | +}; |
| 143 | + |
| 144 | +module_platform_driver(cdns_ufs_pltfrm_driver); |
| 145 | + |
| 146 | +MODULE_AUTHOR("Jan Kotas <jank@cadence.com>"); |
| 147 | +MODULE_DESCRIPTION("Cadence UFS host controller platform driver"); |
| 148 | +MODULE_LICENSE("GPL v2"); |
| 149 | +MODULE_VERSION(UFSHCD_DRIVER_VERSION); |
0 commit comments