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