i2c-amd8111.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. /*
  2. * SMBus 2.0 driver for AMD-8111 IO-Hub.
  3. *
  4. * Copyright (c) 2002 Vojtech Pavlik
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation version 2.
  9. */
  10. #include <linux/module.h>
  11. #include <linux/pci.h>
  12. #include <linux/kernel.h>
  13. #include <linux/stddef.h>
  14. #include <linux/sched.h>
  15. #include <linux/ioport.h>
  16. #include <linux/init.h>
  17. #include <linux/i2c.h>
  18. #include <linux/delay.h>
  19. #include <asm/io.h>
  20. MODULE_LICENSE("GPL");
  21. MODULE_AUTHOR ("Vojtech Pavlik <vojtech@suse.cz>");
  22. MODULE_DESCRIPTION("AMD8111 SMBus 2.0 driver");
  23. struct amd_smbus {
  24. struct pci_dev *dev;
  25. struct i2c_adapter adapter;
  26. int base;
  27. int size;
  28. };
  29. static struct pci_driver amd8111_driver;
  30. /*
  31. * AMD PCI control registers definitions.
  32. */
  33. #define AMD_PCI_MISC 0x48
  34. #define AMD_PCI_MISC_SCI 0x04 /* deliver SCI */
  35. #define AMD_PCI_MISC_INT 0x02 /* deliver PCI IRQ */
  36. #define AMD_PCI_MISC_SPEEDUP 0x01 /* 16x clock speedup */
  37. /*
  38. * ACPI 2.0 chapter 13 PCI interface definitions.
  39. */
  40. #define AMD_EC_DATA 0x00 /* data register */
  41. #define AMD_EC_SC 0x04 /* status of controller */
  42. #define AMD_EC_CMD 0x04 /* command register */
  43. #define AMD_EC_ICR 0x08 /* interrupt control register */
  44. #define AMD_EC_SC_SMI 0x04 /* smi event pending */
  45. #define AMD_EC_SC_SCI 0x02 /* sci event pending */
  46. #define AMD_EC_SC_BURST 0x01 /* burst mode enabled */
  47. #define AMD_EC_SC_CMD 0x08 /* byte in data reg is command */
  48. #define AMD_EC_SC_IBF 0x02 /* data ready for embedded controller */
  49. #define AMD_EC_SC_OBF 0x01 /* data ready for host */
  50. #define AMD_EC_CMD_RD 0x80 /* read EC */
  51. #define AMD_EC_CMD_WR 0x81 /* write EC */
  52. #define AMD_EC_CMD_BE 0x82 /* enable burst mode */
  53. #define AMD_EC_CMD_BD 0x83 /* disable burst mode */
  54. #define AMD_EC_CMD_QR 0x84 /* query EC */
  55. /*
  56. * ACPI 2.0 chapter 13 access of registers of the EC
  57. */
  58. static unsigned int amd_ec_wait_write(struct amd_smbus *smbus)
  59. {
  60. int timeout = 500;
  61. while (timeout-- && (inb(smbus->base + AMD_EC_SC) & AMD_EC_SC_IBF))
  62. udelay(1);
  63. if (!timeout) {
  64. dev_warn(&smbus->dev->dev, "Timeout while waiting for IBF to clear\n");
  65. return -1;
  66. }
  67. return 0;
  68. }
  69. static unsigned int amd_ec_wait_read(struct amd_smbus *smbus)
  70. {
  71. int timeout = 500;
  72. while (timeout-- && (~inb(smbus->base + AMD_EC_SC) & AMD_EC_SC_OBF))
  73. udelay(1);
  74. if (!timeout) {
  75. dev_warn(&smbus->dev->dev, "Timeout while waiting for OBF to set\n");
  76. return -1;
  77. }
  78. return 0;
  79. }
  80. static unsigned int amd_ec_read(struct amd_smbus *smbus, unsigned char address, unsigned char *data)
  81. {
  82. if (amd_ec_wait_write(smbus))
  83. return -1;
  84. outb(AMD_EC_CMD_RD, smbus->base + AMD_EC_CMD);
  85. if (amd_ec_wait_write(smbus))
  86. return -1;
  87. outb(address, smbus->base + AMD_EC_DATA);
  88. if (amd_ec_wait_read(smbus))
  89. return -1;
  90. *data = inb(smbus->base + AMD_EC_DATA);
  91. return 0;
  92. }
  93. static unsigned int amd_ec_write(struct amd_smbus *smbus, unsigned char address, unsigned char data)
  94. {
  95. if (amd_ec_wait_write(smbus))
  96. return -1;
  97. outb(AMD_EC_CMD_WR, smbus->base + AMD_EC_CMD);
  98. if (amd_ec_wait_write(smbus))
  99. return -1;
  100. outb(address, smbus->base + AMD_EC_DATA);
  101. if (amd_ec_wait_write(smbus))
  102. return -1;
  103. outb(data, smbus->base + AMD_EC_DATA);
  104. return 0;
  105. }
  106. /*
  107. * ACPI 2.0 chapter 13 SMBus 2.0 EC register model
  108. */
  109. #define AMD_SMB_PRTCL 0x00 /* protocol, PEC */
  110. #define AMD_SMB_STS 0x01 /* status */
  111. #define AMD_SMB_ADDR 0x02 /* address */
  112. #define AMD_SMB_CMD 0x03 /* command */
  113. #define AMD_SMB_DATA 0x04 /* 32 data registers */
  114. #define AMD_SMB_BCNT 0x24 /* number of data bytes */
  115. #define AMD_SMB_ALRM_A 0x25 /* alarm address */
  116. #define AMD_SMB_ALRM_D 0x26 /* 2 bytes alarm data */
  117. #define AMD_SMB_STS_DONE 0x80
  118. #define AMD_SMB_STS_ALRM 0x40
  119. #define AMD_SMB_STS_RES 0x20
  120. #define AMD_SMB_STS_STATUS 0x1f
  121. #define AMD_SMB_STATUS_OK 0x00
  122. #define AMD_SMB_STATUS_FAIL 0x07
  123. #define AMD_SMB_STATUS_DNAK 0x10
  124. #define AMD_SMB_STATUS_DERR 0x11
  125. #define AMD_SMB_STATUS_CMD_DENY 0x12
  126. #define AMD_SMB_STATUS_UNKNOWN 0x13
  127. #define AMD_SMB_STATUS_ACC_DENY 0x17
  128. #define AMD_SMB_STATUS_TIMEOUT 0x18
  129. #define AMD_SMB_STATUS_NOTSUP 0x19
  130. #define AMD_SMB_STATUS_BUSY 0x1A
  131. #define AMD_SMB_STATUS_PEC 0x1F
  132. #define AMD_SMB_PRTCL_WRITE 0x00
  133. #define AMD_SMB_PRTCL_READ 0x01
  134. #define AMD_SMB_PRTCL_QUICK 0x02
  135. #define AMD_SMB_PRTCL_BYTE 0x04
  136. #define AMD_SMB_PRTCL_BYTE_DATA 0x06
  137. #define AMD_SMB_PRTCL_WORD_DATA 0x08
  138. #define AMD_SMB_PRTCL_BLOCK_DATA 0x0a
  139. #define AMD_SMB_PRTCL_PROC_CALL 0x0c
  140. #define AMD_SMB_PRTCL_BLOCK_PROC_CALL 0x0d
  141. #define AMD_SMB_PRTCL_I2C_BLOCK_DATA 0x4a
  142. #define AMD_SMB_PRTCL_PEC 0x80
  143. static s32 amd8111_access(struct i2c_adapter * adap, u16 addr, unsigned short flags,
  144. char read_write, u8 command, int size, union i2c_smbus_data * data)
  145. {
  146. struct amd_smbus *smbus = adap->algo_data;
  147. unsigned char protocol, len, pec, temp[2];
  148. int i;
  149. protocol = (read_write == I2C_SMBUS_READ) ? AMD_SMB_PRTCL_READ : AMD_SMB_PRTCL_WRITE;
  150. pec = (flags & I2C_CLIENT_PEC) ? AMD_SMB_PRTCL_PEC : 0;
  151. switch (size) {
  152. case I2C_SMBUS_QUICK:
  153. protocol |= AMD_SMB_PRTCL_QUICK;
  154. read_write = I2C_SMBUS_WRITE;
  155. break;
  156. case I2C_SMBUS_BYTE:
  157. if (read_write == I2C_SMBUS_WRITE)
  158. amd_ec_write(smbus, AMD_SMB_CMD, command);
  159. protocol |= AMD_SMB_PRTCL_BYTE;
  160. break;
  161. case I2C_SMBUS_BYTE_DATA:
  162. amd_ec_write(smbus, AMD_SMB_CMD, command);
  163. if (read_write == I2C_SMBUS_WRITE)
  164. amd_ec_write(smbus, AMD_SMB_DATA, data->byte);
  165. protocol |= AMD_SMB_PRTCL_BYTE_DATA;
  166. break;
  167. case I2C_SMBUS_WORD_DATA:
  168. amd_ec_write(smbus, AMD_SMB_CMD, command);
  169. if (read_write == I2C_SMBUS_WRITE) {
  170. amd_ec_write(smbus, AMD_SMB_DATA, data->word);
  171. amd_ec_write(smbus, AMD_SMB_DATA + 1, data->word >> 8);
  172. }
  173. protocol |= AMD_SMB_PRTCL_WORD_DATA | pec;
  174. break;
  175. case I2C_SMBUS_BLOCK_DATA:
  176. amd_ec_write(smbus, AMD_SMB_CMD, command);
  177. if (read_write == I2C_SMBUS_WRITE) {
  178. len = min_t(u8, data->block[0], 32);
  179. amd_ec_write(smbus, AMD_SMB_BCNT, len);
  180. for (i = 0; i < len; i++)
  181. amd_ec_write(smbus, AMD_SMB_DATA + i, data->block[i + 1]);
  182. }
  183. protocol |= AMD_SMB_PRTCL_BLOCK_DATA | pec;
  184. break;
  185. case I2C_SMBUS_I2C_BLOCK_DATA:
  186. len = min_t(u8, data->block[0], 32);
  187. amd_ec_write(smbus, AMD_SMB_CMD, command);
  188. amd_ec_write(smbus, AMD_SMB_BCNT, len);
  189. if (read_write == I2C_SMBUS_WRITE)
  190. for (i = 0; i < len; i++)
  191. amd_ec_write(smbus, AMD_SMB_DATA + i, data->block[i + 1]);
  192. protocol |= AMD_SMB_PRTCL_I2C_BLOCK_DATA;
  193. break;
  194. case I2C_SMBUS_PROC_CALL:
  195. amd_ec_write(smbus, AMD_SMB_CMD, command);
  196. amd_ec_write(smbus, AMD_SMB_DATA, data->word);
  197. amd_ec_write(smbus, AMD_SMB_DATA + 1, data->word >> 8);
  198. protocol = AMD_SMB_PRTCL_PROC_CALL | pec;
  199. read_write = I2C_SMBUS_READ;
  200. break;
  201. case I2C_SMBUS_BLOCK_PROC_CALL:
  202. len = min_t(u8, data->block[0], 31);
  203. amd_ec_write(smbus, AMD_SMB_CMD, command);
  204. amd_ec_write(smbus, AMD_SMB_BCNT, len);
  205. for (i = 0; i < len; i++)
  206. amd_ec_write(smbus, AMD_SMB_DATA + i, data->block[i + 1]);
  207. protocol = AMD_SMB_PRTCL_BLOCK_PROC_CALL | pec;
  208. read_write = I2C_SMBUS_READ;
  209. break;
  210. default:
  211. dev_warn(&adap->dev, "Unsupported transaction %d\n", size);
  212. return -1;
  213. }
  214. amd_ec_write(smbus, AMD_SMB_ADDR, addr << 1);
  215. amd_ec_write(smbus, AMD_SMB_PRTCL, protocol);
  216. amd_ec_read(smbus, AMD_SMB_STS, temp + 0);
  217. if (~temp[0] & AMD_SMB_STS_DONE) {
  218. udelay(500);
  219. amd_ec_read(smbus, AMD_SMB_STS, temp + 0);
  220. }
  221. if (~temp[0] & AMD_SMB_STS_DONE) {
  222. msleep(1);
  223. amd_ec_read(smbus, AMD_SMB_STS, temp + 0);
  224. }
  225. if ((~temp[0] & AMD_SMB_STS_DONE) || (temp[0] & AMD_SMB_STS_STATUS))
  226. return -1;
  227. if (read_write == I2C_SMBUS_WRITE)
  228. return 0;
  229. switch (size) {
  230. case I2C_SMBUS_BYTE:
  231. case I2C_SMBUS_BYTE_DATA:
  232. amd_ec_read(smbus, AMD_SMB_DATA, &data->byte);
  233. break;
  234. case I2C_SMBUS_WORD_DATA:
  235. case I2C_SMBUS_PROC_CALL:
  236. amd_ec_read(smbus, AMD_SMB_DATA, temp + 0);
  237. amd_ec_read(smbus, AMD_SMB_DATA + 1, temp + 1);
  238. data->word = (temp[1] << 8) | temp[0];
  239. break;
  240. case I2C_SMBUS_BLOCK_DATA:
  241. case I2C_SMBUS_BLOCK_PROC_CALL:
  242. amd_ec_read(smbus, AMD_SMB_BCNT, &len);
  243. len = min_t(u8, len, 32);
  244. case I2C_SMBUS_I2C_BLOCK_DATA:
  245. for (i = 0; i < len; i++)
  246. amd_ec_read(smbus, AMD_SMB_DATA + i, data->block + i + 1);
  247. data->block[0] = len;
  248. break;
  249. }
  250. return 0;
  251. }
  252. static u32 amd8111_func(struct i2c_adapter *adapter)
  253. {
  254. return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE | I2C_FUNC_SMBUS_BYTE_DATA |
  255. I2C_FUNC_SMBUS_WORD_DATA | I2C_FUNC_SMBUS_BLOCK_DATA |
  256. I2C_FUNC_SMBUS_PROC_CALL | I2C_FUNC_SMBUS_BLOCK_PROC_CALL |
  257. I2C_FUNC_SMBUS_I2C_BLOCK | I2C_FUNC_SMBUS_HWPEC_CALC;
  258. }
  259. static struct i2c_algorithm smbus_algorithm = {
  260. .smbus_xfer = amd8111_access,
  261. .functionality = amd8111_func,
  262. };
  263. static struct pci_device_id amd8111_ids[] = {
  264. { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_8111_SMBUS2) },
  265. { 0, }
  266. };
  267. MODULE_DEVICE_TABLE (pci, amd8111_ids);
  268. static int __devinit amd8111_probe(struct pci_dev *dev, const struct pci_device_id *id)
  269. {
  270. struct amd_smbus *smbus;
  271. int error = -ENODEV;
  272. if (~pci_resource_flags(dev, 0) & IORESOURCE_IO)
  273. return -ENODEV;
  274. smbus = kzalloc(sizeof(struct amd_smbus), GFP_KERNEL);
  275. if (!smbus)
  276. return -ENOMEM;
  277. smbus->dev = dev;
  278. smbus->base = pci_resource_start(dev, 0);
  279. smbus->size = pci_resource_len(dev, 0);
  280. if (!request_region(smbus->base, smbus->size, amd8111_driver.name))
  281. goto out_kfree;
  282. smbus->adapter.owner = THIS_MODULE;
  283. snprintf(smbus->adapter.name, I2C_NAME_SIZE,
  284. "SMBus2 AMD8111 adapter at %04x", smbus->base);
  285. smbus->adapter.class = I2C_CLASS_HWMON;
  286. smbus->adapter.algo = &smbus_algorithm;
  287. smbus->adapter.algo_data = smbus;
  288. /* set up the driverfs linkage to our parent device */
  289. smbus->adapter.dev.parent = &dev->dev;
  290. error = i2c_add_adapter(&smbus->adapter);
  291. if (error)
  292. goto out_release_region;
  293. pci_write_config_dword(smbus->dev, AMD_PCI_MISC, 0);
  294. pci_set_drvdata(dev, smbus);
  295. return 0;
  296. out_release_region:
  297. release_region(smbus->base, smbus->size);
  298. out_kfree:
  299. kfree(smbus);
  300. return -1;
  301. }
  302. static void __devexit amd8111_remove(struct pci_dev *dev)
  303. {
  304. struct amd_smbus *smbus = pci_get_drvdata(dev);
  305. i2c_del_adapter(&smbus->adapter);
  306. release_region(smbus->base, smbus->size);
  307. kfree(smbus);
  308. }
  309. static struct pci_driver amd8111_driver = {
  310. .name = "amd8111_smbus2",
  311. .id_table = amd8111_ids,
  312. .probe = amd8111_probe,
  313. .remove = __devexit_p(amd8111_remove),
  314. };
  315. static int __init i2c_amd8111_init(void)
  316. {
  317. return pci_register_driver(&amd8111_driver);
  318. }
  319. static void __exit i2c_amd8111_exit(void)
  320. {
  321. pci_unregister_driver(&amd8111_driver);
  322. }
  323. module_init(i2c_amd8111_init);
  324. module_exit(i2c_amd8111_exit);