blob: aee9e55194edfb299113f1dbd17586ca8f17d8c2 [file] [log] [blame]
Caleb Connolly90ff5812024-07-15 12:08:02 +02001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (c) 2016-2018, The Linux Foundation. All rights reserved.
4 * Copyright (c) 2023-2024, Qualcomm Innovation Center, Inc. All rights reserved.
5 */
6
7#define pr_fmt(fmt) "%s " fmt, KBUILD_MODNAME
8
Caleb Connollybdc5a792024-07-15 12:08:08 +02009#include <dm.h>
10#include <dm/device_compat.h>
11#include <dm/devres.h>
12#include <dm/lists.h>
13#include <dm/ofnode.h>
14#include <linux/bitmap.h>
Caleb Connolly90ff5812024-07-15 12:08:02 +020015#include <linux/delay.h>
Caleb Connollybdc5a792024-07-15 12:08:08 +020016#include <linux/err.h>
17#include <linux/types.h>
18#include <asm/bitops.h>
19#include <asm/io.h>
Caleb Connolly90ff5812024-07-15 12:08:02 +020020
Caleb Connollybdc5a792024-07-15 12:08:08 +020021#include <log.h>
22
Caleb Connolly90ff5812024-07-15 12:08:02 +020023#include <soc/qcom/tcs.h>
24#include <dt-bindings/soc/qcom,rpmh-rsc.h>
25
26#include "rpmh-internal.h"
27
Caleb Connolly90ff5812024-07-15 12:08:02 +020028
29#define RSC_DRV_ID 0
30
31#define MAJOR_VER_MASK 0xFF
32#define MAJOR_VER_SHIFT 16
33#define MINOR_VER_MASK 0xFF
34#define MINOR_VER_SHIFT 8
35
36enum {
37 RSC_DRV_TCS_OFFSET,
38 RSC_DRV_CMD_OFFSET,
39 DRV_SOLVER_CONFIG,
40 DRV_PRNT_CHLD_CONFIG,
41 RSC_DRV_IRQ_ENABLE,
42 RSC_DRV_IRQ_STATUS,
43 RSC_DRV_IRQ_CLEAR,
44 RSC_DRV_CMD_WAIT_FOR_CMPL,
45 RSC_DRV_CONTROL,
46 RSC_DRV_STATUS,
47 RSC_DRV_CMD_ENABLE,
48 RSC_DRV_CMD_MSGID,
49 RSC_DRV_CMD_ADDR,
50 RSC_DRV_CMD_DATA,
51 RSC_DRV_CMD_STATUS,
52 RSC_DRV_CMD_RESP_DATA,
53};
54
55/* DRV HW Solver Configuration Information Register */
56#define DRV_HW_SOLVER_MASK 1
57#define DRV_HW_SOLVER_SHIFT 24
58
59/* DRV TCS Configuration Information Register */
60#define DRV_NUM_TCS_MASK 0x3F
61#define DRV_NUM_TCS_SHIFT 6
62#define DRV_NCPT_MASK 0x1F
63#define DRV_NCPT_SHIFT 27
64
65/* Offsets for CONTROL TCS Registers */
66#define RSC_DRV_CTL_TCS_DATA_HI 0x38
67#define RSC_DRV_CTL_TCS_DATA_HI_MASK 0xFFFFFF
68#define RSC_DRV_CTL_TCS_DATA_HI_VALID BIT(31)
69#define RSC_DRV_CTL_TCS_DATA_LO 0x40
70#define RSC_DRV_CTL_TCS_DATA_LO_MASK 0xFFFFFFFF
71#define RSC_DRV_CTL_TCS_DATA_SIZE 32
72
73#define TCS_AMC_MODE_ENABLE BIT(16)
74#define TCS_AMC_MODE_TRIGGER BIT(24)
75
76/* TCS CMD register bit mask */
77#define CMD_MSGID_LEN 8
78#define CMD_MSGID_RESP_REQ BIT(8)
79#define CMD_MSGID_WRITE BIT(16)
80#define CMD_STATUS_ISSUED BIT(8)
81#define CMD_STATUS_COMPL BIT(16)
82
83/*
84 * Here's a high level overview of how all the registers in RPMH work
85 * together:
86 *
87 * - The main rpmh-rsc address is the base of a register space that can
88 * be used to find overall configuration of the hardware
89 * (DRV_PRNT_CHLD_CONFIG). Also found within the rpmh-rsc register
90 * space are all the TCS blocks. The offset of the TCS blocks is
91 * specified in the device tree by "qcom,tcs-offset" and used to
92 * compute tcs_base.
93 * - TCS blocks come one after another. Type, count, and order are
94 * specified by the device tree as "qcom,tcs-config".
95 * - Each TCS block has some registers, then space for up to 16 commands.
96 * Note that though address space is reserved for 16 commands, fewer
97 * might be present. See ncpt (num cmds per TCS).
98 *
99 * Here's a picture:
100 *
101 * +---------------------------------------------------+
102 * |RSC |
103 * | ctrl |
104 * | |
105 * | Drvs: |
106 * | +-----------------------------------------------+ |
107 * | |DRV0 | |
108 * | | ctrl/config | |
109 * | | IRQ | |
110 * | | | |
111 * | | TCSes: | |
112 * | | +------------------------------------------+ | |
113 * | | |TCS0 | | | | | | | | | | | | | | |
114 * | | | ctrl | 0| 1| 2| 3| 4| 5| .| .| .| .|14|15| | |
115 * | | | | | | | | | | | | | | | | | |
116 * | | +------------------------------------------+ | |
117 * | | +------------------------------------------+ | |
118 * | | |TCS1 | | | | | | | | | | | | | | |
119 * | | | ctrl | 0| 1| 2| 3| 4| 5| .| .| .| .|14|15| | |
120 * | | | | | | | | | | | | | | | | | |
121 * | | +------------------------------------------+ | |
122 * | | +------------------------------------------+ | |
123 * | | |TCS2 | | | | | | | | | | | | | | |
124 * | | | ctrl | 0| 1| 2| 3| 4| 5| .| .| .| .|14|15| | |
125 * | | | | | | | | | | | | | | | | | |
126 * | | +------------------------------------------+ | |
127 * | | ...... | |
128 * | +-----------------------------------------------+ |
129 * | +-----------------------------------------------+ |
130 * | |DRV1 | |
131 * | | (same as DRV0) | |
132 * | +-----------------------------------------------+ |
133 * | ...... |
134 * +---------------------------------------------------+
135 */
136
Caleb Connolly90ff5812024-07-15 12:08:02 +0200137static u32 rpmh_rsc_reg_offset_ver_2_7[] = {
138 [RSC_DRV_TCS_OFFSET] = 672,
139 [RSC_DRV_CMD_OFFSET] = 20,
140 [DRV_SOLVER_CONFIG] = 0x04,
141 [DRV_PRNT_CHLD_CONFIG] = 0x0C,
142 [RSC_DRV_IRQ_ENABLE] = 0x00,
143 [RSC_DRV_IRQ_STATUS] = 0x04,
144 [RSC_DRV_IRQ_CLEAR] = 0x08,
145 [RSC_DRV_CMD_WAIT_FOR_CMPL] = 0x10,
146 [RSC_DRV_CONTROL] = 0x14,
147 [RSC_DRV_STATUS] = 0x18,
148 [RSC_DRV_CMD_ENABLE] = 0x1C,
149 [RSC_DRV_CMD_MSGID] = 0x30,
150 [RSC_DRV_CMD_ADDR] = 0x34,
151 [RSC_DRV_CMD_DATA] = 0x38,
152 [RSC_DRV_CMD_STATUS] = 0x3C,
153 [RSC_DRV_CMD_RESP_DATA] = 0x40,
154};
155
156static u32 rpmh_rsc_reg_offset_ver_3_0[] = {
157 [RSC_DRV_TCS_OFFSET] = 672,
158 [RSC_DRV_CMD_OFFSET] = 24,
159 [DRV_SOLVER_CONFIG] = 0x04,
160 [DRV_PRNT_CHLD_CONFIG] = 0x0C,
161 [RSC_DRV_IRQ_ENABLE] = 0x00,
162 [RSC_DRV_IRQ_STATUS] = 0x04,
163 [RSC_DRV_IRQ_CLEAR] = 0x08,
164 [RSC_DRV_CMD_WAIT_FOR_CMPL] = 0x20,
165 [RSC_DRV_CONTROL] = 0x24,
166 [RSC_DRV_STATUS] = 0x28,
167 [RSC_DRV_CMD_ENABLE] = 0x2C,
168 [RSC_DRV_CMD_MSGID] = 0x34,
169 [RSC_DRV_CMD_ADDR] = 0x38,
170 [RSC_DRV_CMD_DATA] = 0x3C,
171 [RSC_DRV_CMD_STATUS] = 0x40,
172 [RSC_DRV_CMD_RESP_DATA] = 0x44,
173};
174
175static inline void __iomem *
176tcs_reg_addr(const struct rsc_drv *drv, int reg, int tcs_id)
177{
178 return drv->tcs_base + drv->regs[RSC_DRV_TCS_OFFSET] * tcs_id + reg;
179}
180
181static inline void __iomem *
182tcs_cmd_addr(const struct rsc_drv *drv, int reg, int tcs_id, int cmd_id)
183{
184 return tcs_reg_addr(drv, reg, tcs_id) + drv->regs[RSC_DRV_CMD_OFFSET] * cmd_id;
185}
186
187static u32 read_tcs_cmd(const struct rsc_drv *drv, int reg, int tcs_id,
188 int cmd_id)
189{
190 return readl_relaxed(tcs_cmd_addr(drv, reg, tcs_id, cmd_id));
191}
192
193static u32 read_tcs_reg(const struct rsc_drv *drv, int reg, int tcs_id)
194{
195 return readl_relaxed(tcs_reg_addr(drv, reg, tcs_id));
196}
197
198static void write_tcs_cmd(const struct rsc_drv *drv, int reg, int tcs_id,
199 int cmd_id, u32 data)
200{
201 writel_relaxed(data, tcs_cmd_addr(drv, reg, tcs_id, cmd_id));
202}
203
204static void write_tcs_reg(const struct rsc_drv *drv, int reg, int tcs_id,
205 u32 data)
206{
207 writel_relaxed(data, tcs_reg_addr(drv, reg, tcs_id));
208}
209
210static void write_tcs_reg_sync(const struct rsc_drv *drv, int reg, int tcs_id,
211 u32 data)
212{
213 int i;
214
215 writel(data, tcs_reg_addr(drv, reg, tcs_id));
216
217 /*
218 * Wait until we read back the same value. Use a counter rather than
219 * ktime for timeout since this may be called after timekeeping stops.
220 */
221 for (i = 0; i < USEC_PER_SEC; i++) {
222 if (readl(tcs_reg_addr(drv, reg, tcs_id)) == data)
223 return;
224 udelay(1);
225 }
226 pr_err("%s: error writing %#x to %d:%#x\n", drv->name,
227 data, tcs_id, reg);
228}
229
230/**
Caleb Connolly90ff5812024-07-15 12:08:02 +0200231 * get_tcs_for_msg() - Get the tcs_group used to send the given message.
232 * @drv: The RSC controller.
233 * @msg: The message we want to send.
234 *
235 * This is normally pretty straightforward except if we are trying to send
236 * an ACTIVE_ONLY message but don't have any active_only TCSes.
237 *
238 * Return: A pointer to a tcs_group or an ERR_PTR.
239 */
240static struct tcs_group *get_tcs_for_msg(struct rsc_drv *drv,
241 const struct tcs_request *msg)
242{
Caleb Connolly90ff5812024-07-15 12:08:02 +0200243 /*
Caleb Connolly974e7432024-07-15 12:08:10 +0200244 * U-Boot: since we're single threaded and running synchronously we can
245 * just always used the first active TCS.
Caleb Connolly90ff5812024-07-15 12:08:02 +0200246 */
Caleb Connolly974e7432024-07-15 12:08:10 +0200247 if (msg->state != RPMH_ACTIVE_ONLY_STATE) {
248 log_err("WARN: only ACTIVE_ONLY state supported\n");
249 return ERR_PTR(-EINVAL);
250 }
Caleb Connolly90ff5812024-07-15 12:08:02 +0200251
Caleb Connolly974e7432024-07-15 12:08:10 +0200252 return &drv->tcs[ACTIVE_TCS];
Caleb Connolly90ff5812024-07-15 12:08:02 +0200253}
254
255/**
Caleb Connolly90ff5812024-07-15 12:08:02 +0200256 * __tcs_buffer_write() - Write to TCS hardware from a request; don't trigger.
257 * @drv: The controller.
258 * @tcs_id: The global ID of this TCS.
259 * @cmd_id: The index within the TCS to start writing.
260 * @msg: The message we want to send, which will contain several addr/data
261 * pairs to program (but few enough that they all fit in one TCS).
262 *
263 * This is used for all types of transfers (active, sleep, and wake).
264 */
265static void __tcs_buffer_write(struct rsc_drv *drv, int tcs_id, int cmd_id,
266 const struct tcs_request *msg)
267{
268 u32 msgid;
269 u32 cmd_msgid = CMD_MSGID_LEN | CMD_MSGID_WRITE;
270 u32 cmd_enable = 0;
271 struct tcs_cmd *cmd;
272 int i, j;
273
Caleb Connolly90ff5812024-07-15 12:08:02 +0200274 for (i = 0, j = cmd_id; i < msg->num_cmds; i++, j++) {
275 cmd = &msg->cmds[i];
276 cmd_enable |= BIT(j);
277 msgid = cmd_msgid;
278 /*
279 * Additionally, if the cmd->wait is set, make the command
280 * response reqd even if the overall request was fire-n-forget.
281 */
282 msgid |= cmd->wait ? CMD_MSGID_RESP_REQ : 0;
283
284 write_tcs_cmd(drv, drv->regs[RSC_DRV_CMD_MSGID], tcs_id, j, msgid);
285 write_tcs_cmd(drv, drv->regs[RSC_DRV_CMD_ADDR], tcs_id, j, cmd->addr);
286 write_tcs_cmd(drv, drv->regs[RSC_DRV_CMD_DATA], tcs_id, j, cmd->data);
Caleb Connolly974e7432024-07-15 12:08:10 +0200287 debug("tcs(m): %d [%s] cmd(n): %d msgid: %#x addr: %#x data: %#x complete: %d\n",
288 tcs_id, msg->state == RPMH_ACTIVE_ONLY_STATE ? "active" : "?", j, msgid,
289 cmd->addr, cmd->data, cmd->wait);
Caleb Connolly90ff5812024-07-15 12:08:02 +0200290 }
291
292 cmd_enable |= read_tcs_reg(drv, drv->regs[RSC_DRV_CMD_ENABLE], tcs_id);
293 write_tcs_reg(drv, drv->regs[RSC_DRV_CMD_ENABLE], tcs_id, cmd_enable);
294}
295
296/**
Neil Armstrong4cc941f2024-09-03 18:13:30 +0200297 * __tcs_set_trigger() - Start xfer on a TCS or unset trigger on a borrowed TCS
298 * @drv: The controller.
299 * @tcs_id: The global ID of this TCS.
300 * @trigger: If true then untrigger/retrigger. If false then just untrigger.
301 *
302 * In the normal case we only ever call with "trigger=true" to start a
303 * transfer. That will un-trigger/disable the TCS from the last transfer
304 * then trigger/enable for this transfer.
305 *
306 * If we borrowed a wake TCS for an active-only transfer we'll also call
307 * this function with "trigger=false" to just do the un-trigger/disable
308 * before using the TCS for wake purposes again.
309 *
310 * Note that the AP is only in charge of triggering active-only transfers.
311 * The AP never triggers sleep/wake values using this function.
312 */
313static void __tcs_set_trigger(struct rsc_drv *drv, int tcs_id, bool trigger)
314{
315 u32 enable;
316 u32 reg = drv->regs[RSC_DRV_CONTROL];
317
318 /*
319 * HW req: Clear the DRV_CONTROL and enable TCS again
320 * While clearing ensure that the AMC mode trigger is cleared
321 * and then the mode enable is cleared.
322 */
323 enable = read_tcs_reg(drv, reg, tcs_id);
324 enable &= ~TCS_AMC_MODE_TRIGGER;
325 write_tcs_reg_sync(drv, reg, tcs_id, enable);
326 enable &= ~TCS_AMC_MODE_ENABLE;
327 write_tcs_reg_sync(drv, reg, tcs_id, enable);
328
329 if (trigger) {
330 /* Enable the AMC mode on the TCS and then trigger the TCS */
331 enable = TCS_AMC_MODE_ENABLE;
332 write_tcs_reg_sync(drv, reg, tcs_id, enable);
333 enable |= TCS_AMC_MODE_TRIGGER;
334 write_tcs_reg(drv, reg, tcs_id, enable);
335 }
336}
337
338/**
Caleb Connolly90ff5812024-07-15 12:08:02 +0200339 * rpmh_rsc_send_data() - Write / trigger active-only message.
340 * @drv: The controller.
341 * @msg: The data to be sent.
342 *
343 * NOTES:
344 * - This is only used for "ACTIVE_ONLY" since the limitations of this
345 * function don't make sense for sleep/wake cases.
346 * - To do the transfer, we will grab a whole TCS for ourselves--we don't
347 * try to share. If there are none available we'll wait indefinitely
348 * for a free one.
349 * - This function will not wait for the commands to be finished, only for
350 * data to be programmed into the RPMh. See rpmh_tx_done() which will
351 * be called when the transfer is fully complete.
352 * - This function must be called with interrupts enabled. If the hardware
353 * is busy doing someone else's transfer we need that transfer to fully
354 * finish so that we can have the hardware, and to fully finish it needs
355 * the interrupt handler to run. If the interrupts is set to run on the
356 * active CPU this can never happen if interrupts are disabled.
357 *
358 * Return: 0 on success, -EINVAL on error.
359 */
360int rpmh_rsc_send_data(struct rsc_drv *drv, const struct tcs_request *msg)
361{
362 struct tcs_group *tcs;
Caleb Connolly974e7432024-07-15 12:08:10 +0200363 int tcs_id, i;
364 u32 addr;
Caleb Connolly90ff5812024-07-15 12:08:02 +0200365
366 tcs = get_tcs_for_msg(drv, msg);
367 if (IS_ERR(tcs))
368 return PTR_ERR(tcs);
369
Caleb Connolly974e7432024-07-15 12:08:10 +0200370 /* u-boot is single-threaded, always use the first TCS as we'll never conflict */
371 tcs_id = tcs->offset;
Caleb Connolly90ff5812024-07-15 12:08:02 +0200372
373 tcs->req[tcs_id - tcs->offset] = msg;
Caleb Connolly974e7432024-07-15 12:08:10 +0200374 generic_set_bit(tcs_id, drv->tcs_in_use);
Caleb Connolly90ff5812024-07-15 12:08:02 +0200375 if (msg->state == RPMH_ACTIVE_ONLY_STATE && tcs->type != ACTIVE_TCS) {
376 /*
377 * Clear previously programmed WAKE commands in selected
378 * repurposed TCS to avoid triggering them. tcs->slots will be
379 * cleaned from rpmh_flush() by invoking rpmh_rsc_invalidate()
380 */
381 write_tcs_reg_sync(drv, drv->regs[RSC_DRV_CMD_ENABLE], tcs_id, 0);
Caleb Connolly90ff5812024-07-15 12:08:02 +0200382 }
Caleb Connolly90ff5812024-07-15 12:08:02 +0200383
384 /*
385 * These two can be done after the lock is released because:
386 * - We marked "tcs_in_use" under lock.
387 * - Once "tcs_in_use" has been marked nobody else could be writing
388 * to these registers until the interrupt goes off.
389 * - The interrupt can't go off until we trigger w/ the last line
390 * of __tcs_set_trigger() below.
391 */
392 __tcs_buffer_write(drv, tcs_id, 0, msg);
Neil Armstrong4cc941f2024-09-03 18:13:30 +0200393 __tcs_set_trigger(drv, tcs_id, true);
Caleb Connolly90ff5812024-07-15 12:08:02 +0200394
Caleb Connolly974e7432024-07-15 12:08:10 +0200395 /* U-Boot: Now wait for the TCS to be cleared, indicating that we're done */
396 for (i = 0; i < USEC_PER_SEC; i++) {
397 addr = read_tcs_cmd(drv, drv->regs[RSC_DRV_CMD_ADDR], i, 0);
398 if (addr != msg->cmds[0].addr)
399 break;
400 udelay(1);
401 }
402
403 if (i == USEC_PER_SEC) {
404 log_err("%s: error writing %#x to %d:%#x\n", drv->name,
405 msg->cmds[0].addr, tcs_id, drv->regs[RSC_DRV_CMD_ADDR]);
406 return -EINVAL;
407 }
408
Caleb Connolly90ff5812024-07-15 12:08:02 +0200409 return 0;
410}
411
Caleb Connolly8a540102024-07-15 12:08:09 +0200412static int rpmh_probe_tcs_config(struct udevice *dev, struct rsc_drv *drv)
Caleb Connolly90ff5812024-07-15 12:08:02 +0200413{
414 struct tcs_type_config {
415 u32 type;
416 u32 n;
417 } tcs_cfg[TCS_TYPE_NR] = { { 0 } };
Caleb Connolly8a540102024-07-15 12:08:09 +0200418 ofnode dn = dev_ofnode(dev);
Caleb Connolly90ff5812024-07-15 12:08:02 +0200419 u32 config, max_tcs, ncpt, offset;
420 int i, ret, n, st = 0;
421 struct tcs_group *tcs;
422
Caleb Connolly8a540102024-07-15 12:08:09 +0200423 ret = ofnode_read_u32(dn, "qcom,tcs-offset", &offset);
Caleb Connolly90ff5812024-07-15 12:08:02 +0200424 if (ret)
425 return ret;
426 drv->tcs_base = drv->base + offset;
427
428 config = readl_relaxed(drv->base + drv->regs[DRV_PRNT_CHLD_CONFIG]);
429
430 max_tcs = config;
431 max_tcs &= DRV_NUM_TCS_MASK << (DRV_NUM_TCS_SHIFT * drv->id);
432 max_tcs = max_tcs >> (DRV_NUM_TCS_SHIFT * drv->id);
433
434 ncpt = config & (DRV_NCPT_MASK << DRV_NCPT_SHIFT);
435 ncpt = ncpt >> DRV_NCPT_SHIFT;
436
Caleb Connolly8a540102024-07-15 12:08:09 +0200437 n = ofnode_read_u32_array(dn, "qcom,tcs-config", (u32 *)tcs_cfg, 2 * TCS_TYPE_NR);
438 if (n < 0) {
439 log_err("RPMh: %s: error reading qcom,tcs-config %d\n", dev->name, n);
440 return n;
441 }
Caleb Connolly90ff5812024-07-15 12:08:02 +0200442
443 for (i = 0; i < TCS_TYPE_NR; i++) {
Caleb Connolly90ff5812024-07-15 12:08:02 +0200444 if (tcs_cfg[i].n > MAX_TCS_PER_TYPE)
445 return -EINVAL;
446 }
447
448 for (i = 0; i < TCS_TYPE_NR; i++) {
449 tcs = &drv->tcs[tcs_cfg[i].type];
450 if (tcs->drv)
451 return -EINVAL;
452 tcs->drv = drv;
453 tcs->type = tcs_cfg[i].type;
454 tcs->num_tcs = tcs_cfg[i].n;
455 tcs->ncpt = ncpt;
456
457 if (!tcs->num_tcs || tcs->type == CONTROL_TCS)
458 continue;
459
460 if (st + tcs->num_tcs > max_tcs ||
461 st + tcs->num_tcs >= BITS_PER_BYTE * sizeof(tcs->mask))
462 return -EINVAL;
463
464 tcs->mask = ((1 << tcs->num_tcs) - 1) << st;
465 tcs->offset = st;
466 st += tcs->num_tcs;
467 }
468
469 drv->num_tcs = st;
470
471 return 0;
472}
473
Caleb Connolly8a540102024-07-15 12:08:09 +0200474static int rpmh_rsc_probe(struct udevice *dev)
Caleb Connolly90ff5812024-07-15 12:08:02 +0200475{
Caleb Connolly8a540102024-07-15 12:08:09 +0200476 ofnode dn = dev_ofnode(dev);
Caleb Connolly90ff5812024-07-15 12:08:02 +0200477 struct rsc_drv *drv;
478 char drv_id[10] = {0};
Caleb Connolly8a540102024-07-15 12:08:09 +0200479 int ret;
Caleb Connolly90ff5812024-07-15 12:08:02 +0200480 u32 rsc_id;
481
Caleb Connolly8a540102024-07-15 12:08:09 +0200482 drv = dev_get_priv(dev);
Caleb Connolly90ff5812024-07-15 12:08:02 +0200483
Caleb Connolly8a540102024-07-15 12:08:09 +0200484 ret = ofnode_read_u32(dn, "qcom,drv-id", &drv->id);
Caleb Connolly90ff5812024-07-15 12:08:02 +0200485 if (ret)
486 return ret;
487
Caleb Connolly8a540102024-07-15 12:08:09 +0200488 drv->name = ofnode_get_property(dn, "label", NULL);
Caleb Connolly90ff5812024-07-15 12:08:02 +0200489 if (!drv->name)
Caleb Connolly8a540102024-07-15 12:08:09 +0200490 drv->name = dev->name;
Caleb Connolly90ff5812024-07-15 12:08:02 +0200491
492 snprintf(drv_id, ARRAY_SIZE(drv_id), "drv-%d", drv->id);
Caleb Connolly8a540102024-07-15 12:08:09 +0200493 drv->base = (void __iomem *)dev_read_addr_name(dev, drv_id);
Caleb Connolly90ff5812024-07-15 12:08:02 +0200494 if (IS_ERR(drv->base))
495 return PTR_ERR(drv->base);
496
497 rsc_id = readl_relaxed(drv->base + RSC_DRV_ID);
498 drv->ver.major = rsc_id & (MAJOR_VER_MASK << MAJOR_VER_SHIFT);
499 drv->ver.major >>= MAJOR_VER_SHIFT;
500 drv->ver.minor = rsc_id & (MINOR_VER_MASK << MINOR_VER_SHIFT);
501 drv->ver.minor >>= MINOR_VER_SHIFT;
502
503 if (drv->ver.major == 3)
504 drv->regs = rpmh_rsc_reg_offset_ver_3_0;
505 else
506 drv->regs = rpmh_rsc_reg_offset_ver_2_7;
507
Caleb Connolly8a540102024-07-15 12:08:09 +0200508 ret = rpmh_probe_tcs_config(dev, drv);
Caleb Connolly90ff5812024-07-15 12:08:02 +0200509 if (ret)
510 return ret;
511
512 spin_lock_init(&drv->lock);
513 init_waitqueue_head(&drv->tcs_wait);
514 bitmap_zero(drv->tcs_in_use, MAX_TCS_NR);
515
Caleb Connolly90ff5812024-07-15 12:08:02 +0200516 /* Enable the active TCS to send requests immediately */
517 writel_relaxed(drv->tcs[ACTIVE_TCS].mask,
518 drv->tcs_base + drv->regs[RSC_DRV_IRQ_ENABLE]);
519
520 spin_lock_init(&drv->client.cache_lock);
521 INIT_LIST_HEAD(&drv->client.cache);
522 INIT_LIST_HEAD(&drv->client.batch_cache);
523
Caleb Connolly8a540102024-07-15 12:08:09 +0200524 dev_set_drvdata(dev, drv);
525 drv->dev = dev;
Caleb Connolly90ff5812024-07-15 12:08:02 +0200526
Caleb Connolly8a540102024-07-15 12:08:09 +0200527 log_debug("RPMh: %s: v%d.%d\n", dev->name, drv->ver.major, drv->ver.minor);
Caleb Connolly90ff5812024-07-15 12:08:02 +0200528
529 return ret;
530}
531
Caleb Connolly8a540102024-07-15 12:08:09 +0200532static const struct udevice_id qcom_rpmh_ids[] = {
533 { .compatible = "qcom,rpmh-rsc" },
Caleb Connolly90ff5812024-07-15 12:08:02 +0200534 { }
535};
Caleb Connolly90ff5812024-07-15 12:08:02 +0200536
Caleb Connolly8a540102024-07-15 12:08:09 +0200537U_BOOT_DRIVER(qcom_rpmh_rsc) = {
538 .name = "qcom_rpmh_rsc",
539 .id = UCLASS_MISC,
540 .priv_auto = sizeof(struct rsc_drv),
541 .probe = rpmh_rsc_probe,
542 .of_match = qcom_rpmh_ids,
543 /* rpmh is under CLUSTER_PD which we don't support, so skip trying to enable PDs */
544 .flags = DM_FLAG_DEFAULT_PD_CTRL_OFF,
Caleb Connolly90ff5812024-07-15 12:08:02 +0200545};
546
Caleb Connolly90ff5812024-07-15 12:08:02 +0200547MODULE_DESCRIPTION("Qualcomm Technologies, Inc. RPMh Driver");
548MODULE_LICENSE("GPL v2");