1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2017 Intel Corporation
5 * Intel Mobile Internet Devices (MID) based on Intel Atom SoCs have few
6 * microcontrollers inside to do some auxiliary tasks. One of such
7 * microcontroller is System Controller Unit (SCU) which, in particular,
8 * is servicing watchdog and controlling system reset function.
10 * This driver enables IPC channel to SCU.
18 #include <linux/bitops.h>
19 #include <linux/delay.h>
20 #include <linux/errno.h>
22 #include <linux/kernel.h>
24 /* SCU register map */
36 struct ipc_regs *regs;
40 * scu_ipc_send_command() - send command to SCU
41 * @regs: register map of SCU
44 * Command Register (Write Only):
45 * A write to this register results in an interrupt to the SCU core processor
47 * |rfu2(8) | size(8) | command id(4) | rfu1(3) | ioc(1) | command(8)|
49 static void scu_ipc_send_command(struct ipc_regs *regs, u32 cmd)
51 writel(cmd, ®s->cmd);
55 * scu_ipc_check_status() - check status of last command
56 * @regs: register map of SCU
58 * Status Register (Read Only):
59 * Driver will read this register to get the ready/busy status of the IPC
60 * block and error status of the IPC command that was just processed by SCU
62 * |rfu3(8)|error code(8)|initiator id(8)|cmd id(4)|rfu1(2)|error(1)|busy(1)|
64 static int scu_ipc_check_status(struct ipc_regs *regs)
66 int loop_count = 100000;
70 status = readl(®s->status);
71 if (!(status & BIT(0)))
75 } while (--loop_count);
79 if (status & BIT(1)) {
80 printf("%s() status=0x%08x\n", __func__, status);
87 static int scu_ipc_cmd(struct ipc_regs *regs, u32 cmd, u32 sub,
88 u32 *in, int inlen, u32 *out, int outlen)
92 for (i = 0; i < inlen; i++)
93 writel(*in++, ®s->wbuf[i]);
95 scu_ipc_send_command(regs, (inlen << 16) | (sub << 12) | cmd);
96 err = scu_ipc_check_status(regs);
99 for (i = 0; i < outlen; i++)
100 *out++ = readl(®s->rbuf[i]);
107 * scu_ipc_raw_command() - IPC command with data and pointers
108 * @cmd: IPC command code
109 * @sub: IPC command sub type
110 * @in: input data of this IPC command
111 * @inlen: input data length in dwords
112 * @out: output data of this IPC command
113 * @outlen: output data length in dwords
114 * @dptr: data writing to SPTR register
115 * @sptr: data writing to DPTR register
117 * Send an IPC command to SCU with input/output data and source/dest pointers.
119 * Return: an IPC error code or 0 on success.
121 int scu_ipc_raw_command(u32 cmd, u32 sub, u32 *in, int inlen, u32 *out,
122 int outlen, u32 dptr, u32 sptr)
124 int inbuflen = DIV_ROUND_UP(inlen, 4);
129 ret = syscon_get_by_driver_data(X86_SYSCON_SCU, &dev);
133 scu = dev_get_priv(dev);
139 writel(dptr, &scu->regs->dptr);
140 writel(sptr, &scu->regs->sptr);
143 * SRAM controller doesn't support 8-bit writes, it only
144 * supports 32-bit writes, so we have to copy input data into
145 * the temporary buffer, and SCU FW will use the inlen to
146 * determine the actual input data length in the temporary
152 memcpy(inbuf, in, inlen);
154 return scu_ipc_cmd(scu->regs, cmd, sub, inbuf, inlen, out, outlen);
158 * scu_ipc_simple_command() - send a simple command
162 * Issue a simple command to the SCU. Do not use this interface if
163 * you must then access data as any data values may be overwritten
164 * by another SCU access by the time this function returns.
166 * This function may sleep. Locking for SCU accesses is handled for
169 int scu_ipc_simple_command(u32 cmd, u32 sub)
175 ret = syscon_get_by_driver_data(X86_SYSCON_SCU, &dev);
179 scu = dev_get_priv(dev);
181 scu_ipc_send_command(scu->regs, sub << 12 | cmd);
182 return scu_ipc_check_status(scu->regs);
186 * scu_ipc_command - command with data
190 * @inlen: input length in dwords
192 * @outlen: output length in dwords
194 * Issue a command to the SCU which involves data transfers.
196 int scu_ipc_command(u32 cmd, u32 sub, u32 *in, int inlen, u32 *out, int outlen)
202 ret = syscon_get_by_driver_data(X86_SYSCON_SCU, &dev);
206 scu = dev_get_priv(dev);
208 return scu_ipc_cmd(scu->regs, cmd, sub, in, inlen, out, outlen);
211 static int scu_ipc_probe(struct udevice *dev)
213 struct scu *scu = dev_get_priv(dev);
215 scu->regs = syscon_get_first_range(X86_SYSCON_SCU);
220 static const struct udevice_id scu_ipc_match[] = {
221 { .compatible = "intel,scu-ipc", .data = X86_SYSCON_SCU },
225 U_BOOT_DRIVER(scu_ipc) = {
228 .of_match = scu_ipc_match,
229 .probe = scu_ipc_probe,
230 .priv_auto = sizeof(struct scu),