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/mfd/syscon.h>
17 #include <linux/module.h>
18 #include <linux/phy/phy.h>
19 #include <linux/platform_device.h>
20 #include <linux/regmap.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 static int samsung_ufs_phy_wait_for_lock_acq(struct phy *phy)
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(
66 ufs_phy->reg_pma + PHY_APB_ADDR(PHY_CDR_LOCK_STATUS),
67 val, (val & PHY_CDR_LOCK_BIT), sleep_us, timeout_us);
70 "failed to get phy cdr lock acquisition %d\n", err);
75 static int samsung_ufs_phy_calibrate(struct phy *phy)
77 struct samsung_ufs_phy *ufs_phy = get_samsung_ufs_phy(phy);
78 struct samsung_ufs_phy_cfg **cfgs = ufs_phy->cfg;
79 const struct samsung_ufs_phy_cfg *cfg;
83 if (unlikely(ufs_phy->ufs_phy_state < CFG_PRE_INIT ||
84 ufs_phy->ufs_phy_state >= CFG_TAG_MAX)) {
85 dev_err(ufs_phy->dev, "invalid phy config index %d\n", ufs_phy->ufs_phy_state);
89 cfg = cfgs[ufs_phy->ufs_phy_state];
93 for_each_phy_cfg(cfg) {
94 for_each_phy_lane(ufs_phy, i) {
95 samsung_ufs_phy_config(ufs_phy, cfg, i);
99 if (ufs_phy->ufs_phy_state == CFG_POST_PWR_HS)
100 err = samsung_ufs_phy_wait_for_lock_acq(phy);
103 * In Samsung ufshci, PHY need to be calibrated at different
104 * stages / state mainly before Linkstartup, after Linkstartup,
105 * before power mode change and after power mode change.
106 * Below state machine to make sure to calibrate PHY in each
107 * state. Here after configuring PHY in a given state, will
108 * change the state to next state so that next state phy
109 * calibration value can be programed
112 switch (ufs_phy->ufs_phy_state) {
114 ufs_phy->ufs_phy_state = CFG_POST_INIT;
117 ufs_phy->ufs_phy_state = CFG_PRE_PWR_HS;
120 ufs_phy->ufs_phy_state = CFG_POST_PWR_HS;
122 case CFG_POST_PWR_HS:
123 /* Change back to INIT state */
124 ufs_phy->ufs_phy_state = CFG_PRE_INIT;
127 dev_err(ufs_phy->dev, "wrong state for phy calibration\n");
133 static int samsung_ufs_phy_symbol_clk_init(struct samsung_ufs_phy *phy)
137 phy->tx0_symbol_clk = devm_clk_get(phy->dev, "tx0_symbol_clk");
138 if (IS_ERR(phy->tx0_symbol_clk)) {
139 dev_err(phy->dev, "failed to get tx0_symbol_clk clock\n");
140 return PTR_ERR(phy->tx0_symbol_clk);
143 phy->rx0_symbol_clk = devm_clk_get(phy->dev, "rx0_symbol_clk");
144 if (IS_ERR(phy->rx0_symbol_clk)) {
145 dev_err(phy->dev, "failed to get rx0_symbol_clk clock\n");
146 return PTR_ERR(phy->rx0_symbol_clk);
149 phy->rx1_symbol_clk = devm_clk_get(phy->dev, "rx1_symbol_clk");
150 if (IS_ERR(phy->rx1_symbol_clk)) {
151 dev_err(phy->dev, "failed to get rx1_symbol_clk clock\n");
152 return PTR_ERR(phy->rx1_symbol_clk);
155 ret = clk_prepare_enable(phy->tx0_symbol_clk);
157 dev_err(phy->dev, "%s: tx0_symbol_clk enable failed %d\n", __func__, ret);
161 ret = clk_prepare_enable(phy->rx0_symbol_clk);
163 dev_err(phy->dev, "%s: rx0_symbol_clk enable failed %d\n", __func__, ret);
164 goto out_disable_tx0_clk;
167 ret = clk_prepare_enable(phy->rx1_symbol_clk);
169 dev_err(phy->dev, "%s: rx1_symbol_clk enable failed %d\n", __func__, ret);
170 goto out_disable_rx0_clk;
176 clk_disable_unprepare(phy->rx0_symbol_clk);
178 clk_disable_unprepare(phy->tx0_symbol_clk);
183 static int samsung_ufs_phy_clks_init(struct samsung_ufs_phy *phy)
187 phy->ref_clk = devm_clk_get(phy->dev, "ref_clk");
188 if (IS_ERR(phy->ref_clk))
189 dev_err(phy->dev, "failed to get ref_clk clock\n");
191 ret = clk_prepare_enable(phy->ref_clk);
193 dev_err(phy->dev, "%s: ref_clk enable failed %d\n", __func__, ret);
197 dev_dbg(phy->dev, "UFS MPHY ref_clk_rate = %ld\n", clk_get_rate(phy->ref_clk));
202 static int samsung_ufs_phy_init(struct phy *phy)
204 struct samsung_ufs_phy *ss_phy = get_samsung_ufs_phy(phy);
207 ss_phy->lane_cnt = phy->attrs.bus_width;
208 ss_phy->ufs_phy_state = CFG_PRE_INIT;
210 if (ss_phy->drvdata->has_symbol_clk) {
211 ret = samsung_ufs_phy_symbol_clk_init(ss_phy);
213 dev_err(ss_phy->dev, "failed to set ufs phy symbol clocks\n");
216 ret = samsung_ufs_phy_clks_init(ss_phy);
218 dev_err(ss_phy->dev, "failed to set ufs phy clocks\n");
220 ret = samsung_ufs_phy_calibrate(phy);
222 dev_err(ss_phy->dev, "ufs phy calibration failed\n");
227 static int samsung_ufs_phy_power_on(struct phy *phy)
229 struct samsung_ufs_phy *ss_phy = get_samsung_ufs_phy(phy);
231 samsung_ufs_phy_ctrl_isol(ss_phy, false);
235 static int samsung_ufs_phy_power_off(struct phy *phy)
237 struct samsung_ufs_phy *ss_phy = get_samsung_ufs_phy(phy);
239 samsung_ufs_phy_ctrl_isol(ss_phy, true);
243 static int samsung_ufs_phy_set_mode(struct phy *generic_phy,
244 enum phy_mode mode, int submode)
246 struct samsung_ufs_phy *ss_phy = get_samsung_ufs_phy(generic_phy);
248 ss_phy->mode = PHY_MODE_INVALID;
256 static int samsung_ufs_phy_exit(struct phy *phy)
258 struct samsung_ufs_phy *ss_phy = get_samsung_ufs_phy(phy);
260 clk_disable_unprepare(ss_phy->ref_clk);
262 if (ss_phy->drvdata->has_symbol_clk) {
263 clk_disable_unprepare(ss_phy->tx0_symbol_clk);
264 clk_disable_unprepare(ss_phy->rx0_symbol_clk);
265 clk_disable_unprepare(ss_phy->rx1_symbol_clk);
271 static const struct phy_ops samsung_ufs_phy_ops = {
272 .init = samsung_ufs_phy_init,
273 .exit = samsung_ufs_phy_exit,
274 .power_on = samsung_ufs_phy_power_on,
275 .power_off = samsung_ufs_phy_power_off,
276 .calibrate = samsung_ufs_phy_calibrate,
277 .set_mode = samsung_ufs_phy_set_mode,
278 .owner = THIS_MODULE,
281 static const struct of_device_id samsung_ufs_phy_match[];
283 static int samsung_ufs_phy_probe(struct platform_device *pdev)
285 struct device *dev = &pdev->dev;
286 const struct of_device_id *match;
287 struct samsung_ufs_phy *phy;
289 struct phy_provider *phy_provider;
290 const struct samsung_ufs_phy_drvdata *drvdata;
293 match = of_match_node(samsung_ufs_phy_match, dev->of_node);
296 dev_err(dev, "failed to get match_node\n");
300 phy = devm_kzalloc(dev, sizeof(*phy), GFP_KERNEL);
306 phy->reg_pma = devm_platform_ioremap_resource_byname(pdev, "phy-pma");
307 if (IS_ERR(phy->reg_pma)) {
308 err = PTR_ERR(phy->reg_pma);
312 phy->reg_pmu = syscon_regmap_lookup_by_phandle(
313 dev->of_node, "samsung,pmu-syscon");
314 if (IS_ERR(phy->reg_pmu)) {
315 err = PTR_ERR(phy->reg_pmu);
316 dev_err(dev, "failed syscon remap for pmu\n");
320 gen_phy = devm_phy_create(dev, NULL, &samsung_ufs_phy_ops);
321 if (IS_ERR(gen_phy)) {
322 err = PTR_ERR(gen_phy);
323 dev_err(dev, "failed to create PHY for ufs-phy\n");
327 drvdata = match->data;
329 phy->drvdata = drvdata;
330 phy->cfg = (struct samsung_ufs_phy_cfg **)drvdata->cfg;
331 phy->isol = &drvdata->isol;
332 phy->lane_cnt = PHY_DEF_LANE_CNT;
334 phy_set_drvdata(gen_phy, phy);
336 phy_provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
337 if (IS_ERR(phy_provider)) {
338 err = PTR_ERR(phy_provider);
339 dev_err(dev, "failed to register phy-provider\n");
346 static const struct of_device_id samsung_ufs_phy_match[] = {
348 .compatible = "samsung,exynos7-ufs-phy",
349 .data = &exynos7_ufs_phy,
353 MODULE_DEVICE_TABLE(of, samsung_ufs_phy_match);
355 static struct platform_driver samsung_ufs_phy_driver = {
356 .probe = samsung_ufs_phy_probe,
358 .name = "samsung-ufs-phy",
359 .of_match_table = samsung_ufs_phy_match,
362 module_platform_driver(samsung_ufs_phy_driver);
363 MODULE_DESCRIPTION("Samsung SoC UFS PHY Driver");
364 MODULE_AUTHOR("Seungwon Jeon <essuuj@gmail.com>");
365 MODULE_AUTHOR("Alim Akhtar <alim.akhtar@samsung.com>");
366 MODULE_LICENSE("GPL v2");