1/*
2 * Renesas R-Car Gen2 USB phy driver
3 *
4 * Copyright (C) 2013 Renesas Solutions Corp.
5 * Copyright (C) 2013 Cogent Embedded, Inc.
6 *
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.
10 */
11
12#include <linux/clk.h>
13#include <linux/delay.h>
14#include <linux/io.h>
15#include <linux/module.h>
16#include <linux/platform_data/usb-rcar-gen2-phy.h>
17#include <linux/platform_device.h>
18#include <linux/spinlock.h>
19#include <linux/usb/otg.h>
20
21struct rcar_gen2_usb_phy_priv {
22	struct usb_phy phy;
23	void __iomem *base;
24	struct clk *clk;
25	spinlock_t lock;
26	int usecount;
27	u32 ugctrl2;
28};
29
30#define usb_phy_to_priv(p) container_of(p, struct rcar_gen2_usb_phy_priv, phy)
31
32/* Low Power Status register */
33#define USBHS_LPSTS_REG			0x02
34#define USBHS_LPSTS_SUSPM		(1 << 14)
35
36/* USB General control register */
37#define USBHS_UGCTRL_REG		0x80
38#define USBHS_UGCTRL_CONNECT		(1 << 2)
39#define USBHS_UGCTRL_PLLRESET		(1 << 0)
40
41/* USB General control register 2 */
42#define USBHS_UGCTRL2_REG		0x84
43#define USBHS_UGCTRL2_USB0_PCI		(1 << 4)
44#define USBHS_UGCTRL2_USB0_HS		(3 << 4)
45#define USBHS_UGCTRL2_USB2_PCI		(0 << 31)
46#define USBHS_UGCTRL2_USB2_SS		(1 << 31)
47
48/* USB General status register */
49#define USBHS_UGSTS_REG			0x88
50#define USBHS_UGSTS_LOCK		(3 << 8)
51
52/* Enable USBHS internal phy */
53static int __rcar_gen2_usbhs_phy_enable(void __iomem *base)
54{
55	u32 val;
56	int i;
57
58	/* USBHS PHY power on */
59	val = ioread32(base + USBHS_UGCTRL_REG);
60	val &= ~USBHS_UGCTRL_PLLRESET;
61	iowrite32(val, base + USBHS_UGCTRL_REG);
62
63	val = ioread16(base + USBHS_LPSTS_REG);
64	val |= USBHS_LPSTS_SUSPM;
65	iowrite16(val, base + USBHS_LPSTS_REG);
66
67	for (i = 0; i < 20; i++) {
68		val = ioread32(base + USBHS_UGSTS_REG);
69		if ((val & USBHS_UGSTS_LOCK) == USBHS_UGSTS_LOCK) {
70			val = ioread32(base + USBHS_UGCTRL_REG);
71			val |= USBHS_UGCTRL_CONNECT;
72			iowrite32(val, base + USBHS_UGCTRL_REG);
73			return 0;
74		}
75		udelay(1);
76	}
77
78	/* Timed out waiting for the PLL lock */
79	return -ETIMEDOUT;
80}
81
82/* Disable USBHS internal phy */
83static int __rcar_gen2_usbhs_phy_disable(void __iomem *base)
84{
85	u32 val;
86
87	/* USBHS PHY power off */
88	val = ioread32(base + USBHS_UGCTRL_REG);
89	val &= ~USBHS_UGCTRL_CONNECT;
90	iowrite32(val, base + USBHS_UGCTRL_REG);
91
92	val = ioread16(base + USBHS_LPSTS_REG);
93	val &= ~USBHS_LPSTS_SUSPM;
94	iowrite16(val, base + USBHS_LPSTS_REG);
95
96	val = ioread32(base + USBHS_UGCTRL_REG);
97	val |= USBHS_UGCTRL_PLLRESET;
98	iowrite32(val, base + USBHS_UGCTRL_REG);
99	return 0;
100}
101
102/* Setup USB channels */
103static void __rcar_gen2_usb_phy_init(struct rcar_gen2_usb_phy_priv *priv)
104{
105	u32 val;
106
107	clk_prepare_enable(priv->clk);
108
109	/* Set USB channels in the USBHS UGCTRL2 register */
110	val = ioread32(priv->base + USBHS_UGCTRL2_REG);
111	val &= ~(USBHS_UGCTRL2_USB0_HS | USBHS_UGCTRL2_USB2_SS);
112	val |= priv->ugctrl2;
113	iowrite32(val, priv->base + USBHS_UGCTRL2_REG);
114}
115
116/* Shutdown USB channels */
117static void __rcar_gen2_usb_phy_shutdown(struct rcar_gen2_usb_phy_priv *priv)
118{
119	__rcar_gen2_usbhs_phy_disable(priv->base);
120	clk_disable_unprepare(priv->clk);
121}
122
123static int rcar_gen2_usb_phy_set_suspend(struct usb_phy *phy, int suspend)
124{
125	struct rcar_gen2_usb_phy_priv *priv = usb_phy_to_priv(phy);
126	unsigned long flags;
127	int retval;
128
129	spin_lock_irqsave(&priv->lock, flags);
130	retval = suspend ? __rcar_gen2_usbhs_phy_disable(priv->base) :
131			   __rcar_gen2_usbhs_phy_enable(priv->base);
132	spin_unlock_irqrestore(&priv->lock, flags);
133	return retval;
134}
135
136static int rcar_gen2_usb_phy_init(struct usb_phy *phy)
137{
138	struct rcar_gen2_usb_phy_priv *priv = usb_phy_to_priv(phy);
139	unsigned long flags;
140
141	spin_lock_irqsave(&priv->lock, flags);
142	/*
143	 * Enable the clock and setup USB channels
144	 * if it's the first user
145	 */
146	if (!priv->usecount++)
147		__rcar_gen2_usb_phy_init(priv);
148	spin_unlock_irqrestore(&priv->lock, flags);
149	return 0;
150}
151
152static void rcar_gen2_usb_phy_shutdown(struct usb_phy *phy)
153{
154	struct rcar_gen2_usb_phy_priv *priv = usb_phy_to_priv(phy);
155	unsigned long flags;
156
157	spin_lock_irqsave(&priv->lock, flags);
158	if (!priv->usecount) {
159		dev_warn(phy->dev, "Trying to disable phy with 0 usecount\n");
160		goto out;
161	}
162
163	/* Disable everything if it's the last user */
164	if (!--priv->usecount)
165		__rcar_gen2_usb_phy_shutdown(priv);
166out:
167	spin_unlock_irqrestore(&priv->lock, flags);
168}
169
170static int rcar_gen2_usb_phy_probe(struct platform_device *pdev)
171{
172	struct device *dev = &pdev->dev;
173	struct rcar_gen2_phy_platform_data *pdata;
174	struct rcar_gen2_usb_phy_priv *priv;
175	struct resource *res;
176	void __iomem *base;
177	struct clk *clk;
178	int retval;
179
180	pdata = dev_get_platdata(dev);
181	if (!pdata) {
182		dev_err(dev, "No platform data\n");
183		return -EINVAL;
184	}
185
186	clk = devm_clk_get(dev, "usbhs");
187	if (IS_ERR(clk)) {
188		dev_err(dev, "Can't get the clock\n");
189		return PTR_ERR(clk);
190	}
191
192	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
193	base = devm_ioremap_resource(dev, res);
194	if (IS_ERR(base))
195		return PTR_ERR(base);
196
197	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
198	if (!priv) {
199		dev_err(dev, "Memory allocation failed\n");
200		return -ENOMEM;
201	}
202
203	spin_lock_init(&priv->lock);
204	priv->clk = clk;
205	priv->base = base;
206	priv->ugctrl2 = pdata->chan0_pci ?
207			USBHS_UGCTRL2_USB0_PCI : USBHS_UGCTRL2_USB0_HS;
208	priv->ugctrl2 |= pdata->chan2_pci ?
209			USBHS_UGCTRL2_USB2_PCI : USBHS_UGCTRL2_USB2_SS;
210	priv->phy.dev = dev;
211	priv->phy.label = dev_name(dev);
212	priv->phy.init = rcar_gen2_usb_phy_init;
213	priv->phy.shutdown = rcar_gen2_usb_phy_shutdown;
214	priv->phy.set_suspend = rcar_gen2_usb_phy_set_suspend;
215
216	retval = usb_add_phy_dev(&priv->phy);
217	if (retval < 0) {
218		dev_err(dev, "Failed to add USB phy\n");
219		return retval;
220	}
221
222	platform_set_drvdata(pdev, priv);
223
224	return retval;
225}
226
227static int rcar_gen2_usb_phy_remove(struct platform_device *pdev)
228{
229	struct rcar_gen2_usb_phy_priv *priv = platform_get_drvdata(pdev);
230
231	usb_remove_phy(&priv->phy);
232
233	return 0;
234}
235
236static struct platform_driver rcar_gen2_usb_phy_driver = {
237	.driver = {
238		.name = "usb_phy_rcar_gen2",
239	},
240	.probe = rcar_gen2_usb_phy_probe,
241	.remove = rcar_gen2_usb_phy_remove,
242};
243
244module_platform_driver(rcar_gen2_usb_phy_driver);
245
246MODULE_LICENSE("GPL v2");
247MODULE_DESCRIPTION("Renesas R-Car Gen2 USB phy");
248MODULE_AUTHOR("Valentine Barshak <valentine.barshak@cogentembedded.com>");
249