linux/drivers/i2c/busses/i2c-ismt.c
<<
>>
Prefs
   1/*
   2 * This file is provided under a dual BSD/GPLv2 license.  When using or
   3 * redistributing this file, you may do so under either license.
   4 *
   5 * Copyright(c) 2012 Intel Corporation. All rights reserved.
   6 *
   7 * GPL LICENSE SUMMARY
   8 *
   9 * This program is free software; you can redistribute it and/or modify
  10 * it under the terms of version 2 of the GNU General Public License as
  11 * published by the Free Software Foundation.
  12 *
  13 * This program is distributed in the hope that it will be useful, but
  14 * WITHOUT ANY WARRANTY; without even the implied warranty of
  15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16 * General Public License for more details.
  17 * The full GNU General Public License is included in this distribution
  18 * in the file called LICENSE.GPL.
  19 *
  20 * BSD LICENSE
  21 *
  22 * Redistribution and use in source and binary forms, with or without
  23 * modification, are permitted provided that the following conditions
  24 * are met:
  25 *
  26 *   * Redistributions of source code must retain the above copyright
  27 *     notice, this list of conditions and the following disclaimer.
  28 *   * Redistributions in binary form must reproduce the above copyright
  29 *     notice, this list of conditions and the following disclaimer in
  30 *     the documentation and/or other materials provided with the
  31 *     distribution.
  32 *   * Neither the name of Intel Corporation nor the names of its
  33 *     contributors may be used to endorse or promote products derived
  34 *     from this software without specific prior written permission.
  35 *
  36 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  37 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  38 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  39 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  40 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  41 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  42 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  43 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  44 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  45 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  46 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  47 */
  48
  49/*
  50 *  Supports the SMBus Message Transport (SMT) in the Intel Atom Processor
  51 *  S12xx Product Family.
  52 *
  53 *  Features supported by this driver:
  54 *  Hardware PEC                     yes
  55 *  Block buffer                     yes
  56 *  Block process call transaction   no
  57 *  Slave mode                       no
  58 */
  59
  60#include <linux/module.h>
  61#include <linux/pci.h>
  62#include <linux/kernel.h>
  63#include <linux/stddef.h>
  64#include <linux/completion.h>
  65#include <linux/dma-mapping.h>
  66#include <linux/i2c.h>
  67#include <linux/acpi.h>
  68#include <linux/interrupt.h>
  69
  70#include <linux/io-64-nonatomic-lo-hi.h>
  71
  72/* PCI Address Constants */
  73#define SMBBAR          0
  74
  75/* PCI DIDs for the Intel SMBus Message Transport (SMT) Devices */
  76#define PCI_DEVICE_ID_INTEL_S1200_SMT0  0x0c59
  77#define PCI_DEVICE_ID_INTEL_S1200_SMT1  0x0c5a
  78#define PCI_DEVICE_ID_INTEL_CDF_SMT     0x18ac
  79#define PCI_DEVICE_ID_INTEL_DNV_SMT     0x19ac
  80#define PCI_DEVICE_ID_INTEL_EBG_SMT     0x1bff
  81#define PCI_DEVICE_ID_INTEL_AVOTON_SMT  0x1f15
  82
  83#define ISMT_DESC_ENTRIES       2       /* number of descriptor entries */
  84#define ISMT_MAX_RETRIES        3       /* number of SMBus retries to attempt */
  85
  86/* Hardware Descriptor Constants - Control Field */
  87#define ISMT_DESC_CWRL  0x01    /* Command/Write Length */
  88#define ISMT_DESC_BLK   0X04    /* Perform Block Transaction */
  89#define ISMT_DESC_FAIR  0x08    /* Set fairness flag upon successful arbit. */
  90#define ISMT_DESC_PEC   0x10    /* Packet Error Code */
  91#define ISMT_DESC_I2C   0x20    /* I2C Enable */
  92#define ISMT_DESC_INT   0x40    /* Interrupt */
  93#define ISMT_DESC_SOE   0x80    /* Stop On Error */
  94
  95/* Hardware Descriptor Constants - Status Field */
  96#define ISMT_DESC_SCS   0x01    /* Success */
  97#define ISMT_DESC_DLTO  0x04    /* Data Low Time Out */
  98#define ISMT_DESC_NAK   0x08    /* NAK Received */
  99#define ISMT_DESC_CRC   0x10    /* CRC Error */
 100#define ISMT_DESC_CLTO  0x20    /* Clock Low Time Out */
 101#define ISMT_DESC_COL   0x40    /* Collisions */
 102#define ISMT_DESC_LPR   0x80    /* Large Packet Received */
 103
 104/* Macros */
 105#define ISMT_DESC_ADDR_RW(addr, rw) (((addr) << 1) | (rw))
 106
 107/* iSMT General Register address offsets (SMBBAR + <addr>) */
 108#define ISMT_GR_GCTRL           0x000   /* General Control */
 109#define ISMT_GR_SMTICL          0x008   /* SMT Interrupt Cause Location */
 110#define ISMT_GR_ERRINTMSK       0x010   /* Error Interrupt Mask */
 111#define ISMT_GR_ERRAERMSK       0x014   /* Error AER Mask */
 112#define ISMT_GR_ERRSTS          0x018   /* Error Status */
 113#define ISMT_GR_ERRINFO         0x01c   /* Error Information */
 114
 115/* iSMT Master Registers */
 116#define ISMT_MSTR_MDBA          0x100   /* Master Descriptor Base Address */
 117#define ISMT_MSTR_MCTRL         0x108   /* Master Control */
 118#define ISMT_MSTR_MSTS          0x10c   /* Master Status */
 119#define ISMT_MSTR_MDS           0x110   /* Master Descriptor Size */
 120#define ISMT_MSTR_RPOLICY       0x114   /* Retry Policy */
 121
 122/* iSMT Miscellaneous Registers */
 123#define ISMT_SPGT       0x300   /* SMBus PHY Global Timing */
 124
 125/* General Control Register (GCTRL) bit definitions */
 126#define ISMT_GCTRL_TRST 0x04    /* Target Reset */
 127#define ISMT_GCTRL_KILL 0x08    /* Kill */
 128#define ISMT_GCTRL_SRST 0x40    /* Soft Reset */
 129
 130/* Master Control Register (MCTRL) bit definitions */
 131#define ISMT_MCTRL_SS   0x01            /* Start/Stop */
 132#define ISMT_MCTRL_MEIE 0x10            /* Master Error Interrupt Enable */
 133#define ISMT_MCTRL_FMHP 0x00ff0000      /* Firmware Master Head Ptr (FMHP) */
 134
 135/* Master Status Register (MSTS) bit definitions */
 136#define ISMT_MSTS_HMTP  0xff0000        /* HW Master Tail Pointer (HMTP) */
 137#define ISMT_MSTS_MIS   0x20            /* Master Interrupt Status (MIS) */
 138#define ISMT_MSTS_MEIS  0x10            /* Master Error Int Status (MEIS) */
 139#define ISMT_MSTS_IP    0x01            /* In Progress */
 140
 141/* Master Descriptor Size (MDS) bit definitions */
 142#define ISMT_MDS_MASK   0xff    /* Master Descriptor Size mask (MDS) */
 143
 144/* SMBus PHY Global Timing Register (SPGT) bit definitions */
 145#define ISMT_SPGT_SPD_MASK      0xc0000000      /* SMBus Speed mask */
 146#define ISMT_SPGT_SPD_80K       0x00            /* 80 kHz */
 147#define ISMT_SPGT_SPD_100K      (0x1 << 30)     /* 100 kHz */
 148#define ISMT_SPGT_SPD_400K      (0x2 << 30)     /* 400 kHz */
 149#define ISMT_SPGT_SPD_1M        (0x3 << 30)     /* 1 MHz */
 150
 151
 152/* MSI Control Register (MSICTL) bit definitions */
 153#define ISMT_MSICTL_MSIE        0x01    /* MSI Enable */
 154
 155/* iSMT Hardware Descriptor */
 156struct ismt_desc {
 157        u8 tgtaddr_rw;  /* target address & r/w bit */
 158        u8 wr_len_cmd;  /* write length in bytes or a command */
 159        u8 rd_len;      /* read length */
 160        u8 control;     /* control bits */
 161        u8 status;      /* status bits */
 162        u8 retry;       /* collision retry and retry count */
 163        u8 rxbytes;     /* received bytes */
 164        u8 txbytes;     /* transmitted bytes */
 165        u32 dptr_low;   /* lower 32 bit of the data pointer */
 166        u32 dptr_high;  /* upper 32 bit of the data pointer */
 167} __packed;
 168
 169struct ismt_priv {
 170        struct i2c_adapter adapter;
 171        void __iomem *smba;                     /* PCI BAR */
 172        struct pci_dev *pci_dev;
 173        struct ismt_desc *hw;                   /* descriptor virt base addr */
 174        dma_addr_t io_rng_dma;                  /* descriptor HW base addr */
 175        u8 head;                                /* ring buffer head pointer */
 176        struct completion cmp;                  /* interrupt completion */
 177        u8 buffer[I2C_SMBUS_BLOCK_MAX + 16];    /* temp R/W data buffer */
 178};
 179
 180/**
 181 * ismt_ids - PCI device IDs supported by this driver
 182 */
 183static const struct pci_device_id ismt_ids[] = {
 184        { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_S1200_SMT0) },
 185        { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_S1200_SMT1) },
 186        { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_CDF_SMT) },
 187        { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_DNV_SMT) },
 188        { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_EBG_SMT) },
 189        { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_AVOTON_SMT) },
 190        { 0, }
 191};
 192
 193MODULE_DEVICE_TABLE(pci, ismt_ids);
 194
 195/* Bus speed control bits for slow debuggers - refer to the docs for usage */
 196static unsigned int bus_speed;
 197module_param(bus_speed, uint, S_IRUGO);
 198MODULE_PARM_DESC(bus_speed, "Bus Speed in kHz (0 = BIOS default)");
 199
 200/**
 201 * __ismt_desc_dump() - dump the contents of a specific descriptor
 202 */
 203static void __ismt_desc_dump(struct device *dev, const struct ismt_desc *desc)
 204{
 205
 206        dev_dbg(dev, "Descriptor struct:  %p\n", desc);
 207        dev_dbg(dev, "\ttgtaddr_rw=0x%02X\n", desc->tgtaddr_rw);
 208        dev_dbg(dev, "\twr_len_cmd=0x%02X\n", desc->wr_len_cmd);
 209        dev_dbg(dev, "\trd_len=    0x%02X\n", desc->rd_len);
 210        dev_dbg(dev, "\tcontrol=   0x%02X\n", desc->control);
 211        dev_dbg(dev, "\tstatus=    0x%02X\n", desc->status);
 212        dev_dbg(dev, "\tretry=     0x%02X\n", desc->retry);
 213        dev_dbg(dev, "\trxbytes=   0x%02X\n", desc->rxbytes);
 214        dev_dbg(dev, "\ttxbytes=   0x%02X\n", desc->txbytes);
 215        dev_dbg(dev, "\tdptr_low=  0x%08X\n", desc->dptr_low);
 216        dev_dbg(dev, "\tdptr_high= 0x%08X\n", desc->dptr_high);
 217}
 218/**
 219 * ismt_desc_dump() - dump the contents of a descriptor for debug purposes
 220 * @priv: iSMT private data
 221 */
 222static void ismt_desc_dump(struct ismt_priv *priv)
 223{
 224        struct device *dev = &priv->pci_dev->dev;
 225        struct ismt_desc *desc = &priv->hw[priv->head];
 226
 227        dev_dbg(dev, "Dump of the descriptor struct:  0x%X\n", priv->head);
 228        __ismt_desc_dump(dev, desc);
 229}
 230
 231/**
 232 * ismt_gen_reg_dump() - dump the iSMT General Registers
 233 * @priv: iSMT private data
 234 */
 235static void ismt_gen_reg_dump(struct ismt_priv *priv)
 236{
 237        struct device *dev = &priv->pci_dev->dev;
 238
 239        dev_dbg(dev, "Dump of the iSMT General Registers\n");
 240        dev_dbg(dev, "  GCTRL.... : (0x%p)=0x%X\n",
 241                priv->smba + ISMT_GR_GCTRL,
 242                readl(priv->smba + ISMT_GR_GCTRL));
 243        dev_dbg(dev, "  SMTICL... : (0x%p)=0x%016llX\n",
 244                priv->smba + ISMT_GR_SMTICL,
 245                (long long unsigned int)readq(priv->smba + ISMT_GR_SMTICL));
 246        dev_dbg(dev, "  ERRINTMSK : (0x%p)=0x%X\n",
 247                priv->smba + ISMT_GR_ERRINTMSK,
 248                readl(priv->smba + ISMT_GR_ERRINTMSK));
 249        dev_dbg(dev, "  ERRAERMSK : (0x%p)=0x%X\n",
 250                priv->smba + ISMT_GR_ERRAERMSK,
 251                readl(priv->smba + ISMT_GR_ERRAERMSK));
 252        dev_dbg(dev, "  ERRSTS... : (0x%p)=0x%X\n",
 253                priv->smba + ISMT_GR_ERRSTS,
 254                readl(priv->smba + ISMT_GR_ERRSTS));
 255        dev_dbg(dev, "  ERRINFO.. : (0x%p)=0x%X\n",
 256                priv->smba + ISMT_GR_ERRINFO,
 257                readl(priv->smba + ISMT_GR_ERRINFO));
 258}
 259
 260/**
 261 * ismt_mstr_reg_dump() - dump the iSMT Master Registers
 262 * @priv: iSMT private data
 263 */
 264static void ismt_mstr_reg_dump(struct ismt_priv *priv)
 265{
 266        struct device *dev = &priv->pci_dev->dev;
 267
 268        dev_dbg(dev, "Dump of the iSMT Master Registers\n");
 269        dev_dbg(dev, "  MDBA..... : (0x%p)=0x%016llX\n",
 270                priv->smba + ISMT_MSTR_MDBA,
 271                (long long unsigned int)readq(priv->smba + ISMT_MSTR_MDBA));
 272        dev_dbg(dev, "  MCTRL.... : (0x%p)=0x%X\n",
 273                priv->smba + ISMT_MSTR_MCTRL,
 274                readl(priv->smba + ISMT_MSTR_MCTRL));
 275        dev_dbg(dev, "  MSTS..... : (0x%p)=0x%X\n",
 276                priv->smba + ISMT_MSTR_MSTS,
 277                readl(priv->smba + ISMT_MSTR_MSTS));
 278        dev_dbg(dev, "  MDS...... : (0x%p)=0x%X\n",
 279                priv->smba + ISMT_MSTR_MDS,
 280                readl(priv->smba + ISMT_MSTR_MDS));
 281        dev_dbg(dev, "  RPOLICY.. : (0x%p)=0x%X\n",
 282                priv->smba + ISMT_MSTR_RPOLICY,
 283                readl(priv->smba + ISMT_MSTR_RPOLICY));
 284        dev_dbg(dev, "  SPGT..... : (0x%p)=0x%X\n",
 285                priv->smba + ISMT_SPGT,
 286                readl(priv->smba + ISMT_SPGT));
 287}
 288
 289/**
 290 * ismt_submit_desc() - add a descriptor to the ring
 291 * @priv: iSMT private data
 292 */
 293static void ismt_submit_desc(struct ismt_priv *priv)
 294{
 295        uint fmhp;
 296        uint val;
 297
 298        ismt_desc_dump(priv);
 299        ismt_gen_reg_dump(priv);
 300        ismt_mstr_reg_dump(priv);
 301
 302        /* Set the FMHP (Firmware Master Head Pointer)*/
 303        fmhp = ((priv->head + 1) % ISMT_DESC_ENTRIES) << 16;
 304        val = readl(priv->smba + ISMT_MSTR_MCTRL);
 305        writel((val & ~ISMT_MCTRL_FMHP) | fmhp,
 306               priv->smba + ISMT_MSTR_MCTRL);
 307
 308        /* Set the start bit */
 309        val = readl(priv->smba + ISMT_MSTR_MCTRL);
 310        writel(val | ISMT_MCTRL_SS,
 311               priv->smba + ISMT_MSTR_MCTRL);
 312}
 313
 314/**
 315 * ismt_process_desc() - handle the completion of the descriptor
 316 * @desc: the iSMT hardware descriptor
 317 * @data: data buffer from the upper layer
 318 * @priv: ismt_priv struct holding our dma buffer
 319 * @size: SMBus transaction type
 320 * @read_write: flag to indicate if this is a read or write
 321 */
 322static int ismt_process_desc(const struct ismt_desc *desc,
 323                             union i2c_smbus_data *data,
 324                             struct ismt_priv *priv, int size,
 325                             char read_write)
 326{
 327        u8 *dma_buffer = PTR_ALIGN(&priv->buffer[0], 16);
 328
 329        dev_dbg(&priv->pci_dev->dev, "Processing completed descriptor\n");
 330        __ismt_desc_dump(&priv->pci_dev->dev, desc);
 331        ismt_gen_reg_dump(priv);
 332        ismt_mstr_reg_dump(priv);
 333
 334        if (desc->status & ISMT_DESC_SCS) {
 335                if (read_write == I2C_SMBUS_WRITE &&
 336                    size != I2C_SMBUS_PROC_CALL)
 337                        return 0;
 338
 339                switch (size) {
 340                case I2C_SMBUS_BYTE:
 341                case I2C_SMBUS_BYTE_DATA:
 342                        data->byte = dma_buffer[0];
 343                        break;
 344                case I2C_SMBUS_WORD_DATA:
 345                case I2C_SMBUS_PROC_CALL:
 346                        data->word = dma_buffer[0] | (dma_buffer[1] << 8);
 347                        break;
 348                case I2C_SMBUS_BLOCK_DATA:
 349                        if (desc->rxbytes != dma_buffer[0] + 1)
 350                                return -EMSGSIZE;
 351
 352                        memcpy(data->block, dma_buffer, desc->rxbytes);
 353                        break;
 354                case I2C_SMBUS_I2C_BLOCK_DATA:
 355                        memcpy(&data->block[1], dma_buffer, desc->rxbytes);
 356                        data->block[0] = desc->rxbytes;
 357                        break;
 358                }
 359                return 0;
 360        }
 361
 362        if (likely(desc->status & ISMT_DESC_NAK))
 363                return -ENXIO;
 364
 365        if (desc->status & ISMT_DESC_CRC)
 366                return -EBADMSG;
 367
 368        if (desc->status & ISMT_DESC_COL)
 369                return -EAGAIN;
 370
 371        if (desc->status & ISMT_DESC_LPR)
 372                return -EPROTO;
 373
 374        if (desc->status & (ISMT_DESC_DLTO | ISMT_DESC_CLTO))
 375                return -ETIMEDOUT;
 376
 377        return -EIO;
 378}
 379
 380/**
 381 * ismt_access() - process an SMBus command
 382 * @adap: the i2c host adapter
 383 * @addr: address of the i2c/SMBus target
 384 * @flags: command options
 385 * @read_write: read from or write to device
 386 * @command: the i2c/SMBus command to issue
 387 * @size: SMBus transaction type
 388 * @data: read/write data buffer
 389 */
 390static int ismt_access(struct i2c_adapter *adap, u16 addr,
 391                       unsigned short flags, char read_write, u8 command,
 392                       int size, union i2c_smbus_data *data)
 393{
 394        int ret;
 395        unsigned long time_left;
 396        dma_addr_t dma_addr = 0; /* address of the data buffer */
 397        u8 dma_size = 0;
 398        enum dma_data_direction dma_direction = 0;
 399        struct ismt_desc *desc;
 400        struct ismt_priv *priv = i2c_get_adapdata(adap);
 401        struct device *dev = &priv->pci_dev->dev;
 402        u8 *dma_buffer = PTR_ALIGN(&priv->buffer[0], 16);
 403
 404        desc = &priv->hw[priv->head];
 405
 406        /* Initialize the DMA buffer */
 407        memset(priv->buffer, 0, sizeof(priv->buffer));
 408
 409        /* Initialize the descriptor */
 410        memset(desc, 0, sizeof(struct ismt_desc));
 411        desc->tgtaddr_rw = ISMT_DESC_ADDR_RW(addr, read_write);
 412
 413        /* Initialize common control bits */
 414        if (likely(pci_dev_msi_enabled(priv->pci_dev)))
 415                desc->control = ISMT_DESC_INT | ISMT_DESC_FAIR;
 416        else
 417                desc->control = ISMT_DESC_FAIR;
 418
 419        if ((flags & I2C_CLIENT_PEC) && (size != I2C_SMBUS_QUICK)
 420            && (size != I2C_SMBUS_I2C_BLOCK_DATA))
 421                desc->control |= ISMT_DESC_PEC;
 422
 423        switch (size) {
 424        case I2C_SMBUS_QUICK:
 425                dev_dbg(dev, "I2C_SMBUS_QUICK\n");
 426                break;
 427
 428        case I2C_SMBUS_BYTE:
 429                if (read_write == I2C_SMBUS_WRITE) {
 430                        /*
 431                         * Send Byte
 432                         * The command field contains the write data
 433                         */
 434                        dev_dbg(dev, "I2C_SMBUS_BYTE:  WRITE\n");
 435                        desc->control |= ISMT_DESC_CWRL;
 436                        desc->wr_len_cmd = command;
 437                } else {
 438                        /* Receive Byte */
 439                        dev_dbg(dev, "I2C_SMBUS_BYTE:  READ\n");
 440                        dma_size = 1;
 441                        dma_direction = DMA_FROM_DEVICE;
 442                        desc->rd_len = 1;
 443                }
 444                break;
 445
 446        case I2C_SMBUS_BYTE_DATA:
 447                if (read_write == I2C_SMBUS_WRITE) {
 448                        /*
 449                         * Write Byte
 450                         * Command plus 1 data byte
 451                         */
 452                        dev_dbg(dev, "I2C_SMBUS_BYTE_DATA:  WRITE\n");
 453                        desc->wr_len_cmd = 2;
 454                        dma_size = 2;
 455                        dma_direction = DMA_TO_DEVICE;
 456                        dma_buffer[0] = command;
 457                        dma_buffer[1] = data->byte;
 458                } else {
 459                        /* Read Byte */
 460                        dev_dbg(dev, "I2C_SMBUS_BYTE_DATA:  READ\n");
 461                        desc->control |= ISMT_DESC_CWRL;
 462                        desc->wr_len_cmd = command;
 463                        desc->rd_len = 1;
 464                        dma_size = 1;
 465                        dma_direction = DMA_FROM_DEVICE;
 466                }
 467                break;
 468
 469        case I2C_SMBUS_WORD_DATA:
 470                if (read_write == I2C_SMBUS_WRITE) {
 471                        /* Write Word */
 472                        dev_dbg(dev, "I2C_SMBUS_WORD_DATA:  WRITE\n");
 473                        desc->wr_len_cmd = 3;
 474                        dma_size = 3;
 475                        dma_direction = DMA_TO_DEVICE;
 476                        dma_buffer[0] = command;
 477                        dma_buffer[1] = data->word & 0xff;
 478                        dma_buffer[2] = data->word >> 8;
 479                } else {
 480                        /* Read Word */
 481                        dev_dbg(dev, "I2C_SMBUS_WORD_DATA:  READ\n");
 482                        desc->wr_len_cmd = command;
 483                        desc->control |= ISMT_DESC_CWRL;
 484                        desc->rd_len = 2;
 485                        dma_size = 2;
 486                        dma_direction = DMA_FROM_DEVICE;
 487                }
 488                break;
 489
 490        case I2C_SMBUS_PROC_CALL:
 491                dev_dbg(dev, "I2C_SMBUS_PROC_CALL\n");
 492                desc->wr_len_cmd = 3;
 493                desc->rd_len = 2;
 494                dma_size = 3;
 495                dma_direction = DMA_BIDIRECTIONAL;
 496                dma_buffer[0] = command;
 497                dma_buffer[1] = data->word & 0xff;
 498                dma_buffer[2] = data->word >> 8;
 499                break;
 500
 501        case I2C_SMBUS_BLOCK_DATA:
 502                if (read_write == I2C_SMBUS_WRITE) {
 503                        /* Block Write */
 504                        dev_dbg(dev, "I2C_SMBUS_BLOCK_DATA:  WRITE\n");
 505                        dma_size = data->block[0] + 1;
 506                        dma_direction = DMA_TO_DEVICE;
 507                        desc->wr_len_cmd = dma_size;
 508                        desc->control |= ISMT_DESC_BLK;
 509                        dma_buffer[0] = command;
 510                        memcpy(&dma_buffer[1], &data->block[1], dma_size - 1);
 511                } else {
 512                        /* Block Read */
 513                        dev_dbg(dev, "I2C_SMBUS_BLOCK_DATA:  READ\n");
 514                        dma_size = I2C_SMBUS_BLOCK_MAX;
 515                        dma_direction = DMA_FROM_DEVICE;
 516                        desc->rd_len = dma_size;
 517                        desc->wr_len_cmd = command;
 518                        desc->control |= (ISMT_DESC_BLK | ISMT_DESC_CWRL);
 519                }
 520                break;
 521
 522        case I2C_SMBUS_I2C_BLOCK_DATA:
 523                /* Make sure the length is valid */
 524                if (data->block[0] < 1)
 525                        data->block[0] = 1;
 526
 527                if (data->block[0] > I2C_SMBUS_BLOCK_MAX)
 528                        data->block[0] = I2C_SMBUS_BLOCK_MAX;
 529
 530                if (read_write == I2C_SMBUS_WRITE) {
 531                        /* i2c Block Write */
 532                        dev_dbg(dev, "I2C_SMBUS_I2C_BLOCK_DATA:  WRITE\n");
 533                        dma_size = data->block[0] + 1;
 534                        dma_direction = DMA_TO_DEVICE;
 535                        desc->wr_len_cmd = dma_size;
 536                        desc->control |= ISMT_DESC_I2C;
 537                        dma_buffer[0] = command;
 538                        memcpy(&dma_buffer[1], &data->block[1], dma_size - 1);
 539                } else {
 540                        /* i2c Block Read */
 541                        dev_dbg(dev, "I2C_SMBUS_I2C_BLOCK_DATA:  READ\n");
 542                        dma_size = data->block[0];
 543                        dma_direction = DMA_FROM_DEVICE;
 544                        desc->rd_len = dma_size;
 545                        desc->wr_len_cmd = command;
 546                        desc->control |= (ISMT_DESC_I2C | ISMT_DESC_CWRL);
 547                        /*
 548                         * Per the "Table 15-15. I2C Commands",
 549                         * in the External Design Specification (EDS),
 550                         * (Document Number: 508084, Revision: 2.0),
 551                         * the _rw bit must be 0
 552                         */
 553                        desc->tgtaddr_rw = ISMT_DESC_ADDR_RW(addr, 0);
 554                }
 555                break;
 556
 557        default:
 558                dev_err(dev, "Unsupported transaction %d\n",
 559                        size);
 560                return -EOPNOTSUPP;
 561        }
 562
 563        /* map the data buffer */
 564        if (dma_size != 0) {
 565                dev_dbg(dev, " dev=%p\n", dev);
 566                dev_dbg(dev, " data=%p\n", data);
 567                dev_dbg(dev, " dma_buffer=%p\n", dma_buffer);
 568                dev_dbg(dev, " dma_size=%d\n", dma_size);
 569                dev_dbg(dev, " dma_direction=%d\n", dma_direction);
 570
 571                dma_addr = dma_map_single(dev,
 572                                      dma_buffer,
 573                                      dma_size,
 574                                      dma_direction);
 575
 576                if (dma_mapping_error(dev, dma_addr)) {
 577                        dev_err(dev, "Error in mapping dma buffer %p\n",
 578                                dma_buffer);
 579                        return -EIO;
 580                }
 581
 582                dev_dbg(dev, " dma_addr = %pad\n", &dma_addr);
 583
 584                desc->dptr_low = lower_32_bits(dma_addr);
 585                desc->dptr_high = upper_32_bits(dma_addr);
 586        }
 587
 588        reinit_completion(&priv->cmp);
 589
 590        /* Add the descriptor */
 591        ismt_submit_desc(priv);
 592
 593        /* Now we wait for interrupt completion, 1s */
 594        time_left = wait_for_completion_timeout(&priv->cmp, HZ*1);
 595
 596        /* unmap the data buffer */
 597        if (dma_size != 0)
 598                dma_unmap_single(dev, dma_addr, dma_size, dma_direction);
 599
 600        if (unlikely(!time_left)) {
 601                dev_err(dev, "completion wait timed out\n");
 602                ret = -ETIMEDOUT;
 603                goto out;
 604        }
 605
 606        /* do any post processing of the descriptor here */
 607        ret = ismt_process_desc(desc, data, priv, size, read_write);
 608
 609out:
 610        /* Update the ring pointer */
 611        priv->head++;
 612        priv->head %= ISMT_DESC_ENTRIES;
 613
 614        return ret;
 615}
 616
 617/**
 618 * ismt_func() - report which i2c commands are supported by this adapter
 619 * @adap: the i2c host adapter
 620 */
 621static u32 ismt_func(struct i2c_adapter *adap)
 622{
 623        return I2C_FUNC_SMBUS_QUICK             |
 624               I2C_FUNC_SMBUS_BYTE              |
 625               I2C_FUNC_SMBUS_BYTE_DATA         |
 626               I2C_FUNC_SMBUS_WORD_DATA         |
 627               I2C_FUNC_SMBUS_PROC_CALL         |
 628               I2C_FUNC_SMBUS_BLOCK_DATA        |
 629               I2C_FUNC_SMBUS_I2C_BLOCK         |
 630               I2C_FUNC_SMBUS_PEC;
 631}
 632
 633/**
 634 * smbus_algorithm - the adapter algorithm and supported functionality
 635 * @smbus_xfer: the adapter algorithm
 636 * @functionality: functionality supported by the adapter
 637 */
 638static const struct i2c_algorithm smbus_algorithm = {
 639        .smbus_xfer     = ismt_access,
 640        .functionality  = ismt_func,
 641};
 642
 643/**
 644 * ismt_handle_isr() - interrupt handler bottom half
 645 * @priv: iSMT private data
 646 */
 647static irqreturn_t ismt_handle_isr(struct ismt_priv *priv)
 648{
 649        complete(&priv->cmp);
 650
 651        return IRQ_HANDLED;
 652}
 653
 654
 655/**
 656 * ismt_do_interrupt() - IRQ interrupt handler
 657 * @vec: interrupt vector
 658 * @data: iSMT private data
 659 */
 660static irqreturn_t ismt_do_interrupt(int vec, void *data)
 661{
 662        u32 val;
 663        struct ismt_priv *priv = data;
 664
 665        /*
 666         * check to see it's our interrupt, return IRQ_NONE if not ours
 667         * since we are sharing interrupt
 668         */
 669        val = readl(priv->smba + ISMT_MSTR_MSTS);
 670
 671        if (!(val & (ISMT_MSTS_MIS | ISMT_MSTS_MEIS)))
 672                return IRQ_NONE;
 673        else
 674                writel(val | ISMT_MSTS_MIS | ISMT_MSTS_MEIS,
 675                       priv->smba + ISMT_MSTR_MSTS);
 676
 677        return ismt_handle_isr(priv);
 678}
 679
 680/**
 681 * ismt_do_msi_interrupt() - MSI interrupt handler
 682 * @vec: interrupt vector
 683 * @data: iSMT private data
 684 */
 685static irqreturn_t ismt_do_msi_interrupt(int vec, void *data)
 686{
 687        return ismt_handle_isr(data);
 688}
 689
 690/**
 691 * ismt_hw_init() - initialize the iSMT hardware
 692 * @priv: iSMT private data
 693 */
 694static void ismt_hw_init(struct ismt_priv *priv)
 695{
 696        u32 val;
 697        struct device *dev = &priv->pci_dev->dev;
 698
 699        /* initialize the Master Descriptor Base Address (MDBA) */
 700        writeq(priv->io_rng_dma, priv->smba + ISMT_MSTR_MDBA);
 701
 702        /* initialize the Master Control Register (MCTRL) */
 703        writel(ISMT_MCTRL_MEIE, priv->smba + ISMT_MSTR_MCTRL);
 704
 705        /* initialize the Master Status Register (MSTS) */
 706        writel(0, priv->smba + ISMT_MSTR_MSTS);
 707
 708        /* initialize the Master Descriptor Size (MDS) */
 709        val = readl(priv->smba + ISMT_MSTR_MDS);
 710        writel((val & ~ISMT_MDS_MASK) | (ISMT_DESC_ENTRIES - 1),
 711                priv->smba + ISMT_MSTR_MDS);
 712
 713        /*
 714         * Set the SMBus speed (could use this for slow HW debuggers)
 715         */
 716
 717        val = readl(priv->smba + ISMT_SPGT);
 718
 719        switch (bus_speed) {
 720        case 0:
 721                break;
 722
 723        case 80:
 724                dev_dbg(dev, "Setting SMBus clock to 80 kHz\n");
 725                writel(((val & ~ISMT_SPGT_SPD_MASK) | ISMT_SPGT_SPD_80K),
 726                        priv->smba + ISMT_SPGT);
 727                break;
 728
 729        case 100:
 730                dev_dbg(dev, "Setting SMBus clock to 100 kHz\n");
 731                writel(((val & ~ISMT_SPGT_SPD_MASK) | ISMT_SPGT_SPD_100K),
 732                        priv->smba + ISMT_SPGT);
 733                break;
 734
 735        case 400:
 736                dev_dbg(dev, "Setting SMBus clock to 400 kHz\n");
 737                writel(((val & ~ISMT_SPGT_SPD_MASK) | ISMT_SPGT_SPD_400K),
 738                        priv->smba + ISMT_SPGT);
 739                break;
 740
 741        case 1000:
 742                dev_dbg(dev, "Setting SMBus clock to 1000 kHz\n");
 743                writel(((val & ~ISMT_SPGT_SPD_MASK) | ISMT_SPGT_SPD_1M),
 744                        priv->smba + ISMT_SPGT);
 745                break;
 746
 747        default:
 748                dev_warn(dev, "Invalid SMBus clock speed, only 0, 80, 100, 400, and 1000 are valid\n");
 749                break;
 750        }
 751
 752        val = readl(priv->smba + ISMT_SPGT);
 753
 754        switch (val & ISMT_SPGT_SPD_MASK) {
 755        case ISMT_SPGT_SPD_80K:
 756                bus_speed = 80;
 757                break;
 758        case ISMT_SPGT_SPD_100K:
 759                bus_speed = 100;
 760                break;
 761        case ISMT_SPGT_SPD_400K:
 762                bus_speed = 400;
 763                break;
 764        case ISMT_SPGT_SPD_1M:
 765                bus_speed = 1000;
 766                break;
 767        }
 768        dev_dbg(dev, "SMBus clock is running at %d kHz\n", bus_speed);
 769}
 770
 771/**
 772 * ismt_dev_init() - initialize the iSMT data structures
 773 * @priv: iSMT private data
 774 */
 775static int ismt_dev_init(struct ismt_priv *priv)
 776{
 777        /* allocate memory for the descriptor */
 778        priv->hw = dmam_alloc_coherent(&priv->pci_dev->dev,
 779                                       (ISMT_DESC_ENTRIES
 780                                               * sizeof(struct ismt_desc)),
 781                                       &priv->io_rng_dma,
 782                                       GFP_KERNEL);
 783        if (!priv->hw)
 784                return -ENOMEM;
 785
 786        priv->head = 0;
 787        init_completion(&priv->cmp);
 788
 789        return 0;
 790}
 791
 792/**
 793 * ismt_int_init() - initialize interrupts
 794 * @priv: iSMT private data
 795 */
 796static int ismt_int_init(struct ismt_priv *priv)
 797{
 798        int err;
 799
 800        /* Try using MSI interrupts */
 801        err = pci_enable_msi(priv->pci_dev);
 802        if (err)
 803                goto intx;
 804
 805        err = devm_request_irq(&priv->pci_dev->dev,
 806                               priv->pci_dev->irq,
 807                               ismt_do_msi_interrupt,
 808                               0,
 809                               "ismt-msi",
 810                               priv);
 811        if (err) {
 812                pci_disable_msi(priv->pci_dev);
 813                goto intx;
 814        }
 815
 816        return 0;
 817
 818        /* Try using legacy interrupts */
 819intx:
 820        dev_warn(&priv->pci_dev->dev,
 821                 "Unable to use MSI interrupts, falling back to legacy\n");
 822
 823        err = devm_request_irq(&priv->pci_dev->dev,
 824                               priv->pci_dev->irq,
 825                               ismt_do_interrupt,
 826                               IRQF_SHARED,
 827                               "ismt-intx",
 828                               priv);
 829        if (err) {
 830                dev_err(&priv->pci_dev->dev, "no usable interrupts\n");
 831                return err;
 832        }
 833
 834        return 0;
 835}
 836
 837static struct pci_driver ismt_driver;
 838
 839/**
 840 * ismt_probe() - probe for iSMT devices
 841 * @pdev: PCI-Express device
 842 * @id: PCI-Express device ID
 843 */
 844static int
 845ismt_probe(struct pci_dev *pdev, const struct pci_device_id *id)
 846{
 847        int err;
 848        struct ismt_priv *priv;
 849        unsigned long start, len;
 850
 851        priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
 852        if (!priv)
 853                return -ENOMEM;
 854
 855        pci_set_drvdata(pdev, priv);
 856
 857        i2c_set_adapdata(&priv->adapter, priv);
 858        priv->adapter.owner = THIS_MODULE;
 859        priv->adapter.class = I2C_CLASS_HWMON;
 860        priv->adapter.algo = &smbus_algorithm;
 861        priv->adapter.dev.parent = &pdev->dev;
 862        ACPI_COMPANION_SET(&priv->adapter.dev, ACPI_COMPANION(&pdev->dev));
 863        priv->adapter.retries = ISMT_MAX_RETRIES;
 864
 865        priv->pci_dev = pdev;
 866
 867        err = pcim_enable_device(pdev);
 868        if (err) {
 869                dev_err(&pdev->dev, "Failed to enable SMBus PCI device (%d)\n",
 870                        err);
 871                return err;
 872        }
 873
 874        /* enable bus mastering */
 875        pci_set_master(pdev);
 876
 877        /* Determine the address of the SMBus area */
 878        start = pci_resource_start(pdev, SMBBAR);
 879        len = pci_resource_len(pdev, SMBBAR);
 880        if (!start || !len) {
 881                dev_err(&pdev->dev,
 882                        "SMBus base address uninitialized, upgrade BIOS\n");
 883                return -ENODEV;
 884        }
 885
 886        snprintf(priv->adapter.name, sizeof(priv->adapter.name),
 887                 "SMBus iSMT adapter at %lx", start);
 888
 889        dev_dbg(&priv->pci_dev->dev, " start=0x%lX\n", start);
 890        dev_dbg(&priv->pci_dev->dev, " len=0x%lX\n", len);
 891
 892        err = acpi_check_resource_conflict(&pdev->resource[SMBBAR]);
 893        if (err) {
 894                dev_err(&pdev->dev, "ACPI resource conflict!\n");
 895                return err;
 896        }
 897
 898        err = pci_request_region(pdev, SMBBAR, ismt_driver.name);
 899        if (err) {
 900                dev_err(&pdev->dev,
 901                        "Failed to request SMBus region 0x%lx-0x%lx\n",
 902                        start, start + len);
 903                return err;
 904        }
 905
 906        priv->smba = pcim_iomap(pdev, SMBBAR, len);
 907        if (!priv->smba) {
 908                dev_err(&pdev->dev, "Unable to ioremap SMBus BAR\n");
 909                return -ENODEV;
 910        }
 911
 912        if ((pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0) ||
 913            (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64)) != 0)) {
 914                if ((pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0) ||
 915                    (pci_set_consistent_dma_mask(pdev,
 916                                                 DMA_BIT_MASK(32)) != 0)) {
 917                        dev_err(&pdev->dev, "pci_set_dma_mask fail %p\n",
 918                                pdev);
 919                        return -ENODEV;
 920                }
 921        }
 922
 923        err = ismt_dev_init(priv);
 924        if (err)
 925                return err;
 926
 927        ismt_hw_init(priv);
 928
 929        err = ismt_int_init(priv);
 930        if (err)
 931                return err;
 932
 933        err = i2c_add_adapter(&priv->adapter);
 934        if (err)
 935                return -ENODEV;
 936        return 0;
 937}
 938
 939/**
 940 * ismt_remove() - release driver resources
 941 * @pdev: PCI-Express device
 942 */
 943static void ismt_remove(struct pci_dev *pdev)
 944{
 945        struct ismt_priv *priv = pci_get_drvdata(pdev);
 946
 947        i2c_del_adapter(&priv->adapter);
 948}
 949
 950static struct pci_driver ismt_driver = {
 951        .name = "ismt_smbus",
 952        .id_table = ismt_ids,
 953        .probe = ismt_probe,
 954        .remove = ismt_remove,
 955};
 956
 957module_pci_driver(ismt_driver);
 958
 959MODULE_LICENSE("Dual BSD/GPL");
 960MODULE_AUTHOR("Bill E. Brown <bill.e.brown@intel.com>");
 961MODULE_DESCRIPTION("Intel SMBus Message Transport (iSMT) driver");
 962