1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (C) IoT.bzh 2021 4 */ 5 6 #include <linux/limits.h> 7 #include <linux/module.h> 8 #include <linux/of_device.h> 9 #include <linux/of_reserved_mem.h> 10 #include <linux/pm_runtime.h> 11 #include <linux/remoteproc.h> 12 #include <linux/reset.h> 13 #include <linux/soc/renesas/rcar-rst.h> 14 15 #include "remoteproc_internal.h" 16 17 struct rcar_rproc { 18 struct reset_control *rst; 19 }; 20 21 static int rcar_rproc_mem_alloc(struct rproc *rproc, 22 struct rproc_mem_entry *mem) 23 { 24 struct device *dev = &rproc->dev; 25 void *va; 26 27 dev_dbg(dev, "map memory: %pa+%zx\n", &mem->dma, mem->len); 28 va = ioremap_wc(mem->dma, mem->len); 29 if (!va) { 30 dev_err(dev, "Unable to map memory region: %pa+%zx\n", 31 &mem->dma, mem->len); 32 return -ENOMEM; 33 } 34 35 /* Update memory entry va */ 36 mem->va = va; 37 38 return 0; 39 } 40 41 static int rcar_rproc_mem_release(struct rproc *rproc, 42 struct rproc_mem_entry *mem) 43 { 44 dev_dbg(&rproc->dev, "unmap memory: %pa\n", &mem->dma); 45 iounmap(mem->va); 46 47 return 0; 48 } 49 50 static int rcar_rproc_prepare(struct rproc *rproc) 51 { 52 struct device *dev = rproc->dev.parent; 53 struct device_node *np = dev->of_node; 54 struct of_phandle_iterator it; 55 struct rproc_mem_entry *mem; 56 struct reserved_mem *rmem; 57 u32 da; 58 59 /* Register associated reserved memory regions */ 60 of_phandle_iterator_init(&it, np, "memory-region", NULL, 0); 61 while (of_phandle_iterator_next(&it) == 0) { 62 63 rmem = of_reserved_mem_lookup(it.node); 64 if (!rmem) { 65 of_node_put(it.node); 66 dev_err(&rproc->dev, 67 "unable to acquire memory-region\n"); 68 return -EINVAL; 69 } 70 71 if (rmem->base > U32_MAX) { 72 of_node_put(it.node); 73 return -EINVAL; 74 } 75 76 /* No need to translate pa to da, R-Car use same map */ 77 da = rmem->base; 78 mem = rproc_mem_entry_init(dev, NULL, 79 rmem->base, 80 rmem->size, da, 81 rcar_rproc_mem_alloc, 82 rcar_rproc_mem_release, 83 it.node->name); 84 85 if (!mem) { 86 of_node_put(it.node); 87 return -ENOMEM; 88 } 89 90 rproc_add_carveout(rproc, mem); 91 } 92 93 return 0; 94 } 95 96 static int rcar_rproc_parse_fw(struct rproc *rproc, const struct firmware *fw) 97 { 98 int ret; 99 100 ret = rproc_elf_load_rsc_table(rproc, fw); 101 if (ret) 102 dev_info(&rproc->dev, "No resource table in elf\n"); 103 104 return 0; 105 } 106 107 static int rcar_rproc_start(struct rproc *rproc) 108 { 109 struct rcar_rproc *priv = rproc->priv; 110 int err; 111 112 if (!rproc->bootaddr) 113 return -EINVAL; 114 115 err = rcar_rst_set_rproc_boot_addr(rproc->bootaddr); 116 if (err) { 117 dev_err(&rproc->dev, "failed to set rproc boot addr\n"); 118 return err; 119 } 120 121 err = reset_control_deassert(priv->rst); 122 if (err) 123 dev_err(&rproc->dev, "failed to deassert reset\n"); 124 125 return err; 126 } 127 128 static int rcar_rproc_stop(struct rproc *rproc) 129 { 130 struct rcar_rproc *priv = rproc->priv; 131 int err; 132 133 err = reset_control_assert(priv->rst); 134 if (err) 135 dev_err(&rproc->dev, "failed to assert reset\n"); 136 137 return err; 138 } 139 140 static struct rproc_ops rcar_rproc_ops = { 141 .prepare = rcar_rproc_prepare, 142 .start = rcar_rproc_start, 143 .stop = rcar_rproc_stop, 144 .load = rproc_elf_load_segments, 145 .parse_fw = rcar_rproc_parse_fw, 146 .find_loaded_rsc_table = rproc_elf_find_loaded_rsc_table, 147 .sanity_check = rproc_elf_sanity_check, 148 .get_boot_addr = rproc_elf_get_boot_addr, 149 150 }; 151 152 static int rcar_rproc_probe(struct platform_device *pdev) 153 { 154 struct device *dev = &pdev->dev; 155 struct device_node *np = dev->of_node; 156 struct rcar_rproc *priv; 157 struct rproc *rproc; 158 int ret; 159 160 rproc = devm_rproc_alloc(dev, np->name, &rcar_rproc_ops, 161 NULL, sizeof(*priv)); 162 if (!rproc) 163 return -ENOMEM; 164 165 priv = rproc->priv; 166 167 priv->rst = devm_reset_control_get_exclusive(dev, NULL); 168 if (IS_ERR(priv->rst)) { 169 ret = PTR_ERR(priv->rst); 170 dev_err_probe(dev, ret, "fail to acquire rproc reset\n"); 171 return ret; 172 } 173 174 pm_runtime_enable(dev); 175 ret = pm_runtime_resume_and_get(dev); 176 if (ret) { 177 dev_err(dev, "failed to power up\n"); 178 return ret; 179 } 180 181 dev_set_drvdata(dev, rproc); 182 183 /* Manually start the rproc */ 184 rproc->auto_boot = false; 185 186 ret = devm_rproc_add(dev, rproc); 187 if (ret) { 188 dev_err(dev, "rproc_add failed\n"); 189 goto pm_disable; 190 } 191 192 return 0; 193 194 pm_disable: 195 pm_runtime_disable(dev); 196 197 return ret; 198 } 199 200 static void rcar_rproc_remove(struct platform_device *pdev) 201 { 202 struct device *dev = &pdev->dev; 203 204 pm_runtime_disable(dev); 205 } 206 207 static const struct of_device_id rcar_rproc_of_match[] = { 208 { .compatible = "renesas,rcar-cr7" }, 209 {}, 210 }; 211 212 MODULE_DEVICE_TABLE(of, rcar_rproc_of_match); 213 214 static struct platform_driver rcar_rproc_driver = { 215 .probe = rcar_rproc_probe, 216 .remove_new = rcar_rproc_remove, 217 .driver = { 218 .name = "rcar-rproc", 219 .of_match_table = rcar_rproc_of_match, 220 }, 221 }; 222 223 module_platform_driver(rcar_rproc_driver); 224 225 MODULE_LICENSE("GPL v2"); 226 MODULE_DESCRIPTION("Renesas R-Car Gen3 remote processor control driver"); 227 MODULE_AUTHOR("Julien Massot <julien.massot@iot.bzh>"); 228