meson_i2c.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2017 - Beniamino Galvani <b.galvani@gmail.com>
  4. */
  5. #include <common.h>
  6. #include <asm/io.h>
  7. #include <clk.h>
  8. #include <dm.h>
  9. #include <i2c.h>
  10. #define I2C_TIMEOUT_MS 100
  11. /* Control register fields */
  12. #define REG_CTRL_START BIT(0)
  13. #define REG_CTRL_ACK_IGNORE BIT(1)
  14. #define REG_CTRL_STATUS BIT(2)
  15. #define REG_CTRL_ERROR BIT(3)
  16. #define REG_CTRL_CLKDIV_SHIFT 12
  17. #define REG_CTRL_CLKDIV_MASK GENMASK(21, 12)
  18. #define REG_CTRL_CLKDIVEXT_SHIFT 28
  19. #define REG_CTRL_CLKDIVEXT_MASK GENMASK(29, 28)
  20. enum {
  21. TOKEN_END = 0,
  22. TOKEN_START,
  23. TOKEN_SLAVE_ADDR_WRITE,
  24. TOKEN_SLAVE_ADDR_READ,
  25. TOKEN_DATA,
  26. TOKEN_DATA_LAST,
  27. TOKEN_STOP,
  28. };
  29. struct i2c_regs {
  30. u32 ctrl;
  31. u32 slave_addr;
  32. u32 tok_list0;
  33. u32 tok_list1;
  34. u32 tok_wdata0;
  35. u32 tok_wdata1;
  36. u32 tok_rdata0;
  37. u32 tok_rdata1;
  38. };
  39. struct meson_i2c {
  40. struct clk clk;
  41. struct i2c_regs *regs;
  42. struct i2c_msg *msg; /* Current I2C message */
  43. bool last; /* Whether the message is the last */
  44. uint count; /* Number of bytes in the current transfer */
  45. uint pos; /* Position of current transfer in message */
  46. u32 tokens[2]; /* Sequence of tokens to be written */
  47. uint num_tokens; /* Number of tokens to be written */
  48. };
  49. static void meson_i2c_reset_tokens(struct meson_i2c *i2c)
  50. {
  51. i2c->tokens[0] = 0;
  52. i2c->tokens[1] = 0;
  53. i2c->num_tokens = 0;
  54. }
  55. static void meson_i2c_add_token(struct meson_i2c *i2c, int token)
  56. {
  57. if (i2c->num_tokens < 8)
  58. i2c->tokens[0] |= (token & 0xf) << (i2c->num_tokens * 4);
  59. else
  60. i2c->tokens[1] |= (token & 0xf) << ((i2c->num_tokens % 8) * 4);
  61. i2c->num_tokens++;
  62. }
  63. /*
  64. * Retrieve data for the current transfer (which can be at most 8
  65. * bytes) from the device internal buffer.
  66. */
  67. static void meson_i2c_get_data(struct meson_i2c *i2c, u8 *buf, int len)
  68. {
  69. u32 rdata0, rdata1;
  70. int i;
  71. rdata0 = readl(&i2c->regs->tok_rdata0);
  72. rdata1 = readl(&i2c->regs->tok_rdata1);
  73. debug("meson i2c: read data %08x %08x len %d\n", rdata0, rdata1, len);
  74. for (i = 0; i < min(4, len); i++)
  75. *buf++ = (rdata0 >> i * 8) & 0xff;
  76. for (i = 4; i < min(8, len); i++)
  77. *buf++ = (rdata1 >> (i - 4) * 8) & 0xff;
  78. }
  79. /*
  80. * Write data for the current transfer (which can be at most 8 bytes)
  81. * to the device internal buffer.
  82. */
  83. static void meson_i2c_put_data(struct meson_i2c *i2c, u8 *buf, int len)
  84. {
  85. u32 wdata0 = 0, wdata1 = 0;
  86. int i;
  87. for (i = 0; i < min(4, len); i++)
  88. wdata0 |= *buf++ << (i * 8);
  89. for (i = 4; i < min(8, len); i++)
  90. wdata1 |= *buf++ << ((i - 4) * 8);
  91. writel(wdata0, &i2c->regs->tok_wdata0);
  92. writel(wdata1, &i2c->regs->tok_wdata1);
  93. debug("meson i2c: write data %08x %08x len %d\n", wdata0, wdata1, len);
  94. }
  95. /*
  96. * Prepare the next transfer: pick the next 8 bytes in the remaining
  97. * part of message and write tokens and data (if needed) to the
  98. * device.
  99. */
  100. static void meson_i2c_prepare_xfer(struct meson_i2c *i2c)
  101. {
  102. bool write = !(i2c->msg->flags & I2C_M_RD);
  103. int i;
  104. i2c->count = min(i2c->msg->len - i2c->pos, 8u);
  105. for (i = 0; i + 1 < i2c->count; i++)
  106. meson_i2c_add_token(i2c, TOKEN_DATA);
  107. if (i2c->count) {
  108. if (write || i2c->pos + i2c->count < i2c->msg->len)
  109. meson_i2c_add_token(i2c, TOKEN_DATA);
  110. else
  111. meson_i2c_add_token(i2c, TOKEN_DATA_LAST);
  112. }
  113. if (write)
  114. meson_i2c_put_data(i2c, i2c->msg->buf + i2c->pos, i2c->count);
  115. if (i2c->last && i2c->pos + i2c->count >= i2c->msg->len)
  116. meson_i2c_add_token(i2c, TOKEN_STOP);
  117. writel(i2c->tokens[0], &i2c->regs->tok_list0);
  118. writel(i2c->tokens[1], &i2c->regs->tok_list1);
  119. }
  120. static void meson_i2c_do_start(struct meson_i2c *i2c, struct i2c_msg *msg)
  121. {
  122. int token;
  123. token = (msg->flags & I2C_M_RD) ? TOKEN_SLAVE_ADDR_READ :
  124. TOKEN_SLAVE_ADDR_WRITE;
  125. writel(msg->addr << 1, &i2c->regs->slave_addr);
  126. meson_i2c_add_token(i2c, TOKEN_START);
  127. meson_i2c_add_token(i2c, token);
  128. }
  129. static int meson_i2c_xfer_msg(struct meson_i2c *i2c, struct i2c_msg *msg,
  130. int last)
  131. {
  132. ulong start;
  133. debug("meson i2c: %s addr %u len %u\n",
  134. (msg->flags & I2C_M_RD) ? "read" : "write",
  135. msg->addr, msg->len);
  136. i2c->msg = msg;
  137. i2c->last = last;
  138. i2c->pos = 0;
  139. i2c->count = 0;
  140. meson_i2c_reset_tokens(i2c);
  141. meson_i2c_do_start(i2c, msg);
  142. do {
  143. meson_i2c_prepare_xfer(i2c);
  144. /* start the transfer */
  145. setbits_le32(&i2c->regs->ctrl, REG_CTRL_START);
  146. start = get_timer(0);
  147. while (readl(&i2c->regs->ctrl) & REG_CTRL_STATUS) {
  148. if (get_timer(start) > I2C_TIMEOUT_MS) {
  149. clrbits_le32(&i2c->regs->ctrl, REG_CTRL_START);
  150. debug("meson i2c: timeout\n");
  151. return -ETIMEDOUT;
  152. }
  153. udelay(1);
  154. }
  155. meson_i2c_reset_tokens(i2c);
  156. clrbits_le32(&i2c->regs->ctrl, REG_CTRL_START);
  157. if (readl(&i2c->regs->ctrl) & REG_CTRL_ERROR) {
  158. debug("meson i2c: error\n");
  159. return -EREMOTEIO;
  160. }
  161. if ((msg->flags & I2C_M_RD) && i2c->count) {
  162. meson_i2c_get_data(i2c, i2c->msg->buf + i2c->pos,
  163. i2c->count);
  164. }
  165. i2c->pos += i2c->count;
  166. } while (i2c->pos < msg->len);
  167. return 0;
  168. }
  169. static int meson_i2c_xfer(struct udevice *bus, struct i2c_msg *msg,
  170. int nmsgs)
  171. {
  172. struct meson_i2c *i2c = dev_get_priv(bus);
  173. int i, ret = 0;
  174. for (i = 0; i < nmsgs; i++) {
  175. ret = meson_i2c_xfer_msg(i2c, msg + i, i == nmsgs - 1);
  176. if (ret)
  177. return ret;
  178. }
  179. return 0;
  180. }
  181. static int meson_i2c_set_bus_speed(struct udevice *bus, unsigned int speed)
  182. {
  183. struct meson_i2c *i2c = dev_get_priv(bus);
  184. ulong clk_rate;
  185. unsigned int div;
  186. clk_rate = clk_get_rate(&i2c->clk);
  187. if (IS_ERR_VALUE(clk_rate))
  188. return -EINVAL;
  189. div = DIV_ROUND_UP(clk_rate, speed * 4);
  190. /* clock divider has 12 bits */
  191. if (div >= (1 << 12)) {
  192. debug("meson i2c: requested bus frequency too low\n");
  193. div = (1 << 12) - 1;
  194. }
  195. clrsetbits_le32(&i2c->regs->ctrl, REG_CTRL_CLKDIV_MASK,
  196. (div & GENMASK(9, 0)) << REG_CTRL_CLKDIV_SHIFT);
  197. clrsetbits_le32(&i2c->regs->ctrl, REG_CTRL_CLKDIVEXT_MASK,
  198. (div >> 10) << REG_CTRL_CLKDIVEXT_SHIFT);
  199. debug("meson i2c: set clk %u, src %lu, div %u\n", speed, clk_rate, div);
  200. return 0;
  201. }
  202. static int meson_i2c_probe(struct udevice *bus)
  203. {
  204. struct meson_i2c *i2c = dev_get_priv(bus);
  205. int ret;
  206. ret = clk_get_by_index(bus, 0, &i2c->clk);
  207. if (ret < 0)
  208. return ret;
  209. ret = clk_enable(&i2c->clk);
  210. if (ret)
  211. return ret;
  212. i2c->regs = dev_read_addr_ptr(bus);
  213. clrbits_le32(&i2c->regs->ctrl, REG_CTRL_START);
  214. return 0;
  215. }
  216. static const struct dm_i2c_ops meson_i2c_ops = {
  217. .xfer = meson_i2c_xfer,
  218. .set_bus_speed = meson_i2c_set_bus_speed,
  219. };
  220. static const struct udevice_id meson_i2c_ids[] = {
  221. { .compatible = "amlogic,meson6-i2c" },
  222. { .compatible = "amlogic,meson-gx-i2c" },
  223. { .compatible = "amlogic,meson-gxbb-i2c" },
  224. { }
  225. };
  226. U_BOOT_DRIVER(i2c_meson) = {
  227. .name = "i2c_meson",
  228. .id = UCLASS_I2C,
  229. .of_match = meson_i2c_ids,
  230. .probe = meson_i2c_probe,
  231. .priv_auto_alloc_size = sizeof(struct meson_i2c),
  232. .ops = &meson_i2c_ops,
  233. };