blob: cd52f17028cd12f19b2de187368c735100926e37 [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
Baruch Siach1ab52cf2009-06-22 16:36:29 +030037static char *abort_sources[] = {
Shinya Kuribayashia0e06ea2009-11-06 21:52:22 +090038 [ABRT_7B_ADDR_NOACK] =
Baruch Siach1ab52cf2009-06-22 16:36:29 +030039 "slave address not acknowledged (7bit mode)",
Shinya Kuribayashia0e06ea2009-11-06 21:52:22 +090040 [ABRT_10ADDR1_NOACK] =
Baruch Siach1ab52cf2009-06-22 16:36:29 +030041 "first address byte not acknowledged (10bit mode)",
Shinya Kuribayashia0e06ea2009-11-06 21:52:22 +090042 [ABRT_10ADDR2_NOACK] =
Baruch Siach1ab52cf2009-06-22 16:36:29 +030043 "second address byte not acknowledged (10bit mode)",
Shinya Kuribayashia0e06ea2009-11-06 21:52:22 +090044 [ABRT_TXDATA_NOACK] =
Baruch Siach1ab52cf2009-06-22 16:36:29 +030045 "data not acknowledged",
Shinya Kuribayashia0e06ea2009-11-06 21:52:22 +090046 [ABRT_GCALL_NOACK] =
Baruch Siach1ab52cf2009-06-22 16:36:29 +030047 "no acknowledgement for a general call",
Shinya Kuribayashia0e06ea2009-11-06 21:52:22 +090048 [ABRT_GCALL_READ] =
Baruch Siach1ab52cf2009-06-22 16:36:29 +030049 "read after general call",
Shinya Kuribayashia0e06ea2009-11-06 21:52:22 +090050 [ABRT_SBYTE_ACKDET] =
Baruch Siach1ab52cf2009-06-22 16:36:29 +030051 "start byte acknowledged",
Shinya Kuribayashia0e06ea2009-11-06 21:52:22 +090052 [ABRT_SBYTE_NORSTRT] =
Baruch Siach1ab52cf2009-06-22 16:36:29 +030053 "trying to send start byte when restart is disabled",
Shinya Kuribayashia0e06ea2009-11-06 21:52:22 +090054 [ABRT_10B_RD_NORSTRT] =
Baruch Siach1ab52cf2009-06-22 16:36:29 +030055 "trying to read when restart is disabled (10bit mode)",
Shinya Kuribayashia0e06ea2009-11-06 21:52:22 +090056 [ABRT_MASTER_DIS] =
Baruch Siach1ab52cf2009-06-22 16:36:29 +030057 "trying to use disabled adapter",
Shinya Kuribayashia0e06ea2009-11-06 21:52:22 +090058 [ARB_LOST] =
Baruch Siach1ab52cf2009-06-22 16:36:29 +030059 "lost arbitration",
60};
61
Dirk Brandewie2373f6b2011-10-29 10:57:23 +010062u32 dw_readl(struct dw_i2c_dev *dev, int offset)
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -070063{
Jean-Hugues Deschenes18c40892011-10-06 11:26:27 -070064 u32 value = readl(dev->base + offset);
65
66 if (dev->swab)
67 return swab32(value);
68 else
69 return value;
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -070070}
71
Dirk Brandewie2373f6b2011-10-29 10:57:23 +010072void dw_writel(struct dw_i2c_dev *dev, u32 b, int offset)
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -070073{
Jean-Hugues Deschenes18c40892011-10-06 11:26:27 -070074 if (dev->swab)
75 b = swab32(b);
76
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -070077 writel(b, dev->base + offset);
78}
79
Shinya Kuribayashid60c7e82009-11-06 21:47:01 +090080static u32
81i2c_dw_scl_hcnt(u32 ic_clk, u32 tSYMBOL, u32 tf, int cond, int offset)
82{
83 /*
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.
87 */
88 if (cond)
89 /*
90 * Conditional expression:
91 *
92 * IC_[FS]S_SCL_HCNT + (1+4+3) >= IC_CLK * tHIGH
93 *
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.
97 *
98 * If your hardware is free from tHD;STA issue, try this one.
99 */
100 return (ic_clk * tSYMBOL + 5000) / 10000 - 8 + offset;
101 else
102 /*
103 * Conditional expression:
104 *
105 * IC_[FS]S_SCL_HCNT + 3 >= IC_CLK * (tHD;STA + tf)
106 *
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.
110 *
111 * If unsure, you'd better to take this alternative.
112 *
113 * The reason why we need to take into account "tf" here,
114 * is the same as described in i2c_dw_scl_lcnt().
115 */
116 return (ic_clk * (tSYMBOL + tf) + 5000) / 10000 - 3 + offset;
117}
118
119static u32 i2c_dw_scl_lcnt(u32 ic_clk, u32 tLOW, u32 tf, int offset)
120{
121 /*
122 * Conditional expression:
123 *
124 * IC_[FS]S_SCL_LCNT + 1 >= IC_CLK * (tLOW + tf)
125 *
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.
131 */
132 return ((ic_clk * (tLOW + tf) + 5000) / 10000) - 1 + offset;
133}
134
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300135/**
136 * i2c_dw_init() - initialize the designware i2c master hardware
137 * @dev: device private data
138 *
139 * This functions configures and enables the I2C master.
140 * This function is called during I2C init function, and in case of timeout at
141 * run time.
142 */
Dirk Brandewie2373f6b2011-10-29 10:57:23 +0100143int i2c_dw_init(struct dw_i2c_dev *dev)
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300144{
Dirk Brandewie1d31b582011-10-06 11:26:30 -0700145 u32 input_clock_khz;
Shinya Kuribayashid60c7e82009-11-06 21:47:01 +0900146 u32 ic_con, hcnt, lcnt;
Dirk Brandewie4a423a82011-10-06 11:26:28 -0700147 u32 reg;
148
Dirk Brandewie1d31b582011-10-06 11:26:30 -0700149 input_clock_khz = dev->get_clk_rate_khz(dev);
150
Dirk Brandewie4a423a82011-10-06 11:26:28 -0700151 /* Configure register endianess access */
152 reg = dw_readl(dev, DW_IC_COMP_TYPE);
153 if (reg == ___constant_swab32(DW_IC_COMP_TYPE_VALUE)) {
154 dev->swab = 1;
155 reg = DW_IC_COMP_TYPE_VALUE;
156 }
157
158 if (reg != DW_IC_COMP_TYPE_VALUE) {
159 dev_err(dev->dev, "Unknown Synopsys component type: "
160 "0x%08x\n", reg);
161 return -ENODEV;
162 }
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300163
164 /* Disable the adapter */
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700165 dw_writel(dev, 0, DW_IC_ENABLE);
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300166
167 /* set standard and fast speed deviders for high/low periods */
Shinya Kuribayashid60c7e82009-11-06 21:47:01 +0900168
169 /* Standard-mode */
170 hcnt = i2c_dw_scl_hcnt(input_clock_khz,
171 40, /* tHD;STA = tHIGH = 4.0 us */
172 3, /* tf = 0.3 us */
173 0, /* 0: DW default, 1: Ideal */
174 0); /* No offset */
175 lcnt = i2c_dw_scl_lcnt(input_clock_khz,
176 47, /* tLOW = 4.7 us */
177 3, /* tf = 0.3 us */
178 0); /* No offset */
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700179 dw_writel(dev, hcnt, DW_IC_SS_SCL_HCNT);
180 dw_writel(dev, lcnt, DW_IC_SS_SCL_LCNT);
Shinya Kuribayashid60c7e82009-11-06 21:47:01 +0900181 dev_dbg(dev->dev, "Standard-mode HCNT:LCNT = %d:%d\n", hcnt, lcnt);
182
183 /* Fast-mode */
184 hcnt = i2c_dw_scl_hcnt(input_clock_khz,
185 6, /* tHD;STA = tHIGH = 0.6 us */
186 3, /* tf = 0.3 us */
187 0, /* 0: DW default, 1: Ideal */
188 0); /* No offset */
189 lcnt = i2c_dw_scl_lcnt(input_clock_khz,
190 13, /* tLOW = 1.3 us */
191 3, /* tf = 0.3 us */
192 0); /* No offset */
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700193 dw_writel(dev, hcnt, DW_IC_FS_SCL_HCNT);
194 dw_writel(dev, lcnt, DW_IC_FS_SCL_LCNT);
Shinya Kuribayashid60c7e82009-11-06 21:47:01 +0900195 dev_dbg(dev->dev, "Fast-mode HCNT:LCNT = %d:%d\n", hcnt, lcnt);
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300196
Shinya Kuribayashi4cb6d1d2009-11-06 21:48:12 +0900197 /* Configure Tx/Rx FIFO threshold levels */
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700198 dw_writel(dev, dev->tx_fifo_depth - 1, DW_IC_TX_TL);
199 dw_writel(dev, 0, DW_IC_RX_TL);
Shinya Kuribayashi4cb6d1d2009-11-06 21:48:12 +0900200
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300201 /* configure the i2c master */
202 ic_con = DW_IC_CON_MASTER | DW_IC_CON_SLAVE_DISABLE |
203 DW_IC_CON_RESTART_EN | DW_IC_CON_SPEED_FAST;
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700204 dw_writel(dev, ic_con, DW_IC_CON);
Dirk Brandewie4a423a82011-10-06 11:26:28 -0700205 return 0;
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300206}
207
208/*
209 * Waiting for bus not busy
210 */
211static int i2c_dw_wait_bus_not_busy(struct dw_i2c_dev *dev)
212{
213 int timeout = TIMEOUT;
214
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700215 while (dw_readl(dev, DW_IC_STATUS) & DW_IC_STATUS_ACTIVITY) {
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300216 if (timeout <= 0) {
217 dev_warn(dev->dev, "timeout waiting for bus ready\n");
218 return -ETIMEDOUT;
219 }
220 timeout--;
221 mdelay(1);
222 }
223
224 return 0;
225}
226
Shinya Kuribayashi81e798b2009-11-06 21:48:55 +0900227static void i2c_dw_xfer_init(struct dw_i2c_dev *dev)
228{
229 struct i2c_msg *msgs = dev->msgs;
230 u32 ic_con;
231
232 /* Disable the adapter */
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700233 dw_writel(dev, 0, DW_IC_ENABLE);
Shinya Kuribayashi81e798b2009-11-06 21:48:55 +0900234
235 /* set the slave (target) address */
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700236 dw_writel(dev, msgs[dev->msg_write_idx].addr, DW_IC_TAR);
Shinya Kuribayashi81e798b2009-11-06 21:48:55 +0900237
238 /* if the slave address is ten bit address, enable 10BITADDR */
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700239 ic_con = dw_readl(dev, DW_IC_CON);
Shinya Kuribayashi81e798b2009-11-06 21:48:55 +0900240 if (msgs[dev->msg_write_idx].flags & I2C_M_TEN)
241 ic_con |= DW_IC_CON_10BITADDR_MASTER;
242 else
243 ic_con &= ~DW_IC_CON_10BITADDR_MASTER;
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700244 dw_writel(dev, ic_con, DW_IC_CON);
Shinya Kuribayashi81e798b2009-11-06 21:48:55 +0900245
246 /* Enable the adapter */
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700247 dw_writel(dev, 1, DW_IC_ENABLE);
Shinya Kuribayashi201d6a72009-11-06 21:50:40 +0900248
249 /* Enable interrupts */
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700250 dw_writel(dev, DW_IC_INTR_DEFAULT_MASK, DW_IC_INTR_MASK);
Shinya Kuribayashi81e798b2009-11-06 21:48:55 +0900251}
252
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300253/*
Shinya Kuribayashi201d6a72009-11-06 21:50:40 +0900254 * Initiate (and continue) low level master read/write transaction.
255 * This function is only called from i2c_dw_isr, and pumping i2c_msg
256 * messages into the tx buffer. Even if the size of i2c_msg data is
257 * longer than the size of the tx buffer, it handles everything.
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300258 */
Dirk Brandewie2373f6b2011-10-29 10:57:23 +0100259void
Shinya Kuribayashie77cf232009-11-06 21:46:04 +0900260i2c_dw_xfer_msg(struct dw_i2c_dev *dev)
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300261{
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300262 struct i2c_msg *msgs = dev->msgs;
Shinya Kuribayashi81e798b2009-11-06 21:48:55 +0900263 u32 intr_mask;
Shinya Kuribayashiae722222009-11-06 21:49:39 +0900264 int tx_limit, rx_limit;
Shinya Kuribayashied5e1dd2009-11-06 21:43:52 +0900265 u32 addr = msgs[dev->msg_write_idx].addr;
266 u32 buf_len = dev->tx_buf_len;
Justin P. Mattock69932482011-07-26 23:06:29 -0700267 u8 *buf = dev->tx_buf;
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300268
Shinya Kuribayashi201d6a72009-11-06 21:50:40 +0900269 intr_mask = DW_IC_INTR_DEFAULT_MASK;
Shinya Kuribayashic70c5cd2009-11-06 21:47:30 +0900270
Shinya Kuribayashi6d2ea482009-11-06 21:46:29 +0900271 for (; dev->msg_write_idx < dev->msgs_num; dev->msg_write_idx++) {
Shinya Kuribayashia0e06ea2009-11-06 21:52:22 +0900272 /*
273 * if target address has changed, we need to
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300274 * reprogram the target address in the i2c
275 * adapter when we are done with this transfer
276 */
Shinya Kuribayashi8f588e42009-11-06 21:51:18 +0900277 if (msgs[dev->msg_write_idx].addr != addr) {
278 dev_err(dev->dev,
279 "%s: invalid target address\n", __func__);
280 dev->msg_err = -EINVAL;
281 break;
282 }
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300283
284 if (msgs[dev->msg_write_idx].len == 0) {
285 dev_err(dev->dev,
286 "%s: invalid message length\n", __func__);
287 dev->msg_err = -EINVAL;
Shinya Kuribayashi8f588e42009-11-06 21:51:18 +0900288 break;
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300289 }
290
291 if (!(dev->status & STATUS_WRITE_IN_PROGRESS)) {
292 /* new i2c_msg */
Shinya Kuribayashi26ea15b2009-11-06 21:49:14 +0900293 buf = msgs[dev->msg_write_idx].buf;
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300294 buf_len = msgs[dev->msg_write_idx].len;
295 }
296
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700297 tx_limit = dev->tx_fifo_depth - dw_readl(dev, DW_IC_TXFLR);
298 rx_limit = dev->rx_fifo_depth - dw_readl(dev, DW_IC_RXFLR);
Shinya Kuribayashiae722222009-11-06 21:49:39 +0900299
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300300 while (buf_len > 0 && tx_limit > 0 && rx_limit > 0) {
301 if (msgs[dev->msg_write_idx].flags & I2C_M_RD) {
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700302 dw_writel(dev, 0x100, DW_IC_DATA_CMD);
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300303 rx_limit--;
304 } else
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700305 dw_writel(dev, *buf++, DW_IC_DATA_CMD);
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300306 tx_limit--; buf_len--;
307 }
Shinya Kuribayashic70c5cd2009-11-06 21:47:30 +0900308
Shinya Kuribayashi26ea15b2009-11-06 21:49:14 +0900309 dev->tx_buf = buf;
Shinya Kuribayashic70c5cd2009-11-06 21:47:30 +0900310 dev->tx_buf_len = buf_len;
311
312 if (buf_len > 0) {
313 /* more bytes to be written */
Shinya Kuribayashic70c5cd2009-11-06 21:47:30 +0900314 dev->status |= STATUS_WRITE_IN_PROGRESS;
315 break;
Shinya Kuribayashi69151e52009-11-06 21:51:00 +0900316 } else
Shinya Kuribayashic70c5cd2009-11-06 21:47:30 +0900317 dev->status &= ~STATUS_WRITE_IN_PROGRESS;
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300318 }
319
Shinya Kuribayashi69151e52009-11-06 21:51:00 +0900320 /*
321 * If i2c_msg index search is completed, we don't need TX_EMPTY
322 * interrupt any more.
323 */
324 if (dev->msg_write_idx == dev->msgs_num)
325 intr_mask &= ~DW_IC_INTR_TX_EMPTY;
326
Shinya Kuribayashi8f588e42009-11-06 21:51:18 +0900327 if (dev->msg_err)
328 intr_mask = 0;
329
Dirk Brandewie2373f6b2011-10-29 10:57:23 +0100330 dw_writel(dev, intr_mask, DW_IC_INTR_MASK);
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300331}
332
333static void
Shinya Kuribayashi78839bd2009-11-06 21:45:39 +0900334i2c_dw_read(struct dw_i2c_dev *dev)
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300335{
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300336 struct i2c_msg *msgs = dev->msgs;
Shinya Kuribayashiae722222009-11-06 21:49:39 +0900337 int rx_valid;
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300338
Shinya Kuribayashi6d2ea482009-11-06 21:46:29 +0900339 for (; dev->msg_read_idx < dev->msgs_num; dev->msg_read_idx++) {
Shinya Kuribayashied5e1dd2009-11-06 21:43:52 +0900340 u32 len;
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300341 u8 *buf;
342
343 if (!(msgs[dev->msg_read_idx].flags & I2C_M_RD))
344 continue;
345
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300346 if (!(dev->status & STATUS_READ_IN_PROGRESS)) {
347 len = msgs[dev->msg_read_idx].len;
348 buf = msgs[dev->msg_read_idx].buf;
349 } else {
350 len = dev->rx_buf_len;
351 buf = dev->rx_buf;
352 }
353
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700354 rx_valid = dw_readl(dev, DW_IC_RXFLR);
Shinya Kuribayashiae722222009-11-06 21:49:39 +0900355
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300356 for (; len > 0 && rx_valid > 0; len--, rx_valid--)
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700357 *buf++ = dw_readl(dev, DW_IC_DATA_CMD);
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300358
359 if (len > 0) {
360 dev->status |= STATUS_READ_IN_PROGRESS;
361 dev->rx_buf_len = len;
362 dev->rx_buf = buf;
363 return;
364 } else
365 dev->status &= ~STATUS_READ_IN_PROGRESS;
366 }
367}
368
Shinya Kuribayashice6eb572009-11-06 21:51:57 +0900369static int i2c_dw_handle_tx_abort(struct dw_i2c_dev *dev)
370{
371 unsigned long abort_source = dev->abort_source;
372 int i;
373
Shinya Kuribayashi6d1ea0f2009-11-16 20:40:14 +0900374 if (abort_source & DW_IC_TX_ABRT_NOACK) {
Akinobu Mita984b3f52010-03-05 13:41:37 -0800375 for_each_set_bit(i, &abort_source, ARRAY_SIZE(abort_sources))
Shinya Kuribayashi6d1ea0f2009-11-16 20:40:14 +0900376 dev_dbg(dev->dev,
377 "%s: %s\n", __func__, abort_sources[i]);
378 return -EREMOTEIO;
379 }
380
Akinobu Mita984b3f52010-03-05 13:41:37 -0800381 for_each_set_bit(i, &abort_source, ARRAY_SIZE(abort_sources))
Shinya Kuribayashice6eb572009-11-06 21:51:57 +0900382 dev_err(dev->dev, "%s: %s\n", __func__, abort_sources[i]);
383
384 if (abort_source & DW_IC_TX_ARB_LOST)
385 return -EAGAIN;
Shinya Kuribayashice6eb572009-11-06 21:51:57 +0900386 else if (abort_source & DW_IC_TX_ABRT_GCALL_READ)
387 return -EINVAL; /* wrong msgs[] data */
388 else
389 return -EIO;
390}
391
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300392/*
393 * Prepare controller for a transaction and call i2c_dw_xfer_msg
394 */
Dirk Brandewie2373f6b2011-10-29 10:57:23 +0100395int
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300396i2c_dw_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], int num)
397{
398 struct dw_i2c_dev *dev = i2c_get_adapdata(adap);
399 int ret;
400
401 dev_dbg(dev->dev, "%s: msgs: %d\n", __func__, num);
402
403 mutex_lock(&dev->lock);
404
405 INIT_COMPLETION(dev->cmd_complete);
406 dev->msgs = msgs;
407 dev->msgs_num = num;
408 dev->cmd_err = 0;
409 dev->msg_write_idx = 0;
410 dev->msg_read_idx = 0;
411 dev->msg_err = 0;
412 dev->status = STATUS_IDLE;
Shinya Kuribayashice6eb572009-11-06 21:51:57 +0900413 dev->abort_source = 0;
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300414
415 ret = i2c_dw_wait_bus_not_busy(dev);
416 if (ret < 0)
417 goto done;
418
419 /* start the transfers */
Shinya Kuribayashi81e798b2009-11-06 21:48:55 +0900420 i2c_dw_xfer_init(dev);
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300421
422 /* wait for tx to complete */
423 ret = wait_for_completion_interruptible_timeout(&dev->cmd_complete, HZ);
424 if (ret == 0) {
425 dev_err(dev->dev, "controller timed out\n");
426 i2c_dw_init(dev);
427 ret = -ETIMEDOUT;
428 goto done;
429 } else if (ret < 0)
430 goto done;
431
432 if (dev->msg_err) {
433 ret = dev->msg_err;
434 goto done;
435 }
436
437 /* no error */
438 if (likely(!dev->cmd_err)) {
Shinya Kuribayashi07745392009-11-06 21:47:51 +0900439 /* Disable the adapter */
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700440 dw_writel(dev, 0, DW_IC_ENABLE);
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300441 ret = num;
442 goto done;
443 }
444
445 /* We have an error */
446 if (dev->cmd_err == DW_IC_ERR_TX_ABRT) {
Shinya Kuribayashice6eb572009-11-06 21:51:57 +0900447 ret = i2c_dw_handle_tx_abort(dev);
448 goto done;
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300449 }
450 ret = -EIO;
451
452done:
453 mutex_unlock(&dev->lock);
454
455 return ret;
456}
457
Dirk Brandewie2373f6b2011-10-29 10:57:23 +0100458u32 i2c_dw_func(struct i2c_adapter *adap)
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300459{
Shinya Kuribayashi52d7e432009-11-06 21:50:02 +0900460 return I2C_FUNC_I2C |
461 I2C_FUNC_10BIT_ADDR |
462 I2C_FUNC_SMBUS_BYTE |
463 I2C_FUNC_SMBUS_BYTE_DATA |
464 I2C_FUNC_SMBUS_WORD_DATA |
465 I2C_FUNC_SMBUS_I2C_BLOCK;
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300466}
467
Shinya Kuribayashie28000a2009-11-06 21:44:37 +0900468static u32 i2c_dw_read_clear_intrbits(struct dw_i2c_dev *dev)
469{
470 u32 stat;
471
472 /*
473 * The IC_INTR_STAT register just indicates "enabled" interrupts.
474 * Ths unmasked raw version of interrupt status bits are available
475 * in the IC_RAW_INTR_STAT register.
476 *
477 * That is,
Dirk Brandewie2373f6b2011-10-29 10:57:23 +0100478 * stat = dw_readl(IC_INTR_STAT);
Shinya Kuribayashie28000a2009-11-06 21:44:37 +0900479 * equals to,
Dirk Brandewie2373f6b2011-10-29 10:57:23 +0100480 * stat = dw_readl(IC_RAW_INTR_STAT) & dw_readl(IC_INTR_MASK);
Shinya Kuribayashie28000a2009-11-06 21:44:37 +0900481 *
482 * The raw version might be useful for debugging purposes.
483 */
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700484 stat = dw_readl(dev, DW_IC_INTR_STAT);
Shinya Kuribayashie28000a2009-11-06 21:44:37 +0900485
486 /*
487 * Do not use the IC_CLR_INTR register to clear interrupts, or
488 * you'll miss some interrupts, triggered during the period from
Dirk Brandewie2373f6b2011-10-29 10:57:23 +0100489 * dw_readl(IC_INTR_STAT) to dw_readl(IC_CLR_INTR).
Shinya Kuribayashie28000a2009-11-06 21:44:37 +0900490 *
491 * Instead, use the separately-prepared IC_CLR_* registers.
492 */
493 if (stat & DW_IC_INTR_RX_UNDER)
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700494 dw_readl(dev, DW_IC_CLR_RX_UNDER);
Shinya Kuribayashie28000a2009-11-06 21:44:37 +0900495 if (stat & DW_IC_INTR_RX_OVER)
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700496 dw_readl(dev, DW_IC_CLR_RX_OVER);
Shinya Kuribayashie28000a2009-11-06 21:44:37 +0900497 if (stat & DW_IC_INTR_TX_OVER)
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700498 dw_readl(dev, DW_IC_CLR_TX_OVER);
Shinya Kuribayashie28000a2009-11-06 21:44:37 +0900499 if (stat & DW_IC_INTR_RD_REQ)
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700500 dw_readl(dev, DW_IC_CLR_RD_REQ);
Shinya Kuribayashie28000a2009-11-06 21:44:37 +0900501 if (stat & DW_IC_INTR_TX_ABRT) {
502 /*
503 * The IC_TX_ABRT_SOURCE register is cleared whenever
504 * the IC_CLR_TX_ABRT is read. Preserve it beforehand.
505 */
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700506 dev->abort_source = dw_readl(dev, DW_IC_TX_ABRT_SOURCE);
507 dw_readl(dev, DW_IC_CLR_TX_ABRT);
Shinya Kuribayashie28000a2009-11-06 21:44:37 +0900508 }
509 if (stat & DW_IC_INTR_RX_DONE)
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700510 dw_readl(dev, DW_IC_CLR_RX_DONE);
Shinya Kuribayashie28000a2009-11-06 21:44:37 +0900511 if (stat & DW_IC_INTR_ACTIVITY)
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700512 dw_readl(dev, DW_IC_CLR_ACTIVITY);
Shinya Kuribayashie28000a2009-11-06 21:44:37 +0900513 if (stat & DW_IC_INTR_STOP_DET)
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700514 dw_readl(dev, DW_IC_CLR_STOP_DET);
Shinya Kuribayashie28000a2009-11-06 21:44:37 +0900515 if (stat & DW_IC_INTR_START_DET)
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700516 dw_readl(dev, DW_IC_CLR_START_DET);
Shinya Kuribayashie28000a2009-11-06 21:44:37 +0900517 if (stat & DW_IC_INTR_GEN_CALL)
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700518 dw_readl(dev, DW_IC_CLR_GEN_CALL);
Shinya Kuribayashie28000a2009-11-06 21:44:37 +0900519
520 return stat;
521}
522
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300523/*
524 * Interrupt service routine. This gets called whenever an I2C interrupt
525 * occurs.
526 */
Dirk Brandewie2373f6b2011-10-29 10:57:23 +0100527irqreturn_t i2c_dw_isr(int this_irq, void *dev_id)
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300528{
529 struct dw_i2c_dev *dev = dev_id;
Shinya Kuribayashied5e1dd2009-11-06 21:43:52 +0900530 u32 stat;
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300531
Shinya Kuribayashie28000a2009-11-06 21:44:37 +0900532 stat = i2c_dw_read_clear_intrbits(dev);
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300533 dev_dbg(dev->dev, "%s: stat=0x%x\n", __func__, stat);
Shinya Kuribayashie28000a2009-11-06 21:44:37 +0900534
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300535 if (stat & DW_IC_INTR_TX_ABRT) {
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300536 dev->cmd_err |= DW_IC_ERR_TX_ABRT;
537 dev->status = STATUS_IDLE;
Shinya Kuribayashi597fe312009-11-06 21:51:36 +0900538
539 /*
540 * Anytime TX_ABRT is set, the contents of the tx/rx
541 * buffers are flushed. Make sure to skip them.
542 */
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700543 dw_writel(dev, 0, DW_IC_INTR_MASK);
Shinya Kuribayashi597fe312009-11-06 21:51:36 +0900544 goto tx_aborted;
Shinya Kuribayashi07745392009-11-06 21:47:51 +0900545 }
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300546
Shinya Kuribayashi21a89d42009-11-06 21:48:33 +0900547 if (stat & DW_IC_INTR_RX_FULL)
Shinya Kuribayashi07745392009-11-06 21:47:51 +0900548 i2c_dw_read(dev);
Shinya Kuribayashi21a89d42009-11-06 21:48:33 +0900549
550 if (stat & DW_IC_INTR_TX_EMPTY)
Shinya Kuribayashi07745392009-11-06 21:47:51 +0900551 i2c_dw_xfer_msg(dev);
Shinya Kuribayashi07745392009-11-06 21:47:51 +0900552
553 /*
554 * No need to modify or disable the interrupt mask here.
555 * i2c_dw_xfer_msg() will take care of it according to
556 * the current transmit status.
557 */
558
Shinya Kuribayashi597fe312009-11-06 21:51:36 +0900559tx_aborted:
Shinya Kuribayashi8f588e42009-11-06 21:51:18 +0900560 if ((stat & (DW_IC_INTR_TX_ABRT | DW_IC_INTR_STOP_DET)) || dev->msg_err)
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300561 complete(&dev->cmd_complete);
562
563 return IRQ_HANDLED;
564}