1 1.12 thorpej /* $NetBSD: vr4181aiu.c,v 1.12 2023/12/20 14:50:02 thorpej Exp $ */ 2 1.1 igy 3 1.1 igy /* 4 1.1 igy * Copyright (c) 2002 The NetBSD Foundation, Inc. 5 1.1 igy * All rights reserved. 6 1.1 igy * 7 1.1 igy * This code is derived from software contributed to The NetBSD Foundation 8 1.1 igy * by Naoto Shimazaki of YOKOGAWA Electric Corporation. 9 1.1 igy * 10 1.1 igy * Redistribution and use in source and binary forms, with or without 11 1.1 igy * modification, are permitted provided that the following conditions 12 1.1 igy * are met: 13 1.1 igy * 1. Redistributions of source code must retain the above copyright 14 1.1 igy * notice, this list of conditions and the following disclaimer. 15 1.1 igy * 2. Redistributions in binary form must reproduce the above copyright 16 1.1 igy * notice, this list of conditions and the following disclaimer in the 17 1.1 igy * documentation and/or other materials provided with the distribution. 18 1.1 igy * 19 1.1 igy * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 1.1 igy * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 1.1 igy * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 1.1 igy * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 1.1 igy * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 1.1 igy * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 1.1 igy * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 1.1 igy * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 1.1 igy * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 1.1 igy * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 1.1 igy * POSSIBILITY OF SUCH DAMAGE. 30 1.1 igy */ 31 1.2 lukem 32 1.2 lukem #include <sys/cdefs.h> 33 1.12 thorpej __KERNEL_RCSID(0, "$NetBSD: vr4181aiu.c,v 1.12 2023/12/20 14:50:02 thorpej Exp $"); 34 1.1 igy 35 1.1 igy #include <sys/param.h> 36 1.1 igy #include <sys/conf.h> 37 1.1 igy #include <sys/device.h> 38 1.1 igy #include <sys/errno.h> 39 1.1 igy #include <sys/proc.h> 40 1.1 igy #include <sys/systm.h> 41 1.1 igy 42 1.1 igy #include <mips/cpuregs.h> 43 1.1 igy 44 1.1 igy #include <machine/bus.h> 45 1.1 igy 46 1.1 igy #include <hpcmips/vr/vripif.h> 47 1.1 igy #include <hpcmips/vr/vr4181aiureg.h> 48 1.1 igy #include <hpcmips/vr/vr4181dcureg.h> 49 1.1 igy 50 1.1 igy #define INBUFLEN 1024 /* length in u_int16_t */ 51 1.1 igy #define INPUTLEN 1000 52 1.1 igy #define SAMPLEFREQ 1000 53 1.1 igy #define PICKUPFREQ 100 54 1.1 igy #define PICKUPCOUNT (SAMPLEFREQ / PICKUPFREQ) 55 1.1 igy 56 1.1 igy #define ST_BUSY 0x01 57 1.1 igy #define ST_OVERRUN 0x02 58 1.1 igy 59 1.1 igy #define INBUF_MASK 0x3ff /* 2Kbyte */ 60 1.1 igy #define INBUF_RAW_SIZE (INBUFLEN * 4 + (INBUF_MASK + 1)) 61 1.1 igy 62 1.1 igy #ifdef VR4181AIU_DEBUG 63 1.1 igy int vr4181aiu_debug = 0; 64 1.1 igy #define DPRINTF(x) if (vr4181aiu_debug) printf x 65 1.1 igy #else 66 1.1 igy #define DPRINTF(x) 67 1.1 igy #endif 68 1.1 igy 69 1.1 igy 70 1.1 igy struct vr4181aiu_softc { 71 1.1 igy bus_space_tag_t sc_iot; 72 1.1 igy bus_space_handle_t sc_dcu1_ioh; 73 1.1 igy bus_space_handle_t sc_dcu2_ioh; 74 1.1 igy bus_space_handle_t sc_aiu_ioh; 75 1.1 igy u_int16_t *sc_inbuf_head; 76 1.1 igy u_int16_t *sc_inbuf_tail; 77 1.1 igy u_int16_t *sc_inbuf_which; 78 1.1 igy u_int16_t *sc_inbuf1; 79 1.1 igy u_int16_t *sc_inbuf2; 80 1.1 igy u_int16_t *sc_inbuf_raw; 81 1.1 igy int sc_status; 82 1.1 igy }; 83 1.1 igy 84 1.8 chs static int vr4181aiu_match(device_t, cfdata_t, void *); 85 1.8 chs static void vr4181aiu_attach(device_t, device_t, void *); 86 1.1 igy static int vr4181aiu_intr(void *); 87 1.1 igy 88 1.1 igy extern struct cfdriver vr4181aiu_cd; 89 1.1 igy 90 1.8 chs CFATTACH_DECL_NEW(vr4181aiu, sizeof(struct vr4181aiu_softc), 91 1.1 igy vr4181aiu_match, vr4181aiu_attach, NULL, NULL); 92 1.1 igy 93 1.1 igy dev_type_open(vr4181aiuopen); 94 1.1 igy dev_type_close(vr4181aiuclose); 95 1.1 igy dev_type_read(vr4181aiuread); 96 1.1 igy dev_type_write(vr4181aiuwrite); 97 1.1 igy 98 1.1 igy const struct cdevsw vr4181aiu_cdevsw = { 99 1.9 dholland .d_open = vr4181aiuopen, 100 1.9 dholland .d_close = vr4181aiuclose, 101 1.9 dholland .d_read = vr4181aiuread, 102 1.9 dholland .d_write = vr4181aiuwrite, 103 1.9 dholland .d_ioctl = noioctl, 104 1.9 dholland .d_stop = nostop, 105 1.9 dholland .d_tty = notty, 106 1.9 dholland .d_poll = nopoll, 107 1.9 dholland .d_mmap = nommap, 108 1.9 dholland .d_kqfilter = nokqfilter, 109 1.10 dholland .d_discard = nodiscard, 110 1.9 dholland .d_flag = 0 111 1.1 igy }; 112 1.1 igy 113 1.1 igy static int 114 1.8 chs vr4181aiu_match(device_t parent, cfdata_t cf, void *aux) 115 1.1 igy { 116 1.1 igy return 1; 117 1.1 igy } 118 1.1 igy 119 1.1 igy static void 120 1.1 igy vr4181aiu_init_inbuf(struct vr4181aiu_softc *sc) 121 1.1 igy { 122 1.1 igy /* 123 1.1 igy * XXXXXXXXXXXXXXXXX 124 1.1 igy * 125 1.1 igy * this is just a quick and dirty hack to locate the buffer 126 1.1 igy * in KSEG0 space. the only reason is that i want the physical 127 1.1 igy * address of the buffer. 128 1.1 igy * 129 1.1 igy * bus_dma framework should be used. 130 1.1 igy */ 131 1.1 igy static char inbufbase[INBUF_RAW_SIZE]; 132 1.1 igy 133 1.1 igy sc->sc_inbuf_raw = (u_int16_t *) inbufbase; 134 1.1 igy 135 1.1 igy sc->sc_inbuf1 = (u_int16_t *) ((((u_int32_t) sc->sc_inbuf_raw) 136 1.1 igy + INBUF_MASK) 137 1.1 igy & ~INBUF_MASK); 138 1.1 igy sc->sc_inbuf2 = sc->sc_inbuf1 + INBUFLEN; 139 1.1 igy } 140 1.1 igy 141 1.1 igy static void 142 1.1 igy vr4181aiu_disable(struct vr4181aiu_softc *sc) 143 1.1 igy { 144 1.1 igy /* irq clear */ 145 1.1 igy bus_space_write_2(sc->sc_iot, sc->sc_dcu2_ioh, 146 1.1 igy DCU_DMAITRQ_REG_W, DCU_MICEOP); 147 1.1 igy bus_space_write_2(sc->sc_iot, sc->sc_aiu_ioh, 148 1.1 igy VR4181AIU_INT_REG_W, 149 1.1 igy VR4181AIU_MIDLEINTR 150 1.1 igy | VR4181AIU_MSTINTR 151 1.1 igy | VR4181AIU_SIDLEINTR); 152 1.1 igy 153 1.1 igy /* disable microphone */ 154 1.1 igy bus_space_write_2(sc->sc_iot, sc->sc_aiu_ioh, 155 1.1 igy VR4181AIU_SEQ_REG_W, 0); 156 1.1 igy 157 1.1 igy /* disable ADC */ 158 1.1 igy bus_space_write_2(sc->sc_iot, sc->sc_aiu_ioh, 159 1.1 igy VR4181AIU_MCNT_REG_W, 0); 160 1.1 igy 161 1.1 igy /* disable DMA */ 162 1.1 igy bus_space_write_2(sc->sc_iot, sc->sc_dcu1_ioh, 163 1.1 igy DCU_AIUDMAMSK_REG_W, 0); 164 1.1 igy bus_space_write_2(sc->sc_iot, sc->sc_dcu2_ioh, 165 1.1 igy DCU_DMAITMK_REG_W, 0); 166 1.1 igy 167 1.1 igy sc->sc_status = 0; 168 1.1 igy } 169 1.1 igy 170 1.1 igy static void 171 1.8 chs vr4181aiu_attach(device_t parent, device_t self, void *aux) 172 1.1 igy { 173 1.1 igy struct vrip_attach_args *va = aux; 174 1.8 chs struct vr4181aiu_softc *sc = device_private(self); 175 1.1 igy 176 1.1 igy vr4181aiu_init_inbuf(sc); 177 1.1 igy memset(sc->sc_inbuf1, 0x55, INBUFLEN * 2); 178 1.1 igy memset(sc->sc_inbuf2, 0xaa, INBUFLEN * 2); 179 1.1 igy 180 1.1 igy sc->sc_status = 0; 181 1.1 igy sc->sc_iot = va->va_iot; 182 1.1 igy 183 1.1 igy if (bus_space_map(sc->sc_iot, 184 1.1 igy VR4181AIU_DCU1_BASE, VR4181AIU_DCU1_SIZE, 185 1.1 igy 0, &sc->sc_dcu1_ioh)) 186 1.1 igy goto out_dcu1; 187 1.1 igy if (bus_space_map(sc->sc_iot, 188 1.1 igy VR4181AIU_DCU2_BASE, VR4181AIU_DCU2_SIZE, 189 1.1 igy 0, &sc->sc_dcu2_ioh)) 190 1.1 igy goto out_dcu2; 191 1.1 igy if (bus_space_map(sc->sc_iot, 192 1.1 igy VR4181AIU_AIU_BASE, VR4181AIU_AIU_SIZE, 193 1.1 igy 0, &sc->sc_aiu_ioh)) 194 1.1 igy goto out_aiu; 195 1.1 igy 196 1.1 igy /* 197 1.1 igy * reset AIU 198 1.1 igy */ 199 1.1 igy bus_space_write_2(sc->sc_iot, sc->sc_aiu_ioh, 200 1.1 igy VR4181AIU_SEQ_REG_W, VR4181AIU_AIURST); 201 1.1 igy bus_space_write_2(sc->sc_iot, sc->sc_aiu_ioh, 202 1.1 igy VR4181AIU_SEQ_REG_W, 0); 203 1.1 igy 204 1.1 igy /* 205 1.1 igy * set sample rate (1kHz fixed) 206 1.1 igy * XXXX 207 1.1 igy * assume to PCLK is 32.768MHz 208 1.1 igy */ 209 1.1 igy bus_space_write_2(sc->sc_iot, sc->sc_aiu_ioh, 210 1.1 igy VR4181AIU_MCNVC_END, 211 1.1 igy 32768000 / SAMPLEFREQ); 212 1.1 igy 213 1.1 igy /* 214 1.1 igy * XXXX 215 1.1 igy * assume to PCLK is 32.768MHz 216 1.1 igy * DAVREF_SETUP = 5usec * PCLK = 163.84 217 1.1 igy */ 218 1.1 igy bus_space_write_2(sc->sc_iot, sc->sc_aiu_ioh, 219 1.1 igy VR4181AIU_DAVREF_SETUP_REG_W, 164); 220 1.1 igy 221 1.1 igy vr4181aiu_disable(sc); 222 1.1 igy 223 1.1 igy if (vrip_intr_establish(va->va_vc, va->va_unit, 0, 224 1.1 igy IPL_BIO, vr4181aiu_intr, sc) == NULL) { 225 1.1 igy printf("%s: can't establish interrupt\n", 226 1.8 chs device_xname(self)); 227 1.1 igy return; 228 1.1 igy } 229 1.1 igy 230 1.1 igy printf("\n"); 231 1.1 igy return; 232 1.1 igy 233 1.1 igy out_aiu: 234 1.1 igy bus_space_unmap(sc->sc_iot, sc->sc_dcu2_ioh, VR4181AIU_DCU2_SIZE); 235 1.1 igy out_dcu2: 236 1.1 igy bus_space_unmap(sc->sc_iot, sc->sc_dcu1_ioh, VR4181AIU_DCU1_SIZE); 237 1.1 igy out_dcu1: 238 1.1 igy printf(": can't map i/o space\n"); 239 1.1 igy } 240 1.1 igy 241 1.1 igy int 242 1.4 christos vr4181aiuopen(dev_t dev, int flag, int mode, struct lwp *l) 243 1.1 igy { 244 1.1 igy struct vr4181aiu_softc *sc; 245 1.1 igy 246 1.7 dholland sc = device_lookup_private(&vr4181aiu_cd, minor(dev)); 247 1.6 cegger if (sc == NULL) 248 1.1 igy return ENXIO; 249 1.1 igy 250 1.1 igy if (sc->sc_status & ST_BUSY) 251 1.1 igy return EBUSY; 252 1.1 igy 253 1.1 igy sc->sc_inbuf_head = sc->sc_inbuf_tail 254 1.1 igy = sc->sc_inbuf_which = sc->sc_inbuf1; 255 1.1 igy sc->sc_status &= ~ST_OVERRUN; 256 1.1 igy 257 1.1 igy /* setup DMA */ 258 1.1 igy /* reset */ 259 1.1 igy bus_space_write_2(sc->sc_iot, sc->sc_dcu1_ioh, 260 1.1 igy DCU_DMARST_REG_W, 0); 261 1.1 igy bus_space_write_2(sc->sc_iot, sc->sc_dcu1_ioh, 262 1.1 igy DCU_DMARST_REG_W, DCU_DMARST); 263 1.1 igy /* dest1 <- sc_inbuf1 */ 264 1.1 igy bus_space_write_2(sc->sc_iot, sc->sc_dcu1_ioh, 265 1.1 igy DCU_MICDEST1REG1_W, 266 1.1 igy MIPS_KSEG0_TO_PHYS(sc->sc_inbuf1) & 0xffff); 267 1.1 igy bus_space_write_2(sc->sc_iot, sc->sc_dcu1_ioh, 268 1.1 igy DCU_MICDEST1REG2_W, 269 1.1 igy MIPS_KSEG0_TO_PHYS(sc->sc_inbuf1) >> 16); 270 1.1 igy /* dest2 <- sc_inbuf2 */ 271 1.1 igy bus_space_write_2(sc->sc_iot, sc->sc_dcu1_ioh, 272 1.1 igy DCU_MICDEST2REG1_W, 273 1.1 igy MIPS_KSEG0_TO_PHYS(sc->sc_inbuf2) & 0xffff); 274 1.1 igy bus_space_write_2(sc->sc_iot, sc->sc_dcu1_ioh, 275 1.1 igy DCU_MICDEST2REG2_W, 276 1.1 igy MIPS_KSEG0_TO_PHYS(sc->sc_inbuf2) >> 16); 277 1.1 igy /* record length <- INPUTLEN */ 278 1.1 igy bus_space_write_2(sc->sc_iot, sc->sc_dcu2_ioh, 279 1.1 igy DCU_MICRCLEN_REG_W, INPUTLEN); 280 1.1 igy /* config <- auto load */ 281 1.1 igy bus_space_write_2(sc->sc_iot, sc->sc_dcu2_ioh, 282 1.1 igy DCU_MICDMACFG_REG_W, DCU_MICLOAD); 283 1.1 igy /* irq <- irq clear */ 284 1.1 igy bus_space_write_2(sc->sc_iot, sc->sc_dcu2_ioh, 285 1.1 igy DCU_DMAITRQ_REG_W, DCU_MICEOP); 286 1.1 igy /* control <- INC */ 287 1.1 igy bus_space_write_2(sc->sc_iot, sc->sc_dcu2_ioh, 288 1.1 igy DCU_DMACTL_REG_W, DCU_MICCNT_INC); 289 1.1 igy /* irq mask <- microphone end of process */ 290 1.1 igy bus_space_write_2(sc->sc_iot, sc->sc_dcu2_ioh, 291 1.1 igy DCU_DMAITMK_REG_W, DCU_MICEOP_ENABLE); 292 1.1 igy 293 1.1 igy /* enable DMA */ 294 1.1 igy bus_space_write_2(sc->sc_iot, sc->sc_dcu1_ioh, 295 1.1 igy DCU_AIUDMAMSK_REG_W, DCU_ENABLE_MIC); 296 1.1 igy 297 1.1 igy /* enable ADC */ 298 1.1 igy bus_space_write_2(sc->sc_iot, sc->sc_aiu_ioh, 299 1.1 igy VR4181AIU_MCNT_REG_W, VR4181AIU_ADENAIU); 300 1.1 igy 301 1.1 igy /* enable microphone */ 302 1.1 igy bus_space_write_2(sc->sc_iot, sc->sc_aiu_ioh, 303 1.1 igy VR4181AIU_SEQ_REG_W, VR4181AIU_AIUMEN); 304 1.1 igy 305 1.1 igy sc->sc_status |= ST_BUSY; 306 1.1 igy 307 1.1 igy return 0; 308 1.1 igy } 309 1.1 igy 310 1.1 igy int 311 1.4 christos vr4181aiuclose(dev_t dev, int flag, int mode, struct lwp *l) 312 1.1 igy { 313 1.6 cegger vr4181aiu_disable(device_lookup_private(&vr4181aiu_cd, minor(dev))); 314 1.1 igy return 0; 315 1.1 igy } 316 1.1 igy 317 1.1 igy int 318 1.1 igy vr4181aiuread(dev_t dev, struct uio *uio, int flag) 319 1.1 igy { 320 1.1 igy struct vr4181aiu_softc *sc; 321 1.1 igy int s; 322 1.1 igy u_int16_t *fence; 323 1.1 igy int avail; 324 1.1 igy int count; 325 1.1 igy u_int8_t tmp[INPUTLEN / PICKUPCOUNT]; 326 1.1 igy u_int16_t *src; 327 1.1 igy u_int8_t *dst; 328 1.1 igy 329 1.6 cegger sc = device_lookup_private(&vr4181aiu_cd, minor(dev)); 330 1.1 igy 331 1.1 igy src = sc->sc_inbuf_tail; 332 1.1 igy s = splbio(); 333 1.1 igy if (src == sc->sc_inbuf_head) { 334 1.1 igy /* wait for DMA to complete writing */ 335 1.1 igy tsleep(sc, PRIBIO, "aiu read", 0); 336 1.1 igy /* now sc_inbuf_head points alternate buffer */ 337 1.1 igy } 338 1.1 igy splx(s); 339 1.1 igy 340 1.1 igy fence = sc->sc_inbuf_which == sc->sc_inbuf1 341 1.1 igy ? &sc->sc_inbuf1[INPUTLEN] 342 1.1 igy : &sc->sc_inbuf2[INPUTLEN]; 343 1.1 igy avail = (fence - src) / PICKUPCOUNT; 344 1.11 riastrad count = uimin(avail, uio->uio_resid); 345 1.1 igy dst = tmp; 346 1.1 igy while (count > 0) { 347 1.1 igy *dst++ = (u_int8_t) (*src >> 2); 348 1.1 igy src += PICKUPCOUNT; 349 1.1 igy count--; 350 1.1 igy } 351 1.1 igy 352 1.1 igy if (src < fence) { 353 1.1 igy sc->sc_inbuf_tail = src; 354 1.1 igy } else { 355 1.1 igy /* alter the buffer */ 356 1.1 igy sc->sc_inbuf_tail 357 1.1 igy = sc->sc_inbuf_which 358 1.1 igy = sc->sc_inbuf_which == sc->sc_inbuf1 359 1.1 igy ? sc->sc_inbuf2 : sc->sc_inbuf1; 360 1.1 igy } 361 1.1 igy 362 1.1 igy return uiomove(tmp, dst - tmp, uio); 363 1.1 igy } 364 1.1 igy 365 1.1 igy int 366 1.1 igy vr4181aiuwrite(dev_t dev, struct uio *uio, int flag) 367 1.1 igy { 368 1.1 igy return 0; 369 1.1 igy } 370 1.1 igy 371 1.1 igy /* 372 1.1 igy * interrupt handler 373 1.1 igy */ 374 1.1 igy static int 375 1.1 igy vr4181aiu_intr(void *arg) 376 1.1 igy { 377 1.1 igy struct vr4181aiu_softc *sc = arg; 378 1.1 igy 379 1.1 igy if (!(sc->sc_status & ST_BUSY)) { 380 1.1 igy printf("vr4181aiu_intr: stray interrupt\n"); 381 1.1 igy vr4181aiu_disable(sc); 382 1.1 igy return 0; 383 1.1 igy } 384 1.1 igy 385 1.1 igy /* irq clear */ 386 1.1 igy bus_space_write_2(sc->sc_iot, sc->sc_dcu2_ioh, 387 1.1 igy DCU_DMAITRQ_REG_W, DCU_MICEOP); 388 1.1 igy 389 1.1 igy if (sc->sc_inbuf_head == sc->sc_inbuf1) { 390 1.1 igy if (sc->sc_inbuf_tail != sc->sc_inbuf1) 391 1.1 igy sc->sc_status |= ST_OVERRUN; 392 1.1 igy sc->sc_inbuf_head = sc->sc_inbuf2; 393 1.1 igy } else { 394 1.1 igy if (sc->sc_inbuf_tail != sc->sc_inbuf2) 395 1.1 igy sc->sc_status |= ST_OVERRUN; 396 1.1 igy sc->sc_inbuf_head = sc->sc_inbuf1; 397 1.1 igy } 398 1.1 igy 399 1.1 igy if (sc->sc_status & ST_OVERRUN) { 400 1.1 igy printf("vr4181aiu_intr: overrun\n"); 401 1.1 igy } 402 1.1 igy 403 1.1 igy DPRINTF(("vr4181aiu_intr: sc_inbuf1 = %04x, sc_inbuf2 = %04x\n", 404 1.1 igy sc->sc_inbuf1[0], sc->sc_inbuf2[0])); 405 1.1 igy 406 1.1 igy wakeup(sc); 407 1.1 igy 408 1.1 igy return 0; 409 1.1 igy } 410