puv3_ost.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. * OSTimer device simulation in PKUnity SoC
  3. *
  4. * Copyright (C) 2010-2012 Guan Xuetao
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation, or any later version.
  9. * See the COPYING file in the top-level directory.
  10. */
  11. #include "qemu/osdep.h"
  12. #include "hw/sysbus.h"
  13. #include "hw/irq.h"
  14. #include "hw/ptimer.h"
  15. #include "qemu/module.h"
  16. #include "qemu/log.h"
  17. #undef DEBUG_PUV3
  18. #include "hw/unicore32/puv3.h"
  19. #define TYPE_PUV3_OST "puv3_ost"
  20. #define PUV3_OST(obj) OBJECT_CHECK(PUV3OSTState, (obj), TYPE_PUV3_OST)
  21. /* puv3 ostimer implementation. */
  22. typedef struct PUV3OSTState {
  23. SysBusDevice parent_obj;
  24. MemoryRegion iomem;
  25. qemu_irq irq;
  26. ptimer_state *ptimer;
  27. uint32_t reg_OSMR0;
  28. uint32_t reg_OSCR;
  29. uint32_t reg_OSSR;
  30. uint32_t reg_OIER;
  31. } PUV3OSTState;
  32. static uint64_t puv3_ost_read(void *opaque, hwaddr offset,
  33. unsigned size)
  34. {
  35. PUV3OSTState *s = opaque;
  36. uint32_t ret = 0;
  37. switch (offset) {
  38. case 0x10: /* Counter Register */
  39. ret = s->reg_OSMR0 - (uint32_t)ptimer_get_count(s->ptimer);
  40. break;
  41. case 0x14: /* Status Register */
  42. ret = s->reg_OSSR;
  43. break;
  44. case 0x1c: /* Interrupt Enable Register */
  45. ret = s->reg_OIER;
  46. break;
  47. default:
  48. qemu_log_mask(LOG_GUEST_ERROR,
  49. "%s: Bad read offset 0x%"HWADDR_PRIx"\n",
  50. __func__, offset);
  51. }
  52. DPRINTF("offset 0x%x, value 0x%x\n", offset, ret);
  53. return ret;
  54. }
  55. static void puv3_ost_write(void *opaque, hwaddr offset,
  56. uint64_t value, unsigned size)
  57. {
  58. PUV3OSTState *s = opaque;
  59. DPRINTF("offset 0x%x, value 0x%x\n", offset, value);
  60. switch (offset) {
  61. case 0x00: /* Match Register 0 */
  62. ptimer_transaction_begin(s->ptimer);
  63. s->reg_OSMR0 = value;
  64. if (s->reg_OSMR0 > s->reg_OSCR) {
  65. ptimer_set_count(s->ptimer, s->reg_OSMR0 - s->reg_OSCR);
  66. } else {
  67. ptimer_set_count(s->ptimer, s->reg_OSMR0 +
  68. (0xffffffff - s->reg_OSCR));
  69. }
  70. ptimer_run(s->ptimer, 2);
  71. ptimer_transaction_commit(s->ptimer);
  72. break;
  73. case 0x14: /* Status Register */
  74. assert(value == 0);
  75. if (s->reg_OSSR) {
  76. s->reg_OSSR = value;
  77. qemu_irq_lower(s->irq);
  78. }
  79. break;
  80. case 0x1c: /* Interrupt Enable Register */
  81. s->reg_OIER = value;
  82. break;
  83. default:
  84. qemu_log_mask(LOG_GUEST_ERROR,
  85. "%s: Bad write offset 0x%"HWADDR_PRIx"\n",
  86. __func__, offset);
  87. }
  88. }
  89. static const MemoryRegionOps puv3_ost_ops = {
  90. .read = puv3_ost_read,
  91. .write = puv3_ost_write,
  92. .impl = {
  93. .min_access_size = 4,
  94. .max_access_size = 4,
  95. },
  96. .endianness = DEVICE_NATIVE_ENDIAN,
  97. };
  98. static void puv3_ost_tick(void *opaque)
  99. {
  100. PUV3OSTState *s = opaque;
  101. DPRINTF("ost hit when ptimer counter from 0x%x to 0x%x!\n",
  102. s->reg_OSCR, s->reg_OSMR0);
  103. s->reg_OSCR = s->reg_OSMR0;
  104. if (s->reg_OIER) {
  105. s->reg_OSSR = 1;
  106. qemu_irq_raise(s->irq);
  107. }
  108. }
  109. static void puv3_ost_realize(DeviceState *dev, Error **errp)
  110. {
  111. PUV3OSTState *s = PUV3_OST(dev);
  112. SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
  113. s->reg_OIER = 0;
  114. s->reg_OSSR = 0;
  115. s->reg_OSMR0 = 0;
  116. s->reg_OSCR = 0;
  117. sysbus_init_irq(sbd, &s->irq);
  118. s->ptimer = ptimer_init(puv3_ost_tick, s, PTIMER_POLICY_DEFAULT);
  119. ptimer_transaction_begin(s->ptimer);
  120. ptimer_set_freq(s->ptimer, 50 * 1000 * 1000);
  121. ptimer_transaction_commit(s->ptimer);
  122. memory_region_init_io(&s->iomem, OBJECT(s), &puv3_ost_ops, s, "puv3_ost",
  123. PUV3_REGS_OFFSET);
  124. sysbus_init_mmio(sbd, &s->iomem);
  125. }
  126. static void puv3_ost_class_init(ObjectClass *klass, void *data)
  127. {
  128. DeviceClass *dc = DEVICE_CLASS(klass);
  129. dc->realize = puv3_ost_realize;
  130. }
  131. static const TypeInfo puv3_ost_info = {
  132. .name = TYPE_PUV3_OST,
  133. .parent = TYPE_SYS_BUS_DEVICE,
  134. .instance_size = sizeof(PUV3OSTState),
  135. .class_init = puv3_ost_class_init,
  136. };
  137. static void puv3_ost_register_type(void)
  138. {
  139. type_register_static(&puv3_ost_info);
  140. }
  141. type_init(puv3_ost_register_type)