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