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://www.milkymist.org/socdoc/ac97.pdf
25 #include "hw/sysbus.h"
27 #include "audio/audio.h"
28 #include "qemu/error-report.h"
46 AC97_CTRL_RQEN
= (1<<0),
47 AC97_CTRL_WRITE
= (1<<1),
54 #define TYPE_MILKYMIST_AC97 "milkymist-ac97"
55 #define MILKYMIST_AC97(obj) \
56 OBJECT_CHECK(MilkymistAC97State, (obj), TYPE_MILKYMIST_AC97)
58 struct MilkymistAC97State
{
59 SysBusDevice parent_obj
;
61 MemoryRegion regs_region
;
65 SWVoiceOut
*voice_out
;
69 qemu_irq crrequest_irq
;
74 typedef struct MilkymistAC97State MilkymistAC97State
;
76 static void update_voices(MilkymistAC97State
*s
)
78 if (s
->regs
[R_D_CTRL
] & CTRL_EN
) {
79 AUD_set_active_out(s
->voice_out
, 1);
81 AUD_set_active_out(s
->voice_out
, 0);
84 if (s
->regs
[R_U_CTRL
] & CTRL_EN
) {
85 AUD_set_active_in(s
->voice_in
, 1);
87 AUD_set_active_in(s
->voice_in
, 0);
91 static uint64_t ac97_read(void *opaque
, hwaddr addr
,
94 MilkymistAC97State
*s
= opaque
;
113 error_report("milkymist_ac97: read access to unknown register 0x"
114 TARGET_FMT_plx
, addr
<< 2);
118 trace_milkymist_ac97_memory_read(addr
<< 2, r
);
123 static void ac97_write(void *opaque
, hwaddr addr
, uint64_t value
,
126 MilkymistAC97State
*s
= opaque
;
128 trace_milkymist_ac97_memory_write(addr
, value
);
133 /* always raise an IRQ according to the direction */
134 if (value
& AC97_CTRL_RQEN
) {
135 if (value
& AC97_CTRL_WRITE
) {
136 trace_milkymist_ac97_pulse_irq_crrequest();
137 qemu_irq_pulse(s
->crrequest_irq
);
139 trace_milkymist_ac97_pulse_irq_crreply();
140 qemu_irq_pulse(s
->crreply_irq
);
144 /* RQEN is self clearing */
145 s
->regs
[addr
] = value
& ~AC97_CTRL_RQEN
;
149 s
->regs
[addr
] = value
;
159 s
->regs
[addr
] = value
;
163 error_report("milkymist_ac97: write access to unknown register 0x"
164 TARGET_FMT_plx
, addr
);
170 static const MemoryRegionOps ac97_mmio_ops
= {
174 .min_access_size
= 4,
175 .max_access_size
= 4,
177 .endianness
= DEVICE_NATIVE_ENDIAN
,
180 static void ac97_in_cb(void *opaque
, int avail_b
)
182 MilkymistAC97State
*s
= opaque
;
184 uint32_t remaining
= s
->regs
[R_U_REMAINING
];
185 int temp
= audio_MIN(remaining
, avail_b
);
186 uint32_t addr
= s
->regs
[R_U_ADDR
];
189 trace_milkymist_ac97_in_cb(avail_b
, remaining
);
191 /* prevent from raising an IRQ */
197 int acquired
, to_copy
;
199 to_copy
= audio_MIN(temp
, sizeof(buf
));
200 acquired
= AUD_read(s
->voice_in
, buf
, to_copy
);
205 cpu_physical_memory_write(addr
, buf
, acquired
);
209 transferred
+= acquired
;
212 trace_milkymist_ac97_in_cb_transferred(transferred
);
214 s
->regs
[R_U_ADDR
] = addr
;
215 s
->regs
[R_U_REMAINING
] -= transferred
;
217 if ((s
->regs
[R_U_CTRL
] & CTRL_EN
) && (s
->regs
[R_U_REMAINING
] == 0)) {
218 trace_milkymist_ac97_pulse_irq_dmaw();
219 qemu_irq_pulse(s
->dmaw_irq
);
223 static void ac97_out_cb(void *opaque
, int free_b
)
225 MilkymistAC97State
*s
= opaque
;
227 uint32_t remaining
= s
->regs
[R_D_REMAINING
];
228 int temp
= audio_MIN(remaining
, free_b
);
229 uint32_t addr
= s
->regs
[R_D_ADDR
];
232 trace_milkymist_ac97_out_cb(free_b
, remaining
);
234 /* prevent from raising an IRQ */
242 to_copy
= audio_MIN(temp
, sizeof(buf
));
243 cpu_physical_memory_read(addr
, buf
, to_copy
);
244 copied
= AUD_write(s
->voice_out
, buf
, to_copy
);
250 transferred
+= copied
;
253 trace_milkymist_ac97_out_cb_transferred(transferred
);
255 s
->regs
[R_D_ADDR
] = addr
;
256 s
->regs
[R_D_REMAINING
] -= transferred
;
258 if ((s
->regs
[R_D_CTRL
] & CTRL_EN
) && (s
->regs
[R_D_REMAINING
] == 0)) {
259 trace_milkymist_ac97_pulse_irq_dmar();
260 qemu_irq_pulse(s
->dmar_irq
);
264 static void milkymist_ac97_reset(DeviceState
*d
)
266 MilkymistAC97State
*s
= MILKYMIST_AC97(d
);
269 for (i
= 0; i
< R_MAX
; i
++) {
273 AUD_set_active_in(s
->voice_in
, 0);
274 AUD_set_active_out(s
->voice_out
, 0);
277 static int ac97_post_load(void *opaque
, int version_id
)
279 MilkymistAC97State
*s
= opaque
;
286 static int milkymist_ac97_init(SysBusDevice
*dev
)
288 MilkymistAC97State
*s
= MILKYMIST_AC97(dev
);
290 struct audsettings as
;
291 sysbus_init_irq(dev
, &s
->crrequest_irq
);
292 sysbus_init_irq(dev
, &s
->crreply_irq
);
293 sysbus_init_irq(dev
, &s
->dmar_irq
);
294 sysbus_init_irq(dev
, &s
->dmaw_irq
);
296 AUD_register_card("Milkymist AC'97", &s
->card
);
300 as
.fmt
= AUD_FMT_S16
;
303 s
->voice_in
= AUD_open_in(&s
->card
, s
->voice_in
,
304 "mm_ac97.in", s
, ac97_in_cb
, &as
);
305 s
->voice_out
= AUD_open_out(&s
->card
, s
->voice_out
,
306 "mm_ac97.out", s
, ac97_out_cb
, &as
);
308 memory_region_init_io(&s
->regs_region
, OBJECT(s
), &ac97_mmio_ops
, s
,
309 "milkymist-ac97", R_MAX
* 4);
310 sysbus_init_mmio(dev
, &s
->regs_region
);
315 static const VMStateDescription vmstate_milkymist_ac97
= {
316 .name
= "milkymist-ac97",
318 .minimum_version_id
= 1,
319 .minimum_version_id_old
= 1,
320 .post_load
= ac97_post_load
,
321 .fields
= (VMStateField
[]) {
322 VMSTATE_UINT32_ARRAY(regs
, MilkymistAC97State
, R_MAX
),
323 VMSTATE_END_OF_LIST()
327 static void milkymist_ac97_class_init(ObjectClass
*klass
, void *data
)
329 DeviceClass
*dc
= DEVICE_CLASS(klass
);
330 SysBusDeviceClass
*k
= SYS_BUS_DEVICE_CLASS(klass
);
332 k
->init
= milkymist_ac97_init
;
333 dc
->reset
= milkymist_ac97_reset
;
334 dc
->vmsd
= &vmstate_milkymist_ac97
;
337 static const TypeInfo milkymist_ac97_info
= {
338 .name
= TYPE_MILKYMIST_AC97
,
339 .parent
= TYPE_SYS_BUS_DEVICE
,
340 .instance_size
= sizeof(MilkymistAC97State
),
341 .class_init
= milkymist_ac97_class_init
,
344 static void milkymist_ac97_register_types(void)
346 type_register_static(&milkymist_ac97_info
);
349 type_init(milkymist_ac97_register_types
)