i2c-amd756.c 11 KB

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