Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 // Copyright (C) 2014 Broadcom Corporation
0003 
0004 #include <linux/delay.h>
0005 #include <linux/i2c.h>
0006 #include <linux/interrupt.h>
0007 #include <linux/io.h>
0008 #include <linux/kernel.h>
0009 #include <linux/module.h>
0010 #include <linux/of_device.h>
0011 #include <linux/platform_device.h>
0012 #include <linux/slab.h>
0013 
0014 #define IDM_CTRL_DIRECT_OFFSET       0x00
0015 #define CFG_OFFSET                   0x00
0016 #define CFG_RESET_SHIFT              31
0017 #define CFG_EN_SHIFT                 30
0018 #define CFG_SLAVE_ADDR_0_SHIFT       28
0019 #define CFG_M_RETRY_CNT_SHIFT        16
0020 #define CFG_M_RETRY_CNT_MASK         0x0f
0021 
0022 #define TIM_CFG_OFFSET               0x04
0023 #define TIM_CFG_MODE_400_SHIFT       31
0024 #define TIM_RAND_SLAVE_STRETCH_SHIFT      24
0025 #define TIM_RAND_SLAVE_STRETCH_MASK       0x7f
0026 #define TIM_PERIODIC_SLAVE_STRETCH_SHIFT  16
0027 #define TIM_PERIODIC_SLAVE_STRETCH_MASK   0x7f
0028 
0029 #define S_CFG_SMBUS_ADDR_OFFSET           0x08
0030 #define S_CFG_EN_NIC_SMB_ADDR3_SHIFT      31
0031 #define S_CFG_NIC_SMB_ADDR3_SHIFT         24
0032 #define S_CFG_NIC_SMB_ADDR3_MASK          0x7f
0033 #define S_CFG_EN_NIC_SMB_ADDR2_SHIFT      23
0034 #define S_CFG_NIC_SMB_ADDR2_SHIFT         16
0035 #define S_CFG_NIC_SMB_ADDR2_MASK          0x7f
0036 #define S_CFG_EN_NIC_SMB_ADDR1_SHIFT      15
0037 #define S_CFG_NIC_SMB_ADDR1_SHIFT         8
0038 #define S_CFG_NIC_SMB_ADDR1_MASK          0x7f
0039 #define S_CFG_EN_NIC_SMB_ADDR0_SHIFT      7
0040 #define S_CFG_NIC_SMB_ADDR0_SHIFT         0
0041 #define S_CFG_NIC_SMB_ADDR0_MASK          0x7f
0042 
0043 #define M_FIFO_CTRL_OFFSET           0x0c
0044 #define M_FIFO_RX_FLUSH_SHIFT        31
0045 #define M_FIFO_TX_FLUSH_SHIFT        30
0046 #define M_FIFO_RX_CNT_SHIFT          16
0047 #define M_FIFO_RX_CNT_MASK           0x7f
0048 #define M_FIFO_RX_THLD_SHIFT         8
0049 #define M_FIFO_RX_THLD_MASK          0x3f
0050 
0051 #define S_FIFO_CTRL_OFFSET           0x10
0052 #define S_FIFO_RX_FLUSH_SHIFT        31
0053 #define S_FIFO_TX_FLUSH_SHIFT        30
0054 #define S_FIFO_RX_CNT_SHIFT          16
0055 #define S_FIFO_RX_CNT_MASK           0x7f
0056 #define S_FIFO_RX_THLD_SHIFT         8
0057 #define S_FIFO_RX_THLD_MASK          0x3f
0058 
0059 #define M_CMD_OFFSET                 0x30
0060 #define M_CMD_START_BUSY_SHIFT       31
0061 #define M_CMD_STATUS_SHIFT           25
0062 #define M_CMD_STATUS_MASK            0x07
0063 #define M_CMD_STATUS_SUCCESS         0x0
0064 #define M_CMD_STATUS_LOST_ARB        0x1
0065 #define M_CMD_STATUS_NACK_ADDR       0x2
0066 #define M_CMD_STATUS_NACK_DATA       0x3
0067 #define M_CMD_STATUS_TIMEOUT         0x4
0068 #define M_CMD_STATUS_FIFO_UNDERRUN   0x5
0069 #define M_CMD_STATUS_RX_FIFO_FULL    0x6
0070 #define M_CMD_PROTOCOL_SHIFT         9
0071 #define M_CMD_PROTOCOL_MASK          0xf
0072 #define M_CMD_PROTOCOL_QUICK         0x0
0073 #define M_CMD_PROTOCOL_BLK_WR        0x7
0074 #define M_CMD_PROTOCOL_BLK_RD        0x8
0075 #define M_CMD_PROTOCOL_PROCESS       0xa
0076 #define M_CMD_PEC_SHIFT              8
0077 #define M_CMD_RD_CNT_SHIFT           0
0078 #define M_CMD_RD_CNT_MASK            0xff
0079 
0080 #define S_CMD_OFFSET                 0x34
0081 #define S_CMD_START_BUSY_SHIFT       31
0082 #define S_CMD_STATUS_SHIFT           23
0083 #define S_CMD_STATUS_MASK            0x07
0084 #define S_CMD_STATUS_SUCCESS         0x0
0085 #define S_CMD_STATUS_TIMEOUT         0x5
0086 #define S_CMD_STATUS_MASTER_ABORT    0x7
0087 
0088 #define IE_OFFSET                    0x38
0089 #define IE_M_RX_FIFO_FULL_SHIFT      31
0090 #define IE_M_RX_THLD_SHIFT           30
0091 #define IE_M_START_BUSY_SHIFT        28
0092 #define IE_M_TX_UNDERRUN_SHIFT       27
0093 #define IE_S_RX_FIFO_FULL_SHIFT      26
0094 #define IE_S_RX_THLD_SHIFT           25
0095 #define IE_S_RX_EVENT_SHIFT          24
0096 #define IE_S_START_BUSY_SHIFT        23
0097 #define IE_S_TX_UNDERRUN_SHIFT       22
0098 #define IE_S_RD_EVENT_SHIFT          21
0099 
0100 #define IS_OFFSET                    0x3c
0101 #define IS_M_RX_FIFO_FULL_SHIFT      31
0102 #define IS_M_RX_THLD_SHIFT           30
0103 #define IS_M_START_BUSY_SHIFT        28
0104 #define IS_M_TX_UNDERRUN_SHIFT       27
0105 #define IS_S_RX_FIFO_FULL_SHIFT      26
0106 #define IS_S_RX_THLD_SHIFT           25
0107 #define IS_S_RX_EVENT_SHIFT          24
0108 #define IS_S_START_BUSY_SHIFT        23
0109 #define IS_S_TX_UNDERRUN_SHIFT       22
0110 #define IS_S_RD_EVENT_SHIFT          21
0111 
0112 #define M_TX_OFFSET                  0x40
0113 #define M_TX_WR_STATUS_SHIFT         31
0114 #define M_TX_DATA_SHIFT              0
0115 #define M_TX_DATA_MASK               0xff
0116 
0117 #define M_RX_OFFSET                  0x44
0118 #define M_RX_STATUS_SHIFT            30
0119 #define M_RX_STATUS_MASK             0x03
0120 #define M_RX_PEC_ERR_SHIFT           29
0121 #define M_RX_DATA_SHIFT              0
0122 #define M_RX_DATA_MASK               0xff
0123 
0124 #define S_TX_OFFSET                  0x48
0125 #define S_TX_WR_STATUS_SHIFT         31
0126 #define S_TX_DATA_SHIFT              0
0127 #define S_TX_DATA_MASK               0xff
0128 
0129 #define S_RX_OFFSET                  0x4c
0130 #define S_RX_STATUS_SHIFT            30
0131 #define S_RX_STATUS_MASK             0x03
0132 #define S_RX_PEC_ERR_SHIFT           29
0133 #define S_RX_DATA_SHIFT              0
0134 #define S_RX_DATA_MASK               0xff
0135 
0136 #define I2C_TIMEOUT_MSEC             50000
0137 #define M_TX_RX_FIFO_SIZE            64
0138 #define M_RX_FIFO_MAX_THLD_VALUE     (M_TX_RX_FIFO_SIZE - 1)
0139 
0140 #define M_RX_MAX_READ_LEN            255
0141 #define M_RX_FIFO_THLD_VALUE         50
0142 
0143 #define IE_M_ALL_INTERRUPT_SHIFT     27
0144 #define IE_M_ALL_INTERRUPT_MASK      0x1e
0145 
0146 #define SLAVE_READ_WRITE_BIT_MASK    0x1
0147 #define SLAVE_READ_WRITE_BIT_SHIFT   0x1
0148 #define SLAVE_MAX_SIZE_TRANSACTION   64
0149 #define SLAVE_CLOCK_STRETCH_TIME     25
0150 
0151 #define IE_S_ALL_INTERRUPT_SHIFT     21
0152 #define IE_S_ALL_INTERRUPT_MASK      0x3f
0153 /*
0154  * It takes ~18us to reading 10bytes of data, hence to keep tasklet
0155  * running for less time, max slave read per tasklet is set to 10 bytes.
0156  */
0157 #define MAX_SLAVE_RX_PER_INT         10
0158 
0159 enum i2c_slave_read_status {
0160     I2C_SLAVE_RX_FIFO_EMPTY = 0,
0161     I2C_SLAVE_RX_START,
0162     I2C_SLAVE_RX_DATA,
0163     I2C_SLAVE_RX_END,
0164 };
0165 
0166 enum bus_speed_index {
0167     I2C_SPD_100K = 0,
0168     I2C_SPD_400K,
0169 };
0170 
0171 enum bcm_iproc_i2c_type {
0172     IPROC_I2C,
0173     IPROC_I2C_NIC
0174 };
0175 
0176 struct bcm_iproc_i2c_dev {
0177     struct device *device;
0178     enum bcm_iproc_i2c_type type;
0179     int irq;
0180 
0181     void __iomem *base;
0182     void __iomem *idm_base;
0183 
0184     u32 ape_addr_mask;
0185 
0186     /* lock for indirect access through IDM */
0187     spinlock_t idm_lock;
0188 
0189     struct i2c_adapter adapter;
0190     unsigned int bus_speed;
0191 
0192     struct completion done;
0193     int xfer_is_done;
0194 
0195     struct i2c_msg *msg;
0196 
0197     struct i2c_client *slave;
0198 
0199     /* bytes that have been transferred */
0200     unsigned int tx_bytes;
0201     /* bytes that have been read */
0202     unsigned int rx_bytes;
0203     unsigned int thld_bytes;
0204 
0205     bool slave_rx_only;
0206     bool rx_start_rcvd;
0207     bool slave_read_complete;
0208     u32 tx_underrun;
0209     u32 slave_int_mask;
0210     struct tasklet_struct slave_rx_tasklet;
0211 };
0212 
0213 /* tasklet to process slave rx data */
0214 static void slave_rx_tasklet_fn(unsigned long);
0215 
0216 /*
0217  * Can be expanded in the future if more interrupt status bits are utilized
0218  */
0219 #define ISR_MASK (BIT(IS_M_START_BUSY_SHIFT) | BIT(IS_M_TX_UNDERRUN_SHIFT)\
0220         | BIT(IS_M_RX_THLD_SHIFT))
0221 
0222 #define ISR_MASK_SLAVE (BIT(IS_S_START_BUSY_SHIFT)\
0223         | BIT(IS_S_RX_EVENT_SHIFT) | BIT(IS_S_RD_EVENT_SHIFT)\
0224         | BIT(IS_S_TX_UNDERRUN_SHIFT) | BIT(IS_S_RX_FIFO_FULL_SHIFT)\
0225         | BIT(IS_S_RX_THLD_SHIFT))
0226 
0227 static int bcm_iproc_i2c_reg_slave(struct i2c_client *slave);
0228 static int bcm_iproc_i2c_unreg_slave(struct i2c_client *slave);
0229 static void bcm_iproc_i2c_enable_disable(struct bcm_iproc_i2c_dev *iproc_i2c,
0230                      bool enable);
0231 
0232 static inline u32 iproc_i2c_rd_reg(struct bcm_iproc_i2c_dev *iproc_i2c,
0233                    u32 offset)
0234 {
0235     u32 val;
0236 
0237     if (iproc_i2c->idm_base) {
0238         spin_lock(&iproc_i2c->idm_lock);
0239         writel(iproc_i2c->ape_addr_mask,
0240                iproc_i2c->idm_base + IDM_CTRL_DIRECT_OFFSET);
0241         val = readl(iproc_i2c->base + offset);
0242         spin_unlock(&iproc_i2c->idm_lock);
0243     } else {
0244         val = readl(iproc_i2c->base + offset);
0245     }
0246 
0247     return val;
0248 }
0249 
0250 static inline void iproc_i2c_wr_reg(struct bcm_iproc_i2c_dev *iproc_i2c,
0251                     u32 offset, u32 val)
0252 {
0253     if (iproc_i2c->idm_base) {
0254         spin_lock(&iproc_i2c->idm_lock);
0255         writel(iproc_i2c->ape_addr_mask,
0256                iproc_i2c->idm_base + IDM_CTRL_DIRECT_OFFSET);
0257         writel(val, iproc_i2c->base + offset);
0258         spin_unlock(&iproc_i2c->idm_lock);
0259     } else {
0260         writel(val, iproc_i2c->base + offset);
0261     }
0262 }
0263 
0264 static void bcm_iproc_i2c_slave_init(
0265     struct bcm_iproc_i2c_dev *iproc_i2c, bool need_reset)
0266 {
0267     u32 val;
0268 
0269     iproc_i2c->tx_underrun = 0;
0270     if (need_reset) {
0271         /* put controller in reset */
0272         val = iproc_i2c_rd_reg(iproc_i2c, CFG_OFFSET);
0273         val |= BIT(CFG_RESET_SHIFT);
0274         iproc_i2c_wr_reg(iproc_i2c, CFG_OFFSET, val);
0275 
0276         /* wait 100 usec per spec */
0277         udelay(100);
0278 
0279         /* bring controller out of reset */
0280         val &= ~(BIT(CFG_RESET_SHIFT));
0281         iproc_i2c_wr_reg(iproc_i2c, CFG_OFFSET, val);
0282     }
0283 
0284     /* flush TX/RX FIFOs */
0285     val = (BIT(S_FIFO_RX_FLUSH_SHIFT) | BIT(S_FIFO_TX_FLUSH_SHIFT));
0286     iproc_i2c_wr_reg(iproc_i2c, S_FIFO_CTRL_OFFSET, val);
0287 
0288     /* Maximum slave stretch time */
0289     val = iproc_i2c_rd_reg(iproc_i2c, TIM_CFG_OFFSET);
0290     val &= ~(TIM_RAND_SLAVE_STRETCH_MASK << TIM_RAND_SLAVE_STRETCH_SHIFT);
0291     val |= (SLAVE_CLOCK_STRETCH_TIME << TIM_RAND_SLAVE_STRETCH_SHIFT);
0292     iproc_i2c_wr_reg(iproc_i2c, TIM_CFG_OFFSET, val);
0293 
0294     /* Configure the slave address */
0295     val = iproc_i2c_rd_reg(iproc_i2c, S_CFG_SMBUS_ADDR_OFFSET);
0296     val |= BIT(S_CFG_EN_NIC_SMB_ADDR3_SHIFT);
0297     val &= ~(S_CFG_NIC_SMB_ADDR3_MASK << S_CFG_NIC_SMB_ADDR3_SHIFT);
0298     val |= (iproc_i2c->slave->addr << S_CFG_NIC_SMB_ADDR3_SHIFT);
0299     iproc_i2c_wr_reg(iproc_i2c, S_CFG_SMBUS_ADDR_OFFSET, val);
0300 
0301     /* clear all pending slave interrupts */
0302     iproc_i2c_wr_reg(iproc_i2c, IS_OFFSET, ISR_MASK_SLAVE);
0303 
0304     /* Enable interrupt register to indicate a valid byte in receive fifo */
0305     val = BIT(IE_S_RX_EVENT_SHIFT);
0306     /* Enable interrupt register to indicate Slave Rx FIFO Full */
0307     val |= BIT(IE_S_RX_FIFO_FULL_SHIFT);
0308     /* Enable interrupt register to indicate a Master read transaction */
0309     val |= BIT(IE_S_RD_EVENT_SHIFT);
0310     /* Enable interrupt register for the Slave BUSY command */
0311     val |= BIT(IE_S_START_BUSY_SHIFT);
0312     iproc_i2c->slave_int_mask = val;
0313     iproc_i2c_wr_reg(iproc_i2c, IE_OFFSET, val);
0314 }
0315 
0316 static void bcm_iproc_i2c_check_slave_status(
0317     struct bcm_iproc_i2c_dev *iproc_i2c)
0318 {
0319     u32 val;
0320 
0321     val = iproc_i2c_rd_reg(iproc_i2c, S_CMD_OFFSET);
0322     /* status is valid only when START_BUSY is cleared after it was set */
0323     if (val & BIT(S_CMD_START_BUSY_SHIFT))
0324         return;
0325 
0326     val = (val >> S_CMD_STATUS_SHIFT) & S_CMD_STATUS_MASK;
0327     if (val == S_CMD_STATUS_TIMEOUT || val == S_CMD_STATUS_MASTER_ABORT) {
0328         dev_err(iproc_i2c->device, (val == S_CMD_STATUS_TIMEOUT) ?
0329             "slave random stretch time timeout\n" :
0330             "Master aborted read transaction\n");
0331         /* re-initialize i2c for recovery */
0332         bcm_iproc_i2c_enable_disable(iproc_i2c, false);
0333         bcm_iproc_i2c_slave_init(iproc_i2c, true);
0334         bcm_iproc_i2c_enable_disable(iproc_i2c, true);
0335     }
0336 }
0337 
0338 static void bcm_iproc_i2c_slave_read(struct bcm_iproc_i2c_dev *iproc_i2c)
0339 {
0340     u8 rx_data, rx_status;
0341     u32 rx_bytes = 0;
0342     u32 val;
0343 
0344     while (rx_bytes < MAX_SLAVE_RX_PER_INT) {
0345         val = iproc_i2c_rd_reg(iproc_i2c, S_RX_OFFSET);
0346         rx_status = (val >> S_RX_STATUS_SHIFT) & S_RX_STATUS_MASK;
0347         rx_data = ((val >> S_RX_DATA_SHIFT) & S_RX_DATA_MASK);
0348 
0349         if (rx_status == I2C_SLAVE_RX_START) {
0350             /* Start of SMBUS Master write */
0351             i2c_slave_event(iproc_i2c->slave,
0352                     I2C_SLAVE_WRITE_REQUESTED, &rx_data);
0353             iproc_i2c->rx_start_rcvd = true;
0354             iproc_i2c->slave_read_complete = false;
0355         } else if (rx_status == I2C_SLAVE_RX_DATA &&
0356                iproc_i2c->rx_start_rcvd) {
0357             /* Middle of SMBUS Master write */
0358             i2c_slave_event(iproc_i2c->slave,
0359                     I2C_SLAVE_WRITE_RECEIVED, &rx_data);
0360         } else if (rx_status == I2C_SLAVE_RX_END &&
0361                iproc_i2c->rx_start_rcvd) {
0362             /* End of SMBUS Master write */
0363             if (iproc_i2c->slave_rx_only)
0364                 i2c_slave_event(iproc_i2c->slave,
0365                         I2C_SLAVE_WRITE_RECEIVED,
0366                         &rx_data);
0367 
0368             i2c_slave_event(iproc_i2c->slave, I2C_SLAVE_STOP,
0369                     &rx_data);
0370         } else if (rx_status == I2C_SLAVE_RX_FIFO_EMPTY) {
0371             iproc_i2c->rx_start_rcvd = false;
0372             iproc_i2c->slave_read_complete = true;
0373             break;
0374         }
0375 
0376         rx_bytes++;
0377     }
0378 }
0379 
0380 static void slave_rx_tasklet_fn(unsigned long data)
0381 {
0382     struct bcm_iproc_i2c_dev *iproc_i2c = (struct bcm_iproc_i2c_dev *)data;
0383     u32 int_clr;
0384 
0385     bcm_iproc_i2c_slave_read(iproc_i2c);
0386 
0387     /* clear pending IS_S_RX_EVENT_SHIFT interrupt */
0388     int_clr = BIT(IS_S_RX_EVENT_SHIFT);
0389 
0390     if (!iproc_i2c->slave_rx_only && iproc_i2c->slave_read_complete) {
0391         /*
0392          * In case of single byte master-read request,
0393          * IS_S_TX_UNDERRUN_SHIFT event is generated before
0394          * IS_S_START_BUSY_SHIFT event. Hence start slave data send
0395          * from first IS_S_TX_UNDERRUN_SHIFT event.
0396          *
0397          * This means don't send any data from slave when
0398          * IS_S_RD_EVENT_SHIFT event is generated else it will increment
0399          * eeprom or other backend slave driver read pointer twice.
0400          */
0401         iproc_i2c->tx_underrun = 0;
0402         iproc_i2c->slave_int_mask |= BIT(IE_S_TX_UNDERRUN_SHIFT);
0403 
0404         /* clear IS_S_RD_EVENT_SHIFT interrupt */
0405         int_clr |= BIT(IS_S_RD_EVENT_SHIFT);
0406     }
0407 
0408     /* clear slave interrupt */
0409     iproc_i2c_wr_reg(iproc_i2c, IS_OFFSET, int_clr);
0410     /* enable slave interrupts */
0411     iproc_i2c_wr_reg(iproc_i2c, IE_OFFSET, iproc_i2c->slave_int_mask);
0412 }
0413 
0414 static bool bcm_iproc_i2c_slave_isr(struct bcm_iproc_i2c_dev *iproc_i2c,
0415                     u32 status)
0416 {
0417     u32 val;
0418     u8 value;
0419 
0420     /*
0421      * Slave events in case of master-write, master-write-read and,
0422      * master-read
0423      *
0424      * Master-write     : only IS_S_RX_EVENT_SHIFT event
0425      * Master-write-read: both IS_S_RX_EVENT_SHIFT and IS_S_RD_EVENT_SHIFT
0426      *                    events
0427      * Master-read      : both IS_S_RX_EVENT_SHIFT and IS_S_RD_EVENT_SHIFT
0428      *                    events or only IS_S_RD_EVENT_SHIFT
0429      *
0430      * iproc has a slave rx fifo size of 64 bytes. Rx fifo full interrupt
0431      * (IS_S_RX_FIFO_FULL_SHIFT) will be generated when RX fifo becomes
0432      * full. This can happen if Master issues write requests of more than
0433      * 64 bytes.
0434      */
0435     if (status & BIT(IS_S_RX_EVENT_SHIFT) ||
0436         status & BIT(IS_S_RD_EVENT_SHIFT) ||
0437         status & BIT(IS_S_RX_FIFO_FULL_SHIFT)) {
0438         /* disable slave interrupts */
0439         val = iproc_i2c_rd_reg(iproc_i2c, IE_OFFSET);
0440         val &= ~iproc_i2c->slave_int_mask;
0441         iproc_i2c_wr_reg(iproc_i2c, IE_OFFSET, val);
0442 
0443         if (status & BIT(IS_S_RD_EVENT_SHIFT))
0444             /* Master-write-read request */
0445             iproc_i2c->slave_rx_only = false;
0446         else
0447             /* Master-write request only */
0448             iproc_i2c->slave_rx_only = true;
0449 
0450         /* schedule tasklet to read data later */
0451         tasklet_schedule(&iproc_i2c->slave_rx_tasklet);
0452 
0453         /*
0454          * clear only IS_S_RX_EVENT_SHIFT and
0455          * IS_S_RX_FIFO_FULL_SHIFT interrupt.
0456          */
0457         val = BIT(IS_S_RX_EVENT_SHIFT);
0458         if (status & BIT(IS_S_RX_FIFO_FULL_SHIFT))
0459             val |= BIT(IS_S_RX_FIFO_FULL_SHIFT);
0460         iproc_i2c_wr_reg(iproc_i2c, IS_OFFSET, val);
0461     }
0462 
0463     if (status & BIT(IS_S_TX_UNDERRUN_SHIFT)) {
0464         iproc_i2c->tx_underrun++;
0465         if (iproc_i2c->tx_underrun == 1)
0466             /* Start of SMBUS for Master Read */
0467             i2c_slave_event(iproc_i2c->slave,
0468                     I2C_SLAVE_READ_REQUESTED,
0469                     &value);
0470         else
0471             /* Master read other than start */
0472             i2c_slave_event(iproc_i2c->slave,
0473                     I2C_SLAVE_READ_PROCESSED,
0474                     &value);
0475 
0476         iproc_i2c_wr_reg(iproc_i2c, S_TX_OFFSET, value);
0477         /* start transfer */
0478         val = BIT(S_CMD_START_BUSY_SHIFT);
0479         iproc_i2c_wr_reg(iproc_i2c, S_CMD_OFFSET, val);
0480 
0481         /* clear interrupt */
0482         iproc_i2c_wr_reg(iproc_i2c, IS_OFFSET,
0483                  BIT(IS_S_TX_UNDERRUN_SHIFT));
0484     }
0485 
0486     /* Stop received from master in case of master read transaction */
0487     if (status & BIT(IS_S_START_BUSY_SHIFT)) {
0488         /*
0489          * Disable interrupt for TX FIFO becomes empty and
0490          * less than PKT_LENGTH bytes were output on the SMBUS
0491          */
0492         iproc_i2c->slave_int_mask &= ~BIT(IE_S_TX_UNDERRUN_SHIFT);
0493         iproc_i2c_wr_reg(iproc_i2c, IE_OFFSET,
0494                  iproc_i2c->slave_int_mask);
0495 
0496         /* End of SMBUS for Master Read */
0497         val = BIT(S_TX_WR_STATUS_SHIFT);
0498         iproc_i2c_wr_reg(iproc_i2c, S_TX_OFFSET, val);
0499 
0500         val = BIT(S_CMD_START_BUSY_SHIFT);
0501         iproc_i2c_wr_reg(iproc_i2c, S_CMD_OFFSET, val);
0502 
0503         /* flush TX FIFOs */
0504         val = iproc_i2c_rd_reg(iproc_i2c, S_FIFO_CTRL_OFFSET);
0505         val |= (BIT(S_FIFO_TX_FLUSH_SHIFT));
0506         iproc_i2c_wr_reg(iproc_i2c, S_FIFO_CTRL_OFFSET, val);
0507 
0508         i2c_slave_event(iproc_i2c->slave, I2C_SLAVE_STOP, &value);
0509 
0510         /* clear interrupt */
0511         iproc_i2c_wr_reg(iproc_i2c, IS_OFFSET,
0512                  BIT(IS_S_START_BUSY_SHIFT));
0513     }
0514 
0515     /* check slave transmit status only if slave is transmitting */
0516     if (!iproc_i2c->slave_rx_only)
0517         bcm_iproc_i2c_check_slave_status(iproc_i2c);
0518 
0519     return true;
0520 }
0521 
0522 static void bcm_iproc_i2c_read_valid_bytes(struct bcm_iproc_i2c_dev *iproc_i2c)
0523 {
0524     struct i2c_msg *msg = iproc_i2c->msg;
0525     uint32_t val;
0526 
0527     /* Read valid data from RX FIFO */
0528     while (iproc_i2c->rx_bytes < msg->len) {
0529         val = iproc_i2c_rd_reg(iproc_i2c, M_RX_OFFSET);
0530 
0531         /* rx fifo empty */
0532         if (!((val >> M_RX_STATUS_SHIFT) & M_RX_STATUS_MASK))
0533             break;
0534 
0535         msg->buf[iproc_i2c->rx_bytes] =
0536             (val >> M_RX_DATA_SHIFT) & M_RX_DATA_MASK;
0537         iproc_i2c->rx_bytes++;
0538     }
0539 }
0540 
0541 static void bcm_iproc_i2c_send(struct bcm_iproc_i2c_dev *iproc_i2c)
0542 {
0543     struct i2c_msg *msg = iproc_i2c->msg;
0544     unsigned int tx_bytes = msg->len - iproc_i2c->tx_bytes;
0545     unsigned int i;
0546     u32 val;
0547 
0548     /* can only fill up to the FIFO size */
0549     tx_bytes = min_t(unsigned int, tx_bytes, M_TX_RX_FIFO_SIZE);
0550     for (i = 0; i < tx_bytes; i++) {
0551         /* start from where we left over */
0552         unsigned int idx = iproc_i2c->tx_bytes + i;
0553 
0554         val = msg->buf[idx];
0555 
0556         /* mark the last byte */
0557         if (idx == msg->len - 1) {
0558             val |= BIT(M_TX_WR_STATUS_SHIFT);
0559 
0560             if (iproc_i2c->irq) {
0561                 u32 tmp;
0562 
0563                 /*
0564                  * Since this is the last byte, we should now
0565                  * disable TX FIFO underrun interrupt
0566                  */
0567                 tmp = iproc_i2c_rd_reg(iproc_i2c, IE_OFFSET);
0568                 tmp &= ~BIT(IE_M_TX_UNDERRUN_SHIFT);
0569                 iproc_i2c_wr_reg(iproc_i2c, IE_OFFSET,
0570                          tmp);
0571             }
0572         }
0573 
0574         /* load data into TX FIFO */
0575         iproc_i2c_wr_reg(iproc_i2c, M_TX_OFFSET, val);
0576     }
0577 
0578     /* update number of transferred bytes */
0579     iproc_i2c->tx_bytes += tx_bytes;
0580 }
0581 
0582 static void bcm_iproc_i2c_read(struct bcm_iproc_i2c_dev *iproc_i2c)
0583 {
0584     struct i2c_msg *msg = iproc_i2c->msg;
0585     u32 bytes_left, val;
0586 
0587     bcm_iproc_i2c_read_valid_bytes(iproc_i2c);
0588     bytes_left = msg->len - iproc_i2c->rx_bytes;
0589     if (bytes_left == 0) {
0590         if (iproc_i2c->irq) {
0591             /* finished reading all data, disable rx thld event */
0592             val = iproc_i2c_rd_reg(iproc_i2c, IE_OFFSET);
0593             val &= ~BIT(IS_M_RX_THLD_SHIFT);
0594             iproc_i2c_wr_reg(iproc_i2c, IE_OFFSET, val);
0595         }
0596     } else if (bytes_left < iproc_i2c->thld_bytes) {
0597         /* set bytes left as threshold */
0598         val = iproc_i2c_rd_reg(iproc_i2c, M_FIFO_CTRL_OFFSET);
0599         val &= ~(M_FIFO_RX_THLD_MASK << M_FIFO_RX_THLD_SHIFT);
0600         val |= (bytes_left << M_FIFO_RX_THLD_SHIFT);
0601         iproc_i2c_wr_reg(iproc_i2c, M_FIFO_CTRL_OFFSET, val);
0602         iproc_i2c->thld_bytes = bytes_left;
0603     }
0604     /*
0605      * bytes_left >= iproc_i2c->thld_bytes,
0606      * hence no need to change the THRESHOLD SET.
0607      * It will remain as iproc_i2c->thld_bytes itself
0608      */
0609 }
0610 
0611 static void bcm_iproc_i2c_process_m_event(struct bcm_iproc_i2c_dev *iproc_i2c,
0612                       u32 status)
0613 {
0614     /* TX FIFO is empty and we have more data to send */
0615     if (status & BIT(IS_M_TX_UNDERRUN_SHIFT))
0616         bcm_iproc_i2c_send(iproc_i2c);
0617 
0618     /* RX FIFO threshold is reached and data needs to be read out */
0619     if (status & BIT(IS_M_RX_THLD_SHIFT))
0620         bcm_iproc_i2c_read(iproc_i2c);
0621 
0622     /* transfer is done */
0623     if (status & BIT(IS_M_START_BUSY_SHIFT)) {
0624         iproc_i2c->xfer_is_done = 1;
0625         if (iproc_i2c->irq)
0626             complete(&iproc_i2c->done);
0627     }
0628 }
0629 
0630 static irqreturn_t bcm_iproc_i2c_isr(int irq, void *data)
0631 {
0632     struct bcm_iproc_i2c_dev *iproc_i2c = data;
0633     u32 slave_status;
0634     u32 status;
0635     bool ret;
0636 
0637     status = iproc_i2c_rd_reg(iproc_i2c, IS_OFFSET);
0638     /* process only slave interrupt which are enabled */
0639     slave_status = status & iproc_i2c_rd_reg(iproc_i2c, IE_OFFSET) &
0640                ISR_MASK_SLAVE;
0641 
0642     if (slave_status) {
0643         ret = bcm_iproc_i2c_slave_isr(iproc_i2c, slave_status);
0644         if (ret)
0645             return IRQ_HANDLED;
0646         else
0647             return IRQ_NONE;
0648     }
0649 
0650     status &= ISR_MASK;
0651     if (!status)
0652         return IRQ_NONE;
0653 
0654     /* process all master based events */
0655     bcm_iproc_i2c_process_m_event(iproc_i2c, status);
0656     iproc_i2c_wr_reg(iproc_i2c, IS_OFFSET, status);
0657 
0658     return IRQ_HANDLED;
0659 }
0660 
0661 static int bcm_iproc_i2c_init(struct bcm_iproc_i2c_dev *iproc_i2c)
0662 {
0663     u32 val;
0664 
0665     /* put controller in reset */
0666     val = iproc_i2c_rd_reg(iproc_i2c, CFG_OFFSET);
0667     val |= BIT(CFG_RESET_SHIFT);
0668     val &= ~(BIT(CFG_EN_SHIFT));
0669     iproc_i2c_wr_reg(iproc_i2c, CFG_OFFSET, val);
0670 
0671     /* wait 100 usec per spec */
0672     udelay(100);
0673 
0674     /* bring controller out of reset */
0675     val &= ~(BIT(CFG_RESET_SHIFT));
0676     iproc_i2c_wr_reg(iproc_i2c, CFG_OFFSET, val);
0677 
0678     /* flush TX/RX FIFOs and set RX FIFO threshold to zero */
0679     val = (BIT(M_FIFO_RX_FLUSH_SHIFT) | BIT(M_FIFO_TX_FLUSH_SHIFT));
0680     iproc_i2c_wr_reg(iproc_i2c, M_FIFO_CTRL_OFFSET, val);
0681     /* disable all interrupts */
0682     val = iproc_i2c_rd_reg(iproc_i2c, IE_OFFSET);
0683     val &= ~(IE_M_ALL_INTERRUPT_MASK <<
0684             IE_M_ALL_INTERRUPT_SHIFT);
0685     iproc_i2c_wr_reg(iproc_i2c, IE_OFFSET, val);
0686 
0687     /* clear all pending interrupts */
0688     iproc_i2c_wr_reg(iproc_i2c, IS_OFFSET, 0xffffffff);
0689 
0690     return 0;
0691 }
0692 
0693 static void bcm_iproc_i2c_enable_disable(struct bcm_iproc_i2c_dev *iproc_i2c,
0694                      bool enable)
0695 {
0696     u32 val;
0697 
0698     val = iproc_i2c_rd_reg(iproc_i2c, CFG_OFFSET);
0699     if (enable)
0700         val |= BIT(CFG_EN_SHIFT);
0701     else
0702         val &= ~BIT(CFG_EN_SHIFT);
0703     iproc_i2c_wr_reg(iproc_i2c, CFG_OFFSET, val);
0704 }
0705 
0706 static int bcm_iproc_i2c_check_status(struct bcm_iproc_i2c_dev *iproc_i2c,
0707                       struct i2c_msg *msg)
0708 {
0709     u32 val;
0710 
0711     val = iproc_i2c_rd_reg(iproc_i2c, M_CMD_OFFSET);
0712     val = (val >> M_CMD_STATUS_SHIFT) & M_CMD_STATUS_MASK;
0713 
0714     switch (val) {
0715     case M_CMD_STATUS_SUCCESS:
0716         return 0;
0717 
0718     case M_CMD_STATUS_LOST_ARB:
0719         dev_dbg(iproc_i2c->device, "lost bus arbitration\n");
0720         return -EAGAIN;
0721 
0722     case M_CMD_STATUS_NACK_ADDR:
0723         dev_dbg(iproc_i2c->device, "NAK addr:0x%02x\n", msg->addr);
0724         return -ENXIO;
0725 
0726     case M_CMD_STATUS_NACK_DATA:
0727         dev_dbg(iproc_i2c->device, "NAK data\n");
0728         return -ENXIO;
0729 
0730     case M_CMD_STATUS_TIMEOUT:
0731         dev_dbg(iproc_i2c->device, "bus timeout\n");
0732         return -ETIMEDOUT;
0733 
0734     case M_CMD_STATUS_FIFO_UNDERRUN:
0735         dev_dbg(iproc_i2c->device, "FIFO under-run\n");
0736         return -ENXIO;
0737 
0738     case M_CMD_STATUS_RX_FIFO_FULL:
0739         dev_dbg(iproc_i2c->device, "RX FIFO full\n");
0740         return -ETIMEDOUT;
0741 
0742     default:
0743         dev_dbg(iproc_i2c->device, "unknown error code=%d\n", val);
0744 
0745         /* re-initialize i2c for recovery */
0746         bcm_iproc_i2c_enable_disable(iproc_i2c, false);
0747         bcm_iproc_i2c_init(iproc_i2c);
0748         bcm_iproc_i2c_enable_disable(iproc_i2c, true);
0749 
0750         return -EIO;
0751     }
0752 }
0753 
0754 static int bcm_iproc_i2c_xfer_wait(struct bcm_iproc_i2c_dev *iproc_i2c,
0755                    struct i2c_msg *msg,
0756                    u32 cmd)
0757 {
0758     unsigned long time_left = msecs_to_jiffies(I2C_TIMEOUT_MSEC);
0759     u32 val, status;
0760     int ret;
0761 
0762     iproc_i2c_wr_reg(iproc_i2c, M_CMD_OFFSET, cmd);
0763 
0764     if (iproc_i2c->irq) {
0765         time_left = wait_for_completion_timeout(&iproc_i2c->done,
0766                             time_left);
0767         /* disable all interrupts */
0768         iproc_i2c_wr_reg(iproc_i2c, IE_OFFSET, 0);
0769         /* read it back to flush the write */
0770         iproc_i2c_rd_reg(iproc_i2c, IE_OFFSET);
0771         /* make sure the interrupt handler isn't running */
0772         synchronize_irq(iproc_i2c->irq);
0773 
0774     } else { /* polling mode */
0775         unsigned long timeout = jiffies + time_left;
0776 
0777         do {
0778             status = iproc_i2c_rd_reg(iproc_i2c,
0779                           IS_OFFSET) & ISR_MASK;
0780             bcm_iproc_i2c_process_m_event(iproc_i2c, status);
0781             iproc_i2c_wr_reg(iproc_i2c, IS_OFFSET, status);
0782 
0783             if (time_after(jiffies, timeout)) {
0784                 time_left = 0;
0785                 break;
0786             }
0787 
0788             cpu_relax();
0789             cond_resched();
0790         } while (!iproc_i2c->xfer_is_done);
0791     }
0792 
0793     if (!time_left && !iproc_i2c->xfer_is_done) {
0794         dev_err(iproc_i2c->device, "transaction timed out\n");
0795 
0796         /* flush both TX/RX FIFOs */
0797         val = BIT(M_FIFO_RX_FLUSH_SHIFT) | BIT(M_FIFO_TX_FLUSH_SHIFT);
0798         iproc_i2c_wr_reg(iproc_i2c, M_FIFO_CTRL_OFFSET, val);
0799         return -ETIMEDOUT;
0800     }
0801 
0802     ret = bcm_iproc_i2c_check_status(iproc_i2c, msg);
0803     if (ret) {
0804         /* flush both TX/RX FIFOs */
0805         val = BIT(M_FIFO_RX_FLUSH_SHIFT) | BIT(M_FIFO_TX_FLUSH_SHIFT);
0806         iproc_i2c_wr_reg(iproc_i2c, M_FIFO_CTRL_OFFSET, val);
0807         return ret;
0808     }
0809 
0810     return 0;
0811 }
0812 
0813 /*
0814  * If 'process_call' is true, then this is a multi-msg transfer that requires
0815  * a repeated start between the messages.
0816  * More specifically, it must be a write (reg) followed by a read (data).
0817  * The i2c quirks are set to enforce this rule.
0818  */
0819 static int bcm_iproc_i2c_xfer_internal(struct bcm_iproc_i2c_dev *iproc_i2c,
0820                     struct i2c_msg *msgs, bool process_call)
0821 {
0822     int i;
0823     u8 addr;
0824     u32 val, tmp, val_intr_en;
0825     unsigned int tx_bytes;
0826     struct i2c_msg *msg = &msgs[0];
0827 
0828     /* check if bus is busy */
0829     if (!!(iproc_i2c_rd_reg(iproc_i2c,
0830                 M_CMD_OFFSET) & BIT(M_CMD_START_BUSY_SHIFT))) {
0831         dev_warn(iproc_i2c->device, "bus is busy\n");
0832         return -EBUSY;
0833     }
0834 
0835     iproc_i2c->msg = msg;
0836 
0837     /* format and load slave address into the TX FIFO */
0838     addr = i2c_8bit_addr_from_msg(msg);
0839     iproc_i2c_wr_reg(iproc_i2c, M_TX_OFFSET, addr);
0840 
0841     /*
0842      * For a write transaction, load data into the TX FIFO. Only allow
0843      * loading up to TX FIFO size - 1 bytes of data since the first byte
0844      * has been used up by the slave address
0845      */
0846     tx_bytes = min_t(unsigned int, msg->len, M_TX_RX_FIFO_SIZE - 1);
0847     if (!(msg->flags & I2C_M_RD)) {
0848         for (i = 0; i < tx_bytes; i++) {
0849             val = msg->buf[i];
0850 
0851             /* mark the last byte */
0852             if (!process_call && (i == msg->len - 1))
0853                 val |= BIT(M_TX_WR_STATUS_SHIFT);
0854 
0855             iproc_i2c_wr_reg(iproc_i2c, M_TX_OFFSET, val);
0856         }
0857         iproc_i2c->tx_bytes = tx_bytes;
0858     }
0859 
0860     /* Process the read message if this is process call */
0861     if (process_call) {
0862         msg++;
0863         iproc_i2c->msg = msg;  /* point to second msg */
0864 
0865         /*
0866          * The last byte to be sent out should be a slave
0867          * address with read operation
0868          */
0869         addr = i2c_8bit_addr_from_msg(msg);
0870         /* mark it the last byte out */
0871         val = addr | BIT(M_TX_WR_STATUS_SHIFT);
0872         iproc_i2c_wr_reg(iproc_i2c, M_TX_OFFSET, val);
0873     }
0874 
0875     /* mark as incomplete before starting the transaction */
0876     if (iproc_i2c->irq)
0877         reinit_completion(&iproc_i2c->done);
0878 
0879     iproc_i2c->xfer_is_done = 0;
0880 
0881     /*
0882      * Enable the "start busy" interrupt, which will be triggered after the
0883      * transaction is done, i.e., the internal start_busy bit, transitions
0884      * from 1 to 0.
0885      */
0886     val_intr_en = BIT(IE_M_START_BUSY_SHIFT);
0887 
0888     /*
0889      * If TX data size is larger than the TX FIFO, need to enable TX
0890      * underrun interrupt, which will be triggerred when the TX FIFO is
0891      * empty. When that happens we can then pump more data into the FIFO
0892      */
0893     if (!process_call && !(msg->flags & I2C_M_RD) &&
0894         msg->len > iproc_i2c->tx_bytes)
0895         val_intr_en |= BIT(IE_M_TX_UNDERRUN_SHIFT);
0896 
0897     /*
0898      * Now we can activate the transfer. For a read operation, specify the
0899      * number of bytes to read
0900      */
0901     val = BIT(M_CMD_START_BUSY_SHIFT);
0902 
0903     if (msg->len == 0) {
0904         /* SMBUS QUICK Command (Read/Write) */
0905         val |= (M_CMD_PROTOCOL_QUICK << M_CMD_PROTOCOL_SHIFT);
0906     } else if (msg->flags & I2C_M_RD) {
0907         u32 protocol;
0908 
0909         iproc_i2c->rx_bytes = 0;
0910         if (msg->len > M_RX_FIFO_MAX_THLD_VALUE)
0911             iproc_i2c->thld_bytes = M_RX_FIFO_THLD_VALUE;
0912         else
0913             iproc_i2c->thld_bytes = msg->len;
0914 
0915         /* set threshold value */
0916         tmp = iproc_i2c_rd_reg(iproc_i2c, M_FIFO_CTRL_OFFSET);
0917         tmp &= ~(M_FIFO_RX_THLD_MASK << M_FIFO_RX_THLD_SHIFT);
0918         tmp |= iproc_i2c->thld_bytes << M_FIFO_RX_THLD_SHIFT;
0919         iproc_i2c_wr_reg(iproc_i2c, M_FIFO_CTRL_OFFSET, tmp);
0920 
0921         /* enable the RX threshold interrupt */
0922         val_intr_en |= BIT(IE_M_RX_THLD_SHIFT);
0923 
0924         protocol = process_call ?
0925                 M_CMD_PROTOCOL_PROCESS : M_CMD_PROTOCOL_BLK_RD;
0926 
0927         val |= (protocol << M_CMD_PROTOCOL_SHIFT) |
0928                (msg->len << M_CMD_RD_CNT_SHIFT);
0929     } else {
0930         val |= (M_CMD_PROTOCOL_BLK_WR << M_CMD_PROTOCOL_SHIFT);
0931     }
0932 
0933     if (iproc_i2c->irq)
0934         iproc_i2c_wr_reg(iproc_i2c, IE_OFFSET, val_intr_en);
0935 
0936     return bcm_iproc_i2c_xfer_wait(iproc_i2c, msg, val);
0937 }
0938 
0939 static int bcm_iproc_i2c_xfer(struct i2c_adapter *adapter,
0940                   struct i2c_msg msgs[], int num)
0941 {
0942     struct bcm_iproc_i2c_dev *iproc_i2c = i2c_get_adapdata(adapter);
0943     bool process_call = false;
0944     int ret;
0945 
0946     if (num == 2) {
0947         /* Repeated start, use process call */
0948         process_call = true;
0949         if (msgs[1].flags & I2C_M_NOSTART) {
0950             dev_err(iproc_i2c->device, "Invalid repeated start\n");
0951             return -EOPNOTSUPP;
0952         }
0953     }
0954 
0955     ret = bcm_iproc_i2c_xfer_internal(iproc_i2c, msgs, process_call);
0956     if (ret) {
0957         dev_dbg(iproc_i2c->device, "xfer failed\n");
0958         return ret;
0959     }
0960 
0961     return num;
0962 }
0963 
0964 static uint32_t bcm_iproc_i2c_functionality(struct i2c_adapter *adap)
0965 {
0966     u32 val;
0967 
0968     val = I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
0969 
0970     if (adap->algo->reg_slave)
0971         val |= I2C_FUNC_SLAVE;
0972 
0973     return val;
0974 }
0975 
0976 static struct i2c_algorithm bcm_iproc_algo = {
0977     .master_xfer = bcm_iproc_i2c_xfer,
0978     .functionality = bcm_iproc_i2c_functionality,
0979     .reg_slave = bcm_iproc_i2c_reg_slave,
0980     .unreg_slave = bcm_iproc_i2c_unreg_slave,
0981 };
0982 
0983 static const struct i2c_adapter_quirks bcm_iproc_i2c_quirks = {
0984     .flags = I2C_AQ_COMB_WRITE_THEN_READ,
0985     .max_comb_1st_msg_len = M_TX_RX_FIFO_SIZE,
0986     .max_read_len = M_RX_MAX_READ_LEN,
0987 };
0988 
0989 static int bcm_iproc_i2c_cfg_speed(struct bcm_iproc_i2c_dev *iproc_i2c)
0990 {
0991     unsigned int bus_speed;
0992     u32 val;
0993     int ret = of_property_read_u32(iproc_i2c->device->of_node,
0994                        "clock-frequency", &bus_speed);
0995     if (ret < 0) {
0996         dev_info(iproc_i2c->device,
0997             "unable to interpret clock-frequency DT property\n");
0998         bus_speed = I2C_MAX_STANDARD_MODE_FREQ;
0999     }
1000 
1001     if (bus_speed < I2C_MAX_STANDARD_MODE_FREQ) {
1002         dev_err(iproc_i2c->device, "%d Hz bus speed not supported\n",
1003             bus_speed);
1004         dev_err(iproc_i2c->device,
1005             "valid speeds are 100khz and 400khz\n");
1006         return -EINVAL;
1007     } else if (bus_speed < I2C_MAX_FAST_MODE_FREQ) {
1008         bus_speed = I2C_MAX_STANDARD_MODE_FREQ;
1009     } else {
1010         bus_speed = I2C_MAX_FAST_MODE_FREQ;
1011     }
1012 
1013     iproc_i2c->bus_speed = bus_speed;
1014     val = iproc_i2c_rd_reg(iproc_i2c, TIM_CFG_OFFSET);
1015     val &= ~BIT(TIM_CFG_MODE_400_SHIFT);
1016     val |= (bus_speed == I2C_MAX_FAST_MODE_FREQ) << TIM_CFG_MODE_400_SHIFT;
1017     iproc_i2c_wr_reg(iproc_i2c, TIM_CFG_OFFSET, val);
1018 
1019     dev_info(iproc_i2c->device, "bus set to %u Hz\n", bus_speed);
1020 
1021     return 0;
1022 }
1023 
1024 static int bcm_iproc_i2c_probe(struct platform_device *pdev)
1025 {
1026     int irq, ret = 0;
1027     struct bcm_iproc_i2c_dev *iproc_i2c;
1028     struct i2c_adapter *adap;
1029     struct resource *res;
1030 
1031     iproc_i2c = devm_kzalloc(&pdev->dev, sizeof(*iproc_i2c),
1032                  GFP_KERNEL);
1033     if (!iproc_i2c)
1034         return -ENOMEM;
1035 
1036     platform_set_drvdata(pdev, iproc_i2c);
1037     iproc_i2c->device = &pdev->dev;
1038     iproc_i2c->type =
1039         (enum bcm_iproc_i2c_type)of_device_get_match_data(&pdev->dev);
1040     init_completion(&iproc_i2c->done);
1041 
1042     res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1043     iproc_i2c->base = devm_ioremap_resource(iproc_i2c->device, res);
1044     if (IS_ERR(iproc_i2c->base))
1045         return PTR_ERR(iproc_i2c->base);
1046 
1047     if (iproc_i2c->type == IPROC_I2C_NIC) {
1048         res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1049         iproc_i2c->idm_base = devm_ioremap_resource(iproc_i2c->device,
1050                                 res);
1051         if (IS_ERR(iproc_i2c->idm_base))
1052             return PTR_ERR(iproc_i2c->idm_base);
1053 
1054         ret = of_property_read_u32(iproc_i2c->device->of_node,
1055                        "brcm,ape-hsls-addr-mask",
1056                        &iproc_i2c->ape_addr_mask);
1057         if (ret < 0) {
1058             dev_err(iproc_i2c->device,
1059                 "'brcm,ape-hsls-addr-mask' missing\n");
1060             return -EINVAL;
1061         }
1062 
1063         spin_lock_init(&iproc_i2c->idm_lock);
1064 
1065         /* no slave support */
1066         bcm_iproc_algo.reg_slave = NULL;
1067         bcm_iproc_algo.unreg_slave = NULL;
1068     }
1069 
1070     ret = bcm_iproc_i2c_init(iproc_i2c);
1071     if (ret)
1072         return ret;
1073 
1074     ret = bcm_iproc_i2c_cfg_speed(iproc_i2c);
1075     if (ret)
1076         return ret;
1077 
1078     irq = platform_get_irq(pdev, 0);
1079     if (irq > 0) {
1080         ret = devm_request_irq(iproc_i2c->device, irq,
1081                        bcm_iproc_i2c_isr, 0, pdev->name,
1082                        iproc_i2c);
1083         if (ret < 0) {
1084             dev_err(iproc_i2c->device,
1085                 "unable to request irq %i\n", irq);
1086             return ret;
1087         }
1088 
1089         iproc_i2c->irq = irq;
1090     } else {
1091         dev_warn(iproc_i2c->device,
1092              "no irq resource, falling back to poll mode\n");
1093     }
1094 
1095     bcm_iproc_i2c_enable_disable(iproc_i2c, true);
1096 
1097     adap = &iproc_i2c->adapter;
1098     i2c_set_adapdata(adap, iproc_i2c);
1099     snprintf(adap->name, sizeof(adap->name),
1100         "Broadcom iProc (%s)",
1101         of_node_full_name(iproc_i2c->device->of_node));
1102     adap->algo = &bcm_iproc_algo;
1103     adap->quirks = &bcm_iproc_i2c_quirks;
1104     adap->dev.parent = &pdev->dev;
1105     adap->dev.of_node = pdev->dev.of_node;
1106 
1107     return i2c_add_adapter(adap);
1108 }
1109 
1110 static int bcm_iproc_i2c_remove(struct platform_device *pdev)
1111 {
1112     struct bcm_iproc_i2c_dev *iproc_i2c = platform_get_drvdata(pdev);
1113 
1114     if (iproc_i2c->irq) {
1115         /*
1116          * Make sure there's no pending interrupt when we remove the
1117          * adapter
1118          */
1119         iproc_i2c_wr_reg(iproc_i2c, IE_OFFSET, 0);
1120         iproc_i2c_rd_reg(iproc_i2c, IE_OFFSET);
1121         synchronize_irq(iproc_i2c->irq);
1122     }
1123 
1124     i2c_del_adapter(&iproc_i2c->adapter);
1125     bcm_iproc_i2c_enable_disable(iproc_i2c, false);
1126 
1127     return 0;
1128 }
1129 
1130 #ifdef CONFIG_PM_SLEEP
1131 
1132 static int bcm_iproc_i2c_suspend(struct device *dev)
1133 {
1134     struct bcm_iproc_i2c_dev *iproc_i2c = dev_get_drvdata(dev);
1135 
1136     if (iproc_i2c->irq) {
1137         /*
1138          * Make sure there's no pending interrupt when we go into
1139          * suspend
1140          */
1141         iproc_i2c_wr_reg(iproc_i2c, IE_OFFSET, 0);
1142         iproc_i2c_rd_reg(iproc_i2c, IE_OFFSET);
1143         synchronize_irq(iproc_i2c->irq);
1144     }
1145 
1146     /* now disable the controller */
1147     bcm_iproc_i2c_enable_disable(iproc_i2c, false);
1148 
1149     return 0;
1150 }
1151 
1152 static int bcm_iproc_i2c_resume(struct device *dev)
1153 {
1154     struct bcm_iproc_i2c_dev *iproc_i2c = dev_get_drvdata(dev);
1155     int ret;
1156     u32 val;
1157 
1158     /*
1159      * Power domain could have been shut off completely in system deep
1160      * sleep, so re-initialize the block here
1161      */
1162     ret = bcm_iproc_i2c_init(iproc_i2c);
1163     if (ret)
1164         return ret;
1165 
1166     /* configure to the desired bus speed */
1167     val = iproc_i2c_rd_reg(iproc_i2c, TIM_CFG_OFFSET);
1168     val &= ~BIT(TIM_CFG_MODE_400_SHIFT);
1169     val |= (iproc_i2c->bus_speed == I2C_MAX_FAST_MODE_FREQ) << TIM_CFG_MODE_400_SHIFT;
1170     iproc_i2c_wr_reg(iproc_i2c, TIM_CFG_OFFSET, val);
1171 
1172     bcm_iproc_i2c_enable_disable(iproc_i2c, true);
1173 
1174     return 0;
1175 }
1176 
1177 static const struct dev_pm_ops bcm_iproc_i2c_pm_ops = {
1178     .suspend_late = &bcm_iproc_i2c_suspend,
1179     .resume_early = &bcm_iproc_i2c_resume
1180 };
1181 
1182 #define BCM_IPROC_I2C_PM_OPS (&bcm_iproc_i2c_pm_ops)
1183 #else
1184 #define BCM_IPROC_I2C_PM_OPS NULL
1185 #endif /* CONFIG_PM_SLEEP */
1186 
1187 
1188 static int bcm_iproc_i2c_reg_slave(struct i2c_client *slave)
1189 {
1190     struct bcm_iproc_i2c_dev *iproc_i2c = i2c_get_adapdata(slave->adapter);
1191 
1192     if (iproc_i2c->slave)
1193         return -EBUSY;
1194 
1195     if (slave->flags & I2C_CLIENT_TEN)
1196         return -EAFNOSUPPORT;
1197 
1198     iproc_i2c->slave = slave;
1199 
1200     tasklet_init(&iproc_i2c->slave_rx_tasklet, slave_rx_tasklet_fn,
1201              (unsigned long)iproc_i2c);
1202 
1203     bcm_iproc_i2c_slave_init(iproc_i2c, false);
1204     return 0;
1205 }
1206 
1207 static int bcm_iproc_i2c_unreg_slave(struct i2c_client *slave)
1208 {
1209     u32 tmp;
1210     struct bcm_iproc_i2c_dev *iproc_i2c = i2c_get_adapdata(slave->adapter);
1211 
1212     if (!iproc_i2c->slave)
1213         return -EINVAL;
1214 
1215     disable_irq(iproc_i2c->irq);
1216 
1217     tasklet_kill(&iproc_i2c->slave_rx_tasklet);
1218 
1219     /* disable all slave interrupts */
1220     tmp = iproc_i2c_rd_reg(iproc_i2c, IE_OFFSET);
1221     tmp &= ~(IE_S_ALL_INTERRUPT_MASK <<
1222             IE_S_ALL_INTERRUPT_SHIFT);
1223     iproc_i2c_wr_reg(iproc_i2c, IE_OFFSET, tmp);
1224 
1225     /* Erase the slave address programmed */
1226     tmp = iproc_i2c_rd_reg(iproc_i2c, S_CFG_SMBUS_ADDR_OFFSET);
1227     tmp &= ~BIT(S_CFG_EN_NIC_SMB_ADDR3_SHIFT);
1228     iproc_i2c_wr_reg(iproc_i2c, S_CFG_SMBUS_ADDR_OFFSET, tmp);
1229 
1230     /* flush TX/RX FIFOs */
1231     tmp = (BIT(S_FIFO_RX_FLUSH_SHIFT) | BIT(S_FIFO_TX_FLUSH_SHIFT));
1232     iproc_i2c_wr_reg(iproc_i2c, S_FIFO_CTRL_OFFSET, tmp);
1233 
1234     /* clear all pending slave interrupts */
1235     iproc_i2c_wr_reg(iproc_i2c, IS_OFFSET, ISR_MASK_SLAVE);
1236 
1237     iproc_i2c->slave = NULL;
1238 
1239     enable_irq(iproc_i2c->irq);
1240 
1241     return 0;
1242 }
1243 
1244 static const struct of_device_id bcm_iproc_i2c_of_match[] = {
1245     {
1246         .compatible = "brcm,iproc-i2c",
1247         .data = (int *)IPROC_I2C,
1248     }, {
1249         .compatible = "brcm,iproc-nic-i2c",
1250         .data = (int *)IPROC_I2C_NIC,
1251     },
1252     { /* sentinel */ }
1253 };
1254 MODULE_DEVICE_TABLE(of, bcm_iproc_i2c_of_match);
1255 
1256 static struct platform_driver bcm_iproc_i2c_driver = {
1257     .driver = {
1258         .name = "bcm-iproc-i2c",
1259         .of_match_table = bcm_iproc_i2c_of_match,
1260         .pm = BCM_IPROC_I2C_PM_OPS,
1261     },
1262     .probe = bcm_iproc_i2c_probe,
1263     .remove = bcm_iproc_i2c_remove,
1264 };
1265 module_platform_driver(bcm_iproc_i2c_driver);
1266 
1267 MODULE_AUTHOR("Ray Jui <rjui@broadcom.com>");
1268 MODULE_DESCRIPTION("Broadcom iProc I2C Driver");
1269 MODULE_LICENSE("GPL v2");