tegra_nand.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025
  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. * Check NAND status to see if it is ready or not
  201. *
  202. * @param mtd MTD device structure
  203. * @return
  204. * 1 - ready
  205. * 0 - not ready
  206. */
  207. static int nand_dev_ready(struct mtd_info *mtd)
  208. {
  209. struct nand_chip *chip = mtd->priv;
  210. int reg_val;
  211. struct nand_drv *info;
  212. info = (struct nand_drv *)chip->priv;
  213. reg_val = readl(&info->reg->status);
  214. if (reg_val & STATUS_RBSY0)
  215. return 1;
  216. else
  217. return 0;
  218. }
  219. /* Dummy implementation: we don't support multiple chips */
  220. static void nand_select_chip(struct mtd_info *mtd, int chipnr)
  221. {
  222. switch (chipnr) {
  223. case -1:
  224. case 0:
  225. break;
  226. default:
  227. BUG();
  228. }
  229. }
  230. /**
  231. * Clear all interrupt status bits
  232. *
  233. * @param reg nand_ctlr structure
  234. */
  235. static void nand_clear_interrupt_status(struct nand_ctlr *reg)
  236. {
  237. u32 reg_val;
  238. /* Clear interrupt status */
  239. reg_val = readl(&reg->isr);
  240. writel(reg_val, &reg->isr);
  241. }
  242. /**
  243. * Send command to NAND device
  244. *
  245. * @param mtd MTD device structure
  246. * @param command the command to be sent
  247. * @param column the column address for this command, -1 if none
  248. * @param page_addr the page address for this command, -1 if none
  249. */
  250. static void nand_command(struct mtd_info *mtd, unsigned int command,
  251. int column, int page_addr)
  252. {
  253. struct nand_chip *chip = mtd->priv;
  254. struct nand_drv *info;
  255. info = (struct nand_drv *)chip->priv;
  256. /*
  257. * Write out the command to the device.
  258. *
  259. * Only command NAND_CMD_RESET or NAND_CMD_READID will come
  260. * here before mtd->writesize is initialized.
  261. */
  262. /* Emulate NAND_CMD_READOOB */
  263. if (command == NAND_CMD_READOOB) {
  264. assert(mtd->writesize != 0);
  265. column += mtd->writesize;
  266. command = NAND_CMD_READ0;
  267. }
  268. /* Adjust columns for 16 bit bus-width */
  269. if (column != -1 && (chip->options & NAND_BUSWIDTH_16))
  270. column >>= 1;
  271. nand_clear_interrupt_status(info->reg);
  272. /* Stop DMA engine, clear DMA completion status */
  273. writel(DMA_MST_CTRL_EN_A_DISABLE
  274. | DMA_MST_CTRL_EN_B_DISABLE
  275. | DMA_MST_CTRL_IS_DMA_DONE,
  276. &info->reg->dma_mst_ctrl);
  277. /*
  278. * Program and erase have their own busy handlers
  279. * status and sequential in needs no delay
  280. */
  281. switch (command) {
  282. case NAND_CMD_READID:
  283. writel(NAND_CMD_READID, &info->reg->cmd_reg1);
  284. writel(CMD_GO | CMD_CLE | CMD_ALE | CMD_PIO
  285. | CMD_RX |
  286. ((4 - 1) << CMD_TRANS_SIZE_SHIFT)
  287. | CMD_CE0,
  288. &info->reg->command);
  289. info->pio_byte_index = 0;
  290. break;
  291. case NAND_CMD_READ0:
  292. writel(NAND_CMD_READ0, &info->reg->cmd_reg1);
  293. writel(NAND_CMD_READSTART, &info->reg->cmd_reg2);
  294. writel((page_addr << 16) | (column & 0xFFFF),
  295. &info->reg->addr_reg1);
  296. writel(page_addr >> 16, &info->reg->addr_reg2);
  297. return;
  298. case NAND_CMD_SEQIN:
  299. writel(NAND_CMD_SEQIN, &info->reg->cmd_reg1);
  300. writel(NAND_CMD_PAGEPROG, &info->reg->cmd_reg2);
  301. writel((page_addr << 16) | (column & 0xFFFF),
  302. &info->reg->addr_reg1);
  303. writel(page_addr >> 16,
  304. &info->reg->addr_reg2);
  305. return;
  306. case NAND_CMD_PAGEPROG:
  307. return;
  308. case NAND_CMD_ERASE1:
  309. writel(NAND_CMD_ERASE1, &info->reg->cmd_reg1);
  310. writel(NAND_CMD_ERASE2, &info->reg->cmd_reg2);
  311. writel(page_addr, &info->reg->addr_reg1);
  312. writel(CMD_GO | CMD_CLE | CMD_ALE |
  313. CMD_SEC_CMD | CMD_CE0 | CMD_ALE_BYTES3,
  314. &info->reg->command);
  315. break;
  316. case NAND_CMD_ERASE2:
  317. return;
  318. case NAND_CMD_STATUS:
  319. writel(NAND_CMD_STATUS, &info->reg->cmd_reg1);
  320. writel(CMD_GO | CMD_CLE | CMD_PIO | CMD_RX
  321. | ((1 - 0) << CMD_TRANS_SIZE_SHIFT)
  322. | CMD_CE0,
  323. &info->reg->command);
  324. info->pio_byte_index = 0;
  325. break;
  326. case NAND_CMD_RESET:
  327. writel(NAND_CMD_RESET, &info->reg->cmd_reg1);
  328. writel(CMD_GO | CMD_CLE | CMD_CE0,
  329. &info->reg->command);
  330. break;
  331. case NAND_CMD_RNDOUT:
  332. default:
  333. printf("%s: Unsupported command %d\n", __func__, command);
  334. return;
  335. }
  336. if (!nand_waitfor_cmd_completion(info->reg))
  337. printf("Command 0x%02X timeout\n", command);
  338. }
  339. /**
  340. * Check whether the pointed buffer are all 0xff (blank).
  341. *
  342. * @param buf data buffer for blank check
  343. * @param len length of the buffer in byte
  344. * @return
  345. * 1 - blank
  346. * 0 - non-blank
  347. */
  348. static int blank_check(u8 *buf, int len)
  349. {
  350. int i;
  351. for (i = 0; i < len; i++)
  352. if (buf[i] != 0xFF)
  353. return 0;
  354. return 1;
  355. }
  356. /**
  357. * After a DMA transfer for read, we call this function to see whether there
  358. * is any uncorrectable error on the pointed data buffer or oob buffer.
  359. *
  360. * @param reg nand_ctlr structure
  361. * @param databuf data buffer
  362. * @param a_len data buffer length
  363. * @param oobbuf oob buffer
  364. * @param b_len oob buffer length
  365. * @return
  366. * ECC_OK - no ECC error or correctable ECC error
  367. * ECC_TAG_ERROR - uncorrectable tag ECC error
  368. * ECC_DATA_ERROR - uncorrectable data ECC error
  369. * ECC_DATA_ERROR + ECC_TAG_ERROR - uncorrectable data+tag ECC error
  370. */
  371. static int check_ecc_error(struct nand_ctlr *reg, u8 *databuf,
  372. int a_len, u8 *oobbuf, int b_len)
  373. {
  374. int return_val = ECC_OK;
  375. u32 reg_val;
  376. if (!(readl(&reg->isr) & ISR_IS_ECC_ERR))
  377. return ECC_OK;
  378. /*
  379. * Area A is used for the data block (databuf). Area B is used for
  380. * the spare block (oobbuf)
  381. */
  382. reg_val = readl(&reg->dec_status);
  383. if ((reg_val & DEC_STATUS_A_ECC_FAIL) && databuf) {
  384. reg_val = readl(&reg->bch_dec_status_buf);
  385. /*
  386. * If uncorrectable error occurs on data area, then see whether
  387. * they are all FF. If all are FF, it's a blank page.
  388. * Not error.
  389. */
  390. if ((reg_val & BCH_DEC_STATUS_FAIL_SEC_FLAG_MASK) &&
  391. !blank_check(databuf, a_len))
  392. return_val |= ECC_DATA_ERROR;
  393. }
  394. if ((reg_val & DEC_STATUS_B_ECC_FAIL) && oobbuf) {
  395. reg_val = readl(&reg->bch_dec_status_buf);
  396. /*
  397. * If uncorrectable error occurs on tag area, then see whether
  398. * they are all FF. If all are FF, it's a blank page.
  399. * Not error.
  400. */
  401. if ((reg_val & BCH_DEC_STATUS_FAIL_TAG_MASK) &&
  402. !blank_check(oobbuf, b_len))
  403. return_val |= ECC_TAG_ERROR;
  404. }
  405. return return_val;
  406. }
  407. /**
  408. * Set GO bit to send command to device
  409. *
  410. * @param reg nand_ctlr structure
  411. */
  412. static void start_command(struct nand_ctlr *reg)
  413. {
  414. u32 reg_val;
  415. reg_val = readl(&reg->command);
  416. reg_val |= CMD_GO;
  417. writel(reg_val, &reg->command);
  418. }
  419. /**
  420. * Clear command GO bit, DMA GO bit, and DMA completion status
  421. *
  422. * @param reg nand_ctlr structure
  423. */
  424. static void stop_command(struct nand_ctlr *reg)
  425. {
  426. /* Stop command */
  427. writel(0, &reg->command);
  428. /* Stop DMA engine and clear DMA completion status */
  429. writel(DMA_MST_CTRL_GO_DISABLE
  430. | DMA_MST_CTRL_IS_DMA_DONE,
  431. &reg->dma_mst_ctrl);
  432. }
  433. /**
  434. * Set up NAND bus width and page size
  435. *
  436. * @param info nand_info structure
  437. * @param *reg_val address of reg_val
  438. * @return 0 if ok, -1 on error
  439. */
  440. static int set_bus_width_page_size(struct fdt_nand *config,
  441. u32 *reg_val)
  442. {
  443. if (config->width == 8)
  444. *reg_val = CFG_BUS_WIDTH_8BIT;
  445. else if (config->width == 16)
  446. *reg_val = CFG_BUS_WIDTH_16BIT;
  447. else {
  448. debug("%s: Unsupported bus width %d\n", __func__,
  449. config->width);
  450. return -1;
  451. }
  452. if (our_mtd->writesize == 512)
  453. *reg_val |= CFG_PAGE_SIZE_512;
  454. else if (our_mtd->writesize == 2048)
  455. *reg_val |= CFG_PAGE_SIZE_2048;
  456. else if (our_mtd->writesize == 4096)
  457. *reg_val |= CFG_PAGE_SIZE_4096;
  458. else {
  459. debug("%s: Unsupported page size %d\n", __func__,
  460. our_mtd->writesize);
  461. return -1;
  462. }
  463. return 0;
  464. }
  465. /**
  466. * Page read/write function
  467. *
  468. * @param mtd mtd info structure
  469. * @param chip nand chip info structure
  470. * @param buf data buffer
  471. * @param page page number
  472. * @param with_ecc 1 to enable ECC, 0 to disable ECC
  473. * @param is_writing 0 for read, 1 for write
  474. * @return 0 when successfully completed
  475. * -EIO when command timeout
  476. */
  477. static int nand_rw_page(struct mtd_info *mtd, struct nand_chip *chip,
  478. uint8_t *buf, int page, int with_ecc, int is_writing)
  479. {
  480. u32 reg_val;
  481. int tag_size;
  482. struct nand_oobfree *free = chip->ecc.layout->oobfree;
  483. /* 4*128=512 (byte) is the value that our HW can support. */
  484. ALLOC_CACHE_ALIGN_BUFFER(u32, tag_buf, 128);
  485. char *tag_ptr;
  486. struct nand_drv *info;
  487. struct fdt_nand *config;
  488. if ((uintptr_t)buf & 0x03) {
  489. printf("buf %p has to be 4-byte aligned\n", buf);
  490. return -EINVAL;
  491. }
  492. info = (struct nand_drv *)chip->priv;
  493. config = &info->config;
  494. if (set_bus_width_page_size(config, &reg_val))
  495. return -EINVAL;
  496. /* Need to be 4-byte aligned */
  497. tag_ptr = (char *)tag_buf;
  498. stop_command(info->reg);
  499. writel((1 << chip->page_shift) - 1, &info->reg->dma_cfg_a);
  500. writel(virt_to_phys(buf), &info->reg->data_block_ptr);
  501. if (with_ecc) {
  502. writel(virt_to_phys(tag_ptr), &info->reg->tag_ptr);
  503. if (is_writing)
  504. memcpy(tag_ptr, chip->oob_poi + free->offset,
  505. chip->ecc.layout->oobavail +
  506. TAG_ECC_BYTES);
  507. } else {
  508. writel(virt_to_phys(chip->oob_poi), &info->reg->tag_ptr);
  509. }
  510. /* Set ECC selection, configure ECC settings */
  511. if (with_ecc) {
  512. tag_size = chip->ecc.layout->oobavail + TAG_ECC_BYTES;
  513. reg_val |= (CFG_SKIP_SPARE_SEL_4
  514. | CFG_SKIP_SPARE_ENABLE
  515. | CFG_HW_ECC_CORRECTION_ENABLE
  516. | CFG_ECC_EN_TAG_DISABLE
  517. | CFG_HW_ECC_SEL_RS
  518. | CFG_HW_ECC_ENABLE
  519. | CFG_TVAL4
  520. | (tag_size - 1));
  521. if (!is_writing)
  522. tag_size += SKIPPED_SPARE_BYTES;
  523. dma_prepare(tag_ptr, tag_size, is_writing);
  524. } else {
  525. tag_size = mtd->oobsize;
  526. reg_val |= (CFG_SKIP_SPARE_DISABLE
  527. | CFG_HW_ECC_CORRECTION_DISABLE
  528. | CFG_ECC_EN_TAG_DISABLE
  529. | CFG_HW_ECC_DISABLE
  530. | (tag_size - 1));
  531. dma_prepare(chip->oob_poi, tag_size, is_writing);
  532. }
  533. writel(reg_val, &info->reg->config);
  534. dma_prepare(buf, 1 << chip->page_shift, is_writing);
  535. writel(BCH_CONFIG_BCH_ECC_DISABLE, &info->reg->bch_config);
  536. writel(tag_size - 1, &info->reg->dma_cfg_b);
  537. nand_clear_interrupt_status(info->reg);
  538. reg_val = CMD_CLE | CMD_ALE
  539. | CMD_SEC_CMD
  540. | (CMD_ALE_BYTES5 << CMD_ALE_BYTE_SIZE_SHIFT)
  541. | CMD_A_VALID
  542. | CMD_B_VALID
  543. | (CMD_TRANS_SIZE_PAGE << CMD_TRANS_SIZE_SHIFT)
  544. | CMD_CE0;
  545. if (!is_writing)
  546. reg_val |= (CMD_AFT_DAT_DISABLE | CMD_RX);
  547. else
  548. reg_val |= (CMD_AFT_DAT_ENABLE | CMD_TX);
  549. writel(reg_val, &info->reg->command);
  550. /* Setup DMA engine */
  551. reg_val = DMA_MST_CTRL_GO_ENABLE
  552. | DMA_MST_CTRL_BURST_8WORDS
  553. | DMA_MST_CTRL_EN_A_ENABLE
  554. | DMA_MST_CTRL_EN_B_ENABLE;
  555. if (!is_writing)
  556. reg_val |= DMA_MST_CTRL_DIR_READ;
  557. else
  558. reg_val |= DMA_MST_CTRL_DIR_WRITE;
  559. writel(reg_val, &info->reg->dma_mst_ctrl);
  560. start_command(info->reg);
  561. if (!nand_waitfor_cmd_completion(info->reg)) {
  562. if (!is_writing)
  563. printf("Read Page 0x%X timeout ", page);
  564. else
  565. printf("Write Page 0x%X timeout ", page);
  566. if (with_ecc)
  567. printf("with ECC");
  568. else
  569. printf("without ECC");
  570. printf("\n");
  571. return -EIO;
  572. }
  573. if (with_ecc && !is_writing) {
  574. memcpy(chip->oob_poi, tag_ptr,
  575. SKIPPED_SPARE_BYTES);
  576. memcpy(chip->oob_poi + free->offset,
  577. tag_ptr + SKIPPED_SPARE_BYTES,
  578. chip->ecc.layout->oobavail);
  579. reg_val = (u32)check_ecc_error(info->reg, (u8 *)buf,
  580. 1 << chip->page_shift,
  581. (u8 *)(tag_ptr + SKIPPED_SPARE_BYTES),
  582. chip->ecc.layout->oobavail);
  583. if (reg_val & ECC_TAG_ERROR)
  584. printf("Read Page 0x%X tag ECC error\n", page);
  585. if (reg_val & ECC_DATA_ERROR)
  586. printf("Read Page 0x%X data ECC error\n",
  587. page);
  588. if (reg_val & (ECC_DATA_ERROR | ECC_TAG_ERROR))
  589. return -EIO;
  590. }
  591. return 0;
  592. }
  593. /**
  594. * Hardware ecc based page read function
  595. *
  596. * @param mtd mtd info structure
  597. * @param chip nand chip info structure
  598. * @param buf buffer to store read data
  599. * @param page page number to read
  600. * @return 0 when successfully completed
  601. * -EIO when command timeout
  602. */
  603. static int nand_read_page_hwecc(struct mtd_info *mtd,
  604. struct nand_chip *chip, uint8_t *buf, int page)
  605. {
  606. return nand_rw_page(mtd, chip, buf, page, 1, 0);
  607. }
  608. /**
  609. * Hardware ecc based page write function
  610. *
  611. * @param mtd mtd info structure
  612. * @param chip nand chip info structure
  613. * @param buf data buffer
  614. */
  615. static void nand_write_page_hwecc(struct mtd_info *mtd,
  616. struct nand_chip *chip, const uint8_t *buf)
  617. {
  618. int page;
  619. struct nand_drv *info;
  620. info = (struct nand_drv *)chip->priv;
  621. page = (readl(&info->reg->addr_reg1) >> 16) |
  622. (readl(&info->reg->addr_reg2) << 16);
  623. nand_rw_page(mtd, chip, (uint8_t *)buf, page, 1, 1);
  624. }
  625. /**
  626. * Read raw page data without ecc
  627. *
  628. * @param mtd mtd info structure
  629. * @param chip nand chip info structure
  630. * @param buf buffer to store read data
  631. * @param page page number to read
  632. * @return 0 when successfully completed
  633. * -EINVAL when chip->oob_poi is not double-word aligned
  634. * -EIO when command timeout
  635. */
  636. static int nand_read_page_raw(struct mtd_info *mtd,
  637. struct nand_chip *chip, uint8_t *buf, int page)
  638. {
  639. return nand_rw_page(mtd, chip, buf, page, 0, 0);
  640. }
  641. /**
  642. * Raw 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 void nand_write_page_raw(struct mtd_info *mtd,
  649. struct nand_chip *chip, const uint8_t *buf)
  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, 0, 1);
  657. }
  658. /**
  659. * OOB data read/write function
  660. *
  661. * @param mtd mtd info structure
  662. * @param chip nand chip info structure
  663. * @param page page number to read
  664. * @param with_ecc 1 to enable ECC, 0 to disable ECC
  665. * @param is_writing 0 for read, 1 for write
  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_rw_oob(struct mtd_info *mtd, struct nand_chip *chip,
  671. int page, int with_ecc, int is_writing)
  672. {
  673. u32 reg_val;
  674. int tag_size;
  675. struct nand_oobfree *free = chip->ecc.layout->oobfree;
  676. struct nand_drv *info;
  677. if (((int)chip->oob_poi) & 0x03)
  678. return -EINVAL;
  679. info = (struct nand_drv *)chip->priv;
  680. if (set_bus_width_page_size(&info->config, &reg_val))
  681. return -EINVAL;
  682. stop_command(info->reg);
  683. writel(virt_to_phys(chip->oob_poi), &info->reg->tag_ptr);
  684. /* Set ECC selection */
  685. tag_size = mtd->oobsize;
  686. if (with_ecc)
  687. reg_val |= CFG_ECC_EN_TAG_ENABLE;
  688. else
  689. reg_val |= (CFG_ECC_EN_TAG_DISABLE);
  690. reg_val |= ((tag_size - 1) |
  691. CFG_SKIP_SPARE_DISABLE |
  692. CFG_HW_ECC_CORRECTION_DISABLE |
  693. CFG_HW_ECC_DISABLE);
  694. writel(reg_val, &info->reg->config);
  695. dma_prepare(chip->oob_poi, tag_size, is_writing);
  696. writel(BCH_CONFIG_BCH_ECC_DISABLE, &info->reg->bch_config);
  697. if (is_writing && with_ecc)
  698. tag_size -= TAG_ECC_BYTES;
  699. writel(tag_size - 1, &info->reg->dma_cfg_b);
  700. nand_clear_interrupt_status(info->reg);
  701. reg_val = CMD_CLE | CMD_ALE
  702. | CMD_SEC_CMD
  703. | (CMD_ALE_BYTES5 << CMD_ALE_BYTE_SIZE_SHIFT)
  704. | CMD_B_VALID
  705. | CMD_CE0;
  706. if (!is_writing)
  707. reg_val |= (CMD_AFT_DAT_DISABLE | CMD_RX);
  708. else
  709. reg_val |= (CMD_AFT_DAT_ENABLE | CMD_TX);
  710. writel(reg_val, &info->reg->command);
  711. /* Setup DMA engine */
  712. reg_val = DMA_MST_CTRL_GO_ENABLE
  713. | DMA_MST_CTRL_BURST_8WORDS
  714. | DMA_MST_CTRL_EN_B_ENABLE;
  715. if (!is_writing)
  716. reg_val |= DMA_MST_CTRL_DIR_READ;
  717. else
  718. reg_val |= DMA_MST_CTRL_DIR_WRITE;
  719. writel(reg_val, &info->reg->dma_mst_ctrl);
  720. start_command(info->reg);
  721. if (!nand_waitfor_cmd_completion(info->reg)) {
  722. if (!is_writing)
  723. printf("Read OOB of Page 0x%X timeout\n", page);
  724. else
  725. printf("Write OOB of Page 0x%X timeout\n", page);
  726. return -EIO;
  727. }
  728. if (with_ecc && !is_writing) {
  729. reg_val = (u32)check_ecc_error(info->reg, 0, 0,
  730. (u8 *)(chip->oob_poi + free->offset),
  731. chip->ecc.layout->oobavail);
  732. if (reg_val & ECC_TAG_ERROR)
  733. printf("Read OOB of Page 0x%X tag ECC error\n", page);
  734. }
  735. return 0;
  736. }
  737. /**
  738. * OOB data read function
  739. *
  740. * @param mtd mtd info structure
  741. * @param chip nand chip info structure
  742. * @param page page number to read
  743. * @param sndcmd flag whether to issue read command or not
  744. * @return 1 - issue read command next time
  745. * 0 - not to issue
  746. */
  747. static int nand_read_oob(struct mtd_info *mtd, struct nand_chip *chip,
  748. int page, int sndcmd)
  749. {
  750. if (sndcmd) {
  751. chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
  752. sndcmd = 0;
  753. }
  754. nand_rw_oob(mtd, chip, page, 0, 0);
  755. return sndcmd;
  756. }
  757. /**
  758. * OOB data write function
  759. *
  760. * @param mtd mtd info structure
  761. * @param chip nand chip info structure
  762. * @param page page number to write
  763. * @return 0 when successfully completed
  764. * -EINVAL when chip->oob_poi is not double-word aligned
  765. * -EIO when command timeout
  766. */
  767. static int nand_write_oob(struct mtd_info *mtd, struct nand_chip *chip,
  768. int page)
  769. {
  770. chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
  771. return nand_rw_oob(mtd, chip, page, 0, 1);
  772. }
  773. /**
  774. * Set up NAND memory timings according to the provided parameters
  775. *
  776. * @param timing Timing parameters
  777. * @param reg NAND controller register address
  778. */
  779. static void setup_timing(unsigned timing[FDT_NAND_TIMING_COUNT],
  780. struct nand_ctlr *reg)
  781. {
  782. u32 reg_val, clk_rate, clk_period, time_val;
  783. clk_rate = (u32)clock_get_periph_rate(PERIPH_ID_NDFLASH,
  784. CLOCK_ID_PERIPH) / 1000000;
  785. clk_period = 1000 / clk_rate;
  786. reg_val = ((timing[FDT_NAND_MAX_TRP_TREA] / clk_period) <<
  787. TIMING_TRP_RESP_CNT_SHIFT) & TIMING_TRP_RESP_CNT_MASK;
  788. reg_val |= ((timing[FDT_NAND_TWB] / clk_period) <<
  789. TIMING_TWB_CNT_SHIFT) & TIMING_TWB_CNT_MASK;
  790. time_val = timing[FDT_NAND_MAX_TCR_TAR_TRR] / clk_period;
  791. if (time_val > 2)
  792. reg_val |= ((time_val - 2) << TIMING_TCR_TAR_TRR_CNT_SHIFT) &
  793. TIMING_TCR_TAR_TRR_CNT_MASK;
  794. reg_val |= ((timing[FDT_NAND_TWHR] / clk_period) <<
  795. TIMING_TWHR_CNT_SHIFT) & TIMING_TWHR_CNT_MASK;
  796. time_val = timing[FDT_NAND_MAX_TCS_TCH_TALS_TALH] / clk_period;
  797. if (time_val > 1)
  798. reg_val |= ((time_val - 1) << TIMING_TCS_CNT_SHIFT) &
  799. TIMING_TCS_CNT_MASK;
  800. reg_val |= ((timing[FDT_NAND_TWH] / clk_period) <<
  801. TIMING_TWH_CNT_SHIFT) & TIMING_TWH_CNT_MASK;
  802. reg_val |= ((timing[FDT_NAND_TWP] / clk_period) <<
  803. TIMING_TWP_CNT_SHIFT) & TIMING_TWP_CNT_MASK;
  804. reg_val |= ((timing[FDT_NAND_TRH] / clk_period) <<
  805. TIMING_TRH_CNT_SHIFT) & TIMING_TRH_CNT_MASK;
  806. reg_val |= ((timing[FDT_NAND_MAX_TRP_TREA] / clk_period) <<
  807. TIMING_TRP_CNT_SHIFT) & TIMING_TRP_CNT_MASK;
  808. writel(reg_val, &reg->timing);
  809. reg_val = 0;
  810. time_val = timing[FDT_NAND_TADL] / clk_period;
  811. if (time_val > 2)
  812. reg_val = (time_val - 2) & TIMING2_TADL_CNT_MASK;
  813. writel(reg_val, &reg->timing2);
  814. }
  815. /**
  816. * Decode NAND parameters from the device tree
  817. *
  818. * @param blob Device tree blob
  819. * @param node Node containing "nand-flash" compatble node
  820. * @return 0 if ok, -ve on error (FDT_ERR_...)
  821. */
  822. static int fdt_decode_nand(const void *blob, int node, struct fdt_nand *config)
  823. {
  824. int err;
  825. config->reg = (struct nand_ctlr *)fdtdec_get_addr(blob, node, "reg");
  826. config->enabled = fdtdec_get_is_enabled(blob, node);
  827. config->width = fdtdec_get_int(blob, node, "nvidia,nand-width", 8);
  828. err = fdtdec_decode_gpio(blob, node, "nvidia,wp-gpios",
  829. &config->wp_gpio);
  830. if (err)
  831. return err;
  832. err = fdtdec_get_int_array(blob, node, "nvidia,timing",
  833. config->timing, FDT_NAND_TIMING_COUNT);
  834. if (err < 0)
  835. return err;
  836. /* Now look up the controller and decode that */
  837. node = fdt_next_node(blob, node, NULL);
  838. if (node < 0)
  839. return node;
  840. return 0;
  841. }
  842. /**
  843. * Board-specific NAND initialization
  844. *
  845. * @param nand nand chip info structure
  846. * @return 0, after initialized, -1 on error
  847. */
  848. int tegra_nand_init(struct nand_chip *nand, int devnum)
  849. {
  850. struct nand_drv *info = &nand_ctrl;
  851. struct fdt_nand *config = &info->config;
  852. int node, ret;
  853. node = fdtdec_next_compatible(gd->fdt_blob, 0,
  854. COMPAT_NVIDIA_TEGRA20_NAND);
  855. if (node < 0)
  856. return -1;
  857. if (fdt_decode_nand(gd->fdt_blob, node, config)) {
  858. printf("Could not decode nand-flash in device tree\n");
  859. return -1;
  860. }
  861. if (!config->enabled)
  862. return -1;
  863. info->reg = config->reg;
  864. nand->ecc.mode = NAND_ECC_HW;
  865. nand->ecc.layout = &eccoob;
  866. nand->options = LP_OPTIONS;
  867. nand->cmdfunc = nand_command;
  868. nand->read_byte = read_byte;
  869. nand->ecc.read_page = nand_read_page_hwecc;
  870. nand->ecc.write_page = nand_write_page_hwecc;
  871. nand->ecc.read_page_raw = nand_read_page_raw;
  872. nand->ecc.write_page_raw = nand_write_page_raw;
  873. nand->ecc.read_oob = nand_read_oob;
  874. nand->ecc.write_oob = nand_write_oob;
  875. nand->select_chip = nand_select_chip;
  876. nand->dev_ready = nand_dev_ready;
  877. nand->priv = &nand_ctrl;
  878. /* Adjust controller clock rate */
  879. clock_start_periph_pll(PERIPH_ID_NDFLASH, CLOCK_ID_PERIPH, 52000000);
  880. /* Adjust timing for NAND device */
  881. setup_timing(config->timing, info->reg);
  882. funcmux_select(PERIPH_ID_NDFLASH, FUNCMUX_DEFAULT);
  883. fdtdec_setup_gpio(&config->wp_gpio);
  884. gpio_direction_output(config->wp_gpio.gpio, 1);
  885. our_mtd = &nand_info[devnum];
  886. our_mtd->priv = nand;
  887. ret = nand_scan_ident(our_mtd, CONFIG_SYS_NAND_MAX_CHIPS, NULL);
  888. if (ret)
  889. return ret;
  890. nand->ecc.size = our_mtd->writesize;
  891. nand->ecc.bytes = our_mtd->oobsize;
  892. ret = nand_scan_tail(our_mtd);
  893. if (ret)
  894. return ret;
  895. ret = nand_register(devnum);
  896. if (ret)
  897. return ret;
  898. return 0;
  899. }
  900. void board_nand_init(void)
  901. {
  902. struct nand_chip *nand = &nand_chip[0];
  903. if (tegra_nand_init(nand, 0))
  904. puts("Tegra NAND init failed\n");
  905. }