elm.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*
  2. * (C) Copyright 2010-2011 Texas Instruments, <www.ti.com>
  3. * Mansoor Ahamed <mansoor.ahamed@ti.com>
  4. *
  5. * BCH Error Location Module (ELM) support.
  6. *
  7. * NOTE:
  8. * 1. Supports only continuous mode. Dont see need for page mode in uboot
  9. * 2. Supports only syndrome polynomial 0. i.e. poly local variable is
  10. * always set to ELM_DEFAULT_POLY. Dont see need for other polynomial
  11. * sets in uboot
  12. *
  13. * See file CREDITS for list of people who contributed to this
  14. * project.
  15. *
  16. * This program is free software; you can redistribute it and/or
  17. * modify it under the terms of the GNU General Public License as
  18. * published by the Free Software Foundation; either version 2 of
  19. * the License, or (at your option) any later version.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU General Public License
  27. * along with this program; if not, write to the Free Software
  28. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  29. * MA 02111-1307 USA
  30. */
  31. #include <common.h>
  32. #include <asm/io.h>
  33. #include <asm/errno.h>
  34. #include <asm/arch/cpu.h>
  35. #include <asm/omap_gpmc.h>
  36. #include <asm/arch/elm.h>
  37. #define ELM_DEFAULT_POLY (0)
  38. struct elm *elm_cfg;
  39. /**
  40. * elm_load_syndromes - Load BCH syndromes based on nibble selection
  41. * @syndrome: BCH syndrome
  42. * @nibbles:
  43. * @poly: Syndrome Polynomial set to use
  44. *
  45. * Load BCH syndromes based on nibble selection
  46. */
  47. static void elm_load_syndromes(u8 *syndrome, u32 nibbles, u8 poly)
  48. {
  49. u32 *ptr;
  50. u32 val;
  51. /* reg 0 */
  52. ptr = &elm_cfg->syndrome_fragments[poly].syndrome_fragment_x[0];
  53. val = syndrome[0] | (syndrome[1] << 8) | (syndrome[2] << 16) |
  54. (syndrome[3] << 24);
  55. writel(val, ptr);
  56. /* reg 1 */
  57. ptr = &elm_cfg->syndrome_fragments[poly].syndrome_fragment_x[1];
  58. val = syndrome[4] | (syndrome[5] << 8) | (syndrome[6] << 16) |
  59. (syndrome[7] << 24);
  60. writel(val, ptr);
  61. /* BCH 8-bit with 26 nibbles (4*8=32) */
  62. if (nibbles > 13) {
  63. /* reg 2 */
  64. ptr = &elm_cfg->syndrome_fragments[poly].syndrome_fragment_x[2];
  65. val = syndrome[8] | (syndrome[9] << 8) | (syndrome[10] << 16) |
  66. (syndrome[11] << 24);
  67. writel(val, ptr);
  68. /* reg 3 */
  69. ptr = &elm_cfg->syndrome_fragments[poly].syndrome_fragment_x[3];
  70. val = syndrome[12] | (syndrome[13] << 8) |
  71. (syndrome[14] << 16) | (syndrome[15] << 24);
  72. writel(val, ptr);
  73. }
  74. /* BCH 16-bit with 52 nibbles (7*8=56) */
  75. if (nibbles > 26) {
  76. /* reg 4 */
  77. ptr = &elm_cfg->syndrome_fragments[poly].syndrome_fragment_x[4];
  78. val = syndrome[16] | (syndrome[17] << 8) |
  79. (syndrome[18] << 16) | (syndrome[19] << 24);
  80. writel(val, ptr);
  81. /* reg 5 */
  82. ptr = &elm_cfg->syndrome_fragments[poly].syndrome_fragment_x[5];
  83. val = syndrome[20] | (syndrome[21] << 8) |
  84. (syndrome[22] << 16) | (syndrome[23] << 24);
  85. writel(val, ptr);
  86. /* reg 6 */
  87. ptr = &elm_cfg->syndrome_fragments[poly].syndrome_fragment_x[6];
  88. val = syndrome[24] | (syndrome[25] << 8) |
  89. (syndrome[26] << 16) | (syndrome[27] << 24);
  90. writel(val, ptr);
  91. }
  92. }
  93. /**
  94. * elm_check_errors - Check for BCH errors and return error locations
  95. * @syndrome: BCH syndrome
  96. * @nibbles:
  97. * @error_count: Returns number of errrors in the syndrome
  98. * @error_locations: Returns error locations (in decimal) in this array
  99. *
  100. * Check the provided syndrome for BCH errors and return error count
  101. * and locations in the array passed. Returns -1 if error is not correctable,
  102. * else returns 0
  103. */
  104. int elm_check_error(u8 *syndrome, u32 nibbles, u32 *error_count,
  105. u32 *error_locations)
  106. {
  107. u8 poly = ELM_DEFAULT_POLY;
  108. s8 i;
  109. u32 location_status;
  110. elm_load_syndromes(syndrome, nibbles, poly);
  111. /* start processing */
  112. writel((readl(&elm_cfg->syndrome_fragments[poly].syndrome_fragment_x[6])
  113. | ELM_SYNDROME_FRAGMENT_6_SYNDROME_VALID),
  114. &elm_cfg->syndrome_fragments[poly].syndrome_fragment_x[6]);
  115. /* wait for processing to complete */
  116. while ((readl(&elm_cfg->irqstatus) & (0x1 << poly)) != 0x1)
  117. ;
  118. /* clear status */
  119. writel((readl(&elm_cfg->irqstatus) | (0x1 << poly)),
  120. &elm_cfg->irqstatus);
  121. /* check if correctable */
  122. location_status = readl(&elm_cfg->error_location[poly].location_status);
  123. if (!(location_status & ELM_LOCATION_STATUS_ECC_CORRECTABLE_MASK))
  124. return -1;
  125. /* get error count */
  126. *error_count = readl(&elm_cfg->error_location[poly].location_status) &
  127. ELM_LOCATION_STATUS_ECC_NB_ERRORS_MASK;
  128. for (i = 0; i < *error_count; i++) {
  129. error_locations[i] =
  130. readl(&elm_cfg->error_location[poly].error_location_x[i]);
  131. }
  132. return 0;
  133. }
  134. /**
  135. * elm_config - Configure ELM module
  136. * @level: 4 / 8 / 16 bit BCH
  137. *
  138. * Configure ELM module based on BCH level.
  139. * Set mode as continuous mode.
  140. * Currently we are using only syndrome 0 and syndromes 1 to 6 are not used.
  141. * Also, the mode is set only for syndrome 0
  142. */
  143. int elm_config(enum bch_level level)
  144. {
  145. u32 val;
  146. u8 poly = ELM_DEFAULT_POLY;
  147. u32 buffer_size = 0x7FF;
  148. /* config size and level */
  149. val = (u32)(level) & ELM_LOCATION_CONFIG_ECC_BCH_LEVEL_MASK;
  150. val |= ((buffer_size << ELM_LOCATION_CONFIG_ECC_SIZE_POS) &
  151. ELM_LOCATION_CONFIG_ECC_SIZE_MASK);
  152. writel(val, &elm_cfg->location_config);
  153. /* config continous mode */
  154. /* enable interrupt generation for syndrome polynomial set */
  155. writel((readl(&elm_cfg->irqenable) | (0x1 << poly)),
  156. &elm_cfg->irqenable);
  157. /* set continuous mode for the syndrome polynomial set */
  158. writel((readl(&elm_cfg->page_ctrl) & ~(0x1 << poly)),
  159. &elm_cfg->page_ctrl);
  160. return 0;
  161. }
  162. /**
  163. * elm_reset - Do a soft reset of ELM
  164. *
  165. * Perform a soft reset of ELM and return after reset is done.
  166. */
  167. void elm_reset(void)
  168. {
  169. /* initiate reset */
  170. writel((readl(&elm_cfg->sysconfig) | ELM_SYSCONFIG_SOFTRESET),
  171. &elm_cfg->sysconfig);
  172. /* wait for reset complete and normal operation */
  173. while ((readl(&elm_cfg->sysstatus) & ELM_SYSSTATUS_RESETDONE) !=
  174. ELM_SYSSTATUS_RESETDONE)
  175. ;
  176. }
  177. /**
  178. * elm_init - Initialize ELM module
  179. *
  180. * Initialize ELM support. Currently it does only base address init
  181. * and ELM reset.
  182. */
  183. void elm_init(void)
  184. {
  185. elm_cfg = (struct elm *)ELM_BASE;
  186. elm_reset();
  187. }