blob: 397f827387b70c2dac9debaa3666007200d22ebb [file] [log] [blame]
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +09001/*
2 * SAMSUNG S5P USB HOST EHCI Controller
3 *
4 * Copyright (C) 2011 Samsung Electronics Co.Ltd
5 * Author: Jingoo Han <jg1.han@samsung.com>
6 * Author: Joonyoung Shim <jy0922.shim@samsung.com>
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version.
12 *
13 */
14
15#include <linux/clk.h>
16#include <linux/platform_device.h>
17#include <mach/regs-pmu.h>
18#include <plat/cpu.h>
19#include <plat/ehci.h>
20#include <plat/usb-phy.h>
21
22struct s5p_ehci_hcd {
23 struct device *dev;
24 struct usb_hcd *hcd;
25 struct clk *clk;
26};
27
28static const struct hc_driver s5p_ehci_hc_driver = {
29 .description = hcd_name,
30 .product_desc = "S5P EHCI Host Controller",
31 .hcd_priv_size = sizeof(struct ehci_hcd),
32
33 .irq = ehci_irq,
34 .flags = HCD_MEMORY | HCD_USB2,
35
36 .reset = ehci_init,
37 .start = ehci_run,
38 .stop = ehci_stop,
39 .shutdown = ehci_shutdown,
40
41 .get_frame_number = ehci_get_frame,
42
43 .urb_enqueue = ehci_urb_enqueue,
44 .urb_dequeue = ehci_urb_dequeue,
45 .endpoint_disable = ehci_endpoint_disable,
46 .endpoint_reset = ehci_endpoint_reset,
47
48 .hub_status_data = ehci_hub_status_data,
49 .hub_control = ehci_hub_control,
50 .bus_suspend = ehci_bus_suspend,
51 .bus_resume = ehci_bus_resume,
52
53 .relinquish_port = ehci_relinquish_port,
54 .port_handed_over = ehci_port_handed_over,
55
56 .clear_tt_buffer_complete = ehci_clear_tt_buffer_complete,
57};
58
Jingoo Han27362d42011-05-09 15:28:39 +090059static int __devinit s5p_ehci_probe(struct platform_device *pdev)
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +090060{
61 struct s5p_ehci_platdata *pdata;
62 struct s5p_ehci_hcd *s5p_ehci;
63 struct usb_hcd *hcd;
64 struct ehci_hcd *ehci;
65 struct resource *res;
66 int irq;
67 int err;
68
69 pdata = pdev->dev.platform_data;
70 if (!pdata) {
71 dev_err(&pdev->dev, "No platform data defined\n");
72 return -EINVAL;
73 }
74
75 s5p_ehci = kzalloc(sizeof(struct s5p_ehci_hcd), GFP_KERNEL);
76 if (!s5p_ehci)
77 return -ENOMEM;
78
79 s5p_ehci->dev = &pdev->dev;
80
81 hcd = usb_create_hcd(&s5p_ehci_hc_driver, &pdev->dev,
82 dev_name(&pdev->dev));
83 if (!hcd) {
84 dev_err(&pdev->dev, "Unable to create HCD\n");
85 err = -ENOMEM;
86 goto fail_hcd;
87 }
88
Yulgon Kime5d3d442011-08-18 14:02:45 +090089 s5p_ehci->hcd = hcd;
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +090090 s5p_ehci->clk = clk_get(&pdev->dev, "usbhost");
91
92 if (IS_ERR(s5p_ehci->clk)) {
93 dev_err(&pdev->dev, "Failed to get usbhost clock\n");
94 err = PTR_ERR(s5p_ehci->clk);
95 goto fail_clk;
96 }
97
98 err = clk_enable(s5p_ehci->clk);
99 if (err)
100 goto fail_clken;
101
102 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
103 if (!res) {
104 dev_err(&pdev->dev, "Failed to get I/O memory\n");
105 err = -ENXIO;
106 goto fail_io;
107 }
108
109 hcd->rsrc_start = res->start;
110 hcd->rsrc_len = resource_size(res);
111 hcd->regs = ioremap(res->start, resource_size(res));
112 if (!hcd->regs) {
113 dev_err(&pdev->dev, "Failed to remap I/O memory\n");
114 err = -ENOMEM;
115 goto fail_io;
116 }
117
118 irq = platform_get_irq(pdev, 0);
119 if (!irq) {
120 dev_err(&pdev->dev, "Failed to get IRQ\n");
121 err = -ENODEV;
122 goto fail;
123 }
124
125 if (pdata->phy_init)
126 pdata->phy_init(pdev, S5P_USB_PHY_HOST);
127
128 ehci = hcd_to_ehci(hcd);
129 ehci->caps = hcd->regs;
Jan Anderssonc4301312011-05-03 20:11:57 +0200130 ehci->regs = hcd->regs +
131 HC_LENGTH(ehci, readl(&ehci->caps->hc_capbase));
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900132
133 dbg_hcs_params(ehci, "reset");
134 dbg_hcc_params(ehci, "reset");
135
136 /* cache this readonly data; minimize chip reads */
137 ehci->hcs_params = readl(&ehci->caps->hcs_params);
138
Geoff Levand876e0df2011-11-22 18:04:45 -0800139 ehci_reset(ehci);
140
Yong Zhangb5dd18d2011-09-07 16:10:52 +0800141 err = usb_add_hcd(hcd, irq, IRQF_SHARED);
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900142 if (err) {
143 dev_err(&pdev->dev, "Failed to add USB HCD\n");
144 goto fail;
145 }
146
147 platform_set_drvdata(pdev, s5p_ehci);
148
149 return 0;
150
151fail:
152 iounmap(hcd->regs);
153fail_io:
154 clk_disable(s5p_ehci->clk);
155fail_clken:
156 clk_put(s5p_ehci->clk);
157fail_clk:
158 usb_put_hcd(hcd);
159fail_hcd:
160 kfree(s5p_ehci);
161 return err;
162}
163
Jingoo Han27362d42011-05-09 15:28:39 +0900164static int __devexit s5p_ehci_remove(struct platform_device *pdev)
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900165{
166 struct s5p_ehci_platdata *pdata = pdev->dev.platform_data;
167 struct s5p_ehci_hcd *s5p_ehci = platform_get_drvdata(pdev);
168 struct usb_hcd *hcd = s5p_ehci->hcd;
169
170 usb_remove_hcd(hcd);
171
172 if (pdata && pdata->phy_exit)
173 pdata->phy_exit(pdev, S5P_USB_PHY_HOST);
174
175 iounmap(hcd->regs);
176
177 clk_disable(s5p_ehci->clk);
178 clk_put(s5p_ehci->clk);
179
180 usb_put_hcd(hcd);
181 kfree(s5p_ehci);
182
183 return 0;
184}
185
186static void s5p_ehci_shutdown(struct platform_device *pdev)
187{
188 struct s5p_ehci_hcd *s5p_ehci = platform_get_drvdata(pdev);
189 struct usb_hcd *hcd = s5p_ehci->hcd;
190
191 if (hcd->driver->shutdown)
192 hcd->driver->shutdown(hcd);
193}
194
Jingoo Han1acb30e2011-05-20 20:48:33 +0900195#ifdef CONFIG_PM
196static int s5p_ehci_suspend(struct device *dev)
197{
198 struct s5p_ehci_hcd *s5p_ehci = dev_get_drvdata(dev);
199 struct usb_hcd *hcd = s5p_ehci->hcd;
200 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
201 struct platform_device *pdev = to_platform_device(dev);
202 struct s5p_ehci_platdata *pdata = pdev->dev.platform_data;
203 unsigned long flags;
204 int rc = 0;
205
206 if (time_before(jiffies, ehci->next_statechange))
207 msleep(20);
208
209 /*
210 * Root hub was already suspended. Disable irq emission and
211 * mark HW unaccessible. The PM and USB cores make sure that
212 * the root hub is either suspended or stopped.
213 */
214 ehci_prepare_ports_for_controller_suspend(ehci, device_may_wakeup(dev));
215 spin_lock_irqsave(&ehci->lock, flags);
216 ehci_writel(ehci, 0, &ehci->regs->intr_enable);
217 (void)ehci_readl(ehci, &ehci->regs->intr_enable);
218
219 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
220 spin_unlock_irqrestore(&ehci->lock, flags);
221
222 if (pdata && pdata->phy_exit)
223 pdata->phy_exit(pdev, S5P_USB_PHY_HOST);
224
225 return rc;
226}
227
228static int s5p_ehci_resume(struct device *dev)
229{
230 struct s5p_ehci_hcd *s5p_ehci = dev_get_drvdata(dev);
231 struct usb_hcd *hcd = s5p_ehci->hcd;
232 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
233 struct platform_device *pdev = to_platform_device(dev);
234 struct s5p_ehci_platdata *pdata = pdev->dev.platform_data;
235
236 if (pdata && pdata->phy_init)
237 pdata->phy_init(pdev, S5P_USB_PHY_HOST);
238
239 if (time_before(jiffies, ehci->next_statechange))
240 msleep(100);
241
242 /* Mark hardware accessible again as we are out of D3 state by now */
243 set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
244
245 if (ehci_readl(ehci, &ehci->regs->configured_flag) == FLAG_CF) {
246 int mask = INTR_MASK;
247
248 ehci_prepare_ports_for_controller_resume(ehci);
249 if (!hcd->self.root_hub->do_remote_wakeup)
250 mask &= ~STS_PCD;
251 ehci_writel(ehci, mask, &ehci->regs->intr_enable);
252 ehci_readl(ehci, &ehci->regs->intr_enable);
253 return 0;
254 }
255
256 usb_root_hub_lost_power(hcd->self.root_hub);
257
258 (void) ehci_halt(ehci);
259 (void) ehci_reset(ehci);
260
261 /* emptying the schedule aborts any urbs */
262 spin_lock_irq(&ehci->lock);
263 if (ehci->reclaim)
264 end_unlink_async(ehci);
265 ehci_work(ehci);
266 spin_unlock_irq(&ehci->lock);
267
268 ehci_writel(ehci, ehci->command, &ehci->regs->command);
269 ehci_writel(ehci, FLAG_CF, &ehci->regs->configured_flag);
270 ehci_readl(ehci, &ehci->regs->command); /* unblock posted writes */
271
272 /* here we "know" root ports should always stay powered */
273 ehci_port_power(ehci, 1);
274
Alan Sterne8799902011-08-18 16:31:30 -0400275 ehci->rh_state = EHCI_RH_SUSPENDED;
Jingoo Han1acb30e2011-05-20 20:48:33 +0900276
277 return 0;
278}
279#else
280#define s5p_ehci_suspend NULL
281#define s5p_ehci_resume NULL
282#endif
283
284static const struct dev_pm_ops s5p_ehci_pm_ops = {
285 .suspend = s5p_ehci_suspend,
286 .resume = s5p_ehci_resume,
287};
288
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900289static struct platform_driver s5p_ehci_driver = {
290 .probe = s5p_ehci_probe,
Jingoo Han27362d42011-05-09 15:28:39 +0900291 .remove = __devexit_p(s5p_ehci_remove),
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900292 .shutdown = s5p_ehci_shutdown,
293 .driver = {
294 .name = "s5p-ehci",
295 .owner = THIS_MODULE,
Jingoo Han1acb30e2011-05-20 20:48:33 +0900296 .pm = &s5p_ehci_pm_ops,
Joonyoung Shim1bcc5aa2011-04-08 14:08:50 +0900297 }
298};
299
300MODULE_ALIAS("platform:s5p-ehci");