Path: blob/master/drivers/infiniband/hw/qib/qib_twsi.c
15112 views
/*1* Copyright (c) 2006, 2007, 2008, 2009 QLogic Corporation. All rights reserved.2* Copyright (c) 2003, 2004, 2005, 2006 PathScale, Inc. All rights reserved.3*4* This software is available to you under a choice of one of two5* licenses. You may choose to be licensed under the terms of the GNU6* General Public License (GPL) Version 2, available from the file7* COPYING in the main directory of this source tree, or the8* OpenIB.org BSD license below:9*10* Redistribution and use in source and binary forms, with or11* without modification, are permitted provided that the following12* conditions are met:13*14* - Redistributions of source code must retain the above15* copyright notice, this list of conditions and the following16* disclaimer.17*18* - Redistributions in binary form must reproduce the above19* copyright notice, this list of conditions and the following20* disclaimer in the documentation and/or other materials21* provided with the distribution.22*23* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,24* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF25* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND26* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS27* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN28* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN29* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE30* SOFTWARE.31*/3233#include <linux/delay.h>34#include <linux/pci.h>35#include <linux/vmalloc.h>3637#include "qib.h"3839/*40* QLogic_IB "Two Wire Serial Interface" driver.41* Originally written for a not-quite-i2c serial eeprom, which is42* still used on some supported boards. Later boards have added a43* variety of other uses, most board-specific, so the bit-boffing44* part has been split off to this file, while the other parts45* have been moved to chip-specific files.46*47* We have also dropped all pretense of fully generic (e.g. pretend48* we don't know whether '1' is the higher voltage) interface, as49* the restrictions of the generic i2c interface (e.g. no access from50* driver itself) make it unsuitable for this use.51*/5253#define READ_CMD 154#define WRITE_CMD 05556/**57* i2c_wait_for_writes - wait for a write58* @dd: the qlogic_ib device59*60* We use this instead of udelay directly, so we can make sure61* that previous register writes have been flushed all the way62* to the chip. Since we are delaying anyway, the cost doesn't63* hurt, and makes the bit twiddling more regular64*/65static void i2c_wait_for_writes(struct qib_devdata *dd)66{67/*68* implicit read of EXTStatus is as good as explicit69* read of scratch, if all we want to do is flush70* writes.71*/72dd->f_gpio_mod(dd, 0, 0, 0);73rmb(); /* inlined, so prevent compiler reordering */74}7576/*77* QSFP modules are allowed to hold SCL low for 500uSec. Allow twice that78* for "almost compliant" modules79*/80#define SCL_WAIT_USEC 10008182/* BUF_WAIT is time bus must be free between STOP or ACK and to next START.83* Should be 20, but some chips need more.84*/85#define TWSI_BUF_WAIT_USEC 608687static void scl_out(struct qib_devdata *dd, u8 bit)88{89u32 mask;9091udelay(1);9293mask = 1UL << dd->gpio_scl_num;9495/* SCL is meant to be bare-drain, so never set "OUT", just DIR */96dd->f_gpio_mod(dd, 0, bit ? 0 : mask, mask);9798/*99* Allow for slow slaves by simple100* delay for falling edge, sampling on rise.101*/102if (!bit)103udelay(2);104else {105int rise_usec;106for (rise_usec = SCL_WAIT_USEC; rise_usec > 0; rise_usec -= 2) {107if (mask & dd->f_gpio_mod(dd, 0, 0, 0))108break;109udelay(2);110}111if (rise_usec <= 0)112qib_dev_err(dd, "SCL interface stuck low > %d uSec\n",113SCL_WAIT_USEC);114}115i2c_wait_for_writes(dd);116}117118static void sda_out(struct qib_devdata *dd, u8 bit)119{120u32 mask;121122mask = 1UL << dd->gpio_sda_num;123124/* SDA is meant to be bare-drain, so never set "OUT", just DIR */125dd->f_gpio_mod(dd, 0, bit ? 0 : mask, mask);126127i2c_wait_for_writes(dd);128udelay(2);129}130131static u8 sda_in(struct qib_devdata *dd, int wait)132{133int bnum;134u32 read_val, mask;135136bnum = dd->gpio_sda_num;137mask = (1UL << bnum);138/* SDA is meant to be bare-drain, so never set "OUT", just DIR */139dd->f_gpio_mod(dd, 0, 0, mask);140read_val = dd->f_gpio_mod(dd, 0, 0, 0);141if (wait)142i2c_wait_for_writes(dd);143return (read_val & mask) >> bnum;144}145146/**147* i2c_ackrcv - see if ack following write is true148* @dd: the qlogic_ib device149*/150static int i2c_ackrcv(struct qib_devdata *dd)151{152u8 ack_received;153154/* AT ENTRY SCL = LOW */155/* change direction, ignore data */156ack_received = sda_in(dd, 1);157scl_out(dd, 1);158ack_received = sda_in(dd, 1) == 0;159scl_out(dd, 0);160return ack_received;161}162163static void stop_cmd(struct qib_devdata *dd);164165/**166* rd_byte - read a byte, sending STOP on last, else ACK167* @dd: the qlogic_ib device168*169* Returns byte shifted out of device170*/171static int rd_byte(struct qib_devdata *dd, int last)172{173int bit_cntr, data;174175data = 0;176177for (bit_cntr = 7; bit_cntr >= 0; --bit_cntr) {178data <<= 1;179scl_out(dd, 1);180data |= sda_in(dd, 0);181scl_out(dd, 0);182}183if (last) {184scl_out(dd, 1);185stop_cmd(dd);186} else {187sda_out(dd, 0);188scl_out(dd, 1);189scl_out(dd, 0);190sda_out(dd, 1);191}192return data;193}194195/**196* wr_byte - write a byte, one bit at a time197* @dd: the qlogic_ib device198* @data: the byte to write199*200* Returns 0 if we got the following ack, otherwise 1201*/202static int wr_byte(struct qib_devdata *dd, u8 data)203{204int bit_cntr;205u8 bit;206207for (bit_cntr = 7; bit_cntr >= 0; bit_cntr--) {208bit = (data >> bit_cntr) & 1;209sda_out(dd, bit);210scl_out(dd, 1);211scl_out(dd, 0);212}213return (!i2c_ackrcv(dd)) ? 1 : 0;214}215216/*217* issue TWSI start sequence:218* (both clock/data high, clock high, data low while clock is high)219*/220static void start_seq(struct qib_devdata *dd)221{222sda_out(dd, 1);223scl_out(dd, 1);224sda_out(dd, 0);225udelay(1);226scl_out(dd, 0);227}228229/**230* stop_seq - transmit the stop sequence231* @dd: the qlogic_ib device232*233* (both clock/data low, clock high, data high while clock is high)234*/235static void stop_seq(struct qib_devdata *dd)236{237scl_out(dd, 0);238sda_out(dd, 0);239scl_out(dd, 1);240sda_out(dd, 1);241}242243/**244* stop_cmd - transmit the stop condition245* @dd: the qlogic_ib device246*247* (both clock/data low, clock high, data high while clock is high)248*/249static void stop_cmd(struct qib_devdata *dd)250{251stop_seq(dd);252udelay(TWSI_BUF_WAIT_USEC);253}254255/**256* qib_twsi_reset - reset I2C communication257* @dd: the qlogic_ib device258*/259260int qib_twsi_reset(struct qib_devdata *dd)261{262int clock_cycles_left = 9;263int was_high = 0;264u32 pins, mask;265266/* Both SCL and SDA should be high. If not, there267* is something wrong.268*/269mask = (1UL << dd->gpio_scl_num) | (1UL << dd->gpio_sda_num);270271/*272* Force pins to desired innocuous state.273* This is the default power-on state with out=0 and dir=0,274* So tri-stated and should be floating high (barring HW problems)275*/276dd->f_gpio_mod(dd, 0, 0, mask);277278/*279* Clock nine times to get all listeners into a sane state.280* If SDA does not go high at any point, we are wedged.281* One vendor recommends then issuing START followed by STOP.282* we cannot use our "normal" functions to do that, because283* if SCL drops between them, another vendor's part will284* wedge, dropping SDA and keeping it low forever, at the end of285* the next transaction (even if it was not the device addressed).286* So our START and STOP take place with SCL held high.287*/288while (clock_cycles_left--) {289scl_out(dd, 0);290scl_out(dd, 1);291/* Note if SDA is high, but keep clocking to sync slave */292was_high |= sda_in(dd, 0);293}294295if (was_high) {296/*297* We saw a high, which we hope means the slave is sync'd.298* Issue START, STOP, pause for T_BUF.299*/300301pins = dd->f_gpio_mod(dd, 0, 0, 0);302if ((pins & mask) != mask)303qib_dev_err(dd, "GPIO pins not at rest: %d\n",304pins & mask);305/* Drop SDA to issue START */306udelay(1); /* Guarantee .6 uSec setup */307sda_out(dd, 0);308udelay(1); /* Guarantee .6 uSec hold */309/* At this point, SCL is high, SDA low. Raise SDA for STOP */310sda_out(dd, 1);311udelay(TWSI_BUF_WAIT_USEC);312}313314return !was_high;315}316317#define QIB_TWSI_START 0x100318#define QIB_TWSI_STOP 0x200319320/* Write byte to TWSI, optionally prefixed with START or suffixed with321* STOP.322* returns 0 if OK (ACK received), else != 0323*/324static int qib_twsi_wr(struct qib_devdata *dd, int data, int flags)325{326int ret = 1;327if (flags & QIB_TWSI_START)328start_seq(dd);329330ret = wr_byte(dd, data); /* Leaves SCL low (from i2c_ackrcv()) */331332if (flags & QIB_TWSI_STOP)333stop_cmd(dd);334return ret;335}336337/* Added functionality for IBA7220-based cards */338#define QIB_TEMP_DEV 0x98339340/*341* qib_twsi_blk_rd342* Formerly called qib_eeprom_internal_read, and only used for eeprom,343* but now the general interface for data transfer from twsi devices.344* One vestige of its former role is that it recognizes a device345* QIB_TWSI_NO_DEV and does the correct operation for the legacy part,346* which responded to all TWSI device codes, interpreting them as347* address within device. On all other devices found on board handled by348* this driver, the device is followed by a one-byte "address" which selects349* the "register" or "offset" within the device from which data should350* be read.351*/352int qib_twsi_blk_rd(struct qib_devdata *dd, int dev, int addr,353void *buffer, int len)354{355int ret;356u8 *bp = buffer;357358ret = 1;359360if (dev == QIB_TWSI_NO_DEV) {361/* legacy not-really-I2C */362addr = (addr << 1) | READ_CMD;363ret = qib_twsi_wr(dd, addr, QIB_TWSI_START);364} else {365/* Actual I2C */366ret = qib_twsi_wr(dd, dev | WRITE_CMD, QIB_TWSI_START);367if (ret) {368stop_cmd(dd);369ret = 1;370goto bail;371}372/*373* SFF spec claims we do _not_ stop after the addr374* but simply issue a start with the "read" dev-addr.375* Since we are implicitely waiting for ACK here,376* we need t_buf (nominally 20uSec) before that start,377* and cannot rely on the delay built in to the STOP378*/379ret = qib_twsi_wr(dd, addr, 0);380udelay(TWSI_BUF_WAIT_USEC);381382if (ret) {383qib_dev_err(dd,384"Failed to write interface read addr %02X\n",385addr);386ret = 1;387goto bail;388}389ret = qib_twsi_wr(dd, dev | READ_CMD, QIB_TWSI_START);390}391if (ret) {392stop_cmd(dd);393ret = 1;394goto bail;395}396397/*398* block devices keeps clocking data out as long as we ack,399* automatically incrementing the address. Some have "pages"400* whose boundaries will not be crossed, but the handling401* of these is left to the caller, who is in a better402* position to know.403*/404while (len-- > 0) {405/*406* Get and store data, sending ACK if length remaining,407* else STOP408*/409*bp++ = rd_byte(dd, !len);410}411412ret = 0;413414bail:415return ret;416}417418/*419* qib_twsi_blk_wr420* Formerly called qib_eeprom_internal_write, and only used for eeprom,421* but now the general interface for data transfer to twsi devices.422* One vestige of its former role is that it recognizes a device423* QIB_TWSI_NO_DEV and does the correct operation for the legacy part,424* which responded to all TWSI device codes, interpreting them as425* address within device. On all other devices found on board handled by426* this driver, the device is followed by a one-byte "address" which selects427* the "register" or "offset" within the device to which data should428* be written.429*/430int qib_twsi_blk_wr(struct qib_devdata *dd, int dev, int addr,431const void *buffer, int len)432{433int sub_len;434const u8 *bp = buffer;435int max_wait_time, i;436int ret;437ret = 1;438439while (len > 0) {440if (dev == QIB_TWSI_NO_DEV) {441if (qib_twsi_wr(dd, (addr << 1) | WRITE_CMD,442QIB_TWSI_START)) {443goto failed_write;444}445} else {446/* Real I2C */447if (qib_twsi_wr(dd, dev | WRITE_CMD, QIB_TWSI_START))448goto failed_write;449ret = qib_twsi_wr(dd, addr, 0);450if (ret) {451qib_dev_err(dd, "Failed to write interface"452" write addr %02X\n", addr);453goto failed_write;454}455}456457sub_len = min(len, 4);458addr += sub_len;459len -= sub_len;460461for (i = 0; i < sub_len; i++)462if (qib_twsi_wr(dd, *bp++, 0))463goto failed_write;464465stop_cmd(dd);466467/*468* Wait for write complete by waiting for a successful469* read (the chip replies with a zero after the write470* cmd completes, and before it writes to the eeprom.471* The startcmd for the read will fail the ack until472* the writes have completed. We do this inline to avoid473* the debug prints that are in the real read routine474* if the startcmd fails.475* We also use the proper device address, so it doesn't matter476* whether we have real eeprom_dev. Legacy likes any address.477*/478max_wait_time = 100;479while (qib_twsi_wr(dd, dev | READ_CMD, QIB_TWSI_START)) {480stop_cmd(dd);481if (!--max_wait_time)482goto failed_write;483}484/* now read (and ignore) the resulting byte */485rd_byte(dd, 1);486}487488ret = 0;489goto bail;490491failed_write:492stop_cmd(dd);493ret = 1;494495bail:496return ret;497}498499500