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