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