MIPS: Yosemite, Emma: Fix off-by-two in arcs_cmdline buffer size check
[linux-2.6/linux-mips.git] / drivers / usb / host / ehci-s5p.c
blob024b65c4990dc117417b594387da3ac14f73d730
1 /*
2 * SAMSUNG S5P USB HOST EHCI Controller
4 * Copyright (C) 2011 Samsung Electronics Co.Ltd
5 * Author: Jingoo Han <jg1.han@samsung.com>
6 * Author: Joonyoung Shim <jy0922.shim@samsung.com>
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.
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>
22 struct s5p_ehci_hcd {
23 struct device *dev;
24 struct usb_hcd *hcd;
25 struct clk *clk;
28 static 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),
33 .irq = ehci_irq,
34 .flags = HCD_MEMORY | HCD_USB2,
36 .reset = ehci_init,
37 .start = ehci_run,
38 .stop = ehci_stop,
39 .shutdown = ehci_shutdown,
41 .get_frame_number = ehci_get_frame,
43 .urb_enqueue = ehci_urb_enqueue,
44 .urb_dequeue = ehci_urb_dequeue,
45 .endpoint_disable = ehci_endpoint_disable,
46 .endpoint_reset = ehci_endpoint_reset,
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,
53 .relinquish_port = ehci_relinquish_port,
54 .port_handed_over = ehci_port_handed_over,
56 .clear_tt_buffer_complete = ehci_clear_tt_buffer_complete,
59 static int __devinit s5p_ehci_probe(struct platform_device *pdev)
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;
69 pdata = pdev->dev.platform_data;
70 if (!pdata) {
71 dev_err(&pdev->dev, "No platform data defined\n");
72 return -EINVAL;
75 s5p_ehci = kzalloc(sizeof(struct s5p_ehci_hcd), GFP_KERNEL);
76 if (!s5p_ehci)
77 return -ENOMEM;
79 s5p_ehci->dev = &pdev->dev;
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;
89 s5p_ehci->hcd = hcd;
90 s5p_ehci->clk = clk_get(&pdev->dev, "usbhost");
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;
98 err = clk_enable(s5p_ehci->clk);
99 if (err)
100 goto fail_clken;
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;
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;
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;
125 if (pdata->phy_init)
126 pdata->phy_init(pdev, S5P_USB_PHY_HOST);
128 ehci = hcd_to_ehci(hcd);
129 ehci->caps = hcd->regs;
130 ehci->regs = hcd->regs +
131 HC_LENGTH(ehci, readl(&ehci->caps->hc_capbase));
133 dbg_hcs_params(ehci, "reset");
134 dbg_hcc_params(ehci, "reset");
136 /* cache this readonly data; minimize chip reads */
137 ehci->hcs_params = readl(&ehci->caps->hcs_params);
139 err = usb_add_hcd(hcd, irq, IRQF_SHARED);
140 if (err) {
141 dev_err(&pdev->dev, "Failed to add USB HCD\n");
142 goto fail;
145 platform_set_drvdata(pdev, s5p_ehci);
147 return 0;
149 fail:
150 iounmap(hcd->regs);
151 fail_io:
152 clk_disable(s5p_ehci->clk);
153 fail_clken:
154 clk_put(s5p_ehci->clk);
155 fail_clk:
156 usb_put_hcd(hcd);
157 fail_hcd:
158 kfree(s5p_ehci);
159 return err;
162 static int __devexit s5p_ehci_remove(struct platform_device *pdev)
164 struct s5p_ehci_platdata *pdata = pdev->dev.platform_data;
165 struct s5p_ehci_hcd *s5p_ehci = platform_get_drvdata(pdev);
166 struct usb_hcd *hcd = s5p_ehci->hcd;
168 usb_remove_hcd(hcd);
170 if (pdata && pdata->phy_exit)
171 pdata->phy_exit(pdev, S5P_USB_PHY_HOST);
173 iounmap(hcd->regs);
175 clk_disable(s5p_ehci->clk);
176 clk_put(s5p_ehci->clk);
178 usb_put_hcd(hcd);
179 kfree(s5p_ehci);
181 return 0;
184 static void s5p_ehci_shutdown(struct platform_device *pdev)
186 struct s5p_ehci_hcd *s5p_ehci = platform_get_drvdata(pdev);
187 struct usb_hcd *hcd = s5p_ehci->hcd;
189 if (hcd->driver->shutdown)
190 hcd->driver->shutdown(hcd);
193 #ifdef CONFIG_PM
194 static int s5p_ehci_suspend(struct device *dev)
196 struct s5p_ehci_hcd *s5p_ehci = dev_get_drvdata(dev);
197 struct usb_hcd *hcd = s5p_ehci->hcd;
198 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
199 struct platform_device *pdev = to_platform_device(dev);
200 struct s5p_ehci_platdata *pdata = pdev->dev.platform_data;
201 unsigned long flags;
202 int rc = 0;
204 if (time_before(jiffies, ehci->next_statechange))
205 msleep(20);
208 * Root hub was already suspended. Disable irq emission and
209 * mark HW unaccessible. The PM and USB cores make sure that
210 * the root hub is either suspended or stopped.
212 ehci_prepare_ports_for_controller_suspend(ehci, device_may_wakeup(dev));
213 spin_lock_irqsave(&ehci->lock, flags);
214 ehci_writel(ehci, 0, &ehci->regs->intr_enable);
215 (void)ehci_readl(ehci, &ehci->regs->intr_enable);
217 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
218 spin_unlock_irqrestore(&ehci->lock, flags);
220 if (pdata && pdata->phy_exit)
221 pdata->phy_exit(pdev, S5P_USB_PHY_HOST);
223 return rc;
226 static int s5p_ehci_resume(struct device *dev)
228 struct s5p_ehci_hcd *s5p_ehci = dev_get_drvdata(dev);
229 struct usb_hcd *hcd = s5p_ehci->hcd;
230 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
231 struct platform_device *pdev = to_platform_device(dev);
232 struct s5p_ehci_platdata *pdata = pdev->dev.platform_data;
234 if (pdata && pdata->phy_init)
235 pdata->phy_init(pdev, S5P_USB_PHY_HOST);
237 if (time_before(jiffies, ehci->next_statechange))
238 msleep(100);
240 /* Mark hardware accessible again as we are out of D3 state by now */
241 set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
243 if (ehci_readl(ehci, &ehci->regs->configured_flag) == FLAG_CF) {
244 int mask = INTR_MASK;
246 ehci_prepare_ports_for_controller_resume(ehci);
247 if (!hcd->self.root_hub->do_remote_wakeup)
248 mask &= ~STS_PCD;
249 ehci_writel(ehci, mask, &ehci->regs->intr_enable);
250 ehci_readl(ehci, &ehci->regs->intr_enable);
251 return 0;
254 usb_root_hub_lost_power(hcd->self.root_hub);
256 (void) ehci_halt(ehci);
257 (void) ehci_reset(ehci);
259 /* emptying the schedule aborts any urbs */
260 spin_lock_irq(&ehci->lock);
261 if (ehci->reclaim)
262 end_unlink_async(ehci);
263 ehci_work(ehci);
264 spin_unlock_irq(&ehci->lock);
266 ehci_writel(ehci, ehci->command, &ehci->regs->command);
267 ehci_writel(ehci, FLAG_CF, &ehci->regs->configured_flag);
268 ehci_readl(ehci, &ehci->regs->command); /* unblock posted writes */
270 /* here we "know" root ports should always stay powered */
271 ehci_port_power(ehci, 1);
273 ehci->rh_state = EHCI_RH_SUSPENDED;
275 return 0;
277 #else
278 #define s5p_ehci_suspend NULL
279 #define s5p_ehci_resume NULL
280 #endif
282 static const struct dev_pm_ops s5p_ehci_pm_ops = {
283 .suspend = s5p_ehci_suspend,
284 .resume = s5p_ehci_resume,
287 static struct platform_driver s5p_ehci_driver = {
288 .probe = s5p_ehci_probe,
289 .remove = __devexit_p(s5p_ehci_remove),
290 .shutdown = s5p_ehci_shutdown,
291 .driver = {
292 .name = "s5p-ehci",
293 .owner = THIS_MODULE,
294 .pm = &s5p_ehci_pm_ops,
298 MODULE_ALIAS("platform:s5p-ehci");