2 * OHCI HCD (Host Controller Driver) for USB.
4 * Copyright (C) 2004 SAN People (Pty) Ltd.
5 * Copyright (C) 2005 Thibaut VARENE <varenet@parisc-linux.org>
9 * Based on fragments of 2.4 driver by Rick Bronson.
10 * Based on ohci-omap.c
12 * This file is licenced under the GPL.
15 #include <linux/clk.h>
16 #include <linux/platform_device.h>
18 #include <mach/hardware.h>
21 #include <mach/board.h>
24 #ifndef CONFIG_ARCH_AT91
25 #error "CONFIG_ARCH_AT91 must be defined."
28 /* interface and function clocks; sometimes also an AHB clock */
29 static struct clk
*iclk
, *fclk
, *hclk
;
32 extern int usb_disabled(void);
34 /*-------------------------------------------------------------------------*/
36 static void at91_start_clock(void)
44 static void at91_stop_clock(void)
52 static void at91_start_hc(struct platform_device
*pdev
)
54 struct usb_hcd
*hcd
= platform_get_drvdata(pdev
);
55 struct ohci_regs __iomem
*regs
= hcd
->regs
;
57 dev_dbg(&pdev
->dev
, "start\n");
60 * Start the USB clocks.
65 * The USB host controller must remain in reset.
67 writel(0, ®s
->control
);
70 static void at91_stop_hc(struct platform_device
*pdev
)
72 struct usb_hcd
*hcd
= platform_get_drvdata(pdev
);
73 struct ohci_regs __iomem
*regs
= hcd
->regs
;
75 dev_dbg(&pdev
->dev
, "stop\n");
78 * Put the USB host controller into reset.
80 writel(0, ®s
->control
);
83 * Stop the USB clocks.
89 /*-------------------------------------------------------------------------*/
91 static void usb_hcd_at91_remove (struct usb_hcd
*, struct platform_device
*);
93 /* configure so an HC device and id are always provided */
94 /* always called with process context; sleeping is OK */
98 * usb_hcd_at91_probe - initialize AT91-based HCDs
99 * Context: !in_interrupt()
101 * Allocates basic resources for this USB host controller, and
102 * then invokes the start() method for the HCD associated with it
103 * through the hotplug entry's driver_data.
105 static int usb_hcd_at91_probe(const struct hc_driver
*driver
,
106 struct platform_device
*pdev
)
109 struct usb_hcd
*hcd
= NULL
;
111 if (pdev
->num_resources
!= 2) {
112 pr_debug("hcd probe: invalid num_resources");
116 if ((pdev
->resource
[0].flags
!= IORESOURCE_MEM
)
117 || (pdev
->resource
[1].flags
!= IORESOURCE_IRQ
)) {
118 pr_debug("hcd probe: invalid resource type\n");
122 hcd
= usb_create_hcd(driver
, &pdev
->dev
, "at91");
125 hcd
->rsrc_start
= pdev
->resource
[0].start
;
126 hcd
->rsrc_len
= pdev
->resource
[0].end
- pdev
->resource
[0].start
+ 1;
128 if (!request_mem_region(hcd
->rsrc_start
, hcd
->rsrc_len
, hcd_name
)) {
129 pr_debug("request_mem_region failed\n");
134 hcd
->regs
= ioremap(hcd
->rsrc_start
, hcd
->rsrc_len
);
136 pr_debug("ioremap failed\n");
141 iclk
= clk_get(&pdev
->dev
, "ohci_clk");
143 dev_err(&pdev
->dev
, "failed to get ohci_clk\n");
144 retval
= PTR_ERR(iclk
);
147 fclk
= clk_get(&pdev
->dev
, "uhpck");
149 dev_err(&pdev
->dev
, "failed to get uhpck\n");
150 retval
= PTR_ERR(fclk
);
153 hclk
= clk_get(&pdev
->dev
, "hclk");
155 dev_err(&pdev
->dev
, "failed to get hclk\n");
156 retval
= PTR_ERR(hclk
);
161 ohci_hcd_init(hcd_to_ohci(hcd
));
163 retval
= usb_add_hcd(hcd
, pdev
->resource
[1].start
, IRQF_SHARED
);
180 release_mem_region(hcd
->rsrc_start
, hcd
->rsrc_len
);
188 /* may be called with controller, bus, and devices active */
191 * usb_hcd_at91_remove - shutdown processing for AT91-based HCDs
192 * @dev: USB Host Controller being removed
193 * Context: !in_interrupt()
195 * Reverses the effect of usb_hcd_at91_probe(), first invoking
196 * the HCD's stop() method. It is always called from a thread
197 * context, "rmmod" or something similar.
200 static void usb_hcd_at91_remove(struct usb_hcd
*hcd
,
201 struct platform_device
*pdev
)
206 release_mem_region(hcd
->rsrc_start
, hcd
->rsrc_len
);
212 fclk
= iclk
= hclk
= NULL
;
214 dev_set_drvdata(&pdev
->dev
, NULL
);
217 /*-------------------------------------------------------------------------*/
220 ohci_at91_start (struct usb_hcd
*hcd
)
222 struct at91_usbh_data
*board
= hcd
->self
.controller
->platform_data
;
223 struct ohci_hcd
*ohci
= hcd_to_ohci (hcd
);
226 if ((ret
= ohci_init(ohci
)) < 0)
229 ohci
->num_ports
= board
->ports
;
231 if ((ret
= ohci_run(ohci
)) < 0) {
232 err("can't start %s", hcd
->self
.bus_name
);
239 static void ohci_at91_usb_set_power(struct at91_usbh_data
*pdata
, int port
, int enable
)
241 if (port
< 0 || port
>= 2)
244 if (!gpio_is_valid(pdata
->vbus_pin
[port
]))
247 gpio_set_value(pdata
->vbus_pin
[port
], !pdata
->vbus_pin_inverted
^ enable
);
250 static int ohci_at91_usb_get_power(struct at91_usbh_data
*pdata
, int port
)
252 if (port
< 0 || port
>= 2)
255 if (!gpio_is_valid(pdata
->vbus_pin
[port
]))
258 return gpio_get_value(pdata
->vbus_pin
[port
]) ^ !pdata
->vbus_pin_inverted
;
262 * Update the status data from the hub with the over-current indicator change.
264 static int ohci_at91_hub_status_data(struct usb_hcd
*hcd
, char *buf
)
266 struct at91_usbh_data
*pdata
= hcd
->self
.controller
->platform_data
;
267 int length
= ohci_hub_status_data(hcd
, buf
);
270 for (port
= 0; port
< ARRAY_SIZE(pdata
->overcurrent_pin
); port
++) {
271 if (pdata
->overcurrent_changed
[port
]) {
274 buf
[0] |= 1 << (port
+ 1);
282 * Look at the control requests to the root hub and see if we need to override.
284 static int ohci_at91_hub_control(struct usb_hcd
*hcd
, u16 typeReq
, u16 wValue
,
285 u16 wIndex
, char *buf
, u16 wLength
)
287 struct at91_usbh_data
*pdata
= hcd
->self
.controller
->platform_data
;
288 struct usb_hub_descriptor
*desc
;
290 u32
*data
= (u32
*)buf
;
292 dev_dbg(hcd
->self
.controller
,
293 "ohci_at91_hub_control(%p,0x%04x,0x%04x,0x%04x,%p,%04x)\n",
294 hcd
, typeReq
, wValue
, wIndex
, buf
, wLength
);
298 if (wValue
== USB_PORT_FEAT_POWER
) {
299 dev_dbg(hcd
->self
.controller
, "SetPortFeat: POWER\n");
300 ohci_at91_usb_set_power(pdata
, wIndex
- 1, 1);
305 case ClearPortFeature
:
307 case USB_PORT_FEAT_C_OVER_CURRENT
:
308 dev_dbg(hcd
->self
.controller
,
309 "ClearPortFeature: C_OVER_CURRENT\n");
311 if (wIndex
== 1 || wIndex
== 2) {
312 pdata
->overcurrent_changed
[wIndex
-1] = 0;
313 pdata
->overcurrent_status
[wIndex
-1] = 0;
318 case USB_PORT_FEAT_OVER_CURRENT
:
319 dev_dbg(hcd
->self
.controller
,
320 "ClearPortFeature: OVER_CURRENT\n");
322 if (wIndex
== 1 || wIndex
== 2) {
323 pdata
->overcurrent_status
[wIndex
-1] = 0;
328 case USB_PORT_FEAT_POWER
:
329 dev_dbg(hcd
->self
.controller
,
330 "ClearPortFeature: POWER\n");
332 if (wIndex
== 1 || wIndex
== 2) {
333 ohci_at91_usb_set_power(pdata
, wIndex
- 1, 0);
340 ret
= ohci_hub_control(hcd
, typeReq
, wValue
, wIndex
, buf
, wLength
);
345 case GetHubDescriptor
:
347 /* update the hub's descriptor */
349 desc
= (struct usb_hub_descriptor
*)buf
;
351 dev_dbg(hcd
->self
.controller
, "wHubCharacteristics 0x%04x\n",
352 desc
->wHubCharacteristics
);
354 /* remove the old configurations for power-switching, and
355 * over-current protection, and insert our new configuration
358 desc
->wHubCharacteristics
&= ~cpu_to_le16(HUB_CHAR_LPSM
);
359 desc
->wHubCharacteristics
|= cpu_to_le16(0x0001);
361 if (pdata
->overcurrent_supported
) {
362 desc
->wHubCharacteristics
&= ~cpu_to_le16(HUB_CHAR_OCPM
);
363 desc
->wHubCharacteristics
|= cpu_to_le16(0x0008|0x0001);
366 dev_dbg(hcd
->self
.controller
, "wHubCharacteristics after 0x%04x\n",
367 desc
->wHubCharacteristics
);
372 /* check port status */
374 dev_dbg(hcd
->self
.controller
, "GetPortStatus(%d)\n", wIndex
);
376 if (wIndex
== 1 || wIndex
== 2) {
377 if (! ohci_at91_usb_get_power(pdata
, wIndex
-1)) {
378 *data
&= ~cpu_to_le32(RH_PS_PPS
);
381 if (pdata
->overcurrent_changed
[wIndex
-1]) {
382 *data
|= cpu_to_le32(RH_PS_OCIC
);
385 if (pdata
->overcurrent_status
[wIndex
-1]) {
386 *data
|= cpu_to_le32(RH_PS_POCI
);
395 /*-------------------------------------------------------------------------*/
397 static const struct hc_driver ohci_at91_hc_driver
= {
398 .description
= hcd_name
,
399 .product_desc
= "AT91 OHCI",
400 .hcd_priv_size
= sizeof(struct ohci_hcd
),
403 * generic hardware linkage
406 .flags
= HCD_USB11
| HCD_MEMORY
,
409 * basic lifecycle operations
411 .start
= ohci_at91_start
,
413 .shutdown
= ohci_shutdown
,
416 * managing i/o requests and associated device resources
418 .urb_enqueue
= ohci_urb_enqueue
,
419 .urb_dequeue
= ohci_urb_dequeue
,
420 .endpoint_disable
= ohci_endpoint_disable
,
425 .get_frame_number
= ohci_get_frame
,
430 .hub_status_data
= ohci_at91_hub_status_data
,
431 .hub_control
= ohci_at91_hub_control
,
433 .bus_suspend
= ohci_bus_suspend
,
434 .bus_resume
= ohci_bus_resume
,
436 .start_port_reset
= ohci_start_port_reset
,
439 /*-------------------------------------------------------------------------*/
441 static irqreturn_t
ohci_hcd_at91_overcurrent_irq(int irq
, void *data
)
443 struct platform_device
*pdev
= data
;
444 struct at91_usbh_data
*pdata
= pdev
->dev
.platform_data
;
447 /* From the GPIO notifying the over-current situation, find
448 * out the corresponding port */
449 gpio
= irq_to_gpio(irq
);
450 for (port
= 0; port
< ARRAY_SIZE(pdata
->overcurrent_pin
); port
++) {
451 if (pdata
->overcurrent_pin
[port
] == gpio
)
455 if (port
== ARRAY_SIZE(pdata
->overcurrent_pin
)) {
456 dev_err(& pdev
->dev
, "overcurrent interrupt from unknown GPIO\n");
460 val
= gpio_get_value(gpio
);
462 /* When notified of an over-current situation, disable power
463 on the corresponding port, and mark this port in
466 ohci_at91_usb_set_power(pdata
, port
, 0);
467 pdata
->overcurrent_status
[port
] = 1;
468 pdata
->overcurrent_changed
[port
] = 1;
471 dev_dbg(& pdev
->dev
, "overcurrent situation %s\n",
472 val
? "exited" : "notified");
477 /*-------------------------------------------------------------------------*/
479 static int ohci_hcd_at91_drv_probe(struct platform_device
*pdev
)
481 struct at91_usbh_data
*pdata
= pdev
->dev
.platform_data
;
485 for (i
= 0; i
< ARRAY_SIZE(pdata
->vbus_pin
); i
++) {
486 if (!gpio_is_valid(pdata
->vbus_pin
[i
]))
488 gpio_request(pdata
->vbus_pin
[i
], "ohci_vbus");
489 ohci_at91_usb_set_power(pdata
, i
, 1);
492 for (i
= 0; i
< ARRAY_SIZE(pdata
->overcurrent_pin
); i
++) {
495 if (!gpio_is_valid(pdata
->overcurrent_pin
[i
]))
497 gpio_request(pdata
->overcurrent_pin
[i
], "ohci_overcurrent");
499 ret
= request_irq(gpio_to_irq(pdata
->overcurrent_pin
[i
]),
500 ohci_hcd_at91_overcurrent_irq
,
501 IRQF_SHARED
, "ohci_overcurrent", pdev
);
503 gpio_free(pdata
->overcurrent_pin
[i
]);
504 dev_warn(& pdev
->dev
, "cannot get GPIO IRQ for overcurrent\n");
509 device_init_wakeup(&pdev
->dev
, 1);
510 return usb_hcd_at91_probe(&ohci_at91_hc_driver
, pdev
);
513 static int ohci_hcd_at91_drv_remove(struct platform_device
*pdev
)
515 struct at91_usbh_data
*pdata
= pdev
->dev
.platform_data
;
519 for (i
= 0; i
< ARRAY_SIZE(pdata
->vbus_pin
); i
++) {
520 if (!gpio_is_valid(pdata
->vbus_pin
[i
]))
522 ohci_at91_usb_set_power(pdata
, i
, 0);
523 gpio_free(pdata
->vbus_pin
[i
]);
526 for (i
= 0; i
< ARRAY_SIZE(pdata
->overcurrent_pin
); i
++) {
527 if (!gpio_is_valid(pdata
->overcurrent_pin
[i
]))
529 free_irq(gpio_to_irq(pdata
->overcurrent_pin
[i
]), pdev
);
530 gpio_free(pdata
->overcurrent_pin
[i
]);
534 device_init_wakeup(&pdev
->dev
, 0);
535 usb_hcd_at91_remove(platform_get_drvdata(pdev
), pdev
);
542 ohci_hcd_at91_drv_suspend(struct platform_device
*pdev
, pm_message_t mesg
)
544 struct usb_hcd
*hcd
= platform_get_drvdata(pdev
);
545 struct ohci_hcd
*ohci
= hcd_to_ohci(hcd
);
547 if (device_may_wakeup(&pdev
->dev
))
548 enable_irq_wake(hcd
->irq
);
551 * The integrated transceivers seem unable to notice disconnect,
552 * reconnect, or wakeup without the 48 MHz clock active. so for
553 * correctness, always discard connection state (using reset).
555 * REVISIT: some boards will be able to turn VBUS off...
557 if (at91_suspend_entering_slow_clock()) {
558 ohci_usb_reset (ohci
);
559 /* flush the writes */
560 (void) ohci_readl (ohci
, &ohci
->regs
->control
);
567 static int ohci_hcd_at91_drv_resume(struct platform_device
*pdev
)
569 struct usb_hcd
*hcd
= platform_get_drvdata(pdev
);
571 if (device_may_wakeup(&pdev
->dev
))
572 disable_irq_wake(hcd
->irq
);
577 ohci_finish_controller_resume(hcd
);
581 #define ohci_hcd_at91_drv_suspend NULL
582 #define ohci_hcd_at91_drv_resume NULL
585 MODULE_ALIAS("platform:at91_ohci");
587 static struct platform_driver ohci_hcd_at91_driver
= {
588 .probe
= ohci_hcd_at91_drv_probe
,
589 .remove
= ohci_hcd_at91_drv_remove
,
590 .shutdown
= usb_hcd_platform_shutdown
,
591 .suspend
= ohci_hcd_at91_drv_suspend
,
592 .resume
= ohci_hcd_at91_drv_resume
,
595 .owner
= THIS_MODULE
,