2 * Synopsys DesignWare I2C adapter driver (master only).
4 * Based on the TI DAVINCI I2C adapter driver.
6 * Copyright (C) 2006 Texas Instruments.
7 * Copyright (C) 2007 MontaVista Software Inc.
8 * Copyright (C) 2009 Provigent Ltd.
10 * ----------------------------------------------------------------------------
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 * ----------------------------------------------------------------------------
28 #include <linux/clk.h>
29 #include <linux/errno.h>
30 #include <linux/err.h>
31 #include <linux/i2c.h>
32 #include <linux/interrupt.h>
34 #include <linux/delay.h>
35 #include "i2c-designware-core.h"
37 static char *abort_sources
[] = {
38 [ABRT_7B_ADDR_NOACK
] =
39 "slave address not acknowledged (7bit mode)",
40 [ABRT_10ADDR1_NOACK
] =
41 "first address byte not acknowledged (10bit mode)",
42 [ABRT_10ADDR2_NOACK
] =
43 "second address byte not acknowledged (10bit mode)",
45 "data not acknowledged",
47 "no acknowledgement for a general call",
49 "read after general call",
51 "start byte acknowledged",
52 [ABRT_SBYTE_NORSTRT
] =
53 "trying to send start byte when restart is disabled",
54 [ABRT_10B_RD_NORSTRT
] =
55 "trying to read when restart is disabled (10bit mode)",
57 "trying to use disabled adapter",
62 u32
dw_readl(struct dw_i2c_dev
*dev
, int offset
)
64 u32 value
= readl(dev
->base
+ offset
);
72 void dw_writel(struct dw_i2c_dev
*dev
, u32 b
, int offset
)
77 writel(b
, dev
->base
+ offset
);
81 i2c_dw_scl_hcnt(u32 ic_clk
, u32 tSYMBOL
, u32 tf
, int cond
, int offset
)
84 * DesignWare I2C core doesn't seem to have solid strategy to meet
85 * the tHD;STA timing spec. Configuring _HCNT based on tHIGH spec
86 * will result in violation of the tHD;STA spec.
90 * Conditional expression:
92 * IC_[FS]S_SCL_HCNT + (1+4+3) >= IC_CLK * tHIGH
94 * This is based on the DW manuals, and represents an ideal
95 * configuration. The resulting I2C bus speed will be
96 * faster than any of the others.
98 * If your hardware is free from tHD;STA issue, try this one.
100 return (ic_clk
* tSYMBOL
+ 5000) / 10000 - 8 + offset
;
103 * Conditional expression:
105 * IC_[FS]S_SCL_HCNT + 3 >= IC_CLK * (tHD;STA + tf)
107 * This is just experimental rule; the tHD;STA period turned
108 * out to be proportinal to (_HCNT + 3). With this setting,
109 * we could meet both tHIGH and tHD;STA timing specs.
111 * If unsure, you'd better to take this alternative.
113 * The reason why we need to take into account "tf" here,
114 * is the same as described in i2c_dw_scl_lcnt().
116 return (ic_clk
* (tSYMBOL
+ tf
) + 5000) / 10000 - 3 + offset
;
119 static u32
i2c_dw_scl_lcnt(u32 ic_clk
, u32 tLOW
, u32 tf
, int offset
)
122 * Conditional expression:
124 * IC_[FS]S_SCL_LCNT + 1 >= IC_CLK * (tLOW + tf)
126 * DW I2C core starts counting the SCL CNTs for the LOW period
127 * of the SCL clock (tLOW) as soon as it pulls the SCL line.
128 * In order to meet the tLOW timing spec, we need to take into
129 * account the fall time of SCL signal (tf). Default tf value
130 * should be 0.3 us, for safety.
132 return ((ic_clk
* (tLOW
+ tf
) + 5000) / 10000) - 1 + offset
;
136 * i2c_dw_init() - initialize the designware i2c master hardware
137 * @dev: device private data
139 * This functions configures and enables the I2C master.
140 * This function is called during I2C init function, and in case of timeout at
143 int i2c_dw_init(struct dw_i2c_dev
*dev
)
145 u32 input_clock_khz
= clk_get_rate(dev
->clk
) / 1000;
146 u32 ic_con
, hcnt
, lcnt
;
149 /* Configure register endianess access */
150 reg
= dw_readl(dev
, DW_IC_COMP_TYPE
);
151 if (reg
== ___constant_swab32(DW_IC_COMP_TYPE_VALUE
)) {
153 reg
= DW_IC_COMP_TYPE_VALUE
;
156 if (reg
!= DW_IC_COMP_TYPE_VALUE
) {
157 dev_err(dev
->dev
, "Unknown Synopsys component type: "
162 /* Disable the adapter */
163 dw_writel(dev
, 0, DW_IC_ENABLE
);
165 /* set standard and fast speed deviders for high/low periods */
168 hcnt
= i2c_dw_scl_hcnt(input_clock_khz
,
169 40, /* tHD;STA = tHIGH = 4.0 us */
171 0, /* 0: DW default, 1: Ideal */
173 lcnt
= i2c_dw_scl_lcnt(input_clock_khz
,
174 47, /* tLOW = 4.7 us */
177 dw_writel(dev
, hcnt
, DW_IC_SS_SCL_HCNT
);
178 dw_writel(dev
, lcnt
, DW_IC_SS_SCL_LCNT
);
179 dev_dbg(dev
->dev
, "Standard-mode HCNT:LCNT = %d:%d\n", hcnt
, lcnt
);
182 hcnt
= i2c_dw_scl_hcnt(input_clock_khz
,
183 6, /* tHD;STA = tHIGH = 0.6 us */
185 0, /* 0: DW default, 1: Ideal */
187 lcnt
= i2c_dw_scl_lcnt(input_clock_khz
,
188 13, /* tLOW = 1.3 us */
191 dw_writel(dev
, hcnt
, DW_IC_FS_SCL_HCNT
);
192 dw_writel(dev
, lcnt
, DW_IC_FS_SCL_LCNT
);
193 dev_dbg(dev
->dev
, "Fast-mode HCNT:LCNT = %d:%d\n", hcnt
, lcnt
);
195 /* Configure Tx/Rx FIFO threshold levels */
196 dw_writel(dev
, dev
->tx_fifo_depth
- 1, DW_IC_TX_TL
);
197 dw_writel(dev
, 0, DW_IC_RX_TL
);
199 /* configure the i2c master */
200 ic_con
= DW_IC_CON_MASTER
| DW_IC_CON_SLAVE_DISABLE
|
201 DW_IC_CON_RESTART_EN
| DW_IC_CON_SPEED_FAST
;
202 dw_writel(dev
, ic_con
, DW_IC_CON
);
207 * Waiting for bus not busy
209 static int i2c_dw_wait_bus_not_busy(struct dw_i2c_dev
*dev
)
211 int timeout
= TIMEOUT
;
213 while (dw_readl(dev
, DW_IC_STATUS
) & DW_IC_STATUS_ACTIVITY
) {
215 dev_warn(dev
->dev
, "timeout waiting for bus ready\n");
225 static void i2c_dw_xfer_init(struct dw_i2c_dev
*dev
)
227 struct i2c_msg
*msgs
= dev
->msgs
;
230 /* Disable the adapter */
231 dw_writel(dev
, 0, DW_IC_ENABLE
);
233 /* set the slave (target) address */
234 dw_writel(dev
, msgs
[dev
->msg_write_idx
].addr
, DW_IC_TAR
);
236 /* if the slave address is ten bit address, enable 10BITADDR */
237 ic_con
= dw_readl(dev
, DW_IC_CON
);
238 if (msgs
[dev
->msg_write_idx
].flags
& I2C_M_TEN
)
239 ic_con
|= DW_IC_CON_10BITADDR_MASTER
;
241 ic_con
&= ~DW_IC_CON_10BITADDR_MASTER
;
242 dw_writel(dev
, ic_con
, DW_IC_CON
);
244 /* Enable the adapter */
245 dw_writel(dev
, 1, DW_IC_ENABLE
);
247 /* Enable interrupts */
248 dw_writel(dev
, DW_IC_INTR_DEFAULT_MASK
, DW_IC_INTR_MASK
);
252 * Initiate (and continue) low level master read/write transaction.
253 * This function is only called from i2c_dw_isr, and pumping i2c_msg
254 * messages into the tx buffer. Even if the size of i2c_msg data is
255 * longer than the size of the tx buffer, it handles everything.
258 i2c_dw_xfer_msg(struct dw_i2c_dev
*dev
)
260 struct i2c_msg
*msgs
= dev
->msgs
;
262 int tx_limit
, rx_limit
;
263 u32 addr
= msgs
[dev
->msg_write_idx
].addr
;
264 u32 buf_len
= dev
->tx_buf_len
;
265 u8
*buf
= dev
->tx_buf
;
267 intr_mask
= DW_IC_INTR_DEFAULT_MASK
;
269 for (; dev
->msg_write_idx
< dev
->msgs_num
; dev
->msg_write_idx
++) {
271 * if target address has changed, we need to
272 * reprogram the target address in the i2c
273 * adapter when we are done with this transfer
275 if (msgs
[dev
->msg_write_idx
].addr
!= addr
) {
277 "%s: invalid target address\n", __func__
);
278 dev
->msg_err
= -EINVAL
;
282 if (msgs
[dev
->msg_write_idx
].len
== 0) {
284 "%s: invalid message length\n", __func__
);
285 dev
->msg_err
= -EINVAL
;
289 if (!(dev
->status
& STATUS_WRITE_IN_PROGRESS
)) {
291 buf
= msgs
[dev
->msg_write_idx
].buf
;
292 buf_len
= msgs
[dev
->msg_write_idx
].len
;
295 tx_limit
= dev
->tx_fifo_depth
- dw_readl(dev
, DW_IC_TXFLR
);
296 rx_limit
= dev
->rx_fifo_depth
- dw_readl(dev
, DW_IC_RXFLR
);
298 while (buf_len
> 0 && tx_limit
> 0 && rx_limit
> 0) {
299 if (msgs
[dev
->msg_write_idx
].flags
& I2C_M_RD
) {
300 dw_writel(dev
, 0x100, DW_IC_DATA_CMD
);
303 dw_writel(dev
, *buf
++, DW_IC_DATA_CMD
);
304 tx_limit
--; buf_len
--;
308 dev
->tx_buf_len
= buf_len
;
311 /* more bytes to be written */
312 dev
->status
|= STATUS_WRITE_IN_PROGRESS
;
315 dev
->status
&= ~STATUS_WRITE_IN_PROGRESS
;
319 * If i2c_msg index search is completed, we don't need TX_EMPTY
320 * interrupt any more.
322 if (dev
->msg_write_idx
== dev
->msgs_num
)
323 intr_mask
&= ~DW_IC_INTR_TX_EMPTY
;
328 dw_writel(dev
, intr_mask
, DW_IC_INTR_MASK
);
332 i2c_dw_read(struct dw_i2c_dev
*dev
)
334 struct i2c_msg
*msgs
= dev
->msgs
;
337 for (; dev
->msg_read_idx
< dev
->msgs_num
; dev
->msg_read_idx
++) {
341 if (!(msgs
[dev
->msg_read_idx
].flags
& I2C_M_RD
))
344 if (!(dev
->status
& STATUS_READ_IN_PROGRESS
)) {
345 len
= msgs
[dev
->msg_read_idx
].len
;
346 buf
= msgs
[dev
->msg_read_idx
].buf
;
348 len
= dev
->rx_buf_len
;
352 rx_valid
= dw_readl(dev
, DW_IC_RXFLR
);
354 for (; len
> 0 && rx_valid
> 0; len
--, rx_valid
--)
355 *buf
++ = dw_readl(dev
, DW_IC_DATA_CMD
);
358 dev
->status
|= STATUS_READ_IN_PROGRESS
;
359 dev
->rx_buf_len
= len
;
363 dev
->status
&= ~STATUS_READ_IN_PROGRESS
;
367 static int i2c_dw_handle_tx_abort(struct dw_i2c_dev
*dev
)
369 unsigned long abort_source
= dev
->abort_source
;
372 if (abort_source
& DW_IC_TX_ABRT_NOACK
) {
373 for_each_set_bit(i
, &abort_source
, ARRAY_SIZE(abort_sources
))
375 "%s: %s\n", __func__
, abort_sources
[i
]);
379 for_each_set_bit(i
, &abort_source
, ARRAY_SIZE(abort_sources
))
380 dev_err(dev
->dev
, "%s: %s\n", __func__
, abort_sources
[i
]);
382 if (abort_source
& DW_IC_TX_ARB_LOST
)
384 else if (abort_source
& DW_IC_TX_ABRT_GCALL_READ
)
385 return -EINVAL
; /* wrong msgs[] data */
391 * Prepare controller for a transaction and call i2c_dw_xfer_msg
394 i2c_dw_xfer(struct i2c_adapter
*adap
, struct i2c_msg msgs
[], int num
)
396 struct dw_i2c_dev
*dev
= i2c_get_adapdata(adap
);
399 dev_dbg(dev
->dev
, "%s: msgs: %d\n", __func__
, num
);
401 mutex_lock(&dev
->lock
);
403 INIT_COMPLETION(dev
->cmd_complete
);
407 dev
->msg_write_idx
= 0;
408 dev
->msg_read_idx
= 0;
410 dev
->status
= STATUS_IDLE
;
411 dev
->abort_source
= 0;
413 ret
= i2c_dw_wait_bus_not_busy(dev
);
417 /* start the transfers */
418 i2c_dw_xfer_init(dev
);
420 /* wait for tx to complete */
421 ret
= wait_for_completion_interruptible_timeout(&dev
->cmd_complete
, HZ
);
423 dev_err(dev
->dev
, "controller timed out\n");
436 if (likely(!dev
->cmd_err
)) {
437 /* Disable the adapter */
438 dw_writel(dev
, 0, DW_IC_ENABLE
);
443 /* We have an error */
444 if (dev
->cmd_err
== DW_IC_ERR_TX_ABRT
) {
445 ret
= i2c_dw_handle_tx_abort(dev
);
451 mutex_unlock(&dev
->lock
);
456 u32
i2c_dw_func(struct i2c_adapter
*adap
)
458 return I2C_FUNC_I2C
|
459 I2C_FUNC_10BIT_ADDR
|
460 I2C_FUNC_SMBUS_BYTE
|
461 I2C_FUNC_SMBUS_BYTE_DATA
|
462 I2C_FUNC_SMBUS_WORD_DATA
|
463 I2C_FUNC_SMBUS_I2C_BLOCK
;
466 static u32
i2c_dw_read_clear_intrbits(struct dw_i2c_dev
*dev
)
471 * The IC_INTR_STAT register just indicates "enabled" interrupts.
472 * Ths unmasked raw version of interrupt status bits are available
473 * in the IC_RAW_INTR_STAT register.
476 * stat = dw_readl(IC_INTR_STAT);
478 * stat = dw_readl(IC_RAW_INTR_STAT) & dw_readl(IC_INTR_MASK);
480 * The raw version might be useful for debugging purposes.
482 stat
= dw_readl(dev
, DW_IC_INTR_STAT
);
485 * Do not use the IC_CLR_INTR register to clear interrupts, or
486 * you'll miss some interrupts, triggered during the period from
487 * dw_readl(IC_INTR_STAT) to dw_readl(IC_CLR_INTR).
489 * Instead, use the separately-prepared IC_CLR_* registers.
491 if (stat
& DW_IC_INTR_RX_UNDER
)
492 dw_readl(dev
, DW_IC_CLR_RX_UNDER
);
493 if (stat
& DW_IC_INTR_RX_OVER
)
494 dw_readl(dev
, DW_IC_CLR_RX_OVER
);
495 if (stat
& DW_IC_INTR_TX_OVER
)
496 dw_readl(dev
, DW_IC_CLR_TX_OVER
);
497 if (stat
& DW_IC_INTR_RD_REQ
)
498 dw_readl(dev
, DW_IC_CLR_RD_REQ
);
499 if (stat
& DW_IC_INTR_TX_ABRT
) {
501 * The IC_TX_ABRT_SOURCE register is cleared whenever
502 * the IC_CLR_TX_ABRT is read. Preserve it beforehand.
504 dev
->abort_source
= dw_readl(dev
, DW_IC_TX_ABRT_SOURCE
);
505 dw_readl(dev
, DW_IC_CLR_TX_ABRT
);
507 if (stat
& DW_IC_INTR_RX_DONE
)
508 dw_readl(dev
, DW_IC_CLR_RX_DONE
);
509 if (stat
& DW_IC_INTR_ACTIVITY
)
510 dw_readl(dev
, DW_IC_CLR_ACTIVITY
);
511 if (stat
& DW_IC_INTR_STOP_DET
)
512 dw_readl(dev
, DW_IC_CLR_STOP_DET
);
513 if (stat
& DW_IC_INTR_START_DET
)
514 dw_readl(dev
, DW_IC_CLR_START_DET
);
515 if (stat
& DW_IC_INTR_GEN_CALL
)
516 dw_readl(dev
, DW_IC_CLR_GEN_CALL
);
522 * Interrupt service routine. This gets called whenever an I2C interrupt
525 irqreturn_t
i2c_dw_isr(int this_irq
, void *dev_id
)
527 struct dw_i2c_dev
*dev
= dev_id
;
530 stat
= i2c_dw_read_clear_intrbits(dev
);
531 dev_dbg(dev
->dev
, "%s: stat=0x%x\n", __func__
, stat
);
533 if (stat
& DW_IC_INTR_TX_ABRT
) {
534 dev
->cmd_err
|= DW_IC_ERR_TX_ABRT
;
535 dev
->status
= STATUS_IDLE
;
538 * Anytime TX_ABRT is set, the contents of the tx/rx
539 * buffers are flushed. Make sure to skip them.
541 dw_writel(dev
, 0, DW_IC_INTR_MASK
);
545 if (stat
& DW_IC_INTR_RX_FULL
)
548 if (stat
& DW_IC_INTR_TX_EMPTY
)
549 i2c_dw_xfer_msg(dev
);
552 * No need to modify or disable the interrupt mask here.
553 * i2c_dw_xfer_msg() will take care of it according to
554 * the current transmit status.
558 if ((stat
& (DW_IC_INTR_TX_ABRT
| DW_IC_INTR_STOP_DET
)) || dev
->msg_err
)
559 complete(&dev
->cmd_complete
);