arm: socfpga: mailbox: Always read mailbox responses before returning status
[platform/kernel/u-boot.git] / arch / arm / mach-socfpga / mailbox_s10.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 2017-2018 Intel Corporation <www.intel.com>
4  *
5  */
6
7 #include <common.h>
8 #include <hang.h>
9 #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
15 DECLARE_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
29 static __always_inline int mbox_polling_resp(u32 rout)
30 {
31         u32 rin;
32         unsigned long i = 2000;
33
34         while (i) {
35                 rin = MBOX_READL(MBOX_RIN);
36                 if (rout != rin)
37                         return 0;
38
39                 udelay(1000);
40                 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  */
49 static __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
60          * to fit the data. Note, len is in u32 unit.
61          */
62         if (((cin + 1) % MBOX_CMD_BUFFER_SIZE) == cout ||
63             ((MBOX_CMD_BUFFER_SIZE - cin + cout - 1) %
64              MBOX_CMD_BUFFER_SIZE) < (len + 1))
65                 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 */
86 static __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 */
109 static __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 */
121 static __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 */
152 static __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
165         if (urgent) {
166                 /* Read status because it is toggled */
167                 status = MBOX_READL(MBOX_STATUS) & MBOX_STATUS_UA_MSK;
168                 /* 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;
174         }
175
176         /* write doorbell */
177         MBOX_WRITEL(1, MBOX_DOORBELL_TO_SDM);
178
179         while (1) {
180                 ret = 1000;
181
182                 /* Wait for doorbell from SDM */
183                 do {
184                         if (MBOX_READL(MBOX_DOORBELL_FROM_SDM))
185                                 break;
186                         udelay(1000);
187                 } while (--ret);
188
189                 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);
197
198                         /* 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)) {
224                                 int resp_err = MBOX_RESP_ERR_GET(resp);
225
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                                 }
253                                 return resp_err;
254                         }
255                 }
256         };
257
258         return -EIO;
259 }
260
261 int 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
286 int 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
292 int mbox_qspi_open(void)
293 {
294         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]);
322         writel(resp_buf[0],
323                socfpga_get_sysmgr_addr() + SYSMGR_SOC64_BOOT_SCRATCH_COLD0);
324
325         return 0;
326
327 error:
328         mbox_qspi_close();
329
330         return ret;
331 }
332 #endif /* CONFIG_CADENCE_QSPI */
333
334 int 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
347 /* Accepted commands: CONFIG_STATUS or RECONFIG_STATUS */
348 static __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
385 int mbox_get_fpga_config_status(u32 cmd)
386 {
387         return mbox_get_fpga_config_status_common(cmd);
388 }
389
390 int __secure mbox_get_fpga_config_status_psci(u32 cmd)
391 {
392         return mbox_get_fpga_config_status_common(cmd);
393 }
394
395 int 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
402 int __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
410 int 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
415 int __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
421 int 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
426 int __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 }