| 1 | /* |
|---|
| 2 | i2c-viapro.c - Part of lm_sensors, Linux kernel modules for hardware |
|---|
| 3 | monitoring |
|---|
| 4 | Copyright (c) 1998 - 2001 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 | 8233 |
|---|
| 29 | Note: we assume there can only be one device, with one SMBus interface. |
|---|
| 30 | */ |
|---|
| 31 | |
|---|
| 32 | #include <linux/version.h> |
|---|
| 33 | #include <linux/module.h> |
|---|
| 34 | #include <linux/pci.h> |
|---|
| 35 | #include <asm/io.h> |
|---|
| 36 | #include <linux/kernel.h> |
|---|
| 37 | #include <linux/stddef.h> |
|---|
| 38 | #include <linux/sched.h> |
|---|
| 39 | #include <linux/ioport.h> |
|---|
| 40 | #include <linux/i2c.h> |
|---|
| 41 | #include "version.h" |
|---|
| 42 | |
|---|
| 43 | #include <linux/init.h> |
|---|
| 44 | |
|---|
| 45 | #ifndef PCI_DEVICE_ID_VIA_82C596_3 |
|---|
| 46 | #define PCI_DEVICE_ID_VIA_82C596_3 0x3050 |
|---|
| 47 | #endif |
|---|
| 48 | #ifndef PCI_DEVICE_ID_VIA_82C596B_3 |
|---|
| 49 | #define PCI_DEVICE_ID_VIA_82C596B_3 0x3051 |
|---|
| 50 | #endif |
|---|
| 51 | #ifndef PCI_DEVICE_ID_VIA_82C686_4 |
|---|
| 52 | #define PCI_DEVICE_ID_VIA_82C686_4 0x3057 |
|---|
| 53 | #endif |
|---|
| 54 | #ifndef PCI_DEVICE_ID_VIA_8233_0 |
|---|
| 55 | #define PCI_DEVICE_ID_VIA_8233_0 0x3074 |
|---|
| 56 | #endif |
|---|
| 57 | |
|---|
| 58 | #define SMBBA1 0x90 |
|---|
| 59 | #define SMBBA2 0x80 |
|---|
| 60 | #define SMBBA3 0xD0 |
|---|
| 61 | |
|---|
| 62 | struct sd { |
|---|
| 63 | const unsigned short dev; |
|---|
| 64 | const unsigned char base; |
|---|
| 65 | const unsigned char hstcfg; |
|---|
| 66 | const char *name; |
|---|
| 67 | }; |
|---|
| 68 | |
|---|
| 69 | static struct sd supported[] = { |
|---|
| 70 | {PCI_DEVICE_ID_VIA_82C596_3, SMBBA1, 0xD2, "VT82C596A/B"}, |
|---|
| 71 | {PCI_DEVICE_ID_VIA_82C596B_3, SMBBA1, 0xD2, "VT82C596B"}, |
|---|
| 72 | {PCI_DEVICE_ID_VIA_82C686_4, SMBBA1, 0xD2, "VT82C686A/B"}, |
|---|
| 73 | {PCI_DEVICE_ID_VIA_8233_0, SMBBA3, 0xD2, "VT8233"}, |
|---|
| 74 | {0, 0, 0, NULL} |
|---|
| 75 | }; |
|---|
| 76 | |
|---|
| 77 | static struct sd *num = supported; |
|---|
| 78 | |
|---|
| 79 | /* SMBus address offsets */ |
|---|
| 80 | #define SMBHSTSTS (0 + vt596_smba) |
|---|
| 81 | #define SMBHSLVSTS (1 + vt596_smba) |
|---|
| 82 | #define SMBHSTCNT (2 + vt596_smba) |
|---|
| 83 | #define SMBHSTCMD (3 + vt596_smba) |
|---|
| 84 | #define SMBHSTADD (4 + vt596_smba) |
|---|
| 85 | #define SMBHSTDAT0 (5 + vt596_smba) |
|---|
| 86 | #define SMBHSTDAT1 (6 + vt596_smba) |
|---|
| 87 | #define SMBBLKDAT (7 + vt596_smba) |
|---|
| 88 | #define SMBSLVCNT (8 + vt596_smba) |
|---|
| 89 | #define SMBSHDWCMD (9 + vt596_smba) |
|---|
| 90 | #define SMBSLVEVT (0xA + vt596_smba) |
|---|
| 91 | #define SMBSLVDAT (0xC + vt596_smba) |
|---|
| 92 | |
|---|
| 93 | /* PCI Address Constants */ |
|---|
| 94 | |
|---|
| 95 | /* SMBus data in configuration space can be found in two places, |
|---|
| 96 | We try to select the better one*/ |
|---|
| 97 | |
|---|
| 98 | static unsigned short smb_cf_hstcfg; |
|---|
| 99 | |
|---|
| 100 | #define SMBHSTCFG (smb_cf_hstcfg) |
|---|
| 101 | #define SMBSLVC (SMBHSTCFG+1) |
|---|
| 102 | #define SMBSHDW1 (SMBHSTCFG+2) |
|---|
| 103 | #define SMBSHDW2 (SMBHSTCFG+3) |
|---|
| 104 | #define SMBREV (SMBHSTCFG+4) |
|---|
| 105 | |
|---|
| 106 | /* Other settings */ |
|---|
| 107 | #define MAX_TIMEOUT 500 |
|---|
| 108 | #define ENABLE_INT9 0 |
|---|
| 109 | |
|---|
| 110 | /* VT82C596 constants */ |
|---|
| 111 | #define VT596_QUICK 0x00 |
|---|
| 112 | #define VT596_BYTE 0x04 |
|---|
| 113 | #define VT596_BYTE_DATA 0x08 |
|---|
| 114 | #define VT596_WORD_DATA 0x0C |
|---|
| 115 | #define VT596_BLOCK_DATA 0x14 |
|---|
| 116 | |
|---|
| 117 | /* insmod parameters */ |
|---|
| 118 | |
|---|
| 119 | /* If force is set to anything different from 0, we forcibly enable the |
|---|
| 120 | VT596. DANGEROUS! */ |
|---|
| 121 | static int force = 0; |
|---|
| 122 | MODULE_PARM(force, "i"); |
|---|
| 123 | MODULE_PARM_DESC(force, "Forcibly enable the SMBus. DANGEROUS!"); |
|---|
| 124 | |
|---|
| 125 | /* If force_addr is set to anything different from 0, we forcibly enable |
|---|
| 126 | the VT596 at the given address. VERY DANGEROUS! */ |
|---|
| 127 | static int force_addr = 0; |
|---|
| 128 | MODULE_PARM(force_addr, "i"); |
|---|
| 129 | MODULE_PARM_DESC(force_addr, |
|---|
| 130 | "Forcibly enable the SMBus at the given address. " |
|---|
| 131 | "EXTREMELY DANGEROUS!"); |
|---|
| 132 | |
|---|
| 133 | #ifdef MODULE |
|---|
| 134 | static |
|---|
| 135 | #else |
|---|
| 136 | extern |
|---|
| 137 | #endif |
|---|
| 138 | int __init i2c_vt596_init(void); |
|---|
| 139 | static int __init vt596_cleanup(void); |
|---|
| 140 | static int vt596_setup(void); |
|---|
| 141 | static s32 vt596_access(struct i2c_adapter *adap, u16 addr, |
|---|
| 142 | unsigned short flags, char read_write, |
|---|
| 143 | u8 command, int size, union i2c_smbus_data *data); |
|---|
| 144 | static void vt596_do_pause(unsigned int amount); |
|---|
| 145 | static int vt596_transaction(void); |
|---|
| 146 | static void vt596_inc(struct i2c_adapter *adapter); |
|---|
| 147 | static void vt596_dec(struct i2c_adapter *adapter); |
|---|
| 148 | static u32 vt596_func(struct i2c_adapter *adapter); |
|---|
| 149 | |
|---|
| 150 | #ifdef MODULE |
|---|
| 151 | extern int init_module(void); |
|---|
| 152 | extern int cleanup_module(void); |
|---|
| 153 | #endif /* MODULE */ |
|---|
| 154 | |
|---|
| 155 | static struct i2c_algorithm smbus_algorithm = { |
|---|
| 156 | /* name */ "Non-I2C SMBus adapter", |
|---|
| 157 | /* id */ I2C_ALGO_SMBUS, |
|---|
| 158 | /* master_xfer */ NULL, |
|---|
| 159 | /* smbus_access */ vt596_access, |
|---|
| 160 | /* slave_send */ NULL, |
|---|
| 161 | /* slave_rcv */ NULL, |
|---|
| 162 | /* algo_control */ NULL, |
|---|
| 163 | /* functionality */ vt596_func, |
|---|
| 164 | }; |
|---|
| 165 | |
|---|
| 166 | static struct i2c_adapter vt596_adapter = { |
|---|
| 167 | "unset", |
|---|
| 168 | I2C_ALGO_SMBUS | I2C_HW_SMBUS_VIA2, |
|---|
| 169 | &smbus_algorithm, |
|---|
| 170 | NULL, |
|---|
| 171 | vt596_inc, |
|---|
| 172 | vt596_dec, |
|---|
| 173 | NULL, |
|---|
| 174 | NULL, |
|---|
| 175 | }; |
|---|
| 176 | |
|---|
| 177 | static int __initdata vt596_initialized; |
|---|
| 178 | static unsigned short vt596_smba = 0; |
|---|
| 179 | |
|---|
| 180 | |
|---|
| 181 | /* Detect whether a compatible device can be found, and initialize it. */ |
|---|
| 182 | int vt596_setup(void) |
|---|
| 183 | { |
|---|
| 184 | unsigned char temp; |
|---|
| 185 | |
|---|
| 186 | struct pci_dev *VT596_dev = NULL; |
|---|
| 187 | |
|---|
| 188 | /* First check whether we can access PCI at all */ |
|---|
| 189 | if (pci_present() == 0) |
|---|
| 190 | return(-ENODEV); |
|---|
| 191 | |
|---|
| 192 | /* Look for a supported device/function */ |
|---|
| 193 | do { |
|---|
| 194 | if((VT596_dev = pci_find_device(PCI_VENDOR_ID_VIA, num->dev, |
|---|
| 195 | VT596_dev))) |
|---|
| 196 | break; |
|---|
| 197 | } while ((++num)->dev); |
|---|
| 198 | |
|---|
| 199 | if (VT596_dev == NULL) |
|---|
| 200 | return(-ENODEV); |
|---|
| 201 | printk("i2c-viapro.o: Found Via %s device\n", num->name); |
|---|
| 202 | |
|---|
| 203 | /* Determine the address of the SMBus areas */ |
|---|
| 204 | smb_cf_hstcfg = num->hstcfg; |
|---|
| 205 | if (force_addr) { |
|---|
| 206 | vt596_smba = force_addr; |
|---|
| 207 | force = 0; |
|---|
| 208 | } else { |
|---|
| 209 | if ((pci_read_config_word(VT596_dev, num->base, &vt596_smba)) |
|---|
| 210 | || !(vt596_smba & 0x1)) { |
|---|
| 211 | /* try 2nd address and config reg. for 596 */ |
|---|
| 212 | if((num->dev == PCI_DEVICE_ID_VIA_82C596_3) && |
|---|
| 213 | (!pci_read_config_word(VT596_dev, SMBBA2, &vt596_smba)) && |
|---|
| 214 | (vt596_smba & 0x1)) |
|---|
| 215 | smb_cf_hstcfg = 0x84; |
|---|
| 216 | else |
|---|
| 217 | goto FAIL; |
|---|
| 218 | } else { |
|---|
| 219 | FAIL: |
|---|
| 220 | printk |
|---|
| 221 | ("i2c-viapro.o: Cannot configure SMBus I/O Base address\n"); |
|---|
| 222 | return(-ENODEV); |
|---|
| 223 | } |
|---|
| 224 | } |
|---|
| 225 | vt596_smba &= 0xfff0; |
|---|
| 226 | |
|---|
| 227 | if (check_region(vt596_smba, 8)) { |
|---|
| 228 | printk("i2c-viapro.o: SMB region 0x%x already in use!\n", |
|---|
| 229 | vt596_smba); |
|---|
| 230 | return(-ENODEV); |
|---|
| 231 | } |
|---|
| 232 | |
|---|
| 233 | pci_read_config_byte(VT596_dev, SMBHSTCFG, &temp); |
|---|
| 234 | /* If force_addr is set, we program the new address here. Just to make |
|---|
| 235 | sure, we disable the VT596 first. */ |
|---|
| 236 | if (force_addr) { |
|---|
| 237 | pci_write_config_byte(VT596_dev, SMBHSTCFG, temp & 0xfe); |
|---|
| 238 | pci_write_config_word(VT596_dev, num->base, vt596_smba); |
|---|
| 239 | pci_write_config_byte(VT596_dev, SMBHSTCFG, temp | 0x01); |
|---|
| 240 | printk |
|---|
| 241 | ("i2c-viapro.o: WARNING: SMBus interface set to new " |
|---|
| 242 | "address 0x%04x!\n", vt596_smba); |
|---|
| 243 | } else if ((temp & 1) == 0) { |
|---|
| 244 | if (force) { |
|---|
| 245 | /* NOTE: This assumes I/O space and other allocations WERE |
|---|
| 246 | done by the Bios! Don't complain if your hardware does weird |
|---|
| 247 | things after enabling this. :') Check for Bios updates before |
|---|
| 248 | resorting to this. */ |
|---|
| 249 | pci_write_config_byte(VT596_dev, SMBHSTCFG, |
|---|
| 250 | temp | 1); |
|---|
| 251 | printk |
|---|
| 252 | ("i2c-viapro.o: WARNING: SMBus interface has been FORCEFULLY " |
|---|
| 253 | "ENABLED!\n"); |
|---|
| 254 | } else { |
|---|
| 255 | printk |
|---|
| 256 | ("SMBUS: Error: Host SMBus controller not enabled!\n"); |
|---|
| 257 | return(-ENODEV); |
|---|
| 258 | } |
|---|
| 259 | } |
|---|
| 260 | |
|---|
| 261 | /* Everything is happy, let's grab the memory and set things up. */ |
|---|
| 262 | request_region(vt596_smba, 8, "via2-smbus"); |
|---|
| 263 | |
|---|
| 264 | #ifdef DEBUG |
|---|
| 265 | if ((temp & 0x0E) == 8) |
|---|
| 266 | printk("i2c-viapro.o: using Interrupt 9 for SMBus.\n"); |
|---|
| 267 | else if ((temp & 0x0E) == 0) |
|---|
| 268 | printk("i2c-viapro.o: using Interrupt SMI# for SMBus.\n"); |
|---|
| 269 | else |
|---|
| 270 | printk |
|---|
| 271 | ("i2c-viapro.o: Illegal Interrupt configuration (or code out " |
|---|
| 272 | "of date)!\n"); |
|---|
| 273 | |
|---|
| 274 | pci_read_config_byte(VT596_dev, SMBREV, &temp); |
|---|
| 275 | printk("i2c-viapro.o: SMBREV = 0x%X\n", temp); |
|---|
| 276 | printk("i2c-viapro.o: VT596_smba = 0x%X\n", vt596_smba); |
|---|
| 277 | #endif /* DEBUG */ |
|---|
| 278 | |
|---|
| 279 | return(0); |
|---|
| 280 | } |
|---|
| 281 | |
|---|
| 282 | |
|---|
| 283 | /* Internally used pause function */ |
|---|
| 284 | void vt596_do_pause(unsigned int amount) |
|---|
| 285 | { |
|---|
| 286 | current->state = TASK_INTERRUPTIBLE; |
|---|
| 287 | schedule_timeout(amount); |
|---|
| 288 | } |
|---|
| 289 | |
|---|
| 290 | /* Another internally used function */ |
|---|
| 291 | int vt596_transaction(void) |
|---|
| 292 | { |
|---|
| 293 | int temp; |
|---|
| 294 | int result = 0; |
|---|
| 295 | int timeout = 0; |
|---|
| 296 | |
|---|
| 297 | #ifdef DEBUG |
|---|
| 298 | printk |
|---|
| 299 | ("i2c-viapro.o: Transaction (pre): CNT=%02x, CMD=%02x, ADD=%02x, DAT0=%02x, " |
|---|
| 300 | "DAT1=%02x\n", inb_p(SMBHSTCNT), inb_p(SMBHSTCMD), |
|---|
| 301 | inb_p(SMBHSTADD), inb_p(SMBHSTDAT0), inb_p(SMBHSTDAT1)); |
|---|
| 302 | #endif |
|---|
| 303 | |
|---|
| 304 | /* Make sure the SMBus host is ready to start transmitting */ |
|---|
| 305 | if ((temp = inb_p(SMBHSTSTS)) != 0x00) { |
|---|
| 306 | #ifdef DEBUG |
|---|
| 307 | printk("i2c-viapro.o: SMBus busy (0x%02x). Resetting... \n", |
|---|
| 308 | temp); |
|---|
| 309 | #endif |
|---|
| 310 | outb_p(temp, SMBHSTSTS); |
|---|
| 311 | if ((temp = inb_p(SMBHSTSTS)) != 0x00) { |
|---|
| 312 | #ifdef DEBUG |
|---|
| 313 | printk("i2c-viapro.o: Failed! (0x%02x)\n", temp); |
|---|
| 314 | #endif |
|---|
| 315 | return -1; |
|---|
| 316 | } else { |
|---|
| 317 | #ifdef DEBUG |
|---|
| 318 | printk("i2c-viapro.o: Successfull!\n"); |
|---|
| 319 | #endif |
|---|
| 320 | } |
|---|
| 321 | } |
|---|
| 322 | |
|---|
| 323 | /* start the transaction by setting bit 6 */ |
|---|
| 324 | outb_p(inb(SMBHSTCNT) | 0x040, SMBHSTCNT); |
|---|
| 325 | |
|---|
| 326 | /* We will always wait for a fraction of a second! |
|---|
| 327 | I don't know if VIA needs this, Intel did */ |
|---|
| 328 | do { |
|---|
| 329 | vt596_do_pause(1); |
|---|
| 330 | temp = inb_p(SMBHSTSTS); |
|---|
| 331 | } while ((temp & 0x01) && (timeout++ < MAX_TIMEOUT)); |
|---|
| 332 | |
|---|
| 333 | /* If the SMBus is still busy, we give up */ |
|---|
| 334 | if (timeout >= MAX_TIMEOUT) { |
|---|
| 335 | #ifdef DEBUG |
|---|
| 336 | printk("i2c-viapro.o: SMBus Timeout!\n"); |
|---|
| 337 | result = -1; |
|---|
| 338 | #endif |
|---|
| 339 | } |
|---|
| 340 | |
|---|
| 341 | if (temp & 0x10) { |
|---|
| 342 | result = -1; |
|---|
| 343 | #ifdef DEBUG |
|---|
| 344 | printk("i2c-viapro.o: Error: Failed bus transaction\n"); |
|---|
| 345 | #endif |
|---|
| 346 | } |
|---|
| 347 | |
|---|
| 348 | if (temp & 0x08) { |
|---|
| 349 | result = -1; |
|---|
| 350 | printk |
|---|
| 351 | ("i2c-viapro.o: Bus collision! SMBus may be locked until next hard\n" |
|---|
| 352 | "reset. (sorry!)\n"); |
|---|
| 353 | /* Clock stops and slave is stuck in mid-transmission */ |
|---|
| 354 | } |
|---|
| 355 | |
|---|
| 356 | if (temp & 0x04) { |
|---|
| 357 | result = -1; |
|---|
| 358 | #ifdef DEBUG |
|---|
| 359 | printk("i2c-viapro.o: Error: no response!\n"); |
|---|
| 360 | #endif |
|---|
| 361 | } |
|---|
| 362 | |
|---|
| 363 | if (inb_p(SMBHSTSTS) != 0x00) |
|---|
| 364 | outb_p(inb(SMBHSTSTS), SMBHSTSTS); |
|---|
| 365 | |
|---|
| 366 | if ((temp = inb_p(SMBHSTSTS)) != 0x00) { |
|---|
| 367 | #ifdef DEBUG |
|---|
| 368 | printk |
|---|
| 369 | ("i2c-viapro.o: Failed reset at end of transaction (%02x)\n", |
|---|
| 370 | temp); |
|---|
| 371 | #endif |
|---|
| 372 | } |
|---|
| 373 | #ifdef DEBUG |
|---|
| 374 | printk |
|---|
| 375 | ("i2c-viapro.o: Transaction (post): CNT=%02x, CMD=%02x, ADD=%02x, " |
|---|
| 376 | "DAT0=%02x, DAT1=%02x\n", inb_p(SMBHSTCNT), inb_p(SMBHSTCMD), |
|---|
| 377 | inb_p(SMBHSTADD), inb_p(SMBHSTDAT0), inb_p(SMBHSTDAT1)); |
|---|
| 378 | #endif |
|---|
| 379 | return result; |
|---|
| 380 | } |
|---|
| 381 | |
|---|
| 382 | /* Return -1 on error. See smbus.h for more information */ |
|---|
| 383 | s32 vt596_access(struct i2c_adapter * adap, u16 addr, unsigned short flags, |
|---|
| 384 | char read_write, |
|---|
| 385 | u8 command, int size, union i2c_smbus_data * data) |
|---|
| 386 | { |
|---|
| 387 | int i, len; |
|---|
| 388 | |
|---|
| 389 | switch (size) { |
|---|
| 390 | case I2C_SMBUS_PROC_CALL: |
|---|
| 391 | printk |
|---|
| 392 | ("i2c-viapro.o: I2C_SMBUS_PROC_CALL not supported!\n"); |
|---|
| 393 | return -1; |
|---|
| 394 | case I2C_SMBUS_QUICK: |
|---|
| 395 | outb_p(((addr & 0x7f) << 1) | (read_write & 0x01), |
|---|
| 396 | SMBHSTADD); |
|---|
| 397 | size = VT596_QUICK; |
|---|
| 398 | break; |
|---|
| 399 | case I2C_SMBUS_BYTE: |
|---|
| 400 | outb_p(((addr & 0x7f) << 1) | (read_write & 0x01), |
|---|
| 401 | SMBHSTADD); |
|---|
| 402 | if (read_write == I2C_SMBUS_WRITE) |
|---|
| 403 | outb_p(command, SMBHSTCMD); |
|---|
| 404 | size = VT596_BYTE; |
|---|
| 405 | break; |
|---|
| 406 | case I2C_SMBUS_BYTE_DATA: |
|---|
| 407 | outb_p(((addr & 0x7f) << 1) | (read_write & 0x01), |
|---|
| 408 | SMBHSTADD); |
|---|
| 409 | outb_p(command, SMBHSTCMD); |
|---|
| 410 | if (read_write == I2C_SMBUS_WRITE) |
|---|
| 411 | outb_p(data->byte, SMBHSTDAT0); |
|---|
| 412 | size = VT596_BYTE_DATA; |
|---|
| 413 | break; |
|---|
| 414 | case I2C_SMBUS_WORD_DATA: |
|---|
| 415 | outb_p(((addr & 0x7f) << 1) | (read_write & 0x01), |
|---|
| 416 | SMBHSTADD); |
|---|
| 417 | outb_p(command, SMBHSTCMD); |
|---|
| 418 | if (read_write == I2C_SMBUS_WRITE) { |
|---|
| 419 | outb_p(data->word & 0xff, SMBHSTDAT0); |
|---|
| 420 | outb_p((data->word & 0xff00) >> 8, SMBHSTDAT1); |
|---|
| 421 | } |
|---|
| 422 | size = VT596_WORD_DATA; |
|---|
| 423 | break; |
|---|
| 424 | case I2C_SMBUS_BLOCK_DATA: |
|---|
| 425 | outb_p(((addr & 0x7f) << 1) | (read_write & 0x01), |
|---|
| 426 | SMBHSTADD); |
|---|
| 427 | outb_p(command, SMBHSTCMD); |
|---|
| 428 | if (read_write == I2C_SMBUS_WRITE) { |
|---|
| 429 | len = data->block[0]; |
|---|
| 430 | if (len < 0) |
|---|
| 431 | len = 0; |
|---|
| 432 | if (len > 32) |
|---|
| 433 | len = 32; |
|---|
| 434 | outb_p(len, SMBHSTDAT0); |
|---|
| 435 | i = inb_p(SMBHSTCNT); /* Reset SMBBLKDAT */ |
|---|
| 436 | for (i = 1; i <= len; i++) |
|---|
| 437 | outb_p(data->block[i], SMBBLKDAT); |
|---|
| 438 | } |
|---|
| 439 | size = VT596_BLOCK_DATA; |
|---|
| 440 | break; |
|---|
| 441 | } |
|---|
| 442 | |
|---|
| 443 | outb_p((size & 0x1C) + (ENABLE_INT9 & 1), SMBHSTCNT); |
|---|
| 444 | |
|---|
| 445 | if (vt596_transaction()) /* Error in transaction */ |
|---|
| 446 | return -1; |
|---|
| 447 | |
|---|
| 448 | if ((read_write == I2C_SMBUS_WRITE) || (size == VT596_QUICK)) |
|---|
| 449 | return 0; |
|---|
| 450 | |
|---|
| 451 | |
|---|
| 452 | switch (size) { |
|---|
| 453 | case VT596_BYTE: /* Where is the result put? I assume here it is in |
|---|
| 454 | SMBHSTDAT0 but it might just as well be in the |
|---|
| 455 | SMBHSTCMD. No clue in the docs */ |
|---|
| 456 | |
|---|
| 457 | data->byte = inb_p(SMBHSTDAT0); |
|---|
| 458 | break; |
|---|
| 459 | case VT596_BYTE_DATA: |
|---|
| 460 | data->byte = inb_p(SMBHSTDAT0); |
|---|
| 461 | break; |
|---|
| 462 | case VT596_WORD_DATA: |
|---|
| 463 | data->word = inb_p(SMBHSTDAT0) + (inb_p(SMBHSTDAT1) << 8); |
|---|
| 464 | break; |
|---|
| 465 | case VT596_BLOCK_DATA: |
|---|
| 466 | data->block[0] = inb_p(SMBHSTDAT0); |
|---|
| 467 | i = inb_p(SMBHSTCNT); /* Reset SMBBLKDAT */ |
|---|
| 468 | for (i = 1; i <= data->block[0]; i++) |
|---|
| 469 | data->block[i] = inb_p(SMBBLKDAT); |
|---|
| 470 | break; |
|---|
| 471 | } |
|---|
| 472 | return 0; |
|---|
| 473 | } |
|---|
| 474 | |
|---|
| 475 | void vt596_inc(struct i2c_adapter *adapter) |
|---|
| 476 | { |
|---|
| 477 | MOD_INC_USE_COUNT; |
|---|
| 478 | } |
|---|
| 479 | |
|---|
| 480 | void vt596_dec(struct i2c_adapter *adapter) |
|---|
| 481 | { |
|---|
| 482 | |
|---|
| 483 | MOD_DEC_USE_COUNT; |
|---|
| 484 | } |
|---|
| 485 | |
|---|
| 486 | u32 vt596_func(struct i2c_adapter *adapter) |
|---|
| 487 | { |
|---|
| 488 | return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE | |
|---|
| 489 | I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA | |
|---|
| 490 | I2C_FUNC_SMBUS_BLOCK_DATA; |
|---|
| 491 | } |
|---|
| 492 | |
|---|
| 493 | int __init i2c_vt596_init(void) |
|---|
| 494 | { |
|---|
| 495 | int res; |
|---|
| 496 | printk("i2c-viapro.o version %s (%s)\n", LM_VERSION, LM_DATE); |
|---|
| 497 | #ifdef DEBUG |
|---|
| 498 | /* PE- It might be good to make this a permanent part of the code! */ |
|---|
| 499 | if (vt596_initialized) { |
|---|
| 500 | printk |
|---|
| 501 | ("i2c-viapro.o: Oops, vt596_init called a second time!\n"); |
|---|
| 502 | return -EBUSY; |
|---|
| 503 | } |
|---|
| 504 | #endif |
|---|
| 505 | vt596_initialized = 0; |
|---|
| 506 | if ((res = vt596_setup())) { |
|---|
| 507 | printk |
|---|
| 508 | ("i2c-viapro.o: Can't detect vt82c596 or compatible device, module not inserted.\n"); |
|---|
| 509 | vt596_cleanup(); |
|---|
| 510 | return res; |
|---|
| 511 | } |
|---|
| 512 | vt596_initialized++; |
|---|
| 513 | sprintf(vt596_adapter.name, "SMBus Via Pro adapter at %04x", |
|---|
| 514 | vt596_smba); |
|---|
| 515 | if ((res = i2c_add_adapter(&vt596_adapter))) { |
|---|
| 516 | printk |
|---|
| 517 | ("i2c-viapro.o: Adapter registration failed, module not inserted.\n"); |
|---|
| 518 | vt596_cleanup(); |
|---|
| 519 | return res; |
|---|
| 520 | } |
|---|
| 521 | vt596_initialized++; |
|---|
| 522 | printk("i2c-viapro.o: Via Pro SMBus detected and initialized\n"); |
|---|
| 523 | return 0; |
|---|
| 524 | } |
|---|
| 525 | |
|---|
| 526 | int __init vt596_cleanup(void) |
|---|
| 527 | { |
|---|
| 528 | int res; |
|---|
| 529 | if (vt596_initialized >= 2) { |
|---|
| 530 | if ((res = i2c_del_adapter(&vt596_adapter))) { |
|---|
| 531 | printk |
|---|
| 532 | ("i2c-viapro.o: i2c_del_adapter failed, module not removed\n"); |
|---|
| 533 | return res; |
|---|
| 534 | } else |
|---|
| 535 | vt596_initialized--; |
|---|
| 536 | } |
|---|
| 537 | if (vt596_initialized >= 1) { |
|---|
| 538 | release_region(vt596_smba, 8); |
|---|
| 539 | vt596_initialized--; |
|---|
| 540 | } |
|---|
| 541 | return 0; |
|---|
| 542 | } |
|---|
| 543 | |
|---|
| 544 | EXPORT_NO_SYMBOLS; |
|---|
| 545 | |
|---|
| 546 | #ifdef MODULE |
|---|
| 547 | |
|---|
| 548 | MODULE_AUTHOR |
|---|
| 549 | ("Frodo Looijaard <frodol@dds.nl> and Philip Edelbrock <phil@netroedge.com>"); |
|---|
| 550 | MODULE_DESCRIPTION("vt82c596 SMBus driver"); |
|---|
| 551 | |
|---|
| 552 | |
|---|
| 553 | int init_module(void) |
|---|
| 554 | { |
|---|
| 555 | return i2c_vt596_init(); |
|---|
| 556 | } |
|---|
| 557 | |
|---|
| 558 | int cleanup_module(void) |
|---|
| 559 | { |
|---|
| 560 | return vt596_cleanup(); |
|---|
| 561 | } |
|---|
| 562 | |
|---|
| 563 | #endif /* MODULE */ |
|---|