i2c-amd8111.c 11 KB

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