blob: b3cdd1467521c0d40f7805d349b35e998b51a6cb [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 dev_set_drvdata(&pdev->dev, NULL);
Andrew Victor39a269c2006-01-22 10:32:13 -0800240}
241
242/*-------------------------------------------------------------------------*/
243
Bill Pemberton41ac7b32012-11-19 13:21:48 -0500244static int
Nicolas Ferre07e4e552012-05-09 10:48:54 +0200245ohci_at91_reset (struct usb_hcd *hcd)
Andrew Victor39a269c2006-01-22 10:32:13 -0800246{
Jingoo Hand4f09e22013-07-30 19:59:40 +0900247 struct at91_usbh_data *board = dev_get_platdata(hcd->self.controller);
Andrew Victor39a269c2006-01-22 10:32:13 -0800248 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
249 int ret;
250
251 if ((ret = ohci_init(ohci)) < 0)
252 return ret;
253
David Brownellcd22afd2006-09-03 12:21:50 -0700254 ohci->num_ports = board->ports;
Nicolas Ferre07e4e552012-05-09 10:48:54 +0200255 return 0;
256}
257
Bill Pemberton41ac7b32012-11-19 13:21:48 -0500258static int
Nicolas Ferre07e4e552012-05-09 10:48:54 +0200259ohci_at91_start (struct usb_hcd *hcd)
260{
261 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
262 int ret;
David Brownell0365ee02006-06-19 14:27:20 -0700263
Andrew Victor39a269c2006-01-22 10:32:13 -0800264 if ((ret = ohci_run(ohci)) < 0) {
Greg Kroah-Hartman2418d5f2012-04-27 11:24:39 -0700265 dev_err(hcd->self.controller, "can't start %s\n",
266 hcd->self.bus_name);
Andrew Victor39a269c2006-01-22 10:32:13 -0800267 ohci_stop(hcd);
268 return ret;
269 }
Andrew Victor39a269c2006-01-22 10:32:13 -0800270 return 0;
271}
272
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200273static void ohci_at91_usb_set_power(struct at91_usbh_data *pdata, int port, int enable)
274{
Nicolas Ferre0ee6d1e2012-03-21 16:53:09 +0100275 if (!valid_port(port))
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200276 return;
277
Jean-Christophe PLAGNIOL-VILLARD8a7a49d2011-09-19 15:32:23 +0800278 if (!gpio_is_valid(pdata->vbus_pin[port]))
Jean-Christophe PLAGNIOL-VILLARD770f0ba2011-11-12 16:46:13 +0100279 return;
280
Jean-Christophe PLAGNIOL-VILLARD8134ff52011-12-08 16:32:01 +0100281 gpio_set_value(pdata->vbus_pin[port],
Nicolas Ferre1e7caf82012-03-21 14:38:55 +0100282 pdata->vbus_pin_active_low[port] ^ enable);
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200283}
284
285static int ohci_at91_usb_get_power(struct at91_usbh_data *pdata, int port)
286{
Nicolas Ferre0ee6d1e2012-03-21 16:53:09 +0100287 if (!valid_port(port))
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200288 return -EINVAL;
289
Jean-Christophe PLAGNIOL-VILLARD8a7a49d2011-09-19 15:32:23 +0800290 if (!gpio_is_valid(pdata->vbus_pin[port]))
Jean-Christophe PLAGNIOL-VILLARD770f0ba2011-11-12 16:46:13 +0100291 return -EINVAL;
292
Jean-Christophe PLAGNIOL-VILLARD8134ff52011-12-08 16:32:01 +0100293 return gpio_get_value(pdata->vbus_pin[port]) ^
Nicolas Ferre1e7caf82012-03-21 14:38:55 +0100294 pdata->vbus_pin_active_low[port];
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200295}
296
297/*
298 * Update the status data from the hub with the over-current indicator change.
299 */
300static int ohci_at91_hub_status_data(struct usb_hcd *hcd, char *buf)
301{
Jingoo Hand4f09e22013-07-30 19:59:40 +0900302 struct at91_usbh_data *pdata = dev_get_platdata(hcd->self.controller);
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200303 int length = ohci_hub_status_data(hcd, buf);
304 int port;
305
Nicolas Ferre0ee6d1e2012-03-21 16:53:09 +0100306 at91_for_each_port(port) {
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200307 if (pdata->overcurrent_changed[port]) {
Nicolas Ferre0ee6d1e2012-03-21 16:53:09 +0100308 if (!length)
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200309 length = 1;
310 buf[0] |= 1 << (port + 1);
311 }
312 }
313
314 return length;
315}
316
317/*
318 * Look at the control requests to the root hub and see if we need to override.
319 */
320static int ohci_at91_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
321 u16 wIndex, char *buf, u16 wLength)
322{
Jingoo Hand4f09e22013-07-30 19:59:40 +0900323 struct at91_usbh_data *pdata = dev_get_platdata(hcd->self.controller);
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200324 struct usb_hub_descriptor *desc;
325 int ret = -EINVAL;
326 u32 *data = (u32 *)buf;
327
328 dev_dbg(hcd->self.controller,
329 "ohci_at91_hub_control(%p,0x%04x,0x%04x,0x%04x,%p,%04x)\n",
330 hcd, typeReq, wValue, wIndex, buf, wLength);
331
Nicolas Ferre0ee6d1e2012-03-21 16:53:09 +0100332 wIndex--;
333
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200334 switch (typeReq) {
335 case SetPortFeature:
336 if (wValue == USB_PORT_FEAT_POWER) {
337 dev_dbg(hcd->self.controller, "SetPortFeat: POWER\n");
Nicolas Ferre0ee6d1e2012-03-21 16:53:09 +0100338 if (valid_port(wIndex)) {
339 ohci_at91_usb_set_power(pdata, wIndex, 1);
340 ret = 0;
341 }
342
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200343 goto out;
344 }
345 break;
346
347 case ClearPortFeature:
348 switch (wValue) {
349 case USB_PORT_FEAT_C_OVER_CURRENT:
350 dev_dbg(hcd->self.controller,
351 "ClearPortFeature: C_OVER_CURRENT\n");
352
Nicolas Ferre0ee6d1e2012-03-21 16:53:09 +0100353 if (valid_port(wIndex)) {
354 pdata->overcurrent_changed[wIndex] = 0;
355 pdata->overcurrent_status[wIndex] = 0;
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200356 }
357
358 goto out;
359
360 case USB_PORT_FEAT_OVER_CURRENT:
361 dev_dbg(hcd->self.controller,
362 "ClearPortFeature: OVER_CURRENT\n");
363
Nicolas Ferre0ee6d1e2012-03-21 16:53:09 +0100364 if (valid_port(wIndex))
365 pdata->overcurrent_status[wIndex] = 0;
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200366
367 goto out;
368
369 case USB_PORT_FEAT_POWER:
370 dev_dbg(hcd->self.controller,
371 "ClearPortFeature: POWER\n");
372
Nicolas Ferre0ee6d1e2012-03-21 16:53:09 +0100373 if (valid_port(wIndex)) {
374 ohci_at91_usb_set_power(pdata, wIndex, 0);
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200375 return 0;
376 }
377 }
378 break;
379 }
380
Nicolas Ferre0ee6d1e2012-03-21 16:53:09 +0100381 ret = ohci_hub_control(hcd, typeReq, wValue, wIndex + 1, buf, wLength);
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200382 if (ret)
383 goto out;
384
385 switch (typeReq) {
386 case GetHubDescriptor:
387
388 /* update the hub's descriptor */
389
390 desc = (struct usb_hub_descriptor *)buf;
391
392 dev_dbg(hcd->self.controller, "wHubCharacteristics 0x%04x\n",
393 desc->wHubCharacteristics);
394
395 /* remove the old configurations for power-switching, and
396 * over-current protection, and insert our new configuration
397 */
398
399 desc->wHubCharacteristics &= ~cpu_to_le16(HUB_CHAR_LPSM);
400 desc->wHubCharacteristics |= cpu_to_le16(0x0001);
401
402 if (pdata->overcurrent_supported) {
403 desc->wHubCharacteristics &= ~cpu_to_le16(HUB_CHAR_OCPM);
404 desc->wHubCharacteristics |= cpu_to_le16(0x0008|0x0001);
405 }
406
407 dev_dbg(hcd->self.controller, "wHubCharacteristics after 0x%04x\n",
408 desc->wHubCharacteristics);
409
410 return ret;
411
412 case GetPortStatus:
413 /* check port status */
414
415 dev_dbg(hcd->self.controller, "GetPortStatus(%d)\n", wIndex);
416
Nicolas Ferre0ee6d1e2012-03-21 16:53:09 +0100417 if (valid_port(wIndex)) {
418 if (!ohci_at91_usb_get_power(pdata, wIndex))
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200419 *data &= ~cpu_to_le32(RH_PS_PPS);
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200420
Nicolas Ferre0ee6d1e2012-03-21 16:53:09 +0100421 if (pdata->overcurrent_changed[wIndex])
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200422 *data |= cpu_to_le32(RH_PS_OCIC);
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200423
Nicolas Ferre0ee6d1e2012-03-21 16:53:09 +0100424 if (pdata->overcurrent_status[wIndex])
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200425 *data |= cpu_to_le32(RH_PS_POCI);
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200426 }
427 }
428
429 out:
430 return ret;
431}
432
Andrew Victor39a269c2006-01-22 10:32:13 -0800433/*-------------------------------------------------------------------------*/
434
435static const struct hc_driver ohci_at91_hc_driver = {
436 .description = hcd_name,
David Brownell0365ee02006-06-19 14:27:20 -0700437 .product_desc = "AT91 OHCI",
Andrew Victor39a269c2006-01-22 10:32:13 -0800438 .hcd_priv_size = sizeof(struct ohci_hcd),
439
440 /*
441 * generic hardware linkage
442 */
443 .irq = ohci_irq,
444 .flags = HCD_USB11 | HCD_MEMORY,
445
446 /*
447 * basic lifecycle operations
448 */
Nicolas Ferre07e4e552012-05-09 10:48:54 +0200449 .reset = ohci_at91_reset,
Andrew Victor39a269c2006-01-22 10:32:13 -0800450 .start = ohci_at91_start,
451 .stop = ohci_stop,
David Brownelldd9048a2006-12-05 03:18:31 -0800452 .shutdown = ohci_shutdown,
Andrew Victor39a269c2006-01-22 10:32:13 -0800453
454 /*
455 * managing i/o requests and associated device resources
456 */
457 .urb_enqueue = ohci_urb_enqueue,
458 .urb_dequeue = ohci_urb_dequeue,
459 .endpoint_disable = ohci_endpoint_disable,
460
461 /*
462 * scheduling support
463 */
464 .get_frame_number = ohci_get_frame,
465
466 /*
467 * root hub support
468 */
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200469 .hub_status_data = ohci_at91_hub_status_data,
470 .hub_control = ohci_at91_hub_control,
Andrew Victor39a269c2006-01-22 10:32:13 -0800471#ifdef CONFIG_PM
David Brownell68ba61b2006-04-02 20:26:21 -0800472 .bus_suspend = ohci_bus_suspend,
473 .bus_resume = ohci_bus_resume,
Andrew Victor39a269c2006-01-22 10:32:13 -0800474#endif
475 .start_port_reset = ohci_start_port_reset,
476};
477
478/*-------------------------------------------------------------------------*/
479
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200480static irqreturn_t ohci_hcd_at91_overcurrent_irq(int irq, void *data)
481{
482 struct platform_device *pdev = data;
Jingoo Hand4f09e22013-07-30 19:59:40 +0900483 struct at91_usbh_data *pdata = dev_get_platdata(&pdev->dev);
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200484 int val, gpio, port;
485
486 /* From the GPIO notifying the over-current situation, find
487 * out the corresponding port */
Nicolas Ferre0ee6d1e2012-03-21 16:53:09 +0100488 at91_for_each_port(port) {
Joachim Eastwood01bb6502012-09-23 22:56:00 +0200489 if (gpio_is_valid(pdata->overcurrent_pin[port]) &&
490 gpio_to_irq(pdata->overcurrent_pin[port]) == irq) {
Nicolas Ferree4499072012-02-11 14:23:06 +0100491 gpio = pdata->overcurrent_pin[port];
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200492 break;
Nicolas Ferree4499072012-02-11 14:23:06 +0100493 }
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200494 }
495
Nicolas Ferre0ee6d1e2012-03-21 16:53:09 +0100496 if (port == AT91_MAX_USBH_PORTS) {
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200497 dev_err(& pdev->dev, "overcurrent interrupt from unknown GPIO\n");
498 return IRQ_HANDLED;
499 }
500
501 val = gpio_get_value(gpio);
502
503 /* When notified of an over-current situation, disable power
504 on the corresponding port, and mark this port in
505 over-current. */
Nicolas Ferre0ee6d1e2012-03-21 16:53:09 +0100506 if (!val) {
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200507 ohci_at91_usb_set_power(pdata, port, 0);
508 pdata->overcurrent_status[port] = 1;
509 pdata->overcurrent_changed[port] = 1;
510 }
511
512 dev_dbg(& pdev->dev, "overcurrent situation %s\n",
513 val ? "exited" : "notified");
514
515 return IRQ_HANDLED;
516}
517
Jean-Christophe PLAGNIOL-VILLARD24197302011-11-21 06:55:18 +0800518#ifdef CONFIG_OF
519static const struct of_device_id at91_ohci_dt_ids[] = {
520 { .compatible = "atmel,at91rm9200-ohci" },
521 { /* sentinel */ }
522};
523
524MODULE_DEVICE_TABLE(of, at91_ohci_dt_ids);
525
Bill Pemberton41ac7b32012-11-19 13:21:48 -0500526static int ohci_at91_of_init(struct platform_device *pdev)
Jean-Christophe PLAGNIOL-VILLARD24197302011-11-21 06:55:18 +0800527{
528 struct device_node *np = pdev->dev.of_node;
Nicolas Ferreaaf9f5f2012-03-21 16:58:11 +0100529 int i, gpio;
Jean-Christophe PLAGNIOL-VILLARD24197302011-11-21 06:55:18 +0800530 enum of_gpio_flags flags;
531 struct at91_usbh_data *pdata;
532 u32 ports;
533
534 if (!np)
535 return 0;
536
537 /* Right now device-tree probed devices don't get dma_mask set.
538 * Since shared usb code relies on it, set it here for now.
539 * Once we have dma capability bindings this can go away.
540 */
541 if (!pdev->dev.dma_mask)
Stephen Warren3b9561e2013-05-07 16:53:52 -0600542 pdev->dev.dma_mask = &pdev->dev.coherent_dma_mask;
543 if (!pdev->dev.coherent_dma_mask)
544 pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
Jean-Christophe PLAGNIOL-VILLARD24197302011-11-21 06:55:18 +0800545
546 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
547 if (!pdata)
548 return -ENOMEM;
549
550 if (!of_property_read_u32(np, "num-ports", &ports))
551 pdata->ports = ports;
552
Nicolas Ferre0ee6d1e2012-03-21 16:53:09 +0100553 at91_for_each_port(i) {
Jean-Christophe PLAGNIOL-VILLARD24197302011-11-21 06:55:18 +0800554 gpio = of_get_named_gpio_flags(np, "atmel,vbus-gpio", i, &flags);
555 pdata->vbus_pin[i] = gpio;
556 if (!gpio_is_valid(gpio))
557 continue;
558 pdata->vbus_pin_active_low[i] = flags & OF_GPIO_ACTIVE_LOW;
Jean-Christophe PLAGNIOL-VILLARD24197302011-11-21 06:55:18 +0800559 }
560
Nicolas Ferre0ee6d1e2012-03-21 16:53:09 +0100561 at91_for_each_port(i)
Nicolas Ferreaaf9f5f2012-03-21 16:58:11 +0100562 pdata->overcurrent_pin[i] =
563 of_get_named_gpio_flags(np, "atmel,oc-gpio", i, &flags);
Jean-Christophe PLAGNIOL-VILLARD24197302011-11-21 06:55:18 +0800564
565 pdev->dev.platform_data = pdata;
566
567 return 0;
568}
569#else
Bill Pemberton41ac7b32012-11-19 13:21:48 -0500570static int ohci_at91_of_init(struct platform_device *pdev)
Jean-Christophe PLAGNIOL-VILLARD24197302011-11-21 06:55:18 +0800571{
572 return 0;
573}
574#endif
575
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200576/*-------------------------------------------------------------------------*/
577
Bill Pemberton41ac7b32012-11-19 13:21:48 -0500578static int ohci_hcd_at91_drv_probe(struct platform_device *pdev)
Andrew Victor39a269c2006-01-22 10:32:13 -0800579{
Jean-Christophe PLAGNIOL-VILLARD24197302011-11-21 06:55:18 +0800580 struct at91_usbh_data *pdata;
David Brownell4bde4a42007-12-27 11:19:49 -0800581 int i;
Nicolas Ferreaaf9f5f2012-03-21 16:58:11 +0100582 int gpio;
583 int ret;
David Brownell4bde4a42007-12-27 11:19:49 -0800584
Nicolas Ferre1887ab2b2012-03-28 11:49:01 +0200585 ret = ohci_at91_of_init(pdev);
586 if (ret)
587 return ret;
Jean-Christophe PLAGNIOL-VILLARD24197302011-11-21 06:55:18 +0800588
Jingoo Hand4f09e22013-07-30 19:59:40 +0900589 pdata = dev_get_platdata(&pdev->dev);
Jean-Christophe PLAGNIOL-VILLARD24197302011-11-21 06:55:18 +0800590
David Brownell4bde4a42007-12-27 11:19:49 -0800591 if (pdata) {
Nicolas Ferre0ee6d1e2012-03-21 16:53:09 +0100592 at91_for_each_port(i) {
Nicolas Ferre6fffb772012-08-29 11:49:18 +0200593 /*
594 * do not configure PIO if not in relation with
595 * real USB port on board
596 */
597 if (i >= pdata->ports) {
598 pdata->vbus_pin[i] = -EINVAL;
599 pdata->overcurrent_pin[i] = -EINVAL;
600 break;
601 }
602
Jean-Christophe PLAGNIOL-VILLARD8a7a49d2011-09-19 15:32:23 +0800603 if (!gpio_is_valid(pdata->vbus_pin[i]))
David Brownell4bde4a42007-12-27 11:19:49 -0800604 continue;
Nicolas Ferreaaf9f5f2012-03-21 16:58:11 +0100605 gpio = pdata->vbus_pin[i];
606
607 ret = gpio_request(gpio, "ohci_vbus");
608 if (ret) {
609 dev_err(&pdev->dev,
610 "can't request vbus gpio %d\n", gpio);
611 continue;
612 }
613 ret = gpio_direction_output(gpio,
614 !pdata->vbus_pin_active_low[i]);
615 if (ret) {
616 dev_err(&pdev->dev,
617 "can't put vbus gpio %d as output %d\n",
618 gpio, !pdata->vbus_pin_active_low[i]);
619 gpio_free(gpio);
620 continue;
621 }
622
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200623 ohci_at91_usb_set_power(pdata, i, 1);
624 }
625
Nicolas Ferre0ee6d1e2012-03-21 16:53:09 +0100626 at91_for_each_port(i) {
Jean-Christophe PLAGNIOL-VILLARD8a7a49d2011-09-19 15:32:23 +0800627 if (!gpio_is_valid(pdata->overcurrent_pin[i]))
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200628 continue;
Nicolas Ferreaaf9f5f2012-03-21 16:58:11 +0100629 gpio = pdata->overcurrent_pin[i];
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200630
Nicolas Ferreaaf9f5f2012-03-21 16:58:11 +0100631 ret = gpio_request(gpio, "ohci_overcurrent");
632 if (ret) {
633 dev_err(&pdev->dev,
634 "can't request overcurrent gpio %d\n",
635 gpio);
636 continue;
637 }
638
639 ret = gpio_direction_input(gpio);
640 if (ret) {
641 dev_err(&pdev->dev,
642 "can't configure overcurrent gpio %d as input\n",
643 gpio);
644 gpio_free(gpio);
645 continue;
646 }
647
648 ret = request_irq(gpio_to_irq(gpio),
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200649 ohci_hcd_at91_overcurrent_irq,
650 IRQF_SHARED, "ohci_overcurrent", pdev);
651 if (ret) {
Nicolas Ferreaaf9f5f2012-03-21 16:58:11 +0100652 gpio_free(gpio);
653 dev_err(&pdev->dev,
654 "can't get gpio IRQ for overcurrent\n");
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200655 }
David Brownell4bde4a42007-12-27 11:19:49 -0800656 }
657 }
658
David Brownell0365ee02006-06-19 14:27:20 -0700659 device_init_wakeup(&pdev->dev, 1);
660 return usb_hcd_at91_probe(&ohci_at91_hc_driver, pdev);
Andrew Victor39a269c2006-01-22 10:32:13 -0800661}
662
Bill Pembertonfb4e98a2012-11-19 13:26:20 -0500663static int ohci_hcd_at91_drv_remove(struct platform_device *pdev)
Andrew Victor39a269c2006-01-22 10:32:13 -0800664{
Jingoo Hand4f09e22013-07-30 19:59:40 +0900665 struct at91_usbh_data *pdata = dev_get_platdata(&pdev->dev);
David Brownell4bde4a42007-12-27 11:19:49 -0800666 int i;
667
668 if (pdata) {
Nicolas Ferre0ee6d1e2012-03-21 16:53:09 +0100669 at91_for_each_port(i) {
Jean-Christophe PLAGNIOL-VILLARD8a7a49d2011-09-19 15:32:23 +0800670 if (!gpio_is_valid(pdata->vbus_pin[i]))
David Brownell4bde4a42007-12-27 11:19:49 -0800671 continue;
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200672 ohci_at91_usb_set_power(pdata, i, 0);
David Brownell4bde4a42007-12-27 11:19:49 -0800673 gpio_free(pdata->vbus_pin[i]);
674 }
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200675
Nicolas Ferre0ee6d1e2012-03-21 16:53:09 +0100676 at91_for_each_port(i) {
Jean-Christophe PLAGNIOL-VILLARD8a7a49d2011-09-19 15:32:23 +0800677 if (!gpio_is_valid(pdata->overcurrent_pin[i]))
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200678 continue;
679 free_irq(gpio_to_irq(pdata->overcurrent_pin[i]), pdev);
680 gpio_free(pdata->overcurrent_pin[i]);
681 }
David Brownell4bde4a42007-12-27 11:19:49 -0800682 }
683
David Brownell0365ee02006-06-19 14:27:20 -0700684 device_init_wakeup(&pdev->dev, 0);
Pete Zaitcev421b4bf2008-06-01 14:38:43 -0700685 usb_hcd_at91_remove(platform_get_drvdata(pdev), pdev);
686 return 0;
Andrew Victor39a269c2006-01-22 10:32:13 -0800687}
688
689#ifdef CONFIG_PM
Andrew Victor39a269c2006-01-22 10:32:13 -0800690
David Brownell68ba61b2006-04-02 20:26:21 -0800691static int
David Brownell0365ee02006-06-19 14:27:20 -0700692ohci_hcd_at91_drv_suspend(struct platform_device *pdev, pm_message_t mesg)
David Brownell68ba61b2006-04-02 20:26:21 -0800693{
David Brownell0365ee02006-06-19 14:27:20 -0700694 struct usb_hcd *hcd = platform_get_drvdata(pdev);
695 struct ohci_hcd *ohci = hcd_to_ohci(hcd);
696
697 if (device_may_wakeup(&pdev->dev))
698 enable_irq_wake(hcd->irq);
David Brownell0365ee02006-06-19 14:27:20 -0700699
700 /*
701 * The integrated transceivers seem unable to notice disconnect,
702 * reconnect, or wakeup without the 48 MHz clock active. so for
703 * correctness, always discard connection state (using reset).
704 *
705 * REVISIT: some boards will be able to turn VBUS off...
706 */
707 if (at91_suspend_entering_slow_clock()) {
708 ohci_usb_reset (ohci);
Patrice Vilchez869aa982010-04-28 13:45:40 +0200709 /* flush the writes */
710 (void) ohci_readl (ohci, &ohci->regs->control);
Andrew Victored077bb2007-02-16 10:18:58 -0800711 at91_stop_clock();
David Brownell0365ee02006-06-19 14:27:20 -0700712 }
Andrew Victor39a269c2006-01-22 10:32:13 -0800713
714 return 0;
715}
716
David Brownell0365ee02006-06-19 14:27:20 -0700717static int ohci_hcd_at91_drv_resume(struct platform_device *pdev)
Andrew Victor39a269c2006-01-22 10:32:13 -0800718{
Marc Pignat6dde8962007-01-09 14:00:11 -0800719 struct usb_hcd *hcd = platform_get_drvdata(pdev);
720
721 if (device_may_wakeup(&pdev->dev))
722 disable_irq_wake(hcd->irq);
723
Andrew Victored077bb2007-02-16 10:18:58 -0800724 if (!clocked)
725 at91_start_clock();
Andrew Victor39a269c2006-01-22 10:32:13 -0800726
Florian Fainellicfa49b42012-10-08 15:11:29 +0200727 ohci_resume(hcd, false);
Andrew Victor39a269c2006-01-22 10:32:13 -0800728 return 0;
729}
730#else
731#define ohci_hcd_at91_drv_suspend NULL
732#define ohci_hcd_at91_drv_resume NULL
733#endif
734
Kay Sieversf4fce612008-04-10 21:29:22 -0700735MODULE_ALIAS("platform:at91_ohci");
David Brownell68ba61b2006-04-02 20:26:21 -0800736
Andrew Victor39a269c2006-01-22 10:32:13 -0800737static struct platform_driver ohci_hcd_at91_driver = {
738 .probe = ohci_hcd_at91_drv_probe,
Bill Pemberton76904172012-11-19 13:21:08 -0500739 .remove = ohci_hcd_at91_drv_remove,
Aleksey Gorelov64a21d02006-08-08 17:24:08 -0700740 .shutdown = usb_hcd_platform_shutdown,
Andrew Victor39a269c2006-01-22 10:32:13 -0800741 .suspend = ohci_hcd_at91_drv_suspend,
742 .resume = ohci_hcd_at91_drv_resume,
743 .driver = {
David Brownell0365ee02006-06-19 14:27:20 -0700744 .name = "at91_ohci",
Andrew Victor39a269c2006-01-22 10:32:13 -0800745 .owner = THIS_MODULE,
Jean-Christophe PLAGNIOL-VILLARD24197302011-11-21 06:55:18 +0800746 .of_match_table = of_match_ptr(at91_ohci_dt_ids),
Andrew Victor39a269c2006-01-22 10:32:13 -0800747 },
748};