i2c-piix4.c 13 KB

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