xref: /onnv-gate/usr/src/uts/sun4u/io/bbc_beep.c (revision 7656:2621e50fdf4a)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
55129Smarx  * Common Development and Distribution License (the "License").
65129Smarx  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*7656SSherry.Moore@Sun.COM  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate 
270Sstevel@tonic-gate /*
280Sstevel@tonic-gate  * This is the Beep driver for bbc based beep mechanism.
290Sstevel@tonic-gate  *
300Sstevel@tonic-gate  */
310Sstevel@tonic-gate #include <sys/types.h>
320Sstevel@tonic-gate #include <sys/conf.h>
330Sstevel@tonic-gate #include <sys/ddi.h>
340Sstevel@tonic-gate #include <sys/sunddi.h>
350Sstevel@tonic-gate #include <sys/modctl.h>
360Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
370Sstevel@tonic-gate #include <sys/kmem.h>
380Sstevel@tonic-gate #include <sys/devops.h>
390Sstevel@tonic-gate #include <sys/bbc_beep.h>
405129Smarx #include <sys/beep.h>
410Sstevel@tonic-gate 
420Sstevel@tonic-gate 
430Sstevel@tonic-gate /* Pointer to the state structure */
440Sstevel@tonic-gate static void *bbc_beep_statep;
450Sstevel@tonic-gate 
460Sstevel@tonic-gate 
470Sstevel@tonic-gate /*
480Sstevel@tonic-gate  * Debug stuff
490Sstevel@tonic-gate  */
500Sstevel@tonic-gate #ifdef DEBUG
510Sstevel@tonic-gate int bbc_beep_debug = 0;
520Sstevel@tonic-gate #define	BBC_BEEP_DEBUG(args)  if (bbc_beep_debug) cmn_err args
530Sstevel@tonic-gate #define	BBC_BEEP_DEBUG1(args)  if (bbc_beep_debug > 1) cmn_err args
540Sstevel@tonic-gate #else
550Sstevel@tonic-gate #define	BBC_BEEP_DEBUG(args)
560Sstevel@tonic-gate #define	BBC_BEEP_DEBUG1(args)
570Sstevel@tonic-gate #endif
580Sstevel@tonic-gate 
590Sstevel@tonic-gate 
600Sstevel@tonic-gate /*
610Sstevel@tonic-gate  * Prototypes
620Sstevel@tonic-gate  */
630Sstevel@tonic-gate static int bbc_beep_attach(dev_info_t *dip, ddi_attach_cmd_t cmd);
640Sstevel@tonic-gate static int bbc_beep_detach(dev_info_t *dip, ddi_detach_cmd_t cmd);
650Sstevel@tonic-gate static int bbc_beep_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg,
660Sstevel@tonic-gate 		void **result);
675129Smarx static void bbc_beep_freq(void *arg, int freq);
685129Smarx static void bbc_beep_on(void *arg);
695129Smarx static void bbc_beep_off(void *arg);
700Sstevel@tonic-gate static void bbc_beep_cleanup(bbc_beep_state_t *);
710Sstevel@tonic-gate static int bbc_beep_map_regs(dev_info_t *, bbc_beep_state_t *);
720Sstevel@tonic-gate static bbc_beep_state_t *bbc_beep_obtain_state(dev_info_t *);
730Sstevel@tonic-gate static unsigned long bbc_beep_hztocounter(int);
740Sstevel@tonic-gate 
750Sstevel@tonic-gate 
760Sstevel@tonic-gate struct cb_ops bbc_beep_cb_ops = {
770Sstevel@tonic-gate 	nulldev,	/* open  */
780Sstevel@tonic-gate 	nulldev,	/* close */
790Sstevel@tonic-gate 	nulldev,	/* strategy */
800Sstevel@tonic-gate 	nulldev,	/* print */
810Sstevel@tonic-gate 	nulldev,	/* dump */
820Sstevel@tonic-gate 	nulldev,	/* read */
830Sstevel@tonic-gate 	nulldev,	/* write */
840Sstevel@tonic-gate 	nulldev,	/* ioctl */
850Sstevel@tonic-gate 	nulldev,	/* devmap */
860Sstevel@tonic-gate 	nulldev,	/* mmap */
870Sstevel@tonic-gate 	nulldev,	/* segmap */
880Sstevel@tonic-gate 	nochpoll,	/* poll */
890Sstevel@tonic-gate 	ddi_prop_op,	/* cb_prop_op */
900Sstevel@tonic-gate 	NULL,		/* streamtab  */
910Sstevel@tonic-gate 	D_64BIT | D_MP | D_NEW| D_HOTPLUG
920Sstevel@tonic-gate };
930Sstevel@tonic-gate 
940Sstevel@tonic-gate 
950Sstevel@tonic-gate static struct dev_ops bbc_beep_ops = {
960Sstevel@tonic-gate 	DEVO_REV,		/* Devo_rev */
970Sstevel@tonic-gate 	0,			/* Refcnt */
980Sstevel@tonic-gate 	bbc_beep_info,		/* Info */
990Sstevel@tonic-gate 	nulldev,		/* Identify */
1000Sstevel@tonic-gate 	nulldev,		/* Probe */
1010Sstevel@tonic-gate 	bbc_beep_attach,	/* Attach */
1020Sstevel@tonic-gate 	bbc_beep_detach,	/* Detach */
1030Sstevel@tonic-gate 	nodev,			/* Reset */
1040Sstevel@tonic-gate 	&bbc_beep_cb_ops,	/* Driver operations */
1050Sstevel@tonic-gate 	0,			/* Bus operations */
106*7656SSherry.Moore@Sun.COM 	ddi_power,		/* Power */
107*7656SSherry.Moore@Sun.COM 	ddi_quiesce_not_supported,	/* devo_quiesce */
1080Sstevel@tonic-gate };
1090Sstevel@tonic-gate 
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate static struct modldrv modldrv = {
1120Sstevel@tonic-gate 	&mod_driverops, 		/* This one is a driver */
113*7656SSherry.Moore@Sun.COM 	"BBC Beep Driver", 		/* Name of the module. */
1140Sstevel@tonic-gate 	&bbc_beep_ops,			/* Driver ops */
1150Sstevel@tonic-gate };
1160Sstevel@tonic-gate 
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate static struct modlinkage modlinkage = {
1190Sstevel@tonic-gate 	MODREV_1, (void *)&modldrv, NULL
1200Sstevel@tonic-gate };
1210Sstevel@tonic-gate 
1220Sstevel@tonic-gate 
1230Sstevel@tonic-gate int
_init(void)1240Sstevel@tonic-gate _init(void)
1250Sstevel@tonic-gate {
1260Sstevel@tonic-gate 	int error;
1270Sstevel@tonic-gate 
1280Sstevel@tonic-gate 	/* Initialize the soft state structures */
1290Sstevel@tonic-gate 	if ((error = ddi_soft_state_init(&bbc_beep_statep,
1305129Smarx 	    sizeof (bbc_beep_state_t), 1)) != 0) {
1310Sstevel@tonic-gate 
1320Sstevel@tonic-gate 		return (error);
1330Sstevel@tonic-gate 	}
1340Sstevel@tonic-gate 
1350Sstevel@tonic-gate 	/* Install the loadable module */
1360Sstevel@tonic-gate 	if ((error = mod_install(&modlinkage)) != 0) {
1370Sstevel@tonic-gate 		ddi_soft_state_fini(&bbc_beep_statep);
1380Sstevel@tonic-gate 	}
1390Sstevel@tonic-gate 
1400Sstevel@tonic-gate 	return (error);
1410Sstevel@tonic-gate }
1420Sstevel@tonic-gate 
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate int
_info(struct modinfo * modinfop)1450Sstevel@tonic-gate _info(struct modinfo *modinfop)
1460Sstevel@tonic-gate {
1470Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
1480Sstevel@tonic-gate }
1490Sstevel@tonic-gate 
1500Sstevel@tonic-gate 
1510Sstevel@tonic-gate int
_fini(void)1520Sstevel@tonic-gate _fini(void)
1530Sstevel@tonic-gate {
1540Sstevel@tonic-gate 	int error;
1550Sstevel@tonic-gate 
1560Sstevel@tonic-gate 	error = mod_remove(&modlinkage);
1570Sstevel@tonic-gate 
1580Sstevel@tonic-gate 	if (error == 0) {
1590Sstevel@tonic-gate 		/* Release per module resources */
1600Sstevel@tonic-gate 		ddi_soft_state_fini(&bbc_beep_statep);
1610Sstevel@tonic-gate 	}
1620Sstevel@tonic-gate 
1630Sstevel@tonic-gate 	return (error);
1640Sstevel@tonic-gate }
1650Sstevel@tonic-gate 
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate /*
1680Sstevel@tonic-gate  * Beep entry points
1690Sstevel@tonic-gate  */
1700Sstevel@tonic-gate 
1710Sstevel@tonic-gate /*
1720Sstevel@tonic-gate  * bbc_beep_attach:
1730Sstevel@tonic-gate  */
1740Sstevel@tonic-gate static int
bbc_beep_attach(dev_info_t * dip,ddi_attach_cmd_t cmd)1750Sstevel@tonic-gate bbc_beep_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
1760Sstevel@tonic-gate {
1770Sstevel@tonic-gate 	int		instance;		/* Instance number */
1780Sstevel@tonic-gate 
1790Sstevel@tonic-gate 	/* Pointer to soft state */
1800Sstevel@tonic-gate 	bbc_beep_state_t	*bbc_beeptr = NULL;
1810Sstevel@tonic-gate 
1820Sstevel@tonic-gate 	BBC_BEEP_DEBUG1((CE_CONT, "bbc_beep_attach: Start"));
1830Sstevel@tonic-gate 
1840Sstevel@tonic-gate 	switch (cmd) {
1850Sstevel@tonic-gate 		case DDI_ATTACH:
1860Sstevel@tonic-gate 			break;
1870Sstevel@tonic-gate 		case DDI_RESUME:
1880Sstevel@tonic-gate 			return (DDI_SUCCESS);
1890Sstevel@tonic-gate 		default:
1900Sstevel@tonic-gate 			return (DDI_FAILURE);
1910Sstevel@tonic-gate 	}
1920Sstevel@tonic-gate 
1930Sstevel@tonic-gate 	/* Get the instance and create soft state */
1940Sstevel@tonic-gate 	instance = ddi_get_instance(dip);
1950Sstevel@tonic-gate 
1960Sstevel@tonic-gate 	if (ddi_soft_state_zalloc(bbc_beep_statep, instance) != 0) {
1970Sstevel@tonic-gate 
1980Sstevel@tonic-gate 		return (DDI_FAILURE);
1990Sstevel@tonic-gate 	}
2000Sstevel@tonic-gate 
2010Sstevel@tonic-gate 	bbc_beeptr = ddi_get_soft_state(bbc_beep_statep, instance);
2020Sstevel@tonic-gate 
2030Sstevel@tonic-gate 	if (bbc_beeptr == NULL) {
2040Sstevel@tonic-gate 
2050Sstevel@tonic-gate 		return (DDI_FAILURE);
2060Sstevel@tonic-gate 	}
2070Sstevel@tonic-gate 
2080Sstevel@tonic-gate 	BBC_BEEP_DEBUG1((CE_CONT, "bbc_beeptr = 0x%p, instance %x",
2090Sstevel@tonic-gate 	    (void *)bbc_beeptr, instance));
2100Sstevel@tonic-gate 
2110Sstevel@tonic-gate 	/* Save the dip */
2120Sstevel@tonic-gate 	bbc_beeptr->bbc_beep_dip = dip;
2130Sstevel@tonic-gate 
2140Sstevel@tonic-gate 	/* Initialize beeper mode */
2150Sstevel@tonic-gate 	bbc_beeptr->bbc_beep_mode = BBC_BEEP_OFF;
2160Sstevel@tonic-gate 
2170Sstevel@tonic-gate 	/* Map the Beep Control and Beep counter Registers */
2180Sstevel@tonic-gate 	if (bbc_beep_map_regs(dip, bbc_beeptr) != DDI_SUCCESS) {
2190Sstevel@tonic-gate 
2200Sstevel@tonic-gate 		BBC_BEEP_DEBUG((CE_WARN, \
2215129Smarx 		    "bbc_beep_attach: Mapping of bbc registers failed."));
2220Sstevel@tonic-gate 
2230Sstevel@tonic-gate 		bbc_beep_cleanup(bbc_beeptr);
2240Sstevel@tonic-gate 
2250Sstevel@tonic-gate 		return (DDI_FAILURE);
2260Sstevel@tonic-gate 	}
2270Sstevel@tonic-gate 
2285129Smarx 	(void) beep_init((void *)dip, bbc_beep_on, bbc_beep_off, bbc_beep_freq);
2290Sstevel@tonic-gate 
2300Sstevel@tonic-gate 	/* Display information in the banner */
2310Sstevel@tonic-gate 	ddi_report_dev(dip);
2320Sstevel@tonic-gate 
2330Sstevel@tonic-gate 	BBC_BEEP_DEBUG1((CE_CONT, "bbc_beep_attach: dip = 0x%p done",
2340Sstevel@tonic-gate 	    (void *)dip));
2350Sstevel@tonic-gate 
2360Sstevel@tonic-gate 	return (DDI_SUCCESS);
2370Sstevel@tonic-gate }
2380Sstevel@tonic-gate 
2390Sstevel@tonic-gate 
2400Sstevel@tonic-gate /*
2410Sstevel@tonic-gate  * bbc_beep_detach:
2420Sstevel@tonic-gate  */
2430Sstevel@tonic-gate static int
bbc_beep_detach(dev_info_t * dip,ddi_detach_cmd_t cmd)2440Sstevel@tonic-gate bbc_beep_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
2450Sstevel@tonic-gate {
2460Sstevel@tonic-gate 	/* Pointer to soft state */
2470Sstevel@tonic-gate 	bbc_beep_state_t	*bbc_beeptr = NULL;
2480Sstevel@tonic-gate 
2490Sstevel@tonic-gate 	BBC_BEEP_DEBUG1((CE_CONT, "bbc_beep_detach: Start"));
2500Sstevel@tonic-gate 
2510Sstevel@tonic-gate 	switch (cmd) {
2520Sstevel@tonic-gate 		case DDI_SUSPEND:
2530Sstevel@tonic-gate 			bbc_beeptr = bbc_beep_obtain_state(dip);
2540Sstevel@tonic-gate 
2550Sstevel@tonic-gate 			if (bbc_beeptr == NULL) {
2560Sstevel@tonic-gate 
2570Sstevel@tonic-gate 				return (DDI_FAILURE);
2580Sstevel@tonic-gate 			}
2590Sstevel@tonic-gate 
2600Sstevel@tonic-gate 			/*
2610Sstevel@tonic-gate 			 * If a beep is in progress; fail suspend
2620Sstevel@tonic-gate 			 */
2630Sstevel@tonic-gate 			if (bbc_beeptr->bbc_beep_mode == BBC_BEEP_OFF) {
2640Sstevel@tonic-gate 				return (DDI_SUCCESS);
2650Sstevel@tonic-gate 			} else {
2660Sstevel@tonic-gate 				return (DDI_FAILURE);
2670Sstevel@tonic-gate 			}
2680Sstevel@tonic-gate 		default:
2690Sstevel@tonic-gate 
2700Sstevel@tonic-gate 			return (DDI_FAILURE);
2710Sstevel@tonic-gate 	}
2720Sstevel@tonic-gate }
2730Sstevel@tonic-gate 
2740Sstevel@tonic-gate 
2750Sstevel@tonic-gate /*
2760Sstevel@tonic-gate  * bbc_beep_info:
2770Sstevel@tonic-gate  */
2780Sstevel@tonic-gate /* ARGSUSED */
2790Sstevel@tonic-gate static int
bbc_beep_info(dev_info_t * dip,ddi_info_cmd_t infocmd,void * arg,void ** result)2800Sstevel@tonic-gate bbc_beep_info(dev_info_t *dip, ddi_info_cmd_t infocmd,
2810Sstevel@tonic-gate 		void *arg, void **result)
2820Sstevel@tonic-gate {
2830Sstevel@tonic-gate 	dev_t dev;
2840Sstevel@tonic-gate 	bbc_beep_state_t  *bbc_beeptr;
2850Sstevel@tonic-gate 	int instance, error;
2860Sstevel@tonic-gate 
2870Sstevel@tonic-gate 	switch (infocmd) {
2880Sstevel@tonic-gate 
2890Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
2900Sstevel@tonic-gate 		dev = (dev_t)arg;
2910Sstevel@tonic-gate 		instance = BEEP_UNIT(dev);
2920Sstevel@tonic-gate 
2930Sstevel@tonic-gate 		if ((bbc_beeptr = ddi_get_soft_state(bbc_beep_statep,
2940Sstevel@tonic-gate 		    instance)) == NULL) {
2950Sstevel@tonic-gate 
2960Sstevel@tonic-gate 			return (DDI_FAILURE);
2970Sstevel@tonic-gate 		}
2980Sstevel@tonic-gate 
2990Sstevel@tonic-gate 		*result = (void *)bbc_beeptr->bbc_beep_dip;
3000Sstevel@tonic-gate 
3010Sstevel@tonic-gate 		error = DDI_SUCCESS;
3020Sstevel@tonic-gate 		break;
3030Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
3040Sstevel@tonic-gate 		dev = (dev_t)arg;
3050Sstevel@tonic-gate 		instance = BEEP_UNIT(dev);
3060Sstevel@tonic-gate 
3070Sstevel@tonic-gate 		*result = (void *)(uintptr_t)instance;
3080Sstevel@tonic-gate 
3090Sstevel@tonic-gate 		error = DDI_SUCCESS;
3100Sstevel@tonic-gate 		break;
3110Sstevel@tonic-gate 	default:
3120Sstevel@tonic-gate 		error = DDI_FAILURE;
3130Sstevel@tonic-gate 
3140Sstevel@tonic-gate 	}
3150Sstevel@tonic-gate 
3160Sstevel@tonic-gate 	return (error);
3170Sstevel@tonic-gate }
3180Sstevel@tonic-gate 
3190Sstevel@tonic-gate 
3200Sstevel@tonic-gate /*
3210Sstevel@tonic-gate  * bbc_beep_freq() :
3220Sstevel@tonic-gate  *	Set the frequency
3230Sstevel@tonic-gate  */
3240Sstevel@tonic-gate static void
bbc_beep_freq(void * arg,int freq)3255129Smarx bbc_beep_freq(void *arg, int freq)
3260Sstevel@tonic-gate {
3275129Smarx 	dev_info_t *dip = (dev_info_t *)arg;
3280Sstevel@tonic-gate 	unsigned long counter;
3290Sstevel@tonic-gate 	int8_t beep_c2 = 0;
3300Sstevel@tonic-gate 	int8_t beep_c3 = 0;
3310Sstevel@tonic-gate 
3320Sstevel@tonic-gate 	bbc_beep_state_t *bbc_beeptr = bbc_beep_obtain_state(dip);
3330Sstevel@tonic-gate 
3340Sstevel@tonic-gate 	/* Convert the frequency in hz to the bbc counter value */
3350Sstevel@tonic-gate 	counter = bbc_beep_hztocounter(freq);
3360Sstevel@tonic-gate 
3370Sstevel@tonic-gate 	/* Extract relevant second and third byte of counter value */
3380Sstevel@tonic-gate 	beep_c2 = (counter & 0xff00) >> 8;
3390Sstevel@tonic-gate 	beep_c3 = (counter & 0xff0000) >> 16;
3400Sstevel@tonic-gate 
3410Sstevel@tonic-gate 	/*
3420Sstevel@tonic-gate 	 * We need to write individual bytes instead of writing
3430Sstevel@tonic-gate 	 * all of 32 bits to take care of allignment problem.
3440Sstevel@tonic-gate 	 * Write 0 to LS 8 bits and MS 8 bits
3450Sstevel@tonic-gate 	 * Write beep_c3 to bit 8..15 and beep_c2 to bit 16..24
3460Sstevel@tonic-gate 	 * Little Endian format
3470Sstevel@tonic-gate 	 */
3480Sstevel@tonic-gate 	BEEP_WRITE_COUNTER_REG(0, 0);
3490Sstevel@tonic-gate 	BEEP_WRITE_COUNTER_REG(1, beep_c3);
3500Sstevel@tonic-gate 	BEEP_WRITE_COUNTER_REG(2, beep_c2);
3510Sstevel@tonic-gate 	BEEP_WRITE_COUNTER_REG(3, 0);
3520Sstevel@tonic-gate 
3530Sstevel@tonic-gate 	BBC_BEEP_DEBUG1((CE_CONT,
3540Sstevel@tonic-gate 	    "bbc_beep_freq: dip = 0x%p, freq = %d, counter = 0x%x : Done",
3550Sstevel@tonic-gate 	    (void *)dip, freq, (int)counter));
3560Sstevel@tonic-gate }
3570Sstevel@tonic-gate 
3580Sstevel@tonic-gate 
3590Sstevel@tonic-gate /*
3600Sstevel@tonic-gate  * bbc_beep_on() :
3610Sstevel@tonic-gate  *	Turn the beeper on
3620Sstevel@tonic-gate  */
3630Sstevel@tonic-gate static void
bbc_beep_on(void * arg)3645129Smarx bbc_beep_on(void *arg)
3650Sstevel@tonic-gate {
3665129Smarx 	dev_info_t *dip = (dev_info_t *)arg;
3670Sstevel@tonic-gate 	bbc_beep_state_t *bbc_beeptr = bbc_beep_obtain_state(dip);
3680Sstevel@tonic-gate 
3690Sstevel@tonic-gate 	BEEP_WRITE_CTRL_REG(BBC_BEEP_ON);
3700Sstevel@tonic-gate 
3710Sstevel@tonic-gate 	bbc_beeptr->bbc_beep_mode = BBC_BEEP_ON;
3720Sstevel@tonic-gate 
3730Sstevel@tonic-gate 	BBC_BEEP_DEBUG1((CE_CONT, "bbc_beep_on: dip = 0x%p done",
3740Sstevel@tonic-gate 	    (void *)dip));
3750Sstevel@tonic-gate }
3760Sstevel@tonic-gate 
3770Sstevel@tonic-gate 
3780Sstevel@tonic-gate /*
3790Sstevel@tonic-gate  * bbc_beep_off() :
3800Sstevel@tonic-gate  * 	Turn the beeper off
3810Sstevel@tonic-gate  */
3820Sstevel@tonic-gate static void
bbc_beep_off(void * arg)3835129Smarx bbc_beep_off(void *arg)
3840Sstevel@tonic-gate {
3855129Smarx 	dev_info_t *dip = (dev_info_t *)arg;
3860Sstevel@tonic-gate 	bbc_beep_state_t *bbc_beeptr = bbc_beep_obtain_state(dip);
3870Sstevel@tonic-gate 
3880Sstevel@tonic-gate 	BEEP_WRITE_CTRL_REG(BBC_BEEP_OFF);
3890Sstevel@tonic-gate 
3900Sstevel@tonic-gate 	bbc_beeptr->bbc_beep_mode = BBC_BEEP_OFF;
3910Sstevel@tonic-gate 
3920Sstevel@tonic-gate 	BBC_BEEP_DEBUG1((CE_CONT, "bbc_beep_off: dip = 0x%p done",
3930Sstevel@tonic-gate 	    (void *)dip));
3940Sstevel@tonic-gate }
3950Sstevel@tonic-gate 
3960Sstevel@tonic-gate 
3970Sstevel@tonic-gate /*
3980Sstevel@tonic-gate  * bbc_beep_map_regs() :
3990Sstevel@tonic-gate  *
4000Sstevel@tonic-gate  *	The Keyboard Beep Control Register and Keyboard Beep Counter Register
4010Sstevel@tonic-gate  *	should be mapped into a non-cacheable portion of the  system
4020Sstevel@tonic-gate  *	addressable space.
4030Sstevel@tonic-gate  */
4040Sstevel@tonic-gate static int
bbc_beep_map_regs(dev_info_t * dip,bbc_beep_state_t * bbc_beeptr)4050Sstevel@tonic-gate bbc_beep_map_regs(dev_info_t *dip, bbc_beep_state_t *bbc_beeptr)
4060Sstevel@tonic-gate {
4070Sstevel@tonic-gate 	ddi_device_acc_attr_t attr;
4080Sstevel@tonic-gate 
4090Sstevel@tonic-gate 	BBC_BEEP_DEBUG1((CE_CONT, "bbc_beep_map_regs: Start\n"));
4100Sstevel@tonic-gate 
4110Sstevel@tonic-gate 	/* The host controller will be little endian */
4120Sstevel@tonic-gate 	attr.devacc_attr_version = DDI_DEVICE_ATTR_V0;
4130Sstevel@tonic-gate 	attr.devacc_attr_endian_flags  = DDI_STRUCTURE_LE_ACC;
4140Sstevel@tonic-gate 	attr.devacc_attr_dataorder = DDI_STRICTORDER_ACC;
4150Sstevel@tonic-gate 
4160Sstevel@tonic-gate 	/* Map in operational registers */
4170Sstevel@tonic-gate 	if (ddi_regs_map_setup(dip, 0,
4185129Smarx 	    (caddr_t *)&bbc_beeptr->bbc_beep_regsp,
4195129Smarx 	    0,
4205129Smarx 	    sizeof (bbc_beep_regs_t),
4215129Smarx 	    &attr,
4225129Smarx 	    &bbc_beeptr->bbc_beep_regs_handle) != DDI_SUCCESS) {
4230Sstevel@tonic-gate 
4240Sstevel@tonic-gate 		return (DDI_FAILURE);
4250Sstevel@tonic-gate 	}
4260Sstevel@tonic-gate 
4270Sstevel@tonic-gate 	BBC_BEEP_DEBUG1((CE_CONT, "bbc_beep_map_regs: done\n"));
4280Sstevel@tonic-gate 
4290Sstevel@tonic-gate 	return (DDI_SUCCESS);
4300Sstevel@tonic-gate }
4310Sstevel@tonic-gate 
4320Sstevel@tonic-gate 
4330Sstevel@tonic-gate /*
4340Sstevel@tonic-gate  * bbc_beep_obtain_state:
4350Sstevel@tonic-gate  */
4360Sstevel@tonic-gate static bbc_beep_state_t *
bbc_beep_obtain_state(dev_info_t * dip)4370Sstevel@tonic-gate bbc_beep_obtain_state(dev_info_t *dip)
4380Sstevel@tonic-gate {
4390Sstevel@tonic-gate 	int instance = ddi_get_instance(dip);
4400Sstevel@tonic-gate 
4410Sstevel@tonic-gate 	bbc_beep_state_t *state = ddi_get_soft_state(bbc_beep_statep, instance);
4420Sstevel@tonic-gate 
4430Sstevel@tonic-gate 	ASSERT(state != NULL);
4440Sstevel@tonic-gate 
4450Sstevel@tonic-gate 	BBC_BEEP_DEBUG1((CE_CONT, "bbc_beep_obtain_state: done"));
4460Sstevel@tonic-gate 
4470Sstevel@tonic-gate 	return (state);
4480Sstevel@tonic-gate }
4490Sstevel@tonic-gate 
4500Sstevel@tonic-gate 
4510Sstevel@tonic-gate /*
4520Sstevel@tonic-gate  * bbc_beep_cleanup :
4530Sstevel@tonic-gate  *	Cleanup soft state
4540Sstevel@tonic-gate  */
4550Sstevel@tonic-gate static void
bbc_beep_cleanup(bbc_beep_state_t * bbc_beeptr)4560Sstevel@tonic-gate bbc_beep_cleanup(bbc_beep_state_t *bbc_beeptr)
4570Sstevel@tonic-gate {
4580Sstevel@tonic-gate 	int instance = ddi_get_instance(bbc_beeptr->bbc_beep_dip);
4590Sstevel@tonic-gate 
4600Sstevel@tonic-gate 	ddi_soft_state_free(bbc_beep_statep, instance);
4610Sstevel@tonic-gate 
4620Sstevel@tonic-gate 	BBC_BEEP_DEBUG1((CE_CONT, "bbc_beep_cleanup: done"));
4630Sstevel@tonic-gate }
4640Sstevel@tonic-gate 
4650Sstevel@tonic-gate 
4660Sstevel@tonic-gate /*
4670Sstevel@tonic-gate  * bbc_beep_hztocounter() :
4680Sstevel@tonic-gate  *	Given a frequency in hz, find out the value to
4690Sstevel@tonic-gate  *	be set in the Keyboard Beep Counter register
4700Sstevel@tonic-gate  *	BBC beeper uses the following formula to calculate
4710Sstevel@tonic-gate  * 	frequency. The formulae is :
4720Sstevel@tonic-gate  *	frequency generated = system freq /2^(n+2)
4730Sstevel@tonic-gate  *	Where n = position of the bit of counter register
4740Sstevel@tonic-gate  *	that is turned on and can range between 10 to 18.
4750Sstevel@tonic-gate  *	So in this function, the inputs are frequency generated
4760Sstevel@tonic-gate  *	and system frequency and we need to find out n, i.e, which
4770Sstevel@tonic-gate  *	bit to turn on.(Ref. to Section 4.2.22 of the BBC programming
4780Sstevel@tonic-gate  *	manual).
4790Sstevel@tonic-gate  */
4800Sstevel@tonic-gate unsigned long
bbc_beep_hztocounter(int freq)4810Sstevel@tonic-gate bbc_beep_hztocounter(int freq)
4820Sstevel@tonic-gate {
4830Sstevel@tonic-gate 	int 		i;
4840Sstevel@tonic-gate 	unsigned long	counter;
4850Sstevel@tonic-gate 	int 		newfreq, oldfreq;
4860Sstevel@tonic-gate 
4870Sstevel@tonic-gate 	int		system_freq;
4880Sstevel@tonic-gate 
4890Sstevel@tonic-gate 	/*
4900Sstevel@tonic-gate 	 * Get system frequency for the root dev_info properties
4910Sstevel@tonic-gate 	 */
4920Sstevel@tonic-gate 	system_freq = ddi_prop_get_int(DDI_DEV_T_ANY, ddi_root_node(),
4935129Smarx 	    0, "clock-frequency", 0);
4940Sstevel@tonic-gate 
4950Sstevel@tonic-gate 	oldfreq = 0;
4960Sstevel@tonic-gate 
4970Sstevel@tonic-gate 	/*
4980Sstevel@tonic-gate 	 * Calculate frequency by turning on ith bit and
4990Sstevel@tonic-gate 	 * matching it with the passed frequency and we do this
5000Sstevel@tonic-gate 	 * in a loop for all the relevant bits
5010Sstevel@tonic-gate 	 */
5020Sstevel@tonic-gate 	for (i = BBC_BEEP_MIN_SHIFT, counter = 1 << BBC_BEEP_MSBIT;
5030Sstevel@tonic-gate 	    i >= BBC_BEEP_MAX_SHIFT; i--, counter >>= 1) {
5040Sstevel@tonic-gate 
5050Sstevel@tonic-gate 		/*
5060Sstevel@tonic-gate 		 * Calculate the frequency by dividing the system
5070Sstevel@tonic-gate 		 * frequency by 2^i
5080Sstevel@tonic-gate 		 */
5090Sstevel@tonic-gate 		newfreq = system_freq >> i;
5100Sstevel@tonic-gate 
5110Sstevel@tonic-gate 		/*
5120Sstevel@tonic-gate 		 * Check if we turn on the ith bit, the
5130Sstevel@tonic-gate 		 * frequency matches exactly or not
5140Sstevel@tonic-gate 		 */
5150Sstevel@tonic-gate 		if (newfreq == freq) {
5160Sstevel@tonic-gate 			/*
5170Sstevel@tonic-gate 			 * Exact match of passed frequency with the
5180Sstevel@tonic-gate 			 * counter value
5190Sstevel@tonic-gate 			 */
5200Sstevel@tonic-gate 
5210Sstevel@tonic-gate 			return (counter);
5220Sstevel@tonic-gate 		}
5230Sstevel@tonic-gate 
5240Sstevel@tonic-gate 		/*
5250Sstevel@tonic-gate 		 * If calculated frequency is bigger
5260Sstevel@tonic-gate 		 * return the passed frequency
5270Sstevel@tonic-gate 		 */
5280Sstevel@tonic-gate 		if (newfreq > freq) {
5290Sstevel@tonic-gate 
5300Sstevel@tonic-gate 			if (i == BBC_BEEP_MIN_SHIFT) {
5310Sstevel@tonic-gate 				/* Input freq is less than the possible min */
5320Sstevel@tonic-gate 
5330Sstevel@tonic-gate 				return (counter);
5340Sstevel@tonic-gate 			}
5350Sstevel@tonic-gate 
5360Sstevel@tonic-gate 			/*
5370Sstevel@tonic-gate 			 * Find out the nearest frequency to the passed
5380Sstevel@tonic-gate 			 * frequency by comparing the difference between
5390Sstevel@tonic-gate 			 * the calculated frequency and the passed frequency
5400Sstevel@tonic-gate 			 */
5410Sstevel@tonic-gate 			if ((freq - oldfreq) > (newfreq - freq)) {
5420Sstevel@tonic-gate 				/* Return new counter corres. to newfreq */
5430Sstevel@tonic-gate 
5440Sstevel@tonic-gate 				return (counter);
5450Sstevel@tonic-gate 			}
5460Sstevel@tonic-gate 
5470Sstevel@tonic-gate 			/* Return old counter corresponding to oldfreq */
5480Sstevel@tonic-gate 
5490Sstevel@tonic-gate 			return (counter << 1);
5500Sstevel@tonic-gate 		}
5510Sstevel@tonic-gate 
5520Sstevel@tonic-gate 		oldfreq = newfreq;
5530Sstevel@tonic-gate 	}
5540Sstevel@tonic-gate 
5550Sstevel@tonic-gate 	/*
5560Sstevel@tonic-gate 	 * Input freq is greater than the possible max;
5570Sstevel@tonic-gate 	 * Back off the counter value and return max counter
5580Sstevel@tonic-gate 	 * value possible in the register
5590Sstevel@tonic-gate 	 */
5600Sstevel@tonic-gate 	return (counter << 1);
5610Sstevel@tonic-gate }
562