blob: 1c63c4f2e4b6d79487fd1ae975bb4cd7738c6ccb [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
Jean Delvaref75803d2007-05-01 23:26:29 +020036 nForce MCP61 03EB
37 nForce MCP65 0446
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
39 This driver supports the 2 SMBuses that are included in the MCP of the
Hans-Frieder Vogtad04d5c2006-12-10 21:21:29 +010040 nForce2/3/4/5xx chipsets.
Linus Torvalds1da177e2005-04-16 15:20:36 -070041*/
42
43/* Note: we assume there can only be one nForce2, with two SMBus interfaces */
44
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/module.h>
46#include <linux/pci.h>
47#include <linux/kernel.h>
48#include <linux/stddef.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <linux/ioport.h>
50#include <linux/init.h>
51#include <linux/i2c.h>
52#include <linux/delay.h>
53#include <asm/io.h>
54
55MODULE_LICENSE("GPL");
Hans-Frieder Vogtad04d5c2006-12-10 21:21:29 +010056MODULE_AUTHOR ("Hans-Frieder Vogt <hfvogt@gmx.net>");
57MODULE_DESCRIPTION("nForce2/3/4/5xx SMBus driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
59
60struct nforce2_smbus {
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 struct i2c_adapter adapter;
62 int base;
63 int size;
Oleg Ryjkovb53c8222007-07-12 14:12:29 +020064 int blockops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065};
66
67
68/*
69 * nVidia nForce2 SMBus control register definitions
Jean Delvare5c7ae652006-04-25 14:18:16 +020070 * (Newer incarnations use standard BARs 4 and 5 instead)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 */
72#define NFORCE_PCI_SMB1 0x50
73#define NFORCE_PCI_SMB2 0x54
74
75
76/*
77 * ACPI 2.0 chapter 13 SMBus 2.0 EC register model
78 */
79#define NVIDIA_SMB_PRTCL (smbus->base + 0x00) /* protocol, PEC */
80#define NVIDIA_SMB_STS (smbus->base + 0x01) /* status */
81#define NVIDIA_SMB_ADDR (smbus->base + 0x02) /* address */
82#define NVIDIA_SMB_CMD (smbus->base + 0x03) /* command */
83#define NVIDIA_SMB_DATA (smbus->base + 0x04) /* 32 data registers */
Oleg Ryjkovb53c8222007-07-12 14:12:29 +020084#define NVIDIA_SMB_BCNT (smbus->base + 0x24) /* number of data
85 bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
87#define NVIDIA_SMB_STS_DONE 0x80
88#define NVIDIA_SMB_STS_ALRM 0x40
89#define NVIDIA_SMB_STS_RES 0x20
90#define NVIDIA_SMB_STS_STATUS 0x1f
91
92#define NVIDIA_SMB_PRTCL_WRITE 0x00
93#define NVIDIA_SMB_PRTCL_READ 0x01
94#define NVIDIA_SMB_PRTCL_QUICK 0x02
95#define NVIDIA_SMB_PRTCL_BYTE 0x04
96#define NVIDIA_SMB_PRTCL_BYTE_DATA 0x06
97#define NVIDIA_SMB_PRTCL_WORD_DATA 0x08
Oleg Ryjkovb53c8222007-07-12 14:12:29 +020098#define NVIDIA_SMB_PRTCL_BLOCK_DATA 0x0a
Linus Torvalds1da177e2005-04-16 15:20:36 -070099#define NVIDIA_SMB_PRTCL_PEC 0x80
100
Oleg Ryjkov41535492007-10-13 23:56:33 +0200101/* Misc definitions */
102#define MAX_TIMEOUT 100
103
Jean Delvared6072f82005-09-25 16:37:04 +0200104static struct pci_driver nforce2_driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
Oleg Ryjkov41535492007-10-13 23:56:33 +0200106static int nforce2_check_status(struct i2c_adapter *adap)
107{
108 struct nforce2_smbus *smbus = adap->algo_data;
109 int timeout = 0;
110 unsigned char temp;
111
112 do {
113 msleep(1);
114 temp = inb_p(NVIDIA_SMB_STS);
115 } while ((!temp) && (timeout++ < MAX_TIMEOUT));
116
117 if (timeout >= MAX_TIMEOUT) {
118 dev_dbg(&adap->dev, "SMBus Timeout!\n");
119 return -1;
120 }
121 if (!(temp & NVIDIA_SMB_STS_DONE) || (temp & NVIDIA_SMB_STS_STATUS)) {
122 dev_dbg(&adap->dev, "Transaction failed (0x%02x)!\n", temp);
123 return -1;
124 }
125 return 0;
126}
127
Hans-Frieder Vogtad04d5c2006-12-10 21:21:29 +0100128/* Return -1 on error */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129static s32 nforce2_access(struct i2c_adapter * adap, u16 addr,
130 unsigned short flags, char read_write,
131 u8 command, int size, union i2c_smbus_data * data)
132{
133 struct nforce2_smbus *smbus = adap->algo_data;
Oleg Ryjkov41535492007-10-13 23:56:33 +0200134 unsigned char protocol, pec;
Oleg Ryjkovb53c8222007-07-12 14:12:29 +0200135 u8 len;
136 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
138 protocol = (read_write == I2C_SMBUS_READ) ? NVIDIA_SMB_PRTCL_READ :
139 NVIDIA_SMB_PRTCL_WRITE;
140 pec = (flags & I2C_CLIENT_PEC) ? NVIDIA_SMB_PRTCL_PEC : 0;
141
142 switch (size) {
143
144 case I2C_SMBUS_QUICK:
145 protocol |= NVIDIA_SMB_PRTCL_QUICK;
146 read_write = I2C_SMBUS_WRITE;
147 break;
148
149 case I2C_SMBUS_BYTE:
150 if (read_write == I2C_SMBUS_WRITE)
151 outb_p(command, NVIDIA_SMB_CMD);
152 protocol |= NVIDIA_SMB_PRTCL_BYTE;
153 break;
154
155 case I2C_SMBUS_BYTE_DATA:
156 outb_p(command, NVIDIA_SMB_CMD);
157 if (read_write == I2C_SMBUS_WRITE)
158 outb_p(data->byte, NVIDIA_SMB_DATA);
159 protocol |= NVIDIA_SMB_PRTCL_BYTE_DATA;
160 break;
161
162 case I2C_SMBUS_WORD_DATA:
163 outb_p(command, NVIDIA_SMB_CMD);
164 if (read_write == I2C_SMBUS_WRITE) {
165 outb_p(data->word, NVIDIA_SMB_DATA);
166 outb_p(data->word >> 8, NVIDIA_SMB_DATA+1);
167 }
168 protocol |= NVIDIA_SMB_PRTCL_WORD_DATA | pec;
169 break;
170
Oleg Ryjkovb53c8222007-07-12 14:12:29 +0200171 case I2C_SMBUS_BLOCK_DATA:
172 outb_p(command, NVIDIA_SMB_CMD);
173 if (read_write == I2C_SMBUS_WRITE) {
174 len = data->block[0];
175 if ((len == 0) || (len > I2C_SMBUS_BLOCK_MAX)) {
176 dev_err(&adap->dev,
177 "Transaction failed "
178 "(requested block size: %d)\n",
179 len);
180 return -1;
181 }
182 outb_p(len, NVIDIA_SMB_BCNT);
183 for (i = 0; i < I2C_SMBUS_BLOCK_MAX; i++)
184 outb_p(data->block[i + 1],
185 NVIDIA_SMB_DATA+i);
186 }
187 protocol |= NVIDIA_SMB_PRTCL_BLOCK_DATA | pec;
188 break;
189
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 default:
191 dev_err(&adap->dev, "Unsupported transaction %d\n", size);
192 return -1;
193 }
194
195 outb_p((addr & 0x7f) << 1, NVIDIA_SMB_ADDR);
196 outb_p(protocol, NVIDIA_SMB_PRTCL);
197
Oleg Ryjkov41535492007-10-13 23:56:33 +0200198 if (nforce2_check_status(adap))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
201 if (read_write == I2C_SMBUS_WRITE)
202 return 0;
203
204 switch (size) {
205
206 case I2C_SMBUS_BYTE:
207 case I2C_SMBUS_BYTE_DATA:
208 data->byte = inb_p(NVIDIA_SMB_DATA);
209 break;
210
211 case I2C_SMBUS_WORD_DATA:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 data->word = inb_p(NVIDIA_SMB_DATA) | (inb_p(NVIDIA_SMB_DATA+1) << 8);
213 break;
Oleg Ryjkovb53c8222007-07-12 14:12:29 +0200214
215 case I2C_SMBUS_BLOCK_DATA:
216 len = inb_p(NVIDIA_SMB_BCNT);
Oleg Ryjkov41535492007-10-13 23:56:33 +0200217 if ((len <= 0) || (len > I2C_SMBUS_BLOCK_MAX)) {
218 dev_err(&adap->dev, "Transaction failed "
219 "(received block size: 0x%02x)\n",
220 len);
221 return -1;
222 }
Oleg Ryjkovb53c8222007-07-12 14:12:29 +0200223 for (i = 0; i < len; i++)
224 data->block[i+1] = inb_p(NVIDIA_SMB_DATA + i);
225 data->block[0] = len;
226 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 }
228
229 return 0;
230}
231
232
233static u32 nforce2_func(struct i2c_adapter *adapter)
234{
235 /* other functionality might be possible, but is not tested */
236 return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
Oleg Ryjkovb53c8222007-07-12 14:12:29 +0200237 I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
238 (((struct nforce2_smbus*)adapter->algo_data)->blockops ?
239 I2C_FUNC_SMBUS_BLOCK_DATA : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240}
241
Hans-Frieder Vogtad04d5c2006-12-10 21:21:29 +0100242static struct i2c_algorithm smbus_algorithm = {
243 .smbus_xfer = nforce2_access,
244 .functionality = nforce2_func,
245};
246
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
248static struct pci_device_id nforce2_ids[] = {
249 { PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE2_SMBUS) },
250 { PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE2S_SMBUS) },
251 { PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE3_SMBUS) },
252 { PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE3S_SMBUS) },
253 { PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE4_SMBUS) },
Jean Delvare7c72ccf2005-12-18 17:25:18 +0100254 { PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP04_SMBUS) },
Jean Delvare5c7ae652006-04-25 14:18:16 +0200255 { PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP51_SMBUS) },
256 { PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP55_SMBUS) },
Jean Delvaref75803d2007-05-01 23:26:29 +0200257 { PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP61_SMBUS) },
258 { PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP65_SMBUS) },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 { 0 }
260};
261
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262MODULE_DEVICE_TABLE (pci, nforce2_ids);
263
264
Jean Delvare5c7ae652006-04-25 14:18:16 +0200265static int __devinit nforce2_probe_smb (struct pci_dev *dev, int bar,
266 int alt_reg, struct nforce2_smbus *smbus, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 int error;
269
Jean Delvare5c7ae652006-04-25 14:18:16 +0200270 smbus->base = pci_resource_start(dev, bar);
271 if (smbus->base) {
272 smbus->size = pci_resource_len(dev, bar);
273 } else {
274 /* Older incarnations of the device used non-standard BARs */
275 u16 iobase;
276
277 if (pci_read_config_word(dev, alt_reg, &iobase)
278 != PCIBIOS_SUCCESSFUL) {
279 dev_err(&dev->dev, "Error reading PCI config for %s\n",
280 name);
281 return -1;
282 }
283
284 smbus->base = iobase & PCI_BASE_ADDRESS_IO_MASK;
Hans-Frieder Vogtad04d5c2006-12-10 21:21:29 +0100285 smbus->size = 64;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
Jean Delvared6072f82005-09-25 16:37:04 +0200288 if (!request_region(smbus->base, smbus->size, nforce2_driver.name)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 dev_err(&smbus->adapter.dev, "Error requesting region %02x .. %02X for %s\n",
290 smbus->base, smbus->base+smbus->size-1, name);
291 return -1;
292 }
Hans-Frieder Vogtad04d5c2006-12-10 21:21:29 +0100293 smbus->adapter.owner = THIS_MODULE;
Stephen Hemminger9ace5552007-02-13 22:09:01 +0100294 smbus->adapter.id = I2C_HW_SMBUS_NFORCE2;
Hans-Frieder Vogtad04d5c2006-12-10 21:21:29 +0100295 smbus->adapter.class = I2C_CLASS_HWMON;
296 smbus->adapter.algo = &smbus_algorithm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 smbus->adapter.algo_data = smbus;
298 smbus->adapter.dev.parent = &dev->dev;
David Brownell2096b952007-05-01 23:26:28 +0200299 snprintf(smbus->adapter.name, sizeof(smbus->adapter.name),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 "SMBus nForce2 adapter at %04x", smbus->base);
301
302 error = i2c_add_adapter(&smbus->adapter);
303 if (error) {
304 dev_err(&smbus->adapter.dev, "Failed to register adapter.\n");
305 release_region(smbus->base, smbus->size);
306 return -1;
307 }
308 dev_info(&smbus->adapter.dev, "nForce2 SMBus adapter at %#x\n", smbus->base);
309 return 0;
310}
311
312
313static int __devinit nforce2_probe(struct pci_dev *dev, const struct pci_device_id *id)
314{
315 struct nforce2_smbus *smbuses;
316 int res1, res2;
317
318 /* we support 2 SMBus adapters */
Jean Delvare078d9fe2005-10-17 23:12:36 +0200319 if (!(smbuses = kzalloc(2*sizeof(struct nforce2_smbus), GFP_KERNEL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 pci_set_drvdata(dev, smbuses);
322
Oleg Ryjkovb53c8222007-07-12 14:12:29 +0200323 switch(dev->device) {
324 case PCI_DEVICE_ID_NVIDIA_NFORCE_MCP51_SMBUS:
325 case PCI_DEVICE_ID_NVIDIA_NFORCE_MCP55_SMBUS:
326 smbuses[0].blockops = 1;
327 smbuses[1].blockops = 1;
328 }
329
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 /* SMBus adapter 1 */
Jean Delvare5c7ae652006-04-25 14:18:16 +0200331 res1 = nforce2_probe_smb(dev, 4, NFORCE_PCI_SMB1, &smbuses[0], "SMB1");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 if (res1 < 0) {
333 dev_err(&dev->dev, "Error probing SMB1.\n");
334 smbuses[0].base = 0; /* to have a check value */
335 }
Jean Delvare5c7ae652006-04-25 14:18:16 +0200336 /* SMBus adapter 2 */
337 res2 = nforce2_probe_smb(dev, 5, NFORCE_PCI_SMB2, &smbuses[1], "SMB2");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 if (res2 < 0) {
339 dev_err(&dev->dev, "Error probing SMB2.\n");
340 smbuses[1].base = 0; /* to have a check value */
341 }
342 if ((res1 < 0) && (res2 < 0)) {
343 /* we did not find even one of the SMBuses, so we give up */
344 kfree(smbuses);
345 return -ENODEV;
346 }
347
348 return 0;
349}
350
351
352static void __devexit nforce2_remove(struct pci_dev *dev)
353{
354 struct nforce2_smbus *smbuses = (void*) pci_get_drvdata(dev);
355
356 if (smbuses[0].base) {
357 i2c_del_adapter(&smbuses[0].adapter);
358 release_region(smbuses[0].base, smbuses[0].size);
359 }
360 if (smbuses[1].base) {
361 i2c_del_adapter(&smbuses[1].adapter);
362 release_region(smbuses[1].base, smbuses[1].size);
363 }
364 kfree(smbuses);
365}
366
367static struct pci_driver nforce2_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 .name = "nForce2_smbus",
369 .id_table = nforce2_ids,
370 .probe = nforce2_probe,
371 .remove = __devexit_p(nforce2_remove),
372};
373
374static int __init nforce2_init(void)
375{
376 return pci_register_driver(&nforce2_driver);
377}
378
379static void __exit nforce2_exit(void)
380{
381 pci_unregister_driver(&nforce2_driver);
382}
383
384module_init(nforce2_init);
385module_exit(nforce2_exit);
386