b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame^] | 1 | /* |
| 2 | * Base driver for ASR USB |
| 3 | * |
| 4 | * Copyright 2021 ASR Microelectronics (Shanghai) Co., Ltd. |
| 5 | * |
| 6 | * This file is subject to the terms and conditions of the GNU General |
| 7 | * Public License. See the file "COPYING" in the main directory of this |
| 8 | * archive for more details. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 18 | */ |
| 19 | |
| 20 | |
| 21 | #include <linux/clk.h> |
| 22 | #include <linux/err.h> |
| 23 | #include <linux/io.h> |
| 24 | #include <linux/module.h> |
| 25 | #include <linux/of.h> |
| 26 | #include <linux/of_platform.h> |
| 27 | #include <linux/platform_device.h> |
| 28 | #include <linux/kernel.h> |
| 29 | #include <linux/platform_data/mv_usb.h> |
| 30 | #include <linux/irq.h> |
| 31 | #include <linux/interrupt.h> |
| 32 | #include <soc/asr/regs-addr.h> |
| 33 | #include <linux/delay.h> |
| 34 | #include <linux/cputype.h> |
| 35 | #include <linux/usb/phy.h> |
| 36 | |
| 37 | #include "core.h" |
| 38 | |
| 39 | #define USB_HANDLE_TIME_MSEC (5000) |
| 40 | #ifdef CONFIG_CPU_ASR1901 |
| 41 | #define APMU_USB_WAKE_CLR (0x07c) |
| 42 | #define USB_WAKE_INT_EN (0x1 << 15) |
| 43 | #define USB_WAKE_PMU_EN (0x1 << 14) |
| 44 | |
| 45 | #define USB_VBUS_WAKE_EN (0x1 << 10) |
| 46 | #define USB_ID_WAKE_EN (0x1 << 12) |
| 47 | #define USB_LINEST_WAKE_EN ((0x1 << 8) | (0x1 << 9)) |
| 48 | #define USB_RXELEC_WAKE_EN (0x1 << 11) |
| 49 | |
| 50 | #define USB_VBUS_WAKE_CLR (0x1 << 18) |
| 51 | #define USB_ID_WAKE_CLR (0x1 << 20) |
| 52 | #define USB_LINEST_WAKE_CLR ((0x1 << 16) | (0x1 << 17)) |
| 53 | #define USB_RXELEC_WAKE_CLR (0x1 << 19) |
| 54 | #elif defined(CONFIG_CPU_ASR1903) |
| 55 | #define APMU_USB_WAKE_CLR (0x07c) |
| 56 | |
| 57 | #define USB_WAKE_INT_EN (0x1 << 29) |
| 58 | #define USB_WAKE_PMU_EN (0x1 << 29) |
| 59 | |
| 60 | #define USB_VBUS_WAKE_EN (0x1 << 11) |
| 61 | #define USB_ID_WAKE_EN (0x1 << 22) |
| 62 | #define USB_LINEST_WAKE_EN ((0x1 << 9) | (0x1 << 10)) |
| 63 | #define USB_RXELEC_WAKE_EN ((0x1 << 9) | (0x1 << 10)) |
| 64 | |
| 65 | #define USB_VBUS_WAKE_CLR (0x1 << 4) |
| 66 | #define USB_ID_WAKE_CLR (0x1 << 23) |
| 67 | #define USB_LINEST_WAKE_CLR (0x1 << 7) |
| 68 | #define USB_RXELEC_WAKE_CLR (0x1 << 7) |
| 69 | #else |
| 70 | #define APMU_USB_WAKE_CLR (0x07c) |
| 71 | |
| 72 | #define USB_WAKE_INT_EN (0x1 << 16) |
| 73 | #define USB_WAKE_PMU_EN (0x1 << 16) |
| 74 | |
| 75 | #define USB_VBUS_WAKE_EN (0x1 << 11) |
| 76 | #define USB_ID_WAKE_EN (0x1 << 12) |
| 77 | #define USB_LINEST_WAKE_EN ((0x1 << 9) | (0x1 << 10)) |
| 78 | #define USB_RXELEC_WAKE_EN (0x1 << 28) |
| 79 | |
| 80 | #define USB_VBUS_WAKE_CLR (0x1 << 4) |
| 81 | #define USB_ID_WAKE_CLR (0x1 << 23) |
| 82 | #define USB_LINEST_WAKE_CLR (0x1 << 7) |
| 83 | #define USB_RXELEC_WAKE_CLR (0x1 << 29) |
| 84 | #endif |
| 85 | |
| 86 | struct dwc3_asr { |
| 87 | struct device *dev; |
| 88 | struct clk *usb_clk; |
| 89 | int vbus_irq; |
| 90 | #ifdef CONFIG_DWC3_HWSULOG |
| 91 | int sulog_irq; |
| 92 | #endif |
| 93 | spinlock_t lock; |
| 94 | }; |
| 95 | extern void dwc3_release_pm_qos(void); |
| 96 | extern void dwc3_release_pm_qos_timeout(u32 sec); |
| 97 | extern void dwc3_acquire_pm_qos(void); |
| 98 | extern void dwc3_acquire_wakeup_event(void); |
| 99 | |
| 100 | extern struct dwc3 *dwc3_get_controller(void); |
| 101 | #ifdef CONFIG_DWC3_HWSULOG |
| 102 | extern void dwc3_hwsulog_clear_int(void); |
| 103 | #endif |
| 104 | |
| 105 | static irqreturn_t vbus_irq(int irq, void *dev) |
| 106 | { |
| 107 | struct dwc3_asr *adwc = (struct dwc3_asr *)dev; |
| 108 | void __iomem *apmu_base = regs_addr_get_va(REGS_ADDR_APMU); |
| 109 | |
| 110 | #ifdef CONFIG_DWC3_HWSULOG |
| 111 | dwc3_hwsulog_clear_int(); |
| 112 | #endif |
| 113 | |
| 114 | /* wait 50ms for vbus to be stable */ |
| 115 | msleep(50); |
| 116 | if (cpu_is_asr1901() || cpu_is_asr1906()) |
| 117 | writel(readl(apmu_base + APMU_USB_WAKE_CLR) |
| 118 | | USB_VBUS_WAKE_CLR | USB_LINEST_WAKE_CLR |
| 119 | | USB_ID_WAKE_CLR | USB_RXELEC_WAKE_CLR, |
| 120 | apmu_base + APMU_USB_WAKE_CLR); |
| 121 | else |
| 122 | writel(readl(apmu_base + APMU_USB_WAKE_CLR) |
| 123 | | (USB_VBUS_WAKE_CLR | USB_LINEST_WAKE_CLR | USB_RXELEC_WAKE_CLR |
| 124 | | USB_ID_WAKE_CLR | USB_WAKE_INT_EN), |
| 125 | apmu_base + APMU_USB_WAKE_CLR); |
| 126 | |
| 127 | pm_wakeup_event(adwc->dev, USB_HANDLE_TIME_MSEC); |
| 128 | pxa_usb_notify(PXA_USB_DEV_OTG, EVENT_VBUS, 0); |
| 129 | dev_info(adwc->dev, "asr-usb vbus interrupt is served..\n"); |
| 130 | return IRQ_HANDLED; |
| 131 | } |
| 132 | |
| 133 | #ifdef CONFIG_DWC3_HWSULOG |
| 134 | static irqreturn_t sulog_irq_handler(int irq, void *dev) |
| 135 | { |
| 136 | hwsulog_error_handler(); |
| 137 | |
| 138 | return IRQ_HANDLED; |
| 139 | } |
| 140 | #endif |
| 141 | |
| 142 | static int dwc3_asr_probe(struct platform_device *pdev) |
| 143 | { |
| 144 | struct device_node *node = pdev->dev.of_node; |
| 145 | struct dwc3_asr *adwc; |
| 146 | int ret; |
| 147 | void __iomem *apmu_base; |
| 148 | |
| 149 | adwc = devm_kzalloc(&pdev->dev, sizeof(*adwc), GFP_KERNEL); |
| 150 | if (!adwc) |
| 151 | return -ENOMEM; |
| 152 | |
| 153 | platform_set_drvdata(pdev, adwc); |
| 154 | |
| 155 | adwc->dev = &pdev->dev; |
| 156 | |
| 157 | adwc->usb_clk = devm_clk_get(adwc->dev, NULL); |
| 158 | if (IS_ERR(adwc->usb_clk)) { |
| 159 | dev_err(adwc->dev, "failed to get core clock\n"); |
| 160 | return PTR_ERR(adwc->usb_clk); |
| 161 | } |
| 162 | |
| 163 | ret = clk_prepare_enable(adwc->usb_clk); |
| 164 | if (ret) { |
| 165 | dev_err(adwc->dev, "failed to enable core clock\n"); |
| 166 | goto err_core; |
| 167 | } |
| 168 | |
| 169 | ret = of_platform_populate(node, NULL, NULL, adwc->dev); |
| 170 | if (ret) { |
| 171 | dev_err(adwc->dev, "failed to register core - %d\n", ret); |
| 172 | goto err_iface; |
| 173 | } |
| 174 | |
| 175 | adwc->vbus_irq = platform_get_irq(pdev, 0); |
| 176 | if (adwc->vbus_irq < 0) { |
| 177 | dev_err(&pdev->dev, "failed to get vbus irq\n"); |
| 178 | ret = -ENXIO; |
| 179 | goto err_iface; |
| 180 | } |
| 181 | |
| 182 | ret = devm_request_threaded_irq(&pdev->dev, adwc->vbus_irq, |
| 183 | NULL, vbus_irq, |
| 184 | IRQF_ONESHOT | IRQF_NO_SUSPEND, |
| 185 | "asr-usb-vbus", adwc); |
| 186 | if (ret) { |
| 187 | dev_info(&pdev->dev, |
| 188 | "Can not request irq for VBUS\n"); |
| 189 | goto err_iface; |
| 190 | } |
| 191 | |
| 192 | #ifdef CONFIG_DWC3_HWSULOG |
| 193 | adwc->sulog_irq = platform_get_irq(pdev, 1); |
| 194 | if (adwc->sulog_irq < 0) { |
| 195 | dev_info(&pdev->dev, "no sulog irq\n"); |
| 196 | } else { |
| 197 | ret = devm_request_threaded_irq(&pdev->dev, adwc->sulog_irq, |
| 198 | NULL, sulog_irq_handler, |
| 199 | IRQF_ONESHOT | IRQF_SHARED, |
| 200 | "dwc3-sulog-irq", adwc); |
| 201 | if (ret) { |
| 202 | dev_info(&pdev->dev, |
| 203 | "Can not request irq for dwc3 sulog %d\n", ret); |
| 204 | goto err_iface; |
| 205 | } |
| 206 | } |
| 207 | #endif |
| 208 | |
| 209 | apmu_base = regs_addr_get_va(REGS_ADDR_APMU); |
| 210 | writel(readl(apmu_base + APMU_USB_WAKE_CLR) | USB_WAKE_INT_EN | USB_VBUS_WAKE_EN, |
| 211 | apmu_base + APMU_USB_WAKE_CLR); |
| 212 | writel(readl(apmu_base + APMU_USB_WAKE_CLR) |
| 213 | | (USB_WAKE_INT_EN | USB_VBUS_WAKE_CLR | USB_LINEST_WAKE_CLR | USB_ID_WAKE_CLR), |
| 214 | apmu_base + APMU_USB_WAKE_CLR); |
| 215 | |
| 216 | platform_set_drvdata(pdev, adwc); |
| 217 | device_init_wakeup(&pdev->dev, 1); |
| 218 | |
| 219 | dev_info(&pdev->dev, "%s done\n", __func__); |
| 220 | |
| 221 | return 0; |
| 222 | |
| 223 | err_iface: |
| 224 | clk_disable_unprepare(adwc->usb_clk); |
| 225 | err_core: |
| 226 | return ret; |
| 227 | } |
| 228 | |
| 229 | static int dwc3_asr_remove(struct platform_device *pdev) |
| 230 | { |
| 231 | struct dwc3_asr *adwc = platform_get_drvdata(pdev); |
| 232 | |
| 233 | clk_disable_unprepare(adwc->usb_clk); |
| 234 | |
| 235 | return 0; |
| 236 | } |
| 237 | |
| 238 | #ifdef CONFIG_PM_SLEEP |
| 239 | static int dwc3_asr_suspend_noirq(struct device *dev) |
| 240 | { |
| 241 | struct dwc3_asr *adwc = dev_get_drvdata(dev); |
| 242 | void __iomem *apmu_base = regs_addr_get_va(REGS_ADDR_APMU); |
| 243 | struct dwc3 *dwc = dwc3_get_controller(); |
| 244 | |
| 245 | writel(readl(apmu_base + APMU_USB_WAKE_CLR) |
| 246 | | (USB_WAKE_INT_EN | USB_WAKE_PMU_EN |
| 247 | | USB_LINEST_WAKE_EN | USB_ID_WAKE_EN | USB_RXELEC_WAKE_EN |
| 248 | | USB_VBUS_WAKE_CLR | USB_LINEST_WAKE_CLR |
| 249 | | USB_ID_WAKE_CLR | USB_RXELEC_WAKE_CLR), |
| 250 | apmu_base + APMU_USB_WAKE_CLR); |
| 251 | |
| 252 | if (dwc->allow_suspend) { |
| 253 | if (dwc->link_state == DWC3_LINK_STATE_U3) |
| 254 | usb_phy_set_suspend2(dwc->usb2_phy, 1); |
| 255 | else |
| 256 | pr_info("dwc3 linkst: %d\n", dwc->link_state); |
| 257 | } |
| 258 | |
| 259 | enable_irq_wake(adwc->vbus_irq); |
| 260 | |
| 261 | dwc3_release_pm_qos(); |
| 262 | return 0; |
| 263 | } |
| 264 | |
| 265 | static int dwc3_asr_resume_noirq(struct device *dev) |
| 266 | { |
| 267 | volatile u32 value; |
| 268 | void __iomem *apmu_base = regs_addr_get_va(REGS_ADDR_APMU); |
| 269 | struct dwc3_asr *adwc = dev_get_drvdata(dev); |
| 270 | struct dwc3 *dwc = dwc3_get_controller(); |
| 271 | |
| 272 | if (dwc->allow_suspend) { |
| 273 | if (dwc->vbus_active) { |
| 274 | dwc3_acquire_pm_qos(); |
| 275 | usb_phy_set_suspend2(dwc->usb2_phy, 0); |
| 276 | } else { |
| 277 | pr_info("dwc3 vbus off, linkst: %d\n", dwc->link_state); |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | /* clear linestat wakeup and disable linestat/pmu wake en */ |
| 282 | value = readl(apmu_base + APMU_USB_WAKE_CLR); |
| 283 | value |= (USB_WAKE_INT_EN | USB_LINEST_WAKE_CLR | USB_RXELEC_WAKE_CLR); |
| 284 | writel(value, apmu_base + APMU_USB_WAKE_CLR); |
| 285 | udelay(50); |
| 286 | |
| 287 | value = readl(apmu_base + APMU_USB_WAKE_CLR); |
| 288 | value &= ~(USB_WAKE_PMU_EN | USB_LINEST_WAKE_EN | USB_RXELEC_WAKE_EN); |
| 289 | value |= (USB_VBUS_WAKE_EN | USB_ID_WAKE_EN | USB_WAKE_INT_EN); |
| 290 | writel(value, apmu_base + APMU_USB_WAKE_CLR); |
| 291 | |
| 292 | disable_irq_wake(adwc->vbus_irq); |
| 293 | return 0; |
| 294 | } |
| 295 | |
| 296 | static const struct dev_pm_ops dwc3_asr_pm_ops = { |
| 297 | .suspend_noirq = dwc3_asr_suspend_noirq, |
| 298 | .resume_noirq = dwc3_asr_resume_noirq, |
| 299 | }; |
| 300 | #endif |
| 301 | |
| 302 | static const struct of_device_id of_dwc3_match[] = { |
| 303 | { .compatible = "asr,dwc3" }, |
| 304 | { /* Sentinel */ } |
| 305 | }; |
| 306 | MODULE_DEVICE_TABLE(of, of_dwc3_match); |
| 307 | |
| 308 | static struct platform_driver dwc3_asr_driver = { |
| 309 | .probe = dwc3_asr_probe, |
| 310 | .remove = dwc3_asr_remove, |
| 311 | .driver = { |
| 312 | .name = "asr-dwc3", |
| 313 | .of_match_table = of_dwc3_match, |
| 314 | #ifdef CONFIG_PM_SLEEP |
| 315 | .pm = &dwc3_asr_pm_ops, |
| 316 | #endif |
| 317 | }, |
| 318 | }; |
| 319 | |
| 320 | module_platform_driver(dwc3_asr_driver); |
| 321 | |
| 322 | MODULE_ALIAS("platform:asr-dwc3"); |
| 323 | MODULE_LICENSE("GPL v2"); |
| 324 | MODULE_DESCRIPTION("ASR USB Glue Layer"); |