/* need a longer timeout if we're dealing with the fact we may well be * looking at a multi-master environment
*/ #define DEF_TIMEOUT 32
#define NO_SLAVE (-ENXIO) #define BUS_ERROR (-EREMOTEIO) #define XFER_NAKED (-ECONNREFUSED) #define I2C_RETRY (-2000) /* an error has occurred retry transmit */
/* ICR initialize bit values * * 15 FM 0 (100 kHz operation) * 14 UR 0 (No unit reset) * 13 SADIE 0 (Disables the unit from interrupting on slave addresses * matching its slave address) * 12 ALDIE 0 (Disables the unit from interrupt when it loses arbitration * in master mode) * 11 SSDIE 0 (Disables interrupts from a slave stop detected, in slave mode) * 10 BEIE 1 (Enable interrupts from detected bus errors, no ACK sent) * 9 IRFIE 1 (Enable interrupts from full buffer received) * 8 ITEIE 1 (Enables the I2C unit to interrupt when transmit buffer empty) * 7 GCD 1 (Disables i2c unit response to general call messages as a slave) * 6 IUE 0 (Disable unit until we change settings) * 5 SCLE 1 (Enables the i2c clock output for master mode (drives SCL) * 4 MA 0 (Only send stop with the ICR stop bit) * 3 TB 0 (We are not transmitting a byte initially) * 2 ACKNAK 0 (Send an ACK after the unit receives a byte) * 1 STOP 0 (Do not send a STOP) * 0 START 0 (Do not send a START)
*/ #define I2C_ICR_INIT (ICR_BEIE | ICR_IRFIE | ICR_ITEIE | ICR_GCD | ICR_SCLE)
#define show_state(i2c) do { } while (0) #define decode_ISR(val) do { } while (0) #define decode_ICR(val) do { } while (0) #define i2c_pxa_scream_blue_murder(i2c, why) do { } while (0)
#endif/* ifdef DEBUG / else */
staticvoid i2c_pxa_master_complete(struct pxa_i2c *i2c, int ret);
while (time_before(jiffies, timeout)) { if (i2c_debug > 1)
dev_dbg(&i2c->adap.dev, "%s: %ld: ISR=%08x, ICR=%08x, IBMR=%02x\n",
__func__, (long)jiffies, readl(_ISR(i2c)), readl(_ICR(i2c)), readl(_IBMR(i2c)));
if (readl(_ISR(i2c)) & ISR_SAD) { if (i2c_debug > 0)
dev_dbg(&i2c->adap.dev, "%s: Slave detected\n", __func__); goto out;
}
/* wait for unit and bus being not busy, and we also do a * quick check of the i2c lines themselves to ensure they've * gone high...
*/ if ((readl(_ISR(i2c)) & (ISR_UB | ISR_IBB)) == 0 &&
readl(_IBMR(i2c)) == (IBMR_SCLS | IBMR_SDAS)) { if (i2c_debug > 0)
dev_dbg(&i2c->adap.dev, "%s: done\n", __func__); return 1;
}
msleep(1);
}
if (i2c_debug > 0)
dev_dbg(&i2c->adap.dev, "%s: did not free\n", __func__);
out: return 0;
}
staticint i2c_pxa_set_master(struct pxa_i2c *i2c)
{ if (i2c_debug)
dev_dbg(&i2c->adap.dev, "setting to bus master\n");
if ((readl(_ISR(i2c)) & (ISR_UB | ISR_IBB)) != 0) {
dev_dbg(&i2c->adap.dev, "%s: unit is busy\n", __func__); if (!i2c_pxa_wait_master(i2c)) {
dev_dbg(&i2c->adap.dev, "%s: error: unit busy\n", __func__); return I2C_RETRY;
}
}
if (i2c_debug > 0)
dev_dbg(&i2c->adap.dev, "%s: did not free\n", __func__); return 0;
}
/* * clear the hold on the bus, and take of anything else * that has been configured
*/ staticvoid i2c_pxa_set_slave(struct pxa_i2c *i2c, int errcode)
{
show_state(i2c);
if (errcode < 0) {
udelay(100); /* simple delay */
} else { /* we need to wait for the stop condition to end */
/* if we where in stop, then clear... */ if (readl(_ICR(i2c)) & ICR_STOP) {
udelay(100);
writel(readl(_ICR(i2c)) & ~ICR_STOP, _ICR(i2c));
}
/* * slave could interrupt in the middle of us generating a * start condition... if this happens, we'd better back off * and stop holding the poor thing up
*/
writel(readl(_ICR(i2c)) & ~(ICR_START|ICR_STOP), _ICR(i2c));
writel(readl(_ICR(i2c)) | ICR_TB, _ICR(i2c));
timeout = 0x10000;
while (1) { if ((readl(_IBMR(i2c)) & IBMR_SCLS) == IBMR_SCLS) break;
timeout--;
if (timeout <= 0) {
dev_err(&i2c->adap.dev, "timeout waiting for SCL high\n"); break;
}
}
if (i2c->slave != NULL)
i2c_slave_event(i2c->slave, I2C_SLAVE_STOP, NULL);
if (i2c_debug > 2)
dev_dbg(&i2c->adap.dev, "ISR: SSD (Slave Stop) acked\n");
/* * If we have a master-mode message waiting, * kick it off now that the slave has completed.
*/ if (i2c->msg)
i2c_pxa_master_complete(i2c, I2C_RETRY);
}
staticvoid i2c_pxa_slave_start(struct pxa_i2c *i2c, u32 isr)
{ int timeout;
/* * slave could interrupt in the middle of us generating a * start condition... if this happens, we'd better back off * and stop holding the poor thing up
*/
writel(readl(_ICR(i2c)) & ~(ICR_START|ICR_STOP), _ICR(i2c));
writel(readl(_ICR(i2c)) | ICR_TB | ICR_ACKNAK, _ICR(i2c));
timeout = 0x10000;
while (1) { if ((readl(_IBMR(i2c)) & IBMR_SCLS) == IBMR_SCLS) break;
timeout--;
if (timeout <= 0) {
dev_err(&i2c->adap.dev, "timeout waiting for SCL high\n"); break;
}
}
again: /* * If ISR_ALD is set, we lost arbitration.
*/ if (isr & ISR_ALD) { /* * Do we need to do anything here? The PXA docs * are vague about what happens.
*/
i2c_pxa_scream_blue_murder(i2c, "ALD set");
/* * We ignore this error. We seem to see spurious ALDs * for seemingly no reason. If we handle them as I think * they should, we end up causing an I2C error, which * is painful for some systems.
*/ return; /* ignore */
}
if ((isr & ISR_BED) &&
(!((i2c->msg->flags & I2C_M_IGNORE_NAK) &&
(isr & ISR_ACKNAK)))) { int ret = BUS_ERROR;
/* * I2C bus error - either the device NAK'd us, or * something more serious happened. If we were NAK'd * on the initial address phase, we can retry.
*/ if (isr & ISR_ACKNAK) { if (i2c->msg_ptr == 0 && i2c->msg_idx == 0)
ret = NO_SLAVE; else
ret = XFER_NAKED;
}
i2c_pxa_master_complete(i2c, ret);
} elseif (isr & ISR_RWM) { /* * Read mode. We have just sent the address byte, and * now we must initiate the transfer.
*/ if (i2c->msg_ptr == i2c->msg->len - 1 &&
i2c->msg_idx == i2c->msg_num - 1)
icr |= ICR_STOP | ICR_ACKNAK;
icr |= ICR_ALDIE | ICR_TB;
} elseif (i2c->msg_ptr < i2c->msg->len) { /* * Write mode. Write the next data byte.
*/
writel(i2c->msg->buf[i2c->msg_ptr++], _IDBR(i2c));
icr |= ICR_ALDIE | ICR_TB;
/* * If this is the last byte of the last message or last byte * of any message with I2C_M_STOP (e.g. SCCB), send a STOP.
*/ if ((i2c->msg_ptr == i2c->msg->len) &&
((i2c->msg->flags & I2C_M_STOP) ||
(i2c->msg_idx == i2c->msg_num - 1)))
icr |= ICR_STOP;
} elseif (i2c->msg_idx < i2c->msg_num - 1) { /* * Next segment of the message.
*/
i2c->msg_ptr = 0;
i2c->msg_idx ++;
i2c->msg++;
/* * If we aren't doing a repeated start and address, * go back and try to send the next byte. Note that * we do not support switching the R/W direction here.
*/ if (i2c->msg->flags & I2C_M_NOSTART) goto again;
/* * Write the next address.
*/
i2c->req_slave_addr = i2c_8bit_addr_from_msg(i2c->msg);
writel(i2c->req_slave_addr, _IDBR(i2c));
/* * And trigger a repeated start, and send the byte.
*/
icr &= ~ICR_ALDIE;
icr |= ICR_START | ICR_TB;
} else { if (i2c->msg->len == 0)
icr |= ICR_MA;
i2c_pxa_master_complete(i2c, 0);
}
/* * Read the byte.
*/
i2c->msg->buf[i2c->msg_ptr++] = readl(_IDBR(i2c));
if (i2c->msg_ptr < i2c->msg->len) { /* * If this is the last byte of the last * message, send a STOP.
*/ if (i2c->msg_ptr == i2c->msg->len - 1)
icr |= ICR_STOP | ICR_ACKNAK;
/* * We are protected by the adapter bus mutex.
*/ staticint i2c_pxa_do_xfer(struct pxa_i2c *i2c, struct i2c_msg *msg, int num)
{ long time_left; int ret;
/* * Wait for the bus to become free.
*/
ret = i2c_pxa_wait_bus_not_busy(i2c); if (ret) {
dev_err(&i2c->adap.dev, "i2c_pxa: timeout waiting for bus free\n");
i2c_recover_bus(&i2c->adap); goto out;
}
/* * Set master mode.
*/
ret = i2c_pxa_set_master(i2c); if (ret) {
dev_err(&i2c->adap.dev, "i2c_pxa_set_master: error %d\n", ret); goto out;
}
if (i2c->high_mode) {
ret = i2c_pxa_send_mastercode(i2c); if (ret) {
dev_err(&i2c->adap.dev, "i2c_pxa_send_mastercode timeout\n"); goto out;
}
}
/* * The rest of the processing occurs in the interrupt handler.
*/
time_left = wait_event_timeout(i2c->wait, i2c->msg_num == 0, HZ * 5);
i2c_pxa_stop_message(i2c);
/* * We place the return code in i2c->msg_idx.
*/
ret = i2c->msg_idx;
if (!time_left && i2c->msg_num) {
i2c_pxa_scream_blue_murder(i2c, "timeout with active message");
i2c_recover_bus(&i2c->adap);
ret = I2C_RETRY;
}
out: return ret;
}
staticint i2c_pxa_internal_xfer(struct pxa_i2c *i2c, struct i2c_msg *msgs, int num, int (*xfer)(struct pxa_i2c *, struct i2c_msg *, int num))
{ int ret, i;
for (i = 0; ; ) {
ret = xfer(i2c, msgs, num); if (ret != I2C_RETRY && ret != NO_SLAVE) goto out; if (++i >= i2c->adap.retries) break;
if (i2c_debug)
dev_dbg(&i2c->adap.dev, "Retrying transmission\n");
udelay(100);
} if (ret != NO_SLAVE)
i2c_pxa_scream_blue_murder(i2c, "exhausted retries");
ret = -EREMOTEIO;
out:
i2c_pxa_set_slave(i2c, ret); return ret;
}
/* Non-interrupt mode support */ staticint i2c_pxa_pio_set_master(struct pxa_i2c *i2c)
{ /* make timeout the same as for interrupt based functions */ long timeout = 2 * DEF_TIMEOUT;
/* * Wait for the bus to become free.
*/ while (timeout-- && readl(_ISR(i2c)) & (ISR_IBB | ISR_UB))
udelay(1000);
if (timeout < 0) {
show_state(i2c);
dev_err(&i2c->adap.dev, "i2c_pxa: timeout waiting for bus free (set_master)\n"); return I2C_RETRY;
}
/* * Set master mode.
*/
writel(readl(_ICR(i2c)) | ICR_SCLE, _ICR(i2c));
return 0;
}
staticint i2c_pxa_do_pio_xfer(struct pxa_i2c *i2c, struct i2c_msg *msg, int num)
{ unsignedlong timeout = 500000; /* 5 seconds */ int ret = 0;
ret = i2c_pxa_pio_set_master(i2c); if (ret) goto out;
/* If the I2C controller is disabled we need to reset it (probably due to a suspend/resume destroying state). We do this here as we can then avoid worrying about resuming the
controller before its users. */ if (!(readl(_ICR(i2c)) & ICR_IUE))
i2c_pxa_reset(i2c);
/* * Program the GPIOs to reflect the current I2C bus state while * we transition to recovery; this avoids glitching the bus.
*/
gpiod_set_value(i2c->recovery.scl_gpiod, ibmr & IBMR_SCLS);
gpiod_set_value(i2c->recovery.sda_gpiod, ibmr & IBMR_SDAS);
/* * The bus should now be free. Clear up the I2C controller before * handing control of the bus back to avoid the bus changing state.
*/
isr = readl(_ISR(i2c)); if (isr & (ISR_UB | ISR_IBB)) {
dev_dbg(&i2c->adap.dev, "recovery: resetting controller, ISR=0x%08x\n", isr);
i2c_pxa_do_reset(i2c);
}
/* * When slave mode is enabled, we are not the only master on the bus. * Bus recovery can only be performed when we are the master, which * we can't be certain of. Therefore, when slave mode is enabled, do * not configure bus recovery.
*/ if (IS_ENABLED(CONFIG_I2C_PXA_SLAVE)) return 0;
i2c->pinctrl = devm_pinctrl_get(dev); if (PTR_ERR(i2c->pinctrl) == -ENODEV)
i2c->pinctrl = NULL; if (IS_ERR(i2c->pinctrl)) return PTR_ERR(i2c->pinctrl);
/* * Claiming GPIOs can influence the pinmux state, and may glitch the * I2C bus. Do this carefully.
*/
bri->scl_gpiod = devm_gpiod_get(dev, "scl", GPIOD_OUT_HIGH_OPEN_DRAIN); if (bri->scl_gpiod == ERR_PTR(-EPROBE_DEFER)) return -EPROBE_DEFER; if (IS_ERR(bri->scl_gpiod)) {
dev_info(dev, "missing scl gpio recovery information: %pe\n",
bri->scl_gpiod); return 0;
}
/* * We have SCL. Pull SCL low and wait a bit so that SDA glitches * have no effect.
*/
gpiod_direction_output(bri->scl_gpiod, 0);
udelay(10);
bri->sda_gpiod = devm_gpiod_get(dev, "sda", GPIOD_OUT_HIGH_OPEN_DRAIN);
/* Wait a bit in case of a SDA glitch, and then release SCL. */
udelay(10);
gpiod_direction_output(bri->scl_gpiod, 1);
if (bri->sda_gpiod == ERR_PTR(-EPROBE_DEFER)) return -EPROBE_DEFER;
/* * Claiming GPIOs can change the pinmux state, which confuses the * pinctrl since pinctrl's idea of the current setting is unaffected * by the pinmux change caused by claiming the GPIO. Work around that * by switching pinctrl to the GPIO state here. We do it this way to * avoid glitching the I2C bus.
*/
pinctrl_select_state(i2c->pinctrl, i2c->pinctrl_recovery);
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.