1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) Marvell International Ltd. and its affiliates
11 #include <asm/arch/cpu.h>
12 #include <asm/arch/soc.h>
17 static u32 xor_regs_ctrl_backup;
18 static u32 xor_regs_base_backup[MAX_CS];
19 static u32 xor_regs_mask_backup[MAX_CS];
21 static int mv_xor_cmd_set(u32 chan, int command);
22 static int mv_xor_ctrl_set(u32 chan, u32 xor_ctrl);
24 void mv_sys_xor_init(MV_DRAM_INFO *dram_info)
26 u32 reg, ui, base, cs_count;
28 xor_regs_ctrl_backup = reg_read(XOR_WINDOW_CTRL_REG(0, 0));
29 for (ui = 0; ui < MAX_CS; ui++)
30 xor_regs_base_backup[ui] = reg_read(XOR_BASE_ADDR_REG(0, ui));
31 for (ui = 0; ui < MAX_CS; ui++)
32 xor_regs_mask_backup[ui] = reg_read(XOR_SIZE_MASK_REG(0, ui));
35 for (ui = 0; ui < (dram_info->num_cs + 1); ui++) {
36 /* Enable Window x for each CS */
38 /* Enable Window x for each CS */
39 reg |= (0x3 << ((ui * 2) + 16));
42 reg_write(XOR_WINDOW_CTRL_REG(0, 0), reg);
44 /* Last window - Base - 0x40000000, Attribute 0x1E - SRAM */
45 base = (SRAM_BASE & 0xFFFF0000) | 0x1E00;
46 reg_write(XOR_BASE_ADDR_REG(0, dram_info->num_cs), base);
47 /* Last window - Size - 64 MB */
48 reg_write(XOR_SIZE_MASK_REG(0, dram_info->num_cs), 0x03FF0000);
51 for (ui = 0; ui < MAX_CS; ui++) {
52 if (dram_info->cs_ena & (1 << ui)) {
54 * Window x - Base - 0x00000000, Attribute 0x0E - DRAM
72 reg_write(XOR_BASE_ADDR_REG(0, cs_count), base);
74 /* Window x - Size - 256 MB */
75 reg_write(XOR_SIZE_MASK_REG(0, cs_count), 0x0FFF0000);
85 void mv_sys_xor_finish(void)
89 reg_write(XOR_WINDOW_CTRL_REG(0, 0), xor_regs_ctrl_backup);
90 for (ui = 0; ui < MAX_CS; ui++)
91 reg_write(XOR_BASE_ADDR_REG(0, ui), xor_regs_base_backup[ui]);
92 for (ui = 0; ui < MAX_CS; ui++)
93 reg_write(XOR_SIZE_MASK_REG(0, ui), xor_regs_mask_backup[ui]);
95 reg_write(XOR_ADDR_OVRD_REG(0, 0), 0);
99 * mv_xor_hal_init - Initialize XOR engine
102 * This function initialize XOR unit.
110 * MV_BAD_PARAM if parameters to function invalid, MV_OK otherwise.
112 void mv_xor_hal_init(u32 chan_num)
116 /* Abort any XOR activity & set default configuration */
117 for (i = 0; i < chan_num; i++) {
118 mv_xor_cmd_set(i, MV_STOP);
119 mv_xor_ctrl_set(i, (1 << XEXCR_REG_ACC_PROTECT_OFFS) |
120 (4 << XEXCR_DST_BURST_LIMIT_OFFS) |
121 (4 << XEXCR_SRC_BURST_LIMIT_OFFS));
126 * mv_xor_ctrl_set - Set XOR channel control registers
136 * MV_BAD_PARAM if parameters to function invalid, MV_OK otherwise.
138 * This function does not modify the OperationMode field of control register.
141 static int mv_xor_ctrl_set(u32 chan, u32 xor_ctrl)
145 /* Update the XOR Engine [0..1] Configuration Registers (XExCR) */
146 val = reg_read(XOR_CONFIG_REG(XOR_UNIT(chan), XOR_CHAN(chan)))
147 & XEXCR_OPERATION_MODE_MASK;
148 xor_ctrl &= ~XEXCR_OPERATION_MODE_MASK;
150 reg_write(XOR_CONFIG_REG(XOR_UNIT(chan), XOR_CHAN(chan)), xor_ctrl);
155 int mv_xor_mem_init(u32 chan, u32 start_ptr, u32 block_size, u32 init_val_high,
160 /* Parameter checking */
161 if (chan >= MV_XOR_MAX_CHAN)
164 if (MV_ACTIVE == mv_xor_state_get(chan))
167 if ((block_size < XEXBSR_BLOCK_SIZE_MIN_VALUE) ||
168 (block_size > XEXBSR_BLOCK_SIZE_MAX_VALUE))
171 /* Set the operation mode to Memory Init */
172 tmp = reg_read(XOR_CONFIG_REG(XOR_UNIT(chan), XOR_CHAN(chan)));
173 tmp &= ~XEXCR_OPERATION_MODE_MASK;
174 tmp |= XEXCR_OPERATION_MODE_MEM_INIT;
175 reg_write(XOR_CONFIG_REG(XOR_UNIT(chan), XOR_CHAN(chan)), tmp);
178 * Update the start_ptr field in XOR Engine [0..1] Destination Pointer
181 reg_write(XOR_DST_PTR_REG(XOR_UNIT(chan), XOR_CHAN(chan)), start_ptr);
184 * Update the BlockSize field in the XOR Engine[0..1] Block Size
187 reg_write(XOR_BLOCK_SIZE_REG(XOR_UNIT(chan), XOR_CHAN(chan)),
191 * Update the field InitValL in the XOR Engine Initial Value Register
194 reg_write(XOR_INIT_VAL_LOW_REG(XOR_UNIT(chan)), init_val_low);
197 * Update the field InitValH in the XOR Engine Initial Value Register
200 reg_write(XOR_INIT_VAL_HIGH_REG(XOR_UNIT(chan)), init_val_high);
203 reg_bit_set(XOR_ACTIVATION_REG(XOR_UNIT(chan), XOR_CHAN(chan)),
204 XEXACTR_XESTART_MASK);
210 * mv_xor_transfer - Transfer data from source to destination on one of
211 * three modes (XOR,CRC32,DMA)
214 * This function initiates XOR channel, according to function parameters,
215 * in order to perform XOR or CRC32 or DMA transaction.
216 * To gain maximum performance the user is asked to keep the following
218 * 1) Selected engine is available (not busy).
219 * 1) This module does not take into consideration CPU MMU issues.
220 * In order for the XOR engine to access the appropreate source
221 * and destination, address parameters must be given in system
223 * 2) This API does not take care of cache coherency issues. The source,
224 * destination and in case of chain the descriptor list are assumed
225 * to be cache coherent.
226 * 4) Parameters validity. For example, does size parameter exceeds
227 * maximum byte count of descriptor mode (16M or 64K).
230 * chan - XOR channel number. See MV_XOR_CHANNEL enumerator.
231 * xor_type - One of three: XOR, CRC32 and DMA operations.
232 * xor_chain_ptr - address of chain pointer
238 * MV_BAD_PARAM if parameters to function invalid, MV_OK otherwise.
241 int mv_xor_transfer(u32 chan, int xor_type, u32 xor_chain_ptr)
245 /* Parameter checking */
246 if (chan >= MV_XOR_MAX_CHAN) {
247 debug("%s: ERR. Invalid chan num %d\n", __func__, chan);
251 if (MV_ACTIVE == mv_xor_state_get(chan)) {
252 debug("%s: ERR. Channel is already active\n", __func__);
256 if (0x0 == xor_chain_ptr) {
257 debug("%s: ERR. xor_chain_ptr is NULL pointer\n", __func__);
261 /* Read configuration register and mask the operation mode field */
262 tmp = reg_read(XOR_CONFIG_REG(XOR_UNIT(chan), XOR_CHAN(chan)));
263 tmp &= ~XEXCR_OPERATION_MODE_MASK;
267 if (0 != (xor_chain_ptr & XEXDPR_DST_PTR_XOR_MASK)) {
268 debug("%s: ERR. Invalid chain pointer (bits [5:0] must be cleared)\n",
273 /* Set the operation mode to XOR */
274 tmp |= XEXCR_OPERATION_MODE_XOR;
278 if (0 != (xor_chain_ptr & XEXDPR_DST_PTR_DMA_MASK)) {
279 debug("%s: ERR. Invalid chain pointer (bits [4:0] must be cleared)\n",
284 /* Set the operation mode to DMA */
285 tmp |= XEXCR_OPERATION_MODE_DMA;
289 if (0 != (xor_chain_ptr & XEXDPR_DST_PTR_CRC_MASK)) {
290 debug("%s: ERR. Invalid chain pointer (bits [4:0] must be cleared)\n",
295 /* Set the operation mode to CRC32 */
296 tmp |= XEXCR_OPERATION_MODE_CRC;
303 /* Write the operation mode to the register */
304 reg_write(XOR_CONFIG_REG(XOR_UNIT(chan), XOR_CHAN(chan)), tmp);
307 * Update the NextDescPtr field in the XOR Engine [0..1] Next Descriptor
308 * Pointer Register (XExNDPR)
310 reg_write(XOR_NEXT_DESC_PTR_REG(XOR_UNIT(chan), XOR_CHAN(chan)),
314 reg_bit_set(XOR_ACTIVATION_REG(XOR_UNIT(chan), XOR_CHAN(chan)),
315 XEXACTR_XESTART_MASK);
321 * mv_xor_state_get - Get XOR channel state.
324 * XOR channel activity state can be active, idle, paused.
325 * This function retrunes the channel activity state.
328 * chan - the channel number
334 * XOR_CHANNEL_IDLE - If the engine is idle.
335 * XOR_CHANNEL_ACTIVE - If the engine is busy.
336 * XOR_CHANNEL_PAUSED - If the engine is paused.
337 * MV_UNDEFINED_STATE - If the engine state is undefind or there is no
341 int mv_xor_state_get(u32 chan)
345 /* Parameter checking */
346 if (chan >= MV_XOR_MAX_CHAN) {
347 debug("%s: ERR. Invalid chan num %d\n", __func__, chan);
348 return MV_UNDEFINED_STATE;
351 /* Read the current state */
352 state = reg_read(XOR_ACTIVATION_REG(XOR_UNIT(chan), XOR_CHAN(chan)));
353 state &= XEXACTR_XESTATUS_MASK;
355 /* Return the state */
357 case XEXACTR_XESTATUS_IDLE:
359 case XEXACTR_XESTATUS_ACTIVE:
361 case XEXACTR_XESTATUS_PAUSED:
365 return MV_UNDEFINED_STATE;
369 * mv_xor_cmd_set - Set command of XOR channel
372 * XOR channel can be started, idle, paused and restarted.
373 * Paused can be set only if channel is active.
374 * Start can be set only if channel is idle or paused.
375 * Restart can be set only if channel is paused.
376 * Stop can be set only if channel is active.
379 * chan - The channel number
380 * command - The command type (start, stop, restart, pause)
386 * MV_OK on success , MV_BAD_PARAM on erroneous parameter, MV_ERROR on
387 * undefind XOR engine mode
390 static int mv_xor_cmd_set(u32 chan, int command)
394 /* Parameter checking */
395 if (chan >= MV_XOR_MAX_CHAN) {
396 debug("%s: ERR. Invalid chan num %d\n", __func__, chan);
400 /* Get the current state */
401 state = mv_xor_state_get(chan);
403 /* Command is start and current state is idle */
404 if ((command == MV_START) && (state == MV_IDLE)) {
405 reg_bit_set(XOR_ACTIVATION_REG(XOR_UNIT(chan), XOR_CHAN(chan)),
406 XEXACTR_XESTART_MASK);
409 /* Command is stop and current state is active */
410 else if ((command == MV_STOP) && (state == MV_ACTIVE)) {
411 reg_bit_set(XOR_ACTIVATION_REG(XOR_UNIT(chan), XOR_CHAN(chan)),
412 XEXACTR_XESTOP_MASK);
415 /* Command is paused and current state is active */
416 else if ((command == MV_PAUSED) && (state == MV_ACTIVE)) {
417 reg_bit_set(XOR_ACTIVATION_REG(XOR_UNIT(chan), XOR_CHAN(chan)),
418 XEXACTR_XEPAUSE_MASK);
421 /* Command is restart and current state is paused */
422 else if ((command == MV_RESTART) && (state == MV_PAUSED)) {
423 reg_bit_set(XOR_ACTIVATION_REG(XOR_UNIT(chan), XOR_CHAN(chan)),
424 XEXACTR_XERESTART_MASK);
427 /* Command is stop and current state is active */
428 else if ((command == MV_STOP) && (state == MV_IDLE))
431 /* Illegal command */
432 debug("%s: ERR. Illegal command\n", __func__);