1 /* Best viewed with tabsize 4 */
4 * November 2005 ES1371 driver (Laurens Bronwasser)
7 /* Ensoniq ES1371 driver
11 * This is the main file of the ES1371 sound driver. There is no main function
12 * over here, instead the main function is located in the generic dma driver.
13 * All this driver does is implement the interface audio/audio_fw.h. All
14 * functions having the prefix 'drv_' are dictated by audio/audio_fw.h. The
15 * function prototypes you see below define a set of private helper functions.
16 * Control over the AC97 codec is delegated AC97.c.
18 * October 2007 ES1371 driver (Pieter Hijma),
19 * based on ES1370 driver which is based on the ES1371 driver
20 * by Laurens Bronwasser
23 #include <machine/pci.h>
25 #include <minix/audio_fw.h>
28 #include "sample_rate_converter.h"
29 #include "pci_helper.h"
32 /* reg(n) will be the device specific addresses */
33 #define reg(n) dev.base + n
36 /* prototypes of private functions */
37 static int detect_hw(void);
38 static int disable_int(int sub_dev
);
39 static int set_stereo(u32_t stereo
, int sub_dev
);
40 static int set_bits(u32_t nr_of_bits
, int sub_dev
);
41 static int set_sample_rate(u32_t rate
, int sub_dev
);
42 static int set_sign(u32_t val
, int sub_dev
);
43 static int get_max_frag_size(u32_t
* val
, int *len
, int sub_dev
);
44 static int set_frag_size(u32_t fragment_size
, int sub_dev
);
45 static int set_int_cnt(int sub_dev
);
46 static int free_buf(u32_t
*val
, int *len
, int sub_dev
);
47 static int get_samples_in_buf(u32_t
*val
, int *len
, int sub_dev
);
48 static int get_set_volume(struct volume_level
*level
, int *len
, int
50 static int reset(int sub_dev
);
54 aud_sub_dev_conf_t aud_conf
[4];
58 special_file_t special_file
[4];
63 drv
.DriverName
= DRIVER_NAME
;
64 drv
.NrOfSubDevices
= 4;
65 drv
.NrOfSpecialFiles
= 4;
67 sub_dev
[DAC1_CHAN
].readable
= 0;
68 sub_dev
[DAC1_CHAN
].writable
= 1;
69 sub_dev
[DAC1_CHAN
].DmaSize
= 64 * 1024;
70 sub_dev
[DAC1_CHAN
].NrOfDmaFragments
= 2;
71 sub_dev
[DAC1_CHAN
].MinFragmentSize
= 1024;
72 sub_dev
[DAC1_CHAN
].NrOfExtraBuffers
= 4;
74 sub_dev
[ADC1_CHAN
].readable
= 1;
75 sub_dev
[ADC1_CHAN
].writable
= 0;
76 sub_dev
[ADC1_CHAN
].DmaSize
= 64 * 1024;
77 sub_dev
[ADC1_CHAN
].NrOfDmaFragments
= 2;
78 sub_dev
[ADC1_CHAN
].MinFragmentSize
= 1024;
79 sub_dev
[ADC1_CHAN
].NrOfExtraBuffers
= 4;
81 sub_dev
[MIXER
].writable
= 0;
82 sub_dev
[MIXER
].readable
= 0;
84 sub_dev
[DAC2_CHAN
].readable
= 0;
85 sub_dev
[DAC2_CHAN
].writable
= 1;
86 sub_dev
[DAC2_CHAN
].DmaSize
= 64 * 1024;
87 sub_dev
[DAC2_CHAN
].NrOfDmaFragments
= 2;
88 sub_dev
[DAC2_CHAN
].MinFragmentSize
= 1024;
89 sub_dev
[DAC2_CHAN
].NrOfExtraBuffers
= 4;
91 special_file
[0].minor_dev_nr
= 0;
92 special_file
[0].write_chan
= DAC1_CHAN
;
93 special_file
[0].read_chan
= NO_CHANNEL
;
94 special_file
[0].io_ctl
= DAC1_CHAN
;
96 special_file
[1].minor_dev_nr
= 1;
97 special_file
[1].write_chan
= NO_CHANNEL
;
98 special_file
[1].read_chan
= ADC1_CHAN
;
99 special_file
[1].io_ctl
= ADC1_CHAN
;
101 special_file
[2].minor_dev_nr
= 2;
102 special_file
[2].write_chan
= NO_CHANNEL
;
103 special_file
[2].read_chan
= NO_CHANNEL
;
104 special_file
[2].io_ctl
= MIXER
;
106 special_file
[3].minor_dev_nr
= 3;
107 special_file
[3].write_chan
= DAC2_CHAN
;
108 special_file
[3].read_chan
= NO_CHANNEL
;
109 special_file
[3].io_ctl
= DAC2_CHAN
;
115 int drv_init_hw (void) {
118 /* First, detect the hardware */
119 if (detect_hw() != OK
) {
123 /* PCI command register
124 * enable the SERR# driver, PCI bus mastering and I/O access
126 pci_attr_w16 (dev
.devind
, PCI_CR
, SERR_EN
|PCI_MASTER
|IO_ACCESS
);
128 /* turn everything off */
129 pci_outl(reg(CHIP_SEL_CTRL
), 0x0UL
);
131 /* turn off legacy (legacy control is undocumented) */
132 pci_outl(reg(LEGACY
), 0x0UL
);
133 pci_outl(reg(LEGACY
+4), 0x0UL
);
135 /* turn off serial interface */
136 pci_outl(reg(SERIAL_INTERFACE_CTRL
), 0x0UL
);
137 /*pci_outl(reg(SERIAL_INTERFACE_CTRL), 0x3UL);*/
140 /* clear all the memory */
141 for (i
= 0; i
< 0x10; ++i
) {
142 pci_outb(reg(MEM_PAGE
), i
);
143 for (j
= 0; j
< 0x10; j
+= 4) {
144 pci_outl (reg(MEMORY
) + j
, 0x0UL
);
148 /* Sample Rate Converter initialization */
149 if (src_init(&dev
) != OK
) {
152 if (AC97_init(&dev
) != OK
) {
156 /* initialize variables for each sub_device */
157 for (i
= 0; i
< drv
.NrOfSubDevices
; i
++) {
159 aud_conf
[i
].busy
= 0;
160 aud_conf
[i
].stereo
= DEFAULT_STEREO
;
161 aud_conf
[i
].sample_rate
= DEFAULT_RATE
;
162 aud_conf
[i
].nr_of_bits
= DEFAULT_NR_OF_BITS
;
163 aud_conf
[i
].sign
= DEFAULT_SIGNED
;
164 aud_conf
[i
].fragment_size
=
165 sub_dev
[i
].DmaSize
/ sub_dev
[i
].NrOfDmaFragments
;
172 static int detect_hw(void) {
177 /* detect_hw tries to find device and get IRQ and base address
178 with a little (much) help from the PCI library.
179 This code is quite device independent and you can copy it.
180 (just make sure to get the bugs out first)*/
183 /* get first device and then search through the list */
184 device
= pci_first_dev(&devind
, &v_id
, &d_id
);
185 while( device
> 0 ) {
186 /* if we have a match...break */
187 if (v_id
== VENDOR_ID
&& d_id
== DEVICE_ID
) break;
188 device
= pci_next_dev(&devind
, &v_id
, &d_id
);
191 /* did we find anything? */
192 if (v_id
!= VENDOR_ID
|| d_id
!= DEVICE_ID
) {
198 dev
.name
= pci_dev_name(v_id
, d_id
);
200 /* get base address of our device, ignore least signif. bit
201 this last bit thing could be device dependent, i don't know */
202 dev
.base
= pci_attr_r32(devind
, PCI_BAR
) & 0xfffffffe;
205 dev
.irq
= pci_attr_r8(devind
, PCI_ILR
);
206 dev
.revision
= pci_attr_r8(devind
, PCI_REV
);
209 dev
.devind
= devind
; /* pci device identifier */
215 static int reset(int chan
) {
217 sub_dev
[chan
].OutOfData
= 1;
228 int drv_start(int sub_dev
, int UNUSED(DmaMode
)) {
229 u32_t enable_bit
, result
= 0;
231 /* Write default values to device in case user failed to configure.
232 If user did configure properly, everything is written twice.
233 please raise your hand if you object against to this strategy...*/
234 result
|= set_sample_rate(aud_conf
[sub_dev
].sample_rate
, sub_dev
);
235 result
|= set_stereo(aud_conf
[sub_dev
].stereo
, sub_dev
);
236 result
|= set_bits(aud_conf
[sub_dev
].nr_of_bits
, sub_dev
);
237 result
|= set_sign(aud_conf
[sub_dev
].sign
, sub_dev
);
239 /* set the interrupt count */
240 result
|= set_int_cnt(sub_dev
);
246 /* if device currently paused, resume */
250 case ADC1_CHAN
: enable_bit
= ADC1_EN
;break;
251 case DAC1_CHAN
: enable_bit
= DAC1_EN
;break;
252 case DAC2_CHAN
: enable_bit
= DAC2_EN
;break;
253 default: return EINVAL
;
256 /* enable interrupts from 'sub device' */
257 drv_reenable_int(sub_dev
);
259 /* this means play!!! */
260 pci_outw(reg(CHIP_SEL_CTRL
), pci_inw(reg(CHIP_SEL_CTRL
)) | enable_bit
);
262 aud_conf
[sub_dev
].busy
= 1;
268 int drv_stop(int sub_dev
)
273 case ADC1_CHAN
: enable_bit
= ADC1_EN
;break;
274 case DAC1_CHAN
: enable_bit
= DAC1_EN
;break;
275 case DAC2_CHAN
: enable_bit
= DAC2_EN
;break;
276 default: return EINVAL
;
279 /* stop the specified channel */
280 pci_outw(reg(CHIP_SEL_CTRL
),
281 pci_inw(reg(CHIP_SEL_CTRL
)) & ~enable_bit
);
282 aud_conf
[sub_dev
].busy
= 0;
283 disable_int(sub_dev
);
289 /* all IO-ctl's sent to the upper driver are passed to this function */
290 int drv_io_ctl(int request
, void * val
, int * len
, int sub_dev
) {
296 status
= set_sample_rate(*((u32_t
*) val
), sub_dev
); break;
298 status
= set_stereo(*((u32_t
*) val
), sub_dev
); break;
300 status
= set_bits(*((u32_t
*) val
), sub_dev
); break;
302 status
= set_frag_size(*((u32_t
*) val
), sub_dev
); break;
304 status
= set_sign(*((u32_t
*) val
), sub_dev
); break;
306 status
= get_max_frag_size(val
, len
, sub_dev
); break;
308 status
= reset(sub_dev
); break;
310 status
= free_buf(val
, len
, sub_dev
); break;
311 case DSPIOSAMPLESINBUF
:
312 status
= get_samples_in_buf(val
, len
, sub_dev
); break;
314 status
= drv_pause(sub_dev
); break;
316 status
= drv_resume(sub_dev
); break;
318 status
= get_set_volume(val
, len
, sub_dev
, 0); break;
320 status
= get_set_volume(val
, len
, sub_dev
, 1); break;
322 status
= EINVAL
; break;
329 int drv_get_irq(char *irq
) {
335 int drv_get_frag_size(u32_t
*frag_size
, int sub_dev
) {
336 *frag_size
= aud_conf
[sub_dev
].fragment_size
;
341 int drv_set_dma(u32_t dma
, u32_t length
, int chan
) {
342 /* dma length in bytes,
343 max is 64k long words for es1371 = 256k bytes */
344 u32_t page
, frame_count_reg
, dma_add_reg
;
347 case ADC1_CHAN
: page
= ADC_MEM_PAGE
;
348 frame_count_reg
= ADC_BUFFER_SIZE
;
349 dma_add_reg
= ADC_PCI_ADDRESS
;
351 case DAC1_CHAN
: page
= DAC_MEM_PAGE
;
352 frame_count_reg
= DAC1_BUFFER_SIZE
;
353 dma_add_reg
= DAC1_PCI_ADDRESS
;
355 case DAC2_CHAN
: page
= DAC_MEM_PAGE
;
356 frame_count_reg
= DAC2_BUFFER_SIZE
;
357 dma_add_reg
= DAC2_PCI_ADDRESS
;
361 pci_outb(reg(MEM_PAGE
), page
);
362 pci_outl(reg(dma_add_reg
), dma
);
364 /* device expects long word count in stead of bytes */
367 /* It seems that register _CURRENT_COUNT is overwritten, but this is
368 * the way to go. The register frame_count_reg is only longword
370 * It expects length -1
372 pci_outl(reg(frame_count_reg
), (u32_t
) (length
- 1));
378 /* return status of the interrupt summary bit */
379 int drv_int_sum(void) {
380 return pci_inl(reg(INTERRUPT_STATUS
)) & INTR
;
384 int drv_int(int sub_dev
) {
388 /* return status of interrupt bit of specified channel*/
390 case DAC1_CHAN
: bit
= DAC1
;break;
391 case DAC2_CHAN
: bit
= DAC2
;break;
392 case ADC1_CHAN
: bit
= ADC
;break;
393 default: return EINVAL
;
396 int_status
= pci_inl(reg(INTERRUPT_STATUS
)) & bit
;
402 int drv_reenable_int(int chan
) {
403 u16_t ser_interface
, int_en_bit
;
406 case ADC1_CHAN
: int_en_bit
= R1_INT_EN
; break;
407 case DAC1_CHAN
: int_en_bit
= P1_INTR_EN
; break;
408 case DAC2_CHAN
: int_en_bit
= P2_INTR_EN
; break;
409 default: return EINVAL
;
412 /* clear and reenable an interrupt */
413 ser_interface
= pci_inw(reg(SERIAL_INTERFACE_CTRL
));
414 pci_outw(reg(SERIAL_INTERFACE_CTRL
), ser_interface
& ~int_en_bit
);
415 pci_outw(reg(SERIAL_INTERFACE_CTRL
), ser_interface
| int_en_bit
);
421 int drv_pause(int sub_dev
) {
424 disable_int(sub_dev
); /* don't send interrupts */
427 case DAC1_CHAN
: pause_bit
= P1_PAUSE
;break;
428 case DAC2_CHAN
: pause_bit
= P2_PAUSE
;break;
429 default: return EINVAL
;
433 pci_outl(reg(SERIAL_INTERFACE_CTRL
),
434 pci_inl(reg(SERIAL_INTERFACE_CTRL
)) | pause_bit
);
440 int drv_resume(int sub_dev
) {
443 drv_reenable_int(sub_dev
); /* enable interrupts */
446 case DAC1_CHAN
: pause_bit
= P1_PAUSE
;break;
447 case DAC2_CHAN
: pause_bit
= P2_PAUSE
;break;
448 default: return EINVAL
;
451 /* clear pause bit */
452 pci_outl(reg(SERIAL_INTERFACE_CTRL
),
453 pci_inl(reg(SERIAL_INTERFACE_CTRL
)) & ~pause_bit
);
459 static int set_bits(u32_t nr_of_bits
, int sub_dev
) {
460 /* set format bits for specified channel. */
461 u16_t size_16_bit
, ser_interface
;
464 case ADC1_CHAN
: size_16_bit
= R1_S_EB
; break;
465 case DAC1_CHAN
: size_16_bit
= P1_S_EB
; break;
466 case DAC2_CHAN
: size_16_bit
= P2_S_EB
; break;
467 default: return EINVAL
;
470 ser_interface
= pci_inw(reg(SERIAL_INTERFACE_CTRL
));
471 ser_interface
&= ~size_16_bit
;
473 case 16: ser_interface
|= size_16_bit
;break;
475 default: return EINVAL
;
477 pci_outw(reg(SERIAL_INTERFACE_CTRL
), ser_interface
);
478 aud_conf
[sub_dev
].nr_of_bits
= nr_of_bits
;
483 static int set_stereo(u32_t stereo
, int sub_dev
) {
484 /* set format bits for specified channel. */
485 u16_t stereo_bit
, ser_interface
;
488 case ADC1_CHAN
: stereo_bit
= R1_S_MB
; break;
489 case DAC1_CHAN
: stereo_bit
= P1_S_MB
; break;
490 case DAC2_CHAN
: stereo_bit
= P2_S_MB
; break;
491 default: return EINVAL
;
493 ser_interface
= pci_inw(reg(SERIAL_INTERFACE_CTRL
));
494 ser_interface
&= ~stereo_bit
;
496 ser_interface
|= stereo_bit
;
498 pci_outw(reg(SERIAL_INTERFACE_CTRL
), ser_interface
);
499 aud_conf
[sub_dev
].stereo
= stereo
;
505 static int set_sign(u32_t
UNUSED(val
), int UNUSED(sub_dev
)) {
510 static int set_frag_size(u32_t fragment_size
, int sub_dev_nr
) {
511 if (fragment_size
> (sub_dev
[sub_dev_nr
].DmaSize
/
512 sub_dev
[sub_dev_nr
].NrOfDmaFragments
) ||
513 fragment_size
< sub_dev
[sub_dev_nr
].MinFragmentSize
) {
516 aud_conf
[sub_dev_nr
].fragment_size
= fragment_size
;
522 static int set_sample_rate(u32_t rate
, int sub_dev
) {
525 if (rate
> MAX_RATE
|| rate
< MIN_RATE
) {
528 /* set the sample rate for the specified channel*/
530 case ADC1_CHAN
: src_base_reg
= SRC_ADC_BASE
;break;
531 case DAC1_CHAN
: src_base_reg
= SRC_SYNTH_BASE
;break;
532 case DAC2_CHAN
: src_base_reg
= SRC_DAC_BASE
;break;
533 default: return EINVAL
;
535 src_set_rate(&dev
, src_base_reg
, rate
);
536 aud_conf
[sub_dev
].sample_rate
= rate
;
541 static int set_int_cnt(int chan
) {
542 /* Write interrupt count for specified channel.
543 After <DspFragmentSize> bytes, an interrupt will be generated */
548 if (aud_conf
[chan
].fragment_size
>
549 (sub_dev
[chan
].DmaSize
/ sub_dev
[chan
].NrOfDmaFragments
)
550 || aud_conf
[chan
].fragment_size
< sub_dev
[chan
].MinFragmentSize
) {
555 case ADC1_CHAN
: int_cnt_reg
= ADC_SAMP_CT
; break;
556 case DAC1_CHAN
: int_cnt_reg
= DAC1_SAMP_CT
; break;
557 case DAC2_CHAN
: int_cnt_reg
= DAC2_SAMP_CT
; break;
558 default: return EINVAL
;
561 sample_count
= aud_conf
[chan
].fragment_size
;
563 /* adjust sample count according to sample format */
564 if( aud_conf
[chan
].stereo
== TRUE
) sample_count
>>= 1;
565 switch(aud_conf
[chan
].nr_of_bits
) {
566 case 16: sample_count
>>= 1;break;
568 default: return EINVAL
;
571 /* set the sample count - 1 for the specified channel. */
572 pci_outw(reg(int_cnt_reg
), sample_count
- 1);
578 static int get_max_frag_size(u32_t
* val
, int * len
, int sub_dev_nr
) {
580 *val
= (sub_dev
[sub_dev_nr
].DmaSize
/
581 sub_dev
[sub_dev_nr
].NrOfDmaFragments
);
586 static int disable_int(int chan
) {
587 u16_t ser_interface
, int_en_bit
;
590 case ADC1_CHAN
: int_en_bit
= R1_INT_EN
; break;
591 case DAC1_CHAN
: int_en_bit
= P1_INTR_EN
; break;
592 case DAC2_CHAN
: int_en_bit
= P2_INTR_EN
; break;
593 default: return EINVAL
;
595 /* clear the interrupt */
596 ser_interface
= pci_inw(reg(SERIAL_INTERFACE_CTRL
));
597 pci_outw(reg(SERIAL_INTERFACE_CTRL
), ser_interface
& ~int_en_bit
);
602 static int get_samples_in_buf (u32_t
*samples_in_buf
, int *len
, int chan
) {
604 u16_t curr_samp_ct_reg
;
605 u16_t curr_samp_ct
; /* counts back from SAMP_CT till 0 */
607 *len
= sizeof(*samples_in_buf
);
611 curr_samp_ct_reg
= ADC_CURR_SAMP_CT
;
612 samp_ct_reg
= ADC_SAMP_CT
; break;
614 curr_samp_ct_reg
= DAC1_CURR_SAMP_CT
;
615 samp_ct_reg
= DAC1_SAMP_CT
; break;
617 curr_samp_ct_reg
= DAC2_CURR_SAMP_CT
;
618 samp_ct_reg
= DAC2_SAMP_CT
; break;
619 default: return EINVAL
;
622 /* TODO: is this statement useful? */
623 (void) pci_inw(reg(samp_ct_reg
));
624 curr_samp_ct
= pci_inw(reg(curr_samp_ct_reg
));
626 *samples_in_buf
= (u32_t
) (sub_dev
[chan
].BufLength
* 8192) +
633 /* returns 1 if there are free buffers */
634 static int free_buf (u32_t
*val
, int *len
, int sub_dev_nr
) {
636 if (sub_dev
[sub_dev_nr
].BufLength
==
637 sub_dev
[sub_dev_nr
].NrOfExtraBuffers
) {
647 static int get_set_volume(struct volume_level
*level
, int *len
, int sub_dev
,
649 *len
= sizeof(struct volume_level
);
650 if (sub_dev
== MIXER
) {
651 return AC97_get_set_volume(level
, flag
);