2 * QEMU model of the Milkymist System Controller.
4 * Copyright (c) 2010 Michael Walle <michael@walle.cc>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
20 * Specification available at:
21 * http://milkymist.walle.cc/socdoc/ac97.pdf
24 #include "qemu/osdep.h"
26 #include "hw/sysbus.h"
28 #include "audio/audio.h"
29 #include "qemu/error-report.h"
47 AC97_CTRL_RQEN
= (1<<0),
48 AC97_CTRL_WRITE
= (1<<1),
55 #define TYPE_MILKYMIST_AC97 "milkymist-ac97"
56 #define MILKYMIST_AC97(obj) \
57 OBJECT_CHECK(MilkymistAC97State, (obj), TYPE_MILKYMIST_AC97)
59 struct MilkymistAC97State
{
60 SysBusDevice parent_obj
;
62 MemoryRegion regs_region
;
66 SWVoiceOut
*voice_out
;
70 qemu_irq crrequest_irq
;
75 typedef struct MilkymistAC97State MilkymistAC97State
;
77 static void update_voices(MilkymistAC97State
*s
)
79 if (s
->regs
[R_D_CTRL
] & CTRL_EN
) {
80 AUD_set_active_out(s
->voice_out
, 1);
82 AUD_set_active_out(s
->voice_out
, 0);
85 if (s
->regs
[R_U_CTRL
] & CTRL_EN
) {
86 AUD_set_active_in(s
->voice_in
, 1);
88 AUD_set_active_in(s
->voice_in
, 0);
92 static uint64_t ac97_read(void *opaque
, hwaddr addr
,
95 MilkymistAC97State
*s
= opaque
;
114 error_report("milkymist_ac97: read access to unknown register 0x"
115 TARGET_FMT_plx
, addr
<< 2);
119 trace_milkymist_ac97_memory_read(addr
<< 2, r
);
124 static void ac97_write(void *opaque
, hwaddr addr
, uint64_t value
,
127 MilkymistAC97State
*s
= opaque
;
129 trace_milkymist_ac97_memory_write(addr
, value
);
134 /* always raise an IRQ according to the direction */
135 if (value
& AC97_CTRL_RQEN
) {
136 if (value
& AC97_CTRL_WRITE
) {
137 trace_milkymist_ac97_pulse_irq_crrequest();
138 qemu_irq_pulse(s
->crrequest_irq
);
140 trace_milkymist_ac97_pulse_irq_crreply();
141 qemu_irq_pulse(s
->crreply_irq
);
145 /* RQEN is self clearing */
146 s
->regs
[addr
] = value
& ~AC97_CTRL_RQEN
;
150 s
->regs
[addr
] = value
;
160 s
->regs
[addr
] = value
;
164 error_report("milkymist_ac97: write access to unknown register 0x"
165 TARGET_FMT_plx
, addr
);
171 static const MemoryRegionOps ac97_mmio_ops
= {
175 .min_access_size
= 4,
176 .max_access_size
= 4,
178 .endianness
= DEVICE_NATIVE_ENDIAN
,
181 static void ac97_in_cb(void *opaque
, int avail_b
)
183 MilkymistAC97State
*s
= opaque
;
185 uint32_t remaining
= s
->regs
[R_U_REMAINING
];
186 int temp
= audio_MIN(remaining
, avail_b
);
187 uint32_t addr
= s
->regs
[R_U_ADDR
];
190 trace_milkymist_ac97_in_cb(avail_b
, remaining
);
192 /* prevent from raising an IRQ */
198 int acquired
, to_copy
;
200 to_copy
= audio_MIN(temp
, sizeof(buf
));
201 acquired
= AUD_read(s
->voice_in
, buf
, to_copy
);
206 cpu_physical_memory_write(addr
, buf
, acquired
);
210 transferred
+= acquired
;
213 trace_milkymist_ac97_in_cb_transferred(transferred
);
215 s
->regs
[R_U_ADDR
] = addr
;
216 s
->regs
[R_U_REMAINING
] -= transferred
;
218 if ((s
->regs
[R_U_CTRL
] & CTRL_EN
) && (s
->regs
[R_U_REMAINING
] == 0)) {
219 trace_milkymist_ac97_pulse_irq_dmaw();
220 qemu_irq_pulse(s
->dmaw_irq
);
224 static void ac97_out_cb(void *opaque
, int free_b
)
226 MilkymistAC97State
*s
= opaque
;
228 uint32_t remaining
= s
->regs
[R_D_REMAINING
];
229 int temp
= audio_MIN(remaining
, free_b
);
230 uint32_t addr
= s
->regs
[R_D_ADDR
];
233 trace_milkymist_ac97_out_cb(free_b
, remaining
);
235 /* prevent from raising an IRQ */
243 to_copy
= audio_MIN(temp
, sizeof(buf
));
244 cpu_physical_memory_read(addr
, buf
, to_copy
);
245 copied
= AUD_write(s
->voice_out
, buf
, to_copy
);
251 transferred
+= copied
;
254 trace_milkymist_ac97_out_cb_transferred(transferred
);
256 s
->regs
[R_D_ADDR
] = addr
;
257 s
->regs
[R_D_REMAINING
] -= transferred
;
259 if ((s
->regs
[R_D_CTRL
] & CTRL_EN
) && (s
->regs
[R_D_REMAINING
] == 0)) {
260 trace_milkymist_ac97_pulse_irq_dmar();
261 qemu_irq_pulse(s
->dmar_irq
);
265 static void milkymist_ac97_reset(DeviceState
*d
)
267 MilkymistAC97State
*s
= MILKYMIST_AC97(d
);
270 for (i
= 0; i
< R_MAX
; i
++) {
274 AUD_set_active_in(s
->voice_in
, 0);
275 AUD_set_active_out(s
->voice_out
, 0);
278 static int ac97_post_load(void *opaque
, int version_id
)
280 MilkymistAC97State
*s
= opaque
;
287 static void milkymist_ac97_init(Object
*obj
)
289 MilkymistAC97State
*s
= MILKYMIST_AC97(obj
);
290 SysBusDevice
*dev
= SYS_BUS_DEVICE(obj
);
292 sysbus_init_irq(dev
, &s
->crrequest_irq
);
293 sysbus_init_irq(dev
, &s
->crreply_irq
);
294 sysbus_init_irq(dev
, &s
->dmar_irq
);
295 sysbus_init_irq(dev
, &s
->dmaw_irq
);
297 memory_region_init_io(&s
->regs_region
, obj
, &ac97_mmio_ops
, s
,
298 "milkymist-ac97", R_MAX
* 4);
299 sysbus_init_mmio(dev
, &s
->regs_region
);
302 static void milkymist_ac97_realize(DeviceState
*dev
, Error
**errp
)
304 MilkymistAC97State
*s
= MILKYMIST_AC97(dev
);
305 struct audsettings as
;
307 AUD_register_card("Milkymist AC'97", &s
->card
);
311 as
.fmt
= AUD_FMT_S16
;
314 s
->voice_in
= AUD_open_in(&s
->card
, s
->voice_in
,
315 "mm_ac97.in", s
, ac97_in_cb
, &as
);
316 s
->voice_out
= AUD_open_out(&s
->card
, s
->voice_out
,
317 "mm_ac97.out", s
, ac97_out_cb
, &as
);
320 static const VMStateDescription vmstate_milkymist_ac97
= {
321 .name
= "milkymist-ac97",
323 .minimum_version_id
= 1,
324 .post_load
= ac97_post_load
,
325 .fields
= (VMStateField
[]) {
326 VMSTATE_UINT32_ARRAY(regs
, MilkymistAC97State
, R_MAX
),
327 VMSTATE_END_OF_LIST()
331 static void milkymist_ac97_class_init(ObjectClass
*klass
, void *data
)
333 DeviceClass
*dc
= DEVICE_CLASS(klass
);
335 dc
->realize
= milkymist_ac97_realize
;
336 dc
->reset
= milkymist_ac97_reset
;
337 dc
->vmsd
= &vmstate_milkymist_ac97
;
340 static const TypeInfo milkymist_ac97_info
= {
341 .name
= TYPE_MILKYMIST_AC97
,
342 .parent
= TYPE_SYS_BUS_DEVICE
,
343 .instance_size
= sizeof(MilkymistAC97State
),
344 .instance_init
= milkymist_ac97_init
,
345 .class_init
= milkymist_ac97_class_init
,
348 static void milkymist_ac97_register_types(void)
350 type_register_static(&milkymist_ac97_info
);
353 type_init(milkymist_ac97_register_types
)