USB: bcma: fix error handling in bcma_hcd_create_pdev()
[linux-2.6-block.git] / drivers / usb / host / bcma-hcd.c
CommitLineData
62e11d1b
HM
1/*
2 * Broadcom specific Advanced Microcontroller Bus
3 * Broadcom USB-core driver (BCMA bus glue)
4 *
5 * Copyright 2011-2012 Hauke Mehrtens <hauke@hauke-m.de>
6 *
7 * Based on ssb-ohci driver
8 * Copyright 2007 Michael Buesch <m@bues.ch>
9 *
10 * Derived from the OHCI-PCI driver
11 * Copyright 1999 Roman Weissgaerber
12 * Copyright 2000-2002 David Brownell
13 * Copyright 1999 Linus Torvalds
14 * Copyright 1999 Gregory P. Smith
15 *
16 * Derived from the USBcore related parts of Broadcom-SB
17 * Copyright 2005-2011 Broadcom Corporation
18 *
19 * Licensed under the GNU/GPL. See COPYING for details.
20 */
21#include <linux/bcma/bcma.h>
22#include <linux/delay.h>
23#include <linux/platform_device.h>
24#include <linux/module.h>
6ba0d809 25#include <linux/slab.h>
62e11d1b
HM
26#include <linux/usb/ehci_pdriver.h>
27#include <linux/usb/ohci_pdriver.h>
28
29MODULE_AUTHOR("Hauke Mehrtens");
30MODULE_DESCRIPTION("Common USB driver for BCMA Bus");
31MODULE_LICENSE("GPL");
32
33struct bcma_hcd_device {
34 struct platform_device *ehci_dev;
35 struct platform_device *ohci_dev;
36};
37
38/* Wait for bitmask in a register to get set or cleared.
39 * timeout is in units of ten-microseconds.
40 */
41static int bcma_wait_bits(struct bcma_device *dev, u16 reg, u32 bitmask,
42 int timeout)
43{
44 int i;
45 u32 val;
46
47 for (i = 0; i < timeout; i++) {
48 val = bcma_read32(dev, reg);
49 if ((val & bitmask) == bitmask)
50 return 0;
51 udelay(10);
52 }
53
54 return -ETIMEDOUT;
55}
56
41ac7b3a 57static void bcma_hcd_4716wa(struct bcma_device *dev)
62e11d1b
HM
58{
59#ifdef CONFIG_BCMA_DRIVER_MIPS
60 /* Work around for 4716 failures. */
61 if (dev->bus->chipinfo.id == 0x4716) {
62 u32 tmp;
63
64 tmp = bcma_cpu_clock(&dev->bus->drv_mips);
65 if (tmp >= 480000000)
66 tmp = 0x1846b; /* set CDR to 0x11(fast) */
67 else if (tmp == 453000000)
68 tmp = 0x1046b; /* set CDR to 0x10(slow) */
69 else
70 tmp = 0;
71
72 /* Change Shim mdio control reg to fix host not acking at
73 * high frequencies
74 */
75 if (tmp) {
76 bcma_write32(dev, 0x524, 0x1); /* write sel to enable */
77 udelay(500);
78
79 bcma_write32(dev, 0x524, tmp);
80 udelay(500);
81 bcma_write32(dev, 0x524, 0x4ab);
82 udelay(500);
83 bcma_read32(dev, 0x528);
84 bcma_write32(dev, 0x528, 0x80000000);
85 }
86 }
87#endif /* CONFIG_BCMA_DRIVER_MIPS */
88}
89
90/* based on arch/mips/brcm-boards/bcm947xx/pcibios.c */
41ac7b3a 91static void bcma_hcd_init_chip(struct bcma_device *dev)
62e11d1b
HM
92{
93 u32 tmp;
94
95 /*
96 * USB 2.0 special considerations:
97 *
98 * 1. Since the core supports both OHCI and EHCI functions, it must
99 * only be reset once.
100 *
101 * 2. In addition to the standard SI reset sequence, the Host Control
102 * Register must be programmed to bring the USB core and various
103 * phy components out of reset.
104 */
105 if (!bcma_core_is_enabled(dev)) {
106 bcma_core_enable(dev, 0);
107 mdelay(10);
108 if (dev->id.rev >= 5) {
109 /* Enable Misc PLL */
110 tmp = bcma_read32(dev, 0x1e0);
111 tmp |= 0x100;
112 bcma_write32(dev, 0x1e0, tmp);
113 if (bcma_wait_bits(dev, 0x1e0, 1 << 24, 100))
114 printk(KERN_EMERG "Failed to enable misc PPL!\n");
115
116 /* Take out of resets */
117 bcma_write32(dev, 0x200, 0x4ff);
118 udelay(25);
119 bcma_write32(dev, 0x200, 0x6ff);
120 udelay(25);
121
122 /* Make sure digital and AFE are locked in USB PHY */
123 bcma_write32(dev, 0x524, 0x6b);
124 udelay(50);
125 tmp = bcma_read32(dev, 0x524);
126 udelay(50);
127 bcma_write32(dev, 0x524, 0xab);
128 udelay(50);
129 tmp = bcma_read32(dev, 0x524);
130 udelay(50);
131 bcma_write32(dev, 0x524, 0x2b);
132 udelay(50);
133 tmp = bcma_read32(dev, 0x524);
134 udelay(50);
135 bcma_write32(dev, 0x524, 0x10ab);
136 udelay(50);
137 tmp = bcma_read32(dev, 0x524);
138
139 if (bcma_wait_bits(dev, 0x528, 0xc000, 10000)) {
140 tmp = bcma_read32(dev, 0x528);
141 printk(KERN_EMERG
142 "USB20H mdio_rddata 0x%08x\n", tmp);
143 }
144 bcma_write32(dev, 0x528, 0x80000000);
145 tmp = bcma_read32(dev, 0x314);
146 udelay(265);
147 bcma_write32(dev, 0x200, 0x7ff);
148 udelay(10);
149
150 /* Take USB and HSIC out of non-driving modes */
151 bcma_write32(dev, 0x510, 0);
152 } else {
153 bcma_write32(dev, 0x200, 0x7ff);
154
155 udelay(1);
156 }
157
158 bcma_hcd_4716wa(dev);
159 }
160}
161
162static const struct usb_ehci_pdata ehci_pdata = {
163};
164
165static const struct usb_ohci_pdata ohci_pdata = {
166};
167
41ac7b3a 168static struct platform_device *bcma_hcd_create_pdev(struct bcma_device *dev, bool ohci, u32 addr)
62e11d1b
HM
169{
170 struct platform_device *hci_dev;
171 struct resource hci_res[2];
ab2de579 172 int ret;
62e11d1b
HM
173
174 memset(hci_res, 0, sizeof(hci_res));
175
176 hci_res[0].start = addr;
177 hci_res[0].end = hci_res[0].start + 0x1000 - 1;
178 hci_res[0].flags = IORESOURCE_MEM;
179
180 hci_res[1].start = dev->irq;
181 hci_res[1].flags = IORESOURCE_IRQ;
182
183 hci_dev = platform_device_alloc(ohci ? "ohci-platform" :
184 "ehci-platform" , 0);
185 if (!hci_dev)
ab2de579 186 return ERR_PTR(-ENOMEM);
62e11d1b
HM
187
188 hci_dev->dev.parent = &dev->dev;
189 hci_dev->dev.dma_mask = &hci_dev->dev.coherent_dma_mask;
190
191 ret = platform_device_add_resources(hci_dev, hci_res,
192 ARRAY_SIZE(hci_res));
193 if (ret)
194 goto err_alloc;
195 if (ohci)
196 ret = platform_device_add_data(hci_dev, &ohci_pdata,
197 sizeof(ohci_pdata));
198 else
199 ret = platform_device_add_data(hci_dev, &ehci_pdata,
200 sizeof(ehci_pdata));
201 if (ret)
202 goto err_alloc;
203 ret = platform_device_add(hci_dev);
204 if (ret)
205 goto err_alloc;
206
207 return hci_dev;
208
209err_alloc:
210 platform_device_put(hci_dev);
211 return ERR_PTR(ret);
212}
213
41ac7b3a 214static int bcma_hcd_probe(struct bcma_device *dev)
62e11d1b
HM
215{
216 int err;
62e11d1b
HM
217 u32 ohci_addr;
218 struct bcma_hcd_device *usb_dev;
219 struct bcma_chipinfo *chipinfo;
220
221 chipinfo = &dev->bus->chipinfo;
62e11d1b
HM
222
223 /* TODO: Probably need checks here; is the core connected? */
224
d288059e 225 if (dma_set_mask_and_coherent(dev->dma_dev, DMA_BIT_MASK(32)))
62e11d1b
HM
226 return -EOPNOTSUPP;
227
c27da2b2
HM
228 usb_dev = devm_kzalloc(&dev->dev, sizeof(struct bcma_hcd_device),
229 GFP_KERNEL);
62e11d1b
HM
230 if (!usb_dev)
231 return -ENOMEM;
232
233 bcma_hcd_init_chip(dev);
234
235 /* In AI chips EHCI is addrspace 0, OHCI is 1 */
23a2f39c 236 ohci_addr = dev->addr_s[0];
98e13e05
HM
237 if ((chipinfo->id == BCMA_CHIP_ID_BCM5357 ||
238 chipinfo->id == BCMA_CHIP_ID_BCM4749)
62e11d1b
HM
239 && chipinfo->rev == 0)
240 ohci_addr = 0x18009000;
241
242 usb_dev->ohci_dev = bcma_hcd_create_pdev(dev, true, ohci_addr);
c27da2b2
HM
243 if (IS_ERR(usb_dev->ohci_dev))
244 return PTR_ERR(usb_dev->ohci_dev);
62e11d1b
HM
245
246 usb_dev->ehci_dev = bcma_hcd_create_pdev(dev, false, dev->addr);
247 if (IS_ERR(usb_dev->ehci_dev)) {
248 err = PTR_ERR(usb_dev->ehci_dev);
249 goto err_unregister_ohci_dev;
250 }
251
252 bcma_set_drvdata(dev, usb_dev);
253 return 0;
254
255err_unregister_ohci_dev:
256 platform_device_unregister(usb_dev->ohci_dev);
62e11d1b
HM
257 return err;
258}
259
fb4e98ab 260static void bcma_hcd_remove(struct bcma_device *dev)
62e11d1b
HM
261{
262 struct bcma_hcd_device *usb_dev = bcma_get_drvdata(dev);
263 struct platform_device *ohci_dev = usb_dev->ohci_dev;
264 struct platform_device *ehci_dev = usb_dev->ehci_dev;
265
266 if (ohci_dev)
267 platform_device_unregister(ohci_dev);
268 if (ehci_dev)
269 platform_device_unregister(ehci_dev);
270
271 bcma_core_disable(dev, 0);
272}
273
274static void bcma_hcd_shutdown(struct bcma_device *dev)
275{
276 bcma_core_disable(dev, 0);
277}
278
279#ifdef CONFIG_PM
280
1f6155f5 281static int bcma_hcd_suspend(struct bcma_device *dev)
62e11d1b
HM
282{
283 bcma_core_disable(dev, 0);
284
285 return 0;
286}
287
288static int bcma_hcd_resume(struct bcma_device *dev)
289{
290 bcma_core_enable(dev, 0);
291
292 return 0;
293}
294
295#else /* !CONFIG_PM */
296#define bcma_hcd_suspend NULL
297#define bcma_hcd_resume NULL
298#endif /* CONFIG_PM */
299
2f82686e 300static const struct bcma_device_id bcma_hcd_table[] = {
62e11d1b 301 BCMA_CORE(BCMA_MANUF_BCM, BCMA_CORE_USB20_HOST, BCMA_ANY_REV, BCMA_ANY_CLASS),
f7219b52 302 {},
62e11d1b
HM
303};
304MODULE_DEVICE_TABLE(bcma, bcma_hcd_table);
305
306static struct bcma_driver bcma_hcd_driver = {
307 .name = KBUILD_MODNAME,
308 .id_table = bcma_hcd_table,
309 .probe = bcma_hcd_probe,
7690417d 310 .remove = bcma_hcd_remove,
62e11d1b
HM
311 .shutdown = bcma_hcd_shutdown,
312 .suspend = bcma_hcd_suspend,
313 .resume = bcma_hcd_resume,
314};
315
316static int __init bcma_hcd_init(void)
317{
318 return bcma_driver_register(&bcma_hcd_driver);
319}
320module_init(bcma_hcd_init);
321
322static void __exit bcma_hcd_exit(void)
323{
324 bcma_driver_unregister(&bcma_hcd_driver);
325}
326module_exit(bcma_hcd_exit);