omap_elm.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. * SPDX-License-Identifier: GPL-2.0+
  14. */
  15. #include <common.h>
  16. #include <asm/io.h>
  17. #include <asm/errno.h>
  18. #include <linux/mtd/omap_gpmc.h>
  19. #include <asm/omap_elm.h>
  20. #include <asm/arch/hardware.h>
  21. #define ELM_DEFAULT_POLY (0)
  22. struct elm *elm_cfg;
  23. /**
  24. * elm_load_syndromes - Load BCH syndromes based on nibble selection
  25. * @syndrome: BCH syndrome
  26. * @nibbles:
  27. * @poly: Syndrome Polynomial set to use
  28. *
  29. * Load BCH syndromes based on nibble selection
  30. */
  31. static void elm_load_syndromes(u8 *syndrome, u32 nibbles, u8 poly)
  32. {
  33. u32 *ptr;
  34. u32 val;
  35. /* reg 0 */
  36. ptr = &elm_cfg->syndrome_fragments[poly].syndrome_fragment_x[0];
  37. val = syndrome[0] | (syndrome[1] << 8) | (syndrome[2] << 16) |
  38. (syndrome[3] << 24);
  39. writel(val, ptr);
  40. /* reg 1 */
  41. ptr = &elm_cfg->syndrome_fragments[poly].syndrome_fragment_x[1];
  42. val = syndrome[4] | (syndrome[5] << 8) | (syndrome[6] << 16) |
  43. (syndrome[7] << 24);
  44. writel(val, ptr);
  45. /* BCH 8-bit with 26 nibbles (4*8=32) */
  46. if (nibbles > 13) {
  47. /* reg 2 */
  48. ptr = &elm_cfg->syndrome_fragments[poly].syndrome_fragment_x[2];
  49. val = syndrome[8] | (syndrome[9] << 8) | (syndrome[10] << 16) |
  50. (syndrome[11] << 24);
  51. writel(val, ptr);
  52. /* reg 3 */
  53. ptr = &elm_cfg->syndrome_fragments[poly].syndrome_fragment_x[3];
  54. val = syndrome[12] | (syndrome[13] << 8) |
  55. (syndrome[14] << 16) | (syndrome[15] << 24);
  56. writel(val, ptr);
  57. }
  58. /* BCH 16-bit with 52 nibbles (7*8=56) */
  59. if (nibbles > 26) {
  60. /* reg 4 */
  61. ptr = &elm_cfg->syndrome_fragments[poly].syndrome_fragment_x[4];
  62. val = syndrome[16] | (syndrome[17] << 8) |
  63. (syndrome[18] << 16) | (syndrome[19] << 24);
  64. writel(val, ptr);
  65. /* reg 5 */
  66. ptr = &elm_cfg->syndrome_fragments[poly].syndrome_fragment_x[5];
  67. val = syndrome[20] | (syndrome[21] << 8) |
  68. (syndrome[22] << 16) | (syndrome[23] << 24);
  69. writel(val, ptr);
  70. /* reg 6 */
  71. ptr = &elm_cfg->syndrome_fragments[poly].syndrome_fragment_x[6];
  72. val = syndrome[24] | (syndrome[25] << 8) |
  73. (syndrome[26] << 16) | (syndrome[27] << 24);
  74. writel(val, ptr);
  75. }
  76. }
  77. /**
  78. * elm_check_errors - Check for BCH errors and return error locations
  79. * @syndrome: BCH syndrome
  80. * @nibbles:
  81. * @error_count: Returns number of errrors in the syndrome
  82. * @error_locations: Returns error locations (in decimal) in this array
  83. *
  84. * Check the provided syndrome for BCH errors and return error count
  85. * and locations in the array passed. Returns -1 if error is not correctable,
  86. * else returns 0
  87. */
  88. int elm_check_error(u8 *syndrome, u32 nibbles, u32 *error_count,
  89. u32 *error_locations)
  90. {
  91. u8 poly = ELM_DEFAULT_POLY;
  92. s8 i;
  93. u32 location_status;
  94. elm_load_syndromes(syndrome, nibbles, poly);
  95. /* start processing */
  96. writel((readl(&elm_cfg->syndrome_fragments[poly].syndrome_fragment_x[6])
  97. | ELM_SYNDROME_FRAGMENT_6_SYNDROME_VALID),
  98. &elm_cfg->syndrome_fragments[poly].syndrome_fragment_x[6]);
  99. /* wait for processing to complete */
  100. while ((readl(&elm_cfg->irqstatus) & (0x1 << poly)) != 0x1)
  101. ;
  102. /* clear status */
  103. writel((readl(&elm_cfg->irqstatus) | (0x1 << poly)),
  104. &elm_cfg->irqstatus);
  105. /* check if correctable */
  106. location_status = readl(&elm_cfg->error_location[poly].location_status);
  107. if (!(location_status & ELM_LOCATION_STATUS_ECC_CORRECTABLE_MASK))
  108. return -1;
  109. /* get error count */
  110. *error_count = readl(&elm_cfg->error_location[poly].location_status) &
  111. ELM_LOCATION_STATUS_ECC_NB_ERRORS_MASK;
  112. for (i = 0; i < *error_count; i++) {
  113. error_locations[i] =
  114. readl(&elm_cfg->error_location[poly].error_location_x[i]);
  115. }
  116. return 0;
  117. }
  118. /**
  119. * elm_config - Configure ELM module
  120. * @level: 4 / 8 / 16 bit BCH
  121. *
  122. * Configure ELM module based on BCH level.
  123. * Set mode as continuous mode.
  124. * Currently we are using only syndrome 0 and syndromes 1 to 6 are not used.
  125. * Also, the mode is set only for syndrome 0
  126. */
  127. int elm_config(enum bch_level level)
  128. {
  129. u32 val;
  130. u8 poly = ELM_DEFAULT_POLY;
  131. u32 buffer_size = 0x7FF;
  132. /* config size and level */
  133. val = (u32)(level) & ELM_LOCATION_CONFIG_ECC_BCH_LEVEL_MASK;
  134. val |= ((buffer_size << ELM_LOCATION_CONFIG_ECC_SIZE_POS) &
  135. ELM_LOCATION_CONFIG_ECC_SIZE_MASK);
  136. writel(val, &elm_cfg->location_config);
  137. /* config continous mode */
  138. /* enable interrupt generation for syndrome polynomial set */
  139. writel((readl(&elm_cfg->irqenable) | (0x1 << poly)),
  140. &elm_cfg->irqenable);
  141. /* set continuous mode for the syndrome polynomial set */
  142. writel((readl(&elm_cfg->page_ctrl) & ~(0x1 << poly)),
  143. &elm_cfg->page_ctrl);
  144. return 0;
  145. }
  146. /**
  147. * elm_reset - Do a soft reset of ELM
  148. *
  149. * Perform a soft reset of ELM and return after reset is done.
  150. */
  151. void elm_reset(void)
  152. {
  153. /* initiate reset */
  154. writel((readl(&elm_cfg->sysconfig) | ELM_SYSCONFIG_SOFTRESET),
  155. &elm_cfg->sysconfig);
  156. /* wait for reset complete and normal operation */
  157. while ((readl(&elm_cfg->sysstatus) & ELM_SYSSTATUS_RESETDONE) !=
  158. ELM_SYSSTATUS_RESETDONE)
  159. ;
  160. }
  161. /**
  162. * elm_init - Initialize ELM module
  163. *
  164. * Initialize ELM support. Currently it does only base address init
  165. * and ELM reset.
  166. */
  167. void elm_init(void)
  168. {
  169. elm_cfg = (struct elm *)ELM_BASE;
  170. elm_reset();
  171. }