cs4231.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 "sysbus.h"
  25. #include "trace.h"
  26. /*
  27. * In addition to Crystal CS4231 there is a DMA controller on Sparc.
  28. */
  29. #define CS_SIZE 0x40
  30. #define CS_REGS 16
  31. #define CS_DREGS 32
  32. #define CS_MAXDREG (CS_DREGS - 1)
  33. typedef struct CSState {
  34. SysBusDevice busdev;
  35. qemu_irq irq;
  36. uint32_t regs[CS_REGS];
  37. uint8_t dregs[CS_DREGS];
  38. } CSState;
  39. #define CS_RAP(s) ((s)->regs[0] & CS_MAXDREG)
  40. #define CS_VER 0xa0
  41. #define CS_CDC_VER 0x8a
  42. static void cs_reset(DeviceState *d)
  43. {
  44. CSState *s = container_of(d, CSState, busdev.qdev);
  45. memset(s->regs, 0, CS_REGS * 4);
  46. memset(s->dregs, 0, CS_DREGS);
  47. s->dregs[12] = CS_CDC_VER;
  48. s->dregs[25] = CS_VER;
  49. }
  50. static uint32_t cs_mem_readl(void *opaque, target_phys_addr_t addr)
  51. {
  52. CSState *s = opaque;
  53. uint32_t saddr, ret;
  54. saddr = addr >> 2;
  55. switch (saddr) {
  56. case 1:
  57. switch (CS_RAP(s)) {
  58. case 3: // Write only
  59. ret = 0;
  60. break;
  61. default:
  62. ret = s->dregs[CS_RAP(s)];
  63. break;
  64. }
  65. trace_cs4231_mem_readl_dreg(CS_RAP(s), ret);
  66. break;
  67. default:
  68. ret = s->regs[saddr];
  69. trace_cs4231_mem_readl_reg(saddr, ret);
  70. break;
  71. }
  72. return ret;
  73. }
  74. static void cs_mem_writel(void *opaque, target_phys_addr_t addr, uint32_t val)
  75. {
  76. CSState *s = opaque;
  77. uint32_t saddr;
  78. saddr = addr >> 2;
  79. trace_cs4231_mem_writel_reg(saddr, s->regs[saddr], val);
  80. switch (saddr) {
  81. case 1:
  82. trace_cs4231_mem_writel_dreg(CS_RAP(s), s->dregs[CS_RAP(s)], val);
  83. switch(CS_RAP(s)) {
  84. case 11:
  85. case 25: // Read only
  86. break;
  87. case 12:
  88. val &= 0x40;
  89. val |= CS_CDC_VER; // Codec version
  90. s->dregs[CS_RAP(s)] = val;
  91. break;
  92. default:
  93. s->dregs[CS_RAP(s)] = val;
  94. break;
  95. }
  96. break;
  97. case 2: // Read only
  98. break;
  99. case 4:
  100. if (val & 1) {
  101. cs_reset(&s->busdev.qdev);
  102. }
  103. val &= 0x7f;
  104. s->regs[saddr] = val;
  105. break;
  106. default:
  107. s->regs[saddr] = val;
  108. break;
  109. }
  110. }
  111. static CPUReadMemoryFunc * const cs_mem_read[3] = {
  112. cs_mem_readl,
  113. cs_mem_readl,
  114. cs_mem_readl,
  115. };
  116. static CPUWriteMemoryFunc * const cs_mem_write[3] = {
  117. cs_mem_writel,
  118. cs_mem_writel,
  119. cs_mem_writel,
  120. };
  121. static const VMStateDescription vmstate_cs4231 = {
  122. .name ="cs4231",
  123. .version_id = 1,
  124. .minimum_version_id = 1,
  125. .minimum_version_id_old = 1,
  126. .fields = (VMStateField []) {
  127. VMSTATE_UINT32_ARRAY(regs, CSState, CS_REGS),
  128. VMSTATE_UINT8_ARRAY(dregs, CSState, CS_DREGS),
  129. VMSTATE_END_OF_LIST()
  130. }
  131. };
  132. static int cs4231_init1(SysBusDevice *dev)
  133. {
  134. int io;
  135. CSState *s = FROM_SYSBUS(CSState, dev);
  136. io = cpu_register_io_memory(cs_mem_read, cs_mem_write, s,
  137. DEVICE_NATIVE_ENDIAN);
  138. sysbus_init_mmio(dev, CS_SIZE, io);
  139. sysbus_init_irq(dev, &s->irq);
  140. return 0;
  141. }
  142. static SysBusDeviceInfo cs4231_info = {
  143. .init = cs4231_init1,
  144. .qdev.name = "SUNW,CS4231",
  145. .qdev.size = sizeof(CSState),
  146. .qdev.vmsd = &vmstate_cs4231,
  147. .qdev.reset = cs_reset,
  148. .qdev.props = (Property[]) {
  149. {.name = NULL}
  150. }
  151. };
  152. static void cs4231_register_devices(void)
  153. {
  154. sysbus_register_withprop(&cs4231_info);
  155. }
  156. device_init(cs4231_register_devices)