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