omap_elm.c 5.4 KB

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