nomadik.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /*
  2. * (C) Copyright 2007 STMicroelectronics, <www.st.com>
  3. * (C) Copyright 2009 Alessandro Rubini <rubini@unipv.it>
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. #include <common.h>
  24. #include <nand.h>
  25. #include <asm/io.h>
  26. static inline int parity(int b) /* b is really a byte; returns 0 or ~0 */
  27. {
  28. __asm__ __volatile__(
  29. "eor %0, %0, %0, lsr #4\n\t"
  30. "eor %0, %0, %0, lsr #2\n\t"
  31. "eor %0, %0, %0, lsr #1\n\t"
  32. "ands %0, %0, #1\n\t"
  33. "subne %0, %0, #2\t"
  34. : "=r" (b) : "0" (b));
  35. return b;
  36. }
  37. /*
  38. * This is the ECC routine used in hardware, according to the manual.
  39. * HW claims to make the calculation but not the correction; so we must
  40. * recalculate the bytes for a comparison.
  41. */
  42. static int ecc512(const unsigned char *data, unsigned char *ecc)
  43. {
  44. int gpar = 0;
  45. int i, val, par;
  46. int pbits = 0; /* P8, P16, ... P2048 */
  47. int pprime = 0; /* P8', P16', ... P2048' */
  48. int lowbits; /* P1, P2, P4 and primes */
  49. for (i = 0; i < 512; i++) {
  50. par = parity((val = data[i]));
  51. gpar ^= val;
  52. pbits ^= (i & par);
  53. }
  54. /*
  55. * Ok, now gpar is global parity (xor of all bytes)
  56. * pbits are all the parity bits (non-prime ones)
  57. */
  58. par = parity(gpar);
  59. pprime = pbits ^ par;
  60. /* Put low bits in the right position for ecc[2] (bits 7..2) */
  61. lowbits = 0
  62. | (parity(gpar & 0xf0) & 0x80) /* P4 */
  63. | (parity(gpar & 0x0f) & 0x40) /* P4' */
  64. | (parity(gpar & 0xcc) & 0x20) /* P2 */
  65. | (parity(gpar & 0x33) & 0x10) /* P2' */
  66. | (parity(gpar & 0xaa) & 0x08) /* P1 */
  67. | (parity(gpar & 0x55) & 0x04); /* P1' */
  68. ecc[2] = ~(lowbits | ((pbits & 0x100) >> 7) | ((pprime & 0x100) >> 8));
  69. /* now intermix bits for ecc[1] (P1024..P128') and ecc[0] (P64..P8') */
  70. ecc[1] = ~( (pbits & 0x80) >> 0 | ((pprime & 0x80) >> 1)
  71. | ((pbits & 0x40) >> 1) | ((pprime & 0x40) >> 2)
  72. | ((pbits & 0x20) >> 2) | ((pprime & 0x20) >> 3)
  73. | ((pbits & 0x10) >> 3) | ((pprime & 0x10) >> 4));
  74. ecc[0] = ~( (pbits & 0x8) << 4 | ((pprime & 0x8) << 3)
  75. | ((pbits & 0x4) << 3) | ((pprime & 0x4) << 2)
  76. | ((pbits & 0x2) << 2) | ((pprime & 0x2) << 1)
  77. | ((pbits & 0x1) << 1) | ((pprime & 0x1) << 0));
  78. return 0;
  79. }
  80. /* This is the method in the chip->ecc field */
  81. static int nomadik_ecc_calculate(struct mtd_info *mtd, const uint8_t *dat,
  82. uint8_t *ecc_code)
  83. {
  84. return ecc512(dat, ecc_code);
  85. }
  86. static int nomadik_ecc_correct(struct mtd_info *mtd, uint8_t *dat,
  87. uint8_t *r_ecc, uint8_t *c_ecc)
  88. {
  89. struct nand_chip *chip = mtd->priv;
  90. uint32_t r, c, d, diff; /*read, calculated, xor of them */
  91. if (!memcmp(r_ecc, c_ecc, chip->ecc.bytes))
  92. return 0;
  93. /* Reorder the bytes into ascending-order 24 bits -- see manual */
  94. r = r_ecc[2] << 22 | r_ecc[1] << 14 | r_ecc[0] << 6 | r_ecc[2] >> 2;
  95. c = c_ecc[2] << 22 | c_ecc[1] << 14 | c_ecc[0] << 6 | c_ecc[2] >> 2;
  96. diff = (r ^ c) & ((1<<24)-1); /* use 24 bits only */
  97. /* If 12 bits are different, one per pair, it's correctable */
  98. if (((diff | (diff>>1)) & 0x555555) == 0x555555) {
  99. int bit = ((diff & 2) >> 1)
  100. | ((diff & 0x8) >> 2) | ((diff & 0x20) >> 3);
  101. int byte;
  102. d = diff >> 6; /* remove bit-order info */
  103. byte = ((d & 2) >> 1)
  104. | ((d & 0x8) >> 2) | ((d & 0x20) >> 3)
  105. | ((d & 0x80) >> 4) | ((d & 0x200) >> 5)
  106. | ((d & 0x800) >> 6) | ((d & 0x2000) >> 7)
  107. | ((d & 0x8000) >> 8) | ((d & 0x20000) >> 9);
  108. /* correct the single bit */
  109. dat[byte] ^= 1<<bit;
  110. return 0;
  111. }
  112. /* If 1 bit only differs, it's one bit error in ECC, ignore */
  113. if ((diff ^ (1 << (ffs(diff) - 1))) == 0)
  114. return 0;
  115. /* Otherwise, uncorrectable */
  116. return -1;
  117. }
  118. static void nomadik_ecc_hwctl(struct mtd_info *mtd, int mode)
  119. { /* mandatory in the structure but not used here */ }
  120. /* This is the layout used by older installations, we keep compatible */
  121. struct nand_ecclayout nomadik_ecc_layout = {
  122. .eccbytes = 3 * 4,
  123. .eccpos = { /* each subpage has 16 bytes: pos 2,3,4 hosts ECC */
  124. 0x02, 0x03, 0x04,
  125. 0x12, 0x13, 0x14,
  126. 0x22, 0x23, 0x24,
  127. 0x32, 0x33, 0x34},
  128. .oobfree = { {0x08, 0x08}, {0x18, 0x08}, {0x28, 0x08}, {0x38, 0x08} },
  129. };
  130. #define MASK_ALE (1 << 24) /* our ALE is AD21 */
  131. #define MASK_CLE (1 << 23) /* our CLE is AD22 */
  132. /* This is copied from the AT91SAM9 devices (Stelian Pop, Lead Tech Design) */
  133. static void nomadik_nand_hwcontrol(struct mtd_info *mtd,
  134. int cmd, unsigned int ctrl)
  135. {
  136. struct nand_chip *this = mtd->priv;
  137. u32 pcr0 = readl(REG_FSMC_PCR0);
  138. if (ctrl & NAND_CTRL_CHANGE) {
  139. ulong IO_ADDR_W = (ulong) this->IO_ADDR_W;
  140. IO_ADDR_W &= ~(MASK_ALE | MASK_CLE);
  141. if (ctrl & NAND_CLE)
  142. IO_ADDR_W |= MASK_CLE;
  143. if (ctrl & NAND_ALE)
  144. IO_ADDR_W |= MASK_ALE;
  145. if (ctrl & NAND_NCE)
  146. writel(pcr0 | 0x4, REG_FSMC_PCR0);
  147. else
  148. writel(pcr0 & ~0x4, REG_FSMC_PCR0);
  149. this->IO_ADDR_W = (void *) IO_ADDR_W;
  150. this->IO_ADDR_R = (void *) IO_ADDR_W;
  151. }
  152. if (cmd != NAND_CMD_NONE)
  153. writeb(cmd, this->IO_ADDR_W);
  154. }
  155. /* Returns 1 when ready; upper layers timeout at 20ms with timer routines */
  156. static int nomadik_nand_ready(struct mtd_info *mtd)
  157. {
  158. return 1; /* The ready bit is handled in hardware */
  159. }
  160. /* Copy a buffer 32bits at a time: faster than defualt method which is 8bit */
  161. static void nomadik_nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
  162. {
  163. int i;
  164. struct nand_chip *chip = mtd->priv;
  165. u32 *p = (u32 *) buf;
  166. len >>= 2;
  167. writel(0, REG_FSMC_ECCR0);
  168. for (i = 0; i < len; i++)
  169. p[i] = readl(chip->IO_ADDR_R);
  170. }
  171. int board_nand_init(struct nand_chip *chip)
  172. {
  173. /* Set up the FSMC_PCR0 for nand access*/
  174. writel(0x0000004a, REG_FSMC_PCR0);
  175. /* Set up FSMC_PMEM0, FSMC_PATT0 with timing data for access */
  176. writel(0x00020401, REG_FSMC_PMEM0);
  177. writel(0x00020404, REG_FSMC_PATT0);
  178. chip->options = NAND_COPYBACK | NAND_CACHEPRG | NAND_NO_PADDING;
  179. chip->cmd_ctrl = nomadik_nand_hwcontrol;
  180. chip->dev_ready = nomadik_nand_ready;
  181. /* The chip allows 32bit reads, so avoid the default 8bit copy */
  182. chip->read_buf = nomadik_nand_read_buf;
  183. /* ECC: follow the hardware-defined rulse, but do it in sw */
  184. chip->ecc.mode = NAND_ECC_HW;
  185. chip->ecc.bytes = 3;
  186. chip->ecc.size = 512;
  187. chip->ecc.layout = &nomadik_ecc_layout;
  188. chip->ecc.calculate = nomadik_ecc_calculate;
  189. chip->ecc.hwctl = nomadik_ecc_hwctl;
  190. chip->ecc.correct = nomadik_ecc_correct;
  191. return 0;
  192. }