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