bfin_nand.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. /*
  2. * Driver for Blackfin on-chip NAND controller.
  3. *
  4. * Enter bugs at http://blackfin.uclinux.org/
  5. *
  6. * Copyright (c) 2007-2008 Analog Devices Inc.
  7. *
  8. * Licensed under the GPL-2 or later.
  9. */
  10. /* TODO:
  11. * - move bit defines into mach-common/bits/nand.h
  12. * - try and replace all IRQSTAT usage with STAT polling
  13. * - have software ecc mode use same algo as hw ecc ?
  14. */
  15. #include <common.h>
  16. #include <asm/io.h>
  17. #ifdef DEBUG
  18. # define pr_stamp() printf("%s:%s:%i: here i am\n", __FILE__, __func__, __LINE__)
  19. #else
  20. # define pr_stamp()
  21. #endif
  22. #include <nand.h>
  23. #include <asm/blackfin.h>
  24. /* Bit masks for NFC_CTL */
  25. #define WR_DLY 0xf /* Write Strobe Delay */
  26. #define RD_DLY 0xf0 /* Read Strobe Delay */
  27. #define NWIDTH 0x100 /* NAND Data Width */
  28. #define PG_SIZE 0x200 /* Page Size */
  29. /* Bit masks for NFC_STAT */
  30. #define NBUSY 0x1 /* Not Busy */
  31. #define WB_FULL 0x2 /* Write Buffer Full */
  32. #define PG_WR_STAT 0x4 /* Page Write Pending */
  33. #define PG_RD_STAT 0x8 /* Page Read Pending */
  34. #define WB_EMPTY 0x10 /* Write Buffer Empty */
  35. /* Bit masks for NFC_IRQSTAT */
  36. #define NBUSYIRQ 0x1 /* Not Busy IRQ */
  37. #define WB_OVF 0x2 /* Write Buffer Overflow */
  38. #define WB_EDGE 0x4 /* Write Buffer Edge Detect */
  39. #define RD_RDY 0x8 /* Read Data Ready */
  40. #define WR_DONE 0x10 /* Page Write Done */
  41. #define NAND_IS_512() (CONFIG_BFIN_NFC_CTL_VAL & 0x200)
  42. /*
  43. * hardware specific access to control-lines
  44. */
  45. static void bfin_nfc_cmd_ctrl(struct mtd_info *mtd, int cmd, unsigned int ctrl)
  46. {
  47. pr_stamp();
  48. if (cmd == NAND_CMD_NONE)
  49. return;
  50. while (bfin_read_NFC_STAT() & WB_FULL)
  51. continue;
  52. if (ctrl & NAND_CLE)
  53. bfin_write_NFC_CMD(cmd);
  54. else
  55. bfin_write_NFC_ADDR(cmd);
  56. SSYNC();
  57. }
  58. int bfin_nfc_devready(struct mtd_info *mtd)
  59. {
  60. pr_stamp();
  61. return (bfin_read_NFC_STAT() & NBUSY ? 1 : 0);
  62. }
  63. /*
  64. * PIO mode for buffer writing and reading
  65. */
  66. static void bfin_nfc_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
  67. {
  68. pr_stamp();
  69. int i;
  70. /*
  71. * Data reads are requested by first writing to NFC_DATA_RD
  72. * and then reading back from NFC_READ.
  73. */
  74. for (i = 0; i < len; ++i) {
  75. while (bfin_read_NFC_STAT() & WB_FULL)
  76. if (ctrlc())
  77. return;
  78. /* Contents do not matter */
  79. bfin_write_NFC_DATA_RD(0x0000);
  80. SSYNC();
  81. while (!(bfin_read_NFC_IRQSTAT() & RD_RDY))
  82. if (ctrlc())
  83. return;
  84. buf[i] = bfin_read_NFC_READ();
  85. bfin_write_NFC_IRQSTAT(RD_RDY);
  86. }
  87. }
  88. static uint8_t bfin_nfc_read_byte(struct mtd_info *mtd)
  89. {
  90. pr_stamp();
  91. uint8_t val;
  92. bfin_nfc_read_buf(mtd, &val, 1);
  93. return val;
  94. }
  95. static void bfin_nfc_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
  96. {
  97. pr_stamp();
  98. int i;
  99. for (i = 0; i < len; ++i) {
  100. while (bfin_read_NFC_STAT() & WB_FULL)
  101. if (ctrlc())
  102. return;
  103. bfin_write_NFC_DATA_WR(buf[i]);
  104. }
  105. }
  106. /*
  107. * ECC functions
  108. * These allow the bfin to use the controller's ECC
  109. * generator block to ECC the data as it passes through
  110. */
  111. /*
  112. * ECC error correction function
  113. */
  114. static int bfin_nfc_correct_data_256(struct mtd_info *mtd, u_char *dat,
  115. u_char *read_ecc, u_char *calc_ecc)
  116. {
  117. u32 syndrome[5];
  118. u32 calced, stored;
  119. unsigned short failing_bit, failing_byte;
  120. u_char data;
  121. pr_stamp();
  122. calced = calc_ecc[0] | (calc_ecc[1] << 8) | (calc_ecc[2] << 16);
  123. stored = read_ecc[0] | (read_ecc[1] << 8) | (read_ecc[2] << 16);
  124. syndrome[0] = (calced ^ stored);
  125. /*
  126. * syndrome 0: all zero
  127. * No error in data
  128. * No action
  129. */
  130. if (!syndrome[0] || !calced || !stored)
  131. return 0;
  132. /*
  133. * sysdrome 0: only one bit is one
  134. * ECC data was incorrect
  135. * No action
  136. */
  137. if (hweight32(syndrome[0]) == 1)
  138. return 1;
  139. syndrome[1] = (calced & 0x7FF) ^ (stored & 0x7FF);
  140. syndrome[2] = (calced & 0x7FF) ^ ((calced >> 11) & 0x7FF);
  141. syndrome[3] = (stored & 0x7FF) ^ ((stored >> 11) & 0x7FF);
  142. syndrome[4] = syndrome[2] ^ syndrome[3];
  143. /*
  144. * sysdrome 0: exactly 11 bits are one, each parity
  145. * and parity' pair is 1 & 0 or 0 & 1.
  146. * 1-bit correctable error
  147. * Correct the error
  148. */
  149. if (hweight32(syndrome[0]) == 11 && syndrome[4] == 0x7FF) {
  150. failing_bit = syndrome[1] & 0x7;
  151. failing_byte = syndrome[1] >> 0x3;
  152. data = *(dat + failing_byte);
  153. data = data ^ (0x1 << failing_bit);
  154. *(dat + failing_byte) = data;
  155. return 0;
  156. }
  157. /*
  158. * sysdrome 0: random data
  159. * More than 1-bit error, non-correctable error
  160. * Discard data, mark bad block
  161. */
  162. return 1;
  163. }
  164. static int bfin_nfc_correct_data(struct mtd_info *mtd, u_char *dat,
  165. u_char *read_ecc, u_char *calc_ecc)
  166. {
  167. int ret;
  168. pr_stamp();
  169. ret = bfin_nfc_correct_data_256(mtd, dat, read_ecc, calc_ecc);
  170. /* If page size is 512, correct second 256 bytes */
  171. if (NAND_IS_512()) {
  172. dat += 256;
  173. read_ecc += 8;
  174. calc_ecc += 8;
  175. ret |= bfin_nfc_correct_data_256(mtd, dat, read_ecc, calc_ecc);
  176. }
  177. return ret;
  178. }
  179. static void reset_ecc(void)
  180. {
  181. bfin_write_NFC_RST(0x1);
  182. while (bfin_read_NFC_RST() & 1)
  183. continue;
  184. }
  185. static void bfin_nfc_enable_hwecc(struct mtd_info *mtd, int mode)
  186. {
  187. reset_ecc();
  188. }
  189. static int bfin_nfc_calculate_ecc(struct mtd_info *mtd,
  190. const u_char *dat, u_char *ecc_code)
  191. {
  192. u16 ecc0, ecc1;
  193. u32 code[2];
  194. u8 *p;
  195. pr_stamp();
  196. /* first 4 bytes ECC code for 256 page size */
  197. ecc0 = bfin_read_NFC_ECC0();
  198. ecc1 = bfin_read_NFC_ECC1();
  199. code[0] = (ecc0 & 0x7FF) | ((ecc1 & 0x7FF) << 11);
  200. /* first 3 bytes in ecc_code for 256 page size */
  201. p = (u8 *) code;
  202. memcpy(ecc_code, p, 3);
  203. /* second 4 bytes ECC code for 512 page size */
  204. if (NAND_IS_512()) {
  205. ecc0 = bfin_read_NFC_ECC2();
  206. ecc1 = bfin_read_NFC_ECC3();
  207. code[1] = (ecc0 & 0x7FF) | ((ecc1 & 0x7FF) << 11);
  208. /* second 3 bytes in ecc_code for second 256
  209. * bytes of 512 page size
  210. */
  211. p = (u8 *) (code + 1);
  212. memcpy((ecc_code + 3), p, 3);
  213. }
  214. reset_ecc();
  215. return 0;
  216. }
  217. #ifdef CONFIG_BFIN_NFC_BOOTROM_ECC
  218. # define BOOTROM_ECC 1
  219. #else
  220. # define BOOTROM_ECC 0
  221. #endif
  222. static uint8_t bbt_pattern[] = { 0xff };
  223. static struct nand_bbt_descr bootrom_bbt = {
  224. .options = 0,
  225. .offs = 63,
  226. .len = 1,
  227. .pattern = bbt_pattern,
  228. };
  229. static struct nand_ecclayout bootrom_ecclayout = {
  230. .eccbytes = 24,
  231. .eccpos = {
  232. 0x8 * 0, 0x8 * 0 + 1, 0x8 * 0 + 2,
  233. 0x8 * 1, 0x8 * 1 + 1, 0x8 * 1 + 2,
  234. 0x8 * 2, 0x8 * 2 + 1, 0x8 * 2 + 2,
  235. 0x8 * 3, 0x8 * 3 + 1, 0x8 * 3 + 2,
  236. 0x8 * 4, 0x8 * 4 + 1, 0x8 * 4 + 2,
  237. 0x8 * 5, 0x8 * 5 + 1, 0x8 * 5 + 2,
  238. 0x8 * 6, 0x8 * 6 + 1, 0x8 * 6 + 2,
  239. 0x8 * 7, 0x8 * 7 + 1, 0x8 * 7 + 2
  240. },
  241. .oobfree = {
  242. { 0x8 * 0 + 3, 5 },
  243. { 0x8 * 1 + 3, 5 },
  244. { 0x8 * 2 + 3, 5 },
  245. { 0x8 * 3 + 3, 5 },
  246. { 0x8 * 4 + 3, 5 },
  247. { 0x8 * 5 + 3, 5 },
  248. { 0x8 * 6 + 3, 5 },
  249. { 0x8 * 7 + 3, 5 },
  250. }
  251. };
  252. /*
  253. * Board-specific NAND initialization. The following members of the
  254. * argument are board-specific (per include/linux/mtd/nand.h):
  255. * - IO_ADDR_R?: address to read the 8 I/O lines of the flash device
  256. * - IO_ADDR_W?: address to write the 8 I/O lines of the flash device
  257. * - cmd_ctrl: hardwarespecific function for accesing control-lines
  258. * - dev_ready: hardwarespecific function for accesing device ready/busy line
  259. * - enable_hwecc?: function to enable (reset) hardware ecc generator. Must
  260. * only be provided if a hardware ECC is available
  261. * - ecc.mode: mode of ecc, see defines
  262. * - chip_delay: chip dependent delay for transfering data from array to
  263. * read regs (tR)
  264. * - options: various chip options. They can partly be set to inform
  265. * nand_scan about special functionality. See the defines for further
  266. * explanation
  267. * Members with a "?" were not set in the merged testing-NAND branch,
  268. * so they are not set here either.
  269. */
  270. int board_nand_init(struct nand_chip *chip)
  271. {
  272. pr_stamp();
  273. /* set width/ecc/timings/etc... */
  274. bfin_write_NFC_CTL(CONFIG_BFIN_NFC_CTL_VAL);
  275. /* clear interrupt status */
  276. bfin_write_NFC_IRQMASK(0x0);
  277. bfin_write_NFC_IRQSTAT(0xffff);
  278. /* enable GPIO function enable register */
  279. #ifdef __ADSPBF54x__
  280. bfin_write_PORTJ_FER(bfin_read_PORTJ_FER() | 6);
  281. #elif defined(__ADSPBF52x__)
  282. bfin_write_PORTH_FER(bfin_read_PORTH_FER() | 0xFCFF);
  283. bfin_write_PORTH_MUX(0);
  284. #else
  285. # error no support for this variant
  286. #endif
  287. chip->cmd_ctrl = bfin_nfc_cmd_ctrl;
  288. chip->read_buf = bfin_nfc_read_buf;
  289. chip->write_buf = bfin_nfc_write_buf;
  290. chip->read_byte = bfin_nfc_read_byte;
  291. #ifdef CONFIG_BFIN_NFC_NO_HW_ECC
  292. # define ECC_HW 0
  293. #else
  294. # define ECC_HW 1
  295. #endif
  296. if (ECC_HW) {
  297. if (BOOTROM_ECC) {
  298. chip->badblock_pattern = &bootrom_bbt;
  299. chip->ecc.layout = &bootrom_ecclayout;
  300. }
  301. if (!NAND_IS_512()) {
  302. chip->ecc.bytes = 3;
  303. chip->ecc.size = 256;
  304. } else {
  305. chip->ecc.bytes = 6;
  306. chip->ecc.size = 512;
  307. }
  308. chip->ecc.mode = NAND_ECC_HW;
  309. chip->ecc.calculate = bfin_nfc_calculate_ecc;
  310. chip->ecc.correct = bfin_nfc_correct_data;
  311. chip->ecc.hwctl = bfin_nfc_enable_hwecc;
  312. } else
  313. chip->ecc.mode = NAND_ECC_SOFT;
  314. chip->dev_ready = bfin_nfc_devready;
  315. chip->chip_delay = 0;
  316. return 0;
  317. }