xref: /titanic_44/usr/src/uts/sun4u/io/grbeep.c (revision 193974072f41a843678abf5f61979c748687e66b)
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 SMBUS based beep mechanism.
297c478bd9Sstevel@tonic-gate  * The driver exports the interfaces to set frequency,
307c478bd9Sstevel@tonic-gate  * turn on beeper and turn off beeper to the generic beep
317c478bd9Sstevel@tonic-gate  * module. If a beep is in progress, the driver discards a
327c478bd9Sstevel@tonic-gate  * second beep. This driver uses the 8254 timer to program
337c478bd9Sstevel@tonic-gate  * the beeper ports.
347c478bd9Sstevel@tonic-gate  */
357c478bd9Sstevel@tonic-gate #include <sys/types.h>
367c478bd9Sstevel@tonic-gate #include <sys/conf.h>
377c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
387c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
397c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
407c478bd9Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
417c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
427c478bd9Sstevel@tonic-gate #include <sys/devops.h>
437c478bd9Sstevel@tonic-gate #include <sys/grbeep.h>
44c35aa225Smarx #include <sys/beep.h>
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate /* Pointer to the state structure */
487c478bd9Sstevel@tonic-gate static void *grbeep_statep;
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate /*
527c478bd9Sstevel@tonic-gate  * Debug stuff
537c478bd9Sstevel@tonic-gate  */
547c478bd9Sstevel@tonic-gate #ifdef DEBUG
557c478bd9Sstevel@tonic-gate int grbeep_debug = 0;
567c478bd9Sstevel@tonic-gate #define	GRBEEP_DEBUG(args)  if (grbeep_debug) cmn_err args
577c478bd9Sstevel@tonic-gate #define	GRBEEP_DEBUG1(args)  if (grbeep_debug > 1) cmn_err args
587c478bd9Sstevel@tonic-gate #else
597c478bd9Sstevel@tonic-gate #define	GRBEEP_DEBUG(args)
607c478bd9Sstevel@tonic-gate #define	GRBEEP_DEBUG1(args)
617c478bd9Sstevel@tonic-gate #endif
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate /*
657c478bd9Sstevel@tonic-gate  * Prototypes
667c478bd9Sstevel@tonic-gate  */
677c478bd9Sstevel@tonic-gate static int grbeep_attach(dev_info_t *dip, ddi_attach_cmd_t cmd);
687c478bd9Sstevel@tonic-gate static int grbeep_detach(dev_info_t *dip, ddi_detach_cmd_t cmd);
697c478bd9Sstevel@tonic-gate static int grbeep_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg,
707c478bd9Sstevel@tonic-gate 		void **result);
71c35aa225Smarx static void grbeep_freq(void *arg, int freq);
72c35aa225Smarx static void grbeep_on(void *arg);
73c35aa225Smarx static void grbeep_off(void *arg);
747c478bd9Sstevel@tonic-gate static void grbeep_cleanup(grbeep_state_t *);
757c478bd9Sstevel@tonic-gate static int grbeep_map_regs(dev_info_t *, grbeep_state_t *);
767c478bd9Sstevel@tonic-gate static grbeep_state_t *grbeep_obtain_state(dev_info_t *);
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate struct cb_ops grbeep_cb_ops = {
807c478bd9Sstevel@tonic-gate 	nulldev,	/* open  */
817c478bd9Sstevel@tonic-gate 	nulldev,	/* close */
827c478bd9Sstevel@tonic-gate 	nulldev,	/* strategy */
837c478bd9Sstevel@tonic-gate 	nulldev,	/* print */
847c478bd9Sstevel@tonic-gate 	nulldev,	/* dump */
857c478bd9Sstevel@tonic-gate 	nulldev,	/* read */
867c478bd9Sstevel@tonic-gate 	nulldev,	/* write */
877c478bd9Sstevel@tonic-gate 	nulldev,	/* ioctl */
887c478bd9Sstevel@tonic-gate 	nulldev,	/* devmap */
897c478bd9Sstevel@tonic-gate 	nulldev,	/* mmap */
907c478bd9Sstevel@tonic-gate 	nulldev,	/* segmap */
917c478bd9Sstevel@tonic-gate 	nochpoll,	/* poll */
927c478bd9Sstevel@tonic-gate 	ddi_prop_op,	/* cb_prop_op */
937c478bd9Sstevel@tonic-gate 	NULL,		/* streamtab  */
947c478bd9Sstevel@tonic-gate 	D_MP | D_NEW
957c478bd9Sstevel@tonic-gate };
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate static struct dev_ops grbeep_ops = {
997c478bd9Sstevel@tonic-gate 	DEVO_REV,		/* Devo_rev */
1007c478bd9Sstevel@tonic-gate 	0,			/* Refcnt */
1017c478bd9Sstevel@tonic-gate 	grbeep_info,		/* Info */
1027c478bd9Sstevel@tonic-gate 	nulldev,		/* Identify */
1037c478bd9Sstevel@tonic-gate 	nulldev,		/* Probe */
1047c478bd9Sstevel@tonic-gate 	grbeep_attach,		/* Attach */
1057c478bd9Sstevel@tonic-gate 	grbeep_detach,		/* Detach */
1067c478bd9Sstevel@tonic-gate 	nodev,			/* Reset */
1077c478bd9Sstevel@tonic-gate 	&grbeep_cb_ops,		/* Driver operations */
1087c478bd9Sstevel@tonic-gate 	0,			/* Bus operations */
109*19397407SSherry Moore 	NULL,			/* Power */
110*19397407SSherry Moore 	ddi_quiesce_not_supported,	/* devo_quiesce */
1117c478bd9Sstevel@tonic-gate };
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate static struct modldrv modldrv = {
1157c478bd9Sstevel@tonic-gate 	&mod_driverops, 		/* This one is a driver */
116*19397407SSherry Moore 	"SMBUS Beep Driver", 		/* Name of the module. */
1177c478bd9Sstevel@tonic-gate 	&grbeep_ops,			/* Driver ops */
1187c478bd9Sstevel@tonic-gate };
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = {
1227c478bd9Sstevel@tonic-gate 	MODREV_1, (void *)&modldrv, NULL
1237c478bd9Sstevel@tonic-gate };
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate int
_init(void)1277c478bd9Sstevel@tonic-gate _init(void)
1287c478bd9Sstevel@tonic-gate {
1297c478bd9Sstevel@tonic-gate 	int error;
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate 	/* Initialize the soft state structures */
1327c478bd9Sstevel@tonic-gate 	if ((error = ddi_soft_state_init(&grbeep_statep,
1337c478bd9Sstevel@tonic-gate 	    sizeof (grbeep_state_t), 1)) != 0) {
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate 		return (error);
1367c478bd9Sstevel@tonic-gate 	}
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate 	/* Install the loadable module */
1397c478bd9Sstevel@tonic-gate 	if ((error = mod_install(&modlinkage)) != 0) {
1407c478bd9Sstevel@tonic-gate 		ddi_soft_state_fini(&grbeep_statep);
1417c478bd9Sstevel@tonic-gate 	}
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate 	return (error);
1447c478bd9Sstevel@tonic-gate }
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate int
_info(struct modinfo * modinfop)1487c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
1497c478bd9Sstevel@tonic-gate {
1507c478bd9Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
1517c478bd9Sstevel@tonic-gate }
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate int
_fini(void)1557c478bd9Sstevel@tonic-gate _fini(void)
1567c478bd9Sstevel@tonic-gate {
1577c478bd9Sstevel@tonic-gate 	int error;
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate 	error = mod_remove(&modlinkage);
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate 	if (error == 0) {
1627c478bd9Sstevel@tonic-gate 		/* Release per module resources */
1637c478bd9Sstevel@tonic-gate 		ddi_soft_state_fini(&grbeep_statep);
1647c478bd9Sstevel@tonic-gate 	}
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate 	return (error);
1677c478bd9Sstevel@tonic-gate }
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate /*
1717c478bd9Sstevel@tonic-gate  * Beep entry points
1727c478bd9Sstevel@tonic-gate  */
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate /*
1757c478bd9Sstevel@tonic-gate  * grbeep_attach:
1767c478bd9Sstevel@tonic-gate  */
1777c478bd9Sstevel@tonic-gate static int
grbeep_attach(dev_info_t * dip,ddi_attach_cmd_t cmd)1787c478bd9Sstevel@tonic-gate grbeep_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
1797c478bd9Sstevel@tonic-gate {
1807c478bd9Sstevel@tonic-gate 	int		instance;
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate 	/* Pointer to soft state */
1837c478bd9Sstevel@tonic-gate 	grbeep_state_t	*grbeeptr = NULL;
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate 	GRBEEP_DEBUG1((CE_CONT, "grbeep_attach: Start"));
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate 	switch (cmd) {
1887c478bd9Sstevel@tonic-gate 		case DDI_ATTACH:
1897c478bd9Sstevel@tonic-gate 			break;
1907c478bd9Sstevel@tonic-gate 		case DDI_RESUME:
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate 			return (DDI_SUCCESS);
1937c478bd9Sstevel@tonic-gate 		default:
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
1967c478bd9Sstevel@tonic-gate 	}
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate 	/* Get the instance and create soft state */
1997c478bd9Sstevel@tonic-gate 	instance = ddi_get_instance(dip);
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate 	if (ddi_soft_state_zalloc(grbeep_statep, instance) != 0) {
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
2047c478bd9Sstevel@tonic-gate 	}
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate 	grbeeptr = ddi_get_soft_state(grbeep_statep, instance);
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate 	if (grbeeptr == NULL) {
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
2117c478bd9Sstevel@tonic-gate 	}
2127c478bd9Sstevel@tonic-gate 
2137c478bd9Sstevel@tonic-gate 	GRBEEP_DEBUG1((CE_CONT, "grbeeptr = 0x%p, instance %x",
2147c478bd9Sstevel@tonic-gate 	    (void *)grbeeptr, instance));
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate 	/* Save the dip */
2177c478bd9Sstevel@tonic-gate 	grbeeptr->grbeep_dip = dip;
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate 	/* Initialize beeper mode */
2207c478bd9Sstevel@tonic-gate 	grbeeptr->grbeep_mode = GRBEEP_OFF;
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate 	/* Map the Beep Control and Beep counter Registers */
2237c478bd9Sstevel@tonic-gate 	if (grbeep_map_regs(dip, grbeeptr) != DDI_SUCCESS) {
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate 		GRBEEP_DEBUG((CE_WARN,
2267c478bd9Sstevel@tonic-gate 		    "grbeep_attach: Mapping of beep registers failed."));
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate 		grbeep_cleanup(grbeeptr);
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
2317c478bd9Sstevel@tonic-gate 	}
2327c478bd9Sstevel@tonic-gate 
233c35aa225Smarx 	(void) beep_init((void *)dip, grbeep_on, grbeep_off, grbeep_freq);
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate 	/* Display information in the banner */
2367c478bd9Sstevel@tonic-gate 	ddi_report_dev(dip);
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate 	GRBEEP_DEBUG1((CE_CONT, "grbeep_attach: dip = 0x%p done",
2397c478bd9Sstevel@tonic-gate 	    (void *)dip));
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
2427c478bd9Sstevel@tonic-gate }
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate /*
2467c478bd9Sstevel@tonic-gate  * grbeep_detach:
2477c478bd9Sstevel@tonic-gate  */
2487c478bd9Sstevel@tonic-gate static int
grbeep_detach(dev_info_t * dip,ddi_detach_cmd_t cmd)2497c478bd9Sstevel@tonic-gate grbeep_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
2507c478bd9Sstevel@tonic-gate {
2517c478bd9Sstevel@tonic-gate 	/* Pointer to soft state */
2527c478bd9Sstevel@tonic-gate 	grbeep_state_t	*grbeeptr = NULL;
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate 	GRBEEP_DEBUG1((CE_CONT, "grbeep_detach: Start"));
2557c478bd9Sstevel@tonic-gate 
2567c478bd9Sstevel@tonic-gate 	switch (cmd) {
2577c478bd9Sstevel@tonic-gate 		case DDI_SUSPEND:
2587c478bd9Sstevel@tonic-gate 			grbeeptr = grbeep_obtain_state(dip);
2597c478bd9Sstevel@tonic-gate 
2607c478bd9Sstevel@tonic-gate 			if (grbeeptr == NULL) {
2617c478bd9Sstevel@tonic-gate 
2627c478bd9Sstevel@tonic-gate 				return (DDI_FAILURE);
2637c478bd9Sstevel@tonic-gate 			}
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate 			/*
2667c478bd9Sstevel@tonic-gate 			 * If a beep is in progress; fail suspend
2677c478bd9Sstevel@tonic-gate 			 */
2687c478bd9Sstevel@tonic-gate 			if (grbeeptr->grbeep_mode == GRBEEP_OFF) {
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate 				return (DDI_SUCCESS);
2717c478bd9Sstevel@tonic-gate 			} else {
2727c478bd9Sstevel@tonic-gate 
2737c478bd9Sstevel@tonic-gate 				return (DDI_FAILURE);
2747c478bd9Sstevel@tonic-gate 			}
2757c478bd9Sstevel@tonic-gate 		default:
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
2787c478bd9Sstevel@tonic-gate 	}
2797c478bd9Sstevel@tonic-gate }
2807c478bd9Sstevel@tonic-gate 
2817c478bd9Sstevel@tonic-gate 
2827c478bd9Sstevel@tonic-gate /*
2837c478bd9Sstevel@tonic-gate  * grbeep_info:
2847c478bd9Sstevel@tonic-gate  */
2857c478bd9Sstevel@tonic-gate /* ARGSUSED */
2867c478bd9Sstevel@tonic-gate static int
grbeep_info(dev_info_t * dip,ddi_info_cmd_t infocmd,void * arg,void ** result)2877c478bd9Sstevel@tonic-gate grbeep_info(dev_info_t *dip, ddi_info_cmd_t infocmd,
2887c478bd9Sstevel@tonic-gate 		void *arg, void **result)
2897c478bd9Sstevel@tonic-gate {
2907c478bd9Sstevel@tonic-gate 	dev_t dev;
2917c478bd9Sstevel@tonic-gate 	grbeep_state_t  *grbeeptr;
2927c478bd9Sstevel@tonic-gate 	int instance, error;
2937c478bd9Sstevel@tonic-gate 
2947c478bd9Sstevel@tonic-gate 	switch (infocmd) {
2957c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
2967c478bd9Sstevel@tonic-gate 		dev = (dev_t)arg;
2977c478bd9Sstevel@tonic-gate 		instance = GRBEEP_UNIT(dev);
2987c478bd9Sstevel@tonic-gate 
2997c478bd9Sstevel@tonic-gate 		if ((grbeeptr = ddi_get_soft_state(grbeep_statep,
3007c478bd9Sstevel@tonic-gate 		    instance)) == NULL) {
3017c478bd9Sstevel@tonic-gate 
3027c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
3037c478bd9Sstevel@tonic-gate 		}
3047c478bd9Sstevel@tonic-gate 
3057c478bd9Sstevel@tonic-gate 		*result = (void *)grbeeptr->grbeep_dip;
3067c478bd9Sstevel@tonic-gate 
3077c478bd9Sstevel@tonic-gate 		error = DDI_SUCCESS;
3087c478bd9Sstevel@tonic-gate 		break;
3097c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
3107c478bd9Sstevel@tonic-gate 		dev = (dev_t)arg;
3117c478bd9Sstevel@tonic-gate 		instance = GRBEEP_UNIT(dev);
3127c478bd9Sstevel@tonic-gate 
3137c478bd9Sstevel@tonic-gate 		*result = (void *)(uintptr_t)instance;
3147c478bd9Sstevel@tonic-gate 
3157c478bd9Sstevel@tonic-gate 		error = DDI_SUCCESS;
3167c478bd9Sstevel@tonic-gate 		break;
3177c478bd9Sstevel@tonic-gate 	default:
3187c478bd9Sstevel@tonic-gate 		error = DDI_FAILURE;
3197c478bd9Sstevel@tonic-gate 
3207c478bd9Sstevel@tonic-gate 	}
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate 	return (error);
3237c478bd9Sstevel@tonic-gate }
3247c478bd9Sstevel@tonic-gate 
3257c478bd9Sstevel@tonic-gate 
3267c478bd9Sstevel@tonic-gate /*
3277c478bd9Sstevel@tonic-gate  * grbeep_freq() :
3287c478bd9Sstevel@tonic-gate  * 	Set beep frequency
3297c478bd9Sstevel@tonic-gate  */
3307c478bd9Sstevel@tonic-gate static void
grbeep_freq(void * arg,int freq)331c35aa225Smarx grbeep_freq(void *arg, int freq)
3327c478bd9Sstevel@tonic-gate {
333c35aa225Smarx 	dev_info_t *dip = (dev_info_t *)arg;
3347c478bd9Sstevel@tonic-gate 	grbeep_state_t *grbeeptr = grbeep_obtain_state(dip);
3357c478bd9Sstevel@tonic-gate 	int divisor = 0;
3367c478bd9Sstevel@tonic-gate 
3377c478bd9Sstevel@tonic-gate 	ASSERT(freq != 0);
3387c478bd9Sstevel@tonic-gate 
3397c478bd9Sstevel@tonic-gate 	GRBEEP_DEBUG1((CE_CONT, "grbeep_freq: dip=0x%p freq=%d mode=%d",
3407c478bd9Sstevel@tonic-gate 	    (void *)dip, freq, grbeeptr->grbeep_mode));
3417c478bd9Sstevel@tonic-gate 
3427c478bd9Sstevel@tonic-gate 	GRBEEP_WRITE_FREQ_CONTROL_REG(GRBEEP_CONTROL);
3437c478bd9Sstevel@tonic-gate 
3447c478bd9Sstevel@tonic-gate 	divisor = GRBEEP_INPUT_FREQ / freq;
3457c478bd9Sstevel@tonic-gate 
3467c478bd9Sstevel@tonic-gate 	if (divisor > GRBEEP_DIVISOR_MAX) {
3477c478bd9Sstevel@tonic-gate 		divisor = GRBEEP_DIVISOR_MAX;
3487c478bd9Sstevel@tonic-gate 	} else if (divisor < GRBEEP_DIVISOR_MIN) {
3497c478bd9Sstevel@tonic-gate 		divisor = GRBEEP_DIVISOR_MIN;
3507c478bd9Sstevel@tonic-gate 	}
3517c478bd9Sstevel@tonic-gate 
3527c478bd9Sstevel@tonic-gate 	GRBEEP_DEBUG1((CE_CONT, "grbeep_freq: first=0x%x second=0x%x",
3537c478bd9Sstevel@tonic-gate 	    (divisor & 0xff), ((divisor & 0xff00) >> 8)));
3547c478bd9Sstevel@tonic-gate 
3557c478bd9Sstevel@tonic-gate 	GRBEEP_WRITE_FREQ_DIVISOR_REG(divisor & 0xff);
3567c478bd9Sstevel@tonic-gate 	GRBEEP_WRITE_FREQ_DIVISOR_REG((divisor & 0xff00) >> 8);
3577c478bd9Sstevel@tonic-gate }
3587c478bd9Sstevel@tonic-gate 
3597c478bd9Sstevel@tonic-gate 
3607c478bd9Sstevel@tonic-gate /*
3617c478bd9Sstevel@tonic-gate  * grbeep_on() :
3627c478bd9Sstevel@tonic-gate  *	Turn the beeper on
3637c478bd9Sstevel@tonic-gate  */
3647c478bd9Sstevel@tonic-gate static void
grbeep_on(void * arg)365c35aa225Smarx grbeep_on(void *arg)
3667c478bd9Sstevel@tonic-gate {
367c35aa225Smarx 	dev_info_t *dip = (dev_info_t *)arg;
3687c478bd9Sstevel@tonic-gate 	grbeep_state_t *grbeeptr = grbeep_obtain_state(dip);
3697c478bd9Sstevel@tonic-gate 
3707c478bd9Sstevel@tonic-gate 	GRBEEP_DEBUG1((CE_CONT, "grbeep_on: dip = 0x%p mode=%d",
3717c478bd9Sstevel@tonic-gate 	    (void *)dip, grbeeptr->grbeep_mode));
3727c478bd9Sstevel@tonic-gate 
3737c478bd9Sstevel@tonic-gate 	if (grbeeptr->grbeep_mode == GRBEEP_OFF) {
3747c478bd9Sstevel@tonic-gate 
3757c478bd9Sstevel@tonic-gate 		grbeeptr->grbeep_mode = GRBEEP_ON;
3767c478bd9Sstevel@tonic-gate 		GRBEEP_DEBUG1((CE_CONT, "grbeep_on: Starting beep"));
3777c478bd9Sstevel@tonic-gate 		GRBEEP_WRITE_START_STOP_REG(GRBEEP_START);
3787c478bd9Sstevel@tonic-gate 
3797c478bd9Sstevel@tonic-gate 	}
3807c478bd9Sstevel@tonic-gate 
3817c478bd9Sstevel@tonic-gate 	GRBEEP_DEBUG1((CE_CONT, "grbeep_on: dip = 0x%p done", (void *)dip));
3827c478bd9Sstevel@tonic-gate }
3837c478bd9Sstevel@tonic-gate 
3847c478bd9Sstevel@tonic-gate 
3857c478bd9Sstevel@tonic-gate /*
3867c478bd9Sstevel@tonic-gate  * grbeep_off() :
3877c478bd9Sstevel@tonic-gate  * 	Turn the beeper off
3887c478bd9Sstevel@tonic-gate  */
3897c478bd9Sstevel@tonic-gate static void
grbeep_off(void * arg)390c35aa225Smarx grbeep_off(void *arg)
3917c478bd9Sstevel@tonic-gate {
392c35aa225Smarx 	dev_info_t *dip = (dev_info_t *)arg;
3937c478bd9Sstevel@tonic-gate 	grbeep_state_t *grbeeptr = grbeep_obtain_state(dip);
3947c478bd9Sstevel@tonic-gate 
3957c478bd9Sstevel@tonic-gate 	GRBEEP_DEBUG1((CE_CONT, "grbeep_off: dip = 0x%p mode=%d",
3967c478bd9Sstevel@tonic-gate 	    (void *)dip, grbeeptr->grbeep_mode));
3977c478bd9Sstevel@tonic-gate 
3987c478bd9Sstevel@tonic-gate 	if (grbeeptr->grbeep_mode == GRBEEP_ON) {
3997c478bd9Sstevel@tonic-gate 
4007c478bd9Sstevel@tonic-gate 		grbeeptr->grbeep_mode = GRBEEP_OFF;
4017c478bd9Sstevel@tonic-gate 		GRBEEP_DEBUG1((CE_CONT, "grbeep_off: Stopping beep"));
4027c478bd9Sstevel@tonic-gate 		GRBEEP_WRITE_START_STOP_REG(GRBEEP_STOP);
4037c478bd9Sstevel@tonic-gate 
4047c478bd9Sstevel@tonic-gate 	}
4057c478bd9Sstevel@tonic-gate 
4067c478bd9Sstevel@tonic-gate 	GRBEEP_DEBUG1((CE_CONT, "grbeep_off: dip = 0x%p done", (void *)dip));
4077c478bd9Sstevel@tonic-gate }
4087c478bd9Sstevel@tonic-gate 
4097c478bd9Sstevel@tonic-gate /*
4107c478bd9Sstevel@tonic-gate  * grbeep_map_regs() :
4117c478bd9Sstevel@tonic-gate  *
4127c478bd9Sstevel@tonic-gate  *	The write beep port register and spkr control register
4137c478bd9Sstevel@tonic-gate  *	should be mapped into a non-cacheable portion of the  system
4147c478bd9Sstevel@tonic-gate  *	addressable space.
4157c478bd9Sstevel@tonic-gate  */
4167c478bd9Sstevel@tonic-gate static int
grbeep_map_regs(dev_info_t * dip,grbeep_state_t * grbeeptr)4177c478bd9Sstevel@tonic-gate grbeep_map_regs(dev_info_t *dip, grbeep_state_t *grbeeptr)
4187c478bd9Sstevel@tonic-gate {
4197c478bd9Sstevel@tonic-gate 	ddi_device_acc_attr_t attr;
4207c478bd9Sstevel@tonic-gate 
4217c478bd9Sstevel@tonic-gate 	GRBEEP_DEBUG1((CE_CONT, "grbeep_map_regs: Start"));
4227c478bd9Sstevel@tonic-gate 
4237c478bd9Sstevel@tonic-gate 	/* The host controller will be little endian */
4247c478bd9Sstevel@tonic-gate 	attr.devacc_attr_version = DDI_DEVICE_ATTR_V0;
4257c478bd9Sstevel@tonic-gate 	attr.devacc_attr_endian_flags  = DDI_STRUCTURE_LE_ACC;
4267c478bd9Sstevel@tonic-gate 	attr.devacc_attr_dataorder = DDI_STRICTORDER_ACC;
4277c478bd9Sstevel@tonic-gate 
4287c478bd9Sstevel@tonic-gate 	/* Map in operational registers */
4297c478bd9Sstevel@tonic-gate 	if (ddi_regs_map_setup(dip, 2,
4307c478bd9Sstevel@tonic-gate 	    (caddr_t *)&grbeeptr->grbeep_freq_regs,
4317c478bd9Sstevel@tonic-gate 	    0,
4327c478bd9Sstevel@tonic-gate 	    sizeof (grbeep_freq_regs_t),
4337c478bd9Sstevel@tonic-gate 	    &attr,
4347c478bd9Sstevel@tonic-gate 	    &grbeeptr->grbeep_freq_regs_handle)
4357c478bd9Sstevel@tonic-gate 	    != DDI_SUCCESS) {
4367c478bd9Sstevel@tonic-gate 
4377c478bd9Sstevel@tonic-gate 		GRBEEP_DEBUG((CE_CONT, "grbeep_map_regs: Failed to map"));
4387c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
4397c478bd9Sstevel@tonic-gate 	}
4407c478bd9Sstevel@tonic-gate 
4417c478bd9Sstevel@tonic-gate 	/* Map in operational registers */
4427c478bd9Sstevel@tonic-gate 	if (ddi_regs_map_setup(dip, 3,
4437c478bd9Sstevel@tonic-gate 	    (caddr_t *)&grbeeptr->grbeep_start_stop_reg,
4447c478bd9Sstevel@tonic-gate 	    0,
4457c478bd9Sstevel@tonic-gate 	    1,
4467c478bd9Sstevel@tonic-gate 	    &attr,
4477c478bd9Sstevel@tonic-gate 	    &grbeeptr->grbeep_start_stop_reg_handle)
4487c478bd9Sstevel@tonic-gate 	    != DDI_SUCCESS) {
4497c478bd9Sstevel@tonic-gate 
4507c478bd9Sstevel@tonic-gate 		GRBEEP_DEBUG((CE_CONT, "grbeep_map_regs: Failed to map"));
4517c478bd9Sstevel@tonic-gate 		ddi_regs_map_free((void *)&grbeeptr->grbeep_freq_regs_handle);
4527c478bd9Sstevel@tonic-gate 
4537c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
4547c478bd9Sstevel@tonic-gate 	}
4557c478bd9Sstevel@tonic-gate 
4567c478bd9Sstevel@tonic-gate 	GRBEEP_DEBUG1((CE_CONT, "grbeep_map_regs: done"));
4577c478bd9Sstevel@tonic-gate 
4587c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
4597c478bd9Sstevel@tonic-gate }
4607c478bd9Sstevel@tonic-gate 
4617c478bd9Sstevel@tonic-gate 
4627c478bd9Sstevel@tonic-gate /*
4637c478bd9Sstevel@tonic-gate  * grbeep_obtain_state:
4647c478bd9Sstevel@tonic-gate  */
4657c478bd9Sstevel@tonic-gate static grbeep_state_t *
grbeep_obtain_state(dev_info_t * dip)4667c478bd9Sstevel@tonic-gate grbeep_obtain_state(dev_info_t *dip)
4677c478bd9Sstevel@tonic-gate {
4687c478bd9Sstevel@tonic-gate 	int instance = ddi_get_instance(dip);
4697c478bd9Sstevel@tonic-gate 
4707c478bd9Sstevel@tonic-gate 	grbeep_state_t *state = ddi_get_soft_state(grbeep_statep, instance);
4717c478bd9Sstevel@tonic-gate 
4727c478bd9Sstevel@tonic-gate 	ASSERT(state != NULL);
4737c478bd9Sstevel@tonic-gate 
4747c478bd9Sstevel@tonic-gate 	GRBEEP_DEBUG1((CE_CONT, "grbeep_obtain_state: done"));
4757c478bd9Sstevel@tonic-gate 
4767c478bd9Sstevel@tonic-gate 	return (state);
4777c478bd9Sstevel@tonic-gate }
4787c478bd9Sstevel@tonic-gate 
4797c478bd9Sstevel@tonic-gate 
4807c478bd9Sstevel@tonic-gate /*
4817c478bd9Sstevel@tonic-gate  * grbeep_cleanup :
4827c478bd9Sstevel@tonic-gate  *	Cleanup soft state
4837c478bd9Sstevel@tonic-gate  */
4847c478bd9Sstevel@tonic-gate static void
grbeep_cleanup(grbeep_state_t * grbeeptr)4857c478bd9Sstevel@tonic-gate grbeep_cleanup(grbeep_state_t *grbeeptr)
4867c478bd9Sstevel@tonic-gate {
4877c478bd9Sstevel@tonic-gate 	int instance = ddi_get_instance(grbeeptr->grbeep_dip);
4887c478bd9Sstevel@tonic-gate 
4897c478bd9Sstevel@tonic-gate 	ddi_soft_state_free(grbeep_statep, instance);
4907c478bd9Sstevel@tonic-gate 
4917c478bd9Sstevel@tonic-gate 	GRBEEP_DEBUG1((CE_CONT, "grbeep_cleanup: done"));
4927c478bd9Sstevel@tonic-gate }
493