mcb-parse.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. #include <linux/types.h>
  2. #include <linux/ioport.h>
  3. #include <linux/slab.h>
  4. #include <linux/export.h>
  5. #include <linux/io.h>
  6. #include <linux/mcb.h>
  7. #include "mcb-internal.h"
  8. struct mcb_parse_priv {
  9. phys_addr_t mapbase;
  10. void __iomem *base;
  11. };
  12. #define for_each_chameleon_cell(dtype, p) \
  13. for ((dtype) = get_next_dtype((p)); \
  14. (dtype) != CHAMELEON_DTYPE_END; \
  15. (dtype) = get_next_dtype((p)))
  16. static inline uint32_t get_next_dtype(void __iomem *p)
  17. {
  18. uint32_t dtype;
  19. dtype = readl(p);
  20. return dtype >> 28;
  21. }
  22. static int chameleon_parse_bdd(struct mcb_bus *bus,
  23. phys_addr_t mapbase,
  24. void __iomem *base)
  25. {
  26. return 0;
  27. }
  28. static int chameleon_parse_gdd(struct mcb_bus *bus,
  29. phys_addr_t mapbase,
  30. void __iomem *base)
  31. {
  32. struct chameleon_gdd __iomem *gdd =
  33. (struct chameleon_gdd __iomem *) base;
  34. struct mcb_device *mdev;
  35. u32 offset;
  36. u32 size;
  37. int ret;
  38. __le32 reg1;
  39. __le32 reg2;
  40. mdev = mcb_alloc_dev(bus);
  41. if (!mdev)
  42. return -ENOMEM;
  43. reg1 = readl(&gdd->reg1);
  44. reg2 = readl(&gdd->reg2);
  45. offset = readl(&gdd->offset);
  46. size = readl(&gdd->size);
  47. mdev->id = GDD_DEV(reg1);
  48. mdev->rev = GDD_REV(reg1);
  49. mdev->var = GDD_VAR(reg1);
  50. mdev->bar = GDD_BAR(reg2);
  51. mdev->group = GDD_GRP(reg2);
  52. mdev->inst = GDD_INS(reg2);
  53. pr_debug("Found a 16z%03d\n", mdev->id);
  54. mdev->irq.start = GDD_IRQ(reg1);
  55. mdev->irq.end = GDD_IRQ(reg1);
  56. mdev->irq.flags = IORESOURCE_IRQ;
  57. mdev->mem.start = mapbase + offset;
  58. mdev->mem.end = mdev->mem.start + size - 1;
  59. mdev->mem.flags = IORESOURCE_MEM;
  60. mdev->is_added = false;
  61. ret = mcb_device_register(bus, mdev);
  62. if (ret < 0)
  63. goto err;
  64. return 0;
  65. err:
  66. mcb_free_dev(mdev);
  67. return ret;
  68. }
  69. int chameleon_parse_cells(struct mcb_bus *bus, phys_addr_t mapbase,
  70. void __iomem *base)
  71. {
  72. char __iomem *p = base;
  73. struct chameleon_fpga_header *header;
  74. uint32_t dtype;
  75. int num_cells = 0;
  76. int ret = 0;
  77. u32 hsize;
  78. hsize = sizeof(struct chameleon_fpga_header);
  79. header = kzalloc(hsize, GFP_KERNEL);
  80. if (!header)
  81. return -ENOMEM;
  82. /* Extract header information */
  83. memcpy_fromio(header, p, hsize);
  84. /* We only support chameleon v2 at the moment */
  85. header->magic = le16_to_cpu(header->magic);
  86. if (header->magic != CHAMELEONV2_MAGIC) {
  87. pr_err("Unsupported chameleon version 0x%x\n",
  88. header->magic);
  89. kfree(header);
  90. return -ENODEV;
  91. }
  92. p += hsize;
  93. pr_debug("header->revision = %d\n", header->revision);
  94. pr_debug("header->model = 0x%x ('%c')\n", header->model,
  95. header->model);
  96. pr_debug("header->minor = %d\n", header->minor);
  97. pr_debug("header->bus_type = 0x%x\n", header->bus_type);
  98. pr_debug("header->magic = 0x%x\n", header->magic);
  99. pr_debug("header->filename = \"%.*s\"\n", CHAMELEON_FILENAME_LEN,
  100. header->filename);
  101. for_each_chameleon_cell(dtype, p) {
  102. switch (dtype) {
  103. case CHAMELEON_DTYPE_GENERAL:
  104. ret = chameleon_parse_gdd(bus, mapbase, p);
  105. if (ret < 0)
  106. goto out;
  107. p += sizeof(struct chameleon_gdd);
  108. break;
  109. case CHAMELEON_DTYPE_BRIDGE:
  110. chameleon_parse_bdd(bus, mapbase, p);
  111. p += sizeof(struct chameleon_bdd);
  112. break;
  113. case CHAMELEON_DTYPE_END:
  114. break;
  115. default:
  116. pr_err("Invalid chameleon descriptor type 0x%x\n",
  117. dtype);
  118. kfree(header);
  119. return -EINVAL;
  120. }
  121. num_cells++;
  122. }
  123. if (num_cells == 0)
  124. num_cells = -EINVAL;
  125. kfree(header);
  126. return num_cells;
  127. out:
  128. kfree(header);
  129. return ret;
  130. }
  131. EXPORT_SYMBOL_GPL(chameleon_parse_cells);