Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Oleksij Rempel | a0ff4aa6 | 2017-08-17 09:15:26 +0200 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2017 Pengutronix, Oleksij Rempel <kernel@pengutronix.de> |
Oleksij Rempel | a0ff4aa6 | 2017-08-17 09:15:26 +0200 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <linux/clk.h> |
| 7 | #include <linux/err.h> |
| 8 | #include <linux/interrupt.h> |
| 9 | #include <linux/kernel.h> |
Peng Fan | 2df7062 | 2021-03-06 19:24:25 +0800 | [diff] [blame] | 10 | #include <linux/mailbox_client.h> |
Oleksij Rempel | a0ff4aa6 | 2017-08-17 09:15:26 +0200 | [diff] [blame] | 11 | #include <linux/mfd/syscon.h> |
| 12 | #include <linux/module.h> |
| 13 | #include <linux/of_address.h> |
Peng Fan | b29b424 | 2021-03-06 19:24:22 +0800 | [diff] [blame] | 14 | #include <linux/of_reserved_mem.h> |
Oleksij Rempel | a0ff4aa6 | 2017-08-17 09:15:26 +0200 | [diff] [blame] | 15 | #include <linux/of_device.h> |
| 16 | #include <linux/platform_device.h> |
| 17 | #include <linux/regmap.h> |
| 18 | #include <linux/remoteproc.h> |
Peng Fan | 2df7062 | 2021-03-06 19:24:25 +0800 | [diff] [blame] | 19 | #include <linux/workqueue.h> |
| 20 | |
| 21 | #include "remoteproc_internal.h" |
Oleksij Rempel | a0ff4aa6 | 2017-08-17 09:15:26 +0200 | [diff] [blame] | 22 | |
| 23 | #define IMX7D_SRC_SCR 0x0C |
| 24 | #define IMX7D_ENABLE_M4 BIT(3) |
| 25 | #define IMX7D_SW_M4P_RST BIT(2) |
| 26 | #define IMX7D_SW_M4C_RST BIT(1) |
| 27 | #define IMX7D_SW_M4C_NON_SCLR_RST BIT(0) |
| 28 | |
| 29 | #define IMX7D_M4_RST_MASK (IMX7D_ENABLE_M4 | IMX7D_SW_M4P_RST \ |
| 30 | | IMX7D_SW_M4C_RST \ |
| 31 | | IMX7D_SW_M4C_NON_SCLR_RST) |
| 32 | |
| 33 | #define IMX7D_M4_START (IMX7D_ENABLE_M4 | IMX7D_SW_M4P_RST \ |
| 34 | | IMX7D_SW_M4C_RST) |
| 35 | #define IMX7D_M4_STOP IMX7D_SW_M4C_NON_SCLR_RST |
| 36 | |
| 37 | /* Address: 0x020D8000 */ |
| 38 | #define IMX6SX_SRC_SCR 0x00 |
| 39 | #define IMX6SX_ENABLE_M4 BIT(22) |
| 40 | #define IMX6SX_SW_M4P_RST BIT(12) |
| 41 | #define IMX6SX_SW_M4C_NON_SCLR_RST BIT(4) |
| 42 | #define IMX6SX_SW_M4C_RST BIT(3) |
| 43 | |
| 44 | #define IMX6SX_M4_START (IMX6SX_ENABLE_M4 | IMX6SX_SW_M4P_RST \ |
| 45 | | IMX6SX_SW_M4C_RST) |
| 46 | #define IMX6SX_M4_STOP IMX6SX_SW_M4C_NON_SCLR_RST |
| 47 | #define IMX6SX_M4_RST_MASK (IMX6SX_ENABLE_M4 | IMX6SX_SW_M4P_RST \ |
| 48 | | IMX6SX_SW_M4C_NON_SCLR_RST \ |
| 49 | | IMX6SX_SW_M4C_RST) |
| 50 | |
| 51 | #define IMX7D_RPROC_MEM_MAX 8 |
| 52 | |
| 53 | /** |
| 54 | * struct imx_rproc_mem - slim internal memory structure |
| 55 | * @cpu_addr: MPU virtual address of the memory region |
| 56 | * @sys_addr: Bus address used to access the memory region |
| 57 | * @size: Size of the memory region |
| 58 | */ |
| 59 | struct imx_rproc_mem { |
| 60 | void __iomem *cpu_addr; |
| 61 | phys_addr_t sys_addr; |
| 62 | size_t size; |
| 63 | }; |
| 64 | |
| 65 | /* att flags */ |
| 66 | /* M4 own area. Can be mapped at probe */ |
| 67 | #define ATT_OWN BIT(1) |
| 68 | |
| 69 | /* address translation table */ |
| 70 | struct imx_rproc_att { |
| 71 | u32 da; /* device address (From Cortex M4 view)*/ |
| 72 | u32 sa; /* system bus address */ |
| 73 | u32 size; /* size of reg range */ |
| 74 | int flags; |
| 75 | }; |
| 76 | |
| 77 | struct imx_rproc_dcfg { |
| 78 | u32 src_reg; |
| 79 | u32 src_mask; |
| 80 | u32 src_start; |
| 81 | u32 src_stop; |
| 82 | const struct imx_rproc_att *att; |
| 83 | size_t att_size; |
| 84 | }; |
| 85 | |
| 86 | struct imx_rproc { |
| 87 | struct device *dev; |
| 88 | struct regmap *regmap; |
| 89 | struct rproc *rproc; |
| 90 | const struct imx_rproc_dcfg *dcfg; |
| 91 | struct imx_rproc_mem mem[IMX7D_RPROC_MEM_MAX]; |
| 92 | struct clk *clk; |
Peng Fan | 2df7062 | 2021-03-06 19:24:25 +0800 | [diff] [blame] | 93 | struct mbox_client cl; |
| 94 | struct mbox_chan *tx_ch; |
| 95 | struct mbox_chan *rx_ch; |
| 96 | struct work_struct rproc_work; |
| 97 | struct workqueue_struct *workqueue; |
Oleksij Rempel | a0ff4aa6 | 2017-08-17 09:15:26 +0200 | [diff] [blame] | 98 | }; |
| 99 | |
Peng Fan | 4ab8f96 | 2021-03-06 19:24:23 +0800 | [diff] [blame] | 100 | static const struct imx_rproc_att imx_rproc_att_imx8mq[] = { |
| 101 | /* dev addr , sys addr , size , flags */ |
| 102 | /* TCML - alias */ |
| 103 | { 0x00000000, 0x007e0000, 0x00020000, 0 }, |
| 104 | /* OCRAM_S */ |
| 105 | { 0x00180000, 0x00180000, 0x00008000, 0 }, |
| 106 | /* OCRAM */ |
| 107 | { 0x00900000, 0x00900000, 0x00020000, 0 }, |
| 108 | /* OCRAM */ |
| 109 | { 0x00920000, 0x00920000, 0x00020000, 0 }, |
| 110 | /* QSPI Code - alias */ |
| 111 | { 0x08000000, 0x08000000, 0x08000000, 0 }, |
| 112 | /* DDR (Code) - alias */ |
| 113 | { 0x10000000, 0x80000000, 0x0FFE0000, 0 }, |
| 114 | /* TCML */ |
| 115 | { 0x1FFE0000, 0x007E0000, 0x00020000, ATT_OWN }, |
| 116 | /* TCMU */ |
| 117 | { 0x20000000, 0x00800000, 0x00020000, ATT_OWN }, |
| 118 | /* OCRAM_S */ |
| 119 | { 0x20180000, 0x00180000, 0x00008000, ATT_OWN }, |
| 120 | /* OCRAM */ |
| 121 | { 0x20200000, 0x00900000, 0x00020000, ATT_OWN }, |
| 122 | /* OCRAM */ |
| 123 | { 0x20220000, 0x00920000, 0x00020000, ATT_OWN }, |
| 124 | /* DDR (Data) */ |
| 125 | { 0x40000000, 0x40000000, 0x80000000, 0 }, |
| 126 | }; |
| 127 | |
Oleksij Rempel | a0ff4aa6 | 2017-08-17 09:15:26 +0200 | [diff] [blame] | 128 | static const struct imx_rproc_att imx_rproc_att_imx7d[] = { |
| 129 | /* dev addr , sys addr , size , flags */ |
| 130 | /* OCRAM_S (M4 Boot code) - alias */ |
| 131 | { 0x00000000, 0x00180000, 0x00008000, 0 }, |
| 132 | /* OCRAM_S (Code) */ |
| 133 | { 0x00180000, 0x00180000, 0x00008000, ATT_OWN }, |
| 134 | /* OCRAM (Code) - alias */ |
| 135 | { 0x00900000, 0x00900000, 0x00020000, 0 }, |
| 136 | /* OCRAM_EPDC (Code) - alias */ |
| 137 | { 0x00920000, 0x00920000, 0x00020000, 0 }, |
| 138 | /* OCRAM_PXP (Code) - alias */ |
| 139 | { 0x00940000, 0x00940000, 0x00008000, 0 }, |
| 140 | /* TCML (Code) */ |
| 141 | { 0x1FFF8000, 0x007F8000, 0x00008000, ATT_OWN }, |
| 142 | /* DDR (Code) - alias, first part of DDR (Data) */ |
| 143 | { 0x10000000, 0x80000000, 0x0FFF0000, 0 }, |
| 144 | |
| 145 | /* TCMU (Data) */ |
| 146 | { 0x20000000, 0x00800000, 0x00008000, ATT_OWN }, |
| 147 | /* OCRAM (Data) */ |
| 148 | { 0x20200000, 0x00900000, 0x00020000, 0 }, |
| 149 | /* OCRAM_EPDC (Data) */ |
| 150 | { 0x20220000, 0x00920000, 0x00020000, 0 }, |
| 151 | /* OCRAM_PXP (Data) */ |
| 152 | { 0x20240000, 0x00940000, 0x00008000, 0 }, |
| 153 | /* DDR (Data) */ |
| 154 | { 0x80000000, 0x80000000, 0x60000000, 0 }, |
| 155 | }; |
| 156 | |
| 157 | static const struct imx_rproc_att imx_rproc_att_imx6sx[] = { |
| 158 | /* dev addr , sys addr , size , flags */ |
| 159 | /* TCML (M4 Boot Code) - alias */ |
| 160 | { 0x00000000, 0x007F8000, 0x00008000, 0 }, |
| 161 | /* OCRAM_S (Code) */ |
| 162 | { 0x00180000, 0x008F8000, 0x00004000, 0 }, |
| 163 | /* OCRAM_S (Code) - alias */ |
| 164 | { 0x00180000, 0x008FC000, 0x00004000, 0 }, |
| 165 | /* TCML (Code) */ |
| 166 | { 0x1FFF8000, 0x007F8000, 0x00008000, ATT_OWN }, |
| 167 | /* DDR (Code) - alias, first part of DDR (Data) */ |
| 168 | { 0x10000000, 0x80000000, 0x0FFF8000, 0 }, |
| 169 | |
| 170 | /* TCMU (Data) */ |
| 171 | { 0x20000000, 0x00800000, 0x00008000, ATT_OWN }, |
| 172 | /* OCRAM_S (Data) - alias? */ |
| 173 | { 0x208F8000, 0x008F8000, 0x00004000, 0 }, |
| 174 | /* DDR (Data) */ |
| 175 | { 0x80000000, 0x80000000, 0x60000000, 0 }, |
| 176 | }; |
| 177 | |
Peng Fan | 4ab8f96 | 2021-03-06 19:24:23 +0800 | [diff] [blame] | 178 | static const struct imx_rproc_dcfg imx_rproc_cfg_imx8mq = { |
| 179 | .src_reg = IMX7D_SRC_SCR, |
| 180 | .src_mask = IMX7D_M4_RST_MASK, |
| 181 | .src_start = IMX7D_M4_START, |
| 182 | .src_stop = IMX7D_M4_STOP, |
| 183 | .att = imx_rproc_att_imx8mq, |
| 184 | .att_size = ARRAY_SIZE(imx_rproc_att_imx8mq), |
| 185 | }; |
| 186 | |
Oleksij Rempel | a0ff4aa6 | 2017-08-17 09:15:26 +0200 | [diff] [blame] | 187 | static const struct imx_rproc_dcfg imx_rproc_cfg_imx7d = { |
| 188 | .src_reg = IMX7D_SRC_SCR, |
| 189 | .src_mask = IMX7D_M4_RST_MASK, |
| 190 | .src_start = IMX7D_M4_START, |
| 191 | .src_stop = IMX7D_M4_STOP, |
| 192 | .att = imx_rproc_att_imx7d, |
| 193 | .att_size = ARRAY_SIZE(imx_rproc_att_imx7d), |
| 194 | }; |
| 195 | |
| 196 | static const struct imx_rproc_dcfg imx_rproc_cfg_imx6sx = { |
| 197 | .src_reg = IMX6SX_SRC_SCR, |
| 198 | .src_mask = IMX6SX_M4_RST_MASK, |
| 199 | .src_start = IMX6SX_M4_START, |
| 200 | .src_stop = IMX6SX_M4_STOP, |
| 201 | .att = imx_rproc_att_imx6sx, |
| 202 | .att_size = ARRAY_SIZE(imx_rproc_att_imx6sx), |
| 203 | }; |
| 204 | |
| 205 | static int imx_rproc_start(struct rproc *rproc) |
| 206 | { |
| 207 | struct imx_rproc *priv = rproc->priv; |
| 208 | const struct imx_rproc_dcfg *dcfg = priv->dcfg; |
| 209 | struct device *dev = priv->dev; |
| 210 | int ret; |
| 211 | |
| 212 | ret = regmap_update_bits(priv->regmap, dcfg->src_reg, |
| 213 | dcfg->src_mask, dcfg->src_start); |
| 214 | if (ret) |
Fabio Estevam | 16a3c63 | 2019-06-03 20:46:28 -0300 | [diff] [blame] | 215 | dev_err(dev, "Failed to enable M4!\n"); |
Oleksij Rempel | a0ff4aa6 | 2017-08-17 09:15:26 +0200 | [diff] [blame] | 216 | |
| 217 | return ret; |
| 218 | } |
| 219 | |
| 220 | static int imx_rproc_stop(struct rproc *rproc) |
| 221 | { |
| 222 | struct imx_rproc *priv = rproc->priv; |
| 223 | const struct imx_rproc_dcfg *dcfg = priv->dcfg; |
| 224 | struct device *dev = priv->dev; |
| 225 | int ret; |
| 226 | |
| 227 | ret = regmap_update_bits(priv->regmap, dcfg->src_reg, |
| 228 | dcfg->src_mask, dcfg->src_stop); |
| 229 | if (ret) |
Fabio Estevam | 16a3c63 | 2019-06-03 20:46:28 -0300 | [diff] [blame] | 230 | dev_err(dev, "Failed to stop M4!\n"); |
Oleksij Rempel | a0ff4aa6 | 2017-08-17 09:15:26 +0200 | [diff] [blame] | 231 | |
| 232 | return ret; |
| 233 | } |
| 234 | |
| 235 | static int imx_rproc_da_to_sys(struct imx_rproc *priv, u64 da, |
Clement Leger | 9ce3bf2 | 2020-03-02 10:38:55 +0100 | [diff] [blame] | 236 | size_t len, u64 *sys) |
Oleksij Rempel | a0ff4aa6 | 2017-08-17 09:15:26 +0200 | [diff] [blame] | 237 | { |
| 238 | const struct imx_rproc_dcfg *dcfg = priv->dcfg; |
| 239 | int i; |
| 240 | |
| 241 | /* parse address translation table */ |
| 242 | for (i = 0; i < dcfg->att_size; i++) { |
| 243 | const struct imx_rproc_att *att = &dcfg->att[i]; |
| 244 | |
| 245 | if (da >= att->da && da + len < att->da + att->size) { |
| 246 | unsigned int offset = da - att->da; |
| 247 | |
| 248 | *sys = att->sa + offset; |
| 249 | return 0; |
| 250 | } |
| 251 | } |
| 252 | |
Clement Leger | 9ce3bf2 | 2020-03-02 10:38:55 +0100 | [diff] [blame] | 253 | dev_warn(priv->dev, "Translation failed: da = 0x%llx len = 0x%zx\n", |
Oleksij Rempel | a0ff4aa6 | 2017-08-17 09:15:26 +0200 | [diff] [blame] | 254 | da, len); |
| 255 | return -ENOENT; |
| 256 | } |
| 257 | |
Peng Fan | 40df0a9 | 2021-03-06 19:24:19 +0800 | [diff] [blame] | 258 | static void *imx_rproc_da_to_va(struct rproc *rproc, u64 da, size_t len, bool *is_iomem) |
Oleksij Rempel | a0ff4aa6 | 2017-08-17 09:15:26 +0200 | [diff] [blame] | 259 | { |
| 260 | struct imx_rproc *priv = rproc->priv; |
| 261 | void *va = NULL; |
| 262 | u64 sys; |
| 263 | int i; |
| 264 | |
Clement Leger | 9ce3bf2 | 2020-03-02 10:38:55 +0100 | [diff] [blame] | 265 | if (len == 0) |
Oleksij Rempel | a0ff4aa6 | 2017-08-17 09:15:26 +0200 | [diff] [blame] | 266 | return NULL; |
| 267 | |
| 268 | /* |
| 269 | * On device side we have many aliases, so we need to convert device |
| 270 | * address (M4) to system bus address first. |
| 271 | */ |
| 272 | if (imx_rproc_da_to_sys(priv, da, len, &sys)) |
| 273 | return NULL; |
| 274 | |
| 275 | for (i = 0; i < IMX7D_RPROC_MEM_MAX; i++) { |
| 276 | if (sys >= priv->mem[i].sys_addr && sys + len < |
| 277 | priv->mem[i].sys_addr + priv->mem[i].size) { |
| 278 | unsigned int offset = sys - priv->mem[i].sys_addr; |
| 279 | /* __force to make sparse happy with type conversion */ |
| 280 | va = (__force void *)(priv->mem[i].cpu_addr + offset); |
| 281 | break; |
| 282 | } |
| 283 | } |
| 284 | |
Clement Leger | 9ce3bf2 | 2020-03-02 10:38:55 +0100 | [diff] [blame] | 285 | dev_dbg(&rproc->dev, "da = 0x%llx len = 0x%zx va = 0x%p\n", |
| 286 | da, len, va); |
Oleksij Rempel | a0ff4aa6 | 2017-08-17 09:15:26 +0200 | [diff] [blame] | 287 | |
| 288 | return va; |
| 289 | } |
| 290 | |
Peng Fan | b29b424 | 2021-03-06 19:24:22 +0800 | [diff] [blame] | 291 | static int imx_rproc_mem_alloc(struct rproc *rproc, |
| 292 | struct rproc_mem_entry *mem) |
| 293 | { |
| 294 | struct device *dev = rproc->dev.parent; |
| 295 | void *va; |
| 296 | |
| 297 | dev_dbg(dev, "map memory: %p+%zx\n", &mem->dma, mem->len); |
| 298 | va = ioremap_wc(mem->dma, mem->len); |
| 299 | if (IS_ERR_OR_NULL(va)) { |
| 300 | dev_err(dev, "Unable to map memory region: %p+%zx\n", |
| 301 | &mem->dma, mem->len); |
| 302 | return -ENOMEM; |
| 303 | } |
| 304 | |
| 305 | /* Update memory entry va */ |
| 306 | mem->va = va; |
| 307 | |
| 308 | return 0; |
| 309 | } |
| 310 | |
| 311 | static int imx_rproc_mem_release(struct rproc *rproc, |
| 312 | struct rproc_mem_entry *mem) |
| 313 | { |
| 314 | dev_dbg(rproc->dev.parent, "unmap memory: %pa\n", &mem->dma); |
| 315 | iounmap(mem->va); |
| 316 | |
| 317 | return 0; |
| 318 | } |
| 319 | |
| 320 | static int imx_rproc_parse_memory_regions(struct rproc *rproc) |
| 321 | { |
| 322 | struct imx_rproc *priv = rproc->priv; |
| 323 | struct device_node *np = priv->dev->of_node; |
| 324 | struct of_phandle_iterator it; |
| 325 | struct rproc_mem_entry *mem; |
| 326 | struct reserved_mem *rmem; |
| 327 | u32 da; |
| 328 | |
| 329 | /* Register associated reserved memory regions */ |
| 330 | of_phandle_iterator_init(&it, np, "memory-region", NULL, 0); |
| 331 | while (of_phandle_iterator_next(&it) == 0) { |
| 332 | /* |
| 333 | * Ignore the first memory region which will be used vdev buffer. |
| 334 | * No need to do extra handlings, rproc_add_virtio_dev will handle it. |
| 335 | */ |
| 336 | if (!strcmp(it.node->name, "vdev0buffer")) |
| 337 | continue; |
| 338 | |
| 339 | rmem = of_reserved_mem_lookup(it.node); |
| 340 | if (!rmem) { |
| 341 | dev_err(priv->dev, "unable to acquire memory-region\n"); |
| 342 | return -EINVAL; |
| 343 | } |
| 344 | |
| 345 | /* No need to translate pa to da, i.MX use same map */ |
| 346 | da = rmem->base; |
| 347 | |
| 348 | /* Register memory region */ |
| 349 | mem = rproc_mem_entry_init(priv->dev, NULL, (dma_addr_t)rmem->base, rmem->size, da, |
| 350 | imx_rproc_mem_alloc, imx_rproc_mem_release, |
| 351 | it.node->name); |
| 352 | |
| 353 | if (mem) |
| 354 | rproc_coredump_add_segment(rproc, da, rmem->size); |
| 355 | else |
| 356 | return -ENOMEM; |
| 357 | |
| 358 | rproc_add_carveout(rproc, mem); |
| 359 | } |
| 360 | |
| 361 | return 0; |
| 362 | } |
| 363 | |
| 364 | static int imx_rproc_parse_fw(struct rproc *rproc, const struct firmware *fw) |
| 365 | { |
| 366 | int ret = imx_rproc_parse_memory_regions(rproc); |
| 367 | |
| 368 | if (ret) |
| 369 | return ret; |
| 370 | |
| 371 | ret = rproc_elf_load_rsc_table(rproc, fw); |
| 372 | if (ret) |
| 373 | dev_info(&rproc->dev, "No resource table in elf\n"); |
| 374 | |
| 375 | return 0; |
| 376 | } |
| 377 | |
Peng Fan | 2df7062 | 2021-03-06 19:24:25 +0800 | [diff] [blame] | 378 | static void imx_rproc_kick(struct rproc *rproc, int vqid) |
| 379 | { |
| 380 | struct imx_rproc *priv = rproc->priv; |
| 381 | int err; |
| 382 | __u32 mmsg; |
| 383 | |
| 384 | if (!priv->tx_ch) { |
| 385 | dev_err(priv->dev, "No initialized mbox tx channel\n"); |
| 386 | return; |
| 387 | } |
| 388 | |
| 389 | /* |
| 390 | * Send the index of the triggered virtqueue as the mu payload. |
| 391 | * Let remote processor know which virtqueue is used. |
| 392 | */ |
| 393 | mmsg = vqid << 16; |
| 394 | |
| 395 | err = mbox_send_message(priv->tx_ch, (void *)&mmsg); |
| 396 | if (err < 0) |
| 397 | dev_err(priv->dev, "%s: failed (%d, err:%d)\n", |
| 398 | __func__, vqid, err); |
| 399 | } |
| 400 | |
Oleksij Rempel | a0ff4aa6 | 2017-08-17 09:15:26 +0200 | [diff] [blame] | 401 | static const struct rproc_ops imx_rproc_ops = { |
| 402 | .start = imx_rproc_start, |
| 403 | .stop = imx_rproc_stop, |
Peng Fan | 2df7062 | 2021-03-06 19:24:25 +0800 | [diff] [blame] | 404 | .kick = imx_rproc_kick, |
Oleksij Rempel | a0ff4aa6 | 2017-08-17 09:15:26 +0200 | [diff] [blame] | 405 | .da_to_va = imx_rproc_da_to_va, |
Peng Fan | b29b424 | 2021-03-06 19:24:22 +0800 | [diff] [blame] | 406 | .load = rproc_elf_load_segments, |
| 407 | .parse_fw = imx_rproc_parse_fw, |
| 408 | .find_loaded_rsc_table = rproc_elf_find_loaded_rsc_table, |
| 409 | .sanity_check = rproc_elf_sanity_check, |
| 410 | .get_boot_addr = rproc_elf_get_boot_addr, |
Oleksij Rempel | a0ff4aa6 | 2017-08-17 09:15:26 +0200 | [diff] [blame] | 411 | }; |
| 412 | |
| 413 | static int imx_rproc_addr_init(struct imx_rproc *priv, |
| 414 | struct platform_device *pdev) |
| 415 | { |
| 416 | const struct imx_rproc_dcfg *dcfg = priv->dcfg; |
| 417 | struct device *dev = &pdev->dev; |
| 418 | struct device_node *np = dev->of_node; |
| 419 | int a, b = 0, err, nph; |
| 420 | |
| 421 | /* remap required addresses */ |
| 422 | for (a = 0; a < dcfg->att_size; a++) { |
| 423 | const struct imx_rproc_att *att = &dcfg->att[a]; |
| 424 | |
| 425 | if (!(att->flags & ATT_OWN)) |
| 426 | continue; |
| 427 | |
Dan Carpenter | 68c2d645 | 2017-10-05 15:58:27 +0300 | [diff] [blame] | 428 | if (b >= IMX7D_RPROC_MEM_MAX) |
Oleksij Rempel | a0ff4aa6 | 2017-08-17 09:15:26 +0200 | [diff] [blame] | 429 | break; |
| 430 | |
| 431 | priv->mem[b].cpu_addr = devm_ioremap(&pdev->dev, |
| 432 | att->sa, att->size); |
Wei Yongjun | 68a39a3 | 2017-10-11 10:48:44 +0000 | [diff] [blame] | 433 | if (!priv->mem[b].cpu_addr) { |
Peng Fan | 1896b3d | 2021-03-06 19:24:20 +0800 | [diff] [blame] | 434 | dev_err(dev, "failed to remap %#x bytes from %#x\n", att->size, att->sa); |
Wei Yongjun | 68a39a3 | 2017-10-11 10:48:44 +0000 | [diff] [blame] | 435 | return -ENOMEM; |
Oleksij Rempel | a0ff4aa6 | 2017-08-17 09:15:26 +0200 | [diff] [blame] | 436 | } |
| 437 | priv->mem[b].sys_addr = att->sa; |
| 438 | priv->mem[b].size = att->size; |
| 439 | b++; |
| 440 | } |
| 441 | |
| 442 | /* memory-region is optional property */ |
| 443 | nph = of_count_phandle_with_args(np, "memory-region", NULL); |
| 444 | if (nph <= 0) |
| 445 | return 0; |
| 446 | |
| 447 | /* remap optional addresses */ |
| 448 | for (a = 0; a < nph; a++) { |
| 449 | struct device_node *node; |
| 450 | struct resource res; |
| 451 | |
| 452 | node = of_parse_phandle(np, "memory-region", a); |
Peng Fan | 8f2d896 | 2021-03-06 19:24:24 +0800 | [diff] [blame] | 453 | /* Not map vdev region */ |
| 454 | if (!strcmp(node->name, "vdev")) |
| 455 | continue; |
Oleksij Rempel | a0ff4aa6 | 2017-08-17 09:15:26 +0200 | [diff] [blame] | 456 | err = of_address_to_resource(node, 0, &res); |
| 457 | if (err) { |
| 458 | dev_err(dev, "unable to resolve memory region\n"); |
| 459 | return err; |
| 460 | } |
| 461 | |
Peng Fan | 6e962bf | 2021-04-08 09:44:46 +0800 | [diff] [blame^] | 462 | of_node_put(node); |
| 463 | |
Dan Carpenter | 68c2d645 | 2017-10-05 15:58:27 +0300 | [diff] [blame] | 464 | if (b >= IMX7D_RPROC_MEM_MAX) |
Oleksij Rempel | a0ff4aa6 | 2017-08-17 09:15:26 +0200 | [diff] [blame] | 465 | break; |
| 466 | |
Peng Fan | ecadcc4 | 2021-03-06 19:24:21 +0800 | [diff] [blame] | 467 | /* Not use resource version, because we might share region */ |
| 468 | priv->mem[b].cpu_addr = devm_ioremap(&pdev->dev, res.start, resource_size(&res)); |
Wei Yongjun | 18cda80 | 2021-03-12 08:04:20 +0000 | [diff] [blame] | 469 | if (!priv->mem[b].cpu_addr) { |
Peng Fan | 1896b3d | 2021-03-06 19:24:20 +0800 | [diff] [blame] | 470 | dev_err(dev, "failed to remap %pr\n", &res); |
Wei Yongjun | 18cda80 | 2021-03-12 08:04:20 +0000 | [diff] [blame] | 471 | return -ENOMEM; |
Oleksij Rempel | a0ff4aa6 | 2017-08-17 09:15:26 +0200 | [diff] [blame] | 472 | } |
| 473 | priv->mem[b].sys_addr = res.start; |
| 474 | priv->mem[b].size = resource_size(&res); |
| 475 | b++; |
| 476 | } |
| 477 | |
| 478 | return 0; |
| 479 | } |
| 480 | |
Peng Fan | 2df7062 | 2021-03-06 19:24:25 +0800 | [diff] [blame] | 481 | static void imx_rproc_vq_work(struct work_struct *work) |
| 482 | { |
| 483 | struct imx_rproc *priv = container_of(work, struct imx_rproc, |
| 484 | rproc_work); |
| 485 | |
| 486 | rproc_vq_interrupt(priv->rproc, 0); |
| 487 | rproc_vq_interrupt(priv->rproc, 1); |
| 488 | } |
| 489 | |
| 490 | static void imx_rproc_rx_callback(struct mbox_client *cl, void *msg) |
| 491 | { |
| 492 | struct rproc *rproc = dev_get_drvdata(cl->dev); |
| 493 | struct imx_rproc *priv = rproc->priv; |
| 494 | |
| 495 | queue_work(priv->workqueue, &priv->rproc_work); |
| 496 | } |
| 497 | |
| 498 | static int imx_rproc_xtr_mbox_init(struct rproc *rproc) |
| 499 | { |
| 500 | struct imx_rproc *priv = rproc->priv; |
| 501 | struct device *dev = priv->dev; |
| 502 | struct mbox_client *cl; |
| 503 | int ret; |
| 504 | |
| 505 | if (!of_get_property(dev->of_node, "mbox-names", NULL)) |
| 506 | return 0; |
| 507 | |
| 508 | cl = &priv->cl; |
| 509 | cl->dev = dev; |
| 510 | cl->tx_block = true; |
| 511 | cl->tx_tout = 100; |
| 512 | cl->knows_txdone = false; |
| 513 | cl->rx_callback = imx_rproc_rx_callback; |
| 514 | |
| 515 | priv->tx_ch = mbox_request_channel_byname(cl, "tx"); |
| 516 | if (IS_ERR(priv->tx_ch)) { |
| 517 | ret = PTR_ERR(priv->tx_ch); |
| 518 | return dev_err_probe(cl->dev, ret, |
| 519 | "failed to request tx mailbox channel: %d\n", ret); |
| 520 | } |
| 521 | |
| 522 | priv->rx_ch = mbox_request_channel_byname(cl, "rx"); |
| 523 | if (IS_ERR(priv->rx_ch)) { |
| 524 | mbox_free_channel(priv->tx_ch); |
| 525 | ret = PTR_ERR(priv->rx_ch); |
| 526 | return dev_err_probe(cl->dev, ret, |
| 527 | "failed to request rx mailbox channel: %d\n", ret); |
| 528 | } |
| 529 | |
| 530 | return 0; |
| 531 | } |
| 532 | |
| 533 | static void imx_rproc_free_mbox(struct rproc *rproc) |
| 534 | { |
| 535 | struct imx_rproc *priv = rproc->priv; |
| 536 | |
| 537 | mbox_free_channel(priv->tx_ch); |
| 538 | mbox_free_channel(priv->rx_ch); |
| 539 | } |
| 540 | |
Oleksij Rempel | a0ff4aa6 | 2017-08-17 09:15:26 +0200 | [diff] [blame] | 541 | static int imx_rproc_probe(struct platform_device *pdev) |
| 542 | { |
| 543 | struct device *dev = &pdev->dev; |
| 544 | struct device_node *np = dev->of_node; |
| 545 | struct imx_rproc *priv; |
| 546 | struct rproc *rproc; |
| 547 | struct regmap_config config = { .name = "imx-rproc" }; |
| 548 | const struct imx_rproc_dcfg *dcfg; |
| 549 | struct regmap *regmap; |
| 550 | int ret; |
| 551 | |
| 552 | regmap = syscon_regmap_lookup_by_phandle(np, "syscon"); |
| 553 | if (IS_ERR(regmap)) { |
| 554 | dev_err(dev, "failed to find syscon\n"); |
| 555 | return PTR_ERR(regmap); |
| 556 | } |
| 557 | regmap_attach_dev(dev, regmap, &config); |
| 558 | |
| 559 | /* set some other name then imx */ |
| 560 | rproc = rproc_alloc(dev, "imx-rproc", &imx_rproc_ops, |
| 561 | NULL, sizeof(*priv)); |
Christophe JAILLET | 99a31ad | 2018-03-14 20:56:39 +0100 | [diff] [blame] | 562 | if (!rproc) |
| 563 | return -ENOMEM; |
Oleksij Rempel | a0ff4aa6 | 2017-08-17 09:15:26 +0200 | [diff] [blame] | 564 | |
| 565 | dcfg = of_device_get_match_data(dev); |
Christophe JAILLET | de6f83f | 2018-03-14 20:56:37 +0100 | [diff] [blame] | 566 | if (!dcfg) { |
| 567 | ret = -EINVAL; |
| 568 | goto err_put_rproc; |
| 569 | } |
Oleksij Rempel | a0ff4aa6 | 2017-08-17 09:15:26 +0200 | [diff] [blame] | 570 | |
| 571 | priv = rproc->priv; |
| 572 | priv->rproc = rproc; |
| 573 | priv->regmap = regmap; |
| 574 | priv->dcfg = dcfg; |
| 575 | priv->dev = dev; |
| 576 | |
| 577 | dev_set_drvdata(dev, rproc); |
Peng Fan | 2df7062 | 2021-03-06 19:24:25 +0800 | [diff] [blame] | 578 | priv->workqueue = create_workqueue(dev_name(dev)); |
| 579 | if (!priv->workqueue) { |
| 580 | dev_err(dev, "cannot create workqueue\n"); |
| 581 | ret = -ENOMEM; |
| 582 | goto err_put_rproc; |
| 583 | } |
| 584 | |
| 585 | ret = imx_rproc_xtr_mbox_init(rproc); |
| 586 | if (ret) |
| 587 | goto err_put_wkq; |
Oleksij Rempel | a0ff4aa6 | 2017-08-17 09:15:26 +0200 | [diff] [blame] | 588 | |
| 589 | ret = imx_rproc_addr_init(priv, pdev); |
| 590 | if (ret) { |
Fabio Estevam | 16a3c63 | 2019-06-03 20:46:28 -0300 | [diff] [blame] | 591 | dev_err(dev, "failed on imx_rproc_addr_init\n"); |
Peng Fan | 2df7062 | 2021-03-06 19:24:25 +0800 | [diff] [blame] | 592 | goto err_put_mbox; |
Oleksij Rempel | a0ff4aa6 | 2017-08-17 09:15:26 +0200 | [diff] [blame] | 593 | } |
| 594 | |
| 595 | priv->clk = devm_clk_get(dev, NULL); |
| 596 | if (IS_ERR(priv->clk)) { |
| 597 | dev_err(dev, "Failed to get clock\n"); |
Christophe JAILLET | 96a30d7 | 2018-03-14 20:56:38 +0100 | [diff] [blame] | 598 | ret = PTR_ERR(priv->clk); |
Peng Fan | 2df7062 | 2021-03-06 19:24:25 +0800 | [diff] [blame] | 599 | goto err_put_mbox; |
Oleksij Rempel | a0ff4aa6 | 2017-08-17 09:15:26 +0200 | [diff] [blame] | 600 | } |
| 601 | |
| 602 | /* |
| 603 | * clk for M4 block including memory. Should be |
| 604 | * enabled before .start for FW transfer. |
| 605 | */ |
| 606 | ret = clk_prepare_enable(priv->clk); |
| 607 | if (ret) { |
| 608 | dev_err(&rproc->dev, "Failed to enable clock\n"); |
Peng Fan | 2df7062 | 2021-03-06 19:24:25 +0800 | [diff] [blame] | 609 | goto err_put_mbox; |
Oleksij Rempel | a0ff4aa6 | 2017-08-17 09:15:26 +0200 | [diff] [blame] | 610 | } |
| 611 | |
Peng Fan | 2df7062 | 2021-03-06 19:24:25 +0800 | [diff] [blame] | 612 | INIT_WORK(&priv->rproc_work, imx_rproc_vq_work); |
| 613 | |
Oleksij Rempel | a0ff4aa6 | 2017-08-17 09:15:26 +0200 | [diff] [blame] | 614 | ret = rproc_add(rproc); |
| 615 | if (ret) { |
| 616 | dev_err(dev, "rproc_add failed\n"); |
| 617 | goto err_put_clk; |
| 618 | } |
| 619 | |
Christophe JAILLET | 99a31ad | 2018-03-14 20:56:39 +0100 | [diff] [blame] | 620 | return 0; |
Oleksij Rempel | a0ff4aa6 | 2017-08-17 09:15:26 +0200 | [diff] [blame] | 621 | |
| 622 | err_put_clk: |
| 623 | clk_disable_unprepare(priv->clk); |
Peng Fan | 2df7062 | 2021-03-06 19:24:25 +0800 | [diff] [blame] | 624 | err_put_mbox: |
| 625 | imx_rproc_free_mbox(rproc); |
| 626 | err_put_wkq: |
| 627 | destroy_workqueue(priv->workqueue); |
Oleksij Rempel | a0ff4aa6 | 2017-08-17 09:15:26 +0200 | [diff] [blame] | 628 | err_put_rproc: |
| 629 | rproc_free(rproc); |
Christophe JAILLET | 99a31ad | 2018-03-14 20:56:39 +0100 | [diff] [blame] | 630 | |
Oleksij Rempel | a0ff4aa6 | 2017-08-17 09:15:26 +0200 | [diff] [blame] | 631 | return ret; |
| 632 | } |
| 633 | |
| 634 | static int imx_rproc_remove(struct platform_device *pdev) |
| 635 | { |
| 636 | struct rproc *rproc = platform_get_drvdata(pdev); |
| 637 | struct imx_rproc *priv = rproc->priv; |
| 638 | |
| 639 | clk_disable_unprepare(priv->clk); |
| 640 | rproc_del(rproc); |
Peng Fan | 2df7062 | 2021-03-06 19:24:25 +0800 | [diff] [blame] | 641 | imx_rproc_free_mbox(rproc); |
Oleksij Rempel | a0ff4aa6 | 2017-08-17 09:15:26 +0200 | [diff] [blame] | 642 | rproc_free(rproc); |
| 643 | |
| 644 | return 0; |
| 645 | } |
| 646 | |
| 647 | static const struct of_device_id imx_rproc_of_match[] = { |
| 648 | { .compatible = "fsl,imx7d-cm4", .data = &imx_rproc_cfg_imx7d }, |
| 649 | { .compatible = "fsl,imx6sx-cm4", .data = &imx_rproc_cfg_imx6sx }, |
Peng Fan | 4ab8f96 | 2021-03-06 19:24:23 +0800 | [diff] [blame] | 650 | { .compatible = "fsl,imx8mq-cm4", .data = &imx_rproc_cfg_imx8mq }, |
| 651 | { .compatible = "fsl,imx8mm-cm4", .data = &imx_rproc_cfg_imx8mq }, |
Oleksij Rempel | a0ff4aa6 | 2017-08-17 09:15:26 +0200 | [diff] [blame] | 652 | {}, |
| 653 | }; |
| 654 | MODULE_DEVICE_TABLE(of, imx_rproc_of_match); |
| 655 | |
| 656 | static struct platform_driver imx_rproc_driver = { |
| 657 | .probe = imx_rproc_probe, |
| 658 | .remove = imx_rproc_remove, |
| 659 | .driver = { |
| 660 | .name = "imx-rproc", |
| 661 | .of_match_table = imx_rproc_of_match, |
| 662 | }, |
| 663 | }; |
| 664 | |
| 665 | module_platform_driver(imx_rproc_driver); |
| 666 | |
| 667 | MODULE_LICENSE("GPL v2"); |
Peng Fan | 4ab8f96 | 2021-03-06 19:24:23 +0800 | [diff] [blame] | 668 | MODULE_DESCRIPTION("i.MX remote processor control driver"); |
Oleksij Rempel | a0ff4aa6 | 2017-08-17 09:15:26 +0200 | [diff] [blame] | 669 | MODULE_AUTHOR("Oleksij Rempel <o.rempel@pengutronix.de>"); |