puv3_ost.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. #undef DEBUG_PUV3
  17. #include "hw/unicore32/puv3.h"
  18. #define TYPE_PUV3_OST "puv3_ost"
  19. #define PUV3_OST(obj) OBJECT_CHECK(PUV3OSTState, (obj), TYPE_PUV3_OST)
  20. /* puv3 ostimer implementation. */
  21. typedef struct PUV3OSTState {
  22. SysBusDevice parent_obj;
  23. MemoryRegion iomem;
  24. qemu_irq irq;
  25. ptimer_state *ptimer;
  26. uint32_t reg_OSMR0;
  27. uint32_t reg_OSCR;
  28. uint32_t reg_OSSR;
  29. uint32_t reg_OIER;
  30. } PUV3OSTState;
  31. static uint64_t puv3_ost_read(void *opaque, hwaddr offset,
  32. unsigned size)
  33. {
  34. PUV3OSTState *s = opaque;
  35. uint32_t ret = 0;
  36. switch (offset) {
  37. case 0x10: /* Counter Register */
  38. ret = s->reg_OSMR0 - (uint32_t)ptimer_get_count(s->ptimer);
  39. break;
  40. case 0x14: /* Status Register */
  41. ret = s->reg_OSSR;
  42. break;
  43. case 0x1c: /* Interrupt Enable Register */
  44. ret = s->reg_OIER;
  45. break;
  46. default:
  47. DPRINTF("Bad offset %x\n", (int)offset);
  48. }
  49. DPRINTF("offset 0x%x, value 0x%x\n", offset, ret);
  50. return ret;
  51. }
  52. static void puv3_ost_write(void *opaque, hwaddr offset,
  53. uint64_t value, unsigned size)
  54. {
  55. PUV3OSTState *s = opaque;
  56. DPRINTF("offset 0x%x, value 0x%x\n", offset, value);
  57. switch (offset) {
  58. case 0x00: /* Match Register 0 */
  59. ptimer_transaction_begin(s->ptimer);
  60. s->reg_OSMR0 = value;
  61. if (s->reg_OSMR0 > s->reg_OSCR) {
  62. ptimer_set_count(s->ptimer, s->reg_OSMR0 - s->reg_OSCR);
  63. } else {
  64. ptimer_set_count(s->ptimer, s->reg_OSMR0 +
  65. (0xffffffff - s->reg_OSCR));
  66. }
  67. ptimer_run(s->ptimer, 2);
  68. ptimer_transaction_commit(s->ptimer);
  69. break;
  70. case 0x14: /* Status Register */
  71. assert(value == 0);
  72. if (s->reg_OSSR) {
  73. s->reg_OSSR = value;
  74. qemu_irq_lower(s->irq);
  75. }
  76. break;
  77. case 0x1c: /* Interrupt Enable Register */
  78. s->reg_OIER = value;
  79. break;
  80. default:
  81. DPRINTF("Bad offset %x\n", (int)offset);
  82. }
  83. }
  84. static const MemoryRegionOps puv3_ost_ops = {
  85. .read = puv3_ost_read,
  86. .write = puv3_ost_write,
  87. .impl = {
  88. .min_access_size = 4,
  89. .max_access_size = 4,
  90. },
  91. .endianness = DEVICE_NATIVE_ENDIAN,
  92. };
  93. static void puv3_ost_tick(void *opaque)
  94. {
  95. PUV3OSTState *s = opaque;
  96. DPRINTF("ost hit when ptimer counter from 0x%x to 0x%x!\n",
  97. s->reg_OSCR, s->reg_OSMR0);
  98. s->reg_OSCR = s->reg_OSMR0;
  99. if (s->reg_OIER) {
  100. s->reg_OSSR = 1;
  101. qemu_irq_raise(s->irq);
  102. }
  103. }
  104. static void puv3_ost_realize(DeviceState *dev, Error **errp)
  105. {
  106. PUV3OSTState *s = PUV3_OST(dev);
  107. SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
  108. s->reg_OIER = 0;
  109. s->reg_OSSR = 0;
  110. s->reg_OSMR0 = 0;
  111. s->reg_OSCR = 0;
  112. sysbus_init_irq(sbd, &s->irq);
  113. s->ptimer = ptimer_init(puv3_ost_tick, s, PTIMER_POLICY_DEFAULT);
  114. ptimer_transaction_begin(s->ptimer);
  115. ptimer_set_freq(s->ptimer, 50 * 1000 * 1000);
  116. ptimer_transaction_commit(s->ptimer);
  117. memory_region_init_io(&s->iomem, OBJECT(s), &puv3_ost_ops, s, "puv3_ost",
  118. PUV3_REGS_OFFSET);
  119. sysbus_init_mmio(sbd, &s->iomem);
  120. }
  121. static void puv3_ost_class_init(ObjectClass *klass, void *data)
  122. {
  123. DeviceClass *dc = DEVICE_CLASS(klass);
  124. dc->realize = puv3_ost_realize;
  125. }
  126. static const TypeInfo puv3_ost_info = {
  127. .name = TYPE_PUV3_OST,
  128. .parent = TYPE_SYS_BUS_DEVICE,
  129. .instance_size = sizeof(PUV3OSTState),
  130. .class_init = puv3_ost_class_init,
  131. };
  132. static void puv3_ost_register_type(void)
  133. {
  134. type_register_static(&puv3_ost_info);
  135. }
  136. type_init(puv3_ost_register_type)