blob: e2b8b7bc08ec119f3cd02cd443589b32ecefcf93 [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;
45};
Manjunath Goudare3825b42013-09-21 16:38:42 +053046/* interface and function clocks; sometimes also an AHB clock */
47
48#define DRIVER_DESC "OHCI Atmel driver"
49
50static const char hcd_name[] = "ohci-atmel";
51
52static struct hc_driver __read_mostly ohci_at91_hc_driver;
Sylvain Rochet1b207452015-01-20 14:39:02 +010053
54static const struct ohci_driver_overrides ohci_at91_drv_overrides __initconst = {
55 .extra_priv_size = sizeof(struct ohci_at91_priv),
56};
Andrew Victor39a269c2006-01-22 10:32:13 -080057
58extern int usb_disabled(void);
59
60/*-------------------------------------------------------------------------*/
61
Sylvain Rochet1b207452015-01-20 14:39:02 +010062static void at91_start_clock(struct ohci_at91_priv *ohci_at91)
Andrew Victored077bb2007-02-16 10:18:58 -080063{
Boris BREZILLON6b0a1cf2013-08-01 19:09:13 +020064 if (IS_ENABLED(CONFIG_COMMON_CLK)) {
Sylvain Rochet1b207452015-01-20 14:39:02 +010065 clk_set_rate(ohci_at91->uclk, 48000000);
66 clk_prepare_enable(ohci_at91->uclk);
Boris BREZILLON6b0a1cf2013-08-01 19:09:13 +020067 }
Sylvain Rochet1b207452015-01-20 14:39:02 +010068 clk_prepare_enable(ohci_at91->hclk);
69 clk_prepare_enable(ohci_at91->iclk);
70 clk_prepare_enable(ohci_at91->fclk);
71 ohci_at91->clocked = true;
Andrew Victored077bb2007-02-16 10:18:58 -080072}
73
Sylvain Rochet1b207452015-01-20 14:39:02 +010074static void at91_stop_clock(struct ohci_at91_priv *ohci_at91)
Andrew Victored077bb2007-02-16 10:18:58 -080075{
Sylvain Rochet1b207452015-01-20 14:39:02 +010076 clk_disable_unprepare(ohci_at91->fclk);
77 clk_disable_unprepare(ohci_at91->iclk);
78 clk_disable_unprepare(ohci_at91->hclk);
Boris BREZILLON6b0a1cf2013-08-01 19:09:13 +020079 if (IS_ENABLED(CONFIG_COMMON_CLK))
Sylvain Rochet1b207452015-01-20 14:39:02 +010080 clk_disable_unprepare(ohci_at91->uclk);
81 ohci_at91->clocked = false;
Andrew Victored077bb2007-02-16 10:18:58 -080082}
83
Andrew Victor39a269c2006-01-22 10:32:13 -080084static void at91_start_hc(struct platform_device *pdev)
85{
86 struct usb_hcd *hcd = platform_get_drvdata(pdev);
87 struct ohci_regs __iomem *regs = hcd->regs;
Sylvain Rochet1b207452015-01-20 14:39:02 +010088 struct ohci_at91_priv *ohci_at91 = hcd_to_ohci_at91_priv(hcd);
Andrew Victor39a269c2006-01-22 10:32:13 -080089
David Brownell0365ee02006-06-19 14:27:20 -070090 dev_dbg(&pdev->dev, "start\n");
Andrew Victor39a269c2006-01-22 10:32:13 -080091
92 /*
93 * Start the USB clocks.
94 */
Sylvain Rochet1b207452015-01-20 14:39:02 +010095 at91_start_clock(ohci_at91);
Andrew Victor39a269c2006-01-22 10:32:13 -080096
97 /*
98 * The USB host controller must remain in reset.
99 */
100 writel(0, &regs->control);
101}
102
103static void at91_stop_hc(struct platform_device *pdev)
104{
105 struct usb_hcd *hcd = platform_get_drvdata(pdev);
106 struct ohci_regs __iomem *regs = hcd->regs;
Sylvain Rochet1b207452015-01-20 14:39:02 +0100107 struct ohci_at91_priv *ohci_at91 = hcd_to_ohci_at91_priv(hcd);
Andrew Victor39a269c2006-01-22 10:32:13 -0800108
David Brownell0365ee02006-06-19 14:27:20 -0700109 dev_dbg(&pdev->dev, "stop\n");
Andrew Victor39a269c2006-01-22 10:32:13 -0800110
111 /*
112 * Put the USB host controller into reset.
113 */
114 writel(0, &regs->control);
115
116 /*
117 * Stop the USB clocks.
118 */
Sylvain Rochet1b207452015-01-20 14:39:02 +0100119 at91_stop_clock(ohci_at91);
Andrew Victor39a269c2006-01-22 10:32:13 -0800120}
121
122
123/*-------------------------------------------------------------------------*/
124
Bill Pembertonfb4e98a2012-11-19 13:26:20 -0500125static void usb_hcd_at91_remove (struct usb_hcd *, struct platform_device *);
Andrew Victor39a269c2006-01-22 10:32:13 -0800126
127/* configure so an HC device and id are always provided */
128/* always called with process context; sleeping is OK */
129
130
131/**
David Brownell0365ee02006-06-19 14:27:20 -0700132 * usb_hcd_at91_probe - initialize AT91-based HCDs
Andrew Victor39a269c2006-01-22 10:32:13 -0800133 * Context: !in_interrupt()
134 *
135 * Allocates basic resources for this USB host controller, and
136 * then invokes the start() method for the HCD associated with it
137 * through the hotplug entry's driver_data.
Andrew Victor39a269c2006-01-22 10:32:13 -0800138 */
Bill Pemberton41ac7b32012-11-19 13:21:48 -0500139static int usb_hcd_at91_probe(const struct hc_driver *driver,
David Brownell0365ee02006-06-19 14:27:20 -0700140 struct platform_device *pdev)
Andrew Victor39a269c2006-01-22 10:32:13 -0800141{
Manjunath Goudare3825b42013-09-21 16:38:42 +0530142 struct at91_usbh_data *board;
143 struct ohci_hcd *ohci;
Andrew Victor39a269c2006-01-22 10:32:13 -0800144 int retval;
Sylvain Rochet3b3394a2015-01-20 14:39:03 +0100145 struct usb_hcd *hcd;
Sylvain Rochet1b207452015-01-20 14:39:02 +0100146 struct ohci_at91_priv *ohci_at91;
Boris BREZILLONfb5f1832013-12-08 15:59:59 +0100147 struct device *dev = &pdev->dev;
148 struct resource *res;
149 int irq;
Andrew Victor39a269c2006-01-22 10:32:13 -0800150
Boris BREZILLONfb5f1832013-12-08 15:59:59 +0100151 irq = platform_get_irq(pdev, 0);
152 if (irq < 0) {
153 dev_dbg(dev, "hcd probe: missing irq resource\n");
154 return irq;
Andrew Victor39a269c2006-01-22 10:32:13 -0800155 }
156
Boris BREZILLON5b218a02013-12-09 09:51:54 +0100157 hcd = usb_create_hcd(driver, dev, "at91");
Andrew Victor39a269c2006-01-22 10:32:13 -0800158 if (!hcd)
159 return -ENOMEM;
Sylvain Rochet1b207452015-01-20 14:39:02 +0100160 ohci_at91 = hcd_to_ohci_at91_priv(hcd);
Andrew Victor39a269c2006-01-22 10:32:13 -0800161
Varka Bhadram04dc3152014-11-04 07:51:12 +0530162 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Boris BREZILLONcca2bbb2013-12-09 09:51:53 +0100163 hcd->regs = devm_ioremap_resource(dev, res);
164 if (IS_ERR(hcd->regs)) {
165 retval = PTR_ERR(hcd->regs);
166 goto err;
Andrew Victor39a269c2006-01-22 10:32:13 -0800167 }
Varka Bhadram04dc3152014-11-04 07:51:12 +0530168 hcd->rsrc_start = res->start;
169 hcd->rsrc_len = resource_size(res);
Andrew Victor39a269c2006-01-22 10:32:13 -0800170
Sylvain Rochet1b207452015-01-20 14:39:02 +0100171 ohci_at91->iclk = devm_clk_get(dev, "ohci_clk");
172 if (IS_ERR(ohci_at91->iclk)) {
Boris BREZILLON5b218a02013-12-09 09:51:54 +0100173 dev_err(dev, "failed to get ohci_clk\n");
Sylvain Rochet1b207452015-01-20 14:39:02 +0100174 retval = PTR_ERR(ohci_at91->iclk);
Boris BREZILLONcca2bbb2013-12-09 09:51:53 +0100175 goto err;
Jean-Christophe PLAGNIOL-VILLARD68134632011-12-08 16:32:00 +0100176 }
Sylvain Rochet1b207452015-01-20 14:39:02 +0100177 ohci_at91->fclk = devm_clk_get(dev, "uhpck");
178 if (IS_ERR(ohci_at91->fclk)) {
Boris BREZILLON5b218a02013-12-09 09:51:54 +0100179 dev_err(dev, "failed to get uhpck\n");
Sylvain Rochet1b207452015-01-20 14:39:02 +0100180 retval = PTR_ERR(ohci_at91->fclk);
Boris BREZILLONfc7a3252013-12-09 09:51:55 +0100181 goto err;
Jean-Christophe PLAGNIOL-VILLARD68134632011-12-08 16:32:00 +0100182 }
Sylvain Rochet1b207452015-01-20 14:39:02 +0100183 ohci_at91->hclk = devm_clk_get(dev, "hclk");
184 if (IS_ERR(ohci_at91->hclk)) {
Boris BREZILLON5b218a02013-12-09 09:51:54 +0100185 dev_err(dev, "failed to get hclk\n");
Sylvain Rochet1b207452015-01-20 14:39:02 +0100186 retval = PTR_ERR(ohci_at91->hclk);
Boris BREZILLONfc7a3252013-12-09 09:51:55 +0100187 goto err;
Jean-Christophe PLAGNIOL-VILLARD68134632011-12-08 16:32:00 +0100188 }
Boris BREZILLON6b0a1cf2013-08-01 19:09:13 +0200189 if (IS_ENABLED(CONFIG_COMMON_CLK)) {
Sylvain Rochet1b207452015-01-20 14:39:02 +0100190 ohci_at91->uclk = devm_clk_get(dev, "usb_clk");
191 if (IS_ERR(ohci_at91->uclk)) {
Boris BREZILLON5b218a02013-12-09 09:51:54 +0100192 dev_err(dev, "failed to get uclk\n");
Sylvain Rochet1b207452015-01-20 14:39:02 +0100193 retval = PTR_ERR(ohci_at91->uclk);
Boris BREZILLONfc7a3252013-12-09 09:51:55 +0100194 goto err;
Boris BREZILLON6b0a1cf2013-08-01 19:09:13 +0200195 }
196 }
Andrew Victor39a269c2006-01-22 10:32:13 -0800197
Manjunath Goudare3825b42013-09-21 16:38:42 +0530198 board = hcd->self.controller->platform_data;
199 ohci = hcd_to_ohci(hcd);
200 ohci->num_ports = board->ports;
Andrew Victor39a269c2006-01-22 10:32:13 -0800201 at91_start_hc(pdev);
Andrew Victor39a269c2006-01-22 10:32:13 -0800202
Boris BREZILLONfb5f1832013-12-08 15:59:59 +0100203 retval = usb_add_hcd(hcd, irq, IRQF_SHARED);
Arnd Bergmann1f53b482014-01-07 12:54:58 +0100204 if (retval == 0) {
Peter Chen3c9740a2013-11-05 10:46:02 +0800205 device_wakeup_enable(hcd->self.controller);
Andrew Victor39a269c2006-01-22 10:32:13 -0800206 return retval;
Peter Chen3c9740a2013-11-05 10:46:02 +0800207 }
Andrew Victor39a269c2006-01-22 10:32:13 -0800208
209 /* Error handling */
210 at91_stop_hc(pdev);
211
Boris BREZILLONcca2bbb2013-12-09 09:51:53 +0100212 err:
Andrew Victor39a269c2006-01-22 10:32:13 -0800213 usb_put_hcd(hcd);
214 return retval;
215}
216
217
Andrew Victor39a269c2006-01-22 10:32:13 -0800218/* may be called with controller, bus, and devices active */
219
220/**
David Brownell0365ee02006-06-19 14:27:20 -0700221 * usb_hcd_at91_remove - shutdown processing for AT91-based HCDs
Andrew Victor39a269c2006-01-22 10:32:13 -0800222 * @dev: USB Host Controller being removed
223 * Context: !in_interrupt()
224 *
225 * Reverses the effect of usb_hcd_at91_probe(), first invoking
226 * the HCD's stop() method. It is always called from a thread
David Brownell0365ee02006-06-19 14:27:20 -0700227 * context, "rmmod" or something similar.
Andrew Victor39a269c2006-01-22 10:32:13 -0800228 *
229 */
Bill Pembertonfb4e98a2012-11-19 13:26:20 -0500230static void usb_hcd_at91_remove(struct usb_hcd *hcd,
David Brownell0365ee02006-06-19 14:27:20 -0700231 struct platform_device *pdev)
Andrew Victor39a269c2006-01-22 10:32:13 -0800232{
233 usb_remove_hcd(hcd);
234 at91_stop_hc(pdev);
Pete Zaitcev421b4bf2008-06-01 14:38:43 -0700235 usb_put_hcd(hcd);
Andrew Victor39a269c2006-01-22 10:32:13 -0800236}
237
238/*-------------------------------------------------------------------------*/
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200239static void ohci_at91_usb_set_power(struct at91_usbh_data *pdata, int port, int enable)
240{
Nicolas Ferre0ee6d1e2012-03-21 16:53:09 +0100241 if (!valid_port(port))
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200242 return;
243
Jean-Christophe PLAGNIOL-VILLARD8a7a49d2011-09-19 15:32:23 +0800244 if (!gpio_is_valid(pdata->vbus_pin[port]))
Jean-Christophe PLAGNIOL-VILLARD770f0ba2011-11-12 16:46:13 +0100245 return;
246
Jean-Christophe PLAGNIOL-VILLARD8134ff52011-12-08 16:32:01 +0100247 gpio_set_value(pdata->vbus_pin[port],
Nicolas Ferre1e7caf82012-03-21 14:38:55 +0100248 pdata->vbus_pin_active_low[port] ^ enable);
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200249}
250
251static int ohci_at91_usb_get_power(struct at91_usbh_data *pdata, int port)
252{
Nicolas Ferre0ee6d1e2012-03-21 16:53:09 +0100253 if (!valid_port(port))
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200254 return -EINVAL;
255
Jean-Christophe PLAGNIOL-VILLARD8a7a49d2011-09-19 15:32:23 +0800256 if (!gpio_is_valid(pdata->vbus_pin[port]))
Jean-Christophe PLAGNIOL-VILLARD770f0ba2011-11-12 16:46:13 +0100257 return -EINVAL;
258
Jean-Christophe PLAGNIOL-VILLARD8134ff52011-12-08 16:32:01 +0100259 return gpio_get_value(pdata->vbus_pin[port]) ^
Nicolas Ferre1e7caf82012-03-21 14:38:55 +0100260 pdata->vbus_pin_active_low[port];
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200261}
262
263/*
264 * Update the status data from the hub with the over-current indicator change.
265 */
266static int ohci_at91_hub_status_data(struct usb_hcd *hcd, char *buf)
267{
Manjunath Goudare3825b42013-09-21 16:38:42 +0530268 struct at91_usbh_data *pdata = hcd->self.controller->platform_data;
Laurent Pinchart42b59eb2014-04-16 18:00:09 +0200269 int length = ohci_hub_status_data(hcd, buf);
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200270 int port;
271
Nicolas Ferre0ee6d1e2012-03-21 16:53:09 +0100272 at91_for_each_port(port) {
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200273 if (pdata->overcurrent_changed[port]) {
Nicolas Ferre0ee6d1e2012-03-21 16:53:09 +0100274 if (!length)
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200275 length = 1;
276 buf[0] |= 1 << (port + 1);
277 }
278 }
279
280 return length;
281}
282
283/*
284 * Look at the control requests to the root hub and see if we need to override.
285 */
286static int ohci_at91_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
287 u16 wIndex, char *buf, u16 wLength)
288{
Jingoo Hand4f09e22013-07-30 19:59:40 +0900289 struct at91_usbh_data *pdata = dev_get_platdata(hcd->self.controller);
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200290 struct usb_hub_descriptor *desc;
291 int ret = -EINVAL;
292 u32 *data = (u32 *)buf;
293
294 dev_dbg(hcd->self.controller,
295 "ohci_at91_hub_control(%p,0x%04x,0x%04x,0x%04x,%p,%04x)\n",
296 hcd, typeReq, wValue, wIndex, buf, wLength);
297
Nicolas Ferre0ee6d1e2012-03-21 16:53:09 +0100298 wIndex--;
299
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200300 switch (typeReq) {
301 case SetPortFeature:
302 if (wValue == USB_PORT_FEAT_POWER) {
303 dev_dbg(hcd->self.controller, "SetPortFeat: POWER\n");
Nicolas Ferre0ee6d1e2012-03-21 16:53:09 +0100304 if (valid_port(wIndex)) {
305 ohci_at91_usb_set_power(pdata, wIndex, 1);
306 ret = 0;
307 }
308
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200309 goto out;
310 }
311 break;
312
313 case ClearPortFeature:
314 switch (wValue) {
315 case USB_PORT_FEAT_C_OVER_CURRENT:
316 dev_dbg(hcd->self.controller,
317 "ClearPortFeature: C_OVER_CURRENT\n");
318
Nicolas Ferre0ee6d1e2012-03-21 16:53:09 +0100319 if (valid_port(wIndex)) {
320 pdata->overcurrent_changed[wIndex] = 0;
321 pdata->overcurrent_status[wIndex] = 0;
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200322 }
323
324 goto out;
325
326 case USB_PORT_FEAT_OVER_CURRENT:
327 dev_dbg(hcd->self.controller,
328 "ClearPortFeature: OVER_CURRENT\n");
329
Nicolas Ferre0ee6d1e2012-03-21 16:53:09 +0100330 if (valid_port(wIndex))
331 pdata->overcurrent_status[wIndex] = 0;
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200332
333 goto out;
334
335 case USB_PORT_FEAT_POWER:
336 dev_dbg(hcd->self.controller,
337 "ClearPortFeature: POWER\n");
338
Nicolas Ferre0ee6d1e2012-03-21 16:53:09 +0100339 if (valid_port(wIndex)) {
340 ohci_at91_usb_set_power(pdata, wIndex, 0);
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200341 return 0;
342 }
343 }
344 break;
345 }
346
Laurent Pinchart42b59eb2014-04-16 18:00:09 +0200347 ret = ohci_hub_control(hcd, typeReq, wValue, wIndex + 1, buf, wLength);
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200348 if (ret)
349 goto out;
350
351 switch (typeReq) {
352 case GetHubDescriptor:
353
354 /* update the hub's descriptor */
355
356 desc = (struct usb_hub_descriptor *)buf;
357
358 dev_dbg(hcd->self.controller, "wHubCharacteristics 0x%04x\n",
359 desc->wHubCharacteristics);
360
361 /* remove the old configurations for power-switching, and
362 * over-current protection, and insert our new configuration
363 */
364
365 desc->wHubCharacteristics &= ~cpu_to_le16(HUB_CHAR_LPSM);
Sergei Shtylyova9c49bc2015-01-19 01:39:44 +0300366 desc->wHubCharacteristics |=
367 cpu_to_le16(HUB_CHAR_INDV_PORT_LPSM);
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200368
369 if (pdata->overcurrent_supported) {
370 desc->wHubCharacteristics &= ~cpu_to_le16(HUB_CHAR_OCPM);
Sergei Shtylyova9c49bc2015-01-19 01:39:44 +0300371 desc->wHubCharacteristics |=
372 cpu_to_le16(HUB_CHAR_INDV_PORT_OCPM);
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200373 }
374
375 dev_dbg(hcd->self.controller, "wHubCharacteristics after 0x%04x\n",
376 desc->wHubCharacteristics);
377
378 return ret;
379
380 case GetPortStatus:
381 /* check port status */
382
383 dev_dbg(hcd->self.controller, "GetPortStatus(%d)\n", wIndex);
384
Nicolas Ferre0ee6d1e2012-03-21 16:53:09 +0100385 if (valid_port(wIndex)) {
386 if (!ohci_at91_usb_get_power(pdata, wIndex))
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200387 *data &= ~cpu_to_le32(RH_PS_PPS);
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200388
Nicolas Ferre0ee6d1e2012-03-21 16:53:09 +0100389 if (pdata->overcurrent_changed[wIndex])
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200390 *data |= cpu_to_le32(RH_PS_OCIC);
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200391
Nicolas Ferre0ee6d1e2012-03-21 16:53:09 +0100392 if (pdata->overcurrent_status[wIndex])
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200393 *data |= cpu_to_le32(RH_PS_POCI);
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200394 }
395 }
396
397 out:
398 return ret;
399}
400
Andrew Victor39a269c2006-01-22 10:32:13 -0800401/*-------------------------------------------------------------------------*/
402
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200403static irqreturn_t ohci_hcd_at91_overcurrent_irq(int irq, void *data)
404{
405 struct platform_device *pdev = data;
Jingoo Hand4f09e22013-07-30 19:59:40 +0900406 struct at91_usbh_data *pdata = dev_get_platdata(&pdev->dev);
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200407 int val, gpio, port;
408
409 /* From the GPIO notifying the over-current situation, find
410 * out the corresponding port */
Nicolas Ferre0ee6d1e2012-03-21 16:53:09 +0100411 at91_for_each_port(port) {
Joachim Eastwood01bb6502012-09-23 22:56:00 +0200412 if (gpio_is_valid(pdata->overcurrent_pin[port]) &&
413 gpio_to_irq(pdata->overcurrent_pin[port]) == irq) {
Nicolas Ferree4499072012-02-11 14:23:06 +0100414 gpio = pdata->overcurrent_pin[port];
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200415 break;
Nicolas Ferree4499072012-02-11 14:23:06 +0100416 }
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200417 }
418
Nicolas Ferre0ee6d1e2012-03-21 16:53:09 +0100419 if (port == AT91_MAX_USBH_PORTS) {
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200420 dev_err(& pdev->dev, "overcurrent interrupt from unknown GPIO\n");
421 return IRQ_HANDLED;
422 }
423
424 val = gpio_get_value(gpio);
425
426 /* When notified of an over-current situation, disable power
427 on the corresponding port, and mark this port in
428 over-current. */
Nicolas Ferre0ee6d1e2012-03-21 16:53:09 +0100429 if (!val) {
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200430 ohci_at91_usb_set_power(pdata, port, 0);
431 pdata->overcurrent_status[port] = 1;
432 pdata->overcurrent_changed[port] = 1;
433 }
434
435 dev_dbg(& pdev->dev, "overcurrent situation %s\n",
436 val ? "exited" : "notified");
437
438 return IRQ_HANDLED;
439}
440
Jean-Christophe PLAGNIOL-VILLARD24197302011-11-21 06:55:18 +0800441#ifdef CONFIG_OF
442static const struct of_device_id at91_ohci_dt_ids[] = {
443 { .compatible = "atmel,at91rm9200-ohci" },
444 { /* sentinel */ }
445};
446
447MODULE_DEVICE_TABLE(of, at91_ohci_dt_ids);
448
Bill Pemberton41ac7b32012-11-19 13:21:48 -0500449static int ohci_at91_of_init(struct platform_device *pdev)
Jean-Christophe PLAGNIOL-VILLARD24197302011-11-21 06:55:18 +0800450{
451 struct device_node *np = pdev->dev.of_node;
Russell King22d9d8e2013-06-10 16:28:49 +0100452 int i, gpio, ret;
Jean-Christophe PLAGNIOL-VILLARD24197302011-11-21 06:55:18 +0800453 enum of_gpio_flags flags;
454 struct at91_usbh_data *pdata;
455 u32 ports;
456
457 if (!np)
458 return 0;
459
460 /* Right now device-tree probed devices don't get dma_mask set.
461 * Since shared usb code relies on it, set it here for now.
462 * Once we have dma capability bindings this can go away.
463 */
Russell Kinge1fd7342013-06-27 12:36:37 +0100464 ret = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
Russell King22d9d8e2013-06-10 16:28:49 +0100465 if (ret)
466 return ret;
Jean-Christophe PLAGNIOL-VILLARD24197302011-11-21 06:55:18 +0800467
468 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
469 if (!pdata)
470 return -ENOMEM;
471
472 if (!of_property_read_u32(np, "num-ports", &ports))
473 pdata->ports = ports;
474
Nicolas Ferre0ee6d1e2012-03-21 16:53:09 +0100475 at91_for_each_port(i) {
Jean-Christophe PLAGNIOL-VILLARD24197302011-11-21 06:55:18 +0800476 gpio = of_get_named_gpio_flags(np, "atmel,vbus-gpio", i, &flags);
477 pdata->vbus_pin[i] = gpio;
478 if (!gpio_is_valid(gpio))
479 continue;
480 pdata->vbus_pin_active_low[i] = flags & OF_GPIO_ACTIVE_LOW;
Jean-Christophe PLAGNIOL-VILLARD24197302011-11-21 06:55:18 +0800481 }
482
Nicolas Ferre0ee6d1e2012-03-21 16:53:09 +0100483 at91_for_each_port(i)
Nicolas Ferreaaf9f5f2012-03-21 16:58:11 +0100484 pdata->overcurrent_pin[i] =
485 of_get_named_gpio_flags(np, "atmel,oc-gpio", i, &flags);
Jean-Christophe PLAGNIOL-VILLARD24197302011-11-21 06:55:18 +0800486
487 pdev->dev.platform_data = pdata;
488
489 return 0;
490}
491#else
Bill Pemberton41ac7b32012-11-19 13:21:48 -0500492static int ohci_at91_of_init(struct platform_device *pdev)
Jean-Christophe PLAGNIOL-VILLARD24197302011-11-21 06:55:18 +0800493{
494 return 0;
495}
496#endif
497
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200498/*-------------------------------------------------------------------------*/
499
Bill Pemberton41ac7b32012-11-19 13:21:48 -0500500static int ohci_hcd_at91_drv_probe(struct platform_device *pdev)
Andrew Victor39a269c2006-01-22 10:32:13 -0800501{
Jean-Christophe PLAGNIOL-VILLARD24197302011-11-21 06:55:18 +0800502 struct at91_usbh_data *pdata;
David Brownell4bde4a42007-12-27 11:19:49 -0800503 int i;
Nicolas Ferreaaf9f5f2012-03-21 16:58:11 +0100504 int gpio;
505 int ret;
David Brownell4bde4a42007-12-27 11:19:49 -0800506
Nicolas Ferre1887ab2b2012-03-28 11:49:01 +0200507 ret = ohci_at91_of_init(pdev);
508 if (ret)
509 return ret;
Jean-Christophe PLAGNIOL-VILLARD24197302011-11-21 06:55:18 +0800510
Jingoo Hand4f09e22013-07-30 19:59:40 +0900511 pdata = dev_get_platdata(&pdev->dev);
Jean-Christophe PLAGNIOL-VILLARD24197302011-11-21 06:55:18 +0800512
David Brownell4bde4a42007-12-27 11:19:49 -0800513 if (pdata) {
Nicolas Ferre0ee6d1e2012-03-21 16:53:09 +0100514 at91_for_each_port(i) {
Nicolas Ferre6fffb772012-08-29 11:49:18 +0200515 /*
516 * do not configure PIO if not in relation with
517 * real USB port on board
518 */
519 if (i >= pdata->ports) {
520 pdata->vbus_pin[i] = -EINVAL;
521 pdata->overcurrent_pin[i] = -EINVAL;
522 break;
523 }
524
Jean-Christophe PLAGNIOL-VILLARD8a7a49d2011-09-19 15:32:23 +0800525 if (!gpio_is_valid(pdata->vbus_pin[i]))
David Brownell4bde4a42007-12-27 11:19:49 -0800526 continue;
Nicolas Ferreaaf9f5f2012-03-21 16:58:11 +0100527 gpio = pdata->vbus_pin[i];
528
529 ret = gpio_request(gpio, "ohci_vbus");
530 if (ret) {
531 dev_err(&pdev->dev,
532 "can't request vbus gpio %d\n", gpio);
533 continue;
534 }
535 ret = gpio_direction_output(gpio,
536 !pdata->vbus_pin_active_low[i]);
537 if (ret) {
538 dev_err(&pdev->dev,
539 "can't put vbus gpio %d as output %d\n",
540 gpio, !pdata->vbus_pin_active_low[i]);
541 gpio_free(gpio);
542 continue;
543 }
544
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200545 ohci_at91_usb_set_power(pdata, i, 1);
546 }
547
Nicolas Ferre0ee6d1e2012-03-21 16:53:09 +0100548 at91_for_each_port(i) {
Jean-Christophe PLAGNIOL-VILLARD8a7a49d2011-09-19 15:32:23 +0800549 if (!gpio_is_valid(pdata->overcurrent_pin[i]))
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200550 continue;
Nicolas Ferreaaf9f5f2012-03-21 16:58:11 +0100551 gpio = pdata->overcurrent_pin[i];
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200552
Nicolas Ferreaaf9f5f2012-03-21 16:58:11 +0100553 ret = gpio_request(gpio, "ohci_overcurrent");
554 if (ret) {
555 dev_err(&pdev->dev,
556 "can't request overcurrent gpio %d\n",
557 gpio);
558 continue;
559 }
560
561 ret = gpio_direction_input(gpio);
562 if (ret) {
563 dev_err(&pdev->dev,
564 "can't configure overcurrent gpio %d as input\n",
565 gpio);
566 gpio_free(gpio);
567 continue;
568 }
569
570 ret = request_irq(gpio_to_irq(gpio),
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200571 ohci_hcd_at91_overcurrent_irq,
572 IRQF_SHARED, "ohci_overcurrent", pdev);
573 if (ret) {
Nicolas Ferreaaf9f5f2012-03-21 16:58:11 +0100574 gpio_free(gpio);
575 dev_err(&pdev->dev,
576 "can't get gpio IRQ for overcurrent\n");
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200577 }
David Brownell4bde4a42007-12-27 11:19:49 -0800578 }
579 }
580
David Brownell0365ee02006-06-19 14:27:20 -0700581 device_init_wakeup(&pdev->dev, 1);
582 return usb_hcd_at91_probe(&ohci_at91_hc_driver, pdev);
Andrew Victor39a269c2006-01-22 10:32:13 -0800583}
584
Bill Pembertonfb4e98a2012-11-19 13:26:20 -0500585static int ohci_hcd_at91_drv_remove(struct platform_device *pdev)
Andrew Victor39a269c2006-01-22 10:32:13 -0800586{
Jingoo Hand4f09e22013-07-30 19:59:40 +0900587 struct at91_usbh_data *pdata = dev_get_platdata(&pdev->dev);
David Brownell4bde4a42007-12-27 11:19:49 -0800588 int i;
589
590 if (pdata) {
Nicolas Ferre0ee6d1e2012-03-21 16:53:09 +0100591 at91_for_each_port(i) {
Jean-Christophe PLAGNIOL-VILLARD8a7a49d2011-09-19 15:32:23 +0800592 if (!gpio_is_valid(pdata->vbus_pin[i]))
David Brownell4bde4a42007-12-27 11:19:49 -0800593 continue;
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200594 ohci_at91_usb_set_power(pdata, i, 0);
David Brownell4bde4a42007-12-27 11:19:49 -0800595 gpio_free(pdata->vbus_pin[i]);
596 }
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200597
Nicolas Ferre0ee6d1e2012-03-21 16:53:09 +0100598 at91_for_each_port(i) {
Jean-Christophe PLAGNIOL-VILLARD8a7a49d2011-09-19 15:32:23 +0800599 if (!gpio_is_valid(pdata->overcurrent_pin[i]))
Thomas Petazzoniaa6e52a2011-07-13 11:29:17 +0200600 continue;
601 free_irq(gpio_to_irq(pdata->overcurrent_pin[i]), pdev);
602 gpio_free(pdata->overcurrent_pin[i]);
603 }
David Brownell4bde4a42007-12-27 11:19:49 -0800604 }
605
David Brownell0365ee02006-06-19 14:27:20 -0700606 device_init_wakeup(&pdev->dev, 0);
Pete Zaitcev421b4bf2008-06-01 14:38:43 -0700607 usb_hcd_at91_remove(platform_get_drvdata(pdev), pdev);
608 return 0;
Andrew Victor39a269c2006-01-22 10:32:13 -0800609}
610
611#ifdef CONFIG_PM
Andrew Victor39a269c2006-01-22 10:32:13 -0800612
David Brownell68ba61b2006-04-02 20:26:21 -0800613static int
Sylvain Rochet94ac0e52015-01-20 14:39:00 +0100614ohci_hcd_at91_drv_suspend(struct device *dev)
David Brownell68ba61b2006-04-02 20:26:21 -0800615{
Sylvain Rochet94ac0e52015-01-20 14:39:00 +0100616 struct usb_hcd *hcd = dev_get_drvdata(dev);
David Brownell0365ee02006-06-19 14:27:20 -0700617 struct ohci_hcd *ohci = hcd_to_ohci(hcd);
Sylvain Rochet1b207452015-01-20 14:39:02 +0100618 struct ohci_at91_priv *ohci_at91 = hcd_to_ohci_at91_priv(hcd);
Sylvain Rochet94ac0e52015-01-20 14:39:00 +0100619 bool do_wakeup = device_may_wakeup(dev);
Majunath Goudara9d38402013-11-13 17:40:17 +0530620 int ret;
David Brownell0365ee02006-06-19 14:27:20 -0700621
Majunath Goudara9d38402013-11-13 17:40:17 +0530622 if (do_wakeup)
David Brownell0365ee02006-06-19 14:27:20 -0700623 enable_irq_wake(hcd->irq);
David Brownell0365ee02006-06-19 14:27:20 -0700624
Majunath Goudara9d38402013-11-13 17:40:17 +0530625 ret = ohci_suspend(hcd, do_wakeup);
626 if (ret) {
627 disable_irq_wake(hcd->irq);
628 return ret;
629 }
David Brownell0365ee02006-06-19 14:27:20 -0700630 /*
631 * The integrated transceivers seem unable to notice disconnect,
632 * reconnect, or wakeup without the 48 MHz clock active. so for
633 * correctness, always discard connection state (using reset).
634 *
635 * REVISIT: some boards will be able to turn VBUS off...
636 */
637 if (at91_suspend_entering_slow_clock()) {
Manjunath Goudare3825b42013-09-21 16:38:42 +0530638 ohci->hc_control = ohci_readl(ohci, &ohci->regs->control);
639 ohci->hc_control &= OHCI_CTRL_RWC;
640 ohci_writel(ohci, ohci->hc_control, &ohci->regs->control);
641 ohci->rh_state = OHCI_RH_HALTED;
642
Patrice Vilchez869aa982010-04-28 13:45:40 +0200643 /* flush the writes */
644 (void) ohci_readl (ohci, &ohci->regs->control);
Sylvain Rochet1b207452015-01-20 14:39:02 +0100645 at91_stop_clock(ohci_at91);
David Brownell0365ee02006-06-19 14:27:20 -0700646 }
Andrew Victor39a269c2006-01-22 10:32:13 -0800647
Majunath Goudara9d38402013-11-13 17:40:17 +0530648 return ret;
Andrew Victor39a269c2006-01-22 10:32:13 -0800649}
650
Sylvain Rochet94ac0e52015-01-20 14:39:00 +0100651static int ohci_hcd_at91_drv_resume(struct device *dev)
Andrew Victor39a269c2006-01-22 10:32:13 -0800652{
Sylvain Rochet94ac0e52015-01-20 14:39:00 +0100653 struct usb_hcd *hcd = dev_get_drvdata(dev);
Sylvain Rochet1b207452015-01-20 14:39:02 +0100654 struct ohci_at91_priv *ohci_at91 = hcd_to_ohci_at91_priv(hcd);
Marc Pignat6dde8962007-01-09 14:00:11 -0800655
Sylvain Rochet94ac0e52015-01-20 14:39:00 +0100656 if (device_may_wakeup(dev))
Marc Pignat6dde8962007-01-09 14:00:11 -0800657 disable_irq_wake(hcd->irq);
658
Sylvain Rochet1b207452015-01-20 14:39:02 +0100659 if (!ohci_at91->clocked)
660 at91_start_clock(ohci_at91);
Andrew Victor39a269c2006-01-22 10:32:13 -0800661
Florian Fainellicfa49b42012-10-08 15:11:29 +0200662 ohci_resume(hcd, false);
Andrew Victor39a269c2006-01-22 10:32:13 -0800663 return 0;
664}
Andrew Victor39a269c2006-01-22 10:32:13 -0800665#endif
666
Sylvain Rochet94ac0e52015-01-20 14:39:00 +0100667static SIMPLE_DEV_PM_OPS(ohci_hcd_at91_pm_ops, ohci_hcd_at91_drv_suspend,
668 ohci_hcd_at91_drv_resume);
669
Andrew Victor39a269c2006-01-22 10:32:13 -0800670static struct platform_driver ohci_hcd_at91_driver = {
671 .probe = ohci_hcd_at91_drv_probe,
Bill Pemberton76904172012-11-19 13:21:08 -0500672 .remove = ohci_hcd_at91_drv_remove,
Aleksey Gorelov64a21d02006-08-08 17:24:08 -0700673 .shutdown = usb_hcd_platform_shutdown,
Andrew Victor39a269c2006-01-22 10:32:13 -0800674 .driver = {
David Brownell0365ee02006-06-19 14:27:20 -0700675 .name = "at91_ohci",
Sylvain Rochet94ac0e52015-01-20 14:39:00 +0100676 .pm = &ohci_hcd_at91_pm_ops,
Jean-Christophe PLAGNIOL-VILLARD24197302011-11-21 06:55:18 +0800677 .of_match_table = of_match_ptr(at91_ohci_dt_ids),
Andrew Victor39a269c2006-01-22 10:32:13 -0800678 },
679};
Manjunath Goudare3825b42013-09-21 16:38:42 +0530680
681static int __init ohci_at91_init(void)
682{
683 if (usb_disabled())
684 return -ENODEV;
685
686 pr_info("%s: " DRIVER_DESC "\n", hcd_name);
Sylvain Rochet1b207452015-01-20 14:39:02 +0100687 ohci_init_driver(&ohci_at91_hc_driver, &ohci_at91_drv_overrides);
Manjunath Goudare3825b42013-09-21 16:38:42 +0530688
689 /*
690 * The Atmel HW has some unusual quirks, which require Atmel-specific
691 * workarounds. We override certain hc_driver functions here to
692 * achieve that. We explicitly do not enhance ohci_driver_overrides to
693 * allow this more easily, since this is an unusual case, and we don't
694 * want to encourage others to override these functions by making it
695 * too easy.
696 */
697
Manjunath Goudare3825b42013-09-21 16:38:42 +0530698 ohci_at91_hc_driver.hub_status_data = ohci_at91_hub_status_data;
699 ohci_at91_hc_driver.hub_control = ohci_at91_hub_control;
700
701 return platform_driver_register(&ohci_hcd_at91_driver);
702}
703module_init(ohci_at91_init);
704
705static void __exit ohci_at91_cleanup(void)
706{
707 platform_driver_unregister(&ohci_hcd_at91_driver);
708}
709module_exit(ohci_at91_cleanup);
710
711MODULE_DESCRIPTION(DRIVER_DESC);
712MODULE_LICENSE("GPL");
713MODULE_ALIAS("platform:at91_ohci");