i2c-viapro.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. /*
  2. i2c-viapro.c - Part of lm_sensors, Linux kernel modules for hardware
  3. monitoring
  4. Copyright (c) 1998 - 2002 Frodo Looijaard <frodol@dds.nl>,
  5. Philip Edelbrock <phil@netroedge.com>, Kyösti Mälkki <kmalkki@cc.hut.fi>,
  6. Mark D. Studebaker <mdsxyz123@yahoo.com>
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. */
  19. /*
  20. Supports Via devices:
  21. 82C596A/B (0x3050)
  22. 82C596B (0x3051)
  23. 82C686A/B
  24. 8231
  25. 8233
  26. 8233A (0x3147 and 0x3177)
  27. 8235
  28. 8237
  29. Note: we assume there can only be one device, with one SMBus interface.
  30. */
  31. #include <linux/config.h>
  32. #include <linux/module.h>
  33. #include <linux/delay.h>
  34. #include <linux/pci.h>
  35. #include <linux/kernel.h>
  36. #include <linux/stddef.h>
  37. #include <linux/sched.h>
  38. #include <linux/ioport.h>
  39. #include <linux/i2c.h>
  40. #include <linux/init.h>
  41. #include <asm/io.h>
  42. static struct pci_dev *vt596_pdev;
  43. #define SMBBA1 0x90
  44. #define SMBBA2 0x80
  45. #define SMBBA3 0xD0
  46. /* SMBus address offsets */
  47. static unsigned short vt596_smba;
  48. #define SMBHSTSTS (vt596_smba + 0)
  49. #define SMBHSLVSTS (vt596_smba + 1)
  50. #define SMBHSTCNT (vt596_smba + 2)
  51. #define SMBHSTCMD (vt596_smba + 3)
  52. #define SMBHSTADD (vt596_smba + 4)
  53. #define SMBHSTDAT0 (vt596_smba + 5)
  54. #define SMBHSTDAT1 (vt596_smba + 6)
  55. #define SMBBLKDAT (vt596_smba + 7)
  56. #define SMBSLVCNT (vt596_smba + 8)
  57. #define SMBSHDWCMD (vt596_smba + 9)
  58. #define SMBSLVEVT (vt596_smba + 0xA)
  59. #define SMBSLVDAT (vt596_smba + 0xC)
  60. /* PCI Address Constants */
  61. /* SMBus data in configuration space can be found in two places,
  62. We try to select the better one*/
  63. static unsigned short smb_cf_hstcfg = 0xD2;
  64. #define SMBHSTCFG (smb_cf_hstcfg)
  65. #define SMBSLVC (smb_cf_hstcfg + 1)
  66. #define SMBSHDW1 (smb_cf_hstcfg + 2)
  67. #define SMBSHDW2 (smb_cf_hstcfg + 3)
  68. #define SMBREV (smb_cf_hstcfg + 4)
  69. /* Other settings */
  70. #define MAX_TIMEOUT 500
  71. #define ENABLE_INT9 0
  72. /* VT82C596 constants */
  73. #define VT596_QUICK 0x00
  74. #define VT596_BYTE 0x04
  75. #define VT596_BYTE_DATA 0x08
  76. #define VT596_WORD_DATA 0x0C
  77. #define VT596_BLOCK_DATA 0x14
  78. /* If force is set to anything different from 0, we forcibly enable the
  79. VT596. DANGEROUS! */
  80. static int force;
  81. module_param(force, bool, 0);
  82. MODULE_PARM_DESC(force, "Forcibly enable the SMBus. DANGEROUS!");
  83. /* If force_addr is set to anything different from 0, we forcibly enable
  84. the VT596 at the given address. VERY DANGEROUS! */
  85. static u16 force_addr;
  86. module_param(force_addr, ushort, 0);
  87. MODULE_PARM_DESC(force_addr,
  88. "Forcibly enable the SMBus at the given address. "
  89. "EXTREMELY DANGEROUS!");
  90. static struct i2c_adapter vt596_adapter;
  91. /* Another internally used function */
  92. static int vt596_transaction(void)
  93. {
  94. int temp;
  95. int result = 0;
  96. int timeout = 0;
  97. dev_dbg(&vt596_adapter.dev, "Transaction (pre): CNT=%02x, CMD=%02x, "
  98. "ADD=%02x, DAT0=%02x, DAT1=%02x\n", inb_p(SMBHSTCNT),
  99. inb_p(SMBHSTCMD), inb_p(SMBHSTADD), inb_p(SMBHSTDAT0),
  100. inb_p(SMBHSTDAT1));
  101. /* Make sure the SMBus host is ready to start transmitting */
  102. if ((temp = inb_p(SMBHSTSTS)) & 0x1F) {
  103. dev_dbg(&vt596_adapter.dev, "SMBus busy (0x%02x). "
  104. "Resetting...\n", temp);
  105. outb_p(temp, SMBHSTSTS);
  106. if ((temp = inb_p(SMBHSTSTS)) & 0x1F) {
  107. dev_dbg(&vt596_adapter.dev, "Failed! (0x%02x)\n", temp);
  108. return -1;
  109. } else {
  110. dev_dbg(&vt596_adapter.dev, "Successfull!\n");
  111. }
  112. }
  113. /* start the transaction by setting bit 6 */
  114. outb_p(inb(SMBHSTCNT) | 0x040, SMBHSTCNT);
  115. /* We will always wait for a fraction of a second!
  116. I don't know if VIA needs this, Intel did */
  117. do {
  118. msleep(1);
  119. temp = inb_p(SMBHSTSTS);
  120. } while ((temp & 0x01) && (timeout++ < MAX_TIMEOUT));
  121. /* If the SMBus is still busy, we give up */
  122. if (timeout >= MAX_TIMEOUT) {
  123. result = -1;
  124. dev_dbg(&vt596_adapter.dev, "SMBus Timeout!\n");
  125. }
  126. if (temp & 0x10) {
  127. result = -1;
  128. dev_dbg(&vt596_adapter.dev, "Error: Failed bus transaction\n");
  129. }
  130. if (temp & 0x08) {
  131. result = -1;
  132. dev_info(&vt596_adapter.dev, "Bus collision! SMBus may be "
  133. "locked until next hard\nreset. (sorry!)\n");
  134. /* Clock stops and slave is stuck in mid-transmission */
  135. }
  136. if (temp & 0x04) {
  137. result = -1;
  138. dev_dbg(&vt596_adapter.dev, "Error: no response!\n");
  139. }
  140. if ((temp = inb_p(SMBHSTSTS)) & 0x1F) {
  141. outb_p(temp, SMBHSTSTS);
  142. if ((temp = inb_p(SMBHSTSTS)) & 0x1F) {
  143. dev_warn(&vt596_adapter.dev, "Failed reset at end "
  144. "of transaction (%02x)\n", temp);
  145. }
  146. }
  147. dev_dbg(&vt596_adapter.dev, "Transaction (post): CNT=%02x, CMD=%02x, "
  148. "ADD=%02x, DAT0=%02x, DAT1=%02x\n", inb_p(SMBHSTCNT),
  149. inb_p(SMBHSTCMD), inb_p(SMBHSTADD), inb_p(SMBHSTDAT0),
  150. inb_p(SMBHSTDAT1));
  151. return result;
  152. }
  153. /* Return -1 on error. */
  154. static s32 vt596_access(struct i2c_adapter *adap, u16 addr,
  155. unsigned short flags, char read_write, u8 command,
  156. int size, union i2c_smbus_data *data)
  157. {
  158. int i, len;
  159. switch (size) {
  160. case I2C_SMBUS_PROC_CALL:
  161. dev_info(&vt596_adapter.dev,
  162. "I2C_SMBUS_PROC_CALL not supported!\n");
  163. return -1;
  164. case I2C_SMBUS_QUICK:
  165. outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
  166. SMBHSTADD);
  167. size = VT596_QUICK;
  168. break;
  169. case I2C_SMBUS_BYTE:
  170. outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
  171. SMBHSTADD);
  172. if (read_write == I2C_SMBUS_WRITE)
  173. outb_p(command, SMBHSTCMD);
  174. size = VT596_BYTE;
  175. break;
  176. case I2C_SMBUS_BYTE_DATA:
  177. outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
  178. SMBHSTADD);
  179. outb_p(command, SMBHSTCMD);
  180. if (read_write == I2C_SMBUS_WRITE)
  181. outb_p(data->byte, SMBHSTDAT0);
  182. size = VT596_BYTE_DATA;
  183. break;
  184. case I2C_SMBUS_WORD_DATA:
  185. outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
  186. SMBHSTADD);
  187. outb_p(command, SMBHSTCMD);
  188. if (read_write == I2C_SMBUS_WRITE) {
  189. outb_p(data->word & 0xff, SMBHSTDAT0);
  190. outb_p((data->word & 0xff00) >> 8, SMBHSTDAT1);
  191. }
  192. size = VT596_WORD_DATA;
  193. break;
  194. case I2C_SMBUS_BLOCK_DATA:
  195. outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
  196. SMBHSTADD);
  197. outb_p(command, SMBHSTCMD);
  198. if (read_write == I2C_SMBUS_WRITE) {
  199. len = data->block[0];
  200. if (len < 0)
  201. len = 0;
  202. if (len > I2C_SMBUS_BLOCK_MAX)
  203. len = I2C_SMBUS_BLOCK_MAX;
  204. outb_p(len, SMBHSTDAT0);
  205. i = inb_p(SMBHSTCNT); /* Reset SMBBLKDAT */
  206. for (i = 1; i <= len; i++)
  207. outb_p(data->block[i], SMBBLKDAT);
  208. }
  209. size = VT596_BLOCK_DATA;
  210. break;
  211. }
  212. outb_p((size & 0x1C) + (ENABLE_INT9 & 1), SMBHSTCNT);
  213. if (vt596_transaction()) /* Error in transaction */
  214. return -1;
  215. if ((read_write == I2C_SMBUS_WRITE) || (size == VT596_QUICK))
  216. return 0;
  217. switch (size) {
  218. case VT596_BYTE:
  219. /* Where is the result put? I assume here it is in
  220. * SMBHSTDAT0 but it might just as well be in the
  221. * SMBHSTCMD. No clue in the docs
  222. */
  223. data->byte = inb_p(SMBHSTDAT0);
  224. break;
  225. case VT596_BYTE_DATA:
  226. data->byte = inb_p(SMBHSTDAT0);
  227. break;
  228. case VT596_WORD_DATA:
  229. data->word = inb_p(SMBHSTDAT0) + (inb_p(SMBHSTDAT1) << 8);
  230. break;
  231. case VT596_BLOCK_DATA:
  232. data->block[0] = inb_p(SMBHSTDAT0);
  233. if (data->block[0] > I2C_SMBUS_BLOCK_MAX)
  234. data->block[0] = I2C_SMBUS_BLOCK_MAX;
  235. i = inb_p(SMBHSTCNT); /* Reset SMBBLKDAT */
  236. for (i = 1; i <= data->block[0]; i++)
  237. data->block[i] = inb_p(SMBBLKDAT);
  238. break;
  239. }
  240. return 0;
  241. }
  242. static u32 vt596_func(struct i2c_adapter *adapter)
  243. {
  244. return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
  245. I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
  246. I2C_FUNC_SMBUS_BLOCK_DATA;
  247. }
  248. static struct i2c_algorithm smbus_algorithm = {
  249. .name = "Non-I2C SMBus adapter",
  250. .id = I2C_ALGO_SMBUS,
  251. .smbus_xfer = vt596_access,
  252. .functionality = vt596_func,
  253. };
  254. static struct i2c_adapter vt596_adapter = {
  255. .owner = THIS_MODULE,
  256. .class = I2C_CLASS_HWMON,
  257. .algo = &smbus_algorithm,
  258. .name = "unset",
  259. };
  260. static int __devinit vt596_probe(struct pci_dev *pdev,
  261. const struct pci_device_id *id)
  262. {
  263. unsigned char temp;
  264. int error = -ENODEV;
  265. /* Determine the address of the SMBus areas */
  266. if (force_addr) {
  267. vt596_smba = force_addr & 0xfff0;
  268. force = 0;
  269. goto found;
  270. }
  271. if ((pci_read_config_word(pdev, id->driver_data, &vt596_smba)) ||
  272. !(vt596_smba & 0x1)) {
  273. /* try 2nd address and config reg. for 596 */
  274. if (id->device == PCI_DEVICE_ID_VIA_82C596_3 &&
  275. !pci_read_config_word(pdev, SMBBA2, &vt596_smba) &&
  276. (vt596_smba & 0x1)) {
  277. smb_cf_hstcfg = 0x84;
  278. } else {
  279. /* no matches at all */
  280. dev_err(&pdev->dev, "Cannot configure "
  281. "SMBus I/O Base address\n");
  282. return -ENODEV;
  283. }
  284. }
  285. vt596_smba &= 0xfff0;
  286. if (vt596_smba == 0) {
  287. dev_err(&pdev->dev, "SMBus base address "
  288. "uninitialized - upgrade BIOS or use "
  289. "force_addr=0xaddr\n");
  290. return -ENODEV;
  291. }
  292. found:
  293. if (!request_region(vt596_smba, 8, "viapro-smbus")) {
  294. dev_err(&pdev->dev, "SMBus region 0x%x already in use!\n",
  295. vt596_smba);
  296. return -ENODEV;
  297. }
  298. pci_read_config_byte(pdev, SMBHSTCFG, &temp);
  299. /* If force_addr is set, we program the new address here. Just to make
  300. sure, we disable the VT596 first. */
  301. if (force_addr) {
  302. pci_write_config_byte(pdev, SMBHSTCFG, temp & 0xfe);
  303. pci_write_config_word(pdev, id->driver_data, vt596_smba);
  304. pci_write_config_byte(pdev, SMBHSTCFG, temp | 0x01);
  305. dev_warn(&pdev->dev, "WARNING: SMBus interface set to new "
  306. "address 0x%04x!\n", vt596_smba);
  307. } else if ((temp & 1) == 0) {
  308. if (force) {
  309. /* NOTE: This assumes I/O space and other allocations
  310. * WERE done by the Bios! Don't complain if your
  311. * hardware does weird things after enabling this.
  312. * :') Check for Bios updates before resorting to
  313. * this.
  314. */
  315. pci_write_config_byte(pdev, SMBHSTCFG, temp | 1);
  316. dev_info(&pdev->dev, "Enabling SMBus device\n");
  317. } else {
  318. dev_err(&pdev->dev, "SMBUS: Error: Host SMBus "
  319. "controller not enabled! - upgrade BIOS or "
  320. "use force=1\n");
  321. goto release_region;
  322. }
  323. }
  324. if ((temp & 0x0E) == 8)
  325. dev_dbg(&pdev->dev, "using Interrupt 9 for SMBus.\n");
  326. else if ((temp & 0x0E) == 0)
  327. dev_dbg(&pdev->dev, "using Interrupt SMI# for SMBus.\n");
  328. else
  329. dev_dbg(&pdev->dev, "Illegal Interrupt configuration "
  330. "(or code out of date)!\n");
  331. pci_read_config_byte(pdev, SMBREV, &temp);
  332. dev_dbg(&pdev->dev, "SMBREV = 0x%X\n", temp);
  333. dev_dbg(&pdev->dev, "VT596_smba = 0x%X\n", vt596_smba);
  334. vt596_adapter.dev.parent = &pdev->dev;
  335. snprintf(vt596_adapter.name, I2C_NAME_SIZE,
  336. "SMBus Via Pro adapter at %04x", vt596_smba);
  337. vt596_pdev = pci_dev_get(pdev);
  338. if (i2c_add_adapter(&vt596_adapter)) {
  339. pci_dev_put(vt596_pdev);
  340. vt596_pdev = NULL;
  341. }
  342. /* Always return failure here. This is to allow other drivers to bind
  343. * to this pci device. We don't really want to have control over the
  344. * pci device, we only wanted to read as few register values from it.
  345. */
  346. return -ENODEV;
  347. release_region:
  348. release_region(vt596_smba, 8);
  349. return error;
  350. }
  351. static struct pci_device_id vt596_ids[] = {
  352. { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_82C596_3),
  353. .driver_data = SMBBA1 },
  354. { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_82C596B_3),
  355. .driver_data = SMBBA1 },
  356. { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_82C686_4),
  357. .driver_data = SMBBA1 },
  358. { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8233_0),
  359. .driver_data = SMBBA3 },
  360. { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8233A),
  361. .driver_data = SMBBA3 },
  362. { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8235),
  363. .driver_data = SMBBA3 },
  364. { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8237),
  365. .driver_data = SMBBA3 },
  366. { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8231_4),
  367. .driver_data = SMBBA1 },
  368. { 0, }
  369. };
  370. MODULE_DEVICE_TABLE (pci, vt596_ids);
  371. static struct pci_driver vt596_driver = {
  372. .name = "vt596_smbus",
  373. .id_table = vt596_ids,
  374. .probe = vt596_probe,
  375. };
  376. static int __init i2c_vt596_init(void)
  377. {
  378. return pci_register_driver(&vt596_driver);
  379. }
  380. static void __exit i2c_vt596_exit(void)
  381. {
  382. pci_unregister_driver(&vt596_driver);
  383. if (vt596_pdev != NULL) {
  384. i2c_del_adapter(&vt596_adapter);
  385. release_region(vt596_smba, 8);
  386. pci_dev_put(vt596_pdev);
  387. vt596_pdev = NULL;
  388. }
  389. }
  390. MODULE_AUTHOR(
  391. "Frodo Looijaard <frodol@dds.nl> and "
  392. "Philip Edelbrock <phil@netroedge.com>");
  393. MODULE_DESCRIPTION("vt82c596 SMBus driver");
  394. MODULE_LICENSE("GPL");
  395. module_init(i2c_vt596_init);
  396. module_exit(i2c_vt596_exit);