tegra186_bpmp_i2c.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * Copyright (c) 2016, NVIDIA CORPORATION.
  3. *
  4. * SPDX-License-Identifier: GPL-2.0
  5. */
  6. #include <common.h>
  7. #include <dm.h>
  8. #include <i2c.h>
  9. #include <misc.h>
  10. #include <asm/arch-tegra/bpmp_abi.h>
  11. DECLARE_GLOBAL_DATA_PTR;
  12. struct tegra186_bpmp_i2c {
  13. uint32_t bpmp_bus_id;
  14. };
  15. static inline void serialize_u16(uint8_t **p, uint16_t val)
  16. {
  17. (*p)[0] = val & 0xff;
  18. (*p)[1] = val >> 8;
  19. (*p) += 2;
  20. }
  21. /* These just happen to have the same values as I2C_M_* and SERIALI2C_* */
  22. #define SUPPORTED_FLAGS \
  23. (I2C_M_TEN | \
  24. I2C_M_RD | \
  25. I2C_M_STOP | \
  26. I2C_M_NOSTART | \
  27. I2C_M_REV_DIR_ADDR | \
  28. I2C_M_IGNORE_NAK | \
  29. I2C_M_NO_RD_ACK | \
  30. I2C_M_RECV_LEN)
  31. static int tegra186_bpmp_i2c_xfer(struct udevice *dev, struct i2c_msg *msg,
  32. int nmsgs)
  33. {
  34. struct tegra186_bpmp_i2c *priv = dev_get_priv(dev);
  35. struct mrq_i2c_request req;
  36. struct mrq_i2c_response resp;
  37. uint8_t *p;
  38. int left, i, ret;
  39. req.cmd = CMD_I2C_XFER;
  40. req.xfer.bus_id = priv->bpmp_bus_id;
  41. p = &req.xfer.data_buf[0];
  42. left = ARRAY_SIZE(req.xfer.data_buf);
  43. for (i = 0; i < nmsgs; i++) {
  44. int len = 6;
  45. if (!(msg[i].flags & I2C_M_RD))
  46. len += msg[i].len;
  47. if ((len >= BIT(16)) || (len > left))
  48. return -ENOSPC;
  49. if (msg[i].flags & ~SUPPORTED_FLAGS)
  50. return -EINVAL;
  51. serialize_u16(&p, msg[i].addr);
  52. serialize_u16(&p, msg[i].flags);
  53. serialize_u16(&p, msg[i].len);
  54. if (!(msg[i].flags & I2C_M_RD)) {
  55. memcpy(p, msg[i].buf, msg[i].len);
  56. p += msg[i].len;
  57. }
  58. }
  59. req.xfer.data_size = p - &req.xfer.data_buf[0];
  60. ret = misc_call(dev->parent, MRQ_I2C, &req, sizeof(req), &resp,
  61. sizeof(resp));
  62. if (ret < 0)
  63. return ret;
  64. p = &resp.xfer.data_buf[0];
  65. left = resp.xfer.data_size;
  66. if (left > ARRAY_SIZE(resp.xfer.data_buf))
  67. return -EINVAL;
  68. for (i = 0; i < nmsgs; i++) {
  69. if (msg[i].flags & I2C_M_RD) {
  70. memcpy(msg[i].buf, p, msg[i].len);
  71. p += msg[i].len;
  72. }
  73. }
  74. return 0;
  75. }
  76. static int tegra186_bpmp_i2c_probe(struct udevice *dev)
  77. {
  78. struct tegra186_bpmp_i2c *priv = dev_get_priv(dev);
  79. priv->bpmp_bus_id = fdtdec_get_uint(gd->fdt_blob, dev->of_offset,
  80. "nvidia,bpmp-bus-id", U32_MAX);
  81. if (priv->bpmp_bus_id == U32_MAX) {
  82. debug("%s: could not parse nvidia,bpmp-bus-id\n", __func__);
  83. return -ENODEV;
  84. }
  85. return 0;
  86. }
  87. static const struct dm_i2c_ops tegra186_bpmp_i2c_ops = {
  88. .xfer = tegra186_bpmp_i2c_xfer,
  89. };
  90. static const struct udevice_id tegra186_bpmp_i2c_ids[] = {
  91. { .compatible = "nvidia,tegra186-bpmp-i2c" },
  92. { }
  93. };
  94. U_BOOT_DRIVER(i2c_gpio) = {
  95. .name = "tegra186_bpmp_i2c",
  96. .id = UCLASS_I2C,
  97. .of_match = tegra186_bpmp_i2c_ids,
  98. .probe = tegra186_bpmp_i2c_probe,
  99. .priv_auto_alloc_size = sizeof(struct tegra186_bpmp_i2c),
  100. .ops = &tegra186_bpmp_i2c_ops,
  101. };