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