i2c-bcm-iproc.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  1. /*
  2. * Copyright (C) 2014 Broadcom Corporation
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation version 2.
  7. *
  8. * This program is distributed "as is" WITHOUT ANY WARRANTY of any
  9. * kind, whether express or implied; without even the implied warranty
  10. * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. #include <linux/delay.h>
  14. #include <linux/i2c.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/io.h>
  17. #include <linux/kernel.h>
  18. #include <linux/module.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/slab.h>
  21. #define CFG_OFFSET 0x00
  22. #define CFG_RESET_SHIFT 31
  23. #define CFG_EN_SHIFT 30
  24. #define CFG_M_RETRY_CNT_SHIFT 16
  25. #define CFG_M_RETRY_CNT_MASK 0x0f
  26. #define TIM_CFG_OFFSET 0x04
  27. #define TIM_CFG_MODE_400_SHIFT 31
  28. #define M_FIFO_CTRL_OFFSET 0x0c
  29. #define M_FIFO_RX_FLUSH_SHIFT 31
  30. #define M_FIFO_TX_FLUSH_SHIFT 30
  31. #define M_FIFO_RX_CNT_SHIFT 16
  32. #define M_FIFO_RX_CNT_MASK 0x7f
  33. #define M_FIFO_RX_THLD_SHIFT 8
  34. #define M_FIFO_RX_THLD_MASK 0x3f
  35. #define M_CMD_OFFSET 0x30
  36. #define M_CMD_START_BUSY_SHIFT 31
  37. #define M_CMD_STATUS_SHIFT 25
  38. #define M_CMD_STATUS_MASK 0x07
  39. #define M_CMD_STATUS_SUCCESS 0x0
  40. #define M_CMD_STATUS_LOST_ARB 0x1
  41. #define M_CMD_STATUS_NACK_ADDR 0x2
  42. #define M_CMD_STATUS_NACK_DATA 0x3
  43. #define M_CMD_STATUS_TIMEOUT 0x4
  44. #define M_CMD_PROTOCOL_SHIFT 9
  45. #define M_CMD_PROTOCOL_MASK 0xf
  46. #define M_CMD_PROTOCOL_BLK_WR 0x7
  47. #define M_CMD_PROTOCOL_BLK_RD 0x8
  48. #define M_CMD_PEC_SHIFT 8
  49. #define M_CMD_RD_CNT_SHIFT 0
  50. #define M_CMD_RD_CNT_MASK 0xff
  51. #define IE_OFFSET 0x38
  52. #define IE_M_RX_FIFO_FULL_SHIFT 31
  53. #define IE_M_RX_THLD_SHIFT 30
  54. #define IE_M_START_BUSY_SHIFT 28
  55. #define IS_OFFSET 0x3c
  56. #define IS_M_RX_FIFO_FULL_SHIFT 31
  57. #define IS_M_RX_THLD_SHIFT 30
  58. #define IS_M_START_BUSY_SHIFT 28
  59. #define M_TX_OFFSET 0x40
  60. #define M_TX_WR_STATUS_SHIFT 31
  61. #define M_TX_DATA_SHIFT 0
  62. #define M_TX_DATA_MASK 0xff
  63. #define M_RX_OFFSET 0x44
  64. #define M_RX_STATUS_SHIFT 30
  65. #define M_RX_STATUS_MASK 0x03
  66. #define M_RX_PEC_ERR_SHIFT 29
  67. #define M_RX_DATA_SHIFT 0
  68. #define M_RX_DATA_MASK 0xff
  69. #define I2C_TIMEOUT_MESC 100
  70. #define M_TX_RX_FIFO_SIZE 64
  71. enum bus_speed_index {
  72. I2C_SPD_100K = 0,
  73. I2C_SPD_400K,
  74. };
  75. struct bcm_iproc_i2c_dev {
  76. struct device *device;
  77. int irq;
  78. void __iomem *base;
  79. struct i2c_adapter adapter;
  80. unsigned int bus_speed;
  81. struct completion done;
  82. int xfer_is_done;
  83. };
  84. /*
  85. * Can be expanded in the future if more interrupt status bits are utilized
  86. */
  87. #define ISR_MASK (1 << IS_M_START_BUSY_SHIFT)
  88. static irqreturn_t bcm_iproc_i2c_isr(int irq, void *data)
  89. {
  90. struct bcm_iproc_i2c_dev *iproc_i2c = data;
  91. u32 status = readl(iproc_i2c->base + IS_OFFSET);
  92. status &= ISR_MASK;
  93. if (!status)
  94. return IRQ_NONE;
  95. writel(status, iproc_i2c->base + IS_OFFSET);
  96. iproc_i2c->xfer_is_done = 1;
  97. complete_all(&iproc_i2c->done);
  98. return IRQ_HANDLED;
  99. }
  100. static int bcm_iproc_i2c_check_status(struct bcm_iproc_i2c_dev *iproc_i2c,
  101. struct i2c_msg *msg)
  102. {
  103. u32 val;
  104. val = readl(iproc_i2c->base + M_CMD_OFFSET);
  105. val = (val >> M_CMD_STATUS_SHIFT) & M_CMD_STATUS_MASK;
  106. switch (val) {
  107. case M_CMD_STATUS_SUCCESS:
  108. return 0;
  109. case M_CMD_STATUS_LOST_ARB:
  110. dev_dbg(iproc_i2c->device, "lost bus arbitration\n");
  111. return -EAGAIN;
  112. case M_CMD_STATUS_NACK_ADDR:
  113. dev_dbg(iproc_i2c->device, "NAK addr:0x%02x\n", msg->addr);
  114. return -ENXIO;
  115. case M_CMD_STATUS_NACK_DATA:
  116. dev_dbg(iproc_i2c->device, "NAK data\n");
  117. return -ENXIO;
  118. case M_CMD_STATUS_TIMEOUT:
  119. dev_dbg(iproc_i2c->device, "bus timeout\n");
  120. return -ETIMEDOUT;
  121. default:
  122. dev_dbg(iproc_i2c->device, "unknown error code=%d\n", val);
  123. return -EIO;
  124. }
  125. }
  126. static int bcm_iproc_i2c_xfer_single_msg(struct bcm_iproc_i2c_dev *iproc_i2c,
  127. struct i2c_msg *msg)
  128. {
  129. int ret, i;
  130. u8 addr;
  131. u32 val;
  132. unsigned long time_left = msecs_to_jiffies(I2C_TIMEOUT_MESC);
  133. /* check if bus is busy */
  134. if (!!(readl(iproc_i2c->base + M_CMD_OFFSET) &
  135. BIT(M_CMD_START_BUSY_SHIFT))) {
  136. dev_warn(iproc_i2c->device, "bus is busy\n");
  137. return -EBUSY;
  138. }
  139. /* format and load slave address into the TX FIFO */
  140. addr = msg->addr << 1 | (msg->flags & I2C_M_RD ? 1 : 0);
  141. writel(addr, iproc_i2c->base + M_TX_OFFSET);
  142. /* for a write transaction, load data into the TX FIFO */
  143. if (!(msg->flags & I2C_M_RD)) {
  144. for (i = 0; i < msg->len; i++) {
  145. val = msg->buf[i];
  146. /* mark the last byte */
  147. if (i == msg->len - 1)
  148. val |= 1 << M_TX_WR_STATUS_SHIFT;
  149. writel(val, iproc_i2c->base + M_TX_OFFSET);
  150. }
  151. }
  152. /* mark as incomplete before starting the transaction */
  153. reinit_completion(&iproc_i2c->done);
  154. iproc_i2c->xfer_is_done = 0;
  155. /*
  156. * Enable the "start busy" interrupt, which will be triggered after the
  157. * transaction is done, i.e., the internal start_busy bit, transitions
  158. * from 1 to 0.
  159. */
  160. writel(1 << IE_M_START_BUSY_SHIFT, iproc_i2c->base + IE_OFFSET);
  161. /*
  162. * Now we can activate the transfer. For a read operation, specify the
  163. * number of bytes to read
  164. */
  165. val = 1 << M_CMD_START_BUSY_SHIFT;
  166. if (msg->flags & I2C_M_RD) {
  167. val |= (M_CMD_PROTOCOL_BLK_RD << M_CMD_PROTOCOL_SHIFT) |
  168. (msg->len << M_CMD_RD_CNT_SHIFT);
  169. } else {
  170. val |= (M_CMD_PROTOCOL_BLK_WR << M_CMD_PROTOCOL_SHIFT);
  171. }
  172. writel(val, iproc_i2c->base + M_CMD_OFFSET);
  173. time_left = wait_for_completion_timeout(&iproc_i2c->done, time_left);
  174. /* disable all interrupts */
  175. writel(0, iproc_i2c->base + IE_OFFSET);
  176. /* read it back to flush the write */
  177. readl(iproc_i2c->base + IE_OFFSET);
  178. /* make sure the interrupt handler isn't running */
  179. synchronize_irq(iproc_i2c->irq);
  180. if (!time_left && !iproc_i2c->xfer_is_done) {
  181. dev_err(iproc_i2c->device, "transaction timed out\n");
  182. /* flush FIFOs */
  183. val = (1 << M_FIFO_RX_FLUSH_SHIFT) |
  184. (1 << M_FIFO_TX_FLUSH_SHIFT);
  185. writel(val, iproc_i2c->base + M_FIFO_CTRL_OFFSET);
  186. return -ETIMEDOUT;
  187. }
  188. ret = bcm_iproc_i2c_check_status(iproc_i2c, msg);
  189. if (ret) {
  190. /* flush both TX/RX FIFOs */
  191. val = (1 << M_FIFO_RX_FLUSH_SHIFT) |
  192. (1 << M_FIFO_TX_FLUSH_SHIFT);
  193. writel(val, iproc_i2c->base + M_FIFO_CTRL_OFFSET);
  194. return ret;
  195. }
  196. /*
  197. * For a read operation, we now need to load the data from FIFO
  198. * into the memory buffer
  199. */
  200. if (msg->flags & I2C_M_RD) {
  201. for (i = 0; i < msg->len; i++) {
  202. msg->buf[i] = (readl(iproc_i2c->base + M_RX_OFFSET) >>
  203. M_RX_DATA_SHIFT) & M_RX_DATA_MASK;
  204. }
  205. }
  206. return 0;
  207. }
  208. static int bcm_iproc_i2c_xfer(struct i2c_adapter *adapter,
  209. struct i2c_msg msgs[], int num)
  210. {
  211. struct bcm_iproc_i2c_dev *iproc_i2c = i2c_get_adapdata(adapter);
  212. int ret, i;
  213. /* go through all messages */
  214. for (i = 0; i < num; i++) {
  215. ret = bcm_iproc_i2c_xfer_single_msg(iproc_i2c, &msgs[i]);
  216. if (ret) {
  217. dev_dbg(iproc_i2c->device, "xfer failed\n");
  218. return ret;
  219. }
  220. }
  221. return num;
  222. }
  223. static uint32_t bcm_iproc_i2c_functionality(struct i2c_adapter *adap)
  224. {
  225. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
  226. }
  227. static const struct i2c_algorithm bcm_iproc_algo = {
  228. .master_xfer = bcm_iproc_i2c_xfer,
  229. .functionality = bcm_iproc_i2c_functionality,
  230. };
  231. static struct i2c_adapter_quirks bcm_iproc_i2c_quirks = {
  232. /* need to reserve one byte in the FIFO for the slave address */
  233. .max_read_len = M_TX_RX_FIFO_SIZE - 1,
  234. .max_write_len = M_TX_RX_FIFO_SIZE - 1,
  235. };
  236. static int bcm_iproc_i2c_cfg_speed(struct bcm_iproc_i2c_dev *iproc_i2c)
  237. {
  238. unsigned int bus_speed;
  239. u32 val;
  240. int ret = of_property_read_u32(iproc_i2c->device->of_node,
  241. "clock-frequency", &bus_speed);
  242. if (ret < 0) {
  243. dev_info(iproc_i2c->device,
  244. "unable to interpret clock-frequency DT property\n");
  245. bus_speed = 100000;
  246. }
  247. if (bus_speed < 100000) {
  248. dev_err(iproc_i2c->device, "%d Hz bus speed not supported\n",
  249. bus_speed);
  250. dev_err(iproc_i2c->device,
  251. "valid speeds are 100khz and 400khz\n");
  252. return -EINVAL;
  253. } else if (bus_speed < 400000) {
  254. bus_speed = 100000;
  255. } else {
  256. bus_speed = 400000;
  257. }
  258. iproc_i2c->bus_speed = bus_speed;
  259. val = readl(iproc_i2c->base + TIM_CFG_OFFSET);
  260. val &= ~(1 << TIM_CFG_MODE_400_SHIFT);
  261. val |= (bus_speed == 400000) << TIM_CFG_MODE_400_SHIFT;
  262. writel(val, iproc_i2c->base + TIM_CFG_OFFSET);
  263. dev_info(iproc_i2c->device, "bus set to %u Hz\n", bus_speed);
  264. return 0;
  265. }
  266. static int bcm_iproc_i2c_init(struct bcm_iproc_i2c_dev *iproc_i2c)
  267. {
  268. u32 val;
  269. /* put controller in reset */
  270. val = readl(iproc_i2c->base + CFG_OFFSET);
  271. val |= 1 << CFG_RESET_SHIFT;
  272. val &= ~(1 << CFG_EN_SHIFT);
  273. writel(val, iproc_i2c->base + CFG_OFFSET);
  274. /* wait 100 usec per spec */
  275. udelay(100);
  276. /* bring controller out of reset */
  277. val &= ~(1 << CFG_RESET_SHIFT);
  278. writel(val, iproc_i2c->base + CFG_OFFSET);
  279. /* flush TX/RX FIFOs and set RX FIFO threshold to zero */
  280. val = (1 << M_FIFO_RX_FLUSH_SHIFT) | (1 << M_FIFO_TX_FLUSH_SHIFT);
  281. writel(val, iproc_i2c->base + M_FIFO_CTRL_OFFSET);
  282. /* disable all interrupts */
  283. writel(0, iproc_i2c->base + IE_OFFSET);
  284. /* clear all pending interrupts */
  285. writel(0xffffffff, iproc_i2c->base + IS_OFFSET);
  286. return 0;
  287. }
  288. static void bcm_iproc_i2c_enable_disable(struct bcm_iproc_i2c_dev *iproc_i2c,
  289. bool enable)
  290. {
  291. u32 val;
  292. val = readl(iproc_i2c->base + CFG_OFFSET);
  293. if (enable)
  294. val |= BIT(CFG_EN_SHIFT);
  295. else
  296. val &= ~BIT(CFG_EN_SHIFT);
  297. writel(val, iproc_i2c->base + CFG_OFFSET);
  298. }
  299. static int bcm_iproc_i2c_probe(struct platform_device *pdev)
  300. {
  301. int irq, ret = 0;
  302. struct bcm_iproc_i2c_dev *iproc_i2c;
  303. struct i2c_adapter *adap;
  304. struct resource *res;
  305. iproc_i2c = devm_kzalloc(&pdev->dev, sizeof(*iproc_i2c),
  306. GFP_KERNEL);
  307. if (!iproc_i2c)
  308. return -ENOMEM;
  309. platform_set_drvdata(pdev, iproc_i2c);
  310. iproc_i2c->device = &pdev->dev;
  311. init_completion(&iproc_i2c->done);
  312. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  313. iproc_i2c->base = devm_ioremap_resource(iproc_i2c->device, res);
  314. if (IS_ERR(iproc_i2c->base))
  315. return PTR_ERR(iproc_i2c->base);
  316. ret = bcm_iproc_i2c_init(iproc_i2c);
  317. if (ret)
  318. return ret;
  319. ret = bcm_iproc_i2c_cfg_speed(iproc_i2c);
  320. if (ret)
  321. return ret;
  322. irq = platform_get_irq(pdev, 0);
  323. if (irq <= 0) {
  324. dev_err(iproc_i2c->device, "no irq resource\n");
  325. return irq;
  326. }
  327. iproc_i2c->irq = irq;
  328. ret = devm_request_irq(iproc_i2c->device, irq, bcm_iproc_i2c_isr, 0,
  329. pdev->name, iproc_i2c);
  330. if (ret < 0) {
  331. dev_err(iproc_i2c->device, "unable to request irq %i\n", irq);
  332. return ret;
  333. }
  334. bcm_iproc_i2c_enable_disable(iproc_i2c, true);
  335. adap = &iproc_i2c->adapter;
  336. i2c_set_adapdata(adap, iproc_i2c);
  337. strlcpy(adap->name, "Broadcom iProc I2C adapter", sizeof(adap->name));
  338. adap->algo = &bcm_iproc_algo;
  339. adap->quirks = &bcm_iproc_i2c_quirks;
  340. adap->dev.parent = &pdev->dev;
  341. adap->dev.of_node = pdev->dev.of_node;
  342. ret = i2c_add_adapter(adap);
  343. if (ret) {
  344. dev_err(iproc_i2c->device, "failed to add adapter\n");
  345. return ret;
  346. }
  347. return 0;
  348. }
  349. static int bcm_iproc_i2c_remove(struct platform_device *pdev)
  350. {
  351. struct bcm_iproc_i2c_dev *iproc_i2c = platform_get_drvdata(pdev);
  352. /* make sure there's no pending interrupt when we remove the adapter */
  353. writel(0, iproc_i2c->base + IE_OFFSET);
  354. readl(iproc_i2c->base + IE_OFFSET);
  355. synchronize_irq(iproc_i2c->irq);
  356. i2c_del_adapter(&iproc_i2c->adapter);
  357. bcm_iproc_i2c_enable_disable(iproc_i2c, false);
  358. return 0;
  359. }
  360. #ifdef CONFIG_PM_SLEEP
  361. static int bcm_iproc_i2c_suspend(struct device *dev)
  362. {
  363. struct platform_device *pdev = to_platform_device(dev);
  364. struct bcm_iproc_i2c_dev *iproc_i2c = platform_get_drvdata(pdev);
  365. /* make sure there's no pending interrupt when we go into suspend */
  366. writel(0, iproc_i2c->base + IE_OFFSET);
  367. readl(iproc_i2c->base + IE_OFFSET);
  368. synchronize_irq(iproc_i2c->irq);
  369. /* now disable the controller */
  370. bcm_iproc_i2c_enable_disable(iproc_i2c, false);
  371. return 0;
  372. }
  373. static int bcm_iproc_i2c_resume(struct device *dev)
  374. {
  375. struct platform_device *pdev = to_platform_device(dev);
  376. struct bcm_iproc_i2c_dev *iproc_i2c = platform_get_drvdata(pdev);
  377. int ret;
  378. u32 val;
  379. /*
  380. * Power domain could have been shut off completely in system deep
  381. * sleep, so re-initialize the block here
  382. */
  383. ret = bcm_iproc_i2c_init(iproc_i2c);
  384. if (ret)
  385. return ret;
  386. /* configure to the desired bus speed */
  387. val = readl(iproc_i2c->base + TIM_CFG_OFFSET);
  388. val &= ~(1 << TIM_CFG_MODE_400_SHIFT);
  389. val |= (iproc_i2c->bus_speed == 400000) << TIM_CFG_MODE_400_SHIFT;
  390. writel(val, iproc_i2c->base + TIM_CFG_OFFSET);
  391. bcm_iproc_i2c_enable_disable(iproc_i2c, true);
  392. return 0;
  393. }
  394. static const struct dev_pm_ops bcm_iproc_i2c_pm_ops = {
  395. .suspend_late = &bcm_iproc_i2c_suspend,
  396. .resume_early = &bcm_iproc_i2c_resume
  397. };
  398. #define BCM_IPROC_I2C_PM_OPS (&bcm_iproc_i2c_pm_ops)
  399. #else
  400. #define BCM_IPROC_I2C_PM_OPS NULL
  401. #endif /* CONFIG_PM_SLEEP */
  402. static const struct of_device_id bcm_iproc_i2c_of_match[] = {
  403. { .compatible = "brcm,iproc-i2c" },
  404. { /* sentinel */ }
  405. };
  406. MODULE_DEVICE_TABLE(of, bcm_iproc_i2c_of_match);
  407. static struct platform_driver bcm_iproc_i2c_driver = {
  408. .driver = {
  409. .name = "bcm-iproc-i2c",
  410. .of_match_table = bcm_iproc_i2c_of_match,
  411. .pm = BCM_IPROC_I2C_PM_OPS,
  412. },
  413. .probe = bcm_iproc_i2c_probe,
  414. .remove = bcm_iproc_i2c_remove,
  415. };
  416. module_platform_driver(bcm_iproc_i2c_driver);
  417. MODULE_AUTHOR("Ray Jui <rjui@broadcom.com>");
  418. MODULE_DESCRIPTION("Broadcom iProc I2C Driver");
  419. MODULE_LICENSE("GPL v2");