USB: add SPDX identifiers to all remaining files in drivers/usb/
[linux-2.6-block.git] / drivers / usb / host / ssb-hcd.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Sonics Silicon Backplane
4  * Broadcom USB-core driver  (SSB bus glue)
5  *
6  * Copyright 2011-2012 Hauke Mehrtens <hauke@hauke-m.de>
7  *
8  * Based on ssb-ohci driver
9  * Copyright 2007 Michael Buesch <m@bues.ch>
10  *
11  * Derived from the OHCI-PCI driver
12  * Copyright 1999 Roman Weissgaerber
13  * Copyright 2000-2002 David Brownell
14  * Copyright 1999 Linus Torvalds
15  * Copyright 1999 Gregory P. Smith
16  *
17  * Derived from the USBcore related parts of Broadcom-SB
18  * Copyright 2005-2011 Broadcom Corporation
19  *
20  * Licensed under the GNU/GPL. See COPYING for details.
21  */
22 #include <linux/ssb/ssb.h>
23 #include <linux/delay.h>
24 #include <linux/platform_device.h>
25 #include <linux/module.h>
26 #include <linux/slab.h>
27 #include <linux/usb/ehci_pdriver.h>
28 #include <linux/usb/ohci_pdriver.h>
29
30 MODULE_AUTHOR("Hauke Mehrtens");
31 MODULE_DESCRIPTION("Common USB driver for SSB Bus");
32 MODULE_LICENSE("GPL");
33
34 #define SSB_HCD_TMSLOW_HOSTMODE (1 << 29)
35
36 struct ssb_hcd_device {
37         struct platform_device *ehci_dev;
38         struct platform_device *ohci_dev;
39
40         u32 enable_flags;
41 };
42
43 static void ssb_hcd_5354wa(struct ssb_device *dev)
44 {
45 #ifdef CONFIG_SSB_DRIVER_MIPS
46         /* Work around for 5354 failures */
47         if (dev->id.revision == 2 && dev->bus->chip_id == 0x5354) {
48                 /* Change syn01 reg */
49                 ssb_write32(dev, 0x894, 0x00fe00fe);
50
51                 /* Change syn03 reg */
52                 ssb_write32(dev, 0x89c, ssb_read32(dev, 0x89c) | 0x1);
53         }
54 #endif
55 }
56
57 static void ssb_hcd_usb20wa(struct ssb_device *dev)
58 {
59         if (dev->id.coreid == SSB_DEV_USB20_HOST) {
60                 /*
61                  * USB 2.0 special considerations:
62                  *
63                  * In addition to the standard SSB reset sequence, the Host
64                  * Control Register must be programmed to bring the USB core
65                  * and various phy components out of reset.
66                  */
67                 ssb_write32(dev, 0x200, 0x7ff);
68
69                 /* Change Flush control reg */
70                 ssb_write32(dev, 0x400, ssb_read32(dev, 0x400) & ~8);
71                 ssb_read32(dev, 0x400);
72
73                 /* Change Shim control reg */
74                 ssb_write32(dev, 0x304, ssb_read32(dev, 0x304) & ~0x100);
75                 ssb_read32(dev, 0x304);
76
77                 udelay(1);
78
79                 ssb_hcd_5354wa(dev);
80         }
81 }
82
83 /* based on arch/mips/brcm-boards/bcm947xx/pcibios.c */
84 static u32 ssb_hcd_init_chip(struct ssb_device *dev)
85 {
86         u32 flags = 0;
87
88         if (dev->id.coreid == SSB_DEV_USB11_HOSTDEV)
89                 /* Put the device into host-mode. */
90                 flags |= SSB_HCD_TMSLOW_HOSTMODE;
91
92         ssb_device_enable(dev, flags);
93
94         ssb_hcd_usb20wa(dev);
95
96         return flags;
97 }
98
99 static const struct usb_ehci_pdata ehci_pdata = {
100 };
101
102 static const struct usb_ohci_pdata ohci_pdata = {
103 };
104
105 static struct platform_device *ssb_hcd_create_pdev(struct ssb_device *dev, bool ohci, u32 addr, u32 len)
106 {
107         struct platform_device *hci_dev;
108         struct resource hci_res[2];
109         int ret;
110
111         memset(hci_res, 0, sizeof(hci_res));
112
113         hci_res[0].start = addr;
114         hci_res[0].end = hci_res[0].start + len - 1;
115         hci_res[0].flags = IORESOURCE_MEM;
116
117         hci_res[1].start = dev->irq;
118         hci_res[1].flags = IORESOURCE_IRQ;
119
120         hci_dev = platform_device_alloc(ohci ? "ohci-platform" :
121                                         "ehci-platform" , 0);
122         if (!hci_dev)
123                 return ERR_PTR(-ENOMEM);
124
125         hci_dev->dev.parent = dev->dev;
126         hci_dev->dev.dma_mask = &hci_dev->dev.coherent_dma_mask;
127
128         ret = platform_device_add_resources(hci_dev, hci_res,
129                                             ARRAY_SIZE(hci_res));
130         if (ret)
131                 goto err_alloc;
132         if (ohci)
133                 ret = platform_device_add_data(hci_dev, &ohci_pdata,
134                                                sizeof(ohci_pdata));
135         else
136                 ret = platform_device_add_data(hci_dev, &ehci_pdata,
137                                                sizeof(ehci_pdata));
138         if (ret)
139                 goto err_alloc;
140         ret = platform_device_add(hci_dev);
141         if (ret)
142                 goto err_alloc;
143
144         return hci_dev;
145
146 err_alloc:
147         platform_device_put(hci_dev);
148         return ERR_PTR(ret);
149 }
150
151 static int ssb_hcd_probe(struct ssb_device *dev,
152                                    const struct ssb_device_id *id)
153 {
154         int err, tmp;
155         int start, len;
156         u16 chipid_top;
157         u16 coreid = dev->id.coreid;
158         struct ssb_hcd_device *usb_dev;
159
160         /* USBcores are only connected on embedded devices. */
161         chipid_top = (dev->bus->chip_id & 0xFF00);
162         if (chipid_top != 0x4700 && chipid_top != 0x5300)
163                 return -ENODEV;
164
165         /* TODO: Probably need checks here; is the core connected? */
166
167         if (dma_set_mask_and_coherent(dev->dma_dev, DMA_BIT_MASK(32)))
168                 return -EOPNOTSUPP;
169
170         usb_dev = devm_kzalloc(dev->dev, sizeof(struct ssb_hcd_device),
171                                GFP_KERNEL);
172         if (!usb_dev)
173                 return -ENOMEM;
174
175         /* We currently always attach SSB_DEV_USB11_HOSTDEV
176          * as HOST OHCI. If we want to attach it as Client device,
177          * we must branch here and call into the (yet to
178          * be written) Client mode driver. Same for remove(). */
179         usb_dev->enable_flags = ssb_hcd_init_chip(dev);
180
181         tmp = ssb_read32(dev, SSB_ADMATCH0);
182
183         start = ssb_admatch_base(tmp);
184         len = (coreid == SSB_DEV_USB20_HOST) ? 0x800 : ssb_admatch_size(tmp);
185         usb_dev->ohci_dev = ssb_hcd_create_pdev(dev, true, start, len);
186         if (IS_ERR(usb_dev->ohci_dev))
187                 return PTR_ERR(usb_dev->ohci_dev);
188
189         if (coreid == SSB_DEV_USB20_HOST) {
190                 start = ssb_admatch_base(tmp) + 0x800; /* ehci core offset */
191                 usb_dev->ehci_dev = ssb_hcd_create_pdev(dev, false, start, len);
192                 if (IS_ERR(usb_dev->ehci_dev)) {
193                         err = PTR_ERR(usb_dev->ehci_dev);
194                         goto err_unregister_ohci_dev;
195                 }
196         }
197
198         ssb_set_drvdata(dev, usb_dev);
199         return 0;
200
201 err_unregister_ohci_dev:
202         platform_device_unregister(usb_dev->ohci_dev);
203         return err;
204 }
205
206 static void ssb_hcd_remove(struct ssb_device *dev)
207 {
208         struct ssb_hcd_device *usb_dev = ssb_get_drvdata(dev);
209         struct platform_device *ohci_dev = usb_dev->ohci_dev;
210         struct platform_device *ehci_dev = usb_dev->ehci_dev;
211
212         if (ohci_dev)
213                 platform_device_unregister(ohci_dev);
214         if (ehci_dev)
215                 platform_device_unregister(ehci_dev);
216
217         ssb_device_disable(dev, 0);
218 }
219
220 static void ssb_hcd_shutdown(struct ssb_device *dev)
221 {
222         ssb_device_disable(dev, 0);
223 }
224
225 #ifdef CONFIG_PM
226
227 static int ssb_hcd_suspend(struct ssb_device *dev, pm_message_t state)
228 {
229         ssb_device_disable(dev, 0);
230
231         return 0;
232 }
233
234 static int ssb_hcd_resume(struct ssb_device *dev)
235 {
236         struct ssb_hcd_device *usb_dev = ssb_get_drvdata(dev);
237
238         ssb_device_enable(dev, usb_dev->enable_flags);
239
240         return 0;
241 }
242
243 #else /* !CONFIG_PM */
244 #define ssb_hcd_suspend NULL
245 #define ssb_hcd_resume  NULL
246 #endif /* CONFIG_PM */
247
248 static const struct ssb_device_id ssb_hcd_table[] = {
249         SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_USB11_HOSTDEV, SSB_ANY_REV),
250         SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_USB11_HOST, SSB_ANY_REV),
251         SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_USB20_HOST, SSB_ANY_REV),
252         {},
253 };
254 MODULE_DEVICE_TABLE(ssb, ssb_hcd_table);
255
256 static struct ssb_driver ssb_hcd_driver = {
257         .name           = KBUILD_MODNAME,
258         .id_table       = ssb_hcd_table,
259         .probe          = ssb_hcd_probe,
260         .remove         = ssb_hcd_remove,
261         .shutdown       = ssb_hcd_shutdown,
262         .suspend        = ssb_hcd_suspend,
263         .resume         = ssb_hcd_resume,
264 };
265
266 static int __init ssb_hcd_init(void)
267 {
268         return ssb_driver_register(&ssb_hcd_driver);
269 }
270 module_init(ssb_hcd_init);
271
272 static void __exit ssb_hcd_exit(void)
273 {
274         ssb_driver_unregister(&ssb_hcd_driver);
275 }
276 module_exit(ssb_hcd_exit);