xref: /linux/drivers/usb/host/ehci-npcm7xx.c (revision 4ae68b26c3ab5a82aa271e6e9fc9b1a06e1d6b40)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Nuvoton NPCM7xx driver for EHCI HCD
4  *
5  * Copyright (C) 2018 Nuvoton Technologies,
6  * Avi Fishman <avi.fishman@nuvoton.com> <avifishman70@gmail.com>
7  * Tomer Maimon <tomer.maimon@nuvoton.com> <tmaimon77@gmail.com>
8  *
9  * Based on various ehci-spear.c driver
10  */
11 
12 
13 #include <linux/dma-mapping.h>
14 
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/of.h>
18 #include <linux/platform_device.h>
19 #include <linux/pm.h>
20 #include <linux/usb.h>
21 #include <linux/usb/hcd.h>
22 
23 #include "ehci.h"
24 
25 #define DRIVER_DESC "EHCI npcm7xx driver"
26 
27 static struct hc_driver __read_mostly ehci_npcm7xx_hc_driver;
28 
29 static int __maybe_unused ehci_npcm7xx_drv_suspend(struct device *dev)
30 {
31 	struct usb_hcd *hcd = dev_get_drvdata(dev);
32 	bool do_wakeup = device_may_wakeup(dev);
33 
34 	return ehci_suspend(hcd, do_wakeup);
35 }
36 
37 static int __maybe_unused ehci_npcm7xx_drv_resume(struct device *dev)
38 {
39 	struct usb_hcd *hcd = dev_get_drvdata(dev);
40 
41 	ehci_resume(hcd, false);
42 	return 0;
43 }
44 
45 static SIMPLE_DEV_PM_OPS(ehci_npcm7xx_pm_ops, ehci_npcm7xx_drv_suspend,
46 		ehci_npcm7xx_drv_resume);
47 
48 static int npcm7xx_ehci_hcd_drv_probe(struct platform_device *pdev)
49 {
50 	struct usb_hcd *hcd;
51 	struct resource *res;
52 	const struct hc_driver *driver = &ehci_npcm7xx_hc_driver;
53 	int irq;
54 	int retval;
55 
56 	dev_dbg(&pdev->dev,	"initializing npcm7xx ehci USB Controller\n");
57 
58 	if (usb_disabled())
59 		return -ENODEV;
60 
61 	irq = platform_get_irq(pdev, 0);
62 	if (irq < 0) {
63 		retval = irq;
64 		goto fail;
65 	}
66 
67 	/*
68 	 * Right now device-tree probed devices don't get dma_mask set.
69 	 * Since shared usb code relies on it, set it here for now.
70 	 * Once we have dma capability bindings this can go away.
71 	 */
72 	retval = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
73 	if (retval)
74 		goto fail;
75 
76 	hcd = usb_create_hcd(driver, &pdev->dev, dev_name(&pdev->dev));
77 	if (!hcd) {
78 		retval = -ENOMEM;
79 		goto fail;
80 	}
81 
82 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
83 	hcd->regs = devm_ioremap_resource(&pdev->dev, res);
84 	if (IS_ERR(hcd->regs)) {
85 		retval = PTR_ERR(hcd->regs);
86 		goto err_put_hcd;
87 	}
88 	hcd->rsrc_start = res->start;
89 	hcd->rsrc_len = resource_size(res);
90 
91 	/* registers start at offset 0x0 */
92 	hcd_to_ehci(hcd)->caps = hcd->regs;
93 
94 	retval = usb_add_hcd(hcd, irq, IRQF_SHARED);
95 	if (retval)
96 		goto err_put_hcd;
97 
98 	device_wakeup_enable(hcd->self.controller);
99 	return retval;
100 
101 err_put_hcd:
102 	usb_put_hcd(hcd);
103 fail:
104 	dev_err(&pdev->dev, "init fail, %d\n", retval);
105 
106 	return retval;
107 }
108 
109 static void npcm7xx_ehci_hcd_drv_remove(struct platform_device *pdev)
110 {
111 	struct usb_hcd *hcd = platform_get_drvdata(pdev);
112 
113 	usb_remove_hcd(hcd);
114 
115 	usb_put_hcd(hcd);
116 }
117 
118 static const struct of_device_id npcm7xx_ehci_id_table[] = {
119 	{ .compatible = "nuvoton,npcm750-ehci" },
120 	{ },
121 };
122 MODULE_DEVICE_TABLE(of, npcm7xx_ehci_id_table);
123 
124 static struct platform_driver npcm7xx_ehci_hcd_driver = {
125 	.probe		= npcm7xx_ehci_hcd_drv_probe,
126 	.remove_new	= npcm7xx_ehci_hcd_drv_remove,
127 	.shutdown	= usb_hcd_platform_shutdown,
128 	.driver		= {
129 		.name = "npcm7xx-ehci",
130 		.bus = &platform_bus_type,
131 		.pm = pm_ptr(&ehci_npcm7xx_pm_ops),
132 		.of_match_table = npcm7xx_ehci_id_table,
133 	}
134 };
135 
136 static int __init ehci_npcm7xx_init(void)
137 {
138 	if (usb_disabled())
139 		return -ENODEV;
140 
141 	ehci_init_driver(&ehci_npcm7xx_hc_driver, NULL);
142 	return platform_driver_register(&npcm7xx_ehci_hcd_driver);
143 }
144 module_init(ehci_npcm7xx_init);
145 
146 static void __exit ehci_npcm7xx_cleanup(void)
147 {
148 	platform_driver_unregister(&npcm7xx_ehci_hcd_driver);
149 }
150 module_exit(ehci_npcm7xx_cleanup);
151 
152 MODULE_DESCRIPTION(DRIVER_DESC);
153 MODULE_ALIAS("platform:npcm7xx-ehci");
154 MODULE_AUTHOR("Avi Fishman");
155 MODULE_LICENSE("GPL v2");
156