i2c-sirf.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. /*
  2. * I2C bus driver for CSR SiRFprimaII
  3. *
  4. * Copyright (c) 2011 Cambridge Silicon Radio Limited, a CSR plc group company.
  5. *
  6. * Licensed under GPLv2 or later.
  7. */
  8. #include <linux/interrupt.h>
  9. #include <linux/kernel.h>
  10. #include <linux/module.h>
  11. #include <linux/slab.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/i2c.h>
  14. #include <linux/clk.h>
  15. #include <linux/err.h>
  16. #include <linux/io.h>
  17. #define SIRFSOC_I2C_CLK_CTRL 0x00
  18. #define SIRFSOC_I2C_STATUS 0x0C
  19. #define SIRFSOC_I2C_CTRL 0x10
  20. #define SIRFSOC_I2C_IO_CTRL 0x14
  21. #define SIRFSOC_I2C_SDA_DELAY 0x18
  22. #define SIRFSOC_I2C_CMD_START 0x1C
  23. #define SIRFSOC_I2C_CMD_BUF 0x30
  24. #define SIRFSOC_I2C_DATA_BUF 0x80
  25. #define SIRFSOC_I2C_CMD_BUF_MAX 16
  26. #define SIRFSOC_I2C_DATA_BUF_MAX 16
  27. #define SIRFSOC_I2C_CMD(x) (SIRFSOC_I2C_CMD_BUF + (x)*0x04)
  28. #define SIRFSOC_I2C_DATA_MASK(x) (0xFF<<(((x)&3)*8))
  29. #define SIRFSOC_I2C_DATA_SHIFT(x) (((x)&3)*8)
  30. #define SIRFSOC_I2C_DIV_MASK (0xFFFF)
  31. /* I2C status flags */
  32. #define SIRFSOC_I2C_STAT_BUSY BIT(0)
  33. #define SIRFSOC_I2C_STAT_TIP BIT(1)
  34. #define SIRFSOC_I2C_STAT_NACK BIT(2)
  35. #define SIRFSOC_I2C_STAT_TR_INT BIT(4)
  36. #define SIRFSOC_I2C_STAT_STOP BIT(6)
  37. #define SIRFSOC_I2C_STAT_CMD_DONE BIT(8)
  38. #define SIRFSOC_I2C_STAT_ERR BIT(9)
  39. #define SIRFSOC_I2C_CMD_INDEX (0x1F<<16)
  40. /* I2C control flags */
  41. #define SIRFSOC_I2C_RESET BIT(0)
  42. #define SIRFSOC_I2C_CORE_EN BIT(1)
  43. #define SIRFSOC_I2C_MASTER_MODE BIT(2)
  44. #define SIRFSOC_I2C_CMD_DONE_EN BIT(11)
  45. #define SIRFSOC_I2C_ERR_INT_EN BIT(12)
  46. #define SIRFSOC_I2C_SDA_DELAY_MASK (0xFF)
  47. #define SIRFSOC_I2C_SCLF_FILTER (3<<8)
  48. #define SIRFSOC_I2C_START_CMD BIT(0)
  49. #define SIRFSOC_I2C_CMD_RP(x) ((x)&0x7)
  50. #define SIRFSOC_I2C_NACK BIT(3)
  51. #define SIRFSOC_I2C_WRITE BIT(4)
  52. #define SIRFSOC_I2C_READ BIT(5)
  53. #define SIRFSOC_I2C_STOP BIT(6)
  54. #define SIRFSOC_I2C_START BIT(7)
  55. #define SIRFSOC_I2C_DEFAULT_SPEED 100000
  56. #define SIRFSOC_I2C_ERR_NOACK 1
  57. #define SIRFSOC_I2C_ERR_TIMEOUT 2
  58. struct sirfsoc_i2c {
  59. void __iomem *base;
  60. struct clk *clk;
  61. u32 cmd_ptr; /* Current position in CMD buffer */
  62. u8 *buf; /* Buffer passed by user */
  63. u32 msg_len; /* Message length */
  64. u32 finished_len; /* number of bytes read/written */
  65. u32 read_cmd_len; /* number of read cmd sent */
  66. int msg_read; /* 1 indicates a read message */
  67. int err_status; /* 1 indicates an error on bus */
  68. u32 sda_delay; /* For suspend/resume */
  69. u32 clk_div;
  70. int last; /* Last message in transfer, STOP cmd can be sent */
  71. struct completion done; /* indicates completion of message transfer */
  72. struct i2c_adapter adapter;
  73. };
  74. static void i2c_sirfsoc_read_data(struct sirfsoc_i2c *siic)
  75. {
  76. u32 data = 0;
  77. int i;
  78. for (i = 0; i < siic->read_cmd_len; i++) {
  79. if (!(i & 0x3))
  80. data = readl(siic->base + SIRFSOC_I2C_DATA_BUF + i);
  81. siic->buf[siic->finished_len++] =
  82. (u8)((data & SIRFSOC_I2C_DATA_MASK(i)) >>
  83. SIRFSOC_I2C_DATA_SHIFT(i));
  84. }
  85. }
  86. static void i2c_sirfsoc_queue_cmd(struct sirfsoc_i2c *siic)
  87. {
  88. u32 regval;
  89. int i = 0;
  90. if (siic->msg_read) {
  91. while (((siic->finished_len + i) < siic->msg_len)
  92. && (siic->cmd_ptr < SIRFSOC_I2C_CMD_BUF_MAX)) {
  93. regval = SIRFSOC_I2C_READ | SIRFSOC_I2C_CMD_RP(0);
  94. if (((siic->finished_len + i) ==
  95. (siic->msg_len - 1)) && siic->last)
  96. regval |= SIRFSOC_I2C_STOP | SIRFSOC_I2C_NACK;
  97. writel(regval,
  98. siic->base + SIRFSOC_I2C_CMD(siic->cmd_ptr++));
  99. i++;
  100. }
  101. siic->read_cmd_len = i;
  102. } else {
  103. while ((siic->cmd_ptr < SIRFSOC_I2C_CMD_BUF_MAX - 1)
  104. && (siic->finished_len < siic->msg_len)) {
  105. regval = SIRFSOC_I2C_WRITE | SIRFSOC_I2C_CMD_RP(0);
  106. if ((siic->finished_len == (siic->msg_len - 1))
  107. && siic->last)
  108. regval |= SIRFSOC_I2C_STOP;
  109. writel(regval,
  110. siic->base + SIRFSOC_I2C_CMD(siic->cmd_ptr++));
  111. writel(siic->buf[siic->finished_len++],
  112. siic->base + SIRFSOC_I2C_CMD(siic->cmd_ptr++));
  113. }
  114. }
  115. siic->cmd_ptr = 0;
  116. /* Trigger the transfer */
  117. writel(SIRFSOC_I2C_START_CMD, siic->base + SIRFSOC_I2C_CMD_START);
  118. }
  119. static irqreturn_t i2c_sirfsoc_irq(int irq, void *dev_id)
  120. {
  121. struct sirfsoc_i2c *siic = (struct sirfsoc_i2c *)dev_id;
  122. u32 i2c_stat = readl(siic->base + SIRFSOC_I2C_STATUS);
  123. if (i2c_stat & SIRFSOC_I2C_STAT_ERR) {
  124. /* Error conditions */
  125. siic->err_status = SIRFSOC_I2C_ERR_NOACK;
  126. writel(SIRFSOC_I2C_STAT_ERR, siic->base + SIRFSOC_I2C_STATUS);
  127. if (i2c_stat & SIRFSOC_I2C_STAT_NACK)
  128. dev_dbg(&siic->adapter.dev, "ACK not received\n");
  129. else
  130. dev_err(&siic->adapter.dev, "I2C error\n");
  131. /*
  132. * Due to hardware ANOMALY, we need to reset I2C earlier after
  133. * we get NOACK while accessing non-existing clients, otherwise
  134. * we will get errors even we access existing clients later
  135. */
  136. writel(readl(siic->base + SIRFSOC_I2C_CTRL) | SIRFSOC_I2C_RESET,
  137. siic->base + SIRFSOC_I2C_CTRL);
  138. while (readl(siic->base + SIRFSOC_I2C_CTRL) & SIRFSOC_I2C_RESET)
  139. cpu_relax();
  140. complete(&siic->done);
  141. } else if (i2c_stat & SIRFSOC_I2C_STAT_CMD_DONE) {
  142. /* CMD buffer execution complete */
  143. if (siic->msg_read)
  144. i2c_sirfsoc_read_data(siic);
  145. if (siic->finished_len == siic->msg_len)
  146. complete(&siic->done);
  147. else /* Fill a new CMD buffer for left data */
  148. i2c_sirfsoc_queue_cmd(siic);
  149. writel(SIRFSOC_I2C_STAT_CMD_DONE, siic->base + SIRFSOC_I2C_STATUS);
  150. }
  151. return IRQ_HANDLED;
  152. }
  153. static void i2c_sirfsoc_set_address(struct sirfsoc_i2c *siic,
  154. struct i2c_msg *msg)
  155. {
  156. unsigned char addr;
  157. u32 regval = SIRFSOC_I2C_START | SIRFSOC_I2C_CMD_RP(0) | SIRFSOC_I2C_WRITE;
  158. /* no data and last message -> add STOP */
  159. if (siic->last && (msg->len == 0))
  160. regval |= SIRFSOC_I2C_STOP;
  161. writel(regval, siic->base + SIRFSOC_I2C_CMD(siic->cmd_ptr++));
  162. addr = i2c_8bit_addr_from_msg(msg);
  163. /* Reverse direction bit */
  164. if (msg->flags & I2C_M_REV_DIR_ADDR)
  165. addr ^= 1;
  166. writel(addr, siic->base + SIRFSOC_I2C_CMD(siic->cmd_ptr++));
  167. }
  168. static int i2c_sirfsoc_xfer_msg(struct sirfsoc_i2c *siic, struct i2c_msg *msg)
  169. {
  170. u32 regval = readl(siic->base + SIRFSOC_I2C_CTRL);
  171. /* timeout waiting for the xfer to finish or fail */
  172. int timeout = msecs_to_jiffies((msg->len + 1) * 50);
  173. i2c_sirfsoc_set_address(siic, msg);
  174. writel(regval | SIRFSOC_I2C_CMD_DONE_EN | SIRFSOC_I2C_ERR_INT_EN,
  175. siic->base + SIRFSOC_I2C_CTRL);
  176. i2c_sirfsoc_queue_cmd(siic);
  177. if (wait_for_completion_timeout(&siic->done, timeout) == 0) {
  178. siic->err_status = SIRFSOC_I2C_ERR_TIMEOUT;
  179. dev_err(&siic->adapter.dev, "Transfer timeout\n");
  180. }
  181. writel(regval & ~(SIRFSOC_I2C_CMD_DONE_EN | SIRFSOC_I2C_ERR_INT_EN),
  182. siic->base + SIRFSOC_I2C_CTRL);
  183. writel(0, siic->base + SIRFSOC_I2C_CMD_START);
  184. /* i2c control doesn't response, reset it */
  185. if (siic->err_status == SIRFSOC_I2C_ERR_TIMEOUT) {
  186. writel(readl(siic->base + SIRFSOC_I2C_CTRL) | SIRFSOC_I2C_RESET,
  187. siic->base + SIRFSOC_I2C_CTRL);
  188. while (readl(siic->base + SIRFSOC_I2C_CTRL) & SIRFSOC_I2C_RESET)
  189. cpu_relax();
  190. }
  191. return siic->err_status ? -EAGAIN : 0;
  192. }
  193. static u32 i2c_sirfsoc_func(struct i2c_adapter *adap)
  194. {
  195. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
  196. }
  197. static int i2c_sirfsoc_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs,
  198. int num)
  199. {
  200. struct sirfsoc_i2c *siic = adap->algo_data;
  201. int i, ret;
  202. clk_enable(siic->clk);
  203. for (i = 0; i < num; i++) {
  204. siic->buf = msgs[i].buf;
  205. siic->msg_len = msgs[i].len;
  206. siic->msg_read = !!(msgs[i].flags & I2C_M_RD);
  207. siic->err_status = 0;
  208. siic->cmd_ptr = 0;
  209. siic->finished_len = 0;
  210. siic->last = (i == (num - 1));
  211. ret = i2c_sirfsoc_xfer_msg(siic, &msgs[i]);
  212. if (ret) {
  213. clk_disable(siic->clk);
  214. return ret;
  215. }
  216. }
  217. clk_disable(siic->clk);
  218. return num;
  219. }
  220. /* I2C algorithms associated with this master controller driver */
  221. static const struct i2c_algorithm i2c_sirfsoc_algo = {
  222. .master_xfer = i2c_sirfsoc_xfer,
  223. .functionality = i2c_sirfsoc_func,
  224. };
  225. static int i2c_sirfsoc_probe(struct platform_device *pdev)
  226. {
  227. struct sirfsoc_i2c *siic;
  228. struct i2c_adapter *adap;
  229. struct resource *mem_res;
  230. struct clk *clk;
  231. int bitrate;
  232. int ctrl_speed;
  233. int irq;
  234. int err;
  235. u32 regval;
  236. clk = clk_get(&pdev->dev, NULL);
  237. if (IS_ERR(clk)) {
  238. err = PTR_ERR(clk);
  239. dev_err(&pdev->dev, "Clock get failed\n");
  240. goto err_get_clk;
  241. }
  242. err = clk_prepare(clk);
  243. if (err) {
  244. dev_err(&pdev->dev, "Clock prepare failed\n");
  245. goto err_clk_prep;
  246. }
  247. err = clk_enable(clk);
  248. if (err) {
  249. dev_err(&pdev->dev, "Clock enable failed\n");
  250. goto err_clk_en;
  251. }
  252. ctrl_speed = clk_get_rate(clk);
  253. siic = devm_kzalloc(&pdev->dev, sizeof(*siic), GFP_KERNEL);
  254. if (!siic) {
  255. err = -ENOMEM;
  256. goto out;
  257. }
  258. adap = &siic->adapter;
  259. adap->class = I2C_CLASS_DEPRECATED;
  260. mem_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  261. siic->base = devm_ioremap_resource(&pdev->dev, mem_res);
  262. if (IS_ERR(siic->base)) {
  263. err = PTR_ERR(siic->base);
  264. goto out;
  265. }
  266. irq = platform_get_irq(pdev, 0);
  267. if (irq < 0) {
  268. err = irq;
  269. goto out;
  270. }
  271. err = devm_request_irq(&pdev->dev, irq, i2c_sirfsoc_irq, 0,
  272. dev_name(&pdev->dev), siic);
  273. if (err)
  274. goto out;
  275. adap->algo = &i2c_sirfsoc_algo;
  276. adap->algo_data = siic;
  277. adap->retries = 3;
  278. adap->dev.of_node = pdev->dev.of_node;
  279. adap->dev.parent = &pdev->dev;
  280. adap->nr = pdev->id;
  281. strlcpy(adap->name, "sirfsoc-i2c", sizeof(adap->name));
  282. platform_set_drvdata(pdev, adap);
  283. init_completion(&siic->done);
  284. /* Controller Initalisation */
  285. writel(SIRFSOC_I2C_RESET, siic->base + SIRFSOC_I2C_CTRL);
  286. while (readl(siic->base + SIRFSOC_I2C_CTRL) & SIRFSOC_I2C_RESET)
  287. cpu_relax();
  288. writel(SIRFSOC_I2C_CORE_EN | SIRFSOC_I2C_MASTER_MODE,
  289. siic->base + SIRFSOC_I2C_CTRL);
  290. siic->clk = clk;
  291. err = of_property_read_u32(pdev->dev.of_node,
  292. "clock-frequency", &bitrate);
  293. if (err < 0)
  294. bitrate = SIRFSOC_I2C_DEFAULT_SPEED;
  295. /*
  296. * Due to some hardware design issues, we need to tune the formula.
  297. * Since i2c is open drain interface that allows the slave to
  298. * stall the transaction by holding the SCL line at '0', the RTL
  299. * implementation is waiting for SCL feedback from the pin after
  300. * setting it to High-Z ('1'). This wait adds to the high-time
  301. * interval counter few cycles of the input synchronization
  302. * (depending on the SCL_FILTER_REG field), and also the time it
  303. * takes for the board pull-up resistor to rise the SCL line.
  304. * For slow SCL settings these additions are negligible,
  305. * but they start to affect the speed when clock is set to faster
  306. * frequencies.
  307. * Through the actual tests, use the different user_div value(which
  308. * in the divider formular 'Fio / (Fi2c * user_div)') to adapt
  309. * the different ranges of i2c bus clock frequency, to make the SCL
  310. * more accurate.
  311. */
  312. if (bitrate <= 30000)
  313. regval = ctrl_speed / (bitrate * 5);
  314. else if (bitrate > 30000 && bitrate <= 280000)
  315. regval = (2 * ctrl_speed) / (bitrate * 11);
  316. else
  317. regval = ctrl_speed / (bitrate * 6);
  318. writel(regval, siic->base + SIRFSOC_I2C_CLK_CTRL);
  319. if (regval > 0xFF)
  320. writel(0xFF, siic->base + SIRFSOC_I2C_SDA_DELAY);
  321. else
  322. writel(regval, siic->base + SIRFSOC_I2C_SDA_DELAY);
  323. err = i2c_add_numbered_adapter(adap);
  324. if (err < 0)
  325. goto out;
  326. clk_disable(clk);
  327. dev_info(&pdev->dev, " I2C adapter ready to operate\n");
  328. return 0;
  329. out:
  330. clk_disable(clk);
  331. err_clk_en:
  332. clk_unprepare(clk);
  333. err_clk_prep:
  334. clk_put(clk);
  335. err_get_clk:
  336. return err;
  337. }
  338. static int i2c_sirfsoc_remove(struct platform_device *pdev)
  339. {
  340. struct i2c_adapter *adapter = platform_get_drvdata(pdev);
  341. struct sirfsoc_i2c *siic = adapter->algo_data;
  342. writel(SIRFSOC_I2C_RESET, siic->base + SIRFSOC_I2C_CTRL);
  343. i2c_del_adapter(adapter);
  344. clk_unprepare(siic->clk);
  345. clk_put(siic->clk);
  346. return 0;
  347. }
  348. #ifdef CONFIG_PM
  349. static int i2c_sirfsoc_suspend(struct device *dev)
  350. {
  351. struct platform_device *pdev = to_platform_device(dev);
  352. struct i2c_adapter *adapter = platform_get_drvdata(pdev);
  353. struct sirfsoc_i2c *siic = adapter->algo_data;
  354. clk_enable(siic->clk);
  355. siic->sda_delay = readl(siic->base + SIRFSOC_I2C_SDA_DELAY);
  356. siic->clk_div = readl(siic->base + SIRFSOC_I2C_CLK_CTRL);
  357. clk_disable(siic->clk);
  358. return 0;
  359. }
  360. static int i2c_sirfsoc_resume(struct device *dev)
  361. {
  362. struct platform_device *pdev = to_platform_device(dev);
  363. struct i2c_adapter *adapter = platform_get_drvdata(pdev);
  364. struct sirfsoc_i2c *siic = adapter->algo_data;
  365. clk_enable(siic->clk);
  366. writel(SIRFSOC_I2C_RESET, siic->base + SIRFSOC_I2C_CTRL);
  367. while (readl(siic->base + SIRFSOC_I2C_CTRL) & SIRFSOC_I2C_RESET)
  368. cpu_relax();
  369. writel(SIRFSOC_I2C_CORE_EN | SIRFSOC_I2C_MASTER_MODE,
  370. siic->base + SIRFSOC_I2C_CTRL);
  371. writel(siic->clk_div, siic->base + SIRFSOC_I2C_CLK_CTRL);
  372. writel(siic->sda_delay, siic->base + SIRFSOC_I2C_SDA_DELAY);
  373. clk_disable(siic->clk);
  374. return 0;
  375. }
  376. static const struct dev_pm_ops i2c_sirfsoc_pm_ops = {
  377. .suspend = i2c_sirfsoc_suspend,
  378. .resume = i2c_sirfsoc_resume,
  379. };
  380. #endif
  381. static const struct of_device_id sirfsoc_i2c_of_match[] = {
  382. { .compatible = "sirf,prima2-i2c", },
  383. {},
  384. };
  385. MODULE_DEVICE_TABLE(of, sirfsoc_i2c_of_match);
  386. static struct platform_driver i2c_sirfsoc_driver = {
  387. .driver = {
  388. .name = "sirfsoc_i2c",
  389. #ifdef CONFIG_PM
  390. .pm = &i2c_sirfsoc_pm_ops,
  391. #endif
  392. .of_match_table = sirfsoc_i2c_of_match,
  393. },
  394. .probe = i2c_sirfsoc_probe,
  395. .remove = i2c_sirfsoc_remove,
  396. };
  397. module_platform_driver(i2c_sirfsoc_driver);
  398. MODULE_DESCRIPTION("SiRF SoC I2C master controller driver");
  399. MODULE_AUTHOR("Zhiwu Song <Zhiwu.Song@csr.com>, "
  400. "Xiangzhen Ye <Xiangzhen.Ye@csr.com>");
  401. MODULE_LICENSE("GPL v2");