summaryrefslogtreecommitdiff
path: root/drivers/soc/imx/sc/main/ipc.c
blob: 55516d2880e49cd6fcc792eb8138a74549bb5ba7 (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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
/*
 * Copyright (C) 2016 Freescale Semiconductor, Inc.
 * Copyright 2017 NXP
 *
 * SPDX-License-Identifier:     GPL-2.0+
 */

/* Includes */
#include <linux/err.h>
#include <linux/kernel.h>
#include <linux/of.h>
#include <linux/of_irq.h>
#include <linux/of_address.h>
#include <linux/of_device.h>
#include <linux/of_fdt.h>
#include <linux/interrupt.h>
#include <linux/irq.h>
#include <linux/mx8_mu.h>

#include <soc/imx8/sc/svc/irq/api.h>
#include <soc/imx8/sc/ipc.h>
#include <soc/imx8/sc/sci.h>

#include "rpc.h"

/* Local Defines */
#define MU_SIZE 0x10000

/* Local Types */
unsigned int scu_mu_id;
static void __iomem *mu_base_virtaddr;
static struct delayed_work scu_mu_work;
static sc_ipc_t mu_ipcHandle;

/* Local functions */

/* Local variables */
static uint32_t gIPCport;
static bool scu_mu_init;

DEFINE_MUTEX(scu_mu_mutex);

static BLOCKING_NOTIFIER_HEAD(SCU_notifier_chain);

EXPORT_SYMBOL(sc_pm_set_resource_power_mode);
EXPORT_SYMBOL(sc_pm_get_resource_power_mode);
EXPORT_SYMBOL(sc_pm_cpu_start);
EXPORT_SYMBOL(sc_misc_set_control);
EXPORT_SYMBOL(sc_pm_clock_enable);
EXPORT_SYMBOL(sc_pm_set_clock_rate);
/*--------------------------------------------------------------------------*/
/* RPC command/response                                                     */
/*--------------------------------------------------------------------------*/
void sc_call_rpc(sc_ipc_t handle, sc_rpc_msg_t *msg, bool no_resp)
{
	if (in_interrupt()) {
		pr_warn("Cannot make SC IPC calls from an interrupt context\n");
		dump_stack();
		return;
	}
	mutex_lock(&scu_mu_mutex);

	sc_ipc_write(handle, msg);
	if (!no_resp)
		sc_ipc_read(handle, msg);

	mutex_unlock(&scu_mu_mutex);
}
EXPORT_SYMBOL(sc_call_rpc);

/*--------------------------------------------------------------------------*/
/* Get MU base address for specified IPC channel                            */
/*--------------------------------------------------------------------------*/
static uint32_t *sc_ipc_get_mu_base(uint32_t id)
{
	uint32_t *base;

	/* Check parameters */
	if (id >= SC_NUM_IPC)
		base = NULL;
	else
		base = (uint32_t *) (mu_base_virtaddr + (id * MU_SIZE));

	return base;
}

/*--------------------------------------------------------------------------*/
/* Get the MU ID used by Linux                                              */
/*--------------------------------------------------------------------------*/
int sc_ipc_getMuID(uint32_t *mu_id)
{
	if (scu_mu_init) {
		*mu_id = scu_mu_id;
		return SC_ERR_NONE;
	}
	return SC_ERR_UNAVAILABLE;
}

EXPORT_SYMBOL(sc_ipc_getMuID);

/*--------------------------------------------------------------------------*/
/* Open an IPC channel                                                      */
/*--------------------------------------------------------------------------*/
sc_err_t sc_ipc_requestInt(sc_ipc_t *handle, uint32_t id)
{
	return SC_ERR_NONE;
}

/*--------------------------------------------------------------------------*/
/* Open an IPC channel                                                      */
/*--------------------------------------------------------------------------*/
sc_err_t sc_ipc_open(sc_ipc_t *handle, uint32_t id)
{
	uint32_t *base;

	mutex_lock(&scu_mu_mutex);

	if (!scu_mu_init) {
		mutex_unlock(&scu_mu_mutex);
		return SC_ERR_UNAVAILABLE;
	}
	/* Get MU base associated with IPC channel */
	base = sc_ipc_get_mu_base(id);

	if (base == NULL) {
		mutex_unlock(&scu_mu_mutex);
		return SC_ERR_IPC;
	}

	*handle = (sc_ipc_t) task_pid_vnr(current);

	mutex_unlock(&scu_mu_mutex);

	return SC_ERR_NONE;
}
EXPORT_SYMBOL(sc_ipc_open);
/*--------------------------------------------------------------------------*/
/* Close an IPC channel                                                     */
/*--------------------------------------------------------------------------*/
void sc_ipc_close(sc_ipc_t handle)
{
	uint32_t *base;

	mutex_lock(&scu_mu_mutex);

	if (!scu_mu_init) {
		mutex_unlock(&scu_mu_mutex);
		return;
	}

	/* Get MU base associated with IPC channel */
	base = sc_ipc_get_mu_base(gIPCport);

	/* TBD ***** What needs to be done here? */
	mutex_unlock(&scu_mu_mutex);
}
EXPORT_SYMBOL(sc_ipc_close);

/*!
 * This function reads a message from an IPC channel.
 *
 * @param[in]     ipc         id of channel read from
 * @param[out]    data        pointer to message buffer to read
 *
 * This function will block if no message is available to be read.
 */
void sc_ipc_read(sc_ipc_t handle, void *data)
{
	uint32_t *base;
	uint8_t count = 0;
	sc_rpc_msg_t *msg = (sc_rpc_msg_t *) data;

	/* Get MU base associated with IPC channel */
	base = sc_ipc_get_mu_base(gIPCport);

	if ((base == NULL) || (msg == NULL))
		return;

	/* Read first word */
	MU_ReceiveMsg(base, 0, (uint32_t *) msg);
	count++;

	/* Check size */
	if (msg->size > SC_RPC_MAX_MSG) {
		*((uint32_t *) msg) = 0;
		return;
	}

	/* Read remaining words */
	while (count < msg->size) {
		MU_ReceiveMsg(base, count % MU_RR_COUNT,
				  &(msg->DATA.u32[count - 1]));
		count++;
	}
}

/*!
 * This function writes a message to an IPC channel.
 *
 * @param[in]     ipc         id of channel to write to
 * @param[in]     data        pointer to message buffer to write
 *
 * This function will block if the outgoing buffer is full.
 */
void sc_ipc_write(sc_ipc_t handle, void *data)
{
	uint32_t *base;
	uint8_t count = 0;
	sc_rpc_msg_t *msg = (sc_rpc_msg_t *) data;

	/* Get MU base associated with IPC channel */
	base = sc_ipc_get_mu_base(gIPCport);

	if ((base == NULL) || (msg == NULL))
		return;

	/* Check size */
	if (msg->size > SC_RPC_MAX_MSG)
		return;

	/* Write first word */
	MU_SendMessage(base, 0, *((uint32_t *) msg));
	count++;

	/* Write remaining words */
	while (count < msg->size) {
		MU_SendMessage(base, count % MU_TR_COUNT, msg->DATA.u32[count - 1]);
		count++;
	}
}

int register_scu_notifier(struct notifier_block *nb)
{
	return blocking_notifier_chain_register(&SCU_notifier_chain, nb);
}

EXPORT_SYMBOL(register_scu_notifier);

int unregister_scu_notifier(struct notifier_block *nb)
{
	return blocking_notifier_chain_unregister(&SCU_notifier_chain, nb);
}

EXPORT_SYMBOL(unregister_scu_notifier);

static int SCU_notifier_call_chain(unsigned long status, sc_irq_group_t *group)
{
	return blocking_notifier_call_chain(&SCU_notifier_chain, status,
						(void *)group);
}

static void scu_mu_work_handler(struct work_struct *work)
{
	uint32_t irq_status;
	sc_err_t sciErr;
	sc_irq_group_t i;

	/* Walk all groups interrupt callback, callback will judge if it's
	 * the right group for itself, return directly if not.
	 */
	for (i = 0; i < SC_IRQ_NUM_GROUP; i++) {
		sciErr = sc_irq_status(mu_ipcHandle, SC_R_MU_0A, i,
					&irq_status);
		/* no irq? */
		if (!irq_status)
			continue;

		SCU_notifier_call_chain(irq_status, &i);
	}
}

static irqreturn_t imx8_scu_mu_isr(int irq, void *param)
{
	u32 irqs;

	irqs = (readl_relaxed(mu_base_virtaddr + 0x20) & (0xf << 28));
	if (irqs) {
		/* Clear the General Interrupt */
		writel_relaxed(irqs, mu_base_virtaddr + 0x20);
		/* Setup a bottom-half to handle the irq work. */
		schedule_delayed_work(&scu_mu_work, 0);
	}
	return IRQ_HANDLED;
}

/*Initialization of the MU code. */
int __init imx8_mu_init(void)
{
	struct device_node *np;
	int irq;
	int err;
	sc_err_t sciErr;

	/*
	 * Get the address of MU to be used for communication with the SCU
	 */
	np = of_find_compatible_node(NULL, NULL, "fsl,imx8-mu");
	if (!np) {
		pr_info("Cannot find MU entry in device tree\n");
		return 0;
	}
	mu_base_virtaddr = of_iomap(np, 0);
	WARN_ON(!mu_base_virtaddr);

	err = of_property_read_u32_index(np, "fsl,scu_ap_mu_id", 0, &scu_mu_id);
	if (err)
		pr_info("imx8_mu_init: Cannot get mu_id err = %d\n", err);

	irq = of_irq_get(np, 0);

	if (irq <= 0) {
		/* SCU works just fine without irq */
		pr_warn("imx8_mu_init: no irq: %d\n", irq);
	} else {
		err = request_irq(irq, imx8_scu_mu_isr,
				  IRQF_EARLY_RESUME, "imx8_mu_isr", NULL);
		if (err) {
			pr_err("imx8_mu_init: request_irq %d failed: %d\n",
					irq, err);
			return err;
		}

		err = irq_set_irq_wake(irq, 1);
		if (err) {
			pr_err("imx8mu_init: set_irq_wake failed: %d\n", err);
			return err;
		}
	}

	if (!scu_mu_init) {
		uint32_t i;

		INIT_DELAYED_WORK(&scu_mu_work, scu_mu_work_handler);

		/* Init MU */
		MU_Init(mu_base_virtaddr);

#if 1
		/* Enable all RX interrupts */
		for (i = 0; i < MU_RR_COUNT; i++)
			MU_EnableGeneralInt(mu_base_virtaddr, i);
#endif
		gIPCport = scu_mu_id;
		scu_mu_init = true;
	}

	sciErr = sc_ipc_open(&mu_ipcHandle, scu_mu_id);
	if (sciErr != SC_ERR_NONE) {
		pr_info("Cannot open MU channel to SCU\n");
		return sciErr;
	};

	/* Request for the high temp interrupt. */
	sciErr = sc_irq_enable(mu_ipcHandle, SC_R_MU_0A, SC_IRQ_GROUP_TEMP,
			       SC_IRQ_TEMP_PMIC0_HIGH, true);

	if (sciErr)
		pr_info("Cannot request PMIC0_TEMP interrupt\n");

	/* Request for the high temp interrupt. */
	sciErr = sc_irq_enable(mu_ipcHandle, SC_R_MU_0A, SC_IRQ_GROUP_TEMP,
			       SC_IRQ_TEMP_PMIC1_HIGH, true);

	if (sciErr)
		pr_info("Cannot request PMIC1_TEMP interrupt\n");

	/* Request for the rtc alarm interrupt. */
	sciErr = sc_irq_enable(mu_ipcHandle, SC_R_MU_0A, SC_IRQ_GROUP_RTC,
			       SC_IRQ_RTC, true);

	if (sciErr)
		pr_info("Cannot request ALARM_RTC interrupt\n");

	/* Request for the ON/OFF interrupt. */
	sciErr = sc_irq_enable(mu_ipcHandle, SC_R_MU_0A, SC_IRQ_GROUP_WAKE,
			       SC_IRQ_BUTTON, true);

	if (sciErr)
		pr_info("Cannot request ON/OFF interrupt\n");

	/* Request for the watchdog interrupt. */
	sciErr = sc_irq_enable(mu_ipcHandle, SC_R_MU_0A, SC_IRQ_GROUP_WDOG,
			       SC_IRQ_WDOG, true);

	if (sciErr)
		pr_info("Cannot request WDOG interrupt\n");

	pr_info("*****Initialized MU\n");
	return scu_mu_id;
}

early_initcall(imx8_mu_init);