1 From 3d74980ca3ba91dae6e84fbc23750e702e71d41a Mon Sep 17 00:00:00 2001
2 From: Minda Chen <minda.chen@starfivetech.com>
3 Date: Thu, 18 May 2023 19:27:46 +0800
4 Subject: [PATCH 089/122] phy: starfive: Add JH7110 USB 2.0 PHY driver
6 Add Starfive JH7110 SoC USB 2.0 PHY driver support.
7 USB 2.0 PHY default connect to Cadence USB controller.
9 Signed-off-by: Minda Chen <minda.chen@starfivetech.com>
11 drivers/phy/starfive/Kconfig | 11 ++
12 drivers/phy/starfive/Makefile | 1 +
13 drivers/phy/starfive/phy-jh7110-usb.c | 150 ++++++++++++++++++++++++++
14 3 files changed, 162 insertions(+)
15 create mode 100644 drivers/phy/starfive/phy-jh7110-usb.c
17 --- a/drivers/phy/starfive/Kconfig
18 +++ b/drivers/phy/starfive/Kconfig
19 @@ -11,3 +11,14 @@ config PHY_STARFIVE_DPHY_RX
20 Choose this option if you have a StarFive D-PHY in your
21 system. If M is selected, the module will be called
24 +config PHY_STARFIVE_JH7110_USB
25 + tristate "Starfive JH7110 USB 2.0 PHY support"
26 + depends on USB_SUPPORT
30 + Enable this to support the StarFive USB 2.0 PHY,
31 + used with the Cadence USB controller.
32 + If M is selected, the module will be called
34 --- a/drivers/phy/starfive/Makefile
35 +++ b/drivers/phy/starfive/Makefile
37 # SPDX-License-Identifier: GPL-2.0
38 obj-$(CONFIG_PHY_STARFIVE_DPHY_RX) += phy-starfive-dphy-rx.o
39 +obj-$(CONFIG_PHY_STARFIVE_JH7110_USB) += phy-jh7110-usb.o
41 +++ b/drivers/phy/starfive/phy-jh7110-usb.c
43 +// SPDX-License-Identifier: GPL-2.0+
45 + * StarFive JH7110 USB 2.0 PHY driver
47 + * Copyright (C) 2023 StarFive Technology Co., Ltd.
48 + * Author: Minda Chen <minda.chen@starfivetech.com>
51 +#include <linux/bits.h>
52 +#include <linux/clk.h>
53 +#include <linux/err.h>
54 +#include <linux/io.h>
55 +#include <linux/module.h>
56 +#include <linux/phy/phy.h>
57 +#include <linux/platform_device.h>
58 +#include <linux/usb/of.h>
60 +#define USB_125M_CLK_RATE 125000000
61 +#define USB_LS_KEEPALIVE_OFF 0x4
62 +#define USB_LS_KEEPALIVE_ENABLE BIT(4)
64 +struct jh7110_usb2_phy {
67 + struct clk *usb_125m_clk;
68 + struct clk *app_125m;
72 +static void jh7110_usb2_mode_set(struct jh7110_usb2_phy *phy)
76 + if (phy->mode != PHY_MODE_USB_HOST) {
77 + /* Enable the LS speed keep-alive signal */
78 + val = readl(phy->regs + USB_LS_KEEPALIVE_OFF);
79 + val |= USB_LS_KEEPALIVE_ENABLE;
80 + writel(val, phy->regs + USB_LS_KEEPALIVE_OFF);
84 +static int jh7110_usb2_phy_set_mode(struct phy *_phy,
85 + enum phy_mode mode, int submode)
87 + struct jh7110_usb2_phy *phy = phy_get_drvdata(_phy);
90 + case PHY_MODE_USB_HOST:
91 + case PHY_MODE_USB_DEVICE:
92 + case PHY_MODE_USB_OTG:
98 + if (mode != phy->mode) {
99 + dev_dbg(&_phy->dev, "Changing phy to %d\n", mode);
101 + jh7110_usb2_mode_set(phy);
107 +static int jh7110_usb2_phy_init(struct phy *_phy)
109 + struct jh7110_usb2_phy *phy = phy_get_drvdata(_phy);
112 + ret = clk_set_rate(phy->usb_125m_clk, USB_125M_CLK_RATE);
116 + ret = clk_prepare_enable(phy->app_125m);
123 +static int jh7110_usb2_phy_exit(struct phy *_phy)
125 + struct jh7110_usb2_phy *phy = phy_get_drvdata(_phy);
127 + clk_disable_unprepare(phy->app_125m);
132 +static const struct phy_ops jh7110_usb2_phy_ops = {
133 + .init = jh7110_usb2_phy_init,
134 + .exit = jh7110_usb2_phy_exit,
135 + .set_mode = jh7110_usb2_phy_set_mode,
136 + .owner = THIS_MODULE,
139 +static int jh7110_usb_phy_probe(struct platform_device *pdev)
141 + struct jh7110_usb2_phy *phy;
142 + struct device *dev = &pdev->dev;
143 + struct phy_provider *phy_provider;
145 + phy = devm_kzalloc(dev, sizeof(*phy), GFP_KERNEL);
149 + phy->usb_125m_clk = devm_clk_get(dev, "125m");
150 + if (IS_ERR(phy->usb_125m_clk))
151 + return dev_err_probe(dev, PTR_ERR(phy->usb_125m_clk),
152 + "Failed to get 125m clock\n");
154 + phy->app_125m = devm_clk_get(dev, "app_125m");
155 + if (IS_ERR(phy->app_125m))
156 + return dev_err_probe(dev, PTR_ERR(phy->app_125m),
157 + "Failed to get app 125m clock\n");
159 + phy->regs = devm_platform_ioremap_resource(pdev, 0);
160 + if (IS_ERR(phy->regs))
161 + return dev_err_probe(dev, PTR_ERR(phy->regs),
162 + "Failed to map phy base\n");
164 + phy->phy = devm_phy_create(dev, NULL, &jh7110_usb2_phy_ops);
165 + if (IS_ERR(phy->phy))
166 + return dev_err_probe(dev, PTR_ERR(phy->phy),
167 + "Failed to create phy\n");
169 + phy_set_drvdata(phy->phy, phy);
170 + phy_provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
172 + return PTR_ERR_OR_ZERO(phy_provider);
175 +static const struct of_device_id jh7110_usb_phy_of_match[] = {
176 + { .compatible = "starfive,jh7110-usb-phy" },
177 + { /* sentinel */ },
179 +MODULE_DEVICE_TABLE(of, jh7110_usb_phy_of_match);
181 +static struct platform_driver jh7110_usb_phy_driver = {
182 + .probe = jh7110_usb_phy_probe,
184 + .of_match_table = jh7110_usb_phy_of_match,
185 + .name = "jh7110-usb-phy",
188 +module_platform_driver(jh7110_usb_phy_driver);
190 +MODULE_DESCRIPTION("StarFive JH7110 USB 2.0 PHY driver");
191 +MODULE_AUTHOR("Minda Chen <minda.chen@starfivetech.com>");
192 +MODULE_LICENSE("GPL");