blob: 52a603a01ddda659d32f8c348715658fa0d071e4 [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.
Baruch Siach1ab52cf2009-06-22 16:36:29 +030021 * ----------------------------------------------------------------------------
22 *
23 */
Axel Line68bb912012-09-10 10:14:02 +020024#include <linux/export.h>
Baruch Siach1ab52cf2009-06-22 16:36:29 +030025#include <linux/errno.h>
Baruch Siach1ab52cf2009-06-22 16:36:29 +030026#include <linux/err.h>
Dirk Brandewie2373f6b2011-10-29 10:57:23 +010027#include <linux/i2c.h>
Baruch Siach1ab52cf2009-06-22 16:36:29 +030028#include <linux/interrupt.h>
Baruch Siach1ab52cf2009-06-22 16:36:29 +030029#include <linux/io.h>
Dirk Brandewie18dbdda2011-10-06 11:26:36 -070030#include <linux/pm_runtime.h>
Dirk Brandewie2373f6b2011-10-29 10:57:23 +010031#include <linux/delay.h>
Mika Westerberg9dd31622013-01-17 12:31:04 +020032#include <linux/module.h>
Dirk Brandewie2373f6b2011-10-29 10:57:23 +010033#include "i2c-designware-core.h"
Shinya Kuribayashice6eb572009-11-06 21:51:57 +090034
Dirk Brandewief3fa9f32011-10-06 11:26:34 -070035/*
36 * Registers offset
37 */
38#define DW_IC_CON 0x0
39#define DW_IC_TAR 0x4
40#define DW_IC_DATA_CMD 0x10
41#define DW_IC_SS_SCL_HCNT 0x14
42#define DW_IC_SS_SCL_LCNT 0x18
43#define DW_IC_FS_SCL_HCNT 0x1c
44#define DW_IC_FS_SCL_LCNT 0x20
Weifeng Voonb6e67142016-08-12 17:02:51 +030045#define DW_IC_HS_SCL_HCNT 0x24
46#define DW_IC_HS_SCL_LCNT 0x28
Dirk Brandewief3fa9f32011-10-06 11:26:34 -070047#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
Christian Ruppert9803f862013-06-26 10:55:06 +020067#define DW_IC_SDA_HOLD 0x7c
Dirk Brandewief3fa9f32011-10-06 11:26:34 -070068#define DW_IC_TX_ABRT_SOURCE 0x80
Mika Westerberg3ca4ed872013-04-10 00:36:40 +000069#define DW_IC_ENABLE_STATUS 0x9c
Dirk Brandewief3fa9f32011-10-06 11:26:34 -070070#define DW_IC_COMP_PARAM_1 0xf4
Christian Ruppert9803f862013-06-26 10:55:06 +020071#define DW_IC_COMP_VERSION 0xf8
72#define DW_IC_SDA_HOLD_MIN_VERS 0x3131312A
Dirk Brandewief3fa9f32011-10-06 11:26:34 -070073#define DW_IC_COMP_TYPE 0xfc
74#define DW_IC_COMP_TYPE_VALUE 0x44570140
75
76#define DW_IC_INTR_RX_UNDER 0x001
77#define DW_IC_INTR_RX_OVER 0x002
78#define DW_IC_INTR_RX_FULL 0x004
79#define DW_IC_INTR_TX_OVER 0x008
80#define DW_IC_INTR_TX_EMPTY 0x010
81#define DW_IC_INTR_RD_REQ 0x020
82#define DW_IC_INTR_TX_ABRT 0x040
83#define DW_IC_INTR_RX_DONE 0x080
84#define DW_IC_INTR_ACTIVITY 0x100
85#define DW_IC_INTR_STOP_DET 0x200
86#define DW_IC_INTR_START_DET 0x400
87#define DW_IC_INTR_GEN_CALL 0x800
88
89#define DW_IC_INTR_DEFAULT_MASK (DW_IC_INTR_RX_FULL | \
90 DW_IC_INTR_TX_EMPTY | \
91 DW_IC_INTR_TX_ABRT | \
92 DW_IC_INTR_STOP_DET)
93
Lucas De Marchi0317e6c2016-08-23 19:18:56 -030094#define DW_IC_STATUS_ACTIVITY 0x1
95#define DW_IC_STATUS_TFE BIT(2)
96#define DW_IC_STATUS_MST_ACTIVITY BIT(5)
Dirk Brandewief3fa9f32011-10-06 11:26:34 -070097
98#define DW_IC_ERR_TX_ABRT 0x1
99
Chew, Chiau Eebd63ace2013-09-27 02:57:35 +0800100#define DW_IC_TAR_10BITADDR_MASTER BIT(12)
101
Weifeng Voonb6e67142016-08-12 17:02:51 +0300102#define DW_IC_COMP_PARAM_1_SPEED_MODE_HIGH (BIT(2) | BIT(3))
103#define DW_IC_COMP_PARAM_1_SPEED_MODE_MASK GENMASK(3, 2)
104
Dirk Brandewief3fa9f32011-10-06 11:26:34 -0700105/*
106 * status codes
107 */
108#define STATUS_IDLE 0x0
109#define STATUS_WRITE_IN_PROGRESS 0x1
110#define STATUS_READ_IN_PROGRESS 0x2
111
112#define TIMEOUT 20 /* ms */
113
114/*
115 * hardware abort codes from the DW_IC_TX_ABRT_SOURCE register
116 *
117 * only expected abort codes are listed here
118 * refer to the datasheet for the full list
119 */
120#define ABRT_7B_ADDR_NOACK 0
121#define ABRT_10ADDR1_NOACK 1
122#define ABRT_10ADDR2_NOACK 2
123#define ABRT_TXDATA_NOACK 3
124#define ABRT_GCALL_NOACK 4
125#define ABRT_GCALL_READ 5
126#define ABRT_SBYTE_ACKDET 7
127#define ABRT_SBYTE_NORSTRT 9
128#define ABRT_10B_RD_NORSTRT 10
129#define ABRT_MASTER_DIS 11
130#define ARB_LOST 12
131
132#define DW_IC_TX_ABRT_7B_ADDR_NOACK (1UL << ABRT_7B_ADDR_NOACK)
133#define DW_IC_TX_ABRT_10ADDR1_NOACK (1UL << ABRT_10ADDR1_NOACK)
134#define DW_IC_TX_ABRT_10ADDR2_NOACK (1UL << ABRT_10ADDR2_NOACK)
135#define DW_IC_TX_ABRT_TXDATA_NOACK (1UL << ABRT_TXDATA_NOACK)
136#define DW_IC_TX_ABRT_GCALL_NOACK (1UL << ABRT_GCALL_NOACK)
137#define DW_IC_TX_ABRT_GCALL_READ (1UL << ABRT_GCALL_READ)
138#define DW_IC_TX_ABRT_SBYTE_ACKDET (1UL << ABRT_SBYTE_ACKDET)
139#define DW_IC_TX_ABRT_SBYTE_NORSTRT (1UL << ABRT_SBYTE_NORSTRT)
140#define DW_IC_TX_ABRT_10B_RD_NORSTRT (1UL << ABRT_10B_RD_NORSTRT)
141#define DW_IC_TX_ABRT_MASTER_DIS (1UL << ABRT_MASTER_DIS)
142#define DW_IC_TX_ARB_LOST (1UL << ARB_LOST)
143
144#define DW_IC_TX_ABRT_NOACK (DW_IC_TX_ABRT_7B_ADDR_NOACK | \
145 DW_IC_TX_ABRT_10ADDR1_NOACK | \
146 DW_IC_TX_ABRT_10ADDR2_NOACK | \
147 DW_IC_TX_ABRT_TXDATA_NOACK | \
148 DW_IC_TX_ABRT_GCALL_NOACK)
149
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300150static char *abort_sources[] = {
Shinya Kuribayashia0e06ea2009-11-06 21:52:22 +0900151 [ABRT_7B_ADDR_NOACK] =
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300152 "slave address not acknowledged (7bit mode)",
Shinya Kuribayashia0e06ea2009-11-06 21:52:22 +0900153 [ABRT_10ADDR1_NOACK] =
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300154 "first address byte not acknowledged (10bit mode)",
Shinya Kuribayashia0e06ea2009-11-06 21:52:22 +0900155 [ABRT_10ADDR2_NOACK] =
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300156 "second address byte not acknowledged (10bit mode)",
Shinya Kuribayashia0e06ea2009-11-06 21:52:22 +0900157 [ABRT_TXDATA_NOACK] =
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300158 "data not acknowledged",
Shinya Kuribayashia0e06ea2009-11-06 21:52:22 +0900159 [ABRT_GCALL_NOACK] =
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300160 "no acknowledgement for a general call",
Shinya Kuribayashia0e06ea2009-11-06 21:52:22 +0900161 [ABRT_GCALL_READ] =
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300162 "read after general call",
Shinya Kuribayashia0e06ea2009-11-06 21:52:22 +0900163 [ABRT_SBYTE_ACKDET] =
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300164 "start byte acknowledged",
Shinya Kuribayashia0e06ea2009-11-06 21:52:22 +0900165 [ABRT_SBYTE_NORSTRT] =
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300166 "trying to send start byte when restart is disabled",
Shinya Kuribayashia0e06ea2009-11-06 21:52:22 +0900167 [ABRT_10B_RD_NORSTRT] =
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300168 "trying to read when restart is disabled (10bit mode)",
Shinya Kuribayashia0e06ea2009-11-06 21:52:22 +0900169 [ABRT_MASTER_DIS] =
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300170 "trying to use disabled adapter",
Shinya Kuribayashia0e06ea2009-11-06 21:52:22 +0900171 [ARB_LOST] =
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300172 "lost arbitration",
173};
174
Jarkko Nikula8a437452015-08-31 17:31:31 +0300175static u32 dw_readl(struct dw_i2c_dev *dev, int offset)
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700176{
Stefan Roesea8a9f3f2012-04-18 15:01:41 +0200177 u32 value;
Jean-Hugues Deschenes18c40892011-10-06 11:26:27 -0700178
Stefan Roesea8a9f3f2012-04-18 15:01:41 +0200179 if (dev->accessor_flags & ACCESS_16BIT)
Jisheng Zhang67105c52014-12-11 14:26:41 +0800180 value = readw_relaxed(dev->base + offset) |
181 (readw_relaxed(dev->base + offset + 2) << 16);
Stefan Roesea8a9f3f2012-04-18 15:01:41 +0200182 else
Jisheng Zhang67105c52014-12-11 14:26:41 +0800183 value = readl_relaxed(dev->base + offset);
Stefan Roesea8a9f3f2012-04-18 15:01:41 +0200184
185 if (dev->accessor_flags & ACCESS_SWAP)
Jean-Hugues Deschenes18c40892011-10-06 11:26:27 -0700186 return swab32(value);
187 else
188 return value;
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700189}
190
Jarkko Nikula8a437452015-08-31 17:31:31 +0300191static void dw_writel(struct dw_i2c_dev *dev, u32 b, int offset)
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700192{
Stefan Roesea8a9f3f2012-04-18 15:01:41 +0200193 if (dev->accessor_flags & ACCESS_SWAP)
Jean-Hugues Deschenes18c40892011-10-06 11:26:27 -0700194 b = swab32(b);
195
Stefan Roesea8a9f3f2012-04-18 15:01:41 +0200196 if (dev->accessor_flags & ACCESS_16BIT) {
Jisheng Zhang67105c52014-12-11 14:26:41 +0800197 writew_relaxed((u16)b, dev->base + offset);
198 writew_relaxed((u16)(b >> 16), dev->base + offset + 2);
Stefan Roesea8a9f3f2012-04-18 15:01:41 +0200199 } else {
Jisheng Zhang67105c52014-12-11 14:26:41 +0800200 writel_relaxed(b, dev->base + offset);
Stefan Roesea8a9f3f2012-04-18 15:01:41 +0200201 }
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700202}
203
Shinya Kuribayashid60c7e82009-11-06 21:47:01 +0900204static u32
205i2c_dw_scl_hcnt(u32 ic_clk, u32 tSYMBOL, u32 tf, int cond, int offset)
206{
207 /*
208 * DesignWare I2C core doesn't seem to have solid strategy to meet
209 * the tHD;STA timing spec. Configuring _HCNT based on tHIGH spec
210 * will result in violation of the tHD;STA spec.
211 */
212 if (cond)
213 /*
214 * Conditional expression:
215 *
216 * IC_[FS]S_SCL_HCNT + (1+4+3) >= IC_CLK * tHIGH
217 *
218 * This is based on the DW manuals, and represents an ideal
219 * configuration. The resulting I2C bus speed will be
220 * faster than any of the others.
221 *
222 * If your hardware is free from tHD;STA issue, try this one.
223 */
Romain Baeriswyl64682762014-01-20 17:43:43 +0100224 return (ic_clk * tSYMBOL + 500000) / 1000000 - 8 + offset;
Shinya Kuribayashid60c7e82009-11-06 21:47:01 +0900225 else
226 /*
227 * Conditional expression:
228 *
229 * IC_[FS]S_SCL_HCNT + 3 >= IC_CLK * (tHD;STA + tf)
230 *
231 * This is just experimental rule; the tHD;STA period turned
232 * out to be proportinal to (_HCNT + 3). With this setting,
233 * we could meet both tHIGH and tHD;STA timing specs.
234 *
235 * If unsure, you'd better to take this alternative.
236 *
237 * The reason why we need to take into account "tf" here,
238 * is the same as described in i2c_dw_scl_lcnt().
239 */
Romain Baeriswyl64682762014-01-20 17:43:43 +0100240 return (ic_clk * (tSYMBOL + tf) + 500000) / 1000000
241 - 3 + offset;
Shinya Kuribayashid60c7e82009-11-06 21:47:01 +0900242}
243
244static u32 i2c_dw_scl_lcnt(u32 ic_clk, u32 tLOW, u32 tf, int offset)
245{
246 /*
247 * Conditional expression:
248 *
249 * IC_[FS]S_SCL_LCNT + 1 >= IC_CLK * (tLOW + tf)
250 *
251 * DW I2C core starts counting the SCL CNTs for the LOW period
252 * of the SCL clock (tLOW) as soon as it pulls the SCL line.
253 * In order to meet the tLOW timing spec, we need to take into
254 * account the fall time of SCL signal (tf). Default tf value
255 * should be 0.3 us, for safety.
256 */
Romain Baeriswyl64682762014-01-20 17:43:43 +0100257 return ((ic_clk * (tLOW + tf) + 500000) / 1000000) - 1 + offset;
Shinya Kuribayashid60c7e82009-11-06 21:47:01 +0900258}
259
Mika Westerberg3ca4ed872013-04-10 00:36:40 +0000260static void __i2c_dw_enable(struct dw_i2c_dev *dev, bool enable)
261{
José Roberto de Souza2702ea72016-08-23 19:18:53 -0300262 dw_writel(dev, enable, DW_IC_ENABLE);
263}
264
265static void __i2c_dw_enable_and_wait(struct dw_i2c_dev *dev, bool enable)
266{
Mika Westerberg3ca4ed872013-04-10 00:36:40 +0000267 int timeout = 100;
268
269 do {
José Roberto de Souza2702ea72016-08-23 19:18:53 -0300270 __i2c_dw_enable(dev, enable);
Mika Westerberg3ca4ed872013-04-10 00:36:40 +0000271 if ((dw_readl(dev, DW_IC_ENABLE_STATUS) & 1) == enable)
272 return;
273
274 /*
275 * Wait 10 times the signaling period of the highest I2C
276 * transfer supported by the driver (for 400KHz this is
277 * 25us) as described in the DesignWare I2C databook.
278 */
279 usleep_range(25, 250);
280 } while (timeout--);
281
282 dev_warn(dev->dev, "timeout in %sabling adapter\n",
283 enable ? "en" : "dis");
284}
285
Suravee Suthikulpanitb33af112016-01-04 09:17:35 -0600286static unsigned long i2c_dw_clk_rate(struct dw_i2c_dev *dev)
287{
288 /*
289 * Clock is not necessary if we got LCNT/HCNT values directly from
290 * the platform code.
291 */
292 if (WARN_ON_ONCE(!dev->get_clk_rate_khz))
293 return 0;
294 return dev->get_clk_rate_khz(dev);
295}
296
Lucas De Marchi8c5660b2016-08-23 19:18:54 -0300297static int i2c_dw_acquire_lock(struct dw_i2c_dev *dev)
298{
299 int ret;
300
301 if (!dev->acquire_lock)
302 return 0;
303
304 ret = dev->acquire_lock(dev);
305 if (!ret)
306 return 0;
307
308 dev_err(dev->dev, "couldn't acquire bus ownership\n");
309
310 return ret;
311}
312
313static void i2c_dw_release_lock(struct dw_i2c_dev *dev)
314{
315 if (dev->release_lock)
316 dev->release_lock(dev);
317}
318
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300319/**
320 * i2c_dw_init() - initialize the designware i2c master hardware
321 * @dev: device private data
322 *
323 * This functions configures and enables the I2C master.
324 * This function is called during I2C init function, and in case of timeout at
325 * run time.
326 */
Dirk Brandewie2373f6b2011-10-29 10:57:23 +0100327int i2c_dw_init(struct dw_i2c_dev *dev)
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300328{
Dirk Brandewiee18563f2011-10-06 11:26:32 -0700329 u32 hcnt, lcnt;
Weifeng Voonb6e67142016-08-12 17:02:51 +0300330 u32 reg, comp_param1;
Romain Baeriswyl64682762014-01-20 17:43:43 +0100331 u32 sda_falling_time, scl_falling_time;
David Boxc0601d22015-01-15 01:12:16 -0800332 int ret;
333
Lucas De Marchi8c5660b2016-08-23 19:18:54 -0300334 ret = i2c_dw_acquire_lock(dev);
335 if (ret)
336 return ret;
Dirk Brandewie4a423a82011-10-06 11:26:28 -0700337
Dirk Brandewie4a423a82011-10-06 11:26:28 -0700338 reg = dw_readl(dev, DW_IC_COMP_TYPE);
339 if (reg == ___constant_swab32(DW_IC_COMP_TYPE_VALUE)) {
Stefan Roesea8a9f3f2012-04-18 15:01:41 +0200340 /* Configure register endianess access */
341 dev->accessor_flags |= ACCESS_SWAP;
342 } else if (reg == (DW_IC_COMP_TYPE_VALUE & 0x0000ffff)) {
343 /* Configure register access mode 16bit */
344 dev->accessor_flags |= ACCESS_16BIT;
345 } else if (reg != DW_IC_COMP_TYPE_VALUE) {
Dirk Brandewie4a423a82011-10-06 11:26:28 -0700346 dev_err(dev->dev, "Unknown Synopsys component type: "
347 "0x%08x\n", reg);
Lucas De Marchi8c5660b2016-08-23 19:18:54 -0300348 i2c_dw_release_lock(dev);
Dirk Brandewie4a423a82011-10-06 11:26:28 -0700349 return -ENODEV;
350 }
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300351
Weifeng Voonb6e67142016-08-12 17:02:51 +0300352 comp_param1 = dw_readl(dev, DW_IC_COMP_PARAM_1);
353
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300354 /* Disable the adapter */
José Roberto de Souza2702ea72016-08-23 19:18:53 -0300355 __i2c_dw_enable_and_wait(dev, false);
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300356
357 /* set standard and fast speed deviders for high/low periods */
Shinya Kuribayashid60c7e82009-11-06 21:47:01 +0900358
Romain Baeriswyl64682762014-01-20 17:43:43 +0100359 sda_falling_time = dev->sda_falling_time ?: 300; /* ns */
360 scl_falling_time = dev->scl_falling_time ?: 300; /* ns */
361
Jarkko Nikula42ffd392015-01-23 11:35:55 +0200362 /* Set SCL timing parameters for standard-mode */
Mika Westerbergdefc0b22013-08-19 15:07:53 +0300363 if (dev->ss_hcnt && dev->ss_lcnt) {
364 hcnt = dev->ss_hcnt;
365 lcnt = dev->ss_lcnt;
Jarkko Nikula42ffd392015-01-23 11:35:55 +0200366 } else {
Suravee Suthikulpanitb33af112016-01-04 09:17:35 -0600367 hcnt = i2c_dw_scl_hcnt(i2c_dw_clk_rate(dev),
Jarkko Nikula42ffd392015-01-23 11:35:55 +0200368 4000, /* tHD;STA = tHIGH = 4.0 us */
369 sda_falling_time,
370 0, /* 0: DW default, 1: Ideal */
371 0); /* No offset */
Suravee Suthikulpanitb33af112016-01-04 09:17:35 -0600372 lcnt = i2c_dw_scl_lcnt(i2c_dw_clk_rate(dev),
Jarkko Nikula42ffd392015-01-23 11:35:55 +0200373 4700, /* tLOW = 4.7 us */
374 scl_falling_time,
375 0); /* No offset */
Mika Westerbergdefc0b22013-08-19 15:07:53 +0300376 }
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700377 dw_writel(dev, hcnt, DW_IC_SS_SCL_HCNT);
378 dw_writel(dev, lcnt, DW_IC_SS_SCL_LCNT);
Shinya Kuribayashid60c7e82009-11-06 21:47:01 +0900379 dev_dbg(dev->dev, "Standard-mode HCNT:LCNT = %d:%d\n", hcnt, lcnt);
380
Weifeng Voond608c3d2016-08-12 17:02:49 +0300381 /* Set SCL timing parameters for fast-mode or fast-mode plus */
382 if ((dev->clk_freq == 1000000) && dev->fp_hcnt && dev->fp_lcnt) {
383 hcnt = dev->fp_hcnt;
384 lcnt = dev->fp_lcnt;
385 } else if (dev->fs_hcnt && dev->fs_lcnt) {
Mika Westerbergdefc0b22013-08-19 15:07:53 +0300386 hcnt = dev->fs_hcnt;
387 lcnt = dev->fs_lcnt;
Jarkko Nikula42ffd392015-01-23 11:35:55 +0200388 } else {
Suravee Suthikulpanitb33af112016-01-04 09:17:35 -0600389 hcnt = i2c_dw_scl_hcnt(i2c_dw_clk_rate(dev),
Jarkko Nikula42ffd392015-01-23 11:35:55 +0200390 600, /* tHD;STA = tHIGH = 0.6 us */
391 sda_falling_time,
392 0, /* 0: DW default, 1: Ideal */
393 0); /* No offset */
Suravee Suthikulpanitb33af112016-01-04 09:17:35 -0600394 lcnt = i2c_dw_scl_lcnt(i2c_dw_clk_rate(dev),
Jarkko Nikula42ffd392015-01-23 11:35:55 +0200395 1300, /* tLOW = 1.3 us */
396 scl_falling_time,
397 0); /* No offset */
Mika Westerbergdefc0b22013-08-19 15:07:53 +0300398 }
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700399 dw_writel(dev, hcnt, DW_IC_FS_SCL_HCNT);
400 dw_writel(dev, lcnt, DW_IC_FS_SCL_LCNT);
Shinya Kuribayashid60c7e82009-11-06 21:47:01 +0900401 dev_dbg(dev->dev, "Fast-mode HCNT:LCNT = %d:%d\n", hcnt, lcnt);
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300402
Weifeng Voonb6e67142016-08-12 17:02:51 +0300403 if ((dev->master_cfg & DW_IC_CON_SPEED_MASK) ==
404 DW_IC_CON_SPEED_HIGH) {
405 if ((comp_param1 & DW_IC_COMP_PARAM_1_SPEED_MODE_MASK)
406 != DW_IC_COMP_PARAM_1_SPEED_MODE_HIGH) {
407 dev_err(dev->dev, "High Speed not supported!\n");
408 dev->master_cfg &= ~DW_IC_CON_SPEED_MASK;
409 dev->master_cfg |= DW_IC_CON_SPEED_FAST;
410 } else if (dev->hs_hcnt && dev->hs_lcnt) {
411 hcnt = dev->hs_hcnt;
412 lcnt = dev->hs_lcnt;
413 dw_writel(dev, hcnt, DW_IC_HS_SCL_HCNT);
414 dw_writel(dev, lcnt, DW_IC_HS_SCL_LCNT);
415 dev_dbg(dev->dev, "HighSpeed-mode HCNT:LCNT = %d:%d\n",
416 hcnt, lcnt);
417 }
418 }
419
Christian Ruppert9803f862013-06-26 10:55:06 +0200420 /* Configure SDA Hold Time if required */
421 if (dev->sda_hold_time) {
422 reg = dw_readl(dev, DW_IC_COMP_VERSION);
423 if (reg >= DW_IC_SDA_HOLD_MIN_VERS)
424 dw_writel(dev, dev->sda_hold_time, DW_IC_SDA_HOLD);
425 else
426 dev_warn(dev->dev,
427 "Hardware too old to adjust SDA hold time.");
428 }
429
Shinya Kuribayashi4cb6d1d2009-11-06 21:48:12 +0900430 /* Configure Tx/Rx FIFO threshold levels */
Andrew Jacksond39f77b2014-11-07 12:10:44 +0000431 dw_writel(dev, dev->tx_fifo_depth / 2, DW_IC_TX_TL);
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700432 dw_writel(dev, 0, DW_IC_RX_TL);
Shinya Kuribayashi4cb6d1d2009-11-06 21:48:12 +0900433
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300434 /* configure the i2c master */
Dirk Brandewiee18563f2011-10-06 11:26:32 -0700435 dw_writel(dev, dev->master_cfg , DW_IC_CON);
David Boxc0601d22015-01-15 01:12:16 -0800436
Lucas De Marchi8c5660b2016-08-23 19:18:54 -0300437 i2c_dw_release_lock(dev);
438
Dirk Brandewie4a423a82011-10-06 11:26:28 -0700439 return 0;
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300440}
Axel Line68bb912012-09-10 10:14:02 +0200441EXPORT_SYMBOL_GPL(i2c_dw_init);
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300442
443/*
444 * Waiting for bus not busy
445 */
446static int i2c_dw_wait_bus_not_busy(struct dw_i2c_dev *dev)
447{
448 int timeout = TIMEOUT;
449
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700450 while (dw_readl(dev, DW_IC_STATUS) & DW_IC_STATUS_ACTIVITY) {
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300451 if (timeout <= 0) {
452 dev_warn(dev->dev, "timeout waiting for bus ready\n");
453 return -ETIMEDOUT;
454 }
455 timeout--;
Mika Westerberg1451b912013-04-10 00:36:41 +0000456 usleep_range(1000, 1100);
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300457 }
458
459 return 0;
460}
461
Shinya Kuribayashi81e798b2009-11-06 21:48:55 +0900462static void i2c_dw_xfer_init(struct dw_i2c_dev *dev)
463{
464 struct i2c_msg *msgs = dev->msgs;
Lucas De Marchi63d0f0a62016-08-23 19:18:55 -0300465 u32 ic_tar = 0;
Lucas De Marchi0317e6c2016-08-23 19:18:56 -0300466 bool enabled;
Shinya Kuribayashi81e798b2009-11-06 21:48:55 +0900467
Lucas De Marchi0317e6c2016-08-23 19:18:56 -0300468 enabled = dw_readl(dev, DW_IC_ENABLE_STATUS) & 1;
469
470 if (enabled) {
471 u32 ic_status;
472
473 /*
474 * Only disable adapter if ic_tar and ic_con can't be
475 * dynamically updated
476 */
477 ic_status = dw_readl(dev, DW_IC_STATUS);
478 if (!dev->dynamic_tar_update_enabled ||
479 (ic_status & DW_IC_STATUS_MST_ACTIVITY) ||
480 !(ic_status & DW_IC_STATUS_TFE)) {
481 __i2c_dw_enable_and_wait(dev, false);
482 enabled = false;
483 }
484 }
Shinya Kuribayashi81e798b2009-11-06 21:48:55 +0900485
Shinya Kuribayashi81e798b2009-11-06 21:48:55 +0900486 /* if the slave address is ten bit address, enable 10BITADDR */
Lucas De Marchi63d0f0a62016-08-23 19:18:55 -0300487 if (dev->dynamic_tar_update_enabled) {
Chew, Chiau Eebd63ace2013-09-27 02:57:35 +0800488 /*
489 * If I2C_DYNAMIC_TAR_UPDATE is set, the 10-bit addressing
Lucas De Marchi63d0f0a62016-08-23 19:18:55 -0300490 * mode has to be enabled via bit 12 of IC_TAR register,
491 * otherwise bit 4 of IC_CON is used.
Chew, Chiau Eebd63ace2013-09-27 02:57:35 +0800492 */
Lucas De Marchi63d0f0a62016-08-23 19:18:55 -0300493 if (msgs[dev->msg_write_idx].flags & I2C_M_TEN)
494 ic_tar = DW_IC_TAR_10BITADDR_MASTER;
Chew, Chiau Eebd63ace2013-09-27 02:57:35 +0800495 } else {
Lucas De Marchi63d0f0a62016-08-23 19:18:55 -0300496 u32 ic_con = dw_readl(dev, DW_IC_CON);
Chew, Chiau Eebd63ace2013-09-27 02:57:35 +0800497
Lucas De Marchi63d0f0a62016-08-23 19:18:55 -0300498 if (msgs[dev->msg_write_idx].flags & I2C_M_TEN)
499 ic_con |= DW_IC_CON_10BITADDR_MASTER;
500 else
501 ic_con &= ~DW_IC_CON_10BITADDR_MASTER;
502 dw_writel(dev, ic_con, DW_IC_CON);
503 }
Shinya Kuribayashi81e798b2009-11-06 21:48:55 +0900504
Chew, Chiau Eebd63ace2013-09-27 02:57:35 +0800505 /*
506 * Set the slave (target) address and enable 10-bit addressing mode
507 * if applicable.
508 */
509 dw_writel(dev, msgs[dev->msg_write_idx].addr | ic_tar, DW_IC_TAR);
510
Du, Wenkai47bb27e2014-04-10 23:03:19 +0000511 /* enforce disabled interrupts (due to HW issues) */
512 i2c_dw_disable_int(dev);
513
Lucas De Marchi0317e6c2016-08-23 19:18:56 -0300514 if (!enabled)
515 __i2c_dw_enable(dev, true);
Shinya Kuribayashi201d6a72009-11-06 21:50:40 +0900516
Mika Westerberg2a2d95e2013-05-13 00:54:30 +0000517 /* Clear and enable interrupts */
Jarkko Nikulac3356312015-08-31 17:31:28 +0300518 dw_readl(dev, DW_IC_CLR_INTR);
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700519 dw_writel(dev, DW_IC_INTR_DEFAULT_MASK, DW_IC_INTR_MASK);
Shinya Kuribayashi81e798b2009-11-06 21:48:55 +0900520}
521
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300522/*
Shinya Kuribayashi201d6a72009-11-06 21:50:40 +0900523 * Initiate (and continue) low level master read/write transaction.
524 * This function is only called from i2c_dw_isr, and pumping i2c_msg
525 * messages into the tx buffer. Even if the size of i2c_msg data is
526 * longer than the size of the tx buffer, it handles everything.
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300527 */
Jean Delvarebccd7802012-10-05 22:23:53 +0200528static void
Shinya Kuribayashie77cf232009-11-06 21:46:04 +0900529i2c_dw_xfer_msg(struct dw_i2c_dev *dev)
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300530{
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300531 struct i2c_msg *msgs = dev->msgs;
Shinya Kuribayashi81e798b2009-11-06 21:48:55 +0900532 u32 intr_mask;
Shinya Kuribayashiae722222009-11-06 21:49:39 +0900533 int tx_limit, rx_limit;
Shinya Kuribayashied5e1dd2009-11-06 21:43:52 +0900534 u32 addr = msgs[dev->msg_write_idx].addr;
535 u32 buf_len = dev->tx_buf_len;
Justin P. Mattock69932482011-07-26 23:06:29 -0700536 u8 *buf = dev->tx_buf;
Chew, Chiau Ee82564242013-06-21 15:05:28 +0800537 bool need_restart = false;
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300538
Shinya Kuribayashi201d6a72009-11-06 21:50:40 +0900539 intr_mask = DW_IC_INTR_DEFAULT_MASK;
Shinya Kuribayashic70c5cd2009-11-06 21:47:30 +0900540
Shinya Kuribayashi6d2ea482009-11-06 21:46:29 +0900541 for (; dev->msg_write_idx < dev->msgs_num; dev->msg_write_idx++) {
Shinya Kuribayashia0e06ea2009-11-06 21:52:22 +0900542 /*
543 * if target address has changed, we need to
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300544 * reprogram the target address in the i2c
545 * adapter when we are done with this transfer
546 */
Shinya Kuribayashi8f588e42009-11-06 21:51:18 +0900547 if (msgs[dev->msg_write_idx].addr != addr) {
548 dev_err(dev->dev,
549 "%s: invalid target address\n", __func__);
550 dev->msg_err = -EINVAL;
551 break;
552 }
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300553
554 if (msgs[dev->msg_write_idx].len == 0) {
555 dev_err(dev->dev,
556 "%s: invalid message length\n", __func__);
557 dev->msg_err = -EINVAL;
Shinya Kuribayashi8f588e42009-11-06 21:51:18 +0900558 break;
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300559 }
560
561 if (!(dev->status & STATUS_WRITE_IN_PROGRESS)) {
562 /* new i2c_msg */
Shinya Kuribayashi26ea15b2009-11-06 21:49:14 +0900563 buf = msgs[dev->msg_write_idx].buf;
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300564 buf_len = msgs[dev->msg_write_idx].len;
Chew, Chiau Ee82564242013-06-21 15:05:28 +0800565
566 /* If both IC_EMPTYFIFO_HOLD_MASTER_EN and
567 * IC_RESTART_EN are set, we must manually
568 * set restart bit between messages.
569 */
570 if ((dev->master_cfg & DW_IC_CON_RESTART_EN) &&
571 (dev->msg_write_idx > 0))
572 need_restart = true;
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300573 }
574
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700575 tx_limit = dev->tx_fifo_depth - dw_readl(dev, DW_IC_TXFLR);
576 rx_limit = dev->rx_fifo_depth - dw_readl(dev, DW_IC_RXFLR);
Shinya Kuribayashiae722222009-11-06 21:49:39 +0900577
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300578 while (buf_len > 0 && tx_limit > 0 && rx_limit > 0) {
Mika Westerberg17a76b42013-01-17 12:31:05 +0200579 u32 cmd = 0;
580
581 /*
582 * If IC_EMPTYFIFO_HOLD_MASTER_EN is set we must
583 * manually set the stop bit. However, it cannot be
584 * detected from the registers so we set it always
585 * when writing/reading the last byte.
586 */
587 if (dev->msg_write_idx == dev->msgs_num - 1 &&
588 buf_len == 1)
589 cmd |= BIT(9);
590
Chew, Chiau Ee82564242013-06-21 15:05:28 +0800591 if (need_restart) {
592 cmd |= BIT(10);
593 need_restart = false;
594 }
595
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300596 if (msgs[dev->msg_write_idx].flags & I2C_M_RD) {
Josef Ahmade6f34ce2013-04-19 17:28:10 +0100597
598 /* avoid rx buffer overrun */
599 if (rx_limit - dev->rx_outstanding <= 0)
600 break;
601
Mika Westerberg17a76b42013-01-17 12:31:05 +0200602 dw_writel(dev, cmd | 0x100, DW_IC_DATA_CMD);
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300603 rx_limit--;
Josef Ahmade6f34ce2013-04-19 17:28:10 +0100604 dev->rx_outstanding++;
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300605 } else
Mika Westerberg17a76b42013-01-17 12:31:05 +0200606 dw_writel(dev, cmd | *buf++, DW_IC_DATA_CMD);
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300607 tx_limit--; buf_len--;
608 }
Shinya Kuribayashic70c5cd2009-11-06 21:47:30 +0900609
Shinya Kuribayashi26ea15b2009-11-06 21:49:14 +0900610 dev->tx_buf = buf;
Shinya Kuribayashic70c5cd2009-11-06 21:47:30 +0900611 dev->tx_buf_len = buf_len;
612
613 if (buf_len > 0) {
614 /* more bytes to be written */
Shinya Kuribayashic70c5cd2009-11-06 21:47:30 +0900615 dev->status |= STATUS_WRITE_IN_PROGRESS;
616 break;
Shinya Kuribayashi69151e52009-11-06 21:51:00 +0900617 } else
Shinya Kuribayashic70c5cd2009-11-06 21:47:30 +0900618 dev->status &= ~STATUS_WRITE_IN_PROGRESS;
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300619 }
620
Shinya Kuribayashi69151e52009-11-06 21:51:00 +0900621 /*
622 * If i2c_msg index search is completed, we don't need TX_EMPTY
623 * interrupt any more.
624 */
625 if (dev->msg_write_idx == dev->msgs_num)
626 intr_mask &= ~DW_IC_INTR_TX_EMPTY;
627
Shinya Kuribayashi8f588e42009-11-06 21:51:18 +0900628 if (dev->msg_err)
629 intr_mask = 0;
630
Dirk Brandewie2373f6b2011-10-29 10:57:23 +0100631 dw_writel(dev, intr_mask, DW_IC_INTR_MASK);
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300632}
633
634static void
Shinya Kuribayashi78839bd2009-11-06 21:45:39 +0900635i2c_dw_read(struct dw_i2c_dev *dev)
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300636{
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300637 struct i2c_msg *msgs = dev->msgs;
Shinya Kuribayashiae722222009-11-06 21:49:39 +0900638 int rx_valid;
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300639
Shinya Kuribayashi6d2ea482009-11-06 21:46:29 +0900640 for (; dev->msg_read_idx < dev->msgs_num; dev->msg_read_idx++) {
Shinya Kuribayashied5e1dd2009-11-06 21:43:52 +0900641 u32 len;
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300642 u8 *buf;
643
644 if (!(msgs[dev->msg_read_idx].flags & I2C_M_RD))
645 continue;
646
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300647 if (!(dev->status & STATUS_READ_IN_PROGRESS)) {
648 len = msgs[dev->msg_read_idx].len;
649 buf = msgs[dev->msg_read_idx].buf;
650 } else {
651 len = dev->rx_buf_len;
652 buf = dev->rx_buf;
653 }
654
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700655 rx_valid = dw_readl(dev, DW_IC_RXFLR);
Shinya Kuribayashiae722222009-11-06 21:49:39 +0900656
Josef Ahmade6f34ce2013-04-19 17:28:10 +0100657 for (; len > 0 && rx_valid > 0; len--, rx_valid--) {
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700658 *buf++ = dw_readl(dev, DW_IC_DATA_CMD);
Josef Ahmade6f34ce2013-04-19 17:28:10 +0100659 dev->rx_outstanding--;
660 }
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300661
662 if (len > 0) {
663 dev->status |= STATUS_READ_IN_PROGRESS;
664 dev->rx_buf_len = len;
665 dev->rx_buf = buf;
666 return;
667 } else
668 dev->status &= ~STATUS_READ_IN_PROGRESS;
669 }
670}
671
Shinya Kuribayashice6eb572009-11-06 21:51:57 +0900672static int i2c_dw_handle_tx_abort(struct dw_i2c_dev *dev)
673{
674 unsigned long abort_source = dev->abort_source;
675 int i;
676
Shinya Kuribayashi6d1ea0f2009-11-16 20:40:14 +0900677 if (abort_source & DW_IC_TX_ABRT_NOACK) {
Akinobu Mita984b3f52010-03-05 13:41:37 -0800678 for_each_set_bit(i, &abort_source, ARRAY_SIZE(abort_sources))
Shinya Kuribayashi6d1ea0f2009-11-16 20:40:14 +0900679 dev_dbg(dev->dev,
680 "%s: %s\n", __func__, abort_sources[i]);
681 return -EREMOTEIO;
682 }
683
Akinobu Mita984b3f52010-03-05 13:41:37 -0800684 for_each_set_bit(i, &abort_source, ARRAY_SIZE(abort_sources))
Shinya Kuribayashice6eb572009-11-06 21:51:57 +0900685 dev_err(dev->dev, "%s: %s\n", __func__, abort_sources[i]);
686
687 if (abort_source & DW_IC_TX_ARB_LOST)
688 return -EAGAIN;
Shinya Kuribayashice6eb572009-11-06 21:51:57 +0900689 else if (abort_source & DW_IC_TX_ABRT_GCALL_READ)
690 return -EINVAL; /* wrong msgs[] data */
691 else
692 return -EIO;
693}
694
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300695/*
Lucas De Marchi0317e6c2016-08-23 19:18:56 -0300696 * Prepare controller for a transaction and start transfer by calling
697 * i2c_dw_xfer_init()
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300698 */
Jarkko Nikulad80d1342015-10-12 16:55:35 +0300699static int
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300700i2c_dw_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], int num)
701{
702 struct dw_i2c_dev *dev = i2c_get_adapdata(adap);
703 int ret;
704
705 dev_dbg(dev->dev, "%s: msgs: %d\n", __func__, num);
706
Dirk Brandewie18dbdda2011-10-06 11:26:36 -0700707 pm_runtime_get_sync(dev->dev);
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300708
Wolfram Sang16735d02013-11-14 14:32:02 -0800709 reinit_completion(&dev->cmd_complete);
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300710 dev->msgs = msgs;
711 dev->msgs_num = num;
712 dev->cmd_err = 0;
713 dev->msg_write_idx = 0;
714 dev->msg_read_idx = 0;
715 dev->msg_err = 0;
716 dev->status = STATUS_IDLE;
Shinya Kuribayashice6eb572009-11-06 21:51:57 +0900717 dev->abort_source = 0;
Josef Ahmade6f34ce2013-04-19 17:28:10 +0100718 dev->rx_outstanding = 0;
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300719
Lucas De Marchi8c5660b2016-08-23 19:18:54 -0300720 ret = i2c_dw_acquire_lock(dev);
721 if (ret)
722 goto done_nolock;
David Boxc0601d22015-01-15 01:12:16 -0800723
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300724 ret = i2c_dw_wait_bus_not_busy(dev);
725 if (ret < 0)
726 goto done;
727
728 /* start the transfers */
Shinya Kuribayashi81e798b2009-11-06 21:48:55 +0900729 i2c_dw_xfer_init(dev);
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300730
731 /* wait for tx to complete */
Weifeng Voond0bcd8d2016-06-17 09:46:35 +0800732 if (!wait_for_completion_timeout(&dev->cmd_complete, adap->timeout)) {
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300733 dev_err(dev->dev, "controller timed out\n");
Christian Ruppert38d7fad2013-06-07 10:51:23 +0200734 /* i2c_dw_init implicitly disables the adapter */
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300735 i2c_dw_init(dev);
736 ret = -ETIMEDOUT;
737 goto done;
Mika Westerberge42dba52013-05-22 13:03:11 +0300738 }
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300739
740 if (dev->msg_err) {
741 ret = dev->msg_err;
742 goto done;
743 }
744
745 /* no error */
746 if (likely(!dev->cmd_err)) {
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300747 ret = num;
748 goto done;
749 }
750
751 /* We have an error */
752 if (dev->cmd_err == DW_IC_ERR_TX_ABRT) {
Shinya Kuribayashice6eb572009-11-06 21:51:57 +0900753 ret = i2c_dw_handle_tx_abort(dev);
754 goto done;
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300755 }
756 ret = -EIO;
757
758done:
Lucas De Marchi8c5660b2016-08-23 19:18:54 -0300759 i2c_dw_release_lock(dev);
David Boxc0601d22015-01-15 01:12:16 -0800760
761done_nolock:
Mika Westerberg43452332013-04-10 00:36:42 +0000762 pm_runtime_mark_last_busy(dev->dev);
763 pm_runtime_put_autosuspend(dev->dev);
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300764
765 return ret;
766}
767
Jarkko Nikulad80d1342015-10-12 16:55:35 +0300768static u32 i2c_dw_func(struct i2c_adapter *adap)
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300769{
Dirk Brandewie2fa83262011-10-06 11:26:31 -0700770 struct dw_i2c_dev *dev = i2c_get_adapdata(adap);
771 return dev->functionality;
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300772}
Jarkko Nikulad80d1342015-10-12 16:55:35 +0300773
774static struct i2c_algorithm i2c_dw_algo = {
775 .master_xfer = i2c_dw_xfer,
776 .functionality = i2c_dw_func,
777};
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300778
Shinya Kuribayashie28000a2009-11-06 21:44:37 +0900779static u32 i2c_dw_read_clear_intrbits(struct dw_i2c_dev *dev)
780{
781 u32 stat;
782
783 /*
784 * The IC_INTR_STAT register just indicates "enabled" interrupts.
785 * Ths unmasked raw version of interrupt status bits are available
786 * in the IC_RAW_INTR_STAT register.
787 *
788 * That is,
Dirk Brandewie2373f6b2011-10-29 10:57:23 +0100789 * stat = dw_readl(IC_INTR_STAT);
Shinya Kuribayashie28000a2009-11-06 21:44:37 +0900790 * equals to,
Dirk Brandewie2373f6b2011-10-29 10:57:23 +0100791 * stat = dw_readl(IC_RAW_INTR_STAT) & dw_readl(IC_INTR_MASK);
Shinya Kuribayashie28000a2009-11-06 21:44:37 +0900792 *
793 * The raw version might be useful for debugging purposes.
794 */
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700795 stat = dw_readl(dev, DW_IC_INTR_STAT);
Shinya Kuribayashie28000a2009-11-06 21:44:37 +0900796
797 /*
798 * Do not use the IC_CLR_INTR register to clear interrupts, or
799 * you'll miss some interrupts, triggered during the period from
Dirk Brandewie2373f6b2011-10-29 10:57:23 +0100800 * dw_readl(IC_INTR_STAT) to dw_readl(IC_CLR_INTR).
Shinya Kuribayashie28000a2009-11-06 21:44:37 +0900801 *
802 * Instead, use the separately-prepared IC_CLR_* registers.
803 */
804 if (stat & DW_IC_INTR_RX_UNDER)
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700805 dw_readl(dev, DW_IC_CLR_RX_UNDER);
Shinya Kuribayashie28000a2009-11-06 21:44:37 +0900806 if (stat & DW_IC_INTR_RX_OVER)
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700807 dw_readl(dev, DW_IC_CLR_RX_OVER);
Shinya Kuribayashie28000a2009-11-06 21:44:37 +0900808 if (stat & DW_IC_INTR_TX_OVER)
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700809 dw_readl(dev, DW_IC_CLR_TX_OVER);
Shinya Kuribayashie28000a2009-11-06 21:44:37 +0900810 if (stat & DW_IC_INTR_RD_REQ)
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700811 dw_readl(dev, DW_IC_CLR_RD_REQ);
Shinya Kuribayashie28000a2009-11-06 21:44:37 +0900812 if (stat & DW_IC_INTR_TX_ABRT) {
813 /*
814 * The IC_TX_ABRT_SOURCE register is cleared whenever
815 * the IC_CLR_TX_ABRT is read. Preserve it beforehand.
816 */
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700817 dev->abort_source = dw_readl(dev, DW_IC_TX_ABRT_SOURCE);
818 dw_readl(dev, DW_IC_CLR_TX_ABRT);
Shinya Kuribayashie28000a2009-11-06 21:44:37 +0900819 }
820 if (stat & DW_IC_INTR_RX_DONE)
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700821 dw_readl(dev, DW_IC_CLR_RX_DONE);
Shinya Kuribayashie28000a2009-11-06 21:44:37 +0900822 if (stat & DW_IC_INTR_ACTIVITY)
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700823 dw_readl(dev, DW_IC_CLR_ACTIVITY);
Shinya Kuribayashie28000a2009-11-06 21:44:37 +0900824 if (stat & DW_IC_INTR_STOP_DET)
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700825 dw_readl(dev, DW_IC_CLR_STOP_DET);
Shinya Kuribayashie28000a2009-11-06 21:44:37 +0900826 if (stat & DW_IC_INTR_START_DET)
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700827 dw_readl(dev, DW_IC_CLR_START_DET);
Shinya Kuribayashie28000a2009-11-06 21:44:37 +0900828 if (stat & DW_IC_INTR_GEN_CALL)
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700829 dw_readl(dev, DW_IC_CLR_GEN_CALL);
Shinya Kuribayashie28000a2009-11-06 21:44:37 +0900830
831 return stat;
832}
833
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300834/*
835 * Interrupt service routine. This gets called whenever an I2C interrupt
836 * occurs.
837 */
Jarkko Nikulad80d1342015-10-12 16:55:35 +0300838static irqreturn_t i2c_dw_isr(int this_irq, void *dev_id)
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300839{
840 struct dw_i2c_dev *dev = dev_id;
Dirk Brandewieaf06cf62011-10-06 11:26:33 -0700841 u32 stat, enabled;
842
843 enabled = dw_readl(dev, DW_IC_ENABLE);
844 stat = dw_readl(dev, DW_IC_RAW_INTR_STAT);
Jarkko Nikulafb427462015-08-07 14:53:03 +0300845 dev_dbg(dev->dev, "%s: enabled=%#x stat=%#x\n", __func__, enabled, stat);
Dirk Brandewieaf06cf62011-10-06 11:26:33 -0700846 if (!enabled || !(stat & ~DW_IC_INTR_ACTIVITY))
847 return IRQ_NONE;
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300848
Shinya Kuribayashie28000a2009-11-06 21:44:37 +0900849 stat = i2c_dw_read_clear_intrbits(dev);
Shinya Kuribayashie28000a2009-11-06 21:44:37 +0900850
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300851 if (stat & DW_IC_INTR_TX_ABRT) {
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300852 dev->cmd_err |= DW_IC_ERR_TX_ABRT;
853 dev->status = STATUS_IDLE;
Shinya Kuribayashi597fe312009-11-06 21:51:36 +0900854
855 /*
856 * Anytime TX_ABRT is set, the contents of the tx/rx
857 * buffers are flushed. Make sure to skip them.
858 */
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700859 dw_writel(dev, 0, DW_IC_INTR_MASK);
Shinya Kuribayashi597fe312009-11-06 21:51:36 +0900860 goto tx_aborted;
Shinya Kuribayashi07745392009-11-06 21:47:51 +0900861 }
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300862
Shinya Kuribayashi21a89d42009-11-06 21:48:33 +0900863 if (stat & DW_IC_INTR_RX_FULL)
Shinya Kuribayashi07745392009-11-06 21:47:51 +0900864 i2c_dw_read(dev);
Shinya Kuribayashi21a89d42009-11-06 21:48:33 +0900865
866 if (stat & DW_IC_INTR_TX_EMPTY)
Shinya Kuribayashi07745392009-11-06 21:47:51 +0900867 i2c_dw_xfer_msg(dev);
Shinya Kuribayashi07745392009-11-06 21:47:51 +0900868
869 /*
870 * No need to modify or disable the interrupt mask here.
871 * i2c_dw_xfer_msg() will take care of it according to
872 * the current transmit status.
873 */
874
Shinya Kuribayashi597fe312009-11-06 21:51:36 +0900875tx_aborted:
Lucas De Marchi0317e6c2016-08-23 19:18:56 -0300876 if ((stat & (DW_IC_INTR_TX_ABRT | DW_IC_INTR_STOP_DET))
877 || dev->msg_err) {
878 /*
879 * We must disable interruts before returning and signaling
880 * the end of the current transfer. Otherwise the hardware
881 * might continue generating interrupts for non-existent
882 * transfers.
883 */
884 i2c_dw_disable_int(dev);
885 dw_readl(dev, DW_IC_CLR_INTR);
886
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300887 complete(&dev->cmd_complete);
Lucas De Marchi0317e6c2016-08-23 19:18:56 -0300888 } else if (unlikely(dev->accessor_flags & ACCESS_INTR_MASK)) {
Xiangliang Yu2d244c82015-12-11 20:02:53 +0800889 /* workaround to trigger pending interrupt */
890 stat = dw_readl(dev, DW_IC_INTR_MASK);
891 i2c_dw_disable_int(dev);
892 dw_writel(dev, stat, DW_IC_INTR_MASK);
893 }
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300894
895 return IRQ_HANDLED;
896}
Dirk Brandewief3fa9f32011-10-06 11:26:34 -0700897
Dirk Brandewief3fa9f32011-10-06 11:26:34 -0700898void i2c_dw_disable(struct dw_i2c_dev *dev)
899{
Dirk Brandewief3fa9f32011-10-06 11:26:34 -0700900 /* Disable controller */
José Roberto de Souza2702ea72016-08-23 19:18:53 -0300901 __i2c_dw_enable_and_wait(dev, false);
Dirk Brandewief3fa9f32011-10-06 11:26:34 -0700902
903 /* Disable all interupts */
904 dw_writel(dev, 0, DW_IC_INTR_MASK);
905 dw_readl(dev, DW_IC_CLR_INTR);
906}
Axel Line68bb912012-09-10 10:14:02 +0200907EXPORT_SYMBOL_GPL(i2c_dw_disable);
Dirk Brandewief3fa9f32011-10-06 11:26:34 -0700908
Dirk Brandewief3fa9f32011-10-06 11:26:34 -0700909void i2c_dw_disable_int(struct dw_i2c_dev *dev)
910{
911 dw_writel(dev, 0, DW_IC_INTR_MASK);
912}
Axel Line68bb912012-09-10 10:14:02 +0200913EXPORT_SYMBOL_GPL(i2c_dw_disable_int);
Dirk Brandewief3fa9f32011-10-06 11:26:34 -0700914
915u32 i2c_dw_read_comp_param(struct dw_i2c_dev *dev)
916{
917 return dw_readl(dev, DW_IC_COMP_PARAM_1);
918}
Axel Line68bb912012-09-10 10:14:02 +0200919EXPORT_SYMBOL_GPL(i2c_dw_read_comp_param);
Mika Westerberg9dd31622013-01-17 12:31:04 +0200920
Jarkko Nikulad80d1342015-10-12 16:55:35 +0300921int i2c_dw_probe(struct dw_i2c_dev *dev)
922{
923 struct i2c_adapter *adap = &dev->adapter;
924 int r;
Lucas De Marchi63d0f0a62016-08-23 19:18:55 -0300925 u32 reg;
Jarkko Nikulad80d1342015-10-12 16:55:35 +0300926
927 init_completion(&dev->cmd_complete);
Jarkko Nikulad80d1342015-10-12 16:55:35 +0300928
929 r = i2c_dw_init(dev);
930 if (r)
931 return r;
932
Lucas De Marchi63d0f0a62016-08-23 19:18:55 -0300933 r = i2c_dw_acquire_lock(dev);
934 if (r)
935 return r;
936
937 /*
938 * Test if dynamic TAR update is enabled in this controller by writing
939 * to IC_10BITADDR_MASTER field in IC_CON: when it is enabled this
940 * field is read-only so it should not succeed
941 */
942 reg = dw_readl(dev, DW_IC_CON);
943 dw_writel(dev, reg ^ DW_IC_CON_10BITADDR_MASTER, DW_IC_CON);
944
945 if ((dw_readl(dev, DW_IC_CON) & DW_IC_CON_10BITADDR_MASTER) ==
946 (reg & DW_IC_CON_10BITADDR_MASTER)) {
947 dev->dynamic_tar_update_enabled = true;
948 dev_dbg(dev->dev, "Dynamic TAR update enabled");
949 }
950
951 i2c_dw_release_lock(dev);
952
Jarkko Nikulad80d1342015-10-12 16:55:35 +0300953 snprintf(adap->name, sizeof(adap->name),
954 "Synopsys DesignWare I2C adapter");
Baruch Siach8d22f302015-12-23 18:43:24 +0200955 adap->retries = 3;
Jarkko Nikulad80d1342015-10-12 16:55:35 +0300956 adap->algo = &i2c_dw_algo;
957 adap->dev.parent = dev->dev;
958 i2c_set_adapdata(adap, dev);
959
960 i2c_dw_disable_int(dev);
Andy Shevchenko08c6e8c2016-01-15 22:02:12 +0200961 r = devm_request_irq(dev->dev, dev->irq, i2c_dw_isr,
962 IRQF_SHARED | IRQF_COND_SUSPEND,
Jarkko Nikulad80d1342015-10-12 16:55:35 +0300963 dev_name(dev->dev), dev);
964 if (r) {
965 dev_err(dev->dev, "failure requesting irq %i: %d\n",
966 dev->irq, r);
967 return r;
968 }
969
Jarkko Nikulacd998de2016-02-11 16:36:03 +0200970 /*
971 * Increment PM usage count during adapter registration in order to
972 * avoid possible spurious runtime suspend when adapter device is
973 * registered to the device core and immediate resume in case bus has
974 * registered I2C slaves that do I2C transfers in their probe.
975 */
976 pm_runtime_get_noresume(dev->dev);
Jarkko Nikulad80d1342015-10-12 16:55:35 +0300977 r = i2c_add_numbered_adapter(adap);
978 if (r)
979 dev_err(dev->dev, "failure adding adapter: %d\n", r);
Jarkko Nikulacd998de2016-02-11 16:36:03 +0200980 pm_runtime_put_noidle(dev->dev);
Jarkko Nikulad80d1342015-10-12 16:55:35 +0300981
982 return r;
983}
984EXPORT_SYMBOL_GPL(i2c_dw_probe);
985
Mika Westerberg9dd31622013-01-17 12:31:04 +0200986MODULE_DESCRIPTION("Synopsys DesignWare I2C bus adapter core");
987MODULE_LICENSE("GPL");