grlib_ahb_apb_pnp.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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. val = extract32(val, (4 - (offset & 3) - size) * 8, size * 8);
  127. trace_grlib_ahb_pnp_read(offset, size, val);
  128. return val;
  129. }
  130. static void grlib_ahb_pnp_write(void *opaque, hwaddr addr,
  131. uint64_t val, unsigned size)
  132. {
  133. qemu_log_mask(LOG_UNIMP, "%s not implemented\n", __func__);
  134. }
  135. static const MemoryRegionOps grlib_ahb_pnp_ops = {
  136. .read = grlib_ahb_pnp_read,
  137. .write = grlib_ahb_pnp_write,
  138. .endianness = DEVICE_BIG_ENDIAN,
  139. .impl = {
  140. .min_access_size = 1,
  141. .max_access_size = 4,
  142. },
  143. };
  144. static void grlib_ahb_pnp_realize(DeviceState *dev, Error **errp)
  145. {
  146. AHBPnp *ahb_pnp = GRLIB_AHB_PNP(dev);
  147. SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
  148. memory_region_init_io(&ahb_pnp->iomem, OBJECT(dev), &grlib_ahb_pnp_ops,
  149. ahb_pnp, TYPE_GRLIB_AHB_PNP, GRLIB_PNP_MAX_REGS);
  150. sysbus_init_mmio(sbd, &ahb_pnp->iomem);
  151. }
  152. static void grlib_ahb_pnp_class_init(ObjectClass *klass, void *data)
  153. {
  154. DeviceClass *dc = DEVICE_CLASS(klass);
  155. dc->realize = grlib_ahb_pnp_realize;
  156. }
  157. static const TypeInfo grlib_ahb_pnp_info = {
  158. .name = TYPE_GRLIB_AHB_PNP,
  159. .parent = TYPE_SYS_BUS_DEVICE,
  160. .instance_size = sizeof(AHBPnp),
  161. .class_init = grlib_ahb_pnp_class_init,
  162. };
  163. /* APBPnp */
  164. typedef struct APBPnp {
  165. SysBusDevice parent_obj;
  166. MemoryRegion iomem;
  167. uint32_t regs[GRLIB_PNP_MAX_REGS >> 2];
  168. uint32_t entry_count;
  169. } APBPnp;
  170. void grlib_apb_pnp_add_entry(APBPnp *dev, uint32_t address, uint32_t mask,
  171. uint8_t vendor, uint16_t device, uint8_t version,
  172. uint8_t irq, int type)
  173. {
  174. unsigned int reg_start;
  175. /*
  176. * APB entries look like this:
  177. *
  178. * 31 -------- 23 -------- 11 ----- 9 ------- 4 --- 0
  179. * | VENDOR ID | DEVICE ID | IRQ ? | VERSION | IRQ |
  180. *
  181. * 31 ---------- 20 --- 15 ----------------- 3 ---- 0
  182. * | ADDR[20..8] | 0000 | MASK | TYPE |
  183. */
  184. assert(dev->entry_count < GRLIB_APB_MAX_DEV);
  185. reg_start = (dev->entry_count * GRLIB_APB_ENTRY_SIZE) >> 2;
  186. dev->entry_count++;
  187. dev->regs[reg_start] = deposit32(dev->regs[reg_start],
  188. GRLIB_PNP_VENDOR_SHIFT,
  189. GRLIB_PNP_VENDOR_SIZE,
  190. vendor);
  191. dev->regs[reg_start] = deposit32(dev->regs[reg_start],
  192. GRLIB_PNP_DEV_SHIFT,
  193. GRLIB_PNP_DEV_SIZE,
  194. device);
  195. dev->regs[reg_start] = deposit32(dev->regs[reg_start],
  196. GRLIB_PNP_VER_SHIFT,
  197. GRLIB_PNP_VER_SIZE,
  198. version);
  199. dev->regs[reg_start] = deposit32(dev->regs[reg_start],
  200. GRLIB_PNP_IRQ_SHIFT,
  201. GRLIB_PNP_IRQ_SIZE,
  202. irq);
  203. reg_start += 1;
  204. dev->regs[reg_start] = type;
  205. dev->regs[reg_start] = deposit32(dev->regs[reg_start],
  206. GRLIB_PNP_ADDR_SHIFT,
  207. GRLIB_PNP_ADDR_SIZE,
  208. extract32(address,
  209. GRLIB_APB_DEV_ADDR_SHIFT,
  210. GRLIB_APB_DEV_ADDR_SIZE));
  211. dev->regs[reg_start] = deposit32(dev->regs[reg_start],
  212. GRLIB_PNP_MASK_SHIFT,
  213. GRLIB_PNP_MASK_SIZE,
  214. mask);
  215. }
  216. static uint64_t grlib_apb_pnp_read(void *opaque, hwaddr offset, unsigned size)
  217. {
  218. APBPnp *apb_pnp = GRLIB_APB_PNP(opaque);
  219. uint32_t val;
  220. val = apb_pnp->regs[offset >> 2];
  221. val = extract32(val, (4 - (offset & 3) - size) * 8, size * 8);
  222. trace_grlib_apb_pnp_read(offset, size, val);
  223. return val;
  224. }
  225. static void grlib_apb_pnp_write(void *opaque, hwaddr addr,
  226. uint64_t val, unsigned size)
  227. {
  228. qemu_log_mask(LOG_UNIMP, "%s not implemented\n", __func__);
  229. }
  230. static const MemoryRegionOps grlib_apb_pnp_ops = {
  231. .read = grlib_apb_pnp_read,
  232. .write = grlib_apb_pnp_write,
  233. .endianness = DEVICE_BIG_ENDIAN,
  234. .impl = {
  235. .min_access_size = 1,
  236. .max_access_size = 4,
  237. },
  238. };
  239. static void grlib_apb_pnp_realize(DeviceState *dev, Error **errp)
  240. {
  241. APBPnp *apb_pnp = GRLIB_APB_PNP(dev);
  242. SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
  243. memory_region_init_io(&apb_pnp->iomem, OBJECT(dev), &grlib_apb_pnp_ops,
  244. apb_pnp, TYPE_GRLIB_APB_PNP, GRLIB_PNP_MAX_REGS);
  245. sysbus_init_mmio(sbd, &apb_pnp->iomem);
  246. }
  247. static void grlib_apb_pnp_class_init(ObjectClass *klass, void *data)
  248. {
  249. DeviceClass *dc = DEVICE_CLASS(klass);
  250. dc->realize = grlib_apb_pnp_realize;
  251. }
  252. static const TypeInfo grlib_apb_pnp_info = {
  253. .name = TYPE_GRLIB_APB_PNP,
  254. .parent = TYPE_SYS_BUS_DEVICE,
  255. .instance_size = sizeof(APBPnp),
  256. .class_init = grlib_apb_pnp_class_init,
  257. };
  258. static void grlib_ahb_apb_pnp_register_types(void)
  259. {
  260. type_register_static(&grlib_ahb_pnp_info);
  261. type_register_static(&grlib_apb_pnp_info);
  262. }
  263. type_init(grlib_ahb_apb_pnp_register_types)