1 // SPDX-License-Identifier: GPL-2.0-only
3 * UFS PHY driver for Samsung SoC
5 * Copyright (C) 2020 Samsung Electronics Co., Ltd.
6 * Author: Seungwon Jeon <essuuj@gmail.com>
7 * Author: Alim Akhtar <alim.akhtar@samsung.com>
10 #include <linux/clk.h>
11 #include <linux/delay.h>
12 #include <linux/err.h>
15 #include <linux/iopoll.h>
16 #include <linux/module.h>
17 #include <linux/phy/phy.h>
18 #include <linux/platform_device.h>
19 #include <linux/regmap.h>
20 #include <linux/soc/samsung/exynos-pmu.h>
22 #include "phy-samsung-ufs.h"
24 #define for_each_phy_lane(phy, i) \
25 for (i = 0; i < (phy)->lane_cnt; i++)
26 #define for_each_phy_cfg(cfg) \
27 for (; (cfg)->id; (cfg)++)
29 #define PHY_DEF_LANE_CNT 1
31 static void samsung_ufs_phy_config(struct samsung_ufs_phy *phy,
32 const struct samsung_ufs_phy_cfg *cfg,
35 enum {LANE_0, LANE_1}; /* lane index */
39 writel(cfg->val, (phy)->reg_pma + cfg->off_0);
42 if (cfg->id == PHY_TRSV_BLK)
43 writel(cfg->val, (phy)->reg_pma + cfg->off_1);
48 int samsung_ufs_phy_wait_for_lock_acq(struct phy *phy, u8 lane)
50 struct samsung_ufs_phy *ufs_phy = get_samsung_ufs_phy(phy);
51 const unsigned int timeout_us = 100000;
52 const unsigned int sleep_us = 10;
56 err = readl_poll_timeout(
57 ufs_phy->reg_pma + PHY_APB_ADDR(PHY_PLL_LOCK_STATUS),
58 val, (val & PHY_PLL_LOCK_BIT), sleep_us, timeout_us);
61 "failed to get phy pll lock acquisition %d\n", err);
65 err = readl_poll_timeout(
67 PHY_APB_ADDR(ufs_phy->drvdata->cdr_lock_status_offset),
68 val, (val & PHY_CDR_LOCK_BIT), sleep_us, timeout_us);
71 "failed to get phy cdr lock acquisition %d\n", err);
76 static int samsung_ufs_phy_calibrate(struct phy *phy)
78 struct samsung_ufs_phy *ufs_phy = get_samsung_ufs_phy(phy);
79 const struct samsung_ufs_phy_cfg * const *cfgs = ufs_phy->cfgs;
80 const struct samsung_ufs_phy_cfg *cfg;
84 if (unlikely(ufs_phy->ufs_phy_state < CFG_PRE_INIT ||
85 ufs_phy->ufs_phy_state >= CFG_TAG_MAX)) {
86 dev_err(ufs_phy->dev, "invalid phy config index %d\n", ufs_phy->ufs_phy_state);
90 cfg = cfgs[ufs_phy->ufs_phy_state];
94 for_each_phy_cfg(cfg) {
95 for_each_phy_lane(ufs_phy, i) {
96 samsung_ufs_phy_config(ufs_phy, cfg, i);
100 for_each_phy_lane(ufs_phy, i) {
101 if (ufs_phy->ufs_phy_state == CFG_PRE_INIT &&
102 ufs_phy->drvdata->wait_for_cal) {
103 err = ufs_phy->drvdata->wait_for_cal(phy, i);
108 if (ufs_phy->ufs_phy_state == CFG_POST_PWR_HS &&
109 ufs_phy->drvdata->wait_for_cdr) {
110 err = ufs_phy->drvdata->wait_for_cdr(phy, i);
117 * In Samsung ufshci, PHY need to be calibrated at different
118 * stages / state mainly before Linkstartup, after Linkstartup,
119 * before power mode change and after power mode change.
120 * Below state machine to make sure to calibrate PHY in each
121 * state. Here after configuring PHY in a given state, will
122 * change the state to next state so that next state phy
123 * calibration value can be programed
126 switch (ufs_phy->ufs_phy_state) {
128 ufs_phy->ufs_phy_state = CFG_POST_INIT;
131 ufs_phy->ufs_phy_state = CFG_PRE_PWR_HS;
134 ufs_phy->ufs_phy_state = CFG_POST_PWR_HS;
136 case CFG_POST_PWR_HS:
137 /* Change back to INIT state */
138 ufs_phy->ufs_phy_state = CFG_PRE_INIT;
141 dev_err(ufs_phy->dev, "wrong state for phy calibration\n");
147 static int samsung_ufs_phy_clks_init(struct samsung_ufs_phy *phy)
150 const struct samsung_ufs_phy_drvdata *drvdata = phy->drvdata;
151 int num_clks = drvdata->num_clks;
153 phy->clks = devm_kcalloc(phy->dev, num_clks, sizeof(*phy->clks),
158 for (i = 0; i < num_clks; i++)
159 phy->clks[i].id = drvdata->clk_list[i];
161 return devm_clk_bulk_get(phy->dev, num_clks, phy->clks);
164 static int samsung_ufs_phy_init(struct phy *phy)
166 struct samsung_ufs_phy *ss_phy = get_samsung_ufs_phy(phy);
168 ss_phy->lane_cnt = phy->attrs.bus_width;
169 ss_phy->ufs_phy_state = CFG_PRE_INIT;
174 static int samsung_ufs_phy_power_on(struct phy *phy)
176 struct samsung_ufs_phy *ss_phy = get_samsung_ufs_phy(phy);
179 samsung_ufs_phy_ctrl_isol(ss_phy, false);
181 ret = clk_bulk_prepare_enable(ss_phy->drvdata->num_clks, ss_phy->clks);
183 dev_err(ss_phy->dev, "failed to enable ufs phy clocks\n");
187 if (ss_phy->ufs_phy_state == CFG_PRE_INIT) {
188 ret = samsung_ufs_phy_calibrate(phy);
190 dev_err(ss_phy->dev, "ufs phy calibration failed\n");
196 static int samsung_ufs_phy_power_off(struct phy *phy)
198 struct samsung_ufs_phy *ss_phy = get_samsung_ufs_phy(phy);
200 clk_bulk_disable_unprepare(ss_phy->drvdata->num_clks, ss_phy->clks);
202 samsung_ufs_phy_ctrl_isol(ss_phy, true);
207 static int samsung_ufs_phy_set_mode(struct phy *generic_phy,
208 enum phy_mode mode, int submode)
210 struct samsung_ufs_phy *ss_phy = get_samsung_ufs_phy(generic_phy);
212 ss_phy->mode = PHY_MODE_INVALID;
220 static int samsung_ufs_phy_exit(struct phy *phy)
222 struct samsung_ufs_phy *ss_phy = get_samsung_ufs_phy(phy);
224 ss_phy->ufs_phy_state = CFG_TAG_MAX;
229 static const struct phy_ops samsung_ufs_phy_ops = {
230 .init = samsung_ufs_phy_init,
231 .exit = samsung_ufs_phy_exit,
232 .power_on = samsung_ufs_phy_power_on,
233 .power_off = samsung_ufs_phy_power_off,
234 .calibrate = samsung_ufs_phy_calibrate,
235 .set_mode = samsung_ufs_phy_set_mode,
236 .owner = THIS_MODULE,
239 static const struct of_device_id samsung_ufs_phy_match[];
241 static int samsung_ufs_phy_probe(struct platform_device *pdev)
243 struct device *dev = &pdev->dev;
244 const struct of_device_id *match;
245 struct samsung_ufs_phy *phy;
247 struct phy_provider *phy_provider;
248 const struct samsung_ufs_phy_drvdata *drvdata;
252 match = of_match_node(samsung_ufs_phy_match, dev->of_node);
255 dev_err(dev, "failed to get match_node\n");
259 phy = devm_kzalloc(dev, sizeof(*phy), GFP_KERNEL);
265 phy->reg_pma = devm_platform_ioremap_resource_byname(pdev, "phy-pma");
266 if (IS_ERR(phy->reg_pma)) {
267 err = PTR_ERR(phy->reg_pma);
271 phy->reg_pmu = exynos_get_pmu_regmap_by_phandle(dev->of_node,
272 "samsung,pmu-syscon");
273 if (IS_ERR(phy->reg_pmu)) {
274 err = PTR_ERR(phy->reg_pmu);
275 dev_err(dev, "failed syscon remap for pmu\n");
279 gen_phy = devm_phy_create(dev, NULL, &samsung_ufs_phy_ops);
280 if (IS_ERR(gen_phy)) {
281 err = PTR_ERR(gen_phy);
282 dev_err(dev, "failed to create PHY for ufs-phy\n");
286 drvdata = match->data;
288 phy->drvdata = drvdata;
289 phy->cfgs = drvdata->cfgs;
290 memcpy(&phy->isol, &drvdata->isol, sizeof(phy->isol));
292 if (!of_property_read_u32_index(dev->of_node, "samsung,pmu-syscon", 1,
294 phy->isol.offset = isol_offset;
296 phy->lane_cnt = PHY_DEF_LANE_CNT;
298 err = samsung_ufs_phy_clks_init(phy);
300 dev_err(dev, "failed to get phy clocks\n");
304 phy_set_drvdata(gen_phy, phy);
306 phy_provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
307 if (IS_ERR(phy_provider)) {
308 err = PTR_ERR(phy_provider);
309 dev_err(dev, "failed to register phy-provider\n");
316 static const struct of_device_id samsung_ufs_phy_match[] = {
318 .compatible = "google,gs101-ufs-phy",
319 .data = &tensor_gs101_ufs_phy,
321 .compatible = "samsung,exynos7-ufs-phy",
322 .data = &exynos7_ufs_phy,
324 .compatible = "samsung,exynosautov9-ufs-phy",
325 .data = &exynosautov9_ufs_phy,
327 .compatible = "tesla,fsd-ufs-phy",
328 .data = &fsd_ufs_phy,
332 MODULE_DEVICE_TABLE(of, samsung_ufs_phy_match);
334 static struct platform_driver samsung_ufs_phy_driver = {
335 .probe = samsung_ufs_phy_probe,
337 .name = "samsung-ufs-phy",
338 .of_match_table = samsung_ufs_phy_match,
341 module_platform_driver(samsung_ufs_phy_driver);
342 MODULE_DESCRIPTION("Samsung SoC UFS PHY Driver");
343 MODULE_AUTHOR("Seungwon Jeon <essuuj@gmail.com>");
344 MODULE_AUTHOR("Alim Akhtar <alim.akhtar@samsung.com>");
345 MODULE_LICENSE("GPL v2");