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> 18 #include <linux/pm_wakeirq.h> 19 #include <linux/regmap.h> 20 #include <linux/remoteproc.h> 21 #include <linux/reset.h> 22 #include <linux/slab.h> 23 #include <linux/workqueue.h> 24 25 #include "remoteproc_internal.h" 26 27 #define HOLD_BOOT 0 28 #define RELEASE_BOOT 1 29 30 #define MBOX_NB_VQ 2 31 #define MBOX_NB_MBX 4 32 33 #define STM32_SMC_RCC 0x82001000 34 #define STM32_SMC_REG_WRITE 0x1 35 36 #define STM32_MBX_VQ0 "vq0" 37 #define STM32_MBX_VQ0_ID 0 38 #define STM32_MBX_VQ1 "vq1" 39 #define STM32_MBX_VQ1_ID 1 40 #define STM32_MBX_SHUTDOWN "shutdown" 41 #define STM32_MBX_DETACH "detach" 42 43 #define RSC_TBL_SIZE 1024 44 45 #define M4_STATE_OFF 0 46 #define M4_STATE_INI 1 47 #define M4_STATE_CRUN 2 48 #define M4_STATE_CSTOP 3 49 #define M4_STATE_STANDBY 4 50 #define M4_STATE_CRASH 5 51 52 struct stm32_syscon { 53 struct regmap *map; 54 u32 reg; 55 u32 mask; 56 }; 57 58 struct stm32_rproc_mem { 59 char name[20]; 60 void __iomem *cpu_addr; 61 phys_addr_t bus_addr; 62 u32 dev_addr; 63 size_t size; 64 }; 65 66 struct stm32_rproc_mem_ranges { 67 u32 dev_addr; 68 u32 bus_addr; 69 u32 size; 70 }; 71 72 struct stm32_mbox { 73 const unsigned char name[10]; 74 struct mbox_chan *chan; 75 struct mbox_client client; 76 struct work_struct vq_work; 77 int vq_id; 78 }; 79 80 struct stm32_rproc { 81 struct reset_control *rst; 82 struct stm32_syscon hold_boot; 83 struct stm32_syscon pdds; 84 struct stm32_syscon m4_state; 85 struct stm32_syscon rsctbl; 86 int wdg_irq; 87 u32 nb_rmems; 88 struct stm32_rproc_mem *rmems; 89 struct stm32_mbox mb[MBOX_NB_MBX]; 90 struct workqueue_struct *workqueue; 91 bool secured_soc; 92 void __iomem *rsc_va; 93 }; 94 95 static int stm32_rproc_pa_to_da(struct rproc *rproc, phys_addr_t pa, u64 *da) 96 { 97 unsigned int i; 98 struct stm32_rproc *ddata = rproc->priv; 99 struct stm32_rproc_mem *p_mem; 100 101 for (i = 0; i < ddata->nb_rmems; i++) { 102 p_mem = &ddata->rmems[i]; 103 104 if (pa < p_mem->bus_addr || 105 pa >= p_mem->bus_addr + p_mem->size) 106 continue; 107 *da = pa - p_mem->bus_addr + p_mem->dev_addr; 108 dev_dbg(rproc->dev.parent, "pa %pa to da %llx\n", &pa, *da); 109 return 0; 110 } 111 112 return -EINVAL; 113 } 114 115 static int stm32_rproc_mem_alloc(struct rproc *rproc, 116 struct rproc_mem_entry *mem) 117 { 118 struct device *dev = rproc->dev.parent; 119 void *va; 120 121 dev_dbg(dev, "map memory: %pa+%x\n", &mem->dma, mem->len); 122 va = ioremap_wc(mem->dma, mem->len); 123 if (IS_ERR_OR_NULL(va)) { 124 dev_err(dev, "Unable to map memory region: %pa+%x\n", 125 &mem->dma, mem->len); 126 return -ENOMEM; 127 } 128 129 /* Update memory entry va */ 130 mem->va = va; 131 132 return 0; 133 } 134 135 static int stm32_rproc_mem_release(struct rproc *rproc, 136 struct rproc_mem_entry *mem) 137 { 138 dev_dbg(rproc->dev.parent, "unmap memory: %pa\n", &mem->dma); 139 iounmap(mem->va); 140 141 return 0; 142 } 143 144 static int stm32_rproc_of_memory_translations(struct platform_device *pdev, 145 struct stm32_rproc *ddata) 146 { 147 struct device *parent, *dev = &pdev->dev; 148 struct device_node *np; 149 struct stm32_rproc_mem *p_mems; 150 struct stm32_rproc_mem_ranges *mem_range; 151 int cnt, array_size, i, ret = 0; 152 153 parent = dev->parent; 154 np = parent->of_node; 155 156 cnt = of_property_count_elems_of_size(np, "dma-ranges", 157 sizeof(*mem_range)); 158 if (cnt <= 0) { 159 dev_err(dev, "%s: dma-ranges property not defined\n", __func__); 160 return -EINVAL; 161 } 162 163 p_mems = devm_kcalloc(dev, cnt, sizeof(*p_mems), GFP_KERNEL); 164 if (!p_mems) 165 return -ENOMEM; 166 mem_range = kcalloc(cnt, sizeof(*mem_range), GFP_KERNEL); 167 if (!mem_range) 168 return -ENOMEM; 169 170 array_size = cnt * sizeof(struct stm32_rproc_mem_ranges) / sizeof(u32); 171 172 ret = of_property_read_u32_array(np, "dma-ranges", 173 (u32 *)mem_range, array_size); 174 if (ret) { 175 dev_err(dev, "error while get dma-ranges property: %x\n", ret); 176 goto free_mem; 177 } 178 179 for (i = 0; i < cnt; i++) { 180 p_mems[i].bus_addr = mem_range[i].bus_addr; 181 p_mems[i].dev_addr = mem_range[i].dev_addr; 182 p_mems[i].size = mem_range[i].size; 183 184 dev_dbg(dev, "memory range[%i]: da %#x, pa %pa, size %#zx:\n", 185 i, p_mems[i].dev_addr, &p_mems[i].bus_addr, 186 p_mems[i].size); 187 } 188 189 ddata->rmems = p_mems; 190 ddata->nb_rmems = cnt; 191 192 free_mem: 193 kfree(mem_range); 194 return ret; 195 } 196 197 static int stm32_rproc_mbox_idx(struct rproc *rproc, const unsigned char *name) 198 { 199 struct stm32_rproc *ddata = rproc->priv; 200 int i; 201 202 for (i = 0; i < ARRAY_SIZE(ddata->mb); i++) { 203 if (!strncmp(ddata->mb[i].name, name, strlen(name))) 204 return i; 205 } 206 dev_err(&rproc->dev, "mailbox %s not found\n", name); 207 208 return -EINVAL; 209 } 210 211 static int stm32_rproc_prepare(struct rproc *rproc) 212 { 213 struct device *dev = rproc->dev.parent; 214 struct device_node *np = dev->of_node; 215 struct of_phandle_iterator it; 216 struct rproc_mem_entry *mem; 217 struct reserved_mem *rmem; 218 u64 da; 219 int index = 0; 220 221 /* Register associated reserved memory regions */ 222 of_phandle_iterator_init(&it, np, "memory-region", NULL, 0); 223 while (of_phandle_iterator_next(&it) == 0) { 224 rmem = of_reserved_mem_lookup(it.node); 225 if (!rmem) { 226 of_node_put(it.node); 227 dev_err(dev, "unable to acquire memory-region\n"); 228 return -EINVAL; 229 } 230 231 if (stm32_rproc_pa_to_da(rproc, rmem->base, &da) < 0) { 232 of_node_put(it.node); 233 dev_err(dev, "memory region not valid %pa\n", 234 &rmem->base); 235 return -EINVAL; 236 } 237 238 /* No need to map vdev buffer */ 239 if (strcmp(it.node->name, "vdev0buffer")) { 240 /* Register memory region */ 241 mem = rproc_mem_entry_init(dev, NULL, 242 (dma_addr_t)rmem->base, 243 rmem->size, da, 244 stm32_rproc_mem_alloc, 245 stm32_rproc_mem_release, 246 it.node->name); 247 248 if (mem) 249 rproc_coredump_add_segment(rproc, da, 250 rmem->size); 251 } else { 252 /* Register reserved memory for vdev buffer alloc */ 253 mem = rproc_of_resm_mem_entry_init(dev, index, 254 rmem->size, 255 rmem->base, 256 it.node->name); 257 } 258 259 if (!mem) { 260 of_node_put(it.node); 261 return -ENOMEM; 262 } 263 264 rproc_add_carveout(rproc, mem); 265 index++; 266 } 267 268 return 0; 269 } 270 271 static int stm32_rproc_parse_fw(struct rproc *rproc, const struct firmware *fw) 272 { 273 if (rproc_elf_load_rsc_table(rproc, fw)) 274 dev_warn(&rproc->dev, "no resource table found for this firmware\n"); 275 276 return 0; 277 } 278 279 static irqreturn_t stm32_rproc_wdg(int irq, void *data) 280 { 281 struct platform_device *pdev = data; 282 struct rproc *rproc = platform_get_drvdata(pdev); 283 284 rproc_report_crash(rproc, RPROC_WATCHDOG); 285 286 return IRQ_HANDLED; 287 } 288 289 static void stm32_rproc_mb_vq_work(struct work_struct *work) 290 { 291 struct stm32_mbox *mb = container_of(work, struct stm32_mbox, vq_work); 292 struct rproc *rproc = dev_get_drvdata(mb->client.dev); 293 294 mutex_lock(&rproc->lock); 295 296 if (rproc->state != RPROC_RUNNING) 297 goto unlock_mutex; 298 299 if (rproc_vq_interrupt(rproc, mb->vq_id) == IRQ_NONE) 300 dev_dbg(&rproc->dev, "no message found in vq%d\n", mb->vq_id); 301 302 unlock_mutex: 303 mutex_unlock(&rproc->lock); 304 } 305 306 static void stm32_rproc_mb_callback(struct mbox_client *cl, void *data) 307 { 308 struct rproc *rproc = dev_get_drvdata(cl->dev); 309 struct stm32_mbox *mb = container_of(cl, struct stm32_mbox, client); 310 struct stm32_rproc *ddata = rproc->priv; 311 312 queue_work(ddata->workqueue, &mb->vq_work); 313 } 314 315 static void stm32_rproc_free_mbox(struct rproc *rproc) 316 { 317 struct stm32_rproc *ddata = rproc->priv; 318 unsigned int i; 319 320 for (i = 0; i < ARRAY_SIZE(ddata->mb); i++) { 321 if (ddata->mb[i].chan) 322 mbox_free_channel(ddata->mb[i].chan); 323 ddata->mb[i].chan = NULL; 324 } 325 } 326 327 static const struct stm32_mbox stm32_rproc_mbox[MBOX_NB_MBX] = { 328 { 329 .name = STM32_MBX_VQ0, 330 .vq_id = STM32_MBX_VQ0_ID, 331 .client = { 332 .rx_callback = stm32_rproc_mb_callback, 333 .tx_block = false, 334 }, 335 }, 336 { 337 .name = STM32_MBX_VQ1, 338 .vq_id = STM32_MBX_VQ1_ID, 339 .client = { 340 .rx_callback = stm32_rproc_mb_callback, 341 .tx_block = false, 342 }, 343 }, 344 { 345 .name = STM32_MBX_SHUTDOWN, 346 .vq_id = -1, 347 .client = { 348 .tx_block = true, 349 .tx_done = NULL, 350 .tx_tout = 500, /* 500 ms time out */ 351 }, 352 }, 353 { 354 .name = STM32_MBX_DETACH, 355 .vq_id = -1, 356 .client = { 357 .tx_block = true, 358 .tx_done = NULL, 359 .tx_tout = 200, /* 200 ms time out to detach should be fair enough */ 360 }, 361 } 362 }; 363 364 static int stm32_rproc_request_mbox(struct rproc *rproc) 365 { 366 struct stm32_rproc *ddata = rproc->priv; 367 struct device *dev = &rproc->dev; 368 unsigned int i; 369 int j; 370 const unsigned char *name; 371 struct mbox_client *cl; 372 373 /* Initialise mailbox structure table */ 374 memcpy(ddata->mb, stm32_rproc_mbox, sizeof(stm32_rproc_mbox)); 375 376 for (i = 0; i < MBOX_NB_MBX; i++) { 377 name = ddata->mb[i].name; 378 379 cl = &ddata->mb[i].client; 380 cl->dev = dev->parent; 381 382 ddata->mb[i].chan = mbox_request_channel_byname(cl, name); 383 if (IS_ERR(ddata->mb[i].chan)) { 384 if (PTR_ERR(ddata->mb[i].chan) == -EPROBE_DEFER) { 385 dev_err_probe(dev->parent, 386 PTR_ERR(ddata->mb[i].chan), 387 "failed to request mailbox %s\n", 388 name); 389 goto err_probe; 390 } 391 dev_warn(dev, "cannot get %s mbox\n", name); 392 ddata->mb[i].chan = NULL; 393 } 394 if (ddata->mb[i].vq_id >= 0) { 395 INIT_WORK(&ddata->mb[i].vq_work, 396 stm32_rproc_mb_vq_work); 397 } 398 } 399 400 return 0; 401 402 err_probe: 403 for (j = i - 1; j >= 0; j--) 404 if (ddata->mb[j].chan) 405 mbox_free_channel(ddata->mb[j].chan); 406 return -EPROBE_DEFER; 407 } 408 409 static int stm32_rproc_set_hold_boot(struct rproc *rproc, bool hold) 410 { 411 struct stm32_rproc *ddata = rproc->priv; 412 struct stm32_syscon hold_boot = ddata->hold_boot; 413 struct arm_smccc_res smc_res; 414 int val, err; 415 416 val = hold ? HOLD_BOOT : RELEASE_BOOT; 417 418 if (IS_ENABLED(CONFIG_HAVE_ARM_SMCCC) && ddata->secured_soc) { 419 arm_smccc_smc(STM32_SMC_RCC, STM32_SMC_REG_WRITE, 420 hold_boot.reg, val, 0, 0, 0, 0, &smc_res); 421 err = smc_res.a0; 422 } else { 423 err = regmap_update_bits(hold_boot.map, hold_boot.reg, 424 hold_boot.mask, val); 425 } 426 427 if (err) 428 dev_err(&rproc->dev, "failed to set hold boot\n"); 429 430 return err; 431 } 432 433 static void stm32_rproc_add_coredump_trace(struct rproc *rproc) 434 { 435 struct rproc_debug_trace *trace; 436 struct rproc_dump_segment *segment; 437 bool already_added; 438 439 list_for_each_entry(trace, &rproc->traces, node) { 440 already_added = false; 441 442 list_for_each_entry(segment, &rproc->dump_segments, node) { 443 if (segment->da == trace->trace_mem.da) { 444 already_added = true; 445 break; 446 } 447 } 448 449 if (!already_added) 450 rproc_coredump_add_segment(rproc, trace->trace_mem.da, 451 trace->trace_mem.len); 452 } 453 } 454 455 static int stm32_rproc_start(struct rproc *rproc) 456 { 457 struct stm32_rproc *ddata = rproc->priv; 458 int err; 459 460 stm32_rproc_add_coredump_trace(rproc); 461 462 /* clear remote proc Deep Sleep */ 463 if (ddata->pdds.map) { 464 err = regmap_update_bits(ddata->pdds.map, ddata->pdds.reg, 465 ddata->pdds.mask, 0); 466 if (err) { 467 dev_err(&rproc->dev, "failed to clear pdds\n"); 468 return err; 469 } 470 } 471 472 err = stm32_rproc_set_hold_boot(rproc, false); 473 if (err) 474 return err; 475 476 return stm32_rproc_set_hold_boot(rproc, true); 477 } 478 479 static int stm32_rproc_attach(struct rproc *rproc) 480 { 481 stm32_rproc_add_coredump_trace(rproc); 482 483 return stm32_rproc_set_hold_boot(rproc, true); 484 } 485 486 static int stm32_rproc_detach(struct rproc *rproc) 487 { 488 struct stm32_rproc *ddata = rproc->priv; 489 int err, idx; 490 491 /* Inform the remote processor of the detach */ 492 idx = stm32_rproc_mbox_idx(rproc, STM32_MBX_DETACH); 493 if (idx >= 0 && ddata->mb[idx].chan) { 494 err = mbox_send_message(ddata->mb[idx].chan, "stop"); 495 if (err < 0) 496 dev_warn(&rproc->dev, "warning: remote FW detach without ack\n"); 497 } 498 499 /* Allow remote processor to auto-reboot */ 500 return stm32_rproc_set_hold_boot(rproc, false); 501 } 502 503 static int stm32_rproc_stop(struct rproc *rproc) 504 { 505 struct stm32_rproc *ddata = rproc->priv; 506 int err, idx; 507 508 /* request shutdown of the remote processor */ 509 if (rproc->state != RPROC_OFFLINE && rproc->state != RPROC_CRASHED) { 510 idx = stm32_rproc_mbox_idx(rproc, STM32_MBX_SHUTDOWN); 511 if (idx >= 0 && ddata->mb[idx].chan) { 512 err = mbox_send_message(ddata->mb[idx].chan, "detach"); 513 if (err < 0) 514 dev_warn(&rproc->dev, "warning: remote FW shutdown without ack\n"); 515 } 516 } 517 518 err = stm32_rproc_set_hold_boot(rproc, true); 519 if (err) 520 return err; 521 522 err = reset_control_assert(ddata->rst); 523 if (err) { 524 dev_err(&rproc->dev, "failed to assert the reset\n"); 525 return err; 526 } 527 528 /* to allow platform Standby power mode, set remote proc Deep Sleep */ 529 if (ddata->pdds.map) { 530 err = regmap_update_bits(ddata->pdds.map, ddata->pdds.reg, 531 ddata->pdds.mask, 1); 532 if (err) { 533 dev_err(&rproc->dev, "failed to set pdds\n"); 534 return err; 535 } 536 } 537 538 /* update coprocessor state to OFF if available */ 539 if (ddata->m4_state.map) { 540 err = regmap_update_bits(ddata->m4_state.map, 541 ddata->m4_state.reg, 542 ddata->m4_state.mask, 543 M4_STATE_OFF); 544 if (err) { 545 dev_err(&rproc->dev, "failed to set copro state\n"); 546 return err; 547 } 548 } 549 550 return 0; 551 } 552 553 static void stm32_rproc_kick(struct rproc *rproc, int vqid) 554 { 555 struct stm32_rproc *ddata = rproc->priv; 556 unsigned int i; 557 int err; 558 559 if (WARN_ON(vqid >= MBOX_NB_VQ)) 560 return; 561 562 for (i = 0; i < MBOX_NB_MBX; i++) { 563 if (vqid != ddata->mb[i].vq_id) 564 continue; 565 if (!ddata->mb[i].chan) 566 return; 567 err = mbox_send_message(ddata->mb[i].chan, "kick"); 568 if (err < 0) 569 dev_err(&rproc->dev, "%s: failed (%s, err:%d)\n", 570 __func__, ddata->mb[i].name, err); 571 return; 572 } 573 } 574 575 static int stm32_rproc_da_to_pa(struct rproc *rproc, 576 u64 da, phys_addr_t *pa) 577 { 578 struct stm32_rproc *ddata = rproc->priv; 579 struct device *dev = rproc->dev.parent; 580 struct stm32_rproc_mem *p_mem; 581 unsigned int i; 582 583 for (i = 0; i < ddata->nb_rmems; i++) { 584 p_mem = &ddata->rmems[i]; 585 586 if (da < p_mem->dev_addr || 587 da >= p_mem->dev_addr + p_mem->size) 588 continue; 589 590 *pa = da - p_mem->dev_addr + p_mem->bus_addr; 591 dev_dbg(dev, "da %llx to pa %pap\n", da, pa); 592 593 return 0; 594 } 595 596 dev_err(dev, "can't translate da %llx\n", da); 597 598 return -EINVAL; 599 } 600 601 static struct resource_table * 602 stm32_rproc_get_loaded_rsc_table(struct rproc *rproc, size_t *table_sz) 603 { 604 struct stm32_rproc *ddata = rproc->priv; 605 struct device *dev = rproc->dev.parent; 606 phys_addr_t rsc_pa; 607 u32 rsc_da; 608 int err; 609 610 /* The resource table has already been mapped, nothing to do */ 611 if (ddata->rsc_va) 612 goto done; 613 614 err = regmap_read(ddata->rsctbl.map, ddata->rsctbl.reg, &rsc_da); 615 if (err) { 616 dev_err(dev, "failed to read rsc tbl addr\n"); 617 return ERR_PTR(-EINVAL); 618 } 619 620 if (!rsc_da) 621 /* no rsc table */ 622 return ERR_PTR(-ENOENT); 623 624 err = stm32_rproc_da_to_pa(rproc, rsc_da, &rsc_pa); 625 if (err) 626 return ERR_PTR(err); 627 628 ddata->rsc_va = devm_ioremap_wc(dev, rsc_pa, RSC_TBL_SIZE); 629 if (IS_ERR_OR_NULL(ddata->rsc_va)) { 630 dev_err(dev, "Unable to map memory region: %pa+%zx\n", 631 &rsc_pa, RSC_TBL_SIZE); 632 ddata->rsc_va = NULL; 633 return ERR_PTR(-ENOMEM); 634 } 635 636 done: 637 /* 638 * Assuming the resource table fits in 1kB is fair. 639 * Notice for the detach, that this 1 kB memory area has to be reserved in the coprocessor 640 * firmware for the resource table. On detach, the remoteproc core re-initializes this 641 * entire area by overwriting it with the initial values stored in rproc->clean_table. 642 */ 643 *table_sz = RSC_TBL_SIZE; 644 return (struct resource_table *)ddata->rsc_va; 645 } 646 647 static const struct rproc_ops st_rproc_ops = { 648 .prepare = stm32_rproc_prepare, 649 .start = stm32_rproc_start, 650 .stop = stm32_rproc_stop, 651 .attach = stm32_rproc_attach, 652 .detach = stm32_rproc_detach, 653 .kick = stm32_rproc_kick, 654 .load = rproc_elf_load_segments, 655 .parse_fw = stm32_rproc_parse_fw, 656 .find_loaded_rsc_table = rproc_elf_find_loaded_rsc_table, 657 .get_loaded_rsc_table = stm32_rproc_get_loaded_rsc_table, 658 .sanity_check = rproc_elf_sanity_check, 659 .get_boot_addr = rproc_elf_get_boot_addr, 660 }; 661 662 static const struct of_device_id stm32_rproc_match[] = { 663 { .compatible = "st,stm32mp1-m4" }, 664 {}, 665 }; 666 MODULE_DEVICE_TABLE(of, stm32_rproc_match); 667 668 static int stm32_rproc_get_syscon(struct device_node *np, const char *prop, 669 struct stm32_syscon *syscon) 670 { 671 int err = 0; 672 673 syscon->map = syscon_regmap_lookup_by_phandle(np, prop); 674 if (IS_ERR(syscon->map)) { 675 err = PTR_ERR(syscon->map); 676 syscon->map = NULL; 677 goto out; 678 } 679 680 err = of_property_read_u32_index(np, prop, 1, &syscon->reg); 681 if (err) 682 goto out; 683 684 err = of_property_read_u32_index(np, prop, 2, &syscon->mask); 685 686 out: 687 return err; 688 } 689 690 static int stm32_rproc_parse_dt(struct platform_device *pdev, 691 struct stm32_rproc *ddata, bool *auto_boot) 692 { 693 struct device *dev = &pdev->dev; 694 struct device_node *np = dev->of_node; 695 struct stm32_syscon tz; 696 unsigned int tzen; 697 int err, irq; 698 699 irq = platform_get_irq(pdev, 0); 700 if (irq == -EPROBE_DEFER) 701 return dev_err_probe(dev, irq, "failed to get interrupt\n"); 702 703 if (irq > 0) { 704 err = devm_request_irq(dev, irq, stm32_rproc_wdg, 0, 705 dev_name(dev), pdev); 706 if (err) 707 return dev_err_probe(dev, err, 708 "failed to request wdg irq\n"); 709 710 ddata->wdg_irq = irq; 711 712 if (of_property_read_bool(np, "wakeup-source")) { 713 device_init_wakeup(dev, true); 714 dev_pm_set_wake_irq(dev, irq); 715 } 716 717 dev_info(dev, "wdg irq registered\n"); 718 } 719 720 ddata->rst = devm_reset_control_get_by_index(dev, 0); 721 if (IS_ERR(ddata->rst)) 722 return dev_err_probe(dev, PTR_ERR(ddata->rst), 723 "failed to get mcu_reset\n"); 724 725 /* 726 * if platform is secured the hold boot bit must be written by 727 * smc call and read normally. 728 * if not secure the hold boot bit could be read/write normally 729 */ 730 err = stm32_rproc_get_syscon(np, "st,syscfg-tz", &tz); 731 if (err) { 732 dev_err(dev, "failed to get tz syscfg\n"); 733 return err; 734 } 735 736 err = regmap_read(tz.map, tz.reg, &tzen); 737 if (err) { 738 dev_err(dev, "failed to read tzen\n"); 739 return err; 740 } 741 ddata->secured_soc = tzen & tz.mask; 742 743 err = stm32_rproc_get_syscon(np, "st,syscfg-holdboot", 744 &ddata->hold_boot); 745 if (err) { 746 dev_err(dev, "failed to get hold boot\n"); 747 return err; 748 } 749 750 err = stm32_rproc_get_syscon(np, "st,syscfg-pdds", &ddata->pdds); 751 if (err) 752 dev_info(dev, "failed to get pdds\n"); 753 754 *auto_boot = of_property_read_bool(np, "st,auto-boot"); 755 756 /* 757 * See if we can check the M4 status, i.e if it was started 758 * from the boot loader or not. 759 */ 760 err = stm32_rproc_get_syscon(np, "st,syscfg-m4-state", 761 &ddata->m4_state); 762 if (err) { 763 /* remember this */ 764 ddata->m4_state.map = NULL; 765 /* no coprocessor state syscon (optional) */ 766 dev_warn(dev, "m4 state not supported\n"); 767 768 /* no need to go further */ 769 return 0; 770 } 771 772 /* See if we can get the resource table */ 773 err = stm32_rproc_get_syscon(np, "st,syscfg-rsc-tbl", 774 &ddata->rsctbl); 775 if (err) { 776 /* no rsc table syscon (optional) */ 777 dev_warn(dev, "rsc tbl syscon not supported\n"); 778 } 779 780 return 0; 781 } 782 783 static int stm32_rproc_get_m4_status(struct stm32_rproc *ddata, 784 unsigned int *state) 785 { 786 /* See stm32_rproc_parse_dt() */ 787 if (!ddata->m4_state.map) { 788 /* 789 * We couldn't get the coprocessor's state, assume 790 * it is not running. 791 */ 792 *state = M4_STATE_OFF; 793 return 0; 794 } 795 796 return regmap_read(ddata->m4_state.map, ddata->m4_state.reg, state); 797 } 798 799 static int stm32_rproc_probe(struct platform_device *pdev) 800 { 801 struct device *dev = &pdev->dev; 802 struct stm32_rproc *ddata; 803 struct device_node *np = dev->of_node; 804 struct rproc *rproc; 805 unsigned int state; 806 int ret; 807 808 ret = dma_coerce_mask_and_coherent(dev, DMA_BIT_MASK(32)); 809 if (ret) 810 return ret; 811 812 rproc = rproc_alloc(dev, np->name, &st_rproc_ops, NULL, sizeof(*ddata)); 813 if (!rproc) 814 return -ENOMEM; 815 816 ddata = rproc->priv; 817 818 rproc_coredump_set_elf_info(rproc, ELFCLASS32, EM_NONE); 819 820 ret = stm32_rproc_parse_dt(pdev, ddata, &rproc->auto_boot); 821 if (ret) 822 goto free_rproc; 823 824 ret = stm32_rproc_of_memory_translations(pdev, ddata); 825 if (ret) 826 goto free_rproc; 827 828 ret = stm32_rproc_get_m4_status(ddata, &state); 829 if (ret) 830 goto free_rproc; 831 832 if (state == M4_STATE_CRUN) 833 rproc->state = RPROC_DETACHED; 834 835 rproc->has_iommu = false; 836 ddata->workqueue = create_workqueue(dev_name(dev)); 837 if (!ddata->workqueue) { 838 dev_err(dev, "cannot create workqueue\n"); 839 ret = -ENOMEM; 840 goto free_resources; 841 } 842 843 platform_set_drvdata(pdev, rproc); 844 845 ret = stm32_rproc_request_mbox(rproc); 846 if (ret) 847 goto free_wkq; 848 849 ret = rproc_add(rproc); 850 if (ret) 851 goto free_mb; 852 853 return 0; 854 855 free_mb: 856 stm32_rproc_free_mbox(rproc); 857 free_wkq: 858 destroy_workqueue(ddata->workqueue); 859 free_resources: 860 rproc_resource_cleanup(rproc); 861 free_rproc: 862 if (device_may_wakeup(dev)) { 863 dev_pm_clear_wake_irq(dev); 864 device_init_wakeup(dev, false); 865 } 866 rproc_free(rproc); 867 return ret; 868 } 869 870 static int stm32_rproc_remove(struct platform_device *pdev) 871 { 872 struct rproc *rproc = platform_get_drvdata(pdev); 873 struct stm32_rproc *ddata = rproc->priv; 874 struct device *dev = &pdev->dev; 875 876 if (atomic_read(&rproc->power) > 0) 877 rproc_shutdown(rproc); 878 879 rproc_del(rproc); 880 stm32_rproc_free_mbox(rproc); 881 destroy_workqueue(ddata->workqueue); 882 883 if (device_may_wakeup(dev)) { 884 dev_pm_clear_wake_irq(dev); 885 device_init_wakeup(dev, false); 886 } 887 rproc_free(rproc); 888 889 return 0; 890 } 891 892 static int __maybe_unused stm32_rproc_suspend(struct device *dev) 893 { 894 struct rproc *rproc = dev_get_drvdata(dev); 895 struct stm32_rproc *ddata = rproc->priv; 896 897 if (device_may_wakeup(dev)) 898 return enable_irq_wake(ddata->wdg_irq); 899 900 return 0; 901 } 902 903 static int __maybe_unused stm32_rproc_resume(struct device *dev) 904 { 905 struct rproc *rproc = dev_get_drvdata(dev); 906 struct stm32_rproc *ddata = rproc->priv; 907 908 if (device_may_wakeup(dev)) 909 return disable_irq_wake(ddata->wdg_irq); 910 911 return 0; 912 } 913 914 static SIMPLE_DEV_PM_OPS(stm32_rproc_pm_ops, 915 stm32_rproc_suspend, stm32_rproc_resume); 916 917 static struct platform_driver stm32_rproc_driver = { 918 .probe = stm32_rproc_probe, 919 .remove = stm32_rproc_remove, 920 .driver = { 921 .name = "stm32-rproc", 922 .pm = &stm32_rproc_pm_ops, 923 .of_match_table = of_match_ptr(stm32_rproc_match), 924 }, 925 }; 926 module_platform_driver(stm32_rproc_driver); 927 928 MODULE_DESCRIPTION("STM32 Remote Processor Control Driver"); 929 MODULE_AUTHOR("Ludovic Barre <ludovic.barre@st.com>"); 930 MODULE_AUTHOR("Fabien Dessenne <fabien.dessenne@st.com>"); 931 MODULE_LICENSE("GPL v2"); 932 933