blob: 7cce85a1f7dc170299e5aded49fed41b306b987f [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 */
Sylvain Rochet1b207452015-01-20 14:39:02 +010036#define hcd_to_ohci_at91_priv(h) \
37 ((struct ohci_at91_priv *)hcd_to_ohci(h)->priv)
38
39struct ohci_at91_priv {
40 struct clk *iclk;
41 struct clk *fclk;
42 struct clk *uclk;
43 struct clk *hclk;
44 bool clocked;
Sylvain Rochet1cee6b82015-01-20 14:39:04 +010045 bool wakeup; /* Saved wake-up state for resume */
Sylvain Rochet1b207452015-01-20 14:39:02 +010046};
Manjunath Goudare3825b42013-09-21 16:38:42 +053047/* interface and function clocks; sometimes also an AHB clock */
48
49#define DRIVER_DESC "OHCI Atmel driver"
50
51static const char hcd_name[] = "ohci-atmel";
52
53static struct hc_driver __read_mostly ohci_at91_hc_driver;
Sylvain Rochet1b207452015-01-20 14:39:02 +010054
55static const struct ohci_driver_overrides ohci_at91_drv_overrides __initconst = {
56 .extra_priv_size = sizeof(struct ohci_at91_priv),
57};
Andrew Victor39a269c2006-01-22 10:32:13 -080058
59extern int usb_disabled(void);
60
61/*-------------------------------------------------------------------------*/
62
Sylvain Rochet1b207452015-01-20 14:39:02 +010063static void at91_start_clock(struct ohci_at91_priv *ohci_at91)
Andrew Victored077bb2007-02-16 10:18:58 -080064{
Sylvain Rochet1cee6b82015-01-20 14:39:04 +010065 if (ohci_at91->clocked)
66 return;
Boris BREZILLON6b0a1cf2013-08-01 19:09:13 +020067 if (IS_ENABLED(CONFIG_COMMON_CLK)) {
Sylvain Rochet1b207452015-01-20 14:39:02 +010068 clk_set_rate(ohci_at91->uclk, 48000000);
69 clk_prepare_enable(ohci_at91->uclk);
Boris BREZILLON6b0a1cf2013-08-01 19:09:13 +020070 }
Sylvain Rochet1b207452015-01-20 14:39:02 +010071 clk_prepare_enable(ohci_at91->hclk);
72 clk_prepare_enable(ohci_at91->iclk);
73 clk_prepare_enable(ohci_at91->fclk);
74 ohci_at91->clocked = true;
Andrew Victored077bb2007-02-16 10:18:58 -080075}
76
Sylvain Rochet1b207452015-01-20 14:39:02 +010077static void at91_stop_clock(struct ohci_at91_priv *ohci_at91)
Andrew Victored077bb2007-02-16 10:18:58 -080078{
Sylvain Rochet1cee6b82015-01-20 14:39:04 +010079 if (!ohci_at91->clocked)
80 return;
Sylvain Rochet1b207452015-01-20 14:39:02 +010081 clk_disable_unprepare(ohci_at91->fclk);
82 clk_disable_unprepare(ohci_at91->iclk);
83 clk_disable_unprepare(ohci_at91->hclk);
Boris BREZILLON6b0a1cf2013-08-01 19:09:13 +020084 if (IS_ENABLED(CONFIG_COMMON_CLK))
Sylvain Rochet1b207452015-01-20 14:39:02 +010085 clk_disable_unprepare(ohci_at91->uclk);
86 ohci_at91->clocked = false;
Andrew Victored077bb2007-02-16 10:18:58 -080087}
88
Andrew Victor39a269c2006-01-22 10:32:13 -080089static void at91_start_hc(struct platform_device *pdev)
90{
91 struct usb_hcd *hcd = platform_get_drvdata(pdev);
92 struct ohci_regs __iomem *regs = hcd->regs;
Sylvain Rochet1b207452015-01-20 14:39:02 +010093 struct ohci_at91_priv *ohci_at91 = hcd_to_ohci_at91_priv(hcd);
Andrew Victor39a269c2006-01-22 10:32:13 -080094
David Brownell0365ee02006-06-19 14:27:20 -070095 dev_dbg(&pdev->dev, "start\n");
Andrew Victor39a269c2006-01-22 10:32:13 -080096
97 /*
98 * Start the USB clocks.
99 */
Sylvain Rochet1b207452015-01-20 14:39:02 +0100100 at91_start_clock(ohci_at91);
Andrew Victor39a269c2006-01-22 10:32:13 -0800101
102 /*
103 * The USB host controller must remain in reset.
104 */
105 writel(0, &regs->control);
106}
107
108static void at91_stop_hc(struct platform_device *pdev)
109{
110 struct usb_hcd *hcd = platform_get_drvdata(pdev);
111 struct ohci_regs __iomem *regs = hcd->regs;
Sylvain Rochet1b207452015-01-20 14:39:02 +0100112 struct ohci_at91_priv *ohci_at91 = hcd_to_ohci_at91_priv(hcd);
Andrew Victor39a269c2006-01-22 10:32:13 -0800113
David Brownell0365ee02006-06-19 14:27:20 -0700114 dev_dbg(&pdev->dev, "stop\n");
Andrew Victor39a269c2006-01-22 10:32:13 -0800115
116 /*
117 * Put the USB host controller into reset.
118 */
119 writel(0, &regs->control);
120
121 /*
122 * Stop the USB clocks.
123 */
Sylvain Rochet1b207452015-01-20 14:39:02 +0100124 at91_stop_clock(ohci_at91);
Andrew Victor39a269c2006-01-22 10:32:13 -0800125}
126
127
128/*-------------------------------------------------------------------------*/
129
Bill Pembertonfb4e98a2012-11-19 13:26:20 -0500130static void usb_hcd_at91_remove (struct usb_hcd *, struct platform_device *);
Andrew Victor39a269c2006-01-22 10:32:13 -0800131
132/* configure so an HC device and id are always provided */
133/* always called with process context; sleeping is OK */
134
135
136/**
David Brownell0365ee02006-06-19 14:27:20 -0700137 * usb_hcd_at91_probe - initialize AT91-based HCDs
Andrew Victor39a269c2006-01-22 10:32:13 -0800138 * Context: !in_interrupt()
139 *
140 * Allocates basic resources for this USB host controller, and
141 * then invokes the start() method for the HCD associated with it
142 * through the hotplug entry's driver_data.
Andrew Victor39a269c2006-01-22 10:32:13 -0800143 */
Bill Pemberton41ac7b32012-11-19 13:21:48 -0500144static int usb_hcd_at91_probe(const struct hc_driver *driver,
David Brownell0365ee02006-06-19 14:27:20 -0700145 struct platform_device *pdev)
Andrew Victor39a269c2006-01-22 10:32:13 -0800146{
Manjunath Goudare3825b42013-09-21 16:38:42 +0530147 struct at91_usbh_data *board;
148 struct ohci_hcd *ohci;
Andrew Victor39a269c2006-01-22 10:32:13 -0800149 int retval;
Sylvain Rochet3b3394a2015-01-20 14:39:03 +0100150 struct usb_hcd *hcd;
Sylvain Rochet1b207452015-01-20 14:39:02 +0100151 struct ohci_at91_priv *ohci_at91;
Boris BREZILLONfb5f1832013-12-08 15:59:59 +0100152 struct device *dev = &pdev->dev;
153 struct resource *res;
154 int irq;
Andrew Victor39a269c2006-01-22 10:32:13 -0800155
Boris BREZILLONfb5f1832013-12-08 15:59:59 +0100156 irq = platform_get_irq(pdev, 0);
157 if (irq < 0) {
158 dev_dbg(dev, "hcd probe: missing irq resource\n");
159 return irq;
Andrew Victor39a269c2006-01-22 10:32:13 -0800160 }
161
Boris BREZILLON5b218a02013-12-09 09:51:54 +0100162 hcd = usb_create_hcd(driver, dev, "at91");
Andrew Victor39a269c2006-01-22 10:32:13 -0800163 if (!hcd)
164 return -ENOMEM;
Sylvain Rochet1b207452015-01-20 14:39:02 +0100165 ohci_at91 = hcd_to_ohci_at91_priv(hcd);
Andrew Victor39a269c2006-01-22 10:32:13 -0800166
Varka Bhadram04dc3152014-11-04 07:51:12 +0530167 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Boris BREZILLONcca2bbb2013-12-09 09:51:53 +0100168 hcd->regs = devm_ioremap_resource(dev, res);
169 if (IS_ERR(hcd->regs)) {
170 retval = PTR_ERR(hcd->regs);
171 goto err;
Andrew Victor39a269c2006-01-22 10:32:13 -0800172 }
Varka Bhadram04dc3152014-11-04 07:51:12 +0530173 hcd->rsrc_start = res->start;
174 hcd->rsrc_len = resource_size(res);
Andrew Victor39a269c2006-01-22 10:32:13 -0800175
Sylvain Rochet1b207452015-01-20 14:39:02 +0100176 ohci_at91->iclk = devm_clk_get(dev, "ohci_clk");
177 if (IS_ERR(ohci_at91->iclk)) {
Boris BREZILLON5b218a02013-12-09 09:51:54 +0100178 dev_err(dev, "failed to get ohci_clk\n");
Sylvain Rochet1b207452015-01-20 14:39:02 +0100179 retval = PTR_ERR(ohci_at91->iclk);
Boris BREZILLONcca2bbb2013-12-09 09:51:53 +0100180 goto err;
Jean-Christophe PLAGNIOL-VILLARD68134632011-12-08 16:32:00 +0100181 }
Sylvain Rochet1b207452015-01-20 14:39:02 +0100182 ohci_at91->fclk = devm_clk_get(dev, "uhpck");
183 if (IS_ERR(ohci_at91->fclk)) {
Boris BREZILLON5b218a02013-12-09 09:51:54 +0100184 dev_err(dev, "failed to get uhpck\n");
Sylvain Rochet1b207452015-01-20 14:39:02 +0100185 retval = PTR_ERR(ohci_at91->fclk);
Boris BREZILLONfc7a3252013-12-09 09:51:55 +0100186 goto err;
Jean-Christophe PLAGNIOL-VILLARD68134632011-12-08 16:32:00 +0100187 }
Sylvain Rochet1b207452015-01-20 14:39:02 +0100188 ohci_at91->hclk = devm_clk_get(dev, "hclk");
189 if (IS_ERR(ohci_at91->hclk)) {
Boris BREZILLON5b218a02013-12-09 09:51:54 +0100190 dev_err(dev, "failed to get hclk\n");
Sylvain Rochet1b207452015-01-20 14:39:02 +0100191 retval = PTR_ERR(ohci_at91->hclk);
Boris BREZILLONfc7a3252013-12-09 09:51:55 +0100192 goto err;
Jean-Christophe PLAGNIOL-VILLARD68134632011-12-08 16:32:00 +0100193 }
Boris BREZILLON6b0a1cf2013-08-01 19:09:13 +0200194 if (IS_ENABLED(CONFIG_COMMON_CLK)) {
Sylvain Rochet1b207452015-01-20 14:39:02 +0100195 ohci_at91->uclk = devm_clk_get(dev, "usb_clk");
196 if (IS_ERR(ohci_at91->uclk)) {
Boris BREZILLON5b218a02013-12-09 09:51:54 +0100197 dev_err(dev, "failed to get uclk\n");
Sylvain Rochet1b207452015-01-20 14:39:02 +0100198 retval = PTR_ERR(ohci_at91->uclk);
Boris BREZILLONfc7a3252013-12-09 09:51:55 +0100199 goto err;
Boris BREZILLON6b0a1cf2013-08-01 19:09:13 +0200200 }
201 }
Andrew Victor39a269c2006-01-22 10:32:13 -0800202
Manjunath Goudare3825b42013-09-21 16:38:42 +0530203 board = hcd->self.controller->platform_data;
204 ohci = hcd_to_ohci(hcd);
205 ohci->num_ports = board->ports;
Andrew Victor39a269c2006-01-22 10:32:13 -0800206 at91_start_hc(pdev);
Andrew Victor39a269c2006-01-22 10:32:13 -0800207
Boris BREZILLONfb5f1832013-12-08 15:59:59 +0100208 retval = usb_add_hcd(hcd, irq, IRQF_SHARED);
Arnd Bergmann1f53b482014-01-07 12:54:58 +0100209 if (retval == 0) {
Peter Chen3c9740a2013-11-05 10:46:02 +0800210 device_wakeup_enable(hcd->self.controller);
Andrew Victor39a269c2006-01-22 10:32:13 -0800211 return retval;
Peter Chen3c9740a2013-11-05 10:46:02 +0800212 }
Andrew Victor39a269c2006-01-22 10:32:13 -0800213
214 /* Error handling */
215 at91_stop_hc(pdev);
216
Boris BREZILLONcca2bbb2013-12-09 09:51:53 +0100217 err:
Andrew Victor39a269c2006-01-22 10:32:13 -0800218 usb_put_hcd(hcd);
219 return retval;
220}
221
222
Andrew Victor39a269c2006-01-22 10:32:13 -0800223/* may be called with controller, bus, and devices active */
224
225/**
David Brownell0365ee02006-06-19 14:27:20 -0700226 * usb_hcd_at91_remove - shutdown processing for AT91-based HCDs
Andrew Victor39a269c2006-01-22 10:32:13 -0800227 * @dev: USB Host Controller being removed
228 * Context: !in_interrupt()
229 *
230 * Reverses the effect of usb_hcd_at91_probe(), first invoking
231 * the HCD's stop() method. It is always called from a thread
David Brownell0365ee02006-06-19 14:27:20 -0700232 * context, "rmmod" or something similar.
Andrew Victor39a269c2006-01-22 10:32:13 -0800233 *
234 */
Bill Pembertonfb4e98a2012-11-19 13:26:20 -0500235static void usb_hcd_at91_remove(struct usb_hcd *hcd,
David Brownell0365ee02006-06-19 14:27:20 -0700236 struct platform_device *pdev)
Andrew Victor39a269c2006-01-22 10:32:13 -0800237{
238 usb_remove_hcd(hcd);
239 at91_stop_hc(pdev);
Pete Zaitcev421b4bf2008-06-01 14:38:43 -0700240 usb_put_hcd(hcd);
Andrew Victor39a269c2006-01-22 10:32:13 -0800241}
242
243/*-------------------------------------------------------------------------*/
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200244static void ohci_at91_usb_set_power(struct at91_usbh_data *pdata, int port, int enable)
245{
Nicolas Ferre0ee6d1e2012-03-21 16:53:09 +0100246 if (!valid_port(port))
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200247 return;
248
Jean-Christophe PLAGNIOL-VILLARD8a7a49d2011-09-19 15:32:23 +0800249 if (!gpio_is_valid(pdata->vbus_pin[port]))
Jean-Christophe PLAGNIOL-VILLARD770f0ba2011-11-12 16:46:13 +0100250 return;
251
Jean-Christophe PLAGNIOL-VILLARD8134ff52011-12-08 16:32:01 +0100252 gpio_set_value(pdata->vbus_pin[port],
Nicolas Ferre1e7caf82012-03-21 14:38:55 +0100253 pdata->vbus_pin_active_low[port] ^ enable);
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200254}
255
256static int ohci_at91_usb_get_power(struct at91_usbh_data *pdata, int port)
257{
Nicolas Ferre0ee6d1e2012-03-21 16:53:09 +0100258 if (!valid_port(port))
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200259 return -EINVAL;
260
Jean-Christophe PLAGNIOL-VILLARD8a7a49d2011-09-19 15:32:23 +0800261 if (!gpio_is_valid(pdata->vbus_pin[port]))
Jean-Christophe PLAGNIOL-VILLARD770f0ba2011-11-12 16:46:13 +0100262 return -EINVAL;
263
Jean-Christophe PLAGNIOL-VILLARD8134ff52011-12-08 16:32:01 +0100264 return gpio_get_value(pdata->vbus_pin[port]) ^
Nicolas Ferre1e7caf82012-03-21 14:38:55 +0100265 pdata->vbus_pin_active_low[port];
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200266}
267
268/*
269 * Update the status data from the hub with the over-current indicator change.
270 */
271static int ohci_at91_hub_status_data(struct usb_hcd *hcd, char *buf)
272{
Manjunath Goudare3825b42013-09-21 16:38:42 +0530273 struct at91_usbh_data *pdata = hcd->self.controller->platform_data;
Laurent Pinchart42b59eb2014-04-16 18:00:09 +0200274 int length = ohci_hub_status_data(hcd, buf);
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200275 int port;
276
Nicolas Ferre0ee6d1e2012-03-21 16:53:09 +0100277 at91_for_each_port(port) {
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200278 if (pdata->overcurrent_changed[port]) {
Nicolas Ferre0ee6d1e2012-03-21 16:53:09 +0100279 if (!length)
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200280 length = 1;
281 buf[0] |= 1 << (port + 1);
282 }
283 }
284
285 return length;
286}
287
288/*
289 * Look at the control requests to the root hub and see if we need to override.
290 */
291static int ohci_at91_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
292 u16 wIndex, char *buf, u16 wLength)
293{
Jingoo Hand4f09e22013-07-30 19:59:40 +0900294 struct at91_usbh_data *pdata = dev_get_platdata(hcd->self.controller);
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200295 struct usb_hub_descriptor *desc;
296 int ret = -EINVAL;
297 u32 *data = (u32 *)buf;
298
299 dev_dbg(hcd->self.controller,
300 "ohci_at91_hub_control(%p,0x%04x,0x%04x,0x%04x,%p,%04x)\n",
301 hcd, typeReq, wValue, wIndex, buf, wLength);
302
Nicolas Ferre0ee6d1e2012-03-21 16:53:09 +0100303 wIndex--;
304
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200305 switch (typeReq) {
306 case SetPortFeature:
307 if (wValue == USB_PORT_FEAT_POWER) {
308 dev_dbg(hcd->self.controller, "SetPortFeat: POWER\n");
Nicolas Ferre0ee6d1e2012-03-21 16:53:09 +0100309 if (valid_port(wIndex)) {
310 ohci_at91_usb_set_power(pdata, wIndex, 1);
311 ret = 0;
312 }
313
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200314 goto out;
315 }
316 break;
317
318 case ClearPortFeature:
319 switch (wValue) {
320 case USB_PORT_FEAT_C_OVER_CURRENT:
321 dev_dbg(hcd->self.controller,
322 "ClearPortFeature: C_OVER_CURRENT\n");
323
Nicolas Ferre0ee6d1e2012-03-21 16:53:09 +0100324 if (valid_port(wIndex)) {
325 pdata->overcurrent_changed[wIndex] = 0;
326 pdata->overcurrent_status[wIndex] = 0;
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200327 }
328
329 goto out;
330
331 case USB_PORT_FEAT_OVER_CURRENT:
332 dev_dbg(hcd->self.controller,
333 "ClearPortFeature: OVER_CURRENT\n");
334
Nicolas Ferre0ee6d1e2012-03-21 16:53:09 +0100335 if (valid_port(wIndex))
336 pdata->overcurrent_status[wIndex] = 0;
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200337
338 goto out;
339
340 case USB_PORT_FEAT_POWER:
341 dev_dbg(hcd->self.controller,
342 "ClearPortFeature: POWER\n");
343
Nicolas Ferre0ee6d1e2012-03-21 16:53:09 +0100344 if (valid_port(wIndex)) {
345 ohci_at91_usb_set_power(pdata, wIndex, 0);
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200346 return 0;
347 }
348 }
349 break;
350 }
351
Laurent Pinchart42b59eb2014-04-16 18:00:09 +0200352 ret = ohci_hub_control(hcd, typeReq, wValue, wIndex + 1, buf, wLength);
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200353 if (ret)
354 goto out;
355
356 switch (typeReq) {
357 case GetHubDescriptor:
358
359 /* update the hub's descriptor */
360
361 desc = (struct usb_hub_descriptor *)buf;
362
363 dev_dbg(hcd->self.controller, "wHubCharacteristics 0x%04x\n",
364 desc->wHubCharacteristics);
365
366 /* remove the old configurations for power-switching, and
367 * over-current protection, and insert our new configuration
368 */
369
370 desc->wHubCharacteristics &= ~cpu_to_le16(HUB_CHAR_LPSM);
Sergei Shtylyova9c49bc2015-01-19 01:39:44 +0300371 desc->wHubCharacteristics |=
372 cpu_to_le16(HUB_CHAR_INDV_PORT_LPSM);
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200373
374 if (pdata->overcurrent_supported) {
375 desc->wHubCharacteristics &= ~cpu_to_le16(HUB_CHAR_OCPM);
Sergei Shtylyova9c49bc2015-01-19 01:39:44 +0300376 desc->wHubCharacteristics |=
377 cpu_to_le16(HUB_CHAR_INDV_PORT_OCPM);
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200378 }
379
380 dev_dbg(hcd->self.controller, "wHubCharacteristics after 0x%04x\n",
381 desc->wHubCharacteristics);
382
383 return ret;
384
385 case GetPortStatus:
386 /* check port status */
387
388 dev_dbg(hcd->self.controller, "GetPortStatus(%d)\n", wIndex);
389
Nicolas Ferre0ee6d1e2012-03-21 16:53:09 +0100390 if (valid_port(wIndex)) {
391 if (!ohci_at91_usb_get_power(pdata, wIndex))
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200392 *data &= ~cpu_to_le32(RH_PS_PPS);
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200393
Nicolas Ferre0ee6d1e2012-03-21 16:53:09 +0100394 if (pdata->overcurrent_changed[wIndex])
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200395 *data |= cpu_to_le32(RH_PS_OCIC);
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200396
Nicolas Ferre0ee6d1e2012-03-21 16:53:09 +0100397 if (pdata->overcurrent_status[wIndex])
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200398 *data |= cpu_to_le32(RH_PS_POCI);
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200399 }
400 }
401
402 out:
403 return ret;
404}
405
Andrew Victor39a269c2006-01-22 10:32:13 -0800406/*-------------------------------------------------------------------------*/
407
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200408static irqreturn_t ohci_hcd_at91_overcurrent_irq(int irq, void *data)
409{
410 struct platform_device *pdev = data;
Jingoo Hand4f09e22013-07-30 19:59:40 +0900411 struct at91_usbh_data *pdata = dev_get_platdata(&pdev->dev);
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200412 int val, gpio, port;
413
414 /* From the GPIO notifying the over-current situation, find
415 * out the corresponding port */
Nicolas Ferre0ee6d1e2012-03-21 16:53:09 +0100416 at91_for_each_port(port) {
Joachim Eastwood01bb6502012-09-23 22:56:00 +0200417 if (gpio_is_valid(pdata->overcurrent_pin[port]) &&
418 gpio_to_irq(pdata->overcurrent_pin[port]) == irq) {
Nicolas Ferree4499072012-02-11 14:23:06 +0100419 gpio = pdata->overcurrent_pin[port];
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200420 break;
Nicolas Ferree4499072012-02-11 14:23:06 +0100421 }
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200422 }
423
Nicolas Ferre0ee6d1e2012-03-21 16:53:09 +0100424 if (port == AT91_MAX_USBH_PORTS) {
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200425 dev_err(& pdev->dev, "overcurrent interrupt from unknown GPIO\n");
426 return IRQ_HANDLED;
427 }
428
429 val = gpio_get_value(gpio);
430
431 /* When notified of an over-current situation, disable power
432 on the corresponding port, and mark this port in
433 over-current. */
Nicolas Ferre0ee6d1e2012-03-21 16:53:09 +0100434 if (!val) {
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200435 ohci_at91_usb_set_power(pdata, port, 0);
436 pdata->overcurrent_status[port] = 1;
437 pdata->overcurrent_changed[port] = 1;
438 }
439
440 dev_dbg(& pdev->dev, "overcurrent situation %s\n",
441 val ? "exited" : "notified");
442
443 return IRQ_HANDLED;
444}
445
Jean-Christophe PLAGNIOL-VILLARD24197302011-11-21 06:55:18 +0800446#ifdef CONFIG_OF
447static const struct of_device_id at91_ohci_dt_ids[] = {
448 { .compatible = "atmel,at91rm9200-ohci" },
449 { /* sentinel */ }
450};
451
452MODULE_DEVICE_TABLE(of, at91_ohci_dt_ids);
453
Bill Pemberton41ac7b32012-11-19 13:21:48 -0500454static int ohci_at91_of_init(struct platform_device *pdev)
Jean-Christophe PLAGNIOL-VILLARD24197302011-11-21 06:55:18 +0800455{
456 struct device_node *np = pdev->dev.of_node;
Russell King22d9d8e2013-06-10 16:28:49 +0100457 int i, gpio, ret;
Jean-Christophe PLAGNIOL-VILLARD24197302011-11-21 06:55:18 +0800458 enum of_gpio_flags flags;
459 struct at91_usbh_data *pdata;
460 u32 ports;
461
462 if (!np)
463 return 0;
464
465 /* Right now device-tree probed devices don't get dma_mask set.
466 * Since shared usb code relies on it, set it here for now.
467 * Once we have dma capability bindings this can go away.
468 */
Russell Kinge1fd7342013-06-27 12:36:37 +0100469 ret = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
Russell King22d9d8e2013-06-10 16:28:49 +0100470 if (ret)
471 return ret;
Jean-Christophe PLAGNIOL-VILLARD24197302011-11-21 06:55:18 +0800472
473 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
474 if (!pdata)
475 return -ENOMEM;
476
477 if (!of_property_read_u32(np, "num-ports", &ports))
478 pdata->ports = ports;
479
Nicolas Ferre0ee6d1e2012-03-21 16:53:09 +0100480 at91_for_each_port(i) {
Jean-Christophe PLAGNIOL-VILLARD24197302011-11-21 06:55:18 +0800481 gpio = of_get_named_gpio_flags(np, "atmel,vbus-gpio", i, &flags);
482 pdata->vbus_pin[i] = gpio;
483 if (!gpio_is_valid(gpio))
484 continue;
485 pdata->vbus_pin_active_low[i] = flags & OF_GPIO_ACTIVE_LOW;
Jean-Christophe PLAGNIOL-VILLARD24197302011-11-21 06:55:18 +0800486 }
487
Nicolas Ferre0ee6d1e2012-03-21 16:53:09 +0100488 at91_for_each_port(i)
Nicolas Ferreaaf9f5f2012-03-21 16:58:11 +0100489 pdata->overcurrent_pin[i] =
490 of_get_named_gpio_flags(np, "atmel,oc-gpio", i, &flags);
Jean-Christophe PLAGNIOL-VILLARD24197302011-11-21 06:55:18 +0800491
492 pdev->dev.platform_data = pdata;
493
494 return 0;
495}
496#else
Bill Pemberton41ac7b32012-11-19 13:21:48 -0500497static int ohci_at91_of_init(struct platform_device *pdev)
Jean-Christophe PLAGNIOL-VILLARD24197302011-11-21 06:55:18 +0800498{
499 return 0;
500}
501#endif
502
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200503/*-------------------------------------------------------------------------*/
504
Bill Pemberton41ac7b32012-11-19 13:21:48 -0500505static int ohci_hcd_at91_drv_probe(struct platform_device *pdev)
Andrew Victor39a269c2006-01-22 10:32:13 -0800506{
Jean-Christophe PLAGNIOL-VILLARD24197302011-11-21 06:55:18 +0800507 struct at91_usbh_data *pdata;
David Brownell4bde4a42007-12-27 11:19:49 -0800508 int i;
Nicolas Ferreaaf9f5f2012-03-21 16:58:11 +0100509 int gpio;
510 int ret;
David Brownell4bde4a42007-12-27 11:19:49 -0800511
Nicolas Ferre1887ab2b2012-03-28 11:49:01 +0200512 ret = ohci_at91_of_init(pdev);
513 if (ret)
514 return ret;
Jean-Christophe PLAGNIOL-VILLARD24197302011-11-21 06:55:18 +0800515
Jingoo Hand4f09e22013-07-30 19:59:40 +0900516 pdata = dev_get_platdata(&pdev->dev);
Jean-Christophe PLAGNIOL-VILLARD24197302011-11-21 06:55:18 +0800517
David Brownell4bde4a42007-12-27 11:19:49 -0800518 if (pdata) {
Nicolas Ferre0ee6d1e2012-03-21 16:53:09 +0100519 at91_for_each_port(i) {
Nicolas Ferre6fffb772012-08-29 11:49:18 +0200520 /*
521 * do not configure PIO if not in relation with
522 * real USB port on board
523 */
524 if (i >= pdata->ports) {
525 pdata->vbus_pin[i] = -EINVAL;
526 pdata->overcurrent_pin[i] = -EINVAL;
527 break;
528 }
529
Jean-Christophe PLAGNIOL-VILLARD8a7a49d2011-09-19 15:32:23 +0800530 if (!gpio_is_valid(pdata->vbus_pin[i]))
David Brownell4bde4a42007-12-27 11:19:49 -0800531 continue;
Nicolas Ferreaaf9f5f2012-03-21 16:58:11 +0100532 gpio = pdata->vbus_pin[i];
533
534 ret = gpio_request(gpio, "ohci_vbus");
535 if (ret) {
536 dev_err(&pdev->dev,
537 "can't request vbus gpio %d\n", gpio);
538 continue;
539 }
540 ret = gpio_direction_output(gpio,
541 !pdata->vbus_pin_active_low[i]);
542 if (ret) {
543 dev_err(&pdev->dev,
544 "can't put vbus gpio %d as output %d\n",
545 gpio, !pdata->vbus_pin_active_low[i]);
546 gpio_free(gpio);
547 continue;
548 }
549
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200550 ohci_at91_usb_set_power(pdata, i, 1);
551 }
552
Nicolas Ferre0ee6d1e2012-03-21 16:53:09 +0100553 at91_for_each_port(i) {
Jean-Christophe PLAGNIOL-VILLARD8a7a49d2011-09-19 15:32:23 +0800554 if (!gpio_is_valid(pdata->overcurrent_pin[i]))
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200555 continue;
Nicolas Ferreaaf9f5f2012-03-21 16:58:11 +0100556 gpio = pdata->overcurrent_pin[i];
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200557
Nicolas Ferreaaf9f5f2012-03-21 16:58:11 +0100558 ret = gpio_request(gpio, "ohci_overcurrent");
559 if (ret) {
560 dev_err(&pdev->dev,
561 "can't request overcurrent gpio %d\n",
562 gpio);
563 continue;
564 }
565
566 ret = gpio_direction_input(gpio);
567 if (ret) {
568 dev_err(&pdev->dev,
569 "can't configure overcurrent gpio %d as input\n",
570 gpio);
571 gpio_free(gpio);
572 continue;
573 }
574
575 ret = request_irq(gpio_to_irq(gpio),
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200576 ohci_hcd_at91_overcurrent_irq,
577 IRQF_SHARED, "ohci_overcurrent", pdev);
578 if (ret) {
Nicolas Ferreaaf9f5f2012-03-21 16:58:11 +0100579 gpio_free(gpio);
580 dev_err(&pdev->dev,
581 "can't get gpio IRQ for overcurrent\n");
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200582 }
David Brownell4bde4a42007-12-27 11:19:49 -0800583 }
584 }
585
David Brownell0365ee02006-06-19 14:27:20 -0700586 device_init_wakeup(&pdev->dev, 1);
587 return usb_hcd_at91_probe(&ohci_at91_hc_driver, pdev);
Andrew Victor39a269c2006-01-22 10:32:13 -0800588}
589
Bill Pembertonfb4e98a2012-11-19 13:26:20 -0500590static int ohci_hcd_at91_drv_remove(struct platform_device *pdev)
Andrew Victor39a269c2006-01-22 10:32:13 -0800591{
Jingoo Hand4f09e22013-07-30 19:59:40 +0900592 struct at91_usbh_data *pdata = dev_get_platdata(&pdev->dev);
David Brownell4bde4a42007-12-27 11:19:49 -0800593 int i;
594
595 if (pdata) {
Nicolas Ferre0ee6d1e2012-03-21 16:53:09 +0100596 at91_for_each_port(i) {
Jean-Christophe PLAGNIOL-VILLARD8a7a49d2011-09-19 15:32:23 +0800597 if (!gpio_is_valid(pdata->vbus_pin[i]))
David Brownell4bde4a42007-12-27 11:19:49 -0800598 continue;
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200599 ohci_at91_usb_set_power(pdata, i, 0);
David Brownell4bde4a42007-12-27 11:19:49 -0800600 gpio_free(pdata->vbus_pin[i]);
601 }
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200602
Nicolas Ferre0ee6d1e2012-03-21 16:53:09 +0100603 at91_for_each_port(i) {
Jean-Christophe PLAGNIOL-VILLARD8a7a49d2011-09-19 15:32:23 +0800604 if (!gpio_is_valid(pdata->overcurrent_pin[i]))
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200605 continue;
606 free_irq(gpio_to_irq(pdata->overcurrent_pin[i]), pdev);
607 gpio_free(pdata->overcurrent_pin[i]);
608 }
David Brownell4bde4a42007-12-27 11:19:49 -0800609 }
610
David Brownell0365ee02006-06-19 14:27:20 -0700611 device_init_wakeup(&pdev->dev, 0);
Pete Zaitcev421b4bf2008-06-01 14:38:43 -0700612 usb_hcd_at91_remove(platform_get_drvdata(pdev), pdev);
613 return 0;
Andrew Victor39a269c2006-01-22 10:32:13 -0800614}
615
616#ifdef CONFIG_PM
Andrew Victor39a269c2006-01-22 10:32:13 -0800617
David Brownell68ba61b2006-04-02 20:26:21 -0800618static int
Sylvain Rochet94ac0e52015-01-20 14:39:00 +0100619ohci_hcd_at91_drv_suspend(struct device *dev)
David Brownell68ba61b2006-04-02 20:26:21 -0800620{
Sylvain Rochet94ac0e52015-01-20 14:39:00 +0100621 struct usb_hcd *hcd = dev_get_drvdata(dev);
David Brownell0365ee02006-06-19 14:27:20 -0700622 struct ohci_hcd *ohci = hcd_to_ohci(hcd);
Sylvain Rochet1b207452015-01-20 14:39:02 +0100623 struct ohci_at91_priv *ohci_at91 = hcd_to_ohci_at91_priv(hcd);
Majunath Goudara9d38402013-11-13 17:40:17 +0530624 int ret;
David Brownell0365ee02006-06-19 14:27:20 -0700625
Sylvain Rochet1cee6b82015-01-20 14:39:04 +0100626 /*
627 * Disable wakeup if we are going to sleep with slow clock mode
628 * enabled.
629 */
630 ohci_at91->wakeup = device_may_wakeup(dev)
631 && !at91_suspend_entering_slow_clock();
632
633 if (ohci_at91->wakeup)
David Brownell0365ee02006-06-19 14:27:20 -0700634 enable_irq_wake(hcd->irq);
David Brownell0365ee02006-06-19 14:27:20 -0700635
Sylvain Rochet1cee6b82015-01-20 14:39:04 +0100636 ret = ohci_suspend(hcd, ohci_at91->wakeup);
Majunath Goudara9d38402013-11-13 17:40:17 +0530637 if (ret) {
Sylvain Rochet1cee6b82015-01-20 14:39:04 +0100638 if (ohci_at91->wakeup)
639 disable_irq_wake(hcd->irq);
Majunath Goudara9d38402013-11-13 17:40:17 +0530640 return ret;
641 }
David Brownell0365ee02006-06-19 14:27:20 -0700642 /*
643 * The integrated transceivers seem unable to notice disconnect,
644 * reconnect, or wakeup without the 48 MHz clock active. so for
645 * correctness, always discard connection state (using reset).
646 *
647 * REVISIT: some boards will be able to turn VBUS off...
648 */
Sylvain Rochet1cee6b82015-01-20 14:39:04 +0100649 if (!ohci_at91->wakeup) {
Manjunath Goudare3825b42013-09-21 16:38:42 +0530650 ohci->hc_control = ohci_readl(ohci, &ohci->regs->control);
651 ohci->hc_control &= OHCI_CTRL_RWC;
652 ohci_writel(ohci, ohci->hc_control, &ohci->regs->control);
653 ohci->rh_state = OHCI_RH_HALTED;
654
Patrice Vilchez869aa982010-04-28 13:45:40 +0200655 /* flush the writes */
656 (void) ohci_readl (ohci, &ohci->regs->control);
Sylvain Rochet1b207452015-01-20 14:39:02 +0100657 at91_stop_clock(ohci_at91);
David Brownell0365ee02006-06-19 14:27:20 -0700658 }
Andrew Victor39a269c2006-01-22 10:32:13 -0800659
Majunath Goudara9d38402013-11-13 17:40:17 +0530660 return ret;
Andrew Victor39a269c2006-01-22 10:32:13 -0800661}
662
Sylvain Rochet94ac0e52015-01-20 14:39:00 +0100663static int ohci_hcd_at91_drv_resume(struct device *dev)
Andrew Victor39a269c2006-01-22 10:32:13 -0800664{
Sylvain Rochet94ac0e52015-01-20 14:39:00 +0100665 struct usb_hcd *hcd = dev_get_drvdata(dev);
Sylvain Rochet1b207452015-01-20 14:39:02 +0100666 struct ohci_at91_priv *ohci_at91 = hcd_to_ohci_at91_priv(hcd);
Marc Pignat6dde8962007-01-09 14:00:11 -0800667
Sylvain Rochet1cee6b82015-01-20 14:39:04 +0100668 if (ohci_at91->wakeup)
Marc Pignat6dde8962007-01-09 14:00:11 -0800669 disable_irq_wake(hcd->irq);
670
Sylvain Rochet1cee6b82015-01-20 14:39:04 +0100671 at91_start_clock(ohci_at91);
Andrew Victor39a269c2006-01-22 10:32:13 -0800672
Florian Fainellicfa49b42012-10-08 15:11:29 +0200673 ohci_resume(hcd, false);
Andrew Victor39a269c2006-01-22 10:32:13 -0800674 return 0;
675}
Andrew Victor39a269c2006-01-22 10:32:13 -0800676#endif
677
Sylvain Rochet94ac0e52015-01-20 14:39:00 +0100678static SIMPLE_DEV_PM_OPS(ohci_hcd_at91_pm_ops, ohci_hcd_at91_drv_suspend,
679 ohci_hcd_at91_drv_resume);
680
Andrew Victor39a269c2006-01-22 10:32:13 -0800681static struct platform_driver ohci_hcd_at91_driver = {
682 .probe = ohci_hcd_at91_drv_probe,
Bill Pemberton76904172012-11-19 13:21:08 -0500683 .remove = ohci_hcd_at91_drv_remove,
Aleksey Gorelov64a21d02006-08-08 17:24:08 -0700684 .shutdown = usb_hcd_platform_shutdown,
Andrew Victor39a269c2006-01-22 10:32:13 -0800685 .driver = {
David Brownell0365ee02006-06-19 14:27:20 -0700686 .name = "at91_ohci",
Sylvain Rochet94ac0e52015-01-20 14:39:00 +0100687 .pm = &ohci_hcd_at91_pm_ops,
Jean-Christophe PLAGNIOL-VILLARD24197302011-11-21 06:55:18 +0800688 .of_match_table = of_match_ptr(at91_ohci_dt_ids),
Andrew Victor39a269c2006-01-22 10:32:13 -0800689 },
690};
Manjunath Goudare3825b42013-09-21 16:38:42 +0530691
692static int __init ohci_at91_init(void)
693{
694 if (usb_disabled())
695 return -ENODEV;
696
697 pr_info("%s: " DRIVER_DESC "\n", hcd_name);
Sylvain Rochet1b207452015-01-20 14:39:02 +0100698 ohci_init_driver(&ohci_at91_hc_driver, &ohci_at91_drv_overrides);
Manjunath Goudare3825b42013-09-21 16:38:42 +0530699
700 /*
701 * The Atmel HW has some unusual quirks, which require Atmel-specific
702 * workarounds. We override certain hc_driver functions here to
703 * achieve that. We explicitly do not enhance ohci_driver_overrides to
704 * allow this more easily, since this is an unusual case, and we don't
705 * want to encourage others to override these functions by making it
706 * too easy.
707 */
708
Manjunath Goudare3825b42013-09-21 16:38:42 +0530709 ohci_at91_hc_driver.hub_status_data = ohci_at91_hub_status_data;
710 ohci_at91_hc_driver.hub_control = ohci_at91_hub_control;
711
712 return platform_driver_register(&ohci_hcd_at91_driver);
713}
714module_init(ohci_at91_init);
715
716static void __exit ohci_at91_cleanup(void)
717{
718 platform_driver_unregister(&ohci_hcd_at91_driver);
719}
720module_exit(ohci_at91_cleanup);
721
722MODULE_DESCRIPTION(DRIVER_DESC);
723MODULE_LICENSE("GPL");
724MODULE_ALIAS("platform:at91_ohci");