i2c-isch.c 9.0 KB

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