i2c-isch.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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. */
  16. /*
  17. Supports:
  18. Intel SCH chipsets (AF82US15W, AF82US15L, AF82UL11L)
  19. Note: we assume there can only be one device, with one SMBus interface.
  20. */
  21. #include <linux/module.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/kernel.h>
  24. #include <linux/delay.h>
  25. #include <linux/stddef.h>
  26. #include <linux/ioport.h>
  27. #include <linux/i2c.h>
  28. #include <linux/io.h>
  29. #include <linux/acpi.h>
  30. /* SCH SMBus address offsets */
  31. #define SMBHSTCNT (0 + sch_smba)
  32. #define SMBHSTSTS (1 + sch_smba)
  33. #define SMBHSTCLK (2 + sch_smba)
  34. #define SMBHSTADD (4 + sch_smba) /* TSA */
  35. #define SMBHSTCMD (5 + sch_smba)
  36. #define SMBHSTDAT0 (6 + sch_smba)
  37. #define SMBHSTDAT1 (7 + sch_smba)
  38. #define SMBBLKDAT (0x20 + sch_smba)
  39. /* Other settings */
  40. #define MAX_RETRIES 5000
  41. /* I2C constants */
  42. #define SCH_QUICK 0x00
  43. #define SCH_BYTE 0x01
  44. #define SCH_BYTE_DATA 0x02
  45. #define SCH_WORD_DATA 0x03
  46. #define SCH_BLOCK_DATA 0x05
  47. static unsigned short sch_smba;
  48. static struct i2c_adapter sch_adapter;
  49. static int backbone_speed = 33000; /* backbone speed in kHz */
  50. module_param(backbone_speed, int, S_IRUSR | S_IWUSR);
  51. MODULE_PARM_DESC(backbone_speed, "Backbone speed in kHz, (default = 33000)");
  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. temp = inw(SMBHSTCLK);
  143. if (!temp) {
  144. /*
  145. * We can't determine if we have 33 or 25 MHz clock for
  146. * SMBus, so expect 33 MHz and calculate a bus clock of
  147. * 100 kHz. If we actually run at 25 MHz the bus will be
  148. * run ~75 kHz instead which should do no harm.
  149. */
  150. dev_notice(&sch_adapter.dev,
  151. "Clock divider unitialized. Setting defaults\n");
  152. outw(backbone_speed / (4 * 100), SMBHSTCLK);
  153. }
  154. dev_dbg(&sch_adapter.dev, "access size: %d %s\n", size,
  155. (read_write)?"READ":"WRITE");
  156. switch (size) {
  157. case I2C_SMBUS_QUICK:
  158. outb((addr << 1) | read_write, SMBHSTADD);
  159. size = SCH_QUICK;
  160. break;
  161. case I2C_SMBUS_BYTE:
  162. outb((addr << 1) | read_write, SMBHSTADD);
  163. if (read_write == I2C_SMBUS_WRITE)
  164. outb(command, SMBHSTCMD);
  165. size = SCH_BYTE;
  166. break;
  167. case I2C_SMBUS_BYTE_DATA:
  168. outb((addr << 1) | read_write, SMBHSTADD);
  169. outb(command, SMBHSTCMD);
  170. if (read_write == I2C_SMBUS_WRITE)
  171. outb(data->byte, SMBHSTDAT0);
  172. size = SCH_BYTE_DATA;
  173. break;
  174. case I2C_SMBUS_WORD_DATA:
  175. outb((addr << 1) | read_write, SMBHSTADD);
  176. outb(command, SMBHSTCMD);
  177. if (read_write == I2C_SMBUS_WRITE) {
  178. outb(data->word & 0xff, SMBHSTDAT0);
  179. outb((data->word & 0xff00) >> 8, SMBHSTDAT1);
  180. }
  181. size = SCH_WORD_DATA;
  182. break;
  183. case I2C_SMBUS_BLOCK_DATA:
  184. outb((addr << 1) | read_write, SMBHSTADD);
  185. outb(command, SMBHSTCMD);
  186. if (read_write == I2C_SMBUS_WRITE) {
  187. len = data->block[0];
  188. if (len == 0 || len > I2C_SMBUS_BLOCK_MAX)
  189. return -EINVAL;
  190. outb(len, SMBHSTDAT0);
  191. for (i = 1; i <= len; i++)
  192. outb(data->block[i], SMBBLKDAT+i-1);
  193. }
  194. size = SCH_BLOCK_DATA;
  195. break;
  196. default:
  197. dev_warn(&adap->dev, "Unsupported transaction %d\n", size);
  198. return -EOPNOTSUPP;
  199. }
  200. dev_dbg(&sch_adapter.dev, "write size %d to 0x%04x\n", size, SMBHSTCNT);
  201. outb((inb(SMBHSTCNT) & 0xb0) | (size & 0x7), SMBHSTCNT);
  202. rc = sch_transaction();
  203. if (rc) /* Error in transaction */
  204. return rc;
  205. if ((read_write == I2C_SMBUS_WRITE) || (size == SCH_QUICK))
  206. return 0;
  207. switch (size) {
  208. case SCH_BYTE:
  209. case SCH_BYTE_DATA:
  210. data->byte = inb(SMBHSTDAT0);
  211. break;
  212. case SCH_WORD_DATA:
  213. data->word = inb(SMBHSTDAT0) + (inb(SMBHSTDAT1) << 8);
  214. break;
  215. case SCH_BLOCK_DATA:
  216. data->block[0] = inb(SMBHSTDAT0);
  217. if (data->block[0] == 0 || data->block[0] > I2C_SMBUS_BLOCK_MAX)
  218. return -EPROTO;
  219. for (i = 1; i <= data->block[0]; i++)
  220. data->block[i] = inb(SMBBLKDAT+i-1);
  221. break;
  222. }
  223. return 0;
  224. }
  225. static u32 sch_func(struct i2c_adapter *adapter)
  226. {
  227. return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
  228. I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
  229. I2C_FUNC_SMBUS_BLOCK_DATA;
  230. }
  231. static const struct i2c_algorithm smbus_algorithm = {
  232. .smbus_xfer = sch_access,
  233. .functionality = sch_func,
  234. };
  235. static struct i2c_adapter sch_adapter = {
  236. .owner = THIS_MODULE,
  237. .class = I2C_CLASS_HWMON | I2C_CLASS_SPD,
  238. .algo = &smbus_algorithm,
  239. };
  240. static int smbus_sch_probe(struct platform_device *dev)
  241. {
  242. struct resource *res;
  243. int retval;
  244. res = platform_get_resource(dev, IORESOURCE_IO, 0);
  245. if (!res)
  246. return -EBUSY;
  247. if (!devm_request_region(&dev->dev, res->start, resource_size(res),
  248. dev->name)) {
  249. dev_err(&dev->dev, "SMBus region 0x%x already in use!\n",
  250. sch_smba);
  251. return -EBUSY;
  252. }
  253. sch_smba = res->start;
  254. dev_dbg(&dev->dev, "SMBA = 0x%X\n", sch_smba);
  255. /* set up the sysfs linkage to our parent device */
  256. sch_adapter.dev.parent = &dev->dev;
  257. snprintf(sch_adapter.name, sizeof(sch_adapter.name),
  258. "SMBus SCH adapter at %04x", sch_smba);
  259. retval = i2c_add_adapter(&sch_adapter);
  260. if (retval) {
  261. dev_err(&dev->dev, "Couldn't register adapter!\n");
  262. sch_smba = 0;
  263. }
  264. return retval;
  265. }
  266. static int smbus_sch_remove(struct platform_device *pdev)
  267. {
  268. if (sch_smba) {
  269. i2c_del_adapter(&sch_adapter);
  270. sch_smba = 0;
  271. }
  272. return 0;
  273. }
  274. static struct platform_driver smbus_sch_driver = {
  275. .driver = {
  276. .name = "isch_smbus",
  277. },
  278. .probe = smbus_sch_probe,
  279. .remove = smbus_sch_remove,
  280. };
  281. module_platform_driver(smbus_sch_driver);
  282. MODULE_AUTHOR("Jacob Pan <jacob.jun.pan@intel.com>");
  283. MODULE_DESCRIPTION("Intel SCH SMBus driver");
  284. MODULE_LICENSE("GPL");
  285. MODULE_ALIAS("platform:isch_smbus");