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