ucbsnd.c revision 1.3 1 1.3 uch /* $NetBSD: ucbsnd.c,v 1.3 2000/03/03 19:54:34 uch Exp $ */
2 1.1 uch
3 1.1 uch /*
4 1.1 uch * Copyright (c) 2000, by UCHIYAMA Yasushi
5 1.1 uch * All rights reserved.
6 1.1 uch *
7 1.1 uch * Redistribution and use in source and binary forms, with or without
8 1.1 uch * modification, are permitted provided that the following conditions
9 1.1 uch * are met:
10 1.1 uch * 1. Redistributions of source code must retain the above copyright
11 1.1 uch * notice, this list of conditions and the following disclaimer.
12 1.1 uch * 2. The name of the developer may NOT be used to endorse or promote products
13 1.1 uch * derived from this software without specific prior written permission.
14 1.1 uch *
15 1.1 uch * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 1.1 uch * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 1.1 uch * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 1.1 uch * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 1.1 uch * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 1.1 uch * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 1.1 uch * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 1.1 uch * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 1.1 uch * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 1.1 uch * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 1.1 uch * SUCH DAMAGE.
26 1.1 uch *
27 1.1 uch */
28 1.1 uch
29 1.1 uch /*
30 1.1 uch * Device driver for PHILIPS UCB1200 Advanced modem/audio analog front-end
31 1.1 uch * Audio codec part.
32 1.3 uch *
33 1.3 uch * /dev/ucbsnd0 : sampling rate 22.154kHz monoral 16bit straight PCM device.
34 1.1 uch */
35 1.1 uch #define UCBSNDDEBUG
36 1.1 uch
37 1.1 uch #include "opt_tx39_debug.h"
38 1.1 uch #include "opt_use_poll.h"
39 1.1 uch
40 1.1 uch #include <sys/param.h>
41 1.1 uch #include <sys/systm.h>
42 1.3 uch #include <sys/conf.h>
43 1.3 uch #include <sys/malloc.h>
44 1.1 uch #include <sys/device.h>
45 1.3 uch #include <sys/proc.h>
46 1.3 uch #include <sys/endian.h>
47 1.1 uch
48 1.1 uch #include <machine/bus.h>
49 1.1 uch #include <machine/intr.h>
50 1.1 uch
51 1.1 uch #include <hpcmips/tx/tx39var.h>
52 1.1 uch #include <hpcmips/tx/tx39sibvar.h>
53 1.1 uch #include <hpcmips/tx/tx39sibreg.h>
54 1.1 uch #include <hpcmips/tx/tx39icureg.h>
55 1.1 uch #include <hpcmips/tx/txsnd.h>
56 1.1 uch
57 1.1 uch #include <hpcmips/dev/ucb1200var.h>
58 1.1 uch #include <hpcmips/dev/ucb1200reg.h>
59 1.1 uch
60 1.3 uch #define AUDIOUNIT(x) (minor(x)&0x0f)
61 1.3 uch #define AUDIODEV(x) (minor(x)&0xf0)
62 1.3 uch
63 1.1 uch #ifdef UCBSNDDEBUG
64 1.1 uch int ucbsnd_debug = 1;
65 1.1 uch #define DPRINTF(arg) if (ucbsnd_debug) printf arg;
66 1.1 uch #define DPRINTFN(n, arg) if (ucbsnd_debug > (n)) printf arg;
67 1.1 uch #else
68 1.1 uch #define DPRINTF(arg)
69 1.1 uch #define DPRINTFN(n, arg)
70 1.1 uch #endif
71 1.1 uch
72 1.3 uch #define UCBSND_BUFBLOCK 5
73 1.3 uch /*
74 1.3 uch * XXX temporary DMA buffer
75 1.3 uch */
76 1.3 uch static u_int8_t dmabuf_static[TX39_SIBDMA_SIZE * UCBSND_BUFBLOCK] __attribute__((__aligned__(16))); /* XXX */
77 1.3 uch static size_t dmabufcnt_static[UCBSND_BUFBLOCK]; /* XXX */
78 1.3 uch
79 1.1 uch enum ucbsnd_state {
80 1.1 uch /* 0 */ UCBSND_IDLE,
81 1.1 uch /* 1 */ UCBSND_INIT,
82 1.1 uch /* 2 */ UCBSND_ENABLE_SAMPLERATE,
83 1.1 uch /* 3 */ UCBSND_ENABLE_OUTPUTPATH,
84 1.3 uch /* 4 */ UCBSND_ENABLE_SETVOLUME,
85 1.1 uch /* 5 */ UCBSND_ENABLE_SPEAKER0,
86 1.1 uch /* 6 */ UCBSND_ENABLE_SPEAKER1,
87 1.1 uch /* 7 */ UCBSND_TRANSITION_PIO,
88 1.1 uch /* 8 */ UCBSND_PIO,
89 1.1 uch /* 9 */ UCBSND_TRANSITION_DISABLE,
90 1.1 uch /*10 */ UCBSND_DISABLE_OUTPUTPATH,
91 1.1 uch /*11 */ UCBSND_DISABLE_SPEAKER0,
92 1.1 uch /*12 */ UCBSND_DISABLE_SPEAKER1,
93 1.3 uch /*13 */ UCBSND_DISABLE_SIB,
94 1.3 uch /*14 */ UCBSND_DMASTART,
95 1.3 uch /*15 */ UCBSND_DMAEND,
96 1.3 uch };
97 1.3 uch
98 1.3 uch struct ring_buf {
99 1.3 uch u_int32_t rb_buf; /* buffer start address */
100 1.3 uch size_t *rb_bufcnt; /* effective data count (max rb_blksize)*/
101 1.3 uch
102 1.3 uch size_t rb_bufsize; /* total amount of buffer */
103 1.3 uch int rb_blksize; /* DMA block size */
104 1.3 uch int rb_maxblks; /* # of blocks in ring */
105 1.3 uch
106 1.3 uch int rb_inp; /* start of input (to buffer) */
107 1.3 uch int rb_outp; /* output pointer */
108 1.1 uch };
109 1.1 uch
110 1.1 uch struct ucbsnd_softc {
111 1.1 uch struct device sc_dev;
112 1.1 uch struct device *sc_sib; /* parent (TX39 SIB module) */
113 1.1 uch struct device *sc_ucb; /* parent (UCB1200 module) */
114 1.1 uch tx_chipset_tag_t sc_tc;
115 1.1 uch
116 1.3 uch struct tx_sound_tag sc_tag;
117 1.3 uch int sc_mute;
118 1.1 uch
119 1.1 uch /*
120 1.1 uch * audio codec state machine
121 1.1 uch */
122 1.3 uch int sa_transfer_mode;
123 1.3 uch #define UCBSND_TRANSFERMODE_DMA 0
124 1.3 uch #define UCBSND_TRANSFERMODE_PIO 1
125 1.1 uch enum ucbsnd_state sa_state;
126 1.3 uch int sa_snd_attenuation;
127 1.3 uch #define UCBSND_DEFAULT_ATTENUATION 0 /* Full volume */
128 1.1 uch int sa_snd_rate; /* passed down from SIB module */
129 1.1 uch int sa_tel_rate;
130 1.1 uch int sa_enabled;
131 1.1 uch void* sa_sf0ih;
132 1.1 uch void* sa_sndih;
133 1.1 uch int sa_retry;
134 1.1 uch int sa_cnt; /* misc counter */
135 1.3 uch
136 1.3 uch /*
137 1.3 uch * input buffer
138 1.3 uch */
139 1.3 uch size_t sa_dmacnt;
140 1.3 uch struct ring_buf sc_rb;
141 1.1 uch };
142 1.1 uch
143 1.3 uch cdev_decl(ucbsnd);
144 1.3 uch
145 1.1 uch int ucbsnd_match __P((struct device*, struct cfdata*, void*));
146 1.1 uch void ucbsnd_attach __P((struct device*, struct device*, void*));
147 1.1 uch
148 1.1 uch int ucbsnd_exec_output __P((void*));
149 1.3 uch int ucbsnd_busy __P((void*));
150 1.1 uch
151 1.1 uch void ucbsnd_sound_init __P((struct ucbsnd_softc*));
152 1.1 uch void __ucbsnd_sound_click __P((tx_sound_tag_t));
153 1.2 uch void __ucbsnd_sound_mute __P((tx_sound_tag_t, int));
154 1.1 uch
155 1.3 uch int ucbsndwrite_subr __P((struct ucbsnd_softc *, u_int32_t *, size_t,
156 1.3 uch struct uio *));
157 1.3 uch
158 1.3 uch int ringbuf_allocate __P((struct ring_buf*, size_t, int));
159 1.3 uch void ringbuf_deallocate __P((struct ring_buf*));
160 1.3 uch void ringbuf_reset __P((struct ring_buf*));
161 1.3 uch int ringbuf_full __P((struct ring_buf*));
162 1.3 uch void *ringbuf_producer_get __P((struct ring_buf*));
163 1.3 uch void ringbuf_producer_return __P((struct ring_buf*, size_t));
164 1.3 uch void *ringbuf_consumer_get __P((struct ring_buf*, size_t*));
165 1.3 uch void ringbuf_consumer_return __P((struct ring_buf*));
166 1.3 uch
167 1.1 uch struct cfattach ucbsnd_ca = {
168 1.1 uch sizeof(struct ucbsnd_softc), ucbsnd_match, ucbsnd_attach
169 1.1 uch };
170 1.1 uch
171 1.1 uch int
172 1.1 uch ucbsnd_match(parent, cf, aux)
173 1.1 uch struct device *parent;
174 1.1 uch struct cfdata *cf;
175 1.1 uch void *aux;
176 1.1 uch {
177 1.1 uch return 1;
178 1.1 uch }
179 1.1 uch
180 1.1 uch void
181 1.1 uch ucbsnd_attach(parent, self, aux)
182 1.1 uch struct device *parent;
183 1.1 uch struct device *self;
184 1.1 uch void *aux;
185 1.1 uch {
186 1.1 uch struct ucb1200_attach_args *ucba = aux;
187 1.1 uch struct ucbsnd_softc *sc = (void*)self;
188 1.1 uch tx_chipset_tag_t tc;
189 1.1 uch
190 1.1 uch tc = sc->sc_tc = ucba->ucba_tc;
191 1.1 uch sc->sc_sib = ucba->ucba_sib;
192 1.1 uch sc->sc_ucb = ucba->ucba_ucb;
193 1.2 uch
194 1.2 uch /* register sound functions */
195 1.1 uch ucbsnd_sound_init(sc);
196 1.2 uch
197 1.1 uch sc->sa_snd_rate = ucba->ucba_snd_rate;
198 1.1 uch sc->sa_tel_rate = ucba->ucba_tel_rate;
199 1.1 uch
200 1.3 uch sc->sa_snd_attenuation = UCBSND_DEFAULT_ATTENUATION;
201 1.1 uch #define KHZ(a) ((a) / 1000), (((a) % 1000))
202 1.1 uch printf(": audio %d.%03d kHz telecom %d.%03d kHz",
203 1.1 uch KHZ((tx39sib_clock(sc->sc_sib) * 2) /
204 1.1 uch (sc->sa_snd_rate * 64)),
205 1.1 uch KHZ((tx39sib_clock(sc->sc_sib) * 2) /
206 1.1 uch (sc->sa_tel_rate * 64)));
207 1.1 uch
208 1.1 uch ucb1200_state_install(parent, ucbsnd_busy, self,
209 1.1 uch UCB1200_SND_MODULE);
210 1.1 uch
211 1.3 uch ringbuf_allocate(&sc->sc_rb, TX39_SIBDMA_SIZE, UCBSND_BUFBLOCK);
212 1.3 uch
213 1.1 uch printf("\n");
214 1.1 uch }
215 1.1 uch
216 1.1 uch int
217 1.1 uch ucbsnd_busy(arg)
218 1.1 uch void *arg;
219 1.1 uch {
220 1.1 uch struct ucbsnd_softc *sc = arg;
221 1.1 uch
222 1.1 uch return sc->sa_state != UCBSND_IDLE;
223 1.1 uch }
224 1.1 uch
225 1.1 uch int
226 1.1 uch ucbsnd_exec_output(arg)
227 1.1 uch void *arg;
228 1.1 uch {
229 1.1 uch struct ucbsnd_softc *sc = arg;
230 1.1 uch tx_chipset_tag_t tc = sc->sc_tc;
231 1.1 uch txreg_t reg;
232 1.3 uch u_int32_t *buf;
233 1.3 uch size_t bufcnt;
234 1.1 uch
235 1.1 uch switch (sc->sa_state) {
236 1.1 uch default:
237 1.1 uch panic("ucbsnd_exec_output: invalid state %d", sc->sa_state);
238 1.1 uch /* NOTREACHED */
239 1.1 uch break;
240 1.1 uch
241 1.1 uch case UCBSND_IDLE:
242 1.1 uch /* nothing to do */
243 1.1 uch return 0;
244 1.1 uch
245 1.1 uch case UCBSND_INIT:
246 1.1 uch sc->sa_sf0ih = tx_intr_establish(
247 1.1 uch tc, MAKEINTR(1, TX39_INTRSTATUS1_SIBSF0INT),
248 1.1 uch IST_EDGE, IPL_TTY, ucbsnd_exec_output, sc);
249 1.1 uch
250 1.1 uch sc->sa_state = UCBSND_ENABLE_SAMPLERATE;
251 1.1 uch return 0;
252 1.1 uch
253 1.1 uch case UCBSND_ENABLE_SAMPLERATE:
254 1.1 uch /* Enable UCB1200 side sample rate */
255 1.1 uch reg = TX39_SIBSF0_WRITE;
256 1.1 uch reg = TX39_SIBSF0_REGADDR_SET(reg, UCB1200_AUDIOCTRLA_REG);
257 1.1 uch reg = TX39_SIBSF0_REGDATA_SET(reg, sc->sa_snd_rate);
258 1.1 uch tx_conf_write(tc, TX39_SIBSF0CTRL_REG, reg);
259 1.3 uch
260 1.1 uch sc->sa_state = UCBSND_ENABLE_OUTPUTPATH;
261 1.1 uch return 0;
262 1.3 uch
263 1.1 uch case UCBSND_ENABLE_OUTPUTPATH:
264 1.1 uch /* Enable UCB1200 side */
265 1.1 uch reg = TX39_SIBSF0_WRITE;
266 1.1 uch reg = TX39_SIBSF0_REGADDR_SET(reg, UCB1200_AUDIOCTRLB_REG);
267 1.3 uch reg = TX39_SIBSF0_REGDATA_SET(reg, sc->sa_snd_attenuation |
268 1.1 uch UCB1200_AUDIOCTRLB_OUTEN);
269 1.1 uch tx_conf_write(tc, TX39_SIBSF0CTRL_REG, reg);
270 1.1 uch
271 1.1 uch /* Enable SIB side */
272 1.1 uch reg = tx_conf_read(tc, TX39_SIBCTRL_REG);
273 1.1 uch tx_conf_write(tc, TX39_SIBCTRL_REG,
274 1.1 uch reg | TX39_SIBCTRL_ENSND);
275 1.1 uch
276 1.1 uch sc->sa_state = UCBSND_ENABLE_SPEAKER0;
277 1.1 uch sc->sa_retry = 10;
278 1.1 uch return 0;
279 1.1 uch case UCBSND_ENABLE_SPEAKER0:
280 1.1 uch /* Speaker on */
281 1.1 uch
282 1.1 uch reg = TX39_SIBSF0_REGADDR_SET(0, UCB1200_IO_DATA_REG);
283 1.1 uch tx_conf_write(tc, TX39_SIBSF0CTRL_REG, reg);
284 1.1 uch
285 1.1 uch sc->sa_state = UCBSND_ENABLE_SPEAKER1;
286 1.1 uch return 0;
287 1.1 uch
288 1.1 uch case UCBSND_ENABLE_SPEAKER1:
289 1.1 uch reg = tx_conf_read(tc, TX39_SIBSF0STAT_REG);
290 1.1 uch if ((TX39_SIBSF0_REGADDR(reg) != UCB1200_IO_DATA_REG) &&
291 1.1 uch --sc->sa_retry > 0) {
292 1.1 uch
293 1.1 uch sc->sa_state = UCBSND_ENABLE_SPEAKER0;
294 1.1 uch return 0;
295 1.1 uch }
296 1.1 uch
297 1.1 uch if (sc->sa_retry <= 0) {
298 1.1 uch printf("ucbsnd_exec_output: subframe0 busy\n");
299 1.1 uch
300 1.1 uch sc->sa_state = UCBSND_IDLE;
301 1.1 uch return 0;
302 1.1 uch }
303 1.1 uch
304 1.1 uch reg |= TX39_SIBSF0_WRITE;
305 1.1 uch reg |= UCB1200_IO_DATA_SPEAKER;
306 1.1 uch tx_conf_write(tc, TX39_SIBSF0CTRL_REG, reg);
307 1.1 uch
308 1.3 uch /*
309 1.3 uch * Begin to transfer.
310 1.3 uch */
311 1.3 uch switch (sc->sa_transfer_mode) {
312 1.3 uch case UCBSND_TRANSFERMODE_DMA:
313 1.3 uch sc->sa_state = UCBSND_DMASTART;
314 1.3 uch sc->sa_dmacnt = 0;
315 1.3 uch break;
316 1.3 uch case UCBSND_TRANSFERMODE_PIO:
317 1.3 uch sc->sa_state = UCBSND_TRANSITION_PIO;
318 1.3 uch break;
319 1.3 uch }
320 1.3 uch
321 1.1 uch return 0;
322 1.3 uch case UCBSND_DMASTART:
323 1.3 uch /* get data */
324 1.3 uch if (sc->sa_dmacnt) /* return previous buffer */
325 1.3 uch ringbuf_consumer_return(&sc->sc_rb);
326 1.3 uch buf = ringbuf_consumer_get(&sc->sc_rb, &bufcnt);
327 1.3 uch if (buf == 0) {
328 1.3 uch sc->sa_state = UCBSND_DMAEND;
329 1.3 uch return 0;
330 1.3 uch }
331 1.3 uch
332 1.3 uch if (sc->sa_dmacnt == 0) {
333 1.3 uch /* change interrupt source */
334 1.3 uch if (sc->sa_sf0ih) {
335 1.3 uch tx_intr_disestablish(tc, sc->sa_sf0ih);
336 1.3 uch sc->sa_sf0ih = 0;
337 1.3 uch }
338 1.3 uch sc->sa_sndih = tx_intr_establish(
339 1.3 uch tc, MAKEINTR(1, TX39_INTRSTATUS1_SND1_0INT),
340 1.3 uch IST_EDGE, IPL_TTY, ucbsnd_exec_output, sc);
341 1.3 uch } else {
342 1.3 uch wakeup(&sc->sc_rb);
343 1.3 uch }
344 1.3 uch
345 1.3 uch /* set DMA buffer address */
346 1.3 uch tx_conf_write(tc, TX39_SIBSNDTXSTART_REG,
347 1.3 uch MIPS_KSEG0_TO_PHYS(buf));
348 1.3 uch
349 1.3 uch /* set DMA buffer size */
350 1.3 uch tx_conf_write(tc, TX39_SIBSIZE_REG,
351 1.3 uch TX39_SIBSIZE_SNDSIZE_SET(0, bufcnt));
352 1.3 uch
353 1.3 uch tx_conf_write(tc, TX39_SIBSF0CTRL_REG, TX39_SIBSF0_SNDVALID);
354 1.3 uch
355 1.3 uch /* kick DMA */
356 1.3 uch reg = tx_conf_read(tc, TX39_SIBDMACTRL_REG);
357 1.3 uch reg |= TX39_SIBDMACTRL_ENDMATXSND;
358 1.3 uch tx_conf_write(tc, TX39_SIBDMACTRL_REG, reg);
359 1.3 uch
360 1.3 uch /* set next */
361 1.3 uch sc->sa_dmacnt += bufcnt;
362 1.3 uch
363 1.3 uch break;
364 1.1 uch
365 1.3 uch case UCBSND_DMAEND:
366 1.3 uch sc->sa_state = UCBSND_TRANSITION_DISABLE;
367 1.3 uch break;
368 1.1 uch case UCBSND_TRANSITION_PIO:
369 1.1 uch /* change interrupt source */
370 1.3 uch if (sc->sa_sf0ih) {
371 1.3 uch tx_intr_disestablish(tc, sc->sa_sf0ih);
372 1.3 uch sc->sa_sf0ih = 0;
373 1.3 uch }
374 1.1 uch sc->sa_sndih = tx_intr_establish(
375 1.1 uch tc, MAKEINTR(1, TX39_INTRSTATUS1_SNDININT),
376 1.1 uch IST_EDGE, IPL_TTY, ucbsnd_exec_output, sc);
377 1.1 uch sc->sa_enabled = 1;
378 1.1 uch
379 1.1 uch sc->sa_state = UCBSND_PIO;
380 1.2 uch sc->sa_cnt = 0;
381 1.1 uch return 0;
382 1.1 uch
383 1.1 uch case UCBSND_PIO:
384 1.3 uch {
385 1.3 uch /* PIO test routine */
386 1.3 uch int dummy_data = sc->sa_cnt * 3;
387 1.3 uch tx_conf_write(tc, TX39_SIBSNDHOLD_REG,
388 1.3 uch dummy_data << 16 | dummy_data);
389 1.1 uch tx_conf_write(tc, TX39_SIBSF0CTRL_REG, TX39_SIBSF0_SNDVALID);
390 1.3 uch if (sc->sa_cnt++ > 50) {
391 1.1 uch sc->sa_state = UCBSND_TRANSITION_DISABLE;
392 1.3 uch }
393 1.1 uch return 0;
394 1.3 uch }
395 1.1 uch case UCBSND_TRANSITION_DISABLE:
396 1.1 uch /* change interrupt source */
397 1.1 uch sc->sa_enabled = 0;
398 1.3 uch if (sc->sa_sndih) {
399 1.3 uch tx_intr_disestablish(tc, sc->sa_sndih);
400 1.3 uch sc->sa_sndih = 0;
401 1.3 uch }
402 1.1 uch sc->sa_sf0ih = tx_intr_establish(
403 1.1 uch tc, MAKEINTR(1, TX39_INTRSTATUS1_SIBSF0INT),
404 1.1 uch IST_EDGE, IPL_TTY, ucbsnd_exec_output, sc);
405 1.1 uch
406 1.1 uch sc->sa_state = UCBSND_DISABLE_OUTPUTPATH;
407 1.1 uch return 0;
408 1.1 uch
409 1.1 uch case UCBSND_DISABLE_OUTPUTPATH:
410 1.1 uch /* disable codec output path and mute */
411 1.1 uch reg = TX39_SIBSF0_WRITE;
412 1.1 uch reg = TX39_SIBSF0_REGADDR_SET(reg, UCB1200_AUDIOCTRLB_REG);
413 1.1 uch reg = TX39_SIBSF0_REGDATA_SET(reg, UCB1200_AUDIOCTRLB_MUTE);
414 1.1 uch tx_conf_write(tc, TX39_SIBSF0CTRL_REG, reg);
415 1.1 uch
416 1.1 uch sc->sa_state = UCBSND_DISABLE_SPEAKER0;
417 1.1 uch sc->sa_retry = 10;
418 1.1 uch return 0;
419 1.1 uch
420 1.1 uch case UCBSND_DISABLE_SPEAKER0:
421 1.1 uch /* Speaker off */
422 1.1 uch reg = TX39_SIBSF0_REGADDR_SET(0, UCB1200_IO_DATA_REG);
423 1.1 uch tx_conf_write(tc, TX39_SIBSF0CTRL_REG, reg);
424 1.1 uch
425 1.1 uch sc->sa_state = UCBSND_DISABLE_SPEAKER1;
426 1.1 uch return 0;
427 1.1 uch
428 1.1 uch case UCBSND_DISABLE_SPEAKER1:
429 1.1 uch reg = tx_conf_read(tc, TX39_SIBSF0STAT_REG);
430 1.1 uch if ((TX39_SIBSF0_REGADDR(reg) != UCB1200_IO_DATA_REG) &&
431 1.1 uch --sc->sa_retry > 0) {
432 1.1 uch
433 1.1 uch sc->sa_state = UCBSND_DISABLE_SPEAKER0;
434 1.1 uch return 0;
435 1.1 uch }
436 1.1 uch
437 1.1 uch if (sc->sa_retry <= 0) {
438 1.1 uch printf("ucbsnd_exec_output: subframe0 busy\n");
439 1.1 uch
440 1.1 uch sc->sa_state = UCBSND_IDLE;
441 1.1 uch return 0;
442 1.1 uch }
443 1.1 uch
444 1.1 uch reg |= TX39_SIBSF0_WRITE;
445 1.1 uch reg &= ~UCB1200_IO_DATA_SPEAKER;
446 1.1 uch tx_conf_write(tc, TX39_SIBSF0CTRL_REG, reg);
447 1.1 uch
448 1.1 uch sc->sa_state = UCBSND_DISABLE_SIB;
449 1.1 uch return 0;
450 1.1 uch
451 1.1 uch case UCBSND_DISABLE_SIB:
452 1.1 uch /* Disable SIB side */
453 1.1 uch reg = tx_conf_read(tc, TX39_SIBCTRL_REG);
454 1.1 uch reg &= ~TX39_SIBCTRL_ENSND;
455 1.1 uch tx_conf_write(tc, TX39_SIBCTRL_REG, reg);
456 1.1 uch
457 1.1 uch /* end audio disable sequence */
458 1.3 uch if (sc->sa_sf0ih) {
459 1.3 uch tx_intr_disestablish(tc, sc->sa_sf0ih);
460 1.3 uch sc->sa_sf0ih = 0;
461 1.3 uch }
462 1.1 uch sc->sa_state = UCBSND_IDLE;
463 1.1 uch
464 1.1 uch return 0;
465 1.1 uch }
466 1.1 uch
467 1.1 uch return 0;
468 1.1 uch }
469 1.1 uch
470 1.1 uch /*
471 1.1 uch * global sound interface.
472 1.1 uch */
473 1.1 uch void
474 1.1 uch ucbsnd_sound_init(sc)
475 1.1 uch struct ucbsnd_softc *sc;
476 1.1 uch {
477 1.1 uch tx_sound_tag_t ts = &sc->sc_tag;
478 1.1 uch tx_chipset_tag_t tc = sc->sc_tc;
479 1.1 uch
480 1.1 uch ts->ts_v = sc;
481 1.2 uch ts->ts_click = __ucbsnd_sound_click;
482 1.2 uch ts->ts_mute = __ucbsnd_sound_mute;
483 1.1 uch
484 1.1 uch tx_conf_register_sound(tc, ts);
485 1.1 uch }
486 1.1 uch
487 1.1 uch void
488 1.1 uch __ucbsnd_sound_click(arg)
489 1.1 uch tx_sound_tag_t arg;
490 1.1 uch {
491 1.1 uch struct ucbsnd_softc *sc = (void*)arg;
492 1.1 uch
493 1.2 uch if (!sc->sc_mute && sc->sa_state == UCBSND_IDLE) {
494 1.3 uch sc->sa_transfer_mode = UCBSND_TRANSFERMODE_PIO;
495 1.1 uch sc->sa_state = UCBSND_INIT;
496 1.1 uch ucbsnd_exec_output((void*)sc);
497 1.1 uch }
498 1.1 uch }
499 1.1 uch
500 1.2 uch void
501 1.2 uch __ucbsnd_sound_mute(arg, onoff)
502 1.2 uch tx_sound_tag_t arg;
503 1.2 uch int onoff;
504 1.2 uch {
505 1.2 uch struct ucbsnd_softc *sc = (void*)arg;
506 1.2 uch sc->sc_mute = onoff;
507 1.3 uch }
508 1.3 uch
509 1.3 uch /*
510 1.3 uch * device access
511 1.3 uch */
512 1.3 uch extern struct cfdriver ucbsnd_cd;
513 1.3 uch
514 1.3 uch int
515 1.3 uch ucbsndopen(dev, flags, ifmt, p)
516 1.3 uch dev_t dev;
517 1.3 uch int flags, ifmt;
518 1.3 uch struct proc *p;
519 1.3 uch {
520 1.3 uch int unit = AUDIOUNIT(dev);
521 1.3 uch struct ucbsnd_softc *sc;
522 1.3 uch
523 1.3 uch if (unit >= ucbsnd_cd.cd_ndevs ||
524 1.3 uch (sc = ucbsnd_cd.cd_devs[unit]) == NULL)
525 1.3 uch return (ENXIO);
526 1.3 uch
527 1.3 uch return (0);
528 1.3 uch }
529 1.3 uch
530 1.3 uch int
531 1.3 uch ucbsndclose(dev, flags, ifmt, p)
532 1.3 uch dev_t dev;
533 1.3 uch int flags, ifmt;
534 1.3 uch struct proc *p;
535 1.3 uch {
536 1.3 uch int unit = AUDIOUNIT(dev);
537 1.3 uch struct ucbsnd_softc *sc;
538 1.3 uch
539 1.3 uch if (unit >= ucbsnd_cd.cd_ndevs ||
540 1.3 uch (sc = ucbsnd_cd.cd_devs[unit]) == NULL)
541 1.3 uch return (ENXIO);
542 1.3 uch
543 1.3 uch DPRINTF(("ucbsndclose\n"));
544 1.3 uch
545 1.3 uch return (0);
546 1.3 uch }
547 1.3 uch
548 1.3 uch int
549 1.3 uch ucbsndread(dev, uio, ioflag)
550 1.3 uch dev_t dev;
551 1.3 uch struct uio *uio;
552 1.3 uch int ioflag;
553 1.3 uch {
554 1.3 uch int unit = AUDIOUNIT(dev);
555 1.3 uch struct ucbsnd_softc *sc;
556 1.3 uch int error = 0;
557 1.3 uch
558 1.3 uch if (unit >= ucbsnd_cd.cd_ndevs ||
559 1.3 uch (sc = ucbsnd_cd.cd_devs[unit]) == NULL)
560 1.3 uch return (ENXIO);
561 1.3 uch /* not supported yet */
562 1.3 uch
563 1.3 uch return (error);
564 1.3 uch }
565 1.3 uch
566 1.3 uch int
567 1.3 uch ucbsndwrite_subr(sc, buf, bufsize, uio)
568 1.3 uch struct ucbsnd_softc *sc;
569 1.3 uch u_int32_t *buf;
570 1.3 uch size_t bufsize;
571 1.3 uch struct uio *uio;
572 1.3 uch {
573 1.3 uch int i, s, error;
574 1.3 uch
575 1.3 uch error = uiomove(buf, bufsize, uio);
576 1.3 uch /*
577 1.3 uch * inverse endian for UCB1200
578 1.3 uch */
579 1.3 uch for (i = 0; i < bufsize / sizeof(int); i++)
580 1.3 uch buf[i] = htobe32(buf[i]);
581 1.3 uch MachFlushCache();
582 1.3 uch
583 1.3 uch ringbuf_producer_return(&sc->sc_rb, bufsize);
584 1.3 uch
585 1.3 uch s = splhigh();
586 1.3 uch if (sc->sa_state == UCBSND_IDLE && ringbuf_full(&sc->sc_rb)) {
587 1.3 uch sc->sa_transfer_mode = UCBSND_TRANSFERMODE_DMA;
588 1.3 uch sc->sa_state = UCBSND_INIT;
589 1.3 uch ucbsnd_exec_output((void*)sc);
590 1.3 uch }
591 1.3 uch splx(s);
592 1.3 uch
593 1.3 uch return error;
594 1.3 uch }
595 1.3 uch
596 1.3 uch int
597 1.3 uch ucbsndwrite(dev, uio, ioflag)
598 1.3 uch dev_t dev;
599 1.3 uch struct uio *uio;
600 1.3 uch int ioflag;
601 1.3 uch {
602 1.3 uch int unit = AUDIOUNIT(dev);
603 1.3 uch struct ucbsnd_softc *sc;
604 1.3 uch int len, error = 0;
605 1.3 uch int i, n, rest;
606 1.3 uch void *buf;
607 1.3 uch
608 1.3 uch if (unit >= ucbsnd_cd.cd_ndevs ||
609 1.3 uch (sc = ucbsnd_cd.cd_devs[unit]) == NULL)
610 1.3 uch return (ENXIO);
611 1.3 uch
612 1.3 uch len = uio->uio_resid;
613 1.3 uch n = (len + TX39_SIBDMA_SIZE - 1) / TX39_SIBDMA_SIZE;
614 1.3 uch rest = len % TX39_SIBDMA_SIZE;
615 1.3 uch
616 1.3 uch if (rest)
617 1.3 uch --n;
618 1.3 uch
619 1.3 uch for (i = 0; i < n; i++) {
620 1.3 uch while (!(buf = ringbuf_producer_get(&sc->sc_rb)))
621 1.3 uch tsleep(&sc->sc_rb, PRIBIO, "ucbsnd", 0);
622 1.3 uch
623 1.3 uch error = ucbsndwrite_subr(sc, buf, TX39_SIBDMA_SIZE, uio);
624 1.3 uch if (error)
625 1.3 uch goto out;
626 1.3 uch }
627 1.3 uch
628 1.3 uch if (rest) {
629 1.3 uch while (!(buf = ringbuf_producer_get(&sc->sc_rb)))
630 1.3 uch tsleep(&sc->sc_rb, PRIBIO, "ucbsnd", 0);
631 1.3 uch
632 1.3 uch error = ucbsndwrite_subr(sc, buf, rest, uio);
633 1.3 uch }
634 1.3 uch
635 1.3 uch out:
636 1.3 uch
637 1.3 uch return (error);
638 1.3 uch }
639 1.3 uch
640 1.3 uch int
641 1.3 uch ucbsndioctl(dev, cmd, addr, flag, p)
642 1.3 uch dev_t dev;
643 1.3 uch u_long cmd;
644 1.3 uch caddr_t addr;
645 1.3 uch int flag;
646 1.3 uch struct proc *p;
647 1.3 uch {
648 1.3 uch int error = 0;
649 1.3 uch
650 1.3 uch /* not coded yet */
651 1.3 uch
652 1.3 uch return (error);
653 1.3 uch }
654 1.3 uch
655 1.3 uch int
656 1.3 uch ucbsndpoll(dev, events, p)
657 1.3 uch dev_t dev;
658 1.3 uch int events;
659 1.3 uch struct proc *p;
660 1.3 uch {
661 1.3 uch int error = 0;
662 1.3 uch
663 1.3 uch /* not coded yet */
664 1.3 uch
665 1.3 uch return (error);
666 1.3 uch }
667 1.3 uch
668 1.3 uch int
669 1.3 uch ucbsndmmap(dev, off, prot)
670 1.3 uch dev_t dev;
671 1.3 uch int off, prot;
672 1.3 uch {
673 1.3 uch int error = 0;
674 1.3 uch
675 1.3 uch /* not coded yet */
676 1.3 uch
677 1.3 uch return (error);
678 1.3 uch }
679 1.3 uch
680 1.3 uch /*
681 1.3 uch * Ring buffer.
682 1.3 uch */
683 1.3 uch int
684 1.3 uch ringbuf_allocate(rb, blksize, maxblk)
685 1.3 uch struct ring_buf *rb;
686 1.3 uch size_t blksize;
687 1.3 uch int maxblk;
688 1.3 uch {
689 1.3 uch rb->rb_bufsize = blksize * maxblk;
690 1.3 uch rb->rb_blksize = blksize;
691 1.3 uch rb->rb_maxblks = maxblk;
692 1.3 uch #if notyet
693 1.3 uch rb->rb_buf = (u_int32_t)malloc(rb->rb_bufsize, M_DEVBUF, M_WAITOK);
694 1.3 uch #else
695 1.3 uch rb->rb_buf = (u_int32_t)dmabuf_static;
696 1.3 uch #endif
697 1.3 uch if (rb->rb_buf == 0) {
698 1.3 uch printf("ringbuf_allocate: can't allocate buffer\n");
699 1.3 uch return 1;
700 1.3 uch }
701 1.3 uch memset((char*)rb->rb_buf, 0, rb->rb_bufsize);
702 1.3 uch #if notyet
703 1.3 uch rb->rb_bufcnt = malloc(rb->rb_maxblks * sizeof(size_t), M_DEVBUF,
704 1.3 uch M_WAITOK);
705 1.3 uch #else
706 1.3 uch rb->rb_bufcnt = dmabufcnt_static;
707 1.3 uch #endif
708 1.3 uch if (rb->rb_bufcnt == 0) {
709 1.3 uch printf("ringbuf_allocate: can't allocate buffer\n");
710 1.3 uch return 1;
711 1.3 uch }
712 1.3 uch memset((char*)rb->rb_bufcnt, 0, rb->rb_maxblks * sizeof(size_t));
713 1.3 uch
714 1.3 uch ringbuf_reset(rb);
715 1.3 uch
716 1.3 uch return 0;
717 1.3 uch }
718 1.3 uch
719 1.3 uch void
720 1.3 uch ringbuf_deallocate(rb)
721 1.3 uch struct ring_buf *rb;
722 1.3 uch {
723 1.3 uch #if notyet
724 1.3 uch free((void*)rb->rb_buf, M_DEVBUF);
725 1.3 uch free(rb->rb_bufcnt, M_DEVBUF);
726 1.3 uch #endif
727 1.3 uch }
728 1.3 uch
729 1.3 uch void
730 1.3 uch ringbuf_reset(rb)
731 1.3 uch struct ring_buf *rb;
732 1.3 uch {
733 1.3 uch rb->rb_outp = 0;
734 1.3 uch rb->rb_inp = 0;
735 1.3 uch }
736 1.3 uch
737 1.3 uch int
738 1.3 uch ringbuf_full(rb)
739 1.3 uch struct ring_buf *rb;
740 1.3 uch {
741 1.3 uch int ret;
742 1.3 uch
743 1.3 uch ret = rb->rb_outp == rb->rb_maxblks;
744 1.3 uch
745 1.3 uch return ret;
746 1.3 uch }
747 1.3 uch
748 1.3 uch void*
749 1.3 uch ringbuf_producer_get(rb)
750 1.3 uch struct ring_buf *rb;
751 1.3 uch {
752 1.3 uch u_int32_t ret;
753 1.3 uch int s;
754 1.3 uch
755 1.3 uch s = splhigh();
756 1.3 uch ret = ringbuf_full(rb) ? 0 :
757 1.3 uch rb->rb_buf + rb->rb_inp * rb->rb_blksize;
758 1.3 uch splx(s);
759 1.3 uch
760 1.3 uch return (void*)ret;
761 1.3 uch }
762 1.3 uch
763 1.3 uch void
764 1.3 uch ringbuf_producer_return(rb, cnt)
765 1.3 uch struct ring_buf *rb;
766 1.3 uch size_t cnt;
767 1.3 uch {
768 1.3 uch int s;
769 1.3 uch
770 1.3 uch assert(cnt <= rb->rb_blksize);
771 1.3 uch
772 1.3 uch s = splhigh();
773 1.3 uch rb->rb_outp++;
774 1.3 uch
775 1.3 uch rb->rb_bufcnt[rb->rb_inp] = cnt;
776 1.3 uch rb->rb_inp = (rb->rb_inp + 1) % rb->rb_maxblks;
777 1.3 uch splx(s);
778 1.3 uch }
779 1.3 uch
780 1.3 uch void*
781 1.3 uch ringbuf_consumer_get(rb, cntp)
782 1.3 uch struct ring_buf *rb;
783 1.3 uch size_t *cntp;
784 1.3 uch {
785 1.3 uch u_int32_t p;
786 1.3 uch int idx;
787 1.3 uch
788 1.3 uch if (rb->rb_outp == 0)
789 1.3 uch return 0;
790 1.3 uch
791 1.3 uch idx = (rb->rb_inp - rb->rb_outp + rb->rb_maxblks) % rb->rb_maxblks;
792 1.3 uch
793 1.3 uch p = rb->rb_buf + idx * rb->rb_blksize;
794 1.3 uch *cntp = rb->rb_bufcnt[idx];
795 1.3 uch
796 1.3 uch return (void*)p;
797 1.3 uch }
798 1.3 uch
799 1.3 uch void
800 1.3 uch ringbuf_consumer_return(rb)
801 1.3 uch struct ring_buf *rb;
802 1.3 uch {
803 1.3 uch
804 1.3 uch if (rb->rb_outp > 0)
805 1.3 uch rb->rb_outp--;
806 1.2 uch }
807