nand_bcm_umi.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. /*****************************************************************************
  2. * Copyright 2003 - 2009 Broadcom Corporation. All rights reserved.
  3. *
  4. * Unless you and Broadcom execute a separate written software license
  5. * agreement governing use of this software, this software is licensed to you
  6. * under the terms of the GNU General Public License version 2, available at
  7. * http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
  8. *
  9. * Notwithstanding the above, under no circumstances may you combine this
  10. * software in any way with any other Broadcom software provided under a
  11. * license other than the GPL, without Broadcom's express prior written
  12. * consent.
  13. *****************************************************************************/
  14. #ifndef NAND_BCM_UMI_H
  15. #define NAND_BCM_UMI_H
  16. /* ---- Include Files ---------------------------------------------------- */
  17. #include <mach/reg_umi.h>
  18. #include <mach/reg_nand.h>
  19. #include <mach/cfg_global.h>
  20. /* ---- Constants and Types ---------------------------------------------- */
  21. #if (CFG_GLOBAL_CHIP_FAMILY == CFG_GLOBAL_CHIP_FAMILY_BCMRING)
  22. #define NAND_ECC_BCH (CFG_GLOBAL_CHIP_REV > 0xA0)
  23. #else
  24. #define NAND_ECC_BCH 0
  25. #endif
  26. #define CFG_GLOBAL_NAND_ECC_BCH_NUM_BYTES 13
  27. #if NAND_ECC_BCH
  28. #ifdef BOOT0_BUILD
  29. #define NAND_ECC_NUM_BYTES 13
  30. #else
  31. #define NAND_ECC_NUM_BYTES CFG_GLOBAL_NAND_ECC_BCH_NUM_BYTES
  32. #endif
  33. #else
  34. #define NAND_ECC_NUM_BYTES 3
  35. #endif
  36. #define NAND_DATA_ACCESS_SIZE 512
  37. /* ---- Variable Externs ------------------------------------------ */
  38. /* ---- Function Prototypes --------------------------------------- */
  39. int nand_bcm_umi_bch_correct_page(uint8_t *datap, uint8_t *readEccData,
  40. int numEccBytes);
  41. /* Check in device is ready */
  42. static inline int nand_bcm_umi_dev_ready(void)
  43. {
  44. return readl(&REG_UMI_NAND_RCSR) & REG_UMI_NAND_RCSR_RDY;
  45. }
  46. /* Wait until device is ready */
  47. static inline void nand_bcm_umi_wait_till_ready(void)
  48. {
  49. while (nand_bcm_umi_dev_ready() == 0)
  50. ;
  51. }
  52. /* Enable Hamming ECC */
  53. static inline void nand_bcm_umi_hamming_enable_hwecc(void)
  54. {
  55. /* disable and reset ECC, 512 byte page */
  56. writel(readl(&REG_UMI_NAND_ECC_CSR) & ~(REG_UMI_NAND_ECC_CSR_ECC_ENABLE |
  57. REG_UMI_NAND_ECC_CSR_256BYTE), &REG_UMI_NAND_ECC_CSR);
  58. /* enable ECC */
  59. writel(readl(&REG_UMI_NAND_ECC_CSR) | REG_UMI_NAND_ECC_CSR_ECC_ENABLE,
  60. &REG_UMI_NAND_ECC_CSR);
  61. }
  62. #if NAND_ECC_BCH
  63. /* BCH ECC specifics */
  64. #define ECC_BITS_PER_CORRECTABLE_BIT 13
  65. /* Enable BCH Read ECC */
  66. static inline void nand_bcm_umi_bch_enable_read_hwecc(void)
  67. {
  68. /* disable and reset ECC */
  69. writel(REG_UMI_BCH_CTRL_STATUS_RD_ECC_VALID, &REG_UMI_BCH_CTRL_STATUS);
  70. /* Turn on ECC */
  71. writel(REG_UMI_BCH_CTRL_STATUS_ECC_RD_EN, &REG_UMI_BCH_CTRL_STATUS);
  72. }
  73. /* Enable BCH Write ECC */
  74. static inline void nand_bcm_umi_bch_enable_write_hwecc(void)
  75. {
  76. /* disable and reset ECC */
  77. writel(REG_UMI_BCH_CTRL_STATUS_WR_ECC_VALID, &REG_UMI_BCH_CTRL_STATUS);
  78. /* Turn on ECC */
  79. writel(REG_UMI_BCH_CTRL_STATUS_ECC_WR_EN, &REG_UMI_BCH_CTRL_STATUS);
  80. }
  81. /* Config number of BCH ECC bytes */
  82. static inline void nand_bcm_umi_bch_config_ecc(uint8_t numEccBytes)
  83. {
  84. uint32_t nValue;
  85. uint32_t tValue;
  86. uint32_t kValue;
  87. uint32_t numBits = numEccBytes * 8;
  88. /* disable and reset ECC */
  89. writel(REG_UMI_BCH_CTRL_STATUS_WR_ECC_VALID |
  90. REG_UMI_BCH_CTRL_STATUS_RD_ECC_VALID,
  91. &REG_UMI_BCH_CTRL_STATUS);
  92. /* Every correctible bit requires 13 ECC bits */
  93. tValue = (uint32_t) (numBits / ECC_BITS_PER_CORRECTABLE_BIT);
  94. /* Total data in number of bits for generating and computing BCH ECC */
  95. nValue = (NAND_DATA_ACCESS_SIZE + numEccBytes) * 8;
  96. /* K parameter is used internally. K = N - (T * 13) */
  97. kValue = nValue - (tValue * ECC_BITS_PER_CORRECTABLE_BIT);
  98. /* Write the settings */
  99. writel(nValue, &REG_UMI_BCH_N);
  100. writel(tValue, &REG_UMI_BCH_T);
  101. writel(kValue, &REG_UMI_BCH_K);
  102. }
  103. /* Pause during ECC read calculation to skip bytes in OOB */
  104. static inline void nand_bcm_umi_bch_pause_read_ecc_calc(void)
  105. {
  106. writel(REG_UMI_BCH_CTRL_STATUS_ECC_RD_EN | REG_UMI_BCH_CTRL_STATUS_PAUSE_ECC_DEC, &REG_UMI_BCH_CTRL_STATUS);
  107. }
  108. /* Resume during ECC read calculation after skipping bytes in OOB */
  109. static inline void nand_bcm_umi_bch_resume_read_ecc_calc(void)
  110. {
  111. writel(REG_UMI_BCH_CTRL_STATUS_ECC_RD_EN, &REG_UMI_BCH_CTRL_STATUS);
  112. }
  113. /* Poll read ECC calc to check when hardware completes */
  114. static inline uint32_t nand_bcm_umi_bch_poll_read_ecc_calc(void)
  115. {
  116. uint32_t regVal;
  117. do {
  118. /* wait for ECC to be valid */
  119. regVal = readl(&REG_UMI_BCH_CTRL_STATUS);
  120. } while ((regVal & REG_UMI_BCH_CTRL_STATUS_RD_ECC_VALID) == 0);
  121. return regVal;
  122. }
  123. /* Poll write ECC calc to check when hardware completes */
  124. static inline void nand_bcm_umi_bch_poll_write_ecc_calc(void)
  125. {
  126. /* wait for ECC to be valid */
  127. while ((readl(&REG_UMI_BCH_CTRL_STATUS) & REG_UMI_BCH_CTRL_STATUS_WR_ECC_VALID)
  128. == 0)
  129. ;
  130. }
  131. /* Read the OOB and ECC, for kernel write OOB to a buffer */
  132. #if defined(__KERNEL__) && !defined(STANDALONE)
  133. static inline void nand_bcm_umi_bch_read_oobEcc(uint32_t pageSize,
  134. uint8_t *eccCalc, int numEccBytes, uint8_t *oobp)
  135. #else
  136. static inline void nand_bcm_umi_bch_read_oobEcc(uint32_t pageSize,
  137. uint8_t *eccCalc, int numEccBytes)
  138. #endif
  139. {
  140. int eccPos = 0;
  141. int numToRead = 16; /* There are 16 bytes per sector in the OOB */
  142. /* ECC is already paused when this function is called */
  143. if (pageSize != NAND_DATA_ACCESS_SIZE) {
  144. /* skip BI */
  145. #if defined(__KERNEL__) && !defined(STANDALONE)
  146. *oobp++ = readb(&REG_NAND_DATA8);
  147. #else
  148. readb(&REG_NAND_DATA8);
  149. #endif
  150. numToRead--;
  151. }
  152. while (numToRead > numEccBytes) {
  153. /* skip free oob region */
  154. #if defined(__KERNEL__) && !defined(STANDALONE)
  155. *oobp++ = readb(&REG_NAND_DATA8);
  156. #else
  157. readb(&REG_NAND_DATA8);
  158. #endif
  159. numToRead--;
  160. }
  161. if (pageSize == NAND_DATA_ACCESS_SIZE) {
  162. /* read ECC bytes before BI */
  163. nand_bcm_umi_bch_resume_read_ecc_calc();
  164. while (numToRead > 11) {
  165. #if defined(__KERNEL__) && !defined(STANDALONE)
  166. *oobp = readb(&REG_NAND_DATA8);
  167. eccCalc[eccPos++] = *oobp;
  168. oobp++;
  169. #else
  170. eccCalc[eccPos++] = readb(&REG_NAND_DATA8);
  171. #endif
  172. numToRead--;
  173. }
  174. nand_bcm_umi_bch_pause_read_ecc_calc();
  175. if (numToRead == 11) {
  176. /* read BI */
  177. #if defined(__KERNEL__) && !defined(STANDALONE)
  178. *oobp++ = readb(&REG_NAND_DATA8);
  179. #else
  180. readb(&REG_NAND_DATA8);
  181. #endif
  182. numToRead--;
  183. }
  184. }
  185. /* read ECC bytes */
  186. nand_bcm_umi_bch_resume_read_ecc_calc();
  187. while (numToRead) {
  188. #if defined(__KERNEL__) && !defined(STANDALONE)
  189. *oobp = readb(&REG_NAND_DATA8);
  190. eccCalc[eccPos++] = *oobp;
  191. oobp++;
  192. #else
  193. eccCalc[eccPos++] = readb(&REG_NAND_DATA8);
  194. #endif
  195. numToRead--;
  196. }
  197. }
  198. /* Helper function to write ECC */
  199. static inline void NAND_BCM_UMI_ECC_WRITE(int numEccBytes, int eccBytePos,
  200. uint8_t *oobp, uint8_t eccVal)
  201. {
  202. if (eccBytePos <= numEccBytes)
  203. *oobp = eccVal;
  204. }
  205. /* Write OOB with ECC */
  206. static inline void nand_bcm_umi_bch_write_oobEcc(uint32_t pageSize,
  207. uint8_t *oobp, int numEccBytes)
  208. {
  209. uint32_t eccVal = 0xffffffff;
  210. /* wait for write ECC to be valid */
  211. nand_bcm_umi_bch_poll_write_ecc_calc();
  212. /*
  213. ** Get the hardware ecc from the 32-bit result registers.
  214. ** Read after 512 byte accesses. Format B3B2B1B0
  215. ** where B3 = ecc3, etc.
  216. */
  217. if (pageSize == NAND_DATA_ACCESS_SIZE) {
  218. /* Now fill in the ECC bytes */
  219. if (numEccBytes >= 13)
  220. eccVal = readl(&REG_UMI_BCH_WR_ECC_3);
  221. /* Usually we skip CM in oob[0,1] */
  222. NAND_BCM_UMI_ECC_WRITE(numEccBytes, 15, &oobp[0],
  223. (eccVal >> 16) & 0xff);
  224. NAND_BCM_UMI_ECC_WRITE(numEccBytes, 14, &oobp[1],
  225. (eccVal >> 8) & 0xff);
  226. /* Write ECC in oob[2,3,4] */
  227. NAND_BCM_UMI_ECC_WRITE(numEccBytes, 13, &oobp[2],
  228. eccVal & 0xff); /* ECC 12 */
  229. if (numEccBytes >= 9)
  230. eccVal = readl(&REG_UMI_BCH_WR_ECC_2);
  231. NAND_BCM_UMI_ECC_WRITE(numEccBytes, 12, &oobp[3],
  232. (eccVal >> 24) & 0xff); /* ECC11 */
  233. NAND_BCM_UMI_ECC_WRITE(numEccBytes, 11, &oobp[4],
  234. (eccVal >> 16) & 0xff); /* ECC10 */
  235. /* Always Skip BI in oob[5] */
  236. } else {
  237. /* Always Skip BI in oob[0] */
  238. /* Now fill in the ECC bytes */
  239. if (numEccBytes >= 13)
  240. eccVal = readl(&REG_UMI_BCH_WR_ECC_3);
  241. /* Usually skip CM in oob[1,2] */
  242. NAND_BCM_UMI_ECC_WRITE(numEccBytes, 15, &oobp[1],
  243. (eccVal >> 16) & 0xff);
  244. NAND_BCM_UMI_ECC_WRITE(numEccBytes, 14, &oobp[2],
  245. (eccVal >> 8) & 0xff);
  246. /* Write ECC in oob[3-15] */
  247. NAND_BCM_UMI_ECC_WRITE(numEccBytes, 13, &oobp[3],
  248. eccVal & 0xff); /* ECC12 */
  249. if (numEccBytes >= 9)
  250. eccVal = readl(&REG_UMI_BCH_WR_ECC_2);
  251. NAND_BCM_UMI_ECC_WRITE(numEccBytes, 12, &oobp[4],
  252. (eccVal >> 24) & 0xff); /* ECC11 */
  253. NAND_BCM_UMI_ECC_WRITE(numEccBytes, 11, &oobp[5],
  254. (eccVal >> 16) & 0xff); /* ECC10 */
  255. }
  256. /* Fill in the remainder of ECC locations */
  257. NAND_BCM_UMI_ECC_WRITE(numEccBytes, 10, &oobp[6],
  258. (eccVal >> 8) & 0xff); /* ECC9 */
  259. NAND_BCM_UMI_ECC_WRITE(numEccBytes, 9, &oobp[7],
  260. eccVal & 0xff); /* ECC8 */
  261. if (numEccBytes >= 5)
  262. eccVal = readl(&REG_UMI_BCH_WR_ECC_1);
  263. NAND_BCM_UMI_ECC_WRITE(numEccBytes, 8, &oobp[8],
  264. (eccVal >> 24) & 0xff); /* ECC7 */
  265. NAND_BCM_UMI_ECC_WRITE(numEccBytes, 7, &oobp[9],
  266. (eccVal >> 16) & 0xff); /* ECC6 */
  267. NAND_BCM_UMI_ECC_WRITE(numEccBytes, 6, &oobp[10],
  268. (eccVal >> 8) & 0xff); /* ECC5 */
  269. NAND_BCM_UMI_ECC_WRITE(numEccBytes, 5, &oobp[11],
  270. eccVal & 0xff); /* ECC4 */
  271. if (numEccBytes >= 1)
  272. eccVal = readl(&REG_UMI_BCH_WR_ECC_0);
  273. NAND_BCM_UMI_ECC_WRITE(numEccBytes, 4, &oobp[12],
  274. (eccVal >> 24) & 0xff); /* ECC3 */
  275. NAND_BCM_UMI_ECC_WRITE(numEccBytes, 3, &oobp[13],
  276. (eccVal >> 16) & 0xff); /* ECC2 */
  277. NAND_BCM_UMI_ECC_WRITE(numEccBytes, 2, &oobp[14],
  278. (eccVal >> 8) & 0xff); /* ECC1 */
  279. NAND_BCM_UMI_ECC_WRITE(numEccBytes, 1, &oobp[15],
  280. eccVal & 0xff); /* ECC0 */
  281. }
  282. #endif
  283. #endif /* NAND_BCM_UMI_H */