Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Stephen Warren | a214892 | 2016-08-08 09:41:34 -0600 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2016, NVIDIA CORPORATION. |
Stephen Warren | a214892 | 2016-08-08 09:41:34 -0600 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <common.h> |
| 7 | #include <dm.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 8 | #include <log.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 9 | #include <malloc.h> |
Simon Glass | 495a5dc | 2019-11-14 12:57:30 -0700 | [diff] [blame] | 10 | #include <time.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 11 | #include <asm/global_data.h> |
Stephen Warren | a214892 | 2016-08-08 09:41:34 -0600 | [diff] [blame] | 12 | #include <dm/lists.h> |
| 13 | #include <dm/root.h> |
| 14 | #include <mailbox.h> |
| 15 | #include <misc.h> |
| 16 | #include <asm/arch-tegra/bpmp_abi.h> |
| 17 | #include <asm/arch-tegra/ivc.h> |
Simon Glass | 4dcacfc | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 18 | #include <linux/bitops.h> |
Simon Glass | d66c5f7 | 2020-02-03 07:36:15 -0700 | [diff] [blame] | 19 | #include <linux/err.h> |
Simon Glass | bdd5f81 | 2023-09-14 18:21:46 -0600 | [diff] [blame] | 20 | #include <linux/printk.h> |
Stephen Warren | a214892 | 2016-08-08 09:41:34 -0600 | [diff] [blame] | 21 | |
| 22 | #define BPMP_IVC_FRAME_COUNT 1 |
| 23 | #define BPMP_IVC_FRAME_SIZE 128 |
| 24 | |
| 25 | #define BPMP_FLAG_DO_ACK BIT(0) |
| 26 | #define BPMP_FLAG_RING_DOORBELL BIT(1) |
| 27 | |
| 28 | DECLARE_GLOBAL_DATA_PTR; |
| 29 | |
| 30 | struct tegra186_bpmp { |
| 31 | struct mbox_chan mbox; |
| 32 | struct tegra_ivc ivc; |
| 33 | }; |
| 34 | |
| 35 | static int tegra186_bpmp_call(struct udevice *dev, int mrq, void *tx_msg, |
| 36 | int tx_size, void *rx_msg, int rx_size) |
| 37 | { |
| 38 | struct tegra186_bpmp *priv = dev_get_priv(dev); |
| 39 | int ret, err; |
| 40 | void *ivc_frame; |
| 41 | struct mrq_request *req; |
| 42 | struct mrq_response *resp; |
| 43 | ulong start_time; |
| 44 | |
| 45 | debug("%s(dev=%p, mrq=%u, tx_msg=%p, tx_size=%d, rx_msg=%p, rx_size=%d) (priv=%p)\n", |
| 46 | __func__, dev, mrq, tx_msg, tx_size, rx_msg, rx_size, priv); |
| 47 | |
| 48 | if ((tx_size > BPMP_IVC_FRAME_SIZE) || (rx_size > BPMP_IVC_FRAME_SIZE)) |
| 49 | return -EINVAL; |
| 50 | |
| 51 | ret = tegra_ivc_write_get_next_frame(&priv->ivc, &ivc_frame); |
| 52 | if (ret) { |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 53 | pr_err("tegra_ivc_write_get_next_frame() failed: %d\n", ret); |
Stephen Warren | a214892 | 2016-08-08 09:41:34 -0600 | [diff] [blame] | 54 | return ret; |
| 55 | } |
| 56 | |
| 57 | req = ivc_frame; |
| 58 | req->mrq = mrq; |
| 59 | req->flags = BPMP_FLAG_DO_ACK | BPMP_FLAG_RING_DOORBELL; |
| 60 | memcpy(req + 1, tx_msg, tx_size); |
| 61 | |
| 62 | ret = tegra_ivc_write_advance(&priv->ivc); |
| 63 | if (ret) { |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 64 | pr_err("tegra_ivc_write_advance() failed: %d\n", ret); |
Stephen Warren | a214892 | 2016-08-08 09:41:34 -0600 | [diff] [blame] | 65 | return ret; |
| 66 | } |
| 67 | |
| 68 | start_time = timer_get_us(); |
| 69 | for (;;) { |
| 70 | ret = tegra_ivc_channel_notified(&priv->ivc); |
| 71 | if (ret) { |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 72 | pr_err("tegra_ivc_channel_notified() failed: %d\n", ret); |
Stephen Warren | a214892 | 2016-08-08 09:41:34 -0600 | [diff] [blame] | 73 | return ret; |
| 74 | } |
| 75 | |
| 76 | ret = tegra_ivc_read_get_next_frame(&priv->ivc, &ivc_frame); |
| 77 | if (!ret) |
| 78 | break; |
| 79 | |
| 80 | /* Timeout 20ms; roughly 10x current max observed duration */ |
| 81 | if ((timer_get_us() - start_time) > 20 * 1000) { |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 82 | pr_err("tegra_ivc_read_get_next_frame() timed out (%d)\n", |
Stephen Warren | a214892 | 2016-08-08 09:41:34 -0600 | [diff] [blame] | 83 | ret); |
| 84 | return -ETIMEDOUT; |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | resp = ivc_frame; |
| 89 | err = resp->err; |
| 90 | if (!err && rx_msg && rx_size) |
| 91 | memcpy(rx_msg, resp + 1, rx_size); |
| 92 | |
| 93 | ret = tegra_ivc_read_advance(&priv->ivc); |
| 94 | if (ret) { |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 95 | pr_err("tegra_ivc_write_advance() failed: %d\n", ret); |
Stephen Warren | a214892 | 2016-08-08 09:41:34 -0600 | [diff] [blame] | 96 | return ret; |
| 97 | } |
| 98 | |
| 99 | if (err) { |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 100 | pr_err("BPMP responded with error %d\n", err); |
Stephen Warren | a214892 | 2016-08-08 09:41:34 -0600 | [diff] [blame] | 101 | /* err isn't a U-Boot error code, so don't that */ |
| 102 | return -EIO; |
| 103 | } |
| 104 | |
| 105 | return rx_size; |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * The BPMP exposes multiple different services. We create a sub-device for |
| 110 | * each separate type of service, since each device must be of the appropriate |
| 111 | * UCLASS. |
| 112 | */ |
| 113 | static int tegra186_bpmp_bind(struct udevice *dev) |
| 114 | { |
| 115 | int ret; |
| 116 | struct udevice *child; |
| 117 | |
| 118 | debug("%s(dev=%p)\n", __func__, dev); |
| 119 | |
| 120 | ret = device_bind_driver_to_node(dev, "tegra186_clk", "tegra186_clk", |
Simon Glass | e6dd8da | 2017-05-18 20:09:07 -0600 | [diff] [blame] | 121 | dev_ofnode(dev), &child); |
Stephen Warren | a214892 | 2016-08-08 09:41:34 -0600 | [diff] [blame] | 122 | if (ret) |
| 123 | return ret; |
| 124 | |
| 125 | ret = device_bind_driver_to_node(dev, "tegra186_reset", |
Simon Glass | e6dd8da | 2017-05-18 20:09:07 -0600 | [diff] [blame] | 126 | "tegra186_reset", dev_ofnode(dev), |
Stephen Warren | a214892 | 2016-08-08 09:41:34 -0600 | [diff] [blame] | 127 | &child); |
| 128 | if (ret) |
| 129 | return ret; |
| 130 | |
| 131 | ret = device_bind_driver_to_node(dev, "tegra186_power_domain", |
| 132 | "tegra186_power_domain", |
Simon Glass | e6dd8da | 2017-05-18 20:09:07 -0600 | [diff] [blame] | 133 | dev_ofnode(dev), &child); |
Stephen Warren | a214892 | 2016-08-08 09:41:34 -0600 | [diff] [blame] | 134 | if (ret) |
| 135 | return ret; |
| 136 | |
| 137 | ret = dm_scan_fdt_dev(dev); |
| 138 | if (ret) |
| 139 | return ret; |
| 140 | |
| 141 | return 0; |
| 142 | } |
| 143 | |
| 144 | static ulong tegra186_bpmp_get_shmem(struct udevice *dev, int index) |
| 145 | { |
| 146 | int ret; |
| 147 | struct fdtdec_phandle_args args; |
| 148 | fdt_addr_t reg; |
| 149 | |
Simon Glass | dd79d6e | 2017-01-17 16:52:55 -0700 | [diff] [blame] | 150 | ret = fdtdec_parse_phandle_with_args(gd->fdt_blob, dev_of_offset(dev), |
Stephen Warren | a214892 | 2016-08-08 09:41:34 -0600 | [diff] [blame] | 151 | "shmem", NULL, 0, index, &args); |
| 152 | if (ret < 0) { |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 153 | pr_err("fdtdec_parse_phandle_with_args() failed: %d\n", ret); |
Stephen Warren | a214892 | 2016-08-08 09:41:34 -0600 | [diff] [blame] | 154 | return ret; |
| 155 | } |
| 156 | |
| 157 | reg = fdtdec_get_addr_size_auto_noparent(gd->fdt_blob, args.node, |
| 158 | "reg", 0, NULL, true); |
| 159 | if (reg == FDT_ADDR_T_NONE) { |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 160 | pr_err("fdtdec_get_addr_size_auto_noparent() failed\n"); |
Stephen Warren | a214892 | 2016-08-08 09:41:34 -0600 | [diff] [blame] | 161 | return -ENODEV; |
| 162 | } |
| 163 | |
| 164 | return reg; |
| 165 | } |
| 166 | |
| 167 | static void tegra186_bpmp_ivc_notify(struct tegra_ivc *ivc) |
| 168 | { |
| 169 | struct tegra186_bpmp *priv = |
| 170 | container_of(ivc, struct tegra186_bpmp, ivc); |
| 171 | int ret; |
| 172 | |
| 173 | ret = mbox_send(&priv->mbox, NULL); |
| 174 | if (ret) |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 175 | pr_err("mbox_send() failed: %d\n", ret); |
Stephen Warren | a214892 | 2016-08-08 09:41:34 -0600 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | static int tegra186_bpmp_probe(struct udevice *dev) |
| 179 | { |
| 180 | struct tegra186_bpmp *priv = dev_get_priv(dev); |
| 181 | int ret; |
| 182 | ulong tx_base, rx_base, start_time; |
| 183 | |
| 184 | debug("%s(dev=%p) (priv=%p)\n", __func__, dev, priv); |
| 185 | |
| 186 | ret = mbox_get_by_index(dev, 0, &priv->mbox); |
| 187 | if (ret) { |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 188 | pr_err("mbox_get_by_index() failed: %d\n", ret); |
Stephen Warren | a214892 | 2016-08-08 09:41:34 -0600 | [diff] [blame] | 189 | return ret; |
| 190 | } |
| 191 | |
| 192 | tx_base = tegra186_bpmp_get_shmem(dev, 0); |
| 193 | if (IS_ERR_VALUE(tx_base)) { |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 194 | pr_err("tegra186_bpmp_get_shmem failed for tx_base\n"); |
Stephen Warren | a214892 | 2016-08-08 09:41:34 -0600 | [diff] [blame] | 195 | return tx_base; |
| 196 | } |
| 197 | rx_base = tegra186_bpmp_get_shmem(dev, 1); |
| 198 | if (IS_ERR_VALUE(rx_base)) { |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 199 | pr_err("tegra186_bpmp_get_shmem failed for rx_base\n"); |
Stephen Warren | a214892 | 2016-08-08 09:41:34 -0600 | [diff] [blame] | 200 | return rx_base; |
| 201 | } |
| 202 | debug("shmem: rx=%lx, tx=%lx\n", rx_base, tx_base); |
| 203 | |
| 204 | ret = tegra_ivc_init(&priv->ivc, rx_base, tx_base, BPMP_IVC_FRAME_COUNT, |
| 205 | BPMP_IVC_FRAME_SIZE, tegra186_bpmp_ivc_notify); |
| 206 | if (ret) { |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 207 | pr_err("tegra_ivc_init() failed: %d\n", ret); |
Stephen Warren | a214892 | 2016-08-08 09:41:34 -0600 | [diff] [blame] | 208 | return ret; |
| 209 | } |
| 210 | |
| 211 | tegra_ivc_channel_reset(&priv->ivc); |
| 212 | start_time = timer_get_us(); |
| 213 | for (;;) { |
| 214 | ret = tegra_ivc_channel_notified(&priv->ivc); |
| 215 | if (!ret) |
| 216 | break; |
| 217 | |
| 218 | /* Timeout 100ms */ |
| 219 | if ((timer_get_us() - start_time) > 100 * 1000) { |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 220 | pr_err("Initial IVC reset timed out (%d)\n", ret); |
Stephen Warren | a214892 | 2016-08-08 09:41:34 -0600 | [diff] [blame] | 221 | ret = -ETIMEDOUT; |
| 222 | goto err_free_mbox; |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | return 0; |
| 227 | |
| 228 | err_free_mbox: |
| 229 | mbox_free(&priv->mbox); |
| 230 | |
| 231 | return ret; |
| 232 | } |
| 233 | |
| 234 | static int tegra186_bpmp_remove(struct udevice *dev) |
| 235 | { |
| 236 | struct tegra186_bpmp *priv = dev_get_priv(dev); |
| 237 | |
| 238 | debug("%s(dev=%p) (priv=%p)\n", __func__, dev, priv); |
| 239 | |
| 240 | mbox_free(&priv->mbox); |
| 241 | |
| 242 | return 0; |
| 243 | } |
| 244 | |
| 245 | static struct misc_ops tegra186_bpmp_ops = { |
| 246 | .call = tegra186_bpmp_call, |
| 247 | }; |
| 248 | |
| 249 | static const struct udevice_id tegra186_bpmp_ids[] = { |
| 250 | { .compatible = "nvidia,tegra186-bpmp" }, |
| 251 | { } |
| 252 | }; |
| 253 | |
| 254 | U_BOOT_DRIVER(tegra186_bpmp) = { |
| 255 | .name = "tegra186_bpmp", |
| 256 | .id = UCLASS_MISC, |
| 257 | .of_match = tegra186_bpmp_ids, |
| 258 | .bind = tegra186_bpmp_bind, |
| 259 | .probe = tegra186_bpmp_probe, |
| 260 | .remove = tegra186_bpmp_remove, |
| 261 | .ops = &tegra186_bpmp_ops, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 262 | .priv_auto = sizeof(struct tegra186_bpmp), |
Stephen Warren | a214892 | 2016-08-08 09:41:34 -0600 | [diff] [blame] | 263 | }; |