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