1 // SPDX-License-Identifier: GPL-2.0
3 * Renesas R-Car Gen2 PHY driver
5 * Copyright (C) 2014 Renesas Solutions Corp.
6 * Copyright (C) 2014 Cogent Embedded, Inc.
10 #include <linux/delay.h>
12 #include <linux/module.h>
14 #include <linux/phy/phy.h>
15 #include <linux/platform_device.h>
16 #include <linux/spinlock.h>
17 #include <linux/atomic.h>
19 #define USBHS_LPSTS 0x02
20 #define USBHS_UGCTRL 0x80
21 #define USBHS_UGCTRL2 0x84
22 #define USBHS_UGSTS 0x88 /* From technical update */
24 /* Low Power Status register (LPSTS) */
25 #define USBHS_LPSTS_SUSPM 0x4000
27 /* USB General control register (UGCTRL) */
28 #define USBHS_UGCTRL_CONNECT 0x00000004
29 #define USBHS_UGCTRL_PLLRESET 0x00000001
31 /* USB General control register 2 (UGCTRL2) */
32 #define USBHS_UGCTRL2_USB2SEL 0x80000000
33 #define USBHS_UGCTRL2_USB2SEL_PCI 0x00000000
34 #define USBHS_UGCTRL2_USB2SEL_USB30 0x80000000
35 #define USBHS_UGCTRL2_USB0SEL 0x00000030
36 #define USBHS_UGCTRL2_USB0SEL_PCI 0x00000010
37 #define USBHS_UGCTRL2_USB0SEL_HS_USB 0x00000030
39 /* USB General status register (UGSTS) */
40 #define USBHS_UGSTS_LOCK 0x00000100 /* From technical update */
42 #define PHYS_PER_CHANNEL 2
44 struct rcar_gen2_phy {
46 struct rcar_gen2_channel *channel;
51 struct rcar_gen2_channel {
52 struct device_node *of_node;
53 struct rcar_gen2_phy_driver *drv;
54 struct rcar_gen2_phy phys[PHYS_PER_CHANNEL];
59 struct rcar_gen2_phy_driver {
64 struct rcar_gen2_channel *channels;
67 static int rcar_gen2_phy_init(struct phy *p)
69 struct rcar_gen2_phy *phy = phy_get_drvdata(p);
70 struct rcar_gen2_channel *channel = phy->channel;
71 struct rcar_gen2_phy_driver *drv = channel->drv;
76 * Try to acquire exclusive access to PHY. The first driver calling
77 * phy_init() on a given channel wins, and all attempts to use another
78 * PHY on this channel will fail until phy_exit() is called by the first
79 * driver. Achieving this with cmpxcgh() should be SMP-safe.
81 if (cmpxchg(&channel->selected_phy, -1, phy->number) != -1)
84 clk_prepare_enable(drv->clk);
86 spin_lock_irqsave(&drv->lock, flags);
87 ugctrl2 = readl(drv->base + USBHS_UGCTRL2);
88 ugctrl2 &= ~channel->select_mask;
89 ugctrl2 |= phy->select_value;
90 writel(ugctrl2, drv->base + USBHS_UGCTRL2);
91 spin_unlock_irqrestore(&drv->lock, flags);
95 static int rcar_gen2_phy_exit(struct phy *p)
97 struct rcar_gen2_phy *phy = phy_get_drvdata(p);
98 struct rcar_gen2_channel *channel = phy->channel;
100 clk_disable_unprepare(channel->drv->clk);
102 channel->selected_phy = -1;
107 static int rcar_gen2_phy_power_on(struct phy *p)
109 struct rcar_gen2_phy *phy = phy_get_drvdata(p);
110 struct rcar_gen2_phy_driver *drv = phy->channel->drv;
111 void __iomem *base = drv->base;
116 /* Skip if it's not USBHS */
117 if (phy->select_value != USBHS_UGCTRL2_USB0SEL_HS_USB)
120 spin_lock_irqsave(&drv->lock, flags);
122 /* Power on USBHS PHY */
123 value = readl(base + USBHS_UGCTRL);
124 value &= ~USBHS_UGCTRL_PLLRESET;
125 writel(value, base + USBHS_UGCTRL);
127 value = readw(base + USBHS_LPSTS);
128 value |= USBHS_LPSTS_SUSPM;
129 writew(value, base + USBHS_LPSTS);
131 for (i = 0; i < 20; i++) {
132 value = readl(base + USBHS_UGSTS);
133 if ((value & USBHS_UGSTS_LOCK) == USBHS_UGSTS_LOCK) {
134 value = readl(base + USBHS_UGCTRL);
135 value |= USBHS_UGCTRL_CONNECT;
136 writel(value, base + USBHS_UGCTRL);
142 /* Timed out waiting for the PLL lock */
146 spin_unlock_irqrestore(&drv->lock, flags);
151 static int rcar_gen2_phy_power_off(struct phy *p)
153 struct rcar_gen2_phy *phy = phy_get_drvdata(p);
154 struct rcar_gen2_phy_driver *drv = phy->channel->drv;
155 void __iomem *base = drv->base;
159 /* Skip if it's not USBHS */
160 if (phy->select_value != USBHS_UGCTRL2_USB0SEL_HS_USB)
163 spin_lock_irqsave(&drv->lock, flags);
165 /* Power off USBHS PHY */
166 value = readl(base + USBHS_UGCTRL);
167 value &= ~USBHS_UGCTRL_CONNECT;
168 writel(value, base + USBHS_UGCTRL);
170 value = readw(base + USBHS_LPSTS);
171 value &= ~USBHS_LPSTS_SUSPM;
172 writew(value, base + USBHS_LPSTS);
174 value = readl(base + USBHS_UGCTRL);
175 value |= USBHS_UGCTRL_PLLRESET;
176 writel(value, base + USBHS_UGCTRL);
178 spin_unlock_irqrestore(&drv->lock, flags);
183 static const struct phy_ops rcar_gen2_phy_ops = {
184 .init = rcar_gen2_phy_init,
185 .exit = rcar_gen2_phy_exit,
186 .power_on = rcar_gen2_phy_power_on,
187 .power_off = rcar_gen2_phy_power_off,
188 .owner = THIS_MODULE,
191 static const struct of_device_id rcar_gen2_phy_match_table[] = {
192 { .compatible = "renesas,usb-phy-r8a7790" },
193 { .compatible = "renesas,usb-phy-r8a7791" },
194 { .compatible = "renesas,usb-phy-r8a7794" },
195 { .compatible = "renesas,rcar-gen2-usb-phy" },
198 MODULE_DEVICE_TABLE(of, rcar_gen2_phy_match_table);
200 static struct phy *rcar_gen2_phy_xlate(struct device *dev,
201 struct of_phandle_args *args)
203 struct rcar_gen2_phy_driver *drv;
204 struct device_node *np = args->np;
207 drv = dev_get_drvdata(dev);
209 return ERR_PTR(-EINVAL);
211 for (i = 0; i < drv->num_channels; i++) {
212 if (np == drv->channels[i].of_node)
216 if (i >= drv->num_channels || args->args[0] >= 2)
217 return ERR_PTR(-ENODEV);
219 return drv->channels[i].phys[args->args[0]].phy;
222 static const u32 select_mask[] = {
223 [0] = USBHS_UGCTRL2_USB0SEL,
224 [2] = USBHS_UGCTRL2_USB2SEL,
227 static const u32 select_value[][PHYS_PER_CHANNEL] = {
228 [0] = { USBHS_UGCTRL2_USB0SEL_PCI, USBHS_UGCTRL2_USB0SEL_HS_USB },
229 [2] = { USBHS_UGCTRL2_USB2SEL_PCI, USBHS_UGCTRL2_USB2SEL_USB30 },
232 static int rcar_gen2_phy_probe(struct platform_device *pdev)
234 struct device *dev = &pdev->dev;
235 struct rcar_gen2_phy_driver *drv;
236 struct phy_provider *provider;
237 struct device_node *np;
238 struct resource *res;
245 "This driver is required to be instantiated from device tree\n");
249 clk = devm_clk_get(dev, "usbhs");
251 dev_err(dev, "Can't get USBHS clock\n");
255 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
256 base = devm_ioremap_resource(dev, res);
258 return PTR_ERR(base);
260 drv = devm_kzalloc(dev, sizeof(*drv), GFP_KERNEL);
264 spin_lock_init(&drv->lock);
269 drv->num_channels = of_get_child_count(dev->of_node);
270 drv->channels = devm_kcalloc(dev, drv->num_channels,
271 sizeof(struct rcar_gen2_channel),
276 for_each_child_of_node(dev->of_node, np) {
277 struct rcar_gen2_channel *channel = drv->channels + i;
281 channel->of_node = np;
283 channel->selected_phy = -1;
285 error = of_property_read_u32(np, "reg", &channel_num);
286 if (error || channel_num > 2) {
287 dev_err(dev, "Invalid \"reg\" property\n");
290 channel->select_mask = select_mask[channel_num];
292 for (n = 0; n < PHYS_PER_CHANNEL; n++) {
293 struct rcar_gen2_phy *phy = &channel->phys[n];
295 phy->channel = channel;
297 phy->select_value = select_value[channel_num][n];
299 phy->phy = devm_phy_create(dev, NULL,
301 if (IS_ERR(phy->phy)) {
302 dev_err(dev, "Failed to create PHY\n");
303 return PTR_ERR(phy->phy);
305 phy_set_drvdata(phy->phy, phy);
311 provider = devm_of_phy_provider_register(dev, rcar_gen2_phy_xlate);
312 if (IS_ERR(provider)) {
313 dev_err(dev, "Failed to register PHY provider\n");
314 return PTR_ERR(provider);
317 dev_set_drvdata(dev, drv);
322 static struct platform_driver rcar_gen2_phy_driver = {
324 .name = "phy_rcar_gen2",
325 .of_match_table = rcar_gen2_phy_match_table,
327 .probe = rcar_gen2_phy_probe,
330 module_platform_driver(rcar_gen2_phy_driver);
332 MODULE_LICENSE("GPL v2");
333 MODULE_DESCRIPTION("Renesas R-Car Gen2 PHY");
334 MODULE_AUTHOR("Sergei Shtylyov <sergei.shtylyov@cogentembedded.com>");