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