i2c-sis96x.c 8.8 KB

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