| 1 | /* |
|---|
| 2 | ali15x3.c - Part of lm_sensors, Linux kernel modules for hardware |
|---|
| 3 | monitoring |
|---|
| 4 | Copyright (c) 1999 Frodo Looijaard <frodol@dds.nl> and |
|---|
| 5 | Philip Edelbrock <phil@netroedge.com> and |
|---|
| 6 | Mark D. Studebaker <mds@eng.paradyne.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 | This is the driver for the SMB Host controller on |
|---|
| 25 | Acer Labs Inc. (ALI) M1541 and M1543C South Bridges. |
|---|
| 26 | |
|---|
| 27 | The M1543C is a South bridge for desktop systems. |
|---|
| 28 | The M1541 is a South bridge for portable systems. |
|---|
| 29 | They are part of the following ALI chipsets: |
|---|
| 30 | "Aladdin Pro 2": Includes the M1621 Slot 1 North bridge |
|---|
| 31 | with AGP and 100MHz CPU Front Side bus |
|---|
| 32 | "Aladdin V": Includes the M1541 Socket 7 North bridge |
|---|
| 33 | with AGP and 100MHz CPU Front Side bus |
|---|
| 34 | "Aladdin IV": Includes the M1541 Socket 7 North bridge |
|---|
| 35 | with host bus up to 83.3 MHz. |
|---|
| 36 | For an overview of these chips see http://www.acerlabs.com |
|---|
| 37 | |
|---|
| 38 | The M1533/M1543C devices appear as FOUR separate devices |
|---|
| 39 | on the PCI bus. An output of lspci will show something similar |
|---|
| 40 | to the following: |
|---|
| 41 | |
|---|
| 42 | 00:02.0 USB Controller: Acer Laboratories Inc. M5237 |
|---|
| 43 | 00:03.0 Bridge: Acer Laboratories Inc. M7101 |
|---|
| 44 | 00:07.0 ISA bridge: Acer Laboratories Inc. M1533 |
|---|
| 45 | 00:0f.0 IDE interface: Acer Laboratories Inc. M5229 |
|---|
| 46 | |
|---|
| 47 | The SMB controller is part of the 7101 device, which is an |
|---|
| 48 | ACPI-compliant Power Management Unit (PMU). |
|---|
| 49 | |
|---|
| 50 | The whole 7101 device has to be enabled for the SMB to work. |
|---|
| 51 | You can't just enable the SMB alone. |
|---|
| 52 | The SMB and the ACPI have separate I/O spaces. |
|---|
| 53 | So we have to make sure that both the SMB and the ACPI |
|---|
| 54 | are mapped and enabled. |
|---|
| 55 | |
|---|
| 56 | This driver controls the SMB Host only. |
|---|
| 57 | The SMB Slave controller on the M15X3 is not enabled. |
|---|
| 58 | |
|---|
| 59 | This driver requests the I/O space both for the SMB and the ACPI |
|---|
| 60 | registers, just to be safe. It doesn't actually use the ACPI region. |
|---|
| 61 | It will therefore conflict with separate software |
|---|
| 62 | that accesses the ACPI registers? |
|---|
| 63 | To fix this, undefine MAP_ACPI. |
|---|
| 64 | |
|---|
| 65 | This driver does not use interrupts. |
|---|
| 66 | */ |
|---|
| 67 | |
|---|
| 68 | /* Note: we assume there can only be one ALI15X3, with one SMBus interface */ |
|---|
| 69 | |
|---|
| 70 | #include <linux/module.h> |
|---|
| 71 | #include <linux/pci.h> |
|---|
| 72 | #include <asm/io.h> |
|---|
| 73 | #include <linux/kernel.h> |
|---|
| 74 | #include <linux/stddef.h> |
|---|
| 75 | #include <linux/sched.h> |
|---|
| 76 | #include <linux/ioport.h> |
|---|
| 77 | #include <linux/i2c.h> |
|---|
| 78 | #include "version.h" |
|---|
| 79 | #include "compat.h" |
|---|
| 80 | |
|---|
| 81 | #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,1,54)) |
|---|
| 82 | #include <linux/bios32.h> |
|---|
| 83 | #endif |
|---|
| 84 | |
|---|
| 85 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,1,53) |
|---|
| 86 | #include <linux/init.h> |
|---|
| 87 | #else |
|---|
| 88 | #define __init |
|---|
| 89 | #endif |
|---|
| 90 | |
|---|
| 91 | /* undefine this if separate ACPI software is accessing the |
|---|
| 92 | registers at the offset defined at 0x10. |
|---|
| 93 | */ |
|---|
| 94 | #define MAP_ACPI 1 |
|---|
| 95 | #undef FORCE_ALI15X3_ENABLE |
|---|
| 96 | |
|---|
| 97 | /* ALI15X3 SMBus address offsets */ |
|---|
| 98 | #define SMBHSTSTS (0 + ali15x3_smba) |
|---|
| 99 | #define SMBHSTCNT (1 + ali15x3_smba) |
|---|
| 100 | #define SMBHSTSTART (2 + ali15x3_smba) |
|---|
| 101 | #define SMBHSTCMD (7 + ali15x3_smba) |
|---|
| 102 | #define SMBHSTADD (3 + ali15x3_smba) |
|---|
| 103 | #define SMBHSTDAT0 (4 + ali15x3_smba) |
|---|
| 104 | #define SMBHSTDAT1 (5 + ali15x3_smba) |
|---|
| 105 | #define SMBBLKDAT (6 + ali15x3_smba) |
|---|
| 106 | |
|---|
| 107 | /* PCI Address Constants */ |
|---|
| 108 | #define SMBCOM 0x004 |
|---|
| 109 | #define ACPIBA 0x010 |
|---|
| 110 | #define SMBBA 0x014 |
|---|
| 111 | #define SMBATPC 0x05B /* used to unlock xxxBA registers */ |
|---|
| 112 | #define SMBHSTCFG 0x0E0 |
|---|
| 113 | #define SMBSLVC 0x0E1 |
|---|
| 114 | #define SMBCLK 0x0E2 |
|---|
| 115 | #define SMBREV 0x008 |
|---|
| 116 | |
|---|
| 117 | /* Other settings */ |
|---|
| 118 | #define MAX_TIMEOUT 500 /* times 1/100 sec */ |
|---|
| 119 | #define ALI15X3_ACPI_IOSIZE 64 |
|---|
| 120 | #define ALI15X3_SMB_IOSIZE 32 |
|---|
| 121 | |
|---|
| 122 | /* this is what the Award 1004 BIOS sets them to on a ASUS P5A MB. |
|---|
| 123 | We don't use these here. If the bases aren't set to some value we |
|---|
| 124 | tell user to upgrade BIOS and we fail. |
|---|
| 125 | */ |
|---|
| 126 | #define ALI15X3_ACPI_DEFAULTBASE 0xEC00 |
|---|
| 127 | #define ALI15X3_SMB_DEFAULTBASE 0xE800 |
|---|
| 128 | |
|---|
| 129 | /* ALI15X3 address lock bits */ |
|---|
| 130 | #define ALI15X3_LOCK 0x06 |
|---|
| 131 | |
|---|
| 132 | /* ALI15X3 command constants */ |
|---|
| 133 | #define ALI15X3_ABORT 0x02 |
|---|
| 134 | #define ALI15X3_T_OUT 0x04 |
|---|
| 135 | #define ALI15X3_QUICK 0x00 |
|---|
| 136 | #define ALI15X3_BYTE 0x10 |
|---|
| 137 | #define ALI15X3_BYTE_DATA 0x20 |
|---|
| 138 | #define ALI15X3_WORD_DATA 0x30 |
|---|
| 139 | #define ALI15X3_BLOCK_DATA 0x40 |
|---|
| 140 | #define ALI15X3_BLOCK_CLR 0x80 |
|---|
| 141 | |
|---|
| 142 | /* ALI15X3 status register bits */ |
|---|
| 143 | #define ALI15X3_STS_IDLE 0x04 |
|---|
| 144 | #define ALI15X3_STS_BUSY 0x08 |
|---|
| 145 | #define ALI15X3_STS_DONE 0x10 |
|---|
| 146 | #define ALI15X3_STS_DEV 0x20 /* device error */ |
|---|
| 147 | #define ALI15X3_STS_COLL 0x40 /* collision or no response */ |
|---|
| 148 | #define ALI15X3_STS_TERM 0x80 /* terminated by abort */ |
|---|
| 149 | #define ALI15X3_STS_ERR 0xE0 /* all the bad error bits */ |
|---|
| 150 | |
|---|
| 151 | |
|---|
| 152 | static int __init ali15x3_init(void); |
|---|
| 153 | static int __init ali15x3_cleanup(void); |
|---|
| 154 | static int ali15x3_setup(void); |
|---|
| 155 | static s32 ali15x3_access(struct i2c_adapter *adap, u8 addr, char read_write, |
|---|
| 156 | u8 command, int size, union i2c_smbus_data * data); |
|---|
| 157 | static void ali15x3_do_pause( unsigned int amount ); |
|---|
| 158 | static int ali15x3_transaction(void); |
|---|
| 159 | static void ali15x3_inc(struct i2c_adapter *adapter); |
|---|
| 160 | static void ali15x3_dec(struct i2c_adapter *adapter); |
|---|
| 161 | |
|---|
| 162 | #ifdef MODULE |
|---|
| 163 | extern int init_module(void); |
|---|
| 164 | extern int cleanup_module(void); |
|---|
| 165 | #endif /* MODULE */ |
|---|
| 166 | |
|---|
| 167 | static struct i2c_algorithm smbus_algorithm = { |
|---|
| 168 | /* name */ "Non-I2C SMBus adapter", |
|---|
| 169 | /* id */ I2C_ALGO_SMBUS, |
|---|
| 170 | /* master_xfer */ NULL, |
|---|
| 171 | /* smbus_access */ ali15x3_access, |
|---|
| 172 | /* slave_send */ NULL, |
|---|
| 173 | /* slave_rcv */ NULL, |
|---|
| 174 | /* algo_control */ NULL, |
|---|
| 175 | }; |
|---|
| 176 | |
|---|
| 177 | static struct i2c_adapter ali15x3_adapter = { |
|---|
| 178 | "unset", |
|---|
| 179 | I2C_ALGO_SMBUS | I2C_HW_SMBUS_ALI15X3, |
|---|
| 180 | &smbus_algorithm, |
|---|
| 181 | NULL, |
|---|
| 182 | ali15x3_inc, |
|---|
| 183 | ali15x3_dec, |
|---|
| 184 | NULL, |
|---|
| 185 | NULL, |
|---|
| 186 | }; |
|---|
| 187 | |
|---|
| 188 | static int __init ali15x3_initialized; |
|---|
| 189 | #ifdef MAP_ACPI |
|---|
| 190 | static unsigned short ali15x3_acpia = 0; |
|---|
| 191 | #endif |
|---|
| 192 | static unsigned short ali15x3_smba = 0; |
|---|
| 193 | |
|---|
| 194 | |
|---|
| 195 | /* Detect whether a ALI15X3 can be found, and initialize it, where necessary. |
|---|
| 196 | Note the differences between kernels with the old PCI BIOS interface and |
|---|
| 197 | newer kernels with the real PCI interface. In compat.h some things are |
|---|
| 198 | defined to make the transition easier. */ |
|---|
| 199 | int ali15x3_setup(void) |
|---|
| 200 | { |
|---|
| 201 | int error_return=0; |
|---|
| 202 | unsigned char temp; |
|---|
| 203 | |
|---|
| 204 | #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,1,54)) |
|---|
| 205 | struct pci_dev *ALI15X3_dev; |
|---|
| 206 | #else |
|---|
| 207 | unsigned char ALI15X3_bus, ALI15X3_devfn = 0; |
|---|
| 208 | int res; |
|---|
| 209 | #endif |
|---|
| 210 | |
|---|
| 211 | /* First check whether we can access PCI at all */ |
|---|
| 212 | if (pci_present() == 0) { |
|---|
| 213 | printk("i2c-ali15x3.o: Error: No PCI-bus found!\n"); |
|---|
| 214 | error_return=-ENODEV; |
|---|
| 215 | goto END; |
|---|
| 216 | } |
|---|
| 217 | |
|---|
| 218 | /* Look for the ALI15X3, M7101 device */ |
|---|
| 219 | #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,1,54)) |
|---|
| 220 | ALI15X3_dev = NULL; |
|---|
| 221 | ALI15X3_dev = pci_find_device(PCI_VENDOR_ID_AL, |
|---|
| 222 | PCI_DEVICE_ID_AL_M7101, ALI15X3_dev); |
|---|
| 223 | if(ALI15X3_dev == NULL) { |
|---|
| 224 | #else /* LINUX_VERSION_CODE < KERNEL_VERSION(2,1,54) */ |
|---|
| 225 | res = pcibios_find_device(PCI_VENDOR_ID_AL, |
|---|
| 226 | PCI_DEVICE_ID_AL_M7101, |
|---|
| 227 | 0,&ALI15X3_bus, &ALI15X3_devfn); |
|---|
| 228 | |
|---|
| 229 | if (res) { |
|---|
| 230 | #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(2,1,54) */ |
|---|
| 231 | printk("i2c-ali15x3.o: Error: Can't detect ali15x3!\n"); |
|---|
| 232 | error_return=-ENODEV; |
|---|
| 233 | goto END; |
|---|
| 234 | } |
|---|
| 235 | |
|---|
| 236 | /* Check the following things: |
|---|
| 237 | - ACPI and SMB I/O addresses are initialized |
|---|
| 238 | - Device is enabled |
|---|
| 239 | - We can use the addresses |
|---|
| 240 | */ |
|---|
| 241 | |
|---|
| 242 | /* Unlock the register. |
|---|
| 243 | The data sheet says that the address registers are read-only |
|---|
| 244 | if the lock bits are 1, but in fact the address registers |
|---|
| 245 | are zero unless you clear the lock bits. |
|---|
| 246 | */ |
|---|
| 247 | pci_read_config_byte_united(ALI15X3_dev, ALI15X3_bus ,ALI15X3_devfn, |
|---|
| 248 | SMBATPC, &temp); |
|---|
| 249 | if(temp & ALI15X3_LOCK) |
|---|
| 250 | { |
|---|
| 251 | temp &= ~ALI15X3_LOCK; |
|---|
| 252 | pci_write_config_byte_united(ALI15X3_dev, ALI15X3_bus ,ALI15X3_devfn, |
|---|
| 253 | SMBATPC, temp); |
|---|
| 254 | } |
|---|
| 255 | |
|---|
| 256 | /* Determine the address of the ACPI and SMBus areas */ |
|---|
| 257 | #ifdef MAP_ACPI |
|---|
| 258 | pci_read_config_word_united(ALI15X3_dev, ALI15X3_bus ,ALI15X3_devfn, |
|---|
| 259 | ACPIBA,&ali15x3_acpia); |
|---|
| 260 | ali15x3_acpia &= (0xffff & ~ (ALI15X3_ACPI_IOSIZE - 1)); |
|---|
| 261 | if(ali15x3_acpia == 0) { |
|---|
| 262 | printk("i2c-ali15x3.o: ALI15X3_acpi region uninitialized - upgrade BIOS?\n"); |
|---|
| 263 | error_return=-ENODEV; |
|---|
| 264 | } |
|---|
| 265 | #endif |
|---|
| 266 | |
|---|
| 267 | pci_read_config_word_united(ALI15X3_dev, ALI15X3_bus ,ALI15X3_devfn, |
|---|
| 268 | SMBBA,&ali15x3_smba); |
|---|
| 269 | ali15x3_smba &= (0xffff & ~ (ALI15X3_SMB_IOSIZE - 1)); |
|---|
| 270 | if(ali15x3_smba == 0) { |
|---|
| 271 | printk("i2c-ali15x3.o: ALI15X3_smb region uninitialized - upgrade BIOS?\n"); |
|---|
| 272 | error_return=-ENODEV; |
|---|
| 273 | } |
|---|
| 274 | |
|---|
| 275 | if(error_return == -ENODEV) |
|---|
| 276 | goto END; |
|---|
| 277 | |
|---|
| 278 | #ifdef MAP_ACPI |
|---|
| 279 | if (check_region(ali15x3_acpia, ALI15X3_ACPI_IOSIZE)) { |
|---|
| 280 | printk("i2c-ali15x3.o: ALI15X3_acpi region 0x%x already in use!\n", ali15x3_acpia); |
|---|
| 281 | printk("i2c-ali15x3.o: If conflicting ACPI software is installed, undefine MAP_ACPI and recompile!\n"); |
|---|
| 282 | error_return=-ENODEV; |
|---|
| 283 | } |
|---|
| 284 | #endif |
|---|
| 285 | |
|---|
| 286 | if (check_region(ali15x3_smba, ALI15X3_SMB_IOSIZE)) { |
|---|
| 287 | printk("i2c-ali15x3.o: ALI15X3_smb region 0x%x already in use!\n", ali15x3_smba); |
|---|
| 288 | error_return=-ENODEV; |
|---|
| 289 | } |
|---|
| 290 | |
|---|
| 291 | if(error_return == -ENODEV) |
|---|
| 292 | goto END; |
|---|
| 293 | |
|---|
| 294 | /* check if whole device is enabled */ |
|---|
| 295 | pci_read_config_byte_united(ALI15X3_dev, ALI15X3_bus ,ALI15X3_devfn, |
|---|
| 296 | SMBCOM, &temp); |
|---|
| 297 | if ((temp & 1) == 0) { |
|---|
| 298 | printk("SMBUS: Error: ACPI/SMB device not enabled - upgrade BIOS?\n"); |
|---|
| 299 | error_return=-ENODEV; |
|---|
| 300 | goto END; |
|---|
| 301 | } |
|---|
| 302 | |
|---|
| 303 | /* Is SMB Host controller enabled? */ |
|---|
| 304 | pci_read_config_byte_united(ALI15X3_dev, ALI15X3_bus, ALI15X3_devfn, |
|---|
| 305 | SMBHSTCFG, &temp); |
|---|
| 306 | #ifdef FORCE_ALI15X3_ENABLE |
|---|
| 307 | /* This should never need to be done. |
|---|
| 308 | NOTE: This assumes I/O space and other allocations WERE |
|---|
| 309 | done by the Bios! Don't complain if your hardware does weird |
|---|
| 310 | things after enabling this. :') Check for Bios updates before |
|---|
| 311 | resorting to this. */ |
|---|
| 312 | if ((temp & 1) == 0) { |
|---|
| 313 | pci_write_config_byte_united(ALI15X3_dev, ALI15X3_bus, ALI15X3_devfn, |
|---|
| 314 | SMBHSTCFG, temp | 1); |
|---|
| 315 | printk("i2c-ali15x3.o: WARNING: ALI15X3 SMBus interface has been FORCEFULLY " |
|---|
| 316 | "ENABLED!!\n"); |
|---|
| 317 | } |
|---|
| 318 | #else /* FORCE_ALI15X3_ENABLE */ |
|---|
| 319 | if ((temp & 1) == 0) { |
|---|
| 320 | printk("SMBUS: Error: Host SMBus controller not enabled - upgrade BIOS?\n"); |
|---|
| 321 | error_return=-ENODEV; |
|---|
| 322 | goto END; |
|---|
| 323 | } |
|---|
| 324 | #endif /* FORCE_ALI15X3_ENABLE */ |
|---|
| 325 | |
|---|
| 326 | /* set SMB clock to 74KHz as recommended in data sheet */ |
|---|
| 327 | pci_write_config_byte_united(ALI15X3_dev, ALI15X3_bus ,ALI15X3_devfn, |
|---|
| 328 | SMBCLK, 0x20); |
|---|
| 329 | |
|---|
| 330 | /* Everything is happy, let's grab the memory and set things up. */ |
|---|
| 331 | #ifdef MAP_ACPI |
|---|
| 332 | request_region(ali15x3_acpia, ALI15X3_ACPI_IOSIZE, "ali15x3-acpi"); |
|---|
| 333 | #endif |
|---|
| 334 | request_region(ali15x3_smba, ALI15X3_SMB_IOSIZE, "ali15x3-smb"); |
|---|
| 335 | |
|---|
| 336 | #ifdef DEBUG |
|---|
| 337 | /* |
|---|
| 338 | The interrupt routing for SMB is set up in register 0x77 in the |
|---|
| 339 | 1533 ISA Bridge device, NOT in the 7101 device. |
|---|
| 340 | Don't bother with finding the 1533 device and reading the register. |
|---|
| 341 | if ((....... & 0x0F) == 1) |
|---|
| 342 | printk("i2c-ali15x3.o: ALI15X3 using Interrupt 9 for SMBus.\n"); |
|---|
| 343 | */ |
|---|
| 344 | pci_read_config_byte_united(ALI15X3_dev, ALI15X3_bus, ALI15X3_devfn, SMBREV, |
|---|
| 345 | &temp); |
|---|
| 346 | printk("i2c-ali15x3.o: SMBREV = 0x%X\n",temp); |
|---|
| 347 | printk("i2c-ali15x3.o: ALI15X3_smba = 0x%X\n",ali15x3_smba); |
|---|
| 348 | #endif /* DEBUG */ |
|---|
| 349 | |
|---|
| 350 | END: |
|---|
| 351 | return error_return; |
|---|
| 352 | } |
|---|
| 353 | |
|---|
| 354 | |
|---|
| 355 | /* Internally used pause function */ |
|---|
| 356 | void ali15x3_do_pause( unsigned int amount ) |
|---|
| 357 | { |
|---|
| 358 | current->state = TASK_INTERRUPTIBLE; |
|---|
| 359 | schedule_timeout(amount); |
|---|
| 360 | } |
|---|
| 361 | |
|---|
| 362 | /* Another internally used function */ |
|---|
| 363 | int ali15x3_transaction(void) |
|---|
| 364 | { |
|---|
| 365 | int temp; |
|---|
| 366 | int result=0; |
|---|
| 367 | int timeout=0; |
|---|
| 368 | |
|---|
| 369 | #ifdef DEBUG |
|---|
| 370 | printk("i2c-ali15x3.o: Transaction (pre): STS=%02x, CNT=%02x, CMD=%02x, ADD=%02x, DAT0=%02x, " |
|---|
| 371 | "DAT1=%02x\n", |
|---|
| 372 | inb_p(SMBHSTSTS), inb_p(SMBHSTCNT),inb_p(SMBHSTCMD),inb_p(SMBHSTADD),inb_p(SMBHSTDAT0), |
|---|
| 373 | inb_p(SMBHSTDAT1)); |
|---|
| 374 | #endif |
|---|
| 375 | |
|---|
| 376 | /* get status */ |
|---|
| 377 | temp = inb_p(SMBHSTSTS); |
|---|
| 378 | |
|---|
| 379 | /* Make sure the SMBus host is ready to start transmitting */ |
|---|
| 380 | /* Check the busy bit first */ |
|---|
| 381 | if (temp & ALI15X3_STS_BUSY) { |
|---|
| 382 | /* |
|---|
| 383 | If the host controller is still busy, it may have timed out in the previous transaction, |
|---|
| 384 | resulting in a "SMBus Timeout" printk. |
|---|
| 385 | I've tried the following to reset a stuck busy bit. |
|---|
| 386 | 1. Reset the controller with an ABORT command. |
|---|
| 387 | (this doesn't seem to clear the controller if an external device is hung) |
|---|
| 388 | 2. Reset the controller and the other SMBus devices with a T_OUT command. |
|---|
| 389 | (this clears the host busy bit if an external device is hung, |
|---|
| 390 | but it comes back upon a new access to a device) |
|---|
| 391 | 3. Disable and reenable the controller in SMBHSTCFG |
|---|
| 392 | Worst case, nothing seems to work except power reset. |
|---|
| 393 | */ |
|---|
| 394 | /* Abort - reset the host controller */ |
|---|
| 395 | /* |
|---|
| 396 | #ifdef DEBUG |
|---|
| 397 | printk("i2c-ali15x3.o: Resetting host controller to clear busy condition\n",temp); |
|---|
| 398 | #endif |
|---|
| 399 | outb_p(ALI15X3_ABORT, SMBHSTCNT); |
|---|
| 400 | temp = inb_p(SMBHSTSTS); |
|---|
| 401 | if (temp & ALI15X3_STS_BUSY) { |
|---|
| 402 | */ |
|---|
| 403 | |
|---|
| 404 | /* |
|---|
| 405 | Try resetting entire SMB bus, including other devices - |
|---|
| 406 | This may not work either - it clears the BUSY bit but |
|---|
| 407 | then the BUSY bit may come back on when you try and use the chip again. |
|---|
| 408 | If that's the case you are stuck. |
|---|
| 409 | */ |
|---|
| 410 | printk("i2c-ali15x3.o: Resetting entire SMB Bus to clear busy condition (%02x)\n",temp); |
|---|
| 411 | outb_p(ALI15X3_T_OUT, SMBHSTCNT); |
|---|
| 412 | temp = inb_p(SMBHSTSTS); |
|---|
| 413 | } |
|---|
| 414 | /* |
|---|
| 415 | } |
|---|
| 416 | */ |
|---|
| 417 | |
|---|
| 418 | /* now check the error bits and the busy bit */ |
|---|
| 419 | if (temp & (ALI15X3_STS_ERR | ALI15X3_STS_BUSY)) { |
|---|
| 420 | /* do a clear-on-write */ |
|---|
| 421 | outb_p(0xFF, SMBHSTSTS); |
|---|
| 422 | if ((temp = inb_p(SMBHSTSTS)) & (ALI15X3_STS_ERR | ALI15X3_STS_BUSY)) { |
|---|
| 423 | /* this is probably going to be correctable only by a power reset |
|---|
| 424 | as one of the bits now appears to be stuck */ |
|---|
| 425 | /* This may be a bus or device with electrical problems. */ |
|---|
| 426 | printk("i2c-ali15x3.o: SMBus reset failed! (0x%02x) - controller or device on bus is probably hung\n",temp); |
|---|
| 427 | return -1; |
|---|
| 428 | } |
|---|
| 429 | } else { |
|---|
| 430 | /* check and clear done bit */ |
|---|
| 431 | if (temp & ALI15X3_STS_DONE) { |
|---|
| 432 | outb_p(temp, SMBHSTSTS); |
|---|
| 433 | } |
|---|
| 434 | } |
|---|
| 435 | |
|---|
| 436 | /* start the transaction by writing anything to the start register */ |
|---|
| 437 | outb_p(0xFF, SMBHSTSTART); |
|---|
| 438 | |
|---|
| 439 | /* We will always wait for a fraction of a second! */ |
|---|
| 440 | timeout = 0; |
|---|
| 441 | do { |
|---|
| 442 | ali15x3_do_pause(1); |
|---|
| 443 | temp=inb_p(SMBHSTSTS); |
|---|
| 444 | } while ((!(temp & (ALI15X3_STS_ERR | ALI15X3_STS_DONE))) && (timeout++ < MAX_TIMEOUT)); |
|---|
| 445 | |
|---|
| 446 | /* If the SMBus is still busy, we give up */ |
|---|
| 447 | if (timeout >= MAX_TIMEOUT) { |
|---|
| 448 | result = -1; |
|---|
| 449 | printk("i2c-ali15x3.o: SMBus Timeout!\n"); |
|---|
| 450 | } |
|---|
| 451 | |
|---|
| 452 | if (temp & ALI15X3_STS_TERM) { |
|---|
| 453 | result = -1; |
|---|
| 454 | #ifdef DEBUG |
|---|
| 455 | printk("i2c-ali15x3.o: Error: Failed bus transaction\n"); |
|---|
| 456 | #endif |
|---|
| 457 | } |
|---|
| 458 | |
|---|
| 459 | /* |
|---|
| 460 | Unfortunately the ALI SMB controller maps "no response" and "bus collision" |
|---|
| 461 | into a single bit. No reponse is the usual case so don't |
|---|
| 462 | do a printk. |
|---|
| 463 | This means that bus collisions go unreported. |
|---|
| 464 | */ |
|---|
| 465 | if (temp & ALI15X3_STS_COLL) { |
|---|
| 466 | result = -1; |
|---|
| 467 | #ifdef DEBUG |
|---|
| 468 | printk("i2c-ali15x3.o: Error: no response or bus collision ADD=%02x\n", inb_p(SMBHSTADD)); |
|---|
| 469 | #endif |
|---|
| 470 | } |
|---|
| 471 | |
|---|
| 472 | /* haven't ever seen this */ |
|---|
| 473 | if (temp & ALI15X3_STS_DEV) { |
|---|
| 474 | result = -1; |
|---|
| 475 | printk("i2c-ali15x3.o: Error: device error\n"); |
|---|
| 476 | } |
|---|
| 477 | |
|---|
| 478 | #ifdef DEBUG |
|---|
| 479 | printk("i2c-ali15x3.o: Transaction (post): STS=%02x, CNT=%02x, CMD=%02x, ADD=%02x, " |
|---|
| 480 | "DAT0=%02x, DAT1=%02x\n", |
|---|
| 481 | inb_p(SMBHSTSTS), inb_p(SMBHSTCNT),inb_p(SMBHSTCMD),inb_p(SMBHSTADD),inb_p(SMBHSTDAT0), |
|---|
| 482 | inb_p(SMBHSTDAT1)); |
|---|
| 483 | #endif |
|---|
| 484 | return result; |
|---|
| 485 | } |
|---|
| 486 | |
|---|
| 487 | /* Return -1 on error. See smbus.h for more information */ |
|---|
| 488 | s32 ali15x3_access(struct i2c_adapter *adap, u8 addr, char read_write, |
|---|
| 489 | u8 command, int size, union i2c_smbus_data * data) |
|---|
| 490 | { |
|---|
| 491 | int i,len; |
|---|
| 492 | int temp; |
|---|
| 493 | int timeout; |
|---|
| 494 | |
|---|
| 495 | /* clear all the bits (clear-on-write) */ |
|---|
| 496 | outb_p(0xFF, SMBHSTSTS); |
|---|
| 497 | /* make sure SMBus is idle */ |
|---|
| 498 | temp = inb_p(SMBHSTSTS); |
|---|
| 499 | for(timeout = 0; (timeout < MAX_TIMEOUT) && !(temp & ALI15X3_STS_IDLE); timeout++) |
|---|
| 500 | { |
|---|
| 501 | ali15x3_do_pause(1); |
|---|
| 502 | temp=inb_p(SMBHSTSTS); |
|---|
| 503 | } |
|---|
| 504 | if (timeout >= MAX_TIMEOUT) { |
|---|
| 505 | printk("i2c-ali15x3.o: Idle wait Timeout! STS=0x%02x\n", temp); |
|---|
| 506 | } |
|---|
| 507 | |
|---|
| 508 | switch(size) { |
|---|
| 509 | case I2C_SMBUS_PROC_CALL: |
|---|
| 510 | printk("i2c-ali15x3.o: I2C_SMBUS_PROC_CALL not supported!\n"); |
|---|
| 511 | return -1; |
|---|
| 512 | case I2C_SMBUS_QUICK: |
|---|
| 513 | outb_p(((addr & 0x7f) << 1) | (read_write & 0x01), SMBHSTADD); |
|---|
| 514 | size = ALI15X3_QUICK; |
|---|
| 515 | break; |
|---|
| 516 | case I2C_SMBUS_BYTE: |
|---|
| 517 | outb_p(((addr & 0x7f) << 1) | (read_write & 0x01), SMBHSTADD); |
|---|
| 518 | if (read_write == I2C_SMBUS_WRITE) |
|---|
| 519 | outb_p(command, SMBHSTCMD); |
|---|
| 520 | size = ALI15X3_BYTE; |
|---|
| 521 | break; |
|---|
| 522 | case I2C_SMBUS_BYTE_DATA: |
|---|
| 523 | outb_p(((addr & 0x7f) << 1) | (read_write & 0x01), SMBHSTADD); |
|---|
| 524 | outb_p(command, SMBHSTCMD); |
|---|
| 525 | if (read_write == I2C_SMBUS_WRITE) |
|---|
| 526 | outb_p(data->byte,SMBHSTDAT0); |
|---|
| 527 | size = ALI15X3_BYTE_DATA; |
|---|
| 528 | break; |
|---|
| 529 | case I2C_SMBUS_WORD_DATA: |
|---|
| 530 | outb_p(((addr & 0x7f) << 1) | (read_write & 0x01), SMBHSTADD); |
|---|
| 531 | outb_p(command, SMBHSTCMD); |
|---|
| 532 | if (read_write == I2C_SMBUS_WRITE) { |
|---|
| 533 | outb_p(data->word & 0xff,SMBHSTDAT0); |
|---|
| 534 | outb_p((data->word & 0xff00) >> 8,SMBHSTDAT1); |
|---|
| 535 | } |
|---|
| 536 | size = ALI15X3_WORD_DATA; |
|---|
| 537 | break; |
|---|
| 538 | case I2C_SMBUS_BLOCK_DATA: |
|---|
| 539 | outb_p(((addr & 0x7f) << 1) | (read_write & 0x01), SMBHSTADD); |
|---|
| 540 | outb_p(command, SMBHSTCMD); |
|---|
| 541 | if (read_write == I2C_SMBUS_WRITE) { |
|---|
| 542 | len = data->block[0]; |
|---|
| 543 | if (len < 0) { |
|---|
| 544 | len = 0; |
|---|
| 545 | data->block[0] = len; |
|---|
| 546 | } |
|---|
| 547 | if (len > 32) { |
|---|
| 548 | len = 32; |
|---|
| 549 | data->block[0] = len; |
|---|
| 550 | } |
|---|
| 551 | outb_p(len,SMBHSTDAT0); |
|---|
| 552 | outb_p(inb_p(SMBHSTCNT) | ALI15X3_BLOCK_CLR, SMBHSTCNT); /* Reset SMBBLKDAT */ |
|---|
| 553 | for (i = 1; i <= len; i++) |
|---|
| 554 | outb_p(data->block[i],SMBBLKDAT); |
|---|
| 555 | } |
|---|
| 556 | size = ALI15X3_BLOCK_DATA; |
|---|
| 557 | break; |
|---|
| 558 | } |
|---|
| 559 | |
|---|
| 560 | outb_p(size, SMBHSTCNT); /* output command */ |
|---|
| 561 | |
|---|
| 562 | if (ali15x3_transaction()) /* Error in transaction */ |
|---|
| 563 | return -1; |
|---|
| 564 | |
|---|
| 565 | if ((read_write == I2C_SMBUS_WRITE) || (size == ALI15X3_QUICK)) |
|---|
| 566 | return 0; |
|---|
| 567 | |
|---|
| 568 | |
|---|
| 569 | switch(size) { |
|---|
| 570 | case ALI15X3_BYTE: /* Result put in SMBHSTDAT0 */ |
|---|
| 571 | data->byte = inb_p(SMBHSTDAT0); |
|---|
| 572 | break; |
|---|
| 573 | case ALI15X3_BYTE_DATA: |
|---|
| 574 | data->byte = inb_p(SMBHSTDAT0); |
|---|
| 575 | break; |
|---|
| 576 | case ALI15X3_WORD_DATA: |
|---|
| 577 | data->word = inb_p(SMBHSTDAT0) + (inb_p(SMBHSTDAT1) << 8); |
|---|
| 578 | break; |
|---|
| 579 | case ALI15X3_BLOCK_DATA: |
|---|
| 580 | len = inb_p(SMBHSTDAT0); |
|---|
| 581 | if(len > 32) |
|---|
| 582 | len = 32; |
|---|
| 583 | data->block[0] = len; |
|---|
| 584 | outb_p(inb_p(SMBHSTCNT) | ALI15X3_BLOCK_CLR, SMBHSTCNT); /* Reset SMBBLKDAT */ |
|---|
| 585 | for (i = 1; i <= data->block[0]; i++) { |
|---|
| 586 | data->block[i] = inb_p(SMBBLKDAT); |
|---|
| 587 | #ifdef DEBUG |
|---|
| 588 | printk("i2c-ali15x3.o: Blk: len=%d, i=%d, data=%02x\n", len, i, data->block[i]); |
|---|
| 589 | #endif DEBUG |
|---|
| 590 | } |
|---|
| 591 | break; |
|---|
| 592 | } |
|---|
| 593 | return 0; |
|---|
| 594 | } |
|---|
| 595 | |
|---|
| 596 | void ali15x3_inc(struct i2c_adapter *adapter) |
|---|
| 597 | { |
|---|
| 598 | MOD_INC_USE_COUNT; |
|---|
| 599 | } |
|---|
| 600 | |
|---|
| 601 | void ali15x3_dec(struct i2c_adapter *adapter) |
|---|
| 602 | { |
|---|
| 603 | |
|---|
| 604 | MOD_DEC_USE_COUNT; |
|---|
| 605 | } |
|---|
| 606 | |
|---|
| 607 | int __init ali15x3_init(void) |
|---|
| 608 | { |
|---|
| 609 | int res; |
|---|
| 610 | printk("ali15x3.o version %s (%s)\n",LM_VERSION,LM_DATE); |
|---|
| 611 | #ifdef DEBUG |
|---|
| 612 | /* PE- It might be good to make this a permanent part of the code! */ |
|---|
| 613 | if (ali15x3_initialized) { |
|---|
| 614 | printk("i2c-ali15x3.o: Oops, ali15x3_init called a second time!\n"); |
|---|
| 615 | return -EBUSY; |
|---|
| 616 | } |
|---|
| 617 | #endif |
|---|
| 618 | ali15x3_initialized = 0; |
|---|
| 619 | if ((res = ali15x3_setup())) { |
|---|
| 620 | printk("i2c-ali15x3.o: ALI15X3 not detected, module not inserted.\n"); |
|---|
| 621 | ali15x3_cleanup(); |
|---|
| 622 | return res; |
|---|
| 623 | } |
|---|
| 624 | ali15x3_initialized ++; |
|---|
| 625 | #ifdef MAP_ACPI |
|---|
| 626 | sprintf(ali15x3_adapter.name,"ACPI ALI15X3 at %04x",ali15x3_acpia); |
|---|
| 627 | #endif |
|---|
| 628 | sprintf(ali15x3_adapter.name,"SMBus ALI15X3 adapter at %04x",ali15x3_smba); |
|---|
| 629 | if ((res = i2c_add_adapter(&ali15x3_adapter))) { |
|---|
| 630 | printk("i2c-ali15x3.o: Adapter registration failed, module not inserted.\n"); |
|---|
| 631 | ali15x3_cleanup(); |
|---|
| 632 | return res; |
|---|
| 633 | } |
|---|
| 634 | ali15x3_initialized++; |
|---|
| 635 | printk("i2c-ali15x3.o: ALI15X3 SMBus Controller detected and initialized\n"); |
|---|
| 636 | return 0; |
|---|
| 637 | } |
|---|
| 638 | |
|---|
| 639 | int __init ali15x3_cleanup(void) |
|---|
| 640 | { |
|---|
| 641 | int res; |
|---|
| 642 | if (ali15x3_initialized >= 2) |
|---|
| 643 | { |
|---|
| 644 | if ((res = i2c_del_adapter(&ali15x3_adapter))) { |
|---|
| 645 | printk("i2c-ali15x3.o: i2c_del_adapter failed, module not removed\n"); |
|---|
| 646 | return res; |
|---|
| 647 | } else |
|---|
| 648 | ali15x3_initialized--; |
|---|
| 649 | } |
|---|
| 650 | if (ali15x3_initialized >= 1) { |
|---|
| 651 | #ifdef MAP_ACPI |
|---|
| 652 | release_region(ali15x3_acpia, ALI15X3_ACPI_IOSIZE); |
|---|
| 653 | #endif |
|---|
| 654 | release_region(ali15x3_smba, ALI15X3_SMB_IOSIZE); |
|---|
| 655 | ali15x3_initialized--; |
|---|
| 656 | } |
|---|
| 657 | return 0; |
|---|
| 658 | } |
|---|
| 659 | |
|---|
| 660 | EXPORT_NO_SYMBOLS; |
|---|
| 661 | |
|---|
| 662 | #ifdef MODULE |
|---|
| 663 | |
|---|
| 664 | MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>, Philip Edelbrock <phil@netroedge.com>, and Mark D. Studebaker <mds@eng.paradyne.com>"); |
|---|
| 665 | MODULE_DESCRIPTION("ALI15X3 SMBus driver"); |
|---|
| 666 | |
|---|
| 667 | int init_module(void) |
|---|
| 668 | { |
|---|
| 669 | return ali15x3_init(); |
|---|
| 670 | } |
|---|
| 671 | |
|---|
| 672 | int cleanup_module(void) |
|---|
| 673 | { |
|---|
| 674 | return ali15x3_cleanup(); |
|---|
| 675 | } |
|---|
| 676 | |
|---|
| 677 | #endif /* MODULE */ |
|---|