ecc.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. * Copyright (c) 2008 Nuovation System Designs, LLC
  3. * Grant Erickson <gerickson@nuovations.com>
  4. *
  5. * (C) Copyright 2005-2009
  6. * Stefan Roese, DENX Software Engineering, sr@denx.de.
  7. *
  8. * (C) Copyright 2002
  9. * Jun Gu, Artesyn Technology, jung@artesyncp.com
  10. *
  11. * (C) Copyright 2001
  12. * Bill Hunter, Wave 7 Optics, williamhunter@attbi.com
  13. *
  14. * SPDX-License-Identifier: GPL-2.0+
  15. *
  16. * Description:
  17. * This file implements generic DRAM ECC initialization for
  18. * PowerPC processors using a SDRAM DDR/DDR2 controller,
  19. * including the 405EX(r), 440GP/GX/EP/GR, 440SP(E), and
  20. * 460EX/GT.
  21. */
  22. #include <common.h>
  23. #include <asm/ppc4xx.h>
  24. #include <ppc_asm.tmpl>
  25. #include <ppc_defs.h>
  26. #include <asm/processor.h>
  27. #include <asm/io.h>
  28. #include <asm/mmu.h>
  29. #include <asm/cache.h>
  30. #include "ecc.h"
  31. #if defined(CONFIG_SDRAM_PPC4xx_IBM_DDR) || \
  32. defined(CONFIG_SDRAM_PPC4xx_IBM_DDR2)
  33. #if defined(CONFIG_DDR_ECC) || defined(CONFIG_SDRAM_ECC)
  34. #if defined(CONFIG_405EX)
  35. /*
  36. * Currently only 405EX uses 16bit data bus width as an alternative
  37. * option to 32bit data width (SDRAM0_MCOPT1_WDTH)
  38. */
  39. #define SDRAM_DATA_ALT_WIDTH 2
  40. #else
  41. #define SDRAM_DATA_ALT_WIDTH 8
  42. #endif
  43. static void wait_ddr_idle(void)
  44. {
  45. u32 val;
  46. do {
  47. mfsdram(SDRAM_MCSTAT, val);
  48. } while ((val & SDRAM_MCSTAT_IDLE_MASK) == SDRAM_MCSTAT_IDLE_NOT);
  49. }
  50. static void program_ecc_addr(unsigned long start_address,
  51. unsigned long num_bytes,
  52. unsigned long tlb_word2_i_value)
  53. {
  54. unsigned long current_address;
  55. unsigned long end_address;
  56. unsigned long address_increment;
  57. unsigned long mcopt1;
  58. char str[] = "ECC generation -";
  59. char slash[] = "\\|/-\\|/-";
  60. int loop = 0;
  61. int loopi = 0;
  62. current_address = start_address;
  63. mfsdram(SDRAM_MCOPT1, mcopt1);
  64. if ((mcopt1 & SDRAM_MCOPT1_MCHK_MASK) != SDRAM_MCOPT1_MCHK_NON) {
  65. mtsdram(SDRAM_MCOPT1,
  66. (mcopt1 & ~SDRAM_MCOPT1_MCHK_MASK) | SDRAM_MCOPT1_MCHK_GEN);
  67. sync();
  68. eieio();
  69. wait_ddr_idle();
  70. puts(str);
  71. #ifdef CONFIG_440
  72. if (tlb_word2_i_value == TLB_WORD2_I_ENABLE) {
  73. #endif
  74. /* ECC bit set method for non-cached memory */
  75. if ((mcopt1 & SDRAM_MCOPT1_DMWD_MASK) == SDRAM_MCOPT1_DMWD_32)
  76. address_increment = 4;
  77. else
  78. address_increment = SDRAM_DATA_ALT_WIDTH;
  79. end_address = current_address + num_bytes;
  80. while (current_address < end_address) {
  81. *((unsigned long *)current_address) = 0;
  82. current_address += address_increment;
  83. if ((loop++ % (2 << 20)) == 0) {
  84. putc('\b');
  85. putc(slash[loopi++ % 8]);
  86. }
  87. }
  88. #ifdef CONFIG_440
  89. } else {
  90. /* ECC bit set method for cached memory */
  91. dcbz_area(start_address, num_bytes);
  92. /* Write modified dcache lines back to memory */
  93. clean_dcache_range(start_address, start_address + num_bytes);
  94. }
  95. #endif /* CONFIG_440 */
  96. blank_string(strlen(str));
  97. sync();
  98. eieio();
  99. wait_ddr_idle();
  100. /* clear ECC error repoting registers */
  101. mtsdram(SDRAM_ECCES, 0xffffffff);
  102. #if defined(CONFIG_SDRAM_PPC4xx_IBM_DDR)
  103. /*
  104. * IBM DDR(1) core (440GX):
  105. * Clear Mx bits in SDRAM0_BESR0/1
  106. */
  107. mtsdram(SDRAM0_BESR0, 0xffffffff);
  108. mtsdram(SDRAM0_BESR1, 0xffffffff);
  109. #elif defined(CONFIG_440)
  110. /*
  111. * 440/460 DDR2 core:
  112. * Clear EMID (Error PLB Master ID) in MQ0_ESL
  113. */
  114. mtdcr(SDRAM_ERRSTATLL, 0xfff00000);
  115. #else
  116. /*
  117. * 405EX(r) DDR2 core:
  118. * Clear M0ID (Error PLB Master ID) in SDRAM_BESR
  119. */
  120. mtsdram(SDRAM_BESR, 0xf0000000);
  121. #endif
  122. mtsdram(SDRAM_MCOPT1,
  123. (mcopt1 & ~SDRAM_MCOPT1_MCHK_MASK) | SDRAM_MCOPT1_MCHK_CHK_REP);
  124. sync();
  125. eieio();
  126. wait_ddr_idle();
  127. }
  128. }
  129. #if defined(CONFIG_SDRAM_PPC4xx_IBM_DDR)
  130. void ecc_init(unsigned long * const start, unsigned long size)
  131. {
  132. /*
  133. * Init ECC with cache disabled (on PPC's with IBM DDR
  134. * controller (non DDR2), not tested with cache enabled yet
  135. */
  136. program_ecc_addr((u32)start, size, TLB_WORD2_I_ENABLE);
  137. }
  138. #endif
  139. #if defined(CONFIG_SDRAM_PPC4xx_IBM_DDR2)
  140. void do_program_ecc(unsigned long tlb_word2_i_value)
  141. {
  142. unsigned long mcopt1;
  143. unsigned long mcopt2;
  144. unsigned long mcstat;
  145. phys_size_t memsize = sdram_memsize();
  146. if (memsize > CONFIG_MAX_MEM_MAPPED) {
  147. printf("\nWarning: Can't enable ECC on systems with more than 2GB of SDRAM!\n");
  148. return;
  149. }
  150. mfsdram(SDRAM_MCOPT1, mcopt1);
  151. mfsdram(SDRAM_MCOPT2, mcopt2);
  152. if ((mcopt1 & SDRAM_MCOPT1_MCHK_MASK) != SDRAM_MCOPT1_MCHK_NON) {
  153. /* DDR controller must be enabled and not in self-refresh. */
  154. mfsdram(SDRAM_MCSTAT, mcstat);
  155. if (((mcopt2 & SDRAM_MCOPT2_DCEN_MASK) == SDRAM_MCOPT2_DCEN_ENABLE)
  156. && ((mcopt2 & SDRAM_MCOPT2_SREN_MASK) == SDRAM_MCOPT2_SREN_EXIT)
  157. && ((mcstat & (SDRAM_MCSTAT_MIC_MASK | SDRAM_MCSTAT_SRMS_MASK))
  158. == (SDRAM_MCSTAT_MIC_COMP | SDRAM_MCSTAT_SRMS_NOT_SF))) {
  159. program_ecc_addr(0, memsize, tlb_word2_i_value);
  160. }
  161. }
  162. }
  163. #endif
  164. #endif /* defined(CONFIG_DDR_ECC) || defined(CONFIG_SDRAM_ECC) */
  165. #endif /* defined(CONFIG_SDRAM_PPC4xx_IBM_DDR)... */