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