GNU Linux-libre 4.9.309-gnu1
[releases.git] / drivers / i2c / busses / i2c-piix4.c
1 /*
2     Copyright (c) 1998 - 2002 Frodo Looijaard <frodol@dds.nl> and
3     Philip Edelbrock <phil@netroedge.com>
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14 */
15
16 /*
17    Supports:
18         Intel PIIX4, 440MX
19         Serverworks OSB4, CSB5, CSB6, HT-1000, HT-1100
20         ATI IXP200, IXP300, IXP400, SB600, SB700/SP5100, SB800
21         AMD Hudson-2, ML, CZ
22         SMSC Victory66
23
24    Note: we assume there can only be one device, with one or more
25    SMBus interfaces.
26    The device can register multiple i2c_adapters (up to PIIX4_MAX_ADAPTERS).
27    For devices supporting multiple ports the i2c_adapter should provide
28    an i2c_algorithm to access them.
29 */
30
31 #include <linux/module.h>
32 #include <linux/moduleparam.h>
33 #include <linux/pci.h>
34 #include <linux/kernel.h>
35 #include <linux/delay.h>
36 #include <linux/stddef.h>
37 #include <linux/ioport.h>
38 #include <linux/i2c.h>
39 #include <linux/slab.h>
40 #include <linux/dmi.h>
41 #include <linux/acpi.h>
42 #include <linux/io.h>
43 #include <linux/mutex.h>
44
45
46 /* PIIX4 SMBus address offsets */
47 #define SMBHSTSTS       (0 + piix4_smba)
48 #define SMBHSLVSTS      (1 + piix4_smba)
49 #define SMBHSTCNT       (2 + piix4_smba)
50 #define SMBHSTCMD       (3 + piix4_smba)
51 #define SMBHSTADD       (4 + piix4_smba)
52 #define SMBHSTDAT0      (5 + piix4_smba)
53 #define SMBHSTDAT1      (6 + piix4_smba)
54 #define SMBBLKDAT       (7 + piix4_smba)
55 #define SMBSLVCNT       (8 + piix4_smba)
56 #define SMBSHDWCMD      (9 + piix4_smba)
57 #define SMBSLVEVT       (0xA + piix4_smba)
58 #define SMBSLVDAT       (0xC + piix4_smba)
59
60 /* count for request_region */
61 #define SMBIOSIZE       9
62
63 /* PCI Address Constants */
64 #define SMBBA           0x090
65 #define SMBHSTCFG       0x0D2
66 #define SMBSLVC         0x0D3
67 #define SMBSHDW1        0x0D4
68 #define SMBSHDW2        0x0D5
69 #define SMBREV          0x0D6
70
71 /* Other settings */
72 #define MAX_TIMEOUT     500
73 #define  ENABLE_INT9    0
74
75 /* PIIX4 constants */
76 #define PIIX4_QUICK             0x00
77 #define PIIX4_BYTE              0x04
78 #define PIIX4_BYTE_DATA         0x08
79 #define PIIX4_WORD_DATA         0x0C
80 #define PIIX4_BLOCK_DATA        0x14
81
82 /* Multi-port constants */
83 #define PIIX4_MAX_ADAPTERS 4
84
85 /* SB800 constants */
86 #define SB800_PIIX4_SMB_IDX             0xcd6
87
88 /*
89  * SB800 port is selected by bits 2:1 of the smb_en register (0x2c)
90  * or the smb_sel register (0x2e), depending on bit 0 of register 0x2f.
91  * Hudson-2/Bolton port is always selected by bits 2:1 of register 0x2f.
92  */
93 #define SB800_PIIX4_PORT_IDX            0x2c
94 #define SB800_PIIX4_PORT_IDX_ALT        0x2e
95 #define SB800_PIIX4_PORT_IDX_SEL        0x2f
96 #define SB800_PIIX4_PORT_IDX_MASK       0x06
97 #define SB800_PIIX4_PORT_IDX_SHIFT      1
98
99 /* On kerncz and Hudson2, SmBus0Sel is at bit 20:19 of PMx00 DecodeEn */
100 #define SB800_PIIX4_PORT_IDX_KERNCZ             0x02
101 #define SB800_PIIX4_PORT_IDX_MASK_KERNCZ        0x18
102 #define SB800_PIIX4_PORT_IDX_SHIFT_KERNCZ       3
103
104 /* insmod parameters */
105
106 /* If force is set to anything different from 0, we forcibly enable the
107    PIIX4. DANGEROUS! */
108 static int force;
109 module_param (force, int, 0);
110 MODULE_PARM_DESC(force, "Forcibly enable the PIIX4. DANGEROUS!");
111
112 /* If force_addr is set to anything different from 0, we forcibly enable
113    the PIIX4 at the given address. VERY DANGEROUS! */
114 static int force_addr;
115 module_param (force_addr, int, 0);
116 MODULE_PARM_DESC(force_addr,
117                  "Forcibly enable the PIIX4 at the given address. "
118                  "EXTREMELY DANGEROUS!");
119
120 static int srvrworks_csb5_delay;
121 static struct pci_driver piix4_driver;
122
123 static const struct dmi_system_id piix4_dmi_blacklist[] = {
124         {
125                 .ident = "Sapphire AM2RD790",
126                 .matches = {
127                         DMI_MATCH(DMI_BOARD_VENDOR, "SAPPHIRE Inc."),
128                         DMI_MATCH(DMI_BOARD_NAME, "PC-AM2RD790"),
129                 },
130         },
131         {
132                 .ident = "DFI Lanparty UT 790FX",
133                 .matches = {
134                         DMI_MATCH(DMI_BOARD_VENDOR, "DFI Inc."),
135                         DMI_MATCH(DMI_BOARD_NAME, "LP UT 790FX"),
136                 },
137         },
138         { }
139 };
140
141 /* The IBM entry is in a separate table because we only check it
142    on Intel-based systems */
143 static const struct dmi_system_id piix4_dmi_ibm[] = {
144         {
145                 .ident = "IBM",
146                 .matches = { DMI_MATCH(DMI_SYS_VENDOR, "IBM"), },
147         },
148         { },
149 };
150
151 /*
152  * SB800 globals
153  * piix4_mutex_sb800 protects piix4_port_sel_sb800 and the pair
154  * of I/O ports at SB800_PIIX4_SMB_IDX.
155  */
156 static DEFINE_MUTEX(piix4_mutex_sb800);
157 static u8 piix4_port_sel_sb800;
158 static u8 piix4_port_mask_sb800;
159 static u8 piix4_port_shift_sb800;
160 static const char *piix4_main_port_names_sb800[PIIX4_MAX_ADAPTERS] = {
161         " port 0", " port 2", " port 3", " port 4"
162 };
163 static const char *piix4_aux_port_name_sb800 = " port 1";
164
165 struct i2c_piix4_adapdata {
166         unsigned short smba;
167
168         /* SB800 */
169         bool sb800_main;
170         u8 port;                /* Port number, shifted */
171 };
172
173 static int piix4_setup(struct pci_dev *PIIX4_dev,
174                        const struct pci_device_id *id)
175 {
176         unsigned char temp;
177         unsigned short piix4_smba;
178
179         if ((PIIX4_dev->vendor == PCI_VENDOR_ID_SERVERWORKS) &&
180             (PIIX4_dev->device == PCI_DEVICE_ID_SERVERWORKS_CSB5))
181                 srvrworks_csb5_delay = 1;
182
183         /* On some motherboards, it was reported that accessing the SMBus
184            caused severe hardware problems */
185         if (dmi_check_system(piix4_dmi_blacklist)) {
186                 dev_err(&PIIX4_dev->dev,
187                         "Accessing the SMBus on this system is unsafe!\n");
188                 return -EPERM;
189         }
190
191         /* Don't access SMBus on IBM systems which get corrupted eeproms */
192         if (dmi_check_system(piix4_dmi_ibm) &&
193                         PIIX4_dev->vendor == PCI_VENDOR_ID_INTEL) {
194                 dev_err(&PIIX4_dev->dev, "IBM system detected; this module "
195                         "may corrupt your serial eeprom! Refusing to load "
196                         "module!\n");
197                 return -EPERM;
198         }
199
200         /* Determine the address of the SMBus areas */
201         if (force_addr) {
202                 piix4_smba = force_addr & 0xfff0;
203                 force = 0;
204         } else {
205                 pci_read_config_word(PIIX4_dev, SMBBA, &piix4_smba);
206                 piix4_smba &= 0xfff0;
207                 if(piix4_smba == 0) {
208                         dev_err(&PIIX4_dev->dev, "SMBus base address "
209                                 "uninitialized - upgrade BIOS or use "
210                                 "force_addr=0xaddr\n");
211                         return -ENODEV;
212                 }
213         }
214
215         if (acpi_check_region(piix4_smba, SMBIOSIZE, piix4_driver.name))
216                 return -ENODEV;
217
218         if (!request_region(piix4_smba, SMBIOSIZE, piix4_driver.name)) {
219                 dev_err(&PIIX4_dev->dev, "SMBus region 0x%x already in use!\n",
220                         piix4_smba);
221                 return -EBUSY;
222         }
223
224         pci_read_config_byte(PIIX4_dev, SMBHSTCFG, &temp);
225
226         /* If force_addr is set, we program the new address here. Just to make
227            sure, we disable the PIIX4 first. */
228         if (force_addr) {
229                 pci_write_config_byte(PIIX4_dev, SMBHSTCFG, temp & 0xfe);
230                 pci_write_config_word(PIIX4_dev, SMBBA, piix4_smba);
231                 pci_write_config_byte(PIIX4_dev, SMBHSTCFG, temp | 0x01);
232                 dev_info(&PIIX4_dev->dev, "WARNING: SMBus interface set to "
233                         "new address %04x!\n", piix4_smba);
234         } else if ((temp & 1) == 0) {
235                 if (force) {
236                         /* This should never need to be done, but has been
237                          * noted that many Dell machines have the SMBus
238                          * interface on the PIIX4 disabled!? NOTE: This assumes
239                          * I/O space and other allocations WERE done by the
240                          * Bios!  Don't complain if your hardware does weird
241                          * things after enabling this. :') Check for Bios
242                          * updates before resorting to this.
243                          */
244                         pci_write_config_byte(PIIX4_dev, SMBHSTCFG,
245                                               temp | 1);
246                         dev_notice(&PIIX4_dev->dev,
247                                    "WARNING: SMBus interface has been FORCEFULLY ENABLED!\n");
248                 } else {
249                         dev_err(&PIIX4_dev->dev,
250                                 "SMBus Host Controller not enabled!\n");
251                         release_region(piix4_smba, SMBIOSIZE);
252                         return -ENODEV;
253                 }
254         }
255
256         if (((temp & 0x0E) == 8) || ((temp & 0x0E) == 2))
257                 dev_dbg(&PIIX4_dev->dev, "Using IRQ for SMBus\n");
258         else if ((temp & 0x0E) == 0)
259                 dev_dbg(&PIIX4_dev->dev, "Using SMI# for SMBus\n");
260         else
261                 dev_err(&PIIX4_dev->dev, "Illegal Interrupt configuration "
262                         "(or code out of date)!\n");
263
264         pci_read_config_byte(PIIX4_dev, SMBREV, &temp);
265         dev_info(&PIIX4_dev->dev,
266                  "SMBus Host Controller at 0x%x, revision %d\n",
267                  piix4_smba, temp);
268
269         return piix4_smba;
270 }
271
272 static int piix4_setup_sb800(struct pci_dev *PIIX4_dev,
273                              const struct pci_device_id *id, u8 aux)
274 {
275         unsigned short piix4_smba;
276         u8 smba_en_lo, smba_en_hi, smb_en, smb_en_status, port_sel;
277         u8 i2ccfg, i2ccfg_offset = 0x10;
278
279         /* SB800 and later SMBus does not support forcing address */
280         if (force || force_addr) {
281                 dev_err(&PIIX4_dev->dev, "SMBus does not support "
282                         "forcing address!\n");
283                 return -EINVAL;
284         }
285
286         /* Determine the address of the SMBus areas */
287         if ((PIIX4_dev->vendor == PCI_VENDOR_ID_AMD &&
288              PIIX4_dev->device == PCI_DEVICE_ID_AMD_HUDSON2_SMBUS &&
289              PIIX4_dev->revision >= 0x41) ||
290             (PIIX4_dev->vendor == PCI_VENDOR_ID_AMD &&
291              PIIX4_dev->device == PCI_DEVICE_ID_AMD_KERNCZ_SMBUS &&
292              PIIX4_dev->revision >= 0x49))
293                 smb_en = 0x00;
294         else
295                 smb_en = (aux) ? 0x28 : 0x2c;
296
297         mutex_lock(&piix4_mutex_sb800);
298         outb_p(smb_en, SB800_PIIX4_SMB_IDX);
299         smba_en_lo = inb_p(SB800_PIIX4_SMB_IDX + 1);
300         outb_p(smb_en + 1, SB800_PIIX4_SMB_IDX);
301         smba_en_hi = inb_p(SB800_PIIX4_SMB_IDX + 1);
302         mutex_unlock(&piix4_mutex_sb800);
303
304         if (!smb_en) {
305                 smb_en_status = smba_en_lo & 0x10;
306                 piix4_smba = smba_en_hi << 8;
307                 if (aux)
308                         piix4_smba |= 0x20;
309         } else {
310                 smb_en_status = smba_en_lo & 0x01;
311                 piix4_smba = ((smba_en_hi << 8) | smba_en_lo) & 0xffe0;
312         }
313
314         if (!smb_en_status) {
315                 dev_err(&PIIX4_dev->dev,
316                         "SMBus Host Controller not enabled!\n");
317                 return -ENODEV;
318         }
319
320         if (acpi_check_region(piix4_smba, SMBIOSIZE, piix4_driver.name))
321                 return -ENODEV;
322
323         if (!request_region(piix4_smba, SMBIOSIZE, piix4_driver.name)) {
324                 dev_err(&PIIX4_dev->dev, "SMBus region 0x%x already in use!\n",
325                         piix4_smba);
326                 return -EBUSY;
327         }
328
329         /* Aux SMBus does not support IRQ information */
330         if (aux) {
331                 dev_info(&PIIX4_dev->dev,
332                          "Auxiliary SMBus Host Controller at 0x%x\n",
333                          piix4_smba);
334                 return piix4_smba;
335         }
336
337         /* Request the SMBus I2C bus config region */
338         if (!request_region(piix4_smba + i2ccfg_offset, 1, "i2ccfg")) {
339                 dev_err(&PIIX4_dev->dev, "SMBus I2C bus config region "
340                         "0x%x already in use!\n", piix4_smba + i2ccfg_offset);
341                 release_region(piix4_smba, SMBIOSIZE);
342                 return -EBUSY;
343         }
344         i2ccfg = inb_p(piix4_smba + i2ccfg_offset);
345         release_region(piix4_smba + i2ccfg_offset, 1);
346
347         if (i2ccfg & 1)
348                 dev_dbg(&PIIX4_dev->dev, "Using IRQ for SMBus\n");
349         else
350                 dev_dbg(&PIIX4_dev->dev, "Using SMI# for SMBus\n");
351
352         dev_info(&PIIX4_dev->dev,
353                  "SMBus Host Controller at 0x%x, revision %d\n",
354                  piix4_smba, i2ccfg >> 4);
355
356         /* Find which register is used for port selection */
357         if (PIIX4_dev->vendor == PCI_VENDOR_ID_AMD) {
358                 if (PIIX4_dev->device == PCI_DEVICE_ID_AMD_KERNCZ_SMBUS ||
359                     (PIIX4_dev->device == PCI_DEVICE_ID_AMD_HUDSON2_SMBUS &&
360                      PIIX4_dev->revision >= 0x1F)) {
361                         piix4_port_sel_sb800 = SB800_PIIX4_PORT_IDX_KERNCZ;
362                         piix4_port_mask_sb800 = SB800_PIIX4_PORT_IDX_MASK_KERNCZ;
363                         piix4_port_shift_sb800 = SB800_PIIX4_PORT_IDX_SHIFT_KERNCZ;
364                 } else {
365                         piix4_port_sel_sb800 = SB800_PIIX4_PORT_IDX_ALT;
366                         piix4_port_mask_sb800 = SB800_PIIX4_PORT_IDX_MASK;
367                         piix4_port_shift_sb800 = SB800_PIIX4_PORT_IDX_SHIFT;
368                 }
369         } else {
370                 mutex_lock(&piix4_mutex_sb800);
371                 outb_p(SB800_PIIX4_PORT_IDX_SEL, SB800_PIIX4_SMB_IDX);
372                 port_sel = inb_p(SB800_PIIX4_SMB_IDX + 1);
373                 piix4_port_sel_sb800 = (port_sel & 0x01) ?
374                                        SB800_PIIX4_PORT_IDX_ALT :
375                                        SB800_PIIX4_PORT_IDX;
376                 piix4_port_mask_sb800 = SB800_PIIX4_PORT_IDX_MASK;
377                 piix4_port_shift_sb800 = SB800_PIIX4_PORT_IDX_SHIFT;
378                 mutex_unlock(&piix4_mutex_sb800);
379         }
380
381         dev_info(&PIIX4_dev->dev,
382                  "Using register 0x%02x for SMBus port selection\n",
383                  (unsigned int)piix4_port_sel_sb800);
384
385         return piix4_smba;
386 }
387
388 static int piix4_setup_aux(struct pci_dev *PIIX4_dev,
389                            const struct pci_device_id *id,
390                            unsigned short base_reg_addr)
391 {
392         /* Set up auxiliary SMBus controllers found on some
393          * AMD chipsets e.g. SP5100 (SB700 derivative) */
394
395         unsigned short piix4_smba;
396
397         /* Read address of auxiliary SMBus controller */
398         pci_read_config_word(PIIX4_dev, base_reg_addr, &piix4_smba);
399         if ((piix4_smba & 1) == 0) {
400                 dev_dbg(&PIIX4_dev->dev,
401                         "Auxiliary SMBus controller not enabled\n");
402                 return -ENODEV;
403         }
404
405         piix4_smba &= 0xfff0;
406         if (piix4_smba == 0) {
407                 dev_dbg(&PIIX4_dev->dev,
408                         "Auxiliary SMBus base address uninitialized\n");
409                 return -ENODEV;
410         }
411
412         if (acpi_check_region(piix4_smba, SMBIOSIZE, piix4_driver.name))
413                 return -ENODEV;
414
415         if (!request_region(piix4_smba, SMBIOSIZE, piix4_driver.name)) {
416                 dev_err(&PIIX4_dev->dev, "Auxiliary SMBus region 0x%x "
417                         "already in use!\n", piix4_smba);
418                 return -EBUSY;
419         }
420
421         dev_info(&PIIX4_dev->dev,
422                  "Auxiliary SMBus Host Controller at 0x%x\n",
423                  piix4_smba);
424
425         return piix4_smba;
426 }
427
428 static int piix4_transaction(struct i2c_adapter *piix4_adapter)
429 {
430         struct i2c_piix4_adapdata *adapdata = i2c_get_adapdata(piix4_adapter);
431         unsigned short piix4_smba = adapdata->smba;
432         int temp;
433         int result = 0;
434         int timeout = 0;
435
436         dev_dbg(&piix4_adapter->dev, "Transaction (pre): CNT=%02x, CMD=%02x, "
437                 "ADD=%02x, DAT0=%02x, DAT1=%02x\n", inb_p(SMBHSTCNT),
438                 inb_p(SMBHSTCMD), inb_p(SMBHSTADD), inb_p(SMBHSTDAT0),
439                 inb_p(SMBHSTDAT1));
440
441         /* Make sure the SMBus host is ready to start transmitting */
442         if ((temp = inb_p(SMBHSTSTS)) != 0x00) {
443                 dev_dbg(&piix4_adapter->dev, "SMBus busy (%02x). "
444                         "Resetting...\n", temp);
445                 outb_p(temp, SMBHSTSTS);
446                 if ((temp = inb_p(SMBHSTSTS)) != 0x00) {
447                         dev_err(&piix4_adapter->dev, "Failed! (%02x)\n", temp);
448                         return -EBUSY;
449                 } else {
450                         dev_dbg(&piix4_adapter->dev, "Successful!\n");
451                 }
452         }
453
454         /* start the transaction by setting bit 6 */
455         outb_p(inb(SMBHSTCNT) | 0x040, SMBHSTCNT);
456
457         /* We will always wait for a fraction of a second! (See PIIX4 docs errata) */
458         if (srvrworks_csb5_delay) /* Extra delay for SERVERWORKS_CSB5 */
459                 msleep(2);
460         else
461                 msleep(1);
462
463         while ((++timeout < MAX_TIMEOUT) &&
464                ((temp = inb_p(SMBHSTSTS)) & 0x01))
465                 msleep(1);
466
467         /* If the SMBus is still busy, we give up */
468         if (timeout == MAX_TIMEOUT) {
469                 dev_err(&piix4_adapter->dev, "SMBus Timeout!\n");
470                 result = -ETIMEDOUT;
471         }
472
473         if (temp & 0x10) {
474                 result = -EIO;
475                 dev_err(&piix4_adapter->dev, "Error: Failed bus transaction\n");
476         }
477
478         if (temp & 0x08) {
479                 result = -EIO;
480                 dev_dbg(&piix4_adapter->dev, "Bus collision! SMBus may be "
481                         "locked until next hard reset. (sorry!)\n");
482                 /* Clock stops and slave is stuck in mid-transmission */
483         }
484
485         if (temp & 0x04) {
486                 result = -ENXIO;
487                 dev_dbg(&piix4_adapter->dev, "Error: no response!\n");
488         }
489
490         if (inb_p(SMBHSTSTS) != 0x00)
491                 outb_p(inb(SMBHSTSTS), SMBHSTSTS);
492
493         if ((temp = inb_p(SMBHSTSTS)) != 0x00) {
494                 dev_err(&piix4_adapter->dev, "Failed reset at end of "
495                         "transaction (%02x)\n", temp);
496         }
497         dev_dbg(&piix4_adapter->dev, "Transaction (post): CNT=%02x, CMD=%02x, "
498                 "ADD=%02x, DAT0=%02x, DAT1=%02x\n", inb_p(SMBHSTCNT),
499                 inb_p(SMBHSTCMD), inb_p(SMBHSTADD), inb_p(SMBHSTDAT0),
500                 inb_p(SMBHSTDAT1));
501         return result;
502 }
503
504 /* Return negative errno on error. */
505 static s32 piix4_access(struct i2c_adapter * adap, u16 addr,
506                  unsigned short flags, char read_write,
507                  u8 command, int size, union i2c_smbus_data * data)
508 {
509         struct i2c_piix4_adapdata *adapdata = i2c_get_adapdata(adap);
510         unsigned short piix4_smba = adapdata->smba;
511         int i, len;
512         int status;
513
514         switch (size) {
515         case I2C_SMBUS_QUICK:
516                 outb_p((addr << 1) | read_write,
517                        SMBHSTADD);
518                 size = PIIX4_QUICK;
519                 break;
520         case I2C_SMBUS_BYTE:
521                 outb_p((addr << 1) | read_write,
522                        SMBHSTADD);
523                 if (read_write == I2C_SMBUS_WRITE)
524                         outb_p(command, SMBHSTCMD);
525                 size = PIIX4_BYTE;
526                 break;
527         case I2C_SMBUS_BYTE_DATA:
528                 outb_p((addr << 1) | read_write,
529                        SMBHSTADD);
530                 outb_p(command, SMBHSTCMD);
531                 if (read_write == I2C_SMBUS_WRITE)
532                         outb_p(data->byte, SMBHSTDAT0);
533                 size = PIIX4_BYTE_DATA;
534                 break;
535         case I2C_SMBUS_WORD_DATA:
536                 outb_p((addr << 1) | read_write,
537                        SMBHSTADD);
538                 outb_p(command, SMBHSTCMD);
539                 if (read_write == I2C_SMBUS_WRITE) {
540                         outb_p(data->word & 0xff, SMBHSTDAT0);
541                         outb_p((data->word & 0xff00) >> 8, SMBHSTDAT1);
542                 }
543                 size = PIIX4_WORD_DATA;
544                 break;
545         case I2C_SMBUS_BLOCK_DATA:
546                 outb_p((addr << 1) | read_write,
547                        SMBHSTADD);
548                 outb_p(command, SMBHSTCMD);
549                 if (read_write == I2C_SMBUS_WRITE) {
550                         len = data->block[0];
551                         if (len == 0 || len > I2C_SMBUS_BLOCK_MAX)
552                                 return -EINVAL;
553                         outb_p(len, SMBHSTDAT0);
554                         inb_p(SMBHSTCNT);       /* Reset SMBBLKDAT */
555                         for (i = 1; i <= len; i++)
556                                 outb_p(data->block[i], SMBBLKDAT);
557                 }
558                 size = PIIX4_BLOCK_DATA;
559                 break;
560         default:
561                 dev_warn(&adap->dev, "Unsupported transaction %d\n", size);
562                 return -EOPNOTSUPP;
563         }
564
565         outb_p((size & 0x1C) + (ENABLE_INT9 & 1), SMBHSTCNT);
566
567         status = piix4_transaction(adap);
568         if (status)
569                 return status;
570
571         if ((read_write == I2C_SMBUS_WRITE) || (size == PIIX4_QUICK))
572                 return 0;
573
574
575         switch (size) {
576         case PIIX4_BYTE:
577         case PIIX4_BYTE_DATA:
578                 data->byte = inb_p(SMBHSTDAT0);
579                 break;
580         case PIIX4_WORD_DATA:
581                 data->word = inb_p(SMBHSTDAT0) + (inb_p(SMBHSTDAT1) << 8);
582                 break;
583         case PIIX4_BLOCK_DATA:
584                 data->block[0] = inb_p(SMBHSTDAT0);
585                 if (data->block[0] == 0 || data->block[0] > I2C_SMBUS_BLOCK_MAX)
586                         return -EPROTO;
587                 inb_p(SMBHSTCNT);       /* Reset SMBBLKDAT */
588                 for (i = 1; i <= data->block[0]; i++)
589                         data->block[i] = inb_p(SMBBLKDAT);
590                 break;
591         }
592         return 0;
593 }
594
595 /*
596  * Handles access to multiple SMBus ports on the SB800.
597  * The port is selected by bits 2:1 of the smb_en register (0x2c).
598  * Returns negative errno on error.
599  *
600  * Note: The selected port must be returned to the initial selection to avoid
601  * problems on certain systems.
602  */
603 static s32 piix4_access_sb800(struct i2c_adapter *adap, u16 addr,
604                  unsigned short flags, char read_write,
605                  u8 command, int size, union i2c_smbus_data *data)
606 {
607         struct i2c_piix4_adapdata *adapdata = i2c_get_adapdata(adap);
608         unsigned short piix4_smba = adapdata->smba;
609         int retries = MAX_TIMEOUT;
610         int smbslvcnt;
611         u8 smba_en_lo;
612         u8 port;
613         int retval;
614
615         mutex_lock(&piix4_mutex_sb800);
616
617         /* Request the SMBUS semaphore, avoid conflicts with the IMC */
618         smbslvcnt  = inb_p(SMBSLVCNT);
619         do {
620                 outb_p(smbslvcnt | 0x10, SMBSLVCNT);
621
622                 /* Check the semaphore status */
623                 smbslvcnt  = inb_p(SMBSLVCNT);
624                 if (smbslvcnt & 0x10)
625                         break;
626
627                 usleep_range(1000, 2000);
628         } while (--retries);
629         /* SMBus is still owned by the IMC, we give up */
630         if (!retries) {
631                 mutex_unlock(&piix4_mutex_sb800);
632                 return -EBUSY;
633         }
634
635         outb_p(piix4_port_sel_sb800, SB800_PIIX4_SMB_IDX);
636         smba_en_lo = inb_p(SB800_PIIX4_SMB_IDX + 1);
637
638         port = adapdata->port;
639         if ((smba_en_lo & piix4_port_mask_sb800) != port)
640                 outb_p((smba_en_lo & ~piix4_port_mask_sb800) | port,
641                        SB800_PIIX4_SMB_IDX + 1);
642
643         retval = piix4_access(adap, addr, flags, read_write,
644                               command, size, data);
645
646         outb_p(smba_en_lo, SB800_PIIX4_SMB_IDX + 1);
647
648         /* Release the semaphore */
649         outb_p(smbslvcnt | 0x20, SMBSLVCNT);
650
651         mutex_unlock(&piix4_mutex_sb800);
652
653         return retval;
654 }
655
656 static u32 piix4_func(struct i2c_adapter *adapter)
657 {
658         return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
659             I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
660             I2C_FUNC_SMBUS_BLOCK_DATA;
661 }
662
663 static const struct i2c_algorithm smbus_algorithm = {
664         .smbus_xfer     = piix4_access,
665         .functionality  = piix4_func,
666 };
667
668 static const struct i2c_algorithm piix4_smbus_algorithm_sb800 = {
669         .smbus_xfer     = piix4_access_sb800,
670         .functionality  = piix4_func,
671 };
672
673 static const struct pci_device_id piix4_ids[] = {
674         { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82371AB_3) },
675         { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82443MX_3) },
676         { PCI_DEVICE(PCI_VENDOR_ID_EFAR, PCI_DEVICE_ID_EFAR_SLC90E66_3) },
677         { PCI_DEVICE(PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_IXP200_SMBUS) },
678         { PCI_DEVICE(PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_IXP300_SMBUS) },
679         { PCI_DEVICE(PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_IXP400_SMBUS) },
680         { PCI_DEVICE(PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_SBX00_SMBUS) },
681         { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_HUDSON2_SMBUS) },
682         { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_KERNCZ_SMBUS) },
683         { PCI_DEVICE(PCI_VENDOR_ID_SERVERWORKS,
684                      PCI_DEVICE_ID_SERVERWORKS_OSB4) },
685         { PCI_DEVICE(PCI_VENDOR_ID_SERVERWORKS,
686                      PCI_DEVICE_ID_SERVERWORKS_CSB5) },
687         { PCI_DEVICE(PCI_VENDOR_ID_SERVERWORKS,
688                      PCI_DEVICE_ID_SERVERWORKS_CSB6) },
689         { PCI_DEVICE(PCI_VENDOR_ID_SERVERWORKS,
690                      PCI_DEVICE_ID_SERVERWORKS_HT1000SB) },
691         { PCI_DEVICE(PCI_VENDOR_ID_SERVERWORKS,
692                      PCI_DEVICE_ID_SERVERWORKS_HT1100LD) },
693         { 0, }
694 };
695
696 MODULE_DEVICE_TABLE (pci, piix4_ids);
697
698 static struct i2c_adapter *piix4_main_adapters[PIIX4_MAX_ADAPTERS];
699 static struct i2c_adapter *piix4_aux_adapter;
700
701 static int piix4_add_adapter(struct pci_dev *dev, unsigned short smba,
702                              bool sb800_main, u8 port,
703                              const char *name, struct i2c_adapter **padap)
704 {
705         struct i2c_adapter *adap;
706         struct i2c_piix4_adapdata *adapdata;
707         int retval;
708
709         adap = kzalloc(sizeof(*adap), GFP_KERNEL);
710         if (adap == NULL) {
711                 release_region(smba, SMBIOSIZE);
712                 return -ENOMEM;
713         }
714
715         adap->owner = THIS_MODULE;
716         adap->class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
717         adap->algo = sb800_main ? &piix4_smbus_algorithm_sb800
718                                 : &smbus_algorithm;
719
720         adapdata = kzalloc(sizeof(*adapdata), GFP_KERNEL);
721         if (adapdata == NULL) {
722                 kfree(adap);
723                 release_region(smba, SMBIOSIZE);
724                 return -ENOMEM;
725         }
726
727         adapdata->smba = smba;
728         adapdata->sb800_main = sb800_main;
729         adapdata->port = port << piix4_port_shift_sb800;
730
731         /* set up the sysfs linkage to our parent device */
732         adap->dev.parent = &dev->dev;
733
734         snprintf(adap->name, sizeof(adap->name),
735                 "SMBus PIIX4 adapter%s at %04x", name, smba);
736
737         i2c_set_adapdata(adap, adapdata);
738
739         retval = i2c_add_adapter(adap);
740         if (retval) {
741                 kfree(adapdata);
742                 kfree(adap);
743                 release_region(smba, SMBIOSIZE);
744                 return retval;
745         }
746
747         *padap = adap;
748         return 0;
749 }
750
751 static int piix4_add_adapters_sb800(struct pci_dev *dev, unsigned short smba)
752 {
753         struct i2c_piix4_adapdata *adapdata;
754         int port;
755         int retval;
756
757         for (port = 0; port < PIIX4_MAX_ADAPTERS; port++) {
758                 retval = piix4_add_adapter(dev, smba, true, port,
759                                            piix4_main_port_names_sb800[port],
760                                            &piix4_main_adapters[port]);
761                 if (retval < 0)
762                         goto error;
763         }
764
765         return retval;
766
767 error:
768         dev_err(&dev->dev,
769                 "Error setting up SB800 adapters. Unregistering!\n");
770         while (--port >= 0) {
771                 adapdata = i2c_get_adapdata(piix4_main_adapters[port]);
772                 if (adapdata->smba) {
773                         i2c_del_adapter(piix4_main_adapters[port]);
774                         kfree(adapdata);
775                         kfree(piix4_main_adapters[port]);
776                         piix4_main_adapters[port] = NULL;
777                 }
778         }
779
780         return retval;
781 }
782
783 static int piix4_probe(struct pci_dev *dev, const struct pci_device_id *id)
784 {
785         int retval;
786         bool is_sb800 = false;
787
788         if ((dev->vendor == PCI_VENDOR_ID_ATI &&
789              dev->device == PCI_DEVICE_ID_ATI_SBX00_SMBUS &&
790              dev->revision >= 0x40) ||
791             dev->vendor == PCI_VENDOR_ID_AMD) {
792                 is_sb800 = true;
793
794                 if (!request_region(SB800_PIIX4_SMB_IDX, 2, "smba_idx")) {
795                         dev_err(&dev->dev,
796                         "SMBus base address index region 0x%x already in use!\n",
797                         SB800_PIIX4_SMB_IDX);
798                         return -EBUSY;
799                 }
800
801                 /* base address location etc changed in SB800 */
802                 retval = piix4_setup_sb800(dev, id, 0);
803                 if (retval < 0) {
804                         release_region(SB800_PIIX4_SMB_IDX, 2);
805                         return retval;
806                 }
807
808                 /*
809                  * Try to register multiplexed main SMBus adapter,
810                  * give up if we can't
811                  */
812                 retval = piix4_add_adapters_sb800(dev, retval);
813                 if (retval < 0) {
814                         release_region(SB800_PIIX4_SMB_IDX, 2);
815                         return retval;
816                 }
817         } else {
818                 retval = piix4_setup(dev, id);
819                 if (retval < 0)
820                         return retval;
821
822                 /* Try to register main SMBus adapter, give up if we can't */
823                 retval = piix4_add_adapter(dev, retval, false, 0, "",
824                                            &piix4_main_adapters[0]);
825                 if (retval < 0)
826                         return retval;
827         }
828
829         /* Check for auxiliary SMBus on some AMD chipsets */
830         retval = -ENODEV;
831
832         if (dev->vendor == PCI_VENDOR_ID_ATI &&
833             dev->device == PCI_DEVICE_ID_ATI_SBX00_SMBUS) {
834                 if (dev->revision < 0x40) {
835                         retval = piix4_setup_aux(dev, id, 0x58);
836                 } else {
837                         /* SB800 added aux bus too */
838                         retval = piix4_setup_sb800(dev, id, 1);
839                 }
840         }
841
842         if (dev->vendor == PCI_VENDOR_ID_AMD &&
843             (dev->device == PCI_DEVICE_ID_AMD_HUDSON2_SMBUS ||
844              dev->device == PCI_DEVICE_ID_AMD_KERNCZ_SMBUS)) {
845                 retval = piix4_setup_sb800(dev, id, 1);
846         }
847
848         if (retval > 0) {
849                 /* Try to add the aux adapter if it exists,
850                  * piix4_add_adapter will clean up if this fails */
851                 piix4_add_adapter(dev, retval, false, 0,
852                                   is_sb800 ? piix4_aux_port_name_sb800 : "",
853                                   &piix4_aux_adapter);
854         }
855
856         return 0;
857 }
858
859 static void piix4_adap_remove(struct i2c_adapter *adap)
860 {
861         struct i2c_piix4_adapdata *adapdata = i2c_get_adapdata(adap);
862
863         if (adapdata->smba) {
864                 i2c_del_adapter(adap);
865                 if (adapdata->port == (0 << 1)) {
866                         release_region(adapdata->smba, SMBIOSIZE);
867                         if (adapdata->sb800_main)
868                                 release_region(SB800_PIIX4_SMB_IDX, 2);
869                 }
870                 kfree(adapdata);
871                 kfree(adap);
872         }
873 }
874
875 static void piix4_remove(struct pci_dev *dev)
876 {
877         int port = PIIX4_MAX_ADAPTERS;
878
879         while (--port >= 0) {
880                 if (piix4_main_adapters[port]) {
881                         piix4_adap_remove(piix4_main_adapters[port]);
882                         piix4_main_adapters[port] = NULL;
883                 }
884         }
885
886         if (piix4_aux_adapter) {
887                 piix4_adap_remove(piix4_aux_adapter);
888                 piix4_aux_adapter = NULL;
889         }
890 }
891
892 static struct pci_driver piix4_driver = {
893         .name           = "piix4_smbus",
894         .id_table       = piix4_ids,
895         .probe          = piix4_probe,
896         .remove         = piix4_remove,
897 };
898
899 module_pci_driver(piix4_driver);
900
901 MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl> and "
902                 "Philip Edelbrock <phil@netroedge.com>");
903 MODULE_DESCRIPTION("PIIX4 SMBus driver");
904 MODULE_LICENSE("GPL");