i2c-amd756.c 11 KB

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