cs428x.c revision 1.13.6.1 1 /* $NetBSD: cs428x.c,v 1.13.6.1 2007/02/27 14:16:25 ad 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.13.6.1 2007/02/27 14:16:25 ad 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_round_blocksize(void *addr, int blk,
67 int mode, const audio_params_t *param)
68 {
69 struct cs428x_softc *sc;
70 int retval;
71
72 DPRINTFN(5,("cs428x_round_blocksize blk=%d -> ", blk));
73
74 sc = addr;
75 if (blk < sc->hw_blocksize)
76 retval = sc->hw_blocksize;
77 else
78 retval = blk & -(sc->hw_blocksize);
79
80 DPRINTFN(5,("%d\n", retval));
81
82 return retval;
83 }
84
85 int
86 cs428x_mixer_set_port(void *addr, mixer_ctrl_t *cp)
87 {
88 struct cs428x_softc *sc;
89 int val;
90
91 sc = addr;
92 val = sc->codec_if->vtbl->mixer_set_port(sc->codec_if, cp);
93 DPRINTFN(3,("mixer_set_port: val=%d\n", val));
94 return (val);
95 }
96
97 int
98 cs428x_mixer_get_port(void *addr, mixer_ctrl_t *cp)
99 {
100 struct cs428x_softc *sc;
101
102 sc = addr;
103 return (sc->codec_if->vtbl->mixer_get_port(sc->codec_if, cp));
104 }
105
106 int
107 cs428x_query_devinfo(void *addr, mixer_devinfo_t *dip)
108 {
109 struct cs428x_softc *sc;
110
111 sc = addr;
112 return (sc->codec_if->vtbl->query_devinfo(sc->codec_if, dip));
113 }
114
115 void *
116 cs428x_malloc(void *addr, int direction, size_t size,
117 struct malloc_type *pool, int flags)
118 {
119 struct cs428x_softc *sc;
120 struct cs428x_dma *p;
121 int error;
122
123 sc = addr;
124
125 p = malloc(sizeof(*p), pool, flags);
126 if (p == NULL)
127 return 0;
128
129 error = cs428x_allocmem(sc, size, pool, flags, p);
130
131 if (error) {
132 free(p, pool);
133 return 0;
134 }
135
136 p->next = sc->sc_dmas;
137 sc->sc_dmas = p;
138 return BUFADDR(p);
139 }
140
141 void
142 cs428x_free(void *addr, void *ptr, struct malloc_type *pool)
143 {
144 struct cs428x_softc *sc;
145 struct cs428x_dma **pp, *p;
146
147 sc = addr;
148 for (pp = &sc->sc_dmas; (p = *pp) != NULL; pp = &p->next) {
149 if (BUFADDR(p) == ptr) {
150 bus_dmamap_unload(sc->sc_dmatag, p->map);
151 bus_dmamap_destroy(sc->sc_dmatag, p->map);
152 bus_dmamem_unmap(sc->sc_dmatag, p->addr, p->size);
153 bus_dmamem_free(sc->sc_dmatag, p->segs, p->nsegs);
154 free(p->dum, pool);
155 *pp = p->next;
156 free(p, pool);
157 return;
158 }
159 }
160 }
161
162 size_t
163 cs428x_round_buffersize(void *addr, int direction,
164 size_t size)
165 {
166 /* The real DMA buffersize are 4KB for CS4280
167 * and 64kB/MAX_CHANNELS for CS4281.
168 * But they are too small for high quality audio,
169 * let the upper layer(audio) use a larger buffer.
170 * (originally suggested by Lennart Augustsson.)
171 */
172 return size;
173 }
174
175 paddr_t
176 cs428x_mappage(void *addr, void *mem, off_t off, int prot)
177 {
178 struct cs428x_softc *sc;
179 struct cs428x_dma *p;
180 paddr_t pa;
181
182 sc = addr;
183
184 if (off < 0)
185 return -1;
186
187 for (p = sc->sc_dmas; p && BUFADDR(p) != mem; p = p->next)
188 continue;
189
190 if (p == NULL) {
191 DPRINTF(("cs428x_mappage: bad buffer address\n"));
192 return -1;
193 }
194
195 mutex_exit(&sc->sc_lock);
196 pa = bus_dmamem_mmap(sc->sc_dmatag, p->segs, p->nsegs,
197 off, prot, BUS_DMA_WAITOK);
198 mutex_enter(&sc->sc_lock);
199 return pa;
200 }
201
202 int
203 cs428x_get_props(void *addr)
204 {
205 int retval;
206
207 retval = AUDIO_PROP_INDEPENDENT | AUDIO_PROP_FULLDUPLEX;
208 #ifdef MMAP_READY
209 /* How can I mmap ? */
210 retval |= AUDIO_PROP_MMAP;
211 #endif
212 return retval;
213 }
214
215 /* AC97 */
216 int
217 cs428x_attach_codec(void *addr, struct ac97_codec_if *codec_if)
218 {
219 struct cs428x_softc *sc;
220
221 DPRINTF(("cs428x_attach_codec:\n"));
222 sc = addr;
223 sc->codec_if = codec_if;
224 return 0;
225 }
226
227 int
228 cs428x_read_codec(void *addr, uint8_t ac97_addr, uint16_t *ac97_data)
229 {
230 struct cs428x_softc *sc;
231 uint32_t acctl;
232 int n;
233
234 sc = addr;
235
236 DPRINTFN(5,("read_codec: add=0x%02x ", ac97_addr));
237 /*
238 * Make sure that there is not data sitting around from a previous
239 * uncompleted access.
240 */
241 BA0READ4(sc, CS428X_ACSDA);
242
243 /* Set up AC97 control registers. */
244 BA0WRITE4(sc, CS428X_ACCAD, ac97_addr);
245 BA0WRITE4(sc, CS428X_ACCDA, 0);
246
247 acctl = ACCTL_ESYN | ACCTL_VFRM | ACCTL_CRW | ACCTL_DCV;
248 if (sc->type == TYPE_CS4280)
249 acctl |= ACCTL_RSTN;
250 BA0WRITE4(sc, CS428X_ACCTL, acctl);
251
252 if (cs428x_src_wait(sc) < 0) {
253 printf("%s: AC97 read prob. (DCV!=0) for add=0x%0x\n",
254 sc->sc_dev.dv_xname, ac97_addr);
255 return 1;
256 }
257
258 /* wait for valid status bit is active */
259 n = 0;
260 while ((BA0READ4(sc, CS428X_ACSTS) & ACSTS_VSTS) == 0) {
261 delay(1);
262 while (++n > 1000) {
263 printf("%s: AC97 read fail (VSTS==0) for add=0x%0x\n",
264 sc->sc_dev.dv_xname, ac97_addr);
265 return 1;
266 }
267 }
268 *ac97_data = BA0READ4(sc, CS428X_ACSDA);
269 DPRINTFN(5,("data=0x%04x\n", *ac97_data));
270 return 0;
271 }
272
273 int
274 cs428x_write_codec(void *addr, uint8_t ac97_addr, uint16_t ac97_data)
275 {
276 struct cs428x_softc *sc;
277 uint32_t acctl;
278
279 sc = addr;
280
281 DPRINTFN(5,("write_codec: add=0x%02x data=0x%04x\n", ac97_addr, ac97_data));
282 BA0WRITE4(sc, CS428X_ACCAD, ac97_addr);
283 BA0WRITE4(sc, CS428X_ACCDA, ac97_data);
284
285 acctl = ACCTL_ESYN | ACCTL_VFRM | ACCTL_DCV;
286 if (sc->type == TYPE_CS4280)
287 acctl |= ACCTL_RSTN;
288 BA0WRITE4(sc, CS428X_ACCTL, acctl);
289
290 if (cs428x_src_wait(sc) < 0) {
291 printf("%s: AC97 write fail (DCV!=0) for add=0x%02x data="
292 "0x%04x\n", sc->sc_dev.dv_xname, ac97_addr, ac97_data);
293 return 1;
294 }
295 return 0;
296 }
297
298 /* Internal functions */
299 int
300 cs428x_allocmem(struct cs428x_softc *sc,
301 size_t size, struct malloc_type *pool, int flags,
302 struct cs428x_dma *p)
303 {
304 int error;
305 size_t align;
306
307 align = sc->dma_align;
308 p->size = sc->dma_size;
309 /* allocate memory for upper audio driver */
310 p->dum = malloc(size, pool, flags);
311 if (p->dum == NULL)
312 return 1;
313
314 error = bus_dmamem_alloc(sc->sc_dmatag, p->size, align, 0,
315 p->segs, sizeof(p->segs)/sizeof(p->segs[0]),
316 &p->nsegs, BUS_DMA_NOWAIT);
317 if (error) {
318 printf("%s: unable to allocate DMA. error=%d\n",
319 sc->sc_dev.dv_xname, error);
320 goto allfree;
321 }
322
323 error = bus_dmamem_map(sc->sc_dmatag, p->segs, p->nsegs, p->size,
324 &p->addr, BUS_DMA_NOWAIT|BUS_DMA_COHERENT);
325 if (error) {
326 printf("%s: unable to map DMA, error=%d\n",
327 sc->sc_dev.dv_xname, error);
328 goto free;
329 }
330
331 error = bus_dmamap_create(sc->sc_dmatag, p->size, 1, p->size,
332 0, BUS_DMA_NOWAIT, &p->map);
333 if (error) {
334 printf("%s: unable to create DMA map, error=%d\n",
335 sc->sc_dev.dv_xname, error);
336 goto unmap;
337 }
338
339 error = bus_dmamap_load(sc->sc_dmatag, p->map, p->addr, p->size, NULL,
340 BUS_DMA_NOWAIT);
341 if (error) {
342 printf("%s: unable to load DMA map, error=%d\n",
343 sc->sc_dev.dv_xname, error);
344 goto destroy;
345 }
346 return 0;
347
348 destroy:
349 bus_dmamap_destroy(sc->sc_dmatag, p->map);
350 unmap:
351 bus_dmamem_unmap(sc->sc_dmatag, p->addr, p->size);
352 free:
353 bus_dmamem_free(sc->sc_dmatag, p->segs, p->nsegs);
354 allfree:
355 free(p->dum, pool);
356
357 return error;
358 }
359
360 int
361 cs428x_src_wait(struct cs428x_softc *sc)
362 {
363 int n;
364
365 n = 0;
366 while ((BA0READ4(sc, CS428X_ACCTL) & ACCTL_DCV)) {
367 delay(1000);
368 while (++n > 1000) {
369 printf("cs428x_src_wait: 0x%08x\n",
370 BA0READ4(sc, CS428X_ACCTL));
371 return -1;
372 }
373 }
374 return 0;
375 }
376
377 void
378 cs428x_get_locks(void *addr, kmutex_t **intr, kmutex_t **proc)
379 {
380 struct cs428x_softc *sc;
381
382 sc = addr;
383 *intr = &sc->sc_intr_lock;
384 *proc = &sc->sc_lock;
385 }
386