/* * Provides I2C support for Philips PNX010x/PNX4008 boards. * * Authors: Dennis Kovalev <dkovalev@ru.mvista.com> * Vitaly Wool <vwool@ru.mvista.com> * * 2004-2006 (c) MontaVista Software, Inc. This file is licensed under * the terms of the GNU General Public License version 2. This program * is licensed "as is" without any warranty of any kind, whether express * or implied.
*/
struct i2c_pnx_mif { int ret; /* Return value */ int mode; /* Interface mode */ struct completion complete; /* I/O completion */
u8 * buf; /* Data buffer */ int len; /* Length of data buffer */ int order; /* RX Bytes to order via TX */
};
/** * i2c_pnx_start - start a device * @slave_addr: slave address * @alg_data: pointer to local driver data structure * * Generate a START signal in the desired mode.
*/ staticint i2c_pnx_start(unsignedchar slave_addr, struct i2c_pnx_algo_data *alg_data)
{
dev_dbg(&alg_data->adapter.dev, "%s(): addr 0x%x mode %d\n", __func__,
slave_addr, alg_data->mif.mode);
/* Check for 7 bit slave addresses only */ if (slave_addr & ~0x7f) {
dev_err(&alg_data->adapter.dev, "%s: Invalid slave address %x. Only 7-bit addresses are supported\n",
alg_data->adapter.name, slave_addr); return -EINVAL;
}
/* First, make sure bus is idle */ if (wait_timeout(alg_data)) { /* Somebody else is monopolizing the bus */
dev_err(&alg_data->adapter.dev, "%s: Bus busy. Slave addr = %02x, cntrl = %x, stat = %x\n",
alg_data->adapter.name, slave_addr,
ioread32(I2C_REG_CTL(alg_data)),
ioread32(I2C_REG_STS(alg_data))); return -EBUSY;
} elseif (ioread32(I2C_REG_STS(alg_data)) & mstatus_afi) { /* Sorry, we lost the bus */
dev_err(&alg_data->adapter.dev, "%s: Arbitration failure. Slave addr = %02x\n",
alg_data->adapter.name, slave_addr); return -EIO;
}
/* * OK, I2C is enabled and we have the bus. * Clear the current TDI and AFI status flags.
*/
iowrite32(ioread32(I2C_REG_STS(alg_data)) | mstatus_tdi | mstatus_afi,
I2C_REG_STS(alg_data));
/** * i2c_pnx_stop - stop a device * @alg_data: pointer to local driver data structure * * Generate a STOP signal to terminate the master transaction.
*/ staticvoid i2c_pnx_stop(struct i2c_pnx_algo_data *alg_data)
{ /* Only 1 msec max timeout due to interrupt context */ long timeout = 1000;
dev_dbg(&alg_data->adapter.dev, "%s(): entering: stat = %04x.\n",
__func__, ioread32(I2C_REG_STS(alg_data)));
/* Write a STOP bit to TX FIFO */
iowrite32(0xff | stop_bit, I2C_REG_TX(alg_data));
/* Wait until the STOP is seen. */ while (timeout > 0 &&
(ioread32(I2C_REG_STS(alg_data)) & mstatus_active)) { /* may be called from interrupt context */
udelay(1);
timeout--;
}
dev_dbg(&alg_data->adapter.dev, "%s(): exiting: stat = %04x.\n",
__func__, ioread32(I2C_REG_STS(alg_data)));
}
/** * i2c_pnx_master_xmit - transmit data to slave * @alg_data: pointer to local driver data structure * * Sends one byte of data to the slave
*/ staticint i2c_pnx_master_xmit(struct i2c_pnx_algo_data *alg_data)
{
u32 val;
dev_dbg(&alg_data->adapter.dev, "%s(): entering: stat = %04x.\n",
__func__, ioread32(I2C_REG_STS(alg_data)));
if (alg_data->mif.len > 0) { /* We still have something to talk about... */
val = *alg_data->mif.buf++;
if (alg_data->mif.len == 0) { if (alg_data->last) { /* Wait until the STOP is seen. */ if (wait_timeout(alg_data))
dev_err(&alg_data->adapter.dev, "The bus is still active after timeout\n");
} /* Disable master interrupts */
iowrite32(ioread32(I2C_REG_CTL(alg_data)) &
~(mcntrl_afie | mcntrl_naie | mcntrl_drmie),
I2C_REG_CTL(alg_data));
dev_dbg(&alg_data->adapter.dev, "%s(): Waking up xfer routine.\n",
__func__);
dev_dbg(&alg_data->adapter.dev, "%s(): Waking up xfer routine after zero-xfer.\n",
__func__);
complete(&alg_data->mif.complete);
}
dev_dbg(&alg_data->adapter.dev, "%s(): exiting: stat = %04x.\n",
__func__, ioread32(I2C_REG_STS(alg_data)));
return 0;
}
/** * i2c_pnx_master_rcv - receive data from slave * @alg_data: pointer to local driver data structure * * Reads one byte data from the slave
*/ staticint i2c_pnx_master_rcv(struct i2c_pnx_algo_data *alg_data)
{ unsignedint val = 0;
u32 ctl = 0;
dev_dbg(&alg_data->adapter.dev, "%s(): entering: stat = %04x.\n",
__func__, ioread32(I2C_REG_STS(alg_data)));
/* Check, whether there is already data, * or we didn't 'ask' for it yet.
*/ if (ioread32(I2C_REG_STS(alg_data)) & mstatus_rfe) { /* 'Asking' is done asynchronously, e.g. dummy TX of several * bytes is done before the first actual RX arrives in FIFO. * Therefore, ordered bytes (via TX) are counted separately.
*/ if (alg_data->mif.order) {
dev_dbg(&alg_data->adapter.dev, "%s(): Write dummy data to fill Rx-fifo...\n",
__func__);
if (alg_data->mif.order == 1) { /* Last byte, do not acknowledge next rcv. */
val |= stop_bit;
/* * Enable interrupt RFDAIE (data in Rx fifo), * and disable DRMIE (need data for Tx)
*/
ctl = ioread32(I2C_REG_CTL(alg_data));
ctl |= mcntrl_rffie | mcntrl_daie;
ctl &= ~mcntrl_drmie;
iowrite32(ctl, I2C_REG_CTL(alg_data));
}
/* * Now we'll 'ask' for data: * For each byte we want to receive, we must * write a (dummy) byte to the Tx-FIFO.
*/
iowrite32(val, I2C_REG_TX(alg_data));
alg_data->mif.order--;
} return 0;
}
alg_data->mif.len--; if (alg_data->mif.len == 0) { if (alg_data->last) /* Wait until the STOP is seen. */ if (wait_timeout(alg_data))
dev_err(&alg_data->adapter.dev, "The bus is still active after timeout\n");
complete(&alg_data->mif.complete);
} elseif (stat & mstatus_nai) { /* Slave did not acknowledge, generate a STOP */
dev_dbg(&alg_data->adapter.dev, "%s(): Slave did not acknowledge, generating a STOP.\n",
__func__);
i2c_pnx_stop(alg_data);
complete(&alg_data->mif.complete);
} else { /* * Two options: * - Master Tx needs data. * - There is data in the Rx-fifo * The latter is only the case if we have requested for data, * via a dummy write. (See 'i2c_pnx_master_rcv'.) * We therefore check, as a sanity check, whether that interrupt * has been enabled.
*/ if ((stat & mstatus_drmi) || !(stat & mstatus_rfe)) { if (alg_data->mif.mode == I2C_SMBUS_WRITE) {
i2c_pnx_master_xmit(alg_data);
} elseif (alg_data->mif.mode == I2C_SMBUS_READ) {
i2c_pnx_master_rcv(alg_data);
}
}
}
/* Clear TDI and AFI bits */
stat = ioread32(I2C_REG_STS(alg_data));
iowrite32(stat | mstatus_tdi | mstatus_afi, I2C_REG_STS(alg_data));
if ((stat = ioread32(I2C_REG_STS(alg_data))) & mstatus_active) {
dev_err(&alg_data->adapter.dev, "%s: Bus is still active after xfer. Reset it...\n",
alg_data->adapter.name);
iowrite32(ioread32(I2C_REG_CTL(alg_data)) | mcntrl_reset,
I2C_REG_CTL(alg_data));
wait_reset(alg_data);
} elseif (!(stat & mstatus_rfe) || !(stat & mstatus_tfe)) { /* If there is data in the fifo's after transfer, * flush fifo's by reset.
*/
iowrite32(ioread32(I2C_REG_CTL(alg_data)) | mcntrl_reset,
I2C_REG_CTL(alg_data));
wait_reset(alg_data);
} elseif (stat & mstatus_nai) {
iowrite32(ioread32(I2C_REG_CTL(alg_data)) | mcntrl_reset,
I2C_REG_CTL(alg_data));
wait_reset(alg_data);
}
}
/** * i2c_pnx_xfer - generic transfer entry point * @adap: pointer to I2C adapter structure * @msgs: array of messages * @num: number of messages * * Initiates the transfer
*/ staticint
i2c_pnx_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
{ struct i2c_msg *pmsg; int rc = 0, completed = 0, i; struct i2c_pnx_algo_data *alg_data = adap->algo_data; unsignedlong time_left;
u32 stat;
alg_data->timeout = msecs_to_jiffies(I2C_PNX_TIMEOUT_DEFAULT); if (alg_data->timeout <= 1)
alg_data->timeout = 2;
#ifdef CONFIG_OF
alg_data->adapter.dev.of_node = of_node_get(pdev->dev.of_node); if (pdev->dev.of_node) {
of_property_read_u32(pdev->dev.of_node, "clock-frequency",
&speed); /* * At this point, it is planned to add an OF timeout property. * As soon as there is a consensus about how to call and handle * this, sth. like the following can be put here: * * of_property_read_u32(pdev->dev.of_node, "timeout", * &alg_data->timeout);
*/
} #endif
alg_data->clk = devm_clk_get(&pdev->dev, NULL); if (IS_ERR(alg_data->clk)) return PTR_ERR(alg_data->clk);
ret = clk_prepare_enable(alg_data->clk); if (ret) return ret;
freq = clk_get_rate(alg_data->clk);
/* * Clock Divisor High This value is the number of system clocks * the serial clock (SCL) will be high. * For example, if the system clock period is 50 ns and the maximum * desired serial period is 10000 ns (100 kHz), then CLKHI would be * set to 0.5*(f_sys/f_i2c)-2=0.5*(20e6/100e3)-2=98. The actual value * programmed into CLKHI will vary from this slightly due to * variations in the output pad's rise and fall times as well as * the deglitching filter length.
*/
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.