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/dma-mapping.h>
17 #include <linux/of_platform.h>
18 #include <linux/of_gpio.h>
19 #include <linux/platform_device.h>
20 #include <linux/platform_data/atmel.h>
22 #include <linux/kernel.h>
23 #include <linux/module.h>
24 #include <linux/usb.h>
25 #include <linux/usb/hcd.h>
31 #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)++)
35 /* interface, function and usb clocks; sometimes also an AHB clock */
36 #define hcd_to_ohci_at91_priv(h) \
37 ((struct ohci_at91_priv *)hcd_to_ohci(h)->priv)
39 struct ohci_at91_priv {
45 bool wakeup; /* Saved wake-up state for resume */
47 /* interface and function clocks; sometimes also an AHB clock */
49 #define DRIVER_DESC "OHCI Atmel driver"
51 static const char hcd_name[] = "ohci-atmel";
53 static struct hc_driver __read_mostly ohci_at91_hc_driver;
55 static const struct ohci_driver_overrides ohci_at91_drv_overrides __initconst = {
56 .extra_priv_size = sizeof(struct ohci_at91_priv),
59 extern int usb_disabled(void);
61 /*-------------------------------------------------------------------------*/
63 static void at91_start_clock(struct ohci_at91_priv *ohci_at91)
65 if (ohci_at91->clocked)
67 if (IS_ENABLED(CONFIG_COMMON_CLK)) {
68 clk_set_rate(ohci_at91->uclk, 48000000);
69 clk_prepare_enable(ohci_at91->uclk);
71 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;
77 static void at91_stop_clock(struct ohci_at91_priv *ohci_at91)
79 if (!ohci_at91->clocked)
81 clk_disable_unprepare(ohci_at91->fclk);
82 clk_disable_unprepare(ohci_at91->iclk);
83 clk_disable_unprepare(ohci_at91->hclk);
84 if (IS_ENABLED(CONFIG_COMMON_CLK))
85 clk_disable_unprepare(ohci_at91->uclk);
86 ohci_at91->clocked = false;
89 static void at91_start_hc(struct platform_device *pdev)
91 struct usb_hcd *hcd = platform_get_drvdata(pdev);
92 struct ohci_regs __iomem *regs = hcd->regs;
93 struct ohci_at91_priv *ohci_at91 = hcd_to_ohci_at91_priv(hcd);
95 dev_dbg(&pdev->dev, "start\n");
98 * Start the USB clocks.
100 at91_start_clock(ohci_at91);
103 * The USB host controller must remain in reset.
105 writel(0, ®s->control);
108 static void at91_stop_hc(struct platform_device *pdev)
110 struct usb_hcd *hcd = platform_get_drvdata(pdev);
111 struct ohci_regs __iomem *regs = hcd->regs;
112 struct ohci_at91_priv *ohci_at91 = hcd_to_ohci_at91_priv(hcd);
114 dev_dbg(&pdev->dev, "stop\n");
117 * Put the USB host controller into reset.
119 writel(0, ®s->control);
122 * Stop the USB clocks.
124 at91_stop_clock(ohci_at91);
128 /*-------------------------------------------------------------------------*/
130 static void usb_hcd_at91_remove (struct usb_hcd *, struct platform_device *);
132 /* configure so an HC device and id are always provided */
133 /* always called with process context; sleeping is OK */
137 * usb_hcd_at91_probe - initialize AT91-based HCDs
138 * Context: !in_interrupt()
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.
144 static int usb_hcd_at91_probe(const struct hc_driver *driver,
145 struct platform_device *pdev)
147 struct at91_usbh_data *board;
148 struct ohci_hcd *ohci;
151 struct ohci_at91_priv *ohci_at91;
152 struct device *dev = &pdev->dev;
153 struct resource *res;
156 irq = platform_get_irq(pdev, 0);
158 dev_dbg(dev, "hcd probe: missing irq resource\n");
162 hcd = usb_create_hcd(driver, dev, "at91");
165 ohci_at91 = hcd_to_ohci_at91_priv(hcd);
167 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
168 hcd->regs = devm_ioremap_resource(dev, res);
169 if (IS_ERR(hcd->regs)) {
170 retval = PTR_ERR(hcd->regs);
173 hcd->rsrc_start = res->start;
174 hcd->rsrc_len = resource_size(res);
176 ohci_at91->iclk = devm_clk_get(dev, "ohci_clk");
177 if (IS_ERR(ohci_at91->iclk)) {
178 dev_err(dev, "failed to get ohci_clk\n");
179 retval = PTR_ERR(ohci_at91->iclk);
182 ohci_at91->fclk = devm_clk_get(dev, "uhpck");
183 if (IS_ERR(ohci_at91->fclk)) {
184 dev_err(dev, "failed to get uhpck\n");
185 retval = PTR_ERR(ohci_at91->fclk);
188 ohci_at91->hclk = devm_clk_get(dev, "hclk");
189 if (IS_ERR(ohci_at91->hclk)) {
190 dev_err(dev, "failed to get hclk\n");
191 retval = PTR_ERR(ohci_at91->hclk);
194 if (IS_ENABLED(CONFIG_COMMON_CLK)) {
195 ohci_at91->uclk = devm_clk_get(dev, "usb_clk");
196 if (IS_ERR(ohci_at91->uclk)) {
197 dev_err(dev, "failed to get uclk\n");
198 retval = PTR_ERR(ohci_at91->uclk);
203 board = hcd->self.controller->platform_data;
204 ohci = hcd_to_ohci(hcd);
205 ohci->num_ports = board->ports;
208 retval = usb_add_hcd(hcd, irq, IRQF_SHARED);
210 device_wakeup_enable(hcd->self.controller);
223 /* may be called with controller, bus, and devices active */
226 * usb_hcd_at91_remove - shutdown processing for AT91-based HCDs
227 * @dev: USB Host Controller being removed
228 * Context: !in_interrupt()
230 * Reverses the effect of usb_hcd_at91_probe(), first invoking
231 * the HCD's stop() method. It is always called from a thread
232 * context, "rmmod" or something similar.
235 static void usb_hcd_at91_remove(struct usb_hcd *hcd,
236 struct platform_device *pdev)
243 /*-------------------------------------------------------------------------*/
244 static void ohci_at91_usb_set_power(struct at91_usbh_data *pdata, int port, int enable)
246 if (!valid_port(port))
249 if (!gpio_is_valid(pdata->vbus_pin[port]))
252 gpio_set_value(pdata->vbus_pin[port],
253 pdata->vbus_pin_active_low[port] ^ enable);
256 static int ohci_at91_usb_get_power(struct at91_usbh_data *pdata, int port)
258 if (!valid_port(port))
261 if (!gpio_is_valid(pdata->vbus_pin[port]))
264 return gpio_get_value(pdata->vbus_pin[port]) ^
265 pdata->vbus_pin_active_low[port];
269 * Update the status data from the hub with the over-current indicator change.
271 static int ohci_at91_hub_status_data(struct usb_hcd *hcd, char *buf)
273 struct at91_usbh_data *pdata = hcd->self.controller->platform_data;
274 int length = ohci_hub_status_data(hcd, buf);
277 at91_for_each_port(port) {
278 if (pdata->overcurrent_changed[port]) {
281 buf[0] |= 1 << (port + 1);
289 * Look at the control requests to the root hub and see if we need to override.
291 static int ohci_at91_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
292 u16 wIndex, char *buf, u16 wLength)
294 struct at91_usbh_data *pdata = dev_get_platdata(hcd->self.controller);
295 struct usb_hub_descriptor *desc;
297 u32 *data = (u32 *)buf;
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);
307 if (wValue == USB_PORT_FEAT_POWER) {
308 dev_dbg(hcd->self.controller, "SetPortFeat: POWER\n");
309 if (valid_port(wIndex)) {
310 ohci_at91_usb_set_power(pdata, wIndex, 1);
318 case ClearPortFeature:
320 case USB_PORT_FEAT_C_OVER_CURRENT:
321 dev_dbg(hcd->self.controller,
322 "ClearPortFeature: C_OVER_CURRENT\n");
324 if (valid_port(wIndex)) {
325 pdata->overcurrent_changed[wIndex] = 0;
326 pdata->overcurrent_status[wIndex] = 0;
331 case USB_PORT_FEAT_OVER_CURRENT:
332 dev_dbg(hcd->self.controller,
333 "ClearPortFeature: OVER_CURRENT\n");
335 if (valid_port(wIndex))
336 pdata->overcurrent_status[wIndex] = 0;
340 case USB_PORT_FEAT_POWER:
341 dev_dbg(hcd->self.controller,
342 "ClearPortFeature: POWER\n");
344 if (valid_port(wIndex)) {
345 ohci_at91_usb_set_power(pdata, wIndex, 0);
352 ret = ohci_hub_control(hcd, typeReq, wValue, wIndex + 1, buf, wLength);
357 case GetHubDescriptor:
359 /* update the hub's descriptor */
361 desc = (struct usb_hub_descriptor *)buf;
363 dev_dbg(hcd->self.controller, "wHubCharacteristics 0x%04x\n",
364 desc->wHubCharacteristics);
366 /* remove the old configurations for power-switching, and
367 * over-current protection, and insert our new configuration
370 desc->wHubCharacteristics &= ~cpu_to_le16(HUB_CHAR_LPSM);
371 desc->wHubCharacteristics |=
372 cpu_to_le16(HUB_CHAR_INDV_PORT_LPSM);
374 if (pdata->overcurrent_supported) {
375 desc->wHubCharacteristics &= ~cpu_to_le16(HUB_CHAR_OCPM);
376 desc->wHubCharacteristics |=
377 cpu_to_le16(HUB_CHAR_INDV_PORT_OCPM);
380 dev_dbg(hcd->self.controller, "wHubCharacteristics after 0x%04x\n",
381 desc->wHubCharacteristics);
386 /* check port status */
388 dev_dbg(hcd->self.controller, "GetPortStatus(%d)\n", wIndex);
390 if (valid_port(wIndex)) {
391 if (!ohci_at91_usb_get_power(pdata, wIndex))
392 *data &= ~cpu_to_le32(RH_PS_PPS);
394 if (pdata->overcurrent_changed[wIndex])
395 *data |= cpu_to_le32(RH_PS_OCIC);
397 if (pdata->overcurrent_status[wIndex])
398 *data |= cpu_to_le32(RH_PS_POCI);
406 /*-------------------------------------------------------------------------*/
408 static irqreturn_t ohci_hcd_at91_overcurrent_irq(int irq, void *data)
410 struct platform_device *pdev = data;
411 struct at91_usbh_data *pdata = dev_get_platdata(&pdev->dev);
414 /* From the GPIO notifying the over-current situation, find
415 * out the corresponding port */
416 at91_for_each_port(port) {
417 if (gpio_is_valid(pdata->overcurrent_pin[port]) &&
418 gpio_to_irq(pdata->overcurrent_pin[port]) == irq) {
419 gpio = pdata->overcurrent_pin[port];
424 if (port == AT91_MAX_USBH_PORTS) {
425 dev_err(& pdev->dev, "overcurrent interrupt from unknown GPIO\n");
429 val = gpio_get_value(gpio);
431 /* When notified of an over-current situation, disable power
432 on the corresponding port, and mark this port in
435 ohci_at91_usb_set_power(pdata, port, 0);
436 pdata->overcurrent_status[port] = 1;
437 pdata->overcurrent_changed[port] = 1;
440 dev_dbg(& pdev->dev, "overcurrent situation %s\n",
441 val ? "exited" : "notified");
447 static const struct of_device_id at91_ohci_dt_ids[] = {
448 { .compatible = "atmel,at91rm9200-ohci" },
452 MODULE_DEVICE_TABLE(of, at91_ohci_dt_ids);
454 static int ohci_at91_of_init(struct platform_device *pdev)
456 struct device_node *np = pdev->dev.of_node;
458 enum of_gpio_flags flags;
459 struct at91_usbh_data *pdata;
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.
469 ret = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
473 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
477 if (!of_property_read_u32(np, "num-ports", &ports))
478 pdata->ports = ports;
480 at91_for_each_port(i) {
481 gpio = of_get_named_gpio_flags(np, "atmel,vbus-gpio", i, &flags);
482 pdata->vbus_pin[i] = gpio;
483 if (!gpio_is_valid(gpio))
485 pdata->vbus_pin_active_low[i] = flags & OF_GPIO_ACTIVE_LOW;
488 at91_for_each_port(i)
489 pdata->overcurrent_pin[i] =
490 of_get_named_gpio_flags(np, "atmel,oc-gpio", i, &flags);
492 pdev->dev.platform_data = pdata;
497 static int ohci_at91_of_init(struct platform_device *pdev)
503 /*-------------------------------------------------------------------------*/
505 static int ohci_hcd_at91_drv_probe(struct platform_device *pdev)
507 struct at91_usbh_data *pdata;
512 ret = ohci_at91_of_init(pdev);
516 pdata = dev_get_platdata(&pdev->dev);
519 at91_for_each_port(i) {
521 * do not configure PIO if not in relation with
522 * real USB port on board
524 if (i >= pdata->ports) {
525 pdata->vbus_pin[i] = -EINVAL;
526 pdata->overcurrent_pin[i] = -EINVAL;
530 if (!gpio_is_valid(pdata->vbus_pin[i]))
532 gpio = pdata->vbus_pin[i];
534 ret = gpio_request(gpio, "ohci_vbus");
537 "can't request vbus gpio %d\n", gpio);
540 ret = gpio_direction_output(gpio,
541 !pdata->vbus_pin_active_low[i]);
544 "can't put vbus gpio %d as output %d\n",
545 gpio, !pdata->vbus_pin_active_low[i]);
550 ohci_at91_usb_set_power(pdata, i, 1);
553 at91_for_each_port(i) {
554 if (!gpio_is_valid(pdata->overcurrent_pin[i]))
556 gpio = pdata->overcurrent_pin[i];
558 ret = gpio_request(gpio, "ohci_overcurrent");
561 "can't request overcurrent gpio %d\n",
566 ret = gpio_direction_input(gpio);
569 "can't configure overcurrent gpio %d as input\n",
575 ret = request_irq(gpio_to_irq(gpio),
576 ohci_hcd_at91_overcurrent_irq,
577 IRQF_SHARED, "ohci_overcurrent", pdev);
581 "can't get gpio IRQ for overcurrent\n");
586 device_init_wakeup(&pdev->dev, 1);
587 return usb_hcd_at91_probe(&ohci_at91_hc_driver, pdev);
590 static int ohci_hcd_at91_drv_remove(struct platform_device *pdev)
592 struct at91_usbh_data *pdata = dev_get_platdata(&pdev->dev);
596 at91_for_each_port(i) {
597 if (!gpio_is_valid(pdata->vbus_pin[i]))
599 ohci_at91_usb_set_power(pdata, i, 0);
600 gpio_free(pdata->vbus_pin[i]);
603 at91_for_each_port(i) {
604 if (!gpio_is_valid(pdata->overcurrent_pin[i]))
606 free_irq(gpio_to_irq(pdata->overcurrent_pin[i]), pdev);
607 gpio_free(pdata->overcurrent_pin[i]);
611 device_init_wakeup(&pdev->dev, 0);
612 usb_hcd_at91_remove(platform_get_drvdata(pdev), pdev);
619 ohci_hcd_at91_drv_suspend(struct device *dev)
621 struct usb_hcd *hcd = dev_get_drvdata(dev);
622 struct ohci_hcd *ohci = hcd_to_ohci(hcd);
623 struct ohci_at91_priv *ohci_at91 = hcd_to_ohci_at91_priv(hcd);
627 * Disable wakeup if we are going to sleep with slow clock mode
630 ohci_at91->wakeup = device_may_wakeup(dev)
631 && !at91_suspend_entering_slow_clock();
633 if (ohci_at91->wakeup)
634 enable_irq_wake(hcd->irq);
636 ret = ohci_suspend(hcd, ohci_at91->wakeup);
638 if (ohci_at91->wakeup)
639 disable_irq_wake(hcd->irq);
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).
647 * REVISIT: some boards will be able to turn VBUS off...
649 if (!ohci_at91->wakeup) {
650 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;
655 /* flush the writes */
656 (void) ohci_readl (ohci, &ohci->regs->control);
657 at91_stop_clock(ohci_at91);
663 static int ohci_hcd_at91_drv_resume(struct device *dev)
665 struct usb_hcd *hcd = dev_get_drvdata(dev);
666 struct ohci_at91_priv *ohci_at91 = hcd_to_ohci_at91_priv(hcd);
668 if (ohci_at91->wakeup)
669 disable_irq_wake(hcd->irq);
671 at91_start_clock(ohci_at91);
673 ohci_resume(hcd, false);
678 static SIMPLE_DEV_PM_OPS(ohci_hcd_at91_pm_ops, ohci_hcd_at91_drv_suspend,
679 ohci_hcd_at91_drv_resume);
681 static struct platform_driver ohci_hcd_at91_driver = {
682 .probe = ohci_hcd_at91_drv_probe,
683 .remove = ohci_hcd_at91_drv_remove,
684 .shutdown = usb_hcd_platform_shutdown,
687 .pm = &ohci_hcd_at91_pm_ops,
688 .of_match_table = of_match_ptr(at91_ohci_dt_ids),
692 static int __init ohci_at91_init(void)
697 pr_info("%s: " DRIVER_DESC "\n", hcd_name);
698 ohci_init_driver(&ohci_at91_hc_driver, &ohci_at91_drv_overrides);
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
709 ohci_at91_hc_driver.hub_status_data = ohci_at91_hub_status_data;
710 ohci_at91_hc_driver.hub_control = ohci_at91_hub_control;
712 return platform_driver_register(&ohci_hcd_at91_driver);
714 module_init(ohci_at91_init);
716 static void __exit ohci_at91_cleanup(void)
718 platform_driver_unregister(&ohci_hcd_at91_driver);
720 module_exit(ohci_at91_cleanup);
722 MODULE_DESCRIPTION(DRIVER_DESC);
723 MODULE_LICENSE("GPL");
724 MODULE_ALIAS("platform:at91_ohci");