1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27#define SILENT_ES1370
28
29#include "hw.h"
30#include "audiodev.h"
31#include "audio/audio.h"
32#include "pci.h"
33#include "dma.h"
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54#define ES1370_REG_CONTROL 0x00
55#define ES1370_REG_STATUS 0x04
56#define ES1370_REG_UART_DATA 0x08
57#define ES1370_REG_UART_STATUS 0x09
58#define ES1370_REG_UART_CONTROL 0x09
59#define ES1370_REG_UART_TEST 0x0a
60#define ES1370_REG_MEMPAGE 0x0c
61#define ES1370_REG_CODEC 0x10
62#define ES1370_REG_SERIAL_CONTROL 0x20
63#define ES1370_REG_DAC1_SCOUNT 0x24
64#define ES1370_REG_DAC2_SCOUNT 0x28
65#define ES1370_REG_ADC_SCOUNT 0x2c
66
67#define ES1370_REG_DAC1_FRAMEADR 0xc30
68#define ES1370_REG_DAC1_FRAMECNT 0xc34
69#define ES1370_REG_DAC2_FRAMEADR 0xc38
70#define ES1370_REG_DAC2_FRAMECNT 0xc3c
71#define ES1370_REG_ADC_FRAMEADR 0xd30
72#define ES1370_REG_ADC_FRAMECNT 0xd34
73#define ES1370_REG_PHANTOM_FRAMEADR 0xd38
74#define ES1370_REG_PHANTOM_FRAMECNT 0xd3c
75
76static const unsigned dac1_samplerate[] = { 5512, 11025, 22050, 44100 };
77
78#define DAC2_SRTODIV(x) (((1411200+(x)/2)/(x))-2)
79#define DAC2_DIVTOSR(x) (1411200/((x)+2))
80
81#define CTRL_ADC_STOP 0x80000000
82#define CTRL_XCTL1 0x40000000
83#define CTRL_OPEN 0x20000000
84#define CTRL_PCLKDIV 0x1fff0000
85#define CTRL_SH_PCLKDIV 16
86#define CTRL_MSFMTSEL 0x00008000
87#define CTRL_M_SBB 0x00004000
88#define CTRL_WTSRSEL 0x00003000
89#define CTRL_SH_WTSRSEL 12
90#define CTRL_DAC_SYNC 0x00000800
91#define CTRL_CCB_INTRM 0x00000400
92#define CTRL_M_CB 0x00000200
93#define CTRL_XCTL0 0x00000100
94#define CTRL_BREQ 0x00000080
95#define CTRL_DAC1_EN 0x00000040
96#define CTRL_DAC2_EN 0x00000020
97#define CTRL_ADC_EN 0x00000010
98#define CTRL_UART_EN 0x00000008
99#define CTRL_JYSTK_EN 0x00000004
100#define CTRL_CDC_EN 0x00000002
101#define CTRL_SERR_DIS 0x00000001
102
103#define STAT_INTR 0x80000000
104#define STAT_CSTAT 0x00000400
105#define STAT_CBUSY 0x00000200
106#define STAT_CWRIP 0x00000100
107#define STAT_VC 0x00000060
108#define STAT_SH_VC 5
109#define STAT_MCCB 0x00000010
110#define STAT_UART 0x00000008
111#define STAT_DAC1 0x00000004
112#define STAT_DAC2 0x00000002
113#define STAT_ADC 0x00000001
114
115#define USTAT_RXINT 0x80
116#define USTAT_TXINT 0x04
117#define USTAT_TXRDY 0x02
118#define USTAT_RXRDY 0x01
119
120#define UCTRL_RXINTEN 0x80
121#define UCTRL_TXINTEN 0x60
122#define UCTRL_ENA_TXINT 0x20
123#define UCTRL_CNTRL 0x03
124#define UCTRL_CNTRL_SWR 0x03
125
126#define SCTRL_P2ENDINC 0x00380000
127#define SCTRL_SH_P2ENDINC 19
128#define SCTRL_P2STINC 0x00070000
129#define SCTRL_SH_P2STINC 16
130#define SCTRL_R1LOOPSEL 0x00008000
131#define SCTRL_P2LOOPSEL 0x00004000
132#define SCTRL_P1LOOPSEL 0x00002000
133#define SCTRL_P2PAUSE 0x00001000
134#define SCTRL_P1PAUSE 0x00000800
135#define SCTRL_R1INTEN 0x00000400
136#define SCTRL_P2INTEN 0x00000200
137#define SCTRL_P1INTEN 0x00000100
138#define SCTRL_P1SCTRLD 0x00000080
139#define SCTRL_P2DACSEN 0x00000040
140#define SCTRL_R1SEB 0x00000020
141#define SCTRL_R1SMB 0x00000010
142#define SCTRL_R1FMT 0x00000030
143#define SCTRL_SH_R1FMT 4
144#define SCTRL_P2SEB 0x00000008
145#define SCTRL_P2SMB 0x00000004
146#define SCTRL_P2FMT 0x0000000c
147#define SCTRL_SH_P2FMT 2
148#define SCTRL_P1SEB 0x00000002
149#define SCTRL_P1SMB 0x00000001
150#define SCTRL_P1FMT 0x00000003
151#define SCTRL_SH_P1FMT 0
152
153
154
155#define NB_CHANNELS 3
156#define DAC1_CHANNEL 0
157#define DAC2_CHANNEL 1
158#define ADC_CHANNEL 2
159
160#define IO_READ_PROTO(n) \
161static uint32_t n (void *opaque, uint32_t addr)
162#define IO_WRITE_PROTO(n) \
163static void n (void *opaque, uint32_t addr, uint32_t val)
164
165static void es1370_dac1_callback (void *opaque, int free);
166static void es1370_dac2_callback (void *opaque, int free);
167static void es1370_adc_callback (void *opaque, int avail);
168
169#ifdef DEBUG_ES1370
170
171#define ldebug(...) AUD_log ("es1370", __VA_ARGS__)
172
173static void print_ctl (uint32_t val)
174{
175 char buf[1024];
176
177 buf[0] = '\0';
178#define a(n) if (val & CTRL_##n) strcat (buf, " "#n)
179 a (ADC_STOP);
180 a (XCTL1);
181 a (OPEN);
182 a (MSFMTSEL);
183 a (M_SBB);
184 a (DAC_SYNC);
185 a (CCB_INTRM);
186 a (M_CB);
187 a (XCTL0);
188 a (BREQ);
189 a (DAC1_EN);
190 a (DAC2_EN);
191 a (ADC_EN);
192 a (UART_EN);
193 a (JYSTK_EN);
194 a (CDC_EN);
195 a (SERR_DIS);
196#undef a
197 AUD_log ("es1370", "ctl - PCLKDIV %d(DAC2 freq %d), freq %d,%s\n",
198 (val & CTRL_PCLKDIV) >> CTRL_SH_PCLKDIV,
199 DAC2_DIVTOSR ((val & CTRL_PCLKDIV) >> CTRL_SH_PCLKDIV),
200 dac1_samplerate[(val & CTRL_WTSRSEL) >> CTRL_SH_WTSRSEL],
201 buf);
202}
203
204static void print_sctl (uint32_t val)
205{
206 static const char *fmt_names[] = {"8M", "8S", "16M", "16S"};
207 char buf[1024];
208
209 buf[0] = '\0';
210
211#define a(n) if (val & SCTRL_##n) strcat (buf, " "#n)
212#define b(n) if (!(val & SCTRL_##n)) strcat (buf, " "#n)
213 b (R1LOOPSEL);
214 b (P2LOOPSEL);
215 b (P1LOOPSEL);
216 a (P2PAUSE);
217 a (P1PAUSE);
218 a (R1INTEN);
219 a (P2INTEN);
220 a (P1INTEN);
221 a (P1SCTRLD);
222 a (P2DACSEN);
223 if (buf[0]) {
224 strcat (buf, "\n ");
225 }
226 else {
227 buf[0] = ' ';
228 buf[1] = '\0';
229 }
230#undef b
231#undef a
232 AUD_log ("es1370",
233 "%s"
234 "p2_end_inc %d, p2_st_inc %d, r1_fmt %s, p2_fmt %s, p1_fmt %s\n",
235 buf,
236 (val & SCTRL_P2ENDINC) >> SCTRL_SH_P2ENDINC,
237 (val & SCTRL_P2STINC) >> SCTRL_SH_P2STINC,
238 fmt_names [(val >> SCTRL_SH_R1FMT) & 3],
239 fmt_names [(val >> SCTRL_SH_P2FMT) & 3],
240 fmt_names [(val >> SCTRL_SH_P1FMT) & 3]
241 );
242}
243#else
244#define ldebug(...)
245#define print_ctl(...)
246#define print_sctl(...)
247#endif
248
249#ifdef VERBOSE_ES1370
250#define dolog(...) AUD_log ("es1370", __VA_ARGS__)
251#else
252#define dolog(...)
253#endif
254
255#ifndef SILENT_ES1370
256#define lwarn(...) AUD_log ("es1370: warning", __VA_ARGS__)
257#else
258#define lwarn(...)
259#endif
260
261struct chan {
262 uint32_t shift;
263 uint32_t leftover;
264 uint32_t scount;
265 uint32_t frame_addr;
266 uint32_t frame_cnt;
267};
268
269typedef struct ES1370State {
270 PCIDevice dev;
271 QEMUSoundCard card;
272 MemoryRegion io;
273 struct chan chan[NB_CHANNELS];
274 SWVoiceOut *dac_voice[2];
275 SWVoiceIn *adc_voice;
276
277 uint32_t ctl;
278 uint32_t status;
279 uint32_t mempage;
280 uint32_t codec;
281 uint32_t sctl;
282} ES1370State;
283
284struct chan_bits {
285 uint32_t ctl_en;
286 uint32_t stat_int;
287 uint32_t sctl_pause;
288 uint32_t sctl_inten;
289 uint32_t sctl_fmt;
290 uint32_t sctl_sh_fmt;
291 uint32_t sctl_loopsel;
292 void (*calc_freq) (ES1370State *s, uint32_t ctl,
293 uint32_t *old_freq, uint32_t *new_freq);
294};
295
296static void es1370_dac1_calc_freq (ES1370State *s, uint32_t ctl,
297 uint32_t *old_freq, uint32_t *new_freq);
298static void es1370_dac2_and_adc_calc_freq (ES1370State *s, uint32_t ctl,
299 uint32_t *old_freq,
300 uint32_t *new_freq);
301
302static const struct chan_bits es1370_chan_bits[] = {
303 {CTRL_DAC1_EN, STAT_DAC1, SCTRL_P1PAUSE, SCTRL_P1INTEN,
304 SCTRL_P1FMT, SCTRL_SH_P1FMT, SCTRL_P1LOOPSEL,
305 es1370_dac1_calc_freq},
306
307 {CTRL_DAC2_EN, STAT_DAC2, SCTRL_P2PAUSE, SCTRL_P2INTEN,
308 SCTRL_P2FMT, SCTRL_SH_P2FMT, SCTRL_P2LOOPSEL,
309 es1370_dac2_and_adc_calc_freq},
310
311 {CTRL_ADC_EN, STAT_ADC, 0, SCTRL_R1INTEN,
312 SCTRL_R1FMT, SCTRL_SH_R1FMT, SCTRL_R1LOOPSEL,
313 es1370_dac2_and_adc_calc_freq}
314};
315
316static void es1370_update_status (ES1370State *s, uint32_t new_status)
317{
318 uint32_t level = new_status & (STAT_DAC1 | STAT_DAC2 | STAT_ADC);
319
320 if (level) {
321 s->status = new_status | STAT_INTR;
322 }
323 else {
324 s->status = new_status & ~STAT_INTR;
325 }
326 qemu_set_irq (s->dev.irq[0], !!level);
327}
328
329static void es1370_reset (ES1370State *s)
330{
331 size_t i;
332
333 s->ctl = 1;
334 s->status = 0x60;
335 s->mempage = 0;
336 s->codec = 0;
337 s->sctl = 0;
338
339 for (i = 0; i < NB_CHANNELS; ++i) {
340 struct chan *d = &s->chan[i];
341 d->scount = 0;
342 d->leftover = 0;
343 if (i == ADC_CHANNEL) {
344 AUD_close_in (&s->card, s->adc_voice);
345 s->adc_voice = NULL;
346 }
347 else {
348 AUD_close_out (&s->card, s->dac_voice[i]);
349 s->dac_voice[i] = NULL;
350 }
351 }
352 qemu_irq_lower (s->dev.irq[0]);
353}
354
355static void es1370_maybe_lower_irq (ES1370State *s, uint32_t sctl)
356{
357 uint32_t new_status = s->status;
358
359 if (!(sctl & SCTRL_P1INTEN) && (s->sctl & SCTRL_P1INTEN)) {
360 new_status &= ~STAT_DAC1;
361 }
362
363 if (!(sctl & SCTRL_P2INTEN) && (s->sctl & SCTRL_P2INTEN)) {
364 new_status &= ~STAT_DAC2;
365 }
366
367 if (!(sctl & SCTRL_R1INTEN) && (s->sctl & SCTRL_R1INTEN)) {
368 new_status &= ~STAT_ADC;
369 }
370
371 if (new_status != s->status) {
372 es1370_update_status (s, new_status);
373 }
374}
375
376static void es1370_dac1_calc_freq (ES1370State *s, uint32_t ctl,
377 uint32_t *old_freq, uint32_t *new_freq)
378
379{
380 *old_freq = dac1_samplerate[(s->ctl & CTRL_WTSRSEL) >> CTRL_SH_WTSRSEL];
381 *new_freq = dac1_samplerate[(ctl & CTRL_WTSRSEL) >> CTRL_SH_WTSRSEL];
382}
383
384static void es1370_dac2_and_adc_calc_freq (ES1370State *s, uint32_t ctl,
385 uint32_t *old_freq,
386 uint32_t *new_freq)
387
388{
389 uint32_t old_pclkdiv, new_pclkdiv;
390
391 new_pclkdiv = (ctl & CTRL_PCLKDIV) >> CTRL_SH_PCLKDIV;
392 old_pclkdiv = (s->ctl & CTRL_PCLKDIV) >> CTRL_SH_PCLKDIV;
393 *new_freq = DAC2_DIVTOSR (new_pclkdiv);
394 *old_freq = DAC2_DIVTOSR (old_pclkdiv);
395}
396
397static void es1370_update_voices (ES1370State *s, uint32_t ctl, uint32_t sctl)
398{
399 size_t i;
400 uint32_t old_freq, new_freq, old_fmt, new_fmt;
401
402 for (i = 0; i < NB_CHANNELS; ++i) {
403 struct chan *d = &s->chan[i];
404 const struct chan_bits *b = &es1370_chan_bits[i];
405
406 new_fmt = (sctl & b->sctl_fmt) >> b->sctl_sh_fmt;
407 old_fmt = (s->sctl & b->sctl_fmt) >> b->sctl_sh_fmt;
408
409 b->calc_freq (s, ctl, &old_freq, &new_freq);
410
411 if ((old_fmt != new_fmt) || (old_freq != new_freq)) {
412 d->shift = (new_fmt & 1) + (new_fmt >> 1);
413 ldebug ("channel %zu, freq = %d, nchannels %d, fmt %d, shift %d\n",
414 i,
415 new_freq,
416 1 << (new_fmt & 1),
417 (new_fmt & 2) ? AUD_FMT_S16 : AUD_FMT_U8,
418 d->shift);
419 if (new_freq) {
420 struct audsettings as;
421
422 as.freq = new_freq;
423 as.nchannels = 1 << (new_fmt & 1);
424 as.fmt = (new_fmt & 2) ? AUD_FMT_S16 : AUD_FMT_U8;
425 as.endianness = 0;
426
427 if (i == ADC_CHANNEL) {
428 s->adc_voice =
429 AUD_open_in (
430 &s->card,
431 s->adc_voice,
432 "es1370.adc",
433 s,
434 es1370_adc_callback,
435 &as
436 );
437 }
438 else {
439 s->dac_voice[i] =
440 AUD_open_out (
441 &s->card,
442 s->dac_voice[i],
443 i ? "es1370.dac2" : "es1370.dac1",
444 s,
445 i ? es1370_dac2_callback : es1370_dac1_callback,
446 &as
447 );
448 }
449 }
450 }
451
452 if (((ctl ^ s->ctl) & b->ctl_en)
453 || ((sctl ^ s->sctl) & b->sctl_pause)) {
454 int on = (ctl & b->ctl_en) && !(sctl & b->sctl_pause);
455
456 if (i == ADC_CHANNEL) {
457 AUD_set_active_in (s->adc_voice, on);
458 }
459 else {
460 AUD_set_active_out (s->dac_voice[i], on);
461 }
462 }
463 }
464
465 s->ctl = ctl;
466 s->sctl = sctl;
467}
468
469static inline uint32_t es1370_fixup (ES1370State *s, uint32_t addr)
470{
471 addr &= 0xff;
472 if (addr >= 0x30 && addr <= 0x3f)
473 addr |= s->mempage << 8;
474 return addr;
475}
476
477IO_WRITE_PROTO (es1370_writeb)
478{
479 ES1370State *s = opaque;
480 uint32_t shift, mask;
481
482 addr = es1370_fixup (s, addr);
483
484 switch (addr) {
485 case ES1370_REG_CONTROL:
486 case ES1370_REG_CONTROL + 1:
487 case ES1370_REG_CONTROL + 2:
488 case ES1370_REG_CONTROL + 3:
489 shift = (addr - ES1370_REG_CONTROL) << 3;
490 mask = 0xff << shift;
491 val = (s->ctl & ~mask) | ((val & 0xff) << shift);
492 es1370_update_voices (s, val, s->sctl);
493 print_ctl (val);
494 break;
495 case ES1370_REG_MEMPAGE:
496 s->mempage = val;
497 break;
498 case ES1370_REG_SERIAL_CONTROL:
499 case ES1370_REG_SERIAL_CONTROL + 1:
500 case ES1370_REG_SERIAL_CONTROL + 2:
501 case ES1370_REG_SERIAL_CONTROL + 3:
502 shift = (addr - ES1370_REG_SERIAL_CONTROL) << 3;
503 mask = 0xff << shift;
504 val = (s->sctl & ~mask) | ((val & 0xff) << shift);
505 es1370_maybe_lower_irq (s, val);
506 es1370_update_voices (s, s->ctl, val);
507 print_sctl (val);
508 break;
509 default:
510 lwarn ("writeb %#x <- %#x\n", addr, val);
511 break;
512 }
513}
514
515IO_WRITE_PROTO (es1370_writew)
516{
517 ES1370State *s = opaque;
518 addr = es1370_fixup (s, addr);
519 uint32_t shift, mask;
520 struct chan *d = &s->chan[0];
521
522 switch (addr) {
523 case ES1370_REG_CODEC:
524 dolog ("ignored codec write address %#x, data %#x\n",
525 (val >> 8) & 0xff, val & 0xff);
526 s->codec = val;
527 break;
528
529 case ES1370_REG_CONTROL:
530 case ES1370_REG_CONTROL + 2:
531 shift = (addr != ES1370_REG_CONTROL) << 4;
532 mask = 0xffff << shift;
533 val = (s->ctl & ~mask) | ((val & 0xffff) << shift);
534 es1370_update_voices (s, val, s->sctl);
535 print_ctl (val);
536 break;
537
538 case ES1370_REG_ADC_SCOUNT:
539 d++;
540 case ES1370_REG_DAC2_SCOUNT:
541 d++;
542 case ES1370_REG_DAC1_SCOUNT:
543 d->scount = (d->scount & ~0xffff) | (val & 0xffff);
544 break;
545
546 default:
547 lwarn ("writew %#x <- %#x\n", addr, val);
548 break;
549 }
550}
551
552IO_WRITE_PROTO (es1370_writel)
553{
554 ES1370State *s = opaque;
555 struct chan *d = &s->chan[0];
556
557 addr = es1370_fixup (s, addr);
558
559 switch (addr) {
560 case ES1370_REG_CONTROL:
561 es1370_update_voices (s, val, s->sctl);
562 print_ctl (val);
563 break;
564
565 case ES1370_REG_MEMPAGE:
566 s->mempage = val & 0xf;
567 break;
568
569 case ES1370_REG_SERIAL_CONTROL:
570 es1370_maybe_lower_irq (s, val);
571 es1370_update_voices (s, s->ctl, val);
572 print_sctl (val);
573 break;
574
575 case ES1370_REG_ADC_SCOUNT:
576 d++;
577 case ES1370_REG_DAC2_SCOUNT:
578 d++;
579 case ES1370_REG_DAC1_SCOUNT:
580 d->scount = (val & 0xffff) | (d->scount & ~0xffff);
581 ldebug ("chan %td CURR_SAMP_CT %d, SAMP_CT %d\n",
582 d - &s->chan[0], val >> 16, (val & 0xffff));
583 break;
584
585 case ES1370_REG_ADC_FRAMEADR:
586 d++;
587 case ES1370_REG_DAC2_FRAMEADR:
588 d++;
589 case ES1370_REG_DAC1_FRAMEADR:
590 d->frame_addr = val;
591 ldebug ("chan %td frame address %#x\n", d - &s->chan[0], val);
592 break;
593
594 case ES1370_REG_PHANTOM_FRAMECNT:
595 lwarn ("writing to phantom frame count %#x\n", val);
596 break;
597 case ES1370_REG_PHANTOM_FRAMEADR:
598 lwarn ("writing to phantom frame address %#x\n", val);
599 break;
600
601 case ES1370_REG_ADC_FRAMECNT:
602 d++;
603 case ES1370_REG_DAC2_FRAMECNT:
604 d++;
605 case ES1370_REG_DAC1_FRAMECNT:
606 d->frame_cnt = val;
607 d->leftover = 0;
608 ldebug ("chan %td frame count %d, buffer size %d\n",
609 d - &s->chan[0], val >> 16, val & 0xffff);
610 break;
611
612 default:
613 lwarn ("writel %#x <- %#x\n", addr, val);
614 break;
615 }
616}
617
618IO_READ_PROTO (es1370_readb)
619{
620 ES1370State *s = opaque;
621 uint32_t val;
622
623 addr = es1370_fixup (s, addr);
624
625 switch (addr) {
626 case 0x1b:
627 lwarn ("Attempt to read from legacy register\n");
628 val = 5;
629 break;
630 case ES1370_REG_MEMPAGE:
631 val = s->mempage;
632 break;
633 case ES1370_REG_CONTROL + 0:
634 case ES1370_REG_CONTROL + 1:
635 case ES1370_REG_CONTROL + 2:
636 case ES1370_REG_CONTROL + 3:
637 val = s->ctl >> ((addr - ES1370_REG_CONTROL) << 3);
638 break;
639 case ES1370_REG_STATUS + 0:
640 case ES1370_REG_STATUS + 1:
641 case ES1370_REG_STATUS + 2:
642 case ES1370_REG_STATUS + 3:
643 val = s->status >> ((addr - ES1370_REG_STATUS) << 3);
644 break;
645 default:
646 val = ~0;
647 lwarn ("readb %#x -> %#x\n", addr, val);
648 break;
649 }
650 return val;
651}
652
653IO_READ_PROTO (es1370_readw)
654{
655 ES1370State *s = opaque;
656 struct chan *d = &s->chan[0];
657 uint32_t val;
658
659 addr = es1370_fixup (s, addr);
660
661 switch (addr) {
662 case ES1370_REG_ADC_SCOUNT + 2:
663 d++;
664 case ES1370_REG_DAC2_SCOUNT + 2:
665 d++;
666 case ES1370_REG_DAC1_SCOUNT + 2:
667 val = d->scount >> 16;
668 break;
669
670 case ES1370_REG_ADC_FRAMECNT:
671 d++;
672 case ES1370_REG_DAC2_FRAMECNT:
673 d++;
674 case ES1370_REG_DAC1_FRAMECNT:
675 val = d->frame_cnt & 0xffff;
676 break;
677
678 case ES1370_REG_ADC_FRAMECNT + 2:
679 d++;
680 case ES1370_REG_DAC2_FRAMECNT + 2:
681 d++;
682 case ES1370_REG_DAC1_FRAMECNT + 2:
683 val = d->frame_cnt >> 16;
684 break;
685
686 default:
687 val = ~0;
688 lwarn ("readw %#x -> %#x\n", addr, val);
689 break;
690 }
691
692 return val;
693}
694
695IO_READ_PROTO (es1370_readl)
696{
697 ES1370State *s = opaque;
698 uint32_t val;
699 struct chan *d = &s->chan[0];
700
701 addr = es1370_fixup (s, addr);
702
703 switch (addr) {
704 case ES1370_REG_CONTROL:
705 val = s->ctl;
706 break;
707 case ES1370_REG_STATUS:
708 val = s->status;
709 break;
710 case ES1370_REG_MEMPAGE:
711 val = s->mempage;
712 break;
713 case ES1370_REG_CODEC:
714 val = s->codec;
715 break;
716 case ES1370_REG_SERIAL_CONTROL:
717 val = s->sctl;
718 break;
719
720 case ES1370_REG_ADC_SCOUNT:
721 d++;
722 case ES1370_REG_DAC2_SCOUNT:
723 d++;
724 case ES1370_REG_DAC1_SCOUNT:
725 val = d->scount;
726#ifdef DEBUG_ES1370
727 {
728 uint32_t curr_count = d->scount >> 16;
729 uint32_t count = d->scount & 0xffff;
730
731 curr_count <<= d->shift;
732 count <<= d->shift;
733 dolog ("read scount curr %d, total %d\n", curr_count, count);
734 }
735#endif
736 break;
737
738 case ES1370_REG_ADC_FRAMECNT:
739 d++;
740 case ES1370_REG_DAC2_FRAMECNT:
741 d++;
742 case ES1370_REG_DAC1_FRAMECNT:
743 val = d->frame_cnt;
744#ifdef DEBUG_ES1370
745 {
746 uint32_t size = ((d->frame_cnt & 0xffff) + 1) << 2;
747 uint32_t curr = ((d->frame_cnt >> 16) + 1) << 2;
748 if (curr > size) {
749 dolog ("read framecnt curr %d, size %d %d\n", curr, size,
750 curr > size);
751 }
752 }
753#endif
754 break;
755
756 case ES1370_REG_ADC_FRAMEADR:
757 d++;
758 case ES1370_REG_DAC2_FRAMEADR:
759 d++;
760 case ES1370_REG_DAC1_FRAMEADR:
761 val = d->frame_addr;
762 break;
763
764 case ES1370_REG_PHANTOM_FRAMECNT:
765 val = ~0U;
766 lwarn ("reading from phantom frame count\n");
767 break;
768 case ES1370_REG_PHANTOM_FRAMEADR:
769 val = ~0U;
770 lwarn ("reading from phantom frame address\n");
771 break;
772
773 default:
774 val = ~0U;
775 lwarn ("readl %#x -> %#x\n", addr, val);
776 break;
777 }
778 return val;
779}
780
781static void es1370_transfer_audio (ES1370State *s, struct chan *d, int loop_sel,
782 int max, int *irq)
783{
784 uint8_t tmpbuf[4096];
785 uint32_t addr = d->frame_addr;
786 int sc = d->scount & 0xffff;
787 int csc = d->scount >> 16;
788 int csc_bytes = (csc + 1) << d->shift;
789 int cnt = d->frame_cnt >> 16;
790 int size = d->frame_cnt & 0xffff;
791 int left = ((size - cnt + 1) << 2) + d->leftover;
792 int transferred = 0;
793 int temp = audio_MIN (max, audio_MIN (left, csc_bytes));
794 int index = d - &s->chan[0];
795
796 addr += (cnt << 2) + d->leftover;
797
798 if (index == ADC_CHANNEL) {
799 while (temp) {
800 int acquired, to_copy;
801
802 to_copy = audio_MIN ((size_t) temp, sizeof (tmpbuf));
803 acquired = AUD_read (s->adc_voice, tmpbuf, to_copy);
804 if (!acquired)
805 break;
806
807 pci_dma_write (&s->dev, addr, tmpbuf, acquired);
808
809 temp -= acquired;
810 addr += acquired;
811 transferred += acquired;
812 }
813 }
814 else {
815 SWVoiceOut *voice = s->dac_voice[index];
816
817 while (temp) {
818 int copied, to_copy;
819
820 to_copy = audio_MIN ((size_t) temp, sizeof (tmpbuf));
821 pci_dma_read (&s->dev, addr, tmpbuf, to_copy);
822 copied = AUD_write (voice, tmpbuf, to_copy);
823 if (!copied)
824 break;
825 temp -= copied;
826 addr += copied;
827 transferred += copied;
828 }
829 }
830
831 if (csc_bytes == transferred) {
832 *irq = 1;
833 d->scount = sc | (sc << 16);
834 ldebug ("sc = %d, rate = %f\n",
835 (sc + 1) << d->shift,
836 (sc + 1) / (double) 44100);
837 }
838 else {
839 *irq = 0;
840 d->scount = sc | (((csc_bytes - transferred - 1) >> d->shift) << 16);
841 }
842
843 cnt += (transferred + d->leftover) >> 2;
844
845 if (s->sctl & loop_sel) {
846
847
848 AUD_log ("es1370: warning", "non looping mode\n");
849 }
850 else {
851 d->frame_cnt = size;
852
853 if ((uint32_t) cnt <= d->frame_cnt)
854 d->frame_cnt |= cnt << 16;
855 }
856
857 d->leftover = (transferred + d->leftover) & 3;
858}
859
860static void es1370_run_channel (ES1370State *s, size_t chan, int free_or_avail)
861{
862 uint32_t new_status = s->status;
863 int max_bytes, irq;
864 struct chan *d = &s->chan[chan];
865 const struct chan_bits *b = &es1370_chan_bits[chan];
866
867 if (!(s->ctl & b->ctl_en) || (s->sctl & b->sctl_pause)) {
868 return;
869 }
870
871 max_bytes = free_or_avail;
872 max_bytes &= ~((1 << d->shift) - 1);
873 if (!max_bytes) {
874 return;
875 }
876
877 es1370_transfer_audio (s, d, b->sctl_loopsel, max_bytes, &irq);
878
879 if (irq) {
880 if (s->sctl & b->sctl_inten) {
881 new_status |= b->stat_int;
882 }
883 }
884
885 if (new_status != s->status) {
886 es1370_update_status (s, new_status);
887 }
888}
889
890static void es1370_dac1_callback (void *opaque, int free)
891{
892 ES1370State *s = opaque;
893
894 es1370_run_channel (s, DAC1_CHANNEL, free);
895}
896
897static void es1370_dac2_callback (void *opaque, int free)
898{
899 ES1370State *s = opaque;
900
901 es1370_run_channel (s, DAC2_CHANNEL, free);
902}
903
904static void es1370_adc_callback (void *opaque, int avail)
905{
906 ES1370State *s = opaque;
907
908 es1370_run_channel (s, ADC_CHANNEL, avail);
909}
910
911static const MemoryRegionPortio es1370_portio[] = {
912 { 0, 0x40 * 4, 1, .write = es1370_writeb, },
913 { 0, 0x40 * 2, 2, .write = es1370_writew, },
914 { 0, 0x40, 4, .write = es1370_writel, },
915 { 0, 0x40 * 4, 1, .read = es1370_readb, },
916 { 0, 0x40 * 2, 2, .read = es1370_readw, },
917 { 0, 0x40, 4, .read = es1370_readl, },
918 PORTIO_END_OF_LIST ()
919};
920
921static const MemoryRegionOps es1370_io_ops = {
922 .old_portio = es1370_portio,
923 .endianness = DEVICE_LITTLE_ENDIAN,
924};
925
926static const VMStateDescription vmstate_es1370_channel = {
927 .name = "es1370_channel",
928 .version_id = 2,
929 .minimum_version_id = 2,
930 .minimum_version_id_old = 2,
931 .fields = (VMStateField []) {
932 VMSTATE_UINT32 (shift, struct chan),
933 VMSTATE_UINT32 (leftover, struct chan),
934 VMSTATE_UINT32 (scount, struct chan),
935 VMSTATE_UINT32 (frame_addr, struct chan),
936 VMSTATE_UINT32 (frame_cnt, struct chan),
937 VMSTATE_END_OF_LIST ()
938 }
939};
940
941static int es1370_post_load (void *opaque, int version_id)
942{
943 uint32_t ctl, sctl;
944 ES1370State *s = opaque;
945 size_t i;
946
947 for (i = 0; i < NB_CHANNELS; ++i) {
948 if (i == ADC_CHANNEL) {
949 if (s->adc_voice) {
950 AUD_close_in (&s->card, s->adc_voice);
951 s->adc_voice = NULL;
952 }
953 }
954 else {
955 if (s->dac_voice[i]) {
956 AUD_close_out (&s->card, s->dac_voice[i]);
957 s->dac_voice[i] = NULL;
958 }
959 }
960 }
961
962 ctl = s->ctl;
963 sctl = s->sctl;
964 s->ctl = 0;
965 s->sctl = 0;
966 es1370_update_voices (s, ctl, sctl);
967 return 0;
968}
969
970static const VMStateDescription vmstate_es1370 = {
971 .name = "es1370",
972 .version_id = 2,
973 .minimum_version_id = 2,
974 .minimum_version_id_old = 2,
975 .post_load = es1370_post_load,
976 .fields = (VMStateField []) {
977 VMSTATE_PCI_DEVICE (dev, ES1370State),
978 VMSTATE_STRUCT_ARRAY (chan, ES1370State, NB_CHANNELS, 2,
979 vmstate_es1370_channel, struct chan),
980 VMSTATE_UINT32 (ctl, ES1370State),
981 VMSTATE_UINT32 (status, ES1370State),
982 VMSTATE_UINT32 (mempage, ES1370State),
983 VMSTATE_UINT32 (codec, ES1370State),
984 VMSTATE_UINT32 (sctl, ES1370State),
985 VMSTATE_END_OF_LIST ()
986 }
987};
988
989static void es1370_on_reset (void *opaque)
990{
991 ES1370State *s = opaque;
992 es1370_reset (s);
993}
994
995static int es1370_initfn (PCIDevice *dev)
996{
997 ES1370State *s = DO_UPCAST (ES1370State, dev, dev);
998 uint8_t *c = s->dev.config;
999
1000 c[PCI_STATUS + 1] = PCI_STATUS_DEVSEL_SLOW >> 8;
1001
1002#if 0
1003 c[PCI_CAPABILITY_LIST] = 0xdc;
1004 c[PCI_INTERRUPT_LINE] = 10;
1005 c[0xdc] = 0x00;
1006#endif
1007
1008 c[PCI_INTERRUPT_PIN] = 1;
1009 c[PCI_MIN_GNT] = 0x0c;
1010 c[PCI_MAX_LAT] = 0x80;
1011
1012 memory_region_init_io (&s->io, &es1370_io_ops, s, "es1370", 256);
1013 pci_register_bar (&s->dev, 0, PCI_BASE_ADDRESS_SPACE_IO, &s->io);
1014 qemu_register_reset (es1370_on_reset, s);
1015
1016 AUD_register_card ("es1370", &s->card);
1017 es1370_reset (s);
1018 return 0;
1019}
1020
1021static void es1370_exitfn (PCIDevice *dev)
1022{
1023 ES1370State *s = DO_UPCAST (ES1370State, dev, dev);
1024
1025 memory_region_destroy (&s->io);
1026}
1027
1028int es1370_init (PCIBus *bus)
1029{
1030 pci_create_simple (bus, -1, "ES1370");
1031 return 0;
1032}
1033
1034static void es1370_class_init (ObjectClass *klass, void *data)
1035{
1036 DeviceClass *dc = DEVICE_CLASS (klass);
1037 PCIDeviceClass *k = PCI_DEVICE_CLASS (klass);
1038
1039 k->init = es1370_initfn;
1040 k->exit = es1370_exitfn;
1041 k->vendor_id = PCI_VENDOR_ID_ENSONIQ;
1042 k->device_id = PCI_DEVICE_ID_ENSONIQ_ES1370;
1043 k->class_id = PCI_CLASS_MULTIMEDIA_AUDIO;
1044 k->subsystem_vendor_id = 0x4942;
1045 k->subsystem_id = 0x4c4c;
1046 dc->desc = "ENSONIQ AudioPCI ES1370";
1047 dc->vmsd = &vmstate_es1370;
1048}
1049
1050static TypeInfo es1370_info = {
1051 .name = "ES1370",
1052 .parent = TYPE_PCI_DEVICE,
1053 .instance_size = sizeof (ES1370State),
1054 .class_init = es1370_class_init,
1055};
1056
1057static void es1370_register_types (void)
1058{
1059 type_register_static (&es1370_info);
1060}
1061
1062type_init (es1370_register_types)
1063
1064