summaryrefslogtreecommitdiff
path: root/drivers/remoteproc/k3_rproc.c
blob: 3c29d925ce05ca3397eecd40e4994fddc6da0b12 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
// SPDX-License-Identifier: GPL-2.0+
/*
 * Texas Instruments' K3 Remoteproc driver
 *
 * Copyright (C) 2018 Texas Instruments Incorporated - http://www.ti.com/
 *	Lokesh Vutla <lokeshvutla@ti.com>
 *
 */

#include <common.h>
#include <dm.h>
#include <remoteproc.h>
#include <errno.h>
#include <clk.h>
#include <reset.h>
#include <asm/io.h>
#include <power-domain.h>
#include <linux/soc/ti/ti_sci_protocol.h>

#define INVALID_ID	0xffff

#define GTC_CNTCR_REG	0x0
#define GTC_CNTR_EN	0x3

/**
 * struct k3_rproc_privdata - Structure representing Remote processor data.
 * @rproc_pwrdmn:	rproc power domain data
 * @rproc_rst:		rproc reset control data
 * @sci:		Pointer to TISCI handle
 * @gtc_base:		Timer base address.
 * @proc_id:		TISCI processor ID
 * @host_id:		TISCI host id to which the processor gets assigned to.
 */
struct k3_rproc_privdata {
	struct power_domain rproc_pwrdmn;
	struct power_domain gtc_pwrdmn;
	struct reset_ctl rproc_rst;
	const struct ti_sci_handle *sci;
	void *gtc_base;
	u16 proc_id;
	u16 host_id;
};

/**
 * k3_rproc_load() - Load up the Remote processor image
 * @dev:	rproc device pointer
 * @addr:	Address at which image is available
 * @size:	size of the image
 *
 * Return: 0 if all goes good, else appropriate error message.
 */
static int k3_rproc_load(struct udevice *dev, ulong addr, ulong size)
{
	struct k3_rproc_privdata *rproc = dev_get_priv(dev);
	const struct ti_sci_proc_ops *pops = &rproc->sci->ops.proc_ops;
	int ret;

	dev_dbg(dev, "%s addr = 0x%lx, size = 0x%lx\n", __func__, addr, size);

	/* request for the processor */
	ret = pops->proc_request(rproc->sci, rproc->proc_id);
	if (ret) {
		dev_err(dev, "Requesting processor failed %d\n", ret);
		return ret;
	}

	ret = pops->set_proc_boot_cfg(rproc->sci, rproc->proc_id, addr, 0, 0);
	if (ret) {
		dev_err(dev, "set_proc_boot_cfg failed %d\n", ret);
		return ret;
	}

	dev_dbg(dev, "%s: rproc successfully loaded\n", __func__);

	return 0;
}

/**
 * k3_rproc_start() - Start the remote processor
 * @dev:	rproc device pointer
 *
 * Return: 0 if all went ok, else return appropriate error
 */
static int k3_rproc_start(struct udevice *dev)
{
	struct k3_rproc_privdata *rproc = dev_get_priv(dev);
	const struct ti_sci_proc_ops *pops = &rproc->sci->ops.proc_ops;
	int ret;

	dev_dbg(dev, "%s\n", __func__);

	ret = power_domain_on(&rproc->gtc_pwrdmn);
	if (ret) {
		dev_err(dev, "power_domain_on() failed: %d\n", ret);
		return ret;
	}

	/* Enable the timer before starting remote core */
	writel(GTC_CNTR_EN, rproc->gtc_base + GTC_CNTCR_REG);

	/*
	 * Setting the right clock frequency would have taken care by
	 * assigned-clock-rates during the device probe. So no need to
	 * set the frequency again here.
	 */
	ret = power_domain_on(&rproc->rproc_pwrdmn);
	if (ret) {
		dev_err(dev, "power_domain_on() failed: %d\n", ret);
		return ret;
	}

	if (rproc->host_id != INVALID_ID) {
		ret = pops->proc_handover(rproc->sci, rproc->proc_id,
					  rproc->host_id);
		if (ret) {
			dev_err(dev, "Handover processor failed %d\n", ret);
			return ret;
		}
	} else {
		ret = pops->proc_release(rproc->sci, rproc->proc_id);
		if (ret) {
			dev_err(dev, "Processor release failed %d\n", ret);
			return ret;
		}
	}

	dev_dbg(dev, "%s: rproc successfully started\n", __func__);

	return 0;
}

/**
 * k3_rproc_init() - Initialize the remote processor
 * @dev:	rproc device pointer
 *
 * Return: 0 if all went ok, else return appropriate error
 */
static int k3_rproc_init(struct udevice *dev)
{
	dev_dbg(dev, "%s\n", __func__);

	/* Enable the module */
	dev_dbg(dev, "%s: rproc successfully initialized\n", __func__);

	return 0;
}

static const struct dm_rproc_ops k3_rproc_ops = {
	.init = k3_rproc_init,
	.load = k3_rproc_load,
	.start = k3_rproc_start,
};

/**
 * k3_of_to_priv() - generate private data from device tree
 * @dev:	corresponding k3 remote processor device
 * @priv:	pointer to driver specific private data
 *
 * Return: 0 if all goes good, else appropriate error message.
 */
static int k3_rproc_of_to_priv(struct udevice *dev,
			       struct k3_rproc_privdata *rproc)
{
	int ret;

	dev_dbg(dev, "%s\n", __func__);

	ret = power_domain_get_by_index(dev, &rproc->rproc_pwrdmn, 1);
	if (ret) {
		dev_err(dev, "power_domain_get() failed: %d\n", ret);
		return ret;
	}

	ret = power_domain_get_by_index(dev, &rproc->gtc_pwrdmn, 0);
	if (ret) {
		dev_err(dev, "power_domain_get() failed: %d\n", ret);
		return ret;
	}

	ret = reset_get_by_index(dev, 0, &rproc->rproc_rst);
	if (ret) {
		dev_err(dev, "reset_get() failed: %d\n", ret);
		return ret;
	}

	rproc->sci = ti_sci_get_by_phandle(dev, "ti,sci");
	if (IS_ERR(rproc->sci)) {
		dev_err(dev, "ti_sci get failed: %d\n", ret);
		return PTR_ERR(rproc->sci);
	}

	rproc->gtc_base = dev_read_addr_ptr(dev);
	if (!rproc->gtc_base) {
		dev_err(dev, "Get address failed\n");
		return -ENODEV;
	}

	rproc->proc_id = dev_read_u32_default(dev, "ti,sci-proc-id",
					      INVALID_ID);
	rproc->host_id = dev_read_u32_default(dev, "ti,sci-host-id",
					      INVALID_ID);

	return 0;
}

/**
 * k3_rproc_probe() - Basic probe
 * @dev:	corresponding k3 remote processor device
 *
 * Return: 0 if all goes good, else appropriate error message.
 */
static int k3_rproc_probe(struct udevice *dev)
{
	struct k3_rproc_privdata *priv;
	int ret;

	dev_dbg(dev, "%s\n", __func__);

	priv = dev_get_priv(dev);

	ret = k3_rproc_of_to_priv(dev, priv);
	if (ret) {
		dev_dbg(dev, "%s: Probe failed with error %d\n", __func__, ret);
		return ret;
	}

	dev_dbg(dev, "Remoteproc successfully probed\n");

	return 0;
}

static const struct udevice_id k3_rproc_ids[] = {
	{ .compatible = "ti,am654-rproc"},
	{}
};

U_BOOT_DRIVER(k3_rproc) = {
	.name = "k3_rproc",
	.of_match = k3_rproc_ids,
	.id = UCLASS_REMOTEPROC,
	.ops = &k3_rproc_ops,
	.probe = k3_rproc_probe,
	.priv_auto_alloc_size = sizeof(struct k3_rproc_privdata),
};