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 irq = platform_get_irq(pdev, 0);
138 dev_dbg(dev, "hcd probe: missing irq resource\n");
142 hcd = usb_create_hcd(driver, dev, "at91");
146 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
147 hcd->regs = devm_ioremap_resource(dev, res);
148 if (IS_ERR(hcd->regs)) {
149 retval = PTR_ERR(hcd->regs);
152 hcd->rsrc_start = res->start;
153 hcd->rsrc_len = resource_size(res);
155 iclk = devm_clk_get(dev, "ohci_clk");
157 dev_err(dev, "failed to get ohci_clk\n");
158 retval = PTR_ERR(iclk);
161 fclk = devm_clk_get(dev, "uhpck");
163 dev_err(dev, "failed to get uhpck\n");
164 retval = PTR_ERR(fclk);
167 hclk = devm_clk_get(dev, "hclk");
169 dev_err(dev, "failed to get hclk\n");
170 retval = PTR_ERR(hclk);
173 if (IS_ENABLED(CONFIG_COMMON_CLK)) {
174 uclk = devm_clk_get(dev, "usb_clk");
176 dev_err(dev, "failed to get uclk\n");
177 retval = PTR_ERR(uclk);
182 board = hcd->self.controller->platform_data;
183 ohci = hcd_to_ohci(hcd);
184 ohci->num_ports = board->ports;
187 retval = usb_add_hcd(hcd, irq, IRQF_SHARED);
189 device_wakeup_enable(hcd->self.controller);
202 /* may be called with controller, bus, and devices active */
205 * usb_hcd_at91_remove - shutdown processing for AT91-based HCDs
206 * @dev: USB Host Controller being removed
207 * Context: !in_interrupt()
209 * Reverses the effect of usb_hcd_at91_probe(), first invoking
210 * the HCD's stop() method. It is always called from a thread
211 * context, "rmmod" or something similar.
214 static void usb_hcd_at91_remove(struct usb_hcd *hcd,
215 struct platform_device *pdev)
222 /*-------------------------------------------------------------------------*/
223 static void ohci_at91_usb_set_power(struct at91_usbh_data *pdata, int port, int enable)
225 if (!valid_port(port))
228 if (!gpio_is_valid(pdata->vbus_pin[port]))
231 gpio_set_value(pdata->vbus_pin[port],
232 pdata->vbus_pin_active_low[port] ^ enable);
235 static int ohci_at91_usb_get_power(struct at91_usbh_data *pdata, int port)
237 if (!valid_port(port))
240 if (!gpio_is_valid(pdata->vbus_pin[port]))
243 return gpio_get_value(pdata->vbus_pin[port]) ^
244 pdata->vbus_pin_active_low[port];
248 * Update the status data from the hub with the over-current indicator change.
250 static int ohci_at91_hub_status_data(struct usb_hcd *hcd, char *buf)
252 struct at91_usbh_data *pdata = hcd->self.controller->platform_data;
253 int length = ohci_hub_status_data(hcd, buf);
256 at91_for_each_port(port) {
257 if (pdata->overcurrent_changed[port]) {
260 buf[0] |= 1 << (port + 1);
268 * Look at the control requests to the root hub and see if we need to override.
270 static int ohci_at91_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
271 u16 wIndex, char *buf, u16 wLength)
273 struct at91_usbh_data *pdata = dev_get_platdata(hcd->self.controller);
274 struct usb_hub_descriptor *desc;
276 u32 *data = (u32 *)buf;
278 dev_dbg(hcd->self.controller,
279 "ohci_at91_hub_control(%p,0x%04x,0x%04x,0x%04x,%p,%04x)\n",
280 hcd, typeReq, wValue, wIndex, buf, wLength);
286 if (wValue == USB_PORT_FEAT_POWER) {
287 dev_dbg(hcd->self.controller, "SetPortFeat: POWER\n");
288 if (valid_port(wIndex)) {
289 ohci_at91_usb_set_power(pdata, wIndex, 1);
297 case ClearPortFeature:
299 case USB_PORT_FEAT_C_OVER_CURRENT:
300 dev_dbg(hcd->self.controller,
301 "ClearPortFeature: C_OVER_CURRENT\n");
303 if (valid_port(wIndex)) {
304 pdata->overcurrent_changed[wIndex] = 0;
305 pdata->overcurrent_status[wIndex] = 0;
310 case USB_PORT_FEAT_OVER_CURRENT:
311 dev_dbg(hcd->self.controller,
312 "ClearPortFeature: OVER_CURRENT\n");
314 if (valid_port(wIndex))
315 pdata->overcurrent_status[wIndex] = 0;
319 case USB_PORT_FEAT_POWER:
320 dev_dbg(hcd->self.controller,
321 "ClearPortFeature: POWER\n");
323 if (valid_port(wIndex)) {
324 ohci_at91_usb_set_power(pdata, wIndex, 0);
331 ret = ohci_hub_control(hcd, typeReq, wValue, wIndex + 1, buf, wLength);
336 case GetHubDescriptor:
338 /* update the hub's descriptor */
340 desc = (struct usb_hub_descriptor *)buf;
342 dev_dbg(hcd->self.controller, "wHubCharacteristics 0x%04x\n",
343 desc->wHubCharacteristics);
345 /* remove the old configurations for power-switching, and
346 * over-current protection, and insert our new configuration
349 desc->wHubCharacteristics &= ~cpu_to_le16(HUB_CHAR_LPSM);
350 desc->wHubCharacteristics |= cpu_to_le16(0x0001);
352 if (pdata->overcurrent_supported) {
353 desc->wHubCharacteristics &= ~cpu_to_le16(HUB_CHAR_OCPM);
354 desc->wHubCharacteristics |= cpu_to_le16(0x0008|0x0001);
357 dev_dbg(hcd->self.controller, "wHubCharacteristics after 0x%04x\n",
358 desc->wHubCharacteristics);
363 /* check port status */
365 dev_dbg(hcd->self.controller, "GetPortStatus(%d)\n", wIndex);
367 if (valid_port(wIndex)) {
368 if (!ohci_at91_usb_get_power(pdata, wIndex))
369 *data &= ~cpu_to_le32(RH_PS_PPS);
371 if (pdata->overcurrent_changed[wIndex])
372 *data |= cpu_to_le32(RH_PS_OCIC);
374 if (pdata->overcurrent_status[wIndex])
375 *data |= cpu_to_le32(RH_PS_POCI);
383 /*-------------------------------------------------------------------------*/
385 static irqreturn_t ohci_hcd_at91_overcurrent_irq(int irq, void *data)
387 struct platform_device *pdev = data;
388 struct at91_usbh_data *pdata = dev_get_platdata(&pdev->dev);
391 /* From the GPIO notifying the over-current situation, find
392 * out the corresponding port */
393 at91_for_each_port(port) {
394 if (gpio_is_valid(pdata->overcurrent_pin[port]) &&
395 gpio_to_irq(pdata->overcurrent_pin[port]) == irq) {
396 gpio = pdata->overcurrent_pin[port];
401 if (port == AT91_MAX_USBH_PORTS) {
402 dev_err(& pdev->dev, "overcurrent interrupt from unknown GPIO\n");
406 val = gpio_get_value(gpio);
408 /* When notified of an over-current situation, disable power
409 on the corresponding port, and mark this port in
412 ohci_at91_usb_set_power(pdata, port, 0);
413 pdata->overcurrent_status[port] = 1;
414 pdata->overcurrent_changed[port] = 1;
417 dev_dbg(& pdev->dev, "overcurrent situation %s\n",
418 val ? "exited" : "notified");
424 static const struct of_device_id at91_ohci_dt_ids[] = {
425 { .compatible = "atmel,at91rm9200-ohci" },
429 MODULE_DEVICE_TABLE(of, at91_ohci_dt_ids);
431 static int ohci_at91_of_init(struct platform_device *pdev)
433 struct device_node *np = pdev->dev.of_node;
435 enum of_gpio_flags flags;
436 struct at91_usbh_data *pdata;
442 /* Right now device-tree probed devices don't get dma_mask set.
443 * Since shared usb code relies on it, set it here for now.
444 * Once we have dma capability bindings this can go away.
446 ret = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
450 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
454 if (!of_property_read_u32(np, "num-ports", &ports))
455 pdata->ports = ports;
457 at91_for_each_port(i) {
458 gpio = of_get_named_gpio_flags(np, "atmel,vbus-gpio", i, &flags);
459 pdata->vbus_pin[i] = gpio;
460 if (!gpio_is_valid(gpio))
462 pdata->vbus_pin_active_low[i] = flags & OF_GPIO_ACTIVE_LOW;
465 at91_for_each_port(i)
466 pdata->overcurrent_pin[i] =
467 of_get_named_gpio_flags(np, "atmel,oc-gpio", i, &flags);
469 pdev->dev.platform_data = pdata;
474 static int ohci_at91_of_init(struct platform_device *pdev)
480 /*-------------------------------------------------------------------------*/
482 static int ohci_hcd_at91_drv_probe(struct platform_device *pdev)
484 struct at91_usbh_data *pdata;
489 ret = ohci_at91_of_init(pdev);
493 pdata = dev_get_platdata(&pdev->dev);
496 at91_for_each_port(i) {
498 * do not configure PIO if not in relation with
499 * real USB port on board
501 if (i >= pdata->ports) {
502 pdata->vbus_pin[i] = -EINVAL;
503 pdata->overcurrent_pin[i] = -EINVAL;
507 if (!gpio_is_valid(pdata->vbus_pin[i]))
509 gpio = pdata->vbus_pin[i];
511 ret = gpio_request(gpio, "ohci_vbus");
514 "can't request vbus gpio %d\n", gpio);
517 ret = gpio_direction_output(gpio,
518 !pdata->vbus_pin_active_low[i]);
521 "can't put vbus gpio %d as output %d\n",
522 gpio, !pdata->vbus_pin_active_low[i]);
527 ohci_at91_usb_set_power(pdata, i, 1);
530 at91_for_each_port(i) {
531 if (!gpio_is_valid(pdata->overcurrent_pin[i]))
533 gpio = pdata->overcurrent_pin[i];
535 ret = gpio_request(gpio, "ohci_overcurrent");
538 "can't request overcurrent gpio %d\n",
543 ret = gpio_direction_input(gpio);
546 "can't configure overcurrent gpio %d as input\n",
552 ret = request_irq(gpio_to_irq(gpio),
553 ohci_hcd_at91_overcurrent_irq,
554 IRQF_SHARED, "ohci_overcurrent", pdev);
558 "can't get gpio IRQ for overcurrent\n");
563 device_init_wakeup(&pdev->dev, 1);
564 return usb_hcd_at91_probe(&ohci_at91_hc_driver, pdev);
567 static int ohci_hcd_at91_drv_remove(struct platform_device *pdev)
569 struct at91_usbh_data *pdata = dev_get_platdata(&pdev->dev);
573 at91_for_each_port(i) {
574 if (!gpio_is_valid(pdata->vbus_pin[i]))
576 ohci_at91_usb_set_power(pdata, i, 0);
577 gpio_free(pdata->vbus_pin[i]);
580 at91_for_each_port(i) {
581 if (!gpio_is_valid(pdata->overcurrent_pin[i]))
583 free_irq(gpio_to_irq(pdata->overcurrent_pin[i]), pdev);
584 gpio_free(pdata->overcurrent_pin[i]);
588 device_init_wakeup(&pdev->dev, 0);
589 usb_hcd_at91_remove(platform_get_drvdata(pdev), pdev);
596 ohci_hcd_at91_drv_suspend(struct platform_device *pdev, pm_message_t mesg)
598 struct usb_hcd *hcd = platform_get_drvdata(pdev);
599 struct ohci_hcd *ohci = hcd_to_ohci(hcd);
600 bool do_wakeup = device_may_wakeup(&pdev->dev);
604 enable_irq_wake(hcd->irq);
606 ret = ohci_suspend(hcd, do_wakeup);
608 disable_irq_wake(hcd->irq);
612 * The integrated transceivers seem unable to notice disconnect,
613 * reconnect, or wakeup without the 48 MHz clock active. so for
614 * correctness, always discard connection state (using reset).
616 * REVISIT: some boards will be able to turn VBUS off...
618 if (at91_suspend_entering_slow_clock()) {
619 ohci->hc_control = ohci_readl(ohci, &ohci->regs->control);
620 ohci->hc_control &= OHCI_CTRL_RWC;
621 ohci_writel(ohci, ohci->hc_control, &ohci->regs->control);
622 ohci->rh_state = OHCI_RH_HALTED;
624 /* flush the writes */
625 (void) ohci_readl (ohci, &ohci->regs->control);
632 static int ohci_hcd_at91_drv_resume(struct platform_device *pdev)
634 struct usb_hcd *hcd = platform_get_drvdata(pdev);
636 if (device_may_wakeup(&pdev->dev))
637 disable_irq_wake(hcd->irq);
642 ohci_resume(hcd, false);
646 #define ohci_hcd_at91_drv_suspend NULL
647 #define ohci_hcd_at91_drv_resume NULL
650 static struct platform_driver ohci_hcd_at91_driver = {
651 .probe = ohci_hcd_at91_drv_probe,
652 .remove = ohci_hcd_at91_drv_remove,
653 .shutdown = usb_hcd_platform_shutdown,
654 .suspend = ohci_hcd_at91_drv_suspend,
655 .resume = ohci_hcd_at91_drv_resume,
658 .of_match_table = of_match_ptr(at91_ohci_dt_ids),
662 static int __init ohci_at91_init(void)
667 pr_info("%s: " DRIVER_DESC "\n", hcd_name);
668 ohci_init_driver(&ohci_at91_hc_driver, NULL);
671 * The Atmel HW has some unusual quirks, which require Atmel-specific
672 * workarounds. We override certain hc_driver functions here to
673 * achieve that. We explicitly do not enhance ohci_driver_overrides to
674 * allow this more easily, since this is an unusual case, and we don't
675 * want to encourage others to override these functions by making it
679 ohci_at91_hc_driver.hub_status_data = ohci_at91_hub_status_data;
680 ohci_at91_hc_driver.hub_control = ohci_at91_hub_control;
682 return platform_driver_register(&ohci_hcd_at91_driver);
684 module_init(ohci_at91_init);
686 static void __exit ohci_at91_cleanup(void)
688 platform_driver_unregister(&ohci_hcd_at91_driver);
690 module_exit(ohci_at91_cleanup);
692 MODULE_DESCRIPTION(DRIVER_DESC);
693 MODULE_LICENSE("GPL");
694 MODULE_ALIAS("platform:at91_ohci");