i2c-amd8111.c 11 KB

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