blob: 36423db63073bc9bce3d6fa28dbf624006f9b6e9 [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>
16#include <linux/platform_device.h>
Jean-Christophe PLAGNIOL-VILLARD24197302011-11-21 06:55:18 +080017#include <linux/of_platform.h>
18#include <linux/of_gpio.h>
Jean-Christophe PLAGNIOL-VILLARDbcd23602012-10-30 05:12:23 +080019#include <linux/platform_data/atmel.h>
Andrew Victor39a269c2006-01-22 10:32:13 -080020
Russell Kinga09e64f2008-08-05 16:14:15 +010021#include <mach/hardware.h>
David Brownell4bde4a42007-12-27 11:19:49 -080022#include <asm/gpio.h>
23
Russell Kinga09e64f2008-08-05 16:14:15 +010024#include <mach/cpu.h>
Andrew Victor39a269c2006-01-22 10:32:13 -080025
David Brownell0365ee02006-06-19 14:27:20 -070026#ifndef CONFIG_ARCH_AT91
27#error "CONFIG_ARCH_AT91 must be defined."
Andrew Victor39a269c2006-01-22 10:32:13 -080028#endif
29
Nicolas Ferre0ee6d1e2012-03-21 16:53:09 +010030#define valid_port(index) ((index) >= 0 && (index) < AT91_MAX_USBH_PORTS)
31#define at91_for_each_port(index) \
32 for ((index) = 0; (index) < AT91_MAX_USBH_PORTS; (index)++)
33
Boris BREZILLON6b0a1cf2013-08-01 19:09:13 +020034/* interface, function and usb clocks; sometimes also an AHB clock */
35static struct clk *iclk, *fclk, *uclk, *hclk;
David Brownell0365ee02006-06-19 14:27:20 -070036static int clocked;
Andrew Victor39a269c2006-01-22 10:32:13 -080037
38extern int usb_disabled(void);
39
40/*-------------------------------------------------------------------------*/
41
Andrew Victored077bb2007-02-16 10:18:58 -080042static void at91_start_clock(void)
43{
Boris BREZILLON6b0a1cf2013-08-01 19:09:13 +020044 if (IS_ENABLED(CONFIG_COMMON_CLK)) {
45 clk_set_rate(uclk, 48000000);
46 clk_prepare_enable(uclk);
47 }
Boris BREZILLON8e82d8d2013-06-19 13:21:08 +020048 clk_prepare_enable(hclk);
49 clk_prepare_enable(iclk);
50 clk_prepare_enable(fclk);
Andrew Victored077bb2007-02-16 10:18:58 -080051 clocked = 1;
52}
53
54static void at91_stop_clock(void)
55{
Boris BREZILLON8e82d8d2013-06-19 13:21:08 +020056 clk_disable_unprepare(fclk);
57 clk_disable_unprepare(iclk);
58 clk_disable_unprepare(hclk);
Boris BREZILLON6b0a1cf2013-08-01 19:09:13 +020059 if (IS_ENABLED(CONFIG_COMMON_CLK))
60 clk_disable_unprepare(uclk);
Andrew Victored077bb2007-02-16 10:18:58 -080061 clocked = 0;
62}
63
Andrew Victor39a269c2006-01-22 10:32:13 -080064static void at91_start_hc(struct platform_device *pdev)
65{
66 struct usb_hcd *hcd = platform_get_drvdata(pdev);
67 struct ohci_regs __iomem *regs = hcd->regs;
68
David Brownell0365ee02006-06-19 14:27:20 -070069 dev_dbg(&pdev->dev, "start\n");
Andrew Victor39a269c2006-01-22 10:32:13 -080070
71 /*
72 * Start the USB clocks.
73 */
Andrew Victored077bb2007-02-16 10:18:58 -080074 at91_start_clock();
Andrew Victor39a269c2006-01-22 10:32:13 -080075
76 /*
77 * The USB host controller must remain in reset.
78 */
79 writel(0, &regs->control);
80}
81
82static void at91_stop_hc(struct platform_device *pdev)
83{
84 struct usb_hcd *hcd = platform_get_drvdata(pdev);
85 struct ohci_regs __iomem *regs = hcd->regs;
86
David Brownell0365ee02006-06-19 14:27:20 -070087 dev_dbg(&pdev->dev, "stop\n");
Andrew Victor39a269c2006-01-22 10:32:13 -080088
89 /*
90 * Put the USB host controller into reset.
91 */
92 writel(0, &regs->control);
93
94 /*
95 * Stop the USB clocks.
96 */
Andrew Victored077bb2007-02-16 10:18:58 -080097 at91_stop_clock();
Andrew Victor39a269c2006-01-22 10:32:13 -080098}
99
100
101/*-------------------------------------------------------------------------*/
102
Bill Pembertonfb4e98a2012-11-19 13:26:20 -0500103static void usb_hcd_at91_remove (struct usb_hcd *, struct platform_device *);
Andrew Victor39a269c2006-01-22 10:32:13 -0800104
105/* configure so an HC device and id are always provided */
106/* always called with process context; sleeping is OK */
107
108
109/**
David Brownell0365ee02006-06-19 14:27:20 -0700110 * usb_hcd_at91_probe - initialize AT91-based HCDs
Andrew Victor39a269c2006-01-22 10:32:13 -0800111 * Context: !in_interrupt()
112 *
113 * Allocates basic resources for this USB host controller, and
114 * then invokes the start() method for the HCD associated with it
115 * through the hotplug entry's driver_data.
Andrew Victor39a269c2006-01-22 10:32:13 -0800116 */
Bill Pemberton41ac7b32012-11-19 13:21:48 -0500117static int usb_hcd_at91_probe(const struct hc_driver *driver,
David Brownell0365ee02006-06-19 14:27:20 -0700118 struct platform_device *pdev)
Andrew Victor39a269c2006-01-22 10:32:13 -0800119{
120 int retval;
121 struct usb_hcd *hcd = NULL;
122
123 if (pdev->num_resources != 2) {
124 pr_debug("hcd probe: invalid num_resources");
125 return -ENODEV;
126 }
127
David Brownell0365ee02006-06-19 14:27:20 -0700128 if ((pdev->resource[0].flags != IORESOURCE_MEM)
129 || (pdev->resource[1].flags != IORESOURCE_IRQ)) {
Andrew Victor39a269c2006-01-22 10:32:13 -0800130 pr_debug("hcd probe: invalid resource type\n");
131 return -ENODEV;
132 }
133
David Brownell0365ee02006-06-19 14:27:20 -0700134 hcd = usb_create_hcd(driver, &pdev->dev, "at91");
Andrew Victor39a269c2006-01-22 10:32:13 -0800135 if (!hcd)
136 return -ENOMEM;
137 hcd->rsrc_start = pdev->resource[0].start;
Nicolas Ferre7a82f612012-05-09 10:49:41 +0200138 hcd->rsrc_len = resource_size(&pdev->resource[0]);
Andrew Victor39a269c2006-01-22 10:32:13 -0800139
140 if (!request_mem_region(hcd->rsrc_start, hcd->rsrc_len, hcd_name)) {
141 pr_debug("request_mem_region failed\n");
142 retval = -EBUSY;
143 goto err1;
144 }
145
146 hcd->regs = ioremap(hcd->rsrc_start, hcd->rsrc_len);
147 if (!hcd->regs) {
148 pr_debug("ioremap failed\n");
149 retval = -EIO;
150 goto err2;
151 }
152
153 iclk = clk_get(&pdev->dev, "ohci_clk");
Jean-Christophe PLAGNIOL-VILLARD68134632011-12-08 16:32:00 +0100154 if (IS_ERR(iclk)) {
155 dev_err(&pdev->dev, "failed to get ohci_clk\n");
156 retval = PTR_ERR(iclk);
157 goto err3;
158 }
Andrew Victor39a269c2006-01-22 10:32:13 -0800159 fclk = clk_get(&pdev->dev, "uhpck");
Jean-Christophe PLAGNIOL-VILLARD68134632011-12-08 16:32:00 +0100160 if (IS_ERR(fclk)) {
161 dev_err(&pdev->dev, "failed to get uhpck\n");
162 retval = PTR_ERR(fclk);
163 goto err4;
164 }
Jean-Christophe PLAGNIOL-VILLARD0af43162011-08-30 03:29:28 +0200165 hclk = clk_get(&pdev->dev, "hclk");
Jean-Christophe PLAGNIOL-VILLARD68134632011-12-08 16:32:00 +0100166 if (IS_ERR(hclk)) {
167 dev_err(&pdev->dev, "failed to get hclk\n");
168 retval = PTR_ERR(hclk);
169 goto err5;
170 }
Boris BREZILLON6b0a1cf2013-08-01 19:09:13 +0200171 if (IS_ENABLED(CONFIG_COMMON_CLK)) {
172 uclk = clk_get(&pdev->dev, "usb_clk");
173 if (IS_ERR(uclk)) {
174 dev_err(&pdev->dev, "failed to get uclk\n");
175 retval = PTR_ERR(uclk);
176 goto err6;
177 }
178 }
Andrew Victor39a269c2006-01-22 10:32:13 -0800179
180 at91_start_hc(pdev);
181 ohci_hcd_init(hcd_to_ohci(hcd));
182
Nicolas Ferre2f2cac32009-07-27 14:59:24 -0700183 retval = usb_add_hcd(hcd, pdev->resource[1].start, IRQF_SHARED);
Andrew Victor39a269c2006-01-22 10:32:13 -0800184 if (retval == 0)
185 return retval;
186
187 /* Error handling */
188 at91_stop_hc(pdev);
189
Boris BREZILLON6b0a1cf2013-08-01 19:09:13 +0200190 if (IS_ENABLED(CONFIG_COMMON_CLK))
191 clk_put(uclk);
192 err6:
Jean-Christophe PLAGNIOL-VILLARD0af43162011-08-30 03:29:28 +0200193 clk_put(hclk);
Jean-Christophe PLAGNIOL-VILLARD68134632011-12-08 16:32:00 +0100194 err5:
Andrew Victor39a269c2006-01-22 10:32:13 -0800195 clk_put(fclk);
Jean-Christophe PLAGNIOL-VILLARD68134632011-12-08 16:32:00 +0100196 err4:
Andrew Victor39a269c2006-01-22 10:32:13 -0800197 clk_put(iclk);
198
Jean-Christophe PLAGNIOL-VILLARD68134632011-12-08 16:32:00 +0100199 err3:
Andrew Victor39a269c2006-01-22 10:32:13 -0800200 iounmap(hcd->regs);
201
202 err2:
203 release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
204
205 err1:
206 usb_put_hcd(hcd);
207 return retval;
208}
209
210
Andrew Victor39a269c2006-01-22 10:32:13 -0800211/* may be called with controller, bus, and devices active */
212
213/**
David Brownell0365ee02006-06-19 14:27:20 -0700214 * usb_hcd_at91_remove - shutdown processing for AT91-based HCDs
Andrew Victor39a269c2006-01-22 10:32:13 -0800215 * @dev: USB Host Controller being removed
216 * Context: !in_interrupt()
217 *
218 * Reverses the effect of usb_hcd_at91_probe(), first invoking
219 * the HCD's stop() method. It is always called from a thread
David Brownell0365ee02006-06-19 14:27:20 -0700220 * context, "rmmod" or something similar.
Andrew Victor39a269c2006-01-22 10:32:13 -0800221 *
222 */
Bill Pembertonfb4e98a2012-11-19 13:26:20 -0500223static void usb_hcd_at91_remove(struct usb_hcd *hcd,
David Brownell0365ee02006-06-19 14:27:20 -0700224 struct platform_device *pdev)
Andrew Victor39a269c2006-01-22 10:32:13 -0800225{
226 usb_remove_hcd(hcd);
227 at91_stop_hc(pdev);
228 iounmap(hcd->regs);
David Brownell68ba61b2006-04-02 20:26:21 -0800229 release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
Pete Zaitcev421b4bf2008-06-01 14:38:43 -0700230 usb_put_hcd(hcd);
Andrew Victor39a269c2006-01-22 10:32:13 -0800231
Boris BREZILLON6b0a1cf2013-08-01 19:09:13 +0200232 if (IS_ENABLED(CONFIG_COMMON_CLK))
233 clk_put(uclk);
Jean-Christophe PLAGNIOL-VILLARD0af43162011-08-30 03:29:28 +0200234 clk_put(hclk);
David Brownell68ba61b2006-04-02 20:26:21 -0800235 clk_put(fclk);
236 clk_put(iclk);
Andrew Victored077bb2007-02-16 10:18:58 -0800237 fclk = iclk = hclk = NULL;
Andrew Victor39a269c2006-01-22 10:32:13 -0800238}
239
240/*-------------------------------------------------------------------------*/
241
Bill Pemberton41ac7b32012-11-19 13:21:48 -0500242static int
Nicolas Ferre07e4e552012-05-09 10:48:54 +0200243ohci_at91_reset (struct usb_hcd *hcd)
Andrew Victor39a269c2006-01-22 10:32:13 -0800244{
Jingoo Hand4f09e22013-07-30 19:59:40 +0900245 struct at91_usbh_data *board = dev_get_platdata(hcd->self.controller);
Andrew Victor39a269c2006-01-22 10:32:13 -0800246 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
247 int ret;
248
249 if ((ret = ohci_init(ohci)) < 0)
250 return ret;
251
David Brownellcd22afd2006-09-03 12:21:50 -0700252 ohci->num_ports = board->ports;
Nicolas Ferre07e4e552012-05-09 10:48:54 +0200253 return 0;
254}
255
Bill Pemberton41ac7b32012-11-19 13:21:48 -0500256static int
Nicolas Ferre07e4e552012-05-09 10:48:54 +0200257ohci_at91_start (struct usb_hcd *hcd)
258{
259 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
260 int ret;
David Brownell0365ee02006-06-19 14:27:20 -0700261
Andrew Victor39a269c2006-01-22 10:32:13 -0800262 if ((ret = ohci_run(ohci)) < 0) {
Greg Kroah-Hartman2418d5f2012-04-27 11:24:39 -0700263 dev_err(hcd->self.controller, "can't start %s\n",
264 hcd->self.bus_name);
Andrew Victor39a269c2006-01-22 10:32:13 -0800265 ohci_stop(hcd);
266 return ret;
267 }
Andrew Victor39a269c2006-01-22 10:32:13 -0800268 return 0;
269}
270
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200271static void ohci_at91_usb_set_power(struct at91_usbh_data *pdata, int port, int enable)
272{
Nicolas Ferre0ee6d1e2012-03-21 16:53:09 +0100273 if (!valid_port(port))
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200274 return;
275
Jean-Christophe PLAGNIOL-VILLARD8a7a49d2011-09-19 15:32:23 +0800276 if (!gpio_is_valid(pdata->vbus_pin[port]))
Jean-Christophe PLAGNIOL-VILLARD770f0ba2011-11-12 16:46:13 +0100277 return;
278
Jean-Christophe PLAGNIOL-VILLARD8134ff52011-12-08 16:32:01 +0100279 gpio_set_value(pdata->vbus_pin[port],
Nicolas Ferre1e7caf82012-03-21 14:38:55 +0100280 pdata->vbus_pin_active_low[port] ^ enable);
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200281}
282
283static int ohci_at91_usb_get_power(struct at91_usbh_data *pdata, int port)
284{
Nicolas Ferre0ee6d1e2012-03-21 16:53:09 +0100285 if (!valid_port(port))
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200286 return -EINVAL;
287
Jean-Christophe PLAGNIOL-VILLARD8a7a49d2011-09-19 15:32:23 +0800288 if (!gpio_is_valid(pdata->vbus_pin[port]))
Jean-Christophe PLAGNIOL-VILLARD770f0ba2011-11-12 16:46:13 +0100289 return -EINVAL;
290
Jean-Christophe PLAGNIOL-VILLARD8134ff52011-12-08 16:32:01 +0100291 return gpio_get_value(pdata->vbus_pin[port]) ^
Nicolas Ferre1e7caf82012-03-21 14:38:55 +0100292 pdata->vbus_pin_active_low[port];
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200293}
294
295/*
296 * Update the status data from the hub with the over-current indicator change.
297 */
298static int ohci_at91_hub_status_data(struct usb_hcd *hcd, char *buf)
299{
Jingoo Hand4f09e22013-07-30 19:59:40 +0900300 struct at91_usbh_data *pdata = dev_get_platdata(hcd->self.controller);
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200301 int length = ohci_hub_status_data(hcd, buf);
302 int port;
303
Nicolas Ferre0ee6d1e2012-03-21 16:53:09 +0100304 at91_for_each_port(port) {
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200305 if (pdata->overcurrent_changed[port]) {
Nicolas Ferre0ee6d1e2012-03-21 16:53:09 +0100306 if (!length)
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200307 length = 1;
308 buf[0] |= 1 << (port + 1);
309 }
310 }
311
312 return length;
313}
314
315/*
316 * Look at the control requests to the root hub and see if we need to override.
317 */
318static int ohci_at91_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
319 u16 wIndex, char *buf, u16 wLength)
320{
Jingoo Hand4f09e22013-07-30 19:59:40 +0900321 struct at91_usbh_data *pdata = dev_get_platdata(hcd->self.controller);
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200322 struct usb_hub_descriptor *desc;
323 int ret = -EINVAL;
324 u32 *data = (u32 *)buf;
325
326 dev_dbg(hcd->self.controller,
327 "ohci_at91_hub_control(%p,0x%04x,0x%04x,0x%04x,%p,%04x)\n",
328 hcd, typeReq, wValue, wIndex, buf, wLength);
329
Nicolas Ferre0ee6d1e2012-03-21 16:53:09 +0100330 wIndex--;
331
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200332 switch (typeReq) {
333 case SetPortFeature:
334 if (wValue == USB_PORT_FEAT_POWER) {
335 dev_dbg(hcd->self.controller, "SetPortFeat: POWER\n");
Nicolas Ferre0ee6d1e2012-03-21 16:53:09 +0100336 if (valid_port(wIndex)) {
337 ohci_at91_usb_set_power(pdata, wIndex, 1);
338 ret = 0;
339 }
340
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200341 goto out;
342 }
343 break;
344
345 case ClearPortFeature:
346 switch (wValue) {
347 case USB_PORT_FEAT_C_OVER_CURRENT:
348 dev_dbg(hcd->self.controller,
349 "ClearPortFeature: C_OVER_CURRENT\n");
350
Nicolas Ferre0ee6d1e2012-03-21 16:53:09 +0100351 if (valid_port(wIndex)) {
352 pdata->overcurrent_changed[wIndex] = 0;
353 pdata->overcurrent_status[wIndex] = 0;
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200354 }
355
356 goto out;
357
358 case USB_PORT_FEAT_OVER_CURRENT:
359 dev_dbg(hcd->self.controller,
360 "ClearPortFeature: OVER_CURRENT\n");
361
Nicolas Ferre0ee6d1e2012-03-21 16:53:09 +0100362 if (valid_port(wIndex))
363 pdata->overcurrent_status[wIndex] = 0;
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200364
365 goto out;
366
367 case USB_PORT_FEAT_POWER:
368 dev_dbg(hcd->self.controller,
369 "ClearPortFeature: POWER\n");
370
Nicolas Ferre0ee6d1e2012-03-21 16:53:09 +0100371 if (valid_port(wIndex)) {
372 ohci_at91_usb_set_power(pdata, wIndex, 0);
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200373 return 0;
374 }
375 }
376 break;
377 }
378
Nicolas Ferre0ee6d1e2012-03-21 16:53:09 +0100379 ret = ohci_hub_control(hcd, typeReq, wValue, wIndex + 1, buf, wLength);
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200380 if (ret)
381 goto out;
382
383 switch (typeReq) {
384 case GetHubDescriptor:
385
386 /* update the hub's descriptor */
387
388 desc = (struct usb_hub_descriptor *)buf;
389
390 dev_dbg(hcd->self.controller, "wHubCharacteristics 0x%04x\n",
391 desc->wHubCharacteristics);
392
393 /* remove the old configurations for power-switching, and
394 * over-current protection, and insert our new configuration
395 */
396
397 desc->wHubCharacteristics &= ~cpu_to_le16(HUB_CHAR_LPSM);
398 desc->wHubCharacteristics |= cpu_to_le16(0x0001);
399
400 if (pdata->overcurrent_supported) {
401 desc->wHubCharacteristics &= ~cpu_to_le16(HUB_CHAR_OCPM);
402 desc->wHubCharacteristics |= cpu_to_le16(0x0008|0x0001);
403 }
404
405 dev_dbg(hcd->self.controller, "wHubCharacteristics after 0x%04x\n",
406 desc->wHubCharacteristics);
407
408 return ret;
409
410 case GetPortStatus:
411 /* check port status */
412
413 dev_dbg(hcd->self.controller, "GetPortStatus(%d)\n", wIndex);
414
Nicolas Ferre0ee6d1e2012-03-21 16:53:09 +0100415 if (valid_port(wIndex)) {
416 if (!ohci_at91_usb_get_power(pdata, wIndex))
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200417 *data &= ~cpu_to_le32(RH_PS_PPS);
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200418
Nicolas Ferre0ee6d1e2012-03-21 16:53:09 +0100419 if (pdata->overcurrent_changed[wIndex])
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200420 *data |= cpu_to_le32(RH_PS_OCIC);
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200421
Nicolas Ferre0ee6d1e2012-03-21 16:53:09 +0100422 if (pdata->overcurrent_status[wIndex])
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200423 *data |= cpu_to_le32(RH_PS_POCI);
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200424 }
425 }
426
427 out:
428 return ret;
429}
430
Andrew Victor39a269c2006-01-22 10:32:13 -0800431/*-------------------------------------------------------------------------*/
432
433static const struct hc_driver ohci_at91_hc_driver = {
434 .description = hcd_name,
David Brownell0365ee02006-06-19 14:27:20 -0700435 .product_desc = "AT91 OHCI",
Andrew Victor39a269c2006-01-22 10:32:13 -0800436 .hcd_priv_size = sizeof(struct ohci_hcd),
437
438 /*
439 * generic hardware linkage
440 */
441 .irq = ohci_irq,
442 .flags = HCD_USB11 | HCD_MEMORY,
443
444 /*
445 * basic lifecycle operations
446 */
Nicolas Ferre07e4e552012-05-09 10:48:54 +0200447 .reset = ohci_at91_reset,
Andrew Victor39a269c2006-01-22 10:32:13 -0800448 .start = ohci_at91_start,
449 .stop = ohci_stop,
David Brownelldd9048a2006-12-05 03:18:31 -0800450 .shutdown = ohci_shutdown,
Andrew Victor39a269c2006-01-22 10:32:13 -0800451
452 /*
453 * managing i/o requests and associated device resources
454 */
455 .urb_enqueue = ohci_urb_enqueue,
456 .urb_dequeue = ohci_urb_dequeue,
457 .endpoint_disable = ohci_endpoint_disable,
458
459 /*
460 * scheduling support
461 */
462 .get_frame_number = ohci_get_frame,
463
464 /*
465 * root hub support
466 */
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200467 .hub_status_data = ohci_at91_hub_status_data,
468 .hub_control = ohci_at91_hub_control,
Andrew Victor39a269c2006-01-22 10:32:13 -0800469#ifdef CONFIG_PM
David Brownell68ba61b2006-04-02 20:26:21 -0800470 .bus_suspend = ohci_bus_suspend,
471 .bus_resume = ohci_bus_resume,
Andrew Victor39a269c2006-01-22 10:32:13 -0800472#endif
473 .start_port_reset = ohci_start_port_reset,
474};
475
476/*-------------------------------------------------------------------------*/
477
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200478static irqreturn_t ohci_hcd_at91_overcurrent_irq(int irq, void *data)
479{
480 struct platform_device *pdev = data;
Jingoo Hand4f09e22013-07-30 19:59:40 +0900481 struct at91_usbh_data *pdata = dev_get_platdata(&pdev->dev);
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200482 int val, gpio, port;
483
484 /* From the GPIO notifying the over-current situation, find
485 * out the corresponding port */
Nicolas Ferre0ee6d1e2012-03-21 16:53:09 +0100486 at91_for_each_port(port) {
Joachim Eastwood01bb6502012-09-23 22:56:00 +0200487 if (gpio_is_valid(pdata->overcurrent_pin[port]) &&
488 gpio_to_irq(pdata->overcurrent_pin[port]) == irq) {
Nicolas Ferree4499072012-02-11 14:23:06 +0100489 gpio = pdata->overcurrent_pin[port];
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200490 break;
Nicolas Ferree4499072012-02-11 14:23:06 +0100491 }
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200492 }
493
Nicolas Ferre0ee6d1e2012-03-21 16:53:09 +0100494 if (port == AT91_MAX_USBH_PORTS) {
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200495 dev_err(& pdev->dev, "overcurrent interrupt from unknown GPIO\n");
496 return IRQ_HANDLED;
497 }
498
499 val = gpio_get_value(gpio);
500
501 /* When notified of an over-current situation, disable power
502 on the corresponding port, and mark this port in
503 over-current. */
Nicolas Ferre0ee6d1e2012-03-21 16:53:09 +0100504 if (!val) {
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200505 ohci_at91_usb_set_power(pdata, port, 0);
506 pdata->overcurrent_status[port] = 1;
507 pdata->overcurrent_changed[port] = 1;
508 }
509
510 dev_dbg(& pdev->dev, "overcurrent situation %s\n",
511 val ? "exited" : "notified");
512
513 return IRQ_HANDLED;
514}
515
Jean-Christophe PLAGNIOL-VILLARD24197302011-11-21 06:55:18 +0800516#ifdef CONFIG_OF
517static const struct of_device_id at91_ohci_dt_ids[] = {
518 { .compatible = "atmel,at91rm9200-ohci" },
519 { /* sentinel */ }
520};
521
522MODULE_DEVICE_TABLE(of, at91_ohci_dt_ids);
523
Bill Pemberton41ac7b32012-11-19 13:21:48 -0500524static int ohci_at91_of_init(struct platform_device *pdev)
Jean-Christophe PLAGNIOL-VILLARD24197302011-11-21 06:55:18 +0800525{
526 struct device_node *np = pdev->dev.of_node;
Russell King22d9d8e2013-06-10 16:28:49 +0100527 int i, gpio, ret;
Jean-Christophe PLAGNIOL-VILLARD24197302011-11-21 06:55:18 +0800528 enum of_gpio_flags flags;
529 struct at91_usbh_data *pdata;
530 u32 ports;
531
532 if (!np)
533 return 0;
534
535 /* Right now device-tree probed devices don't get dma_mask set.
536 * Since shared usb code relies on it, set it here for now.
537 * Once we have dma capability bindings this can go away.
538 */
Russell Kinge1fd7342013-06-27 12:36:37 +0100539 ret = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
Russell King22d9d8e2013-06-10 16:28:49 +0100540 if (ret)
541 return ret;
Jean-Christophe PLAGNIOL-VILLARD24197302011-11-21 06:55:18 +0800542
543 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
544 if (!pdata)
545 return -ENOMEM;
546
547 if (!of_property_read_u32(np, "num-ports", &ports))
548 pdata->ports = ports;
549
Nicolas Ferre0ee6d1e2012-03-21 16:53:09 +0100550 at91_for_each_port(i) {
Jean-Christophe PLAGNIOL-VILLARD24197302011-11-21 06:55:18 +0800551 gpio = of_get_named_gpio_flags(np, "atmel,vbus-gpio", i, &flags);
552 pdata->vbus_pin[i] = gpio;
553 if (!gpio_is_valid(gpio))
554 continue;
555 pdata->vbus_pin_active_low[i] = flags & OF_GPIO_ACTIVE_LOW;
Jean-Christophe PLAGNIOL-VILLARD24197302011-11-21 06:55:18 +0800556 }
557
Nicolas Ferre0ee6d1e2012-03-21 16:53:09 +0100558 at91_for_each_port(i)
Nicolas Ferreaaf9f5f2012-03-21 16:58:11 +0100559 pdata->overcurrent_pin[i] =
560 of_get_named_gpio_flags(np, "atmel,oc-gpio", i, &flags);
Jean-Christophe PLAGNIOL-VILLARD24197302011-11-21 06:55:18 +0800561
562 pdev->dev.platform_data = pdata;
563
564 return 0;
565}
566#else
Bill Pemberton41ac7b32012-11-19 13:21:48 -0500567static int ohci_at91_of_init(struct platform_device *pdev)
Jean-Christophe PLAGNIOL-VILLARD24197302011-11-21 06:55:18 +0800568{
569 return 0;
570}
571#endif
572
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200573/*-------------------------------------------------------------------------*/
574
Bill Pemberton41ac7b32012-11-19 13:21:48 -0500575static int ohci_hcd_at91_drv_probe(struct platform_device *pdev)
Andrew Victor39a269c2006-01-22 10:32:13 -0800576{
Jean-Christophe PLAGNIOL-VILLARD24197302011-11-21 06:55:18 +0800577 struct at91_usbh_data *pdata;
David Brownell4bde4a42007-12-27 11:19:49 -0800578 int i;
Nicolas Ferreaaf9f5f2012-03-21 16:58:11 +0100579 int gpio;
580 int ret;
David Brownell4bde4a42007-12-27 11:19:49 -0800581
Nicolas Ferre1887ab2b2012-03-28 11:49:01 +0200582 ret = ohci_at91_of_init(pdev);
583 if (ret)
584 return ret;
Jean-Christophe PLAGNIOL-VILLARD24197302011-11-21 06:55:18 +0800585
Jingoo Hand4f09e22013-07-30 19:59:40 +0900586 pdata = dev_get_platdata(&pdev->dev);
Jean-Christophe PLAGNIOL-VILLARD24197302011-11-21 06:55:18 +0800587
David Brownell4bde4a42007-12-27 11:19:49 -0800588 if (pdata) {
Nicolas Ferre0ee6d1e2012-03-21 16:53:09 +0100589 at91_for_each_port(i) {
Nicolas Ferre6fffb772012-08-29 11:49:18 +0200590 /*
591 * do not configure PIO if not in relation with
592 * real USB port on board
593 */
594 if (i >= pdata->ports) {
595 pdata->vbus_pin[i] = -EINVAL;
596 pdata->overcurrent_pin[i] = -EINVAL;
597 break;
598 }
599
Jean-Christophe PLAGNIOL-VILLARD8a7a49d2011-09-19 15:32:23 +0800600 if (!gpio_is_valid(pdata->vbus_pin[i]))
David Brownell4bde4a42007-12-27 11:19:49 -0800601 continue;
Nicolas Ferreaaf9f5f2012-03-21 16:58:11 +0100602 gpio = pdata->vbus_pin[i];
603
604 ret = gpio_request(gpio, "ohci_vbus");
605 if (ret) {
606 dev_err(&pdev->dev,
607 "can't request vbus gpio %d\n", gpio);
608 continue;
609 }
610 ret = gpio_direction_output(gpio,
611 !pdata->vbus_pin_active_low[i]);
612 if (ret) {
613 dev_err(&pdev->dev,
614 "can't put vbus gpio %d as output %d\n",
615 gpio, !pdata->vbus_pin_active_low[i]);
616 gpio_free(gpio);
617 continue;
618 }
619
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200620 ohci_at91_usb_set_power(pdata, i, 1);
621 }
622
Nicolas Ferre0ee6d1e2012-03-21 16:53:09 +0100623 at91_for_each_port(i) {
Jean-Christophe PLAGNIOL-VILLARD8a7a49d2011-09-19 15:32:23 +0800624 if (!gpio_is_valid(pdata->overcurrent_pin[i]))
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200625 continue;
Nicolas Ferreaaf9f5f2012-03-21 16:58:11 +0100626 gpio = pdata->overcurrent_pin[i];
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200627
Nicolas Ferreaaf9f5f2012-03-21 16:58:11 +0100628 ret = gpio_request(gpio, "ohci_overcurrent");
629 if (ret) {
630 dev_err(&pdev->dev,
631 "can't request overcurrent gpio %d\n",
632 gpio);
633 continue;
634 }
635
636 ret = gpio_direction_input(gpio);
637 if (ret) {
638 dev_err(&pdev->dev,
639 "can't configure overcurrent gpio %d as input\n",
640 gpio);
641 gpio_free(gpio);
642 continue;
643 }
644
645 ret = request_irq(gpio_to_irq(gpio),
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200646 ohci_hcd_at91_overcurrent_irq,
647 IRQF_SHARED, "ohci_overcurrent", pdev);
648 if (ret) {
Nicolas Ferreaaf9f5f2012-03-21 16:58:11 +0100649 gpio_free(gpio);
650 dev_err(&pdev->dev,
651 "can't get gpio IRQ for overcurrent\n");
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200652 }
David Brownell4bde4a42007-12-27 11:19:49 -0800653 }
654 }
655
David Brownell0365ee02006-06-19 14:27:20 -0700656 device_init_wakeup(&pdev->dev, 1);
657 return usb_hcd_at91_probe(&ohci_at91_hc_driver, pdev);
Andrew Victor39a269c2006-01-22 10:32:13 -0800658}
659
Bill Pembertonfb4e98a2012-11-19 13:26:20 -0500660static int ohci_hcd_at91_drv_remove(struct platform_device *pdev)
Andrew Victor39a269c2006-01-22 10:32:13 -0800661{
Jingoo Hand4f09e22013-07-30 19:59:40 +0900662 struct at91_usbh_data *pdata = dev_get_platdata(&pdev->dev);
David Brownell4bde4a42007-12-27 11:19:49 -0800663 int i;
664
665 if (pdata) {
Nicolas Ferre0ee6d1e2012-03-21 16:53:09 +0100666 at91_for_each_port(i) {
Jean-Christophe PLAGNIOL-VILLARD8a7a49d2011-09-19 15:32:23 +0800667 if (!gpio_is_valid(pdata->vbus_pin[i]))
David Brownell4bde4a42007-12-27 11:19:49 -0800668 continue;
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200669 ohci_at91_usb_set_power(pdata, i, 0);
David Brownell4bde4a42007-12-27 11:19:49 -0800670 gpio_free(pdata->vbus_pin[i]);
671 }
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200672
Nicolas Ferre0ee6d1e2012-03-21 16:53:09 +0100673 at91_for_each_port(i) {
Jean-Christophe PLAGNIOL-VILLARD8a7a49d2011-09-19 15:32:23 +0800674 if (!gpio_is_valid(pdata->overcurrent_pin[i]))
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200675 continue;
676 free_irq(gpio_to_irq(pdata->overcurrent_pin[i]), pdev);
677 gpio_free(pdata->overcurrent_pin[i]);
678 }
David Brownell4bde4a42007-12-27 11:19:49 -0800679 }
680
David Brownell0365ee02006-06-19 14:27:20 -0700681 device_init_wakeup(&pdev->dev, 0);
Pete Zaitcev421b4bf2008-06-01 14:38:43 -0700682 usb_hcd_at91_remove(platform_get_drvdata(pdev), pdev);
683 return 0;
Andrew Victor39a269c2006-01-22 10:32:13 -0800684}
685
686#ifdef CONFIG_PM
Andrew Victor39a269c2006-01-22 10:32:13 -0800687
David Brownell68ba61b2006-04-02 20:26:21 -0800688static int
David Brownell0365ee02006-06-19 14:27:20 -0700689ohci_hcd_at91_drv_suspend(struct platform_device *pdev, pm_message_t mesg)
David Brownell68ba61b2006-04-02 20:26:21 -0800690{
David Brownell0365ee02006-06-19 14:27:20 -0700691 struct usb_hcd *hcd = platform_get_drvdata(pdev);
692 struct ohci_hcd *ohci = hcd_to_ohci(hcd);
693
694 if (device_may_wakeup(&pdev->dev))
695 enable_irq_wake(hcd->irq);
David Brownell0365ee02006-06-19 14:27:20 -0700696
697 /*
698 * The integrated transceivers seem unable to notice disconnect,
699 * reconnect, or wakeup without the 48 MHz clock active. so for
700 * correctness, always discard connection state (using reset).
701 *
702 * REVISIT: some boards will be able to turn VBUS off...
703 */
704 if (at91_suspend_entering_slow_clock()) {
705 ohci_usb_reset (ohci);
Patrice Vilchez869aa982010-04-28 13:45:40 +0200706 /* flush the writes */
707 (void) ohci_readl (ohci, &ohci->regs->control);
Andrew Victored077bb2007-02-16 10:18:58 -0800708 at91_stop_clock();
David Brownell0365ee02006-06-19 14:27:20 -0700709 }
Andrew Victor39a269c2006-01-22 10:32:13 -0800710
711 return 0;
712}
713
David Brownell0365ee02006-06-19 14:27:20 -0700714static int ohci_hcd_at91_drv_resume(struct platform_device *pdev)
Andrew Victor39a269c2006-01-22 10:32:13 -0800715{
Marc Pignat6dde8962007-01-09 14:00:11 -0800716 struct usb_hcd *hcd = platform_get_drvdata(pdev);
717
718 if (device_may_wakeup(&pdev->dev))
719 disable_irq_wake(hcd->irq);
720
Andrew Victored077bb2007-02-16 10:18:58 -0800721 if (!clocked)
722 at91_start_clock();
Andrew Victor39a269c2006-01-22 10:32:13 -0800723
Florian Fainellicfa49b42012-10-08 15:11:29 +0200724 ohci_resume(hcd, false);
Andrew Victor39a269c2006-01-22 10:32:13 -0800725 return 0;
726}
727#else
728#define ohci_hcd_at91_drv_suspend NULL
729#define ohci_hcd_at91_drv_resume NULL
730#endif
731
Kay Sieversf4fce612008-04-10 21:29:22 -0700732MODULE_ALIAS("platform:at91_ohci");
David Brownell68ba61b2006-04-02 20:26:21 -0800733
Andrew Victor39a269c2006-01-22 10:32:13 -0800734static struct platform_driver ohci_hcd_at91_driver = {
735 .probe = ohci_hcd_at91_drv_probe,
Bill Pemberton76904172012-11-19 13:21:08 -0500736 .remove = ohci_hcd_at91_drv_remove,
Aleksey Gorelov64a21d02006-08-08 17:24:08 -0700737 .shutdown = usb_hcd_platform_shutdown,
Andrew Victor39a269c2006-01-22 10:32:13 -0800738 .suspend = ohci_hcd_at91_drv_suspend,
739 .resume = ohci_hcd_at91_drv_resume,
740 .driver = {
David Brownell0365ee02006-06-19 14:27:20 -0700741 .name = "at91_ohci",
Andrew Victor39a269c2006-01-22 10:32:13 -0800742 .owner = THIS_MODULE,
Jean-Christophe PLAGNIOL-VILLARD24197302011-11-21 06:55:18 +0800743 .of_match_table = of_match_ptr(at91_ohci_dt_ids),
Andrew Victor39a269c2006-01-22 10:32:13 -0800744 },
745};