puv3_ost.c 3.8 KB

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