bfin_nand.c 9.2 KB

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