Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * Copyright (c) 2012 Intel Corporation. All rights reserved.
0003  * Copyright (c) 2006 - 2012 QLogic Corporation. All rights reserved.
0004  * Copyright (c) 2003, 2004, 2005, 2006 PathScale, Inc. All rights reserved.
0005  *
0006  * This software is available to you under a choice of one of two
0007  * licenses.  You may choose to be licensed under the terms of the GNU
0008  * General Public License (GPL) Version 2, available from the file
0009  * COPYING in the main directory of this source tree, or the
0010  * OpenIB.org BSD license below:
0011  *
0012  *     Redistribution and use in source and binary forms, with or
0013  *     without modification, are permitted provided that the following
0014  *     conditions are met:
0015  *
0016  *      - Redistributions of source code must retain the above
0017  *        copyright notice, this list of conditions and the following
0018  *        disclaimer.
0019  *
0020  *      - Redistributions in binary form must reproduce the above
0021  *        copyright notice, this list of conditions and the following
0022  *        disclaimer in the documentation and/or other materials
0023  *        provided with the distribution.
0024  *
0025  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
0026  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
0027  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
0028  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
0029  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
0030  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
0031  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
0032  * SOFTWARE.
0033  */
0034 
0035 #include <linux/delay.h>
0036 #include <linux/pci.h>
0037 #include <linux/vmalloc.h>
0038 
0039 #include "qib.h"
0040 
0041 /*
0042  * QLogic_IB "Two Wire Serial Interface" driver.
0043  * Originally written for a not-quite-i2c serial eeprom, which is
0044  * still used on some supported boards. Later boards have added a
0045  * variety of other uses, most board-specific, so the bit-boffing
0046  * part has been split off to this file, while the other parts
0047  * have been moved to chip-specific files.
0048  *
0049  * We have also dropped all pretense of fully generic (e.g. pretend
0050  * we don't know whether '1' is the higher voltage) interface, as
0051  * the restrictions of the generic i2c interface (e.g. no access from
0052  * driver itself) make it unsuitable for this use.
0053  */
0054 
0055 #define READ_CMD 1
0056 #define WRITE_CMD 0
0057 
0058 /**
0059  * i2c_wait_for_writes - wait for a write
0060  * @dd: the qlogic_ib device
0061  *
0062  * We use this instead of udelay directly, so we can make sure
0063  * that previous register writes have been flushed all the way
0064  * to the chip.  Since we are delaying anyway, the cost doesn't
0065  * hurt, and makes the bit twiddling more regular
0066  */
0067 static void i2c_wait_for_writes(struct qib_devdata *dd)
0068 {
0069     /*
0070      * implicit read of EXTStatus is as good as explicit
0071      * read of scratch, if all we want to do is flush
0072      * writes.
0073      */
0074     dd->f_gpio_mod(dd, 0, 0, 0);
0075     rmb(); /* inlined, so prevent compiler reordering */
0076 }
0077 
0078 /*
0079  * QSFP modules are allowed to hold SCL low for 500uSec. Allow twice that
0080  * for "almost compliant" modules
0081  */
0082 #define SCL_WAIT_USEC 1000
0083 
0084 /* BUF_WAIT is time bus must be free between STOP or ACK and to next START.
0085  * Should be 20, but some chips need more.
0086  */
0087 #define TWSI_BUF_WAIT_USEC 60
0088 
0089 static void scl_out(struct qib_devdata *dd, u8 bit)
0090 {
0091     u32 mask;
0092 
0093     udelay(1);
0094 
0095     mask = 1UL << dd->gpio_scl_num;
0096 
0097     /* SCL is meant to be bare-drain, so never set "OUT", just DIR */
0098     dd->f_gpio_mod(dd, 0, bit ? 0 : mask, mask);
0099 
0100     /*
0101      * Allow for slow slaves by simple
0102      * delay for falling edge, sampling on rise.
0103      */
0104     if (!bit)
0105         udelay(2);
0106     else {
0107         int rise_usec;
0108 
0109         for (rise_usec = SCL_WAIT_USEC; rise_usec > 0; rise_usec -= 2) {
0110             if (mask & dd->f_gpio_mod(dd, 0, 0, 0))
0111                 break;
0112             udelay(2);
0113         }
0114         if (rise_usec <= 0)
0115             qib_dev_err(dd, "SCL interface stuck low > %d uSec\n",
0116                     SCL_WAIT_USEC);
0117     }
0118     i2c_wait_for_writes(dd);
0119 }
0120 
0121 static void sda_out(struct qib_devdata *dd, u8 bit)
0122 {
0123     u32 mask;
0124 
0125     mask = 1UL << dd->gpio_sda_num;
0126 
0127     /* SDA is meant to be bare-drain, so never set "OUT", just DIR */
0128     dd->f_gpio_mod(dd, 0, bit ? 0 : mask, mask);
0129 
0130     i2c_wait_for_writes(dd);
0131     udelay(2);
0132 }
0133 
0134 static u8 sda_in(struct qib_devdata *dd, int wait)
0135 {
0136     int bnum;
0137     u32 read_val, mask;
0138 
0139     bnum = dd->gpio_sda_num;
0140     mask = (1UL << bnum);
0141     /* SDA is meant to be bare-drain, so never set "OUT", just DIR */
0142     dd->f_gpio_mod(dd, 0, 0, mask);
0143     read_val = dd->f_gpio_mod(dd, 0, 0, 0);
0144     if (wait)
0145         i2c_wait_for_writes(dd);
0146     return (read_val & mask) >> bnum;
0147 }
0148 
0149 /**
0150  * i2c_ackrcv - see if ack following write is true
0151  * @dd: the qlogic_ib device
0152  */
0153 static int i2c_ackrcv(struct qib_devdata *dd)
0154 {
0155     u8 ack_received;
0156 
0157     /* AT ENTRY SCL = LOW */
0158     /* change direction, ignore data */
0159     ack_received = sda_in(dd, 1);
0160     scl_out(dd, 1);
0161     ack_received = sda_in(dd, 1) == 0;
0162     scl_out(dd, 0);
0163     return ack_received;
0164 }
0165 
0166 static void stop_cmd(struct qib_devdata *dd);
0167 
0168 /**
0169  * rd_byte - read a byte, sending STOP on last, else ACK
0170  * @dd: the qlogic_ib device
0171  * @last: identifies the last read
0172  *
0173  * Returns byte shifted out of device
0174  */
0175 static int rd_byte(struct qib_devdata *dd, int last)
0176 {
0177     int bit_cntr, data;
0178 
0179     data = 0;
0180 
0181     for (bit_cntr = 7; bit_cntr >= 0; --bit_cntr) {
0182         data <<= 1;
0183         scl_out(dd, 1);
0184         data |= sda_in(dd, 0);
0185         scl_out(dd, 0);
0186     }
0187     if (last) {
0188         scl_out(dd, 1);
0189         stop_cmd(dd);
0190     } else {
0191         sda_out(dd, 0);
0192         scl_out(dd, 1);
0193         scl_out(dd, 0);
0194         sda_out(dd, 1);
0195     }
0196     return data;
0197 }
0198 
0199 /**
0200  * wr_byte - write a byte, one bit at a time
0201  * @dd: the qlogic_ib device
0202  * @data: the byte to write
0203  *
0204  * Returns 0 if we got the following ack, otherwise 1
0205  */
0206 static int wr_byte(struct qib_devdata *dd, u8 data)
0207 {
0208     int bit_cntr;
0209     u8 bit;
0210 
0211     for (bit_cntr = 7; bit_cntr >= 0; bit_cntr--) {
0212         bit = (data >> bit_cntr) & 1;
0213         sda_out(dd, bit);
0214         scl_out(dd, 1);
0215         scl_out(dd, 0);
0216     }
0217     return (!i2c_ackrcv(dd)) ? 1 : 0;
0218 }
0219 
0220 /*
0221  * issue TWSI start sequence:
0222  * (both clock/data high, clock high, data low while clock is high)
0223  */
0224 static void start_seq(struct qib_devdata *dd)
0225 {
0226     sda_out(dd, 1);
0227     scl_out(dd, 1);
0228     sda_out(dd, 0);
0229     udelay(1);
0230     scl_out(dd, 0);
0231 }
0232 
0233 /**
0234  * stop_seq - transmit the stop sequence
0235  * @dd: the qlogic_ib device
0236  *
0237  * (both clock/data low, clock high, data high while clock is high)
0238  */
0239 static void stop_seq(struct qib_devdata *dd)
0240 {
0241     scl_out(dd, 0);
0242     sda_out(dd, 0);
0243     scl_out(dd, 1);
0244     sda_out(dd, 1);
0245 }
0246 
0247 /**
0248  * stop_cmd - transmit the stop condition
0249  * @dd: the qlogic_ib device
0250  *
0251  * (both clock/data low, clock high, data high while clock is high)
0252  */
0253 static void stop_cmd(struct qib_devdata *dd)
0254 {
0255     stop_seq(dd);
0256     udelay(TWSI_BUF_WAIT_USEC);
0257 }
0258 
0259 /**
0260  * qib_twsi_reset - reset I2C communication
0261  * @dd: the qlogic_ib device
0262  */
0263 
0264 int qib_twsi_reset(struct qib_devdata *dd)
0265 {
0266     int clock_cycles_left = 9;
0267     int was_high = 0;
0268     u32 pins, mask;
0269 
0270     /* Both SCL and SDA should be high. If not, there
0271      * is something wrong.
0272      */
0273     mask = (1UL << dd->gpio_scl_num) | (1UL << dd->gpio_sda_num);
0274 
0275     /*
0276      * Force pins to desired innocuous state.
0277      * This is the default power-on state with out=0 and dir=0,
0278      * So tri-stated and should be floating high (barring HW problems)
0279      */
0280     dd->f_gpio_mod(dd, 0, 0, mask);
0281 
0282     /*
0283      * Clock nine times to get all listeners into a sane state.
0284      * If SDA does not go high at any point, we are wedged.
0285      * One vendor recommends then issuing START followed by STOP.
0286      * we cannot use our "normal" functions to do that, because
0287      * if SCL drops between them, another vendor's part will
0288      * wedge, dropping SDA and keeping it low forever, at the end of
0289      * the next transaction (even if it was not the device addressed).
0290      * So our START and STOP take place with SCL held high.
0291      */
0292     while (clock_cycles_left--) {
0293         scl_out(dd, 0);
0294         scl_out(dd, 1);
0295         /* Note if SDA is high, but keep clocking to sync slave */
0296         was_high |= sda_in(dd, 0);
0297     }
0298 
0299     if (was_high) {
0300         /*
0301          * We saw a high, which we hope means the slave is sync'd.
0302          * Issue START, STOP, pause for T_BUF.
0303          */
0304 
0305         pins = dd->f_gpio_mod(dd, 0, 0, 0);
0306         if ((pins & mask) != mask)
0307             qib_dev_err(dd, "GPIO pins not at rest: %d\n",
0308                     pins & mask);
0309         /* Drop SDA to issue START */
0310         udelay(1); /* Guarantee .6 uSec setup */
0311         sda_out(dd, 0);
0312         udelay(1); /* Guarantee .6 uSec hold */
0313         /* At this point, SCL is high, SDA low. Raise SDA for STOP */
0314         sda_out(dd, 1);
0315         udelay(TWSI_BUF_WAIT_USEC);
0316     }
0317 
0318     return !was_high;
0319 }
0320 
0321 #define QIB_TWSI_START 0x100
0322 #define QIB_TWSI_STOP 0x200
0323 
0324 /* Write byte to TWSI, optionally prefixed with START or suffixed with
0325  * STOP.
0326  * returns 0 if OK (ACK received), else != 0
0327  */
0328 static int qib_twsi_wr(struct qib_devdata *dd, int data, int flags)
0329 {
0330     int ret = 1;
0331 
0332     if (flags & QIB_TWSI_START)
0333         start_seq(dd);
0334 
0335     ret = wr_byte(dd, data); /* Leaves SCL low (from i2c_ackrcv()) */
0336 
0337     if (flags & QIB_TWSI_STOP)
0338         stop_cmd(dd);
0339     return ret;
0340 }
0341 
0342 /* Added functionality for IBA7220-based cards */
0343 #define QIB_TEMP_DEV 0x98
0344 
0345 /*
0346  * qib_twsi_blk_rd
0347  * Formerly called qib_eeprom_internal_read, and only used for eeprom,
0348  * but now the general interface for data transfer from twsi devices.
0349  * One vestige of its former role is that it recognizes a device
0350  * QIB_TWSI_NO_DEV and does the correct operation for the legacy part,
0351  * which responded to all TWSI device codes, interpreting them as
0352  * address within device. On all other devices found on board handled by
0353  * this driver, the device is followed by a one-byte "address" which selects
0354  * the "register" or "offset" within the device from which data should
0355  * be read.
0356  */
0357 int qib_twsi_blk_rd(struct qib_devdata *dd, int dev, int addr,
0358             void *buffer, int len)
0359 {
0360     int ret;
0361     u8 *bp = buffer;
0362 
0363     ret = 1;
0364 
0365     if (dev == QIB_TWSI_NO_DEV) {
0366         /* legacy not-really-I2C */
0367         addr = (addr << 1) | READ_CMD;
0368         ret = qib_twsi_wr(dd, addr, QIB_TWSI_START);
0369     } else {
0370         /* Actual I2C */
0371         ret = qib_twsi_wr(dd, dev | WRITE_CMD, QIB_TWSI_START);
0372         if (ret) {
0373             stop_cmd(dd);
0374             ret = 1;
0375             goto bail;
0376         }
0377         /*
0378          * SFF spec claims we do _not_ stop after the addr
0379          * but simply issue a start with the "read" dev-addr.
0380          * Since we are implicitely waiting for ACK here,
0381          * we need t_buf (nominally 20uSec) before that start,
0382          * and cannot rely on the delay built in to the STOP
0383          */
0384         ret = qib_twsi_wr(dd, addr, 0);
0385         udelay(TWSI_BUF_WAIT_USEC);
0386 
0387         if (ret) {
0388             qib_dev_err(dd,
0389                 "Failed to write interface read addr %02X\n",
0390                 addr);
0391             ret = 1;
0392             goto bail;
0393         }
0394         ret = qib_twsi_wr(dd, dev | READ_CMD, QIB_TWSI_START);
0395     }
0396     if (ret) {
0397         stop_cmd(dd);
0398         ret = 1;
0399         goto bail;
0400     }
0401 
0402     /*
0403      * block devices keeps clocking data out as long as we ack,
0404      * automatically incrementing the address. Some have "pages"
0405      * whose boundaries will not be crossed, but the handling
0406      * of these is left to the caller, who is in a better
0407      * position to know.
0408      */
0409     while (len-- > 0) {
0410         /*
0411          * Get and store data, sending ACK if length remaining,
0412          * else STOP
0413          */
0414         *bp++ = rd_byte(dd, !len);
0415     }
0416 
0417     ret = 0;
0418 
0419 bail:
0420     return ret;
0421 }
0422 
0423 /*
0424  * qib_twsi_blk_wr
0425  * Formerly called qib_eeprom_internal_write, and only used for eeprom,
0426  * but now the general interface for data transfer to twsi devices.
0427  * One vestige of its former role is that it recognizes a device
0428  * QIB_TWSI_NO_DEV and does the correct operation for the legacy part,
0429  * which responded to all TWSI device codes, interpreting them as
0430  * address within device. On all other devices found on board handled by
0431  * this driver, the device is followed by a one-byte "address" which selects
0432  * the "register" or "offset" within the device to which data should
0433  * be written.
0434  */
0435 int qib_twsi_blk_wr(struct qib_devdata *dd, int dev, int addr,
0436             const void *buffer, int len)
0437 {
0438     int sub_len;
0439     const u8 *bp = buffer;
0440     int max_wait_time, i;
0441     int ret = 1;
0442 
0443     while (len > 0) {
0444         if (dev == QIB_TWSI_NO_DEV) {
0445             if (qib_twsi_wr(dd, (addr << 1) | WRITE_CMD,
0446                     QIB_TWSI_START)) {
0447                 goto failed_write;
0448             }
0449         } else {
0450             /* Real I2C */
0451             if (qib_twsi_wr(dd, dev | WRITE_CMD, QIB_TWSI_START))
0452                 goto failed_write;
0453             ret = qib_twsi_wr(dd, addr, 0);
0454             if (ret) {
0455                 qib_dev_err(dd,
0456                     "Failed to write interface write addr %02X\n",
0457                     addr);
0458                 goto failed_write;
0459             }
0460         }
0461 
0462         sub_len = min(len, 4);
0463         addr += sub_len;
0464         len -= sub_len;
0465 
0466         for (i = 0; i < sub_len; i++)
0467             if (qib_twsi_wr(dd, *bp++, 0))
0468                 goto failed_write;
0469 
0470         stop_cmd(dd);
0471 
0472         /*
0473          * Wait for write complete by waiting for a successful
0474          * read (the chip replies with a zero after the write
0475          * cmd completes, and before it writes to the eeprom.
0476          * The startcmd for the read will fail the ack until
0477          * the writes have completed.   We do this inline to avoid
0478          * the debug prints that are in the real read routine
0479          * if the startcmd fails.
0480          * We also use the proper device address, so it doesn't matter
0481          * whether we have real eeprom_dev. Legacy likes any address.
0482          */
0483         max_wait_time = 100;
0484         while (qib_twsi_wr(dd, dev | READ_CMD, QIB_TWSI_START)) {
0485             stop_cmd(dd);
0486             if (!--max_wait_time)
0487                 goto failed_write;
0488         }
0489         /* now read (and ignore) the resulting byte */
0490         rd_byte(dd, 1);
0491     }
0492 
0493     ret = 0;
0494     goto bail;
0495 
0496 failed_write:
0497     stop_cmd(dd);
0498     ret = 1;
0499 
0500 bail:
0501     return ret;
0502 }