11.24Sriastrad/*	$NetBSD: sb_ofisa.c,v 1.24 2023/04/18 06:37:25 riastradh Exp $	*/
21.1Sthorpej
31.1Sthorpej/*-
41.1Sthorpej * Copyright (c) 1998 The NetBSD Foundation, Inc.
51.1Sthorpej * All rights reserved.
61.1Sthorpej *
71.1Sthorpej * This code is derived from software contributed to The NetBSD Foundation
81.1Sthorpej * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
91.1Sthorpej * NASA Ames Research Center.
101.1Sthorpej *
111.1Sthorpej * Redistribution and use in source and binary forms, with or without
121.1Sthorpej * modification, are permitted provided that the following conditions
131.1Sthorpej * are met:
141.1Sthorpej * 1. Redistributions of source code must retain the above copyright
151.1Sthorpej *    notice, this list of conditions and the following disclaimer.
161.1Sthorpej * 2. Redistributions in binary form must reproduce the above copyright
171.1Sthorpej *    notice, this list of conditions and the following disclaimer in the
181.1Sthorpej *    documentation and/or other materials provided with the distribution.
191.1Sthorpej *
201.1Sthorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
211.1Sthorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
221.1Sthorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
231.1Sthorpej * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
241.1Sthorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
251.1Sthorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
261.1Sthorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
271.1Sthorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
281.1Sthorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
291.1Sthorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
301.1Sthorpej * POSSIBILITY OF SUCH DAMAGE.
311.1Sthorpej */
321.6Slukem
331.6Slukem#include <sys/cdefs.h>
341.24Sriastrad__KERNEL_RCSID(0, "$NetBSD: sb_ofisa.c,v 1.24 2023/04/18 06:37:25 riastradh Exp $");
351.1Sthorpej
361.1Sthorpej#include <sys/param.h>
371.1Sthorpej#include <sys/systm.h>
381.1Sthorpej#include <sys/device.h>
391.1Sthorpej
401.15Sad#include <sys/bus.h>
411.15Sad#include <sys/intr.h>
421.1Sthorpej
431.1Sthorpej#include <sys/audioio.h>
441.19Sisaki#include <dev/audio/audio_if.h>
451.3Sthorpej#include <dev/midi_if.h>
461.1Sthorpej
471.1Sthorpej#include <dev/ofw/openfirm.h>
481.1Sthorpej#include <dev/isa/isavar.h>
491.1Sthorpej#include <dev/ofisa/ofisavar.h>
501.1Sthorpej
511.1Sthorpej#include <dev/isa/sbreg.h>
521.1Sthorpej#include <dev/isa/sbvar.h>
531.1Sthorpej#include <dev/isa/sbdspvar.h>
541.1Sthorpej
551.16Scubeint	sb_ofisa_match(device_t, cfdata_t, void *);
561.16Scubevoid	sb_ofisa_attach(device_t, device_t, void *);
571.1Sthorpej
581.16ScubeCFATTACH_DECL_NEW(sb_ofisa, sizeof(struct sbdsp_softc),
591.10Sthorpej    sb_ofisa_match, sb_ofisa_attach, NULL, NULL);
601.1Sthorpej
611.20Sthorpejstatic const struct device_compatible_entry compat_data[] = {
621.20Sthorpej	{ .compat = "pnpPNP,b000" },	/* generic SB 1.5 */
631.20Sthorpej	{ .compat = "pnpPNP,b001" },	/* generic SB 2.0 */
641.20Sthorpej	{ .compat = "pnpPNP,b002" },	/* generic SB Pro */
651.20Sthorpej	{ .compat = "pnpPNP,b003" },	/* generic SB 16 */
661.22Sthorpej	DEVICE_COMPAT_EOL
671.20Sthorpej};
681.20Sthorpej
691.1Sthorpejint
701.16Scubesb_ofisa_match(device_t parent, cfdata_t cf, void *aux)
711.1Sthorpej{
721.1Sthorpej	struct ofisa_attach_args *aa = aux;
731.1Sthorpej
741.20Sthorpej	/*
751.20Sthorpej	 * Use a low match priority so that a more specific driver
761.20Sthorpej	 * can match, e.g. a native ESS driver.
771.20Sthorpej	 */
781.23Sthorpej	return of_compatible_match(aa->oba.oba_phandle, compat_data) ? 1 : 0;
791.1Sthorpej}
801.1Sthorpej
811.1Sthorpejvoid
821.16Scubesb_ofisa_attach(device_t parent, device_t self, void *aux)
831.1Sthorpej{
841.14Sthorpej	struct sbdsp_softc *sc = device_private(self);
851.1Sthorpej	struct ofisa_attach_args *aa = aux;
861.1Sthorpej	struct ofisa_reg_desc reg;
871.1Sthorpej	struct ofisa_intr_desc intr;
881.1Sthorpej	struct ofisa_dma_desc dma[2];
891.18Schristos	int ndrq;
901.1Sthorpej
911.16Scube	sc->sc_dev = self;
921.16Scube
931.1Sthorpej	/*
941.1Sthorpej	 * We're living on an OFW.  We have to ask the OFW what our
951.1Sthorpej	 * registers and interrupts properties look like.
961.1Sthorpej	 *
971.1Sthorpej	 * We expect:
981.1Sthorpej	 *
991.1Sthorpej	 *	1 i/o register region
1001.1Sthorpej	 *	1 interrupt
1011.11Swiz	 *	1 or 2 DMA channels
1021.1Sthorpej	 */
1031.1Sthorpej
1041.1Sthorpej	n = ofisa_reg_get(aa->oba.oba_phandle, &reg, 1);
1051.1Sthorpej	if (n != 1) {
1061.18Schristos		aprint_error(": error getting register data\n");
1071.1Sthorpej		return;
1081.1Sthorpej	}
1091.1Sthorpej	if (reg.type != OFISA_REG_TYPE_IO) {
1101.18Schristos		aprint_error(": register type not i/o\n");
1111.1Sthorpej		return;
1121.1Sthorpej	}
1131.1Sthorpej	if (reg.len != SB_NPORT && reg.len != SBP_NPORT) {
1141.18Schristos		aprint_error(": weird register size (%lu, expected %d or %d)\n",
1151.1Sthorpej		    (unsigned long)reg.len, SB_NPORT, SBP_NPORT);
1161.1Sthorpej		return;
1171.1Sthorpej	}
1181.1Sthorpej
1191.1Sthorpej	n = ofisa_intr_get(aa->oba.oba_phandle, &intr, 1);
1201.1Sthorpej	if (n != 1) {
1211.18Schristos		aprint_error(": error getting interrupt data\n");
1221.1Sthorpej		return;
1231.1Sthorpej	}
1241.1Sthorpej
1251.1Sthorpej	ndrq = ofisa_dma_get(aa->oba.oba_phandle, dma, 2);
1261.1Sthorpej	if (ndrq != 1 && ndrq != 2) {
1271.18Schristos		aprint_error(": error getting DMA data\n");
1281.1Sthorpej		return;
1291.1Sthorpej	}
1301.1Sthorpej
1311.1Sthorpej	sc->sc_ic = aa->ic;
1321.1Sthorpej
1331.1Sthorpej	sc->sc_iot = aa->iot;
1341.4Smycroft	if (bus_space_map(sc->sc_iot, reg.addr, reg.len, 0, &sc->sc_ioh)) {
1351.16Scube		aprint_error(": unable to map register space\n");
1361.1Sthorpej		return;
1371.1Sthorpej	}
1381.1Sthorpej
1391.4Smycroft	/* XXX These are only for setting chip configuration registers. */
1401.4Smycroft	sc->sc_iobase = reg.addr;
1411.1Sthorpej	sc->sc_irq = intr.irq;
1421.1Sthorpej
1431.1Sthorpej	sc->sc_drq8 = DRQUNK;
1441.1Sthorpej	sc->sc_drq16 = DRQUNK;
1451.1Sthorpej
1461.1Sthorpej	for (n = 0; n < ndrq; n++) {
1471.1Sthorpej		/* XXX check mode? */
1481.1Sthorpej		switch (dma[n].width) {
1491.1Sthorpej		case 8:
1501.1Sthorpej			if (sc->sc_drq8 == DRQUNK)
1511.1Sthorpej				sc->sc_drq8 = dma[n].drq;
1521.1Sthorpej			break;
1531.1Sthorpej		case 16:
1541.1Sthorpej			if (sc->sc_drq16 == DRQUNK)
1551.1Sthorpej				sc->sc_drq16 = dma[n].drq;
1561.1Sthorpej			break;
1571.1Sthorpej		default:
1581.16Scube			aprint_error(": weird DMA width %d\n", dma[n].width);
1591.1Sthorpej			return;
1601.1Sthorpej		}
1611.1Sthorpej	}
1621.1Sthorpej
1631.1Sthorpej	if (sc->sc_drq8 == DRQUNK) {
1641.16Scube		aprint_error(": no 8-bit DMA channel\n");
1651.1Sthorpej		return;
1661.1Sthorpej	}
1671.1Sthorpej
1681.1Sthorpej	if (sbmatch(sc) == 0) {
1691.16Scube		aprint_error(": sbmatch failed\n");
1701.1Sthorpej		return;
1711.1Sthorpej	}
1721.4Smycroft
1731.24Sriastrad	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
1741.24Sriastrad	mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_AUDIO);
1751.24Sriastrad
1761.5Sthorpej	sc->sc_ih = isa_intr_establish(aa->ic, intr.irq, IST_EDGE, IPL_AUDIO,
1771.4Smycroft	    sbdsp_intr, sc);
1781.1Sthorpej
1791.18Schristos	ofisa_print_model(self, aa->oba.oba_phandle);
1801.1Sthorpej
1811.1Sthorpej	sbattach(sc);
1821.1Sthorpej}
183