11802d0beSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2e88bb8f7SSuman Anna /*
3e88bb8f7SSuman Anna  * TI Keystone DSP remoteproc driver
4e88bb8f7SSuman Anna  *
5e88bb8f7SSuman Anna  * Copyright (C) 2015-2017 Texas Instruments Incorporated - http://www.ti.com/
6e88bb8f7SSuman Anna  */
7e88bb8f7SSuman Anna 
8e88bb8f7SSuman Anna #include <linux/module.h>
9e88bb8f7SSuman Anna #include <linux/slab.h>
10e88bb8f7SSuman Anna #include <linux/io.h>
11e88bb8f7SSuman Anna #include <linux/interrupt.h>
12e88bb8f7SSuman Anna #include <linux/platform_device.h>
13e88bb8f7SSuman Anna #include <linux/pm_runtime.h>
14e88bb8f7SSuman Anna #include <linux/workqueue.h>
15e88bb8f7SSuman Anna #include <linux/of_address.h>
16e88bb8f7SSuman Anna #include <linux/of_reserved_mem.h>
17e88bb8f7SSuman Anna #include <linux/of_gpio.h>
18e88bb8f7SSuman Anna #include <linux/regmap.h>
19e88bb8f7SSuman Anna #include <linux/mfd/syscon.h>
20e88bb8f7SSuman Anna #include <linux/remoteproc.h>
21e88bb8f7SSuman Anna #include <linux/reset.h>
22e88bb8f7SSuman Anna 
23e88bb8f7SSuman Anna #include "remoteproc_internal.h"
24e88bb8f7SSuman Anna 
25e88bb8f7SSuman Anna #define KEYSTONE_RPROC_LOCAL_ADDRESS_MASK	(SZ_16M - 1)
26e88bb8f7SSuman Anna 
27e88bb8f7SSuman Anna /**
28e88bb8f7SSuman Anna  * struct keystone_rproc_mem - internal memory structure
29e88bb8f7SSuman Anna  * @cpu_addr: MPU virtual address of the memory region
30e88bb8f7SSuman Anna  * @bus_addr: Bus address used to access the memory region
31e88bb8f7SSuman Anna  * @dev_addr: Device address of the memory region from DSP view
32e88bb8f7SSuman Anna  * @size: Size of the memory region
33e88bb8f7SSuman Anna  */
34e88bb8f7SSuman Anna struct keystone_rproc_mem {
35e88bb8f7SSuman Anna 	void __iomem *cpu_addr;
36e88bb8f7SSuman Anna 	phys_addr_t bus_addr;
37e88bb8f7SSuman Anna 	u32 dev_addr;
38e88bb8f7SSuman Anna 	size_t size;
39e88bb8f7SSuman Anna };
40e88bb8f7SSuman Anna 
41e88bb8f7SSuman Anna /**
42e88bb8f7SSuman Anna  * struct keystone_rproc - keystone remote processor driver structure
43e88bb8f7SSuman Anna  * @dev: cached device pointer
44e88bb8f7SSuman Anna  * @rproc: remoteproc device handle
45e88bb8f7SSuman Anna  * @mem: internal memory regions data
46e88bb8f7SSuman Anna  * @num_mems: number of internal memory regions
47e88bb8f7SSuman Anna  * @dev_ctrl: device control regmap handle
48e88bb8f7SSuman Anna  * @reset: reset control handle
49e88bb8f7SSuman Anna  * @boot_offset: boot register offset in @dev_ctrl regmap
50e88bb8f7SSuman Anna  * @irq_ring: irq entry for vring
51e88bb8f7SSuman Anna  * @irq_fault: irq entry for exception
52e88bb8f7SSuman Anna  * @kick_gpio: gpio used for virtio kicks
53e88bb8f7SSuman Anna  * @workqueue: workqueue for processing virtio interrupts
54e88bb8f7SSuman Anna  */
55e88bb8f7SSuman Anna struct keystone_rproc {
56e88bb8f7SSuman Anna 	struct device *dev;
57e88bb8f7SSuman Anna 	struct rproc *rproc;
58e88bb8f7SSuman Anna 	struct keystone_rproc_mem *mem;
59e88bb8f7SSuman Anna 	int num_mems;
60e88bb8f7SSuman Anna 	struct regmap *dev_ctrl;
61e88bb8f7SSuman Anna 	struct reset_control *reset;
62e88bb8f7SSuman Anna 	u32 boot_offset;
63e88bb8f7SSuman Anna 	int irq_ring;
64e88bb8f7SSuman Anna 	int irq_fault;
65e88bb8f7SSuman Anna 	int kick_gpio;
66e88bb8f7SSuman Anna 	struct work_struct workqueue;
67e88bb8f7SSuman Anna };
68e88bb8f7SSuman Anna 
69e88bb8f7SSuman Anna /* Put the DSP processor into reset */
70e88bb8f7SSuman Anna static void keystone_rproc_dsp_reset(struct keystone_rproc *ksproc)
71e88bb8f7SSuman Anna {
72e88bb8f7SSuman Anna 	reset_control_assert(ksproc->reset);
73e88bb8f7SSuman Anna }
74e88bb8f7SSuman Anna 
75e88bb8f7SSuman Anna /* Configure the boot address and boot the DSP processor */
76e88bb8f7SSuman Anna static int keystone_rproc_dsp_boot(struct keystone_rproc *ksproc, u32 boot_addr)
77e88bb8f7SSuman Anna {
78e88bb8f7SSuman Anna 	int ret;
79e88bb8f7SSuman Anna 
80e88bb8f7SSuman Anna 	if (boot_addr & (SZ_1K - 1)) {
81e88bb8f7SSuman Anna 		dev_err(ksproc->dev, "invalid boot address 0x%x, must be aligned on a 1KB boundary\n",
82e88bb8f7SSuman Anna 			boot_addr);
83e88bb8f7SSuman Anna 		return -EINVAL;
84e88bb8f7SSuman Anna 	}
85e88bb8f7SSuman Anna 
86e88bb8f7SSuman Anna 	ret = regmap_write(ksproc->dev_ctrl, ksproc->boot_offset, boot_addr);
87e88bb8f7SSuman Anna 	if (ret) {
88e88bb8f7SSuman Anna 		dev_err(ksproc->dev, "regmap_write of boot address failed, status = %d\n",
89e88bb8f7SSuman Anna 			ret);
90e88bb8f7SSuman Anna 		return ret;
91e88bb8f7SSuman Anna 	}
92e88bb8f7SSuman Anna 
93e88bb8f7SSuman Anna 	reset_control_deassert(ksproc->reset);
94e88bb8f7SSuman Anna 
95e88bb8f7SSuman Anna 	return 0;
96e88bb8f7SSuman Anna }
97e88bb8f7SSuman Anna 
98e88bb8f7SSuman Anna /*
99e88bb8f7SSuman Anna  * Process the remoteproc exceptions
100e88bb8f7SSuman Anna  *
101e88bb8f7SSuman Anna  * The exception reporting on Keystone DSP remote processors is very simple
102e88bb8f7SSuman Anna  * compared to the equivalent processors on the OMAP family, it is notified
103e88bb8f7SSuman Anna  * through a software-designed specific interrupt source in the IPC interrupt
104e88bb8f7SSuman Anna  * generation register.
105e88bb8f7SSuman Anna  *
106e88bb8f7SSuman Anna  * This function just invokes the rproc_report_crash to report the exception
107e88bb8f7SSuman Anna  * to the remoteproc driver core, to trigger a recovery.
108e88bb8f7SSuman Anna  */
109e88bb8f7SSuman Anna static irqreturn_t keystone_rproc_exception_interrupt(int irq, void *dev_id)
110e88bb8f7SSuman Anna {
111e88bb8f7SSuman Anna 	struct keystone_rproc *ksproc = dev_id;
112e88bb8f7SSuman Anna 
113e88bb8f7SSuman Anna 	rproc_report_crash(ksproc->rproc, RPROC_FATAL_ERROR);
114e88bb8f7SSuman Anna 
115e88bb8f7SSuman Anna 	return IRQ_HANDLED;
116e88bb8f7SSuman Anna }
117e88bb8f7SSuman Anna 
118e88bb8f7SSuman Anna /*
119e88bb8f7SSuman Anna  * Main virtqueue message workqueue function
120e88bb8f7SSuman Anna  *
121e88bb8f7SSuman Anna  * This function is executed upon scheduling of the keystone remoteproc
122e88bb8f7SSuman Anna  * driver's workqueue. The workqueue is scheduled by the vring ISR handler.
123e88bb8f7SSuman Anna  *
124e88bb8f7SSuman Anna  * There is no payload message indicating the virtqueue index as is the
125e88bb8f7SSuman Anna  * case with mailbox-based implementations on OMAP family. As such, this
126e88bb8f7SSuman Anna  * handler processes both the Tx and Rx virtqueue indices on every invocation.
127e88bb8f7SSuman Anna  * The rproc_vq_interrupt function can detect if there are new unprocessed
128e88bb8f7SSuman Anna  * messages or not (returns IRQ_NONE vs IRQ_HANDLED), but there is no need
129e88bb8f7SSuman Anna  * to check for these return values. The index 0 triggering will process all
130e88bb8f7SSuman Anna  * pending Rx buffers, and the index 1 triggering will process all newly
131e88bb8f7SSuman Anna  * available Tx buffers and will wakeup any potentially blocked senders.
132e88bb8f7SSuman Anna  *
133e88bb8f7SSuman Anna  * NOTE:
134e88bb8f7SSuman Anna  * 1. A payload could be added by using some of the source bits in the
135e88bb8f7SSuman Anna  *    IPC interrupt generation registers, but this would need additional
136e88bb8f7SSuman Anna  *    changes to the overall IPC stack, and currently there are no benefits
137e88bb8f7SSuman Anna  *    of adapting that approach.
138e88bb8f7SSuman Anna  * 2. The current logic is based on an inherent design assumption of supporting
139e88bb8f7SSuman Anna  *    only 2 vrings, but this can be changed if needed.
140e88bb8f7SSuman Anna  */
141e88bb8f7SSuman Anna static void handle_event(struct work_struct *work)
142e88bb8f7SSuman Anna {
143e88bb8f7SSuman Anna 	struct keystone_rproc *ksproc =
144e88bb8f7SSuman Anna 		container_of(work, struct keystone_rproc, workqueue);
145e88bb8f7SSuman Anna 
146e88bb8f7SSuman Anna 	rproc_vq_interrupt(ksproc->rproc, 0);
147e88bb8f7SSuman Anna 	rproc_vq_interrupt(ksproc->rproc, 1);
148e88bb8f7SSuman Anna }
149e88bb8f7SSuman Anna 
150e88bb8f7SSuman Anna /*
151e88bb8f7SSuman Anna  * Interrupt handler for processing vring kicks from remote processor
152e88bb8f7SSuman Anna  */
153e88bb8f7SSuman Anna static irqreturn_t keystone_rproc_vring_interrupt(int irq, void *dev_id)
154e88bb8f7SSuman Anna {
155e88bb8f7SSuman Anna 	struct keystone_rproc *ksproc = dev_id;
156e88bb8f7SSuman Anna 
157e88bb8f7SSuman Anna 	schedule_work(&ksproc->workqueue);
158e88bb8f7SSuman Anna 
159e88bb8f7SSuman Anna 	return IRQ_HANDLED;
160e88bb8f7SSuman Anna }
161e88bb8f7SSuman Anna 
162e88bb8f7SSuman Anna /*
163e88bb8f7SSuman Anna  * Power up the DSP remote processor.
164e88bb8f7SSuman Anna  *
165e88bb8f7SSuman Anna  * This function will be invoked only after the firmware for this rproc
166e88bb8f7SSuman Anna  * was loaded, parsed successfully, and all of its resource requirements
167e88bb8f7SSuman Anna  * were met.
168e88bb8f7SSuman Anna  */
169e88bb8f7SSuman Anna static int keystone_rproc_start(struct rproc *rproc)
170e88bb8f7SSuman Anna {
171e88bb8f7SSuman Anna 	struct keystone_rproc *ksproc = rproc->priv;
172e88bb8f7SSuman Anna 	int ret;
173e88bb8f7SSuman Anna 
174e88bb8f7SSuman Anna 	INIT_WORK(&ksproc->workqueue, handle_event);
175e88bb8f7SSuman Anna 
176e88bb8f7SSuman Anna 	ret = request_irq(ksproc->irq_ring, keystone_rproc_vring_interrupt, 0,
177e88bb8f7SSuman Anna 			  dev_name(ksproc->dev), ksproc);
178e88bb8f7SSuman Anna 	if (ret) {
179e88bb8f7SSuman Anna 		dev_err(ksproc->dev, "failed to enable vring interrupt, ret = %d\n",
180e88bb8f7SSuman Anna 			ret);
181e88bb8f7SSuman Anna 		goto out;
182e88bb8f7SSuman Anna 	}
183e88bb8f7SSuman Anna 
184e88bb8f7SSuman Anna 	ret = request_irq(ksproc->irq_fault, keystone_rproc_exception_interrupt,
185e88bb8f7SSuman Anna 			  0, dev_name(ksproc->dev), ksproc);
186e88bb8f7SSuman Anna 	if (ret) {
187e88bb8f7SSuman Anna 		dev_err(ksproc->dev, "failed to enable exception interrupt, ret = %d\n",
188e88bb8f7SSuman Anna 			ret);
189e88bb8f7SSuman Anna 		goto free_vring_irq;
190e88bb8f7SSuman Anna 	}
191e88bb8f7SSuman Anna 
192e88bb8f7SSuman Anna 	ret = keystone_rproc_dsp_boot(ksproc, rproc->bootaddr);
193e88bb8f7SSuman Anna 	if (ret)
194e88bb8f7SSuman Anna 		goto free_exc_irq;
195e88bb8f7SSuman Anna 
196e88bb8f7SSuman Anna 	return 0;
197e88bb8f7SSuman Anna 
198e88bb8f7SSuman Anna free_exc_irq:
199e88bb8f7SSuman Anna 	free_irq(ksproc->irq_fault, ksproc);
200e88bb8f7SSuman Anna free_vring_irq:
201e88bb8f7SSuman Anna 	free_irq(ksproc->irq_ring, ksproc);
202e88bb8f7SSuman Anna 	flush_work(&ksproc->workqueue);
203e88bb8f7SSuman Anna out:
204e88bb8f7SSuman Anna 	return ret;
205e88bb8f7SSuman Anna }
206e88bb8f7SSuman Anna 
207e88bb8f7SSuman Anna /*
208e88bb8f7SSuman Anna  * Stop the DSP remote processor.
209e88bb8f7SSuman Anna  *
210e88bb8f7SSuman Anna  * This function puts the DSP processor into reset, and finishes processing
211e88bb8f7SSuman Anna  * of any pending messages.
212e88bb8f7SSuman Anna  */
213e88bb8f7SSuman Anna static int keystone_rproc_stop(struct rproc *rproc)
214e88bb8f7SSuman Anna {
215e88bb8f7SSuman Anna 	struct keystone_rproc *ksproc = rproc->priv;
216e88bb8f7SSuman Anna 
217e88bb8f7SSuman Anna 	keystone_rproc_dsp_reset(ksproc);
218e88bb8f7SSuman Anna 	free_irq(ksproc->irq_fault, ksproc);
219e88bb8f7SSuman Anna 	free_irq(ksproc->irq_ring, ksproc);
220e88bb8f7SSuman Anna 	flush_work(&ksproc->workqueue);
221e88bb8f7SSuman Anna 
222e88bb8f7SSuman Anna 	return 0;
223e88bb8f7SSuman Anna }
224e88bb8f7SSuman Anna 
225e88bb8f7SSuman Anna /*
226e88bb8f7SSuman Anna  * Kick the remote processor to notify about pending unprocessed messages.
227e88bb8f7SSuman Anna  * The vqid usage is not used and is inconsequential, as the kick is performed
228e88bb8f7SSuman Anna  * through a simulated GPIO (a bit in an IPC interrupt-triggering register),
229e88bb8f7SSuman Anna  * the remote processor is expected to process both its Tx and Rx virtqueues.
230e88bb8f7SSuman Anna  */
231e88bb8f7SSuman Anna static void keystone_rproc_kick(struct rproc *rproc, int vqid)
232e88bb8f7SSuman Anna {
233e88bb8f7SSuman Anna 	struct keystone_rproc *ksproc = rproc->priv;
234e88bb8f7SSuman Anna 
235e88bb8f7SSuman Anna 	if (WARN_ON(ksproc->kick_gpio < 0))
236e88bb8f7SSuman Anna 		return;
237e88bb8f7SSuman Anna 
238e88bb8f7SSuman Anna 	gpio_set_value(ksproc->kick_gpio, 1);
239e88bb8f7SSuman Anna }
240e88bb8f7SSuman Anna 
241e88bb8f7SSuman Anna /*
242e88bb8f7SSuman Anna  * Custom function to translate a DSP device address (internal RAMs only) to a
243e88bb8f7SSuman Anna  * kernel virtual address.  The DSPs can access their RAMs at either an internal
244e88bb8f7SSuman Anna  * address visible only from a DSP, or at the SoC-level bus address. Both these
245e88bb8f7SSuman Anna  * addresses need to be looked through for translation. The translated addresses
246e88bb8f7SSuman Anna  * can be used either by the remoteproc core for loading (when using kernel
247e88bb8f7SSuman Anna  * remoteproc loader), or by any rpmsg bus drivers.
248e88bb8f7SSuman Anna  */
249*9ce3bf22SClement Leger static void *keystone_rproc_da_to_va(struct rproc *rproc, u64 da, size_t len)
250e88bb8f7SSuman Anna {
251e88bb8f7SSuman Anna 	struct keystone_rproc *ksproc = rproc->priv;
252e88bb8f7SSuman Anna 	void __iomem *va = NULL;
253e88bb8f7SSuman Anna 	phys_addr_t bus_addr;
254e88bb8f7SSuman Anna 	u32 dev_addr, offset;
255e88bb8f7SSuman Anna 	size_t size;
256e88bb8f7SSuman Anna 	int i;
257e88bb8f7SSuman Anna 
258*9ce3bf22SClement Leger 	if (len == 0)
259e88bb8f7SSuman Anna 		return NULL;
260e88bb8f7SSuman Anna 
261e88bb8f7SSuman Anna 	for (i = 0; i < ksproc->num_mems; i++) {
262e88bb8f7SSuman Anna 		bus_addr = ksproc->mem[i].bus_addr;
263e88bb8f7SSuman Anna 		dev_addr = ksproc->mem[i].dev_addr;
264e88bb8f7SSuman Anna 		size = ksproc->mem[i].size;
265e88bb8f7SSuman Anna 
266e88bb8f7SSuman Anna 		if (da < KEYSTONE_RPROC_LOCAL_ADDRESS_MASK) {
267e88bb8f7SSuman Anna 			/* handle DSP-view addresses */
268e88bb8f7SSuman Anna 			if ((da >= dev_addr) &&
269e88bb8f7SSuman Anna 			    ((da + len) <= (dev_addr + size))) {
270e88bb8f7SSuman Anna 				offset = da - dev_addr;
271e88bb8f7SSuman Anna 				va = ksproc->mem[i].cpu_addr + offset;
272e88bb8f7SSuman Anna 				break;
273e88bb8f7SSuman Anna 			}
274e88bb8f7SSuman Anna 		} else {
275e88bb8f7SSuman Anna 			/* handle SoC-view addresses */
276e88bb8f7SSuman Anna 			if ((da >= bus_addr) &&
277e88bb8f7SSuman Anna 			    (da + len) <= (bus_addr + size)) {
278e88bb8f7SSuman Anna 				offset = da - bus_addr;
279e88bb8f7SSuman Anna 				va = ksproc->mem[i].cpu_addr + offset;
280e88bb8f7SSuman Anna 				break;
281e88bb8f7SSuman Anna 			}
282e88bb8f7SSuman Anna 		}
283e88bb8f7SSuman Anna 	}
284e88bb8f7SSuman Anna 
285e88bb8f7SSuman Anna 	return (__force void *)va;
286e88bb8f7SSuman Anna }
287e88bb8f7SSuman Anna 
288e88bb8f7SSuman Anna static const struct rproc_ops keystone_rproc_ops = {
289e88bb8f7SSuman Anna 	.start		= keystone_rproc_start,
290e88bb8f7SSuman Anna 	.stop		= keystone_rproc_stop,
291e88bb8f7SSuman Anna 	.kick		= keystone_rproc_kick,
292e88bb8f7SSuman Anna 	.da_to_va	= keystone_rproc_da_to_va,
293e88bb8f7SSuman Anna };
294e88bb8f7SSuman Anna 
295e88bb8f7SSuman Anna static int keystone_rproc_of_get_memories(struct platform_device *pdev,
296e88bb8f7SSuman Anna 					  struct keystone_rproc *ksproc)
297e88bb8f7SSuman Anna {
298e88bb8f7SSuman Anna 	static const char * const mem_names[] = {"l2sram", "l1pram", "l1dram"};
299e88bb8f7SSuman Anna 	struct device *dev = &pdev->dev;
300e88bb8f7SSuman Anna 	struct resource *res;
301e88bb8f7SSuman Anna 	int num_mems = 0;
302e88bb8f7SSuman Anna 	int i;
303e88bb8f7SSuman Anna 
304e88bb8f7SSuman Anna 	num_mems = ARRAY_SIZE(mem_names);
305e88bb8f7SSuman Anna 	ksproc->mem = devm_kcalloc(ksproc->dev, num_mems,
306e88bb8f7SSuman Anna 				   sizeof(*ksproc->mem), GFP_KERNEL);
307e88bb8f7SSuman Anna 	if (!ksproc->mem)
308e88bb8f7SSuman Anna 		return -ENOMEM;
309e88bb8f7SSuman Anna 
310e88bb8f7SSuman Anna 	for (i = 0; i < num_mems; i++) {
311e88bb8f7SSuman Anna 		res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
312e88bb8f7SSuman Anna 						   mem_names[i]);
313e88bb8f7SSuman Anna 		ksproc->mem[i].cpu_addr = devm_ioremap_resource(dev, res);
314e88bb8f7SSuman Anna 		if (IS_ERR(ksproc->mem[i].cpu_addr)) {
315e88bb8f7SSuman Anna 			dev_err(dev, "failed to parse and map %s memory\n",
316e88bb8f7SSuman Anna 				mem_names[i]);
317e88bb8f7SSuman Anna 			return PTR_ERR(ksproc->mem[i].cpu_addr);
318e88bb8f7SSuman Anna 		}
319e88bb8f7SSuman Anna 		ksproc->mem[i].bus_addr = res->start;
320e88bb8f7SSuman Anna 		ksproc->mem[i].dev_addr =
321e88bb8f7SSuman Anna 				res->start & KEYSTONE_RPROC_LOCAL_ADDRESS_MASK;
322e88bb8f7SSuman Anna 		ksproc->mem[i].size = resource_size(res);
323e88bb8f7SSuman Anna 
324e88bb8f7SSuman Anna 		/* zero out memories to start in a pristine state */
325e88bb8f7SSuman Anna 		memset((__force void *)ksproc->mem[i].cpu_addr, 0,
326e88bb8f7SSuman Anna 		       ksproc->mem[i].size);
327e88bb8f7SSuman Anna 	}
328e88bb8f7SSuman Anna 	ksproc->num_mems = num_mems;
329e88bb8f7SSuman Anna 
330e88bb8f7SSuman Anna 	return 0;
331e88bb8f7SSuman Anna }
332e88bb8f7SSuman Anna 
333e88bb8f7SSuman Anna static int keystone_rproc_of_get_dev_syscon(struct platform_device *pdev,
334e88bb8f7SSuman Anna 					    struct keystone_rproc *ksproc)
335e88bb8f7SSuman Anna {
336e88bb8f7SSuman Anna 	struct device_node *np = pdev->dev.of_node;
337e88bb8f7SSuman Anna 	struct device *dev = &pdev->dev;
338e88bb8f7SSuman Anna 	int ret;
339e88bb8f7SSuman Anna 
340e88bb8f7SSuman Anna 	if (!of_property_read_bool(np, "ti,syscon-dev")) {
341e88bb8f7SSuman Anna 		dev_err(dev, "ti,syscon-dev property is absent\n");
342e88bb8f7SSuman Anna 		return -EINVAL;
343e88bb8f7SSuman Anna 	}
344e88bb8f7SSuman Anna 
345e88bb8f7SSuman Anna 	ksproc->dev_ctrl =
346e88bb8f7SSuman Anna 		syscon_regmap_lookup_by_phandle(np, "ti,syscon-dev");
347e88bb8f7SSuman Anna 	if (IS_ERR(ksproc->dev_ctrl)) {
348e88bb8f7SSuman Anna 		ret = PTR_ERR(ksproc->dev_ctrl);
349e88bb8f7SSuman Anna 		return ret;
350e88bb8f7SSuman Anna 	}
351e88bb8f7SSuman Anna 
352e88bb8f7SSuman Anna 	if (of_property_read_u32_index(np, "ti,syscon-dev", 1,
353e88bb8f7SSuman Anna 				       &ksproc->boot_offset)) {
354e88bb8f7SSuman Anna 		dev_err(dev, "couldn't read the boot register offset\n");
355e88bb8f7SSuman Anna 		return -EINVAL;
356e88bb8f7SSuman Anna 	}
357e88bb8f7SSuman Anna 
358e88bb8f7SSuman Anna 	return 0;
359e88bb8f7SSuman Anna }
360e88bb8f7SSuman Anna 
361e88bb8f7SSuman Anna static int keystone_rproc_probe(struct platform_device *pdev)
362e88bb8f7SSuman Anna {
363e88bb8f7SSuman Anna 	struct device *dev = &pdev->dev;
364e88bb8f7SSuman Anna 	struct device_node *np = dev->of_node;
365e88bb8f7SSuman Anna 	struct keystone_rproc *ksproc;
366e88bb8f7SSuman Anna 	struct rproc *rproc;
367e88bb8f7SSuman Anna 	int dsp_id;
368e88bb8f7SSuman Anna 	char *fw_name = NULL;
369e88bb8f7SSuman Anna 	char *template = "keystone-dsp%d-fw";
370e88bb8f7SSuman Anna 	int name_len = 0;
371e88bb8f7SSuman Anna 	int ret = 0;
372e88bb8f7SSuman Anna 
373e88bb8f7SSuman Anna 	if (!np) {
374e88bb8f7SSuman Anna 		dev_err(dev, "only DT-based devices are supported\n");
375e88bb8f7SSuman Anna 		return -ENODEV;
376e88bb8f7SSuman Anna 	}
377e88bb8f7SSuman Anna 
378e88bb8f7SSuman Anna 	dsp_id = of_alias_get_id(np, "rproc");
379e88bb8f7SSuman Anna 	if (dsp_id < 0) {
380e88bb8f7SSuman Anna 		dev_warn(dev, "device does not have an alias id\n");
381e88bb8f7SSuman Anna 		return dsp_id;
382e88bb8f7SSuman Anna 	}
383e88bb8f7SSuman Anna 
384e88bb8f7SSuman Anna 	/* construct a custom default fw name - subject to change in future */
385e88bb8f7SSuman Anna 	name_len = strlen(template); /* assuming a single digit alias */
386e88bb8f7SSuman Anna 	fw_name = devm_kzalloc(dev, name_len, GFP_KERNEL);
387e88bb8f7SSuman Anna 	if (!fw_name)
388e88bb8f7SSuman Anna 		return -ENOMEM;
389e88bb8f7SSuman Anna 	snprintf(fw_name, name_len, template, dsp_id);
390e88bb8f7SSuman Anna 
391e88bb8f7SSuman Anna 	rproc = rproc_alloc(dev, dev_name(dev), &keystone_rproc_ops, fw_name,
392e88bb8f7SSuman Anna 			    sizeof(*ksproc));
393e88bb8f7SSuman Anna 	if (!rproc)
394e88bb8f7SSuman Anna 		return -ENOMEM;
395e88bb8f7SSuman Anna 
396e88bb8f7SSuman Anna 	rproc->has_iommu = false;
397e88bb8f7SSuman Anna 	ksproc = rproc->priv;
398e88bb8f7SSuman Anna 	ksproc->rproc = rproc;
399e88bb8f7SSuman Anna 	ksproc->dev = dev;
400e88bb8f7SSuman Anna 
401e88bb8f7SSuman Anna 	ret = keystone_rproc_of_get_dev_syscon(pdev, ksproc);
402e88bb8f7SSuman Anna 	if (ret)
403e88bb8f7SSuman Anna 		goto free_rproc;
404e88bb8f7SSuman Anna 
405f5f98654SPhilipp Zabel 	ksproc->reset = devm_reset_control_get_exclusive(dev, NULL);
406e88bb8f7SSuman Anna 	if (IS_ERR(ksproc->reset)) {
407e88bb8f7SSuman Anna 		ret = PTR_ERR(ksproc->reset);
408e88bb8f7SSuman Anna 		goto free_rproc;
409e88bb8f7SSuman Anna 	}
410e88bb8f7SSuman Anna 
411e88bb8f7SSuman Anna 	/* enable clock for accessing DSP internal memories */
412e88bb8f7SSuman Anna 	pm_runtime_enable(dev);
413e88bb8f7SSuman Anna 	ret = pm_runtime_get_sync(dev);
414e88bb8f7SSuman Anna 	if (ret < 0) {
415e88bb8f7SSuman Anna 		dev_err(dev, "failed to enable clock, status = %d\n", ret);
416e88bb8f7SSuman Anna 		pm_runtime_put_noidle(dev);
417e88bb8f7SSuman Anna 		goto disable_rpm;
418e88bb8f7SSuman Anna 	}
419e88bb8f7SSuman Anna 
420e88bb8f7SSuman Anna 	ret = keystone_rproc_of_get_memories(pdev, ksproc);
421e88bb8f7SSuman Anna 	if (ret)
422e88bb8f7SSuman Anna 		goto disable_clk;
423e88bb8f7SSuman Anna 
424e88bb8f7SSuman Anna 	ksproc->irq_ring = platform_get_irq_byname(pdev, "vring");
425e88bb8f7SSuman Anna 	if (ksproc->irq_ring < 0) {
426e88bb8f7SSuman Anna 		ret = ksproc->irq_ring;
427e88bb8f7SSuman Anna 		goto disable_clk;
428e88bb8f7SSuman Anna 	}
429e88bb8f7SSuman Anna 
430e88bb8f7SSuman Anna 	ksproc->irq_fault = platform_get_irq_byname(pdev, "exception");
431e88bb8f7SSuman Anna 	if (ksproc->irq_fault < 0) {
432e88bb8f7SSuman Anna 		ret = ksproc->irq_fault;
433e88bb8f7SSuman Anna 		goto disable_clk;
434e88bb8f7SSuman Anna 	}
435e88bb8f7SSuman Anna 
436e88bb8f7SSuman Anna 	ksproc->kick_gpio = of_get_named_gpio_flags(np, "kick-gpios", 0, NULL);
437e88bb8f7SSuman Anna 	if (ksproc->kick_gpio < 0) {
438e88bb8f7SSuman Anna 		ret = ksproc->kick_gpio;
439e88bb8f7SSuman Anna 		dev_err(dev, "failed to get gpio for virtio kicks, status = %d\n",
440e88bb8f7SSuman Anna 			ret);
441e88bb8f7SSuman Anna 		goto disable_clk;
442e88bb8f7SSuman Anna 	}
443e88bb8f7SSuman Anna 
444e88bb8f7SSuman Anna 	if (of_reserved_mem_device_init(dev))
445e88bb8f7SSuman Anna 		dev_warn(dev, "device does not have specific CMA pool\n");
446e88bb8f7SSuman Anna 
44711d2a2ffSAndrew F. Davis 	/* ensure the DSP is in reset before loading firmware */
44811d2a2ffSAndrew F. Davis 	ret = reset_control_status(ksproc->reset);
44911d2a2ffSAndrew F. Davis 	if (ret < 0) {
45011d2a2ffSAndrew F. Davis 		dev_err(dev, "failed to get reset status, status = %d\n", ret);
45111d2a2ffSAndrew F. Davis 		goto release_mem;
45211d2a2ffSAndrew F. Davis 	} else if (ret == 0) {
45311d2a2ffSAndrew F. Davis 		WARN(1, "device is not in reset\n");
45411d2a2ffSAndrew F. Davis 		keystone_rproc_dsp_reset(ksproc);
45511d2a2ffSAndrew F. Davis 	}
45611d2a2ffSAndrew F. Davis 
457e88bb8f7SSuman Anna 	ret = rproc_add(rproc);
458e88bb8f7SSuman Anna 	if (ret) {
459e88bb8f7SSuman Anna 		dev_err(dev, "failed to add register device with remoteproc core, status = %d\n",
460e88bb8f7SSuman Anna 			ret);
461e88bb8f7SSuman Anna 		goto release_mem;
462e88bb8f7SSuman Anna 	}
463e88bb8f7SSuman Anna 
464e88bb8f7SSuman Anna 	platform_set_drvdata(pdev, ksproc);
465e88bb8f7SSuman Anna 
466e88bb8f7SSuman Anna 	return 0;
467e88bb8f7SSuman Anna 
468e88bb8f7SSuman Anna release_mem:
469e88bb8f7SSuman Anna 	of_reserved_mem_device_release(dev);
470e88bb8f7SSuman Anna disable_clk:
471e88bb8f7SSuman Anna 	pm_runtime_put_sync(dev);
472e88bb8f7SSuman Anna disable_rpm:
473e88bb8f7SSuman Anna 	pm_runtime_disable(dev);
474e88bb8f7SSuman Anna free_rproc:
475e88bb8f7SSuman Anna 	rproc_free(rproc);
476e88bb8f7SSuman Anna 	return ret;
477e88bb8f7SSuman Anna }
478e88bb8f7SSuman Anna 
479e88bb8f7SSuman Anna static int keystone_rproc_remove(struct platform_device *pdev)
480e88bb8f7SSuman Anna {
481e88bb8f7SSuman Anna 	struct keystone_rproc *ksproc = platform_get_drvdata(pdev);
482e88bb8f7SSuman Anna 
483e88bb8f7SSuman Anna 	rproc_del(ksproc->rproc);
484e88bb8f7SSuman Anna 	pm_runtime_put_sync(&pdev->dev);
485e88bb8f7SSuman Anna 	pm_runtime_disable(&pdev->dev);
486e88bb8f7SSuman Anna 	rproc_free(ksproc->rproc);
487e88bb8f7SSuman Anna 	of_reserved_mem_device_release(&pdev->dev);
488e88bb8f7SSuman Anna 
489e88bb8f7SSuman Anna 	return 0;
490e88bb8f7SSuman Anna }
491e88bb8f7SSuman Anna 
492e88bb8f7SSuman Anna static const struct of_device_id keystone_rproc_of_match[] = {
493e88bb8f7SSuman Anna 	{ .compatible = "ti,k2hk-dsp", },
494e88bb8f7SSuman Anna 	{ .compatible = "ti,k2l-dsp", },
495e88bb8f7SSuman Anna 	{ .compatible = "ti,k2e-dsp", },
496b4daf890SSuman Anna 	{ .compatible = "ti,k2g-dsp", },
497e88bb8f7SSuman Anna 	{ /* sentinel */ },
498e88bb8f7SSuman Anna };
499e88bb8f7SSuman Anna MODULE_DEVICE_TABLE(of, keystone_rproc_of_match);
500e88bb8f7SSuman Anna 
501e88bb8f7SSuman Anna static struct platform_driver keystone_rproc_driver = {
502e88bb8f7SSuman Anna 	.probe	= keystone_rproc_probe,
503e88bb8f7SSuman Anna 	.remove	= keystone_rproc_remove,
504e88bb8f7SSuman Anna 	.driver	= {
505e88bb8f7SSuman Anna 		.name = "keystone-rproc",
506e88bb8f7SSuman Anna 		.of_match_table = keystone_rproc_of_match,
507e88bb8f7SSuman Anna 	},
508e88bb8f7SSuman Anna };
509e88bb8f7SSuman Anna 
510e88bb8f7SSuman Anna module_platform_driver(keystone_rproc_driver);
511e88bb8f7SSuman Anna 
512e88bb8f7SSuman Anna MODULE_AUTHOR("Suman Anna <s-anna@ti.com>");
513e88bb8f7SSuman Anna MODULE_LICENSE("GPL v2");
514e88bb8f7SSuman Anna MODULE_DESCRIPTION("TI Keystone DSP Remoteproc driver");
515