^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright (c) 2008 Sascha Hauer <s.hauer@pengutronix.de>, Pengutronix
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Copyright (c) 2009 Daniel Mack <daniel@caiaq.de>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/io.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/clk.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/delay.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/usb/otg.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/usb/ulpi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/usb.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/usb/hcd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/platform_data/usb-ehci-mxc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include "ehci.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #define DRIVER_DESC "Freescale On-Chip EHCI Host driver"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) static const char hcd_name[] = "ehci-mxc";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #define ULPI_VIEWPORT_OFFSET 0x170
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) struct ehci_mxc_priv {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) struct clk *usbclk, *ahbclk, *phyclk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) static struct hc_driver __read_mostly ehci_mxc_hc_driver;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) static const struct ehci_driver_overrides ehci_mxc_overrides __initconst = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) .extra_priv_size = sizeof(struct ehci_mxc_priv),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) static int ehci_mxc_drv_probe(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) struct device *dev = &pdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) struct mxc_usbh_platform_data *pdata = dev_get_platdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) struct usb_hcd *hcd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) struct resource *res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) int irq, ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) struct ehci_mxc_priv *priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) struct ehci_hcd *ehci;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) if (!pdata) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) dev_err(dev, "No platform data given, bailing out.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) irq = platform_get_irq(pdev, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) if (irq < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) return irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) hcd = usb_create_hcd(&ehci_mxc_hc_driver, dev, dev_name(dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) if (!hcd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) hcd->regs = devm_ioremap_resource(dev, res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) if (IS_ERR(hcd->regs)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) ret = PTR_ERR(hcd->regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) goto err_alloc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) hcd->rsrc_start = res->start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) hcd->rsrc_len = resource_size(res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) hcd->has_tt = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) ehci = hcd_to_ehci(hcd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) priv = (struct ehci_mxc_priv *) ehci->priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) /* enable clocks */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) priv->usbclk = devm_clk_get(dev, "ipg");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) if (IS_ERR(priv->usbclk)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) ret = PTR_ERR(priv->usbclk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) goto err_alloc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) clk_prepare_enable(priv->usbclk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) priv->ahbclk = devm_clk_get(dev, "ahb");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) if (IS_ERR(priv->ahbclk)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) ret = PTR_ERR(priv->ahbclk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) goto err_clk_ahb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) clk_prepare_enable(priv->ahbclk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) /* "dr" device has its own clock on i.MX51 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) priv->phyclk = devm_clk_get(dev, "phy");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) if (IS_ERR(priv->phyclk))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) priv->phyclk = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) if (priv->phyclk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) clk_prepare_enable(priv->phyclk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) /* call platform specific init function */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) if (pdata->init) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) ret = pdata->init(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) dev_err(dev, "platform init failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) goto err_init;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) /* platforms need some time to settle changed IO settings */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) mdelay(10);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) /* EHCI registers start at offset 0x100 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) ehci->caps = hcd->regs + 0x100;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) ehci->regs = hcd->regs + 0x100 +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) HC_LENGTH(ehci, ehci_readl(ehci, &ehci->caps->hc_capbase));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) /* set up the PORTSCx register */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) ehci_writel(ehci, pdata->portsc, &ehci->regs->port_status[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) /* is this really needed? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) msleep(10);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) /* Initialize the transceiver */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) if (pdata->otg) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) pdata->otg->io_priv = hcd->regs + ULPI_VIEWPORT_OFFSET;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) ret = usb_phy_init(pdata->otg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) dev_err(dev, "unable to init transceiver, probably missing\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) ret = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) goto err_add;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) ret = otg_set_vbus(pdata->otg->otg, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) dev_err(dev, "unable to enable vbus on transceiver\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) goto err_add;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) platform_set_drvdata(pdev, hcd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) ret = usb_add_hcd(hcd, irq, IRQF_SHARED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) goto err_add;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) device_wakeup_enable(hcd->self.controller);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) err_add:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) if (pdata && pdata->exit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) pdata->exit(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) err_init:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) if (priv->phyclk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) clk_disable_unprepare(priv->phyclk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) clk_disable_unprepare(priv->ahbclk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) err_clk_ahb:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) clk_disable_unprepare(priv->usbclk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) err_alloc:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) usb_put_hcd(hcd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) static int ehci_mxc_drv_remove(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) struct mxc_usbh_platform_data *pdata = dev_get_platdata(&pdev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) struct usb_hcd *hcd = platform_get_drvdata(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) struct ehci_hcd *ehci = hcd_to_ehci(hcd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) struct ehci_mxc_priv *priv = (struct ehci_mxc_priv *) ehci->priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) usb_remove_hcd(hcd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) if (pdata && pdata->exit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) pdata->exit(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) if (pdata && pdata->otg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) usb_phy_shutdown(pdata->otg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) clk_disable_unprepare(priv->usbclk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) clk_disable_unprepare(priv->ahbclk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) if (priv->phyclk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) clk_disable_unprepare(priv->phyclk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) usb_put_hcd(hcd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) MODULE_ALIAS("platform:mxc-ehci");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) static struct platform_driver ehci_mxc_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) .probe = ehci_mxc_drv_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) .remove = ehci_mxc_drv_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) .shutdown = usb_hcd_platform_shutdown,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) .driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) .name = "mxc-ehci",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) static int __init ehci_mxc_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) if (usb_disabled())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) pr_info("%s: " DRIVER_DESC "\n", hcd_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) ehci_init_driver(&ehci_mxc_hc_driver, &ehci_mxc_overrides);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) return platform_driver_register(&ehci_mxc_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) module_init(ehci_mxc_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) static void __exit ehci_mxc_cleanup(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) platform_driver_unregister(&ehci_mxc_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) module_exit(ehci_mxc_cleanup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) MODULE_DESCRIPTION(DRIVER_DESC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) MODULE_AUTHOR("Sascha Hauer");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) MODULE_LICENSE("GPL");