Andrzej Zaborowski | ef07b49 | 2009-08-23 15:44:15 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Marvell 88w8618 audio emulation extracted from |
| 3 | * Marvell MV88w8618 / Freecom MusicPal emulation. |
| 4 | * |
| 5 | * Copyright (c) 2008 Jan Kiszka |
| 6 | * |
| 7 | * This code is licenced under the GNU GPL v2. |
| 8 | */ |
| 9 | #include "sysbus.h" |
| 10 | #include "hw.h" |
| 11 | #include "i2c.h" |
| 12 | #include "sysbus.h" |
| 13 | #include "audio/audio.h" |
| 14 | |
| 15 | #define MP_AUDIO_SIZE 0x00001000 |
| 16 | |
| 17 | /* Audio register offsets */ |
| 18 | #define MP_AUDIO_PLAYBACK_MODE 0x00 |
| 19 | #define MP_AUDIO_CLOCK_DIV 0x18 |
| 20 | #define MP_AUDIO_IRQ_STATUS 0x20 |
| 21 | #define MP_AUDIO_IRQ_ENABLE 0x24 |
| 22 | #define MP_AUDIO_TX_START_LO 0x28 |
| 23 | #define MP_AUDIO_TX_THRESHOLD 0x2C |
| 24 | #define MP_AUDIO_TX_STATUS 0x38 |
| 25 | #define MP_AUDIO_TX_START_HI 0x40 |
| 26 | |
| 27 | /* Status register and IRQ enable bits */ |
| 28 | #define MP_AUDIO_TX_HALF (1 << 6) |
| 29 | #define MP_AUDIO_TX_FULL (1 << 7) |
| 30 | |
| 31 | /* Playback mode bits */ |
| 32 | #define MP_AUDIO_16BIT_SAMPLE (1 << 0) |
| 33 | #define MP_AUDIO_PLAYBACK_EN (1 << 7) |
| 34 | #define MP_AUDIO_CLOCK_24MHZ (1 << 9) |
| 35 | #define MP_AUDIO_MONO (1 << 14) |
| 36 | |
| 37 | #ifdef HAS_AUDIO |
| 38 | typedef struct mv88w8618_audio_state { |
| 39 | SysBusDevice busdev; |
| 40 | qemu_irq irq; |
| 41 | uint32_t playback_mode; |
| 42 | uint32_t status; |
| 43 | uint32_t irq_enable; |
Jan Kiszka | d5b61dd | 2009-09-18 20:51:23 +0200 | [diff] [blame] | 44 | uint32_t phys_buf; |
Andrzej Zaborowski | ef07b49 | 2009-08-23 15:44:15 +0200 | [diff] [blame] | 45 | uint32_t target_buffer; |
Jan Kiszka | d5b61dd | 2009-09-18 20:51:23 +0200 | [diff] [blame] | 46 | uint32_t threshold; |
| 47 | uint32_t play_pos; |
| 48 | uint32_t last_free; |
Andrzej Zaborowski | ef07b49 | 2009-08-23 15:44:15 +0200 | [diff] [blame] | 49 | uint32_t clock_div; |
| 50 | DeviceState *wm; |
| 51 | } mv88w8618_audio_state; |
| 52 | |
| 53 | static void mv88w8618_audio_callback(void *opaque, int free_out, int free_in) |
| 54 | { |
| 55 | mv88w8618_audio_state *s = opaque; |
| 56 | int16_t *codec_buffer; |
| 57 | int8_t buf[4096]; |
| 58 | int8_t *mem_buffer; |
| 59 | int pos, block_size; |
| 60 | |
Jan Kiszka | 49fedd0 | 2009-09-18 20:51:23 +0200 | [diff] [blame] | 61 | if (!(s->playback_mode & MP_AUDIO_PLAYBACK_EN)) { |
Andrzej Zaborowski | ef07b49 | 2009-08-23 15:44:15 +0200 | [diff] [blame] | 62 | return; |
Jan Kiszka | 49fedd0 | 2009-09-18 20:51:23 +0200 | [diff] [blame] | 63 | } |
| 64 | if (s->playback_mode & MP_AUDIO_16BIT_SAMPLE) { |
Andrzej Zaborowski | ef07b49 | 2009-08-23 15:44:15 +0200 | [diff] [blame] | 65 | free_out <<= 1; |
Jan Kiszka | 49fedd0 | 2009-09-18 20:51:23 +0200 | [diff] [blame] | 66 | } |
| 67 | if (!(s->playback_mode & MP_AUDIO_MONO)) { |
Andrzej Zaborowski | ef07b49 | 2009-08-23 15:44:15 +0200 | [diff] [blame] | 68 | free_out <<= 1; |
Jan Kiszka | 49fedd0 | 2009-09-18 20:51:23 +0200 | [diff] [blame] | 69 | } |
Andrzej Zaborowski | ef07b49 | 2009-08-23 15:44:15 +0200 | [diff] [blame] | 70 | block_size = s->threshold / 2; |
Jan Kiszka | 49fedd0 | 2009-09-18 20:51:23 +0200 | [diff] [blame] | 71 | if (free_out - s->last_free < block_size) { |
Andrzej Zaborowski | ef07b49 | 2009-08-23 15:44:15 +0200 | [diff] [blame] | 72 | return; |
Jan Kiszka | 49fedd0 | 2009-09-18 20:51:23 +0200 | [diff] [blame] | 73 | } |
| 74 | if (block_size > 4096) { |
Andrzej Zaborowski | ef07b49 | 2009-08-23 15:44:15 +0200 | [diff] [blame] | 75 | return; |
Jan Kiszka | 49fedd0 | 2009-09-18 20:51:23 +0200 | [diff] [blame] | 76 | } |
Andrzej Zaborowski | ef07b49 | 2009-08-23 15:44:15 +0200 | [diff] [blame] | 77 | cpu_physical_memory_read(s->target_buffer + s->play_pos, (void *)buf, |
| 78 | block_size); |
| 79 | mem_buffer = buf; |
| 80 | if (s->playback_mode & MP_AUDIO_16BIT_SAMPLE) { |
| 81 | if (s->playback_mode & MP_AUDIO_MONO) { |
| 82 | codec_buffer = wm8750_dac_buffer(s->wm, block_size >> 1); |
| 83 | for (pos = 0; pos < block_size; pos += 2) { |
| 84 | *codec_buffer++ = *(int16_t *)mem_buffer; |
| 85 | *codec_buffer++ = *(int16_t *)mem_buffer; |
| 86 | mem_buffer += 2; |
| 87 | } |
Jan Kiszka | 49fedd0 | 2009-09-18 20:51:23 +0200 | [diff] [blame] | 88 | } else { |
Andrzej Zaborowski | ef07b49 | 2009-08-23 15:44:15 +0200 | [diff] [blame] | 89 | memcpy(wm8750_dac_buffer(s->wm, block_size >> 2), |
| 90 | (uint32_t *)mem_buffer, block_size); |
Jan Kiszka | 49fedd0 | 2009-09-18 20:51:23 +0200 | [diff] [blame] | 91 | } |
Andrzej Zaborowski | ef07b49 | 2009-08-23 15:44:15 +0200 | [diff] [blame] | 92 | } else { |
| 93 | if (s->playback_mode & MP_AUDIO_MONO) { |
| 94 | codec_buffer = wm8750_dac_buffer(s->wm, block_size); |
| 95 | for (pos = 0; pos < block_size; pos++) { |
| 96 | *codec_buffer++ = cpu_to_le16(256 * *mem_buffer); |
| 97 | *codec_buffer++ = cpu_to_le16(256 * *mem_buffer++); |
| 98 | } |
| 99 | } else { |
| 100 | codec_buffer = wm8750_dac_buffer(s->wm, block_size >> 1); |
| 101 | for (pos = 0; pos < block_size; pos += 2) { |
| 102 | *codec_buffer++ = cpu_to_le16(256 * *mem_buffer++); |
| 103 | *codec_buffer++ = cpu_to_le16(256 * *mem_buffer++); |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | wm8750_dac_commit(s->wm); |
| 108 | |
| 109 | s->last_free = free_out - block_size; |
| 110 | |
| 111 | if (s->play_pos == 0) { |
| 112 | s->status |= MP_AUDIO_TX_HALF; |
| 113 | s->play_pos = block_size; |
| 114 | } else { |
| 115 | s->status |= MP_AUDIO_TX_FULL; |
| 116 | s->play_pos = 0; |
| 117 | } |
| 118 | |
Jan Kiszka | 49fedd0 | 2009-09-18 20:51:23 +0200 | [diff] [blame] | 119 | if (s->status & s->irq_enable) { |
Andrzej Zaborowski | ef07b49 | 2009-08-23 15:44:15 +0200 | [diff] [blame] | 120 | qemu_irq_raise(s->irq); |
Jan Kiszka | 49fedd0 | 2009-09-18 20:51:23 +0200 | [diff] [blame] | 121 | } |
Andrzej Zaborowski | ef07b49 | 2009-08-23 15:44:15 +0200 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | static void mv88w8618_audio_clock_update(mv88w8618_audio_state *s) |
| 125 | { |
| 126 | int rate; |
| 127 | |
Jan Kiszka | 49fedd0 | 2009-09-18 20:51:23 +0200 | [diff] [blame] | 128 | if (s->playback_mode & MP_AUDIO_CLOCK_24MHZ) { |
Andrzej Zaborowski | ef07b49 | 2009-08-23 15:44:15 +0200 | [diff] [blame] | 129 | rate = 24576000 / 64; /* 24.576MHz */ |
Jan Kiszka | 49fedd0 | 2009-09-18 20:51:23 +0200 | [diff] [blame] | 130 | } else { |
Andrzej Zaborowski | ef07b49 | 2009-08-23 15:44:15 +0200 | [diff] [blame] | 131 | rate = 11289600 / 64; /* 11.2896MHz */ |
Jan Kiszka | 49fedd0 | 2009-09-18 20:51:23 +0200 | [diff] [blame] | 132 | } |
Andrzej Zaborowski | ef07b49 | 2009-08-23 15:44:15 +0200 | [diff] [blame] | 133 | rate /= ((s->clock_div >> 8) & 0xff) + 1; |
| 134 | |
| 135 | wm8750_set_bclk_in(s->wm, rate); |
| 136 | } |
| 137 | |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 138 | static uint32_t mv88w8618_audio_read(void *opaque, target_phys_addr_t offset) |
Andrzej Zaborowski | ef07b49 | 2009-08-23 15:44:15 +0200 | [diff] [blame] | 139 | { |
| 140 | mv88w8618_audio_state *s = opaque; |
| 141 | |
| 142 | switch (offset) { |
| 143 | case MP_AUDIO_PLAYBACK_MODE: |
| 144 | return s->playback_mode; |
| 145 | |
| 146 | case MP_AUDIO_CLOCK_DIV: |
| 147 | return s->clock_div; |
| 148 | |
| 149 | case MP_AUDIO_IRQ_STATUS: |
| 150 | return s->status; |
| 151 | |
| 152 | case MP_AUDIO_IRQ_ENABLE: |
| 153 | return s->irq_enable; |
| 154 | |
| 155 | case MP_AUDIO_TX_STATUS: |
| 156 | return s->play_pos >> 2; |
| 157 | |
| 158 | default: |
| 159 | return 0; |
| 160 | } |
| 161 | } |
| 162 | |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 163 | static void mv88w8618_audio_write(void *opaque, target_phys_addr_t offset, |
Andrzej Zaborowski | ef07b49 | 2009-08-23 15:44:15 +0200 | [diff] [blame] | 164 | uint32_t value) |
| 165 | { |
| 166 | mv88w8618_audio_state *s = opaque; |
| 167 | |
| 168 | switch (offset) { |
| 169 | case MP_AUDIO_PLAYBACK_MODE: |
| 170 | if (value & MP_AUDIO_PLAYBACK_EN && |
| 171 | !(s->playback_mode & MP_AUDIO_PLAYBACK_EN)) { |
| 172 | s->status = 0; |
| 173 | s->last_free = 0; |
| 174 | s->play_pos = 0; |
| 175 | } |
| 176 | s->playback_mode = value; |
| 177 | mv88w8618_audio_clock_update(s); |
| 178 | break; |
| 179 | |
| 180 | case MP_AUDIO_CLOCK_DIV: |
| 181 | s->clock_div = value; |
| 182 | s->last_free = 0; |
| 183 | s->play_pos = 0; |
| 184 | mv88w8618_audio_clock_update(s); |
| 185 | break; |
| 186 | |
| 187 | case MP_AUDIO_IRQ_STATUS: |
| 188 | s->status &= ~value; |
| 189 | break; |
| 190 | |
| 191 | case MP_AUDIO_IRQ_ENABLE: |
| 192 | s->irq_enable = value; |
Jan Kiszka | 49fedd0 | 2009-09-18 20:51:23 +0200 | [diff] [blame] | 193 | if (s->status & s->irq_enable) { |
Andrzej Zaborowski | ef07b49 | 2009-08-23 15:44:15 +0200 | [diff] [blame] | 194 | qemu_irq_raise(s->irq); |
Jan Kiszka | 49fedd0 | 2009-09-18 20:51:23 +0200 | [diff] [blame] | 195 | } |
Andrzej Zaborowski | ef07b49 | 2009-08-23 15:44:15 +0200 | [diff] [blame] | 196 | break; |
| 197 | |
| 198 | case MP_AUDIO_TX_START_LO: |
| 199 | s->phys_buf = (s->phys_buf & 0xFFFF0000) | (value & 0xFFFF); |
| 200 | s->target_buffer = s->phys_buf; |
| 201 | s->play_pos = 0; |
| 202 | s->last_free = 0; |
| 203 | break; |
| 204 | |
| 205 | case MP_AUDIO_TX_THRESHOLD: |
| 206 | s->threshold = (value + 1) * 4; |
| 207 | break; |
| 208 | |
| 209 | case MP_AUDIO_TX_START_HI: |
| 210 | s->phys_buf = (s->phys_buf & 0xFFFF) | (value << 16); |
| 211 | s->target_buffer = s->phys_buf; |
| 212 | s->play_pos = 0; |
| 213 | s->last_free = 0; |
| 214 | break; |
| 215 | } |
| 216 | } |
| 217 | |
Jan Kiszka | d5b61dd | 2009-09-18 20:51:23 +0200 | [diff] [blame] | 218 | static void mv88w8618_audio_reset(DeviceState *d) |
Andrzej Zaborowski | ef07b49 | 2009-08-23 15:44:15 +0200 | [diff] [blame] | 219 | { |
Jan Kiszka | d5b61dd | 2009-09-18 20:51:23 +0200 | [diff] [blame] | 220 | mv88w8618_audio_state *s = FROM_SYSBUS(mv88w8618_audio_state, |
| 221 | sysbus_from_qdev(d)); |
Andrzej Zaborowski | ef07b49 | 2009-08-23 15:44:15 +0200 | [diff] [blame] | 222 | |
| 223 | s->playback_mode = 0; |
| 224 | s->status = 0; |
| 225 | s->irq_enable = 0; |
Jan Kiszka | 267c484 | 2009-09-18 20:51:23 +0200 | [diff] [blame] | 226 | s->clock_div = 0; |
| 227 | s->threshold = 0; |
| 228 | s->phys_buf = 0; |
Andrzej Zaborowski | ef07b49 | 2009-08-23 15:44:15 +0200 | [diff] [blame] | 229 | } |
| 230 | |
Blue Swirl | d60efc6 | 2009-08-25 18:29:31 +0000 | [diff] [blame] | 231 | static CPUReadMemoryFunc * const mv88w8618_audio_readfn[] = { |
Andrzej Zaborowski | ef07b49 | 2009-08-23 15:44:15 +0200 | [diff] [blame] | 232 | mv88w8618_audio_read, |
| 233 | mv88w8618_audio_read, |
| 234 | mv88w8618_audio_read |
| 235 | }; |
| 236 | |
Blue Swirl | d60efc6 | 2009-08-25 18:29:31 +0000 | [diff] [blame] | 237 | static CPUWriteMemoryFunc * const mv88w8618_audio_writefn[] = { |
Andrzej Zaborowski | ef07b49 | 2009-08-23 15:44:15 +0200 | [diff] [blame] | 238 | mv88w8618_audio_write, |
| 239 | mv88w8618_audio_write, |
| 240 | mv88w8618_audio_write |
| 241 | }; |
| 242 | |
Gerd Hoffmann | 81a322d | 2009-08-14 10:36:05 +0200 | [diff] [blame] | 243 | static int mv88w8618_audio_init(SysBusDevice *dev) |
Andrzej Zaborowski | ef07b49 | 2009-08-23 15:44:15 +0200 | [diff] [blame] | 244 | { |
| 245 | mv88w8618_audio_state *s = FROM_SYSBUS(mv88w8618_audio_state, dev); |
| 246 | int iomemtype; |
| 247 | |
| 248 | sysbus_init_irq(dev, &s->irq); |
| 249 | |
| 250 | wm8750_data_req_set(s->wm, mv88w8618_audio_callback, s); |
| 251 | |
| 252 | iomemtype = cpu_register_io_memory(mv88w8618_audio_readfn, |
| 253 | mv88w8618_audio_writefn, s); |
| 254 | sysbus_init_mmio(dev, MP_AUDIO_SIZE, iomemtype); |
| 255 | |
Gerd Hoffmann | 81a322d | 2009-08-14 10:36:05 +0200 | [diff] [blame] | 256 | return 0; |
Andrzej Zaborowski | ef07b49 | 2009-08-23 15:44:15 +0200 | [diff] [blame] | 257 | } |
| 258 | |
Jan Kiszka | d5b61dd | 2009-09-18 20:51:23 +0200 | [diff] [blame] | 259 | static const VMStateDescription mv88w8618_audio_vmsd = { |
| 260 | .name = "mv88w8618_audio", |
| 261 | .version_id = 1, |
| 262 | .minimum_version_id = 1, |
| 263 | .minimum_version_id_old = 1, |
| 264 | .fields = (VMStateField[]) { |
| 265 | VMSTATE_UINT32(playback_mode, mv88w8618_audio_state), |
| 266 | VMSTATE_UINT32(status, mv88w8618_audio_state), |
| 267 | VMSTATE_UINT32(irq_enable, mv88w8618_audio_state), |
| 268 | VMSTATE_UINT32(phys_buf, mv88w8618_audio_state), |
| 269 | VMSTATE_UINT32(target_buffer, mv88w8618_audio_state), |
| 270 | VMSTATE_UINT32(threshold, mv88w8618_audio_state), |
| 271 | VMSTATE_UINT32(play_pos, mv88w8618_audio_state), |
| 272 | VMSTATE_UINT32(last_free, mv88w8618_audio_state), |
| 273 | VMSTATE_UINT32(clock_div, mv88w8618_audio_state), |
| 274 | VMSTATE_END_OF_LIST() |
| 275 | } |
| 276 | }; |
| 277 | |
Andrzej Zaborowski | ef07b49 | 2009-08-23 15:44:15 +0200 | [diff] [blame] | 278 | static SysBusDeviceInfo mv88w8618_audio_info = { |
| 279 | .init = mv88w8618_audio_init, |
| 280 | .qdev.name = "mv88w8618_audio", |
| 281 | .qdev.size = sizeof(mv88w8618_audio_state), |
Jan Kiszka | 267c484 | 2009-09-18 20:51:23 +0200 | [diff] [blame] | 282 | .qdev.reset = mv88w8618_audio_reset, |
Jan Kiszka | d5b61dd | 2009-09-18 20:51:23 +0200 | [diff] [blame] | 283 | .qdev.vmsd = &mv88w8618_audio_vmsd, |
Andrzej Zaborowski | ef07b49 | 2009-08-23 15:44:15 +0200 | [diff] [blame] | 284 | .qdev.props = (Property[]) { |
| 285 | { |
| 286 | .name = "wm8750", |
| 287 | .info = &qdev_prop_ptr, |
| 288 | .offset = offsetof(mv88w8618_audio_state, wm), |
| 289 | }, |
| 290 | {/* end of list */} |
| 291 | } |
| 292 | }; |
| 293 | #endif |
| 294 | |
| 295 | static void mv88w8618_register_devices(void) |
| 296 | { |
| 297 | #ifdef HAS_AUDIO |
| 298 | sysbus_register_withprop(&mv88w8618_audio_info); |
| 299 | #endif |
| 300 | } |
| 301 | |
| 302 | device_init(mv88w8618_register_devices) |