i2c-cns3xxx.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. /*
  2. * Cavium CNS3xxx I2C Host Controller
  3. *
  4. * Copyright 2010 Cavium Network
  5. * Copyright 2012 Gateworks Corporation
  6. * Chris Lang <clang@gateworks.com>
  7. * Tim Harvey <tharvey@gateworks.com>
  8. *
  9. * This file is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License, Version 2, as
  11. * published by the Free Software Foundation.
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/module.h>
  15. #include <linux/init.h>
  16. #include <linux/platform_device.h>
  17. #include <asm/io.h>
  18. #include <linux/wait.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/delay.h>
  21. #include <linux/i2c.h>
  22. #include <linux/slab.h>
  23. #include <linux/clk.h>
  24. /*
  25. * We need the memory map
  26. */
  27. #define I2C_MEM_MAP_ADDR(x) (i2c->base + x)
  28. #define I2C_MEM_MAP_VALUE(x) (*((unsigned int volatile*)I2C_MEM_MAP_ADDR(x)))
  29. #define I2C_CONTROLLER_REG I2C_MEM_MAP_VALUE(0x00)
  30. #define I2C_TIME_OUT_REG I2C_MEM_MAP_VALUE(0x04)
  31. #define I2C_SLAVE_ADDRESS_REG I2C_MEM_MAP_VALUE(0x08)
  32. #define I2C_WRITE_DATA_REG I2C_MEM_MAP_VALUE(0x0C)
  33. #define I2C_READ_DATA_REG I2C_MEM_MAP_VALUE(0x10)
  34. #define I2C_INTERRUPT_STATUS_REG I2C_MEM_MAP_VALUE(0x14)
  35. #define I2C_INTERRUPT_ENABLE_REG I2C_MEM_MAP_VALUE(0x18)
  36. #define I2C_TWI_OUT_DLY_REG I2C_MEM_MAP_VALUE(0x1C)
  37. #define I2C_BUS_ERROR_FLAG (0x1)
  38. #define I2C_ACTION_DONE_FLAG (0x2)
  39. #define CNS3xxx_I2C_ENABLE() (I2C_CONTROLLER_REG) |= ((unsigned int)0x1 << 31)
  40. #define CNS3xxx_I2C_DISABLE() (I2C_CONTROLLER_REG) &= ~((unsigned int)0x1 << 31)
  41. #define CNS3xxx_I2C_ENABLE_INTR() (I2C_INTERRUPT_ENABLE_REG) |= 0x03
  42. #define CNS3xxx_I2C_DISABLE_INTR() (I2C_INTERRUPT_ENABLE_REG) &= 0xfc
  43. #define TWI_TIMEOUT (10*HZ)
  44. #define I2C_100KHZ 100000
  45. #define I2C_200KHZ 200000
  46. #define I2C_300KHZ 300000
  47. #define I2C_400KHZ 400000
  48. #define CNS3xxx_I2C_CLK I2C_100KHZ
  49. #define STATE_DONE 1
  50. #define STATE_ERROR 2
  51. struct cns3xxx_i2c {
  52. struct device *dev;
  53. void __iomem *base; /* virtual */
  54. wait_queue_head_t wait;
  55. struct i2c_adapter adap;
  56. struct i2c_msg *msg;
  57. u8 state; /* see STATE_ */
  58. u8 error; /* see TWI_STATUS register */
  59. int rd_wr_len;
  60. u8 *buf;
  61. };
  62. static u32 cns3xxx_i2c_func(struct i2c_adapter *adap)
  63. {
  64. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
  65. }
  66. static int
  67. cns3xxx_i2c_xfer_msg(struct i2c_adapter *adap, struct i2c_msg *msg)
  68. {
  69. struct cns3xxx_i2c *i2c = i2c_get_adapdata(adap);
  70. int i, j;
  71. u8 buf[1] = { 0 };
  72. if (msg->len == 0) {
  73. /*
  74. * We are probably doing a probe for a device here,
  75. * so set the length to one, and data to 0
  76. */
  77. msg->len = 1;
  78. i2c->buf = buf;
  79. } else {
  80. i2c->buf = msg->buf;
  81. }
  82. if (msg->flags & I2C_M_TEN) {
  83. printk
  84. ("%s:%d: Presently the driver does not handle extended addressing\n",
  85. __FUNCTION__, __LINE__);
  86. return -EINVAL;
  87. }
  88. i2c->msg = msg;
  89. for (i = 0; i < msg->len; i++) {
  90. if (msg->len - i >= 4)
  91. i2c->rd_wr_len = 3;
  92. else
  93. i2c->rd_wr_len = msg->len - i - 1;
  94. // Set Data Width and TWI_EN
  95. I2C_CONTROLLER_REG = 0x80000000 | (i2c->rd_wr_len << 2) | (i2c->rd_wr_len);
  96. // Clear Write Reg
  97. I2C_WRITE_DATA_REG = 0;
  98. // Set the slave address
  99. I2C_SLAVE_ADDRESS_REG = (msg->addr << 1);
  100. // Are we Writing
  101. if (!(msg->flags & I2C_M_RD)) {
  102. I2C_CONTROLLER_REG |= (1 << 4);
  103. if (i != 0) {
  104. /*
  105. * We need to set the address in the first byte.
  106. * The base address is going to be in buf[0] and then
  107. * it needs to be incremented by i - 1.
  108. */
  109. i2c->buf--;
  110. *i2c->buf = buf[0] + i - 1;
  111. if (i2c->rd_wr_len < 3) {
  112. i += i2c->rd_wr_len;
  113. i2c->rd_wr_len++;
  114. I2C_CONTROLLER_REG = 0x80000000 | (1 << 4) | (i2c->rd_wr_len << 2) | (i2c->rd_wr_len);
  115. } else {
  116. i += i2c->rd_wr_len - 1;
  117. }
  118. } else {
  119. i += i2c->rd_wr_len;
  120. buf[0] = *i2c->buf;
  121. }
  122. for (j = 0; j <= i2c->rd_wr_len; j++) {
  123. I2C_WRITE_DATA_REG |= ((*i2c->buf++) << (8 * j));
  124. }
  125. } else {
  126. i += i2c->rd_wr_len;
  127. }
  128. // Start the Transfer
  129. i2c->state = 0; // Clear out the State
  130. i2c->error = 0;
  131. I2C_CONTROLLER_REG |= (1 << 6);
  132. if (wait_event_timeout(i2c->wait, (i2c->state == STATE_ERROR) ||
  133. (i2c->state == STATE_DONE), TWI_TIMEOUT)) {
  134. if (i2c->state == STATE_ERROR) {
  135. dev_dbg(i2c->dev, "controller error: 0x%2x", i2c->error);
  136. return -EAGAIN; // try again
  137. }
  138. } else {
  139. dev_err(i2c->dev, "controller timed out "
  140. "waiting for start condition to finish\n");
  141. return -ETIMEDOUT;
  142. }
  143. }
  144. return 0;
  145. }
  146. static int
  147. cns3xxx_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
  148. {
  149. int i;
  150. int ret;
  151. for (i = 0; i < num; i++)
  152. {
  153. ret = cns3xxx_i2c_xfer_msg(adap, &msgs[i]);
  154. if (ret < 0) {
  155. return ret;
  156. }
  157. }
  158. return num;
  159. }
  160. static struct i2c_algorithm cns3xxx_i2c_algo = {
  161. .master_xfer = cns3xxx_i2c_xfer,
  162. .functionality = cns3xxx_i2c_func,
  163. };
  164. static struct i2c_adapter cns3xxx_i2c_adapter = {
  165. .owner = THIS_MODULE,
  166. .algo = &cns3xxx_i2c_algo,
  167. .algo_data = NULL,
  168. .nr = 0,
  169. .name = "CNS3xxx I2C 0",
  170. .retries = 5,
  171. };
  172. static void cns3xxx_i2c_adapter_init(struct cns3xxx_i2c *i2c)
  173. {
  174. struct clk *clk;
  175. clk = devm_clk_get(i2c->dev, "cpu");
  176. if (WARN_ON(!clk))
  177. return;
  178. /* Disable the I2C */
  179. I2C_CONTROLLER_REG = 0; /* Disabled the I2C */
  180. /* Check the Reg Dump when testing */
  181. I2C_TIME_OUT_REG =
  182. (((((clk_get_rate(clk) / (2 * CNS3xxx_I2C_CLK)) -
  183. 1) & 0x3FF) << 8) | (1 << 7) | 0x7F);
  184. I2C_TWI_OUT_DLY_REG |= 0x3;
  185. /* Enable The Interrupt */
  186. CNS3xxx_I2C_ENABLE_INTR();
  187. /* Clear Interrupt Status (0x2 | 0x1) */
  188. I2C_INTERRUPT_STATUS_REG |= (I2C_ACTION_DONE_FLAG | I2C_BUS_ERROR_FLAG);
  189. /* Enable the I2C Controller */
  190. CNS3xxx_I2C_ENABLE();
  191. }
  192. static irqreturn_t cns3xxx_i2c_isr(int irq, void *dev_id)
  193. {
  194. struct cns3xxx_i2c *i2c = dev_id;
  195. int i;
  196. uint32_t stat = I2C_INTERRUPT_STATUS_REG;
  197. /* Clear Interrupt */
  198. I2C_INTERRUPT_STATUS_REG |= 0x1;
  199. if (stat & I2C_BUS_ERROR_FLAG) {
  200. i2c->state = STATE_ERROR;
  201. i2c->error = (I2C_INTERRUPT_STATUS_REG & 0xff00)>>8;
  202. } else {
  203. if (i2c->msg->flags & I2C_M_RD) {
  204. for (i = 0; i <= i2c->rd_wr_len; i++)
  205. {
  206. *i2c->buf++ = ((I2C_READ_DATA_REG >> (8 * i)) & 0xff);
  207. }
  208. }
  209. i2c->state = STATE_DONE;
  210. }
  211. wake_up(&i2c->wait);
  212. return IRQ_HANDLED;
  213. }
  214. static int cns3xxx_i2c_probe(struct platform_device *pdev)
  215. {
  216. struct cns3xxx_i2c *i2c;
  217. struct resource *res, *res2;
  218. int ret;
  219. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  220. if (!res) {
  221. printk("%s: IORESOURCE_MEM not defined \n", __FUNCTION__);
  222. return -ENODEV;
  223. }
  224. res2 = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  225. if (!res2) {
  226. printk("%s: IORESOURCE_IRQ not defined \n", __FUNCTION__);
  227. return -ENODEV;
  228. }
  229. i2c = kzalloc(sizeof(*i2c), GFP_KERNEL);
  230. if (!i2c)
  231. return -ENOMEM;
  232. if (!request_mem_region(res->start, res->end - res->start + 1,
  233. pdev->name)) {
  234. dev_err(&pdev->dev, "Memory region busy\n");
  235. ret = -EBUSY;
  236. goto request_mem_failed;
  237. }
  238. i2c->dev = &pdev->dev;
  239. i2c->base = ioremap(res->start, res->end - res->start + 1);
  240. if (!i2c->base) {
  241. dev_err(&pdev->dev, "Unable to map registers\n");
  242. ret = -EIO;
  243. goto map_failed;
  244. }
  245. cns3xxx_i2c_adapter_init(i2c);
  246. init_waitqueue_head(&i2c->wait);
  247. ret = request_irq(res2->start, cns3xxx_i2c_isr, 0, pdev->name, i2c);
  248. if (ret) {
  249. dev_err(&pdev->dev, "Cannot claim IRQ\n");
  250. goto request_irq_failed;
  251. }
  252. platform_set_drvdata(pdev, i2c);
  253. i2c->adap = cns3xxx_i2c_adapter;
  254. i2c_set_adapdata(&i2c->adap, i2c);
  255. i2c->adap.dev.parent = &pdev->dev;
  256. /* add i2c adapter to i2c tree */
  257. ret = i2c_add_numbered_adapter(&i2c->adap);
  258. if (ret) {
  259. dev_err(&pdev->dev, "Failed to add adapter\n");
  260. goto add_adapter_failed;
  261. }
  262. return 0;
  263. add_adapter_failed:
  264. free_irq(res2->start, i2c);
  265. request_irq_failed:
  266. iounmap(i2c->base);
  267. map_failed:
  268. release_mem_region(res->start, res->end - res->start + 1);
  269. request_mem_failed:
  270. kfree(i2c);
  271. return ret;
  272. }
  273. static int cns3xxx_i2c_remove(struct platform_device *pdev)
  274. {
  275. struct cns3xxx_i2c *i2c = platform_get_drvdata(pdev);
  276. struct resource *res;
  277. /* disable i2c logic */
  278. CNS3xxx_I2C_DISABLE_INTR();
  279. CNS3xxx_I2C_DISABLE();
  280. /* remove adapter & data */
  281. i2c_del_adapter(&i2c->adap);
  282. platform_set_drvdata(pdev, NULL);
  283. res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  284. if (res)
  285. free_irq(res->start, i2c);
  286. iounmap(i2c->base);
  287. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  288. if (res)
  289. release_mem_region(res->start, res->end - res->start + 1);
  290. kfree(i2c);
  291. return 0;
  292. }
  293. static struct platform_driver cns3xxx_i2c_driver = {
  294. .probe = cns3xxx_i2c_probe,
  295. .remove = cns3xxx_i2c_remove,
  296. .driver = {
  297. .owner = THIS_MODULE,
  298. .name = "cns3xxx-i2c",
  299. },
  300. };
  301. static int __init cns3xxx_i2c_init(void)
  302. {
  303. return platform_driver_register(&cns3xxx_i2c_driver);
  304. }
  305. static void __exit cns3xxx_i2c_exit(void)
  306. {
  307. platform_driver_unregister(&cns3xxx_i2c_driver);
  308. }
  309. module_init(cns3xxx_i2c_init);
  310. module_exit(cns3xxx_i2c_exit);
  311. MODULE_AUTHOR("Cavium Networks");
  312. MODULE_DESCRIPTION("Cavium CNS3XXX I2C Controller");
  313. MODULE_LICENSE("GPL");