1 /* 2 * Copyright (c) 2014 The DragonFly Project. All rights reserved. 3 * 4 * This code is derived from software contributed to The DragonFly Project 5 * by Matthew Dillon <dillon@backplane.com> and was subsequently ported 6 * to FreeBSD by Michael Gmelin <freebsd@grem.de> 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in 16 * the documentation and/or other materials provided with the 17 * distribution. 18 * 3. Neither the name of The DragonFly Project nor the names of its 19 * contributors may be used to endorse or promote products derived 20 * from this software without specific, prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 23 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 25 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 26 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 27 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 29 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 30 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 31 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 32 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 */ 35 36 #include <sys/cdefs.h> 37 __FBSDID("$FreeBSD$"); 38 39 /* 40 * Intel fourth generation mobile cpus integrated I2C device, smbus driver. 41 * 42 * See ig4_reg.h for datasheet reference and notes. 43 */ 44 45 #include <sys/param.h> 46 #include <sys/systm.h> 47 #include <sys/kernel.h> 48 #include <sys/module.h> 49 #include <sys/errno.h> 50 #include <sys/lock.h> 51 #include <sys/mutex.h> 52 #include <sys/syslog.h> 53 #include <sys/bus.h> 54 #include <sys/sysctl.h> 55 56 #include <machine/bus.h> 57 #include <sys/rman.h> 58 59 #include <dev/pci/pcivar.h> 60 #include <dev/pci/pcireg.h> 61 #include <dev/smbus/smbconf.h> 62 63 #include <dev/ichiic/ig4_reg.h> 64 #include <dev/ichiic/ig4_var.h> 65 66 #define TRANS_NORMAL 1 67 #define TRANS_PCALL 2 68 #define TRANS_BLOCK 3 69 70 static void ig4iic_start(void *xdev); 71 static void ig4iic_intr(void *cookie); 72 static void ig4iic_dump(ig4iic_softc_t *sc); 73 74 static int ig4_dump; 75 SYSCTL_INT(_debug, OID_AUTO, ig4_dump, CTLTYPE_INT | CTLFLAG_RW, 76 &ig4_dump, 0, ""); 77 78 /* 79 * Low-level inline support functions 80 */ 81 static __inline void 82 reg_write(ig4iic_softc_t *sc, uint32_t reg, uint32_t value) 83 { 84 bus_write_4(sc->regs_res, reg, value); 85 bus_barrier(sc->regs_res, reg, 4, BUS_SPACE_BARRIER_WRITE); 86 } 87 88 static __inline uint32_t 89 reg_read(ig4iic_softc_t *sc, uint32_t reg) 90 { 91 uint32_t value; 92 93 bus_barrier(sc->regs_res, reg, 4, BUS_SPACE_BARRIER_READ); 94 value = bus_read_4(sc->regs_res, reg); 95 return (value); 96 } 97 98 /* 99 * Enable or disable the controller and wait for the controller to acknowledge 100 * the state change. 101 */ 102 static int 103 set_controller(ig4iic_softc_t *sc, uint32_t ctl) 104 { 105 int retry; 106 int error; 107 uint32_t v; 108 109 reg_write(sc, IG4_REG_I2C_EN, ctl); 110 error = SMB_ETIMEOUT; 111 112 for (retry = 100; retry > 0; --retry) { 113 v = reg_read(sc, IG4_REG_ENABLE_STATUS); 114 if (((v ^ ctl) & IG4_I2C_ENABLE) == 0) { 115 error = 0; 116 break; 117 } 118 mtx_sleep(sc, &sc->mutex, 0, "i2cslv", 1); 119 } 120 return (error); 121 } 122 123 /* 124 * Wait up to 25ms for the requested status using a 25uS polling loop. 125 */ 126 static int 127 wait_status(ig4iic_softc_t *sc, uint32_t status) 128 { 129 uint32_t v; 130 int error; 131 int txlvl = -1; 132 u_int count_us = 0; 133 u_int limit_us = 25000; /* 25ms */ 134 135 error = SMB_ETIMEOUT; 136 137 for (;;) { 138 /* 139 * Check requested status 140 */ 141 v = reg_read(sc, IG4_REG_I2C_STA); 142 if (v & status) { 143 error = 0; 144 break; 145 } 146 147 /* 148 * When waiting for receive data break-out if the interrupt 149 * loaded data into the FIFO. 150 */ 151 if (status & IG4_STATUS_RX_NOTEMPTY) { 152 if (sc->rpos != sc->rnext) { 153 error = 0; 154 break; 155 } 156 } 157 158 /* 159 * When waiting for the transmit FIFO to become empty, 160 * reset the timeout if we see a change in the transmit 161 * FIFO level as progress is being made. 162 */ 163 if (status & IG4_STATUS_TX_EMPTY) { 164 v = reg_read(sc, IG4_REG_TXFLR) & IG4_FIFOLVL_MASK; 165 if (txlvl != v) { 166 txlvl = v; 167 count_us = 0; 168 } 169 } 170 171 /* 172 * Stop if we've run out of time. 173 */ 174 if (count_us >= limit_us) 175 break; 176 177 /* 178 * When waiting for receive data let the interrupt do its 179 * work, otherwise poll with the lock held. 180 */ 181 if (status & IG4_STATUS_RX_NOTEMPTY) { 182 mtx_sleep(sc, &sc->mutex, PZERO, "i2cwait", 183 (hz + 99) / 100); /* sleep up to 10ms */ 184 count_us += 10000; 185 } else { 186 DELAY(25); 187 count_us += 25; 188 } 189 } 190 191 return (error); 192 } 193 194 /* 195 * Read I2C data. The data might have already been read by 196 * the interrupt code, otherwise it is sitting in the data 197 * register. 198 */ 199 static uint8_t 200 data_read(ig4iic_softc_t *sc) 201 { 202 uint8_t c; 203 204 if (sc->rpos == sc->rnext) { 205 c = (uint8_t)reg_read(sc, IG4_REG_DATA_CMD); 206 } else { 207 c = sc->rbuf[sc->rpos & IG4_RBUFMASK]; 208 ++sc->rpos; 209 } 210 return (c); 211 } 212 213 /* 214 * Set the slave address. The controller must be disabled when 215 * changing the address. 216 * 217 * This operation does not issue anything to the I2C bus but sets 218 * the target address for when the controller later issues a START. 219 */ 220 static void 221 set_slave_addr(ig4iic_softc_t *sc, uint8_t slave, int trans_op) 222 { 223 uint32_t tar; 224 uint32_t ctl; 225 int use_10bit; 226 227 use_10bit = sc->use_10bit; 228 if (trans_op & SMB_TRANS_7BIT) 229 use_10bit = 0; 230 if (trans_op & SMB_TRANS_10BIT) 231 use_10bit = 1; 232 233 if (sc->slave_valid && sc->last_slave == slave && 234 sc->use_10bit == use_10bit) { 235 return; 236 } 237 sc->use_10bit = use_10bit; 238 239 /* 240 * Wait for TXFIFO to drain before disabling the controller. 241 * 242 * If a write message has not been completed it's really a 243 * programming error, but for now in that case issue an extra 244 * byte + STOP. 245 * 246 * If a read message has not been completed it's also a programming 247 * error, for now just ignore it. 248 */ 249 wait_status(sc, IG4_STATUS_TX_NOTFULL); 250 if (sc->write_started) { 251 reg_write(sc, IG4_REG_DATA_CMD, IG4_DATA_STOP); 252 sc->write_started = 0; 253 } 254 if (sc->read_started) 255 sc->read_started = 0; 256 wait_status(sc, IG4_STATUS_TX_EMPTY); 257 258 set_controller(sc, 0); 259 ctl = reg_read(sc, IG4_REG_CTL); 260 ctl &= ~IG4_CTL_10BIT; 261 ctl |= IG4_CTL_RESTARTEN; 262 263 tar = slave; 264 if (sc->use_10bit) { 265 tar |= IG4_TAR_10BIT; 266 ctl |= IG4_CTL_10BIT; 267 } 268 reg_write(sc, IG4_REG_CTL, ctl); 269 reg_write(sc, IG4_REG_TAR_ADD, tar); 270 set_controller(sc, IG4_I2C_ENABLE); 271 sc->slave_valid = 1; 272 sc->last_slave = slave; 273 } 274 275 /* 276 * Issue START with byte command, possible count, and a variable length 277 * read or write buffer, then possible turn-around read. The read also 278 * has a possible count received. 279 * 280 * For SMBUS - 281 * 282 * Quick: START+ADDR+RD/WR STOP 283 * 284 * Normal: START+ADDR+WR CMD DATA..DATA STOP 285 * 286 * START+ADDR+RD CMD 287 * RESTART+ADDR RDATA..RDATA STOP 288 * (can also be used for I2C transactions) 289 * 290 * Process Call: START+ADDR+WR CMD DATAL DATAH 291 * RESTART+ADDR+RD RDATAL RDATAH STOP 292 * 293 * Block: START+ADDR+RD CMD 294 * RESTART+ADDR+RD RCOUNT DATA... STOP 295 * 296 * START+ADDR+WR CMD 297 * RESTART+ADDR+WR WCOUNT DATA... STOP 298 * 299 * For I2C - basically, no *COUNT fields, possibly no *CMD field. If the 300 * sender needs to issue a 2-byte command it will incorporate it 301 * into the write buffer and also set NOCMD. 302 * 303 * Generally speaking, the START+ADDR / RESTART+ADDR is handled automatically 304 * by the controller at the beginning of a command sequence or on a data 305 * direction turn-around, and we only need to tell it when to issue the STOP. 306 */ 307 static int 308 smb_transaction(ig4iic_softc_t *sc, char cmd, int op, 309 char *wbuf, int wcount, char *rbuf, int rcount, int *actualp) 310 { 311 int error; 312 int unit; 313 uint32_t last; 314 315 /* 316 * Debugging - dump registers 317 */ 318 if (ig4_dump) { 319 unit = device_get_unit(sc->dev); 320 if (ig4_dump & (1 << unit)) { 321 ig4_dump &= ~(1 << unit); 322 ig4iic_dump(sc); 323 } 324 } 325 326 /* 327 * Issue START or RESTART with next data byte, clear any previous 328 * abort condition that may have been holding the txfifo in reset. 329 */ 330 last = IG4_DATA_RESTART; 331 reg_read(sc, IG4_REG_CLR_TX_ABORT); 332 if (actualp) 333 *actualp = 0; 334 335 /* 336 * Issue command if not told otherwise (smbus). 337 */ 338 if ((op & SMB_TRANS_NOCMD) == 0) { 339 error = wait_status(sc, IG4_STATUS_TX_NOTFULL); 340 if (error) 341 goto done; 342 last |= (u_char)cmd; 343 if (wcount == 0 && rcount == 0 && (op & SMB_TRANS_NOSTOP) == 0) 344 last |= IG4_DATA_STOP; 345 reg_write(sc, IG4_REG_DATA_CMD, last); 346 last = 0; 347 } 348 349 /* 350 * Clean out any previously received data. 351 */ 352 if (sc->rpos != sc->rnext && 353 (op & SMB_TRANS_NOREPORT) == 0) { 354 device_printf(sc->dev, 355 "discarding %d bytes of spurious data\n", 356 sc->rnext - sc->rpos); 357 } 358 sc->rpos = 0; 359 sc->rnext = 0; 360 361 /* 362 * If writing and not told otherwise, issue the write count (smbus). 363 */ 364 if (wcount && (op & SMB_TRANS_NOCNT) == 0) { 365 error = wait_status(sc, IG4_STATUS_TX_NOTFULL); 366 if (error) 367 goto done; 368 last |= (u_char)cmd; 369 reg_write(sc, IG4_REG_DATA_CMD, last); 370 last = 0; 371 } 372 373 /* 374 * Bulk write (i2c) 375 */ 376 while (wcount) { 377 error = wait_status(sc, IG4_STATUS_TX_NOTFULL); 378 if (error) 379 goto done; 380 last |= (u_char)*wbuf; 381 if (wcount == 1 && rcount == 0 && (op & SMB_TRANS_NOSTOP) == 0) 382 last |= IG4_DATA_STOP; 383 reg_write(sc, IG4_REG_DATA_CMD, last); 384 --wcount; 385 ++wbuf; 386 last = 0; 387 } 388 389 /* 390 * Issue reads to xmit FIFO (strange, I know) to tell the controller 391 * to clock in data. At the moment just issue one read ahead to 392 * pipeline the incoming data. 393 * 394 * NOTE: In the case of NOCMD and wcount == 0 we still issue a 395 * RESTART here, even if the data direction has not changed 396 * from the previous CHAINing call. This we force the RESTART. 397 * (A new START is issued automatically by the controller in 398 * the other nominal cases such as a data direction change or 399 * a previous STOP was issued). 400 * 401 * If this will be the last byte read we must also issue the STOP 402 * at the end of the read. 403 */ 404 if (rcount) { 405 last = IG4_DATA_RESTART | IG4_DATA_COMMAND_RD; 406 if (rcount == 1 && 407 (op & (SMB_TRANS_NOSTOP | SMB_TRANS_NOCNT)) == 408 SMB_TRANS_NOCNT) { 409 last |= IG4_DATA_STOP; 410 } 411 reg_write(sc, IG4_REG_DATA_CMD, last); 412 last = IG4_DATA_COMMAND_RD; 413 } 414 415 /* 416 * Bulk read (i2c) and count field handling (smbus) 417 */ 418 while (rcount) { 419 /* 420 * Maintain a pipeline by queueing the allowance for the next 421 * read before waiting for the current read. 422 */ 423 if (rcount > 1) { 424 if (op & SMB_TRANS_NOCNT) 425 last = (rcount == 2) ? IG4_DATA_STOP : 0; 426 else 427 last = 0; 428 reg_write(sc, IG4_REG_DATA_CMD, IG4_DATA_COMMAND_RD | 429 last); 430 } 431 error = wait_status(sc, IG4_STATUS_RX_NOTEMPTY); 432 if (error) { 433 if ((op & SMB_TRANS_NOREPORT) == 0) { 434 device_printf(sc->dev, 435 "rx timeout addr 0x%02x\n", 436 sc->last_slave); 437 } 438 goto done; 439 } 440 last = data_read(sc); 441 442 if (op & SMB_TRANS_NOCNT) { 443 *rbuf = (u_char)last; 444 ++rbuf; 445 --rcount; 446 if (actualp) 447 ++*actualp; 448 } else { 449 /* 450 * Handle count field (smbus), which is not part of 451 * the rcount'ed buffer. The first read data in a 452 * bulk transfer is the count. 453 * 454 * XXX if rcount is loaded as 0 how do I generate a 455 * STOP now without issuing another RD or WR? 456 */ 457 if (rcount > (u_char)last) 458 rcount = (u_char)last; 459 op |= SMB_TRANS_NOCNT; 460 } 461 } 462 error = 0; 463 done: 464 /* XXX wait for xmit buffer to become empty */ 465 last = reg_read(sc, IG4_REG_TX_ABRT_SOURCE); 466 467 return (error); 468 } 469 470 /* 471 * SMBUS API FUNCTIONS 472 * 473 * Called from ig4iic_pci_attach/detach() 474 */ 475 int 476 ig4iic_attach(ig4iic_softc_t *sc) 477 { 478 int error; 479 uint32_t v; 480 481 v = reg_read(sc, IG4_REG_COMP_TYPE); 482 v = reg_read(sc, IG4_REG_COMP_PARAM1); 483 v = reg_read(sc, IG4_REG_GENERAL); 484 if ((v & IG4_GENERAL_SWMODE) == 0) { 485 v |= IG4_GENERAL_SWMODE; 486 reg_write(sc, IG4_REG_GENERAL, v); 487 v = reg_read(sc, IG4_REG_GENERAL); 488 } 489 490 v = reg_read(sc, IG4_REG_SW_LTR_VALUE); 491 v = reg_read(sc, IG4_REG_AUTO_LTR_VALUE); 492 493 v = reg_read(sc, IG4_REG_COMP_VER); 494 if (v != IG4_COMP_VER) { 495 error = ENXIO; 496 goto done; 497 } 498 v = reg_read(sc, IG4_REG_SS_SCL_HCNT); 499 v = reg_read(sc, IG4_REG_SS_SCL_LCNT); 500 v = reg_read(sc, IG4_REG_FS_SCL_HCNT); 501 v = reg_read(sc, IG4_REG_FS_SCL_LCNT); 502 v = reg_read(sc, IG4_REG_SDA_HOLD); 503 504 v = reg_read(sc, IG4_REG_SS_SCL_HCNT); 505 reg_write(sc, IG4_REG_FS_SCL_HCNT, v); 506 v = reg_read(sc, IG4_REG_SS_SCL_LCNT); 507 reg_write(sc, IG4_REG_FS_SCL_LCNT, v); 508 509 /* 510 * Program based on a 25000 Hz clock. This is a bit of a 511 * hack (obviously). The defaults are 400 and 470 for standard 512 * and 60 and 130 for fast. The defaults for standard fail 513 * utterly (presumably cause an abort) because the clock time 514 * is ~18.8ms by default. This brings it down to ~4ms (for now). 515 */ 516 reg_write(sc, IG4_REG_SS_SCL_HCNT, 100); 517 reg_write(sc, IG4_REG_SS_SCL_LCNT, 125); 518 reg_write(sc, IG4_REG_FS_SCL_HCNT, 100); 519 reg_write(sc, IG4_REG_FS_SCL_LCNT, 125); 520 521 /* 522 * Use a threshold of 1 so we get interrupted on each character, 523 * allowing us to use mtx_sleep() in our poll code. Not perfect 524 * but this is better than using DELAY() for receiving data. 525 */ 526 reg_write(sc, IG4_REG_RX_TL, 1); 527 528 reg_write(sc, IG4_REG_CTL, 529 IG4_CTL_MASTER | 530 IG4_CTL_SLAVE_DISABLE | 531 IG4_CTL_RESTARTEN | 532 IG4_CTL_SPEED_STD); 533 534 sc->smb = device_add_child(sc->dev, "smbus", -1); 535 if (sc->smb == NULL) { 536 device_printf(sc->dev, "smbus driver not found\n"); 537 error = ENXIO; 538 goto done; 539 } 540 541 #if 0 542 /* 543 * Don't do this, it blows up the PCI config 544 */ 545 reg_write(sc, IG4_REG_RESETS, IG4_RESETS_ASSERT); 546 reg_write(sc, IG4_REG_RESETS, IG4_RESETS_DEASSERT); 547 #endif 548 549 /* 550 * Interrupt on STOP detect or receive character ready 551 */ 552 reg_write(sc, IG4_REG_INTR_MASK, IG4_INTR_STOP_DET | 553 IG4_INTR_RX_FULL); 554 mtx_lock(&sc->mutex); 555 if (set_controller(sc, 0)) 556 device_printf(sc->dev, "controller error during attach-1\n"); 557 if (set_controller(sc, IG4_I2C_ENABLE)) 558 device_printf(sc->dev, "controller error during attach-2\n"); 559 mtx_unlock(&sc->mutex); 560 error = bus_setup_intr(sc->dev, sc->intr_res, INTR_TYPE_MISC | INTR_MPSAFE, 561 NULL, ig4iic_intr, sc, &sc->intr_handle); 562 if (error) { 563 device_printf(sc->dev, 564 "Unable to setup irq: error %d\n", error); 565 } 566 567 sc->enum_hook.ich_func = ig4iic_start; 568 sc->enum_hook.ich_arg = sc->dev; 569 570 /* We have to wait until interrupts are enabled. I2C read and write 571 * only works if the interrupts are available. 572 */ 573 if (config_intrhook_establish(&sc->enum_hook) != 0) 574 error = ENOMEM; 575 else 576 error = 0; 577 578 done: 579 return (error); 580 } 581 582 void 583 ig4iic_start(void *xdev) 584 { 585 int error; 586 ig4iic_softc_t *sc; 587 device_t dev = (device_t)xdev; 588 589 sc = device_get_softc(dev); 590 591 config_intrhook_disestablish(&sc->enum_hook); 592 593 /* Attach us to the smbus */ 594 error = bus_generic_attach(sc->dev); 595 if (error) { 596 device_printf(sc->dev, 597 "failed to attach child: error %d\n", error); 598 } 599 } 600 601 602 603 int 604 ig4iic_detach(ig4iic_softc_t *sc) 605 { 606 int error; 607 608 if (device_is_attached(sc->dev)) { 609 error = bus_generic_detach(sc->dev); 610 if (error) 611 return (error); 612 } 613 if (sc->smb) 614 device_delete_child(sc->dev, sc->smb); 615 if (sc->intr_handle) 616 bus_teardown_intr(sc->dev, sc->intr_res, sc->intr_handle); 617 618 mtx_lock(&sc->mutex); 619 620 sc->smb = NULL; 621 sc->intr_handle = NULL; 622 reg_write(sc, IG4_REG_INTR_MASK, 0); 623 reg_read(sc, IG4_REG_CLR_INTR); 624 set_controller(sc, 0); 625 626 mtx_unlock(&sc->mutex); 627 return (0); 628 } 629 630 int 631 ig4iic_smb_callback(device_t dev, int index, void *data) 632 { 633 ig4iic_softc_t *sc = device_get_softc(dev); 634 int error; 635 636 mtx_lock(&sc->mutex); 637 638 switch (index) { 639 case SMB_REQUEST_BUS: 640 error = 0; 641 break; 642 case SMB_RELEASE_BUS: 643 error = 0; 644 break; 645 default: 646 error = SMB_EABORT; 647 break; 648 } 649 650 mtx_unlock(&sc->mutex); 651 652 return (error); 653 } 654 655 /* 656 * Quick command. i.e. START + cmd + R/W + STOP and no data. It is 657 * unclear to me how I could implement this with the intel i2c controller 658 * because the controler sends STARTs and STOPs automatically with data. 659 */ 660 int 661 ig4iic_smb_quick(device_t dev, u_char slave, int how) 662 { 663 ig4iic_softc_t *sc = device_get_softc(dev); 664 int error; 665 666 mtx_lock(&sc->mutex); 667 668 switch (how) { 669 case SMB_QREAD: 670 error = SMB_ENOTSUPP; 671 break; 672 case SMB_QWRITE: 673 error = SMB_ENOTSUPP; 674 break; 675 default: 676 error = SMB_ENOTSUPP; 677 break; 678 } 679 mtx_unlock(&sc->mutex); 680 681 return (error); 682 } 683 684 /* 685 * Incremental send byte without stop (?). It is unclear why the slave 686 * address is specified if this presumably is used in combination with 687 * ig4iic_smb_quick(). 688 * 689 * (Also, how would this work anyway? Issue the last byte with writeb()?) 690 */ 691 int 692 ig4iic_smb_sendb(device_t dev, u_char slave, char byte) 693 { 694 ig4iic_softc_t *sc = device_get_softc(dev); 695 uint32_t cmd; 696 int error; 697 698 mtx_lock(&sc->mutex); 699 700 set_slave_addr(sc, slave, 0); 701 cmd = byte; 702 if (wait_status(sc, IG4_STATUS_TX_NOTFULL) == 0) { 703 reg_write(sc, IG4_REG_DATA_CMD, cmd); 704 error = 0; 705 } else { 706 error = SMB_ETIMEOUT; 707 } 708 709 mtx_unlock(&sc->mutex); 710 return (error); 711 } 712 713 /* 714 * Incremental receive byte without stop (?). It is unclear why the slave 715 * address is specified if this presumably is used in combination with 716 * ig4iic_smb_quick(). 717 */ 718 int 719 ig4iic_smb_recvb(device_t dev, u_char slave, char *byte) 720 { 721 ig4iic_softc_t *sc = device_get_softc(dev); 722 int error; 723 724 mtx_lock(&sc->mutex); 725 726 set_slave_addr(sc, slave, 0); 727 reg_write(sc, IG4_REG_DATA_CMD, IG4_DATA_COMMAND_RD); 728 if (wait_status(sc, IG4_STATUS_RX_NOTEMPTY) == 0) { 729 *byte = data_read(sc); 730 error = 0; 731 } else { 732 *byte = 0; 733 error = SMB_ETIMEOUT; 734 } 735 736 mtx_unlock(&sc->mutex); 737 return (error); 738 } 739 740 /* 741 * Write command and single byte in transaction. 742 */ 743 int 744 ig4iic_smb_writeb(device_t dev, u_char slave, char cmd, char byte) 745 { 746 ig4iic_softc_t *sc = device_get_softc(dev); 747 int error; 748 749 mtx_lock(&sc->mutex); 750 751 set_slave_addr(sc, slave, 0); 752 error = smb_transaction(sc, cmd, SMB_TRANS_NOCNT, 753 &byte, 1, NULL, 0, NULL); 754 755 mtx_unlock(&sc->mutex); 756 return (error); 757 } 758 759 /* 760 * Write command and single word in transaction. 761 */ 762 int 763 ig4iic_smb_writew(device_t dev, u_char slave, char cmd, short word) 764 { 765 ig4iic_softc_t *sc = device_get_softc(dev); 766 char buf[2]; 767 int error; 768 769 mtx_lock(&sc->mutex); 770 771 set_slave_addr(sc, slave, 0); 772 buf[0] = word & 0xFF; 773 buf[1] = word >> 8; 774 error = smb_transaction(sc, cmd, SMB_TRANS_NOCNT, 775 buf, 2, NULL, 0, NULL); 776 777 mtx_unlock(&sc->mutex); 778 return (error); 779 } 780 781 /* 782 * write command and read single byte in transaction. 783 */ 784 int 785 ig4iic_smb_readb(device_t dev, u_char slave, char cmd, char *byte) 786 { 787 ig4iic_softc_t *sc = device_get_softc(dev); 788 int error; 789 790 mtx_lock(&sc->mutex); 791 792 set_slave_addr(sc, slave, 0); 793 error = smb_transaction(sc, cmd, SMB_TRANS_NOCNT, 794 NULL, 0, byte, 1, NULL); 795 796 mtx_unlock(&sc->mutex); 797 return (error); 798 } 799 800 /* 801 * write command and read word in transaction. 802 */ 803 int 804 ig4iic_smb_readw(device_t dev, u_char slave, char cmd, short *word) 805 { 806 ig4iic_softc_t *sc = device_get_softc(dev); 807 char buf[2]; 808 int error; 809 810 mtx_lock(&sc->mutex); 811 812 set_slave_addr(sc, slave, 0); 813 if ((error = smb_transaction(sc, cmd, SMB_TRANS_NOCNT, 814 NULL, 0, buf, 2, NULL)) == 0) { 815 *word = (u_char)buf[0] | ((u_char)buf[1] << 8); 816 } 817 818 mtx_unlock(&sc->mutex); 819 return (error); 820 } 821 822 /* 823 * write command and word and read word in transaction 824 */ 825 int 826 ig4iic_smb_pcall(device_t dev, u_char slave, char cmd, 827 short sdata, short *rdata) 828 { 829 ig4iic_softc_t *sc = device_get_softc(dev); 830 char rbuf[2]; 831 char wbuf[2]; 832 int error; 833 834 mtx_lock(&sc->mutex); 835 836 set_slave_addr(sc, slave, 0); 837 wbuf[0] = sdata & 0xFF; 838 wbuf[1] = sdata >> 8; 839 if ((error = smb_transaction(sc, cmd, SMB_TRANS_NOCNT, 840 wbuf, 2, rbuf, 2, NULL)) == 0) { 841 *rdata = (u_char)rbuf[0] | ((u_char)rbuf[1] << 8); 842 } 843 844 mtx_unlock(&sc->mutex); 845 return (error); 846 } 847 848 int 849 ig4iic_smb_bwrite(device_t dev, u_char slave, char cmd, 850 u_char wcount, char *buf) 851 { 852 ig4iic_softc_t *sc = device_get_softc(dev); 853 int error; 854 855 mtx_lock(&sc->mutex); 856 857 set_slave_addr(sc, slave, 0); 858 error = smb_transaction(sc, cmd, 0, 859 buf, wcount, NULL, 0, NULL); 860 861 mtx_unlock(&sc->mutex); 862 return (error); 863 } 864 865 int 866 ig4iic_smb_bread(device_t dev, u_char slave, char cmd, 867 u_char *countp_char, char *buf) 868 { 869 ig4iic_softc_t *sc = device_get_softc(dev); 870 int rcount = *countp_char; 871 int error; 872 873 mtx_lock(&sc->mutex); 874 875 set_slave_addr(sc, slave, 0); 876 error = smb_transaction(sc, cmd, 0, 877 NULL, 0, buf, rcount, &rcount); 878 *countp_char = rcount; 879 880 mtx_unlock(&sc->mutex); 881 return (error); 882 } 883 884 int 885 ig4iic_smb_trans(device_t dev, int slave, char cmd, int op, 886 char *wbuf, int wcount, char *rbuf, int rcount, 887 int *actualp) 888 { 889 ig4iic_softc_t *sc = device_get_softc(dev); 890 int error; 891 892 mtx_lock(&sc->mutex); 893 894 set_slave_addr(sc, slave, op); 895 error = smb_transaction(sc, cmd, op, 896 wbuf, wcount, rbuf, rcount, actualp); 897 898 mtx_unlock(&sc->mutex); 899 return (error); 900 } 901 902 /* 903 * Interrupt Operation 904 */ 905 static void 906 ig4iic_intr(void *cookie) 907 { 908 ig4iic_softc_t *sc = cookie; 909 uint32_t status; 910 911 mtx_lock(&sc->mutex); 912 /* reg_write(sc, IG4_REG_INTR_MASK, IG4_INTR_STOP_DET);*/ 913 status = reg_read(sc, IG4_REG_I2C_STA); 914 while (status & IG4_STATUS_RX_NOTEMPTY) { 915 sc->rbuf[sc->rnext & IG4_RBUFMASK] = 916 (uint8_t)reg_read(sc, IG4_REG_DATA_CMD); 917 ++sc->rnext; 918 status = reg_read(sc, IG4_REG_I2C_STA); 919 } 920 reg_read(sc, IG4_REG_CLR_INTR); 921 wakeup(sc); 922 mtx_unlock(&sc->mutex); 923 } 924 925 #define REGDUMP(sc, reg) \ 926 device_printf(sc->dev, " %-23s %08x\n", #reg, reg_read(sc, reg)) 927 928 static void 929 ig4iic_dump(ig4iic_softc_t *sc) 930 { 931 device_printf(sc->dev, "ig4iic register dump:\n"); 932 REGDUMP(sc, IG4_REG_CTL); 933 REGDUMP(sc, IG4_REG_TAR_ADD); 934 REGDUMP(sc, IG4_REG_SS_SCL_HCNT); 935 REGDUMP(sc, IG4_REG_SS_SCL_LCNT); 936 REGDUMP(sc, IG4_REG_FS_SCL_HCNT); 937 REGDUMP(sc, IG4_REG_FS_SCL_LCNT); 938 REGDUMP(sc, IG4_REG_INTR_STAT); 939 REGDUMP(sc, IG4_REG_INTR_MASK); 940 REGDUMP(sc, IG4_REG_RAW_INTR_STAT); 941 REGDUMP(sc, IG4_REG_RX_TL); 942 REGDUMP(sc, IG4_REG_TX_TL); 943 REGDUMP(sc, IG4_REG_I2C_EN); 944 REGDUMP(sc, IG4_REG_I2C_STA); 945 REGDUMP(sc, IG4_REG_TXFLR); 946 REGDUMP(sc, IG4_REG_RXFLR); 947 REGDUMP(sc, IG4_REG_SDA_HOLD); 948 REGDUMP(sc, IG4_REG_TX_ABRT_SOURCE); 949 REGDUMP(sc, IG4_REG_SLV_DATA_NACK); 950 REGDUMP(sc, IG4_REG_DMA_CTRL); 951 REGDUMP(sc, IG4_REG_DMA_TDLR); 952 REGDUMP(sc, IG4_REG_DMA_RDLR); 953 REGDUMP(sc, IG4_REG_SDA_SETUP); 954 REGDUMP(sc, IG4_REG_ENABLE_STATUS); 955 REGDUMP(sc, IG4_REG_COMP_PARAM1); 956 REGDUMP(sc, IG4_REG_COMP_VER); 957 REGDUMP(sc, IG4_REG_COMP_TYPE); 958 REGDUMP(sc, IG4_REG_CLK_PARMS); 959 REGDUMP(sc, IG4_REG_RESETS); 960 REGDUMP(sc, IG4_REG_GENERAL); 961 REGDUMP(sc, IG4_REG_SW_LTR_VALUE); 962 REGDUMP(sc, IG4_REG_AUTO_LTR_VALUE); 963 } 964 #undef REGDUMP 965 966 DRIVER_MODULE(smbus, ig4iic, smbus_driver, smbus_devclass, NULL, NULL); 967