2 * QEMU ES1370 emulation
4 * Copyright (c) 2005 Vassili Karpov (malc)
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 /* #define DEBUG_ES1370 */
26 /* #define VERBOSE_ES1370 */
31 #include "audio/audio.h"
35 SCTRL_P[12](END|ST)INC
45 Following macros and samplerate array were copied verbatim from
46 Linux kernel 2.4.30: drivers/sound/es1370.c
48 Copyright (C) 1998-2001, 2003 Thomas Sailer (t.sailer@alumni.ethz.ch)
51 /* Start blatant GPL violation */
53 #define ES1370_REG_CONTROL 0x00
54 #define ES1370_REG_STATUS 0x04
55 #define ES1370_REG_UART_DATA 0x08
56 #define ES1370_REG_UART_STATUS 0x09
57 #define ES1370_REG_UART_CONTROL 0x09
58 #define ES1370_REG_UART_TEST 0x0a
59 #define ES1370_REG_MEMPAGE 0x0c
60 #define ES1370_REG_CODEC 0x10
61 #define ES1370_REG_SERIAL_CONTROL 0x20
62 #define ES1370_REG_DAC1_SCOUNT 0x24
63 #define ES1370_REG_DAC2_SCOUNT 0x28
64 #define ES1370_REG_ADC_SCOUNT 0x2c
66 #define ES1370_REG_DAC1_FRAMEADR 0xc30
67 #define ES1370_REG_DAC1_FRAMECNT 0xc34
68 #define ES1370_REG_DAC2_FRAMEADR 0xc38
69 #define ES1370_REG_DAC2_FRAMECNT 0xc3c
70 #define ES1370_REG_ADC_FRAMEADR 0xd30
71 #define ES1370_REG_ADC_FRAMECNT 0xd34
72 #define ES1370_REG_PHANTOM_FRAMEADR 0xd38
73 #define ES1370_REG_PHANTOM_FRAMECNT 0xd3c
75 static const unsigned dac1_samplerate
[] = { 5512, 11025, 22050, 44100 };
77 #define DAC2_SRTODIV(x) (((1411200+(x)/2)/(x))-2)
78 #define DAC2_DIVTOSR(x) (1411200/((x)+2))
80 #define CTRL_ADC_STOP 0x80000000 /* 1 = ADC stopped */
81 #define CTRL_XCTL1 0x40000000 /* electret mic bias */
82 #define CTRL_OPEN 0x20000000 /* no function, can be read and written */
83 #define CTRL_PCLKDIV 0x1fff0000 /* ADC/DAC2 clock divider */
84 #define CTRL_SH_PCLKDIV 16
85 #define CTRL_MSFMTSEL 0x00008000 /* MPEG serial data fmt: 0 = Sony, 1 = I2S */
86 #define CTRL_M_SBB 0x00004000 /* DAC2 clock: 0 = PCLKDIV, 1 = MPEG */
87 #define CTRL_WTSRSEL 0x00003000 /* DAC1 clock freq: 0=5512, 1=11025, 2=22050, 3=44100 */
88 #define CTRL_SH_WTSRSEL 12
89 #define CTRL_DAC_SYNC 0x00000800 /* 1 = DAC2 runs off DAC1 clock */
90 #define CTRL_CCB_INTRM 0x00000400 /* 1 = CCB "voice" ints enabled */
91 #define CTRL_M_CB 0x00000200 /* recording source: 0 = ADC, 1 = MPEG */
92 #define CTRL_XCTL0 0x00000100 /* 0 = Line in, 1 = Line out */
93 #define CTRL_BREQ 0x00000080 /* 1 = test mode (internal mem test) */
94 #define CTRL_DAC1_EN 0x00000040 /* enable DAC1 */
95 #define CTRL_DAC2_EN 0x00000020 /* enable DAC2 */
96 #define CTRL_ADC_EN 0x00000010 /* enable ADC */
97 #define CTRL_UART_EN 0x00000008 /* enable MIDI uart */
98 #define CTRL_JYSTK_EN 0x00000004 /* enable Joystick port (presumably at address 0x200) */
99 #define CTRL_CDC_EN 0x00000002 /* enable serial (CODEC) interface */
100 #define CTRL_SERR_DIS 0x00000001 /* 1 = disable PCI SERR signal */
102 #define STAT_INTR 0x80000000 /* wired or of all interrupt bits */
103 #define STAT_CSTAT 0x00000400 /* 1 = codec busy or codec write in progress */
104 #define STAT_CBUSY 0x00000200 /* 1 = codec busy */
105 #define STAT_CWRIP 0x00000100 /* 1 = codec write in progress */
106 #define STAT_VC 0x00000060 /* CCB int source, 0=DAC1, 1=DAC2, 2=ADC, 3=undef */
108 #define STAT_MCCB 0x00000010 /* CCB int pending */
109 #define STAT_UART 0x00000008 /* UART int pending */
110 #define STAT_DAC1 0x00000004 /* DAC1 int pending */
111 #define STAT_DAC2 0x00000002 /* DAC2 int pending */
112 #define STAT_ADC 0x00000001 /* ADC int pending */
114 #define USTAT_RXINT 0x80 /* UART rx int pending */
115 #define USTAT_TXINT 0x04 /* UART tx int pending */
116 #define USTAT_TXRDY 0x02 /* UART tx ready */
117 #define USTAT_RXRDY 0x01 /* UART rx ready */
119 #define UCTRL_RXINTEN 0x80 /* 1 = enable RX ints */
120 #define UCTRL_TXINTEN 0x60 /* TX int enable field mask */
121 #define UCTRL_ENA_TXINT 0x20 /* enable TX int */
122 #define UCTRL_CNTRL 0x03 /* control field */
123 #define UCTRL_CNTRL_SWR 0x03 /* software reset command */
125 #define SCTRL_P2ENDINC 0x00380000 /* */
126 #define SCTRL_SH_P2ENDINC 19
127 #define SCTRL_P2STINC 0x00070000 /* */
128 #define SCTRL_SH_P2STINC 16
129 #define SCTRL_R1LOOPSEL 0x00008000 /* 0 = loop mode */
130 #define SCTRL_P2LOOPSEL 0x00004000 /* 0 = loop mode */
131 #define SCTRL_P1LOOPSEL 0x00002000 /* 0 = loop mode */
132 #define SCTRL_P2PAUSE 0x00001000 /* 1 = pause mode */
133 #define SCTRL_P1PAUSE 0x00000800 /* 1 = pause mode */
134 #define SCTRL_R1INTEN 0x00000400 /* enable interrupt */
135 #define SCTRL_P2INTEN 0x00000200 /* enable interrupt */
136 #define SCTRL_P1INTEN 0x00000100 /* enable interrupt */
137 #define SCTRL_P1SCTRLD 0x00000080 /* reload sample count register for DAC1 */
138 #define SCTRL_P2DACSEN 0x00000040 /* 1 = DAC2 play back last sample when disabled */
139 #define SCTRL_R1SEB 0x00000020 /* 1 = 16bit */
140 #define SCTRL_R1SMB 0x00000010 /* 1 = stereo */
141 #define SCTRL_R1FMT 0x00000030 /* format mask */
142 #define SCTRL_SH_R1FMT 4
143 #define SCTRL_P2SEB 0x00000008 /* 1 = 16bit */
144 #define SCTRL_P2SMB 0x00000004 /* 1 = stereo */
145 #define SCTRL_P2FMT 0x0000000c /* format mask */
146 #define SCTRL_SH_P2FMT 2
147 #define SCTRL_P1SEB 0x00000002 /* 1 = 16bit */
148 #define SCTRL_P1SMB 0x00000001 /* 1 = stereo */
149 #define SCTRL_P1FMT 0x00000003 /* format mask */
150 #define SCTRL_SH_P1FMT 0
152 /* End blatant GPL violation */
154 #define NB_CHANNELS 3
155 #define DAC1_CHANNEL 0
156 #define DAC2_CHANNEL 1
157 #define ADC_CHANNEL 2
159 #define IO_READ_PROTO(n) \
160 static uint32_t n (void *opaque, uint32_t addr)
161 #define IO_WRITE_PROTO(n) \
162 static void n (void *opaque, uint32_t addr, uint32_t val)
164 static void es1370_dac1_callback (void *opaque
, int free
);
165 static void es1370_dac2_callback (void *opaque
, int free
);
166 static void es1370_adc_callback (void *opaque
, int avail
);
170 #define ldebug(...) AUD_log ("es1370", __VA_ARGS__)
172 static void print_ctl (uint32_t val
)
177 #define a(n) if (val & CTRL_##n) strcat (buf, " "#n)
196 AUD_log ("es1370", "ctl - PCLKDIV %d(DAC2 freq %d), freq %d,%s\n",
197 (val
& CTRL_PCLKDIV
) >> CTRL_SH_PCLKDIV
,
198 DAC2_DIVTOSR ((val
& CTRL_PCLKDIV
) >> CTRL_SH_PCLKDIV
),
199 dac1_samplerate
[(val
& CTRL_WTSRSEL
) >> CTRL_SH_WTSRSEL
],
203 static void print_sctl (uint32_t val
)
205 static const char *fmt_names
[] = {"8M", "8S", "16M", "16S"};
210 #define a(n) if (val & SCTRL_##n) strcat (buf, " "#n)
211 #define b(n) if (!(val & SCTRL_##n)) strcat (buf, " "#n)
233 "p2_end_inc %d, p2_st_inc %d, r1_fmt %s, p2_fmt %s, p1_fmt %s\n",
235 (val
& SCTRL_P2ENDINC
) >> SCTRL_SH_P2ENDINC
,
236 (val
& SCTRL_P2STINC
) >> SCTRL_SH_P2STINC
,
237 fmt_names
[(val
>> SCTRL_SH_R1FMT
) & 3],
238 fmt_names
[(val
>> SCTRL_SH_P2FMT
) & 3],
239 fmt_names
[(val
>> SCTRL_SH_P1FMT
) & 3]
244 #define print_ctl(...)
245 #define print_sctl(...)
248 #ifdef VERBOSE_ES1370
249 #define dolog(...) AUD_log ("es1370", __VA_ARGS__)
254 #ifndef SILENT_ES1370
255 #define lwarn(...) AUD_log ("es1370: warning", __VA_ARGS__)
268 typedef struct ES1370State
{
272 struct chan chan
[NB_CHANNELS
];
273 SWVoiceOut
*dac_voice
[2];
274 SWVoiceIn
*adc_voice
;
289 uint32_t sctl_sh_fmt
;
290 uint32_t sctl_loopsel
;
291 void (*calc_freq
) (ES1370State
*s
, uint32_t ctl
,
292 uint32_t *old_freq
, uint32_t *new_freq
);
295 static void es1370_dac1_calc_freq (ES1370State
*s
, uint32_t ctl
,
296 uint32_t *old_freq
, uint32_t *new_freq
);
297 static void es1370_dac2_and_adc_calc_freq (ES1370State
*s
, uint32_t ctl
,
301 static const struct chan_bits es1370_chan_bits
[] = {
302 {CTRL_DAC1_EN
, STAT_DAC1
, SCTRL_P1PAUSE
, SCTRL_P1INTEN
,
303 SCTRL_P1FMT
, SCTRL_SH_P1FMT
, SCTRL_P1LOOPSEL
,
304 es1370_dac1_calc_freq
},
306 {CTRL_DAC2_EN
, STAT_DAC2
, SCTRL_P2PAUSE
, SCTRL_P2INTEN
,
307 SCTRL_P2FMT
, SCTRL_SH_P2FMT
, SCTRL_P2LOOPSEL
,
308 es1370_dac2_and_adc_calc_freq
},
310 {CTRL_ADC_EN
, STAT_ADC
, 0, SCTRL_R1INTEN
,
311 SCTRL_R1FMT
, SCTRL_SH_R1FMT
, SCTRL_R1LOOPSEL
,
312 es1370_dac2_and_adc_calc_freq
}
315 static void es1370_update_status (ES1370State
*s
, uint32_t new_status
)
317 uint32_t level
= new_status
& (STAT_DAC1
| STAT_DAC2
| STAT_ADC
);
320 s
->status
= new_status
| STAT_INTR
;
323 s
->status
= new_status
& ~STAT_INTR
;
325 qemu_set_irq (s
->dev
.irq
[0], !!level
);
328 static void es1370_reset (ES1370State
*s
)
338 for (i
= 0; i
< NB_CHANNELS
; ++i
) {
339 struct chan
*d
= &s
->chan
[i
];
342 if (i
== ADC_CHANNEL
) {
343 AUD_close_in (&s
->card
, s
->adc_voice
);
347 AUD_close_out (&s
->card
, s
->dac_voice
[i
]);
348 s
->dac_voice
[i
] = NULL
;
351 qemu_irq_lower (s
->dev
.irq
[0]);
354 static void es1370_maybe_lower_irq (ES1370State
*s
, uint32_t sctl
)
356 uint32_t new_status
= s
->status
;
358 if (!(sctl
& SCTRL_P1INTEN
) && (s
->sctl
& SCTRL_P1INTEN
)) {
359 new_status
&= ~STAT_DAC1
;
362 if (!(sctl
& SCTRL_P2INTEN
) && (s
->sctl
& SCTRL_P2INTEN
)) {
363 new_status
&= ~STAT_DAC2
;
366 if (!(sctl
& SCTRL_R1INTEN
) && (s
->sctl
& SCTRL_R1INTEN
)) {
367 new_status
&= ~STAT_ADC
;
370 if (new_status
!= s
->status
) {
371 es1370_update_status (s
, new_status
);
375 static void es1370_dac1_calc_freq (ES1370State
*s
, uint32_t ctl
,
376 uint32_t *old_freq
, uint32_t *new_freq
)
379 *old_freq
= dac1_samplerate
[(s
->ctl
& CTRL_WTSRSEL
) >> CTRL_SH_WTSRSEL
];
380 *new_freq
= dac1_samplerate
[(ctl
& CTRL_WTSRSEL
) >> CTRL_SH_WTSRSEL
];
383 static void es1370_dac2_and_adc_calc_freq (ES1370State
*s
, uint32_t ctl
,
388 uint32_t old_pclkdiv
, new_pclkdiv
;
390 new_pclkdiv
= (ctl
& CTRL_PCLKDIV
) >> CTRL_SH_PCLKDIV
;
391 old_pclkdiv
= (s
->ctl
& CTRL_PCLKDIV
) >> CTRL_SH_PCLKDIV
;
392 *new_freq
= DAC2_DIVTOSR (new_pclkdiv
);
393 *old_freq
= DAC2_DIVTOSR (old_pclkdiv
);
396 static void es1370_update_voices (ES1370State
*s
, uint32_t ctl
, uint32_t sctl
)
399 uint32_t old_freq
, new_freq
, old_fmt
, new_fmt
;
401 for (i
= 0; i
< NB_CHANNELS
; ++i
) {
402 struct chan
*d
= &s
->chan
[i
];
403 const struct chan_bits
*b
= &es1370_chan_bits
[i
];
405 new_fmt
= (sctl
& b
->sctl_fmt
) >> b
->sctl_sh_fmt
;
406 old_fmt
= (s
->sctl
& b
->sctl_fmt
) >> b
->sctl_sh_fmt
;
408 b
->calc_freq (s
, ctl
, &old_freq
, &new_freq
);
410 if ((old_fmt
!= new_fmt
) || (old_freq
!= new_freq
)) {
411 d
->shift
= (new_fmt
& 1) + (new_fmt
>> 1);
412 ldebug ("channel %d, freq = %d, nchannels %d, fmt %d, shift %d\n",
416 (new_fmt
& 2) ? AUD_FMT_S16
: AUD_FMT_U8
,
419 struct audsettings as
;
422 as
.nchannels
= 1 << (new_fmt
& 1);
423 as
.fmt
= (new_fmt
& 2) ? AUD_FMT_S16
: AUD_FMT_U8
;
426 if (i
== ADC_CHANNEL
) {
442 i
? "es1370.dac2" : "es1370.dac1",
444 i
? es1370_dac2_callback
: es1370_dac1_callback
,
451 if (((ctl
^ s
->ctl
) & b
->ctl_en
)
452 || ((sctl
^ s
->sctl
) & b
->sctl_pause
)) {
453 int on
= (ctl
& b
->ctl_en
) && !(sctl
& b
->sctl_pause
);
455 if (i
== ADC_CHANNEL
) {
456 AUD_set_active_in (s
->adc_voice
, on
);
459 AUD_set_active_out (s
->dac_voice
[i
], on
);
468 static inline uint32_t es1370_fixup (ES1370State
*s
, uint32_t addr
)
471 if (addr
>= 0x30 && addr
<= 0x3f)
472 addr
|= s
->mempage
<< 8;
476 IO_WRITE_PROTO (es1370_writeb
)
478 ES1370State
*s
= opaque
;
479 uint32_t shift
, mask
;
481 addr
= es1370_fixup (s
, addr
);
484 case ES1370_REG_CONTROL
:
485 case ES1370_REG_CONTROL
+ 1:
486 case ES1370_REG_CONTROL
+ 2:
487 case ES1370_REG_CONTROL
+ 3:
488 shift
= (addr
- ES1370_REG_CONTROL
) << 3;
489 mask
= 0xff << shift
;
490 val
= (s
->ctl
& ~mask
) | ((val
& 0xff) << shift
);
491 es1370_update_voices (s
, val
, s
->sctl
);
494 case ES1370_REG_MEMPAGE
:
497 case ES1370_REG_SERIAL_CONTROL
:
498 case ES1370_REG_SERIAL_CONTROL
+ 1:
499 case ES1370_REG_SERIAL_CONTROL
+ 2:
500 case ES1370_REG_SERIAL_CONTROL
+ 3:
501 shift
= (addr
- ES1370_REG_SERIAL_CONTROL
) << 3;
502 mask
= 0xff << shift
;
503 val
= (s
->sctl
& ~mask
) | ((val
& 0xff) << shift
);
504 es1370_maybe_lower_irq (s
, val
);
505 es1370_update_voices (s
, s
->ctl
, val
);
509 lwarn ("writeb %#x <- %#x\n", addr
, val
);
514 IO_WRITE_PROTO (es1370_writew
)
516 ES1370State
*s
= opaque
;
517 addr
= es1370_fixup (s
, addr
);
518 uint32_t shift
, mask
;
519 struct chan
*d
= &s
->chan
[0];
522 case ES1370_REG_CODEC
:
523 dolog ("ignored codec write address %#x, data %#x\n",
524 (val
>> 8) & 0xff, val
& 0xff);
528 case ES1370_REG_CONTROL
:
529 case ES1370_REG_CONTROL
+ 2:
530 shift
= (addr
!= ES1370_REG_CONTROL
) << 4;
531 mask
= 0xffff << shift
;
532 val
= (s
->ctl
& ~mask
) | ((val
& 0xffff) << shift
);
533 es1370_update_voices (s
, val
, s
->sctl
);
537 case ES1370_REG_ADC_SCOUNT
:
539 case ES1370_REG_DAC2_SCOUNT
:
541 case ES1370_REG_DAC1_SCOUNT
:
542 d
->scount
= (d
->scount
& ~0xffff) | (val
& 0xffff);
546 lwarn ("writew %#x <- %#x\n", addr
, val
);
551 IO_WRITE_PROTO (es1370_writel
)
553 ES1370State
*s
= opaque
;
554 struct chan
*d
= &s
->chan
[0];
556 addr
= es1370_fixup (s
, addr
);
559 case ES1370_REG_CONTROL
:
560 es1370_update_voices (s
, val
, s
->sctl
);
564 case ES1370_REG_MEMPAGE
:
565 s
->mempage
= val
& 0xf;
568 case ES1370_REG_SERIAL_CONTROL
:
569 es1370_maybe_lower_irq (s
, val
);
570 es1370_update_voices (s
, s
->ctl
, val
);
574 case ES1370_REG_ADC_SCOUNT
:
576 case ES1370_REG_DAC2_SCOUNT
:
578 case ES1370_REG_DAC1_SCOUNT
:
579 d
->scount
= (val
& 0xffff) | (d
->scount
& ~0xffff);
580 ldebug ("chan %d CURR_SAMP_CT %d, SAMP_CT %d\n",
581 d
- &s
->chan
[0], val
>> 16, (val
& 0xffff));
584 case ES1370_REG_ADC_FRAMEADR
:
586 case ES1370_REG_DAC2_FRAMEADR
:
588 case ES1370_REG_DAC1_FRAMEADR
:
590 ldebug ("chan %d frame address %#x\n", d
- &s
->chan
[0], val
);
593 case ES1370_REG_PHANTOM_FRAMECNT
:
594 lwarn ("writing to phantom frame count %#x\n", val
);
596 case ES1370_REG_PHANTOM_FRAMEADR
:
597 lwarn ("writing to phantom frame address %#x\n", val
);
600 case ES1370_REG_ADC_FRAMECNT
:
602 case ES1370_REG_DAC2_FRAMECNT
:
604 case ES1370_REG_DAC1_FRAMECNT
:
607 ldebug ("chan %d frame count %d, buffer size %d\n",
608 d
- &s
->chan
[0], val
>> 16, val
& 0xffff);
612 lwarn ("writel %#x <- %#x\n", addr
, val
);
617 IO_READ_PROTO (es1370_readb
)
619 ES1370State
*s
= opaque
;
622 addr
= es1370_fixup (s
, addr
);
625 case 0x1b: /* Legacy */
626 lwarn ("Attempt to read from legacy register\n");
629 case ES1370_REG_MEMPAGE
:
632 case ES1370_REG_CONTROL
+ 0:
633 case ES1370_REG_CONTROL
+ 1:
634 case ES1370_REG_CONTROL
+ 2:
635 case ES1370_REG_CONTROL
+ 3:
636 val
= s
->ctl
>> ((addr
- ES1370_REG_CONTROL
) << 3);
638 case ES1370_REG_STATUS
+ 0:
639 case ES1370_REG_STATUS
+ 1:
640 case ES1370_REG_STATUS
+ 2:
641 case ES1370_REG_STATUS
+ 3:
642 val
= s
->status
>> ((addr
- ES1370_REG_STATUS
) << 3);
646 lwarn ("readb %#x -> %#x\n", addr
, val
);
652 IO_READ_PROTO (es1370_readw
)
654 ES1370State
*s
= opaque
;
655 struct chan
*d
= &s
->chan
[0];
658 addr
= es1370_fixup (s
, addr
);
661 case ES1370_REG_ADC_SCOUNT
+ 2:
663 case ES1370_REG_DAC2_SCOUNT
+ 2:
665 case ES1370_REG_DAC1_SCOUNT
+ 2:
666 val
= d
->scount
>> 16;
669 case ES1370_REG_ADC_FRAMECNT
:
671 case ES1370_REG_DAC2_FRAMECNT
:
673 case ES1370_REG_DAC1_FRAMECNT
:
674 val
= d
->frame_cnt
& 0xffff;
677 case ES1370_REG_ADC_FRAMECNT
+ 2:
679 case ES1370_REG_DAC2_FRAMECNT
+ 2:
681 case ES1370_REG_DAC1_FRAMECNT
+ 2:
682 val
= d
->frame_cnt
>> 16;
687 lwarn ("readw %#x -> %#x\n", addr
, val
);
694 IO_READ_PROTO (es1370_readl
)
696 ES1370State
*s
= opaque
;
698 struct chan
*d
= &s
->chan
[0];
700 addr
= es1370_fixup (s
, addr
);
703 case ES1370_REG_CONTROL
:
706 case ES1370_REG_STATUS
:
709 case ES1370_REG_MEMPAGE
:
712 case ES1370_REG_CODEC
:
715 case ES1370_REG_SERIAL_CONTROL
:
719 case ES1370_REG_ADC_SCOUNT
:
721 case ES1370_REG_DAC2_SCOUNT
:
723 case ES1370_REG_DAC1_SCOUNT
:
727 uint32_t curr_count
= d
->scount
>> 16;
728 uint32_t count
= d
->scount
& 0xffff;
730 curr_count
<<= d
->shift
;
732 dolog ("read scount curr %d, total %d\n", curr_count
, count
);
737 case ES1370_REG_ADC_FRAMECNT
:
739 case ES1370_REG_DAC2_FRAMECNT
:
741 case ES1370_REG_DAC1_FRAMECNT
:
745 uint32_t size
= ((d
->frame_cnt
& 0xffff) + 1) << 2;
746 uint32_t curr
= ((d
->frame_cnt
>> 16) + 1) << 2;
748 dolog ("read framecnt curr %d, size %d %d\n", curr
, size
,
754 case ES1370_REG_ADC_FRAMEADR
:
756 case ES1370_REG_DAC2_FRAMEADR
:
758 case ES1370_REG_DAC1_FRAMEADR
:
762 case ES1370_REG_PHANTOM_FRAMECNT
:
764 lwarn ("reading from phantom frame count\n");
766 case ES1370_REG_PHANTOM_FRAMEADR
:
768 lwarn ("reading from phantom frame address\n");
773 lwarn ("readl %#x -> %#x\n", addr
, val
);
779 static void es1370_transfer_audio (ES1370State
*s
, struct chan
*d
, int loop_sel
,
782 uint8_t tmpbuf
[4096];
783 uint32_t addr
= d
->frame_addr
;
784 int sc
= d
->scount
& 0xffff;
785 int csc
= d
->scount
>> 16;
786 int csc_bytes
= (csc
+ 1) << d
->shift
;
787 int cnt
= d
->frame_cnt
>> 16;
788 int size
= d
->frame_cnt
& 0xffff;
789 int left
= ((size
- cnt
+ 1) << 2) + d
->leftover
;
791 int temp
= audio_MIN (max
, audio_MIN (left
, csc_bytes
));
792 int index
= d
- &s
->chan
[0];
794 addr
+= (cnt
<< 2) + d
->leftover
;
796 if (index
== ADC_CHANNEL
) {
798 int acquired
, to_copy
;
800 to_copy
= audio_MIN ((size_t) temp
, sizeof (tmpbuf
));
801 acquired
= AUD_read (s
->adc_voice
, tmpbuf
, to_copy
);
805 cpu_physical_memory_write (addr
, tmpbuf
, acquired
);
809 transfered
+= acquired
;
813 SWVoiceOut
*voice
= s
->dac_voice
[index
];
818 to_copy
= audio_MIN ((size_t) temp
, sizeof (tmpbuf
));
819 cpu_physical_memory_read (addr
, tmpbuf
, to_copy
);
820 copied
= AUD_write (voice
, tmpbuf
, to_copy
);
825 transfered
+= copied
;
829 if (csc_bytes
== transfered
) {
831 d
->scount
= sc
| (sc
<< 16);
832 ldebug ("sc = %d, rate = %f\n",
833 (sc
+ 1) << d
->shift
,
834 (sc
+ 1) / (double) 44100);
838 d
->scount
= sc
| (((csc_bytes
- transfered
- 1) >> d
->shift
) << 16);
841 cnt
+= (transfered
+ d
->leftover
) >> 2;
843 if (s
->sctl
& loop_sel
) {
844 /* Bah, how stupid is that having a 0 represent true value?
845 i just spent few hours on this shit */
846 AUD_log ("es1370: warning", "non looping mode\n");
851 if ((uint32_t) cnt
<= d
->frame_cnt
)
852 d
->frame_cnt
|= cnt
<< 16;
855 d
->leftover
= (transfered
+ d
->leftover
) & 3;
858 static void es1370_run_channel (ES1370State
*s
, size_t chan
, int free_or_avail
)
860 uint32_t new_status
= s
->status
;
862 struct chan
*d
= &s
->chan
[chan
];
863 const struct chan_bits
*b
= &es1370_chan_bits
[chan
];
865 if (!(s
->ctl
& b
->ctl_en
) || (s
->sctl
& b
->sctl_pause
)) {
869 max_bytes
= free_or_avail
;
870 max_bytes
&= ~((1 << d
->shift
) - 1);
875 es1370_transfer_audio (s
, d
, b
->sctl_loopsel
, max_bytes
, &irq
);
878 if (s
->sctl
& b
->sctl_inten
) {
879 new_status
|= b
->stat_int
;
883 if (new_status
!= s
->status
) {
884 es1370_update_status (s
, new_status
);
888 static void es1370_dac1_callback (void *opaque
, int free
)
890 ES1370State
*s
= opaque
;
892 es1370_run_channel (s
, DAC1_CHANNEL
, free
);
895 static void es1370_dac2_callback (void *opaque
, int free
)
897 ES1370State
*s
= opaque
;
899 es1370_run_channel (s
, DAC2_CHANNEL
, free
);
902 static void es1370_adc_callback (void *opaque
, int avail
)
904 ES1370State
*s
= opaque
;
906 es1370_run_channel (s
, ADC_CHANNEL
, avail
);
909 static const MemoryRegionPortio es1370_portio
[] = {
910 { 0, 0x40 * 4, 1, .write
= es1370_writeb
, },
911 { 0, 0x40 * 2, 2, .write
= es1370_writew
, },
912 { 0, 0x40, 4, .write
= es1370_writel
, },
913 { 0, 0x40 * 4, 1, .read
= es1370_readb
, },
914 { 0, 0x40 * 2, 2, .read
= es1370_readw
, },
915 { 0, 0x40, 4, .read
= es1370_readl
, },
919 static const MemoryRegionOps es1370_io_ops
= {
920 .old_portio
= es1370_portio
,
921 .endianness
= DEVICE_LITTLE_ENDIAN
,
924 static const VMStateDescription vmstate_es1370_channel
= {
925 .name
= "es1370_channel",
927 .minimum_version_id
= 2,
928 .minimum_version_id_old
= 2,
929 .fields
= (VMStateField
[]) {
930 VMSTATE_UINT32(shift
, struct chan
),
931 VMSTATE_UINT32(leftover
, struct chan
),
932 VMSTATE_UINT32(scount
, struct chan
),
933 VMSTATE_UINT32(frame_addr
, struct chan
),
934 VMSTATE_UINT32(frame_cnt
, struct chan
),
935 VMSTATE_END_OF_LIST()
939 static int es1370_post_load (void *opaque
, int version_id
)
942 ES1370State
*s
= opaque
;
945 for (i
= 0; i
< NB_CHANNELS
; ++i
) {
946 if (i
== ADC_CHANNEL
) {
948 AUD_close_in (&s
->card
, s
->adc_voice
);
953 if (s
->dac_voice
[i
]) {
954 AUD_close_out (&s
->card
, s
->dac_voice
[i
]);
955 s
->dac_voice
[i
] = NULL
;
964 es1370_update_voices (s
, ctl
, sctl
);
968 static const VMStateDescription vmstate_es1370
= {
971 .minimum_version_id
= 2,
972 .minimum_version_id_old
= 2,
973 .post_load
= es1370_post_load
,
974 .fields
= (VMStateField
[]) {
975 VMSTATE_PCI_DEVICE(dev
, ES1370State
),
976 VMSTATE_STRUCT_ARRAY(chan
, ES1370State
, NB_CHANNELS
, 2,
977 vmstate_es1370_channel
, struct chan
),
978 VMSTATE_UINT32(ctl
, ES1370State
),
979 VMSTATE_UINT32(status
, ES1370State
),
980 VMSTATE_UINT32(mempage
, ES1370State
),
981 VMSTATE_UINT32(codec
, ES1370State
),
982 VMSTATE_UINT32(sctl
, ES1370State
),
983 VMSTATE_END_OF_LIST()
987 static void es1370_on_reset (void *opaque
)
989 ES1370State
*s
= opaque
;
993 static int es1370_initfn (PCIDevice
*dev
)
995 ES1370State
*s
= DO_UPCAST (ES1370State
, dev
, dev
);
996 uint8_t *c
= s
->dev
.config
;
998 c
[PCI_STATUS
+ 1] = PCI_STATUS_DEVSEL_SLOW
>> 8;
1001 c
[PCI_CAPABILITY_LIST
] = 0xdc;
1002 c
[PCI_INTERRUPT_LINE
] = 10;
1006 c
[PCI_INTERRUPT_PIN
] = 1;
1007 c
[PCI_MIN_GNT
] = 0x0c;
1008 c
[PCI_MAX_LAT
] = 0x80;
1010 memory_region_init_io (&s
->io
, &es1370_io_ops
, s
, "es1370", 256);
1011 pci_register_bar (&s
->dev
, 0, PCI_BASE_ADDRESS_SPACE_IO
, &s
->io
);
1012 qemu_register_reset (es1370_on_reset
, s
);
1014 AUD_register_card ("es1370", &s
->card
);
1019 static int es1370_exitfn(PCIDevice
*dev
)
1021 ES1370State
*s
= DO_UPCAST (ES1370State
, dev
, dev
);
1023 memory_region_destroy (&s
->io
);
1027 int es1370_init (PCIBus
*bus
)
1029 pci_create_simple (bus
, -1, "ES1370");
1033 static PCIDeviceInfo es1370_info
= {
1034 .qdev
.name
= "ES1370",
1035 .qdev
.desc
= "ENSONIQ AudioPCI ES1370",
1036 .qdev
.size
= sizeof (ES1370State
),
1037 .qdev
.vmsd
= &vmstate_es1370
,
1038 .init
= es1370_initfn
,
1039 .exit
= es1370_exitfn
,
1040 .vendor_id
= PCI_VENDOR_ID_ENSONIQ
,
1041 .device_id
= PCI_DEVICE_ID_ENSONIQ_ES1370
,
1042 .class_id
= PCI_CLASS_MULTIMEDIA_AUDIO
,
1044 .subsystem_vendor_id
= 0x4942,
1045 .subsystem_id
= 0x4c4c,
1047 .subsystem_vendor_id
= 0x1274,
1048 .subsystem_id
= 0x1371,
1052 static void es1370_register (void)
1054 pci_qdev_register (&es1370_info
);
1056 device_init (es1370_register
);