xref: /linux/drivers/usb/host/ehci-platform.c (revision 22d9d8e8316d7f69046c8805ce9aa8d9c43d4e5b)
1 /*
2  * Generic platform ehci driver
3  *
4  * Copyright 2007 Steven Brown <sbrown@cortland.com>
5  * Copyright 2010-2012 Hauke Mehrtens <hauke@hauke-m.de>
6  *
7  * Derived from the ohci-ssb driver
8  * Copyright 2007 Michael Buesch <m@bues.ch>
9  *
10  * Derived from the EHCI-PCI driver
11  * Copyright (c) 2000-2004 by David Brownell
12  *
13  * Derived from the ohci-pci driver
14  * Copyright 1999 Roman Weissgaerber
15  * Copyright 2000-2002 David Brownell
16  * Copyright 1999 Linus Torvalds
17  * Copyright 1999 Gregory P. Smith
18  *
19  * Licensed under the GNU/GPL. See COPYING for details.
20  */
21 #include <linux/dma-mapping.h>
22 #include <linux/err.h>
23 #include <linux/kernel.h>
24 #include <linux/hrtimer.h>
25 #include <linux/io.h>
26 #include <linux/module.h>
27 #include <linux/of.h>
28 #include <linux/platform_device.h>
29 #include <linux/usb.h>
30 #include <linux/usb/hcd.h>
31 #include <linux/usb/ehci_pdriver.h>
32 
33 #include "ehci.h"
34 
35 #define DRIVER_DESC "EHCI generic platform driver"
36 
37 static const char hcd_name[] = "ehci-platform";
38 
39 static int ehci_platform_reset(struct usb_hcd *hcd)
40 {
41 	struct platform_device *pdev = to_platform_device(hcd->self.controller);
42 	struct usb_ehci_pdata *pdata = dev_get_platdata(&pdev->dev);
43 	struct ehci_hcd *ehci = hcd_to_ehci(hcd);
44 	int retval;
45 
46 	hcd->has_tt = pdata->has_tt;
47 	ehci->has_synopsys_hc_bug = pdata->has_synopsys_hc_bug;
48 	ehci->big_endian_desc = pdata->big_endian_desc;
49 	ehci->big_endian_mmio = pdata->big_endian_mmio;
50 
51 	if (pdata->pre_setup) {
52 		retval = pdata->pre_setup(hcd);
53 		if (retval < 0)
54 			return retval;
55 	}
56 
57 	ehci->caps = hcd->regs + pdata->caps_offset;
58 	retval = ehci_setup(hcd);
59 	if (retval)
60 		return retval;
61 
62 	if (pdata->no_io_watchdog)
63 		ehci->need_io_watchdog = 0;
64 	return 0;
65 }
66 
67 static struct hc_driver __read_mostly ehci_platform_hc_driver;
68 
69 static const struct ehci_driver_overrides platform_overrides __initconst = {
70 	.reset =	ehci_platform_reset,
71 };
72 
73 static struct usb_ehci_pdata ehci_platform_defaults;
74 
75 static int ehci_platform_probe(struct platform_device *dev)
76 {
77 	struct usb_hcd *hcd;
78 	struct resource *res_mem;
79 	struct usb_ehci_pdata *pdata;
80 	int irq;
81 	int err;
82 
83 	if (usb_disabled())
84 		return -ENODEV;
85 
86 	/*
87 	 * use reasonable defaults so platforms don't have to provide these.
88 	 * with DT probing on ARM, none of these are set.
89 	 */
90 	if (!dev_get_platdata(&dev->dev))
91 		dev->dev.platform_data = &ehci_platform_defaults;
92 	if (!dev->dev.dma_mask)
93 		dev->dev.dma_mask = &dev->dev.coherent_dma_mask;
94 	err = dma_set_coherent_mask(&dev->dev, DMA_BIT_MASK(32));
95 	if (err)
96 		return err;
97 
98 	pdata = dev_get_platdata(&dev->dev);
99 
100 	irq = platform_get_irq(dev, 0);
101 	if (irq < 0) {
102 		dev_err(&dev->dev, "no irq provided");
103 		return irq;
104 	}
105 	res_mem = platform_get_resource(dev, IORESOURCE_MEM, 0);
106 	if (!res_mem) {
107 		dev_err(&dev->dev, "no memory resource provided");
108 		return -ENXIO;
109 	}
110 
111 	if (pdata->power_on) {
112 		err = pdata->power_on(dev);
113 		if (err < 0)
114 			return err;
115 	}
116 
117 	hcd = usb_create_hcd(&ehci_platform_hc_driver, &dev->dev,
118 			     dev_name(&dev->dev));
119 	if (!hcd) {
120 		err = -ENOMEM;
121 		goto err_power;
122 	}
123 
124 	hcd->rsrc_start = res_mem->start;
125 	hcd->rsrc_len = resource_size(res_mem);
126 
127 	hcd->regs = devm_ioremap_resource(&dev->dev, res_mem);
128 	if (IS_ERR(hcd->regs)) {
129 		err = PTR_ERR(hcd->regs);
130 		goto err_put_hcd;
131 	}
132 	err = usb_add_hcd(hcd, irq, IRQF_SHARED);
133 	if (err)
134 		goto err_put_hcd;
135 
136 	platform_set_drvdata(dev, hcd);
137 
138 	return err;
139 
140 err_put_hcd:
141 	usb_put_hcd(hcd);
142 err_power:
143 	if (pdata->power_off)
144 		pdata->power_off(dev);
145 
146 	return err;
147 }
148 
149 static int ehci_platform_remove(struct platform_device *dev)
150 {
151 	struct usb_hcd *hcd = platform_get_drvdata(dev);
152 	struct usb_ehci_pdata *pdata = dev_get_platdata(&dev->dev);
153 
154 	usb_remove_hcd(hcd);
155 	usb_put_hcd(hcd);
156 
157 	if (pdata->power_off)
158 		pdata->power_off(dev);
159 
160 	if (pdata == &ehci_platform_defaults)
161 		dev->dev.platform_data = NULL;
162 
163 	return 0;
164 }
165 
166 #ifdef CONFIG_PM
167 
168 static int ehci_platform_suspend(struct device *dev)
169 {
170 	struct usb_hcd *hcd = dev_get_drvdata(dev);
171 	struct usb_ehci_pdata *pdata = dev_get_platdata(dev);
172 	struct platform_device *pdev =
173 		container_of(dev, struct platform_device, dev);
174 	bool do_wakeup = device_may_wakeup(dev);
175 	int ret;
176 
177 	ret = ehci_suspend(hcd, do_wakeup);
178 
179 	if (pdata->power_suspend)
180 		pdata->power_suspend(pdev);
181 
182 	return ret;
183 }
184 
185 static int ehci_platform_resume(struct device *dev)
186 {
187 	struct usb_hcd *hcd = dev_get_drvdata(dev);
188 	struct usb_ehci_pdata *pdata = dev_get_platdata(dev);
189 	struct platform_device *pdev =
190 		container_of(dev, struct platform_device, dev);
191 
192 	if (pdata->power_on) {
193 		int err = pdata->power_on(pdev);
194 		if (err < 0)
195 			return err;
196 	}
197 
198 	ehci_resume(hcd, false);
199 	return 0;
200 }
201 
202 #else /* !CONFIG_PM */
203 #define ehci_platform_suspend	NULL
204 #define ehci_platform_resume	NULL
205 #endif /* CONFIG_PM */
206 
207 static const struct of_device_id vt8500_ehci_ids[] = {
208 	{ .compatible = "via,vt8500-ehci", },
209 	{ .compatible = "wm,prizm-ehci", },
210 	{}
211 };
212 
213 static const struct platform_device_id ehci_platform_table[] = {
214 	{ "ehci-platform", 0 },
215 	{ }
216 };
217 MODULE_DEVICE_TABLE(platform, ehci_platform_table);
218 
219 static const struct dev_pm_ops ehci_platform_pm_ops = {
220 	.suspend	= ehci_platform_suspend,
221 	.resume		= ehci_platform_resume,
222 };
223 
224 static struct platform_driver ehci_platform_driver = {
225 	.id_table	= ehci_platform_table,
226 	.probe		= ehci_platform_probe,
227 	.remove		= ehci_platform_remove,
228 	.shutdown	= usb_hcd_platform_shutdown,
229 	.driver		= {
230 		.owner	= THIS_MODULE,
231 		.name	= "ehci-platform",
232 		.pm	= &ehci_platform_pm_ops,
233 		.of_match_table = vt8500_ehci_ids,
234 	}
235 };
236 
237 static int __init ehci_platform_init(void)
238 {
239 	if (usb_disabled())
240 		return -ENODEV;
241 
242 	pr_info("%s: " DRIVER_DESC "\n", hcd_name);
243 
244 	ehci_init_driver(&ehci_platform_hc_driver, &platform_overrides);
245 	return platform_driver_register(&ehci_platform_driver);
246 }
247 module_init(ehci_platform_init);
248 
249 static void __exit ehci_platform_cleanup(void)
250 {
251 	platform_driver_unregister(&ehci_platform_driver);
252 }
253 module_exit(ehci_platform_cleanup);
254 
255 MODULE_DESCRIPTION(DRIVER_DESC);
256 MODULE_AUTHOR("Hauke Mehrtens");
257 MODULE_AUTHOR("Alan Stern");
258 MODULE_LICENSE("GPL");
259