whci.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /*
  2. * WHCI UWB Multi-interface Controller enumerator.
  3. *
  4. * Copyright (C) 2007 Cambridge Silicon Radio Ltd.
  5. *
  6. * This file is released under the GNU GPL v2.
  7. */
  8. #include <linux/delay.h>
  9. #include <linux/kernel.h>
  10. #include <linux/pci.h>
  11. #include <linux/dma-mapping.h>
  12. #include <linux/slab.h>
  13. #include <linux/uwb/whci.h>
  14. #include <linux/uwb/umc.h>
  15. struct whci_card {
  16. struct pci_dev *pci;
  17. void __iomem *uwbbase;
  18. u8 n_caps;
  19. struct umc_dev *devs[0];
  20. };
  21. /* Fix faulty HW :( */
  22. static
  23. u64 whci_capdata_quirks(struct whci_card *card, u64 capdata)
  24. {
  25. u64 capdata_orig = capdata;
  26. struct pci_dev *pci_dev = card->pci;
  27. if (pci_dev->vendor == PCI_VENDOR_ID_INTEL
  28. && (pci_dev->device == 0x0c3b || pci_dev->device == 0004)
  29. && pci_dev->class == 0x0d1010) {
  30. switch (UWBCAPDATA_TO_CAP_ID(capdata)) {
  31. /* WLP capability has 0x100 bytes of aperture */
  32. case 0x80:
  33. capdata |= 0x40 << 8; break;
  34. /* WUSB capability has 0x80 bytes of aperture
  35. * and ID is 1 */
  36. case 0x02:
  37. capdata &= ~0xffff;
  38. capdata |= 0x2001;
  39. break;
  40. }
  41. }
  42. if (capdata_orig != capdata)
  43. dev_warn(&pci_dev->dev,
  44. "PCI v%04x d%04x c%06x#%02x: "
  45. "corrected capdata from %016Lx to %016Lx\n",
  46. pci_dev->vendor, pci_dev->device, pci_dev->class,
  47. (unsigned)UWBCAPDATA_TO_CAP_ID(capdata),
  48. (unsigned long long)capdata_orig,
  49. (unsigned long long)capdata);
  50. return capdata;
  51. }
  52. /**
  53. * whci_wait_for - wait for a WHCI register to be set
  54. *
  55. * Polls (for at most @max_ms ms) until '*@reg & @mask == @result'.
  56. */
  57. int whci_wait_for(struct device *dev, u32 __iomem *reg, u32 mask, u32 result,
  58. unsigned long max_ms, const char *tag)
  59. {
  60. unsigned t = 0;
  61. u32 val;
  62. for (;;) {
  63. val = le_readl(reg);
  64. if ((val & mask) == result)
  65. break;
  66. if (t >= max_ms) {
  67. dev_err(dev, "%s timed out\n", tag);
  68. return -ETIMEDOUT;
  69. }
  70. msleep(10);
  71. t += 10;
  72. }
  73. return 0;
  74. }
  75. EXPORT_SYMBOL_GPL(whci_wait_for);
  76. /*
  77. * NOTE: the capinfo and capdata registers are slightly different
  78. * (size and cap-id fields). So for cap #0, we need to fill
  79. * in. Size comes from the size of the register block
  80. * (statically calculated); cap_id comes from nowhere, we use
  81. * zero, that is reserved, for the radio controller, because
  82. * none was defined at the spec level.
  83. */
  84. static int whci_add_cap(struct whci_card *card, int n)
  85. {
  86. struct umc_dev *umc;
  87. u64 capdata;
  88. int bar, err;
  89. umc = umc_device_create(&card->pci->dev, n);
  90. if (umc == NULL)
  91. return -ENOMEM;
  92. capdata = le_readq(card->uwbbase + UWBCAPDATA(n));
  93. bar = UWBCAPDATA_TO_BAR(capdata) << 1;
  94. capdata = whci_capdata_quirks(card, capdata);
  95. /* Capability 0 is the radio controller. It's size is 32
  96. * bytes (WHCI0.95[2.3, T2-9]). */
  97. umc->version = UWBCAPDATA_TO_VERSION(capdata);
  98. umc->cap_id = n == 0 ? 0 : UWBCAPDATA_TO_CAP_ID(capdata);
  99. umc->bar = bar;
  100. umc->resource.start = pci_resource_start(card->pci, bar)
  101. + UWBCAPDATA_TO_OFFSET(capdata);
  102. umc->resource.end = umc->resource.start
  103. + (n == 0 ? 0x20 : UWBCAPDATA_TO_SIZE(capdata)) - 1;
  104. umc->resource.name = dev_name(&umc->dev);
  105. umc->resource.flags = card->pci->resource[bar].flags;
  106. umc->resource.parent = &card->pci->resource[bar];
  107. umc->irq = card->pci->irq;
  108. err = umc_device_register(umc);
  109. if (err < 0)
  110. goto error;
  111. card->devs[n] = umc;
  112. return 0;
  113. error:
  114. kfree(umc);
  115. return err;
  116. }
  117. static void whci_del_cap(struct whci_card *card, int n)
  118. {
  119. struct umc_dev *umc = card->devs[n];
  120. if (umc != NULL)
  121. umc_device_unregister(umc);
  122. }
  123. static int whci_n_caps(struct pci_dev *pci)
  124. {
  125. void __iomem *uwbbase;
  126. u64 capinfo;
  127. uwbbase = pci_iomap(pci, 0, 8);
  128. if (!uwbbase)
  129. return -ENOMEM;
  130. capinfo = le_readq(uwbbase + UWBCAPINFO);
  131. pci_iounmap(pci, uwbbase);
  132. return UWBCAPINFO_TO_N_CAPS(capinfo);
  133. }
  134. static int whci_probe(struct pci_dev *pci, const struct pci_device_id *id)
  135. {
  136. struct whci_card *card;
  137. int err, n_caps, n;
  138. err = pci_enable_device(pci);
  139. if (err < 0)
  140. goto error;
  141. pci_enable_msi(pci);
  142. pci_set_master(pci);
  143. err = -ENXIO;
  144. if (!pci_set_dma_mask(pci, DMA_BIT_MASK(64)))
  145. pci_set_consistent_dma_mask(pci, DMA_BIT_MASK(64));
  146. else if (!pci_set_dma_mask(pci, DMA_BIT_MASK(32)))
  147. pci_set_consistent_dma_mask(pci, DMA_BIT_MASK(32));
  148. else
  149. goto error_dma;
  150. err = n_caps = whci_n_caps(pci);
  151. if (n_caps < 0)
  152. goto error_ncaps;
  153. err = -ENOMEM;
  154. card = kzalloc(sizeof(struct whci_card)
  155. + sizeof(struct whci_dev *) * (n_caps + 1),
  156. GFP_KERNEL);
  157. if (card == NULL)
  158. goto error_kzalloc;
  159. card->pci = pci;
  160. card->n_caps = n_caps;
  161. err = -EBUSY;
  162. if (!request_mem_region(pci_resource_start(pci, 0),
  163. UWBCAPDATA_SIZE(card->n_caps),
  164. "whci (capability data)"))
  165. goto error_request_memregion;
  166. err = -ENOMEM;
  167. card->uwbbase = pci_iomap(pci, 0, UWBCAPDATA_SIZE(card->n_caps));
  168. if (!card->uwbbase)
  169. goto error_iomap;
  170. /* Add each capability. */
  171. for (n = 0; n <= card->n_caps; n++) {
  172. err = whci_add_cap(card, n);
  173. if (err < 0 && n == 0) {
  174. dev_err(&pci->dev, "cannot bind UWB radio controller:"
  175. " %d\n", err);
  176. goto error_bind;
  177. }
  178. if (err < 0)
  179. dev_warn(&pci->dev, "warning: cannot bind capability "
  180. "#%u: %d\n", n, err);
  181. }
  182. pci_set_drvdata(pci, card);
  183. return 0;
  184. error_bind:
  185. pci_iounmap(pci, card->uwbbase);
  186. error_iomap:
  187. release_mem_region(pci_resource_start(pci, 0), UWBCAPDATA_SIZE(card->n_caps));
  188. error_request_memregion:
  189. kfree(card);
  190. error_kzalloc:
  191. error_ncaps:
  192. error_dma:
  193. pci_disable_msi(pci);
  194. pci_disable_device(pci);
  195. error:
  196. return err;
  197. }
  198. static void whci_remove(struct pci_dev *pci)
  199. {
  200. struct whci_card *card = pci_get_drvdata(pci);
  201. int n;
  202. pci_set_drvdata(pci, NULL);
  203. /* Unregister each capability in reverse (so the master device
  204. * is unregistered last). */
  205. for (n = card->n_caps; n >= 0 ; n--)
  206. whci_del_cap(card, n);
  207. pci_iounmap(pci, card->uwbbase);
  208. release_mem_region(pci_resource_start(pci, 0), UWBCAPDATA_SIZE(card->n_caps));
  209. kfree(card);
  210. pci_disable_msi(pci);
  211. pci_disable_device(pci);
  212. }
  213. static struct pci_device_id whci_id_table[] = {
  214. { PCI_DEVICE_CLASS(PCI_CLASS_WIRELESS_WHCI, ~0) },
  215. { 0 },
  216. };
  217. MODULE_DEVICE_TABLE(pci, whci_id_table);
  218. static struct pci_driver whci_driver = {
  219. .name = "whci",
  220. .id_table = whci_id_table,
  221. .probe = whci_probe,
  222. .remove = whci_remove,
  223. };
  224. static int __init whci_init(void)
  225. {
  226. return pci_register_driver(&whci_driver);
  227. }
  228. static void __exit whci_exit(void)
  229. {
  230. pci_unregister_driver(&whci_driver);
  231. }
  232. module_init(whci_init);
  233. module_exit(whci_exit);
  234. MODULE_DESCRIPTION("WHCI UWB Multi-interface Controller enumerator");
  235. MODULE_AUTHOR("Cambridge Silicon Radio Ltd.");
  236. MODULE_LICENSE("GPL");