i2c-mlxcpld.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. /*
  2. * Copyright (c) 2016 Mellanox Technologies. All rights reserved.
  3. * Copyright (c) 2016 Michael Shych <michaels@mellanox.com>
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the names of the copyright holders nor the names of its
  14. * contributors may be used to endorse or promote products derived from
  15. * this software without specific prior written permission.
  16. *
  17. * Alternatively, this software may be distributed under the terms of the
  18. * GNU General Public License ("GPL") version 2 as published by the Free
  19. * Software Foundation.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  22. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  25. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  26. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  27. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  28. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  29. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  30. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  31. * POSSIBILITY OF SUCH DAMAGE.
  32. */
  33. #include <linux/delay.h>
  34. #include <linux/i2c.h>
  35. #include <linux/init.h>
  36. #include <linux/io.h>
  37. #include <linux/kernel.h>
  38. #include <linux/module.h>
  39. #include <linux/platform_device.h>
  40. /* General defines */
  41. #define MLXPLAT_CPLD_LPC_I2C_BASE_ADDR 0x2000
  42. #define MLXCPLD_I2C_DEVICE_NAME "i2c_mlxcpld"
  43. #define MLXCPLD_I2C_VALID_FLAG (I2C_M_RECV_LEN | I2C_M_RD)
  44. #define MLXCPLD_I2C_BUS_NUM 1
  45. #define MLXCPLD_I2C_DATA_REG_SZ 36
  46. #define MLXCPLD_I2C_MAX_ADDR_LEN 4
  47. #define MLXCPLD_I2C_RETR_NUM 2
  48. #define MLXCPLD_I2C_XFER_TO 500000 /* usec */
  49. #define MLXCPLD_I2C_POLL_TIME 2000 /* usec */
  50. /* LPC I2C registers */
  51. #define MLXCPLD_LPCI2C_LPF_REG 0x0
  52. #define MLXCPLD_LPCI2C_CTRL_REG 0x1
  53. #define MLXCPLD_LPCI2C_HALF_CYC_REG 0x4
  54. #define MLXCPLD_LPCI2C_I2C_HOLD_REG 0x5
  55. #define MLXCPLD_LPCI2C_CMD_REG 0x6
  56. #define MLXCPLD_LPCI2C_NUM_DAT_REG 0x7
  57. #define MLXCPLD_LPCI2C_NUM_ADDR_REG 0x8
  58. #define MLXCPLD_LPCI2C_STATUS_REG 0x9
  59. #define MLXCPLD_LPCI2C_DATA_REG 0xa
  60. /* LPC I2C masks and parametres */
  61. #define MLXCPLD_LPCI2C_RST_SEL_MASK 0x1
  62. #define MLXCPLD_LPCI2C_TRANS_END 0x1
  63. #define MLXCPLD_LPCI2C_STATUS_NACK 0x10
  64. #define MLXCPLD_LPCI2C_NO_IND 0
  65. #define MLXCPLD_LPCI2C_ACK_IND 1
  66. #define MLXCPLD_LPCI2C_NACK_IND 2
  67. struct mlxcpld_i2c_curr_xfer {
  68. u8 cmd;
  69. u8 addr_width;
  70. u8 data_len;
  71. u8 msg_num;
  72. struct i2c_msg *msg;
  73. };
  74. struct mlxcpld_i2c_priv {
  75. struct i2c_adapter adap;
  76. u32 base_addr;
  77. struct mutex lock;
  78. struct mlxcpld_i2c_curr_xfer xfer;
  79. struct device *dev;
  80. };
  81. static void mlxcpld_i2c_lpc_write_buf(u8 *data, u8 len, u32 addr)
  82. {
  83. int i;
  84. for (i = 0; i < len - len % 4; i += 4)
  85. outl(*(u32 *)(data + i), addr + i);
  86. for (; i < len; ++i)
  87. outb(*(data + i), addr + i);
  88. }
  89. static void mlxcpld_i2c_lpc_read_buf(u8 *data, u8 len, u32 addr)
  90. {
  91. int i;
  92. for (i = 0; i < len - len % 4; i += 4)
  93. *(u32 *)(data + i) = inl(addr + i);
  94. for (; i < len; ++i)
  95. *(data + i) = inb(addr + i);
  96. }
  97. static void mlxcpld_i2c_read_comm(struct mlxcpld_i2c_priv *priv, u8 offs,
  98. u8 *data, u8 datalen)
  99. {
  100. u32 addr = priv->base_addr + offs;
  101. switch (datalen) {
  102. case 1:
  103. *(data) = inb(addr);
  104. break;
  105. case 2:
  106. *((u16 *)data) = inw(addr);
  107. break;
  108. case 3:
  109. *((u16 *)data) = inw(addr);
  110. *(data + 2) = inb(addr + 2);
  111. break;
  112. case 4:
  113. *((u32 *)data) = inl(addr);
  114. break;
  115. default:
  116. mlxcpld_i2c_lpc_read_buf(data, datalen, addr);
  117. break;
  118. }
  119. }
  120. static void mlxcpld_i2c_write_comm(struct mlxcpld_i2c_priv *priv, u8 offs,
  121. u8 *data, u8 datalen)
  122. {
  123. u32 addr = priv->base_addr + offs;
  124. switch (datalen) {
  125. case 1:
  126. outb(*(data), addr);
  127. break;
  128. case 2:
  129. outw(*((u16 *)data), addr);
  130. break;
  131. case 3:
  132. outw(*((u16 *)data), addr);
  133. outb(*(data + 2), addr + 2);
  134. break;
  135. case 4:
  136. outl(*((u32 *)data), addr);
  137. break;
  138. default:
  139. mlxcpld_i2c_lpc_write_buf(data, datalen, addr);
  140. break;
  141. }
  142. }
  143. /*
  144. * Check validity of received i2c messages parameters.
  145. * Returns 0 if OK, other - in case of invalid parameters.
  146. */
  147. static int mlxcpld_i2c_check_msg_params(struct mlxcpld_i2c_priv *priv,
  148. struct i2c_msg *msgs, int num)
  149. {
  150. int i;
  151. if (!num) {
  152. dev_err(priv->dev, "Incorrect 0 num of messages\n");
  153. return -EINVAL;
  154. }
  155. if (unlikely(msgs[0].addr > 0x7f)) {
  156. dev_err(priv->dev, "Invalid address 0x%03x\n",
  157. msgs[0].addr);
  158. return -EINVAL;
  159. }
  160. for (i = 0; i < num; ++i) {
  161. if (unlikely(!msgs[i].buf)) {
  162. dev_err(priv->dev, "Invalid buf in msg[%d]\n",
  163. i);
  164. return -EINVAL;
  165. }
  166. if (unlikely(msgs[0].addr != msgs[i].addr)) {
  167. dev_err(priv->dev, "Invalid addr in msg[%d]\n",
  168. i);
  169. return -EINVAL;
  170. }
  171. }
  172. return 0;
  173. }
  174. /*
  175. * Check if transfer is completed and status of operation.
  176. * Returns 0 - transfer completed (both ACK or NACK),
  177. * negative - transfer isn't finished.
  178. */
  179. static int mlxcpld_i2c_check_status(struct mlxcpld_i2c_priv *priv, int *status)
  180. {
  181. u8 val;
  182. mlxcpld_i2c_read_comm(priv, MLXCPLD_LPCI2C_STATUS_REG, &val, 1);
  183. if (val & MLXCPLD_LPCI2C_TRANS_END) {
  184. if (val & MLXCPLD_LPCI2C_STATUS_NACK)
  185. /*
  186. * The slave is unable to accept the data. No such
  187. * slave, command not understood, or unable to accept
  188. * any more data.
  189. */
  190. *status = MLXCPLD_LPCI2C_NACK_IND;
  191. else
  192. *status = MLXCPLD_LPCI2C_ACK_IND;
  193. return 0;
  194. }
  195. *status = MLXCPLD_LPCI2C_NO_IND;
  196. return -EIO;
  197. }
  198. static void mlxcpld_i2c_set_transf_data(struct mlxcpld_i2c_priv *priv,
  199. struct i2c_msg *msgs, int num,
  200. u8 comm_len)
  201. {
  202. priv->xfer.msg = msgs;
  203. priv->xfer.msg_num = num;
  204. /*
  205. * All upper layers currently are never use transfer with more than
  206. * 2 messages. Actually, it's also not so relevant in Mellanox systems
  207. * because of HW limitation. Max size of transfer is not more than 32
  208. * bytes in the current x86 LPCI2C bridge.
  209. */
  210. priv->xfer.cmd = msgs[num - 1].flags & I2C_M_RD;
  211. if (priv->xfer.cmd == I2C_M_RD && comm_len != msgs[0].len) {
  212. priv->xfer.addr_width = msgs[0].len;
  213. priv->xfer.data_len = comm_len - priv->xfer.addr_width;
  214. } else {
  215. priv->xfer.addr_width = 0;
  216. priv->xfer.data_len = comm_len;
  217. }
  218. }
  219. /* Reset CPLD LPCI2C block */
  220. static void mlxcpld_i2c_reset(struct mlxcpld_i2c_priv *priv)
  221. {
  222. u8 val;
  223. mutex_lock(&priv->lock);
  224. mlxcpld_i2c_read_comm(priv, MLXCPLD_LPCI2C_CTRL_REG, &val, 1);
  225. val &= ~MLXCPLD_LPCI2C_RST_SEL_MASK;
  226. mlxcpld_i2c_write_comm(priv, MLXCPLD_LPCI2C_CTRL_REG, &val, 1);
  227. mutex_unlock(&priv->lock);
  228. }
  229. /* Make sure the CPLD is ready to start transmitting. */
  230. static int mlxcpld_i2c_check_busy(struct mlxcpld_i2c_priv *priv)
  231. {
  232. u8 val;
  233. mlxcpld_i2c_read_comm(priv, MLXCPLD_LPCI2C_STATUS_REG, &val, 1);
  234. if (val & MLXCPLD_LPCI2C_TRANS_END)
  235. return 0;
  236. return -EIO;
  237. }
  238. static int mlxcpld_i2c_wait_for_free(struct mlxcpld_i2c_priv *priv)
  239. {
  240. int timeout = 0;
  241. do {
  242. if (!mlxcpld_i2c_check_busy(priv))
  243. break;
  244. usleep_range(MLXCPLD_I2C_POLL_TIME / 2, MLXCPLD_I2C_POLL_TIME);
  245. timeout += MLXCPLD_I2C_POLL_TIME;
  246. } while (timeout <= MLXCPLD_I2C_XFER_TO);
  247. if (timeout > MLXCPLD_I2C_XFER_TO)
  248. return -ETIMEDOUT;
  249. return 0;
  250. }
  251. /*
  252. * Wait for master transfer to complete.
  253. * It puts current process to sleep until we get interrupt or timeout expires.
  254. * Returns the number of transferred or read bytes or error (<0).
  255. */
  256. static int mlxcpld_i2c_wait_for_tc(struct mlxcpld_i2c_priv *priv)
  257. {
  258. int status, i, timeout = 0;
  259. u8 datalen;
  260. do {
  261. usleep_range(MLXCPLD_I2C_POLL_TIME / 2, MLXCPLD_I2C_POLL_TIME);
  262. if (!mlxcpld_i2c_check_status(priv, &status))
  263. break;
  264. timeout += MLXCPLD_I2C_POLL_TIME;
  265. } while (status == 0 && timeout < MLXCPLD_I2C_XFER_TO);
  266. switch (status) {
  267. case MLXCPLD_LPCI2C_NO_IND:
  268. return -ETIMEDOUT;
  269. case MLXCPLD_LPCI2C_ACK_IND:
  270. if (priv->xfer.cmd != I2C_M_RD)
  271. return (priv->xfer.addr_width + priv->xfer.data_len);
  272. if (priv->xfer.msg_num == 1)
  273. i = 0;
  274. else
  275. i = 1;
  276. if (!priv->xfer.msg[i].buf)
  277. return -EINVAL;
  278. /*
  279. * Actual read data len will be always the same as
  280. * requested len. 0xff (line pull-up) will be returned
  281. * if slave has no data to return. Thus don't read
  282. * MLXCPLD_LPCI2C_NUM_DAT_REG reg from CPLD.
  283. */
  284. datalen = priv->xfer.data_len;
  285. mlxcpld_i2c_read_comm(priv, MLXCPLD_LPCI2C_DATA_REG,
  286. priv->xfer.msg[i].buf, datalen);
  287. return datalen;
  288. case MLXCPLD_LPCI2C_NACK_IND:
  289. return -ENXIO;
  290. default:
  291. return -EINVAL;
  292. }
  293. }
  294. static void mlxcpld_i2c_xfer_msg(struct mlxcpld_i2c_priv *priv)
  295. {
  296. int i, len = 0;
  297. u8 cmd;
  298. mlxcpld_i2c_write_comm(priv, MLXCPLD_LPCI2C_NUM_DAT_REG,
  299. &priv->xfer.data_len, 1);
  300. mlxcpld_i2c_write_comm(priv, MLXCPLD_LPCI2C_NUM_ADDR_REG,
  301. &priv->xfer.addr_width, 1);
  302. for (i = 0; i < priv->xfer.msg_num; i++) {
  303. if ((priv->xfer.msg[i].flags & I2C_M_RD) != I2C_M_RD) {
  304. /* Don't write to CPLD buffer in read transaction */
  305. mlxcpld_i2c_write_comm(priv, MLXCPLD_LPCI2C_DATA_REG +
  306. len, priv->xfer.msg[i].buf,
  307. priv->xfer.msg[i].len);
  308. len += priv->xfer.msg[i].len;
  309. }
  310. }
  311. /*
  312. * Set target slave address with command for master transfer.
  313. * It should be latest executed function before CPLD transaction.
  314. */
  315. cmd = (priv->xfer.msg[0].addr << 1) | priv->xfer.cmd;
  316. mlxcpld_i2c_write_comm(priv, MLXCPLD_LPCI2C_CMD_REG, &cmd, 1);
  317. }
  318. /*
  319. * Generic lpc-i2c transfer.
  320. * Returns the number of processed messages or error (<0).
  321. */
  322. static int mlxcpld_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs,
  323. int num)
  324. {
  325. struct mlxcpld_i2c_priv *priv = i2c_get_adapdata(adap);
  326. u8 comm_len = 0;
  327. int i, err;
  328. err = mlxcpld_i2c_check_msg_params(priv, msgs, num);
  329. if (err) {
  330. dev_err(priv->dev, "Incorrect message\n");
  331. return err;
  332. }
  333. for (i = 0; i < num; ++i)
  334. comm_len += msgs[i].len;
  335. /* Check bus state */
  336. if (mlxcpld_i2c_wait_for_free(priv)) {
  337. dev_err(priv->dev, "LPCI2C bridge is busy\n");
  338. /*
  339. * Usually it means something serious has happened.
  340. * We can not have unfinished previous transfer
  341. * so it doesn't make any sense to try to stop it.
  342. * Probably we were not able to recover from the
  343. * previous error.
  344. * The only reasonable thing - is soft reset.
  345. */
  346. mlxcpld_i2c_reset(priv);
  347. if (mlxcpld_i2c_check_busy(priv)) {
  348. dev_err(priv->dev, "LPCI2C bridge is busy after reset\n");
  349. return -EIO;
  350. }
  351. }
  352. mlxcpld_i2c_set_transf_data(priv, msgs, num, comm_len);
  353. mutex_lock(&priv->lock);
  354. /* Do real transfer. Can't fail */
  355. mlxcpld_i2c_xfer_msg(priv);
  356. /* Wait for transaction complete */
  357. err = mlxcpld_i2c_wait_for_tc(priv);
  358. mutex_unlock(&priv->lock);
  359. return err < 0 ? err : num;
  360. }
  361. static u32 mlxcpld_i2c_func(struct i2c_adapter *adap)
  362. {
  363. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL | I2C_FUNC_SMBUS_BLOCK_DATA;
  364. }
  365. static const struct i2c_algorithm mlxcpld_i2c_algo = {
  366. .master_xfer = mlxcpld_i2c_xfer,
  367. .functionality = mlxcpld_i2c_func
  368. };
  369. static struct i2c_adapter_quirks mlxcpld_i2c_quirks = {
  370. .flags = I2C_AQ_COMB_WRITE_THEN_READ,
  371. .max_read_len = MLXCPLD_I2C_DATA_REG_SZ - MLXCPLD_I2C_MAX_ADDR_LEN,
  372. .max_write_len = MLXCPLD_I2C_DATA_REG_SZ,
  373. .max_comb_1st_msg_len = 4,
  374. };
  375. static struct i2c_adapter mlxcpld_i2c_adapter = {
  376. .owner = THIS_MODULE,
  377. .name = "i2c-mlxcpld",
  378. .class = I2C_CLASS_HWMON | I2C_CLASS_SPD,
  379. .algo = &mlxcpld_i2c_algo,
  380. .quirks = &mlxcpld_i2c_quirks,
  381. .retries = MLXCPLD_I2C_RETR_NUM,
  382. .nr = MLXCPLD_I2C_BUS_NUM,
  383. };
  384. static int mlxcpld_i2c_probe(struct platform_device *pdev)
  385. {
  386. struct mlxcpld_i2c_priv *priv;
  387. int err;
  388. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  389. if (!priv)
  390. return -ENOMEM;
  391. mutex_init(&priv->lock);
  392. platform_set_drvdata(pdev, priv);
  393. priv->dev = &pdev->dev;
  394. /* Register with i2c layer */
  395. mlxcpld_i2c_adapter.timeout = usecs_to_jiffies(MLXCPLD_I2C_XFER_TO);
  396. priv->adap = mlxcpld_i2c_adapter;
  397. priv->adap.dev.parent = &pdev->dev;
  398. priv->base_addr = MLXPLAT_CPLD_LPC_I2C_BASE_ADDR;
  399. i2c_set_adapdata(&priv->adap, priv);
  400. err = i2c_add_numbered_adapter(&priv->adap);
  401. if (err)
  402. mutex_destroy(&priv->lock);
  403. return err;
  404. }
  405. static int mlxcpld_i2c_remove(struct platform_device *pdev)
  406. {
  407. struct mlxcpld_i2c_priv *priv = platform_get_drvdata(pdev);
  408. i2c_del_adapter(&priv->adap);
  409. mutex_destroy(&priv->lock);
  410. return 0;
  411. }
  412. static struct platform_driver mlxcpld_i2c_driver = {
  413. .probe = mlxcpld_i2c_probe,
  414. .remove = mlxcpld_i2c_remove,
  415. .driver = {
  416. .name = MLXCPLD_I2C_DEVICE_NAME,
  417. },
  418. };
  419. module_platform_driver(mlxcpld_i2c_driver);
  420. MODULE_AUTHOR("Michael Shych <michaels@mellanox.com>");
  421. MODULE_DESCRIPTION("Mellanox I2C-CPLD controller driver");
  422. MODULE_LICENSE("Dual BSD/GPL");
  423. MODULE_ALIAS("platform:i2c-mlxcpld");