xref: /freebsd/sys/dev/sound/pci/csa.c (revision 77a0943ded95b9e6438f7db70c4a28e4d93946d4)
1 /*
2  * Copyright (c) 1999 Seigo Tanimura
3  * All rights reserved.
4  *
5  * Portions of this source are based on cwcealdr.cpp and dhwiface.cpp in
6  * cwcealdr1.zip, the sample sources by Crystal Semiconductor.
7  * Copyright (c) 1996-1998 Crystal Semiconductor Corp.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  *
30  * $FreeBSD$
31  */
32 
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
36 #include <sys/bus.h>
37 #include <sys/malloc.h>
38 #include <sys/module.h>
39 #include <machine/resource.h>
40 #include <machine/bus.h>
41 #include <sys/rman.h>
42 #include <sys/soundcard.h>
43 #include <dev/sound/pcm/sound.h>
44 #include <dev/sound/chip.h>
45 #include <dev/sound/pci/csareg.h>
46 #include <dev/sound/pci/csavar.h>
47 
48 #include <pci/pcireg.h>
49 #include <pci/pcivar.h>
50 
51 #include <dev/sound/pci/csaimg.h>
52 
53 /* Here is the parameter structure per a device. */
54 struct csa_softc {
55 	device_t dev; /* device */
56 	csa_res res; /* resources */
57 
58 	device_t pcm; /* pcm device */
59 	driver_intr_t* pcmintr; /* pcm intr */
60 	void *pcmintr_arg; /* pcm intr arg */
61 	device_t midi; /* midi device */
62 	driver_intr_t* midiintr; /* midi intr */
63 	void *midiintr_arg; /* midi intr arg */
64 	void *ih; /* cookie */
65 
66 	struct csa_bridgeinfo binfo; /* The state of this bridge. */
67 };
68 
69 typedef struct csa_softc *sc_p;
70 
71 static int csa_probe(device_t dev);
72 static int csa_attach(device_t dev);
73 static struct resource *csa_alloc_resource(device_t bus, device_t child, int type, int *rid,
74 					      u_long start, u_long end, u_long count, u_int flags);
75 static int csa_release_resource(device_t bus, device_t child, int type, int rid,
76 				   struct resource *r);
77 static int csa_setup_intr(device_t bus, device_t child,
78 			  struct resource *irq, int flags,
79 			  driver_intr_t *intr, void *arg, void **cookiep);
80 static int csa_teardown_intr(device_t bus, device_t child,
81 			     struct resource *irq, void *cookie);
82 static driver_intr_t csa_intr;
83 static int csa_initialize(sc_p scp);
84 static void csa_resetdsp(csa_res *resp);
85 static int csa_downloadimage(csa_res *resp);
86 static int csa_transferimage(csa_res *resp, u_long *src, u_long dest, u_long len);
87 
88 static devclass_t csa_devclass;
89 
90 static int
91 csa_probe(device_t dev)
92 {
93 	char *s;
94 
95 	s = NULL;
96 	switch (pci_get_devid(dev)) {
97 	case CS4610_PCI_ID:
98 		s = "Crystal Semiconductor CS4610/4611 Audio accelerator";
99 		break;
100 	case CS4614_PCI_ID:
101 		s = "Crystal Semiconductor CS4614/4622/4624 Audio accelerator/4280 Audio controller";
102 		break;
103 	case CS4615_PCI_ID:
104 		s = "Crystal Semiconductor CS4615 Audio accelerator";
105 		break;
106 	}
107 
108 	if (s != NULL) {
109 		device_set_desc(dev, s);
110 		return (0);
111 	}
112 
113 	return (ENXIO);
114 }
115 
116 static int
117 csa_attach(device_t dev)
118 {
119 	u_int32_t stcmd;
120 	sc_p scp;
121 	csa_res *resp;
122 	struct sndcard_func *func;
123 
124 	scp = device_get_softc(dev);
125 
126 	/* Fill in the softc. */
127 	bzero(scp, sizeof(*scp));
128 	scp->dev = dev;
129 
130 	/* Wake up the device. */
131 	stcmd = pci_read_config(dev, PCIR_COMMAND, 4);
132 	if ((stcmd & PCIM_CMD_MEMEN) == 0 || (stcmd & PCIM_CMD_BUSMASTEREN) == 0) {
133 		stcmd |= (PCIM_CMD_MEMEN | PCIM_CMD_BUSMASTEREN);
134 		pci_write_config(dev, PCIR_COMMAND, 4, stcmd);
135 	}
136 
137 	/* Allocate the resources. */
138 	resp = &scp->res;
139 	resp->io_rid = CS461x_IO_OFFSET;
140 	resp->io = bus_alloc_resource(dev, SYS_RES_MEMORY, &resp->io_rid, 0, ~0, CS461x_IO_SIZE, RF_ACTIVE);
141 	if (resp->io == NULL)
142 		return (ENXIO);
143 	resp->mem_rid = CS461x_MEM_OFFSET;
144 	resp->mem = bus_alloc_resource(dev, SYS_RES_MEMORY, &resp->mem_rid, 0, ~0, CS461x_MEM_SIZE, RF_ACTIVE);
145 	if (resp->mem == NULL) {
146 		bus_release_resource(dev, SYS_RES_MEMORY, resp->io_rid, resp->io);
147 		return (ENXIO);
148 	}
149 	resp->irq_rid = 0;
150 	resp->irq = bus_alloc_resource(dev, SYS_RES_IRQ, &resp->irq_rid, 0, ~0, 1, RF_ACTIVE | RF_SHAREABLE);
151 	if (resp->irq == NULL) {
152 		bus_release_resource(dev, SYS_RES_MEMORY, resp->io_rid, resp->io);
153 		bus_release_resource(dev, SYS_RES_MEMORY, resp->mem_rid, resp->mem);
154 		return (ENXIO);
155 	}
156 
157 	/* Enable interrupt. */
158 	if (bus_setup_intr(dev, resp->irq, INTR_TYPE_TTY, csa_intr, scp, &scp->ih)) {
159 		bus_release_resource(dev, SYS_RES_MEMORY, resp->io_rid, resp->io);
160 		bus_release_resource(dev, SYS_RES_MEMORY, resp->mem_rid, resp->mem);
161 		bus_release_resource(dev, SYS_RES_IRQ, resp->irq_rid, resp->irq);
162 		return (ENXIO);
163 	}
164 	if ((csa_readio(resp, BA0_HISR) & HISR_INTENA) == 0)
165 		csa_writeio(resp, BA0_HICR, HICR_IEV | HICR_CHGM);
166 
167 	/* Initialize the chip. */
168 	if (csa_initialize(scp)) {
169 		bus_release_resource(dev, SYS_RES_MEMORY, resp->io_rid, resp->io);
170 		bus_release_resource(dev, SYS_RES_MEMORY, resp->mem_rid, resp->mem);
171 		bus_release_resource(dev, SYS_RES_IRQ, resp->irq_rid, resp->irq);
172 		return (ENXIO);
173 	}
174 
175 	/* Reset the Processor. */
176 	csa_resetdsp(resp);
177 
178 	/* Download the Processor Image to the processor. */
179 	if (csa_downloadimage(resp)) {
180 		bus_release_resource(dev, SYS_RES_MEMORY, resp->io_rid, resp->io);
181 		bus_release_resource(dev, SYS_RES_MEMORY, resp->mem_rid, resp->mem);
182 		bus_release_resource(dev, SYS_RES_IRQ, resp->irq_rid, resp->irq);
183 		return (ENXIO);
184 	}
185 
186 	/* Attach the children. */
187 
188 	/* PCM Audio */
189 	func = malloc(sizeof(struct sndcard_func), M_DEVBUF, M_NOWAIT);
190 	if (func == NULL)
191 		return (ENOMEM);
192 	bzero(func, sizeof(*func));
193 	func->varinfo = &scp->binfo;
194 	func->func = SCF_PCM;
195 	scp->pcm = device_add_child(dev, "pcm", -1);
196 	device_set_ivars(scp->pcm, func);
197 
198 	/* Midi Interface */
199 	func = malloc(sizeof(struct sndcard_func), M_DEVBUF, M_NOWAIT);
200 	if (func == NULL)
201 		return (ENOMEM);
202 	bzero(func, sizeof(*func));
203 	func->varinfo = &scp->binfo;
204 	func->func = SCF_MIDI;
205 	scp->midi = device_add_child(dev, "midi", -1);
206 	device_set_ivars(scp->midi, func);
207 
208 	bus_generic_attach(dev);
209 
210 	return (0);
211 }
212 
213 static struct resource *
214 csa_alloc_resource(device_t bus, device_t child, int type, int *rid,
215 		      u_long start, u_long end, u_long count, u_int flags)
216 {
217 	sc_p scp;
218 	csa_res *resp;
219 	struct resource *res;
220 
221 	scp = device_get_softc(bus);
222 	resp = &scp->res;
223 	switch (type) {
224 	case SYS_RES_IRQ:
225 		if (*rid != 0)
226 			return (NULL);
227 		res = resp->irq;
228 		break;
229 	case SYS_RES_MEMORY:
230 		switch (*rid) {
231 		case CS461x_IO_OFFSET:
232 			res = resp->io;
233 			break;
234 		case CS461x_MEM_OFFSET:
235 			res = resp->mem;
236 			break;
237 		default:
238 			return (NULL);
239 		}
240 		break;
241 	default:
242 		return (NULL);
243 	}
244 
245 	return res;
246 }
247 
248 static int
249 csa_release_resource(device_t bus, device_t child, int type, int rid,
250 			struct resource *r)
251 {
252 	return (0);
253 }
254 
255 /*
256  * The following three functions deal with interrupt handling.
257  * An interrupt is primarily handled by the bridge driver.
258  * The bridge driver then determines the child devices to pass
259  * the interrupt. Certain information of the device can be read
260  * only once(eg the value of HISR). The bridge driver is responsible
261  * to pass such the information to the children.
262  */
263 
264 static int
265 csa_setup_intr(device_t bus, device_t child,
266 	       struct resource *irq, int flags,
267 	       driver_intr_t *intr, void *arg, void **cookiep)
268 {
269 	sc_p scp;
270 	csa_res *resp;
271 	struct sndcard_func *func;
272 
273 	scp = device_get_softc(bus);
274 	resp = &scp->res;
275 
276 	/*
277 	 * Look at the function code of the child to determine
278 	 * the appropriate hander for it.
279 	 */
280 	func = device_get_ivars(child);
281 	if (func == NULL || irq != resp->irq)
282 		return (EINVAL);
283 
284 	switch (func->func) {
285 	case SCF_PCM:
286 		scp->pcmintr = intr;
287 		scp->pcmintr_arg = arg;
288 		break;
289 
290 	case SCF_MIDI:
291 		scp->midiintr = intr;
292 		scp->midiintr_arg = arg;
293 		break;
294 
295 	default:
296 		return (EINVAL);
297 	}
298 	*cookiep = scp;
299 	if ((csa_readio(resp, BA0_HISR) & HISR_INTENA) == 0)
300 		csa_writeio(resp, BA0_HICR, HICR_IEV | HICR_CHGM);
301 
302 	return (0);
303 }
304 
305 static int
306 csa_teardown_intr(device_t bus, device_t child,
307 		  struct resource *irq, void *cookie)
308 {
309 	sc_p scp;
310 	csa_res *resp;
311 	struct sndcard_func *func;
312 
313 	scp = device_get_softc(bus);
314 	resp = &scp->res;
315 
316 	/*
317 	 * Look at the function code of the child to determine
318 	 * the appropriate hander for it.
319 	 */
320 	func = device_get_ivars(child);
321 	if (func == NULL || irq != resp->irq || cookie != scp)
322 		return (EINVAL);
323 
324 	switch (func->func) {
325 	case SCF_PCM:
326 		scp->pcmintr = NULL;
327 		scp->pcmintr_arg = NULL;
328 		break;
329 
330 	case SCF_MIDI:
331 		scp->midiintr = NULL;
332 		scp->midiintr_arg = NULL;
333 		break;
334 
335 	default:
336 		return (EINVAL);
337 	}
338 
339 	return (0);
340 }
341 
342 /* The interrupt handler */
343 static void
344 csa_intr(void *arg)
345 {
346 	sc_p scp = arg;
347 	csa_res *resp;
348 	u_int32_t hisr;
349 
350 	resp = &scp->res;
351 
352 	/* Is this interrupt for us? */
353 	hisr = csa_readio(resp, BA0_HISR);
354 	if ((hisr & ~HISR_INTENA) == 0) {
355 		/* Throw an eoi. */
356 		csa_writeio(resp, BA0_HICR, HICR_IEV | HICR_CHGM);
357 		return;
358 	}
359 
360 	/*
361 	 * Pass the value of HISR via struct csa_bridgeinfo.
362 	 * The children get access through their ivars.
363 	 */
364 	scp->binfo.hisr = hisr;
365 
366 	/* Invoke the handlers of the children. */
367 	if ((hisr & (HISR_VC0 | HISR_VC1)) != 0 && scp->pcmintr != NULL)
368 		scp->pcmintr(scp->pcmintr_arg);
369 	if ((hisr & HISR_MIDI) != 0 && scp->midiintr != NULL)
370 		scp->midiintr(scp->midiintr_arg);
371 
372 	/* Throw an eoi. */
373 	csa_writeio(resp, BA0_HICR, HICR_IEV | HICR_CHGM);
374 }
375 
376 static int
377 csa_initialize(sc_p scp)
378 {
379 	int i;
380 	u_int32_t acsts, acisv;
381 	csa_res *resp;
382 
383 	resp = &scp->res;
384 
385 	/*
386 	 * First, blast the clock control register to zero so that the PLL starts
387 	 * out in a known state, and blast the master serial port control register
388 	 * to zero so that the serial ports also start out in a known state.
389 	 */
390 	csa_writeio(resp, BA0_CLKCR1, 0);
391 	csa_writeio(resp, BA0_SERMC1, 0);
392 
393 	/*
394 	 * If we are in AC97 mode, then we must set the part to a host controlled
395 	 * AC-link.  Otherwise, we won't be able to bring up the link.
396 	 */
397 #if 1
398 	csa_writeio(resp, BA0_SERACC, SERACC_HSP | SERACC_CODEC_TYPE_1_03); /* 1.03 codec */
399 #else
400 	csa_writeio(resp, BA0_SERACC, SERACC_HSP | SERACC_CODEC_TYPE_2_0); /* 2.0 codec */
401 #endif /* 1 */
402 
403 	/*
404 	 * Drive the ARST# pin low for a minimum of 1uS (as defined in the AC97
405 	 * spec) and then drive it high.  This is done for non AC97 modes since
406 	 * there might be logic external to the CS461x that uses the ARST# line
407 	 * for a reset.
408 	 */
409 	csa_writeio(resp, BA0_ACCTL, 0);
410 	DELAY(100);
411 	csa_writeio(resp, BA0_ACCTL, ACCTL_RSTN);
412 
413 	/*
414 	 * The first thing we do here is to enable sync generation.  As soon
415 	 * as we start receiving bit clock, we'll start producing the SYNC
416 	 * signal.
417 	 */
418 	csa_writeio(resp, BA0_ACCTL, ACCTL_ESYN | ACCTL_RSTN);
419 
420 	/*
421 	 * Now wait for a short while to allow the AC97 part to start
422 	 * generating bit clock (so we don't try to start the PLL without an
423 	 * input clock).
424 	 */
425 	DELAY(50000);
426 
427 	/*
428 	 * Set the serial port timing configuration, so that
429 	 * the clock control circuit gets its clock from the correct place.
430 	 */
431 	csa_writeio(resp, BA0_SERMC1, SERMC1_PTC_AC97);
432 
433 	/*
434 	 * Write the selected clock control setup to the hardware.  Do not turn on
435 	 * SWCE yet (if requested), so that the devices clocked by the output of
436 	 * PLL are not clocked until the PLL is stable.
437 	 */
438 	csa_writeio(resp, BA0_PLLCC, PLLCC_LPF_1050_2780_KHZ | PLLCC_CDR_73_104_MHZ);
439 	csa_writeio(resp, BA0_PLLM, 0x3a);
440 	csa_writeio(resp, BA0_CLKCR2, CLKCR2_PDIVS_8);
441 
442 	/*
443 	 * Power up the PLL.
444 	 */
445 	csa_writeio(resp, BA0_CLKCR1, CLKCR1_PLLP);
446 
447 	/*
448 	 * Wait until the PLL has stabilized.
449 	 */
450 	DELAY(50000);
451 
452 	/*
453 	 * Turn on clocking of the core so that we can setup the serial ports.
454 	 */
455 	csa_writeio(resp, BA0_CLKCR1, csa_readio(resp, BA0_CLKCR1) | CLKCR1_SWCE);
456 
457 	/*
458 	 * Fill the serial port FIFOs with silence.
459 	 */
460 	csa_clearserialfifos(resp);
461 
462 	/*
463 	 * Set the serial port FIFO pointer to the first sample in the FIFO.
464 	 */
465 #if notdef
466 	csa_writeio(resp, BA0_SERBSP, 0);
467 #endif /* notdef */
468 
469 	/*
470 	 *  Write the serial port configuration to the part.  The master
471 	 *  enable bit is not set until all other values have been written.
472 	 */
473 	csa_writeio(resp, BA0_SERC1, SERC1_SO1F_AC97 | SERC1_SO1EN);
474 	csa_writeio(resp, BA0_SERC2, SERC2_SI1F_AC97 | SERC1_SO1EN);
475 	csa_writeio(resp, BA0_SERMC1, SERMC1_PTC_AC97 | SERMC1_MSPE);
476 
477 	/*
478 	 * Wait for the codec ready signal from the AC97 codec.
479 	 */
480 	acsts = 0;
481 	for (i = 0 ; i < 1000 ; i++) {
482 		/*
483 		 * First, lets wait a short while to let things settle out a bit,
484 		 * and to prevent retrying the read too quickly.
485 		 */
486 		DELAY(125);
487 
488 		/*
489 		 * Read the AC97 status register to see if we've seen a CODEC READY
490 		 * signal from the AC97 codec.
491 		 */
492 		acsts = csa_readio(resp, BA0_ACSTS);
493 		if ((acsts & ACSTS_CRDY) != 0)
494 			break;
495 	}
496 
497 	/*
498 	 * Make sure we sampled CODEC READY.
499 	 */
500 	if ((acsts & ACSTS_CRDY) == 0)
501 		return (ENXIO);
502 
503 	/*
504 	 * Assert the vaid frame signal so that we can start sending commands
505 	 * to the AC97 codec.
506 	 */
507 	csa_writeio(resp, BA0_ACCTL, ACCTL_VFRM | ACCTL_ESYN | ACCTL_RSTN);
508 
509 	/*
510 	 * Wait until we've sampled input slots 3 and 4 as valid, meaning that
511 	 * the codec is pumping ADC data across the AC-link.
512 	 */
513 	acisv = 0;
514 	for (i = 0 ; i < 1000 ; i++) {
515 		/*
516 		 * First, lets wait a short while to let things settle out a bit,
517 		 * and to prevent retrying the read too quickly.
518 		 */
519 #if notdef
520 		DELAY(10000000L); /* clw */
521 #else
522 		DELAY(1000);
523 #endif /* notdef */
524 		/*
525 		 * Read the input slot valid register and see if input slots 3 and
526 		 * 4 are valid yet.
527 		 */
528 		acisv = csa_readio(resp, BA0_ACISV);
529 		if ((acisv & (ACISV_ISV3 | ACISV_ISV4)) == (ACISV_ISV3 | ACISV_ISV4))
530 			break;
531 	}
532 	/*
533 	 * Make sure we sampled valid input slots 3 and 4.  If not, then return
534 	 * an error.
535 	 */
536 	if ((acisv & (ACISV_ISV3 | ACISV_ISV4)) != (ACISV_ISV3 | ACISV_ISV4))
537 		return (ENXIO);
538 
539 	/*
540 	 * Now, assert valid frame and the slot 3 and 4 valid bits.  This will
541 	 * commense the transfer of digital audio data to the AC97 codec.
542 	 */
543 	csa_writeio(resp, BA0_ACOSV, ACOSV_SLV3 | ACOSV_SLV4);
544 
545 	/*
546 	 * Power down the DAC and ADC.  We will power them up (if) when we need
547 	 * them.
548 	 */
549 #if notdef
550 	csa_writeio(resp, BA0_AC97_POWERDOWN, 0x300);
551 #endif /* notdef */
552 
553 	/*
554 	 * Turn off the Processor by turning off the software clock enable flag in
555 	 * the clock control register.
556 	 */
557 #if notdef
558 	clkcr1 = csa_readio(resp, BA0_CLKCR1) & ~CLKCR1_SWCE;
559 	csa_writeio(resp, BA0_CLKCR1, clkcr1);
560 #endif /* notdef */
561 
562 	/*
563 	 * Enable interrupts on the part.
564 	 */
565 #if notdef
566 	csa_writeio(resp, BA0_HICR, HICR_IEV | HICR_CHGM);
567 #endif /* notdef */
568 
569 	return (0);
570 }
571 
572 void
573 csa_clearserialfifos(csa_res *resp)
574 {
575 	int i, j, pwr;
576 	u_int8_t clkcr1, serbst;
577 
578 	/*
579 	 * See if the devices are powered down.  If so, we must power them up first
580 	 * or they will not respond.
581 	 */
582 	pwr = 1;
583 	clkcr1 = csa_readio(resp, BA0_CLKCR1);
584 	if ((clkcr1 & CLKCR1_SWCE) == 0) {
585 		csa_writeio(resp, BA0_CLKCR1, clkcr1 | CLKCR1_SWCE);
586 		pwr = 0;
587 	}
588 
589 	/*
590 	 * We want to clear out the serial port FIFOs so we don't end up playing
591 	 * whatever random garbage happens to be in them.  We fill the sample FIFOs
592 	 * with zero (silence).
593 	 */
594 	csa_writeio(resp, BA0_SERBWP, 0);
595 
596 	/* Fill all 256 sample FIFO locations. */
597 	serbst = 0;
598 	for (i = 0 ; i < 256 ; i++) {
599 		/* Make sure the previous FIFO write operation has completed. */
600 		for (j = 0 ; j < 5 ; j++) {
601 			DELAY(100);
602 			serbst = csa_readio(resp, BA0_SERBST);
603 			if ((serbst & SERBST_WBSY) == 0)
604 				break;
605 		}
606 		if ((serbst & SERBST_WBSY) != 0) {
607 			if (!pwr)
608 				csa_writeio(resp, BA0_CLKCR1, clkcr1);
609 		}
610 		/* Write the serial port FIFO index. */
611 		csa_writeio(resp, BA0_SERBAD, i);
612 		/* Tell the serial port to load the new value into the FIFO location. */
613 		csa_writeio(resp, BA0_SERBCM, SERBCM_WRC);
614 	}
615 	/*
616 	 *  Now, if we powered up the devices, then power them back down again.
617 	 *  This is kinda ugly, but should never happen.
618 	 */
619 	if (!pwr)
620 		csa_writeio(resp, BA0_CLKCR1, clkcr1);
621 }
622 
623 static void
624 csa_resetdsp(csa_res *resp)
625 {
626 	int i;
627 
628 	/*
629 	 * Write the reset bit of the SP control register.
630 	 */
631 	csa_writemem(resp, BA1_SPCR, SPCR_RSTSP);
632 
633 	/*
634 	 * Write the control register.
635 	 */
636 	csa_writemem(resp, BA1_SPCR, SPCR_DRQEN);
637 
638 	/*
639 	 * Clear the trap registers.
640 	 */
641 	for (i = 0 ; i < 8 ; i++) {
642 		csa_writemem(resp, BA1_DREG, DREG_REGID_TRAP_SELECT + i);
643 		csa_writemem(resp, BA1_TWPR, 0xffff);
644 	}
645 	csa_writemem(resp, BA1_DREG, 0);
646 
647 	/*
648 	 * Set the frame timer to reflect the number of cycles per frame.
649 	 */
650 	csa_writemem(resp, BA1_FRMT, 0xadf);
651 }
652 
653 static int
654 csa_downloadimage(csa_res *resp)
655 {
656 	int ret;
657 	u_long ul, offset;
658 
659 	for (ul = 0, offset = 0 ; ul < INKY_MEMORY_COUNT ; ul++) {
660 		/*
661 		 * DMA this block from host memory to the appropriate
662 		 * memory on the CSDevice.
663 		 */
664 		ret = csa_transferimage(
665 			resp,
666 			BA1Struct.BA1Array + offset,
667 			BA1Struct.MemoryStat[ul].ulDestByteOffset,
668 			BA1Struct.MemoryStat[ul].ulSourceByteSize);
669 		if (ret)
670 			return (ret);
671 		offset += BA1Struct.MemoryStat[ul].ulSourceByteSize >> 2;
672 	}
673 
674 	return (0);
675 }
676 
677 static int
678 csa_transferimage(csa_res *resp, u_long *src, u_long dest, u_long len)
679 {
680 	u_long ul;
681 
682 	/*
683 	 * We do not allow DMAs from host memory to host memory (although the DMA
684 	 * can do it) and we do not allow DMAs which are not a multiple of 4 bytes
685 	 * in size (because that DMA can not do that).  Return an error if either
686 	 * of these conditions exist.
687 	 */
688 	if ((len & 0x3) != 0)
689 		return (EINVAL);
690 
691 	/* Check the destination address that it is a multiple of 4 */
692 	if ((dest & 0x3) != 0)
693 		return (EINVAL);
694 
695 	/* Write the buffer out. */
696 	for (ul = 0 ; ul < len ; ul += 4)
697 		csa_writemem(resp, dest + ul, src[ul >> 2]);
698 
699 	return (0);
700 }
701 
702 int
703 csa_readcodec(csa_res *resp, u_long offset, u_int32_t *data)
704 {
705 	int i;
706 	u_int32_t acsda, acctl, acsts;
707 
708 	/*
709 	 * Make sure that there is not data sitting around from a previous
710 	 * uncompleted access. ACSDA = Status Data Register = 47Ch
711 	 */
712 	acsda = csa_readio(resp, BA0_ACSDA);
713 
714 	/*
715 	 * Setup the AC97 control registers on the CS461x to send the
716 	 * appropriate command to the AC97 to perform the read.
717 	 * ACCAD = Command Address Register = 46Ch
718 	 * ACCDA = Command Data Register = 470h
719 	 * ACCTL = Control Register = 460h
720 	 * set DCV - will clear when process completed
721 	 * set CRW - Read command
722 	 * set VFRM - valid frame enabled
723 	 * set ESYN - ASYNC generation enabled
724 	 * set RSTN - ARST# inactive, AC97 codec not reset
725 	 */
726 
727 	/*
728 	 * Get the actual AC97 register from the offset
729 	 */
730 	csa_writeio(resp, BA0_ACCAD, offset - BA0_AC97_RESET);
731 	csa_writeio(resp, BA0_ACCDA, 0);
732 	csa_writeio(resp, BA0_ACCTL, ACCTL_DCV | ACCTL_CRW | ACCTL_VFRM | ACCTL_ESYN | ACCTL_RSTN);
733 
734 	/*
735 	 * Wait for the read to occur.
736 	 */
737 	acctl = 0;
738 	for (i = 0 ; i < 10 ; i++) {
739 		/*
740 		 * First, we want to wait for a short time.
741 		 */
742 		DELAY(25);
743 
744 		/*
745 		 * Now, check to see if the read has completed.
746 		 * ACCTL = 460h, DCV should be reset by now and 460h = 17h
747 		 */
748 		acctl = csa_readio(resp, BA0_ACCTL);
749 		if ((acctl & ACCTL_DCV) == 0)
750 			break;
751 	}
752 
753 	/*
754 	 * Make sure the read completed.
755 	 */
756 	if ((acctl & ACCTL_DCV) != 0)
757 		return (EAGAIN);
758 
759 	/*
760 	 * Wait for the valid status bit to go active.
761 	 */
762 	acsts = 0;
763 	for (i = 0 ; i < 10 ; i++) {
764 		/*
765 		 * Read the AC97 status register.
766 		 * ACSTS = Status Register = 464h
767 		 */
768 		acsts = csa_readio(resp, BA0_ACSTS);
769 		/*
770 		 * See if we have valid status.
771 		 * VSTS - Valid Status
772 		 */
773 		if ((acsts & ACSTS_VSTS) != 0)
774 			break;
775 		/*
776 		 * Wait for a short while.
777 		 */
778 		 DELAY(25);
779 	}
780 
781 	/*
782 	 * Make sure we got valid status.
783 	 */
784 	if ((acsts & ACSTS_VSTS) == 0)
785 		return (EAGAIN);
786 
787 	/*
788 	 * Read the data returned from the AC97 register.
789 	 * ACSDA = Status Data Register = 474h
790 	 */
791 	*data = csa_readio(resp, BA0_ACSDA);
792 
793 	return (0);
794 }
795 
796 int
797 csa_writecodec(csa_res *resp, u_long offset, u_int32_t data)
798 {
799 	int i;
800 	u_int32_t acctl;
801 
802 	/*
803 	 * Setup the AC97 control registers on the CS461x to send the
804 	 * appropriate command to the AC97 to perform the write.
805 	 * ACCAD = Command Address Register = 46Ch
806 	 * ACCDA = Command Data Register = 470h
807 	 * ACCTL = Control Register = 460h
808 	 * set DCV - will clear when process completed
809 	 * set VFRM - valid frame enabled
810 	 * set ESYN - ASYNC generation enabled
811 	 * set RSTN - ARST# inactive, AC97 codec not reset
812 	 */
813 
814 	/*
815 	 * Get the actual AC97 register from the offset
816 	 */
817 	csa_writeio(resp, BA0_ACCAD, offset - BA0_AC97_RESET);
818 	csa_writeio(resp, BA0_ACCDA, data);
819 	csa_writeio(resp, BA0_ACCTL, ACCTL_DCV | ACCTL_VFRM | ACCTL_ESYN | ACCTL_RSTN);
820 
821 	/*
822 	 * Wait for the write to occur.
823 	 */
824 	acctl = 0;
825 	for (i = 0 ; i < 10 ; i++) {
826 		/*
827 		 * First, we want to wait for a short time.
828 		 */
829 		DELAY(25);
830 
831 		/*
832 		 * Now, check to see if the read has completed.
833 		 * ACCTL = 460h, DCV should be reset by now and 460h = 17h
834 		 */
835 		acctl = csa_readio(resp, BA0_ACCTL);
836 		if ((acctl & ACCTL_DCV) == 0)
837 			break;
838 	}
839 
840 	/*
841 	 * Make sure the write completed.
842 	 */
843 	if ((acctl & ACCTL_DCV) != 0)
844 		return (EAGAIN);
845 
846 	return (0);
847 }
848 
849 u_int32_t
850 csa_readio(csa_res *resp, u_long offset)
851 {
852 	u_int32_t ul;
853 
854 	if (offset < BA0_AC97_RESET)
855 		return bus_space_read_4(rman_get_bustag(resp->io), rman_get_bushandle(resp->io), offset) & 0xffffffff;
856 	else {
857 		if (csa_readcodec(resp, offset, &ul))
858 			ul = 0;
859 		return (ul);
860 	}
861 }
862 
863 void
864 csa_writeio(csa_res *resp, u_long offset, u_int32_t data)
865 {
866 	if (offset < BA0_AC97_RESET)
867 		bus_space_write_4(rman_get_bustag(resp->io), rman_get_bushandle(resp->io), offset, data);
868 	else
869 		csa_writecodec(resp, offset, data);
870 }
871 
872 u_int32_t
873 csa_readmem(csa_res *resp, u_long offset)
874 {
875 	return bus_space_read_4(rman_get_bustag(resp->mem), rman_get_bushandle(resp->mem), offset) & 0xffffffff;
876 }
877 
878 void
879 csa_writemem(csa_res *resp, u_long offset, u_int32_t data)
880 {
881 	bus_space_write_4(rman_get_bustag(resp->mem), rman_get_bushandle(resp->mem), offset, data);
882 }
883 
884 static device_method_t csa_methods[] = {
885 	/* Device interface */
886 	DEVMETHOD(device_probe,		csa_probe),
887 	DEVMETHOD(device_attach,	csa_attach),
888 	DEVMETHOD(device_detach,	bus_generic_detach),
889 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
890 	DEVMETHOD(device_suspend,	bus_generic_suspend),
891 	DEVMETHOD(device_resume,	bus_generic_resume),
892 
893 	/* Bus interface */
894 	DEVMETHOD(bus_print_child,	bus_generic_print_child),
895 	DEVMETHOD(bus_alloc_resource,	csa_alloc_resource),
896 	DEVMETHOD(bus_release_resource,	csa_release_resource),
897 	DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
898 	DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
899 	DEVMETHOD(bus_setup_intr,	csa_setup_intr),
900 	DEVMETHOD(bus_teardown_intr,	csa_teardown_intr),
901 
902 	{ 0, 0 }
903 };
904 
905 static driver_t csa_driver = {
906 	"csa",
907 	csa_methods,
908 	sizeof(struct csa_softc),
909 };
910 
911 /*
912  * csa can be attached to a pci bus.
913  */
914 DRIVER_MODULE(snd_csa, pci, csa_driver, csa_devclass, 0, 0);
915 MODULE_DEPEND(snd_csa, snd_pcm, PCM_MINVER, PCM_PREFVER, PCM_MAXVER);
916 MODULE_VERSION(snd_csa, 1);
917