Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Stephen Warren | 67a8348 | 2016-08-08 11:28:27 -0600 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2016, NVIDIA CORPORATION. |
Stephen Warren | 67a8348 | 2016-08-08 11:28:27 -0600 | [diff] [blame] | 4 | */ |
| 5 | |
Tom Rini | abb9a04 | 2024-05-18 20:20:43 -0600 | [diff] [blame] | 6 | #include <common.h> |
Stephen Warren | 67a8348 | 2016-08-08 11:28:27 -0600 | [diff] [blame] | 7 | #include <dm.h> |
| 8 | #include <i2c.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 9 | #include <log.h> |
Stephen Warren | 67a8348 | 2016-08-08 11:28:27 -0600 | [diff] [blame] | 10 | #include <misc.h> |
| 11 | #include <asm/arch-tegra/bpmp_abi.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 12 | #include <asm/global_data.h> |
Simon Glass | 4dcacfc | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 13 | #include <linux/bitops.h> |
Stephen Warren | 67a8348 | 2016-08-08 11:28:27 -0600 | [diff] [blame] | 14 | |
| 15 | DECLARE_GLOBAL_DATA_PTR; |
| 16 | |
| 17 | struct tegra186_bpmp_i2c { |
| 18 | uint32_t bpmp_bus_id; |
| 19 | }; |
| 20 | |
| 21 | static 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 | |
| 39 | static 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 Hiblot | 33c226c | 2018-12-11 19:56:34 +0100 | [diff] [blame] | 91 | static int tegra186_bpmp_probe_chip(struct udevice *bus, uint chip_addr, |
| 92 | uint chip_flags) |
| 93 | { |
| 94 | return 0; |
| 95 | } |
| 96 | |
Stephen Warren | 67a8348 | 2016-08-08 11:28:27 -0600 | [diff] [blame] | 97 | static int tegra186_bpmp_i2c_probe(struct udevice *dev) |
| 98 | { |
| 99 | struct tegra186_bpmp_i2c *priv = dev_get_priv(dev); |
Stephen Warren | 67a8348 | 2016-08-08 11:28:27 -0600 | [diff] [blame] | 100 | |
Simon Glass | dd79d6e | 2017-01-17 16:52:55 -0700 | [diff] [blame] | 101 | priv->bpmp_bus_id = fdtdec_get_uint(gd->fdt_blob, dev_of_offset(dev), |
Stephen Warren | 67a8348 | 2016-08-08 11:28:27 -0600 | [diff] [blame] | 102 | "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 Glass | f44b4bf | 2017-09-17 16:54:53 -0600 | [diff] [blame] | 105 | return -EINVAL; |
Stephen Warren | 67a8348 | 2016-08-08 11:28:27 -0600 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | return 0; |
| 109 | } |
| 110 | |
| 111 | static const struct dm_i2c_ops tegra186_bpmp_i2c_ops = { |
| 112 | .xfer = tegra186_bpmp_i2c_xfer, |
Jean-Jacques Hiblot | 33c226c | 2018-12-11 19:56:34 +0100 | [diff] [blame] | 113 | .probe_chip = tegra186_bpmp_probe_chip, |
Stephen Warren | 67a8348 | 2016-08-08 11:28:27 -0600 | [diff] [blame] | 114 | }; |
| 115 | |
| 116 | static const struct udevice_id tegra186_bpmp_i2c_ids[] = { |
| 117 | { .compatible = "nvidia,tegra186-bpmp-i2c" }, |
| 118 | { } |
| 119 | }; |
| 120 | |
| 121 | U_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 Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 126 | .priv_auto = sizeof(struct tegra186_bpmp_i2c), |
Stephen Warren | 67a8348 | 2016-08-08 11:28:27 -0600 | [diff] [blame] | 127 | .ops = &tegra186_bpmp_i2c_ops, |
| 128 | }; |