i2c-isch.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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 SMBHSTCLK (2 + sch_smba)
  38. #define SMBHSTADD (4 + sch_smba) /* TSA */
  39. #define SMBHSTCMD (5 + sch_smba)
  40. #define SMBHSTDAT0 (6 + sch_smba)
  41. #define SMBHSTDAT1 (7 + sch_smba)
  42. #define SMBBLKDAT (0x20 + sch_smba)
  43. /* Other settings */
  44. #define MAX_RETRIES 5000
  45. /* I2C constants */
  46. #define SCH_QUICK 0x00
  47. #define SCH_BYTE 0x01
  48. #define SCH_BYTE_DATA 0x02
  49. #define SCH_WORD_DATA 0x03
  50. #define SCH_BLOCK_DATA 0x05
  51. static unsigned short sch_smba;
  52. static struct i2c_adapter sch_adapter;
  53. static int backbone_speed = 33000; /* backbone speed in kHz */
  54. module_param(backbone_speed, int, S_IRUSR | S_IWUSR);
  55. MODULE_PARM_DESC(backbone_speed, "Backbone speed in kHz, (default = 33000)");
  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 retries = 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. usleep_range(100, 200);
  94. temp = inb(SMBHSTSTS) & 0x0f;
  95. } while ((temp & 0x08) && (retries++ < MAX_RETRIES));
  96. /* If the SMBus is still busy, we give up */
  97. if (retries > MAX_RETRIES) {
  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 transferred 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. temp = inw(SMBHSTCLK);
  147. if (!temp) {
  148. /*
  149. * We can't determine if we have 33 or 25 MHz clock for
  150. * SMBus, so expect 33 MHz and calculate a bus clock of
  151. * 100 kHz. If we actually run at 25 MHz the bus will be
  152. * run ~75 kHz instead which should do no harm.
  153. */
  154. dev_notice(&sch_adapter.dev,
  155. "Clock divider unitialized. Setting defaults\n");
  156. outw(backbone_speed / (4 * 100), SMBHSTCLK);
  157. }
  158. dev_dbg(&sch_adapter.dev, "access size: %d %s\n", size,
  159. (read_write)?"READ":"WRITE");
  160. switch (size) {
  161. case I2C_SMBUS_QUICK:
  162. outb((addr << 1) | read_write, SMBHSTADD);
  163. size = SCH_QUICK;
  164. break;
  165. case I2C_SMBUS_BYTE:
  166. outb((addr << 1) | read_write, SMBHSTADD);
  167. if (read_write == I2C_SMBUS_WRITE)
  168. outb(command, SMBHSTCMD);
  169. size = SCH_BYTE;
  170. break;
  171. case I2C_SMBUS_BYTE_DATA:
  172. outb((addr << 1) | read_write, SMBHSTADD);
  173. outb(command, SMBHSTCMD);
  174. if (read_write == I2C_SMBUS_WRITE)
  175. outb(data->byte, SMBHSTDAT0);
  176. size = SCH_BYTE_DATA;
  177. break;
  178. case I2C_SMBUS_WORD_DATA:
  179. outb((addr << 1) | read_write, SMBHSTADD);
  180. outb(command, SMBHSTCMD);
  181. if (read_write == I2C_SMBUS_WRITE) {
  182. outb(data->word & 0xff, SMBHSTDAT0);
  183. outb((data->word & 0xff00) >> 8, SMBHSTDAT1);
  184. }
  185. size = SCH_WORD_DATA;
  186. break;
  187. case I2C_SMBUS_BLOCK_DATA:
  188. outb((addr << 1) | read_write, SMBHSTADD);
  189. outb(command, SMBHSTCMD);
  190. if (read_write == I2C_SMBUS_WRITE) {
  191. len = data->block[0];
  192. if (len == 0 || len > I2C_SMBUS_BLOCK_MAX)
  193. return -EINVAL;
  194. outb(len, SMBHSTDAT0);
  195. for (i = 1; i <= len; i++)
  196. outb(data->block[i], SMBBLKDAT+i-1);
  197. }
  198. size = SCH_BLOCK_DATA;
  199. break;
  200. default:
  201. dev_warn(&adap->dev, "Unsupported transaction %d\n", size);
  202. return -EOPNOTSUPP;
  203. }
  204. dev_dbg(&sch_adapter.dev, "write size %d to 0x%04x\n", size, SMBHSTCNT);
  205. outb((inb(SMBHSTCNT) & 0xb0) | (size & 0x7), SMBHSTCNT);
  206. rc = sch_transaction();
  207. if (rc) /* Error in transaction */
  208. return rc;
  209. if ((read_write == I2C_SMBUS_WRITE) || (size == SCH_QUICK))
  210. return 0;
  211. switch (size) {
  212. case SCH_BYTE:
  213. case SCH_BYTE_DATA:
  214. data->byte = inb(SMBHSTDAT0);
  215. break;
  216. case SCH_WORD_DATA:
  217. data->word = inb(SMBHSTDAT0) + (inb(SMBHSTDAT1) << 8);
  218. break;
  219. case SCH_BLOCK_DATA:
  220. data->block[0] = inb(SMBHSTDAT0);
  221. if (data->block[0] == 0 || data->block[0] > I2C_SMBUS_BLOCK_MAX)
  222. return -EPROTO;
  223. for (i = 1; i <= data->block[0]; i++)
  224. data->block[i] = inb(SMBBLKDAT+i-1);
  225. break;
  226. }
  227. return 0;
  228. }
  229. static u32 sch_func(struct i2c_adapter *adapter)
  230. {
  231. return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
  232. I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
  233. I2C_FUNC_SMBUS_BLOCK_DATA;
  234. }
  235. static const struct i2c_algorithm smbus_algorithm = {
  236. .smbus_xfer = sch_access,
  237. .functionality = sch_func,
  238. };
  239. static struct i2c_adapter sch_adapter = {
  240. .owner = THIS_MODULE,
  241. .class = I2C_CLASS_HWMON | I2C_CLASS_SPD,
  242. .algo = &smbus_algorithm,
  243. };
  244. static int smbus_sch_probe(struct platform_device *dev)
  245. {
  246. struct resource *res;
  247. int retval;
  248. res = platform_get_resource(dev, IORESOURCE_IO, 0);
  249. if (!res)
  250. return -EBUSY;
  251. if (!request_region(res->start, resource_size(res), 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. release_region(res->start, resource_size(res));
  266. sch_smba = 0;
  267. }
  268. return retval;
  269. }
  270. static int smbus_sch_remove(struct platform_device *pdev)
  271. {
  272. struct resource *res;
  273. if (sch_smba) {
  274. i2c_del_adapter(&sch_adapter);
  275. res = platform_get_resource(pdev, IORESOURCE_IO, 0);
  276. release_region(res->start, resource_size(res));
  277. sch_smba = 0;
  278. }
  279. return 0;
  280. }
  281. static struct platform_driver smbus_sch_driver = {
  282. .driver = {
  283. .name = "isch_smbus",
  284. .owner = THIS_MODULE,
  285. },
  286. .probe = smbus_sch_probe,
  287. .remove = smbus_sch_remove,
  288. };
  289. module_platform_driver(smbus_sch_driver);
  290. MODULE_AUTHOR("Jacob Pan <jacob.jun.pan@intel.com>");
  291. MODULE_DESCRIPTION("Intel SCH SMBus driver");
  292. MODULE_LICENSE("GPL");
  293. MODULE_ALIAS("platform:isch_smbus");