0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060 #include <linux/module.h>
0061 #include <linux/pci.h>
0062 #include <linux/kernel.h>
0063 #include <linux/stddef.h>
0064 #include <linux/completion.h>
0065 #include <linux/dma-mapping.h>
0066 #include <linux/i2c.h>
0067 #include <linux/acpi.h>
0068 #include <linux/interrupt.h>
0069
0070 #include <linux/io-64-nonatomic-lo-hi.h>
0071
0072
0073 #define SMBBAR 0
0074
0075
0076 #define PCI_DEVICE_ID_INTEL_S1200_SMT0 0x0c59
0077 #define PCI_DEVICE_ID_INTEL_S1200_SMT1 0x0c5a
0078 #define PCI_DEVICE_ID_INTEL_CDF_SMT 0x18ac
0079 #define PCI_DEVICE_ID_INTEL_DNV_SMT 0x19ac
0080 #define PCI_DEVICE_ID_INTEL_EBG_SMT 0x1bff
0081 #define PCI_DEVICE_ID_INTEL_AVOTON_SMT 0x1f15
0082
0083 #define ISMT_DESC_ENTRIES 2
0084 #define ISMT_MAX_RETRIES 3
0085 #define ISMT_LOG_ENTRIES 3
0086
0087
0088 #define ISMT_DESC_CWRL 0x01
0089 #define ISMT_DESC_BLK 0X04
0090 #define ISMT_DESC_FAIR 0x08
0091 #define ISMT_DESC_PEC 0x10
0092 #define ISMT_DESC_I2C 0x20
0093 #define ISMT_DESC_INT 0x40
0094 #define ISMT_DESC_SOE 0x80
0095
0096
0097 #define ISMT_DESC_SCS 0x01
0098 #define ISMT_DESC_DLTO 0x04
0099 #define ISMT_DESC_NAK 0x08
0100 #define ISMT_DESC_CRC 0x10
0101 #define ISMT_DESC_CLTO 0x20
0102 #define ISMT_DESC_COL 0x40
0103 #define ISMT_DESC_LPR 0x80
0104
0105
0106 #define ISMT_DESC_ADDR_RW(addr, rw) (((addr) << 1) | (rw))
0107
0108
0109 #define ISMT_GR_GCTRL 0x000
0110 #define ISMT_GR_SMTICL 0x008
0111 #define ISMT_GR_ERRINTMSK 0x010
0112 #define ISMT_GR_ERRAERMSK 0x014
0113 #define ISMT_GR_ERRSTS 0x018
0114 #define ISMT_GR_ERRINFO 0x01c
0115
0116
0117 #define ISMT_MSTR_MDBA 0x100
0118 #define ISMT_MSTR_MCTRL 0x108
0119 #define ISMT_MSTR_MSTS 0x10c
0120 #define ISMT_MSTR_MDS 0x110
0121 #define ISMT_MSTR_RPOLICY 0x114
0122
0123
0124 #define ISMT_SPGT 0x300
0125
0126
0127 #define ISMT_GCTRL_TRST 0x04
0128 #define ISMT_GCTRL_KILL 0x08
0129 #define ISMT_GCTRL_SRST 0x40
0130
0131
0132 #define ISMT_MCTRL_SS 0x01
0133 #define ISMT_MCTRL_MEIE 0x10
0134 #define ISMT_MCTRL_FMHP 0x00ff0000
0135
0136
0137 #define ISMT_MSTS_HMTP 0xff0000
0138 #define ISMT_MSTS_MIS 0x20
0139 #define ISMT_MSTS_MEIS 0x10
0140 #define ISMT_MSTS_IP 0x01
0141
0142
0143 #define ISMT_MDS_MASK 0xff
0144
0145
0146 #define ISMT_SPGT_SPD_MASK 0xc0000000
0147 #define ISMT_SPGT_SPD_80K 0x00
0148 #define ISMT_SPGT_SPD_100K (0x1 << 30)
0149 #define ISMT_SPGT_SPD_400K (0x2U << 30)
0150 #define ISMT_SPGT_SPD_1M (0x3U << 30)
0151
0152
0153
0154 #define ISMT_MSICTL_MSIE 0x01
0155
0156
0157 struct ismt_desc {
0158 u8 tgtaddr_rw;
0159 u8 wr_len_cmd;
0160 u8 rd_len;
0161 u8 control;
0162 u8 status;
0163 u8 retry;
0164 u8 rxbytes;
0165 u8 txbytes;
0166 u32 dptr_low;
0167 u32 dptr_high;
0168 } __packed;
0169
0170 struct ismt_priv {
0171 struct i2c_adapter adapter;
0172 void __iomem *smba;
0173 struct pci_dev *pci_dev;
0174 struct ismt_desc *hw;
0175 dma_addr_t io_rng_dma;
0176 u8 head;
0177 struct completion cmp;
0178 u8 buffer[I2C_SMBUS_BLOCK_MAX + 16];
0179 dma_addr_t log_dma;
0180 u32 *log;
0181 };
0182
0183 static const struct pci_device_id ismt_ids[] = {
0184 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_S1200_SMT0) },
0185 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_S1200_SMT1) },
0186 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_CDF_SMT) },
0187 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_DNV_SMT) },
0188 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_EBG_SMT) },
0189 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_AVOTON_SMT) },
0190 { 0, }
0191 };
0192
0193 MODULE_DEVICE_TABLE(pci, ismt_ids);
0194
0195
0196 static unsigned int bus_speed;
0197 module_param(bus_speed, uint, S_IRUGO);
0198 MODULE_PARM_DESC(bus_speed, "Bus Speed in kHz (0 = BIOS default)");
0199
0200
0201
0202
0203
0204
0205 static void __ismt_desc_dump(struct device *dev, const struct ismt_desc *desc)
0206 {
0207
0208 dev_dbg(dev, "Descriptor struct: %p\n", desc);
0209 dev_dbg(dev, "\ttgtaddr_rw=0x%02X\n", desc->tgtaddr_rw);
0210 dev_dbg(dev, "\twr_len_cmd=0x%02X\n", desc->wr_len_cmd);
0211 dev_dbg(dev, "\trd_len= 0x%02X\n", desc->rd_len);
0212 dev_dbg(dev, "\tcontrol= 0x%02X\n", desc->control);
0213 dev_dbg(dev, "\tstatus= 0x%02X\n", desc->status);
0214 dev_dbg(dev, "\tretry= 0x%02X\n", desc->retry);
0215 dev_dbg(dev, "\trxbytes= 0x%02X\n", desc->rxbytes);
0216 dev_dbg(dev, "\ttxbytes= 0x%02X\n", desc->txbytes);
0217 dev_dbg(dev, "\tdptr_low= 0x%08X\n", desc->dptr_low);
0218 dev_dbg(dev, "\tdptr_high= 0x%08X\n", desc->dptr_high);
0219 }
0220
0221
0222
0223
0224 static void ismt_desc_dump(struct ismt_priv *priv)
0225 {
0226 struct device *dev = &priv->pci_dev->dev;
0227 struct ismt_desc *desc = &priv->hw[priv->head];
0228
0229 dev_dbg(dev, "Dump of the descriptor struct: 0x%X\n", priv->head);
0230 __ismt_desc_dump(dev, desc);
0231 }
0232
0233
0234
0235
0236
0237 static void ismt_gen_reg_dump(struct ismt_priv *priv)
0238 {
0239 struct device *dev = &priv->pci_dev->dev;
0240
0241 dev_dbg(dev, "Dump of the iSMT General Registers\n");
0242 dev_dbg(dev, " GCTRL.... : (0x%p)=0x%X\n",
0243 priv->smba + ISMT_GR_GCTRL,
0244 readl(priv->smba + ISMT_GR_GCTRL));
0245 dev_dbg(dev, " SMTICL... : (0x%p)=0x%016llX\n",
0246 priv->smba + ISMT_GR_SMTICL,
0247 (long long unsigned int)readq(priv->smba + ISMT_GR_SMTICL));
0248 dev_dbg(dev, " ERRINTMSK : (0x%p)=0x%X\n",
0249 priv->smba + ISMT_GR_ERRINTMSK,
0250 readl(priv->smba + ISMT_GR_ERRINTMSK));
0251 dev_dbg(dev, " ERRAERMSK : (0x%p)=0x%X\n",
0252 priv->smba + ISMT_GR_ERRAERMSK,
0253 readl(priv->smba + ISMT_GR_ERRAERMSK));
0254 dev_dbg(dev, " ERRSTS... : (0x%p)=0x%X\n",
0255 priv->smba + ISMT_GR_ERRSTS,
0256 readl(priv->smba + ISMT_GR_ERRSTS));
0257 dev_dbg(dev, " ERRINFO.. : (0x%p)=0x%X\n",
0258 priv->smba + ISMT_GR_ERRINFO,
0259 readl(priv->smba + ISMT_GR_ERRINFO));
0260 }
0261
0262
0263
0264
0265
0266 static void ismt_mstr_reg_dump(struct ismt_priv *priv)
0267 {
0268 struct device *dev = &priv->pci_dev->dev;
0269
0270 dev_dbg(dev, "Dump of the iSMT Master Registers\n");
0271 dev_dbg(dev, " MDBA..... : (0x%p)=0x%016llX\n",
0272 priv->smba + ISMT_MSTR_MDBA,
0273 (long long unsigned int)readq(priv->smba + ISMT_MSTR_MDBA));
0274 dev_dbg(dev, " MCTRL.... : (0x%p)=0x%X\n",
0275 priv->smba + ISMT_MSTR_MCTRL,
0276 readl(priv->smba + ISMT_MSTR_MCTRL));
0277 dev_dbg(dev, " MSTS..... : (0x%p)=0x%X\n",
0278 priv->smba + ISMT_MSTR_MSTS,
0279 readl(priv->smba + ISMT_MSTR_MSTS));
0280 dev_dbg(dev, " MDS...... : (0x%p)=0x%X\n",
0281 priv->smba + ISMT_MSTR_MDS,
0282 readl(priv->smba + ISMT_MSTR_MDS));
0283 dev_dbg(dev, " RPOLICY.. : (0x%p)=0x%X\n",
0284 priv->smba + ISMT_MSTR_RPOLICY,
0285 readl(priv->smba + ISMT_MSTR_RPOLICY));
0286 dev_dbg(dev, " SPGT..... : (0x%p)=0x%X\n",
0287 priv->smba + ISMT_SPGT,
0288 readl(priv->smba + ISMT_SPGT));
0289 }
0290
0291
0292
0293
0294
0295 static void ismt_submit_desc(struct ismt_priv *priv)
0296 {
0297 uint fmhp;
0298 uint val;
0299
0300 ismt_desc_dump(priv);
0301 ismt_gen_reg_dump(priv);
0302 ismt_mstr_reg_dump(priv);
0303
0304
0305 fmhp = ((priv->head + 1) % ISMT_DESC_ENTRIES) << 16;
0306 val = readl(priv->smba + ISMT_MSTR_MCTRL);
0307 writel((val & ~ISMT_MCTRL_FMHP) | fmhp,
0308 priv->smba + ISMT_MSTR_MCTRL);
0309
0310
0311 val = readl(priv->smba + ISMT_MSTR_MCTRL);
0312 writel(val | ISMT_MCTRL_SS,
0313 priv->smba + ISMT_MSTR_MCTRL);
0314 }
0315
0316
0317
0318
0319
0320
0321
0322
0323
0324 static int ismt_process_desc(const struct ismt_desc *desc,
0325 union i2c_smbus_data *data,
0326 struct ismt_priv *priv, int size,
0327 char read_write)
0328 {
0329 u8 *dma_buffer = PTR_ALIGN(&priv->buffer[0], 16);
0330
0331 dev_dbg(&priv->pci_dev->dev, "Processing completed descriptor\n");
0332 __ismt_desc_dump(&priv->pci_dev->dev, desc);
0333 ismt_gen_reg_dump(priv);
0334 ismt_mstr_reg_dump(priv);
0335
0336 if (desc->status & ISMT_DESC_SCS) {
0337 if (read_write == I2C_SMBUS_WRITE &&
0338 size != I2C_SMBUS_PROC_CALL &&
0339 size != I2C_SMBUS_BLOCK_PROC_CALL)
0340 return 0;
0341
0342 switch (size) {
0343 case I2C_SMBUS_BYTE:
0344 case I2C_SMBUS_BYTE_DATA:
0345 data->byte = dma_buffer[0];
0346 break;
0347 case I2C_SMBUS_WORD_DATA:
0348 case I2C_SMBUS_PROC_CALL:
0349 data->word = dma_buffer[0] | (dma_buffer[1] << 8);
0350 break;
0351 case I2C_SMBUS_BLOCK_DATA:
0352 case I2C_SMBUS_BLOCK_PROC_CALL:
0353 if (desc->rxbytes != dma_buffer[0] + 1)
0354 return -EMSGSIZE;
0355
0356 memcpy(data->block, dma_buffer, desc->rxbytes);
0357 break;
0358 case I2C_SMBUS_I2C_BLOCK_DATA:
0359 memcpy(&data->block[1], dma_buffer, desc->rxbytes);
0360 data->block[0] = desc->rxbytes;
0361 break;
0362 }
0363 return 0;
0364 }
0365
0366 if (likely(desc->status & ISMT_DESC_NAK))
0367 return -ENXIO;
0368
0369 if (desc->status & ISMT_DESC_CRC)
0370 return -EBADMSG;
0371
0372 if (desc->status & ISMT_DESC_COL)
0373 return -EAGAIN;
0374
0375 if (desc->status & ISMT_DESC_LPR)
0376 return -EPROTO;
0377
0378 if (desc->status & (ISMT_DESC_DLTO | ISMT_DESC_CLTO))
0379 return -ETIMEDOUT;
0380
0381 return -EIO;
0382 }
0383
0384
0385
0386
0387
0388
0389
0390
0391
0392
0393
0394 static int ismt_access(struct i2c_adapter *adap, u16 addr,
0395 unsigned short flags, char read_write, u8 command,
0396 int size, union i2c_smbus_data *data)
0397 {
0398 int ret;
0399 unsigned long time_left;
0400 dma_addr_t dma_addr = 0;
0401 u8 dma_size = 0;
0402 enum dma_data_direction dma_direction = 0;
0403 struct ismt_desc *desc;
0404 struct ismt_priv *priv = i2c_get_adapdata(adap);
0405 struct device *dev = &priv->pci_dev->dev;
0406 u8 *dma_buffer = PTR_ALIGN(&priv->buffer[0], 16);
0407
0408 desc = &priv->hw[priv->head];
0409
0410
0411 memset(priv->buffer, 0, sizeof(priv->buffer));
0412
0413
0414 memset(desc, 0, sizeof(struct ismt_desc));
0415 desc->tgtaddr_rw = ISMT_DESC_ADDR_RW(addr, read_write);
0416
0417
0418 memset(priv->log, 0, ISMT_LOG_ENTRIES * sizeof(u32));
0419
0420
0421 if (likely(pci_dev_msi_enabled(priv->pci_dev)))
0422 desc->control = ISMT_DESC_INT | ISMT_DESC_FAIR;
0423 else
0424 desc->control = ISMT_DESC_FAIR;
0425
0426 if ((flags & I2C_CLIENT_PEC) && (size != I2C_SMBUS_QUICK)
0427 && (size != I2C_SMBUS_I2C_BLOCK_DATA))
0428 desc->control |= ISMT_DESC_PEC;
0429
0430 switch (size) {
0431 case I2C_SMBUS_QUICK:
0432 dev_dbg(dev, "I2C_SMBUS_QUICK\n");
0433 break;
0434
0435 case I2C_SMBUS_BYTE:
0436 if (read_write == I2C_SMBUS_WRITE) {
0437
0438
0439
0440
0441 dev_dbg(dev, "I2C_SMBUS_BYTE: WRITE\n");
0442 desc->control |= ISMT_DESC_CWRL;
0443 desc->wr_len_cmd = command;
0444 } else {
0445
0446 dev_dbg(dev, "I2C_SMBUS_BYTE: READ\n");
0447 dma_size = 1;
0448 dma_direction = DMA_FROM_DEVICE;
0449 desc->rd_len = 1;
0450 }
0451 break;
0452
0453 case I2C_SMBUS_BYTE_DATA:
0454 if (read_write == I2C_SMBUS_WRITE) {
0455
0456
0457
0458
0459 dev_dbg(dev, "I2C_SMBUS_BYTE_DATA: WRITE\n");
0460 desc->wr_len_cmd = 2;
0461 dma_size = 2;
0462 dma_direction = DMA_TO_DEVICE;
0463 dma_buffer[0] = command;
0464 dma_buffer[1] = data->byte;
0465 } else {
0466
0467 dev_dbg(dev, "I2C_SMBUS_BYTE_DATA: READ\n");
0468 desc->control |= ISMT_DESC_CWRL;
0469 desc->wr_len_cmd = command;
0470 desc->rd_len = 1;
0471 dma_size = 1;
0472 dma_direction = DMA_FROM_DEVICE;
0473 }
0474 break;
0475
0476 case I2C_SMBUS_WORD_DATA:
0477 if (read_write == I2C_SMBUS_WRITE) {
0478
0479 dev_dbg(dev, "I2C_SMBUS_WORD_DATA: WRITE\n");
0480 desc->wr_len_cmd = 3;
0481 dma_size = 3;
0482 dma_direction = DMA_TO_DEVICE;
0483 dma_buffer[0] = command;
0484 dma_buffer[1] = data->word & 0xff;
0485 dma_buffer[2] = data->word >> 8;
0486 } else {
0487
0488 dev_dbg(dev, "I2C_SMBUS_WORD_DATA: READ\n");
0489 desc->wr_len_cmd = command;
0490 desc->control |= ISMT_DESC_CWRL;
0491 desc->rd_len = 2;
0492 dma_size = 2;
0493 dma_direction = DMA_FROM_DEVICE;
0494 }
0495 break;
0496
0497 case I2C_SMBUS_PROC_CALL:
0498 dev_dbg(dev, "I2C_SMBUS_PROC_CALL\n");
0499 desc->wr_len_cmd = 3;
0500 desc->rd_len = 2;
0501 dma_size = 3;
0502 dma_direction = DMA_BIDIRECTIONAL;
0503 dma_buffer[0] = command;
0504 dma_buffer[1] = data->word & 0xff;
0505 dma_buffer[2] = data->word >> 8;
0506 break;
0507
0508 case I2C_SMBUS_BLOCK_DATA:
0509 if (read_write == I2C_SMBUS_WRITE) {
0510
0511 dev_dbg(dev, "I2C_SMBUS_BLOCK_DATA: WRITE\n");
0512 dma_size = data->block[0] + 1;
0513 dma_direction = DMA_TO_DEVICE;
0514 desc->wr_len_cmd = dma_size;
0515 desc->control |= ISMT_DESC_BLK;
0516 dma_buffer[0] = command;
0517 memcpy(&dma_buffer[1], &data->block[1], dma_size - 1);
0518 } else {
0519
0520 dev_dbg(dev, "I2C_SMBUS_BLOCK_DATA: READ\n");
0521 dma_size = I2C_SMBUS_BLOCK_MAX;
0522 dma_direction = DMA_FROM_DEVICE;
0523 desc->rd_len = dma_size;
0524 desc->wr_len_cmd = command;
0525 desc->control |= (ISMT_DESC_BLK | ISMT_DESC_CWRL);
0526 }
0527 break;
0528
0529 case I2C_SMBUS_BLOCK_PROC_CALL:
0530 dev_dbg(dev, "I2C_SMBUS_BLOCK_PROC_CALL\n");
0531 if (data->block[0] > I2C_SMBUS_BLOCK_MAX)
0532 return -EINVAL;
0533
0534 dma_size = I2C_SMBUS_BLOCK_MAX;
0535 desc->tgtaddr_rw = ISMT_DESC_ADDR_RW(addr, 1);
0536 desc->wr_len_cmd = data->block[0] + 1;
0537 desc->rd_len = dma_size;
0538 desc->control |= ISMT_DESC_BLK;
0539 dma_direction = DMA_BIDIRECTIONAL;
0540 dma_buffer[0] = command;
0541 memcpy(&dma_buffer[1], &data->block[1], data->block[0]);
0542 break;
0543
0544 case I2C_SMBUS_I2C_BLOCK_DATA:
0545
0546 if (data->block[0] < 1)
0547 data->block[0] = 1;
0548
0549 if (data->block[0] > I2C_SMBUS_BLOCK_MAX)
0550 data->block[0] = I2C_SMBUS_BLOCK_MAX;
0551
0552 if (read_write == I2C_SMBUS_WRITE) {
0553
0554 dev_dbg(dev, "I2C_SMBUS_I2C_BLOCK_DATA: WRITE\n");
0555 dma_size = data->block[0] + 1;
0556 dma_direction = DMA_TO_DEVICE;
0557 desc->wr_len_cmd = dma_size;
0558 desc->control |= ISMT_DESC_I2C;
0559 dma_buffer[0] = command;
0560 memcpy(&dma_buffer[1], &data->block[1], dma_size - 1);
0561 } else {
0562
0563 dev_dbg(dev, "I2C_SMBUS_I2C_BLOCK_DATA: READ\n");
0564 dma_size = data->block[0];
0565 dma_direction = DMA_FROM_DEVICE;
0566 desc->rd_len = dma_size;
0567 desc->wr_len_cmd = command;
0568 desc->control |= (ISMT_DESC_I2C | ISMT_DESC_CWRL);
0569
0570
0571
0572
0573
0574
0575 desc->tgtaddr_rw = ISMT_DESC_ADDR_RW(addr, 0);
0576 }
0577 break;
0578
0579 default:
0580 dev_err(dev, "Unsupported transaction %d\n",
0581 size);
0582 return -EOPNOTSUPP;
0583 }
0584
0585
0586 if (dma_size != 0) {
0587 dev_dbg(dev, " dev=%p\n", dev);
0588 dev_dbg(dev, " data=%p\n", data);
0589 dev_dbg(dev, " dma_buffer=%p\n", dma_buffer);
0590 dev_dbg(dev, " dma_size=%d\n", dma_size);
0591 dev_dbg(dev, " dma_direction=%d\n", dma_direction);
0592
0593 dma_addr = dma_map_single(dev,
0594 dma_buffer,
0595 dma_size,
0596 dma_direction);
0597
0598 if (dma_mapping_error(dev, dma_addr)) {
0599 dev_err(dev, "Error in mapping dma buffer %p\n",
0600 dma_buffer);
0601 return -EIO;
0602 }
0603
0604 dev_dbg(dev, " dma_addr = %pad\n", &dma_addr);
0605
0606 desc->dptr_low = lower_32_bits(dma_addr);
0607 desc->dptr_high = upper_32_bits(dma_addr);
0608 }
0609
0610 reinit_completion(&priv->cmp);
0611
0612
0613 ismt_submit_desc(priv);
0614
0615
0616 time_left = wait_for_completion_timeout(&priv->cmp, HZ*1);
0617
0618
0619 if (dma_size != 0)
0620 dma_unmap_single(dev, dma_addr, dma_size, dma_direction);
0621
0622 if (unlikely(!time_left)) {
0623 dev_err(dev, "completion wait timed out\n");
0624 ret = -ETIMEDOUT;
0625 goto out;
0626 }
0627
0628
0629 ret = ismt_process_desc(desc, data, priv, size, read_write);
0630
0631 out:
0632
0633 priv->head++;
0634 priv->head %= ISMT_DESC_ENTRIES;
0635
0636 return ret;
0637 }
0638
0639
0640
0641
0642
0643 static u32 ismt_func(struct i2c_adapter *adap)
0644 {
0645 return I2C_FUNC_SMBUS_QUICK |
0646 I2C_FUNC_SMBUS_BYTE |
0647 I2C_FUNC_SMBUS_BYTE_DATA |
0648 I2C_FUNC_SMBUS_WORD_DATA |
0649 I2C_FUNC_SMBUS_PROC_CALL |
0650 I2C_FUNC_SMBUS_BLOCK_PROC_CALL |
0651 I2C_FUNC_SMBUS_BLOCK_DATA |
0652 I2C_FUNC_SMBUS_I2C_BLOCK |
0653 I2C_FUNC_SMBUS_PEC;
0654 }
0655
0656 static const struct i2c_algorithm smbus_algorithm = {
0657 .smbus_xfer = ismt_access,
0658 .functionality = ismt_func,
0659 };
0660
0661
0662
0663
0664
0665 static irqreturn_t ismt_handle_isr(struct ismt_priv *priv)
0666 {
0667 complete(&priv->cmp);
0668
0669 return IRQ_HANDLED;
0670 }
0671
0672
0673
0674
0675
0676
0677
0678 static irqreturn_t ismt_do_interrupt(int vec, void *data)
0679 {
0680 u32 val;
0681 struct ismt_priv *priv = data;
0682
0683
0684
0685
0686
0687 val = readl(priv->smba + ISMT_MSTR_MSTS);
0688
0689 if (!(val & (ISMT_MSTS_MIS | ISMT_MSTS_MEIS)))
0690 return IRQ_NONE;
0691 else
0692 writel(val | ISMT_MSTS_MIS | ISMT_MSTS_MEIS,
0693 priv->smba + ISMT_MSTR_MSTS);
0694
0695 return ismt_handle_isr(priv);
0696 }
0697
0698
0699
0700
0701
0702
0703 static irqreturn_t ismt_do_msi_interrupt(int vec, void *data)
0704 {
0705 return ismt_handle_isr(data);
0706 }
0707
0708
0709
0710
0711
0712 static void ismt_hw_init(struct ismt_priv *priv)
0713 {
0714 u32 val;
0715 struct device *dev = &priv->pci_dev->dev;
0716
0717
0718 writeq(priv->io_rng_dma, priv->smba + ISMT_MSTR_MDBA);
0719
0720 writeq(priv->log_dma, priv->smba + ISMT_GR_SMTICL);
0721
0722
0723 writel(ISMT_MCTRL_MEIE, priv->smba + ISMT_MSTR_MCTRL);
0724
0725
0726 writel(0, priv->smba + ISMT_MSTR_MSTS);
0727
0728
0729 val = readl(priv->smba + ISMT_MSTR_MDS);
0730 writel((val & ~ISMT_MDS_MASK) | (ISMT_DESC_ENTRIES - 1),
0731 priv->smba + ISMT_MSTR_MDS);
0732
0733
0734
0735
0736
0737 val = readl(priv->smba + ISMT_SPGT);
0738
0739 switch (bus_speed) {
0740 case 0:
0741 break;
0742
0743 case 80:
0744 dev_dbg(dev, "Setting SMBus clock to 80 kHz\n");
0745 writel(((val & ~ISMT_SPGT_SPD_MASK) | ISMT_SPGT_SPD_80K),
0746 priv->smba + ISMT_SPGT);
0747 break;
0748
0749 case 100:
0750 dev_dbg(dev, "Setting SMBus clock to 100 kHz\n");
0751 writel(((val & ~ISMT_SPGT_SPD_MASK) | ISMT_SPGT_SPD_100K),
0752 priv->smba + ISMT_SPGT);
0753 break;
0754
0755 case 400:
0756 dev_dbg(dev, "Setting SMBus clock to 400 kHz\n");
0757 writel(((val & ~ISMT_SPGT_SPD_MASK) | ISMT_SPGT_SPD_400K),
0758 priv->smba + ISMT_SPGT);
0759 break;
0760
0761 case 1000:
0762 dev_dbg(dev, "Setting SMBus clock to 1000 kHz\n");
0763 writel(((val & ~ISMT_SPGT_SPD_MASK) | ISMT_SPGT_SPD_1M),
0764 priv->smba + ISMT_SPGT);
0765 break;
0766
0767 default:
0768 dev_warn(dev, "Invalid SMBus clock speed, only 0, 80, 100, 400, and 1000 are valid\n");
0769 break;
0770 }
0771
0772 val = readl(priv->smba + ISMT_SPGT);
0773
0774 switch (val & ISMT_SPGT_SPD_MASK) {
0775 case ISMT_SPGT_SPD_80K:
0776 bus_speed = 80;
0777 break;
0778 case ISMT_SPGT_SPD_100K:
0779 bus_speed = 100;
0780 break;
0781 case ISMT_SPGT_SPD_400K:
0782 bus_speed = 400;
0783 break;
0784 case ISMT_SPGT_SPD_1M:
0785 bus_speed = 1000;
0786 break;
0787 }
0788 dev_dbg(dev, "SMBus clock is running at %d kHz\n", bus_speed);
0789 }
0790
0791
0792
0793
0794
0795 static int ismt_dev_init(struct ismt_priv *priv)
0796 {
0797
0798 priv->hw = dmam_alloc_coherent(&priv->pci_dev->dev,
0799 (ISMT_DESC_ENTRIES
0800 * sizeof(struct ismt_desc)),
0801 &priv->io_rng_dma,
0802 GFP_KERNEL);
0803 if (!priv->hw)
0804 return -ENOMEM;
0805
0806 priv->head = 0;
0807 init_completion(&priv->cmp);
0808
0809 priv->log = dmam_alloc_coherent(&priv->pci_dev->dev,
0810 ISMT_LOG_ENTRIES * sizeof(u32),
0811 &priv->log_dma, GFP_KERNEL);
0812 if (!priv->log)
0813 return -ENOMEM;
0814
0815 return 0;
0816 }
0817
0818
0819
0820
0821
0822 static int ismt_int_init(struct ismt_priv *priv)
0823 {
0824 int err;
0825
0826
0827 err = pci_enable_msi(priv->pci_dev);
0828 if (err)
0829 goto intx;
0830
0831 err = devm_request_irq(&priv->pci_dev->dev,
0832 priv->pci_dev->irq,
0833 ismt_do_msi_interrupt,
0834 0,
0835 "ismt-msi",
0836 priv);
0837 if (err) {
0838 pci_disable_msi(priv->pci_dev);
0839 goto intx;
0840 }
0841
0842 return 0;
0843
0844
0845 intx:
0846 dev_warn(&priv->pci_dev->dev,
0847 "Unable to use MSI interrupts, falling back to legacy\n");
0848
0849 err = devm_request_irq(&priv->pci_dev->dev,
0850 priv->pci_dev->irq,
0851 ismt_do_interrupt,
0852 IRQF_SHARED,
0853 "ismt-intx",
0854 priv);
0855 if (err) {
0856 dev_err(&priv->pci_dev->dev, "no usable interrupts\n");
0857 return err;
0858 }
0859
0860 return 0;
0861 }
0862
0863 static struct pci_driver ismt_driver;
0864
0865
0866
0867
0868
0869
0870 static int
0871 ismt_probe(struct pci_dev *pdev, const struct pci_device_id *id)
0872 {
0873 int err;
0874 struct ismt_priv *priv;
0875 unsigned long start, len;
0876
0877 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
0878 if (!priv)
0879 return -ENOMEM;
0880
0881 pci_set_drvdata(pdev, priv);
0882
0883 i2c_set_adapdata(&priv->adapter, priv);
0884 priv->adapter.owner = THIS_MODULE;
0885 priv->adapter.class = I2C_CLASS_HWMON;
0886 priv->adapter.algo = &smbus_algorithm;
0887 priv->adapter.dev.parent = &pdev->dev;
0888 ACPI_COMPANION_SET(&priv->adapter.dev, ACPI_COMPANION(&pdev->dev));
0889 priv->adapter.retries = ISMT_MAX_RETRIES;
0890
0891 priv->pci_dev = pdev;
0892
0893 err = pcim_enable_device(pdev);
0894 if (err) {
0895 dev_err(&pdev->dev, "Failed to enable SMBus PCI device (%d)\n",
0896 err);
0897 return err;
0898 }
0899
0900
0901 pci_set_master(pdev);
0902
0903
0904 start = pci_resource_start(pdev, SMBBAR);
0905 len = pci_resource_len(pdev, SMBBAR);
0906 if (!start || !len) {
0907 dev_err(&pdev->dev,
0908 "SMBus base address uninitialized, upgrade BIOS\n");
0909 return -ENODEV;
0910 }
0911
0912 snprintf(priv->adapter.name, sizeof(priv->adapter.name),
0913 "SMBus iSMT adapter at %lx", start);
0914
0915 dev_dbg(&priv->pci_dev->dev, " start=0x%lX\n", start);
0916 dev_dbg(&priv->pci_dev->dev, " len=0x%lX\n", len);
0917
0918 err = acpi_check_resource_conflict(&pdev->resource[SMBBAR]);
0919 if (err) {
0920 dev_err(&pdev->dev, "ACPI resource conflict!\n");
0921 return err;
0922 }
0923
0924 err = pci_request_region(pdev, SMBBAR, ismt_driver.name);
0925 if (err) {
0926 dev_err(&pdev->dev,
0927 "Failed to request SMBus region 0x%lx-0x%lx\n",
0928 start, start + len);
0929 return err;
0930 }
0931
0932 priv->smba = pcim_iomap(pdev, SMBBAR, len);
0933 if (!priv->smba) {
0934 dev_err(&pdev->dev, "Unable to ioremap SMBus BAR\n");
0935 return -ENODEV;
0936 }
0937
0938 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
0939 if (err) {
0940 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
0941 if (err) {
0942 dev_err(&pdev->dev, "dma_set_mask fail\n");
0943 return -ENODEV;
0944 }
0945 }
0946
0947 err = ismt_dev_init(priv);
0948 if (err)
0949 return err;
0950
0951 ismt_hw_init(priv);
0952
0953 err = ismt_int_init(priv);
0954 if (err)
0955 return err;
0956
0957 err = i2c_add_adapter(&priv->adapter);
0958 if (err)
0959 return -ENODEV;
0960 return 0;
0961 }
0962
0963
0964
0965
0966
0967 static void ismt_remove(struct pci_dev *pdev)
0968 {
0969 struct ismt_priv *priv = pci_get_drvdata(pdev);
0970
0971 i2c_del_adapter(&priv->adapter);
0972 }
0973
0974 static struct pci_driver ismt_driver = {
0975 .name = "ismt_smbus",
0976 .id_table = ismt_ids,
0977 .probe = ismt_probe,
0978 .remove = ismt_remove,
0979 };
0980
0981 module_pci_driver(ismt_driver);
0982
0983 MODULE_LICENSE("Dual BSD/GPL");
0984 MODULE_AUTHOR("Bill E. Brown <bill.e.brown@intel.com>");
0985 MODULE_DESCRIPTION("Intel SMBus Message Transport (iSMT) driver");