i2c-ali1563.c 11 KB

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