omap_elm.c 5.4 KB

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