1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2016, NVIDIA CORPORATION.
14 #include <asm/arch-tegra/bpmp_abi.h>
15 #include <asm/arch-tegra/ivc.h>
16 #include <linux/err.h>
18 #define BPMP_IVC_FRAME_COUNT 1
19 #define BPMP_IVC_FRAME_SIZE 128
21 #define BPMP_FLAG_DO_ACK BIT(0)
22 #define BPMP_FLAG_RING_DOORBELL BIT(1)
24 DECLARE_GLOBAL_DATA_PTR;
26 struct tegra186_bpmp {
27 struct mbox_chan mbox;
31 static int tegra186_bpmp_call(struct udevice *dev, int mrq, void *tx_msg,
32 int tx_size, void *rx_msg, int rx_size)
34 struct tegra186_bpmp *priv = dev_get_priv(dev);
37 struct mrq_request *req;
38 struct mrq_response *resp;
41 debug("%s(dev=%p, mrq=%u, tx_msg=%p, tx_size=%d, rx_msg=%p, rx_size=%d) (priv=%p)\n",
42 __func__, dev, mrq, tx_msg, tx_size, rx_msg, rx_size, priv);
44 if ((tx_size > BPMP_IVC_FRAME_SIZE) || (rx_size > BPMP_IVC_FRAME_SIZE))
47 ret = tegra_ivc_write_get_next_frame(&priv->ivc, &ivc_frame);
49 pr_err("tegra_ivc_write_get_next_frame() failed: %d\n", ret);
55 req->flags = BPMP_FLAG_DO_ACK | BPMP_FLAG_RING_DOORBELL;
56 memcpy(req + 1, tx_msg, tx_size);
58 ret = tegra_ivc_write_advance(&priv->ivc);
60 pr_err("tegra_ivc_write_advance() failed: %d\n", ret);
64 start_time = timer_get_us();
66 ret = tegra_ivc_channel_notified(&priv->ivc);
68 pr_err("tegra_ivc_channel_notified() failed: %d\n", ret);
72 ret = tegra_ivc_read_get_next_frame(&priv->ivc, &ivc_frame);
76 /* Timeout 20ms; roughly 10x current max observed duration */
77 if ((timer_get_us() - start_time) > 20 * 1000) {
78 pr_err("tegra_ivc_read_get_next_frame() timed out (%d)\n",
86 if (!err && rx_msg && rx_size)
87 memcpy(rx_msg, resp + 1, rx_size);
89 ret = tegra_ivc_read_advance(&priv->ivc);
91 pr_err("tegra_ivc_write_advance() failed: %d\n", ret);
96 pr_err("BPMP responded with error %d\n", err);
97 /* err isn't a U-Boot error code, so don't that */
105 * The BPMP exposes multiple different services. We create a sub-device for
106 * each separate type of service, since each device must be of the appropriate
109 static int tegra186_bpmp_bind(struct udevice *dev)
112 struct udevice *child;
114 debug("%s(dev=%p)\n", __func__, dev);
116 ret = device_bind_driver_to_node(dev, "tegra186_clk", "tegra186_clk",
117 dev_ofnode(dev), &child);
121 ret = device_bind_driver_to_node(dev, "tegra186_reset",
122 "tegra186_reset", dev_ofnode(dev),
127 ret = device_bind_driver_to_node(dev, "tegra186_power_domain",
128 "tegra186_power_domain",
129 dev_ofnode(dev), &child);
133 ret = dm_scan_fdt_dev(dev);
140 static ulong tegra186_bpmp_get_shmem(struct udevice *dev, int index)
143 struct fdtdec_phandle_args args;
146 ret = fdtdec_parse_phandle_with_args(gd->fdt_blob, dev_of_offset(dev),
147 "shmem", NULL, 0, index, &args);
149 pr_err("fdtdec_parse_phandle_with_args() failed: %d\n", ret);
153 reg = fdtdec_get_addr_size_auto_noparent(gd->fdt_blob, args.node,
154 "reg", 0, NULL, true);
155 if (reg == FDT_ADDR_T_NONE) {
156 pr_err("fdtdec_get_addr_size_auto_noparent() failed\n");
163 static void tegra186_bpmp_ivc_notify(struct tegra_ivc *ivc)
165 struct tegra186_bpmp *priv =
166 container_of(ivc, struct tegra186_bpmp, ivc);
169 ret = mbox_send(&priv->mbox, NULL);
171 pr_err("mbox_send() failed: %d\n", ret);
174 static int tegra186_bpmp_probe(struct udevice *dev)
176 struct tegra186_bpmp *priv = dev_get_priv(dev);
178 ulong tx_base, rx_base, start_time;
180 debug("%s(dev=%p) (priv=%p)\n", __func__, dev, priv);
182 ret = mbox_get_by_index(dev, 0, &priv->mbox);
184 pr_err("mbox_get_by_index() failed: %d\n", ret);
188 tx_base = tegra186_bpmp_get_shmem(dev, 0);
189 if (IS_ERR_VALUE(tx_base)) {
190 pr_err("tegra186_bpmp_get_shmem failed for tx_base\n");
193 rx_base = tegra186_bpmp_get_shmem(dev, 1);
194 if (IS_ERR_VALUE(rx_base)) {
195 pr_err("tegra186_bpmp_get_shmem failed for rx_base\n");
198 debug("shmem: rx=%lx, tx=%lx\n", rx_base, tx_base);
200 ret = tegra_ivc_init(&priv->ivc, rx_base, tx_base, BPMP_IVC_FRAME_COUNT,
201 BPMP_IVC_FRAME_SIZE, tegra186_bpmp_ivc_notify);
203 pr_err("tegra_ivc_init() failed: %d\n", ret);
207 tegra_ivc_channel_reset(&priv->ivc);
208 start_time = timer_get_us();
210 ret = tegra_ivc_channel_notified(&priv->ivc);
215 if ((timer_get_us() - start_time) > 100 * 1000) {
216 pr_err("Initial IVC reset timed out (%d)\n", ret);
225 mbox_free(&priv->mbox);
230 static int tegra186_bpmp_remove(struct udevice *dev)
232 struct tegra186_bpmp *priv = dev_get_priv(dev);
234 debug("%s(dev=%p) (priv=%p)\n", __func__, dev, priv);
236 mbox_free(&priv->mbox);
241 static struct misc_ops tegra186_bpmp_ops = {
242 .call = tegra186_bpmp_call,
245 static const struct udevice_id tegra186_bpmp_ids[] = {
246 { .compatible = "nvidia,tegra186-bpmp" },
250 U_BOOT_DRIVER(tegra186_bpmp) = {
251 .name = "tegra186_bpmp",
253 .of_match = tegra186_bpmp_ids,
254 .bind = tegra186_bpmp_bind,
255 .probe = tegra186_bpmp_probe,
256 .remove = tegra186_bpmp_remove,
257 .ops = &tegra186_bpmp_ops,
258 .priv_auto_alloc_size = sizeof(struct tegra186_bpmp),