bfin_nand.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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. /* Wait for the buffer to drain before we return */
  106. while (!(bfin_read_NFC_STAT() & WB_EMPTY))
  107. if (ctrlc())
  108. return;
  109. }
  110. /*
  111. * ECC functions
  112. * These allow the bfin to use the controller's ECC
  113. * generator block to ECC the data as it passes through
  114. */
  115. /*
  116. * ECC error correction function
  117. */
  118. static int bfin_nfc_correct_data_256(struct mtd_info *mtd, u_char *dat,
  119. u_char *read_ecc, u_char *calc_ecc)
  120. {
  121. u32 syndrome[5];
  122. u32 calced, stored;
  123. unsigned short failing_bit, failing_byte;
  124. u_char data;
  125. pr_stamp();
  126. calced = calc_ecc[0] | (calc_ecc[1] << 8) | (calc_ecc[2] << 16);
  127. stored = read_ecc[0] | (read_ecc[1] << 8) | (read_ecc[2] << 16);
  128. syndrome[0] = (calced ^ stored);
  129. /*
  130. * syndrome 0: all zero
  131. * No error in data
  132. * No action
  133. */
  134. if (!syndrome[0] || !calced || !stored)
  135. return 0;
  136. /*
  137. * sysdrome 0: only one bit is one
  138. * ECC data was incorrect
  139. * No action
  140. */
  141. if (hweight32(syndrome[0]) == 1)
  142. return 1;
  143. syndrome[1] = (calced & 0x7FF) ^ (stored & 0x7FF);
  144. syndrome[2] = (calced & 0x7FF) ^ ((calced >> 11) & 0x7FF);
  145. syndrome[3] = (stored & 0x7FF) ^ ((stored >> 11) & 0x7FF);
  146. syndrome[4] = syndrome[2] ^ syndrome[3];
  147. /*
  148. * sysdrome 0: exactly 11 bits are one, each parity
  149. * and parity' pair is 1 & 0 or 0 & 1.
  150. * 1-bit correctable error
  151. * Correct the error
  152. */
  153. if (hweight32(syndrome[0]) == 11 && syndrome[4] == 0x7FF) {
  154. failing_bit = syndrome[1] & 0x7;
  155. failing_byte = syndrome[1] >> 0x3;
  156. data = *(dat + failing_byte);
  157. data = data ^ (0x1 << failing_bit);
  158. *(dat + failing_byte) = data;
  159. return 0;
  160. }
  161. /*
  162. * sysdrome 0: random data
  163. * More than 1-bit error, non-correctable error
  164. * Discard data, mark bad block
  165. */
  166. return 1;
  167. }
  168. static int bfin_nfc_correct_data(struct mtd_info *mtd, u_char *dat,
  169. u_char *read_ecc, u_char *calc_ecc)
  170. {
  171. int ret;
  172. pr_stamp();
  173. ret = bfin_nfc_correct_data_256(mtd, dat, read_ecc, calc_ecc);
  174. /* If page size is 512, correct second 256 bytes */
  175. if (NAND_IS_512()) {
  176. dat += 256;
  177. read_ecc += 8;
  178. calc_ecc += 8;
  179. ret |= bfin_nfc_correct_data_256(mtd, dat, read_ecc, calc_ecc);
  180. }
  181. return ret;
  182. }
  183. static void reset_ecc(void)
  184. {
  185. bfin_write_NFC_RST(0x1);
  186. while (bfin_read_NFC_RST() & 1)
  187. continue;
  188. }
  189. static void bfin_nfc_enable_hwecc(struct mtd_info *mtd, int mode)
  190. {
  191. reset_ecc();
  192. }
  193. static int bfin_nfc_calculate_ecc(struct mtd_info *mtd,
  194. const u_char *dat, u_char *ecc_code)
  195. {
  196. u16 ecc0, ecc1;
  197. u32 code[2];
  198. u8 *p;
  199. pr_stamp();
  200. /* first 4 bytes ECC code for 256 page size */
  201. ecc0 = bfin_read_NFC_ECC0();
  202. ecc1 = bfin_read_NFC_ECC1();
  203. code[0] = (ecc0 & 0x7FF) | ((ecc1 & 0x7FF) << 11);
  204. /* first 3 bytes in ecc_code for 256 page size */
  205. p = (u8 *) code;
  206. memcpy(ecc_code, p, 3);
  207. /* second 4 bytes ECC code for 512 page size */
  208. if (NAND_IS_512()) {
  209. ecc0 = bfin_read_NFC_ECC2();
  210. ecc1 = bfin_read_NFC_ECC3();
  211. code[1] = (ecc0 & 0x7FF) | ((ecc1 & 0x7FF) << 11);
  212. /* second 3 bytes in ecc_code for second 256
  213. * bytes of 512 page size
  214. */
  215. p = (u8 *) (code + 1);
  216. memcpy((ecc_code + 3), p, 3);
  217. }
  218. reset_ecc();
  219. return 0;
  220. }
  221. #ifdef CONFIG_BFIN_NFC_BOOTROM_ECC
  222. # define BOOTROM_ECC 1
  223. #else
  224. # define BOOTROM_ECC 0
  225. #endif
  226. static uint8_t bbt_pattern[] = { 0xff };
  227. static struct nand_bbt_descr bootrom_bbt = {
  228. .options = 0,
  229. .offs = 63,
  230. .len = 1,
  231. .pattern = bbt_pattern,
  232. };
  233. static struct nand_ecclayout bootrom_ecclayout = {
  234. .eccbytes = 24,
  235. .eccpos = {
  236. 0x8 * 0, 0x8 * 0 + 1, 0x8 * 0 + 2,
  237. 0x8 * 1, 0x8 * 1 + 1, 0x8 * 1 + 2,
  238. 0x8 * 2, 0x8 * 2 + 1, 0x8 * 2 + 2,
  239. 0x8 * 3, 0x8 * 3 + 1, 0x8 * 3 + 2,
  240. 0x8 * 4, 0x8 * 4 + 1, 0x8 * 4 + 2,
  241. 0x8 * 5, 0x8 * 5 + 1, 0x8 * 5 + 2,
  242. 0x8 * 6, 0x8 * 6 + 1, 0x8 * 6 + 2,
  243. 0x8 * 7, 0x8 * 7 + 1, 0x8 * 7 + 2
  244. },
  245. .oobfree = {
  246. { 0x8 * 0 + 3, 5 },
  247. { 0x8 * 1 + 3, 5 },
  248. { 0x8 * 2 + 3, 5 },
  249. { 0x8 * 3 + 3, 5 },
  250. { 0x8 * 4 + 3, 5 },
  251. { 0x8 * 5 + 3, 5 },
  252. { 0x8 * 6 + 3, 5 },
  253. { 0x8 * 7 + 3, 5 },
  254. }
  255. };
  256. /*
  257. * Board-specific NAND initialization. The following members of the
  258. * argument are board-specific (per include/linux/mtd/nand.h):
  259. * - IO_ADDR_R?: address to read the 8 I/O lines of the flash device
  260. * - IO_ADDR_W?: address to write the 8 I/O lines of the flash device
  261. * - cmd_ctrl: hardwarespecific function for accesing control-lines
  262. * - dev_ready: hardwarespecific function for accesing device ready/busy line
  263. * - enable_hwecc?: function to enable (reset) hardware ecc generator. Must
  264. * only be provided if a hardware ECC is available
  265. * - ecc.mode: mode of ecc, see defines
  266. * - chip_delay: chip dependent delay for transfering data from array to
  267. * read regs (tR)
  268. * - options: various chip options. They can partly be set to inform
  269. * nand_scan about special functionality. See the defines for further
  270. * explanation
  271. * Members with a "?" were not set in the merged testing-NAND branch,
  272. * so they are not set here either.
  273. */
  274. int board_nand_init(struct nand_chip *chip)
  275. {
  276. pr_stamp();
  277. /* set width/ecc/timings/etc... */
  278. bfin_write_NFC_CTL(CONFIG_BFIN_NFC_CTL_VAL);
  279. /* clear interrupt status */
  280. bfin_write_NFC_IRQMASK(0x0);
  281. bfin_write_NFC_IRQSTAT(0xffff);
  282. /* enable GPIO function enable register */
  283. #ifdef __ADSPBF54x__
  284. bfin_write_PORTJ_FER(bfin_read_PORTJ_FER() | 6);
  285. #elif defined(__ADSPBF52x__)
  286. bfin_write_PORTH_FER(bfin_read_PORTH_FER() | 0xFCFF);
  287. bfin_write_PORTH_MUX(0);
  288. #else
  289. # error no support for this variant
  290. #endif
  291. chip->cmd_ctrl = bfin_nfc_cmd_ctrl;
  292. chip->read_buf = bfin_nfc_read_buf;
  293. chip->write_buf = bfin_nfc_write_buf;
  294. chip->read_byte = bfin_nfc_read_byte;
  295. #ifdef CONFIG_BFIN_NFC_NO_HW_ECC
  296. # define ECC_HW 0
  297. #else
  298. # define ECC_HW 1
  299. #endif
  300. if (ECC_HW) {
  301. if (BOOTROM_ECC) {
  302. chip->badblock_pattern = &bootrom_bbt;
  303. chip->ecc.layout = &bootrom_ecclayout;
  304. }
  305. if (!NAND_IS_512()) {
  306. chip->ecc.bytes = 3;
  307. chip->ecc.size = 256;
  308. } else {
  309. chip->ecc.bytes = 6;
  310. chip->ecc.size = 512;
  311. }
  312. chip->ecc.mode = NAND_ECC_HW;
  313. chip->ecc.calculate = bfin_nfc_calculate_ecc;
  314. chip->ecc.correct = bfin_nfc_correct_data;
  315. chip->ecc.hwctl = bfin_nfc_enable_hwecc;
  316. } else
  317. chip->ecc.mode = NAND_ECC_SOFT;
  318. chip->dev_ready = bfin_nfc_devready;
  319. chip->chip_delay = 0;
  320. return 0;
  321. }