meson_i2c.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /*
  2. * (C) Copyright 2017 - Beniamino Galvani <b.galvani@gmail.com>
  3. *
  4. * SPDX-License-Identifier: GPL-2.0+
  5. */
  6. #include <common.h>
  7. #include <asm/arch/i2c.h>
  8. #include <asm/io.h>
  9. #include <dm.h>
  10. #include <i2c.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 {
  41. struct i2c_regs *regs;
  42. struct i2c_msg *msg;
  43. bool last;
  44. uint count;
  45. uint pos;
  46. u32 tokens[2];
  47. uint num_tokens;
  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. static void meson_i2c_get_data(struct meson_i2c *i2c, u8 *buf, int len)
  64. {
  65. u32 rdata0, rdata1;
  66. int i;
  67. rdata0 = readl(&i2c->regs->tok_rdata0);
  68. rdata1 = readl(&i2c->regs->tok_rdata1);
  69. debug("meson i2c: read data %08x %08x len %d\n", rdata0, rdata1, len);
  70. for (i = 0; i < min(4, len); i++)
  71. *buf++ = (rdata0 >> i * 8) & 0xff;
  72. for (i = 4; i < min(8, len); i++)
  73. *buf++ = (rdata1 >> (i - 4) * 8) & 0xff;
  74. }
  75. static void meson_i2c_put_data(struct meson_i2c *i2c, u8 *buf, int len)
  76. {
  77. u32 wdata0 = 0, wdata1 = 0;
  78. int i;
  79. for (i = 0; i < min(4, len); i++)
  80. wdata0 |= *buf++ << (i * 8);
  81. for (i = 4; i < min(8, len); i++)
  82. wdata1 |= *buf++ << ((i - 4) * 8);
  83. writel(wdata0, &i2c->regs->tok_wdata0);
  84. writel(wdata1, &i2c->regs->tok_wdata1);
  85. debug("meson i2c: write data %08x %08x len %d\n", wdata0, wdata1, len);
  86. }
  87. static void meson_i2c_prepare_xfer(struct meson_i2c *i2c)
  88. {
  89. bool write = !(i2c->msg->flags & I2C_M_RD);
  90. int i;
  91. i2c->count = min(i2c->msg->len - i2c->pos, 8u);
  92. for (i = 0; i + 1 < i2c->count; i++)
  93. meson_i2c_add_token(i2c, TOKEN_DATA);
  94. if (i2c->count) {
  95. if (write || i2c->pos + i2c->count < i2c->msg->len)
  96. meson_i2c_add_token(i2c, TOKEN_DATA);
  97. else
  98. meson_i2c_add_token(i2c, TOKEN_DATA_LAST);
  99. }
  100. if (write)
  101. meson_i2c_put_data(i2c, i2c->msg->buf + i2c->pos, i2c->count);
  102. if (i2c->last && i2c->pos + i2c->count >= i2c->msg->len)
  103. meson_i2c_add_token(i2c, TOKEN_STOP);
  104. writel(i2c->tokens[0], &i2c->regs->tok_list0);
  105. writel(i2c->tokens[1], &i2c->regs->tok_list1);
  106. }
  107. static void meson_i2c_do_start(struct meson_i2c *i2c, struct i2c_msg *msg)
  108. {
  109. int token;
  110. token = (msg->flags & I2C_M_RD) ? TOKEN_SLAVE_ADDR_READ :
  111. TOKEN_SLAVE_ADDR_WRITE;
  112. writel(msg->addr << 1, &i2c->regs->slave_addr);
  113. meson_i2c_add_token(i2c, TOKEN_START);
  114. meson_i2c_add_token(i2c, token);
  115. }
  116. static int meson_i2c_xfer_msg(struct meson_i2c *i2c, struct i2c_msg *msg,
  117. int last)
  118. {
  119. ulong start;
  120. debug("meson i2c: %s addr %u len %u\n",
  121. (msg->flags & I2C_M_RD) ? "read" : "write",
  122. msg->addr, msg->len);
  123. i2c->msg = msg;
  124. i2c->last = last;
  125. i2c->pos = 0;
  126. i2c->count = 0;
  127. meson_i2c_reset_tokens(i2c);
  128. meson_i2c_do_start(i2c, msg);
  129. do {
  130. meson_i2c_prepare_xfer(i2c);
  131. /* start the transfer */
  132. setbits_le32(&i2c->regs->ctrl, REG_CTRL_START);
  133. start = get_timer(0);
  134. while (readl(&i2c->regs->ctrl) & REG_CTRL_STATUS) {
  135. if (get_timer(start) > I2C_TIMEOUT_MS) {
  136. clrbits_le32(&i2c->regs->ctrl, REG_CTRL_START);
  137. debug("meson i2c: timeout\n");
  138. return -ETIMEDOUT;
  139. }
  140. udelay(1);
  141. }
  142. meson_i2c_reset_tokens(i2c);
  143. clrbits_le32(&i2c->regs->ctrl, REG_CTRL_START);
  144. if (readl(&i2c->regs->ctrl) & REG_CTRL_ERROR) {
  145. debug("meson i2c: error\n");
  146. return -ENXIO;
  147. }
  148. if ((msg->flags & I2C_M_RD) && i2c->count) {
  149. meson_i2c_get_data(i2c, i2c->msg->buf + i2c->pos,
  150. i2c->count);
  151. }
  152. i2c->pos += i2c->count;
  153. } while (i2c->pos < msg->len);
  154. return 0;
  155. }
  156. static int meson_i2c_xfer(struct udevice *bus, struct i2c_msg *msg,
  157. int nmsgs)
  158. {
  159. struct meson_i2c *i2c = dev_get_priv(bus);
  160. int i, ret = 0;
  161. for (i = 0; i < nmsgs; i++) {
  162. ret = meson_i2c_xfer_msg(i2c, msg + i, i == nmsgs - 1);
  163. if (ret)
  164. return -EREMOTEIO;
  165. }
  166. return 0;
  167. }
  168. static int meson_i2c_set_bus_speed(struct udevice *bus, unsigned int speed)
  169. {
  170. struct meson_i2c *i2c = dev_get_priv(bus);
  171. unsigned int clk_rate = MESON_I2C_CLK_RATE;
  172. unsigned int div;
  173. div = DIV_ROUND_UP(clk_rate, speed * 4);
  174. /* clock divider has 12 bits */
  175. if (div >= (1 << 12)) {
  176. debug("meson i2c: requested bus frequency too low\n");
  177. div = (1 << 12) - 1;
  178. }
  179. clrsetbits_le32(&i2c->regs->ctrl, REG_CTRL_CLKDIV_MASK,
  180. (div & GENMASK(9, 0)) << REG_CTRL_CLKDIV_SHIFT);
  181. clrsetbits_le32(&i2c->regs->ctrl, REG_CTRL_CLKDIVEXT_MASK,
  182. (div >> 10) << REG_CTRL_CLKDIVEXT_SHIFT);
  183. debug("meson i2c: set clk %u, src %u, div %u\n", speed, clk_rate, div);
  184. return 0;
  185. }
  186. static int meson_i2c_probe(struct udevice *bus)
  187. {
  188. struct meson_i2c *i2c = dev_get_priv(bus);
  189. i2c->regs = dev_read_addr_ptr(bus);
  190. clrbits_le32(&i2c->regs->ctrl, REG_CTRL_START);
  191. return 0;
  192. }
  193. static const struct dm_i2c_ops meson_i2c_ops = {
  194. .xfer = meson_i2c_xfer,
  195. .set_bus_speed = meson_i2c_set_bus_speed,
  196. };
  197. static const struct udevice_id meson_i2c_ids[] = {
  198. { .compatible = "amlogic,meson6-i2c" },
  199. { .compatible = "amlogic,meson-gx-i2c" },
  200. { .compatible = "amlogic,meson-gxbb-i2c" },
  201. { }
  202. };
  203. U_BOOT_DRIVER(i2c_meson) = {
  204. .name = "i2c_meson",
  205. .id = UCLASS_I2C,
  206. .of_match = meson_i2c_ids,
  207. .probe = meson_i2c_probe,
  208. .priv_auto_alloc_size = sizeof(struct meson_i2c),
  209. .ops = &meson_i2c_ops,
  210. };