grlib_ahb_apb_pnp.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /*
  2. * GRLIB AHB APB PNP
  3. *
  4. * Copyright (C) 2019 AdaCore
  5. *
  6. * Developed by :
  7. * Frederic Konrad <frederic.konrad@adacore.com>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation, either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License along
  20. * with this program; if not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. #include "qemu/osdep.h"
  24. #include "qemu/log.h"
  25. #include "hw/sysbus.h"
  26. #include "hw/misc/grlib_ahb_apb_pnp.h"
  27. #define GRLIB_PNP_VENDOR_SHIFT (24)
  28. #define GRLIB_PNP_VENDOR_SIZE (8)
  29. #define GRLIB_PNP_DEV_SHIFT (12)
  30. #define GRLIB_PNP_DEV_SIZE (12)
  31. #define GRLIB_PNP_VER_SHIFT (5)
  32. #define GRLIB_PNP_VER_SIZE (5)
  33. #define GRLIB_PNP_IRQ_SHIFT (0)
  34. #define GRLIB_PNP_IRQ_SIZE (5)
  35. #define GRLIB_PNP_ADDR_SHIFT (20)
  36. #define GRLIB_PNP_ADDR_SIZE (12)
  37. #define GRLIB_PNP_MASK_SHIFT (4)
  38. #define GRLIB_PNP_MASK_SIZE (12)
  39. #define GRLIB_AHB_DEV_ADDR_SHIFT (20)
  40. #define GRLIB_AHB_DEV_ADDR_SIZE (12)
  41. #define GRLIB_AHB_ENTRY_SIZE (0x20)
  42. #define GRLIB_AHB_MAX_DEV (64)
  43. #define GRLIB_AHB_SLAVE_OFFSET (0x800)
  44. #define GRLIB_APB_DEV_ADDR_SHIFT (8)
  45. #define GRLIB_APB_DEV_ADDR_SIZE (12)
  46. #define GRLIB_APB_ENTRY_SIZE (0x08)
  47. #define GRLIB_APB_MAX_DEV (512)
  48. #define GRLIB_PNP_MAX_REGS (0x1000)
  49. typedef struct AHBPnp {
  50. SysBusDevice parent_obj;
  51. MemoryRegion iomem;
  52. uint32_t regs[GRLIB_PNP_MAX_REGS >> 2];
  53. uint8_t master_count;
  54. uint8_t slave_count;
  55. } AHBPnp;
  56. void grlib_ahb_pnp_add_entry(AHBPnp *dev, uint32_t address, uint32_t mask,
  57. uint8_t vendor, uint16_t device, int slave,
  58. int type)
  59. {
  60. unsigned int reg_start;
  61. /*
  62. * AHB entries look like this:
  63. *
  64. * 31 -------- 23 -------- 11 ----- 9 -------- 4 --- 0
  65. * | VENDOR ID | DEVICE ID | IRQ ? | VERSION | IRQ |
  66. * --------------------------------------------------
  67. * | USER |
  68. * --------------------------------------------------
  69. * | USER |
  70. * --------------------------------------------------
  71. * | USER |
  72. * --------------------------------------------------
  73. * | USER |
  74. * --------------------------------------------------
  75. * 31 ----------- 20 --- 15 ----------------- 3 ---- 0
  76. * | ADDR[31..12] | 00PC | MASK | TYPE |
  77. * --------------------------------------------------
  78. * 31 ----------- 20 --- 15 ----------------- 3 ---- 0
  79. * | ADDR[31..12] | 00PC | MASK | TYPE |
  80. * --------------------------------------------------
  81. * 31 ----------- 20 --- 15 ----------------- 3 ---- 0
  82. * | ADDR[31..12] | 00PC | MASK | TYPE |
  83. * --------------------------------------------------
  84. * 31 ----------- 20 --- 15 ----------------- 3 ---- 0
  85. * | ADDR[31..12] | 00PC | MASK | TYPE |
  86. * --------------------------------------------------
  87. */
  88. if (slave) {
  89. assert(dev->slave_count < GRLIB_AHB_MAX_DEV);
  90. reg_start = (GRLIB_AHB_SLAVE_OFFSET
  91. + (dev->slave_count * GRLIB_AHB_ENTRY_SIZE)) >> 2;
  92. dev->slave_count++;
  93. } else {
  94. assert(dev->master_count < GRLIB_AHB_MAX_DEV);
  95. reg_start = (dev->master_count * GRLIB_AHB_ENTRY_SIZE) >> 2;
  96. dev->master_count++;
  97. }
  98. dev->regs[reg_start] = deposit32(dev->regs[reg_start],
  99. GRLIB_PNP_VENDOR_SHIFT,
  100. GRLIB_PNP_VENDOR_SIZE,
  101. vendor);
  102. dev->regs[reg_start] = deposit32(dev->regs[reg_start],
  103. GRLIB_PNP_DEV_SHIFT,
  104. GRLIB_PNP_DEV_SIZE,
  105. device);
  106. reg_start += 4;
  107. /* AHB Memory Space */
  108. dev->regs[reg_start] = type;
  109. dev->regs[reg_start] = deposit32(dev->regs[reg_start],
  110. GRLIB_PNP_ADDR_SHIFT,
  111. GRLIB_PNP_ADDR_SIZE,
  112. extract32(address,
  113. GRLIB_AHB_DEV_ADDR_SHIFT,
  114. GRLIB_AHB_DEV_ADDR_SIZE));
  115. dev->regs[reg_start] = deposit32(dev->regs[reg_start],
  116. GRLIB_PNP_MASK_SHIFT,
  117. GRLIB_PNP_MASK_SIZE,
  118. mask);
  119. }
  120. static uint64_t grlib_ahb_pnp_read(void *opaque, hwaddr offset, unsigned size)
  121. {
  122. AHBPnp *ahb_pnp = GRLIB_AHB_PNP(opaque);
  123. return ahb_pnp->regs[offset >> 2];
  124. }
  125. static const MemoryRegionOps grlib_ahb_pnp_ops = {
  126. .read = grlib_ahb_pnp_read,
  127. .endianness = DEVICE_BIG_ENDIAN,
  128. };
  129. static void grlib_ahb_pnp_realize(DeviceState *dev, Error **errp)
  130. {
  131. AHBPnp *ahb_pnp = GRLIB_AHB_PNP(dev);
  132. SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
  133. memory_region_init_io(&ahb_pnp->iomem, OBJECT(dev), &grlib_ahb_pnp_ops,
  134. ahb_pnp, TYPE_GRLIB_AHB_PNP, GRLIB_PNP_MAX_REGS);
  135. sysbus_init_mmio(sbd, &ahb_pnp->iomem);
  136. }
  137. static void grlib_ahb_pnp_class_init(ObjectClass *klass, void *data)
  138. {
  139. DeviceClass *dc = DEVICE_CLASS(klass);
  140. dc->realize = grlib_ahb_pnp_realize;
  141. }
  142. static const TypeInfo grlib_ahb_pnp_info = {
  143. .name = TYPE_GRLIB_AHB_PNP,
  144. .parent = TYPE_SYS_BUS_DEVICE,
  145. .instance_size = sizeof(AHBPnp),
  146. .class_init = grlib_ahb_pnp_class_init,
  147. };
  148. /* APBPnp */
  149. typedef struct APBPnp {
  150. SysBusDevice parent_obj;
  151. MemoryRegion iomem;
  152. uint32_t regs[GRLIB_PNP_MAX_REGS >> 2];
  153. uint32_t entry_count;
  154. } APBPnp;
  155. void grlib_apb_pnp_add_entry(APBPnp *dev, uint32_t address, uint32_t mask,
  156. uint8_t vendor, uint16_t device, uint8_t version,
  157. uint8_t irq, int type)
  158. {
  159. unsigned int reg_start;
  160. /*
  161. * APB entries look like this:
  162. *
  163. * 31 -------- 23 -------- 11 ----- 9 ------- 4 --- 0
  164. * | VENDOR ID | DEVICE ID | IRQ ? | VERSION | IRQ |
  165. *
  166. * 31 ---------- 20 --- 15 ----------------- 3 ---- 0
  167. * | ADDR[20..8] | 0000 | MASK | TYPE |
  168. */
  169. assert(dev->entry_count < GRLIB_APB_MAX_DEV);
  170. reg_start = (dev->entry_count * GRLIB_APB_ENTRY_SIZE) >> 2;
  171. dev->entry_count++;
  172. dev->regs[reg_start] = deposit32(dev->regs[reg_start],
  173. GRLIB_PNP_VENDOR_SHIFT,
  174. GRLIB_PNP_VENDOR_SIZE,
  175. vendor);
  176. dev->regs[reg_start] = deposit32(dev->regs[reg_start],
  177. GRLIB_PNP_DEV_SHIFT,
  178. GRLIB_PNP_DEV_SIZE,
  179. device);
  180. dev->regs[reg_start] = deposit32(dev->regs[reg_start],
  181. GRLIB_PNP_VER_SHIFT,
  182. GRLIB_PNP_VER_SIZE,
  183. version);
  184. dev->regs[reg_start] = deposit32(dev->regs[reg_start],
  185. GRLIB_PNP_IRQ_SHIFT,
  186. GRLIB_PNP_IRQ_SIZE,
  187. irq);
  188. reg_start += 1;
  189. dev->regs[reg_start] = type;
  190. dev->regs[reg_start] = deposit32(dev->regs[reg_start],
  191. GRLIB_PNP_ADDR_SHIFT,
  192. GRLIB_PNP_ADDR_SIZE,
  193. extract32(address,
  194. GRLIB_APB_DEV_ADDR_SHIFT,
  195. GRLIB_APB_DEV_ADDR_SIZE));
  196. dev->regs[reg_start] = deposit32(dev->regs[reg_start],
  197. GRLIB_PNP_MASK_SHIFT,
  198. GRLIB_PNP_MASK_SIZE,
  199. mask);
  200. }
  201. static uint64_t grlib_apb_pnp_read(void *opaque, hwaddr offset, unsigned size)
  202. {
  203. APBPnp *apb_pnp = GRLIB_APB_PNP(opaque);
  204. return apb_pnp->regs[offset >> 2];
  205. }
  206. static void grlib_apb_pnp_write(void *opaque, hwaddr addr,
  207. uint64_t val, unsigned size)
  208. {
  209. qemu_log_mask(LOG_UNIMP, "%s not implemented\n", __func__);
  210. }
  211. static const MemoryRegionOps grlib_apb_pnp_ops = {
  212. .read = grlib_apb_pnp_read,
  213. .write = grlib_apb_pnp_write,
  214. .endianness = DEVICE_BIG_ENDIAN,
  215. .impl = {
  216. .min_access_size = 4,
  217. .max_access_size = 4,
  218. },
  219. };
  220. static void grlib_apb_pnp_realize(DeviceState *dev, Error **errp)
  221. {
  222. APBPnp *apb_pnp = GRLIB_APB_PNP(dev);
  223. SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
  224. memory_region_init_io(&apb_pnp->iomem, OBJECT(dev), &grlib_apb_pnp_ops,
  225. apb_pnp, TYPE_GRLIB_APB_PNP, GRLIB_PNP_MAX_REGS);
  226. sysbus_init_mmio(sbd, &apb_pnp->iomem);
  227. }
  228. static void grlib_apb_pnp_class_init(ObjectClass *klass, void *data)
  229. {
  230. DeviceClass *dc = DEVICE_CLASS(klass);
  231. dc->realize = grlib_apb_pnp_realize;
  232. }
  233. static const TypeInfo grlib_apb_pnp_info = {
  234. .name = TYPE_GRLIB_APB_PNP,
  235. .parent = TYPE_SYS_BUS_DEVICE,
  236. .instance_size = sizeof(APBPnp),
  237. .class_init = grlib_apb_pnp_class_init,
  238. };
  239. static void grlib_ahb_apb_pnp_register_types(void)
  240. {
  241. type_register_static(&grlib_ahb_pnp_info);
  242. type_register_static(&grlib_apb_pnp_info);
  243. }
  244. type_init(grlib_ahb_apb_pnp_register_types)