1 // SPDX-License-Identifier: GPL-2.0
3 * PRU-RTU remoteproc driver for various SoCs
5 * Copyright (C) 2018 Texas Instruments Incorporated - https://www.ti.com/
11 #include <dm/of_access.h>
12 #include <remoteproc.h>
19 #include <power-domain.h>
20 #include <linux/pruss_driver.h>
21 #include <dm/device_compat.h>
23 /* PRU_ICSS_PRU_CTRL registers */
24 #define PRU_CTRL_CTRL 0x0000
25 #define PRU_CTRL_STS 0x0004
26 #define PRU_CTRL_WAKEUP_EN 0x0008
27 #define PRU_CTRL_CYCLE 0x000C
28 #define PRU_CTRL_STALL 0x0010
29 #define PRU_CTRL_CTBIR0 0x0020
30 #define PRU_CTRL_CTBIR1 0x0024
31 #define PRU_CTRL_CTPPR0 0x0028
32 #define PRU_CTRL_CTPPR1 0x002C
34 /* CTRL register bit-fields */
35 #define CTRL_CTRL_SOFT_RST_N BIT(0)
36 #define CTRL_CTRL_EN BIT(1)
37 #define CTRL_CTRL_SLEEPING BIT(2)
38 #define CTRL_CTRL_CTR_EN BIT(3)
39 #define CTRL_CTRL_SINGLE_STEP BIT(8)
40 #define CTRL_CTRL_RUNSTATE BIT(15)
42 #define RPROC_FLAGS_SHIFT 16
43 #define RPROC_FLAGS_NONE 0
44 #define RPROC_FLAGS_ELF_PHDR BIT(0 + RPROC_FLAGS_SHIFT)
45 #define RPROC_FLAGS_ELF_SHDR BIT(1 + RPROC_FLAGS_SHIFT)
48 * enum pru_mem - PRU core memory range identifiers
60 phys_addr_t pru_debug;
61 fdt_size_t pru_iramsz;
62 fdt_size_t pru_ctrlsz;
63 fdt_size_t pru_debugsz;
71 struct pruss *prusspriv;
74 static inline u32 pru_control_read_reg(struct pru_privdata *pru, unsigned int reg)
76 return readl(pru->pru_ctrl + reg);
80 void pru_control_write_reg(struct pru_privdata *pru, unsigned int reg, u32 val)
82 writel(val, pru->pru_ctrl + reg);
86 void pru_control_set_reg(struct pru_privdata *pru, unsigned int reg,
91 val = pru_control_read_reg(pru, reg);
94 pru_control_write_reg(pru, reg, val);
98 * pru_rproc_set_ctable() - set the constant table index for the PRU
99 * @rproc: the rproc instance of the PRU
100 * @c: constant table index to set
101 * @addr: physical address to set it to
103 static int pru_rproc_set_ctable(struct pru_privdata *pru, enum pru_ctable_idx c, u32 addr)
110 /* pointer is 16 bit and index is 8-bit so mask out the rest */
111 idx_mask = (c >= PRU_C28) ? 0xFFFF : 0xFF;
113 /* ctable uses bit 8 and upwards only */
114 idx = (addr >> 8) & idx_mask;
116 /* configurable ctable (i.e. C24) starts at PRU_CTRL_CTBIR0 */
117 reg = PRU_CTRL_CTBIR0 + 4 * (c >> 1);
118 mask = idx_mask << (16 * (c & 1));
119 set = idx << (16 * (c & 1));
121 pru_control_set_reg(pru, reg, mask, set);
127 * pru_start() - start the pru processor
128 * @dev: corresponding k3 remote processor device
130 * Return: 0 if all goes good, else appropriate error message.
132 static int pru_start(struct udevice *dev)
134 struct pru_privdata *priv;
137 priv = dev_get_priv(dev);
139 pru_rproc_set_ctable(priv, PRU_C28, 0x100 << 8);
141 val = CTRL_CTRL_EN | ((priv->bootaddr >> 2) << 16);
142 writel(val, priv->pru_ctrl + PRU_CTRL_CTRL);
148 * pru_stop() - Stop pru processor
149 * @dev: corresponding k3 remote processor device
151 * Return: 0 if all goes good, else appropriate error message.
153 static int pru_stop(struct udevice *dev)
155 struct pru_privdata *priv;
158 priv = dev_get_priv(dev);
160 val = readl(priv->pru_ctrl + PRU_CTRL_CTRL);
161 val &= ~CTRL_CTRL_EN;
162 writel(val, priv->pru_ctrl + PRU_CTRL_CTRL);
168 * pru_init() - Initialize the remote processor
169 * @dev: rproc device pointer
171 * Return: 0 if all went ok, else return appropriate error
173 static int pru_init(struct udevice *dev)
179 * Convert PRU device address (data spaces only) to kernel virtual address
181 * Each PRU has access to all data memories within the PRUSS, accessible at
182 * different ranges. So, look through both its primary and secondary Data
183 * RAMs as well as any shared Data RAM to convert a PRU device address to
184 * kernel virtual address. Data RAM0 is primary Data RAM for PRU0 and Data
185 * RAM1 is primary Data RAM for PRU1.
187 static void *pru_d_da_to_pa(struct pru_privdata *priv, u32 da, int len)
191 phys_addr_t dram0, dram1, shrdram2;
192 u32 dram0sz, dram1sz, shrdram2sz;
197 dram0 = priv->prusspriv->mem_regions[PRUSS_MEM_DRAM0].pa;
198 dram1 = priv->prusspriv->mem_regions[PRUSS_MEM_DRAM1].pa;
199 shrdram2 = priv->prusspriv->mem_regions[PRUSS_MEM_SHRD_RAM2].pa;
200 dram0sz = priv->prusspriv->mem_regions[PRUSS_MEM_DRAM0].size;
201 dram1sz = priv->prusspriv->mem_regions[PRUSS_MEM_DRAM1].size;
202 shrdram2sz = priv->prusspriv->mem_regions[PRUSS_MEM_SHRD_RAM2].size;
204 /* PRU1 has its local RAM addresses reversed */
209 dram0 = priv->prusspriv->mem_regions[PRUSS_MEM_DRAM1].pa;
210 dram0sz = priv->prusspriv->mem_regions[PRUSS_MEM_DRAM1].size;
213 if (da >= priv->pdram_da && da + len <= priv->pdram_da + dram0sz) {
214 offset = da - priv->pdram_da;
215 pa = (__force void *)(dram0 + offset);
216 } else if (da >= priv->sdram_da &&
217 da + len <= priv->sdram_da + dram1sz) {
218 offset = da - priv->sdram_da;
219 pa = (__force void *)(dram1 + offset);
220 } else if (da >= priv->shrdram_da &&
221 da + len <= priv->shrdram_da + shrdram2sz) {
222 offset = da - priv->shrdram_da;
223 pa = (__force void *)(shrdram2 + offset);
230 * Convert PRU device address (instruction space) to kernel virtual address
232 * A PRU does not have an unified address space. Each PRU has its very own
233 * private Instruction RAM, and its device address is identical to that of
234 * its primary Data RAM device address.
236 static void *pru_i_da_to_pa(struct pru_privdata *priv, u32 da, int len)
244 if (da >= priv->iram_da &&
245 da + len <= priv->iram_da + priv->pru_iramsz) {
246 offset = da - priv->iram_da;
247 pa = (__force void *)(priv->pru_iram + offset);
253 /* PRU-specific address translator */
254 static void *pru_da_to_pa(struct pru_privdata *priv, u64 da, int len, u32 flags)
259 exec_flag = ((flags & RPROC_FLAGS_ELF_SHDR) ? flags & SHF_EXECINSTR :
260 ((flags & RPROC_FLAGS_ELF_PHDR) ? flags & PF_X : 0));
263 pa = pru_i_da_to_pa(priv, da, len);
265 pa = pru_d_da_to_pa(priv, da, len);
271 * Custom memory copy implementation for ICSSG PRU/RTU Cores
273 * The ICSSG PRU/RTU cores have a memory copying issue with IRAM memories, that
274 * is not seen on previous generation SoCs. The data is reflected properly in
275 * the IRAM memories only for integer (4-byte) copies. Any unaligned copies
276 * result in all the other pre-existing bytes zeroed out within that 4-byte
277 * boundary, thereby resulting in wrong text/code in the IRAMs. Also, the
278 * IRAM memory port interface does not allow any 8-byte copies (as commonly
279 * used by ARM64 memcpy implementation) and throws an exception. The DRAM
280 * memory ports do not show this behavior. Use this custom copying function
281 * to properly load the PRU/RTU firmware images on all memories for simplicity.
283 * TODO: Improve the function to deal with additional corner cases like
284 * unaligned copy sizes or sub-integer trailing bytes when the need arises.
286 static int pru_rproc_memcpy(void *dest, void *src, size_t count)
290 int size = count / 4;
293 /* limited to 4-byte aligned addresses and copy sizes */
294 if ((long)dest % 4 || count % 4)
297 /* src offsets in ELF firmware image can be non-aligned */
299 tmp_src = malloc(count);
303 memcpy(tmp_src, src, count);
316 * pru_load() - Load pru firmware
317 * @dev: corresponding k3 remote processor device
318 * @addr: Address on the RAM from which firmware is to be loaded
319 * @size: Size of the pru firmware in bytes
321 * Return: 0 if all goes good, else appropriate error message.
323 static int pru_load(struct udevice *dev, ulong addr, ulong size)
325 struct pru_privdata *priv;
330 priv = dev_get_priv(dev);
332 ehdr = (Elf32_Ehdr *)addr;
333 phdr = (Elf32_Phdr *)(addr + ehdr->e_phoff);
335 /* go through the available ELF segments */
336 for (i = 0; i < ehdr->e_phnum; i++, phdr++) {
337 u32 da = phdr->p_paddr;
338 u32 memsz = phdr->p_memsz;
339 u32 filesz = phdr->p_filesz;
340 u32 offset = phdr->p_offset;
343 if (phdr->p_type != PT_LOAD)
346 dev_dbg(dev, "phdr: type %d da 0x%x memsz 0x%x filesz 0x%x\n",
347 phdr->p_type, da, memsz, filesz);
349 if (filesz > memsz) {
350 dev_dbg(dev, "bad phdr filesz 0x%x memsz 0x%x\n",
356 if (offset + filesz > size) {
357 dev_dbg(dev, "truncated fw: need 0x%x avail 0x%zx\n",
358 offset + filesz, size);
363 /* grab the kernel address for this device address */
364 ptr = pru_da_to_pa(priv, da, memsz,
365 RPROC_FLAGS_ELF_PHDR | phdr->p_flags);
367 dev_dbg(dev, "bad phdr da 0x%x mem 0x%x\n", da, memsz);
372 /* skip the memzero logic performed by remoteproc ELF loader */
376 ret = pru_rproc_memcpy(ptr,
377 (void *)addr + phdr->p_offset, filesz);
379 dev_dbg(dev, "PRU custom memory copy failed for da 0x%x memsz 0x%x\n",
385 priv->bootaddr = ehdr->e_entry;
390 static const struct dm_rproc_ops pru_ops = {
397 static void pru_set_id(struct pru_privdata *priv, struct udevice *dev)
401 if (device_is_compatible(dev, "ti,am654-rtu") ||
402 device_is_compatible(dev, "ti,am642-rtu"))
405 if (device_is_compatible(dev, "ti,am654-tx-pru") ||
406 device_is_compatible(dev, "ti,am642-tx-pru"))
409 if ((priv->pru_iram & mask2) == mask2)
416 * pru_probe() - Basic probe
417 * @dev: corresponding k3 remote processor device
419 * Return: 0 if all goes good, else appropriate error message.
421 static int pru_probe(struct udevice *dev)
423 struct pru_privdata *priv;
426 node = dev_ofnode(dev);
428 priv = dev_get_priv(dev);
429 priv->prusspriv = dev_get_priv(dev->parent);
431 priv->pru_iram = devfdt_get_addr_size_index(dev, PRU_MEM_IRAM,
433 priv->pru_ctrl = devfdt_get_addr_size_index(dev, PRU_MEM_CTRL,
435 priv->pru_debug = devfdt_get_addr_size_index(dev, PRU_MEM_DEBUG,
440 priv->sdram_da = 0x2000;
441 priv->shrdram_da = 0x10000;
443 pru_set_id(priv, dev);
448 static const struct udevice_id pru_ids[] = {
449 { .compatible = "ti,am654-pru"},
450 { .compatible = "ti,am654-rtu"},
451 { .compatible = "ti,am654-tx-pru" },
452 { .compatible = "ti,am642-pru"},
453 { .compatible = "ti,am642-rtu"},
454 { .compatible = "ti,am642-tx-pru" },
458 U_BOOT_DRIVER(pru) = {
461 .id = UCLASS_REMOTEPROC,
464 .priv_auto = sizeof(struct pru_privdata),