dm: core: Require users of devres to include the header
[oweals/u-boot.git] / drivers / ufs / cdns-platform.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /**
3  * cdns-platform.c - Platform driver for Cadence UFSHCI device
4  *
5  * Copyright (C) 2019 Texas Instruments Incorporated - http://www.ti.com
6  */
7
8 #include <clk.h>
9 #include <common.h>
10 #include <dm.h>
11 #include <ufs.h>
12 #include <linux/err.h>
13
14 #include "ufs.h"
15
16 #define USEC_PER_SEC    1000000L
17
18 #define CDNS_UFS_REG_HCLKDIV    0xFC
19 #define CDNS_UFS_REG_PHY_XCFGD1 0x113C
20
21 static int cdns_ufs_link_startup_notify(struct ufs_hba *hba,
22                                         enum ufs_notify_change_status status)
23 {
24         hba->quirks |= UFSHCD_QUIRK_BROKEN_LCC;
25         switch (status) {
26         case PRE_CHANGE:
27                 return ufshcd_dme_set(hba,
28                                       UIC_ARG_MIB(PA_LOCAL_TX_LCC_ENABLE),
29                                       0);
30         case POST_CHANGE:
31         ;
32         }
33
34         return 0;
35 }
36
37 static int cdns_ufs_set_hclkdiv(struct ufs_hba *hba)
38 {
39         struct clk clk;
40         unsigned long core_clk_rate = 0;
41         u32 core_clk_div = 0;
42         int ret;
43
44         ret = clk_get_by_name(hba->dev, "core_clk", &clk);
45         if (ret) {
46                 dev_err(hba->dev, "failed to get core_clk clock\n");
47                 return ret;
48         }
49
50         core_clk_rate = clk_get_rate(&clk);
51         if (IS_ERR_VALUE(core_clk_rate)) {
52                 dev_err(hba->dev, "%s: unable to find core_clk rate\n",
53                         __func__);
54                 return core_clk_rate;
55         }
56
57         core_clk_div = core_clk_rate / USEC_PER_SEC;
58         ufshcd_writel(hba, core_clk_div, CDNS_UFS_REG_HCLKDIV);
59
60         return 0;
61 }
62
63 static int cdns_ufs_hce_enable_notify(struct ufs_hba *hba,
64                                       enum ufs_notify_change_status status)
65 {
66         switch (status) {
67         case PRE_CHANGE:
68                 return cdns_ufs_set_hclkdiv(hba);
69         case POST_CHANGE:
70         ;
71         }
72
73         return 0;
74 }
75
76 static int cdns_ufs_init(struct ufs_hba *hba)
77 {
78         u32 data;
79
80         /* Increase RX_Advanced_Min_ActivateTime_Capability */
81         data = ufshcd_readl(hba, CDNS_UFS_REG_PHY_XCFGD1);
82         data |= BIT(24);
83         ufshcd_writel(hba, data, CDNS_UFS_REG_PHY_XCFGD1);
84
85         return 0;
86 }
87
88 static struct ufs_hba_ops cdns_pltfm_hba_ops = {
89         .init = cdns_ufs_init,
90         .hce_enable_notify = cdns_ufs_hce_enable_notify,
91         .link_startup_notify = cdns_ufs_link_startup_notify,
92 };
93
94 static int cdns_ufs_pltfm_probe(struct udevice *dev)
95 {
96         int err = ufshcd_probe(dev, &cdns_pltfm_hba_ops);
97         if (err)
98                 dev_err(dev, "ufshcd_probe() failed %d\n", err);
99
100         return err;
101 }
102
103 static int cdns_ufs_pltfm_bind(struct udevice *dev)
104 {
105         struct udevice *scsi_dev;
106
107         return ufs_scsi_bind(dev, &scsi_dev);
108 }
109
110 static const struct udevice_id cdns_ufs_pltfm_ids[] = {
111         {
112                 .compatible = "cdns,ufshc-m31-16nm",
113         },
114         {},
115 };
116
117 U_BOOT_DRIVER(cdns_ufs_pltfm) = {
118         .name           = "cdns-ufs-pltfm",
119         .id             =  UCLASS_UFS,
120         .of_match       = cdns_ufs_pltfm_ids,
121         .probe          = cdns_ufs_pltfm_probe,
122         .bind           = cdns_ufs_pltfm_bind,
123 };