tegra186_bpmp_i2c.c 2.8 KB

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