2002-11-03 00:24:07 +00:00
|
|
|
/*
|
|
|
|
* (C) Copyright 2000
|
|
|
|
* Paolo Scaffardi, AIRVENT SAM s.p.a - RIMINI(ITALY), arsenio@tin.it
|
|
|
|
*
|
|
|
|
* (C) Copyright 2000 Sysgo Real-Time Solutions, GmbH <www.elinos.com>
|
|
|
|
* Marius Groeger <mgroeger@sysgo.de>
|
|
|
|
*
|
2013-07-08 07:37:19 +00:00
|
|
|
* SPDX-License-Identifier: GPL-2.0+
|
2002-11-03 00:24:07 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <common.h>
|
|
|
|
|
|
|
|
#if defined(CONFIG_HARD_I2C)
|
|
|
|
|
|
|
|
#include <asm/cpm_8260.h>
|
|
|
|
#include <i2c.h>
|
|
|
|
|
2006-03-31 16:32:53 +00:00
|
|
|
DECLARE_GLOBAL_DATA_PTR;
|
|
|
|
|
2008-10-15 07:34:45 +00:00
|
|
|
#if defined(CONFIG_I2C_MULTI_BUS)
|
2011-11-04 15:55:56 +00:00
|
|
|
static unsigned int i2c_bus_num __attribute__ ((section(".data"))) = 0;
|
2008-10-15 07:34:45 +00:00
|
|
|
#endif /* CONFIG_I2C_MULTI_BUS */
|
|
|
|
|
2002-11-03 00:24:07 +00:00
|
|
|
/* uSec to wait between polls of the i2c */
|
|
|
|
#define DELAY_US 100
|
|
|
|
/* uSec to wait for the CPM to start processing the buffer */
|
|
|
|
#define START_DELAY_US 1000
|
|
|
|
|
|
|
|
/*
|
|
|
|
* tx/rx per-byte timeout: we delay DELAY_US uSec between polls so the
|
|
|
|
* timeout will be (tx_length + rx_length) * DELAY_US * TOUT_LOOP
|
|
|
|
*/
|
|
|
|
#define TOUT_LOOP 5
|
|
|
|
|
2011-11-04 15:55:56 +00:00
|
|
|
/*
|
2002-11-03 00:24:07 +00:00
|
|
|
* Set default values
|
|
|
|
*/
|
2008-10-16 13:01:15 +00:00
|
|
|
#ifndef CONFIG_SYS_I2C_SPEED
|
|
|
|
#define CONFIG_SYS_I2C_SPEED 50000
|
2002-11-03 00:24:07 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2011-11-04 15:55:56 +00:00
|
|
|
typedef void (*i2c_ecb_t) (int, int, void *); /* error callback function */
|
2002-11-03 00:24:07 +00:00
|
|
|
|
|
|
|
/* This structure keeps track of the bd and buffer space usage. */
|
|
|
|
typedef struct i2c_state {
|
2011-11-04 15:55:56 +00:00
|
|
|
int rx_idx; /* index to next free Rx BD */
|
|
|
|
int tx_idx; /* index to next free Tx BD */
|
|
|
|
void *rxbd; /* pointer to next free Rx BD */
|
|
|
|
void *txbd; /* pointer to next free Tx BD */
|
|
|
|
int tx_space; /* number of Tx bytes left */
|
|
|
|
unsigned char *tx_buf; /* pointer to free Tx area */
|
|
|
|
i2c_ecb_t err_cb; /* error callback function */
|
|
|
|
void *cb_data; /* private data to be passed */
|
2002-11-03 00:24:07 +00:00
|
|
|
} i2c_state_t;
|
|
|
|
|
|
|
|
/* flags for i2c_send() and i2c_receive() */
|
2011-11-04 15:55:56 +00:00
|
|
|
#define I2CF_ENABLE_SECONDARY 0x01 /* secondary_address is valid */
|
|
|
|
#define I2CF_START_COND 0x02 /* tx: generate start condition */
|
|
|
|
#define I2CF_STOP_COND 0x04 /* tx: generate stop condition */
|
2002-11-03 00:24:07 +00:00
|
|
|
|
|
|
|
/* return codes */
|
2011-11-04 15:55:56 +00:00
|
|
|
#define I2CERR_NO_BUFFERS 1 /* no more BDs or buffer space */
|
|
|
|
#define I2CERR_MSG_TOO_LONG 2 /* tried to send/receive to much data */
|
|
|
|
#define I2CERR_TIMEOUT 3 /* timeout in i2c_doio() */
|
|
|
|
#define I2CERR_QUEUE_EMPTY 4 /* i2c_doio called without send/rcv */
|
|
|
|
#define I2CERR_IO_ERROR 5 /* had an error during comms */
|
2002-11-03 00:24:07 +00:00
|
|
|
|
|
|
|
/* error callback flags */
|
2011-11-04 15:55:56 +00:00
|
|
|
#define I2CECB_RX_ERR 0x10 /* this is a receive error */
|
|
|
|
#define I2CECB_RX_OV 0x02 /* receive overrun error */
|
|
|
|
#define I2CECB_RX_MASK 0x0f /* mask for error bits */
|
|
|
|
#define I2CECB_TX_ERR 0x20 /* this is a transmit error */
|
|
|
|
#define I2CECB_TX_CL 0x01 /* transmit collision error */
|
|
|
|
#define I2CECB_TX_UN 0x02 /* transmit underflow error */
|
|
|
|
#define I2CECB_TX_NAK 0x04 /* transmit no ack error */
|
|
|
|
#define I2CECB_TX_MASK 0x0f /* mask for error bits */
|
|
|
|
#define I2CECB_TIMEOUT 0x40 /* this is a timeout error */
|
2002-11-03 00:24:07 +00:00
|
|
|
|
|
|
|
#define ERROR_I2C_NONE 0
|
|
|
|
#define ERROR_I2C_LENGTH 1
|
|
|
|
|
|
|
|
#define I2C_WRITE_BIT 0x00
|
|
|
|
#define I2C_READ_BIT 0x01
|
|
|
|
|
|
|
|
#define I2C_RXTX_LEN 128 /* maximum tx/rx buffer length */
|
|
|
|
|
|
|
|
|
|
|
|
#define NUM_RX_BDS 4
|
|
|
|
#define NUM_TX_BDS 4
|
|
|
|
#define MAX_TX_SPACE 256
|
|
|
|
|
2011-11-04 15:55:56 +00:00
|
|
|
typedef struct I2C_BD {
|
|
|
|
unsigned short status;
|
|
|
|
unsigned short length;
|
|
|
|
unsigned char *addr;
|
2002-11-03 00:24:07 +00:00
|
|
|
} I2C_BD;
|
2011-11-04 15:55:56 +00:00
|
|
|
|
|
|
|
#define BD_I2C_TX_START 0x0400 /* special status for i2c: Start condition */
|
2002-11-03 00:24:07 +00:00
|
|
|
|
|
|
|
#define BD_I2C_TX_CL 0x0001 /* collision error */
|
|
|
|
#define BD_I2C_TX_UN 0x0002 /* underflow error */
|
|
|
|
#define BD_I2C_TX_NAK 0x0004 /* no acknowledge error */
|
|
|
|
#define BD_I2C_TX_ERR (BD_I2C_TX_NAK|BD_I2C_TX_UN|BD_I2C_TX_CL)
|
|
|
|
|
|
|
|
#define BD_I2C_RX_ERR BD_SC_OV
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Returns the best value of I2BRG to meet desired clock speed of I2C with
|
|
|
|
* input parameters (clock speed, filter, and predivider value).
|
|
|
|
* It returns computer speed value and the difference between it and desired
|
|
|
|
* speed.
|
|
|
|
*/
|
|
|
|
static inline int
|
|
|
|
i2c_roundrate(int hz, int speed, int filter, int modval,
|
2011-11-04 15:55:56 +00:00
|
|
|
int *brgval, int *totspeed)
|
2002-11-03 00:24:07 +00:00
|
|
|
{
|
2011-11-04 15:55:56 +00:00
|
|
|
int moddiv = 1 << (5 - (modval & 3)), brgdiv, div;
|
2002-11-03 00:24:07 +00:00
|
|
|
|
2011-11-04 15:55:57 +00:00
|
|
|
debug("\t[I2C] trying hz=%d, speed=%d, filter=%d, modval=%d\n",
|
|
|
|
hz, speed, filter, modval);
|
2002-11-03 00:24:07 +00:00
|
|
|
|
2011-11-04 15:55:56 +00:00
|
|
|
div = moddiv * speed;
|
|
|
|
brgdiv = (hz + div - 1) / div;
|
2002-11-03 00:24:07 +00:00
|
|
|
|
2011-11-04 15:55:57 +00:00
|
|
|
debug("\t\tmoddiv=%d, brgdiv=%d\n", moddiv, brgdiv);
|
2002-11-03 00:24:07 +00:00
|
|
|
|
2011-11-04 15:55:56 +00:00
|
|
|
*brgval = ((brgdiv + 1) / 2) - 3 - (2 * filter);
|
2002-11-03 00:24:07 +00:00
|
|
|
|
2011-11-04 15:55:56 +00:00
|
|
|
if ((*brgval < 0) || (*brgval > 255)) {
|
2011-11-04 15:55:57 +00:00
|
|
|
debug("\t\trejected brgval=%d\n", *brgval);
|
2011-11-04 15:55:56 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2002-11-03 00:24:07 +00:00
|
|
|
|
2011-11-04 15:55:56 +00:00
|
|
|
brgdiv = 2 * (*brgval + 3 + (2 * filter));
|
|
|
|
div = moddiv * brgdiv;
|
|
|
|
*totspeed = hz / div;
|
2002-11-03 00:24:07 +00:00
|
|
|
|
2011-11-04 15:55:57 +00:00
|
|
|
debug("\t\taccepted brgval=%d, totspeed=%d\n", *brgval, *totspeed);
|
2002-11-03 00:24:07 +00:00
|
|
|
|
2011-11-04 15:55:56 +00:00
|
|
|
return 0;
|
2002-11-03 00:24:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Sets the I2C clock predivider and divider to meet required clock speed.
|
|
|
|
*/
|
|
|
|
static int i2c_setrate(int hz, int speed)
|
|
|
|
{
|
2011-11-04 15:55:56 +00:00
|
|
|
immap_t *immap = (immap_t *)CONFIG_SYS_IMMR;
|
|
|
|
volatile i2c8260_t *i2c = (i2c8260_t *)&immap->im_i2c;
|
|
|
|
int brgval,
|
|
|
|
modval, /* 0-3 */
|
|
|
|
bestspeed_diff = speed,
|
|
|
|
bestspeed_brgval = 0,
|
|
|
|
bestspeed_modval = 0,
|
|
|
|
bestspeed_filter = 0,
|
|
|
|
totspeed,
|
|
|
|
filter = 0; /* Use this fixed value */
|
|
|
|
|
|
|
|
for (modval = 0; modval < 4; modval++) {
|
|
|
|
if (i2c_roundrate(hz, speed, filter, modval, &brgval, &totspeed)
|
|
|
|
== 0) {
|
|
|
|
int diff = speed - totspeed;
|
|
|
|
|
|
|
|
if ((diff >= 0) && (diff < bestspeed_diff)) {
|
|
|
|
bestspeed_diff = diff;
|
|
|
|
bestspeed_modval = modval;
|
|
|
|
bestspeed_brgval = brgval;
|
|
|
|
bestspeed_filter = filter;
|
2002-11-03 00:24:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-04 15:55:57 +00:00
|
|
|
debug("[I2C] Best is:\n");
|
|
|
|
debug("[I2C] CPU=%dhz RATE=%d F=%d I2MOD=%08x I2BRG=%08x DIFF=%dhz\n",
|
2011-11-04 15:55:56 +00:00
|
|
|
hz, speed, bestspeed_filter, bestspeed_modval, bestspeed_brgval,
|
2011-11-04 15:55:57 +00:00
|
|
|
bestspeed_diff);
|
2002-11-03 00:24:07 +00:00
|
|
|
|
2011-11-04 15:55:56 +00:00
|
|
|
i2c->i2c_i2mod |= ((bestspeed_modval & 3) << 1) |
|
|
|
|
(bestspeed_filter << 3);
|
|
|
|
i2c->i2c_i2brg = bestspeed_brgval & 0xff;
|
2002-11-03 00:24:07 +00:00
|
|
|
|
2011-11-04 15:55:57 +00:00
|
|
|
debug("[I2C] i2mod=%08x i2brg=%08x\n", i2c->i2c_i2mod,
|
|
|
|
i2c->i2c_i2brg);
|
2002-11-03 00:24:07 +00:00
|
|
|
|
2011-11-04 15:55:56 +00:00
|
|
|
return 1;
|
2002-11-03 00:24:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void i2c_init(int speed, int slaveadd)
|
|
|
|
{
|
2011-11-04 15:55:56 +00:00
|
|
|
volatile immap_t *immap = (immap_t *)CONFIG_SYS_IMMR;
|
2002-11-03 00:24:07 +00:00
|
|
|
volatile cpm8260_t *cp = (cpm8260_t *)&immap->im_cpm;
|
2011-11-04 15:55:56 +00:00
|
|
|
volatile i2c8260_t *i2c = (i2c8260_t *)&immap->im_i2c;
|
2002-11-03 00:24:07 +00:00
|
|
|
volatile iic_t *iip;
|
|
|
|
ulong rbase, tbase;
|
|
|
|
volatile I2C_BD *rxbd, *txbd;
|
|
|
|
uint dpaddr;
|
|
|
|
|
2008-10-16 13:01:15 +00:00
|
|
|
#ifdef CONFIG_SYS_I2C_INIT_BOARD
|
2011-11-04 15:55:56 +00:00
|
|
|
/*
|
|
|
|
* call board specific i2c bus reset routine before accessing the
|
|
|
|
* environment, which might be in a chip on that bus. For details
|
|
|
|
* about this problem see doc/I2C_Edge_Conditions.
|
|
|
|
*/
|
2003-03-06 13:39:27 +00:00
|
|
|
i2c_init_board();
|
|
|
|
#endif
|
|
|
|
|
2013-05-18 01:01:54 +00:00
|
|
|
dpaddr = immap->im_dprambase16[PROFF_I2C_BASE / sizeof(u16)];
|
2002-11-03 00:24:07 +00:00
|
|
|
if (dpaddr == 0) {
|
2011-11-04 15:55:56 +00:00
|
|
|
/* need to allocate dual port ram */
|
|
|
|
dpaddr = m8260_cpm_dpalloc(64 +
|
|
|
|
(NUM_RX_BDS * sizeof(I2C_BD)) +
|
|
|
|
(NUM_TX_BDS * sizeof(I2C_BD)) +
|
|
|
|
MAX_TX_SPACE, 64);
|
2013-05-18 01:01:54 +00:00
|
|
|
immap->im_dprambase16[PROFF_I2C_BASE / sizeof(u16)] =
|
2011-11-04 15:55:56 +00:00
|
|
|
dpaddr;
|
2002-11-03 00:24:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* initialise data in dual port ram:
|
|
|
|
*
|
2011-11-04 15:55:56 +00:00
|
|
|
* dpaddr -> parameter ram (64 bytes)
|
2002-11-03 00:24:07 +00:00
|
|
|
* rbase -> rx BD (NUM_RX_BDS * sizeof(I2C_BD) bytes)
|
|
|
|
* tbase -> tx BD (NUM_TX_BDS * sizeof(I2C_BD) bytes)
|
|
|
|
* tx buffer (MAX_TX_SPACE bytes)
|
|
|
|
*/
|
|
|
|
|
|
|
|
iip = (iic_t *)&immap->im_dprambase[dpaddr];
|
2011-11-04 15:55:56 +00:00
|
|
|
memset((void *)iip, 0, sizeof(iic_t));
|
2002-11-03 00:24:07 +00:00
|
|
|
|
|
|
|
rbase = dpaddr + 64;
|
|
|
|
tbase = rbase + NUM_RX_BDS * sizeof(I2C_BD);
|
|
|
|
|
|
|
|
/* Disable interrupts */
|
|
|
|
i2c->i2c_i2mod = 0x00;
|
|
|
|
i2c->i2c_i2cmr = 0x00;
|
|
|
|
i2c->i2c_i2cer = 0xff;
|
|
|
|
i2c->i2c_i2add = slaveadd;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set the I2C BRG Clock division factor from desired i2c rate
|
|
|
|
* and current CPU rate (we assume sccr dfbgr field is 0;
|
|
|
|
* divide BRGCLK by 1)
|
|
|
|
*/
|
2011-11-04 15:55:57 +00:00
|
|
|
debug("[I2C] Setting rate...\n");
|
2012-12-13 20:48:44 +00:00
|
|
|
i2c_setrate(gd->arch.brg_clk, CONFIG_SYS_I2C_SPEED);
|
2002-11-03 00:24:07 +00:00
|
|
|
|
|
|
|
/* Set I2C controller in master mode */
|
|
|
|
i2c->i2c_i2com = 0x01;
|
|
|
|
|
|
|
|
/* Initialize Tx/Rx parameters */
|
|
|
|
iip->iic_rbase = rbase;
|
|
|
|
iip->iic_tbase = tbase;
|
2011-11-04 15:55:56 +00:00
|
|
|
rxbd = (I2C_BD *)((unsigned char *) &immap->
|
|
|
|
im_dprambase[iip->iic_rbase]);
|
|
|
|
txbd = (I2C_BD *)((unsigned char *) &immap->
|
|
|
|
im_dprambase[iip->iic_tbase]);
|
2002-11-03 00:24:07 +00:00
|
|
|
|
2011-11-04 15:55:57 +00:00
|
|
|
debug("[I2C] rbase = %04x\n", iip->iic_rbase);
|
|
|
|
debug("[I2C] tbase = %04x\n", iip->iic_tbase);
|
|
|
|
debug("[I2C] rxbd = %08x\n", (int) rxbd);
|
|
|
|
debug("[I2C] txbd = %08x\n", (int) txbd);
|
2002-11-03 00:24:07 +00:00
|
|
|
|
|
|
|
/* Set big endian byte order */
|
|
|
|
iip->iic_tfcr = 0x10;
|
|
|
|
iip->iic_rfcr = 0x10;
|
|
|
|
|
|
|
|
/* Set maximum receive size. */
|
|
|
|
iip->iic_mrblr = I2C_RXTX_LEN;
|
|
|
|
|
2011-11-04 15:55:56 +00:00
|
|
|
cp->cp_cpcr = mk_cr_cmd(CPM_CR_I2C_PAGE,
|
|
|
|
CPM_CR_I2C_SBLOCK,
|
|
|
|
0x00, CPM_CR_INIT_TRX) | CPM_CR_FLG;
|
|
|
|
do {
|
|
|
|
__asm__ __volatile__("eieio");
|
|
|
|
} while (cp->cp_cpcr & CPM_CR_FLG);
|
2002-11-03 00:24:07 +00:00
|
|
|
|
|
|
|
/* Clear events and interrupts */
|
|
|
|
i2c->i2c_i2cer = 0xff;
|
|
|
|
i2c->i2c_i2cmr = 0x00;
|
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
void i2c_newio(i2c_state_t *state)
|
|
|
|
{
|
2011-11-04 15:55:56 +00:00
|
|
|
volatile immap_t *immap = (immap_t *)CONFIG_SYS_IMMR;
|
2002-11-03 00:24:07 +00:00
|
|
|
volatile iic_t *iip;
|
|
|
|
uint dpaddr;
|
|
|
|
|
2011-11-04 15:55:57 +00:00
|
|
|
debug("[I2C] i2c_newio\n");
|
2002-11-03 00:24:07 +00:00
|
|
|
|
2013-05-18 01:01:54 +00:00
|
|
|
dpaddr = immap->im_dprambase16[PROFF_I2C_BASE / sizeof(u16)];
|
2002-11-03 00:24:07 +00:00
|
|
|
iip = (iic_t *)&immap->im_dprambase[dpaddr];
|
|
|
|
state->rx_idx = 0;
|
|
|
|
state->tx_idx = 0;
|
2011-11-04 15:55:56 +00:00
|
|
|
state->rxbd = (void *)&immap->im_dprambase[iip->iic_rbase];
|
|
|
|
state->txbd = (void *)&immap->im_dprambase[iip->iic_tbase];
|
2002-11-03 00:24:07 +00:00
|
|
|
state->tx_space = MAX_TX_SPACE;
|
2011-11-04 15:55:56 +00:00
|
|
|
state->tx_buf = (uchar *)state->txbd + NUM_TX_BDS * sizeof(I2C_BD);
|
2002-11-03 00:24:07 +00:00
|
|
|
state->err_cb = NULL;
|
2003-06-19 23:40:20 +00:00
|
|
|
state->cb_data = NULL;
|
2002-11-03 00:24:07 +00:00
|
|
|
|
2011-11-04 15:55:57 +00:00
|
|
|
debug("[I2C] rxbd = %08x\n", (int)state->rxbd);
|
|
|
|
debug("[I2C] txbd = %08x\n", (int)state->txbd);
|
|
|
|
debug("[I2C] tx_buf = %08x\n", (int)state->tx_buf);
|
2002-11-03 00:24:07 +00:00
|
|
|
|
|
|
|
/* clear the buffer memory */
|
2011-11-04 15:55:56 +00:00
|
|
|
memset((char *) state->tx_buf, 0, MAX_TX_SPACE);
|
2002-11-03 00:24:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
int i2c_send(i2c_state_t *state,
|
2011-11-04 15:55:56 +00:00
|
|
|
unsigned char address,
|
|
|
|
unsigned char secondary_address,
|
|
|
|
unsigned int flags, unsigned short size, unsigned char *dataout)
|
2002-11-03 00:24:07 +00:00
|
|
|
{
|
|
|
|
volatile I2C_BD *txbd;
|
2011-11-04 15:55:56 +00:00
|
|
|
int i, j;
|
2002-11-03 00:24:07 +00:00
|
|
|
|
2011-11-04 15:55:57 +00:00
|
|
|
debug("[I2C] i2c_send add=%02d sec=%02d flag=%02d size=%d\n",
|
|
|
|
address, secondary_address, flags, size);
|
2002-11-03 00:24:07 +00:00
|
|
|
|
|
|
|
/* trying to send message larger than BD */
|
|
|
|
if (size > I2C_RXTX_LEN)
|
2011-11-04 15:55:56 +00:00
|
|
|
return I2CERR_MSG_TOO_LONG;
|
2002-11-03 00:24:07 +00:00
|
|
|
|
|
|
|
/* no more free bds */
|
|
|
|
if (state->tx_idx >= NUM_TX_BDS || state->tx_space < (2 + size))
|
2011-11-04 15:55:56 +00:00
|
|
|
return I2CERR_NO_BUFFERS;
|
2002-11-03 00:24:07 +00:00
|
|
|
|
|
|
|
txbd = (I2C_BD *)state->txbd;
|
|
|
|
txbd->addr = state->tx_buf;
|
|
|
|
|
2011-11-04 15:55:57 +00:00
|
|
|
debug("[I2C] txbd = %08x\n", (int) txbd);
|
2011-11-04 15:55:56 +00:00
|
|
|
|
|
|
|
if (flags & I2CF_START_COND) {
|
2011-11-04 15:55:57 +00:00
|
|
|
debug("[I2C] Formatting addresses...\n");
|
2011-11-04 15:55:56 +00:00
|
|
|
if (flags & I2CF_ENABLE_SECONDARY) {
|
|
|
|
/* Length of message plus dest addresses */
|
|
|
|
txbd->length = size + 2;
|
|
|
|
txbd->addr[0] = address << 1;
|
|
|
|
txbd->addr[1] = secondary_address;
|
|
|
|
i = 2;
|
|
|
|
} else {
|
|
|
|
/* Length of message plus dest address */
|
|
|
|
txbd->length = size + 1;
|
|
|
|
/* Write destination address to BD */
|
|
|
|
txbd->addr[0] = address << 1;
|
|
|
|
i = 1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
txbd->length = size; /* Length of message */
|
|
|
|
i = 0;
|
2002-11-03 00:24:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* set up txbd */
|
|
|
|
txbd->status = BD_SC_READY;
|
|
|
|
if (flags & I2CF_START_COND)
|
2011-11-04 15:55:56 +00:00
|
|
|
txbd->status |= BD_I2C_TX_START;
|
2002-11-03 00:24:07 +00:00
|
|
|
if (flags & I2CF_STOP_COND)
|
2011-11-04 15:55:56 +00:00
|
|
|
txbd->status |= BD_SC_LAST | BD_SC_WRAP;
|
2002-11-03 00:24:07 +00:00
|
|
|
|
|
|
|
/* Copy data to send into buffer */
|
2011-11-04 15:55:57 +00:00
|
|
|
debug("[I2C] copy data...\n");
|
2011-11-04 15:55:56 +00:00
|
|
|
for (j = 0; j < size; i++, j++)
|
|
|
|
txbd->addr[i] = dataout[j];
|
2002-11-03 00:24:07 +00:00
|
|
|
|
2011-11-04 15:55:57 +00:00
|
|
|
debug("[I2C] txbd: length=0x%04x status=0x%04x addr[0]=0x%02x addr[1]=0x%02x\n",
|
|
|
|
txbd->length, txbd->status, txbd->addr[0], txbd->addr[1]);
|
2002-11-03 00:24:07 +00:00
|
|
|
|
|
|
|
/* advance state */
|
|
|
|
state->tx_buf += txbd->length;
|
|
|
|
state->tx_space -= txbd->length;
|
|
|
|
state->tx_idx++;
|
2011-11-04 15:55:56 +00:00
|
|
|
state->txbd = (void *) (txbd + 1);
|
2002-11-03 00:24:07 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
int i2c_receive(i2c_state_t *state,
|
2011-11-04 15:55:56 +00:00
|
|
|
unsigned char address,
|
|
|
|
unsigned char secondary_address,
|
|
|
|
unsigned int flags,
|
|
|
|
unsigned short size_to_expect, unsigned char *datain)
|
2002-11-03 00:24:07 +00:00
|
|
|
{
|
|
|
|
volatile I2C_BD *rxbd, *txbd;
|
|
|
|
|
2011-11-04 15:55:57 +00:00
|
|
|
debug("[I2C] i2c_receive %02d %02d %02d\n", address,
|
|
|
|
secondary_address, flags);
|
2002-11-03 00:24:07 +00:00
|
|
|
|
|
|
|
/* Expected to receive too much */
|
|
|
|
if (size_to_expect > I2C_RXTX_LEN)
|
2011-11-04 15:55:56 +00:00
|
|
|
return I2CERR_MSG_TOO_LONG;
|
2002-11-03 00:24:07 +00:00
|
|
|
|
|
|
|
/* no more free bds */
|
|
|
|
if (state->tx_idx >= NUM_TX_BDS || state->rx_idx >= NUM_RX_BDS
|
2011-11-04 15:55:56 +00:00
|
|
|
|| state->tx_space < 2)
|
|
|
|
return I2CERR_NO_BUFFERS;
|
2002-11-03 00:24:07 +00:00
|
|
|
|
2011-11-04 15:55:56 +00:00
|
|
|
rxbd = (I2C_BD *) state->rxbd;
|
|
|
|
txbd = (I2C_BD *) state->txbd;
|
2002-11-03 00:24:07 +00:00
|
|
|
|
2011-11-04 15:55:57 +00:00
|
|
|
debug("[I2C] rxbd = %08x\n", (int) rxbd);
|
|
|
|
debug("[I2C] txbd = %08x\n", (int) txbd);
|
2002-11-03 00:24:07 +00:00
|
|
|
|
|
|
|
txbd->addr = state->tx_buf;
|
|
|
|
|
|
|
|
/* set up TXBD for destination address */
|
2011-11-04 15:55:56 +00:00
|
|
|
if (flags & I2CF_ENABLE_SECONDARY) {
|
2002-11-03 00:24:07 +00:00
|
|
|
txbd->length = 2;
|
2011-11-04 15:55:56 +00:00
|
|
|
txbd->addr[0] = address << 1; /* Write data */
|
|
|
|
txbd->addr[1] = secondary_address; /* Internal address */
|
2002-11-03 00:24:07 +00:00
|
|
|
txbd->status = BD_SC_READY;
|
2011-11-04 15:55:56 +00:00
|
|
|
} else {
|
2002-11-03 00:24:07 +00:00
|
|
|
txbd->length = 1 + size_to_expect;
|
|
|
|
txbd->addr[0] = (address << 1) | 0x01;
|
|
|
|
txbd->status = BD_SC_READY;
|
|
|
|
memset(&txbd->addr[1], 0, txbd->length);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* set up rxbd for reception */
|
|
|
|
rxbd->status = BD_SC_EMPTY;
|
|
|
|
rxbd->length = size_to_expect;
|
|
|
|
rxbd->addr = datain;
|
|
|
|
|
|
|
|
txbd->status |= BD_I2C_TX_START;
|
2011-11-04 15:55:56 +00:00
|
|
|
if (flags & I2CF_STOP_COND) {
|
2002-11-03 00:24:07 +00:00
|
|
|
txbd->status |= BD_SC_LAST | BD_SC_WRAP;
|
|
|
|
rxbd->status |= BD_SC_WRAP;
|
|
|
|
}
|
|
|
|
|
2011-11-04 15:55:57 +00:00
|
|
|
debug("[I2C] txbd: length=0x%04x status=0x%04x addr[0]=0x%02x addr[1]=0x%02x\n",
|
|
|
|
txbd->length, txbd->status, txbd->addr[0], txbd->addr[1]);
|
|
|
|
debug("[I2C] rxbd: length=0x%04x status=0x%04x addr[0]=0x%02x addr[1]=0x%02x\n",
|
|
|
|
rxbd->length, rxbd->status, rxbd->addr[0], rxbd->addr[1]);
|
2002-11-03 00:24:07 +00:00
|
|
|
|
|
|
|
/* advance state */
|
|
|
|
state->tx_buf += txbd->length;
|
|
|
|
state->tx_space -= txbd->length;
|
|
|
|
state->tx_idx++;
|
2011-11-04 15:55:56 +00:00
|
|
|
state->txbd = (void *) (txbd + 1);
|
2002-11-03 00:24:07 +00:00
|
|
|
state->rx_idx++;
|
2011-11-04 15:55:56 +00:00
|
|
|
state->rxbd = (void *) (rxbd + 1);
|
2002-11-03 00:24:07 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static
|
|
|
|
int i2c_doio(i2c_state_t *state)
|
|
|
|
{
|
2011-11-04 15:55:56 +00:00
|
|
|
volatile immap_t *immap = (immap_t *)CONFIG_SYS_IMMR;
|
2002-11-03 00:24:07 +00:00
|
|
|
volatile iic_t *iip;
|
2011-11-04 15:55:56 +00:00
|
|
|
volatile i2c8260_t *i2c = (i2c8260_t *)&immap->im_i2c;
|
2002-11-03 00:24:07 +00:00
|
|
|
volatile I2C_BD *txbd, *rxbd;
|
2011-11-04 15:55:56 +00:00
|
|
|
int n, i, b, rxcnt = 0, rxtimeo = 0, txcnt = 0, txtimeo = 0, rc = 0;
|
2002-11-03 00:24:07 +00:00
|
|
|
uint dpaddr;
|
|
|
|
|
2011-11-04 15:55:57 +00:00
|
|
|
debug("[I2C] i2c_doio\n");
|
2002-11-03 00:24:07 +00:00
|
|
|
|
|
|
|
if (state->tx_idx <= 0 && state->rx_idx <= 0) {
|
2011-11-04 15:55:57 +00:00
|
|
|
debug("[I2C] No I/O is queued\n");
|
2002-11-03 00:24:07 +00:00
|
|
|
return I2CERR_QUEUE_EMPTY;
|
|
|
|
}
|
|
|
|
|
2013-05-18 01:01:54 +00:00
|
|
|
dpaddr = immap->im_dprambase16[PROFF_I2C_BASE / sizeof(u16)];
|
2002-11-03 00:24:07 +00:00
|
|
|
iip = (iic_t *)&immap->im_dprambase[dpaddr];
|
|
|
|
iip->iic_rbptr = iip->iic_rbase;
|
|
|
|
iip->iic_tbptr = iip->iic_tbase;
|
|
|
|
|
|
|
|
/* Enable I2C */
|
2011-11-04 15:55:57 +00:00
|
|
|
debug("[I2C] Enabling I2C...\n");
|
2002-11-03 00:24:07 +00:00
|
|
|
i2c->i2c_i2mod |= 0x01;
|
|
|
|
|
|
|
|
/* Begin transmission */
|
|
|
|
i2c->i2c_i2com |= 0x80;
|
|
|
|
|
|
|
|
/* Loop until transmit & receive completed */
|
|
|
|
|
2011-11-04 15:55:56 +00:00
|
|
|
n = state->tx_idx;
|
|
|
|
|
|
|
|
if (n > 0) {
|
2003-06-19 23:40:20 +00:00
|
|
|
|
2011-11-04 15:55:56 +00:00
|
|
|
txbd = ((I2C_BD *) state->txbd) - n;
|
2003-06-19 23:40:20 +00:00
|
|
|
for (i = 0; i < n; i++) {
|
|
|
|
txtimeo += TOUT_LOOP * txbd->length;
|
|
|
|
txbd++;
|
|
|
|
}
|
|
|
|
|
2011-11-04 15:55:56 +00:00
|
|
|
txbd--; /* wait until last in list is done */
|
2002-11-03 00:24:07 +00:00
|
|
|
|
2011-11-04 15:55:57 +00:00
|
|
|
debug("[I2C] Transmitting...(txbd=0x%08lx)\n",
|
|
|
|
(ulong) txbd);
|
2003-06-19 23:40:20 +00:00
|
|
|
|
2002-11-03 00:24:07 +00:00
|
|
|
udelay(START_DELAY_US); /* give it time to start */
|
2011-11-04 15:55:56 +00:00
|
|
|
while ((txbd->status & BD_SC_READY) && (++txcnt < txtimeo)) {
|
2002-11-03 00:24:07 +00:00
|
|
|
udelay(DELAY_US);
|
|
|
|
if (ctrlc())
|
2011-11-04 15:55:56 +00:00
|
|
|
return -1;
|
|
|
|
__asm__ __volatile__("eieio");
|
2002-11-03 00:24:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-04 15:55:56 +00:00
|
|
|
n = state->rx_idx;
|
2003-06-19 23:40:20 +00:00
|
|
|
|
2011-11-04 15:55:56 +00:00
|
|
|
if (txcnt < txtimeo && n > 0) {
|
|
|
|
|
|
|
|
rxbd = ((I2C_BD *) state->rxbd) - n;
|
2003-06-19 23:40:20 +00:00
|
|
|
for (i = 0; i < n; i++) {
|
2003-06-27 21:31:46 +00:00
|
|
|
rxtimeo += TOUT_LOOP * rxbd->length;
|
2003-06-19 23:40:20 +00:00
|
|
|
rxbd++;
|
|
|
|
}
|
|
|
|
|
2011-11-04 15:55:56 +00:00
|
|
|
rxbd--; /* wait until last in list is done */
|
2003-06-19 23:40:20 +00:00
|
|
|
|
2011-11-04 15:55:57 +00:00
|
|
|
debug("[I2C] Receiving...(rxbd=0x%08lx)\n", (ulong) rxbd);
|
2003-06-19 23:40:20 +00:00
|
|
|
|
2002-11-03 00:24:07 +00:00
|
|
|
udelay(START_DELAY_US); /* give it time to start */
|
2011-11-04 15:55:56 +00:00
|
|
|
while ((rxbd->status & BD_SC_EMPTY) && (++rxcnt < rxtimeo)) {
|
2002-11-03 00:24:07 +00:00
|
|
|
udelay(DELAY_US);
|
|
|
|
if (ctrlc())
|
2011-11-04 15:55:56 +00:00
|
|
|
return -1;
|
|
|
|
__asm__ __volatile__("eieio");
|
2002-11-03 00:24:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Turn off I2C */
|
|
|
|
i2c->i2c_i2mod &= ~0x01;
|
|
|
|
|
2011-11-04 15:55:56 +00:00
|
|
|
n = state->tx_idx;
|
|
|
|
|
|
|
|
if (n > 0) {
|
2003-06-19 23:40:20 +00:00
|
|
|
for (i = 0; i < n; i++) {
|
2011-11-04 15:55:56 +00:00
|
|
|
txbd = ((I2C_BD *) state->txbd) - (n - i);
|
|
|
|
b = txbd->status & BD_I2C_TX_ERR;
|
|
|
|
if (b != 0) {
|
2003-06-19 23:40:20 +00:00
|
|
|
if (state->err_cb != NULL)
|
2011-11-04 15:55:56 +00:00
|
|
|
(*state->err_cb) (I2CECB_TX_ERR | b,
|
|
|
|
i, state->cb_data);
|
2003-06-19 23:40:20 +00:00
|
|
|
if (rc == 0)
|
|
|
|
rc = I2CERR_IO_ERROR;
|
2002-11-03 00:24:07 +00:00
|
|
|
}
|
|
|
|
}
|
2003-06-19 23:40:20 +00:00
|
|
|
}
|
2002-11-03 00:24:07 +00:00
|
|
|
|
2011-11-04 15:55:56 +00:00
|
|
|
n = state->rx_idx;
|
|
|
|
|
|
|
|
if (n > 0) {
|
2003-06-19 23:40:20 +00:00
|
|
|
for (i = 0; i < n; i++) {
|
2011-11-04 15:55:56 +00:00
|
|
|
rxbd = ((I2C_BD *) state->rxbd) - (n - i);
|
|
|
|
b = rxbd->status & BD_I2C_RX_ERR;
|
|
|
|
if (b != 0) {
|
2003-06-19 23:40:20 +00:00
|
|
|
if (state->err_cb != NULL)
|
2011-11-04 15:55:56 +00:00
|
|
|
(*state->err_cb) (I2CECB_RX_ERR | b,
|
|
|
|
i, state->cb_data);
|
2003-06-19 23:40:20 +00:00
|
|
|
if (rc == 0)
|
|
|
|
rc = I2CERR_IO_ERROR;
|
2002-11-03 00:24:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-04 15:55:56 +00:00
|
|
|
if ((txtimeo > 0 && txcnt >= txtimeo) ||
|
2003-06-19 23:40:20 +00:00
|
|
|
(rxtimeo > 0 && rxcnt >= rxtimeo)) {
|
|
|
|
if (state->err_cb != NULL)
|
2011-11-04 15:55:56 +00:00
|
|
|
(*state->err_cb) (I2CECB_TIMEOUT, -1, state->cb_data);
|
2003-06-19 23:40:20 +00:00
|
|
|
if (rc == 0)
|
|
|
|
rc = I2CERR_TIMEOUT;
|
|
|
|
}
|
2002-11-03 00:24:07 +00:00
|
|
|
|
2011-11-04 15:55:56 +00:00
|
|
|
return rc;
|
2002-11-03 00:24:07 +00:00
|
|
|
}
|
|
|
|
|
2011-11-04 15:55:56 +00:00
|
|
|
static void i2c_probe_callback(int flags, int xnum, void *data)
|
2002-11-03 00:24:07 +00:00
|
|
|
{
|
2003-06-19 23:40:20 +00:00
|
|
|
/*
|
|
|
|
* the only acceptable errors are a transmit NAK or a receive
|
|
|
|
* overrun - tx NAK means the device does not exist, rx OV
|
|
|
|
* means the device must have responded to the slave address
|
|
|
|
* even though the transfer failed
|
|
|
|
*/
|
2011-11-04 15:55:56 +00:00
|
|
|
if (flags == (I2CECB_TX_ERR | I2CECB_TX_NAK))
|
|
|
|
*(int *) data |= 1;
|
|
|
|
if (flags == (I2CECB_RX_ERR | I2CECB_RX_OV))
|
|
|
|
*(int *) data |= 2;
|
2002-11-03 00:24:07 +00:00
|
|
|
}
|
|
|
|
|
2011-11-04 15:55:56 +00:00
|
|
|
int i2c_probe(uchar chip)
|
2002-11-03 00:24:07 +00:00
|
|
|
{
|
|
|
|
i2c_state_t state;
|
2003-06-19 23:40:20 +00:00
|
|
|
int rc, err_flag;
|
2002-11-03 00:24:07 +00:00
|
|
|
uchar buf[1];
|
|
|
|
|
|
|
|
i2c_newio(&state);
|
|
|
|
|
2003-06-19 23:40:20 +00:00
|
|
|
state.err_cb = i2c_probe_callback;
|
|
|
|
state.cb_data = (void *) &err_flag;
|
|
|
|
err_flag = 0;
|
2002-11-03 00:24:07 +00:00
|
|
|
|
2011-11-04 15:55:56 +00:00
|
|
|
rc = i2c_receive(&state, chip, 0, I2CF_START_COND | I2CF_STOP_COND, 1,
|
|
|
|
buf);
|
2002-11-03 00:24:07 +00:00
|
|
|
|
|
|
|
if (rc != 0)
|
2011-11-04 15:55:56 +00:00
|
|
|
return rc; /* probe failed */
|
2002-11-03 00:24:07 +00:00
|
|
|
|
|
|
|
rc = i2c_doio(&state);
|
|
|
|
|
2003-06-19 23:40:20 +00:00
|
|
|
if (rc == 0)
|
2011-11-04 15:55:56 +00:00
|
|
|
return 0; /* device exists - read succeeded */
|
2003-06-19 23:40:20 +00:00
|
|
|
|
|
|
|
if (rc == I2CERR_TIMEOUT)
|
2011-11-04 15:55:56 +00:00
|
|
|
return -1; /* device does not exist - timeout */
|
2003-06-19 23:40:20 +00:00
|
|
|
|
|
|
|
if (rc != I2CERR_IO_ERROR || err_flag == 0)
|
2011-11-04 15:55:56 +00:00
|
|
|
return rc; /* probe failed */
|
2003-06-19 23:40:20 +00:00
|
|
|
|
|
|
|
if (err_flag & 1)
|
2011-11-04 15:55:56 +00:00
|
|
|
return -1; /* device does not exist - had transmit NAK */
|
2002-11-03 00:24:07 +00:00
|
|
|
|
2011-11-04 15:55:56 +00:00
|
|
|
return 0; /* device exists - had receive overrun */
|
2002-11-03 00:24:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-11-04 15:55:56 +00:00
|
|
|
int i2c_read(uchar chip, uint addr, int alen, uchar *buffer, int len)
|
2002-11-03 00:24:07 +00:00
|
|
|
{
|
|
|
|
i2c_state_t state;
|
|
|
|
uchar xaddr[4];
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
xaddr[0] = (addr >> 24) & 0xFF;
|
|
|
|
xaddr[1] = (addr >> 16) & 0xFF;
|
2011-11-04 15:55:56 +00:00
|
|
|
xaddr[2] = (addr >> 8) & 0xFF;
|
|
|
|
xaddr[3] = addr & 0xFF;
|
2002-11-03 00:24:07 +00:00
|
|
|
|
2008-10-16 13:01:15 +00:00
|
|
|
#ifdef CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW
|
2011-11-04 15:55:56 +00:00
|
|
|
/*
|
|
|
|
* EEPROM chips that implement "address overflow" are ones
|
|
|
|
* like Catalyst 24WC04/08/16 which has 9/10/11 bits of address
|
|
|
|
* and the extra bits end up in the "chip address" bit slots.
|
|
|
|
* This makes a 24WC08 (1Kbyte) chip look like four 256 byte
|
|
|
|
* chips.
|
|
|
|
*
|
|
|
|
* Note that we consider the length of the address field to still
|
|
|
|
* be one byte because the extra address bits are hidden in the
|
|
|
|
* chip address.
|
|
|
|
*/
|
2008-10-16 13:01:15 +00:00
|
|
|
chip |= ((addr >> (alen * 8)) & CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW);
|
2002-11-03 00:24:07 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
i2c_newio(&state);
|
|
|
|
|
2011-11-04 15:55:56 +00:00
|
|
|
rc = i2c_send(&state, chip, 0, I2CF_START_COND, alen,
|
|
|
|
&xaddr[4 - alen]);
|
2002-11-03 00:24:07 +00:00
|
|
|
if (rc != 0) {
|
|
|
|
printf("i2c_read: i2c_send failed (%d)\n", rc);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = i2c_receive(&state, chip, 0, I2CF_STOP_COND, len, buffer);
|
|
|
|
if (rc != 0) {
|
|
|
|
printf("i2c_read: i2c_receive failed (%d)\n", rc);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = i2c_doio(&state);
|
|
|
|
if (rc != 0) {
|
|
|
|
printf("i2c_read: i2c_doio failed (%d)\n", rc);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-11-04 15:55:56 +00:00
|
|
|
int i2c_write(uchar chip, uint addr, int alen, uchar *buffer, int len)
|
2002-11-03 00:24:07 +00:00
|
|
|
{
|
|
|
|
i2c_state_t state;
|
|
|
|
uchar xaddr[4];
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
xaddr[0] = (addr >> 24) & 0xFF;
|
|
|
|
xaddr[1] = (addr >> 16) & 0xFF;
|
2011-11-04 15:55:56 +00:00
|
|
|
xaddr[2] = (addr >> 8) & 0xFF;
|
|
|
|
xaddr[3] = addr & 0xFF;
|
2002-11-03 00:24:07 +00:00
|
|
|
|
2008-10-16 13:01:15 +00:00
|
|
|
#ifdef CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW
|
2011-11-04 15:55:56 +00:00
|
|
|
/*
|
|
|
|
* EEPROM chips that implement "address overflow" are ones
|
|
|
|
* like Catalyst 24WC04/08/16 which has 9/10/11 bits of address
|
|
|
|
* and the extra bits end up in the "chip address" bit slots.
|
|
|
|
* This makes a 24WC08 (1Kbyte) chip look like four 256 byte
|
|
|
|
* chips.
|
|
|
|
*
|
|
|
|
* Note that we consider the length of the address field to still
|
|
|
|
* be one byte because the extra address bits are hidden in the
|
|
|
|
* chip address.
|
|
|
|
*/
|
2008-10-16 13:01:15 +00:00
|
|
|
chip |= ((addr >> (alen * 8)) & CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW);
|
2002-11-03 00:24:07 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
i2c_newio(&state);
|
|
|
|
|
2011-11-04 15:55:56 +00:00
|
|
|
rc = i2c_send(&state, chip, 0, I2CF_START_COND, alen,
|
|
|
|
&xaddr[4 - alen]);
|
2002-11-03 00:24:07 +00:00
|
|
|
if (rc != 0) {
|
|
|
|
printf("i2c_write: first i2c_send failed (%d)\n", rc);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = i2c_send(&state, 0, 0, I2CF_STOP_COND, len, buffer);
|
|
|
|
if (rc != 0) {
|
|
|
|
printf("i2c_write: second i2c_send failed (%d)\n", rc);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = i2c_doio(&state);
|
|
|
|
if (rc != 0) {
|
|
|
|
printf("i2c_write: i2c_doio failed (%d)\n", rc);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-10-15 07:34:45 +00:00
|
|
|
#if defined(CONFIG_I2C_MULTI_BUS)
|
|
|
|
/*
|
|
|
|
* Functions for multiple I2C bus handling
|
|
|
|
*/
|
|
|
|
unsigned int i2c_get_bus_num(void)
|
|
|
|
{
|
|
|
|
return i2c_bus_num;
|
|
|
|
}
|
|
|
|
|
|
|
|
int i2c_set_bus_num(unsigned int bus)
|
|
|
|
{
|
2008-10-16 13:01:15 +00:00
|
|
|
if (bus >= CONFIG_SYS_MAX_I2C_BUS)
|
2008-10-15 07:34:45 +00:00
|
|
|
return -1;
|
|
|
|
i2c_bus_num = bus;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-11-04 15:55:56 +00:00
|
|
|
#endif /* CONFIG_I2C_MULTI_BUS */
|
|
|
|
#endif /* CONFIG_HARD_I2C */
|