blob: 809f4d4e93a0a70c7fe211b200afa7fd6fc15b14 [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 Westerberg3ca4ed82013-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
Jarkko Nikula89119f02016-11-25 17:22:27 +020094#define DW_IC_STATUS_ACTIVITY 0x1
Dirk Brandewief3fa9f32011-10-06 11:26:34 -070095
Jarkko Nikula171e23e2016-09-29 16:04:59 +030096#define DW_IC_SDA_HOLD_RX_SHIFT 16
97#define DW_IC_SDA_HOLD_RX_MASK GENMASK(23, DW_IC_SDA_HOLD_RX_SHIFT)
98
Dirk Brandewief3fa9f32011-10-06 11:26:34 -070099#define DW_IC_ERR_TX_ABRT 0x1
100
Chew, Chiau Eebd63ace2013-09-27 02:57:35 +0800101#define DW_IC_TAR_10BITADDR_MASTER BIT(12)
102
Weifeng Voonb6e67142016-08-12 17:02:51 +0300103#define DW_IC_COMP_PARAM_1_SPEED_MODE_HIGH (BIT(2) | BIT(3))
104#define DW_IC_COMP_PARAM_1_SPEED_MODE_MASK GENMASK(3, 2)
105
Dirk Brandewief3fa9f32011-10-06 11:26:34 -0700106/*
107 * status codes
108 */
109#define STATUS_IDLE 0x0
110#define STATUS_WRITE_IN_PROGRESS 0x1
111#define STATUS_READ_IN_PROGRESS 0x2
112
113#define TIMEOUT 20 /* ms */
114
115/*
116 * hardware abort codes from the DW_IC_TX_ABRT_SOURCE register
117 *
118 * only expected abort codes are listed here
119 * refer to the datasheet for the full list
120 */
121#define ABRT_7B_ADDR_NOACK 0
122#define ABRT_10ADDR1_NOACK 1
123#define ABRT_10ADDR2_NOACK 2
124#define ABRT_TXDATA_NOACK 3
125#define ABRT_GCALL_NOACK 4
126#define ABRT_GCALL_READ 5
127#define ABRT_SBYTE_ACKDET 7
128#define ABRT_SBYTE_NORSTRT 9
129#define ABRT_10B_RD_NORSTRT 10
130#define ABRT_MASTER_DIS 11
131#define ARB_LOST 12
132
133#define DW_IC_TX_ABRT_7B_ADDR_NOACK (1UL << ABRT_7B_ADDR_NOACK)
134#define DW_IC_TX_ABRT_10ADDR1_NOACK (1UL << ABRT_10ADDR1_NOACK)
135#define DW_IC_TX_ABRT_10ADDR2_NOACK (1UL << ABRT_10ADDR2_NOACK)
136#define DW_IC_TX_ABRT_TXDATA_NOACK (1UL << ABRT_TXDATA_NOACK)
137#define DW_IC_TX_ABRT_GCALL_NOACK (1UL << ABRT_GCALL_NOACK)
138#define DW_IC_TX_ABRT_GCALL_READ (1UL << ABRT_GCALL_READ)
139#define DW_IC_TX_ABRT_SBYTE_ACKDET (1UL << ABRT_SBYTE_ACKDET)
140#define DW_IC_TX_ABRT_SBYTE_NORSTRT (1UL << ABRT_SBYTE_NORSTRT)
141#define DW_IC_TX_ABRT_10B_RD_NORSTRT (1UL << ABRT_10B_RD_NORSTRT)
142#define DW_IC_TX_ABRT_MASTER_DIS (1UL << ABRT_MASTER_DIS)
143#define DW_IC_TX_ARB_LOST (1UL << ARB_LOST)
144
145#define DW_IC_TX_ABRT_NOACK (DW_IC_TX_ABRT_7B_ADDR_NOACK | \
146 DW_IC_TX_ABRT_10ADDR1_NOACK | \
147 DW_IC_TX_ABRT_10ADDR2_NOACK | \
148 DW_IC_TX_ABRT_TXDATA_NOACK | \
149 DW_IC_TX_ABRT_GCALL_NOACK)
150
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300151static char *abort_sources[] = {
Shinya Kuribayashia0e06ea2009-11-06 21:52:22 +0900152 [ABRT_7B_ADDR_NOACK] =
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300153 "slave address not acknowledged (7bit mode)",
Shinya Kuribayashia0e06ea2009-11-06 21:52:22 +0900154 [ABRT_10ADDR1_NOACK] =
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300155 "first address byte not acknowledged (10bit mode)",
Shinya Kuribayashia0e06ea2009-11-06 21:52:22 +0900156 [ABRT_10ADDR2_NOACK] =
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300157 "second address byte not acknowledged (10bit mode)",
Shinya Kuribayashia0e06ea2009-11-06 21:52:22 +0900158 [ABRT_TXDATA_NOACK] =
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300159 "data not acknowledged",
Shinya Kuribayashia0e06ea2009-11-06 21:52:22 +0900160 [ABRT_GCALL_NOACK] =
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300161 "no acknowledgement for a general call",
Shinya Kuribayashia0e06ea2009-11-06 21:52:22 +0900162 [ABRT_GCALL_READ] =
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300163 "read after general call",
Shinya Kuribayashia0e06ea2009-11-06 21:52:22 +0900164 [ABRT_SBYTE_ACKDET] =
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300165 "start byte acknowledged",
Shinya Kuribayashia0e06ea2009-11-06 21:52:22 +0900166 [ABRT_SBYTE_NORSTRT] =
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300167 "trying to send start byte when restart is disabled",
Shinya Kuribayashia0e06ea2009-11-06 21:52:22 +0900168 [ABRT_10B_RD_NORSTRT] =
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300169 "trying to read when restart is disabled (10bit mode)",
Shinya Kuribayashia0e06ea2009-11-06 21:52:22 +0900170 [ABRT_MASTER_DIS] =
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300171 "trying to use disabled adapter",
Shinya Kuribayashia0e06ea2009-11-06 21:52:22 +0900172 [ARB_LOST] =
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300173 "lost arbitration",
174};
175
Jarkko Nikula8a437452015-08-31 17:31:31 +0300176static u32 dw_readl(struct dw_i2c_dev *dev, int offset)
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700177{
Stefan Roesea8a9f3f2012-04-18 15:01:41 +0200178 u32 value;
Jean-Hugues Deschenes18c40892011-10-06 11:26:27 -0700179
Stefan Roesea8a9f3f2012-04-18 15:01:41 +0200180 if (dev->accessor_flags & ACCESS_16BIT)
Jisheng Zhang67105c52014-12-11 14:26:41 +0800181 value = readw_relaxed(dev->base + offset) |
182 (readw_relaxed(dev->base + offset + 2) << 16);
Stefan Roesea8a9f3f2012-04-18 15:01:41 +0200183 else
Jisheng Zhang67105c52014-12-11 14:26:41 +0800184 value = readl_relaxed(dev->base + offset);
Stefan Roesea8a9f3f2012-04-18 15:01:41 +0200185
186 if (dev->accessor_flags & ACCESS_SWAP)
Jean-Hugues Deschenes18c40892011-10-06 11:26:27 -0700187 return swab32(value);
188 else
189 return value;
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700190}
191
Jarkko Nikula8a437452015-08-31 17:31:31 +0300192static void dw_writel(struct dw_i2c_dev *dev, u32 b, int offset)
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700193{
Stefan Roesea8a9f3f2012-04-18 15:01:41 +0200194 if (dev->accessor_flags & ACCESS_SWAP)
Jean-Hugues Deschenes18c40892011-10-06 11:26:27 -0700195 b = swab32(b);
196
Stefan Roesea8a9f3f2012-04-18 15:01:41 +0200197 if (dev->accessor_flags & ACCESS_16BIT) {
Jisheng Zhang67105c52014-12-11 14:26:41 +0800198 writew_relaxed((u16)b, dev->base + offset);
199 writew_relaxed((u16)(b >> 16), dev->base + offset + 2);
Stefan Roesea8a9f3f2012-04-18 15:01:41 +0200200 } else {
Jisheng Zhang67105c52014-12-11 14:26:41 +0800201 writel_relaxed(b, dev->base + offset);
Stefan Roesea8a9f3f2012-04-18 15:01:41 +0200202 }
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700203}
204
Shinya Kuribayashid60c7e82009-11-06 21:47:01 +0900205static u32
206i2c_dw_scl_hcnt(u32 ic_clk, u32 tSYMBOL, u32 tf, int cond, int offset)
207{
208 /*
209 * DesignWare I2C core doesn't seem to have solid strategy to meet
210 * the tHD;STA timing spec. Configuring _HCNT based on tHIGH spec
211 * will result in violation of the tHD;STA spec.
212 */
213 if (cond)
214 /*
215 * Conditional expression:
216 *
217 * IC_[FS]S_SCL_HCNT + (1+4+3) >= IC_CLK * tHIGH
218 *
219 * This is based on the DW manuals, and represents an ideal
220 * configuration. The resulting I2C bus speed will be
221 * faster than any of the others.
222 *
223 * If your hardware is free from tHD;STA issue, try this one.
224 */
Romain Baeriswyl64682762014-01-20 17:43:43 +0100225 return (ic_clk * tSYMBOL + 500000) / 1000000 - 8 + offset;
Shinya Kuribayashid60c7e82009-11-06 21:47:01 +0900226 else
227 /*
228 * Conditional expression:
229 *
230 * IC_[FS]S_SCL_HCNT + 3 >= IC_CLK * (tHD;STA + tf)
231 *
232 * This is just experimental rule; the tHD;STA period turned
233 * out to be proportinal to (_HCNT + 3). With this setting,
234 * we could meet both tHIGH and tHD;STA timing specs.
235 *
236 * If unsure, you'd better to take this alternative.
237 *
238 * The reason why we need to take into account "tf" here,
239 * is the same as described in i2c_dw_scl_lcnt().
240 */
Romain Baeriswyl64682762014-01-20 17:43:43 +0100241 return (ic_clk * (tSYMBOL + tf) + 500000) / 1000000
242 - 3 + offset;
Shinya Kuribayashid60c7e82009-11-06 21:47:01 +0900243}
244
245static u32 i2c_dw_scl_lcnt(u32 ic_clk, u32 tLOW, u32 tf, int offset)
246{
247 /*
248 * Conditional expression:
249 *
250 * IC_[FS]S_SCL_LCNT + 1 >= IC_CLK * (tLOW + tf)
251 *
252 * DW I2C core starts counting the SCL CNTs for the LOW period
253 * of the SCL clock (tLOW) as soon as it pulls the SCL line.
254 * In order to meet the tLOW timing spec, we need to take into
255 * account the fall time of SCL signal (tf). Default tf value
256 * should be 0.3 us, for safety.
257 */
Romain Baeriswyl64682762014-01-20 17:43:43 +0100258 return ((ic_clk * (tLOW + tf) + 500000) / 1000000) - 1 + offset;
Shinya Kuribayashid60c7e82009-11-06 21:47:01 +0900259}
260
Mika Westerberg3ca4ed82013-04-10 00:36:40 +0000261static void __i2c_dw_enable(struct dw_i2c_dev *dev, bool enable)
262{
José Roberto de Souza2702ea72016-08-23 19:18:53 -0300263 dw_writel(dev, enable, DW_IC_ENABLE);
264}
265
266static void __i2c_dw_enable_and_wait(struct dw_i2c_dev *dev, bool enable)
267{
Mika Westerberg3ca4ed82013-04-10 00:36:40 +0000268 int timeout = 100;
269
270 do {
José Roberto de Souza2702ea72016-08-23 19:18:53 -0300271 __i2c_dw_enable(dev, enable);
Mika Westerberg3ca4ed82013-04-10 00:36:40 +0000272 if ((dw_readl(dev, DW_IC_ENABLE_STATUS) & 1) == enable)
273 return;
274
275 /*
276 * Wait 10 times the signaling period of the highest I2C
277 * transfer supported by the driver (for 400KHz this is
278 * 25us) as described in the DesignWare I2C databook.
279 */
280 usleep_range(25, 250);
281 } while (timeout--);
282
283 dev_warn(dev->dev, "timeout in %sabling adapter\n",
284 enable ? "en" : "dis");
285}
286
Suravee Suthikulpanitb33af112016-01-04 09:17:35 -0600287static unsigned long i2c_dw_clk_rate(struct dw_i2c_dev *dev)
288{
289 /*
290 * Clock is not necessary if we got LCNT/HCNT values directly from
291 * the platform code.
292 */
293 if (WARN_ON_ONCE(!dev->get_clk_rate_khz))
294 return 0;
295 return dev->get_clk_rate_khz(dev);
296}
297
Lucas De Marchi8c5660b2016-08-23 19:18:54 -0300298static int i2c_dw_acquire_lock(struct dw_i2c_dev *dev)
299{
300 int ret;
301
302 if (!dev->acquire_lock)
303 return 0;
304
305 ret = dev->acquire_lock(dev);
306 if (!ret)
307 return 0;
308
309 dev_err(dev->dev, "couldn't acquire bus ownership\n");
310
311 return ret;
312}
313
314static void i2c_dw_release_lock(struct dw_i2c_dev *dev)
315{
316 if (dev->release_lock)
317 dev->release_lock(dev);
318}
319
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300320/**
321 * i2c_dw_init() - initialize the designware i2c master hardware
322 * @dev: device private data
323 *
324 * This functions configures and enables the I2C master.
325 * This function is called during I2C init function, and in case of timeout at
326 * run time.
327 */
Dirk Brandewie2373f6b2011-10-29 10:57:23 +0100328int i2c_dw_init(struct dw_i2c_dev *dev)
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300329{
Dirk Brandewiee18563f2011-10-06 11:26:32 -0700330 u32 hcnt, lcnt;
Weifeng Voonb6e67142016-08-12 17:02:51 +0300331 u32 reg, comp_param1;
Romain Baeriswyl64682762014-01-20 17:43:43 +0100332 u32 sda_falling_time, scl_falling_time;
David Boxc0601d22015-01-15 01:12:16 -0800333 int ret;
334
Lucas De Marchi8c5660b2016-08-23 19:18:54 -0300335 ret = i2c_dw_acquire_lock(dev);
336 if (ret)
337 return ret;
Dirk Brandewie4a423a82011-10-06 11:26:28 -0700338
Dirk Brandewie4a423a82011-10-06 11:26:28 -0700339 reg = dw_readl(dev, DW_IC_COMP_TYPE);
340 if (reg == ___constant_swab32(DW_IC_COMP_TYPE_VALUE)) {
Stefan Roesea8a9f3f2012-04-18 15:01:41 +0200341 /* Configure register endianess access */
342 dev->accessor_flags |= ACCESS_SWAP;
343 } else if (reg == (DW_IC_COMP_TYPE_VALUE & 0x0000ffff)) {
344 /* Configure register access mode 16bit */
345 dev->accessor_flags |= ACCESS_16BIT;
346 } else if (reg != DW_IC_COMP_TYPE_VALUE) {
Dirk Brandewie4a423a82011-10-06 11:26:28 -0700347 dev_err(dev->dev, "Unknown Synopsys component type: "
348 "0x%08x\n", reg);
Lucas De Marchi8c5660b2016-08-23 19:18:54 -0300349 i2c_dw_release_lock(dev);
Dirk Brandewie4a423a82011-10-06 11:26:28 -0700350 return -ENODEV;
351 }
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300352
Weifeng Voonb6e67142016-08-12 17:02:51 +0300353 comp_param1 = dw_readl(dev, DW_IC_COMP_PARAM_1);
354
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300355 /* Disable the adapter */
José Roberto de Souza2702ea72016-08-23 19:18:53 -0300356 __i2c_dw_enable_and_wait(dev, false);
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300357
358 /* set standard and fast speed deviders for high/low periods */
Shinya Kuribayashid60c7e82009-11-06 21:47:01 +0900359
Romain Baeriswyl64682762014-01-20 17:43:43 +0100360 sda_falling_time = dev->sda_falling_time ?: 300; /* ns */
361 scl_falling_time = dev->scl_falling_time ?: 300; /* ns */
362
Jarkko Nikula42ffd392015-01-23 11:35:55 +0200363 /* Set SCL timing parameters for standard-mode */
Mika Westerbergdefc0b22013-08-19 15:07:53 +0300364 if (dev->ss_hcnt && dev->ss_lcnt) {
365 hcnt = dev->ss_hcnt;
366 lcnt = dev->ss_lcnt;
Jarkko Nikula42ffd392015-01-23 11:35:55 +0200367 } else {
Suravee Suthikulpanitb33af112016-01-04 09:17:35 -0600368 hcnt = i2c_dw_scl_hcnt(i2c_dw_clk_rate(dev),
Jarkko Nikula42ffd392015-01-23 11:35:55 +0200369 4000, /* tHD;STA = tHIGH = 4.0 us */
370 sda_falling_time,
371 0, /* 0: DW default, 1: Ideal */
372 0); /* No offset */
Suravee Suthikulpanitb33af112016-01-04 09:17:35 -0600373 lcnt = i2c_dw_scl_lcnt(i2c_dw_clk_rate(dev),
Jarkko Nikula42ffd392015-01-23 11:35:55 +0200374 4700, /* tLOW = 4.7 us */
375 scl_falling_time,
376 0); /* No offset */
Mika Westerbergdefc0b22013-08-19 15:07:53 +0300377 }
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700378 dw_writel(dev, hcnt, DW_IC_SS_SCL_HCNT);
379 dw_writel(dev, lcnt, DW_IC_SS_SCL_LCNT);
Shinya Kuribayashid60c7e82009-11-06 21:47:01 +0900380 dev_dbg(dev->dev, "Standard-mode HCNT:LCNT = %d:%d\n", hcnt, lcnt);
381
Weifeng Voond608c3d2016-08-12 17:02:49 +0300382 /* Set SCL timing parameters for fast-mode or fast-mode plus */
383 if ((dev->clk_freq == 1000000) && dev->fp_hcnt && dev->fp_lcnt) {
384 hcnt = dev->fp_hcnt;
385 lcnt = dev->fp_lcnt;
386 } else if (dev->fs_hcnt && dev->fs_lcnt) {
Mika Westerbergdefc0b22013-08-19 15:07:53 +0300387 hcnt = dev->fs_hcnt;
388 lcnt = dev->fs_lcnt;
Jarkko Nikula42ffd392015-01-23 11:35:55 +0200389 } else {
Suravee Suthikulpanitb33af112016-01-04 09:17:35 -0600390 hcnt = i2c_dw_scl_hcnt(i2c_dw_clk_rate(dev),
Jarkko Nikula42ffd392015-01-23 11:35:55 +0200391 600, /* tHD;STA = tHIGH = 0.6 us */
392 sda_falling_time,
393 0, /* 0: DW default, 1: Ideal */
394 0); /* No offset */
Suravee Suthikulpanitb33af112016-01-04 09:17:35 -0600395 lcnt = i2c_dw_scl_lcnt(i2c_dw_clk_rate(dev),
Jarkko Nikula42ffd392015-01-23 11:35:55 +0200396 1300, /* tLOW = 1.3 us */
397 scl_falling_time,
398 0); /* No offset */
Mika Westerbergdefc0b22013-08-19 15:07:53 +0300399 }
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700400 dw_writel(dev, hcnt, DW_IC_FS_SCL_HCNT);
401 dw_writel(dev, lcnt, DW_IC_FS_SCL_LCNT);
Shinya Kuribayashid60c7e82009-11-06 21:47:01 +0900402 dev_dbg(dev->dev, "Fast-mode HCNT:LCNT = %d:%d\n", hcnt, lcnt);
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300403
Weifeng Voonb6e67142016-08-12 17:02:51 +0300404 if ((dev->master_cfg & DW_IC_CON_SPEED_MASK) ==
405 DW_IC_CON_SPEED_HIGH) {
406 if ((comp_param1 & DW_IC_COMP_PARAM_1_SPEED_MODE_MASK)
407 != DW_IC_COMP_PARAM_1_SPEED_MODE_HIGH) {
408 dev_err(dev->dev, "High Speed not supported!\n");
409 dev->master_cfg &= ~DW_IC_CON_SPEED_MASK;
410 dev->master_cfg |= DW_IC_CON_SPEED_FAST;
411 } else if (dev->hs_hcnt && dev->hs_lcnt) {
412 hcnt = dev->hs_hcnt;
413 lcnt = dev->hs_lcnt;
414 dw_writel(dev, hcnt, DW_IC_HS_SCL_HCNT);
415 dw_writel(dev, lcnt, DW_IC_HS_SCL_LCNT);
416 dev_dbg(dev->dev, "HighSpeed-mode HCNT:LCNT = %d:%d\n",
417 hcnt, lcnt);
418 }
419 }
420
Christian Ruppert9803f862013-06-26 10:55:06 +0200421 /* Configure SDA Hold Time if required */
Zhuo-hao Lee664d58b2016-08-27 15:39:30 +0800422 reg = dw_readl(dev, DW_IC_COMP_VERSION);
423 if (reg >= DW_IC_SDA_HOLD_MIN_VERS) {
Jarkko Nikula171e23e2016-09-29 16:04:59 +0300424 if (!dev->sda_hold_time) {
Zhuo-hao Lee664d58b2016-08-27 15:39:30 +0800425 /* Keep previous hold time setting if no one set it */
426 dev->sda_hold_time = dw_readl(dev, DW_IC_SDA_HOLD);
427 }
Jarkko Nikula171e23e2016-09-29 16:04:59 +0300428 /*
429 * Workaround for avoiding TX arbitration lost in case I2C
430 * slave pulls SDA down "too quickly" after falling egde of
431 * SCL by enabling non-zero SDA RX hold. Specification says it
432 * extends incoming SDA low to high transition while SCL is
433 * high but it apprears to help also above issue.
434 */
435 if (!(dev->sda_hold_time & DW_IC_SDA_HOLD_RX_MASK))
436 dev->sda_hold_time |= 1 << DW_IC_SDA_HOLD_RX_SHIFT;
437 dw_writel(dev, dev->sda_hold_time, DW_IC_SDA_HOLD);
Zhuo-hao Lee664d58b2016-08-27 15:39:30 +0800438 } else {
439 dev_warn(dev->dev,
440 "Hardware too old to adjust SDA hold time.\n");
Christian Ruppert9803f862013-06-26 10:55:06 +0200441 }
442
Shinya Kuribayashi4cb6d1d2009-11-06 21:48:12 +0900443 /* Configure Tx/Rx FIFO threshold levels */
Andrew Jacksond39f77b2014-11-07 12:10:44 +0000444 dw_writel(dev, dev->tx_fifo_depth / 2, DW_IC_TX_TL);
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700445 dw_writel(dev, 0, DW_IC_RX_TL);
Shinya Kuribayashi4cb6d1d2009-11-06 21:48:12 +0900446
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300447 /* configure the i2c master */
Dirk Brandewiee18563f2011-10-06 11:26:32 -0700448 dw_writel(dev, dev->master_cfg , DW_IC_CON);
David Boxc0601d22015-01-15 01:12:16 -0800449
Lucas De Marchi8c5660b2016-08-23 19:18:54 -0300450 i2c_dw_release_lock(dev);
451
Dirk Brandewie4a423a82011-10-06 11:26:28 -0700452 return 0;
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300453}
Axel Line68bb912012-09-10 10:14:02 +0200454EXPORT_SYMBOL_GPL(i2c_dw_init);
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300455
456/*
457 * Waiting for bus not busy
458 */
459static int i2c_dw_wait_bus_not_busy(struct dw_i2c_dev *dev)
460{
461 int timeout = TIMEOUT;
462
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700463 while (dw_readl(dev, DW_IC_STATUS) & DW_IC_STATUS_ACTIVITY) {
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300464 if (timeout <= 0) {
465 dev_warn(dev->dev, "timeout waiting for bus ready\n");
466 return -ETIMEDOUT;
467 }
468 timeout--;
Mika Westerberg1451b912013-04-10 00:36:41 +0000469 usleep_range(1000, 1100);
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300470 }
471
472 return 0;
473}
474
Shinya Kuribayashi81e798b2009-11-06 21:48:55 +0900475static void i2c_dw_xfer_init(struct dw_i2c_dev *dev)
476{
477 struct i2c_msg *msgs = dev->msgs;
Jarkko Nikula48a41582017-02-13 11:18:19 +0200478 u32 ic_con, ic_tar = 0;
Shinya Kuribayashi81e798b2009-11-06 21:48:55 +0900479
Jarkko Nikula89119f02016-11-25 17:22:27 +0200480 /* Disable the adapter */
481 __i2c_dw_enable_and_wait(dev, false);
Chew, Chiau Eebd63ace2013-09-27 02:57:35 +0800482
Shinya Kuribayashi81e798b2009-11-06 21:48:55 +0900483 /* if the slave address is ten bit address, enable 10BITADDR */
Jarkko Nikula48a41582017-02-13 11:18:19 +0200484 ic_con = dw_readl(dev, DW_IC_CON);
485 if (msgs[dev->msg_write_idx].flags & I2C_M_TEN) {
486 ic_con |= DW_IC_CON_10BITADDR_MASTER;
Mika Westerberg3ca4ed82013-04-10 00:36:40 +0000487 /*
Shinya Kuribayashi81e798b2009-11-06 21:48:55 +0900488 * If I2C_DYNAMIC_TAR_UPDATE is set, the 10-bit addressing
Jarkko Nikula48a41582017-02-13 11:18:19 +0200489 * mode has to be enabled via bit 12 of IC_TAR register.
490 * We set it always as I2C_DYNAMIC_TAR_UPDATE can't be
491 * detected from registers.
Shinya Kuribayashi81e798b2009-11-06 21:48:55 +0900492 */
Jarkko Nikula48a41582017-02-13 11:18:19 +0200493 ic_tar = DW_IC_TAR_10BITADDR_MASTER;
Shinya Kuribayashi81e798b2009-11-06 21:48:55 +0900494 } else {
Jarkko Nikula48a41582017-02-13 11:18:19 +0200495 ic_con &= ~DW_IC_CON_10BITADDR_MASTER;
Lucas De Marchi63d0f0a62016-08-23 19:18:55 -0300496 }
Shinya Kuribayashi81e798b2009-11-06 21:48:55 +0900497
Jarkko Nikula48a41582017-02-13 11:18:19 +0200498 dw_writel(dev, ic_con, DW_IC_CON);
499
Chew, Chiau Eebd63ace2013-09-27 02:57:35 +0800500 /*
501 * Set the slave (target) address and enable 10-bit addressing mode
502 * if applicable.
503 */
504 dw_writel(dev, msgs[dev->msg_write_idx].addr | ic_tar, DW_IC_TAR);
505
Du, Wenkai47bb27e2014-04-10 23:03:19 +0000506 /* enforce disabled interrupts (due to HW issues) */
507 i2c_dw_disable_int(dev);
508
Jarkko Nikula89119f02016-11-25 17:22:27 +0200509 /* Enable the adapter */
510 __i2c_dw_enable(dev, true);
Shinya Kuribayashi201d6a72009-11-06 21:50:40 +0900511
Mika Westerberg2a2d95e2013-05-13 00:54:30 +0000512 /* Clear and enable interrupts */
Jarkko Nikulac3356312015-08-31 17:31:28 +0300513 dw_readl(dev, DW_IC_CLR_INTR);
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700514 dw_writel(dev, DW_IC_INTR_DEFAULT_MASK, DW_IC_INTR_MASK);
Shinya Kuribayashi81e798b2009-11-06 21:48:55 +0900515}
516
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300517/*
Shinya Kuribayashi201d6a72009-11-06 21:50:40 +0900518 * Initiate (and continue) low level master read/write transaction.
519 * This function is only called from i2c_dw_isr, and pumping i2c_msg
520 * messages into the tx buffer. Even if the size of i2c_msg data is
521 * longer than the size of the tx buffer, it handles everything.
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300522 */
Jean Delvarebccd7802012-10-05 22:23:53 +0200523static void
Shinya Kuribayashie77cf232009-11-06 21:46:04 +0900524i2c_dw_xfer_msg(struct dw_i2c_dev *dev)
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300525{
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300526 struct i2c_msg *msgs = dev->msgs;
Shinya Kuribayashi81e798b2009-11-06 21:48:55 +0900527 u32 intr_mask;
Shinya Kuribayashiae722222009-11-06 21:49:39 +0900528 int tx_limit, rx_limit;
Shinya Kuribayashied5e1dd2009-11-06 21:43:52 +0900529 u32 addr = msgs[dev->msg_write_idx].addr;
530 u32 buf_len = dev->tx_buf_len;
Justin P. Mattock69932482011-07-26 23:06:29 -0700531 u8 *buf = dev->tx_buf;
Chew, Chiau Ee82564242013-06-21 15:05:28 +0800532 bool need_restart = false;
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300533
Shinya Kuribayashi201d6a72009-11-06 21:50:40 +0900534 intr_mask = DW_IC_INTR_DEFAULT_MASK;
Shinya Kuribayashic70c5cd2009-11-06 21:47:30 +0900535
Shinya Kuribayashi6d2ea482009-11-06 21:46:29 +0900536 for (; dev->msg_write_idx < dev->msgs_num; dev->msg_write_idx++) {
Shinya Kuribayashia0e06ea2009-11-06 21:52:22 +0900537 /*
538 * if target address has changed, we need to
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300539 * reprogram the target address in the i2c
540 * adapter when we are done with this transfer
541 */
Shinya Kuribayashi8f588e42009-11-06 21:51:18 +0900542 if (msgs[dev->msg_write_idx].addr != addr) {
543 dev_err(dev->dev,
544 "%s: invalid target address\n", __func__);
545 dev->msg_err = -EINVAL;
546 break;
547 }
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300548
549 if (msgs[dev->msg_write_idx].len == 0) {
550 dev_err(dev->dev,
551 "%s: invalid message length\n", __func__);
552 dev->msg_err = -EINVAL;
Shinya Kuribayashi8f588e42009-11-06 21:51:18 +0900553 break;
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300554 }
555
556 if (!(dev->status & STATUS_WRITE_IN_PROGRESS)) {
557 /* new i2c_msg */
Shinya Kuribayashi26ea15b2009-11-06 21:49:14 +0900558 buf = msgs[dev->msg_write_idx].buf;
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300559 buf_len = msgs[dev->msg_write_idx].len;
Chew, Chiau Ee82564242013-06-21 15:05:28 +0800560
561 /* If both IC_EMPTYFIFO_HOLD_MASTER_EN and
562 * IC_RESTART_EN are set, we must manually
563 * set restart bit between messages.
564 */
565 if ((dev->master_cfg & DW_IC_CON_RESTART_EN) &&
566 (dev->msg_write_idx > 0))
567 need_restart = true;
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300568 }
569
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700570 tx_limit = dev->tx_fifo_depth - dw_readl(dev, DW_IC_TXFLR);
571 rx_limit = dev->rx_fifo_depth - dw_readl(dev, DW_IC_RXFLR);
Shinya Kuribayashiae722222009-11-06 21:49:39 +0900572
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300573 while (buf_len > 0 && tx_limit > 0 && rx_limit > 0) {
Mika Westerberg17a76b42013-01-17 12:31:05 +0200574 u32 cmd = 0;
575
576 /*
577 * If IC_EMPTYFIFO_HOLD_MASTER_EN is set we must
578 * manually set the stop bit. However, it cannot be
579 * detected from the registers so we set it always
580 * when writing/reading the last byte.
581 */
582 if (dev->msg_write_idx == dev->msgs_num - 1 &&
583 buf_len == 1)
584 cmd |= BIT(9);
585
Chew, Chiau Ee82564242013-06-21 15:05:28 +0800586 if (need_restart) {
587 cmd |= BIT(10);
588 need_restart = false;
589 }
590
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300591 if (msgs[dev->msg_write_idx].flags & I2C_M_RD) {
Josef Ahmade6f34ce2013-04-19 17:28:10 +0100592
593 /* avoid rx buffer overrun */
Russell King4d6d5f12016-11-18 19:40:10 +0000594 if (dev->rx_outstanding >= dev->rx_fifo_depth)
Josef Ahmade6f34ce2013-04-19 17:28:10 +0100595 break;
596
Mika Westerberg17a76b42013-01-17 12:31:05 +0200597 dw_writel(dev, cmd | 0x100, DW_IC_DATA_CMD);
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300598 rx_limit--;
Josef Ahmade6f34ce2013-04-19 17:28:10 +0100599 dev->rx_outstanding++;
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300600 } else
Mika Westerberg17a76b42013-01-17 12:31:05 +0200601 dw_writel(dev, cmd | *buf++, DW_IC_DATA_CMD);
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300602 tx_limit--; buf_len--;
603 }
Shinya Kuribayashic70c5cd2009-11-06 21:47:30 +0900604
Shinya Kuribayashi26ea15b2009-11-06 21:49:14 +0900605 dev->tx_buf = buf;
Shinya Kuribayashic70c5cd2009-11-06 21:47:30 +0900606 dev->tx_buf_len = buf_len;
607
608 if (buf_len > 0) {
609 /* more bytes to be written */
Shinya Kuribayashic70c5cd2009-11-06 21:47:30 +0900610 dev->status |= STATUS_WRITE_IN_PROGRESS;
611 break;
Shinya Kuribayashi69151e52009-11-06 21:51:00 +0900612 } else
Shinya Kuribayashic70c5cd2009-11-06 21:47:30 +0900613 dev->status &= ~STATUS_WRITE_IN_PROGRESS;
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300614 }
615
Shinya Kuribayashi69151e52009-11-06 21:51:00 +0900616 /*
617 * If i2c_msg index search is completed, we don't need TX_EMPTY
618 * interrupt any more.
619 */
620 if (dev->msg_write_idx == dev->msgs_num)
621 intr_mask &= ~DW_IC_INTR_TX_EMPTY;
622
Shinya Kuribayashi8f588e42009-11-06 21:51:18 +0900623 if (dev->msg_err)
624 intr_mask = 0;
625
Dirk Brandewie2373f6b2011-10-29 10:57:23 +0100626 dw_writel(dev, intr_mask, DW_IC_INTR_MASK);
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300627}
628
629static void
Shinya Kuribayashi78839bd2009-11-06 21:45:39 +0900630i2c_dw_read(struct dw_i2c_dev *dev)
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300631{
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300632 struct i2c_msg *msgs = dev->msgs;
Shinya Kuribayashiae722222009-11-06 21:49:39 +0900633 int rx_valid;
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300634
Shinya Kuribayashi6d2ea482009-11-06 21:46:29 +0900635 for (; dev->msg_read_idx < dev->msgs_num; dev->msg_read_idx++) {
Shinya Kuribayashied5e1dd2009-11-06 21:43:52 +0900636 u32 len;
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300637 u8 *buf;
638
639 if (!(msgs[dev->msg_read_idx].flags & I2C_M_RD))
640 continue;
641
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300642 if (!(dev->status & STATUS_READ_IN_PROGRESS)) {
643 len = msgs[dev->msg_read_idx].len;
644 buf = msgs[dev->msg_read_idx].buf;
645 } else {
646 len = dev->rx_buf_len;
647 buf = dev->rx_buf;
648 }
649
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700650 rx_valid = dw_readl(dev, DW_IC_RXFLR);
Shinya Kuribayashiae722222009-11-06 21:49:39 +0900651
Josef Ahmade6f34ce2013-04-19 17:28:10 +0100652 for (; len > 0 && rx_valid > 0; len--, rx_valid--) {
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700653 *buf++ = dw_readl(dev, DW_IC_DATA_CMD);
Josef Ahmade6f34ce2013-04-19 17:28:10 +0100654 dev->rx_outstanding--;
655 }
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300656
657 if (len > 0) {
658 dev->status |= STATUS_READ_IN_PROGRESS;
659 dev->rx_buf_len = len;
660 dev->rx_buf = buf;
661 return;
662 } else
663 dev->status &= ~STATUS_READ_IN_PROGRESS;
664 }
665}
666
Shinya Kuribayashice6eb572009-11-06 21:51:57 +0900667static int i2c_dw_handle_tx_abort(struct dw_i2c_dev *dev)
668{
669 unsigned long abort_source = dev->abort_source;
670 int i;
671
Shinya Kuribayashi6d1ea0f2009-11-16 20:40:14 +0900672 if (abort_source & DW_IC_TX_ABRT_NOACK) {
Akinobu Mita984b3f52010-03-05 13:41:37 -0800673 for_each_set_bit(i, &abort_source, ARRAY_SIZE(abort_sources))
Shinya Kuribayashi6d1ea0f2009-11-16 20:40:14 +0900674 dev_dbg(dev->dev,
675 "%s: %s\n", __func__, abort_sources[i]);
676 return -EREMOTEIO;
677 }
678
Akinobu Mita984b3f52010-03-05 13:41:37 -0800679 for_each_set_bit(i, &abort_source, ARRAY_SIZE(abort_sources))
Shinya Kuribayashice6eb572009-11-06 21:51:57 +0900680 dev_err(dev->dev, "%s: %s\n", __func__, abort_sources[i]);
681
682 if (abort_source & DW_IC_TX_ARB_LOST)
683 return -EAGAIN;
Shinya Kuribayashice6eb572009-11-06 21:51:57 +0900684 else if (abort_source & DW_IC_TX_ABRT_GCALL_READ)
685 return -EINVAL; /* wrong msgs[] data */
686 else
687 return -EIO;
688}
689
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300690/*
Jarkko Nikula89119f02016-11-25 17:22:27 +0200691 * Prepare controller for a transaction and call i2c_dw_xfer_msg
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300692 */
Jarkko Nikulad80d1342015-10-12 16:55:35 +0300693static int
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300694i2c_dw_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], int num)
695{
696 struct dw_i2c_dev *dev = i2c_get_adapdata(adap);
697 int ret;
698
699 dev_dbg(dev->dev, "%s: msgs: %d\n", __func__, num);
700
Dirk Brandewie18dbdda2011-10-06 11:26:36 -0700701 pm_runtime_get_sync(dev->dev);
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300702
Wolfram Sang16735d02013-11-14 14:32:02 -0800703 reinit_completion(&dev->cmd_complete);
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300704 dev->msgs = msgs;
705 dev->msgs_num = num;
706 dev->cmd_err = 0;
707 dev->msg_write_idx = 0;
708 dev->msg_read_idx = 0;
709 dev->msg_err = 0;
710 dev->status = STATUS_IDLE;
Shinya Kuribayashice6eb572009-11-06 21:51:57 +0900711 dev->abort_source = 0;
Josef Ahmade6f34ce2013-04-19 17:28:10 +0100712 dev->rx_outstanding = 0;
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300713
Lucas De Marchi8c5660b2016-08-23 19:18:54 -0300714 ret = i2c_dw_acquire_lock(dev);
715 if (ret)
716 goto done_nolock;
David Boxc0601d22015-01-15 01:12:16 -0800717
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300718 ret = i2c_dw_wait_bus_not_busy(dev);
719 if (ret < 0)
720 goto done;
721
722 /* start the transfers */
Shinya Kuribayashi81e798b2009-11-06 21:48:55 +0900723 i2c_dw_xfer_init(dev);
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300724
725 /* wait for tx to complete */
Weifeng Voond0bcd8d2016-06-17 09:46:35 +0800726 if (!wait_for_completion_timeout(&dev->cmd_complete, adap->timeout)) {
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300727 dev_err(dev->dev, "controller timed out\n");
Christian Ruppert38d7fad2013-06-07 10:51:23 +0200728 /* i2c_dw_init implicitly disables the adapter */
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300729 i2c_dw_init(dev);
730 ret = -ETIMEDOUT;
731 goto done;
Mika Westerberge42dba52013-05-22 13:03:11 +0300732 }
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300733
Jarkko Nikula89119f02016-11-25 17:22:27 +0200734 /*
735 * We must disable the adapter before returning and signaling the end
736 * of the current transfer. Otherwise the hardware might continue
737 * generating interrupts which in turn causes a race condition with
738 * the following transfer. Needs some more investigation if the
739 * additional interrupts are a hardware bug or this driver doesn't
740 * handle them correctly yet.
741 */
742 __i2c_dw_enable(dev, false);
743
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300744 if (dev->msg_err) {
745 ret = dev->msg_err;
746 goto done;
747 }
748
749 /* no error */
Russell King2bf413d2016-11-18 19:40:04 +0000750 if (likely(!dev->cmd_err && !dev->status)) {
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300751 ret = num;
752 goto done;
753 }
754
755 /* We have an error */
756 if (dev->cmd_err == DW_IC_ERR_TX_ABRT) {
Shinya Kuribayashice6eb572009-11-06 21:51:57 +0900757 ret = i2c_dw_handle_tx_abort(dev);
758 goto done;
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300759 }
Russell King2bf413d2016-11-18 19:40:04 +0000760
761 if (dev->status)
762 dev_err(dev->dev,
763 "transfer terminated early - interrupt latency too high?\n");
764
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300765 ret = -EIO;
766
767done:
Lucas De Marchi8c5660b2016-08-23 19:18:54 -0300768 i2c_dw_release_lock(dev);
David Boxc0601d22015-01-15 01:12:16 -0800769
770done_nolock:
Mika Westerberg43452332013-04-10 00:36:42 +0000771 pm_runtime_mark_last_busy(dev->dev);
772 pm_runtime_put_autosuspend(dev->dev);
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300773
774 return ret;
775}
776
Jarkko Nikulad80d1342015-10-12 16:55:35 +0300777static u32 i2c_dw_func(struct i2c_adapter *adap)
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300778{
Dirk Brandewie2fa83262011-10-06 11:26:31 -0700779 struct dw_i2c_dev *dev = i2c_get_adapdata(adap);
780 return dev->functionality;
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300781}
Jarkko Nikulad80d1342015-10-12 16:55:35 +0300782
783static struct i2c_algorithm i2c_dw_algo = {
784 .master_xfer = i2c_dw_xfer,
785 .functionality = i2c_dw_func,
786};
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300787
Shinya Kuribayashie28000a2009-11-06 21:44:37 +0900788static u32 i2c_dw_read_clear_intrbits(struct dw_i2c_dev *dev)
789{
790 u32 stat;
791
792 /*
793 * The IC_INTR_STAT register just indicates "enabled" interrupts.
794 * Ths unmasked raw version of interrupt status bits are available
795 * in the IC_RAW_INTR_STAT register.
796 *
797 * That is,
Dirk Brandewie2373f6b2011-10-29 10:57:23 +0100798 * stat = dw_readl(IC_INTR_STAT);
Shinya Kuribayashie28000a2009-11-06 21:44:37 +0900799 * equals to,
Dirk Brandewie2373f6b2011-10-29 10:57:23 +0100800 * stat = dw_readl(IC_RAW_INTR_STAT) & dw_readl(IC_INTR_MASK);
Shinya Kuribayashie28000a2009-11-06 21:44:37 +0900801 *
802 * The raw version might be useful for debugging purposes.
803 */
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700804 stat = dw_readl(dev, DW_IC_INTR_STAT);
Shinya Kuribayashie28000a2009-11-06 21:44:37 +0900805
806 /*
807 * Do not use the IC_CLR_INTR register to clear interrupts, or
808 * you'll miss some interrupts, triggered during the period from
Dirk Brandewie2373f6b2011-10-29 10:57:23 +0100809 * dw_readl(IC_INTR_STAT) to dw_readl(IC_CLR_INTR).
Shinya Kuribayashie28000a2009-11-06 21:44:37 +0900810 *
811 * Instead, use the separately-prepared IC_CLR_* registers.
812 */
813 if (stat & DW_IC_INTR_RX_UNDER)
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700814 dw_readl(dev, DW_IC_CLR_RX_UNDER);
Shinya Kuribayashie28000a2009-11-06 21:44:37 +0900815 if (stat & DW_IC_INTR_RX_OVER)
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700816 dw_readl(dev, DW_IC_CLR_RX_OVER);
Shinya Kuribayashie28000a2009-11-06 21:44:37 +0900817 if (stat & DW_IC_INTR_TX_OVER)
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700818 dw_readl(dev, DW_IC_CLR_TX_OVER);
Shinya Kuribayashie28000a2009-11-06 21:44:37 +0900819 if (stat & DW_IC_INTR_RD_REQ)
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700820 dw_readl(dev, DW_IC_CLR_RD_REQ);
Shinya Kuribayashie28000a2009-11-06 21:44:37 +0900821 if (stat & DW_IC_INTR_TX_ABRT) {
822 /*
823 * The IC_TX_ABRT_SOURCE register is cleared whenever
824 * the IC_CLR_TX_ABRT is read. Preserve it beforehand.
825 */
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700826 dev->abort_source = dw_readl(dev, DW_IC_TX_ABRT_SOURCE);
827 dw_readl(dev, DW_IC_CLR_TX_ABRT);
Shinya Kuribayashie28000a2009-11-06 21:44:37 +0900828 }
829 if (stat & DW_IC_INTR_RX_DONE)
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700830 dw_readl(dev, DW_IC_CLR_RX_DONE);
Shinya Kuribayashie28000a2009-11-06 21:44:37 +0900831 if (stat & DW_IC_INTR_ACTIVITY)
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700832 dw_readl(dev, DW_IC_CLR_ACTIVITY);
Shinya Kuribayashie28000a2009-11-06 21:44:37 +0900833 if (stat & DW_IC_INTR_STOP_DET)
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700834 dw_readl(dev, DW_IC_CLR_STOP_DET);
Shinya Kuribayashie28000a2009-11-06 21:44:37 +0900835 if (stat & DW_IC_INTR_START_DET)
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700836 dw_readl(dev, DW_IC_CLR_START_DET);
Shinya Kuribayashie28000a2009-11-06 21:44:37 +0900837 if (stat & DW_IC_INTR_GEN_CALL)
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700838 dw_readl(dev, DW_IC_CLR_GEN_CALL);
Shinya Kuribayashie28000a2009-11-06 21:44:37 +0900839
840 return stat;
841}
842
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300843/*
844 * Interrupt service routine. This gets called whenever an I2C interrupt
845 * occurs.
846 */
Jarkko Nikulad80d1342015-10-12 16:55:35 +0300847static irqreturn_t i2c_dw_isr(int this_irq, void *dev_id)
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300848{
849 struct dw_i2c_dev *dev = dev_id;
Dirk Brandewieaf06cf62011-10-06 11:26:33 -0700850 u32 stat, enabled;
851
852 enabled = dw_readl(dev, DW_IC_ENABLE);
853 stat = dw_readl(dev, DW_IC_RAW_INTR_STAT);
Jarkko Nikulafb427462015-08-07 14:53:03 +0300854 dev_dbg(dev->dev, "%s: enabled=%#x stat=%#x\n", __func__, enabled, stat);
Dirk Brandewieaf06cf62011-10-06 11:26:33 -0700855 if (!enabled || !(stat & ~DW_IC_INTR_ACTIVITY))
856 return IRQ_NONE;
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300857
Shinya Kuribayashie28000a2009-11-06 21:44:37 +0900858 stat = i2c_dw_read_clear_intrbits(dev);
Shinya Kuribayashie28000a2009-11-06 21:44:37 +0900859
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300860 if (stat & DW_IC_INTR_TX_ABRT) {
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300861 dev->cmd_err |= DW_IC_ERR_TX_ABRT;
862 dev->status = STATUS_IDLE;
Shinya Kuribayashi597fe312009-11-06 21:51:36 +0900863
864 /*
865 * Anytime TX_ABRT is set, the contents of the tx/rx
866 * buffers are flushed. Make sure to skip them.
867 */
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700868 dw_writel(dev, 0, DW_IC_INTR_MASK);
Shinya Kuribayashi597fe312009-11-06 21:51:36 +0900869 goto tx_aborted;
Shinya Kuribayashi07745392009-11-06 21:47:51 +0900870 }
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300871
Shinya Kuribayashi21a89d42009-11-06 21:48:33 +0900872 if (stat & DW_IC_INTR_RX_FULL)
Shinya Kuribayashi07745392009-11-06 21:47:51 +0900873 i2c_dw_read(dev);
Shinya Kuribayashi21a89d42009-11-06 21:48:33 +0900874
875 if (stat & DW_IC_INTR_TX_EMPTY)
Shinya Kuribayashi07745392009-11-06 21:47:51 +0900876 i2c_dw_xfer_msg(dev);
Shinya Kuribayashi07745392009-11-06 21:47:51 +0900877
878 /*
879 * No need to modify or disable the interrupt mask here.
880 * i2c_dw_xfer_msg() will take care of it according to
881 * the current transmit status.
882 */
883
Shinya Kuribayashi597fe312009-11-06 21:51:36 +0900884tx_aborted:
Jarkko Nikula89119f02016-11-25 17:22:27 +0200885 if ((stat & (DW_IC_INTR_TX_ABRT | DW_IC_INTR_STOP_DET)) || dev->msg_err)
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300886 complete(&dev->cmd_complete);
Jarkko Nikula89119f02016-11-25 17:22:27 +0200887 else if (unlikely(dev->accessor_flags & ACCESS_INTR_MASK)) {
Xiangliang Yu2d244c82015-12-11 20:02:53 +0800888 /* workaround to trigger pending interrupt */
889 stat = dw_readl(dev, DW_IC_INTR_MASK);
890 i2c_dw_disable_int(dev);
891 dw_writel(dev, stat, DW_IC_INTR_MASK);
892 }
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300893
894 return IRQ_HANDLED;
895}
Dirk Brandewief3fa9f32011-10-06 11:26:34 -0700896
Dirk Brandewief3fa9f32011-10-06 11:26:34 -0700897void i2c_dw_disable(struct dw_i2c_dev *dev)
898{
Dirk Brandewief3fa9f32011-10-06 11:26:34 -0700899 /* Disable controller */
José Roberto de Souza2702ea72016-08-23 19:18:53 -0300900 __i2c_dw_enable_and_wait(dev, false);
Dirk Brandewief3fa9f32011-10-06 11:26:34 -0700901
902 /* Disable all interupts */
903 dw_writel(dev, 0, DW_IC_INTR_MASK);
904 dw_readl(dev, DW_IC_CLR_INTR);
905}
Axel Line68bb912012-09-10 10:14:02 +0200906EXPORT_SYMBOL_GPL(i2c_dw_disable);
Dirk Brandewief3fa9f32011-10-06 11:26:34 -0700907
Dirk Brandewief3fa9f32011-10-06 11:26:34 -0700908void i2c_dw_disable_int(struct dw_i2c_dev *dev)
909{
910 dw_writel(dev, 0, DW_IC_INTR_MASK);
911}
Axel Line68bb912012-09-10 10:14:02 +0200912EXPORT_SYMBOL_GPL(i2c_dw_disable_int);
Dirk Brandewief3fa9f32011-10-06 11:26:34 -0700913
914u32 i2c_dw_read_comp_param(struct dw_i2c_dev *dev)
915{
916 return dw_readl(dev, DW_IC_COMP_PARAM_1);
917}
Axel Line68bb912012-09-10 10:14:02 +0200918EXPORT_SYMBOL_GPL(i2c_dw_read_comp_param);
Mika Westerberg9dd31622013-01-17 12:31:04 +0200919
Jarkko Nikulad80d1342015-10-12 16:55:35 +0300920int i2c_dw_probe(struct dw_i2c_dev *dev)
921{
922 struct i2c_adapter *adap = &dev->adapter;
923 int r;
924
925 init_completion(&dev->cmd_complete);
Jarkko Nikulad80d1342015-10-12 16:55:35 +0300926
927 r = i2c_dw_init(dev);
928 if (r)
929 return r;
930
931 snprintf(adap->name, sizeof(adap->name),
932 "Synopsys DesignWare I2C adapter");
Baruch Siach8d22f302015-12-23 18:43:24 +0200933 adap->retries = 3;
Jarkko Nikulad80d1342015-10-12 16:55:35 +0300934 adap->algo = &i2c_dw_algo;
935 adap->dev.parent = dev->dev;
936 i2c_set_adapdata(adap, dev);
937
938 i2c_dw_disable_int(dev);
Andy Shevchenko08c6e8c2016-01-15 22:02:12 +0200939 r = devm_request_irq(dev->dev, dev->irq, i2c_dw_isr,
940 IRQF_SHARED | IRQF_COND_SUSPEND,
Jarkko Nikulad80d1342015-10-12 16:55:35 +0300941 dev_name(dev->dev), dev);
942 if (r) {
943 dev_err(dev->dev, "failure requesting irq %i: %d\n",
944 dev->irq, r);
945 return r;
946 }
947
Jarkko Nikulacd998de2016-02-11 16:36:03 +0200948 /*
949 * Increment PM usage count during adapter registration in order to
950 * avoid possible spurious runtime suspend when adapter device is
951 * registered to the device core and immediate resume in case bus has
952 * registered I2C slaves that do I2C transfers in their probe.
953 */
954 pm_runtime_get_noresume(dev->dev);
Jarkko Nikulad80d1342015-10-12 16:55:35 +0300955 r = i2c_add_numbered_adapter(adap);
956 if (r)
957 dev_err(dev->dev, "failure adding adapter: %d\n", r);
Jarkko Nikulacd998de2016-02-11 16:36:03 +0200958 pm_runtime_put_noidle(dev->dev);
Jarkko Nikulad80d1342015-10-12 16:55:35 +0300959
960 return r;
961}
962EXPORT_SYMBOL_GPL(i2c_dw_probe);
963
Mika Westerberg9dd31622013-01-17 12:31:04 +0200964MODULE_DESCRIPTION("Synopsys DesignWare I2C bus adapter core");
965MODULE_LICENSE("GPL");