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