pl041.c revision 1.3 1 1.3 jmcneill /* $NetBSD: pl041.c,v 1.3 2017/06/08 11:05:16 jmcneill 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.3 jmcneill __KERNEL_RCSID(0, "$NetBSD: pl041.c,v 1.3 2017/06/08 11:05:16 jmcneill 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.1 jmcneill #include <dev/audio_if.h>
40 1.1 jmcneill #include <dev/auconv.h>
41 1.1 jmcneill
42 1.1 jmcneill #include <dev/ic/ac97var.h>
43 1.1 jmcneill #include <dev/ic/ac97reg.h>
44 1.1 jmcneill
45 1.1 jmcneill #include <dev/ic/pl041var.h>
46 1.1 jmcneill
47 1.1 jmcneill #define AACIRXCR 0x00
48 1.1 jmcneill #define AACITXCR 0x04
49 1.1 jmcneill #define AACITXCR_TXFEN __BIT(16)
50 1.1 jmcneill #define AACITXCR_TXCM __BIT(15)
51 1.1 jmcneill #define AACITXCR_TSIZE __BITS(14,13)
52 1.1 jmcneill #define AACITXCR_TSIZE_16 __SHIFTIN(0, AACITXCR_TSIZE)
53 1.1 jmcneill #define AACITXCR_TX(n) __BIT(n)
54 1.1 jmcneill #define AACITXCR_TXEN __BIT(0)
55 1.1 jmcneill #define AACISR 0x08
56 1.1 jmcneill #define AACISR_TXFF __BIT(5)
57 1.1 jmcneill #define AACISR_TXHE __BIT(3)
58 1.1 jmcneill #define AACIISR 0x0c
59 1.1 jmcneill #define AACIISR_URINTR __BIT(5)
60 1.1 jmcneill #define AACIISR_TXINTR __BIT(2)
61 1.1 jmcneill #define AACIIE 0x10
62 1.1 jmcneill #define AACIIE_TXUIE __BIT(5)
63 1.1 jmcneill #define AACIIE_TXIE __BIT(2)
64 1.1 jmcneill #define AACISL1RX 0x50
65 1.1 jmcneill #define AACISL1TX 0x54
66 1.1 jmcneill #define AACISL2RX 0x58
67 1.1 jmcneill #define AACISL2TX 0x5c
68 1.1 jmcneill #define AACISLFR 0x68
69 1.1 jmcneill #define AACISLISTAT 0x6c
70 1.1 jmcneill #define AACISLIEN 0x70
71 1.1 jmcneill #define AACIINTCLR 0x74
72 1.1 jmcneill #define AACIMAINCR 0x78
73 1.1 jmcneill #define AACIRESET 0x7c
74 1.1 jmcneill #define AACISYNC 0x80
75 1.1 jmcneill #define AACIALLINTS 0x84
76 1.1 jmcneill #define AACIMAINFR 0x88
77 1.1 jmcneill #define AACIDR 0x90
78 1.1 jmcneill
79 1.1 jmcneill #define AACI_FIFO_DEPTH 512
80 1.1 jmcneill #define AACI_BLOCK_ALIGN 4
81 1.1 jmcneill
82 1.1 jmcneill #define AACI_READ(sc, reg) \
83 1.1 jmcneill bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
84 1.1 jmcneill #define AACI_WRITE(sc, reg, val) \
85 1.1 jmcneill bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
86 1.1 jmcneill #define AACI_WRITE_MULTI(sc, reg, val, cnt) \
87 1.1 jmcneill bus_space_write_multi_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val), (cnt))
88 1.1 jmcneill
89 1.1 jmcneill static const struct audio_format aaci_format = {
90 1.1 jmcneill .mode = AUMODE_PLAY,
91 1.1 jmcneill .encoding = AUDIO_ENCODING_SLINEAR_LE,
92 1.1 jmcneill .validbits = 16,
93 1.1 jmcneill .precision = 16,
94 1.1 jmcneill .channels = 2,
95 1.1 jmcneill .channel_mask = AUFMT_STEREO,
96 1.1 jmcneill .frequency_type = 0,
97 1.1 jmcneill .frequency = { 48000, 48000 }
98 1.1 jmcneill };
99 1.1 jmcneill
100 1.1 jmcneill static void
101 1.1 jmcneill aaci_write_data(struct aaci_softc *sc)
102 1.1 jmcneill {
103 1.1 jmcneill KASSERT(mutex_owned(&sc->sc_intr_lock));
104 1.1 jmcneill
105 1.1 jmcneill if (sc->sc_pint == NULL)
106 1.1 jmcneill return;
107 1.1 jmcneill
108 1.2 jmcneill while ((AACI_READ(sc, AACISR) & AACISR_TXHE) != 0) {
109 1.1 jmcneill const int len = min(AACI_FIFO_DEPTH / 2, min(sc->sc_pblkresid,
110 1.1 jmcneill (uintptr_t)sc->sc_pend - (uintptr_t)sc->sc_pcur));
111 1.1 jmcneill KASSERT((len & 3) == 0);
112 1.3 jmcneill AACI_WRITE_MULTI(sc, AACIDR, sc->sc_pcur, len >> 2);
113 1.1 jmcneill sc->sc_pcur += (len >> 2);
114 1.1 jmcneill if (sc->sc_pcur == sc->sc_pend)
115 1.1 jmcneill sc->sc_pcur = sc->sc_pstart;
116 1.1 jmcneill sc->sc_pblkresid -= len;
117 1.1 jmcneill if (sc->sc_pblkresid == 0) {
118 1.1 jmcneill sc->sc_pblkresid = sc->sc_pblksize;
119 1.1 jmcneill sc->sc_pint(sc->sc_pintarg);
120 1.1 jmcneill }
121 1.1 jmcneill }
122 1.1 jmcneill }
123 1.1 jmcneill
124 1.1 jmcneill static int
125 1.1 jmcneill aaci_query_encoding(void *priv, struct audio_encoding *enc)
126 1.1 jmcneill {
127 1.1 jmcneill struct aaci_softc * const sc = priv;
128 1.1 jmcneill
129 1.1 jmcneill return auconv_query_encoding(sc->sc_encodings, enc);
130 1.1 jmcneill }
131 1.1 jmcneill
132 1.1 jmcneill static int
133 1.1 jmcneill aaci_set_params(void *priv, int setmode, int usermode,
134 1.1 jmcneill audio_params_t *play, audio_params_t *rec,
135 1.1 jmcneill stream_filter_list_t *pfil, stream_filter_list_t *rfil)
136 1.1 jmcneill {
137 1.1 jmcneill int index;
138 1.1 jmcneill
139 1.1 jmcneill if (play && (setmode & AUMODE_PLAY) != 0) {
140 1.1 jmcneill index = auconv_set_converter(&aaci_format, 1, AUMODE_PLAY,
141 1.1 jmcneill play, true, pfil);
142 1.1 jmcneill if (index < 0)
143 1.1 jmcneill return EINVAL;
144 1.1 jmcneill }
145 1.1 jmcneill
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_getdev(void *priv, struct audio_device *audiodev)
151 1.1 jmcneill {
152 1.1 jmcneill snprintf(audiodev->name, sizeof(audiodev->name), "ARM");
153 1.1 jmcneill snprintf(audiodev->version, sizeof(audiodev->version),
154 1.1 jmcneill "PrimeCell AACI");
155 1.1 jmcneill snprintf(audiodev->config, sizeof(audiodev->config), "aaci");
156 1.1 jmcneill return 0;
157 1.1 jmcneill }
158 1.1 jmcneill
159 1.1 jmcneill static int
160 1.1 jmcneill aaci_set_port(void *priv, mixer_ctrl_t *mc)
161 1.1 jmcneill {
162 1.1 jmcneill struct aaci_softc * const sc = priv;
163 1.1 jmcneill
164 1.1 jmcneill return sc->sc_ac97_codec->vtbl->mixer_set_port(sc->sc_ac97_codec, mc);
165 1.1 jmcneill }
166 1.1 jmcneill
167 1.1 jmcneill static int
168 1.1 jmcneill aaci_get_port(void *priv, mixer_ctrl_t *mc)
169 1.1 jmcneill {
170 1.1 jmcneill struct aaci_softc * const sc = priv;
171 1.1 jmcneill
172 1.1 jmcneill return sc->sc_ac97_codec->vtbl->mixer_get_port(sc->sc_ac97_codec, mc);
173 1.1 jmcneill }
174 1.1 jmcneill
175 1.1 jmcneill static int
176 1.1 jmcneill aaci_query_devinfo(void *priv, mixer_devinfo_t *di)
177 1.1 jmcneill {
178 1.1 jmcneill struct aaci_softc * const sc = priv;
179 1.1 jmcneill
180 1.1 jmcneill return sc->sc_ac97_codec->vtbl->query_devinfo(sc->sc_ac97_codec, di);
181 1.1 jmcneill }
182 1.1 jmcneill
183 1.1 jmcneill static int
184 1.1 jmcneill aaci_get_props(void *priv)
185 1.1 jmcneill {
186 1.1 jmcneill return AUDIO_PROP_PLAYBACK;
187 1.1 jmcneill }
188 1.1 jmcneill
189 1.1 jmcneill static int
190 1.1 jmcneill aaci_trigger_output(void *priv, void *start, void *end, int blksize,
191 1.1 jmcneill void (*intr)(void *), void *intrarg, const audio_params_t *params)
192 1.1 jmcneill {
193 1.1 jmcneill struct aaci_softc * const sc = priv;
194 1.1 jmcneill
195 1.1 jmcneill sc->sc_pcur = start;
196 1.1 jmcneill sc->sc_pstart = start;
197 1.1 jmcneill sc->sc_pend = end;
198 1.1 jmcneill sc->sc_pblksize = blksize;
199 1.1 jmcneill sc->sc_pblkresid = blksize;
200 1.1 jmcneill sc->sc_pint = intr;
201 1.1 jmcneill sc->sc_pintarg = intrarg;
202 1.1 jmcneill
203 1.1 jmcneill AACI_WRITE(sc, AACIIE, AACIIE_TXIE | AACIIE_TXUIE);
204 1.1 jmcneill AACI_WRITE(sc, AACITXCR, AACITXCR_TXFEN | AACITXCR_TXEN |
205 1.1 jmcneill AACITXCR_TXCM | AACITXCR_TSIZE_16 |
206 1.1 jmcneill AACITXCR_TX(3) | AACITXCR_TX(4));
207 1.1 jmcneill
208 1.2 jmcneill return 0;
209 1.1 jmcneill }
210 1.1 jmcneill
211 1.1 jmcneill static int
212 1.1 jmcneill aaci_trigger_input(void *priv, void *start, void *end, int blksize,
213 1.1 jmcneill void (*intr)(void *), void *intrarg, const audio_params_t *params)
214 1.1 jmcneill {
215 1.1 jmcneill return ENXIO;
216 1.1 jmcneill }
217 1.1 jmcneill
218 1.1 jmcneill static int
219 1.1 jmcneill aaci_halt_output(void *priv)
220 1.1 jmcneill {
221 1.1 jmcneill struct aaci_softc * const sc = priv;
222 1.1 jmcneill
223 1.1 jmcneill sc->sc_pint = NULL;
224 1.1 jmcneill
225 1.1 jmcneill AACI_WRITE(sc, AACITXCR, 0);
226 1.1 jmcneill AACI_WRITE(sc, AACIIE, 0);
227 1.1 jmcneill
228 1.1 jmcneill return 0;
229 1.1 jmcneill }
230 1.1 jmcneill
231 1.1 jmcneill static int
232 1.1 jmcneill aaci_halt_input(void *priv)
233 1.1 jmcneill {
234 1.1 jmcneill return ENXIO;
235 1.1 jmcneill }
236 1.1 jmcneill
237 1.1 jmcneill static int
238 1.1 jmcneill aaci_round_blocksize(void *priv, int bs, int mode, const audio_params_t *params)
239 1.1 jmcneill {
240 1.1 jmcneill return roundup(bs, AACI_BLOCK_ALIGN);
241 1.1 jmcneill }
242 1.1 jmcneill
243 1.1 jmcneill static void
244 1.1 jmcneill aaci_get_locks(void *priv, kmutex_t **intr, kmutex_t **thread)
245 1.1 jmcneill {
246 1.1 jmcneill struct aaci_softc * const sc = priv;
247 1.1 jmcneill
248 1.1 jmcneill *intr = &sc->sc_intr_lock;
249 1.1 jmcneill *thread = &sc->sc_lock;
250 1.1 jmcneill }
251 1.1 jmcneill
252 1.1 jmcneill static const struct audio_hw_if aaci_hw_if = {
253 1.1 jmcneill .query_encoding = aaci_query_encoding,
254 1.1 jmcneill .set_params = aaci_set_params,
255 1.1 jmcneill .getdev = aaci_getdev,
256 1.1 jmcneill .set_port = aaci_set_port,
257 1.1 jmcneill .get_port = aaci_get_port,
258 1.1 jmcneill .query_devinfo = aaci_query_devinfo,
259 1.1 jmcneill .get_props = aaci_get_props,
260 1.1 jmcneill .trigger_output = aaci_trigger_output,
261 1.1 jmcneill .trigger_input = aaci_trigger_input,
262 1.1 jmcneill .halt_output = aaci_halt_output,
263 1.1 jmcneill .halt_input = aaci_halt_input,
264 1.1 jmcneill .round_blocksize = aaci_round_blocksize,
265 1.1 jmcneill .get_locks = aaci_get_locks,
266 1.1 jmcneill };
267 1.1 jmcneill
268 1.1 jmcneill static int
269 1.1 jmcneill aaci_ac97_attach(void *priv, struct ac97_codec_if *codec)
270 1.1 jmcneill {
271 1.1 jmcneill struct aaci_softc * const sc = priv;
272 1.1 jmcneill
273 1.1 jmcneill sc->sc_ac97_codec = codec;
274 1.1 jmcneill
275 1.1 jmcneill return 0;
276 1.1 jmcneill }
277 1.1 jmcneill
278 1.1 jmcneill static int
279 1.1 jmcneill aaci_ac97_read(void *priv, uint8_t reg, uint16_t *val)
280 1.1 jmcneill {
281 1.1 jmcneill struct aaci_softc * const sc = priv;
282 1.1 jmcneill
283 1.1 jmcneill AACI_WRITE(sc, AACISL1TX, (reg << 12) | (1 << 19));
284 1.1 jmcneill
285 1.1 jmcneill if (AACI_READ(sc, AACISL1RX) != (reg << 12))
286 1.1 jmcneill return 1;
287 1.1 jmcneill
288 1.1 jmcneill *val = AACI_READ(sc, AACISL2RX) >> 4;
289 1.1 jmcneill return 0;
290 1.1 jmcneill }
291 1.1 jmcneill
292 1.1 jmcneill static int
293 1.1 jmcneill aaci_ac97_write(void *priv, uint8_t reg, uint16_t val)
294 1.1 jmcneill {
295 1.1 jmcneill struct aaci_softc * const sc = priv;
296 1.1 jmcneill
297 1.1 jmcneill AACI_WRITE(sc, AACISL2TX, val << 4);
298 1.1 jmcneill AACI_WRITE(sc, AACISL1TX, (reg << 12) | (0 << 19));
299 1.1 jmcneill
300 1.1 jmcneill return 0;
301 1.1 jmcneill }
302 1.1 jmcneill
303 1.1 jmcneill void
304 1.1 jmcneill aaci_attach(struct aaci_softc *sc)
305 1.1 jmcneill {
306 1.1 jmcneill int error;
307 1.1 jmcneill
308 1.1 jmcneill aprint_naive("\n");
309 1.1 jmcneill aprint_normal(": Advanced Audio CODEC\n");
310 1.1 jmcneill
311 1.1 jmcneill mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
312 1.1 jmcneill mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_AUDIO);
313 1.1 jmcneill
314 1.1 jmcneill sc->sc_ac97_host.arg = sc;
315 1.1 jmcneill sc->sc_ac97_host.attach = aaci_ac97_attach;
316 1.1 jmcneill sc->sc_ac97_host.read = aaci_ac97_read;
317 1.1 jmcneill sc->sc_ac97_host.write = aaci_ac97_write;
318 1.1 jmcneill error = ac97_attach(&sc->sc_ac97_host, sc->sc_dev, &sc->sc_lock);
319 1.1 jmcneill if (error) {
320 1.1 jmcneill aprint_error_dev(sc->sc_dev, "couldn't attach codec (%d)\n",
321 1.1 jmcneill error);
322 1.1 jmcneill return;
323 1.1 jmcneill }
324 1.1 jmcneill
325 1.1 jmcneill error = auconv_create_encodings(&aaci_format, 1, &sc->sc_encodings);
326 1.1 jmcneill if (error) {
327 1.1 jmcneill aprint_error_dev(sc->sc_dev, "couldn't create encodings (%d)\n",
328 1.1 jmcneill error);
329 1.1 jmcneill return;
330 1.1 jmcneill }
331 1.1 jmcneill
332 1.1 jmcneill sc->sc_audiodev = audio_attach_mi(&aaci_hw_if, sc, sc->sc_dev);
333 1.1 jmcneill }
334 1.1 jmcneill
335 1.1 jmcneill int
336 1.1 jmcneill aaci_intr(void *priv)
337 1.1 jmcneill {
338 1.1 jmcneill struct aaci_softc * const sc = priv;
339 1.1 jmcneill uint32_t isr;
340 1.1 jmcneill
341 1.1 jmcneill if (sc->sc_audiodev == NULL)
342 1.1 jmcneill return 0;
343 1.1 jmcneill
344 1.1 jmcneill isr = AACI_READ(sc, AACIISR);
345 1.1 jmcneill
346 1.1 jmcneill if (isr & AACIISR_URINTR)
347 1.1 jmcneill AACI_WRITE(sc, AACIINTCLR, AACIISR_URINTR);
348 1.1 jmcneill
349 1.1 jmcneill if (isr & AACIISR_TXINTR) {
350 1.1 jmcneill mutex_enter(&sc->sc_intr_lock);
351 1.1 jmcneill if (sc->sc_pint == NULL)
352 1.1 jmcneill AACI_WRITE(sc, AACIIE, 0);
353 1.1 jmcneill aaci_write_data(sc);
354 1.1 jmcneill mutex_exit(&sc->sc_intr_lock);
355 1.1 jmcneill }
356 1.1 jmcneill
357 1.1 jmcneill return 1;
358 1.1 jmcneill }
359