i2c-sis96x.c 7.9 KB

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