1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64#include <linux/module.h>
65#include <linux/init.h>
66#include <linux/pci.h>
67#include <linux/kernel.h>
68#include <linux/stddef.h>
69#include <linux/completion.h>
70#include <linux/dma-mapping.h>
71#include <linux/i2c.h>
72#include <linux/acpi.h>
73#include <linux/interrupt.h>
74
75#include <asm-generic/io-64-nonatomic-lo-hi.h>
76
77
78#define SMBBAR 0
79
80
81#define PCI_DEVICE_ID_INTEL_S1200_SMT0 0x0c59
82#define PCI_DEVICE_ID_INTEL_S1200_SMT1 0x0c5a
83#define PCI_DEVICE_ID_INTEL_AVOTON_SMT 0x1f15
84
85#define ISMT_DESC_ENTRIES 32
86#define ISMT_MAX_RETRIES 3
87
88
89#define ISMT_DESC_CWRL 0x01
90#define ISMT_DESC_BLK 0X04
91#define ISMT_DESC_FAIR 0x08
92#define ISMT_DESC_PEC 0x10
93#define ISMT_DESC_I2C 0x20
94#define ISMT_DESC_INT 0x40
95#define ISMT_DESC_SOE 0x80
96
97
98#define ISMT_DESC_SCS 0x01
99#define ISMT_DESC_DLTO 0x04
100#define ISMT_DESC_NAK 0x08
101#define ISMT_DESC_CRC 0x10
102#define ISMT_DESC_CLTO 0x20
103#define ISMT_DESC_COL 0x40
104#define ISMT_DESC_LPR 0x80
105
106
107#define ISMT_DESC_ADDR_RW(addr, rw) (((addr) << 1) | (rw))
108
109
110#define ISMT_GR_GCTRL 0x000
111#define ISMT_GR_SMTICL 0x008
112#define ISMT_GR_ERRINTMSK 0x010
113#define ISMT_GR_ERRAERMSK 0x014
114#define ISMT_GR_ERRSTS 0x018
115#define ISMT_GR_ERRINFO 0x01c
116
117
118#define ISMT_MSTR_MDBA 0x100
119#define ISMT_MSTR_MCTRL 0x108
120#define ISMT_MSTR_MSTS 0x10c
121#define ISMT_MSTR_MDS 0x110
122#define ISMT_MSTR_RPOLICY 0x114
123
124
125#define ISMT_SPGT 0x300
126
127
128#define ISMT_GCTRL_TRST 0x04
129#define ISMT_GCTRL_KILL 0x08
130#define ISMT_GCTRL_SRST 0x40
131
132
133#define ISMT_MCTRL_SS 0x01
134#define ISMT_MCTRL_MEIE 0x10
135#define ISMT_MCTRL_FMHP 0x00ff0000
136
137
138#define ISMT_MSTS_HMTP 0xff0000
139#define ISMT_MSTS_MIS 0x20
140#define ISMT_MSTS_MEIS 0x10
141#define ISMT_MSTS_IP 0x01
142
143
144#define ISMT_MDS_MASK 0xff
145
146
147#define ISMT_SPGT_SPD_MASK 0xc0000000
148#define ISMT_SPGT_SPD_80K 0x00
149#define ISMT_SPGT_SPD_100K (0x1 << 30)
150#define ISMT_SPGT_SPD_400K (0x2 << 30)
151#define ISMT_SPGT_SPD_1M (0x3 << 30)
152
153
154
155#define ISMT_MSICTL_MSIE 0x01
156
157
158struct ismt_desc {
159 u8 tgtaddr_rw;
160 u8 wr_len_cmd;
161 u8 rd_len;
162 u8 control;
163 u8 status;
164 u8 retry;
165 u8 rxbytes;
166 u8 txbytes;
167 u32 dptr_low;
168 u32 dptr_high;
169} __packed;
170
171struct ismt_priv {
172 struct i2c_adapter adapter;
173 void *smba;
174 struct pci_dev *pci_dev;
175 struct ismt_desc *hw;
176 dma_addr_t io_rng_dma;
177 u8 head;
178 struct completion cmp;
179 u8 dma_buffer[I2C_SMBUS_BLOCK_MAX + 1];
180 bool using_msi;
181};
182
183
184
185
186static DEFINE_PCI_DEVICE_TABLE(ismt_ids) = {
187 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_S1200_SMT0) },
188 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_S1200_SMT1) },
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
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
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
220
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
233
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
262
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
291
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
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
309 val = readl(priv->smba + ISMT_MSTR_MCTRL);
310 writel(val | ISMT_MCTRL_SS,
311 priv->smba + ISMT_MSTR_MCTRL);
312}
313
314
315
316
317
318
319
320
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 = priv->dma_buffer;
328
329 dev_dbg(&priv->pci_dev->dev, "Processing completed descriptor\n");
330 __ismt_desc_dump(&priv->pci_dev->dev, desc);
331
332 if (desc->status & ISMT_DESC_SCS) {
333 if (read_write == I2C_SMBUS_WRITE &&
334 size != I2C_SMBUS_PROC_CALL)
335 return 0;
336
337 switch (size) {
338 case I2C_SMBUS_BYTE:
339 case I2C_SMBUS_BYTE_DATA:
340 data->byte = dma_buffer[0];
341 break;
342 case I2C_SMBUS_WORD_DATA:
343 case I2C_SMBUS_PROC_CALL:
344 data->word = dma_buffer[0] | (dma_buffer[1] << 8);
345 break;
346 case I2C_SMBUS_BLOCK_DATA:
347 memcpy(&data->block[1], dma_buffer, desc->rxbytes);
348 data->block[0] = desc->rxbytes;
349 break;
350 }
351 return 0;
352 }
353
354 if (likely(desc->status & ISMT_DESC_NAK))
355 return -ENXIO;
356
357 if (desc->status & ISMT_DESC_CRC)
358 return -EBADMSG;
359
360 if (desc->status & ISMT_DESC_COL)
361 return -EAGAIN;
362
363 if (desc->status & ISMT_DESC_LPR)
364 return -EPROTO;
365
366 if (desc->status & (ISMT_DESC_DLTO | ISMT_DESC_CLTO))
367 return -ETIMEDOUT;
368
369 return -EIO;
370}
371
372
373
374
375
376
377
378
379
380
381
382static int ismt_access(struct i2c_adapter *adap, u16 addr,
383 unsigned short flags, char read_write, u8 command,
384 int size, union i2c_smbus_data *data)
385{
386 int ret;
387 dma_addr_t dma_addr = 0;
388 u8 dma_size = 0;
389 enum dma_data_direction dma_direction = 0;
390 struct ismt_desc *desc;
391 struct ismt_priv *priv = i2c_get_adapdata(adap);
392 struct device *dev = &priv->pci_dev->dev;
393
394 desc = &priv->hw[priv->head];
395
396
397 memset(priv->dma_buffer, 0, sizeof(priv->dma_buffer));
398
399
400 memset(desc, 0, sizeof(struct ismt_desc));
401 desc->tgtaddr_rw = ISMT_DESC_ADDR_RW(addr, read_write);
402
403
404 if (likely(priv->using_msi))
405 desc->control = ISMT_DESC_INT | ISMT_DESC_FAIR;
406 else
407 desc->control = ISMT_DESC_FAIR;
408
409 if ((flags & I2C_CLIENT_PEC) && (size != I2C_SMBUS_QUICK)
410 && (size != I2C_SMBUS_I2C_BLOCK_DATA))
411 desc->control |= ISMT_DESC_PEC;
412
413 switch (size) {
414 case I2C_SMBUS_QUICK:
415 dev_dbg(dev, "I2C_SMBUS_QUICK\n");
416 break;
417
418 case I2C_SMBUS_BYTE:
419 if (read_write == I2C_SMBUS_WRITE) {
420
421
422
423
424 dev_dbg(dev, "I2C_SMBUS_BYTE: WRITE\n");
425 desc->control |= ISMT_DESC_CWRL;
426 desc->wr_len_cmd = command;
427 } else {
428
429 dev_dbg(dev, "I2C_SMBUS_BYTE: READ\n");
430 dma_size = 1;
431 dma_direction = DMA_FROM_DEVICE;
432 desc->rd_len = 1;
433 }
434 break;
435
436 case I2C_SMBUS_BYTE_DATA:
437 if (read_write == I2C_SMBUS_WRITE) {
438
439
440
441
442 dev_dbg(dev, "I2C_SMBUS_BYTE_DATA: WRITE\n");
443 desc->wr_len_cmd = 2;
444 dma_size = 2;
445 dma_direction = DMA_TO_DEVICE;
446 priv->dma_buffer[0] = command;
447 priv->dma_buffer[1] = data->byte;
448 } else {
449
450 dev_dbg(dev, "I2C_SMBUS_BYTE_DATA: READ\n");
451 desc->control |= ISMT_DESC_CWRL;
452 desc->wr_len_cmd = command;
453 desc->rd_len = 1;
454 dma_size = 1;
455 dma_direction = DMA_FROM_DEVICE;
456 }
457 break;
458
459 case I2C_SMBUS_WORD_DATA:
460 if (read_write == I2C_SMBUS_WRITE) {
461
462 dev_dbg(dev, "I2C_SMBUS_WORD_DATA: WRITE\n");
463 desc->wr_len_cmd = 3;
464 dma_size = 3;
465 dma_direction = DMA_TO_DEVICE;
466 priv->dma_buffer[0] = command;
467 priv->dma_buffer[1] = data->word & 0xff;
468 priv->dma_buffer[2] = data->word >> 8;
469 } else {
470
471 dev_dbg(dev, "I2C_SMBUS_WORD_DATA: READ\n");
472 desc->wr_len_cmd = command;
473 desc->control |= ISMT_DESC_CWRL;
474 desc->rd_len = 2;
475 dma_size = 2;
476 dma_direction = DMA_FROM_DEVICE;
477 }
478 break;
479
480 case I2C_SMBUS_PROC_CALL:
481 dev_dbg(dev, "I2C_SMBUS_PROC_CALL\n");
482 desc->wr_len_cmd = 3;
483 desc->rd_len = 2;
484 dma_size = 3;
485 dma_direction = DMA_BIDIRECTIONAL;
486 priv->dma_buffer[0] = command;
487 priv->dma_buffer[1] = data->word & 0xff;
488 priv->dma_buffer[2] = data->word >> 8;
489 break;
490
491 case I2C_SMBUS_BLOCK_DATA:
492 if (read_write == I2C_SMBUS_WRITE) {
493
494 dev_dbg(dev, "I2C_SMBUS_BLOCK_DATA: WRITE\n");
495 dma_size = data->block[0] + 1;
496 dma_direction = DMA_TO_DEVICE;
497 desc->wr_len_cmd = dma_size;
498 desc->control |= ISMT_DESC_BLK;
499 priv->dma_buffer[0] = command;
500 memcpy(&priv->dma_buffer[1], &data->block[1], dma_size);
501 } else {
502
503 dev_dbg(dev, "I2C_SMBUS_BLOCK_DATA: READ\n");
504 dma_size = I2C_SMBUS_BLOCK_MAX;
505 dma_direction = DMA_FROM_DEVICE;
506 desc->rd_len = dma_size;
507 desc->wr_len_cmd = command;
508 desc->control |= (ISMT_DESC_BLK | ISMT_DESC_CWRL);
509 }
510 break;
511
512 default:
513 dev_err(dev, "Unsupported transaction %d\n",
514 size);
515 return -EOPNOTSUPP;
516 }
517
518
519 if (dma_size != 0) {
520 dev_dbg(dev, " dev=%p\n", dev);
521 dev_dbg(dev, " data=%p\n", data);
522 dev_dbg(dev, " dma_buffer=%p\n", priv->dma_buffer);
523 dev_dbg(dev, " dma_size=%d\n", dma_size);
524 dev_dbg(dev, " dma_direction=%d\n", dma_direction);
525
526 dma_addr = dma_map_single(dev,
527 priv->dma_buffer,
528 dma_size,
529 dma_direction);
530
531 if (dma_mapping_error(dev, dma_addr)) {
532 dev_err(dev, "Error in mapping dma buffer %p\n",
533 priv->dma_buffer);
534 return -EIO;
535 }
536
537 dev_dbg(dev, " dma_addr = 0x%016llX\n",
538 (unsigned long long)dma_addr);
539
540 desc->dptr_low = lower_32_bits(dma_addr);
541 desc->dptr_high = upper_32_bits(dma_addr);
542 }
543
544 INIT_COMPLETION(priv->cmp);
545
546
547 ismt_submit_desc(priv);
548
549
550 ret = wait_for_completion_timeout(&priv->cmp, HZ*1);
551
552
553 if (dma_size != 0)
554 dma_unmap_single(&adap->dev, dma_addr, dma_size, dma_direction);
555
556 if (unlikely(!ret)) {
557 dev_err(dev, "completion wait timed out\n");
558 ret = -ETIMEDOUT;
559 goto out;
560 }
561
562
563 ret = ismt_process_desc(desc, data, priv, size, read_write);
564
565out:
566
567 priv->head++;
568 priv->head %= ISMT_DESC_ENTRIES;
569
570 return ret;
571}
572
573
574
575
576
577static u32 ismt_func(struct i2c_adapter *adap)
578{
579 return I2C_FUNC_SMBUS_QUICK |
580 I2C_FUNC_SMBUS_BYTE |
581 I2C_FUNC_SMBUS_BYTE_DATA |
582 I2C_FUNC_SMBUS_WORD_DATA |
583 I2C_FUNC_SMBUS_PROC_CALL |
584 I2C_FUNC_SMBUS_BLOCK_DATA |
585 I2C_FUNC_SMBUS_PEC;
586}
587
588
589
590
591
592
593static const struct i2c_algorithm smbus_algorithm = {
594 .smbus_xfer = ismt_access,
595 .functionality = ismt_func,
596};
597
598
599
600
601
602static irqreturn_t ismt_handle_isr(struct ismt_priv *priv)
603{
604 complete(&priv->cmp);
605
606 return IRQ_HANDLED;
607}
608
609
610
611
612
613
614
615static irqreturn_t ismt_do_interrupt(int vec, void *data)
616{
617 u32 val;
618 struct ismt_priv *priv = data;
619
620
621
622
623
624 val = readl(priv->smba + ISMT_MSTR_MSTS);
625
626 if (!(val & (ISMT_MSTS_MIS | ISMT_MSTS_MEIS)))
627 return IRQ_NONE;
628 else
629 writel(val | ISMT_MSTS_MIS | ISMT_MSTS_MEIS,
630 priv->smba + ISMT_MSTR_MSTS);
631
632 return ismt_handle_isr(priv);
633}
634
635
636
637
638
639
640static irqreturn_t ismt_do_msi_interrupt(int vec, void *data)
641{
642 return ismt_handle_isr(data);
643}
644
645
646
647
648
649static void ismt_hw_init(struct ismt_priv *priv)
650{
651 u32 val;
652 struct device *dev = &priv->pci_dev->dev;
653
654
655 writeq(priv->io_rng_dma, priv->smba + ISMT_MSTR_MDBA);
656
657
658 writel(ISMT_MCTRL_MEIE, priv->smba + ISMT_MSTR_MCTRL);
659
660
661 writel(0, priv->smba + ISMT_MSTR_MSTS);
662
663
664 val = readl(priv->smba + ISMT_MSTR_MDS);
665 writel((val & ~ISMT_MDS_MASK) | (ISMT_DESC_ENTRIES - 1),
666 priv->smba + ISMT_MSTR_MDS);
667
668
669
670
671
672 val = readl(priv->smba + ISMT_SPGT);
673
674 switch (bus_speed) {
675 case 0:
676 break;
677
678 case 80:
679 dev_dbg(dev, "Setting SMBus clock to 80 kHz\n");
680 writel(((val & ~ISMT_SPGT_SPD_MASK) | ISMT_SPGT_SPD_80K),
681 priv->smba + ISMT_SPGT);
682 break;
683
684 case 100:
685 dev_dbg(dev, "Setting SMBus clock to 100 kHz\n");
686 writel(((val & ~ISMT_SPGT_SPD_MASK) | ISMT_SPGT_SPD_100K),
687 priv->smba + ISMT_SPGT);
688 break;
689
690 case 400:
691 dev_dbg(dev, "Setting SMBus clock to 400 kHz\n");
692 writel(((val & ~ISMT_SPGT_SPD_MASK) | ISMT_SPGT_SPD_400K),
693 priv->smba + ISMT_SPGT);
694 break;
695
696 case 1000:
697 dev_dbg(dev, "Setting SMBus clock to 1000 kHz\n");
698 writel(((val & ~ISMT_SPGT_SPD_MASK) | ISMT_SPGT_SPD_1M),
699 priv->smba + ISMT_SPGT);
700 break;
701
702 default:
703 dev_warn(dev, "Invalid SMBus clock speed, only 0, 80, 100, 400, and 1000 are valid\n");
704 break;
705 }
706
707 val = readl(priv->smba + ISMT_SPGT);
708
709 switch (val & ISMT_SPGT_SPD_MASK) {
710 case ISMT_SPGT_SPD_80K:
711 bus_speed = 80;
712 break;
713 case ISMT_SPGT_SPD_100K:
714 bus_speed = 100;
715 break;
716 case ISMT_SPGT_SPD_400K:
717 bus_speed = 400;
718 break;
719 case ISMT_SPGT_SPD_1M:
720 bus_speed = 1000;
721 break;
722 }
723 dev_dbg(dev, "SMBus clock is running at %d kHz\n", bus_speed);
724}
725
726
727
728
729
730static int ismt_dev_init(struct ismt_priv *priv)
731{
732
733 priv->hw = dmam_alloc_coherent(&priv->pci_dev->dev,
734 (ISMT_DESC_ENTRIES
735 * sizeof(struct ismt_desc)),
736 &priv->io_rng_dma,
737 GFP_KERNEL);
738 if (!priv->hw)
739 return -ENOMEM;
740
741 memset(priv->hw, 0, (ISMT_DESC_ENTRIES * sizeof(struct ismt_desc)));
742
743 priv->head = 0;
744 init_completion(&priv->cmp);
745
746 return 0;
747}
748
749
750
751
752
753static int ismt_int_init(struct ismt_priv *priv)
754{
755 int err;
756
757
758 err = pci_enable_msi(priv->pci_dev);
759 if (err) {
760 dev_warn(&priv->pci_dev->dev,
761 "Unable to use MSI interrupts, falling back to legacy\n");
762 goto intx;
763 }
764
765 err = devm_request_irq(&priv->pci_dev->dev,
766 priv->pci_dev->irq,
767 ismt_do_msi_interrupt,
768 0,
769 "ismt-msi",
770 priv);
771 if (err) {
772 pci_disable_msi(priv->pci_dev);
773 goto intx;
774 }
775
776 priv->using_msi = true;
777 goto done;
778
779
780intx:
781 err = devm_request_irq(&priv->pci_dev->dev,
782 priv->pci_dev->irq,
783 ismt_do_interrupt,
784 IRQF_SHARED,
785 "ismt-intx",
786 priv);
787 if (err) {
788 dev_err(&priv->pci_dev->dev, "no usable interrupts\n");
789 return -ENODEV;
790 }
791
792 priv->using_msi = false;
793
794done:
795 return 0;
796}
797
798static struct pci_driver ismt_driver;
799
800
801
802
803
804
805static int
806ismt_probe(struct pci_dev *pdev, const struct pci_device_id *id)
807{
808 int err;
809 struct ismt_priv *priv;
810 unsigned long start, len;
811
812 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
813 if (!priv)
814 return -ENOMEM;
815
816 pci_set_drvdata(pdev, priv);
817 i2c_set_adapdata(&priv->adapter, priv);
818 priv->adapter.owner = THIS_MODULE;
819
820 priv->adapter.class = I2C_CLASS_HWMON;
821
822 priv->adapter.algo = &smbus_algorithm;
823
824
825 priv->adapter.dev.parent = &pdev->dev;
826
827
828 priv->adapter.retries = ISMT_MAX_RETRIES;
829
830 priv->pci_dev = pdev;
831
832 err = pcim_enable_device(pdev);
833 if (err) {
834 dev_err(&pdev->dev, "Failed to enable SMBus PCI device (%d)\n",
835 err);
836 return err;
837 }
838
839
840 pci_set_master(pdev);
841
842
843 start = pci_resource_start(pdev, SMBBAR);
844 len = pci_resource_len(pdev, SMBBAR);
845 if (!start || !len) {
846 dev_err(&pdev->dev,
847 "SMBus base address uninitialized, upgrade BIOS\n");
848 return -ENODEV;
849 }
850
851 snprintf(priv->adapter.name, sizeof(priv->adapter.name),
852 "SMBus iSMT adapter at %lx", start);
853
854 dev_dbg(&priv->pci_dev->dev, " start=0x%lX\n", start);
855 dev_dbg(&priv->pci_dev->dev, " len=0x%lX\n", len);
856
857 err = acpi_check_resource_conflict(&pdev->resource[SMBBAR]);
858 if (err) {
859 dev_err(&pdev->dev, "ACPI resource conflict!\n");
860 return err;
861 }
862
863 err = pci_request_region(pdev, SMBBAR, ismt_driver.name);
864 if (err) {
865 dev_err(&pdev->dev,
866 "Failed to request SMBus region 0x%lx-0x%lx\n",
867 start, start + len);
868 return err;
869 }
870
871 priv->smba = pcim_iomap(pdev, SMBBAR, len);
872 if (!priv->smba) {
873 dev_err(&pdev->dev, "Unable to ioremap SMBus BAR\n");
874 err = -ENODEV;
875 goto fail;
876 }
877
878 if ((pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0) ||
879 (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64)) != 0)) {
880 if ((pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0) ||
881 (pci_set_consistent_dma_mask(pdev,
882 DMA_BIT_MASK(32)) != 0)) {
883 dev_err(&pdev->dev, "pci_set_dma_mask fail %p\n",
884 pdev);
885 err = -ENODEV;
886 goto fail;
887 }
888 }
889
890 err = ismt_dev_init(priv);
891 if (err)
892 goto fail;
893
894 ismt_hw_init(priv);
895
896 err = ismt_int_init(priv);
897 if (err)
898 goto fail;
899
900 err = i2c_add_adapter(&priv->adapter);
901 if (err) {
902 dev_err(&pdev->dev, "Failed to add SMBus iSMT adapter\n");
903 err = -ENODEV;
904 goto fail;
905 }
906 return 0;
907
908fail:
909 pci_release_region(pdev, SMBBAR);
910 return err;
911}
912
913
914
915
916
917static void ismt_remove(struct pci_dev *pdev)
918{
919 struct ismt_priv *priv = pci_get_drvdata(pdev);
920
921 i2c_del_adapter(&priv->adapter);
922 pci_release_region(pdev, SMBBAR);
923}
924
925
926
927
928
929
930#ifdef CONFIG_PM
931static int ismt_suspend(struct pci_dev *pdev, pm_message_t mesg)
932{
933 pci_save_state(pdev);
934 pci_set_power_state(pdev, pci_choose_state(pdev, mesg));
935 return 0;
936}
937
938
939
940
941
942static int ismt_resume(struct pci_dev *pdev)
943{
944 pci_set_power_state(pdev, PCI_D0);
945 pci_restore_state(pdev);
946 return pci_enable_device(pdev);
947}
948
949#else
950
951#define ismt_suspend NULL
952#define ismt_resume NULL
953
954#endif
955
956static struct pci_driver ismt_driver = {
957 .name = "ismt_smbus",
958 .id_table = ismt_ids,
959 .probe = ismt_probe,
960 .remove = ismt_remove,
961 .suspend = ismt_suspend,
962 .resume = ismt_resume,
963};
964
965module_pci_driver(ismt_driver);
966
967MODULE_LICENSE("Dual BSD/GPL");
968MODULE_AUTHOR("Bill E. Brown <bill.e.brown@intel.com>");
969MODULE_DESCRIPTION("Intel SMBus Message Transport (iSMT) driver");
970