2
0

cs4231.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. * QEMU Crystal CS4231 audio chip emulation
  3. *
  4. * Copyright (c) 2006 Fabrice Bellard
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. #include "qemu/osdep.h"
  25. #include "hw/sysbus.h"
  26. #include "migration/vmstate.h"
  27. #include "qemu/module.h"
  28. #include "trace.h"
  29. #include "qom/object.h"
  30. /*
  31. * In addition to Crystal CS4231 there is a DMA controller on Sparc.
  32. */
  33. #define CS_SIZE 0x40
  34. #define CS_REGS 16
  35. #define CS_DREGS 32
  36. #define CS_MAXDREG (CS_DREGS - 1)
  37. #define TYPE_CS4231 "sun-CS4231"
  38. typedef struct CSState CSState;
  39. DECLARE_INSTANCE_CHECKER(CSState, CS4231,
  40. TYPE_CS4231)
  41. struct CSState {
  42. SysBusDevice parent_obj;
  43. MemoryRegion iomem;
  44. qemu_irq irq;
  45. uint32_t regs[CS_REGS];
  46. uint8_t dregs[CS_DREGS];
  47. };
  48. #define CS_RAP(s) ((s)->regs[0] & CS_MAXDREG)
  49. #define CS_VER 0xa0
  50. #define CS_CDC_VER 0x8a
  51. static void cs_reset(DeviceState *d)
  52. {
  53. CSState *s = CS4231(d);
  54. memset(s->regs, 0, CS_REGS * 4);
  55. memset(s->dregs, 0, CS_DREGS);
  56. s->dregs[12] = CS_CDC_VER;
  57. s->dregs[25] = CS_VER;
  58. }
  59. static uint64_t cs_mem_read(void *opaque, hwaddr addr,
  60. unsigned size)
  61. {
  62. CSState *s = opaque;
  63. uint32_t saddr, ret;
  64. saddr = addr >> 2;
  65. switch (saddr) {
  66. case 1:
  67. switch (CS_RAP(s)) {
  68. case 3: // Write only
  69. ret = 0;
  70. break;
  71. default:
  72. ret = s->dregs[CS_RAP(s)];
  73. break;
  74. }
  75. trace_cs4231_mem_readl_dreg(CS_RAP(s), ret);
  76. break;
  77. default:
  78. ret = s->regs[saddr];
  79. trace_cs4231_mem_readl_reg(saddr, ret);
  80. break;
  81. }
  82. return ret;
  83. }
  84. static void cs_mem_write(void *opaque, hwaddr addr,
  85. uint64_t val, unsigned size)
  86. {
  87. CSState *s = opaque;
  88. uint32_t saddr;
  89. saddr = addr >> 2;
  90. trace_cs4231_mem_writel_reg(saddr, s->regs[saddr], val);
  91. switch (saddr) {
  92. case 1:
  93. trace_cs4231_mem_writel_dreg(CS_RAP(s), s->dregs[CS_RAP(s)], val);
  94. switch(CS_RAP(s)) {
  95. case 11:
  96. case 25: // Read only
  97. break;
  98. case 12:
  99. val &= 0x40;
  100. val |= CS_CDC_VER; // Codec version
  101. s->dregs[CS_RAP(s)] = val;
  102. break;
  103. default:
  104. s->dregs[CS_RAP(s)] = val;
  105. break;
  106. }
  107. break;
  108. case 2: // Read only
  109. break;
  110. case 4:
  111. if (val & 1) {
  112. cs_reset(DEVICE(s));
  113. }
  114. val &= 0x7f;
  115. s->regs[saddr] = val;
  116. break;
  117. default:
  118. s->regs[saddr] = val;
  119. break;
  120. }
  121. }
  122. static const MemoryRegionOps cs_mem_ops = {
  123. .read = cs_mem_read,
  124. .write = cs_mem_write,
  125. .endianness = DEVICE_NATIVE_ENDIAN,
  126. };
  127. static const VMStateDescription vmstate_cs4231 = {
  128. .name ="cs4231",
  129. .version_id = 1,
  130. .minimum_version_id = 1,
  131. .fields = (const VMStateField[]) {
  132. VMSTATE_UINT32_ARRAY(regs, CSState, CS_REGS),
  133. VMSTATE_UINT8_ARRAY(dregs, CSState, CS_DREGS),
  134. VMSTATE_END_OF_LIST()
  135. }
  136. };
  137. static void cs4231_init(Object *obj)
  138. {
  139. CSState *s = CS4231(obj);
  140. SysBusDevice *dev = SYS_BUS_DEVICE(obj);
  141. memory_region_init_io(&s->iomem, obj, &cs_mem_ops, s, "cs4321",
  142. CS_SIZE);
  143. sysbus_init_mmio(dev, &s->iomem);
  144. sysbus_init_irq(dev, &s->irq);
  145. }
  146. static void cs4231_class_init(ObjectClass *klass, void *data)
  147. {
  148. DeviceClass *dc = DEVICE_CLASS(klass);
  149. device_class_set_legacy_reset(dc, cs_reset);
  150. dc->vmsd = &vmstate_cs4231;
  151. }
  152. static const TypeInfo cs4231_info = {
  153. .name = TYPE_CS4231,
  154. .parent = TYPE_SYS_BUS_DEVICE,
  155. .instance_size = sizeof(CSState),
  156. .instance_init = cs4231_init,
  157. .class_init = cs4231_class_init,
  158. };
  159. static void cs4231_register_types(void)
  160. {
  161. type_register_static(&cs4231_info);
  162. }
  163. type_init(cs4231_register_types)