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 static struct clk *iclk, *fclk, *uclk, *hclk;
37 /* interface and function clocks; sometimes also an AHB clock */
39 #define DRIVER_DESC "OHCI Atmel driver"
41 static const char hcd_name[] = "ohci-atmel";
43 static struct hc_driver __read_mostly ohci_at91_hc_driver;
46 extern int usb_disabled(void);
48 /*-------------------------------------------------------------------------*/
50 static void at91_start_clock(void)
52 if (IS_ENABLED(CONFIG_COMMON_CLK)) {
53 clk_set_rate(uclk, 48000000);
54 clk_prepare_enable(uclk);
56 clk_prepare_enable(hclk);
57 clk_prepare_enable(iclk);
58 clk_prepare_enable(fclk);
62 static void at91_stop_clock(void)
64 clk_disable_unprepare(fclk);
65 clk_disable_unprepare(iclk);
66 clk_disable_unprepare(hclk);
67 if (IS_ENABLED(CONFIG_COMMON_CLK))
68 clk_disable_unprepare(uclk);
72 static void at91_start_hc(struct platform_device *pdev)
74 struct usb_hcd *hcd = platform_get_drvdata(pdev);
75 struct ohci_regs __iomem *regs = hcd->regs;
77 dev_dbg(&pdev->dev, "start\n");
80 * Start the USB clocks.
85 * The USB host controller must remain in reset.
87 writel(0, ®s->control);
90 static void at91_stop_hc(struct platform_device *pdev)
92 struct usb_hcd *hcd = platform_get_drvdata(pdev);
93 struct ohci_regs __iomem *regs = hcd->regs;
95 dev_dbg(&pdev->dev, "stop\n");
98 * Put the USB host controller into reset.
100 writel(0, ®s->control);
103 * Stop the USB clocks.
109 /*-------------------------------------------------------------------------*/
111 static void usb_hcd_at91_remove (struct usb_hcd *, struct platform_device *);
113 /* configure so an HC device and id are always provided */
114 /* always called with process context; sleeping is OK */
118 * usb_hcd_at91_probe - initialize AT91-based HCDs
119 * Context: !in_interrupt()
121 * Allocates basic resources for this USB host controller, and
122 * then invokes the start() method for the HCD associated with it
123 * through the hotplug entry's driver_data.
125 static int usb_hcd_at91_probe(const struct hc_driver *driver,
126 struct platform_device *pdev)
128 struct at91_usbh_data *board;
129 struct ohci_hcd *ohci;
131 struct usb_hcd *hcd = NULL;
132 struct device *dev = &pdev->dev;
133 struct resource *res;
136 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
138 dev_dbg(dev, "hcd probe: missing memory resource\n");
142 irq = platform_get_irq(pdev, 0);
144 dev_dbg(dev, "hcd probe: missing irq resource\n");
148 hcd = usb_create_hcd(driver, dev, "at91");
151 hcd->rsrc_start = res->start;
152 hcd->rsrc_len = resource_size(res);
154 hcd->regs = devm_ioremap_resource(dev, res);
155 if (IS_ERR(hcd->regs)) {
156 retval = PTR_ERR(hcd->regs);
160 iclk = devm_clk_get(dev, "ohci_clk");
162 dev_err(dev, "failed to get ohci_clk\n");
163 retval = PTR_ERR(iclk);
166 fclk = devm_clk_get(dev, "uhpck");
168 dev_err(dev, "failed to get uhpck\n");
169 retval = PTR_ERR(fclk);
172 hclk = devm_clk_get(dev, "hclk");
174 dev_err(dev, "failed to get hclk\n");
175 retval = PTR_ERR(hclk);
178 if (IS_ENABLED(CONFIG_COMMON_CLK)) {
179 uclk = devm_clk_get(dev, "usb_clk");
181 dev_err(dev, "failed to get uclk\n");
182 retval = PTR_ERR(uclk);
187 board = hcd->self.controller->platform_data;
188 ohci = hcd_to_ohci(hcd);
189 ohci->num_ports = board->ports;
192 retval = usb_add_hcd(hcd, irq, IRQF_SHARED);
194 device_wakeup_enable(hcd->self.controller);
207 /* may be called with controller, bus, and devices active */
210 * usb_hcd_at91_remove - shutdown processing for AT91-based HCDs
211 * @dev: USB Host Controller being removed
212 * Context: !in_interrupt()
214 * Reverses the effect of usb_hcd_at91_probe(), first invoking
215 * the HCD's stop() method. It is always called from a thread
216 * context, "rmmod" or something similar.
219 static void usb_hcd_at91_remove(struct usb_hcd *hcd,
220 struct platform_device *pdev)
227 /*-------------------------------------------------------------------------*/
228 static void ohci_at91_usb_set_power(struct at91_usbh_data *pdata, int port, int enable)
230 if (!valid_port(port))
233 if (!gpio_is_valid(pdata->vbus_pin[port]))
236 gpio_set_value(pdata->vbus_pin[port],
237 pdata->vbus_pin_active_low[port] ^ enable);
240 static int ohci_at91_usb_get_power(struct at91_usbh_data *pdata, int port)
242 if (!valid_port(port))
245 if (!gpio_is_valid(pdata->vbus_pin[port]))
248 return gpio_get_value(pdata->vbus_pin[port]) ^
249 pdata->vbus_pin_active_low[port];
253 * Update the status data from the hub with the over-current indicator change.
255 static int ohci_at91_hub_status_data(struct usb_hcd *hcd, char *buf)
257 struct at91_usbh_data *pdata = hcd->self.controller->platform_data;
258 int length = ohci_hub_status_data(hcd, buf);
261 at91_for_each_port(port) {
262 if (pdata->overcurrent_changed[port]) {
265 buf[0] |= 1 << (port + 1);
273 * Look at the control requests to the root hub and see if we need to override.
275 static int ohci_at91_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
276 u16 wIndex, char *buf, u16 wLength)
278 struct at91_usbh_data *pdata = dev_get_platdata(hcd->self.controller);
279 struct usb_hub_descriptor *desc;
281 u32 *data = (u32 *)buf;
283 dev_dbg(hcd->self.controller,
284 "ohci_at91_hub_control(%p,0x%04x,0x%04x,0x%04x,%p,%04x)\n",
285 hcd, typeReq, wValue, wIndex, buf, wLength);
291 if (wValue == USB_PORT_FEAT_POWER) {
292 dev_dbg(hcd->self.controller, "SetPortFeat: POWER\n");
293 if (valid_port(wIndex)) {
294 ohci_at91_usb_set_power(pdata, wIndex, 1);
302 case ClearPortFeature:
304 case USB_PORT_FEAT_C_OVER_CURRENT:
305 dev_dbg(hcd->self.controller,
306 "ClearPortFeature: C_OVER_CURRENT\n");
308 if (valid_port(wIndex)) {
309 pdata->overcurrent_changed[wIndex] = 0;
310 pdata->overcurrent_status[wIndex] = 0;
315 case USB_PORT_FEAT_OVER_CURRENT:
316 dev_dbg(hcd->self.controller,
317 "ClearPortFeature: OVER_CURRENT\n");
319 if (valid_port(wIndex))
320 pdata->overcurrent_status[wIndex] = 0;
324 case USB_PORT_FEAT_POWER:
325 dev_dbg(hcd->self.controller,
326 "ClearPortFeature: POWER\n");
328 if (valid_port(wIndex)) {
329 ohci_at91_usb_set_power(pdata, wIndex, 0);
336 ret = ohci_hub_control(hcd, typeReq, wValue, wIndex + 1, buf, wLength);
341 case GetHubDescriptor:
343 /* update the hub's descriptor */
345 desc = (struct usb_hub_descriptor *)buf;
347 dev_dbg(hcd->self.controller, "wHubCharacteristics 0x%04x\n",
348 desc->wHubCharacteristics);
350 /* remove the old configurations for power-switching, and
351 * over-current protection, and insert our new configuration
354 desc->wHubCharacteristics &= ~cpu_to_le16(HUB_CHAR_LPSM);
355 desc->wHubCharacteristics |= cpu_to_le16(0x0001);
357 if (pdata->overcurrent_supported) {
358 desc->wHubCharacteristics &= ~cpu_to_le16(HUB_CHAR_OCPM);
359 desc->wHubCharacteristics |= cpu_to_le16(0x0008|0x0001);
362 dev_dbg(hcd->self.controller, "wHubCharacteristics after 0x%04x\n",
363 desc->wHubCharacteristics);
368 /* check port status */
370 dev_dbg(hcd->self.controller, "GetPortStatus(%d)\n", wIndex);
372 if (valid_port(wIndex)) {
373 if (!ohci_at91_usb_get_power(pdata, wIndex))
374 *data &= ~cpu_to_le32(RH_PS_PPS);
376 if (pdata->overcurrent_changed[wIndex])
377 *data |= cpu_to_le32(RH_PS_OCIC);
379 if (pdata->overcurrent_status[wIndex])
380 *data |= cpu_to_le32(RH_PS_POCI);
388 /*-------------------------------------------------------------------------*/
390 static irqreturn_t ohci_hcd_at91_overcurrent_irq(int irq, void *data)
392 struct platform_device *pdev = data;
393 struct at91_usbh_data *pdata = dev_get_platdata(&pdev->dev);
396 /* From the GPIO notifying the over-current situation, find
397 * out the corresponding port */
398 at91_for_each_port(port) {
399 if (gpio_is_valid(pdata->overcurrent_pin[port]) &&
400 gpio_to_irq(pdata->overcurrent_pin[port]) == irq) {
401 gpio = pdata->overcurrent_pin[port];
406 if (port == AT91_MAX_USBH_PORTS) {
407 dev_err(& pdev->dev, "overcurrent interrupt from unknown GPIO\n");
411 val = gpio_get_value(gpio);
413 /* When notified of an over-current situation, disable power
414 on the corresponding port, and mark this port in
417 ohci_at91_usb_set_power(pdata, port, 0);
418 pdata->overcurrent_status[port] = 1;
419 pdata->overcurrent_changed[port] = 1;
422 dev_dbg(& pdev->dev, "overcurrent situation %s\n",
423 val ? "exited" : "notified");
429 static const struct of_device_id at91_ohci_dt_ids[] = {
430 { .compatible = "atmel,at91rm9200-ohci" },
434 MODULE_DEVICE_TABLE(of, at91_ohci_dt_ids);
436 static int ohci_at91_of_init(struct platform_device *pdev)
438 struct device_node *np = pdev->dev.of_node;
440 enum of_gpio_flags flags;
441 struct at91_usbh_data *pdata;
447 /* Right now device-tree probed devices don't get dma_mask set.
448 * Since shared usb code relies on it, set it here for now.
449 * Once we have dma capability bindings this can go away.
451 ret = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
455 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
459 if (!of_property_read_u32(np, "num-ports", &ports))
460 pdata->ports = ports;
462 at91_for_each_port(i) {
463 gpio = of_get_named_gpio_flags(np, "atmel,vbus-gpio", i, &flags);
464 pdata->vbus_pin[i] = gpio;
465 if (!gpio_is_valid(gpio))
467 pdata->vbus_pin_active_low[i] = flags & OF_GPIO_ACTIVE_LOW;
470 at91_for_each_port(i)
471 pdata->overcurrent_pin[i] =
472 of_get_named_gpio_flags(np, "atmel,oc-gpio", i, &flags);
474 pdev->dev.platform_data = pdata;
479 static int ohci_at91_of_init(struct platform_device *pdev)
485 /*-------------------------------------------------------------------------*/
487 static int ohci_hcd_at91_drv_probe(struct platform_device *pdev)
489 struct at91_usbh_data *pdata;
494 ret = ohci_at91_of_init(pdev);
498 pdata = dev_get_platdata(&pdev->dev);
501 at91_for_each_port(i) {
503 * do not configure PIO if not in relation with
504 * real USB port on board
506 if (i >= pdata->ports) {
507 pdata->vbus_pin[i] = -EINVAL;
508 pdata->overcurrent_pin[i] = -EINVAL;
512 if (!gpio_is_valid(pdata->vbus_pin[i]))
514 gpio = pdata->vbus_pin[i];
516 ret = gpio_request(gpio, "ohci_vbus");
519 "can't request vbus gpio %d\n", gpio);
522 ret = gpio_direction_output(gpio,
523 !pdata->vbus_pin_active_low[i]);
526 "can't put vbus gpio %d as output %d\n",
527 gpio, !pdata->vbus_pin_active_low[i]);
532 ohci_at91_usb_set_power(pdata, i, 1);
535 at91_for_each_port(i) {
536 if (!gpio_is_valid(pdata->overcurrent_pin[i]))
538 gpio = pdata->overcurrent_pin[i];
540 ret = gpio_request(gpio, "ohci_overcurrent");
543 "can't request overcurrent gpio %d\n",
548 ret = gpio_direction_input(gpio);
551 "can't configure overcurrent gpio %d as input\n",
557 ret = request_irq(gpio_to_irq(gpio),
558 ohci_hcd_at91_overcurrent_irq,
559 IRQF_SHARED, "ohci_overcurrent", pdev);
563 "can't get gpio IRQ for overcurrent\n");
568 device_init_wakeup(&pdev->dev, 1);
569 return usb_hcd_at91_probe(&ohci_at91_hc_driver, pdev);
572 static int ohci_hcd_at91_drv_remove(struct platform_device *pdev)
574 struct at91_usbh_data *pdata = dev_get_platdata(&pdev->dev);
578 at91_for_each_port(i) {
579 if (!gpio_is_valid(pdata->vbus_pin[i]))
581 ohci_at91_usb_set_power(pdata, i, 0);
582 gpio_free(pdata->vbus_pin[i]);
585 at91_for_each_port(i) {
586 if (!gpio_is_valid(pdata->overcurrent_pin[i]))
588 free_irq(gpio_to_irq(pdata->overcurrent_pin[i]), pdev);
589 gpio_free(pdata->overcurrent_pin[i]);
593 device_init_wakeup(&pdev->dev, 0);
594 usb_hcd_at91_remove(platform_get_drvdata(pdev), pdev);
601 ohci_hcd_at91_drv_suspend(struct platform_device *pdev, pm_message_t mesg)
603 struct usb_hcd *hcd = platform_get_drvdata(pdev);
604 struct ohci_hcd *ohci = hcd_to_ohci(hcd);
605 bool do_wakeup = device_may_wakeup(&pdev->dev);
609 enable_irq_wake(hcd->irq);
611 ret = ohci_suspend(hcd, do_wakeup);
613 disable_irq_wake(hcd->irq);
617 * The integrated transceivers seem unable to notice disconnect,
618 * reconnect, or wakeup without the 48 MHz clock active. so for
619 * correctness, always discard connection state (using reset).
621 * REVISIT: some boards will be able to turn VBUS off...
623 if (at91_suspend_entering_slow_clock()) {
624 ohci->hc_control = ohci_readl(ohci, &ohci->regs->control);
625 ohci->hc_control &= OHCI_CTRL_RWC;
626 ohci_writel(ohci, ohci->hc_control, &ohci->regs->control);
627 ohci->rh_state = OHCI_RH_HALTED;
629 /* flush the writes */
630 (void) ohci_readl (ohci, &ohci->regs->control);
637 static int ohci_hcd_at91_drv_resume(struct platform_device *pdev)
639 struct usb_hcd *hcd = platform_get_drvdata(pdev);
641 if (device_may_wakeup(&pdev->dev))
642 disable_irq_wake(hcd->irq);
647 ohci_resume(hcd, false);
651 #define ohci_hcd_at91_drv_suspend NULL
652 #define ohci_hcd_at91_drv_resume NULL
655 static struct platform_driver ohci_hcd_at91_driver = {
656 .probe = ohci_hcd_at91_drv_probe,
657 .remove = ohci_hcd_at91_drv_remove,
658 .shutdown = usb_hcd_platform_shutdown,
659 .suspend = ohci_hcd_at91_drv_suspend,
660 .resume = ohci_hcd_at91_drv_resume,
663 .owner = THIS_MODULE,
664 .of_match_table = of_match_ptr(at91_ohci_dt_ids),
668 static int __init ohci_at91_init(void)
673 pr_info("%s: " DRIVER_DESC "\n", hcd_name);
674 ohci_init_driver(&ohci_at91_hc_driver, NULL);
677 * The Atmel HW has some unusual quirks, which require Atmel-specific
678 * workarounds. We override certain hc_driver functions here to
679 * achieve that. We explicitly do not enhance ohci_driver_overrides to
680 * allow this more easily, since this is an unusual case, and we don't
681 * want to encourage others to override these functions by making it
685 ohci_at91_hc_driver.hub_status_data = ohci_at91_hub_status_data;
686 ohci_at91_hc_driver.hub_control = ohci_at91_hub_control;
688 return platform_driver_register(&ohci_hcd_at91_driver);
690 module_init(ohci_at91_init);
692 static void __exit ohci_at91_cleanup(void)
694 platform_driver_unregister(&ohci_hcd_at91_driver);
696 module_exit(ohci_at91_cleanup);
698 MODULE_DESCRIPTION(DRIVER_DESC);
699 MODULE_LICENSE("GPL");
700 MODULE_ALIAS("platform:at91_ohci");