rk_i2c.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. /*
  2. * (C) Copyright 2015 Google, Inc
  3. *
  4. * (C) Copyright 2008-2014 Rockchip Electronics
  5. * Peter, Software Engineering, <superpeter.cai@gmail.com>.
  6. *
  7. * SPDX-License-Identifier: GPL-2.0+
  8. */
  9. #include <common.h>
  10. #include <clk.h>
  11. #include <dm.h>
  12. #include <errno.h>
  13. #include <i2c.h>
  14. #include <asm/io.h>
  15. #include <asm/arch/clock.h>
  16. #include <asm/arch/i2c.h>
  17. #include <asm/arch/periph.h>
  18. #include <dm/pinctrl.h>
  19. #include <linux/sizes.h>
  20. DECLARE_GLOBAL_DATA_PTR;
  21. /* i2c timerout */
  22. #define I2C_TIMEOUT_MS 100
  23. #define I2C_RETRY_COUNT 3
  24. /* rk i2c fifo max transfer bytes */
  25. #define RK_I2C_FIFO_SIZE 32
  26. struct rk_i2c {
  27. struct clk clk;
  28. struct i2c_regs *regs;
  29. unsigned int speed;
  30. };
  31. static inline void rk_i2c_get_div(int div, int *divh, int *divl)
  32. {
  33. *divl = div / 2;
  34. if (div % 2 == 0)
  35. *divh = div / 2;
  36. else
  37. *divh = DIV_ROUND_UP(div, 2);
  38. }
  39. /*
  40. * SCL Divisor = 8 * (CLKDIVL+1 + CLKDIVH+1)
  41. * SCL = PCLK / SCLK Divisor
  42. * i2c_rate = PCLK
  43. */
  44. static void rk_i2c_set_clk(struct rk_i2c *i2c, uint32_t scl_rate)
  45. {
  46. uint32_t i2c_rate;
  47. int div, divl, divh;
  48. /* First get i2c rate from pclk */
  49. i2c_rate = clk_get_rate(&i2c->clk);
  50. div = DIV_ROUND_UP(i2c_rate, scl_rate * 8) - 2;
  51. divh = 0;
  52. divl = 0;
  53. if (div >= 0)
  54. rk_i2c_get_div(div, &divh, &divl);
  55. writel(I2C_CLKDIV_VAL(divl, divh), &i2c->regs->clkdiv);
  56. debug("rk_i2c_set_clk: i2c rate = %d, scl rate = %d\n", i2c_rate,
  57. scl_rate);
  58. debug("set i2c clk div = %d, divh = %d, divl = %d\n", div, divh, divl);
  59. debug("set clk(I2C_CLKDIV: 0x%08x)\n", readl(&i2c->regs->clkdiv));
  60. }
  61. static void rk_i2c_show_regs(struct i2c_regs *regs)
  62. {
  63. #ifdef DEBUG
  64. uint i;
  65. debug("i2c_con: 0x%08x\n", readl(&regs->con));
  66. debug("i2c_clkdiv: 0x%08x\n", readl(&regs->clkdiv));
  67. debug("i2c_mrxaddr: 0x%08x\n", readl(&regs->mrxaddr));
  68. debug("i2c_mrxraddR: 0x%08x\n", readl(&regs->mrxraddr));
  69. debug("i2c_mtxcnt: 0x%08x\n", readl(&regs->mtxcnt));
  70. debug("i2c_mrxcnt: 0x%08x\n", readl(&regs->mrxcnt));
  71. debug("i2c_ien: 0x%08x\n", readl(&regs->ien));
  72. debug("i2c_ipd: 0x%08x\n", readl(&regs->ipd));
  73. debug("i2c_fcnt: 0x%08x\n", readl(&regs->fcnt));
  74. for (i = 0; i < 8; i++)
  75. debug("i2c_txdata%d: 0x%08x\n", i, readl(&regs->txdata[i]));
  76. for (i = 0; i < 8; i++)
  77. debug("i2c_rxdata%d: 0x%08x\n", i, readl(&regs->rxdata[i]));
  78. #endif
  79. }
  80. static int rk_i2c_send_start_bit(struct rk_i2c *i2c)
  81. {
  82. struct i2c_regs *regs = i2c->regs;
  83. ulong start;
  84. debug("I2c Send Start bit.\n");
  85. writel(I2C_IPD_ALL_CLEAN, &regs->ipd);
  86. writel(I2C_CON_EN | I2C_CON_START, &regs->con);
  87. writel(I2C_STARTIEN, &regs->ien);
  88. start = get_timer(0);
  89. while (1) {
  90. if (readl(&regs->ipd) & I2C_STARTIPD) {
  91. writel(I2C_STARTIPD, &regs->ipd);
  92. break;
  93. }
  94. if (get_timer(start) > I2C_TIMEOUT_MS) {
  95. debug("I2C Send Start Bit Timeout\n");
  96. rk_i2c_show_regs(regs);
  97. return -ETIMEDOUT;
  98. }
  99. udelay(1);
  100. }
  101. return 0;
  102. }
  103. static int rk_i2c_send_stop_bit(struct rk_i2c *i2c)
  104. {
  105. struct i2c_regs *regs = i2c->regs;
  106. ulong start;
  107. debug("I2c Send Stop bit.\n");
  108. writel(I2C_IPD_ALL_CLEAN, &regs->ipd);
  109. writel(I2C_CON_EN | I2C_CON_STOP, &regs->con);
  110. writel(I2C_CON_STOP, &regs->ien);
  111. start = get_timer(0);
  112. while (1) {
  113. if (readl(&regs->ipd) & I2C_STOPIPD) {
  114. writel(I2C_STOPIPD, &regs->ipd);
  115. break;
  116. }
  117. if (get_timer(start) > I2C_TIMEOUT_MS) {
  118. debug("I2C Send Start Bit Timeout\n");
  119. rk_i2c_show_regs(regs);
  120. return -ETIMEDOUT;
  121. }
  122. udelay(1);
  123. }
  124. return 0;
  125. }
  126. static inline void rk_i2c_disable(struct rk_i2c *i2c)
  127. {
  128. writel(0, &i2c->regs->con);
  129. }
  130. static int rk_i2c_read(struct rk_i2c *i2c, uchar chip, uint reg, uint r_len,
  131. uchar *buf, uint b_len)
  132. {
  133. struct i2c_regs *regs = i2c->regs;
  134. uchar *pbuf = buf;
  135. uint bytes_remain_len = b_len;
  136. uint bytes_xferred = 0;
  137. uint words_xferred = 0;
  138. ulong start;
  139. uint con = 0;
  140. uint rxdata;
  141. uint i, j;
  142. int err;
  143. debug("rk_i2c_read: chip = %d, reg = %d, r_len = %d, b_len = %d\n",
  144. chip, reg, r_len, b_len);
  145. err = rk_i2c_send_start_bit(i2c);
  146. if (err)
  147. return err;
  148. writel(I2C_MRXADDR_SET(1, chip << 1 | 1), &regs->mrxaddr);
  149. if (r_len == 0) {
  150. writel(0, &regs->mrxraddr);
  151. } else if (r_len < 4) {
  152. writel(I2C_MRXRADDR_SET(r_len, reg), &regs->mrxraddr);
  153. } else {
  154. debug("I2C Read: addr len %d not supported\n", r_len);
  155. return -EIO;
  156. }
  157. while (bytes_remain_len) {
  158. if (bytes_remain_len > RK_I2C_FIFO_SIZE) {
  159. con = I2C_CON_EN | I2C_CON_MOD(I2C_MODE_TRX);
  160. bytes_xferred = 32;
  161. } else {
  162. con = I2C_CON_EN | I2C_CON_MOD(I2C_MODE_TRX) |
  163. I2C_CON_LASTACK;
  164. bytes_xferred = bytes_remain_len;
  165. }
  166. words_xferred = DIV_ROUND_UP(bytes_xferred, 4);
  167. writel(con, &regs->con);
  168. writel(bytes_xferred, &regs->mrxcnt);
  169. writel(I2C_MBRFIEN | I2C_NAKRCVIEN, &regs->ien);
  170. start = get_timer(0);
  171. while (1) {
  172. if (readl(&regs->ipd) & I2C_NAKRCVIPD) {
  173. writel(I2C_NAKRCVIPD, &regs->ipd);
  174. err = -EREMOTEIO;
  175. }
  176. if (readl(&regs->ipd) & I2C_MBRFIPD) {
  177. writel(I2C_MBRFIPD, &regs->ipd);
  178. break;
  179. }
  180. if (get_timer(start) > I2C_TIMEOUT_MS) {
  181. debug("I2C Read Data Timeout\n");
  182. err = -ETIMEDOUT;
  183. rk_i2c_show_regs(regs);
  184. goto i2c_exit;
  185. }
  186. udelay(1);
  187. }
  188. for (i = 0; i < words_xferred; i++) {
  189. rxdata = readl(&regs->rxdata[i]);
  190. debug("I2c Read RXDATA[%d] = 0x%x\n", i, rxdata);
  191. for (j = 0; j < 4; j++) {
  192. if ((i * 4 + j) == bytes_xferred)
  193. break;
  194. *pbuf++ = (rxdata >> (j * 8)) & 0xff;
  195. }
  196. }
  197. bytes_remain_len -= bytes_xferred;
  198. debug("I2C Read bytes_remain_len %d\n", bytes_remain_len);
  199. }
  200. i2c_exit:
  201. rk_i2c_send_stop_bit(i2c);
  202. rk_i2c_disable(i2c);
  203. return err;
  204. }
  205. static int rk_i2c_write(struct rk_i2c *i2c, uchar chip, uint reg, uint r_len,
  206. uchar *buf, uint b_len)
  207. {
  208. struct i2c_regs *regs = i2c->regs;
  209. int err;
  210. uchar *pbuf = buf;
  211. uint bytes_remain_len = b_len + r_len + 1;
  212. uint bytes_xferred = 0;
  213. uint words_xferred = 0;
  214. ulong start;
  215. uint txdata;
  216. uint i, j;
  217. debug("rk_i2c_write: chip = %d, reg = %d, r_len = %d, b_len = %d\n",
  218. chip, reg, r_len, b_len);
  219. err = rk_i2c_send_start_bit(i2c);
  220. if (err)
  221. return err;
  222. while (bytes_remain_len) {
  223. if (bytes_remain_len > RK_I2C_FIFO_SIZE)
  224. bytes_xferred = RK_I2C_FIFO_SIZE;
  225. else
  226. bytes_xferred = bytes_remain_len;
  227. words_xferred = DIV_ROUND_UP(bytes_xferred, 4);
  228. for (i = 0; i < words_xferred; i++) {
  229. txdata = 0;
  230. for (j = 0; j < 4; j++) {
  231. if ((i * 4 + j) == bytes_xferred)
  232. break;
  233. if (i == 0 && j == 0 && pbuf == buf) {
  234. txdata |= (chip << 1);
  235. } else if (i == 0 && j <= r_len && pbuf == buf) {
  236. txdata |= (reg &
  237. (0xff << ((j - 1) * 8))) << 8;
  238. } else {
  239. txdata |= (*pbuf++)<<(j * 8);
  240. }
  241. }
  242. writel(txdata, &regs->txdata[i]);
  243. debug("I2c Write TXDATA[%d] = 0x%08x\n", i, txdata);
  244. }
  245. writel(I2C_CON_EN | I2C_CON_MOD(I2C_MODE_TX), &regs->con);
  246. writel(bytes_xferred, &regs->mtxcnt);
  247. writel(I2C_MBTFIEN | I2C_NAKRCVIEN, &regs->ien);
  248. start = get_timer(0);
  249. while (1) {
  250. if (readl(&regs->ipd) & I2C_NAKRCVIPD) {
  251. writel(I2C_NAKRCVIPD, &regs->ipd);
  252. err = -EREMOTEIO;
  253. }
  254. if (readl(&regs->ipd) & I2C_MBTFIPD) {
  255. writel(I2C_MBTFIPD, &regs->ipd);
  256. break;
  257. }
  258. if (get_timer(start) > I2C_TIMEOUT_MS) {
  259. debug("I2C Write Data Timeout\n");
  260. err = -ETIMEDOUT;
  261. rk_i2c_show_regs(regs);
  262. goto i2c_exit;
  263. }
  264. udelay(1);
  265. }
  266. bytes_remain_len -= bytes_xferred;
  267. debug("I2C Write bytes_remain_len %d\n", bytes_remain_len);
  268. }
  269. i2c_exit:
  270. rk_i2c_send_stop_bit(i2c);
  271. rk_i2c_disable(i2c);
  272. return err;
  273. }
  274. static int rockchip_i2c_xfer(struct udevice *bus, struct i2c_msg *msg,
  275. int nmsgs)
  276. {
  277. struct rk_i2c *i2c = dev_get_priv(bus);
  278. int ret;
  279. debug("i2c_xfer: %d messages\n", nmsgs);
  280. for (; nmsgs > 0; nmsgs--, msg++) {
  281. debug("i2c_xfer: chip=0x%x, len=0x%x\n", msg->addr, msg->len);
  282. if (msg->flags & I2C_M_RD) {
  283. ret = rk_i2c_read(i2c, msg->addr, 0, 0, msg->buf,
  284. msg->len);
  285. } else {
  286. ret = rk_i2c_write(i2c, msg->addr, 0, 0, msg->buf,
  287. msg->len);
  288. }
  289. if (ret) {
  290. debug("i2c_write: error sending\n");
  291. return -EREMOTEIO;
  292. }
  293. }
  294. return 0;
  295. }
  296. int rockchip_i2c_set_bus_speed(struct udevice *bus, unsigned int speed)
  297. {
  298. struct rk_i2c *i2c = dev_get_priv(bus);
  299. rk_i2c_set_clk(i2c, speed);
  300. return 0;
  301. }
  302. static int rockchip_i2c_ofdata_to_platdata(struct udevice *bus)
  303. {
  304. struct rk_i2c *priv = dev_get_priv(bus);
  305. int ret;
  306. ret = clk_get_by_index(bus, 0, &priv->clk);
  307. if (ret < 0) {
  308. debug("%s: Could not get clock for %s: %d\n", __func__,
  309. bus->name, ret);
  310. return ret;
  311. }
  312. return 0;
  313. }
  314. static int rockchip_i2c_probe(struct udevice *bus)
  315. {
  316. struct rk_i2c *priv = dev_get_priv(bus);
  317. priv->regs = (void *)dev_get_addr(bus);
  318. return 0;
  319. }
  320. static const struct dm_i2c_ops rockchip_i2c_ops = {
  321. .xfer = rockchip_i2c_xfer,
  322. .set_bus_speed = rockchip_i2c_set_bus_speed,
  323. };
  324. static const struct udevice_id rockchip_i2c_ids[] = {
  325. { .compatible = "rockchip,rk3288-i2c" },
  326. { }
  327. };
  328. U_BOOT_DRIVER(i2c_rockchip) = {
  329. .name = "i2c_rockchip",
  330. .id = UCLASS_I2C,
  331. .of_match = rockchip_i2c_ids,
  332. .ofdata_to_platdata = rockchip_i2c_ofdata_to_platdata,
  333. .probe = rockchip_i2c_probe,
  334. .priv_auto_alloc_size = sizeof(struct rk_i2c),
  335. .ops = &rockchip_i2c_ops,
  336. };