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