cs428x.c revision 1.2.4.1 1 /* $NetBSD: cs428x.c,v 1.2.4.1 2002/01/10 19:56:30 thorpej Exp $ */
2
3 /*
4 * Copyright (c) 2000 Tatoku Ogaito. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by Tatoku Ogaito
17 * for the NetBSD Project.
18 * 4. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 /* Common functions for CS4280 and CS4281 */
34
35 #include <sys/cdefs.h>
36 __KERNEL_RCSID(0, "$NetBSD: cs428x.c,v 1.2.4.1 2002/01/10 19:56:30 thorpej Exp $");
37
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/kernel.h>
41 #include <sys/malloc.h>
42 #include <sys/device.h>
43
44 #include <dev/pci/pcidevs.h>
45 #include <dev/pci/pcivar.h>
46
47 #include <sys/audioio.h>
48 #include <dev/audio_if.h>
49 #include <dev/midi_if.h>
50 #include <dev/mulaw.h>
51 #include <dev/auconv.h>
52
53 #include <dev/ic/ac97reg.h>
54 #include <dev/ic/ac97var.h>
55
56 #include <machine/bus.h>
57
58 #include <dev/pci/cs428xreg.h>
59 #include <dev/pci/cs428x.h>
60
61 #if defined(CS4280_DEBUG) || defined(CS4281_DEBUG)
62 int cs428x_debug = 0;
63 #endif
64
65 int
66 cs428x_open(void *addr, int flags)
67 {
68 return 0;
69 }
70
71 void
72 cs428x_close(void *addr)
73 {
74 struct cs428x_softc *sc;
75
76 sc = addr;
77
78 (*sc->halt_output)(sc);
79 (*sc->halt_input)(sc);
80
81 sc->sc_pintr = 0;
82 sc->sc_rintr = 0;
83 }
84
85 int
86 cs428x_round_blocksize(void *addr, int blk)
87 {
88 struct cs428x_softc *sc;
89 int retval;
90
91 DPRINTFN(5,("cs428x_round_blocksize blk=%d -> ", blk));
92
93 sc = addr;
94 if (blk < sc->hw_blocksize)
95 retval = sc->hw_blocksize;
96 else
97 retval = blk & -(sc->hw_blocksize);
98
99 DPRINTFN(5,("%d\n", retval));
100
101 return retval;
102 }
103
104 int
105 cs428x_mixer_set_port(void *addr, mixer_ctrl_t *cp)
106 {
107 struct cs428x_softc *sc;
108 int val;
109
110 sc = addr;
111 val = sc->codec_if->vtbl->mixer_set_port(sc->codec_if, cp);
112 DPRINTFN(3,("mixer_set_port: val=%d\n", val));
113 return (val);
114 }
115
116 int
117 cs428x_mixer_get_port(void *addr, mixer_ctrl_t *cp)
118 {
119 struct cs428x_softc *sc;
120
121 sc = addr;
122 return (sc->codec_if->vtbl->mixer_get_port(sc->codec_if, cp));
123 }
124
125 int
126 cs428x_query_devinfo(void *addr, mixer_devinfo_t *dip)
127 {
128 struct cs428x_softc *sc;
129
130 sc = addr;
131 return (sc->codec_if->vtbl->query_devinfo(sc->codec_if, dip));
132 }
133
134 void *
135 cs428x_malloc(void *addr, int direction, size_t size, int pool, int flags)
136 {
137 struct cs428x_softc *sc;
138 struct cs428x_dma *p;
139 int error;
140
141 sc = addr;
142
143 p = malloc(sizeof(*p), pool, flags);
144 if (p == NULL)
145 return 0;
146
147 error = cs428x_allocmem(sc, size, pool, flags, p);
148
149 if (error) {
150 free(p, pool);
151 return 0;
152 }
153
154 p->next = sc->sc_dmas;
155 sc->sc_dmas = p;
156 return BUFADDR(p);
157 }
158
159 void
160 cs428x_free(void *addr, void *ptr, int pool)
161 {
162 struct cs428x_softc *sc;
163 struct cs428x_dma **pp, *p;
164
165 sc = addr;
166 for (pp = &sc->sc_dmas; (p = *pp) != NULL; pp = &p->next) {
167 if (BUFADDR(p) == ptr) {
168 bus_dmamap_unload(sc->sc_dmatag, p->map);
169 bus_dmamap_destroy(sc->sc_dmatag, p->map);
170 bus_dmamem_unmap(sc->sc_dmatag, p->addr, p->size);
171 bus_dmamem_free(sc->sc_dmatag, p->segs, p->nsegs);
172 free(p->dum, pool);
173 *pp = p->next;
174 free(p, pool);
175 return;
176 }
177 }
178 }
179
180 size_t
181 cs428x_round_buffersize(void *addr, int direction, size_t size)
182 {
183 /* The real dma buffersize are 4KB for CS4280
184 * and 64kB/MAX_CHANNELS for CS4281.
185 * But they are too small for high quality audio,
186 * let the upper layer(audio) use a larger buffer.
187 * (originally suggested by Lennart Augustsson.)
188 */
189 return size;
190 }
191
192 paddr_t
193 cs428x_mappage(void *addr, void *mem, off_t off, int prot)
194 {
195 struct cs428x_softc *sc;
196 struct cs428x_dma *p;
197
198 sc = addr;
199
200 if (off < 0)
201 return -1;
202
203 for (p = sc->sc_dmas; p && BUFADDR(p) != mem; p = p->next)
204 ;
205
206 if (p == NULL) {
207 DPRINTF(("cs428x_mappage: bad buffer address\n"));
208 return -1;
209 }
210
211 return (bus_dmamem_mmap(sc->sc_dmatag, p->segs, p->nsegs,
212 off, prot, BUS_DMA_WAITOK));
213 }
214
215 int
216 cs428x_get_props(void *addr)
217 {
218 int retval;
219
220 retval = AUDIO_PROP_INDEPENDENT | AUDIO_PROP_FULLDUPLEX;
221 #ifdef MMAP_READY
222 /* How can I mmap ? */
223 retval |= AUDIO_PROP_MMAP;
224 #endif
225 return retval;
226 }
227
228 /* AC97 */
229 int
230 cs428x_attach_codec(void *addr, struct ac97_codec_if *codec_if)
231 {
232 struct cs428x_softc *sc;
233
234 DPRINTF(("cs428x_attach_codec:\n"));
235 sc = addr;
236 sc->codec_if = codec_if;
237 return 0;
238 }
239
240 int
241 cs428x_read_codec(void *addr, u_int8_t ac97_addr, u_int16_t *ac97_data)
242 {
243 struct cs428x_softc *sc;
244 u_int32_t acctl;
245 int n;
246
247 sc = addr;
248
249 DPRINTFN(5,("read_codec: add=0x%02x ", ac97_addr));
250 /*
251 * Make sure that there is not data sitting around from a preivous
252 * uncompleted access.
253 */
254 BA0READ4(sc, CS428X_ACSDA);
255
256 /* Set up AC97 control registers. */
257 BA0WRITE4(sc, CS428X_ACCAD, ac97_addr);
258 BA0WRITE4(sc, CS428X_ACCDA, 0);
259
260 acctl = ACCTL_ESYN | ACCTL_VFRM | ACCTL_CRW | ACCTL_DCV;
261 if (sc->type == TYPE_CS4280)
262 acctl |= ACCTL_RSTN;
263 BA0WRITE4(sc, CS428X_ACCTL, acctl);
264
265 if (cs428x_src_wait(sc) < 0) {
266 printf("%s: AC97 read prob. (DCV!=0) for add=0x%0x\n",
267 sc->sc_dev.dv_xname, ac97_addr);
268 return 1;
269 }
270
271 /* wait for valid status bit is active */
272 n = 0;
273 while ((BA0READ4(sc, CS428X_ACSTS) & ACSTS_VSTS) == 0) {
274 delay(1);
275 while (++n > 1000) {
276 printf("%s: AC97 read fail (VSTS==0) for add=0x%0x\n",
277 sc->sc_dev.dv_xname, ac97_addr);
278 return 1;
279 }
280 }
281 *ac97_data = BA0READ4(sc, CS428X_ACSDA);
282 DPRINTFN(5,("data=0x%04x\n", *ac97_data));
283 return 0;
284 }
285
286 int
287 cs428x_write_codec(void *addr, u_int8_t ac97_addr, u_int16_t ac97_data)
288 {
289 struct cs428x_softc *sc;
290 u_int32_t acctl;
291
292 sc = addr;
293
294 DPRINTFN(5,("write_codec: add=0x%02x data=0x%04x\n", ac97_addr, ac97_data));
295 BA0WRITE4(sc, CS428X_ACCAD, ac97_addr);
296 BA0WRITE4(sc, CS428X_ACCDA, ac97_data);
297
298 acctl = ACCTL_ESYN | ACCTL_VFRM | ACCTL_DCV;
299 if (sc->type == TYPE_CS4280)
300 acctl |= ACCTL_RSTN;
301 BA0WRITE4(sc, CS428X_ACCTL, acctl);
302
303 if (cs428x_src_wait(sc) < 0) {
304 printf("%s: AC97 write fail (DCV!=0) for add=0x%02x data="
305 "0x%04x\n", sc->sc_dev.dv_xname, ac97_addr, ac97_data);
306 return 1;
307 }
308 return 0;
309 }
310
311 /* Internal functions */
312 int
313 cs428x_allocmem(struct cs428x_softc *sc,
314 size_t size, int pool, int flags,
315 struct cs428x_dma *p)
316 {
317 int error;
318 size_t align;
319
320 align = sc->dma_align;
321 p->size = sc->dma_size;
322 /* allocate memory for upper audio driver */
323 p->dum = malloc(size, pool, flags);
324 if (p->dum == NULL)
325 return 1;
326
327 error = bus_dmamem_alloc(sc->sc_dmatag, p->size, align, 0,
328 p->segs, sizeof(p->segs)/sizeof(p->segs[0]),
329 &p->nsegs, BUS_DMA_NOWAIT);
330 if (error) {
331 printf("%s: unable to allocate dma. error=%d\n",
332 sc->sc_dev.dv_xname, error);
333 goto allfree;
334 }
335
336 error = bus_dmamem_map(sc->sc_dmatag, p->segs, p->nsegs, p->size,
337 &p->addr, BUS_DMA_NOWAIT|BUS_DMA_COHERENT);
338 if (error) {
339 printf("%s: unable to map dma, error=%d\n",
340 sc->sc_dev.dv_xname, error);
341 goto free;
342 }
343
344 error = bus_dmamap_create(sc->sc_dmatag, p->size, 1, p->size,
345 0, BUS_DMA_NOWAIT, &p->map);
346 if (error) {
347 printf("%s: unable to create dma map, error=%d\n",
348 sc->sc_dev.dv_xname, error);
349 goto unmap;
350 }
351
352 error = bus_dmamap_load(sc->sc_dmatag, p->map, p->addr, p->size, NULL,
353 BUS_DMA_NOWAIT);
354 if (error) {
355 printf("%s: unable to load dma map, error=%d\n",
356 sc->sc_dev.dv_xname, error);
357 goto destroy;
358 }
359 return 0;
360
361 destroy:
362 bus_dmamap_destroy(sc->sc_dmatag, p->map);
363 unmap:
364 bus_dmamem_unmap(sc->sc_dmatag, p->addr, p->size);
365 free:
366 bus_dmamem_free(sc->sc_dmatag, p->segs, p->nsegs);
367 allfree:
368 free(p->dum, pool);
369
370 return error;
371 }
372
373 int
374 cs428x_src_wait(sc)
375 struct cs428x_softc *sc;
376 {
377 int n;
378
379 n = 0;
380 while ((BA0READ4(sc, CS428X_ACCTL) & ACCTL_DCV)) {
381 delay(1000);
382 while (++n > 1000) {
383 printf("cs428x_src_wait: 0x%08x\n",
384 BA0READ4(sc, CS428X_ACCTL));
385 return -1;
386 }
387 }
388 return 0;
389 }
390