Commit | Line | Data |
---|---|---|
3429e91a SAS |
1 | /* |
2 | * xhci-plat.c - xHCI host controller driver platform Bus Glue. | |
3 | * | |
4 | * Copyright (C) 2012 Texas Instruments Incorporated - http://www.ti.com | |
5 | * Author: Sebastian Andrzej Siewior <bigeasy@linutronix.de> | |
6 | * | |
7 | * A lot of code borrowed from the Linux xHCI driver. | |
8 | * | |
9 | * This program is free software; you can redistribute it and/or | |
10 | * modify it under the terms of the GNU General Public License | |
11 | * version 2 as published by the Free Software Foundation. | |
12 | */ | |
13 | ||
14 | #include <linux/platform_device.h> | |
15 | #include <linux/module.h> | |
16 | #include <linux/slab.h> | |
6fa3eb70 | 17 | #include <linux/dma-mapping.h> |
3429e91a SAS |
18 | |
19 | #include "xhci.h" | |
20 | ||
6fa3eb70 S |
21 | #ifdef CONFIG_MTK_XHCI |
22 | #include <linux/xhci/xhci-mtk.h> | |
23 | #include <linux/of.h> | |
24 | #endif | |
25 | ||
3429e91a SAS |
26 | static void xhci_plat_quirks(struct device *dev, struct xhci_hcd *xhci) |
27 | { | |
28 | /* | |
29 | * As of now platform drivers don't provide MSI support so we ensure | |
30 | * here that the generic code does not try to make a pci_dev from our | |
31 | * dev struct in order to setup MSI | |
32 | */ | |
a6025b95 | 33 | xhci->quirks |= XHCI_PLAT; |
6fa3eb70 S |
34 | //CC: MTK host controller gives a spurious successful event after a |
35 | // short transfer. Ignore it. | |
36 | xhci->quirks |= XHCI_SPURIOUS_SUCCESS; | |
37 | xhci->quirks |= XHCI_LPM_SUPPORT; | |
3429e91a SAS |
38 | } |
39 | ||
40 | /* called during probe() after chip reset completes */ | |
41 | static int xhci_plat_setup(struct usb_hcd *hcd) | |
42 | { | |
43 | return xhci_gen_setup(hcd, xhci_plat_quirks); | |
44 | } | |
45 | ||
46 | static const struct hc_driver xhci_plat_xhci_driver = { | |
47 | .description = "xhci-hcd", | |
48 | .product_desc = "xHCI Host Controller", | |
49 | .hcd_priv_size = sizeof(struct xhci_hcd *), | |
50 | ||
51 | /* | |
52 | * generic hardware linkage | |
53 | */ | |
54 | .irq = xhci_irq, | |
55 | .flags = HCD_MEMORY | HCD_USB3 | HCD_SHARED, | |
56 | ||
57 | /* | |
58 | * basic lifecycle operations | |
59 | */ | |
60 | .reset = xhci_plat_setup, | |
61 | .start = xhci_run, | |
62 | .stop = xhci_stop, | |
63 | .shutdown = xhci_shutdown, | |
64 | ||
65 | /* | |
66 | * managing i/o requests and associated device resources | |
67 | */ | |
68 | .urb_enqueue = xhci_urb_enqueue, | |
69 | .urb_dequeue = xhci_urb_dequeue, | |
70 | .alloc_dev = xhci_alloc_dev, | |
71 | .free_dev = xhci_free_dev, | |
72 | .alloc_streams = xhci_alloc_streams, | |
73 | .free_streams = xhci_free_streams, | |
74 | .add_endpoint = xhci_add_endpoint, | |
75 | .drop_endpoint = xhci_drop_endpoint, | |
76 | .endpoint_reset = xhci_endpoint_reset, | |
77 | .check_bandwidth = xhci_check_bandwidth, | |
78 | .reset_bandwidth = xhci_reset_bandwidth, | |
79 | .address_device = xhci_address_device, | |
80 | .update_hub_device = xhci_update_hub_device, | |
81 | .reset_device = xhci_discover_or_reset_device, | |
82 | ||
83 | /* | |
84 | * scheduling support | |
85 | */ | |
86 | .get_frame_number = xhci_get_frame, | |
87 | ||
88 | /* Root hub support */ | |
89 | .hub_control = xhci_hub_control, | |
90 | .hub_status_data = xhci_hub_status_data, | |
91 | .bus_suspend = xhci_bus_suspend, | |
92 | .bus_resume = xhci_bus_resume, | |
93 | }; | |
94 | ||
6fa3eb70 S |
95 | #if defined(CONFIG_MTK_LM_MODE) |
96 | #define XHCI_DMA_BIT_MASK DMA_BIT_MASK(64) | |
97 | #else | |
98 | #define XHCI_DMA_BIT_MASK DMA_BIT_MASK(32) | |
99 | #endif | |
100 | ||
101 | static u64 xhci_dma_mask = XHCI_DMA_BIT_MASK; | |
102 | ||
103 | static void xhci_hcd_release (struct device *dev) | |
104 | { | |
105 | printk(KERN_INFO "dev = 0x%p\n", dev); | |
106 | } | |
107 | ||
3429e91a SAS |
108 | static int xhci_plat_probe(struct platform_device *pdev) |
109 | { | |
110 | const struct hc_driver *driver; | |
111 | struct xhci_hcd *xhci; | |
112 | struct resource *res; | |
113 | struct usb_hcd *hcd; | |
114 | int ret; | |
115 | int irq; | |
116 | ||
117 | if (usb_disabled()) | |
118 | return -ENODEV; | |
119 | ||
120 | driver = &xhci_plat_xhci_driver; | |
121 | ||
6fa3eb70 S |
122 | #ifdef CONFIG_MTK_XHCI /* device tree support */ |
123 | irq = platform_get_irq_byname(pdev, XHCI_DRIVER_NAME); | |
124 | printk("%s(%d): %d\n", __func__, __LINE__, irq); | |
125 | if(irq < 0) | |
126 | return -ENODEV; | |
127 | ||
128 | res = platform_get_resource_byname(pdev, IORESOURCE_MEM, XHCI_BASE_REGS_ADDR_RES_NAME); | |
129 | if (!res) | |
130 | return -ENODEV; | |
131 | ||
132 | pdev->dev.coherent_dma_mask = XHCI_DMA_BIT_MASK; | |
133 | pdev->dev.dma_mask = &xhci_dma_mask; | |
134 | pdev->dev.release = xhci_hcd_release; | |
135 | #else | |
3429e91a SAS |
136 | irq = platform_get_irq(pdev, 0); |
137 | if (irq < 0) | |
138 | return -ENODEV; | |
139 | ||
140 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); | |
141 | if (!res) | |
142 | return -ENODEV; | |
6fa3eb70 | 143 | #endif |
3429e91a SAS |
144 | |
145 | hcd = usb_create_hcd(driver, &pdev->dev, dev_name(&pdev->dev)); | |
146 | if (!hcd) | |
147 | return -ENOMEM; | |
148 | ||
149 | hcd->rsrc_start = res->start; | |
150 | hcd->rsrc_len = resource_size(res); | |
151 | ||
152 | if (!request_mem_region(hcd->rsrc_start, hcd->rsrc_len, | |
153 | driver->description)) { | |
154 | dev_dbg(&pdev->dev, "controller already in use\n"); | |
155 | ret = -EBUSY; | |
156 | goto put_hcd; | |
157 | } | |
158 | ||
319acdfc | 159 | hcd->regs = ioremap_nocache(hcd->rsrc_start, hcd->rsrc_len); |
3429e91a SAS |
160 | if (!hcd->regs) { |
161 | dev_dbg(&pdev->dev, "error mapping memory\n"); | |
162 | ret = -EFAULT; | |
163 | goto release_mem_region; | |
164 | } | |
165 | ||
6fa3eb70 S |
166 | printk("%s(%d): logic 0x%p, phys 0x%p\n", __func__, __LINE__, |
167 | (void *)(unsigned long)res->start, hcd->regs); | |
168 | ||
169 | #ifdef CONFIG_MTK_XHCI | |
170 | ret = usb_add_hcd(hcd, irq, IRQF_SHARED | IRQF_TRIGGER_LOW); | |
171 | #else | |
3429e91a | 172 | ret = usb_add_hcd(hcd, irq, IRQF_SHARED); |
6fa3eb70 S |
173 | #endif |
174 | ||
3429e91a SAS |
175 | if (ret) |
176 | goto unmap_registers; | |
177 | ||
178 | /* USB 2.0 roothub is stored in the platform_device now. */ | |
179 | hcd = dev_get_drvdata(&pdev->dev); | |
180 | xhci = hcd_to_xhci(hcd); | |
181 | xhci->shared_hcd = usb_create_shared_hcd(driver, &pdev->dev, | |
182 | dev_name(&pdev->dev), hcd); | |
183 | if (!xhci->shared_hcd) { | |
184 | ret = -ENOMEM; | |
185 | goto dealloc_usb2_hcd; | |
186 | } | |
187 | ||
188 | /* | |
189 | * Set the xHCI pointer before xhci_plat_setup() (aka hcd_driver.reset) | |
190 | * is called by usb_add_hcd(). | |
191 | */ | |
192 | *((struct xhci_hcd **) xhci->shared_hcd->hcd_priv) = xhci; | |
193 | ||
6fa3eb70 S |
194 | #ifdef CONFIG_MTK_XHCI |
195 | ret = usb_add_hcd(xhci->shared_hcd, irq, IRQF_SHARED | IRQF_TRIGGER_LOW); | |
196 | #else | |
3429e91a | 197 | ret = usb_add_hcd(xhci->shared_hcd, irq, IRQF_SHARED); |
6fa3eb70 | 198 | #endif |
3429e91a SAS |
199 | if (ret) |
200 | goto put_usb3_hcd; | |
201 | ||
202 | return 0; | |
203 | ||
204 | put_usb3_hcd: | |
205 | usb_put_hcd(xhci->shared_hcd); | |
206 | ||
207 | dealloc_usb2_hcd: | |
208 | usb_remove_hcd(hcd); | |
209 | ||
210 | unmap_registers: | |
211 | iounmap(hcd->regs); | |
212 | ||
213 | release_mem_region: | |
214 | release_mem_region(hcd->rsrc_start, hcd->rsrc_len); | |
215 | ||
216 | put_hcd: | |
217 | usb_put_hcd(hcd); | |
218 | ||
219 | return ret; | |
220 | } | |
221 | ||
222 | static int xhci_plat_remove(struct platform_device *dev) | |
223 | { | |
224 | struct usb_hcd *hcd = platform_get_drvdata(dev); | |
225 | struct xhci_hcd *xhci = hcd_to_xhci(hcd); | |
226 | ||
a3fdfc64 GR |
227 | xhci->xhc_state |= XHCI_STATE_REMOVING; |
228 | ||
3429e91a SAS |
229 | usb_remove_hcd(xhci->shared_hcd); |
230 | usb_put_hcd(xhci->shared_hcd); | |
231 | ||
232 | usb_remove_hcd(hcd); | |
233 | iounmap(hcd->regs); | |
a5ea8ca0 | 234 | release_mem_region(hcd->rsrc_start, hcd->rsrc_len); |
3429e91a | 235 | usb_put_hcd(hcd); |
6fa3eb70 S |
236 | #ifdef CONFIG_MTK_XHCI |
237 | mtk_xhci_reset(xhci); | |
238 | #endif | |
3429e91a SAS |
239 | kfree(xhci); |
240 | ||
241 | return 0; | |
242 | } | |
243 | ||
6fa3eb70 S |
244 | #ifdef CONFIG_MTK_XHCI |
245 | static const struct of_device_id mtk_xhci_of_match[] = { | |
246 | { | |
247 | .compatible = "mediatek,USB3_XHCI", | |
248 | }, | |
249 | { }, | |
250 | }; | |
251 | #endif | |
252 | ||
3429e91a SAS |
253 | static struct platform_driver usb_xhci_driver = { |
254 | .probe = xhci_plat_probe, | |
255 | .remove = xhci_plat_remove, | |
256 | .driver = { | |
257 | .name = "xhci-hcd", | |
6fa3eb70 S |
258 | #ifdef CONFIG_MTK_XHCI |
259 | .of_match_table = of_match_ptr(mtk_xhci_of_match), | |
260 | #endif | |
3429e91a SAS |
261 | }, |
262 | }; | |
263 | MODULE_ALIAS("platform:xhci-hcd"); | |
264 | ||
265 | int xhci_register_plat(void) | |
266 | { | |
267 | return platform_driver_register(&usb_xhci_driver); | |
268 | } | |
269 | ||
270 | void xhci_unregister_plat(void) | |
271 | { | |
272 | platform_driver_unregister(&usb_xhci_driver); | |
273 | } |