Fabien Dessenne | 13140de | 2019-05-14 10:26:58 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * Copyright (C) STMicroelectronics 2018 - All Rights Reserved |
| 4 | * Authors: Ludovic Barre <ludovic.barre@st.com> for STMicroelectronics. |
| 5 | * Fabien Dessenne <fabien.dessenne@st.com> for STMicroelectronics. |
| 6 | */ |
| 7 | |
| 8 | #include <linux/arm-smccc.h> |
| 9 | #include <linux/dma-mapping.h> |
| 10 | #include <linux/interrupt.h> |
| 11 | #include <linux/io.h> |
| 12 | #include <linux/mailbox_client.h> |
| 13 | #include <linux/mfd/syscon.h> |
| 14 | #include <linux/module.h> |
| 15 | #include <linux/of_address.h> |
| 16 | #include <linux/of_device.h> |
| 17 | #include <linux/of_reserved_mem.h> |
Fabien Dessenne | 410119e | 2019-08-26 17:38:43 +0200 | [diff] [blame] | 18 | #include <linux/pm_wakeirq.h> |
Fabien Dessenne | 13140de | 2019-05-14 10:26:58 +0200 | [diff] [blame] | 19 | #include <linux/regmap.h> |
| 20 | #include <linux/remoteproc.h> |
| 21 | #include <linux/reset.h> |
Arnaud Pouliquen | 714cf5e | 2019-10-25 10:56:39 +0200 | [diff] [blame] | 22 | #include <linux/workqueue.h> |
Fabien Dessenne | 13140de | 2019-05-14 10:26:58 +0200 | [diff] [blame] | 23 | |
| 24 | #include "remoteproc_internal.h" |
| 25 | |
| 26 | #define HOLD_BOOT 0 |
| 27 | #define RELEASE_BOOT 1 |
| 28 | |
| 29 | #define MBOX_NB_VQ 2 |
| 30 | #define MBOX_NB_MBX 3 |
| 31 | |
| 32 | #define STM32_SMC_RCC 0x82001000 |
| 33 | #define STM32_SMC_REG_WRITE 0x1 |
| 34 | |
| 35 | #define STM32_MBX_VQ0 "vq0" |
Arnaud Pouliquen | 714cf5e | 2019-10-25 10:56:39 +0200 | [diff] [blame] | 36 | #define STM32_MBX_VQ0_ID 0 |
Fabien Dessenne | 13140de | 2019-05-14 10:26:58 +0200 | [diff] [blame] | 37 | #define STM32_MBX_VQ1 "vq1" |
Arnaud Pouliquen | 714cf5e | 2019-10-25 10:56:39 +0200 | [diff] [blame] | 38 | #define STM32_MBX_VQ1_ID 1 |
Fabien Dessenne | 13140de | 2019-05-14 10:26:58 +0200 | [diff] [blame] | 39 | #define STM32_MBX_SHUTDOWN "shutdown" |
| 40 | |
| 41 | struct stm32_syscon { |
| 42 | struct regmap *map; |
| 43 | u32 reg; |
| 44 | u32 mask; |
| 45 | }; |
| 46 | |
| 47 | struct stm32_rproc_mem { |
| 48 | char name[20]; |
| 49 | void __iomem *cpu_addr; |
| 50 | phys_addr_t bus_addr; |
| 51 | u32 dev_addr; |
| 52 | size_t size; |
| 53 | }; |
| 54 | |
| 55 | struct stm32_rproc_mem_ranges { |
| 56 | u32 dev_addr; |
| 57 | u32 bus_addr; |
| 58 | u32 size; |
| 59 | }; |
| 60 | |
| 61 | struct stm32_mbox { |
| 62 | const unsigned char name[10]; |
| 63 | struct mbox_chan *chan; |
| 64 | struct mbox_client client; |
Arnaud Pouliquen | 714cf5e | 2019-10-25 10:56:39 +0200 | [diff] [blame] | 65 | struct work_struct vq_work; |
Fabien Dessenne | 13140de | 2019-05-14 10:26:58 +0200 | [diff] [blame] | 66 | int vq_id; |
| 67 | }; |
| 68 | |
| 69 | struct stm32_rproc { |
| 70 | struct reset_control *rst; |
| 71 | struct stm32_syscon hold_boot; |
| 72 | struct stm32_syscon pdds; |
Fabien Dessenne | 410119e | 2019-08-26 17:38:43 +0200 | [diff] [blame] | 73 | int wdg_irq; |
Fabien Dessenne | 13140de | 2019-05-14 10:26:58 +0200 | [diff] [blame] | 74 | u32 nb_rmems; |
| 75 | struct stm32_rproc_mem *rmems; |
| 76 | struct stm32_mbox mb[MBOX_NB_MBX]; |
Arnaud Pouliquen | 714cf5e | 2019-10-25 10:56:39 +0200 | [diff] [blame] | 77 | struct workqueue_struct *workqueue; |
Fabien Dessenne | 13140de | 2019-05-14 10:26:58 +0200 | [diff] [blame] | 78 | bool secured_soc; |
| 79 | }; |
| 80 | |
| 81 | static int stm32_rproc_pa_to_da(struct rproc *rproc, phys_addr_t pa, u64 *da) |
| 82 | { |
| 83 | unsigned int i; |
| 84 | struct stm32_rproc *ddata = rproc->priv; |
| 85 | struct stm32_rproc_mem *p_mem; |
| 86 | |
| 87 | for (i = 0; i < ddata->nb_rmems; i++) { |
| 88 | p_mem = &ddata->rmems[i]; |
| 89 | |
| 90 | if (pa < p_mem->bus_addr || |
| 91 | pa >= p_mem->bus_addr + p_mem->size) |
| 92 | continue; |
| 93 | *da = pa - p_mem->bus_addr + p_mem->dev_addr; |
| 94 | dev_dbg(rproc->dev.parent, "pa %pa to da %llx\n", &pa, *da); |
| 95 | return 0; |
| 96 | } |
| 97 | |
| 98 | return -EINVAL; |
| 99 | } |
| 100 | |
| 101 | static int stm32_rproc_mem_alloc(struct rproc *rproc, |
| 102 | struct rproc_mem_entry *mem) |
| 103 | { |
| 104 | struct device *dev = rproc->dev.parent; |
| 105 | void *va; |
| 106 | |
| 107 | dev_dbg(dev, "map memory: %pa+%x\n", &mem->dma, mem->len); |
| 108 | va = ioremap_wc(mem->dma, mem->len); |
| 109 | if (IS_ERR_OR_NULL(va)) { |
| 110 | dev_err(dev, "Unable to map memory region: %pa+%x\n", |
| 111 | &mem->dma, mem->len); |
| 112 | return -ENOMEM; |
| 113 | } |
| 114 | |
| 115 | /* Update memory entry va */ |
| 116 | mem->va = va; |
| 117 | |
| 118 | return 0; |
| 119 | } |
| 120 | |
| 121 | static int stm32_rproc_mem_release(struct rproc *rproc, |
| 122 | struct rproc_mem_entry *mem) |
| 123 | { |
| 124 | dev_dbg(rproc->dev.parent, "unmap memory: %pa\n", &mem->dma); |
| 125 | iounmap(mem->va); |
| 126 | |
| 127 | return 0; |
| 128 | } |
| 129 | |
| 130 | static int stm32_rproc_of_memory_translations(struct rproc *rproc) |
| 131 | { |
| 132 | struct device *parent, *dev = rproc->dev.parent; |
| 133 | struct stm32_rproc *ddata = rproc->priv; |
| 134 | struct device_node *np; |
| 135 | struct stm32_rproc_mem *p_mems; |
| 136 | struct stm32_rproc_mem_ranges *mem_range; |
| 137 | int cnt, array_size, i, ret = 0; |
| 138 | |
| 139 | parent = dev->parent; |
| 140 | np = parent->of_node; |
| 141 | |
| 142 | cnt = of_property_count_elems_of_size(np, "dma-ranges", |
| 143 | sizeof(*mem_range)); |
| 144 | if (cnt <= 0) { |
| 145 | dev_err(dev, "%s: dma-ranges property not defined\n", __func__); |
| 146 | return -EINVAL; |
| 147 | } |
| 148 | |
| 149 | p_mems = devm_kcalloc(dev, cnt, sizeof(*p_mems), GFP_KERNEL); |
| 150 | if (!p_mems) |
| 151 | return -ENOMEM; |
| 152 | mem_range = kcalloc(cnt, sizeof(*mem_range), GFP_KERNEL); |
| 153 | if (!mem_range) |
| 154 | return -ENOMEM; |
| 155 | |
| 156 | array_size = cnt * sizeof(struct stm32_rproc_mem_ranges) / sizeof(u32); |
| 157 | |
| 158 | ret = of_property_read_u32_array(np, "dma-ranges", |
| 159 | (u32 *)mem_range, array_size); |
| 160 | if (ret) { |
| 161 | dev_err(dev, "error while get dma-ranges property: %x\n", ret); |
| 162 | goto free_mem; |
| 163 | } |
| 164 | |
| 165 | for (i = 0; i < cnt; i++) { |
| 166 | p_mems[i].bus_addr = mem_range[i].bus_addr; |
| 167 | p_mems[i].dev_addr = mem_range[i].dev_addr; |
| 168 | p_mems[i].size = mem_range[i].size; |
| 169 | |
| 170 | dev_dbg(dev, "memory range[%i]: da %#x, pa %pa, size %#zx:\n", |
| 171 | i, p_mems[i].dev_addr, &p_mems[i].bus_addr, |
| 172 | p_mems[i].size); |
| 173 | } |
| 174 | |
| 175 | ddata->rmems = p_mems; |
| 176 | ddata->nb_rmems = cnt; |
| 177 | |
| 178 | free_mem: |
| 179 | kfree(mem_range); |
| 180 | return ret; |
| 181 | } |
| 182 | |
| 183 | static int stm32_rproc_mbox_idx(struct rproc *rproc, const unsigned char *name) |
| 184 | { |
| 185 | struct stm32_rproc *ddata = rproc->priv; |
| 186 | int i; |
| 187 | |
| 188 | for (i = 0; i < ARRAY_SIZE(ddata->mb); i++) { |
| 189 | if (!strncmp(ddata->mb[i].name, name, strlen(name))) |
| 190 | return i; |
| 191 | } |
| 192 | dev_err(&rproc->dev, "mailbox %s not found\n", name); |
| 193 | |
| 194 | return -EINVAL; |
| 195 | } |
| 196 | |
| 197 | static int stm32_rproc_elf_load_rsc_table(struct rproc *rproc, |
| 198 | const struct firmware *fw) |
| 199 | { |
| 200 | if (rproc_elf_load_rsc_table(rproc, fw)) |
| 201 | dev_warn(&rproc->dev, "no resource table found for this firmware\n"); |
| 202 | |
| 203 | return 0; |
| 204 | } |
| 205 | |
| 206 | static int stm32_rproc_parse_fw(struct rproc *rproc, const struct firmware *fw) |
| 207 | { |
| 208 | struct device *dev = rproc->dev.parent; |
| 209 | struct device_node *np = dev->of_node; |
| 210 | struct of_phandle_iterator it; |
| 211 | struct rproc_mem_entry *mem; |
| 212 | struct reserved_mem *rmem; |
| 213 | u64 da; |
| 214 | int index = 0; |
| 215 | |
| 216 | /* Register associated reserved memory regions */ |
| 217 | of_phandle_iterator_init(&it, np, "memory-region", NULL, 0); |
| 218 | while (of_phandle_iterator_next(&it) == 0) { |
| 219 | rmem = of_reserved_mem_lookup(it.node); |
| 220 | if (!rmem) { |
| 221 | dev_err(dev, "unable to acquire memory-region\n"); |
| 222 | return -EINVAL; |
| 223 | } |
| 224 | |
| 225 | if (stm32_rproc_pa_to_da(rproc, rmem->base, &da) < 0) { |
| 226 | dev_err(dev, "memory region not valid %pa\n", |
| 227 | &rmem->base); |
| 228 | return -EINVAL; |
| 229 | } |
| 230 | |
| 231 | /* No need to map vdev buffer */ |
| 232 | if (strcmp(it.node->name, "vdev0buffer")) { |
| 233 | /* Register memory region */ |
| 234 | mem = rproc_mem_entry_init(dev, NULL, |
| 235 | (dma_addr_t)rmem->base, |
| 236 | rmem->size, da, |
| 237 | stm32_rproc_mem_alloc, |
| 238 | stm32_rproc_mem_release, |
| 239 | it.node->name); |
| 240 | |
| 241 | if (mem) |
| 242 | rproc_coredump_add_segment(rproc, da, |
| 243 | rmem->size); |
| 244 | } else { |
| 245 | /* Register reserved memory for vdev buffer alloc */ |
| 246 | mem = rproc_of_resm_mem_entry_init(dev, index, |
| 247 | rmem->size, |
| 248 | rmem->base, |
| 249 | it.node->name); |
| 250 | } |
| 251 | |
| 252 | if (!mem) |
| 253 | return -ENOMEM; |
| 254 | |
| 255 | rproc_add_carveout(rproc, mem); |
| 256 | index++; |
| 257 | } |
| 258 | |
| 259 | return stm32_rproc_elf_load_rsc_table(rproc, fw); |
| 260 | } |
| 261 | |
| 262 | static irqreturn_t stm32_rproc_wdg(int irq, void *data) |
| 263 | { |
| 264 | struct rproc *rproc = data; |
| 265 | |
| 266 | rproc_report_crash(rproc, RPROC_WATCHDOG); |
| 267 | |
| 268 | return IRQ_HANDLED; |
| 269 | } |
| 270 | |
Arnaud Pouliquen | 714cf5e | 2019-10-25 10:56:39 +0200 | [diff] [blame] | 271 | static void stm32_rproc_mb_vq_work(struct work_struct *work) |
| 272 | { |
| 273 | struct stm32_mbox *mb = container_of(work, struct stm32_mbox, vq_work); |
| 274 | struct rproc *rproc = dev_get_drvdata(mb->client.dev); |
| 275 | |
| 276 | if (rproc_vq_interrupt(rproc, mb->vq_id) == IRQ_NONE) |
| 277 | dev_dbg(&rproc->dev, "no message found in vq%d\n", mb->vq_id); |
| 278 | } |
| 279 | |
Fabien Dessenne | 13140de | 2019-05-14 10:26:58 +0200 | [diff] [blame] | 280 | static void stm32_rproc_mb_callback(struct mbox_client *cl, void *data) |
| 281 | { |
| 282 | struct rproc *rproc = dev_get_drvdata(cl->dev); |
| 283 | struct stm32_mbox *mb = container_of(cl, struct stm32_mbox, client); |
Arnaud Pouliquen | 714cf5e | 2019-10-25 10:56:39 +0200 | [diff] [blame] | 284 | struct stm32_rproc *ddata = rproc->priv; |
Fabien Dessenne | 13140de | 2019-05-14 10:26:58 +0200 | [diff] [blame] | 285 | |
Arnaud Pouliquen | 714cf5e | 2019-10-25 10:56:39 +0200 | [diff] [blame] | 286 | queue_work(ddata->workqueue, &mb->vq_work); |
Fabien Dessenne | 13140de | 2019-05-14 10:26:58 +0200 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | static void stm32_rproc_free_mbox(struct rproc *rproc) |
| 290 | { |
| 291 | struct stm32_rproc *ddata = rproc->priv; |
| 292 | unsigned int i; |
| 293 | |
| 294 | for (i = 0; i < ARRAY_SIZE(ddata->mb); i++) { |
| 295 | if (ddata->mb[i].chan) |
| 296 | mbox_free_channel(ddata->mb[i].chan); |
| 297 | ddata->mb[i].chan = NULL; |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | static const struct stm32_mbox stm32_rproc_mbox[MBOX_NB_MBX] = { |
| 302 | { |
| 303 | .name = STM32_MBX_VQ0, |
Arnaud Pouliquen | 714cf5e | 2019-10-25 10:56:39 +0200 | [diff] [blame] | 304 | .vq_id = STM32_MBX_VQ0_ID, |
Fabien Dessenne | 13140de | 2019-05-14 10:26:58 +0200 | [diff] [blame] | 305 | .client = { |
| 306 | .rx_callback = stm32_rproc_mb_callback, |
| 307 | .tx_block = false, |
| 308 | }, |
| 309 | }, |
| 310 | { |
| 311 | .name = STM32_MBX_VQ1, |
Arnaud Pouliquen | 714cf5e | 2019-10-25 10:56:39 +0200 | [diff] [blame] | 312 | .vq_id = STM32_MBX_VQ1_ID, |
Fabien Dessenne | 13140de | 2019-05-14 10:26:58 +0200 | [diff] [blame] | 313 | .client = { |
| 314 | .rx_callback = stm32_rproc_mb_callback, |
| 315 | .tx_block = false, |
| 316 | }, |
| 317 | }, |
| 318 | { |
| 319 | .name = STM32_MBX_SHUTDOWN, |
| 320 | .vq_id = -1, |
| 321 | .client = { |
| 322 | .tx_block = true, |
| 323 | .tx_done = NULL, |
| 324 | .tx_tout = 500, /* 500 ms time out */ |
| 325 | }, |
| 326 | } |
| 327 | }; |
| 328 | |
Fabien Dessenne | 4a56e423 | 2019-11-15 11:03:08 +0100 | [diff] [blame] | 329 | static int stm32_rproc_request_mbox(struct rproc *rproc) |
Fabien Dessenne | 13140de | 2019-05-14 10:26:58 +0200 | [diff] [blame] | 330 | { |
| 331 | struct stm32_rproc *ddata = rproc->priv; |
| 332 | struct device *dev = &rproc->dev; |
| 333 | unsigned int i; |
Fabien Dessenne | 4a56e423 | 2019-11-15 11:03:08 +0100 | [diff] [blame] | 334 | int j; |
Fabien Dessenne | 13140de | 2019-05-14 10:26:58 +0200 | [diff] [blame] | 335 | const unsigned char *name; |
| 336 | struct mbox_client *cl; |
| 337 | |
| 338 | /* Initialise mailbox structure table */ |
| 339 | memcpy(ddata->mb, stm32_rproc_mbox, sizeof(stm32_rproc_mbox)); |
| 340 | |
| 341 | for (i = 0; i < MBOX_NB_MBX; i++) { |
| 342 | name = ddata->mb[i].name; |
| 343 | |
| 344 | cl = &ddata->mb[i].client; |
| 345 | cl->dev = dev->parent; |
| 346 | |
| 347 | ddata->mb[i].chan = mbox_request_channel_byname(cl, name); |
| 348 | if (IS_ERR(ddata->mb[i].chan)) { |
Fabien Dessenne | 4a56e423 | 2019-11-15 11:03:08 +0100 | [diff] [blame] | 349 | if (PTR_ERR(ddata->mb[i].chan) == -EPROBE_DEFER) |
| 350 | goto err_probe; |
Fabien Dessenne | 13140de | 2019-05-14 10:26:58 +0200 | [diff] [blame] | 351 | dev_warn(dev, "cannot get %s mbox\n", name); |
| 352 | ddata->mb[i].chan = NULL; |
| 353 | } |
Arnaud Pouliquen | 714cf5e | 2019-10-25 10:56:39 +0200 | [diff] [blame] | 354 | if (ddata->mb[i].vq_id >= 0) { |
| 355 | INIT_WORK(&ddata->mb[i].vq_work, |
| 356 | stm32_rproc_mb_vq_work); |
| 357 | } |
Fabien Dessenne | 13140de | 2019-05-14 10:26:58 +0200 | [diff] [blame] | 358 | } |
Fabien Dessenne | 4a56e423 | 2019-11-15 11:03:08 +0100 | [diff] [blame] | 359 | |
| 360 | return 0; |
| 361 | |
| 362 | err_probe: |
| 363 | for (j = i - 1; j >= 0; j--) |
| 364 | if (ddata->mb[j].chan) |
| 365 | mbox_free_channel(ddata->mb[j].chan); |
| 366 | return -EPROBE_DEFER; |
Fabien Dessenne | 13140de | 2019-05-14 10:26:58 +0200 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | static int stm32_rproc_set_hold_boot(struct rproc *rproc, bool hold) |
| 370 | { |
| 371 | struct stm32_rproc *ddata = rproc->priv; |
| 372 | struct stm32_syscon hold_boot = ddata->hold_boot; |
| 373 | struct arm_smccc_res smc_res; |
| 374 | int val, err; |
| 375 | |
| 376 | val = hold ? HOLD_BOOT : RELEASE_BOOT; |
| 377 | |
Arnd Bergmann | 93f1d3e | 2019-07-08 14:42:51 +0200 | [diff] [blame] | 378 | if (IS_ENABLED(CONFIG_HAVE_ARM_SMCCC) && ddata->secured_soc) { |
Fabien Dessenne | 13140de | 2019-05-14 10:26:58 +0200 | [diff] [blame] | 379 | arm_smccc_smc(STM32_SMC_RCC, STM32_SMC_REG_WRITE, |
| 380 | hold_boot.reg, val, 0, 0, 0, 0, &smc_res); |
| 381 | err = smc_res.a0; |
| 382 | } else { |
| 383 | err = regmap_update_bits(hold_boot.map, hold_boot.reg, |
| 384 | hold_boot.mask, val); |
| 385 | } |
| 386 | |
| 387 | if (err) |
| 388 | dev_err(&rproc->dev, "failed to set hold boot\n"); |
| 389 | |
| 390 | return err; |
| 391 | } |
| 392 | |
| 393 | static void stm32_rproc_add_coredump_trace(struct rproc *rproc) |
| 394 | { |
| 395 | struct rproc_debug_trace *trace; |
| 396 | struct rproc_dump_segment *segment; |
| 397 | bool already_added; |
| 398 | |
| 399 | list_for_each_entry(trace, &rproc->traces, node) { |
| 400 | already_added = false; |
| 401 | |
| 402 | list_for_each_entry(segment, &rproc->dump_segments, node) { |
| 403 | if (segment->da == trace->trace_mem.da) { |
| 404 | already_added = true; |
| 405 | break; |
| 406 | } |
| 407 | } |
| 408 | |
| 409 | if (!already_added) |
| 410 | rproc_coredump_add_segment(rproc, trace->trace_mem.da, |
| 411 | trace->trace_mem.len); |
| 412 | } |
| 413 | } |
| 414 | |
| 415 | static int stm32_rproc_start(struct rproc *rproc) |
| 416 | { |
Fabien Dessenne | abbe429 | 2019-08-26 17:38:06 +0200 | [diff] [blame] | 417 | struct stm32_rproc *ddata = rproc->priv; |
Fabien Dessenne | 13140de | 2019-05-14 10:26:58 +0200 | [diff] [blame] | 418 | int err; |
| 419 | |
| 420 | stm32_rproc_add_coredump_trace(rproc); |
| 421 | |
Fabien Dessenne | abbe429 | 2019-08-26 17:38:06 +0200 | [diff] [blame] | 422 | /* clear remote proc Deep Sleep */ |
| 423 | if (ddata->pdds.map) { |
| 424 | err = regmap_update_bits(ddata->pdds.map, ddata->pdds.reg, |
| 425 | ddata->pdds.mask, 0); |
| 426 | if (err) { |
| 427 | dev_err(&rproc->dev, "failed to clear pdds\n"); |
| 428 | return err; |
| 429 | } |
| 430 | } |
| 431 | |
Fabien Dessenne | 13140de | 2019-05-14 10:26:58 +0200 | [diff] [blame] | 432 | err = stm32_rproc_set_hold_boot(rproc, false); |
| 433 | if (err) |
| 434 | return err; |
| 435 | |
| 436 | return stm32_rproc_set_hold_boot(rproc, true); |
| 437 | } |
| 438 | |
| 439 | static int stm32_rproc_stop(struct rproc *rproc) |
| 440 | { |
| 441 | struct stm32_rproc *ddata = rproc->priv; |
| 442 | int err, dummy_data, idx; |
| 443 | |
| 444 | /* request shutdown of the remote processor */ |
| 445 | if (rproc->state != RPROC_OFFLINE) { |
| 446 | idx = stm32_rproc_mbox_idx(rproc, STM32_MBX_SHUTDOWN); |
| 447 | if (idx >= 0 && ddata->mb[idx].chan) { |
| 448 | /* a dummy data is sent to allow to block on transmit */ |
| 449 | err = mbox_send_message(ddata->mb[idx].chan, |
| 450 | &dummy_data); |
| 451 | if (err < 0) |
| 452 | dev_warn(&rproc->dev, "warning: remote FW shutdown without ack\n"); |
| 453 | } |
| 454 | } |
| 455 | |
| 456 | err = stm32_rproc_set_hold_boot(rproc, true); |
| 457 | if (err) |
| 458 | return err; |
| 459 | |
| 460 | err = reset_control_assert(ddata->rst); |
| 461 | if (err) { |
| 462 | dev_err(&rproc->dev, "failed to assert the reset\n"); |
| 463 | return err; |
| 464 | } |
| 465 | |
| 466 | /* to allow platform Standby power mode, set remote proc Deep Sleep */ |
| 467 | if (ddata->pdds.map) { |
| 468 | err = regmap_update_bits(ddata->pdds.map, ddata->pdds.reg, |
| 469 | ddata->pdds.mask, 1); |
| 470 | if (err) { |
| 471 | dev_err(&rproc->dev, "failed to set pdds\n"); |
| 472 | return err; |
| 473 | } |
| 474 | } |
| 475 | |
| 476 | return 0; |
| 477 | } |
| 478 | |
| 479 | static void stm32_rproc_kick(struct rproc *rproc, int vqid) |
| 480 | { |
| 481 | struct stm32_rproc *ddata = rproc->priv; |
| 482 | unsigned int i; |
| 483 | int err; |
| 484 | |
| 485 | if (WARN_ON(vqid >= MBOX_NB_VQ)) |
| 486 | return; |
| 487 | |
| 488 | for (i = 0; i < MBOX_NB_MBX; i++) { |
| 489 | if (vqid != ddata->mb[i].vq_id) |
| 490 | continue; |
| 491 | if (!ddata->mb[i].chan) |
| 492 | return; |
| 493 | err = mbox_send_message(ddata->mb[i].chan, (void *)(long)vqid); |
| 494 | if (err < 0) |
| 495 | dev_err(&rproc->dev, "%s: failed (%s, err:%d)\n", |
| 496 | __func__, ddata->mb[i].name, err); |
| 497 | return; |
| 498 | } |
| 499 | } |
| 500 | |
| 501 | static struct rproc_ops st_rproc_ops = { |
| 502 | .start = stm32_rproc_start, |
| 503 | .stop = stm32_rproc_stop, |
| 504 | .kick = stm32_rproc_kick, |
| 505 | .load = rproc_elf_load_segments, |
| 506 | .parse_fw = stm32_rproc_parse_fw, |
| 507 | .find_loaded_rsc_table = rproc_elf_find_loaded_rsc_table, |
| 508 | .sanity_check = rproc_elf_sanity_check, |
| 509 | .get_boot_addr = rproc_elf_get_boot_addr, |
| 510 | }; |
| 511 | |
| 512 | static const struct of_device_id stm32_rproc_match[] = { |
| 513 | { .compatible = "st,stm32mp1-m4" }, |
| 514 | {}, |
| 515 | }; |
| 516 | MODULE_DEVICE_TABLE(of, stm32_rproc_match); |
| 517 | |
| 518 | static int stm32_rproc_get_syscon(struct device_node *np, const char *prop, |
| 519 | struct stm32_syscon *syscon) |
| 520 | { |
| 521 | int err = 0; |
| 522 | |
| 523 | syscon->map = syscon_regmap_lookup_by_phandle(np, prop); |
| 524 | if (IS_ERR(syscon->map)) { |
| 525 | err = PTR_ERR(syscon->map); |
| 526 | syscon->map = NULL; |
| 527 | goto out; |
| 528 | } |
| 529 | |
| 530 | err = of_property_read_u32_index(np, prop, 1, &syscon->reg); |
| 531 | if (err) |
| 532 | goto out; |
| 533 | |
| 534 | err = of_property_read_u32_index(np, prop, 2, &syscon->mask); |
| 535 | |
| 536 | out: |
| 537 | return err; |
| 538 | } |
| 539 | |
| 540 | static int stm32_rproc_parse_dt(struct platform_device *pdev) |
| 541 | { |
| 542 | struct device *dev = &pdev->dev; |
| 543 | struct device_node *np = dev->of_node; |
| 544 | struct rproc *rproc = platform_get_drvdata(pdev); |
| 545 | struct stm32_rproc *ddata = rproc->priv; |
| 546 | struct stm32_syscon tz; |
| 547 | unsigned int tzen; |
| 548 | int err, irq; |
| 549 | |
| 550 | irq = platform_get_irq(pdev, 0); |
Fabien Dessenne | d333de3 | 2019-08-26 17:38:29 +0200 | [diff] [blame] | 551 | if (irq == -EPROBE_DEFER) |
| 552 | return -EPROBE_DEFER; |
| 553 | |
Fabien Dessenne | 13140de | 2019-05-14 10:26:58 +0200 | [diff] [blame] | 554 | if (irq > 0) { |
| 555 | err = devm_request_irq(dev, irq, stm32_rproc_wdg, 0, |
| 556 | dev_name(dev), rproc); |
| 557 | if (err) { |
| 558 | dev_err(dev, "failed to request wdg irq\n"); |
| 559 | return err; |
| 560 | } |
| 561 | |
Fabien Dessenne | 410119e | 2019-08-26 17:38:43 +0200 | [diff] [blame] | 562 | ddata->wdg_irq = irq; |
| 563 | |
| 564 | if (of_property_read_bool(np, "wakeup-source")) { |
| 565 | device_init_wakeup(dev, true); |
| 566 | dev_pm_set_wake_irq(dev, irq); |
| 567 | } |
| 568 | |
Fabien Dessenne | 13140de | 2019-05-14 10:26:58 +0200 | [diff] [blame] | 569 | dev_info(dev, "wdg irq registered\n"); |
| 570 | } |
| 571 | |
| 572 | ddata->rst = devm_reset_control_get_by_index(dev, 0); |
| 573 | if (IS_ERR(ddata->rst)) { |
| 574 | dev_err(dev, "failed to get mcu reset\n"); |
| 575 | return PTR_ERR(ddata->rst); |
| 576 | } |
| 577 | |
| 578 | /* |
| 579 | * if platform is secured the hold boot bit must be written by |
| 580 | * smc call and read normally. |
| 581 | * if not secure the hold boot bit could be read/write normally |
| 582 | */ |
| 583 | err = stm32_rproc_get_syscon(np, "st,syscfg-tz", &tz); |
| 584 | if (err) { |
| 585 | dev_err(dev, "failed to get tz syscfg\n"); |
| 586 | return err; |
| 587 | } |
| 588 | |
| 589 | err = regmap_read(tz.map, tz.reg, &tzen); |
| 590 | if (err) { |
| 591 | dev_err(&rproc->dev, "failed to read tzen\n"); |
| 592 | return err; |
| 593 | } |
| 594 | ddata->secured_soc = tzen & tz.mask; |
| 595 | |
| 596 | err = stm32_rproc_get_syscon(np, "st,syscfg-holdboot", |
| 597 | &ddata->hold_boot); |
| 598 | if (err) { |
| 599 | dev_err(dev, "failed to get hold boot\n"); |
| 600 | return err; |
| 601 | } |
| 602 | |
| 603 | err = stm32_rproc_get_syscon(np, "st,syscfg-pdds", &ddata->pdds); |
| 604 | if (err) |
| 605 | dev_warn(dev, "failed to get pdds\n"); |
| 606 | |
| 607 | rproc->auto_boot = of_property_read_bool(np, "st,auto-boot"); |
| 608 | |
| 609 | return stm32_rproc_of_memory_translations(rproc); |
| 610 | } |
| 611 | |
| 612 | static int stm32_rproc_probe(struct platform_device *pdev) |
| 613 | { |
| 614 | struct device *dev = &pdev->dev; |
| 615 | struct stm32_rproc *ddata; |
| 616 | struct device_node *np = dev->of_node; |
| 617 | struct rproc *rproc; |
| 618 | int ret; |
| 619 | |
| 620 | ret = dma_coerce_mask_and_coherent(dev, DMA_BIT_MASK(32)); |
| 621 | if (ret) |
| 622 | return ret; |
| 623 | |
| 624 | rproc = rproc_alloc(dev, np->name, &st_rproc_ops, NULL, sizeof(*ddata)); |
| 625 | if (!rproc) |
| 626 | return -ENOMEM; |
| 627 | |
| 628 | rproc->has_iommu = false; |
| 629 | ddata = rproc->priv; |
Arnaud Pouliquen | 714cf5e | 2019-10-25 10:56:39 +0200 | [diff] [blame] | 630 | ddata->workqueue = create_workqueue(dev_name(dev)); |
| 631 | if (!ddata->workqueue) { |
| 632 | dev_err(dev, "cannot create workqueue\n"); |
| 633 | ret = -ENOMEM; |
| 634 | goto free_rproc; |
| 635 | } |
Fabien Dessenne | 13140de | 2019-05-14 10:26:58 +0200 | [diff] [blame] | 636 | |
| 637 | platform_set_drvdata(pdev, rproc); |
| 638 | |
| 639 | ret = stm32_rproc_parse_dt(pdev); |
| 640 | if (ret) |
Arnaud Pouliquen | 714cf5e | 2019-10-25 10:56:39 +0200 | [diff] [blame] | 641 | goto free_wkq; |
Fabien Dessenne | 13140de | 2019-05-14 10:26:58 +0200 | [diff] [blame] | 642 | |
Fabien Dessenne | 4a56e423 | 2019-11-15 11:03:08 +0100 | [diff] [blame] | 643 | ret = stm32_rproc_request_mbox(rproc); |
| 644 | if (ret) |
| 645 | goto free_rproc; |
Fabien Dessenne | 13140de | 2019-05-14 10:26:58 +0200 | [diff] [blame] | 646 | |
| 647 | ret = rproc_add(rproc); |
| 648 | if (ret) |
| 649 | goto free_mb; |
| 650 | |
| 651 | return 0; |
| 652 | |
| 653 | free_mb: |
| 654 | stm32_rproc_free_mbox(rproc); |
Arnaud Pouliquen | 714cf5e | 2019-10-25 10:56:39 +0200 | [diff] [blame] | 655 | free_wkq: |
| 656 | destroy_workqueue(ddata->workqueue); |
Fabien Dessenne | 13140de | 2019-05-14 10:26:58 +0200 | [diff] [blame] | 657 | free_rproc: |
Fabien Dessenne | 410119e | 2019-08-26 17:38:43 +0200 | [diff] [blame] | 658 | if (device_may_wakeup(dev)) { |
| 659 | dev_pm_clear_wake_irq(dev); |
| 660 | device_init_wakeup(dev, false); |
| 661 | } |
Fabien Dessenne | 13140de | 2019-05-14 10:26:58 +0200 | [diff] [blame] | 662 | rproc_free(rproc); |
| 663 | return ret; |
| 664 | } |
| 665 | |
| 666 | static int stm32_rproc_remove(struct platform_device *pdev) |
| 667 | { |
| 668 | struct rproc *rproc = platform_get_drvdata(pdev); |
Arnaud Pouliquen | 714cf5e | 2019-10-25 10:56:39 +0200 | [diff] [blame] | 669 | struct stm32_rproc *ddata = rproc->priv; |
Fabien Dessenne | 410119e | 2019-08-26 17:38:43 +0200 | [diff] [blame] | 670 | struct device *dev = &pdev->dev; |
Fabien Dessenne | 13140de | 2019-05-14 10:26:58 +0200 | [diff] [blame] | 671 | |
| 672 | if (atomic_read(&rproc->power) > 0) |
| 673 | rproc_shutdown(rproc); |
| 674 | |
| 675 | rproc_del(rproc); |
| 676 | stm32_rproc_free_mbox(rproc); |
Arnaud Pouliquen | 714cf5e | 2019-10-25 10:56:39 +0200 | [diff] [blame] | 677 | destroy_workqueue(ddata->workqueue); |
Fabien Dessenne | 410119e | 2019-08-26 17:38:43 +0200 | [diff] [blame] | 678 | |
| 679 | if (device_may_wakeup(dev)) { |
| 680 | dev_pm_clear_wake_irq(dev); |
| 681 | device_init_wakeup(dev, false); |
| 682 | } |
Fabien Dessenne | 13140de | 2019-05-14 10:26:58 +0200 | [diff] [blame] | 683 | rproc_free(rproc); |
| 684 | |
| 685 | return 0; |
| 686 | } |
| 687 | |
Fabien Dessenne | 410119e | 2019-08-26 17:38:43 +0200 | [diff] [blame] | 688 | static int __maybe_unused stm32_rproc_suspend(struct device *dev) |
| 689 | { |
| 690 | struct rproc *rproc = dev_get_drvdata(dev); |
| 691 | struct stm32_rproc *ddata = rproc->priv; |
| 692 | |
| 693 | if (device_may_wakeup(dev)) |
| 694 | return enable_irq_wake(ddata->wdg_irq); |
| 695 | |
| 696 | return 0; |
| 697 | } |
| 698 | |
| 699 | static int __maybe_unused stm32_rproc_resume(struct device *dev) |
| 700 | { |
| 701 | struct rproc *rproc = dev_get_drvdata(dev); |
| 702 | struct stm32_rproc *ddata = rproc->priv; |
| 703 | |
| 704 | if (device_may_wakeup(dev)) |
| 705 | return disable_irq_wake(ddata->wdg_irq); |
| 706 | |
| 707 | return 0; |
| 708 | } |
| 709 | |
| 710 | static SIMPLE_DEV_PM_OPS(stm32_rproc_pm_ops, |
| 711 | stm32_rproc_suspend, stm32_rproc_resume); |
| 712 | |
Fabien Dessenne | 13140de | 2019-05-14 10:26:58 +0200 | [diff] [blame] | 713 | static struct platform_driver stm32_rproc_driver = { |
| 714 | .probe = stm32_rproc_probe, |
| 715 | .remove = stm32_rproc_remove, |
| 716 | .driver = { |
| 717 | .name = "stm32-rproc", |
Fabien Dessenne | 410119e | 2019-08-26 17:38:43 +0200 | [diff] [blame] | 718 | .pm = &stm32_rproc_pm_ops, |
Fabien Dessenne | 13140de | 2019-05-14 10:26:58 +0200 | [diff] [blame] | 719 | .of_match_table = of_match_ptr(stm32_rproc_match), |
| 720 | }, |
| 721 | }; |
| 722 | module_platform_driver(stm32_rproc_driver); |
| 723 | |
| 724 | MODULE_DESCRIPTION("STM32 Remote Processor Control Driver"); |
| 725 | MODULE_AUTHOR("Ludovic Barre <ludovic.barre@st.com>"); |
| 726 | MODULE_AUTHOR("Fabien Dessenne <fabien.dessenne@st.com>"); |
| 727 | MODULE_LICENSE("GPL v2"); |
| 728 | |