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 * pcf8584.c is the nexus driver for all pcf8584 controller 31 * implementations. It supports both interrupt and polled 32 * mode operation, but defaults to interrupt. 33 */ 34 35 #include <sys/types.h> 36 #include <sys/conf.h> 37 #include <sys/file.h> 38 #include <sys/open.h> 39 #include <sys/ddi.h> 40 #include <sys/sunddi.h> 41 #include <sys/sunndi.h> 42 #include <sys/modctl.h> 43 #include <sys/stat.h> 44 #include <sys/kmem.h> 45 #include <sys/archsystm.h> 46 #include <sys/platform_module.h> 47 48 #include <sys/i2c/clients/i2c_client.h> 49 #include <sys/i2c/misc/i2c_svc.h> 50 #include <sys/i2c/misc/i2c_svc_impl.h> 51 #include <sys/i2c/nexus/pcf8584.h> 52 53 #include <sys/note.h> 54 55 /* 56 * static function declarations 57 */ 58 static void pcf8584_resume(dev_info_t *dip); 59 static void pcf8584_suspend(dev_info_t *dip); 60 static int pcf8584_bus_ctl(dev_info_t *dip, dev_info_t *rdip, 61 ddi_ctl_enum_t op, void *arg, void *result); 62 static void pcf8584_acquire(pcf8584_t *, dev_info_t *dip, 63 i2c_transfer_t *tp, boolean_t force); 64 static void pcf8584_release(pcf8584_t *, boolean_t force); 65 static int pcf8584_attach(dev_info_t *dip, ddi_attach_cmd_t cmd); 66 static int pcf8584_detach(dev_info_t *dip, ddi_detach_cmd_t cmd); 67 static int pcf8584_open(dev_t *devp, int flag, int otyp, 68 cred_t *cred_p); 69 static int pcf8584_close(dev_t dev, int flag, int otyp, 70 cred_t *cred_p); 71 static int pcf8584_ioctl(dev_t, int, intptr_t, int, cred_t *, int *); 72 static void pcf8584_select_bus(pcf8584_t *i2c); 73 static enum tran_state pcf8584_type_to_state(int i2c_flags); 74 static void pcf8584_put_s1(pcf8584_t *i2c, char cmd); 75 static void pcf8584_put_s0(pcf8584_t *i2c, char data); 76 static uint8_t pcf8584_get_s0(pcf8584_t *i2c); 77 static uint8_t pcf8584_get_s1(pcf8584_t *i2c); 78 static int pcf8584_bbn_ready(pcf8584_t *i2c); 79 static int pcf8584_error(int status, uint8_t rdwr, pcf8584_t *i2c); 80 static void pcf8584_monitor_mode(pcf8584_t *i2c); 81 static int pcf8584_initchild(dev_info_t *cdip); 82 static void pcf8584_uninitchild(dev_info_t *cdip); 83 static void pcf8584_init(pcf8584_t *i2c); 84 static int pcf8584_setup_regs(dev_info_t *dip, pcf8584_t *i2c); 85 static void pcf8584_free_regs(pcf8584_t *i2c); 86 static void pcf8584_reportdev(dev_info_t *dip, dev_info_t *rdip); 87 static int pcf8584_dip_to_addr(dev_info_t *dip); 88 static uint_t pcf8584_intr(caddr_t arg); 89 static int pcf8584_process(pcf8584_t *i2c, uint8_t s1); 90 int pcf8584_transfer(dev_info_t *dip, i2c_transfer_t *tp); 91 92 static void pcf8584_do_polled_io(pcf8584_t *i2c); 93 static void pcf8584_take_over(pcf8584_t *i2c, dev_info_t *dip, 94 i2c_transfer_t *tp, kcondvar_t **waiter, int *saved_mode); 95 static void pcf8584_give_up(pcf8584_t *i2c, kcondvar_t *waiter, int saved_mode); 96 97 static struct bus_ops pcf8584_busops = { 98 BUSO_REV, 99 nullbusmap, /* bus_map */ 100 NULL, /* bus_get_intrspec */ 101 NULL, /* bus_add_intrspec */ 102 NULL, /* bus_remove_intrspec */ 103 NULL, /* bus_map_fault */ 104 ddi_no_dma_map, /* bus_dma_map */ 105 ddi_no_dma_allochdl, /* bus_dma_allochdl */ 106 ddi_no_dma_freehdl, /* bus_dma_freehdl */ 107 ddi_no_dma_bindhdl, /* bus_dma_bindhdl */ 108 ddi_no_dma_unbindhdl, /* bus_unbindhdl */ 109 ddi_no_dma_flush, /* bus_dma_flush */ 110 ddi_no_dma_win, /* bus_dma_win */ 111 ddi_no_dma_mctl, /* bus_dma_ctl */ 112 pcf8584_bus_ctl, /* bus_ctl */ 113 ddi_bus_prop_op, /* bus_prop_op */ 114 NULL, /* bus_get_eventcookie */ 115 NULL, /* bus_add_eventcall */ 116 NULL, /* bus_remove_eventcall */ 117 NULL, /* bus_post_event */ 118 0, /* bus_intr_ctl */ 119 0, /* bus_config */ 120 0, /* bus_unconfig */ 121 0, /* bus_fm_init */ 122 0, /* bus_fm_fini */ 123 0, /* bus_fm_access_enter */ 124 0, /* bus_fm_access_exit */ 125 0, /* bus_power */ 126 i_ddi_intr_ops /* bus_intr_op */ 127 }; 128 129 struct cb_ops pcf8584_cb_ops = { 130 pcf8584_open, /* open */ 131 pcf8584_close, /* close */ 132 nodev, /* strategy */ 133 nodev, /* print */ 134 nodev, /* dump */ 135 nodev, /* read */ 136 nodev, /* write */ 137 pcf8584_ioctl, /* ioctl */ 138 nodev, /* devmap */ 139 nodev, /* mmap */ 140 nodev, /* segmap */ 141 nochpoll, /* poll */ 142 ddi_prop_op, /* cb_prop_op */ 143 0, /* streamtab */ 144 D_MP | D_NEW /* Driver compatibility flag */ 145 }; 146 147 static struct dev_ops pcf8584_ops = { 148 DEVO_REV, 149 0, 150 ddi_getinfo_1to1, 151 nulldev, 152 nulldev, 153 pcf8584_attach, 154 pcf8584_detach, 155 nodev, 156 &pcf8584_cb_ops, 157 &pcf8584_busops 158 }; 159 160 static struct modldrv modldrv = { 161 &mod_driverops, /* Type of module. This one is a driver */ 162 "I2C Nexus Driver %I%", /* Name of the module. */ 163 &pcf8584_ops, /* driver ops */ 164 }; 165 166 static struct modlinkage modlinkage = { 167 MODREV_1, 168 &modldrv, 169 NULL 170 }; 171 172 /* 173 * pcf8584 soft state 174 */ 175 static void *pcf8584_state; 176 177 i2c_nexus_reg_t pcf8584_regvec = { 178 I2C_NEXUS_REV, 179 pcf8584_transfer, 180 }; 181 182 /* 183 * The "interrupt_priorities" property is how a driver can specify a SPARC 184 * PIL level to associate with each of its interrupt properties. Most 185 * self-identifying busses have a better mechanism for managing this, but I2C 186 * doesn't. 187 */ 188 int pcf8584_pil = PCF8584_PIL; 189 190 #ifdef DEBUG 191 int pcf8584_print_lvl = 0; 192 static kmutex_t msg_buf_lock; 193 static char msg_buff[1024]; 194 #define PCF8584_DDB(command) \ 195 do { \ 196 { command; } \ 197 _NOTE(CONSTANTCONDITION) \ 198 } while (0) 199 200 static void 201 pcf8584_print(int flags, const char *fmt, ...) 202 { 203 if (flags & pcf8584_print_lvl) { 204 va_list ap; 205 206 va_start(ap, fmt); 207 208 if (pcf8584_print_lvl & PRT_PROM) { 209 prom_vprintf(fmt, ap); 210 } else { 211 mutex_enter(&msg_buf_lock); 212 (void) vsprintf(msg_buff, fmt, ap); 213 if (pcf8584_print_lvl & PRT_BUFFONLY) { 214 cmn_err(CE_CONT, "?%s", msg_buff); 215 } else { 216 cmn_err(CE_CONT, "%s", msg_buff); 217 } 218 mutex_exit(&msg_buf_lock); 219 } 220 va_end(ap); 221 } 222 } 223 #else 224 #define PCF8584_DDB(command) \ 225 do { \ 226 { _NOTE(EMPTY); } \ 227 _NOTE(CONSTANTCONDITION) \ 228 } while (0) 229 #endif 230 231 int 232 _init(void) 233 { 234 int status; 235 236 status = ddi_soft_state_init(&pcf8584_state, sizeof (pcf8584_t), 237 PCF8584_INITIAL_SOFT_SPACE); 238 if (status != 0) { 239 240 return (status); 241 } 242 243 if ((status = mod_install(&modlinkage)) != 0) { 244 ddi_soft_state_fini(&pcf8584_state); 245 } 246 247 return (status); 248 } 249 250 int 251 _fini(void) 252 { 253 int status; 254 255 if ((status = mod_remove(&modlinkage)) == 0) { 256 ddi_soft_state_fini(&pcf8584_state); 257 } 258 259 return (status); 260 } 261 262 /* 263 * The loadable-module _info(9E) entry point 264 */ 265 int 266 _info(struct modinfo *modinfop) 267 { 268 return (mod_info(&modlinkage, modinfop)); 269 } 270 271 static void 272 pcf8584_dodetach(dev_info_t *dip) 273 { 274 pcf8584_t *i2c; 275 int instance = ddi_get_instance(dip); 276 277 i2c = (pcf8584_t *)ddi_get_soft_state(pcf8584_state, instance); 278 279 if ((i2c->pcf8584_attachflags & ADD_INTR) != 0) { 280 ddi_remove_intr(dip, 0, i2c->pcf8584_icookie); 281 } 282 283 cv_destroy(&i2c->pcf8584_cv); 284 285 if ((i2c->pcf8584_attachflags & IMUTEX) != 0) { 286 mutex_destroy(&i2c->pcf8584_imutex); 287 cv_destroy(&i2c->pcf8584_icv); 288 } 289 if ((i2c->pcf8584_attachflags & SETUP_REGS) != 0) { 290 pcf8584_free_regs(i2c); 291 } 292 if ((i2c->pcf8584_attachflags & NEXUS_REGISTER) != 0) { 293 i2c_nexus_unregister(dip); 294 } 295 if ((i2c->pcf8584_attachflags & PROP_CREATE) != 0) { 296 (void) ddi_prop_remove(DDI_DEV_T_NONE, dip, 297 "interrupt-priorities"); 298 } 299 if ((i2c->pcf8584_attachflags & MINOR_NODE) != 0) { 300 ddi_remove_minor_node(dip, NULL); 301 } 302 303 ddi_soft_state_free(pcf8584_state, instance); 304 } 305 306 static int 307 pcf8584_doattach(dev_info_t *dip) 308 { 309 pcf8584_t *i2c; 310 int instance = ddi_get_instance(dip); 311 312 /* 313 * Allocate soft state structure. 314 */ 315 if (ddi_soft_state_zalloc(pcf8584_state, instance) != DDI_SUCCESS) { 316 317 return (DDI_FAILURE); 318 } 319 320 i2c = (pcf8584_t *)ddi_get_soft_state(pcf8584_state, instance); 321 322 i2c->pcf8584_dip = dip; 323 324 (void) snprintf(i2c->pcf8584_name, sizeof (i2c->pcf8584_name), 325 "%s_%d", ddi_node_name(dip), instance); 326 327 if (ddi_prop_exists(DDI_DEV_T_ANY, dip, 328 DDI_PROP_NOTPROM | DDI_PROP_DONTPASS, 329 "interrupt-priorities") != 1) { 330 (void) ddi_prop_create(DDI_DEV_T_NONE, dip, 331 DDI_PROP_CANSLEEP, "interrupt-priorities", 332 (caddr_t)&pcf8584_pil, 333 sizeof (pcf8584_pil)); 334 i2c->pcf8584_attachflags |= PROP_CREATE; 335 } 336 337 cv_init(&i2c->pcf8584_cv, NULL, CV_DRIVER, NULL); 338 339 if (pcf8584_setup_regs(dip, i2c) != DDI_SUCCESS) { 340 goto bad; 341 } 342 343 i2c->pcf8584_attachflags |= SETUP_REGS; 344 345 if (ddi_prop_exists(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS | 346 DDI_PROP_CANSLEEP, "poll-mode") == 1) { 347 i2c->pcf8584_mode = PCF8584_POLL_MODE; 348 } else { 349 350 if (ddi_get_iblock_cookie(dip, 0, 351 &i2c->pcf8584_icookie) == DDI_SUCCESS) { 352 mutex_init(&i2c->pcf8584_imutex, NULL, MUTEX_DRIVER, 353 (void *)i2c->pcf8584_icookie); 354 cv_init(&i2c->pcf8584_icv, NULL, CV_DRIVER, NULL); 355 i2c->pcf8584_attachflags |= IMUTEX; 356 357 if (ddi_add_intr(dip, 0, NULL, NULL, pcf8584_intr, 358 (caddr_t)i2c) == DDI_SUCCESS) { 359 i2c->pcf8584_attachflags |= ADD_INTR; 360 i2c->pcf8584_mode = PCF8584_INTR_MODE; 361 } else { 362 cmn_err(CE_WARN, "%s failed to add interrupt", 363 i2c->pcf8584_name); 364 i2c->pcf8584_mode = PCF8584_POLL_MODE; 365 } 366 } else { 367 cmn_err(CE_WARN, "%s failed to retrieve iblock cookie. " 368 "Operating in POLL MODE only", i2c->pcf8584_name); 369 i2c->pcf8584_mode = PCF8584_POLL_MODE; 370 } 371 } 372 373 /* 374 * For polled mode, still initialize a cv and mutex 375 */ 376 if ((i2c->pcf8584_attachflags & IMUTEX) == 0) { 377 cv_init(&i2c->pcf8584_icv, NULL, CV_DRIVER, NULL); 378 mutex_init(&i2c->pcf8584_imutex, NULL, MUTEX_DRIVER, NULL); 379 i2c->pcf8584_attachflags |= IMUTEX; 380 } 381 382 i2c_nexus_register(dip, &pcf8584_regvec); 383 i2c->pcf8584_attachflags |= NEXUS_REGISTER; 384 385 if (ddi_create_minor_node(dip, "devctl", S_IFCHR, instance, 386 DDI_NT_NEXUS, 0) == DDI_FAILURE) { 387 cmn_err(CE_WARN, "%s ddi_create_minor_node failed", 388 i2c->pcf8584_name); 389 goto bad; 390 } 391 392 i2c->pcf8584_attachflags |= MINOR_NODE; 393 394 pcf8584_init(i2c); 395 396 i2c->pcf8584_nexus_dip = dip; 397 398 return (DDI_SUCCESS); 399 400 bad: 401 pcf8584_dodetach(dip); 402 403 return (DDI_FAILURE); 404 } 405 406 static int 407 pcf8584_attach(dev_info_t *dip, ddi_attach_cmd_t cmd) 408 { 409 switch (cmd) { 410 case DDI_ATTACH: 411 412 return (pcf8584_doattach(dip)); 413 case DDI_RESUME: 414 pcf8584_resume(dip); 415 416 return (DDI_SUCCESS); 417 default: 418 419 return (DDI_FAILURE); 420 } 421 } 422 423 static int 424 pcf8584_detach(dev_info_t *dip, ddi_detach_cmd_t cmd) 425 { 426 switch (cmd) { 427 case DDI_DETACH: 428 pcf8584_dodetach(dip); 429 430 return (DDI_SUCCESS); 431 case DDI_SUSPEND: 432 pcf8584_suspend(dip); 433 434 return (DDI_SUCCESS); 435 default: 436 437 return (DDI_FAILURE); 438 } 439 } 440 441 /*ARGSUSED*/ 442 static int 443 pcf8584_open(dev_t *devp, int flag, int otyp, cred_t *cred_p) 444 { 445 int instance; 446 pcf8584_t *i2c; 447 448 /* 449 * Make sure the open is for the right file type 450 */ 451 if (otyp != OTYP_CHR) 452 return (EINVAL); 453 454 instance = getminor(*devp); 455 i2c = (pcf8584_t *)ddi_get_soft_state(pcf8584_state, instance); 456 if (i2c == NULL) 457 return (ENXIO); 458 459 /* 460 * Enforce exclusive access 461 */ 462 mutex_enter(&i2c->pcf8584_imutex); 463 if (i2c->pcf8584_open) { 464 mutex_exit(&i2c->pcf8584_imutex); 465 466 return (EBUSY); 467 } else 468 i2c->pcf8584_open = 1; 469 mutex_exit(&i2c->pcf8584_imutex); 470 471 return (0); 472 } 473 474 /*ARGSUSED*/ 475 static int 476 pcf8584_close(dev_t dev, int flag, int otyp, cred_t *cred_p) 477 { 478 int instance; 479 pcf8584_t *i2c; 480 481 /* 482 * Make sure the close is for the right file type 483 */ 484 if (otyp != OTYP_CHR) 485 return (EINVAL); 486 487 instance = getminor(dev); 488 i2c = (pcf8584_t *)ddi_get_soft_state(pcf8584_state, instance); 489 if (i2c == NULL) 490 return (ENXIO); 491 492 mutex_enter(&i2c->pcf8584_imutex); 493 i2c->pcf8584_open = 0; 494 mutex_exit(&i2c->pcf8584_imutex); 495 496 return (0); 497 } 498 499 /*ARGSUSED*/ 500 static int 501 pcf8584_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp, 502 int *rvalp) 503 { 504 pcf8584_t *i2c; 505 dev_info_t *self; 506 struct devctl_iocdata *dcp; 507 int rv; 508 509 i2c = (pcf8584_t *)ddi_get_soft_state(pcf8584_state, getminor(dev)); 510 if (i2c == NULL) 511 return (ENXIO); 512 513 self = (dev_info_t *)i2c->pcf8584_nexus_dip; 514 515 /* 516 * read devctl ioctl data 517 */ 518 if (ndi_dc_allochdl((void *)arg, &dcp) != NDI_SUCCESS) { 519 520 return (EFAULT); 521 } 522 523 switch (cmd) { 524 case DEVCTL_BUS_DEV_CREATE: 525 rv = ndi_dc_devi_create(dcp, self, 0, NULL); 526 break; 527 case DEVCTL_DEVICE_REMOVE: 528 rv = ndi_devctl_device_remove(self, dcp, 0); 529 break; 530 default: 531 rv = ENOTSUP; 532 } 533 534 ndi_dc_freehdl(dcp); 535 536 return (rv); 537 } 538 539 static int 540 pcf8584_bus_ctl(dev_info_t *dip, dev_info_t *rdip, ddi_ctl_enum_t op, 541 void *arg, void *result) 542 { 543 switch (op) { 544 case DDI_CTLOPS_INITCHILD: 545 546 return (pcf8584_initchild((dev_info_t *)arg)); 547 case DDI_CTLOPS_UNINITCHILD: 548 pcf8584_uninitchild((dev_info_t *)arg); 549 550 return (DDI_SUCCESS); 551 CTLOPS_REPORTDEV: 552 pcf8584_reportdev(dip, rdip); 553 554 return (DDI_SUCCESS); 555 case DDI_CTLOPS_DMAPMAPC: 556 case DDI_CTLOPS_POKE: 557 case DDI_CTLOPS_PEEK: 558 case DDI_CTLOPS_IOMIN: 559 case DDI_CTLOPS_REPORTINT: 560 case DDI_CTLOPS_SIDDEV: 561 case DDI_CTLOPS_SLAVEONLY: 562 case DDI_CTLOPS_AFFINITY: 563 case DDI_CTLOPS_PTOB: 564 case DDI_CTLOPS_BTOP: 565 case DDI_CTLOPS_BTOPR: 566 case DDI_CTLOPS_INTR_HILEVEL: 567 case DDI_CTLOPS_XLATE_INTRS: 568 case DDI_CTLOPS_DVMAPAGESIZE: 569 570 return (DDI_FAILURE); 571 default: 572 573 return (ddi_ctlops(dip, rdip, op, arg, result)); 574 } 575 } 576 577 /* 578 * pcf8584_suspend() is called before the system suspends. Existing 579 * transfer in progress or waiting will complete, but new transfers are 580 * effectively blocked by "acquiring" the bus. 581 */ 582 static void 583 pcf8584_suspend(dev_info_t *dip) 584 { 585 pcf8584_t *i2c; 586 int instance; 587 588 instance = ddi_get_instance(dip); 589 i2c = (pcf8584_t *)ddi_get_soft_state(pcf8584_state, instance); 590 591 pcf8584_acquire(i2c, NULL, NULL, B_FALSE); 592 } 593 594 /* 595 * pcf8584_resume() is called when the system resumes from CPR. It releases 596 * the hold that was placed on the i2c bus, which allows any real 597 * transfers to continue. 598 */ 599 static void 600 pcf8584_resume(dev_info_t *dip) 601 { 602 pcf8584_t *i2c; 603 int instance; 604 605 instance = ddi_get_instance(dip); 606 i2c = (pcf8584_t *)ddi_get_soft_state(pcf8584_state, instance); 607 608 pcf8584_release(i2c, B_FALSE); 609 610 pcf8584_init(i2c); 611 } 612 613 /* 614 * pcf8584_acquire() is called by a thread wishing to "own" the I2C bus. 615 * It should not be held across multiple transfers. If the 'force' flag 616 * is set, do not try to acquire mutex or do cv_wait. 617 */ 618 static void 619 pcf8584_acquire(pcf8584_t *i2c, dev_info_t *dip, i2c_transfer_t *tp, 620 boolean_t force) 621 { 622 if (force) { 623 i2c->pcf8584_busy = 1; 624 i2c->pcf8584_cur_tran = tp; 625 i2c->pcf8584_cur_dip = dip; 626 i2c->pcf8584_cur_status = PCF8584_TRANSFER_NEW; 627 return; 628 } 629 630 mutex_enter(&i2c->pcf8584_imutex); 631 while (i2c->pcf8584_busy) { 632 cv_wait(&i2c->pcf8584_cv, &i2c->pcf8584_imutex); 633 } 634 i2c->pcf8584_busy = 1; 635 mutex_exit(&i2c->pcf8584_imutex); 636 /* 637 * On systems where OBP shares a pcf8584 controller with the 638 * OS, plat_shared_i2c_enter will serialize access to the 639 * pcf8584 controller. Do not grab this lock during CPR 640 * suspend as the CPR thread also acquires this muxex 641 * through through prom_setprop which causes recursive 642 * mutex enter. 643 * 644 * dip == NULL during CPR. 645 */ 646 if ((&plat_shared_i2c_enter != NULL) && (dip != NULL)) { 647 plat_shared_i2c_enter(i2c->pcf8584_dip); 648 } 649 650 mutex_enter(&i2c->pcf8584_imutex); 651 i2c->pcf8584_cur_tran = tp; 652 i2c->pcf8584_cur_dip = dip; 653 mutex_exit(&i2c->pcf8584_imutex); 654 } 655 656 /* 657 * pcf8584_release() is called to release a hold made by pcf8584_acquire(). 658 */ 659 static void 660 pcf8584_release(pcf8584_t *i2c, boolean_t force) 661 { 662 if (force) { 663 i2c->pcf8584_busy = 0; 664 i2c->pcf8584_cur_tran = NULL; 665 i2c->pcf8584_cur_dip = NULL; 666 i2c->pcf8584_cur_status = PCF8584_TRANSFER_OVER; 667 cv_signal(&i2c->pcf8584_cv); 668 return; 669 } 670 671 mutex_enter(&i2c->pcf8584_imutex); 672 i2c->pcf8584_busy = 0; 673 i2c->pcf8584_cur_tran = NULL; 674 cv_signal(&i2c->pcf8584_cv); 675 mutex_exit(&i2c->pcf8584_imutex); 676 677 if ((&plat_shared_i2c_exit != NULL) && (i2c->pcf8584_cur_dip != NULL)) { 678 plat_shared_i2c_exit(i2c->pcf8584_dip); 679 } 680 } 681 682 /* 683 * if pcf8584_b_reg exists, it means the current bus controller signals 684 * are multiplexed into more than a single bus. Select the bus needed 685 * by writing to the mux register. 686 */ 687 static void 688 pcf8584_select_bus(pcf8584_t *i2c) 689 { 690 int bus; 691 pcf8584_ppvt_t *ppvt; 692 693 /* 694 * The existence of pcf8584_b_reg means the bus registers 695 * are multiplexed. 696 */ 697 698 PCF8584_DDB(pcf8584_print(PRT_SELECT, "bus multiplex: %X\n", 699 i2c->pcf8584_b_reg)); 700 if (i2c->pcf8584_b_reg != NULL) { 701 ppvt = ddi_get_parent_data(i2c->pcf8584_cur_dip); 702 703 bus = ppvt->pcf8584_ppvt_bus; 704 705 PCF8584_DDB(pcf8584_print(PRT_SELECT, 706 "transmitting bus number %d\n", bus)); 707 708 ddi_put8(i2c->pcf8584_b_rhandle, i2c->pcf8584_b_reg, bus); 709 } 710 } 711 712 /* 713 * pcf8584_type_to_state() converts a transfer type to the 714 * next state of the I2C state machine based on the requested 715 * transfer type. 716 */ 717 static enum tran_state 718 pcf8584_type_to_state(int i2c_flags) 719 { 720 switch (i2c_flags) { 721 case I2C_WR: 722 723 return (TRAN_STATE_WR); 724 case I2C_RD: 725 726 return (TRAN_STATE_DUMMY_RD); 727 case I2C_WR_RD: 728 729 return (TRAN_STATE_WR_RD); 730 } 731 /*NOTREACHED*/ 732 } 733 734 /* 735 * pcf8584_put_s1() writes out cmd to register S1. 736 */ 737 static void 738 pcf8584_put_s1(pcf8584_t *i2c, char cmd) 739 { 740 ddi_acc_handle_t hp = i2c->pcf8584_rhandle; 741 pcf8584_regs_t *rp = &i2c->pcf8584_regs; 742 743 ddi_put8(hp, rp->pcf8584_regs_s1, cmd); 744 /* 745 * read status to make sure write is flushed 746 */ 747 (void) ddi_get8(hp, rp->pcf8584_regs_s1); 748 } 749 750 /* 751 * pcf8584_put_s0() writes out data to register S0. 752 */ 753 static void 754 pcf8584_put_s0(pcf8584_t *i2c, char data) 755 { 756 ddi_acc_handle_t hp = i2c->pcf8584_rhandle; 757 pcf8584_regs_t *rp = &i2c->pcf8584_regs; 758 759 ddi_put8(hp, rp->pcf8584_regs_s0, data); 760 /* 761 * read status to make sure write is flushed 762 */ 763 (void) ddi_get8(hp, rp->pcf8584_regs_s1); 764 } 765 766 /* 767 * pcf8584_get_s0() reads from register S0. 768 */ 769 static uint8_t 770 pcf8584_get_s0(pcf8584_t *i2c) 771 { 772 ddi_acc_handle_t hp = i2c->pcf8584_rhandle; 773 pcf8584_regs_t *rp = &i2c->pcf8584_regs; 774 775 return (ddi_get8(hp, rp->pcf8584_regs_s0)); 776 } 777 778 /* 779 * pcf8584_get_s1() reads from register S1. 780 */ 781 static uint8_t 782 pcf8584_get_s1(pcf8584_t *i2c) 783 { 784 ddi_acc_handle_t hp = i2c->pcf8584_rhandle; 785 pcf8584_regs_t *rp = &i2c->pcf8584_regs; 786 787 return (ddi_get8(hp, rp->pcf8584_regs_s1)); 788 } 789 790 /* 791 * If the previous transaction was a write, the stop 792 * bit may not make it out on the wire before 793 * the next transaction startes. And unfortunately, there 794 * is no interrupt after the stop bit is written, so this 795 * function will poll to make sure the BBC is ready. 796 */ 797 static int 798 pcf8584_bbn_ready(pcf8584_t *i2c) 799 { 800 uint8_t s1; 801 int usecwaits = 0; 802 803 s1 = pcf8584_get_s1(i2c); 804 805 while ((s1 & S1_BBN) == 0) { 806 807 if (usecwaits++ == 100) { 808 /* Try initializing the bus */ 809 pcf8584_monitor_mode(i2c); 810 pcf8584_put_s1(i2c, S1_STOP); 811 delay(1); 812 pcf8584_init(i2c); 813 (void) pcf8584_get_s0(i2c); 814 s1 = pcf8584_get_s1(i2c); 815 if (s1 & S1_BBN) { 816 cmn_err(CE_WARN, 817 "!%s: cleared bus busy. addr=0x%x", 818 i2c->pcf8584_name, 819 pcf8584_dip_to_addr(i2c->pcf8584_cur_dip)); 820 821 return (I2C_SUCCESS); 822 } else { 823 cmn_err(CE_WARN, 824 "!%s bus busy after init addr=0x%x", 825 i2c->pcf8584_name, 826 pcf8584_dip_to_addr(i2c->pcf8584_cur_dip)); 827 828 return (I2C_FAILURE); 829 } 830 } 831 drv_usecwait(1); 832 s1 = pcf8584_get_s1(i2c); 833 } 834 835 return (I2C_SUCCESS); 836 } 837 838 static int 839 pcf8584_error(int status, uint8_t rdwr, pcf8584_t *i2c) 840 { 841 int addr = pcf8584_dip_to_addr(i2c->pcf8584_cur_dip); 842 pcf8584_regs_t *rp = &i2c->pcf8584_regs; 843 844 if (status & S1_BER) { 845 cmn_err(CE_WARN, 846 "!%s bus error; Controller = 0x%x " 847 " addr = 0x%x", i2c->pcf8584_name, 848 (unsigned int)rp->pcf8584_regs_s1, addr); 849 pcf8584_init(i2c); 850 851 return (I2C_FAILURE); 852 } else if (status & S1_LAB) { 853 cmn_err(CE_WARN, "!%s lost arbitration; Controller =" 854 " 0x%x addr = 0x%x", i2c->pcf8584_name, 855 (unsigned int)rp->pcf8584_regs_s1, addr); 856 pcf8584_init(i2c); 857 858 return (I2C_FAILURE); 859 } else if ((status & S1_LRB) && (rdwr == I2C_WR)) { 860 /* 861 * No error logged here, because this may be benign. 862 * Cf. the "Alert Response Address" feature of SMBUS. 863 */ 864 pcf8584_put_s1(i2c, S1_STOP); 865 866 return (I2C_FAILURE); 867 } 868 869 return (I2C_SUCCESS); 870 } 871 872 static void 873 pcf8584_monitor_mode(pcf8584_t *i2c) 874 { 875 pcf8584_put_s1(i2c, S1_PIN); 876 877 pcf8584_put_s0(i2c, MONITOR_ADDRESS); 878 } 879 880 static int 881 pcf8584_initchild(dev_info_t *cdip) 882 { 883 int32_t cell_size; 884 int len; 885 int32_t regs[2]; 886 int err; 887 pcf8584_ppvt_t *ppvt; 888 char name[30]; 889 890 PCF8584_DDB(pcf8584_print(PRT_INIT, "pcf8584_initchild enter: %s\n", 891 ddi_node_name(cdip))); 892 893 ppvt = kmem_alloc(sizeof (pcf8584_ppvt_t), KM_SLEEP); 894 895 len = sizeof (cell_size); 896 err = ddi_getlongprop_buf(DDI_DEV_T_ANY, cdip, 897 DDI_PROP_CANSLEEP, "#address-cells", 898 (caddr_t)&cell_size, &len); 899 if (err != DDI_PROP_SUCCESS || len != sizeof (cell_size)) { 900 901 return (DDI_FAILURE); 902 } 903 904 len = sizeof (regs); 905 err = ddi_getlongprop_buf(DDI_DEV_T_ANY, cdip, 906 DDI_PROP_DONTPASS | DDI_PROP_CANSLEEP, 907 "reg", (caddr_t)regs, &len); 908 if (err != DDI_PROP_SUCCESS || 909 len != (cell_size * sizeof (int32_t))) { 910 911 return (DDI_FAILURE); 912 } 913 914 if (cell_size == 1) { 915 ppvt->pcf8584_ppvt_addr = regs[0]; 916 (void) sprintf(name, "%x", regs[0]); 917 } else if (cell_size == 2) { 918 ppvt->pcf8584_ppvt_bus = regs[0]; 919 ppvt->pcf8584_ppvt_addr = regs[1]; 920 (void) sprintf(name, "%x,%x", regs[0], regs[1]); 921 } else { 922 923 return (DDI_FAILURE); 924 } 925 926 ddi_set_parent_data(cdip, ppvt); 927 928 ddi_set_name_addr(cdip, name); 929 930 PCF8584_DDB(pcf8584_print(PRT_INIT, 931 "pcf8584_initchild SUCCESS: %s\n", ddi_node_name(cdip))); 932 933 return (DDI_SUCCESS); 934 } 935 936 static void 937 pcf8584_uninitchild(dev_info_t *cdip) 938 { 939 pcf8584_ppvt_t *ppvt; 940 941 ppvt = ddi_get_parent_data(cdip); 942 kmem_free(ppvt, sizeof (pcf8584_ppvt_t)); 943 944 ddi_set_parent_data(cdip, NULL); 945 ddi_set_name_addr(cdip, NULL); 946 947 PCF8584_DDB(pcf8584_print(PRT_INIT, "i2c_uninitchild: %s\n", 948 ddi_node_name(cdip))); 949 } 950 951 static void 952 pcf8584_init(pcf8584_t *i2c) 953 { 954 uint8_t clk_div = 0x1C; 955 956 pcf8584_put_s1(i2c, S1_PIN); 957 958 pcf8584_put_s0(i2c, S0_OWN); 959 960 pcf8584_put_s1(i2c, S1_PIN | S1_ES1); 961 962 /* 963 * The default case is to set the clock divisor to the least common 964 * denominator to avoid over clocking the I2C bus. Assume that 965 * BBC based systems are using the Safari clock as input, so select 966 * the clk divisor based on it. 967 */ 968 if (strcmp(ddi_binding_name(i2c->pcf8584_dip), "SUNW,bbc-i2c") == 0) { 969 dev_info_t *root_node; 970 int clock_freq; 971 root_node = ddi_root_node(); 972 clock_freq = ddi_prop_get_int(DDI_DEV_T_ANY, root_node, 973 DDI_PROP_DONTPASS, "clock-frequency", 0); 974 975 if (clock_freq < 105000000) { 976 clk_div = 0x00; 977 } else if (clock_freq < 160000000) { 978 clk_div = 0x10; 979 } else { 980 clk_div = 0x1C; 981 } 982 } 983 984 /* set I2C clock speed */ 985 pcf8584_put_s0(i2c, clk_div); 986 987 pcf8584_put_s1(i2c, S1_PIN | S1_ESO | S1_ACK); 988 989 /* 990 * Multi-Master: Wait for a period of time equal to the 991 * longest I2C message. This accounts for the case 992 * where multiple controllers and, if this particular one 993 * is "lagging", misses the BB(bus busy) condition. 994 * We wait 200 ms since the longest transaction at this time 995 * on the i2c bus is a 256 byte read from the seprom which takes 996 * about 75 ms. Some additional buffer does no harm to the driver. 997 */ 998 999 delay(drv_usectohz(PCF8584_INIT_WAIT)); 1000 } 1001 1002 /* 1003 * pcf8584_setup_regs() is called to map in registers specific to 1004 * the pcf8584. 1005 */ 1006 static int 1007 pcf8584_setup_regs(dev_info_t *dip, pcf8584_t *i2c) 1008 { 1009 int nregs; 1010 ddi_device_acc_attr_t attr; 1011 caddr_t reg_base; 1012 1013 attr.devacc_attr_version = DDI_DEVICE_ATTR_V0; 1014 attr.devacc_attr_endian_flags = DDI_STRUCTURE_LE_ACC; 1015 attr.devacc_attr_dataorder = DDI_STRICTORDER_ACC; 1016 1017 if (ddi_dev_nregs(dip, &nregs) != DDI_SUCCESS) { 1018 1019 return (DDI_FAILURE); 1020 } 1021 1022 if (ddi_regs_map_setup(dip, 0, 1023 (caddr_t *)®_base, 0, 0, &attr, 1024 &i2c->pcf8584_rhandle) != DDI_SUCCESS) { 1025 1026 return (DDI_FAILURE); 1027 } 1028 1029 /* 1030 * If i2c controller is on BBC, then s1 comes before s0. 1031 */ 1032 if (strcmp(ddi_binding_name(dip), "SUNW,bbc-i2c") == 0) { 1033 i2c->pcf8584_regs.pcf8584_regs_s0 = 1034 (uint8_t *)®_base[1]; 1035 i2c->pcf8584_regs.pcf8584_regs_s1 = 1036 (uint8_t *)®_base[0]; 1037 } else { 1038 i2c->pcf8584_regs.pcf8584_regs_s0 = 1039 (uint8_t *)®_base[0]; 1040 i2c->pcf8584_regs.pcf8584_regs_s1 = 1041 (uint8_t *)®_base[1]; 1042 } 1043 1044 if (nregs > 1) { 1045 if (ddi_regs_map_setup(dip, 1046 1, (caddr_t *)&i2c->pcf8584_b_reg, 1047 0, 0, &attr, &i2c->pcf8584_b_rhandle) != 1048 DDI_SUCCESS) { 1049 1050 return (DDI_FAILURE); 1051 } 1052 } 1053 1054 return (DDI_SUCCESS); 1055 } 1056 1057 /* 1058 * pcf8584_free_regs() frees any registers previously 1059 * allocated. 1060 */ 1061 static void 1062 pcf8584_free_regs(pcf8584_t *i2c) 1063 { 1064 if (i2c->pcf8584_regs.pcf8584_regs_s0 != NULL) { 1065 ddi_regs_map_free(&i2c->pcf8584_rhandle); 1066 } 1067 if (i2c->pcf8584_b_reg != NULL) { 1068 ddi_regs_map_free(&i2c->pcf8584_b_rhandle); 1069 } 1070 } 1071 1072 static void 1073 pcf8584_reportdev(dev_info_t *dip, dev_info_t *rdip) 1074 { 1075 pcf8584_ppvt_t *ppvt; 1076 1077 ppvt = ddi_get_parent_data(rdip); 1078 1079 cmn_err(CE_CONT, "?%s%d at %s%d: addr 0x%x", 1080 ddi_driver_name(rdip), ddi_get_instance(rdip), 1081 ddi_driver_name(dip), ddi_get_instance(dip), 1082 ppvt->pcf8584_ppvt_addr); 1083 } 1084 1085 /* 1086 * i2_nexus_dip_to_addr() takes a dip and returns an I2C address. 1087 */ 1088 static int 1089 pcf8584_dip_to_addr(dev_info_t *dip) 1090 { 1091 pcf8584_ppvt_t *ppvt; 1092 1093 ppvt = ddi_get_parent_data(dip); 1094 1095 return (ppvt->pcf8584_ppvt_addr); 1096 } 1097 1098 /* 1099 * pcf8584_intr() is the interrupt service routine registered during 1100 * attach, and remains registered even if the driver is in POLLED mode. So if 1101 * this is called from POLLED mode, it needs to return without doing 1102 * any work to prevent the I2C bus from entering an unknown state. 1103 */ 1104 static uint_t 1105 pcf8584_intr(caddr_t arg) 1106 { 1107 pcf8584_t *i2c = (pcf8584_t *)arg; 1108 uint8_t s1; 1109 1110 PCF8584_DDB(pcf8584_print(PRT_INTR, "pcf8584_intr: enter\n")); 1111 1112 mutex_enter(&i2c->pcf8584_imutex); 1113 1114 /* 1115 * It is necessary to check both whether the hardware is interrupting 1116 * and that there is a current transaction for the bus in progress. 1117 * Checking just one but not the other will lead to a panic on xcal 1118 * since both controllers share the same ino, and also because OBP 1119 * shares a controller with the kernel even while the kernel is running. 1120 */ 1121 1122 if (i2c->pcf8584_cur_tran == NULL) { 1123 mutex_exit(&i2c->pcf8584_imutex); 1124 1125 return (DDI_INTR_UNCLAIMED); 1126 } 1127 1128 1129 s1 = pcf8584_get_s1(i2c); 1130 if (s1 & S1_PIN) { 1131 mutex_exit(&i2c->pcf8584_imutex); 1132 1133 return (DDI_INTR_UNCLAIMED); 1134 } 1135 1136 if (pcf8584_process(i2c, s1) == I2C_COMPLETE) { 1137 i2c->pcf8584_tran_state = TRAN_STATE_NULL; 1138 i2c->pcf8584_cur_status = PCF8584_TRANSFER_OVER; 1139 cv_signal(&i2c->pcf8584_icv); 1140 } else 1141 i2c->pcf8584_cur_status = PCF8584_TRANSFER_ON; 1142 1143 mutex_exit(&i2c->pcf8584_imutex); 1144 1145 return (DDI_INTR_CLAIMED); 1146 } 1147 1148 /* 1149 * Interrupt occurs after a byte is transmitted or received, indicating 1150 * the device is ready to be serviced. 1151 */ 1152 static int 1153 pcf8584_process(pcf8584_t *i2c, uint8_t s1) 1154 { 1155 i2c_transfer_t *tp = i2c->pcf8584_cur_tran; 1156 int addr = pcf8584_dip_to_addr(i2c->pcf8584_cur_dip); 1157 int dummy_read; 1158 1159 ASSERT(i2c->pcf8584_tran_state != TRAN_STATE_NULL); 1160 1161 switch (i2c->pcf8584_tran_state) { 1162 case TRAN_STATE_DUMMY_DATA: 1163 PCF8584_DDB(pcf8584_print(PRT_TRAN, 1164 "TRAN_STATE_DUMMY DATA: write dummy %x\n", DUMMY_DATA)); 1165 if (pcf8584_error(s1, I2C_RD, i2c) != I2C_SUCCESS) { 1166 tp->i2c_result = I2C_FAILURE; 1167 1168 return (I2C_COMPLETE); 1169 } 1170 i2c->pcf8584_tran_state = TRAN_STATE_START; 1171 pcf8584_put_s0(i2c, DUMMY_DATA); 1172 1173 return (I2C_PENDING); 1174 case TRAN_STATE_START: 1175 if (pcf8584_error(s1, I2C_RD, i2c) != I2C_SUCCESS) { 1176 PCF8584_DDB(pcf8584_print(PRT_TRAN, 1177 "TRAN_STATE_START failure\n")); 1178 tp->i2c_result = I2C_FAILURE; 1179 1180 return (I2C_COMPLETE); 1181 } 1182 i2c->pcf8584_tran_state = 1183 pcf8584_type_to_state(tp->i2c_flags); 1184 1185 /* Set read bit if this is a read transaction */ 1186 if (tp->i2c_flags == I2C_RD) { 1187 addr |= I2C_READ; 1188 } 1189 pcf8584_put_s1(i2c, S1_START2 | S1_ENI); 1190 pcf8584_put_s0(i2c, addr); 1191 PCF8584_DDB(pcf8584_print(PRT_TRAN, 1192 "TRAN_STATE_START: write addr: %x\n", addr)); 1193 1194 return (I2C_PENDING); 1195 case TRAN_STATE_WR: 1196 1197 if (pcf8584_error(s1, I2C_WR, i2c) != I2C_SUCCESS) { 1198 PCF8584_DDB(pcf8584_print(PRT_TRAN, 1199 "TRAN_STATE_WR failure\n")); 1200 tp->i2c_result = I2C_FAILURE; 1201 1202 return (I2C_COMPLETE); 1203 } 1204 /* check to see if at end of buffer */ 1205 if (tp->i2c_w_resid == 0) { 1206 pcf8584_put_s1(i2c, S1_STOP); 1207 PCF8584_DDB(pcf8584_print(PRT_TRAN, 1208 "TRAN_STATE_WR: write STOP\n")); 1209 1210 return (I2C_COMPLETE); 1211 } 1212 1213 pcf8584_put_s0(i2c, tp->i2c_wbuf[tp->i2c_wlen - 1214 tp->i2c_w_resid--]); 1215 PCF8584_DDB(pcf8584_print(PRT_TRAN, 1216 "TRAN_STATE_WR: write data %x\n", 1217 tp->i2c_wbuf[tp->i2c_wlen - (tp->i2c_w_resid + 1)])); 1218 1219 return (I2C_PENDING); 1220 case TRAN_STATE_DUMMY_RD: 1221 1222 if (pcf8584_error(s1, I2C_WR, i2c) != I2C_SUCCESS) { 1223 tp->i2c_result = I2C_FAILURE; 1224 1225 return (I2C_COMPLETE); 1226 } 1227 /* 1228 * The first read is always a dummy read, because reading S0 1229 * is what starts bit shifting and ACK on the I2c bus. 1230 * This byte is accessed during the next read, which starts 1231 * another 8 bit bus shift. 1232 * 1233 * special case for 1 byte reads: Clear the ACK bit 1234 * here since this read causes the last and only byte 1235 * to be sent on the I2C bus. 1236 */ 1237 if (tp->i2c_r_resid == 1) { 1238 pcf8584_put_s1(i2c, S1_ESO | S1_ENI); 1239 } 1240 1241 /* 1242 * dummy read 1243 */ 1244 dummy_read = pcf8584_get_s0(i2c); 1245 1246 i2c->pcf8584_tran_state = TRAN_STATE_RD; 1247 PCF8584_DDB(pcf8584_print(PRT_TRAN, 1248 "TRAN_STATE_DUMMY_RD: read dummy %d\n", dummy_read)); 1249 1250 return (I2C_PENDING); 1251 case TRAN_STATE_RD: 1252 if (pcf8584_error(s1, I2C_RD, i2c) != I2C_SUCCESS) { 1253 tp->i2c_result = I2C_FAILURE; 1254 PCF8584_DDB(pcf8584_print(PRT_TRAN, 1255 "TRAN_STATE_RD failure\n")); 1256 1257 return (I2C_COMPLETE); 1258 } 1259 1260 /* 1261 * If resid == 1, the last byte has already been shifted into 1262 * the accumulator. Send the stop bit. This also prevents the 1263 * last S0 read from shifting in another byte from the I2C bus. 1264 */ 1265 if (tp->i2c_r_resid == 1) { 1266 pcf8584_put_s1(i2c, S1_STOP); 1267 } 1268 1269 /* 1270 * If resid == 2, then the next read will cause the I2C bus to 1271 * start shifting in the last byte on the I2C bus, which we 1272 * don't want to be ACK'd, so clear the ACK bit. 1273 */ 1274 if (tp->i2c_r_resid == 2) { 1275 pcf8584_put_s1(i2c, S1_ESO | S1_ENI); 1276 } 1277 1278 tp->i2c_rbuf[tp->i2c_rlen - tp->i2c_r_resid] = 1279 pcf8584_get_s0(i2c); 1280 1281 PCF8584_DDB(pcf8584_print(PRT_TRAN, 1282 "TRAN_STATE_RD: returning. i2c_rlen = %d " 1283 "i2c_r_resid = %d, data =%x\n", tp->i2c_rlen, 1284 tp->i2c_r_resid, tp->i2c_rbuf[tp->i2c_rlen - 1285 tp->i2c_r_resid])); 1286 1287 if (--tp->i2c_r_resid == 0) { 1288 1289 return (I2C_COMPLETE); 1290 } 1291 1292 return (I2C_PENDING); 1293 case TRAN_STATE_WR_RD: 1294 1295 if (pcf8584_error(s1, I2C_WR, i2c) != I2C_SUCCESS) { 1296 tp->i2c_result = I2C_FAILURE; 1297 1298 return (I2C_COMPLETE); 1299 } 1300 if ((s1 & S1_LRB)) { 1301 pcf8584_put_s1(i2c, S1_STOP); 1302 PCF8584_DDB(pcf8584_print(PRT_TRAN, 1303 "TRAN_STATE_WR_RD sending STOP\n")); 1304 1305 return (I2C_COMPLETE); 1306 } 1307 if (tp->i2c_w_resid != 0) { 1308 pcf8584_put_s0(i2c, tp->i2c_wbuf[tp->i2c_wlen - 1309 tp->i2c_w_resid--]); 1310 PCF8584_DDB(pcf8584_print(PRT_TRAN, 1311 "TRAN_STATE_WR_RD: write data %x\n", 1312 tp->i2c_wbuf[tp->i2c_wlen - 1313 (tp->i2c_w_resid + 1)])); 1314 } else { 1315 pcf8584_put_s1(i2c, S1_START2 | S1_ENI); 1316 pcf8584_put_s0(i2c, addr | I2C_READ); 1317 i2c->pcf8584_tran_state = 1318 TRAN_STATE_DUMMY_RD; 1319 PCF8584_DDB(pcf8584_print(PRT_TRAN, 1320 "TRAN_STATE_WR_RD: write addr " 1321 "%x\n", addr | I2C_READ)); 1322 } 1323 1324 return (I2C_PENDING); 1325 default: 1326 1327 return (I2C_COMPLETE); 1328 } 1329 } 1330 1331 /* 1332 * pcf8584_transfer() is the function that is registered with 1333 * I2C services to be called from pcf8584_transfer() for each transfer. 1334 * 1335 * This function starts the transfer, and then waits for the 1336 * interrupt or polled thread to signal that the transfer has 1337 * completed. 1338 */ 1339 int 1340 pcf8584_transfer(dev_info_t *dip, i2c_transfer_t *tp) 1341 { 1342 pcf8584_t *i2c; 1343 int saved_mode, took_over = 0; 1344 kcondvar_t *waiter = NULL; 1345 extern int do_polled_io; 1346 1347 i2c = (pcf8584_t *)ddi_get_soft_state(pcf8584_state, 1348 ddi_get_instance(ddi_get_parent(dip))); 1349 1350 tp->i2c_r_resid = tp->i2c_rlen; 1351 tp->i2c_w_resid = tp->i2c_wlen; 1352 tp->i2c_result = I2C_SUCCESS; 1353 1354 begin: 1355 /* 1356 * If we're explicitly asked to do polled io (or if we are panic'ing), 1357 * we need to usurp ownership of the I2C bus, bypassing any other 1358 * waiters. 1359 */ 1360 if ((do_polled_io || ddi_in_panic()) && (getpil() >= pcf8584_pil)) { 1361 pcf8584_take_over(i2c, dip, tp, &waiter, &saved_mode); 1362 took_over = 1; 1363 } else { 1364 pcf8584_acquire(i2c, dip, tp, B_FALSE); 1365 mutex_enter(&i2c->pcf8584_imutex); 1366 1367 /* 1368 * See if someone else had intruded and taken over the bus 1369 * between the 'pcf8584_acquire' and 'mutex_enter' above. 1370 * If so, we'll have to start all over again. 1371 */ 1372 if (i2c->pcf8584_cur_tran != tp) { 1373 mutex_exit(&i2c->pcf8584_imutex); 1374 goto begin; 1375 } 1376 } 1377 1378 if (pcf8584_bbn_ready(i2c) != I2C_SUCCESS) { 1379 if (took_over) 1380 pcf8584_give_up(i2c, waiter, saved_mode); 1381 else { 1382 mutex_exit(&i2c->pcf8584_imutex); 1383 pcf8584_release(i2c, B_FALSE); 1384 } 1385 1386 return (tp->i2c_result = I2C_FAILURE); 1387 } 1388 1389 /* 1390 * Bus selection must be followed by pcf8584_bbn_ready(), 1391 * otherwise the bus can be switched before the stop 1392 * bit is written out, causing the stop bit to get 1393 * sent to the wrong (new) bus. This causes the 1394 * previous bus to permanently hang waiting for the 1395 * stop bit. 1396 */ 1397 pcf8584_select_bus(i2c); 1398 1399 i2c->pcf8584_tran_state = TRAN_STATE_DUMMY_DATA; 1400 pcf8584_put_s0(i2c, DUMMY_ADDR); 1401 PCF8584_DDB(pcf8584_print(PRT_TRAN, 1402 "FIRST WRITE DUMMY ADDR: write %x\n", DUMMY_ADDR)); 1403 pcf8584_put_s1(i2c, S1_START | S1_ENI); 1404 1405 /* 1406 * Update transfer status so any polled i/o request coming in 1407 * after this will complete this transfer for us, before issuing 1408 * its own. 1409 */ 1410 i2c->pcf8584_cur_status = PCF8584_TRANSFER_ON; 1411 1412 if (i2c->pcf8584_mode == PCF8584_POLL_MODE) 1413 pcf8584_do_polled_io(i2c); 1414 1415 if (took_over) 1416 pcf8584_give_up(i2c, waiter, saved_mode); 1417 else { 1418 if (i2c->pcf8584_mode != PCF8584_POLL_MODE) 1419 cv_wait(&i2c->pcf8584_icv, &i2c->pcf8584_imutex); 1420 mutex_exit(&i2c->pcf8584_imutex); 1421 1422 /* 1423 * Release the I2C bus only if we still own it. If we don't 1424 * own it (someone usurped it from us while we were waiting), 1425 * we still need to drop the lock that serializes access to 1426 * the pcf8584 controller on systems where OBP shares the 1427 * controller with the OS. 1428 */ 1429 if (i2c->pcf8584_cur_tran == tp) 1430 pcf8584_release(i2c, B_FALSE); 1431 else if (&plat_shared_i2c_exit && dip) 1432 plat_shared_i2c_exit(i2c->pcf8584_dip); 1433 } 1434 1435 return (tp->i2c_result); 1436 } 1437 1438 static void 1439 pcf8584_do_polled_io(pcf8584_t *i2c) 1440 { 1441 int completed = I2C_PENDING; 1442 uint8_t s1; 1443 1444 while (completed != I2C_COMPLETE) { 1445 s1 = pcf8584_get_s1(i2c); 1446 if (!(s1 & S1_PIN)) { 1447 ASSERT(i2c->pcf8584_cur_tran); 1448 completed = pcf8584_process(i2c, s1); 1449 } 1450 drv_usecwait(1); 1451 } 1452 1453 i2c->pcf8584_cur_status = PCF8584_TRANSFER_OVER; 1454 } 1455 1456 /* 1457 * pcf8584_take_over() grabs the I2C bus and other resources by force and 1458 * flushes any pending transaction. This is called if a polled i/o 1459 * request comes in. 1460 */ 1461 static void 1462 pcf8584_take_over(pcf8584_t *i2c, dev_info_t *dip, i2c_transfer_t *tp, 1463 kcondvar_t **waiter, int *saved_mode) 1464 { 1465 mutex_enter(&i2c->pcf8584_imutex); 1466 1467 /* 1468 * We need to flush out any currently pending transaction before 1469 * issuing ours. 1470 */ 1471 if (i2c->pcf8584_busy) { 1472 if (i2c->pcf8584_cur_tran && 1473 i2c->pcf8584_cur_status == PCF8584_TRANSFER_ON) { 1474 pcf8584_do_polled_io(i2c); 1475 *waiter = &i2c->pcf8584_icv; 1476 } 1477 } 1478 1479 /* 1480 * Since pcf8584_acquire() is by default a good citizen that 1481 * will wait its turn to acquire the I2C bus, we need to set 1482 * the 'force' flag on. 1483 */ 1484 pcf8584_acquire(i2c, dip, tp, B_TRUE); 1485 1486 *saved_mode = i2c->pcf8584_mode; 1487 i2c->pcf8584_mode = PCF8584_POLL_MODE; 1488 } 1489 1490 /* 1491 * pcf8584_give_up() returns all resources that were taken over forcefully 1492 */ 1493 static void 1494 pcf8584_give_up(pcf8584_t *i2c, kcondvar_t *waiter, int saved_mode) 1495 { 1496 i2c->pcf8584_mode = saved_mode; 1497 1498 /* 1499 * Note that pcf8584_release only wakes up threads waiting to acquire 1500 * the I2C bus. We still need to wake up the waiter from whom we 1501 * usurped the bus. 1502 */ 1503 pcf8584_release(i2c, B_TRUE); 1504 if (waiter) 1505 cv_signal(waiter); 1506 1507 mutex_exit(&i2c->pcf8584_imutex); 1508 } 1509