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