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/mfd/syscon.h>
25 #include <linux/regmap.h>
26 #include <linux/usb.h>
27 #include <linux/usb/hcd.h>
28 #include <soc/at91/at91_sfr.h>
32 #define valid_port(index) ((index) >= 0 && (index) < AT91_MAX_USBH_PORTS)
33 #define at91_for_each_port(index) \
34 for ((index) = 0; (index) < AT91_MAX_USBH_PORTS; (index)++)
36 /* interface, function and usb clocks; sometimes also an AHB clock */
37 #define hcd_to_ohci_at91_priv(h) \
38 ((struct ohci_at91_priv *)hcd_to_ohci(h)->priv)
40 #define AT91_MAX_USBH_PORTS 3
41 struct at91_usbh_data {
42 int vbus_pin[AT91_MAX_USBH_PORTS]; /* port power-control pin */
43 int overcurrent_pin[AT91_MAX_USBH_PORTS];
44 u8 ports; /* number of ports on root hub */
45 u8 overcurrent_supported;
46 u8 vbus_pin_active_low[AT91_MAX_USBH_PORTS];
47 u8 overcurrent_status[AT91_MAX_USBH_PORTS];
48 u8 overcurrent_changed[AT91_MAX_USBH_PORTS];
51 struct ohci_at91_caps {
55 struct ohci_at91_priv {
60 bool wakeup; /* Saved wake-up state for resume */
61 const struct ohci_at91_caps *caps;
62 struct regmap *sfr_regmap;
64 /* interface and function clocks; sometimes also an AHB clock */
66 #define DRIVER_DESC "OHCI Atmel driver"
68 static const char hcd_name[] = "ohci-atmel";
70 static struct hc_driver __read_mostly ohci_at91_hc_driver;
72 static const struct ohci_driver_overrides ohci_at91_drv_overrides __initconst = {
73 .extra_priv_size = sizeof(struct ohci_at91_priv),
76 extern int usb_disabled(void);
78 /*-------------------------------------------------------------------------*/
80 static void at91_start_clock(struct ohci_at91_priv *ohci_at91)
82 if (ohci_at91->clocked)
85 clk_set_rate(ohci_at91->fclk, 48000000);
86 clk_prepare_enable(ohci_at91->hclk);
87 clk_prepare_enable(ohci_at91->iclk);
88 clk_prepare_enable(ohci_at91->fclk);
89 ohci_at91->clocked = true;
92 static void at91_stop_clock(struct ohci_at91_priv *ohci_at91)
94 if (!ohci_at91->clocked)
97 clk_disable_unprepare(ohci_at91->fclk);
98 clk_disable_unprepare(ohci_at91->iclk);
99 clk_disable_unprepare(ohci_at91->hclk);
100 ohci_at91->clocked = false;
103 static void at91_start_hc(struct platform_device *pdev)
105 struct usb_hcd *hcd = platform_get_drvdata(pdev);
106 struct ohci_regs __iomem *regs = hcd->regs;
107 struct ohci_at91_priv *ohci_at91 = hcd_to_ohci_at91_priv(hcd);
109 dev_dbg(&pdev->dev, "start\n");
112 * Start the USB clocks.
114 at91_start_clock(ohci_at91);
117 * The USB host controller must remain in reset.
119 writel(0, ®s->control);
122 static void at91_stop_hc(struct platform_device *pdev)
124 struct usb_hcd *hcd = platform_get_drvdata(pdev);
125 struct ohci_regs __iomem *regs = hcd->regs;
126 struct ohci_at91_priv *ohci_at91 = hcd_to_ohci_at91_priv(hcd);
128 dev_dbg(&pdev->dev, "stop\n");
131 * Put the USB host controller into reset.
133 writel(0, ®s->control);
136 * Stop the USB clocks.
138 at91_stop_clock(ohci_at91);
142 /*-------------------------------------------------------------------------*/
144 struct regmap *at91_dt_syscon_sfr(void)
146 struct regmap *regmap;
148 regmap = syscon_regmap_lookup_by_compatible("atmel,sama5d2-sfr");
155 static void usb_hcd_at91_remove (struct usb_hcd *, struct platform_device *);
157 /* configure so an HC device and id are always provided */
158 /* always called with process context; sleeping is OK */
162 * usb_hcd_at91_probe - initialize AT91-based HCDs
163 * Context: !in_interrupt()
165 * Allocates basic resources for this USB host controller, and
166 * then invokes the start() method for the HCD associated with it
167 * through the hotplug entry's driver_data.
169 static int usb_hcd_at91_probe(const struct hc_driver *driver,
170 struct platform_device *pdev)
172 struct at91_usbh_data *board;
173 struct ohci_hcd *ohci;
176 struct ohci_at91_priv *ohci_at91;
177 struct device *dev = &pdev->dev;
178 struct resource *res;
181 irq = platform_get_irq(pdev, 0);
183 dev_dbg(dev, "hcd probe: missing irq resource\n");
187 hcd = usb_create_hcd(driver, dev, "at91");
190 ohci_at91 = hcd_to_ohci_at91_priv(hcd);
192 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
193 hcd->regs = devm_ioremap_resource(dev, res);
194 if (IS_ERR(hcd->regs)) {
195 retval = PTR_ERR(hcd->regs);
198 hcd->rsrc_start = res->start;
199 hcd->rsrc_len = resource_size(res);
201 ohci_at91->iclk = devm_clk_get(dev, "ohci_clk");
202 if (IS_ERR(ohci_at91->iclk)) {
203 dev_err(dev, "failed to get ohci_clk\n");
204 retval = PTR_ERR(ohci_at91->iclk);
207 ohci_at91->fclk = devm_clk_get(dev, "uhpck");
208 if (IS_ERR(ohci_at91->fclk)) {
209 dev_err(dev, "failed to get uhpck\n");
210 retval = PTR_ERR(ohci_at91->fclk);
213 ohci_at91->hclk = devm_clk_get(dev, "hclk");
214 if (IS_ERR(ohci_at91->hclk)) {
215 dev_err(dev, "failed to get hclk\n");
216 retval = PTR_ERR(ohci_at91->hclk);
220 ohci_at91->caps = (const struct ohci_at91_caps *)
221 of_device_get_match_data(&pdev->dev);
222 if (!ohci_at91->caps)
225 if (ohci_at91->caps->suspend_ctrl) {
226 ohci_at91->sfr_regmap = at91_dt_syscon_sfr();
227 if (!ohci_at91->sfr_regmap)
228 dev_warn(dev, "failed to find sfr node\n");
231 board = hcd->self.controller->platform_data;
232 ohci = hcd_to_ohci(hcd);
233 ohci->num_ports = board->ports;
236 retval = usb_add_hcd(hcd, irq, IRQF_SHARED);
238 device_wakeup_enable(hcd->self.controller);
251 /* may be called with controller, bus, and devices active */
254 * usb_hcd_at91_remove - shutdown processing for AT91-based HCDs
255 * @dev: USB Host Controller being removed
256 * Context: !in_interrupt()
258 * Reverses the effect of usb_hcd_at91_probe(), first invoking
259 * the HCD's stop() method. It is always called from a thread
260 * context, "rmmod" or something similar.
263 static void usb_hcd_at91_remove(struct usb_hcd *hcd,
264 struct platform_device *pdev)
271 /*-------------------------------------------------------------------------*/
272 static void ohci_at91_usb_set_power(struct at91_usbh_data *pdata, int port, int enable)
274 if (!valid_port(port))
277 if (!gpio_is_valid(pdata->vbus_pin[port]))
280 gpio_set_value(pdata->vbus_pin[port],
281 pdata->vbus_pin_active_low[port] ^ enable);
284 static int ohci_at91_usb_get_power(struct at91_usbh_data *pdata, int port)
286 if (!valid_port(port))
289 if (!gpio_is_valid(pdata->vbus_pin[port]))
292 return gpio_get_value(pdata->vbus_pin[port]) ^
293 pdata->vbus_pin_active_low[port];
297 * Update the status data from the hub with the over-current indicator change.
299 static int ohci_at91_hub_status_data(struct usb_hcd *hcd, char *buf)
301 struct at91_usbh_data *pdata = hcd->self.controller->platform_data;
302 int length = ohci_hub_status_data(hcd, buf);
305 at91_for_each_port(port) {
306 if (pdata->overcurrent_changed[port]) {
309 buf[0] |= 1 << (port + 1);
317 * Look at the control requests to the root hub and see if we need to override.
319 static int ohci_at91_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
320 u16 wIndex, char *buf, u16 wLength)
322 struct at91_usbh_data *pdata = dev_get_platdata(hcd->self.controller);
323 struct usb_hub_descriptor *desc;
325 u32 *data = (u32 *)buf;
327 dev_dbg(hcd->self.controller,
328 "ohci_at91_hub_control(%p,0x%04x,0x%04x,0x%04x,%p,%04x)\n",
329 hcd, typeReq, wValue, wIndex, buf, wLength);
335 if (wValue == USB_PORT_FEAT_POWER) {
336 dev_dbg(hcd->self.controller, "SetPortFeat: POWER\n");
337 if (valid_port(wIndex)) {
338 ohci_at91_usb_set_power(pdata, wIndex, 1);
346 case ClearPortFeature:
348 case USB_PORT_FEAT_C_OVER_CURRENT:
349 dev_dbg(hcd->self.controller,
350 "ClearPortFeature: C_OVER_CURRENT\n");
352 if (valid_port(wIndex)) {
353 pdata->overcurrent_changed[wIndex] = 0;
354 pdata->overcurrent_status[wIndex] = 0;
359 case USB_PORT_FEAT_OVER_CURRENT:
360 dev_dbg(hcd->self.controller,
361 "ClearPortFeature: OVER_CURRENT\n");
363 if (valid_port(wIndex))
364 pdata->overcurrent_status[wIndex] = 0;
368 case USB_PORT_FEAT_POWER:
369 dev_dbg(hcd->self.controller,
370 "ClearPortFeature: POWER\n");
372 if (valid_port(wIndex)) {
373 ohci_at91_usb_set_power(pdata, wIndex, 0);
380 ret = ohci_hub_control(hcd, typeReq, wValue, wIndex + 1, buf, wLength);
385 case GetHubDescriptor:
387 /* update the hub's descriptor */
389 desc = (struct usb_hub_descriptor *)buf;
391 dev_dbg(hcd->self.controller, "wHubCharacteristics 0x%04x\n",
392 desc->wHubCharacteristics);
394 /* remove the old configurations for power-switching, and
395 * over-current protection, and insert our new configuration
398 desc->wHubCharacteristics &= ~cpu_to_le16(HUB_CHAR_LPSM);
399 desc->wHubCharacteristics |=
400 cpu_to_le16(HUB_CHAR_INDV_PORT_LPSM);
402 if (pdata->overcurrent_supported) {
403 desc->wHubCharacteristics &= ~cpu_to_le16(HUB_CHAR_OCPM);
404 desc->wHubCharacteristics |=
405 cpu_to_le16(HUB_CHAR_INDV_PORT_OCPM);
408 dev_dbg(hcd->self.controller, "wHubCharacteristics after 0x%04x\n",
409 desc->wHubCharacteristics);
414 /* check port status */
416 dev_dbg(hcd->self.controller, "GetPortStatus(%d)\n", wIndex);
418 if (valid_port(wIndex)) {
419 if (!ohci_at91_usb_get_power(pdata, wIndex))
420 *data &= ~cpu_to_le32(RH_PS_PPS);
422 if (pdata->overcurrent_changed[wIndex])
423 *data |= cpu_to_le32(RH_PS_OCIC);
425 if (pdata->overcurrent_status[wIndex])
426 *data |= cpu_to_le32(RH_PS_POCI);
434 /*-------------------------------------------------------------------------*/
436 static irqreturn_t ohci_hcd_at91_overcurrent_irq(int irq, void *data)
438 struct platform_device *pdev = data;
439 struct at91_usbh_data *pdata = dev_get_platdata(&pdev->dev);
442 /* From the GPIO notifying the over-current situation, find
443 * out the corresponding port */
444 at91_for_each_port(port) {
445 if (gpio_is_valid(pdata->overcurrent_pin[port]) &&
446 gpio_to_irq(pdata->overcurrent_pin[port]) == irq) {
447 gpio = pdata->overcurrent_pin[port];
452 if (port == AT91_MAX_USBH_PORTS) {
453 dev_err(& pdev->dev, "overcurrent interrupt from unknown GPIO\n");
457 val = gpio_get_value(gpio);
459 /* When notified of an over-current situation, disable power
460 on the corresponding port, and mark this port in
463 ohci_at91_usb_set_power(pdata, port, 0);
464 pdata->overcurrent_status[port] = 1;
465 pdata->overcurrent_changed[port] = 1;
468 dev_dbg(& pdev->dev, "overcurrent situation %s\n",
469 val ? "exited" : "notified");
474 static const struct ohci_at91_caps at91rm9200_caps = {
475 .suspend_ctrl = false,
478 static const struct ohci_at91_caps sama5d2_caps = {
479 .suspend_ctrl = true,
482 static const struct of_device_id at91_ohci_dt_ids[] = {
483 { .compatible = "atmel,at91rm9200-ohci", .data = &at91rm9200_caps },
484 { .compatible = "atmel,sama5d2-ohci", .data = &sama5d2_caps },
488 MODULE_DEVICE_TABLE(of, at91_ohci_dt_ids);
490 /*-------------------------------------------------------------------------*/
492 static int ohci_hcd_at91_drv_probe(struct platform_device *pdev)
494 struct device_node *np = pdev->dev.of_node;
495 struct at91_usbh_data *pdata;
499 enum of_gpio_flags flags;
502 /* Right now device-tree probed devices don't get dma_mask set.
503 * Since shared usb code relies on it, set it here for now.
504 * Once we have dma capability bindings this can go away.
506 ret = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
510 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
514 pdev->dev.platform_data = pdata;
516 if (!of_property_read_u32(np, "num-ports", &ports))
517 pdata->ports = ports;
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 gpio = of_get_named_gpio_flags(np, "atmel,vbus-gpio", i,
532 pdata->vbus_pin[i] = gpio;
533 if (!gpio_is_valid(gpio))
535 pdata->vbus_pin_active_low[i] = flags & OF_GPIO_ACTIVE_LOW;
537 ret = gpio_request(gpio, "ohci_vbus");
540 "can't request vbus gpio %d\n", gpio);
543 ret = gpio_direction_output(gpio,
544 !pdata->vbus_pin_active_low[i]);
547 "can't put vbus gpio %d as output %d\n",
548 gpio, !pdata->vbus_pin_active_low[i]);
553 ohci_at91_usb_set_power(pdata, i, 1);
556 at91_for_each_port(i) {
557 if (i >= pdata->ports)
560 pdata->overcurrent_pin[i] =
561 of_get_named_gpio_flags(np, "atmel,oc-gpio", i, &flags);
563 if (!gpio_is_valid(pdata->overcurrent_pin[i]))
565 gpio = pdata->overcurrent_pin[i];
567 ret = gpio_request(gpio, "ohci_overcurrent");
570 "can't request overcurrent gpio %d\n",
575 ret = gpio_direction_input(gpio);
578 "can't configure overcurrent gpio %d as input\n",
584 ret = request_irq(gpio_to_irq(gpio),
585 ohci_hcd_at91_overcurrent_irq,
586 IRQF_SHARED, "ohci_overcurrent", pdev);
590 "can't get gpio IRQ for overcurrent\n");
594 device_init_wakeup(&pdev->dev, 1);
595 return usb_hcd_at91_probe(&ohci_at91_hc_driver, pdev);
598 static int ohci_hcd_at91_drv_remove(struct platform_device *pdev)
600 struct at91_usbh_data *pdata = dev_get_platdata(&pdev->dev);
604 at91_for_each_port(i) {
605 if (!gpio_is_valid(pdata->vbus_pin[i]))
607 ohci_at91_usb_set_power(pdata, i, 0);
608 gpio_free(pdata->vbus_pin[i]);
611 at91_for_each_port(i) {
612 if (!gpio_is_valid(pdata->overcurrent_pin[i]))
614 free_irq(gpio_to_irq(pdata->overcurrent_pin[i]), pdev);
615 gpio_free(pdata->overcurrent_pin[i]);
619 device_init_wakeup(&pdev->dev, 0);
620 usb_hcd_at91_remove(platform_get_drvdata(pdev), pdev);
624 static int ohci_at91_port_ctrl(struct regmap *regmap, bool enable)
632 ret = regmap_read(regmap, SFR_OHCIICR, ®val);
637 regval &= ~SFR_OHCIICR_USB_SUSPEND;
639 regval |= SFR_OHCIICR_USB_SUSPEND;
641 regmap_write(regmap, SFR_OHCIICR, regval);
646 static int ohci_at91_port_suspend(struct regmap *regmap)
648 return ohci_at91_port_ctrl(regmap, false);
651 static int ohci_at91_port_resume(struct regmap *regmap)
653 return ohci_at91_port_ctrl(regmap, true);
656 static int __maybe_unused
657 ohci_hcd_at91_drv_suspend(struct device *dev)
659 struct usb_hcd *hcd = dev_get_drvdata(dev);
660 struct ohci_hcd *ohci = hcd_to_ohci(hcd);
661 struct ohci_at91_priv *ohci_at91 = hcd_to_ohci_at91_priv(hcd);
665 * Disable wakeup if we are going to sleep with slow clock mode
668 ohci_at91->wakeup = device_may_wakeup(dev)
669 && !at91_suspend_entering_slow_clock();
671 if (ohci_at91->wakeup)
672 enable_irq_wake(hcd->irq);
674 ret = ohci_suspend(hcd, ohci_at91->wakeup);
676 if (ohci_at91->wakeup)
677 disable_irq_wake(hcd->irq);
681 * The integrated transceivers seem unable to notice disconnect,
682 * reconnect, or wakeup without the 48 MHz clock active. so for
683 * correctness, always discard connection state (using reset).
685 * REVISIT: some boards will be able to turn VBUS off...
687 if (!ohci_at91->wakeup) {
688 ohci->hc_control = ohci_readl(ohci, &ohci->regs->control);
689 ohci->hc_control &= OHCI_CTRL_RWC;
690 ohci_writel(ohci, ohci->hc_control, &ohci->regs->control);
691 ohci->rh_state = OHCI_RH_HALTED;
693 if (ohci_at91->caps->suspend_ctrl)
694 ohci_at91_port_suspend(ohci_at91->sfr_regmap);
696 /* flush the writes */
697 (void) ohci_readl (ohci, &ohci->regs->control);
698 at91_stop_clock(ohci_at91);
704 static int __maybe_unused
705 ohci_hcd_at91_drv_resume(struct device *dev)
707 struct usb_hcd *hcd = dev_get_drvdata(dev);
708 struct ohci_at91_priv *ohci_at91 = hcd_to_ohci_at91_priv(hcd);
710 if (ohci_at91->wakeup)
711 disable_irq_wake(hcd->irq);
713 at91_start_clock(ohci_at91);
715 if (ohci_at91->caps->suspend_ctrl)
716 ohci_at91_port_resume(ohci_at91->sfr_regmap);
718 ohci_resume(hcd, false);
722 static SIMPLE_DEV_PM_OPS(ohci_hcd_at91_pm_ops, ohci_hcd_at91_drv_suspend,
723 ohci_hcd_at91_drv_resume);
725 static struct platform_driver ohci_hcd_at91_driver = {
726 .probe = ohci_hcd_at91_drv_probe,
727 .remove = ohci_hcd_at91_drv_remove,
728 .shutdown = usb_hcd_platform_shutdown,
731 .pm = &ohci_hcd_at91_pm_ops,
732 .of_match_table = at91_ohci_dt_ids,
736 static int __init ohci_at91_init(void)
741 pr_info("%s: " DRIVER_DESC "\n", hcd_name);
742 ohci_init_driver(&ohci_at91_hc_driver, &ohci_at91_drv_overrides);
745 * The Atmel HW has some unusual quirks, which require Atmel-specific
746 * workarounds. We override certain hc_driver functions here to
747 * achieve that. We explicitly do not enhance ohci_driver_overrides to
748 * allow this more easily, since this is an unusual case, and we don't
749 * want to encourage others to override these functions by making it
753 ohci_at91_hc_driver.hub_status_data = ohci_at91_hub_status_data;
754 ohci_at91_hc_driver.hub_control = ohci_at91_hub_control;
756 return platform_driver_register(&ohci_hcd_at91_driver);
758 module_init(ohci_at91_init);
760 static void __exit ohci_at91_cleanup(void)
762 platform_driver_unregister(&ohci_hcd_at91_driver);
764 module_exit(ohci_at91_cleanup);
766 MODULE_DESCRIPTION(DRIVER_DESC);
767 MODULE_LICENSE("GPL");
768 MODULE_ALIAS("platform:at91_ohci");