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