fmc-sdb.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. * Copyright (C) 2012 CERN (www.cern.ch)
  3. * Author: Alessandro Rubini <rubini@gnudd.com>
  4. *
  5. * Released according to the GNU GPL, version 2 or any later version.
  6. *
  7. * This work is part of the White Rabbit project, a research effort led
  8. * by CERN, the European Institute for Nuclear Research.
  9. */
  10. #include <linux/module.h>
  11. #include <linux/slab.h>
  12. #include <linux/fmc.h>
  13. #include <linux/sdb.h>
  14. #include <linux/err.h>
  15. #include <linux/fmc-sdb.h>
  16. #include <asm/byteorder.h>
  17. static uint32_t __sdb_rd(struct fmc_device *fmc, unsigned long address,
  18. int convert)
  19. {
  20. uint32_t res = fmc_readl(fmc, address);
  21. if (convert)
  22. return __be32_to_cpu(res);
  23. return res;
  24. }
  25. static struct sdb_array *__fmc_scan_sdb_tree(struct fmc_device *fmc,
  26. unsigned long sdb_addr,
  27. unsigned long reg_base, int level)
  28. {
  29. uint32_t onew;
  30. int i, j, n, convert = 0;
  31. struct sdb_array *arr, *sub;
  32. onew = fmc_readl(fmc, sdb_addr);
  33. if (onew == SDB_MAGIC) {
  34. /* Uh! If we are little-endian, we must convert */
  35. if (SDB_MAGIC != __be32_to_cpu(SDB_MAGIC))
  36. convert = 1;
  37. } else if (onew == __be32_to_cpu(SDB_MAGIC)) {
  38. /* ok, don't convert */
  39. } else {
  40. return ERR_PTR(-ENOENT);
  41. }
  42. /* So, the magic was there: get the count from offset 4*/
  43. onew = __sdb_rd(fmc, sdb_addr + 4, convert);
  44. n = __be16_to_cpu(*(uint16_t *)&onew);
  45. arr = kzalloc(sizeof(*arr), GFP_KERNEL);
  46. if (!arr)
  47. return ERR_PTR(-ENOMEM);
  48. arr->record = kcalloc(n, sizeof(arr->record[0]), GFP_KERNEL);
  49. arr->subtree = kcalloc(n, sizeof(arr->subtree[0]), GFP_KERNEL);
  50. if (!arr->record || !arr->subtree) {
  51. kfree(arr->record);
  52. kfree(arr->subtree);
  53. kfree(arr);
  54. return ERR_PTR(-ENOMEM);
  55. }
  56. arr->len = n;
  57. arr->level = level;
  58. arr->fmc = fmc;
  59. for (i = 0; i < n; i++) {
  60. union sdb_record *r;
  61. for (j = 0; j < sizeof(arr->record[0]); j += 4) {
  62. *(uint32_t *)((void *)(arr->record + i) + j) =
  63. __sdb_rd(fmc, sdb_addr + (i * 64) + j, convert);
  64. }
  65. r = &arr->record[i];
  66. arr->subtree[i] = ERR_PTR(-ENODEV);
  67. if (r->empty.record_type == sdb_type_bridge) {
  68. struct sdb_component *c = &r->bridge.sdb_component;
  69. uint64_t subaddr = __be64_to_cpu(r->bridge.sdb_child);
  70. uint64_t newbase = __be64_to_cpu(c->addr_first);
  71. subaddr += reg_base;
  72. newbase += reg_base;
  73. sub = __fmc_scan_sdb_tree(fmc, subaddr, newbase,
  74. level + 1);
  75. arr->subtree[i] = sub; /* may be error */
  76. if (IS_ERR(sub))
  77. continue;
  78. sub->parent = arr;
  79. sub->baseaddr = newbase;
  80. }
  81. }
  82. return arr;
  83. }
  84. int fmc_scan_sdb_tree(struct fmc_device *fmc, unsigned long address)
  85. {
  86. struct sdb_array *ret;
  87. if (fmc->sdb)
  88. return -EBUSY;
  89. ret = __fmc_scan_sdb_tree(fmc, address, 0 /* regs */, 0);
  90. if (IS_ERR(ret))
  91. return PTR_ERR(ret);
  92. fmc->sdb = ret;
  93. return 0;
  94. }
  95. EXPORT_SYMBOL(fmc_scan_sdb_tree);
  96. static void __fmc_sdb_free(struct sdb_array *arr)
  97. {
  98. int i, n;
  99. if (!arr)
  100. return;
  101. n = arr->len;
  102. for (i = 0; i < n; i++) {
  103. if (IS_ERR(arr->subtree[i]))
  104. continue;
  105. __fmc_sdb_free(arr->subtree[i]);
  106. }
  107. kfree(arr->record);
  108. kfree(arr->subtree);
  109. kfree(arr);
  110. }
  111. int fmc_free_sdb_tree(struct fmc_device *fmc)
  112. {
  113. __fmc_sdb_free(fmc->sdb);
  114. fmc->sdb = NULL;
  115. return 0;
  116. }
  117. EXPORT_SYMBOL(fmc_free_sdb_tree);
  118. /* This helper calls reprogram and inizialized sdb as well */
  119. int fmc_reprogram_raw(struct fmc_device *fmc, struct fmc_driver *d,
  120. void *gw, unsigned long len, int sdb_entry)
  121. {
  122. int ret;
  123. ret = fmc->op->reprogram_raw(fmc, d, gw, len);
  124. if (ret < 0)
  125. return ret;
  126. if (sdb_entry < 0)
  127. return ret;
  128. /* We are required to find SDB at a given offset */
  129. ret = fmc_scan_sdb_tree(fmc, sdb_entry);
  130. if (ret < 0) {
  131. dev_err(&fmc->dev, "Can't find SDB at address 0x%x\n",
  132. sdb_entry);
  133. return -ENODEV;
  134. }
  135. return 0;
  136. }
  137. EXPORT_SYMBOL(fmc_reprogram_raw);
  138. /* This helper calls reprogram and inizialized sdb as well */
  139. int fmc_reprogram(struct fmc_device *fmc, struct fmc_driver *d, char *gw,
  140. int sdb_entry)
  141. {
  142. int ret;
  143. ret = fmc->op->reprogram(fmc, d, gw);
  144. if (ret < 0)
  145. return ret;
  146. if (sdb_entry < 0)
  147. return ret;
  148. /* We are required to find SDB at a given offset */
  149. ret = fmc_scan_sdb_tree(fmc, sdb_entry);
  150. if (ret < 0) {
  151. dev_err(&fmc->dev, "Can't find SDB at address 0x%x\n",
  152. sdb_entry);
  153. return -ENODEV;
  154. }
  155. return 0;
  156. }
  157. EXPORT_SYMBOL(fmc_reprogram);
  158. void fmc_show_sdb_tree(const struct fmc_device *fmc)
  159. {
  160. pr_err("%s: not supported anymore, use debugfs to dump SDB\n",
  161. __func__);
  162. }
  163. EXPORT_SYMBOL(fmc_show_sdb_tree);
  164. signed long fmc_find_sdb_device(struct sdb_array *tree,
  165. uint64_t vid, uint32_t did, unsigned long *sz)
  166. {
  167. signed long res = -ENODEV;
  168. union sdb_record *r;
  169. struct sdb_product *p;
  170. struct sdb_component *c;
  171. int i, n = tree->len;
  172. uint64_t last, first;
  173. /* FIXME: what if the first interconnect is not at zero? */
  174. for (i = 0; i < n; i++) {
  175. r = &tree->record[i];
  176. c = &r->dev.sdb_component;
  177. p = &c->product;
  178. if (!IS_ERR(tree->subtree[i]))
  179. res = fmc_find_sdb_device(tree->subtree[i],
  180. vid, did, sz);
  181. if (res >= 0)
  182. return res + tree->baseaddr;
  183. if (r->empty.record_type != sdb_type_device)
  184. continue;
  185. if (__be64_to_cpu(p->vendor_id) != vid)
  186. continue;
  187. if (__be32_to_cpu(p->device_id) != did)
  188. continue;
  189. /* found */
  190. last = __be64_to_cpu(c->addr_last);
  191. first = __be64_to_cpu(c->addr_first);
  192. if (sz)
  193. *sz = (typeof(*sz))(last + 1 - first);
  194. return first + tree->baseaddr;
  195. }
  196. return res;
  197. }
  198. EXPORT_SYMBOL(fmc_find_sdb_device);