Merge remote-tracking branch 'upstream' into next
[cascardo/linux.git] / drivers / usb / host / ehci-spear.c
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/of.h>
17 #include <linux/platform_device.h>
18 #include <linux/pm.h>
19
20 struct spear_ehci {
21         struct ehci_hcd ehci;
22         struct clk *clk;
23 };
24
25 #define to_spear_ehci(hcd)      (struct spear_ehci *)hcd_to_ehci(hcd)
26
27 static void spear_start_ehci(struct spear_ehci *ehci)
28 {
29         clk_prepare_enable(ehci->clk);
30 }
31
32 static void spear_stop_ehci(struct spear_ehci *ehci)
33 {
34         clk_disable_unprepare(ehci->clk);
35 }
36
37 static int ehci_spear_setup(struct usb_hcd *hcd)
38 {
39         struct ehci_hcd *ehci = hcd_to_ehci(hcd);
40         int retval = 0;
41
42         /* registers start at offset 0x0 */
43         ehci->caps = hcd->regs;
44
45         retval = ehci_setup(hcd);
46         if (retval)
47                 return retval;
48
49         ehci_port_power(ehci, 0);
50
51         return retval;
52 }
53
54 static const struct hc_driver ehci_spear_hc_driver = {
55         .description                    = hcd_name,
56         .product_desc                   = "SPEAr EHCI",
57         .hcd_priv_size                  = sizeof(struct spear_ehci),
58
59         /* generic hardware linkage */
60         .irq                            = ehci_irq,
61         .flags                          = HCD_MEMORY | HCD_USB2,
62
63         /* basic lifecycle operations */
64         .reset                          = ehci_spear_setup,
65         .start                          = ehci_run,
66         .stop                           = ehci_stop,
67         .shutdown                       = ehci_shutdown,
68
69         /* managing i/o requests and associated device resources */
70         .urb_enqueue                    = ehci_urb_enqueue,
71         .urb_dequeue                    = ehci_urb_dequeue,
72         .endpoint_disable               = ehci_endpoint_disable,
73         .endpoint_reset                 = ehci_endpoint_reset,
74
75         /* scheduling support */
76         .get_frame_number               = ehci_get_frame,
77
78         /* root hub support */
79         .hub_status_data                = ehci_hub_status_data,
80         .hub_control                    = ehci_hub_control,
81         .bus_suspend                    = ehci_bus_suspend,
82         .bus_resume                     = ehci_bus_resume,
83         .relinquish_port                = ehci_relinquish_port,
84         .port_handed_over               = ehci_port_handed_over,
85         .clear_tt_buffer_complete       = ehci_clear_tt_buffer_complete,
86 };
87
88 #ifdef CONFIG_PM
89 static int ehci_spear_drv_suspend(struct device *dev)
90 {
91         struct usb_hcd *hcd = dev_get_drvdata(dev);
92         bool do_wakeup = device_may_wakeup(dev);
93
94         return ehci_suspend(hcd, do_wakeup);
95 }
96
97 static int ehci_spear_drv_resume(struct device *dev)
98 {
99         struct usb_hcd *hcd = dev_get_drvdata(dev);
100
101         ehci_resume(hcd, false);
102         return 0;
103 }
104 #endif /* CONFIG_PM */
105
106 static SIMPLE_DEV_PM_OPS(ehci_spear_pm_ops, ehci_spear_drv_suspend,
107                 ehci_spear_drv_resume);
108
109 static u64 spear_ehci_dma_mask = DMA_BIT_MASK(32);
110
111 static int spear_ehci_hcd_drv_probe(struct platform_device *pdev)
112 {
113         struct usb_hcd *hcd ;
114         struct spear_ehci *ehci;
115         struct resource *res;
116         struct clk *usbh_clk;
117         const struct hc_driver *driver = &ehci_spear_hc_driver;
118         int irq, retval;
119         char clk_name[20] = "usbh_clk";
120         static int instance = -1;
121
122         if (usb_disabled())
123                 return -ENODEV;
124
125         irq = platform_get_irq(pdev, 0);
126         if (irq < 0) {
127                 retval = irq;
128                 goto fail_irq_get;
129         }
130
131         /*
132          * Right now device-tree probed devices don't get dma_mask set.
133          * Since shared usb code relies on it, set it here for now.
134          * Once we have dma capability bindings this can go away.
135          */
136         if (!pdev->dev.dma_mask)
137                 pdev->dev.dma_mask = &spear_ehci_dma_mask;
138
139         /*
140          * Increment the device instance, when probing via device-tree
141          */
142         if (pdev->id < 0)
143                 instance++;
144         else
145                 instance = pdev->id;
146         sprintf(clk_name, "usbh.%01d_clk", instance);
147
148         usbh_clk = clk_get(NULL, clk_name);
149         if (IS_ERR(usbh_clk)) {
150                 dev_err(&pdev->dev, "Error getting interface clock\n");
151                 retval = PTR_ERR(usbh_clk);
152                 goto fail_get_usbh_clk;
153         }
154
155         hcd = usb_create_hcd(driver, &pdev->dev, dev_name(&pdev->dev));
156         if (!hcd) {
157                 retval = -ENOMEM;
158                 goto fail_create_hcd;
159         }
160
161         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
162         if (!res) {
163                 retval = -ENODEV;
164                 goto fail_request_resource;
165         }
166
167         hcd->rsrc_start = res->start;
168         hcd->rsrc_len = resource_size(res);
169         if (!request_mem_region(hcd->rsrc_start, hcd->rsrc_len,
170                                 driver->description)) {
171                 retval = -EBUSY;
172                 goto fail_request_resource;
173         }
174
175         hcd->regs = ioremap(hcd->rsrc_start, hcd->rsrc_len);
176         if (hcd->regs == NULL) {
177                 dev_dbg(&pdev->dev, "error mapping memory\n");
178                 retval = -ENOMEM;
179                 goto fail_ioremap;
180         }
181
182         ehci = (struct spear_ehci *)hcd_to_ehci(hcd);
183         ehci->clk = usbh_clk;
184
185         spear_start_ehci(ehci);
186         retval = usb_add_hcd(hcd, irq, IRQF_SHARED);
187         if (retval)
188                 goto fail_add_hcd;
189
190         return retval;
191
192 fail_add_hcd:
193         spear_stop_ehci(ehci);
194         iounmap(hcd->regs);
195 fail_ioremap:
196         release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
197 fail_request_resource:
198         usb_put_hcd(hcd);
199 fail_create_hcd:
200         clk_put(usbh_clk);
201 fail_get_usbh_clk:
202 fail_irq_get:
203         dev_err(&pdev->dev, "init fail, %d\n", retval);
204
205         return retval ;
206 }
207
208 static int spear_ehci_hcd_drv_remove(struct platform_device *pdev)
209 {
210         struct usb_hcd *hcd = platform_get_drvdata(pdev);
211         struct spear_ehci *ehci_p = to_spear_ehci(hcd);
212
213         if (!hcd)
214                 return 0;
215         if (in_interrupt())
216                 BUG();
217         usb_remove_hcd(hcd);
218
219         if (ehci_p->clk)
220                 spear_stop_ehci(ehci_p);
221         iounmap(hcd->regs);
222         release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
223         usb_put_hcd(hcd);
224
225         if (ehci_p->clk)
226                 clk_put(ehci_p->clk);
227
228         return 0;
229 }
230
231 static struct of_device_id spear_ehci_id_table[] __devinitdata = {
232         { .compatible = "st,spear600-ehci", },
233         { },
234 };
235
236 static struct platform_driver spear_ehci_hcd_driver = {
237         .probe          = spear_ehci_hcd_drv_probe,
238         .remove         = spear_ehci_hcd_drv_remove,
239         .shutdown       = usb_hcd_platform_shutdown,
240         .driver         = {
241                 .name = "spear-ehci",
242                 .bus = &platform_bus_type,
243                 .pm = &ehci_spear_pm_ops,
244                 .of_match_table = of_match_ptr(spear_ehci_id_table),
245         }
246 };
247
248 MODULE_ALIAS("platform:spear-ehci");