blob: 26acc657e5c93b49f3bf228eba55036ea7159051 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 amd756.c - Part of lm_sensors, Linux kernel modules for hardware
3 monitoring
4
5 Copyright (c) 1999-2002 Merlin Hughes <merlin@merlin.org>
6
7 Shamelessly ripped from i2c-piix4.c:
8
9 Copyright (c) 1998, 1999 Frodo Looijaard <frodol@dds.nl> and
10 Philip Edelbrock <phil@netroedge.com>
11
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2 of the License, or
15 (at your option) any later version.
16
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
21
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25*/
26
27/*
28 2002-04-08: Added nForce support. (Csaba Halasz)
29 2002-10-03: Fixed nForce PnP I/O port. (Michael Steil)
30 2002-12-28: Rewritten into something that resembles a Linux driver (hch)
31 2003-11-29: Added back AMD8111 removed by the previous rewrite.
32 (Philip Pokorny)
33*/
34
35/*
36 Supports AMD756, AMD766, AMD768, AMD8111 and nVidia nForce
37 Note: we assume there can only be one device, with one SMBus interface.
38*/
39
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <linux/module.h>
41#include <linux/pci.h>
42#include <linux/kernel.h>
43#include <linux/delay.h>
44#include <linux/stddef.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/ioport.h>
46#include <linux/i2c.h>
47#include <linux/init.h>
Jean Delvare54fb4a052008-07-14 22:38:33 +020048#include <linux/acpi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <asm/io.h>
50
51/* AMD756 SMBus address offsets */
52#define SMB_ADDR_OFFSET 0xE0
53#define SMB_IOSIZE 16
54#define SMB_GLOBAL_STATUS (0x0 + amd756_ioport)
55#define SMB_GLOBAL_ENABLE (0x2 + amd756_ioport)
56#define SMB_HOST_ADDRESS (0x4 + amd756_ioport)
57#define SMB_HOST_DATA (0x6 + amd756_ioport)
58#define SMB_HOST_COMMAND (0x8 + amd756_ioport)
59#define SMB_HOST_BLOCK_DATA (0x9 + amd756_ioport)
60#define SMB_HAS_DATA (0xA + amd756_ioport)
61#define SMB_HAS_DEVICE_ADDRESS (0xC + amd756_ioport)
62#define SMB_HAS_HOST_ADDRESS (0xE + amd756_ioport)
63#define SMB_SNOOP_ADDRESS (0xF + amd756_ioport)
64
65/* PCI Address Constants */
66
67/* address of I/O space */
68#define SMBBA 0x058 /* mh */
69#define SMBBANFORCE 0x014
70
71/* general configuration */
72#define SMBGCFG 0x041 /* mh */
73
74/* silicon revision code */
75#define SMBREV 0x008
76
77/* Other settings */
78#define MAX_TIMEOUT 500
79
80/* AMD756 constants */
81#define AMD756_QUICK 0x00
82#define AMD756_BYTE 0x01
83#define AMD756_BYTE_DATA 0x02
84#define AMD756_WORD_DATA 0x03
85#define AMD756_PROCESS_CALL 0x04
86#define AMD756_BLOCK_DATA 0x05
87
Jean Delvared6072f82005-09-25 16:37:04 +020088static struct pci_driver amd756_driver;
Jean Delvare60507092005-09-25 16:23:07 +020089static unsigned short amd756_ioport;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
91/*
92 SMBUS event = I/O 28-29 bit 11
93 see E0 for the status bits and enabled in E2
94
95*/
96#define GS_ABRT_STS (1 << 0)
97#define GS_COL_STS (1 << 1)
98#define GS_PRERR_STS (1 << 2)
99#define GS_HST_STS (1 << 3)
100#define GS_HCYC_STS (1 << 4)
101#define GS_TO_STS (1 << 5)
102#define GS_SMB_STS (1 << 11)
103
104#define GS_CLEAR_STS (GS_ABRT_STS | GS_COL_STS | GS_PRERR_STS | \
105 GS_HCYC_STS | GS_TO_STS )
106
107#define GE_CYC_TYPE_MASK (7)
108#define GE_HOST_STC (1 << 3)
109#define GE_ABORT (1 << 5)
110
111
112static int amd756_transaction(struct i2c_adapter *adap)
113{
114 int temp;
115 int result = 0;
116 int timeout = 0;
117
118 dev_dbg(&adap->dev, "Transaction (pre): GS=%04x, GE=%04x, ADD=%04x, "
119 "DAT=%04x\n", inw_p(SMB_GLOBAL_STATUS),
120 inw_p(SMB_GLOBAL_ENABLE), inw_p(SMB_HOST_ADDRESS),
121 inb_p(SMB_HOST_DATA));
122
123 /* Make sure the SMBus host is ready to start transmitting */
124 if ((temp = inw_p(SMB_GLOBAL_STATUS)) & (GS_HST_STS | GS_SMB_STS)) {
125 dev_dbg(&adap->dev, "SMBus busy (%04x). Waiting...\n", temp);
126 do {
127 msleep(1);
128 temp = inw_p(SMB_GLOBAL_STATUS);
129 } while ((temp & (GS_HST_STS | GS_SMB_STS)) &&
130 (timeout++ < MAX_TIMEOUT));
131 /* If the SMBus is still busy, we give up */
132 if (timeout >= MAX_TIMEOUT) {
133 dev_dbg(&adap->dev, "Busy wait timeout (%04x)\n", temp);
134 goto abort;
135 }
136 timeout = 0;
137 }
138
139 /* start the transaction by setting the start bit */
140 outw_p(inw(SMB_GLOBAL_ENABLE) | GE_HOST_STC, SMB_GLOBAL_ENABLE);
141
142 /* We will always wait for a fraction of a second! */
143 do {
144 msleep(1);
145 temp = inw_p(SMB_GLOBAL_STATUS);
146 } while ((temp & GS_HST_STS) && (timeout++ < MAX_TIMEOUT));
147
148 /* If the SMBus is still busy, we give up */
149 if (timeout >= MAX_TIMEOUT) {
150 dev_dbg(&adap->dev, "Completion timeout!\n");
151 goto abort;
152 }
153
154 if (temp & GS_PRERR_STS) {
David Brownell97140342008-07-14 22:38:25 +0200155 result = -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 dev_dbg(&adap->dev, "SMBus Protocol error (no response)!\n");
157 }
158
159 if (temp & GS_COL_STS) {
David Brownell97140342008-07-14 22:38:25 +0200160 result = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 dev_warn(&adap->dev, "SMBus collision!\n");
162 }
163
164 if (temp & GS_TO_STS) {
David Brownell97140342008-07-14 22:38:25 +0200165 result = -ETIMEDOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 dev_dbg(&adap->dev, "SMBus protocol timeout!\n");
167 }
168
169 if (temp & GS_HCYC_STS)
170 dev_dbg(&adap->dev, "SMBus protocol success!\n");
171
172 outw_p(GS_CLEAR_STS, SMB_GLOBAL_STATUS);
173
174#ifdef DEBUG
175 if (((temp = inw_p(SMB_GLOBAL_STATUS)) & GS_CLEAR_STS) != 0x00) {
176 dev_dbg(&adap->dev,
177 "Failed reset at end of transaction (%04x)\n", temp);
178 }
179#endif
180
181 dev_dbg(&adap->dev,
182 "Transaction (post): GS=%04x, GE=%04x, ADD=%04x, DAT=%04x\n",
183 inw_p(SMB_GLOBAL_STATUS), inw_p(SMB_GLOBAL_ENABLE),
184 inw_p(SMB_HOST_ADDRESS), inb_p(SMB_HOST_DATA));
185
186 return result;
187
188 abort:
189 dev_warn(&adap->dev, "Sending abort\n");
190 outw_p(inw(SMB_GLOBAL_ENABLE) | GE_ABORT, SMB_GLOBAL_ENABLE);
191 msleep(100);
192 outw_p(GS_CLEAR_STS, SMB_GLOBAL_STATUS);
David Brownell97140342008-07-14 22:38:25 +0200193 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194}
195
David Brownell97140342008-07-14 22:38:25 +0200196/* Return negative errno on error. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197static s32 amd756_access(struct i2c_adapter * adap, u16 addr,
198 unsigned short flags, char read_write,
199 u8 command, int size, union i2c_smbus_data * data)
200{
201 int i, len;
David Brownell97140342008-07-14 22:38:25 +0200202 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 switch (size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 case I2C_SMBUS_QUICK:
206 outw_p(((addr & 0x7f) << 1) | (read_write & 0x01),
207 SMB_HOST_ADDRESS);
208 size = AMD756_QUICK;
209 break;
210 case I2C_SMBUS_BYTE:
211 outw_p(((addr & 0x7f) << 1) | (read_write & 0x01),
212 SMB_HOST_ADDRESS);
213 if (read_write == I2C_SMBUS_WRITE)
214 outb_p(command, SMB_HOST_DATA);
215 size = AMD756_BYTE;
216 break;
217 case I2C_SMBUS_BYTE_DATA:
218 outw_p(((addr & 0x7f) << 1) | (read_write & 0x01),
219 SMB_HOST_ADDRESS);
220 outb_p(command, SMB_HOST_COMMAND);
221 if (read_write == I2C_SMBUS_WRITE)
222 outw_p(data->byte, SMB_HOST_DATA);
223 size = AMD756_BYTE_DATA;
224 break;
225 case I2C_SMBUS_WORD_DATA:
226 outw_p(((addr & 0x7f) << 1) | (read_write & 0x01),
227 SMB_HOST_ADDRESS);
228 outb_p(command, SMB_HOST_COMMAND);
229 if (read_write == I2C_SMBUS_WRITE)
230 outw_p(data->word, SMB_HOST_DATA); /* TODO: endian???? */
231 size = AMD756_WORD_DATA;
232 break;
233 case I2C_SMBUS_BLOCK_DATA:
234 outw_p(((addr & 0x7f) << 1) | (read_write & 0x01),
235 SMB_HOST_ADDRESS);
236 outb_p(command, SMB_HOST_COMMAND);
237 if (read_write == I2C_SMBUS_WRITE) {
238 len = data->block[0];
239 if (len < 0)
240 len = 0;
241 if (len > 32)
242 len = 32;
243 outw_p(len, SMB_HOST_DATA);
244 /* i = inw_p(SMBHSTCNT); Reset SMBBLKDAT */
245 for (i = 1; i <= len; i++)
246 outb_p(data->block[i],
247 SMB_HOST_BLOCK_DATA);
248 }
249 size = AMD756_BLOCK_DATA;
250 break;
Jean Delvareac7fc4f2008-07-14 22:38:25 +0200251 default:
252 dev_warn(&adap->dev, "Unsupported transaction %d\n", size);
253 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 }
255
256 /* How about enabling interrupts... */
257 outw_p(size & GE_CYC_TYPE_MASK, SMB_GLOBAL_ENABLE);
258
David Brownell97140342008-07-14 22:38:25 +0200259 status = amd756_transaction(adap);
260 if (status)
261 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
263 if ((read_write == I2C_SMBUS_WRITE) || (size == AMD756_QUICK))
264 return 0;
265
266
267 switch (size) {
268 case AMD756_BYTE:
269 data->byte = inw_p(SMB_HOST_DATA);
270 break;
271 case AMD756_BYTE_DATA:
272 data->byte = inw_p(SMB_HOST_DATA);
273 break;
274 case AMD756_WORD_DATA:
275 data->word = inw_p(SMB_HOST_DATA); /* TODO: endian???? */
276 break;
277 case AMD756_BLOCK_DATA:
278 data->block[0] = inw_p(SMB_HOST_DATA) & 0x3f;
279 if(data->block[0] > 32)
280 data->block[0] = 32;
281 /* i = inw_p(SMBHSTCNT); Reset SMBBLKDAT */
282 for (i = 1; i <= data->block[0]; i++)
283 data->block[i] = inb_p(SMB_HOST_BLOCK_DATA);
284 break;
285 }
286
287 return 0;
288}
289
290static u32 amd756_func(struct i2c_adapter *adapter)
291{
292 return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
293 I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
Jean Delvare875b0a42008-05-18 20:49:41 +0200294 I2C_FUNC_SMBUS_BLOCK_DATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295}
296
Jean Delvare8f9082c2006-09-03 22:39:46 +0200297static const struct i2c_algorithm smbus_algorithm = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 .smbus_xfer = amd756_access,
299 .functionality = amd756_func,
300};
301
302struct i2c_adapter amd756_smbus = {
303 .owner = THIS_MODULE,
Stephen Hemminger9ace5552007-02-13 22:09:01 +0100304 .id = I2C_HW_SMBUS_AMD756,
Jean Delvare3401b2f2008-07-14 22:38:29 +0200305 .class = I2C_CLASS_HWMON | I2C_CLASS_SPD,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 .algo = &smbus_algorithm,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307};
308
309enum chiptype { AMD756, AMD766, AMD768, NFORCE, AMD8111 };
310static const char* chipname[] = {
311 "AMD756", "AMD766", "AMD768",
312 "nVidia nForce", "AMD8111",
313};
314
315static struct pci_device_id amd756_ids[] = {
316 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_VIPER_740B),
317 .driver_data = AMD756 },
318 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_VIPER_7413),
319 .driver_data = AMD766 },
320 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_OPUS_7443),
321 .driver_data = AMD768 },
322 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_8111_SMBUS),
323 .driver_data = AMD8111 },
324 { PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_SMBUS),
325 .driver_data = NFORCE },
326 { 0, }
327};
328
329MODULE_DEVICE_TABLE (pci, amd756_ids);
330
331static int __devinit amd756_probe(struct pci_dev *pdev,
332 const struct pci_device_id *id)
333{
334 int nforce = (id->driver_data == NFORCE);
335 int error;
336 u8 temp;
337
Jean Delvare0f07a242008-01-27 18:14:51 +0100338 /* driver_data might come from user-space, so check it */
Adrian Bunk5edc68b2008-03-12 14:15:00 +0100339 if (id->driver_data >= ARRAY_SIZE(chipname))
Jean Delvare0f07a242008-01-27 18:14:51 +0100340 return -EINVAL;
341
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 if (amd756_ioport) {
343 dev_err(&pdev->dev, "Only one device supported "
344 "(you have a strange motherboard, btw)\n");
345 return -ENODEV;
346 }
347
348 if (nforce) {
349 if (PCI_FUNC(pdev->devfn) != 1)
350 return -ENODEV;
351
352 pci_read_config_word(pdev, SMBBANFORCE, &amd756_ioport);
353 amd756_ioport &= 0xfffc;
354 } else { /* amd */
355 if (PCI_FUNC(pdev->devfn) != 3)
356 return -ENODEV;
357
358 pci_read_config_byte(pdev, SMBGCFG, &temp);
359 if ((temp & 128) == 0) {
360 dev_err(&pdev->dev,
361 "Error: SMBus controller I/O not enabled!\n");
362 return -ENODEV;
363 }
364
365 /* Determine the address of the SMBus areas */
366 /* Technically it is a dword but... */
367 pci_read_config_word(pdev, SMBBA, &amd756_ioport);
368 amd756_ioport &= 0xff00;
369 amd756_ioport += SMB_ADDR_OFFSET;
370 }
371
Jean Delvare54fb4a052008-07-14 22:38:33 +0200372 error = acpi_check_region(amd756_ioport, SMB_IOSIZE,
373 amd756_driver.name);
374 if (error)
375 return error;
376
Jean Delvared6072f82005-09-25 16:37:04 +0200377 if (!request_region(amd756_ioport, SMB_IOSIZE, amd756_driver.name)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 dev_err(&pdev->dev, "SMB region 0x%x already in use!\n",
379 amd756_ioport);
380 return -ENODEV;
381 }
382
383 pci_read_config_byte(pdev, SMBREV, &temp);
384 dev_dbg(&pdev->dev, "SMBREV = 0x%X\n", temp);
385 dev_dbg(&pdev->dev, "AMD756_smba = 0x%X\n", amd756_ioport);
386
Robert P. J. Day405ae7d2007-02-17 19:13:42 +0100387 /* set up the sysfs linkage to our parent device */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 amd756_smbus.dev.parent = &pdev->dev;
389
390 sprintf(amd756_smbus.name, "SMBus %s adapter at %04x",
391 chipname[id->driver_data], amd756_ioport);
392
393 error = i2c_add_adapter(&amd756_smbus);
394 if (error) {
395 dev_err(&pdev->dev,
396 "Adapter registration failed, module not inserted\n");
397 goto out_err;
398 }
399
400 return 0;
401
402 out_err:
403 release_region(amd756_ioport, SMB_IOSIZE);
404 return error;
405}
406
407static void __devexit amd756_remove(struct pci_dev *dev)
408{
409 i2c_del_adapter(&amd756_smbus);
410 release_region(amd756_ioport, SMB_IOSIZE);
411}
412
413static struct pci_driver amd756_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 .name = "amd756_smbus",
415 .id_table = amd756_ids,
416 .probe = amd756_probe,
417 .remove = __devexit_p(amd756_remove),
Jean Delvare0f07a242008-01-27 18:14:51 +0100418 .dynids.use_driver_data = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419};
420
421static int __init amd756_init(void)
422{
423 return pci_register_driver(&amd756_driver);
424}
425
426static void __exit amd756_exit(void)
427{
428 pci_unregister_driver(&amd756_driver);
429}
430
431MODULE_AUTHOR("Merlin Hughes <merlin@merlin.org>");
432MODULE_DESCRIPTION("AMD756/766/768/8111 and nVidia nForce SMBus driver");
433MODULE_LICENSE("GPL");
434
435EXPORT_SYMBOL(amd756_smbus);
436
437module_init(amd756_init)
438module_exit(amd756_exit)