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 * See ig4_var.h for locking semantics. 44 */ 45 46 #include <sys/param.h> 47 #include <sys/systm.h> 48 #include <sys/kernel.h> 49 #include <sys/module.h> 50 #include <sys/errno.h> 51 #include <sys/lock.h> 52 #include <sys/mutex.h> 53 #include <sys/sx.h> 54 #include <sys/syslog.h> 55 #include <sys/bus.h> 56 #include <sys/sysctl.h> 57 58 #include <machine/bus.h> 59 #include <sys/rman.h> 60 61 #include <dev/pci/pcivar.h> 62 #include <dev/pci/pcireg.h> 63 #include <dev/smbus/smbconf.h> 64 #include <dev/iicbus/iicbus.h> 65 #include <dev/iicbus/iiconf.h> 66 67 #include <dev/ichiic/ig4_reg.h> 68 #include <dev/ichiic/ig4_var.h> 69 70 #define TRANS_NORMAL 1 71 #define TRANS_PCALL 2 72 #define TRANS_BLOCK 3 73 74 static void ig4iic_start(void *xdev); 75 static void ig4iic_intr(void *cookie); 76 static void ig4iic_dump(ig4iic_softc_t *sc); 77 78 static int ig4_dump; 79 SYSCTL_INT(_debug, OID_AUTO, ig4_dump, CTLFLAG_RW, 80 &ig4_dump, 0, "Dump controller registers"); 81 82 /* 83 * Low-level inline support functions 84 */ 85 static __inline void 86 reg_write(ig4iic_softc_t *sc, uint32_t reg, uint32_t value) 87 { 88 bus_write_4(sc->regs_res, reg, value); 89 bus_barrier(sc->regs_res, reg, 4, BUS_SPACE_BARRIER_WRITE); 90 } 91 92 static __inline uint32_t 93 reg_read(ig4iic_softc_t *sc, uint32_t reg) 94 { 95 uint32_t value; 96 97 bus_barrier(sc->regs_res, reg, 4, BUS_SPACE_BARRIER_READ); 98 value = bus_read_4(sc->regs_res, reg); 99 return (value); 100 } 101 102 /* 103 * Enable or disable the controller and wait for the controller to acknowledge 104 * the state change. 105 */ 106 static int 107 set_controller(ig4iic_softc_t *sc, uint32_t ctl) 108 { 109 int retry; 110 int error; 111 uint32_t v; 112 113 /* 114 * When the controller is enabled, interrupt on STOP detect 115 * or receive character ready and clear pending interrupts. 116 */ 117 if (ctl & IG4_I2C_ENABLE) { 118 reg_write(sc, IG4_REG_INTR_MASK, IG4_INTR_STOP_DET | 119 IG4_INTR_RX_FULL); 120 reg_read(sc, IG4_REG_CLR_INTR); 121 } else 122 reg_write(sc, IG4_REG_INTR_MASK, 0); 123 124 reg_write(sc, IG4_REG_I2C_EN, ctl); 125 error = IIC_ETIMEOUT; 126 127 for (retry = 100; retry > 0; --retry) { 128 v = reg_read(sc, IG4_REG_ENABLE_STATUS); 129 if (((v ^ ctl) & IG4_I2C_ENABLE) == 0) { 130 error = 0; 131 break; 132 } 133 if (cold) 134 DELAY(1000); 135 else 136 mtx_sleep(sc, &sc->io_lock, 0, "i2cslv", 1); 137 } 138 return (error); 139 } 140 141 /* 142 * Wait up to 25ms for the requested status using a 25uS polling loop. 143 */ 144 static int 145 wait_status(ig4iic_softc_t *sc, uint32_t status) 146 { 147 uint32_t v; 148 int error; 149 int txlvl = -1; 150 u_int count_us = 0; 151 u_int limit_us = 25000; /* 25ms */ 152 153 error = IIC_ETIMEOUT; 154 155 for (;;) { 156 /* 157 * Check requested status 158 */ 159 v = reg_read(sc, IG4_REG_I2C_STA); 160 if (v & status) { 161 error = 0; 162 break; 163 } 164 165 /* 166 * When waiting for receive data break-out if the interrupt 167 * loaded data into the FIFO. 168 */ 169 if (status & IG4_STATUS_RX_NOTEMPTY) { 170 if (sc->rpos != sc->rnext) { 171 error = 0; 172 break; 173 } 174 } 175 176 /* 177 * When waiting for the transmit FIFO to become empty, 178 * reset the timeout if we see a change in the transmit 179 * FIFO level as progress is being made. 180 */ 181 if (status & IG4_STATUS_TX_EMPTY) { 182 v = reg_read(sc, IG4_REG_TXFLR) & IG4_FIFOLVL_MASK; 183 if (txlvl != v) { 184 txlvl = v; 185 count_us = 0; 186 } 187 } 188 189 /* 190 * Stop if we've run out of time. 191 */ 192 if (count_us >= limit_us) 193 break; 194 195 /* 196 * When waiting for receive data let the interrupt do its 197 * work, otherwise poll with the lock held. 198 */ 199 if (status & IG4_STATUS_RX_NOTEMPTY) { 200 mtx_sleep(sc, &sc->io_lock, 0, "i2cwait", 201 (hz + 99) / 100); /* sleep up to 10ms */ 202 count_us += 10000; 203 } else { 204 DELAY(25); 205 count_us += 25; 206 } 207 } 208 209 return (error); 210 } 211 212 /* 213 * Read I2C data. The data might have already been read by 214 * the interrupt code, otherwise it is sitting in the data 215 * register. 216 */ 217 static uint8_t 218 data_read(ig4iic_softc_t *sc) 219 { 220 uint8_t c; 221 222 if (sc->rpos == sc->rnext) { 223 c = (uint8_t)reg_read(sc, IG4_REG_DATA_CMD); 224 } else { 225 c = sc->rbuf[sc->rpos & IG4_RBUFMASK]; 226 ++sc->rpos; 227 } 228 return (c); 229 } 230 231 /* 232 * Set the slave address. The controller must be disabled when 233 * changing the address. 234 * 235 * This operation does not issue anything to the I2C bus but sets 236 * the target address for when the controller later issues a START. 237 */ 238 static void 239 set_slave_addr(ig4iic_softc_t *sc, uint8_t slave, int trans_op) 240 { 241 uint32_t tar; 242 uint32_t ctl; 243 int use_10bit; 244 245 use_10bit = sc->use_10bit; 246 if (trans_op & SMB_TRANS_7BIT) 247 use_10bit = 0; 248 if (trans_op & SMB_TRANS_10BIT) 249 use_10bit = 1; 250 251 if (sc->slave_valid && sc->last_slave == slave && 252 sc->use_10bit == use_10bit) { 253 return; 254 } 255 sc->use_10bit = use_10bit; 256 257 /* 258 * Wait for TXFIFO to drain before disabling the controller. 259 * 260 * If a write message has not been completed it's really a 261 * programming error, but for now in that case issue an extra 262 * byte + STOP. 263 * 264 * If a read message has not been completed it's also a programming 265 * error, for now just ignore it. 266 */ 267 wait_status(sc, IG4_STATUS_TX_NOTFULL); 268 if (sc->write_started) { 269 reg_write(sc, IG4_REG_DATA_CMD, IG4_DATA_STOP); 270 sc->write_started = 0; 271 } 272 if (sc->read_started) 273 sc->read_started = 0; 274 wait_status(sc, IG4_STATUS_TX_EMPTY); 275 276 set_controller(sc, 0); 277 ctl = reg_read(sc, IG4_REG_CTL); 278 ctl &= ~IG4_CTL_10BIT; 279 ctl |= IG4_CTL_RESTARTEN; 280 281 tar = slave; 282 if (sc->use_10bit) { 283 tar |= IG4_TAR_10BIT; 284 ctl |= IG4_CTL_10BIT; 285 } 286 reg_write(sc, IG4_REG_CTL, ctl); 287 reg_write(sc, IG4_REG_TAR_ADD, tar); 288 set_controller(sc, IG4_I2C_ENABLE); 289 sc->slave_valid = 1; 290 sc->last_slave = slave; 291 } 292 293 /* 294 * Issue START with byte command, possible count, and a variable length 295 * read or write buffer, then possible turn-around read. The read also 296 * has a possible count received. 297 * 298 * For SMBUS - 299 * 300 * Quick: START+ADDR+RD/WR STOP 301 * 302 * Normal: START+ADDR+WR CMD DATA..DATA STOP 303 * 304 * START+ADDR+RD CMD 305 * RESTART+ADDR RDATA..RDATA STOP 306 * (can also be used for I2C transactions) 307 * 308 * Process Call: START+ADDR+WR CMD DATAL DATAH 309 * RESTART+ADDR+RD RDATAL RDATAH STOP 310 * 311 * Block: START+ADDR+RD CMD 312 * RESTART+ADDR+RD RCOUNT DATA... STOP 313 * 314 * START+ADDR+WR CMD 315 * RESTART+ADDR+WR WCOUNT DATA... STOP 316 * 317 * For I2C - basically, no *COUNT fields, possibly no *CMD field. If the 318 * sender needs to issue a 2-byte command it will incorporate it 319 * into the write buffer and also set NOCMD. 320 * 321 * Generally speaking, the START+ADDR / RESTART+ADDR is handled automatically 322 * by the controller at the beginning of a command sequence or on a data 323 * direction turn-around, and we only need to tell it when to issue the STOP. 324 */ 325 static int 326 smb_transaction(ig4iic_softc_t *sc, char cmd, int op, 327 char *wbuf, int wcount, char *rbuf, int rcount, int *actualp) 328 { 329 int error; 330 int unit; 331 uint32_t last; 332 333 /* 334 * Debugging - dump registers 335 */ 336 if (ig4_dump) { 337 unit = device_get_unit(sc->dev); 338 if (ig4_dump & (1 << unit)) { 339 ig4_dump &= ~(1 << unit); 340 ig4iic_dump(sc); 341 } 342 } 343 344 /* 345 * Issue START or RESTART with next data byte, clear any previous 346 * abort condition that may have been holding the txfifo in reset. 347 */ 348 last = IG4_DATA_RESTART; 349 reg_read(sc, IG4_REG_CLR_TX_ABORT); 350 if (actualp) 351 *actualp = 0; 352 353 /* 354 * Issue command if not told otherwise (smbus). 355 */ 356 if ((op & SMB_TRANS_NOCMD) == 0) { 357 error = wait_status(sc, IG4_STATUS_TX_NOTFULL); 358 if (error) 359 goto done; 360 last |= (u_char)cmd; 361 if (wcount == 0 && rcount == 0 && (op & SMB_TRANS_NOSTOP) == 0) 362 last |= IG4_DATA_STOP; 363 reg_write(sc, IG4_REG_DATA_CMD, last); 364 last = 0; 365 } 366 367 /* 368 * Clean out any previously received data. 369 */ 370 if (sc->rpos != sc->rnext && 371 (op & SMB_TRANS_NOREPORT) == 0) { 372 device_printf(sc->dev, 373 "discarding %d bytes of spurious data\n", 374 sc->rnext - sc->rpos); 375 } 376 sc->rpos = 0; 377 sc->rnext = 0; 378 379 /* 380 * If writing and not told otherwise, issue the write count (smbus). 381 */ 382 if (wcount && (op & SMB_TRANS_NOCNT) == 0) { 383 error = wait_status(sc, IG4_STATUS_TX_NOTFULL); 384 if (error) 385 goto done; 386 last |= (u_char)cmd; 387 reg_write(sc, IG4_REG_DATA_CMD, last); 388 last = 0; 389 } 390 391 /* 392 * Bulk write (i2c) 393 */ 394 while (wcount) { 395 error = wait_status(sc, IG4_STATUS_TX_NOTFULL); 396 if (error) 397 goto done; 398 last |= (u_char)*wbuf; 399 if (wcount == 1 && rcount == 0 && (op & SMB_TRANS_NOSTOP) == 0) 400 last |= IG4_DATA_STOP; 401 reg_write(sc, IG4_REG_DATA_CMD, last); 402 --wcount; 403 ++wbuf; 404 last = 0; 405 } 406 407 /* 408 * Issue reads to xmit FIFO (strange, I know) to tell the controller 409 * to clock in data. At the moment just issue one read ahead to 410 * pipeline the incoming data. 411 * 412 * NOTE: In the case of NOCMD and wcount == 0 we still issue a 413 * RESTART here, even if the data direction has not changed 414 * from the previous CHAINing call. This we force the RESTART. 415 * (A new START is issued automatically by the controller in 416 * the other nominal cases such as a data direction change or 417 * a previous STOP was issued). 418 * 419 * If this will be the last byte read we must also issue the STOP 420 * at the end of the read. 421 */ 422 if (rcount) { 423 last = IG4_DATA_RESTART | IG4_DATA_COMMAND_RD; 424 if (rcount == 1 && 425 (op & (SMB_TRANS_NOSTOP | SMB_TRANS_NOCNT)) == 426 SMB_TRANS_NOCNT) { 427 last |= IG4_DATA_STOP; 428 } 429 reg_write(sc, IG4_REG_DATA_CMD, last); 430 last = IG4_DATA_COMMAND_RD; 431 } 432 433 /* 434 * Bulk read (i2c) and count field handling (smbus) 435 */ 436 while (rcount) { 437 /* 438 * Maintain a pipeline by queueing the allowance for the next 439 * read before waiting for the current read. 440 */ 441 if (rcount > 1) { 442 if (op & SMB_TRANS_NOCNT) 443 last = (rcount == 2) ? IG4_DATA_STOP : 0; 444 else 445 last = 0; 446 reg_write(sc, IG4_REG_DATA_CMD, IG4_DATA_COMMAND_RD | 447 last); 448 } 449 error = wait_status(sc, IG4_STATUS_RX_NOTEMPTY); 450 if (error) { 451 if ((op & SMB_TRANS_NOREPORT) == 0) { 452 device_printf(sc->dev, 453 "rx timeout addr 0x%02x\n", 454 sc->last_slave); 455 } 456 goto done; 457 } 458 last = data_read(sc); 459 460 if (op & SMB_TRANS_NOCNT) { 461 *rbuf = (u_char)last; 462 ++rbuf; 463 --rcount; 464 if (actualp) 465 ++*actualp; 466 } else { 467 /* 468 * Handle count field (smbus), which is not part of 469 * the rcount'ed buffer. The first read data in a 470 * bulk transfer is the count. 471 * 472 * XXX if rcount is loaded as 0 how do I generate a 473 * STOP now without issuing another RD or WR? 474 */ 475 if (rcount > (u_char)last) 476 rcount = (u_char)last; 477 op |= SMB_TRANS_NOCNT; 478 } 479 } 480 error = 0; 481 done: 482 /* XXX wait for xmit buffer to become empty */ 483 last = reg_read(sc, IG4_REG_TX_ABRT_SOURCE); 484 485 return (error); 486 } 487 488 /* 489 * IICBUS API FUNCTIONS 490 */ 491 static int 492 ig4iic_xfer_start(ig4iic_softc_t *sc, uint16_t slave) 493 { 494 /* XXX 10-bit address support? */ 495 set_slave_addr(sc, slave >> 1, 0); 496 return (0); 497 } 498 499 static int 500 ig4iic_read(ig4iic_softc_t *sc, uint8_t *buf, uint16_t len, 501 bool repeated_start, bool stop) 502 { 503 uint32_t cmd; 504 uint16_t i; 505 int error; 506 507 if (len == 0) 508 return (0); 509 510 cmd = IG4_DATA_COMMAND_RD; 511 cmd |= repeated_start ? IG4_DATA_RESTART : 0; 512 cmd |= stop && len == 1 ? IG4_DATA_STOP : 0; 513 514 /* Issue request for the first byte (could be last as well). */ 515 reg_write(sc, IG4_REG_DATA_CMD, cmd); 516 517 for (i = 0; i < len; i++) { 518 /* 519 * Maintain a pipeline by queueing the allowance for the next 520 * read before waiting for the current read. 521 */ 522 cmd = IG4_DATA_COMMAND_RD; 523 if (i < len - 1) { 524 cmd = IG4_DATA_COMMAND_RD; 525 cmd |= stop && i == len - 2 ? IG4_DATA_STOP : 0; 526 reg_write(sc, IG4_REG_DATA_CMD, cmd); 527 } 528 error = wait_status(sc, IG4_STATUS_RX_NOTEMPTY); 529 if (error) 530 break; 531 buf[i] = data_read(sc); 532 } 533 534 (void)reg_read(sc, IG4_REG_TX_ABRT_SOURCE); 535 return (error); 536 } 537 538 static int 539 ig4iic_write(ig4iic_softc_t *sc, uint8_t *buf, uint16_t len, 540 bool repeated_start, bool stop) 541 { 542 uint32_t cmd; 543 uint16_t i; 544 int error; 545 546 if (len == 0) 547 return (0); 548 549 cmd = repeated_start ? IG4_DATA_RESTART : 0; 550 for (i = 0; i < len; i++) { 551 error = wait_status(sc, IG4_STATUS_TX_NOTFULL); 552 if (error) 553 break; 554 cmd |= buf[i]; 555 cmd |= stop && i == len - 1 ? IG4_DATA_STOP : 0; 556 reg_write(sc, IG4_REG_DATA_CMD, cmd); 557 cmd = 0; 558 } 559 560 (void)reg_read(sc, IG4_REG_TX_ABRT_SOURCE); 561 return (error); 562 } 563 564 int 565 ig4iic_transfer(device_t dev, struct iic_msg *msgs, uint32_t nmsgs) 566 { 567 ig4iic_softc_t *sc = device_get_softc(dev); 568 const char *reason = NULL; 569 uint32_t i; 570 int error; 571 int unit; 572 bool rpstart; 573 bool stop; 574 575 /* 576 * The hardware interface imposes limits on allowed I2C messages. 577 * It is not possible to explicitly send a start or stop. 578 * They are automatically sent (or not sent, depending on the 579 * configuration) when a data byte is transferred. 580 * For this reason it's impossible to send a message with no data 581 * at all (like an SMBus quick message). 582 * The start condition is automatically generated after the stop 583 * condition, so it's impossible to not have a start after a stop. 584 * The repeated start condition is automatically sent if a change 585 * of the transfer direction happens, so it's impossible to have 586 * a change of direction without a (repeated) start. 587 * The repeated start can be forced even without the change of 588 * direction. 589 * Changing the target slave address requires resetting the hardware 590 * state, so it's impossible to do that without the stop followed 591 * by the start. 592 */ 593 for (i = 0; i < nmsgs; i++) { 594 #if 0 595 if (i == 0 && (msgs[i].flags & IIC_M_NOSTART) != 0) { 596 reason = "first message without start"; 597 break; 598 } 599 if (i == nmsgs - 1 && (msgs[i].flags & IIC_M_NOSTOP) != 0) { 600 reason = "last message without stop"; 601 break; 602 } 603 #endif 604 if (msgs[i].len == 0) { 605 reason = "message with no data"; 606 break; 607 } 608 if (i > 0) { 609 if ((msgs[i].flags & IIC_M_NOSTART) != 0 && 610 (msgs[i - 1].flags & IIC_M_NOSTOP) == 0) { 611 reason = "stop not followed by start"; 612 break; 613 } 614 if ((msgs[i - 1].flags & IIC_M_NOSTOP) != 0 && 615 msgs[i].slave != msgs[i - 1].slave) { 616 reason = "change of slave without stop"; 617 break; 618 } 619 if ((msgs[i].flags & IIC_M_NOSTART) != 0 && 620 (msgs[i].flags & IIC_M_RD) != 621 (msgs[i - 1].flags & IIC_M_RD)) { 622 reason = "change of direction without repeated" 623 " start"; 624 break; 625 } 626 } 627 } 628 if (reason != NULL) { 629 if (bootverbose) 630 device_printf(dev, "%s\n", reason); 631 return (IIC_ENOTSUPP); 632 } 633 634 sx_xlock(&sc->call_lock); 635 mtx_lock(&sc->io_lock); 636 637 /* Debugging - dump registers. */ 638 if (ig4_dump) { 639 unit = device_get_unit(dev); 640 if (ig4_dump & (1 << unit)) { 641 ig4_dump &= ~(1 << unit); 642 ig4iic_dump(sc); 643 } 644 } 645 646 /* 647 * Clear any previous abort condition that may have been holding 648 * the txfifo in reset. 649 */ 650 reg_read(sc, IG4_REG_CLR_TX_ABORT); 651 652 /* 653 * Clean out any previously received data. 654 */ 655 if (sc->rpos != sc->rnext && bootverbose) { 656 device_printf(sc->dev, "discarding %d bytes of spurious data\n", 657 sc->rnext - sc->rpos); 658 } 659 sc->rpos = 0; 660 sc->rnext = 0; 661 662 rpstart = false; 663 error = 0; 664 for (i = 0; i < nmsgs; i++) { 665 if ((msgs[i].flags & IIC_M_NOSTART) == 0) { 666 error = ig4iic_xfer_start(sc, msgs[i].slave); 667 } else { 668 if (!sc->slave_valid || 669 (msgs[i].slave >> 1) != sc->last_slave) { 670 device_printf(dev, "start condition suppressed" 671 "but slave address is not set up"); 672 error = EINVAL; 673 break; 674 } 675 rpstart = false; 676 } 677 if (error != 0) 678 break; 679 680 stop = (msgs[i].flags & IIC_M_NOSTOP) == 0; 681 if (msgs[i].flags & IIC_M_RD) 682 error = ig4iic_read(sc, msgs[i].buf, msgs[i].len, 683 rpstart, stop); 684 else 685 error = ig4iic_write(sc, msgs[i].buf, msgs[i].len, 686 rpstart, stop); 687 if (error != 0) 688 break; 689 690 rpstart = !stop; 691 } 692 693 mtx_unlock(&sc->io_lock); 694 sx_unlock(&sc->call_lock); 695 return (error); 696 } 697 698 int 699 ig4iic_reset(device_t dev, u_char speed, u_char addr, u_char *oldaddr) 700 { 701 ig4iic_softc_t *sc = device_get_softc(dev); 702 703 sx_xlock(&sc->call_lock); 704 mtx_lock(&sc->io_lock); 705 706 /* TODO handle speed configuration? */ 707 if (oldaddr != NULL) 708 *oldaddr = sc->last_slave << 1; 709 set_slave_addr(sc, addr >> 1, 0); 710 if (addr == IIC_UNKNOWN) 711 sc->slave_valid = false; 712 713 mtx_unlock(&sc->io_lock); 714 sx_unlock(&sc->call_lock); 715 return (0); 716 } 717 718 /* 719 * SMBUS API FUNCTIONS 720 * 721 * Called from ig4iic_pci_attach/detach() 722 */ 723 int 724 ig4iic_attach(ig4iic_softc_t *sc) 725 { 726 int error; 727 uint32_t v; 728 729 v = reg_read(sc, IG4_REG_COMP_TYPE); 730 v = reg_read(sc, IG4_REG_COMP_PARAM1); 731 v = reg_read(sc, IG4_REG_GENERAL); 732 if ((v & IG4_GENERAL_SWMODE) == 0) { 733 v |= IG4_GENERAL_SWMODE; 734 reg_write(sc, IG4_REG_GENERAL, v); 735 v = reg_read(sc, IG4_REG_GENERAL); 736 } 737 738 v = reg_read(sc, IG4_REG_SW_LTR_VALUE); 739 v = reg_read(sc, IG4_REG_AUTO_LTR_VALUE); 740 741 v = reg_read(sc, IG4_REG_COMP_VER); 742 if (v != IG4_COMP_VER) { 743 error = ENXIO; 744 goto done; 745 } 746 v = reg_read(sc, IG4_REG_SS_SCL_HCNT); 747 v = reg_read(sc, IG4_REG_SS_SCL_LCNT); 748 v = reg_read(sc, IG4_REG_FS_SCL_HCNT); 749 v = reg_read(sc, IG4_REG_FS_SCL_LCNT); 750 v = reg_read(sc, IG4_REG_SDA_HOLD); 751 752 v = reg_read(sc, IG4_REG_SS_SCL_HCNT); 753 reg_write(sc, IG4_REG_FS_SCL_HCNT, v); 754 v = reg_read(sc, IG4_REG_SS_SCL_LCNT); 755 reg_write(sc, IG4_REG_FS_SCL_LCNT, v); 756 757 /* 758 * Program based on a 25000 Hz clock. This is a bit of a 759 * hack (obviously). The defaults are 400 and 470 for standard 760 * and 60 and 130 for fast. The defaults for standard fail 761 * utterly (presumably cause an abort) because the clock time 762 * is ~18.8ms by default. This brings it down to ~4ms (for now). 763 */ 764 reg_write(sc, IG4_REG_SS_SCL_HCNT, 100); 765 reg_write(sc, IG4_REG_SS_SCL_LCNT, 125); 766 reg_write(sc, IG4_REG_FS_SCL_HCNT, 100); 767 reg_write(sc, IG4_REG_FS_SCL_LCNT, 125); 768 769 /* 770 * Use a threshold of 1 so we get interrupted on each character, 771 * allowing us to use mtx_sleep() in our poll code. Not perfect 772 * but this is better than using DELAY() for receiving data. 773 * 774 * See ig4_var.h for details on interrupt handler synchronization. 775 */ 776 reg_write(sc, IG4_REG_RX_TL, 1); 777 778 reg_write(sc, IG4_REG_CTL, 779 IG4_CTL_MASTER | 780 IG4_CTL_SLAVE_DISABLE | 781 IG4_CTL_RESTARTEN | 782 IG4_CTL_SPEED_STD); 783 784 sc->iicbus = device_add_child(sc->dev, "iicbus", -1); 785 if (sc->iicbus == NULL) { 786 device_printf(sc->dev, "iicbus driver not found\n"); 787 error = ENXIO; 788 goto done; 789 } 790 791 #if 0 792 /* 793 * Don't do this, it blows up the PCI config 794 */ 795 reg_write(sc, IG4_REG_RESETS, IG4_RESETS_ASSERT); 796 reg_write(sc, IG4_REG_RESETS, IG4_RESETS_DEASSERT); 797 #endif 798 799 mtx_lock(&sc->io_lock); 800 if (set_controller(sc, 0)) 801 device_printf(sc->dev, "controller error during attach-1\n"); 802 if (set_controller(sc, IG4_I2C_ENABLE)) 803 device_printf(sc->dev, "controller error during attach-2\n"); 804 mtx_unlock(&sc->io_lock); 805 error = bus_setup_intr(sc->dev, sc->intr_res, INTR_TYPE_MISC | INTR_MPSAFE, 806 NULL, ig4iic_intr, sc, &sc->intr_handle); 807 if (error) { 808 device_printf(sc->dev, 809 "Unable to setup irq: error %d\n", error); 810 } 811 812 sc->enum_hook.ich_func = ig4iic_start; 813 sc->enum_hook.ich_arg = sc->dev; 814 815 /* 816 * We have to wait until interrupts are enabled. I2C read and write 817 * only works if the interrupts are available. 818 */ 819 if (config_intrhook_establish(&sc->enum_hook) != 0) 820 error = ENOMEM; 821 else 822 error = 0; 823 824 done: 825 return (error); 826 } 827 828 void 829 ig4iic_start(void *xdev) 830 { 831 int error; 832 ig4iic_softc_t *sc; 833 device_t dev = (device_t)xdev; 834 835 sc = device_get_softc(dev); 836 837 config_intrhook_disestablish(&sc->enum_hook); 838 839 /* Attach us to the smbus */ 840 error = bus_generic_attach(sc->dev); 841 if (error) { 842 device_printf(sc->dev, 843 "failed to attach child: error %d\n", error); 844 } 845 } 846 847 848 849 int 850 ig4iic_detach(ig4iic_softc_t *sc) 851 { 852 int error; 853 854 if (device_is_attached(sc->dev)) { 855 error = bus_generic_detach(sc->dev); 856 if (error) 857 return (error); 858 } 859 if (sc->iicbus) 860 device_delete_child(sc->dev, sc->iicbus); 861 if (sc->intr_handle) 862 bus_teardown_intr(sc->dev, sc->intr_res, sc->intr_handle); 863 864 sx_xlock(&sc->call_lock); 865 mtx_lock(&sc->io_lock); 866 867 sc->iicbus = NULL; 868 sc->intr_handle = NULL; 869 reg_write(sc, IG4_REG_INTR_MASK, 0); 870 set_controller(sc, 0); 871 872 mtx_unlock(&sc->io_lock); 873 sx_xunlock(&sc->call_lock); 874 return (0); 875 } 876 877 int 878 ig4iic_smb_callback(device_t dev, int index, void *data) 879 { 880 int error; 881 882 switch (index) { 883 case SMB_REQUEST_BUS: 884 error = 0; 885 break; 886 case SMB_RELEASE_BUS: 887 error = 0; 888 break; 889 default: 890 error = SMB_EABORT; 891 break; 892 } 893 894 return (error); 895 } 896 897 /* 898 * Quick command. i.e. START + cmd + R/W + STOP and no data. It is 899 * unclear to me how I could implement this with the intel i2c controller 900 * because the controller sends STARTs and STOPs automatically with data. 901 */ 902 int 903 ig4iic_smb_quick(device_t dev, u_char slave, int how) 904 { 905 906 return (SMB_ENOTSUPP); 907 } 908 909 /* 910 * Incremental send byte without stop (?). It is unclear why the slave 911 * address is specified if this presumably is used in combination with 912 * ig4iic_smb_quick(). 913 * 914 * (Also, how would this work anyway? Issue the last byte with writeb()?) 915 */ 916 int 917 ig4iic_smb_sendb(device_t dev, u_char slave, char byte) 918 { 919 ig4iic_softc_t *sc = device_get_softc(dev); 920 uint32_t cmd; 921 int error; 922 923 sx_xlock(&sc->call_lock); 924 mtx_lock(&sc->io_lock); 925 926 set_slave_addr(sc, slave, 0); 927 cmd = byte; 928 if (wait_status(sc, IG4_STATUS_TX_NOTFULL) == 0) { 929 reg_write(sc, IG4_REG_DATA_CMD, cmd); 930 error = 0; 931 } else { 932 error = SMB_ETIMEOUT; 933 } 934 935 mtx_unlock(&sc->io_lock); 936 sx_xunlock(&sc->call_lock); 937 return (error); 938 } 939 940 /* 941 * Incremental receive byte without stop (?). It is unclear why the slave 942 * address is specified if this presumably is used in combination with 943 * ig4iic_smb_quick(). 944 */ 945 int 946 ig4iic_smb_recvb(device_t dev, u_char slave, char *byte) 947 { 948 ig4iic_softc_t *sc = device_get_softc(dev); 949 int error; 950 951 sx_xlock(&sc->call_lock); 952 mtx_lock(&sc->io_lock); 953 954 set_slave_addr(sc, slave, 0); 955 reg_write(sc, IG4_REG_DATA_CMD, IG4_DATA_COMMAND_RD); 956 if (wait_status(sc, IG4_STATUS_RX_NOTEMPTY) == 0) { 957 *byte = data_read(sc); 958 error = 0; 959 } else { 960 *byte = 0; 961 error = SMB_ETIMEOUT; 962 } 963 964 mtx_unlock(&sc->io_lock); 965 sx_xunlock(&sc->call_lock); 966 return (error); 967 } 968 969 /* 970 * Write command and single byte in transaction. 971 */ 972 int 973 ig4iic_smb_writeb(device_t dev, u_char slave, char cmd, char byte) 974 { 975 ig4iic_softc_t *sc = device_get_softc(dev); 976 int error; 977 978 sx_xlock(&sc->call_lock); 979 mtx_lock(&sc->io_lock); 980 981 set_slave_addr(sc, slave, 0); 982 error = smb_transaction(sc, cmd, SMB_TRANS_NOCNT, 983 &byte, 1, NULL, 0, NULL); 984 985 mtx_unlock(&sc->io_lock); 986 sx_xunlock(&sc->call_lock); 987 return (error); 988 } 989 990 /* 991 * Write command and single word in transaction. 992 */ 993 int 994 ig4iic_smb_writew(device_t dev, u_char slave, char cmd, short word) 995 { 996 ig4iic_softc_t *sc = device_get_softc(dev); 997 char buf[2]; 998 int error; 999 1000 sx_xlock(&sc->call_lock); 1001 mtx_lock(&sc->io_lock); 1002 1003 set_slave_addr(sc, slave, 0); 1004 buf[0] = word & 0xFF; 1005 buf[1] = word >> 8; 1006 error = smb_transaction(sc, cmd, SMB_TRANS_NOCNT, 1007 buf, 2, NULL, 0, NULL); 1008 1009 mtx_unlock(&sc->io_lock); 1010 sx_xunlock(&sc->call_lock); 1011 return (error); 1012 } 1013 1014 /* 1015 * write command and read single byte in transaction. 1016 */ 1017 int 1018 ig4iic_smb_readb(device_t dev, u_char slave, char cmd, char *byte) 1019 { 1020 ig4iic_softc_t *sc = device_get_softc(dev); 1021 int error; 1022 1023 sx_xlock(&sc->call_lock); 1024 mtx_lock(&sc->io_lock); 1025 1026 set_slave_addr(sc, slave, 0); 1027 error = smb_transaction(sc, cmd, SMB_TRANS_NOCNT, 1028 NULL, 0, byte, 1, NULL); 1029 1030 mtx_unlock(&sc->io_lock); 1031 sx_xunlock(&sc->call_lock); 1032 return (error); 1033 } 1034 1035 /* 1036 * write command and read word in transaction. 1037 */ 1038 int 1039 ig4iic_smb_readw(device_t dev, u_char slave, char cmd, short *word) 1040 { 1041 ig4iic_softc_t *sc = device_get_softc(dev); 1042 char buf[2]; 1043 int error; 1044 1045 sx_xlock(&sc->call_lock); 1046 mtx_lock(&sc->io_lock); 1047 1048 set_slave_addr(sc, slave, 0); 1049 if ((error = smb_transaction(sc, cmd, SMB_TRANS_NOCNT, 1050 NULL, 0, buf, 2, NULL)) == 0) { 1051 *word = (u_char)buf[0] | ((u_char)buf[1] << 8); 1052 } 1053 1054 mtx_unlock(&sc->io_lock); 1055 sx_xunlock(&sc->call_lock); 1056 return (error); 1057 } 1058 1059 /* 1060 * write command and word and read word in transaction 1061 */ 1062 int 1063 ig4iic_smb_pcall(device_t dev, u_char slave, char cmd, 1064 short sdata, short *rdata) 1065 { 1066 ig4iic_softc_t *sc = device_get_softc(dev); 1067 char rbuf[2]; 1068 char wbuf[2]; 1069 int error; 1070 1071 sx_xlock(&sc->call_lock); 1072 mtx_lock(&sc->io_lock); 1073 1074 set_slave_addr(sc, slave, 0); 1075 wbuf[0] = sdata & 0xFF; 1076 wbuf[1] = sdata >> 8; 1077 if ((error = smb_transaction(sc, cmd, SMB_TRANS_NOCNT, 1078 wbuf, 2, rbuf, 2, NULL)) == 0) { 1079 *rdata = (u_char)rbuf[0] | ((u_char)rbuf[1] << 8); 1080 } 1081 1082 mtx_unlock(&sc->io_lock); 1083 sx_xunlock(&sc->call_lock); 1084 return (error); 1085 } 1086 1087 int 1088 ig4iic_smb_bwrite(device_t dev, u_char slave, char cmd, 1089 u_char wcount, char *buf) 1090 { 1091 ig4iic_softc_t *sc = device_get_softc(dev); 1092 int error; 1093 1094 sx_xlock(&sc->call_lock); 1095 mtx_lock(&sc->io_lock); 1096 1097 set_slave_addr(sc, slave, 0); 1098 error = smb_transaction(sc, cmd, 0, 1099 buf, wcount, NULL, 0, NULL); 1100 1101 mtx_unlock(&sc->io_lock); 1102 sx_xunlock(&sc->call_lock); 1103 return (error); 1104 } 1105 1106 int 1107 ig4iic_smb_bread(device_t dev, u_char slave, char cmd, 1108 u_char *countp_char, char *buf) 1109 { 1110 ig4iic_softc_t *sc = device_get_softc(dev); 1111 int rcount = *countp_char; 1112 int error; 1113 1114 sx_xlock(&sc->call_lock); 1115 mtx_lock(&sc->io_lock); 1116 1117 set_slave_addr(sc, slave, 0); 1118 error = smb_transaction(sc, cmd, 0, 1119 NULL, 0, buf, rcount, &rcount); 1120 *countp_char = rcount; 1121 1122 mtx_unlock(&sc->io_lock); 1123 sx_xunlock(&sc->call_lock); 1124 return (error); 1125 } 1126 1127 int 1128 ig4iic_smb_trans(device_t dev, int slave, char cmd, int op, 1129 char *wbuf, int wcount, char *rbuf, int rcount, 1130 int *actualp) 1131 { 1132 ig4iic_softc_t *sc = device_get_softc(dev); 1133 int error; 1134 1135 sx_xlock(&sc->call_lock); 1136 mtx_lock(&sc->io_lock); 1137 1138 set_slave_addr(sc, slave, op); 1139 error = smb_transaction(sc, cmd, op, 1140 wbuf, wcount, rbuf, rcount, actualp); 1141 1142 mtx_unlock(&sc->io_lock); 1143 sx_xunlock(&sc->call_lock); 1144 return (error); 1145 } 1146 1147 /* 1148 * Interrupt Operation, see ig4_var.h for locking semantics. 1149 */ 1150 static void 1151 ig4iic_intr(void *cookie) 1152 { 1153 ig4iic_softc_t *sc = cookie; 1154 uint32_t status; 1155 1156 mtx_lock(&sc->io_lock); 1157 /* reg_write(sc, IG4_REG_INTR_MASK, IG4_INTR_STOP_DET);*/ 1158 reg_read(sc, IG4_REG_CLR_INTR); 1159 status = reg_read(sc, IG4_REG_I2C_STA); 1160 while (status & IG4_STATUS_RX_NOTEMPTY) { 1161 sc->rbuf[sc->rnext & IG4_RBUFMASK] = 1162 (uint8_t)reg_read(sc, IG4_REG_DATA_CMD); 1163 ++sc->rnext; 1164 status = reg_read(sc, IG4_REG_I2C_STA); 1165 } 1166 wakeup(sc); 1167 mtx_unlock(&sc->io_lock); 1168 } 1169 1170 #define REGDUMP(sc, reg) \ 1171 device_printf(sc->dev, " %-23s %08x\n", #reg, reg_read(sc, reg)) 1172 1173 static void 1174 ig4iic_dump(ig4iic_softc_t *sc) 1175 { 1176 device_printf(sc->dev, "ig4iic register dump:\n"); 1177 REGDUMP(sc, IG4_REG_CTL); 1178 REGDUMP(sc, IG4_REG_TAR_ADD); 1179 REGDUMP(sc, IG4_REG_SS_SCL_HCNT); 1180 REGDUMP(sc, IG4_REG_SS_SCL_LCNT); 1181 REGDUMP(sc, IG4_REG_FS_SCL_HCNT); 1182 REGDUMP(sc, IG4_REG_FS_SCL_LCNT); 1183 REGDUMP(sc, IG4_REG_INTR_STAT); 1184 REGDUMP(sc, IG4_REG_INTR_MASK); 1185 REGDUMP(sc, IG4_REG_RAW_INTR_STAT); 1186 REGDUMP(sc, IG4_REG_RX_TL); 1187 REGDUMP(sc, IG4_REG_TX_TL); 1188 REGDUMP(sc, IG4_REG_I2C_EN); 1189 REGDUMP(sc, IG4_REG_I2C_STA); 1190 REGDUMP(sc, IG4_REG_TXFLR); 1191 REGDUMP(sc, IG4_REG_RXFLR); 1192 REGDUMP(sc, IG4_REG_SDA_HOLD); 1193 REGDUMP(sc, IG4_REG_TX_ABRT_SOURCE); 1194 REGDUMP(sc, IG4_REG_SLV_DATA_NACK); 1195 REGDUMP(sc, IG4_REG_DMA_CTRL); 1196 REGDUMP(sc, IG4_REG_DMA_TDLR); 1197 REGDUMP(sc, IG4_REG_DMA_RDLR); 1198 REGDUMP(sc, IG4_REG_SDA_SETUP); 1199 REGDUMP(sc, IG4_REG_ENABLE_STATUS); 1200 REGDUMP(sc, IG4_REG_COMP_PARAM1); 1201 REGDUMP(sc, IG4_REG_COMP_VER); 1202 REGDUMP(sc, IG4_REG_COMP_TYPE); 1203 REGDUMP(sc, IG4_REG_CLK_PARMS); 1204 REGDUMP(sc, IG4_REG_RESETS); 1205 REGDUMP(sc, IG4_REG_GENERAL); 1206 REGDUMP(sc, IG4_REG_SW_LTR_VALUE); 1207 REGDUMP(sc, IG4_REG_AUTO_LTR_VALUE); 1208 } 1209 #undef REGDUMP 1210 1211 DRIVER_MODULE(iicbus, ig4iic, iicbus_driver, iicbus_devclass, NULL, NULL); 1212