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