bfin_nand.c 9.1 KB

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