1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License, Version 1.0 only 6 * (the "License"). You may not use this file except in compliance 7 * with the License. 8 * 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10 * or http://www.opensolaris.org/os/licensing. 11 * See the License for the specific language governing permissions 12 * and limitations under the License. 13 * 14 * When distributing Covered Code, include this CDDL HEADER in each 15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16 * If applicable, add the following below this CDDL HEADER, with the 17 * fields enclosed by brackets "[]" replaced with your own identifying 18 * information: Portions Copyright [yyyy] [name of copyright owner] 19 * 20 * CDDL HEADER END 21 */ 22 /* 23 * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 #pragma ident "%Z%%M% %I% %E% SMI" 28 29 /* 30 * Driver to map the PIC for the chicago platform. 31 */ 32 #include <sys/types.h> 33 #include <sys/time.h> 34 #include <sys/errno.h> 35 #include <sys/cmn_err.h> 36 #include <sys/param.h> 37 #include <sys/modctl.h> 38 #include <sys/conf.h> 39 #include <sys/open.h> 40 #include <sys/stat.h> 41 #include <sys/clock.h> 42 #include <sys/pic.h> 43 #include <sys/pic16f747.h> 44 #include <sys/ddi.h> 45 #include <sys/sunddi.h> 46 #include <sys/file.h> 47 48 /* dev_ops and cb_ops entry point function declarations */ 49 static int pic_attach(dev_info_t *, ddi_attach_cmd_t); 50 static int pic_detach(dev_info_t *, ddi_detach_cmd_t); 51 static int pic_getinfo(dev_info_t *, ddi_info_cmd_t, void *, void **); 52 static int pic_open(dev_t *, int, int, cred_t *); 53 static int pic_close(dev_t, int, int, cred_t *); 54 static int pic_ioctl(dev_t, int, intptr_t, int, cred_t *, int *); 55 56 struct cb_ops pic_cb_ops = { 57 pic_open, 58 pic_close, 59 nodev, 60 nodev, 61 nodev, /* dump */ 62 nodev, 63 nodev, 64 pic_ioctl, 65 nodev, /* devmap */ 66 nodev, 67 ddi_segmap, /* segmap */ 68 nochpoll, 69 ddi_prop_op, 70 NULL, /* for STREAMS drivers */ 71 D_NEW | D_MP /* driver compatibility flag */ 72 }; 73 74 static struct dev_ops pic_dev_ops = { 75 DEVO_REV, /* driver build version */ 76 0, /* device reference count */ 77 pic_getinfo, 78 nulldev, 79 nulldev, /* probe */ 80 pic_attach, 81 pic_detach, 82 nulldev, /* reset */ 83 &pic_cb_ops, 84 (struct bus_ops *)NULL, 85 nulldev /* power */ 86 }; 87 88 /* 89 * Fans' and sensors' node names and register offsets 90 */ 91 static struct minor_node_info pic_nodes[N_PIC_NODES] = { 92 {NULL, 0, 0}, /* Reserved */ 93 {"fan_0", RF_FAN0_PERIOD, 0}, /* System Fan 0 */ 94 {"fan_1", RF_FAN1_PERIOD, 1}, /* System Fan 1 */ 95 {"fan_2", RF_FAN2_PERIOD, 2}, /* System Fan 2 */ 96 {"fan_3", RF_FAN3_PERIOD, 3}, /* System Fan 3 */ 97 {"fan_4", RF_FAN4_PERIOD, 4}, /* System Fan 4 in P0.1 */ 98 {"adt7462", RF_LOCAL_TEMP, 0}, /* ADT7462 Local Temperature */ 99 {"cpu_0", RF_REMOTE1_TEMP, 0}, /* CPU 0 temp */ 100 {"cpu_1", RF_REMOTE2_TEMP, 0}, /* CPU 1 temp */ 101 {"mb", RF_REMOTE3_TEMP, 0}, /* Motherboard temp */ 102 {"lm95221", RF_LM95221_TEMP, 0}, /* LM95221 Local Temperature */ 103 {"fire", RF_FIRE_TEMP, 0}, /* FIRE Temp */ 104 {"lsi1064", RF_LSI1064_TEMP, 0}, /* LSI1064 Temp */ 105 {"front_panel", RF_FRONT_TEMP, 0} /* Front Panel Temperature */ 106 }; 107 108 /* 109 * Soft state 110 */ 111 struct pic_softc { 112 dev_info_t *dip; 113 kmutex_t mutex; 114 uint8_t *cmd_reg; 115 ddi_acc_handle_t cmd_handle; 116 }; 117 #define getsoftc(inst) ((struct pic_softc *)ddi_get_soft_state(statep, (inst))) 118 119 /* module configuration stuff */ 120 static void *statep; 121 extern struct mod_ops mod_driverops; 122 123 static struct modldrv modldrv = { 124 &mod_driverops, 125 "pic_client driver (v.%I%) ", 126 &pic_dev_ops 127 }; 128 129 static struct modlinkage modlinkage = { 130 MODREV_1, 131 &modldrv, 132 0 133 }; 134 135 int 136 _init(void) 137 { 138 int e; 139 140 if (e = ddi_soft_state_init(&statep, sizeof (struct pic_softc), 141 MAX_PIC_INSTANCES)) { 142 return (e); 143 } 144 145 if ((e = mod_install(&modlinkage)) != 0) 146 ddi_soft_state_fini(&statep); 147 148 return (e); 149 } 150 151 int 152 _fini(void) 153 { 154 int e; 155 156 if ((e = mod_remove(&modlinkage)) != 0) 157 return (e); 158 159 ddi_soft_state_fini(&statep); 160 161 return (DDI_SUCCESS); 162 } 163 164 int 165 _info(struct modinfo *modinfop) 166 { 167 return (mod_info(&modlinkage, modinfop)); 168 } 169 170 /*ARGSUSED*/ 171 static int 172 pic_getinfo(dev_info_t *dip, ddi_info_cmd_t cmd, void *arg, void **result) 173 { 174 int inst; 175 int retval = DDI_SUCCESS; 176 struct pic_softc *softc; 177 178 inst = PIC_MINOR_TO_INST(getminor((dev_t)arg)); 179 180 switch (cmd) { 181 case DDI_INFO_DEVT2DEVINFO: 182 if ((softc = getsoftc(inst)) == NULL) { 183 *result = (void *)NULL; 184 retval = DDI_FAILURE; 185 } else 186 *result = (void *)softc->dip; 187 break; 188 189 case DDI_INFO_DEVT2INSTANCE: 190 *result = (void *)((uintptr_t)inst); 191 break; 192 193 default: 194 retval = DDI_FAILURE; 195 } 196 197 return (retval); 198 } 199 200 static int 201 pic_attach(dev_info_t *dip, ddi_attach_cmd_t cmd) 202 { 203 int inst; 204 int i; 205 struct pic_softc *softc = NULL; 206 char *minor_name; 207 int minor; 208 char name[80]; 209 ddi_device_acc_attr_t dev_attr; 210 int res; 211 212 switch (cmd) { 213 case DDI_ATTACH: 214 inst = ddi_get_instance(dip); 215 if (inst >= MAX_PIC_INSTANCES) { 216 cmn_err(CE_WARN, "attach failed, too many instances\n"); 217 return (DDI_FAILURE); 218 } 219 220 (void) sprintf(name, "env-monitor%d", inst); 221 minor = PIC_INST_TO_MINOR(inst) | PIC_UNIT_TO_MINOR(0); 222 if (ddi_create_minor_node(dip, name, S_IFCHR, minor, 223 DDI_PSEUDO, NULL) == DDI_FAILURE) { 224 cmn_err(CE_WARN, 225 "ddi_create_minor_node() failed for inst %d\n", 226 inst); 227 return (DDI_FAILURE); 228 } 229 230 /* Allocate a soft state structure for this instance */ 231 if (ddi_soft_state_zalloc(statep, inst) != DDI_SUCCESS) { 232 cmn_err(CE_WARN, " ddi_soft_state_zalloc() failed " 233 "for inst %d\n", inst); 234 goto attach_failed; 235 } 236 237 /* Setup soft state */ 238 softc = getsoftc(inst); 239 softc->dip = dip; 240 mutex_init(&softc->mutex, NULL, MUTEX_DRIVER, NULL); 241 242 /* Setup device attributes */ 243 dev_attr.devacc_attr_version = DDI_DEVICE_ATTR_V0; 244 dev_attr.devacc_attr_endian_flags = DDI_STRUCTURE_LE_ACC; 245 dev_attr.devacc_attr_dataorder = DDI_STRICTORDER_ACC; 246 247 /* 248 * The RF_COMMAND/RF_STATUS and RF_IND_DATA/RF_IND_ADDR 249 * register pairs are mapped as one register set starting 250 * from 0x0 and length 0x42. 251 */ 252 res = ddi_regs_map_setup(dip, 0, (caddr_t *)&softc->cmd_reg, 253 0, 0x42, &dev_attr, &softc->cmd_handle); 254 if (res != DDI_SUCCESS) { 255 cmn_err(CE_WARN, "ddi_regs_map_setup() failed\n"); 256 goto attach_failed; 257 } 258 259 /* Set up fans' and sensors' device minor nodes */ 260 for (i = 1; i < N_PIC_NODES; i++) { 261 minor_name = pic_nodes[i].minor_name; 262 minor = PIC_INST_TO_MINOR(inst) | PIC_UNIT_TO_MINOR(i); 263 if (ddi_create_minor_node(dip, minor_name, S_IFCHR, 264 minor, PICDEV_NODE_TYPE, NULL) == DDI_FAILURE) { 265 cmn_err(CE_WARN, 266 "%s:%d ddi_create_minor_node failed", 267 ddi_driver_name(dip), inst); 268 (void) pic_detach(dip, DDI_DETACH); 269 return (DDI_FAILURE); 270 } 271 } 272 273 /* Create main environmental node */ 274 ddi_report_dev(dip); 275 276 return (DDI_SUCCESS); 277 278 case DDI_RESUME: 279 return (DDI_SUCCESS); 280 281 default: 282 return (DDI_FAILURE); 283 } 284 285 attach_failed: 286 287 /* Free soft state, if allocated. remove minor node if added earlier */ 288 if (softc) 289 ddi_soft_state_free(statep, inst); 290 291 ddi_remove_minor_node(dip, NULL); 292 293 return (DDI_FAILURE); 294 } 295 296 static int 297 pic_detach(dev_info_t *dip, ddi_detach_cmd_t cmd) 298 { 299 int inst; 300 struct pic_softc *softc; 301 302 switch (cmd) { 303 case DDI_DETACH: 304 inst = ddi_get_instance(dip); 305 if ((softc = getsoftc(inst)) == NULL) 306 return (ENXIO); 307 308 (void) ddi_regs_map_free(&softc->cmd_handle); 309 /* Free the soft state and remove minor node added earlier */ 310 mutex_destroy(&softc->mutex); 311 ddi_soft_state_free(statep, inst); 312 ddi_remove_minor_node(dip, NULL); 313 return (DDI_SUCCESS); 314 315 case DDI_SUSPEND: 316 return (DDI_SUCCESS); 317 318 default: 319 return (DDI_FAILURE); 320 } 321 } 322 323 /*ARGSUSED*/ 324 static int 325 pic_open(dev_t *devp, int flag, int otyp, cred_t *credp) 326 { 327 int inst = PIC_MINOR_TO_INST(getminor(*devp)); 328 329 return (getsoftc(inst) == NULL ? ENXIO : 0); 330 } 331 332 /*ARGSUSED*/ 333 static int 334 pic_close(dev_t dev, int flag, int otyp, cred_t *credp) 335 { 336 int inst = PIC_MINOR_TO_INST(getminor(dev)); 337 338 return (getsoftc(inst) == NULL ? ENXIO : 0); 339 } 340 341 /*ARGSUSED*/ 342 static int 343 pic_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp, int *rvalp) 344 { 345 int inst; 346 int node; 347 struct pic_softc *softc; 348 uint8_t in_command; 349 int16_t tempr; 350 351 inst = PIC_MINOR_TO_INST(getminor(dev)); 352 if ((softc = getsoftc(inst)) == NULL) 353 return (ENXIO); 354 355 mutex_enter(&softc->mutex); 356 357 if (ddi_copyin((caddr_t)arg, &in_command, sizeof (in_command), 358 mode) != DDI_SUCCESS) { 359 mutex_exit(&softc->mutex); 360 return (EFAULT); 361 } 362 363 node = PIC_MINOR_TO_UNIT(getminor(dev)); 364 if ((node >= N_PIC_NODES) || (node < 1)) { 365 mutex_exit(&softc->mutex); 366 return (ENXIO); 367 } 368 369 switch (cmd) { 370 case PIC_GET_TEMPERATURE: 371 drv_usecwait(10); 372 373 /* select the temp sensor */ 374 (void) ddi_put8(softc->cmd_handle, (uint8_t *)softc->cmd_reg + 375 RF_IND_ADDR, pic_nodes[node].reg_offset); 376 377 /* retrieve temperature data */ 378 tempr = (int16_t)ddi_get8(softc->cmd_handle, 379 (uint8_t *)softc->cmd_reg + RF_IND_DATA); 380 mutex_exit(&softc->mutex); 381 382 if (tempr == 0xff) 383 return (EIO); 384 385 /* 386 * The temp is passed in as a uint8 value, we need to convert 387 * it to a signed 16 bit value to be able to handle the range 388 * of -64 to 190 degrees. 389 */ 390 tempr -= 64; 391 (void) ddi_copyout(&tempr, (caddr_t)arg, sizeof (tempr), mode); 392 return (0); 393 394 case PIC_GET_FAN_SPEED: 395 drv_usecwait(10); 396 397 /* select fan */ 398 (void) ddi_put8(softc->cmd_handle, (uint8_t *)softc->cmd_reg + 399 RF_IND_ADDR, pic_nodes[node].reg_offset); 400 401 /* retrieve fan data */ 402 in_command = ddi_get8(softc->cmd_handle, 403 (uint8_t *)softc->cmd_reg + RF_IND_DATA); 404 mutex_exit(&softc->mutex); 405 406 if (in_command == 0xff) 407 return (EIO); 408 409 (void) ddi_copyout(&in_command, (caddr_t)arg, 1, mode); 410 return (0); 411 412 case PIC_SET_FAN_SPEED: 413 /* select fan */ 414 (void) ddi_put8(softc->cmd_handle, (uint8_t *)softc->cmd_reg + 415 RF_IND_ADDR, pic_nodes[node].reg_offset); 416 417 /* send the fan data */ 418 (void) ddi_put8(softc->cmd_handle, 419 (uint8_t *)softc->cmd_reg + RF_IND_DATA, in_command); 420 421 mutex_exit(&softc->mutex); 422 return (0); 423 424 case PIC_GET_STATUS: 425 mutex_exit(&softc->mutex); 426 427 /* we don't read the status reg anymore */ 428 in_command = 0; 429 (void) ddi_copyout(&in_command, (caddr_t)arg, 1, mode); 430 431 return (0); 432 433 case PIC_GET_FAN_STATUS: 434 drv_usecwait(10); 435 436 /* read ffault register */ 437 (void) ddi_put8(softc->cmd_handle, (uint8_t *)softc->cmd_reg + 438 RF_IND_ADDR, RF_FAN_STATUS); 439 440 /* retrieve fan failure status */ 441 in_command = ddi_get8(softc->cmd_handle, 442 (uint8_t *)softc->cmd_reg + RF_IND_DATA); 443 mutex_exit(&softc->mutex); 444 445 if (in_command == 0xff) 446 return (EIO); 447 448 in_command = (in_command >> pic_nodes[node].ff_shift) & 0x1; 449 (void) ddi_copyout(&in_command, (caddr_t)arg, 1, mode); 450 return (0); 451 452 case PIC_SET_ESTAR_MODE: 453 (void) ddi_put8(softc->cmd_handle, 454 (uint8_t *)softc->cmd_reg + RF_COMMAND, CMD_TO_ESTAR); 455 mutex_exit(&softc->mutex); 456 return (0); 457 458 default: 459 mutex_exit(&softc->mutex); 460 cmn_err(CE_NOTE, "cmd %d isnt valid", cmd); 461 return (EINVAL); 462 } 463 } 464