1 // SPDX-License-Identifier: GPL-2.0 2 /** 3 * dwc3-keystone.c - Keystone Specific Glue layer 4 * 5 * Copyright (C) 2010-2013 Texas Instruments Incorporated - https://www.ti.com 6 * 7 * Author: WingMan Kwok <w-kwok2@ti.com> 8 */ 9 10 #include <linux/module.h> 11 #include <linux/kernel.h> 12 #include <linux/interrupt.h> 13 #include <linux/platform_device.h> 14 #include <linux/dma-mapping.h> 15 #include <linux/io.h> 16 #include <linux/of_platform.h> 17 #include <linux/phy/phy.h> 18 #include <linux/pm_runtime.h> 19 20 /* USBSS register offsets */ 21 #define USBSS_REVISION 0x0000 22 #define USBSS_SYSCONFIG 0x0010 23 #define USBSS_IRQ_EOI 0x0018 24 #define USBSS_IRQSTATUS_RAW_0 0x0020 25 #define USBSS_IRQSTATUS_0 0x0024 26 #define USBSS_IRQENABLE_SET_0 0x0028 27 #define USBSS_IRQENABLE_CLR_0 0x002c 28 29 /* IRQ register bits */ 30 #define USBSS_IRQ_EOI_LINE(n) BIT(n) 31 #define USBSS_IRQ_EVENT_ST BIT(0) 32 #define USBSS_IRQ_COREIRQ_EN BIT(0) 33 #define USBSS_IRQ_COREIRQ_CLR BIT(0) 34 35 struct dwc3_keystone { 36 struct device *dev; 37 void __iomem *usbss; 38 struct phy *usb3_phy; 39 }; 40 41 static inline u32 kdwc3_readl(void __iomem *base, u32 offset) 42 { 43 return readl(base + offset); 44 } 45 46 static inline void kdwc3_writel(void __iomem *base, u32 offset, u32 value) 47 { 48 writel(value, base + offset); 49 } 50 51 static void kdwc3_enable_irqs(struct dwc3_keystone *kdwc) 52 { 53 u32 val; 54 55 val = kdwc3_readl(kdwc->usbss, USBSS_IRQENABLE_SET_0); 56 val |= USBSS_IRQ_COREIRQ_EN; 57 kdwc3_writel(kdwc->usbss, USBSS_IRQENABLE_SET_0, val); 58 } 59 60 static void kdwc3_disable_irqs(struct dwc3_keystone *kdwc) 61 { 62 u32 val; 63 64 val = kdwc3_readl(kdwc->usbss, USBSS_IRQENABLE_SET_0); 65 val &= ~USBSS_IRQ_COREIRQ_EN; 66 kdwc3_writel(kdwc->usbss, USBSS_IRQENABLE_SET_0, val); 67 } 68 69 static irqreturn_t dwc3_keystone_interrupt(int irq, void *_kdwc) 70 { 71 struct dwc3_keystone *kdwc = _kdwc; 72 73 kdwc3_writel(kdwc->usbss, USBSS_IRQENABLE_CLR_0, USBSS_IRQ_COREIRQ_CLR); 74 kdwc3_writel(kdwc->usbss, USBSS_IRQSTATUS_0, USBSS_IRQ_EVENT_ST); 75 kdwc3_writel(kdwc->usbss, USBSS_IRQENABLE_SET_0, USBSS_IRQ_COREIRQ_EN); 76 kdwc3_writel(kdwc->usbss, USBSS_IRQ_EOI, USBSS_IRQ_EOI_LINE(0)); 77 78 return IRQ_HANDLED; 79 } 80 81 static int kdwc3_probe(struct platform_device *pdev) 82 { 83 struct device *dev = &pdev->dev; 84 struct device_node *node = pdev->dev.of_node; 85 struct dwc3_keystone *kdwc; 86 int error, irq; 87 88 kdwc = devm_kzalloc(dev, sizeof(*kdwc), GFP_KERNEL); 89 if (!kdwc) 90 return -ENOMEM; 91 92 platform_set_drvdata(pdev, kdwc); 93 94 kdwc->dev = dev; 95 96 kdwc->usbss = devm_platform_ioremap_resource(pdev, 0); 97 if (IS_ERR(kdwc->usbss)) 98 return PTR_ERR(kdwc->usbss); 99 100 /* PSC dependency on AM65 needs SERDES0 to be powered before USB0 */ 101 kdwc->usb3_phy = devm_phy_optional_get(dev, "usb3-phy"); 102 if (IS_ERR(kdwc->usb3_phy)) { 103 error = PTR_ERR(kdwc->usb3_phy); 104 if (error != -EPROBE_DEFER) 105 dev_err(dev, "couldn't get usb3 phy: %d\n", error); 106 107 return error; 108 } 109 110 phy_pm_runtime_get_sync(kdwc->usb3_phy); 111 112 error = phy_reset(kdwc->usb3_phy); 113 if (error < 0) { 114 dev_err(dev, "usb3 phy reset failed: %d\n", error); 115 return error; 116 } 117 118 error = phy_init(kdwc->usb3_phy); 119 if (error < 0) { 120 dev_err(dev, "usb3 phy init failed: %d\n", error); 121 return error; 122 } 123 124 error = phy_power_on(kdwc->usb3_phy); 125 if (error < 0) { 126 dev_err(dev, "usb3 phy power on failed: %d\n", error); 127 phy_exit(kdwc->usb3_phy); 128 return error; 129 } 130 131 pm_runtime_enable(kdwc->dev); 132 error = pm_runtime_get_sync(kdwc->dev); 133 if (error < 0) { 134 dev_err(kdwc->dev, "pm_runtime_get_sync failed, error %d\n", 135 error); 136 goto err_irq; 137 } 138 139 /* IRQ processing not required currently for AM65 */ 140 if (of_device_is_compatible(node, "ti,am654-dwc3")) 141 goto skip_irq; 142 143 irq = platform_get_irq(pdev, 0); 144 if (irq < 0) { 145 error = irq; 146 goto err_irq; 147 } 148 149 error = devm_request_irq(dev, irq, dwc3_keystone_interrupt, IRQF_SHARED, 150 dev_name(dev), kdwc); 151 if (error) { 152 dev_err(dev, "failed to request IRQ #%d --> %d\n", 153 irq, error); 154 goto err_irq; 155 } 156 157 kdwc3_enable_irqs(kdwc); 158 159 skip_irq: 160 error = of_platform_populate(node, NULL, NULL, dev); 161 if (error) { 162 dev_err(&pdev->dev, "failed to create dwc3 core\n"); 163 goto err_core; 164 } 165 166 return 0; 167 168 err_core: 169 kdwc3_disable_irqs(kdwc); 170 err_irq: 171 pm_runtime_put_sync(kdwc->dev); 172 pm_runtime_disable(kdwc->dev); 173 phy_power_off(kdwc->usb3_phy); 174 phy_exit(kdwc->usb3_phy); 175 phy_pm_runtime_put_sync(kdwc->usb3_phy); 176 177 return error; 178 } 179 180 static int kdwc3_remove_core(struct device *dev, void *c) 181 { 182 struct platform_device *pdev = to_platform_device(dev); 183 184 platform_device_unregister(pdev); 185 186 return 0; 187 } 188 189 static int kdwc3_remove(struct platform_device *pdev) 190 { 191 struct dwc3_keystone *kdwc = platform_get_drvdata(pdev); 192 struct device_node *node = pdev->dev.of_node; 193 194 if (!of_device_is_compatible(node, "ti,am654-dwc3")) 195 kdwc3_disable_irqs(kdwc); 196 197 device_for_each_child(&pdev->dev, NULL, kdwc3_remove_core); 198 pm_runtime_put_sync(kdwc->dev); 199 pm_runtime_disable(kdwc->dev); 200 201 phy_power_off(kdwc->usb3_phy); 202 phy_exit(kdwc->usb3_phy); 203 phy_pm_runtime_put_sync(kdwc->usb3_phy); 204 205 platform_set_drvdata(pdev, NULL); 206 207 return 0; 208 } 209 210 static const struct of_device_id kdwc3_of_match[] = { 211 { .compatible = "ti,keystone-dwc3", }, 212 { .compatible = "ti,am654-dwc3" }, 213 {}, 214 }; 215 MODULE_DEVICE_TABLE(of, kdwc3_of_match); 216 217 static struct platform_driver kdwc3_driver = { 218 .probe = kdwc3_probe, 219 .remove = kdwc3_remove, 220 .driver = { 221 .name = "keystone-dwc3", 222 .of_match_table = kdwc3_of_match, 223 }, 224 }; 225 226 module_platform_driver(kdwc3_driver); 227 228 MODULE_ALIAS("platform:keystone-dwc3"); 229 MODULE_AUTHOR("WingMan Kwok <w-kwok2@ti.com>"); 230 MODULE_LICENSE("GPL v2"); 231 MODULE_DESCRIPTION("DesignWare USB3 KEYSTONE Glue Layer"); 232