i2c-sis96x.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. /*
  2. sis96x.c - Part of lm_sensors, Linux kernel modules for hardware
  3. monitoring
  4. Copyright (c) 2003 Mark M. Hoffman <mhoffman@lightlink.com>
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. */
  17. /*
  18. This module must be considered BETA unless and until
  19. the chipset manufacturer releases a datasheet.
  20. The register definitions are based on the SiS630.
  21. This module relies on quirk_sis_96x_smbus (drivers/pci/quirks.c)
  22. for just about every machine for which users have reported.
  23. If this module isn't detecting your 96x south bridge, have a
  24. look there.
  25. We assume there can only be one SiS96x with one SMBus interface.
  26. */
  27. #include <linux/module.h>
  28. #include <linux/pci.h>
  29. #include <linux/kernel.h>
  30. #include <linux/delay.h>
  31. #include <linux/stddef.h>
  32. #include <linux/sched.h>
  33. #include <linux/ioport.h>
  34. #include <linux/i2c.h>
  35. #include <linux/init.h>
  36. #include <asm/io.h>
  37. /*
  38. HISTORY:
  39. 2003-05-11 1.0.0 Updated from lm_sensors project for kernel 2.5
  40. (was i2c-sis645.c from lm_sensors 2.7.0)
  41. */
  42. #define SIS96x_VERSION "1.0.0"
  43. /* base address register in PCI config space */
  44. #define SIS96x_BAR 0x04
  45. /* SiS96x SMBus registers */
  46. #define SMB_STS 0x00
  47. #define SMB_EN 0x01
  48. #define SMB_CNT 0x02
  49. #define SMB_HOST_CNT 0x03
  50. #define SMB_ADDR 0x04
  51. #define SMB_CMD 0x05
  52. #define SMB_PCOUNT 0x06
  53. #define SMB_COUNT 0x07
  54. #define SMB_BYTE 0x08
  55. #define SMB_DEV_ADDR 0x10
  56. #define SMB_DB0 0x11
  57. #define SMB_DB1 0x12
  58. #define SMB_SAA 0x13
  59. /* register count for request_region */
  60. #define SMB_IOSIZE 0x20
  61. /* Other settings */
  62. #define MAX_TIMEOUT 500
  63. /* SiS96x SMBus constants */
  64. #define SIS96x_QUICK 0x00
  65. #define SIS96x_BYTE 0x01
  66. #define SIS96x_BYTE_DATA 0x02
  67. #define SIS96x_WORD_DATA 0x03
  68. #define SIS96x_PROC_CALL 0x04
  69. #define SIS96x_BLOCK_DATA 0x05
  70. static struct pci_driver sis96x_driver;
  71. static struct i2c_adapter sis96x_adapter;
  72. static u16 sis96x_smbus_base;
  73. static inline u8 sis96x_read(u8 reg)
  74. {
  75. return inb(sis96x_smbus_base + reg) ;
  76. }
  77. static inline void sis96x_write(u8 reg, u8 data)
  78. {
  79. outb(data, sis96x_smbus_base + reg) ;
  80. }
  81. /* Execute a SMBus transaction.
  82. int size is from SIS96x_QUICK to SIS96x_BLOCK_DATA
  83. */
  84. static int sis96x_transaction(int size)
  85. {
  86. int temp;
  87. int result = 0;
  88. int timeout = 0;
  89. dev_dbg(&sis96x_adapter.dev, "SMBus transaction %d\n", size);
  90. /* Make sure the SMBus host is ready to start transmitting */
  91. if (((temp = sis96x_read(SMB_CNT)) & 0x03) != 0x00) {
  92. dev_dbg(&sis96x_adapter.dev, "SMBus busy (0x%02x). "
  93. "Resetting...\n", temp);
  94. /* kill the transaction */
  95. sis96x_write(SMB_HOST_CNT, 0x20);
  96. /* check it again */
  97. if (((temp = sis96x_read(SMB_CNT)) & 0x03) != 0x00) {
  98. dev_dbg(&sis96x_adapter.dev, "Failed (0x%02x)\n", temp);
  99. return -1;
  100. } else {
  101. dev_dbg(&sis96x_adapter.dev, "Successful\n");
  102. }
  103. }
  104. /* Turn off timeout interrupts, set fast host clock */
  105. sis96x_write(SMB_CNT, 0x20);
  106. /* clear all (sticky) status flags */
  107. temp = sis96x_read(SMB_STS);
  108. sis96x_write(SMB_STS, temp & 0x1e);
  109. /* start the transaction by setting bit 4 and size bits */
  110. sis96x_write(SMB_HOST_CNT, 0x10 | (size & 0x07));
  111. /* We will always wait for a fraction of a second! */
  112. do {
  113. msleep(1);
  114. temp = sis96x_read(SMB_STS);
  115. } while (!(temp & 0x0e) && (timeout++ < MAX_TIMEOUT));
  116. /* If the SMBus is still busy, we give up */
  117. if (timeout >= MAX_TIMEOUT) {
  118. dev_dbg(&sis96x_adapter.dev, "SMBus Timeout! (0x%02x)\n", temp);
  119. result = -1;
  120. }
  121. /* device error - probably missing ACK */
  122. if (temp & 0x02) {
  123. dev_dbg(&sis96x_adapter.dev, "Failed bus transaction!\n");
  124. result = -1;
  125. }
  126. /* bus collision */
  127. if (temp & 0x04) {
  128. dev_dbg(&sis96x_adapter.dev, "Bus collision!\n");
  129. result = -1;
  130. }
  131. /* Finish up by resetting the bus */
  132. sis96x_write(SMB_STS, temp);
  133. if ((temp = sis96x_read(SMB_STS))) {
  134. dev_dbg(&sis96x_adapter.dev, "Failed reset at "
  135. "end of transaction! (0x%02x)\n", temp);
  136. }
  137. return result;
  138. }
  139. /* Return -1 on error. */
  140. static s32 sis96x_access(struct i2c_adapter * adap, u16 addr,
  141. unsigned short flags, char read_write,
  142. u8 command, int size, union i2c_smbus_data * data)
  143. {
  144. switch (size) {
  145. case I2C_SMBUS_QUICK:
  146. sis96x_write(SMB_ADDR, ((addr & 0x7f) << 1) | (read_write & 0x01));
  147. size = SIS96x_QUICK;
  148. break;
  149. case I2C_SMBUS_BYTE:
  150. sis96x_write(SMB_ADDR, ((addr & 0x7f) << 1) | (read_write & 0x01));
  151. if (read_write == I2C_SMBUS_WRITE)
  152. sis96x_write(SMB_CMD, command);
  153. size = SIS96x_BYTE;
  154. break;
  155. case I2C_SMBUS_BYTE_DATA:
  156. sis96x_write(SMB_ADDR, ((addr & 0x7f) << 1) | (read_write & 0x01));
  157. sis96x_write(SMB_CMD, command);
  158. if (read_write == I2C_SMBUS_WRITE)
  159. sis96x_write(SMB_BYTE, data->byte);
  160. size = SIS96x_BYTE_DATA;
  161. break;
  162. case I2C_SMBUS_PROC_CALL:
  163. case I2C_SMBUS_WORD_DATA:
  164. sis96x_write(SMB_ADDR, ((addr & 0x7f) << 1) | (read_write & 0x01));
  165. sis96x_write(SMB_CMD, command);
  166. if (read_write == I2C_SMBUS_WRITE) {
  167. sis96x_write(SMB_BYTE, data->word & 0xff);
  168. sis96x_write(SMB_BYTE + 1, (data->word & 0xff00) >> 8);
  169. }
  170. size = (size == I2C_SMBUS_PROC_CALL ?
  171. SIS96x_PROC_CALL : SIS96x_WORD_DATA);
  172. break;
  173. case I2C_SMBUS_BLOCK_DATA:
  174. /* TO DO: */
  175. dev_info(&adap->dev, "SMBus block not implemented!\n");
  176. return -1;
  177. break;
  178. default:
  179. dev_info(&adap->dev, "Unsupported I2C size\n");
  180. return -1;
  181. break;
  182. }
  183. if (sis96x_transaction(size))
  184. return -1;
  185. if ((size != SIS96x_PROC_CALL) &&
  186. ((read_write == I2C_SMBUS_WRITE) || (size == SIS96x_QUICK)))
  187. return 0;
  188. switch (size) {
  189. case SIS96x_BYTE:
  190. case SIS96x_BYTE_DATA:
  191. data->byte = sis96x_read(SMB_BYTE);
  192. break;
  193. case SIS96x_WORD_DATA:
  194. case SIS96x_PROC_CALL:
  195. data->word = sis96x_read(SMB_BYTE) +
  196. (sis96x_read(SMB_BYTE + 1) << 8);
  197. break;
  198. }
  199. return 0;
  200. }
  201. static u32 sis96x_func(struct i2c_adapter *adapter)
  202. {
  203. return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
  204. I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
  205. I2C_FUNC_SMBUS_PROC_CALL;
  206. }
  207. static struct i2c_algorithm smbus_algorithm = {
  208. .smbus_xfer = sis96x_access,
  209. .functionality = sis96x_func,
  210. };
  211. static struct i2c_adapter sis96x_adapter = {
  212. .owner = THIS_MODULE,
  213. .class = I2C_CLASS_HWMON,
  214. .algo = &smbus_algorithm,
  215. };
  216. static struct pci_device_id sis96x_ids[] = {
  217. { PCI_DEVICE(PCI_VENDOR_ID_SI, PCI_DEVICE_ID_SI_SMBUS) },
  218. { 0, }
  219. };
  220. MODULE_DEVICE_TABLE (pci, sis96x_ids);
  221. static int __devinit sis96x_probe(struct pci_dev *dev,
  222. const struct pci_device_id *id)
  223. {
  224. u16 ww = 0;
  225. int retval;
  226. if (sis96x_smbus_base) {
  227. dev_err(&dev->dev, "Only one device supported.\n");
  228. return -EBUSY;
  229. }
  230. pci_read_config_word(dev, PCI_CLASS_DEVICE, &ww);
  231. if (PCI_CLASS_SERIAL_SMBUS != ww) {
  232. dev_err(&dev->dev, "Unsupported device class 0x%04x!\n", ww);
  233. return -ENODEV;
  234. }
  235. sis96x_smbus_base = pci_resource_start(dev, SIS96x_BAR);
  236. if (!sis96x_smbus_base) {
  237. dev_err(&dev->dev, "SiS96x SMBus base address "
  238. "not initialized!\n");
  239. return -EINVAL;
  240. }
  241. dev_info(&dev->dev, "SiS96x SMBus base address: 0x%04x\n",
  242. sis96x_smbus_base);
  243. /* Everything is happy, let's grab the memory and set things up. */
  244. if (!request_region(sis96x_smbus_base, SMB_IOSIZE,
  245. sis96x_driver.name)) {
  246. dev_err(&dev->dev, "SMBus registers 0x%04x-0x%04x "
  247. "already in use!\n", sis96x_smbus_base,
  248. sis96x_smbus_base + SMB_IOSIZE - 1);
  249. sis96x_smbus_base = 0;
  250. return -EINVAL;
  251. }
  252. /* set up the driverfs linkage to our parent device */
  253. sis96x_adapter.dev.parent = &dev->dev;
  254. snprintf(sis96x_adapter.name, I2C_NAME_SIZE,
  255. "SiS96x SMBus adapter at 0x%04x", sis96x_smbus_base);
  256. if ((retval = i2c_add_adapter(&sis96x_adapter))) {
  257. dev_err(&dev->dev, "Couldn't register adapter!\n");
  258. release_region(sis96x_smbus_base, SMB_IOSIZE);
  259. sis96x_smbus_base = 0;
  260. }
  261. return retval;
  262. }
  263. static void __devexit sis96x_remove(struct pci_dev *dev)
  264. {
  265. if (sis96x_smbus_base) {
  266. i2c_del_adapter(&sis96x_adapter);
  267. release_region(sis96x_smbus_base, SMB_IOSIZE);
  268. sis96x_smbus_base = 0;
  269. }
  270. }
  271. static struct pci_driver sis96x_driver = {
  272. .name = "sis96x_smbus",
  273. .id_table = sis96x_ids,
  274. .probe = sis96x_probe,
  275. .remove = __devexit_p(sis96x_remove),
  276. };
  277. static int __init i2c_sis96x_init(void)
  278. {
  279. printk(KERN_INFO "i2c-sis96x version %s\n", SIS96x_VERSION);
  280. return pci_register_driver(&sis96x_driver);
  281. }
  282. static void __exit i2c_sis96x_exit(void)
  283. {
  284. pci_unregister_driver(&sis96x_driver);
  285. }
  286. MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>");
  287. MODULE_DESCRIPTION("SiS96x SMBus driver");
  288. MODULE_LICENSE("GPL");
  289. /* Register initialization functions using helper macros */
  290. module_init(i2c_sis96x_init);
  291. module_exit(i2c_sis96x_exit);