2 * Renesas R-Car Gen2 PHY driver
4 * Copyright (C) 2014 Renesas Solutions Corp.
5 * Copyright (C) 2014 Cogent Embedded, Inc.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
12 #include <linux/clk.h>
13 #include <linux/delay.h>
15 #include <linux/module.h>
17 #include <linux/phy/phy.h>
18 #include <linux/platform_device.h>
19 #include <linux/spinlock.h>
21 #include <asm/cmpxchg.h>
23 #define USBHS_LPSTS 0x02
24 #define USBHS_UGCTRL 0x80
25 #define USBHS_UGCTRL2 0x84
26 #define USBHS_UGSTS 0x88 /* The manuals have 0x90 */
28 /* Low Power Status register (LPSTS) */
29 #define USBHS_LPSTS_SUSPM 0x4000
31 /* USB General control register (UGCTRL) */
32 #define USBHS_UGCTRL_CONNECT 0x00000004
33 #define USBHS_UGCTRL_PLLRESET 0x00000001
35 /* USB General control register 2 (UGCTRL2) */
36 #define USBHS_UGCTRL2_USB2SEL 0x80000000
37 #define USBHS_UGCTRL2_USB2SEL_PCI 0x00000000
38 #define USBHS_UGCTRL2_USB2SEL_USB30 0x80000000
39 #define USBHS_UGCTRL2_USB0SEL 0x00000030
40 #define USBHS_UGCTRL2_USB0SEL_PCI 0x00000010
41 #define USBHS_UGCTRL2_USB0SEL_HS_USB 0x00000030
43 /* USB General status register (UGSTS) */
44 #define USBHS_UGSTS_LOCK 0x00000300 /* The manuals have 0x3 */
46 #define PHYS_PER_CHANNEL 2
48 struct rcar_gen2_phy {
50 struct rcar_gen2_channel *channel;
55 struct rcar_gen2_channel {
56 struct device_node *of_node;
57 struct rcar_gen2_phy_driver *drv;
58 struct rcar_gen2_phy phys[PHYS_PER_CHANNEL];
63 struct rcar_gen2_phy_driver {
68 struct rcar_gen2_channel *channels;
71 static int rcar_gen2_phy_init(struct phy *p)
73 struct rcar_gen2_phy *phy = phy_get_drvdata(p);
74 struct rcar_gen2_channel *channel = phy->channel;
75 struct rcar_gen2_phy_driver *drv = channel->drv;
80 * Try to acquire exclusive access to PHY. The first driver calling
81 * phy_init() on a given channel wins, and all attempts to use another
82 * PHY on this channel will fail until phy_exit() is called by the first
83 * driver. Achieving this with cmpxcgh() should be SMP-safe.
85 if (cmpxchg(&channel->selected_phy, -1, phy->number) != -1)
88 clk_prepare_enable(drv->clk);
90 spin_lock_irqsave(&drv->lock, flags);
91 ugctrl2 = readl(drv->base + USBHS_UGCTRL2);
92 ugctrl2 &= ~channel->select_mask;
93 ugctrl2 |= phy->select_value;
94 writel(ugctrl2, drv->base + USBHS_UGCTRL2);
95 spin_unlock_irqrestore(&drv->lock, flags);
99 static int rcar_gen2_phy_exit(struct phy *p)
101 struct rcar_gen2_phy *phy = phy_get_drvdata(p);
102 struct rcar_gen2_channel *channel = phy->channel;
104 clk_disable_unprepare(channel->drv->clk);
106 channel->selected_phy = -1;
111 static int rcar_gen2_phy_power_on(struct phy *p)
113 struct rcar_gen2_phy *phy = phy_get_drvdata(p);
114 struct rcar_gen2_phy_driver *drv = phy->channel->drv;
115 void __iomem *base = drv->base;
120 /* Skip if it's not USBHS */
121 if (phy->select_value != USBHS_UGCTRL2_USB0SEL_HS_USB)
124 spin_lock_irqsave(&drv->lock, flags);
126 /* Power on USBHS PHY */
127 value = readl(base + USBHS_UGCTRL);
128 value &= ~USBHS_UGCTRL_PLLRESET;
129 writel(value, base + USBHS_UGCTRL);
131 value = readw(base + USBHS_LPSTS);
132 value |= USBHS_LPSTS_SUSPM;
133 writew(value, base + USBHS_LPSTS);
135 for (i = 0; i < 20; i++) {
136 value = readl(base + USBHS_UGSTS);
137 if ((value & USBHS_UGSTS_LOCK) == USBHS_UGSTS_LOCK) {
138 value = readl(base + USBHS_UGCTRL);
139 value |= USBHS_UGCTRL_CONNECT;
140 writel(value, base + USBHS_UGCTRL);
146 /* Timed out waiting for the PLL lock */
150 spin_unlock_irqrestore(&drv->lock, flags);
155 static int rcar_gen2_phy_power_off(struct phy *p)
157 struct rcar_gen2_phy *phy = phy_get_drvdata(p);
158 struct rcar_gen2_phy_driver *drv = phy->channel->drv;
159 void __iomem *base = drv->base;
163 /* Skip if it's not USBHS */
164 if (phy->select_value != USBHS_UGCTRL2_USB0SEL_HS_USB)
167 spin_lock_irqsave(&drv->lock, flags);
169 /* Power off USBHS PHY */
170 value = readl(base + USBHS_UGCTRL);
171 value &= ~USBHS_UGCTRL_CONNECT;
172 writel(value, base + USBHS_UGCTRL);
174 value = readw(base + USBHS_LPSTS);
175 value &= ~USBHS_LPSTS_SUSPM;
176 writew(value, base + USBHS_LPSTS);
178 value = readl(base + USBHS_UGCTRL);
179 value |= USBHS_UGCTRL_PLLRESET;
180 writel(value, base + USBHS_UGCTRL);
182 spin_unlock_irqrestore(&drv->lock, flags);
187 static struct phy_ops rcar_gen2_phy_ops = {
188 .init = rcar_gen2_phy_init,
189 .exit = rcar_gen2_phy_exit,
190 .power_on = rcar_gen2_phy_power_on,
191 .power_off = rcar_gen2_phy_power_off,
192 .owner = THIS_MODULE,
195 static const struct of_device_id rcar_gen2_phy_match_table[] = {
196 { .compatible = "renesas,usb-phy-r8a7790" },
197 { .compatible = "renesas,usb-phy-r8a7791" },
200 MODULE_DEVICE_TABLE(of, rcar_gen2_phy_match_table);
202 static struct phy *rcar_gen2_phy_xlate(struct device *dev,
203 struct of_phandle_args *args)
205 struct rcar_gen2_phy_driver *drv;
206 struct device_node *np = args->np;
209 if (!of_device_is_available(np)) {
210 dev_warn(dev, "Requested PHY is disabled\n");
211 return ERR_PTR(-ENODEV);
214 drv = dev_get_drvdata(dev);
216 return ERR_PTR(-EINVAL);
218 for (i = 0; i < drv->num_channels; i++) {
219 if (np == drv->channels[i].of_node)
223 if (i >= drv->num_channels || args->args[0] >= 2)
224 return ERR_PTR(-ENODEV);
226 return drv->channels[i].phys[args->args[0]].phy;
229 static const u32 select_mask[] = {
230 [0] = USBHS_UGCTRL2_USB0SEL,
231 [2] = USBHS_UGCTRL2_USB2SEL,
234 static const u32 select_value[][PHYS_PER_CHANNEL] = {
235 [0] = { USBHS_UGCTRL2_USB0SEL_PCI, USBHS_UGCTRL2_USB0SEL_HS_USB },
236 [2] = { USBHS_UGCTRL2_USB2SEL_PCI, USBHS_UGCTRL2_USB2SEL_USB30 },
239 static int rcar_gen2_phy_probe(struct platform_device *pdev)
241 struct device *dev = &pdev->dev;
242 struct rcar_gen2_phy_driver *drv;
243 struct phy_provider *provider;
244 struct device_node *np;
245 struct resource *res;
252 "This driver is required to be instantiated from device tree\n");
256 clk = devm_clk_get(dev, "usbhs");
258 dev_err(dev, "Can't get USBHS clock\n");
262 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
263 base = devm_ioremap_resource(dev, res);
265 return PTR_ERR(base);
267 drv = devm_kzalloc(dev, sizeof(*drv), GFP_KERNEL);
271 spin_lock_init(&drv->lock);
276 drv->num_channels = of_get_child_count(dev->of_node);
277 drv->channels = devm_kcalloc(dev, drv->num_channels,
278 sizeof(struct rcar_gen2_channel),
283 for_each_child_of_node(dev->of_node, np) {
284 struct rcar_gen2_channel *channel = drv->channels + i;
288 channel->of_node = np;
290 channel->selected_phy = -1;
292 error = of_property_read_u32(np, "reg", &channel_num);
293 if (error || channel_num > 2) {
294 dev_err(dev, "Invalid \"reg\" property\n");
297 channel->select_mask = select_mask[channel_num];
299 for (n = 0; n < PHYS_PER_CHANNEL; n++) {
300 struct rcar_gen2_phy *phy = &channel->phys[n];
302 phy->channel = channel;
304 phy->select_value = select_value[channel_num][n];
306 phy->phy = devm_phy_create(dev, NULL,
308 if (IS_ERR(phy->phy)) {
309 dev_err(dev, "Failed to create PHY\n");
310 return PTR_ERR(phy->phy);
312 phy_set_drvdata(phy->phy, phy);
318 provider = devm_of_phy_provider_register(dev, rcar_gen2_phy_xlate);
319 if (IS_ERR(provider)) {
320 dev_err(dev, "Failed to register PHY provider\n");
321 return PTR_ERR(provider);
324 dev_set_drvdata(dev, drv);
329 static struct platform_driver rcar_gen2_phy_driver = {
331 .name = "phy_rcar_gen2",
332 .of_match_table = rcar_gen2_phy_match_table,
334 .probe = rcar_gen2_phy_probe,
337 module_platform_driver(rcar_gen2_phy_driver);
339 MODULE_LICENSE("GPL v2");
340 MODULE_DESCRIPTION("Renesas R-Car Gen2 PHY");
341 MODULE_AUTHOR("Sergei Shtylyov <sergei.shtylyov@cogentembedded.com>");