i2c-ali1563.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. /**
  2. * i2c-ali1563.c - i2c driver for the ALi 1563 Southbridge
  3. *
  4. * Copyright (C) 2004 Patrick Mochel
  5. * 2005 Rudolf Marek <r.marek@assembler.cz>
  6. *
  7. * The 1563 southbridge is deceptively similar to the 1533, with a
  8. * few notable exceptions. One of those happens to be the fact they
  9. * upgraded the i2c core to be 2.0 compliant, and happens to be almost
  10. * identical to the i2c controller found in the Intel 801 south
  11. * bridges.
  12. *
  13. * This driver is based on a mix of the 15x3, 1535, and i801 drivers,
  14. * with a little help from the ALi 1563 spec.
  15. *
  16. * This file is released under the GPLv2
  17. */
  18. #include <linux/module.h>
  19. #include <linux/delay.h>
  20. #include <linux/i2c.h>
  21. #include <linux/pci.h>
  22. #include <linux/init.h>
  23. #include <linux/acpi.h>
  24. #define ALI1563_MAX_TIMEOUT 500
  25. #define ALI1563_SMBBA 0x80
  26. #define ALI1563_SMB_IOEN 1
  27. #define ALI1563_SMB_HOSTEN 2
  28. #define ALI1563_SMB_IOSIZE 16
  29. #define SMB_HST_STS (ali1563_smba + 0)
  30. #define SMB_HST_CNTL1 (ali1563_smba + 1)
  31. #define SMB_HST_CNTL2 (ali1563_smba + 2)
  32. #define SMB_HST_CMD (ali1563_smba + 3)
  33. #define SMB_HST_ADD (ali1563_smba + 4)
  34. #define SMB_HST_DAT0 (ali1563_smba + 5)
  35. #define SMB_HST_DAT1 (ali1563_smba + 6)
  36. #define SMB_BLK_DAT (ali1563_smba + 7)
  37. #define HST_STS_BUSY 0x01
  38. #define HST_STS_INTR 0x02
  39. #define HST_STS_DEVERR 0x04
  40. #define HST_STS_BUSERR 0x08
  41. #define HST_STS_FAIL 0x10
  42. #define HST_STS_DONE 0x80
  43. #define HST_STS_BAD 0x1c
  44. #define HST_CNTL1_TIMEOUT 0x80
  45. #define HST_CNTL1_LAST 0x40
  46. #define HST_CNTL2_KILL 0x04
  47. #define HST_CNTL2_START 0x40
  48. #define HST_CNTL2_QUICK 0x00
  49. #define HST_CNTL2_BYTE 0x01
  50. #define HST_CNTL2_BYTE_DATA 0x02
  51. #define HST_CNTL2_WORD_DATA 0x03
  52. #define HST_CNTL2_BLOCK 0x05
  53. #define HST_CNTL2_SIZEMASK 0x38
  54. static struct pci_driver ali1563_pci_driver;
  55. static unsigned short ali1563_smba;
  56. static int ali1563_transaction(struct i2c_adapter * a, int size)
  57. {
  58. u32 data;
  59. int timeout;
  60. int status = -EIO;
  61. dev_dbg(&a->dev, "Transaction (pre): STS=%02x, CNTL1=%02x, "
  62. "CNTL2=%02x, CMD=%02x, ADD=%02x, DAT0=%02x, DAT1=%02x\n",
  63. inb_p(SMB_HST_STS), inb_p(SMB_HST_CNTL1), inb_p(SMB_HST_CNTL2),
  64. inb_p(SMB_HST_CMD), inb_p(SMB_HST_ADD), inb_p(SMB_HST_DAT0),
  65. inb_p(SMB_HST_DAT1));
  66. data = inb_p(SMB_HST_STS);
  67. if (data & HST_STS_BAD) {
  68. dev_err(&a->dev, "ali1563: Trying to reset busy device\n");
  69. outb_p(data | HST_STS_BAD,SMB_HST_STS);
  70. data = inb_p(SMB_HST_STS);
  71. if (data & HST_STS_BAD)
  72. return -EBUSY;
  73. }
  74. outb_p(inb_p(SMB_HST_CNTL2) | HST_CNTL2_START, SMB_HST_CNTL2);
  75. timeout = ALI1563_MAX_TIMEOUT;
  76. do {
  77. msleep(1);
  78. } while (((data = inb_p(SMB_HST_STS)) & HST_STS_BUSY) && --timeout);
  79. dev_dbg(&a->dev, "Transaction (post): STS=%02x, CNTL1=%02x, "
  80. "CNTL2=%02x, CMD=%02x, ADD=%02x, DAT0=%02x, DAT1=%02x\n",
  81. inb_p(SMB_HST_STS), inb_p(SMB_HST_CNTL1), inb_p(SMB_HST_CNTL2),
  82. inb_p(SMB_HST_CMD), inb_p(SMB_HST_ADD), inb_p(SMB_HST_DAT0),
  83. inb_p(SMB_HST_DAT1));
  84. if (timeout && !(data & HST_STS_BAD))
  85. return 0;
  86. if (!timeout) {
  87. dev_err(&a->dev, "Timeout - Trying to KILL transaction!\n");
  88. /* Issue 'kill' to host controller */
  89. outb_p(HST_CNTL2_KILL,SMB_HST_CNTL2);
  90. data = inb_p(SMB_HST_STS);
  91. status = -ETIMEDOUT;
  92. }
  93. /* device error - no response, ignore the autodetection case */
  94. if (data & HST_STS_DEVERR) {
  95. if (size != HST_CNTL2_QUICK)
  96. dev_err(&a->dev, "Device error!\n");
  97. status = -ENXIO;
  98. }
  99. /* bus collision */
  100. if (data & HST_STS_BUSERR) {
  101. dev_err(&a->dev, "Bus collision!\n");
  102. /* Issue timeout, hoping it helps */
  103. outb_p(HST_CNTL1_TIMEOUT,SMB_HST_CNTL1);
  104. }
  105. if (data & HST_STS_FAIL) {
  106. dev_err(&a->dev, "Cleaning fail after KILL!\n");
  107. outb_p(0x0,SMB_HST_CNTL2);
  108. }
  109. return status;
  110. }
  111. static int ali1563_block_start(struct i2c_adapter * a)
  112. {
  113. u32 data;
  114. int timeout;
  115. int status = -EIO;
  116. dev_dbg(&a->dev, "Block (pre): STS=%02x, CNTL1=%02x, "
  117. "CNTL2=%02x, CMD=%02x, ADD=%02x, DAT0=%02x, DAT1=%02x\n",
  118. inb_p(SMB_HST_STS), inb_p(SMB_HST_CNTL1), inb_p(SMB_HST_CNTL2),
  119. inb_p(SMB_HST_CMD), inb_p(SMB_HST_ADD), inb_p(SMB_HST_DAT0),
  120. inb_p(SMB_HST_DAT1));
  121. data = inb_p(SMB_HST_STS);
  122. if (data & HST_STS_BAD) {
  123. dev_warn(&a->dev,"ali1563: Trying to reset busy device\n");
  124. outb_p(data | HST_STS_BAD,SMB_HST_STS);
  125. data = inb_p(SMB_HST_STS);
  126. if (data & HST_STS_BAD)
  127. return -EBUSY;
  128. }
  129. /* Clear byte-ready bit */
  130. outb_p(data | HST_STS_DONE, SMB_HST_STS);
  131. /* Start transaction and wait for byte-ready bit to be set */
  132. outb_p(inb_p(SMB_HST_CNTL2) | HST_CNTL2_START, SMB_HST_CNTL2);
  133. timeout = ALI1563_MAX_TIMEOUT;
  134. do {
  135. msleep(1);
  136. } while (!((data = inb_p(SMB_HST_STS)) & HST_STS_DONE) && --timeout);
  137. dev_dbg(&a->dev, "Block (post): STS=%02x, CNTL1=%02x, "
  138. "CNTL2=%02x, CMD=%02x, ADD=%02x, DAT0=%02x, DAT1=%02x\n",
  139. inb_p(SMB_HST_STS), inb_p(SMB_HST_CNTL1), inb_p(SMB_HST_CNTL2),
  140. inb_p(SMB_HST_CMD), inb_p(SMB_HST_ADD), inb_p(SMB_HST_DAT0),
  141. inb_p(SMB_HST_DAT1));
  142. if (timeout && !(data & HST_STS_BAD))
  143. return 0;
  144. if (timeout == 0)
  145. status = -ETIMEDOUT;
  146. if (data & HST_STS_DEVERR)
  147. status = -ENXIO;
  148. dev_err(&a->dev, "SMBus Error: %s%s%s%s%s\n",
  149. timeout ? "" : "Timeout ",
  150. data & HST_STS_FAIL ? "Transaction Failed " : "",
  151. data & HST_STS_BUSERR ? "No response or Bus Collision " : "",
  152. data & HST_STS_DEVERR ? "Device Error " : "",
  153. !(data & HST_STS_DONE) ? "Transaction Never Finished " : "");
  154. return status;
  155. }
  156. static int ali1563_block(struct i2c_adapter * a, union i2c_smbus_data * data, u8 rw)
  157. {
  158. int i, len;
  159. int error = 0;
  160. /* Do we need this? */
  161. outb_p(HST_CNTL1_LAST,SMB_HST_CNTL1);
  162. if (rw == I2C_SMBUS_WRITE) {
  163. len = data->block[0];
  164. if (len < 1)
  165. len = 1;
  166. else if (len > 32)
  167. len = 32;
  168. outb_p(len,SMB_HST_DAT0);
  169. outb_p(data->block[1],SMB_BLK_DAT);
  170. } else
  171. len = 32;
  172. outb_p(inb_p(SMB_HST_CNTL2) | HST_CNTL2_BLOCK, SMB_HST_CNTL2);
  173. for (i = 0; i < len; i++) {
  174. if (rw == I2C_SMBUS_WRITE) {
  175. outb_p(data->block[i + 1], SMB_BLK_DAT);
  176. if ((error = ali1563_block_start(a)))
  177. break;
  178. } else {
  179. if ((error = ali1563_block_start(a)))
  180. break;
  181. if (i == 0) {
  182. len = inb_p(SMB_HST_DAT0);
  183. if (len < 1)
  184. len = 1;
  185. else if (len > 32)
  186. len = 32;
  187. }
  188. data->block[i+1] = inb_p(SMB_BLK_DAT);
  189. }
  190. }
  191. /* Do we need this? */
  192. outb_p(HST_CNTL1_LAST,SMB_HST_CNTL1);
  193. return error;
  194. }
  195. static s32 ali1563_access(struct i2c_adapter * a, u16 addr,
  196. unsigned short flags, char rw, u8 cmd,
  197. int size, union i2c_smbus_data * data)
  198. {
  199. int error = 0;
  200. int timeout;
  201. u32 reg;
  202. for (timeout = ALI1563_MAX_TIMEOUT; timeout; timeout--) {
  203. if (!(reg = inb_p(SMB_HST_STS) & HST_STS_BUSY))
  204. break;
  205. }
  206. if (!timeout)
  207. dev_warn(&a->dev,"SMBus not idle. HST_STS = %02x\n",reg);
  208. outb_p(0xff,SMB_HST_STS);
  209. /* Map the size to what the chip understands */
  210. switch (size) {
  211. case I2C_SMBUS_QUICK:
  212. size = HST_CNTL2_QUICK;
  213. break;
  214. case I2C_SMBUS_BYTE:
  215. size = HST_CNTL2_BYTE;
  216. break;
  217. case I2C_SMBUS_BYTE_DATA:
  218. size = HST_CNTL2_BYTE_DATA;
  219. break;
  220. case I2C_SMBUS_WORD_DATA:
  221. size = HST_CNTL2_WORD_DATA;
  222. break;
  223. case I2C_SMBUS_BLOCK_DATA:
  224. size = HST_CNTL2_BLOCK;
  225. break;
  226. default:
  227. dev_warn(&a->dev, "Unsupported transaction %d\n", size);
  228. error = -EOPNOTSUPP;
  229. goto Done;
  230. }
  231. outb_p(((addr & 0x7f) << 1) | (rw & 0x01), SMB_HST_ADD);
  232. outb_p((inb_p(SMB_HST_CNTL2) & ~HST_CNTL2_SIZEMASK) | (size << 3), SMB_HST_CNTL2);
  233. /* Write the command register */
  234. switch(size) {
  235. case HST_CNTL2_BYTE:
  236. if (rw== I2C_SMBUS_WRITE)
  237. /* Beware it uses DAT0 register and not CMD! */
  238. outb_p(cmd, SMB_HST_DAT0);
  239. break;
  240. case HST_CNTL2_BYTE_DATA:
  241. outb_p(cmd, SMB_HST_CMD);
  242. if (rw == I2C_SMBUS_WRITE)
  243. outb_p(data->byte, SMB_HST_DAT0);
  244. break;
  245. case HST_CNTL2_WORD_DATA:
  246. outb_p(cmd, SMB_HST_CMD);
  247. if (rw == I2C_SMBUS_WRITE) {
  248. outb_p(data->word & 0xff, SMB_HST_DAT0);
  249. outb_p((data->word & 0xff00) >> 8, SMB_HST_DAT1);
  250. }
  251. break;
  252. case HST_CNTL2_BLOCK:
  253. outb_p(cmd, SMB_HST_CMD);
  254. error = ali1563_block(a,data,rw);
  255. goto Done;
  256. }
  257. if ((error = ali1563_transaction(a, size)))
  258. goto Done;
  259. if ((rw == I2C_SMBUS_WRITE) || (size == HST_CNTL2_QUICK))
  260. goto Done;
  261. switch (size) {
  262. case HST_CNTL2_BYTE: /* Result put in SMBHSTDAT0 */
  263. data->byte = inb_p(SMB_HST_DAT0);
  264. break;
  265. case HST_CNTL2_BYTE_DATA:
  266. data->byte = inb_p(SMB_HST_DAT0);
  267. break;
  268. case HST_CNTL2_WORD_DATA:
  269. data->word = inb_p(SMB_HST_DAT0) + (inb_p(SMB_HST_DAT1) << 8);
  270. break;
  271. }
  272. Done:
  273. return error;
  274. }
  275. static u32 ali1563_func(struct i2c_adapter * a)
  276. {
  277. return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
  278. I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
  279. I2C_FUNC_SMBUS_BLOCK_DATA;
  280. }
  281. static int __devinit ali1563_setup(struct pci_dev * dev)
  282. {
  283. u16 ctrl;
  284. pci_read_config_word(dev,ALI1563_SMBBA,&ctrl);
  285. /* SMB I/O Base in high 12 bits and must be aligned with the
  286. * size of the I/O space. */
  287. ali1563_smba = ctrl & ~(ALI1563_SMB_IOSIZE - 1);
  288. if (!ali1563_smba) {
  289. dev_warn(&dev->dev,"ali1563_smba Uninitialized\n");
  290. goto Err;
  291. }
  292. /* Check if device is enabled */
  293. if (!(ctrl & ALI1563_SMB_HOSTEN)) {
  294. dev_warn(&dev->dev, "Host Controller not enabled\n");
  295. goto Err;
  296. }
  297. if (!(ctrl & ALI1563_SMB_IOEN)) {
  298. dev_warn(&dev->dev, "I/O space not enabled, trying manually\n");
  299. pci_write_config_word(dev, ALI1563_SMBBA,
  300. ctrl | ALI1563_SMB_IOEN);
  301. pci_read_config_word(dev, ALI1563_SMBBA, &ctrl);
  302. if (!(ctrl & ALI1563_SMB_IOEN)) {
  303. dev_err(&dev->dev, "I/O space still not enabled, "
  304. "giving up\n");
  305. goto Err;
  306. }
  307. }
  308. if (acpi_check_region(ali1563_smba, ALI1563_SMB_IOSIZE,
  309. ali1563_pci_driver.name))
  310. goto Err;
  311. if (!request_region(ali1563_smba, ALI1563_SMB_IOSIZE,
  312. ali1563_pci_driver.name)) {
  313. dev_err(&dev->dev, "Could not allocate I/O space at 0x%04x\n",
  314. ali1563_smba);
  315. goto Err;
  316. }
  317. dev_info(&dev->dev, "Found ALi1563 SMBus at 0x%04x\n", ali1563_smba);
  318. return 0;
  319. Err:
  320. return -ENODEV;
  321. }
  322. static void ali1563_shutdown(struct pci_dev *dev)
  323. {
  324. release_region(ali1563_smba,ALI1563_SMB_IOSIZE);
  325. }
  326. static const struct i2c_algorithm ali1563_algorithm = {
  327. .smbus_xfer = ali1563_access,
  328. .functionality = ali1563_func,
  329. };
  330. static struct i2c_adapter ali1563_adapter = {
  331. .owner = THIS_MODULE,
  332. .class = I2C_CLASS_HWMON | I2C_CLASS_SPD,
  333. .algo = &ali1563_algorithm,
  334. };
  335. static int __devinit ali1563_probe(struct pci_dev * dev,
  336. const struct pci_device_id * id_table)
  337. {
  338. int error;
  339. if ((error = ali1563_setup(dev)))
  340. goto exit;
  341. ali1563_adapter.dev.parent = &dev->dev;
  342. snprintf(ali1563_adapter.name, sizeof(ali1563_adapter.name),
  343. "SMBus ALi 1563 Adapter @ %04x", ali1563_smba);
  344. if ((error = i2c_add_adapter(&ali1563_adapter)))
  345. goto exit_shutdown;
  346. return 0;
  347. exit_shutdown:
  348. ali1563_shutdown(dev);
  349. exit:
  350. dev_warn(&dev->dev, "ALi1563 SMBus probe failed (%d)\n", error);
  351. return error;
  352. }
  353. static void __devexit ali1563_remove(struct pci_dev * dev)
  354. {
  355. i2c_del_adapter(&ali1563_adapter);
  356. ali1563_shutdown(dev);
  357. }
  358. static DEFINE_PCI_DEVICE_TABLE(ali1563_id_table) = {
  359. { PCI_DEVICE(PCI_VENDOR_ID_AL, PCI_DEVICE_ID_AL_M1563) },
  360. {},
  361. };
  362. MODULE_DEVICE_TABLE (pci, ali1563_id_table);
  363. static struct pci_driver ali1563_pci_driver = {
  364. .name = "ali1563_smbus",
  365. .id_table = ali1563_id_table,
  366. .probe = ali1563_probe,
  367. .remove = __devexit_p(ali1563_remove),
  368. };
  369. static int __init ali1563_init(void)
  370. {
  371. return pci_register_driver(&ali1563_pci_driver);
  372. }
  373. module_init(ali1563_init);
  374. static void __exit ali1563_exit(void)
  375. {
  376. pci_unregister_driver(&ali1563_pci_driver);
  377. }
  378. module_exit(ali1563_exit);
  379. MODULE_LICENSE("GPL");