i2c-amd756.c 11 KB

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