i2c-amd8111.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  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/i2c.h>
  16. #include <linux/delay.h>
  17. #include <linux/acpi.h>
  18. #include <linux/slab.h>
  19. #include <linux/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 int amd_ec_wait_write(struct amd_smbus *smbus)
  59. {
  60. int timeout = 500;
  61. while ((inb(smbus->base + AMD_EC_SC) & AMD_EC_SC_IBF) && --timeout)
  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 int amd_ec_wait_read(struct amd_smbus *smbus)
  71. {
  72. int timeout = 500;
  73. while ((~inb(smbus->base + AMD_EC_SC) & AMD_EC_SC_OBF) && --timeout)
  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 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 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, status;
  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. status = amd_ec_write(smbus, AMD_SMB_CMD,
  173. command);
  174. if (status)
  175. return status;
  176. }
  177. protocol |= AMD_SMB_PRTCL_BYTE;
  178. break;
  179. case I2C_SMBUS_BYTE_DATA:
  180. status = amd_ec_write(smbus, AMD_SMB_CMD, command);
  181. if (status)
  182. return status;
  183. if (read_write == I2C_SMBUS_WRITE) {
  184. status = amd_ec_write(smbus, AMD_SMB_DATA,
  185. data->byte);
  186. if (status)
  187. return status;
  188. }
  189. protocol |= AMD_SMB_PRTCL_BYTE_DATA;
  190. break;
  191. case I2C_SMBUS_WORD_DATA:
  192. status = amd_ec_write(smbus, AMD_SMB_CMD, command);
  193. if (status)
  194. return status;
  195. if (read_write == I2C_SMBUS_WRITE) {
  196. status = amd_ec_write(smbus, AMD_SMB_DATA,
  197. data->word & 0xff);
  198. if (status)
  199. return status;
  200. status = amd_ec_write(smbus, AMD_SMB_DATA + 1,
  201. data->word >> 8);
  202. if (status)
  203. return status;
  204. }
  205. protocol |= AMD_SMB_PRTCL_WORD_DATA | pec;
  206. break;
  207. case I2C_SMBUS_BLOCK_DATA:
  208. status = amd_ec_write(smbus, AMD_SMB_CMD, command);
  209. if (status)
  210. return status;
  211. if (read_write == I2C_SMBUS_WRITE) {
  212. len = min_t(u8, data->block[0],
  213. I2C_SMBUS_BLOCK_MAX);
  214. status = amd_ec_write(smbus, AMD_SMB_BCNT, len);
  215. if (status)
  216. return status;
  217. for (i = 0; i < len; i++) {
  218. status =
  219. amd_ec_write(smbus, AMD_SMB_DATA + i,
  220. data->block[i + 1]);
  221. if (status)
  222. return status;
  223. }
  224. }
  225. protocol |= AMD_SMB_PRTCL_BLOCK_DATA | pec;
  226. break;
  227. case I2C_SMBUS_I2C_BLOCK_DATA:
  228. len = min_t(u8, data->block[0],
  229. I2C_SMBUS_BLOCK_MAX);
  230. status = amd_ec_write(smbus, AMD_SMB_CMD, command);
  231. if (status)
  232. return status;
  233. status = amd_ec_write(smbus, AMD_SMB_BCNT, len);
  234. if (status)
  235. return status;
  236. if (read_write == I2C_SMBUS_WRITE)
  237. for (i = 0; i < len; i++) {
  238. status =
  239. amd_ec_write(smbus, AMD_SMB_DATA + i,
  240. data->block[i + 1]);
  241. if (status)
  242. return status;
  243. }
  244. protocol |= AMD_SMB_PRTCL_I2C_BLOCK_DATA;
  245. break;
  246. case I2C_SMBUS_PROC_CALL:
  247. status = amd_ec_write(smbus, AMD_SMB_CMD, command);
  248. if (status)
  249. return status;
  250. status = amd_ec_write(smbus, AMD_SMB_DATA,
  251. data->word & 0xff);
  252. if (status)
  253. return status;
  254. status = amd_ec_write(smbus, AMD_SMB_DATA + 1,
  255. data->word >> 8);
  256. if (status)
  257. return status;
  258. protocol = AMD_SMB_PRTCL_PROC_CALL | pec;
  259. read_write = I2C_SMBUS_READ;
  260. break;
  261. case I2C_SMBUS_BLOCK_PROC_CALL:
  262. len = min_t(u8, data->block[0],
  263. I2C_SMBUS_BLOCK_MAX - 1);
  264. status = amd_ec_write(smbus, AMD_SMB_CMD, command);
  265. if (status)
  266. return status;
  267. status = amd_ec_write(smbus, AMD_SMB_BCNT, len);
  268. if (status)
  269. return status;
  270. for (i = 0; i < len; i++) {
  271. status = amd_ec_write(smbus, AMD_SMB_DATA + i,
  272. data->block[i + 1]);
  273. if (status)
  274. return status;
  275. }
  276. protocol = AMD_SMB_PRTCL_BLOCK_PROC_CALL | pec;
  277. read_write = I2C_SMBUS_READ;
  278. break;
  279. default:
  280. dev_warn(&adap->dev, "Unsupported transaction %d\n", size);
  281. return -EOPNOTSUPP;
  282. }
  283. status = amd_ec_write(smbus, AMD_SMB_ADDR, addr << 1);
  284. if (status)
  285. return status;
  286. status = amd_ec_write(smbus, AMD_SMB_PRTCL, protocol);
  287. if (status)
  288. return status;
  289. status = amd_ec_read(smbus, AMD_SMB_STS, temp + 0);
  290. if (status)
  291. return status;
  292. if (~temp[0] & AMD_SMB_STS_DONE) {
  293. udelay(500);
  294. status = amd_ec_read(smbus, AMD_SMB_STS, temp + 0);
  295. if (status)
  296. return status;
  297. }
  298. if (~temp[0] & AMD_SMB_STS_DONE) {
  299. msleep(1);
  300. status = amd_ec_read(smbus, AMD_SMB_STS, temp + 0);
  301. if (status)
  302. return status;
  303. }
  304. if ((~temp[0] & AMD_SMB_STS_DONE) || (temp[0] & AMD_SMB_STS_STATUS))
  305. return -EIO;
  306. if (read_write == I2C_SMBUS_WRITE)
  307. return 0;
  308. switch (size) {
  309. case I2C_SMBUS_BYTE:
  310. case I2C_SMBUS_BYTE_DATA:
  311. status = amd_ec_read(smbus, AMD_SMB_DATA, &data->byte);
  312. if (status)
  313. return status;
  314. break;
  315. case I2C_SMBUS_WORD_DATA:
  316. case I2C_SMBUS_PROC_CALL:
  317. status = amd_ec_read(smbus, AMD_SMB_DATA, temp + 0);
  318. if (status)
  319. return status;
  320. status = amd_ec_read(smbus, AMD_SMB_DATA + 1, temp + 1);
  321. if (status)
  322. return status;
  323. data->word = (temp[1] << 8) | temp[0];
  324. break;
  325. case I2C_SMBUS_BLOCK_DATA:
  326. case I2C_SMBUS_BLOCK_PROC_CALL:
  327. status = amd_ec_read(smbus, AMD_SMB_BCNT, &len);
  328. if (status)
  329. return status;
  330. len = min_t(u8, len, I2C_SMBUS_BLOCK_MAX);
  331. case I2C_SMBUS_I2C_BLOCK_DATA:
  332. for (i = 0; i < len; i++) {
  333. status = amd_ec_read(smbus, AMD_SMB_DATA + i,
  334. data->block + i + 1);
  335. if (status)
  336. return status;
  337. }
  338. data->block[0] = len;
  339. break;
  340. }
  341. return 0;
  342. }
  343. static u32 amd8111_func(struct i2c_adapter *adapter)
  344. {
  345. return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
  346. I2C_FUNC_SMBUS_BYTE_DATA |
  347. I2C_FUNC_SMBUS_WORD_DATA | I2C_FUNC_SMBUS_BLOCK_DATA |
  348. I2C_FUNC_SMBUS_PROC_CALL | I2C_FUNC_SMBUS_BLOCK_PROC_CALL |
  349. I2C_FUNC_SMBUS_I2C_BLOCK | I2C_FUNC_SMBUS_PEC;
  350. }
  351. static const struct i2c_algorithm smbus_algorithm = {
  352. .smbus_xfer = amd8111_access,
  353. .functionality = amd8111_func,
  354. };
  355. static const struct pci_device_id amd8111_ids[] = {
  356. { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_8111_SMBUS2) },
  357. { 0, }
  358. };
  359. MODULE_DEVICE_TABLE (pci, amd8111_ids);
  360. static int amd8111_probe(struct pci_dev *dev, const struct pci_device_id *id)
  361. {
  362. struct amd_smbus *smbus;
  363. int error;
  364. if (!(pci_resource_flags(dev, 0) & IORESOURCE_IO))
  365. return -ENODEV;
  366. smbus = kzalloc(sizeof(struct amd_smbus), GFP_KERNEL);
  367. if (!smbus)
  368. return -ENOMEM;
  369. smbus->dev = dev;
  370. smbus->base = pci_resource_start(dev, 0);
  371. smbus->size = pci_resource_len(dev, 0);
  372. error = acpi_check_resource_conflict(&dev->resource[0]);
  373. if (error) {
  374. error = -ENODEV;
  375. goto out_kfree;
  376. }
  377. if (!request_region(smbus->base, smbus->size, amd8111_driver.name)) {
  378. error = -EBUSY;
  379. goto out_kfree;
  380. }
  381. smbus->adapter.owner = THIS_MODULE;
  382. snprintf(smbus->adapter.name, sizeof(smbus->adapter.name),
  383. "SMBus2 AMD8111 adapter at %04x", smbus->base);
  384. smbus->adapter.class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
  385. smbus->adapter.algo = &smbus_algorithm;
  386. smbus->adapter.algo_data = smbus;
  387. /* set up the sysfs linkage to our parent device */
  388. smbus->adapter.dev.parent = &dev->dev;
  389. pci_write_config_dword(smbus->dev, AMD_PCI_MISC, 0);
  390. error = i2c_add_adapter(&smbus->adapter);
  391. if (error)
  392. goto out_release_region;
  393. pci_set_drvdata(dev, smbus);
  394. return 0;
  395. out_release_region:
  396. release_region(smbus->base, smbus->size);
  397. out_kfree:
  398. kfree(smbus);
  399. return error;
  400. }
  401. static void amd8111_remove(struct pci_dev *dev)
  402. {
  403. struct amd_smbus *smbus = pci_get_drvdata(dev);
  404. i2c_del_adapter(&smbus->adapter);
  405. release_region(smbus->base, smbus->size);
  406. kfree(smbus);
  407. }
  408. static struct pci_driver amd8111_driver = {
  409. .name = "amd8111_smbus2",
  410. .id_table = amd8111_ids,
  411. .probe = amd8111_probe,
  412. .remove = amd8111_remove,
  413. };
  414. module_pci_driver(amd8111_driver);