i2c-ali1563.c 11 KB

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