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, int type, int rid,
86 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
amp_none(void)99 amp_none(void)
100 {
101 }
102
103 static void
amp_voyetra(void)104 amp_voyetra(void)
105 {
106 }
107
108 static int
clkrun_hack(int run)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 *
csa_findcard(device_t dev)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 *
csa_findsubcard(device_t dev)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
csa_probe(device_t dev)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
csa_attach(device_t dev)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
csa_detach(device_t dev)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
csa_resume(device_t dev)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 *
csa_alloc_resource(device_t bus,device_t child,int type,int * rid,rman_res_t start,rman_res_t end,rman_res_t count,u_int flags)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
csa_release_resource(device_t bus,device_t child,int type,int rid,struct resource * r)432 csa_release_resource(device_t bus, device_t child, int type, int rid,
433 struct resource *r)
434 {
435 return (0);
436 }
437
438 /*
439 * The following three functions deal with interrupt handling.
440 * An interrupt is primarily handled by the bridge driver.
441 * The bridge driver then determines the child devices to pass
442 * the interrupt. Certain information of the device can be read
443 * only once(eg the value of HISR). The bridge driver is responsible
444 * to pass such the information to the children.
445 */
446
447 static int
csa_setup_intr(device_t bus,device_t child,struct resource * irq,int flags,driver_filter_t * filter,driver_intr_t * intr,void * arg,void ** cookiep)448 csa_setup_intr(device_t bus, device_t child,
449 struct resource *irq, int flags,
450 driver_filter_t *filter,
451 driver_intr_t *intr, void *arg, void **cookiep)
452 {
453 sc_p scp;
454 csa_res *resp;
455 struct sndcard_func *func;
456
457 if (filter != NULL) {
458 printf("ata-csa.c: we cannot use a filter here\n");
459 return (EINVAL);
460 }
461 scp = device_get_softc(bus);
462 resp = &scp->res;
463
464 /*
465 * Look at the function code of the child to determine
466 * the appropriate handler for it.
467 */
468 func = device_get_ivars(child);
469 if (func == NULL || irq != resp->irq)
470 return (EINVAL);
471
472 switch (func->func) {
473 case SCF_PCM:
474 scp->pcmintr = intr;
475 scp->pcmintr_arg = arg;
476 break;
477
478 case SCF_MIDI:
479 scp->midiintr = intr;
480 scp->midiintr_arg = arg;
481 break;
482
483 default:
484 return (EINVAL);
485 }
486 *cookiep = scp;
487 if ((csa_readio(resp, BA0_HISR) & HISR_INTENA) == 0)
488 csa_writeio(resp, BA0_HICR, HICR_IEV | HICR_CHGM);
489
490 return (0);
491 }
492
493 static int
csa_teardown_intr(device_t bus,device_t child,struct resource * irq,void * cookie)494 csa_teardown_intr(device_t bus, device_t child,
495 struct resource *irq, void *cookie)
496 {
497 sc_p scp;
498 csa_res *resp;
499 struct sndcard_func *func;
500
501 scp = device_get_softc(bus);
502 resp = &scp->res;
503
504 /*
505 * Look at the function code of the child to determine
506 * the appropriate handler for it.
507 */
508 func = device_get_ivars(child);
509 if (func == NULL || irq != resp->irq || cookie != scp)
510 return (EINVAL);
511
512 switch (func->func) {
513 case SCF_PCM:
514 scp->pcmintr = NULL;
515 scp->pcmintr_arg = NULL;
516 break;
517
518 case SCF_MIDI:
519 scp->midiintr = NULL;
520 scp->midiintr_arg = NULL;
521 break;
522
523 default:
524 return (EINVAL);
525 }
526
527 return (0);
528 }
529
530 /* The interrupt handler */
531 static void
csa_intr(void * arg)532 csa_intr(void *arg)
533 {
534 sc_p scp = arg;
535 csa_res *resp;
536 u_int32_t hisr;
537
538 resp = &scp->res;
539
540 /* Is this interrupt for us? */
541 hisr = csa_readio(resp, BA0_HISR);
542 if ((hisr & 0x7fffffff) == 0) {
543 /* Throw an eoi. */
544 csa_writeio(resp, BA0_HICR, HICR_IEV | HICR_CHGM);
545 return;
546 }
547
548 /*
549 * Pass the value of HISR via struct csa_bridgeinfo.
550 * The children get access through their ivars.
551 */
552 scp->binfo.hisr = hisr;
553
554 /* Invoke the handlers of the children. */
555 if ((hisr & (HISR_VC0 | HISR_VC1)) != 0 && scp->pcmintr != NULL) {
556 scp->pcmintr(scp->pcmintr_arg);
557 hisr &= ~(HISR_VC0 | HISR_VC1);
558 }
559 if ((hisr & HISR_MIDI) != 0 && scp->midiintr != NULL) {
560 scp->midiintr(scp->midiintr_arg);
561 hisr &= ~HISR_MIDI;
562 }
563
564 /* Throw an eoi. */
565 csa_writeio(resp, BA0_HICR, HICR_IEV | HICR_CHGM);
566 }
567
568 static int
csa_initialize(sc_p scp)569 csa_initialize(sc_p scp)
570 {
571 int i;
572 u_int32_t acsts, acisv;
573 csa_res *resp;
574
575 resp = &scp->res;
576
577 /*
578 * First, blast the clock control register to zero so that the PLL starts
579 * out in a known state, and blast the master serial port control register
580 * to zero so that the serial ports also start out in a known state.
581 */
582 csa_writeio(resp, BA0_CLKCR1, 0);
583 csa_writeio(resp, BA0_SERMC1, 0);
584
585 /*
586 * If we are in AC97 mode, then we must set the part to a host controlled
587 * AC-link. Otherwise, we won't be able to bring up the link.
588 */
589 #if 1
590 csa_writeio(resp, BA0_SERACC, SERACC_HSP | SERACC_CODEC_TYPE_1_03); /* 1.03 codec */
591 #else
592 csa_writeio(resp, BA0_SERACC, SERACC_HSP | SERACC_CODEC_TYPE_2_0); /* 2.0 codec */
593 #endif /* 1 */
594
595 /*
596 * Drive the ARST# pin low for a minimum of 1uS (as defined in the AC97
597 * spec) and then drive it high. This is done for non AC97 modes since
598 * there might be logic external to the CS461x that uses the ARST# line
599 * for a reset.
600 */
601 csa_writeio(resp, BA0_ACCTL, 1);
602 DELAY(50);
603 csa_writeio(resp, BA0_ACCTL, 0);
604 DELAY(50);
605 csa_writeio(resp, BA0_ACCTL, ACCTL_RSTN);
606
607 /*
608 * The first thing we do here is to enable sync generation. As soon
609 * as we start receiving bit clock, we'll start producing the SYNC
610 * signal.
611 */
612 csa_writeio(resp, BA0_ACCTL, ACCTL_ESYN | ACCTL_RSTN);
613
614 /*
615 * Now wait for a short while to allow the AC97 part to start
616 * generating bit clock (so we don't try to start the PLL without an
617 * input clock).
618 */
619 DELAY(50000);
620
621 /*
622 * Set the serial port timing configuration, so that
623 * the clock control circuit gets its clock from the correct place.
624 */
625 csa_writeio(resp, BA0_SERMC1, SERMC1_PTC_AC97);
626 DELAY(700000);
627
628 /*
629 * Write the selected clock control setup to the hardware. Do not turn on
630 * SWCE yet (if requested), so that the devices clocked by the output of
631 * PLL are not clocked until the PLL is stable.
632 */
633 csa_writeio(resp, BA0_PLLCC, PLLCC_LPF_1050_2780_KHZ | PLLCC_CDR_73_104_MHZ);
634 csa_writeio(resp, BA0_PLLM, 0x3a);
635 csa_writeio(resp, BA0_CLKCR2, CLKCR2_PDIVS_8);
636
637 /*
638 * Power up the PLL.
639 */
640 csa_writeio(resp, BA0_CLKCR1, CLKCR1_PLLP);
641
642 /*
643 * Wait until the PLL has stabilized.
644 */
645 DELAY(5000);
646
647 /*
648 * Turn on clocking of the core so that we can setup the serial ports.
649 */
650 csa_writeio(resp, BA0_CLKCR1, csa_readio(resp, BA0_CLKCR1) | CLKCR1_SWCE);
651
652 /*
653 * Fill the serial port FIFOs with silence.
654 */
655 csa_clearserialfifos(resp);
656
657 /*
658 * Set the serial port FIFO pointer to the first sample in the FIFO.
659 */
660 #ifdef notdef
661 csa_writeio(resp, BA0_SERBSP, 0);
662 #endif /* notdef */
663
664 /*
665 * Write the serial port configuration to the part. The master
666 * enable bit is not set until all other values have been written.
667 */
668 csa_writeio(resp, BA0_SERC1, SERC1_SO1F_AC97 | SERC1_SO1EN);
669 csa_writeio(resp, BA0_SERC2, SERC2_SI1F_AC97 | SERC1_SO1EN);
670 csa_writeio(resp, BA0_SERMC1, SERMC1_PTC_AC97 | SERMC1_MSPE);
671
672 /*
673 * Wait for the codec ready signal from the AC97 codec.
674 */
675 acsts = 0;
676 for (i = 0 ; i < 1000 ; i++) {
677 /*
678 * First, lets wait a short while to let things settle out a bit,
679 * and to prevent retrying the read too quickly.
680 */
681 DELAY(125);
682
683 /*
684 * Read the AC97 status register to see if we've seen a CODEC READY
685 * signal from the AC97 codec.
686 */
687 acsts = csa_readio(resp, BA0_ACSTS);
688 if ((acsts & ACSTS_CRDY) != 0)
689 break;
690 }
691
692 /*
693 * Make sure we sampled CODEC READY.
694 */
695 if ((acsts & ACSTS_CRDY) == 0)
696 return (ENXIO);
697
698 /*
699 * Assert the vaid frame signal so that we can start sending commands
700 * to the AC97 codec.
701 */
702 csa_writeio(resp, BA0_ACCTL, ACCTL_VFRM | ACCTL_ESYN | ACCTL_RSTN);
703
704 /*
705 * Wait until we've sampled input slots 3 and 4 as valid, meaning that
706 * the codec is pumping ADC data across the AC-link.
707 */
708 acisv = 0;
709 for (i = 0 ; i < 2000 ; i++) {
710 /*
711 * First, lets wait a short while to let things settle out a bit,
712 * and to prevent retrying the read too quickly.
713 */
714 #ifdef notdef
715 DELAY(10000000L); /* clw */
716 #else
717 DELAY(1000);
718 #endif /* notdef */
719 /*
720 * Read the input slot valid register and see if input slots 3 and
721 * 4 are valid yet.
722 */
723 acisv = csa_readio(resp, BA0_ACISV);
724 if ((acisv & (ACISV_ISV3 | ACISV_ISV4)) == (ACISV_ISV3 | ACISV_ISV4))
725 break;
726 }
727 /*
728 * Make sure we sampled valid input slots 3 and 4. If not, then return
729 * an error.
730 */
731 if ((acisv & (ACISV_ISV3 | ACISV_ISV4)) != (ACISV_ISV3 | ACISV_ISV4))
732 return (ENXIO);
733
734 /*
735 * Now, assert valid frame and the slot 3 and 4 valid bits. This will
736 * commense the transfer of digital audio data to the AC97 codec.
737 */
738 csa_writeio(resp, BA0_ACOSV, ACOSV_SLV3 | ACOSV_SLV4);
739
740 /*
741 * Power down the DAC and ADC. We will power them up (if) when we need
742 * them.
743 */
744 #ifdef notdef
745 csa_writeio(resp, BA0_AC97_POWERDOWN, 0x300);
746 #endif /* notdef */
747
748 /*
749 * Turn off the Processor by turning off the software clock enable flag in
750 * the clock control register.
751 */
752 #ifdef notdef
753 clkcr1 = csa_readio(resp, BA0_CLKCR1) & ~CLKCR1_SWCE;
754 csa_writeio(resp, BA0_CLKCR1, clkcr1);
755 #endif /* notdef */
756
757 /*
758 * Enable interrupts on the part.
759 */
760 #if 0
761 csa_writeio(resp, BA0_HICR, HICR_IEV | HICR_CHGM);
762 #endif /* notdef */
763
764 return (0);
765 }
766
767 void
csa_clearserialfifos(csa_res * resp)768 csa_clearserialfifos(csa_res *resp)
769 {
770 int i, j, pwr;
771 u_int8_t clkcr1, serbst;
772
773 /*
774 * See if the devices are powered down. If so, we must power them up first
775 * or they will not respond.
776 */
777 pwr = 1;
778 clkcr1 = csa_readio(resp, BA0_CLKCR1);
779 if ((clkcr1 & CLKCR1_SWCE) == 0) {
780 csa_writeio(resp, BA0_CLKCR1, clkcr1 | CLKCR1_SWCE);
781 pwr = 0;
782 }
783
784 /*
785 * We want to clear out the serial port FIFOs so we don't end up playing
786 * whatever random garbage happens to be in them. We fill the sample FIFOs
787 * with zero (silence).
788 */
789 csa_writeio(resp, BA0_SERBWP, 0);
790
791 /* Fill all 256 sample FIFO locations. */
792 serbst = 0;
793 for (i = 0 ; i < 256 ; i++) {
794 /* Make sure the previous FIFO write operation has completed. */
795 for (j = 0 ; j < 5 ; j++) {
796 DELAY(100);
797 serbst = csa_readio(resp, BA0_SERBST);
798 if ((serbst & SERBST_WBSY) == 0)
799 break;
800 }
801 if ((serbst & SERBST_WBSY) != 0) {
802 if (!pwr)
803 csa_writeio(resp, BA0_CLKCR1, clkcr1);
804 }
805 /* Write the serial port FIFO index. */
806 csa_writeio(resp, BA0_SERBAD, i);
807 /* Tell the serial port to load the new value into the FIFO location. */
808 csa_writeio(resp, BA0_SERBCM, SERBCM_WRC);
809 }
810 /*
811 * Now, if we powered up the devices, then power them back down again.
812 * This is kinda ugly, but should never happen.
813 */
814 if (!pwr)
815 csa_writeio(resp, BA0_CLKCR1, clkcr1);
816 }
817
818 void
csa_resetdsp(csa_res * resp)819 csa_resetdsp(csa_res *resp)
820 {
821 int i;
822
823 /*
824 * Write the reset bit of the SP control register.
825 */
826 csa_writemem(resp, BA1_SPCR, SPCR_RSTSP);
827
828 /*
829 * Write the control register.
830 */
831 csa_writemem(resp, BA1_SPCR, SPCR_DRQEN);
832
833 /*
834 * Clear the trap registers.
835 */
836 for (i = 0 ; i < 8 ; i++) {
837 csa_writemem(resp, BA1_DREG, DREG_REGID_TRAP_SELECT + i);
838 csa_writemem(resp, BA1_TWPR, 0xffff);
839 }
840 csa_writemem(resp, BA1_DREG, 0);
841
842 /*
843 * Set the frame timer to reflect the number of cycles per frame.
844 */
845 csa_writemem(resp, BA1_FRMT, 0xadf);
846 }
847
848 static int
csa_downloadimage(csa_res * resp)849 csa_downloadimage(csa_res *resp)
850 {
851 int ret;
852 u_long ul, offset;
853
854 for (ul = 0, offset = 0 ; ul < INKY_MEMORY_COUNT ; ul++) {
855 /*
856 * DMA this block from host memory to the appropriate
857 * memory on the CSDevice.
858 */
859 ret = csa_transferimage(resp,
860 cs461x_firmware.BA1Array + offset,
861 cs461x_firmware.MemoryStat[ul].ulDestAddr,
862 cs461x_firmware.MemoryStat[ul].ulSourceSize);
863 if (ret)
864 return (ret);
865 offset += cs461x_firmware.MemoryStat[ul].ulSourceSize >> 2;
866 }
867 return (0);
868 }
869
870 static int
csa_transferimage(csa_res * resp,u_int32_t * src,u_long dest,u_long len)871 csa_transferimage(csa_res *resp, u_int32_t *src, u_long dest, u_long len)
872 {
873 u_long ul;
874
875 /*
876 * We do not allow DMAs from host memory to host memory (although the DMA
877 * can do it) and we do not allow DMAs which are not a multiple of 4 bytes
878 * in size (because that DMA can not do that). Return an error if either
879 * of these conditions exist.
880 */
881 if ((len & 0x3) != 0)
882 return (EINVAL);
883
884 /* Check the destination address that it is a multiple of 4 */
885 if ((dest & 0x3) != 0)
886 return (EINVAL);
887
888 /* Write the buffer out. */
889 for (ul = 0 ; ul < len ; ul += 4)
890 csa_writemem(resp, dest + ul, src[ul >> 2]);
891 return (0);
892 }
893
894 int
csa_readcodec(csa_res * resp,u_long offset,u_int32_t * data)895 csa_readcodec(csa_res *resp, u_long offset, u_int32_t *data)
896 {
897 int i;
898 u_int32_t acctl, acsts;
899
900 /*
901 * Make sure that there is not data sitting around from a previous
902 * uncompleted access. ACSDA = Status Data Register = 47Ch
903 */
904 csa_readio(resp, BA0_ACSDA);
905
906 /*
907 * Setup the AC97 control registers on the CS461x to send the
908 * appropriate command to the AC97 to perform the read.
909 * ACCAD = Command Address Register = 46Ch
910 * ACCDA = Command Data Register = 470h
911 * ACCTL = Control Register = 460h
912 * set DCV - will clear when process completed
913 * set CRW - Read command
914 * set VFRM - valid frame enabled
915 * set ESYN - ASYNC generation enabled
916 * set RSTN - ARST# inactive, AC97 codec not reset
917 */
918
919 /*
920 * Get the actual AC97 register from the offset
921 */
922 csa_writeio(resp, BA0_ACCAD, offset - BA0_AC97_RESET);
923 csa_writeio(resp, BA0_ACCDA, 0);
924 csa_writeio(resp, BA0_ACCTL, ACCTL_DCV | ACCTL_CRW | ACCTL_VFRM | ACCTL_ESYN | ACCTL_RSTN);
925
926 /*
927 * Wait for the read to occur.
928 */
929 acctl = 0;
930 for (i = 0 ; i < 10 ; i++) {
931 /*
932 * First, we want to wait for a short time.
933 */
934 DELAY(25);
935
936 /*
937 * Now, check to see if the read has completed.
938 * ACCTL = 460h, DCV should be reset by now and 460h = 17h
939 */
940 acctl = csa_readio(resp, BA0_ACCTL);
941 if ((acctl & ACCTL_DCV) == 0)
942 break;
943 }
944
945 /*
946 * Make sure the read completed.
947 */
948 if ((acctl & ACCTL_DCV) != 0)
949 return (EAGAIN);
950
951 /*
952 * Wait for the valid status bit to go active.
953 */
954 acsts = 0;
955 for (i = 0 ; i < 10 ; i++) {
956 /*
957 * Read the AC97 status register.
958 * ACSTS = Status Register = 464h
959 */
960 acsts = csa_readio(resp, BA0_ACSTS);
961 /*
962 * See if we have valid status.
963 * VSTS - Valid Status
964 */
965 if ((acsts & ACSTS_VSTS) != 0)
966 break;
967 /*
968 * Wait for a short while.
969 */
970 DELAY(25);
971 }
972
973 /*
974 * Make sure we got valid status.
975 */
976 if ((acsts & ACSTS_VSTS) == 0)
977 return (EAGAIN);
978
979 /*
980 * Read the data returned from the AC97 register.
981 * ACSDA = Status Data Register = 474h
982 */
983 *data = csa_readio(resp, BA0_ACSDA);
984
985 return (0);
986 }
987
988 int
csa_writecodec(csa_res * resp,u_long offset,u_int32_t data)989 csa_writecodec(csa_res *resp, u_long offset, u_int32_t data)
990 {
991 int i;
992 u_int32_t acctl;
993
994 /*
995 * Setup the AC97 control registers on the CS461x to send the
996 * appropriate command to the AC97 to perform the write.
997 * ACCAD = Command Address Register = 46Ch
998 * ACCDA = Command Data Register = 470h
999 * ACCTL = Control Register = 460h
1000 * set DCV - will clear when process completed
1001 * set VFRM - valid frame enabled
1002 * set ESYN - ASYNC generation enabled
1003 * set RSTN - ARST# inactive, AC97 codec not reset
1004 */
1005
1006 /*
1007 * Get the actual AC97 register from the offset
1008 */
1009 csa_writeio(resp, BA0_ACCAD, offset - BA0_AC97_RESET);
1010 csa_writeio(resp, BA0_ACCDA, data);
1011 csa_writeio(resp, BA0_ACCTL, ACCTL_DCV | ACCTL_VFRM | ACCTL_ESYN | ACCTL_RSTN);
1012
1013 /*
1014 * Wait for the write to occur.
1015 */
1016 acctl = 0;
1017 for (i = 0 ; i < 10 ; i++) {
1018 /*
1019 * First, we want to wait for a short time.
1020 */
1021 DELAY(25);
1022
1023 /*
1024 * Now, check to see if the read has completed.
1025 * ACCTL = 460h, DCV should be reset by now and 460h = 17h
1026 */
1027 acctl = csa_readio(resp, BA0_ACCTL);
1028 if ((acctl & ACCTL_DCV) == 0)
1029 break;
1030 }
1031
1032 /*
1033 * Make sure the write completed.
1034 */
1035 if ((acctl & ACCTL_DCV) != 0)
1036 return (EAGAIN);
1037
1038 return (0);
1039 }
1040
1041 u_int32_t
csa_readio(csa_res * resp,u_long offset)1042 csa_readio(csa_res *resp, u_long offset)
1043 {
1044 u_int32_t ul;
1045
1046 if (offset < BA0_AC97_RESET)
1047 return bus_space_read_4(rman_get_bustag(resp->io), rman_get_bushandle(resp->io), offset) & 0xffffffff;
1048 else {
1049 if (csa_readcodec(resp, offset, &ul))
1050 ul = 0;
1051 return (ul);
1052 }
1053 }
1054
1055 void
csa_writeio(csa_res * resp,u_long offset,u_int32_t data)1056 csa_writeio(csa_res *resp, u_long offset, u_int32_t data)
1057 {
1058 if (offset < BA0_AC97_RESET)
1059 bus_space_write_4(rman_get_bustag(resp->io), rman_get_bushandle(resp->io), offset, data);
1060 else
1061 csa_writecodec(resp, offset, data);
1062 }
1063
1064 u_int32_t
csa_readmem(csa_res * resp,u_long offset)1065 csa_readmem(csa_res *resp, u_long offset)
1066 {
1067 return bus_space_read_4(rman_get_bustag(resp->mem), rman_get_bushandle(resp->mem), offset);
1068 }
1069
1070 void
csa_writemem(csa_res * resp,u_long offset,u_int32_t data)1071 csa_writemem(csa_res *resp, u_long offset, u_int32_t data)
1072 {
1073 bus_space_write_4(rman_get_bustag(resp->mem), rman_get_bushandle(resp->mem), offset, data);
1074 }
1075
1076 static device_method_t csa_methods[] = {
1077 /* Device interface */
1078 DEVMETHOD(device_probe, csa_probe),
1079 DEVMETHOD(device_attach, csa_attach),
1080 DEVMETHOD(device_detach, csa_detach),
1081 DEVMETHOD(device_shutdown, bus_generic_shutdown),
1082 DEVMETHOD(device_suspend, bus_generic_suspend),
1083 DEVMETHOD(device_resume, csa_resume),
1084
1085 /* Bus interface */
1086 DEVMETHOD(bus_alloc_resource, csa_alloc_resource),
1087 DEVMETHOD(bus_release_resource, csa_release_resource),
1088 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
1089 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
1090 DEVMETHOD(bus_setup_intr, csa_setup_intr),
1091 DEVMETHOD(bus_teardown_intr, csa_teardown_intr),
1092
1093 DEVMETHOD_END
1094 };
1095
1096 static driver_t csa_driver = {
1097 "csa",
1098 csa_methods,
1099 sizeof(struct csa_softc),
1100 };
1101
1102 /*
1103 * csa can be attached to a pci bus.
1104 */
1105 DRIVER_MODULE(snd_csa, pci, csa_driver, 0, 0);
1106 MODULE_DEPEND(snd_csa, sound, SOUND_MINVER, SOUND_PREFVER, SOUND_MAXVER);
1107 MODULE_VERSION(snd_csa, 1);
1108