1 // SPDX-License-Identifier: GPL-2.0
3 * phy-uniphier-usb3ss.c - SS-PHY driver for Socionext UniPhier USB3 controller
4 * Copyright 2015-2018 Socionext Inc.
6 * Kunihiko Hayashi <hayashi.kunihiko@socionext.com>
8 * Motoya Tanigawa <tanigawa.motoya@socionext.com>
9 * Masami Hiramatsu <masami.hiramatsu@linaro.org>
12 #include <linux/bitfield.h>
13 #include <linux/bitops.h>
14 #include <linux/clk.h>
16 #include <linux/module.h>
18 #include <linux/of_platform.h>
19 #include <linux/phy/phy.h>
20 #include <linux/platform_device.h>
21 #include <linux/regulator/consumer.h>
22 #include <linux/reset.h>
24 #define SSPHY_TESTI 0x0
25 #define TESTI_DAT_MASK GENMASK(13, 6)
26 #define TESTI_ADR_MASK GENMASK(5, 1)
27 #define TESTI_WR_EN BIT(0)
29 #define SSPHY_TESTO 0x4
30 #define TESTO_DAT_MASK GENMASK(7, 0)
32 #define PHY_F(regno, msb, lsb) { (regno), (msb), (lsb) }
34 #define CDR_CPD_TRIM PHY_F(7, 3, 0) /* RxPLL charge pump current */
35 #define CDR_CPF_TRIM PHY_F(8, 3, 0) /* RxPLL charge pump current 2 */
36 #define TX_PLL_TRIM PHY_F(9, 3, 0) /* TxPLL charge pump current */
37 #define BGAP_TRIM PHY_F(11, 3, 0) /* Bandgap voltage */
38 #define CDR_TRIM PHY_F(13, 6, 5) /* Clock Data Recovery setting */
39 #define VCO_CTRL PHY_F(26, 7, 4) /* VCO control */
40 #define VCOPLL_CTRL PHY_F(27, 2, 0) /* TxPLL VCO tuning */
41 #define VCOPLL_CM PHY_F(28, 1, 0) /* TxPLL voltage */
43 #define MAX_PHY_PARAMS 7
45 struct uniphier_u3ssphy_param {
54 struct uniphier_u3ssphy_priv {
57 struct clk *clk, *clk_ext, *clk_parent, *clk_parent_gio;
58 struct reset_control *rst, *rst_parent, *rst_parent_gio;
59 struct regulator *vbus;
60 const struct uniphier_u3ssphy_soc_data *data;
63 struct uniphier_u3ssphy_soc_data {
66 const struct uniphier_u3ssphy_param param[MAX_PHY_PARAMS];
69 static void uniphier_u3ssphy_testio_write(struct uniphier_u3ssphy_priv *priv,
72 /* need to read TESTO twice after accessing TESTI */
73 writel(data, priv->base + SSPHY_TESTI);
74 readl(priv->base + SSPHY_TESTO);
75 readl(priv->base + SSPHY_TESTO);
78 static void uniphier_u3ssphy_set_param(struct uniphier_u3ssphy_priv *priv,
79 const struct uniphier_u3ssphy_param *p)
82 u8 field_mask = GENMASK(p->field.msb, p->field.lsb);
85 /* read previous data */
86 val = FIELD_PREP(TESTI_DAT_MASK, 1);
87 val |= FIELD_PREP(TESTI_ADR_MASK, p->field.reg_no);
88 uniphier_u3ssphy_testio_write(priv, val);
89 val = readl(priv->base + SSPHY_TESTO) & TESTO_DAT_MASK;
93 data = field_mask & (p->value << p->field.lsb);
94 val = FIELD_PREP(TESTI_DAT_MASK, data | val);
95 val |= FIELD_PREP(TESTI_ADR_MASK, p->field.reg_no);
96 uniphier_u3ssphy_testio_write(priv, val);
97 uniphier_u3ssphy_testio_write(priv, val | TESTI_WR_EN);
98 uniphier_u3ssphy_testio_write(priv, val);
100 /* read current data as dummy */
101 val = FIELD_PREP(TESTI_DAT_MASK, 1);
102 val |= FIELD_PREP(TESTI_ADR_MASK, p->field.reg_no);
103 uniphier_u3ssphy_testio_write(priv, val);
104 readl(priv->base + SSPHY_TESTO);
107 static int uniphier_u3ssphy_power_on(struct phy *phy)
109 struct uniphier_u3ssphy_priv *priv = phy_get_drvdata(phy);
112 ret = clk_prepare_enable(priv->clk_ext);
116 ret = clk_prepare_enable(priv->clk);
118 goto out_clk_ext_disable;
120 ret = reset_control_deassert(priv->rst);
122 goto out_clk_disable;
125 ret = regulator_enable(priv->vbus);
133 reset_control_assert(priv->rst);
135 clk_disable_unprepare(priv->clk);
137 clk_disable_unprepare(priv->clk_ext);
142 static int uniphier_u3ssphy_power_off(struct phy *phy)
144 struct uniphier_u3ssphy_priv *priv = phy_get_drvdata(phy);
147 regulator_disable(priv->vbus);
149 reset_control_assert(priv->rst);
150 clk_disable_unprepare(priv->clk);
151 clk_disable_unprepare(priv->clk_ext);
156 static int uniphier_u3ssphy_init(struct phy *phy)
158 struct uniphier_u3ssphy_priv *priv = phy_get_drvdata(phy);
161 ret = clk_prepare_enable(priv->clk_parent);
165 ret = clk_prepare_enable(priv->clk_parent_gio);
167 goto out_clk_disable;
169 ret = reset_control_deassert(priv->rst_parent);
171 goto out_clk_gio_disable;
173 ret = reset_control_deassert(priv->rst_parent_gio);
177 if (priv->data->is_legacy)
180 for (i = 0; i < priv->data->nparams; i++)
181 uniphier_u3ssphy_set_param(priv, &priv->data->param[i]);
186 reset_control_assert(priv->rst_parent);
188 clk_disable_unprepare(priv->clk_parent_gio);
190 clk_disable_unprepare(priv->clk_parent);
195 static int uniphier_u3ssphy_exit(struct phy *phy)
197 struct uniphier_u3ssphy_priv *priv = phy_get_drvdata(phy);
199 reset_control_assert(priv->rst_parent_gio);
200 reset_control_assert(priv->rst_parent);
201 clk_disable_unprepare(priv->clk_parent_gio);
202 clk_disable_unprepare(priv->clk_parent);
207 static const struct phy_ops uniphier_u3ssphy_ops = {
208 .init = uniphier_u3ssphy_init,
209 .exit = uniphier_u3ssphy_exit,
210 .power_on = uniphier_u3ssphy_power_on,
211 .power_off = uniphier_u3ssphy_power_off,
212 .owner = THIS_MODULE,
215 static int uniphier_u3ssphy_probe(struct platform_device *pdev)
217 struct device *dev = &pdev->dev;
218 struct uniphier_u3ssphy_priv *priv;
219 struct phy_provider *phy_provider;
222 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
227 priv->data = of_device_get_match_data(dev);
228 if (WARN_ON(!priv->data ||
229 priv->data->nparams > MAX_PHY_PARAMS))
232 priv->base = devm_platform_ioremap_resource(pdev, 0);
233 if (IS_ERR(priv->base))
234 return PTR_ERR(priv->base);
236 if (!priv->data->is_legacy) {
237 priv->clk = devm_clk_get(dev, "phy");
238 if (IS_ERR(priv->clk))
239 return PTR_ERR(priv->clk);
241 priv->clk_ext = devm_clk_get_optional(dev, "phy-ext");
242 if (IS_ERR(priv->clk_ext))
243 return PTR_ERR(priv->clk_ext);
245 priv->rst = devm_reset_control_get_shared(dev, "phy");
246 if (IS_ERR(priv->rst))
247 return PTR_ERR(priv->rst);
249 priv->clk_parent_gio = devm_clk_get(dev, "gio");
250 if (IS_ERR(priv->clk_parent_gio))
251 return PTR_ERR(priv->clk_parent_gio);
253 priv->rst_parent_gio =
254 devm_reset_control_get_shared(dev, "gio");
255 if (IS_ERR(priv->rst_parent_gio))
256 return PTR_ERR(priv->rst_parent_gio);
259 priv->clk_parent = devm_clk_get(dev, "link");
260 if (IS_ERR(priv->clk_parent))
261 return PTR_ERR(priv->clk_parent);
263 priv->rst_parent = devm_reset_control_get_shared(dev, "link");
264 if (IS_ERR(priv->rst_parent))
265 return PTR_ERR(priv->rst_parent);
267 priv->vbus = devm_regulator_get_optional(dev, "vbus");
268 if (IS_ERR(priv->vbus)) {
269 if (PTR_ERR(priv->vbus) == -EPROBE_DEFER)
270 return PTR_ERR(priv->vbus);
274 phy = devm_phy_create(dev, dev->of_node, &uniphier_u3ssphy_ops);
278 phy_set_drvdata(phy, priv);
279 phy_provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
281 return PTR_ERR_OR_ZERO(phy_provider);
284 static const struct uniphier_u3ssphy_soc_data uniphier_pro4_data = {
288 static const struct uniphier_u3ssphy_soc_data uniphier_pxs2_data = {
292 { CDR_CPD_TRIM, 10 },
302 static const struct uniphier_u3ssphy_soc_data uniphier_ld20_data = {
312 static const struct of_device_id uniphier_u3ssphy_match[] = {
314 .compatible = "socionext,uniphier-pro4-usb3-ssphy",
315 .data = &uniphier_pro4_data,
318 .compatible = "socionext,uniphier-pro5-usb3-ssphy",
319 .data = &uniphier_pro4_data,
322 .compatible = "socionext,uniphier-pxs2-usb3-ssphy",
323 .data = &uniphier_pxs2_data,
326 .compatible = "socionext,uniphier-ld20-usb3-ssphy",
327 .data = &uniphier_ld20_data,
330 .compatible = "socionext,uniphier-pxs3-usb3-ssphy",
331 .data = &uniphier_ld20_data,
335 MODULE_DEVICE_TABLE(of, uniphier_u3ssphy_match);
337 static struct platform_driver uniphier_u3ssphy_driver = {
338 .probe = uniphier_u3ssphy_probe,
340 .name = "uniphier-usb3-ssphy",
341 .of_match_table = uniphier_u3ssphy_match,
345 module_platform_driver(uniphier_u3ssphy_driver);
347 MODULE_AUTHOR("Kunihiko Hayashi <hayashi.kunihiko@socionext.com>");
348 MODULE_DESCRIPTION("UniPhier SS-PHY driver for USB3 controller");
349 MODULE_LICENSE("GPL v2");