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