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