linux/drivers/i2c/busses/i2c-cadence.c
<<
>>
Prefs
   1/*
   2 * I2C bus driver for the Cadence I2C controller.
   3 *
   4 * Copyright (C) 2009 - 2014 Xilinx, Inc.
   5 *
   6 * This program is free software; you can redistribute it
   7 * and/or modify it under the terms of the GNU General Public
   8 * License as published by the Free Software Foundation;
   9 * either version 2 of the License, or (at your option) any
  10 * later version.
  11 */
  12
  13#include <linux/clk.h>
  14#include <linux/delay.h>
  15#include <linux/i2c.h>
  16#include <linux/interrupt.h>
  17#include <linux/io.h>
  18#include <linux/module.h>
  19#include <linux/platform_device.h>
  20#include <linux/of.h>
  21#include <linux/pm_runtime.h>
  22
  23/* Register offsets for the I2C device. */
  24#define CDNS_I2C_CR_OFFSET              0x00 /* Control Register, RW */
  25#define CDNS_I2C_SR_OFFSET              0x04 /* Status Register, RO */
  26#define CDNS_I2C_ADDR_OFFSET            0x08 /* I2C Address Register, RW */
  27#define CDNS_I2C_DATA_OFFSET            0x0C /* I2C Data Register, RW */
  28#define CDNS_I2C_ISR_OFFSET             0x10 /* IRQ Status Register, RW */
  29#define CDNS_I2C_XFER_SIZE_OFFSET       0x14 /* Transfer Size Register, RW */
  30#define CDNS_I2C_SLV_PAUSE_OFFSET       0x18 /* Transfer Size Register, RW */
  31#define CDNS_I2C_TIME_OUT_OFFSET        0x1C /* Time Out Register, RW */
  32#define CDNS_I2C_IER_OFFSET             0x24 /* IRQ Enable Register, WO */
  33#define CDNS_I2C_IDR_OFFSET             0x28 /* IRQ Disable Register, WO */
  34
  35/* Control Register Bit mask definitions */
  36#define CDNS_I2C_CR_SLVMON              BIT(5) /* Slave monitor mode bit */
  37#define CDNS_I2C_CR_HOLD                BIT(4) /* Hold Bus bit */
  38#define CDNS_I2C_CR_ACK_EN              BIT(3)
  39#define CDNS_I2C_CR_NEA                 BIT(2)
  40#define CDNS_I2C_CR_MS                  BIT(1)
  41/* Read or Write Master transfer 0 = Transmitter, 1 = Receiver */
  42#define CDNS_I2C_CR_RW                  BIT(0)
  43/* 1 = Auto init FIFO to zeroes */
  44#define CDNS_I2C_CR_CLR_FIFO            BIT(6)
  45#define CDNS_I2C_CR_DIVA_SHIFT          14
  46#define CDNS_I2C_CR_DIVA_MASK           (3 << CDNS_I2C_CR_DIVA_SHIFT)
  47#define CDNS_I2C_CR_DIVB_SHIFT          8
  48#define CDNS_I2C_CR_DIVB_MASK           (0x3f << CDNS_I2C_CR_DIVB_SHIFT)
  49
  50/* Status Register Bit mask definitions */
  51#define CDNS_I2C_SR_BA          BIT(8)
  52#define CDNS_I2C_SR_RXDV        BIT(5)
  53
  54/*
  55 * I2C Address Register Bit mask definitions
  56 * Normal addressing mode uses [6:0] bits. Extended addressing mode uses [9:0]
  57 * bits. A write access to this register always initiates a transfer if the I2C
  58 * is in master mode.
  59 */
  60#define CDNS_I2C_ADDR_MASK      0x000003FF /* I2C Address Mask */
  61
  62/*
  63 * I2C Interrupt Registers Bit mask definitions
  64 * All the four interrupt registers (Status/Mask/Enable/Disable) have the same
  65 * bit definitions.
  66 */
  67#define CDNS_I2C_IXR_ARB_LOST           BIT(9)
  68#define CDNS_I2C_IXR_RX_UNF             BIT(7)
  69#define CDNS_I2C_IXR_TX_OVF             BIT(6)
  70#define CDNS_I2C_IXR_RX_OVF             BIT(5)
  71#define CDNS_I2C_IXR_SLV_RDY            BIT(4)
  72#define CDNS_I2C_IXR_TO                 BIT(3)
  73#define CDNS_I2C_IXR_NACK               BIT(2)
  74#define CDNS_I2C_IXR_DATA               BIT(1)
  75#define CDNS_I2C_IXR_COMP               BIT(0)
  76
  77#define CDNS_I2C_IXR_ALL_INTR_MASK      (CDNS_I2C_IXR_ARB_LOST | \
  78                                         CDNS_I2C_IXR_RX_UNF | \
  79                                         CDNS_I2C_IXR_TX_OVF | \
  80                                         CDNS_I2C_IXR_RX_OVF | \
  81                                         CDNS_I2C_IXR_SLV_RDY | \
  82                                         CDNS_I2C_IXR_TO | \
  83                                         CDNS_I2C_IXR_NACK | \
  84                                         CDNS_I2C_IXR_DATA | \
  85                                         CDNS_I2C_IXR_COMP)
  86
  87#define CDNS_I2C_IXR_ERR_INTR_MASK      (CDNS_I2C_IXR_ARB_LOST | \
  88                                         CDNS_I2C_IXR_RX_UNF | \
  89                                         CDNS_I2C_IXR_TX_OVF | \
  90                                         CDNS_I2C_IXR_RX_OVF | \
  91                                         CDNS_I2C_IXR_NACK)
  92
  93#define CDNS_I2C_ENABLED_INTR_MASK      (CDNS_I2C_IXR_ARB_LOST | \
  94                                         CDNS_I2C_IXR_RX_UNF | \
  95                                         CDNS_I2C_IXR_TX_OVF | \
  96                                         CDNS_I2C_IXR_RX_OVF | \
  97                                         CDNS_I2C_IXR_NACK | \
  98                                         CDNS_I2C_IXR_DATA | \
  99                                         CDNS_I2C_IXR_COMP)
 100
 101#define CDNS_I2C_TIMEOUT                msecs_to_jiffies(1000)
 102/* timeout for pm runtime autosuspend */
 103#define CNDS_I2C_PM_TIMEOUT             1000    /* ms */
 104
 105#define CDNS_I2C_FIFO_DEPTH             16
 106/* FIFO depth at which the DATA interrupt occurs */
 107#define CDNS_I2C_DATA_INTR_DEPTH        (CDNS_I2C_FIFO_DEPTH - 2)
 108#define CDNS_I2C_MAX_TRANSFER_SIZE      255
 109/* Transfer size in multiples of data interrupt depth */
 110#define CDNS_I2C_TRANSFER_SIZE  (CDNS_I2C_MAX_TRANSFER_SIZE - 3)
 111
 112#define DRIVER_NAME             "cdns-i2c"
 113
 114#define CDNS_I2C_SPEED_MAX      400000
 115#define CDNS_I2C_SPEED_DEFAULT  100000
 116
 117#define CDNS_I2C_DIVA_MAX       4
 118#define CDNS_I2C_DIVB_MAX       64
 119
 120#define CDNS_I2C_TIMEOUT_MAX    0xFF
 121
 122#define CDNS_I2C_BROKEN_HOLD_BIT        BIT(0)
 123
 124#define cdns_i2c_readreg(offset)       readl_relaxed(id->membase + offset)
 125#define cdns_i2c_writereg(val, offset) writel_relaxed(val, id->membase + offset)
 126
 127/**
 128 * struct cdns_i2c - I2C device private data structure
 129 *
 130 * @dev:                Pointer to device structure
 131 * @membase:            Base address of the I2C device
 132 * @adap:               I2C adapter instance
 133 * @p_msg:              Message pointer
 134 * @err_status:         Error status in Interrupt Status Register
 135 * @xfer_done:          Transfer complete status
 136 * @p_send_buf:         Pointer to transmit buffer
 137 * @p_recv_buf:         Pointer to receive buffer
 138 * @send_count:         Number of bytes still expected to send
 139 * @recv_count:         Number of bytes still expected to receive
 140 * @curr_recv_count:    Number of bytes to be received in current transfer
 141 * @irq:                IRQ number
 142 * @input_clk:          Input clock to I2C controller
 143 * @i2c_clk:            Maximum I2C clock speed
 144 * @bus_hold_flag:      Flag used in repeated start for clearing HOLD bit
 145 * @clk:                Pointer to struct clk
 146 * @clk_rate_change_nb: Notifier block for clock rate changes
 147 * @quirks:             flag for broken hold bit usage in r1p10
 148 * @ctrl_reg:           Cached value of the control register.
 149 */
 150struct cdns_i2c {
 151        struct device           *dev;
 152        void __iomem *membase;
 153        struct i2c_adapter adap;
 154        struct i2c_msg *p_msg;
 155        int err_status;
 156        struct completion xfer_done;
 157        unsigned char *p_send_buf;
 158        unsigned char *p_recv_buf;
 159        unsigned int send_count;
 160        unsigned int recv_count;
 161        unsigned int curr_recv_count;
 162        int irq;
 163        unsigned long input_clk;
 164        unsigned int i2c_clk;
 165        unsigned int bus_hold_flag;
 166        struct clk *clk;
 167        struct notifier_block clk_rate_change_nb;
 168        u32 quirks;
 169        u32 ctrl_reg;
 170};
 171
 172struct cdns_platform_data {
 173        u32 quirks;
 174};
 175
 176#define to_cdns_i2c(_nb)        container_of(_nb, struct cdns_i2c, \
 177                                             clk_rate_change_nb)
 178
 179/**
 180 * cdns_i2c_clear_bus_hold - Clear bus hold bit
 181 * @id: Pointer to driver data struct
 182 *
 183 * Helper to clear the controller's bus hold bit.
 184 */
 185static void cdns_i2c_clear_bus_hold(struct cdns_i2c *id)
 186{
 187        u32 reg = cdns_i2c_readreg(CDNS_I2C_CR_OFFSET);
 188        if (reg & CDNS_I2C_CR_HOLD)
 189                cdns_i2c_writereg(reg & ~CDNS_I2C_CR_HOLD, CDNS_I2C_CR_OFFSET);
 190}
 191
 192static inline bool cdns_is_holdquirk(struct cdns_i2c *id, bool hold_wrkaround)
 193{
 194        return (hold_wrkaround &&
 195                (id->curr_recv_count == CDNS_I2C_FIFO_DEPTH + 1));
 196}
 197
 198/**
 199 * cdns_i2c_isr - Interrupt handler for the I2C device
 200 * @irq:        irq number for the I2C device
 201 * @ptr:        void pointer to cdns_i2c structure
 202 *
 203 * This function handles the data interrupt, transfer complete interrupt and
 204 * the error interrupts of the I2C device.
 205 *
 206 * Return: IRQ_HANDLED always
 207 */
 208static irqreturn_t cdns_i2c_isr(int irq, void *ptr)
 209{
 210        unsigned int isr_status, avail_bytes, updatetx;
 211        unsigned int bytes_to_send;
 212        bool hold_quirk;
 213        struct cdns_i2c *id = ptr;
 214        /* Signal completion only after everything is updated */
 215        int done_flag = 0;
 216        irqreturn_t status = IRQ_NONE;
 217
 218        isr_status = cdns_i2c_readreg(CDNS_I2C_ISR_OFFSET);
 219        cdns_i2c_writereg(isr_status, CDNS_I2C_ISR_OFFSET);
 220
 221        /* Handling nack and arbitration lost interrupt */
 222        if (isr_status & (CDNS_I2C_IXR_NACK | CDNS_I2C_IXR_ARB_LOST)) {
 223                done_flag = 1;
 224                status = IRQ_HANDLED;
 225        }
 226
 227        /*
 228         * Check if transfer size register needs to be updated again for a
 229         * large data receive operation.
 230         */
 231        updatetx = 0;
 232        if (id->recv_count > id->curr_recv_count)
 233                updatetx = 1;
 234
 235        hold_quirk = (id->quirks & CDNS_I2C_BROKEN_HOLD_BIT) && updatetx;
 236
 237        /* When receiving, handle data interrupt and completion interrupt */
 238        if (id->p_recv_buf &&
 239            ((isr_status & CDNS_I2C_IXR_COMP) ||
 240             (isr_status & CDNS_I2C_IXR_DATA))) {
 241                /* Read data if receive data valid is set */
 242                while (cdns_i2c_readreg(CDNS_I2C_SR_OFFSET) &
 243                       CDNS_I2C_SR_RXDV) {
 244                        /*
 245                         * Clear hold bit that was set for FIFO control if
 246                         * RX data left is less than FIFO depth, unless
 247                         * repeated start is selected.
 248                         */
 249                        if ((id->recv_count < CDNS_I2C_FIFO_DEPTH) &&
 250                            !id->bus_hold_flag)
 251                                cdns_i2c_clear_bus_hold(id);
 252
 253                        *(id->p_recv_buf)++ =
 254                                cdns_i2c_readreg(CDNS_I2C_DATA_OFFSET);
 255                        id->recv_count--;
 256                        id->curr_recv_count--;
 257
 258                        if (cdns_is_holdquirk(id, hold_quirk))
 259                                break;
 260                }
 261
 262                /*
 263                 * The controller sends NACK to the slave when transfer size
 264                 * register reaches zero without considering the HOLD bit.
 265                 * This workaround is implemented for large data transfers to
 266                 * maintain transfer size non-zero while performing a large
 267                 * receive operation.
 268                 */
 269                if (cdns_is_holdquirk(id, hold_quirk)) {
 270                        /* wait while fifo is full */
 271                        while (cdns_i2c_readreg(CDNS_I2C_XFER_SIZE_OFFSET) !=
 272                               (id->curr_recv_count - CDNS_I2C_FIFO_DEPTH))
 273                                ;
 274
 275                        /*
 276                         * Check number of bytes to be received against maximum
 277                         * transfer size and update register accordingly.
 278                         */
 279                        if (((int)(id->recv_count) - CDNS_I2C_FIFO_DEPTH) >
 280                            CDNS_I2C_TRANSFER_SIZE) {
 281                                cdns_i2c_writereg(CDNS_I2C_TRANSFER_SIZE,
 282                                                  CDNS_I2C_XFER_SIZE_OFFSET);
 283                                id->curr_recv_count = CDNS_I2C_TRANSFER_SIZE +
 284                                                      CDNS_I2C_FIFO_DEPTH;
 285                        } else {
 286                                cdns_i2c_writereg(id->recv_count -
 287                                                  CDNS_I2C_FIFO_DEPTH,
 288                                                  CDNS_I2C_XFER_SIZE_OFFSET);
 289                                id->curr_recv_count = id->recv_count;
 290                        }
 291                } else if (id->recv_count && !hold_quirk &&
 292                                                !id->curr_recv_count) {
 293
 294                        /* Set the slave address in address register*/
 295                        cdns_i2c_writereg(id->p_msg->addr & CDNS_I2C_ADDR_MASK,
 296                                                CDNS_I2C_ADDR_OFFSET);
 297
 298                        if (id->recv_count > CDNS_I2C_TRANSFER_SIZE) {
 299                                cdns_i2c_writereg(CDNS_I2C_TRANSFER_SIZE,
 300                                                CDNS_I2C_XFER_SIZE_OFFSET);
 301                                id->curr_recv_count = CDNS_I2C_TRANSFER_SIZE;
 302                        } else {
 303                                cdns_i2c_writereg(id->recv_count,
 304                                                CDNS_I2C_XFER_SIZE_OFFSET);
 305                                id->curr_recv_count = id->recv_count;
 306                        }
 307                }
 308
 309                /* Clear hold (if not repeated start) and signal completion */
 310                if ((isr_status & CDNS_I2C_IXR_COMP) && !id->recv_count) {
 311                        if (!id->bus_hold_flag)
 312                                cdns_i2c_clear_bus_hold(id);
 313                        done_flag = 1;
 314                }
 315
 316                status = IRQ_HANDLED;
 317        }
 318
 319        /* When sending, handle transfer complete interrupt */
 320        if ((isr_status & CDNS_I2C_IXR_COMP) && !id->p_recv_buf) {
 321                /*
 322                 * If there is more data to be sent, calculate the
 323                 * space available in FIFO and fill with that many bytes.
 324                 */
 325                if (id->send_count) {
 326                        avail_bytes = CDNS_I2C_FIFO_DEPTH -
 327                            cdns_i2c_readreg(CDNS_I2C_XFER_SIZE_OFFSET);
 328                        if (id->send_count > avail_bytes)
 329                                bytes_to_send = avail_bytes;
 330                        else
 331                                bytes_to_send = id->send_count;
 332
 333                        while (bytes_to_send--) {
 334                                cdns_i2c_writereg(
 335                                        (*(id->p_send_buf)++),
 336                                         CDNS_I2C_DATA_OFFSET);
 337                                id->send_count--;
 338                        }
 339                } else {
 340                        /*
 341                         * Signal the completion of transaction and
 342                         * clear the hold bus bit if there are no
 343                         * further messages to be processed.
 344                         */
 345                        done_flag = 1;
 346                }
 347                if (!id->send_count && !id->bus_hold_flag)
 348                        cdns_i2c_clear_bus_hold(id);
 349
 350                status = IRQ_HANDLED;
 351        }
 352
 353        /* Handling Slave monitor mode interrupt */
 354        if (isr_status & CDNS_I2C_IXR_SLV_RDY) {
 355                unsigned int ctrl_reg;
 356                /* Read control register */
 357                ctrl_reg = cdns_i2c_readreg(CDNS_I2C_CR_OFFSET);
 358
 359                /* Disable slave monitor mode */
 360                ctrl_reg &= ~CDNS_I2C_CR_SLVMON;
 361                cdns_i2c_writereg(ctrl_reg, CDNS_I2C_CR_OFFSET);
 362
 363                /* Clear interrupt flag for slvmon mode */
 364                cdns_i2c_writereg(CDNS_I2C_IXR_SLV_RDY, CDNS_I2C_IDR_OFFSET);
 365
 366                done_flag = 1;
 367                status = IRQ_HANDLED;
 368        }
 369
 370        /* Update the status for errors */
 371        id->err_status = isr_status & CDNS_I2C_IXR_ERR_INTR_MASK;
 372        if (id->err_status)
 373                status = IRQ_HANDLED;
 374
 375        if (done_flag)
 376                complete(&id->xfer_done);
 377
 378        return status;
 379}
 380
 381/**
 382 * cdns_i2c_mrecv - Prepare and start a master receive operation
 383 * @id:         pointer to the i2c device structure
 384 */
 385static void cdns_i2c_mrecv(struct cdns_i2c *id)
 386{
 387        unsigned int ctrl_reg;
 388        unsigned int isr_status;
 389
 390        id->p_recv_buf = id->p_msg->buf;
 391        id->recv_count = id->p_msg->len;
 392
 393        /* Put the controller in master receive mode and clear the FIFO */
 394        ctrl_reg = cdns_i2c_readreg(CDNS_I2C_CR_OFFSET);
 395        ctrl_reg |= CDNS_I2C_CR_RW | CDNS_I2C_CR_CLR_FIFO;
 396
 397        if (id->p_msg->flags & I2C_M_RECV_LEN)
 398                id->recv_count = I2C_SMBUS_BLOCK_MAX + 1;
 399
 400        id->curr_recv_count = id->recv_count;
 401
 402        /*
 403         * Check for the message size against FIFO depth and set the
 404         * 'hold bus' bit if it is greater than FIFO depth.
 405         */
 406        if (id->recv_count > CDNS_I2C_FIFO_DEPTH)
 407                ctrl_reg |= CDNS_I2C_CR_HOLD;
 408
 409        cdns_i2c_writereg(ctrl_reg, CDNS_I2C_CR_OFFSET);
 410
 411        /* Clear the interrupts in interrupt status register */
 412        isr_status = cdns_i2c_readreg(CDNS_I2C_ISR_OFFSET);
 413        cdns_i2c_writereg(isr_status, CDNS_I2C_ISR_OFFSET);
 414
 415        /*
 416         * The no. of bytes to receive is checked against the limit of
 417         * max transfer size. Set transfer size register with no of bytes
 418         * receive if it is less than transfer size and transfer size if
 419         * it is more. Enable the interrupts.
 420         */
 421        if (id->recv_count > CDNS_I2C_TRANSFER_SIZE) {
 422                cdns_i2c_writereg(CDNS_I2C_TRANSFER_SIZE,
 423                                  CDNS_I2C_XFER_SIZE_OFFSET);
 424                id->curr_recv_count = CDNS_I2C_TRANSFER_SIZE;
 425        } else {
 426                cdns_i2c_writereg(id->recv_count, CDNS_I2C_XFER_SIZE_OFFSET);
 427        }
 428
 429        /* Clear the bus hold flag if bytes to receive is less than FIFO size */
 430        if (!id->bus_hold_flag &&
 431                ((id->p_msg->flags & I2C_M_RECV_LEN) != I2C_M_RECV_LEN) &&
 432                (id->recv_count <= CDNS_I2C_FIFO_DEPTH))
 433                        cdns_i2c_clear_bus_hold(id);
 434        /* Set the slave address in address register - triggers operation */
 435        cdns_i2c_writereg(id->p_msg->addr & CDNS_I2C_ADDR_MASK,
 436                                                CDNS_I2C_ADDR_OFFSET);
 437        cdns_i2c_writereg(CDNS_I2C_ENABLED_INTR_MASK, CDNS_I2C_IER_OFFSET);
 438}
 439
 440/**
 441 * cdns_i2c_msend - Prepare and start a master send operation
 442 * @id:         pointer to the i2c device
 443 */
 444static void cdns_i2c_msend(struct cdns_i2c *id)
 445{
 446        unsigned int avail_bytes;
 447        unsigned int bytes_to_send;
 448        unsigned int ctrl_reg;
 449        unsigned int isr_status;
 450
 451        id->p_recv_buf = NULL;
 452        id->p_send_buf = id->p_msg->buf;
 453        id->send_count = id->p_msg->len;
 454
 455        /* Set the controller in Master transmit mode and clear the FIFO. */
 456        ctrl_reg = cdns_i2c_readreg(CDNS_I2C_CR_OFFSET);
 457        ctrl_reg &= ~CDNS_I2C_CR_RW;
 458        ctrl_reg |= CDNS_I2C_CR_CLR_FIFO;
 459
 460        /*
 461         * Check for the message size against FIFO depth and set the
 462         * 'hold bus' bit if it is greater than FIFO depth.
 463         */
 464        if (id->send_count > CDNS_I2C_FIFO_DEPTH)
 465                ctrl_reg |= CDNS_I2C_CR_HOLD;
 466        cdns_i2c_writereg(ctrl_reg, CDNS_I2C_CR_OFFSET);
 467
 468        /* Clear the interrupts in interrupt status register. */
 469        isr_status = cdns_i2c_readreg(CDNS_I2C_ISR_OFFSET);
 470        cdns_i2c_writereg(isr_status, CDNS_I2C_ISR_OFFSET);
 471
 472        /*
 473         * Calculate the space available in FIFO. Check the message length
 474         * against the space available, and fill the FIFO accordingly.
 475         * Enable the interrupts.
 476         */
 477        avail_bytes = CDNS_I2C_FIFO_DEPTH -
 478                                cdns_i2c_readreg(CDNS_I2C_XFER_SIZE_OFFSET);
 479
 480        if (id->send_count > avail_bytes)
 481                bytes_to_send = avail_bytes;
 482        else
 483                bytes_to_send = id->send_count;
 484
 485        while (bytes_to_send--) {
 486                cdns_i2c_writereg((*(id->p_send_buf)++), CDNS_I2C_DATA_OFFSET);
 487                id->send_count--;
 488        }
 489
 490        /*
 491         * Clear the bus hold flag if there is no more data
 492         * and if it is the last message.
 493         */
 494        if (!id->bus_hold_flag && !id->send_count)
 495                cdns_i2c_clear_bus_hold(id);
 496        /* Set the slave address in address register - triggers operation. */
 497        cdns_i2c_writereg(id->p_msg->addr & CDNS_I2C_ADDR_MASK,
 498                                                CDNS_I2C_ADDR_OFFSET);
 499
 500        cdns_i2c_writereg(CDNS_I2C_ENABLED_INTR_MASK, CDNS_I2C_IER_OFFSET);
 501}
 502
 503/**
 504 * cdns_i2c_slvmon - Handling Slav monitor mode feature
 505 * @id:         pointer to the i2c device
 506 */
 507static void cdns_i2c_slvmon(struct cdns_i2c *id)
 508{
 509        unsigned int ctrl_reg;
 510        unsigned int isr_status;
 511
 512        id->p_recv_buf = NULL;
 513        id->p_send_buf = id->p_msg->buf;
 514        id->send_count = id->p_msg->len;
 515
 516        /* Clear the interrupts in interrupt status register. */
 517        isr_status = cdns_i2c_readreg(CDNS_I2C_ISR_OFFSET);
 518        cdns_i2c_writereg(isr_status, CDNS_I2C_ISR_OFFSET);
 519
 520        /* Enable slvmon control reg */
 521        ctrl_reg = cdns_i2c_readreg(CDNS_I2C_CR_OFFSET);
 522        ctrl_reg |=  CDNS_I2C_CR_MS | CDNS_I2C_CR_NEA | CDNS_I2C_CR_SLVMON
 523                        | CDNS_I2C_CR_CLR_FIFO;
 524        ctrl_reg &= ~(CDNS_I2C_CR_RW);
 525        cdns_i2c_writereg(ctrl_reg, CDNS_I2C_CR_OFFSET);
 526
 527        /* Initialize slvmon reg */
 528        cdns_i2c_writereg(0xF, CDNS_I2C_SLV_PAUSE_OFFSET);
 529
 530        /* Set the slave address to start the slave address transmission */
 531        cdns_i2c_writereg(id->p_msg->addr, CDNS_I2C_ADDR_OFFSET);
 532
 533        /* Setup slvmon interrupt flag */
 534        cdns_i2c_writereg(CDNS_I2C_IXR_SLV_RDY, CDNS_I2C_IER_OFFSET);
 535}
 536
 537/**
 538 * cdns_i2c_master_reset - Reset the interface
 539 * @adap:       pointer to the i2c adapter driver instance
 540 *
 541 * This function cleanup the fifos, clear the hold bit and status
 542 * and disable the interrupts.
 543 */
 544static void cdns_i2c_master_reset(struct i2c_adapter *adap)
 545{
 546        struct cdns_i2c *id = adap->algo_data;
 547        u32 regval;
 548
 549        /* Disable the interrupts */
 550        cdns_i2c_writereg(CDNS_I2C_IXR_ALL_INTR_MASK, CDNS_I2C_IDR_OFFSET);
 551        /* Clear the hold bit and fifos */
 552        regval = cdns_i2c_readreg(CDNS_I2C_CR_OFFSET);
 553        regval &= ~(CDNS_I2C_CR_HOLD | CDNS_I2C_CR_SLVMON);
 554        regval |= CDNS_I2C_CR_CLR_FIFO;
 555        cdns_i2c_writereg(regval, CDNS_I2C_CR_OFFSET);
 556        /* Update the transfercount register to zero */
 557        cdns_i2c_writereg(0, CDNS_I2C_XFER_SIZE_OFFSET);
 558        /* Clear the interupt status register */
 559        regval = cdns_i2c_readreg(CDNS_I2C_ISR_OFFSET);
 560        cdns_i2c_writereg(regval, CDNS_I2C_ISR_OFFSET);
 561        /* Clear the status register */
 562        regval = cdns_i2c_readreg(CDNS_I2C_SR_OFFSET);
 563        cdns_i2c_writereg(regval, CDNS_I2C_SR_OFFSET);
 564}
 565
 566static int cdns_i2c_process_msg(struct cdns_i2c *id, struct i2c_msg *msg,
 567                struct i2c_adapter *adap)
 568{
 569        unsigned long time_left;
 570        u32 reg;
 571
 572        id->p_msg = msg;
 573        id->err_status = 0;
 574        reinit_completion(&id->xfer_done);
 575
 576        /* Check for the TEN Bit mode on each msg */
 577        reg = cdns_i2c_readreg(CDNS_I2C_CR_OFFSET);
 578        if (msg->flags & I2C_M_TEN) {
 579                if (reg & CDNS_I2C_CR_NEA)
 580                        cdns_i2c_writereg(reg & ~CDNS_I2C_CR_NEA,
 581                                        CDNS_I2C_CR_OFFSET);
 582        } else {
 583                if (!(reg & CDNS_I2C_CR_NEA))
 584                        cdns_i2c_writereg(reg | CDNS_I2C_CR_NEA,
 585                                        CDNS_I2C_CR_OFFSET);
 586        }
 587        /* Check for zero lenght - Slave monitor mode */
 588        if (msg->len == 0)
 589                cdns_i2c_slvmon(id);
 590         /* Check for the R/W flag on each msg */
 591        else if (msg->flags & I2C_M_RD)
 592                cdns_i2c_mrecv(id);
 593        else
 594                cdns_i2c_msend(id);
 595
 596        /* Wait for the signal of completion */
 597        time_left = wait_for_completion_timeout(&id->xfer_done, adap->timeout);
 598        if (time_left == 0) {
 599                cdns_i2c_master_reset(adap);
 600                dev_err(id->adap.dev.parent,
 601                                "timeout waiting on completion\n");
 602                return -ETIMEDOUT;
 603        }
 604
 605        cdns_i2c_writereg(CDNS_I2C_IXR_ALL_INTR_MASK,
 606                          CDNS_I2C_IDR_OFFSET);
 607
 608        /* If it is bus arbitration error, try again */
 609        if (id->err_status & CDNS_I2C_IXR_ARB_LOST)
 610                return -EAGAIN;
 611
 612        return 0;
 613}
 614
 615/**
 616 * cdns_i2c_master_xfer - The main i2c transfer function
 617 * @adap:       pointer to the i2c adapter driver instance
 618 * @msgs:       pointer to the i2c message structure
 619 * @num:        the number of messages to transfer
 620 *
 621 * Initiates the send/recv activity based on the transfer message received.
 622 *
 623 * Return: number of msgs processed on success, negative error otherwise
 624 */
 625static int cdns_i2c_master_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs,
 626                                int num)
 627{
 628        int ret, count;
 629        u32 reg;
 630        struct cdns_i2c *id = adap->algo_data;
 631        bool hold_quirk;
 632
 633        ret = pm_runtime_get_sync(id->dev);
 634        if (ret < 0)
 635                return ret;
 636        /* Check if the bus is free */
 637        if (msgs->len)
 638                if (cdns_i2c_readreg(CDNS_I2C_SR_OFFSET) & CDNS_I2C_SR_BA) {
 639                        ret = -EAGAIN;
 640                        goto out;
 641                }
 642
 643        hold_quirk = !!(id->quirks & CDNS_I2C_BROKEN_HOLD_BIT);
 644        /*
 645         * Set the flag to one when multiple messages are to be
 646         * processed with a repeated start.
 647         */
 648        if (num > 1) {
 649                /*
 650                 * This controller does not give completion interrupt after a
 651                 * master receive message if HOLD bit is set (repeated start),
 652                 * resulting in SW timeout. Hence, if a receive message is
 653                 * followed by any other message, an error is returned
 654                 * indicating that this sequence is not supported.
 655                 */
 656                for (count = 0; (count < num - 1 && hold_quirk); count++) {
 657                        if (msgs[count].flags & I2C_M_RD) {
 658                                dev_warn(adap->dev.parent,
 659                                         "Can't do repeated start after a receive message\n");
 660                                ret = -EOPNOTSUPP;
 661                                goto out;
 662                        }
 663                }
 664                id->bus_hold_flag = 1;
 665                reg = cdns_i2c_readreg(CDNS_I2C_CR_OFFSET);
 666                reg |= CDNS_I2C_CR_HOLD;
 667                cdns_i2c_writereg(reg, CDNS_I2C_CR_OFFSET);
 668        } else {
 669                id->bus_hold_flag = 0;
 670        }
 671
 672        /* Process the msg one by one */
 673        for (count = 0; count < num; count++, msgs++) {
 674                if (count == (num - 1))
 675                        id->bus_hold_flag = 0;
 676
 677                ret = cdns_i2c_process_msg(id, msgs, adap);
 678                if (ret)
 679                        goto out;
 680
 681                /* Report the other error interrupts to application */
 682                if (id->err_status) {
 683                        cdns_i2c_master_reset(adap);
 684
 685                        if (id->err_status & CDNS_I2C_IXR_NACK) {
 686                                ret = -ENXIO;
 687                                goto out;
 688                        }
 689                        ret = -EIO;
 690                        goto out;
 691                }
 692        }
 693
 694        ret = num;
 695out:
 696        pm_runtime_mark_last_busy(id->dev);
 697        pm_runtime_put_autosuspend(id->dev);
 698        return ret;
 699}
 700
 701/**
 702 * cdns_i2c_func - Returns the supported features of the I2C driver
 703 * @adap:       pointer to the i2c adapter structure
 704 *
 705 * Return: 32 bit value, each bit corresponding to a feature
 706 */
 707static u32 cdns_i2c_func(struct i2c_adapter *adap)
 708{
 709        return I2C_FUNC_I2C | I2C_FUNC_10BIT_ADDR |
 710                (I2C_FUNC_SMBUS_EMUL & ~I2C_FUNC_SMBUS_QUICK) |
 711                I2C_FUNC_SMBUS_BLOCK_DATA;
 712}
 713
 714static const struct i2c_algorithm cdns_i2c_algo = {
 715        .master_xfer    = cdns_i2c_master_xfer,
 716        .functionality  = cdns_i2c_func,
 717};
 718
 719/**
 720 * cdns_i2c_calc_divs - Calculate clock dividers
 721 * @f:          I2C clock frequency
 722 * @input_clk:  Input clock frequency
 723 * @a:          First divider (return value)
 724 * @b:          Second divider (return value)
 725 *
 726 * f is used as input and output variable. As input it is used as target I2C
 727 * frequency. On function exit f holds the actually resulting I2C frequency.
 728 *
 729 * Return: 0 on success, negative errno otherwise.
 730 */
 731static int cdns_i2c_calc_divs(unsigned long *f, unsigned long input_clk,
 732                unsigned int *a, unsigned int *b)
 733{
 734        unsigned long fscl = *f, best_fscl = *f, actual_fscl, temp;
 735        unsigned int div_a, div_b, calc_div_a = 0, calc_div_b = 0;
 736        unsigned int last_error, current_error;
 737
 738        /* calculate (divisor_a+1) x (divisor_b+1) */
 739        temp = input_clk / (22 * fscl);
 740
 741        /*
 742         * If the calculated value is negative or 0, the fscl input is out of
 743         * range. Return error.
 744         */
 745        if (!temp || (temp > (CDNS_I2C_DIVA_MAX * CDNS_I2C_DIVB_MAX)))
 746                return -EINVAL;
 747
 748        last_error = -1;
 749        for (div_a = 0; div_a < CDNS_I2C_DIVA_MAX; div_a++) {
 750                div_b = DIV_ROUND_UP(input_clk, 22 * fscl * (div_a + 1));
 751
 752                if ((div_b < 1) || (div_b > CDNS_I2C_DIVB_MAX))
 753                        continue;
 754                div_b--;
 755
 756                actual_fscl = input_clk / (22 * (div_a + 1) * (div_b + 1));
 757
 758                if (actual_fscl > fscl)
 759                        continue;
 760
 761                current_error = ((actual_fscl > fscl) ? (actual_fscl - fscl) :
 762                                                        (fscl - actual_fscl));
 763
 764                if (last_error > current_error) {
 765                        calc_div_a = div_a;
 766                        calc_div_b = div_b;
 767                        best_fscl = actual_fscl;
 768                        last_error = current_error;
 769                }
 770        }
 771
 772        *a = calc_div_a;
 773        *b = calc_div_b;
 774        *f = best_fscl;
 775
 776        return 0;
 777}
 778
 779/**
 780 * cdns_i2c_setclk - This function sets the serial clock rate for the I2C device
 781 * @clk_in:     I2C clock input frequency in Hz
 782 * @id:         Pointer to the I2C device structure
 783 *
 784 * The device must be idle rather than busy transferring data before setting
 785 * these device options.
 786 * The data rate is set by values in the control register.
 787 * The formula for determining the correct register values is
 788 *      Fscl = Fpclk/(22 x (divisor_a+1) x (divisor_b+1))
 789 * See the hardware data sheet for a full explanation of setting the serial
 790 * clock rate. The clock can not be faster than the input clock divide by 22.
 791 * The two most common clock rates are 100KHz and 400KHz.
 792 *
 793 * Return: 0 on success, negative error otherwise
 794 */
 795static int cdns_i2c_setclk(unsigned long clk_in, struct cdns_i2c *id)
 796{
 797        unsigned int div_a, div_b;
 798        unsigned int ctrl_reg;
 799        int ret = 0;
 800        unsigned long fscl = id->i2c_clk;
 801
 802        ret = cdns_i2c_calc_divs(&fscl, clk_in, &div_a, &div_b);
 803        if (ret)
 804                return ret;
 805
 806        ctrl_reg = id->ctrl_reg;
 807        ctrl_reg &= ~(CDNS_I2C_CR_DIVA_MASK | CDNS_I2C_CR_DIVB_MASK);
 808        ctrl_reg |= ((div_a << CDNS_I2C_CR_DIVA_SHIFT) |
 809                        (div_b << CDNS_I2C_CR_DIVB_SHIFT));
 810        id->ctrl_reg = ctrl_reg;
 811        return 0;
 812}
 813
 814/**
 815 * cdns_i2c_clk_notifier_cb - Clock rate change callback
 816 * @nb:         Pointer to notifier block
 817 * @event:      Notification reason
 818 * @data:       Pointer to notification data object
 819 *
 820 * This function is called when the cdns_i2c input clock frequency changes.
 821 * The callback checks whether a valid bus frequency can be generated after the
 822 * change. If so, the change is acknowledged, otherwise the change is aborted.
 823 * New dividers are written to the HW in the pre- or post change notification
 824 * depending on the scaling direction.
 825 *
 826 * Return:      NOTIFY_STOP if the rate change should be aborted, NOTIFY_OK
 827 *              to acknowedge the change, NOTIFY_DONE if the notification is
 828 *              considered irrelevant.
 829 */
 830static int cdns_i2c_clk_notifier_cb(struct notifier_block *nb, unsigned long
 831                event, void *data)
 832{
 833        struct clk_notifier_data *ndata = data;
 834        struct cdns_i2c *id = to_cdns_i2c(nb);
 835
 836        if (pm_runtime_suspended(id->dev))
 837                return NOTIFY_OK;
 838
 839        switch (event) {
 840        case PRE_RATE_CHANGE:
 841        {
 842                unsigned long input_clk = ndata->new_rate;
 843                unsigned long fscl = id->i2c_clk;
 844                unsigned int div_a, div_b;
 845                int ret;
 846
 847                ret = cdns_i2c_calc_divs(&fscl, input_clk, &div_a, &div_b);
 848                if (ret) {
 849                        dev_warn(id->adap.dev.parent,
 850                                        "clock rate change rejected\n");
 851                        return NOTIFY_STOP;
 852                }
 853
 854                /* scale up */
 855                if (ndata->new_rate > ndata->old_rate)
 856                        cdns_i2c_setclk(ndata->new_rate, id);
 857
 858                return NOTIFY_OK;
 859        }
 860        case POST_RATE_CHANGE:
 861                id->input_clk = ndata->new_rate;
 862                /* scale down */
 863                if (ndata->new_rate < ndata->old_rate)
 864                        cdns_i2c_setclk(ndata->new_rate, id);
 865                return NOTIFY_OK;
 866        case ABORT_RATE_CHANGE:
 867                /* scale up */
 868                if (ndata->new_rate > ndata->old_rate)
 869                        cdns_i2c_setclk(ndata->old_rate, id);
 870                return NOTIFY_OK;
 871        default:
 872                return NOTIFY_DONE;
 873        }
 874}
 875
 876/**
 877 * cdns_i2c_runtime_suspend -  Runtime suspend method for the driver
 878 * @dev:        Address of the platform_device structure
 879 *
 880 * Put the driver into low power mode.
 881 *
 882 * Return: 0 always
 883 */
 884static int __maybe_unused cdns_i2c_runtime_suspend(struct device *dev)
 885{
 886        struct platform_device *pdev = to_platform_device(dev);
 887        struct cdns_i2c *xi2c = platform_get_drvdata(pdev);
 888
 889        clk_disable(xi2c->clk);
 890
 891        return 0;
 892}
 893
 894/**
 895 * cdns_i2c_init -  Controller initialisation
 896 * @id:         Device private data structure
 897 *
 898 * Initialise the i2c controller.
 899 *
 900 */
 901static void cdns_i2c_init(struct cdns_i2c *id)
 902{
 903        cdns_i2c_writereg(id->ctrl_reg, CDNS_I2C_CR_OFFSET);
 904        /*
 905         * Cadence I2C controller has a bug wherein it generates
 906         * invalid read transaction after HW timeout in master receiver mode.
 907         * HW timeout is not used by this driver and the interrupt is disabled.
 908         * But the feature itself cannot be disabled. Hence maximum value
 909         * is written to this register to reduce the chances of error.
 910         */
 911        cdns_i2c_writereg(CDNS_I2C_TIMEOUT_MAX, CDNS_I2C_TIME_OUT_OFFSET);
 912}
 913
 914/**
 915 * cdns_i2c_runtime_resume - Runtime resume
 916 * @dev:        Address of the platform_device structure
 917 *
 918 * Runtime resume callback.
 919 *
 920 * Return: 0 on success and error value on error
 921 */
 922static int __maybe_unused cdns_i2c_runtime_resume(struct device *dev)
 923{
 924        struct platform_device *pdev = to_platform_device(dev);
 925        struct cdns_i2c *xi2c = platform_get_drvdata(pdev);
 926        int ret;
 927
 928        ret = clk_enable(xi2c->clk);
 929        if (ret) {
 930                dev_err(dev, "Cannot enable clock.\n");
 931                return ret;
 932        }
 933        cdns_i2c_init(xi2c);
 934
 935        return 0;
 936}
 937
 938static const struct dev_pm_ops cdns_i2c_dev_pm_ops = {
 939        SET_RUNTIME_PM_OPS(cdns_i2c_runtime_suspend,
 940                           cdns_i2c_runtime_resume, NULL)
 941};
 942
 943static const struct cdns_platform_data r1p10_i2c_def = {
 944        .quirks = CDNS_I2C_BROKEN_HOLD_BIT,
 945};
 946
 947static const struct of_device_id cdns_i2c_of_match[] = {
 948        { .compatible = "cdns,i2c-r1p10", .data = &r1p10_i2c_def },
 949        { .compatible = "cdns,i2c-r1p14",},
 950        { /* end of table */ }
 951};
 952MODULE_DEVICE_TABLE(of, cdns_i2c_of_match);
 953
 954/**
 955 * cdns_i2c_probe - Platform registration call
 956 * @pdev:       Handle to the platform device structure
 957 *
 958 * This function does all the memory allocation and registration for the i2c
 959 * device. User can modify the address mode to 10 bit address mode using the
 960 * ioctl call with option I2C_TENBIT.
 961 *
 962 * Return: 0 on success, negative error otherwise
 963 */
 964static int cdns_i2c_probe(struct platform_device *pdev)
 965{
 966        struct resource *r_mem;
 967        struct cdns_i2c *id;
 968        int ret;
 969        const struct of_device_id *match;
 970
 971        id = devm_kzalloc(&pdev->dev, sizeof(*id), GFP_KERNEL);
 972        if (!id)
 973                return -ENOMEM;
 974
 975        id->dev = &pdev->dev;
 976        platform_set_drvdata(pdev, id);
 977
 978        match = of_match_node(cdns_i2c_of_match, pdev->dev.of_node);
 979        if (match && match->data) {
 980                const struct cdns_platform_data *data = match->data;
 981                id->quirks = data->quirks;
 982        }
 983
 984        r_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
 985        id->membase = devm_ioremap_resource(&pdev->dev, r_mem);
 986        if (IS_ERR(id->membase))
 987                return PTR_ERR(id->membase);
 988
 989        id->irq = platform_get_irq(pdev, 0);
 990
 991        id->adap.owner = THIS_MODULE;
 992        id->adap.dev.of_node = pdev->dev.of_node;
 993        id->adap.algo = &cdns_i2c_algo;
 994        id->adap.timeout = CDNS_I2C_TIMEOUT;
 995        id->adap.retries = 3;           /* Default retry value. */
 996        id->adap.algo_data = id;
 997        id->adap.dev.parent = &pdev->dev;
 998        init_completion(&id->xfer_done);
 999        snprintf(id->adap.name, sizeof(id->adap.name),
1000                 "Cadence I2C at %08lx", (unsigned long)r_mem->start);
1001
1002        id->clk = devm_clk_get(&pdev->dev, NULL);
1003        if (IS_ERR(id->clk)) {
1004                dev_err(&pdev->dev, "input clock not found.\n");
1005                return PTR_ERR(id->clk);
1006        }
1007        ret = clk_prepare_enable(id->clk);
1008        if (ret)
1009                dev_err(&pdev->dev, "Unable to enable clock.\n");
1010
1011        pm_runtime_set_autosuspend_delay(id->dev, CNDS_I2C_PM_TIMEOUT);
1012        pm_runtime_use_autosuspend(id->dev);
1013        pm_runtime_set_active(id->dev);
1014        pm_runtime_enable(id->dev);
1015
1016        id->clk_rate_change_nb.notifier_call = cdns_i2c_clk_notifier_cb;
1017        if (clk_notifier_register(id->clk, &id->clk_rate_change_nb))
1018                dev_warn(&pdev->dev, "Unable to register clock notifier.\n");
1019        id->input_clk = clk_get_rate(id->clk);
1020
1021        ret = of_property_read_u32(pdev->dev.of_node, "clock-frequency",
1022                        &id->i2c_clk);
1023        if (ret || (id->i2c_clk > CDNS_I2C_SPEED_MAX))
1024                id->i2c_clk = CDNS_I2C_SPEED_DEFAULT;
1025
1026        id->ctrl_reg = CDNS_I2C_CR_ACK_EN | CDNS_I2C_CR_NEA | CDNS_I2C_CR_MS;
1027
1028        ret = cdns_i2c_setclk(id->input_clk, id);
1029        if (ret) {
1030                dev_err(&pdev->dev, "invalid SCL clock: %u Hz\n", id->i2c_clk);
1031                ret = -EINVAL;
1032                goto err_clk_dis;
1033        }
1034
1035        ret = devm_request_irq(&pdev->dev, id->irq, cdns_i2c_isr, 0,
1036                                 DRIVER_NAME, id);
1037        if (ret) {
1038                dev_err(&pdev->dev, "cannot get irq %d\n", id->irq);
1039                goto err_clk_dis;
1040        }
1041
1042        ret = i2c_add_adapter(&id->adap);
1043        if (ret < 0) {
1044                dev_err(&pdev->dev, "reg adap failed: %d\n", ret);
1045                goto err_clk_dis;
1046        }
1047        cdns_i2c_init(id);
1048
1049        dev_info(&pdev->dev, "%u kHz mmio %08lx irq %d\n",
1050                 id->i2c_clk / 1000, (unsigned long)r_mem->start, id->irq);
1051
1052        return 0;
1053
1054err_clk_dis:
1055        clk_disable_unprepare(id->clk);
1056        pm_runtime_disable(&pdev->dev);
1057        pm_runtime_set_suspended(&pdev->dev);
1058        return ret;
1059}
1060
1061/**
1062 * cdns_i2c_remove - Unregister the device after releasing the resources
1063 * @pdev:       Handle to the platform device structure
1064 *
1065 * This function frees all the resources allocated to the device.
1066 *
1067 * Return: 0 always
1068 */
1069static int cdns_i2c_remove(struct platform_device *pdev)
1070{
1071        struct cdns_i2c *id = platform_get_drvdata(pdev);
1072
1073        pm_runtime_disable(&pdev->dev);
1074        pm_runtime_set_suspended(&pdev->dev);
1075        pm_runtime_dont_use_autosuspend(&pdev->dev);
1076
1077        i2c_del_adapter(&id->adap);
1078        clk_notifier_unregister(id->clk, &id->clk_rate_change_nb);
1079        clk_disable_unprepare(id->clk);
1080        pm_runtime_disable(&pdev->dev);
1081
1082        return 0;
1083}
1084
1085static struct platform_driver cdns_i2c_drv = {
1086        .driver = {
1087                .name  = DRIVER_NAME,
1088                .of_match_table = cdns_i2c_of_match,
1089                .pm = &cdns_i2c_dev_pm_ops,
1090        },
1091        .probe  = cdns_i2c_probe,
1092        .remove = cdns_i2c_remove,
1093};
1094
1095module_platform_driver(cdns_i2c_drv);
1096
1097MODULE_AUTHOR("Xilinx Inc.");
1098MODULE_DESCRIPTION("Cadence I2C bus driver");
1099MODULE_LICENSE("GPL");
1100