Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  *  Mellanox BlueField I2C bus driver
0004  *
0005  *  Copyright (C) 2020 Mellanox Technologies, Ltd.
0006  */
0007 
0008 #include <linux/acpi.h>
0009 #include <linux/bitfield.h>
0010 #include <linux/delay.h>
0011 #include <linux/err.h>
0012 #include <linux/interrupt.h>
0013 #include <linux/i2c.h>
0014 #include <linux/io.h>
0015 #include <linux/kernel.h>
0016 #include <linux/module.h>
0017 #include <linux/mutex.h>
0018 #include <linux/of_device.h>
0019 #include <linux/platform_device.h>
0020 #include <linux/string.h>
0021 
0022 /* Defines what functionality is present. */
0023 #define MLXBF_I2C_FUNC_SMBUS_BLOCK \
0024     (I2C_FUNC_SMBUS_BLOCK_DATA | I2C_FUNC_SMBUS_BLOCK_PROC_CALL)
0025 
0026 #define MLXBF_I2C_FUNC_SMBUS_DEFAULT \
0027     (I2C_FUNC_SMBUS_BYTE      | I2C_FUNC_SMBUS_BYTE_DATA | \
0028      I2C_FUNC_SMBUS_WORD_DATA | I2C_FUNC_SMBUS_I2C_BLOCK | \
0029      I2C_FUNC_SMBUS_PROC_CALL)
0030 
0031 #define MLXBF_I2C_FUNC_ALL \
0032     (MLXBF_I2C_FUNC_SMBUS_DEFAULT | MLXBF_I2C_FUNC_SMBUS_BLOCK | \
0033      I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SLAVE)
0034 
0035 #define MLXBF_I2C_SMBUS_MAX        3
0036 
0037 /* Shared resources info in BlueField platforms. */
0038 
0039 #define MLXBF_I2C_COALESCE_TYU_ADDR    0x02801300
0040 #define MLXBF_I2C_COALESCE_TYU_SIZE    0x010
0041 
0042 #define MLXBF_I2C_GPIO_TYU_ADDR        0x02802000
0043 #define MLXBF_I2C_GPIO_TYU_SIZE        0x100
0044 
0045 #define MLXBF_I2C_COREPLL_TYU_ADDR     0x02800358
0046 #define MLXBF_I2C_COREPLL_TYU_SIZE     0x008
0047 
0048 #define MLXBF_I2C_COREPLL_YU_ADDR      0x02800c30
0049 #define MLXBF_I2C_COREPLL_YU_SIZE      0x00c
0050 
0051 #define MLXBF_I2C_SHARED_RES_MAX       3
0052 
0053 /*
0054  * Note that the following SMBus, CAUSE, GPIO and PLL register addresses
0055  * refer to their respective offsets relative to the corresponding
0056  * memory-mapped region whose addresses are specified in either the DT or
0057  * the ACPI tables or above.
0058  */
0059 
0060 /*
0061  * SMBus Master core clock frequency. Timing configurations are
0062  * strongly dependent on the core clock frequency of the SMBus
0063  * Master. Default value is set to 400MHz.
0064  */
0065 #define MLXBF_I2C_TYU_PLL_OUT_FREQ  (400 * 1000 * 1000)
0066 /* Reference clock for Bluefield - 156 MHz. */
0067 #define MLXBF_I2C_PLL_IN_FREQ       156250000ULL
0068 
0069 /* Constant used to determine the PLL frequency. */
0070 #define MLNXBF_I2C_COREPLL_CONST    16384ULL
0071 
0072 #define MLXBF_I2C_FREQUENCY_1GHZ  1000000000ULL
0073 
0074 /* PLL registers. */
0075 #define MLXBF_I2C_CORE_PLL_REG1         0x4
0076 #define MLXBF_I2C_CORE_PLL_REG2         0x8
0077 
0078 /* OR cause register. */
0079 #define MLXBF_I2C_CAUSE_OR_EVTEN0    0x14
0080 #define MLXBF_I2C_CAUSE_OR_CLEAR     0x18
0081 
0082 /* Arbiter Cause Register. */
0083 #define MLXBF_I2C_CAUSE_ARBITER      0x1c
0084 
0085 /*
0086  * Cause Status flags. Note that those bits might be considered
0087  * as interrupt enabled bits.
0088  */
0089 
0090 /* Transaction ended with STOP. */
0091 #define MLXBF_I2C_CAUSE_TRANSACTION_ENDED  BIT(0)
0092 /* Master arbitration lost. */
0093 #define MLXBF_I2C_CAUSE_M_ARBITRATION_LOST BIT(1)
0094 /* Unexpected start detected. */
0095 #define MLXBF_I2C_CAUSE_UNEXPECTED_START   BIT(2)
0096 /* Unexpected stop detected. */
0097 #define MLXBF_I2C_CAUSE_UNEXPECTED_STOP    BIT(3)
0098 /* Wait for transfer continuation. */
0099 #define MLXBF_I2C_CAUSE_WAIT_FOR_FW_DATA   BIT(4)
0100 /* Failed to generate STOP. */
0101 #define MLXBF_I2C_CAUSE_PUT_STOP_FAILED    BIT(5)
0102 /* Failed to generate START. */
0103 #define MLXBF_I2C_CAUSE_PUT_START_FAILED   BIT(6)
0104 /* Clock toggle completed. */
0105 #define MLXBF_I2C_CAUSE_CLK_TOGGLE_DONE    BIT(7)
0106 /* Transfer timeout occurred. */
0107 #define MLXBF_I2C_CAUSE_M_FW_TIMEOUT       BIT(8)
0108 /* Master busy bit reset. */
0109 #define MLXBF_I2C_CAUSE_M_GW_BUSY_FALL     BIT(9)
0110 
0111 #define MLXBF_I2C_CAUSE_MASTER_ARBITER_BITS_MASK     GENMASK(9, 0)
0112 
0113 #define MLXBF_I2C_CAUSE_MASTER_STATUS_ERROR \
0114     (MLXBF_I2C_CAUSE_M_ARBITRATION_LOST | \
0115      MLXBF_I2C_CAUSE_UNEXPECTED_START | \
0116      MLXBF_I2C_CAUSE_UNEXPECTED_STOP | \
0117      MLXBF_I2C_CAUSE_PUT_STOP_FAILED | \
0118      MLXBF_I2C_CAUSE_PUT_START_FAILED | \
0119      MLXBF_I2C_CAUSE_CLK_TOGGLE_DONE | \
0120      MLXBF_I2C_CAUSE_M_FW_TIMEOUT)
0121 
0122 /*
0123  * Slave cause status flags. Note that those bits might be considered
0124  * as interrupt enabled bits.
0125  */
0126 
0127 /* Write transaction received successfully. */
0128 #define MLXBF_I2C_CAUSE_WRITE_SUCCESS         BIT(0)
0129 /* Read transaction received, waiting for response. */
0130 #define MLXBF_I2C_CAUSE_READ_WAIT_FW_RESPONSE BIT(13)
0131 /* Slave busy bit reset. */
0132 #define MLXBF_I2C_CAUSE_S_GW_BUSY_FALL        BIT(18)
0133 
0134 #define MLXBF_I2C_CAUSE_SLAVE_ARBITER_BITS_MASK     GENMASK(20, 0)
0135 
0136 /* Cause coalesce registers. */
0137 #define MLXBF_I2C_CAUSE_COALESCE_0        0x00
0138 #define MLXBF_I2C_CAUSE_COALESCE_1        0x04
0139 #define MLXBF_I2C_CAUSE_COALESCE_2        0x08
0140 
0141 #define MLXBF_I2C_CAUSE_TYU_SLAVE_BIT   MLXBF_I2C_SMBUS_MAX
0142 #define MLXBF_I2C_CAUSE_YU_SLAVE_BIT    1
0143 
0144 /* Functional enable register. */
0145 #define MLXBF_I2C_GPIO_0_FUNC_EN_0    0x28
0146 /* Force OE enable register. */
0147 #define MLXBF_I2C_GPIO_0_FORCE_OE_EN  0x30
0148 /*
0149  * Note that Smbus GWs are on GPIOs 30:25. Two pins are used to control
0150  * SDA/SCL lines:
0151  *
0152  *  SMBUS GW0 -> bits[26:25]
0153  *  SMBUS GW1 -> bits[28:27]
0154  *  SMBUS GW2 -> bits[30:29]
0155  */
0156 #define MLXBF_I2C_GPIO_SMBUS_GW_PINS(num) (25 + ((num) << 1))
0157 
0158 /* Note that gw_id can be 0,1 or 2. */
0159 #define MLXBF_I2C_GPIO_SMBUS_GW_MASK(num) \
0160     (0xffffffff & (~(0x3 << MLXBF_I2C_GPIO_SMBUS_GW_PINS(num))))
0161 
0162 #define MLXBF_I2C_GPIO_SMBUS_GW_RESET_PINS(num, val) \
0163     ((val) & MLXBF_I2C_GPIO_SMBUS_GW_MASK(num))
0164 
0165 #define MLXBF_I2C_GPIO_SMBUS_GW_ASSERT_PINS(num, val) \
0166     ((val) | (0x3 << MLXBF_I2C_GPIO_SMBUS_GW_PINS(num)))
0167 
0168 /* SMBus timing parameters. */
0169 #define MLXBF_I2C_SMBUS_TIMER_SCL_LOW_SCL_HIGH    0x00
0170 #define MLXBF_I2C_SMBUS_TIMER_FALL_RISE_SPIKE     0x04
0171 #define MLXBF_I2C_SMBUS_TIMER_THOLD               0x08
0172 #define MLXBF_I2C_SMBUS_TIMER_TSETUP_START_STOP   0x0c
0173 #define MLXBF_I2C_SMBUS_TIMER_TSETUP_DATA         0x10
0174 #define MLXBF_I2C_SMBUS_THIGH_MAX_TBUF            0x14
0175 #define MLXBF_I2C_SMBUS_SCL_LOW_TIMEOUT           0x18
0176 
0177 /*
0178  * Defines SMBus operating frequency and core clock frequency.
0179  * According to ADB files, default values are compliant to 100KHz SMBus
0180  * @ 400MHz core clock. The driver should be able to calculate core
0181  * frequency based on PLL parameters.
0182  */
0183 #define MLXBF_I2C_COREPLL_FREQ          MLXBF_I2C_TYU_PLL_OUT_FREQ
0184 
0185 /* Core PLL TYU configuration. */
0186 #define MLXBF_I2C_COREPLL_CORE_F_TYU_MASK   GENMASK(15, 3)
0187 #define MLXBF_I2C_COREPLL_CORE_OD_TYU_MASK  GENMASK(19, 16)
0188 #define MLXBF_I2C_COREPLL_CORE_R_TYU_MASK   GENMASK(25, 20)
0189 
0190 /* Core PLL YU configuration. */
0191 #define MLXBF_I2C_COREPLL_CORE_F_YU_MASK    GENMASK(25, 0)
0192 #define MLXBF_I2C_COREPLL_CORE_OD_YU_MASK   GENMASK(3, 0)
0193 #define MLXBF_I2C_COREPLL_CORE_R_YU_MASK    GENMASK(31, 26)
0194 
0195 
0196 /* Core PLL frequency. */
0197 static u64 mlxbf_i2c_corepll_frequency;
0198 
0199 /* SMBus Master GW. */
0200 #define MLXBF_I2C_SMBUS_MASTER_GW     0x200
0201 /* Number of bytes received and sent. */
0202 #define MLXBF_I2C_SMBUS_RS_BYTES      0x300
0203 /* Packet error check (PEC) value. */
0204 #define MLXBF_I2C_SMBUS_MASTER_PEC    0x304
0205 /* Status bits (ACK/NACK/FW Timeout). */
0206 #define MLXBF_I2C_SMBUS_MASTER_STATUS 0x308
0207 /* SMbus Master Finite State Machine. */
0208 #define MLXBF_I2C_SMBUS_MASTER_FSM    0x310
0209 
0210 /*
0211  * When enabled, the master will issue a stop condition in case of
0212  * timeout while waiting for FW response.
0213  */
0214 #define MLXBF_I2C_SMBUS_EN_FW_TIMEOUT 0x31c
0215 
0216 /* SMBus master GW control bits offset in MLXBF_I2C_SMBUS_MASTER_GW[31:3]. */
0217 #define MLXBF_I2C_MASTER_LOCK_BIT         BIT(31) /* Lock bit. */
0218 #define MLXBF_I2C_MASTER_BUSY_BIT         BIT(30) /* Busy bit. */
0219 #define MLXBF_I2C_MASTER_START_BIT        BIT(29) /* Control start. */
0220 #define MLXBF_I2C_MASTER_CTL_WRITE_BIT    BIT(28) /* Control write phase. */
0221 #define MLXBF_I2C_MASTER_CTL_READ_BIT     BIT(19) /* Control read phase. */
0222 #define MLXBF_I2C_MASTER_STOP_BIT         BIT(3)  /* Control stop. */
0223 
0224 #define MLXBF_I2C_MASTER_ENABLE \
0225     (MLXBF_I2C_MASTER_LOCK_BIT | MLXBF_I2C_MASTER_BUSY_BIT | \
0226      MLXBF_I2C_MASTER_START_BIT | MLXBF_I2C_MASTER_STOP_BIT)
0227 
0228 #define MLXBF_I2C_MASTER_ENABLE_WRITE \
0229     (MLXBF_I2C_MASTER_ENABLE | MLXBF_I2C_MASTER_CTL_WRITE_BIT)
0230 
0231 #define MLXBF_I2C_MASTER_ENABLE_READ \
0232     (MLXBF_I2C_MASTER_ENABLE | MLXBF_I2C_MASTER_CTL_READ_BIT)
0233 
0234 #define MLXBF_I2C_MASTER_SLV_ADDR_SHIFT   12 /* Slave address shift. */
0235 #define MLXBF_I2C_MASTER_WRITE_SHIFT      21 /* Control write bytes shift. */
0236 #define MLXBF_I2C_MASTER_SEND_PEC_SHIFT   20 /* Send PEC byte shift. */
0237 #define MLXBF_I2C_MASTER_PARSE_EXP_SHIFT  11 /* Parse expected bytes shift. */
0238 #define MLXBF_I2C_MASTER_READ_SHIFT       4  /* Control read bytes shift. */
0239 
0240 /* SMBus master GW Data descriptor. */
0241 #define MLXBF_I2C_MASTER_DATA_DESC_ADDR   0x280
0242 #define MLXBF_I2C_MASTER_DATA_DESC_SIZE   0x80 /* Size in bytes. */
0243 
0244 /* Maximum bytes to read/write per SMBus transaction. */
0245 #define MLXBF_I2C_MASTER_DATA_R_LENGTH  MLXBF_I2C_MASTER_DATA_DESC_SIZE
0246 #define MLXBF_I2C_MASTER_DATA_W_LENGTH (MLXBF_I2C_MASTER_DATA_DESC_SIZE - 1)
0247 
0248 /* All bytes were transmitted. */
0249 #define MLXBF_I2C_SMBUS_STATUS_BYTE_CNT_DONE      BIT(0)
0250 /* NACK received. */
0251 #define MLXBF_I2C_SMBUS_STATUS_NACK_RCV           BIT(1)
0252 /* Slave's byte count >128 bytes. */
0253 #define MLXBF_I2C_SMBUS_STATUS_READ_ERR           BIT(2)
0254 /* Timeout occurred. */
0255 #define MLXBF_I2C_SMBUS_STATUS_FW_TIMEOUT         BIT(3)
0256 
0257 #define MLXBF_I2C_SMBUS_MASTER_STATUS_MASK        GENMASK(3, 0)
0258 
0259 #define MLXBF_I2C_SMBUS_MASTER_STATUS_ERROR \
0260     (MLXBF_I2C_SMBUS_STATUS_NACK_RCV | \
0261      MLXBF_I2C_SMBUS_STATUS_READ_ERR | \
0262      MLXBF_I2C_SMBUS_STATUS_FW_TIMEOUT)
0263 
0264 #define MLXBF_I2C_SMBUS_MASTER_FSM_STOP_MASK      BIT(31)
0265 #define MLXBF_I2C_SMBUS_MASTER_FSM_PS_STATE_MASK  BIT(15)
0266 
0267 /* SMBus slave GW. */
0268 #define MLXBF_I2C_SMBUS_SLAVE_GW              0x400
0269 /* Number of bytes received and sent from/to master. */
0270 #define MLXBF_I2C_SMBUS_SLAVE_RS_MASTER_BYTES 0x500
0271 /* Packet error check (PEC) value. */
0272 #define MLXBF_I2C_SMBUS_SLAVE_PEC             0x504
0273 /* SMBus slave Finite State Machine (FSM). */
0274 #define MLXBF_I2C_SMBUS_SLAVE_FSM             0x510
0275 /*
0276  * Should be set when all raised causes handled, and cleared by HW on
0277  * every new cause.
0278  */
0279 #define MLXBF_I2C_SMBUS_SLAVE_READY           0x52c
0280 
0281 /* SMBus slave GW control bits offset in MLXBF_I2C_SMBUS_SLAVE_GW[31:19]. */
0282 #define MLXBF_I2C_SLAVE_BUSY_BIT         BIT(30) /* Busy bit. */
0283 #define MLXBF_I2C_SLAVE_WRITE_BIT        BIT(29) /* Control write enable. */
0284 
0285 #define MLXBF_I2C_SLAVE_ENABLE \
0286     (MLXBF_I2C_SLAVE_BUSY_BIT | MLXBF_I2C_SLAVE_WRITE_BIT)
0287 
0288 #define MLXBF_I2C_SLAVE_WRITE_BYTES_SHIFT 22 /* Number of bytes to write. */
0289 #define MLXBF_I2C_SLAVE_SEND_PEC_SHIFT    21 /* Send PEC byte shift. */
0290 
0291 /* SMBus slave GW Data descriptor. */
0292 #define MLXBF_I2C_SLAVE_DATA_DESC_ADDR   0x480
0293 #define MLXBF_I2C_SLAVE_DATA_DESC_SIZE   0x80 /* Size in bytes. */
0294 
0295 /* SMbus slave configuration registers. */
0296 #define MLXBF_I2C_SMBUS_SLAVE_ADDR_CFG        0x514
0297 #define MLXBF_I2C_SMBUS_SLAVE_ADDR_CNT        16
0298 #define MLXBF_I2C_SMBUS_SLAVE_ADDR_EN_BIT     7
0299 #define MLXBF_I2C_SMBUS_SLAVE_ADDR_MASK       GENMASK(6, 0)
0300 
0301 #define MLXBF_I2C_SLAVE_ADDR_ENABLED(addr) \
0302     ((addr) & (1 << MLXBF_I2C_SMBUS_SLAVE_ADDR_EN_BIT))
0303 
0304 /*
0305  * Timeout is given in microsends. Note also that timeout handling is not
0306  * exact.
0307  */
0308 #define MLXBF_I2C_SMBUS_TIMEOUT   (300 * 1000) /* 300ms */
0309 
0310 /* Encapsulates timing parameters. */
0311 struct mlxbf_i2c_timings {
0312     u16 scl_high;       /* Clock high period. */
0313     u16 scl_low;        /* Clock low period. */
0314     u8 sda_rise;        /* Data rise time. */
0315     u8 sda_fall;        /* Data fall time. */
0316     u8 scl_rise;        /* Clock rise time. */
0317     u8 scl_fall;        /* Clock fall time. */
0318     u16 hold_start;     /* Hold time after (REPEATED) START. */
0319     u16 hold_data;      /* Data hold time. */
0320     u16 setup_start;    /* REPEATED START condition setup time. */
0321     u16 setup_stop;     /* STOP condition setup time. */
0322     u16 setup_data;     /* Data setup time. */
0323     u16 pad;        /* Padding. */
0324     u16 buf;        /* Bus free time between STOP and START. */
0325     u16 thigh_max;      /* Thigh max. */
0326     u32 timeout;        /* Detect clock low timeout. */
0327 };
0328 
0329 enum {
0330     MLXBF_I2C_F_READ = BIT(0),
0331     MLXBF_I2C_F_WRITE = BIT(1),
0332     MLXBF_I2C_F_NORESTART = BIT(3),
0333     MLXBF_I2C_F_SMBUS_OPERATION = BIT(4),
0334     MLXBF_I2C_F_SMBUS_BLOCK = BIT(5),
0335     MLXBF_I2C_F_SMBUS_PEC = BIT(6),
0336     MLXBF_I2C_F_SMBUS_PROCESS_CALL = BIT(7),
0337 };
0338 
0339 struct mlxbf_i2c_smbus_operation {
0340     u32 flags;
0341     u32 length; /* Buffer length in bytes. */
0342     u8 *buffer;
0343 };
0344 
0345 #define MLXBF_I2C_SMBUS_OP_CNT_1    1
0346 #define MLXBF_I2C_SMBUS_OP_CNT_2    2
0347 #define MLXBF_I2C_SMBUS_OP_CNT_3    3
0348 #define MLXBF_I2C_SMBUS_MAX_OP_CNT  MLXBF_I2C_SMBUS_OP_CNT_3
0349 
0350 struct mlxbf_i2c_smbus_request {
0351     u8 slave;
0352     u8 operation_cnt;
0353     struct mlxbf_i2c_smbus_operation operation[MLXBF_I2C_SMBUS_MAX_OP_CNT];
0354 };
0355 
0356 struct mlxbf_i2c_resource {
0357     void __iomem *io;
0358     struct resource *params;
0359     struct mutex *lock; /* Mutex to protect mlxbf_i2c_resource. */
0360     u8 type;
0361 };
0362 
0363 /* List of chip resources that are being accessed by the driver. */
0364 enum {
0365     MLXBF_I2C_SMBUS_RES,
0366     MLXBF_I2C_MST_CAUSE_RES,
0367     MLXBF_I2C_SLV_CAUSE_RES,
0368     MLXBF_I2C_COALESCE_RES,
0369     MLXBF_I2C_COREPLL_RES,
0370     MLXBF_I2C_GPIO_RES,
0371     MLXBF_I2C_END_RES,
0372 };
0373 
0374 /* Helper macro to define an I2C resource parameters. */
0375 #define MLXBF_I2C_RES_PARAMS(addr, size, str) \
0376     { \
0377         .start = (addr), \
0378         .end = (addr) + (size) - 1, \
0379         .name = (str) \
0380     }
0381 
0382 static struct resource mlxbf_i2c_coalesce_tyu_params =
0383         MLXBF_I2C_RES_PARAMS(MLXBF_I2C_COALESCE_TYU_ADDR,
0384                      MLXBF_I2C_COALESCE_TYU_SIZE,
0385                      "COALESCE_MEM");
0386 static struct resource mlxbf_i2c_corepll_tyu_params =
0387         MLXBF_I2C_RES_PARAMS(MLXBF_I2C_COREPLL_TYU_ADDR,
0388                      MLXBF_I2C_COREPLL_TYU_SIZE,
0389                      "COREPLL_MEM");
0390 static struct resource mlxbf_i2c_corepll_yu_params =
0391         MLXBF_I2C_RES_PARAMS(MLXBF_I2C_COREPLL_YU_ADDR,
0392                      MLXBF_I2C_COREPLL_YU_SIZE,
0393                      "COREPLL_MEM");
0394 static struct resource mlxbf_i2c_gpio_tyu_params =
0395         MLXBF_I2C_RES_PARAMS(MLXBF_I2C_GPIO_TYU_ADDR,
0396                      MLXBF_I2C_GPIO_TYU_SIZE,
0397                      "GPIO_MEM");
0398 
0399 static struct mutex mlxbf_i2c_coalesce_lock;
0400 static struct mutex mlxbf_i2c_corepll_lock;
0401 static struct mutex mlxbf_i2c_gpio_lock;
0402 
0403 /* Mellanox BlueField chip type. */
0404 enum mlxbf_i2c_chip_type {
0405     MLXBF_I2C_CHIP_TYPE_1, /* Mellanox BlueField-1 chip. */
0406     MLXBF_I2C_CHIP_TYPE_2, /* Mallanox BlueField-2 chip. */
0407 };
0408 
0409 struct mlxbf_i2c_chip_info {
0410     enum mlxbf_i2c_chip_type type;
0411     /* Chip shared resources that are being used by the I2C controller. */
0412     struct mlxbf_i2c_resource *shared_res[MLXBF_I2C_SHARED_RES_MAX];
0413 
0414     /* Callback to calculate the core PLL frequency. */
0415     u64 (*calculate_freq)(struct mlxbf_i2c_resource *corepll_res);
0416 };
0417 
0418 struct mlxbf_i2c_priv {
0419     const struct mlxbf_i2c_chip_info *chip;
0420     struct i2c_adapter adap;
0421     struct mlxbf_i2c_resource *smbus;
0422     struct mlxbf_i2c_resource *mst_cause;
0423     struct mlxbf_i2c_resource *slv_cause;
0424     struct mlxbf_i2c_resource *coalesce;
0425     u64 frequency; /* Core frequency in Hz. */
0426     int bus; /* Physical bus identifier. */
0427     int irq;
0428     struct i2c_client *slave;
0429 };
0430 
0431 static struct mlxbf_i2c_resource mlxbf_i2c_coalesce_res[] = {
0432     [MLXBF_I2C_CHIP_TYPE_1] = {
0433         .params = &mlxbf_i2c_coalesce_tyu_params,
0434         .lock = &mlxbf_i2c_coalesce_lock,
0435         .type = MLXBF_I2C_COALESCE_RES
0436     },
0437     {}
0438 };
0439 
0440 static struct mlxbf_i2c_resource mlxbf_i2c_corepll_res[] = {
0441     [MLXBF_I2C_CHIP_TYPE_1] = {
0442         .params = &mlxbf_i2c_corepll_tyu_params,
0443         .lock = &mlxbf_i2c_corepll_lock,
0444         .type = MLXBF_I2C_COREPLL_RES
0445     },
0446     [MLXBF_I2C_CHIP_TYPE_2] = {
0447         .params = &mlxbf_i2c_corepll_yu_params,
0448         .lock = &mlxbf_i2c_corepll_lock,
0449         .type = MLXBF_I2C_COREPLL_RES,
0450     }
0451 };
0452 
0453 static struct mlxbf_i2c_resource mlxbf_i2c_gpio_res[] = {
0454     [MLXBF_I2C_CHIP_TYPE_1] = {
0455         .params = &mlxbf_i2c_gpio_tyu_params,
0456         .lock = &mlxbf_i2c_gpio_lock,
0457         .type = MLXBF_I2C_GPIO_RES
0458     },
0459     {}
0460 };
0461 
0462 static u8 mlxbf_i2c_bus_count;
0463 
0464 static struct mutex mlxbf_i2c_bus_lock;
0465 
0466 /* Polling frequency in microseconds. */
0467 #define MLXBF_I2C_POLL_FREQ_IN_USEC        200
0468 
0469 #define MLXBF_I2C_SHIFT_0   0
0470 #define MLXBF_I2C_SHIFT_8   8
0471 #define MLXBF_I2C_SHIFT_16  16
0472 #define MLXBF_I2C_SHIFT_24  24
0473 
0474 #define MLXBF_I2C_MASK_8    GENMASK(7, 0)
0475 #define MLXBF_I2C_MASK_16   GENMASK(15, 0)
0476 
0477 /*
0478  * Function to poll a set of bits at a specific address; it checks whether
0479  * the bits are equal to zero when eq_zero is set to 'true', and not equal
0480  * to zero when eq_zero is set to 'false'.
0481  * Note that the timeout is given in microseconds.
0482  */
0483 static u32 mlxbf_smbus_poll(void __iomem *io, u32 addr, u32 mask,
0484                 bool eq_zero, u32  timeout)
0485 {
0486     u32 bits;
0487 
0488     timeout = (timeout / MLXBF_I2C_POLL_FREQ_IN_USEC) + 1;
0489 
0490     do {
0491         bits = readl(io + addr) & mask;
0492         if (eq_zero ? bits == 0 : bits != 0)
0493             return eq_zero ? 1 : bits;
0494         udelay(MLXBF_I2C_POLL_FREQ_IN_USEC);
0495     } while (timeout-- != 0);
0496 
0497     return 0;
0498 }
0499 
0500 /*
0501  * SW must make sure that the SMBus Master GW is idle before starting
0502  * a transaction. Accordingly, this function polls the Master FSM stop
0503  * bit; it returns false when the bit is asserted, true if not.
0504  */
0505 static bool mlxbf_smbus_master_wait_for_idle(struct mlxbf_i2c_priv *priv)
0506 {
0507     u32 mask = MLXBF_I2C_SMBUS_MASTER_FSM_STOP_MASK;
0508     u32 addr = MLXBF_I2C_SMBUS_MASTER_FSM;
0509     u32 timeout = MLXBF_I2C_SMBUS_TIMEOUT;
0510 
0511     if (mlxbf_smbus_poll(priv->smbus->io, addr, mask, true, timeout))
0512         return true;
0513 
0514     return false;
0515 }
0516 
0517 static bool mlxbf_i2c_smbus_transaction_success(u32 master_status,
0518                         u32 cause_status)
0519 {
0520     /*
0521      * When transaction ended with STOP, all bytes were transmitted,
0522      * and no NACK received, then the transaction ended successfully.
0523      * On the other hand, when the GW is configured with the stop bit
0524      * de-asserted then the SMBus expects the following GW configuration
0525      * for transfer continuation.
0526      */
0527     if ((cause_status & MLXBF_I2C_CAUSE_WAIT_FOR_FW_DATA) ||
0528         ((cause_status & MLXBF_I2C_CAUSE_TRANSACTION_ENDED) &&
0529          (master_status & MLXBF_I2C_SMBUS_STATUS_BYTE_CNT_DONE) &&
0530          !(master_status & MLXBF_I2C_SMBUS_STATUS_NACK_RCV)))
0531         return true;
0532 
0533     return false;
0534 }
0535 
0536 /*
0537  * Poll SMBus master status and return transaction status,
0538  * i.e. whether succeeded or failed. I2C and SMBus fault codes
0539  * are returned as negative numbers from most calls, with zero
0540  * or some positive number indicating a non-fault return.
0541  */
0542 static int mlxbf_i2c_smbus_check_status(struct mlxbf_i2c_priv *priv)
0543 {
0544     u32 master_status_bits;
0545     u32 cause_status_bits;
0546 
0547     /*
0548      * GW busy bit is raised by the driver and cleared by the HW
0549      * when the transaction is completed. The busy bit is a good
0550      * indicator of transaction status. So poll the busy bit, and
0551      * then read the cause and master status bits to determine if
0552      * errors occurred during the transaction.
0553      */
0554     mlxbf_smbus_poll(priv->smbus->io, MLXBF_I2C_SMBUS_MASTER_GW,
0555              MLXBF_I2C_MASTER_BUSY_BIT, true,
0556              MLXBF_I2C_SMBUS_TIMEOUT);
0557 
0558     /* Read cause status bits. */
0559     cause_status_bits = readl(priv->mst_cause->io +
0560                     MLXBF_I2C_CAUSE_ARBITER);
0561     cause_status_bits &= MLXBF_I2C_CAUSE_MASTER_ARBITER_BITS_MASK;
0562 
0563     /*
0564      * Parse both Cause and Master GW bits, then return transaction status.
0565      */
0566 
0567     master_status_bits = readl(priv->smbus->io +
0568                     MLXBF_I2C_SMBUS_MASTER_STATUS);
0569     master_status_bits &= MLXBF_I2C_SMBUS_MASTER_STATUS_MASK;
0570 
0571     if (mlxbf_i2c_smbus_transaction_success(master_status_bits,
0572                         cause_status_bits))
0573         return 0;
0574 
0575     /*
0576      * In case of timeout on GW busy, the ISR will clear busy bit but
0577      * transaction ended bits cause will not be set so the transaction
0578      * fails. Then, we must check Master GW status bits.
0579      */
0580     if ((master_status_bits & MLXBF_I2C_SMBUS_MASTER_STATUS_ERROR) &&
0581         (cause_status_bits & (MLXBF_I2C_CAUSE_TRANSACTION_ENDED |
0582                   MLXBF_I2C_CAUSE_M_GW_BUSY_FALL)))
0583         return -EIO;
0584 
0585     if (cause_status_bits & MLXBF_I2C_CAUSE_MASTER_STATUS_ERROR)
0586         return -EAGAIN;
0587 
0588     return -ETIMEDOUT;
0589 }
0590 
0591 static void mlxbf_i2c_smbus_write_data(struct mlxbf_i2c_priv *priv,
0592                        const u8 *data, u8 length, u32 addr)
0593 {
0594     u8 offset, aligned_length;
0595     u32 data32;
0596 
0597     aligned_length = round_up(length, 4);
0598 
0599     /*
0600      * Copy data bytes from 4-byte aligned source buffer.
0601      * Data copied to the Master GW Data Descriptor MUST be shifted
0602      * left so the data starts at the MSB of the descriptor registers
0603      * as required by the underlying hardware. Enable byte swapping
0604      * when writing data bytes to the 32 * 32-bit HW Data registers
0605      * a.k.a Master GW Data Descriptor.
0606      */
0607     for (offset = 0; offset < aligned_length; offset += sizeof(u32)) {
0608         data32 = *((u32 *)(data + offset));
0609         iowrite32be(data32, priv->smbus->io + addr + offset);
0610     }
0611 }
0612 
0613 static void mlxbf_i2c_smbus_read_data(struct mlxbf_i2c_priv *priv,
0614                       u8 *data, u8 length, u32 addr)
0615 {
0616     u32 data32, mask;
0617     u8 byte, offset;
0618 
0619     mask = sizeof(u32) - 1;
0620 
0621     /*
0622      * Data bytes in the Master GW Data Descriptor are shifted left
0623      * so the data starts at the MSB of the descriptor registers as
0624      * set by the underlying hardware. Enable byte swapping while
0625      * reading data bytes from the 32 * 32-bit HW Data registers
0626      * a.k.a Master GW Data Descriptor.
0627      */
0628 
0629     for (offset = 0; offset < (length & ~mask); offset += sizeof(u32)) {
0630         data32 = ioread32be(priv->smbus->io + addr + offset);
0631         *((u32 *)(data + offset)) = data32;
0632     }
0633 
0634     if (!(length & mask))
0635         return;
0636 
0637     data32 = ioread32be(priv->smbus->io + addr + offset);
0638 
0639     for (byte = 0; byte < (length & mask); byte++) {
0640         data[offset + byte] = data32 & GENMASK(7, 0);
0641         data32 = ror32(data32, MLXBF_I2C_SHIFT_8);
0642     }
0643 }
0644 
0645 static int mlxbf_i2c_smbus_enable(struct mlxbf_i2c_priv *priv, u8 slave,
0646                   u8 len, u8 block_en, u8 pec_en, bool read)
0647 {
0648     u32 command;
0649 
0650     /* Set Master GW control word. */
0651     if (read) {
0652         command = MLXBF_I2C_MASTER_ENABLE_READ;
0653         command |= rol32(len, MLXBF_I2C_MASTER_READ_SHIFT);
0654     } else {
0655         command = MLXBF_I2C_MASTER_ENABLE_WRITE;
0656         command |= rol32(len, MLXBF_I2C_MASTER_WRITE_SHIFT);
0657     }
0658     command |= rol32(slave, MLXBF_I2C_MASTER_SLV_ADDR_SHIFT);
0659     command |= rol32(block_en, MLXBF_I2C_MASTER_PARSE_EXP_SHIFT);
0660     command |= rol32(pec_en, MLXBF_I2C_MASTER_SEND_PEC_SHIFT);
0661 
0662     /* Clear status bits. */
0663     writel(0x0, priv->smbus->io + MLXBF_I2C_SMBUS_MASTER_STATUS);
0664     /* Set the cause data. */
0665     writel(~0x0, priv->mst_cause->io + MLXBF_I2C_CAUSE_OR_CLEAR);
0666     /* Zero PEC byte. */
0667     writel(0x0, priv->smbus->io + MLXBF_I2C_SMBUS_MASTER_PEC);
0668     /* Zero byte count. */
0669     writel(0x0, priv->smbus->io + MLXBF_I2C_SMBUS_RS_BYTES);
0670 
0671     /* GW activation. */
0672     writel(command, priv->smbus->io + MLXBF_I2C_SMBUS_MASTER_GW);
0673 
0674     /*
0675      * Poll master status and check status bits. An ACK is sent when
0676      * completing writing data to the bus (Master 'byte_count_done' bit
0677      * is set to 1).
0678      */
0679     return mlxbf_i2c_smbus_check_status(priv);
0680 }
0681 
0682 static int
0683 mlxbf_i2c_smbus_start_transaction(struct mlxbf_i2c_priv *priv,
0684                   struct mlxbf_i2c_smbus_request *request)
0685 {
0686     u8 data_desc[MLXBF_I2C_MASTER_DATA_DESC_SIZE] = { 0 };
0687     u8 op_idx, data_idx, data_len, write_len, read_len;
0688     struct mlxbf_i2c_smbus_operation *operation;
0689     u8 read_en, write_en, block_en, pec_en;
0690     u8 slave, flags, addr;
0691     u8 *read_buf;
0692     int ret = 0;
0693 
0694     if (request->operation_cnt > MLXBF_I2C_SMBUS_MAX_OP_CNT)
0695         return -EINVAL;
0696 
0697     read_buf = NULL;
0698     data_idx = 0;
0699     read_en = 0;
0700     write_en = 0;
0701     write_len = 0;
0702     read_len = 0;
0703     block_en = 0;
0704     pec_en = 0;
0705     slave = request->slave & GENMASK(6, 0);
0706     addr = slave << 1;
0707 
0708     /* First of all, check whether the HW is idle. */
0709     if (WARN_ON(!mlxbf_smbus_master_wait_for_idle(priv)))
0710         return -EBUSY;
0711 
0712     /* Set first byte. */
0713     data_desc[data_idx++] = addr;
0714 
0715     for (op_idx = 0; op_idx < request->operation_cnt; op_idx++) {
0716         operation = &request->operation[op_idx];
0717         flags = operation->flags;
0718 
0719         /*
0720          * Note that read and write operations might be handled by a
0721          * single command. If the MLXBF_I2C_F_SMBUS_OPERATION is set
0722          * then write command byte and set the optional SMBus specific
0723          * bits such as block_en and pec_en. These bits MUST be
0724          * submitted by the first operation only.
0725          */
0726         if (op_idx == 0 && flags & MLXBF_I2C_F_SMBUS_OPERATION) {
0727             block_en = flags & MLXBF_I2C_F_SMBUS_BLOCK;
0728             pec_en = flags & MLXBF_I2C_F_SMBUS_PEC;
0729         }
0730 
0731         if (flags & MLXBF_I2C_F_WRITE) {
0732             write_en = 1;
0733             write_len += operation->length;
0734             if (data_idx + operation->length >
0735                     MLXBF_I2C_MASTER_DATA_DESC_SIZE)
0736                 return -ENOBUFS;
0737             memcpy(data_desc + data_idx,
0738                    operation->buffer, operation->length);
0739             data_idx += operation->length;
0740         }
0741         /*
0742          * We assume that read operations are performed only once per
0743          * SMBus transaction. *TBD* protect this statement so it won't
0744          * be executed twice? or return an error if we try to read more
0745          * than once?
0746          */
0747         if (flags & MLXBF_I2C_F_READ) {
0748             read_en = 1;
0749             /* Subtract 1 as required by HW. */
0750             read_len = operation->length - 1;
0751             read_buf = operation->buffer;
0752         }
0753     }
0754 
0755     /* Set Master GW data descriptor. */
0756     data_len = write_len + 1; /* Add one byte of the slave address. */
0757     /*
0758      * Note that data_len cannot be 0. Indeed, the slave address byte
0759      * must be written to the data registers.
0760      */
0761     mlxbf_i2c_smbus_write_data(priv, (const u8 *)data_desc, data_len,
0762                    MLXBF_I2C_MASTER_DATA_DESC_ADDR);
0763 
0764     if (write_en) {
0765         ret = mlxbf_i2c_smbus_enable(priv, slave, write_len, block_en,
0766                      pec_en, 0);
0767         if (ret)
0768             return ret;
0769     }
0770 
0771     if (read_en) {
0772         /* Write slave address to Master GW data descriptor. */
0773         mlxbf_i2c_smbus_write_data(priv, (const u8 *)&addr, 1,
0774                        MLXBF_I2C_MASTER_DATA_DESC_ADDR);
0775         ret = mlxbf_i2c_smbus_enable(priv, slave, read_len, block_en,
0776                      pec_en, 1);
0777         if (!ret) {
0778             /* Get Master GW data descriptor. */
0779             mlxbf_i2c_smbus_read_data(priv, data_desc, read_len + 1,
0780                          MLXBF_I2C_MASTER_DATA_DESC_ADDR);
0781 
0782             /* Get data from Master GW data descriptor. */
0783             memcpy(read_buf, data_desc, read_len + 1);
0784         }
0785 
0786         /*
0787          * After a read operation the SMBus FSM ps (present state)
0788          * needs to be 'manually' reset. This should be removed in
0789          * next tag integration.
0790          */
0791         writel(MLXBF_I2C_SMBUS_MASTER_FSM_PS_STATE_MASK,
0792             priv->smbus->io + MLXBF_I2C_SMBUS_MASTER_FSM);
0793     }
0794 
0795     return ret;
0796 }
0797 
0798 /* I2C SMBus protocols. */
0799 
0800 static void
0801 mlxbf_i2c_smbus_quick_command(struct mlxbf_i2c_smbus_request *request,
0802                   u8 read)
0803 {
0804     request->operation_cnt = MLXBF_I2C_SMBUS_OP_CNT_1;
0805 
0806     request->operation[0].length = 0;
0807     request->operation[0].flags = MLXBF_I2C_F_WRITE;
0808     request->operation[0].flags |= read ? MLXBF_I2C_F_READ : 0;
0809 }
0810 
0811 static void mlxbf_i2c_smbus_byte_func(struct mlxbf_i2c_smbus_request *request,
0812                       u8 *data, bool read, bool pec_check)
0813 {
0814     request->operation_cnt = MLXBF_I2C_SMBUS_OP_CNT_1;
0815 
0816     request->operation[0].length = 1;
0817     request->operation[0].length += pec_check;
0818 
0819     request->operation[0].flags = MLXBF_I2C_F_SMBUS_OPERATION;
0820     request->operation[0].flags |= read ?
0821                 MLXBF_I2C_F_READ : MLXBF_I2C_F_WRITE;
0822     request->operation[0].flags |= pec_check ? MLXBF_I2C_F_SMBUS_PEC : 0;
0823 
0824     request->operation[0].buffer = data;
0825 }
0826 
0827 static void
0828 mlxbf_i2c_smbus_data_byte_func(struct mlxbf_i2c_smbus_request *request,
0829                    u8 *command, u8 *data, bool read, bool pec_check)
0830 {
0831     request->operation_cnt = MLXBF_I2C_SMBUS_OP_CNT_2;
0832 
0833     request->operation[0].length = 1;
0834     request->operation[0].flags =
0835             MLXBF_I2C_F_SMBUS_OPERATION | MLXBF_I2C_F_WRITE;
0836     request->operation[0].flags |= pec_check ? MLXBF_I2C_F_SMBUS_PEC : 0;
0837     request->operation[0].buffer = command;
0838 
0839     request->operation[1].length = 1;
0840     request->operation[1].length += pec_check;
0841     request->operation[1].flags = read ?
0842                 MLXBF_I2C_F_READ : MLXBF_I2C_F_WRITE;
0843     request->operation[1].buffer = data;
0844 }
0845 
0846 static void
0847 mlxbf_i2c_smbus_data_word_func(struct mlxbf_i2c_smbus_request *request,
0848                    u8 *command, u8 *data, bool read, bool pec_check)
0849 {
0850     request->operation_cnt = MLXBF_I2C_SMBUS_OP_CNT_2;
0851 
0852     request->operation[0].length = 1;
0853     request->operation[0].flags =
0854             MLXBF_I2C_F_SMBUS_OPERATION | MLXBF_I2C_F_WRITE;
0855     request->operation[0].flags |= pec_check ? MLXBF_I2C_F_SMBUS_PEC : 0;
0856     request->operation[0].buffer = command;
0857 
0858     request->operation[1].length = 2;
0859     request->operation[1].length += pec_check;
0860     request->operation[1].flags = read ?
0861                 MLXBF_I2C_F_READ : MLXBF_I2C_F_WRITE;
0862     request->operation[1].buffer = data;
0863 }
0864 
0865 static void
0866 mlxbf_i2c_smbus_i2c_block_func(struct mlxbf_i2c_smbus_request *request,
0867                    u8 *command, u8 *data, u8 *data_len, bool read,
0868                    bool pec_check)
0869 {
0870     request->operation_cnt = MLXBF_I2C_SMBUS_OP_CNT_2;
0871 
0872     request->operation[0].length = 1;
0873     request->operation[0].flags =
0874             MLXBF_I2C_F_SMBUS_OPERATION | MLXBF_I2C_F_WRITE;
0875     request->operation[0].flags |= pec_check ? MLXBF_I2C_F_SMBUS_PEC : 0;
0876     request->operation[0].buffer = command;
0877 
0878     /*
0879      * As specified in the standard, the max number of bytes to read/write
0880      * per block operation is 32 bytes. In Golan code, the controller can
0881      * read up to 128 bytes and write up to 127 bytes.
0882      */
0883     request->operation[1].length =
0884         (*data_len + pec_check > I2C_SMBUS_BLOCK_MAX) ?
0885         I2C_SMBUS_BLOCK_MAX : *data_len + pec_check;
0886     request->operation[1].flags = read ?
0887                 MLXBF_I2C_F_READ : MLXBF_I2C_F_WRITE;
0888     /*
0889      * Skip the first data byte, which corresponds to the number of bytes
0890      * to read/write.
0891      */
0892     request->operation[1].buffer = data + 1;
0893 
0894     *data_len = request->operation[1].length;
0895 
0896     /* Set the number of byte to read. This will be used by userspace. */
0897     if (read)
0898         data[0] = *data_len;
0899 }
0900 
0901 static void mlxbf_i2c_smbus_block_func(struct mlxbf_i2c_smbus_request *request,
0902                        u8 *command, u8 *data, u8 *data_len,
0903                        bool read, bool pec_check)
0904 {
0905     request->operation_cnt = MLXBF_I2C_SMBUS_OP_CNT_2;
0906 
0907     request->operation[0].length = 1;
0908     request->operation[0].flags =
0909             MLXBF_I2C_F_SMBUS_OPERATION | MLXBF_I2C_F_WRITE;
0910     request->operation[0].flags |= MLXBF_I2C_F_SMBUS_BLOCK;
0911     request->operation[0].flags |= pec_check ? MLXBF_I2C_F_SMBUS_PEC : 0;
0912     request->operation[0].buffer = command;
0913 
0914     request->operation[1].length =
0915         (*data_len + pec_check > I2C_SMBUS_BLOCK_MAX) ?
0916         I2C_SMBUS_BLOCK_MAX : *data_len + pec_check;
0917     request->operation[1].flags = read ?
0918                 MLXBF_I2C_F_READ : MLXBF_I2C_F_WRITE;
0919     request->operation[1].buffer = data + 1;
0920 
0921     *data_len = request->operation[1].length;
0922 
0923     /* Set the number of bytes to read. This will be used by userspace. */
0924     if (read)
0925         data[0] = *data_len;
0926 }
0927 
0928 static void
0929 mlxbf_i2c_smbus_process_call_func(struct mlxbf_i2c_smbus_request *request,
0930                   u8 *command, u8 *data, bool pec_check)
0931 {
0932     request->operation_cnt = MLXBF_I2C_SMBUS_OP_CNT_3;
0933 
0934     request->operation[0].length = 1;
0935     request->operation[0].flags =
0936             MLXBF_I2C_F_SMBUS_OPERATION | MLXBF_I2C_F_WRITE;
0937     request->operation[0].flags |= MLXBF_I2C_F_SMBUS_BLOCK;
0938     request->operation[0].flags |= pec_check ? MLXBF_I2C_F_SMBUS_PEC : 0;
0939     request->operation[0].buffer = command;
0940 
0941     request->operation[1].length = 2;
0942     request->operation[1].flags = MLXBF_I2C_F_WRITE;
0943     request->operation[1].buffer = data;
0944 
0945     request->operation[2].length = 3;
0946     request->operation[2].flags = MLXBF_I2C_F_READ;
0947     request->operation[2].buffer = data;
0948 }
0949 
0950 static void
0951 mlxbf_i2c_smbus_blk_process_call_func(struct mlxbf_i2c_smbus_request *request,
0952                       u8 *command, u8 *data, u8 *data_len,
0953                       bool pec_check)
0954 {
0955     u32 length;
0956 
0957     request->operation_cnt = MLXBF_I2C_SMBUS_OP_CNT_3;
0958 
0959     request->operation[0].length = 1;
0960     request->operation[0].flags =
0961             MLXBF_I2C_F_SMBUS_OPERATION | MLXBF_I2C_F_WRITE;
0962     request->operation[0].flags |= MLXBF_I2C_F_SMBUS_BLOCK;
0963     request->operation[0].flags |= (pec_check) ? MLXBF_I2C_F_SMBUS_PEC : 0;
0964     request->operation[0].buffer = command;
0965 
0966     length = (*data_len + pec_check > I2C_SMBUS_BLOCK_MAX) ?
0967         I2C_SMBUS_BLOCK_MAX : *data_len + pec_check;
0968 
0969     request->operation[1].length = length - pec_check;
0970     request->operation[1].flags = MLXBF_I2C_F_WRITE;
0971     request->operation[1].buffer = data;
0972 
0973     request->operation[2].length = length;
0974     request->operation[2].flags = MLXBF_I2C_F_READ;
0975     request->operation[2].buffer = data;
0976 
0977     *data_len = length; /* including PEC byte. */
0978 }
0979 
0980 /* Initialization functions. */
0981 
0982 static bool mlxbf_i2c_has_chip_type(struct mlxbf_i2c_priv *priv, u8 type)
0983 {
0984     return priv->chip->type == type;
0985 }
0986 
0987 static struct mlxbf_i2c_resource *
0988 mlxbf_i2c_get_shared_resource(struct mlxbf_i2c_priv *priv, u8 type)
0989 {
0990     const struct mlxbf_i2c_chip_info *chip = priv->chip;
0991     struct mlxbf_i2c_resource *res;
0992     u8 res_idx = 0;
0993 
0994     for (res_idx = 0; res_idx < MLXBF_I2C_SHARED_RES_MAX; res_idx++) {
0995         res = chip->shared_res[res_idx];
0996         if (res && res->type == type)
0997             return res;
0998     }
0999 
1000     return NULL;
1001 }
1002 
1003 static int mlxbf_i2c_init_resource(struct platform_device *pdev,
1004                    struct mlxbf_i2c_resource **res,
1005                    u8 type)
1006 {
1007     struct mlxbf_i2c_resource *tmp_res;
1008     struct device *dev = &pdev->dev;
1009 
1010     if (!res || *res || type >= MLXBF_I2C_END_RES)
1011         return -EINVAL;
1012 
1013     tmp_res = devm_kzalloc(dev, sizeof(struct mlxbf_i2c_resource),
1014                    GFP_KERNEL);
1015     if (!tmp_res)
1016         return -ENOMEM;
1017 
1018     tmp_res->params = platform_get_resource(pdev, IORESOURCE_MEM, type);
1019     if (!tmp_res->params) {
1020         devm_kfree(dev, tmp_res);
1021         return -EIO;
1022     }
1023 
1024     tmp_res->io = devm_ioremap_resource(dev, tmp_res->params);
1025     if (IS_ERR(tmp_res->io)) {
1026         devm_kfree(dev, tmp_res);
1027         return PTR_ERR(tmp_res->io);
1028     }
1029 
1030     tmp_res->type = type;
1031 
1032     *res = tmp_res;
1033 
1034     return 0;
1035 }
1036 
1037 static u32 mlxbf_i2c_get_ticks(struct mlxbf_i2c_priv *priv, u64 nanoseconds,
1038                    bool minimum)
1039 {
1040     u64 frequency;
1041     u32 ticks;
1042 
1043     /*
1044      * Compute ticks as follow:
1045      *
1046      *           Ticks
1047      * Time = --------- x 10^9    =>    Ticks = Time x Frequency x 10^-9
1048      *         Frequency
1049      */
1050     frequency = priv->frequency;
1051     ticks = (nanoseconds * frequency) / MLXBF_I2C_FREQUENCY_1GHZ;
1052     /*
1053      * The number of ticks is rounded down and if minimum is equal to 1
1054      * then add one tick.
1055      */
1056     if (minimum)
1057         ticks++;
1058 
1059     return ticks;
1060 }
1061 
1062 static u32 mlxbf_i2c_set_timer(struct mlxbf_i2c_priv *priv, u64 nsec, bool opt,
1063                    u32 mask, u8 shift)
1064 {
1065     u32 val = (mlxbf_i2c_get_ticks(priv, nsec, opt) & mask) << shift;
1066 
1067     return val;
1068 }
1069 
1070 static void mlxbf_i2c_set_timings(struct mlxbf_i2c_priv *priv,
1071                   const struct mlxbf_i2c_timings *timings)
1072 {
1073     u32 timer;
1074 
1075     timer = mlxbf_i2c_set_timer(priv, timings->scl_high,
1076                     false, MLXBF_I2C_MASK_16,
1077                     MLXBF_I2C_SHIFT_0);
1078     timer |= mlxbf_i2c_set_timer(priv, timings->scl_low,
1079                      false, MLXBF_I2C_MASK_16,
1080                      MLXBF_I2C_SHIFT_16);
1081     writel(timer, priv->smbus->io +
1082         MLXBF_I2C_SMBUS_TIMER_SCL_LOW_SCL_HIGH);
1083 
1084     timer = mlxbf_i2c_set_timer(priv, timings->sda_rise, false,
1085                     MLXBF_I2C_MASK_8, MLXBF_I2C_SHIFT_0);
1086     timer |= mlxbf_i2c_set_timer(priv, timings->sda_fall, false,
1087                      MLXBF_I2C_MASK_8, MLXBF_I2C_SHIFT_8);
1088     timer |= mlxbf_i2c_set_timer(priv, timings->scl_rise, false,
1089                      MLXBF_I2C_MASK_8, MLXBF_I2C_SHIFT_16);
1090     timer |= mlxbf_i2c_set_timer(priv, timings->scl_fall, false,
1091                      MLXBF_I2C_MASK_8, MLXBF_I2C_SHIFT_24);
1092     writel(timer, priv->smbus->io +
1093         MLXBF_I2C_SMBUS_TIMER_FALL_RISE_SPIKE);
1094 
1095     timer = mlxbf_i2c_set_timer(priv, timings->hold_start, true,
1096                     MLXBF_I2C_MASK_16, MLXBF_I2C_SHIFT_0);
1097     timer |= mlxbf_i2c_set_timer(priv, timings->hold_data, true,
1098                      MLXBF_I2C_MASK_16, MLXBF_I2C_SHIFT_16);
1099     writel(timer, priv->smbus->io + MLXBF_I2C_SMBUS_TIMER_THOLD);
1100 
1101     timer = mlxbf_i2c_set_timer(priv, timings->setup_start, true,
1102                     MLXBF_I2C_MASK_16, MLXBF_I2C_SHIFT_0);
1103     timer |= mlxbf_i2c_set_timer(priv, timings->setup_stop, true,
1104                      MLXBF_I2C_MASK_16, MLXBF_I2C_SHIFT_16);
1105     writel(timer, priv->smbus->io +
1106         MLXBF_I2C_SMBUS_TIMER_TSETUP_START_STOP);
1107 
1108     timer = mlxbf_i2c_set_timer(priv, timings->setup_data, true,
1109                     MLXBF_I2C_MASK_16, MLXBF_I2C_SHIFT_0);
1110     writel(timer, priv->smbus->io + MLXBF_I2C_SMBUS_TIMER_TSETUP_DATA);
1111 
1112     timer = mlxbf_i2c_set_timer(priv, timings->buf, false,
1113                     MLXBF_I2C_MASK_16, MLXBF_I2C_SHIFT_0);
1114     timer |= mlxbf_i2c_set_timer(priv, timings->thigh_max, false,
1115                      MLXBF_I2C_MASK_16, MLXBF_I2C_SHIFT_16);
1116     writel(timer, priv->smbus->io + MLXBF_I2C_SMBUS_THIGH_MAX_TBUF);
1117 
1118     timer = timings->timeout;
1119     writel(timer, priv->smbus->io + MLXBF_I2C_SMBUS_SCL_LOW_TIMEOUT);
1120 }
1121 
1122 enum mlxbf_i2c_timings_config {
1123     MLXBF_I2C_TIMING_CONFIG_100KHZ,
1124     MLXBF_I2C_TIMING_CONFIG_400KHZ,
1125     MLXBF_I2C_TIMING_CONFIG_1000KHZ,
1126 };
1127 
1128 /*
1129  * Note that the mlxbf_i2c_timings->timeout value is not related to the
1130  * bus frequency, it is impacted by the time it takes the driver to
1131  * complete data transmission before transaction abort.
1132  */
1133 static const struct mlxbf_i2c_timings mlxbf_i2c_timings[] = {
1134     [MLXBF_I2C_TIMING_CONFIG_100KHZ] = {
1135         .scl_high = 4810,
1136         .scl_low = 5000,
1137         .hold_start = 4000,
1138         .setup_start = 4800,
1139         .setup_stop = 4000,
1140         .setup_data = 250,
1141         .sda_rise = 50,
1142         .sda_fall = 50,
1143         .scl_rise = 50,
1144         .scl_fall = 50,
1145         .hold_data = 300,
1146         .buf = 20000,
1147         .thigh_max = 5000,
1148         .timeout = 106500
1149     },
1150     [MLXBF_I2C_TIMING_CONFIG_400KHZ] = {
1151         .scl_high = 1011,
1152         .scl_low = 1300,
1153         .hold_start = 600,
1154         .setup_start = 700,
1155         .setup_stop = 600,
1156         .setup_data = 100,
1157         .sda_rise = 50,
1158         .sda_fall = 50,
1159         .scl_rise = 50,
1160         .scl_fall = 50,
1161         .hold_data = 300,
1162         .buf = 20000,
1163         .thigh_max = 5000,
1164         .timeout = 106500
1165     },
1166     [MLXBF_I2C_TIMING_CONFIG_1000KHZ] = {
1167         .scl_high = 600,
1168         .scl_low = 1300,
1169         .hold_start = 600,
1170         .setup_start = 600,
1171         .setup_stop = 600,
1172         .setup_data = 100,
1173         .sda_rise = 50,
1174         .sda_fall = 50,
1175         .scl_rise = 50,
1176         .scl_fall = 50,
1177         .hold_data = 300,
1178         .buf = 20000,
1179         .thigh_max = 5000,
1180         .timeout = 106500
1181     }
1182 };
1183 
1184 static int mlxbf_i2c_init_timings(struct platform_device *pdev,
1185                   struct mlxbf_i2c_priv *priv)
1186 {
1187     enum mlxbf_i2c_timings_config config_idx;
1188     struct device *dev = &pdev->dev;
1189     u32 config_khz;
1190 
1191     int ret;
1192 
1193     ret = device_property_read_u32(dev, "clock-frequency", &config_khz);
1194     if (ret < 0)
1195         config_khz = I2C_MAX_STANDARD_MODE_FREQ;
1196 
1197     switch (config_khz) {
1198     default:
1199         /* Default settings is 100 KHz. */
1200         pr_warn("Illegal value %d: defaulting to 100 KHz\n",
1201             config_khz);
1202         fallthrough;
1203     case I2C_MAX_STANDARD_MODE_FREQ:
1204         config_idx = MLXBF_I2C_TIMING_CONFIG_100KHZ;
1205         break;
1206 
1207     case I2C_MAX_FAST_MODE_FREQ:
1208         config_idx = MLXBF_I2C_TIMING_CONFIG_400KHZ;
1209         break;
1210 
1211     case I2C_MAX_FAST_MODE_PLUS_FREQ:
1212         config_idx = MLXBF_I2C_TIMING_CONFIG_1000KHZ;
1213         break;
1214     }
1215 
1216     mlxbf_i2c_set_timings(priv, &mlxbf_i2c_timings[config_idx]);
1217 
1218     return 0;
1219 }
1220 
1221 static int mlxbf_i2c_get_gpio(struct platform_device *pdev,
1222                   struct mlxbf_i2c_priv *priv)
1223 {
1224     struct mlxbf_i2c_resource *gpio_res;
1225     struct device *dev = &pdev->dev;
1226     struct resource *params;
1227     resource_size_t size;
1228 
1229     gpio_res = mlxbf_i2c_get_shared_resource(priv, MLXBF_I2C_GPIO_RES);
1230     if (!gpio_res)
1231         return -EPERM;
1232 
1233     /*
1234      * The GPIO region in TYU space is shared among I2C busses.
1235      * This function MUST be serialized to avoid racing when
1236      * claiming the memory region and/or setting up the GPIO.
1237      */
1238     lockdep_assert_held(gpio_res->lock);
1239 
1240     /* Check whether the memory map exist. */
1241     if (gpio_res->io)
1242         return 0;
1243 
1244     params = gpio_res->params;
1245     size = resource_size(params);
1246 
1247     if (!devm_request_mem_region(dev, params->start, size, params->name))
1248         return -EFAULT;
1249 
1250     gpio_res->io = devm_ioremap(dev, params->start, size);
1251     if (!gpio_res->io) {
1252         devm_release_mem_region(dev, params->start, size);
1253         return -ENOMEM;
1254     }
1255 
1256     return 0;
1257 }
1258 
1259 static int mlxbf_i2c_release_gpio(struct platform_device *pdev,
1260                   struct mlxbf_i2c_priv *priv)
1261 {
1262     struct mlxbf_i2c_resource *gpio_res;
1263     struct device *dev = &pdev->dev;
1264     struct resource *params;
1265 
1266     gpio_res = mlxbf_i2c_get_shared_resource(priv, MLXBF_I2C_GPIO_RES);
1267     if (!gpio_res)
1268         return 0;
1269 
1270     mutex_lock(gpio_res->lock);
1271 
1272     if (gpio_res->io) {
1273         /* Release the GPIO resource. */
1274         params = gpio_res->params;
1275         devm_iounmap(dev, gpio_res->io);
1276         devm_release_mem_region(dev, params->start,
1277                     resource_size(params));
1278     }
1279 
1280     mutex_unlock(gpio_res->lock);
1281 
1282     return 0;
1283 }
1284 
1285 static int mlxbf_i2c_get_corepll(struct platform_device *pdev,
1286                  struct mlxbf_i2c_priv *priv)
1287 {
1288     struct mlxbf_i2c_resource *corepll_res;
1289     struct device *dev = &pdev->dev;
1290     struct resource *params;
1291     resource_size_t size;
1292 
1293     corepll_res = mlxbf_i2c_get_shared_resource(priv,
1294                             MLXBF_I2C_COREPLL_RES);
1295     if (!corepll_res)
1296         return -EPERM;
1297 
1298     /*
1299      * The COREPLL region in TYU space is shared among I2C busses.
1300      * This function MUST be serialized to avoid racing when
1301      * claiming the memory region.
1302      */
1303     lockdep_assert_held(corepll_res->lock);
1304 
1305     /* Check whether the memory map exist. */
1306     if (corepll_res->io)
1307         return 0;
1308 
1309     params = corepll_res->params;
1310     size = resource_size(params);
1311 
1312     if (!devm_request_mem_region(dev, params->start, size, params->name))
1313         return -EFAULT;
1314 
1315     corepll_res->io = devm_ioremap(dev, params->start, size);
1316     if (!corepll_res->io) {
1317         devm_release_mem_region(dev, params->start, size);
1318         return -ENOMEM;
1319     }
1320 
1321     return 0;
1322 }
1323 
1324 static int mlxbf_i2c_release_corepll(struct platform_device *pdev,
1325                      struct mlxbf_i2c_priv *priv)
1326 {
1327     struct mlxbf_i2c_resource *corepll_res;
1328     struct device *dev = &pdev->dev;
1329     struct resource *params;
1330 
1331     corepll_res = mlxbf_i2c_get_shared_resource(priv,
1332                             MLXBF_I2C_COREPLL_RES);
1333 
1334     mutex_lock(corepll_res->lock);
1335 
1336     if (corepll_res->io) {
1337         /* Release the CorePLL resource. */
1338         params = corepll_res->params;
1339         devm_iounmap(dev, corepll_res->io);
1340         devm_release_mem_region(dev, params->start,
1341                     resource_size(params));
1342     }
1343 
1344     mutex_unlock(corepll_res->lock);
1345 
1346     return 0;
1347 }
1348 
1349 static int mlxbf_i2c_init_master(struct platform_device *pdev,
1350                  struct mlxbf_i2c_priv *priv)
1351 {
1352     struct mlxbf_i2c_resource *gpio_res;
1353     struct device *dev = &pdev->dev;
1354     u32 config_reg;
1355     int ret;
1356 
1357     /* This configuration is only needed for BlueField 1. */
1358     if (!mlxbf_i2c_has_chip_type(priv, MLXBF_I2C_CHIP_TYPE_1))
1359         return 0;
1360 
1361     gpio_res = mlxbf_i2c_get_shared_resource(priv, MLXBF_I2C_GPIO_RES);
1362     if (!gpio_res)
1363         return -EPERM;
1364 
1365     /*
1366      * The GPIO region in TYU space is shared among I2C busses.
1367      * This function MUST be serialized to avoid racing when
1368      * claiming the memory region and/or setting up the GPIO.
1369      */
1370 
1371     mutex_lock(gpio_res->lock);
1372 
1373     ret = mlxbf_i2c_get_gpio(pdev, priv);
1374     if (ret < 0) {
1375         dev_err(dev, "Failed to get gpio resource");
1376         mutex_unlock(gpio_res->lock);
1377         return ret;
1378     }
1379 
1380     /*
1381      * TYU - Configuration for GPIO pins. Those pins must be asserted in
1382      * MLXBF_I2C_GPIO_0_FUNC_EN_0, i.e. GPIO 0 is controlled by HW, and must
1383      * be reset in MLXBF_I2C_GPIO_0_FORCE_OE_EN, i.e. GPIO_OE will be driven
1384      * instead of HW_OE.
1385      * For now, we do not reset the GPIO state when the driver is removed.
1386      * First, it is not necessary to disable the bus since we are using
1387      * the same busses. Then, some busses might be shared among Linux and
1388      * platform firmware; disabling the bus might compromise the system
1389      * functionality.
1390      */
1391     config_reg = readl(gpio_res->io + MLXBF_I2C_GPIO_0_FUNC_EN_0);
1392     config_reg = MLXBF_I2C_GPIO_SMBUS_GW_ASSERT_PINS(priv->bus,
1393                              config_reg);
1394     writel(config_reg, gpio_res->io + MLXBF_I2C_GPIO_0_FUNC_EN_0);
1395 
1396     config_reg = readl(gpio_res->io + MLXBF_I2C_GPIO_0_FORCE_OE_EN);
1397     config_reg = MLXBF_I2C_GPIO_SMBUS_GW_RESET_PINS(priv->bus,
1398                             config_reg);
1399     writel(config_reg, gpio_res->io + MLXBF_I2C_GPIO_0_FORCE_OE_EN);
1400 
1401     mutex_unlock(gpio_res->lock);
1402 
1403     return 0;
1404 }
1405 
1406 static u64 mlxbf_i2c_calculate_freq_from_tyu(struct mlxbf_i2c_resource *corepll_res)
1407 {
1408     u64 core_frequency;
1409     u8 core_od, core_r;
1410     u32 corepll_val;
1411     u16 core_f;
1412 
1413     corepll_val = readl(corepll_res->io + MLXBF_I2C_CORE_PLL_REG1);
1414 
1415     /* Get Core PLL configuration bits. */
1416     core_f = FIELD_GET(MLXBF_I2C_COREPLL_CORE_F_TYU_MASK, corepll_val);
1417     core_od = FIELD_GET(MLXBF_I2C_COREPLL_CORE_OD_TYU_MASK, corepll_val);
1418     core_r = FIELD_GET(MLXBF_I2C_COREPLL_CORE_R_TYU_MASK, corepll_val);
1419 
1420     /*
1421      * Compute PLL output frequency as follow:
1422      *
1423      *                                       CORE_F + 1
1424      * PLL_OUT_FREQ = PLL_IN_FREQ * ----------------------------
1425      *                              (CORE_R + 1) * (CORE_OD + 1)
1426      *
1427      * Where PLL_OUT_FREQ and PLL_IN_FREQ refer to CoreFrequency
1428      * and PadFrequency, respectively.
1429      */
1430     core_frequency = MLXBF_I2C_PLL_IN_FREQ * (++core_f);
1431     core_frequency /= (++core_r) * (++core_od);
1432 
1433     return core_frequency;
1434 }
1435 
1436 static u64 mlxbf_i2c_calculate_freq_from_yu(struct mlxbf_i2c_resource *corepll_res)
1437 {
1438     u32 corepll_reg1_val, corepll_reg2_val;
1439     u64 corepll_frequency;
1440     u8 core_od, core_r;
1441     u32 core_f;
1442 
1443     corepll_reg1_val = readl(corepll_res->io + MLXBF_I2C_CORE_PLL_REG1);
1444     corepll_reg2_val = readl(corepll_res->io + MLXBF_I2C_CORE_PLL_REG2);
1445 
1446     /* Get Core PLL configuration bits */
1447     core_f = FIELD_GET(MLXBF_I2C_COREPLL_CORE_F_YU_MASK, corepll_reg1_val);
1448     core_r = FIELD_GET(MLXBF_I2C_COREPLL_CORE_R_YU_MASK, corepll_reg1_val);
1449     core_od = FIELD_GET(MLXBF_I2C_COREPLL_CORE_OD_YU_MASK, corepll_reg2_val);
1450 
1451     /*
1452      * Compute PLL output frequency as follow:
1453      *
1454      *                                     CORE_F / 16384
1455      * PLL_OUT_FREQ = PLL_IN_FREQ * ----------------------------
1456      *                              (CORE_R + 1) * (CORE_OD + 1)
1457      *
1458      * Where PLL_OUT_FREQ and PLL_IN_FREQ refer to CoreFrequency
1459      * and PadFrequency, respectively.
1460      */
1461     corepll_frequency = (MLXBF_I2C_PLL_IN_FREQ * core_f) / MLNXBF_I2C_COREPLL_CONST;
1462     corepll_frequency /= (++core_r) * (++core_od);
1463 
1464     return corepll_frequency;
1465 }
1466 
1467 static int mlxbf_i2c_calculate_corepll_freq(struct platform_device *pdev,
1468                         struct mlxbf_i2c_priv *priv)
1469 {
1470     const struct mlxbf_i2c_chip_info *chip = priv->chip;
1471     struct mlxbf_i2c_resource *corepll_res;
1472     struct device *dev = &pdev->dev;
1473     u64 *freq = &priv->frequency;
1474     int ret;
1475 
1476     corepll_res = mlxbf_i2c_get_shared_resource(priv,
1477                             MLXBF_I2C_COREPLL_RES);
1478     if (!corepll_res)
1479         return -EPERM;
1480 
1481     /*
1482      * First, check whether the TYU core Clock frequency is set.
1483      * The TYU core frequency is the same for all I2C busses; when
1484      * the first device gets probed the frequency is determined and
1485      * stored into a globally visible variable. So, first of all,
1486      * check whether the frequency is already set. Here, we assume
1487      * that the frequency is expected to be greater than 0.
1488      */
1489     mutex_lock(corepll_res->lock);
1490     if (!mlxbf_i2c_corepll_frequency) {
1491         if (!chip->calculate_freq) {
1492             mutex_unlock(corepll_res->lock);
1493             return -EPERM;
1494         }
1495 
1496         ret = mlxbf_i2c_get_corepll(pdev, priv);
1497         if (ret < 0) {
1498             dev_err(dev, "Failed to get corePLL resource");
1499             mutex_unlock(corepll_res->lock);
1500             return ret;
1501         }
1502 
1503         mlxbf_i2c_corepll_frequency = chip->calculate_freq(corepll_res);
1504     }
1505     mutex_unlock(corepll_res->lock);
1506 
1507     *freq = mlxbf_i2c_corepll_frequency;
1508 
1509     return 0;
1510 }
1511 
1512 static int mlxbf_slave_enable(struct mlxbf_i2c_priv *priv, u8 addr)
1513 {
1514     u32 slave_reg, slave_reg_tmp, slave_reg_avail, slave_addr_mask;
1515     u8 reg, reg_cnt, byte, addr_tmp, reg_avail, byte_avail;
1516     bool avail, disabled;
1517 
1518     disabled = false;
1519     avail = false;
1520 
1521     if (!priv)
1522         return -EPERM;
1523 
1524     reg_cnt = MLXBF_I2C_SMBUS_SLAVE_ADDR_CNT >> 2;
1525     slave_addr_mask = MLXBF_I2C_SMBUS_SLAVE_ADDR_MASK;
1526 
1527     /*
1528      * Read the slave registers. There are 4 * 32-bit slave registers.
1529      * Each slave register can hold up to 4 * 8-bit slave configuration
1530      * (7-bit address, 1 status bit (1 if enabled, 0 if not)).
1531      */
1532     for (reg = 0; reg < reg_cnt; reg++) {
1533         slave_reg = readl(priv->smbus->io +
1534                 MLXBF_I2C_SMBUS_SLAVE_ADDR_CFG + reg * 0x4);
1535         /*
1536          * Each register holds 4 slave addresses. So, we have to keep
1537          * the byte order consistent with the value read in order to
1538          * update the register correctly, if needed.
1539          */
1540         slave_reg_tmp = slave_reg;
1541         for (byte = 0; byte < 4; byte++) {
1542             addr_tmp = slave_reg_tmp & GENMASK(7, 0);
1543 
1544             /*
1545              * Mark the first available slave address slot, i.e. its
1546              * enabled bit should be unset. This slot might be used
1547              * later on to register our slave.
1548              */
1549             if (!avail && !MLXBF_I2C_SLAVE_ADDR_ENABLED(addr_tmp)) {
1550                 avail = true;
1551                 reg_avail = reg;
1552                 byte_avail = byte;
1553                 slave_reg_avail = slave_reg;
1554             }
1555 
1556             /*
1557              * Parse slave address bytes and check whether the
1558              * slave address already exists and it's enabled,
1559              * i.e. most significant bit is set.
1560              */
1561             if ((addr_tmp & slave_addr_mask) == addr) {
1562                 if (MLXBF_I2C_SLAVE_ADDR_ENABLED(addr_tmp))
1563                     return 0;
1564                 disabled = true;
1565                 break;
1566             }
1567 
1568             /* Parse next byte. */
1569             slave_reg_tmp >>= 8;
1570         }
1571 
1572         /* Exit the loop if the slave address is found. */
1573         if (disabled)
1574             break;
1575     }
1576 
1577     if (!avail && !disabled)
1578         return -EINVAL; /* No room for a new slave address. */
1579 
1580     if (avail && !disabled) {
1581         reg = reg_avail;
1582         byte = byte_avail;
1583         /* Set the slave address. */
1584         slave_reg_avail &= ~(slave_addr_mask << (byte * 8));
1585         slave_reg_avail |= addr << (byte * 8);
1586         slave_reg = slave_reg_avail;
1587     }
1588 
1589     /* Enable the slave address and update the register. */
1590     slave_reg |= (1 << MLXBF_I2C_SMBUS_SLAVE_ADDR_EN_BIT) << (byte * 8);
1591     writel(slave_reg, priv->smbus->io + MLXBF_I2C_SMBUS_SLAVE_ADDR_CFG +
1592         reg * 0x4);
1593 
1594     return 0;
1595 }
1596 
1597 static int mlxbf_slave_disable(struct mlxbf_i2c_priv *priv)
1598 {
1599     u32 slave_reg, slave_reg_tmp, slave_addr_mask;
1600     u8 addr, addr_tmp, reg, reg_cnt, slave_byte;
1601     struct i2c_client *client = priv->slave;
1602     bool exist;
1603 
1604     exist = false;
1605 
1606     addr = client->addr;
1607     reg_cnt = MLXBF_I2C_SMBUS_SLAVE_ADDR_CNT >> 2;
1608     slave_addr_mask = MLXBF_I2C_SMBUS_SLAVE_ADDR_MASK;
1609 
1610     /*
1611      * Read the slave registers. There are 4 * 32-bit slave registers.
1612      * Each slave register can hold up to 4 * 8-bit slave configuration
1613      * (7-bit address, 1 status bit (1 if enabled, 0 if not)).
1614      */
1615     for (reg = 0; reg < reg_cnt; reg++) {
1616         slave_reg = readl(priv->smbus->io +
1617                 MLXBF_I2C_SMBUS_SLAVE_ADDR_CFG + reg * 0x4);
1618 
1619         /* Check whether the address slots are empty. */
1620         if (slave_reg == 0)
1621             continue;
1622 
1623         /*
1624          * Each register holds 4 slave addresses. So, we have to keep
1625          * the byte order consistent with the value read in order to
1626          * update the register correctly, if needed.
1627          */
1628         slave_reg_tmp = slave_reg;
1629         slave_byte = 0;
1630         while (slave_reg_tmp != 0) {
1631             addr_tmp = slave_reg_tmp & slave_addr_mask;
1632             /*
1633              * Parse slave address bytes and check whether the
1634              * slave address already exists.
1635              */
1636             if (addr_tmp == addr) {
1637                 exist = true;
1638                 break;
1639             }
1640 
1641             /* Parse next byte. */
1642             slave_reg_tmp >>= 8;
1643             slave_byte += 1;
1644         }
1645 
1646         /* Exit the loop if the slave address is found. */
1647         if (exist)
1648             break;
1649     }
1650 
1651     if (!exist)
1652         return 0; /* Slave is not registered, nothing to do. */
1653 
1654     /* Cleanup the slave address slot. */
1655     slave_reg &= ~(GENMASK(7, 0) << (slave_byte * 8));
1656     writel(slave_reg, priv->smbus->io + MLXBF_I2C_SMBUS_SLAVE_ADDR_CFG +
1657         reg * 0x4);
1658 
1659     return 0;
1660 }
1661 
1662 static int mlxbf_i2c_init_coalesce(struct platform_device *pdev,
1663                    struct mlxbf_i2c_priv *priv)
1664 {
1665     struct mlxbf_i2c_resource *coalesce_res;
1666     struct resource *params;
1667     resource_size_t size;
1668     int ret = 0;
1669 
1670     /*
1671      * Unlike BlueField-1 platform, the coalesce registers is a dedicated
1672      * resource in the next generations of BlueField.
1673      */
1674     if (mlxbf_i2c_has_chip_type(priv, MLXBF_I2C_CHIP_TYPE_1)) {
1675         coalesce_res = mlxbf_i2c_get_shared_resource(priv,
1676                         MLXBF_I2C_COALESCE_RES);
1677         if (!coalesce_res)
1678             return -EPERM;
1679 
1680         /*
1681          * The Cause Coalesce group in TYU space is shared among
1682          * I2C busses. This function MUST be serialized to avoid
1683          * racing when claiming the memory region.
1684          */
1685         lockdep_assert_held(mlxbf_i2c_gpio_res->lock);
1686 
1687         /* Check whether the memory map exist. */
1688         if (coalesce_res->io) {
1689             priv->coalesce = coalesce_res;
1690             return 0;
1691         }
1692 
1693         params = coalesce_res->params;
1694         size = resource_size(params);
1695 
1696         if (!request_mem_region(params->start, size, params->name))
1697             return -EFAULT;
1698 
1699         coalesce_res->io = ioremap(params->start, size);
1700         if (!coalesce_res->io) {
1701             release_mem_region(params->start, size);
1702             return -ENOMEM;
1703         }
1704 
1705         priv->coalesce = coalesce_res;
1706 
1707     } else {
1708         ret = mlxbf_i2c_init_resource(pdev, &priv->coalesce,
1709                           MLXBF_I2C_COALESCE_RES);
1710     }
1711 
1712     return ret;
1713 }
1714 
1715 static int mlxbf_i2c_release_coalesce(struct platform_device *pdev,
1716                       struct mlxbf_i2c_priv *priv)
1717 {
1718     struct mlxbf_i2c_resource *coalesce_res;
1719     struct device *dev = &pdev->dev;
1720     struct resource *params;
1721     resource_size_t size;
1722 
1723     coalesce_res = priv->coalesce;
1724 
1725     if (coalesce_res->io) {
1726         params = coalesce_res->params;
1727         size = resource_size(params);
1728         if (mlxbf_i2c_has_chip_type(priv, MLXBF_I2C_CHIP_TYPE_1)) {
1729             mutex_lock(coalesce_res->lock);
1730             iounmap(coalesce_res->io);
1731             release_mem_region(params->start, size);
1732             mutex_unlock(coalesce_res->lock);
1733         } else {
1734             devm_release_mem_region(dev, params->start, size);
1735         }
1736     }
1737 
1738     return 0;
1739 }
1740 
1741 static int mlxbf_i2c_init_slave(struct platform_device *pdev,
1742                 struct mlxbf_i2c_priv *priv)
1743 {
1744     struct device *dev = &pdev->dev;
1745     u32 int_reg;
1746     int ret;
1747 
1748     /* Reset FSM. */
1749     writel(0, priv->smbus->io + MLXBF_I2C_SMBUS_SLAVE_FSM);
1750 
1751     /*
1752      * Enable slave cause interrupt bits. Drive
1753      * MLXBF_I2C_CAUSE_READ_WAIT_FW_RESPONSE and
1754      * MLXBF_I2C_CAUSE_WRITE_SUCCESS, these are enabled when an external
1755      * masters issue a Read and Write, respectively. But, clear all
1756      * interrupts first.
1757      */
1758     writel(~0, priv->slv_cause->io + MLXBF_I2C_CAUSE_OR_CLEAR);
1759     int_reg = MLXBF_I2C_CAUSE_READ_WAIT_FW_RESPONSE;
1760     int_reg |= MLXBF_I2C_CAUSE_WRITE_SUCCESS;
1761     writel(int_reg, priv->slv_cause->io + MLXBF_I2C_CAUSE_OR_EVTEN0);
1762 
1763     /* Finally, set the 'ready' bit to start handling transactions. */
1764     writel(0x1, priv->smbus->io + MLXBF_I2C_SMBUS_SLAVE_READY);
1765 
1766     /* Initialize the cause coalesce resource. */
1767     ret = mlxbf_i2c_init_coalesce(pdev, priv);
1768     if (ret < 0) {
1769         dev_err(dev, "failed to initialize cause coalesce\n");
1770         return ret;
1771     }
1772 
1773     return 0;
1774 }
1775 
1776 static bool mlxbf_i2c_has_coalesce(struct mlxbf_i2c_priv *priv, bool *read,
1777                    bool *write)
1778 {
1779     const struct mlxbf_i2c_chip_info *chip = priv->chip;
1780     u32 coalesce0_reg, cause_reg;
1781     u8 slave_shift, is_set;
1782 
1783     *write = false;
1784     *read = false;
1785 
1786     slave_shift = chip->type != MLXBF_I2C_CHIP_TYPE_1 ?
1787                 MLXBF_I2C_CAUSE_YU_SLAVE_BIT :
1788                 priv->bus + MLXBF_I2C_CAUSE_TYU_SLAVE_BIT;
1789 
1790     coalesce0_reg = readl(priv->coalesce->io + MLXBF_I2C_CAUSE_COALESCE_0);
1791     is_set = coalesce0_reg & (1 << slave_shift);
1792 
1793     if (!is_set)
1794         return false;
1795 
1796     /* Check the source of the interrupt, i.e. whether a Read or Write. */
1797     cause_reg = readl(priv->slv_cause->io + MLXBF_I2C_CAUSE_ARBITER);
1798     if (cause_reg & MLXBF_I2C_CAUSE_READ_WAIT_FW_RESPONSE)
1799         *read = true;
1800     else if (cause_reg & MLXBF_I2C_CAUSE_WRITE_SUCCESS)
1801         *write = true;
1802 
1803     /* Clear cause bits. */
1804     writel(~0x0, priv->slv_cause->io + MLXBF_I2C_CAUSE_OR_CLEAR);
1805 
1806     return true;
1807 }
1808 
1809 static bool mlxbf_smbus_slave_wait_for_idle(struct mlxbf_i2c_priv *priv,
1810                         u32 timeout)
1811 {
1812     u32 mask = MLXBF_I2C_CAUSE_S_GW_BUSY_FALL;
1813     u32 addr = MLXBF_I2C_CAUSE_ARBITER;
1814 
1815     if (mlxbf_smbus_poll(priv->slv_cause->io, addr, mask, false, timeout))
1816         return true;
1817 
1818     return false;
1819 }
1820 
1821 /* Send byte to 'external' smbus master. */
1822 static int mlxbf_smbus_irq_send(struct mlxbf_i2c_priv *priv, u8 recv_bytes)
1823 {
1824     u8 data_desc[MLXBF_I2C_SLAVE_DATA_DESC_SIZE] = { 0 };
1825     u8 write_size, pec_en, addr, byte, value, byte_cnt, desc_size;
1826     struct i2c_client *slave = priv->slave;
1827     u32 control32, data32;
1828     int ret;
1829 
1830     if (!slave)
1831         return -EINVAL;
1832 
1833     addr = 0;
1834     byte = 0;
1835     desc_size = MLXBF_I2C_SLAVE_DATA_DESC_SIZE;
1836 
1837     /*
1838      * Read bytes received from the external master. These bytes should
1839      * be located in the first data descriptor register of the slave GW.
1840      * These bytes are the slave address byte and the internal register
1841      * address, if supplied.
1842      */
1843     if (recv_bytes > 0) {
1844         data32 = ioread32be(priv->smbus->io +
1845                     MLXBF_I2C_SLAVE_DATA_DESC_ADDR);
1846 
1847         /* Parse the received bytes. */
1848         switch (recv_bytes) {
1849         case 2:
1850             byte = (data32 >> 8) & GENMASK(7, 0);
1851             fallthrough;
1852         case 1:
1853             addr = (data32 & GENMASK(7, 0)) >> 1;
1854         }
1855 
1856         /* Check whether it's our slave address. */
1857         if (slave->addr != addr)
1858             return -EINVAL;
1859     }
1860 
1861     /*
1862      * I2C read transactions may start by a WRITE followed by a READ.
1863      * Indeed, most slave devices would expect the internal address
1864      * following the slave address byte. So, write that byte first,
1865      * and then, send the requested data bytes to the master.
1866      */
1867     if (recv_bytes > 1) {
1868         i2c_slave_event(slave, I2C_SLAVE_WRITE_REQUESTED, &value);
1869         value = byte;
1870         ret = i2c_slave_event(slave, I2C_SLAVE_WRITE_RECEIVED,
1871                       &value);
1872         i2c_slave_event(slave, I2C_SLAVE_STOP, &value);
1873 
1874         if (ret < 0)
1875             return ret;
1876     }
1877 
1878     /*
1879      * Now, send data to the master; currently, the driver supports
1880      * READ_BYTE, READ_WORD and BLOCK READ protocols. Note that the
1881      * hardware can send up to 128 bytes per transfer. That is the
1882      * size of its data registers.
1883      */
1884     i2c_slave_event(slave, I2C_SLAVE_READ_REQUESTED, &value);
1885 
1886     for (byte_cnt = 0; byte_cnt < desc_size; byte_cnt++) {
1887         data_desc[byte_cnt] = value;
1888         i2c_slave_event(slave, I2C_SLAVE_READ_PROCESSED, &value);
1889     }
1890 
1891     /* Send a stop condition to the backend. */
1892     i2c_slave_event(slave, I2C_SLAVE_STOP, &value);
1893 
1894     /* Handle the actual transfer. */
1895 
1896     /* Set the number of bytes to write to master. */
1897     write_size = (byte_cnt - 1) & 0x7f;
1898 
1899     /* Write data to Slave GW data descriptor. */
1900     mlxbf_i2c_smbus_write_data(priv, data_desc, byte_cnt,
1901                    MLXBF_I2C_SLAVE_DATA_DESC_ADDR);
1902 
1903     pec_en = 0; /* Disable PEC since it is not supported. */
1904 
1905     /* Prepare control word. */
1906     control32 = MLXBF_I2C_SLAVE_ENABLE;
1907     control32 |= rol32(write_size, MLXBF_I2C_SLAVE_WRITE_BYTES_SHIFT);
1908     control32 |= rol32(pec_en, MLXBF_I2C_SLAVE_SEND_PEC_SHIFT);
1909 
1910     writel(control32, priv->smbus->io + MLXBF_I2C_SMBUS_SLAVE_GW);
1911 
1912     /*
1913      * Wait until the transfer is completed; the driver will wait
1914      * until the GW is idle, a cause will rise on fall of GW busy.
1915      */
1916     mlxbf_smbus_slave_wait_for_idle(priv, MLXBF_I2C_SMBUS_TIMEOUT);
1917 
1918     /* Release the Slave GW. */
1919     writel(0x0, priv->smbus->io + MLXBF_I2C_SMBUS_SLAVE_RS_MASTER_BYTES);
1920     writel(0x0, priv->smbus->io + MLXBF_I2C_SMBUS_SLAVE_PEC);
1921     writel(0x1, priv->smbus->io + MLXBF_I2C_SMBUS_SLAVE_READY);
1922 
1923     return 0;
1924 }
1925 
1926 /* Receive bytes from 'external' smbus master. */
1927 static int mlxbf_smbus_irq_recv(struct mlxbf_i2c_priv *priv, u8 recv_bytes)
1928 {
1929     u8 data_desc[MLXBF_I2C_SLAVE_DATA_DESC_SIZE] = { 0 };
1930     struct i2c_client *slave = priv->slave;
1931     u8 value, byte, addr;
1932     int ret = 0;
1933 
1934     if (!slave)
1935         return -EINVAL;
1936 
1937     /* Read data from Slave GW data descriptor. */
1938     mlxbf_i2c_smbus_read_data(priv, data_desc, recv_bytes,
1939                   MLXBF_I2C_SLAVE_DATA_DESC_ADDR);
1940 
1941     /* Check whether its our slave address. */
1942     addr = data_desc[0] >> 1;
1943     if (slave->addr != addr)
1944         return -EINVAL;
1945 
1946     /*
1947      * Notify the slave backend; another I2C master wants to write data
1948      * to us. This event is sent once the slave address and the write bit
1949      * is detected.
1950      */
1951     i2c_slave_event(slave, I2C_SLAVE_WRITE_REQUESTED, &value);
1952 
1953     /* Send the received data to the slave backend. */
1954     for (byte = 1; byte < recv_bytes; byte++) {
1955         value = data_desc[byte];
1956         ret = i2c_slave_event(slave, I2C_SLAVE_WRITE_RECEIVED,
1957                       &value);
1958         if (ret < 0)
1959             break;
1960     }
1961 
1962     /* Send a stop condition to the backend. */
1963     i2c_slave_event(slave, I2C_SLAVE_STOP, &value);
1964 
1965     /* Release the Slave GW. */
1966     writel(0x0, priv->smbus->io + MLXBF_I2C_SMBUS_SLAVE_RS_MASTER_BYTES);
1967     writel(0x0, priv->smbus->io + MLXBF_I2C_SMBUS_SLAVE_PEC);
1968     writel(0x1, priv->smbus->io + MLXBF_I2C_SMBUS_SLAVE_READY);
1969 
1970     return ret;
1971 }
1972 
1973 static irqreturn_t mlxbf_smbus_irq(int irq, void *ptr)
1974 {
1975     struct mlxbf_i2c_priv *priv = ptr;
1976     bool read, write, irq_is_set;
1977     u32 rw_bytes_reg;
1978     u8 recv_bytes;
1979 
1980     /*
1981      * Read TYU interrupt register and determine the source of the
1982      * interrupt. Based on the source of the interrupt one of the
1983      * following actions are performed:
1984      *  - Receive data and send response to master.
1985      *  - Send data and release slave GW.
1986      *
1987      * Handle read/write transaction only. CRmaster and Iarp requests
1988      * are ignored for now.
1989      */
1990     irq_is_set = mlxbf_i2c_has_coalesce(priv, &read, &write);
1991     if (!irq_is_set || (!read && !write)) {
1992         /* Nothing to do here, interrupt was not from this device. */
1993         return IRQ_NONE;
1994     }
1995 
1996     /*
1997      * The MLXBF_I2C_SMBUS_SLAVE_RS_MASTER_BYTES includes the number of
1998      * bytes from/to master. These are defined by 8-bits each. If the lower
1999      * 8 bits are set, then the master expect to read N bytes from the
2000      * slave, if the higher 8 bits are sent then the slave expect N bytes
2001      * from the master.
2002      */
2003     rw_bytes_reg = readl(priv->smbus->io +
2004                 MLXBF_I2C_SMBUS_SLAVE_RS_MASTER_BYTES);
2005     recv_bytes = (rw_bytes_reg >> 8) & GENMASK(7, 0);
2006 
2007     /*
2008      * For now, the slave supports 128 bytes transfer. Discard remaining
2009      * data bytes if the master wrote more than
2010      * MLXBF_I2C_SLAVE_DATA_DESC_SIZE, i.e, the actual size of the slave
2011      * data descriptor.
2012      *
2013      * Note that we will never expect to transfer more than 128 bytes; as
2014      * specified in the SMBus standard, block transactions cannot exceed
2015      * 32 bytes.
2016      */
2017     recv_bytes = recv_bytes > MLXBF_I2C_SLAVE_DATA_DESC_SIZE ?
2018         MLXBF_I2C_SLAVE_DATA_DESC_SIZE : recv_bytes;
2019 
2020     if (read)
2021         mlxbf_smbus_irq_send(priv, recv_bytes);
2022     else
2023         mlxbf_smbus_irq_recv(priv, recv_bytes);
2024 
2025     return IRQ_HANDLED;
2026 }
2027 
2028 /* Return negative errno on error. */
2029 static s32 mlxbf_i2c_smbus_xfer(struct i2c_adapter *adap, u16 addr,
2030                 unsigned short flags, char read_write,
2031                 u8 command, int size,
2032                 union i2c_smbus_data *data)
2033 {
2034     struct mlxbf_i2c_smbus_request request = { 0 };
2035     struct mlxbf_i2c_priv *priv;
2036     bool read, pec;
2037     u8 byte_cnt;
2038 
2039     request.slave = addr;
2040 
2041     read = (read_write == I2C_SMBUS_READ);
2042     pec = flags & I2C_FUNC_SMBUS_PEC;
2043 
2044     switch (size) {
2045     case I2C_SMBUS_QUICK:
2046         mlxbf_i2c_smbus_quick_command(&request, read);
2047         dev_dbg(&adap->dev, "smbus quick, slave 0x%02x\n", addr);
2048         break;
2049 
2050     case I2C_SMBUS_BYTE:
2051         mlxbf_i2c_smbus_byte_func(&request,
2052                       read ? &data->byte : &command, read,
2053                       pec);
2054         dev_dbg(&adap->dev, "smbus %s byte, slave 0x%02x.\n",
2055             read ? "read" : "write", addr);
2056         break;
2057 
2058     case I2C_SMBUS_BYTE_DATA:
2059         mlxbf_i2c_smbus_data_byte_func(&request, &command, &data->byte,
2060                            read, pec);
2061         dev_dbg(&adap->dev, "smbus %s byte data at 0x%02x, slave 0x%02x.\n",
2062             read ? "read" : "write", command, addr);
2063         break;
2064 
2065     case I2C_SMBUS_WORD_DATA:
2066         mlxbf_i2c_smbus_data_word_func(&request, &command,
2067                            (u8 *)&data->word, read, pec);
2068         dev_dbg(&adap->dev, "smbus %s word data at 0x%02x, slave 0x%02x.\n",
2069             read ? "read" : "write", command, addr);
2070         break;
2071 
2072     case I2C_SMBUS_I2C_BLOCK_DATA:
2073         byte_cnt = data->block[0];
2074         mlxbf_i2c_smbus_i2c_block_func(&request, &command, data->block,
2075                            &byte_cnt, read, pec);
2076         dev_dbg(&adap->dev, "i2c %s block data, %d bytes at 0x%02x, slave 0x%02x.\n",
2077             read ? "read" : "write", byte_cnt, command, addr);
2078         break;
2079 
2080     case I2C_SMBUS_BLOCK_DATA:
2081         byte_cnt = read ? I2C_SMBUS_BLOCK_MAX : data->block[0];
2082         mlxbf_i2c_smbus_block_func(&request, &command, data->block,
2083                        &byte_cnt, read, pec);
2084         dev_dbg(&adap->dev, "smbus %s block data, %d bytes at 0x%02x, slave 0x%02x.\n",
2085             read ? "read" : "write", byte_cnt, command, addr);
2086         break;
2087 
2088     case I2C_FUNC_SMBUS_PROC_CALL:
2089         mlxbf_i2c_smbus_process_call_func(&request, &command,
2090                           (u8 *)&data->word, pec);
2091         dev_dbg(&adap->dev, "process call, wr/rd at 0x%02x, slave 0x%02x.\n",
2092             command, addr);
2093         break;
2094 
2095     case I2C_FUNC_SMBUS_BLOCK_PROC_CALL:
2096         byte_cnt = data->block[0];
2097         mlxbf_i2c_smbus_blk_process_call_func(&request, &command,
2098                               data->block, &byte_cnt,
2099                               pec);
2100         dev_dbg(&adap->dev, "block process call, wr/rd %d bytes, slave 0x%02x.\n",
2101             byte_cnt, addr);
2102         break;
2103 
2104     default:
2105         dev_dbg(&adap->dev, "Unsupported I2C/SMBus command %d\n",
2106             size);
2107         return -EOPNOTSUPP;
2108     }
2109 
2110     priv = i2c_get_adapdata(adap);
2111 
2112     return mlxbf_i2c_smbus_start_transaction(priv, &request);
2113 }
2114 
2115 static int mlxbf_i2c_reg_slave(struct i2c_client *slave)
2116 {
2117     struct mlxbf_i2c_priv *priv = i2c_get_adapdata(slave->adapter);
2118     int ret;
2119 
2120     if (priv->slave)
2121         return -EBUSY;
2122 
2123     /*
2124      * Do not support ten bit chip address and do not use Packet Error
2125      * Checking (PEC).
2126      */
2127     if (slave->flags & (I2C_CLIENT_TEN | I2C_CLIENT_PEC))
2128         return -EAFNOSUPPORT;
2129 
2130     ret = mlxbf_slave_enable(priv, slave->addr);
2131     if (ret < 0)
2132         return ret;
2133 
2134     priv->slave = slave;
2135 
2136     return 0;
2137 }
2138 
2139 static int mlxbf_i2c_unreg_slave(struct i2c_client *slave)
2140 {
2141     struct mlxbf_i2c_priv *priv = i2c_get_adapdata(slave->adapter);
2142     int ret;
2143 
2144     WARN_ON(!priv->slave);
2145 
2146     /* Unregister slave, i.e. disable the slave address in hardware. */
2147     ret = mlxbf_slave_disable(priv);
2148     if (ret < 0)
2149         return ret;
2150 
2151     priv->slave = NULL;
2152 
2153     return 0;
2154 }
2155 
2156 static u32 mlxbf_i2c_functionality(struct i2c_adapter *adap)
2157 {
2158     return MLXBF_I2C_FUNC_ALL;
2159 }
2160 
2161 static struct mlxbf_i2c_chip_info mlxbf_i2c_chip[] = {
2162     [MLXBF_I2C_CHIP_TYPE_1] = {
2163         .type = MLXBF_I2C_CHIP_TYPE_1,
2164         .shared_res = {
2165             [0] = &mlxbf_i2c_coalesce_res[MLXBF_I2C_CHIP_TYPE_1],
2166             [1] = &mlxbf_i2c_corepll_res[MLXBF_I2C_CHIP_TYPE_1],
2167             [2] = &mlxbf_i2c_gpio_res[MLXBF_I2C_CHIP_TYPE_1]
2168         },
2169         .calculate_freq = mlxbf_i2c_calculate_freq_from_tyu
2170     },
2171     [MLXBF_I2C_CHIP_TYPE_2] = {
2172         .type = MLXBF_I2C_CHIP_TYPE_2,
2173         .shared_res = {
2174             [0] = &mlxbf_i2c_corepll_res[MLXBF_I2C_CHIP_TYPE_2]
2175         },
2176         .calculate_freq = mlxbf_i2c_calculate_freq_from_yu
2177     }
2178 };
2179 
2180 static const struct i2c_algorithm mlxbf_i2c_algo = {
2181     .smbus_xfer = mlxbf_i2c_smbus_xfer,
2182     .functionality = mlxbf_i2c_functionality,
2183     .reg_slave = mlxbf_i2c_reg_slave,
2184     .unreg_slave = mlxbf_i2c_unreg_slave,
2185 };
2186 
2187 static struct i2c_adapter_quirks mlxbf_i2c_quirks = {
2188     .max_read_len = MLXBF_I2C_MASTER_DATA_R_LENGTH,
2189     .max_write_len = MLXBF_I2C_MASTER_DATA_W_LENGTH,
2190 };
2191 
2192 static const struct of_device_id mlxbf_i2c_dt_ids[] = {
2193     {
2194         .compatible = "mellanox,i2c-mlxbf1",
2195         .data = &mlxbf_i2c_chip[MLXBF_I2C_CHIP_TYPE_1]
2196     },
2197     {
2198         .compatible = "mellanox,i2c-mlxbf2",
2199         .data = &mlxbf_i2c_chip[MLXBF_I2C_CHIP_TYPE_2]
2200     },
2201     {},
2202 };
2203 
2204 MODULE_DEVICE_TABLE(of, mlxbf_i2c_dt_ids);
2205 
2206 #ifdef CONFIG_ACPI
2207 static const struct acpi_device_id mlxbf_i2c_acpi_ids[] = {
2208     { "MLNXBF03", (kernel_ulong_t)&mlxbf_i2c_chip[MLXBF_I2C_CHIP_TYPE_1] },
2209     { "MLNXBF23", (kernel_ulong_t)&mlxbf_i2c_chip[MLXBF_I2C_CHIP_TYPE_2] },
2210     {},
2211 };
2212 
2213 MODULE_DEVICE_TABLE(acpi, mlxbf_i2c_acpi_ids);
2214 
2215 static int mlxbf_i2c_acpi_probe(struct device *dev, struct mlxbf_i2c_priv *priv)
2216 {
2217     const struct acpi_device_id *aid;
2218     struct acpi_device *adev;
2219     unsigned long bus_id = 0;
2220     const char *uid;
2221     int ret;
2222 
2223     if (acpi_disabled)
2224         return -ENOENT;
2225 
2226     adev = ACPI_COMPANION(dev);
2227     if (!adev)
2228         return -ENXIO;
2229 
2230     aid = acpi_match_device(mlxbf_i2c_acpi_ids, dev);
2231     if (!aid)
2232         return -ENODEV;
2233 
2234     priv->chip = (struct mlxbf_i2c_chip_info *)aid->driver_data;
2235 
2236     uid = acpi_device_uid(adev);
2237     if (!uid || !(*uid)) {
2238         dev_err(dev, "Cannot retrieve UID\n");
2239         return -ENODEV;
2240     }
2241 
2242     ret = kstrtoul(uid, 0, &bus_id);
2243     if (!ret)
2244         priv->bus = bus_id;
2245 
2246     return ret;
2247 }
2248 #else
2249 static int mlxbf_i2c_acpi_probe(struct device *dev, struct mlxbf_i2c_priv *priv)
2250 {
2251     return -ENOENT;
2252 }
2253 #endif /* CONFIG_ACPI */
2254 
2255 static int mlxbf_i2c_of_probe(struct device *dev, struct mlxbf_i2c_priv *priv)
2256 {
2257     const struct of_device_id *oid;
2258     int bus_id = -1;
2259 
2260     if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
2261         oid = of_match_node(mlxbf_i2c_dt_ids, dev->of_node);
2262         if (!oid)
2263             return -ENODEV;
2264 
2265         priv->chip = oid->data;
2266 
2267         bus_id = of_alias_get_id(dev->of_node, "i2c");
2268         if (bus_id >= 0)
2269             priv->bus = bus_id;
2270     }
2271 
2272     if (bus_id < 0) {
2273         dev_err(dev, "Cannot get bus id");
2274         return bus_id;
2275     }
2276 
2277     return 0;
2278 }
2279 
2280 static int mlxbf_i2c_probe(struct platform_device *pdev)
2281 {
2282     struct device *dev = &pdev->dev;
2283     struct mlxbf_i2c_priv *priv;
2284     struct i2c_adapter *adap;
2285     int irq, ret;
2286 
2287     priv = devm_kzalloc(dev, sizeof(struct mlxbf_i2c_priv), GFP_KERNEL);
2288     if (!priv)
2289         return -ENOMEM;
2290 
2291     ret = mlxbf_i2c_acpi_probe(dev, priv);
2292     if (ret < 0 && ret != -ENOENT && ret != -ENXIO)
2293         ret = mlxbf_i2c_of_probe(dev, priv);
2294 
2295     if (ret < 0)
2296         return ret;
2297 
2298     ret = mlxbf_i2c_init_resource(pdev, &priv->smbus,
2299                       MLXBF_I2C_SMBUS_RES);
2300     if (ret < 0) {
2301         dev_err(dev, "Cannot fetch smbus resource info");
2302         return ret;
2303     }
2304 
2305     ret = mlxbf_i2c_init_resource(pdev, &priv->mst_cause,
2306                       MLXBF_I2C_MST_CAUSE_RES);
2307     if (ret < 0) {
2308         dev_err(dev, "Cannot fetch cause master resource info");
2309         return ret;
2310     }
2311 
2312     ret = mlxbf_i2c_init_resource(pdev, &priv->slv_cause,
2313                       MLXBF_I2C_SLV_CAUSE_RES);
2314     if (ret < 0) {
2315         dev_err(dev, "Cannot fetch cause slave resource info");
2316         return ret;
2317     }
2318 
2319     adap = &priv->adap;
2320     adap->owner = THIS_MODULE;
2321     adap->class = I2C_CLASS_HWMON;
2322     adap->algo = &mlxbf_i2c_algo;
2323     adap->quirks = &mlxbf_i2c_quirks;
2324     adap->dev.parent = dev;
2325     adap->dev.of_node = dev->of_node;
2326     adap->nr = priv->bus;
2327 
2328     snprintf(adap->name, sizeof(adap->name), "i2c%d", adap->nr);
2329     i2c_set_adapdata(adap, priv);
2330 
2331     /* Read Core PLL frequency. */
2332     ret = mlxbf_i2c_calculate_corepll_freq(pdev, priv);
2333     if (ret < 0) {
2334         dev_err(dev, "cannot get core clock frequency\n");
2335         /* Set to default value. */
2336         priv->frequency = MLXBF_I2C_COREPLL_FREQ;
2337     }
2338 
2339     /*
2340      * Initialize master.
2341      * Note that a physical bus might be shared among Linux and firmware
2342      * (e.g., ATF). Thus, the bus should be initialized and ready and
2343      * bus initialization would be unnecessary. This requires additional
2344      * knowledge about physical busses. But, since an extra initialization
2345      * does not really hurt, then keep the code as is.
2346      */
2347     ret = mlxbf_i2c_init_master(pdev, priv);
2348     if (ret < 0) {
2349         dev_err(dev, "failed to initialize smbus master %d",
2350             priv->bus);
2351         return ret;
2352     }
2353 
2354     mlxbf_i2c_init_timings(pdev, priv);
2355 
2356     mlxbf_i2c_init_slave(pdev, priv);
2357 
2358     irq = platform_get_irq(pdev, 0);
2359     if (irq < 0)
2360         return irq;
2361     ret = devm_request_irq(dev, irq, mlxbf_smbus_irq,
2362                    IRQF_ONESHOT | IRQF_SHARED | IRQF_PROBE_SHARED,
2363                    dev_name(dev), priv);
2364     if (ret < 0) {
2365         dev_err(dev, "Cannot get irq %d\n", irq);
2366         return ret;
2367     }
2368 
2369     priv->irq = irq;
2370 
2371     platform_set_drvdata(pdev, priv);
2372 
2373     ret = i2c_add_numbered_adapter(adap);
2374     if (ret < 0)
2375         return ret;
2376 
2377     mutex_lock(&mlxbf_i2c_bus_lock);
2378     mlxbf_i2c_bus_count++;
2379     mutex_unlock(&mlxbf_i2c_bus_lock);
2380 
2381     return 0;
2382 }
2383 
2384 static int mlxbf_i2c_remove(struct platform_device *pdev)
2385 {
2386     struct mlxbf_i2c_priv *priv = platform_get_drvdata(pdev);
2387     struct device *dev = &pdev->dev;
2388     struct resource *params;
2389 
2390     params = priv->smbus->params;
2391     devm_release_mem_region(dev, params->start, resource_size(params));
2392 
2393     params = priv->mst_cause->params;
2394     devm_release_mem_region(dev, params->start, resource_size(params));
2395 
2396     params = priv->slv_cause->params;
2397     devm_release_mem_region(dev, params->start, resource_size(params));
2398 
2399     /*
2400      * Release shared resources. This should be done when releasing
2401      * the I2C controller.
2402      */
2403     mutex_lock(&mlxbf_i2c_bus_lock);
2404     if (--mlxbf_i2c_bus_count == 0) {
2405         mlxbf_i2c_release_coalesce(pdev, priv);
2406         mlxbf_i2c_release_corepll(pdev, priv);
2407         mlxbf_i2c_release_gpio(pdev, priv);
2408     }
2409     mutex_unlock(&mlxbf_i2c_bus_lock);
2410 
2411     devm_free_irq(dev, priv->irq, priv);
2412 
2413     i2c_del_adapter(&priv->adap);
2414 
2415     return 0;
2416 }
2417 
2418 static struct platform_driver mlxbf_i2c_driver = {
2419     .probe = mlxbf_i2c_probe,
2420     .remove = mlxbf_i2c_remove,
2421     .driver = {
2422         .name = "i2c-mlxbf",
2423         .of_match_table = mlxbf_i2c_dt_ids,
2424 #ifdef CONFIG_ACPI
2425         .acpi_match_table = ACPI_PTR(mlxbf_i2c_acpi_ids),
2426 #endif /* CONFIG_ACPI  */
2427     },
2428 };
2429 
2430 static int __init mlxbf_i2c_init(void)
2431 {
2432     mutex_init(&mlxbf_i2c_coalesce_lock);
2433     mutex_init(&mlxbf_i2c_corepll_lock);
2434     mutex_init(&mlxbf_i2c_gpio_lock);
2435 
2436     mutex_init(&mlxbf_i2c_bus_lock);
2437 
2438     return platform_driver_register(&mlxbf_i2c_driver);
2439 }
2440 module_init(mlxbf_i2c_init);
2441 
2442 static void __exit mlxbf_i2c_exit(void)
2443 {
2444     platform_driver_unregister(&mlxbf_i2c_driver);
2445 
2446     mutex_destroy(&mlxbf_i2c_bus_lock);
2447 
2448     mutex_destroy(&mlxbf_i2c_gpio_lock);
2449     mutex_destroy(&mlxbf_i2c_corepll_lock);
2450     mutex_destroy(&mlxbf_i2c_coalesce_lock);
2451 }
2452 module_exit(mlxbf_i2c_exit);
2453 
2454 MODULE_DESCRIPTION("Mellanox BlueField I2C bus driver");
2455 MODULE_AUTHOR("Khalil Blaiech <kblaiech@nvidia.com>");
2456 MODULE_LICENSE("GPL v2");