staticint mxs_i2c_reset(struct mxs_i2c_dev *i2c)
{ int ret = stmp_reset_block(i2c->regs); if (ret) return ret;
/* * Configure timing for the I2C block. The I2C TIMING2 register has to * be programmed with this particular magic number. The rest is derived * from the XTAL speed and requested I2C speed. * * For details, see i.MX233 [25.4.2 - 25.4.4] and i.MX28 [27.5.2 - 27.5.4].
*/
writel(i2c->timing0, i2c->regs + MXS_I2C_TIMING0);
writel(i2c->timing1, i2c->regs + MXS_I2C_TIMING1);
writel(i2c->timing2, i2c->regs + MXS_I2C_TIMING2);
if (msg->flags & I2C_M_RD) {
i2c->dma_read = true;
/* * SELECT command.
*/
/* Queue the PIO register write transfer. */
i2c->pio_data[0] = MXS_CMD_I2C_SELECT;
desc = dmaengine_prep_slave_sg(i2c->dmach,
(struct scatterlist *)&i2c->pio_data[0],
1, DMA_TRANS_NONE, 0); if (!desc) {
dev_err(i2c->dev, "Failed to get PIO reg. write descriptor.\n"); goto select_init_pio_fail;
}
/* Queue the DMA data transfer. */
sg_init_one(&i2c->sg_io[0], &i2c->addr_data, 1);
dma_map_sg(i2c->dev, &i2c->sg_io[0], 1, DMA_TO_DEVICE);
desc = dmaengine_prep_slave_sg(i2c->dmach, &i2c->sg_io[0], 1,
DMA_MEM_TO_DEV,
DMA_PREP_INTERRUPT |
MXS_DMA_CTRL_WAIT4END); if (!desc) {
dev_err(i2c->dev, "Failed to get DMA data write descriptor.\n"); goto select_init_dma_fail;
}
/* * READ command.
*/
/* Queue the PIO register write transfer. */
i2c->pio_data[1] = flags | MXS_CMD_I2C_READ |
MXS_I2C_CTRL0_XFER_COUNT(msg->len);
desc = dmaengine_prep_slave_sg(i2c->dmach,
(struct scatterlist *)&i2c->pio_data[1],
1, DMA_TRANS_NONE, DMA_PREP_INTERRUPT); if (!desc) {
dev_err(i2c->dev, "Failed to get PIO reg. write descriptor.\n"); goto select_init_dma_fail;
}
/* Queue the DMA data transfer. */
sg_init_one(&i2c->sg_io[1], buf, msg->len);
dma_map_sg(i2c->dev, &i2c->sg_io[1], 1, DMA_FROM_DEVICE);
desc = dmaengine_prep_slave_sg(i2c->dmach, &i2c->sg_io[1], 1,
DMA_DEV_TO_MEM,
DMA_PREP_INTERRUPT |
MXS_DMA_CTRL_WAIT4END); if (!desc) {
dev_err(i2c->dev, "Failed to get DMA data write descriptor.\n"); goto read_init_dma_fail;
}
} else {
i2c->dma_read = false;
/* * WRITE command.
*/
/* Queue the PIO register write transfer. */
i2c->pio_data[0] = flags | MXS_CMD_I2C_WRITE |
MXS_I2C_CTRL0_XFER_COUNT(msg->len + 1);
desc = dmaengine_prep_slave_sg(i2c->dmach,
(struct scatterlist *)&i2c->pio_data[0],
1, DMA_TRANS_NONE, 0); if (!desc) {
dev_err(i2c->dev, "Failed to get PIO reg. write descriptor.\n"); goto write_init_pio_fail;
}
/* Queue the DMA data transfer. */
sg_init_table(i2c->sg_io, 2);
sg_set_buf(&i2c->sg_io[0], &i2c->addr_data, 1);
sg_set_buf(&i2c->sg_io[1], buf, msg->len);
dma_map_sg(i2c->dev, i2c->sg_io, 2, DMA_TO_DEVICE);
desc = dmaengine_prep_slave_sg(i2c->dmach, i2c->sg_io, 2,
DMA_MEM_TO_DEV,
DMA_PREP_INTERRUPT |
MXS_DMA_CTRL_WAIT4END); if (!desc) {
dev_err(i2c->dev, "Failed to get DMA data write descriptor.\n"); goto write_init_dma_fail;
}
}
/* * The last descriptor must have this callback, * to finish the DMA transaction.
*/
desc->callback = mxs_i2c_dma_irq_callback;
desc->callback_param = i2c;
/* Start the transfer. */
dmaengine_submit(desc);
dma_async_issue_pending(i2c->dmach); return 0;
/* readback makes sure the write is latched into hardware */
reg = readl(i2c->regs + MXS_I2C_CTRL0);
reg |= MXS_I2C_CTRL0_RUN;
writel(reg, i2c->regs + MXS_I2C_CTRL0);
}
/* * Start WRITE transaction on the I2C bus. By studying i.MX23 datasheet, * CTRL0::PIO_MODE bit description clarifies the order in which the registers * must be written during PIO mode operation. First, the CTRL0 register has * to be programmed with all the necessary bits but the RUN bit. Then the * payload has to be written into the DATA register. Finally, the transmission * is executed by setting the RUN bit in CTRL0.
*/ staticvoid mxs_i2c_pio_trigger_write_cmd(struct mxs_i2c_dev *i2c, u32 cmd,
u32 data)
{
writel(cmd, i2c->regs + MXS_I2C_CTRL0);
if (i2c->dev_type == MXS_I2C_V1)
writel(MXS_I2C_CTRL0_PIO_MODE, i2c->regs + MXS_I2C_CTRL0_SET);
/* Mute IRQs coming from this block. */
writel(MXS_I2C_IRQ_MASK << 8, i2c->regs + MXS_I2C_CTRL1_CLR);
/* * MX23 idea: * - Enable CTRL0::PIO_MODE (1 << 24) * - Enable CTRL1::ACK_MODE (1 << 27) * * WARNING! The MX23 is broken in some way, even if it claims * to support PIO, when we try to transfer any amount of data * that is not aligned to 4 bytes, the DMA engine will have * bits in DEBUG1::DMA_BYTES_ENABLES still set even after the * transfer. This in turn will mess up the next transfer as * the block it emit one byte write onto the bus terminated * with a NAK+STOP. A possible workaround is to reset the IP * block after every PIO transmission, which might just work. * * NOTE: The CTRL0::PIO_MODE description is important, since * it outlines how the PIO mode is really supposed to work.
*/ if (msg->flags & I2C_M_RD) { /* * PIO READ transfer: * * This transfer MUST be limited to 4 bytes maximum. It is not * possible to transfer more than four bytes via PIO, since we * can not in any way make sure we can read the data from the * DATA register fast enough. Besides, the RX FIFO is only four * bytes deep, thus we can only really read up to four bytes at * time. Finally, there is no bit indicating us that new data * arrived at the FIFO and can thus be fetched from the DATA * register.
*/
BUG_ON(msg->len > 4);
ret = mxs_i2c_pio_wait_xfer_end(i2c); if (ret) {
dev_dbg(i2c->dev, "PIO: Failed to send READ command!\n"); goto cleanup;
}
data = readl(i2c->regs + MXS_I2C_DATA(i2c)); for (i = 0; i < msg->len; i++) {
msg->buf[i] = data & 0xff;
data >>= 8;
}
} else { /* * PIO WRITE transfer: * * The code below implements clock stretching to circumvent * the possibility of kernel not being able to supply data * fast enough. It is possible to transfer arbitrary amount * of data using PIO write.
*/
/* * The LSB of data buffer is the first byte blasted across * the bus. Higher order bytes follow. Thus the following * filling schematic.
*/
data = addr_data << 24;
/* Start the transfer with START condition. */
start = MXS_I2C_CTRL0_PRE_SEND_START;
/* If the transfer is long, use clock stretching. */ if (msg->len > 3)
start |= MXS_I2C_CTRL0_RETAIN_CLOCK;
for (i = 0; i < msg->len; i++) {
data >>= 8;
data |= (msg->buf[i] << 24);
xmit = 0;
/* This is the last transfer of the message. */ if (i + 1 == msg->len) { /* Add optional STOP flag. */
start |= flags; /* Remove RETAIN_CLOCK bit. */
start &= ~MXS_I2C_CTRL0_RETAIN_CLOCK;
xmit = 1;
}
/* Four bytes are ready in the "data" variable. */ if ((i & 3) == 2)
xmit = 1;
/* Nothing interesting happened, continue stuffing. */ if (!xmit) continue;
/* * Compute the size of the transfer and shift the * data accordingly. * * i = (4k + 0) .... xlen = 2 * i = (4k + 1) .... xlen = 3 * i = (4k + 2) .... xlen = 4 * i = (4k + 3) .... xlen = 1
*/
if ((i % 4) == 3)
xlen = 1; else
xlen = (i % 4) + 2;
/* The START condition is sent only once. */
start &= ~MXS_I2C_CTRL0_PRE_SEND_START;
/* Wait for the end of the transfer. */
ret = mxs_i2c_pio_wait_xfer_end(i2c); if (ret) {
dev_dbg(i2c->dev, "PIO: Failed to finish WRITE cmd!\n"); break;
}
/* Check NAK here. */
ret = readl(i2c->regs + MXS_I2C_STAT) &
MXS_I2C_STAT_GOT_A_NAK; if (ret) {
ret = -ENXIO; goto cleanup;
}
}
}
/* make sure we capture any occurred error into cmd_err */
ret = mxs_i2c_pio_check_error_state(i2c);
cleanup: /* Clear any dangling IRQs and re-enable interrupts. */
writel(MXS_I2C_IRQ_MASK, i2c->regs + MXS_I2C_CTRL1_CLR);
writel(MXS_I2C_IRQ_MASK << 8, i2c->regs + MXS_I2C_CTRL1_SET);
/* Clear the PIO_MODE on i.MX23 */ if (i2c->dev_type == MXS_I2C_V1)
writel(MXS_I2C_CTRL0_PIO_MODE, i2c->regs + MXS_I2C_CTRL0_CLR);
return ret;
}
/* * Low level master read/write transaction.
*/ staticint mxs_i2c_xfer_msg(struct i2c_adapter *adap, struct i2c_msg *msg, int stop)
{ struct mxs_i2c_dev *i2c = i2c_get_adapdata(adap); int ret; int flags;
u8 *dma_buf; int use_pio = 0; unsignedlong time_left;
/* * The MX28 I2C IP block can only do PIO READ for transfer of to up * 4 bytes of length. The write transfer is not limited as it can use * clock stretching to avoid FIFO underruns.
*/ if ((msg->flags & I2C_M_RD) && (msg->len <= 4))
use_pio = 1; if (!(msg->flags & I2C_M_RD) && (msg->len < 7))
use_pio = 1;
i2c->cmd_err = 0; if (use_pio) {
ret = mxs_i2c_pio_setup_xfer(adap, msg, flags); /* No need to reset the block if NAK was received. */ if (ret && (ret != -ENXIO))
mxs_i2c_reset(i2c);
} else {
dma_buf = i2c_get_dma_safe_msg_buf(msg, 1); if (!dma_buf) return -ENOMEM;
reinit_completion(&i2c->cmd_complete);
ret = mxs_i2c_dma_setup_xfer(adap, msg, dma_buf, flags); if (ret) {
i2c_put_dma_safe_msg_buf(dma_buf, msg, false); return ret;
}
if (ret == -ENXIO) { /* * If the transfer fails with a NAK from the slave the * controller halts until it gets told to return to idle state.
*/
writel(MXS_I2C_CTRL1_CLR_GOT_A_NAK,
i2c->regs + MXS_I2C_CTRL1_SET);
}
/* * WARNING! * The i.MX23 is strange. After each and every operation, it's I2C IP * block must be reset, otherwise the IP block will misbehave. This can * be observed on the bus by the block sending out one single byte onto * the bus. In case such an error happens, bit 27 will be set in the * DEBUG0 register. This bit is not documented in the i.MX23 datasheet * and is marked as "TBD" instead. To reset this bit to a correct state, * reset the whole block. Since the block reset does not take long, do * reset the block after every transfer to play safe.
*/ if (i2c->dev_type == MXS_I2C_V1)
mxs_i2c_reset(i2c);
dev_dbg(i2c->dev, "Done with err=%d\n", ret);
return ret;
timeout:
dev_dbg(i2c->dev, "Timeout!\n");
mxs_i2c_dma_finish(i2c);
ret = mxs_i2c_reset(i2c); if (ret) return ret;
return -ETIMEDOUT;
}
staticint mxs_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], int num)
{ int i; int err;
for (i = 0; i < num; i++) {
err = mxs_i2c_xfer_msg(adap, &msgs[i], i == (num - 1)); if (err) return err;
}
ret = of_property_read_u32(node, "clock-frequency", &speed); if (ret) {
dev_warn(dev, "No I2C speed selected, using 100kHz\n");
speed = I2C_MAX_STANDARD_MODE_FREQ;
}
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.