cs4280.c revision 1.26.4.1 1 /* $NetBSD: cs4280.c,v 1.26.4.1 2004/09/22 20:58:18 jmc Exp $ */
2
3 /*
4 * Copyright (c) 1999, 2000 Tatoku Ogaito. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by Tatoku Ogaito
17 * for the NetBSD Project.
18 * 4. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 /*
34 * Cirrus Logic CS4280 (and maybe CS461x) driver.
35 * Data sheets can be found
36 * http://www.cirrus.com/ftp/pubs/4280.pdf
37 * http://www.cirrus.com/ftp/pubs/4297.pdf
38 * ftp://ftp.alsa-project.org/pub/manuals/cirrus/embedded_audio_spec.pdf
39 * ftp://ftp.alsa-project.org/pub/manuals/cirrus/embedded_audio_spec.doc
40 *
41 * Note: CS4610/CS4611 + CS423x ISA codec should be worked with
42 * wss* at pnpbios?
43 * or
44 * sb* at pnpbios?
45 * Since I could not find any documents on handling ISA codec,
46 * clcs does not support those chips.
47 */
48
49 /*
50 * TODO
51 * Joystick support
52 */
53
54 #include <sys/cdefs.h>
55 __KERNEL_RCSID(0, "$NetBSD: cs4280.c,v 1.26.4.1 2004/09/22 20:58:18 jmc Exp $");
56
57 #include "midi.h"
58
59 #include <sys/param.h>
60 #include <sys/systm.h>
61 #include <sys/kernel.h>
62 #include <sys/fcntl.h>
63 #include <sys/malloc.h>
64 #include <sys/device.h>
65 #include <sys/proc.h>
66 #include <sys/systm.h>
67
68 #include <dev/pci/pcidevs.h>
69 #include <dev/pci/pcivar.h>
70 #include <dev/pci/cs4280reg.h>
71 #include <dev/pci/cs4280_image.h>
72 #include <dev/pci/cs428xreg.h>
73
74 #include <sys/audioio.h>
75 #include <dev/audio_if.h>
76 #include <dev/midi_if.h>
77 #include <dev/mulaw.h>
78 #include <dev/auconv.h>
79
80 #include <dev/ic/ac97reg.h>
81 #include <dev/ic/ac97var.h>
82
83 #include <dev/pci/cs428x.h>
84
85 #include <machine/bus.h>
86 #include <machine/bswap.h>
87
88 #define BA1READ4(sc, r) bus_space_read_4((sc)->ba1t, (sc)->ba1h, (r))
89 #define BA1WRITE4(sc, r, x) bus_space_write_4((sc)->ba1t, (sc)->ba1h, (r), (x))
90
91 /* IF functions for audio driver */
92 int cs4280_match(struct device *, struct cfdata *, void *);
93 void cs4280_attach(struct device *, struct device *, void *);
94 int cs4280_intr(void *);
95 int cs4280_query_encoding(void *, struct audio_encoding *);
96 int cs4280_set_params(void *, int, int, struct audio_params *, struct audio_params *);
97 int cs4280_halt_output(void *);
98 int cs4280_halt_input(void *);
99 int cs4280_getdev(void *, struct audio_device *);
100 int cs4280_trigger_output(void *, void *, void *, int, void (*)(void *),
101 void *, struct audio_params *);
102 int cs4280_trigger_input(void *, void *, void *, int, void (*)(void *),
103 void *, struct audio_params *);
104
105 int cs4280_reset_codec(void *);
106
107 /* For PowerHook */
108 void cs4280_power(int, void *);
109
110 /* Internal functions */
111 void cs4280_set_adc_rate(struct cs428x_softc *, int );
112 void cs4280_set_dac_rate(struct cs428x_softc *, int );
113 int cs4280_download(struct cs428x_softc *, const u_int32_t *, u_int32_t, u_int32_t);
114 int cs4280_download_image(struct cs428x_softc *);
115 void cs4280_reset(void *);
116 int cs4280_get_portnum_by_name(struct cs428x_softc *, char *, char *, char *);
117 int cs4280_init(struct cs428x_softc *, int);
118 void cs4280_clear_fifos(struct cs428x_softc *);
119
120 #if CS4280_DEBUG > 10
121 /* Thease two function is only for checking image loading is succeeded or not. */
122 int cs4280_check_images(struct cs428x_softc *);
123 int cs4280_checkimage(struct cs428x_softc *, u_int32_t *, u_int32_t, u_int32_t);
124 #endif
125
126 struct audio_hw_if cs4280_hw_if = {
127 cs428x_open,
128 cs428x_close,
129 NULL,
130 cs4280_query_encoding,
131 cs4280_set_params,
132 cs428x_round_blocksize,
133 NULL,
134 NULL,
135 NULL,
136 NULL,
137 NULL,
138 cs4280_halt_output,
139 cs4280_halt_input,
140 NULL,
141 cs4280_getdev,
142 NULL,
143 cs428x_mixer_set_port,
144 cs428x_mixer_get_port,
145 cs428x_query_devinfo,
146 cs428x_malloc,
147 cs428x_free,
148 cs428x_round_buffersize,
149 cs428x_mappage,
150 cs428x_get_props,
151 cs4280_trigger_output,
152 cs4280_trigger_input,
153 NULL,
154 };
155
156 #if NMIDI > 0
157 /* Midi Interface */
158 int cs4280_midi_open(void *, int, void (*)(void *, int),
159 void (*)(void *), void *);
160 void cs4280_midi_close(void*);
161 int cs4280_midi_output(void *, int);
162 void cs4280_midi_getinfo(void *, struct midi_info *);
163
164 struct midi_hw_if cs4280_midi_hw_if = {
165 cs4280_midi_open,
166 cs4280_midi_close,
167 cs4280_midi_output,
168 cs4280_midi_getinfo,
169 0,
170 };
171 #endif
172
173 CFATTACH_DECL(clcs, sizeof(struct cs428x_softc),
174 cs4280_match, cs4280_attach, NULL, NULL);
175
176 struct audio_device cs4280_device = {
177 "CS4280",
178 "",
179 "cs4280"
180 };
181
182
183 int
184 cs4280_match(parent, match, aux)
185 struct device *parent;
186 struct cfdata *match;
187 void *aux;
188 {
189 struct pci_attach_args *pa = (struct pci_attach_args *)aux;
190
191 if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_CIRRUS)
192 return 0;
193 if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_CIRRUS_CS4280
194 #if 0 /* I can't confirm */
195 || PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_CIRRUS_CS4610
196 #endif
197 )
198 return 1;
199 return 0;
200 }
201
202 void
203 cs4280_attach(parent, self, aux)
204 struct device *parent;
205 struct device *self;
206 void *aux;
207 {
208 struct cs428x_softc *sc = (struct cs428x_softc *)self;
209 struct pci_attach_args *pa = (struct pci_attach_args *)aux;
210 pci_chipset_tag_t pc = pa->pa_pc;
211 char const *intrstr;
212 pci_intr_handle_t ih;
213 pcireg_t reg;
214 char devinfo[256];
215 mixer_ctrl_t ctl;
216 u_int32_t mem;
217 int pci_pwrmgmt_cap_reg, pci_pwrmgmt_csr_reg;
218
219 aprint_naive(": Audio controller\n");
220
221 pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo);
222 aprint_normal(": %s (rev. 0x%02x)\n", devinfo,
223 PCI_REVISION(pa->pa_class));
224
225 /* Map I/O register */
226 if (pci_mapreg_map(pa, PCI_BA0,
227 PCI_MAPREG_TYPE_MEM|PCI_MAPREG_MEM_TYPE_32BIT, 0,
228 &sc->ba0t, &sc->ba0h, NULL, NULL)) {
229 aprint_error("%s: can't map BA0 space\n", sc->sc_dev.dv_xname);
230 return;
231 }
232 if (pci_mapreg_map(pa, PCI_BA1,
233 PCI_MAPREG_TYPE_MEM|PCI_MAPREG_MEM_TYPE_32BIT, 0,
234 &sc->ba1t, &sc->ba1h, NULL, NULL)) {
235 aprint_error("%s: can't map BA1 space\n", sc->sc_dev.dv_xname);
236 return;
237 }
238
239 sc->sc_dmatag = pa->pa_dmat;
240
241 /* Check and set Power State */
242 if (pci_get_capability(pa->pa_pc, pa->pa_tag, PCI_CAP_PWRMGMT,
243 &pci_pwrmgmt_cap_reg, 0)) {
244 pci_pwrmgmt_csr_reg = pci_pwrmgmt_cap_reg + PCI_PMCSR;
245 reg = pci_conf_read(pa->pa_pc, pa->pa_tag,
246 pci_pwrmgmt_csr_reg);
247 DPRINTF(("%s: Power State is %d\n",
248 sc->sc_dev.dv_xname, reg & PCI_PMCSR_STATE_MASK));
249 if ((reg & PCI_PMCSR_STATE_MASK) != PCI_PMCSR_STATE_D0) {
250 pci_conf_write(pc, pa->pa_tag, pci_pwrmgmt_csr_reg,
251 (reg & ~PCI_PMCSR_STATE_MASK) |
252 PCI_PMCSR_STATE_D0);
253 }
254 }
255
256 /* Enable the device (set bus master flag) */
257 reg = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
258 pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
259 reg | PCI_COMMAND_MASTER_ENABLE);
260
261 /* LATENCY_TIMER setting */
262 mem = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_BHLC_REG);
263 if ( PCI_LATTIMER(mem) < 32 ) {
264 mem &= 0xffff00ff;
265 mem |= 0x00002000;
266 pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_BHLC_REG, mem);
267 }
268
269 /* Map and establish the interrupt. */
270 if (pci_intr_map(pa, &ih)) {
271 aprint_error("%s: couldn't map interrupt\n",
272 sc->sc_dev.dv_xname);
273 return;
274 }
275 intrstr = pci_intr_string(pc, ih);
276
277 sc->sc_ih = pci_intr_establish(pc, ih, IPL_AUDIO, cs4280_intr, sc);
278 if (sc->sc_ih == NULL) {
279 aprint_error("%s: couldn't establish interrupt",
280 sc->sc_dev.dv_xname);
281 if (intrstr != NULL)
282 aprint_normal(" at %s", intrstr);
283 aprint_normal("\n");
284 return;
285 }
286 aprint_normal("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr);
287
288 /* Initialization */
289 if(cs4280_init(sc, 1) != 0)
290 return;
291
292 sc->type = TYPE_CS4280;
293 sc->halt_input = cs4280_halt_input;
294 sc->halt_output = cs4280_halt_output;
295
296 /* setup buffer related parameters */
297 sc->dma_size = CS4280_DCHUNK;
298 sc->dma_align = CS4280_DALIGN;
299 sc->hw_blocksize = CS4280_ICHUNK;
300
301 /* AC 97 attachment */
302 sc->host_if.arg = sc;
303 sc->host_if.attach = cs428x_attach_codec;
304 sc->host_if.read = cs428x_read_codec;
305 sc->host_if.write = cs428x_write_codec;
306 sc->host_if.reset = cs4280_reset_codec;
307 if (ac97_attach(&sc->host_if) != 0) {
308 aprint_error("%s: ac97_attach failed\n", sc->sc_dev.dv_xname);
309 return;
310 }
311
312 /* Turn mute off of DAC, CD and master volumes by default */
313 ctl.type = AUDIO_MIXER_ENUM;
314 ctl.un.ord = 0; /* off */
315
316 ctl.dev = cs4280_get_portnum_by_name(sc, AudioCoutputs,
317 AudioNmaster, AudioNmute);
318 cs428x_mixer_set_port(sc, &ctl);
319
320 ctl.dev = cs4280_get_portnum_by_name(sc, AudioCinputs,
321 AudioNdac, AudioNmute);
322 cs428x_mixer_set_port(sc, &ctl);
323
324 ctl.dev = cs4280_get_portnum_by_name(sc, AudioCinputs,
325 AudioNcd, AudioNmute);
326 cs428x_mixer_set_port(sc, &ctl);
327
328 audio_attach_mi(&cs4280_hw_if, sc, &sc->sc_dev);
329
330 #if NMIDI > 0
331 midi_attach_mi(&cs4280_midi_hw_if, sc, &sc->sc_dev);
332 #endif
333
334 sc->sc_suspend = PWR_RESUME;
335 sc->sc_powerhook = powerhook_establish(cs4280_power, sc);
336 }
337
338 /* Interrupt handling function */
339 int
340 cs4280_intr(p)
341 void *p;
342 {
343 /*
344 * XXX
345 *
346 * Since CS4280 has only 4kB DMA buffer and
347 * interrupt occurs every 2kB block, I create dummy buffer
348 * which returns to audio driver and actual DMA buffer
349 * using in DMA transfer.
350 *
351 *
352 * ring buffer in audio.c is pointed by BUFADDR
353 * <------ ring buffer size == 64kB ------>
354 * <-----> blksize == 2048*(sc->sc_[pr]count) kB
355 * |= = = =|= = = =|= = = =|= = = =|= = = =|
356 * | | | | | | <- call audio_intp every
357 * sc->sc_[pr]_count time.
358 *
359 * actual DMA buffer is pointed by KERNADDR
360 * <-> DMA buffer size = 4kB
361 * |= =|
362 *
363 *
364 */
365 struct cs428x_softc *sc = p;
366 u_int32_t intr, mem;
367 char * empty_dma;
368 int handled = 0;
369
370 /* grab interrupt register then clear it */
371 intr = BA0READ4(sc, CS4280_HISR);
372 BA0WRITE4(sc, CS4280_HICR, HICR_CHGM | HICR_IEV);
373
374 /* Playback Interrupt */
375 if (intr & HISR_PINT) {
376 handled = 1;
377 mem = BA1READ4(sc, CS4280_PFIE);
378 BA1WRITE4(sc, CS4280_PFIE, (mem & ~PFIE_PI_MASK) | PFIE_PI_DISABLE);
379 if (sc->sc_pintr) {
380 if ((sc->sc_pi%sc->sc_pcount) == 0)
381 sc->sc_pintr(sc->sc_parg);
382 } else {
383 printf("unexpected play intr\n");
384 }
385 /* copy buffer */
386 ++sc->sc_pi;
387 empty_dma = sc->sc_pdma->addr;
388 if (sc->sc_pi&1)
389 empty_dma += sc->hw_blocksize;
390 memcpy(empty_dma, sc->sc_pn, sc->hw_blocksize);
391 sc->sc_pn += sc->hw_blocksize;
392 if (sc->sc_pn >= sc->sc_pe)
393 sc->sc_pn = sc->sc_ps;
394 BA1WRITE4(sc, CS4280_PFIE, mem);
395 }
396 /* Capture Interrupt */
397 if (intr & HISR_CINT) {
398 int i;
399 int16_t rdata;
400
401 handled = 1;
402 mem = BA1READ4(sc, CS4280_CIE);
403 BA1WRITE4(sc, CS4280_CIE, (mem & ~CIE_CI_MASK) | CIE_CI_DISABLE);
404 ++sc->sc_ri;
405 empty_dma = sc->sc_rdma->addr;
406 if ((sc->sc_ri&1) == 0)
407 empty_dma += sc->hw_blocksize;
408
409 /*
410 * XXX
411 * I think this audio data conversion should be
412 * happend in upper layer, but I put this here
413 * since there is no conversion function available.
414 */
415 switch(sc->sc_rparam) {
416 case CF_16BIT_STEREO:
417 /* just copy it */
418 memcpy(sc->sc_rn, empty_dma, sc->hw_blocksize);
419 sc->sc_rn += sc->hw_blocksize;
420 break;
421 case CF_16BIT_MONO:
422 for (i = 0; i < 512; i++) {
423 rdata = *((int16_t *)empty_dma)++>>1;
424 rdata += *((int16_t *)empty_dma)++>>1;
425 *((int16_t *)sc->sc_rn)++ = rdata;
426 }
427 break;
428 case CF_8BIT_STEREO:
429 for (i = 0; i < 512; i++) {
430 rdata = *((int16_t*)empty_dma)++;
431 *sc->sc_rn++ = rdata >> 8;
432 rdata = *((int16_t*)empty_dma)++;
433 *sc->sc_rn++ = rdata >> 8;
434 }
435 break;
436 case CF_8BIT_MONO:
437 for (i = 0; i < 512; i++) {
438 rdata = *((int16_t*)empty_dma)++ >>1;
439 rdata += *((int16_t*)empty_dma)++ >>1;
440 *sc->sc_rn++ = rdata >>8;
441 }
442 break;
443 default:
444 /* Should not reach here */
445 printf("unknown sc->sc_rparam: %d\n", sc->sc_rparam);
446 }
447 if (sc->sc_rn >= sc->sc_re)
448 sc->sc_rn = sc->sc_rs;
449 BA1WRITE4(sc, CS4280_CIE, mem);
450 if (sc->sc_rintr) {
451 if ((sc->sc_ri%(sc->sc_rcount)) == 0)
452 sc->sc_rintr(sc->sc_rarg);
453 } else {
454 printf("unexpected record intr\n");
455 }
456 }
457
458 #if NMIDI > 0
459 /* Midi port Interrupt */
460 if (intr & HISR_MIDI) {
461 int data;
462
463 handled = 1;
464 DPRINTF(("i: %d: ",
465 BA0READ4(sc, CS4280_MIDSR)));
466 /* Read the received data */
467 while ((sc->sc_iintr != NULL) &&
468 ((BA0READ4(sc, CS4280_MIDSR) & MIDSR_RBE) == 0)) {
469 data = BA0READ4(sc, CS4280_MIDRP) & MIDRP_MASK;
470 DPRINTF(("r:%x\n",data));
471 sc->sc_iintr(sc->sc_arg, data);
472 }
473
474 /* Write the data */
475 #if 1
476 /* XXX:
477 * It seems "Transmit Buffer Full" never activate until EOI
478 * is deliverd. Shall I throw EOI top of this routine ?
479 */
480 if ((BA0READ4(sc, CS4280_MIDSR) & MIDSR_TBF) == 0) {
481 DPRINTF(("w: "));
482 if (sc->sc_ointr != NULL)
483 sc->sc_ointr(sc->sc_arg);
484 }
485 #else
486 while ((sc->sc_ointr != NULL) &&
487 ((BA0READ4(sc, CS4280_MIDSR) & MIDSR_TBF) == 0)) {
488 DPRINTF(("w: "));
489 sc->sc_ointr(sc->sc_arg);
490 }
491 #endif
492 DPRINTF(("\n"));
493 }
494 #endif
495
496 return handled;
497 }
498
499 int
500 cs4280_query_encoding(addr, fp)
501 void *addr;
502 struct audio_encoding *fp;
503 {
504 switch (fp->index) {
505 case 0:
506 strcpy(fp->name, AudioEulinear);
507 fp->encoding = AUDIO_ENCODING_ULINEAR;
508 fp->precision = 8;
509 fp->flags = 0;
510 break;
511 case 1:
512 strcpy(fp->name, AudioEmulaw);
513 fp->encoding = AUDIO_ENCODING_ULAW;
514 fp->precision = 8;
515 fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
516 break;
517 case 2:
518 strcpy(fp->name, AudioEalaw);
519 fp->encoding = AUDIO_ENCODING_ALAW;
520 fp->precision = 8;
521 fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
522 break;
523 case 3:
524 strcpy(fp->name, AudioEslinear);
525 fp->encoding = AUDIO_ENCODING_SLINEAR;
526 fp->precision = 8;
527 fp->flags = 0;
528 break;
529 case 4:
530 strcpy(fp->name, AudioEslinear_le);
531 fp->encoding = AUDIO_ENCODING_SLINEAR_LE;
532 fp->precision = 16;
533 fp->flags = 0;
534 break;
535 case 5:
536 strcpy(fp->name, AudioEulinear_le);
537 fp->encoding = AUDIO_ENCODING_ULINEAR_LE;
538 fp->precision = 16;
539 fp->flags = 0;
540 break;
541 case 6:
542 strcpy(fp->name, AudioEslinear_be);
543 fp->encoding = AUDIO_ENCODING_SLINEAR_BE;
544 fp->precision = 16;
545 fp->flags = 0;
546 break;
547 case 7:
548 strcpy(fp->name, AudioEulinear_be);
549 fp->encoding = AUDIO_ENCODING_ULINEAR_BE;
550 fp->precision = 16;
551 fp->flags = 0;
552 break;
553 default:
554 return EINVAL;
555 }
556 return 0;
557 }
558
559 int
560 cs4280_set_params(addr, setmode, usemode, play, rec)
561 void *addr;
562 int setmode, usemode;
563 struct audio_params *play, *rec;
564 {
565 struct cs428x_softc *sc = addr;
566 struct audio_params *p;
567 int mode;
568
569 for (mode = AUMODE_RECORD; mode != -1;
570 mode = mode == AUMODE_RECORD ? AUMODE_PLAY : -1 ) {
571 if ((setmode & mode) == 0)
572 continue;
573
574 p = mode == AUMODE_PLAY ? play : rec;
575
576 if (p == play) {
577 DPRINTFN(5,("play: sample=%ld precision=%d channels=%d\n",
578 p->sample_rate, p->precision, p->channels));
579 /* play back data format may be 8- or 16-bit and
580 * either stereo or mono.
581 * playback rate may range from 8000Hz to 48000Hz
582 */
583 if (p->sample_rate < 8000 || p->sample_rate > 48000 ||
584 (p->precision != 8 && p->precision != 16) ||
585 (p->channels != 1 && p->channels != 2) ) {
586 return EINVAL;
587 }
588 } else {
589 DPRINTFN(5,("rec: sample=%ld precision=%d channels=%d\n",
590 p->sample_rate, p->precision, p->channels));
591 /* capture data format must be 16bit stereo
592 * and sample rate range from 11025Hz to 48000Hz.
593 *
594 * XXX: it looks like to work with 8000Hz,
595 * although data sheets say lower limit is
596 * 11025 Hz.
597 */
598
599 if (p->sample_rate < 8000 || p->sample_rate > 48000 ||
600 (p->precision != 8 && p->precision != 16) ||
601 (p->channels != 1 && p->channels != 2) ) {
602 return EINVAL;
603 }
604 }
605 p->factor = 1;
606 p->sw_code = 0;
607
608 /* capturing data is slinear */
609 switch (p->encoding) {
610 case AUDIO_ENCODING_SLINEAR_BE:
611 if (mode == AUMODE_RECORD) {
612 if (p->precision == 16)
613 p->sw_code = swap_bytes;
614 }
615 break;
616 case AUDIO_ENCODING_SLINEAR_LE:
617 break;
618 case AUDIO_ENCODING_ULINEAR_BE:
619 if (mode == AUMODE_RECORD) {
620 if (p->precision == 16)
621 p->sw_code = change_sign16_swap_bytes_le;
622 else
623 p->sw_code = change_sign8;
624 }
625 break;
626 case AUDIO_ENCODING_ULINEAR_LE:
627 if (mode == AUMODE_RECORD) {
628 if (p->precision == 16)
629 p->sw_code = change_sign16_le;
630 else
631 p->sw_code = change_sign8;
632 }
633 break;
634 case AUDIO_ENCODING_ULAW:
635 if (mode == AUMODE_PLAY) {
636 p->factor = 2;
637 p->sw_code = mulaw_to_slinear16_le;
638 } else {
639 p->sw_code = slinear8_to_mulaw;
640 }
641 break;
642 case AUDIO_ENCODING_ALAW:
643 if (mode == AUMODE_PLAY) {
644 p->factor = 2;
645 p->sw_code = alaw_to_slinear16_le;
646 } else {
647 p->sw_code = slinear8_to_alaw;
648 }
649 break;
650 default:
651 return EINVAL;
652 }
653 }
654
655 /* set sample rate */
656 cs4280_set_dac_rate(sc, play->sample_rate);
657 cs4280_set_adc_rate(sc, rec->sample_rate);
658 return 0;
659 }
660
661 int
662 cs4280_halt_output(addr)
663 void *addr;
664 {
665 struct cs428x_softc *sc = addr;
666 u_int32_t mem;
667
668 mem = BA1READ4(sc, CS4280_PCTL);
669 BA1WRITE4(sc, CS4280_PCTL, mem & ~PCTL_MASK);
670 sc->sc_prun = 0;
671 return 0;
672 }
673
674 int
675 cs4280_halt_input(addr)
676 void *addr;
677 {
678 struct cs428x_softc *sc = addr;
679 u_int32_t mem;
680
681 mem = BA1READ4(sc, CS4280_CCTL);
682 BA1WRITE4(sc, CS4280_CCTL, mem & ~CCTL_MASK);
683 sc->sc_rrun = 0;
684 return 0;
685 }
686
687 int
688 cs4280_getdev(addr, retp)
689 void *addr;
690 struct audio_device *retp;
691 {
692 *retp = cs4280_device;
693 return 0;
694 }
695
696 int
697 cs4280_trigger_output(addr, start, end, blksize, intr, arg, param)
698 void *addr;
699 void *start, *end;
700 int blksize;
701 void (*intr) __P((void *));
702 void *arg;
703 struct audio_params *param;
704 {
705 struct cs428x_softc *sc = addr;
706 u_int32_t pfie, pctl, pdtc;
707 struct cs428x_dma *p;
708
709 #ifdef DIAGNOSTIC
710 if (sc->sc_prun)
711 printf("cs4280_trigger_output: already running\n");
712 #endif
713 sc->sc_prun = 1;
714
715 DPRINTF(("cs4280_trigger_output: sc=%p start=%p end=%p "
716 "blksize=%d intr=%p(%p)\n", addr, start, end, blksize, intr, arg));
717 sc->sc_pintr = intr;
718 sc->sc_parg = arg;
719
720 /* stop playback DMA */
721 BA1WRITE4(sc, CS4280_PCTL, BA1READ4(sc, CS4280_PCTL) & ~PCTL_MASK);
722
723 /* setup PDTC */
724 pdtc = BA1READ4(sc, CS4280_PDTC);
725 pdtc &= ~PDTC_MASK;
726 pdtc |= CS4280_MK_PDTC(param->precision * param->channels);
727 BA1WRITE4(sc, CS4280_PDTC, pdtc);
728
729 DPRINTF(("param: precision=%d factor=%d channels=%d encoding=%d\n",
730 param->precision, param->factor, param->channels,
731 param->encoding));
732 for (p = sc->sc_dmas; p != NULL && BUFADDR(p) != start; p = p->next)
733 ;
734 if (p == NULL) {
735 printf("cs4280_trigger_output: bad addr %p\n", start);
736 return EINVAL;
737 }
738 if (DMAADDR(p) % sc->dma_align != 0 ) {
739 printf("cs4280_trigger_output: DMAADDR(p)=0x%lx does not start"
740 "4kB align\n", (ulong)DMAADDR(p));
741 return EINVAL;
742 }
743
744 sc->sc_pcount = blksize / sc->hw_blocksize; /* sc->hw_blocksize is fixed hardware blksize*/
745 sc->sc_ps = (char *)start;
746 sc->sc_pe = (char *)end;
747 sc->sc_pdma = p;
748 sc->sc_pbuf = KERNADDR(p);
749 sc->sc_pi = 0;
750 sc->sc_pn = sc->sc_ps;
751 if (blksize >= sc->dma_size) {
752 sc->sc_pn = sc->sc_ps + sc->dma_size;
753 memcpy(sc->sc_pbuf, start, sc->dma_size);
754 ++sc->sc_pi;
755 } else {
756 sc->sc_pn = sc->sc_ps + sc->hw_blocksize;
757 memcpy(sc->sc_pbuf, start, sc->hw_blocksize);
758 }
759
760 /* initiate playback DMA */
761 BA1WRITE4(sc, CS4280_PBA, DMAADDR(p));
762
763 /* set PFIE */
764 pfie = BA1READ4(sc, CS4280_PFIE) & ~PFIE_MASK;
765
766 if (param->precision * param->factor == 8)
767 pfie |= PFIE_8BIT;
768 if (param->channels == 1)
769 pfie |= PFIE_MONO;
770
771 if (param->encoding == AUDIO_ENCODING_ULINEAR_BE ||
772 param->encoding == AUDIO_ENCODING_SLINEAR_BE)
773 pfie |= PFIE_SWAPPED;
774 if (param->encoding == AUDIO_ENCODING_ULINEAR_BE ||
775 param->encoding == AUDIO_ENCODING_ULINEAR_LE)
776 pfie |= PFIE_UNSIGNED;
777
778 BA1WRITE4(sc, CS4280_PFIE, pfie | PFIE_PI_ENABLE);
779
780 sc->sc_prate = param->sample_rate;
781 cs4280_set_dac_rate(sc, param->sample_rate);
782
783 pctl = BA1READ4(sc, CS4280_PCTL) & ~PCTL_MASK;
784 pctl |= sc->pctl;
785 BA1WRITE4(sc, CS4280_PCTL, pctl);
786 return 0;
787 }
788
789 int
790 cs4280_trigger_input(addr, start, end, blksize, intr, arg, param)
791 void *addr;
792 void *start, *end;
793 int blksize;
794 void (*intr) __P((void *));
795 void *arg;
796 struct audio_params *param;
797 {
798 struct cs428x_softc *sc = addr;
799 u_int32_t cctl, cie;
800 struct cs428x_dma *p;
801
802 #ifdef DIAGNOSTIC
803 if (sc->sc_rrun)
804 printf("cs4280_trigger_input: already running\n");
805 #endif
806 sc->sc_rrun = 1;
807
808 DPRINTF(("cs4280_trigger_input: sc=%p start=%p end=%p "
809 "blksize=%d intr=%p(%p)\n", addr, start, end, blksize, intr, arg));
810 sc->sc_rintr = intr;
811 sc->sc_rarg = arg;
812
813 /* stop capture DMA */
814 BA1WRITE4(sc, CS4280_CCTL, BA1READ4(sc, CS4280_CCTL) & ~CCTL_MASK);
815
816 for (p = sc->sc_dmas; p && BUFADDR(p) != start; p = p->next)
817 ;
818 if (p == NULL) {
819 printf("cs4280_trigger_input: bad addr %p\n", start);
820 return EINVAL;
821 }
822 if (DMAADDR(p) % sc->dma_align != 0) {
823 printf("cs4280_trigger_input: DMAADDR(p)=0x%lx does not start"
824 "4kB align\n", (ulong)DMAADDR(p));
825 return EINVAL;
826 }
827
828 sc->sc_rcount = blksize / sc->hw_blocksize; /* sc->hw_blocksize is fixed hardware blksize*/
829 sc->sc_rs = (char *)start;
830 sc->sc_re = (char *)end;
831 sc->sc_rdma = p;
832 sc->sc_rbuf = KERNADDR(p);
833 sc->sc_ri = 0;
834 sc->sc_rn = sc->sc_rs;
835
836 /* initiate capture DMA */
837 BA1WRITE4(sc, CS4280_CBA, DMAADDR(p));
838
839 /* setup format information for internal converter */
840 sc->sc_rparam = 0;
841 if (param->precision == 8) {
842 sc->sc_rparam += CF_8BIT;
843 sc->sc_rcount <<= 1;
844 }
845 if (param->channels == 1) {
846 sc->sc_rparam += CF_MONO;
847 sc->sc_rcount <<= 1;
848 }
849
850 /* set CIE */
851 cie = BA1READ4(sc, CS4280_CIE) & ~CIE_CI_MASK;
852 BA1WRITE4(sc, CS4280_CIE, cie | CIE_CI_ENABLE);
853
854 sc->sc_rrate = param->sample_rate;
855 cs4280_set_adc_rate(sc, param->sample_rate);
856
857 cctl = BA1READ4(sc, CS4280_CCTL) & ~CCTL_MASK;
858 cctl |= sc->cctl;
859 BA1WRITE4(sc, CS4280_CCTL, cctl);
860 return 0;
861 }
862
863 /* Power Hook */
864 void
865 cs4280_power(why, v)
866 int why;
867 void *v;
868 {
869 struct cs428x_softc *sc = (struct cs428x_softc *)v;
870 static u_int32_t pctl = 0, pba = 0, pfie = 0, pdtc = 0;
871 static u_int32_t cctl = 0, cba = 0, cie = 0;
872
873 DPRINTF(("%s: cs4280_power why=%d\n",
874 sc->sc_dev.dv_xname, why));
875 switch (why) {
876 case PWR_SUSPEND:
877 case PWR_STANDBY:
878 sc->sc_suspend = why;
879
880 /* save current playback status */
881 if ( sc->sc_prun ) {
882 pctl = BA1READ4(sc, CS4280_PCTL);
883 pfie = BA1READ4(sc, CS4280_PFIE);
884 pba = BA1READ4(sc, CS4280_PBA);
885 pdtc = BA1READ4(sc, CS4280_PDTC);
886 DPRINTF(("pctl=0x%08x pfie=0x%08x pba=0x%08x pdtc=0x%08x\n",
887 pctl, pfie, pba, pdtc));
888 }
889
890 /* save current capture status */
891 if ( sc->sc_rrun ) {
892 cctl = BA1READ4(sc, CS4280_CCTL);
893 cie = BA1READ4(sc, CS4280_CIE);
894 cba = BA1READ4(sc, CS4280_CBA);
895 DPRINTF(("cctl=0x%08x cie=0x%08x cba=0x%08x\n",
896 cctl, cie, cba));
897 }
898
899 /* Stop DMA */
900 BA1WRITE4(sc, CS4280_PCTL, pctl & ~PCTL_MASK);
901 BA1WRITE4(sc, CS4280_CCTL, BA1READ4(sc, CS4280_CCTL) & ~CCTL_MASK);
902 break;
903 case PWR_RESUME:
904 if (sc->sc_suspend == PWR_RESUME) {
905 printf("cs4280_power: odd, resume without suspend.\n");
906 sc->sc_suspend = why;
907 return;
908 }
909 sc->sc_suspend = why;
910 cs4280_init(sc, 0);
911 cs4280_reset_codec(sc);
912
913 /* restore ac97 registers */
914 (*sc->codec_if->vtbl->restore_ports)(sc->codec_if);
915
916 /* restore DMA related status */
917 if(sc->sc_prun) {
918 DPRINTF(("pctl=0x%08x pfie=0x%08x pba=0x%08x pdtc=0x%08x\n",
919 pctl, pfie, pba, pdtc));
920 cs4280_set_dac_rate(sc, sc->sc_prate);
921 BA1WRITE4(sc, CS4280_PDTC, pdtc);
922 BA1WRITE4(sc, CS4280_PBA, pba);
923 BA1WRITE4(sc, CS4280_PFIE, pfie);
924 BA1WRITE4(sc, CS4280_PCTL, pctl);
925 }
926
927 if (sc->sc_rrun) {
928 DPRINTF(("cctl=0x%08x cie=0x%08x cba=0x%08x\n",
929 cctl, cie, cba));
930 cs4280_set_adc_rate(sc, sc->sc_rrate);
931 BA1WRITE4(sc, CS4280_CBA, cba);
932 BA1WRITE4(sc, CS4280_CIE, cie);
933 BA1WRITE4(sc, CS4280_CCTL, cctl);
934 }
935 break;
936 case PWR_SOFTSUSPEND:
937 case PWR_SOFTSTANDBY:
938 case PWR_SOFTRESUME:
939 break;
940 }
941 }
942
943 /* control AC97 codec */
944 int
945 cs4280_reset_codec(void *addr)
946 {
947 struct cs428x_softc *sc;
948 int n;
949
950 sc = addr;
951
952 /* Reset codec */
953 BA0WRITE4(sc, CS428X_ACCTL, 0);
954 delay(100); /* delay 100us */
955 BA0WRITE4(sc, CS428X_ACCTL, ACCTL_RSTN);
956
957 /*
958 * It looks like we do the following procedure, too
959 */
960
961 /* Enable AC-link sync generation */
962 BA0WRITE4(sc, CS428X_ACCTL, ACCTL_ESYN | ACCTL_RSTN);
963 delay(50*1000); /* XXX delay 50ms */
964
965 /* Assert valid frame signal */
966 BA0WRITE4(sc, CS428X_ACCTL, ACCTL_VFRM | ACCTL_ESYN | ACCTL_RSTN);
967
968 /* Wait for valid AC97 input slot */
969 n = 0;
970 while ((BA0READ4(sc, CS428X_ACISV) & (ACISV_ISV3 | ACISV_ISV4)) !=
971 (ACISV_ISV3 | ACISV_ISV4)) {
972 delay(1000);
973 if (++n > 1000) {
974 printf("reset_codec: AC97 inputs slot ready timeout\n");
975 return ETIMEDOUT;
976 }
977 }
978 return 0;
979 }
980
981
982 /* Internal functions */
983
984 void
985 cs4280_set_adc_rate(sc, rate)
986 struct cs428x_softc *sc;
987 int rate;
988 {
989 /* calculate capture rate:
990 *
991 * capture_coefficient_increment = -round(rate*128*65536/48000;
992 * capture_phase_increment = floor(48000*65536*1024/rate);
993 * cx = round(48000*65536*1024 - capture_phase_increment*rate);
994 * cy = floor(cx/200);
995 * capture_sample_rate_correction = cx - 200*cy;
996 * capture_delay = ceil(24*48000/rate);
997 * capture_num_triplets = floor(65536*rate/24000);
998 * capture_group_length = 24000/GCD(rate, 24000);
999 * where GCD means "Greatest Common Divisor".
1000 *
1001 * capture_coefficient_increment, capture_phase_increment and
1002 * capture_num_triplets are 32-bit signed quantities.
1003 * capture_sample_rate_correction and capture_group_length are
1004 * 16-bit signed quantities.
1005 * capture_delay is a 14-bit unsigned quantity.
1006 */
1007 u_int32_t cci,cpi,cnt,cx,cy, tmp1;
1008 u_int16_t csrc, cgl, cdlay;
1009
1010 /* XXX
1011 * Even though, embedded_audio_spec says capture rate range 11025 to
1012 * 48000, dhwiface.cpp says,
1013 *
1014 * "We can only decimate by up to a factor of 1/9th the hardware rate.
1015 * Return an error if an attempt is made to stray outside that limit."
1016 *
1017 * so assume range as 48000/9 to 48000
1018 */
1019
1020 if (rate < 8000)
1021 rate = 8000;
1022 if (rate > 48000)
1023 rate = 48000;
1024
1025 cx = rate << 16;
1026 cci = cx / 48000;
1027 cx -= cci * 48000;
1028 cx <<= 7;
1029 cci <<= 7;
1030 cci += cx / 48000;
1031 cci = - cci;
1032
1033 cx = 48000 << 16;
1034 cpi = cx / rate;
1035 cx -= cpi * rate;
1036 cx <<= 10;
1037 cpi <<= 10;
1038 cy = cx / rate;
1039 cpi += cy;
1040 cx -= cy * rate;
1041
1042 cy = cx / 200;
1043 csrc = cx - 200*cy;
1044
1045 cdlay = ((48000 * 24) + rate - 1) / rate;
1046 #if 0
1047 cdlay &= 0x3fff; /* make sure cdlay is 14-bit */
1048 #endif
1049
1050 cnt = rate << 16;
1051 cnt /= 24000;
1052
1053 cgl = 1;
1054 for (tmp1 = 2; tmp1 <= 64; tmp1 *= 2) {
1055 if (((rate / tmp1) * tmp1) != rate)
1056 cgl *= 2;
1057 }
1058 if (((rate / 3) * 3) != rate)
1059 cgl *= 3;
1060 for (tmp1 = 5; tmp1 <= 125; tmp1 *= 5) {
1061 if (((rate / tmp1) * tmp1) != rate)
1062 cgl *= 5;
1063 }
1064 #if 0
1065 /* XXX what manual says */
1066 tmp1 = BA1READ4(sc, CS4280_CSRC) & ~CSRC_MASK;
1067 tmp1 |= csrc<<16;
1068 BA1WRITE4(sc, CS4280_CSRC, tmp1);
1069 #else
1070 /* suggested by cs461x.c (ALSA driver) */
1071 BA1WRITE4(sc, CS4280_CSRC, CS4280_MK_CSRC(csrc, cy));
1072 #endif
1073
1074 #if 0
1075 /* I am confused. The sample rate calculation section says
1076 * cci *is* 32-bit signed quantity but in the parameter description
1077 * section, CCI only assigned 16bit.
1078 * I believe size of the variable.
1079 */
1080 tmp1 = BA1READ4(sc, CS4280_CCI) & ~CCI_MASK;
1081 tmp1 |= cci<<16;
1082 BA1WRITE4(sc, CS4280_CCI, tmp1);
1083 #else
1084 BA1WRITE4(sc, CS4280_CCI, cci);
1085 #endif
1086
1087 tmp1 = BA1READ4(sc, CS4280_CD) & ~CD_MASK;
1088 tmp1 |= cdlay <<18;
1089 BA1WRITE4(sc, CS4280_CD, tmp1);
1090
1091 BA1WRITE4(sc, CS4280_CPI, cpi);
1092
1093 tmp1 = BA1READ4(sc, CS4280_CGL) & ~CGL_MASK;
1094 tmp1 |= cgl;
1095 BA1WRITE4(sc, CS4280_CGL, tmp1);
1096
1097 BA1WRITE4(sc, CS4280_CNT, cnt);
1098
1099 tmp1 = BA1READ4(sc, CS4280_CGC) & ~CGC_MASK;
1100 tmp1 |= cgl;
1101 BA1WRITE4(sc, CS4280_CGC, tmp1);
1102 }
1103
1104 void
1105 cs4280_set_dac_rate(sc, rate)
1106 struct cs428x_softc *sc;
1107 int rate;
1108 {
1109 /*
1110 * playback rate may range from 8000Hz to 48000Hz
1111 *
1112 * play_phase_increment = floor(rate*65536*1024/48000)
1113 * px = round(rate*65536*1024 - play_phase_incremnt*48000)
1114 * py=floor(px/200)
1115 * play_sample_rate_correction = px - 200*py
1116 *
1117 * play_phase_increment is a 32bit signed quantity.
1118 * play_sample_rate_correction is a 16bit signed quantity.
1119 */
1120 int32_t ppi;
1121 int16_t psrc;
1122 u_int32_t px, py;
1123
1124 if (rate < 8000)
1125 rate = 8000;
1126 if (rate > 48000)
1127 rate = 48000;
1128 px = rate << 16;
1129 ppi = px/48000;
1130 px -= ppi*48000;
1131 ppi <<= 10;
1132 px <<= 10;
1133 py = px / 48000;
1134 ppi += py;
1135 px -= py*48000;
1136 py = px/200;
1137 px -= py*200;
1138 psrc = px;
1139 #if 0
1140 /* what manual says */
1141 px = BA1READ4(sc, CS4280_PSRC) & ~PSRC_MASK;
1142 BA1WRITE4(sc, CS4280_PSRC,
1143 ( ((psrc<<16) & PSRC_MASK) | px ));
1144 #else
1145 /* suggested by cs461x.c (ALSA driver) */
1146 BA1WRITE4(sc, CS4280_PSRC, CS4280_MK_PSRC(psrc,py));
1147 #endif
1148 BA1WRITE4(sc, CS4280_PPI, ppi);
1149 }
1150
1151 /* Download Proceessor Code and Data image */
1152 int
1153 cs4280_download(sc, src, offset, len)
1154 struct cs428x_softc *sc;
1155 const u_int32_t *src;
1156 u_int32_t offset, len;
1157 {
1158 u_int32_t ctr;
1159
1160 #if CS4280_DEBUG > 10
1161 u_int32_t con, data;
1162 u_int8_t c0,c1,c2,c3;
1163 #endif
1164 if ((offset&3) || (len&3))
1165 return -1;
1166
1167 len /= sizeof(u_int32_t);
1168 for (ctr = 0; ctr < len; ctr++) {
1169 /* XXX:
1170 * I cannot confirm this is the right thing or not
1171 * on BIG-ENDIAN machines.
1172 */
1173 BA1WRITE4(sc, offset+ctr*4, htole32(*(src+ctr)));
1174 #if CS4280_DEBUG > 10
1175 data = htole32(*(src+ctr));
1176 c0 = bus_space_read_1(sc->ba1t, sc->ba1h, offset+ctr*4+0);
1177 c1 = bus_space_read_1(sc->ba1t, sc->ba1h, offset+ctr*4+1);
1178 c2 = bus_space_read_1(sc->ba1t, sc->ba1h, offset+ctr*4+2);
1179 c3 = bus_space_read_1(sc->ba1t, sc->ba1h, offset+ctr*4+3);
1180 con = ( (c3<<24) | (c2<<16) | (c1<<8) | c0 );
1181 if (data != con ) {
1182 printf("0x%06x: write=0x%08x read=0x%08x\n",
1183 offset+ctr*4, data, con);
1184 return -1;
1185 }
1186 #endif
1187 }
1188 return 0;
1189 }
1190
1191 int
1192 cs4280_download_image(sc)
1193 struct cs428x_softc *sc;
1194 {
1195 int idx, err;
1196 u_int32_t offset = 0;
1197
1198 err = 0;
1199 for (idx = 0; idx < BA1_MEMORY_COUNT; ++idx) {
1200 err = cs4280_download(sc, &BA1Struct.map[offset],
1201 BA1Struct.memory[idx].offset,
1202 BA1Struct.memory[idx].size);
1203 if (err != 0) {
1204 printf("%s: load_image failed at %d\n",
1205 sc->sc_dev.dv_xname, idx);
1206 return -1;
1207 }
1208 offset += BA1Struct.memory[idx].size / sizeof(u_int32_t);
1209 }
1210 return err;
1211 }
1212
1213 /* Processor Soft Reset */
1214 void
1215 cs4280_reset(sc_)
1216 void *sc_;
1217 {
1218 struct cs428x_softc *sc = sc_;
1219
1220 /* Set RSTSP bit in SPCR (also clear RUN, RUNFR, and DRQEN) */
1221 BA1WRITE4(sc, CS4280_SPCR, SPCR_RSTSP);
1222 delay(100);
1223 /* Clear RSTSP bit in SPCR */
1224 BA1WRITE4(sc, CS4280_SPCR, 0);
1225 /* enable DMA reqest */
1226 BA1WRITE4(sc, CS4280_SPCR, SPCR_DRQEN);
1227 }
1228
1229 int
1230 cs4280_get_portnum_by_name(sc, class, device, qualifier)
1231 struct cs428x_softc *sc;
1232 char *class, *device, *qualifier;
1233 {
1234 return (sc->codec_if->vtbl->get_portnum_by_name(sc->codec_if, class,
1235 device, qualifier));
1236 }
1237
1238 int
1239 cs4280_init(sc, init)
1240 struct cs428x_softc *sc;
1241 int init;
1242 {
1243 int n;
1244 u_int32_t mem;
1245
1246 /* Start PLL out in known state */
1247 BA0WRITE4(sc, CS4280_CLKCR1, 0);
1248 /* Start serial ports out in known state */
1249 BA0WRITE4(sc, CS4280_SERMC1, 0);
1250
1251 /* Specify type of CODEC */
1252 /* XXX should not be here */
1253 #define SERACC_CODEC_TYPE_1_03
1254 #ifdef SERACC_CODEC_TYPE_1_03
1255 BA0WRITE4(sc, CS4280_SERACC, SERACC_HSP | SERACC_CTYPE_1_03); /* AC 97 1.03 */
1256 #else
1257 BA0WRITE4(sc, CS4280_SERACC, SERACC_HSP | SERACC_CTYPE_2_0); /* AC 97 2.0 */
1258 #endif
1259
1260 /* Reset codec */
1261 BA0WRITE4(sc, CS428X_ACCTL, 0);
1262 delay(100); /* delay 100us */
1263 BA0WRITE4(sc, CS428X_ACCTL, ACCTL_RSTN);
1264
1265 /* Enable AC-link sync generation */
1266 BA0WRITE4(sc, CS428X_ACCTL, ACCTL_ESYN | ACCTL_RSTN);
1267 delay(50*1000); /* delay 50ms */
1268
1269 /* Set the serial port timing configuration */
1270 BA0WRITE4(sc, CS4280_SERMC1, SERMC1_PTC_AC97);
1271
1272 /* Setup clock control */
1273 BA0WRITE4(sc, CS4280_PLLCC, PLLCC_CDR_STATE|PLLCC_LPF_STATE);
1274 BA0WRITE4(sc, CS4280_PLLM, PLLM_STATE);
1275 BA0WRITE4(sc, CS4280_CLKCR2, CLKCR2_PDIVS_8);
1276
1277 /* Power up the PLL */
1278 BA0WRITE4(sc, CS4280_CLKCR1, CLKCR1_PLLP);
1279 delay(50*1000); /* delay 50ms */
1280
1281 /* Turn on clock */
1282 mem = BA0READ4(sc, CS4280_CLKCR1) | CLKCR1_SWCE;
1283 BA0WRITE4(sc, CS4280_CLKCR1, mem);
1284
1285 /* Set the serial port FIFO pointer to the
1286 * first sample in FIFO. (not documented) */
1287 cs4280_clear_fifos(sc);
1288
1289 #if 0
1290 /* Set the serial port FIFO pointer to the first sample in the FIFO */
1291 BA0WRITE4(sc, CS4280_SERBSP, 0);
1292 #endif
1293
1294 /* Configure the serial port */
1295 BA0WRITE4(sc, CS4280_SERC1, SERC1_SO1EN | SERC1_SO1F_AC97);
1296 BA0WRITE4(sc, CS4280_SERC2, SERC2_SI1EN | SERC2_SI1F_AC97);
1297 BA0WRITE4(sc, CS4280_SERMC1, SERMC1_MSPE | SERMC1_PTC_AC97);
1298
1299 /* Wait for CODEC ready */
1300 n = 0;
1301 while ((BA0READ4(sc, CS428X_ACSTS) & ACSTS_CRDY) == 0) {
1302 delay(125);
1303 if (++n > 1000) {
1304 printf("%s: codec ready timeout\n",
1305 sc->sc_dev.dv_xname);
1306 return(1);
1307 }
1308 }
1309
1310 /* Assert valid frame signal */
1311 BA0WRITE4(sc, CS428X_ACCTL, ACCTL_VFRM | ACCTL_ESYN | ACCTL_RSTN);
1312
1313 /* Wait for valid AC97 input slot */
1314 n = 0;
1315 while ((BA0READ4(sc, CS428X_ACISV) & (ACISV_ISV3 | ACISV_ISV4)) !=
1316 (ACISV_ISV3 | ACISV_ISV4)) {
1317 delay(1000);
1318 if (++n > 1000) {
1319 printf("AC97 inputs slot ready timeout\n");
1320 return(1);
1321 }
1322 }
1323
1324 /* Set AC97 output slot valid signals */
1325 BA0WRITE4(sc, CS428X_ACOSV, ACOSV_SLV3 | ACOSV_SLV4);
1326
1327 /* reset the processor */
1328 cs4280_reset(sc);
1329
1330 /* Download the image to the processor */
1331 if (cs4280_download_image(sc) != 0) {
1332 printf("%s: image download error\n", sc->sc_dev.dv_xname);
1333 return(1);
1334 }
1335
1336 /* Save playback parameter and then write zero.
1337 * this ensures that DMA doesn't immediately occur upon
1338 * starting the processor core
1339 */
1340 mem = BA1READ4(sc, CS4280_PCTL);
1341 sc->pctl = mem & PCTL_MASK; /* save startup value */
1342 BA1WRITE4(sc, CS4280_PCTL, mem & ~PCTL_MASK);
1343 if (init != 0)
1344 sc->sc_prun = 0;
1345
1346 /* Save capture parameter and then write zero.
1347 * this ensures that DMA doesn't immediately occur upon
1348 * starting the processor core
1349 */
1350 mem = BA1READ4(sc, CS4280_CCTL);
1351 sc->cctl = mem & CCTL_MASK; /* save startup value */
1352 BA1WRITE4(sc, CS4280_CCTL, mem & ~CCTL_MASK);
1353 if (init != 0)
1354 sc->sc_rrun = 0;
1355
1356 /* Processor Startup Procedure */
1357 BA1WRITE4(sc, CS4280_FRMT, FRMT_FTV);
1358 BA1WRITE4(sc, CS4280_SPCR, SPCR_RUN | SPCR_RUNFR | SPCR_DRQEN);
1359
1360 /* Monitor RUNFR bit in SPCR for 1 to 0 transition */
1361 n = 0;
1362 while (BA1READ4(sc, CS4280_SPCR) & SPCR_RUNFR) {
1363 delay(10);
1364 if (++n > 1000) {
1365 printf("SPCR 1->0 transition timeout\n");
1366 return(1);
1367 }
1368 }
1369
1370 n = 0;
1371 while (!(BA1READ4(sc, CS4280_SPCS) & SPCS_SPRUN)) {
1372 delay(10);
1373 if (++n > 1000) {
1374 printf("SPCS 0->1 transition timeout\n");
1375 return(1);
1376 }
1377 }
1378 /* Processor is now running !!! */
1379
1380 /* Setup volume */
1381 BA1WRITE4(sc, CS4280_PVOL, 0x80008000);
1382 BA1WRITE4(sc, CS4280_CVOL, 0x80008000);
1383
1384 /* Interrupt enable */
1385 BA0WRITE4(sc, CS4280_HICR, HICR_IEV|HICR_CHGM);
1386
1387 /* playback interrupt enable */
1388 mem = BA1READ4(sc, CS4280_PFIE) & ~PFIE_PI_MASK;
1389 mem |= PFIE_PI_ENABLE;
1390 BA1WRITE4(sc, CS4280_PFIE, mem);
1391 /* capture interrupt enable */
1392 mem = BA1READ4(sc, CS4280_CIE) & ~CIE_CI_MASK;
1393 mem |= CIE_CI_ENABLE;
1394 BA1WRITE4(sc, CS4280_CIE, mem);
1395
1396 #if NMIDI > 0
1397 /* Reset midi port */
1398 mem = BA0READ4(sc, CS4280_MIDCR) & ~MIDCR_MASK;
1399 BA0WRITE4(sc, CS4280_MIDCR, mem | MIDCR_MRST);
1400 DPRINTF(("midi reset: 0x%x\n", BA0READ4(sc, CS4280_MIDCR)));
1401 /* midi interrupt enable */
1402 mem |= MIDCR_TXE | MIDCR_RXE | MIDCR_RIE | MIDCR_TIE;
1403 BA0WRITE4(sc, CS4280_MIDCR, mem);
1404 #endif
1405 return(0);
1406 }
1407
1408 void
1409 cs4280_clear_fifos(sc)
1410 struct cs428x_softc *sc;
1411 {
1412 int pd = 0, cnt, n;
1413 u_int32_t mem;
1414
1415 /*
1416 * If device power down, power up the device and keep power down
1417 * state.
1418 */
1419 mem = BA0READ4(sc, CS4280_CLKCR1);
1420 if (!(mem & CLKCR1_SWCE)) {
1421 printf("cs4280_clear_fifo: power down found.\n");
1422 BA0WRITE4(sc, CS4280_CLKCR1, mem | CLKCR1_SWCE);
1423 pd = 1;
1424 }
1425 BA0WRITE4(sc, CS4280_SERBWP, 0);
1426 for (cnt = 0; cnt < 256; cnt++) {
1427 n = 0;
1428 while (BA0READ4(sc, CS4280_SERBST) & SERBST_WBSY) {
1429 delay(1000);
1430 if (++n > 1000) {
1431 printf("clear_fifo: fist timeout cnt=%d\n", cnt);
1432 break;
1433 }
1434 }
1435 BA0WRITE4(sc, CS4280_SERBAD, cnt);
1436 BA0WRITE4(sc, CS4280_SERBCM, SERBCM_WRC);
1437 }
1438 if (pd)
1439 BA0WRITE4(sc, CS4280_CLKCR1, mem);
1440 }
1441
1442 #if NMIDI > 0
1443 int
1444 cs4280_midi_open(addr, flags, iintr, ointr, arg)
1445 void *addr;
1446 int flags;
1447 void (*iintr)__P((void *, int));
1448 void (*ointr)__P((void *));
1449 void *arg;
1450 {
1451 struct cs428x_softc *sc = addr;
1452 u_int32_t mem;
1453
1454 DPRINTF(("midi_open\n"));
1455 sc->sc_iintr = iintr;
1456 sc->sc_ointr = ointr;
1457 sc->sc_arg = arg;
1458
1459 /* midi interrupt enable */
1460 mem = BA0READ4(sc, CS4280_MIDCR) & ~MIDCR_MASK;
1461 mem |= MIDCR_TXE | MIDCR_RXE | MIDCR_RIE | MIDCR_TIE | MIDCR_MLB;
1462 BA0WRITE4(sc, CS4280_MIDCR, mem);
1463 #ifdef CS4280_DEBUG
1464 if (mem != BA0READ4(sc, CS4280_MIDCR)) {
1465 DPRINTF(("midi_open: MIDCR=%d\n", BA0READ4(sc, CS4280_MIDCR)));
1466 return(EINVAL);
1467 }
1468 DPRINTF(("MIDCR=0x%x\n", BA0READ4(sc, CS4280_MIDCR)));
1469 #endif
1470 return 0;
1471 }
1472
1473 void
1474 cs4280_midi_close(addr)
1475 void *addr;
1476 {
1477 struct cs428x_softc *sc = addr;
1478 u_int32_t mem;
1479
1480 DPRINTF(("midi_close\n"));
1481 tsleep(sc, PWAIT, "cs0clm", hz/10); /* give uart a chance to drain */
1482 mem = BA0READ4(sc, CS4280_MIDCR);
1483 mem &= ~MIDCR_MASK;
1484 BA0WRITE4(sc, CS4280_MIDCR, mem);
1485
1486 sc->sc_iintr = 0;
1487 sc->sc_ointr = 0;
1488 }
1489
1490 int
1491 cs4280_midi_output(addr, d)
1492 void *addr;
1493 int d;
1494 {
1495 struct cs428x_softc *sc = addr;
1496 u_int32_t mem;
1497 int x;
1498
1499 for (x = 0; x != MIDI_BUSY_WAIT; x++) {
1500 if ((BA0READ4(sc, CS4280_MIDSR) & MIDSR_TBF) == 0) {
1501 mem = BA0READ4(sc, CS4280_MIDWP) & ~MIDWP_MASK;
1502 mem |= d & MIDWP_MASK;
1503 DPRINTFN(5,("midi_output d=0x%08x",d));
1504 BA0WRITE4(sc, CS4280_MIDWP, mem);
1505 #ifdef DIAGNOSTIC
1506 if (mem != BA0READ4(sc, CS4280_MIDWP)) {
1507 DPRINTF(("Bad write data: %d %d",
1508 mem, BA0READ4(sc, CS4280_MIDWP)));
1509 return(EIO);
1510 }
1511 #endif
1512 return 0;
1513 }
1514 delay(MIDI_BUSY_DELAY);
1515 }
1516 return (EIO);
1517 }
1518
1519 void
1520 cs4280_midi_getinfo(addr, mi)
1521 void *addr;
1522 struct midi_info *mi;
1523 {
1524 mi->name = "CS4280 MIDI UART";
1525 mi->props = MIDI_PROP_CAN_INPUT | MIDI_PROP_OUT_INTR;
1526 }
1527
1528 #endif
1529
1530 /* DEBUG functions */
1531 #if CS4280_DEBUG > 10
1532 int
1533 cs4280_checkimage(sc, src, offset, len)
1534 struct cs428x_softc *sc;
1535 u_int32_t *src;
1536 u_int32_t offset, len;
1537 {
1538 u_int32_t ctr, data;
1539 int err = 0;
1540
1541 if ((offset&3) || (len&3))
1542 return -1;
1543
1544 len /= sizeof(u_int32_t);
1545 for (ctr = 0; ctr < len; ctr++) {
1546 /* I cannot confirm this is the right thing
1547 * on BIG-ENDIAN machines
1548 */
1549 data = BA1READ4(sc, offset+ctr*4);
1550 if (data != htole32(*(src+ctr))) {
1551 printf("0x%06x: 0x%08x(0x%08x)\n",
1552 offset+ctr*4, data, *(src+ctr));
1553 *(src+ctr) = data;
1554 ++err;
1555 }
1556 }
1557 return err;
1558 }
1559
1560 int
1561 cs4280_check_images(sc)
1562 struct cs428x_softc *sc;
1563 {
1564 int idx, err;
1565 u_int32_t offset = 0;
1566
1567 err = 0;
1568 /*for (idx=0; idx < BA1_MEMORY_COUNT; ++idx) { */
1569 for (idx = 0; idx < 1; ++idx) {
1570 err = cs4280_checkimage(sc, &BA1Struct.map[offset],
1571 BA1Struct.memory[idx].offset,
1572 BA1Struct.memory[idx].size);
1573 if (err != 0) {
1574 printf("%s: check_image failed at %d\n",
1575 sc->sc_dev.dv_xname, idx);
1576 }
1577 offset += BA1Struct.memory[idx].size / sizeof(u_int32_t);
1578 }
1579 return err;
1580 }
1581
1582 #endif
1583