blob: 69d073049e14382ea99668dd05431ce5c1a62ee1 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 SMBus driver for nVidia nForce2 MCP
3
4 Added nForce3 Pro 150 Thomas Leibold <thomas@plx.com>,
5 Ported to 2.5 Patrick Dreker <patrick@dreker.de>,
6 Copyright (c) 2003 Hans-Frieder Vogt <hfvogt@arcor.de>,
7 Based on
8 SMBus 2.0 driver for AMD-8111 IO-Hub
9 Copyright (c) 2002 Vojtech Pavlik
10
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2 of the License, or
14 (at your option) any later version.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24*/
25
26/*
27 SUPPORTED DEVICES PCI ID
28 nForce2 MCP 0064
29 nForce2 Ultra 400 MCP 0084
30 nForce3 Pro150 MCP 00D4
31 nForce3 250Gb MCP 00E4
32 nForce4 MCP 0052
Jean Delvare7c72ccf2005-12-18 17:25:18 +010033 nForce4 MCP-04 0034
Jean Delvare5c7ae652006-04-25 14:18:16 +020034 nForce4 MCP51 0264
35 nForce4 MCP55 0368
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
37 This driver supports the 2 SMBuses that are included in the MCP of the
Hans-Frieder Vogtad04d5c2006-12-10 21:21:29 +010038 nForce2/3/4/5xx chipsets.
Linus Torvalds1da177e2005-04-16 15:20:36 -070039*/
40
41/* Note: we assume there can only be one nForce2, with two SMBus interfaces */
42
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/module.h>
44#include <linux/pci.h>
45#include <linux/kernel.h>
46#include <linux/stddef.h>
47#include <linux/sched.h>
48#include <linux/ioport.h>
49#include <linux/init.h>
50#include <linux/i2c.h>
51#include <linux/delay.h>
52#include <asm/io.h>
53
54MODULE_LICENSE("GPL");
Hans-Frieder Vogtad04d5c2006-12-10 21:21:29 +010055MODULE_AUTHOR ("Hans-Frieder Vogt <hfvogt@gmx.net>");
56MODULE_DESCRIPTION("nForce2/3/4/5xx SMBus driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
58
59struct nforce2_smbus {
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 struct i2c_adapter adapter;
61 int base;
62 int size;
63};
64
65
66/*
67 * nVidia nForce2 SMBus control register definitions
Jean Delvare5c7ae652006-04-25 14:18:16 +020068 * (Newer incarnations use standard BARs 4 and 5 instead)
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 */
70#define NFORCE_PCI_SMB1 0x50
71#define NFORCE_PCI_SMB2 0x54
72
73
74/*
75 * ACPI 2.0 chapter 13 SMBus 2.0 EC register model
76 */
77#define NVIDIA_SMB_PRTCL (smbus->base + 0x00) /* protocol, PEC */
78#define NVIDIA_SMB_STS (smbus->base + 0x01) /* status */
79#define NVIDIA_SMB_ADDR (smbus->base + 0x02) /* address */
80#define NVIDIA_SMB_CMD (smbus->base + 0x03) /* command */
81#define NVIDIA_SMB_DATA (smbus->base + 0x04) /* 32 data registers */
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
83#define NVIDIA_SMB_STS_DONE 0x80
84#define NVIDIA_SMB_STS_ALRM 0x40
85#define NVIDIA_SMB_STS_RES 0x20
86#define NVIDIA_SMB_STS_STATUS 0x1f
87
88#define NVIDIA_SMB_PRTCL_WRITE 0x00
89#define NVIDIA_SMB_PRTCL_READ 0x01
90#define NVIDIA_SMB_PRTCL_QUICK 0x02
91#define NVIDIA_SMB_PRTCL_BYTE 0x04
92#define NVIDIA_SMB_PRTCL_BYTE_DATA 0x06
93#define NVIDIA_SMB_PRTCL_WORD_DATA 0x08
Linus Torvalds1da177e2005-04-16 15:20:36 -070094#define NVIDIA_SMB_PRTCL_PEC 0x80
95
Jean Delvared6072f82005-09-25 16:37:04 +020096static struct pci_driver nforce2_driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Hans-Frieder Vogtad04d5c2006-12-10 21:21:29 +010098/* Return -1 on error */
Linus Torvalds1da177e2005-04-16 15:20:36 -070099static s32 nforce2_access(struct i2c_adapter * adap, u16 addr,
100 unsigned short flags, char read_write,
101 u8 command, int size, union i2c_smbus_data * data)
102{
103 struct nforce2_smbus *smbus = adap->algo_data;
104 unsigned char protocol, pec, temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
106 protocol = (read_write == I2C_SMBUS_READ) ? NVIDIA_SMB_PRTCL_READ :
107 NVIDIA_SMB_PRTCL_WRITE;
108 pec = (flags & I2C_CLIENT_PEC) ? NVIDIA_SMB_PRTCL_PEC : 0;
109
110 switch (size) {
111
112 case I2C_SMBUS_QUICK:
113 protocol |= NVIDIA_SMB_PRTCL_QUICK;
114 read_write = I2C_SMBUS_WRITE;
115 break;
116
117 case I2C_SMBUS_BYTE:
118 if (read_write == I2C_SMBUS_WRITE)
119 outb_p(command, NVIDIA_SMB_CMD);
120 protocol |= NVIDIA_SMB_PRTCL_BYTE;
121 break;
122
123 case I2C_SMBUS_BYTE_DATA:
124 outb_p(command, NVIDIA_SMB_CMD);
125 if (read_write == I2C_SMBUS_WRITE)
126 outb_p(data->byte, NVIDIA_SMB_DATA);
127 protocol |= NVIDIA_SMB_PRTCL_BYTE_DATA;
128 break;
129
130 case I2C_SMBUS_WORD_DATA:
131 outb_p(command, NVIDIA_SMB_CMD);
132 if (read_write == I2C_SMBUS_WRITE) {
133 outb_p(data->word, NVIDIA_SMB_DATA);
134 outb_p(data->word >> 8, NVIDIA_SMB_DATA+1);
135 }
136 protocol |= NVIDIA_SMB_PRTCL_WORD_DATA | pec;
137 break;
138
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 default:
140 dev_err(&adap->dev, "Unsupported transaction %d\n", size);
141 return -1;
142 }
143
144 outb_p((addr & 0x7f) << 1, NVIDIA_SMB_ADDR);
145 outb_p(protocol, NVIDIA_SMB_PRTCL);
146
147 temp = inb_p(NVIDIA_SMB_STS);
148
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 if (~temp & NVIDIA_SMB_STS_DONE) {
150 udelay(500);
151 temp = inb_p(NVIDIA_SMB_STS);
152 }
153 if (~temp & NVIDIA_SMB_STS_DONE) {
154 msleep(10);
155 temp = inb_p(NVIDIA_SMB_STS);
156 }
157
Hans-Frieder Vogt50330172005-07-23 15:33:39 +0200158 if ((~temp & NVIDIA_SMB_STS_DONE) || (temp & NVIDIA_SMB_STS_STATUS)) {
159 dev_dbg(&adap->dev, "SMBus Timeout! (0x%02x)\n", temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 return -1;
Hans-Frieder Vogt50330172005-07-23 15:33:39 +0200161 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
163 if (read_write == I2C_SMBUS_WRITE)
164 return 0;
165
166 switch (size) {
167
168 case I2C_SMBUS_BYTE:
169 case I2C_SMBUS_BYTE_DATA:
170 data->byte = inb_p(NVIDIA_SMB_DATA);
171 break;
172
173 case I2C_SMBUS_WORD_DATA:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 data->word = inb_p(NVIDIA_SMB_DATA) | (inb_p(NVIDIA_SMB_DATA+1) << 8);
175 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 }
177
178 return 0;
179}
180
181
182static u32 nforce2_func(struct i2c_adapter *adapter)
183{
184 /* other functionality might be possible, but is not tested */
185 return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
Hans-Frieder Vogtad04d5c2006-12-10 21:21:29 +0100186 I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187}
188
Hans-Frieder Vogtad04d5c2006-12-10 21:21:29 +0100189static struct i2c_algorithm smbus_algorithm = {
190 .smbus_xfer = nforce2_access,
191 .functionality = nforce2_func,
192};
193
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
195static struct pci_device_id nforce2_ids[] = {
196 { PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE2_SMBUS) },
197 { PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE2S_SMBUS) },
198 { PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE3_SMBUS) },
199 { PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE3S_SMBUS) },
200 { PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE4_SMBUS) },
Jean Delvare7c72ccf2005-12-18 17:25:18 +0100201 { PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP04_SMBUS) },
Jean Delvare5c7ae652006-04-25 14:18:16 +0200202 { PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP51_SMBUS) },
203 { PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP55_SMBUS) },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 { 0 }
205};
206
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207MODULE_DEVICE_TABLE (pci, nforce2_ids);
208
209
Jean Delvare5c7ae652006-04-25 14:18:16 +0200210static int __devinit nforce2_probe_smb (struct pci_dev *dev, int bar,
211 int alt_reg, struct nforce2_smbus *smbus, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 int error;
214
Jean Delvare5c7ae652006-04-25 14:18:16 +0200215 smbus->base = pci_resource_start(dev, bar);
216 if (smbus->base) {
217 smbus->size = pci_resource_len(dev, bar);
218 } else {
219 /* Older incarnations of the device used non-standard BARs */
220 u16 iobase;
221
222 if (pci_read_config_word(dev, alt_reg, &iobase)
223 != PCIBIOS_SUCCESSFUL) {
224 dev_err(&dev->dev, "Error reading PCI config for %s\n",
225 name);
226 return -1;
227 }
228
229 smbus->base = iobase & PCI_BASE_ADDRESS_IO_MASK;
Hans-Frieder Vogtad04d5c2006-12-10 21:21:29 +0100230 smbus->size = 64;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
Jean Delvared6072f82005-09-25 16:37:04 +0200233 if (!request_region(smbus->base, smbus->size, nforce2_driver.name)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 dev_err(&smbus->adapter.dev, "Error requesting region %02x .. %02X for %s\n",
235 smbus->base, smbus->base+smbus->size-1, name);
236 return -1;
237 }
Hans-Frieder Vogtad04d5c2006-12-10 21:21:29 +0100238 smbus->adapter.owner = THIS_MODULE;
239 smbus->adapter.class = I2C_CLASS_HWMON;
240 smbus->adapter.algo = &smbus_algorithm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 smbus->adapter.algo_data = smbus;
242 smbus->adapter.dev.parent = &dev->dev;
243 snprintf(smbus->adapter.name, I2C_NAME_SIZE,
244 "SMBus nForce2 adapter at %04x", smbus->base);
245
246 error = i2c_add_adapter(&smbus->adapter);
247 if (error) {
248 dev_err(&smbus->adapter.dev, "Failed to register adapter.\n");
249 release_region(smbus->base, smbus->size);
250 return -1;
251 }
252 dev_info(&smbus->adapter.dev, "nForce2 SMBus adapter at %#x\n", smbus->base);
253 return 0;
254}
255
256
257static int __devinit nforce2_probe(struct pci_dev *dev, const struct pci_device_id *id)
258{
259 struct nforce2_smbus *smbuses;
260 int res1, res2;
261
262 /* we support 2 SMBus adapters */
Jean Delvare078d9fe2005-10-17 23:12:36 +0200263 if (!(smbuses = kzalloc(2*sizeof(struct nforce2_smbus), GFP_KERNEL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 pci_set_drvdata(dev, smbuses);
266
267 /* SMBus adapter 1 */
Jean Delvare5c7ae652006-04-25 14:18:16 +0200268 res1 = nforce2_probe_smb(dev, 4, NFORCE_PCI_SMB1, &smbuses[0], "SMB1");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 if (res1 < 0) {
270 dev_err(&dev->dev, "Error probing SMB1.\n");
271 smbuses[0].base = 0; /* to have a check value */
272 }
Jean Delvare5c7ae652006-04-25 14:18:16 +0200273 /* SMBus adapter 2 */
274 res2 = nforce2_probe_smb(dev, 5, NFORCE_PCI_SMB2, &smbuses[1], "SMB2");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 if (res2 < 0) {
276 dev_err(&dev->dev, "Error probing SMB2.\n");
277 smbuses[1].base = 0; /* to have a check value */
278 }
279 if ((res1 < 0) && (res2 < 0)) {
280 /* we did not find even one of the SMBuses, so we give up */
281 kfree(smbuses);
282 return -ENODEV;
283 }
284
285 return 0;
286}
287
288
289static void __devexit nforce2_remove(struct pci_dev *dev)
290{
291 struct nforce2_smbus *smbuses = (void*) pci_get_drvdata(dev);
292
293 if (smbuses[0].base) {
294 i2c_del_adapter(&smbuses[0].adapter);
295 release_region(smbuses[0].base, smbuses[0].size);
296 }
297 if (smbuses[1].base) {
298 i2c_del_adapter(&smbuses[1].adapter);
299 release_region(smbuses[1].base, smbuses[1].size);
300 }
301 kfree(smbuses);
302}
303
304static struct pci_driver nforce2_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 .name = "nForce2_smbus",
306 .id_table = nforce2_ids,
307 .probe = nforce2_probe,
308 .remove = __devexit_p(nforce2_remove),
309};
310
311static int __init nforce2_init(void)
312{
313 return pci_register_driver(&nforce2_driver);
314}
315
316static void __exit nforce2_exit(void)
317{
318 pci_unregister_driver(&nforce2_driver);
319}
320
321module_init(nforce2_init);
322module_exit(nforce2_exit);
323