i2c-viapro.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. /*
  2. Copyright (c) 1998 - 2002 Frodo Looijaard <frodol@dds.nl>,
  3. Philip Edelbrock <phil@netroedge.com>, Kyösti Mälkki <kmalkki@cc.hut.fi>,
  4. Mark D. Studebaker <mdsxyz123@yahoo.com>
  5. Copyright (C) 2005 - 2008 Jean Delvare <jdelvare@suse.de>
  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. */
  15. /*
  16. Supports the following VIA south bridges:
  17. Chip name PCI ID REV I2C block
  18. VT82C596A 0x3050 no
  19. VT82C596B 0x3051 no
  20. VT82C686A 0x3057 0x30 no
  21. VT82C686B 0x3057 0x40 yes
  22. VT8231 0x8235 no?
  23. VT8233 0x3074 yes
  24. VT8233A 0x3147 yes?
  25. VT8235 0x3177 yes
  26. VT8237R 0x3227 yes
  27. VT8237A 0x3337 yes
  28. VT8237S 0x3372 yes
  29. VT8251 0x3287 yes
  30. CX700 0x8324 yes
  31. VX800/VX820 0x8353 yes
  32. VX855/VX875 0x8409 yes
  33. Note: we assume there can only be one device, with one SMBus interface.
  34. */
  35. #include <linux/module.h>
  36. #include <linux/delay.h>
  37. #include <linux/pci.h>
  38. #include <linux/kernel.h>
  39. #include <linux/stddef.h>
  40. #include <linux/ioport.h>
  41. #include <linux/i2c.h>
  42. #include <linux/init.h>
  43. #include <linux/acpi.h>
  44. #include <linux/io.h>
  45. static struct pci_dev *vt596_pdev;
  46. #define SMBBA1 0x90
  47. #define SMBBA2 0x80
  48. #define SMBBA3 0xD0
  49. /* SMBus address offsets */
  50. static unsigned short vt596_smba;
  51. #define SMBHSTSTS (vt596_smba + 0)
  52. #define SMBHSTCNT (vt596_smba + 2)
  53. #define SMBHSTCMD (vt596_smba + 3)
  54. #define SMBHSTADD (vt596_smba + 4)
  55. #define SMBHSTDAT0 (vt596_smba + 5)
  56. #define SMBHSTDAT1 (vt596_smba + 6)
  57. #define SMBBLKDAT (vt596_smba + 7)
  58. /* PCI Address Constants */
  59. /* SMBus data in configuration space can be found in two places,
  60. We try to select the better one */
  61. static unsigned short SMBHSTCFG = 0xD2;
  62. /* Other settings */
  63. #define MAX_TIMEOUT 500
  64. /* VT82C596 constants */
  65. #define VT596_QUICK 0x00
  66. #define VT596_BYTE 0x04
  67. #define VT596_BYTE_DATA 0x08
  68. #define VT596_WORD_DATA 0x0C
  69. #define VT596_PROC_CALL 0x10
  70. #define VT596_BLOCK_DATA 0x14
  71. #define VT596_I2C_BLOCK_DATA 0x34
  72. /* If force is set to anything different from 0, we forcibly enable the
  73. VT596. DANGEROUS! */
  74. static bool force;
  75. module_param(force, bool, 0);
  76. MODULE_PARM_DESC(force, "Forcibly enable the SMBus. DANGEROUS!");
  77. /* If force_addr is set to anything different from 0, we forcibly enable
  78. the VT596 at the given address. VERY DANGEROUS! */
  79. static u16 force_addr;
  80. module_param(force_addr, ushort, 0);
  81. MODULE_PARM_DESC(force_addr,
  82. "Forcibly enable the SMBus at the given address. "
  83. "EXTREMELY DANGEROUS!");
  84. static struct pci_driver vt596_driver;
  85. static struct i2c_adapter vt596_adapter;
  86. #define FEATURE_I2CBLOCK (1<<0)
  87. static unsigned int vt596_features;
  88. #ifdef DEBUG
  89. static void vt596_dump_regs(const char *msg, u8 size)
  90. {
  91. dev_dbg(&vt596_adapter.dev, "%s: STS=%02x CNT=%02x CMD=%02x ADD=%02x "
  92. "DAT=%02x,%02x\n", msg, inb_p(SMBHSTSTS), inb_p(SMBHSTCNT),
  93. inb_p(SMBHSTCMD), inb_p(SMBHSTADD), inb_p(SMBHSTDAT0),
  94. inb_p(SMBHSTDAT1));
  95. if (size == VT596_BLOCK_DATA
  96. || size == VT596_I2C_BLOCK_DATA) {
  97. int i;
  98. dev_dbg(&vt596_adapter.dev, "BLK=");
  99. for (i = 0; i < I2C_SMBUS_BLOCK_MAX / 2; i++)
  100. printk("%02x,", inb_p(SMBBLKDAT));
  101. printk("\n");
  102. dev_dbg(&vt596_adapter.dev, " ");
  103. for (; i < I2C_SMBUS_BLOCK_MAX - 1; i++)
  104. printk("%02x,", inb_p(SMBBLKDAT));
  105. printk("%02x\n", inb_p(SMBBLKDAT));
  106. }
  107. }
  108. #else
  109. static inline void vt596_dump_regs(const char *msg, u8 size) { }
  110. #endif
  111. /* Return -1 on error, 0 on success */
  112. static int vt596_transaction(u8 size)
  113. {
  114. int temp;
  115. int result = 0;
  116. int timeout = 0;
  117. vt596_dump_regs("Transaction (pre)", size);
  118. /* Make sure the SMBus host is ready to start transmitting */
  119. if ((temp = inb_p(SMBHSTSTS)) & 0x1F) {
  120. dev_dbg(&vt596_adapter.dev, "SMBus busy (0x%02x). "
  121. "Resetting...\n", temp);
  122. outb_p(temp, SMBHSTSTS);
  123. if ((temp = inb_p(SMBHSTSTS)) & 0x1F) {
  124. dev_err(&vt596_adapter.dev, "SMBus reset failed! "
  125. "(0x%02x)\n", temp);
  126. return -EBUSY;
  127. }
  128. }
  129. /* Start the transaction by setting bit 6 */
  130. outb_p(0x40 | size, SMBHSTCNT);
  131. /* We will always wait for a fraction of a second */
  132. do {
  133. msleep(1);
  134. temp = inb_p(SMBHSTSTS);
  135. } while ((temp & 0x01) && (++timeout < MAX_TIMEOUT));
  136. /* If the SMBus is still busy, we give up */
  137. if (timeout == MAX_TIMEOUT) {
  138. result = -ETIMEDOUT;
  139. dev_err(&vt596_adapter.dev, "SMBus timeout!\n");
  140. }
  141. if (temp & 0x10) {
  142. result = -EIO;
  143. dev_err(&vt596_adapter.dev, "Transaction failed (0x%02x)\n",
  144. size);
  145. }
  146. if (temp & 0x08) {
  147. result = -EIO;
  148. dev_err(&vt596_adapter.dev, "SMBus collision!\n");
  149. }
  150. if (temp & 0x04) {
  151. result = -ENXIO;
  152. dev_dbg(&vt596_adapter.dev, "No response\n");
  153. }
  154. /* Resetting status register */
  155. if (temp & 0x1F)
  156. outb_p(temp, SMBHSTSTS);
  157. vt596_dump_regs("Transaction (post)", size);
  158. return result;
  159. }
  160. /* Return negative errno on error, 0 on success */
  161. static s32 vt596_access(struct i2c_adapter *adap, u16 addr,
  162. unsigned short flags, char read_write, u8 command,
  163. int size, union i2c_smbus_data *data)
  164. {
  165. int i;
  166. int status;
  167. switch (size) {
  168. case I2C_SMBUS_QUICK:
  169. size = VT596_QUICK;
  170. break;
  171. case I2C_SMBUS_BYTE:
  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(command, SMBHSTCMD);
  178. if (read_write == I2C_SMBUS_WRITE)
  179. outb_p(data->byte, SMBHSTDAT0);
  180. size = VT596_BYTE_DATA;
  181. break;
  182. case I2C_SMBUS_WORD_DATA:
  183. outb_p(command, SMBHSTCMD);
  184. if (read_write == I2C_SMBUS_WRITE) {
  185. outb_p(data->word & 0xff, SMBHSTDAT0);
  186. outb_p((data->word & 0xff00) >> 8, SMBHSTDAT1);
  187. }
  188. size = VT596_WORD_DATA;
  189. break;
  190. case I2C_SMBUS_PROC_CALL:
  191. outb_p(command, SMBHSTCMD);
  192. outb_p(data->word & 0xff, SMBHSTDAT0);
  193. outb_p((data->word & 0xff00) >> 8, SMBHSTDAT1);
  194. size = VT596_PROC_CALL;
  195. break;
  196. case I2C_SMBUS_I2C_BLOCK_DATA:
  197. if (!(vt596_features & FEATURE_I2CBLOCK))
  198. goto exit_unsupported;
  199. if (read_write == I2C_SMBUS_READ)
  200. outb_p(data->block[0], SMBHSTDAT0);
  201. /* Fall through */
  202. case I2C_SMBUS_BLOCK_DATA:
  203. outb_p(command, SMBHSTCMD);
  204. if (read_write == I2C_SMBUS_WRITE) {
  205. u8 len = data->block[0];
  206. if (len > I2C_SMBUS_BLOCK_MAX)
  207. len = I2C_SMBUS_BLOCK_MAX;
  208. outb_p(len, SMBHSTDAT0);
  209. inb_p(SMBHSTCNT); /* Reset SMBBLKDAT */
  210. for (i = 1; i <= len; i++)
  211. outb_p(data->block[i], SMBBLKDAT);
  212. }
  213. size = (size == I2C_SMBUS_I2C_BLOCK_DATA) ?
  214. VT596_I2C_BLOCK_DATA : VT596_BLOCK_DATA;
  215. break;
  216. default:
  217. goto exit_unsupported;
  218. }
  219. outb_p(((addr & 0x7f) << 1) | read_write, SMBHSTADD);
  220. status = vt596_transaction(size);
  221. if (status)
  222. return status;
  223. if (size == VT596_PROC_CALL)
  224. read_write = I2C_SMBUS_READ;
  225. if ((read_write == I2C_SMBUS_WRITE) || (size == VT596_QUICK))
  226. return 0;
  227. switch (size) {
  228. case VT596_BYTE:
  229. case VT596_BYTE_DATA:
  230. data->byte = inb_p(SMBHSTDAT0);
  231. break;
  232. case VT596_WORD_DATA:
  233. case VT596_PROC_CALL:
  234. data->word = inb_p(SMBHSTDAT0) + (inb_p(SMBHSTDAT1) << 8);
  235. break;
  236. case VT596_I2C_BLOCK_DATA:
  237. case VT596_BLOCK_DATA:
  238. data->block[0] = inb_p(SMBHSTDAT0);
  239. if (data->block[0] > I2C_SMBUS_BLOCK_MAX)
  240. data->block[0] = I2C_SMBUS_BLOCK_MAX;
  241. inb_p(SMBHSTCNT); /* Reset SMBBLKDAT */
  242. for (i = 1; i <= data->block[0]; i++)
  243. data->block[i] = inb_p(SMBBLKDAT);
  244. break;
  245. }
  246. return 0;
  247. exit_unsupported:
  248. dev_warn(&vt596_adapter.dev, "Unsupported transaction %d\n",
  249. size);
  250. return -EOPNOTSUPP;
  251. }
  252. static u32 vt596_func(struct i2c_adapter *adapter)
  253. {
  254. u32 func = I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
  255. I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
  256. I2C_SMBUS_PROC_CALL | I2C_FUNC_SMBUS_BLOCK_DATA;
  257. if (vt596_features & FEATURE_I2CBLOCK)
  258. func |= I2C_FUNC_SMBUS_I2C_BLOCK;
  259. return func;
  260. }
  261. static const struct i2c_algorithm smbus_algorithm = {
  262. .smbus_xfer = vt596_access,
  263. .functionality = vt596_func,
  264. };
  265. static struct i2c_adapter vt596_adapter = {
  266. .owner = THIS_MODULE,
  267. .class = I2C_CLASS_HWMON | I2C_CLASS_SPD,
  268. .algo = &smbus_algorithm,
  269. };
  270. static int vt596_probe(struct pci_dev *pdev,
  271. const struct pci_device_id *id)
  272. {
  273. unsigned char temp;
  274. int error;
  275. /* Determine the address of the SMBus areas */
  276. if (force_addr) {
  277. vt596_smba = force_addr & 0xfff0;
  278. force = 0;
  279. goto found;
  280. }
  281. if ((pci_read_config_word(pdev, id->driver_data, &vt596_smba)) ||
  282. !(vt596_smba & 0x0001)) {
  283. /* try 2nd address and config reg. for 596 */
  284. if (id->device == PCI_DEVICE_ID_VIA_82C596_3 &&
  285. !pci_read_config_word(pdev, SMBBA2, &vt596_smba) &&
  286. (vt596_smba & 0x0001)) {
  287. SMBHSTCFG = 0x84;
  288. } else {
  289. /* no matches at all */
  290. dev_err(&pdev->dev, "Cannot configure "
  291. "SMBus I/O Base address\n");
  292. return -ENODEV;
  293. }
  294. }
  295. vt596_smba &= 0xfff0;
  296. if (vt596_smba == 0) {
  297. dev_err(&pdev->dev, "SMBus base address "
  298. "uninitialized - upgrade BIOS or use "
  299. "force_addr=0xaddr\n");
  300. return -ENODEV;
  301. }
  302. found:
  303. error = acpi_check_region(vt596_smba, 8, vt596_driver.name);
  304. if (error)
  305. return -ENODEV;
  306. if (!request_region(vt596_smba, 8, vt596_driver.name)) {
  307. dev_err(&pdev->dev, "SMBus region 0x%x already in use!\n",
  308. vt596_smba);
  309. return -ENODEV;
  310. }
  311. pci_read_config_byte(pdev, SMBHSTCFG, &temp);
  312. /* If force_addr is set, we program the new address here. Just to make
  313. sure, we disable the VT596 first. */
  314. if (force_addr) {
  315. pci_write_config_byte(pdev, SMBHSTCFG, temp & 0xfe);
  316. pci_write_config_word(pdev, id->driver_data, vt596_smba);
  317. pci_write_config_byte(pdev, SMBHSTCFG, temp | 0x01);
  318. dev_warn(&pdev->dev, "WARNING: SMBus interface set to new "
  319. "address 0x%04x!\n", vt596_smba);
  320. } else if (!(temp & 0x01)) {
  321. if (force) {
  322. /* NOTE: This assumes I/O space and other allocations
  323. * WERE done by the Bios! Don't complain if your
  324. * hardware does weird things after enabling this.
  325. * :') Check for Bios updates before resorting to
  326. * this.
  327. */
  328. pci_write_config_byte(pdev, SMBHSTCFG, temp | 0x01);
  329. dev_info(&pdev->dev, "Enabling SMBus device\n");
  330. } else {
  331. dev_err(&pdev->dev, "SMBUS: Error: Host SMBus "
  332. "controller not enabled! - upgrade BIOS or "
  333. "use force=1\n");
  334. error = -ENODEV;
  335. goto release_region;
  336. }
  337. }
  338. dev_dbg(&pdev->dev, "VT596_smba = 0x%X\n", vt596_smba);
  339. switch (pdev->device) {
  340. case PCI_DEVICE_ID_VIA_CX700:
  341. case PCI_DEVICE_ID_VIA_VX800:
  342. case PCI_DEVICE_ID_VIA_VX855:
  343. case PCI_DEVICE_ID_VIA_VX900:
  344. case PCI_DEVICE_ID_VIA_8251:
  345. case PCI_DEVICE_ID_VIA_8237:
  346. case PCI_DEVICE_ID_VIA_8237A:
  347. case PCI_DEVICE_ID_VIA_8237S:
  348. case PCI_DEVICE_ID_VIA_8235:
  349. case PCI_DEVICE_ID_VIA_8233A:
  350. case PCI_DEVICE_ID_VIA_8233_0:
  351. vt596_features |= FEATURE_I2CBLOCK;
  352. break;
  353. case PCI_DEVICE_ID_VIA_82C686_4:
  354. /* The VT82C686B (rev 0x40) does support I2C block
  355. transactions, but the VT82C686A (rev 0x30) doesn't */
  356. if (pdev->revision >= 0x40)
  357. vt596_features |= FEATURE_I2CBLOCK;
  358. break;
  359. }
  360. vt596_adapter.dev.parent = &pdev->dev;
  361. snprintf(vt596_adapter.name, sizeof(vt596_adapter.name),
  362. "SMBus Via Pro adapter at %04x", vt596_smba);
  363. vt596_pdev = pci_dev_get(pdev);
  364. error = i2c_add_adapter(&vt596_adapter);
  365. if (error) {
  366. pci_dev_put(vt596_pdev);
  367. vt596_pdev = NULL;
  368. goto release_region;
  369. }
  370. /* Always return failure here. This is to allow other drivers to bind
  371. * to this pci device. We don't really want to have control over the
  372. * pci device, we only wanted to read as few register values from it.
  373. */
  374. return -ENODEV;
  375. release_region:
  376. release_region(vt596_smba, 8);
  377. return error;
  378. }
  379. static const struct pci_device_id vt596_ids[] = {
  380. { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_82C596_3),
  381. .driver_data = SMBBA1 },
  382. { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_82C596B_3),
  383. .driver_data = SMBBA1 },
  384. { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_82C686_4),
  385. .driver_data = SMBBA1 },
  386. { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8233_0),
  387. .driver_data = SMBBA3 },
  388. { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8233A),
  389. .driver_data = SMBBA3 },
  390. { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8235),
  391. .driver_data = SMBBA3 },
  392. { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8237),
  393. .driver_data = SMBBA3 },
  394. { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8237A),
  395. .driver_data = SMBBA3 },
  396. { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8237S),
  397. .driver_data = SMBBA3 },
  398. { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8231_4),
  399. .driver_data = SMBBA1 },
  400. { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8251),
  401. .driver_data = SMBBA3 },
  402. { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_CX700),
  403. .driver_data = SMBBA3 },
  404. { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_VX800),
  405. .driver_data = SMBBA3 },
  406. { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_VX855),
  407. .driver_data = SMBBA3 },
  408. { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_VX900),
  409. .driver_data = SMBBA3 },
  410. { 0, }
  411. };
  412. MODULE_DEVICE_TABLE(pci, vt596_ids);
  413. static struct pci_driver vt596_driver = {
  414. .name = "vt596_smbus",
  415. .id_table = vt596_ids,
  416. .probe = vt596_probe,
  417. };
  418. static int __init i2c_vt596_init(void)
  419. {
  420. return pci_register_driver(&vt596_driver);
  421. }
  422. static void __exit i2c_vt596_exit(void)
  423. {
  424. pci_unregister_driver(&vt596_driver);
  425. if (vt596_pdev != NULL) {
  426. i2c_del_adapter(&vt596_adapter);
  427. release_region(vt596_smba, 8);
  428. pci_dev_put(vt596_pdev);
  429. vt596_pdev = NULL;
  430. }
  431. }
  432. MODULE_AUTHOR("Kyosti Malkki <kmalkki@cc.hut.fi>, "
  433. "Mark D. Studebaker <mdsxyz123@yahoo.com> and "
  434. "Jean Delvare <jdelvare@suse.de>");
  435. MODULE_DESCRIPTION("vt82c596 SMBus driver");
  436. MODULE_LICENSE("GPL");
  437. module_init(i2c_vt596_init);
  438. module_exit(i2c_vt596_exit);