blob: aa6f904fd1dd07a13b0c44e7ddadafa6c15c8238 [file] [log] [blame]
Sergei Shtylyovefe7daf2010-02-12 23:52:34 +04001/*
2 * OHCI HCD (Host Controller Driver) for USB.
3 *
4 * TI DA8xx (OMAP-L1x) Bus Glue
5 *
6 * Derived from: ohci-omap.c and ohci-s3c2410.c
7 * Copyright (C) 2008-2009 MontaVista Software, Inc. <source@mvista.com>
8 *
9 * This file is licensed under the terms of the GNU General Public License
10 * version 2. This program is licensed "as is" without any warranty of any
11 * kind, whether express or implied.
12 */
13
Manjunath Goudar6c21caa2016-10-27 15:52:29 +020014#include <linux/clk.h>
15#include <linux/io.h>
Sergei Shtylyovefe7daf2010-02-12 23:52:34 +040016#include <linux/interrupt.h>
17#include <linux/jiffies.h>
Manjunath Goudar6c21caa2016-10-27 15:52:29 +020018#include <linux/kernel.h>
19#include <linux/module.h>
Sergei Shtylyovefe7daf2010-02-12 23:52:34 +040020#include <linux/platform_device.h>
David Lechner6110c422016-10-12 20:44:46 -050021#include <linux/phy/phy.h>
Arnd Bergmannec2a0832012-08-24 15:11:34 +020022#include <linux/platform_data/usb-davinci.h>
Manjunath Goudar6c21caa2016-10-27 15:52:29 +020023#include <linux/usb.h>
24#include <linux/usb/hcd.h>
25#include <asm/unaligned.h>
Sergei Shtylyovefe7daf2010-02-12 23:52:34 +040026
Manjunath Goudar6c21caa2016-10-27 15:52:29 +020027#include "ohci.h"
28
29#define DRIVER_DESC "DA8XX"
Axel Haslameacae5d2016-11-03 17:03:08 +010030#define DRV_NAME "ohci-da8xx"
Manjunath Goudar6c21caa2016-10-27 15:52:29 +020031
32static struct hc_driver __read_mostly ohci_da8xx_hc_driver;
33
34static int (*orig_ohci_hub_control)(struct usb_hcd *hcd, u16 typeReq,
35 u16 wValue, u16 wIndex, char *buf, u16 wLength);
36static int (*orig_ohci_hub_status_data)(struct usb_hcd *hcd, char *buf);
Sergei Shtylyovefe7daf2010-02-12 23:52:34 +040037
Axel Haslamc7a4f9f2016-11-23 19:06:45 +010038struct da8xx_ohci_hcd {
39 struct clk *usb11_clk;
40 struct phy *usb11_phy;
41};
42
43#define to_da8xx_ohci(hcd) (struct da8xx_ohci_hcd *)(hcd_to_ohci(hcd)->priv)
Sergei Shtylyovefe7daf2010-02-12 23:52:34 +040044
45/* Over-current indicator change bitmask */
46static volatile u16 ocic_mask;
47
Axel Haslamc7a4f9f2016-11-23 19:06:45 +010048static int ohci_da8xx_enable(struct usb_hcd *hcd)
Sergei Shtylyovefe7daf2010-02-12 23:52:34 +040049{
Axel Haslamc7a4f9f2016-11-23 19:06:45 +010050 struct da8xx_ohci_hcd *da8xx_ohci = to_da8xx_ohci(hcd);
David Lechner6110c422016-10-12 20:44:46 -050051 int ret;
Sergei Shtylyovefe7daf2010-02-12 23:52:34 +040052
Axel Haslamc7a4f9f2016-11-23 19:06:45 +010053 ret = clk_prepare_enable(da8xx_ohci->usb11_clk);
David Lechner6110c422016-10-12 20:44:46 -050054 if (ret)
55 return ret;
Sergei Shtylyovefe7daf2010-02-12 23:52:34 +040056
Axel Haslamc7a4f9f2016-11-23 19:06:45 +010057 ret = phy_init(da8xx_ohci->usb11_phy);
David Lechner6110c422016-10-12 20:44:46 -050058 if (ret)
59 goto err_phy_init;
Sergei Shtylyovefe7daf2010-02-12 23:52:34 +040060
Axel Haslamc7a4f9f2016-11-23 19:06:45 +010061 ret = phy_power_on(da8xx_ohci->usb11_phy);
David Lechner6110c422016-10-12 20:44:46 -050062 if (ret)
63 goto err_phy_power_on;
Sergei Shtylyovefe7daf2010-02-12 23:52:34 +040064
David Lechner6110c422016-10-12 20:44:46 -050065 return 0;
Sergei Shtylyovefe7daf2010-02-12 23:52:34 +040066
David Lechner6110c422016-10-12 20:44:46 -050067err_phy_power_on:
Axel Haslamc7a4f9f2016-11-23 19:06:45 +010068 phy_exit(da8xx_ohci->usb11_phy);
David Lechner6110c422016-10-12 20:44:46 -050069err_phy_init:
Axel Haslamc7a4f9f2016-11-23 19:06:45 +010070 clk_disable_unprepare(da8xx_ohci->usb11_clk);
Sergei Shtylyovefe7daf2010-02-12 23:52:34 +040071
David Lechner6110c422016-10-12 20:44:46 -050072 return ret;
73}
74
Axel Haslamc7a4f9f2016-11-23 19:06:45 +010075static void ohci_da8xx_disable(struct usb_hcd *hcd)
David Lechner6110c422016-10-12 20:44:46 -050076{
Axel Haslamc7a4f9f2016-11-23 19:06:45 +010077 struct da8xx_ohci_hcd *da8xx_ohci = to_da8xx_ohci(hcd);
78
79 phy_power_off(da8xx_ohci->usb11_phy);
80 phy_exit(da8xx_ohci->usb11_phy);
81 clk_disable_unprepare(da8xx_ohci->usb11_clk);
Sergei Shtylyovefe7daf2010-02-12 23:52:34 +040082}
83
84/*
85 * Handle the port over-current indicator change.
86 */
87static void ohci_da8xx_ocic_handler(struct da8xx_ohci_root_hub *hub,
88 unsigned port)
89{
90 ocic_mask |= 1 << port;
91
92 /* Once over-current is detected, the port needs to be powered down */
93 if (hub->get_oci(port) > 0)
94 hub->set_power(port, 0);
95}
96
Manjunath Goudar6c21caa2016-10-27 15:52:29 +020097static int ohci_da8xx_reset(struct usb_hcd *hcd)
Sergei Shtylyovefe7daf2010-02-12 23:52:34 +040098{
99 struct device *dev = hcd->self.controller;
Jingoo Hand4f09e22013-07-30 19:59:40 +0900100 struct da8xx_ohci_root_hub *hub = dev_get_platdata(dev);
Sergei Shtylyovefe7daf2010-02-12 23:52:34 +0400101 struct ohci_hcd *ohci = hcd_to_ohci(hcd);
102 int result;
103 u32 rh_a;
104
105 dev_dbg(dev, "starting USB controller\n");
106
Axel Haslamc7a4f9f2016-11-23 19:06:45 +0100107 result = ohci_da8xx_enable(hcd);
David Lechner6110c422016-10-12 20:44:46 -0500108 if (result < 0)
109 return result;
Sergei Shtylyovefe7daf2010-02-12 23:52:34 +0400110
111 /*
112 * DA8xx only have 1 port connected to the pins but the HC root hub
113 * register A reports 2 ports, thus we'll have to override it...
114 */
115 ohci->num_ports = 1;
116
Manjunath Goudar6c21caa2016-10-27 15:52:29 +0200117 result = ohci_setup(hcd);
David Lechner6110c422016-10-12 20:44:46 -0500118 if (result < 0) {
Axel Haslamc7a4f9f2016-11-23 19:06:45 +0100119 ohci_da8xx_disable(hcd);
Sergei Shtylyovefe7daf2010-02-12 23:52:34 +0400120 return result;
David Lechner6110c422016-10-12 20:44:46 -0500121 }
Sergei Shtylyovefe7daf2010-02-12 23:52:34 +0400122
123 /*
124 * Since we're providing a board-specific root hub port power control
125 * and over-current reporting, we have to override the HC root hub A
126 * register's default value, so that ohci_hub_control() could return
127 * the correct hub descriptor...
128 */
129 rh_a = ohci_readl(ohci, &ohci->regs->roothub.a);
130 if (hub->set_power) {
131 rh_a &= ~RH_A_NPS;
132 rh_a |= RH_A_PSM;
133 }
134 if (hub->get_oci) {
135 rh_a &= ~RH_A_NOCP;
136 rh_a |= RH_A_OCPM;
137 }
138 rh_a &= ~RH_A_POTPGT;
139 rh_a |= hub->potpgt << 24;
140 ohci_writel(ohci, rh_a, &ohci->regs->roothub.a);
141
142 return result;
143}
144
Sergei Shtylyovefe7daf2010-02-12 23:52:34 +0400145/*
146 * Update the status data from the hub with the over-current indicator change.
147 */
148static int ohci_da8xx_hub_status_data(struct usb_hcd *hcd, char *buf)
149{
Manjunath Goudar6c21caa2016-10-27 15:52:29 +0200150 int length = orig_ohci_hub_status_data(hcd, buf);
Sergei Shtylyovefe7daf2010-02-12 23:52:34 +0400151
152 /* See if we have OCIC bit set on port 1 */
153 if (ocic_mask & (1 << 1)) {
154 dev_dbg(hcd->self.controller, "over-current indicator change "
155 "on port 1\n");
156
157 if (!length)
158 length = 1;
159
160 buf[0] |= 1 << 1;
161 }
162 return length;
163}
164
165/*
166 * Look at the control requests to the root hub and see if we need to override.
167 */
168static int ohci_da8xx_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
169 u16 wIndex, char *buf, u16 wLength)
170{
171 struct device *dev = hcd->self.controller;
Jingoo Hand4f09e22013-07-30 19:59:40 +0900172 struct da8xx_ohci_root_hub *hub = dev_get_platdata(dev);
Sergei Shtylyovefe7daf2010-02-12 23:52:34 +0400173 int temp;
174
175 switch (typeReq) {
176 case GetPortStatus:
177 /* Check the port number */
178 if (wIndex != 1)
179 break;
180
181 dev_dbg(dev, "GetPortStatus(%u)\n", wIndex);
182
183 temp = roothub_portstatus(hcd_to_ohci(hcd), wIndex - 1);
184
185 /* The port power status (PPS) bit defaults to 1 */
186 if (hub->get_power && hub->get_power(wIndex) == 0)
187 temp &= ~RH_PS_PPS;
188
189 /* The port over-current indicator (POCI) bit is always 0 */
190 if (hub->get_oci && hub->get_oci(wIndex) > 0)
191 temp |= RH_PS_POCI;
192
193 /* The over-current indicator change (OCIC) bit is 0 too */
194 if (ocic_mask & (1 << wIndex))
195 temp |= RH_PS_OCIC;
196
197 put_unaligned(cpu_to_le32(temp), (__le32 *)buf);
198 return 0;
199 case SetPortFeature:
200 temp = 1;
201 goto check_port;
202 case ClearPortFeature:
203 temp = 0;
204
205check_port:
206 /* Check the port number */
207 if (wIndex != 1)
208 break;
209
210 switch (wValue) {
211 case USB_PORT_FEAT_POWER:
212 dev_dbg(dev, "%sPortFeature(%u): %s\n",
213 temp ? "Set" : "Clear", wIndex, "POWER");
214
215 if (!hub->set_power)
216 return -EPIPE;
217
218 return hub->set_power(wIndex, temp) ? -EPIPE : 0;
219 case USB_PORT_FEAT_C_OVER_CURRENT:
220 dev_dbg(dev, "%sPortFeature(%u): %s\n",
221 temp ? "Set" : "Clear", wIndex,
222 "C_OVER_CURRENT");
223
224 if (temp)
225 ocic_mask |= 1 << wIndex;
226 else
227 ocic_mask &= ~(1 << wIndex);
228 return 0;
229 }
230 }
231
Manjunath Goudar6c21caa2016-10-27 15:52:29 +0200232 return orig_ohci_hub_control(hcd, typeReq, wValue,
233 wIndex, buf, wLength);
Sergei Shtylyovefe7daf2010-02-12 23:52:34 +0400234}
235
Sergei Shtylyovefe7daf2010-02-12 23:52:34 +0400236/*-------------------------------------------------------------------------*/
237
Manjunath Goudar6c21caa2016-10-27 15:52:29 +0200238static int ohci_da8xx_probe(struct platform_device *pdev)
Sergei Shtylyovefe7daf2010-02-12 23:52:34 +0400239{
Jingoo Hand4f09e22013-07-30 19:59:40 +0900240 struct da8xx_ohci_root_hub *hub = dev_get_platdata(&pdev->dev);
Axel Haslamc7a4f9f2016-11-23 19:06:45 +0100241 struct da8xx_ohci_hcd *da8xx_ohci;
Sergei Shtylyovefe7daf2010-02-12 23:52:34 +0400242 struct usb_hcd *hcd;
243 struct resource *mem;
244 int error, irq;
245
246 if (hub == NULL)
247 return -ENODEV;
248
Manjunath Goudar6c21caa2016-10-27 15:52:29 +0200249 hcd = usb_create_hcd(&ohci_da8xx_hc_driver, &pdev->dev,
250 dev_name(&pdev->dev));
Jingoo Han644db162013-12-11 16:23:39 +0900251 if (!hcd)
252 return -ENOMEM;
Sergei Shtylyovefe7daf2010-02-12 23:52:34 +0400253
Axel Haslamc7a4f9f2016-11-23 19:06:45 +0100254 da8xx_ohci = to_da8xx_ohci(hcd);
255
256 da8xx_ohci->usb11_clk = devm_clk_get(&pdev->dev, "usb11");
257 if (IS_ERR(da8xx_ohci->usb11_clk)) {
258 error = PTR_ERR(da8xx_ohci->usb11_clk);
259 if (error != -EPROBE_DEFER)
260 dev_err(&pdev->dev, "Failed to get clock.\n");
261 goto err;
262 }
263
264 da8xx_ohci->usb11_phy = devm_phy_get(&pdev->dev, "usb-phy");
265 if (IS_ERR(da8xx_ohci->usb11_phy)) {
266 error = PTR_ERR(da8xx_ohci->usb11_phy);
267 if (error != -EPROBE_DEFER)
268 dev_err(&pdev->dev, "Failed to get phy.\n");
269 goto err;
270 }
271
Sergei Shtylyovefe7daf2010-02-12 23:52:34 +0400272 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Jingoo Han644db162013-12-11 16:23:39 +0900273 hcd->regs = devm_ioremap_resource(&pdev->dev, mem);
274 if (IS_ERR(hcd->regs)) {
275 error = PTR_ERR(hcd->regs);
276 goto err;
Sergei Shtylyovefe7daf2010-02-12 23:52:34 +0400277 }
Varka Bhadram54891d72014-11-04 07:51:09 +0530278 hcd->rsrc_start = mem->start;
279 hcd->rsrc_len = resource_size(mem);
Sergei Shtylyovefe7daf2010-02-12 23:52:34 +0400280
Sergei Shtylyovefe7daf2010-02-12 23:52:34 +0400281 irq = platform_get_irq(pdev, 0);
282 if (irq < 0) {
283 error = -ENODEV;
Jingoo Han644db162013-12-11 16:23:39 +0900284 goto err;
Sergei Shtylyovefe7daf2010-02-12 23:52:34 +0400285 }
Manjunath Goudar6c21caa2016-10-27 15:52:29 +0200286
Yong Zhangb5dd18d2011-09-07 16:10:52 +0800287 error = usb_add_hcd(hcd, irq, 0);
Sergei Shtylyovefe7daf2010-02-12 23:52:34 +0400288 if (error)
Jingoo Han644db162013-12-11 16:23:39 +0900289 goto err;
Sergei Shtylyovefe7daf2010-02-12 23:52:34 +0400290
Peter Chen3c9740a2013-11-05 10:46:02 +0800291 device_wakeup_enable(hcd->self.controller);
292
Sergei Shtylyovefe7daf2010-02-12 23:52:34 +0400293 if (hub->ocic_notify) {
294 error = hub->ocic_notify(ohci_da8xx_ocic_handler);
295 if (!error)
296 return 0;
297 }
298
299 usb_remove_hcd(hcd);
Jingoo Han644db162013-12-11 16:23:39 +0900300err:
Sergei Shtylyovefe7daf2010-02-12 23:52:34 +0400301 usb_put_hcd(hcd);
Sergei Shtylyovefe7daf2010-02-12 23:52:34 +0400302 return error;
303}
304
Manjunath Goudar6c21caa2016-10-27 15:52:29 +0200305static int ohci_da8xx_remove(struct platform_device *pdev)
Sergei Shtylyovefe7daf2010-02-12 23:52:34 +0400306{
Manjunath Goudar6c21caa2016-10-27 15:52:29 +0200307 struct usb_hcd *hcd = platform_get_drvdata(pdev);
Jingoo Hand4f09e22013-07-30 19:59:40 +0900308 struct da8xx_ohci_root_hub *hub = dev_get_platdata(&pdev->dev);
Sergei Shtylyovefe7daf2010-02-12 23:52:34 +0400309
310 hub->ocic_notify(NULL);
311 usb_remove_hcd(hcd);
Sergei Shtylyovefe7daf2010-02-12 23:52:34 +0400312 usb_put_hcd(hcd);
Sergei Shtylyovefe7daf2010-02-12 23:52:34 +0400313
314 return 0;
315}
316
317#ifdef CONFIG_PM
Majunath Goudar933bb1f2013-11-13 17:40:19 +0530318static int ohci_da8xx_suspend(struct platform_device *pdev,
319 pm_message_t message)
Sergei Shtylyovefe7daf2010-02-12 23:52:34 +0400320{
Majunath Goudar933bb1f2013-11-13 17:40:19 +0530321 struct usb_hcd *hcd = platform_get_drvdata(pdev);
Sergei Shtylyovefe7daf2010-02-12 23:52:34 +0400322 struct ohci_hcd *ohci = hcd_to_ohci(hcd);
Majunath Goudar933bb1f2013-11-13 17:40:19 +0530323 bool do_wakeup = device_may_wakeup(&pdev->dev);
324 int ret;
325
Sergei Shtylyovefe7daf2010-02-12 23:52:34 +0400326
327 if (time_before(jiffies, ohci->next_statechange))
328 msleep(5);
329 ohci->next_statechange = jiffies;
330
Majunath Goudar933bb1f2013-11-13 17:40:19 +0530331 ret = ohci_suspend(hcd, do_wakeup);
332 if (ret)
333 return ret;
334
Axel Haslamc7a4f9f2016-11-23 19:06:45 +0100335 ohci_da8xx_disable(hcd);
Sergei Shtylyovefe7daf2010-02-12 23:52:34 +0400336 hcd->state = HC_STATE_SUSPENDED;
Majunath Goudar933bb1f2013-11-13 17:40:19 +0530337
338 return ret;
Sergei Shtylyovefe7daf2010-02-12 23:52:34 +0400339}
340
341static int ohci_da8xx_resume(struct platform_device *dev)
342{
343 struct usb_hcd *hcd = platform_get_drvdata(dev);
344 struct ohci_hcd *ohci = hcd_to_ohci(hcd);
David Lechner6110c422016-10-12 20:44:46 -0500345 int ret;
Sergei Shtylyovefe7daf2010-02-12 23:52:34 +0400346
347 if (time_before(jiffies, ohci->next_statechange))
348 msleep(5);
349 ohci->next_statechange = jiffies;
350
Axel Haslamc7a4f9f2016-11-23 19:06:45 +0100351 ret = ohci_da8xx_enable(hcd);
David Lechner6110c422016-10-12 20:44:46 -0500352 if (ret)
353 return ret;
354
Sergei Shtylyovefe7daf2010-02-12 23:52:34 +0400355 dev->dev.power.power_state = PMSG_ON;
356 usb_hcd_resume_root_hub(hcd);
David Lechner6110c422016-10-12 20:44:46 -0500357
Sergei Shtylyovefe7daf2010-02-12 23:52:34 +0400358 return 0;
359}
360#endif
361
Manjunath Goudar6c21caa2016-10-27 15:52:29 +0200362static const struct ohci_driver_overrides da8xx_overrides __initconst = {
Axel Haslamc7a4f9f2016-11-23 19:06:45 +0100363 .reset = ohci_da8xx_reset,
364 .extra_priv_size = sizeof(struct da8xx_ohci_hcd),
Manjunath Goudar6c21caa2016-10-27 15:52:29 +0200365};
366
Sergei Shtylyovefe7daf2010-02-12 23:52:34 +0400367/*
368 * Driver definition to register with platform structure.
369 */
370static struct platform_driver ohci_hcd_da8xx_driver = {
Manjunath Goudar6c21caa2016-10-27 15:52:29 +0200371 .probe = ohci_da8xx_probe,
372 .remove = ohci_da8xx_remove,
Sergei Shtylyovefe7daf2010-02-12 23:52:34 +0400373 .shutdown = usb_hcd_platform_shutdown,
374#ifdef CONFIG_PM
375 .suspend = ohci_da8xx_suspend,
376 .resume = ohci_da8xx_resume,
377#endif
378 .driver = {
Manjunath Goudar6c21caa2016-10-27 15:52:29 +0200379 .name = DRV_NAME,
Sergei Shtylyovefe7daf2010-02-12 23:52:34 +0400380 },
381};
Jan Luebbeab59ac012012-05-07 10:25:16 +0200382
Manjunath Goudar6c21caa2016-10-27 15:52:29 +0200383static int __init ohci_da8xx_init(void)
384{
385
386 if (usb_disabled())
387 return -ENODEV;
388
389 pr_info("%s: " DRIVER_DESC "\n", DRV_NAME);
390 ohci_init_driver(&ohci_da8xx_hc_driver, &da8xx_overrides);
391
392 /*
393 * The Davinci da8xx HW has some unusual quirks, which require
394 * da8xx-specific workarounds. We override certain hc_driver
395 * functions here to achieve that. We explicitly do not enhance
396 * ohci_driver_overrides to allow this more easily, since this
397 * is an unusual case, and we don't want to encourage others to
398 * override these functions by making it too easy.
399 */
400
401 orig_ohci_hub_control = ohci_da8xx_hc_driver.hub_control;
402 orig_ohci_hub_status_data = ohci_da8xx_hc_driver.hub_status_data;
403
404 ohci_da8xx_hc_driver.hub_status_data = ohci_da8xx_hub_status_data;
405 ohci_da8xx_hc_driver.hub_control = ohci_da8xx_hub_control;
406
407 return platform_driver_register(&ohci_hcd_da8xx_driver);
408}
409module_init(ohci_da8xx_init);
410
411static void __exit ohci_da8xx_exit(void)
412{
413 platform_driver_unregister(&ohci_hcd_da8xx_driver);
414}
415module_exit(ohci_da8xx_exit);
416MODULE_DESCRIPTION(DRIVER_DESC);
417MODULE_LICENSE("GPL");
418MODULE_ALIAS("platform:" DRV_NAME);