blob: b9f18ddaaa7654a49fd09c9ff0ecfaf060f8f764 [file] [log] [blame]
Baruch Siach1ab52cf2009-06-22 16:36:29 +03001/*
Shinya Kuribayashia0e06ea2009-11-06 21:52:22 +09002 * Synopsys DesignWare I2C adapter driver (master only).
Baruch Siach1ab52cf2009-06-22 16:36:29 +03003 *
4 * Based on the TI DAVINCI I2C adapter driver.
5 *
6 * Copyright (C) 2006 Texas Instruments.
7 * Copyright (C) 2007 MontaVista Software Inc.
8 * Copyright (C) 2009 Provigent Ltd.
9 *
10 * ----------------------------------------------------------------------------
11 *
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.
16 *
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.
21 *
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 * ----------------------------------------------------------------------------
26 *
27 */
Baruch Siach1ab52cf2009-06-22 16:36:29 +030028#include <linux/clk.h>
29#include <linux/errno.h>
Baruch Siach1ab52cf2009-06-22 16:36:29 +030030#include <linux/err.h>
Dirk Brandewie2373f6b2011-10-29 10:57:23 +010031#include <linux/i2c.h>
Baruch Siach1ab52cf2009-06-22 16:36:29 +030032#include <linux/interrupt.h>
Baruch Siach1ab52cf2009-06-22 16:36:29 +030033#include <linux/io.h>
Dirk Brandewie2373f6b2011-10-29 10:57:23 +010034#include <linux/delay.h>
35#include "i2c-designware-core.h"
Shinya Kuribayashice6eb572009-11-06 21:51:57 +090036
Dirk Brandewief3fa9f32011-10-06 11:26:34 -070037/*
38 * Registers offset
39 */
40#define DW_IC_CON 0x0
41#define DW_IC_TAR 0x4
42#define DW_IC_DATA_CMD 0x10
43#define DW_IC_SS_SCL_HCNT 0x14
44#define DW_IC_SS_SCL_LCNT 0x18
45#define DW_IC_FS_SCL_HCNT 0x1c
46#define DW_IC_FS_SCL_LCNT 0x20
47#define DW_IC_INTR_STAT 0x2c
48#define DW_IC_INTR_MASK 0x30
49#define DW_IC_RAW_INTR_STAT 0x34
50#define DW_IC_RX_TL 0x38
51#define DW_IC_TX_TL 0x3c
52#define DW_IC_CLR_INTR 0x40
53#define DW_IC_CLR_RX_UNDER 0x44
54#define DW_IC_CLR_RX_OVER 0x48
55#define DW_IC_CLR_TX_OVER 0x4c
56#define DW_IC_CLR_RD_REQ 0x50
57#define DW_IC_CLR_TX_ABRT 0x54
58#define DW_IC_CLR_RX_DONE 0x58
59#define DW_IC_CLR_ACTIVITY 0x5c
60#define DW_IC_CLR_STOP_DET 0x60
61#define DW_IC_CLR_START_DET 0x64
62#define DW_IC_CLR_GEN_CALL 0x68
63#define DW_IC_ENABLE 0x6c
64#define DW_IC_STATUS 0x70
65#define DW_IC_TXFLR 0x74
66#define DW_IC_RXFLR 0x78
67#define DW_IC_TX_ABRT_SOURCE 0x80
68#define DW_IC_COMP_PARAM_1 0xf4
69#define DW_IC_COMP_TYPE 0xfc
70#define DW_IC_COMP_TYPE_VALUE 0x44570140
71
72#define DW_IC_INTR_RX_UNDER 0x001
73#define DW_IC_INTR_RX_OVER 0x002
74#define DW_IC_INTR_RX_FULL 0x004
75#define DW_IC_INTR_TX_OVER 0x008
76#define DW_IC_INTR_TX_EMPTY 0x010
77#define DW_IC_INTR_RD_REQ 0x020
78#define DW_IC_INTR_TX_ABRT 0x040
79#define DW_IC_INTR_RX_DONE 0x080
80#define DW_IC_INTR_ACTIVITY 0x100
81#define DW_IC_INTR_STOP_DET 0x200
82#define DW_IC_INTR_START_DET 0x400
83#define DW_IC_INTR_GEN_CALL 0x800
84
85#define DW_IC_INTR_DEFAULT_MASK (DW_IC_INTR_RX_FULL | \
86 DW_IC_INTR_TX_EMPTY | \
87 DW_IC_INTR_TX_ABRT | \
88 DW_IC_INTR_STOP_DET)
89
90#define DW_IC_STATUS_ACTIVITY 0x1
91
92#define DW_IC_ERR_TX_ABRT 0x1
93
94/*
95 * status codes
96 */
97#define STATUS_IDLE 0x0
98#define STATUS_WRITE_IN_PROGRESS 0x1
99#define STATUS_READ_IN_PROGRESS 0x2
100
101#define TIMEOUT 20 /* ms */
102
103/*
104 * hardware abort codes from the DW_IC_TX_ABRT_SOURCE register
105 *
106 * only expected abort codes are listed here
107 * refer to the datasheet for the full list
108 */
109#define ABRT_7B_ADDR_NOACK 0
110#define ABRT_10ADDR1_NOACK 1
111#define ABRT_10ADDR2_NOACK 2
112#define ABRT_TXDATA_NOACK 3
113#define ABRT_GCALL_NOACK 4
114#define ABRT_GCALL_READ 5
115#define ABRT_SBYTE_ACKDET 7
116#define ABRT_SBYTE_NORSTRT 9
117#define ABRT_10B_RD_NORSTRT 10
118#define ABRT_MASTER_DIS 11
119#define ARB_LOST 12
120
121#define DW_IC_TX_ABRT_7B_ADDR_NOACK (1UL << ABRT_7B_ADDR_NOACK)
122#define DW_IC_TX_ABRT_10ADDR1_NOACK (1UL << ABRT_10ADDR1_NOACK)
123#define DW_IC_TX_ABRT_10ADDR2_NOACK (1UL << ABRT_10ADDR2_NOACK)
124#define DW_IC_TX_ABRT_TXDATA_NOACK (1UL << ABRT_TXDATA_NOACK)
125#define DW_IC_TX_ABRT_GCALL_NOACK (1UL << ABRT_GCALL_NOACK)
126#define DW_IC_TX_ABRT_GCALL_READ (1UL << ABRT_GCALL_READ)
127#define DW_IC_TX_ABRT_SBYTE_ACKDET (1UL << ABRT_SBYTE_ACKDET)
128#define DW_IC_TX_ABRT_SBYTE_NORSTRT (1UL << ABRT_SBYTE_NORSTRT)
129#define DW_IC_TX_ABRT_10B_RD_NORSTRT (1UL << ABRT_10B_RD_NORSTRT)
130#define DW_IC_TX_ABRT_MASTER_DIS (1UL << ABRT_MASTER_DIS)
131#define DW_IC_TX_ARB_LOST (1UL << ARB_LOST)
132
133#define DW_IC_TX_ABRT_NOACK (DW_IC_TX_ABRT_7B_ADDR_NOACK | \
134 DW_IC_TX_ABRT_10ADDR1_NOACK | \
135 DW_IC_TX_ABRT_10ADDR2_NOACK | \
136 DW_IC_TX_ABRT_TXDATA_NOACK | \
137 DW_IC_TX_ABRT_GCALL_NOACK)
138
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300139static char *abort_sources[] = {
Shinya Kuribayashia0e06ea2009-11-06 21:52:22 +0900140 [ABRT_7B_ADDR_NOACK] =
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300141 "slave address not acknowledged (7bit mode)",
Shinya Kuribayashia0e06ea2009-11-06 21:52:22 +0900142 [ABRT_10ADDR1_NOACK] =
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300143 "first address byte not acknowledged (10bit mode)",
Shinya Kuribayashia0e06ea2009-11-06 21:52:22 +0900144 [ABRT_10ADDR2_NOACK] =
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300145 "second address byte not acknowledged (10bit mode)",
Shinya Kuribayashia0e06ea2009-11-06 21:52:22 +0900146 [ABRT_TXDATA_NOACK] =
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300147 "data not acknowledged",
Shinya Kuribayashia0e06ea2009-11-06 21:52:22 +0900148 [ABRT_GCALL_NOACK] =
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300149 "no acknowledgement for a general call",
Shinya Kuribayashia0e06ea2009-11-06 21:52:22 +0900150 [ABRT_GCALL_READ] =
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300151 "read after general call",
Shinya Kuribayashia0e06ea2009-11-06 21:52:22 +0900152 [ABRT_SBYTE_ACKDET] =
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300153 "start byte acknowledged",
Shinya Kuribayashia0e06ea2009-11-06 21:52:22 +0900154 [ABRT_SBYTE_NORSTRT] =
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300155 "trying to send start byte when restart is disabled",
Shinya Kuribayashia0e06ea2009-11-06 21:52:22 +0900156 [ABRT_10B_RD_NORSTRT] =
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300157 "trying to read when restart is disabled (10bit mode)",
Shinya Kuribayashia0e06ea2009-11-06 21:52:22 +0900158 [ABRT_MASTER_DIS] =
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300159 "trying to use disabled adapter",
Shinya Kuribayashia0e06ea2009-11-06 21:52:22 +0900160 [ARB_LOST] =
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300161 "lost arbitration",
162};
163
Dirk Brandewie2373f6b2011-10-29 10:57:23 +0100164u32 dw_readl(struct dw_i2c_dev *dev, int offset)
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700165{
Jean-Hugues Deschenes18c40892011-10-06 11:26:27 -0700166 u32 value = readl(dev->base + offset);
167
168 if (dev->swab)
169 return swab32(value);
170 else
171 return value;
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700172}
173
Dirk Brandewie2373f6b2011-10-29 10:57:23 +0100174void dw_writel(struct dw_i2c_dev *dev, u32 b, int offset)
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700175{
Jean-Hugues Deschenes18c40892011-10-06 11:26:27 -0700176 if (dev->swab)
177 b = swab32(b);
178
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700179 writel(b, dev->base + offset);
180}
181
Shinya Kuribayashid60c7e82009-11-06 21:47:01 +0900182static u32
183i2c_dw_scl_hcnt(u32 ic_clk, u32 tSYMBOL, u32 tf, int cond, int offset)
184{
185 /*
186 * DesignWare I2C core doesn't seem to have solid strategy to meet
187 * the tHD;STA timing spec. Configuring _HCNT based on tHIGH spec
188 * will result in violation of the tHD;STA spec.
189 */
190 if (cond)
191 /*
192 * Conditional expression:
193 *
194 * IC_[FS]S_SCL_HCNT + (1+4+3) >= IC_CLK * tHIGH
195 *
196 * This is based on the DW manuals, and represents an ideal
197 * configuration. The resulting I2C bus speed will be
198 * faster than any of the others.
199 *
200 * If your hardware is free from tHD;STA issue, try this one.
201 */
202 return (ic_clk * tSYMBOL + 5000) / 10000 - 8 + offset;
203 else
204 /*
205 * Conditional expression:
206 *
207 * IC_[FS]S_SCL_HCNT + 3 >= IC_CLK * (tHD;STA + tf)
208 *
209 * This is just experimental rule; the tHD;STA period turned
210 * out to be proportinal to (_HCNT + 3). With this setting,
211 * we could meet both tHIGH and tHD;STA timing specs.
212 *
213 * If unsure, you'd better to take this alternative.
214 *
215 * The reason why we need to take into account "tf" here,
216 * is the same as described in i2c_dw_scl_lcnt().
217 */
218 return (ic_clk * (tSYMBOL + tf) + 5000) / 10000 - 3 + offset;
219}
220
221static u32 i2c_dw_scl_lcnt(u32 ic_clk, u32 tLOW, u32 tf, int offset)
222{
223 /*
224 * Conditional expression:
225 *
226 * IC_[FS]S_SCL_LCNT + 1 >= IC_CLK * (tLOW + tf)
227 *
228 * DW I2C core starts counting the SCL CNTs for the LOW period
229 * of the SCL clock (tLOW) as soon as it pulls the SCL line.
230 * In order to meet the tLOW timing spec, we need to take into
231 * account the fall time of SCL signal (tf). Default tf value
232 * should be 0.3 us, for safety.
233 */
234 return ((ic_clk * (tLOW + tf) + 5000) / 10000) - 1 + offset;
235}
236
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300237/**
238 * i2c_dw_init() - initialize the designware i2c master hardware
239 * @dev: device private data
240 *
241 * This functions configures and enables the I2C master.
242 * This function is called during I2C init function, and in case of timeout at
243 * run time.
244 */
Dirk Brandewie2373f6b2011-10-29 10:57:23 +0100245int i2c_dw_init(struct dw_i2c_dev *dev)
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300246{
Dirk Brandewie1d31b582011-10-06 11:26:30 -0700247 u32 input_clock_khz;
Dirk Brandewiee18563f2011-10-06 11:26:32 -0700248 u32 hcnt, lcnt;
Dirk Brandewie4a423a82011-10-06 11:26:28 -0700249 u32 reg;
250
Dirk Brandewie1d31b582011-10-06 11:26:30 -0700251 input_clock_khz = dev->get_clk_rate_khz(dev);
252
Dirk Brandewie4a423a82011-10-06 11:26:28 -0700253 /* Configure register endianess access */
254 reg = dw_readl(dev, DW_IC_COMP_TYPE);
255 if (reg == ___constant_swab32(DW_IC_COMP_TYPE_VALUE)) {
256 dev->swab = 1;
257 reg = DW_IC_COMP_TYPE_VALUE;
258 }
259
260 if (reg != DW_IC_COMP_TYPE_VALUE) {
261 dev_err(dev->dev, "Unknown Synopsys component type: "
262 "0x%08x\n", reg);
263 return -ENODEV;
264 }
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300265
266 /* Disable the adapter */
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700267 dw_writel(dev, 0, DW_IC_ENABLE);
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300268
269 /* set standard and fast speed deviders for high/low periods */
Shinya Kuribayashid60c7e82009-11-06 21:47:01 +0900270
271 /* Standard-mode */
272 hcnt = i2c_dw_scl_hcnt(input_clock_khz,
273 40, /* tHD;STA = tHIGH = 4.0 us */
274 3, /* tf = 0.3 us */
275 0, /* 0: DW default, 1: Ideal */
276 0); /* No offset */
277 lcnt = i2c_dw_scl_lcnt(input_clock_khz,
278 47, /* tLOW = 4.7 us */
279 3, /* tf = 0.3 us */
280 0); /* No offset */
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700281 dw_writel(dev, hcnt, DW_IC_SS_SCL_HCNT);
282 dw_writel(dev, lcnt, DW_IC_SS_SCL_LCNT);
Shinya Kuribayashid60c7e82009-11-06 21:47:01 +0900283 dev_dbg(dev->dev, "Standard-mode HCNT:LCNT = %d:%d\n", hcnt, lcnt);
284
285 /* Fast-mode */
286 hcnt = i2c_dw_scl_hcnt(input_clock_khz,
287 6, /* tHD;STA = tHIGH = 0.6 us */
288 3, /* tf = 0.3 us */
289 0, /* 0: DW default, 1: Ideal */
290 0); /* No offset */
291 lcnt = i2c_dw_scl_lcnt(input_clock_khz,
292 13, /* tLOW = 1.3 us */
293 3, /* tf = 0.3 us */
294 0); /* No offset */
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700295 dw_writel(dev, hcnt, DW_IC_FS_SCL_HCNT);
296 dw_writel(dev, lcnt, DW_IC_FS_SCL_LCNT);
Shinya Kuribayashid60c7e82009-11-06 21:47:01 +0900297 dev_dbg(dev->dev, "Fast-mode HCNT:LCNT = %d:%d\n", hcnt, lcnt);
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300298
Shinya Kuribayashi4cb6d1d2009-11-06 21:48:12 +0900299 /* Configure Tx/Rx FIFO threshold levels */
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700300 dw_writel(dev, dev->tx_fifo_depth - 1, DW_IC_TX_TL);
301 dw_writel(dev, 0, DW_IC_RX_TL);
Shinya Kuribayashi4cb6d1d2009-11-06 21:48:12 +0900302
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300303 /* configure the i2c master */
Dirk Brandewiee18563f2011-10-06 11:26:32 -0700304 dw_writel(dev, dev->master_cfg , DW_IC_CON);
Dirk Brandewie4a423a82011-10-06 11:26:28 -0700305 return 0;
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300306}
307
308/*
309 * Waiting for bus not busy
310 */
311static int i2c_dw_wait_bus_not_busy(struct dw_i2c_dev *dev)
312{
313 int timeout = TIMEOUT;
314
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700315 while (dw_readl(dev, DW_IC_STATUS) & DW_IC_STATUS_ACTIVITY) {
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300316 if (timeout <= 0) {
317 dev_warn(dev->dev, "timeout waiting for bus ready\n");
318 return -ETIMEDOUT;
319 }
320 timeout--;
321 mdelay(1);
322 }
323
324 return 0;
325}
326
Shinya Kuribayashi81e798b2009-11-06 21:48:55 +0900327static void i2c_dw_xfer_init(struct dw_i2c_dev *dev)
328{
329 struct i2c_msg *msgs = dev->msgs;
330 u32 ic_con;
331
332 /* Disable the adapter */
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700333 dw_writel(dev, 0, DW_IC_ENABLE);
Shinya Kuribayashi81e798b2009-11-06 21:48:55 +0900334
335 /* set the slave (target) address */
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700336 dw_writel(dev, msgs[dev->msg_write_idx].addr, DW_IC_TAR);
Shinya Kuribayashi81e798b2009-11-06 21:48:55 +0900337
338 /* if the slave address is ten bit address, enable 10BITADDR */
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700339 ic_con = dw_readl(dev, DW_IC_CON);
Shinya Kuribayashi81e798b2009-11-06 21:48:55 +0900340 if (msgs[dev->msg_write_idx].flags & I2C_M_TEN)
341 ic_con |= DW_IC_CON_10BITADDR_MASTER;
342 else
343 ic_con &= ~DW_IC_CON_10BITADDR_MASTER;
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700344 dw_writel(dev, ic_con, DW_IC_CON);
Shinya Kuribayashi81e798b2009-11-06 21:48:55 +0900345
346 /* Enable the adapter */
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700347 dw_writel(dev, 1, DW_IC_ENABLE);
Shinya Kuribayashi201d6a72009-11-06 21:50:40 +0900348
349 /* Enable interrupts */
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700350 dw_writel(dev, DW_IC_INTR_DEFAULT_MASK, DW_IC_INTR_MASK);
Shinya Kuribayashi81e798b2009-11-06 21:48:55 +0900351}
352
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300353/*
Shinya Kuribayashi201d6a72009-11-06 21:50:40 +0900354 * Initiate (and continue) low level master read/write transaction.
355 * This function is only called from i2c_dw_isr, and pumping i2c_msg
356 * messages into the tx buffer. Even if the size of i2c_msg data is
357 * longer than the size of the tx buffer, it handles everything.
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300358 */
Dirk Brandewie2373f6b2011-10-29 10:57:23 +0100359void
Shinya Kuribayashie77cf232009-11-06 21:46:04 +0900360i2c_dw_xfer_msg(struct dw_i2c_dev *dev)
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300361{
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300362 struct i2c_msg *msgs = dev->msgs;
Shinya Kuribayashi81e798b2009-11-06 21:48:55 +0900363 u32 intr_mask;
Shinya Kuribayashiae722222009-11-06 21:49:39 +0900364 int tx_limit, rx_limit;
Shinya Kuribayashied5e1dd2009-11-06 21:43:52 +0900365 u32 addr = msgs[dev->msg_write_idx].addr;
366 u32 buf_len = dev->tx_buf_len;
Justin P. Mattock69932482011-07-26 23:06:29 -0700367 u8 *buf = dev->tx_buf;
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300368
Shinya Kuribayashi201d6a72009-11-06 21:50:40 +0900369 intr_mask = DW_IC_INTR_DEFAULT_MASK;
Shinya Kuribayashic70c5cd2009-11-06 21:47:30 +0900370
Shinya Kuribayashi6d2ea482009-11-06 21:46:29 +0900371 for (; dev->msg_write_idx < dev->msgs_num; dev->msg_write_idx++) {
Shinya Kuribayashia0e06ea2009-11-06 21:52:22 +0900372 /*
373 * if target address has changed, we need to
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300374 * reprogram the target address in the i2c
375 * adapter when we are done with this transfer
376 */
Shinya Kuribayashi8f588e42009-11-06 21:51:18 +0900377 if (msgs[dev->msg_write_idx].addr != addr) {
378 dev_err(dev->dev,
379 "%s: invalid target address\n", __func__);
380 dev->msg_err = -EINVAL;
381 break;
382 }
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300383
384 if (msgs[dev->msg_write_idx].len == 0) {
385 dev_err(dev->dev,
386 "%s: invalid message length\n", __func__);
387 dev->msg_err = -EINVAL;
Shinya Kuribayashi8f588e42009-11-06 21:51:18 +0900388 break;
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300389 }
390
391 if (!(dev->status & STATUS_WRITE_IN_PROGRESS)) {
392 /* new i2c_msg */
Shinya Kuribayashi26ea15b2009-11-06 21:49:14 +0900393 buf = msgs[dev->msg_write_idx].buf;
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300394 buf_len = msgs[dev->msg_write_idx].len;
395 }
396
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700397 tx_limit = dev->tx_fifo_depth - dw_readl(dev, DW_IC_TXFLR);
398 rx_limit = dev->rx_fifo_depth - dw_readl(dev, DW_IC_RXFLR);
Shinya Kuribayashiae722222009-11-06 21:49:39 +0900399
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300400 while (buf_len > 0 && tx_limit > 0 && rx_limit > 0) {
401 if (msgs[dev->msg_write_idx].flags & I2C_M_RD) {
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700402 dw_writel(dev, 0x100, DW_IC_DATA_CMD);
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300403 rx_limit--;
404 } else
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700405 dw_writel(dev, *buf++, DW_IC_DATA_CMD);
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300406 tx_limit--; buf_len--;
407 }
Shinya Kuribayashic70c5cd2009-11-06 21:47:30 +0900408
Shinya Kuribayashi26ea15b2009-11-06 21:49:14 +0900409 dev->tx_buf = buf;
Shinya Kuribayashic70c5cd2009-11-06 21:47:30 +0900410 dev->tx_buf_len = buf_len;
411
412 if (buf_len > 0) {
413 /* more bytes to be written */
Shinya Kuribayashic70c5cd2009-11-06 21:47:30 +0900414 dev->status |= STATUS_WRITE_IN_PROGRESS;
415 break;
Shinya Kuribayashi69151e52009-11-06 21:51:00 +0900416 } else
Shinya Kuribayashic70c5cd2009-11-06 21:47:30 +0900417 dev->status &= ~STATUS_WRITE_IN_PROGRESS;
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300418 }
419
Shinya Kuribayashi69151e52009-11-06 21:51:00 +0900420 /*
421 * If i2c_msg index search is completed, we don't need TX_EMPTY
422 * interrupt any more.
423 */
424 if (dev->msg_write_idx == dev->msgs_num)
425 intr_mask &= ~DW_IC_INTR_TX_EMPTY;
426
Shinya Kuribayashi8f588e42009-11-06 21:51:18 +0900427 if (dev->msg_err)
428 intr_mask = 0;
429
Dirk Brandewie2373f6b2011-10-29 10:57:23 +0100430 dw_writel(dev, intr_mask, DW_IC_INTR_MASK);
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300431}
432
433static void
Shinya Kuribayashi78839bd2009-11-06 21:45:39 +0900434i2c_dw_read(struct dw_i2c_dev *dev)
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300435{
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300436 struct i2c_msg *msgs = dev->msgs;
Shinya Kuribayashiae722222009-11-06 21:49:39 +0900437 int rx_valid;
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300438
Shinya Kuribayashi6d2ea482009-11-06 21:46:29 +0900439 for (; dev->msg_read_idx < dev->msgs_num; dev->msg_read_idx++) {
Shinya Kuribayashied5e1dd2009-11-06 21:43:52 +0900440 u32 len;
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300441 u8 *buf;
442
443 if (!(msgs[dev->msg_read_idx].flags & I2C_M_RD))
444 continue;
445
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300446 if (!(dev->status & STATUS_READ_IN_PROGRESS)) {
447 len = msgs[dev->msg_read_idx].len;
448 buf = msgs[dev->msg_read_idx].buf;
449 } else {
450 len = dev->rx_buf_len;
451 buf = dev->rx_buf;
452 }
453
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700454 rx_valid = dw_readl(dev, DW_IC_RXFLR);
Shinya Kuribayashiae722222009-11-06 21:49:39 +0900455
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300456 for (; len > 0 && rx_valid > 0; len--, rx_valid--)
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700457 *buf++ = dw_readl(dev, DW_IC_DATA_CMD);
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300458
459 if (len > 0) {
460 dev->status |= STATUS_READ_IN_PROGRESS;
461 dev->rx_buf_len = len;
462 dev->rx_buf = buf;
463 return;
464 } else
465 dev->status &= ~STATUS_READ_IN_PROGRESS;
466 }
467}
468
Shinya Kuribayashice6eb572009-11-06 21:51:57 +0900469static int i2c_dw_handle_tx_abort(struct dw_i2c_dev *dev)
470{
471 unsigned long abort_source = dev->abort_source;
472 int i;
473
Shinya Kuribayashi6d1ea0f2009-11-16 20:40:14 +0900474 if (abort_source & DW_IC_TX_ABRT_NOACK) {
Akinobu Mita984b3f52010-03-05 13:41:37 -0800475 for_each_set_bit(i, &abort_source, ARRAY_SIZE(abort_sources))
Shinya Kuribayashi6d1ea0f2009-11-16 20:40:14 +0900476 dev_dbg(dev->dev,
477 "%s: %s\n", __func__, abort_sources[i]);
478 return -EREMOTEIO;
479 }
480
Akinobu Mita984b3f52010-03-05 13:41:37 -0800481 for_each_set_bit(i, &abort_source, ARRAY_SIZE(abort_sources))
Shinya Kuribayashice6eb572009-11-06 21:51:57 +0900482 dev_err(dev->dev, "%s: %s\n", __func__, abort_sources[i]);
483
484 if (abort_source & DW_IC_TX_ARB_LOST)
485 return -EAGAIN;
Shinya Kuribayashice6eb572009-11-06 21:51:57 +0900486 else if (abort_source & DW_IC_TX_ABRT_GCALL_READ)
487 return -EINVAL; /* wrong msgs[] data */
488 else
489 return -EIO;
490}
491
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300492/*
493 * Prepare controller for a transaction and call i2c_dw_xfer_msg
494 */
Dirk Brandewie2373f6b2011-10-29 10:57:23 +0100495int
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300496i2c_dw_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], int num)
497{
498 struct dw_i2c_dev *dev = i2c_get_adapdata(adap);
499 int ret;
500
501 dev_dbg(dev->dev, "%s: msgs: %d\n", __func__, num);
502
503 mutex_lock(&dev->lock);
504
505 INIT_COMPLETION(dev->cmd_complete);
506 dev->msgs = msgs;
507 dev->msgs_num = num;
508 dev->cmd_err = 0;
509 dev->msg_write_idx = 0;
510 dev->msg_read_idx = 0;
511 dev->msg_err = 0;
512 dev->status = STATUS_IDLE;
Shinya Kuribayashice6eb572009-11-06 21:51:57 +0900513 dev->abort_source = 0;
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300514
515 ret = i2c_dw_wait_bus_not_busy(dev);
516 if (ret < 0)
517 goto done;
518
519 /* start the transfers */
Shinya Kuribayashi81e798b2009-11-06 21:48:55 +0900520 i2c_dw_xfer_init(dev);
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300521
522 /* wait for tx to complete */
523 ret = wait_for_completion_interruptible_timeout(&dev->cmd_complete, HZ);
524 if (ret == 0) {
525 dev_err(dev->dev, "controller timed out\n");
526 i2c_dw_init(dev);
527 ret = -ETIMEDOUT;
528 goto done;
529 } else if (ret < 0)
530 goto done;
531
532 if (dev->msg_err) {
533 ret = dev->msg_err;
534 goto done;
535 }
536
537 /* no error */
538 if (likely(!dev->cmd_err)) {
Shinya Kuribayashi07745392009-11-06 21:47:51 +0900539 /* Disable the adapter */
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700540 dw_writel(dev, 0, DW_IC_ENABLE);
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300541 ret = num;
542 goto done;
543 }
544
545 /* We have an error */
546 if (dev->cmd_err == DW_IC_ERR_TX_ABRT) {
Shinya Kuribayashice6eb572009-11-06 21:51:57 +0900547 ret = i2c_dw_handle_tx_abort(dev);
548 goto done;
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300549 }
550 ret = -EIO;
551
552done:
553 mutex_unlock(&dev->lock);
554
555 return ret;
556}
557
Dirk Brandewie2373f6b2011-10-29 10:57:23 +0100558u32 i2c_dw_func(struct i2c_adapter *adap)
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300559{
Dirk Brandewie2fa83262011-10-06 11:26:31 -0700560 struct dw_i2c_dev *dev = i2c_get_adapdata(adap);
561 return dev->functionality;
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300562}
563
Shinya Kuribayashie28000a2009-11-06 21:44:37 +0900564static u32 i2c_dw_read_clear_intrbits(struct dw_i2c_dev *dev)
565{
566 u32 stat;
567
568 /*
569 * The IC_INTR_STAT register just indicates "enabled" interrupts.
570 * Ths unmasked raw version of interrupt status bits are available
571 * in the IC_RAW_INTR_STAT register.
572 *
573 * That is,
Dirk Brandewie2373f6b2011-10-29 10:57:23 +0100574 * stat = dw_readl(IC_INTR_STAT);
Shinya Kuribayashie28000a2009-11-06 21:44:37 +0900575 * equals to,
Dirk Brandewie2373f6b2011-10-29 10:57:23 +0100576 * stat = dw_readl(IC_RAW_INTR_STAT) & dw_readl(IC_INTR_MASK);
Shinya Kuribayashie28000a2009-11-06 21:44:37 +0900577 *
578 * The raw version might be useful for debugging purposes.
579 */
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700580 stat = dw_readl(dev, DW_IC_INTR_STAT);
Shinya Kuribayashie28000a2009-11-06 21:44:37 +0900581
582 /*
583 * Do not use the IC_CLR_INTR register to clear interrupts, or
584 * you'll miss some interrupts, triggered during the period from
Dirk Brandewie2373f6b2011-10-29 10:57:23 +0100585 * dw_readl(IC_INTR_STAT) to dw_readl(IC_CLR_INTR).
Shinya Kuribayashie28000a2009-11-06 21:44:37 +0900586 *
587 * Instead, use the separately-prepared IC_CLR_* registers.
588 */
589 if (stat & DW_IC_INTR_RX_UNDER)
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700590 dw_readl(dev, DW_IC_CLR_RX_UNDER);
Shinya Kuribayashie28000a2009-11-06 21:44:37 +0900591 if (stat & DW_IC_INTR_RX_OVER)
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700592 dw_readl(dev, DW_IC_CLR_RX_OVER);
Shinya Kuribayashie28000a2009-11-06 21:44:37 +0900593 if (stat & DW_IC_INTR_TX_OVER)
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700594 dw_readl(dev, DW_IC_CLR_TX_OVER);
Shinya Kuribayashie28000a2009-11-06 21:44:37 +0900595 if (stat & DW_IC_INTR_RD_REQ)
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700596 dw_readl(dev, DW_IC_CLR_RD_REQ);
Shinya Kuribayashie28000a2009-11-06 21:44:37 +0900597 if (stat & DW_IC_INTR_TX_ABRT) {
598 /*
599 * The IC_TX_ABRT_SOURCE register is cleared whenever
600 * the IC_CLR_TX_ABRT is read. Preserve it beforehand.
601 */
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700602 dev->abort_source = dw_readl(dev, DW_IC_TX_ABRT_SOURCE);
603 dw_readl(dev, DW_IC_CLR_TX_ABRT);
Shinya Kuribayashie28000a2009-11-06 21:44:37 +0900604 }
605 if (stat & DW_IC_INTR_RX_DONE)
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700606 dw_readl(dev, DW_IC_CLR_RX_DONE);
Shinya Kuribayashie28000a2009-11-06 21:44:37 +0900607 if (stat & DW_IC_INTR_ACTIVITY)
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700608 dw_readl(dev, DW_IC_CLR_ACTIVITY);
Shinya Kuribayashie28000a2009-11-06 21:44:37 +0900609 if (stat & DW_IC_INTR_STOP_DET)
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700610 dw_readl(dev, DW_IC_CLR_STOP_DET);
Shinya Kuribayashie28000a2009-11-06 21:44:37 +0900611 if (stat & DW_IC_INTR_START_DET)
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700612 dw_readl(dev, DW_IC_CLR_START_DET);
Shinya Kuribayashie28000a2009-11-06 21:44:37 +0900613 if (stat & DW_IC_INTR_GEN_CALL)
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700614 dw_readl(dev, DW_IC_CLR_GEN_CALL);
Shinya Kuribayashie28000a2009-11-06 21:44:37 +0900615
616 return stat;
617}
618
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300619/*
620 * Interrupt service routine. This gets called whenever an I2C interrupt
621 * occurs.
622 */
Dirk Brandewie2373f6b2011-10-29 10:57:23 +0100623irqreturn_t i2c_dw_isr(int this_irq, void *dev_id)
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300624{
625 struct dw_i2c_dev *dev = dev_id;
Dirk Brandewieaf06cf62011-10-06 11:26:33 -0700626 u32 stat, enabled;
627
628 enabled = dw_readl(dev, DW_IC_ENABLE);
629 stat = dw_readl(dev, DW_IC_RAW_INTR_STAT);
630 dev_dbg(dev->dev, "%s: %s enabled= 0x%x stat=0x%x\n", __func__,
631 dev->adapter.name, enabled, stat);
632 if (!enabled || !(stat & ~DW_IC_INTR_ACTIVITY))
633 return IRQ_NONE;
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300634
Shinya Kuribayashie28000a2009-11-06 21:44:37 +0900635 stat = i2c_dw_read_clear_intrbits(dev);
Shinya Kuribayashie28000a2009-11-06 21:44:37 +0900636
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300637 if (stat & DW_IC_INTR_TX_ABRT) {
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300638 dev->cmd_err |= DW_IC_ERR_TX_ABRT;
639 dev->status = STATUS_IDLE;
Shinya Kuribayashi597fe312009-11-06 21:51:36 +0900640
641 /*
642 * Anytime TX_ABRT is set, the contents of the tx/rx
643 * buffers are flushed. Make sure to skip them.
644 */
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700645 dw_writel(dev, 0, DW_IC_INTR_MASK);
Shinya Kuribayashi597fe312009-11-06 21:51:36 +0900646 goto tx_aborted;
Shinya Kuribayashi07745392009-11-06 21:47:51 +0900647 }
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300648
Shinya Kuribayashi21a89d42009-11-06 21:48:33 +0900649 if (stat & DW_IC_INTR_RX_FULL)
Shinya Kuribayashi07745392009-11-06 21:47:51 +0900650 i2c_dw_read(dev);
Shinya Kuribayashi21a89d42009-11-06 21:48:33 +0900651
652 if (stat & DW_IC_INTR_TX_EMPTY)
Shinya Kuribayashi07745392009-11-06 21:47:51 +0900653 i2c_dw_xfer_msg(dev);
Shinya Kuribayashi07745392009-11-06 21:47:51 +0900654
655 /*
656 * No need to modify or disable the interrupt mask here.
657 * i2c_dw_xfer_msg() will take care of it according to
658 * the current transmit status.
659 */
660
Shinya Kuribayashi597fe312009-11-06 21:51:36 +0900661tx_aborted:
Shinya Kuribayashi8f588e42009-11-06 21:51:18 +0900662 if ((stat & (DW_IC_INTR_TX_ABRT | DW_IC_INTR_STOP_DET)) || dev->msg_err)
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300663 complete(&dev->cmd_complete);
664
665 return IRQ_HANDLED;
666}
Dirk Brandewief3fa9f32011-10-06 11:26:34 -0700667
668void i2c_dw_enable(struct dw_i2c_dev *dev)
669{
670 /* Enable the adapter */
671 dw_writel(dev, 1, DW_IC_ENABLE);
672}
673
674void i2c_dw_disable(struct dw_i2c_dev *dev)
675{
676 int ret;
677
678 /* Disable controller */
679 dw_writel(dev, 0, DW_IC_ENABLE);
680
681 /* Disable all interupts */
682 dw_writel(dev, 0, DW_IC_INTR_MASK);
683 dw_readl(dev, DW_IC_CLR_INTR);
684}
685
686void i2c_dw_clear_int(struct dw_i2c_dev *dev)
687{
688 dw_readl(dev, DW_IC_CLR_INTR);
689}
690
691void i2c_dw_disable_int(struct dw_i2c_dev *dev)
692{
693 dw_writel(dev, 0, DW_IC_INTR_MASK);
694}
695
696u32 i2c_dw_read_comp_param(struct dw_i2c_dev *dev)
697{
698 return dw_readl(dev, DW_IC_COMP_PARAM_1);
699}