xor.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) Marvell International Ltd. and its affiliates
  4. */
  5. #include <common.h>
  6. #include <i2c.h>
  7. #include <spl.h>
  8. #include <asm/io.h>
  9. #include <asm/arch/cpu.h>
  10. #include <asm/arch/soc.h>
  11. #include "xor.h"
  12. #include "xor_regs.h"
  13. static u32 xor_regs_ctrl_backup;
  14. static u32 xor_regs_base_backup[MAX_CS];
  15. static u32 xor_regs_mask_backup[MAX_CS];
  16. static int mv_xor_cmd_set(u32 chan, int command);
  17. static int mv_xor_ctrl_set(u32 chan, u32 xor_ctrl);
  18. void mv_sys_xor_init(MV_DRAM_INFO *dram_info)
  19. {
  20. u32 reg, ui, base, cs_count;
  21. xor_regs_ctrl_backup = reg_read(XOR_WINDOW_CTRL_REG(0, 0));
  22. for (ui = 0; ui < MAX_CS; ui++)
  23. xor_regs_base_backup[ui] = reg_read(XOR_BASE_ADDR_REG(0, ui));
  24. for (ui = 0; ui < MAX_CS; ui++)
  25. xor_regs_mask_backup[ui] = reg_read(XOR_SIZE_MASK_REG(0, ui));
  26. reg = 0;
  27. for (ui = 0; ui < (dram_info->num_cs + 1); ui++) {
  28. /* Enable Window x for each CS */
  29. reg |= (0x1 << (ui));
  30. /* Enable Window x for each CS */
  31. reg |= (0x3 << ((ui * 2) + 16));
  32. }
  33. reg_write(XOR_WINDOW_CTRL_REG(0, 0), reg);
  34. /* Last window - Base - 0x40000000, Attribute 0x1E - SRAM */
  35. base = (SRAM_BASE & 0xFFFF0000) | 0x1E00;
  36. reg_write(XOR_BASE_ADDR_REG(0, dram_info->num_cs), base);
  37. /* Last window - Size - 64 MB */
  38. reg_write(XOR_SIZE_MASK_REG(0, dram_info->num_cs), 0x03FF0000);
  39. cs_count = 0;
  40. for (ui = 0; ui < MAX_CS; ui++) {
  41. if (dram_info->cs_ena & (1 << ui)) {
  42. /*
  43. * Window x - Base - 0x00000000, Attribute 0x0E - DRAM
  44. */
  45. base = 0;
  46. switch (ui) {
  47. case 0:
  48. base |= 0xE00;
  49. break;
  50. case 1:
  51. base |= 0xD00;
  52. break;
  53. case 2:
  54. base |= 0xB00;
  55. break;
  56. case 3:
  57. base |= 0x700;
  58. break;
  59. }
  60. reg_write(XOR_BASE_ADDR_REG(0, cs_count), base);
  61. /* Window x - Size - 256 MB */
  62. reg_write(XOR_SIZE_MASK_REG(0, cs_count), 0x0FFF0000);
  63. cs_count++;
  64. }
  65. }
  66. mv_xor_hal_init(1);
  67. return;
  68. }
  69. void mv_sys_xor_finish(void)
  70. {
  71. u32 ui;
  72. reg_write(XOR_WINDOW_CTRL_REG(0, 0), xor_regs_ctrl_backup);
  73. for (ui = 0; ui < MAX_CS; ui++)
  74. reg_write(XOR_BASE_ADDR_REG(0, ui), xor_regs_base_backup[ui]);
  75. for (ui = 0; ui < MAX_CS; ui++)
  76. reg_write(XOR_SIZE_MASK_REG(0, ui), xor_regs_mask_backup[ui]);
  77. reg_write(XOR_ADDR_OVRD_REG(0, 0), 0);
  78. }
  79. /*
  80. * mv_xor_hal_init - Initialize XOR engine
  81. *
  82. * DESCRIPTION:
  83. * This function initialize XOR unit.
  84. * INPUT:
  85. * None.
  86. *
  87. * OUTPUT:
  88. * None.
  89. *
  90. * RETURN:
  91. * MV_BAD_PARAM if parameters to function invalid, MV_OK otherwise.
  92. */
  93. void mv_xor_hal_init(u32 chan_num)
  94. {
  95. u32 i;
  96. /* Abort any XOR activity & set default configuration */
  97. for (i = 0; i < chan_num; i++) {
  98. mv_xor_cmd_set(i, MV_STOP);
  99. mv_xor_ctrl_set(i, (1 << XEXCR_REG_ACC_PROTECT_OFFS) |
  100. (4 << XEXCR_DST_BURST_LIMIT_OFFS) |
  101. (4 << XEXCR_SRC_BURST_LIMIT_OFFS));
  102. }
  103. }
  104. /*
  105. * mv_xor_ctrl_set - Set XOR channel control registers
  106. *
  107. * DESCRIPTION:
  108. *
  109. * INPUT:
  110. *
  111. * OUTPUT:
  112. * None.
  113. *
  114. * RETURN:
  115. * MV_BAD_PARAM if parameters to function invalid, MV_OK otherwise.
  116. * NOTE:
  117. * This function does not modify the OperationMode field of control register.
  118. *
  119. */
  120. static int mv_xor_ctrl_set(u32 chan, u32 xor_ctrl)
  121. {
  122. u32 val;
  123. /* Update the XOR Engine [0..1] Configuration Registers (XExCR) */
  124. val = reg_read(XOR_CONFIG_REG(XOR_UNIT(chan), XOR_CHAN(chan)))
  125. & XEXCR_OPERATION_MODE_MASK;
  126. xor_ctrl &= ~XEXCR_OPERATION_MODE_MASK;
  127. xor_ctrl |= val;
  128. reg_write(XOR_CONFIG_REG(XOR_UNIT(chan), XOR_CHAN(chan)), xor_ctrl);
  129. return MV_OK;
  130. }
  131. int mv_xor_mem_init(u32 chan, u32 start_ptr, u32 block_size, u32 init_val_high,
  132. u32 init_val_low)
  133. {
  134. u32 tmp;
  135. /* Parameter checking */
  136. if (chan >= MV_XOR_MAX_CHAN)
  137. return MV_BAD_PARAM;
  138. if (MV_ACTIVE == mv_xor_state_get(chan))
  139. return MV_BUSY;
  140. if ((block_size < XEXBSR_BLOCK_SIZE_MIN_VALUE) ||
  141. (block_size > XEXBSR_BLOCK_SIZE_MAX_VALUE))
  142. return MV_BAD_PARAM;
  143. /* Set the operation mode to Memory Init */
  144. tmp = reg_read(XOR_CONFIG_REG(XOR_UNIT(chan), XOR_CHAN(chan)));
  145. tmp &= ~XEXCR_OPERATION_MODE_MASK;
  146. tmp |= XEXCR_OPERATION_MODE_MEM_INIT;
  147. reg_write(XOR_CONFIG_REG(XOR_UNIT(chan), XOR_CHAN(chan)), tmp);
  148. /*
  149. * Update the start_ptr field in XOR Engine [0..1] Destination Pointer
  150. * Register (XExDPR0)
  151. */
  152. reg_write(XOR_DST_PTR_REG(XOR_UNIT(chan), XOR_CHAN(chan)), start_ptr);
  153. /*
  154. * Update the BlockSize field in the XOR Engine[0..1] Block Size
  155. * Registers (XExBSR)
  156. */
  157. reg_write(XOR_BLOCK_SIZE_REG(XOR_UNIT(chan), XOR_CHAN(chan)),
  158. block_size);
  159. /*
  160. * Update the field InitValL in the XOR Engine Initial Value Register
  161. * Low (XEIVRL)
  162. */
  163. reg_write(XOR_INIT_VAL_LOW_REG(XOR_UNIT(chan)), init_val_low);
  164. /*
  165. * Update the field InitValH in the XOR Engine Initial Value Register
  166. * High (XEIVRH)
  167. */
  168. reg_write(XOR_INIT_VAL_HIGH_REG(XOR_UNIT(chan)), init_val_high);
  169. /* Start transfer */
  170. reg_bit_set(XOR_ACTIVATION_REG(XOR_UNIT(chan), XOR_CHAN(chan)),
  171. XEXACTR_XESTART_MASK);
  172. return MV_OK;
  173. }
  174. /*
  175. * mv_xor_transfer - Transfer data from source to destination on one of
  176. * three modes (XOR,CRC32,DMA)
  177. *
  178. * DESCRIPTION:
  179. * This function initiates XOR channel, according to function parameters,
  180. * in order to perform XOR or CRC32 or DMA transaction.
  181. * To gain maximum performance the user is asked to keep the following
  182. * restrictions:
  183. * 1) Selected engine is available (not busy).
  184. * 1) This module does not take into consideration CPU MMU issues.
  185. * In order for the XOR engine to access the appropreate source
  186. * and destination, address parameters must be given in system
  187. * physical mode.
  188. * 2) This API does not take care of cache coherency issues. The source,
  189. * destination and in case of chain the descriptor list are assumed
  190. * to be cache coherent.
  191. * 4) Parameters validity. For example, does size parameter exceeds
  192. * maximum byte count of descriptor mode (16M or 64K).
  193. *
  194. * INPUT:
  195. * chan - XOR channel number. See MV_XOR_CHANNEL enumerator.
  196. * xor_type - One of three: XOR, CRC32 and DMA operations.
  197. * xor_chain_ptr - address of chain pointer
  198. *
  199. * OUTPUT:
  200. * None.
  201. *
  202. * RETURS:
  203. * MV_BAD_PARAM if parameters to function invalid, MV_OK otherwise.
  204. *
  205. */
  206. int mv_xor_transfer(u32 chan, int xor_type, u32 xor_chain_ptr)
  207. {
  208. u32 tmp;
  209. /* Parameter checking */
  210. if (chan >= MV_XOR_MAX_CHAN) {
  211. debug("%s: ERR. Invalid chan num %d\n", __func__, chan);
  212. return MV_BAD_PARAM;
  213. }
  214. if (MV_ACTIVE == mv_xor_state_get(chan)) {
  215. debug("%s: ERR. Channel is already active\n", __func__);
  216. return MV_BUSY;
  217. }
  218. if (0x0 == xor_chain_ptr) {
  219. debug("%s: ERR. xor_chain_ptr is NULL pointer\n", __func__);
  220. return MV_BAD_PARAM;
  221. }
  222. /* Read configuration register and mask the operation mode field */
  223. tmp = reg_read(XOR_CONFIG_REG(XOR_UNIT(chan), XOR_CHAN(chan)));
  224. tmp &= ~XEXCR_OPERATION_MODE_MASK;
  225. switch (xor_type) {
  226. case MV_XOR:
  227. if (0 != (xor_chain_ptr & XEXDPR_DST_PTR_XOR_MASK)) {
  228. debug("%s: ERR. Invalid chain pointer (bits [5:0] must be cleared)\n",
  229. __func__);
  230. return MV_BAD_PARAM;
  231. }
  232. /* Set the operation mode to XOR */
  233. tmp |= XEXCR_OPERATION_MODE_XOR;
  234. break;
  235. case MV_DMA:
  236. if (0 != (xor_chain_ptr & XEXDPR_DST_PTR_DMA_MASK)) {
  237. debug("%s: ERR. Invalid chain pointer (bits [4:0] must be cleared)\n",
  238. __func__);
  239. return MV_BAD_PARAM;
  240. }
  241. /* Set the operation mode to DMA */
  242. tmp |= XEXCR_OPERATION_MODE_DMA;
  243. break;
  244. case MV_CRC32:
  245. if (0 != (xor_chain_ptr & XEXDPR_DST_PTR_CRC_MASK)) {
  246. debug("%s: ERR. Invalid chain pointer (bits [4:0] must be cleared)\n",
  247. __func__);
  248. return MV_BAD_PARAM;
  249. }
  250. /* Set the operation mode to CRC32 */
  251. tmp |= XEXCR_OPERATION_MODE_CRC;
  252. break;
  253. default:
  254. return MV_BAD_PARAM;
  255. }
  256. /* Write the operation mode to the register */
  257. reg_write(XOR_CONFIG_REG(XOR_UNIT(chan), XOR_CHAN(chan)), tmp);
  258. /*
  259. * Update the NextDescPtr field in the XOR Engine [0..1] Next Descriptor
  260. * Pointer Register (XExNDPR)
  261. */
  262. reg_write(XOR_NEXT_DESC_PTR_REG(XOR_UNIT(chan), XOR_CHAN(chan)),
  263. xor_chain_ptr);
  264. /* Start transfer */
  265. reg_bit_set(XOR_ACTIVATION_REG(XOR_UNIT(chan), XOR_CHAN(chan)),
  266. XEXACTR_XESTART_MASK);
  267. return MV_OK;
  268. }
  269. /*
  270. * mv_xor_state_get - Get XOR channel state.
  271. *
  272. * DESCRIPTION:
  273. * XOR channel activity state can be active, idle, paused.
  274. * This function retrunes the channel activity state.
  275. *
  276. * INPUT:
  277. * chan - the channel number
  278. *
  279. * OUTPUT:
  280. * None.
  281. *
  282. * RETURN:
  283. * XOR_CHANNEL_IDLE - If the engine is idle.
  284. * XOR_CHANNEL_ACTIVE - If the engine is busy.
  285. * XOR_CHANNEL_PAUSED - If the engine is paused.
  286. * MV_UNDEFINED_STATE - If the engine state is undefind or there is no
  287. * such engine
  288. *
  289. */
  290. int mv_xor_state_get(u32 chan)
  291. {
  292. u32 state;
  293. /* Parameter checking */
  294. if (chan >= MV_XOR_MAX_CHAN) {
  295. debug("%s: ERR. Invalid chan num %d\n", __func__, chan);
  296. return MV_UNDEFINED_STATE;
  297. }
  298. /* Read the current state */
  299. state = reg_read(XOR_ACTIVATION_REG(XOR_UNIT(chan), XOR_CHAN(chan)));
  300. state &= XEXACTR_XESTATUS_MASK;
  301. /* Return the state */
  302. switch (state) {
  303. case XEXACTR_XESTATUS_IDLE:
  304. return MV_IDLE;
  305. case XEXACTR_XESTATUS_ACTIVE:
  306. return MV_ACTIVE;
  307. case XEXACTR_XESTATUS_PAUSED:
  308. return MV_PAUSED;
  309. }
  310. return MV_UNDEFINED_STATE;
  311. }
  312. /*
  313. * mv_xor_cmd_set - Set command of XOR channel
  314. *
  315. * DESCRIPTION:
  316. * XOR channel can be started, idle, paused and restarted.
  317. * Paused can be set only if channel is active.
  318. * Start can be set only if channel is idle or paused.
  319. * Restart can be set only if channel is paused.
  320. * Stop can be set only if channel is active.
  321. *
  322. * INPUT:
  323. * chan - The channel number
  324. * command - The command type (start, stop, restart, pause)
  325. *
  326. * OUTPUT:
  327. * None.
  328. *
  329. * RETURN:
  330. * MV_OK on success , MV_BAD_PARAM on erroneous parameter, MV_ERROR on
  331. * undefind XOR engine mode
  332. *
  333. */
  334. static int mv_xor_cmd_set(u32 chan, int command)
  335. {
  336. int state;
  337. /* Parameter checking */
  338. if (chan >= MV_XOR_MAX_CHAN) {
  339. debug("%s: ERR. Invalid chan num %d\n", __func__, chan);
  340. return MV_BAD_PARAM;
  341. }
  342. /* Get the current state */
  343. state = mv_xor_state_get(chan);
  344. /* Command is start and current state is idle */
  345. if ((command == MV_START) && (state == MV_IDLE)) {
  346. reg_bit_set(XOR_ACTIVATION_REG(XOR_UNIT(chan), XOR_CHAN(chan)),
  347. XEXACTR_XESTART_MASK);
  348. return MV_OK;
  349. }
  350. /* Command is stop and current state is active */
  351. else if ((command == MV_STOP) && (state == MV_ACTIVE)) {
  352. reg_bit_set(XOR_ACTIVATION_REG(XOR_UNIT(chan), XOR_CHAN(chan)),
  353. XEXACTR_XESTOP_MASK);
  354. return MV_OK;
  355. }
  356. /* Command is paused and current state is active */
  357. else if ((command == MV_PAUSED) && (state == MV_ACTIVE)) {
  358. reg_bit_set(XOR_ACTIVATION_REG(XOR_UNIT(chan), XOR_CHAN(chan)),
  359. XEXACTR_XEPAUSE_MASK);
  360. return MV_OK;
  361. }
  362. /* Command is restart and current state is paused */
  363. else if ((command == MV_RESTART) && (state == MV_PAUSED)) {
  364. reg_bit_set(XOR_ACTIVATION_REG(XOR_UNIT(chan), XOR_CHAN(chan)),
  365. XEXACTR_XERESTART_MASK);
  366. return MV_OK;
  367. }
  368. /* Command is stop and current state is active */
  369. else if ((command == MV_STOP) && (state == MV_IDLE))
  370. return MV_OK;
  371. /* Illegal command */
  372. debug("%s: ERR. Illegal command\n", __func__);
  373. return MV_BAD_PARAM;
  374. }