Home | History | Annotate | Line # | Download | only in ic
      1  1.8     isaki /* $NetBSD: pl041.c,v 1.8 2020/02/29 05:51:11 isaki Exp $ */
      2  1.1  jmcneill 
      3  1.1  jmcneill /*-
      4  1.1  jmcneill  * Copyright (c) 2017 Jared McNeill <jmcneill (at) invisible.ca>
      5  1.1  jmcneill  * All rights reserved.
      6  1.1  jmcneill  *
      7  1.1  jmcneill  * Redistribution and use in source and binary forms, with or without
      8  1.1  jmcneill  * modification, are permitted provided that the following conditions
      9  1.1  jmcneill  * are met:
     10  1.1  jmcneill  * 1. Redistributions of source code must retain the above copyright
     11  1.1  jmcneill  *    notice, this list of conditions and the following disclaimer.
     12  1.1  jmcneill  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  jmcneill  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  jmcneill  *    documentation and/or other materials provided with the distribution.
     15  1.1  jmcneill  *
     16  1.1  jmcneill  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  1.1  jmcneill  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  1.1  jmcneill  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  1.1  jmcneill  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  1.1  jmcneill  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     21  1.1  jmcneill  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     22  1.1  jmcneill  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     23  1.1  jmcneill  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     24  1.1  jmcneill  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  1.1  jmcneill  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  1.1  jmcneill  * SUCH DAMAGE.
     27  1.1  jmcneill  */
     28  1.1  jmcneill 
     29  1.1  jmcneill #include <sys/cdefs.h>
     30  1.8     isaki __KERNEL_RCSID(0, "$NetBSD: pl041.c,v 1.8 2020/02/29 05:51:11 isaki Exp $");
     31  1.1  jmcneill 
     32  1.1  jmcneill #include <sys/param.h>
     33  1.1  jmcneill #include <sys/systm.h>
     34  1.1  jmcneill #include <sys/device.h>
     35  1.1  jmcneill #include <sys/kmem.h>
     36  1.1  jmcneill #include <sys/bus.h>
     37  1.1  jmcneill #include <sys/audioio.h>
     38  1.1  jmcneill 
     39  1.6     isaki #include <dev/audio/audio_if.h>
     40  1.1  jmcneill 
     41  1.1  jmcneill #include <dev/ic/ac97var.h>
     42  1.1  jmcneill #include <dev/ic/ac97reg.h>
     43  1.1  jmcneill 
     44  1.1  jmcneill #include <dev/ic/pl041var.h>
     45  1.1  jmcneill 
     46  1.1  jmcneill #define	AACIRXCR		0x00
     47  1.1  jmcneill #define	AACITXCR		0x04
     48  1.1  jmcneill #define	 AACITXCR_TXFEN		__BIT(16)
     49  1.1  jmcneill #define	 AACITXCR_TXCM		__BIT(15)
     50  1.1  jmcneill #define	 AACITXCR_TSIZE		__BITS(14,13)
     51  1.1  jmcneill #define	  AACITXCR_TSIZE_16	__SHIFTIN(0, AACITXCR_TSIZE)
     52  1.1  jmcneill #define	  AACITXCR_TX(n)	__BIT(n)
     53  1.1  jmcneill #define	 AACITXCR_TXEN		__BIT(0)
     54  1.1  jmcneill #define	AACISR			0x08
     55  1.1  jmcneill #define	 AACISR_TXFF		__BIT(5)
     56  1.1  jmcneill #define	 AACISR_TXHE		__BIT(3)
     57  1.1  jmcneill #define	AACIISR			0x0c
     58  1.1  jmcneill #define	 AACIISR_URINTR		__BIT(5)
     59  1.1  jmcneill #define	 AACIISR_TXINTR		__BIT(2)
     60  1.1  jmcneill #define	AACIIE			0x10
     61  1.1  jmcneill #define	 AACIIE_TXUIE		__BIT(5)
     62  1.1  jmcneill #define	 AACIIE_TXIE		__BIT(2)
     63  1.1  jmcneill #define	AACISL1RX		0x50
     64  1.1  jmcneill #define	AACISL1TX		0x54
     65  1.1  jmcneill #define	AACISL2RX		0x58
     66  1.1  jmcneill #define	AACISL2TX		0x5c
     67  1.1  jmcneill #define	AACISLFR		0x68
     68  1.1  jmcneill #define	AACISLISTAT		0x6c
     69  1.1  jmcneill #define	AACISLIEN		0x70
     70  1.1  jmcneill #define	AACIINTCLR		0x74
     71  1.1  jmcneill #define	AACIMAINCR		0x78
     72  1.1  jmcneill #define	AACIRESET		0x7c
     73  1.1  jmcneill #define	AACISYNC		0x80
     74  1.1  jmcneill #define	AACIALLINTS		0x84
     75  1.1  jmcneill #define	AACIMAINFR		0x88
     76  1.1  jmcneill #define	AACIDR			0x90
     77  1.1  jmcneill 
     78  1.1  jmcneill #define	AACI_FIFO_DEPTH		512
     79  1.1  jmcneill 
     80  1.1  jmcneill #define	AACI_READ(sc, reg)			\
     81  1.1  jmcneill 	bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
     82  1.1  jmcneill #define	AACI_WRITE(sc, reg, val)		\
     83  1.1  jmcneill 	bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
     84  1.1  jmcneill #define	AACI_WRITE_MULTI(sc, reg, val, cnt)	\
     85  1.1  jmcneill 	bus_space_write_multi_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val), (cnt))
     86  1.1  jmcneill 
     87  1.1  jmcneill static const struct audio_format aaci_format = {
     88  1.1  jmcneill 	.mode = AUMODE_PLAY,
     89  1.1  jmcneill 	.encoding = AUDIO_ENCODING_SLINEAR_LE,
     90  1.1  jmcneill 	.validbits = 16,
     91  1.1  jmcneill 	.precision = 16,
     92  1.1  jmcneill 	.channels = 2,
     93  1.1  jmcneill 	.channel_mask = AUFMT_STEREO,
     94  1.6     isaki 	.frequency_type = 1,
     95  1.6     isaki 	.frequency = { 48000 }
     96  1.1  jmcneill };
     97  1.1  jmcneill 
     98  1.1  jmcneill static void
     99  1.1  jmcneill aaci_write_data(struct aaci_softc *sc)
    100  1.1  jmcneill {
    101  1.1  jmcneill 	KASSERT(mutex_owned(&sc->sc_intr_lock));
    102  1.1  jmcneill 
    103  1.1  jmcneill 	if (sc->sc_pint == NULL)
    104  1.1  jmcneill 		return;
    105  1.1  jmcneill 
    106  1.2  jmcneill 	while ((AACI_READ(sc, AACISR) & AACISR_TXHE) != 0) {
    107  1.5  riastrad 		const int len = uimin(AACI_FIFO_DEPTH / 2, uimin(sc->sc_pblkresid,
    108  1.1  jmcneill 		    (uintptr_t)sc->sc_pend - (uintptr_t)sc->sc_pcur));
    109  1.1  jmcneill 		KASSERT((len & 3) == 0);
    110  1.3  jmcneill 		AACI_WRITE_MULTI(sc, AACIDR, sc->sc_pcur, len >> 2);
    111  1.1  jmcneill 		sc->sc_pcur += (len >> 2);
    112  1.1  jmcneill 		if (sc->sc_pcur == sc->sc_pend)
    113  1.1  jmcneill 			sc->sc_pcur = sc->sc_pstart;
    114  1.1  jmcneill 		sc->sc_pblkresid -= len;
    115  1.1  jmcneill 		if (sc->sc_pblkresid == 0) {
    116  1.1  jmcneill 			sc->sc_pblkresid = sc->sc_pblksize;
    117  1.1  jmcneill 			sc->sc_pint(sc->sc_pintarg);
    118  1.1  jmcneill 		}
    119  1.1  jmcneill 	}
    120  1.1  jmcneill }
    121  1.1  jmcneill 
    122  1.1  jmcneill static int
    123  1.6     isaki aaci_query_format(void *priv, audio_format_query_t *afp)
    124  1.1  jmcneill {
    125  1.1  jmcneill 
    126  1.6     isaki 	return audio_query_format(&aaci_format, 1, afp);
    127  1.1  jmcneill }
    128  1.1  jmcneill 
    129  1.1  jmcneill static int
    130  1.6     isaki aaci_set_format(void *priv, int setmode,
    131  1.6     isaki     const audio_params_t *play, const audio_params_t *rec,
    132  1.6     isaki     audio_filter_reg_t *pfil, audio_filter_reg_t *rfil)
    133  1.1  jmcneill {
    134  1.1  jmcneill 
    135  1.6     isaki 	/* We have only one format so nothing to do here. */
    136  1.1  jmcneill 	return 0;
    137  1.1  jmcneill }
    138  1.1  jmcneill 
    139  1.1  jmcneill static int
    140  1.1  jmcneill aaci_getdev(void *priv, struct audio_device *audiodev)
    141  1.1  jmcneill {
    142  1.1  jmcneill 	snprintf(audiodev->name, sizeof(audiodev->name), "ARM");
    143  1.1  jmcneill 	snprintf(audiodev->version, sizeof(audiodev->version),
    144  1.1  jmcneill 	    "PrimeCell AACI");
    145  1.1  jmcneill 	snprintf(audiodev->config, sizeof(audiodev->config), "aaci");
    146  1.1  jmcneill 	return 0;
    147  1.1  jmcneill }
    148  1.1  jmcneill 
    149  1.1  jmcneill static int
    150  1.1  jmcneill aaci_set_port(void *priv, mixer_ctrl_t *mc)
    151  1.1  jmcneill {
    152  1.1  jmcneill 	struct aaci_softc * const sc = priv;
    153  1.1  jmcneill 
    154  1.1  jmcneill 	return sc->sc_ac97_codec->vtbl->mixer_set_port(sc->sc_ac97_codec, mc);
    155  1.1  jmcneill }
    156  1.1  jmcneill 
    157  1.1  jmcneill static int
    158  1.1  jmcneill aaci_get_port(void *priv, mixer_ctrl_t *mc)
    159  1.1  jmcneill {
    160  1.1  jmcneill 	struct aaci_softc * const sc = priv;
    161  1.1  jmcneill 
    162  1.1  jmcneill 	return sc->sc_ac97_codec->vtbl->mixer_get_port(sc->sc_ac97_codec, mc);
    163  1.1  jmcneill }
    164  1.1  jmcneill 
    165  1.1  jmcneill static int
    166  1.1  jmcneill aaci_query_devinfo(void *priv, mixer_devinfo_t *di)
    167  1.1  jmcneill {
    168  1.1  jmcneill 	struct aaci_softc * const sc = priv;
    169  1.1  jmcneill 
    170  1.1  jmcneill 	return sc->sc_ac97_codec->vtbl->query_devinfo(sc->sc_ac97_codec, di);
    171  1.1  jmcneill }
    172  1.1  jmcneill 
    173  1.1  jmcneill static int
    174  1.1  jmcneill aaci_get_props(void *priv)
    175  1.1  jmcneill {
    176  1.1  jmcneill 	return AUDIO_PROP_PLAYBACK;
    177  1.1  jmcneill }
    178  1.1  jmcneill 
    179  1.1  jmcneill static int
    180  1.1  jmcneill aaci_trigger_output(void *priv, void *start, void *end, int blksize,
    181  1.1  jmcneill     void (*intr)(void *), void *intrarg, const audio_params_t *params)
    182  1.1  jmcneill {
    183  1.1  jmcneill 	struct aaci_softc * const sc = priv;
    184  1.1  jmcneill 
    185  1.1  jmcneill 	sc->sc_pcur = start;
    186  1.1  jmcneill 	sc->sc_pstart = start;
    187  1.1  jmcneill 	sc->sc_pend = end;
    188  1.1  jmcneill 	sc->sc_pblksize = blksize;
    189  1.1  jmcneill 	sc->sc_pblkresid = blksize;
    190  1.1  jmcneill 	sc->sc_pint = intr;
    191  1.1  jmcneill 	sc->sc_pintarg = intrarg;
    192  1.1  jmcneill 
    193  1.1  jmcneill 	AACI_WRITE(sc, AACIIE, AACIIE_TXIE | AACIIE_TXUIE);
    194  1.1  jmcneill 	AACI_WRITE(sc, AACITXCR, AACITXCR_TXFEN | AACITXCR_TXEN |
    195  1.1  jmcneill 	    AACITXCR_TXCM | AACITXCR_TSIZE_16 |
    196  1.4  jmcneill 	    AACITXCR_TX(AC97_SLOT_PCM_L) | AACITXCR_TX(AC97_SLOT_PCM_R));
    197  1.1  jmcneill 
    198  1.2  jmcneill 	return 0;
    199  1.1  jmcneill }
    200  1.1  jmcneill 
    201  1.1  jmcneill static int
    202  1.1  jmcneill aaci_halt_output(void *priv)
    203  1.1  jmcneill {
    204  1.1  jmcneill 	struct aaci_softc * const sc = priv;
    205  1.1  jmcneill 
    206  1.1  jmcneill 	sc->sc_pint = NULL;
    207  1.1  jmcneill 
    208  1.1  jmcneill 	AACI_WRITE(sc, AACITXCR, 0);
    209  1.1  jmcneill 	AACI_WRITE(sc, AACIIE, 0);
    210  1.1  jmcneill 
    211  1.1  jmcneill 	return 0;
    212  1.1  jmcneill }
    213  1.1  jmcneill 
    214  1.1  jmcneill static void
    215  1.1  jmcneill aaci_get_locks(void *priv, kmutex_t **intr, kmutex_t **thread)
    216  1.1  jmcneill {
    217  1.1  jmcneill 	struct aaci_softc * const sc = priv;
    218  1.1  jmcneill 
    219  1.1  jmcneill 	*intr = &sc->sc_intr_lock;
    220  1.1  jmcneill 	*thread = &sc->sc_lock;
    221  1.1  jmcneill }
    222  1.1  jmcneill 
    223  1.1  jmcneill static const struct audio_hw_if aaci_hw_if = {
    224  1.6     isaki 	.query_format = aaci_query_format,
    225  1.6     isaki 	.set_format = aaci_set_format,
    226  1.1  jmcneill 	.getdev = aaci_getdev,
    227  1.1  jmcneill 	.set_port = aaci_set_port,
    228  1.1  jmcneill 	.get_port = aaci_get_port,
    229  1.1  jmcneill 	.query_devinfo = aaci_query_devinfo,
    230  1.1  jmcneill 	.get_props = aaci_get_props,
    231  1.1  jmcneill 	.trigger_output = aaci_trigger_output,
    232  1.1  jmcneill 	.halt_output = aaci_halt_output,
    233  1.1  jmcneill 	.get_locks = aaci_get_locks,
    234  1.1  jmcneill };
    235  1.1  jmcneill 
    236  1.1  jmcneill static int
    237  1.1  jmcneill aaci_ac97_attach(void *priv, struct ac97_codec_if *codec)
    238  1.1  jmcneill {
    239  1.1  jmcneill 	struct aaci_softc * const sc = priv;
    240  1.1  jmcneill 
    241  1.1  jmcneill 	sc->sc_ac97_codec = codec;
    242  1.1  jmcneill 
    243  1.1  jmcneill 	return 0;
    244  1.1  jmcneill }
    245  1.1  jmcneill 
    246  1.1  jmcneill static int
    247  1.1  jmcneill aaci_ac97_read(void *priv, uint8_t reg, uint16_t *val)
    248  1.1  jmcneill {
    249  1.1  jmcneill 	struct aaci_softc * const sc = priv;
    250  1.1  jmcneill 
    251  1.1  jmcneill 	AACI_WRITE(sc, AACISL1TX, (reg << 12) | (1 << 19));
    252  1.1  jmcneill 
    253  1.1  jmcneill 	if (AACI_READ(sc, AACISL1RX) != (reg << 12))
    254  1.1  jmcneill 		return 1;
    255  1.1  jmcneill 
    256  1.1  jmcneill 	*val = AACI_READ(sc, AACISL2RX) >> 4;
    257  1.1  jmcneill 	return 0;
    258  1.1  jmcneill }
    259  1.1  jmcneill 
    260  1.1  jmcneill static int
    261  1.1  jmcneill aaci_ac97_write(void *priv, uint8_t reg, uint16_t val)
    262  1.1  jmcneill {
    263  1.1  jmcneill 	struct aaci_softc * const sc = priv;
    264  1.1  jmcneill 
    265  1.1  jmcneill 	AACI_WRITE(sc, AACISL2TX, val << 4);
    266  1.1  jmcneill 	AACI_WRITE(sc, AACISL1TX, (reg << 12) | (0 << 19));
    267  1.1  jmcneill 
    268  1.1  jmcneill 	return 0;
    269  1.1  jmcneill }
    270  1.1  jmcneill 
    271  1.1  jmcneill void
    272  1.1  jmcneill aaci_attach(struct aaci_softc *sc)
    273  1.1  jmcneill {
    274  1.1  jmcneill 	int error;
    275  1.1  jmcneill 
    276  1.1  jmcneill 	aprint_naive("\n");
    277  1.1  jmcneill 	aprint_normal(": Advanced Audio CODEC\n");
    278  1.1  jmcneill 
    279  1.1  jmcneill 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
    280  1.1  jmcneill 	mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_AUDIO);
    281  1.1  jmcneill 
    282  1.1  jmcneill 	sc->sc_ac97_host.arg = sc;
    283  1.1  jmcneill 	sc->sc_ac97_host.attach = aaci_ac97_attach;
    284  1.1  jmcneill 	sc->sc_ac97_host.read = aaci_ac97_read;
    285  1.1  jmcneill 	sc->sc_ac97_host.write = aaci_ac97_write;
    286  1.1  jmcneill 	error = ac97_attach(&sc->sc_ac97_host, sc->sc_dev, &sc->sc_lock);
    287  1.1  jmcneill 	if (error) {
    288  1.1  jmcneill 		aprint_error_dev(sc->sc_dev, "couldn't attach codec (%d)\n",
    289  1.1  jmcneill 		    error);
    290  1.1  jmcneill 		return;
    291  1.1  jmcneill 	}
    292  1.1  jmcneill 
    293  1.1  jmcneill 	sc->sc_audiodev = audio_attach_mi(&aaci_hw_if, sc, sc->sc_dev);
    294  1.1  jmcneill }
    295  1.1  jmcneill 
    296  1.1  jmcneill int
    297  1.1  jmcneill aaci_intr(void *priv)
    298  1.1  jmcneill {
    299  1.1  jmcneill 	struct aaci_softc * const sc = priv;
    300  1.1  jmcneill 	uint32_t isr;
    301  1.1  jmcneill 
    302  1.1  jmcneill 	if (sc->sc_audiodev == NULL)
    303  1.1  jmcneill 		return 0;
    304  1.1  jmcneill 
    305  1.1  jmcneill 	isr = AACI_READ(sc, AACIISR);
    306  1.1  jmcneill 
    307  1.1  jmcneill 	if (isr & AACIISR_URINTR)
    308  1.1  jmcneill 		AACI_WRITE(sc, AACIINTCLR, AACIISR_URINTR);
    309  1.1  jmcneill 
    310  1.1  jmcneill 	if (isr & AACIISR_TXINTR) {
    311  1.1  jmcneill 		mutex_enter(&sc->sc_intr_lock);
    312  1.1  jmcneill 		if (sc->sc_pint == NULL)
    313  1.1  jmcneill 			AACI_WRITE(sc, AACIIE, 0);
    314  1.1  jmcneill 		aaci_write_data(sc);
    315  1.1  jmcneill 		mutex_exit(&sc->sc_intr_lock);
    316  1.1  jmcneill 	}
    317  1.1  jmcneill 
    318  1.1  jmcneill 	return 1;
    319  1.1  jmcneill }
    320