i2c-viapro.c 13 KB

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