i2c-isch.c 9.1 KB

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