1 // SPDX-License-Identifier: GPL-2.0-only
3 * PRU-ICSS remoteproc driver for various TI SoCs
5 * Copyright (C) 2014-2020 Texas Instruments Incorporated - https://www.ti.com/
8 * Suman Anna <s-anna@ti.com>
9 * Andrew F. Davis <afd@ti.com>
10 * Grzegorz Jaszczyk <grzegorz.jaszczyk@linaro.org> for Texas Instruments
13 #include <linux/bitops.h>
14 #include <linux/irqdomain.h>
15 #include <linux/module.h>
16 #include <linux/of_device.h>
17 #include <linux/of_irq.h>
18 #include <linux/pruss_driver.h>
19 #include <linux/remoteproc.h>
21 #include "remoteproc_internal.h"
22 #include "remoteproc_elf_helpers.h"
23 #include "pru_rproc.h"
25 /* PRU_ICSS_PRU_CTRL registers */
26 #define PRU_CTRL_CTRL 0x0000
27 #define PRU_CTRL_STS 0x0004
29 /* CTRL register bit-fields */
30 #define CTRL_CTRL_SOFT_RST_N BIT(0)
31 #define CTRL_CTRL_EN BIT(1)
32 #define CTRL_CTRL_SLEEPING BIT(2)
33 #define CTRL_CTRL_CTR_EN BIT(3)
34 #define CTRL_CTRL_SINGLE_STEP BIT(8)
35 #define CTRL_CTRL_RUNSTATE BIT(15)
37 /* PRU Core IRAM address masks */
38 #define PRU_IRAM_ADDR_MASK 0x3ffff
39 #define PRU0_IRAM_ADDR_MASK 0x34000
40 #define PRU1_IRAM_ADDR_MASK 0x38000
42 /* PRU device addresses for various type of PRU RAMs */
43 #define PRU_IRAM_DA 0 /* Instruction RAM */
44 #define PRU_PDRAM_DA 0 /* Primary Data RAM */
45 #define PRU_SDRAM_DA 0x2000 /* Secondary Data RAM */
46 #define PRU_SHRDRAM_DA 0x10000 /* Shared Data RAM */
48 #define MAX_PRU_SYS_EVENTS 160
51 * enum pru_iomem - PRU core memory/register range identifiers
53 * @PRU_IOMEM_IRAM: PRU Instruction RAM range
54 * @PRU_IOMEM_CTRL: PRU Control register range
55 * @PRU_IOMEM_DEBUG: PRU Debug register range
56 * @PRU_IOMEM_MAX: just keep this one at the end
66 * struct pru_rproc - PRU remoteproc structure
67 * @id: id of the PRU core within the PRUSS
68 * @dev: PRU core device pointer
69 * @pruss: back-reference to parent PRUSS structure
70 * @rproc: remoteproc pointer for this PRU core
71 * @mem_regions: data for each of the PRU memory regions
72 * @fw_name: name of firmware image used during loading
73 * @mapped_irq: virtual interrupt numbers of created fw specific mapping
74 * @pru_interrupt_map: pointer to interrupt mapping description (firmware)
75 * @pru_interrupt_map_sz: pru_interrupt_map size
76 * @evt_count: number of mapped events
83 struct pruss_mem_region mem_regions[PRU_IOMEM_MAX];
85 unsigned int *mapped_irq;
86 struct pru_irq_rsc *pru_interrupt_map;
87 size_t pru_interrupt_map_sz;
91 static inline u32 pru_control_read_reg(struct pru_rproc *pru, unsigned int reg)
93 return readl_relaxed(pru->mem_regions[PRU_IOMEM_CTRL].va + reg);
97 void pru_control_write_reg(struct pru_rproc *pru, unsigned int reg, u32 val)
99 writel_relaxed(val, pru->mem_regions[PRU_IOMEM_CTRL].va + reg);
102 static void pru_dispose_irq_mapping(struct pru_rproc *pru)
104 while (pru->evt_count--) {
105 if (pru->mapped_irq[pru->evt_count] > 0)
106 irq_dispose_mapping(pru->mapped_irq[pru->evt_count]);
109 kfree(pru->mapped_irq);
113 * Parse the custom PRU interrupt map resource and configure the INTC
116 static int pru_handle_intrmap(struct rproc *rproc)
118 struct device *dev = rproc->dev.parent;
119 struct pru_rproc *pru = rproc->priv;
120 struct pru_irq_rsc *rsc = pru->pru_interrupt_map;
121 struct irq_fwspec fwspec;
122 struct device_node *irq_parent;
125 /* not having pru_interrupt_map is not an error */
129 /* currently supporting only type 0 */
130 if (rsc->type != 0) {
131 dev_err(dev, "unsupported rsc type: %d\n", rsc->type);
135 if (rsc->num_evts > MAX_PRU_SYS_EVENTS)
138 if (sizeof(*rsc) + rsc->num_evts * sizeof(struct pruss_int_map) !=
139 pru->pru_interrupt_map_sz)
142 pru->evt_count = rsc->num_evts;
143 pru->mapped_irq = kcalloc(pru->evt_count, sizeof(unsigned int),
145 if (!pru->mapped_irq)
149 * parse and fill in system event to interrupt channel and
150 * channel-to-host mapping
152 irq_parent = of_irq_find_parent(pru->dev->of_node);
154 kfree(pru->mapped_irq);
158 fwspec.fwnode = of_node_to_fwnode(irq_parent);
159 fwspec.param_count = 3;
160 for (i = 0; i < pru->evt_count; i++) {
161 fwspec.param[0] = rsc->pru_intc_map[i].event;
162 fwspec.param[1] = rsc->pru_intc_map[i].chnl;
163 fwspec.param[2] = rsc->pru_intc_map[i].host;
165 dev_dbg(dev, "mapping%d: event %d, chnl %d, host %d\n",
166 i, fwspec.param[0], fwspec.param[1], fwspec.param[2]);
168 pru->mapped_irq[i] = irq_create_fwspec_mapping(&fwspec);
169 if (!pru->mapped_irq[i]) {
170 dev_err(dev, "failed to get virq\n");
171 ret = pru->mapped_irq[i];
179 pru_dispose_irq_mapping(pru);
184 static int pru_rproc_start(struct rproc *rproc)
186 struct device *dev = &rproc->dev;
187 struct pru_rproc *pru = rproc->priv;
191 dev_dbg(dev, "starting PRU%d: entry-point = 0x%llx\n",
192 pru->id, (rproc->bootaddr >> 2));
194 ret = pru_handle_intrmap(rproc);
196 * reset references to pru interrupt map - they will stop being valid
197 * after rproc_start returns
199 pru->pru_interrupt_map = NULL;
200 pru->pru_interrupt_map_sz = 0;
204 val = CTRL_CTRL_EN | ((rproc->bootaddr >> 2) << 16);
205 pru_control_write_reg(pru, PRU_CTRL_CTRL, val);
210 static int pru_rproc_stop(struct rproc *rproc)
212 struct device *dev = &rproc->dev;
213 struct pru_rproc *pru = rproc->priv;
216 dev_dbg(dev, "stopping PRU%d\n", pru->id);
218 val = pru_control_read_reg(pru, PRU_CTRL_CTRL);
219 val &= ~CTRL_CTRL_EN;
220 pru_control_write_reg(pru, PRU_CTRL_CTRL, val);
222 /* dispose irq mapping - new firmware can provide new mapping */
224 pru_dispose_irq_mapping(pru);
230 * Convert PRU device address (data spaces only) to kernel virtual address.
232 * Each PRU has access to all data memories within the PRUSS, accessible at
233 * different ranges. So, look through both its primary and secondary Data
234 * RAMs as well as any shared Data RAM to convert a PRU device address to
235 * kernel virtual address. Data RAM0 is primary Data RAM for PRU0 and Data
236 * RAM1 is primary Data RAM for PRU1.
238 static void *pru_d_da_to_va(struct pru_rproc *pru, u32 da, size_t len)
240 struct pruss_mem_region dram0, dram1, shrd_ram;
241 struct pruss *pruss = pru->pruss;
248 dram0 = pruss->mem_regions[PRUSS_MEM_DRAM0];
249 dram1 = pruss->mem_regions[PRUSS_MEM_DRAM1];
250 /* PRU1 has its local RAM addresses reversed */
253 shrd_ram = pruss->mem_regions[PRUSS_MEM_SHRD_RAM2];
255 if (da >= PRU_PDRAM_DA && da + len <= PRU_PDRAM_DA + dram0.size) {
256 offset = da - PRU_PDRAM_DA;
257 va = (__force void *)(dram0.va + offset);
258 } else if (da >= PRU_SDRAM_DA &&
259 da + len <= PRU_SDRAM_DA + dram1.size) {
260 offset = da - PRU_SDRAM_DA;
261 va = (__force void *)(dram1.va + offset);
262 } else if (da >= PRU_SHRDRAM_DA &&
263 da + len <= PRU_SHRDRAM_DA + shrd_ram.size) {
264 offset = da - PRU_SHRDRAM_DA;
265 va = (__force void *)(shrd_ram.va + offset);
272 * Convert PRU device address (instruction space) to kernel virtual address.
274 * A PRU does not have an unified address space. Each PRU has its very own
275 * private Instruction RAM, and its device address is identical to that of
276 * its primary Data RAM device address.
278 static void *pru_i_da_to_va(struct pru_rproc *pru, u32 da, size_t len)
286 if (da >= PRU_IRAM_DA &&
287 da + len <= PRU_IRAM_DA + pru->mem_regions[PRU_IOMEM_IRAM].size) {
288 offset = da - PRU_IRAM_DA;
289 va = (__force void *)(pru->mem_regions[PRU_IOMEM_IRAM].va +
297 * Provide address translations for only PRU Data RAMs through the remoteproc
298 * core for any PRU client drivers. The PRU Instruction RAM access is restricted
299 * only to the PRU loader code.
301 static void *pru_rproc_da_to_va(struct rproc *rproc, u64 da, size_t len)
303 struct pru_rproc *pru = rproc->priv;
305 return pru_d_da_to_va(pru, da, len);
308 /* PRU-specific address translator used by PRU loader. */
309 static void *pru_da_to_va(struct rproc *rproc, u64 da, size_t len, bool is_iram)
311 struct pru_rproc *pru = rproc->priv;
315 va = pru_i_da_to_va(pru, da, len);
317 va = pru_d_da_to_va(pru, da, len);
322 static struct rproc_ops pru_rproc_ops = {
323 .start = pru_rproc_start,
324 .stop = pru_rproc_stop,
325 .da_to_va = pru_rproc_da_to_va,
329 pru_rproc_load_elf_segments(struct rproc *rproc, const struct firmware *fw)
331 struct device *dev = &rproc->dev;
332 struct elf32_hdr *ehdr;
333 struct elf32_phdr *phdr;
335 const u8 *elf_data = fw->data;
337 ehdr = (struct elf32_hdr *)elf_data;
338 phdr = (struct elf32_phdr *)(elf_data + ehdr->e_phoff);
340 /* go through the available ELF segments */
341 for (i = 0; i < ehdr->e_phnum; i++, phdr++) {
342 u32 da = phdr->p_paddr;
343 u32 memsz = phdr->p_memsz;
344 u32 filesz = phdr->p_filesz;
345 u32 offset = phdr->p_offset;
349 if (phdr->p_type != PT_LOAD || !filesz)
352 dev_dbg(dev, "phdr: type %d da 0x%x memsz 0x%x filesz 0x%x\n",
353 phdr->p_type, da, memsz, filesz);
355 if (filesz > memsz) {
356 dev_err(dev, "bad phdr filesz 0x%x memsz 0x%x\n",
362 if (offset + filesz > fw->size) {
363 dev_err(dev, "truncated fw: need 0x%x avail 0x%zx\n",
364 offset + filesz, fw->size);
369 /* grab the kernel address for this device address */
370 is_iram = phdr->p_flags & PF_X;
371 ptr = pru_da_to_va(rproc, da, memsz, is_iram);
373 dev_err(dev, "bad phdr da 0x%x mem 0x%x\n", da, memsz);
378 memcpy(ptr, elf_data + phdr->p_offset, filesz);
380 /* skip the memzero logic performed by remoteproc ELF loader */
387 pru_rproc_find_interrupt_map(struct device *dev, const struct firmware *fw)
389 struct elf32_shdr *shdr, *name_table_shdr;
390 const char *name_table;
391 const u8 *elf_data = fw->data;
392 struct elf32_hdr *ehdr = (struct elf32_hdr *)elf_data;
393 u16 shnum = ehdr->e_shnum;
394 u16 shstrndx = ehdr->e_shstrndx;
397 /* first, get the section header */
398 shdr = (struct elf32_shdr *)(elf_data + ehdr->e_shoff);
399 /* compute name table section header entry in shdr array */
400 name_table_shdr = shdr + shstrndx;
401 /* finally, compute the name table section address in elf */
402 name_table = elf_data + name_table_shdr->sh_offset;
404 for (i = 0; i < shnum; i++, shdr++) {
405 u32 size = shdr->sh_size;
406 u32 offset = shdr->sh_offset;
407 u32 name = shdr->sh_name;
409 if (strcmp(name_table + name, ".pru_irq_map"))
412 /* make sure we have the entire irq map */
413 if (offset + size > fw->size || offset + size < size) {
414 dev_err(dev, ".pru_irq_map section truncated\n");
415 return ERR_PTR(-EINVAL);
418 /* make sure irq map has at least the header */
419 if (sizeof(struct pru_irq_rsc) > size) {
420 dev_err(dev, "header-less .pru_irq_map section\n");
421 return ERR_PTR(-EINVAL);
427 dev_dbg(dev, "no .pru_irq_map section found for this fw\n");
433 * Use a custom parse_fw callback function for dealing with PRU firmware
436 * The firmware blob can contain optional ELF sections: .resource_table section
437 * and .pru_irq_map one. The second one contains the PRUSS interrupt mapping
438 * description, which needs to be setup before powering on the PRU core. To
439 * avoid RAM wastage this ELF section is not mapped to any ELF segment (by the
440 * firmware linker) and therefore is not loaded to PRU memory.
442 static int pru_rproc_parse_fw(struct rproc *rproc, const struct firmware *fw)
444 struct device *dev = &rproc->dev;
445 struct pru_rproc *pru = rproc->priv;
446 const u8 *elf_data = fw->data;
448 u8 class = fw_elf_get_class(fw);
452 /* load optional rsc table */
453 ret = rproc_elf_load_rsc_table(rproc, fw);
455 dev_dbg(&rproc->dev, "no resource table found for this fw\n");
459 /* find .pru_interrupt_map section, not having it is not an error */
460 shdr = pru_rproc_find_interrupt_map(dev, fw);
462 return PTR_ERR(shdr);
467 /* preserve pointer to PRU interrupt map together with it size */
468 sh_offset = elf_shdr_get_sh_offset(class, shdr);
469 pru->pru_interrupt_map = (struct pru_irq_rsc *)(elf_data + sh_offset);
470 pru->pru_interrupt_map_sz = elf_shdr_get_sh_size(class, shdr);
476 * Compute PRU id based on the IRAM addresses. The PRU IRAMs are
477 * always at a particular offset within the PRUSS address space.
479 static int pru_rproc_set_id(struct pru_rproc *pru)
483 switch (pru->mem_regions[PRU_IOMEM_IRAM].pa & PRU_IRAM_ADDR_MASK) {
484 case PRU0_IRAM_ADDR_MASK:
487 case PRU1_IRAM_ADDR_MASK:
497 static int pru_rproc_probe(struct platform_device *pdev)
499 struct device *dev = &pdev->dev;
500 struct device_node *np = dev->of_node;
501 struct platform_device *ppdev = to_platform_device(dev->parent);
502 struct pru_rproc *pru;
504 struct rproc *rproc = NULL;
505 struct resource *res;
507 const char *mem_names[PRU_IOMEM_MAX] = { "iram", "control", "debug" };
509 ret = of_property_read_string(np, "firmware-name", &fw_name);
511 dev_err(dev, "unable to retrieve firmware-name %d\n", ret);
515 rproc = devm_rproc_alloc(dev, pdev->name, &pru_rproc_ops, fw_name,
518 dev_err(dev, "rproc_alloc failed\n");
521 /* use a custom load function to deal with PRU-specific quirks */
522 rproc->ops->load = pru_rproc_load_elf_segments;
524 /* use a custom parse function to deal with PRU-specific resources */
525 rproc->ops->parse_fw = pru_rproc_parse_fw;
527 /* error recovery is not supported for PRUs */
528 rproc->recovery_disabled = true;
531 * rproc_add will auto-boot the processor normally, but this is not
532 * desired with PRU client driven boot-flow methodology. A PRU
533 * application/client driver will boot the corresponding PRU
534 * remote-processor as part of its state machine either through the
535 * remoteproc sysfs interface or through the equivalent kernel API.
537 rproc->auto_boot = false;
541 pru->pruss = platform_get_drvdata(ppdev);
543 pru->fw_name = fw_name;
545 for (i = 0; i < ARRAY_SIZE(mem_names); i++) {
546 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
548 pru->mem_regions[i].va = devm_ioremap_resource(dev, res);
549 if (IS_ERR(pru->mem_regions[i].va)) {
550 dev_err(dev, "failed to parse and map memory resource %d %s\n",
552 ret = PTR_ERR(pru->mem_regions[i].va);
555 pru->mem_regions[i].pa = res->start;
556 pru->mem_regions[i].size = resource_size(res);
558 dev_dbg(dev, "memory %8s: pa %pa size 0x%zx va %pK\n",
559 mem_names[i], &pru->mem_regions[i].pa,
560 pru->mem_regions[i].size, pru->mem_regions[i].va);
563 ret = pru_rproc_set_id(pru);
567 platform_set_drvdata(pdev, rproc);
569 ret = devm_rproc_add(dev, pru->rproc);
571 dev_err(dev, "rproc_add failed: %d\n", ret);
575 dev_dbg(dev, "PRU rproc node %pOF probed successfully\n", np);
580 static int pru_rproc_remove(struct platform_device *pdev)
582 struct device *dev = &pdev->dev;
583 struct rproc *rproc = platform_get_drvdata(pdev);
585 dev_dbg(dev, "%s: removing rproc %s\n", __func__, rproc->name);
590 static const struct of_device_id pru_rproc_match[] = {
591 { .compatible = "ti,am3356-pru", },
592 { .compatible = "ti,am4376-pru", },
593 { .compatible = "ti,am5728-pru", },
594 { .compatible = "ti,k2g-pru", },
597 MODULE_DEVICE_TABLE(of, pru_rproc_match);
599 static struct platform_driver pru_rproc_driver = {
602 .of_match_table = pru_rproc_match,
603 .suppress_bind_attrs = true,
605 .probe = pru_rproc_probe,
606 .remove = pru_rproc_remove,
608 module_platform_driver(pru_rproc_driver);
610 MODULE_AUTHOR("Suman Anna <s-anna@ti.com>");
611 MODULE_AUTHOR("Andrew F. Davis <afd@ti.com>");
612 MODULE_AUTHOR("Grzegorz Jaszczyk <grzegorz.jaszczyk@linaro.org>");
613 MODULE_DESCRIPTION("PRU-ICSS Remote Processor Driver");
614 MODULE_LICENSE("GPL v2");