patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / drivers / i2c / busses / i2c-viapro.c
1 /*
2     i2c-viapro.c - Part of lm_sensors, Linux kernel modules for hardware
3               monitoring
4     Copyright (c) 1998 - 2002  Frodo Looijaard <frodol@dds.nl>, 
5     Philip Edelbrock <phil@netroedge.com>, Kyösti Mälkki <kmalkki@cc.hut.fi>,
6     Mark D. Studebaker <mdsxyz123@yahoo.com>
7
8     This program is free software; you can redistribute it and/or modify
9     it under the terms of the GNU General Public License as published by
10     the Free Software Foundation; either version 2 of the License, or
11     (at your option) any later version.
12
13     This program is distributed in the hope that it will be useful,
14     but WITHOUT ANY WARRANTY; without even the implied warranty of
15     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16     GNU General Public License for more details.
17
18     You should have received a copy of the GNU General Public License
19     along with this program; if not, write to the Free Software
20     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 /*
24    Supports Via devices:
25         82C596A/B (0x3050)
26         82C596B (0x3051)
27         82C686A/B
28         8231
29         8233
30         8233A (0x3147 and 0x3177)
31         8235
32         8237
33    Note: we assume there can only be one device, with one SMBus interface.
34 */
35
36 #include <linux/config.h>
37 #include <linux/module.h>
38 #include <linux/delay.h>
39 #include <linux/pci.h>
40 #include <linux/kernel.h>
41 #include <linux/stddef.h>
42 #include <linux/sched.h>
43 #include <linux/ioport.h>
44 #include <linux/i2c.h>
45 #include <linux/init.h>
46 #include <asm/io.h>
47
48 #define SMBBA1           0x90
49 #define SMBBA2           0x80
50 #define SMBBA3           0xD0
51
52 /* SMBus address offsets */
53 static unsigned short vt596_smba;
54 #define SMBHSTSTS       (vt596_smba + 0)
55 #define SMBHSLVSTS      (vt596_smba + 1)
56 #define SMBHSTCNT       (vt596_smba + 2)
57 #define SMBHSTCMD       (vt596_smba + 3)
58 #define SMBHSTADD       (vt596_smba + 4)
59 #define SMBHSTDAT0      (vt596_smba + 5)
60 #define SMBHSTDAT1      (vt596_smba + 6)
61 #define SMBBLKDAT       (vt596_smba + 7)
62 #define SMBSLVCNT       (vt596_smba + 8)
63 #define SMBSHDWCMD      (vt596_smba + 9)
64 #define SMBSLVEVT       (vt596_smba + 0xA)
65 #define SMBSLVDAT       (vt596_smba + 0xC)
66
67 /* PCI Address Constants */
68
69 /* SMBus data in configuration space can be found in two places,
70    We try to select the better one*/
71
72 static unsigned short smb_cf_hstcfg = 0xD2;
73
74 #define SMBHSTCFG   (smb_cf_hstcfg)
75 #define SMBSLVC     (smb_cf_hstcfg + 1)
76 #define SMBSHDW1    (smb_cf_hstcfg + 2)
77 #define SMBSHDW2    (smb_cf_hstcfg + 3)
78 #define SMBREV      (smb_cf_hstcfg + 4)
79
80 /* Other settings */
81 #define MAX_TIMEOUT     500
82 #define ENABLE_INT9     0
83
84 /* VT82C596 constants */
85 #define VT596_QUICK      0x00
86 #define VT596_BYTE       0x04
87 #define VT596_BYTE_DATA  0x08
88 #define VT596_WORD_DATA  0x0C
89 #define VT596_BLOCK_DATA 0x14
90
91
92 /* If force is set to anything different from 0, we forcibly enable the
93    VT596. DANGEROUS! */
94 static int force;
95 MODULE_PARM(force, "i");
96 MODULE_PARM_DESC(force, "Forcibly enable the SMBus. DANGEROUS!");
97
98 /* If force_addr is set to anything different from 0, we forcibly enable
99    the VT596 at the given address. VERY DANGEROUS! */
100 static int force_addr;
101 MODULE_PARM(force_addr, "i");
102 MODULE_PARM_DESC(force_addr,
103                  "Forcibly enable the SMBus at the given address. "
104                  "EXTREMELY DANGEROUS!");
105
106
107 static struct i2c_adapter vt596_adapter;
108
109 /* Another internally used function */
110 static int vt596_transaction(void)
111 {
112         int temp;
113         int result = 0;
114         int timeout = 0;
115
116         dev_dbg(&vt596_adapter.dev, "Transaction (pre): CNT=%02x, CMD=%02x, "
117                 "ADD=%02x, DAT0=%02x, DAT1=%02x\n", inb_p(SMBHSTCNT), 
118                 inb_p(SMBHSTCMD), inb_p(SMBHSTADD), inb_p(SMBHSTDAT0), 
119                 inb_p(SMBHSTDAT1));
120
121         /* Make sure the SMBus host is ready to start transmitting */
122         if ((temp = inb_p(SMBHSTSTS)) != 0x00) {
123                 dev_dbg(&vt596_adapter.dev, "SMBus busy (0x%02x). "
124                                 "Resetting...\n", temp);
125                 
126                 outb_p(temp, SMBHSTSTS);
127                 if ((temp = inb_p(SMBHSTSTS)) != 0x00) {
128                         dev_dbg(&vt596_adapter.dev, "Failed! (0x%02x)\n", temp);
129                         
130                         return -1;
131                 } else {
132                         dev_dbg(&vt596_adapter.dev, "Successfull!\n");
133                 }
134         }
135
136         /* start the transaction by setting bit 6 */
137         outb_p(inb(SMBHSTCNT) | 0x040, SMBHSTCNT);
138
139         /* We will always wait for a fraction of a second! 
140            I don't know if VIA needs this, Intel did  */
141         do {
142                 msleep(1);
143                 temp = inb_p(SMBHSTSTS);
144         } while ((temp & 0x01) && (timeout++ < MAX_TIMEOUT));
145
146         /* If the SMBus is still busy, we give up */
147         if (timeout >= MAX_TIMEOUT) {
148                 result = -1;
149                 dev_dbg(&vt596_adapter.dev, "SMBus Timeout!\n");
150         }
151
152         if (temp & 0x10) {
153                 result = -1;
154                 dev_dbg(&vt596_adapter.dev, "Error: Failed bus transaction\n");
155         }
156
157         if (temp & 0x08) {
158                 result = -1;
159                 dev_info(&vt596_adapter.dev, "Bus collision! SMBus may be "
160                         "locked until next hard\nreset. (sorry!)\n");
161                 /* Clock stops and slave is stuck in mid-transmission */
162         }
163
164         if (temp & 0x04) {
165                 result = -1;
166                 dev_dbg(&vt596_adapter.dev, "Error: no response!\n");
167         }
168
169         if (inb_p(SMBHSTSTS) != 0x00)
170                 outb_p(inb(SMBHSTSTS), SMBHSTSTS);
171
172         if ((temp = inb_p(SMBHSTSTS)) != 0x00) {
173                 dev_dbg(&vt596_adapter.dev, "Failed reset at end of "
174                         "transaction (%02x)\n", temp);
175         }
176         dev_dbg(&vt596_adapter.dev, "Transaction (post): CNT=%02x, CMD=%02x, "
177                 "ADD=%02x, DAT0=%02x, DAT1=%02x\n", inb_p(SMBHSTCNT),
178                 inb_p(SMBHSTCMD), inb_p(SMBHSTADD), inb_p(SMBHSTDAT0), 
179                 inb_p(SMBHSTDAT1));
180         
181         return result;
182 }
183
184 /* Return -1 on error. */
185 static s32 vt596_access(struct i2c_adapter *adap, u16 addr,
186                 unsigned short flags,  char read_write, u8 command,
187                 int size,  union i2c_smbus_data *data)
188 {
189         int i, len;
190
191         switch (size) {
192         case I2C_SMBUS_PROC_CALL:
193                 dev_info(&vt596_adapter.dev,
194                          "I2C_SMBUS_PROC_CALL not supported!\n");
195                 return -1;
196         case I2C_SMBUS_QUICK:
197                 outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
198                        SMBHSTADD);
199                 size = VT596_QUICK;
200                 break;
201         case I2C_SMBUS_BYTE:
202                 outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
203                        SMBHSTADD);
204                 if (read_write == I2C_SMBUS_WRITE)
205                         outb_p(command, SMBHSTCMD);
206                 size = VT596_BYTE;
207                 break;
208         case I2C_SMBUS_BYTE_DATA:
209                 outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
210                        SMBHSTADD);
211                 outb_p(command, SMBHSTCMD);
212                 if (read_write == I2C_SMBUS_WRITE)
213                         outb_p(data->byte, SMBHSTDAT0);
214                 size = VT596_BYTE_DATA;
215                 break;
216         case I2C_SMBUS_WORD_DATA:
217                 outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
218                        SMBHSTADD);
219                 outb_p(command, SMBHSTCMD);
220                 if (read_write == I2C_SMBUS_WRITE) {
221                         outb_p(data->word & 0xff, SMBHSTDAT0);
222                         outb_p((data->word & 0xff00) >> 8, SMBHSTDAT1);
223                 }
224                 size = VT596_WORD_DATA;
225                 break;
226         case I2C_SMBUS_BLOCK_DATA:
227                 outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
228                        SMBHSTADD);
229                 outb_p(command, SMBHSTCMD);
230                 if (read_write == I2C_SMBUS_WRITE) {
231                         len = data->block[0];
232                         if (len < 0)
233                                 len = 0;
234                         if (len > 32)
235                                 len = 32;
236                         outb_p(len, SMBHSTDAT0);
237                         i = inb_p(SMBHSTCNT);   /* Reset SMBBLKDAT */
238                         for (i = 1; i <= len; i++)
239                                 outb_p(data->block[i], SMBBLKDAT);
240                 }
241                 size = VT596_BLOCK_DATA;
242                 break;
243         }
244
245         outb_p((size & 0x1C) + (ENABLE_INT9 & 1), SMBHSTCNT);
246
247         if (vt596_transaction()) /* Error in transaction */
248                 return -1;
249
250         if ((read_write == I2C_SMBUS_WRITE) || (size == VT596_QUICK))
251                 return 0;
252
253         switch (size) {
254         case VT596_BYTE:
255                 /* Where is the result put? I assume here it is in
256                  * SMBHSTDAT0 but it might just as well be in the
257                  * SMBHSTCMD. No clue in the docs 
258                  */
259                 data->byte = inb_p(SMBHSTDAT0);
260                 break;
261         case VT596_BYTE_DATA:
262                 data->byte = inb_p(SMBHSTDAT0);
263                 break;
264         case VT596_WORD_DATA:
265                 data->word = inb_p(SMBHSTDAT0) + (inb_p(SMBHSTDAT1) << 8);
266                 break;
267         case VT596_BLOCK_DATA:
268                 data->block[0] = inb_p(SMBHSTDAT0);
269                 i = inb_p(SMBHSTCNT);   /* Reset SMBBLKDAT */
270                 for (i = 1; i <= data->block[0]; i++)
271                         data->block[i] = inb_p(SMBBLKDAT);
272                 break;
273         }
274         return 0;
275 }
276
277 static u32 vt596_func(struct i2c_adapter *adapter)
278 {
279         return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
280             I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
281             I2C_FUNC_SMBUS_BLOCK_DATA;
282 }
283
284 static struct i2c_algorithm smbus_algorithm = {
285         .name           = "Non-I2C SMBus adapter",
286         .id             = I2C_ALGO_SMBUS,
287         .smbus_xfer     = vt596_access,
288         .functionality  = vt596_func,
289 };
290
291 static struct i2c_adapter vt596_adapter = {
292         .owner          = THIS_MODULE,
293         .class          = I2C_CLASS_HWMON,
294         .algo           = &smbus_algorithm,
295         .name           = "unset",
296 };
297
298 static int __devinit vt596_probe(struct pci_dev *pdev,
299                                  const struct pci_device_id *id)
300 {
301         unsigned char temp;
302         int error = -ENODEV;
303         
304         /* Determine the address of the SMBus areas */
305         if (force_addr) {
306                 vt596_smba = force_addr & 0xfff0;
307                 force = 0;
308                 goto found;
309         }
310
311         if ((pci_read_config_word(pdev, id->driver_data, &vt596_smba)) ||
312             !(vt596_smba & 0x1)) {
313                 /* try 2nd address and config reg. for 596 */
314                 if (id->device == PCI_DEVICE_ID_VIA_82C596_3 &&
315                     !pci_read_config_word(pdev, SMBBA2, &vt596_smba) &&
316                     (vt596_smba & 0x1)) {
317                         smb_cf_hstcfg = 0x84;
318                 } else {
319                         /* no matches at all */
320                         dev_err(&pdev->dev, "Cannot configure "
321                                 "SMBus I/O Base address\n");
322                         return -ENODEV;
323                 }
324         }
325
326         vt596_smba &= 0xfff0;
327         if (vt596_smba == 0) {
328                 dev_err(&pdev->dev, "SMBus base address "
329                         "uninitialized - upgrade BIOS or use "
330                         "force_addr=0xaddr\n");
331                 return -ENODEV;
332         }
333
334  found:
335         if (!request_region(vt596_smba, 8, "viapro-smbus")) {
336                 dev_err(&pdev->dev, "SMBus region 0x%x already in use!\n",
337                         vt596_smba);
338                 return -ENODEV;
339         }
340
341         pci_read_config_byte(pdev, SMBHSTCFG, &temp);
342         /* If force_addr is set, we program the new address here. Just to make
343            sure, we disable the VT596 first. */
344         if (force_addr) {
345                 pci_write_config_byte(pdev, SMBHSTCFG, temp & 0xfe);
346                 pci_write_config_word(pdev, id->driver_data, vt596_smba);
347                 pci_write_config_byte(pdev, SMBHSTCFG, temp | 0x01);
348                 dev_warn(&pdev->dev, "WARNING: SMBus interface set to new "
349                      "address 0x%04x!\n", vt596_smba);
350         } else if ((temp & 1) == 0) {
351                 if (force) {
352                         /* NOTE: This assumes I/O space and other allocations 
353                          * WERE done by the Bios!  Don't complain if your 
354                          * hardware does weird things after enabling this. 
355                          * :') Check for Bios updates before resorting to 
356                          * this.
357                          */
358                         pci_write_config_byte(pdev, SMBHSTCFG, temp | 1);
359                         dev_info(&pdev->dev, "Enabling SMBus device\n");
360                 } else {
361                         dev_err(&pdev->dev, "SMBUS: Error: Host SMBus "
362                                 "controller not enabled! - upgrade BIOS or "
363                                 "use force=1\n");
364                         goto release_region;
365                 }
366         }
367
368         if ((temp & 0x0E) == 8)
369                 dev_dbg(&pdev->dev, "using Interrupt 9 for SMBus.\n");
370         else if ((temp & 0x0E) == 0)
371                 dev_dbg(&pdev->dev, "using Interrupt SMI# for SMBus.\n");
372         else
373                 dev_dbg(&pdev->dev, "Illegal Interrupt configuration "
374                         "(or code out of date)!\n");
375
376         pci_read_config_byte(pdev, SMBREV, &temp);
377         dev_dbg(&pdev->dev, "SMBREV = 0x%X\n", temp);
378         dev_dbg(&pdev->dev, "VT596_smba = 0x%X\n", vt596_smba);
379
380         vt596_adapter.dev.parent = &pdev->dev;
381         snprintf(vt596_adapter.name, I2C_NAME_SIZE,
382                         "SMBus Via Pro adapter at %04x", vt596_smba);
383         
384         return i2c_add_adapter(&vt596_adapter);
385
386  release_region:
387         release_region(vt596_smba, 8);
388         return error;
389 }
390
391 static void __devexit vt596_remove(struct pci_dev *pdev)
392 {
393         i2c_del_adapter(&vt596_adapter);
394         release_region(vt596_smba, 8);
395 }
396
397 static struct pci_device_id vt596_ids[] = {
398         {
399                 .vendor         = PCI_VENDOR_ID_VIA,
400                 .device         = PCI_DEVICE_ID_VIA_82C596_3,
401                 .subvendor      = PCI_ANY_ID,
402                 .subdevice      = PCI_ANY_ID,
403                 .driver_data    = SMBBA1,
404         },
405         {
406                 .vendor         = PCI_VENDOR_ID_VIA,
407                 .device         = PCI_DEVICE_ID_VIA_82C596B_3,
408                 .subvendor      = PCI_ANY_ID,
409                 .subdevice      = PCI_ANY_ID,
410                 .driver_data    = SMBBA1,
411         },
412         {
413                 .vendor         = PCI_VENDOR_ID_VIA,
414                 .device         = PCI_DEVICE_ID_VIA_82C686_4,
415                 .subvendor      = PCI_ANY_ID,
416                 .subdevice      = PCI_ANY_ID,
417                 .driver_data    = SMBBA1,
418         },
419         {
420                 .vendor         = PCI_VENDOR_ID_VIA,
421                 .device         = PCI_DEVICE_ID_VIA_8233_0,
422                 .subvendor      = PCI_ANY_ID,
423                 .subdevice      = PCI_ANY_ID,
424                 .driver_data    = SMBBA3
425         },
426         {
427                 .vendor         = PCI_VENDOR_ID_VIA,
428                 .device         = PCI_DEVICE_ID_VIA_8233A,
429                 .subvendor      = PCI_ANY_ID,
430                 .subdevice      = PCI_ANY_ID,
431                 .driver_data    = SMBBA3,
432         },
433         {
434                 .vendor         = PCI_VENDOR_ID_VIA,
435                 .device         = PCI_DEVICE_ID_VIA_8235,
436                 .subvendor      = PCI_ANY_ID,
437                 .subdevice      = PCI_ANY_ID,
438                 .driver_data    = SMBBA3
439         },
440         {
441                 .vendor         = PCI_VENDOR_ID_VIA,
442                 .device         = PCI_DEVICE_ID_VIA_8237,
443                 .subvendor      = PCI_ANY_ID,
444                 .subdevice      = PCI_ANY_ID,
445                 .driver_data    = SMBBA3
446         },
447         {
448                 .vendor         = PCI_VENDOR_ID_VIA,
449                 .device         = PCI_DEVICE_ID_VIA_8231_4,
450                 .subvendor      = PCI_ANY_ID,
451                 .subdevice      = PCI_ANY_ID,
452                 .driver_data    = SMBBA1,
453         },
454         { 0, }
455 };
456
457 static struct pci_driver vt596_driver = {
458         .name           = "vt596 smbus",
459         .id_table       = vt596_ids,
460         .probe          = vt596_probe,
461         .remove         = __devexit_p(vt596_remove),
462 };
463
464 static int __init i2c_vt596_init(void)
465 {
466         return pci_module_init(&vt596_driver);
467 }
468
469
470 static void __exit i2c_vt596_exit(void)
471 {
472         pci_unregister_driver(&vt596_driver);
473 }
474
475 MODULE_AUTHOR(
476     "Frodo Looijaard <frodol@dds.nl> and "
477     "Philip Edelbrock <phil@netroedge.com>");
478 MODULE_DESCRIPTION("vt82c596 SMBus driver");
479 MODULE_LICENSE("GPL");
480
481 module_init(i2c_vt596_init);
482 module_exit(i2c_vt596_exit);