xref: /openbmc/linux/drivers/usb/musb/ux500.c (revision d026e9c7)
1 /*
2  * Copyright (C) 2010 ST-Ericsson AB
3  * Mian Yousaf Kaukab <mian.yousaf.kaukab@stericsson.com>
4  *
5  * Based on omap2430.c
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 as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  */
21 
22 #include <linux/module.h>
23 #include <linux/kernel.h>
24 #include <linux/clk.h>
25 #include <linux/err.h>
26 #include <linux/io.h>
27 #include <linux/of.h>
28 #include <linux/platform_device.h>
29 #include <linux/usb/musb-ux500.h>
30 
31 #include "musb_core.h"
32 
33 static struct musb_hdrc_config ux500_musb_hdrc_config = {
34 	.multipoint	= true,
35 	.dyn_fifo	= true,
36 	.num_eps	= 16,
37 	.ram_bits	= 16,
38 };
39 
40 struct ux500_glue {
41 	struct device		*dev;
42 	struct platform_device	*musb;
43 	struct clk		*clk;
44 };
45 #define glue_to_musb(g)	platform_get_drvdata(g->musb)
46 
47 static void ux500_musb_set_vbus(struct musb *musb, int is_on)
48 {
49 	u8            devctl;
50 	unsigned long timeout = jiffies + msecs_to_jiffies(1000);
51 	/* HDRC controls CPEN, but beware current surges during device
52 	 * connect.  They can trigger transient overcurrent conditions
53 	 * that must be ignored.
54 	 */
55 
56 	devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
57 
58 	if (is_on) {
59 		if (musb->xceiv->otg->state == OTG_STATE_A_IDLE) {
60 			/* start the session */
61 			devctl |= MUSB_DEVCTL_SESSION;
62 			musb_writeb(musb->mregs, MUSB_DEVCTL, devctl);
63 			/*
64 			 * Wait for the musb to set as A device to enable the
65 			 * VBUS
66 			 */
67 			while (musb_readb(musb->mregs, MUSB_DEVCTL) & 0x80) {
68 
69 				if (time_after(jiffies, timeout)) {
70 					dev_err(musb->controller,
71 					"configured as A device timeout");
72 					break;
73 				}
74 			}
75 
76 		} else {
77 			musb->is_active = 1;
78 			musb->xceiv->otg->default_a = 1;
79 			musb->xceiv->otg->state = OTG_STATE_A_WAIT_VRISE;
80 			devctl |= MUSB_DEVCTL_SESSION;
81 			MUSB_HST_MODE(musb);
82 		}
83 	} else {
84 		musb->is_active = 0;
85 
86 		/* NOTE: we're skipping A_WAIT_VFALL -> A_IDLE and jumping
87 		 * right to B_IDLE...
88 		 */
89 		musb->xceiv->otg->default_a = 0;
90 		devctl &= ~MUSB_DEVCTL_SESSION;
91 		MUSB_DEV_MODE(musb);
92 	}
93 	musb_writeb(musb->mregs, MUSB_DEVCTL, devctl);
94 
95 	/*
96 	 * Devctl values will be updated after vbus goes below
97 	 * session_valid. The time taken depends on the capacitance
98 	 * on VBUS line. The max discharge time can be upto 1 sec
99 	 * as per the spec. Typically on our platform, it is 200ms
100 	 */
101 	if (!is_on)
102 		mdelay(200);
103 
104 	dev_dbg(musb->controller, "VBUS %s, devctl %02x\n",
105 		usb_otg_state_string(musb->xceiv->otg->state),
106 		musb_readb(musb->mregs, MUSB_DEVCTL));
107 }
108 
109 static int musb_otg_notifications(struct notifier_block *nb,
110 		unsigned long event, void *unused)
111 {
112 	struct musb *musb = container_of(nb, struct musb, nb);
113 
114 	dev_dbg(musb->controller, "musb_otg_notifications %ld %s\n",
115 			event, usb_otg_state_string(musb->xceiv->otg->state));
116 
117 	switch (event) {
118 	case UX500_MUSB_ID:
119 		dev_dbg(musb->controller, "ID GND\n");
120 		ux500_musb_set_vbus(musb, 1);
121 		break;
122 	case UX500_MUSB_VBUS:
123 		dev_dbg(musb->controller, "VBUS Connect\n");
124 		break;
125 	case UX500_MUSB_NONE:
126 		dev_dbg(musb->controller, "VBUS Disconnect\n");
127 		if (is_host_active(musb))
128 			ux500_musb_set_vbus(musb, 0);
129 		else
130 			musb->xceiv->otg->state = OTG_STATE_B_IDLE;
131 		break;
132 	default:
133 		dev_dbg(musb->controller, "ID float\n");
134 		return NOTIFY_DONE;
135 	}
136 	return NOTIFY_OK;
137 }
138 
139 static irqreturn_t ux500_musb_interrupt(int irq, void *__hci)
140 {
141 	unsigned long   flags;
142 	irqreturn_t     retval = IRQ_NONE;
143 	struct musb     *musb = __hci;
144 
145 	spin_lock_irqsave(&musb->lock, flags);
146 
147 	musb->int_usb = musb_readb(musb->mregs, MUSB_INTRUSB);
148 	musb->int_tx = musb_readw(musb->mregs, MUSB_INTRTX);
149 	musb->int_rx = musb_readw(musb->mregs, MUSB_INTRRX);
150 
151 	if (musb->int_usb || musb->int_tx || musb->int_rx)
152 		retval = musb_interrupt(musb);
153 
154 	spin_unlock_irqrestore(&musb->lock, flags);
155 
156 	return retval;
157 }
158 
159 static int ux500_musb_init(struct musb *musb)
160 {
161 	int status;
162 
163 	musb->xceiv = usb_get_phy(USB_PHY_TYPE_USB2);
164 	if (IS_ERR_OR_NULL(musb->xceiv)) {
165 		pr_err("HS USB OTG: no transceiver configured\n");
166 		return -EPROBE_DEFER;
167 	}
168 
169 	musb->nb.notifier_call = musb_otg_notifications;
170 	status = usb_register_notifier(musb->xceiv, &musb->nb);
171 	if (status < 0) {
172 		dev_dbg(musb->controller, "notification register failed\n");
173 		return status;
174 	}
175 
176 	musb->isr = ux500_musb_interrupt;
177 
178 	return 0;
179 }
180 
181 static int ux500_musb_exit(struct musb *musb)
182 {
183 	usb_unregister_notifier(musb->xceiv, &musb->nb);
184 
185 	usb_put_phy(musb->xceiv);
186 
187 	return 0;
188 }
189 
190 static const struct musb_platform_ops ux500_ops = {
191 	.quirks		= MUSB_INDEXED_EP,
192 	.init		= ux500_musb_init,
193 	.exit		= ux500_musb_exit,
194 
195 	.set_vbus	= ux500_musb_set_vbus,
196 };
197 
198 static struct musb_hdrc_platform_data *
199 ux500_of_probe(struct platform_device *pdev, struct device_node *np)
200 {
201 	struct musb_hdrc_platform_data *pdata;
202 	const char *mode;
203 	int strlen;
204 
205 	pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
206 	if (!pdata)
207 		return NULL;
208 
209 	mode = of_get_property(np, "dr_mode", &strlen);
210 	if (!mode) {
211 		dev_err(&pdev->dev, "No 'dr_mode' property found\n");
212 		return NULL;
213 	}
214 
215 	if (strlen > 0) {
216 		if (!strcmp(mode, "host"))
217 			pdata->mode = MUSB_HOST;
218 		if (!strcmp(mode, "otg"))
219 			pdata->mode = MUSB_OTG;
220 		if (!strcmp(mode, "peripheral"))
221 			pdata->mode = MUSB_PERIPHERAL;
222 	}
223 
224 	return pdata;
225 }
226 
227 static int ux500_probe(struct platform_device *pdev)
228 {
229 	struct resource musb_resources[2];
230 	struct musb_hdrc_platform_data	*pdata = dev_get_platdata(&pdev->dev);
231 	struct device_node		*np = pdev->dev.of_node;
232 	struct platform_device		*musb;
233 	struct ux500_glue		*glue;
234 	struct clk			*clk;
235 	int				ret = -ENOMEM;
236 
237 	if (!pdata) {
238 		if (np) {
239 			pdata = ux500_of_probe(pdev, np);
240 			if (!pdata)
241 				goto err0;
242 
243 			pdev->dev.platform_data = pdata;
244 		} else {
245 			dev_err(&pdev->dev, "no pdata or device tree found\n");
246 			goto err0;
247 		}
248 	}
249 
250 	glue = devm_kzalloc(&pdev->dev, sizeof(*glue), GFP_KERNEL);
251 	if (!glue)
252 		goto err0;
253 
254 	musb = platform_device_alloc("musb-hdrc", PLATFORM_DEVID_AUTO);
255 	if (!musb) {
256 		dev_err(&pdev->dev, "failed to allocate musb device\n");
257 		goto err0;
258 	}
259 
260 	clk = devm_clk_get(&pdev->dev, NULL);
261 	if (IS_ERR(clk)) {
262 		dev_err(&pdev->dev, "failed to get clock\n");
263 		ret = PTR_ERR(clk);
264 		goto err1;
265 	}
266 
267 	ret = clk_prepare_enable(clk);
268 	if (ret) {
269 		dev_err(&pdev->dev, "failed to enable clock\n");
270 		goto err1;
271 	}
272 
273 	musb->dev.parent		= &pdev->dev;
274 	musb->dev.dma_mask		= &pdev->dev.coherent_dma_mask;
275 	musb->dev.coherent_dma_mask	= pdev->dev.coherent_dma_mask;
276 
277 	glue->dev			= &pdev->dev;
278 	glue->musb			= musb;
279 	glue->clk			= clk;
280 
281 	pdata->platform_ops		= &ux500_ops;
282 	pdata->config 			= &ux500_musb_hdrc_config;
283 
284 	platform_set_drvdata(pdev, glue);
285 
286 	memset(musb_resources, 0x00, sizeof(*musb_resources) *
287 			ARRAY_SIZE(musb_resources));
288 
289 	musb_resources[0].name = pdev->resource[0].name;
290 	musb_resources[0].start = pdev->resource[0].start;
291 	musb_resources[0].end = pdev->resource[0].end;
292 	musb_resources[0].flags = pdev->resource[0].flags;
293 
294 	musb_resources[1].name = pdev->resource[1].name;
295 	musb_resources[1].start = pdev->resource[1].start;
296 	musb_resources[1].end = pdev->resource[1].end;
297 	musb_resources[1].flags = pdev->resource[1].flags;
298 
299 	ret = platform_device_add_resources(musb, musb_resources,
300 			ARRAY_SIZE(musb_resources));
301 	if (ret) {
302 		dev_err(&pdev->dev, "failed to add resources\n");
303 		goto err2;
304 	}
305 
306 	ret = platform_device_add_data(musb, pdata, sizeof(*pdata));
307 	if (ret) {
308 		dev_err(&pdev->dev, "failed to add platform_data\n");
309 		goto err2;
310 	}
311 
312 	ret = platform_device_add(musb);
313 	if (ret) {
314 		dev_err(&pdev->dev, "failed to register musb device\n");
315 		goto err2;
316 	}
317 
318 	return 0;
319 
320 err2:
321 	clk_disable_unprepare(clk);
322 
323 err1:
324 	platform_device_put(musb);
325 
326 err0:
327 	return ret;
328 }
329 
330 static int ux500_remove(struct platform_device *pdev)
331 {
332 	struct ux500_glue	*glue = platform_get_drvdata(pdev);
333 
334 	platform_device_unregister(glue->musb);
335 	clk_disable_unprepare(glue->clk);
336 
337 	return 0;
338 }
339 
340 #ifdef CONFIG_PM
341 static int ux500_suspend(struct device *dev)
342 {
343 	struct ux500_glue	*glue = dev_get_drvdata(dev);
344 	struct musb		*musb = glue_to_musb(glue);
345 
346 	usb_phy_set_suspend(musb->xceiv, 1);
347 	clk_disable_unprepare(glue->clk);
348 
349 	return 0;
350 }
351 
352 static int ux500_resume(struct device *dev)
353 {
354 	struct ux500_glue	*glue = dev_get_drvdata(dev);
355 	struct musb		*musb = glue_to_musb(glue);
356 	int			ret;
357 
358 	ret = clk_prepare_enable(glue->clk);
359 	if (ret) {
360 		dev_err(dev, "failed to enable clock\n");
361 		return ret;
362 	}
363 
364 	usb_phy_set_suspend(musb->xceiv, 0);
365 
366 	return 0;
367 }
368 #endif
369 
370 static SIMPLE_DEV_PM_OPS(ux500_pm_ops, ux500_suspend, ux500_resume);
371 
372 static const struct of_device_id ux500_match[] = {
373         { .compatible = "stericsson,db8500-musb", },
374         {}
375 };
376 
377 static struct platform_driver ux500_driver = {
378 	.probe		= ux500_probe,
379 	.remove		= ux500_remove,
380 	.driver		= {
381 		.name	= "musb-ux500",
382 		.pm	= &ux500_pm_ops,
383 		.of_match_table = ux500_match,
384 	},
385 };
386 
387 MODULE_DESCRIPTION("UX500 MUSB Glue Layer");
388 MODULE_AUTHOR("Mian Yousaf Kaukab <mian.yousaf.kaukab@stericsson.com>");
389 MODULE_LICENSE("GPL v2");
390 module_platform_driver(ux500_driver);
391