i2c-isch.c 8.6 KB

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