pl041.c revision 1.6 1 /* $NetBSD: pl041.c,v 1.6 2019/05/08 13:40:18 isaki Exp $ */
2
3 /*-
4 * Copyright (c) 2017 Jared McNeill <jmcneill (at) 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.6 2019/05/08 13:40:18 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 #define AACI_BLOCK_ALIGN 4
80
81 #define AACI_READ(sc, reg) \
82 bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
83 #define AACI_WRITE(sc, reg, val) \
84 bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
85 #define AACI_WRITE_MULTI(sc, reg, val, cnt) \
86 bus_space_write_multi_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val), (cnt))
87
88 static const struct audio_format aaci_format = {
89 .mode = AUMODE_PLAY,
90 .encoding = AUDIO_ENCODING_SLINEAR_LE,
91 .validbits = 16,
92 .precision = 16,
93 .channels = 2,
94 .channel_mask = AUFMT_STEREO,
95 .frequency_type = 1,
96 .frequency = { 48000 }
97 };
98
99 static void
100 aaci_write_data(struct aaci_softc *sc)
101 {
102 KASSERT(mutex_owned(&sc->sc_intr_lock));
103
104 if (sc->sc_pint == NULL)
105 return;
106
107 while ((AACI_READ(sc, AACISR) & AACISR_TXHE) != 0) {
108 const int len = uimin(AACI_FIFO_DEPTH / 2, uimin(sc->sc_pblkresid,
109 (uintptr_t)sc->sc_pend - (uintptr_t)sc->sc_pcur));
110 KASSERT((len & 3) == 0);
111 AACI_WRITE_MULTI(sc, AACIDR, sc->sc_pcur, len >> 2);
112 sc->sc_pcur += (len >> 2);
113 if (sc->sc_pcur == sc->sc_pend)
114 sc->sc_pcur = sc->sc_pstart;
115 sc->sc_pblkresid -= len;
116 if (sc->sc_pblkresid == 0) {
117 sc->sc_pblkresid = sc->sc_pblksize;
118 sc->sc_pint(sc->sc_pintarg);
119 }
120 }
121 }
122
123 static int
124 aaci_query_format(void *priv, audio_format_query_t *afp)
125 {
126
127 return audio_query_format(&aaci_format, 1, afp);
128 }
129
130 static int
131 aaci_set_format(void *priv, int setmode,
132 const audio_params_t *play, const audio_params_t *rec,
133 audio_filter_reg_t *pfil, audio_filter_reg_t *rfil)
134 {
135
136 /* We have only one format so nothing to do here. */
137 return 0;
138 }
139
140 static int
141 aaci_getdev(void *priv, struct audio_device *audiodev)
142 {
143 snprintf(audiodev->name, sizeof(audiodev->name), "ARM");
144 snprintf(audiodev->version, sizeof(audiodev->version),
145 "PrimeCell AACI");
146 snprintf(audiodev->config, sizeof(audiodev->config), "aaci");
147 return 0;
148 }
149
150 static int
151 aaci_set_port(void *priv, mixer_ctrl_t *mc)
152 {
153 struct aaci_softc * const sc = priv;
154
155 return sc->sc_ac97_codec->vtbl->mixer_set_port(sc->sc_ac97_codec, mc);
156 }
157
158 static int
159 aaci_get_port(void *priv, mixer_ctrl_t *mc)
160 {
161 struct aaci_softc * const sc = priv;
162
163 return sc->sc_ac97_codec->vtbl->mixer_get_port(sc->sc_ac97_codec, mc);
164 }
165
166 static int
167 aaci_query_devinfo(void *priv, mixer_devinfo_t *di)
168 {
169 struct aaci_softc * const sc = priv;
170
171 return sc->sc_ac97_codec->vtbl->query_devinfo(sc->sc_ac97_codec, di);
172 }
173
174 static int
175 aaci_get_props(void *priv)
176 {
177 return AUDIO_PROP_PLAYBACK;
178 }
179
180 static int
181 aaci_trigger_output(void *priv, void *start, void *end, int blksize,
182 void (*intr)(void *), void *intrarg, const audio_params_t *params)
183 {
184 struct aaci_softc * const sc = priv;
185
186 sc->sc_pcur = start;
187 sc->sc_pstart = start;
188 sc->sc_pend = end;
189 sc->sc_pblksize = blksize;
190 sc->sc_pblkresid = blksize;
191 sc->sc_pint = intr;
192 sc->sc_pintarg = intrarg;
193
194 AACI_WRITE(sc, AACIIE, AACIIE_TXIE | AACIIE_TXUIE);
195 AACI_WRITE(sc, AACITXCR, AACITXCR_TXFEN | AACITXCR_TXEN |
196 AACITXCR_TXCM | AACITXCR_TSIZE_16 |
197 AACITXCR_TX(AC97_SLOT_PCM_L) | AACITXCR_TX(AC97_SLOT_PCM_R));
198
199 return 0;
200 }
201
202 static int
203 aaci_trigger_input(void *priv, void *start, void *end, int blksize,
204 void (*intr)(void *), void *intrarg, const audio_params_t *params)
205 {
206 return ENXIO;
207 }
208
209 static int
210 aaci_halt_output(void *priv)
211 {
212 struct aaci_softc * const sc = priv;
213
214 sc->sc_pint = NULL;
215
216 AACI_WRITE(sc, AACITXCR, 0);
217 AACI_WRITE(sc, AACIIE, 0);
218
219 return 0;
220 }
221
222 static int
223 aaci_halt_input(void *priv)
224 {
225 return ENXIO;
226 }
227
228 static int
229 aaci_round_blocksize(void *priv, int bs, int mode, const audio_params_t *params)
230 {
231 return roundup(bs, AACI_BLOCK_ALIGN);
232 }
233
234 static void
235 aaci_get_locks(void *priv, kmutex_t **intr, kmutex_t **thread)
236 {
237 struct aaci_softc * const sc = priv;
238
239 *intr = &sc->sc_intr_lock;
240 *thread = &sc->sc_lock;
241 }
242
243 static const struct audio_hw_if aaci_hw_if = {
244 .query_format = aaci_query_format,
245 .set_format = aaci_set_format,
246 .getdev = aaci_getdev,
247 .set_port = aaci_set_port,
248 .get_port = aaci_get_port,
249 .query_devinfo = aaci_query_devinfo,
250 .get_props = aaci_get_props,
251 .trigger_output = aaci_trigger_output,
252 .trigger_input = aaci_trigger_input,
253 .halt_output = aaci_halt_output,
254 .halt_input = aaci_halt_input,
255 .round_blocksize = aaci_round_blocksize,
256 .get_locks = aaci_get_locks,
257 };
258
259 static int
260 aaci_ac97_attach(void *priv, struct ac97_codec_if *codec)
261 {
262 struct aaci_softc * const sc = priv;
263
264 sc->sc_ac97_codec = codec;
265
266 return 0;
267 }
268
269 static int
270 aaci_ac97_read(void *priv, uint8_t reg, uint16_t *val)
271 {
272 struct aaci_softc * const sc = priv;
273
274 AACI_WRITE(sc, AACISL1TX, (reg << 12) | (1 << 19));
275
276 if (AACI_READ(sc, AACISL1RX) != (reg << 12))
277 return 1;
278
279 *val = AACI_READ(sc, AACISL2RX) >> 4;
280 return 0;
281 }
282
283 static int
284 aaci_ac97_write(void *priv, uint8_t reg, uint16_t val)
285 {
286 struct aaci_softc * const sc = priv;
287
288 AACI_WRITE(sc, AACISL2TX, val << 4);
289 AACI_WRITE(sc, AACISL1TX, (reg << 12) | (0 << 19));
290
291 return 0;
292 }
293
294 void
295 aaci_attach(struct aaci_softc *sc)
296 {
297 int error;
298
299 aprint_naive("\n");
300 aprint_normal(": Advanced Audio CODEC\n");
301
302 mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
303 mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_AUDIO);
304
305 sc->sc_ac97_host.arg = sc;
306 sc->sc_ac97_host.attach = aaci_ac97_attach;
307 sc->sc_ac97_host.read = aaci_ac97_read;
308 sc->sc_ac97_host.write = aaci_ac97_write;
309 error = ac97_attach(&sc->sc_ac97_host, sc->sc_dev, &sc->sc_lock);
310 if (error) {
311 aprint_error_dev(sc->sc_dev, "couldn't attach codec (%d)\n",
312 error);
313 return;
314 }
315
316 sc->sc_audiodev = audio_attach_mi(&aaci_hw_if, sc, sc->sc_dev);
317 }
318
319 int
320 aaci_intr(void *priv)
321 {
322 struct aaci_softc * const sc = priv;
323 uint32_t isr;
324
325 if (sc->sc_audiodev == NULL)
326 return 0;
327
328 isr = AACI_READ(sc, AACIISR);
329
330 if (isr & AACIISR_URINTR)
331 AACI_WRITE(sc, AACIINTCLR, AACIISR_URINTR);
332
333 if (isr & AACIISR_TXINTR) {
334 mutex_enter(&sc->sc_intr_lock);
335 if (sc->sc_pint == NULL)
336 AACI_WRITE(sc, AACIIE, 0);
337 aaci_write_data(sc);
338 mutex_exit(&sc->sc_intr_lock);
339 }
340
341 return 1;
342 }
343