blob: e8a587f00796d61fefb2905487af6e045d8da9b9 [file] [log] [blame]
Ley Foon Tana280e9d2018-05-24 00:17:25 +08001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (C) 2017-2018 Intel Corporation <www.intel.com>
4 *
5 */
6
7#include <common.h>
Simon Glassdb41d652019-12-28 10:45:07 -07008#include <hang.h>
Ley Foon Tana280e9d2018-05-24 00:17:25 +08009#include <wait_bit.h>
10#include <asm/io.h>
11#include <asm/arch/mailbox_s10.h>
12#include <asm/arch/system_manager.h>
13#include <asm/secure.h>
14
15DECLARE_GLOBAL_DATA_PTR;
16
17#define MBOX_READL(reg) \
18 readl(SOCFPGA_MAILBOX_ADDRESS + (reg))
19
20#define MBOX_WRITEL(data, reg) \
21 writel(data, SOCFPGA_MAILBOX_ADDRESS + (reg))
22
23#define MBOX_READ_RESP_BUF(rout) \
24 MBOX_READL(MBOX_RESP_BUF + ((rout) * sizeof(u32)))
25
26#define MBOX_WRITE_CMD_BUF(data, cin) \
27 MBOX_WRITEL(data, MBOX_CMD_BUF + ((cin) * sizeof(u32)))
28
29static __always_inline int mbox_polling_resp(u32 rout)
30{
31 u32 rin;
Chee Hong Ange3fca502020-08-12 09:56:21 +080032 unsigned long i = 2000;
Ley Foon Tana280e9d2018-05-24 00:17:25 +080033
34 while (i) {
35 rin = MBOX_READL(MBOX_RIN);
36 if (rout != rin)
37 return 0;
38
Chee Hong Ange3fca502020-08-12 09:56:21 +080039 udelay(1000);
Ley Foon Tana280e9d2018-05-24 00:17:25 +080040 i--;
41 }
42
43 return -ETIMEDOUT;
44}
45
46/* Check for available slot and write to circular buffer.
47 * It also update command valid offset (cin) register.
48 */
49static __always_inline int mbox_fill_cmd_circular_buff(u32 header, u32 len,
50 u32 *arg)
51{
52 u32 cin;
53 u32 cout;
54 u32 i;
55
56 cin = MBOX_READL(MBOX_CIN) % MBOX_CMD_BUFFER_SIZE;
57 cout = MBOX_READL(MBOX_COUT) % MBOX_CMD_BUFFER_SIZE;
58
59 /* if command buffer is full or not enough free space
Ley Foon Tanbf068c72019-04-24 13:21:47 +080060 * to fit the data. Note, len is in u32 unit.
Ley Foon Tana280e9d2018-05-24 00:17:25 +080061 */
62 if (((cin + 1) % MBOX_CMD_BUFFER_SIZE) == cout ||
63 ((MBOX_CMD_BUFFER_SIZE - cin + cout - 1) %
Ley Foon Tanbf068c72019-04-24 13:21:47 +080064 MBOX_CMD_BUFFER_SIZE) < (len + 1))
Ley Foon Tana280e9d2018-05-24 00:17:25 +080065 return -ENOMEM;
66
67 /* write header to circular buffer */
68 MBOX_WRITE_CMD_BUF(header, cin++);
69 /* wrapping around when it reach the buffer size */
70 cin %= MBOX_CMD_BUFFER_SIZE;
71
72 /* write arguments */
73 for (i = 0; i < len; i++) {
74 MBOX_WRITE_CMD_BUF(arg[i], cin++);
75 /* wrapping around when it reach the buffer size */
76 cin %= MBOX_CMD_BUFFER_SIZE;
77 }
78
79 /* write command valid offset */
80 MBOX_WRITEL(cin, MBOX_CIN);
81
82 return 0;
83}
84
85/* Check the command and fill it into circular buffer */
86static __always_inline int mbox_prepare_cmd_only(u8 id, u32 cmd,
87 u8 is_indirect, u32 len,
88 u32 *arg)
89{
90 u32 header;
91 int ret;
92
93 /* Total length is command + argument length */
94 if ((len + 1) > MBOX_CMD_BUFFER_SIZE)
95 return -EINVAL;
96
97 if (cmd > MBOX_MAX_CMD_INDEX)
98 return -EINVAL;
99
100 header = MBOX_CMD_HEADER(MBOX_CLIENT_ID_UBOOT, id, len,
101 (is_indirect) ? 1 : 0, cmd);
102
103 ret = mbox_fill_cmd_circular_buff(header, len, arg);
104
105 return ret;
106}
107
108/* Send command only without waiting for responses from SDM */
109static __always_inline int mbox_send_cmd_only_common(u8 id, u32 cmd,
110 u8 is_indirect, u32 len,
111 u32 *arg)
112{
113 int ret = mbox_prepare_cmd_only(id, cmd, is_indirect, len, arg);
114 /* write doorbell */
115 MBOX_WRITEL(1, MBOX_DOORBELL_TO_SDM);
116
117 return ret;
118}
119
120/* Return number of responses received in buffer */
121static __always_inline int __mbox_rcv_resp(u32 *resp_buf, u32 resp_buf_max_len)
122{
123 u32 rin;
124 u32 rout;
125 u32 resp_len = 0;
126
127 /* clear doorbell from SDM if it was SET */
128 if (MBOX_READL(MBOX_DOORBELL_FROM_SDM) & 1)
129 MBOX_WRITEL(0, MBOX_DOORBELL_FROM_SDM);
130
131 /* read current response offset */
132 rout = MBOX_READL(MBOX_ROUT);
133 /* read response valid offset */
134 rin = MBOX_READL(MBOX_RIN);
135
136 while (rin != rout && (resp_len < resp_buf_max_len)) {
137 /* Response received */
138 if (resp_buf)
139 resp_buf[resp_len++] = MBOX_READ_RESP_BUF(rout);
140
141 rout++;
142 /* wrapping around when it reach the buffer size */
143 rout %= MBOX_RESP_BUFFER_SIZE;
144 /* update next ROUT */
145 MBOX_WRITEL(rout, MBOX_ROUT);
146 }
147
148 return resp_len;
149}
150
151/* Support one command and up to 31 words argument length only */
152static __always_inline int mbox_send_cmd_common(u8 id, u32 cmd, u8 is_indirect,
153 u32 len, u32 *arg, u8 urgent,
154 u32 *resp_buf_len,
155 u32 *resp_buf)
156{
157 u32 rin;
158 u32 resp;
159 u32 rout;
160 u32 status;
161 u32 resp_len;
162 u32 buf_len;
163 int ret;
164
Ley Foon Tana280e9d2018-05-24 00:17:25 +0800165 if (urgent) {
166 /* Read status because it is toggled */
167 status = MBOX_READL(MBOX_STATUS) & MBOX_STATUS_UA_MSK;
Ley Foon Tan8497cb92018-08-17 16:22:03 +0800168 /* Write urgent command to urgent register */
169 MBOX_WRITEL(cmd, MBOX_URG);
170 } else {
171 ret = mbox_prepare_cmd_only(id, cmd, is_indirect, len, arg);
172 if (ret)
173 return ret;
Ley Foon Tana280e9d2018-05-24 00:17:25 +0800174 }
175
176 /* write doorbell */
177 MBOX_WRITEL(1, MBOX_DOORBELL_TO_SDM);
178
179 while (1) {
Chee Hong Ange3fca502020-08-12 09:56:21 +0800180 ret = 1000;
Ley Foon Tana280e9d2018-05-24 00:17:25 +0800181
182 /* Wait for doorbell from SDM */
Chee Hong Ange3fca502020-08-12 09:56:21 +0800183 do {
184 if (MBOX_READL(MBOX_DOORBELL_FROM_SDM))
185 break;
186 udelay(1000);
187 } while (--ret);
188
Ley Foon Tana280e9d2018-05-24 00:17:25 +0800189 if (!ret)
190 return -ETIMEDOUT;
191
192 /* clear interrupt */
193 MBOX_WRITEL(0, MBOX_DOORBELL_FROM_SDM);
194
195 if (urgent) {
196 u32 new_status = MBOX_READL(MBOX_STATUS);
Ley Foon Tan8497cb92018-08-17 16:22:03 +0800197
Ley Foon Tana280e9d2018-05-24 00:17:25 +0800198 /* Urgent ACK is toggled */
199 if ((new_status & MBOX_STATUS_UA_MSK) ^ status)
200 return 0;
201
202 return -ECOMM;
203 }
204
205 /* read current response offset */
206 rout = MBOX_READL(MBOX_ROUT);
207
208 /* read response valid offset */
209 rin = MBOX_READL(MBOX_RIN);
210
211 if (rout != rin) {
212 /* Response received */
213 resp = MBOX_READ_RESP_BUF(rout);
214 rout++;
215 /* wrapping around when it reach the buffer size */
216 rout %= MBOX_RESP_BUFFER_SIZE;
217 /* update next ROUT */
218 MBOX_WRITEL(rout, MBOX_ROUT);
219
220 /* check client ID and ID */
221 if ((MBOX_RESP_CLIENT_GET(resp) ==
222 MBOX_CLIENT_ID_UBOOT) &&
223 (MBOX_RESP_ID_GET(resp) == id)) {
Chee Hong Ang833230e2020-08-12 09:56:22 +0800224 int resp_err = MBOX_RESP_ERR_GET(resp);
Ley Foon Tana280e9d2018-05-24 00:17:25 +0800225
226 if (resp_buf_len) {
227 buf_len = *resp_buf_len;
228 *resp_buf_len = 0;
229 } else {
230 buf_len = 0;
231 }
232
233 resp_len = MBOX_RESP_LEN_GET(resp);
234 while (resp_len) {
235 ret = mbox_polling_resp(rout);
236 if (ret)
237 return ret;
238 /* we need to process response buffer
239 * even caller doesn't need it
240 */
241 resp = MBOX_READ_RESP_BUF(rout);
242 rout++;
243 resp_len--;
244 rout %= MBOX_RESP_BUFFER_SIZE;
245 MBOX_WRITEL(rout, MBOX_ROUT);
246 if (buf_len) {
247 /* copy response to buffer */
248 resp_buf[*resp_buf_len] = resp;
249 (*resp_buf_len)++;
250 buf_len--;
251 }
252 }
Chee Hong Ang833230e2020-08-12 09:56:22 +0800253 return resp_err;
Ley Foon Tana280e9d2018-05-24 00:17:25 +0800254 }
255 }
256 };
257
258 return -EIO;
259}
260
261int mbox_init(void)
262{
263 int ret;
264
265 /* enable mailbox interrupts */
266 MBOX_WRITEL(MBOX_ALL_INTRS, MBOX_FLAGS);
267
268 /* Ensure urgent request is cleared */
269 MBOX_WRITEL(0, MBOX_URG);
270
271 /* Ensure the Doorbell Interrupt is cleared */
272 MBOX_WRITEL(0, MBOX_DOORBELL_FROM_SDM);
273
274 ret = mbox_send_cmd(MBOX_ID_UBOOT, MBOX_RESTART, MBOX_CMD_DIRECT, 0,
275 NULL, 1, 0, NULL);
276 if (ret)
277 return ret;
278
279 /* Renable mailbox interrupts after MBOX_RESTART */
280 MBOX_WRITEL(MBOX_ALL_INTRS, MBOX_FLAGS);
281
282 return 0;
283}
284
285#ifdef CONFIG_CADENCE_QSPI
286int mbox_qspi_close(void)
287{
288 return mbox_send_cmd(MBOX_ID_UBOOT, MBOX_QSPI_CLOSE, MBOX_CMD_DIRECT,
289 0, NULL, 0, 0, NULL);
290}
291
292int mbox_qspi_open(void)
293{
Ley Foon Tana280e9d2018-05-24 00:17:25 +0800294 int ret;
295 u32 resp_buf[1];
296 u32 resp_buf_len;
297
298 ret = mbox_send_cmd(MBOX_ID_UBOOT, MBOX_QSPI_OPEN, MBOX_CMD_DIRECT,
299 0, NULL, 0, 0, NULL);
300 if (ret) {
301 /* retry again by closing and reopen the QSPI again */
302 ret = mbox_qspi_close();
303 if (ret)
304 return ret;
305
306 ret = mbox_send_cmd(MBOX_ID_UBOOT, MBOX_QSPI_OPEN,
307 MBOX_CMD_DIRECT, 0, NULL, 0, 0, NULL);
308 if (ret)
309 return ret;
310 }
311
312 /* HPS will directly control the QSPI controller, no longer mailbox */
313 resp_buf_len = 1;
314 ret = mbox_send_cmd(MBOX_ID_UBOOT, MBOX_QSPI_DIRECT, MBOX_CMD_DIRECT,
315 0, NULL, 0, (u32 *)&resp_buf_len,
316 (u32 *)&resp_buf);
317 if (ret)
318 goto error;
319
320 /* We are getting QSPI ref clock and set into sysmgr boot register */
321 printf("QSPI: Reference clock at %d Hz\n", resp_buf[0]);
Ley Foon Tandb5741f2019-11-08 10:38:20 +0800322 writel(resp_buf[0],
Ley Foon Tan2fd1dc52019-11-27 15:55:18 +0800323 socfpga_get_sysmgr_addr() + SYSMGR_SOC64_BOOT_SCRATCH_COLD0);
Ley Foon Tana280e9d2018-05-24 00:17:25 +0800324
325 return 0;
326
327error:
328 mbox_qspi_close();
329
330 return ret;
331}
332#endif /* CONFIG_CADENCE_QSPI */
333
334int mbox_reset_cold(void)
335{
336 int ret;
337
338 ret = mbox_send_cmd(MBOX_ID_UBOOT, MBOX_REBOOT_HPS, MBOX_CMD_DIRECT,
339 0, NULL, 0, 0, NULL);
340 if (ret) {
341 /* mailbox sent failure, wait for watchdog to kick in */
342 hang();
343 }
344 return 0;
345}
346
Ang, Chee Hongd99f1e92018-12-19 18:35:12 -0800347/* Accepted commands: CONFIG_STATUS or RECONFIG_STATUS */
348static __always_inline int mbox_get_fpga_config_status_common(u32 cmd)
349{
350 u32 reconfig_status_resp_len;
351 u32 reconfig_status_resp[RECONFIG_STATUS_RESPONSE_LEN];
352 int ret;
353
354 reconfig_status_resp_len = RECONFIG_STATUS_RESPONSE_LEN;
355 ret = mbox_send_cmd_common(MBOX_ID_UBOOT, cmd,
356 MBOX_CMD_DIRECT, 0, NULL, 0,
357 &reconfig_status_resp_len,
358 reconfig_status_resp);
359
360 if (ret)
361 return ret;
362
363 /* Check for any error */
364 ret = reconfig_status_resp[RECONFIG_STATUS_STATE];
365 if (ret && ret != MBOX_CFGSTAT_STATE_CONFIG)
366 return ret;
367
368 /* Make sure nStatus is not 0 */
369 ret = reconfig_status_resp[RECONFIG_STATUS_PIN_STATUS];
370 if (!(ret & RCF_PIN_STATUS_NSTATUS))
371 return MBOX_CFGSTAT_STATE_ERROR_HARDWARE;
372
373 ret = reconfig_status_resp[RECONFIG_STATUS_SOFTFUNC_STATUS];
374 if (ret & RCF_SOFTFUNC_STATUS_SEU_ERROR)
375 return MBOX_CFGSTAT_STATE_ERROR_HARDWARE;
376
377 if ((ret & RCF_SOFTFUNC_STATUS_CONF_DONE) &&
378 (ret & RCF_SOFTFUNC_STATUS_INIT_DONE) &&
379 !reconfig_status_resp[RECONFIG_STATUS_STATE])
380 return 0; /* configuration success */
381
382 return MBOX_CFGSTAT_STATE_CONFIG;
383}
384
385int mbox_get_fpga_config_status(u32 cmd)
386{
387 return mbox_get_fpga_config_status_common(cmd);
388}
389
390int __secure mbox_get_fpga_config_status_psci(u32 cmd)
391{
392 return mbox_get_fpga_config_status_common(cmd);
393}
394
Ley Foon Tana280e9d2018-05-24 00:17:25 +0800395int mbox_send_cmd(u8 id, u32 cmd, u8 is_indirect, u32 len, u32 *arg,
396 u8 urgent, u32 *resp_buf_len, u32 *resp_buf)
397{
398 return mbox_send_cmd_common(id, cmd, is_indirect, len, arg, urgent,
399 resp_buf_len, resp_buf);
400}
401
402int __secure mbox_send_cmd_psci(u8 id, u32 cmd, u8 is_indirect, u32 len,
403 u32 *arg, u8 urgent, u32 *resp_buf_len,
404 u32 *resp_buf)
405{
406 return mbox_send_cmd_common(id, cmd, is_indirect, len, arg, urgent,
407 resp_buf_len, resp_buf);
408}
409
410int mbox_send_cmd_only(u8 id, u32 cmd, u8 is_indirect, u32 len, u32 *arg)
411{
412 return mbox_send_cmd_only_common(id, cmd, is_indirect, len, arg);
413}
414
415int __secure mbox_send_cmd_only_psci(u8 id, u32 cmd, u8 is_indirect, u32 len,
416 u32 *arg)
417{
418 return mbox_send_cmd_only_common(id, cmd, is_indirect, len, arg);
419}
420
421int mbox_rcv_resp(u32 *resp_buf, u32 resp_buf_max_len)
422{
423 return __mbox_rcv_resp(resp_buf, resp_buf_max_len);
424}
425
426int __secure mbox_rcv_resp_psci(u32 *resp_buf, u32 resp_buf_max_len)
427{
428 return __mbox_rcv_resp(resp_buf, resp_buf_max_len);
429}