i2c-piix4.c 14 KB

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