2 * WHCI UWB Multi-interface Controller enumerator.
4 * Copyright (C) 2007 Cambridge Silicon Radio Ltd.
6 * This file is released under the GNU GPL v2.
8 #include <linux/delay.h>
9 #include <linux/kernel.h>
10 #include <linux/module.h>
11 #include <linux/pci.h>
12 #include <linux/dma-mapping.h>
13 #include <linux/slab.h>
14 #include <linux/uwb/whci.h>
15 #include <linux/uwb/umc.h>
19 void __iomem *uwbbase;
21 struct umc_dev *devs[0];
25 /* Fix faulty HW :( */
27 u64 whci_capdata_quirks(struct whci_card *card, u64 capdata)
29 u64 capdata_orig = capdata;
30 struct pci_dev *pci_dev = card->pci;
31 if (pci_dev->vendor == PCI_VENDOR_ID_INTEL
32 && (pci_dev->device == 0x0c3b || pci_dev->device == 0004)
33 && pci_dev->class == 0x0d1010) {
34 switch (UWBCAPDATA_TO_CAP_ID(capdata)) {
35 /* WLP capability has 0x100 bytes of aperture */
37 capdata |= 0x40 << 8; break;
38 /* WUSB capability has 0x80 bytes of aperture
46 if (capdata_orig != capdata)
47 dev_warn(&pci_dev->dev,
48 "PCI v%04x d%04x c%06x#%02x: "
49 "corrected capdata from %016Lx to %016Lx\n",
50 pci_dev->vendor, pci_dev->device, pci_dev->class,
51 (unsigned)UWBCAPDATA_TO_CAP_ID(capdata),
52 (unsigned long long)capdata_orig,
53 (unsigned long long)capdata);
59 * whci_wait_for - wait for a WHCI register to be set
61 * Polls (for at most @max_ms ms) until '*@reg & @mask == @result'.
63 int whci_wait_for(struct device *dev, u32 __iomem *reg, u32 mask, u32 result,
64 unsigned long max_ms, const char *tag)
70 if ((val & mask) == result)
73 dev_err(dev, "%s timed out\n", tag);
81 EXPORT_SYMBOL_GPL(whci_wait_for);
85 * NOTE: the capinfo and capdata registers are slightly different
86 * (size and cap-id fields). So for cap #0, we need to fill
87 * in. Size comes from the size of the register block
88 * (statically calculated); cap_id comes from nowhere, we use
89 * zero, that is reserved, for the radio controller, because
90 * none was defined at the spec level.
92 static int whci_add_cap(struct whci_card *card, int n)
98 umc = umc_device_create(&card->pci->dev, n);
102 capdata = le_readq(card->uwbbase + UWBCAPDATA(n));
104 bar = UWBCAPDATA_TO_BAR(capdata) << 1;
106 capdata = whci_capdata_quirks(card, capdata);
107 /* Capability 0 is the radio controller. It's size is 32
108 * bytes (WHCI0.95[2.3, T2-9]). */
109 umc->version = UWBCAPDATA_TO_VERSION(capdata);
110 umc->cap_id = n == 0 ? 0 : UWBCAPDATA_TO_CAP_ID(capdata);
112 umc->resource.start = pci_resource_start(card->pci, bar)
113 + UWBCAPDATA_TO_OFFSET(capdata);
114 umc->resource.end = umc->resource.start
115 + (n == 0 ? 0x20 : UWBCAPDATA_TO_SIZE(capdata)) - 1;
116 umc->resource.name = dev_name(&umc->dev);
117 umc->resource.flags = card->pci->resource[bar].flags;
118 umc->resource.parent = &card->pci->resource[bar];
119 umc->irq = card->pci->irq;
121 err = umc_device_register(umc);
132 static void whci_del_cap(struct whci_card *card, int n)
134 struct umc_dev *umc = card->devs[n];
137 umc_device_unregister(umc);
140 static int whci_n_caps(struct pci_dev *pci)
142 void __iomem *uwbbase;
145 uwbbase = pci_iomap(pci, 0, 8);
148 capinfo = le_readq(uwbbase + UWBCAPINFO);
149 pci_iounmap(pci, uwbbase);
151 return UWBCAPINFO_TO_N_CAPS(capinfo);
154 static int whci_probe(struct pci_dev *pci, const struct pci_device_id *id)
156 struct whci_card *card;
159 err = pci_enable_device(pci);
165 if (!pci_set_dma_mask(pci, DMA_BIT_MASK(64)))
166 pci_set_consistent_dma_mask(pci, DMA_BIT_MASK(64));
167 else if (!pci_set_dma_mask(pci, DMA_BIT_MASK(32)))
168 pci_set_consistent_dma_mask(pci, DMA_BIT_MASK(32));
172 err = n_caps = whci_n_caps(pci);
177 card = kzalloc(sizeof(struct whci_card)
178 + sizeof(struct whci_dev *) * (n_caps + 1),
183 card->n_caps = n_caps;
186 if (!request_mem_region(pci_resource_start(pci, 0),
187 UWBCAPDATA_SIZE(card->n_caps),
188 "whci (capability data)"))
189 goto error_request_memregion;
191 card->uwbbase = pci_iomap(pci, 0, UWBCAPDATA_SIZE(card->n_caps));
195 /* Add each capability. */
196 for (n = 0; n <= card->n_caps; n++) {
197 err = whci_add_cap(card, n);
198 if (err < 0 && n == 0) {
199 dev_err(&pci->dev, "cannot bind UWB radio controller:"
204 dev_warn(&pci->dev, "warning: cannot bind capability "
205 "#%u: %d\n", n, err);
207 pci_set_drvdata(pci, card);
211 pci_iounmap(pci, card->uwbbase);
213 release_mem_region(pci_resource_start(pci, 0), UWBCAPDATA_SIZE(card->n_caps));
214 error_request_memregion:
219 pci_disable_msi(pci);
220 pci_disable_device(pci);
225 static void whci_remove(struct pci_dev *pci)
227 struct whci_card *card = pci_get_drvdata(pci);
230 pci_set_drvdata(pci, NULL);
231 /* Unregister each capability in reverse (so the master device
232 * is unregistered last). */
233 for (n = card->n_caps; n >= 0 ; n--)
234 whci_del_cap(card, n);
235 pci_iounmap(pci, card->uwbbase);
236 release_mem_region(pci_resource_start(pci, 0), UWBCAPDATA_SIZE(card->n_caps));
238 pci_disable_msi(pci);
239 pci_disable_device(pci);
242 static struct pci_device_id whci_id_table[] = {
243 { PCI_DEVICE_CLASS(PCI_CLASS_WIRELESS_WHCI, ~0) },
246 MODULE_DEVICE_TABLE(pci, whci_id_table);
249 static struct pci_driver whci_driver = {
251 .id_table = whci_id_table,
253 .remove = whci_remove,
256 static int __init whci_init(void)
258 return pci_register_driver(&whci_driver);
261 static void __exit whci_exit(void)
263 pci_unregister_driver(&whci_driver);
266 module_init(whci_init);
267 module_exit(whci_exit);
269 MODULE_DESCRIPTION("WHCI UWB Multi-interface Controller enumerator");
270 MODULE_AUTHOR("Cambridge Silicon Radio Ltd.");
271 MODULE_LICENSE("GPL");