xref: /illumos-gate/usr/src/uts/sun4u/io/bbc_beep.c (revision 19397407)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5c35aa225Smarx  * Common Development and Distribution License (the "License").
6c35aa225Smarx  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*19397407SSherry Moore  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*
287c478bd9Sstevel@tonic-gate  * This is the Beep driver for bbc based beep mechanism.
297c478bd9Sstevel@tonic-gate  *
307c478bd9Sstevel@tonic-gate  */
317c478bd9Sstevel@tonic-gate #include <sys/types.h>
327c478bd9Sstevel@tonic-gate #include <sys/conf.h>
337c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
347c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
357c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
367c478bd9Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
377c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
387c478bd9Sstevel@tonic-gate #include <sys/devops.h>
397c478bd9Sstevel@tonic-gate #include <sys/bbc_beep.h>
40c35aa225Smarx #include <sys/beep.h>
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate /* Pointer to the state structure */
447c478bd9Sstevel@tonic-gate static void *bbc_beep_statep;
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate /*
487c478bd9Sstevel@tonic-gate  * Debug stuff
497c478bd9Sstevel@tonic-gate  */
507c478bd9Sstevel@tonic-gate #ifdef DEBUG
517c478bd9Sstevel@tonic-gate int bbc_beep_debug = 0;
527c478bd9Sstevel@tonic-gate #define	BBC_BEEP_DEBUG(args)  if (bbc_beep_debug) cmn_err args
537c478bd9Sstevel@tonic-gate #define	BBC_BEEP_DEBUG1(args)  if (bbc_beep_debug > 1) cmn_err args
547c478bd9Sstevel@tonic-gate #else
557c478bd9Sstevel@tonic-gate #define	BBC_BEEP_DEBUG(args)
567c478bd9Sstevel@tonic-gate #define	BBC_BEEP_DEBUG1(args)
577c478bd9Sstevel@tonic-gate #endif
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate /*
617c478bd9Sstevel@tonic-gate  * Prototypes
627c478bd9Sstevel@tonic-gate  */
637c478bd9Sstevel@tonic-gate static int bbc_beep_attach(dev_info_t *dip, ddi_attach_cmd_t cmd);
647c478bd9Sstevel@tonic-gate static int bbc_beep_detach(dev_info_t *dip, ddi_detach_cmd_t cmd);
657c478bd9Sstevel@tonic-gate static int bbc_beep_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg,
667c478bd9Sstevel@tonic-gate 		void **result);
67c35aa225Smarx static void bbc_beep_freq(void *arg, int freq);
68c35aa225Smarx static void bbc_beep_on(void *arg);
69c35aa225Smarx static void bbc_beep_off(void *arg);
707c478bd9Sstevel@tonic-gate static void bbc_beep_cleanup(bbc_beep_state_t *);
717c478bd9Sstevel@tonic-gate static int bbc_beep_map_regs(dev_info_t *, bbc_beep_state_t *);
727c478bd9Sstevel@tonic-gate static bbc_beep_state_t *bbc_beep_obtain_state(dev_info_t *);
737c478bd9Sstevel@tonic-gate static unsigned long bbc_beep_hztocounter(int);
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate struct cb_ops bbc_beep_cb_ops = {
777c478bd9Sstevel@tonic-gate 	nulldev,	/* open  */
787c478bd9Sstevel@tonic-gate 	nulldev,	/* close */
797c478bd9Sstevel@tonic-gate 	nulldev,	/* strategy */
807c478bd9Sstevel@tonic-gate 	nulldev,	/* print */
817c478bd9Sstevel@tonic-gate 	nulldev,	/* dump */
827c478bd9Sstevel@tonic-gate 	nulldev,	/* read */
837c478bd9Sstevel@tonic-gate 	nulldev,	/* write */
847c478bd9Sstevel@tonic-gate 	nulldev,	/* ioctl */
857c478bd9Sstevel@tonic-gate 	nulldev,	/* devmap */
867c478bd9Sstevel@tonic-gate 	nulldev,	/* mmap */
877c478bd9Sstevel@tonic-gate 	nulldev,	/* segmap */
887c478bd9Sstevel@tonic-gate 	nochpoll,	/* poll */
897c478bd9Sstevel@tonic-gate 	ddi_prop_op,	/* cb_prop_op */
907c478bd9Sstevel@tonic-gate 	NULL,		/* streamtab  */
917c478bd9Sstevel@tonic-gate 	D_64BIT | D_MP | D_NEW| D_HOTPLUG
927c478bd9Sstevel@tonic-gate };
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate static struct dev_ops bbc_beep_ops = {
967c478bd9Sstevel@tonic-gate 	DEVO_REV,		/* Devo_rev */
977c478bd9Sstevel@tonic-gate 	0,			/* Refcnt */
987c478bd9Sstevel@tonic-gate 	bbc_beep_info,		/* Info */
997c478bd9Sstevel@tonic-gate 	nulldev,		/* Identify */
1007c478bd9Sstevel@tonic-gate 	nulldev,		/* Probe */
1017c478bd9Sstevel@tonic-gate 	bbc_beep_attach,	/* Attach */
1027c478bd9Sstevel@tonic-gate 	bbc_beep_detach,	/* Detach */
1037c478bd9Sstevel@tonic-gate 	nodev,			/* Reset */
1047c478bd9Sstevel@tonic-gate 	&bbc_beep_cb_ops,	/* Driver operations */
1057c478bd9Sstevel@tonic-gate 	0,			/* Bus operations */
106*19397407SSherry Moore 	ddi_power,		/* Power */
107*19397407SSherry Moore 	ddi_quiesce_not_supported,	/* devo_quiesce */
1087c478bd9Sstevel@tonic-gate };
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate static struct modldrv modldrv = {
1127c478bd9Sstevel@tonic-gate 	&mod_driverops, 		/* This one is a driver */
113*19397407SSherry Moore 	"BBC Beep Driver", 		/* Name of the module. */
1147c478bd9Sstevel@tonic-gate 	&bbc_beep_ops,			/* Driver ops */
1157c478bd9Sstevel@tonic-gate };
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = {
1197c478bd9Sstevel@tonic-gate 	MODREV_1, (void *)&modldrv, NULL
1207c478bd9Sstevel@tonic-gate };
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate int
_init(void)1247c478bd9Sstevel@tonic-gate _init(void)
1257c478bd9Sstevel@tonic-gate {
1267c478bd9Sstevel@tonic-gate 	int error;
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate 	/* Initialize the soft state structures */
1297c478bd9Sstevel@tonic-gate 	if ((error = ddi_soft_state_init(&bbc_beep_statep,
130c35aa225Smarx 	    sizeof (bbc_beep_state_t), 1)) != 0) {
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate 		return (error);
1337c478bd9Sstevel@tonic-gate 	}
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate 	/* Install the loadable module */
1367c478bd9Sstevel@tonic-gate 	if ((error = mod_install(&modlinkage)) != 0) {
1377c478bd9Sstevel@tonic-gate 		ddi_soft_state_fini(&bbc_beep_statep);
1387c478bd9Sstevel@tonic-gate 	}
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate 	return (error);
1417c478bd9Sstevel@tonic-gate }
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate int
_info(struct modinfo * modinfop)1457c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
1467c478bd9Sstevel@tonic-gate {
1477c478bd9Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
1487c478bd9Sstevel@tonic-gate }
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate int
_fini(void)1527c478bd9Sstevel@tonic-gate _fini(void)
1537c478bd9Sstevel@tonic-gate {
1547c478bd9Sstevel@tonic-gate 	int error;
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate 	error = mod_remove(&modlinkage);
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate 	if (error == 0) {
1597c478bd9Sstevel@tonic-gate 		/* Release per module resources */
1607c478bd9Sstevel@tonic-gate 		ddi_soft_state_fini(&bbc_beep_statep);
1617c478bd9Sstevel@tonic-gate 	}
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate 	return (error);
1647c478bd9Sstevel@tonic-gate }
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate /*
1687c478bd9Sstevel@tonic-gate  * Beep entry points
1697c478bd9Sstevel@tonic-gate  */
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate /*
1727c478bd9Sstevel@tonic-gate  * bbc_beep_attach:
1737c478bd9Sstevel@tonic-gate  */
1747c478bd9Sstevel@tonic-gate static int
bbc_beep_attach(dev_info_t * dip,ddi_attach_cmd_t cmd)1757c478bd9Sstevel@tonic-gate bbc_beep_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
1767c478bd9Sstevel@tonic-gate {
1777c478bd9Sstevel@tonic-gate 	int		instance;		/* Instance number */
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate 	/* Pointer to soft state */
1807c478bd9Sstevel@tonic-gate 	bbc_beep_state_t	*bbc_beeptr = NULL;
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate 	BBC_BEEP_DEBUG1((CE_CONT, "bbc_beep_attach: Start"));
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate 	switch (cmd) {
1857c478bd9Sstevel@tonic-gate 		case DDI_ATTACH:
1867c478bd9Sstevel@tonic-gate 			break;
1877c478bd9Sstevel@tonic-gate 		case DDI_RESUME:
1887c478bd9Sstevel@tonic-gate 			return (DDI_SUCCESS);
1897c478bd9Sstevel@tonic-gate 		default:
1907c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
1917c478bd9Sstevel@tonic-gate 	}
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate 	/* Get the instance and create soft state */
1947c478bd9Sstevel@tonic-gate 	instance = ddi_get_instance(dip);
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate 	if (ddi_soft_state_zalloc(bbc_beep_statep, instance) != 0) {
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
1997c478bd9Sstevel@tonic-gate 	}
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate 	bbc_beeptr = ddi_get_soft_state(bbc_beep_statep, instance);
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate 	if (bbc_beeptr == NULL) {
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
2067c478bd9Sstevel@tonic-gate 	}
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate 	BBC_BEEP_DEBUG1((CE_CONT, "bbc_beeptr = 0x%p, instance %x",
2097c478bd9Sstevel@tonic-gate 	    (void *)bbc_beeptr, instance));
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate 	/* Save the dip */
2127c478bd9Sstevel@tonic-gate 	bbc_beeptr->bbc_beep_dip = dip;
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate 	/* Initialize beeper mode */
2157c478bd9Sstevel@tonic-gate 	bbc_beeptr->bbc_beep_mode = BBC_BEEP_OFF;
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate 	/* Map the Beep Control and Beep counter Registers */
2187c478bd9Sstevel@tonic-gate 	if (bbc_beep_map_regs(dip, bbc_beeptr) != DDI_SUCCESS) {
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate 		BBC_BEEP_DEBUG((CE_WARN, \
221c35aa225Smarx 		    "bbc_beep_attach: Mapping of bbc registers failed."));
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate 		bbc_beep_cleanup(bbc_beeptr);
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
2267c478bd9Sstevel@tonic-gate 	}
2277c478bd9Sstevel@tonic-gate 
228c35aa225Smarx 	(void) beep_init((void *)dip, bbc_beep_on, bbc_beep_off, bbc_beep_freq);
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate 	/* Display information in the banner */
2317c478bd9Sstevel@tonic-gate 	ddi_report_dev(dip);
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate 	BBC_BEEP_DEBUG1((CE_CONT, "bbc_beep_attach: dip = 0x%p done",
2347c478bd9Sstevel@tonic-gate 	    (void *)dip));
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
2377c478bd9Sstevel@tonic-gate }
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate /*
2417c478bd9Sstevel@tonic-gate  * bbc_beep_detach:
2427c478bd9Sstevel@tonic-gate  */
2437c478bd9Sstevel@tonic-gate static int
bbc_beep_detach(dev_info_t * dip,ddi_detach_cmd_t cmd)2447c478bd9Sstevel@tonic-gate bbc_beep_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
2457c478bd9Sstevel@tonic-gate {
2467c478bd9Sstevel@tonic-gate 	/* Pointer to soft state */
2477c478bd9Sstevel@tonic-gate 	bbc_beep_state_t	*bbc_beeptr = NULL;
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate 	BBC_BEEP_DEBUG1((CE_CONT, "bbc_beep_detach: Start"));
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate 	switch (cmd) {
2527c478bd9Sstevel@tonic-gate 		case DDI_SUSPEND:
2537c478bd9Sstevel@tonic-gate 			bbc_beeptr = bbc_beep_obtain_state(dip);
2547c478bd9Sstevel@tonic-gate 
2557c478bd9Sstevel@tonic-gate 			if (bbc_beeptr == NULL) {
2567c478bd9Sstevel@tonic-gate 
2577c478bd9Sstevel@tonic-gate 				return (DDI_FAILURE);
2587c478bd9Sstevel@tonic-gate 			}
2597c478bd9Sstevel@tonic-gate 
2607c478bd9Sstevel@tonic-gate 			/*
2617c478bd9Sstevel@tonic-gate 			 * If a beep is in progress; fail suspend
2627c478bd9Sstevel@tonic-gate 			 */
2637c478bd9Sstevel@tonic-gate 			if (bbc_beeptr->bbc_beep_mode == BBC_BEEP_OFF) {
2647c478bd9Sstevel@tonic-gate 				return (DDI_SUCCESS);
2657c478bd9Sstevel@tonic-gate 			} else {
2667c478bd9Sstevel@tonic-gate 				return (DDI_FAILURE);
2677c478bd9Sstevel@tonic-gate 			}
2687c478bd9Sstevel@tonic-gate 		default:
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
2717c478bd9Sstevel@tonic-gate 	}
2727c478bd9Sstevel@tonic-gate }
2737c478bd9Sstevel@tonic-gate 
2747c478bd9Sstevel@tonic-gate 
2757c478bd9Sstevel@tonic-gate /*
2767c478bd9Sstevel@tonic-gate  * bbc_beep_info:
2777c478bd9Sstevel@tonic-gate  */
2787c478bd9Sstevel@tonic-gate /* ARGSUSED */
2797c478bd9Sstevel@tonic-gate static int
bbc_beep_info(dev_info_t * dip,ddi_info_cmd_t infocmd,void * arg,void ** result)2807c478bd9Sstevel@tonic-gate bbc_beep_info(dev_info_t *dip, ddi_info_cmd_t infocmd,
2817c478bd9Sstevel@tonic-gate 		void *arg, void **result)
2827c478bd9Sstevel@tonic-gate {
2837c478bd9Sstevel@tonic-gate 	dev_t dev;
2847c478bd9Sstevel@tonic-gate 	bbc_beep_state_t  *bbc_beeptr;
2857c478bd9Sstevel@tonic-gate 	int instance, error;
2867c478bd9Sstevel@tonic-gate 
2877c478bd9Sstevel@tonic-gate 	switch (infocmd) {
2887c478bd9Sstevel@tonic-gate 
2897c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
2907c478bd9Sstevel@tonic-gate 		dev = (dev_t)arg;
2917c478bd9Sstevel@tonic-gate 		instance = BEEP_UNIT(dev);
2927c478bd9Sstevel@tonic-gate 
2937c478bd9Sstevel@tonic-gate 		if ((bbc_beeptr = ddi_get_soft_state(bbc_beep_statep,
2947c478bd9Sstevel@tonic-gate 		    instance)) == NULL) {
2957c478bd9Sstevel@tonic-gate 
2967c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
2977c478bd9Sstevel@tonic-gate 		}
2987c478bd9Sstevel@tonic-gate 
2997c478bd9Sstevel@tonic-gate 		*result = (void *)bbc_beeptr->bbc_beep_dip;
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate 		error = DDI_SUCCESS;
3027c478bd9Sstevel@tonic-gate 		break;
3037c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
3047c478bd9Sstevel@tonic-gate 		dev = (dev_t)arg;
3057c478bd9Sstevel@tonic-gate 		instance = BEEP_UNIT(dev);
3067c478bd9Sstevel@tonic-gate 
3077c478bd9Sstevel@tonic-gate 		*result = (void *)(uintptr_t)instance;
3087c478bd9Sstevel@tonic-gate 
3097c478bd9Sstevel@tonic-gate 		error = DDI_SUCCESS;
3107c478bd9Sstevel@tonic-gate 		break;
3117c478bd9Sstevel@tonic-gate 	default:
3127c478bd9Sstevel@tonic-gate 		error = DDI_FAILURE;
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate 	}
3157c478bd9Sstevel@tonic-gate 
3167c478bd9Sstevel@tonic-gate 	return (error);
3177c478bd9Sstevel@tonic-gate }
3187c478bd9Sstevel@tonic-gate 
3197c478bd9Sstevel@tonic-gate 
3207c478bd9Sstevel@tonic-gate /*
3217c478bd9Sstevel@tonic-gate  * bbc_beep_freq() :
3227c478bd9Sstevel@tonic-gate  *	Set the frequency
3237c478bd9Sstevel@tonic-gate  */
3247c478bd9Sstevel@tonic-gate static void
bbc_beep_freq(void * arg,int freq)325c35aa225Smarx bbc_beep_freq(void *arg, int freq)
3267c478bd9Sstevel@tonic-gate {
327c35aa225Smarx 	dev_info_t *dip = (dev_info_t *)arg;
3287c478bd9Sstevel@tonic-gate 	unsigned long counter;
3297c478bd9Sstevel@tonic-gate 	int8_t beep_c2 = 0;
3307c478bd9Sstevel@tonic-gate 	int8_t beep_c3 = 0;
3317c478bd9Sstevel@tonic-gate 
3327c478bd9Sstevel@tonic-gate 	bbc_beep_state_t *bbc_beeptr = bbc_beep_obtain_state(dip);
3337c478bd9Sstevel@tonic-gate 
3347c478bd9Sstevel@tonic-gate 	/* Convert the frequency in hz to the bbc counter value */
3357c478bd9Sstevel@tonic-gate 	counter = bbc_beep_hztocounter(freq);
3367c478bd9Sstevel@tonic-gate 
3377c478bd9Sstevel@tonic-gate 	/* Extract relevant second and third byte of counter value */
3387c478bd9Sstevel@tonic-gate 	beep_c2 = (counter & 0xff00) >> 8;
3397c478bd9Sstevel@tonic-gate 	beep_c3 = (counter & 0xff0000) >> 16;
3407c478bd9Sstevel@tonic-gate 
3417c478bd9Sstevel@tonic-gate 	/*
3427c478bd9Sstevel@tonic-gate 	 * We need to write individual bytes instead of writing
3437c478bd9Sstevel@tonic-gate 	 * all of 32 bits to take care of allignment problem.
3447c478bd9Sstevel@tonic-gate 	 * Write 0 to LS 8 bits and MS 8 bits
3457c478bd9Sstevel@tonic-gate 	 * Write beep_c3 to bit 8..15 and beep_c2 to bit 16..24
3467c478bd9Sstevel@tonic-gate 	 * Little Endian format
3477c478bd9Sstevel@tonic-gate 	 */
3487c478bd9Sstevel@tonic-gate 	BEEP_WRITE_COUNTER_REG(0, 0);
3497c478bd9Sstevel@tonic-gate 	BEEP_WRITE_COUNTER_REG(1, beep_c3);
3507c478bd9Sstevel@tonic-gate 	BEEP_WRITE_COUNTER_REG(2, beep_c2);
3517c478bd9Sstevel@tonic-gate 	BEEP_WRITE_COUNTER_REG(3, 0);
3527c478bd9Sstevel@tonic-gate 
3537c478bd9Sstevel@tonic-gate 	BBC_BEEP_DEBUG1((CE_CONT,
3547c478bd9Sstevel@tonic-gate 	    "bbc_beep_freq: dip = 0x%p, freq = %d, counter = 0x%x : Done",
3557c478bd9Sstevel@tonic-gate 	    (void *)dip, freq, (int)counter));
3567c478bd9Sstevel@tonic-gate }
3577c478bd9Sstevel@tonic-gate 
3587c478bd9Sstevel@tonic-gate 
3597c478bd9Sstevel@tonic-gate /*
3607c478bd9Sstevel@tonic-gate  * bbc_beep_on() :
3617c478bd9Sstevel@tonic-gate  *	Turn the beeper on
3627c478bd9Sstevel@tonic-gate  */
3637c478bd9Sstevel@tonic-gate static void
bbc_beep_on(void * arg)364c35aa225Smarx bbc_beep_on(void *arg)
3657c478bd9Sstevel@tonic-gate {
366c35aa225Smarx 	dev_info_t *dip = (dev_info_t *)arg;
3677c478bd9Sstevel@tonic-gate 	bbc_beep_state_t *bbc_beeptr = bbc_beep_obtain_state(dip);
3687c478bd9Sstevel@tonic-gate 
3697c478bd9Sstevel@tonic-gate 	BEEP_WRITE_CTRL_REG(BBC_BEEP_ON);
3707c478bd9Sstevel@tonic-gate 
3717c478bd9Sstevel@tonic-gate 	bbc_beeptr->bbc_beep_mode = BBC_BEEP_ON;
3727c478bd9Sstevel@tonic-gate 
3737c478bd9Sstevel@tonic-gate 	BBC_BEEP_DEBUG1((CE_CONT, "bbc_beep_on: dip = 0x%p done",
3747c478bd9Sstevel@tonic-gate 	    (void *)dip));
3757c478bd9Sstevel@tonic-gate }
3767c478bd9Sstevel@tonic-gate 
3777c478bd9Sstevel@tonic-gate 
3787c478bd9Sstevel@tonic-gate /*
3797c478bd9Sstevel@tonic-gate  * bbc_beep_off() :
3807c478bd9Sstevel@tonic-gate  * 	Turn the beeper off
3817c478bd9Sstevel@tonic-gate  */
3827c478bd9Sstevel@tonic-gate static void
bbc_beep_off(void * arg)383c35aa225Smarx bbc_beep_off(void *arg)
3847c478bd9Sstevel@tonic-gate {
385c35aa225Smarx 	dev_info_t *dip = (dev_info_t *)arg;
3867c478bd9Sstevel@tonic-gate 	bbc_beep_state_t *bbc_beeptr = bbc_beep_obtain_state(dip);
3877c478bd9Sstevel@tonic-gate 
3887c478bd9Sstevel@tonic-gate 	BEEP_WRITE_CTRL_REG(BBC_BEEP_OFF);
3897c478bd9Sstevel@tonic-gate 
3907c478bd9Sstevel@tonic-gate 	bbc_beeptr->bbc_beep_mode = BBC_BEEP_OFF;
3917c478bd9Sstevel@tonic-gate 
3927c478bd9Sstevel@tonic-gate 	BBC_BEEP_DEBUG1((CE_CONT, "bbc_beep_off: dip = 0x%p done",
3937c478bd9Sstevel@tonic-gate 	    (void *)dip));
3947c478bd9Sstevel@tonic-gate }
3957c478bd9Sstevel@tonic-gate 
3967c478bd9Sstevel@tonic-gate 
3977c478bd9Sstevel@tonic-gate /*
3987c478bd9Sstevel@tonic-gate  * bbc_beep_map_regs() :
3997c478bd9Sstevel@tonic-gate  *
4007c478bd9Sstevel@tonic-gate  *	The Keyboard Beep Control Register and Keyboard Beep Counter Register
4017c478bd9Sstevel@tonic-gate  *	should be mapped into a non-cacheable portion of the  system
4027c478bd9Sstevel@tonic-gate  *	addressable space.
4037c478bd9Sstevel@tonic-gate  */
4047c478bd9Sstevel@tonic-gate static int
bbc_beep_map_regs(dev_info_t * dip,bbc_beep_state_t * bbc_beeptr)4057c478bd9Sstevel@tonic-gate bbc_beep_map_regs(dev_info_t *dip, bbc_beep_state_t *bbc_beeptr)
4067c478bd9Sstevel@tonic-gate {
4077c478bd9Sstevel@tonic-gate 	ddi_device_acc_attr_t attr;
4087c478bd9Sstevel@tonic-gate 
4097c478bd9Sstevel@tonic-gate 	BBC_BEEP_DEBUG1((CE_CONT, "bbc_beep_map_regs: Start\n"));
4107c478bd9Sstevel@tonic-gate 
4117c478bd9Sstevel@tonic-gate 	/* The host controller will be little endian */
4127c478bd9Sstevel@tonic-gate 	attr.devacc_attr_version = DDI_DEVICE_ATTR_V0;
4137c478bd9Sstevel@tonic-gate 	attr.devacc_attr_endian_flags  = DDI_STRUCTURE_LE_ACC;
4147c478bd9Sstevel@tonic-gate 	attr.devacc_attr_dataorder = DDI_STRICTORDER_ACC;
4157c478bd9Sstevel@tonic-gate 
4167c478bd9Sstevel@tonic-gate 	/* Map in operational registers */
4177c478bd9Sstevel@tonic-gate 	if (ddi_regs_map_setup(dip, 0,
418c35aa225Smarx 	    (caddr_t *)&bbc_beeptr->bbc_beep_regsp,
419c35aa225Smarx 	    0,
420c35aa225Smarx 	    sizeof (bbc_beep_regs_t),
421c35aa225Smarx 	    &attr,
422c35aa225Smarx 	    &bbc_beeptr->bbc_beep_regs_handle) != DDI_SUCCESS) {
4237c478bd9Sstevel@tonic-gate 
4247c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
4257c478bd9Sstevel@tonic-gate 	}
4267c478bd9Sstevel@tonic-gate 
4277c478bd9Sstevel@tonic-gate 	BBC_BEEP_DEBUG1((CE_CONT, "bbc_beep_map_regs: done\n"));
4287c478bd9Sstevel@tonic-gate 
4297c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
4307c478bd9Sstevel@tonic-gate }
4317c478bd9Sstevel@tonic-gate 
4327c478bd9Sstevel@tonic-gate 
4337c478bd9Sstevel@tonic-gate /*
4347c478bd9Sstevel@tonic-gate  * bbc_beep_obtain_state:
4357c478bd9Sstevel@tonic-gate  */
4367c478bd9Sstevel@tonic-gate static bbc_beep_state_t *
bbc_beep_obtain_state(dev_info_t * dip)4377c478bd9Sstevel@tonic-gate bbc_beep_obtain_state(dev_info_t *dip)
4387c478bd9Sstevel@tonic-gate {
4397c478bd9Sstevel@tonic-gate 	int instance = ddi_get_instance(dip);
4407c478bd9Sstevel@tonic-gate 
4417c478bd9Sstevel@tonic-gate 	bbc_beep_state_t *state = ddi_get_soft_state(bbc_beep_statep, instance);
4427c478bd9Sstevel@tonic-gate 
4437c478bd9Sstevel@tonic-gate 	ASSERT(state != NULL);
4447c478bd9Sstevel@tonic-gate 
4457c478bd9Sstevel@tonic-gate 	BBC_BEEP_DEBUG1((CE_CONT, "bbc_beep_obtain_state: done"));
4467c478bd9Sstevel@tonic-gate 
4477c478bd9Sstevel@tonic-gate 	return (state);
4487c478bd9Sstevel@tonic-gate }
4497c478bd9Sstevel@tonic-gate 
4507c478bd9Sstevel@tonic-gate 
4517c478bd9Sstevel@tonic-gate /*
4527c478bd9Sstevel@tonic-gate  * bbc_beep_cleanup :
4537c478bd9Sstevel@tonic-gate  *	Cleanup soft state
4547c478bd9Sstevel@tonic-gate  */
4557c478bd9Sstevel@tonic-gate static void
bbc_beep_cleanup(bbc_beep_state_t * bbc_beeptr)4567c478bd9Sstevel@tonic-gate bbc_beep_cleanup(bbc_beep_state_t *bbc_beeptr)
4577c478bd9Sstevel@tonic-gate {
4587c478bd9Sstevel@tonic-gate 	int instance = ddi_get_instance(bbc_beeptr->bbc_beep_dip);
4597c478bd9Sstevel@tonic-gate 
4607c478bd9Sstevel@tonic-gate 	ddi_soft_state_free(bbc_beep_statep, instance);
4617c478bd9Sstevel@tonic-gate 
4627c478bd9Sstevel@tonic-gate 	BBC_BEEP_DEBUG1((CE_CONT, "bbc_beep_cleanup: done"));
4637c478bd9Sstevel@tonic-gate }
4647c478bd9Sstevel@tonic-gate 
4657c478bd9Sstevel@tonic-gate 
4667c478bd9Sstevel@tonic-gate /*
4677c478bd9Sstevel@tonic-gate  * bbc_beep_hztocounter() :
4687c478bd9Sstevel@tonic-gate  *	Given a frequency in hz, find out the value to
4697c478bd9Sstevel@tonic-gate  *	be set in the Keyboard Beep Counter register
4707c478bd9Sstevel@tonic-gate  *	BBC beeper uses the following formula to calculate
4717c478bd9Sstevel@tonic-gate  * 	frequency. The formulae is :
4727c478bd9Sstevel@tonic-gate  *	frequency generated = system freq /2^(n+2)
4737c478bd9Sstevel@tonic-gate  *	Where n = position of the bit of counter register
4747c478bd9Sstevel@tonic-gate  *	that is turned on and can range between 10 to 18.
4757c478bd9Sstevel@tonic-gate  *	So in this function, the inputs are frequency generated
4767c478bd9Sstevel@tonic-gate  *	and system frequency and we need to find out n, i.e, which
4777c478bd9Sstevel@tonic-gate  *	bit to turn on.(Ref. to Section 4.2.22 of the BBC programming
4787c478bd9Sstevel@tonic-gate  *	manual).
4797c478bd9Sstevel@tonic-gate  */
4807c478bd9Sstevel@tonic-gate unsigned long
bbc_beep_hztocounter(int freq)4817c478bd9Sstevel@tonic-gate bbc_beep_hztocounter(int freq)
4827c478bd9Sstevel@tonic-gate {
4837c478bd9Sstevel@tonic-gate 	int 		i;
4847c478bd9Sstevel@tonic-gate 	unsigned long	counter;
4857c478bd9Sstevel@tonic-gate 	int 		newfreq, oldfreq;
4867c478bd9Sstevel@tonic-gate 
4877c478bd9Sstevel@tonic-gate 	int		system_freq;
4887c478bd9Sstevel@tonic-gate 
4897c478bd9Sstevel@tonic-gate 	/*
4907c478bd9Sstevel@tonic-gate 	 * Get system frequency for the root dev_info properties
4917c478bd9Sstevel@tonic-gate 	 */
4927c478bd9Sstevel@tonic-gate 	system_freq = ddi_prop_get_int(DDI_DEV_T_ANY, ddi_root_node(),
493c35aa225Smarx 	    0, "clock-frequency", 0);
4947c478bd9Sstevel@tonic-gate 
4957c478bd9Sstevel@tonic-gate 	oldfreq = 0;
4967c478bd9Sstevel@tonic-gate 
4977c478bd9Sstevel@tonic-gate 	/*
4987c478bd9Sstevel@tonic-gate 	 * Calculate frequency by turning on ith bit and
4997c478bd9Sstevel@tonic-gate 	 * matching it with the passed frequency and we do this
5007c478bd9Sstevel@tonic-gate 	 * in a loop for all the relevant bits
5017c478bd9Sstevel@tonic-gate 	 */
5027c478bd9Sstevel@tonic-gate 	for (i = BBC_BEEP_MIN_SHIFT, counter = 1 << BBC_BEEP_MSBIT;
5037c478bd9Sstevel@tonic-gate 	    i >= BBC_BEEP_MAX_SHIFT; i--, counter >>= 1) {
5047c478bd9Sstevel@tonic-gate 
5057c478bd9Sstevel@tonic-gate 		/*
5067c478bd9Sstevel@tonic-gate 		 * Calculate the frequency by dividing the system
5077c478bd9Sstevel@tonic-gate 		 * frequency by 2^i
5087c478bd9Sstevel@tonic-gate 		 */
5097c478bd9Sstevel@tonic-gate 		newfreq = system_freq >> i;
5107c478bd9Sstevel@tonic-gate 
5117c478bd9Sstevel@tonic-gate 		/*
5127c478bd9Sstevel@tonic-gate 		 * Check if we turn on the ith bit, the
5137c478bd9Sstevel@tonic-gate 		 * frequency matches exactly or not
5147c478bd9Sstevel@tonic-gate 		 */
5157c478bd9Sstevel@tonic-gate 		if (newfreq == freq) {
5167c478bd9Sstevel@tonic-gate 			/*
5177c478bd9Sstevel@tonic-gate 			 * Exact match of passed frequency with the
5187c478bd9Sstevel@tonic-gate 			 * counter value
5197c478bd9Sstevel@tonic-gate 			 */
5207c478bd9Sstevel@tonic-gate 
5217c478bd9Sstevel@tonic-gate 			return (counter);
5227c478bd9Sstevel@tonic-gate 		}
5237c478bd9Sstevel@tonic-gate 
5247c478bd9Sstevel@tonic-gate 		/*
5257c478bd9Sstevel@tonic-gate 		 * If calculated frequency is bigger
5267c478bd9Sstevel@tonic-gate 		 * return the passed frequency
5277c478bd9Sstevel@tonic-gate 		 */
5287c478bd9Sstevel@tonic-gate 		if (newfreq > freq) {
5297c478bd9Sstevel@tonic-gate 
5307c478bd9Sstevel@tonic-gate 			if (i == BBC_BEEP_MIN_SHIFT) {
5317c478bd9Sstevel@tonic-gate 				/* Input freq is less than the possible min */
5327c478bd9Sstevel@tonic-gate 
5337c478bd9Sstevel@tonic-gate 				return (counter);
5347c478bd9Sstevel@tonic-gate 			}
5357c478bd9Sstevel@tonic-gate 
5367c478bd9Sstevel@tonic-gate 			/*
5377c478bd9Sstevel@tonic-gate 			 * Find out the nearest frequency to the passed
5387c478bd9Sstevel@tonic-gate 			 * frequency by comparing the difference between
5397c478bd9Sstevel@tonic-gate 			 * the calculated frequency and the passed frequency
5407c478bd9Sstevel@tonic-gate 			 */
5417c478bd9Sstevel@tonic-gate 			if ((freq - oldfreq) > (newfreq - freq)) {
5427c478bd9Sstevel@tonic-gate 				/* Return new counter corres. to newfreq */
5437c478bd9Sstevel@tonic-gate 
5447c478bd9Sstevel@tonic-gate 				return (counter);
5457c478bd9Sstevel@tonic-gate 			}
5467c478bd9Sstevel@tonic-gate 
5477c478bd9Sstevel@tonic-gate 			/* Return old counter corresponding to oldfreq */
5487c478bd9Sstevel@tonic-gate 
5497c478bd9Sstevel@tonic-gate 			return (counter << 1);
5507c478bd9Sstevel@tonic-gate 		}
5517c478bd9Sstevel@tonic-gate 
5527c478bd9Sstevel@tonic-gate 		oldfreq = newfreq;
5537c478bd9Sstevel@tonic-gate 	}
5547c478bd9Sstevel@tonic-gate 
5557c478bd9Sstevel@tonic-gate 	/*
5567c478bd9Sstevel@tonic-gate 	 * Input freq is greater than the possible max;
5577c478bd9Sstevel@tonic-gate 	 * Back off the counter value and return max counter
5587c478bd9Sstevel@tonic-gate 	 * value possible in the register
5597c478bd9Sstevel@tonic-gate 	 */
5607c478bd9Sstevel@tonic-gate 	return (counter << 1);
5617c478bd9Sstevel@tonic-gate }
562