2 * Copyright (c) 2014 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * Intel 4th generation mobile cpus integrated I2C device, smbus driver.
37 * See ig4_reg.h for datasheet reference and notes.
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/kernel.h>
43 #include <sys/module.h>
44 #include <sys/errno.h>
46 #include <sys/mutex.h>
47 #include <sys/syslog.h>
52 #include <bus/pci/pcivar.h>
53 #include <bus/pci/pcireg.h>
54 #include <bus/smbus/smbconf.h>
61 #define TRANS_NORMAL 1
65 static void ig4iic_intr(void *cookie);
68 * Low-level inline support functions
72 reg_write(ig4iic_softc_t *sc, uint32_t reg, uint32_t value)
74 bus_space_write_4(sc->regs_t, sc->regs_h, reg, value);
75 bus_space_barrier(sc->regs_t, sc->regs_h, reg, 4,
76 BUS_SPACE_BARRIER_WRITE);
81 reg_read(ig4iic_softc_t *sc, uint32_t reg)
85 bus_space_barrier(sc->regs_t, sc->regs_h, reg, 4,
86 BUS_SPACE_BARRIER_READ);
87 value = bus_space_read_4(sc->regs_t, sc->regs_h, reg);
92 * Enable or disable the controller and wait for the controller to acknowledge
97 set_controller(ig4iic_softc_t *sc, uint32_t ctl)
103 reg_write(sc, IG4_REG_I2C_EN, ctl);
104 error = SMB_ETIMEOUT;
106 for (retry = 100; retry > 0; --retry) {
107 v = reg_read(sc, IG4_REG_ENABLE_STATUS);
108 if (((v ^ ctl) & IG4_I2C_ENABLE) == 0) {
112 tsleep(sc, 0, "i2cslv", 1);
118 * Wait up to 25ms for the requested status using a 25uS polling loop.
122 wait_status(ig4iic_softc_t *sc, uint32_t status)
130 error = SMB_ETIMEOUT;
131 count = sys_cputimer->count();
132 limit = sys_cputimer->freq / 40;
134 while (sys_cputimer->count() - count <= limit) {
135 v = reg_read(sc, IG4_REG_I2C_STA);
141 if (status & IG4_STATUS_TX_EMPTY) {
142 v = reg_read(sc, IG4_REG_TXFLR) & IG4_FIFOLVL_MASK;
145 count = sys_cputimer->count();
149 if (status & IG4_STATUS_RX_NOTEMPTY) {
150 lksleep(sc, &sc->lk, 0, "i2cwait", (hz + 99) / 100);
159 * Set the slave address. The controller must be disabled when
160 * changing the address.
162 * This operation does not issue anything to the I2C bus but sets
163 * the target address for when the controller later issues a START.
167 set_slave_addr(ig4iic_softc_t *sc, uint8_t slave, int trans_op)
173 use_10bit = sc->use_10bit;
174 if (trans_op & SMB_TRANS_7BIT)
176 if (trans_op & SMB_TRANS_10BIT)
179 if (sc->slave_valid && sc->last_slave == slave &&
180 sc->use_10bit == use_10bit) {
183 sc->use_10bit = use_10bit;
186 * Wait for TXFIFO to drain before disabling the controller.
188 * If a write message has not been completed it's really a
189 * programming error, but for now in that case issue an extra
192 * If a read message has not been completed it's also a programming
193 * error, for now just ignore it.
195 wait_status(sc, IG4_STATUS_TX_NOTFULL);
196 if (sc->write_started) {
197 reg_write(sc, IG4_REG_DATA_CMD, IG4_DATA_STOP);
198 sc->write_started = 0;
200 if (sc->read_started)
201 sc->read_started = 0;
202 wait_status(sc, IG4_STATUS_TX_EMPTY);
204 set_controller(sc, 0);
205 ctl = reg_read(sc, IG4_REG_CTL);
206 ctl &= ~IG4_CTL_10BIT;
207 ctl |= IG4_CTL_RESTARTEN;
211 tar |= IG4_TAR_10BIT;
212 ctl |= IG4_CTL_10BIT;
214 reg_write(sc, IG4_REG_CTL, ctl);
215 reg_write(sc, IG4_REG_TAR_ADD, tar);
216 set_controller(sc, IG4_I2C_ENABLE);
218 sc->last_slave = slave;
222 * Issue START with byte command, possible count, and a variable length
223 * read or write buffer, then possible turn-around read. The read also
224 * has a possible count received.
228 * Quick: START+ADDR+RD/WR STOP
230 * Normal: START+ADDR+WR COMM DATA..DATA STOP
233 * RESTART+ADDR RDATA..RDATA STOP
234 * (can also be used for I2C transactions)
236 * Process Call: START+ADDR+WR COMM DATAL DATAH
237 * RESTART+ADDR+RD RDATAL RDATAH STOP
239 * Block: START+ADDR+RD COMM
240 * RESTART+ADDR+RD RCOUNT DATA... STOP
243 * RESTART+ADDR+WR WCOUNT DATA... STOP
245 * For I2C - basically, no *COUNT fields.
247 * Generally speaking, the START+ADDR / RESTART+ADDR is handled automatically
248 * by the controller at the beginning of a command sequence or on a data
249 * direction turn-around, and we only need to tell it when to issue the STOP.
252 smb_transaction(ig4iic_softc_t *sc, char cmd, int op,
253 char *wbuf, int wcount, char *rbuf, int rcount, int *actualp)
259 * Issue START or RESTART with next data byte, clear any previous
260 * abort condition that may have been holding the txfifo in reset.
262 last = IG4_DATA_RESTART;
263 reg_read(sc, IG4_REG_CLR_TX_ABORT);
268 * Issue command if not told otherwise (smbus).
270 if ((op & SMB_TRANS_NOCMD) == 0) {
271 error = wait_status(sc, IG4_STATUS_TX_NOTFULL);
275 if (wcount == 0 && rcount == 0 && (op & SMB_TRANS_NOSTOP) == 0)
276 last |= IG4_DATA_STOP;
277 reg_write(sc, IG4_REG_DATA_CMD, last);
282 * If writing and not told otherwise, issue the write count (smbus).
284 if (wcount && (op & SMB_TRANS_NOCNT) == 0) {
285 error = wait_status(sc, IG4_STATUS_TX_NOTFULL);
289 reg_write(sc, IG4_REG_DATA_CMD, last);
297 error = wait_status(sc, IG4_STATUS_TX_NOTFULL);
300 last |= (u_char)*wbuf;
301 if (wcount == 1 && rcount == 0 && (op & SMB_TRANS_NOSTOP) == 0)
302 last |= IG4_DATA_STOP;
303 reg_write(sc, IG4_REG_DATA_CMD, last);
310 * Issue reads to xmit FIFO (strange, I know) to tell the controller
311 * to clock in data. At the moment just issue one read ahead to
312 * pipeline the incoming data.
314 * NOTE: In the case of NOCMD and wcount == 0 we still issue a
315 * RESTART here, even if the data direction has not changed
316 * from the previous CHAINing call. This we force the RESTART.
317 * (A new START is issued automatically by the controller in
318 * the other nominal cases such as a data direction change or
319 * a previous STOP was issued).
321 * If this will be the last byte read we must also issue the STOP
322 * at the end of the read.
325 last = IG4_DATA_RESTART | IG4_DATA_COMMAND_RD;
327 (op & (SMB_TRANS_NOSTOP | SMB_TRANS_NOCNT)) ==
329 last |= IG4_DATA_STOP;
331 reg_write(sc, IG4_REG_DATA_CMD, last);
332 last = IG4_DATA_COMMAND_RD;
336 * Bulk read (i2c) and count field handling (smbus)
340 * Maintain a pipeline by queueing the allowance for the next
341 * read before waiting for the current read.
344 if (op & SMB_TRANS_NOCNT)
345 last = (rcount == 2) ? IG4_DATA_STOP : 0;
348 reg_write(sc, IG4_REG_DATA_CMD, IG4_DATA_COMMAND_RD |
351 error = wait_status(sc, IG4_STATUS_RX_NOTEMPTY);
354 last = reg_read(sc, IG4_REG_DATA_CMD);
356 if (op & SMB_TRANS_NOCNT) {
357 *rbuf = (u_char)last;
364 * Handle count field (smbus), which is not part of
365 * the rcount'ed buffer. The first read data in a
366 * bulk transfer is the count.
368 * XXX if rcount is loaded as 0 how do I generate a
369 * STOP now without issuing another RD or WR?
371 if (rcount > (u_char)last)
372 rcount = (u_char)last;
373 op |= SMB_TRANS_NOCNT;
378 /* XXX wait for xmit buffer to become empty */
379 last = reg_read(sc, IG4_REG_TX_ABRT_SOURCE);
385 * SMBUS API FUNCTIONS
387 * Called from ig4iic_pci_attach/detach()
390 ig4iic_attach(ig4iic_softc_t *sc)
395 lockmgr(&sc->lk, LK_EXCLUSIVE);
397 v = reg_read(sc, IG4_REG_COMP_TYPE);
398 kprintf("type %08x\n", v);
399 v = reg_read(sc, IG4_REG_COMP_PARAM1);
400 kprintf("params %08x\n", v);
401 v = reg_read(sc, IG4_REG_COMP_VER);
402 kprintf("version %08x\n", v);
403 if (v != IG4_COMP_VER) {
408 v = reg_read(sc, IG4_REG_SS_SCL_HCNT);
409 kprintf("SS_SCL_HCNT %08x\n", v);
410 v = reg_read(sc, IG4_REG_SS_SCL_LCNT);
411 kprintf("SS_SCL_LCNT %08x\n", v);
412 v = reg_read(sc, IG4_REG_FS_SCL_HCNT);
413 kprintf("FS_SCL_HCNT %08x\n", v);
414 v = reg_read(sc, IG4_REG_FS_SCL_LCNT);
415 kprintf("FS_SCL_LCNT %08x\n", v);
416 v = reg_read(sc, IG4_REG_SDA_HOLD);
417 kprintf("HOLD %08x\n", v);
419 v = reg_read(sc, IG4_REG_SS_SCL_HCNT);
420 reg_write(sc, IG4_REG_FS_SCL_HCNT, v);
421 v = reg_read(sc, IG4_REG_SS_SCL_LCNT);
422 reg_write(sc, IG4_REG_FS_SCL_LCNT, v);
425 * Program based on a 25000 Hz clock. This is a bit of a
426 * hack (obviously). The defaults are 400 and 470 for standard
427 * and 60 and 130 for fast. The defaults for standard fail
428 * utterly (presumably cause an abort) because the clock time
429 * is ~18.8ms by default. This brings it down to ~4ms (for now).
431 reg_write(sc, IG4_REG_SS_SCL_HCNT, 100);
432 reg_write(sc, IG4_REG_SS_SCL_LCNT, 125);
433 reg_write(sc, IG4_REG_FS_SCL_HCNT, 100);
434 reg_write(sc, IG4_REG_FS_SCL_LCNT, 125);
437 * Use a threshold of 1 so we get interrupted on each character,
438 * allowing us to use lksleep() in our poll code. Not perfect
439 * but this is better than using DELAY() for receiving data.
441 reg_write(sc, IG4_REG_RX_TL, 1);
443 reg_write(sc, IG4_REG_CTL,
445 IG4_CTL_SLAVE_DISABLE |
449 sc->smb = device_add_child(sc->dev, "smbus", -1);
450 if (sc->smb == NULL) {
451 device_printf(sc->dev, "smbus driver not found\n");
458 * Don't do this, it blows up the PCI config
460 reg_write(sc, IG4_REG_RESETS, IG4_RESETS_ASSERT);
461 reg_write(sc, IG4_REG_RESETS, IG4_RESETS_DEASSERT);
465 * Interrupt on STOP detect or receive character ready
467 reg_write(sc, IG4_REG_INTR_MASK, IG4_INTR_STOP_DET |
469 if (set_controller(sc, 0))
470 device_printf(sc->dev, "controller error during attach-1\n");
471 if (set_controller(sc, IG4_I2C_ENABLE))
472 device_printf(sc->dev, "controller error during attach-2\n");
473 error = bus_setup_intr(sc->dev, sc->intr_res, 0,
474 ig4iic_intr, sc, &sc->intr_handle, NULL);
476 device_printf(sc->dev,
477 "Unable to setup irq: error %d\n", error);
481 /* Attach us to the smbus */
482 lockmgr(&sc->lk, LK_RELEASE);
483 error = bus_generic_attach(sc->dev);
484 lockmgr(&sc->lk, LK_EXCLUSIVE);
486 device_printf(sc->dev,
487 "failed to attach child: error %d\n", error);
490 sc->generic_attached = 1;
493 lockmgr(&sc->lk, LK_RELEASE);
498 ig4iic_detach(ig4iic_softc_t *sc)
502 lockmgr(&sc->lk, LK_EXCLUSIVE);
504 reg_write(sc, IG4_REG_INTR_MASK, 0);
505 reg_read(sc, IG4_REG_CLR_INTR);
506 set_controller(sc, 0);
508 if (sc->generic_attached) {
509 error = bus_generic_detach(sc->dev);
512 sc->generic_attached = 0;
515 device_delete_child(sc->dev, sc->smb);
518 if (sc->intr_handle) {
519 bus_teardown_intr(sc->dev, sc->intr_res, sc->intr_handle);
520 sc->intr_handle = NULL;
525 lockmgr(&sc->lk, LK_RELEASE);
530 ig4iic_smb_callback(device_t dev, int index, void *data)
532 ig4iic_softc_t *sc = device_get_softc(dev);
535 lockmgr(&sc->lk, LK_EXCLUSIVE);
538 case SMB_REQUEST_BUS:
541 case SMB_RELEASE_BUS:
549 lockmgr(&sc->lk, LK_RELEASE);
555 * Quick command. i.e. START + cmd + R/W + STOP and no data. It is
556 * unclear to me how I could implement this with the intel i2c controller
557 * because the controler sends STARTs and STOPs automatically with data.
560 ig4iic_smb_quick(device_t dev, u_char slave, int how)
562 ig4iic_softc_t *sc = device_get_softc(dev);
565 lockmgr(&sc->lk, LK_EXCLUSIVE);
569 error = SMB_ENOTSUPP;
572 error = SMB_ENOTSUPP;
575 error = SMB_ENOTSUPP;
578 lockmgr(&sc->lk, LK_RELEASE);
584 * Incremental send byte without stop (?). It is unclear why the slave
585 * address is specified if this presumably is used in combination with
586 * ig4iic_smb_quick().
588 * (Also, how would this work anyway? Issue the last byte with writeb()?)
591 ig4iic_smb_sendb(device_t dev, u_char slave, char byte)
593 ig4iic_softc_t *sc = device_get_softc(dev);
597 lockmgr(&sc->lk, LK_EXCLUSIVE);
599 set_slave_addr(sc, slave, 0);
601 if (wait_status(sc, IG4_STATUS_TX_NOTFULL) == 0) {
602 reg_write(sc, IG4_REG_DATA_CMD, cmd);
605 error = SMB_ETIMEOUT;
608 lockmgr(&sc->lk, LK_RELEASE);
613 * Incremental receive byte without stop (?). It is unclear why the slave
614 * address is specified if this presumably is used in combination with
615 * ig4iic_smb_quick().
618 ig4iic_smb_recvb(device_t dev, u_char slave, char *byte)
620 ig4iic_softc_t *sc = device_get_softc(dev);
623 lockmgr(&sc->lk, LK_EXCLUSIVE);
625 set_slave_addr(sc, slave, 0);
626 reg_write(sc, IG4_REG_DATA_CMD, IG4_DATA_COMMAND_RD);
627 if (wait_status(sc, IG4_STATUS_RX_NOTEMPTY) == 0) {
628 *byte = (uint8_t)reg_read(sc, IG4_REG_DATA_CMD);
632 error = SMB_ETIMEOUT;
635 lockmgr(&sc->lk, LK_RELEASE);
640 * Write command and single byte in transaction.
643 ig4iic_smb_writeb(device_t dev, u_char slave, char cmd, char byte)
645 ig4iic_softc_t *sc = device_get_softc(dev);
648 lockmgr(&sc->lk, LK_EXCLUSIVE);
650 set_slave_addr(sc, slave, 0);
651 error = smb_transaction(sc, cmd, SMB_TRANS_NOCNT,
652 &byte, 1, NULL, 0, NULL);
654 lockmgr(&sc->lk, LK_RELEASE);
659 * Write command and single word in transaction.
662 ig4iic_smb_writew(device_t dev, u_char slave, char cmd, short word)
664 ig4iic_softc_t *sc = device_get_softc(dev);
668 lockmgr(&sc->lk, LK_EXCLUSIVE);
670 set_slave_addr(sc, slave, 0);
671 buf[0] = word & 0xFF;
673 error = smb_transaction(sc, cmd, SMB_TRANS_NOCNT,
674 buf, 2, NULL, 0, NULL);
676 lockmgr(&sc->lk, LK_RELEASE);
681 * write command and read single byte in transaction.
684 ig4iic_smb_readb(device_t dev, u_char slave, char cmd, char *byte)
686 ig4iic_softc_t *sc = device_get_softc(dev);
689 lockmgr(&sc->lk, LK_EXCLUSIVE);
691 set_slave_addr(sc, slave, 0);
692 error = smb_transaction(sc, cmd, SMB_TRANS_NOCNT,
693 NULL, 0, byte, 1, NULL);
695 lockmgr(&sc->lk, LK_RELEASE);
700 * write command and read word in transaction.
703 ig4iic_smb_readw(device_t dev, u_char slave, char cmd, short *word)
705 ig4iic_softc_t *sc = device_get_softc(dev);
709 lockmgr(&sc->lk, LK_EXCLUSIVE);
711 set_slave_addr(sc, slave, 0);
712 if ((error = smb_transaction(sc, cmd, SMB_TRANS_NOCNT,
713 NULL, 0, buf, 2, NULL)) == 0) {
714 *word = (u_char)buf[0] | ((u_char)buf[1] << 8);
717 lockmgr(&sc->lk, LK_RELEASE);
722 * write command and word and read word in transaction
725 ig4iic_smb_pcall(device_t dev, u_char slave, char cmd,
726 short sdata, short *rdata)
728 ig4iic_softc_t *sc = device_get_softc(dev);
733 lockmgr(&sc->lk, LK_EXCLUSIVE);
735 set_slave_addr(sc, slave, 0);
736 wbuf[0] = sdata & 0xFF;
737 wbuf[1] = sdata >> 8;
738 if ((error = smb_transaction(sc, cmd, SMB_TRANS_NOCNT,
739 wbuf, 2, rbuf, 2, NULL)) == 0) {
740 *rdata = (u_char)rbuf[0] | ((u_char)rbuf[1] << 8);
743 lockmgr(&sc->lk, LK_RELEASE);
748 ig4iic_smb_bwrite(device_t dev, u_char slave, char cmd,
749 u_char wcount, char *buf)
751 ig4iic_softc_t *sc = device_get_softc(dev);
754 lockmgr(&sc->lk, LK_EXCLUSIVE);
756 set_slave_addr(sc, slave, 0);
757 error = smb_transaction(sc, cmd, 0,
758 buf, wcount, NULL, 0, NULL);
760 lockmgr(&sc->lk, LK_RELEASE);
765 ig4iic_smb_bread(device_t dev, u_char slave, char cmd,
766 u_char *countp_char, char *buf)
768 ig4iic_softc_t *sc = device_get_softc(dev);
769 int rcount = *countp_char;
772 lockmgr(&sc->lk, LK_EXCLUSIVE);
774 set_slave_addr(sc, slave, 0);
775 error = smb_transaction(sc, cmd, 0,
776 NULL, 0, buf, rcount, &rcount);
777 *countp_char = rcount;
779 lockmgr(&sc->lk, LK_RELEASE);
784 ig4iic_smb_trans(device_t dev, int slave, char cmd, int op,
785 char *wbuf, int wcount, char *rbuf, int rcount,
788 ig4iic_softc_t *sc = device_get_softc(dev);
791 lockmgr(&sc->lk, LK_EXCLUSIVE);
793 set_slave_addr(sc, slave, op);
794 error = smb_transaction(sc, cmd, op,
795 wbuf, wcount, rbuf, rcount, actualp);
797 lockmgr(&sc->lk, LK_RELEASE);
802 * Interrupt Operation
806 ig4iic_intr(void *cookie)
808 ig4iic_softc_t *sc = cookie;
810 lockmgr(&sc->lk, LK_EXCLUSIVE);
811 /* reg_write(sc, IG4_REG_INTR_MASK, IG4_INTR_STOP_DET);*/
812 reg_read(sc, IG4_REG_CLR_INTR);
814 lockmgr(&sc->lk, LK_RELEASE);
817 DRIVER_MODULE(smbus, ig4iic, smbus_driver, smbus_devclass, NULL, NULL);