grlib_ahb_apb_pnp.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. #include "trace.h"
  28. #define GRLIB_PNP_VENDOR_SHIFT (24)
  29. #define GRLIB_PNP_VENDOR_SIZE (8)
  30. #define GRLIB_PNP_DEV_SHIFT (12)
  31. #define GRLIB_PNP_DEV_SIZE (12)
  32. #define GRLIB_PNP_VER_SHIFT (5)
  33. #define GRLIB_PNP_VER_SIZE (5)
  34. #define GRLIB_PNP_IRQ_SHIFT (0)
  35. #define GRLIB_PNP_IRQ_SIZE (5)
  36. #define GRLIB_PNP_ADDR_SHIFT (20)
  37. #define GRLIB_PNP_ADDR_SIZE (12)
  38. #define GRLIB_PNP_MASK_SHIFT (4)
  39. #define GRLIB_PNP_MASK_SIZE (12)
  40. #define GRLIB_AHB_DEV_ADDR_SHIFT (20)
  41. #define GRLIB_AHB_DEV_ADDR_SIZE (12)
  42. #define GRLIB_AHB_ENTRY_SIZE (0x20)
  43. #define GRLIB_AHB_MAX_DEV (64)
  44. #define GRLIB_AHB_SLAVE_OFFSET (0x800)
  45. #define GRLIB_APB_DEV_ADDR_SHIFT (8)
  46. #define GRLIB_APB_DEV_ADDR_SIZE (12)
  47. #define GRLIB_APB_ENTRY_SIZE (0x08)
  48. #define GRLIB_APB_MAX_DEV (512)
  49. #define GRLIB_PNP_MAX_REGS (0x1000)
  50. typedef struct AHBPnp {
  51. SysBusDevice parent_obj;
  52. MemoryRegion iomem;
  53. uint32_t regs[GRLIB_PNP_MAX_REGS >> 2];
  54. uint8_t master_count;
  55. uint8_t slave_count;
  56. } AHBPnp;
  57. void grlib_ahb_pnp_add_entry(AHBPnp *dev, uint32_t address, uint32_t mask,
  58. uint8_t vendor, uint16_t device, int slave,
  59. int type)
  60. {
  61. unsigned int reg_start;
  62. /*
  63. * AHB entries look like this:
  64. *
  65. * 31 -------- 23 -------- 11 ----- 9 -------- 4 --- 0
  66. * | VENDOR ID | DEVICE ID | IRQ ? | VERSION | IRQ |
  67. * --------------------------------------------------
  68. * | USER |
  69. * --------------------------------------------------
  70. * | USER |
  71. * --------------------------------------------------
  72. * | USER |
  73. * --------------------------------------------------
  74. * | USER |
  75. * --------------------------------------------------
  76. * 31 ----------- 20 --- 15 ----------------- 3 ---- 0
  77. * | ADDR[31..12] | 00PC | MASK | TYPE |
  78. * --------------------------------------------------
  79. * 31 ----------- 20 --- 15 ----------------- 3 ---- 0
  80. * | ADDR[31..12] | 00PC | MASK | TYPE |
  81. * --------------------------------------------------
  82. * 31 ----------- 20 --- 15 ----------------- 3 ---- 0
  83. * | ADDR[31..12] | 00PC | MASK | TYPE |
  84. * --------------------------------------------------
  85. * 31 ----------- 20 --- 15 ----------------- 3 ---- 0
  86. * | ADDR[31..12] | 00PC | MASK | TYPE |
  87. * --------------------------------------------------
  88. */
  89. if (slave) {
  90. assert(dev->slave_count < GRLIB_AHB_MAX_DEV);
  91. reg_start = (GRLIB_AHB_SLAVE_OFFSET
  92. + (dev->slave_count * GRLIB_AHB_ENTRY_SIZE)) >> 2;
  93. dev->slave_count++;
  94. } else {
  95. assert(dev->master_count < GRLIB_AHB_MAX_DEV);
  96. reg_start = (dev->master_count * GRLIB_AHB_ENTRY_SIZE) >> 2;
  97. dev->master_count++;
  98. }
  99. dev->regs[reg_start] = deposit32(dev->regs[reg_start],
  100. GRLIB_PNP_VENDOR_SHIFT,
  101. GRLIB_PNP_VENDOR_SIZE,
  102. vendor);
  103. dev->regs[reg_start] = deposit32(dev->regs[reg_start],
  104. GRLIB_PNP_DEV_SHIFT,
  105. GRLIB_PNP_DEV_SIZE,
  106. device);
  107. reg_start += 4;
  108. /* AHB Memory Space */
  109. dev->regs[reg_start] = type;
  110. dev->regs[reg_start] = deposit32(dev->regs[reg_start],
  111. GRLIB_PNP_ADDR_SHIFT,
  112. GRLIB_PNP_ADDR_SIZE,
  113. extract32(address,
  114. GRLIB_AHB_DEV_ADDR_SHIFT,
  115. GRLIB_AHB_DEV_ADDR_SIZE));
  116. dev->regs[reg_start] = deposit32(dev->regs[reg_start],
  117. GRLIB_PNP_MASK_SHIFT,
  118. GRLIB_PNP_MASK_SIZE,
  119. mask);
  120. }
  121. static uint64_t grlib_ahb_pnp_read(void *opaque, hwaddr offset, unsigned size)
  122. {
  123. AHBPnp *ahb_pnp = GRLIB_AHB_PNP(opaque);
  124. uint32_t val;
  125. val = ahb_pnp->regs[offset >> 2];
  126. trace_grlib_ahb_pnp_read(offset, val);
  127. return val;
  128. }
  129. static void grlib_ahb_pnp_write(void *opaque, hwaddr addr,
  130. uint64_t val, unsigned size)
  131. {
  132. qemu_log_mask(LOG_UNIMP, "%s not implemented\n", __func__);
  133. }
  134. static const MemoryRegionOps grlib_ahb_pnp_ops = {
  135. .read = grlib_ahb_pnp_read,
  136. .write = grlib_ahb_pnp_write,
  137. .endianness = DEVICE_BIG_ENDIAN,
  138. .impl = {
  139. .min_access_size = 4,
  140. .max_access_size = 4,
  141. },
  142. };
  143. static void grlib_ahb_pnp_realize(DeviceState *dev, Error **errp)
  144. {
  145. AHBPnp *ahb_pnp = GRLIB_AHB_PNP(dev);
  146. SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
  147. memory_region_init_io(&ahb_pnp->iomem, OBJECT(dev), &grlib_ahb_pnp_ops,
  148. ahb_pnp, TYPE_GRLIB_AHB_PNP, GRLIB_PNP_MAX_REGS);
  149. sysbus_init_mmio(sbd, &ahb_pnp->iomem);
  150. }
  151. static void grlib_ahb_pnp_class_init(ObjectClass *klass, void *data)
  152. {
  153. DeviceClass *dc = DEVICE_CLASS(klass);
  154. dc->realize = grlib_ahb_pnp_realize;
  155. }
  156. static const TypeInfo grlib_ahb_pnp_info = {
  157. .name = TYPE_GRLIB_AHB_PNP,
  158. .parent = TYPE_SYS_BUS_DEVICE,
  159. .instance_size = sizeof(AHBPnp),
  160. .class_init = grlib_ahb_pnp_class_init,
  161. };
  162. /* APBPnp */
  163. typedef struct APBPnp {
  164. SysBusDevice parent_obj;
  165. MemoryRegion iomem;
  166. uint32_t regs[GRLIB_PNP_MAX_REGS >> 2];
  167. uint32_t entry_count;
  168. } APBPnp;
  169. void grlib_apb_pnp_add_entry(APBPnp *dev, uint32_t address, uint32_t mask,
  170. uint8_t vendor, uint16_t device, uint8_t version,
  171. uint8_t irq, int type)
  172. {
  173. unsigned int reg_start;
  174. /*
  175. * APB entries look like this:
  176. *
  177. * 31 -------- 23 -------- 11 ----- 9 ------- 4 --- 0
  178. * | VENDOR ID | DEVICE ID | IRQ ? | VERSION | IRQ |
  179. *
  180. * 31 ---------- 20 --- 15 ----------------- 3 ---- 0
  181. * | ADDR[20..8] | 0000 | MASK | TYPE |
  182. */
  183. assert(dev->entry_count < GRLIB_APB_MAX_DEV);
  184. reg_start = (dev->entry_count * GRLIB_APB_ENTRY_SIZE) >> 2;
  185. dev->entry_count++;
  186. dev->regs[reg_start] = deposit32(dev->regs[reg_start],
  187. GRLIB_PNP_VENDOR_SHIFT,
  188. GRLIB_PNP_VENDOR_SIZE,
  189. vendor);
  190. dev->regs[reg_start] = deposit32(dev->regs[reg_start],
  191. GRLIB_PNP_DEV_SHIFT,
  192. GRLIB_PNP_DEV_SIZE,
  193. device);
  194. dev->regs[reg_start] = deposit32(dev->regs[reg_start],
  195. GRLIB_PNP_VER_SHIFT,
  196. GRLIB_PNP_VER_SIZE,
  197. version);
  198. dev->regs[reg_start] = deposit32(dev->regs[reg_start],
  199. GRLIB_PNP_IRQ_SHIFT,
  200. GRLIB_PNP_IRQ_SIZE,
  201. irq);
  202. reg_start += 1;
  203. dev->regs[reg_start] = type;
  204. dev->regs[reg_start] = deposit32(dev->regs[reg_start],
  205. GRLIB_PNP_ADDR_SHIFT,
  206. GRLIB_PNP_ADDR_SIZE,
  207. extract32(address,
  208. GRLIB_APB_DEV_ADDR_SHIFT,
  209. GRLIB_APB_DEV_ADDR_SIZE));
  210. dev->regs[reg_start] = deposit32(dev->regs[reg_start],
  211. GRLIB_PNP_MASK_SHIFT,
  212. GRLIB_PNP_MASK_SIZE,
  213. mask);
  214. }
  215. static uint64_t grlib_apb_pnp_read(void *opaque, hwaddr offset, unsigned size)
  216. {
  217. APBPnp *apb_pnp = GRLIB_APB_PNP(opaque);
  218. uint32_t val;
  219. val = apb_pnp->regs[offset >> 2];
  220. trace_grlib_apb_pnp_read(offset, val);
  221. return val;
  222. }
  223. static void grlib_apb_pnp_write(void *opaque, hwaddr addr,
  224. uint64_t val, unsigned size)
  225. {
  226. qemu_log_mask(LOG_UNIMP, "%s not implemented\n", __func__);
  227. }
  228. static const MemoryRegionOps grlib_apb_pnp_ops = {
  229. .read = grlib_apb_pnp_read,
  230. .write = grlib_apb_pnp_write,
  231. .endianness = DEVICE_BIG_ENDIAN,
  232. .impl = {
  233. .min_access_size = 4,
  234. .max_access_size = 4,
  235. },
  236. };
  237. static void grlib_apb_pnp_realize(DeviceState *dev, Error **errp)
  238. {
  239. APBPnp *apb_pnp = GRLIB_APB_PNP(dev);
  240. SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
  241. memory_region_init_io(&apb_pnp->iomem, OBJECT(dev), &grlib_apb_pnp_ops,
  242. apb_pnp, TYPE_GRLIB_APB_PNP, GRLIB_PNP_MAX_REGS);
  243. sysbus_init_mmio(sbd, &apb_pnp->iomem);
  244. }
  245. static void grlib_apb_pnp_class_init(ObjectClass *klass, void *data)
  246. {
  247. DeviceClass *dc = DEVICE_CLASS(klass);
  248. dc->realize = grlib_apb_pnp_realize;
  249. }
  250. static const TypeInfo grlib_apb_pnp_info = {
  251. .name = TYPE_GRLIB_APB_PNP,
  252. .parent = TYPE_SYS_BUS_DEVICE,
  253. .instance_size = sizeof(APBPnp),
  254. .class_init = grlib_apb_pnp_class_init,
  255. };
  256. static void grlib_ahb_apb_pnp_register_types(void)
  257. {
  258. type_register_static(&grlib_ahb_pnp_info);
  259. type_register_static(&grlib_apb_pnp_info);
  260. }
  261. type_init(grlib_ahb_apb_pnp_register_types)