Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 1 | /* |
Shinya Kuribayashi | a0e06ea | 2009-11-06 21:52:22 +0900 | [diff] [blame] | 2 | * Synopsys DesignWare I2C adapter driver (master only). |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 3 | * |
| 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 Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 21 | * ---------------------------------------------------------------------------- |
| 22 | * |
| 23 | */ |
Axel Lin | e68bb91 | 2012-09-10 10:14:02 +0200 | [diff] [blame] | 24 | #include <linux/export.h> |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 25 | #include <linux/errno.h> |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 26 | #include <linux/err.h> |
Dirk Brandewie | 2373f6b | 2011-10-29 10:57:23 +0100 | [diff] [blame] | 27 | #include <linux/i2c.h> |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 28 | #include <linux/interrupt.h> |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 29 | #include <linux/io.h> |
Dirk Brandewie | 18dbdda | 2011-10-06 11:26:36 -0700 | [diff] [blame] | 30 | #include <linux/pm_runtime.h> |
Dirk Brandewie | 2373f6b | 2011-10-29 10:57:23 +0100 | [diff] [blame] | 31 | #include <linux/delay.h> |
Mika Westerberg | 9dd3162 | 2013-01-17 12:31:04 +0200 | [diff] [blame] | 32 | #include <linux/module.h> |
Dirk Brandewie | 2373f6b | 2011-10-29 10:57:23 +0100 | [diff] [blame] | 33 | #include "i2c-designware-core.h" |
Shinya Kuribayashi | ce6eb57 | 2009-11-06 21:51:57 +0900 | [diff] [blame] | 34 | |
Dirk Brandewie | f3fa9f3 | 2011-10-06 11:26:34 -0700 | [diff] [blame] | 35 | /* |
| 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 Voon | b6e6714 | 2016-08-12 17:02:51 +0300 | [diff] [blame] | 45 | #define DW_IC_HS_SCL_HCNT 0x24 |
| 46 | #define DW_IC_HS_SCL_LCNT 0x28 |
Dirk Brandewie | f3fa9f3 | 2011-10-06 11:26:34 -0700 | [diff] [blame] | 47 | #define DW_IC_INTR_STAT 0x2c |
| 48 | #define DW_IC_INTR_MASK 0x30 |
| 49 | #define DW_IC_RAW_INTR_STAT 0x34 |
| 50 | #define DW_IC_RX_TL 0x38 |
| 51 | #define DW_IC_TX_TL 0x3c |
| 52 | #define DW_IC_CLR_INTR 0x40 |
| 53 | #define DW_IC_CLR_RX_UNDER 0x44 |
| 54 | #define DW_IC_CLR_RX_OVER 0x48 |
| 55 | #define DW_IC_CLR_TX_OVER 0x4c |
| 56 | #define DW_IC_CLR_RD_REQ 0x50 |
| 57 | #define DW_IC_CLR_TX_ABRT 0x54 |
| 58 | #define DW_IC_CLR_RX_DONE 0x58 |
| 59 | #define DW_IC_CLR_ACTIVITY 0x5c |
| 60 | #define DW_IC_CLR_STOP_DET 0x60 |
| 61 | #define DW_IC_CLR_START_DET 0x64 |
| 62 | #define DW_IC_CLR_GEN_CALL 0x68 |
| 63 | #define DW_IC_ENABLE 0x6c |
| 64 | #define DW_IC_STATUS 0x70 |
| 65 | #define DW_IC_TXFLR 0x74 |
| 66 | #define DW_IC_RXFLR 0x78 |
Christian Ruppert | 9803f86 | 2013-06-26 10:55:06 +0200 | [diff] [blame] | 67 | #define DW_IC_SDA_HOLD 0x7c |
Dirk Brandewie | f3fa9f3 | 2011-10-06 11:26:34 -0700 | [diff] [blame] | 68 | #define DW_IC_TX_ABRT_SOURCE 0x80 |
Mika Westerberg | 3ca4ed8 | 2013-04-10 00:36:40 +0000 | [diff] [blame] | 69 | #define DW_IC_ENABLE_STATUS 0x9c |
Dirk Brandewie | f3fa9f3 | 2011-10-06 11:26:34 -0700 | [diff] [blame] | 70 | #define DW_IC_COMP_PARAM_1 0xf4 |
Christian Ruppert | 9803f86 | 2013-06-26 10:55:06 +0200 | [diff] [blame] | 71 | #define DW_IC_COMP_VERSION 0xf8 |
| 72 | #define DW_IC_SDA_HOLD_MIN_VERS 0x3131312A |
Dirk Brandewie | f3fa9f3 | 2011-10-06 11:26:34 -0700 | [diff] [blame] | 73 | #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 Marchi | 0317e6c | 2016-08-23 19:18:56 -0300 | [diff] [blame] | 94 | #define DW_IC_STATUS_ACTIVITY 0x1 |
| 95 | #define DW_IC_STATUS_TFE BIT(2) |
| 96 | #define DW_IC_STATUS_MST_ACTIVITY BIT(5) |
Dirk Brandewie | f3fa9f3 | 2011-10-06 11:26:34 -0700 | [diff] [blame] | 97 | |
Jarkko Nikula | 171e23e | 2016-09-29 16:04:59 +0300 | [diff] [blame] | 98 | #define DW_IC_SDA_HOLD_RX_SHIFT 16 |
| 99 | #define DW_IC_SDA_HOLD_RX_MASK GENMASK(23, DW_IC_SDA_HOLD_RX_SHIFT) |
| 100 | |
Dirk Brandewie | f3fa9f3 | 2011-10-06 11:26:34 -0700 | [diff] [blame] | 101 | #define DW_IC_ERR_TX_ABRT 0x1 |
| 102 | |
Chew, Chiau Ee | bd63ace | 2013-09-27 02:57:35 +0800 | [diff] [blame] | 103 | #define DW_IC_TAR_10BITADDR_MASTER BIT(12) |
| 104 | |
Weifeng Voon | b6e6714 | 2016-08-12 17:02:51 +0300 | [diff] [blame] | 105 | #define DW_IC_COMP_PARAM_1_SPEED_MODE_HIGH (BIT(2) | BIT(3)) |
| 106 | #define DW_IC_COMP_PARAM_1_SPEED_MODE_MASK GENMASK(3, 2) |
| 107 | |
Dirk Brandewie | f3fa9f3 | 2011-10-06 11:26:34 -0700 | [diff] [blame] | 108 | /* |
| 109 | * status codes |
| 110 | */ |
| 111 | #define STATUS_IDLE 0x0 |
| 112 | #define STATUS_WRITE_IN_PROGRESS 0x1 |
| 113 | #define STATUS_READ_IN_PROGRESS 0x2 |
| 114 | |
| 115 | #define TIMEOUT 20 /* ms */ |
| 116 | |
| 117 | /* |
| 118 | * hardware abort codes from the DW_IC_TX_ABRT_SOURCE register |
| 119 | * |
| 120 | * only expected abort codes are listed here |
| 121 | * refer to the datasheet for the full list |
| 122 | */ |
| 123 | #define ABRT_7B_ADDR_NOACK 0 |
| 124 | #define ABRT_10ADDR1_NOACK 1 |
| 125 | #define ABRT_10ADDR2_NOACK 2 |
| 126 | #define ABRT_TXDATA_NOACK 3 |
| 127 | #define ABRT_GCALL_NOACK 4 |
| 128 | #define ABRT_GCALL_READ 5 |
| 129 | #define ABRT_SBYTE_ACKDET 7 |
| 130 | #define ABRT_SBYTE_NORSTRT 9 |
| 131 | #define ABRT_10B_RD_NORSTRT 10 |
| 132 | #define ABRT_MASTER_DIS 11 |
| 133 | #define ARB_LOST 12 |
| 134 | |
| 135 | #define DW_IC_TX_ABRT_7B_ADDR_NOACK (1UL << ABRT_7B_ADDR_NOACK) |
| 136 | #define DW_IC_TX_ABRT_10ADDR1_NOACK (1UL << ABRT_10ADDR1_NOACK) |
| 137 | #define DW_IC_TX_ABRT_10ADDR2_NOACK (1UL << ABRT_10ADDR2_NOACK) |
| 138 | #define DW_IC_TX_ABRT_TXDATA_NOACK (1UL << ABRT_TXDATA_NOACK) |
| 139 | #define DW_IC_TX_ABRT_GCALL_NOACK (1UL << ABRT_GCALL_NOACK) |
| 140 | #define DW_IC_TX_ABRT_GCALL_READ (1UL << ABRT_GCALL_READ) |
| 141 | #define DW_IC_TX_ABRT_SBYTE_ACKDET (1UL << ABRT_SBYTE_ACKDET) |
| 142 | #define DW_IC_TX_ABRT_SBYTE_NORSTRT (1UL << ABRT_SBYTE_NORSTRT) |
| 143 | #define DW_IC_TX_ABRT_10B_RD_NORSTRT (1UL << ABRT_10B_RD_NORSTRT) |
| 144 | #define DW_IC_TX_ABRT_MASTER_DIS (1UL << ABRT_MASTER_DIS) |
| 145 | #define DW_IC_TX_ARB_LOST (1UL << ARB_LOST) |
| 146 | |
| 147 | #define DW_IC_TX_ABRT_NOACK (DW_IC_TX_ABRT_7B_ADDR_NOACK | \ |
| 148 | DW_IC_TX_ABRT_10ADDR1_NOACK | \ |
| 149 | DW_IC_TX_ABRT_10ADDR2_NOACK | \ |
| 150 | DW_IC_TX_ABRT_TXDATA_NOACK | \ |
| 151 | DW_IC_TX_ABRT_GCALL_NOACK) |
| 152 | |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 153 | static char *abort_sources[] = { |
Shinya Kuribayashi | a0e06ea | 2009-11-06 21:52:22 +0900 | [diff] [blame] | 154 | [ABRT_7B_ADDR_NOACK] = |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 155 | "slave address not acknowledged (7bit mode)", |
Shinya Kuribayashi | a0e06ea | 2009-11-06 21:52:22 +0900 | [diff] [blame] | 156 | [ABRT_10ADDR1_NOACK] = |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 157 | "first address byte not acknowledged (10bit mode)", |
Shinya Kuribayashi | a0e06ea | 2009-11-06 21:52:22 +0900 | [diff] [blame] | 158 | [ABRT_10ADDR2_NOACK] = |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 159 | "second address byte not acknowledged (10bit mode)", |
Shinya Kuribayashi | a0e06ea | 2009-11-06 21:52:22 +0900 | [diff] [blame] | 160 | [ABRT_TXDATA_NOACK] = |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 161 | "data not acknowledged", |
Shinya Kuribayashi | a0e06ea | 2009-11-06 21:52:22 +0900 | [diff] [blame] | 162 | [ABRT_GCALL_NOACK] = |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 163 | "no acknowledgement for a general call", |
Shinya Kuribayashi | a0e06ea | 2009-11-06 21:52:22 +0900 | [diff] [blame] | 164 | [ABRT_GCALL_READ] = |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 165 | "read after general call", |
Shinya Kuribayashi | a0e06ea | 2009-11-06 21:52:22 +0900 | [diff] [blame] | 166 | [ABRT_SBYTE_ACKDET] = |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 167 | "start byte acknowledged", |
Shinya Kuribayashi | a0e06ea | 2009-11-06 21:52:22 +0900 | [diff] [blame] | 168 | [ABRT_SBYTE_NORSTRT] = |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 169 | "trying to send start byte when restart is disabled", |
Shinya Kuribayashi | a0e06ea | 2009-11-06 21:52:22 +0900 | [diff] [blame] | 170 | [ABRT_10B_RD_NORSTRT] = |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 171 | "trying to read when restart is disabled (10bit mode)", |
Shinya Kuribayashi | a0e06ea | 2009-11-06 21:52:22 +0900 | [diff] [blame] | 172 | [ABRT_MASTER_DIS] = |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 173 | "trying to use disabled adapter", |
Shinya Kuribayashi | a0e06ea | 2009-11-06 21:52:22 +0900 | [diff] [blame] | 174 | [ARB_LOST] = |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 175 | "lost arbitration", |
| 176 | }; |
| 177 | |
Jarkko Nikula | 8a43745 | 2015-08-31 17:31:31 +0300 | [diff] [blame] | 178 | static u32 dw_readl(struct dw_i2c_dev *dev, int offset) |
Jean-Hugues Deschenes | 7f27960 | 2011-10-06 11:26:25 -0700 | [diff] [blame] | 179 | { |
Stefan Roese | a8a9f3f | 2012-04-18 15:01:41 +0200 | [diff] [blame] | 180 | u32 value; |
Jean-Hugues Deschenes | 18c4089 | 2011-10-06 11:26:27 -0700 | [diff] [blame] | 181 | |
Stefan Roese | a8a9f3f | 2012-04-18 15:01:41 +0200 | [diff] [blame] | 182 | if (dev->accessor_flags & ACCESS_16BIT) |
Jisheng Zhang | 67105c5 | 2014-12-11 14:26:41 +0800 | [diff] [blame] | 183 | value = readw_relaxed(dev->base + offset) | |
| 184 | (readw_relaxed(dev->base + offset + 2) << 16); |
Stefan Roese | a8a9f3f | 2012-04-18 15:01:41 +0200 | [diff] [blame] | 185 | else |
Jisheng Zhang | 67105c5 | 2014-12-11 14:26:41 +0800 | [diff] [blame] | 186 | value = readl_relaxed(dev->base + offset); |
Stefan Roese | a8a9f3f | 2012-04-18 15:01:41 +0200 | [diff] [blame] | 187 | |
| 188 | if (dev->accessor_flags & ACCESS_SWAP) |
Jean-Hugues Deschenes | 18c4089 | 2011-10-06 11:26:27 -0700 | [diff] [blame] | 189 | return swab32(value); |
| 190 | else |
| 191 | return value; |
Jean-Hugues Deschenes | 7f27960 | 2011-10-06 11:26:25 -0700 | [diff] [blame] | 192 | } |
| 193 | |
Jarkko Nikula | 8a43745 | 2015-08-31 17:31:31 +0300 | [diff] [blame] | 194 | static void dw_writel(struct dw_i2c_dev *dev, u32 b, int offset) |
Jean-Hugues Deschenes | 7f27960 | 2011-10-06 11:26:25 -0700 | [diff] [blame] | 195 | { |
Stefan Roese | a8a9f3f | 2012-04-18 15:01:41 +0200 | [diff] [blame] | 196 | if (dev->accessor_flags & ACCESS_SWAP) |
Jean-Hugues Deschenes | 18c4089 | 2011-10-06 11:26:27 -0700 | [diff] [blame] | 197 | b = swab32(b); |
| 198 | |
Stefan Roese | a8a9f3f | 2012-04-18 15:01:41 +0200 | [diff] [blame] | 199 | if (dev->accessor_flags & ACCESS_16BIT) { |
Jisheng Zhang | 67105c5 | 2014-12-11 14:26:41 +0800 | [diff] [blame] | 200 | writew_relaxed((u16)b, dev->base + offset); |
| 201 | writew_relaxed((u16)(b >> 16), dev->base + offset + 2); |
Stefan Roese | a8a9f3f | 2012-04-18 15:01:41 +0200 | [diff] [blame] | 202 | } else { |
Jisheng Zhang | 67105c5 | 2014-12-11 14:26:41 +0800 | [diff] [blame] | 203 | writel_relaxed(b, dev->base + offset); |
Stefan Roese | a8a9f3f | 2012-04-18 15:01:41 +0200 | [diff] [blame] | 204 | } |
Jean-Hugues Deschenes | 7f27960 | 2011-10-06 11:26:25 -0700 | [diff] [blame] | 205 | } |
| 206 | |
Shinya Kuribayashi | d60c7e8 | 2009-11-06 21:47:01 +0900 | [diff] [blame] | 207 | static u32 |
| 208 | i2c_dw_scl_hcnt(u32 ic_clk, u32 tSYMBOL, u32 tf, int cond, int offset) |
| 209 | { |
| 210 | /* |
| 211 | * DesignWare I2C core doesn't seem to have solid strategy to meet |
| 212 | * the tHD;STA timing spec. Configuring _HCNT based on tHIGH spec |
| 213 | * will result in violation of the tHD;STA spec. |
| 214 | */ |
| 215 | if (cond) |
| 216 | /* |
| 217 | * Conditional expression: |
| 218 | * |
| 219 | * IC_[FS]S_SCL_HCNT + (1+4+3) >= IC_CLK * tHIGH |
| 220 | * |
| 221 | * This is based on the DW manuals, and represents an ideal |
| 222 | * configuration. The resulting I2C bus speed will be |
| 223 | * faster than any of the others. |
| 224 | * |
| 225 | * If your hardware is free from tHD;STA issue, try this one. |
| 226 | */ |
Romain Baeriswyl | 6468276 | 2014-01-20 17:43:43 +0100 | [diff] [blame] | 227 | return (ic_clk * tSYMBOL + 500000) / 1000000 - 8 + offset; |
Shinya Kuribayashi | d60c7e8 | 2009-11-06 21:47:01 +0900 | [diff] [blame] | 228 | else |
| 229 | /* |
| 230 | * Conditional expression: |
| 231 | * |
| 232 | * IC_[FS]S_SCL_HCNT + 3 >= IC_CLK * (tHD;STA + tf) |
| 233 | * |
| 234 | * This is just experimental rule; the tHD;STA period turned |
| 235 | * out to be proportinal to (_HCNT + 3). With this setting, |
| 236 | * we could meet both tHIGH and tHD;STA timing specs. |
| 237 | * |
| 238 | * If unsure, you'd better to take this alternative. |
| 239 | * |
| 240 | * The reason why we need to take into account "tf" here, |
| 241 | * is the same as described in i2c_dw_scl_lcnt(). |
| 242 | */ |
Romain Baeriswyl | 6468276 | 2014-01-20 17:43:43 +0100 | [diff] [blame] | 243 | return (ic_clk * (tSYMBOL + tf) + 500000) / 1000000 |
| 244 | - 3 + offset; |
Shinya Kuribayashi | d60c7e8 | 2009-11-06 21:47:01 +0900 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | static u32 i2c_dw_scl_lcnt(u32 ic_clk, u32 tLOW, u32 tf, int offset) |
| 248 | { |
| 249 | /* |
| 250 | * Conditional expression: |
| 251 | * |
| 252 | * IC_[FS]S_SCL_LCNT + 1 >= IC_CLK * (tLOW + tf) |
| 253 | * |
| 254 | * DW I2C core starts counting the SCL CNTs for the LOW period |
| 255 | * of the SCL clock (tLOW) as soon as it pulls the SCL line. |
| 256 | * In order to meet the tLOW timing spec, we need to take into |
| 257 | * account the fall time of SCL signal (tf). Default tf value |
| 258 | * should be 0.3 us, for safety. |
| 259 | */ |
Romain Baeriswyl | 6468276 | 2014-01-20 17:43:43 +0100 | [diff] [blame] | 260 | return ((ic_clk * (tLOW + tf) + 500000) / 1000000) - 1 + offset; |
Shinya Kuribayashi | d60c7e8 | 2009-11-06 21:47:01 +0900 | [diff] [blame] | 261 | } |
| 262 | |
Mika Westerberg | 3ca4ed8 | 2013-04-10 00:36:40 +0000 | [diff] [blame] | 263 | static void __i2c_dw_enable(struct dw_i2c_dev *dev, bool enable) |
| 264 | { |
José Roberto de Souza | 2702ea7 | 2016-08-23 19:18:53 -0300 | [diff] [blame] | 265 | dw_writel(dev, enable, DW_IC_ENABLE); |
| 266 | } |
| 267 | |
| 268 | static void __i2c_dw_enable_and_wait(struct dw_i2c_dev *dev, bool enable) |
| 269 | { |
Mika Westerberg | 3ca4ed8 | 2013-04-10 00:36:40 +0000 | [diff] [blame] | 270 | int timeout = 100; |
| 271 | |
| 272 | do { |
José Roberto de Souza | 2702ea7 | 2016-08-23 19:18:53 -0300 | [diff] [blame] | 273 | __i2c_dw_enable(dev, enable); |
Mika Westerberg | 3ca4ed8 | 2013-04-10 00:36:40 +0000 | [diff] [blame] | 274 | if ((dw_readl(dev, DW_IC_ENABLE_STATUS) & 1) == enable) |
| 275 | return; |
| 276 | |
| 277 | /* |
| 278 | * Wait 10 times the signaling period of the highest I2C |
| 279 | * transfer supported by the driver (for 400KHz this is |
| 280 | * 25us) as described in the DesignWare I2C databook. |
| 281 | */ |
| 282 | usleep_range(25, 250); |
| 283 | } while (timeout--); |
| 284 | |
| 285 | dev_warn(dev->dev, "timeout in %sabling adapter\n", |
| 286 | enable ? "en" : "dis"); |
| 287 | } |
| 288 | |
Suravee Suthikulpanit | b33af11 | 2016-01-04 09:17:35 -0600 | [diff] [blame] | 289 | static unsigned long i2c_dw_clk_rate(struct dw_i2c_dev *dev) |
| 290 | { |
| 291 | /* |
| 292 | * Clock is not necessary if we got LCNT/HCNT values directly from |
| 293 | * the platform code. |
| 294 | */ |
| 295 | if (WARN_ON_ONCE(!dev->get_clk_rate_khz)) |
| 296 | return 0; |
| 297 | return dev->get_clk_rate_khz(dev); |
| 298 | } |
| 299 | |
Lucas De Marchi | 8c5660b | 2016-08-23 19:18:54 -0300 | [diff] [blame] | 300 | static int i2c_dw_acquire_lock(struct dw_i2c_dev *dev) |
| 301 | { |
| 302 | int ret; |
| 303 | |
| 304 | if (!dev->acquire_lock) |
| 305 | return 0; |
| 306 | |
| 307 | ret = dev->acquire_lock(dev); |
| 308 | if (!ret) |
| 309 | return 0; |
| 310 | |
| 311 | dev_err(dev->dev, "couldn't acquire bus ownership\n"); |
| 312 | |
| 313 | return ret; |
| 314 | } |
| 315 | |
| 316 | static void i2c_dw_release_lock(struct dw_i2c_dev *dev) |
| 317 | { |
| 318 | if (dev->release_lock) |
| 319 | dev->release_lock(dev); |
| 320 | } |
| 321 | |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 322 | /** |
| 323 | * i2c_dw_init() - initialize the designware i2c master hardware |
| 324 | * @dev: device private data |
| 325 | * |
| 326 | * This functions configures and enables the I2C master. |
| 327 | * This function is called during I2C init function, and in case of timeout at |
| 328 | * run time. |
| 329 | */ |
Dirk Brandewie | 2373f6b | 2011-10-29 10:57:23 +0100 | [diff] [blame] | 330 | int i2c_dw_init(struct dw_i2c_dev *dev) |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 331 | { |
Dirk Brandewie | e18563f | 2011-10-06 11:26:32 -0700 | [diff] [blame] | 332 | u32 hcnt, lcnt; |
Weifeng Voon | b6e6714 | 2016-08-12 17:02:51 +0300 | [diff] [blame] | 333 | u32 reg, comp_param1; |
Romain Baeriswyl | 6468276 | 2014-01-20 17:43:43 +0100 | [diff] [blame] | 334 | u32 sda_falling_time, scl_falling_time; |
David Box | c0601d2 | 2015-01-15 01:12:16 -0800 | [diff] [blame] | 335 | int ret; |
| 336 | |
Lucas De Marchi | 8c5660b | 2016-08-23 19:18:54 -0300 | [diff] [blame] | 337 | ret = i2c_dw_acquire_lock(dev); |
| 338 | if (ret) |
| 339 | return ret; |
Dirk Brandewie | 4a423a8 | 2011-10-06 11:26:28 -0700 | [diff] [blame] | 340 | |
Dirk Brandewie | 4a423a8 | 2011-10-06 11:26:28 -0700 | [diff] [blame] | 341 | reg = dw_readl(dev, DW_IC_COMP_TYPE); |
| 342 | if (reg == ___constant_swab32(DW_IC_COMP_TYPE_VALUE)) { |
Stefan Roese | a8a9f3f | 2012-04-18 15:01:41 +0200 | [diff] [blame] | 343 | /* Configure register endianess access */ |
| 344 | dev->accessor_flags |= ACCESS_SWAP; |
| 345 | } else if (reg == (DW_IC_COMP_TYPE_VALUE & 0x0000ffff)) { |
| 346 | /* Configure register access mode 16bit */ |
| 347 | dev->accessor_flags |= ACCESS_16BIT; |
| 348 | } else if (reg != DW_IC_COMP_TYPE_VALUE) { |
Dirk Brandewie | 4a423a8 | 2011-10-06 11:26:28 -0700 | [diff] [blame] | 349 | dev_err(dev->dev, "Unknown Synopsys component type: " |
| 350 | "0x%08x\n", reg); |
Lucas De Marchi | 8c5660b | 2016-08-23 19:18:54 -0300 | [diff] [blame] | 351 | i2c_dw_release_lock(dev); |
Dirk Brandewie | 4a423a8 | 2011-10-06 11:26:28 -0700 | [diff] [blame] | 352 | return -ENODEV; |
| 353 | } |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 354 | |
Weifeng Voon | b6e6714 | 2016-08-12 17:02:51 +0300 | [diff] [blame] | 355 | comp_param1 = dw_readl(dev, DW_IC_COMP_PARAM_1); |
| 356 | |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 357 | /* Disable the adapter */ |
José Roberto de Souza | 2702ea7 | 2016-08-23 19:18:53 -0300 | [diff] [blame] | 358 | __i2c_dw_enable_and_wait(dev, false); |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 359 | |
| 360 | /* set standard and fast speed deviders for high/low periods */ |
Shinya Kuribayashi | d60c7e8 | 2009-11-06 21:47:01 +0900 | [diff] [blame] | 361 | |
Romain Baeriswyl | 6468276 | 2014-01-20 17:43:43 +0100 | [diff] [blame] | 362 | sda_falling_time = dev->sda_falling_time ?: 300; /* ns */ |
| 363 | scl_falling_time = dev->scl_falling_time ?: 300; /* ns */ |
| 364 | |
Jarkko Nikula | 42ffd39 | 2015-01-23 11:35:55 +0200 | [diff] [blame] | 365 | /* Set SCL timing parameters for standard-mode */ |
Mika Westerberg | defc0b2 | 2013-08-19 15:07:53 +0300 | [diff] [blame] | 366 | if (dev->ss_hcnt && dev->ss_lcnt) { |
| 367 | hcnt = dev->ss_hcnt; |
| 368 | lcnt = dev->ss_lcnt; |
Jarkko Nikula | 42ffd39 | 2015-01-23 11:35:55 +0200 | [diff] [blame] | 369 | } else { |
Suravee Suthikulpanit | b33af11 | 2016-01-04 09:17:35 -0600 | [diff] [blame] | 370 | hcnt = i2c_dw_scl_hcnt(i2c_dw_clk_rate(dev), |
Jarkko Nikula | 42ffd39 | 2015-01-23 11:35:55 +0200 | [diff] [blame] | 371 | 4000, /* tHD;STA = tHIGH = 4.0 us */ |
| 372 | sda_falling_time, |
| 373 | 0, /* 0: DW default, 1: Ideal */ |
| 374 | 0); /* No offset */ |
Suravee Suthikulpanit | b33af11 | 2016-01-04 09:17:35 -0600 | [diff] [blame] | 375 | lcnt = i2c_dw_scl_lcnt(i2c_dw_clk_rate(dev), |
Jarkko Nikula | 42ffd39 | 2015-01-23 11:35:55 +0200 | [diff] [blame] | 376 | 4700, /* tLOW = 4.7 us */ |
| 377 | scl_falling_time, |
| 378 | 0); /* No offset */ |
Mika Westerberg | defc0b2 | 2013-08-19 15:07:53 +0300 | [diff] [blame] | 379 | } |
Jean-Hugues Deschenes | 7f27960 | 2011-10-06 11:26:25 -0700 | [diff] [blame] | 380 | dw_writel(dev, hcnt, DW_IC_SS_SCL_HCNT); |
| 381 | dw_writel(dev, lcnt, DW_IC_SS_SCL_LCNT); |
Shinya Kuribayashi | d60c7e8 | 2009-11-06 21:47:01 +0900 | [diff] [blame] | 382 | dev_dbg(dev->dev, "Standard-mode HCNT:LCNT = %d:%d\n", hcnt, lcnt); |
| 383 | |
Weifeng Voon | d608c3d | 2016-08-12 17:02:49 +0300 | [diff] [blame] | 384 | /* Set SCL timing parameters for fast-mode or fast-mode plus */ |
| 385 | if ((dev->clk_freq == 1000000) && dev->fp_hcnt && dev->fp_lcnt) { |
| 386 | hcnt = dev->fp_hcnt; |
| 387 | lcnt = dev->fp_lcnt; |
| 388 | } else if (dev->fs_hcnt && dev->fs_lcnt) { |
Mika Westerberg | defc0b2 | 2013-08-19 15:07:53 +0300 | [diff] [blame] | 389 | hcnt = dev->fs_hcnt; |
| 390 | lcnt = dev->fs_lcnt; |
Jarkko Nikula | 42ffd39 | 2015-01-23 11:35:55 +0200 | [diff] [blame] | 391 | } else { |
Suravee Suthikulpanit | b33af11 | 2016-01-04 09:17:35 -0600 | [diff] [blame] | 392 | hcnt = i2c_dw_scl_hcnt(i2c_dw_clk_rate(dev), |
Jarkko Nikula | 42ffd39 | 2015-01-23 11:35:55 +0200 | [diff] [blame] | 393 | 600, /* tHD;STA = tHIGH = 0.6 us */ |
| 394 | sda_falling_time, |
| 395 | 0, /* 0: DW default, 1: Ideal */ |
| 396 | 0); /* No offset */ |
Suravee Suthikulpanit | b33af11 | 2016-01-04 09:17:35 -0600 | [diff] [blame] | 397 | lcnt = i2c_dw_scl_lcnt(i2c_dw_clk_rate(dev), |
Jarkko Nikula | 42ffd39 | 2015-01-23 11:35:55 +0200 | [diff] [blame] | 398 | 1300, /* tLOW = 1.3 us */ |
| 399 | scl_falling_time, |
| 400 | 0); /* No offset */ |
Mika Westerberg | defc0b2 | 2013-08-19 15:07:53 +0300 | [diff] [blame] | 401 | } |
Jean-Hugues Deschenes | 7f27960 | 2011-10-06 11:26:25 -0700 | [diff] [blame] | 402 | dw_writel(dev, hcnt, DW_IC_FS_SCL_HCNT); |
| 403 | dw_writel(dev, lcnt, DW_IC_FS_SCL_LCNT); |
Shinya Kuribayashi | d60c7e8 | 2009-11-06 21:47:01 +0900 | [diff] [blame] | 404 | dev_dbg(dev->dev, "Fast-mode HCNT:LCNT = %d:%d\n", hcnt, lcnt); |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 405 | |
Weifeng Voon | b6e6714 | 2016-08-12 17:02:51 +0300 | [diff] [blame] | 406 | if ((dev->master_cfg & DW_IC_CON_SPEED_MASK) == |
| 407 | DW_IC_CON_SPEED_HIGH) { |
| 408 | if ((comp_param1 & DW_IC_COMP_PARAM_1_SPEED_MODE_MASK) |
| 409 | != DW_IC_COMP_PARAM_1_SPEED_MODE_HIGH) { |
| 410 | dev_err(dev->dev, "High Speed not supported!\n"); |
| 411 | dev->master_cfg &= ~DW_IC_CON_SPEED_MASK; |
| 412 | dev->master_cfg |= DW_IC_CON_SPEED_FAST; |
| 413 | } else if (dev->hs_hcnt && dev->hs_lcnt) { |
| 414 | hcnt = dev->hs_hcnt; |
| 415 | lcnt = dev->hs_lcnt; |
| 416 | dw_writel(dev, hcnt, DW_IC_HS_SCL_HCNT); |
| 417 | dw_writel(dev, lcnt, DW_IC_HS_SCL_LCNT); |
| 418 | dev_dbg(dev->dev, "HighSpeed-mode HCNT:LCNT = %d:%d\n", |
| 419 | hcnt, lcnt); |
| 420 | } |
| 421 | } |
| 422 | |
Christian Ruppert | 9803f86 | 2013-06-26 10:55:06 +0200 | [diff] [blame] | 423 | /* Configure SDA Hold Time if required */ |
Zhuo-hao Lee | 664d58b | 2016-08-27 15:39:30 +0800 | [diff] [blame] | 424 | reg = dw_readl(dev, DW_IC_COMP_VERSION); |
| 425 | if (reg >= DW_IC_SDA_HOLD_MIN_VERS) { |
Jarkko Nikula | 171e23e | 2016-09-29 16:04:59 +0300 | [diff] [blame] | 426 | if (!dev->sda_hold_time) { |
Zhuo-hao Lee | 664d58b | 2016-08-27 15:39:30 +0800 | [diff] [blame] | 427 | /* Keep previous hold time setting if no one set it */ |
| 428 | dev->sda_hold_time = dw_readl(dev, DW_IC_SDA_HOLD); |
| 429 | } |
Jarkko Nikula | 171e23e | 2016-09-29 16:04:59 +0300 | [diff] [blame] | 430 | /* |
| 431 | * Workaround for avoiding TX arbitration lost in case I2C |
| 432 | * slave pulls SDA down "too quickly" after falling egde of |
| 433 | * SCL by enabling non-zero SDA RX hold. Specification says it |
| 434 | * extends incoming SDA low to high transition while SCL is |
| 435 | * high but it apprears to help also above issue. |
| 436 | */ |
| 437 | if (!(dev->sda_hold_time & DW_IC_SDA_HOLD_RX_MASK)) |
| 438 | dev->sda_hold_time |= 1 << DW_IC_SDA_HOLD_RX_SHIFT; |
| 439 | dw_writel(dev, dev->sda_hold_time, DW_IC_SDA_HOLD); |
Zhuo-hao Lee | 664d58b | 2016-08-27 15:39:30 +0800 | [diff] [blame] | 440 | } else { |
| 441 | dev_warn(dev->dev, |
| 442 | "Hardware too old to adjust SDA hold time.\n"); |
Christian Ruppert | 9803f86 | 2013-06-26 10:55:06 +0200 | [diff] [blame] | 443 | } |
| 444 | |
Shinya Kuribayashi | 4cb6d1d | 2009-11-06 21:48:12 +0900 | [diff] [blame] | 445 | /* Configure Tx/Rx FIFO threshold levels */ |
Andrew Jackson | d39f77b | 2014-11-07 12:10:44 +0000 | [diff] [blame] | 446 | dw_writel(dev, dev->tx_fifo_depth / 2, DW_IC_TX_TL); |
Jean-Hugues Deschenes | 7f27960 | 2011-10-06 11:26:25 -0700 | [diff] [blame] | 447 | dw_writel(dev, 0, DW_IC_RX_TL); |
Shinya Kuribayashi | 4cb6d1d | 2009-11-06 21:48:12 +0900 | [diff] [blame] | 448 | |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 449 | /* configure the i2c master */ |
Dirk Brandewie | e18563f | 2011-10-06 11:26:32 -0700 | [diff] [blame] | 450 | dw_writel(dev, dev->master_cfg , DW_IC_CON); |
David Box | c0601d2 | 2015-01-15 01:12:16 -0800 | [diff] [blame] | 451 | |
Lucas De Marchi | 8c5660b | 2016-08-23 19:18:54 -0300 | [diff] [blame] | 452 | i2c_dw_release_lock(dev); |
| 453 | |
Dirk Brandewie | 4a423a8 | 2011-10-06 11:26:28 -0700 | [diff] [blame] | 454 | return 0; |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 455 | } |
Axel Lin | e68bb91 | 2012-09-10 10:14:02 +0200 | [diff] [blame] | 456 | EXPORT_SYMBOL_GPL(i2c_dw_init); |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 457 | |
| 458 | /* |
| 459 | * Waiting for bus not busy |
| 460 | */ |
| 461 | static int i2c_dw_wait_bus_not_busy(struct dw_i2c_dev *dev) |
| 462 | { |
| 463 | int timeout = TIMEOUT; |
| 464 | |
Jean-Hugues Deschenes | 7f27960 | 2011-10-06 11:26:25 -0700 | [diff] [blame] | 465 | while (dw_readl(dev, DW_IC_STATUS) & DW_IC_STATUS_ACTIVITY) { |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 466 | if (timeout <= 0) { |
| 467 | dev_warn(dev->dev, "timeout waiting for bus ready\n"); |
| 468 | return -ETIMEDOUT; |
| 469 | } |
| 470 | timeout--; |
Mika Westerberg | 1451b91 | 2013-04-10 00:36:41 +0000 | [diff] [blame] | 471 | usleep_range(1000, 1100); |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 472 | } |
| 473 | |
| 474 | return 0; |
| 475 | } |
| 476 | |
Shinya Kuribayashi | 81e798b | 2009-11-06 21:48:55 +0900 | [diff] [blame] | 477 | static void i2c_dw_xfer_init(struct dw_i2c_dev *dev) |
| 478 | { |
| 479 | struct i2c_msg *msgs = dev->msgs; |
Lucas De Marchi | 63d0f0a6 | 2016-08-23 19:18:55 -0300 | [diff] [blame] | 480 | u32 ic_tar = 0; |
Lucas De Marchi | 0317e6c | 2016-08-23 19:18:56 -0300 | [diff] [blame] | 481 | bool enabled; |
Shinya Kuribayashi | 81e798b | 2009-11-06 21:48:55 +0900 | [diff] [blame] | 482 | |
Lucas De Marchi | 0317e6c | 2016-08-23 19:18:56 -0300 | [diff] [blame] | 483 | enabled = dw_readl(dev, DW_IC_ENABLE_STATUS) & 1; |
Shinya Kuribayashi | 81e798b | 2009-11-06 21:48:55 +0900 | [diff] [blame] | 484 | |
Lucas De Marchi | 0317e6c | 2016-08-23 19:18:56 -0300 | [diff] [blame] | 485 | if (enabled) { |
| 486 | u32 ic_status; |
| 487 | |
Chew, Chiau Ee | bd63ace | 2013-09-27 02:57:35 +0800 | [diff] [blame] | 488 | /* |
Lucas De Marchi | 0317e6c | 2016-08-23 19:18:56 -0300 | [diff] [blame] | 489 | * Only disable adapter if ic_tar and ic_con can't be |
| 490 | * dynamically updated |
Chew, Chiau Ee | bd63ace | 2013-09-27 02:57:35 +0800 | [diff] [blame] | 491 | */ |
Lucas De Marchi | 0317e6c | 2016-08-23 19:18:56 -0300 | [diff] [blame] | 492 | ic_status = dw_readl(dev, DW_IC_STATUS); |
| 493 | if (!dev->dynamic_tar_update_enabled || |
| 494 | (ic_status & DW_IC_STATUS_MST_ACTIVITY) || |
| 495 | !(ic_status & DW_IC_STATUS_TFE)) { |
| 496 | __i2c_dw_enable_and_wait(dev, false); |
| 497 | enabled = false; |
| 498 | } |
Chew, Chiau Ee | bd63ace | 2013-09-27 02:57:35 +0800 | [diff] [blame] | 499 | } |
| 500 | |
Shinya Kuribayashi | 81e798b | 2009-11-06 21:48:55 +0900 | [diff] [blame] | 501 | /* if the slave address is ten bit address, enable 10BITADDR */ |
Lucas De Marchi | 63d0f0a6 | 2016-08-23 19:18:55 -0300 | [diff] [blame] | 502 | if (dev->dynamic_tar_update_enabled) { |
Shinya Kuribayashi | 81e798b | 2009-11-06 21:48:55 +0900 | [diff] [blame] | 503 | /* |
| 504 | * If I2C_DYNAMIC_TAR_UPDATE is set, the 10-bit addressing |
Lucas De Marchi | 63d0f0a6 | 2016-08-23 19:18:55 -0300 | [diff] [blame] | 505 | * mode has to be enabled via bit 12 of IC_TAR register, |
| 506 | * otherwise bit 4 of IC_CON is used. |
Shinya Kuribayashi | 81e798b | 2009-11-06 21:48:55 +0900 | [diff] [blame] | 507 | */ |
Lucas De Marchi | 63d0f0a6 | 2016-08-23 19:18:55 -0300 | [diff] [blame] | 508 | if (msgs[dev->msg_write_idx].flags & I2C_M_TEN) |
| 509 | ic_tar = DW_IC_TAR_10BITADDR_MASTER; |
Shinya Kuribayashi | 81e798b | 2009-11-06 21:48:55 +0900 | [diff] [blame] | 510 | } else { |
Lucas De Marchi | 63d0f0a6 | 2016-08-23 19:18:55 -0300 | [diff] [blame] | 511 | u32 ic_con = dw_readl(dev, DW_IC_CON); |
Shinya Kuribayashi | 81e798b | 2009-11-06 21:48:55 +0900 | [diff] [blame] | 512 | |
Lucas De Marchi | 63d0f0a6 | 2016-08-23 19:18:55 -0300 | [diff] [blame] | 513 | if (msgs[dev->msg_write_idx].flags & I2C_M_TEN) |
| 514 | ic_con |= DW_IC_CON_10BITADDR_MASTER; |
| 515 | else |
| 516 | ic_con &= ~DW_IC_CON_10BITADDR_MASTER; |
| 517 | dw_writel(dev, ic_con, DW_IC_CON); |
| 518 | } |
Shinya Kuribayashi | 81e798b | 2009-11-06 21:48:55 +0900 | [diff] [blame] | 519 | |
Chew, Chiau Ee | bd63ace | 2013-09-27 02:57:35 +0800 | [diff] [blame] | 520 | /* |
| 521 | * Set the slave (target) address and enable 10-bit addressing mode |
| 522 | * if applicable. |
| 523 | */ |
| 524 | dw_writel(dev, msgs[dev->msg_write_idx].addr | ic_tar, DW_IC_TAR); |
| 525 | |
Du, Wenkai | 47bb27e | 2014-04-10 23:03:19 +0000 | [diff] [blame] | 526 | /* enforce disabled interrupts (due to HW issues) */ |
| 527 | i2c_dw_disable_int(dev); |
| 528 | |
Lucas De Marchi | 0317e6c | 2016-08-23 19:18:56 -0300 | [diff] [blame] | 529 | if (!enabled) |
| 530 | __i2c_dw_enable(dev, true); |
Shinya Kuribayashi | 201d6a7 | 2009-11-06 21:50:40 +0900 | [diff] [blame] | 531 | |
Mika Westerberg | 2a2d95e | 2013-05-13 00:54:30 +0000 | [diff] [blame] | 532 | /* Clear and enable interrupts */ |
Jarkko Nikula | c335631 | 2015-08-31 17:31:28 +0300 | [diff] [blame] | 533 | dw_readl(dev, DW_IC_CLR_INTR); |
Jean-Hugues Deschenes | 7f27960 | 2011-10-06 11:26:25 -0700 | [diff] [blame] | 534 | dw_writel(dev, DW_IC_INTR_DEFAULT_MASK, DW_IC_INTR_MASK); |
Shinya Kuribayashi | 81e798b | 2009-11-06 21:48:55 +0900 | [diff] [blame] | 535 | } |
| 536 | |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 537 | /* |
Shinya Kuribayashi | 201d6a7 | 2009-11-06 21:50:40 +0900 | [diff] [blame] | 538 | * Initiate (and continue) low level master read/write transaction. |
| 539 | * This function is only called from i2c_dw_isr, and pumping i2c_msg |
| 540 | * messages into the tx buffer. Even if the size of i2c_msg data is |
| 541 | * longer than the size of the tx buffer, it handles everything. |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 542 | */ |
Jean Delvare | bccd780 | 2012-10-05 22:23:53 +0200 | [diff] [blame] | 543 | static void |
Shinya Kuribayashi | e77cf23 | 2009-11-06 21:46:04 +0900 | [diff] [blame] | 544 | i2c_dw_xfer_msg(struct dw_i2c_dev *dev) |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 545 | { |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 546 | struct i2c_msg *msgs = dev->msgs; |
Shinya Kuribayashi | 81e798b | 2009-11-06 21:48:55 +0900 | [diff] [blame] | 547 | u32 intr_mask; |
Shinya Kuribayashi | ae72222 | 2009-11-06 21:49:39 +0900 | [diff] [blame] | 548 | int tx_limit, rx_limit; |
Shinya Kuribayashi | ed5e1dd | 2009-11-06 21:43:52 +0900 | [diff] [blame] | 549 | u32 addr = msgs[dev->msg_write_idx].addr; |
| 550 | u32 buf_len = dev->tx_buf_len; |
Justin P. Mattock | 6993248 | 2011-07-26 23:06:29 -0700 | [diff] [blame] | 551 | u8 *buf = dev->tx_buf; |
Chew, Chiau Ee | 8256424 | 2013-06-21 15:05:28 +0800 | [diff] [blame] | 552 | bool need_restart = false; |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 553 | |
Shinya Kuribayashi | 201d6a7 | 2009-11-06 21:50:40 +0900 | [diff] [blame] | 554 | intr_mask = DW_IC_INTR_DEFAULT_MASK; |
Shinya Kuribayashi | c70c5cd | 2009-11-06 21:47:30 +0900 | [diff] [blame] | 555 | |
Shinya Kuribayashi | 6d2ea48 | 2009-11-06 21:46:29 +0900 | [diff] [blame] | 556 | for (; dev->msg_write_idx < dev->msgs_num; dev->msg_write_idx++) { |
Tin Huynh | c3ae106 | 2016-11-10 09:56:33 +0700 | [diff] [blame^] | 557 | u32 flags = msgs[dev->msg_write_idx].flags; |
| 558 | |
Shinya Kuribayashi | a0e06ea | 2009-11-06 21:52:22 +0900 | [diff] [blame] | 559 | /* |
| 560 | * if target address has changed, we need to |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 561 | * reprogram the target address in the i2c |
| 562 | * adapter when we are done with this transfer |
| 563 | */ |
Shinya Kuribayashi | 8f588e4 | 2009-11-06 21:51:18 +0900 | [diff] [blame] | 564 | if (msgs[dev->msg_write_idx].addr != addr) { |
| 565 | dev_err(dev->dev, |
| 566 | "%s: invalid target address\n", __func__); |
| 567 | dev->msg_err = -EINVAL; |
| 568 | break; |
| 569 | } |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 570 | |
| 571 | if (msgs[dev->msg_write_idx].len == 0) { |
| 572 | dev_err(dev->dev, |
| 573 | "%s: invalid message length\n", __func__); |
| 574 | dev->msg_err = -EINVAL; |
Shinya Kuribayashi | 8f588e4 | 2009-11-06 21:51:18 +0900 | [diff] [blame] | 575 | break; |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 576 | } |
| 577 | |
| 578 | if (!(dev->status & STATUS_WRITE_IN_PROGRESS)) { |
| 579 | /* new i2c_msg */ |
Shinya Kuribayashi | 26ea15b | 2009-11-06 21:49:14 +0900 | [diff] [blame] | 580 | buf = msgs[dev->msg_write_idx].buf; |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 581 | buf_len = msgs[dev->msg_write_idx].len; |
Chew, Chiau Ee | 8256424 | 2013-06-21 15:05:28 +0800 | [diff] [blame] | 582 | |
| 583 | /* If both IC_EMPTYFIFO_HOLD_MASTER_EN and |
| 584 | * IC_RESTART_EN are set, we must manually |
| 585 | * set restart bit between messages. |
| 586 | */ |
| 587 | if ((dev->master_cfg & DW_IC_CON_RESTART_EN) && |
| 588 | (dev->msg_write_idx > 0)) |
| 589 | need_restart = true; |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 590 | } |
| 591 | |
Jean-Hugues Deschenes | 7f27960 | 2011-10-06 11:26:25 -0700 | [diff] [blame] | 592 | tx_limit = dev->tx_fifo_depth - dw_readl(dev, DW_IC_TXFLR); |
| 593 | rx_limit = dev->rx_fifo_depth - dw_readl(dev, DW_IC_RXFLR); |
Shinya Kuribayashi | ae72222 | 2009-11-06 21:49:39 +0900 | [diff] [blame] | 594 | |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 595 | while (buf_len > 0 && tx_limit > 0 && rx_limit > 0) { |
Mika Westerberg | 17a76b4 | 2013-01-17 12:31:05 +0200 | [diff] [blame] | 596 | u32 cmd = 0; |
| 597 | |
| 598 | /* |
| 599 | * If IC_EMPTYFIFO_HOLD_MASTER_EN is set we must |
| 600 | * manually set the stop bit. However, it cannot be |
| 601 | * detected from the registers so we set it always |
| 602 | * when writing/reading the last byte. |
| 603 | */ |
Tin Huynh | c3ae106 | 2016-11-10 09:56:33 +0700 | [diff] [blame^] | 604 | |
| 605 | /* |
| 606 | * i2c-core.c always sets the buffer length of |
| 607 | * I2C_FUNC_SMBUS_BLOCK_DATA to 1. The length will |
| 608 | * be adjusted when receiving the first byte. |
| 609 | * Thus we can't stop the transaction here. |
| 610 | */ |
Mika Westerberg | 17a76b4 | 2013-01-17 12:31:05 +0200 | [diff] [blame] | 611 | if (dev->msg_write_idx == dev->msgs_num - 1 && |
Tin Huynh | c3ae106 | 2016-11-10 09:56:33 +0700 | [diff] [blame^] | 612 | buf_len == 1 && !(flags & I2C_M_RECV_LEN)) |
Mika Westerberg | 17a76b4 | 2013-01-17 12:31:05 +0200 | [diff] [blame] | 613 | cmd |= BIT(9); |
| 614 | |
Chew, Chiau Ee | 8256424 | 2013-06-21 15:05:28 +0800 | [diff] [blame] | 615 | if (need_restart) { |
| 616 | cmd |= BIT(10); |
| 617 | need_restart = false; |
| 618 | } |
| 619 | |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 620 | if (msgs[dev->msg_write_idx].flags & I2C_M_RD) { |
Josef Ahmad | e6f34ce | 2013-04-19 17:28:10 +0100 | [diff] [blame] | 621 | |
| 622 | /* avoid rx buffer overrun */ |
| 623 | if (rx_limit - dev->rx_outstanding <= 0) |
| 624 | break; |
| 625 | |
Mika Westerberg | 17a76b4 | 2013-01-17 12:31:05 +0200 | [diff] [blame] | 626 | dw_writel(dev, cmd | 0x100, DW_IC_DATA_CMD); |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 627 | rx_limit--; |
Josef Ahmad | e6f34ce | 2013-04-19 17:28:10 +0100 | [diff] [blame] | 628 | dev->rx_outstanding++; |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 629 | } else |
Mika Westerberg | 17a76b4 | 2013-01-17 12:31:05 +0200 | [diff] [blame] | 630 | dw_writel(dev, cmd | *buf++, DW_IC_DATA_CMD); |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 631 | tx_limit--; buf_len--; |
| 632 | } |
Shinya Kuribayashi | c70c5cd | 2009-11-06 21:47:30 +0900 | [diff] [blame] | 633 | |
Shinya Kuribayashi | 26ea15b | 2009-11-06 21:49:14 +0900 | [diff] [blame] | 634 | dev->tx_buf = buf; |
Shinya Kuribayashi | c70c5cd | 2009-11-06 21:47:30 +0900 | [diff] [blame] | 635 | dev->tx_buf_len = buf_len; |
| 636 | |
Tin Huynh | c3ae106 | 2016-11-10 09:56:33 +0700 | [diff] [blame^] | 637 | /* |
| 638 | * Because we don't know the buffer length in the |
| 639 | * I2C_FUNC_SMBUS_BLOCK_DATA case, we can't stop |
| 640 | * the transaction here. |
| 641 | */ |
| 642 | if (buf_len > 0 || flags & I2C_M_RECV_LEN) { |
Shinya Kuribayashi | c70c5cd | 2009-11-06 21:47:30 +0900 | [diff] [blame] | 643 | /* more bytes to be written */ |
Shinya Kuribayashi | c70c5cd | 2009-11-06 21:47:30 +0900 | [diff] [blame] | 644 | dev->status |= STATUS_WRITE_IN_PROGRESS; |
| 645 | break; |
Shinya Kuribayashi | 69151e5 | 2009-11-06 21:51:00 +0900 | [diff] [blame] | 646 | } else |
Shinya Kuribayashi | c70c5cd | 2009-11-06 21:47:30 +0900 | [diff] [blame] | 647 | dev->status &= ~STATUS_WRITE_IN_PROGRESS; |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 648 | } |
| 649 | |
Shinya Kuribayashi | 69151e5 | 2009-11-06 21:51:00 +0900 | [diff] [blame] | 650 | /* |
| 651 | * If i2c_msg index search is completed, we don't need TX_EMPTY |
| 652 | * interrupt any more. |
| 653 | */ |
| 654 | if (dev->msg_write_idx == dev->msgs_num) |
| 655 | intr_mask &= ~DW_IC_INTR_TX_EMPTY; |
| 656 | |
Shinya Kuribayashi | 8f588e4 | 2009-11-06 21:51:18 +0900 | [diff] [blame] | 657 | if (dev->msg_err) |
| 658 | intr_mask = 0; |
| 659 | |
Dirk Brandewie | 2373f6b | 2011-10-29 10:57:23 +0100 | [diff] [blame] | 660 | dw_writel(dev, intr_mask, DW_IC_INTR_MASK); |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 661 | } |
| 662 | |
Tin Huynh | c3ae106 | 2016-11-10 09:56:33 +0700 | [diff] [blame^] | 663 | static u8 |
| 664 | i2c_dw_recv_len(struct dw_i2c_dev *dev, u8 len) |
| 665 | { |
| 666 | struct i2c_msg *msgs = dev->msgs; |
| 667 | u32 flags = msgs[dev->msg_read_idx].flags; |
| 668 | |
| 669 | /* |
| 670 | * Adjust the buffer length and mask the flag |
| 671 | * after receiving the first byte. |
| 672 | */ |
| 673 | len += (flags & I2C_CLIENT_PEC) ? 2 : 1; |
| 674 | dev->tx_buf_len = len - min_t(u8, len, dev->rx_outstanding); |
| 675 | msgs[dev->msg_read_idx].len = len; |
| 676 | msgs[dev->msg_read_idx].flags &= ~I2C_M_RECV_LEN; |
| 677 | |
| 678 | return len; |
| 679 | } |
| 680 | |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 681 | static void |
Shinya Kuribayashi | 78839bd | 2009-11-06 21:45:39 +0900 | [diff] [blame] | 682 | i2c_dw_read(struct dw_i2c_dev *dev) |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 683 | { |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 684 | struct i2c_msg *msgs = dev->msgs; |
Shinya Kuribayashi | ae72222 | 2009-11-06 21:49:39 +0900 | [diff] [blame] | 685 | int rx_valid; |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 686 | |
Shinya Kuribayashi | 6d2ea48 | 2009-11-06 21:46:29 +0900 | [diff] [blame] | 687 | for (; dev->msg_read_idx < dev->msgs_num; dev->msg_read_idx++) { |
Shinya Kuribayashi | ed5e1dd | 2009-11-06 21:43:52 +0900 | [diff] [blame] | 688 | u32 len; |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 689 | u8 *buf; |
| 690 | |
| 691 | if (!(msgs[dev->msg_read_idx].flags & I2C_M_RD)) |
| 692 | continue; |
| 693 | |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 694 | if (!(dev->status & STATUS_READ_IN_PROGRESS)) { |
| 695 | len = msgs[dev->msg_read_idx].len; |
| 696 | buf = msgs[dev->msg_read_idx].buf; |
| 697 | } else { |
| 698 | len = dev->rx_buf_len; |
| 699 | buf = dev->rx_buf; |
| 700 | } |
| 701 | |
Jean-Hugues Deschenes | 7f27960 | 2011-10-06 11:26:25 -0700 | [diff] [blame] | 702 | rx_valid = dw_readl(dev, DW_IC_RXFLR); |
Shinya Kuribayashi | ae72222 | 2009-11-06 21:49:39 +0900 | [diff] [blame] | 703 | |
Josef Ahmad | e6f34ce | 2013-04-19 17:28:10 +0100 | [diff] [blame] | 704 | for (; len > 0 && rx_valid > 0; len--, rx_valid--) { |
Tin Huynh | c3ae106 | 2016-11-10 09:56:33 +0700 | [diff] [blame^] | 705 | u32 flags = msgs[dev->msg_read_idx].flags; |
| 706 | |
| 707 | *buf = dw_readl(dev, DW_IC_DATA_CMD); |
| 708 | /* Ensure length byte is a valid value */ |
| 709 | if (flags & I2C_M_RECV_LEN && |
| 710 | *buf <= I2C_SMBUS_BLOCK_MAX && *buf > 0) { |
| 711 | len = i2c_dw_recv_len(dev, *buf); |
| 712 | } |
| 713 | buf++; |
Josef Ahmad | e6f34ce | 2013-04-19 17:28:10 +0100 | [diff] [blame] | 714 | dev->rx_outstanding--; |
| 715 | } |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 716 | |
| 717 | if (len > 0) { |
| 718 | dev->status |= STATUS_READ_IN_PROGRESS; |
| 719 | dev->rx_buf_len = len; |
| 720 | dev->rx_buf = buf; |
| 721 | return; |
| 722 | } else |
| 723 | dev->status &= ~STATUS_READ_IN_PROGRESS; |
| 724 | } |
| 725 | } |
| 726 | |
Shinya Kuribayashi | ce6eb57 | 2009-11-06 21:51:57 +0900 | [diff] [blame] | 727 | static int i2c_dw_handle_tx_abort(struct dw_i2c_dev *dev) |
| 728 | { |
| 729 | unsigned long abort_source = dev->abort_source; |
| 730 | int i; |
| 731 | |
Shinya Kuribayashi | 6d1ea0f | 2009-11-16 20:40:14 +0900 | [diff] [blame] | 732 | if (abort_source & DW_IC_TX_ABRT_NOACK) { |
Akinobu Mita | 984b3f5 | 2010-03-05 13:41:37 -0800 | [diff] [blame] | 733 | for_each_set_bit(i, &abort_source, ARRAY_SIZE(abort_sources)) |
Shinya Kuribayashi | 6d1ea0f | 2009-11-16 20:40:14 +0900 | [diff] [blame] | 734 | dev_dbg(dev->dev, |
| 735 | "%s: %s\n", __func__, abort_sources[i]); |
| 736 | return -EREMOTEIO; |
| 737 | } |
| 738 | |
Akinobu Mita | 984b3f5 | 2010-03-05 13:41:37 -0800 | [diff] [blame] | 739 | for_each_set_bit(i, &abort_source, ARRAY_SIZE(abort_sources)) |
Shinya Kuribayashi | ce6eb57 | 2009-11-06 21:51:57 +0900 | [diff] [blame] | 740 | dev_err(dev->dev, "%s: %s\n", __func__, abort_sources[i]); |
| 741 | |
| 742 | if (abort_source & DW_IC_TX_ARB_LOST) |
| 743 | return -EAGAIN; |
Shinya Kuribayashi | ce6eb57 | 2009-11-06 21:51:57 +0900 | [diff] [blame] | 744 | else if (abort_source & DW_IC_TX_ABRT_GCALL_READ) |
| 745 | return -EINVAL; /* wrong msgs[] data */ |
| 746 | else |
| 747 | return -EIO; |
| 748 | } |
| 749 | |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 750 | /* |
Lucas De Marchi | 0317e6c | 2016-08-23 19:18:56 -0300 | [diff] [blame] | 751 | * Prepare controller for a transaction and start transfer by calling |
| 752 | * i2c_dw_xfer_init() |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 753 | */ |
Jarkko Nikula | d80d134 | 2015-10-12 16:55:35 +0300 | [diff] [blame] | 754 | static int |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 755 | i2c_dw_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], int num) |
| 756 | { |
| 757 | struct dw_i2c_dev *dev = i2c_get_adapdata(adap); |
| 758 | int ret; |
| 759 | |
| 760 | dev_dbg(dev->dev, "%s: msgs: %d\n", __func__, num); |
| 761 | |
Dirk Brandewie | 18dbdda | 2011-10-06 11:26:36 -0700 | [diff] [blame] | 762 | pm_runtime_get_sync(dev->dev); |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 763 | |
Wolfram Sang | 16735d0 | 2013-11-14 14:32:02 -0800 | [diff] [blame] | 764 | reinit_completion(&dev->cmd_complete); |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 765 | dev->msgs = msgs; |
| 766 | dev->msgs_num = num; |
| 767 | dev->cmd_err = 0; |
| 768 | dev->msg_write_idx = 0; |
| 769 | dev->msg_read_idx = 0; |
| 770 | dev->msg_err = 0; |
| 771 | dev->status = STATUS_IDLE; |
Shinya Kuribayashi | ce6eb57 | 2009-11-06 21:51:57 +0900 | [diff] [blame] | 772 | dev->abort_source = 0; |
Josef Ahmad | e6f34ce | 2013-04-19 17:28:10 +0100 | [diff] [blame] | 773 | dev->rx_outstanding = 0; |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 774 | |
Lucas De Marchi | 8c5660b | 2016-08-23 19:18:54 -0300 | [diff] [blame] | 775 | ret = i2c_dw_acquire_lock(dev); |
| 776 | if (ret) |
| 777 | goto done_nolock; |
David Box | c0601d2 | 2015-01-15 01:12:16 -0800 | [diff] [blame] | 778 | |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 779 | ret = i2c_dw_wait_bus_not_busy(dev); |
| 780 | if (ret < 0) |
| 781 | goto done; |
| 782 | |
| 783 | /* start the transfers */ |
Shinya Kuribayashi | 81e798b | 2009-11-06 21:48:55 +0900 | [diff] [blame] | 784 | i2c_dw_xfer_init(dev); |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 785 | |
| 786 | /* wait for tx to complete */ |
Weifeng Voon | d0bcd8d | 2016-06-17 09:46:35 +0800 | [diff] [blame] | 787 | if (!wait_for_completion_timeout(&dev->cmd_complete, adap->timeout)) { |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 788 | dev_err(dev->dev, "controller timed out\n"); |
Christian Ruppert | 38d7fad | 2013-06-07 10:51:23 +0200 | [diff] [blame] | 789 | /* i2c_dw_init implicitly disables the adapter */ |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 790 | i2c_dw_init(dev); |
| 791 | ret = -ETIMEDOUT; |
| 792 | goto done; |
Mika Westerberg | e42dba5 | 2013-05-22 13:03:11 +0300 | [diff] [blame] | 793 | } |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 794 | |
| 795 | if (dev->msg_err) { |
| 796 | ret = dev->msg_err; |
| 797 | goto done; |
| 798 | } |
| 799 | |
| 800 | /* no error */ |
| 801 | if (likely(!dev->cmd_err)) { |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 802 | ret = num; |
| 803 | goto done; |
| 804 | } |
| 805 | |
| 806 | /* We have an error */ |
| 807 | if (dev->cmd_err == DW_IC_ERR_TX_ABRT) { |
Shinya Kuribayashi | ce6eb57 | 2009-11-06 21:51:57 +0900 | [diff] [blame] | 808 | ret = i2c_dw_handle_tx_abort(dev); |
| 809 | goto done; |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 810 | } |
| 811 | ret = -EIO; |
| 812 | |
| 813 | done: |
Lucas De Marchi | 8c5660b | 2016-08-23 19:18:54 -0300 | [diff] [blame] | 814 | i2c_dw_release_lock(dev); |
David Box | c0601d2 | 2015-01-15 01:12:16 -0800 | [diff] [blame] | 815 | |
| 816 | done_nolock: |
Mika Westerberg | 4345233 | 2013-04-10 00:36:42 +0000 | [diff] [blame] | 817 | pm_runtime_mark_last_busy(dev->dev); |
| 818 | pm_runtime_put_autosuspend(dev->dev); |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 819 | |
| 820 | return ret; |
| 821 | } |
| 822 | |
Jarkko Nikula | d80d134 | 2015-10-12 16:55:35 +0300 | [diff] [blame] | 823 | static u32 i2c_dw_func(struct i2c_adapter *adap) |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 824 | { |
Dirk Brandewie | 2fa8326 | 2011-10-06 11:26:31 -0700 | [diff] [blame] | 825 | struct dw_i2c_dev *dev = i2c_get_adapdata(adap); |
| 826 | return dev->functionality; |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 827 | } |
Jarkko Nikula | d80d134 | 2015-10-12 16:55:35 +0300 | [diff] [blame] | 828 | |
| 829 | static struct i2c_algorithm i2c_dw_algo = { |
| 830 | .master_xfer = i2c_dw_xfer, |
| 831 | .functionality = i2c_dw_func, |
| 832 | }; |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 833 | |
Shinya Kuribayashi | e28000a | 2009-11-06 21:44:37 +0900 | [diff] [blame] | 834 | static u32 i2c_dw_read_clear_intrbits(struct dw_i2c_dev *dev) |
| 835 | { |
| 836 | u32 stat; |
| 837 | |
| 838 | /* |
| 839 | * The IC_INTR_STAT register just indicates "enabled" interrupts. |
| 840 | * Ths unmasked raw version of interrupt status bits are available |
| 841 | * in the IC_RAW_INTR_STAT register. |
| 842 | * |
| 843 | * That is, |
Dirk Brandewie | 2373f6b | 2011-10-29 10:57:23 +0100 | [diff] [blame] | 844 | * stat = dw_readl(IC_INTR_STAT); |
Shinya Kuribayashi | e28000a | 2009-11-06 21:44:37 +0900 | [diff] [blame] | 845 | * equals to, |
Dirk Brandewie | 2373f6b | 2011-10-29 10:57:23 +0100 | [diff] [blame] | 846 | * stat = dw_readl(IC_RAW_INTR_STAT) & dw_readl(IC_INTR_MASK); |
Shinya Kuribayashi | e28000a | 2009-11-06 21:44:37 +0900 | [diff] [blame] | 847 | * |
| 848 | * The raw version might be useful for debugging purposes. |
| 849 | */ |
Jean-Hugues Deschenes | 7f27960 | 2011-10-06 11:26:25 -0700 | [diff] [blame] | 850 | stat = dw_readl(dev, DW_IC_INTR_STAT); |
Shinya Kuribayashi | e28000a | 2009-11-06 21:44:37 +0900 | [diff] [blame] | 851 | |
| 852 | /* |
| 853 | * Do not use the IC_CLR_INTR register to clear interrupts, or |
| 854 | * you'll miss some interrupts, triggered during the period from |
Dirk Brandewie | 2373f6b | 2011-10-29 10:57:23 +0100 | [diff] [blame] | 855 | * dw_readl(IC_INTR_STAT) to dw_readl(IC_CLR_INTR). |
Shinya Kuribayashi | e28000a | 2009-11-06 21:44:37 +0900 | [diff] [blame] | 856 | * |
| 857 | * Instead, use the separately-prepared IC_CLR_* registers. |
| 858 | */ |
| 859 | if (stat & DW_IC_INTR_RX_UNDER) |
Jean-Hugues Deschenes | 7f27960 | 2011-10-06 11:26:25 -0700 | [diff] [blame] | 860 | dw_readl(dev, DW_IC_CLR_RX_UNDER); |
Shinya Kuribayashi | e28000a | 2009-11-06 21:44:37 +0900 | [diff] [blame] | 861 | if (stat & DW_IC_INTR_RX_OVER) |
Jean-Hugues Deschenes | 7f27960 | 2011-10-06 11:26:25 -0700 | [diff] [blame] | 862 | dw_readl(dev, DW_IC_CLR_RX_OVER); |
Shinya Kuribayashi | e28000a | 2009-11-06 21:44:37 +0900 | [diff] [blame] | 863 | if (stat & DW_IC_INTR_TX_OVER) |
Jean-Hugues Deschenes | 7f27960 | 2011-10-06 11:26:25 -0700 | [diff] [blame] | 864 | dw_readl(dev, DW_IC_CLR_TX_OVER); |
Shinya Kuribayashi | e28000a | 2009-11-06 21:44:37 +0900 | [diff] [blame] | 865 | if (stat & DW_IC_INTR_RD_REQ) |
Jean-Hugues Deschenes | 7f27960 | 2011-10-06 11:26:25 -0700 | [diff] [blame] | 866 | dw_readl(dev, DW_IC_CLR_RD_REQ); |
Shinya Kuribayashi | e28000a | 2009-11-06 21:44:37 +0900 | [diff] [blame] | 867 | if (stat & DW_IC_INTR_TX_ABRT) { |
| 868 | /* |
| 869 | * The IC_TX_ABRT_SOURCE register is cleared whenever |
| 870 | * the IC_CLR_TX_ABRT is read. Preserve it beforehand. |
| 871 | */ |
Jean-Hugues Deschenes | 7f27960 | 2011-10-06 11:26:25 -0700 | [diff] [blame] | 872 | dev->abort_source = dw_readl(dev, DW_IC_TX_ABRT_SOURCE); |
| 873 | dw_readl(dev, DW_IC_CLR_TX_ABRT); |
Shinya Kuribayashi | e28000a | 2009-11-06 21:44:37 +0900 | [diff] [blame] | 874 | } |
| 875 | if (stat & DW_IC_INTR_RX_DONE) |
Jean-Hugues Deschenes | 7f27960 | 2011-10-06 11:26:25 -0700 | [diff] [blame] | 876 | dw_readl(dev, DW_IC_CLR_RX_DONE); |
Shinya Kuribayashi | e28000a | 2009-11-06 21:44:37 +0900 | [diff] [blame] | 877 | if (stat & DW_IC_INTR_ACTIVITY) |
Jean-Hugues Deschenes | 7f27960 | 2011-10-06 11:26:25 -0700 | [diff] [blame] | 878 | dw_readl(dev, DW_IC_CLR_ACTIVITY); |
Shinya Kuribayashi | e28000a | 2009-11-06 21:44:37 +0900 | [diff] [blame] | 879 | if (stat & DW_IC_INTR_STOP_DET) |
Jean-Hugues Deschenes | 7f27960 | 2011-10-06 11:26:25 -0700 | [diff] [blame] | 880 | dw_readl(dev, DW_IC_CLR_STOP_DET); |
Shinya Kuribayashi | e28000a | 2009-11-06 21:44:37 +0900 | [diff] [blame] | 881 | if (stat & DW_IC_INTR_START_DET) |
Jean-Hugues Deschenes | 7f27960 | 2011-10-06 11:26:25 -0700 | [diff] [blame] | 882 | dw_readl(dev, DW_IC_CLR_START_DET); |
Shinya Kuribayashi | e28000a | 2009-11-06 21:44:37 +0900 | [diff] [blame] | 883 | if (stat & DW_IC_INTR_GEN_CALL) |
Jean-Hugues Deschenes | 7f27960 | 2011-10-06 11:26:25 -0700 | [diff] [blame] | 884 | dw_readl(dev, DW_IC_CLR_GEN_CALL); |
Shinya Kuribayashi | e28000a | 2009-11-06 21:44:37 +0900 | [diff] [blame] | 885 | |
| 886 | return stat; |
| 887 | } |
| 888 | |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 889 | /* |
| 890 | * Interrupt service routine. This gets called whenever an I2C interrupt |
| 891 | * occurs. |
| 892 | */ |
Jarkko Nikula | d80d134 | 2015-10-12 16:55:35 +0300 | [diff] [blame] | 893 | static irqreturn_t i2c_dw_isr(int this_irq, void *dev_id) |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 894 | { |
| 895 | struct dw_i2c_dev *dev = dev_id; |
Dirk Brandewie | af06cf6 | 2011-10-06 11:26:33 -0700 | [diff] [blame] | 896 | u32 stat, enabled; |
| 897 | |
| 898 | enabled = dw_readl(dev, DW_IC_ENABLE); |
| 899 | stat = dw_readl(dev, DW_IC_RAW_INTR_STAT); |
Jarkko Nikula | fb42746 | 2015-08-07 14:53:03 +0300 | [diff] [blame] | 900 | dev_dbg(dev->dev, "%s: enabled=%#x stat=%#x\n", __func__, enabled, stat); |
Dirk Brandewie | af06cf6 | 2011-10-06 11:26:33 -0700 | [diff] [blame] | 901 | if (!enabled || !(stat & ~DW_IC_INTR_ACTIVITY)) |
| 902 | return IRQ_NONE; |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 903 | |
Shinya Kuribayashi | e28000a | 2009-11-06 21:44:37 +0900 | [diff] [blame] | 904 | stat = i2c_dw_read_clear_intrbits(dev); |
Shinya Kuribayashi | e28000a | 2009-11-06 21:44:37 +0900 | [diff] [blame] | 905 | |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 906 | if (stat & DW_IC_INTR_TX_ABRT) { |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 907 | dev->cmd_err |= DW_IC_ERR_TX_ABRT; |
| 908 | dev->status = STATUS_IDLE; |
Shinya Kuribayashi | 597fe31 | 2009-11-06 21:51:36 +0900 | [diff] [blame] | 909 | |
| 910 | /* |
| 911 | * Anytime TX_ABRT is set, the contents of the tx/rx |
| 912 | * buffers are flushed. Make sure to skip them. |
| 913 | */ |
Jean-Hugues Deschenes | 7f27960 | 2011-10-06 11:26:25 -0700 | [diff] [blame] | 914 | dw_writel(dev, 0, DW_IC_INTR_MASK); |
Shinya Kuribayashi | 597fe31 | 2009-11-06 21:51:36 +0900 | [diff] [blame] | 915 | goto tx_aborted; |
Shinya Kuribayashi | 0774539 | 2009-11-06 21:47:51 +0900 | [diff] [blame] | 916 | } |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 917 | |
Shinya Kuribayashi | 21a89d4 | 2009-11-06 21:48:33 +0900 | [diff] [blame] | 918 | if (stat & DW_IC_INTR_RX_FULL) |
Shinya Kuribayashi | 0774539 | 2009-11-06 21:47:51 +0900 | [diff] [blame] | 919 | i2c_dw_read(dev); |
Shinya Kuribayashi | 21a89d4 | 2009-11-06 21:48:33 +0900 | [diff] [blame] | 920 | |
| 921 | if (stat & DW_IC_INTR_TX_EMPTY) |
Shinya Kuribayashi | 0774539 | 2009-11-06 21:47:51 +0900 | [diff] [blame] | 922 | i2c_dw_xfer_msg(dev); |
Shinya Kuribayashi | 0774539 | 2009-11-06 21:47:51 +0900 | [diff] [blame] | 923 | |
| 924 | /* |
| 925 | * No need to modify or disable the interrupt mask here. |
| 926 | * i2c_dw_xfer_msg() will take care of it according to |
| 927 | * the current transmit status. |
| 928 | */ |
| 929 | |
Shinya Kuribayashi | 597fe31 | 2009-11-06 21:51:36 +0900 | [diff] [blame] | 930 | tx_aborted: |
Lucas De Marchi | 0317e6c | 2016-08-23 19:18:56 -0300 | [diff] [blame] | 931 | if ((stat & (DW_IC_INTR_TX_ABRT | DW_IC_INTR_STOP_DET)) |
| 932 | || dev->msg_err) { |
| 933 | /* |
| 934 | * We must disable interruts before returning and signaling |
| 935 | * the end of the current transfer. Otherwise the hardware |
| 936 | * might continue generating interrupts for non-existent |
| 937 | * transfers. |
| 938 | */ |
| 939 | i2c_dw_disable_int(dev); |
| 940 | dw_readl(dev, DW_IC_CLR_INTR); |
| 941 | |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 942 | complete(&dev->cmd_complete); |
Lucas De Marchi | 0317e6c | 2016-08-23 19:18:56 -0300 | [diff] [blame] | 943 | } else if (unlikely(dev->accessor_flags & ACCESS_INTR_MASK)) { |
Xiangliang Yu | 2d244c8 | 2015-12-11 20:02:53 +0800 | [diff] [blame] | 944 | /* workaround to trigger pending interrupt */ |
| 945 | stat = dw_readl(dev, DW_IC_INTR_MASK); |
| 946 | i2c_dw_disable_int(dev); |
| 947 | dw_writel(dev, stat, DW_IC_INTR_MASK); |
| 948 | } |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 949 | |
| 950 | return IRQ_HANDLED; |
| 951 | } |
Dirk Brandewie | f3fa9f3 | 2011-10-06 11:26:34 -0700 | [diff] [blame] | 952 | |
Dirk Brandewie | f3fa9f3 | 2011-10-06 11:26:34 -0700 | [diff] [blame] | 953 | void i2c_dw_disable(struct dw_i2c_dev *dev) |
| 954 | { |
Dirk Brandewie | f3fa9f3 | 2011-10-06 11:26:34 -0700 | [diff] [blame] | 955 | /* Disable controller */ |
José Roberto de Souza | 2702ea7 | 2016-08-23 19:18:53 -0300 | [diff] [blame] | 956 | __i2c_dw_enable_and_wait(dev, false); |
Dirk Brandewie | f3fa9f3 | 2011-10-06 11:26:34 -0700 | [diff] [blame] | 957 | |
| 958 | /* Disable all interupts */ |
| 959 | dw_writel(dev, 0, DW_IC_INTR_MASK); |
| 960 | dw_readl(dev, DW_IC_CLR_INTR); |
| 961 | } |
Axel Lin | e68bb91 | 2012-09-10 10:14:02 +0200 | [diff] [blame] | 962 | EXPORT_SYMBOL_GPL(i2c_dw_disable); |
Dirk Brandewie | f3fa9f3 | 2011-10-06 11:26:34 -0700 | [diff] [blame] | 963 | |
Dirk Brandewie | f3fa9f3 | 2011-10-06 11:26:34 -0700 | [diff] [blame] | 964 | void i2c_dw_disable_int(struct dw_i2c_dev *dev) |
| 965 | { |
| 966 | dw_writel(dev, 0, DW_IC_INTR_MASK); |
| 967 | } |
Axel Lin | e68bb91 | 2012-09-10 10:14:02 +0200 | [diff] [blame] | 968 | EXPORT_SYMBOL_GPL(i2c_dw_disable_int); |
Dirk Brandewie | f3fa9f3 | 2011-10-06 11:26:34 -0700 | [diff] [blame] | 969 | |
| 970 | u32 i2c_dw_read_comp_param(struct dw_i2c_dev *dev) |
| 971 | { |
| 972 | return dw_readl(dev, DW_IC_COMP_PARAM_1); |
| 973 | } |
Axel Lin | e68bb91 | 2012-09-10 10:14:02 +0200 | [diff] [blame] | 974 | EXPORT_SYMBOL_GPL(i2c_dw_read_comp_param); |
Mika Westerberg | 9dd3162 | 2013-01-17 12:31:04 +0200 | [diff] [blame] | 975 | |
Jarkko Nikula | d80d134 | 2015-10-12 16:55:35 +0300 | [diff] [blame] | 976 | int i2c_dw_probe(struct dw_i2c_dev *dev) |
| 977 | { |
| 978 | struct i2c_adapter *adap = &dev->adapter; |
| 979 | int r; |
Lucas De Marchi | 63d0f0a6 | 2016-08-23 19:18:55 -0300 | [diff] [blame] | 980 | u32 reg; |
Jarkko Nikula | d80d134 | 2015-10-12 16:55:35 +0300 | [diff] [blame] | 981 | |
| 982 | init_completion(&dev->cmd_complete); |
Jarkko Nikula | d80d134 | 2015-10-12 16:55:35 +0300 | [diff] [blame] | 983 | |
| 984 | r = i2c_dw_init(dev); |
| 985 | if (r) |
| 986 | return r; |
| 987 | |
Lucas De Marchi | 63d0f0a6 | 2016-08-23 19:18:55 -0300 | [diff] [blame] | 988 | r = i2c_dw_acquire_lock(dev); |
| 989 | if (r) |
| 990 | return r; |
| 991 | |
| 992 | /* |
| 993 | * Test if dynamic TAR update is enabled in this controller by writing |
| 994 | * to IC_10BITADDR_MASTER field in IC_CON: when it is enabled this |
| 995 | * field is read-only so it should not succeed |
| 996 | */ |
| 997 | reg = dw_readl(dev, DW_IC_CON); |
| 998 | dw_writel(dev, reg ^ DW_IC_CON_10BITADDR_MASTER, DW_IC_CON); |
| 999 | |
| 1000 | if ((dw_readl(dev, DW_IC_CON) & DW_IC_CON_10BITADDR_MASTER) == |
| 1001 | (reg & DW_IC_CON_10BITADDR_MASTER)) { |
| 1002 | dev->dynamic_tar_update_enabled = true; |
| 1003 | dev_dbg(dev->dev, "Dynamic TAR update enabled"); |
| 1004 | } |
| 1005 | |
| 1006 | i2c_dw_release_lock(dev); |
| 1007 | |
Jarkko Nikula | d80d134 | 2015-10-12 16:55:35 +0300 | [diff] [blame] | 1008 | snprintf(adap->name, sizeof(adap->name), |
| 1009 | "Synopsys DesignWare I2C adapter"); |
Baruch Siach | 8d22f30 | 2015-12-23 18:43:24 +0200 | [diff] [blame] | 1010 | adap->retries = 3; |
Jarkko Nikula | d80d134 | 2015-10-12 16:55:35 +0300 | [diff] [blame] | 1011 | adap->algo = &i2c_dw_algo; |
| 1012 | adap->dev.parent = dev->dev; |
| 1013 | i2c_set_adapdata(adap, dev); |
| 1014 | |
| 1015 | i2c_dw_disable_int(dev); |
Andy Shevchenko | 08c6e8c | 2016-01-15 22:02:12 +0200 | [diff] [blame] | 1016 | r = devm_request_irq(dev->dev, dev->irq, i2c_dw_isr, |
| 1017 | IRQF_SHARED | IRQF_COND_SUSPEND, |
Jarkko Nikula | d80d134 | 2015-10-12 16:55:35 +0300 | [diff] [blame] | 1018 | dev_name(dev->dev), dev); |
| 1019 | if (r) { |
| 1020 | dev_err(dev->dev, "failure requesting irq %i: %d\n", |
| 1021 | dev->irq, r); |
| 1022 | return r; |
| 1023 | } |
| 1024 | |
Jarkko Nikula | cd998de | 2016-02-11 16:36:03 +0200 | [diff] [blame] | 1025 | /* |
| 1026 | * Increment PM usage count during adapter registration in order to |
| 1027 | * avoid possible spurious runtime suspend when adapter device is |
| 1028 | * registered to the device core and immediate resume in case bus has |
| 1029 | * registered I2C slaves that do I2C transfers in their probe. |
| 1030 | */ |
| 1031 | pm_runtime_get_noresume(dev->dev); |
Jarkko Nikula | d80d134 | 2015-10-12 16:55:35 +0300 | [diff] [blame] | 1032 | r = i2c_add_numbered_adapter(adap); |
| 1033 | if (r) |
| 1034 | dev_err(dev->dev, "failure adding adapter: %d\n", r); |
Jarkko Nikula | cd998de | 2016-02-11 16:36:03 +0200 | [diff] [blame] | 1035 | pm_runtime_put_noidle(dev->dev); |
Jarkko Nikula | d80d134 | 2015-10-12 16:55:35 +0300 | [diff] [blame] | 1036 | |
| 1037 | return r; |
| 1038 | } |
| 1039 | EXPORT_SYMBOL_GPL(i2c_dw_probe); |
| 1040 | |
Mika Westerberg | 9dd3162 | 2013-01-17 12:31:04 +0200 | [diff] [blame] | 1041 | MODULE_DESCRIPTION("Synopsys DesignWare I2C bus adapter core"); |
| 1042 | MODULE_LICENSE("GPL"); |