2 * OHCI HCD (Host Controller Driver) for USB.
4 * Copyright (C) 2004 SAN People (Pty) Ltd.
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>
27 #include <mach/hardware.h>
35 #define valid_port(index) ((index) >= 0 && (index) < AT91_MAX_USBH_PORTS)
36 #define at91_for_each_port(index) \
37 for ((index) = 0; (index) < AT91_MAX_USBH_PORTS; (index)++)
39 /* interface, function and usb clocks; sometimes also an AHB clock */
40 static struct clk *iclk, *fclk, *uclk, *hclk;
41 /* interface and function clocks; sometimes also an AHB clock */
43 #define DRIVER_DESC "OHCI Atmel driver"
45 static const char hcd_name[] = "ohci-atmel";
47 static struct hc_driver __read_mostly ohci_at91_hc_driver;
50 extern int usb_disabled(void);
52 /*-------------------------------------------------------------------------*/
54 static void at91_start_clock(void)
56 if (IS_ENABLED(CONFIG_COMMON_CLK)) {
57 clk_set_rate(uclk, 48000000);
58 clk_prepare_enable(uclk);
60 clk_prepare_enable(hclk);
61 clk_prepare_enable(iclk);
62 clk_prepare_enable(fclk);
66 static void at91_stop_clock(void)
68 clk_disable_unprepare(fclk);
69 clk_disable_unprepare(iclk);
70 clk_disable_unprepare(hclk);
71 if (IS_ENABLED(CONFIG_COMMON_CLK))
72 clk_disable_unprepare(uclk);
76 static void at91_start_hc(struct platform_device *pdev)
78 struct usb_hcd *hcd = platform_get_drvdata(pdev);
79 struct ohci_regs __iomem *regs = hcd->regs;
81 dev_dbg(&pdev->dev, "start\n");
84 * Start the USB clocks.
89 * The USB host controller must remain in reset.
91 writel(0, ®s->control);
94 static void at91_stop_hc(struct platform_device *pdev)
96 struct usb_hcd *hcd = platform_get_drvdata(pdev);
97 struct ohci_regs __iomem *regs = hcd->regs;
99 dev_dbg(&pdev->dev, "stop\n");
102 * Put the USB host controller into reset.
104 writel(0, ®s->control);
107 * Stop the USB clocks.
113 /*-------------------------------------------------------------------------*/
115 static void usb_hcd_at91_remove (struct usb_hcd *, struct platform_device *);
117 /* configure so an HC device and id are always provided */
118 /* always called with process context; sleeping is OK */
122 * usb_hcd_at91_probe - initialize AT91-based HCDs
123 * Context: !in_interrupt()
125 * Allocates basic resources for this USB host controller, and
126 * then invokes the start() method for the HCD associated with it
127 * through the hotplug entry's driver_data.
129 static int usb_hcd_at91_probe(const struct hc_driver *driver,
130 struct platform_device *pdev)
132 struct at91_usbh_data *board;
133 struct ohci_hcd *ohci;
135 struct usb_hcd *hcd = NULL;
136 struct device *dev = &pdev->dev;
137 struct resource *res;
140 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
142 dev_dbg(dev, "hcd probe: missing memory resource\n");
146 irq = platform_get_irq(pdev, 0);
148 dev_dbg(dev, "hcd probe: missing irq resource\n");
152 hcd = usb_create_hcd(driver, dev, "at91");
155 hcd->rsrc_start = res->start;
156 hcd->rsrc_len = resource_size(res);
158 hcd->regs = devm_ioremap_resource(dev, res);
159 if (IS_ERR(hcd->regs)) {
160 retval = PTR_ERR(hcd->regs);
164 iclk = devm_clk_get(dev, "ohci_clk");
166 dev_err(dev, "failed to get ohci_clk\n");
167 retval = PTR_ERR(iclk);
170 fclk = devm_clk_get(dev, "uhpck");
172 dev_err(dev, "failed to get uhpck\n");
173 retval = PTR_ERR(fclk);
176 hclk = devm_clk_get(dev, "hclk");
178 dev_err(dev, "failed to get hclk\n");
179 retval = PTR_ERR(hclk);
182 if (IS_ENABLED(CONFIG_COMMON_CLK)) {
183 uclk = devm_clk_get(dev, "usb_clk");
185 dev_err(dev, "failed to get uclk\n");
186 retval = PTR_ERR(uclk);
191 board = hcd->self.controller->platform_data;
192 ohci = hcd_to_ohci(hcd);
193 ohci->num_ports = board->ports;
196 retval = usb_add_hcd(hcd, irq, IRQF_SHARED);
198 device_wakeup_enable(hcd->self.controller);
211 /* may be called with controller, bus, and devices active */
214 * usb_hcd_at91_remove - shutdown processing for AT91-based HCDs
215 * @dev: USB Host Controller being removed
216 * Context: !in_interrupt()
218 * Reverses the effect of usb_hcd_at91_probe(), first invoking
219 * the HCD's stop() method. It is always called from a thread
220 * context, "rmmod" or something similar.
223 static void usb_hcd_at91_remove(struct usb_hcd *hcd,
224 struct platform_device *pdev)
231 /*-------------------------------------------------------------------------*/
232 static void ohci_at91_usb_set_power(struct at91_usbh_data *pdata, int port, int enable)
234 if (!valid_port(port))
237 if (!gpio_is_valid(pdata->vbus_pin[port]))
240 gpio_set_value(pdata->vbus_pin[port],
241 pdata->vbus_pin_active_low[port] ^ enable);
244 static int ohci_at91_usb_get_power(struct at91_usbh_data *pdata, int port)
246 if (!valid_port(port))
249 if (!gpio_is_valid(pdata->vbus_pin[port]))
252 return gpio_get_value(pdata->vbus_pin[port]) ^
253 pdata->vbus_pin_active_low[port];
257 * Update the status data from the hub with the over-current indicator change.
259 static int ohci_at91_hub_status_data(struct usb_hcd *hcd, char *buf)
261 struct at91_usbh_data *pdata = hcd->self.controller->platform_data;
262 int length = ohci_hub_status_data(hcd, buf);
265 at91_for_each_port(port) {
266 if (pdata->overcurrent_changed[port]) {
269 buf[0] |= 1 << (port + 1);
277 * Look at the control requests to the root hub and see if we need to override.
279 static int ohci_at91_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
280 u16 wIndex, char *buf, u16 wLength)
282 struct at91_usbh_data *pdata = dev_get_platdata(hcd->self.controller);
283 struct usb_hub_descriptor *desc;
285 u32 *data = (u32 *)buf;
287 dev_dbg(hcd->self.controller,
288 "ohci_at91_hub_control(%p,0x%04x,0x%04x,0x%04x,%p,%04x)\n",
289 hcd, typeReq, wValue, wIndex, buf, wLength);
295 if (wValue == USB_PORT_FEAT_POWER) {
296 dev_dbg(hcd->self.controller, "SetPortFeat: POWER\n");
297 if (valid_port(wIndex)) {
298 ohci_at91_usb_set_power(pdata, wIndex, 1);
306 case ClearPortFeature:
308 case USB_PORT_FEAT_C_OVER_CURRENT:
309 dev_dbg(hcd->self.controller,
310 "ClearPortFeature: C_OVER_CURRENT\n");
312 if (valid_port(wIndex)) {
313 pdata->overcurrent_changed[wIndex] = 0;
314 pdata->overcurrent_status[wIndex] = 0;
319 case USB_PORT_FEAT_OVER_CURRENT:
320 dev_dbg(hcd->self.controller,
321 "ClearPortFeature: OVER_CURRENT\n");
323 if (valid_port(wIndex))
324 pdata->overcurrent_status[wIndex] = 0;
328 case USB_PORT_FEAT_POWER:
329 dev_dbg(hcd->self.controller,
330 "ClearPortFeature: POWER\n");
332 if (valid_port(wIndex)) {
333 ohci_at91_usb_set_power(pdata, wIndex, 0);
340 ret = ohci_hub_control(hcd, typeReq, wValue, wIndex + 1, 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 (valid_port(wIndex)) {
377 if (!ohci_at91_usb_get_power(pdata, wIndex))
378 *data &= ~cpu_to_le32(RH_PS_PPS);
380 if (pdata->overcurrent_changed[wIndex])
381 *data |= cpu_to_le32(RH_PS_OCIC);
383 if (pdata->overcurrent_status[wIndex])
384 *data |= cpu_to_le32(RH_PS_POCI);
392 /*-------------------------------------------------------------------------*/
394 static irqreturn_t ohci_hcd_at91_overcurrent_irq(int irq, void *data)
396 struct platform_device *pdev = data;
397 struct at91_usbh_data *pdata = dev_get_platdata(&pdev->dev);
400 /* From the GPIO notifying the over-current situation, find
401 * out the corresponding port */
402 at91_for_each_port(port) {
403 if (gpio_is_valid(pdata->overcurrent_pin[port]) &&
404 gpio_to_irq(pdata->overcurrent_pin[port]) == irq) {
405 gpio = pdata->overcurrent_pin[port];
410 if (port == AT91_MAX_USBH_PORTS) {
411 dev_err(& pdev->dev, "overcurrent interrupt from unknown GPIO\n");
415 val = gpio_get_value(gpio);
417 /* When notified of an over-current situation, disable power
418 on the corresponding port, and mark this port in
421 ohci_at91_usb_set_power(pdata, port, 0);
422 pdata->overcurrent_status[port] = 1;
423 pdata->overcurrent_changed[port] = 1;
426 dev_dbg(& pdev->dev, "overcurrent situation %s\n",
427 val ? "exited" : "notified");
433 static const struct of_device_id at91_ohci_dt_ids[] = {
434 { .compatible = "atmel,at91rm9200-ohci" },
438 MODULE_DEVICE_TABLE(of, at91_ohci_dt_ids);
440 static int ohci_at91_of_init(struct platform_device *pdev)
442 struct device_node *np = pdev->dev.of_node;
444 enum of_gpio_flags flags;
445 struct at91_usbh_data *pdata;
451 /* Right now device-tree probed devices don't get dma_mask set.
452 * Since shared usb code relies on it, set it here for now.
453 * Once we have dma capability bindings this can go away.
455 ret = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
459 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
463 if (!of_property_read_u32(np, "num-ports", &ports))
464 pdata->ports = ports;
466 at91_for_each_port(i) {
467 gpio = of_get_named_gpio_flags(np, "atmel,vbus-gpio", i, &flags);
468 pdata->vbus_pin[i] = gpio;
469 if (!gpio_is_valid(gpio))
471 pdata->vbus_pin_active_low[i] = flags & OF_GPIO_ACTIVE_LOW;
474 at91_for_each_port(i)
475 pdata->overcurrent_pin[i] =
476 of_get_named_gpio_flags(np, "atmel,oc-gpio", i, &flags);
478 pdev->dev.platform_data = pdata;
483 static int ohci_at91_of_init(struct platform_device *pdev)
489 /*-------------------------------------------------------------------------*/
491 static int ohci_hcd_at91_drv_probe(struct platform_device *pdev)
493 struct at91_usbh_data *pdata;
498 ret = ohci_at91_of_init(pdev);
502 pdata = dev_get_platdata(&pdev->dev);
505 at91_for_each_port(i) {
507 * do not configure PIO if not in relation with
508 * real USB port on board
510 if (i >= pdata->ports) {
511 pdata->vbus_pin[i] = -EINVAL;
512 pdata->overcurrent_pin[i] = -EINVAL;
516 if (!gpio_is_valid(pdata->vbus_pin[i]))
518 gpio = pdata->vbus_pin[i];
520 ret = gpio_request(gpio, "ohci_vbus");
523 "can't request vbus gpio %d\n", gpio);
526 ret = gpio_direction_output(gpio,
527 !pdata->vbus_pin_active_low[i]);
530 "can't put vbus gpio %d as output %d\n",
531 gpio, !pdata->vbus_pin_active_low[i]);
536 ohci_at91_usb_set_power(pdata, i, 1);
539 at91_for_each_port(i) {
540 if (!gpio_is_valid(pdata->overcurrent_pin[i]))
542 gpio = pdata->overcurrent_pin[i];
544 ret = gpio_request(gpio, "ohci_overcurrent");
547 "can't request overcurrent gpio %d\n",
552 ret = gpio_direction_input(gpio);
555 "can't configure overcurrent gpio %d as input\n",
561 ret = request_irq(gpio_to_irq(gpio),
562 ohci_hcd_at91_overcurrent_irq,
563 IRQF_SHARED, "ohci_overcurrent", pdev);
567 "can't get gpio IRQ for overcurrent\n");
572 device_init_wakeup(&pdev->dev, 1);
573 return usb_hcd_at91_probe(&ohci_at91_hc_driver, pdev);
576 static int ohci_hcd_at91_drv_remove(struct platform_device *pdev)
578 struct at91_usbh_data *pdata = dev_get_platdata(&pdev->dev);
582 at91_for_each_port(i) {
583 if (!gpio_is_valid(pdata->vbus_pin[i]))
585 ohci_at91_usb_set_power(pdata, i, 0);
586 gpio_free(pdata->vbus_pin[i]);
589 at91_for_each_port(i) {
590 if (!gpio_is_valid(pdata->overcurrent_pin[i]))
592 free_irq(gpio_to_irq(pdata->overcurrent_pin[i]), pdev);
593 gpio_free(pdata->overcurrent_pin[i]);
597 device_init_wakeup(&pdev->dev, 0);
598 usb_hcd_at91_remove(platform_get_drvdata(pdev), pdev);
605 ohci_hcd_at91_drv_suspend(struct platform_device *pdev, pm_message_t mesg)
607 struct usb_hcd *hcd = platform_get_drvdata(pdev);
608 struct ohci_hcd *ohci = hcd_to_ohci(hcd);
609 bool do_wakeup = device_may_wakeup(&pdev->dev);
613 enable_irq_wake(hcd->irq);
615 ret = ohci_suspend(hcd, do_wakeup);
617 disable_irq_wake(hcd->irq);
621 * The integrated transceivers seem unable to notice disconnect,
622 * reconnect, or wakeup without the 48 MHz clock active. so for
623 * correctness, always discard connection state (using reset).
625 * REVISIT: some boards will be able to turn VBUS off...
627 if (at91_suspend_entering_slow_clock()) {
628 ohci->hc_control = ohci_readl(ohci, &ohci->regs->control);
629 ohci->hc_control &= OHCI_CTRL_RWC;
630 ohci_writel(ohci, ohci->hc_control, &ohci->regs->control);
631 ohci->rh_state = OHCI_RH_HALTED;
633 /* flush the writes */
634 (void) ohci_readl (ohci, &ohci->regs->control);
641 static int ohci_hcd_at91_drv_resume(struct platform_device *pdev)
643 struct usb_hcd *hcd = platform_get_drvdata(pdev);
645 if (device_may_wakeup(&pdev->dev))
646 disable_irq_wake(hcd->irq);
651 ohci_resume(hcd, false);
655 #define ohci_hcd_at91_drv_suspend NULL
656 #define ohci_hcd_at91_drv_resume NULL
659 static struct platform_driver ohci_hcd_at91_driver = {
660 .probe = ohci_hcd_at91_drv_probe,
661 .remove = ohci_hcd_at91_drv_remove,
662 .shutdown = usb_hcd_platform_shutdown,
663 .suspend = ohci_hcd_at91_drv_suspend,
664 .resume = ohci_hcd_at91_drv_resume,
667 .owner = THIS_MODULE,
668 .of_match_table = of_match_ptr(at91_ohci_dt_ids),
672 static int __init ohci_at91_init(void)
677 pr_info("%s: " DRIVER_DESC "\n", hcd_name);
678 ohci_init_driver(&ohci_at91_hc_driver, NULL);
681 * The Atmel HW has some unusual quirks, which require Atmel-specific
682 * workarounds. We override certain hc_driver functions here to
683 * achieve that. We explicitly do not enhance ohci_driver_overrides to
684 * allow this more easily, since this is an unusual case, and we don't
685 * want to encourage others to override these functions by making it
689 ohci_at91_hc_driver.hub_status_data = ohci_at91_hub_status_data;
690 ohci_at91_hc_driver.hub_control = ohci_at91_hub_control;
692 return platform_driver_register(&ohci_hcd_at91_driver);
694 module_init(ohci_at91_init);
696 static void __exit ohci_at91_cleanup(void)
698 platform_driver_unregister(&ohci_hcd_at91_driver);
700 module_exit(ohci_at91_cleanup);
702 MODULE_DESCRIPTION(DRIVER_DESC);
703 MODULE_LICENSE("GPL");
704 MODULE_ALIAS("platform:at91_ohci");