Statistics
| Branch: | Revision:

root / hw / milkymist-ac97.c @ d3674c57

History | View | Annotate | Download (7.9 kB)

1 25a8bb96 Michael Walle
/*
2 25a8bb96 Michael Walle
 *  QEMU model of the Milkymist System Controller.
3 25a8bb96 Michael Walle
 *
4 25a8bb96 Michael Walle
 *  Copyright (c) 2010 Michael Walle <michael@walle.cc>
5 25a8bb96 Michael Walle
 *
6 25a8bb96 Michael Walle
 * This library is free software; you can redistribute it and/or
7 25a8bb96 Michael Walle
 * modify it under the terms of the GNU Lesser General Public
8 25a8bb96 Michael Walle
 * License as published by the Free Software Foundation; either
9 25a8bb96 Michael Walle
 * version 2 of the License, or (at your option) any later version.
10 25a8bb96 Michael Walle
 *
11 25a8bb96 Michael Walle
 * This library is distributed in the hope that it will be useful,
12 25a8bb96 Michael Walle
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 25a8bb96 Michael Walle
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 25a8bb96 Michael Walle
 * Lesser General Public License for more details.
15 25a8bb96 Michael Walle
 *
16 25a8bb96 Michael Walle
 * You should have received a copy of the GNU Lesser General Public
17 25a8bb96 Michael Walle
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18 25a8bb96 Michael Walle
 *
19 25a8bb96 Michael Walle
 *
20 25a8bb96 Michael Walle
 * Specification available at:
21 25a8bb96 Michael Walle
 *   http://www.milkymist.org/socdoc/ac97.pdf
22 25a8bb96 Michael Walle
 */
23 25a8bb96 Michael Walle
24 25a8bb96 Michael Walle
#include "hw.h"
25 25a8bb96 Michael Walle
#include "sysbus.h"
26 25a8bb96 Michael Walle
#include "trace.h"
27 25a8bb96 Michael Walle
#include "audio/audio.h"
28 25a8bb96 Michael Walle
#include "qemu-error.h"
29 25a8bb96 Michael Walle
30 25a8bb96 Michael Walle
enum {
31 25a8bb96 Michael Walle
    R_AC97_CTRL = 0,
32 25a8bb96 Michael Walle
    R_AC97_ADDR,
33 25a8bb96 Michael Walle
    R_AC97_DATAOUT,
34 25a8bb96 Michael Walle
    R_AC97_DATAIN,
35 25a8bb96 Michael Walle
    R_D_CTRL,
36 25a8bb96 Michael Walle
    R_D_ADDR,
37 25a8bb96 Michael Walle
    R_D_REMAINING,
38 25a8bb96 Michael Walle
    R_RESERVED,
39 25a8bb96 Michael Walle
    R_U_CTRL,
40 25a8bb96 Michael Walle
    R_U_ADDR,
41 25a8bb96 Michael Walle
    R_U_REMAINING,
42 25a8bb96 Michael Walle
    R_MAX
43 25a8bb96 Michael Walle
};
44 25a8bb96 Michael Walle
45 25a8bb96 Michael Walle
enum {
46 25a8bb96 Michael Walle
    AC97_CTRL_RQEN  = (1<<0),
47 25a8bb96 Michael Walle
    AC97_CTRL_WRITE = (1<<1),
48 25a8bb96 Michael Walle
};
49 25a8bb96 Michael Walle
50 25a8bb96 Michael Walle
enum {
51 25a8bb96 Michael Walle
    CTRL_EN = (1<<0),
52 25a8bb96 Michael Walle
};
53 25a8bb96 Michael Walle
54 25a8bb96 Michael Walle
struct MilkymistAC97State {
55 25a8bb96 Michael Walle
    SysBusDevice busdev;
56 25a8bb96 Michael Walle
57 25a8bb96 Michael Walle
    QEMUSoundCard card;
58 25a8bb96 Michael Walle
    SWVoiceIn *voice_in;
59 25a8bb96 Michael Walle
    SWVoiceOut *voice_out;
60 25a8bb96 Michael Walle
61 25a8bb96 Michael Walle
    uint32_t regs[R_MAX];
62 25a8bb96 Michael Walle
63 25a8bb96 Michael Walle
    qemu_irq crrequest_irq;
64 25a8bb96 Michael Walle
    qemu_irq crreply_irq;
65 25a8bb96 Michael Walle
    qemu_irq dmar_irq;
66 25a8bb96 Michael Walle
    qemu_irq dmaw_irq;
67 25a8bb96 Michael Walle
};
68 25a8bb96 Michael Walle
typedef struct MilkymistAC97State MilkymistAC97State;
69 25a8bb96 Michael Walle
70 25a8bb96 Michael Walle
static void update_voices(MilkymistAC97State *s)
71 25a8bb96 Michael Walle
{
72 25a8bb96 Michael Walle
    if (s->regs[R_D_CTRL] & CTRL_EN) {
73 25a8bb96 Michael Walle
        AUD_set_active_out(s->voice_out, 1);
74 25a8bb96 Michael Walle
    } else {
75 25a8bb96 Michael Walle
        AUD_set_active_out(s->voice_out, 0);
76 25a8bb96 Michael Walle
    }
77 25a8bb96 Michael Walle
78 25a8bb96 Michael Walle
    if (s->regs[R_U_CTRL] & CTRL_EN) {
79 25a8bb96 Michael Walle
        AUD_set_active_in(s->voice_in, 1);
80 25a8bb96 Michael Walle
    } else {
81 25a8bb96 Michael Walle
        AUD_set_active_in(s->voice_in, 0);
82 25a8bb96 Michael Walle
    }
83 25a8bb96 Michael Walle
}
84 25a8bb96 Michael Walle
85 25a8bb96 Michael Walle
static uint32_t ac97_read(void *opaque, target_phys_addr_t addr)
86 25a8bb96 Michael Walle
{
87 25a8bb96 Michael Walle
    MilkymistAC97State *s = opaque;
88 25a8bb96 Michael Walle
    uint32_t r = 0;
89 25a8bb96 Michael Walle
90 25a8bb96 Michael Walle
    addr >>= 2;
91 25a8bb96 Michael Walle
    switch (addr) {
92 25a8bb96 Michael Walle
    case R_AC97_CTRL:
93 25a8bb96 Michael Walle
    case R_AC97_ADDR:
94 25a8bb96 Michael Walle
    case R_AC97_DATAOUT:
95 25a8bb96 Michael Walle
    case R_AC97_DATAIN:
96 25a8bb96 Michael Walle
    case R_D_CTRL:
97 25a8bb96 Michael Walle
    case R_D_ADDR:
98 25a8bb96 Michael Walle
    case R_D_REMAINING:
99 25a8bb96 Michael Walle
    case R_U_CTRL:
100 25a8bb96 Michael Walle
    case R_U_ADDR:
101 25a8bb96 Michael Walle
    case R_U_REMAINING:
102 25a8bb96 Michael Walle
        r = s->regs[addr];
103 25a8bb96 Michael Walle
        break;
104 25a8bb96 Michael Walle
105 25a8bb96 Michael Walle
    default:
106 25a8bb96 Michael Walle
        error_report("milkymist_ac97: read access to unkown register 0x"
107 25a8bb96 Michael Walle
                TARGET_FMT_plx, addr << 2);
108 25a8bb96 Michael Walle
        break;
109 25a8bb96 Michael Walle
    }
110 25a8bb96 Michael Walle
111 25a8bb96 Michael Walle
    trace_milkymist_ac97_memory_read(addr << 2, r);
112 25a8bb96 Michael Walle
113 25a8bb96 Michael Walle
    return r;
114 25a8bb96 Michael Walle
}
115 25a8bb96 Michael Walle
116 25a8bb96 Michael Walle
static void ac97_write(void *opaque, target_phys_addr_t addr, uint32_t value)
117 25a8bb96 Michael Walle
{
118 25a8bb96 Michael Walle
    MilkymistAC97State *s = opaque;
119 25a8bb96 Michael Walle
120 25a8bb96 Michael Walle
    trace_milkymist_ac97_memory_write(addr, value);
121 25a8bb96 Michael Walle
122 25a8bb96 Michael Walle
    addr >>= 2;
123 25a8bb96 Michael Walle
    switch (addr) {
124 25a8bb96 Michael Walle
    case R_AC97_CTRL:
125 25a8bb96 Michael Walle
        /* always raise an IRQ according to the direction */
126 25a8bb96 Michael Walle
        if (value & AC97_CTRL_RQEN) {
127 25a8bb96 Michael Walle
            if (value & AC97_CTRL_WRITE) {
128 25a8bb96 Michael Walle
                trace_milkymist_ac97_pulse_irq_crrequest();
129 25a8bb96 Michael Walle
                qemu_irq_pulse(s->crrequest_irq);
130 25a8bb96 Michael Walle
            } else {
131 25a8bb96 Michael Walle
                trace_milkymist_ac97_pulse_irq_crreply();
132 25a8bb96 Michael Walle
                qemu_irq_pulse(s->crreply_irq);
133 25a8bb96 Michael Walle
            }
134 25a8bb96 Michael Walle
        }
135 25a8bb96 Michael Walle
136 25a8bb96 Michael Walle
        /* RQEN is self clearing */
137 25a8bb96 Michael Walle
        s->regs[addr] = value & ~AC97_CTRL_RQEN;
138 25a8bb96 Michael Walle
        break;
139 25a8bb96 Michael Walle
    case R_D_CTRL:
140 25a8bb96 Michael Walle
    case R_U_CTRL:
141 25a8bb96 Michael Walle
        s->regs[addr] = value;
142 25a8bb96 Michael Walle
        update_voices(s);
143 25a8bb96 Michael Walle
        break;
144 25a8bb96 Michael Walle
    case R_AC97_ADDR:
145 25a8bb96 Michael Walle
    case R_AC97_DATAOUT:
146 25a8bb96 Michael Walle
    case R_AC97_DATAIN:
147 25a8bb96 Michael Walle
    case R_D_ADDR:
148 25a8bb96 Michael Walle
    case R_D_REMAINING:
149 25a8bb96 Michael Walle
    case R_U_ADDR:
150 25a8bb96 Michael Walle
    case R_U_REMAINING:
151 25a8bb96 Michael Walle
        s->regs[addr] = value;
152 25a8bb96 Michael Walle
        break;
153 25a8bb96 Michael Walle
154 25a8bb96 Michael Walle
    default:
155 25a8bb96 Michael Walle
        error_report("milkymist_ac97: write access to unkown register 0x"
156 25a8bb96 Michael Walle
                TARGET_FMT_plx, addr);
157 25a8bb96 Michael Walle
        break;
158 25a8bb96 Michael Walle
    }
159 25a8bb96 Michael Walle
160 25a8bb96 Michael Walle
}
161 25a8bb96 Michael Walle
162 25a8bb96 Michael Walle
static CPUReadMemoryFunc * const ac97_read_fn[] = {
163 25a8bb96 Michael Walle
    NULL,
164 25a8bb96 Michael Walle
    NULL,
165 25a8bb96 Michael Walle
    &ac97_read,
166 25a8bb96 Michael Walle
};
167 25a8bb96 Michael Walle
168 25a8bb96 Michael Walle
static CPUWriteMemoryFunc * const ac97_write_fn[] = {
169 25a8bb96 Michael Walle
    NULL,
170 25a8bb96 Michael Walle
    NULL,
171 25a8bb96 Michael Walle
    &ac97_write,
172 25a8bb96 Michael Walle
};
173 25a8bb96 Michael Walle
174 25a8bb96 Michael Walle
static void ac97_in_cb(void *opaque, int avail_b)
175 25a8bb96 Michael Walle
{
176 25a8bb96 Michael Walle
    MilkymistAC97State *s = opaque;
177 25a8bb96 Michael Walle
    uint8_t buf[4096];
178 25a8bb96 Michael Walle
    uint32_t remaining = s->regs[R_U_REMAINING];
179 25a8bb96 Michael Walle
    int temp = audio_MIN(remaining, avail_b);
180 25a8bb96 Michael Walle
    uint32_t addr = s->regs[R_U_ADDR];
181 25a8bb96 Michael Walle
    int transferred = 0;
182 25a8bb96 Michael Walle
183 25a8bb96 Michael Walle
    trace_milkymist_ac97_in_cb(avail_b, remaining);
184 25a8bb96 Michael Walle
185 25a8bb96 Michael Walle
    /* prevent from raising an IRQ */
186 25a8bb96 Michael Walle
    if (temp == 0) {
187 25a8bb96 Michael Walle
        return;
188 25a8bb96 Michael Walle
    }
189 25a8bb96 Michael Walle
190 25a8bb96 Michael Walle
    while (temp) {
191 25a8bb96 Michael Walle
        int acquired, to_copy;
192 25a8bb96 Michael Walle
193 25a8bb96 Michael Walle
        to_copy = audio_MIN(temp, sizeof(buf));
194 25a8bb96 Michael Walle
        acquired = AUD_read(s->voice_in, buf, to_copy);
195 25a8bb96 Michael Walle
        if (!acquired) {
196 25a8bb96 Michael Walle
            break;
197 25a8bb96 Michael Walle
        }
198 25a8bb96 Michael Walle
199 25a8bb96 Michael Walle
        cpu_physical_memory_write(addr, buf, acquired);
200 25a8bb96 Michael Walle
201 25a8bb96 Michael Walle
        temp -= acquired;
202 25a8bb96 Michael Walle
        addr += acquired;
203 25a8bb96 Michael Walle
        transferred += acquired;
204 25a8bb96 Michael Walle
    }
205 25a8bb96 Michael Walle
206 25a8bb96 Michael Walle
    trace_milkymist_ac97_in_cb_transferred(transferred);
207 25a8bb96 Michael Walle
208 25a8bb96 Michael Walle
    s->regs[R_U_ADDR] = addr;
209 25a8bb96 Michael Walle
    s->regs[R_U_REMAINING] -= transferred;
210 25a8bb96 Michael Walle
211 25a8bb96 Michael Walle
    if ((s->regs[R_U_CTRL] & CTRL_EN) && (s->regs[R_U_REMAINING] == 0)) {
212 25a8bb96 Michael Walle
        trace_milkymist_ac97_pulse_irq_dmaw();
213 25a8bb96 Michael Walle
        qemu_irq_pulse(s->dmaw_irq);
214 25a8bb96 Michael Walle
    }
215 25a8bb96 Michael Walle
}
216 25a8bb96 Michael Walle
217 25a8bb96 Michael Walle
static void ac97_out_cb(void *opaque, int free_b)
218 25a8bb96 Michael Walle
{
219 25a8bb96 Michael Walle
    MilkymistAC97State *s = opaque;
220 25a8bb96 Michael Walle
    uint8_t buf[4096];
221 25a8bb96 Michael Walle
    uint32_t remaining = s->regs[R_D_REMAINING];
222 25a8bb96 Michael Walle
    int temp = audio_MIN(remaining, free_b);
223 25a8bb96 Michael Walle
    uint32_t addr = s->regs[R_D_ADDR];
224 25a8bb96 Michael Walle
    int transferred = 0;
225 25a8bb96 Michael Walle
226 25a8bb96 Michael Walle
    trace_milkymist_ac97_out_cb(free_b, remaining);
227 25a8bb96 Michael Walle
228 25a8bb96 Michael Walle
    /* prevent from raising an IRQ */
229 25a8bb96 Michael Walle
    if (temp == 0) {
230 25a8bb96 Michael Walle
        return;
231 25a8bb96 Michael Walle
    }
232 25a8bb96 Michael Walle
233 25a8bb96 Michael Walle
    while (temp) {
234 25a8bb96 Michael Walle
        int copied, to_copy;
235 25a8bb96 Michael Walle
236 25a8bb96 Michael Walle
        to_copy = audio_MIN(temp, sizeof(buf));
237 25a8bb96 Michael Walle
        cpu_physical_memory_read(addr, buf, to_copy);
238 25a8bb96 Michael Walle
        copied = AUD_write(s->voice_out, buf, to_copy);
239 25a8bb96 Michael Walle
        if (!copied) {
240 25a8bb96 Michael Walle
            break;
241 25a8bb96 Michael Walle
        }
242 25a8bb96 Michael Walle
        temp -= copied;
243 25a8bb96 Michael Walle
        addr += copied;
244 25a8bb96 Michael Walle
        transferred += copied;
245 25a8bb96 Michael Walle
    }
246 25a8bb96 Michael Walle
247 25a8bb96 Michael Walle
    trace_milkymist_ac97_out_cb_transferred(transferred);
248 25a8bb96 Michael Walle
249 25a8bb96 Michael Walle
    s->regs[R_D_ADDR] = addr;
250 25a8bb96 Michael Walle
    s->regs[R_D_REMAINING] -= transferred;
251 25a8bb96 Michael Walle
252 25a8bb96 Michael Walle
    if ((s->regs[R_D_CTRL] & CTRL_EN) && (s->regs[R_D_REMAINING] == 0)) {
253 25a8bb96 Michael Walle
        trace_milkymist_ac97_pulse_irq_dmar();
254 25a8bb96 Michael Walle
        qemu_irq_pulse(s->dmar_irq);
255 25a8bb96 Michael Walle
    }
256 25a8bb96 Michael Walle
}
257 25a8bb96 Michael Walle
258 25a8bb96 Michael Walle
static void milkymist_ac97_reset(DeviceState *d)
259 25a8bb96 Michael Walle
{
260 25a8bb96 Michael Walle
    MilkymistAC97State *s = container_of(d, MilkymistAC97State, busdev.qdev);
261 25a8bb96 Michael Walle
    int i;
262 25a8bb96 Michael Walle
263 25a8bb96 Michael Walle
    for (i = 0; i < R_MAX; i++) {
264 25a8bb96 Michael Walle
        s->regs[i] = 0;
265 25a8bb96 Michael Walle
    }
266 25a8bb96 Michael Walle
267 25a8bb96 Michael Walle
    AUD_set_active_in(s->voice_in, 0);
268 25a8bb96 Michael Walle
    AUD_set_active_out(s->voice_out, 0);
269 25a8bb96 Michael Walle
}
270 25a8bb96 Michael Walle
271 25a8bb96 Michael Walle
static int ac97_post_load(void *opaque, int version_id)
272 25a8bb96 Michael Walle
{
273 25a8bb96 Michael Walle
    MilkymistAC97State *s = opaque;
274 25a8bb96 Michael Walle
275 25a8bb96 Michael Walle
    update_voices(s);
276 25a8bb96 Michael Walle
277 25a8bb96 Michael Walle
    return 0;
278 25a8bb96 Michael Walle
}
279 25a8bb96 Michael Walle
280 25a8bb96 Michael Walle
static int milkymist_ac97_init(SysBusDevice *dev)
281 25a8bb96 Michael Walle
{
282 25a8bb96 Michael Walle
    MilkymistAC97State *s = FROM_SYSBUS(typeof(*s), dev);
283 25a8bb96 Michael Walle
    int ac97_regs;
284 25a8bb96 Michael Walle
285 25a8bb96 Michael Walle
    struct audsettings as;
286 25a8bb96 Michael Walle
    sysbus_init_irq(dev, &s->crrequest_irq);
287 25a8bb96 Michael Walle
    sysbus_init_irq(dev, &s->crreply_irq);
288 25a8bb96 Michael Walle
    sysbus_init_irq(dev, &s->dmar_irq);
289 25a8bb96 Michael Walle
    sysbus_init_irq(dev, &s->dmaw_irq);
290 25a8bb96 Michael Walle
291 25a8bb96 Michael Walle
    AUD_register_card("Milkymist AC'97", &s->card);
292 25a8bb96 Michael Walle
293 25a8bb96 Michael Walle
    as.freq = 48000;
294 25a8bb96 Michael Walle
    as.nchannels = 2;
295 25a8bb96 Michael Walle
    as.fmt = AUD_FMT_S16;
296 25a8bb96 Michael Walle
    as.endianness = 1;
297 25a8bb96 Michael Walle
298 25a8bb96 Michael Walle
    s->voice_in = AUD_open_in(&s->card, s->voice_in,
299 25a8bb96 Michael Walle
            "mm_ac97.in", s, ac97_in_cb, &as);
300 25a8bb96 Michael Walle
    s->voice_out = AUD_open_out(&s->card, s->voice_out,
301 25a8bb96 Michael Walle
            "mm_ac97.out", s, ac97_out_cb, &as);
302 25a8bb96 Michael Walle
303 25a8bb96 Michael Walle
    ac97_regs = cpu_register_io_memory(ac97_read_fn, ac97_write_fn, s,
304 25a8bb96 Michael Walle
            DEVICE_NATIVE_ENDIAN);
305 25a8bb96 Michael Walle
    sysbus_init_mmio(dev, R_MAX * 4, ac97_regs);
306 25a8bb96 Michael Walle
307 25a8bb96 Michael Walle
    return 0;
308 25a8bb96 Michael Walle
}
309 25a8bb96 Michael Walle
310 25a8bb96 Michael Walle
static const VMStateDescription vmstate_milkymist_ac97 = {
311 25a8bb96 Michael Walle
    .name = "milkymist-ac97",
312 25a8bb96 Michael Walle
    .version_id = 1,
313 25a8bb96 Michael Walle
    .minimum_version_id = 1,
314 25a8bb96 Michael Walle
    .minimum_version_id_old = 1,
315 25a8bb96 Michael Walle
    .post_load = ac97_post_load,
316 25a8bb96 Michael Walle
    .fields      = (VMStateField[]) {
317 25a8bb96 Michael Walle
        VMSTATE_UINT32_ARRAY(regs, MilkymistAC97State, R_MAX),
318 25a8bb96 Michael Walle
        VMSTATE_END_OF_LIST()
319 25a8bb96 Michael Walle
    }
320 25a8bb96 Michael Walle
};
321 25a8bb96 Michael Walle
322 25a8bb96 Michael Walle
static SysBusDeviceInfo milkymist_ac97_info = {
323 25a8bb96 Michael Walle
    .init = milkymist_ac97_init,
324 25a8bb96 Michael Walle
    .qdev.name  = "milkymist-ac97",
325 25a8bb96 Michael Walle
    .qdev.size  = sizeof(MilkymistAC97State),
326 25a8bb96 Michael Walle
    .qdev.vmsd  = &vmstate_milkymist_ac97,
327 25a8bb96 Michael Walle
    .qdev.reset = milkymist_ac97_reset,
328 25a8bb96 Michael Walle
};
329 25a8bb96 Michael Walle
330 25a8bb96 Michael Walle
static void milkymist_ac97_register(void)
331 25a8bb96 Michael Walle
{
332 25a8bb96 Michael Walle
    sysbus_register_withprop(&milkymist_ac97_info);
333 25a8bb96 Michael Walle
}
334 25a8bb96 Michael Walle
335 25a8bb96 Michael Walle
device_init(milkymist_ac97_register)