meson_i2c.c 7.3 KB

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