1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Texas Instruments' K3 Remoteproc driver
4  *
5  * Copyright (C) 2018 Texas Instruments Incorporated - http://www.ti.com/
6  *	Lokesh Vutla <lokeshvutla@ti.com>
7  *
8  */
9 
10 #include <common.h>
11 #include <dm.h>
12 #include <remoteproc.h>
13 #include <errno.h>
14 #include <clk.h>
15 #include <reset.h>
16 #include <asm/io.h>
17 #include <power-domain.h>
18 #include <linux/soc/ti/ti_sci_protocol.h>
19 
20 #define INVALID_ID	0xffff
21 
22 #define GTC_CNTCR_REG	0x0
23 #define GTC_CNTR_EN	0x3
24 
25 /**
26  * struct k3_rproc_privdata - Structure representing Remote processor data.
27  * @rproc_pwrdmn:	rproc power domain data
28  * @rproc_rst:		rproc reset control data
29  * @sci:		Pointer to TISCI handle
30  * @gtc_base:		Timer base address.
31  * @proc_id:		TISCI processor ID
32  * @host_id:		TISCI host id to which the processor gets assigned to.
33  */
34 struct k3_rproc_privdata {
35 	struct power_domain rproc_pwrdmn;
36 	struct power_domain gtc_pwrdmn;
37 	struct reset_ctl rproc_rst;
38 	const struct ti_sci_handle *sci;
39 	void *gtc_base;
40 	u16 proc_id;
41 	u16 host_id;
42 };
43 
44 /**
45  * k3_rproc_load() - Load up the Remote processor image
46  * @dev:	rproc device pointer
47  * @addr:	Address at which image is available
48  * @size:	size of the image
49  *
50  * Return: 0 if all goes good, else appropriate error message.
51  */
52 static int k3_rproc_load(struct udevice *dev, ulong addr, ulong size)
53 {
54 	struct k3_rproc_privdata *rproc = dev_get_priv(dev);
55 	const struct ti_sci_proc_ops *pops = &rproc->sci->ops.proc_ops;
56 	int ret;
57 
58 	dev_dbg(dev, "%s addr = 0x%lx, size = 0x%lx\n", __func__, addr, size);
59 
60 	/* request for the processor */
61 	ret = pops->proc_request(rproc->sci, rproc->proc_id);
62 	if (ret) {
63 		dev_err(dev, "Requesting processor failed %d\n", ret);
64 		return ret;
65 	}
66 
67 	ret = pops->set_proc_boot_cfg(rproc->sci, rproc->proc_id, addr, 0, 0);
68 	if (ret) {
69 		dev_err(dev, "set_proc_boot_cfg failed %d\n", ret);
70 		return ret;
71 	}
72 
73 	dev_dbg(dev, "%s: rproc successfully loaded\n", __func__);
74 
75 	return 0;
76 }
77 
78 /**
79  * k3_rproc_start() - Start the remote processor
80  * @dev:	rproc device pointer
81  *
82  * Return: 0 if all went ok, else return appropriate error
83  */
84 static int k3_rproc_start(struct udevice *dev)
85 {
86 	struct k3_rproc_privdata *rproc = dev_get_priv(dev);
87 	const struct ti_sci_proc_ops *pops = &rproc->sci->ops.proc_ops;
88 	int ret;
89 
90 	dev_dbg(dev, "%s\n", __func__);
91 
92 	ret = power_domain_on(&rproc->gtc_pwrdmn);
93 	if (ret) {
94 		dev_err(dev, "power_domain_on() failed: %d\n", ret);
95 		return ret;
96 	}
97 
98 	/* Enable the timer before starting remote core */
99 	writel(GTC_CNTR_EN, rproc->gtc_base + GTC_CNTCR_REG);
100 
101 	/*
102 	 * Setting the right clock frequency would have taken care by
103 	 * assigned-clock-rates during the device probe. So no need to
104 	 * set the frequency again here.
105 	 */
106 	ret = power_domain_on(&rproc->rproc_pwrdmn);
107 	if (ret) {
108 		dev_err(dev, "power_domain_on() failed: %d\n", ret);
109 		return ret;
110 	}
111 
112 	if (rproc->host_id != INVALID_ID) {
113 		ret = pops->proc_handover(rproc->sci, rproc->proc_id,
114 					  rproc->host_id);
115 		if (ret) {
116 			dev_err(dev, "Handover processor failed %d\n", ret);
117 			return ret;
118 		}
119 	} else {
120 		ret = pops->proc_release(rproc->sci, rproc->proc_id);
121 		if (ret) {
122 			dev_err(dev, "Processor release failed %d\n", ret);
123 			return ret;
124 		}
125 	}
126 
127 	dev_dbg(dev, "%s: rproc successfully started\n", __func__);
128 
129 	return 0;
130 }
131 
132 /**
133  * k3_rproc_init() - Initialize the remote processor
134  * @dev:	rproc device pointer
135  *
136  * Return: 0 if all went ok, else return appropriate error
137  */
138 static int k3_rproc_init(struct udevice *dev)
139 {
140 	dev_dbg(dev, "%s\n", __func__);
141 
142 	/* Enable the module */
143 	dev_dbg(dev, "%s: rproc successfully initialized\n", __func__);
144 
145 	return 0;
146 }
147 
148 static const struct dm_rproc_ops k3_rproc_ops = {
149 	.init = k3_rproc_init,
150 	.load = k3_rproc_load,
151 	.start = k3_rproc_start,
152 };
153 
154 /**
155  * k3_of_to_priv() - generate private data from device tree
156  * @dev:	corresponding k3 remote processor device
157  * @priv:	pointer to driver specific private data
158  *
159  * Return: 0 if all goes good, else appropriate error message.
160  */
161 static int k3_rproc_of_to_priv(struct udevice *dev,
162 			       struct k3_rproc_privdata *rproc)
163 {
164 	int ret;
165 
166 	dev_dbg(dev, "%s\n", __func__);
167 
168 	ret = power_domain_get_by_index(dev, &rproc->rproc_pwrdmn, 1);
169 	if (ret) {
170 		dev_err(dev, "power_domain_get() failed: %d\n", ret);
171 		return ret;
172 	}
173 
174 	ret = power_domain_get_by_index(dev, &rproc->gtc_pwrdmn, 0);
175 	if (ret) {
176 		dev_err(dev, "power_domain_get() failed: %d\n", ret);
177 		return ret;
178 	}
179 
180 	ret = reset_get_by_index(dev, 0, &rproc->rproc_rst);
181 	if (ret) {
182 		dev_err(dev, "reset_get() failed: %d\n", ret);
183 		return ret;
184 	}
185 
186 	rproc->sci = ti_sci_get_by_phandle(dev, "ti,sci");
187 	if (IS_ERR(rproc->sci)) {
188 		dev_err(dev, "ti_sci get failed: %d\n", ret);
189 		return PTR_ERR(rproc->sci);
190 	}
191 
192 	rproc->gtc_base = dev_read_addr_ptr(dev);
193 	if (!rproc->gtc_base) {
194 		dev_err(dev, "Get address failed\n");
195 		return -ENODEV;
196 	}
197 
198 	rproc->proc_id = dev_read_u32_default(dev, "ti,sci-proc-id",
199 					      INVALID_ID);
200 	rproc->host_id = dev_read_u32_default(dev, "ti,sci-host-id",
201 					      INVALID_ID);
202 
203 	return 0;
204 }
205 
206 /**
207  * k3_rproc_probe() - Basic probe
208  * @dev:	corresponding k3 remote processor device
209  *
210  * Return: 0 if all goes good, else appropriate error message.
211  */
212 static int k3_rproc_probe(struct udevice *dev)
213 {
214 	struct k3_rproc_privdata *priv;
215 	int ret;
216 
217 	dev_dbg(dev, "%s\n", __func__);
218 
219 	priv = dev_get_priv(dev);
220 
221 	ret = k3_rproc_of_to_priv(dev, priv);
222 	if (ret) {
223 		dev_dbg(dev, "%s: Probe failed with error %d\n", __func__, ret);
224 		return ret;
225 	}
226 
227 	dev_dbg(dev, "Remoteproc successfully probed\n");
228 
229 	return 0;
230 }
231 
232 static const struct udevice_id k3_rproc_ids[] = {
233 	{ .compatible = "ti,am654-rproc"},
234 	{}
235 };
236 
237 U_BOOT_DRIVER(k3_rproc) = {
238 	.name = "k3_rproc",
239 	.of_match = k3_rproc_ids,
240 	.id = UCLASS_REMOTEPROC,
241 	.ops = &k3_rproc_ops,
242 	.probe = k3_rproc_probe,
243 	.priv_auto_alloc_size = sizeof(struct k3_rproc_privdata),
244 };
245