sifive_e_prci.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * QEMU SiFive E PRCI (Power, Reset, Clock, Interrupt)
  3. *
  4. * Copyright (c) 2017 SiFive, Inc.
  5. *
  6. * Simple model of the PRCI to emulate register reads made by the SDK BSP
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms and conditions of the GNU General Public License,
  10. * version 2 or later, as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope it will be useful, but WITHOUT
  13. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  15. * more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along with
  18. * this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #include "qemu/osdep.h"
  21. #include "hw/sysbus.h"
  22. #include "qemu/log.h"
  23. #include "qemu/module.h"
  24. #include "hw/hw.h"
  25. #include "hw/riscv/sifive_e_prci.h"
  26. static uint64_t sifive_e_prci_read(void *opaque, hwaddr addr, unsigned int size)
  27. {
  28. SiFiveEPRCIState *s = opaque;
  29. switch (addr) {
  30. case SIFIVE_E_PRCI_HFROSCCFG:
  31. return s->hfrosccfg;
  32. case SIFIVE_E_PRCI_HFXOSCCFG:
  33. return s->hfxosccfg;
  34. case SIFIVE_E_PRCI_PLLCFG:
  35. return s->pllcfg;
  36. case SIFIVE_E_PRCI_PLLOUTDIV:
  37. return s->plloutdiv;
  38. }
  39. qemu_log_mask(LOG_GUEST_ERROR, "%s: read: addr=0x%x\n",
  40. __func__, (int)addr);
  41. return 0;
  42. }
  43. static void sifive_e_prci_write(void *opaque, hwaddr addr,
  44. uint64_t val64, unsigned int size)
  45. {
  46. SiFiveEPRCIState *s = opaque;
  47. switch (addr) {
  48. case SIFIVE_E_PRCI_HFROSCCFG:
  49. s->hfrosccfg = (uint32_t) val64;
  50. /* OSC stays ready */
  51. s->hfrosccfg |= SIFIVE_E_PRCI_HFROSCCFG_RDY;
  52. break;
  53. case SIFIVE_E_PRCI_HFXOSCCFG:
  54. s->hfxosccfg = (uint32_t) val64;
  55. /* OSC stays ready */
  56. s->hfxosccfg |= SIFIVE_E_PRCI_HFXOSCCFG_RDY;
  57. break;
  58. case SIFIVE_E_PRCI_PLLCFG:
  59. s->pllcfg = (uint32_t) val64;
  60. /* PLL stays locked */
  61. s->pllcfg |= SIFIVE_E_PRCI_PLLCFG_LOCK;
  62. break;
  63. case SIFIVE_E_PRCI_PLLOUTDIV:
  64. s->plloutdiv = (uint32_t) val64;
  65. break;
  66. default:
  67. qemu_log_mask(LOG_GUEST_ERROR, "%s: bad write: addr=0x%x v=0x%x\n",
  68. __func__, (int)addr, (int)val64);
  69. }
  70. }
  71. static const MemoryRegionOps sifive_e_prci_ops = {
  72. .read = sifive_e_prci_read,
  73. .write = sifive_e_prci_write,
  74. .endianness = DEVICE_NATIVE_ENDIAN,
  75. .valid = {
  76. .min_access_size = 4,
  77. .max_access_size = 4
  78. }
  79. };
  80. static void sifive_e_prci_init(Object *obj)
  81. {
  82. SiFiveEPRCIState *s = SIFIVE_E_PRCI(obj);
  83. memory_region_init_io(&s->mmio, obj, &sifive_e_prci_ops, s,
  84. TYPE_SIFIVE_E_PRCI, SIFIVE_E_PRCI_REG_SIZE);
  85. sysbus_init_mmio(SYS_BUS_DEVICE(obj), &s->mmio);
  86. s->hfrosccfg = (SIFIVE_E_PRCI_HFROSCCFG_RDY | SIFIVE_E_PRCI_HFROSCCFG_EN);
  87. s->hfxosccfg = (SIFIVE_E_PRCI_HFXOSCCFG_RDY | SIFIVE_E_PRCI_HFXOSCCFG_EN);
  88. s->pllcfg = (SIFIVE_E_PRCI_PLLCFG_REFSEL | SIFIVE_E_PRCI_PLLCFG_BYPASS |
  89. SIFIVE_E_PRCI_PLLCFG_LOCK);
  90. s->plloutdiv = SIFIVE_E_PRCI_PLLOUTDIV_DIV1;
  91. }
  92. static const TypeInfo sifive_e_prci_info = {
  93. .name = TYPE_SIFIVE_E_PRCI,
  94. .parent = TYPE_SYS_BUS_DEVICE,
  95. .instance_size = sizeof(SiFiveEPRCIState),
  96. .instance_init = sifive_e_prci_init,
  97. };
  98. static void sifive_e_prci_register_types(void)
  99. {
  100. type_register_static(&sifive_e_prci_info);
  101. }
  102. type_init(sifive_e_prci_register_types)
  103. /*
  104. * Create PRCI device.
  105. */
  106. DeviceState *sifive_e_prci_create(hwaddr addr)
  107. {
  108. DeviceState *dev = qdev_create(NULL, TYPE_SIFIVE_E_PRCI);
  109. qdev_init_nofail(dev);
  110. sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0, addr);
  111. return dev;
  112. }