blob: d1f346937c5d04ec3ffbcfd93ec9bcfa3aef7259 [file] [log] [blame]
Faiz Abbasef5fb5a2019-10-15 18:24:37 +05301// SPDX-License-Identifier: GPL-2.0+
2/**
3 * cdns-platform.c - Platform driver for Cadence UFSHCI device
4 *
Nishanth Menona94a4072023-11-01 15:56:03 -05005 * Copyright (C) 2019 Texas Instruments Incorporated - https://www.ti.com
Faiz Abbasef5fb5a2019-10-15 18:24:37 +05306 */
7
8#include <clk.h>
9#include <common.h>
10#include <dm.h>
11#include <ufs.h>
Simon Glass98eb4ce2020-07-19 10:15:54 -060012#include <asm/io.h>
Simon Glass336d4612020-02-03 07:36:16 -070013#include <dm/device_compat.h>
Simon Glasscd93d622020-05-10 11:40:13 -060014#include <linux/bitops.h>
Simon Glass61b29b82020-02-03 07:36:15 -070015#include <linux/err.h>
Igor Prusov13248d62023-11-09 20:10:04 +030016#include <linux/time.h>
Faiz Abbasef5fb5a2019-10-15 18:24:37 +053017
18#include "ufs.h"
19
Faiz Abbasef5fb5a2019-10-15 18:24:37 +053020#define CDNS_UFS_REG_HCLKDIV 0xFC
21#define CDNS_UFS_REG_PHY_XCFGD1 0x113C
22
23static int cdns_ufs_link_startup_notify(struct ufs_hba *hba,
24 enum ufs_notify_change_status status)
25{
26 hba->quirks |= UFSHCD_QUIRK_BROKEN_LCC;
27 switch (status) {
28 case PRE_CHANGE:
29 return ufshcd_dme_set(hba,
30 UIC_ARG_MIB(PA_LOCAL_TX_LCC_ENABLE),
31 0);
32 case POST_CHANGE:
33 ;
34 }
35
36 return 0;
37}
38
39static int cdns_ufs_set_hclkdiv(struct ufs_hba *hba)
40{
41 struct clk clk;
42 unsigned long core_clk_rate = 0;
43 u32 core_clk_div = 0;
44 int ret;
45
46 ret = clk_get_by_name(hba->dev, "core_clk", &clk);
47 if (ret) {
48 dev_err(hba->dev, "failed to get core_clk clock\n");
49 return ret;
50 }
51
52 core_clk_rate = clk_get_rate(&clk);
53 if (IS_ERR_VALUE(core_clk_rate)) {
54 dev_err(hba->dev, "%s: unable to find core_clk rate\n",
55 __func__);
56 return core_clk_rate;
57 }
58
59 core_clk_div = core_clk_rate / USEC_PER_SEC;
60 ufshcd_writel(hba, core_clk_div, CDNS_UFS_REG_HCLKDIV);
61
62 return 0;
63}
64
65static int cdns_ufs_hce_enable_notify(struct ufs_hba *hba,
66 enum ufs_notify_change_status status)
67{
68 switch (status) {
69 case PRE_CHANGE:
70 return cdns_ufs_set_hclkdiv(hba);
71 case POST_CHANGE:
72 ;
73 }
74
75 return 0;
76}
77
78static int cdns_ufs_init(struct ufs_hba *hba)
79{
80 u32 data;
81
82 /* Increase RX_Advanced_Min_ActivateTime_Capability */
83 data = ufshcd_readl(hba, CDNS_UFS_REG_PHY_XCFGD1);
84 data |= BIT(24);
85 ufshcd_writel(hba, data, CDNS_UFS_REG_PHY_XCFGD1);
86
87 return 0;
88}
89
90static struct ufs_hba_ops cdns_pltfm_hba_ops = {
91 .init = cdns_ufs_init,
92 .hce_enable_notify = cdns_ufs_hce_enable_notify,
93 .link_startup_notify = cdns_ufs_link_startup_notify,
94};
95
96static int cdns_ufs_pltfm_probe(struct udevice *dev)
97{
98 int err = ufshcd_probe(dev, &cdns_pltfm_hba_ops);
99 if (err)
100 dev_err(dev, "ufshcd_probe() failed %d\n", err);
101
102 return err;
103}
104
105static int cdns_ufs_pltfm_bind(struct udevice *dev)
106{
107 struct udevice *scsi_dev;
108
109 return ufs_scsi_bind(dev, &scsi_dev);
110}
111
112static const struct udevice_id cdns_ufs_pltfm_ids[] = {
113 {
114 .compatible = "cdns,ufshc-m31-16nm",
115 },
116 {},
117};
118
119U_BOOT_DRIVER(cdns_ufs_pltfm) = {
120 .name = "cdns-ufs-pltfm",
Marek Vasute4e01f82023-08-06 20:18:42 +0200121 .id = UCLASS_UFS,
Faiz Abbasef5fb5a2019-10-15 18:24:37 +0530122 .of_match = cdns_ufs_pltfm_ids,
123 .probe = cdns_ufs_pltfm_probe,
124 .bind = cdns_ufs_pltfm_bind,
125};