xref: /openbmc/linux/drivers/usb/host/ehci-spear.c (revision ce932d0c5589e9766e089c22c66890dfc48fbd94)
1 /*
2 * Driver for EHCI HCD on SPEAR SOC
3 *
4 * Copyright (C) 2010 ST Micro Electronics,
5 * Deepak Sikri <deepak.sikri@st.com>
6 *
7 * Based on various ehci-*.c drivers
8 *
9 * This file is subject to the terms and conditions of the GNU General Public
10 * License. See the file COPYING in the main directory of this archive for
11 * more details.
12 */
13 
14 #include <linux/clk.h>
15 #include <linux/jiffies.h>
16 #include <linux/platform_device.h>
17 #include <linux/pm.h>
18 
19 struct spear_ehci {
20 	struct ehci_hcd ehci;
21 	struct clk *clk;
22 };
23 
24 #define to_spear_ehci(hcd)	(struct spear_ehci *)hcd_to_ehci(hcd)
25 
26 static void spear_start_ehci(struct spear_ehci *ehci)
27 {
28 	clk_enable(ehci->clk);
29 }
30 
31 static void spear_stop_ehci(struct spear_ehci *ehci)
32 {
33 	clk_disable(ehci->clk);
34 }
35 
36 static int ehci_spear_setup(struct usb_hcd *hcd)
37 {
38 	struct ehci_hcd *ehci = hcd_to_ehci(hcd);
39 	int retval = 0;
40 
41 	/* registers start at offset 0x0 */
42 	ehci->caps = hcd->regs;
43 	ehci->regs = hcd->regs + HC_LENGTH(ehci, ehci_readl(ehci,
44 				&ehci->caps->hc_capbase));
45 	/* cache this readonly data; minimize chip reads */
46 	ehci->hcs_params = ehci_readl(ehci, &ehci->caps->hcs_params);
47 	retval = ehci_halt(ehci);
48 	if (retval)
49 		return retval;
50 
51 	retval = ehci_init(hcd);
52 	if (retval)
53 		return retval;
54 
55 	ehci_reset(ehci);
56 	ehci_port_power(ehci, 0);
57 
58 	return retval;
59 }
60 
61 static const struct hc_driver ehci_spear_hc_driver = {
62 	.description			= hcd_name,
63 	.product_desc			= "SPEAr EHCI",
64 	.hcd_priv_size			= sizeof(struct spear_ehci),
65 
66 	/* generic hardware linkage */
67 	.irq				= ehci_irq,
68 	.flags				= HCD_MEMORY | HCD_USB2,
69 
70 	/* basic lifecycle operations */
71 	.reset				= ehci_spear_setup,
72 	.start				= ehci_run,
73 	.stop				= ehci_stop,
74 	.shutdown			= ehci_shutdown,
75 
76 	/* managing i/o requests and associated device resources */
77 	.urb_enqueue			= ehci_urb_enqueue,
78 	.urb_dequeue			= ehci_urb_dequeue,
79 	.endpoint_disable		= ehci_endpoint_disable,
80 	.endpoint_reset			= ehci_endpoint_reset,
81 
82 	/* scheduling support */
83 	.get_frame_number		= ehci_get_frame,
84 
85 	/* root hub support */
86 	.hub_status_data		= ehci_hub_status_data,
87 	.hub_control			= ehci_hub_control,
88 	.bus_suspend			= ehci_bus_suspend,
89 	.bus_resume			= ehci_bus_resume,
90 	.relinquish_port		= ehci_relinquish_port,
91 	.port_handed_over		= ehci_port_handed_over,
92 	.clear_tt_buffer_complete	= ehci_clear_tt_buffer_complete,
93 };
94 
95 #ifdef CONFIG_PM
96 static int ehci_spear_drv_suspend(struct device *dev)
97 {
98 	struct usb_hcd *hcd = dev_get_drvdata(dev);
99 	struct ehci_hcd *ehci = hcd_to_ehci(hcd);
100 	unsigned long flags;
101 	int rc = 0;
102 
103 	if (time_before(jiffies, ehci->next_statechange))
104 		msleep(10);
105 
106 	/*
107 	 * Root hub was already suspended. Disable irq emission and mark HW
108 	 * unaccessible. The PM and USB cores make sure that the root hub is
109 	 * either suspended or stopped.
110 	 */
111 	spin_lock_irqsave(&ehci->lock, flags);
112 	ehci_prepare_ports_for_controller_suspend(ehci, device_may_wakeup(dev));
113 	ehci_writel(ehci, 0, &ehci->regs->intr_enable);
114 	ehci_readl(ehci, &ehci->regs->intr_enable);
115 	spin_unlock_irqrestore(&ehci->lock, flags);
116 
117 	return rc;
118 }
119 
120 static int ehci_spear_drv_resume(struct device *dev)
121 {
122 	struct usb_hcd *hcd = dev_get_drvdata(dev);
123 	struct ehci_hcd *ehci = hcd_to_ehci(hcd);
124 
125 	if (time_before(jiffies, ehci->next_statechange))
126 		msleep(100);
127 
128 	if (ehci_readl(ehci, &ehci->regs->configured_flag) == FLAG_CF) {
129 		int mask = INTR_MASK;
130 
131 		ehci_prepare_ports_for_controller_resume(ehci);
132 
133 		if (!hcd->self.root_hub->do_remote_wakeup)
134 			mask &= ~STS_PCD;
135 
136 		ehci_writel(ehci, mask, &ehci->regs->intr_enable);
137 		ehci_readl(ehci, &ehci->regs->intr_enable);
138 		return 0;
139 	}
140 
141 	usb_root_hub_lost_power(hcd->self.root_hub);
142 
143 	/*
144 	 * Else reset, to cope with power loss or flush-to-storage style
145 	 * "resume" having let BIOS kick in during reboot.
146 	 */
147 	ehci_halt(ehci);
148 	ehci_reset(ehci);
149 
150 	/* emptying the schedule aborts any urbs */
151 	spin_lock_irq(&ehci->lock);
152 	if (ehci->reclaim)
153 		end_unlink_async(ehci);
154 
155 	ehci_work(ehci);
156 	spin_unlock_irq(&ehci->lock);
157 
158 	ehci_writel(ehci, ehci->command, &ehci->regs->command);
159 	ehci_writel(ehci, FLAG_CF, &ehci->regs->configured_flag);
160 	ehci_readl(ehci, &ehci->regs->command);	/* unblock posted writes */
161 
162 	/* here we "know" root ports should always stay powered */
163 	ehci_port_power(ehci, 1);
164 	return 0;
165 }
166 #endif /* CONFIG_PM */
167 
168 static SIMPLE_DEV_PM_OPS(ehci_spear_pm_ops, ehci_spear_drv_suspend,
169 		ehci_spear_drv_resume);
170 
171 static int spear_ehci_hcd_drv_probe(struct platform_device *pdev)
172 {
173 	struct usb_hcd *hcd ;
174 	struct spear_ehci *ehci;
175 	struct resource *res;
176 	struct clk *usbh_clk;
177 	const struct hc_driver *driver = &ehci_spear_hc_driver;
178 	int *pdata = pdev->dev.platform_data;
179 	int irq, retval;
180 	char clk_name[20] = "usbh_clk";
181 
182 	if (pdata == NULL)
183 		return -EFAULT;
184 
185 	if (usb_disabled())
186 		return -ENODEV;
187 
188 	irq = platform_get_irq(pdev, 0);
189 	if (irq < 0) {
190 		retval = irq;
191 		goto fail_irq_get;
192 	}
193 
194 	if (*pdata >= 0)
195 		sprintf(clk_name, "usbh.%01d_clk", *pdata);
196 
197 	usbh_clk = clk_get(NULL, clk_name);
198 	if (IS_ERR(usbh_clk)) {
199 		dev_err(&pdev->dev, "Error getting interface clock\n");
200 		retval = PTR_ERR(usbh_clk);
201 		goto fail_get_usbh_clk;
202 	}
203 
204 	hcd = usb_create_hcd(driver, &pdev->dev, dev_name(&pdev->dev));
205 	if (!hcd) {
206 		retval = -ENOMEM;
207 		goto fail_create_hcd;
208 	}
209 
210 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
211 	if (!res) {
212 		retval = -ENODEV;
213 		goto fail_request_resource;
214 	}
215 
216 	hcd->rsrc_start = res->start;
217 	hcd->rsrc_len = resource_size(res);
218 	if (!request_mem_region(hcd->rsrc_start, hcd->rsrc_len,
219 				driver->description)) {
220 		retval = -EBUSY;
221 		goto fail_request_resource;
222 	}
223 
224 	hcd->regs = ioremap(hcd->rsrc_start, hcd->rsrc_len);
225 	if (hcd->regs == NULL) {
226 		dev_dbg(&pdev->dev, "error mapping memory\n");
227 		retval = -ENOMEM;
228 		goto fail_ioremap;
229 	}
230 
231 	ehci = (struct spear_ehci *)hcd_to_ehci(hcd);
232 	ehci->clk = usbh_clk;
233 
234 	spear_start_ehci(ehci);
235 	retval = usb_add_hcd(hcd, irq, IRQF_SHARED);
236 	if (retval)
237 		goto fail_add_hcd;
238 
239 	return retval;
240 
241 fail_add_hcd:
242 	spear_stop_ehci(ehci);
243 	iounmap(hcd->regs);
244 fail_ioremap:
245 	release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
246 fail_request_resource:
247 	usb_put_hcd(hcd);
248 fail_create_hcd:
249 	clk_put(usbh_clk);
250 fail_get_usbh_clk:
251 fail_irq_get:
252 	dev_err(&pdev->dev, "init fail, %d\n", retval);
253 
254 	return retval ;
255 }
256 
257 static int spear_ehci_hcd_drv_remove(struct platform_device *pdev)
258 {
259 	struct usb_hcd *hcd = platform_get_drvdata(pdev);
260 	struct spear_ehci *ehci_p = to_spear_ehci(hcd);
261 
262 	if (!hcd)
263 		return 0;
264 	if (in_interrupt())
265 		BUG();
266 	usb_remove_hcd(hcd);
267 
268 	if (ehci_p->clk)
269 		spear_stop_ehci(ehci_p);
270 	iounmap(hcd->regs);
271 	release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
272 	usb_put_hcd(hcd);
273 
274 	if (ehci_p->clk)
275 		clk_put(ehci_p->clk);
276 
277 	return 0;
278 }
279 
280 static struct platform_driver spear_ehci_hcd_driver = {
281 	.probe		= spear_ehci_hcd_drv_probe,
282 	.remove		= spear_ehci_hcd_drv_remove,
283 	.shutdown	= usb_hcd_platform_shutdown,
284 	.driver		= {
285 		.name = "spear-ehci",
286 		.bus = &platform_bus_type,
287 		.pm = &ehci_spear_pm_ops,
288 	}
289 };
290 
291 MODULE_ALIAS("platform:spear-ehci");
292