i2c-ali1563.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  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,
  156. 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. error = ali1563_block_start(a);
  177. if (error)
  178. break;
  179. } else {
  180. error = ali1563_block_start(a);
  181. if (error)
  182. break;
  183. if (i == 0) {
  184. len = inb_p(SMB_HST_DAT0);
  185. if (len < 1)
  186. len = 1;
  187. else if (len > 32)
  188. len = 32;
  189. }
  190. data->block[i+1] = inb_p(SMB_BLK_DAT);
  191. }
  192. }
  193. /* Do we need this? */
  194. outb_p(HST_CNTL1_LAST, SMB_HST_CNTL1);
  195. return error;
  196. }
  197. static s32 ali1563_access(struct i2c_adapter *a, u16 addr,
  198. unsigned short flags, char rw, u8 cmd,
  199. int size, union i2c_smbus_data *data)
  200. {
  201. int error = 0;
  202. int timeout;
  203. u32 reg;
  204. for (timeout = ALI1563_MAX_TIMEOUT; timeout; timeout--) {
  205. reg = inb_p(SMB_HST_STS);
  206. if (!(reg & HST_STS_BUSY))
  207. break;
  208. }
  209. if (!timeout)
  210. dev_warn(&a->dev, "SMBus not idle. HST_STS = %02x\n", reg);
  211. outb_p(0xff, SMB_HST_STS);
  212. /* Map the size to what the chip understands */
  213. switch (size) {
  214. case I2C_SMBUS_QUICK:
  215. size = HST_CNTL2_QUICK;
  216. break;
  217. case I2C_SMBUS_BYTE:
  218. size = HST_CNTL2_BYTE;
  219. break;
  220. case I2C_SMBUS_BYTE_DATA:
  221. size = HST_CNTL2_BYTE_DATA;
  222. break;
  223. case I2C_SMBUS_WORD_DATA:
  224. size = HST_CNTL2_WORD_DATA;
  225. break;
  226. case I2C_SMBUS_BLOCK_DATA:
  227. size = HST_CNTL2_BLOCK;
  228. break;
  229. default:
  230. dev_warn(&a->dev, "Unsupported transaction %d\n", size);
  231. error = -EOPNOTSUPP;
  232. goto Done;
  233. }
  234. outb_p(((addr & 0x7f) << 1) | (rw & 0x01), SMB_HST_ADD);
  235. outb_p((inb_p(SMB_HST_CNTL2) & ~HST_CNTL2_SIZEMASK) |
  236. (size << 3), SMB_HST_CNTL2);
  237. /* Write the command register */
  238. switch (size) {
  239. case HST_CNTL2_BYTE:
  240. if (rw == I2C_SMBUS_WRITE)
  241. /* Beware it uses DAT0 register and not CMD! */
  242. outb_p(cmd, SMB_HST_DAT0);
  243. break;
  244. case HST_CNTL2_BYTE_DATA:
  245. outb_p(cmd, SMB_HST_CMD);
  246. if (rw == I2C_SMBUS_WRITE)
  247. outb_p(data->byte, SMB_HST_DAT0);
  248. break;
  249. case HST_CNTL2_WORD_DATA:
  250. outb_p(cmd, SMB_HST_CMD);
  251. if (rw == I2C_SMBUS_WRITE) {
  252. outb_p(data->word & 0xff, SMB_HST_DAT0);
  253. outb_p((data->word & 0xff00) >> 8, SMB_HST_DAT1);
  254. }
  255. break;
  256. case HST_CNTL2_BLOCK:
  257. outb_p(cmd, SMB_HST_CMD);
  258. error = ali1563_block(a, data, rw);
  259. goto Done;
  260. }
  261. error = ali1563_transaction(a, size);
  262. if (error)
  263. goto Done;
  264. if ((rw == I2C_SMBUS_WRITE) || (size == HST_CNTL2_QUICK))
  265. goto Done;
  266. switch (size) {
  267. case HST_CNTL2_BYTE: /* Result put in SMBHSTDAT0 */
  268. data->byte = inb_p(SMB_HST_DAT0);
  269. break;
  270. case HST_CNTL2_BYTE_DATA:
  271. data->byte = inb_p(SMB_HST_DAT0);
  272. break;
  273. case HST_CNTL2_WORD_DATA:
  274. data->word = inb_p(SMB_HST_DAT0) + (inb_p(SMB_HST_DAT1) << 8);
  275. break;
  276. }
  277. Done:
  278. return error;
  279. }
  280. static u32 ali1563_func(struct i2c_adapter *a)
  281. {
  282. return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
  283. I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
  284. I2C_FUNC_SMBUS_BLOCK_DATA;
  285. }
  286. static int ali1563_setup(struct pci_dev *dev)
  287. {
  288. u16 ctrl;
  289. pci_read_config_word(dev, ALI1563_SMBBA, &ctrl);
  290. /* SMB I/O Base in high 12 bits and must be aligned with the
  291. * size of the I/O space. */
  292. ali1563_smba = ctrl & ~(ALI1563_SMB_IOSIZE - 1);
  293. if (!ali1563_smba) {
  294. dev_warn(&dev->dev, "ali1563_smba Uninitialized\n");
  295. goto Err;
  296. }
  297. /* Check if device is enabled */
  298. if (!(ctrl & ALI1563_SMB_HOSTEN)) {
  299. dev_warn(&dev->dev, "Host Controller not enabled\n");
  300. goto Err;
  301. }
  302. if (!(ctrl & ALI1563_SMB_IOEN)) {
  303. dev_warn(&dev->dev, "I/O space not enabled, trying manually\n");
  304. pci_write_config_word(dev, ALI1563_SMBBA,
  305. ctrl | ALI1563_SMB_IOEN);
  306. pci_read_config_word(dev, ALI1563_SMBBA, &ctrl);
  307. if (!(ctrl & ALI1563_SMB_IOEN)) {
  308. dev_err(&dev->dev,
  309. "I/O space still not enabled, giving up\n");
  310. goto Err;
  311. }
  312. }
  313. if (acpi_check_region(ali1563_smba, ALI1563_SMB_IOSIZE,
  314. ali1563_pci_driver.name))
  315. goto Err;
  316. if (!request_region(ali1563_smba, ALI1563_SMB_IOSIZE,
  317. ali1563_pci_driver.name)) {
  318. dev_err(&dev->dev, "Could not allocate I/O space at 0x%04x\n",
  319. ali1563_smba);
  320. goto Err;
  321. }
  322. dev_info(&dev->dev, "Found ALi1563 SMBus at 0x%04x\n", ali1563_smba);
  323. return 0;
  324. Err:
  325. return -ENODEV;
  326. }
  327. static void ali1563_shutdown(struct pci_dev *dev)
  328. {
  329. release_region(ali1563_smba, ALI1563_SMB_IOSIZE);
  330. }
  331. static const struct i2c_algorithm ali1563_algorithm = {
  332. .smbus_xfer = ali1563_access,
  333. .functionality = ali1563_func,
  334. };
  335. static struct i2c_adapter ali1563_adapter = {
  336. .owner = THIS_MODULE,
  337. .class = I2C_CLASS_HWMON | I2C_CLASS_SPD,
  338. .algo = &ali1563_algorithm,
  339. };
  340. static int ali1563_probe(struct pci_dev *dev,
  341. const struct pci_device_id *id_table)
  342. {
  343. int error;
  344. error = ali1563_setup(dev);
  345. if (error)
  346. goto exit;
  347. ali1563_adapter.dev.parent = &dev->dev;
  348. snprintf(ali1563_adapter.name, sizeof(ali1563_adapter.name),
  349. "SMBus ALi 1563 Adapter @ %04x", ali1563_smba);
  350. error = i2c_add_adapter(&ali1563_adapter);
  351. if (error)
  352. goto exit_shutdown;
  353. return 0;
  354. exit_shutdown:
  355. ali1563_shutdown(dev);
  356. exit:
  357. dev_warn(&dev->dev, "ALi1563 SMBus probe failed (%d)\n", error);
  358. return error;
  359. }
  360. static void ali1563_remove(struct pci_dev *dev)
  361. {
  362. i2c_del_adapter(&ali1563_adapter);
  363. ali1563_shutdown(dev);
  364. }
  365. static const struct pci_device_id ali1563_id_table[] = {
  366. { PCI_DEVICE(PCI_VENDOR_ID_AL, PCI_DEVICE_ID_AL_M1563) },
  367. {},
  368. };
  369. MODULE_DEVICE_TABLE(pci, ali1563_id_table);
  370. static struct pci_driver ali1563_pci_driver = {
  371. .name = "ali1563_smbus",
  372. .id_table = ali1563_id_table,
  373. .probe = ali1563_probe,
  374. .remove = ali1563_remove,
  375. };
  376. module_pci_driver(ali1563_pci_driver);
  377. MODULE_LICENSE("GPL");