3939#define MXS_I2C_CTRL0_SET (0x04)
4040
4141#define MXS_I2C_CTRL0_SFTRST 0x80000000
42+ #define MXS_I2C_CTRL0_RUN 0x20000000
4243#define MXS_I2C_CTRL0_SEND_NAK_ON_LAST 0x02000000
4344#define MXS_I2C_CTRL0_RETAIN_CLOCK 0x00200000
4445#define MXS_I2C_CTRL0_POST_SEND_STOP 0x00100000
6465#define MXS_I2C_CTRL1_SLAVE_STOP_IRQ 0x02
6566#define MXS_I2C_CTRL1_SLAVE_IRQ 0x01
6667
68+ #define MXS_I2C_DATA (0xa0)
69+
70+ #define MXS_I2C_DEBUG0 (0xb0)
71+ #define MXS_I2C_DEBUG0_CLR (0xb8)
72+
73+ #define MXS_I2C_DEBUG0_DMAREQ 0x80000000
74+
6775#define MXS_I2C_IRQ_MASK (MXS_I2C_CTRL1_DATA_ENGINE_CMPLT_IRQ | \
6876 MXS_I2C_CTRL1_NO_SLAVE_ACK_IRQ | \
6977 MXS_I2C_CTRL1_EARLY_TERM_IRQ | \
@@ -298,6 +306,135 @@ static int mxs_i2c_dma_setup_xfer(struct i2c_adapter *adap,
298306 return - EINVAL ;
299307}
300308
309+ static int mxs_i2c_pio_wait_dmareq (struct mxs_i2c_dev * i2c )
310+ {
311+ unsigned long timeout = jiffies + msecs_to_jiffies (1000 );
312+
313+ while (!(readl (i2c -> regs + MXS_I2C_DEBUG0 ) &
314+ MXS_I2C_DEBUG0_DMAREQ )) {
315+ if (time_after (jiffies , timeout ))
316+ return - ETIMEDOUT ;
317+ cond_resched ();
318+ }
319+
320+ writel (MXS_I2C_DEBUG0_DMAREQ , i2c -> regs + MXS_I2C_DEBUG0_CLR );
321+
322+ return 0 ;
323+ }
324+
325+ static int mxs_i2c_pio_wait_cplt (struct mxs_i2c_dev * i2c )
326+ {
327+ unsigned long timeout = jiffies + msecs_to_jiffies (1000 );
328+
329+ /*
330+ * We do not use interrupts in the PIO mode. Due to the
331+ * maximum transfer length being 8 bytes in PIO mode, the
332+ * overhead of interrupt would be too large and this would
333+ * neglect the gain from using the PIO mode.
334+ */
335+
336+ while (!(readl (i2c -> regs + MXS_I2C_CTRL1 ) &
337+ MXS_I2C_CTRL1_DATA_ENGINE_CMPLT_IRQ )) {
338+ if (time_after (jiffies , timeout ))
339+ return - ETIMEDOUT ;
340+ cond_resched ();
341+ }
342+
343+ writel (MXS_I2C_CTRL1_DATA_ENGINE_CMPLT_IRQ ,
344+ i2c -> regs + MXS_I2C_CTRL1_CLR );
345+
346+ return 0 ;
347+ }
348+
349+ static int mxs_i2c_pio_setup_xfer (struct i2c_adapter * adap ,
350+ struct i2c_msg * msg , uint32_t flags )
351+ {
352+ struct mxs_i2c_dev * i2c = i2c_get_adapdata (adap );
353+ uint32_t addr_data = msg -> addr << 1 ;
354+ uint32_t data = 0 ;
355+ int i , shifts_left , ret ;
356+
357+ /* Mute IRQs coming from this block. */
358+ writel (MXS_I2C_IRQ_MASK << 8 , i2c -> regs + MXS_I2C_CTRL1_CLR );
359+
360+ if (msg -> flags & I2C_M_RD ) {
361+ addr_data |= I2C_SMBUS_READ ;
362+
363+ /* SELECT command. */
364+ writel (MXS_I2C_CTRL0_RUN | MXS_CMD_I2C_SELECT ,
365+ i2c -> regs + MXS_I2C_CTRL0 );
366+
367+ ret = mxs_i2c_pio_wait_dmareq (i2c );
368+ if (ret )
369+ return ret ;
370+
371+ writel (addr_data , i2c -> regs + MXS_I2C_DATA );
372+
373+ ret = mxs_i2c_pio_wait_cplt (i2c );
374+ if (ret )
375+ return ret ;
376+
377+ /* READ command. */
378+ writel (MXS_I2C_CTRL0_RUN | MXS_CMD_I2C_READ | flags |
379+ MXS_I2C_CTRL0_XFER_COUNT (msg -> len ),
380+ i2c -> regs + MXS_I2C_CTRL0 );
381+
382+ for (i = 0 ; i < msg -> len ; i ++ ) {
383+ if ((i & 3 ) == 0 ) {
384+ ret = mxs_i2c_pio_wait_dmareq (i2c );
385+ if (ret )
386+ return ret ;
387+ data = readl (i2c -> regs + MXS_I2C_DATA );
388+ }
389+ msg -> buf [i ] = data & 0xff ;
390+ data >>= 8 ;
391+ }
392+ } else {
393+ addr_data |= I2C_SMBUS_WRITE ;
394+
395+ /* WRITE command. */
396+ writel (MXS_I2C_CTRL0_RUN | MXS_CMD_I2C_WRITE | flags |
397+ MXS_I2C_CTRL0_XFER_COUNT (msg -> len + 1 ),
398+ i2c -> regs + MXS_I2C_CTRL0 );
399+
400+ /*
401+ * The LSB of data buffer is the first byte blasted across
402+ * the bus. Higher order bytes follow. Thus the following
403+ * filling schematic.
404+ */
405+ data = addr_data << 24 ;
406+ for (i = 0 ; i < msg -> len ; i ++ ) {
407+ data >>= 8 ;
408+ data |= (msg -> buf [i ] << 24 );
409+ if ((i & 3 ) == 2 ) {
410+ ret = mxs_i2c_pio_wait_dmareq (i2c );
411+ if (ret )
412+ return ret ;
413+ writel (data , i2c -> regs + MXS_I2C_DATA );
414+ }
415+ }
416+
417+ shifts_left = 24 - (i & 3 ) * 8 ;
418+ if (shifts_left ) {
419+ data >>= shifts_left ;
420+ ret = mxs_i2c_pio_wait_dmareq (i2c );
421+ if (ret )
422+ return ret ;
423+ writel (data , i2c -> regs + MXS_I2C_DATA );
424+ }
425+ }
426+
427+ ret = mxs_i2c_pio_wait_cplt (i2c );
428+ if (ret )
429+ return ret ;
430+
431+ /* Clear any dangling IRQs and re-enable interrupts. */
432+ writel (MXS_I2C_IRQ_MASK , i2c -> regs + MXS_I2C_CTRL1_CLR );
433+ writel (MXS_I2C_IRQ_MASK << 8 , i2c -> regs + MXS_I2C_CTRL1_SET );
434+
435+ return 0 ;
436+ }
437+
301438/*
302439 * Low level master read/write transaction.
303440 */
@@ -316,24 +453,37 @@ static int mxs_i2c_xfer_msg(struct i2c_adapter *adap, struct i2c_msg *msg,
316453 if (msg -> len == 0 )
317454 return - EINVAL ;
318455
319- INIT_COMPLETION (i2c -> cmd_complete );
320- i2c -> cmd_err = 0 ;
321-
322- ret = mxs_i2c_dma_setup_xfer (adap , msg , flags );
323- if (ret )
324- return ret ;
456+ /*
457+ * The current boundary to select between PIO/DMA transfer method
458+ * is set to 8 bytes, transfers shorter than 8 bytes are transfered
459+ * using PIO mode while longer transfers use DMA. The 8 byte border is
460+ * based on this empirical measurement and a lot of previous frobbing.
461+ */
462+ if (msg -> len < 8 ) {
463+ ret = mxs_i2c_pio_setup_xfer (adap , msg , flags );
464+ if (ret )
465+ mxs_i2c_reset (i2c );
466+ } else {
467+ i2c -> cmd_err = 0 ;
468+ INIT_COMPLETION (i2c -> cmd_complete );
469+ ret = mxs_i2c_dma_setup_xfer (adap , msg , flags );
470+ if (ret )
471+ return ret ;
325472
326- ret = wait_for_completion_timeout (& i2c -> cmd_complete ,
473+ ret = wait_for_completion_timeout (& i2c -> cmd_complete ,
327474 msecs_to_jiffies (1000 ));
328- if (ret == 0 )
329- goto timeout ;
475+ if (ret == 0 )
476+ goto timeout ;
477+
478+ if (i2c -> cmd_err == - ENXIO )
479+ mxs_i2c_reset (i2c );
330480
331- if ( i2c -> cmd_err == - ENXIO )
332- mxs_i2c_reset ( i2c );
481+ ret = i2c -> cmd_err ;
482+ }
333483
334- dev_dbg (i2c -> dev , "Done with err=%d\n" , i2c -> cmd_err );
484+ dev_dbg (i2c -> dev , "Done with err=%d\n" , ret );
335485
336- return i2c -> cmd_err ;
486+ return ret ;
337487
338488timeout :
339489 dev_dbg (i2c -> dev , "Timeout!\n" );
0 commit comments