FreeBSD/Linux Kernel Cross Reference
sys/dev/pci/cs428x.c
1 /* $NetBSD: cs428x.c,v 1.6 2003/05/03 18:11:33 wiz 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.6 2003/05/03 18:11:33 wiz 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,
136 struct malloc_type *pool, int flags)
137 {
138 struct cs428x_softc *sc;
139 struct cs428x_dma *p;
140 int error;
141
142 sc = addr;
143
144 p = malloc(sizeof(*p), pool, flags);
145 if (p == NULL)
146 return 0;
147
148 error = cs428x_allocmem(sc, size, pool, flags, p);
149
150 if (error) {
151 free(p, pool);
152 return 0;
153 }
154
155 p->next = sc->sc_dmas;
156 sc->sc_dmas = p;
157 return BUFADDR(p);
158 }
159
160 void
161 cs428x_free(void *addr, void *ptr, struct malloc_type *pool)
162 {
163 struct cs428x_softc *sc;
164 struct cs428x_dma **pp, *p;
165
166 sc = addr;
167 for (pp = &sc->sc_dmas; (p = *pp) != NULL; pp = &p->next) {
168 if (BUFADDR(p) == ptr) {
169 bus_dmamap_unload(sc->sc_dmatag, p->map);
170 bus_dmamap_destroy(sc->sc_dmatag, p->map);
171 bus_dmamem_unmap(sc->sc_dmatag, p->addr, p->size);
172 bus_dmamem_free(sc->sc_dmatag, p->segs, p->nsegs);
173 free(p->dum, pool);
174 *pp = p->next;
175 free(p, pool);
176 return;
177 }
178 }
179 }
180
181 size_t
182 cs428x_round_buffersize(void *addr, int direction, size_t size)
183 {
184 /* The real DMA buffersize are 4KB for CS4280
185 * and 64kB/MAX_CHANNELS for CS4281.
186 * But they are too small for high quality audio,
187 * let the upper layer(audio) use a larger buffer.
188 * (originally suggested by Lennart Augustsson.)
189 */
190 return size;
191 }
192
193 paddr_t
194 cs428x_mappage(void *addr, void *mem, off_t off, int prot)
195 {
196 struct cs428x_softc *sc;
197 struct cs428x_dma *p;
198
199 sc = addr;
200
201 if (off < 0)
202 return -1;
203
204 for (p = sc->sc_dmas; p && BUFADDR(p) != mem; p = p->next)
205 ;
206
207 if (p == NULL) {
208 DPRINTF(("cs428x_mappage: bad buffer address\n"));
209 return -1;
210 }
211
212 return (bus_dmamem_mmap(sc->sc_dmatag, p->segs, p->nsegs,
213 off, prot, BUS_DMA_WAITOK));
214 }
215
216 int
217 cs428x_get_props(void *addr)
218 {
219 int retval;
220
221 retval = AUDIO_PROP_INDEPENDENT | AUDIO_PROP_FULLDUPLEX;
222 #ifdef MMAP_READY
223 /* How can I mmap ? */
224 retval |= AUDIO_PROP_MMAP;
225 #endif
226 return retval;
227 }
228
229 /* AC97 */
230 int
231 cs428x_attach_codec(void *addr, struct ac97_codec_if *codec_if)
232 {
233 struct cs428x_softc *sc;
234
235 DPRINTF(("cs428x_attach_codec:\n"));
236 sc = addr;
237 sc->codec_if = codec_if;
238 return 0;
239 }
240
241 int
242 cs428x_read_codec(void *addr, u_int8_t ac97_addr, u_int16_t *ac97_data)
243 {
244 struct cs428x_softc *sc;
245 u_int32_t acctl;
246 int n;
247
248 sc = addr;
249
250 DPRINTFN(5,("read_codec: add=0x%02x ", ac97_addr));
251 /*
252 * Make sure that there is not data sitting around from a preivous
253 * uncompleted access.
254 */
255 BA0READ4(sc, CS428X_ACSDA);
256
257 /* Set up AC97 control registers. */
258 BA0WRITE4(sc, CS428X_ACCAD, ac97_addr);
259 BA0WRITE4(sc, CS428X_ACCDA, 0);
260
261 acctl = ACCTL_ESYN | ACCTL_VFRM | ACCTL_CRW | ACCTL_DCV;
262 if (sc->type == TYPE_CS4280)
263 acctl |= ACCTL_RSTN;
264 BA0WRITE4(sc, CS428X_ACCTL, acctl);
265
266 if (cs428x_src_wait(sc) < 0) {
267 printf("%s: AC97 read prob. (DCV!=0) for add=0x%0x\n",
268 sc->sc_dev.dv_xname, ac97_addr);
269 return 1;
270 }
271
272 /* wait for valid status bit is active */
273 n = 0;
274 while ((BA0READ4(sc, CS428X_ACSTS) & ACSTS_VSTS) == 0) {
275 delay(1);
276 while (++n > 1000) {
277 printf("%s: AC97 read fail (VSTS==0) for add=0x%0x\n",
278 sc->sc_dev.dv_xname, ac97_addr);
279 return 1;
280 }
281 }
282 *ac97_data = BA0READ4(sc, CS428X_ACSDA);
283 DPRINTFN(5,("data=0x%04x\n", *ac97_data));
284 return 0;
285 }
286
287 int
288 cs428x_write_codec(void *addr, u_int8_t ac97_addr, u_int16_t ac97_data)
289 {
290 struct cs428x_softc *sc;
291 u_int32_t acctl;
292
293 sc = addr;
294
295 DPRINTFN(5,("write_codec: add=0x%02x data=0x%04x\n", ac97_addr, ac97_data));
296 BA0WRITE4(sc, CS428X_ACCAD, ac97_addr);
297 BA0WRITE4(sc, CS428X_ACCDA, ac97_data);
298
299 acctl = ACCTL_ESYN | ACCTL_VFRM | ACCTL_DCV;
300 if (sc->type == TYPE_CS4280)
301 acctl |= ACCTL_RSTN;
302 BA0WRITE4(sc, CS428X_ACCTL, acctl);
303
304 if (cs428x_src_wait(sc) < 0) {
305 printf("%s: AC97 write fail (DCV!=0) for add=0x%02x data="
306 "0x%04x\n", sc->sc_dev.dv_xname, ac97_addr, ac97_data);
307 return 1;
308 }
309 return 0;
310 }
311
312 /* Internal functions */
313 int
314 cs428x_allocmem(struct cs428x_softc *sc,
315 size_t size, struct malloc_type *pool, int flags,
316 struct cs428x_dma *p)
317 {
318 int error;
319 size_t align;
320
321 align = sc->dma_align;
322 p->size = sc->dma_size;
323 /* allocate memory for upper audio driver */
324 p->dum = malloc(size, pool, flags);
325 if (p->dum == NULL)
326 return 1;
327
328 error = bus_dmamem_alloc(sc->sc_dmatag, p->size, align, 0,
329 p->segs, sizeof(p->segs)/sizeof(p->segs[0]),
330 &p->nsegs, BUS_DMA_NOWAIT);
331 if (error) {
332 printf("%s: unable to allocate DMA. error=%d\n",
333 sc->sc_dev.dv_xname, error);
334 goto allfree;
335 }
336
337 error = bus_dmamem_map(sc->sc_dmatag, p->segs, p->nsegs, p->size,
338 &p->addr, BUS_DMA_NOWAIT|BUS_DMA_COHERENT);
339 if (error) {
340 printf("%s: unable to map DMA, error=%d\n",
341 sc->sc_dev.dv_xname, error);
342 goto free;
343 }
344
345 error = bus_dmamap_create(sc->sc_dmatag, p->size, 1, p->size,
346 0, BUS_DMA_NOWAIT, &p->map);
347 if (error) {
348 printf("%s: unable to create DMA map, error=%d\n",
349 sc->sc_dev.dv_xname, error);
350 goto unmap;
351 }
352
353 error = bus_dmamap_load(sc->sc_dmatag, p->map, p->addr, p->size, NULL,
354 BUS_DMA_NOWAIT);
355 if (error) {
356 printf("%s: unable to load DMA map, error=%d\n",
357 sc->sc_dev.dv_xname, error);
358 goto destroy;
359 }
360 return 0;
361
362 destroy:
363 bus_dmamap_destroy(sc->sc_dmatag, p->map);
364 unmap:
365 bus_dmamem_unmap(sc->sc_dmatag, p->addr, p->size);
366 free:
367 bus_dmamem_free(sc->sc_dmatag, p->segs, p->nsegs);
368 allfree:
369 free(p->dum, pool);
370
371 return error;
372 }
373
374 int
375 cs428x_src_wait(sc)
376 struct cs428x_softc *sc;
377 {
378 int n;
379
380 n = 0;
381 while ((BA0READ4(sc, CS428X_ACCTL) & ACCTL_DCV)) {
382 delay(1000);
383 while (++n > 1000) {
384 printf("cs428x_src_wait: 0x%08x\n",
385 BA0READ4(sc, CS428X_ACCTL));
386 return -1;
387 }
388 }
389 return 0;
390 }
Cache object: 589c81afe3d2b950e43d4ce72f88a209
|