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/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
73#define SMBBAR 0
74
75
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
84#define ISMT_MAX_RETRIES 3
85
86
87#define ISMT_DESC_CWRL 0x01
88#define ISMT_DESC_BLK 0X04
89#define ISMT_DESC_FAIR 0x08
90#define ISMT_DESC_PEC 0x10
91#define ISMT_DESC_I2C 0x20
92#define ISMT_DESC_INT 0x40
93#define ISMT_DESC_SOE 0x80
94
95
96#define ISMT_DESC_SCS 0x01
97#define ISMT_DESC_DLTO 0x04
98#define ISMT_DESC_NAK 0x08
99#define ISMT_DESC_CRC 0x10
100#define ISMT_DESC_CLTO 0x20
101#define ISMT_DESC_COL 0x40
102#define ISMT_DESC_LPR 0x80
103
104
105#define ISMT_DESC_ADDR_RW(addr, rw) (((addr) << 1) | (rw))
106
107
108#define ISMT_GR_GCTRL 0x000
109#define ISMT_GR_SMTICL 0x008
110#define ISMT_GR_ERRINTMSK 0x010
111#define ISMT_GR_ERRAERMSK 0x014
112#define ISMT_GR_ERRSTS 0x018
113#define ISMT_GR_ERRINFO 0x01c
114
115
116#define ISMT_MSTR_MDBA 0x100
117#define ISMT_MSTR_MCTRL 0x108
118#define ISMT_MSTR_MSTS 0x10c
119#define ISMT_MSTR_MDS 0x110
120#define ISMT_MSTR_RPOLICY 0x114
121
122
123#define ISMT_SPGT 0x300
124
125
126#define ISMT_GCTRL_TRST 0x04
127#define ISMT_GCTRL_KILL 0x08
128#define ISMT_GCTRL_SRST 0x40
129
130
131#define ISMT_MCTRL_SS 0x01
132#define ISMT_MCTRL_MEIE 0x10
133#define ISMT_MCTRL_FMHP 0x00ff0000
134
135
136#define ISMT_MSTS_HMTP 0xff0000
137#define ISMT_MSTS_MIS 0x20
138#define ISMT_MSTS_MEIS 0x10
139#define ISMT_MSTS_IP 0x01
140
141
142#define ISMT_MDS_MASK 0xff
143
144
145#define ISMT_SPGT_SPD_MASK 0xc0000000
146#define ISMT_SPGT_SPD_80K 0x00
147#define ISMT_SPGT_SPD_100K (0x1 << 30)
148#define ISMT_SPGT_SPD_400K (0x2 << 30)
149#define ISMT_SPGT_SPD_1M (0x3 << 30)
150
151
152
153#define ISMT_MSICTL_MSIE 0x01
154
155
156struct ismt_desc {
157 u8 tgtaddr_rw;
158 u8 wr_len_cmd;
159 u8 rd_len;
160 u8 control;
161 u8 status;
162 u8 retry;
163 u8 rxbytes;
164 u8 txbytes;
165 u32 dptr_low;
166 u32 dptr_high;
167} __packed;
168
169struct ismt_priv {
170 struct i2c_adapter adapter;
171 void __iomem *smba;
172 struct pci_dev *pci_dev;
173 struct ismt_desc *hw;
174 dma_addr_t io_rng_dma;
175 u8 head;
176 struct completion cmp;
177 u8 buffer[I2C_SMBUS_BLOCK_MAX + 16];
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_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
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 = 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
382
383
384
385
386
387
388
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;
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
407 memset(priv->buffer, 0, sizeof(priv->buffer));
408
409
410 memset(desc, 0, sizeof(struct ismt_desc));
411 desc->tgtaddr_rw = ISMT_DESC_ADDR_RW(addr, read_write);
412
413
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
432
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
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
450
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
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
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
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
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
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
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
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
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
549
550
551
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
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
591 ismt_submit_desc(priv);
592
593
594 time_left = wait_for_completion_timeout(&priv->cmp, HZ*1);
595
596
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
607 ret = ismt_process_desc(desc, data, priv, size, read_write);
608
609out:
610
611 priv->head++;
612 priv->head %= ISMT_DESC_ENTRIES;
613
614 return ret;
615}
616
617
618
619
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
635
636
637
638static const struct i2c_algorithm smbus_algorithm = {
639 .smbus_xfer = ismt_access,
640 .functionality = ismt_func,
641};
642
643
644
645
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
657
658
659
660static irqreturn_t ismt_do_interrupt(int vec, void *data)
661{
662 u32 val;
663 struct ismt_priv *priv = data;
664
665
666
667
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
682
683
684
685static irqreturn_t ismt_do_msi_interrupt(int vec, void *data)
686{
687 return ismt_handle_isr(data);
688}
689
690
691
692
693
694static void ismt_hw_init(struct ismt_priv *priv)
695{
696 u32 val;
697 struct device *dev = &priv->pci_dev->dev;
698
699
700 writeq(priv->io_rng_dma, priv->smba + ISMT_MSTR_MDBA);
701
702
703 writel(ISMT_MCTRL_MEIE, priv->smba + ISMT_MSTR_MCTRL);
704
705
706 writel(0, priv->smba + ISMT_MSTR_MSTS);
707
708
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
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
773
774
775static int ismt_dev_init(struct ismt_priv *priv)
776{
777
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
794
795
796static int ismt_int_init(struct ismt_priv *priv)
797{
798 int err;
799
800
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
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
841
842
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
875 pci_set_master(pdev);
876
877
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
941
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