Fabien Dessenne | e09846a | 2019-05-31 15:11:34 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause |
| 2 | /* |
| 3 | * Copyright (C) 2019, STMicroelectronics - All Rights Reserved |
| 4 | */ |
| 5 | #define pr_fmt(fmt) "%s: " fmt, __func__ |
| 6 | #include <common.h> |
| 7 | #include <dm.h> |
| 8 | #include <errno.h> |
| 9 | #include <fdtdec.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 10 | #include <log.h> |
Fabien Dessenne | e09846a | 2019-05-31 15:11:34 +0200 | [diff] [blame] | 11 | #include <remoteproc.h> |
| 12 | #include <reset.h> |
Fabien Dessenne | e09846a | 2019-05-31 15:11:34 +0200 | [diff] [blame] | 13 | #include <asm/io.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 14 | #include <dm/device_compat.h> |
Simon Glass | d66c5f7 | 2020-02-03 07:36:15 -0700 | [diff] [blame] | 15 | #include <linux/err.h> |
Fabien Dessenne | e09846a | 2019-05-31 15:11:34 +0200 | [diff] [blame] | 16 | |
Fabien Dessenne | e09846a | 2019-05-31 15:11:34 +0200 | [diff] [blame] | 17 | /** |
| 18 | * struct stm32_copro_privdata - power processor private data |
| 19 | * @reset_ctl: reset controller handle |
Patrick Delaunay | a841489 | 2020-10-15 15:01:12 +0200 | [diff] [blame] | 20 | * @hold_boot: hold boot controller handle |
Fabien Dessenne | ec452e1 | 2019-10-30 14:38:33 +0100 | [diff] [blame] | 21 | * @rsc_table_addr: resource table address |
Fabien Dessenne | e09846a | 2019-05-31 15:11:34 +0200 | [diff] [blame] | 22 | */ |
| 23 | struct stm32_copro_privdata { |
| 24 | struct reset_ctl reset_ctl; |
Patrick Delaunay | a841489 | 2020-10-15 15:01:12 +0200 | [diff] [blame] | 25 | struct reset_ctl hold_boot; |
Fabien Dessenne | ec452e1 | 2019-10-30 14:38:33 +0100 | [diff] [blame] | 26 | ulong rsc_table_addr; |
Fabien Dessenne | e09846a | 2019-05-31 15:11:34 +0200 | [diff] [blame] | 27 | }; |
| 28 | |
| 29 | /** |
| 30 | * stm32_copro_probe() - Basic probe |
| 31 | * @dev: corresponding STM32 remote processor device |
| 32 | * @return 0 if all went ok, else corresponding -ve error |
| 33 | */ |
| 34 | static int stm32_copro_probe(struct udevice *dev) |
| 35 | { |
| 36 | struct stm32_copro_privdata *priv; |
Patrick Delaunay | a841489 | 2020-10-15 15:01:12 +0200 | [diff] [blame] | 37 | int ret; |
Fabien Dessenne | e09846a | 2019-05-31 15:11:34 +0200 | [diff] [blame] | 38 | |
| 39 | priv = dev_get_priv(dev); |
| 40 | |
Patrick Delaunay | a841489 | 2020-10-15 15:01:12 +0200 | [diff] [blame] | 41 | ret = reset_get_by_name(dev, "mcu_rst", &priv->reset_ctl); |
| 42 | if (ret) { |
| 43 | dev_err(dev, "failed to get reset (%d)\n", ret); |
| 44 | return ret; |
Fabien Dessenne | e09846a | 2019-05-31 15:11:34 +0200 | [diff] [blame] | 45 | } |
| 46 | |
Patrick Delaunay | a841489 | 2020-10-15 15:01:12 +0200 | [diff] [blame] | 47 | ret = reset_get_by_name(dev, "hold_boot", &priv->hold_boot); |
Fabien Dessenne | e09846a | 2019-05-31 15:11:34 +0200 | [diff] [blame] | 48 | if (ret) { |
Patrick Delaunay | a841489 | 2020-10-15 15:01:12 +0200 | [diff] [blame] | 49 | dev_err(dev, "failed to get hold boot (%d)\n", ret); |
Fabien Dessenne | e09846a | 2019-05-31 15:11:34 +0200 | [diff] [blame] | 50 | return ret; |
| 51 | } |
| 52 | |
| 53 | dev_dbg(dev, "probed\n"); |
| 54 | |
| 55 | return 0; |
| 56 | } |
| 57 | |
| 58 | /** |
Fabien Dessenne | e09846a | 2019-05-31 15:11:34 +0200 | [diff] [blame] | 59 | * stm32_copro_device_to_virt() - Convert device address to virtual address |
| 60 | * @dev: corresponding STM32 remote processor device |
| 61 | * @da: device address |
Lokesh Vutla | e18166f | 2019-09-04 16:01:27 +0530 | [diff] [blame] | 62 | * @size: Size of the memory region @da is pointing to |
Fabien Dessenne | e09846a | 2019-05-31 15:11:34 +0200 | [diff] [blame] | 63 | * @return converted virtual address |
| 64 | */ |
Lokesh Vutla | e18166f | 2019-09-04 16:01:27 +0530 | [diff] [blame] | 65 | static void *stm32_copro_device_to_virt(struct udevice *dev, ulong da, |
| 66 | ulong size) |
Fabien Dessenne | e09846a | 2019-05-31 15:11:34 +0200 | [diff] [blame] | 67 | { |
Lokesh Vutla | e18166f | 2019-09-04 16:01:27 +0530 | [diff] [blame] | 68 | fdt32_t in_addr = cpu_to_be32(da), end_addr; |
Fabien Dessenne | e09846a | 2019-05-31 15:11:34 +0200 | [diff] [blame] | 69 | u64 paddr; |
| 70 | |
| 71 | paddr = dev_translate_dma_address(dev, &in_addr); |
| 72 | if (paddr == OF_BAD_ADDR) { |
| 73 | dev_err(dev, "Unable to convert address %ld\n", da); |
| 74 | return NULL; |
| 75 | } |
| 76 | |
Lokesh Vutla | e18166f | 2019-09-04 16:01:27 +0530 | [diff] [blame] | 77 | end_addr = cpu_to_be32(da + size - 1); |
| 78 | if (dev_translate_dma_address(dev, &end_addr) == OF_BAD_ADDR) { |
| 79 | dev_err(dev, "Unable to convert address %ld\n", da + size - 1); |
| 80 | return NULL; |
| 81 | } |
| 82 | |
Fabien Dessenne | e09846a | 2019-05-31 15:11:34 +0200 | [diff] [blame] | 83 | return phys_to_virt(paddr); |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * stm32_copro_load() - Loadup the STM32 remote processor |
| 88 | * @dev: corresponding STM32 remote processor device |
| 89 | * @addr: Address in memory where image is stored |
| 90 | * @size: Size in bytes of the image |
| 91 | * @return 0 if all went ok, else corresponding -ve error |
| 92 | */ |
| 93 | static int stm32_copro_load(struct udevice *dev, ulong addr, ulong size) |
| 94 | { |
| 95 | struct stm32_copro_privdata *priv; |
Fabien Dessenne | ec452e1 | 2019-10-30 14:38:33 +0100 | [diff] [blame] | 96 | ulong rsc_table_size; |
Fabien Dessenne | e09846a | 2019-05-31 15:11:34 +0200 | [diff] [blame] | 97 | int ret; |
| 98 | |
| 99 | priv = dev_get_priv(dev); |
| 100 | |
Patrick Delaunay | a841489 | 2020-10-15 15:01:12 +0200 | [diff] [blame] | 101 | ret = reset_assert(&priv->hold_boot); |
| 102 | if (ret) { |
| 103 | dev_err(dev, "Unable to assert hold boot (ret=%d)\n", ret); |
Fabien Dessenne | e09846a | 2019-05-31 15:11:34 +0200 | [diff] [blame] | 104 | return ret; |
Patrick Delaunay | a841489 | 2020-10-15 15:01:12 +0200 | [diff] [blame] | 105 | } |
Fabien Dessenne | e09846a | 2019-05-31 15:11:34 +0200 | [diff] [blame] | 106 | |
| 107 | ret = reset_assert(&priv->reset_ctl); |
| 108 | if (ret) { |
| 109 | dev_err(dev, "Unable to assert reset line (ret=%d)\n", ret); |
| 110 | return ret; |
| 111 | } |
| 112 | |
Fabien Dessenne | ec452e1 | 2019-10-30 14:38:33 +0100 | [diff] [blame] | 113 | if (rproc_elf32_load_rsc_table(dev, addr, size, &priv->rsc_table_addr, |
| 114 | &rsc_table_size)) { |
| 115 | priv->rsc_table_addr = 0; |
| 116 | dev_warn(dev, "No valid resource table for this firmware\n"); |
| 117 | } |
| 118 | |
Lokesh Vutla | b506cba | 2019-09-04 16:01:28 +0530 | [diff] [blame] | 119 | return rproc_elf32_load_image(dev, addr, size); |
Fabien Dessenne | e09846a | 2019-05-31 15:11:34 +0200 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | /** |
| 123 | * stm32_copro_start() - Start the STM32 remote processor |
| 124 | * @dev: corresponding STM32 remote processor device |
| 125 | * @return 0 if all went ok, else corresponding -ve error |
| 126 | */ |
| 127 | static int stm32_copro_start(struct udevice *dev) |
| 128 | { |
Fabien Dessenne | ec452e1 | 2019-10-30 14:38:33 +0100 | [diff] [blame] | 129 | struct stm32_copro_privdata *priv; |
Fabien Dessenne | e09846a | 2019-05-31 15:11:34 +0200 | [diff] [blame] | 130 | int ret; |
| 131 | |
Fabien Dessenne | ec452e1 | 2019-10-30 14:38:33 +0100 | [diff] [blame] | 132 | priv = dev_get_priv(dev); |
| 133 | |
Patrick Delaunay | a841489 | 2020-10-15 15:01:12 +0200 | [diff] [blame] | 134 | ret = reset_deassert(&priv->hold_boot); |
| 135 | if (ret) { |
| 136 | dev_err(dev, "Unable to deassert hold boot (ret=%d)\n", ret); |
Fabien Dessenne | e09846a | 2019-05-31 15:11:34 +0200 | [diff] [blame] | 137 | return ret; |
Patrick Delaunay | a841489 | 2020-10-15 15:01:12 +0200 | [diff] [blame] | 138 | } |
Fabien Dessenne | e09846a | 2019-05-31 15:11:34 +0200 | [diff] [blame] | 139 | |
| 140 | /* |
| 141 | * Once copro running, reset hold boot flag to avoid copro |
Patrick Delaunay | b78ceb4 | 2020-10-15 15:01:13 +0200 | [diff] [blame] | 142 | * rebooting autonomously (error should never occur) |
Fabien Dessenne | e09846a | 2019-05-31 15:11:34 +0200 | [diff] [blame] | 143 | */ |
Patrick Delaunay | a841489 | 2020-10-15 15:01:12 +0200 | [diff] [blame] | 144 | ret = reset_assert(&priv->hold_boot); |
| 145 | if (ret) |
| 146 | dev_err(dev, "Unable to assert hold boot (ret=%d)\n", ret); |
| 147 | |
Patrick Delaunay | b78ceb4 | 2020-10-15 15:01:13 +0200 | [diff] [blame] | 148 | /* indicates that copro is running */ |
| 149 | writel(TAMP_COPRO_STATE_CRUN, TAMP_COPRO_STATE); |
| 150 | /* Store rsc_address in bkp register */ |
| 151 | writel(priv->rsc_table_addr, TAMP_COPRO_RSC_TBL_ADDRESS); |
Fabien Dessenne | ec452e1 | 2019-10-30 14:38:33 +0100 | [diff] [blame] | 152 | |
Patrick Delaunay | b78ceb4 | 2020-10-15 15:01:13 +0200 | [diff] [blame] | 153 | return 0; |
Fabien Dessenne | e09846a | 2019-05-31 15:11:34 +0200 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | /** |
| 157 | * stm32_copro_reset() - Reset the STM32 remote processor |
| 158 | * @dev: corresponding STM32 remote processor device |
| 159 | * @return 0 if all went ok, else corresponding -ve error |
| 160 | */ |
| 161 | static int stm32_copro_reset(struct udevice *dev) |
| 162 | { |
| 163 | struct stm32_copro_privdata *priv; |
| 164 | int ret; |
| 165 | |
| 166 | priv = dev_get_priv(dev); |
| 167 | |
Patrick Delaunay | a841489 | 2020-10-15 15:01:12 +0200 | [diff] [blame] | 168 | ret = reset_assert(&priv->hold_boot); |
| 169 | if (ret) { |
| 170 | dev_err(dev, "Unable to assert hold boot (ret=%d)\n", ret); |
Fabien Dessenne | e09846a | 2019-05-31 15:11:34 +0200 | [diff] [blame] | 171 | return ret; |
Patrick Delaunay | a841489 | 2020-10-15 15:01:12 +0200 | [diff] [blame] | 172 | } |
Fabien Dessenne | e09846a | 2019-05-31 15:11:34 +0200 | [diff] [blame] | 173 | |
| 174 | ret = reset_assert(&priv->reset_ctl); |
| 175 | if (ret) { |
| 176 | dev_err(dev, "Unable to assert reset line (ret=%d)\n", ret); |
| 177 | return ret; |
| 178 | } |
| 179 | |
Fabien Dessenne | 4446d2f | 2019-10-30 14:38:31 +0100 | [diff] [blame] | 180 | writel(TAMP_COPRO_STATE_OFF, TAMP_COPRO_STATE); |
Fabien Dessenne | e09846a | 2019-05-31 15:11:34 +0200 | [diff] [blame] | 181 | |
| 182 | return 0; |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * stm32_copro_stop() - Stop the STM32 remote processor |
| 187 | * @dev: corresponding STM32 remote processor device |
| 188 | * @return 0 if all went ok, else corresponding -ve error |
| 189 | */ |
| 190 | static int stm32_copro_stop(struct udevice *dev) |
| 191 | { |
| 192 | return stm32_copro_reset(dev); |
| 193 | } |
| 194 | |
| 195 | /** |
| 196 | * stm32_copro_is_running() - Is the STM32 remote processor running |
| 197 | * @dev: corresponding STM32 remote processor device |
Fabien Dessenne | 4446d2f | 2019-10-30 14:38:31 +0100 | [diff] [blame] | 198 | * @return 0 if the remote processor is running, 1 otherwise |
Fabien Dessenne | e09846a | 2019-05-31 15:11:34 +0200 | [diff] [blame] | 199 | */ |
| 200 | static int stm32_copro_is_running(struct udevice *dev) |
| 201 | { |
Fabien Dessenne | 4446d2f | 2019-10-30 14:38:31 +0100 | [diff] [blame] | 202 | return (readl(TAMP_COPRO_STATE) == TAMP_COPRO_STATE_OFF); |
Fabien Dessenne | e09846a | 2019-05-31 15:11:34 +0200 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | static const struct dm_rproc_ops stm32_copro_ops = { |
| 206 | .load = stm32_copro_load, |
| 207 | .start = stm32_copro_start, |
| 208 | .stop = stm32_copro_stop, |
| 209 | .reset = stm32_copro_reset, |
| 210 | .is_running = stm32_copro_is_running, |
| 211 | .device_to_virt = stm32_copro_device_to_virt, |
| 212 | }; |
| 213 | |
| 214 | static const struct udevice_id stm32_copro_ids[] = { |
Patrick Delaunay | 26c24b4 | 2019-08-02 15:07:18 +0200 | [diff] [blame] | 215 | {.compatible = "st,stm32mp1-m4"}, |
Fabien Dessenne | e09846a | 2019-05-31 15:11:34 +0200 | [diff] [blame] | 216 | {} |
| 217 | }; |
| 218 | |
| 219 | U_BOOT_DRIVER(stm32_copro) = { |
| 220 | .name = "stm32_m4_proc", |
| 221 | .of_match = stm32_copro_ids, |
| 222 | .id = UCLASS_REMOTEPROC, |
| 223 | .ops = &stm32_copro_ops, |
| 224 | .probe = stm32_copro_probe, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 225 | .priv_auto = sizeof(struct stm32_copro_privdata), |
Fabien Dessenne | e09846a | 2019-05-31 15:11:34 +0200 | [diff] [blame] | 226 | }; |