ecc.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. * See file CREDITS for list of people who contributed to this
  15. * project.
  16. *
  17. * This program is free software; you can redistribute it and/or
  18. * modify it under the terms of the GNU General Public License as
  19. * published by the Free Software Foundation; either version 2 of
  20. * the License, or (at your option) any later version.
  21. *
  22. * This program is distributed in the hope that it will abe useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. * GNU General Public License for more details.
  26. *
  27. * You should have received a copy of the GNU General Public License
  28. * along with this program; if not, write to the Free Software
  29. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  30. * MA 02111-1307 USA
  31. *
  32. * Description:
  33. * This file implements generic DRAM ECC initialization for
  34. * PowerPC processors using a SDRAM DDR/DDR2 controller,
  35. * including the 405EX(r), 440GP/GX/EP/GR, 440SP(E), and
  36. * 460EX/GT.
  37. */
  38. #include <common.h>
  39. #include <asm/ppc4xx.h>
  40. #include <ppc_asm.tmpl>
  41. #include <ppc_defs.h>
  42. #include <asm/processor.h>
  43. #include <asm/io.h>
  44. #include <asm/mmu.h>
  45. #include <asm/cache.h>
  46. #include "ecc.h"
  47. #if defined(CONFIG_SDRAM_PPC4xx_IBM_DDR) || \
  48. defined(CONFIG_SDRAM_PPC4xx_IBM_DDR2)
  49. #if defined(CONFIG_DDR_ECC) || defined(CONFIG_SDRAM_ECC)
  50. #if defined(CONFIG_405EX)
  51. /*
  52. * Currently only 405EX uses 16bit data bus width as an alternative
  53. * option to 32bit data width (SDRAM0_MCOPT1_WDTH)
  54. */
  55. #define SDRAM_DATA_ALT_WIDTH 2
  56. #else
  57. #define SDRAM_DATA_ALT_WIDTH 8
  58. #endif
  59. static void wait_ddr_idle(void)
  60. {
  61. u32 val;
  62. do {
  63. mfsdram(SDRAM_MCSTAT, val);
  64. } while ((val & SDRAM_MCSTAT_IDLE_MASK) == SDRAM_MCSTAT_IDLE_NOT);
  65. }
  66. static void program_ecc_addr(unsigned long start_address,
  67. unsigned long num_bytes,
  68. unsigned long tlb_word2_i_value)
  69. {
  70. unsigned long current_address;
  71. unsigned long end_address;
  72. unsigned long address_increment;
  73. unsigned long mcopt1;
  74. char str[] = "ECC generation -";
  75. char slash[] = "\\|/-\\|/-";
  76. int loop = 0;
  77. int loopi = 0;
  78. current_address = start_address;
  79. mfsdram(SDRAM_MCOPT1, mcopt1);
  80. if ((mcopt1 & SDRAM_MCOPT1_MCHK_MASK) != SDRAM_MCOPT1_MCHK_NON) {
  81. mtsdram(SDRAM_MCOPT1,
  82. (mcopt1 & ~SDRAM_MCOPT1_MCHK_MASK) | SDRAM_MCOPT1_MCHK_GEN);
  83. sync();
  84. eieio();
  85. wait_ddr_idle();
  86. puts(str);
  87. #ifdef CONFIG_440
  88. if (tlb_word2_i_value == TLB_WORD2_I_ENABLE) {
  89. #endif
  90. /* ECC bit set method for non-cached memory */
  91. if ((mcopt1 & SDRAM_MCOPT1_DMWD_MASK) == SDRAM_MCOPT1_DMWD_32)
  92. address_increment = 4;
  93. else
  94. address_increment = SDRAM_DATA_ALT_WIDTH;
  95. end_address = current_address + num_bytes;
  96. while (current_address < end_address) {
  97. *((unsigned long *)current_address) = 0;
  98. current_address += address_increment;
  99. if ((loop++ % (2 << 20)) == 0) {
  100. putc('\b');
  101. putc(slash[loopi++ % 8]);
  102. }
  103. }
  104. #ifdef CONFIG_440
  105. } else {
  106. /* ECC bit set method for cached memory */
  107. dcbz_area(start_address, num_bytes);
  108. /* Write modified dcache lines back to memory */
  109. clean_dcache_range(start_address, start_address + num_bytes);
  110. }
  111. #endif /* CONFIG_440 */
  112. blank_string(strlen(str));
  113. sync();
  114. eieio();
  115. wait_ddr_idle();
  116. /* clear ECC error repoting registers */
  117. mtsdram(SDRAM_ECCES, 0xffffffff);
  118. #if defined(CONFIG_SDRAM_PPC4xx_IBM_DDR)
  119. /*
  120. * IBM DDR(1) core (440GX):
  121. * Clear Mx bits in SDRAM0_BESR0/1
  122. */
  123. mtsdram(SDRAM0_BESR0, 0xffffffff);
  124. mtsdram(SDRAM0_BESR1, 0xffffffff);
  125. #elif defined(CONFIG_440)
  126. /*
  127. * 440/460 DDR2 core:
  128. * Clear EMID (Error PLB Master ID) in MQ0_ESL
  129. */
  130. mtdcr(SDRAM_ERRSTATLL, 0xfff00000);
  131. #else
  132. /*
  133. * 405EX(r) DDR2 core:
  134. * Clear M0ID (Error PLB Master ID) in SDRAM_BESR
  135. */
  136. mtsdram(SDRAM_BESR, 0xf0000000);
  137. #endif
  138. mtsdram(SDRAM_MCOPT1,
  139. (mcopt1 & ~SDRAM_MCOPT1_MCHK_MASK) | SDRAM_MCOPT1_MCHK_CHK_REP);
  140. sync();
  141. eieio();
  142. wait_ddr_idle();
  143. }
  144. }
  145. #if defined(CONFIG_SDRAM_PPC4xx_IBM_DDR)
  146. void ecc_init(unsigned long * const start, unsigned long size)
  147. {
  148. /*
  149. * Init ECC with cache disabled (on PPC's with IBM DDR
  150. * controller (non DDR2), not tested with cache enabled yet
  151. */
  152. program_ecc_addr((u32)start, size, TLB_WORD2_I_ENABLE);
  153. }
  154. #endif
  155. #if defined(CONFIG_SDRAM_PPC4xx_IBM_DDR2)
  156. void do_program_ecc(unsigned long tlb_word2_i_value)
  157. {
  158. unsigned long mcopt1;
  159. unsigned long mcopt2;
  160. unsigned long mcstat;
  161. phys_size_t memsize = sdram_memsize();
  162. if (memsize > CONFIG_MAX_MEM_MAPPED) {
  163. printf("\nWarning: Can't enable ECC on systems with more than 2GB of SDRAM!\n");
  164. return;
  165. }
  166. mfsdram(SDRAM_MCOPT1, mcopt1);
  167. mfsdram(SDRAM_MCOPT2, mcopt2);
  168. if ((mcopt1 & SDRAM_MCOPT1_MCHK_MASK) != SDRAM_MCOPT1_MCHK_NON) {
  169. /* DDR controller must be enabled and not in self-refresh. */
  170. mfsdram(SDRAM_MCSTAT, mcstat);
  171. if (((mcopt2 & SDRAM_MCOPT2_DCEN_MASK) == SDRAM_MCOPT2_DCEN_ENABLE)
  172. && ((mcopt2 & SDRAM_MCOPT2_SREN_MASK) == SDRAM_MCOPT2_SREN_EXIT)
  173. && ((mcstat & (SDRAM_MCSTAT_MIC_MASK | SDRAM_MCSTAT_SRMS_MASK))
  174. == (SDRAM_MCSTAT_MIC_COMP | SDRAM_MCSTAT_SRMS_NOT_SF))) {
  175. program_ecc_addr(0, memsize, tlb_word2_i_value);
  176. }
  177. }
  178. }
  179. #endif
  180. #endif /* defined(CONFIG_DDR_ECC) || defined(CONFIG_SDRAM_ECC) */
  181. #endif /* defined(CONFIG_SDRAM_PPC4xx_IBM_DDR)... */