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/sched.h>
  37. #include <linux/ioport.h>
  38. #include <linux/i2c.h>
  39. #include <linux/init.h>
  40. #include <asm/io.h>
  41. /* AMD756 SMBus address offsets */
  42. #define SMB_ADDR_OFFSET 0xE0
  43. #define SMB_IOSIZE 16
  44. #define SMB_GLOBAL_STATUS (0x0 + amd756_ioport)
  45. #define SMB_GLOBAL_ENABLE (0x2 + amd756_ioport)
  46. #define SMB_HOST_ADDRESS (0x4 + amd756_ioport)
  47. #define SMB_HOST_DATA (0x6 + amd756_ioport)
  48. #define SMB_HOST_COMMAND (0x8 + amd756_ioport)
  49. #define SMB_HOST_BLOCK_DATA (0x9 + amd756_ioport)
  50. #define SMB_HAS_DATA (0xA + amd756_ioport)
  51. #define SMB_HAS_DEVICE_ADDRESS (0xC + amd756_ioport)
  52. #define SMB_HAS_HOST_ADDRESS (0xE + amd756_ioport)
  53. #define SMB_SNOOP_ADDRESS (0xF + amd756_ioport)
  54. /* PCI Address Constants */
  55. /* address of I/O space */
  56. #define SMBBA 0x058 /* mh */
  57. #define SMBBANFORCE 0x014
  58. /* general configuration */
  59. #define SMBGCFG 0x041 /* mh */
  60. /* silicon revision code */
  61. #define SMBREV 0x008
  62. /* Other settings */
  63. #define MAX_TIMEOUT 500
  64. /* AMD756 constants */
  65. #define AMD756_QUICK 0x00
  66. #define AMD756_BYTE 0x01
  67. #define AMD756_BYTE_DATA 0x02
  68. #define AMD756_WORD_DATA 0x03
  69. #define AMD756_PROCESS_CALL 0x04
  70. #define AMD756_BLOCK_DATA 0x05
  71. static struct pci_driver amd756_driver;
  72. static unsigned short amd756_ioport;
  73. /*
  74. SMBUS event = I/O 28-29 bit 11
  75. see E0 for the status bits and enabled in E2
  76. */
  77. #define GS_ABRT_STS (1 << 0)
  78. #define GS_COL_STS (1 << 1)
  79. #define GS_PRERR_STS (1 << 2)
  80. #define GS_HST_STS (1 << 3)
  81. #define GS_HCYC_STS (1 << 4)
  82. #define GS_TO_STS (1 << 5)
  83. #define GS_SMB_STS (1 << 11)
  84. #define GS_CLEAR_STS (GS_ABRT_STS | GS_COL_STS | GS_PRERR_STS | \
  85. GS_HCYC_STS | GS_TO_STS )
  86. #define GE_CYC_TYPE_MASK (7)
  87. #define GE_HOST_STC (1 << 3)
  88. #define GE_ABORT (1 << 5)
  89. static int amd756_transaction(struct i2c_adapter *adap)
  90. {
  91. int temp;
  92. int result = 0;
  93. int timeout = 0;
  94. dev_dbg(&adap->dev, "Transaction (pre): GS=%04x, GE=%04x, ADD=%04x, "
  95. "DAT=%04x\n", inw_p(SMB_GLOBAL_STATUS),
  96. inw_p(SMB_GLOBAL_ENABLE), inw_p(SMB_HOST_ADDRESS),
  97. inb_p(SMB_HOST_DATA));
  98. /* Make sure the SMBus host is ready to start transmitting */
  99. if ((temp = inw_p(SMB_GLOBAL_STATUS)) & (GS_HST_STS | GS_SMB_STS)) {
  100. dev_dbg(&adap->dev, "SMBus busy (%04x). Waiting...\n", temp);
  101. do {
  102. msleep(1);
  103. temp = inw_p(SMB_GLOBAL_STATUS);
  104. } while ((temp & (GS_HST_STS | GS_SMB_STS)) &&
  105. (timeout++ < MAX_TIMEOUT));
  106. /* If the SMBus is still busy, we give up */
  107. if (timeout >= MAX_TIMEOUT) {
  108. dev_dbg(&adap->dev, "Busy wait timeout (%04x)\n", temp);
  109. goto abort;
  110. }
  111. timeout = 0;
  112. }
  113. /* start the transaction by setting the start bit */
  114. outw_p(inw(SMB_GLOBAL_ENABLE) | GE_HOST_STC, SMB_GLOBAL_ENABLE);
  115. /* We will always wait for a fraction of a second! */
  116. do {
  117. msleep(1);
  118. temp = inw_p(SMB_GLOBAL_STATUS);
  119. } while ((temp & GS_HST_STS) && (timeout++ < MAX_TIMEOUT));
  120. /* If the SMBus is still busy, we give up */
  121. if (timeout >= MAX_TIMEOUT) {
  122. dev_dbg(&adap->dev, "Completion timeout!\n");
  123. goto abort;
  124. }
  125. if (temp & GS_PRERR_STS) {
  126. result = -1;
  127. dev_dbg(&adap->dev, "SMBus Protocol error (no response)!\n");
  128. }
  129. if (temp & GS_COL_STS) {
  130. result = -1;
  131. dev_warn(&adap->dev, "SMBus collision!\n");
  132. }
  133. if (temp & GS_TO_STS) {
  134. result = -1;
  135. dev_dbg(&adap->dev, "SMBus protocol timeout!\n");
  136. }
  137. if (temp & GS_HCYC_STS)
  138. dev_dbg(&adap->dev, "SMBus protocol success!\n");
  139. outw_p(GS_CLEAR_STS, SMB_GLOBAL_STATUS);
  140. #ifdef DEBUG
  141. if (((temp = inw_p(SMB_GLOBAL_STATUS)) & GS_CLEAR_STS) != 0x00) {
  142. dev_dbg(&adap->dev,
  143. "Failed reset at end of transaction (%04x)\n", temp);
  144. }
  145. #endif
  146. dev_dbg(&adap->dev,
  147. "Transaction (post): GS=%04x, GE=%04x, ADD=%04x, DAT=%04x\n",
  148. inw_p(SMB_GLOBAL_STATUS), inw_p(SMB_GLOBAL_ENABLE),
  149. inw_p(SMB_HOST_ADDRESS), inb_p(SMB_HOST_DATA));
  150. return result;
  151. abort:
  152. dev_warn(&adap->dev, "Sending abort\n");
  153. outw_p(inw(SMB_GLOBAL_ENABLE) | GE_ABORT, SMB_GLOBAL_ENABLE);
  154. msleep(100);
  155. outw_p(GS_CLEAR_STS, SMB_GLOBAL_STATUS);
  156. return -1;
  157. }
  158. /* Return -1 on error. */
  159. static s32 amd756_access(struct i2c_adapter * adap, u16 addr,
  160. unsigned short flags, char read_write,
  161. u8 command, int size, union i2c_smbus_data * data)
  162. {
  163. int i, len;
  164. /** TODO: Should I supporte the 10-bit transfers? */
  165. switch (size) {
  166. case I2C_SMBUS_PROC_CALL:
  167. dev_dbg(&adap->dev, "I2C_SMBUS_PROC_CALL not supported!\n");
  168. /* TODO: Well... It is supported, I'm just not sure what to do here... */
  169. return -1;
  170. case I2C_SMBUS_QUICK:
  171. outw_p(((addr & 0x7f) << 1) | (read_write & 0x01),
  172. SMB_HOST_ADDRESS);
  173. size = AMD756_QUICK;
  174. break;
  175. case I2C_SMBUS_BYTE:
  176. outw_p(((addr & 0x7f) << 1) | (read_write & 0x01),
  177. SMB_HOST_ADDRESS);
  178. if (read_write == I2C_SMBUS_WRITE)
  179. outb_p(command, SMB_HOST_DATA);
  180. size = AMD756_BYTE;
  181. break;
  182. case I2C_SMBUS_BYTE_DATA:
  183. outw_p(((addr & 0x7f) << 1) | (read_write & 0x01),
  184. SMB_HOST_ADDRESS);
  185. outb_p(command, SMB_HOST_COMMAND);
  186. if (read_write == I2C_SMBUS_WRITE)
  187. outw_p(data->byte, SMB_HOST_DATA);
  188. size = AMD756_BYTE_DATA;
  189. break;
  190. case I2C_SMBUS_WORD_DATA:
  191. outw_p(((addr & 0x7f) << 1) | (read_write & 0x01),
  192. SMB_HOST_ADDRESS);
  193. outb_p(command, SMB_HOST_COMMAND);
  194. if (read_write == I2C_SMBUS_WRITE)
  195. outw_p(data->word, SMB_HOST_DATA); /* TODO: endian???? */
  196. size = AMD756_WORD_DATA;
  197. break;
  198. case I2C_SMBUS_BLOCK_DATA:
  199. outw_p(((addr & 0x7f) << 1) | (read_write & 0x01),
  200. SMB_HOST_ADDRESS);
  201. outb_p(command, SMB_HOST_COMMAND);
  202. if (read_write == I2C_SMBUS_WRITE) {
  203. len = data->block[0];
  204. if (len < 0)
  205. len = 0;
  206. if (len > 32)
  207. len = 32;
  208. outw_p(len, SMB_HOST_DATA);
  209. /* i = inw_p(SMBHSTCNT); Reset SMBBLKDAT */
  210. for (i = 1; i <= len; i++)
  211. outb_p(data->block[i],
  212. SMB_HOST_BLOCK_DATA);
  213. }
  214. size = AMD756_BLOCK_DATA;
  215. break;
  216. }
  217. /* How about enabling interrupts... */
  218. outw_p(size & GE_CYC_TYPE_MASK, SMB_GLOBAL_ENABLE);
  219. if (amd756_transaction(adap)) /* Error in transaction */
  220. return -1;
  221. if ((read_write == I2C_SMBUS_WRITE) || (size == AMD756_QUICK))
  222. return 0;
  223. switch (size) {
  224. case AMD756_BYTE:
  225. data->byte = inw_p(SMB_HOST_DATA);
  226. break;
  227. case AMD756_BYTE_DATA:
  228. data->byte = inw_p(SMB_HOST_DATA);
  229. break;
  230. case AMD756_WORD_DATA:
  231. data->word = inw_p(SMB_HOST_DATA); /* TODO: endian???? */
  232. break;
  233. case AMD756_BLOCK_DATA:
  234. data->block[0] = inw_p(SMB_HOST_DATA) & 0x3f;
  235. if(data->block[0] > 32)
  236. data->block[0] = 32;
  237. /* i = inw_p(SMBHSTCNT); Reset SMBBLKDAT */
  238. for (i = 1; i <= data->block[0]; i++)
  239. data->block[i] = inb_p(SMB_HOST_BLOCK_DATA);
  240. break;
  241. }
  242. return 0;
  243. }
  244. static u32 amd756_func(struct i2c_adapter *adapter)
  245. {
  246. return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
  247. I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
  248. I2C_FUNC_SMBUS_BLOCK_DATA | I2C_FUNC_SMBUS_PROC_CALL;
  249. }
  250. static struct i2c_algorithm smbus_algorithm = {
  251. .smbus_xfer = amd756_access,
  252. .functionality = amd756_func,
  253. };
  254. struct i2c_adapter amd756_smbus = {
  255. .owner = THIS_MODULE,
  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 driverfs 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)