xor.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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 "ddr3_init.h"
  12. #include "xor_regs.h"
  13. /* defines */
  14. #ifdef MV_DEBUG
  15. #define DB(x) x
  16. #else
  17. #define DB(x)
  18. #endif
  19. static u32 ui_xor_regs_ctrl_backup;
  20. static u32 ui_xor_regs_base_backup[MAX_CS];
  21. static u32 ui_xor_regs_mask_backup[MAX_CS];
  22. void mv_sys_xor_init(u32 num_of_cs, u32 cs_ena, u32 cs_size, u32 base_delta)
  23. {
  24. u32 reg, ui, base, cs_count;
  25. ui_xor_regs_ctrl_backup = reg_read(XOR_WINDOW_CTRL_REG(0, 0));
  26. for (ui = 0; ui < MAX_CS; ui++)
  27. ui_xor_regs_base_backup[ui] =
  28. reg_read(XOR_BASE_ADDR_REG(0, ui));
  29. for (ui = 0; ui < MAX_CS; ui++)
  30. ui_xor_regs_mask_backup[ui] =
  31. reg_read(XOR_SIZE_MASK_REG(0, ui));
  32. reg = 0;
  33. for (ui = 0; ui < (num_of_cs); ui++) {
  34. /* Enable Window x for each CS */
  35. reg |= (0x1 << (ui));
  36. /* Enable Window x for each CS */
  37. reg |= (0x3 << ((ui * 2) + 16));
  38. }
  39. reg_write(XOR_WINDOW_CTRL_REG(0, 0), reg);
  40. cs_count = 0;
  41. for (ui = 0; ui < num_of_cs; ui++) {
  42. if (cs_ena & (1 << ui)) {
  43. /*
  44. * window x - Base - 0x00000000,
  45. * Attribute 0x0e - DRAM
  46. */
  47. base = cs_size * ui + base_delta;
  48. switch (ui) {
  49. case 0:
  50. base |= 0xe00;
  51. break;
  52. case 1:
  53. base |= 0xd00;
  54. break;
  55. case 2:
  56. base |= 0xb00;
  57. break;
  58. case 3:
  59. base |= 0x700;
  60. break;
  61. }
  62. reg_write(XOR_BASE_ADDR_REG(0, cs_count), base);
  63. /* window x - Size */
  64. reg_write(XOR_SIZE_MASK_REG(0, cs_count), 0x7fff0000);
  65. cs_count++;
  66. }
  67. }
  68. mv_xor_hal_init(1);
  69. return;
  70. }
  71. void mv_sys_xor_finish(void)
  72. {
  73. u32 ui;
  74. reg_write(XOR_WINDOW_CTRL_REG(0, 0), ui_xor_regs_ctrl_backup);
  75. for (ui = 0; ui < MAX_CS; ui++)
  76. reg_write(XOR_BASE_ADDR_REG(0, ui),
  77. ui_xor_regs_base_backup[ui]);
  78. for (ui = 0; ui < MAX_CS; ui++)
  79. reg_write(XOR_SIZE_MASK_REG(0, ui),
  80. ui_xor_regs_mask_backup[ui]);
  81. reg_write(XOR_ADDR_OVRD_REG(0, 0), 0);
  82. }
  83. /*
  84. * mv_xor_hal_init - Initialize XOR engine
  85. *
  86. * DESCRIPTION:
  87. * This function initialize XOR unit.
  88. * INPUT:
  89. * None.
  90. *
  91. * OUTPUT:
  92. * None.
  93. *
  94. * RETURN:
  95. * MV_BAD_PARAM if parameters to function invalid, MV_OK otherwise.
  96. */
  97. void mv_xor_hal_init(u32 xor_chan_num)
  98. {
  99. u32 i;
  100. /* Abort any XOR activity & set default configuration */
  101. for (i = 0; i < xor_chan_num; i++) {
  102. mv_xor_command_set(i, MV_STOP);
  103. mv_xor_ctrl_set(i, (1 << XEXCR_REG_ACC_PROTECT_OFFS) |
  104. (4 << XEXCR_DST_BURST_LIMIT_OFFS) |
  105. (4 << XEXCR_SRC_BURST_LIMIT_OFFS));
  106. }
  107. }
  108. /*
  109. * mv_xor_ctrl_set - Set XOR channel control registers
  110. *
  111. * DESCRIPTION:
  112. *
  113. * INPUT:
  114. *
  115. * OUTPUT:
  116. * None.
  117. *
  118. * RETURN:
  119. * MV_BAD_PARAM if parameters to function invalid, MV_OK otherwise.
  120. * NOTE:
  121. * This function does not modify the Operation_mode field of control register.
  122. */
  123. int mv_xor_ctrl_set(u32 chan, u32 xor_ctrl)
  124. {
  125. u32 old_value;
  126. /* update the XOR Engine [0..1] Configuration Registers (XEx_c_r) */
  127. old_value = reg_read(XOR_CONFIG_REG(XOR_UNIT(chan), XOR_CHAN(chan))) &
  128. XEXCR_OPERATION_MODE_MASK;
  129. xor_ctrl &= ~XEXCR_OPERATION_MODE_MASK;
  130. xor_ctrl |= old_value;
  131. reg_write(XOR_CONFIG_REG(XOR_UNIT(chan), XOR_CHAN(chan)), xor_ctrl);
  132. return MV_OK;
  133. }
  134. int mv_xor_mem_init(u32 chan, u32 start_ptr, u32 block_size,
  135. u32 init_val_high, u32 init_val_low)
  136. {
  137. u32 temp;
  138. /* Parameter checking */
  139. if (chan >= MV_XOR_MAX_CHAN)
  140. return MV_BAD_PARAM;
  141. if (MV_ACTIVE == mv_xor_state_get(chan))
  142. return MV_BUSY;
  143. if ((block_size < XEXBSR_BLOCK_SIZE_MIN_VALUE) ||
  144. (block_size > XEXBSR_BLOCK_SIZE_MAX_VALUE))
  145. return MV_BAD_PARAM;
  146. /* set the operation mode to Memory Init */
  147. temp = reg_read(XOR_CONFIG_REG(XOR_UNIT(chan), XOR_CHAN(chan)));
  148. temp &= ~XEXCR_OPERATION_MODE_MASK;
  149. temp |= XEXCR_OPERATION_MODE_MEM_INIT;
  150. reg_write(XOR_CONFIG_REG(XOR_UNIT(chan), XOR_CHAN(chan)), temp);
  151. /*
  152. * update the start_ptr field in XOR Engine [0..1] Destination Pointer
  153. * Register
  154. */
  155. reg_write(XOR_DST_PTR_REG(XOR_UNIT(chan), XOR_CHAN(chan)), start_ptr);
  156. /*
  157. * update the Block_size field in the XOR Engine[0..1] Block Size
  158. * Registers
  159. */
  160. reg_write(XOR_BLOCK_SIZE_REG(XOR_UNIT(chan), XOR_CHAN(chan)),
  161. block_size);
  162. /*
  163. * update the field Init_val_l in the XOR Engine Initial Value Register
  164. * Low (XEIVRL)
  165. */
  166. reg_write(XOR_INIT_VAL_LOW_REG(XOR_UNIT(chan)), init_val_low);
  167. /*
  168. * update the field Init_val_h in the XOR Engine Initial Value Register
  169. * High (XEIVRH)
  170. */
  171. reg_write(XOR_INIT_VAL_HIGH_REG(XOR_UNIT(chan)), init_val_high);
  172. /* start transfer */
  173. reg_bit_set(XOR_ACTIVATION_REG(XOR_UNIT(chan), XOR_CHAN(chan)),
  174. XEXACTR_XESTART_MASK);
  175. return MV_OK;
  176. }
  177. /*
  178. * mv_xor_state_get - Get XOR channel state.
  179. *
  180. * DESCRIPTION:
  181. * XOR channel activity state can be active, idle, paused.
  182. * This function retrunes the channel activity state.
  183. *
  184. * INPUT:
  185. * chan - the channel number
  186. *
  187. * OUTPUT:
  188. * None.
  189. *
  190. * RETURN:
  191. * XOR_CHANNEL_IDLE - If the engine is idle.
  192. * XOR_CHANNEL_ACTIVE - If the engine is busy.
  193. * XOR_CHANNEL_PAUSED - If the engine is paused.
  194. * MV_UNDEFINED_STATE - If the engine state is undefind or there is no
  195. * such engine
  196. */
  197. enum mv_state mv_xor_state_get(u32 chan)
  198. {
  199. u32 state;
  200. /* Parameter checking */
  201. if (chan >= MV_XOR_MAX_CHAN) {
  202. DB(printf("%s: ERR. Invalid chan num %d\n", __func__, chan));
  203. return MV_UNDEFINED_STATE;
  204. }
  205. /* read the current state */
  206. state = reg_read(XOR_ACTIVATION_REG(XOR_UNIT(chan), XOR_CHAN(chan)));
  207. state &= XEXACTR_XESTATUS_MASK;
  208. /* return the state */
  209. switch (state) {
  210. case XEXACTR_XESTATUS_IDLE:
  211. return MV_IDLE;
  212. case XEXACTR_XESTATUS_ACTIVE:
  213. return MV_ACTIVE;
  214. case XEXACTR_XESTATUS_PAUSED:
  215. return MV_PAUSED;
  216. }
  217. return MV_UNDEFINED_STATE;
  218. }
  219. /*
  220. * mv_xor_command_set - Set command of XOR channel
  221. *
  222. * DESCRIPTION:
  223. * XOR channel can be started, idle, paused and restarted.
  224. * Paused can be set only if channel is active.
  225. * Start can be set only if channel is idle or paused.
  226. * Restart can be set only if channel is paused.
  227. * Stop can be set only if channel is active.
  228. *
  229. * INPUT:
  230. * chan - The channel number
  231. * command - The command type (start, stop, restart, pause)
  232. *
  233. * OUTPUT:
  234. * None.
  235. *
  236. * RETURN:
  237. * MV_OK on success , MV_BAD_PARAM on erroneous parameter, MV_ERROR on
  238. * undefind XOR engine mode
  239. */
  240. int mv_xor_command_set(u32 chan, enum mv_command command)
  241. {
  242. enum mv_state state;
  243. /* Parameter checking */
  244. if (chan >= MV_XOR_MAX_CHAN) {
  245. DB(printf("%s: ERR. Invalid chan num %d\n", __func__, chan));
  246. return MV_BAD_PARAM;
  247. }
  248. /* get the current state */
  249. state = mv_xor_state_get(chan);
  250. if ((command == MV_START) && (state == MV_IDLE)) {
  251. /* command is start and current state is idle */
  252. reg_bit_set(XOR_ACTIVATION_REG
  253. (XOR_UNIT(chan), XOR_CHAN(chan)),
  254. XEXACTR_XESTART_MASK);
  255. return MV_OK;
  256. } else if ((command == MV_STOP) && (state == MV_ACTIVE)) {
  257. /* command is stop and current state is active */
  258. reg_bit_set(XOR_ACTIVATION_REG
  259. (XOR_UNIT(chan), XOR_CHAN(chan)),
  260. XEXACTR_XESTOP_MASK);
  261. return MV_OK;
  262. } else if (((enum mv_state)command == MV_PAUSED) &&
  263. (state == MV_ACTIVE)) {
  264. /* command is paused and current state is active */
  265. reg_bit_set(XOR_ACTIVATION_REG
  266. (XOR_UNIT(chan), XOR_CHAN(chan)),
  267. XEXACTR_XEPAUSE_MASK);
  268. return MV_OK;
  269. } else if ((command == MV_RESTART) && (state == MV_PAUSED)) {
  270. /* command is restart and current state is paused */
  271. reg_bit_set(XOR_ACTIVATION_REG
  272. (XOR_UNIT(chan), XOR_CHAN(chan)),
  273. XEXACTR_XERESTART_MASK);
  274. return MV_OK;
  275. } else if ((command == MV_STOP) && (state == MV_IDLE)) {
  276. /* command is stop and current state is active */
  277. return MV_OK;
  278. }
  279. /* illegal command */
  280. DB(printf("%s: ERR. Illegal command\n", __func__));
  281. return MV_BAD_PARAM;
  282. }
  283. void ddr3_new_tip_ecc_scrub(void)
  284. {
  285. u32 cs_c, max_cs;
  286. u32 cs_ena = 0;
  287. printf("DDR3 Training Sequence - Start scrubbing\n");
  288. max_cs = hws_ddr3_tip_max_cs_get();
  289. for (cs_c = 0; cs_c < max_cs; cs_c++)
  290. cs_ena |= 1 << cs_c;
  291. mv_sys_xor_init(max_cs, cs_ena, 0x80000000, 0);
  292. mv_xor_mem_init(0, 0x00000000, 0x80000000, 0xdeadbeef, 0xdeadbeef);
  293. /* wait for previous transfer completion */
  294. while (mv_xor_state_get(0) != MV_IDLE)
  295. ;
  296. mv_xor_mem_init(0, 0x80000000, 0x40000000, 0xdeadbeef, 0xdeadbeef);
  297. /* wait for previous transfer completion */
  298. while (mv_xor_state_get(0) != MV_IDLE)
  299. ;
  300. /* Return XOR State */
  301. mv_sys_xor_finish();
  302. printf("DDR3 Training Sequence - End scrubbing\n");
  303. }