meson_i2c.c 7.2 KB

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