1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Copyright (c) 2017 Intel Corporation
4 *
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.
9 *
10 * This driver enables IPC channel to SCU.
11 */
12 #include <dm.h>
13 #include <regmap.h>
14 #include <syscon.h>
15 #include <asm/cpu.h>
16 #include <asm/scu.h>
17 #include <linux/bitops.h>
18 #include <linux/delay.h>
19 #include <linux/errno.h>
20 #include <linux/io.h>
21 #include <linux/kernel.h>
22
23 /* SCU register map */
24 struct ipc_regs {
25 u32 cmd;
26 u32 status;
27 u32 sptr;
28 u32 dptr;
29 u32 reserved[28];
30 u32 wbuf[4];
31 u32 rbuf[4];
32 };
33
34 struct scu {
35 struct ipc_regs *regs;
36 };
37
38 /**
39 * scu_ipc_send_command() - send command to SCU
40 * @regs: register map of SCU
41 * @cmd: command
42 *
43 * Command Register (Write Only):
44 * A write to this register results in an interrupt to the SCU core processor
45 * Format:
46 * |rfu2(8) | size(8) | command id(4) | rfu1(3) | ioc(1) | command(8)|
47 */
scu_ipc_send_command(struct ipc_regs * regs,u32 cmd)48 static void scu_ipc_send_command(struct ipc_regs *regs, u32 cmd)
49 {
50 writel(cmd, ®s->cmd);
51 }
52
53 /**
54 * scu_ipc_check_status() - check status of last command
55 * @regs: register map of SCU
56 *
57 * Status Register (Read Only):
58 * Driver will read this register to get the ready/busy status of the IPC
59 * block and error status of the IPC command that was just processed by SCU
60 * Format:
61 * |rfu3(8)|error code(8)|initiator id(8)|cmd id(4)|rfu1(2)|error(1)|busy(1)|
62 */
scu_ipc_check_status(struct ipc_regs * regs)63 static int scu_ipc_check_status(struct ipc_regs *regs)
64 {
65 int loop_count = 100000;
66 int status;
67
68 do {
69 status = readl(®s->status);
70 if (!(status & BIT(0)))
71 break;
72
73 udelay(1);
74 } while (--loop_count);
75 if (!loop_count)
76 return -ETIMEDOUT;
77
78 if (status & BIT(1)) {
79 printf("%s() status=0x%08x\n", __func__, status);
80 return -EIO;
81 }
82
83 return 0;
84 }
85
scu_ipc_cmd(struct ipc_regs * regs,u32 cmd,u32 sub,u32 * in,int inlen,u32 * out,int outlen)86 static int scu_ipc_cmd(struct ipc_regs *regs, u32 cmd, u32 sub,
87 u32 *in, int inlen, u32 *out, int outlen)
88 {
89 int i, err;
90
91 for (i = 0; i < inlen; i++)
92 writel(*in++, ®s->wbuf[i]);
93
94 scu_ipc_send_command(regs, (inlen << 16) | (sub << 12) | cmd);
95 err = scu_ipc_check_status(regs);
96
97 if (!err) {
98 for (i = 0; i < outlen; i++)
99 *out++ = readl(®s->rbuf[i]);
100 }
101
102 return err;
103 }
104
105 /**
106 * scu_ipc_raw_command() - IPC command with data and pointers
107 * @cmd: IPC command code
108 * @sub: IPC command sub type
109 * @in: input data of this IPC command
110 * @inlen: input data length in dwords
111 * @out: output data of this IPC command
112 * @outlen: output data length in dwords
113 * @dptr: data writing to SPTR register
114 * @sptr: data writing to DPTR register
115 *
116 * Send an IPC command to SCU with input/output data and source/dest pointers.
117 *
118 * Return: an IPC error code or 0 on success.
119 */
scu_ipc_raw_command(u32 cmd,u32 sub,u32 * in,int inlen,u32 * out,int outlen,u32 dptr,u32 sptr)120 int scu_ipc_raw_command(u32 cmd, u32 sub, u32 *in, int inlen, u32 *out,
121 int outlen, u32 dptr, u32 sptr)
122 {
123 int inbuflen = DIV_ROUND_UP(inlen, 4);
124 struct udevice *dev;
125 struct scu *scu;
126 int ret;
127
128 ret = syscon_get_by_driver_data(X86_SYSCON_SCU, &dev);
129 if (ret)
130 return ret;
131
132 scu = dev_get_priv(dev);
133
134 /* Up to 16 bytes */
135 if (inbuflen > 4)
136 return -EINVAL;
137
138 writel(dptr, &scu->regs->dptr);
139 writel(sptr, &scu->regs->sptr);
140
141 /*
142 * SRAM controller doesn't support 8-bit writes, it only
143 * supports 32-bit writes, so we have to copy input data into
144 * the temporary buffer, and SCU FW will use the inlen to
145 * determine the actual input data length in the temporary
146 * buffer.
147 */
148
149 u32 inbuf[4] = {0};
150
151 memcpy(inbuf, in, inlen);
152
153 return scu_ipc_cmd(scu->regs, cmd, sub, inbuf, inlen, out, outlen);
154 }
155
156 /**
157 * scu_ipc_simple_command() - send a simple command
158 * @cmd: command
159 * @sub: sub type
160 *
161 * Issue a simple command to the SCU. Do not use this interface if
162 * you must then access data as any data values may be overwritten
163 * by another SCU access by the time this function returns.
164 *
165 * This function may sleep. Locking for SCU accesses is handled for
166 * the caller.
167 */
scu_ipc_simple_command(u32 cmd,u32 sub)168 int scu_ipc_simple_command(u32 cmd, u32 sub)
169 {
170 struct scu *scu;
171 struct udevice *dev;
172 int ret;
173
174 ret = syscon_get_by_driver_data(X86_SYSCON_SCU, &dev);
175 if (ret)
176 return ret;
177
178 scu = dev_get_priv(dev);
179
180 scu_ipc_send_command(scu->regs, sub << 12 | cmd);
181 return scu_ipc_check_status(scu->regs);
182 }
183
184 /**
185 * scu_ipc_command - command with data
186 * @cmd: command
187 * @sub: sub type
188 * @in: input data
189 * @inlen: input length in dwords
190 * @out: output data
191 * @outlen: output length in dwords
192 *
193 * Issue a command to the SCU which involves data transfers.
194 */
scu_ipc_command(u32 cmd,u32 sub,u32 * in,int inlen,u32 * out,int outlen)195 int scu_ipc_command(u32 cmd, u32 sub, u32 *in, int inlen, u32 *out, int outlen)
196 {
197 struct scu *scu;
198 struct udevice *dev;
199 int ret;
200
201 ret = syscon_get_by_driver_data(X86_SYSCON_SCU, &dev);
202 if (ret)
203 return ret;
204
205 scu = dev_get_priv(dev);
206
207 return scu_ipc_cmd(scu->regs, cmd, sub, in, inlen, out, outlen);
208 }
209
scu_ipc_probe(struct udevice * dev)210 static int scu_ipc_probe(struct udevice *dev)
211 {
212 struct scu *scu = dev_get_priv(dev);
213
214 scu->regs = syscon_get_first_range(X86_SYSCON_SCU);
215
216 return 0;
217 }
218
219 static const struct udevice_id scu_ipc_match[] = {
220 { .compatible = "intel,scu-ipc", .data = X86_SYSCON_SCU },
221 { /* sentinel */ }
222 };
223
224 U_BOOT_DRIVER(scu_ipc) = {
225 .name = "scu_ipc",
226 .id = UCLASS_SYSCON,
227 .of_match = scu_ipc_match,
228 .probe = scu_ipc_probe,
229 .priv_auto = sizeof(struct scu),
230 };
231