2 * Renesas R-Car USB phy driver
4 * Copyright (C) 2012 Renesas Solutions Corp.
5 * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
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/delay.h>
14 #include <linux/usb/otg.h>
15 #include <linux/platform_device.h>
16 #include <linux/spinlock.h>
17 #include <linux/module.h>
19 /* USBH common register */
20 #define USBPCTRL0 0x0800
21 #define USBPCTRL1 0x0804
25 #define USBCTL0 0x0858
28 #define PHY_RST (1 << 2)
29 #define PLL_ENB (1 << 1)
30 #define PHY_ENB (1 << 0)
33 #define ST_ACT (1 << 31)
34 #define ST_PLL (1 << 30)
36 struct rcar_usb_phy_priv {
44 #define usb_phy_to_priv(p) container_of(p, struct rcar_usb_phy_priv, phy)
48 * USB initial/install operation.
50 * This function setup USB phy.
51 * The used value and setting order came from
52 * [USB :: Initial setting] on datasheet.
54 static int rcar_usb_phy_init(struct usb_phy *phy)
56 struct rcar_usb_phy_priv *priv = usb_phy_to_priv(phy);
57 struct device *dev = phy->dev;
58 void __iomem *reg0 = priv->reg0;
63 spin_lock_irqsave(&priv->lock, flags);
64 if (priv->counter++ == 0) {
70 /* (1) USB-PHY standby release */
71 iowrite32(PHY_ENB, (reg0 + USBPCTRL1));
73 /* (2) start USB-PHY internal PLL */
74 iowrite32(PHY_ENB | PLL_ENB, (reg0 + USBPCTRL1));
76 /* (3) USB module status check */
77 for (i = 0; i < 1024; i++) {
79 val = ioread32(reg0 + USBST);
80 if (val == (ST_ACT | ST_PLL))
84 if (val != (ST_ACT | ST_PLL)) {
85 dev_err(dev, "USB phy not ready\n");
89 /* (4) USB-PHY reset clear */
90 iowrite32(PHY_ENB | PLL_ENB | PHY_RST, (reg0 + USBPCTRL1));
92 /* set platform specific port settings */
93 iowrite32(0x00000000, (reg0 + USBPCTRL0));
96 * Bus alignment settings
99 /* (1) EHCI bus alignment (little endian) */
100 iowrite32(0x00000000, (reg0 + USBEH0));
102 /* (1) OHCI bus alignment (little endian) */
103 iowrite32(0x00000000, (reg0 + USBOH0));
107 spin_unlock_irqrestore(&priv->lock, flags);
112 static void rcar_usb_phy_shutdown(struct usb_phy *phy)
114 struct rcar_usb_phy_priv *priv = usb_phy_to_priv(phy);
115 void __iomem *reg0 = priv->reg0;
118 spin_lock_irqsave(&priv->lock, flags);
120 if (priv->counter-- == 1) { /* last user */
121 iowrite32(0x00000000, (reg0 + USBPCTRL0));
122 iowrite32(0x00000000, (reg0 + USBPCTRL1));
125 spin_unlock_irqrestore(&priv->lock, flags);
128 static int rcar_usb_phy_probe(struct platform_device *pdev)
130 struct rcar_usb_phy_priv *priv;
131 struct resource *res0;
132 struct device *dev = &pdev->dev;
136 res0 = platform_get_resource(pdev, IORESOURCE_MEM, 0);
138 dev_err(dev, "Not enough platform resources\n");
145 * Because this phy address is also mapped under OHCI/EHCI address area,
146 * this driver can't use devm_request_and_ioremap(dev, res) here
148 reg0 = devm_ioremap_nocache(dev, res0->start, resource_size(res0));
150 dev_err(dev, "ioremap error\n");
154 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
156 dev_err(dev, "priv data allocation error\n");
163 priv->phy.label = dev_name(dev);
164 priv->phy.init = rcar_usb_phy_init;
165 priv->phy.shutdown = rcar_usb_phy_shutdown;
166 spin_lock_init(&priv->lock);
168 ret = usb_add_phy(&priv->phy, USB_PHY_TYPE_USB2);
170 dev_err(dev, "usb phy addition error\n");
174 platform_set_drvdata(pdev, priv);
179 static int rcar_usb_phy_remove(struct platform_device *pdev)
181 struct rcar_usb_phy_priv *priv = platform_get_drvdata(pdev);
183 usb_remove_phy(&priv->phy);
188 static struct platform_driver rcar_usb_phy_driver = {
190 .name = "rcar_usb_phy",
192 .probe = rcar_usb_phy_probe,
193 .remove = rcar_usb_phy_remove,
196 module_platform_driver(rcar_usb_phy_driver);
198 MODULE_LICENSE("GPL v2");
199 MODULE_DESCRIPTION("Renesas R-Car USB phy");
200 MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");