aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/i2c/busses/i2c-designware.c
diff options
context:
space:
mode:
authorAndrea Bastoni <bastoni@cs.unc.edu>2010-05-30 19:16:45 -0400
committerAndrea Bastoni <bastoni@cs.unc.edu>2010-05-30 19:16:45 -0400
commitada47b5fe13d89735805b566185f4885f5a3f750 (patch)
tree644b88f8a71896307d71438e9b3af49126ffb22b /drivers/i2c/busses/i2c-designware.c
parent43e98717ad40a4ae64545b5ba047c7b86aa44f4f (diff)
parent3280f21d43ee541f97f8cda5792150d2dbec20d5 (diff)
Merge branch 'wip-2.6.34' into old-private-masterarchived-private-master
Diffstat (limited to 'drivers/i2c/busses/i2c-designware.c')
-rw-r--r--drivers/i2c/busses/i2c-designware.c489
1 files changed, 356 insertions, 133 deletions
diff --git a/drivers/i2c/busses/i2c-designware.c b/drivers/i2c/busses/i2c-designware.c
index b444762e9b9f..b664ed8bbdb3 100644
--- a/drivers/i2c/busses/i2c-designware.c
+++ b/drivers/i2c/busses/i2c-designware.c
@@ -1,5 +1,5 @@
1/* 1/*
2 * Synopsys Designware I2C adapter driver (master only). 2 * Synopsys DesignWare I2C adapter driver (master only).
3 * 3 *
4 * Based on the TI DAVINCI I2C adapter driver. 4 * Based on the TI DAVINCI I2C adapter driver.
5 * 5 *
@@ -36,6 +36,7 @@
36#include <linux/interrupt.h> 36#include <linux/interrupt.h>
37#include <linux/platform_device.h> 37#include <linux/platform_device.h>
38#include <linux/io.h> 38#include <linux/io.h>
39#include <linux/slab.h>
39 40
40/* 41/*
41 * Registers offset 42 * Registers offset
@@ -49,7 +50,20 @@
49#define DW_IC_FS_SCL_LCNT 0x20 50#define DW_IC_FS_SCL_LCNT 0x20
50#define DW_IC_INTR_STAT 0x2c 51#define DW_IC_INTR_STAT 0x2c
51#define DW_IC_INTR_MASK 0x30 52#define DW_IC_INTR_MASK 0x30
53#define DW_IC_RAW_INTR_STAT 0x34
54#define DW_IC_RX_TL 0x38
55#define DW_IC_TX_TL 0x3c
52#define DW_IC_CLR_INTR 0x40 56#define DW_IC_CLR_INTR 0x40
57#define DW_IC_CLR_RX_UNDER 0x44
58#define DW_IC_CLR_RX_OVER 0x48
59#define DW_IC_CLR_TX_OVER 0x4c
60#define DW_IC_CLR_RD_REQ 0x50
61#define DW_IC_CLR_TX_ABRT 0x54
62#define DW_IC_CLR_RX_DONE 0x58
63#define DW_IC_CLR_ACTIVITY 0x5c
64#define DW_IC_CLR_STOP_DET 0x60
65#define DW_IC_CLR_START_DET 0x64
66#define DW_IC_CLR_GEN_CALL 0x68
53#define DW_IC_ENABLE 0x6c 67#define DW_IC_ENABLE 0x6c
54#define DW_IC_STATUS 0x70 68#define DW_IC_STATUS 0x70
55#define DW_IC_TXFLR 0x74 69#define DW_IC_TXFLR 0x74
@@ -64,9 +78,23 @@
64#define DW_IC_CON_RESTART_EN 0x20 78#define DW_IC_CON_RESTART_EN 0x20
65#define DW_IC_CON_SLAVE_DISABLE 0x40 79#define DW_IC_CON_SLAVE_DISABLE 0x40
66 80
67#define DW_IC_INTR_TX_EMPTY 0x10 81#define DW_IC_INTR_RX_UNDER 0x001
68#define DW_IC_INTR_TX_ABRT 0x40 82#define DW_IC_INTR_RX_OVER 0x002
83#define DW_IC_INTR_RX_FULL 0x004
84#define DW_IC_INTR_TX_OVER 0x008
85#define DW_IC_INTR_TX_EMPTY 0x010
86#define DW_IC_INTR_RD_REQ 0x020
87#define DW_IC_INTR_TX_ABRT 0x040
88#define DW_IC_INTR_RX_DONE 0x080
89#define DW_IC_INTR_ACTIVITY 0x100
69#define DW_IC_INTR_STOP_DET 0x200 90#define DW_IC_INTR_STOP_DET 0x200
91#define DW_IC_INTR_START_DET 0x400
92#define DW_IC_INTR_GEN_CALL 0x800
93
94#define DW_IC_INTR_DEFAULT_MASK (DW_IC_INTR_RX_FULL | \
95 DW_IC_INTR_TX_EMPTY | \
96 DW_IC_INTR_TX_ABRT | \
97 DW_IC_INTR_STOP_DET)
70 98
71#define DW_IC_STATUS_ACTIVITY 0x1 99#define DW_IC_STATUS_ACTIVITY 0x1
72 100
@@ -96,31 +124,49 @@
96#define ABRT_SBYTE_ACKDET 7 124#define ABRT_SBYTE_ACKDET 7
97#define ABRT_SBYTE_NORSTRT 9 125#define ABRT_SBYTE_NORSTRT 9
98#define ABRT_10B_RD_NORSTRT 10 126#define ABRT_10B_RD_NORSTRT 10
99#define ARB_MASTER_DIS 11 127#define ABRT_MASTER_DIS 11
100#define ARB_LOST 12 128#define ARB_LOST 12
101 129
130#define DW_IC_TX_ABRT_7B_ADDR_NOACK (1UL << ABRT_7B_ADDR_NOACK)
131#define DW_IC_TX_ABRT_10ADDR1_NOACK (1UL << ABRT_10ADDR1_NOACK)
132#define DW_IC_TX_ABRT_10ADDR2_NOACK (1UL << ABRT_10ADDR2_NOACK)
133#define DW_IC_TX_ABRT_TXDATA_NOACK (1UL << ABRT_TXDATA_NOACK)
134#define DW_IC_TX_ABRT_GCALL_NOACK (1UL << ABRT_GCALL_NOACK)
135#define DW_IC_TX_ABRT_GCALL_READ (1UL << ABRT_GCALL_READ)
136#define DW_IC_TX_ABRT_SBYTE_ACKDET (1UL << ABRT_SBYTE_ACKDET)
137#define DW_IC_TX_ABRT_SBYTE_NORSTRT (1UL << ABRT_SBYTE_NORSTRT)
138#define DW_IC_TX_ABRT_10B_RD_NORSTRT (1UL << ABRT_10B_RD_NORSTRT)
139#define DW_IC_TX_ABRT_MASTER_DIS (1UL << ABRT_MASTER_DIS)
140#define DW_IC_TX_ARB_LOST (1UL << ARB_LOST)
141
142#define DW_IC_TX_ABRT_NOACK (DW_IC_TX_ABRT_7B_ADDR_NOACK | \
143 DW_IC_TX_ABRT_10ADDR1_NOACK | \
144 DW_IC_TX_ABRT_10ADDR2_NOACK | \
145 DW_IC_TX_ABRT_TXDATA_NOACK | \
146 DW_IC_TX_ABRT_GCALL_NOACK)
147
102static char *abort_sources[] = { 148static char *abort_sources[] = {
103 [ABRT_7B_ADDR_NOACK] = 149 [ABRT_7B_ADDR_NOACK] =
104 "slave address not acknowledged (7bit mode)", 150 "slave address not acknowledged (7bit mode)",
105 [ABRT_10ADDR1_NOACK] = 151 [ABRT_10ADDR1_NOACK] =
106 "first address byte not acknowledged (10bit mode)", 152 "first address byte not acknowledged (10bit mode)",
107 [ABRT_10ADDR2_NOACK] = 153 [ABRT_10ADDR2_NOACK] =
108 "second address byte not acknowledged (10bit mode)", 154 "second address byte not acknowledged (10bit mode)",
109 [ABRT_TXDATA_NOACK] = 155 [ABRT_TXDATA_NOACK] =
110 "data not acknowledged", 156 "data not acknowledged",
111 [ABRT_GCALL_NOACK] = 157 [ABRT_GCALL_NOACK] =
112 "no acknowledgement for a general call", 158 "no acknowledgement for a general call",
113 [ABRT_GCALL_READ] = 159 [ABRT_GCALL_READ] =
114 "read after general call", 160 "read after general call",
115 [ABRT_SBYTE_ACKDET] = 161 [ABRT_SBYTE_ACKDET] =
116 "start byte acknowledged", 162 "start byte acknowledged",
117 [ABRT_SBYTE_NORSTRT] = 163 [ABRT_SBYTE_NORSTRT] =
118 "trying to send start byte when restart is disabled", 164 "trying to send start byte when restart is disabled",
119 [ABRT_10B_RD_NORSTRT] = 165 [ABRT_10B_RD_NORSTRT] =
120 "trying to read when restart is disabled (10bit mode)", 166 "trying to read when restart is disabled (10bit mode)",
121 [ARB_MASTER_DIS] = 167 [ABRT_MASTER_DIS] =
122 "trying to use disabled adapter", 168 "trying to use disabled adapter",
123 [ARB_LOST] = 169 [ARB_LOST] =
124 "lost arbitration", 170 "lost arbitration",
125}; 171};
126 172
@@ -129,7 +175,6 @@ static char *abort_sources[] = {
129 * @dev: driver model device node 175 * @dev: driver model device node
130 * @base: IO registers pointer 176 * @base: IO registers pointer
131 * @cmd_complete: tx completion indicator 177 * @cmd_complete: tx completion indicator
132 * @pump_msg: continue in progress transfers
133 * @lock: protect this struct and IO registers 178 * @lock: protect this struct and IO registers
134 * @clk: input reference clock 179 * @clk: input reference clock
135 * @cmd_err: run time hadware error code 180 * @cmd_err: run time hadware error code
@@ -155,27 +200,81 @@ struct dw_i2c_dev {
155 struct device *dev; 200 struct device *dev;
156 void __iomem *base; 201 void __iomem *base;
157 struct completion cmd_complete; 202 struct completion cmd_complete;
158 struct tasklet_struct pump_msg;
159 struct mutex lock; 203 struct mutex lock;
160 struct clk *clk; 204 struct clk *clk;
161 int cmd_err; 205 int cmd_err;
162 struct i2c_msg *msgs; 206 struct i2c_msg *msgs;
163 int msgs_num; 207 int msgs_num;
164 int msg_write_idx; 208 int msg_write_idx;
165 u16 tx_buf_len; 209 u32 tx_buf_len;
166 u8 *tx_buf; 210 u8 *tx_buf;
167 int msg_read_idx; 211 int msg_read_idx;
168 u16 rx_buf_len; 212 u32 rx_buf_len;
169 u8 *rx_buf; 213 u8 *rx_buf;
170 int msg_err; 214 int msg_err;
171 unsigned int status; 215 unsigned int status;
172 u16 abort_source; 216 u32 abort_source;
173 int irq; 217 int irq;
174 struct i2c_adapter adapter; 218 struct i2c_adapter adapter;
175 unsigned int tx_fifo_depth; 219 unsigned int tx_fifo_depth;
176 unsigned int rx_fifo_depth; 220 unsigned int rx_fifo_depth;
177}; 221};
178 222
223static u32
224i2c_dw_scl_hcnt(u32 ic_clk, u32 tSYMBOL, u32 tf, int cond, int offset)
225{
226 /*
227 * DesignWare I2C core doesn't seem to have solid strategy to meet
228 * the tHD;STA timing spec. Configuring _HCNT based on tHIGH spec
229 * will result in violation of the tHD;STA spec.
230 */
231 if (cond)
232 /*
233 * Conditional expression:
234 *
235 * IC_[FS]S_SCL_HCNT + (1+4+3) >= IC_CLK * tHIGH
236 *
237 * This is based on the DW manuals, and represents an ideal
238 * configuration. The resulting I2C bus speed will be
239 * faster than any of the others.
240 *
241 * If your hardware is free from tHD;STA issue, try this one.
242 */
243 return (ic_clk * tSYMBOL + 5000) / 10000 - 8 + offset;
244 else
245 /*
246 * Conditional expression:
247 *
248 * IC_[FS]S_SCL_HCNT + 3 >= IC_CLK * (tHD;STA + tf)
249 *
250 * This is just experimental rule; the tHD;STA period turned
251 * out to be proportinal to (_HCNT + 3). With this setting,
252 * we could meet both tHIGH and tHD;STA timing specs.
253 *
254 * If unsure, you'd better to take this alternative.
255 *
256 * The reason why we need to take into account "tf" here,
257 * is the same as described in i2c_dw_scl_lcnt().
258 */
259 return (ic_clk * (tSYMBOL + tf) + 5000) / 10000 - 3 + offset;
260}
261
262static u32 i2c_dw_scl_lcnt(u32 ic_clk, u32 tLOW, u32 tf, int offset)
263{
264 /*
265 * Conditional expression:
266 *
267 * IC_[FS]S_SCL_LCNT + 1 >= IC_CLK * (tLOW + tf)
268 *
269 * DW I2C core starts counting the SCL CNTs for the LOW period
270 * of the SCL clock (tLOW) as soon as it pulls the SCL line.
271 * In order to meet the tLOW timing spec, we need to take into
272 * account the fall time of SCL signal (tf). Default tf value
273 * should be 0.3 us, for safety.
274 */
275 return ((ic_clk * (tLOW + tf) + 5000) / 10000) - 1 + offset;
276}
277
179/** 278/**
180 * i2c_dw_init() - initialize the designware i2c master hardware 279 * i2c_dw_init() - initialize the designware i2c master hardware
181 * @dev: device private data 280 * @dev: device private data
@@ -187,25 +286,49 @@ struct dw_i2c_dev {
187static void i2c_dw_init(struct dw_i2c_dev *dev) 286static void i2c_dw_init(struct dw_i2c_dev *dev)
188{ 287{
189 u32 input_clock_khz = clk_get_rate(dev->clk) / 1000; 288 u32 input_clock_khz = clk_get_rate(dev->clk) / 1000;
190 u16 ic_con; 289 u32 ic_con, hcnt, lcnt;
191 290
192 /* Disable the adapter */ 291 /* Disable the adapter */
193 writeb(0, dev->base + DW_IC_ENABLE); 292 writel(0, dev->base + DW_IC_ENABLE);
194 293
195 /* set standard and fast speed deviders for high/low periods */ 294 /* set standard and fast speed deviders for high/low periods */
196 writew((input_clock_khz * 40 / 10000)+1, /* std speed high, 4us */ 295
197 dev->base + DW_IC_SS_SCL_HCNT); 296 /* Standard-mode */
198 writew((input_clock_khz * 47 / 10000)+1, /* std speed low, 4.7us */ 297 hcnt = i2c_dw_scl_hcnt(input_clock_khz,
199 dev->base + DW_IC_SS_SCL_LCNT); 298 40, /* tHD;STA = tHIGH = 4.0 us */
200 writew((input_clock_khz * 6 / 10000)+1, /* fast speed high, 0.6us */ 299 3, /* tf = 0.3 us */
201 dev->base + DW_IC_FS_SCL_HCNT); 300 0, /* 0: DW default, 1: Ideal */
202 writew((input_clock_khz * 13 / 10000)+1, /* fast speed low, 1.3us */ 301 0); /* No offset */
203 dev->base + DW_IC_FS_SCL_LCNT); 302 lcnt = i2c_dw_scl_lcnt(input_clock_khz,
303 47, /* tLOW = 4.7 us */
304 3, /* tf = 0.3 us */
305 0); /* No offset */
306 writel(hcnt, dev->base + DW_IC_SS_SCL_HCNT);
307 writel(lcnt, dev->base + DW_IC_SS_SCL_LCNT);
308 dev_dbg(dev->dev, "Standard-mode HCNT:LCNT = %d:%d\n", hcnt, lcnt);
309
310 /* Fast-mode */
311 hcnt = i2c_dw_scl_hcnt(input_clock_khz,
312 6, /* tHD;STA = tHIGH = 0.6 us */
313 3, /* tf = 0.3 us */
314 0, /* 0: DW default, 1: Ideal */
315 0); /* No offset */
316 lcnt = i2c_dw_scl_lcnt(input_clock_khz,
317 13, /* tLOW = 1.3 us */
318 3, /* tf = 0.3 us */
319 0); /* No offset */
320 writel(hcnt, dev->base + DW_IC_FS_SCL_HCNT);
321 writel(lcnt, dev->base + DW_IC_FS_SCL_LCNT);
322 dev_dbg(dev->dev, "Fast-mode HCNT:LCNT = %d:%d\n", hcnt, lcnt);
323
324 /* Configure Tx/Rx FIFO threshold levels */
325 writel(dev->tx_fifo_depth - 1, dev->base + DW_IC_TX_TL);
326 writel(0, dev->base + DW_IC_RX_TL);
204 327
205 /* configure the i2c master */ 328 /* configure the i2c master */
206 ic_con = DW_IC_CON_MASTER | DW_IC_CON_SLAVE_DISABLE | 329 ic_con = DW_IC_CON_MASTER | DW_IC_CON_SLAVE_DISABLE |
207 DW_IC_CON_RESTART_EN | DW_IC_CON_SPEED_FAST; 330 DW_IC_CON_RESTART_EN | DW_IC_CON_SPEED_FAST;
208 writew(ic_con, dev->base + DW_IC_CON); 331 writel(ic_con, dev->base + DW_IC_CON);
209} 332}
210 333
211/* 334/*
@@ -215,7 +338,7 @@ static int i2c_dw_wait_bus_not_busy(struct dw_i2c_dev *dev)
215{ 338{
216 int timeout = TIMEOUT; 339 int timeout = TIMEOUT;
217 340
218 while (readb(dev->base + DW_IC_STATUS) & DW_IC_STATUS_ACTIVITY) { 341 while (readl(dev->base + DW_IC_STATUS) & DW_IC_STATUS_ACTIVITY) {
219 if (timeout <= 0) { 342 if (timeout <= 0) {
220 dev_warn(dev->dev, "timeout waiting for bus ready\n"); 343 dev_warn(dev->dev, "timeout waiting for bus ready\n");
221 return -ETIMEDOUT; 344 return -ETIMEDOUT;
@@ -227,106 +350,125 @@ static int i2c_dw_wait_bus_not_busy(struct dw_i2c_dev *dev)
227 return 0; 350 return 0;
228} 351}
229 352
353static void i2c_dw_xfer_init(struct dw_i2c_dev *dev)
354{
355 struct i2c_msg *msgs = dev->msgs;
356 u32 ic_con;
357
358 /* Disable the adapter */
359 writel(0, dev->base + DW_IC_ENABLE);
360
361 /* set the slave (target) address */
362 writel(msgs[dev->msg_write_idx].addr, dev->base + DW_IC_TAR);
363
364 /* if the slave address is ten bit address, enable 10BITADDR */
365 ic_con = readl(dev->base + DW_IC_CON);
366 if (msgs[dev->msg_write_idx].flags & I2C_M_TEN)
367 ic_con |= DW_IC_CON_10BITADDR_MASTER;
368 else
369 ic_con &= ~DW_IC_CON_10BITADDR_MASTER;
370 writel(ic_con, dev->base + DW_IC_CON);
371
372 /* Enable the adapter */
373 writel(1, dev->base + DW_IC_ENABLE);
374
375 /* Enable interrupts */
376 writel(DW_IC_INTR_DEFAULT_MASK, dev->base + DW_IC_INTR_MASK);
377}
378
230/* 379/*
231 * Initiate low level master read/write transaction. 380 * Initiate (and continue) low level master read/write transaction.
232 * This function is called from i2c_dw_xfer when starting a transfer. 381 * This function is only called from i2c_dw_isr, and pumping i2c_msg
233 * This function is also called from dw_i2c_pump_msg to continue a transfer 382 * messages into the tx buffer. Even if the size of i2c_msg data is
234 * that is longer than the size of the TX FIFO. 383 * longer than the size of the tx buffer, it handles everything.
235 */ 384 */
236static void 385static void
237i2c_dw_xfer_msg(struct i2c_adapter *adap) 386i2c_dw_xfer_msg(struct dw_i2c_dev *dev)
238{ 387{
239 struct dw_i2c_dev *dev = i2c_get_adapdata(adap);
240 struct i2c_msg *msgs = dev->msgs; 388 struct i2c_msg *msgs = dev->msgs;
241 int num = dev->msgs_num; 389 u32 intr_mask;
242 u16 ic_con, intr_mask; 390 int tx_limit, rx_limit;
243 int tx_limit = dev->tx_fifo_depth - readb(dev->base + DW_IC_TXFLR); 391 u32 addr = msgs[dev->msg_write_idx].addr;
244 int rx_limit = dev->rx_fifo_depth - readb(dev->base + DW_IC_RXFLR); 392 u32 buf_len = dev->tx_buf_len;
245 u16 addr = msgs[dev->msg_write_idx].addr; 393 u8 *buf = dev->tx_buf;;
246 u16 buf_len = dev->tx_buf_len;
247
248 if (!(dev->status & STATUS_WRITE_IN_PROGRESS)) {
249 /* Disable the adapter */
250 writeb(0, dev->base + DW_IC_ENABLE);
251
252 /* set the slave (target) address */
253 writew(msgs[dev->msg_write_idx].addr, dev->base + DW_IC_TAR);
254 394
255 /* if the slave address is ten bit address, enable 10BITADDR */ 395 intr_mask = DW_IC_INTR_DEFAULT_MASK;
256 ic_con = readw(dev->base + DW_IC_CON);
257 if (msgs[dev->msg_write_idx].flags & I2C_M_TEN)
258 ic_con |= DW_IC_CON_10BITADDR_MASTER;
259 else
260 ic_con &= ~DW_IC_CON_10BITADDR_MASTER;
261 writew(ic_con, dev->base + DW_IC_CON);
262 396
263 /* Enable the adapter */ 397 for (; dev->msg_write_idx < dev->msgs_num; dev->msg_write_idx++) {
264 writeb(1, dev->base + DW_IC_ENABLE); 398 /*
265 } 399 * if target address has changed, we need to
266
267 for (; dev->msg_write_idx < num; dev->msg_write_idx++) {
268 /* if target address has changed, we need to
269 * reprogram the target address in the i2c 400 * reprogram the target address in the i2c
270 * adapter when we are done with this transfer 401 * adapter when we are done with this transfer
271 */ 402 */
272 if (msgs[dev->msg_write_idx].addr != addr) 403 if (msgs[dev->msg_write_idx].addr != addr) {
273 return; 404 dev_err(dev->dev,
405 "%s: invalid target address\n", __func__);
406 dev->msg_err = -EINVAL;
407 break;
408 }
274 409
275 if (msgs[dev->msg_write_idx].len == 0) { 410 if (msgs[dev->msg_write_idx].len == 0) {
276 dev_err(dev->dev, 411 dev_err(dev->dev,
277 "%s: invalid message length\n", __func__); 412 "%s: invalid message length\n", __func__);
278 dev->msg_err = -EINVAL; 413 dev->msg_err = -EINVAL;
279 return; 414 break;
280 } 415 }
281 416
282 if (!(dev->status & STATUS_WRITE_IN_PROGRESS)) { 417 if (!(dev->status & STATUS_WRITE_IN_PROGRESS)) {
283 /* new i2c_msg */ 418 /* new i2c_msg */
284 dev->tx_buf = msgs[dev->msg_write_idx].buf; 419 buf = msgs[dev->msg_write_idx].buf;
285 buf_len = msgs[dev->msg_write_idx].len; 420 buf_len = msgs[dev->msg_write_idx].len;
286 } 421 }
287 422
423 tx_limit = dev->tx_fifo_depth - readl(dev->base + DW_IC_TXFLR);
424 rx_limit = dev->rx_fifo_depth - readl(dev->base + DW_IC_RXFLR);
425
288 while (buf_len > 0 && tx_limit > 0 && rx_limit > 0) { 426 while (buf_len > 0 && tx_limit > 0 && rx_limit > 0) {
289 if (msgs[dev->msg_write_idx].flags & I2C_M_RD) { 427 if (msgs[dev->msg_write_idx].flags & I2C_M_RD) {
290 writew(0x100, dev->base + DW_IC_DATA_CMD); 428 writel(0x100, dev->base + DW_IC_DATA_CMD);
291 rx_limit--; 429 rx_limit--;
292 } else 430 } else
293 writew(*(dev->tx_buf++), 431 writel(*buf++, dev->base + DW_IC_DATA_CMD);
294 dev->base + DW_IC_DATA_CMD);
295 tx_limit--; buf_len--; 432 tx_limit--; buf_len--;
296 } 433 }
434
435 dev->tx_buf = buf;
436 dev->tx_buf_len = buf_len;
437
438 if (buf_len > 0) {
439 /* more bytes to be written */
440 dev->status |= STATUS_WRITE_IN_PROGRESS;
441 break;
442 } else
443 dev->status &= ~STATUS_WRITE_IN_PROGRESS;
297 } 444 }
298 445
299 intr_mask = DW_IC_INTR_STOP_DET | DW_IC_INTR_TX_ABRT; 446 /*
300 if (buf_len > 0) { /* more bytes to be written */ 447 * If i2c_msg index search is completed, we don't need TX_EMPTY
301 intr_mask |= DW_IC_INTR_TX_EMPTY; 448 * interrupt any more.
302 dev->status |= STATUS_WRITE_IN_PROGRESS; 449 */
303 } else 450 if (dev->msg_write_idx == dev->msgs_num)
304 dev->status &= ~STATUS_WRITE_IN_PROGRESS; 451 intr_mask &= ~DW_IC_INTR_TX_EMPTY;
305 writew(intr_mask, dev->base + DW_IC_INTR_MASK); 452
453 if (dev->msg_err)
454 intr_mask = 0;
306 455
307 dev->tx_buf_len = buf_len; 456 writel(intr_mask, dev->base + DW_IC_INTR_MASK);
308} 457}
309 458
310static void 459static void
311i2c_dw_read(struct i2c_adapter *adap) 460i2c_dw_read(struct dw_i2c_dev *dev)
312{ 461{
313 struct dw_i2c_dev *dev = i2c_get_adapdata(adap);
314 struct i2c_msg *msgs = dev->msgs; 462 struct i2c_msg *msgs = dev->msgs;
315 int num = dev->msgs_num; 463 int rx_valid;
316 u16 addr = msgs[dev->msg_read_idx].addr;
317 int rx_valid = readw(dev->base + DW_IC_RXFLR);
318 464
319 for (; dev->msg_read_idx < num; dev->msg_read_idx++) { 465 for (; dev->msg_read_idx < dev->msgs_num; dev->msg_read_idx++) {
320 u16 len; 466 u32 len;
321 u8 *buf; 467 u8 *buf;
322 468
323 if (!(msgs[dev->msg_read_idx].flags & I2C_M_RD)) 469 if (!(msgs[dev->msg_read_idx].flags & I2C_M_RD))
324 continue; 470 continue;
325 471
326 /* different i2c client, reprogram the i2c adapter */
327 if (msgs[dev->msg_read_idx].addr != addr)
328 return;
329
330 if (!(dev->status & STATUS_READ_IN_PROGRESS)) { 472 if (!(dev->status & STATUS_READ_IN_PROGRESS)) {
331 len = msgs[dev->msg_read_idx].len; 473 len = msgs[dev->msg_read_idx].len;
332 buf = msgs[dev->msg_read_idx].buf; 474 buf = msgs[dev->msg_read_idx].buf;
@@ -335,8 +477,10 @@ i2c_dw_read(struct i2c_adapter *adap)
335 buf = dev->rx_buf; 477 buf = dev->rx_buf;
336 } 478 }
337 479
480 rx_valid = readl(dev->base + DW_IC_RXFLR);
481
338 for (; len > 0 && rx_valid > 0; len--, rx_valid--) 482 for (; len > 0 && rx_valid > 0; len--, rx_valid--)
339 *buf++ = readb(dev->base + DW_IC_DATA_CMD); 483 *buf++ = readl(dev->base + DW_IC_DATA_CMD);
340 484
341 if (len > 0) { 485 if (len > 0) {
342 dev->status |= STATUS_READ_IN_PROGRESS; 486 dev->status |= STATUS_READ_IN_PROGRESS;
@@ -348,6 +492,29 @@ i2c_dw_read(struct i2c_adapter *adap)
348 } 492 }
349} 493}
350 494
495static int i2c_dw_handle_tx_abort(struct dw_i2c_dev *dev)
496{
497 unsigned long abort_source = dev->abort_source;
498 int i;
499
500 if (abort_source & DW_IC_TX_ABRT_NOACK) {
501 for_each_set_bit(i, &abort_source, ARRAY_SIZE(abort_sources))
502 dev_dbg(dev->dev,
503 "%s: %s\n", __func__, abort_sources[i]);
504 return -EREMOTEIO;
505 }
506
507 for_each_set_bit(i, &abort_source, ARRAY_SIZE(abort_sources))
508 dev_err(dev->dev, "%s: %s\n", __func__, abort_sources[i]);
509
510 if (abort_source & DW_IC_TX_ARB_LOST)
511 return -EAGAIN;
512 else if (abort_source & DW_IC_TX_ABRT_GCALL_READ)
513 return -EINVAL; /* wrong msgs[] data */
514 else
515 return -EIO;
516}
517
351/* 518/*
352 * Prepare controller for a transaction and call i2c_dw_xfer_msg 519 * Prepare controller for a transaction and call i2c_dw_xfer_msg
353 */ 520 */
@@ -369,13 +536,14 @@ i2c_dw_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], int num)
369 dev->msg_read_idx = 0; 536 dev->msg_read_idx = 0;
370 dev->msg_err = 0; 537 dev->msg_err = 0;
371 dev->status = STATUS_IDLE; 538 dev->status = STATUS_IDLE;
539 dev->abort_source = 0;
372 540
373 ret = i2c_dw_wait_bus_not_busy(dev); 541 ret = i2c_dw_wait_bus_not_busy(dev);
374 if (ret < 0) 542 if (ret < 0)
375 goto done; 543 goto done;
376 544
377 /* start the transfers */ 545 /* start the transfers */
378 i2c_dw_xfer_msg(adap); 546 i2c_dw_xfer_init(dev);
379 547
380 /* wait for tx to complete */ 548 /* wait for tx to complete */
381 ret = wait_for_completion_interruptible_timeout(&dev->cmd_complete, HZ); 549 ret = wait_for_completion_interruptible_timeout(&dev->cmd_complete, HZ);
@@ -394,23 +562,16 @@ i2c_dw_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], int num)
394 562
395 /* no error */ 563 /* no error */
396 if (likely(!dev->cmd_err)) { 564 if (likely(!dev->cmd_err)) {
397 /* read rx fifo, and disable the adapter */ 565 /* Disable the adapter */
398 do { 566 writel(0, dev->base + DW_IC_ENABLE);
399 i2c_dw_read(adap);
400 } while (dev->status & STATUS_READ_IN_PROGRESS);
401 writeb(0, dev->base + DW_IC_ENABLE);
402 ret = num; 567 ret = num;
403 goto done; 568 goto done;
404 } 569 }
405 570
406 /* We have an error */ 571 /* We have an error */
407 if (dev->cmd_err == DW_IC_ERR_TX_ABRT) { 572 if (dev->cmd_err == DW_IC_ERR_TX_ABRT) {
408 unsigned long abort_source = dev->abort_source; 573 ret = i2c_dw_handle_tx_abort(dev);
409 int i; 574 goto done;
410
411 for_each_bit(i, &abort_source, ARRAY_SIZE(abort_sources)) {
412 dev_err(dev->dev, "%s: %s\n", __func__, abort_sources[i]);
413 }
414 } 575 }
415 ret = -EIO; 576 ret = -EIO;
416 577
@@ -422,21 +583,67 @@ done:
422 583
423static u32 i2c_dw_func(struct i2c_adapter *adap) 584static u32 i2c_dw_func(struct i2c_adapter *adap)
424{ 585{
425 return I2C_FUNC_I2C | I2C_FUNC_10BIT_ADDR; 586 return I2C_FUNC_I2C |
587 I2C_FUNC_10BIT_ADDR |
588 I2C_FUNC_SMBUS_BYTE |
589 I2C_FUNC_SMBUS_BYTE_DATA |
590 I2C_FUNC_SMBUS_WORD_DATA |
591 I2C_FUNC_SMBUS_I2C_BLOCK;
426} 592}
427 593
428static void dw_i2c_pump_msg(unsigned long data) 594static u32 i2c_dw_read_clear_intrbits(struct dw_i2c_dev *dev)
429{ 595{
430 struct dw_i2c_dev *dev = (struct dw_i2c_dev *) data; 596 u32 stat;
431 u16 intr_mask; 597
432 598 /*
433 i2c_dw_read(&dev->adapter); 599 * The IC_INTR_STAT register just indicates "enabled" interrupts.
434 i2c_dw_xfer_msg(&dev->adapter); 600 * Ths unmasked raw version of interrupt status bits are available
435 601 * in the IC_RAW_INTR_STAT register.
436 intr_mask = DW_IC_INTR_STOP_DET | DW_IC_INTR_TX_ABRT; 602 *
437 if (dev->status & STATUS_WRITE_IN_PROGRESS) 603 * That is,
438 intr_mask |= DW_IC_INTR_TX_EMPTY; 604 * stat = readl(IC_INTR_STAT);
439 writew(intr_mask, dev->base + DW_IC_INTR_MASK); 605 * equals to,
606 * stat = readl(IC_RAW_INTR_STAT) & readl(IC_INTR_MASK);
607 *
608 * The raw version might be useful for debugging purposes.
609 */
610 stat = readl(dev->base + DW_IC_INTR_STAT);
611
612 /*
613 * Do not use the IC_CLR_INTR register to clear interrupts, or
614 * you'll miss some interrupts, triggered during the period from
615 * readl(IC_INTR_STAT) to readl(IC_CLR_INTR).
616 *
617 * Instead, use the separately-prepared IC_CLR_* registers.
618 */
619 if (stat & DW_IC_INTR_RX_UNDER)
620 readl(dev->base + DW_IC_CLR_RX_UNDER);
621 if (stat & DW_IC_INTR_RX_OVER)
622 readl(dev->base + DW_IC_CLR_RX_OVER);
623 if (stat & DW_IC_INTR_TX_OVER)
624 readl(dev->base + DW_IC_CLR_TX_OVER);
625 if (stat & DW_IC_INTR_RD_REQ)
626 readl(dev->base + DW_IC_CLR_RD_REQ);
627 if (stat & DW_IC_INTR_TX_ABRT) {
628 /*
629 * The IC_TX_ABRT_SOURCE register is cleared whenever
630 * the IC_CLR_TX_ABRT is read. Preserve it beforehand.
631 */
632 dev->abort_source = readl(dev->base + DW_IC_TX_ABRT_SOURCE);
633 readl(dev->base + DW_IC_CLR_TX_ABRT);
634 }
635 if (stat & DW_IC_INTR_RX_DONE)
636 readl(dev->base + DW_IC_CLR_RX_DONE);
637 if (stat & DW_IC_INTR_ACTIVITY)
638 readl(dev->base + DW_IC_CLR_ACTIVITY);
639 if (stat & DW_IC_INTR_STOP_DET)
640 readl(dev->base + DW_IC_CLR_STOP_DET);
641 if (stat & DW_IC_INTR_START_DET)
642 readl(dev->base + DW_IC_CLR_START_DET);
643 if (stat & DW_IC_INTR_GEN_CALL)
644 readl(dev->base + DW_IC_CLR_GEN_CALL);
645
646 return stat;
440} 647}
441 648
442/* 649/*
@@ -446,20 +653,37 @@ static void dw_i2c_pump_msg(unsigned long data)
446static irqreturn_t i2c_dw_isr(int this_irq, void *dev_id) 653static irqreturn_t i2c_dw_isr(int this_irq, void *dev_id)
447{ 654{
448 struct dw_i2c_dev *dev = dev_id; 655 struct dw_i2c_dev *dev = dev_id;
449 u16 stat; 656 u32 stat;
450 657
451 stat = readw(dev->base + DW_IC_INTR_STAT); 658 stat = i2c_dw_read_clear_intrbits(dev);
452 dev_dbg(dev->dev, "%s: stat=0x%x\n", __func__, stat); 659 dev_dbg(dev->dev, "%s: stat=0x%x\n", __func__, stat);
660
453 if (stat & DW_IC_INTR_TX_ABRT) { 661 if (stat & DW_IC_INTR_TX_ABRT) {
454 dev->abort_source = readw(dev->base + DW_IC_TX_ABRT_SOURCE);
455 dev->cmd_err |= DW_IC_ERR_TX_ABRT; 662 dev->cmd_err |= DW_IC_ERR_TX_ABRT;
456 dev->status = STATUS_IDLE; 663 dev->status = STATUS_IDLE;
457 } else if (stat & DW_IC_INTR_TX_EMPTY)
458 tasklet_schedule(&dev->pump_msg);
459 664
460 readb(dev->base + DW_IC_CLR_INTR); /* clear interrupts */ 665 /*
461 writew(0, dev->base + DW_IC_INTR_MASK); /* disable interrupts */ 666 * Anytime TX_ABRT is set, the contents of the tx/rx
462 if (stat & (DW_IC_INTR_TX_ABRT | DW_IC_INTR_STOP_DET)) 667 * buffers are flushed. Make sure to skip them.
668 */
669 writel(0, dev->base + DW_IC_INTR_MASK);
670 goto tx_aborted;
671 }
672
673 if (stat & DW_IC_INTR_RX_FULL)
674 i2c_dw_read(dev);
675
676 if (stat & DW_IC_INTR_TX_EMPTY)
677 i2c_dw_xfer_msg(dev);
678
679 /*
680 * No need to modify or disable the interrupt mask here.
681 * i2c_dw_xfer_msg() will take care of it according to
682 * the current transmit status.
683 */
684
685tx_aborted:
686 if ((stat & (DW_IC_INTR_TX_ABRT | DW_IC_INTR_STOP_DET)) || dev->msg_err)
463 complete(&dev->cmd_complete); 687 complete(&dev->cmd_complete);
464 688
465 return IRQ_HANDLED; 689 return IRQ_HANDLED;
@@ -474,8 +698,8 @@ static int __devinit dw_i2c_probe(struct platform_device *pdev)
474{ 698{
475 struct dw_i2c_dev *dev; 699 struct dw_i2c_dev *dev;
476 struct i2c_adapter *adap; 700 struct i2c_adapter *adap;
477 struct resource *mem, *irq, *ioarea; 701 struct resource *mem, *ioarea;
478 int r; 702 int irq, r;
479 703
480 /* NOTE: driver uses the static register mapping */ 704 /* NOTE: driver uses the static register mapping */
481 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0); 705 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
@@ -484,10 +708,10 @@ static int __devinit dw_i2c_probe(struct platform_device *pdev)
484 return -EINVAL; 708 return -EINVAL;
485 } 709 }
486 710
487 irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0); 711 irq = platform_get_irq(pdev, 0);
488 if (!irq) { 712 if (irq < 0) {
489 dev_err(&pdev->dev, "no irq resource?\n"); 713 dev_err(&pdev->dev, "no irq resource?\n");
490 return -EINVAL; 714 return irq; /* -ENXIO */
491 } 715 }
492 716
493 ioarea = request_mem_region(mem->start, resource_size(mem), 717 ioarea = request_mem_region(mem->start, resource_size(mem),
@@ -504,10 +728,9 @@ static int __devinit dw_i2c_probe(struct platform_device *pdev)
504 } 728 }
505 729
506 init_completion(&dev->cmd_complete); 730 init_completion(&dev->cmd_complete);
507 tasklet_init(&dev->pump_msg, dw_i2c_pump_msg, (unsigned long) dev);
508 mutex_init(&dev->lock); 731 mutex_init(&dev->lock);
509 dev->dev = get_device(&pdev->dev); 732 dev->dev = get_device(&pdev->dev);
510 dev->irq = irq->start; 733 dev->irq = irq;
511 platform_set_drvdata(pdev, dev); 734 platform_set_drvdata(pdev, dev);
512 735
513 dev->clk = clk_get(&pdev->dev, NULL); 736 dev->clk = clk_get(&pdev->dev, NULL);
@@ -531,8 +754,8 @@ static int __devinit dw_i2c_probe(struct platform_device *pdev)
531 } 754 }
532 i2c_dw_init(dev); 755 i2c_dw_init(dev);
533 756
534 writew(0, dev->base + DW_IC_INTR_MASK); /* disable IRQ */ 757 writel(0, dev->base + DW_IC_INTR_MASK); /* disable IRQ */
535 r = request_irq(dev->irq, i2c_dw_isr, 0, pdev->name, dev); 758 r = request_irq(dev->irq, i2c_dw_isr, IRQF_DISABLED, pdev->name, dev);
536 if (r) { 759 if (r) {
537 dev_err(&pdev->dev, "failure requesting irq %i\n", dev->irq); 760 dev_err(&pdev->dev, "failure requesting irq %i\n", dev->irq);
538 goto err_iounmap; 761 goto err_iounmap;
@@ -587,7 +810,7 @@ static int __devexit dw_i2c_remove(struct platform_device *pdev)
587 clk_put(dev->clk); 810 clk_put(dev->clk);
588 dev->clk = NULL; 811 dev->clk = NULL;
589 812
590 writeb(0, dev->base + DW_IC_ENABLE); 813 writel(0, dev->base + DW_IC_ENABLE);
591 free_irq(dev->irq, dev); 814 free_irq(dev->irq, dev);
592 kfree(dev); 815 kfree(dev);
593 816