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