i2c-piix4.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. /*
  2. piix4.c - Part of lm_sensors, Linux kernel modules for hardware
  3. monitoring
  4. Copyright (c) 1998 - 2002 Frodo Looijaard <frodol@dds.nl> and
  5. Philip Edelbrock <phil@netroedge.com>
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. /*
  19. Supports:
  20. Intel PIIX4, 440MX
  21. Serverworks OSB4, CSB5, CSB6
  22. SMSC Victory66
  23. Note: we assume there can only be one device, with one SMBus interface.
  24. */
  25. #include <linux/module.h>
  26. #include <linux/moduleparam.h>
  27. #include <linux/pci.h>
  28. #include <linux/kernel.h>
  29. #include <linux/delay.h>
  30. #include <linux/stddef.h>
  31. #include <linux/sched.h>
  32. #include <linux/ioport.h>
  33. #include <linux/i2c.h>
  34. #include <linux/init.h>
  35. #include <linux/apm_bios.h>
  36. #include <linux/dmi.h>
  37. #include <asm/io.h>
  38. struct sd {
  39. const unsigned short mfr;
  40. const unsigned short dev;
  41. const unsigned char fn;
  42. const char *name;
  43. };
  44. /* PIIX4 SMBus address offsets */
  45. #define SMBHSTSTS (0 + piix4_smba)
  46. #define SMBHSLVSTS (1 + piix4_smba)
  47. #define SMBHSTCNT (2 + piix4_smba)
  48. #define SMBHSTCMD (3 + piix4_smba)
  49. #define SMBHSTADD (4 + piix4_smba)
  50. #define SMBHSTDAT0 (5 + piix4_smba)
  51. #define SMBHSTDAT1 (6 + piix4_smba)
  52. #define SMBBLKDAT (7 + piix4_smba)
  53. #define SMBSLVCNT (8 + piix4_smba)
  54. #define SMBSHDWCMD (9 + piix4_smba)
  55. #define SMBSLVEVT (0xA + piix4_smba)
  56. #define SMBSLVDAT (0xC + piix4_smba)
  57. /* count for request_region */
  58. #define SMBIOSIZE 8
  59. /* PCI Address Constants */
  60. #define SMBBA 0x090
  61. #define SMBHSTCFG 0x0D2
  62. #define SMBSLVC 0x0D3
  63. #define SMBSHDW1 0x0D4
  64. #define SMBSHDW2 0x0D5
  65. #define SMBREV 0x0D6
  66. /* Other settings */
  67. #define MAX_TIMEOUT 500
  68. #define ENABLE_INT9 0
  69. /* PIIX4 constants */
  70. #define PIIX4_QUICK 0x00
  71. #define PIIX4_BYTE 0x04
  72. #define PIIX4_BYTE_DATA 0x08
  73. #define PIIX4_WORD_DATA 0x0C
  74. #define PIIX4_BLOCK_DATA 0x14
  75. /* insmod parameters */
  76. /* If force is set to anything different from 0, we forcibly enable the
  77. PIIX4. DANGEROUS! */
  78. static int force = 0;
  79. module_param (force, int, 0);
  80. MODULE_PARM_DESC(force, "Forcibly enable the PIIX4. DANGEROUS!");
  81. /* If force_addr is set to anything different from 0, we forcibly enable
  82. the PIIX4 at the given address. VERY DANGEROUS! */
  83. static int force_addr = 0;
  84. module_param (force_addr, int, 0);
  85. MODULE_PARM_DESC(force_addr,
  86. "Forcibly enable the PIIX4 at the given address. "
  87. "EXTREMELY DANGEROUS!");
  88. /* If fix_hstcfg is set to anything different from 0, we reset one of the
  89. registers to be a valid value. */
  90. static int fix_hstcfg = 0;
  91. module_param (fix_hstcfg, int, 0);
  92. MODULE_PARM_DESC(fix_hstcfg,
  93. "Fix config register. Needed on some boards (Force CPCI735).");
  94. static int piix4_transaction(void);
  95. static unsigned short piix4_smba = 0;
  96. static struct i2c_adapter piix4_adapter;
  97. static struct dmi_system_id __devinitdata piix4_dmi_table[] = {
  98. {
  99. .ident = "IBM",
  100. .matches = { DMI_MATCH(DMI_SYS_VENDOR, "IBM"), },
  101. },
  102. { },
  103. };
  104. static int __devinit piix4_setup(struct pci_dev *PIIX4_dev,
  105. const struct pci_device_id *id)
  106. {
  107. unsigned char temp;
  108. /* match up the function */
  109. if (PCI_FUNC(PIIX4_dev->devfn) != id->driver_data)
  110. return -ENODEV;
  111. dev_info(&PIIX4_dev->dev, "Found %s device\n", pci_name(PIIX4_dev));
  112. /* Don't access SMBus on IBM systems which get corrupted eeproms */
  113. if (dmi_check_system(piix4_dmi_table) &&
  114. PIIX4_dev->vendor == PCI_VENDOR_ID_INTEL) {
  115. dev_err(&PIIX4_dev->dev, "IBM Laptop detected; this module "
  116. "may corrupt your serial eeprom! Refusing to load "
  117. "module!\n");
  118. return -EPERM;
  119. }
  120. /* Determine the address of the SMBus areas */
  121. if (force_addr) {
  122. piix4_smba = force_addr & 0xfff0;
  123. force = 0;
  124. } else {
  125. pci_read_config_word(PIIX4_dev, SMBBA, &piix4_smba);
  126. piix4_smba &= 0xfff0;
  127. if(piix4_smba == 0) {
  128. dev_err(&PIIX4_dev->dev, "SMB base address "
  129. "uninitialized - upgrade BIOS or use "
  130. "force_addr=0xaddr\n");
  131. return -ENODEV;
  132. }
  133. }
  134. if (!request_region(piix4_smba, SMBIOSIZE, "piix4-smbus")) {
  135. dev_err(&PIIX4_dev->dev, "SMB region 0x%x already in use!\n",
  136. piix4_smba);
  137. return -ENODEV;
  138. }
  139. pci_read_config_byte(PIIX4_dev, SMBHSTCFG, &temp);
  140. /* Some BIOS will set up the chipset incorrectly and leave a register
  141. in an undefined state (causing I2C to act very strangely). */
  142. if (temp & 0x02) {
  143. if (fix_hstcfg) {
  144. dev_info(&PIIX4_dev->dev, "Working around buggy BIOS "
  145. "(I2C)\n");
  146. temp &= 0xfd;
  147. pci_write_config_byte(PIIX4_dev, SMBHSTCFG, temp);
  148. } else {
  149. dev_info(&PIIX4_dev->dev, "Unusual config register "
  150. "value\n");
  151. dev_info(&PIIX4_dev->dev, "Try using fix_hstcfg=1 if "
  152. "you experience problems\n");
  153. }
  154. }
  155. /* If force_addr is set, we program the new address here. Just to make
  156. sure, we disable the PIIX4 first. */
  157. if (force_addr) {
  158. pci_write_config_byte(PIIX4_dev, SMBHSTCFG, temp & 0xfe);
  159. pci_write_config_word(PIIX4_dev, SMBBA, piix4_smba);
  160. pci_write_config_byte(PIIX4_dev, SMBHSTCFG, temp | 0x01);
  161. dev_info(&PIIX4_dev->dev, "WARNING: SMBus interface set to "
  162. "new address %04x!\n", piix4_smba);
  163. } else if ((temp & 1) == 0) {
  164. if (force) {
  165. /* This should never need to be done, but has been
  166. * noted that many Dell machines have the SMBus
  167. * interface on the PIIX4 disabled!? NOTE: This assumes
  168. * I/O space and other allocations WERE done by the
  169. * Bios! Don't complain if your hardware does weird
  170. * things after enabling this. :') Check for Bios
  171. * updates before resorting to this.
  172. */
  173. pci_write_config_byte(PIIX4_dev, SMBHSTCFG,
  174. temp | 1);
  175. dev_printk(KERN_NOTICE, &PIIX4_dev->dev,
  176. "WARNING: SMBus interface has been "
  177. "FORCEFULLY ENABLED!\n");
  178. } else {
  179. dev_err(&PIIX4_dev->dev,
  180. "Host SMBus controller not enabled!\n");
  181. release_region(piix4_smba, SMBIOSIZE);
  182. piix4_smba = 0;
  183. return -ENODEV;
  184. }
  185. }
  186. if ((temp & 0x0E) == 8)
  187. dev_dbg(&PIIX4_dev->dev, "Using Interrupt 9 for SMBus.\n");
  188. else if ((temp & 0x0E) == 0)
  189. dev_dbg(&PIIX4_dev->dev, "Using Interrupt SMI# for SMBus.\n");
  190. else
  191. dev_err(&PIIX4_dev->dev, "Illegal Interrupt configuration "
  192. "(or code out of date)!\n");
  193. pci_read_config_byte(PIIX4_dev, SMBREV, &temp);
  194. dev_dbg(&PIIX4_dev->dev, "SMBREV = 0x%X\n", temp);
  195. dev_dbg(&PIIX4_dev->dev, "SMBA = 0x%X\n", piix4_smba);
  196. return 0;
  197. }
  198. /* Another internally used function */
  199. static int piix4_transaction(void)
  200. {
  201. int temp;
  202. int result = 0;
  203. int timeout = 0;
  204. dev_dbg(&piix4_adapter.dev, "Transaction (pre): CNT=%02x, CMD=%02x, "
  205. "ADD=%02x, DAT0=%02x, DAT1=%02x\n", inb_p(SMBHSTCNT),
  206. inb_p(SMBHSTCMD), inb_p(SMBHSTADD), inb_p(SMBHSTDAT0),
  207. inb_p(SMBHSTDAT1));
  208. /* Make sure the SMBus host is ready to start transmitting */
  209. if ((temp = inb_p(SMBHSTSTS)) != 0x00) {
  210. dev_dbg(&piix4_adapter.dev, "SMBus busy (%02x). "
  211. "Resetting... \n", temp);
  212. outb_p(temp, SMBHSTSTS);
  213. if ((temp = inb_p(SMBHSTSTS)) != 0x00) {
  214. dev_err(&piix4_adapter.dev, "Failed! (%02x)\n", temp);
  215. return -1;
  216. } else {
  217. dev_dbg(&piix4_adapter.dev, "Successfull!\n");
  218. }
  219. }
  220. /* start the transaction by setting bit 6 */
  221. outb_p(inb(SMBHSTCNT) | 0x040, SMBHSTCNT);
  222. /* We will always wait for a fraction of a second! (See PIIX4 docs errata) */
  223. do {
  224. msleep(1);
  225. temp = inb_p(SMBHSTSTS);
  226. } while ((temp & 0x01) && (timeout++ < MAX_TIMEOUT));
  227. /* If the SMBus is still busy, we give up */
  228. if (timeout >= MAX_TIMEOUT) {
  229. dev_err(&piix4_adapter.dev, "SMBus Timeout!\n");
  230. result = -1;
  231. }
  232. if (temp & 0x10) {
  233. result = -1;
  234. dev_err(&piix4_adapter.dev, "Error: Failed bus transaction\n");
  235. }
  236. if (temp & 0x08) {
  237. result = -1;
  238. dev_dbg(&piix4_adapter.dev, "Bus collision! SMBus may be "
  239. "locked until next hard reset. (sorry!)\n");
  240. /* Clock stops and slave is stuck in mid-transmission */
  241. }
  242. if (temp & 0x04) {
  243. result = -1;
  244. dev_dbg(&piix4_adapter.dev, "Error: no response!\n");
  245. }
  246. if (inb_p(SMBHSTSTS) != 0x00)
  247. outb_p(inb(SMBHSTSTS), SMBHSTSTS);
  248. if ((temp = inb_p(SMBHSTSTS)) != 0x00) {
  249. dev_err(&piix4_adapter.dev, "Failed reset at end of "
  250. "transaction (%02x)\n", temp);
  251. }
  252. dev_dbg(&piix4_adapter.dev, "Transaction (post): CNT=%02x, CMD=%02x, "
  253. "ADD=%02x, DAT0=%02x, DAT1=%02x\n", inb_p(SMBHSTCNT),
  254. inb_p(SMBHSTCMD), inb_p(SMBHSTADD), inb_p(SMBHSTDAT0),
  255. inb_p(SMBHSTDAT1));
  256. return result;
  257. }
  258. /* Return -1 on error. */
  259. static s32 piix4_access(struct i2c_adapter * adap, u16 addr,
  260. unsigned short flags, char read_write,
  261. u8 command, int size, union i2c_smbus_data * data)
  262. {
  263. int i, len;
  264. switch (size) {
  265. case I2C_SMBUS_PROC_CALL:
  266. dev_err(&adap->dev, "I2C_SMBUS_PROC_CALL not supported!\n");
  267. return -1;
  268. case I2C_SMBUS_QUICK:
  269. outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
  270. SMBHSTADD);
  271. size = PIIX4_QUICK;
  272. break;
  273. case I2C_SMBUS_BYTE:
  274. outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
  275. SMBHSTADD);
  276. if (read_write == I2C_SMBUS_WRITE)
  277. outb_p(command, SMBHSTCMD);
  278. size = PIIX4_BYTE;
  279. break;
  280. case I2C_SMBUS_BYTE_DATA:
  281. outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
  282. SMBHSTADD);
  283. outb_p(command, SMBHSTCMD);
  284. if (read_write == I2C_SMBUS_WRITE)
  285. outb_p(data->byte, SMBHSTDAT0);
  286. size = PIIX4_BYTE_DATA;
  287. break;
  288. case I2C_SMBUS_WORD_DATA:
  289. outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
  290. SMBHSTADD);
  291. outb_p(command, SMBHSTCMD);
  292. if (read_write == I2C_SMBUS_WRITE) {
  293. outb_p(data->word & 0xff, SMBHSTDAT0);
  294. outb_p((data->word & 0xff00) >> 8, SMBHSTDAT1);
  295. }
  296. size = PIIX4_WORD_DATA;
  297. break;
  298. case I2C_SMBUS_BLOCK_DATA:
  299. outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
  300. SMBHSTADD);
  301. outb_p(command, SMBHSTCMD);
  302. if (read_write == I2C_SMBUS_WRITE) {
  303. len = data->block[0];
  304. if (len < 0)
  305. len = 0;
  306. if (len > 32)
  307. len = 32;
  308. outb_p(len, SMBHSTDAT0);
  309. i = inb_p(SMBHSTCNT); /* Reset SMBBLKDAT */
  310. for (i = 1; i <= len; i++)
  311. outb_p(data->block[i], SMBBLKDAT);
  312. }
  313. size = PIIX4_BLOCK_DATA;
  314. break;
  315. }
  316. outb_p((size & 0x1C) + (ENABLE_INT9 & 1), SMBHSTCNT);
  317. if (piix4_transaction()) /* Error in transaction */
  318. return -1;
  319. if ((read_write == I2C_SMBUS_WRITE) || (size == PIIX4_QUICK))
  320. return 0;
  321. switch (size) {
  322. case PIIX4_BYTE: /* Where is the result put? I assume here it is in
  323. SMBHSTDAT0 but it might just as well be in the
  324. SMBHSTCMD. No clue in the docs */
  325. data->byte = inb_p(SMBHSTDAT0);
  326. break;
  327. case PIIX4_BYTE_DATA:
  328. data->byte = inb_p(SMBHSTDAT0);
  329. break;
  330. case PIIX4_WORD_DATA:
  331. data->word = inb_p(SMBHSTDAT0) + (inb_p(SMBHSTDAT1) << 8);
  332. break;
  333. case PIIX4_BLOCK_DATA:
  334. data->block[0] = inb_p(SMBHSTDAT0);
  335. i = inb_p(SMBHSTCNT); /* Reset SMBBLKDAT */
  336. for (i = 1; i <= data->block[0]; i++)
  337. data->block[i] = inb_p(SMBBLKDAT);
  338. break;
  339. }
  340. return 0;
  341. }
  342. static u32 piix4_func(struct i2c_adapter *adapter)
  343. {
  344. return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
  345. I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
  346. I2C_FUNC_SMBUS_BLOCK_DATA;
  347. }
  348. static struct i2c_algorithm smbus_algorithm = {
  349. .name = "Non-I2C SMBus adapter",
  350. .id = I2C_ALGO_SMBUS,
  351. .smbus_xfer = piix4_access,
  352. .functionality = piix4_func,
  353. };
  354. static struct i2c_adapter piix4_adapter = {
  355. .owner = THIS_MODULE,
  356. .class = I2C_CLASS_HWMON,
  357. .algo = &smbus_algorithm,
  358. .name = "unset",
  359. };
  360. static struct pci_device_id piix4_ids[] = {
  361. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82371AB_3),
  362. .driver_data = 3 },
  363. { PCI_DEVICE(PCI_VENDOR_ID_SERVERWORKS, PCI_DEVICE_ID_SERVERWORKS_OSB4),
  364. .driver_data = 0 },
  365. { PCI_DEVICE(PCI_VENDOR_ID_SERVERWORKS, PCI_DEVICE_ID_SERVERWORKS_CSB5),
  366. .driver_data = 0 },
  367. { PCI_DEVICE(PCI_VENDOR_ID_SERVERWORKS, PCI_DEVICE_ID_SERVERWORKS_CSB6),
  368. .driver_data = 0 },
  369. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82443MX_3),
  370. .driver_data = 3 },
  371. { PCI_DEVICE(PCI_VENDOR_ID_EFAR, PCI_DEVICE_ID_EFAR_SLC90E66_3),
  372. .driver_data = 0 },
  373. { 0, }
  374. };
  375. MODULE_DEVICE_TABLE (pci, piix4_ids);
  376. static int __devinit piix4_probe(struct pci_dev *dev,
  377. const struct pci_device_id *id)
  378. {
  379. int retval;
  380. retval = piix4_setup(dev, id);
  381. if (retval)
  382. return retval;
  383. /* set up the driverfs linkage to our parent device */
  384. piix4_adapter.dev.parent = &dev->dev;
  385. snprintf(piix4_adapter.name, I2C_NAME_SIZE,
  386. "SMBus PIIX4 adapter at %04x", piix4_smba);
  387. if ((retval = i2c_add_adapter(&piix4_adapter))) {
  388. dev_err(&dev->dev, "Couldn't register adapter!\n");
  389. release_region(piix4_smba, SMBIOSIZE);
  390. piix4_smba = 0;
  391. }
  392. return retval;
  393. }
  394. static void __devexit piix4_remove(struct pci_dev *dev)
  395. {
  396. if (piix4_smba) {
  397. i2c_del_adapter(&piix4_adapter);
  398. release_region(piix4_smba, SMBIOSIZE);
  399. piix4_smba = 0;
  400. }
  401. }
  402. static struct pci_driver piix4_driver = {
  403. .name = "piix4_smbus",
  404. .id_table = piix4_ids,
  405. .probe = piix4_probe,
  406. .remove = __devexit_p(piix4_remove),
  407. };
  408. static int __init i2c_piix4_init(void)
  409. {
  410. return pci_register_driver(&piix4_driver);
  411. }
  412. static void __exit i2c_piix4_exit(void)
  413. {
  414. pci_unregister_driver(&piix4_driver);
  415. }
  416. MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl> and "
  417. "Philip Edelbrock <phil@netroedge.com>");
  418. MODULE_DESCRIPTION("PIIX4 SMBus driver");
  419. MODULE_LICENSE("GPL");
  420. module_init(i2c_piix4_init);
  421. module_exit(i2c_piix4_exit);