Lokesh Vutla | 2c77185 | 2019-09-04 16:01:36 +0530 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Texas Instruments' K3 DSP Remoteproc driver |
| 4 | * |
| 5 | * Copyright (C) 2018-2019 Texas Instruments Incorporated - http://www.ti.com/ |
| 6 | * Lokesh Vutla <lokeshvutla@ti.com> |
| 7 | * |
| 8 | */ |
| 9 | |
| 10 | #include <common.h> |
| 11 | #include <dm.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 12 | #include <malloc.h> |
Lokesh Vutla | 2c77185 | 2019-09-04 16:01:36 +0530 | [diff] [blame] | 13 | #include <remoteproc.h> |
| 14 | #include <errno.h> |
| 15 | #include <clk.h> |
| 16 | #include <reset.h> |
| 17 | #include <asm/io.h> |
| 18 | #include <power-domain.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 19 | #include <dm/device_compat.h> |
Simon Glass | d66c5f7 | 2020-02-03 07:36:15 -0700 | [diff] [blame] | 20 | #include <linux/err.h> |
Lokesh Vutla | 2c77185 | 2019-09-04 16:01:36 +0530 | [diff] [blame] | 21 | #include <linux/soc/ti/ti_sci_protocol.h> |
| 22 | #include "ti_sci_proc.h" |
| 23 | |
| 24 | #define KEYSTONE_RPROC_LOCAL_ADDRESS_MASK (SZ_16M - 1) |
| 25 | |
| 26 | /** |
| 27 | * struct k3_dsp_mem - internal memory structure |
| 28 | * @cpu_addr: MPU virtual address of the memory region |
| 29 | * @bus_addr: Bus address used to access the memory region |
| 30 | * @dev_addr: Device address from remoteproc view |
| 31 | * @size: Size of the memory region |
| 32 | */ |
| 33 | struct k3_dsp_mem { |
| 34 | void __iomem *cpu_addr; |
| 35 | phys_addr_t bus_addr; |
| 36 | phys_addr_t dev_addr; |
| 37 | size_t size; |
| 38 | }; |
| 39 | |
| 40 | /** |
| 41 | * struct k3_dsp_privdata - Structure representing Remote processor data. |
| 42 | * @rproc_rst: rproc reset control data |
| 43 | * @tsp: Pointer to TISCI proc contrl handle |
| 44 | * @mem: Array of available memories |
| 45 | * @num_mem: Number of available memories |
| 46 | */ |
| 47 | struct k3_dsp_privdata { |
| 48 | struct reset_ctl dsp_rst; |
| 49 | struct ti_sci_proc tsp; |
| 50 | struct k3_dsp_mem *mem; |
| 51 | int num_mems; |
| 52 | }; |
| 53 | |
| 54 | /** |
| 55 | * k3_dsp_load() - Load up the Remote processor image |
| 56 | * @dev: rproc device pointer |
| 57 | * @addr: Address at which image is available |
| 58 | * @size: size of the image |
| 59 | * |
| 60 | * Return: 0 if all goes good, else appropriate error message. |
| 61 | */ |
| 62 | static int k3_dsp_load(struct udevice *dev, ulong addr, ulong size) |
| 63 | { |
| 64 | struct k3_dsp_privdata *dsp = dev_get_priv(dev); |
| 65 | u32 boot_vector; |
| 66 | int ret; |
| 67 | |
| 68 | dev_dbg(dev, "%s addr = 0x%lx, size = 0x%lx\n", __func__, addr, size); |
| 69 | ret = ti_sci_proc_request(&dsp->tsp); |
| 70 | if (ret) |
| 71 | return ret; |
| 72 | |
| 73 | ret = rproc_elf_load_image(dev, addr, size); |
| 74 | if (ret < 0) { |
| 75 | dev_err(dev, "Loading elf failed %d\n", ret); |
| 76 | goto proc_release; |
| 77 | } |
| 78 | |
| 79 | boot_vector = rproc_elf_get_boot_addr(dev, addr); |
| 80 | |
| 81 | dev_dbg(dev, "%s: Boot vector = 0x%x\n", __func__, boot_vector); |
| 82 | |
| 83 | ret = ti_sci_proc_set_config(&dsp->tsp, boot_vector, 0, 0); |
| 84 | proc_release: |
| 85 | ti_sci_proc_release(&dsp->tsp); |
| 86 | return ret; |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * k3_dsp_start() - Start the remote processor |
| 91 | * @dev: rproc device pointer |
| 92 | * |
| 93 | * Return: 0 if all went ok, else return appropriate error |
| 94 | */ |
| 95 | static int k3_dsp_start(struct udevice *dev) |
| 96 | { |
| 97 | struct k3_dsp_privdata *dsp = dev_get_priv(dev); |
| 98 | int ret; |
| 99 | |
| 100 | dev_dbg(dev, "%s\n", __func__); |
| 101 | |
| 102 | ret = ti_sci_proc_request(&dsp->tsp); |
| 103 | if (ret) |
| 104 | return ret; |
Suman Anna | 83d8fab | 2020-03-10 16:05:53 -0500 | [diff] [blame^] | 105 | |
Lokesh Vutla | 2c77185 | 2019-09-04 16:01:36 +0530 | [diff] [blame] | 106 | ret = ti_sci_proc_power_domain_on(&dsp->tsp); |
| 107 | if (ret) |
| 108 | goto proc_release; |
| 109 | |
| 110 | ret = reset_deassert(&dsp->dsp_rst); |
Suman Anna | 83d8fab | 2020-03-10 16:05:53 -0500 | [diff] [blame^] | 111 | if (ret) |
| 112 | ti_sci_proc_power_domain_off(&dsp->tsp); |
Lokesh Vutla | 2c77185 | 2019-09-04 16:01:36 +0530 | [diff] [blame] | 113 | |
| 114 | proc_release: |
| 115 | ti_sci_proc_release(&dsp->tsp); |
| 116 | |
| 117 | return ret; |
| 118 | } |
| 119 | |
| 120 | static int k3_dsp_stop(struct udevice *dev) |
| 121 | { |
| 122 | struct k3_dsp_privdata *dsp = dev_get_priv(dev); |
| 123 | |
| 124 | dev_dbg(dev, "%s\n", __func__); |
| 125 | |
| 126 | ti_sci_proc_request(&dsp->tsp); |
| 127 | reset_assert(&dsp->dsp_rst); |
| 128 | ti_sci_proc_power_domain_off(&dsp->tsp); |
| 129 | ti_sci_proc_release(&dsp->tsp); |
| 130 | |
| 131 | return 0; |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * k3_dsp_init() - Initialize the remote processor |
| 136 | * @dev: rproc device pointer |
| 137 | * |
| 138 | * Return: 0 if all went ok, else return appropriate error |
| 139 | */ |
| 140 | static int k3_dsp_init(struct udevice *dev) |
| 141 | { |
| 142 | dev_dbg(dev, "%s\n", __func__); |
| 143 | |
| 144 | return 0; |
| 145 | } |
| 146 | |
| 147 | static int k3_dsp_reset(struct udevice *dev) |
| 148 | { |
| 149 | dev_dbg(dev, "%s\n", __func__); |
| 150 | |
| 151 | return 0; |
| 152 | } |
| 153 | |
| 154 | static void *k3_dsp_da_to_va(struct udevice *dev, ulong da, ulong len) |
| 155 | { |
| 156 | struct k3_dsp_privdata *dsp = dev_get_priv(dev); |
| 157 | phys_addr_t bus_addr, dev_addr; |
| 158 | void __iomem *va = NULL; |
| 159 | size_t size; |
| 160 | u32 offset; |
| 161 | int i; |
| 162 | |
| 163 | dev_dbg(dev, "%s\n", __func__); |
| 164 | |
| 165 | if (len <= 0) |
| 166 | return NULL; |
| 167 | |
| 168 | for (i = 0; i < dsp->num_mems; i++) { |
| 169 | bus_addr = dsp->mem[i].bus_addr; |
| 170 | dev_addr = dsp->mem[i].dev_addr; |
| 171 | size = dsp->mem[i].size; |
| 172 | |
| 173 | if (da >= dev_addr && ((da + len) <= (dev_addr + size))) { |
| 174 | offset = da - dev_addr; |
| 175 | va = dsp->mem[i].cpu_addr + offset; |
| 176 | return (__force void *)va; |
| 177 | } |
| 178 | |
| 179 | if (da >= bus_addr && (da + len) <= (bus_addr + size)) { |
| 180 | offset = da - bus_addr; |
| 181 | va = dsp->mem[i].cpu_addr + offset; |
| 182 | return (__force void *)va; |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | /* Assume it is DDR region and return da */ |
| 187 | return map_physmem(da, len, MAP_NOCACHE); |
| 188 | } |
| 189 | |
| 190 | static const struct dm_rproc_ops k3_dsp_ops = { |
| 191 | .init = k3_dsp_init, |
| 192 | .load = k3_dsp_load, |
| 193 | .start = k3_dsp_start, |
| 194 | .stop = k3_dsp_stop, |
| 195 | .reset = k3_dsp_reset, |
| 196 | .device_to_virt = k3_dsp_da_to_va, |
| 197 | }; |
| 198 | |
| 199 | static int ti_sci_proc_of_to_priv(struct udevice *dev, struct ti_sci_proc *tsp) |
| 200 | { |
| 201 | u32 ids[2]; |
| 202 | int ret; |
| 203 | |
| 204 | dev_dbg(dev, "%s\n", __func__); |
| 205 | |
| 206 | tsp->sci = ti_sci_get_by_phandle(dev, "ti,sci"); |
| 207 | if (IS_ERR(tsp->sci)) { |
| 208 | dev_err(dev, "ti_sci get failed: %ld\n", PTR_ERR(tsp->sci)); |
| 209 | return PTR_ERR(tsp->sci); |
| 210 | } |
| 211 | |
| 212 | ret = dev_read_u32_array(dev, "ti,sci-proc-ids", ids, 2); |
| 213 | if (ret) { |
| 214 | dev_err(dev, "Proc IDs not populated %d\n", ret); |
| 215 | return ret; |
| 216 | } |
| 217 | |
| 218 | tsp->ops = &tsp->sci->ops.proc_ops; |
| 219 | tsp->proc_id = ids[0]; |
| 220 | tsp->host_id = ids[1]; |
| 221 | tsp->dev_id = dev_read_u32_default(dev, "ti,sci-dev-id", |
| 222 | TI_SCI_RESOURCE_NULL); |
| 223 | if (tsp->dev_id == TI_SCI_RESOURCE_NULL) { |
| 224 | dev_err(dev, "Device ID not populated %d\n", ret); |
| 225 | return -ENODEV; |
| 226 | } |
| 227 | |
| 228 | return 0; |
| 229 | } |
| 230 | |
| 231 | static int k3_dsp_of_get_memories(struct udevice *dev) |
| 232 | { |
| 233 | static const char * const mem_names[] = {"l2sram", "l1pram", "l1dram"}; |
| 234 | struct k3_dsp_privdata *dsp = dev_get_priv(dev); |
| 235 | int i; |
| 236 | |
| 237 | dev_dbg(dev, "%s\n", __func__); |
| 238 | |
| 239 | dsp->num_mems = ARRAY_SIZE(mem_names); |
| 240 | dsp->mem = calloc(dsp->num_mems, sizeof(*dsp->mem)); |
| 241 | if (!dsp->mem) |
| 242 | return -ENOMEM; |
| 243 | |
| 244 | for (i = 0; i < dsp->num_mems; i++) { |
| 245 | /* C71 cores only have a L1P Cache, there are no L1P SRAMs */ |
| 246 | if (device_is_compatible(dev, "ti,j721e-c71-dsp") && |
| 247 | !strcmp(mem_names[i], "l1pram")) { |
| 248 | dsp->mem[i].bus_addr = FDT_ADDR_T_NONE; |
| 249 | dsp->mem[i].dev_addr = FDT_ADDR_T_NONE; |
| 250 | dsp->mem[i].cpu_addr = NULL; |
| 251 | dsp->mem[i].size = 0; |
| 252 | continue; |
| 253 | } |
| 254 | |
| 255 | dsp->mem[i].bus_addr = dev_read_addr_size_name(dev, mem_names[i], |
| 256 | (fdt_addr_t *)&dsp->mem[i].size); |
| 257 | if (dsp->mem[i].bus_addr == FDT_ADDR_T_NONE) { |
| 258 | dev_err(dev, "%s bus address not found\n", mem_names[i]); |
| 259 | return -EINVAL; |
| 260 | } |
| 261 | dsp->mem[i].cpu_addr = map_physmem(dsp->mem[i].bus_addr, |
| 262 | dsp->mem[i].size, |
| 263 | MAP_NOCACHE); |
| 264 | dsp->mem[i].dev_addr = dsp->mem[i].bus_addr & |
| 265 | KEYSTONE_RPROC_LOCAL_ADDRESS_MASK; |
| 266 | |
| 267 | dev_dbg(dev, "memory %8s: bus addr %pa size 0x%zx va %p da %pa\n", |
| 268 | mem_names[i], &dsp->mem[i].bus_addr, |
| 269 | dsp->mem[i].size, dsp->mem[i].cpu_addr, |
| 270 | &dsp->mem[i].dev_addr); |
| 271 | } |
| 272 | |
| 273 | return 0; |
| 274 | } |
| 275 | |
| 276 | /** |
| 277 | * k3_of_to_priv() - generate private data from device tree |
| 278 | * @dev: corresponding k3 dsp processor device |
| 279 | * @dsp: pointer to driver specific private data |
| 280 | * |
| 281 | * Return: 0 if all goes good, else appropriate error message. |
| 282 | */ |
| 283 | static int k3_dsp_of_to_priv(struct udevice *dev, struct k3_dsp_privdata *dsp) |
| 284 | { |
| 285 | int ret; |
| 286 | |
| 287 | dev_dbg(dev, "%s\n", __func__); |
| 288 | |
| 289 | ret = reset_get_by_index(dev, 0, &dsp->dsp_rst); |
| 290 | if (ret) { |
| 291 | dev_err(dev, "reset_get() failed: %d\n", ret); |
| 292 | return ret; |
| 293 | } |
| 294 | |
| 295 | ret = ti_sci_proc_of_to_priv(dev, &dsp->tsp); |
| 296 | if (ret) |
| 297 | return ret; |
| 298 | |
| 299 | ret = k3_dsp_of_get_memories(dev); |
| 300 | if (ret) |
| 301 | return ret; |
| 302 | |
| 303 | return 0; |
| 304 | } |
| 305 | |
| 306 | /** |
| 307 | * k3_dsp_probe() - Basic probe |
| 308 | * @dev: corresponding k3 remote processor device |
| 309 | * |
| 310 | * Return: 0 if all goes good, else appropriate error message. |
| 311 | */ |
| 312 | static int k3_dsp_probe(struct udevice *dev) |
| 313 | { |
| 314 | struct k3_dsp_privdata *dsp; |
| 315 | int ret; |
| 316 | |
| 317 | dev_dbg(dev, "%s\n", __func__); |
| 318 | |
| 319 | dsp = dev_get_priv(dev); |
| 320 | |
| 321 | ret = k3_dsp_of_to_priv(dev, dsp); |
| 322 | if (ret) { |
| 323 | dev_dbg(dev, "%s: Probe failed with error %d\n", __func__, ret); |
| 324 | return ret; |
| 325 | } |
| 326 | |
| 327 | dev_dbg(dev, "Remoteproc successfully probed\n"); |
| 328 | |
| 329 | return 0; |
| 330 | } |
| 331 | |
| 332 | static int k3_dsp_remove(struct udevice *dev) |
| 333 | { |
| 334 | struct k3_dsp_privdata *dsp = dev_get_priv(dev); |
| 335 | |
| 336 | free(dsp->mem); |
| 337 | |
| 338 | return 0; |
| 339 | } |
| 340 | |
| 341 | static const struct udevice_id k3_dsp_ids[] = { |
| 342 | { .compatible = "ti,j721e-c66-dsp"}, |
| 343 | { .compatible = "ti,j721e-c71-dsp"}, |
| 344 | {} |
| 345 | }; |
| 346 | |
| 347 | U_BOOT_DRIVER(k3_dsp) = { |
| 348 | .name = "k3_dsp", |
| 349 | .of_match = k3_dsp_ids, |
| 350 | .id = UCLASS_REMOTEPROC, |
| 351 | .ops = &k3_dsp_ops, |
| 352 | .probe = k3_dsp_probe, |
| 353 | .remove = k3_dsp_remove, |
| 354 | .priv_auto_alloc_size = sizeof(struct k3_dsp_privdata), |
| 355 | }; |