i2c-sirf.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  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 = msg->addr << 1; /* Generate address */
  163. if (msg->flags & I2C_M_RD)
  164. addr |= 1;
  165. /* Reverse direction bit */
  166. if (msg->flags & I2C_M_REV_DIR_ADDR)
  167. addr ^= 1;
  168. writel(addr, siic->base + SIRFSOC_I2C_CMD(siic->cmd_ptr++));
  169. }
  170. static int i2c_sirfsoc_xfer_msg(struct sirfsoc_i2c *siic, struct i2c_msg *msg)
  171. {
  172. u32 regval = readl(siic->base + SIRFSOC_I2C_CTRL);
  173. /* timeout waiting for the xfer to finish or fail */
  174. int timeout = msecs_to_jiffies((msg->len + 1) * 50);
  175. i2c_sirfsoc_set_address(siic, msg);
  176. writel(regval | SIRFSOC_I2C_CMD_DONE_EN | SIRFSOC_I2C_ERR_INT_EN,
  177. siic->base + SIRFSOC_I2C_CTRL);
  178. i2c_sirfsoc_queue_cmd(siic);
  179. if (wait_for_completion_timeout(&siic->done, timeout) == 0) {
  180. siic->err_status = SIRFSOC_I2C_ERR_TIMEOUT;
  181. dev_err(&siic->adapter.dev, "Transfer timeout\n");
  182. }
  183. writel(regval & ~(SIRFSOC_I2C_CMD_DONE_EN | SIRFSOC_I2C_ERR_INT_EN),
  184. siic->base + SIRFSOC_I2C_CTRL);
  185. writel(0, siic->base + SIRFSOC_I2C_CMD_START);
  186. /* i2c control doesn't response, reset it */
  187. if (siic->err_status == SIRFSOC_I2C_ERR_TIMEOUT) {
  188. writel(readl(siic->base + SIRFSOC_I2C_CTRL) | SIRFSOC_I2C_RESET,
  189. siic->base + SIRFSOC_I2C_CTRL);
  190. while (readl(siic->base + SIRFSOC_I2C_CTRL) & SIRFSOC_I2C_RESET)
  191. cpu_relax();
  192. }
  193. return siic->err_status ? -EAGAIN : 0;
  194. }
  195. static u32 i2c_sirfsoc_func(struct i2c_adapter *adap)
  196. {
  197. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
  198. }
  199. static int i2c_sirfsoc_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs,
  200. int num)
  201. {
  202. struct sirfsoc_i2c *siic = adap->algo_data;
  203. int i, ret;
  204. clk_enable(siic->clk);
  205. for (i = 0; i < num; i++) {
  206. siic->buf = msgs[i].buf;
  207. siic->msg_len = msgs[i].len;
  208. siic->msg_read = !!(msgs[i].flags & I2C_M_RD);
  209. siic->err_status = 0;
  210. siic->cmd_ptr = 0;
  211. siic->finished_len = 0;
  212. siic->last = (i == (num - 1));
  213. ret = i2c_sirfsoc_xfer_msg(siic, &msgs[i]);
  214. if (ret) {
  215. clk_disable(siic->clk);
  216. return ret;
  217. }
  218. }
  219. clk_disable(siic->clk);
  220. return num;
  221. }
  222. /* I2C algorithms associated with this master controller driver */
  223. static const struct i2c_algorithm i2c_sirfsoc_algo = {
  224. .master_xfer = i2c_sirfsoc_xfer,
  225. .functionality = i2c_sirfsoc_func,
  226. };
  227. static int i2c_sirfsoc_probe(struct platform_device *pdev)
  228. {
  229. struct sirfsoc_i2c *siic;
  230. struct i2c_adapter *adap;
  231. struct resource *mem_res;
  232. struct clk *clk;
  233. int bitrate;
  234. int ctrl_speed;
  235. int irq;
  236. int err;
  237. u32 regval;
  238. clk = clk_get(&pdev->dev, NULL);
  239. if (IS_ERR(clk)) {
  240. err = PTR_ERR(clk);
  241. dev_err(&pdev->dev, "Clock get failed\n");
  242. goto err_get_clk;
  243. }
  244. err = clk_prepare(clk);
  245. if (err) {
  246. dev_err(&pdev->dev, "Clock prepare failed\n");
  247. goto err_clk_prep;
  248. }
  249. err = clk_enable(clk);
  250. if (err) {
  251. dev_err(&pdev->dev, "Clock enable failed\n");
  252. goto err_clk_en;
  253. }
  254. ctrl_speed = clk_get_rate(clk);
  255. siic = devm_kzalloc(&pdev->dev, sizeof(*siic), GFP_KERNEL);
  256. if (!siic) {
  257. err = -ENOMEM;
  258. goto out;
  259. }
  260. adap = &siic->adapter;
  261. adap->class = I2C_CLASS_DEPRECATED;
  262. mem_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  263. siic->base = devm_ioremap_resource(&pdev->dev, mem_res);
  264. if (IS_ERR(siic->base)) {
  265. err = PTR_ERR(siic->base);
  266. goto out;
  267. }
  268. irq = platform_get_irq(pdev, 0);
  269. if (irq < 0) {
  270. err = irq;
  271. goto out;
  272. }
  273. err = devm_request_irq(&pdev->dev, irq, i2c_sirfsoc_irq, 0,
  274. dev_name(&pdev->dev), siic);
  275. if (err)
  276. goto out;
  277. adap->algo = &i2c_sirfsoc_algo;
  278. adap->algo_data = siic;
  279. adap->retries = 3;
  280. adap->dev.of_node = pdev->dev.of_node;
  281. adap->dev.parent = &pdev->dev;
  282. adap->nr = pdev->id;
  283. strlcpy(adap->name, "sirfsoc-i2c", sizeof(adap->name));
  284. platform_set_drvdata(pdev, adap);
  285. init_completion(&siic->done);
  286. /* Controller Initalisation */
  287. writel(SIRFSOC_I2C_RESET, siic->base + SIRFSOC_I2C_CTRL);
  288. while (readl(siic->base + SIRFSOC_I2C_CTRL) & SIRFSOC_I2C_RESET)
  289. cpu_relax();
  290. writel(SIRFSOC_I2C_CORE_EN | SIRFSOC_I2C_MASTER_MODE,
  291. siic->base + SIRFSOC_I2C_CTRL);
  292. siic->clk = clk;
  293. err = of_property_read_u32(pdev->dev.of_node,
  294. "clock-frequency", &bitrate);
  295. if (err < 0)
  296. bitrate = SIRFSOC_I2C_DEFAULT_SPEED;
  297. /*
  298. * Due to some hardware design issues, we need to tune the formula.
  299. * Since i2c is open drain interface that allows the slave to
  300. * stall the transaction by holding the SCL line at '0', the RTL
  301. * implementation is waiting for SCL feedback from the pin after
  302. * setting it to High-Z ('1'). This wait adds to the high-time
  303. * interval counter few cycles of the input synchronization
  304. * (depending on the SCL_FILTER_REG field), and also the time it
  305. * takes for the board pull-up resistor to rise the SCL line.
  306. * For slow SCL settings these additions are negligible,
  307. * but they start to affect the speed when clock is set to faster
  308. * frequencies.
  309. * Through the actual tests, use the different user_div value(which
  310. * in the divider formular 'Fio / (Fi2c * user_div)') to adapt
  311. * the different ranges of i2c bus clock frequency, to make the SCL
  312. * more accurate.
  313. */
  314. if (bitrate <= 30000)
  315. regval = ctrl_speed / (bitrate * 5);
  316. else if (bitrate > 30000 && bitrate <= 280000)
  317. regval = (2 * ctrl_speed) / (bitrate * 11);
  318. else
  319. regval = ctrl_speed / (bitrate * 6);
  320. writel(regval, siic->base + SIRFSOC_I2C_CLK_CTRL);
  321. if (regval > 0xFF)
  322. writel(0xFF, siic->base + SIRFSOC_I2C_SDA_DELAY);
  323. else
  324. writel(regval, siic->base + SIRFSOC_I2C_SDA_DELAY);
  325. err = i2c_add_numbered_adapter(adap);
  326. if (err < 0) {
  327. dev_err(&pdev->dev, "Can't add new i2c adapter\n");
  328. goto out;
  329. }
  330. clk_disable(clk);
  331. dev_info(&pdev->dev, " I2C adapter ready to operate\n");
  332. return 0;
  333. out:
  334. clk_disable(clk);
  335. err_clk_en:
  336. clk_unprepare(clk);
  337. err_clk_prep:
  338. clk_put(clk);
  339. err_get_clk:
  340. return err;
  341. }
  342. static int i2c_sirfsoc_remove(struct platform_device *pdev)
  343. {
  344. struct i2c_adapter *adapter = platform_get_drvdata(pdev);
  345. struct sirfsoc_i2c *siic = adapter->algo_data;
  346. writel(SIRFSOC_I2C_RESET, siic->base + SIRFSOC_I2C_CTRL);
  347. i2c_del_adapter(adapter);
  348. clk_unprepare(siic->clk);
  349. clk_put(siic->clk);
  350. return 0;
  351. }
  352. #ifdef CONFIG_PM
  353. static int i2c_sirfsoc_suspend(struct device *dev)
  354. {
  355. struct platform_device *pdev = to_platform_device(dev);
  356. struct i2c_adapter *adapter = platform_get_drvdata(pdev);
  357. struct sirfsoc_i2c *siic = adapter->algo_data;
  358. clk_enable(siic->clk);
  359. siic->sda_delay = readl(siic->base + SIRFSOC_I2C_SDA_DELAY);
  360. siic->clk_div = readl(siic->base + SIRFSOC_I2C_CLK_CTRL);
  361. clk_disable(siic->clk);
  362. return 0;
  363. }
  364. static int i2c_sirfsoc_resume(struct device *dev)
  365. {
  366. struct platform_device *pdev = to_platform_device(dev);
  367. struct i2c_adapter *adapter = platform_get_drvdata(pdev);
  368. struct sirfsoc_i2c *siic = adapter->algo_data;
  369. clk_enable(siic->clk);
  370. writel(SIRFSOC_I2C_RESET, siic->base + SIRFSOC_I2C_CTRL);
  371. while (readl(siic->base + SIRFSOC_I2C_CTRL) & SIRFSOC_I2C_RESET)
  372. cpu_relax();
  373. writel(SIRFSOC_I2C_CORE_EN | SIRFSOC_I2C_MASTER_MODE,
  374. siic->base + SIRFSOC_I2C_CTRL);
  375. writel(siic->clk_div, siic->base + SIRFSOC_I2C_CLK_CTRL);
  376. writel(siic->sda_delay, siic->base + SIRFSOC_I2C_SDA_DELAY);
  377. clk_disable(siic->clk);
  378. return 0;
  379. }
  380. static const struct dev_pm_ops i2c_sirfsoc_pm_ops = {
  381. .suspend = i2c_sirfsoc_suspend,
  382. .resume = i2c_sirfsoc_resume,
  383. };
  384. #endif
  385. static const struct of_device_id sirfsoc_i2c_of_match[] = {
  386. { .compatible = "sirf,prima2-i2c", },
  387. {},
  388. };
  389. MODULE_DEVICE_TABLE(of, sirfsoc_i2c_of_match);
  390. static struct platform_driver i2c_sirfsoc_driver = {
  391. .driver = {
  392. .name = "sirfsoc_i2c",
  393. #ifdef CONFIG_PM
  394. .pm = &i2c_sirfsoc_pm_ops,
  395. #endif
  396. .of_match_table = sirfsoc_i2c_of_match,
  397. },
  398. .probe = i2c_sirfsoc_probe,
  399. .remove = i2c_sirfsoc_remove,
  400. };
  401. module_platform_driver(i2c_sirfsoc_driver);
  402. MODULE_DESCRIPTION("SiRF SoC I2C master controller driver");
  403. MODULE_AUTHOR("Zhiwu Song <Zhiwu.Song@csr.com>, "
  404. "Xiangzhen Ye <Xiangzhen.Ye@csr.com>");
  405. MODULE_LICENSE("GPL v2");