^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) * Nuvoton NPCM7xx driver for EHCI HCD
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 2018 Nuvoton Technologies,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Avi Fishman <avi.fishman@nuvoton.com> <avifishman70@gmail.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Tomer Maimon <tomer.maimon@nuvoton.com> <tmaimon77@gmail.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * Based on various ehci-spear.c driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/dma-mapping.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/of.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/pm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/usb.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <linux/usb/hcd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include "ehci.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #include <linux/regmap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #include <linux/mfd/syscon.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #define DRIVER_DESC "EHCI npcm7xx driver"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) static const char hcd_name[] = "npcm7xx-ehci";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) #define USB2PHYCTL_OFFSET 0x144
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) #define IPSRST2_OFFSET 0x24
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) #define IPSRST3_OFFSET 0x34
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) static struct hc_driver __read_mostly ehci_npcm7xx_hc_driver;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) static int __maybe_unused ehci_npcm7xx_drv_suspend(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) struct usb_hcd *hcd = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) bool do_wakeup = device_may_wakeup(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) return ehci_suspend(hcd, do_wakeup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) static int __maybe_unused ehci_npcm7xx_drv_resume(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) struct usb_hcd *hcd = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) ehci_resume(hcd, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) static SIMPLE_DEV_PM_OPS(ehci_npcm7xx_pm_ops, ehci_npcm7xx_drv_suspend,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) ehci_npcm7xx_drv_resume);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) static int npcm7xx_ehci_hcd_drv_probe(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) struct usb_hcd *hcd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) struct resource *res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) struct regmap *gcr_regmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) struct regmap *rst_regmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) const struct hc_driver *driver = &ehci_npcm7xx_hc_driver;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) int irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) int retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) dev_dbg(&pdev->dev, "initializing npcm7xx ehci USB Controller\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) gcr_regmap = syscon_regmap_lookup_by_compatible("nuvoton,npcm750-gcr");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) if (IS_ERR(gcr_regmap)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) dev_err(&pdev->dev, "%s: failed to find nuvoton,npcm750-gcr\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) return PTR_ERR(gcr_regmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) rst_regmap = syscon_regmap_lookup_by_compatible("nuvoton,npcm750-rst");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) if (IS_ERR(rst_regmap)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) dev_err(&pdev->dev, "%s: failed to find nuvoton,npcm750-rst\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) return PTR_ERR(rst_regmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) /********* phy init ******/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) // reset usb host
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) regmap_update_bits(rst_regmap, IPSRST2_OFFSET,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) (0x1 << 26), (0x1 << 26));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) regmap_update_bits(rst_regmap, IPSRST3_OFFSET,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) (0x1 << 25), (0x1 << 25));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) regmap_update_bits(gcr_regmap, USB2PHYCTL_OFFSET,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) (0x1 << 28), 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) udelay(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) // enable phy
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) regmap_update_bits(rst_regmap, IPSRST3_OFFSET,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) (0x1 << 25), 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) udelay(50); // enable phy
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) regmap_update_bits(gcr_regmap, USB2PHYCTL_OFFSET,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) (0x1 << 28), (0x1 << 28));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) // enable host
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) regmap_update_bits(rst_regmap, IPSRST2_OFFSET,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) (0x1 << 26), 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) if (usb_disabled())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) irq = platform_get_irq(pdev, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) if (irq < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) retval = irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) * Right now device-tree probed devices don't get dma_mask set.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) * Since shared usb code relies on it, set it here for now.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) * Once we have dma capability bindings this can go away.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) retval = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) hcd = usb_create_hcd(driver, &pdev->dev, dev_name(&pdev->dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) if (!hcd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) retval = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) goto fail;
^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) res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) hcd->regs = devm_ioremap_resource(&pdev->dev, res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) if (IS_ERR(hcd->regs)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) retval = PTR_ERR(hcd->regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) goto err_put_hcd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) hcd->rsrc_start = res->start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) hcd->rsrc_len = resource_size(res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) /* registers start at offset 0x0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) hcd_to_ehci(hcd)->caps = hcd->regs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) retval = usb_add_hcd(hcd, irq, IRQF_SHARED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) goto err_put_hcd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) device_wakeup_enable(hcd->self.controller);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) err_put_hcd:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) usb_put_hcd(hcd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) dev_err(&pdev->dev, "init fail, %d\n", retval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) static int npcm7xx_ehci_hcd_drv_remove(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) struct usb_hcd *hcd = platform_get_drvdata(pdev);
^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) usb_put_hcd(hcd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) static const struct of_device_id npcm7xx_ehci_id_table[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) { .compatible = "nuvoton,npcm750-ehci" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) { },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) MODULE_DEVICE_TABLE(of, npcm7xx_ehci_id_table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) static struct platform_driver npcm7xx_ehci_hcd_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) .probe = npcm7xx_ehci_hcd_drv_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) .remove = npcm7xx_ehci_hcd_drv_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) .shutdown = usb_hcd_platform_shutdown,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) .driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) .name = "npcm7xx-ehci",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) .bus = &platform_bus_type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) .pm = pm_ptr(&ehci_npcm7xx_pm_ops),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) .of_match_table = npcm7xx_ehci_id_table,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) static int __init ehci_npcm7xx_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) if (usb_disabled())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) pr_info("%s: " DRIVER_DESC "\n", hcd_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) ehci_init_driver(&ehci_npcm7xx_hc_driver, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) return platform_driver_register(&npcm7xx_ehci_hcd_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) module_init(ehci_npcm7xx_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) static void __exit ehci_npcm7xx_cleanup(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) platform_driver_unregister(&npcm7xx_ehci_hcd_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) module_exit(ehci_npcm7xx_cleanup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) MODULE_DESCRIPTION(DRIVER_DESC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) MODULE_ALIAS("platform:npcm7xx-ehci");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) MODULE_AUTHOR("Avi Fishman");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) MODULE_LICENSE("GPL v2");