blob: 39406607e53a4eea37333106ddf56c9da0519603 [file] [log] [blame]
Andrew Victor39a269c2006-01-22 10:32:13 -08001/*
2 * OHCI HCD (Host Controller Driver) for USB.
3 *
4 * Copyright (C) 2004 SAN People (Pty) Ltd.
5 * Copyright (C) 2005 Thibaut VARENE <varenet@parisc-linux.org>
6 *
David Brownell0365ee02006-06-19 14:27:20 -07007 * AT91 Bus Glue
Andrew Victor39a269c2006-01-22 10:32:13 -08008 *
9 * Based on fragments of 2.4 driver by Rick Bronson.
10 * Based on ohci-omap.c
11 *
12 * This file is licenced under the GPL.
13 */
14
15#include <linux/clk.h>
Manjunath Goudare3825b42013-09-21 16:38:42 +053016#include <linux/dma-mapping.h>
Jean-Christophe PLAGNIOL-VILLARD24197302011-11-21 06:55:18 +080017#include <linux/of_platform.h>
18#include <linux/of_gpio.h>
Manjunath Goudare3825b42013-09-21 16:38:42 +053019#include <linux/platform_device.h>
Jean-Christophe PLAGNIOL-VILLARDbcd23602012-10-30 05:12:23 +080020#include <linux/platform_data/atmel.h>
Manjunath Goudare3825b42013-09-21 16:38:42 +053021#include <linux/io.h>
22#include <linux/kernel.h>
23#include <linux/module.h>
24#include <linux/usb.h>
25#include <linux/usb/hcd.h>
Andrew Victor39a269c2006-01-22 10:32:13 -080026
David Brownell4bde4a42007-12-27 11:19:49 -080027#include <asm/gpio.h>
28
Manjunath Goudare3825b42013-09-21 16:38:42 +053029#include "ohci.h"
Andrew Victor39a269c2006-01-22 10:32:13 -080030
Nicolas Ferre0ee6d1e2012-03-21 16:53:09 +010031#define valid_port(index) ((index) >= 0 && (index) < AT91_MAX_USBH_PORTS)
32#define at91_for_each_port(index) \
33 for ((index) = 0; (index) < AT91_MAX_USBH_PORTS; (index)++)
34
Boris BREZILLON6b0a1cf2013-08-01 19:09:13 +020035/* interface, function and usb clocks; sometimes also an AHB clock */
36static struct clk *iclk, *fclk, *uclk, *hclk;
Manjunath Goudare3825b42013-09-21 16:38:42 +053037/* interface and function clocks; sometimes also an AHB clock */
38
39#define DRIVER_DESC "OHCI Atmel driver"
40
41static const char hcd_name[] = "ohci-atmel";
42
43static struct hc_driver __read_mostly ohci_at91_hc_driver;
David Brownell0365ee02006-06-19 14:27:20 -070044static int clocked;
Andrew Victor39a269c2006-01-22 10:32:13 -080045
46extern int usb_disabled(void);
47
48/*-------------------------------------------------------------------------*/
49
Andrew Victored077bb2007-02-16 10:18:58 -080050static void at91_start_clock(void)
51{
Boris BREZILLON6b0a1cf2013-08-01 19:09:13 +020052 if (IS_ENABLED(CONFIG_COMMON_CLK)) {
53 clk_set_rate(uclk, 48000000);
54 clk_prepare_enable(uclk);
55 }
Boris BREZILLON8e82d8d2013-06-19 13:21:08 +020056 clk_prepare_enable(hclk);
57 clk_prepare_enable(iclk);
58 clk_prepare_enable(fclk);
Andrew Victored077bb2007-02-16 10:18:58 -080059 clocked = 1;
60}
61
62static void at91_stop_clock(void)
63{
Boris BREZILLON8e82d8d2013-06-19 13:21:08 +020064 clk_disable_unprepare(fclk);
65 clk_disable_unprepare(iclk);
66 clk_disable_unprepare(hclk);
Boris BREZILLON6b0a1cf2013-08-01 19:09:13 +020067 if (IS_ENABLED(CONFIG_COMMON_CLK))
68 clk_disable_unprepare(uclk);
Andrew Victored077bb2007-02-16 10:18:58 -080069 clocked = 0;
70}
71
Andrew Victor39a269c2006-01-22 10:32:13 -080072static void at91_start_hc(struct platform_device *pdev)
73{
74 struct usb_hcd *hcd = platform_get_drvdata(pdev);
75 struct ohci_regs __iomem *regs = hcd->regs;
76
David Brownell0365ee02006-06-19 14:27:20 -070077 dev_dbg(&pdev->dev, "start\n");
Andrew Victor39a269c2006-01-22 10:32:13 -080078
79 /*
80 * Start the USB clocks.
81 */
Andrew Victored077bb2007-02-16 10:18:58 -080082 at91_start_clock();
Andrew Victor39a269c2006-01-22 10:32:13 -080083
84 /*
85 * The USB host controller must remain in reset.
86 */
87 writel(0, &regs->control);
88}
89
90static void at91_stop_hc(struct platform_device *pdev)
91{
92 struct usb_hcd *hcd = platform_get_drvdata(pdev);
93 struct ohci_regs __iomem *regs = hcd->regs;
94
David Brownell0365ee02006-06-19 14:27:20 -070095 dev_dbg(&pdev->dev, "stop\n");
Andrew Victor39a269c2006-01-22 10:32:13 -080096
97 /*
98 * Put the USB host controller into reset.
99 */
100 writel(0, &regs->control);
101
102 /*
103 * Stop the USB clocks.
104 */
Andrew Victored077bb2007-02-16 10:18:58 -0800105 at91_stop_clock();
Andrew Victor39a269c2006-01-22 10:32:13 -0800106}
107
108
109/*-------------------------------------------------------------------------*/
110
Bill Pembertonfb4e98a2012-11-19 13:26:20 -0500111static void usb_hcd_at91_remove (struct usb_hcd *, struct platform_device *);
Andrew Victor39a269c2006-01-22 10:32:13 -0800112
113/* configure so an HC device and id are always provided */
114/* always called with process context; sleeping is OK */
115
116
117/**
David Brownell0365ee02006-06-19 14:27:20 -0700118 * usb_hcd_at91_probe - initialize AT91-based HCDs
Andrew Victor39a269c2006-01-22 10:32:13 -0800119 * Context: !in_interrupt()
120 *
121 * Allocates basic resources for this USB host controller, and
122 * then invokes the start() method for the HCD associated with it
123 * through the hotplug entry's driver_data.
Andrew Victor39a269c2006-01-22 10:32:13 -0800124 */
Bill Pemberton41ac7b32012-11-19 13:21:48 -0500125static int usb_hcd_at91_probe(const struct hc_driver *driver,
David Brownell0365ee02006-06-19 14:27:20 -0700126 struct platform_device *pdev)
Andrew Victor39a269c2006-01-22 10:32:13 -0800127{
Manjunath Goudare3825b42013-09-21 16:38:42 +0530128 struct at91_usbh_data *board;
129 struct ohci_hcd *ohci;
Andrew Victor39a269c2006-01-22 10:32:13 -0800130 int retval;
131 struct usb_hcd *hcd = NULL;
Boris BREZILLONfb5f1832013-12-08 15:59:59 +0100132 struct device *dev = &pdev->dev;
133 struct resource *res;
134 int irq;
Andrew Victor39a269c2006-01-22 10:32:13 -0800135
Boris BREZILLONfb5f1832013-12-08 15:59:59 +0100136 irq = platform_get_irq(pdev, 0);
137 if (irq < 0) {
138 dev_dbg(dev, "hcd probe: missing irq resource\n");
139 return irq;
Andrew Victor39a269c2006-01-22 10:32:13 -0800140 }
141
Boris BREZILLON5b218a02013-12-09 09:51:54 +0100142 hcd = usb_create_hcd(driver, dev, "at91");
Andrew Victor39a269c2006-01-22 10:32:13 -0800143 if (!hcd)
144 return -ENOMEM;
Andrew Victor39a269c2006-01-22 10:32:13 -0800145
Varka Bhadram04dc3152014-11-04 07:51:12 +0530146 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Boris BREZILLONcca2bbb2013-12-09 09:51:53 +0100147 hcd->regs = devm_ioremap_resource(dev, res);
148 if (IS_ERR(hcd->regs)) {
149 retval = PTR_ERR(hcd->regs);
150 goto err;
Andrew Victor39a269c2006-01-22 10:32:13 -0800151 }
Varka Bhadram04dc3152014-11-04 07:51:12 +0530152 hcd->rsrc_start = res->start;
153 hcd->rsrc_len = resource_size(res);
Andrew Victor39a269c2006-01-22 10:32:13 -0800154
Boris BREZILLONfc7a3252013-12-09 09:51:55 +0100155 iclk = devm_clk_get(dev, "ohci_clk");
Jean-Christophe PLAGNIOL-VILLARD68134632011-12-08 16:32:00 +0100156 if (IS_ERR(iclk)) {
Boris BREZILLON5b218a02013-12-09 09:51:54 +0100157 dev_err(dev, "failed to get ohci_clk\n");
Jean-Christophe PLAGNIOL-VILLARD68134632011-12-08 16:32:00 +0100158 retval = PTR_ERR(iclk);
Boris BREZILLONcca2bbb2013-12-09 09:51:53 +0100159 goto err;
Jean-Christophe PLAGNIOL-VILLARD68134632011-12-08 16:32:00 +0100160 }
Boris BREZILLONfc7a3252013-12-09 09:51:55 +0100161 fclk = devm_clk_get(dev, "uhpck");
Jean-Christophe PLAGNIOL-VILLARD68134632011-12-08 16:32:00 +0100162 if (IS_ERR(fclk)) {
Boris BREZILLON5b218a02013-12-09 09:51:54 +0100163 dev_err(dev, "failed to get uhpck\n");
Jean-Christophe PLAGNIOL-VILLARD68134632011-12-08 16:32:00 +0100164 retval = PTR_ERR(fclk);
Boris BREZILLONfc7a3252013-12-09 09:51:55 +0100165 goto err;
Jean-Christophe PLAGNIOL-VILLARD68134632011-12-08 16:32:00 +0100166 }
Boris BREZILLONfc7a3252013-12-09 09:51:55 +0100167 hclk = devm_clk_get(dev, "hclk");
Jean-Christophe PLAGNIOL-VILLARD68134632011-12-08 16:32:00 +0100168 if (IS_ERR(hclk)) {
Boris BREZILLON5b218a02013-12-09 09:51:54 +0100169 dev_err(dev, "failed to get hclk\n");
Jean-Christophe PLAGNIOL-VILLARD68134632011-12-08 16:32:00 +0100170 retval = PTR_ERR(hclk);
Boris BREZILLONfc7a3252013-12-09 09:51:55 +0100171 goto err;
Jean-Christophe PLAGNIOL-VILLARD68134632011-12-08 16:32:00 +0100172 }
Boris BREZILLON6b0a1cf2013-08-01 19:09:13 +0200173 if (IS_ENABLED(CONFIG_COMMON_CLK)) {
Boris BREZILLONfc7a3252013-12-09 09:51:55 +0100174 uclk = devm_clk_get(dev, "usb_clk");
Boris BREZILLON6b0a1cf2013-08-01 19:09:13 +0200175 if (IS_ERR(uclk)) {
Boris BREZILLON5b218a02013-12-09 09:51:54 +0100176 dev_err(dev, "failed to get uclk\n");
Boris BREZILLON6b0a1cf2013-08-01 19:09:13 +0200177 retval = PTR_ERR(uclk);
Boris BREZILLONfc7a3252013-12-09 09:51:55 +0100178 goto err;
Boris BREZILLON6b0a1cf2013-08-01 19:09:13 +0200179 }
180 }
Andrew Victor39a269c2006-01-22 10:32:13 -0800181
Manjunath Goudare3825b42013-09-21 16:38:42 +0530182 board = hcd->self.controller->platform_data;
183 ohci = hcd_to_ohci(hcd);
184 ohci->num_ports = board->ports;
Andrew Victor39a269c2006-01-22 10:32:13 -0800185 at91_start_hc(pdev);
Andrew Victor39a269c2006-01-22 10:32:13 -0800186
Boris BREZILLONfb5f1832013-12-08 15:59:59 +0100187 retval = usb_add_hcd(hcd, irq, IRQF_SHARED);
Arnd Bergmann1f53b482014-01-07 12:54:58 +0100188 if (retval == 0) {
Peter Chen3c9740a2013-11-05 10:46:02 +0800189 device_wakeup_enable(hcd->self.controller);
Andrew Victor39a269c2006-01-22 10:32:13 -0800190 return retval;
Peter Chen3c9740a2013-11-05 10:46:02 +0800191 }
Andrew Victor39a269c2006-01-22 10:32:13 -0800192
193 /* Error handling */
194 at91_stop_hc(pdev);
195
Boris BREZILLONcca2bbb2013-12-09 09:51:53 +0100196 err:
Andrew Victor39a269c2006-01-22 10:32:13 -0800197 usb_put_hcd(hcd);
198 return retval;
199}
200
201
Andrew Victor39a269c2006-01-22 10:32:13 -0800202/* may be called with controller, bus, and devices active */
203
204/**
David Brownell0365ee02006-06-19 14:27:20 -0700205 * usb_hcd_at91_remove - shutdown processing for AT91-based HCDs
Andrew Victor39a269c2006-01-22 10:32:13 -0800206 * @dev: USB Host Controller being removed
207 * Context: !in_interrupt()
208 *
209 * Reverses the effect of usb_hcd_at91_probe(), first invoking
210 * the HCD's stop() method. It is always called from a thread
David Brownell0365ee02006-06-19 14:27:20 -0700211 * context, "rmmod" or something similar.
Andrew Victor39a269c2006-01-22 10:32:13 -0800212 *
213 */
Bill Pembertonfb4e98a2012-11-19 13:26:20 -0500214static void usb_hcd_at91_remove(struct usb_hcd *hcd,
David Brownell0365ee02006-06-19 14:27:20 -0700215 struct platform_device *pdev)
Andrew Victor39a269c2006-01-22 10:32:13 -0800216{
217 usb_remove_hcd(hcd);
218 at91_stop_hc(pdev);
Pete Zaitcev421b4bf2008-06-01 14:38:43 -0700219 usb_put_hcd(hcd);
Andrew Victor39a269c2006-01-22 10:32:13 -0800220}
221
222/*-------------------------------------------------------------------------*/
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200223static void ohci_at91_usb_set_power(struct at91_usbh_data *pdata, int port, int enable)
224{
Nicolas Ferre0ee6d1e2012-03-21 16:53:09 +0100225 if (!valid_port(port))
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200226 return;
227
Jean-Christophe PLAGNIOL-VILLARD8a7a49d2011-09-19 15:32:23 +0800228 if (!gpio_is_valid(pdata->vbus_pin[port]))
Jean-Christophe PLAGNIOL-VILLARD770f0ba2011-11-12 16:46:13 +0100229 return;
230
Jean-Christophe PLAGNIOL-VILLARD8134ff52011-12-08 16:32:01 +0100231 gpio_set_value(pdata->vbus_pin[port],
Nicolas Ferre1e7caf82012-03-21 14:38:55 +0100232 pdata->vbus_pin_active_low[port] ^ enable);
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200233}
234
235static int ohci_at91_usb_get_power(struct at91_usbh_data *pdata, int port)
236{
Nicolas Ferre0ee6d1e2012-03-21 16:53:09 +0100237 if (!valid_port(port))
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200238 return -EINVAL;
239
Jean-Christophe PLAGNIOL-VILLARD8a7a49d2011-09-19 15:32:23 +0800240 if (!gpio_is_valid(pdata->vbus_pin[port]))
Jean-Christophe PLAGNIOL-VILLARD770f0ba2011-11-12 16:46:13 +0100241 return -EINVAL;
242
Jean-Christophe PLAGNIOL-VILLARD8134ff52011-12-08 16:32:01 +0100243 return gpio_get_value(pdata->vbus_pin[port]) ^
Nicolas Ferre1e7caf82012-03-21 14:38:55 +0100244 pdata->vbus_pin_active_low[port];
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200245}
246
247/*
248 * Update the status data from the hub with the over-current indicator change.
249 */
250static int ohci_at91_hub_status_data(struct usb_hcd *hcd, char *buf)
251{
Manjunath Goudare3825b42013-09-21 16:38:42 +0530252 struct at91_usbh_data *pdata = hcd->self.controller->platform_data;
Laurent Pinchart42b59eb2014-04-16 18:00:09 +0200253 int length = ohci_hub_status_data(hcd, buf);
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200254 int port;
255
Nicolas Ferre0ee6d1e2012-03-21 16:53:09 +0100256 at91_for_each_port(port) {
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200257 if (pdata->overcurrent_changed[port]) {
Nicolas Ferre0ee6d1e2012-03-21 16:53:09 +0100258 if (!length)
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200259 length = 1;
260 buf[0] |= 1 << (port + 1);
261 }
262 }
263
264 return length;
265}
266
267/*
268 * Look at the control requests to the root hub and see if we need to override.
269 */
270static int ohci_at91_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
271 u16 wIndex, char *buf, u16 wLength)
272{
Jingoo Hand4f09e22013-07-30 19:59:40 +0900273 struct at91_usbh_data *pdata = dev_get_platdata(hcd->self.controller);
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200274 struct usb_hub_descriptor *desc;
275 int ret = -EINVAL;
276 u32 *data = (u32 *)buf;
277
278 dev_dbg(hcd->self.controller,
279 "ohci_at91_hub_control(%p,0x%04x,0x%04x,0x%04x,%p,%04x)\n",
280 hcd, typeReq, wValue, wIndex, buf, wLength);
281
Nicolas Ferre0ee6d1e2012-03-21 16:53:09 +0100282 wIndex--;
283
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200284 switch (typeReq) {
285 case SetPortFeature:
286 if (wValue == USB_PORT_FEAT_POWER) {
287 dev_dbg(hcd->self.controller, "SetPortFeat: POWER\n");
Nicolas Ferre0ee6d1e2012-03-21 16:53:09 +0100288 if (valid_port(wIndex)) {
289 ohci_at91_usb_set_power(pdata, wIndex, 1);
290 ret = 0;
291 }
292
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200293 goto out;
294 }
295 break;
296
297 case ClearPortFeature:
298 switch (wValue) {
299 case USB_PORT_FEAT_C_OVER_CURRENT:
300 dev_dbg(hcd->self.controller,
301 "ClearPortFeature: C_OVER_CURRENT\n");
302
Nicolas Ferre0ee6d1e2012-03-21 16:53:09 +0100303 if (valid_port(wIndex)) {
304 pdata->overcurrent_changed[wIndex] = 0;
305 pdata->overcurrent_status[wIndex] = 0;
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200306 }
307
308 goto out;
309
310 case USB_PORT_FEAT_OVER_CURRENT:
311 dev_dbg(hcd->self.controller,
312 "ClearPortFeature: OVER_CURRENT\n");
313
Nicolas Ferre0ee6d1e2012-03-21 16:53:09 +0100314 if (valid_port(wIndex))
315 pdata->overcurrent_status[wIndex] = 0;
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200316
317 goto out;
318
319 case USB_PORT_FEAT_POWER:
320 dev_dbg(hcd->self.controller,
321 "ClearPortFeature: POWER\n");
322
Nicolas Ferre0ee6d1e2012-03-21 16:53:09 +0100323 if (valid_port(wIndex)) {
324 ohci_at91_usb_set_power(pdata, wIndex, 0);
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200325 return 0;
326 }
327 }
328 break;
329 }
330
Laurent Pinchart42b59eb2014-04-16 18:00:09 +0200331 ret = ohci_hub_control(hcd, typeReq, wValue, wIndex + 1, buf, wLength);
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200332 if (ret)
333 goto out;
334
335 switch (typeReq) {
336 case GetHubDescriptor:
337
338 /* update the hub's descriptor */
339
340 desc = (struct usb_hub_descriptor *)buf;
341
342 dev_dbg(hcd->self.controller, "wHubCharacteristics 0x%04x\n",
343 desc->wHubCharacteristics);
344
345 /* remove the old configurations for power-switching, and
346 * over-current protection, and insert our new configuration
347 */
348
349 desc->wHubCharacteristics &= ~cpu_to_le16(HUB_CHAR_LPSM);
350 desc->wHubCharacteristics |= cpu_to_le16(0x0001);
351
352 if (pdata->overcurrent_supported) {
353 desc->wHubCharacteristics &= ~cpu_to_le16(HUB_CHAR_OCPM);
354 desc->wHubCharacteristics |= cpu_to_le16(0x0008|0x0001);
355 }
356
357 dev_dbg(hcd->self.controller, "wHubCharacteristics after 0x%04x\n",
358 desc->wHubCharacteristics);
359
360 return ret;
361
362 case GetPortStatus:
363 /* check port status */
364
365 dev_dbg(hcd->self.controller, "GetPortStatus(%d)\n", wIndex);
366
Nicolas Ferre0ee6d1e2012-03-21 16:53:09 +0100367 if (valid_port(wIndex)) {
368 if (!ohci_at91_usb_get_power(pdata, wIndex))
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200369 *data &= ~cpu_to_le32(RH_PS_PPS);
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200370
Nicolas Ferre0ee6d1e2012-03-21 16:53:09 +0100371 if (pdata->overcurrent_changed[wIndex])
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200372 *data |= cpu_to_le32(RH_PS_OCIC);
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200373
Nicolas Ferre0ee6d1e2012-03-21 16:53:09 +0100374 if (pdata->overcurrent_status[wIndex])
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200375 *data |= cpu_to_le32(RH_PS_POCI);
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200376 }
377 }
378
379 out:
380 return ret;
381}
382
Andrew Victor39a269c2006-01-22 10:32:13 -0800383/*-------------------------------------------------------------------------*/
384
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200385static irqreturn_t ohci_hcd_at91_overcurrent_irq(int irq, void *data)
386{
387 struct platform_device *pdev = data;
Jingoo Hand4f09e22013-07-30 19:59:40 +0900388 struct at91_usbh_data *pdata = dev_get_platdata(&pdev->dev);
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200389 int val, gpio, port;
390
391 /* From the GPIO notifying the over-current situation, find
392 * out the corresponding port */
Nicolas Ferre0ee6d1e2012-03-21 16:53:09 +0100393 at91_for_each_port(port) {
Joachim Eastwood01bb6502012-09-23 22:56:00 +0200394 if (gpio_is_valid(pdata->overcurrent_pin[port]) &&
395 gpio_to_irq(pdata->overcurrent_pin[port]) == irq) {
Nicolas Ferree4499072012-02-11 14:23:06 +0100396 gpio = pdata->overcurrent_pin[port];
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200397 break;
Nicolas Ferree4499072012-02-11 14:23:06 +0100398 }
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200399 }
400
Nicolas Ferre0ee6d1e2012-03-21 16:53:09 +0100401 if (port == AT91_MAX_USBH_PORTS) {
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200402 dev_err(& pdev->dev, "overcurrent interrupt from unknown GPIO\n");
403 return IRQ_HANDLED;
404 }
405
406 val = gpio_get_value(gpio);
407
408 /* When notified of an over-current situation, disable power
409 on the corresponding port, and mark this port in
410 over-current. */
Nicolas Ferre0ee6d1e2012-03-21 16:53:09 +0100411 if (!val) {
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200412 ohci_at91_usb_set_power(pdata, port, 0);
413 pdata->overcurrent_status[port] = 1;
414 pdata->overcurrent_changed[port] = 1;
415 }
416
417 dev_dbg(& pdev->dev, "overcurrent situation %s\n",
418 val ? "exited" : "notified");
419
420 return IRQ_HANDLED;
421}
422
Jean-Christophe PLAGNIOL-VILLARD24197302011-11-21 06:55:18 +0800423#ifdef CONFIG_OF
424static const struct of_device_id at91_ohci_dt_ids[] = {
425 { .compatible = "atmel,at91rm9200-ohci" },
426 { /* sentinel */ }
427};
428
429MODULE_DEVICE_TABLE(of, at91_ohci_dt_ids);
430
Bill Pemberton41ac7b32012-11-19 13:21:48 -0500431static int ohci_at91_of_init(struct platform_device *pdev)
Jean-Christophe PLAGNIOL-VILLARD24197302011-11-21 06:55:18 +0800432{
433 struct device_node *np = pdev->dev.of_node;
Russell King22d9d8e2013-06-10 16:28:49 +0100434 int i, gpio, ret;
Jean-Christophe PLAGNIOL-VILLARD24197302011-11-21 06:55:18 +0800435 enum of_gpio_flags flags;
436 struct at91_usbh_data *pdata;
437 u32 ports;
438
439 if (!np)
440 return 0;
441
442 /* Right now device-tree probed devices don't get dma_mask set.
443 * Since shared usb code relies on it, set it here for now.
444 * Once we have dma capability bindings this can go away.
445 */
Russell Kinge1fd7342013-06-27 12:36:37 +0100446 ret = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
Russell King22d9d8e2013-06-10 16:28:49 +0100447 if (ret)
448 return ret;
Jean-Christophe PLAGNIOL-VILLARD24197302011-11-21 06:55:18 +0800449
450 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
451 if (!pdata)
452 return -ENOMEM;
453
454 if (!of_property_read_u32(np, "num-ports", &ports))
455 pdata->ports = ports;
456
Nicolas Ferre0ee6d1e2012-03-21 16:53:09 +0100457 at91_for_each_port(i) {
Jean-Christophe PLAGNIOL-VILLARD24197302011-11-21 06:55:18 +0800458 gpio = of_get_named_gpio_flags(np, "atmel,vbus-gpio", i, &flags);
459 pdata->vbus_pin[i] = gpio;
460 if (!gpio_is_valid(gpio))
461 continue;
462 pdata->vbus_pin_active_low[i] = flags & OF_GPIO_ACTIVE_LOW;
Jean-Christophe PLAGNIOL-VILLARD24197302011-11-21 06:55:18 +0800463 }
464
Nicolas Ferre0ee6d1e2012-03-21 16:53:09 +0100465 at91_for_each_port(i)
Nicolas Ferreaaf9f5f2012-03-21 16:58:11 +0100466 pdata->overcurrent_pin[i] =
467 of_get_named_gpio_flags(np, "atmel,oc-gpio", i, &flags);
Jean-Christophe PLAGNIOL-VILLARD24197302011-11-21 06:55:18 +0800468
469 pdev->dev.platform_data = pdata;
470
471 return 0;
472}
473#else
Bill Pemberton41ac7b32012-11-19 13:21:48 -0500474static int ohci_at91_of_init(struct platform_device *pdev)
Jean-Christophe PLAGNIOL-VILLARD24197302011-11-21 06:55:18 +0800475{
476 return 0;
477}
478#endif
479
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200480/*-------------------------------------------------------------------------*/
481
Bill Pemberton41ac7b32012-11-19 13:21:48 -0500482static int ohci_hcd_at91_drv_probe(struct platform_device *pdev)
Andrew Victor39a269c2006-01-22 10:32:13 -0800483{
Jean-Christophe PLAGNIOL-VILLARD24197302011-11-21 06:55:18 +0800484 struct at91_usbh_data *pdata;
David Brownell4bde4a42007-12-27 11:19:49 -0800485 int i;
Nicolas Ferreaaf9f5f2012-03-21 16:58:11 +0100486 int gpio;
487 int ret;
David Brownell4bde4a42007-12-27 11:19:49 -0800488
Nicolas Ferre1887ab2b2012-03-28 11:49:01 +0200489 ret = ohci_at91_of_init(pdev);
490 if (ret)
491 return ret;
Jean-Christophe PLAGNIOL-VILLARD24197302011-11-21 06:55:18 +0800492
Jingoo Hand4f09e22013-07-30 19:59:40 +0900493 pdata = dev_get_platdata(&pdev->dev);
Jean-Christophe PLAGNIOL-VILLARD24197302011-11-21 06:55:18 +0800494
David Brownell4bde4a42007-12-27 11:19:49 -0800495 if (pdata) {
Nicolas Ferre0ee6d1e2012-03-21 16:53:09 +0100496 at91_for_each_port(i) {
Nicolas Ferre6fffb772012-08-29 11:49:18 +0200497 /*
498 * do not configure PIO if not in relation with
499 * real USB port on board
500 */
501 if (i >= pdata->ports) {
502 pdata->vbus_pin[i] = -EINVAL;
503 pdata->overcurrent_pin[i] = -EINVAL;
504 break;
505 }
506
Jean-Christophe PLAGNIOL-VILLARD8a7a49d2011-09-19 15:32:23 +0800507 if (!gpio_is_valid(pdata->vbus_pin[i]))
David Brownell4bde4a42007-12-27 11:19:49 -0800508 continue;
Nicolas Ferreaaf9f5f2012-03-21 16:58:11 +0100509 gpio = pdata->vbus_pin[i];
510
511 ret = gpio_request(gpio, "ohci_vbus");
512 if (ret) {
513 dev_err(&pdev->dev,
514 "can't request vbus gpio %d\n", gpio);
515 continue;
516 }
517 ret = gpio_direction_output(gpio,
518 !pdata->vbus_pin_active_low[i]);
519 if (ret) {
520 dev_err(&pdev->dev,
521 "can't put vbus gpio %d as output %d\n",
522 gpio, !pdata->vbus_pin_active_low[i]);
523 gpio_free(gpio);
524 continue;
525 }
526
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200527 ohci_at91_usb_set_power(pdata, i, 1);
528 }
529
Nicolas Ferre0ee6d1e2012-03-21 16:53:09 +0100530 at91_for_each_port(i) {
Jean-Christophe PLAGNIOL-VILLARD8a7a49d2011-09-19 15:32:23 +0800531 if (!gpio_is_valid(pdata->overcurrent_pin[i]))
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200532 continue;
Nicolas Ferreaaf9f5f2012-03-21 16:58:11 +0100533 gpio = pdata->overcurrent_pin[i];
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200534
Nicolas Ferreaaf9f5f2012-03-21 16:58:11 +0100535 ret = gpio_request(gpio, "ohci_overcurrent");
536 if (ret) {
537 dev_err(&pdev->dev,
538 "can't request overcurrent gpio %d\n",
539 gpio);
540 continue;
541 }
542
543 ret = gpio_direction_input(gpio);
544 if (ret) {
545 dev_err(&pdev->dev,
546 "can't configure overcurrent gpio %d as input\n",
547 gpio);
548 gpio_free(gpio);
549 continue;
550 }
551
552 ret = request_irq(gpio_to_irq(gpio),
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200553 ohci_hcd_at91_overcurrent_irq,
554 IRQF_SHARED, "ohci_overcurrent", pdev);
555 if (ret) {
Nicolas Ferreaaf9f5f2012-03-21 16:58:11 +0100556 gpio_free(gpio);
557 dev_err(&pdev->dev,
558 "can't get gpio IRQ for overcurrent\n");
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200559 }
David Brownell4bde4a42007-12-27 11:19:49 -0800560 }
561 }
562
David Brownell0365ee02006-06-19 14:27:20 -0700563 device_init_wakeup(&pdev->dev, 1);
564 return usb_hcd_at91_probe(&ohci_at91_hc_driver, pdev);
Andrew Victor39a269c2006-01-22 10:32:13 -0800565}
566
Bill Pembertonfb4e98a2012-11-19 13:26:20 -0500567static int ohci_hcd_at91_drv_remove(struct platform_device *pdev)
Andrew Victor39a269c2006-01-22 10:32:13 -0800568{
Jingoo Hand4f09e22013-07-30 19:59:40 +0900569 struct at91_usbh_data *pdata = dev_get_platdata(&pdev->dev);
David Brownell4bde4a42007-12-27 11:19:49 -0800570 int i;
571
572 if (pdata) {
Nicolas Ferre0ee6d1e2012-03-21 16:53:09 +0100573 at91_for_each_port(i) {
Jean-Christophe PLAGNIOL-VILLARD8a7a49d2011-09-19 15:32:23 +0800574 if (!gpio_is_valid(pdata->vbus_pin[i]))
David Brownell4bde4a42007-12-27 11:19:49 -0800575 continue;
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200576 ohci_at91_usb_set_power(pdata, i, 0);
David Brownell4bde4a42007-12-27 11:19:49 -0800577 gpio_free(pdata->vbus_pin[i]);
578 }
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200579
Nicolas Ferre0ee6d1e2012-03-21 16:53:09 +0100580 at91_for_each_port(i) {
Jean-Christophe PLAGNIOL-VILLARD8a7a49d2011-09-19 15:32:23 +0800581 if (!gpio_is_valid(pdata->overcurrent_pin[i]))
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200582 continue;
583 free_irq(gpio_to_irq(pdata->overcurrent_pin[i]), pdev);
584 gpio_free(pdata->overcurrent_pin[i]);
585 }
David Brownell4bde4a42007-12-27 11:19:49 -0800586 }
587
David Brownell0365ee02006-06-19 14:27:20 -0700588 device_init_wakeup(&pdev->dev, 0);
Pete Zaitcev421b4bf2008-06-01 14:38:43 -0700589 usb_hcd_at91_remove(platform_get_drvdata(pdev), pdev);
590 return 0;
Andrew Victor39a269c2006-01-22 10:32:13 -0800591}
592
593#ifdef CONFIG_PM
Andrew Victor39a269c2006-01-22 10:32:13 -0800594
David Brownell68ba61b2006-04-02 20:26:21 -0800595static int
David Brownell0365ee02006-06-19 14:27:20 -0700596ohci_hcd_at91_drv_suspend(struct platform_device *pdev, pm_message_t mesg)
David Brownell68ba61b2006-04-02 20:26:21 -0800597{
David Brownell0365ee02006-06-19 14:27:20 -0700598 struct usb_hcd *hcd = platform_get_drvdata(pdev);
599 struct ohci_hcd *ohci = hcd_to_ohci(hcd);
Majunath Goudara9d38402013-11-13 17:40:17 +0530600 bool do_wakeup = device_may_wakeup(&pdev->dev);
601 int ret;
David Brownell0365ee02006-06-19 14:27:20 -0700602
Majunath Goudara9d38402013-11-13 17:40:17 +0530603 if (do_wakeup)
David Brownell0365ee02006-06-19 14:27:20 -0700604 enable_irq_wake(hcd->irq);
David Brownell0365ee02006-06-19 14:27:20 -0700605
Majunath Goudara9d38402013-11-13 17:40:17 +0530606 ret = ohci_suspend(hcd, do_wakeup);
607 if (ret) {
608 disable_irq_wake(hcd->irq);
609 return ret;
610 }
David Brownell0365ee02006-06-19 14:27:20 -0700611 /*
612 * The integrated transceivers seem unable to notice disconnect,
613 * reconnect, or wakeup without the 48 MHz clock active. so for
614 * correctness, always discard connection state (using reset).
615 *
616 * REVISIT: some boards will be able to turn VBUS off...
617 */
618 if (at91_suspend_entering_slow_clock()) {
Manjunath Goudare3825b42013-09-21 16:38:42 +0530619 ohci->hc_control = ohci_readl(ohci, &ohci->regs->control);
620 ohci->hc_control &= OHCI_CTRL_RWC;
621 ohci_writel(ohci, ohci->hc_control, &ohci->regs->control);
622 ohci->rh_state = OHCI_RH_HALTED;
623
Patrice Vilchez869aa982010-04-28 13:45:40 +0200624 /* flush the writes */
625 (void) ohci_readl (ohci, &ohci->regs->control);
Andrew Victored077bb2007-02-16 10:18:58 -0800626 at91_stop_clock();
David Brownell0365ee02006-06-19 14:27:20 -0700627 }
Andrew Victor39a269c2006-01-22 10:32:13 -0800628
Majunath Goudara9d38402013-11-13 17:40:17 +0530629 return ret;
Andrew Victor39a269c2006-01-22 10:32:13 -0800630}
631
David Brownell0365ee02006-06-19 14:27:20 -0700632static int ohci_hcd_at91_drv_resume(struct platform_device *pdev)
Andrew Victor39a269c2006-01-22 10:32:13 -0800633{
Marc Pignat6dde8962007-01-09 14:00:11 -0800634 struct usb_hcd *hcd = platform_get_drvdata(pdev);
635
636 if (device_may_wakeup(&pdev->dev))
637 disable_irq_wake(hcd->irq);
638
Andrew Victored077bb2007-02-16 10:18:58 -0800639 if (!clocked)
640 at91_start_clock();
Andrew Victor39a269c2006-01-22 10:32:13 -0800641
Florian Fainellicfa49b42012-10-08 15:11:29 +0200642 ohci_resume(hcd, false);
Andrew Victor39a269c2006-01-22 10:32:13 -0800643 return 0;
644}
645#else
646#define ohci_hcd_at91_drv_suspend NULL
647#define ohci_hcd_at91_drv_resume NULL
648#endif
649
Andrew Victor39a269c2006-01-22 10:32:13 -0800650static struct platform_driver ohci_hcd_at91_driver = {
651 .probe = ohci_hcd_at91_drv_probe,
Bill Pemberton76904172012-11-19 13:21:08 -0500652 .remove = ohci_hcd_at91_drv_remove,
Aleksey Gorelov64a21d02006-08-08 17:24:08 -0700653 .shutdown = usb_hcd_platform_shutdown,
Andrew Victor39a269c2006-01-22 10:32:13 -0800654 .suspend = ohci_hcd_at91_drv_suspend,
655 .resume = ohci_hcd_at91_drv_resume,
656 .driver = {
David Brownell0365ee02006-06-19 14:27:20 -0700657 .name = "at91_ohci",
Andrew Victor39a269c2006-01-22 10:32:13 -0800658 .owner = THIS_MODULE,
Jean-Christophe PLAGNIOL-VILLARD24197302011-11-21 06:55:18 +0800659 .of_match_table = of_match_ptr(at91_ohci_dt_ids),
Andrew Victor39a269c2006-01-22 10:32:13 -0800660 },
661};
Manjunath Goudare3825b42013-09-21 16:38:42 +0530662
663static int __init ohci_at91_init(void)
664{
665 if (usb_disabled())
666 return -ENODEV;
667
668 pr_info("%s: " DRIVER_DESC "\n", hcd_name);
669 ohci_init_driver(&ohci_at91_hc_driver, NULL);
670
671 /*
672 * The Atmel HW has some unusual quirks, which require Atmel-specific
673 * workarounds. We override certain hc_driver functions here to
674 * achieve that. We explicitly do not enhance ohci_driver_overrides to
675 * allow this more easily, since this is an unusual case, and we don't
676 * want to encourage others to override these functions by making it
677 * too easy.
678 */
679
Manjunath Goudare3825b42013-09-21 16:38:42 +0530680 ohci_at91_hc_driver.hub_status_data = ohci_at91_hub_status_data;
681 ohci_at91_hc_driver.hub_control = ohci_at91_hub_control;
682
683 return platform_driver_register(&ohci_hcd_at91_driver);
684}
685module_init(ohci_at91_init);
686
687static void __exit ohci_at91_cleanup(void)
688{
689 platform_driver_unregister(&ohci_hcd_at91_driver);
690}
691module_exit(ohci_at91_cleanup);
692
693MODULE_DESCRIPTION(DRIVER_DESC);
694MODULE_LICENSE("GPL");
695MODULE_ALIAS("platform:at91_ohci");