code:
#	$OpenBSD: files.sparc64,v 1.147 2015/07/17 22:52:29 tedu Exp $
#	$NetBSD: files.sparc64,v 1.50 2001/08/10 20:53:50 eeh Exp $

# maxpartitions must be first item in files.${ARCH}
maxpartitions 16

maxusers 2 8 1024

define	mainbus {}
device	mainbus: pcibus, mainbus
attach	mainbus at root

device	ssm: mainbus
attach	ssm at mainbus
file	arch/sparc64/dev/ssm.c			ssm

define	upa {}
device	upa
attach	upa at mainbus
file	arch/sparc64/dev/upa.c			upa

define	central {}
device	central
attach	central at mainbus
file	arch/sparc64/dev/central.c		central

define	fhc {}
device	fhc
file	arch/sparc64/dev/fhc.c			fhc

attach	fhc at central with fhc_central
file	arch/sparc64/dev/fhc_central.c		fhc_central

attach	fhc at mainbus with fhc_mainbus
file	arch/sparc64/dev/fhc_mainbus.c		fhc_mainbus

device	clkbrd
attach	clkbrd at fhc
file	arch/sparc64/dev/clkbrd.c		clkbrd

device	environ
attach	environ at fhc
file	arch/sparc64/dev/environ.c		environ

device	vbus {}
attach	vbus at mainbus
file	arch/sparc64/dev/vbus.c			vbus

device	cbus {}
attach	cbus at vbus
file	arch/sparc64/dev/cbus.c			cbus

# Sun HME Ethernet controllers
device	hme: ether, ifnet, mii, ifmedia
file	dev/ic/hme.c				hme

device	uperf
file	arch/sparc64/dev/uperf.c		uperf needs-flag

# Framebuffer support

file	arch/sparc64/dev/fb.c
include	"dev/wscons/files.wscons"
include	"dev/rasops/files.rasops"
include	"dev/wsfont/files.wsfont"

device	pcons: wsemuldisplaydev
attach	pcons at mainbus
file	arch/sparc64/dev/pcons.c		pcons needs-flag

device	prtc
attach	prtc at mainbus
file	arch/sparc64/dev/prtc.c			prtc

include	"dev/sbus/files.sbus"
include	"dev/mii/files.mii"
include	"dev/atapiscsi/files.atapiscsi"
include	"dev/ata/files.ata"
include	"dev/pckbc/files.pckbc"
include	"dev/pcmcia/files.pcmcia"

# Needs to be declared before including files.sun
device	zs {[channel = -1]}

# Sun specific files
include	"dev/sun/files.sun"

define	iommu
define	viommu
file	arch/sparc64/dev/iommu.c		iommu | viommu | sbus
file	arch/sparc64/dev/viommu.c		viommu

define msi
file	arch/sparc64/dev/msi.c			msi

attach	sbus at mainbus with sbus_mb
attach	sbus at xbox with sbus_xbox
file	arch/sparc64/dev/sbus.c			sbus

device	creator: wsemuldisplaydev, rasops32, firmload
attach	creator at mainbus, upa
file	arch/sparc64/dev/creator.c		creator

device	gfb: wsemuldisplaydev, rasops32
attach	gfb at upa
file	arch/sparc64/dev/gfb.c			gfb

include	"dev/pci/files.pci"
major	{wd = 12}

device	psycho: pcibus, iommu
attach	psycho at mainbus
file	arch/sparc64/dev/psycho.c		psycho

device	schizo: pcibus, iommu
attach	schizo at mainbus
file	arch/sparc64/dev/schizo.c		schizo

device	pyro: pcibus, iommu, msi
attach	pyro at mainbus
file	arch/sparc64/dev/pyro.c			pyro

device	vpci: pcibus, viommu, msi
attach	vpci at mainbus
file	arch/sparc64/dev/vpci.c			vpci

file	arch/sparc64/dev/pci_machdep.c		psycho | schizo | pyro | vpci

attach	hme at pci with hme_pci
file	dev/pci/if_hme_pci.c			hme_pci

define	ifb_ident
file	arch/sparc64/dev/ifb_ident.c		ifb_ident

device	vgafb: ifb_ident, wsemuldisplaydev, rasops_bswap, rasops8, rasops16, rasops32
attach	vgafb at pci
file	arch/sparc64/dev/vgafb.c		vgafb

device	machfb: wsemuldisplaydev, rasops_bswap, rasops8
attach	machfb at pci
file	arch/sparc64/dev/machfb.c		machfb

device	radeonfb: wsemuldisplaydev, rasops_bswap, rasops8, rasops32
attach	radeonfb at pci
file	arch/sparc64/dev/radeonfb.c		radeonfb

device	ifb: ifb_ident, wsemuldisplaydev, rasops_bswap, rasops8
attach	ifb at pci
file	arch/sparc64/dev/ifb.c			ifb

device	raptor: wsemuldisplaydev, rasops_bswap, rasops8
attach	raptor at pci
file	arch/sparc64/dev/raptor.c		raptor

device	gfxp: wsemuldisplaydev, rasops_bswap, rasops8, rasops32
attach	gfxp at pci
file	arch/sparc64/dev/gfxp.c			gfxp

device	sbbc
attach	sbbc at pci
file	arch/sparc64/dev/sbbc.c			sbbc needs-flag

# Starfire PC is needed for sbus and pci
file	arch/sparc64/dev/starfire.c		sbus | psycho

device	ebus {[addr = -1]}: pcibus
attach	ebus at pci
attach	ebus at mainbus with ebus_mainbus
file	arch/sparc64/dev/ebus.c			ebus
file	arch/sparc64/dev/ebus_mainbus.c		ebus_mainbus

attach	uperf at ebus with uperf_ebus
file	arch/sparc64/dev/uperf_ebus.c		uperf_ebus

device	clock: mk48txx
attach	clock at mainbus, sbus with clock_sbus
attach	clock at ebus with clock_ebus
attach	clock at fhc with clock_fhc

device	timer
attach	timer at mainbus, sbus

device	eeprom
attach	eeprom at sbus, ebus
file	arch/sparc64/sparc64/clock.c

device	cmp {}
attach	cmp at mainbus
file	arch/sparc64/dev/cmp.c			cmp

device	core {}
attach	core at cmp
file	arch/sparc64/dev/core.c			core

device	cpu
attach	cpu at mainbus, cmp, core
file	arch/sparc64/sparc64/cpu.c

device	auxio
attach	auxio at ebus with auxio_ebus
attach	auxio at sbus with auxio_sbus
file	arch/sparc64/dev/auxio.c		auxio

device	bbc
attach	bbc at ebus
file	arch/sparc64/dev/bbc.c			bbc

device	beeper
attach	beeper at ebus
file	arch/sparc64/dev/beeper.c		beeper

device	beep
attach	beep at ebus
file	arch/sparc64/dev/beep.c			beep

device	led
attach	led at ebus
file	arch/sparc64/dev/led.c			led

device	lom
attach	lom at ebus
file	arch/sparc64/dev/lom.c			lom

device	pmc
attach	pmc at ebus
file	arch/sparc64/dev/pmc.c			pmc

device	ppm
attach	ppm at ebus
file	arch/sparc64/dev/ppm.c			ppm

device	power
attach	power at ebus
file	arch/sparc64/dev/power.c		power

device	rtc
attach	rtc at ebus
file	arch/sparc64/dev/rtc.c			rtc

device	sab {}
attach	sab at ebus
device	sabtty
attach	sabtty at sab
file	arch/sparc64/dev/sab.c			sab | sabtty needs-flag

device	audioce: audio
attach	audioce at ebus
file	arch/sparc64/dev/ce4231.c		audioce

file	arch/sparc64/dev/stp_sbus.c		stp_sbus

attach	wbsd at ebus with wbsd_ebus
file	arch/sparc64/dev/wbsd_ebus.c		wbsd

#
# Console related stuff
#

attach	com at ebus with com_ebus
file	arch/sparc64/dev/com_ebus.c		com_ebus

attach	pckbc at ebus with pckbc_ebus
file	arch/sparc64/dev/pckbc_ebus.c		pckbc_ebus

device	comkbd: wskbddev, sunkbd
attach	comkbd at ebus
file	arch/sparc64/dev/comkbd_ebus.c		comkbd

device	comms: wsmousedev, sunms
attach	comms at ebus
file	arch/sparc64/dev/comms_ebus.c		comms

attach	lpt at ebus with lpt_ebus
file	arch/sparc64/dev/lpt_ebus.c		lpt_ebus

#device	zs {[channel = -1]}
attach	zs at sbus with zs_sbus
attach	zs at fhc with zs_fhc
file	arch/sparc64/dev/zs.c			zs
file	dev/ic/z8530sc.c			zs

define	zstty {}
device	zstty: tty
attach	zstty at zs
file	dev/ic/z8530tty.c			zstty needs-flag

device	zskbd: wskbddev, sunkbd
attach	zskbd at zs
file	arch/sparc64/dev/z8530kbd.c		zskbd

# Floppy drive
device	fdc {}
attach	fdc at sbus with fdc_sbus
attach	fdc at ebus with fdc_ebus
device	fd: disk
attach	fd at fdc
file	arch/sparc64/dev/fd.c			fdc | fd	needs-flag

# PCF8584 I2C Controller
attach	pcfiic at ebus with pcfiic_ebus
file	arch/sparc64/dev/pcfiic_ebus.c		pcfiic_ebus

file	arch/sparc64/dev/consinit.c

file	arch/sparc64/fpu/fpu.c
file	arch/sparc64/fpu/fpu_add.c
file	arch/sparc64/fpu/fpu_compare.c
file	arch/sparc64/fpu/fpu_div.c
file	arch/sparc64/fpu/fpu_explode.c
file	arch/sparc64/fpu/fpu_implode.c
file	arch/sparc64/fpu/fpu_mul.c
file	arch/sparc64/fpu/fpu_sqrt.c
file	arch/sparc64/fpu/fpu_subr.c

file	arch/sparc64/sparc64/autoconf.c
file	arch/sparc64/sparc64/busop.c
file	arch/sparc64/sparc64/cache.c
file	arch/sparc64/sparc64/conf.c
file	arch/sparc64/sparc64/disksubr.c
file	arch/sparc64/sparc64/emul.c
file	arch/sparc64/sparc64/hvcall.S		sun4v
file	arch/sparc64/sparc64/in_cksum.S
file	arch/sparc64/sparc64/in4_cksum.c
file	arch/sparc64/sparc64/intr.c
file	arch/sparc64/sparc64/ipifuncs.c		multiprocessor
file	arch/sparc64/sparc64/kgdb_machdep.c	kgdb
# sparc64/sparc64/locore.s is handled specially in the makefile,
# because it must come first in the "ld" command line.
file	arch/sparc64/sparc64/lock_machdep.c	multiprocessor
file	arch/sparc64/sparc64/machdep.c
file	arch/sparc64/sparc64/mdesc.c		sun4v
file	arch/sparc64/sparc64/mem.c
file	arch/sparc64/sparc64/mutex.S
file	arch/sparc64/sparc64/openprom.c
file	arch/sparc64/sparc64/openfirm.c
file	arch/sparc64/sparc64/ofw_machdep.c
file	arch/sparc64/sparc64/pmap.c
file	arch/sparc64/sparc64/process_machdep.c
file	arch/sparc64/sparc64/sys_machdep.c
file	arch/sparc64/sparc64/trap.c
file	arch/sparc64/sparc64/vm_machdep.c

file	arch/sparc64/sparc64/db_interface.c	ddb | kgdb
file	arch/sparc64/sparc64/db_trace.c		ddb
file	arch/sparc64/sparc64/db_disasm.c	ddb

#
# Machine-independent SCSI drivers
#
include	"scsi/files.scsi"
major	{sd = 7}
major	{vnd = 8}
major	{cd = 18}
major	{rd = 5}

#
# Machine-independent I2C drivers
#
include "dev/i2c/files.i2c"
file	arch/sparc64/dev/ofwi2c.c		i2cbus

#
# Machine-independent SD/MMC drivers
#
include "dev/sdmmc/files.sdmmc"

#
# Machine-independent USB drivers
#
include	"dev/usb/files.usb"

#
# Cardbus
#
include "dev/cardbus/files.cardbus"
file	arch/sparc64/sparc64/rbus_machdep.c	cardbus

#
# Machine-dependent I2C drivers
#

# PCF8591 ADC I2C Sensor (needs ofw, so sparc64 only for now)
device	pcfadc
attach	pcfadc at i2c
file	arch/sparc64/dev/pcf8591_ofw.c		pcfadc

# PCF8591 ADC I2C Sensor (envctrl, so sparc64 only)
device	ecadc
attach	ecadc at i2c
file	arch/sparc64/dev/pcf8591_envctrl.c	ecadc

# TDA8444 (sparc64 only for now)
device	tda
attach	tda at i2c
file	arch/sparc64/dev/tda.c			tda needs-flag

#
# Machine-independent GPIO drivers
#
include "dev/gpio/files.gpio"

#
# Machine-independent 1-Wire drivers
#
include "dev/onewire/files.onewire"

# Virtual console
device	vcons
attach	vcons at vbus
file	arch/sparc64/dev/vcons.c		vcons needs-flag

# Virtual random number generator
device	vrng
attach	vrng at vbus
file	arch/sparc64/dev/vrng.c			vrng

# Virtual rtc
device	vrtc
attach	vrtc at vbus
file	arch/sparc64/dev/vrtc.c			vrtc

define	ldc
file	arch/sparc64/dev/ldc.c			ldc

# Virtual disk server
device	vds {}
attach	vds at cbus
file	arch/sparc64/dev/vds.c			vds

# Virtual disk server port
device	vdsp: ldc
attach	vdsp at vds
file	arch/sparc64/dev/vdsp.c			vdsp needs-flag

# Virtual disk
device	vdsk: ldc, scsi
attach	vdsk at cbus
file	arch/sparc64/dev/vdsk.c			vdsk

# Virtual network switch
device	vsw {}
attach	vsw at cbus
file	arch/sparc64/dev/vsw.c			vsw

# Virtual network
device	vnet: ldc
attach	vnet at cbus, vsw
file	arch/sparc64/dev/vnet.c			vnet

# Virtual console concentrator
device	vcc {}
attach	vcc at cbus
file	arch/sparc64/dev/vcc.c			vcc

device	vcctty: ldc
attach	vcctty at vcc
file	arch/sparc64/dev/vcctty.c		vcctty needs-flag

# Virtual channel
device	vldc {}
attach	vldc at cbus
file	arch/sparc64/dev/vldc.c			vldc

device	vldcp: ldc
attach	vldcp at vldc
file	arch/sparc64/dev/vldcp.c		vldcp needs-flag

device	mgiic: i2cbus
attach	mgiic at mainbus
file	arch/sparc64/dev/mgiic.c		mgiic


code:
/*	$OpenBSD: ld.script,v 1.7 2014/11/26 20:06:53 stsp Exp $	*/

/*
 * Copyright (c) 2013 Mark Kettenis <kettenis@openbsd.org>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

OUTPUT_FORMAT("elf64-sparc")
OUTPUT_ARCH(sparc:v9)
ENTRY(start)

PHDRS
{
	text PT_LOAD;
	data PT_LOAD;
	openbsd_randomize 0x65a3dbe6; /* PT_OPENBSD_RANDOMIZE */
	openbsd_bootdata  0x65a41be6; /* PT_OPENBSD_BOOTDATA */
}

SECTIONS
{
	. = 0x01000000;
	.text :
	{
		*(.text)
		PROVIDE(etext = .);
	} :text
	.rodata :
	{
		*(.rodata*)
	}
	. = 0x01800000;
	.data :
	{
		*(.data)
	} :data
	.openbsd.randomdata :
	{
		*(.openbsd.randomdata)
	} :data :openbsd_randomize
	.openbsd.bootdata :
	{
		*(.openbsd.bootdata)
	} :data :openbsd_bootdata
	.bss :
	{
		*(.bss)
		. = ALIGN(64 / 8);
	} :data
	. = ALIGN(64 / 8);
	PROVIDE (end = .);
}

license: unknown


code:
/*	$OpenBSD: auxio.c,v 1.7 2005/03/09 18:41:48 miod Exp $	*/
/*	$NetBSD: auxio.c,v 1.1 2000/04/15 03:08:13 mrg Exp $	*/

/*
 * Copyright (c) 2000 Matthew R. Green
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

/*
 * AUXIO registers support on the sbus & ebus2.
 */

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/errno.h>
#include <sys/device.h>
#include <sys/timeout.h>
#include <sys/kernel.h>

#include <machine/autoconf.h>
#include <machine/cpu.h>

#include <sparc64/dev/ebusreg.h>
#include <sparc64/dev/ebusvar.h>
#include <sparc64/dev/sbusvar.h>
#include <sparc64/dev/auxioreg.h>
#include <sparc64/dev/auxiovar.h>

#define	AUXIO_ROM_NAME		"auxio"

/*
 * ebus code.
 */
int	auxio_ebus_match(struct device *, void *, void *);
void	auxio_ebus_attach(struct device *, struct device *, void *);
int	auxio_sbus_match(struct device *, void *, void *);
void	auxio_sbus_attach(struct device *, struct device *, void *);
void	auxio_attach_common(struct auxio_softc *);

struct cfattach auxio_ebus_ca = {
	sizeof(struct auxio_softc), auxio_ebus_match, auxio_ebus_attach
};

struct cfattach auxio_sbus_ca = {
	sizeof(struct auxio_softc), auxio_sbus_match, auxio_sbus_attach
};

struct cfdriver auxio_cd = {
	NULL, "auxio", DV_DULL
};

void auxio_led_blink(void *, int);

int
auxio_ebus_match(parent, cf, aux)
	struct device *parent;
	void *cf;
	void *aux;
{
	struct ebus_attach_args *ea = aux;

	return (strcmp(AUXIO_ROM_NAME, ea->ea_name) == 0);
}

void
auxio_ebus_attach(parent, self, aux)
	struct device *parent, *self;
	void *aux;
{
	struct auxio_softc *sc = (struct auxio_softc *)self;
	struct ebus_attach_args *ea = aux;

	if (ea->ea_nregs < 1 || ea->ea_nvaddrs < 1) {
		printf(": no registers??\n");
		return;
	}

	sc->sc_tag = ea->ea_memtag;

	if (ea->ea_nregs != 5 || ea->ea_nvaddrs != 5) {
		printf(": not 5 (%d) registers, only setting led",
		    ea->ea_nregs);
		sc->sc_flags = AUXIO_LEDONLY|AUXIO_EBUS;
	} else {
		sc->sc_flags = AUXIO_EBUS;
		if (bus_space_map(sc->sc_tag, ea->ea_vaddrs[2],
		    sizeof(u_int32_t), BUS_SPACE_MAP_PROMADDRESS,
		    &sc->sc_freq)) {
			printf(": unable to map freq\n");
			return;
		}
		if (bus_space_map(sc->sc_tag, ea->ea_vaddrs[3],
		    sizeof(u_int32_t), BUS_SPACE_MAP_PROMADDRESS,
		    &sc->sc_scsi)) {
			printf(": unable to map SCSI\n");
			return;
		}
		if (bus_space_map(sc->sc_tag, ea->ea_vaddrs[4],
		    sizeof(u_int32_t), BUS_SPACE_MAP_PROMADDRESS,
		    &sc->sc_temp)) {
			printf(": unable to map temp\n");
			return;
		}
	}

	if (bus_space_map(sc->sc_tag, ea->ea_vaddrs[0], sizeof(u_int32_t),
	    BUS_SPACE_MAP_PROMADDRESS, &sc->sc_led)) {
		printf(": unable to map LED\n");
		return;
	}

	auxio_attach_common(sc);
}

int
auxio_sbus_match(parent, cf, aux)
	struct device *parent;
	void *cf;
	void *aux;
{
	struct sbus_attach_args *sa = aux;

	return (strcmp(AUXIO_ROM_NAME, sa->sa_name) == 0);
}

void
auxio_sbus_attach(parent, self, aux)
	struct device *parent, *self;
	void *aux;
{
	struct auxio_softc *sc = (struct auxio_softc *)self;
	struct sbus_attach_args *sa = aux;

	sc->sc_tag = sa->sa_bustag;

	if (sa->sa_nreg < 1 || sa->sa_npromvaddrs < 1) {
		printf(": no registers??\n");
		return;
	}

	if (sa->sa_nreg != 1 || sa->sa_npromvaddrs != 1) {
		printf(": not 1 (%d/%d) registers??", sa->sa_nreg, sa->sa_npromvaddrs);
		return;
	}

	/* sbus auxio only has one set of registers */
	sc->sc_flags = AUXIO_LEDONLY|AUXIO_SBUS;
	if (bus_space_map(sc->sc_tag, sa->sa_promvaddr, 1,
	    BUS_SPACE_MAP_PROMADDRESS, &sc->sc_led)) {
		printf(": couldn't map registers\n");
		return;
	}

	auxio_attach_common(sc);
}

void
auxio_attach_common(sc)
	struct auxio_softc *sc;
{
	sc->sc_blink.bl_func = auxio_led_blink;
	sc->sc_blink.bl_arg = sc;
	blink_led_register(&sc->sc_blink);
	printf("\n");
}

void
auxio_led_blink(void *vsc, int on)
{
	struct auxio_softc *sc = vsc;
	u_int32_t led;
	int s;

	s = splhigh();

	if (sc->sc_flags & AUXIO_EBUS)
		led = letoh32(bus_space_read_4(sc->sc_tag, sc->sc_led, 0));
	else
		led = bus_space_read_1(sc->sc_tag, sc->sc_led, 0);

	if (on)
		led |= AUXIO_LED_LED;
	else
		led &= ~AUXIO_LED_LED;

	if (sc->sc_flags & AUXIO_EBUS)
		bus_space_write_4(sc->sc_tag, sc->sc_led, 0, htole32(led));
	else
		bus_space_write_1(sc->sc_tag, sc->sc_led, 0, led);

	splx(s);
}

int
auxio_fd_control(u_int32_t bits)
{
	struct auxio_softc *sc;
	u_int32_t led;

	if (auxio_cd.cd_ndevs == 0) {
		return ENXIO;
	}

	/*
	 * XXX This does not handle > 1 auxio correctly.
	 * We'll assume the floppy drive is tied to first auxio found.
	 */
	sc = (struct auxio_softc *)auxio_cd.cd_devs[0];
	if (sc->sc_flags & AUXIO_EBUS)
		led = letoh32(bus_space_read_4(sc->sc_tag, sc->sc_led, 0));
	else
		led = bus_space_read_1(sc->sc_tag, sc->sc_led, 0);

	led = (led & ~AUXIO_LED_FLOPPY_MASK) | bits;

	if (sc->sc_flags & AUXIO_EBUS)
		bus_space_write_4(sc->sc_tag, sc->sc_led, 0, htole32(led));
	else
		bus_space_write_1(sc->sc_tag, sc->sc_led, 0, led);

	return 0;
}


code:
/*	$OpenBSD: auxioreg.h,v 1.4 2005/03/09 18:41:48 miod Exp $	*/
/*	$NetBSD: auxioreg.h,v 1.3 2000/04/15 03:08:13 mrg Exp $	*/

/*
 * Copyright (c) 2000 Matthew R. Green
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

/*
 * The AUXIO registers; their offset in the Ebus2 address space, plus the
 * bits for each register.  Note that the fdthree (FD), SUNW,CS4231 (AUDIO)
 * and power (POWER) devices on the Ebus2 have their AUXIO registers mapped
 * into their own "reg" properties, not the "auxio" device's "reg" properties.
 */
#define	AUXIO_FD			0x00720000
#define	AUXIO_FD_DENSENSE_INPUT		0x0
#define	AUXIO_FD_DENSENSE_OUTPUT	0x1

#define	AUXIO_AUDIO			0x00722000
#define	AUXIO_AUDIO_POWERDOWN		0x0

#define	AUXIO_POWER			0x00724000
#define	AUXIO_POWER_SYSTEM_OFF		0x0
#define	AUXIO_POWER_COURTESY_OFF	0x1

#define	AUXIO_LED			0x00726000
/* XXX: these may be useless on Ebus2 auxio! find out! */
#define	AUXIO_LED_FHD		0x20	/* floppy: high density (unreliable?)*/
#define	AUXIO_LED_LTE		0x08	/* link-test enable */
#define	AUXIO_LED_MMUX		0x04	/* Monitor/Mouse MUX; what is it? */
#define	AUXIO_LED_FTC		0x02	/* floppy: drives Terminal Count pin */
#define	AUXIO_LED_LED		0x01	/* front panel LED */
#define	AUXIO_LED_FLOPPY_MASK		(AUXIO_LED_FTC)

#define	AUXIO_PCI			0x00728000
#define	AUXIO_PCI_SLOT0			0x0	/* two bits each */
#define	AUXIO_PCI_SLOT1			0x2
#define	AUXIO_PCI_SLOT2			0x4
#define	AUXIO_PCI_SLOT3			0x6
#define	AUXIO_PCI_MODE			0x8

#define	AUXIO_FREQ			0x0072a000
#define	AUXIO_FREQ_FREQ0		0x0
#define	AUXIO_FREQ_FREQ1		0x1
#define	AUXIO_FREQ_FREQ2		0x2

#define	AUXIO_SCSI			0x0072c000
#define	AUXIO_SCSI_INT_OSC_EN		0x0
#define	AUXIO_SCSI_EXT_OSC_EN		0x1

#define	AUXIO_TEMP			0x0072f000
#define	AUXIO_TEMP_SELECT		0x0
#define	AUXIO_TEMP_CLOCK		0x1
#define	AUXIO_TEMP_ENABLE		0x2
#define	AUXIO_TEMP_DATAOUT		0x3
#define	AUXIO_TEMP_DATAINT		0x4


code:
/*	$OpenBSD: auxiovar.h,v 1.7 2005/03/09 18:41:48 miod Exp $	*/
/*	$NetBSD: auxiovar.h,v 1.4 2000/04/15 03:08:13 mrg Exp $	*/

/*
 * Copyright (c) 2000 Matthew R. Green
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

/*
 * on sun4u, auxio exists with one register (LED) on the sbus, and 5
 * registers on the ebus2 (pci) (LED, PCIMODE, FREQUENCY, SCSI
 * OSCILLATOR, and TEMP SENSE.
 *
 * clients of the auxio registers (eg, blinken lights, or the sbus
 * floppy) should search in auxio_cd for their matching auxio register
 * (to deal with multiple auxio's that may appear.)
 */

struct auxio_softc {
	struct device		sc_dev;
	bus_space_tag_t		sc_tag;
	bus_space_handle_t	sc_led;
	bus_space_handle_t	sc_pci;
	bus_space_handle_t	sc_scsi;
	bus_space_handle_t	sc_freq;
	bus_space_handle_t	sc_temp;
	int			sc_flags;
#define	AUXIO_LEDONLY		0x1
#define	AUXIO_EBUS		0x2
#define	AUXIO_SBUS		0x4
	struct blink_led	sc_blink;
};

#ifndef _LOCORE
int auxio_fd_control(u_int32_t);
#endif


code:
/*	$OpenBSD: bbc.c,v 1.3 2008/12/14 17:10:44 kettenis Exp $	*/

/*
 * Copyright (c) 2007 Mark Kettenis
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <sys/param.h>
#include <sys/kernel.h>
#include <sys/device.h>
#include <sys/malloc.h>
#include <sys/systm.h>

#include <machine/bus.h>
#include <machine/autoconf.h>

#ifdef DDB
#include <machine/db_machdep.h>
#endif

#include <sparc64/dev/ebusreg.h>
#include <sparc64/dev/ebusvar.h>

/* Agent ID */
#define BBC_AID			0x00000

/* Watchdog Action */
#define BBC_WATCHDOG_ACTION	0x00004

/* Perform system reset when watchdog timer expires. */
#define	BBC_WATCHDOG_RESET	0x01

/* Soft_XIR_GEN */
#define BBC_SOFT_XIR_GEN	0x00007

struct bbc_softc {
	struct device		sc_dv;
	bus_space_tag_t		sc_iot;
	bus_space_handle_t	sc_ioh;

	int			sc_aid;
};

int	bbc_match(struct device *, void *, void *);
void	bbc_attach(struct device *, struct device *, void *);

struct cfattach bbc_ca = {
	sizeof(struct bbc_softc), bbc_match, bbc_attach
};

struct cfdriver bbc_cd = {
	NULL, "bbc", DV_DULL
};

#ifdef DDB
void	bbc_xir(void *, int);
#endif

int
bbc_match(struct device *parent, void *cf, void *aux)
{
	struct ebus_attach_args *ea = aux;

	if (strcmp("bbc", ea->ea_name) == 0)
		return (1);
	return (0);
}

void
bbc_attach(struct device *parent, struct device *self, void *aux)
{
	struct bbc_softc *sc = (void *)self;
	struct ebus_attach_args *ea = aux;

	/* Use prom address if available, otherwise map it. */
	if (ea->ea_nvaddrs) {
		if (bus_space_map(ea->ea_memtag, ea->ea_vaddrs[0], 0,
		    BUS_SPACE_MAP_PROMADDRESS, &sc->sc_ioh)) {
			printf(": can't map PROM register space\n");
			return;
		}
		sc->sc_iot = ea->ea_memtag;
	} else if (ebus_bus_map(ea->ea_iotag, 0,
	    EBUS_PADDR_FROM_REG(&ea->ea_regs[0]),
	    ea->ea_regs[0].size, 0, 0, &sc->sc_ioh) == 0) {
		sc->sc_iot = ea->ea_iotag;
	} else if (ebus_bus_map(ea->ea_memtag, 0,
	    EBUS_PADDR_FROM_REG(&ea->ea_regs[0]),
	    ea->ea_regs[0].size, 0, 0, &sc->sc_ioh) == 0) {
		sc->sc_iot = ea->ea_memtag;
	} else {
		printf(": can't map register space\n");
		return;
	}

	sc->sc_aid = bus_space_read_1(sc->sc_iot, sc->sc_ioh, BBC_AID);
	printf(": AID 0x%02x\n", sc->sc_aid);

	/*
	 * Make sure we actually reset the system when the watchdog
	 * timer expires.
	 */
	bus_space_write_1(sc->sc_iot, sc->sc_ioh,
	    BBC_WATCHDOG_ACTION, BBC_WATCHDOG_RESET);

#ifdef DDB
	db_register_xir(bbc_xir, sc);
#endif
}

#ifdef DDB
void
bbc_xir(void *arg, int cpu)
{
	struct bbc_softc *sc = arg;

	/* Redirect a request to reset all processors to Processor 0. */
	if (cpu == -1)
		cpu = 0;

	/* Check whether we're handling the requested processor. */
	if ((cpu & ~0x7) != sc->sc_aid)
		return;

	bus_space_write_1(sc->sc_iot, sc->sc_ioh,
	    BBC_SOFT_XIR_GEN, 1 << (cpu & 0x7));
}
#endif


code:
/*	$OpenBSD: beep.c,v 1.5 2010/07/31 16:04:50 miod Exp $	*/

/*
 * Copyright (c) 2006 Jason L. Wright (jason@thought.net)
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

/*
 * Driver for beeper device on BBC machines (Blade 1k, 2k, etc)
 */

#include <sys/types.h>
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/device.h>
#include <sys/conf.h>
#include <sys/timeout.h>

#include <machine/bus.h>
#include <machine/autoconf.h>
#include <machine/openfirm.h>

#include <sparc64/dev/ebusreg.h>
#include <sparc64/dev/ebusvar.h>

#include "hidkbd.h"
#if NHIDKBD > 0
#include <dev/usb/hidkbdvar.h>
#endif

#define	BEEP_CTRL		0
#define	BEEP_CNT_0		2
#define	BEEP_CNT_1		3
#define	BEEP_CNT_2		4
#define	BEEP_CNT_3		5

#define	BEEP_CTRL_ON		0x01
#define	BEEP_CTRL_OFF		0x00

struct beep_freq {
	int freq;
	u_int32_t reg;
};

struct beep_softc {
	struct device		sc_dev;
	bus_space_tag_t		sc_iot;
	bus_space_handle_t	sc_ioh;
	int			sc_clk;
	struct beep_freq	sc_freqs[9];
	struct timeout		sc_to;
	int			sc_belltimeout;
	int			sc_bellactive;
};

int beep_match(struct device *, void *, void *);
void beep_attach(struct device *, struct device *, void *);
void beep_setfreq(struct beep_softc *, int);

struct cfattach beep_ca = {
	sizeof(struct beep_softc), beep_match, beep_attach
};

struct cfdriver beep_cd = {
	NULL, "beep", DV_DULL
};

#if NHIDKBD > 0
void beep_stop(void *);
void beep_bell(void *, u_int, u_int, u_int, int);
#endif

int
beep_match(struct device *parent, void *match, void *aux)
{
	struct ebus_attach_args *ea = aux;

	if (strcmp(ea->ea_name, "beep") == 0)
		return (1);
	return (0);
}

void
beep_attach(parent, self, aux)
	struct device *parent, *self;
	void *aux;
{
	struct beep_softc *sc = (void *)self;
	struct ebus_attach_args *ea = aux;
	int i;

	sc->sc_iot = ea->ea_memtag;

	/* Use prom address if available, otherwise map it. */
	if (ea->ea_nvaddrs) {
		if (bus_space_map(sc->sc_iot, ea->ea_vaddrs[0], 0,
		    BUS_SPACE_MAP_PROMADDRESS, &sc->sc_ioh)) {
			printf(": can't map PROM register space\n");
			return;
		}
	} else if (ebus_bus_map(sc->sc_iot, 0,
	    EBUS_PADDR_FROM_REG(&ea->ea_regs[0]), ea->ea_regs[0].size, 0, 0,
	    &sc->sc_ioh) != 0) {
		printf(": can't map register space\n");
                return;
	}

	/* The bbc,beep is clocked at half the BBC frequency */
	sc->sc_clk = getpropint(findroot(), "clock-frequency", 0);
	sc->sc_clk /= 2;

	/*
	 * Compute the frequence table based on the scalar and base
	 * board clock speed.
	 */
	for (i = 0; i < 9; i++) {
		sc->sc_freqs[i].reg = 1 << (18 - i);
		sc->sc_freqs[i].freq = sc->sc_clk / sc->sc_freqs[i].reg;
	}

	/* set beep at around 1200hz */
	beep_setfreq(sc, 1200);

#if 0
	bus_space_write_1(sc->sc_iot, sc->sc_ioh, BEEP_CTRL,
	    BEEP_CTRL_ON);
	for (i = 0; i < 1000; i++)
		DELAY(1000);
	bus_space_write_1(sc->sc_iot, sc->sc_ioh, BEEP_CTRL,
	    BEEP_CTRL_OFF);
#endif

	printf(": clock %sMHz\n", clockfreq(sc->sc_clk));

#if NHIDKBD > 0
	timeout_set(&sc->sc_to, beep_stop, sc);
	hidkbd_hookup_bell(beep_bell, sc);
#endif
}

void
beep_setfreq(struct beep_softc *sc, int freq)
{
	int i, n, selected = -1;

	n = sizeof(sc->sc_freqs)/sizeof(sc->sc_freqs[0]);

	if (freq < sc->sc_freqs[0].freq)
		selected = 0;
	if (freq > sc->sc_freqs[n - 1].freq)
		selected = n - 1;

	for (i = 1; selected == -1 && i < n; i++) {
		if (sc->sc_freqs[i].freq == freq)
			selected = i;
		else if (sc->sc_freqs[i].freq > freq) {
			int diff1, diff2;

			diff1 = freq - sc->sc_freqs[i - 1].freq;
			diff2 = sc->sc_freqs[i].freq - freq;
			if (diff1 < diff2)
				selected = i - 1;
			else
				selected = i;
		}
	}

	if (selected == -1)
		selected = 0;

	bus_space_write_1(sc->sc_iot, sc->sc_ioh, BEEP_CNT_0,
	    (sc->sc_freqs[i].reg >> 24) & 0xff);
	bus_space_write_1(sc->sc_iot, sc->sc_ioh, BEEP_CNT_1,
	    (sc->sc_freqs[i].reg >> 16) & 0xff);
	bus_space_write_1(sc->sc_iot, sc->sc_ioh, BEEP_CNT_2,
	    (sc->sc_freqs[i].reg >>  8) & 0xff);
	bus_space_write_1(sc->sc_iot, sc->sc_ioh, BEEP_CNT_3,
	    (sc->sc_freqs[i].reg >>  0) & 0xff);
}

#if NHIDKBD > 0
void
beep_stop(void *vsc)
{
	struct beep_softc *sc = vsc;
	int s;

	s = spltty();
	bus_space_write_1(sc->sc_iot, sc->sc_ioh, BEEP_CTRL,
	    BEEP_CTRL_OFF);
	sc->sc_bellactive = 0;
	sc->sc_belltimeout = 0;
	splx(s);
}

void
beep_bell(void *vsc, u_int pitch, u_int period, u_int volume, int poll)
{
	struct beep_softc *sc = vsc;
	int s, ticks;

	ticks = (period * hz) / 1000;
	if (ticks <= 0)
		ticks = 1;

	s = spltty();
	if (sc->sc_bellactive) {
		if (sc->sc_belltimeout == 0)
			timeout_del(&sc->sc_to);
	}
	if (pitch == 0 || period == 0 || volume == 0) {
		beep_stop(sc);
		splx(s);
		return;
	}
	if (!sc->sc_bellactive) {
		sc->sc_bellactive = 1;
		sc->sc_belltimeout = 1;
		bus_space_write_1(sc->sc_iot, sc->sc_ioh, BEEP_CTRL,
	    	    BEEP_CTRL_ON);
		timeout_add(&sc->sc_to, ticks);
	}
	splx(s);
}
#endif /* NHIDKBD > 0 */


code:
/*	$OpenBSD: beeper.c,v 1.10 2005/12/20 16:50:33 martin Exp $	*/

/*
 * Copyright (c) 2001 Jason L. Wright (jason@thought.net)
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 * Effort sponsored in part by the Defense Advanced Research Projects
 * Agency (DARPA) and Air Force Research Laboratory, Air Force
 * Materiel Command, USAF, under agreement number F30602-01-2-0537.
 *
 */

/*
 * Driver for beeper device on SUNW,Ultra-1-Engine.
 */

#include <sys/types.h>
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/device.h>
#include <sys/conf.h>
#include <sys/timeout.h>

#include <machine/bus.h>
#include <machine/autoconf.h>
#include <machine/openfirm.h>

#include <sparc64/dev/ebusreg.h>
#include <sparc64/dev/ebusvar.h>

#include "pckbd.h"
#if NPCKBD > 0
#include <dev/ic/pckbcvar.h>
#include <dev/pckbc/pckbdvar.h>
#endif

struct beeper_softc {
	struct device		sc_dev;
	bus_space_tag_t		sc_iot;
	bus_space_handle_t	sc_ioh;
	struct timeout		sc_to;
	int 			sc_belltimeout, sc_bellactive;
};

#define	BEEP_REG	0

int	beeper_match(struct device *, void *, void *);
void	beeper_attach(struct device *, struct device *, void *);

struct cfattach beeper_ca = {
	sizeof(struct beeper_softc), beeper_match, beeper_attach
};

struct cfdriver beeper_cd = {
	NULL, "beeper", DV_DULL
};

#if NPCKBD > 0
void beeper_stop(void *);
void beeper_bell(void *, u_int, u_int, u_int, int);
#endif

int
beeper_match(parent, match, aux)
	struct device *parent;
	void *match;
	void *aux;
{
	struct ebus_attach_args *ea = aux;

	if (strcmp(ea->ea_name, "beeper") == 0)
		return (1);
	return (0);
}

void
beeper_attach(parent, self, aux)
	struct device *parent, *self;
	void *aux;
{
	struct beeper_softc *sc = (void *)self;
	struct ebus_attach_args *ea = aux;

	sc->sc_iot = ea->ea_memtag;

	/* Use prom address if available, otherwise map it. */
	if (ea->ea_nvaddrs) {
		if (bus_space_map(sc->sc_iot, ea->ea_vaddrs[0], 0,
		    BUS_SPACE_MAP_PROMADDRESS, &sc->sc_ioh)) {
			printf(": can't map PROM register space\n");
			return;
		}
	} else if (ebus_bus_map(sc->sc_iot, 0,
	    EBUS_PADDR_FROM_REG(&ea->ea_regs[0]), ea->ea_regs[0].size, 0, 0,
	    &sc->sc_ioh) != 0) {
		printf(": can't map register space\n");
                return;
	}

#if NPCKBD > 0
	timeout_set(&sc->sc_to, beeper_stop, sc);
	pckbd_hookup_bell(beeper_bell, sc);
#endif
	printf("\n");
}

#if NPCKBD > 0
void
beeper_stop(vsc)
	void *vsc;
{
	struct beeper_softc *sc = vsc;
	int s;

	s = spltty();
	bus_space_write_4(sc->sc_iot, sc->sc_ioh, BEEP_REG, 0);
	sc->sc_bellactive = 0;
	sc->sc_belltimeout = 0;
	splx(s);
}

void
beeper_bell(vsc, pitch, period, volume, poll)
	void *vsc;
	u_int pitch, period, volume;
	int poll;
{
	struct beeper_softc *sc = vsc;
	int s, ticks;

	ticks = (period * hz) / 1000;
	if (ticks <= 0)
		ticks = 1;

	s = spltty();
	if (sc->sc_bellactive) {
		if (sc->sc_belltimeout == 0)
			timeout_del(&sc->sc_to);
	}
	if (pitch == 0 || period == 0) {
		beeper_stop(sc);
		splx(s);
		return;
	}
	if (!sc->sc_bellactive) {
		sc->sc_bellactive = 1;
		sc->sc_belltimeout = 1;
		bus_space_write_4(sc->sc_iot, sc->sc_ioh, BEEP_REG, 1);
		timeout_add(&sc->sc_to, ticks);
	}
	splx(s);
}
#endif /* NPCKBD > 0 */


code:
/*	$OpenBSD: cbus.c,v 1.14 2015/01/25 21:42:13 kettenis Exp $	*/
/*
 * Copyright (c) 2008 Mark Kettenis
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <sys/param.h>
#include <sys/device.h>
#include <sys/malloc.h>
#include <sys/systm.h>

#include <machine/autoconf.h>
#include <machine/hypervisor.h>
#include <machine/mdesc.h>
#include <machine/openfirm.h>

#include <sparc64/dev/cbusvar.h>
#include <sparc64/dev/vbusvar.h>

struct cbus_softc {
	struct device		sc_dv;
	bus_space_tag_t		sc_bustag;
	bus_dma_tag_t		sc_dmatag;

	uint64_t		sc_devhandle;

	/* Machine description. */
	int			sc_idx;
};

int	cbus_match(struct device *, void *, void *);
void	cbus_attach(struct device *, struct device *, void *);
int	cbus_print(void *, const char *);

struct cfattach cbus_ca = {
	sizeof(struct cbus_softc), cbus_match, cbus_attach
};

struct cfdriver cbus_cd = {
	NULL, "cbus", DV_DULL
};

void	*cbus_intr_establish(bus_space_tag_t, bus_space_tag_t, int, int, int,
    int (*)(void *), void *, const char *);
void	cbus_intr_ack(struct intrhand *);
bus_space_tag_t cbus_alloc_bus_tag(struct cbus_softc *, bus_space_tag_t);

int	cbus_get_channel_endpoint(struct cbus_softc *,
	    struct cbus_attach_args *);

int
cbus_match(struct device *parent, void *match, void *aux)
{
	struct vbus_attach_args *va = aux;

	if (strcmp(va->va_name, "channel-devices") == 0)
		return (1);

	return (0);
}

void
cbus_attach(struct device *parent, struct device *self, void *aux)
{
	struct cbus_softc *sc = (struct cbus_softc *)self;
	struct vbus_attach_args *va = aux;
	int node;
	int reg;

	sc->sc_bustag = cbus_alloc_bus_tag(sc, va->va_bustag);
	sc->sc_dmatag = va->va_dmatag;

	if (OF_getprop(va->va_node, "reg", &reg, sizeof(reg)) != sizeof(reg))
		return;
	sc->sc_devhandle = reg;

	printf("\n");

	sc->sc_idx = mdesc_find(va->va_name, va->va_reg[0]);
	if (sc->sc_idx == -1)
		return;

	for (node = OF_child(va->va_node); node; node = OF_peer(node)) {
		struct cbus_attach_args ca;
		char buf[32];

		bzero(&ca, sizeof(ca));
		ca.ca_node = node;
		if (OF_getprop(node, "name", buf, sizeof(buf)) <= 0)
			continue;
		ca.ca_name = buf;
		ca.ca_bustag = sc->sc_bustag;
		ca.ca_dmatag = sc->sc_dmatag;
		getprop(node, "reg", sizeof(*ca.ca_reg),
		    &ca.ca_nreg, (void **)&ca.ca_reg);
		if (cbus_get_channel_endpoint(sc, &ca) != 0)
			continue;

		config_found(self, &ca, cbus_print);
	}
}

int
cbus_print(void *aux, const char *name)
{
	struct cbus_attach_args *ca = aux;

	if (name)
		printf("\"%s\" at %s", ca->ca_name, name);
	if (ca->ca_id != -1)
		printf(" chan 0x%llx", ca->ca_id);
	return (UNCONF);
}

int
cbus_intr_setstate(bus_space_tag_t t, uint64_t devino, uint64_t state)
{
	struct cbus_softc *sc = t->cookie;
	uint64_t devhandle = sc->sc_devhandle;
	int err;

	err = hv_vintr_setstate(devhandle, devino, state);
	if (err != H_EOK)
		return (-1);

	return (0);
}

int
cbus_intr_setenabled(bus_space_tag_t t, uint64_t devino, uint64_t enabled)
{
	struct cbus_softc *sc = t->cookie;
	uint64_t devhandle = sc->sc_devhandle;
	int err;

	err = hv_vintr_setenabled(devhandle, devino, enabled);
	if (err != H_EOK)
		return (-1);

	return (0);
}

void *
cbus_intr_establish(bus_space_tag_t t, bus_space_tag_t t0, int ihandle,
    int level, int flags, int (*handler)(void *), void *arg, const char *what)
{
	struct cbus_softc *sc = t0->cookie;
	uint64_t devhandle = sc->sc_devhandle;
	uint64_t devino = ihandle;
	struct intrhand *ih;
	int err;

	ih = bus_intr_allocate(t0, handler, arg, ihandle, level,
	    NULL, NULL, what);
	if (ih == NULL)
		return (NULL);

	err = hv_vintr_setenabled(devhandle, devino, INTR_DISABLED);
	if (err != H_EOK) {
		printf("hv_vintr_setenabled: %d\n", err);
		return (NULL);
	}

	err = hv_vintr_setcookie(devhandle, devino, (vaddr_t)ih);
	if (err != H_EOK) {
		printf("hv_vintr_setcookie: %d\n", err);
		return (NULL);
	}

	if (flags & BUS_INTR_ESTABLISH_MPSAFE)
		ih->ih_mpsafe = 1;

	if (ih->ih_name)
		evcount_attach(&ih->ih_count, ih->ih_name, NULL);
	else
		evcount_attach(&ih->ih_count, "unknown", NULL);

	ih->ih_ack = cbus_intr_ack;

	err = hv_vintr_settarget(devhandle, devino, cpus->ci_upaid);
	if (err != H_EOK) {
		printf("hv_vintr_settarget: %d\n", err);
		return (NULL);
	}

	/* Clear pending interrupts. */
	err = hv_vintr_setstate(devhandle, devino, INTR_IDLE);
	if (err != H_EOK) {
		printf("hv_vintr_setstate: %d\n", err);
		return (NULL);
	}

	return (ih);
}

void
cbus_intr_ack(struct intrhand *ih)
{
	bus_space_tag_t t = ih->ih_bus;
	struct cbus_softc *sc = t->cookie;
	uint64_t devhandle = sc->sc_devhandle;
	uint64_t devino = ih->ih_number;

	hv_vintr_setstate(devhandle, devino, INTR_IDLE);
}

bus_space_tag_t
cbus_alloc_bus_tag(struct cbus_softc *sc, bus_space_tag_t parent)
{
	struct sparc_bus_space_tag *bt;

	bt = malloc(sizeof(*bt), M_DEVBUF, M_NOWAIT | M_ZERO);
	if (bt == NULL)
		panic("could not allocate cbus bus tag");

	strlcpy(bt->name, sc->sc_dv.dv_xname, sizeof(bt->name));
	bt->cookie = sc;
	bt->parent = parent;
	bt->asi = parent->asi;
	bt->sasi = parent->sasi;
	bt->sparc_bus_map = parent->sparc_bus_map;
	bt->sparc_intr_establish = cbus_intr_establish;

	return (bt);
}

int
cbus_get_channel_endpoint(struct cbus_softc *sc, struct cbus_attach_args *ca)
{
	struct md_header *hdr;
	struct md_element *elem;
	const char *name_blk;
	const char *str;
	int idx;
	int arc;

	idx = mdesc_find_child(sc->sc_idx, ca->ca_name, ca->ca_reg[0]);
	if (idx == -1)
		return (ENOENT);

	hdr = (struct md_header *)mdesc;
	elem = (struct md_element *)(mdesc + sizeof(struct md_header));
	name_blk = mdesc + sizeof(struct md_header) + hdr->node_blk_sz;

	ca->ca_idx = idx;

	ca->ca_id = -1;
	ca->ca_tx_ino = -1;
	ca->ca_rx_ino = -1;

	if (strcmp(ca->ca_name, "disk") != 0 &&
	    strcmp(ca->ca_name, "network") != 0)
		return (0);

	for (; elem[idx].tag != 'E'; idx++) {
		str = name_blk + elem[idx].name_offset;
		if (elem[idx].tag != 'a' || strcmp(str, "fwd") != 0)
			continue;

		arc = elem[idx].d.val;
		str = name_blk + elem[arc].name_offset;
		if (strcmp(str, "virtual-device-port") == 0) {
			idx = arc;
			continue;
		}

		if (strcmp(str, "channel-endpoint") == 0) {
			ca->ca_id = mdesc_get_prop_val(arc, "id");
			ca->ca_tx_ino = mdesc_get_prop_val(arc, "tx-ino");
			ca->ca_rx_ino = mdesc_get_prop_val(arc, "rx-ino");
			return (0);
		}
	}

	return (0);
}


code:
/*	$OpenBSD: cbusvar.h,v 1.5 2015/01/25 21:42:13 kettenis Exp $	*/
/*
 * Copyright (c) 2008 Mark Kettenis
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#ifndef _SPARC64_DEV_CBUSVAR_H_
#define _SPARC64_DEV_CBUSVAR_H_

struct cbus_attach_args {
	const char	*ca_name;
	int		ca_node;
	int		ca_idx;

	bus_space_tag_t	ca_bustag;
	bus_dma_tag_t	ca_dmatag;

	u_int32_t	*ca_reg;
	int		ca_nreg;

	u_int64_t	ca_id;
	u_int64_t	ca_tx_ino;
	u_int64_t	ca_rx_ino;
};

int	cbus_print(void *, const char *);
int	cbus_intr_setstate(bus_space_tag_t, uint64_t, uint64_t);
int	cbus_intr_setenabled(bus_space_tag_t, uint64_t, uint64_t);

#endif


code:
/*	$OpenBSD: ce4231.c,v 1.33 2015/05/11 06:52:35 ratchov Exp $	*/

/*
 * Copyright (c) 1999 Jason L. Wright (jason@thought.net)
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

/*
 * Driver for CS4231 based audio found in some sun4u systems (cs4231)
 * based on ideas from the S/Linux project and the NetBSD project.
 *
 * Effort sponsored in part by the Defense Advanced Research Projects
 * Agency (DARPA) and Air Force Research Laboratory, Air Force
 * Materiel Command, USAF, under agreement number F30602-01-2-0537.
 *
 */

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/errno.h>
#include <sys/ioctl.h>
#include <sys/device.h>
#include <sys/proc.h>
#include <sys/malloc.h>

#include <machine/cpu.h>
#include <machine/bus.h>
#include <machine/intr.h>
#include <machine/autoconf.h>

#include <sys/audioio.h>
#include <dev/audio_if.h>

#include <sparc64/dev/ebusreg.h>
#include <sparc64/dev/ebusvar.h>
#include <sparc64/dev/ce4231var.h>

/* AD1418 provides basic registers, CS4231 extends with more */
#include <dev/ic/ad1848reg.h>
#include <dev/ic/cs4231reg.h>

/* Mixer classes and mixer knobs */
#define CSAUDIO_INPUT_CLASS	0
#define CSAUDIO_OUTPUT_CLASS	1
#define CSAUDIO_RECORD_CLASS	2
#define CSAUDIO_DAC_LVL		3
#define CSAUDIO_DAC_MUTE	4
#define CSAUDIO_OUTPUTS		5
#define CSAUDIO_CD_LVL		6
#define CSAUDIO_CD_MUTE		7
#define CSAUDIO_LINE_IN_LVL	8
#define CSAUDIO_LINE_IN_MUTE	9
#define CSAUDIO_MONITOR_LVL	10
#define CSAUDIO_MONITOR_MUTE	11
#define CSAUDIO_REC_LVL		12
#define CSAUDIO_RECORD_SOURCE	13
#define CSAUDIO_MIC_PREAMP	14

/* Recording sources */
#define REC_PORT_LINE	0
#define REC_PORT_CD	1
#define REC_PORT_MIC	2
#define REC_PORT_MIX	3

/* Output ports. */
#define OUT_PORT_LINE	0x1
#define OUT_PORT_HP	0x2
#define OUT_PORT_SPKR	0x4

/* Bits on the ADC reg that determine recording source */
#define CS_REC_SRC_BITS 0xc0

#ifdef AUDIO_DEBUG
#define	DPRINTF(x)	printf x
#else
#define	DPRINTF(x)
#endif

#define	CS_TIMEOUT	90000

/* Read/write CS4231 direct registers */
#define CS_WRITE(sc,r,v)	\
    bus_space_write_1((sc)->sc_bustag, (sc)->sc_cshandle, (r) << 2, (v))
#define	CS_READ(sc,r)		\
    bus_space_read_1((sc)->sc_bustag, (sc)->sc_cshandle, (r) << 2)

/* Read/write EBDMA playback registers */
#define	P_WRITE(sc,r,v)		\
    bus_space_write_4((sc)->sc_bustag, (sc)->sc_pdmahandle, (r), (v))
#define	P_READ(sc,r)		\
    bus_space_read_4((sc)->sc_bustag, (sc)->sc_pdmahandle, (r))

/* Read/write EBDMA capture registers */
#define	C_WRITE(sc,r,v)		\
    bus_space_write_4((sc)->sc_bustag, (sc)->sc_cdmahandle, (r), (v))
#define	C_READ(sc,r)		\
    bus_space_read_4((sc)->sc_bustag, (sc)->sc_cdmahandle, (r))

int	ce4231_match(struct device *, void *, void *);
void	ce4231_attach(struct device *, struct device *, void *);
int	ce4231_cintr(void *);
int	ce4231_pintr(void *);

int	ce4231_set_speed(struct ce4231_softc *, u_long *);

void	ce4231_set_outputs(struct ce4231_softc *, int);
int	ce4231_get_outputs(struct ce4231_softc *);

void		ce4231_write(struct ce4231_softc *, u_int8_t, u_int8_t);
u_int8_t	ce4231_read(struct ce4231_softc *, u_int8_t);

/* Audio interface */
int	ce4231_open(void *, int);
void	ce4231_close(void *);
int	ce4231_query_encoding(void *, struct audio_encoding *);
int	ce4231_set_params(void *, int, int, struct audio_params *,
    struct audio_params *);
int	ce4231_round_blocksize(void *, int);
int	ce4231_commit_settings(void *);
int	ce4231_halt_output(void *);
int	ce4231_halt_input(void *);
int	ce4231_getdev(void *, struct audio_device *);
int	ce4231_set_port(void *, mixer_ctrl_t *);
int	ce4231_get_port(void *, mixer_ctrl_t *);
int	ce4231_query_devinfo(void *addr, mixer_devinfo_t *);
void *	ce4231_alloc(void *, int, size_t, int, int);
void	ce4231_free(void *, void *, int);
int	ce4231_get_props(void *);
int	ce4231_trigger_output(void *, void *, void *, int,
    void (*intr)(void *), void *arg, struct audio_params *);
int	ce4231_trigger_input(void *, void *, void *, int,
    void (*intr)(void *), void *arg, struct audio_params *);

struct audio_hw_if ce4231_sa_hw_if = {
	ce4231_open,
	ce4231_close,
	0,
	ce4231_query_encoding,
	ce4231_set_params,
	ce4231_round_blocksize,
	ce4231_commit_settings,
	0,
	0,
	0,
	0,
	ce4231_halt_output,
	ce4231_halt_input,
	0,
	ce4231_getdev,
	0,
	ce4231_set_port,
	ce4231_get_port,
	ce4231_query_devinfo,
	ce4231_alloc,
	ce4231_free,
	0,
	0,
	ce4231_get_props,
	ce4231_trigger_output,
	ce4231_trigger_input,
	0
};

struct cfattach audioce_ca = {
	sizeof (struct ce4231_softc), ce4231_match, ce4231_attach
};

struct cfdriver audioce_cd = {
	NULL, "audioce", DV_DULL
};

struct audio_device ce4231_device = {
	"SUNW,CS4231",
	"b",
	"onboard1",
};

int
ce4231_match(parent, vcf, aux)
	struct device *parent;
	void *vcf, *aux;
{
	struct ebus_attach_args *ea = aux;

	if (!strcmp("SUNW,CS4231", ea->ea_name) ||
	    !strcmp("audio", ea->ea_name))
		return (1);
	return (0);
}

void
ce4231_attach(parent, self, aux)
	struct device *parent, *self;
	void *aux;
{
	struct ebus_attach_args *ea = aux;
	struct ce4231_softc *sc = (struct ce4231_softc *)self;
	mixer_ctrl_t cp;
	int node;

	node = ea->ea_node;

	sc->sc_last_format = 0xffffffff;

	/* Pass on the bus tags */
	sc->sc_bustag = ea->ea_memtag;
	sc->sc_dmatag = ea->ea_dmatag;

	/* Make sure things are sane. */
	if (ea->ea_nintrs != 2) {
		printf(": expected 2 interrupts, got %d\n", ea->ea_nintrs);
		return;
	}
	if (ea->ea_nregs != 4) {
		printf(": expected 4 register set, got %d\n",
		    ea->ea_nregs);
		return;
	}

	sc->sc_cih = bus_intr_establish(sc->sc_bustag, ea->ea_intrs[0],
	    IPL_AUDIO, BUS_INTR_ESTABLISH_MPSAFE, ce4231_cintr,
	    sc, self->dv_xname);
	if (sc->sc_cih == NULL) {
		printf(": couldn't establish capture interrupt\n");
		return;
	}
	sc->sc_pih = bus_intr_establish(sc->sc_bustag, ea->ea_intrs[1],
	    IPL_AUDIO, BUS_INTR_ESTABLISH_MPSAFE, ce4231_pintr,
	    sc, self->dv_xname);
	if (sc->sc_pih == NULL) {
		printf(": couldn't establish play interrupt1\n");
		return;
	}

	/* XXX what if prom has already mapped?! */

	if (ebus_bus_map(sc->sc_bustag, 0,
	    EBUS_PADDR_FROM_REG(&ea->ea_regs[0]), ea->ea_regs[0].size,
	    BUS_SPACE_MAP_LINEAR, 0, &sc->sc_cshandle) != 0) {
		printf(": couldn't map cs4231 registers\n");
		return;
	}

	if (ebus_bus_map(sc->sc_bustag, 0,
	    EBUS_PADDR_FROM_REG(&ea->ea_regs[1]), ea->ea_regs[1].size,
	    BUS_SPACE_MAP_LINEAR, 0, &sc->sc_pdmahandle) != 0) {
		printf(": couldn't map dma1 registers\n");
		return;
	}

	if (ebus_bus_map(sc->sc_bustag, 0,
	    EBUS_PADDR_FROM_REG(&ea->ea_regs[2]), ea->ea_regs[2].size,
	    BUS_SPACE_MAP_LINEAR, 0, &sc->sc_cdmahandle) != 0) {
		printf(": couldn't map dma2 registers\n");
		return;
	}

	if (ebus_bus_map(sc->sc_bustag, 0,
	    EBUS_PADDR_FROM_REG(&ea->ea_regs[3]), ea->ea_regs[3].size,
	    BUS_SPACE_MAP_LINEAR, 0, &sc->sc_auxhandle) != 0) {
		printf(": couldn't map aux registers\n");
		return;
	}

	printf(": nvaddrs %d\n", ea->ea_nvaddrs);

	audio_attach_mi(&ce4231_sa_hw_if, sc, &sc->sc_dev);

	/* Enable mode 2. */
	ce4231_write(sc, SP_MISC_INFO, ce4231_read(sc, SP_MISC_INFO) | MODE2);

	/* Attenuate DAC, CD and line-in.  -22.5 dB for all. */
	cp.dev = CSAUDIO_DAC_LVL;
	cp.type = AUDIO_MIXER_VALUE;
	cp.un.value.num_channels = 2;
	cp.un.value.level[AUDIO_MIXER_LEVEL_LEFT] = 195;
	cp.un.value.level[AUDIO_MIXER_LEVEL_RIGHT] = 195;
	ce4231_set_port(sc, &cp);

	cp.dev = CSAUDIO_CD_LVL;
	cp.un.value.level[AUDIO_MIXER_LEVEL_LEFT] = 135;
	cp.un.value.level[AUDIO_MIXER_LEVEL_RIGHT] = 135;
	ce4231_set_port(sc, &cp);

	cp.dev = CSAUDIO_LINE_IN_LVL;
	ce4231_set_port(sc, &cp);

	/* Unmute DAC, CD and line-in */
	cp.dev = CSAUDIO_DAC_MUTE;
	cp.type = AUDIO_MIXER_ENUM;
	cp.un.ord = 0;
	ce4231_set_port(sc, &cp);

	cp.dev = CSAUDIO_CD_MUTE;
	ce4231_set_port(sc, &cp);

	cp.dev = CSAUDIO_LINE_IN_MUTE;
	ce4231_set_port(sc, &cp);

	/* XXX get real burst... */
	sc->sc_burst = EBDCSR_BURST_8;
}

/*
 * Write to one of the indexed registers of cs4231.
 */
void
ce4231_write(sc, r, v)
	struct ce4231_softc *sc;
	u_int8_t r, v;
{
	CS_WRITE(sc, AD1848_IADDR, r);
	CS_WRITE(sc, AD1848_IDATA, v);
}

/*
 * Read from one of the indexed registers of cs4231.
 */
u_int8_t
ce4231_read(sc, r)
	struct ce4231_softc *sc;
	u_int8_t r;
{
	CS_WRITE(sc, AD1848_IADDR, r);
	return (CS_READ(sc, AD1848_IDATA));
}

int
ce4231_set_speed(sc, argp)
	struct ce4231_softc *sc;
	u_long *argp;

{
	/*
	 * The available speeds are in the following table. Keep the speeds in
	 * the increasing order.
	 */
	typedef struct {
		int speed;
		u_char bits;
	} speed_struct;
	u_long arg = *argp;

	static speed_struct speed_table[] = {
		{5510,	(0 << 1) | CLOCK_XTAL2},
		{5510,	(0 << 1) | CLOCK_XTAL2},
		{6620,	(7 << 1) | CLOCK_XTAL2},
		{8000,	(0 << 1) | CLOCK_XTAL1},
		{9600,	(7 << 1) | CLOCK_XTAL1},
		{11025,	(1 << 1) | CLOCK_XTAL2},
		{16000,	(1 << 1) | CLOCK_XTAL1},
		{18900,	(2 << 1) | CLOCK_XTAL2},
		{22050,	(3 << 1) | CLOCK_XTAL2},
		{27420,	(2 << 1) | CLOCK_XTAL1},
		{32000,	(3 << 1) | CLOCK_XTAL1},
		{33075,	(6 << 1) | CLOCK_XTAL2},
		{33075,	(4 << 1) | CLOCK_XTAL2},
		{44100,	(5 << 1) | CLOCK_XTAL2},
		{48000,	(6 << 1) | CLOCK_XTAL1},
	};

	int i, n, selected = -1;

	n = sizeof(speed_table) / sizeof(speed_struct);

	if (arg < speed_table[0].speed)
		selected = 0;
	if (arg > speed_table[n - 1].speed)
		selected = n - 1;

	for (i = 1; selected == -1 && i < n; i++) {
		if (speed_table[i].speed == arg)
			selected = i;
		else if (speed_table[i].speed > arg) {
			int diff1, diff2;

			diff1 = arg - speed_table[i - 1].speed;
			diff2 = speed_table[i].speed - arg;
			if (diff1 < diff2)
				selected = i - 1;
			else
				selected = i;
		}
	}

	if (selected == -1)
		selected = 3;

	sc->sc_speed_bits = speed_table[selected].bits;
	sc->sc_need_commit = 1;
	*argp = speed_table[selected].speed;

	return (0);
}

/*
 * Audio interface functions
 */
int
ce4231_open(addr, flags)
	void *addr;
	int flags;
{
	struct ce4231_softc *sc = addr;
	int tries;

	DPRINTF(("ce4231_open\n"));

	if (sc->sc_open)
		return (EBUSY);

	sc->sc_open = 1;
	sc->sc_rintr = 0;
	sc->sc_rarg = 0;
	sc->sc_pintr = 0;
	sc->sc_parg = 0;

	P_WRITE(sc, EBDMA_DCSR, EBDCSR_RESET);
	C_WRITE(sc, EBDMA_DCSR, EBDCSR_RESET);
	P_WRITE(sc, EBDMA_DCSR, sc->sc_burst);
	C_WRITE(sc, EBDMA_DCSR, sc->sc_burst);

	DELAY(20);

	for (tries = CS_TIMEOUT;
	     tries && CS_READ(sc, AD1848_IADDR) == SP_IN_INIT; tries--)
		DELAY(10);
	if (tries == 0)
		printf("%s: timeout waiting for reset\n", sc->sc_dev.dv_xname);

	ce4231_write(sc, SP_PIN_CONTROL,
	    ce4231_read(sc, SP_PIN_CONTROL) | INTERRUPT_ENABLE);

	return (0);
}

void
ce4231_close(addr)
	void *addr;
{
	struct ce4231_softc *sc = addr;

	ce4231_halt_input(sc);
	ce4231_halt_output(sc);
	ce4231_write(sc, SP_PIN_CONTROL,
	    ce4231_read(sc, SP_PIN_CONTROL) & (~INTERRUPT_ENABLE));
	sc->sc_open = 0;
}

int
ce4231_query_encoding(addr, fp)
	void *addr;
	struct audio_encoding *fp;
{
	int err = 0;

	switch (fp->index) {
	case 0:
		strlcpy(fp->name, AudioEmulaw, sizeof(fp->name));
		fp->encoding = AUDIO_ENCODING_ULAW;
		fp->precision = 8;
		fp->flags = 0;
		break;
	case 1:
		strlcpy(fp->name, AudioEalaw, sizeof(fp->name));
		fp->encoding = AUDIO_ENCODING_ALAW;
		fp->precision = 8;
		fp->flags = 0;
		break;
	case 2:
		strlcpy(fp->name, AudioEslinear_le, sizeof(fp->name));
		fp->encoding = AUDIO_ENCODING_SLINEAR_LE;
		fp->precision = 16;
		fp->flags = 0;
		break;
	case 3:
		strlcpy(fp->name, AudioEulinear, sizeof(fp->name));
		fp->encoding = AUDIO_ENCODING_ULINEAR;
		fp->precision = 8;
		fp->flags = 0;
		break;
	case 4:
		strlcpy(fp->name, AudioEslinear_be, sizeof(fp->name));
		fp->encoding = AUDIO_ENCODING_SLINEAR_BE;
		fp->precision = 16;
		fp->flags = 0;
		break;
	default:
		err = EINVAL;
	}
	fp->bps = AUDIO_BPS(fp->precision);
	fp->msb = 1;
	return (err);
}

int
ce4231_set_params(addr, setmode, usemode, p, r)
	void *addr;
	int setmode, usemode;
	struct audio_params *p, *r;
{
	struct ce4231_softc *sc = (struct ce4231_softc *)addr;
	int err, bits, enc = p->encoding;

	if (p->precision > 16)
		p->precision = 16;
	switch (enc) {
	case AUDIO_ENCODING_ULAW:
		p->precision = 8;
		bits = FMT_ULAW >> 5;
		break;
	case AUDIO_ENCODING_ALAW:
		p->precision = 8;
		bits = FMT_ALAW >> 5;
		break;
	case AUDIO_ENCODING_SLINEAR_LE:
		p->precision = 16;
		bits = FMT_TWOS_COMP >> 5;
		break;
	case AUDIO_ENCODING_SLINEAR_BE:
		p->precision = 16;
		bits = FMT_TWOS_COMP_BE >> 5;
		break;
	case AUDIO_ENCODING_ULINEAR_LE:
	case AUDIO_ENCODING_ULINEAR_BE:
		p->precision = 8;
		break;
	default:
		return (EINVAL);
	}

	if (p->channels > 2)
		p->channels = 2;

	err = ce4231_set_speed(sc, &p->sample_rate);
	if (err)
		return (err);

	p->bps = AUDIO_BPS(p->precision);
	r->bps = AUDIO_BPS(r->precision);
	p->msb = r->msb = 1;

	sc->sc_format_bits = bits;
	sc->sc_channels = p->channels;
	sc->sc_precision = p->precision;
	sc->sc_need_commit = 1;
	return (0);
}

int
ce4231_round_blocksize(addr, blk)
	void *addr;
	int blk;
{
	return ((blk + 3) & (-4));
}

int
ce4231_commit_settings(addr)
	void *addr;
{
	struct ce4231_softc *sc = (struct ce4231_softc *)addr;
	int tries;
	u_int8_t r, fs;

	if (sc->sc_need_commit == 0)
		return (0);

	fs = sc->sc_speed_bits | (sc->sc_format_bits << 5);
	if (sc->sc_channels == 2)
		fs |= FMT_STEREO;

	if (sc->sc_last_format == fs) {
		sc->sc_need_commit = 0;
		return (0);
	}

	/* XXX: this code is called before DMA (this intrs) is stopped */
	mtx_enter(&audio_lock);

	r = ce4231_read(sc, SP_INTERFACE_CONFIG) | AUTO_CAL_ENABLE;
	CS_WRITE(sc, AD1848_IADDR, MODE_CHANGE_ENABLE);
	CS_WRITE(sc, AD1848_IADDR, MODE_CHANGE_ENABLE | SP_INTERFACE_CONFIG);
	CS_WRITE(sc, AD1848_IDATA, r);

	CS_WRITE(sc, AD1848_IADDR, MODE_CHANGE_ENABLE | SP_CLOCK_DATA_FORMAT);
	CS_WRITE(sc, AD1848_IDATA, fs);
	CS_READ(sc, AD1848_IDATA);
	CS_READ(sc, AD1848_IDATA);
	tries = CS_TIMEOUT;
	for (tries = CS_TIMEOUT;
	     tries && CS_READ(sc, AD1848_IADDR) == SP_IN_INIT; tries--)
		DELAY(10);
	if (tries == 0)
		printf("%s: timeout committing fspb\n", sc->sc_dev.dv_xname);

	CS_WRITE(sc, AD1848_IADDR, MODE_CHANGE_ENABLE | CS_REC_FORMAT);
	CS_WRITE(sc, AD1848_IDATA, fs);
	CS_READ(sc, AD1848_IDATA);
	CS_READ(sc, AD1848_IDATA);
	for (tries = CS_TIMEOUT;
	     tries && CS_READ(sc, AD1848_IADDR) == SP_IN_INIT; tries--)
		DELAY(10);
	if (tries == 0)
		printf("%s: timeout committing cdf\n", sc->sc_dev.dv_xname);

	CS_WRITE(sc, AD1848_IADDR, 0);
	for (tries = CS_TIMEOUT;
	     tries && CS_READ(sc, AD1848_IADDR) == SP_IN_INIT; tries--)
		DELAY(10);
	if (tries == 0)
		printf("%s: timeout waiting for !mce\n", sc->sc_dev.dv_xname);

	CS_WRITE(sc, AD1848_IADDR, SP_TEST_AND_INIT);
	for (tries = CS_TIMEOUT;
	     tries && CS_READ(sc, AD1848_IDATA) & AUTO_CAL_IN_PROG; tries--)
		DELAY(10);
	if (tries == 0)
		printf("%s: timeout waiting for autocalibration\n",
		    sc->sc_dev.dv_xname);

	mtx_leave(&audio_lock);

	sc->sc_need_commit = 0;
	return (0);
}

int
ce4231_halt_output(addr)
	void *addr;
{
	struct ce4231_softc *sc = (struct ce4231_softc *)addr;

	P_WRITE(sc, EBDMA_DCSR,
	    P_READ(sc, EBDMA_DCSR) & ~EBDCSR_DMAEN);
	ce4231_write(sc, SP_INTERFACE_CONFIG,
	    ce4231_read(sc, SP_INTERFACE_CONFIG) & (~PLAYBACK_ENABLE));
	return (0);
}

int
ce4231_halt_input(addr)
	void *addr;
{
	struct ce4231_softc *sc = (struct ce4231_softc *)addr;

	C_WRITE(sc, EBDMA_DCSR,
	    C_READ(sc, EBDMA_DCSR) & ~EBDCSR_DMAEN);
	ce4231_write(sc, SP_INTERFACE_CONFIG,
	    ce4231_read(sc, SP_INTERFACE_CONFIG) & (~CAPTURE_ENABLE));
	return (0);
}

int
ce4231_getdev(addr, retp)
	void *addr;
	struct audio_device *retp;
{
	*retp = ce4231_device;
	return (0);
}

void
ce4231_set_outputs(struct ce4231_softc *sc, int mask)
{
	u_int8_t val;

	val = ce4231_read(sc, CS_MONO_IO_CONTROL) & ~MONO_OUTPUT_MUTE;
	if (!(mask & OUT_PORT_SPKR))
		val |= MONO_OUTPUT_MUTE;
	ce4231_write(sc, CS_MONO_IO_CONTROL, val);

	val = ce4231_read(sc, SP_PIN_CONTROL) & ~(XCTL0_ENABLE | XCTL1_ENABLE);
	if (!(mask & OUT_PORT_LINE))
		val |= XCTL0_ENABLE;
	if (!(mask & OUT_PORT_HP))
		val |= XCTL1_ENABLE;
	ce4231_write(sc, SP_PIN_CONTROL, val);
}

int
ce4231_get_outputs(struct ce4231_softc *sc)
{
	int mask = 0;
	u_int8_t val;

	if (!(ce4231_read(sc, CS_MONO_IO_CONTROL) & MONO_OUTPUT_MUTE))
		mask |= OUT_PORT_SPKR;

	val = ce4231_read(sc, SP_PIN_CONTROL);
	if (!(val & XCTL0_ENABLE))
		mask |= OUT_PORT_LINE;
	if (!(val & XCTL1_ENABLE))
		mask |= OUT_PORT_HP;

	return (mask);
}

int
ce4231_set_port(void *addr, mixer_ctrl_t *cp)
{
	struct ce4231_softc *sc = (struct ce4231_softc *)addr;
	u_int8_t l, r;

	DPRINTF(("ce4231_set_port: dev=%d type=%d\n", cp->dev, cp->type));

	switch (cp->dev) {

	case CSAUDIO_DAC_LVL:
		if (cp->type != AUDIO_MIXER_VALUE)
			return (EINVAL);
		l = ce4231_read(sc, SP_LEFT_OUTPUT_CONTROL) &
		    OUTPUT_ATTEN_MASK;
		r = ce4231_read(sc, SP_RIGHT_OUTPUT_CONTROL) &
		    OUTPUT_ATTEN_MASK;
		l |= (AUDIO_MAX_GAIN -
		    cp->un.value.level[AUDIO_MIXER_LEVEL_LEFT]) >> 2;
		r |= (AUDIO_MAX_GAIN -
		    cp->un.value.level[AUDIO_MIXER_LEVEL_RIGHT]) >> 2;
		ce4231_write(sc, SP_LEFT_OUTPUT_CONTROL, l);
		ce4231_write(sc, SP_RIGHT_OUTPUT_CONTROL, r);
		break;
	case CSAUDIO_DAC_MUTE:
		if (cp->type != AUDIO_MIXER_ENUM)
			return (EINVAL);
		l = ce4231_read(sc, SP_LEFT_OUTPUT_CONTROL) & ~OUTPUT_MUTE;
		r = ce4231_read(sc, SP_RIGHT_OUTPUT_CONTROL) & ~OUTPUT_MUTE;
		if (cp->un.ord) {
			l |= OUTPUT_MUTE;
			r |= OUTPUT_MUTE;
		}
		ce4231_write(sc, SP_LEFT_OUTPUT_CONTROL, l);
		ce4231_write(sc, SP_RIGHT_OUTPUT_CONTROL, r);
		break;

	case CSAUDIO_OUTPUTS:
		if (cp->type != AUDIO_MIXER_SET)
			return (EINVAL);
		ce4231_set_outputs(sc, cp->un.mask);
		break;

	case CSAUDIO_CD_LVL:
		if (cp->type != AUDIO_MIXER_VALUE)
			return (EINVAL);
		l = ce4231_read(sc, SP_LEFT_AUX1_CONTROL) &
		    AUX_INPUT_ATTEN_MASK;
		r = ce4231_read(sc, SP_RIGHT_AUX1_CONTROL) &
		    AUX_INPUT_ATTEN_MASK;
		l |= (AUDIO_MAX_GAIN -
		    cp->un.value.level[AUDIO_MIXER_LEVEL_LEFT]) >> 3;
		r |= (AUDIO_MAX_GAIN -
		    cp->un.value.level[AUDIO_MIXER_LEVEL_RIGHT]) >> 3;
		ce4231_write(sc, SP_LEFT_AUX1_CONTROL, l);
		ce4231_write(sc, SP_RIGHT_AUX1_CONTROL, r);
		break;
	case CSAUDIO_CD_MUTE:
		if (cp->type != AUDIO_MIXER_ENUM)
			return (EINVAL);
		l = ce4231_read(sc, SP_LEFT_AUX1_CONTROL) & ~AUX_INPUT_MUTE;
		r = ce4231_read(sc, SP_RIGHT_AUX1_CONTROL) & ~AUX_INPUT_MUTE;
		if (cp->un.ord) {
			l |= AUX_INPUT_MUTE;
			r |= AUX_INPUT_MUTE;
		}
		ce4231_write(sc, SP_LEFT_AUX1_CONTROL, l);
		ce4231_write(sc, SP_RIGHT_AUX1_CONTROL, r);
		break;

	case CSAUDIO_LINE_IN_LVL:
		if (cp->type != AUDIO_MIXER_VALUE)
			return (EINVAL);
		l = ce4231_read(sc, CS_LEFT_LINE_CONTROL) &
		    LINE_INPUT_ATTEN_MASK;
		r = ce4231_read(sc, CS_RIGHT_LINE_CONTROL) &
		    LINE_INPUT_ATTEN_MASK;
		l |= (AUDIO_MAX_GAIN -
		    cp->un.value.level[AUDIO_MIXER_LEVEL_LEFT]) >> 3;
		r |= (AUDIO_MAX_GAIN -
		    cp->un.value.level[AUDIO_MIXER_LEVEL_RIGHT]) >> 3;
		ce4231_write(sc, CS_LEFT_LINE_CONTROL, l);
		ce4231_write(sc, CS_RIGHT_LINE_CONTROL, r);
		break;
	case CSAUDIO_LINE_IN_MUTE:
		l = ce4231_read(sc, CS_LEFT_LINE_CONTROL) & ~LINE_INPUT_MUTE;
		r = ce4231_read(sc, CS_RIGHT_LINE_CONTROL) & ~LINE_INPUT_MUTE;
		if (cp->un.ord) {
			l |= LINE_INPUT_MUTE;
			r |= LINE_INPUT_MUTE;
		}
		ce4231_write(sc, CS_LEFT_LINE_CONTROL, l);
		ce4231_write(sc, CS_RIGHT_LINE_CONTROL, r);
		break;

	case CSAUDIO_MONITOR_LVL:
		if (cp->type != AUDIO_MIXER_VALUE)
			return (EINVAL);
		if (cp->un.value.num_channels != 1)
			return (EINVAL);
		l = ce4231_read(sc, SP_DIGITAL_MIX) & ~MIX_ATTEN_MASK;
		l |= (AUDIO_MAX_GAIN -
		    cp->un.value.level[AUDIO_MIXER_LEVEL_MONO]) &
		    MIX_ATTEN_MASK;
		ce4231_write(sc, SP_DIGITAL_MIX, l);
		break;
	case CSAUDIO_MONITOR_MUTE:
		if (cp->type != AUDIO_MIXER_ENUM)
			return (EINVAL);
		l = ce4231_read(sc, SP_DIGITAL_MIX) & ~DIGITAL_MIX1_ENABLE;
		if (!cp->un.ord)
			l |= DIGITAL_MIX1_ENABLE;
		ce4231_write(sc, SP_DIGITAL_MIX, l);
		break;

	case CSAUDIO_REC_LVL:
		if (cp->type != AUDIO_MIXER_VALUE)
			return (EINVAL);
		l = ce4231_read(sc, SP_LEFT_INPUT_CONTROL) & INPUT_GAIN_MASK;
		r = ce4231_read(sc, SP_RIGHT_INPUT_CONTROL) & INPUT_GAIN_MASK;
		l = cp->un.value.level[AUDIO_MIXER_LEVEL_LEFT] >> 4;
		r = cp->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] >> 4;
		ce4231_write(sc, SP_LEFT_INPUT_CONTROL, l);
		ce4231_write(sc, SP_RIGHT_INPUT_CONTROL, r);
		break;
	case CSAUDIO_RECORD_SOURCE:
		if (cp->type != AUDIO_MIXER_ENUM)
			return (EINVAL);
		l = ce4231_read(sc, SP_LEFT_INPUT_CONTROL) & INPUT_SOURCE_MASK;
		r = ce4231_read(sc, SP_RIGHT_INPUT_CONTROL) & INPUT_SOURCE_MASK;
		l |= cp->un.ord << 6;
		r |= cp->un.ord << 6;
		ce4231_write(sc, SP_LEFT_INPUT_CONTROL, l);
		ce4231_write(sc, SP_RIGHT_INPUT_CONTROL, r);
		break;

	case CSAUDIO_MIC_PREAMP:
		if (cp->type != AUDIO_MIXER_ENUM)
			return (EINVAL);
		l = ce4231_read(sc, SP_LEFT_INPUT_CONTROL) &
		    ~INPUT_MIC_GAIN_ENABLE;
		r = ce4231_read(sc, SP_RIGHT_INPUT_CONTROL) &
		    ~INPUT_MIC_GAIN_ENABLE;
		if (cp->un.ord) {
			l |= INPUT_MIC_GAIN_ENABLE;
			r |= INPUT_MIC_GAIN_ENABLE;
		}
		ce4231_write(sc, SP_LEFT_INPUT_CONTROL, l);
		ce4231_write(sc, SP_RIGHT_INPUT_CONTROL, r);
		break;

	default:
		return (EINVAL);
	}

	return (0);
}

int
ce4231_get_port(void *addr, mixer_ctrl_t *cp)
{
	struct ce4231_softc *sc = (struct ce4231_softc *)addr;

	DPRINTF(("ce4231_get_port: port=%d type=%d\n", cp->dev, cp->type));

	switch (cp->dev) {

	case CSAUDIO_DAC_LVL:
		if (cp->type != AUDIO_MIXER_VALUE)
			return (EINVAL);
		cp->un.value.level[AUDIO_MIXER_LEVEL_LEFT] =
		    AUDIO_MAX_GAIN - ((ce4231_read(sc, SP_LEFT_OUTPUT_CONTROL) &
		    OUTPUT_ATTEN_BITS) << 2);
		cp->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] =
		    AUDIO_MAX_GAIN - ((ce4231_read(sc, SP_RIGHT_OUTPUT_CONTROL) &
		    OUTPUT_ATTEN_BITS) << 2);
		break;
	case CSAUDIO_DAC_MUTE:
		if (cp->type != AUDIO_MIXER_ENUM)
			return (EINVAL);
		cp->un.ord = (ce4231_read(sc, SP_LEFT_OUTPUT_CONTROL) &
		    OUTPUT_MUTE) ? 1 : 0;
		break;

	case CSAUDIO_OUTPUTS:
		if (cp->type != AUDIO_MIXER_SET)
			return (EINVAL);
		cp->un.mask = ce4231_get_outputs(sc);
		break;

	case CSAUDIO_CD_LVL:
		if (cp->type != AUDIO_MIXER_VALUE)
			return (EINVAL);
		cp->un.value.level[AUDIO_MIXER_LEVEL_LEFT] =
		    AUDIO_MAX_GAIN - ((ce4231_read(sc, SP_LEFT_AUX1_CONTROL) &
		    AUX_INPUT_ATTEN_BITS) << 3);
		cp->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] =
		    AUDIO_MAX_GAIN - ((ce4231_read(sc, SP_RIGHT_AUX1_CONTROL) &
		    AUX_INPUT_ATTEN_BITS) << 3);
		break;
	case CSAUDIO_CD_MUTE:
		if (cp->type != AUDIO_MIXER_ENUM)
			return (EINVAL);
		cp->un.ord = (ce4231_read(sc, SP_LEFT_AUX1_CONTROL) &
		    AUX_INPUT_MUTE) ? 1 : 0;
		break;

	case CSAUDIO_LINE_IN_LVL:
		if (cp->type != AUDIO_MIXER_VALUE)
			return (EINVAL);
		cp->un.value.level[AUDIO_MIXER_LEVEL_LEFT] =
		    AUDIO_MAX_GAIN - ((ce4231_read(sc, CS_LEFT_LINE_CONTROL) &
		    LINE_INPUT_ATTEN_BITS) << 3);
		cp->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] =
		    AUDIO_MAX_GAIN - ((ce4231_read(sc, CS_RIGHT_LINE_CONTROL) &
		    LINE_INPUT_ATTEN_BITS) << 3);
		break;
	case CSAUDIO_LINE_IN_MUTE:
		if (cp->type != AUDIO_MIXER_ENUM)
			return (EINVAL);
		cp->un.ord = (ce4231_read(sc, CS_LEFT_LINE_CONTROL) &
		    LINE_INPUT_MUTE) ? 1 : 0;
		break;

	case CSAUDIO_MONITOR_LVL:
		if (cp->type != AUDIO_MIXER_VALUE)
			return (EINVAL);
		if (cp->un.value.num_channels != 1)
			return (EINVAL);
		cp->un.value.level[AUDIO_MIXER_LEVEL_MONO] =
		    AUDIO_MAX_GAIN - (ce4231_read(sc, SP_DIGITAL_MIX) &
		    MIX_ATTEN_MASK);
		break;
	case CSAUDIO_MONITOR_MUTE:
		if (cp->type != AUDIO_MIXER_ENUM)
			return (EINVAL);
		cp->un.ord = (ce4231_read(sc, SP_DIGITAL_MIX) &
		    DIGITAL_MIX1_ENABLE) ? 0 : 1;
		break;

	case CSAUDIO_REC_LVL:
		if (cp->type != AUDIO_MIXER_VALUE)
			return (EINVAL);
		cp->un.value.level[AUDIO_MIXER_LEVEL_LEFT] =
		    (ce4231_read(sc, SP_LEFT_INPUT_CONTROL) &
		    ~INPUT_GAIN_MASK) << 4;
		cp->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] =
		    (ce4231_read(sc, SP_RIGHT_INPUT_CONTROL) &
		    ~INPUT_GAIN_MASK) << 4;
		break;
	case CSAUDIO_RECORD_SOURCE:
		if (cp->type != AUDIO_MIXER_ENUM)
			return (EINVAL);
		cp->un.ord = (ce4231_read(sc, SP_LEFT_INPUT_CONTROL) &
		    CS_REC_SRC_BITS) >> 6;
		break;

	case CSAUDIO_MIC_PREAMP:
		if (cp->type != AUDIO_MIXER_ENUM)
			return (EINVAL);
		cp->un.ord = (ce4231_read(sc, SP_LEFT_INPUT_CONTROL) &
		    INPUT_MIC_GAIN_ENABLE) ? 1 : 0;
		break;

	default:
		return (EINVAL);
	}
	return (0);
}

int
ce4231_query_devinfo(void *addr, mixer_devinfo_t *dip)
{
	size_t nsize = MAX_AUDIO_DEV_LEN;
	int err = 0;

	switch (dip->index) {
	case CSAUDIO_INPUT_CLASS:
		dip->type = AUDIO_MIXER_CLASS;
		dip->mixer_class = CSAUDIO_INPUT_CLASS;
		dip->prev = dip->next = AUDIO_MIXER_LAST;
		strlcpy(dip->label.name, AudioCinputs, nsize);
		break;
	case CSAUDIO_OUTPUT_CLASS:
		dip->type = AUDIO_MIXER_CLASS;
		dip->mixer_class = CSAUDIO_OUTPUT_CLASS;
		dip->prev = dip->next = AUDIO_MIXER_LAST;
		strlcpy(dip->label.name, AudioCoutputs, nsize);
		break;
	case CSAUDIO_RECORD_CLASS:
		dip->type = AUDIO_MIXER_CLASS;
		dip->mixer_class = CSAUDIO_RECORD_CLASS;
		dip->prev = dip->next = AUDIO_MIXER_LAST;
		strlcpy(dip->label.name, AudioCrecord, nsize);
		break;

	case CSAUDIO_DAC_LVL:
		dip->type = AUDIO_MIXER_VALUE;
		dip->mixer_class = CSAUDIO_OUTPUT_CLASS;
		dip->prev = AUDIO_MIXER_LAST;
		dip->next = CSAUDIO_DAC_MUTE;
		strlcpy(dip->label.name, AudioNdac, nsize);
		dip->un.v.num_channels = 2;
		dip->un.v.delta = 4;
		strlcpy(dip->un.v.units.name, AudioNvolume, nsize);
		break;
	case CSAUDIO_DAC_MUTE:
		dip->type = AUDIO_MIXER_ENUM;
		dip->mixer_class = CSAUDIO_OUTPUT_CLASS;
		dip->prev = CSAUDIO_DAC_LVL;
		dip->next = AUDIO_MIXER_LAST;
		strlcpy(dip->label.name, AudioNmute, nsize);
		goto onoff;

	case CSAUDIO_OUTPUTS:
		dip->type = AUDIO_MIXER_SET;
		dip->mixer_class = CSAUDIO_OUTPUT_CLASS;
		dip->prev = dip->next = AUDIO_MIXER_LAST;
		strlcpy(dip->label.name, AudioNoutput, nsize);
		dip->un.s.num_mem = 3;
		strlcpy(dip->un.s.member[0].label.name, AudioNline, nsize);
		dip->un.s.member[0].mask = OUT_PORT_LINE;
		strlcpy(dip->un.s.member[1].label.name, AudioNheadphone, nsize);
		dip->un.s.member[1].mask = OUT_PORT_HP;
		strlcpy(dip->un.s.member[2].label.name, AudioNspeaker, nsize);
		dip->un.s.member[2].mask = OUT_PORT_SPKR;
		break;

	case CSAUDIO_CD_LVL:
		dip->type = AUDIO_MIXER_VALUE;
		dip->mixer_class = CSAUDIO_INPUT_CLASS;
		dip->prev = AUDIO_MIXER_LAST;
		dip->next = CSAUDIO_CD_MUTE;
		strlcpy(dip->label.name, AudioNcd, nsize);
		dip->un.v.num_channels = 2;
		dip->un.v.delta = 8;
		strlcpy(dip->un.v.units.name, AudioNvolume, nsize);
		break;
	case CSAUDIO_CD_MUTE:
		dip->type = AUDIO_MIXER_ENUM;
		dip->mixer_class = CSAUDIO_INPUT_CLASS;
		dip->prev = CSAUDIO_CD_LVL;
		dip->next = AUDIO_MIXER_LAST;
		strlcpy(dip->label.name, AudioNmute, nsize);
		goto onoff;

	case CSAUDIO_LINE_IN_LVL:
		dip->type = AUDIO_MIXER_VALUE;
		dip->mixer_class = CSAUDIO_INPUT_CLASS;
		dip->prev = AUDIO_MIXER_LAST;
		dip->next = CSAUDIO_LINE_IN_MUTE;
		strlcpy(dip->label.name, AudioNline, nsize);
		dip->un.v.num_channels = 2;
		dip->un.v.delta = 8;
		strlcpy(dip->un.v.units.name, AudioNvolume, nsize);
		break;
	case CSAUDIO_LINE_IN_MUTE:
		dip->type = AUDIO_MIXER_ENUM;
		dip->mixer_class = CSAUDIO_INPUT_CLASS;
		dip->prev = CSAUDIO_LINE_IN_LVL;
		dip->next = AUDIO_MIXER_LAST;
		strlcpy(dip->label.name, AudioNmute, nsize);
		goto onoff;

	case CSAUDIO_MONITOR_LVL:
		dip->type = AUDIO_MIXER_VALUE;
		dip->mixer_class = CSAUDIO_OUTPUT_CLASS;
		dip->prev = AUDIO_MIXER_LAST;
		dip->next = CSAUDIO_MONITOR_MUTE;
		strlcpy(dip->label.name, AudioNmonitor, nsize);
		dip->un.v.num_channels = 1;
		dip->un.v.delta = 4;
		strlcpy(dip->un.v.units.name, AudioNvolume, nsize);
		break;
	case CSAUDIO_MONITOR_MUTE:
		dip->type = AUDIO_MIXER_ENUM;
		dip->mixer_class = CSAUDIO_OUTPUT_CLASS;
		dip->prev = CSAUDIO_MONITOR_LVL;
		dip->next = AUDIO_MIXER_LAST;
		strlcpy(dip->label.name, AudioNmute, nsize);
		goto onoff;

	case CSAUDIO_REC_LVL:
		dip->type = AUDIO_MIXER_VALUE;
		dip->mixer_class = CSAUDIO_RECORD_CLASS;
		dip->prev = dip->next = AUDIO_MIXER_LAST;
		strlcpy(dip->label.name, AudioNvolume, nsize);
		dip->un.v.num_channels = 2;
		dip->un.v.delta = 16;
		strlcpy(dip->un.v.units.name, AudioNvolume, nsize);
		break;
	case CSAUDIO_RECORD_SOURCE:
		dip->type = AUDIO_MIXER_ENUM;
		dip->mixer_class = CSAUDIO_RECORD_CLASS;
		dip->prev = dip->next = AUDIO_MIXER_LAST;
		strlcpy(dip->label.name, AudioNsource, nsize);
		dip->un.e.num_mem = 4;
		strlcpy(dip->un.e.member[0].label.name, AudioNline, nsize);
		dip->un.e.member[0].ord = REC_PORT_LINE;
		strlcpy(dip->un.e.member[1].label.name, AudioNcd, nsize);
		dip->un.e.member[1].ord = REC_PORT_CD;
		strlcpy(dip->un.e.member[2].label.name, AudioNmicrophone, nsize);
		dip->un.e.member[2].ord = REC_PORT_MIC;
		strlcpy(dip->un.e.member[3].label.name, AudioNmixerout, nsize);
		dip->un.e.member[3].ord = REC_PORT_MIX;
		break;

	case CSAUDIO_MIC_PREAMP:
		dip->type = AUDIO_MIXER_ENUM;
		dip->mixer_class = CSAUDIO_RECORD_CLASS;
		dip->prev = dip->next = AUDIO_MIXER_LAST;
		snprintf(dip->label.name, nsize, "%s_%s", AudioNmicrophone,
		   AudioNpreamp);
		goto onoff;

onoff:
		dip->un.e.num_mem = 2;
		strlcpy(dip->un.e.member[0].label.name, AudioNon, nsize);
		dip->un.e.member[0].ord = 1;
		strlcpy(dip->un.e.member[1].label.name, AudioNoff, nsize);
		dip->un.e.member[1].ord = 0;
		break;

	default:
		err = ENXIO;
	}

	return (err);
}

int
ce4231_get_props(addr)
	void *addr;
{
	return (AUDIO_PROP_FULLDUPLEX);
}

/*
 * Hardware interrupt handler
 */
/*
 * Don't bother with the AD1848_STATUS register.  It's interrupt bit gets
 * set for both recording and playback interrupts.  But we have separate
 * handlers for playback and recording, and if we clear the status in
 * one handler while there is an interrupt pending for the other direction
 * as well, we'll never notice the interrupt for the other direction.
 *
 * Instead rely solely on CS_IRQ_STATUS, which has separate bits for
 * playback and recording interrupts.  Also note that resetting
 * AD1848_STATUS clears the interrupt bits in CS_IRQ_STATUS.
 */

int
ce4231_pintr(v)
	void *v;
{
	struct ce4231_softc *sc = (struct ce4231_softc *)v;
	u_int32_t csr;
	u_int8_t reg;
	struct cs_dma *p;
	struct cs_chdma *chdma = &sc->sc_pchdma;
	int r = 0;

	mtx_enter(&audio_lock);
	csr = P_READ(sc, EBDMA_DCSR);

	reg = ce4231_read(sc, CS_IRQ_STATUS);
	if (reg & CS_IRQ_PI) {
		ce4231_write(sc, SP_LOWER_BASE_COUNT, 0xff);
		ce4231_write(sc, SP_UPPER_BASE_COUNT, 0xff);
		ce4231_write(sc, CS_IRQ_STATUS, reg & ~CS_IRQ_PI);
	}

	P_WRITE(sc, EBDMA_DCSR, csr);

	if (csr & EBDCSR_INT)
		r = 1;

	if ((csr & EBDCSR_TC) || ((csr & EBDCSR_A_LOADED) == 0)) {
		u_long nextaddr, togo;

		p = chdma->cur_dma;
		togo = chdma->segsz - chdma->count;
		if (togo == 0) {
			nextaddr = (u_int32_t)p->dmamap->dm_segs[0].ds_addr;
			chdma->count = togo = chdma->blksz;
		} else {
			nextaddr = chdma->lastaddr;
			if (togo > chdma->blksz)
				togo = chdma->blksz;
			chdma->count += togo;
		}

		P_WRITE(sc, EBDMA_DCNT, togo);
		P_WRITE(sc, EBDMA_DADDR, nextaddr);
		chdma->lastaddr = nextaddr + togo;

		if (sc->sc_pintr != NULL)
			(*sc->sc_pintr)(sc->sc_parg);
		r = 1;
	}
	mtx_leave(&audio_lock);
	return (r);
}

int
ce4231_cintr(v)
	void *v;
{
	struct ce4231_softc *sc = (struct ce4231_softc *)v;
	u_int32_t csr;
	u_int8_t reg;
	struct cs_dma *p;
	struct cs_chdma *chdma = &sc->sc_rchdma;
	int r = 0;

	mtx_enter(&audio_lock);
	csr = C_READ(sc, EBDMA_DCSR);

	reg = ce4231_read(sc, CS_IRQ_STATUS);
	if (reg & CS_IRQ_CI) {
		ce4231_write(sc, CS_LOWER_REC_CNT, 0xff);
		ce4231_write(sc, CS_UPPER_REC_CNT, 0xff);
		ce4231_write(sc, CS_IRQ_STATUS, reg & ~CS_IRQ_CI);
	}

	C_WRITE(sc, EBDMA_DCSR, csr);

	if (csr & EBDCSR_INT)
		r = 1;

	if ((csr & EBDCSR_TC) || ((csr & EBDCSR_A_LOADED) == 0)) {
		u_long nextaddr, togo;

		p = chdma->cur_dma;
		togo = chdma->segsz - chdma->count;
		if (togo == 0) {
			nextaddr = (u_int32_t)p->dmamap->dm_segs[0].ds_addr;
			chdma->count = togo = chdma->blksz;
		} else {
			nextaddr = chdma->lastaddr;
			if (togo > chdma->blksz)
				togo = chdma->blksz;
			chdma->count += togo;
		}

		C_WRITE(sc, EBDMA_DCNT, togo);
		C_WRITE(sc, EBDMA_DADDR, nextaddr);
		chdma->lastaddr = nextaddr + togo;

		if (sc->sc_rintr != NULL)
			(*sc->sc_rintr)(sc->sc_rarg);
		r = 1;
	}
	mtx_leave(&audio_lock);
	return (r);
}

void *
ce4231_alloc(addr, direction, size, pool, flags)
	void *addr;
	int direction;
	size_t size;
	int pool;
	int flags;
{
	struct ce4231_softc *sc = (struct ce4231_softc *)addr;
	bus_dma_tag_t dmat = sc->sc_dmatag;
	struct cs_dma *p;

	p = (struct cs_dma *)malloc(sizeof(struct cs_dma), pool, flags);
	if (p == NULL)
		return (NULL);

	if (bus_dmamap_create(dmat, size, 1, size, 0,
	    BUS_DMA_NOWAIT, &p->dmamap) != 0)
		goto fail;

	p->size = size;

	if (bus_dmamem_alloc(dmat, size, 64*1024, 0, p->segs,
	    sizeof(p->segs)/sizeof(p->segs[0]), &p->nsegs,
	    BUS_DMA_NOWAIT) != 0)
		goto fail1;

	if (bus_dmamem_map(dmat, p->segs, p->nsegs, p->size,
	    &p->addr, BUS_DMA_NOWAIT | BUS_DMA_COHERENT) != 0)
		goto fail2;

	if (bus_dmamap_load(dmat, p->dmamap, p->addr, size, NULL,
	    BUS_DMA_NOWAIT) != 0)
		goto fail3;

	p->next = sc->sc_dmas;
	sc->sc_dmas = p;
	return (p->addr);

fail3:
	bus_dmamem_unmap(dmat, p->addr, p->size);
fail2:
	bus_dmamem_free(dmat, p->segs, p->nsegs);
fail1:
	bus_dmamap_destroy(dmat, p->dmamap);
fail:
	free(p, pool, 0);
	return (NULL);
}

void
ce4231_free(addr, ptr, pool)
	void *addr;
	void *ptr;
	int pool;
{
	struct ce4231_softc *sc = addr;
	bus_dma_tag_t dmat = sc->sc_dmatag;
	struct cs_dma *p, **pp;

	for (pp = &sc->sc_dmas; (p = *pp) != NULL; pp = &(*pp)->next) {
		if (p->addr != ptr)
			continue;
		bus_dmamap_unload(dmat, p->dmamap);
		bus_dmamem_unmap(dmat, p->addr, p->size);
		bus_dmamem_free(dmat, p->segs, p->nsegs);
		bus_dmamap_destroy(dmat, p->dmamap);
		*pp = p->next;
		free(p, pool, 0);
		return;
	}
	printf("%s: attempt to free rogue pointer\n", sc->sc_dev.dv_xname);
}

int
ce4231_trigger_output(addr, start, end, blksize, intr, arg, param)
	void *addr, *start, *end;
	int blksize;
	void (*intr)(void *);
	void *arg;
	struct audio_params *param;
{
	struct ce4231_softc *sc = addr;
	struct cs_dma *p;
	struct cs_chdma *chdma = &sc->sc_pchdma;
	u_int32_t csr;
	vaddr_t n;

	sc->sc_pintr = intr;
	sc->sc_parg = arg;

	for (p = sc->sc_dmas; p->addr != start; p = p->next)
		/*EMPTY*/;
	if (p == NULL) {
		printf("%s: trigger_output: bad addr: %p\n",
		    sc->sc_dev.dv_xname, start);
		return (EINVAL);
	}

	n = (char *)end - (char *)start;

	/*
	 * Do only `blksize' at a time, so audio_pint() is kept
	 * synchronous with us...
	 */
	chdma->cur_dma = p;
	chdma->blksz = blksize;
	chdma->segsz = n;

	if (n > chdma->blksz)
		n = chdma->blksz;

	chdma->count = n;

	csr = P_READ(sc, EBDMA_DCSR);
	if (csr & EBDCSR_DMAEN) {
		P_WRITE(sc, EBDMA_DCNT, (u_long)n);
		P_WRITE(sc, EBDMA_DADDR,
		    (u_long)p->dmamap->dm_segs[0].ds_addr);
	} else {
		P_WRITE(sc, EBDMA_DCSR, EBDCSR_RESET);
		P_WRITE(sc, EBDMA_DCSR, sc->sc_burst);

		P_WRITE(sc, EBDMA_DCNT, (u_long)n);
		P_WRITE(sc, EBDMA_DADDR,
		    (u_long)p->dmamap->dm_segs[0].ds_addr);

		P_WRITE(sc, EBDMA_DCSR, sc->sc_burst | EBDCSR_DMAEN |
		    EBDCSR_INTEN | EBDCSR_CNTEN | EBDCSR_NEXTEN);

		ce4231_write(sc, SP_LOWER_BASE_COUNT, 0xff);
		ce4231_write(sc, SP_UPPER_BASE_COUNT, 0xff);
		ce4231_write(sc, SP_INTERFACE_CONFIG,
		    ce4231_read(sc, SP_INTERFACE_CONFIG) | PLAYBACK_ENABLE);
	}
	chdma->lastaddr = p->dmamap->dm_segs[0].ds_addr + n;

	return (0);
}

int
ce4231_trigger_input(addr, start, end, blksize, intr, arg, param)
	void *addr, *start, *end;
	int blksize;
	void (*intr)(void *);
	void *arg;
	struct audio_params *param;
{
	struct ce4231_softc *sc = addr;
	struct cs_dma *p;
	struct cs_chdma *chdma = &sc->sc_rchdma;
	u_int32_t csr;
	vaddr_t n;

	sc->sc_rintr = intr;
	sc->sc_rarg = arg;

	for (p = sc->sc_dmas; p->addr != start; p = p->next)
		/*EMPTY*/;
	if (p == NULL) {
		printf("%s: trigger_input: bad addr: %p\n",
		    sc->sc_dev.dv_xname, start);
		return (EINVAL);
	}

	n = (char *)end - (char *)start;

	/*
	 * Do only `blksize' at a time, so audio_rint() is kept
	 * synchronous with us...
	 */
	chdma->cur_dma = p;
	chdma->blksz = blksize;
	chdma->segsz = n;

	if (n > chdma->blksz)
		n = chdma->blksz;

	chdma->count = n;

	csr = C_READ(sc, EBDMA_DCSR);
	if (csr & EBDCSR_DMAEN) {
		C_WRITE(sc, EBDMA_DCNT, (u_long)n);
		C_WRITE(sc, EBDMA_DADDR,
		    (u_long)p->dmamap->dm_segs[0].ds_addr);
	} else {
		C_WRITE(sc, EBDMA_DCSR, EBDCSR_RESET);
		C_WRITE(sc, EBDMA_DCSR, sc->sc_burst);

		C_WRITE(sc, EBDMA_DCNT, (u_long)n);
		C_WRITE(sc, EBDMA_DADDR,
		    (u_long)p->dmamap->dm_segs[0].ds_addr);

		C_WRITE(sc, EBDMA_DCSR, sc->sc_burst | EBDCSR_WRITE |
		    EBDCSR_DMAEN | EBDCSR_INTEN | EBDCSR_CNTEN | EBDCSR_NEXTEN);

		ce4231_write(sc, CS_LOWER_REC_CNT, 0xff);
		ce4231_write(sc, CS_UPPER_REC_CNT, 0xff);
		ce4231_write(sc, SP_INTERFACE_CONFIG,
		    ce4231_read(sc, SP_INTERFACE_CONFIG) | CAPTURE_ENABLE);
	}
	chdma->lastaddr = p->dmamap->dm_segs[0].ds_addr + n;

	return (0);
}


code:
/*	$OpenBSD: ce4231var.h,v 1.11 2010/07/26 23:17:19 jakemsr Exp $	*/

/*
 * Copyright (c) 1999 Jason L. Wright (jason@thought.net)
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 * Effort sponsored in part by the Defense Advanced Research Projects
 * Agency (DARPA) and Air Force Research Laboratory, Air Force
 * Materiel Command, USAF, under agreement number F30602-01-2-0537.
 *
 */

/*
 * Driver for CS4231/EBDMA based audio found in some sun4u systems
 */

/*
 * List of device memory allocations (see ce4231_malloc/ce4231_free).
 */
struct cs_dma {
	struct cs_dma *		next;
	caddr_t			addr;
	bus_dmamap_t		dmamap;
	bus_dma_segment_t	segs[1];
	int			nsegs;
	size_t			size;
};

struct cs_volume {
	u_int8_t	left;
	u_int8_t	right;
};

/* DMA info container for each channel (play and record). */
struct cs_chdma {
	struct cs_dma	*cur_dma;
	u_int32_t	blksz;
	u_int32_t	count;
	u_int32_t	segsz;
	u_int32_t	lastaddr;
};

struct ce4231_softc {
	struct	device sc_dev;		/* base device */
	struct	intrhand sc_ih;		/* interrupt vectoring */
	bus_dma_tag_t sc_dmatag;
	bus_space_tag_t	sc_bustag;	/* CS4231/DMA register tag */
	bus_space_handle_t sc_cshandle;	/* CS4231 handle */
	bus_space_handle_t sc_cdmahandle; /* capture DMA handle */
	bus_space_handle_t sc_pdmahandle; /* playback DMA handle */
	bus_space_handle_t sc_auxhandle;  /* AUX handle */
	int	sc_open;		/* already open? */

	void	(*sc_rintr)(void *);	/* input completion intr handler */
	void	*sc_rarg;		/* arg for sc_rintr() */
	void	(*sc_pintr)(void *);	/* output completion intr handler */
	void	*sc_parg;		/* arg for sc_pintr() */

	int sc_format_bits;
	int sc_speed_bits;
	int sc_precision;
	int sc_need_commit;
	int sc_channels;
	u_int sc_last_format;
	u_int32_t	sc_burst;
	struct cs_dma	*sc_dmas;	/* dma list */
	struct cs_chdma	sc_pchdma;
	struct cs_chdma	sc_rchdma;
	void *sc_pih, *sc_cih;
};


code:
/*	$OpenBSD: central.c,v 1.8 2014/07/12 18:44:43 tedu Exp $	*/

/*
 * Copyright (c) 2004 Jason L. Wright (jason@thought.net)
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include <sys/types.h>
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/device.h>
#include <sys/conf.h>
#include <sys/timeout.h>
#include <sys/malloc.h>

#include <machine/bus.h>
#include <machine/autoconf.h>
#include <machine/openfirm.h>

#include <sparc64/dev/centralvar.h>

struct central_softc {
	struct device sc_dv;
	bus_space_tag_t sc_bt;
	bus_space_tag_t sc_cbt;
	int sc_node;
	int sc_nrange;
	struct central_range *sc_range;
};

int central_match(struct device *, void *, void *);
void central_attach(struct device *, struct device *, void *);

int central_print(void *, const char *);
int central_get_string(int, char *, char **);

bus_space_tag_t central_alloc_bus_tag(struct central_softc *);
int _central_bus_map(bus_space_tag_t, bus_space_tag_t, bus_addr_t, bus_size_t,
    int, bus_space_handle_t *);

int
central_match(parent, match, aux)
	struct device *parent;
	void *match, *aux;
{
	struct mainbus_attach_args *ma = aux;

	if (strcmp(ma->ma_name, "central") == 0)
		return (1);
	return (0);
}

void
central_attach(parent, self, aux)
	struct device *parent, *self;
	void *aux;
{
	struct central_softc *sc = (struct central_softc *)self;
	struct mainbus_attach_args *ma = aux;
	int node0, node;

	sc->sc_bt = ma->ma_bustag;
	sc->sc_node = ma->ma_node;
	sc->sc_cbt = central_alloc_bus_tag(sc);

	getprop(sc->sc_node, "ranges", sizeof(struct central_range),
	    &sc->sc_nrange, (void **)&sc->sc_range);

	printf("\n");

	node0 = firstchild(sc->sc_node);
	for (node = node0; node; node = nextsibling(node)) {
		struct central_attach_args ca;

		bzero(&ca, sizeof(ca));
		ca.ca_node = node;
		ca.ca_bustag = sc->sc_cbt;
		if (central_get_string(ca.ca_node, "name", &ca.ca_name)) {
			printf("can't fetch name for node 0x%x\n", node);
			continue;
		}

		getprop(node, "reg", sizeof(struct central_reg),
		    &ca.ca_nreg, (void **)&ca.ca_reg);

		(void)config_found(&sc->sc_dv, (void *)&ca, central_print);

		if (ca.ca_name != NULL)
			free(ca.ca_name, M_DEVBUF, 0);
	}
}

int
central_get_string(int node, char *name, char **buf)
{
	int len;

	len = getproplen(node, name);
	if (len < 0)
		return (len);
	*buf = (char *)malloc(len + 1, M_DEVBUF, M_NOWAIT);
	if (*buf == NULL)
		return (-1);

	if (len != 0)
		getpropstringA(node, name, *buf);
	(*buf)[len] = '\0';
	return (0);
}

int
central_print(void *args, const char *busname)
{
	struct central_attach_args *ca = args;
	char *class;

	if (busname != NULL) {
		printf("\"%s\" at %s", ca->ca_name, busname);
		class = getpropstring(ca->ca_node, "device_type");
		if (*class != '\0')
			printf(" class %s", class);
	}
	return (UNCONF);
}

bus_space_tag_t
central_alloc_bus_tag(struct central_softc *sc)
{
	struct sparc_bus_space_tag *bt;

	bt = malloc(sizeof(*bt), M_DEVBUF, M_NOWAIT | M_ZERO);
	if (bt == NULL)
		panic("central: couldn't alloc bus tag");

	strlcpy(bt->name, sc->sc_dv.dv_xname, sizeof(bt->name));
	bt->cookie = sc;
	bt->parent = sc->sc_bt;
	bt->asi = bt->parent->asi;
	bt->sasi = bt->parent->sasi;
	bt->sparc_bus_map = _central_bus_map;
	/* XXX bt->sparc_bus_mmap = central_bus_mmap; */
	/* XXX bt->sparc_intr_establish = upa_intr_establish; */
	return (bt);
}

int
_central_bus_map(bus_space_tag_t t, bus_space_tag_t t0, bus_addr_t addr,
    bus_size_t size, int flags, bus_space_handle_t *hp)
{
	struct central_softc *sc = t->cookie;
	int64_t slot = BUS_ADDR_IOSPACE(addr);
	int64_t offset = BUS_ADDR_PADDR(addr);
	int i;

	if (t->parent == NULL || t->parent->sparc_bus_map == NULL) {
		printf("\ncentral_bus_map: invalid parent");
		return (EINVAL);
	}

	if (flags & BUS_SPACE_MAP_PROMADDRESS)
		return ((*t->parent->sparc_bus_map)(t, t0, addr,
		    size, flags, hp));

	for (i = 0; i < sc->sc_nrange; i++) {
		bus_addr_t paddr;

		if (sc->sc_range[i].cspace != slot)
			continue;

		paddr = offset - sc->sc_range[i].coffset;
		paddr += sc->sc_range[i].poffset;
		paddr |= ((bus_addr_t)sc->sc_range[i].pspace << 32);

		return ((*t->parent->sparc_bus_map)(t->parent, t0, paddr,
		    size, flags, hp));
	}

	return (EINVAL);
}

struct cfattach central_ca = {
	sizeof(struct central_softc), central_match, central_attach
};

struct cfdriver central_cd = {
	NULL, "central", DV_DULL
};


code:
/*	$OpenBSD: centralvar.h,v 1.1 2004/09/22 21:31:51 jason Exp $	*/

/*
 * Copyright (c) 2004 Jason L. Wright (jason@thought.net)
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

struct central_reg {
	u_int32_t	cbr_slot;
	u_int32_t	cbr_offset;
	u_int32_t	cbr_size;
};

struct central_attach_args {
	char *ca_name;
	bus_space_tag_t ca_bustag;
	struct central_reg *ca_reg;
	int ca_node;
	int ca_nreg;
};

struct central_range {
	u_int32_t	cspace;		/* Client space */
	u_int32_t	coffset;	/* Client offset */
	u_int32_t	pspace;		/* Parent space */
	u_int32_t	poffset;	/* Parent offset */
	u_int32_t	size;		/* Size in bytes of this range */
};

#define	central_bus_map(t, slot, offset, sz, flags, hp)		\
    bus_space_map(t, BUS_ADDR(slot, offset), sz, flags, hp)