1*0Sstevel@tonic-gate /* 2*0Sstevel@tonic-gate * CDDL HEADER START 3*0Sstevel@tonic-gate * 4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*0Sstevel@tonic-gate * with the License. 8*0Sstevel@tonic-gate * 9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*0Sstevel@tonic-gate * See the License for the specific language governing permissions 12*0Sstevel@tonic-gate * and limitations under the License. 13*0Sstevel@tonic-gate * 14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*0Sstevel@tonic-gate * 20*0Sstevel@tonic-gate * CDDL HEADER END 21*0Sstevel@tonic-gate */ 22*0Sstevel@tonic-gate /* 23*0Sstevel@tonic-gate * Copyright 1999-2000, 2002 Sun Microsystems, Inc. All rights reserved. 24*0Sstevel@tonic-gate * Use is subject to license terms. 25*0Sstevel@tonic-gate */ 26*0Sstevel@tonic-gate 27*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 28*0Sstevel@tonic-gate 29*0Sstevel@tonic-gate /* 30*0Sstevel@tonic-gate * This is the Beep driver for bbc based beep mechanism. 31*0Sstevel@tonic-gate * 32*0Sstevel@tonic-gate */ 33*0Sstevel@tonic-gate #include <sys/types.h> 34*0Sstevel@tonic-gate #include <sys/conf.h> 35*0Sstevel@tonic-gate #include <sys/ddi.h> 36*0Sstevel@tonic-gate #include <sys/sunddi.h> 37*0Sstevel@tonic-gate #include <sys/modctl.h> 38*0Sstevel@tonic-gate #include <sys/ddi_impldefs.h> 39*0Sstevel@tonic-gate #include <sys/kmem.h> 40*0Sstevel@tonic-gate #include <sys/devops.h> 41*0Sstevel@tonic-gate #include <sys/bbc_beep.h> 42*0Sstevel@tonic-gate #include <sys/beep_driver.h> 43*0Sstevel@tonic-gate 44*0Sstevel@tonic-gate 45*0Sstevel@tonic-gate /* Pointer to the state structure */ 46*0Sstevel@tonic-gate static void *bbc_beep_statep; 47*0Sstevel@tonic-gate 48*0Sstevel@tonic-gate 49*0Sstevel@tonic-gate /* 50*0Sstevel@tonic-gate * Debug stuff 51*0Sstevel@tonic-gate */ 52*0Sstevel@tonic-gate #ifdef DEBUG 53*0Sstevel@tonic-gate int bbc_beep_debug = 0; 54*0Sstevel@tonic-gate #define BBC_BEEP_DEBUG(args) if (bbc_beep_debug) cmn_err args 55*0Sstevel@tonic-gate #define BBC_BEEP_DEBUG1(args) if (bbc_beep_debug > 1) cmn_err args 56*0Sstevel@tonic-gate #else 57*0Sstevel@tonic-gate #define BBC_BEEP_DEBUG(args) 58*0Sstevel@tonic-gate #define BBC_BEEP_DEBUG1(args) 59*0Sstevel@tonic-gate #endif 60*0Sstevel@tonic-gate 61*0Sstevel@tonic-gate 62*0Sstevel@tonic-gate /* 63*0Sstevel@tonic-gate * Prototypes 64*0Sstevel@tonic-gate */ 65*0Sstevel@tonic-gate static int bbc_beep_attach(dev_info_t *dip, ddi_attach_cmd_t cmd); 66*0Sstevel@tonic-gate static int bbc_beep_detach(dev_info_t *dip, ddi_detach_cmd_t cmd); 67*0Sstevel@tonic-gate static int bbc_beep_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, 68*0Sstevel@tonic-gate void **result); 69*0Sstevel@tonic-gate static void bbc_beep_freq(dev_info_t *, int); 70*0Sstevel@tonic-gate static void bbc_beep_on(dev_info_t *); 71*0Sstevel@tonic-gate static void bbc_beep_off(dev_info_t *); 72*0Sstevel@tonic-gate static void bbc_beep_cleanup(bbc_beep_state_t *); 73*0Sstevel@tonic-gate static int bbc_beep_map_regs(dev_info_t *, bbc_beep_state_t *); 74*0Sstevel@tonic-gate static bbc_beep_state_t *bbc_beep_obtain_state(dev_info_t *); 75*0Sstevel@tonic-gate static unsigned long bbc_beep_hztocounter(int); 76*0Sstevel@tonic-gate 77*0Sstevel@tonic-gate 78*0Sstevel@tonic-gate struct cb_ops bbc_beep_cb_ops = { 79*0Sstevel@tonic-gate nulldev, /* open */ 80*0Sstevel@tonic-gate nulldev, /* close */ 81*0Sstevel@tonic-gate nulldev, /* strategy */ 82*0Sstevel@tonic-gate nulldev, /* print */ 83*0Sstevel@tonic-gate nulldev, /* dump */ 84*0Sstevel@tonic-gate nulldev, /* read */ 85*0Sstevel@tonic-gate nulldev, /* write */ 86*0Sstevel@tonic-gate nulldev, /* ioctl */ 87*0Sstevel@tonic-gate nulldev, /* devmap */ 88*0Sstevel@tonic-gate nulldev, /* mmap */ 89*0Sstevel@tonic-gate nulldev, /* segmap */ 90*0Sstevel@tonic-gate nochpoll, /* poll */ 91*0Sstevel@tonic-gate ddi_prop_op, /* cb_prop_op */ 92*0Sstevel@tonic-gate NULL, /* streamtab */ 93*0Sstevel@tonic-gate D_64BIT | D_MP | D_NEW| D_HOTPLUG 94*0Sstevel@tonic-gate }; 95*0Sstevel@tonic-gate 96*0Sstevel@tonic-gate 97*0Sstevel@tonic-gate static struct dev_ops bbc_beep_ops = { 98*0Sstevel@tonic-gate DEVO_REV, /* Devo_rev */ 99*0Sstevel@tonic-gate 0, /* Refcnt */ 100*0Sstevel@tonic-gate bbc_beep_info, /* Info */ 101*0Sstevel@tonic-gate nulldev, /* Identify */ 102*0Sstevel@tonic-gate nulldev, /* Probe */ 103*0Sstevel@tonic-gate bbc_beep_attach, /* Attach */ 104*0Sstevel@tonic-gate bbc_beep_detach, /* Detach */ 105*0Sstevel@tonic-gate nodev, /* Reset */ 106*0Sstevel@tonic-gate &bbc_beep_cb_ops, /* Driver operations */ 107*0Sstevel@tonic-gate 0, /* Bus operations */ 108*0Sstevel@tonic-gate ddi_power /* Power */ 109*0Sstevel@tonic-gate }; 110*0Sstevel@tonic-gate 111*0Sstevel@tonic-gate 112*0Sstevel@tonic-gate static struct modldrv modldrv = { 113*0Sstevel@tonic-gate &mod_driverops, /* This one is a driver */ 114*0Sstevel@tonic-gate "BBC Beep Driver %I%", /* Name of the module. */ 115*0Sstevel@tonic-gate &bbc_beep_ops, /* Driver ops */ 116*0Sstevel@tonic-gate }; 117*0Sstevel@tonic-gate 118*0Sstevel@tonic-gate 119*0Sstevel@tonic-gate static struct modlinkage modlinkage = { 120*0Sstevel@tonic-gate MODREV_1, (void *)&modldrv, NULL 121*0Sstevel@tonic-gate }; 122*0Sstevel@tonic-gate 123*0Sstevel@tonic-gate 124*0Sstevel@tonic-gate int 125*0Sstevel@tonic-gate _init(void) 126*0Sstevel@tonic-gate { 127*0Sstevel@tonic-gate int error; 128*0Sstevel@tonic-gate 129*0Sstevel@tonic-gate /* Initialize the soft state structures */ 130*0Sstevel@tonic-gate if ((error = ddi_soft_state_init(&bbc_beep_statep, 131*0Sstevel@tonic-gate sizeof (bbc_beep_state_t), 1)) != 0) { 132*0Sstevel@tonic-gate 133*0Sstevel@tonic-gate return (error); 134*0Sstevel@tonic-gate } 135*0Sstevel@tonic-gate 136*0Sstevel@tonic-gate /* Install the loadable module */ 137*0Sstevel@tonic-gate if ((error = mod_install(&modlinkage)) != 0) { 138*0Sstevel@tonic-gate ddi_soft_state_fini(&bbc_beep_statep); 139*0Sstevel@tonic-gate } 140*0Sstevel@tonic-gate 141*0Sstevel@tonic-gate return (error); 142*0Sstevel@tonic-gate } 143*0Sstevel@tonic-gate 144*0Sstevel@tonic-gate 145*0Sstevel@tonic-gate int 146*0Sstevel@tonic-gate _info(struct modinfo *modinfop) 147*0Sstevel@tonic-gate { 148*0Sstevel@tonic-gate return (mod_info(&modlinkage, modinfop)); 149*0Sstevel@tonic-gate } 150*0Sstevel@tonic-gate 151*0Sstevel@tonic-gate 152*0Sstevel@tonic-gate int 153*0Sstevel@tonic-gate _fini(void) 154*0Sstevel@tonic-gate { 155*0Sstevel@tonic-gate int error; 156*0Sstevel@tonic-gate 157*0Sstevel@tonic-gate error = mod_remove(&modlinkage); 158*0Sstevel@tonic-gate 159*0Sstevel@tonic-gate if (error == 0) { 160*0Sstevel@tonic-gate /* Release per module resources */ 161*0Sstevel@tonic-gate ddi_soft_state_fini(&bbc_beep_statep); 162*0Sstevel@tonic-gate } 163*0Sstevel@tonic-gate 164*0Sstevel@tonic-gate return (error); 165*0Sstevel@tonic-gate } 166*0Sstevel@tonic-gate 167*0Sstevel@tonic-gate 168*0Sstevel@tonic-gate /* 169*0Sstevel@tonic-gate * Beep entry points 170*0Sstevel@tonic-gate */ 171*0Sstevel@tonic-gate 172*0Sstevel@tonic-gate /* 173*0Sstevel@tonic-gate * bbc_beep_attach: 174*0Sstevel@tonic-gate */ 175*0Sstevel@tonic-gate static int 176*0Sstevel@tonic-gate bbc_beep_attach(dev_info_t *dip, ddi_attach_cmd_t cmd) 177*0Sstevel@tonic-gate { 178*0Sstevel@tonic-gate int instance; /* Instance number */ 179*0Sstevel@tonic-gate 180*0Sstevel@tonic-gate /* Pointer to soft state */ 181*0Sstevel@tonic-gate bbc_beep_state_t *bbc_beeptr = NULL; 182*0Sstevel@tonic-gate 183*0Sstevel@tonic-gate BBC_BEEP_DEBUG1((CE_CONT, "bbc_beep_attach: Start")); 184*0Sstevel@tonic-gate 185*0Sstevel@tonic-gate switch (cmd) { 186*0Sstevel@tonic-gate case DDI_ATTACH: 187*0Sstevel@tonic-gate break; 188*0Sstevel@tonic-gate case DDI_RESUME: 189*0Sstevel@tonic-gate return (DDI_SUCCESS); 190*0Sstevel@tonic-gate default: 191*0Sstevel@tonic-gate return (DDI_FAILURE); 192*0Sstevel@tonic-gate } 193*0Sstevel@tonic-gate 194*0Sstevel@tonic-gate /* Get the instance and create soft state */ 195*0Sstevel@tonic-gate instance = ddi_get_instance(dip); 196*0Sstevel@tonic-gate 197*0Sstevel@tonic-gate if (ddi_soft_state_zalloc(bbc_beep_statep, instance) != 0) { 198*0Sstevel@tonic-gate 199*0Sstevel@tonic-gate return (DDI_FAILURE); 200*0Sstevel@tonic-gate } 201*0Sstevel@tonic-gate 202*0Sstevel@tonic-gate bbc_beeptr = ddi_get_soft_state(bbc_beep_statep, instance); 203*0Sstevel@tonic-gate 204*0Sstevel@tonic-gate if (bbc_beeptr == NULL) { 205*0Sstevel@tonic-gate 206*0Sstevel@tonic-gate return (DDI_FAILURE); 207*0Sstevel@tonic-gate } 208*0Sstevel@tonic-gate 209*0Sstevel@tonic-gate BBC_BEEP_DEBUG1((CE_CONT, "bbc_beeptr = 0x%p, instance %x", 210*0Sstevel@tonic-gate (void *)bbc_beeptr, instance)); 211*0Sstevel@tonic-gate 212*0Sstevel@tonic-gate /* Save the dip */ 213*0Sstevel@tonic-gate bbc_beeptr->bbc_beep_dip = dip; 214*0Sstevel@tonic-gate 215*0Sstevel@tonic-gate /* Initialize beeper mode */ 216*0Sstevel@tonic-gate bbc_beeptr->bbc_beep_mode = BBC_BEEP_OFF; 217*0Sstevel@tonic-gate 218*0Sstevel@tonic-gate /* Map the Beep Control and Beep counter Registers */ 219*0Sstevel@tonic-gate if (bbc_beep_map_regs(dip, bbc_beeptr) != DDI_SUCCESS) { 220*0Sstevel@tonic-gate 221*0Sstevel@tonic-gate BBC_BEEP_DEBUG((CE_WARN, \ 222*0Sstevel@tonic-gate "bbc_beep_attach: Mapping of bbc registers failed.")); 223*0Sstevel@tonic-gate 224*0Sstevel@tonic-gate bbc_beep_cleanup(bbc_beeptr); 225*0Sstevel@tonic-gate 226*0Sstevel@tonic-gate return (DDI_FAILURE); 227*0Sstevel@tonic-gate } 228*0Sstevel@tonic-gate 229*0Sstevel@tonic-gate (void) beep_init(dip, bbc_beep_on, bbc_beep_off, bbc_beep_freq); 230*0Sstevel@tonic-gate 231*0Sstevel@tonic-gate /* Display information in the banner */ 232*0Sstevel@tonic-gate ddi_report_dev(dip); 233*0Sstevel@tonic-gate 234*0Sstevel@tonic-gate BBC_BEEP_DEBUG1((CE_CONT, "bbc_beep_attach: dip = 0x%p done", 235*0Sstevel@tonic-gate (void *)dip)); 236*0Sstevel@tonic-gate 237*0Sstevel@tonic-gate return (DDI_SUCCESS); 238*0Sstevel@tonic-gate } 239*0Sstevel@tonic-gate 240*0Sstevel@tonic-gate 241*0Sstevel@tonic-gate /* 242*0Sstevel@tonic-gate * bbc_beep_detach: 243*0Sstevel@tonic-gate */ 244*0Sstevel@tonic-gate /* ARGSUSED */ 245*0Sstevel@tonic-gate static int 246*0Sstevel@tonic-gate bbc_beep_detach(dev_info_t *dip, ddi_detach_cmd_t cmd) 247*0Sstevel@tonic-gate { 248*0Sstevel@tonic-gate /* Pointer to soft state */ 249*0Sstevel@tonic-gate bbc_beep_state_t *bbc_beeptr = NULL; 250*0Sstevel@tonic-gate 251*0Sstevel@tonic-gate BBC_BEEP_DEBUG1((CE_CONT, "bbc_beep_detach: Start")); 252*0Sstevel@tonic-gate 253*0Sstevel@tonic-gate switch (cmd) { 254*0Sstevel@tonic-gate case DDI_SUSPEND: 255*0Sstevel@tonic-gate bbc_beeptr = bbc_beep_obtain_state(dip); 256*0Sstevel@tonic-gate 257*0Sstevel@tonic-gate if (bbc_beeptr == NULL) { 258*0Sstevel@tonic-gate 259*0Sstevel@tonic-gate return (DDI_FAILURE); 260*0Sstevel@tonic-gate } 261*0Sstevel@tonic-gate 262*0Sstevel@tonic-gate /* 263*0Sstevel@tonic-gate * If a beep is in progress; fail suspend 264*0Sstevel@tonic-gate */ 265*0Sstevel@tonic-gate if (bbc_beeptr->bbc_beep_mode == BBC_BEEP_OFF) { 266*0Sstevel@tonic-gate return (DDI_SUCCESS); 267*0Sstevel@tonic-gate } else { 268*0Sstevel@tonic-gate return (DDI_FAILURE); 269*0Sstevel@tonic-gate } 270*0Sstevel@tonic-gate default: 271*0Sstevel@tonic-gate 272*0Sstevel@tonic-gate return (DDI_FAILURE); 273*0Sstevel@tonic-gate } 274*0Sstevel@tonic-gate } 275*0Sstevel@tonic-gate 276*0Sstevel@tonic-gate 277*0Sstevel@tonic-gate /* 278*0Sstevel@tonic-gate * bbc_beep_info: 279*0Sstevel@tonic-gate */ 280*0Sstevel@tonic-gate /* ARGSUSED */ 281*0Sstevel@tonic-gate static int 282*0Sstevel@tonic-gate bbc_beep_info(dev_info_t *dip, ddi_info_cmd_t infocmd, 283*0Sstevel@tonic-gate void *arg, void **result) 284*0Sstevel@tonic-gate { 285*0Sstevel@tonic-gate dev_t dev; 286*0Sstevel@tonic-gate bbc_beep_state_t *bbc_beeptr; 287*0Sstevel@tonic-gate int instance, error; 288*0Sstevel@tonic-gate 289*0Sstevel@tonic-gate switch (infocmd) { 290*0Sstevel@tonic-gate 291*0Sstevel@tonic-gate case DDI_INFO_DEVT2DEVINFO: 292*0Sstevel@tonic-gate dev = (dev_t)arg; 293*0Sstevel@tonic-gate instance = BEEP_UNIT(dev); 294*0Sstevel@tonic-gate 295*0Sstevel@tonic-gate if ((bbc_beeptr = ddi_get_soft_state(bbc_beep_statep, 296*0Sstevel@tonic-gate instance)) == NULL) { 297*0Sstevel@tonic-gate 298*0Sstevel@tonic-gate return (DDI_FAILURE); 299*0Sstevel@tonic-gate } 300*0Sstevel@tonic-gate 301*0Sstevel@tonic-gate *result = (void *)bbc_beeptr->bbc_beep_dip; 302*0Sstevel@tonic-gate 303*0Sstevel@tonic-gate error = DDI_SUCCESS; 304*0Sstevel@tonic-gate break; 305*0Sstevel@tonic-gate case DDI_INFO_DEVT2INSTANCE: 306*0Sstevel@tonic-gate dev = (dev_t)arg; 307*0Sstevel@tonic-gate instance = BEEP_UNIT(dev); 308*0Sstevel@tonic-gate 309*0Sstevel@tonic-gate *result = (void *)(uintptr_t)instance; 310*0Sstevel@tonic-gate 311*0Sstevel@tonic-gate error = DDI_SUCCESS; 312*0Sstevel@tonic-gate break; 313*0Sstevel@tonic-gate default: 314*0Sstevel@tonic-gate error = DDI_FAILURE; 315*0Sstevel@tonic-gate 316*0Sstevel@tonic-gate } 317*0Sstevel@tonic-gate 318*0Sstevel@tonic-gate return (error); 319*0Sstevel@tonic-gate } 320*0Sstevel@tonic-gate 321*0Sstevel@tonic-gate 322*0Sstevel@tonic-gate /* 323*0Sstevel@tonic-gate * bbc_beep_freq() : 324*0Sstevel@tonic-gate * Set the frequency 325*0Sstevel@tonic-gate */ 326*0Sstevel@tonic-gate static void 327*0Sstevel@tonic-gate bbc_beep_freq(dev_info_t *dip, int freq) 328*0Sstevel@tonic-gate { 329*0Sstevel@tonic-gate unsigned long counter; 330*0Sstevel@tonic-gate int8_t beep_c2 = 0; 331*0Sstevel@tonic-gate int8_t beep_c3 = 0; 332*0Sstevel@tonic-gate 333*0Sstevel@tonic-gate bbc_beep_state_t *bbc_beeptr = bbc_beep_obtain_state(dip); 334*0Sstevel@tonic-gate 335*0Sstevel@tonic-gate /* Convert the frequency in hz to the bbc counter value */ 336*0Sstevel@tonic-gate counter = bbc_beep_hztocounter(freq); 337*0Sstevel@tonic-gate 338*0Sstevel@tonic-gate /* Extract relevant second and third byte of counter value */ 339*0Sstevel@tonic-gate beep_c2 = (counter & 0xff00) >> 8; 340*0Sstevel@tonic-gate beep_c3 = (counter & 0xff0000) >> 16; 341*0Sstevel@tonic-gate 342*0Sstevel@tonic-gate /* 343*0Sstevel@tonic-gate * We need to write individual bytes instead of writing 344*0Sstevel@tonic-gate * all of 32 bits to take care of allignment problem. 345*0Sstevel@tonic-gate * Write 0 to LS 8 bits and MS 8 bits 346*0Sstevel@tonic-gate * Write beep_c3 to bit 8..15 and beep_c2 to bit 16..24 347*0Sstevel@tonic-gate * Little Endian format 348*0Sstevel@tonic-gate */ 349*0Sstevel@tonic-gate BEEP_WRITE_COUNTER_REG(0, 0); 350*0Sstevel@tonic-gate BEEP_WRITE_COUNTER_REG(1, beep_c3); 351*0Sstevel@tonic-gate BEEP_WRITE_COUNTER_REG(2, beep_c2); 352*0Sstevel@tonic-gate BEEP_WRITE_COUNTER_REG(3, 0); 353*0Sstevel@tonic-gate 354*0Sstevel@tonic-gate BBC_BEEP_DEBUG1((CE_CONT, 355*0Sstevel@tonic-gate "bbc_beep_freq: dip = 0x%p, freq = %d, counter = 0x%x : Done", 356*0Sstevel@tonic-gate (void *)dip, freq, (int)counter)); 357*0Sstevel@tonic-gate } 358*0Sstevel@tonic-gate 359*0Sstevel@tonic-gate 360*0Sstevel@tonic-gate /* 361*0Sstevel@tonic-gate * bbc_beep_on() : 362*0Sstevel@tonic-gate * Turn the beeper on 363*0Sstevel@tonic-gate */ 364*0Sstevel@tonic-gate static void 365*0Sstevel@tonic-gate bbc_beep_on(dev_info_t *dip) 366*0Sstevel@tonic-gate { 367*0Sstevel@tonic-gate bbc_beep_state_t *bbc_beeptr = bbc_beep_obtain_state(dip); 368*0Sstevel@tonic-gate 369*0Sstevel@tonic-gate BEEP_WRITE_CTRL_REG(BBC_BEEP_ON); 370*0Sstevel@tonic-gate 371*0Sstevel@tonic-gate bbc_beeptr->bbc_beep_mode = BBC_BEEP_ON; 372*0Sstevel@tonic-gate 373*0Sstevel@tonic-gate BBC_BEEP_DEBUG1((CE_CONT, "bbc_beep_on: dip = 0x%p done", 374*0Sstevel@tonic-gate (void *)dip)); 375*0Sstevel@tonic-gate } 376*0Sstevel@tonic-gate 377*0Sstevel@tonic-gate 378*0Sstevel@tonic-gate /* 379*0Sstevel@tonic-gate * bbc_beep_off() : 380*0Sstevel@tonic-gate * Turn the beeper off 381*0Sstevel@tonic-gate */ 382*0Sstevel@tonic-gate static void 383*0Sstevel@tonic-gate bbc_beep_off(dev_info_t *dip) 384*0Sstevel@tonic-gate { 385*0Sstevel@tonic-gate bbc_beep_state_t *bbc_beeptr = bbc_beep_obtain_state(dip); 386*0Sstevel@tonic-gate 387*0Sstevel@tonic-gate BEEP_WRITE_CTRL_REG(BBC_BEEP_OFF); 388*0Sstevel@tonic-gate 389*0Sstevel@tonic-gate bbc_beeptr->bbc_beep_mode = BBC_BEEP_OFF; 390*0Sstevel@tonic-gate 391*0Sstevel@tonic-gate BBC_BEEP_DEBUG1((CE_CONT, "bbc_beep_off: dip = 0x%p done", 392*0Sstevel@tonic-gate (void *)dip)); 393*0Sstevel@tonic-gate } 394*0Sstevel@tonic-gate 395*0Sstevel@tonic-gate 396*0Sstevel@tonic-gate /* 397*0Sstevel@tonic-gate * bbc_beep_map_regs() : 398*0Sstevel@tonic-gate * 399*0Sstevel@tonic-gate * The Keyboard Beep Control Register and Keyboard Beep Counter Register 400*0Sstevel@tonic-gate * should be mapped into a non-cacheable portion of the system 401*0Sstevel@tonic-gate * addressable space. 402*0Sstevel@tonic-gate */ 403*0Sstevel@tonic-gate static int 404*0Sstevel@tonic-gate bbc_beep_map_regs(dev_info_t *dip, bbc_beep_state_t *bbc_beeptr) 405*0Sstevel@tonic-gate { 406*0Sstevel@tonic-gate ddi_device_acc_attr_t attr; 407*0Sstevel@tonic-gate 408*0Sstevel@tonic-gate BBC_BEEP_DEBUG1((CE_CONT, "bbc_beep_map_regs: Start\n")); 409*0Sstevel@tonic-gate 410*0Sstevel@tonic-gate /* The host controller will be little endian */ 411*0Sstevel@tonic-gate attr.devacc_attr_version = DDI_DEVICE_ATTR_V0; 412*0Sstevel@tonic-gate attr.devacc_attr_endian_flags = DDI_STRUCTURE_LE_ACC; 413*0Sstevel@tonic-gate attr.devacc_attr_dataorder = DDI_STRICTORDER_ACC; 414*0Sstevel@tonic-gate 415*0Sstevel@tonic-gate /* Map in operational registers */ 416*0Sstevel@tonic-gate if (ddi_regs_map_setup(dip, 0, 417*0Sstevel@tonic-gate (caddr_t *)&bbc_beeptr->bbc_beep_regsp, 418*0Sstevel@tonic-gate 0, 419*0Sstevel@tonic-gate sizeof (bbc_beep_regs_t), 420*0Sstevel@tonic-gate &attr, 421*0Sstevel@tonic-gate &bbc_beeptr->bbc_beep_regs_handle) != DDI_SUCCESS) { 422*0Sstevel@tonic-gate 423*0Sstevel@tonic-gate return (DDI_FAILURE); 424*0Sstevel@tonic-gate } 425*0Sstevel@tonic-gate 426*0Sstevel@tonic-gate BBC_BEEP_DEBUG1((CE_CONT, "bbc_beep_map_regs: done\n")); 427*0Sstevel@tonic-gate 428*0Sstevel@tonic-gate return (DDI_SUCCESS); 429*0Sstevel@tonic-gate } 430*0Sstevel@tonic-gate 431*0Sstevel@tonic-gate 432*0Sstevel@tonic-gate /* 433*0Sstevel@tonic-gate * bbc_beep_obtain_state: 434*0Sstevel@tonic-gate */ 435*0Sstevel@tonic-gate static bbc_beep_state_t * 436*0Sstevel@tonic-gate bbc_beep_obtain_state(dev_info_t *dip) 437*0Sstevel@tonic-gate { 438*0Sstevel@tonic-gate int instance = ddi_get_instance(dip); 439*0Sstevel@tonic-gate 440*0Sstevel@tonic-gate bbc_beep_state_t *state = ddi_get_soft_state(bbc_beep_statep, instance); 441*0Sstevel@tonic-gate 442*0Sstevel@tonic-gate ASSERT(state != NULL); 443*0Sstevel@tonic-gate 444*0Sstevel@tonic-gate BBC_BEEP_DEBUG1((CE_CONT, "bbc_beep_obtain_state: done")); 445*0Sstevel@tonic-gate 446*0Sstevel@tonic-gate return (state); 447*0Sstevel@tonic-gate } 448*0Sstevel@tonic-gate 449*0Sstevel@tonic-gate 450*0Sstevel@tonic-gate /* 451*0Sstevel@tonic-gate * bbc_beep_cleanup : 452*0Sstevel@tonic-gate * Cleanup soft state 453*0Sstevel@tonic-gate */ 454*0Sstevel@tonic-gate static void 455*0Sstevel@tonic-gate bbc_beep_cleanup(bbc_beep_state_t *bbc_beeptr) 456*0Sstevel@tonic-gate { 457*0Sstevel@tonic-gate int instance = ddi_get_instance(bbc_beeptr->bbc_beep_dip); 458*0Sstevel@tonic-gate 459*0Sstevel@tonic-gate ddi_soft_state_free(bbc_beep_statep, instance); 460*0Sstevel@tonic-gate 461*0Sstevel@tonic-gate BBC_BEEP_DEBUG1((CE_CONT, "bbc_beep_cleanup: done")); 462*0Sstevel@tonic-gate } 463*0Sstevel@tonic-gate 464*0Sstevel@tonic-gate 465*0Sstevel@tonic-gate /* 466*0Sstevel@tonic-gate * bbc_beep_hztocounter() : 467*0Sstevel@tonic-gate * Given a frequency in hz, find out the value to 468*0Sstevel@tonic-gate * be set in the Keyboard Beep Counter register 469*0Sstevel@tonic-gate * BBC beeper uses the following formula to calculate 470*0Sstevel@tonic-gate * frequency. The formulae is : 471*0Sstevel@tonic-gate * frequency generated = system freq /2^(n+2) 472*0Sstevel@tonic-gate * Where n = position of the bit of counter register 473*0Sstevel@tonic-gate * that is turned on and can range between 10 to 18. 474*0Sstevel@tonic-gate * So in this function, the inputs are frequency generated 475*0Sstevel@tonic-gate * and system frequency and we need to find out n, i.e, which 476*0Sstevel@tonic-gate * bit to turn on.(Ref. to Section 4.2.22 of the BBC programming 477*0Sstevel@tonic-gate * manual). 478*0Sstevel@tonic-gate */ 479*0Sstevel@tonic-gate unsigned long 480*0Sstevel@tonic-gate bbc_beep_hztocounter(int freq) 481*0Sstevel@tonic-gate { 482*0Sstevel@tonic-gate int i; 483*0Sstevel@tonic-gate unsigned long counter; 484*0Sstevel@tonic-gate int newfreq, oldfreq; 485*0Sstevel@tonic-gate 486*0Sstevel@tonic-gate int system_freq; 487*0Sstevel@tonic-gate 488*0Sstevel@tonic-gate /* 489*0Sstevel@tonic-gate * Get system frequency for the root dev_info properties 490*0Sstevel@tonic-gate */ 491*0Sstevel@tonic-gate system_freq = ddi_prop_get_int(DDI_DEV_T_ANY, ddi_root_node(), 492*0Sstevel@tonic-gate 0, "clock-frequency", 0); 493*0Sstevel@tonic-gate 494*0Sstevel@tonic-gate oldfreq = 0; 495*0Sstevel@tonic-gate 496*0Sstevel@tonic-gate /* 497*0Sstevel@tonic-gate * Calculate frequency by turning on ith bit and 498*0Sstevel@tonic-gate * matching it with the passed frequency and we do this 499*0Sstevel@tonic-gate * in a loop for all the relevant bits 500*0Sstevel@tonic-gate */ 501*0Sstevel@tonic-gate for (i = BBC_BEEP_MIN_SHIFT, counter = 1 << BBC_BEEP_MSBIT; 502*0Sstevel@tonic-gate i >= BBC_BEEP_MAX_SHIFT; i--, counter >>= 1) { 503*0Sstevel@tonic-gate 504*0Sstevel@tonic-gate /* 505*0Sstevel@tonic-gate * Calculate the frequency by dividing the system 506*0Sstevel@tonic-gate * frequency by 2^i 507*0Sstevel@tonic-gate */ 508*0Sstevel@tonic-gate newfreq = system_freq >> i; 509*0Sstevel@tonic-gate 510*0Sstevel@tonic-gate /* 511*0Sstevel@tonic-gate * Check if we turn on the ith bit, the 512*0Sstevel@tonic-gate * frequency matches exactly or not 513*0Sstevel@tonic-gate */ 514*0Sstevel@tonic-gate if (newfreq == freq) { 515*0Sstevel@tonic-gate /* 516*0Sstevel@tonic-gate * Exact match of passed frequency with the 517*0Sstevel@tonic-gate * counter value 518*0Sstevel@tonic-gate */ 519*0Sstevel@tonic-gate 520*0Sstevel@tonic-gate return (counter); 521*0Sstevel@tonic-gate } 522*0Sstevel@tonic-gate 523*0Sstevel@tonic-gate /* 524*0Sstevel@tonic-gate * If calculated frequency is bigger 525*0Sstevel@tonic-gate * return the passed frequency 526*0Sstevel@tonic-gate */ 527*0Sstevel@tonic-gate if (newfreq > freq) { 528*0Sstevel@tonic-gate 529*0Sstevel@tonic-gate if (i == BBC_BEEP_MIN_SHIFT) { 530*0Sstevel@tonic-gate /* Input freq is less than the possible min */ 531*0Sstevel@tonic-gate 532*0Sstevel@tonic-gate return (counter); 533*0Sstevel@tonic-gate } 534*0Sstevel@tonic-gate 535*0Sstevel@tonic-gate /* 536*0Sstevel@tonic-gate * Find out the nearest frequency to the passed 537*0Sstevel@tonic-gate * frequency by comparing the difference between 538*0Sstevel@tonic-gate * the calculated frequency and the passed frequency 539*0Sstevel@tonic-gate */ 540*0Sstevel@tonic-gate if ((freq - oldfreq) > (newfreq - freq)) { 541*0Sstevel@tonic-gate /* Return new counter corres. to newfreq */ 542*0Sstevel@tonic-gate 543*0Sstevel@tonic-gate return (counter); 544*0Sstevel@tonic-gate } 545*0Sstevel@tonic-gate 546*0Sstevel@tonic-gate /* Return old counter corresponding to oldfreq */ 547*0Sstevel@tonic-gate 548*0Sstevel@tonic-gate return (counter << 1); 549*0Sstevel@tonic-gate } 550*0Sstevel@tonic-gate 551*0Sstevel@tonic-gate oldfreq = newfreq; 552*0Sstevel@tonic-gate } 553*0Sstevel@tonic-gate 554*0Sstevel@tonic-gate /* 555*0Sstevel@tonic-gate * Input freq is greater than the possible max; 556*0Sstevel@tonic-gate * Back off the counter value and return max counter 557*0Sstevel@tonic-gate * value possible in the register 558*0Sstevel@tonic-gate */ 559*0Sstevel@tonic-gate return (counter << 1); 560*0Sstevel@tonic-gate } 561