1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2016, NVIDIA CORPORATION.
15 #include <asm/arch-tegra/bpmp_abi.h>
16 #include <asm/arch-tegra/ivc.h>
17 #include <linux/err.h>
19 #define BPMP_IVC_FRAME_COUNT 1
20 #define BPMP_IVC_FRAME_SIZE 128
22 #define BPMP_FLAG_DO_ACK BIT(0)
23 #define BPMP_FLAG_RING_DOORBELL BIT(1)
25 DECLARE_GLOBAL_DATA_PTR;
27 struct tegra186_bpmp {
28 struct mbox_chan mbox;
32 static int tegra186_bpmp_call(struct udevice *dev, int mrq, void *tx_msg,
33 int tx_size, void *rx_msg, int rx_size)
35 struct tegra186_bpmp *priv = dev_get_priv(dev);
38 struct mrq_request *req;
39 struct mrq_response *resp;
42 debug("%s(dev=%p, mrq=%u, tx_msg=%p, tx_size=%d, rx_msg=%p, rx_size=%d) (priv=%p)\n",
43 __func__, dev, mrq, tx_msg, tx_size, rx_msg, rx_size, priv);
45 if ((tx_size > BPMP_IVC_FRAME_SIZE) || (rx_size > BPMP_IVC_FRAME_SIZE))
48 ret = tegra_ivc_write_get_next_frame(&priv->ivc, &ivc_frame);
50 pr_err("tegra_ivc_write_get_next_frame() failed: %d\n", ret);
56 req->flags = BPMP_FLAG_DO_ACK | BPMP_FLAG_RING_DOORBELL;
57 memcpy(req + 1, tx_msg, tx_size);
59 ret = tegra_ivc_write_advance(&priv->ivc);
61 pr_err("tegra_ivc_write_advance() failed: %d\n", ret);
65 start_time = timer_get_us();
67 ret = tegra_ivc_channel_notified(&priv->ivc);
69 pr_err("tegra_ivc_channel_notified() failed: %d\n", ret);
73 ret = tegra_ivc_read_get_next_frame(&priv->ivc, &ivc_frame);
77 /* Timeout 20ms; roughly 10x current max observed duration */
78 if ((timer_get_us() - start_time) > 20 * 1000) {
79 pr_err("tegra_ivc_read_get_next_frame() timed out (%d)\n",
87 if (!err && rx_msg && rx_size)
88 memcpy(rx_msg, resp + 1, rx_size);
90 ret = tegra_ivc_read_advance(&priv->ivc);
92 pr_err("tegra_ivc_write_advance() failed: %d\n", ret);
97 pr_err("BPMP responded with error %d\n", err);
98 /* err isn't a U-Boot error code, so don't that */
106 * The BPMP exposes multiple different services. We create a sub-device for
107 * each separate type of service, since each device must be of the appropriate
110 static int tegra186_bpmp_bind(struct udevice *dev)
113 struct udevice *child;
115 debug("%s(dev=%p)\n", __func__, dev);
117 ret = device_bind_driver_to_node(dev, "tegra186_clk", "tegra186_clk",
118 dev_ofnode(dev), &child);
122 ret = device_bind_driver_to_node(dev, "tegra186_reset",
123 "tegra186_reset", dev_ofnode(dev),
128 ret = device_bind_driver_to_node(dev, "tegra186_power_domain",
129 "tegra186_power_domain",
130 dev_ofnode(dev), &child);
134 ret = dm_scan_fdt_dev(dev);
141 static ulong tegra186_bpmp_get_shmem(struct udevice *dev, int index)
144 struct fdtdec_phandle_args args;
147 ret = fdtdec_parse_phandle_with_args(gd->fdt_blob, dev_of_offset(dev),
148 "shmem", NULL, 0, index, &args);
150 pr_err("fdtdec_parse_phandle_with_args() failed: %d\n", ret);
154 reg = fdtdec_get_addr_size_auto_noparent(gd->fdt_blob, args.node,
155 "reg", 0, NULL, true);
156 if (reg == FDT_ADDR_T_NONE) {
157 pr_err("fdtdec_get_addr_size_auto_noparent() failed\n");
164 static void tegra186_bpmp_ivc_notify(struct tegra_ivc *ivc)
166 struct tegra186_bpmp *priv =
167 container_of(ivc, struct tegra186_bpmp, ivc);
170 ret = mbox_send(&priv->mbox, NULL);
172 pr_err("mbox_send() failed: %d\n", ret);
175 static int tegra186_bpmp_probe(struct udevice *dev)
177 struct tegra186_bpmp *priv = dev_get_priv(dev);
179 ulong tx_base, rx_base, start_time;
181 debug("%s(dev=%p) (priv=%p)\n", __func__, dev, priv);
183 ret = mbox_get_by_index(dev, 0, &priv->mbox);
185 pr_err("mbox_get_by_index() failed: %d\n", ret);
189 tx_base = tegra186_bpmp_get_shmem(dev, 0);
190 if (IS_ERR_VALUE(tx_base)) {
191 pr_err("tegra186_bpmp_get_shmem failed for tx_base\n");
194 rx_base = tegra186_bpmp_get_shmem(dev, 1);
195 if (IS_ERR_VALUE(rx_base)) {
196 pr_err("tegra186_bpmp_get_shmem failed for rx_base\n");
199 debug("shmem: rx=%lx, tx=%lx\n", rx_base, tx_base);
201 ret = tegra_ivc_init(&priv->ivc, rx_base, tx_base, BPMP_IVC_FRAME_COUNT,
202 BPMP_IVC_FRAME_SIZE, tegra186_bpmp_ivc_notify);
204 pr_err("tegra_ivc_init() failed: %d\n", ret);
208 tegra_ivc_channel_reset(&priv->ivc);
209 start_time = timer_get_us();
211 ret = tegra_ivc_channel_notified(&priv->ivc);
216 if ((timer_get_us() - start_time) > 100 * 1000) {
217 pr_err("Initial IVC reset timed out (%d)\n", ret);
226 mbox_free(&priv->mbox);
231 static int tegra186_bpmp_remove(struct udevice *dev)
233 struct tegra186_bpmp *priv = dev_get_priv(dev);
235 debug("%s(dev=%p) (priv=%p)\n", __func__, dev, priv);
237 mbox_free(&priv->mbox);
242 static struct misc_ops tegra186_bpmp_ops = {
243 .call = tegra186_bpmp_call,
246 static const struct udevice_id tegra186_bpmp_ids[] = {
247 { .compatible = "nvidia,tegra186-bpmp" },
251 U_BOOT_DRIVER(tegra186_bpmp) = {
252 .name = "tegra186_bpmp",
254 .of_match = tegra186_bpmp_ids,
255 .bind = tegra186_bpmp_bind,
256 .probe = tegra186_bpmp_probe,
257 .remove = tegra186_bpmp_remove,
258 .ops = &tegra186_bpmp_ops,
259 .priv_auto_alloc_size = sizeof(struct tegra186_bpmp),