blob: 56567dd6727604beaa465e1c1afa5afac3d0f712 [file] [log] [blame]
Soren Brinkmann76fcae32016-03-06 20:16:27 -08001/*
Michal Simek2a47faa2023-04-14 08:43:51 +02002 * Copyright (c) 2013-2020, Arm Limited and Contributors. All rights reserved.
Rajan Vaja566b2f92022-08-31 12:54:38 +02003 * Copyright (c) 2019-2022, Xilinx, Inc. All rights reserved.
Prasad Kummari7d0623a2023-06-09 14:32:00 +05304 * Copyright (c) 2022-2023, Advanced Micro Devices, Inc. All rights reserved.
Soren Brinkmann76fcae32016-03-06 20:16:27 -08005 *
dp-armfa3cf0b2017-05-03 09:38:09 +01006 * SPDX-License-Identifier: BSD-3-Clause
Soren Brinkmann76fcae32016-03-06 20:16:27 -08007 */
8
Jolly Shah259afe52019-01-09 12:37:57 -08009
Isla Mitchelle3631462017-07-14 10:46:32 +010010#include <arch_helpers.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000011#include <lib/bakery_lock.h>
12#include <lib/mmio.h>
Jay Buddhabhattic6d624b2023-03-02 02:47:36 -080013#include <lib/spinlock.h>
Prasad Kummari536e1102023-06-22 10:50:02 +053014#include <plat/common/platform.h>
15
Jolly Shahc2583ab2019-01-08 11:31:49 -080016#include <ipi.h>
Jolly Shah4c172372019-01-08 11:21:29 -080017#include <plat_ipi.h>
Jolly Shah0bfd7002019-01-08 11:10:47 -080018#include <plat_private.h>
Rajan Vaja566b2f92022-08-31 12:54:38 +020019#include "pm_defs.h"
Isla Mitchelle3631462017-07-14 10:46:32 +010020#include "pm_ipi.h"
Soren Brinkmann76fcae32016-03-06 20:16:27 -080021
HariBabu Gattem9f9db612022-09-15 22:35:11 -070022#define ERROR_CODE_MASK (0xFFFFU)
HariBabu Gattemaa811712022-10-07 00:07:49 -070023#define PM_OFFSET (0U)
Ravi Patel65123712020-11-23 04:19:08 -080024
Jay Buddhabhattic6d624b2023-03-02 02:47:36 -080025/*
26 * ARM v8.2, the cache will turn off automatically when cpu
27 * power down. Therefore, there is no doubt to use the spin_lock here.
28 */
29#if !HW_ASSISTED_COHERENCY
Stefan Krsmanovicc5e9f662016-05-20 15:51:08 +020030DEFINE_BAKERY_LOCK(pm_secure_lock);
Jay Buddhabhattic6d624b2023-03-02 02:47:36 -080031static inline void pm_ipi_lock_get(void)
32{
33 bakery_lock_get(&pm_secure_lock);
34}
35
36static inline void pm_ipi_lock_release(void)
37{
38 bakery_lock_release(&pm_secure_lock);
39}
40#else
41spinlock_t pm_secure_lock;
42static inline void pm_ipi_lock_get(void)
43{
44 spin_lock(&pm_secure_lock);
45}
46
47static inline void pm_ipi_lock_release(void)
48{
49 spin_unlock(&pm_secure_lock);
50}
51#endif
Soren Brinkmann76fcae32016-03-06 20:16:27 -080052
Soren Brinkmann76fcae32016-03-06 20:16:27 -080053/**
Jolly Shah9bb628d2019-01-07 12:53:32 -080054 * pm_ipi_init() - Initialize IPI peripheral for communication with
Prasad Kummari7d0623a2023-06-09 14:32:00 +053055 * remote processor.
56 * @proc: Pointer to the processor who is initiating request.
Soren Brinkmann76fcae32016-03-06 20:16:27 -080057 *
Prasad Kummari7d0623a2023-06-09 14:32:00 +053058 * Return: On success, the initialization function must return 0.
59 * Any other return value will cause the framework to ignore
60 * the service.
Soren Brinkmann76fcae32016-03-06 20:16:27 -080061 *
Prasad Kummari7d0623a2023-06-09 14:32:00 +053062 * Called from pm_setup initialization function.
Soren Brinkmann76fcae32016-03-06 20:16:27 -080063 */
HariBabu Gattemaa811712022-10-07 00:07:49 -070064void pm_ipi_init(const struct pm_proc *proc)
Soren Brinkmann76fcae32016-03-06 20:16:27 -080065{
Jolly Shah36c930b2019-01-07 12:51:40 -080066 ipi_mb_open(proc->ipi->local_ipi_id, proc->ipi->remote_ipi_id);
Soren Brinkmann76fcae32016-03-06 20:16:27 -080067}
68
69/**
Prasad Kummari7d0623a2023-06-09 14:32:00 +053070 * pm_ipi_send_common() - Sends IPI request to the remote processor.
71 * @proc: Pointer to the processor who is initiating request.
72 * @payload: API id and call arguments to be written in IPI buffer.
73 * @is_blocking: if to trigger the notification in blocking mode or not.
Soren Brinkmann76fcae32016-03-06 20:16:27 -080074 *
75 * Send an IPI request to the power controller. Caller needs to hold
76 * the 'pm_secure_lock' lock.
77 *
Prasad Kummari7d0623a2023-06-09 14:32:00 +053078 * Return: Returns status, either success or error+reason.
79 *
Soren Brinkmann76fcae32016-03-06 20:16:27 -080080 */
81static enum pm_ret_status pm_ipi_send_common(const struct pm_proc *proc,
Tejas Patelaf4b10e2018-02-09 02:42:59 -080082 uint32_t payload[PAYLOAD_ARG_CNT],
83 uint32_t is_blocking)
Soren Brinkmann76fcae32016-03-06 20:16:27 -080084{
HariBabu Gattemaa811712022-10-07 00:07:49 -070085 uint32_t offset = PM_OFFSET;
Soren Brinkmann76fcae32016-03-06 20:16:27 -080086 uintptr_t buffer_base = proc->ipi->buffer_base +
Jolly Shah9bb628d2019-01-07 12:53:32 -080087 IPI_BUFFER_TARGET_REMOTE_OFFSET +
Soren Brinkmann76fcae32016-03-06 20:16:27 -080088 IPI_BUFFER_REQ_OFFSET;
Venkatesh Yadav Abbarapubfa2a2a2021-02-19 01:40:14 -070089#if IPI_CRC_CHECK
Venkatesh Yadav Abbarapu35a8be92019-04-04 14:23:32 +053090 payload[PAYLOAD_CRC_POS] = calculate_crc(payload, IPI_W0_TO_W6_SIZE);
91#endif
Soren Brinkmann76fcae32016-03-06 20:16:27 -080092
Soren Brinkmann76fcae32016-03-06 20:16:27 -080093 /* Write payload into IPI buffer */
94 for (size_t i = 0; i < PAYLOAD_ARG_CNT; i++) {
95 mmio_write_32(buffer_base + offset, payload[i]);
96 offset += PAYLOAD_ARG_SIZE;
97 }
Tejas Patelaf4b10e2018-02-09 02:42:59 -080098
Jolly Shah9bb628d2019-01-07 12:53:32 -080099 /* Generate IPI to remote processor */
Venkatesh Yadav Abbarapuefa1d322021-08-04 21:33:15 -0600100 ipi_mb_notify(proc->ipi->local_ipi_id, proc->ipi->remote_ipi_id,
Tejas Patelaf4b10e2018-02-09 02:42:59 -0800101 is_blocking);
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800102
Venkatesh Yadav Abbarapuefa1d322021-08-04 21:33:15 -0600103 return PM_RET_SUCCESS;
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800104}
105
106/**
Jolly Shah9bb628d2019-01-07 12:53:32 -0800107 * pm_ipi_send_non_blocking() - Sends IPI request to the remote processor
Prasad Kummari7d0623a2023-06-09 14:32:00 +0530108 * without blocking notification.
109 * @proc: Pointer to the processor who is initiating request.
110 * @payload: API id and call arguments to be written in IPI buffer.
Tejas Patelaf4b10e2018-02-09 02:42:59 -0800111 *
112 * Send an IPI request to the power controller.
113 *
Prasad Kummari7d0623a2023-06-09 14:32:00 +0530114 * Return: Returns status, either success or error+reason.
115 *
Tejas Patelaf4b10e2018-02-09 02:42:59 -0800116 */
117enum pm_ret_status pm_ipi_send_non_blocking(const struct pm_proc *proc,
118 uint32_t payload[PAYLOAD_ARG_CNT])
119{
120 enum pm_ret_status ret;
121
Jay Buddhabhattic6d624b2023-03-02 02:47:36 -0800122 pm_ipi_lock_get();
Tejas Patelaf4b10e2018-02-09 02:42:59 -0800123
124 ret = pm_ipi_send_common(proc, payload, IPI_NON_BLOCKING);
125
Jay Buddhabhattic6d624b2023-03-02 02:47:36 -0800126 pm_ipi_lock_release();
Tejas Patelaf4b10e2018-02-09 02:42:59 -0800127
128 return ret;
129}
130
131/**
Prasad Kummari7d0623a2023-06-09 14:32:00 +0530132 * pm_ipi_send() - Sends IPI request to the remote processor.
133 * @proc: Pointer to the processor who is initiating request.
134 * @payload: API id and call arguments to be written in IPI buffer.
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800135 *
136 * Send an IPI request to the power controller.
137 *
Prasad Kummari7d0623a2023-06-09 14:32:00 +0530138 * Return: Returns status, either success or error+reason.
139 *
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800140 */
141enum pm_ret_status pm_ipi_send(const struct pm_proc *proc,
142 uint32_t payload[PAYLOAD_ARG_CNT])
143{
144 enum pm_ret_status ret;
145
Jay Buddhabhattic6d624b2023-03-02 02:47:36 -0800146 pm_ipi_lock_get();
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800147
Tejas Patelaf4b10e2018-02-09 02:42:59 -0800148 ret = pm_ipi_send_common(proc, payload, IPI_BLOCKING);
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800149
Jay Buddhabhattic6d624b2023-03-02 02:47:36 -0800150 pm_ipi_lock_release();
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800151
152 return ret;
153}
154
155
156/**
Jolly Shah9bb628d2019-01-07 12:53:32 -0800157 * pm_ipi_buff_read() - Reads IPI response after remote processor has handled
Prasad Kummari7d0623a2023-06-09 14:32:00 +0530158 * interrupt.
159 * @proc: Pointer to the processor who is waiting and reading response.
160 * @value: Used to return value from IPI buffer element (optional).
161 * @count: Number of values to return in @value.
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800162 *
Prasad Kummari7d0623a2023-06-09 14:32:00 +0530163 * Return: Returns status, either success or error+reason.
164 *
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800165 */
166static enum pm_ret_status pm_ipi_buff_read(const struct pm_proc *proc,
Venkatesh Yadav Abbarapue7c45382022-05-19 14:49:49 +0530167 uint32_t *value, size_t count)
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800168{
Soren Brinkmannd6c9e032016-09-22 11:35:47 -0700169 size_t i;
Naman Trivedi Manojbhaic0dfba82023-03-07 12:41:11 +0530170 enum pm_ret_status ret;
Venkatesh Yadav Abbarapubfa2a2a2021-02-19 01:40:14 -0700171#if IPI_CRC_CHECK
Naman Trivedi Manojbhaic0dfba82023-03-07 12:41:11 +0530172 uint32_t *payload_ptr = value;
Venkatesh Yadav Abbarapu35a8be92019-04-04 14:23:32 +0530173 size_t j;
Venkatesh Yadav Abbarapue7c45382022-05-19 14:49:49 +0530174 uint32_t response_payload[PAYLOAD_ARG_CNT];
Venkatesh Yadav Abbarapu35a8be92019-04-04 14:23:32 +0530175#endif
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800176 uintptr_t buffer_base = proc->ipi->buffer_base +
Jolly Shah9bb628d2019-01-07 12:53:32 -0800177 IPI_BUFFER_TARGET_REMOTE_OFFSET +
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800178 IPI_BUFFER_RESP_OFFSET;
179
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800180 /*
181 * Read response from IPI buffer
182 * buf-0: success or error+reason
183 * buf-1: value
184 * buf-2: unused
185 * buf-3: unused
186 */
Soren Brinkmannd6c9e032016-09-22 11:35:47 -0700187 for (i = 1; i <= count; i++) {
188 *value = mmio_read_32(buffer_base + (i * PAYLOAD_ARG_SIZE));
189 value++;
190 }
Naman Trivedi Manojbhaic0dfba82023-03-07 12:41:11 +0530191
192 ret = mmio_read_32(buffer_base);
Venkatesh Yadav Abbarapubfa2a2a2021-02-19 01:40:14 -0700193#if IPI_CRC_CHECK
Venkatesh Yadav Abbarapu987fad32022-04-29 13:52:00 +0530194 for (j = 0; j < PAYLOAD_ARG_CNT; j++) {
Venkatesh Yadav Abbarapu35a8be92019-04-04 14:23:32 +0530195 response_payload[j] = mmio_read_32(buffer_base +
196 (j * PAYLOAD_ARG_SIZE));
Venkatesh Yadav Abbarapu987fad32022-04-29 13:52:00 +0530197 }
Venkatesh Yadav Abbarapu35a8be92019-04-04 14:23:32 +0530198
199 if (response_payload[PAYLOAD_CRC_POS] !=
Venkatesh Yadav Abbarapu987fad32022-04-29 13:52:00 +0530200 calculate_crc(response_payload, IPI_W0_TO_W6_SIZE)) {
Venkatesh Yadav Abbarapu35a8be92019-04-04 14:23:32 +0530201 NOTICE("ERROR in CRC response payload value:0x%x\n",
202 response_payload[PAYLOAD_CRC_POS]);
Naman Trivedi Manojbhaic0dfba82023-03-07 12:41:11 +0530203 ret = PM_RET_ERROR_INVALID_CRC;
204 /* Payload data is invalid as CRC validation failed
205 * Clear the payload to avoid leakage of data to upper layers
206 */
207 memset(payload_ptr, 0, count);
Venkatesh Yadav Abbarapu987fad32022-04-29 13:52:00 +0530208 }
Venkatesh Yadav Abbarapu35a8be92019-04-04 14:23:32 +0530209#endif
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800210
Naman Trivedi Manojbhaic0dfba82023-03-07 12:41:11 +0530211 return ret;
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800212}
213
214/**
Naman Patele9756422023-02-15 01:01:21 -0800215 * pm_ipi_buff_read_callb() - Callback function that reads value from
Prasad Kummari7d0623a2023-06-09 14:32:00 +0530216 * ipi response buffer.
217 * @value: Used to return value from IPI buffer element.
218 * @count: Number of values to return in @value.
Soren Brinkmann84f0af42016-09-30 14:24:25 -0700219 *
Naman Patele9756422023-02-15 01:01:21 -0800220 * This callback function fills requested data in @value from ipi response
221 * buffer.
Prasad Kummari7d0623a2023-06-09 14:32:00 +0530222 *
223 * Return: Returns status, either success or error.
224 *
Soren Brinkmann84f0af42016-09-30 14:24:25 -0700225 */
Naman Trivedi Manojbhaibeec83f2023-03-07 12:41:12 +0530226enum pm_ret_status pm_ipi_buff_read_callb(uint32_t *value, size_t count)
Soren Brinkmann84f0af42016-09-30 14:24:25 -0700227{
228 size_t i;
Venkatesh Yadav Abbarapubfa2a2a2021-02-19 01:40:14 -0700229#if IPI_CRC_CHECK
Naman Trivedi Manojbhaibeec83f2023-03-07 12:41:12 +0530230 uint32_t *payload_ptr = value;
Venkatesh Yadav Abbarapu35a8be92019-04-04 14:23:32 +0530231 size_t j;
HariBabu Gattemaa811712022-10-07 00:07:49 -0700232 unsigned int response_payload[PAYLOAD_ARG_CNT] = {0};
Venkatesh Yadav Abbarapu35a8be92019-04-04 14:23:32 +0530233#endif
Jolly Shah9bb628d2019-01-07 12:53:32 -0800234 uintptr_t buffer_base = IPI_BUFFER_REMOTE_BASE +
235 IPI_BUFFER_TARGET_LOCAL_OFFSET +
Soren Brinkmann84f0af42016-09-30 14:24:25 -0700236 IPI_BUFFER_REQ_OFFSET;
Naman Trivedi Manojbhaibeec83f2023-03-07 12:41:12 +0530237 enum pm_ret_status ret = PM_RET_SUCCESS;
Soren Brinkmann84f0af42016-09-30 14:24:25 -0700238
Venkatesh Yadav Abbarapu987fad32022-04-29 13:52:00 +0530239 if (count > IPI_BUFFER_MAX_WORDS) {
Soren Brinkmann84f0af42016-09-30 14:24:25 -0700240 count = IPI_BUFFER_MAX_WORDS;
Venkatesh Yadav Abbarapu987fad32022-04-29 13:52:00 +0530241 }
Soren Brinkmann84f0af42016-09-30 14:24:25 -0700242
243 for (i = 0; i <= count; i++) {
244 *value = mmio_read_32(buffer_base + (i * PAYLOAD_ARG_SIZE));
245 value++;
246 }
Venkatesh Yadav Abbarapubfa2a2a2021-02-19 01:40:14 -0700247#if IPI_CRC_CHECK
Venkatesh Yadav Abbarapu987fad32022-04-29 13:52:00 +0530248 for (j = 0; j < PAYLOAD_ARG_CNT; j++) {
Venkatesh Yadav Abbarapu35a8be92019-04-04 14:23:32 +0530249 response_payload[j] = mmio_read_32(buffer_base +
250 (j * PAYLOAD_ARG_SIZE));
Venkatesh Yadav Abbarapu987fad32022-04-29 13:52:00 +0530251 }
Venkatesh Yadav Abbarapu35a8be92019-04-04 14:23:32 +0530252
253 if (response_payload[PAYLOAD_CRC_POS] !=
Venkatesh Yadav Abbarapu987fad32022-04-29 13:52:00 +0530254 calculate_crc(response_payload, IPI_W0_TO_W6_SIZE)) {
Venkatesh Yadav Abbarapu35a8be92019-04-04 14:23:32 +0530255 NOTICE("ERROR in CRC response payload value:0x%x\n",
256 response_payload[PAYLOAD_CRC_POS]);
Naman Trivedi Manojbhaibeec83f2023-03-07 12:41:12 +0530257 ret = PM_RET_ERROR_INVALID_CRC;
258 /* Payload data is invalid as CRC validation failed
259 * Clear the payload to avoid leakage of data to upper layers
260 */
261 memset(payload_ptr, 0, count);
Venkatesh Yadav Abbarapu987fad32022-04-29 13:52:00 +0530262 }
Venkatesh Yadav Abbarapu35a8be92019-04-04 14:23:32 +0530263#endif
Naman Trivedi Manojbhaibeec83f2023-03-07 12:41:12 +0530264 return ret;
Soren Brinkmann84f0af42016-09-30 14:24:25 -0700265}
266
267/**
Prasad Kummari7d0623a2023-06-09 14:32:00 +0530268 * pm_ipi_send_sync() - Sends IPI request to the remote processor.
269 * @proc: Pointer to the processor who is initiating request.
270 * @payload: API id and call arguments to be written in IPI buffer.
271 * @value: Used to return value from IPI buffer element (optional).
272 * @count: Number of values to return in @value.
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800273 *
274 * Send an IPI request to the power controller and wait for it to be handled.
275 *
Prasad Kummari7d0623a2023-06-09 14:32:00 +0530276 * Return: Returns status, either success or error+reason and, optionally,
277 * @value.
278 *
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800279 */
280enum pm_ret_status pm_ipi_send_sync(const struct pm_proc *proc,
281 uint32_t payload[PAYLOAD_ARG_CNT],
Venkatesh Yadav Abbarapue7c45382022-05-19 14:49:49 +0530282 uint32_t *value, size_t count)
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800283{
284 enum pm_ret_status ret;
285
Jay Buddhabhattic6d624b2023-03-02 02:47:36 -0800286 pm_ipi_lock_get();
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800287
Tejas Patelaf4b10e2018-02-09 02:42:59 -0800288 ret = pm_ipi_send_common(proc, payload, IPI_BLOCKING);
Venkatesh Yadav Abbarapu987fad32022-04-29 13:52:00 +0530289 if (ret != PM_RET_SUCCESS) {
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800290 goto unlock;
Venkatesh Yadav Abbarapu987fad32022-04-29 13:52:00 +0530291 }
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800292
Ravi Patel65123712020-11-23 04:19:08 -0800293 ret = ERROR_CODE_MASK & (pm_ipi_buff_read(proc, value, count));
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800294
295unlock:
Jay Buddhabhattic6d624b2023-03-02 02:47:36 -0800296 pm_ipi_lock_release();
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800297
298 return ret;
299}
Soren Brinkmanna1b0a902016-09-30 11:30:21 -0700300
Wendy Liang328105c2017-10-03 23:21:11 -0700301void pm_ipi_irq_enable(const struct pm_proc *proc)
Soren Brinkmanna1b0a902016-09-30 11:30:21 -0700302{
Jolly Shah36c930b2019-01-07 12:51:40 -0800303 ipi_mb_enable_irq(proc->ipi->local_ipi_id, proc->ipi->remote_ipi_id);
Soren Brinkmanna1b0a902016-09-30 11:30:21 -0700304}
Soren Brinkmann84f0af42016-09-30 14:24:25 -0700305
Wendy Liang328105c2017-10-03 23:21:11 -0700306void pm_ipi_irq_clear(const struct pm_proc *proc)
Soren Brinkmann84f0af42016-09-30 14:24:25 -0700307{
Jolly Shah36c930b2019-01-07 12:51:40 -0800308 ipi_mb_ack(proc->ipi->local_ipi_id, proc->ipi->remote_ipi_id);
Soren Brinkmann84f0af42016-09-30 14:24:25 -0700309}
Jolly Shah259afe52019-01-09 12:37:57 -0800310
311uint32_t pm_ipi_irq_status(const struct pm_proc *proc)
312{
Venkatesh Yadav Abbarapue7c45382022-05-19 14:49:49 +0530313 int32_t ret;
Jolly Shah259afe52019-01-09 12:37:57 -0800314
315 ret = ipi_mb_enquire_status(proc->ipi->local_ipi_id,
316 proc->ipi->remote_ipi_id);
Venkatesh Yadav Abbarapu987fad32022-04-29 13:52:00 +0530317 if (ret & IPI_MB_STATUS_RECV_PENDING) {
Jolly Shah259afe52019-01-09 12:37:57 -0800318 return 1;
Venkatesh Yadav Abbarapu987fad32022-04-29 13:52:00 +0530319 } else {
Jolly Shah259afe52019-01-09 12:37:57 -0800320 return 0;
Venkatesh Yadav Abbarapu987fad32022-04-29 13:52:00 +0530321 }
Jolly Shah259afe52019-01-09 12:37:57 -0800322}
Venkatesh Yadav Abbarapu35a8be92019-04-04 14:23:32 +0530323
Venkatesh Yadav Abbarapubfa2a2a2021-02-19 01:40:14 -0700324#if IPI_CRC_CHECK
Venkatesh Yadav Abbarapu28b77592022-04-11 09:25:44 +0530325uint32_t calculate_crc(uint32_t payload[PAYLOAD_ARG_CNT], uint32_t bufsize)
Venkatesh Yadav Abbarapu35a8be92019-04-04 14:23:32 +0530326{
327 uint32_t crcinit = CRC_INIT_VALUE;
328 uint32_t order = CRC_ORDER;
329 uint32_t polynom = CRC_POLYNOM;
330 uint32_t i, j, c, bit, datain, crcmask, crchighbit;
331 uint32_t crc = crcinit;
332
333 crcmask = ((uint32_t)((1U << (order - 1U)) - 1U) << 1U) | 1U;
334 crchighbit = (uint32_t)(1U << (order - 1U));
335
336 for (i = 0U; i < bufsize; i++) {
337 datain = mmio_read_8((unsigned long)payload + i);
338 c = datain;
339 j = 0x80U;
340 while (j != 0U) {
341 bit = crc & crchighbit;
342 crc <<= 1U;
343 if (0U != (c & j))
344 bit ^= crchighbit;
345 if (bit != 0U)
346 crc ^= polynom;
347 j >>= 1U;
348 }
349 crc &= crcmask;
350 }
351 return crc;
352}
353#endif