zynq_nand.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186
  1. /*
  2. * (C) Copyright 2016 Xilinx, Inc.
  3. *
  4. * Xilinx Zynq NAND Flash Controller Driver
  5. * This driver is based on plat_nand.c and mxc_nand.c drivers
  6. *
  7. * SPDX-License-Identifier: GPL-2.0+
  8. */
  9. #include <common.h>
  10. #include <malloc.h>
  11. #include <asm/io.h>
  12. #include <linux/errno.h>
  13. #include <nand.h>
  14. #include <linux/mtd/mtd.h>
  15. #include <linux/mtd/nand.h>
  16. #include <linux/mtd/partitions.h>
  17. #include <linux/mtd/nand_ecc.h>
  18. #include <asm/arch/hardware.h>
  19. /* The NAND flash driver defines */
  20. #define ZYNQ_NAND_CMD_PHASE 1
  21. #define ZYNQ_NAND_DATA_PHASE 2
  22. #define ZYNQ_NAND_ECC_SIZE 512
  23. #define ZYNQ_NAND_SET_OPMODE_8BIT (0 << 0)
  24. #define ZYNQ_NAND_SET_OPMODE_16BIT (1 << 0)
  25. #define ZYNQ_NAND_ECC_STATUS (1 << 6)
  26. #define ZYNQ_MEMC_CLRCR_INT_CLR1 (1 << 4)
  27. #define ZYNQ_MEMC_SR_RAW_INT_ST1 (1 << 6)
  28. #define ZYNQ_MEMC_SR_INT_ST1 (1 << 4)
  29. #define ZYNQ_MEMC_NAND_ECC_MODE_MASK 0xC
  30. /* Flash memory controller operating parameters */
  31. #define ZYNQ_NAND_CLR_CONFIG ((0x1 << 1) | /* Disable interrupt */ \
  32. (0x1 << 4) | /* Clear interrupt */ \
  33. (0x1 << 6)) /* Disable ECC interrupt */
  34. /* Assuming 50MHz clock (20ns cycle time) and 3V operation */
  35. #define ZYNQ_NAND_SET_CYCLES ((0x2 << 20) | /* t_rr from nand_cycles */ \
  36. (0x2 << 17) | /* t_ar from nand_cycles */ \
  37. (0x1 << 14) | /* t_clr from nand_cycles */ \
  38. (0x3 << 11) | /* t_wp from nand_cycles */ \
  39. (0x2 << 8) | /* t_rea from nand_cycles */ \
  40. (0x5 << 4) | /* t_wc from nand_cycles */ \
  41. (0x5 << 0)) /* t_rc from nand_cycles */
  42. #define ZYNQ_NAND_DIRECT_CMD ((0x4 << 23) | /* Chip 0 from interface 1 */ \
  43. (0x2 << 21)) /* UpdateRegs operation */
  44. #define ZYNQ_NAND_ECC_CONFIG ((0x1 << 2) | /* ECC available on APB */ \
  45. (0x1 << 4) | /* ECC read at end of page */ \
  46. (0x0 << 5)) /* No Jumping */
  47. #define ZYNQ_NAND_ECC_CMD1 ((0x80) | /* Write command */ \
  48. (0x00 << 8) | /* Read command */ \
  49. (0x30 << 16) | /* Read End command */ \
  50. (0x1 << 24)) /* Read End command calid */
  51. #define ZYNQ_NAND_ECC_CMD2 ((0x85) | /* Write col change cmd */ \
  52. (0x05 << 8) | /* Read col change cmd */ \
  53. (0xE0 << 16) | /* Read col change end cmd */ \
  54. (0x1 << 24)) /* Read col change
  55. end cmd valid */
  56. /* AXI Address definitions */
  57. #define START_CMD_SHIFT 3
  58. #define END_CMD_SHIFT 11
  59. #define END_CMD_VALID_SHIFT 20
  60. #define ADDR_CYCLES_SHIFT 21
  61. #define CLEAR_CS_SHIFT 21
  62. #define ECC_LAST_SHIFT 10
  63. #define COMMAND_PHASE (0 << 19)
  64. #define DATA_PHASE (1 << 19)
  65. #define ONDIE_ECC_FEATURE_ADDR 0x90
  66. #define ONDIE_ECC_FEATURE_ENABLE 0x08
  67. #define ZYNQ_NAND_ECC_LAST (1 << ECC_LAST_SHIFT) /* Set ECC_Last */
  68. #define ZYNQ_NAND_CLEAR_CS (1 << CLEAR_CS_SHIFT) /* Clear chip select */
  69. /* ECC block registers bit position and bit mask */
  70. #define ZYNQ_NAND_ECC_BUSY (1 << 6) /* ECC block is busy */
  71. #define ZYNQ_NAND_ECC_MASK 0x00FFFFFF /* ECC value mask */
  72. /* SMC register set */
  73. struct zynq_nand_smc_regs {
  74. u32 csr; /* 0x00 */
  75. u32 reserved0[2];
  76. u32 cfr; /* 0x0C */
  77. u32 dcr; /* 0x10 */
  78. u32 scr; /* 0x14 */
  79. u32 sor; /* 0x18 */
  80. u32 reserved1[249];
  81. u32 esr; /* 0x400 */
  82. u32 emcr; /* 0x404 */
  83. u32 emcmd1r; /* 0x408 */
  84. u32 emcmd2r; /* 0x40C */
  85. u32 reserved2[2];
  86. u32 eval0r; /* 0x418 */
  87. };
  88. #define zynq_nand_smc_base ((struct zynq_nand_smc_regs __iomem *)\
  89. ZYNQ_SMC_BASEADDR)
  90. /*
  91. * struct zynq_nand_info - Defines the NAND flash driver instance
  92. * @parts: Pointer to the mtd_partition structure
  93. * @nand_base: Virtual address of the NAND flash device
  94. * @end_cmd_pending: End command is pending
  95. * @end_cmd: End command
  96. */
  97. struct zynq_nand_info {
  98. void __iomem *nand_base;
  99. u8 end_cmd_pending;
  100. u8 end_cmd;
  101. };
  102. /*
  103. * struct zynq_nand_command_format - Defines NAND flash command format
  104. * @start_cmd: First cycle command (Start command)
  105. * @end_cmd: Second cycle command (Last command)
  106. * @addr_cycles: Number of address cycles required to send the address
  107. * @end_cmd_valid: The second cycle command is valid for cmd or data phase
  108. */
  109. struct zynq_nand_command_format {
  110. u8 start_cmd;
  111. u8 end_cmd;
  112. u8 addr_cycles;
  113. u8 end_cmd_valid;
  114. };
  115. /* The NAND flash operations command format */
  116. static const struct zynq_nand_command_format zynq_nand_commands[] = {
  117. {NAND_CMD_READ0, NAND_CMD_READSTART, 5, ZYNQ_NAND_CMD_PHASE},
  118. {NAND_CMD_RNDOUT, NAND_CMD_RNDOUTSTART, 2, ZYNQ_NAND_CMD_PHASE},
  119. {NAND_CMD_READID, NAND_CMD_NONE, 1, 0},
  120. {NAND_CMD_STATUS, NAND_CMD_NONE, 0, 0},
  121. {NAND_CMD_SEQIN, NAND_CMD_PAGEPROG, 5, ZYNQ_NAND_DATA_PHASE},
  122. {NAND_CMD_RNDIN, NAND_CMD_NONE, 2, 0},
  123. {NAND_CMD_ERASE1, NAND_CMD_ERASE2, 3, ZYNQ_NAND_CMD_PHASE},
  124. {NAND_CMD_RESET, NAND_CMD_NONE, 0, 0},
  125. {NAND_CMD_PARAM, NAND_CMD_NONE, 1, 0},
  126. {NAND_CMD_GET_FEATURES, NAND_CMD_NONE, 1, 0},
  127. {NAND_CMD_SET_FEATURES, NAND_CMD_NONE, 1, 0},
  128. {NAND_CMD_NONE, NAND_CMD_NONE, 0, 0},
  129. /* Add all the flash commands supported by the flash device */
  130. };
  131. /* Define default oob placement schemes for large and small page devices */
  132. static struct nand_ecclayout nand_oob_16 = {
  133. .eccbytes = 3,
  134. .eccpos = {0, 1, 2},
  135. .oobfree = {
  136. { .offset = 8, .length = 8 }
  137. }
  138. };
  139. static struct nand_ecclayout nand_oob_64 = {
  140. .eccbytes = 12,
  141. .eccpos = {
  142. 52, 53, 54, 55, 56, 57,
  143. 58, 59, 60, 61, 62, 63},
  144. .oobfree = {
  145. { .offset = 2, .length = 50 }
  146. }
  147. };
  148. static struct nand_ecclayout ondie_nand_oob_64 = {
  149. .eccbytes = 32,
  150. .eccpos = {
  151. 8, 9, 10, 11, 12, 13, 14, 15,
  152. 24, 25, 26, 27, 28, 29, 30, 31,
  153. 40, 41, 42, 43, 44, 45, 46, 47,
  154. 56, 57, 58, 59, 60, 61, 62, 63
  155. },
  156. .oobfree = {
  157. { .offset = 4, .length = 4 },
  158. { .offset = 20, .length = 4 },
  159. { .offset = 36, .length = 4 },
  160. { .offset = 52, .length = 4 }
  161. }
  162. };
  163. /* bbt decriptors for chips with on-die ECC and
  164. chips with 64-byte OOB */
  165. static u8 bbt_pattern[] = {'B', 'b', 't', '0' };
  166. static u8 mirror_pattern[] = {'1', 't', 'b', 'B' };
  167. static struct nand_bbt_descr bbt_main_descr = {
  168. .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE |
  169. NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
  170. .offs = 4,
  171. .len = 4,
  172. .veroffs = 20,
  173. .maxblocks = 4,
  174. .pattern = bbt_pattern
  175. };
  176. static struct nand_bbt_descr bbt_mirror_descr = {
  177. .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE |
  178. NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
  179. .offs = 4,
  180. .len = 4,
  181. .veroffs = 20,
  182. .maxblocks = 4,
  183. .pattern = mirror_pattern
  184. };
  185. /*
  186. * zynq_nand_waitfor_ecc_completion - Wait for ECC completion
  187. *
  188. * returns: status for command completion, -1 for Timeout
  189. */
  190. static int zynq_nand_waitfor_ecc_completion(void)
  191. {
  192. unsigned long timeout;
  193. u32 status;
  194. /* Wait max 10us */
  195. timeout = 10;
  196. status = readl(&zynq_nand_smc_base->esr);
  197. while (status & ZYNQ_NAND_ECC_BUSY) {
  198. status = readl(&zynq_nand_smc_base->esr);
  199. if (timeout == 0)
  200. return -1;
  201. timeout--;
  202. udelay(1);
  203. }
  204. return status;
  205. }
  206. /*
  207. * zynq_nand_init_nand_flash - Initialize NAND controller
  208. * @option: Device property flags
  209. *
  210. * This function initializes the NAND flash interface on the NAND controller.
  211. *
  212. * returns: 0 on success or error value on failure
  213. */
  214. static int zynq_nand_init_nand_flash(int option)
  215. {
  216. u32 status;
  217. /* disable interrupts */
  218. writel(ZYNQ_NAND_CLR_CONFIG, &zynq_nand_smc_base->cfr);
  219. /* Initialize the NAND interface by setting cycles and operation mode */
  220. writel(ZYNQ_NAND_SET_CYCLES, &zynq_nand_smc_base->scr);
  221. if (option & NAND_BUSWIDTH_16)
  222. writel(ZYNQ_NAND_SET_OPMODE_16BIT, &zynq_nand_smc_base->sor);
  223. else
  224. writel(ZYNQ_NAND_SET_OPMODE_8BIT, &zynq_nand_smc_base->sor);
  225. writel(ZYNQ_NAND_DIRECT_CMD, &zynq_nand_smc_base->dcr);
  226. /* Wait till the ECC operation is complete */
  227. status = zynq_nand_waitfor_ecc_completion();
  228. if (status < 0) {
  229. printf("%s: Timeout\n", __func__);
  230. return status;
  231. }
  232. /* Set the command1 and command2 register */
  233. writel(ZYNQ_NAND_ECC_CMD1, &zynq_nand_smc_base->emcmd1r);
  234. writel(ZYNQ_NAND_ECC_CMD2, &zynq_nand_smc_base->emcmd2r);
  235. return 0;
  236. }
  237. /*
  238. * zynq_nand_calculate_hwecc - Calculate Hardware ECC
  239. * @mtd: Pointer to the mtd_info structure
  240. * @data: Pointer to the page data
  241. * @ecc_code: Pointer to the ECC buffer where ECC data needs to be stored
  242. *
  243. * This function retrieves the Hardware ECC data from the controller and returns
  244. * ECC data back to the MTD subsystem.
  245. *
  246. * returns: 0 on success or error value on failure
  247. */
  248. static int zynq_nand_calculate_hwecc(struct mtd_info *mtd, const u8 *data,
  249. u8 *ecc_code)
  250. {
  251. u32 ecc_value = 0;
  252. u8 ecc_reg, ecc_byte;
  253. u32 ecc_status;
  254. /* Wait till the ECC operation is complete */
  255. ecc_status = zynq_nand_waitfor_ecc_completion();
  256. if (ecc_status < 0) {
  257. printf("%s: Timeout\n", __func__);
  258. return ecc_status;
  259. }
  260. for (ecc_reg = 0; ecc_reg < 4; ecc_reg++) {
  261. /* Read ECC value for each block */
  262. ecc_value = readl(&zynq_nand_smc_base->eval0r + ecc_reg);
  263. /* Get the ecc status from ecc read value */
  264. ecc_status = (ecc_value >> 24) & 0xFF;
  265. /* ECC value valid */
  266. if (ecc_status & ZYNQ_NAND_ECC_STATUS) {
  267. for (ecc_byte = 0; ecc_byte < 3; ecc_byte++) {
  268. /* Copy ECC bytes to MTD buffer */
  269. *ecc_code = ecc_value & 0xFF;
  270. ecc_value = ecc_value >> 8;
  271. ecc_code++;
  272. }
  273. } else {
  274. debug("%s: ecc status failed\n", __func__);
  275. }
  276. }
  277. return 0;
  278. }
  279. /*
  280. * onehot - onehot function
  281. * @value: value to check for onehot
  282. *
  283. * This function checks whether a value is onehot or not.
  284. * onehot is if and only if one bit is set.
  285. *
  286. * FIXME: Try to move this in common.h
  287. */
  288. static bool onehot(unsigned short value)
  289. {
  290. bool onehot;
  291. onehot = value && !(value & (value - 1));
  292. return onehot;
  293. }
  294. /*
  295. * zynq_nand_correct_data - ECC correction function
  296. * @mtd: Pointer to the mtd_info structure
  297. * @buf: Pointer to the page data
  298. * @read_ecc: Pointer to the ECC value read from spare data area
  299. * @calc_ecc: Pointer to the calculated ECC value
  300. *
  301. * This function corrects the ECC single bit errors & detects 2-bit errors.
  302. *
  303. * returns: 0 if no ECC errors found
  304. * 1 if single bit error found and corrected.
  305. * -1 if multiple ECC errors found.
  306. */
  307. static int zynq_nand_correct_data(struct mtd_info *mtd, unsigned char *buf,
  308. unsigned char *read_ecc, unsigned char *calc_ecc)
  309. {
  310. unsigned char bit_addr;
  311. unsigned int byte_addr;
  312. unsigned short ecc_odd, ecc_even;
  313. unsigned short read_ecc_lower, read_ecc_upper;
  314. unsigned short calc_ecc_lower, calc_ecc_upper;
  315. read_ecc_lower = (read_ecc[0] | (read_ecc[1] << 8)) & 0xfff;
  316. read_ecc_upper = ((read_ecc[1] >> 4) | (read_ecc[2] << 4)) & 0xfff;
  317. calc_ecc_lower = (calc_ecc[0] | (calc_ecc[1] << 8)) & 0xfff;
  318. calc_ecc_upper = ((calc_ecc[1] >> 4) | (calc_ecc[2] << 4)) & 0xfff;
  319. ecc_odd = read_ecc_lower ^ calc_ecc_lower;
  320. ecc_even = read_ecc_upper ^ calc_ecc_upper;
  321. if ((ecc_odd == 0) && (ecc_even == 0))
  322. return 0; /* no error */
  323. if (ecc_odd == (~ecc_even & 0xfff)) {
  324. /* bits [11:3] of error code is byte offset */
  325. byte_addr = (ecc_odd >> 3) & 0x1ff;
  326. /* bits [2:0] of error code is bit offset */
  327. bit_addr = ecc_odd & 0x7;
  328. /* Toggling error bit */
  329. buf[byte_addr] ^= (1 << bit_addr);
  330. return 1;
  331. }
  332. if (onehot(ecc_odd | ecc_even))
  333. return 1; /* one error in parity */
  334. return -1; /* Uncorrectable error */
  335. }
  336. /*
  337. * zynq_nand_read_oob - [REPLACABLE] the most common OOB data read function
  338. * @mtd: mtd info structure
  339. * @chip: nand chip info structure
  340. * @page: page number to read
  341. * @sndcmd: flag whether to issue read command or not
  342. */
  343. static int zynq_nand_read_oob(struct mtd_info *mtd, struct nand_chip *chip,
  344. int page)
  345. {
  346. unsigned long data_phase_addr = 0;
  347. int data_width = 4;
  348. u8 *p;
  349. chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
  350. p = chip->oob_poi;
  351. chip->read_buf(mtd, p, (mtd->oobsize - data_width));
  352. p += mtd->oobsize - data_width;
  353. data_phase_addr = (unsigned long)chip->IO_ADDR_R;
  354. data_phase_addr |= ZYNQ_NAND_CLEAR_CS;
  355. chip->IO_ADDR_R = (void __iomem *)data_phase_addr;
  356. chip->read_buf(mtd, p, data_width);
  357. return 0;
  358. }
  359. /*
  360. * zynq_nand_write_oob - [REPLACABLE] the most common OOB data write function
  361. * @mtd: mtd info structure
  362. * @chip: nand chip info structure
  363. * @page: page number to write
  364. */
  365. static int zynq_nand_write_oob(struct mtd_info *mtd, struct nand_chip *chip,
  366. int page)
  367. {
  368. int status = 0, data_width = 4;
  369. const u8 *buf = chip->oob_poi;
  370. unsigned long data_phase_addr = 0;
  371. chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
  372. chip->write_buf(mtd, buf, (mtd->oobsize - data_width));
  373. buf += mtd->oobsize - data_width;
  374. data_phase_addr = (unsigned long)chip->IO_ADDR_W;
  375. data_phase_addr |= ZYNQ_NAND_CLEAR_CS;
  376. data_phase_addr |= (1 << END_CMD_VALID_SHIFT);
  377. chip->IO_ADDR_W = (void __iomem *)data_phase_addr;
  378. chip->write_buf(mtd, buf, data_width);
  379. /* Send command to program the OOB data */
  380. chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
  381. status = chip->waitfunc(mtd, chip);
  382. return status & NAND_STATUS_FAIL ? -EIO : 0;
  383. }
  384. /*
  385. * zynq_nand_read_page_raw - [Intern] read raw page data without ecc
  386. * @mtd: mtd info structure
  387. * @chip: nand chip info structure
  388. * @buf: buffer to store read data
  389. * @oob_required: must write chip->oob_poi to OOB
  390. * @page: page number to read
  391. */
  392. static int zynq_nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
  393. u8 *buf, int oob_required, int page)
  394. {
  395. unsigned long data_width = 4;
  396. unsigned long data_phase_addr = 0;
  397. u8 *p;
  398. chip->read_buf(mtd, buf, mtd->writesize);
  399. p = chip->oob_poi;
  400. chip->read_buf(mtd, p, (mtd->oobsize - data_width));
  401. p += (mtd->oobsize - data_width);
  402. data_phase_addr = (unsigned long)chip->IO_ADDR_R;
  403. data_phase_addr |= ZYNQ_NAND_CLEAR_CS;
  404. chip->IO_ADDR_R = (void __iomem *)data_phase_addr;
  405. chip->read_buf(mtd, p, data_width);
  406. return 0;
  407. }
  408. static int zynq_nand_read_page_raw_nooob(struct mtd_info *mtd,
  409. struct nand_chip *chip, u8 *buf, int oob_required, int page)
  410. {
  411. chip->read_buf(mtd, buf, mtd->writesize);
  412. return 0;
  413. }
  414. static int zynq_nand_read_subpage_raw(struct mtd_info *mtd,
  415. struct nand_chip *chip, u32 data_offs,
  416. u32 readlen, u8 *buf, int page)
  417. {
  418. if (data_offs != 0) {
  419. chip->cmdfunc(mtd, NAND_CMD_RNDOUT, data_offs, -1);
  420. buf += data_offs;
  421. }
  422. chip->read_buf(mtd, buf, readlen);
  423. return 0;
  424. }
  425. /*
  426. * zynq_nand_write_page_raw - [Intern] raw page write function
  427. * @mtd: mtd info structure
  428. * @chip: nand chip info structure
  429. * @buf: data buffer
  430. * @oob_required: must write chip->oob_poi to OOB
  431. */
  432. static int zynq_nand_write_page_raw(struct mtd_info *mtd,
  433. struct nand_chip *chip, const u8 *buf, int oob_required, int page)
  434. {
  435. unsigned long data_width = 4;
  436. unsigned long data_phase_addr = 0;
  437. u8 *p;
  438. chip->write_buf(mtd, buf, mtd->writesize);
  439. p = chip->oob_poi;
  440. chip->write_buf(mtd, p, (mtd->oobsize - data_width));
  441. p += (mtd->oobsize - data_width);
  442. data_phase_addr = (unsigned long)chip->IO_ADDR_W;
  443. data_phase_addr |= ZYNQ_NAND_CLEAR_CS;
  444. data_phase_addr |= (1 << END_CMD_VALID_SHIFT);
  445. chip->IO_ADDR_W = (void __iomem *)data_phase_addr;
  446. chip->write_buf(mtd, p, data_width);
  447. return 0;
  448. }
  449. /*
  450. * nand_write_page_hwecc - Hardware ECC based page write function
  451. * @mtd: Pointer to the mtd info structure
  452. * @chip: Pointer to the NAND chip info structure
  453. * @buf: Pointer to the data buffer
  454. * @oob_required: must write chip->oob_poi to OOB
  455. *
  456. * This functions writes data and hardware generated ECC values in to the page.
  457. */
  458. static int zynq_nand_write_page_hwecc(struct mtd_info *mtd,
  459. struct nand_chip *chip, const u8 *buf, int oob_required, int page)
  460. {
  461. int i, eccsteps, eccsize = chip->ecc.size;
  462. u8 *ecc_calc = chip->buffers->ecccalc;
  463. const u8 *p = buf;
  464. u32 *eccpos = chip->ecc.layout->eccpos;
  465. unsigned long data_phase_addr = 0;
  466. unsigned long data_width = 4;
  467. u8 *oob_ptr;
  468. for (eccsteps = chip->ecc.steps; (eccsteps - 1); eccsteps--) {
  469. chip->write_buf(mtd, p, eccsize);
  470. p += eccsize;
  471. }
  472. chip->write_buf(mtd, p, (eccsize - data_width));
  473. p += eccsize - data_width;
  474. /* Set ECC Last bit to 1 */
  475. data_phase_addr = (unsigned long) chip->IO_ADDR_W;
  476. data_phase_addr |= ZYNQ_NAND_ECC_LAST;
  477. chip->IO_ADDR_W = (void __iomem *)data_phase_addr;
  478. chip->write_buf(mtd, p, data_width);
  479. /* Wait for ECC to be calculated and read the error values */
  480. p = buf;
  481. chip->ecc.calculate(mtd, p, &ecc_calc[0]);
  482. for (i = 0; i < chip->ecc.total; i++)
  483. chip->oob_poi[eccpos[i]] = ~(ecc_calc[i]);
  484. /* Clear ECC last bit */
  485. data_phase_addr = (unsigned long)chip->IO_ADDR_W;
  486. data_phase_addr &= ~ZYNQ_NAND_ECC_LAST;
  487. chip->IO_ADDR_W = (void __iomem *)data_phase_addr;
  488. /* Write the spare area with ECC bytes */
  489. oob_ptr = chip->oob_poi;
  490. chip->write_buf(mtd, oob_ptr, (mtd->oobsize - data_width));
  491. data_phase_addr = (unsigned long)chip->IO_ADDR_W;
  492. data_phase_addr |= ZYNQ_NAND_CLEAR_CS;
  493. data_phase_addr |= (1 << END_CMD_VALID_SHIFT);
  494. chip->IO_ADDR_W = (void __iomem *)data_phase_addr;
  495. oob_ptr += (mtd->oobsize - data_width);
  496. chip->write_buf(mtd, oob_ptr, data_width);
  497. return 0;
  498. }
  499. /*
  500. * zynq_nand_write_page_swecc - [REPLACABLE] software ecc based page
  501. * write function
  502. * @mtd: mtd info structure
  503. * @chip: nand chip info structure
  504. * @buf: data buffer
  505. * @oob_required: must write chip->oob_poi to OOB
  506. */
  507. static int zynq_nand_write_page_swecc(struct mtd_info *mtd,
  508. struct nand_chip *chip, const u8 *buf, int oob_required, int page)
  509. {
  510. int i, eccsize = chip->ecc.size;
  511. int eccbytes = chip->ecc.bytes;
  512. int eccsteps = chip->ecc.steps;
  513. u8 *ecc_calc = chip->buffers->ecccalc;
  514. const u8 *p = buf;
  515. u32 *eccpos = chip->ecc.layout->eccpos;
  516. /* Software ecc calculation */
  517. for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
  518. chip->ecc.calculate(mtd, p, &ecc_calc[i]);
  519. for (i = 0; i < chip->ecc.total; i++)
  520. chip->oob_poi[eccpos[i]] = ecc_calc[i];
  521. return chip->ecc.write_page_raw(mtd, chip, buf, 1, page);
  522. }
  523. /*
  524. * nand_read_page_hwecc - Hardware ECC based page read function
  525. * @mtd: Pointer to the mtd info structure
  526. * @chip: Pointer to the NAND chip info structure
  527. * @buf: Pointer to the buffer to store read data
  528. * @oob_required: must write chip->oob_poi to OOB
  529. * @page: page number to read
  530. *
  531. * This functions reads data and checks the data integrity by comparing hardware
  532. * generated ECC values and read ECC values from spare area.
  533. *
  534. * returns: 0 always and updates ECC operation status in to MTD structure
  535. */
  536. static int zynq_nand_read_page_hwecc(struct mtd_info *mtd,
  537. struct nand_chip *chip, u8 *buf, int oob_required, int page)
  538. {
  539. int i, stat, eccsteps, eccsize = chip->ecc.size;
  540. int eccbytes = chip->ecc.bytes;
  541. u8 *p = buf;
  542. u8 *ecc_calc = chip->buffers->ecccalc;
  543. u8 *ecc_code = chip->buffers->ecccode;
  544. u32 *eccpos = chip->ecc.layout->eccpos;
  545. unsigned long data_phase_addr = 0;
  546. unsigned long data_width = 4;
  547. u8 *oob_ptr;
  548. for (eccsteps = chip->ecc.steps; (eccsteps - 1); eccsteps--) {
  549. chip->read_buf(mtd, p, eccsize);
  550. p += eccsize;
  551. }
  552. chip->read_buf(mtd, p, (eccsize - data_width));
  553. p += eccsize - data_width;
  554. /* Set ECC Last bit to 1 */
  555. data_phase_addr = (unsigned long)chip->IO_ADDR_R;
  556. data_phase_addr |= ZYNQ_NAND_ECC_LAST;
  557. chip->IO_ADDR_R = (void __iomem *)data_phase_addr;
  558. chip->read_buf(mtd, p, data_width);
  559. /* Read the calculated ECC value */
  560. p = buf;
  561. chip->ecc.calculate(mtd, p, &ecc_calc[0]);
  562. /* Clear ECC last bit */
  563. data_phase_addr = (unsigned long)chip->IO_ADDR_R;
  564. data_phase_addr &= ~ZYNQ_NAND_ECC_LAST;
  565. chip->IO_ADDR_R = (void __iomem *)data_phase_addr;
  566. /* Read the stored ECC value */
  567. oob_ptr = chip->oob_poi;
  568. chip->read_buf(mtd, oob_ptr, (mtd->oobsize - data_width));
  569. /* de-assert chip select */
  570. data_phase_addr = (unsigned long)chip->IO_ADDR_R;
  571. data_phase_addr |= ZYNQ_NAND_CLEAR_CS;
  572. chip->IO_ADDR_R = (void __iomem *)data_phase_addr;
  573. oob_ptr += (mtd->oobsize - data_width);
  574. chip->read_buf(mtd, oob_ptr, data_width);
  575. for (i = 0; i < chip->ecc.total; i++)
  576. ecc_code[i] = ~(chip->oob_poi[eccpos[i]]);
  577. eccsteps = chip->ecc.steps;
  578. p = buf;
  579. /* Check ECC error for all blocks and correct if it is correctable */
  580. for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
  581. stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
  582. if (stat < 0)
  583. mtd->ecc_stats.failed++;
  584. else
  585. mtd->ecc_stats.corrected += stat;
  586. }
  587. return 0;
  588. }
  589. /*
  590. * zynq_nand_read_page_swecc - [REPLACABLE] software ecc based page
  591. * read function
  592. * @mtd: mtd info structure
  593. * @chip: nand chip info structure
  594. * @buf: buffer to store read data
  595. * @page: page number to read
  596. */
  597. static int zynq_nand_read_page_swecc(struct mtd_info *mtd,
  598. struct nand_chip *chip, u8 *buf, int oob_required, int page)
  599. {
  600. int i, eccsize = chip->ecc.size;
  601. int eccbytes = chip->ecc.bytes;
  602. int eccsteps = chip->ecc.steps;
  603. u8 *p = buf;
  604. u8 *ecc_calc = chip->buffers->ecccalc;
  605. u8 *ecc_code = chip->buffers->ecccode;
  606. u32 *eccpos = chip->ecc.layout->eccpos;
  607. chip->ecc.read_page_raw(mtd, chip, buf, 1, page);
  608. for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
  609. chip->ecc.calculate(mtd, p, &ecc_calc[i]);
  610. for (i = 0; i < chip->ecc.total; i++)
  611. ecc_code[i] = chip->oob_poi[eccpos[i]];
  612. eccsteps = chip->ecc.steps;
  613. p = buf;
  614. for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
  615. int stat;
  616. stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
  617. if (stat < 0)
  618. mtd->ecc_stats.failed++;
  619. else
  620. mtd->ecc_stats.corrected += stat;
  621. }
  622. return 0;
  623. }
  624. /*
  625. * zynq_nand_select_chip - Select the flash device
  626. * @mtd: Pointer to the mtd_info structure
  627. * @chip: Chip number to be selected
  628. *
  629. * This function is empty as the NAND controller handles chip select line
  630. * internally based on the chip address passed in command and data phase.
  631. */
  632. static void zynq_nand_select_chip(struct mtd_info *mtd, int chip)
  633. {
  634. /* Not support multiple chips yet */
  635. }
  636. /*
  637. * zynq_nand_cmd_function - Send command to NAND device
  638. * @mtd: Pointer to the mtd_info structure
  639. * @command: The command to be sent to the flash device
  640. * @column: The column address for this command, -1 if none
  641. * @page_addr: The page address for this command, -1 if none
  642. */
  643. static void zynq_nand_cmd_function(struct mtd_info *mtd, unsigned int command,
  644. int column, int page_addr)
  645. {
  646. struct nand_chip *chip = mtd->priv;
  647. const struct zynq_nand_command_format *curr_cmd = NULL;
  648. struct zynq_nand_info *xnand = (struct zynq_nand_info *)chip->priv;
  649. void *cmd_addr;
  650. unsigned long cmd_data = 0;
  651. unsigned long cmd_phase_addr = 0;
  652. unsigned long data_phase_addr = 0;
  653. u8 end_cmd = 0;
  654. u8 end_cmd_valid = 0;
  655. u32 index;
  656. if (xnand->end_cmd_pending) {
  657. /* Check for end command if this command request is same as the
  658. * pending command then return
  659. */
  660. if (xnand->end_cmd == command) {
  661. xnand->end_cmd = 0;
  662. xnand->end_cmd_pending = 0;
  663. return;
  664. }
  665. }
  666. /* Emulate NAND_CMD_READOOB for large page device */
  667. if ((mtd->writesize > ZYNQ_NAND_ECC_SIZE) &&
  668. (command == NAND_CMD_READOOB)) {
  669. column += mtd->writesize;
  670. command = NAND_CMD_READ0;
  671. }
  672. /* Get the command format */
  673. for (index = 0; index < ARRAY_SIZE(zynq_nand_commands); index++)
  674. if (command == zynq_nand_commands[index].start_cmd)
  675. break;
  676. if (index == ARRAY_SIZE(zynq_nand_commands)) {
  677. printf("%s: Unsupported start cmd %02x\n", __func__, command);
  678. return;
  679. }
  680. curr_cmd = &zynq_nand_commands[index];
  681. /* Clear interrupt */
  682. writel(ZYNQ_MEMC_CLRCR_INT_CLR1, &zynq_nand_smc_base->cfr);
  683. /* Get the command phase address */
  684. if (curr_cmd->end_cmd_valid == ZYNQ_NAND_CMD_PHASE)
  685. end_cmd_valid = 1;
  686. if (curr_cmd->end_cmd == NAND_CMD_NONE)
  687. end_cmd = 0x0;
  688. else
  689. end_cmd = curr_cmd->end_cmd;
  690. cmd_phase_addr = (unsigned long)xnand->nand_base |
  691. (curr_cmd->addr_cycles << ADDR_CYCLES_SHIFT) |
  692. (end_cmd_valid << END_CMD_VALID_SHIFT) |
  693. (COMMAND_PHASE) |
  694. (end_cmd << END_CMD_SHIFT) |
  695. (curr_cmd->start_cmd << START_CMD_SHIFT);
  696. cmd_addr = (void __iomem *)cmd_phase_addr;
  697. /* Get the data phase address */
  698. end_cmd_valid = 0;
  699. data_phase_addr = (unsigned long)xnand->nand_base |
  700. (0x0 << CLEAR_CS_SHIFT) |
  701. (end_cmd_valid << END_CMD_VALID_SHIFT) |
  702. (DATA_PHASE) |
  703. (end_cmd << END_CMD_SHIFT) |
  704. (0x0 << ECC_LAST_SHIFT);
  705. chip->IO_ADDR_R = (void __iomem *)data_phase_addr;
  706. chip->IO_ADDR_W = chip->IO_ADDR_R;
  707. /* Command phase AXI Read & Write */
  708. if (column != -1 && page_addr != -1) {
  709. /* Adjust columns for 16 bit bus width */
  710. if (chip->options & NAND_BUSWIDTH_16)
  711. column >>= 1;
  712. cmd_data = column;
  713. if (mtd->writesize > ZYNQ_NAND_ECC_SIZE) {
  714. cmd_data |= page_addr << 16;
  715. /* Another address cycle for devices > 128MiB */
  716. if (chip->chipsize > (128 << 20)) {
  717. writel(cmd_data, cmd_addr);
  718. cmd_data = (page_addr >> 16);
  719. }
  720. } else {
  721. cmd_data |= page_addr << 8;
  722. }
  723. } else if (page_addr != -1) { /* Erase */
  724. cmd_data = page_addr;
  725. } else if (column != -1) { /* Change read/write column, read id etc */
  726. /* Adjust columns for 16 bit bus width */
  727. if ((chip->options & NAND_BUSWIDTH_16) &&
  728. ((command == NAND_CMD_READ0) ||
  729. (command == NAND_CMD_SEQIN) ||
  730. (command == NAND_CMD_RNDOUT) ||
  731. (command == NAND_CMD_RNDIN)))
  732. column >>= 1;
  733. cmd_data = column;
  734. }
  735. writel(cmd_data, cmd_addr);
  736. if (curr_cmd->end_cmd_valid) {
  737. xnand->end_cmd = curr_cmd->end_cmd;
  738. xnand->end_cmd_pending = 1;
  739. }
  740. ndelay(100);
  741. if ((command == NAND_CMD_READ0) ||
  742. (command == NAND_CMD_RESET) ||
  743. (command == NAND_CMD_PARAM) ||
  744. (command == NAND_CMD_GET_FEATURES))
  745. /* wait until command is processed */
  746. nand_wait_ready(mtd);
  747. }
  748. /*
  749. * zynq_nand_read_buf - read chip data into buffer
  750. * @mtd: MTD device structure
  751. * @buf: buffer to store date
  752. * @len: number of bytes to read
  753. */
  754. static void zynq_nand_read_buf(struct mtd_info *mtd, u8 *buf, int len)
  755. {
  756. struct nand_chip *chip = mtd->priv;
  757. /* Make sure that buf is 32 bit aligned */
  758. if (((unsigned long)buf & 0x3) != 0) {
  759. if (((unsigned long)buf & 0x1) != 0) {
  760. if (len) {
  761. *buf = readb(chip->IO_ADDR_R);
  762. buf += 1;
  763. len--;
  764. }
  765. }
  766. if (((unsigned long)buf & 0x3) != 0) {
  767. if (len >= 2) {
  768. *(u16 *)buf = readw(chip->IO_ADDR_R);
  769. buf += 2;
  770. len -= 2;
  771. }
  772. }
  773. }
  774. /* copy aligned data */
  775. while (len >= 4) {
  776. *(u32 *)buf = readl(chip->IO_ADDR_R);
  777. buf += 4;
  778. len -= 4;
  779. }
  780. /* mop up any remaining bytes */
  781. if (len) {
  782. if (len >= 2) {
  783. *(u16 *)buf = readw(chip->IO_ADDR_R);
  784. buf += 2;
  785. len -= 2;
  786. }
  787. if (len)
  788. *buf = readb(chip->IO_ADDR_R);
  789. }
  790. }
  791. /*
  792. * zynq_nand_write_buf - write buffer to chip
  793. * @mtd: MTD device structure
  794. * @buf: data buffer
  795. * @len: number of bytes to write
  796. */
  797. static void zynq_nand_write_buf(struct mtd_info *mtd, const u8 *buf, int len)
  798. {
  799. struct nand_chip *chip = mtd->priv;
  800. const u32 *nand = chip->IO_ADDR_W;
  801. /* Make sure that buf is 32 bit aligned */
  802. if (((unsigned long)buf & 0x3) != 0) {
  803. if (((unsigned long)buf & 0x1) != 0) {
  804. if (len) {
  805. writeb(*buf, nand);
  806. buf += 1;
  807. len--;
  808. }
  809. }
  810. if (((unsigned long)buf & 0x3) != 0) {
  811. if (len >= 2) {
  812. writew(*(u16 *)buf, nand);
  813. buf += 2;
  814. len -= 2;
  815. }
  816. }
  817. }
  818. /* copy aligned data */
  819. while (len >= 4) {
  820. writel(*(u32 *)buf, nand);
  821. buf += 4;
  822. len -= 4;
  823. }
  824. /* mop up any remaining bytes */
  825. if (len) {
  826. if (len >= 2) {
  827. writew(*(u16 *)buf, nand);
  828. buf += 2;
  829. len -= 2;
  830. }
  831. if (len)
  832. writeb(*buf, nand);
  833. }
  834. }
  835. /*
  836. * zynq_nand_device_ready - Check device ready/busy line
  837. * @mtd: Pointer to the mtd_info structure
  838. *
  839. * returns: 0 on busy or 1 on ready state
  840. */
  841. static int zynq_nand_device_ready(struct mtd_info *mtd)
  842. {
  843. u32 csr_val;
  844. csr_val = readl(&zynq_nand_smc_base->csr);
  845. /* Check the raw_int_status1 bit */
  846. if (csr_val & ZYNQ_MEMC_SR_RAW_INT_ST1) {
  847. /* Clear the interrupt condition */
  848. writel(ZYNQ_MEMC_SR_INT_ST1, &zynq_nand_smc_base->cfr);
  849. return 1;
  850. }
  851. return 0;
  852. }
  853. static int zynq_nand_init(struct nand_chip *nand_chip, int devnum)
  854. {
  855. struct zynq_nand_info *xnand;
  856. struct mtd_info *mtd;
  857. unsigned long ecc_page_size;
  858. u8 maf_id, dev_id, i;
  859. u8 get_feature[4];
  860. u8 set_feature[4] = {ONDIE_ECC_FEATURE_ENABLE, 0x00, 0x00, 0x00};
  861. unsigned long ecc_cfg;
  862. int ondie_ecc_enabled = 0;
  863. int err = -1;
  864. xnand = calloc(1, sizeof(struct zynq_nand_info));
  865. if (!xnand) {
  866. printf("%s: failed to allocate\n", __func__);
  867. goto fail;
  868. }
  869. xnand->nand_base = (void __iomem *)ZYNQ_NAND_BASEADDR;
  870. mtd = (struct mtd_info *)&nand_info[0];
  871. nand_chip->priv = xnand;
  872. mtd->priv = nand_chip;
  873. /* Set address of NAND IO lines */
  874. nand_chip->IO_ADDR_R = xnand->nand_base;
  875. nand_chip->IO_ADDR_W = xnand->nand_base;
  876. /* Set the driver entry points for MTD */
  877. nand_chip->cmdfunc = zynq_nand_cmd_function;
  878. nand_chip->dev_ready = zynq_nand_device_ready;
  879. nand_chip->select_chip = zynq_nand_select_chip;
  880. /* If we don't set this delay driver sets 20us by default */
  881. nand_chip->chip_delay = 30;
  882. /* Buffer read/write routines */
  883. nand_chip->read_buf = zynq_nand_read_buf;
  884. nand_chip->write_buf = zynq_nand_write_buf;
  885. nand_chip->bbt_options = NAND_BBT_USE_FLASH;
  886. /* Initialize the NAND flash interface on NAND controller */
  887. if (zynq_nand_init_nand_flash(nand_chip->options) < 0) {
  888. printf("%s: nand flash init failed\n", __func__);
  889. goto fail;
  890. }
  891. /* first scan to find the device and get the page size */
  892. if (nand_scan_ident(mtd, 1, NULL)) {
  893. printf("%s: nand_scan_ident failed\n", __func__);
  894. goto fail;
  895. }
  896. /* Send the command for reading device ID */
  897. nand_chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
  898. nand_chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
  899. /* Read manufacturer and device IDs */
  900. maf_id = nand_chip->read_byte(mtd);
  901. dev_id = nand_chip->read_byte(mtd);
  902. if ((maf_id == 0x2c) && ((dev_id == 0xf1) ||
  903. (dev_id == 0xa1) || (dev_id == 0xb1) ||
  904. (dev_id == 0xaa) || (dev_id == 0xba) ||
  905. (dev_id == 0xda) || (dev_id == 0xca) ||
  906. (dev_id == 0xac) || (dev_id == 0xbc) ||
  907. (dev_id == 0xdc) || (dev_id == 0xcc) ||
  908. (dev_id == 0xa3) || (dev_id == 0xb3) ||
  909. (dev_id == 0xd3) || (dev_id == 0xc3))) {
  910. nand_chip->cmdfunc(mtd, NAND_CMD_SET_FEATURES,
  911. ONDIE_ECC_FEATURE_ADDR, -1);
  912. for (i = 0; i < 4; i++)
  913. writeb(set_feature[i], nand_chip->IO_ADDR_W);
  914. /* Wait for 1us after writing data with SET_FEATURES command */
  915. ndelay(1000);
  916. nand_chip->cmdfunc(mtd, NAND_CMD_GET_FEATURES,
  917. ONDIE_ECC_FEATURE_ADDR, -1);
  918. nand_chip->read_buf(mtd, get_feature, 4);
  919. if (get_feature[0] & ONDIE_ECC_FEATURE_ENABLE) {
  920. debug("%s: OnDie ECC flash\n", __func__);
  921. ondie_ecc_enabled = 1;
  922. } else {
  923. printf("%s: Unable to detect OnDie ECC\n", __func__);
  924. }
  925. }
  926. if (ondie_ecc_enabled) {
  927. /* Bypass the controller ECC block */
  928. ecc_cfg = readl(&zynq_nand_smc_base->emcr);
  929. ecc_cfg &= ~ZYNQ_MEMC_NAND_ECC_MODE_MASK;
  930. writel(ecc_cfg, &zynq_nand_smc_base->emcr);
  931. /* The software ECC routines won't work
  932. * with the SMC controller
  933. */
  934. nand_chip->ecc.mode = NAND_ECC_HW;
  935. nand_chip->ecc.strength = 1;
  936. nand_chip->ecc.read_page = zynq_nand_read_page_raw_nooob;
  937. nand_chip->ecc.read_subpage = zynq_nand_read_subpage_raw;
  938. nand_chip->ecc.write_page = zynq_nand_write_page_raw;
  939. nand_chip->ecc.read_page_raw = zynq_nand_read_page_raw;
  940. nand_chip->ecc.write_page_raw = zynq_nand_write_page_raw;
  941. nand_chip->ecc.read_oob = zynq_nand_read_oob;
  942. nand_chip->ecc.write_oob = zynq_nand_write_oob;
  943. nand_chip->ecc.size = mtd->writesize;
  944. nand_chip->ecc.bytes = 0;
  945. /* NAND with on-die ECC supports subpage reads */
  946. nand_chip->options |= NAND_SUBPAGE_READ;
  947. /* On-Die ECC spare bytes offset 8 is used for ECC codes */
  948. if (ondie_ecc_enabled) {
  949. nand_chip->ecc.layout = &ondie_nand_oob_64;
  950. /* Use the BBT pattern descriptors */
  951. nand_chip->bbt_td = &bbt_main_descr;
  952. nand_chip->bbt_md = &bbt_mirror_descr;
  953. }
  954. } else {
  955. /* Hardware ECC generates 3 bytes ECC code for each 512 bytes */
  956. nand_chip->ecc.mode = NAND_ECC_HW;
  957. nand_chip->ecc.strength = 1;
  958. nand_chip->ecc.size = ZYNQ_NAND_ECC_SIZE;
  959. nand_chip->ecc.bytes = 3;
  960. nand_chip->ecc.calculate = zynq_nand_calculate_hwecc;
  961. nand_chip->ecc.correct = zynq_nand_correct_data;
  962. nand_chip->ecc.hwctl = NULL;
  963. nand_chip->ecc.read_page = zynq_nand_read_page_hwecc;
  964. nand_chip->ecc.write_page = zynq_nand_write_page_hwecc;
  965. nand_chip->ecc.read_page_raw = zynq_nand_read_page_raw;
  966. nand_chip->ecc.write_page_raw = zynq_nand_write_page_raw;
  967. nand_chip->ecc.read_oob = zynq_nand_read_oob;
  968. nand_chip->ecc.write_oob = zynq_nand_write_oob;
  969. switch (mtd->writesize) {
  970. case 512:
  971. ecc_page_size = 0x1;
  972. /* Set the ECC memory config register */
  973. writel((ZYNQ_NAND_ECC_CONFIG | ecc_page_size),
  974. &zynq_nand_smc_base->emcr);
  975. break;
  976. case 1024:
  977. ecc_page_size = 0x2;
  978. /* Set the ECC memory config register */
  979. writel((ZYNQ_NAND_ECC_CONFIG | ecc_page_size),
  980. &zynq_nand_smc_base->emcr);
  981. break;
  982. case 2048:
  983. ecc_page_size = 0x3;
  984. /* Set the ECC memory config register */
  985. writel((ZYNQ_NAND_ECC_CONFIG | ecc_page_size),
  986. &zynq_nand_smc_base->emcr);
  987. break;
  988. default:
  989. nand_chip->ecc.mode = NAND_ECC_SOFT;
  990. nand_chip->ecc.calculate = nand_calculate_ecc;
  991. nand_chip->ecc.correct = nand_correct_data;
  992. nand_chip->ecc.read_page = zynq_nand_read_page_swecc;
  993. nand_chip->ecc.write_page = zynq_nand_write_page_swecc;
  994. nand_chip->ecc.size = 256;
  995. break;
  996. }
  997. if (mtd->oobsize == 16)
  998. nand_chip->ecc.layout = &nand_oob_16;
  999. else if (mtd->oobsize == 64)
  1000. nand_chip->ecc.layout = &nand_oob_64;
  1001. else
  1002. printf("%s: No oob layout found\n", __func__);
  1003. }
  1004. /* Second phase scan */
  1005. if (nand_scan_tail(mtd)) {
  1006. printf("%s: nand_scan_tail failed\n", __func__);
  1007. goto fail;
  1008. }
  1009. if (nand_register(devnum, mtd))
  1010. goto fail;
  1011. return 0;
  1012. fail:
  1013. free(xnand);
  1014. return err;
  1015. }
  1016. static struct nand_chip nand_chip[CONFIG_SYS_MAX_NAND_DEVICE];
  1017. void board_nand_init(void)
  1018. {
  1019. struct nand_chip *nand = &nand_chip[0];
  1020. if (zynq_nand_init(nand, 0))
  1021. puts("ZYNQ NAND init failed\n");
  1022. }