tegra_nand.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057
  1. /*
  2. * Copyright (c) 2011 The Chromium OS Authors.
  3. * (C) Copyright 2011 NVIDIA Corporation <www.nvidia.com>
  4. * (C) Copyright 2006 Detlev Zundel, dzu@denx.de
  5. * (C) Copyright 2006 DENX Software Engineering
  6. *
  7. * See file CREDITS for list of people who contributed to this
  8. * project.
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License as
  12. * published by the Free Software Foundation; either version 2 of
  13. * the License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  23. * MA 02111-1307 USA
  24. */
  25. #include <common.h>
  26. #include <asm/io.h>
  27. #include <nand.h>
  28. #include <asm/arch/clock.h>
  29. #include <asm/arch/funcmux.h>
  30. #include <asm/arch-tegra/clk_rst.h>
  31. #include <asm/errno.h>
  32. #include <asm/gpio.h>
  33. #include <fdtdec.h>
  34. #include "tegra_nand.h"
  35. DECLARE_GLOBAL_DATA_PTR;
  36. #define NAND_CMD_TIMEOUT_MS 10
  37. #define SKIPPED_SPARE_BYTES 4
  38. /* ECC bytes to be generated for tag data */
  39. #define TAG_ECC_BYTES 4
  40. /* 64 byte oob block info for large page (== 2KB) device
  41. *
  42. * OOB flash layout for Tegra with Reed-Solomon 4 symbol correct ECC:
  43. * Skipped bytes(4)
  44. * Main area Ecc(36)
  45. * Tag data(20)
  46. * Tag data Ecc(4)
  47. *
  48. * Yaffs2 will use 16 tag bytes.
  49. */
  50. static struct nand_ecclayout eccoob = {
  51. .eccbytes = 36,
  52. .eccpos = {
  53. 4, 5, 6, 7, 8, 9, 10, 11, 12,
  54. 13, 14, 15, 16, 17, 18, 19, 20, 21,
  55. 22, 23, 24, 25, 26, 27, 28, 29, 30,
  56. 31, 32, 33, 34, 35, 36, 37, 38, 39,
  57. },
  58. .oobavail = 20,
  59. .oobfree = {
  60. {
  61. .offset = 40,
  62. .length = 20,
  63. },
  64. }
  65. };
  66. enum {
  67. ECC_OK,
  68. ECC_TAG_ERROR = 1 << 0,
  69. ECC_DATA_ERROR = 1 << 1
  70. };
  71. /* Timing parameters */
  72. enum {
  73. FDT_NAND_MAX_TRP_TREA,
  74. FDT_NAND_TWB,
  75. FDT_NAND_MAX_TCR_TAR_TRR,
  76. FDT_NAND_TWHR,
  77. FDT_NAND_MAX_TCS_TCH_TALS_TALH,
  78. FDT_NAND_TWH,
  79. FDT_NAND_TWP,
  80. FDT_NAND_TRH,
  81. FDT_NAND_TADL,
  82. FDT_NAND_TIMING_COUNT
  83. };
  84. /* Information about an attached NAND chip */
  85. struct fdt_nand {
  86. struct nand_ctlr *reg;
  87. int enabled; /* 1 to enable, 0 to disable */
  88. struct fdt_gpio_state wp_gpio; /* write-protect GPIO */
  89. s32 width; /* bit width, normally 8 */
  90. u32 timing[FDT_NAND_TIMING_COUNT];
  91. };
  92. struct nand_drv {
  93. struct nand_ctlr *reg;
  94. /*
  95. * When running in PIO mode to get READ ID bytes from register
  96. * RESP_0, we need this variable as an index to know which byte in
  97. * register RESP_0 should be read.
  98. * Because common code in nand_base.c invokes read_byte function two
  99. * times for NAND_CMD_READID.
  100. * And our controller returns 4 bytes at once in register RESP_0.
  101. */
  102. int pio_byte_index;
  103. struct fdt_nand config;
  104. };
  105. static struct nand_drv nand_ctrl;
  106. static struct mtd_info *our_mtd;
  107. static struct nand_chip nand_chip[CONFIG_SYS_MAX_NAND_DEVICE];
  108. #ifdef CONFIG_SYS_DCACHE_OFF
  109. static inline void dma_prepare(void *start, unsigned long length,
  110. int is_writing)
  111. {
  112. }
  113. #else
  114. /**
  115. * Prepare for a DMA transaction
  116. *
  117. * For a write we flush out our data. For a read we invalidate, since we
  118. * need to do this before we read from the buffer after the DMA has
  119. * completed, so may as well do it now.
  120. *
  121. * @param start Start address for DMA buffer (should be cache-aligned)
  122. * @param length Length of DMA buffer in bytes
  123. * @param is_writing 0 if reading, non-zero if writing
  124. */
  125. static void dma_prepare(void *start, unsigned long length, int is_writing)
  126. {
  127. unsigned long addr = (unsigned long)start;
  128. length = ALIGN(length, ARCH_DMA_MINALIGN);
  129. if (is_writing)
  130. flush_dcache_range(addr, addr + length);
  131. else
  132. invalidate_dcache_range(addr, addr + length);
  133. }
  134. #endif
  135. /**
  136. * Wait for command completion
  137. *
  138. * @param reg nand_ctlr structure
  139. * @return
  140. * 1 - Command completed
  141. * 0 - Timeout
  142. */
  143. static int nand_waitfor_cmd_completion(struct nand_ctlr *reg)
  144. {
  145. u32 reg_val;
  146. int running;
  147. int i;
  148. for (i = 0; i < NAND_CMD_TIMEOUT_MS * 1000; i++) {
  149. if ((readl(&reg->command) & CMD_GO) ||
  150. !(readl(&reg->status) & STATUS_RBSY0) ||
  151. !(readl(&reg->isr) & ISR_IS_CMD_DONE)) {
  152. udelay(1);
  153. continue;
  154. }
  155. reg_val = readl(&reg->dma_mst_ctrl);
  156. /*
  157. * If DMA_MST_CTRL_EN_A_ENABLE or DMA_MST_CTRL_EN_B_ENABLE
  158. * is set, that means DMA engine is running.
  159. *
  160. * Then we have to wait until DMA_MST_CTRL_IS_DMA_DONE
  161. * is cleared, indicating DMA transfer completion.
  162. */
  163. running = reg_val & (DMA_MST_CTRL_EN_A_ENABLE |
  164. DMA_MST_CTRL_EN_B_ENABLE);
  165. if (!running || (reg_val & DMA_MST_CTRL_IS_DMA_DONE))
  166. return 1;
  167. udelay(1);
  168. }
  169. return 0;
  170. }
  171. /**
  172. * Read one byte from the chip
  173. *
  174. * @param mtd MTD device structure
  175. * @return data byte
  176. *
  177. * Read function for 8bit bus-width
  178. */
  179. static uint8_t read_byte(struct mtd_info *mtd)
  180. {
  181. struct nand_chip *chip = mtd->priv;
  182. u32 dword_read;
  183. struct nand_drv *info;
  184. info = (struct nand_drv *)chip->priv;
  185. /* In PIO mode, only 4 bytes can be transferred with single CMD_GO. */
  186. if (info->pio_byte_index > 3) {
  187. info->pio_byte_index = 0;
  188. writel(CMD_GO | CMD_PIO
  189. | CMD_RX | CMD_CE0,
  190. &info->reg->command);
  191. if (!nand_waitfor_cmd_completion(info->reg))
  192. printf("Command timeout\n");
  193. }
  194. dword_read = readl(&info->reg->resp);
  195. dword_read = dword_read >> (8 * info->pio_byte_index);
  196. info->pio_byte_index++;
  197. return (uint8_t)dword_read;
  198. }
  199. /**
  200. * Read len bytes from the chip into a buffer
  201. *
  202. * @param mtd MTD device structure
  203. * @param buf buffer to store data to
  204. * @param len number of bytes to read
  205. *
  206. * Read function for 8bit bus-width
  207. */
  208. static void read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
  209. {
  210. int i, s;
  211. unsigned int reg;
  212. struct nand_chip *chip = mtd->priv;
  213. struct nand_drv *info = (struct nand_drv *)chip->priv;
  214. for (i = 0; i < len; i += 4) {
  215. s = (len - i) > 4 ? 4 : len - i;
  216. writel(CMD_PIO | CMD_RX | CMD_A_VALID | CMD_CE0 |
  217. ((s - 1) << CMD_TRANS_SIZE_SHIFT) | CMD_GO,
  218. &info->reg->command);
  219. if (!nand_waitfor_cmd_completion(info->reg))
  220. puts("Command timeout during read_buf\n");
  221. reg = readl(&info->reg->resp);
  222. memcpy(buf + i, &reg, s);
  223. }
  224. }
  225. /**
  226. * Check NAND status to see if it is ready or not
  227. *
  228. * @param mtd MTD device structure
  229. * @return
  230. * 1 - ready
  231. * 0 - not ready
  232. */
  233. static int nand_dev_ready(struct mtd_info *mtd)
  234. {
  235. struct nand_chip *chip = mtd->priv;
  236. int reg_val;
  237. struct nand_drv *info;
  238. info = (struct nand_drv *)chip->priv;
  239. reg_val = readl(&info->reg->status);
  240. if (reg_val & STATUS_RBSY0)
  241. return 1;
  242. else
  243. return 0;
  244. }
  245. /* Dummy implementation: we don't support multiple chips */
  246. static void nand_select_chip(struct mtd_info *mtd, int chipnr)
  247. {
  248. switch (chipnr) {
  249. case -1:
  250. case 0:
  251. break;
  252. default:
  253. BUG();
  254. }
  255. }
  256. /**
  257. * Clear all interrupt status bits
  258. *
  259. * @param reg nand_ctlr structure
  260. */
  261. static void nand_clear_interrupt_status(struct nand_ctlr *reg)
  262. {
  263. u32 reg_val;
  264. /* Clear interrupt status */
  265. reg_val = readl(&reg->isr);
  266. writel(reg_val, &reg->isr);
  267. }
  268. /**
  269. * Send command to NAND device
  270. *
  271. * @param mtd MTD device structure
  272. * @param command the command to be sent
  273. * @param column the column address for this command, -1 if none
  274. * @param page_addr the page address for this command, -1 if none
  275. */
  276. static void nand_command(struct mtd_info *mtd, unsigned int command,
  277. int column, int page_addr)
  278. {
  279. struct nand_chip *chip = mtd->priv;
  280. struct nand_drv *info;
  281. info = (struct nand_drv *)chip->priv;
  282. /*
  283. * Write out the command to the device.
  284. *
  285. * Only command NAND_CMD_RESET or NAND_CMD_READID will come
  286. * here before mtd->writesize is initialized.
  287. */
  288. /* Emulate NAND_CMD_READOOB */
  289. if (command == NAND_CMD_READOOB) {
  290. assert(mtd->writesize != 0);
  291. column += mtd->writesize;
  292. command = NAND_CMD_READ0;
  293. }
  294. /* Adjust columns for 16 bit bus-width */
  295. if (column != -1 && (chip->options & NAND_BUSWIDTH_16))
  296. column >>= 1;
  297. nand_clear_interrupt_status(info->reg);
  298. /* Stop DMA engine, clear DMA completion status */
  299. writel(DMA_MST_CTRL_EN_A_DISABLE
  300. | DMA_MST_CTRL_EN_B_DISABLE
  301. | DMA_MST_CTRL_IS_DMA_DONE,
  302. &info->reg->dma_mst_ctrl);
  303. /*
  304. * Program and erase have their own busy handlers
  305. * status and sequential in needs no delay
  306. */
  307. switch (command) {
  308. case NAND_CMD_READID:
  309. writel(NAND_CMD_READID, &info->reg->cmd_reg1);
  310. writel(column & 0xFF, &info->reg->addr_reg1);
  311. writel(CMD_GO | CMD_CLE | CMD_ALE | CMD_PIO
  312. | CMD_RX |
  313. ((4 - 1) << CMD_TRANS_SIZE_SHIFT)
  314. | CMD_CE0,
  315. &info->reg->command);
  316. info->pio_byte_index = 0;
  317. break;
  318. case NAND_CMD_PARAM:
  319. writel(NAND_CMD_PARAM, &info->reg->cmd_reg1);
  320. writel(column & 0xFF, &info->reg->addr_reg1);
  321. writel(CMD_GO | CMD_CLE | CMD_ALE | CMD_CE0,
  322. &info->reg->command);
  323. break;
  324. case NAND_CMD_READ0:
  325. writel(NAND_CMD_READ0, &info->reg->cmd_reg1);
  326. writel(NAND_CMD_READSTART, &info->reg->cmd_reg2);
  327. writel((page_addr << 16) | (column & 0xFFFF),
  328. &info->reg->addr_reg1);
  329. writel(page_addr >> 16, &info->reg->addr_reg2);
  330. return;
  331. case NAND_CMD_SEQIN:
  332. writel(NAND_CMD_SEQIN, &info->reg->cmd_reg1);
  333. writel(NAND_CMD_PAGEPROG, &info->reg->cmd_reg2);
  334. writel((page_addr << 16) | (column & 0xFFFF),
  335. &info->reg->addr_reg1);
  336. writel(page_addr >> 16,
  337. &info->reg->addr_reg2);
  338. return;
  339. case NAND_CMD_PAGEPROG:
  340. return;
  341. case NAND_CMD_ERASE1:
  342. writel(NAND_CMD_ERASE1, &info->reg->cmd_reg1);
  343. writel(NAND_CMD_ERASE2, &info->reg->cmd_reg2);
  344. writel(page_addr, &info->reg->addr_reg1);
  345. writel(CMD_GO | CMD_CLE | CMD_ALE |
  346. CMD_SEC_CMD | CMD_CE0 | CMD_ALE_BYTES3,
  347. &info->reg->command);
  348. break;
  349. case NAND_CMD_ERASE2:
  350. return;
  351. case NAND_CMD_STATUS:
  352. writel(NAND_CMD_STATUS, &info->reg->cmd_reg1);
  353. writel(CMD_GO | CMD_CLE | CMD_PIO | CMD_RX
  354. | ((1 - 0) << CMD_TRANS_SIZE_SHIFT)
  355. | CMD_CE0,
  356. &info->reg->command);
  357. info->pio_byte_index = 0;
  358. break;
  359. case NAND_CMD_RESET:
  360. writel(NAND_CMD_RESET, &info->reg->cmd_reg1);
  361. writel(CMD_GO | CMD_CLE | CMD_CE0,
  362. &info->reg->command);
  363. break;
  364. case NAND_CMD_RNDOUT:
  365. default:
  366. printf("%s: Unsupported command %d\n", __func__, command);
  367. return;
  368. }
  369. if (!nand_waitfor_cmd_completion(info->reg))
  370. printf("Command 0x%02X timeout\n", command);
  371. }
  372. /**
  373. * Check whether the pointed buffer are all 0xff (blank).
  374. *
  375. * @param buf data buffer for blank check
  376. * @param len length of the buffer in byte
  377. * @return
  378. * 1 - blank
  379. * 0 - non-blank
  380. */
  381. static int blank_check(u8 *buf, int len)
  382. {
  383. int i;
  384. for (i = 0; i < len; i++)
  385. if (buf[i] != 0xFF)
  386. return 0;
  387. return 1;
  388. }
  389. /**
  390. * After a DMA transfer for read, we call this function to see whether there
  391. * is any uncorrectable error on the pointed data buffer or oob buffer.
  392. *
  393. * @param reg nand_ctlr structure
  394. * @param databuf data buffer
  395. * @param a_len data buffer length
  396. * @param oobbuf oob buffer
  397. * @param b_len oob buffer length
  398. * @return
  399. * ECC_OK - no ECC error or correctable ECC error
  400. * ECC_TAG_ERROR - uncorrectable tag ECC error
  401. * ECC_DATA_ERROR - uncorrectable data ECC error
  402. * ECC_DATA_ERROR + ECC_TAG_ERROR - uncorrectable data+tag ECC error
  403. */
  404. static int check_ecc_error(struct nand_ctlr *reg, u8 *databuf,
  405. int a_len, u8 *oobbuf, int b_len)
  406. {
  407. int return_val = ECC_OK;
  408. u32 reg_val;
  409. if (!(readl(&reg->isr) & ISR_IS_ECC_ERR))
  410. return ECC_OK;
  411. /*
  412. * Area A is used for the data block (databuf). Area B is used for
  413. * the spare block (oobbuf)
  414. */
  415. reg_val = readl(&reg->dec_status);
  416. if ((reg_val & DEC_STATUS_A_ECC_FAIL) && databuf) {
  417. reg_val = readl(&reg->bch_dec_status_buf);
  418. /*
  419. * If uncorrectable error occurs on data area, then see whether
  420. * they are all FF. If all are FF, it's a blank page.
  421. * Not error.
  422. */
  423. if ((reg_val & BCH_DEC_STATUS_FAIL_SEC_FLAG_MASK) &&
  424. !blank_check(databuf, a_len))
  425. return_val |= ECC_DATA_ERROR;
  426. }
  427. if ((reg_val & DEC_STATUS_B_ECC_FAIL) && oobbuf) {
  428. reg_val = readl(&reg->bch_dec_status_buf);
  429. /*
  430. * If uncorrectable error occurs on tag area, then see whether
  431. * they are all FF. If all are FF, it's a blank page.
  432. * Not error.
  433. */
  434. if ((reg_val & BCH_DEC_STATUS_FAIL_TAG_MASK) &&
  435. !blank_check(oobbuf, b_len))
  436. return_val |= ECC_TAG_ERROR;
  437. }
  438. return return_val;
  439. }
  440. /**
  441. * Set GO bit to send command to device
  442. *
  443. * @param reg nand_ctlr structure
  444. */
  445. static void start_command(struct nand_ctlr *reg)
  446. {
  447. u32 reg_val;
  448. reg_val = readl(&reg->command);
  449. reg_val |= CMD_GO;
  450. writel(reg_val, &reg->command);
  451. }
  452. /**
  453. * Clear command GO bit, DMA GO bit, and DMA completion status
  454. *
  455. * @param reg nand_ctlr structure
  456. */
  457. static void stop_command(struct nand_ctlr *reg)
  458. {
  459. /* Stop command */
  460. writel(0, &reg->command);
  461. /* Stop DMA engine and clear DMA completion status */
  462. writel(DMA_MST_CTRL_GO_DISABLE
  463. | DMA_MST_CTRL_IS_DMA_DONE,
  464. &reg->dma_mst_ctrl);
  465. }
  466. /**
  467. * Set up NAND bus width and page size
  468. *
  469. * @param info nand_info structure
  470. * @param *reg_val address of reg_val
  471. * @return 0 if ok, -1 on error
  472. */
  473. static int set_bus_width_page_size(struct fdt_nand *config,
  474. u32 *reg_val)
  475. {
  476. if (config->width == 8)
  477. *reg_val = CFG_BUS_WIDTH_8BIT;
  478. else if (config->width == 16)
  479. *reg_val = CFG_BUS_WIDTH_16BIT;
  480. else {
  481. debug("%s: Unsupported bus width %d\n", __func__,
  482. config->width);
  483. return -1;
  484. }
  485. if (our_mtd->writesize == 512)
  486. *reg_val |= CFG_PAGE_SIZE_512;
  487. else if (our_mtd->writesize == 2048)
  488. *reg_val |= CFG_PAGE_SIZE_2048;
  489. else if (our_mtd->writesize == 4096)
  490. *reg_val |= CFG_PAGE_SIZE_4096;
  491. else {
  492. debug("%s: Unsupported page size %d\n", __func__,
  493. our_mtd->writesize);
  494. return -1;
  495. }
  496. return 0;
  497. }
  498. /**
  499. * Page read/write function
  500. *
  501. * @param mtd mtd info structure
  502. * @param chip nand chip info structure
  503. * @param buf data buffer
  504. * @param page page number
  505. * @param with_ecc 1 to enable ECC, 0 to disable ECC
  506. * @param is_writing 0 for read, 1 for write
  507. * @return 0 when successfully completed
  508. * -EIO when command timeout
  509. */
  510. static int nand_rw_page(struct mtd_info *mtd, struct nand_chip *chip,
  511. uint8_t *buf, int page, int with_ecc, int is_writing)
  512. {
  513. u32 reg_val;
  514. int tag_size;
  515. struct nand_oobfree *free = chip->ecc.layout->oobfree;
  516. /* 4*128=512 (byte) is the value that our HW can support. */
  517. ALLOC_CACHE_ALIGN_BUFFER(u32, tag_buf, 128);
  518. char *tag_ptr;
  519. struct nand_drv *info;
  520. struct fdt_nand *config;
  521. if ((uintptr_t)buf & 0x03) {
  522. printf("buf %p has to be 4-byte aligned\n", buf);
  523. return -EINVAL;
  524. }
  525. info = (struct nand_drv *)chip->priv;
  526. config = &info->config;
  527. if (set_bus_width_page_size(config, &reg_val))
  528. return -EINVAL;
  529. /* Need to be 4-byte aligned */
  530. tag_ptr = (char *)tag_buf;
  531. stop_command(info->reg);
  532. writel((1 << chip->page_shift) - 1, &info->reg->dma_cfg_a);
  533. writel(virt_to_phys(buf), &info->reg->data_block_ptr);
  534. if (with_ecc) {
  535. writel(virt_to_phys(tag_ptr), &info->reg->tag_ptr);
  536. if (is_writing)
  537. memcpy(tag_ptr, chip->oob_poi + free->offset,
  538. chip->ecc.layout->oobavail +
  539. TAG_ECC_BYTES);
  540. } else {
  541. writel(virt_to_phys(chip->oob_poi), &info->reg->tag_ptr);
  542. }
  543. /* Set ECC selection, configure ECC settings */
  544. if (with_ecc) {
  545. tag_size = chip->ecc.layout->oobavail + TAG_ECC_BYTES;
  546. reg_val |= (CFG_SKIP_SPARE_SEL_4
  547. | CFG_SKIP_SPARE_ENABLE
  548. | CFG_HW_ECC_CORRECTION_ENABLE
  549. | CFG_ECC_EN_TAG_DISABLE
  550. | CFG_HW_ECC_SEL_RS
  551. | CFG_HW_ECC_ENABLE
  552. | CFG_TVAL4
  553. | (tag_size - 1));
  554. if (!is_writing)
  555. tag_size += SKIPPED_SPARE_BYTES;
  556. dma_prepare(tag_ptr, tag_size, is_writing);
  557. } else {
  558. tag_size = mtd->oobsize;
  559. reg_val |= (CFG_SKIP_SPARE_DISABLE
  560. | CFG_HW_ECC_CORRECTION_DISABLE
  561. | CFG_ECC_EN_TAG_DISABLE
  562. | CFG_HW_ECC_DISABLE
  563. | (tag_size - 1));
  564. dma_prepare(chip->oob_poi, tag_size, is_writing);
  565. }
  566. writel(reg_val, &info->reg->config);
  567. dma_prepare(buf, 1 << chip->page_shift, is_writing);
  568. writel(BCH_CONFIG_BCH_ECC_DISABLE, &info->reg->bch_config);
  569. writel(tag_size - 1, &info->reg->dma_cfg_b);
  570. nand_clear_interrupt_status(info->reg);
  571. reg_val = CMD_CLE | CMD_ALE
  572. | CMD_SEC_CMD
  573. | (CMD_ALE_BYTES5 << CMD_ALE_BYTE_SIZE_SHIFT)
  574. | CMD_A_VALID
  575. | CMD_B_VALID
  576. | (CMD_TRANS_SIZE_PAGE << CMD_TRANS_SIZE_SHIFT)
  577. | CMD_CE0;
  578. if (!is_writing)
  579. reg_val |= (CMD_AFT_DAT_DISABLE | CMD_RX);
  580. else
  581. reg_val |= (CMD_AFT_DAT_ENABLE | CMD_TX);
  582. writel(reg_val, &info->reg->command);
  583. /* Setup DMA engine */
  584. reg_val = DMA_MST_CTRL_GO_ENABLE
  585. | DMA_MST_CTRL_BURST_8WORDS
  586. | DMA_MST_CTRL_EN_A_ENABLE
  587. | DMA_MST_CTRL_EN_B_ENABLE;
  588. if (!is_writing)
  589. reg_val |= DMA_MST_CTRL_DIR_READ;
  590. else
  591. reg_val |= DMA_MST_CTRL_DIR_WRITE;
  592. writel(reg_val, &info->reg->dma_mst_ctrl);
  593. start_command(info->reg);
  594. if (!nand_waitfor_cmd_completion(info->reg)) {
  595. if (!is_writing)
  596. printf("Read Page 0x%X timeout ", page);
  597. else
  598. printf("Write Page 0x%X timeout ", page);
  599. if (with_ecc)
  600. printf("with ECC");
  601. else
  602. printf("without ECC");
  603. printf("\n");
  604. return -EIO;
  605. }
  606. if (with_ecc && !is_writing) {
  607. memcpy(chip->oob_poi, tag_ptr,
  608. SKIPPED_SPARE_BYTES);
  609. memcpy(chip->oob_poi + free->offset,
  610. tag_ptr + SKIPPED_SPARE_BYTES,
  611. chip->ecc.layout->oobavail);
  612. reg_val = (u32)check_ecc_error(info->reg, (u8 *)buf,
  613. 1 << chip->page_shift,
  614. (u8 *)(tag_ptr + SKIPPED_SPARE_BYTES),
  615. chip->ecc.layout->oobavail);
  616. if (reg_val & ECC_TAG_ERROR)
  617. printf("Read Page 0x%X tag ECC error\n", page);
  618. if (reg_val & ECC_DATA_ERROR)
  619. printf("Read Page 0x%X data ECC error\n",
  620. page);
  621. if (reg_val & (ECC_DATA_ERROR | ECC_TAG_ERROR))
  622. return -EIO;
  623. }
  624. return 0;
  625. }
  626. /**
  627. * Hardware ecc based page read function
  628. *
  629. * @param mtd mtd info structure
  630. * @param chip nand chip info structure
  631. * @param buf buffer to store read data
  632. * @param page page number to read
  633. * @return 0 when successfully completed
  634. * -EIO when command timeout
  635. */
  636. static int nand_read_page_hwecc(struct mtd_info *mtd,
  637. struct nand_chip *chip, uint8_t *buf, int oob_required, int page)
  638. {
  639. return nand_rw_page(mtd, chip, buf, page, 1, 0);
  640. }
  641. /**
  642. * Hardware ecc based page write function
  643. *
  644. * @param mtd mtd info structure
  645. * @param chip nand chip info structure
  646. * @param buf data buffer
  647. */
  648. static int nand_write_page_hwecc(struct mtd_info *mtd,
  649. struct nand_chip *chip, const uint8_t *buf, int oob_required)
  650. {
  651. int page;
  652. struct nand_drv *info;
  653. info = (struct nand_drv *)chip->priv;
  654. page = (readl(&info->reg->addr_reg1) >> 16) |
  655. (readl(&info->reg->addr_reg2) << 16);
  656. nand_rw_page(mtd, chip, (uint8_t *)buf, page, 1, 1);
  657. return 0;
  658. }
  659. /**
  660. * Read raw page data without ecc
  661. *
  662. * @param mtd mtd info structure
  663. * @param chip nand chip info structure
  664. * @param buf buffer to store read data
  665. * @param page page number to read
  666. * @return 0 when successfully completed
  667. * -EINVAL when chip->oob_poi is not double-word aligned
  668. * -EIO when command timeout
  669. */
  670. static int nand_read_page_raw(struct mtd_info *mtd,
  671. struct nand_chip *chip, uint8_t *buf, int oob_required, int page)
  672. {
  673. return nand_rw_page(mtd, chip, buf, page, 0, 0);
  674. }
  675. /**
  676. * Raw page write function
  677. *
  678. * @param mtd mtd info structure
  679. * @param chip nand chip info structure
  680. * @param buf data buffer
  681. */
  682. static int nand_write_page_raw(struct mtd_info *mtd,
  683. struct nand_chip *chip, const uint8_t *buf, int oob_required)
  684. {
  685. int page;
  686. struct nand_drv *info;
  687. info = (struct nand_drv *)chip->priv;
  688. page = (readl(&info->reg->addr_reg1) >> 16) |
  689. (readl(&info->reg->addr_reg2) << 16);
  690. nand_rw_page(mtd, chip, (uint8_t *)buf, page, 0, 1);
  691. return 0;
  692. }
  693. /**
  694. * OOB data read/write function
  695. *
  696. * @param mtd mtd info structure
  697. * @param chip nand chip info structure
  698. * @param page page number to read
  699. * @param with_ecc 1 to enable ECC, 0 to disable ECC
  700. * @param is_writing 0 for read, 1 for write
  701. * @return 0 when successfully completed
  702. * -EINVAL when chip->oob_poi is not double-word aligned
  703. * -EIO when command timeout
  704. */
  705. static int nand_rw_oob(struct mtd_info *mtd, struct nand_chip *chip,
  706. int page, int with_ecc, int is_writing)
  707. {
  708. u32 reg_val;
  709. int tag_size;
  710. struct nand_oobfree *free = chip->ecc.layout->oobfree;
  711. struct nand_drv *info;
  712. if (((int)chip->oob_poi) & 0x03)
  713. return -EINVAL;
  714. info = (struct nand_drv *)chip->priv;
  715. if (set_bus_width_page_size(&info->config, &reg_val))
  716. return -EINVAL;
  717. stop_command(info->reg);
  718. writel(virt_to_phys(chip->oob_poi), &info->reg->tag_ptr);
  719. /* Set ECC selection */
  720. tag_size = mtd->oobsize;
  721. if (with_ecc)
  722. reg_val |= CFG_ECC_EN_TAG_ENABLE;
  723. else
  724. reg_val |= (CFG_ECC_EN_TAG_DISABLE);
  725. reg_val |= ((tag_size - 1) |
  726. CFG_SKIP_SPARE_DISABLE |
  727. CFG_HW_ECC_CORRECTION_DISABLE |
  728. CFG_HW_ECC_DISABLE);
  729. writel(reg_val, &info->reg->config);
  730. dma_prepare(chip->oob_poi, tag_size, is_writing);
  731. writel(BCH_CONFIG_BCH_ECC_DISABLE, &info->reg->bch_config);
  732. if (is_writing && with_ecc)
  733. tag_size -= TAG_ECC_BYTES;
  734. writel(tag_size - 1, &info->reg->dma_cfg_b);
  735. nand_clear_interrupt_status(info->reg);
  736. reg_val = CMD_CLE | CMD_ALE
  737. | CMD_SEC_CMD
  738. | (CMD_ALE_BYTES5 << CMD_ALE_BYTE_SIZE_SHIFT)
  739. | CMD_B_VALID
  740. | CMD_CE0;
  741. if (!is_writing)
  742. reg_val |= (CMD_AFT_DAT_DISABLE | CMD_RX);
  743. else
  744. reg_val |= (CMD_AFT_DAT_ENABLE | CMD_TX);
  745. writel(reg_val, &info->reg->command);
  746. /* Setup DMA engine */
  747. reg_val = DMA_MST_CTRL_GO_ENABLE
  748. | DMA_MST_CTRL_BURST_8WORDS
  749. | DMA_MST_CTRL_EN_B_ENABLE;
  750. if (!is_writing)
  751. reg_val |= DMA_MST_CTRL_DIR_READ;
  752. else
  753. reg_val |= DMA_MST_CTRL_DIR_WRITE;
  754. writel(reg_val, &info->reg->dma_mst_ctrl);
  755. start_command(info->reg);
  756. if (!nand_waitfor_cmd_completion(info->reg)) {
  757. if (!is_writing)
  758. printf("Read OOB of Page 0x%X timeout\n", page);
  759. else
  760. printf("Write OOB of Page 0x%X timeout\n", page);
  761. return -EIO;
  762. }
  763. if (with_ecc && !is_writing) {
  764. reg_val = (u32)check_ecc_error(info->reg, 0, 0,
  765. (u8 *)(chip->oob_poi + free->offset),
  766. chip->ecc.layout->oobavail);
  767. if (reg_val & ECC_TAG_ERROR)
  768. printf("Read OOB of Page 0x%X tag ECC error\n", page);
  769. }
  770. return 0;
  771. }
  772. /**
  773. * OOB data read function
  774. *
  775. * @param mtd mtd info structure
  776. * @param chip nand chip info structure
  777. * @param page page number to read
  778. */
  779. static int nand_read_oob(struct mtd_info *mtd, struct nand_chip *chip,
  780. int page)
  781. {
  782. chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
  783. nand_rw_oob(mtd, chip, page, 0, 0);
  784. return 0;
  785. }
  786. /**
  787. * OOB data write function
  788. *
  789. * @param mtd mtd info structure
  790. * @param chip nand chip info structure
  791. * @param page page number to write
  792. * @return 0 when successfully completed
  793. * -EINVAL when chip->oob_poi is not double-word aligned
  794. * -EIO when command timeout
  795. */
  796. static int nand_write_oob(struct mtd_info *mtd, struct nand_chip *chip,
  797. int page)
  798. {
  799. chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
  800. return nand_rw_oob(mtd, chip, page, 0, 1);
  801. }
  802. /**
  803. * Set up NAND memory timings according to the provided parameters
  804. *
  805. * @param timing Timing parameters
  806. * @param reg NAND controller register address
  807. */
  808. static void setup_timing(unsigned timing[FDT_NAND_TIMING_COUNT],
  809. struct nand_ctlr *reg)
  810. {
  811. u32 reg_val, clk_rate, clk_period, time_val;
  812. clk_rate = (u32)clock_get_periph_rate(PERIPH_ID_NDFLASH,
  813. CLOCK_ID_PERIPH) / 1000000;
  814. clk_period = 1000 / clk_rate;
  815. reg_val = ((timing[FDT_NAND_MAX_TRP_TREA] / clk_period) <<
  816. TIMING_TRP_RESP_CNT_SHIFT) & TIMING_TRP_RESP_CNT_MASK;
  817. reg_val |= ((timing[FDT_NAND_TWB] / clk_period) <<
  818. TIMING_TWB_CNT_SHIFT) & TIMING_TWB_CNT_MASK;
  819. time_val = timing[FDT_NAND_MAX_TCR_TAR_TRR] / clk_period;
  820. if (time_val > 2)
  821. reg_val |= ((time_val - 2) << TIMING_TCR_TAR_TRR_CNT_SHIFT) &
  822. TIMING_TCR_TAR_TRR_CNT_MASK;
  823. reg_val |= ((timing[FDT_NAND_TWHR] / clk_period) <<
  824. TIMING_TWHR_CNT_SHIFT) & TIMING_TWHR_CNT_MASK;
  825. time_val = timing[FDT_NAND_MAX_TCS_TCH_TALS_TALH] / clk_period;
  826. if (time_val > 1)
  827. reg_val |= ((time_val - 1) << TIMING_TCS_CNT_SHIFT) &
  828. TIMING_TCS_CNT_MASK;
  829. reg_val |= ((timing[FDT_NAND_TWH] / clk_period) <<
  830. TIMING_TWH_CNT_SHIFT) & TIMING_TWH_CNT_MASK;
  831. reg_val |= ((timing[FDT_NAND_TWP] / clk_period) <<
  832. TIMING_TWP_CNT_SHIFT) & TIMING_TWP_CNT_MASK;
  833. reg_val |= ((timing[FDT_NAND_TRH] / clk_period) <<
  834. TIMING_TRH_CNT_SHIFT) & TIMING_TRH_CNT_MASK;
  835. reg_val |= ((timing[FDT_NAND_MAX_TRP_TREA] / clk_period) <<
  836. TIMING_TRP_CNT_SHIFT) & TIMING_TRP_CNT_MASK;
  837. writel(reg_val, &reg->timing);
  838. reg_val = 0;
  839. time_val = timing[FDT_NAND_TADL] / clk_period;
  840. if (time_val > 2)
  841. reg_val = (time_val - 2) & TIMING2_TADL_CNT_MASK;
  842. writel(reg_val, &reg->timing2);
  843. }
  844. /**
  845. * Decode NAND parameters from the device tree
  846. *
  847. * @param blob Device tree blob
  848. * @param node Node containing "nand-flash" compatble node
  849. * @return 0 if ok, -ve on error (FDT_ERR_...)
  850. */
  851. static int fdt_decode_nand(const void *blob, int node, struct fdt_nand *config)
  852. {
  853. int err;
  854. config->reg = (struct nand_ctlr *)fdtdec_get_addr(blob, node, "reg");
  855. config->enabled = fdtdec_get_is_enabled(blob, node);
  856. config->width = fdtdec_get_int(blob, node, "nvidia,nand-width", 8);
  857. err = fdtdec_decode_gpio(blob, node, "nvidia,wp-gpios",
  858. &config->wp_gpio);
  859. if (err)
  860. return err;
  861. err = fdtdec_get_int_array(blob, node, "nvidia,timing",
  862. config->timing, FDT_NAND_TIMING_COUNT);
  863. if (err < 0)
  864. return err;
  865. /* Now look up the controller and decode that */
  866. node = fdt_next_node(blob, node, NULL);
  867. if (node < 0)
  868. return node;
  869. return 0;
  870. }
  871. /**
  872. * Board-specific NAND initialization
  873. *
  874. * @param nand nand chip info structure
  875. * @return 0, after initialized, -1 on error
  876. */
  877. int tegra_nand_init(struct nand_chip *nand, int devnum)
  878. {
  879. struct nand_drv *info = &nand_ctrl;
  880. struct fdt_nand *config = &info->config;
  881. int node, ret;
  882. node = fdtdec_next_compatible(gd->fdt_blob, 0,
  883. COMPAT_NVIDIA_TEGRA20_NAND);
  884. if (node < 0)
  885. return -1;
  886. if (fdt_decode_nand(gd->fdt_blob, node, config)) {
  887. printf("Could not decode nand-flash in device tree\n");
  888. return -1;
  889. }
  890. if (!config->enabled)
  891. return -1;
  892. info->reg = config->reg;
  893. nand->ecc.mode = NAND_ECC_HW;
  894. nand->ecc.layout = &eccoob;
  895. nand->options = LP_OPTIONS;
  896. nand->cmdfunc = nand_command;
  897. nand->read_byte = read_byte;
  898. nand->read_buf = read_buf;
  899. nand->ecc.read_page = nand_read_page_hwecc;
  900. nand->ecc.write_page = nand_write_page_hwecc;
  901. nand->ecc.read_page_raw = nand_read_page_raw;
  902. nand->ecc.write_page_raw = nand_write_page_raw;
  903. nand->ecc.read_oob = nand_read_oob;
  904. nand->ecc.write_oob = nand_write_oob;
  905. nand->ecc.strength = 1;
  906. nand->select_chip = nand_select_chip;
  907. nand->dev_ready = nand_dev_ready;
  908. nand->priv = &nand_ctrl;
  909. /* Adjust controller clock rate */
  910. clock_start_periph_pll(PERIPH_ID_NDFLASH, CLOCK_ID_PERIPH, 52000000);
  911. /* Adjust timing for NAND device */
  912. setup_timing(config->timing, info->reg);
  913. fdtdec_setup_gpio(&config->wp_gpio);
  914. gpio_direction_output(config->wp_gpio.gpio, 1);
  915. our_mtd = &nand_info[devnum];
  916. our_mtd->priv = nand;
  917. ret = nand_scan_ident(our_mtd, CONFIG_SYS_NAND_MAX_CHIPS, NULL);
  918. if (ret)
  919. return ret;
  920. nand->ecc.size = our_mtd->writesize;
  921. nand->ecc.bytes = our_mtd->oobsize;
  922. ret = nand_scan_tail(our_mtd);
  923. if (ret)
  924. return ret;
  925. ret = nand_register(devnum);
  926. if (ret)
  927. return ret;
  928. return 0;
  929. }
  930. void board_nand_init(void)
  931. {
  932. struct nand_chip *nand = &nand_chip[0];
  933. if (tegra_nand_init(nand, 0))
  934. puts("Tegra NAND init failed\n");
  935. }