4 Shamelessly ripped from i2c-piix4.c:
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
21 2002-04-08: Added nForce support. (Csaba Halasz)
22 2002-10-03: Fixed nForce PnP I/O port. (Michael Steil)
23 2002-12-28: Rewritten into something that resembles a Linux driver (hch)
24 2003-11-29: Added back AMD8111 removed by the previous rewrite.
29 Supports AMD756, AMD766, AMD768, AMD8111 and nVidia nForce
30 Note: we assume there can only be one device, with one SMBus interface.
33 #include <linux/module.h>
34 #include <linux/pci.h>
35 #include <linux/kernel.h>
36 #include <linux/delay.h>
37 #include <linux/stddef.h>
38 #include <linux/ioport.h>
39 #include <linux/i2c.h>
40 #include <linux/acpi.h>
43 /* AMD756 SMBus address offsets */
44 #define SMB_ADDR_OFFSET 0xE0
46 #define SMB_GLOBAL_STATUS (0x0 + amd756_ioport)
47 #define SMB_GLOBAL_ENABLE (0x2 + amd756_ioport)
48 #define SMB_HOST_ADDRESS (0x4 + amd756_ioport)
49 #define SMB_HOST_DATA (0x6 + amd756_ioport)
50 #define SMB_HOST_COMMAND (0x8 + amd756_ioport)
51 #define SMB_HOST_BLOCK_DATA (0x9 + amd756_ioport)
52 #define SMB_HAS_DATA (0xA + amd756_ioport)
53 #define SMB_HAS_DEVICE_ADDRESS (0xC + amd756_ioport)
54 #define SMB_HAS_HOST_ADDRESS (0xE + amd756_ioport)
55 #define SMB_SNOOP_ADDRESS (0xF + amd756_ioport)
57 /* PCI Address Constants */
59 /* address of I/O space */
60 #define SMBBA 0x058 /* mh */
61 #define SMBBANFORCE 0x014
63 /* general configuration */
64 #define SMBGCFG 0x041 /* mh */
66 /* silicon revision code */
70 #define MAX_TIMEOUT 500
72 /* AMD756 constants */
73 #define AMD756_QUICK 0x00
74 #define AMD756_BYTE 0x01
75 #define AMD756_BYTE_DATA 0x02
76 #define AMD756_WORD_DATA 0x03
77 #define AMD756_PROCESS_CALL 0x04
78 #define AMD756_BLOCK_DATA 0x05
80 static struct pci_driver amd756_driver;
81 static unsigned short amd756_ioport;
84 SMBUS event = I/O 28-29 bit 11
85 see E0 for the status bits and enabled in E2
88 #define GS_ABRT_STS (1 << 0)
89 #define GS_COL_STS (1 << 1)
90 #define GS_PRERR_STS (1 << 2)
91 #define GS_HST_STS (1 << 3)
92 #define GS_HCYC_STS (1 << 4)
93 #define GS_TO_STS (1 << 5)
94 #define GS_SMB_STS (1 << 11)
96 #define GS_CLEAR_STS (GS_ABRT_STS | GS_COL_STS | GS_PRERR_STS | \
97 GS_HCYC_STS | GS_TO_STS )
99 #define GE_CYC_TYPE_MASK (7)
100 #define GE_HOST_STC (1 << 3)
101 #define GE_ABORT (1 << 5)
104 static int amd756_transaction(struct i2c_adapter *adap)
110 dev_dbg(&adap->dev, "Transaction (pre): GS=%04x, GE=%04x, ADD=%04x, "
111 "DAT=%04x\n", inw_p(SMB_GLOBAL_STATUS),
112 inw_p(SMB_GLOBAL_ENABLE), inw_p(SMB_HOST_ADDRESS),
113 inb_p(SMB_HOST_DATA));
115 /* Make sure the SMBus host is ready to start transmitting */
116 if ((temp = inw_p(SMB_GLOBAL_STATUS)) & (GS_HST_STS | GS_SMB_STS)) {
117 dev_dbg(&adap->dev, "SMBus busy (%04x). Waiting...\n", temp);
120 temp = inw_p(SMB_GLOBAL_STATUS);
121 } while ((temp & (GS_HST_STS | GS_SMB_STS)) &&
122 (timeout++ < MAX_TIMEOUT));
123 /* If the SMBus is still busy, we give up */
124 if (timeout > MAX_TIMEOUT) {
125 dev_dbg(&adap->dev, "Busy wait timeout (%04x)\n", temp);
131 /* start the transaction by setting the start bit */
132 outw_p(inw(SMB_GLOBAL_ENABLE) | GE_HOST_STC, SMB_GLOBAL_ENABLE);
134 /* We will always wait for a fraction of a second! */
137 temp = inw_p(SMB_GLOBAL_STATUS);
138 } while ((temp & GS_HST_STS) && (timeout++ < MAX_TIMEOUT));
140 /* If the SMBus is still busy, we give up */
141 if (timeout > MAX_TIMEOUT) {
142 dev_dbg(&adap->dev, "Completion timeout!\n");
146 if (temp & GS_PRERR_STS) {
148 dev_dbg(&adap->dev, "SMBus Protocol error (no response)!\n");
151 if (temp & GS_COL_STS) {
153 dev_warn(&adap->dev, "SMBus collision!\n");
156 if (temp & GS_TO_STS) {
158 dev_dbg(&adap->dev, "SMBus protocol timeout!\n");
161 if (temp & GS_HCYC_STS)
162 dev_dbg(&adap->dev, "SMBus protocol success!\n");
164 outw_p(GS_CLEAR_STS, SMB_GLOBAL_STATUS);
167 if (((temp = inw_p(SMB_GLOBAL_STATUS)) & GS_CLEAR_STS) != 0x00) {
169 "Failed reset at end of transaction (%04x)\n", temp);
174 "Transaction (post): GS=%04x, GE=%04x, ADD=%04x, DAT=%04x\n",
175 inw_p(SMB_GLOBAL_STATUS), inw_p(SMB_GLOBAL_ENABLE),
176 inw_p(SMB_HOST_ADDRESS), inb_p(SMB_HOST_DATA));
181 dev_warn(&adap->dev, "Sending abort\n");
182 outw_p(inw(SMB_GLOBAL_ENABLE) | GE_ABORT, SMB_GLOBAL_ENABLE);
184 outw_p(GS_CLEAR_STS, SMB_GLOBAL_STATUS);
188 /* Return negative errno on error. */
189 static s32 amd756_access(struct i2c_adapter * adap, u16 addr,
190 unsigned short flags, char read_write,
191 u8 command, int size, union i2c_smbus_data * data)
197 case I2C_SMBUS_QUICK:
198 outw_p(((addr & 0x7f) << 1) | (read_write & 0x01),
203 outw_p(((addr & 0x7f) << 1) | (read_write & 0x01),
205 if (read_write == I2C_SMBUS_WRITE)
206 outb_p(command, SMB_HOST_DATA);
209 case I2C_SMBUS_BYTE_DATA:
210 outw_p(((addr & 0x7f) << 1) | (read_write & 0x01),
212 outb_p(command, SMB_HOST_COMMAND);
213 if (read_write == I2C_SMBUS_WRITE)
214 outw_p(data->byte, SMB_HOST_DATA);
215 size = AMD756_BYTE_DATA;
217 case I2C_SMBUS_WORD_DATA:
218 outw_p(((addr & 0x7f) << 1) | (read_write & 0x01),
220 outb_p(command, SMB_HOST_COMMAND);
221 if (read_write == I2C_SMBUS_WRITE)
222 outw_p(data->word, SMB_HOST_DATA); /* TODO: endian???? */
223 size = AMD756_WORD_DATA;
225 case I2C_SMBUS_BLOCK_DATA:
226 outw_p(((addr & 0x7f) << 1) | (read_write & 0x01),
228 outb_p(command, SMB_HOST_COMMAND);
229 if (read_write == I2C_SMBUS_WRITE) {
230 len = data->block[0];
235 outw_p(len, SMB_HOST_DATA);
236 /* i = inw_p(SMBHSTCNT); Reset SMBBLKDAT */
237 for (i = 1; i <= len; i++)
238 outb_p(data->block[i],
239 SMB_HOST_BLOCK_DATA);
241 size = AMD756_BLOCK_DATA;
244 dev_warn(&adap->dev, "Unsupported transaction %d\n", size);
248 /* How about enabling interrupts... */
249 outw_p(size & GE_CYC_TYPE_MASK, SMB_GLOBAL_ENABLE);
251 status = amd756_transaction(adap);
255 if ((read_write == I2C_SMBUS_WRITE) || (size == AMD756_QUICK))
261 data->byte = inw_p(SMB_HOST_DATA);
263 case AMD756_BYTE_DATA:
264 data->byte = inw_p(SMB_HOST_DATA);
266 case AMD756_WORD_DATA:
267 data->word = inw_p(SMB_HOST_DATA); /* TODO: endian???? */
269 case AMD756_BLOCK_DATA:
270 data->block[0] = inw_p(SMB_HOST_DATA) & 0x3f;
271 if(data->block[0] > 32)
273 /* i = inw_p(SMBHSTCNT); Reset SMBBLKDAT */
274 for (i = 1; i <= data->block[0]; i++)
275 data->block[i] = inb_p(SMB_HOST_BLOCK_DATA);
282 static u32 amd756_func(struct i2c_adapter *adapter)
284 return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
285 I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
286 I2C_FUNC_SMBUS_BLOCK_DATA;
289 static const struct i2c_algorithm smbus_algorithm = {
290 .smbus_xfer = amd756_access,
291 .functionality = amd756_func,
294 struct i2c_adapter amd756_smbus = {
295 .owner = THIS_MODULE,
296 .class = I2C_CLASS_HWMON | I2C_CLASS_SPD,
297 .algo = &smbus_algorithm,
300 enum chiptype { AMD756, AMD766, AMD768, NFORCE, AMD8111 };
301 static const char* chipname[] = {
302 "AMD756", "AMD766", "AMD768",
303 "nVidia nForce", "AMD8111",
306 static const struct pci_device_id amd756_ids[] = {
307 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_VIPER_740B),
308 .driver_data = AMD756 },
309 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_VIPER_7413),
310 .driver_data = AMD766 },
311 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_OPUS_7443),
312 .driver_data = AMD768 },
313 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_8111_SMBUS),
314 .driver_data = AMD8111 },
315 { PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_SMBUS),
316 .driver_data = NFORCE },
320 MODULE_DEVICE_TABLE (pci, amd756_ids);
322 static int amd756_probe(struct pci_dev *pdev, const struct pci_device_id *id)
324 int nforce = (id->driver_data == NFORCE);
329 dev_err(&pdev->dev, "Only one device supported "
330 "(you have a strange motherboard, btw)\n");
335 if (PCI_FUNC(pdev->devfn) != 1)
338 pci_read_config_word(pdev, SMBBANFORCE, &amd756_ioport);
339 amd756_ioport &= 0xfffc;
341 if (PCI_FUNC(pdev->devfn) != 3)
344 pci_read_config_byte(pdev, SMBGCFG, &temp);
345 if ((temp & 128) == 0) {
347 "Error: SMBus controller I/O not enabled!\n");
351 /* Determine the address of the SMBus areas */
352 /* Technically it is a dword but... */
353 pci_read_config_word(pdev, SMBBA, &amd756_ioport);
354 amd756_ioport &= 0xff00;
355 amd756_ioport += SMB_ADDR_OFFSET;
358 error = acpi_check_region(amd756_ioport, SMB_IOSIZE,
363 if (!request_region(amd756_ioport, SMB_IOSIZE, amd756_driver.name)) {
364 dev_err(&pdev->dev, "SMB region 0x%x already in use!\n",
369 pci_read_config_byte(pdev, SMBREV, &temp);
370 dev_dbg(&pdev->dev, "SMBREV = 0x%X\n", temp);
371 dev_dbg(&pdev->dev, "AMD756_smba = 0x%X\n", amd756_ioport);
373 /* set up the sysfs linkage to our parent device */
374 amd756_smbus.dev.parent = &pdev->dev;
376 snprintf(amd756_smbus.name, sizeof(amd756_smbus.name),
377 "SMBus %s adapter at %04x", chipname[id->driver_data],
380 error = i2c_add_adapter(&amd756_smbus);
387 release_region(amd756_ioport, SMB_IOSIZE);
391 static void amd756_remove(struct pci_dev *dev)
393 i2c_del_adapter(&amd756_smbus);
394 release_region(amd756_ioport, SMB_IOSIZE);
397 static struct pci_driver amd756_driver = {
398 .name = "amd756_smbus",
399 .id_table = amd756_ids,
400 .probe = amd756_probe,
401 .remove = amd756_remove,
404 module_pci_driver(amd756_driver);
407 MODULE_DESCRIPTION("AMD756/766/768/8111 and nVidia nForce SMBus driver");
408 MODULE_LICENSE("GPL");
410 EXPORT_SYMBOL(amd756_smbus);