i2c-amd756.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. /*
  2. Copyright (c) 1999-2002 Merlin Hughes <merlin@merlin.org>
  3. Shamelessly ripped from i2c-piix4.c:
  4. Copyright (c) 1998, 1999 Frodo Looijaard <frodol@dds.nl> and
  5. Philip Edelbrock <phil@netroedge.com>
  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; either version 2 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. */
  15. /*
  16. 2002-04-08: Added nForce support. (Csaba Halasz)
  17. 2002-10-03: Fixed nForce PnP I/O port. (Michael Steil)
  18. 2002-12-28: Rewritten into something that resembles a Linux driver (hch)
  19. 2003-11-29: Added back AMD8111 removed by the previous rewrite.
  20. (Philip Pokorny)
  21. */
  22. /*
  23. Supports AMD756, AMD766, AMD768, AMD8111 and nVidia nForce
  24. Note: we assume there can only be one device, with one SMBus interface.
  25. */
  26. #include <linux/module.h>
  27. #include <linux/pci.h>
  28. #include <linux/kernel.h>
  29. #include <linux/delay.h>
  30. #include <linux/stddef.h>
  31. #include <linux/ioport.h>
  32. #include <linux/i2c.h>
  33. #include <linux/acpi.h>
  34. #include <linux/io.h>
  35. /* AMD756 SMBus address offsets */
  36. #define SMB_ADDR_OFFSET 0xE0
  37. #define SMB_IOSIZE 16
  38. #define SMB_GLOBAL_STATUS (0x0 + amd756_ioport)
  39. #define SMB_GLOBAL_ENABLE (0x2 + amd756_ioport)
  40. #define SMB_HOST_ADDRESS (0x4 + amd756_ioport)
  41. #define SMB_HOST_DATA (0x6 + amd756_ioport)
  42. #define SMB_HOST_COMMAND (0x8 + amd756_ioport)
  43. #define SMB_HOST_BLOCK_DATA (0x9 + amd756_ioport)
  44. #define SMB_HAS_DATA (0xA + amd756_ioport)
  45. #define SMB_HAS_DEVICE_ADDRESS (0xC + amd756_ioport)
  46. #define SMB_HAS_HOST_ADDRESS (0xE + amd756_ioport)
  47. #define SMB_SNOOP_ADDRESS (0xF + amd756_ioport)
  48. /* PCI Address Constants */
  49. /* address of I/O space */
  50. #define SMBBA 0x058 /* mh */
  51. #define SMBBANFORCE 0x014
  52. /* general configuration */
  53. #define SMBGCFG 0x041 /* mh */
  54. /* silicon revision code */
  55. #define SMBREV 0x008
  56. /* Other settings */
  57. #define MAX_TIMEOUT 500
  58. /* AMD756 constants */
  59. #define AMD756_QUICK 0x00
  60. #define AMD756_BYTE 0x01
  61. #define AMD756_BYTE_DATA 0x02
  62. #define AMD756_WORD_DATA 0x03
  63. #define AMD756_PROCESS_CALL 0x04
  64. #define AMD756_BLOCK_DATA 0x05
  65. static struct pci_driver amd756_driver;
  66. static unsigned short amd756_ioport;
  67. /*
  68. SMBUS event = I/O 28-29 bit 11
  69. see E0 for the status bits and enabled in E2
  70. */
  71. #define GS_ABRT_STS (1 << 0)
  72. #define GS_COL_STS (1 << 1)
  73. #define GS_PRERR_STS (1 << 2)
  74. #define GS_HST_STS (1 << 3)
  75. #define GS_HCYC_STS (1 << 4)
  76. #define GS_TO_STS (1 << 5)
  77. #define GS_SMB_STS (1 << 11)
  78. #define GS_CLEAR_STS (GS_ABRT_STS | GS_COL_STS | GS_PRERR_STS | \
  79. GS_HCYC_STS | GS_TO_STS )
  80. #define GE_CYC_TYPE_MASK (7)
  81. #define GE_HOST_STC (1 << 3)
  82. #define GE_ABORT (1 << 5)
  83. static int amd756_transaction(struct i2c_adapter *adap)
  84. {
  85. int temp;
  86. int result = 0;
  87. int timeout = 0;
  88. dev_dbg(&adap->dev, "Transaction (pre): GS=%04x, GE=%04x, ADD=%04x, "
  89. "DAT=%04x\n", inw_p(SMB_GLOBAL_STATUS),
  90. inw_p(SMB_GLOBAL_ENABLE), inw_p(SMB_HOST_ADDRESS),
  91. inb_p(SMB_HOST_DATA));
  92. /* Make sure the SMBus host is ready to start transmitting */
  93. if ((temp = inw_p(SMB_GLOBAL_STATUS)) & (GS_HST_STS | GS_SMB_STS)) {
  94. dev_dbg(&adap->dev, "SMBus busy (%04x). Waiting...\n", temp);
  95. do {
  96. msleep(1);
  97. temp = inw_p(SMB_GLOBAL_STATUS);
  98. } while ((temp & (GS_HST_STS | GS_SMB_STS)) &&
  99. (timeout++ < MAX_TIMEOUT));
  100. /* If the SMBus is still busy, we give up */
  101. if (timeout > MAX_TIMEOUT) {
  102. dev_dbg(&adap->dev, "Busy wait timeout (%04x)\n", temp);
  103. goto abort;
  104. }
  105. timeout = 0;
  106. }
  107. /* start the transaction by setting the start bit */
  108. outw_p(inw(SMB_GLOBAL_ENABLE) | GE_HOST_STC, SMB_GLOBAL_ENABLE);
  109. /* We will always wait for a fraction of a second! */
  110. do {
  111. msleep(1);
  112. temp = inw_p(SMB_GLOBAL_STATUS);
  113. } while ((temp & GS_HST_STS) && (timeout++ < MAX_TIMEOUT));
  114. /* If the SMBus is still busy, we give up */
  115. if (timeout > MAX_TIMEOUT) {
  116. dev_dbg(&adap->dev, "Completion timeout!\n");
  117. goto abort;
  118. }
  119. if (temp & GS_PRERR_STS) {
  120. result = -ENXIO;
  121. dev_dbg(&adap->dev, "SMBus Protocol error (no response)!\n");
  122. }
  123. if (temp & GS_COL_STS) {
  124. result = -EIO;
  125. dev_warn(&adap->dev, "SMBus collision!\n");
  126. }
  127. if (temp & GS_TO_STS) {
  128. result = -ETIMEDOUT;
  129. dev_dbg(&adap->dev, "SMBus protocol timeout!\n");
  130. }
  131. if (temp & GS_HCYC_STS)
  132. dev_dbg(&adap->dev, "SMBus protocol success!\n");
  133. outw_p(GS_CLEAR_STS, SMB_GLOBAL_STATUS);
  134. #ifdef DEBUG
  135. if (((temp = inw_p(SMB_GLOBAL_STATUS)) & GS_CLEAR_STS) != 0x00) {
  136. dev_dbg(&adap->dev,
  137. "Failed reset at end of transaction (%04x)\n", temp);
  138. }
  139. #endif
  140. dev_dbg(&adap->dev,
  141. "Transaction (post): GS=%04x, GE=%04x, ADD=%04x, DAT=%04x\n",
  142. inw_p(SMB_GLOBAL_STATUS), inw_p(SMB_GLOBAL_ENABLE),
  143. inw_p(SMB_HOST_ADDRESS), inb_p(SMB_HOST_DATA));
  144. return result;
  145. abort:
  146. dev_warn(&adap->dev, "Sending abort\n");
  147. outw_p(inw(SMB_GLOBAL_ENABLE) | GE_ABORT, SMB_GLOBAL_ENABLE);
  148. msleep(100);
  149. outw_p(GS_CLEAR_STS, SMB_GLOBAL_STATUS);
  150. return -EIO;
  151. }
  152. /* Return negative errno on error. */
  153. static s32 amd756_access(struct i2c_adapter * adap, u16 addr,
  154. unsigned short flags, char read_write,
  155. u8 command, int size, union i2c_smbus_data * data)
  156. {
  157. int i, len;
  158. int status;
  159. switch (size) {
  160. case I2C_SMBUS_QUICK:
  161. outw_p(((addr & 0x7f) << 1) | (read_write & 0x01),
  162. SMB_HOST_ADDRESS);
  163. size = AMD756_QUICK;
  164. break;
  165. case I2C_SMBUS_BYTE:
  166. outw_p(((addr & 0x7f) << 1) | (read_write & 0x01),
  167. SMB_HOST_ADDRESS);
  168. if (read_write == I2C_SMBUS_WRITE)
  169. outb_p(command, SMB_HOST_DATA);
  170. size = AMD756_BYTE;
  171. break;
  172. case I2C_SMBUS_BYTE_DATA:
  173. outw_p(((addr & 0x7f) << 1) | (read_write & 0x01),
  174. SMB_HOST_ADDRESS);
  175. outb_p(command, SMB_HOST_COMMAND);
  176. if (read_write == I2C_SMBUS_WRITE)
  177. outw_p(data->byte, SMB_HOST_DATA);
  178. size = AMD756_BYTE_DATA;
  179. break;
  180. case I2C_SMBUS_WORD_DATA:
  181. outw_p(((addr & 0x7f) << 1) | (read_write & 0x01),
  182. SMB_HOST_ADDRESS);
  183. outb_p(command, SMB_HOST_COMMAND);
  184. if (read_write == I2C_SMBUS_WRITE)
  185. outw_p(data->word, SMB_HOST_DATA); /* TODO: endian???? */
  186. size = AMD756_WORD_DATA;
  187. break;
  188. case I2C_SMBUS_BLOCK_DATA:
  189. outw_p(((addr & 0x7f) << 1) | (read_write & 0x01),
  190. SMB_HOST_ADDRESS);
  191. outb_p(command, SMB_HOST_COMMAND);
  192. if (read_write == I2C_SMBUS_WRITE) {
  193. len = data->block[0];
  194. if (len < 0)
  195. len = 0;
  196. if (len > 32)
  197. len = 32;
  198. outw_p(len, SMB_HOST_DATA);
  199. /* i = inw_p(SMBHSTCNT); Reset SMBBLKDAT */
  200. for (i = 1; i <= len; i++)
  201. outb_p(data->block[i],
  202. SMB_HOST_BLOCK_DATA);
  203. }
  204. size = AMD756_BLOCK_DATA;
  205. break;
  206. default:
  207. dev_warn(&adap->dev, "Unsupported transaction %d\n", size);
  208. return -EOPNOTSUPP;
  209. }
  210. /* How about enabling interrupts... */
  211. outw_p(size & GE_CYC_TYPE_MASK, SMB_GLOBAL_ENABLE);
  212. status = amd756_transaction(adap);
  213. if (status)
  214. return status;
  215. if ((read_write == I2C_SMBUS_WRITE) || (size == AMD756_QUICK))
  216. return 0;
  217. switch (size) {
  218. case AMD756_BYTE:
  219. data->byte = inw_p(SMB_HOST_DATA);
  220. break;
  221. case AMD756_BYTE_DATA:
  222. data->byte = inw_p(SMB_HOST_DATA);
  223. break;
  224. case AMD756_WORD_DATA:
  225. data->word = inw_p(SMB_HOST_DATA); /* TODO: endian???? */
  226. break;
  227. case AMD756_BLOCK_DATA:
  228. data->block[0] = inw_p(SMB_HOST_DATA) & 0x3f;
  229. if(data->block[0] > 32)
  230. data->block[0] = 32;
  231. /* i = inw_p(SMBHSTCNT); Reset SMBBLKDAT */
  232. for (i = 1; i <= data->block[0]; i++)
  233. data->block[i] = inb_p(SMB_HOST_BLOCK_DATA);
  234. break;
  235. }
  236. return 0;
  237. }
  238. static u32 amd756_func(struct i2c_adapter *adapter)
  239. {
  240. return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
  241. I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
  242. I2C_FUNC_SMBUS_BLOCK_DATA;
  243. }
  244. static const struct i2c_algorithm smbus_algorithm = {
  245. .smbus_xfer = amd756_access,
  246. .functionality = amd756_func,
  247. };
  248. struct i2c_adapter amd756_smbus = {
  249. .owner = THIS_MODULE,
  250. .class = I2C_CLASS_HWMON | I2C_CLASS_SPD,
  251. .algo = &smbus_algorithm,
  252. };
  253. enum chiptype { AMD756, AMD766, AMD768, NFORCE, AMD8111 };
  254. static const char* chipname[] = {
  255. "AMD756", "AMD766", "AMD768",
  256. "nVidia nForce", "AMD8111",
  257. };
  258. static const struct pci_device_id amd756_ids[] = {
  259. { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_VIPER_740B),
  260. .driver_data = AMD756 },
  261. { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_VIPER_7413),
  262. .driver_data = AMD766 },
  263. { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_OPUS_7443),
  264. .driver_data = AMD768 },
  265. { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_8111_SMBUS),
  266. .driver_data = AMD8111 },
  267. { PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_SMBUS),
  268. .driver_data = NFORCE },
  269. { 0, }
  270. };
  271. MODULE_DEVICE_TABLE (pci, amd756_ids);
  272. static int amd756_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  273. {
  274. int nforce = (id->driver_data == NFORCE);
  275. int error;
  276. u8 temp;
  277. if (amd756_ioport) {
  278. dev_err(&pdev->dev, "Only one device supported "
  279. "(you have a strange motherboard, btw)\n");
  280. return -ENODEV;
  281. }
  282. if (nforce) {
  283. if (PCI_FUNC(pdev->devfn) != 1)
  284. return -ENODEV;
  285. pci_read_config_word(pdev, SMBBANFORCE, &amd756_ioport);
  286. amd756_ioport &= 0xfffc;
  287. } else { /* amd */
  288. if (PCI_FUNC(pdev->devfn) != 3)
  289. return -ENODEV;
  290. pci_read_config_byte(pdev, SMBGCFG, &temp);
  291. if ((temp & 128) == 0) {
  292. dev_err(&pdev->dev,
  293. "Error: SMBus controller I/O not enabled!\n");
  294. return -ENODEV;
  295. }
  296. /* Determine the address of the SMBus areas */
  297. /* Technically it is a dword but... */
  298. pci_read_config_word(pdev, SMBBA, &amd756_ioport);
  299. amd756_ioport &= 0xff00;
  300. amd756_ioport += SMB_ADDR_OFFSET;
  301. }
  302. error = acpi_check_region(amd756_ioport, SMB_IOSIZE,
  303. amd756_driver.name);
  304. if (error)
  305. return -ENODEV;
  306. if (!request_region(amd756_ioport, SMB_IOSIZE, amd756_driver.name)) {
  307. dev_err(&pdev->dev, "SMB region 0x%x already in use!\n",
  308. amd756_ioport);
  309. return -ENODEV;
  310. }
  311. pci_read_config_byte(pdev, SMBREV, &temp);
  312. dev_dbg(&pdev->dev, "SMBREV = 0x%X\n", temp);
  313. dev_dbg(&pdev->dev, "AMD756_smba = 0x%X\n", amd756_ioport);
  314. /* set up the sysfs linkage to our parent device */
  315. amd756_smbus.dev.parent = &pdev->dev;
  316. snprintf(amd756_smbus.name, sizeof(amd756_smbus.name),
  317. "SMBus %s adapter at %04x", chipname[id->driver_data],
  318. amd756_ioport);
  319. error = i2c_add_adapter(&amd756_smbus);
  320. if (error) {
  321. dev_err(&pdev->dev,
  322. "Adapter registration failed, module not inserted\n");
  323. goto out_err;
  324. }
  325. return 0;
  326. out_err:
  327. release_region(amd756_ioport, SMB_IOSIZE);
  328. return error;
  329. }
  330. static void amd756_remove(struct pci_dev *dev)
  331. {
  332. i2c_del_adapter(&amd756_smbus);
  333. release_region(amd756_ioport, SMB_IOSIZE);
  334. }
  335. static struct pci_driver amd756_driver = {
  336. .name = "amd756_smbus",
  337. .id_table = amd756_ids,
  338. .probe = amd756_probe,
  339. .remove = amd756_remove,
  340. };
  341. module_pci_driver(amd756_driver);
  342. MODULE_AUTHOR("Merlin Hughes <merlin@merlin.org>");
  343. MODULE_DESCRIPTION("AMD756/766/768/8111 and nVidia nForce SMBus driver");
  344. MODULE_LICENSE("GPL");
  345. EXPORT_SYMBOL(amd756_smbus);