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