i2c-isch.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. /*
  2. i2c-isch.c - Linux kernel driver for Intel SCH chipset SMBus
  3. - Based on i2c-piix4.c
  4. Copyright (c) 1998 - 2002 Frodo Looijaard <frodol@dds.nl> and
  5. Philip Edelbrock <phil@netroedge.com>
  6. - Intel SCH support
  7. Copyright (c) 2007 - 2008 Jacob Jun Pan <jacob.jun.pan@intel.com>
  8. This program is free software; you can redistribute it and/or modify
  9. it under the terms of the GNU General Public License version 2 as
  10. published by the Free Software Foundation.
  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:
  21. Intel SCH chipsets (AF82US15W, AF82US15L, AF82UL11L)
  22. Note: we assume there can only be one device, with one SMBus interface.
  23. */
  24. #include <linux/module.h>
  25. #include <linux/pci.h>
  26. #include <linux/kernel.h>
  27. #include <linux/delay.h>
  28. #include <linux/stddef.h>
  29. #include <linux/ioport.h>
  30. #include <linux/i2c.h>
  31. #include <linux/init.h>
  32. #include <linux/io.h>
  33. /* SCH SMBus address offsets */
  34. #define SMBHSTCNT (0 + sch_smba)
  35. #define SMBHSTSTS (1 + sch_smba)
  36. #define SMBHSTADD (4 + sch_smba) /* TSA */
  37. #define SMBHSTCMD (5 + sch_smba)
  38. #define SMBHSTDAT0 (6 + sch_smba)
  39. #define SMBHSTDAT1 (7 + sch_smba)
  40. #define SMBBLKDAT (0x20 + sch_smba)
  41. /* count for request_region */
  42. #define SMBIOSIZE 64
  43. /* PCI Address Constants */
  44. #define SMBBA_SCH 0x40
  45. /* Other settings */
  46. #define MAX_TIMEOUT 500
  47. /* I2C constants */
  48. #define SCH_QUICK 0x00
  49. #define SCH_BYTE 0x01
  50. #define SCH_BYTE_DATA 0x02
  51. #define SCH_WORD_DATA 0x03
  52. #define SCH_BLOCK_DATA 0x05
  53. static unsigned short sch_smba;
  54. static struct pci_driver sch_driver;
  55. static struct i2c_adapter sch_adapter;
  56. /*
  57. * Start the i2c transaction -- the i2c_access will prepare the transaction
  58. * and this function will execute it.
  59. * return 0 for success and others for failure.
  60. */
  61. static int sch_transaction(void)
  62. {
  63. int temp;
  64. int result = 0;
  65. int timeout = 0;
  66. dev_dbg(&sch_adapter.dev, "Transaction (pre): CNT=%02x, CMD=%02x, "
  67. "ADD=%02x, DAT0=%02x, DAT1=%02x\n", inb(SMBHSTCNT),
  68. inb(SMBHSTCMD), inb(SMBHSTADD), inb(SMBHSTDAT0),
  69. inb(SMBHSTDAT1));
  70. /* Make sure the SMBus host is ready to start transmitting */
  71. temp = inb(SMBHSTSTS) & 0x0f;
  72. if (temp) {
  73. /* Can not be busy since we checked it in sch_access */
  74. if (temp & 0x01) {
  75. dev_dbg(&sch_adapter.dev, "Completion (%02x). "
  76. "Clear...\n", temp);
  77. }
  78. if (temp & 0x06) {
  79. dev_dbg(&sch_adapter.dev, "SMBus error (%02x). "
  80. "Resetting...\n", temp);
  81. }
  82. outb(temp, SMBHSTSTS);
  83. temp = inb(SMBHSTSTS) & 0x0f;
  84. if (temp) {
  85. dev_err(&sch_adapter.dev,
  86. "SMBus is not ready: (%02x)\n", temp);
  87. return -EAGAIN;
  88. }
  89. }
  90. /* start the transaction by setting bit 4 */
  91. outb(inb(SMBHSTCNT) | 0x10, SMBHSTCNT);
  92. do {
  93. msleep(1);
  94. temp = inb(SMBHSTSTS) & 0x0f;
  95. } while ((temp & 0x08) && (timeout++ < MAX_TIMEOUT));
  96. /* If the SMBus is still busy, we give up */
  97. if (timeout >= MAX_TIMEOUT) {
  98. dev_err(&sch_adapter.dev, "SMBus Timeout!\n");
  99. result = -ETIMEDOUT;
  100. }
  101. if (temp & 0x04) {
  102. result = -EIO;
  103. dev_dbg(&sch_adapter.dev, "Bus collision! SMBus may be "
  104. "locked until next hard reset. (sorry!)\n");
  105. /* Clock stops and slave is stuck in mid-transmission */
  106. } else if (temp & 0x02) {
  107. result = -EIO;
  108. dev_err(&sch_adapter.dev, "Error: no response!\n");
  109. } else if (temp & 0x01) {
  110. dev_dbg(&sch_adapter.dev, "Post complete!\n");
  111. outb(temp, SMBHSTSTS);
  112. temp = inb(SMBHSTSTS) & 0x07;
  113. if (temp & 0x06) {
  114. /* Completion clear failed */
  115. dev_dbg(&sch_adapter.dev, "Failed reset at end of "
  116. "transaction (%02x), Bus error!\n", temp);
  117. }
  118. } else {
  119. result = -ENXIO;
  120. dev_dbg(&sch_adapter.dev, "No such address.\n");
  121. }
  122. dev_dbg(&sch_adapter.dev, "Transaction (post): CNT=%02x, CMD=%02x, "
  123. "ADD=%02x, DAT0=%02x, DAT1=%02x\n", inb(SMBHSTCNT),
  124. inb(SMBHSTCMD), inb(SMBHSTADD), inb(SMBHSTDAT0),
  125. inb(SMBHSTDAT1));
  126. return result;
  127. }
  128. /*
  129. * This is the main access entry for i2c-sch access
  130. * adap is i2c_adapter pointer, addr is the i2c device bus address, read_write
  131. * (0 for read and 1 for write), size is i2c transaction type and data is the
  132. * union of transaction for data to be transfered or data read from bus.
  133. * return 0 for success and others for failure.
  134. */
  135. static s32 sch_access(struct i2c_adapter *adap, u16 addr,
  136. unsigned short flags, char read_write,
  137. u8 command, int size, union i2c_smbus_data *data)
  138. {
  139. int i, len, temp, rc;
  140. /* Make sure the SMBus host is not busy */
  141. temp = inb(SMBHSTSTS) & 0x0f;
  142. if (temp & 0x08) {
  143. dev_dbg(&sch_adapter.dev, "SMBus busy (%02x)\n", temp);
  144. return -EAGAIN;
  145. }
  146. dev_dbg(&sch_adapter.dev, "access size: %d %s\n", size,
  147. (read_write)?"READ":"WRITE");
  148. switch (size) {
  149. case I2C_SMBUS_QUICK:
  150. outb((addr << 1) | read_write, SMBHSTADD);
  151. size = SCH_QUICK;
  152. break;
  153. case I2C_SMBUS_BYTE:
  154. outb((addr << 1) | read_write, SMBHSTADD);
  155. if (read_write == I2C_SMBUS_WRITE)
  156. outb(command, SMBHSTCMD);
  157. size = SCH_BYTE;
  158. break;
  159. case I2C_SMBUS_BYTE_DATA:
  160. outb((addr << 1) | read_write, SMBHSTADD);
  161. outb(command, SMBHSTCMD);
  162. if (read_write == I2C_SMBUS_WRITE)
  163. outb(data->byte, SMBHSTDAT0);
  164. size = SCH_BYTE_DATA;
  165. break;
  166. case I2C_SMBUS_WORD_DATA:
  167. outb((addr << 1) | read_write, SMBHSTADD);
  168. outb(command, SMBHSTCMD);
  169. if (read_write == I2C_SMBUS_WRITE) {
  170. outb(data->word & 0xff, SMBHSTDAT0);
  171. outb((data->word & 0xff00) >> 8, SMBHSTDAT1);
  172. }
  173. size = SCH_WORD_DATA;
  174. break;
  175. case I2C_SMBUS_BLOCK_DATA:
  176. outb((addr << 1) | read_write, SMBHSTADD);
  177. outb(command, SMBHSTCMD);
  178. if (read_write == I2C_SMBUS_WRITE) {
  179. len = data->block[0];
  180. if (len == 0 || len > I2C_SMBUS_BLOCK_MAX)
  181. return -EINVAL;
  182. outb(len, SMBHSTDAT0);
  183. for (i = 1; i <= len; i++)
  184. outb(data->block[i], SMBBLKDAT+i-1);
  185. }
  186. size = SCH_BLOCK_DATA;
  187. break;
  188. default:
  189. dev_warn(&adap->dev, "Unsupported transaction %d\n", size);
  190. return -EOPNOTSUPP;
  191. }
  192. dev_dbg(&sch_adapter.dev, "write size %d to 0x%04x\n", size, SMBHSTCNT);
  193. outb((inb(SMBHSTCNT) & 0xb0) | (size & 0x7), SMBHSTCNT);
  194. rc = sch_transaction();
  195. if (rc) /* Error in transaction */
  196. return rc;
  197. if ((read_write == I2C_SMBUS_WRITE) || (size == SCH_QUICK))
  198. return 0;
  199. switch (size) {
  200. case SCH_BYTE:
  201. case SCH_BYTE_DATA:
  202. data->byte = inb(SMBHSTDAT0);
  203. break;
  204. case SCH_WORD_DATA:
  205. data->word = inb(SMBHSTDAT0) + (inb(SMBHSTDAT1) << 8);
  206. break;
  207. case SCH_BLOCK_DATA:
  208. data->block[0] = inb(SMBHSTDAT0);
  209. if (data->block[0] == 0 || data->block[0] > I2C_SMBUS_BLOCK_MAX)
  210. return -EPROTO;
  211. for (i = 1; i <= data->block[0]; i++)
  212. data->block[i] = inb(SMBBLKDAT+i-1);
  213. break;
  214. }
  215. return 0;
  216. }
  217. static u32 sch_func(struct i2c_adapter *adapter)
  218. {
  219. return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
  220. I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
  221. I2C_FUNC_SMBUS_BLOCK_DATA;
  222. }
  223. static const struct i2c_algorithm smbus_algorithm = {
  224. .smbus_xfer = sch_access,
  225. .functionality = sch_func,
  226. };
  227. static struct i2c_adapter sch_adapter = {
  228. .owner = THIS_MODULE,
  229. .class = I2C_CLASS_HWMON | I2C_CLASS_SPD,
  230. .algo = &smbus_algorithm,
  231. };
  232. static struct pci_device_id sch_ids[] = {
  233. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_SCH_LPC) },
  234. { 0, }
  235. };
  236. MODULE_DEVICE_TABLE(pci, sch_ids);
  237. static int __devinit sch_probe(struct pci_dev *dev,
  238. const struct pci_device_id *id)
  239. {
  240. int retval;
  241. unsigned int smba;
  242. pci_read_config_dword(dev, SMBBA_SCH, &smba);
  243. if (!(smba & (1 << 31))) {
  244. dev_err(&dev->dev, "SMBus I/O space disabled!\n");
  245. return -ENODEV;
  246. }
  247. sch_smba = (unsigned short)smba;
  248. if (sch_smba == 0) {
  249. dev_err(&dev->dev, "SMBus base address uninitialized!\n");
  250. return -ENODEV;
  251. }
  252. if (!request_region(sch_smba, SMBIOSIZE, sch_driver.name)) {
  253. dev_err(&dev->dev, "SMBus region 0x%x already in use!\n",
  254. sch_smba);
  255. return -EBUSY;
  256. }
  257. dev_dbg(&dev->dev, "SMBA = 0x%X\n", sch_smba);
  258. /* set up the sysfs linkage to our parent device */
  259. sch_adapter.dev.parent = &dev->dev;
  260. snprintf(sch_adapter.name, sizeof(sch_adapter.name),
  261. "SMBus SCH adapter at %04x", sch_smba);
  262. retval = i2c_add_adapter(&sch_adapter);
  263. if (retval) {
  264. dev_err(&dev->dev, "Couldn't register adapter!\n");
  265. release_region(sch_smba, SMBIOSIZE);
  266. sch_smba = 0;
  267. }
  268. return retval;
  269. }
  270. static void __devexit sch_remove(struct pci_dev *dev)
  271. {
  272. if (sch_smba) {
  273. i2c_del_adapter(&sch_adapter);
  274. release_region(sch_smba, SMBIOSIZE);
  275. sch_smba = 0;
  276. }
  277. }
  278. static struct pci_driver sch_driver = {
  279. .name = "isch_smbus",
  280. .id_table = sch_ids,
  281. .probe = sch_probe,
  282. .remove = __devexit_p(sch_remove),
  283. };
  284. static int __init i2c_sch_init(void)
  285. {
  286. return pci_register_driver(&sch_driver);
  287. }
  288. static void __exit i2c_sch_exit(void)
  289. {
  290. pci_unregister_driver(&sch_driver);
  291. }
  292. MODULE_AUTHOR("Jacob Pan <jacob.jun.pan@intel.com>");
  293. MODULE_DESCRIPTION("Intel SCH SMBus driver");
  294. MODULE_LICENSE("GPL");
  295. module_init(i2c_sch_init);
  296. module_exit(i2c_sch_exit);