onenand_base.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294
  1. /*
  2. * linux/drivers/mtd/onenand/onenand_base.c
  3. *
  4. * Copyright (C) 2005-2007 Samsung Electronics
  5. * Kyungmin Park <kyungmin.park@samsung.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <common.h>
  12. #ifdef CONFIG_CMD_ONENAND
  13. #include <linux/mtd/compat.h>
  14. #include <linux/mtd/mtd.h>
  15. #include <linux/mtd/onenand.h>
  16. #include <asm/io.h>
  17. #include <asm/errno.h>
  18. static const unsigned char ffchars[] = {
  19. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  20. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 16 */
  21. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  22. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 32 */
  23. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  24. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 48 */
  25. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  26. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 64 */
  27. };
  28. /**
  29. * onenand_readw - [OneNAND Interface] Read OneNAND register
  30. * @param addr address to read
  31. *
  32. * Read OneNAND register
  33. */
  34. static unsigned short onenand_readw(void __iomem * addr)
  35. {
  36. return readw(addr);
  37. }
  38. /**
  39. * onenand_writew - [OneNAND Interface] Write OneNAND register with value
  40. * @param value value to write
  41. * @param addr address to write
  42. *
  43. * Write OneNAND register with value
  44. */
  45. static void onenand_writew(unsigned short value, void __iomem * addr)
  46. {
  47. writew(value, addr);
  48. }
  49. /**
  50. * onenand_block_address - [DEFAULT] Get block address
  51. * @param device the device id
  52. * @param block the block
  53. * @return translated block address if DDP, otherwise same
  54. *
  55. * Setup Start Address 1 Register (F100h)
  56. */
  57. static int onenand_block_address(int device, int block)
  58. {
  59. if (device & ONENAND_DEVICE_IS_DDP) {
  60. /* Device Flash Core select, NAND Flash Block Address */
  61. int dfs = 0, density, mask;
  62. density = device >> ONENAND_DEVICE_DENSITY_SHIFT;
  63. mask = (1 << (density + 6));
  64. if (block & mask)
  65. dfs = 1;
  66. return (dfs << ONENAND_DDP_SHIFT) | (block & (mask - 1));
  67. }
  68. return block;
  69. }
  70. /**
  71. * onenand_bufferram_address - [DEFAULT] Get bufferram address
  72. * @param device the device id
  73. * @param block the block
  74. * @return set DBS value if DDP, otherwise 0
  75. *
  76. * Setup Start Address 2 Register (F101h) for DDP
  77. */
  78. static int onenand_bufferram_address(int device, int block)
  79. {
  80. if (device & ONENAND_DEVICE_IS_DDP) {
  81. /* Device BufferRAM Select */
  82. int dbs = 0, density, mask;
  83. density = device >> ONENAND_DEVICE_DENSITY_SHIFT;
  84. mask = (1 << (density + 6));
  85. if (block & mask)
  86. dbs = 1;
  87. return (dbs << ONENAND_DDP_SHIFT);
  88. }
  89. return 0;
  90. }
  91. /**
  92. * onenand_page_address - [DEFAULT] Get page address
  93. * @param page the page address
  94. * @param sector the sector address
  95. * @return combined page and sector address
  96. *
  97. * Setup Start Address 8 Register (F107h)
  98. */
  99. static int onenand_page_address(int page, int sector)
  100. {
  101. /* Flash Page Address, Flash Sector Address */
  102. int fpa, fsa;
  103. fpa = page & ONENAND_FPA_MASK;
  104. fsa = sector & ONENAND_FSA_MASK;
  105. return ((fpa << ONENAND_FPA_SHIFT) | fsa);
  106. }
  107. /**
  108. * onenand_buffer_address - [DEFAULT] Get buffer address
  109. * @param dataram1 DataRAM index
  110. * @param sectors the sector address
  111. * @param count the number of sectors
  112. * @return the start buffer value
  113. *
  114. * Setup Start Buffer Register (F200h)
  115. */
  116. static int onenand_buffer_address(int dataram1, int sectors, int count)
  117. {
  118. int bsa, bsc;
  119. /* BufferRAM Sector Address */
  120. bsa = sectors & ONENAND_BSA_MASK;
  121. if (dataram1)
  122. bsa |= ONENAND_BSA_DATARAM1; /* DataRAM1 */
  123. else
  124. bsa |= ONENAND_BSA_DATARAM0; /* DataRAM0 */
  125. /* BufferRAM Sector Count */
  126. bsc = count & ONENAND_BSC_MASK;
  127. return ((bsa << ONENAND_BSA_SHIFT) | bsc);
  128. }
  129. /**
  130. * onenand_command - [DEFAULT] Send command to OneNAND device
  131. * @param mtd MTD device structure
  132. * @param cmd the command to be sent
  133. * @param addr offset to read from or write to
  134. * @param len number of bytes to read or write
  135. *
  136. * Send command to OneNAND device. This function is used for middle/large page
  137. * devices (1KB/2KB Bytes per page)
  138. */
  139. static int onenand_command(struct mtd_info *mtd, int cmd, loff_t addr,
  140. size_t len)
  141. {
  142. struct onenand_chip *this = mtd->priv;
  143. int value, readcmd = 0;
  144. int block, page;
  145. /* Now we use page size operation */
  146. int sectors = 4, count = 4;
  147. /* Address translation */
  148. switch (cmd) {
  149. case ONENAND_CMD_UNLOCK:
  150. case ONENAND_CMD_LOCK:
  151. case ONENAND_CMD_LOCK_TIGHT:
  152. block = -1;
  153. page = -1;
  154. break;
  155. case ONENAND_CMD_ERASE:
  156. case ONENAND_CMD_BUFFERRAM:
  157. block = (int)(addr >> this->erase_shift);
  158. page = -1;
  159. break;
  160. default:
  161. block = (int)(addr >> this->erase_shift);
  162. page = (int)(addr >> this->page_shift);
  163. page &= this->page_mask;
  164. break;
  165. }
  166. /* NOTE: The setting order of the registers is very important! */
  167. if (cmd == ONENAND_CMD_BUFFERRAM) {
  168. /* Select DataRAM for DDP */
  169. value = onenand_bufferram_address(this->device_id, block);
  170. this->write_word(value,
  171. this->base + ONENAND_REG_START_ADDRESS2);
  172. /* Switch to the next data buffer */
  173. ONENAND_SET_NEXT_BUFFERRAM(this);
  174. return 0;
  175. }
  176. if (block != -1) {
  177. /* Write 'DFS, FBA' of Flash */
  178. value = onenand_block_address(this->device_id, block);
  179. this->write_word(value,
  180. this->base + ONENAND_REG_START_ADDRESS1);
  181. }
  182. if (page != -1) {
  183. int dataram;
  184. switch (cmd) {
  185. case ONENAND_CMD_READ:
  186. case ONENAND_CMD_READOOB:
  187. dataram = ONENAND_SET_NEXT_BUFFERRAM(this);
  188. readcmd = 1;
  189. break;
  190. default:
  191. dataram = ONENAND_CURRENT_BUFFERRAM(this);
  192. break;
  193. }
  194. /* Write 'FPA, FSA' of Flash */
  195. value = onenand_page_address(page, sectors);
  196. this->write_word(value,
  197. this->base + ONENAND_REG_START_ADDRESS8);
  198. /* Write 'BSA, BSC' of DataRAM */
  199. value = onenand_buffer_address(dataram, sectors, count);
  200. this->write_word(value, this->base + ONENAND_REG_START_BUFFER);
  201. if (readcmd) {
  202. /* Select DataRAM for DDP */
  203. value =
  204. onenand_bufferram_address(this->device_id, block);
  205. this->write_word(value,
  206. this->base +
  207. ONENAND_REG_START_ADDRESS2);
  208. }
  209. }
  210. /* Interrupt clear */
  211. this->write_word(ONENAND_INT_CLEAR, this->base + ONENAND_REG_INTERRUPT);
  212. /* Write command */
  213. this->write_word(cmd, this->base + ONENAND_REG_COMMAND);
  214. return 0;
  215. }
  216. /**
  217. * onenand_wait - [DEFAULT] wait until the command is done
  218. * @param mtd MTD device structure
  219. * @param state state to select the max. timeout value
  220. *
  221. * Wait for command done. This applies to all OneNAND command
  222. * Read can take up to 30us, erase up to 2ms and program up to 350us
  223. * according to general OneNAND specs
  224. */
  225. static int onenand_wait(struct mtd_info *mtd, int state)
  226. {
  227. struct onenand_chip *this = mtd->priv;
  228. unsigned int flags = ONENAND_INT_MASTER;
  229. unsigned int interrupt = 0;
  230. unsigned int ctrl, ecc;
  231. while (1) {
  232. interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
  233. if (interrupt & flags)
  234. break;
  235. }
  236. ctrl = this->read_word(this->base + ONENAND_REG_CTRL_STATUS);
  237. if (ctrl & ONENAND_CTRL_ERROR) {
  238. DEBUG(MTD_DEBUG_LEVEL0,
  239. "onenand_wait: controller error = 0x%04x\n", ctrl);
  240. return -EAGAIN;
  241. }
  242. if (ctrl & ONENAND_CTRL_LOCK) {
  243. DEBUG(MTD_DEBUG_LEVEL0,
  244. "onenand_wait: it's locked error = 0x%04x\n", ctrl);
  245. return -EIO;
  246. }
  247. if (interrupt & ONENAND_INT_READ) {
  248. ecc = this->read_word(this->base + ONENAND_REG_ECC_STATUS);
  249. if (ecc & ONENAND_ECC_2BIT_ALL) {
  250. DEBUG(MTD_DEBUG_LEVEL0,
  251. "onenand_wait: ECC error = 0x%04x\n", ecc);
  252. return -EBADMSG;
  253. }
  254. }
  255. return 0;
  256. }
  257. /**
  258. * onenand_bufferram_offset - [DEFAULT] BufferRAM offset
  259. * @param mtd MTD data structure
  260. * @param area BufferRAM area
  261. * @return offset given area
  262. *
  263. * Return BufferRAM offset given area
  264. */
  265. static inline int onenand_bufferram_offset(struct mtd_info *mtd, int area)
  266. {
  267. struct onenand_chip *this = mtd->priv;
  268. if (ONENAND_CURRENT_BUFFERRAM(this)) {
  269. if (area == ONENAND_DATARAM)
  270. return mtd->oobblock;
  271. if (area == ONENAND_SPARERAM)
  272. return mtd->oobsize;
  273. }
  274. return 0;
  275. }
  276. /**
  277. * onenand_read_bufferram - [OneNAND Interface] Read the bufferram area
  278. * @param mtd MTD data structure
  279. * @param area BufferRAM area
  280. * @param buffer the databuffer to put/get data
  281. * @param offset offset to read from or write to
  282. * @param count number of bytes to read/write
  283. *
  284. * Read the BufferRAM area
  285. */
  286. static int onenand_read_bufferram(struct mtd_info *mtd, int area,
  287. unsigned char *buffer, int offset,
  288. size_t count)
  289. {
  290. struct onenand_chip *this = mtd->priv;
  291. void __iomem *bufferram;
  292. bufferram = this->base + area;
  293. bufferram += onenand_bufferram_offset(mtd, area);
  294. memcpy(buffer, bufferram + offset, count);
  295. return 0;
  296. }
  297. /**
  298. * onenand_sync_read_bufferram - [OneNAND Interface] Read the bufferram area with Sync. Burst mode
  299. * @param mtd MTD data structure
  300. * @param area BufferRAM area
  301. * @param buffer the databuffer to put/get data
  302. * @param offset offset to read from or write to
  303. * @param count number of bytes to read/write
  304. *
  305. * Read the BufferRAM area with Sync. Burst Mode
  306. */
  307. static int onenand_sync_read_bufferram(struct mtd_info *mtd, int area,
  308. unsigned char *buffer, int offset,
  309. size_t count)
  310. {
  311. struct onenand_chip *this = mtd->priv;
  312. void __iomem *bufferram;
  313. bufferram = this->base + area;
  314. bufferram += onenand_bufferram_offset(mtd, area);
  315. this->mmcontrol(mtd, ONENAND_SYS_CFG1_SYNC_READ);
  316. memcpy(buffer, bufferram + offset, count);
  317. this->mmcontrol(mtd, 0);
  318. return 0;
  319. }
  320. /**
  321. * onenand_write_bufferram - [OneNAND Interface] Write the bufferram area
  322. * @param mtd MTD data structure
  323. * @param area BufferRAM area
  324. * @param buffer the databuffer to put/get data
  325. * @param offset offset to read from or write to
  326. * @param count number of bytes to read/write
  327. *
  328. * Write the BufferRAM area
  329. */
  330. static int onenand_write_bufferram(struct mtd_info *mtd, int area,
  331. const unsigned char *buffer, int offset,
  332. size_t count)
  333. {
  334. struct onenand_chip *this = mtd->priv;
  335. void __iomem *bufferram;
  336. bufferram = this->base + area;
  337. bufferram += onenand_bufferram_offset(mtd, area);
  338. memcpy(bufferram + offset, buffer, count);
  339. return 0;
  340. }
  341. /**
  342. * onenand_check_bufferram - [GENERIC] Check BufferRAM information
  343. * @param mtd MTD data structure
  344. * @param addr address to check
  345. * @return 1 if there are valid data, otherwise 0
  346. *
  347. * Check bufferram if there is data we required
  348. */
  349. static int onenand_check_bufferram(struct mtd_info *mtd, loff_t addr)
  350. {
  351. struct onenand_chip *this = mtd->priv;
  352. int block, page;
  353. int i;
  354. block = (int)(addr >> this->erase_shift);
  355. page = (int)(addr >> this->page_shift);
  356. page &= this->page_mask;
  357. i = ONENAND_CURRENT_BUFFERRAM(this);
  358. /* Is there valid data? */
  359. if (this->bufferram[i].block == block &&
  360. this->bufferram[i].page == page && this->bufferram[i].valid)
  361. return 1;
  362. return 0;
  363. }
  364. /**
  365. * onenand_update_bufferram - [GENERIC] Update BufferRAM information
  366. * @param mtd MTD data structure
  367. * @param addr address to update
  368. * @param valid valid flag
  369. *
  370. * Update BufferRAM information
  371. */
  372. static int onenand_update_bufferram(struct mtd_info *mtd, loff_t addr,
  373. int valid)
  374. {
  375. struct onenand_chip *this = mtd->priv;
  376. int block, page;
  377. int i;
  378. block = (int)(addr >> this->erase_shift);
  379. page = (int)(addr >> this->page_shift);
  380. page &= this->page_mask;
  381. /* Invalidate BufferRAM */
  382. for (i = 0; i < MAX_BUFFERRAM; i++) {
  383. if (this->bufferram[i].block == block &&
  384. this->bufferram[i].page == page)
  385. this->bufferram[i].valid = 0;
  386. }
  387. /* Update BufferRAM */
  388. i = ONENAND_CURRENT_BUFFERRAM(this);
  389. this->bufferram[i].block = block;
  390. this->bufferram[i].page = page;
  391. this->bufferram[i].valid = valid;
  392. return 0;
  393. }
  394. /**
  395. * onenand_get_device - [GENERIC] Get chip for selected access
  396. * @param mtd MTD device structure
  397. * @param new_state the state which is requested
  398. *
  399. * Get the device and lock it for exclusive access
  400. */
  401. static void onenand_get_device(struct mtd_info *mtd, int new_state)
  402. {
  403. /* Do nothing */
  404. }
  405. /**
  406. * onenand_release_device - [GENERIC] release chip
  407. * @param mtd MTD device structure
  408. *
  409. * Deselect, release chip lock and wake up anyone waiting on the device
  410. */
  411. static void onenand_release_device(struct mtd_info *mtd)
  412. {
  413. /* Do nothing */
  414. }
  415. /**
  416. * onenand_read_ecc - [MTD Interface] Read data with ECC
  417. * @param mtd MTD device structure
  418. * @param from offset to read from
  419. * @param len number of bytes to read
  420. * @param retlen pointer to variable to store the number of read bytes
  421. * @param buf the databuffer to put data
  422. * @param oob_buf filesystem supplied oob data buffer
  423. * @param oobsel oob selection structure
  424. *
  425. * OneNAND read with ECC
  426. */
  427. static int onenand_read_ecc(struct mtd_info *mtd, loff_t from, size_t len,
  428. size_t * retlen, u_char * buf,
  429. u_char * oob_buf, struct nand_oobinfo *oobsel)
  430. {
  431. struct onenand_chip *this = mtd->priv;
  432. int read = 0, column;
  433. int thislen;
  434. int ret = 0;
  435. DEBUG(MTD_DEBUG_LEVEL3, "onenand_read_ecc: from = 0x%08x, len = %i\n",
  436. (unsigned int)from, (int)len);
  437. /* Do not allow reads past end of device */
  438. if ((from + len) > mtd->size) {
  439. DEBUG(MTD_DEBUG_LEVEL0,
  440. "onenand_read_ecc: Attempt read beyond end of device\n");
  441. *retlen = 0;
  442. return -EINVAL;
  443. }
  444. /* Grab the lock and see if the device is available */
  445. onenand_get_device(mtd, FL_READING);
  446. while (read < len) {
  447. thislen = min_t(int, mtd->oobblock, len - read);
  448. column = from & (mtd->oobblock - 1);
  449. if (column + thislen > mtd->oobblock)
  450. thislen = mtd->oobblock - column;
  451. if (!onenand_check_bufferram(mtd, from)) {
  452. this->command(mtd, ONENAND_CMD_READ, from,
  453. mtd->oobblock);
  454. ret = this->wait(mtd, FL_READING);
  455. /* First copy data and check return value for ECC handling */
  456. onenand_update_bufferram(mtd, from, 1);
  457. }
  458. this->read_bufferram(mtd, ONENAND_DATARAM, buf, column,
  459. thislen);
  460. read += thislen;
  461. if (read == len)
  462. break;
  463. if (ret) {
  464. DEBUG(MTD_DEBUG_LEVEL0,
  465. "onenand_read_ecc: read failed = %d\n", ret);
  466. break;
  467. }
  468. from += thislen;
  469. buf += thislen;
  470. }
  471. /* Deselect and wake up anyone waiting on the device */
  472. onenand_release_device(mtd);
  473. /*
  474. * Return success, if no ECC failures, else -EBADMSG
  475. * fs driver will take care of that, because
  476. * retlen == desired len and result == -EBADMSG
  477. */
  478. *retlen = read;
  479. return ret;
  480. }
  481. /**
  482. * onenand_read - [MTD Interface] MTD compability function for onenand_read_ecc
  483. * @param mtd MTD device structure
  484. * @param from offset to read from
  485. * @param len number of bytes to read
  486. * @param retlen pointer to variable to store the number of read bytes
  487. * @param buf the databuffer to put data
  488. *
  489. * This function simply calls onenand_read_ecc with oob buffer and oobsel = NULL
  490. */
  491. int onenand_read(struct mtd_info *mtd, loff_t from, size_t len,
  492. size_t * retlen, u_char * buf)
  493. {
  494. return onenand_read_ecc(mtd, from, len, retlen, buf, NULL, NULL);
  495. }
  496. /**
  497. * onenand_read_oob - [MTD Interface] OneNAND read out-of-band
  498. * @param mtd MTD device structure
  499. * @param from offset to read from
  500. * @param len number of bytes to read
  501. * @param retlen pointer to variable to store the number of read bytes
  502. * @param buf the databuffer to put data
  503. *
  504. * OneNAND read out-of-band data from the spare area
  505. */
  506. int onenand_read_oob(struct mtd_info *mtd, loff_t from, size_t len,
  507. size_t * retlen, u_char * buf)
  508. {
  509. struct onenand_chip *this = mtd->priv;
  510. int read = 0, thislen, column;
  511. int ret = 0;
  512. DEBUG(MTD_DEBUG_LEVEL3, "onenand_read_oob: from = 0x%08x, len = %i\n",
  513. (unsigned int)from, (int)len);
  514. /* Initialize return length value */
  515. *retlen = 0;
  516. /* Do not allow reads past end of device */
  517. if (unlikely((from + len) > mtd->size)) {
  518. DEBUG(MTD_DEBUG_LEVEL0,
  519. "onenand_read_oob: Attempt read beyond end of device\n");
  520. return -EINVAL;
  521. }
  522. /* Grab the lock and see if the device is available */
  523. onenand_get_device(mtd, FL_READING);
  524. column = from & (mtd->oobsize - 1);
  525. while (read < len) {
  526. thislen = mtd->oobsize - column;
  527. thislen = min_t(int, thislen, len);
  528. this->command(mtd, ONENAND_CMD_READOOB, from, mtd->oobsize);
  529. onenand_update_bufferram(mtd, from, 0);
  530. ret = this->wait(mtd, FL_READING);
  531. /* First copy data and check return value for ECC handling */
  532. this->read_bufferram(mtd, ONENAND_SPARERAM, buf, column,
  533. thislen);
  534. read += thislen;
  535. if (read == len)
  536. break;
  537. if (ret) {
  538. DEBUG(MTD_DEBUG_LEVEL0,
  539. "onenand_read_oob: read failed = %d\n", ret);
  540. break;
  541. }
  542. buf += thislen;
  543. /* Read more? */
  544. if (read < len) {
  545. /* Page size */
  546. from += mtd->oobblock;
  547. column = 0;
  548. }
  549. }
  550. /* Deselect and wake up anyone waiting on the device */
  551. onenand_release_device(mtd);
  552. *retlen = read;
  553. return ret;
  554. }
  555. #ifdef CONFIG_MTD_ONENAND_VERIFY_WRITE
  556. /**
  557. * onenand_verify_page - [GENERIC] verify the chip contents after a write
  558. * @param mtd MTD device structure
  559. * @param buf the databuffer to verify
  560. * @param block block address
  561. * @param page page address
  562. *
  563. * Check DataRAM area directly
  564. */
  565. static int onenand_verify_page(struct mtd_info *mtd, u_char * buf,
  566. loff_t addr, int block, int page)
  567. {
  568. struct onenand_chip *this = mtd->priv;
  569. void __iomem *dataram0, *dataram1;
  570. int ret = 0;
  571. this->command(mtd, ONENAND_CMD_READ, addr, mtd->oobblock);
  572. ret = this->wait(mtd, FL_READING);
  573. if (ret)
  574. return ret;
  575. onenand_update_bufferram(mtd, addr, 1);
  576. /* Check, if the two dataram areas are same */
  577. dataram0 = this->base + ONENAND_DATARAM;
  578. dataram1 = dataram0 + mtd->oobblock;
  579. if (memcmp(dataram0, dataram1, mtd->oobblock))
  580. return -EBADMSG;
  581. return 0;
  582. }
  583. #else
  584. #define onenand_verify_page(...) (0)
  585. #endif
  586. #define NOTALIGNED(x) ((x & (mtd->oobblock - 1)) != 0)
  587. /**
  588. * onenand_write_ecc - [MTD Interface] OneNAND write with ECC
  589. * @param mtd MTD device structure
  590. * @param to offset to write to
  591. * @param len number of bytes to write
  592. * @param retlen pointer to variable to store the number of written bytes
  593. * @param buf the data to write
  594. * @param eccbuf filesystem supplied oob data buffer
  595. * @param oobsel oob selection structure
  596. *
  597. * OneNAND write with ECC
  598. */
  599. static int onenand_write_ecc(struct mtd_info *mtd, loff_t to, size_t len,
  600. size_t * retlen, const u_char * buf,
  601. u_char * eccbuf, struct nand_oobinfo *oobsel)
  602. {
  603. struct onenand_chip *this = mtd->priv;
  604. int written = 0;
  605. int ret = 0;
  606. DEBUG(MTD_DEBUG_LEVEL3, "onenand_write_ecc: to = 0x%08x, len = %i\n",
  607. (unsigned int)to, (int)len);
  608. /* Initialize retlen, in case of early exit */
  609. *retlen = 0;
  610. /* Do not allow writes past end of device */
  611. if (unlikely((to + len) > mtd->size)) {
  612. DEBUG(MTD_DEBUG_LEVEL0,
  613. "onenand_write_ecc: Attempt write to past end of device\n");
  614. return -EINVAL;
  615. }
  616. /* Reject writes, which are not page aligned */
  617. if (unlikely(NOTALIGNED(to)) || unlikely(NOTALIGNED(len))) {
  618. DEBUG(MTD_DEBUG_LEVEL0,
  619. "onenand_write_ecc: Attempt to write not page aligned data\n");
  620. return -EINVAL;
  621. }
  622. /* Grab the lock and see if the device is available */
  623. onenand_get_device(mtd, FL_WRITING);
  624. /* Loop until all data write */
  625. while (written < len) {
  626. int thislen = min_t(int, mtd->oobblock, len - written);
  627. this->command(mtd, ONENAND_CMD_BUFFERRAM, to, mtd->oobblock);
  628. this->write_bufferram(mtd, ONENAND_DATARAM, buf, 0, thislen);
  629. this->write_bufferram(mtd, ONENAND_SPARERAM, ffchars, 0,
  630. mtd->oobsize);
  631. this->command(mtd, ONENAND_CMD_PROG, to, mtd->oobblock);
  632. onenand_update_bufferram(mtd, to, 1);
  633. ret = this->wait(mtd, FL_WRITING);
  634. if (ret) {
  635. DEBUG(MTD_DEBUG_LEVEL0,
  636. "onenand_write_ecc: write filaed %d\n", ret);
  637. break;
  638. }
  639. written += thislen;
  640. /* Only check verify write turn on */
  641. ret = onenand_verify_page(mtd, (u_char *) buf, to, block, page);
  642. if (ret) {
  643. DEBUG(MTD_DEBUG_LEVEL0,
  644. "onenand_write_ecc: verify failed %d\n", ret);
  645. break;
  646. }
  647. if (written == len)
  648. break;
  649. to += thislen;
  650. buf += thislen;
  651. }
  652. /* Deselect and wake up anyone waiting on the device */
  653. onenand_release_device(mtd);
  654. *retlen = written;
  655. return ret;
  656. }
  657. /**
  658. * onenand_write - [MTD Interface] compability function for onenand_write_ecc
  659. * @param mtd MTD device structure
  660. * @param to offset to write to
  661. * @param len number of bytes to write
  662. * @param retlen pointer to variable to store the number of written bytes
  663. * @param buf the data to write
  664. *
  665. * This function simply calls onenand_write_ecc
  666. * with oob buffer and oobsel = NULL
  667. */
  668. int onenand_write(struct mtd_info *mtd, loff_t to, size_t len,
  669. size_t * retlen, const u_char * buf)
  670. {
  671. return onenand_write_ecc(mtd, to, len, retlen, buf, NULL, NULL);
  672. }
  673. /**
  674. * onenand_write_oob - [MTD Interface] OneNAND write out-of-band
  675. * @param mtd MTD device structure
  676. * @param to offset to write to
  677. * @param len number of bytes to write
  678. * @param retlen pointer to variable to store the number of written bytes
  679. * @param buf the data to write
  680. *
  681. * OneNAND write out-of-band
  682. */
  683. int onenand_write_oob(struct mtd_info *mtd, loff_t to, size_t len,
  684. size_t * retlen, const u_char * buf)
  685. {
  686. struct onenand_chip *this = mtd->priv;
  687. int column, status;
  688. int written = 0;
  689. DEBUG(MTD_DEBUG_LEVEL3, "onenand_write_oob: to = 0x%08x, len = %i\n",
  690. (unsigned int)to, (int)len);
  691. /* Initialize retlen, in case of early exit */
  692. *retlen = 0;
  693. /* Do not allow writes past end of device */
  694. if (unlikely((to + len) > mtd->size)) {
  695. DEBUG(MTD_DEBUG_LEVEL0,
  696. "onenand_write_oob: Attempt write to past end of device\n");
  697. return -EINVAL;
  698. }
  699. /* Grab the lock and see if the device is available */
  700. onenand_get_device(mtd, FL_WRITING);
  701. /* Loop until all data write */
  702. while (written < len) {
  703. int thislen = min_t(int, mtd->oobsize, len - written);
  704. column = to & (mtd->oobsize - 1);
  705. this->command(mtd, ONENAND_CMD_BUFFERRAM, to, mtd->oobsize);
  706. this->write_bufferram(mtd, ONENAND_SPARERAM, ffchars, 0,
  707. mtd->oobsize);
  708. this->write_bufferram(mtd, ONENAND_SPARERAM, buf, column,
  709. thislen);
  710. this->command(mtd, ONENAND_CMD_PROGOOB, to, mtd->oobsize);
  711. onenand_update_bufferram(mtd, to, 0);
  712. status = this->wait(mtd, FL_WRITING);
  713. if (status)
  714. break;
  715. written += thislen;
  716. if (written == len)
  717. break;
  718. to += thislen;
  719. buf += thislen;
  720. }
  721. /* Deselect and wake up anyone waiting on the device */
  722. onenand_release_device(mtd);
  723. *retlen = written;
  724. return 0;
  725. }
  726. /**
  727. * onenand_erase - [MTD Interface] erase block(s)
  728. * @param mtd MTD device structure
  729. * @param instr erase instruction
  730. *
  731. * Erase one ore more blocks
  732. */
  733. int onenand_erase(struct mtd_info *mtd, struct erase_info *instr)
  734. {
  735. struct onenand_chip *this = mtd->priv;
  736. unsigned int block_size;
  737. loff_t addr;
  738. int len;
  739. int ret = 0;
  740. DEBUG(MTD_DEBUG_LEVEL3, "onenand_erase: start = 0x%08x, len = %i\n",
  741. (unsigned int)instr->addr, (unsigned int)instr->len);
  742. block_size = (1 << this->erase_shift);
  743. /* Start address must align on block boundary */
  744. if (unlikely(instr->addr & (block_size - 1))) {
  745. DEBUG(MTD_DEBUG_LEVEL0, "onenand_erase: Unaligned address\n");
  746. return -EINVAL;
  747. }
  748. /* Length must align on block boundary */
  749. if (unlikely(instr->len & (block_size - 1))) {
  750. DEBUG(MTD_DEBUG_LEVEL0,
  751. "onenand_erase: Length not block aligned\n");
  752. return -EINVAL;
  753. }
  754. /* Do not allow erase past end of device */
  755. if (unlikely((instr->len + instr->addr) > mtd->size)) {
  756. DEBUG(MTD_DEBUG_LEVEL0,
  757. "onenand_erase: Erase past end of device\n");
  758. return -EINVAL;
  759. }
  760. instr->fail_addr = 0xffffffff;
  761. /* Grab the lock and see if the device is available */
  762. onenand_get_device(mtd, FL_ERASING);
  763. /* Loop throught the pages */
  764. len = instr->len;
  765. addr = instr->addr;
  766. instr->state = MTD_ERASING;
  767. while (len) {
  768. /* TODO Check badblock */
  769. this->command(mtd, ONENAND_CMD_ERASE, addr, block_size);
  770. ret = this->wait(mtd, FL_ERASING);
  771. /* Check, if it is write protected */
  772. if (ret) {
  773. if (ret == -EPERM)
  774. DEBUG(MTD_DEBUG_LEVEL0,
  775. "onenand_erase: Device is write protected!!!\n");
  776. else
  777. DEBUG(MTD_DEBUG_LEVEL0,
  778. "onenand_erase: Failed erase, block %d\n",
  779. (unsigned)(addr >> this->erase_shift));
  780. instr->state = MTD_ERASE_FAILED;
  781. instr->fail_addr = addr;
  782. goto erase_exit;
  783. }
  784. len -= block_size;
  785. addr += block_size;
  786. }
  787. instr->state = MTD_ERASE_DONE;
  788. erase_exit:
  789. ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
  790. /* Do call back function */
  791. if (!ret)
  792. mtd_erase_callback(instr);
  793. /* Deselect and wake up anyone waiting on the device */
  794. onenand_release_device(mtd);
  795. return ret;
  796. }
  797. /**
  798. * onenand_sync - [MTD Interface] sync
  799. * @param mtd MTD device structure
  800. *
  801. * Sync is actually a wait for chip ready function
  802. */
  803. void onenand_sync(struct mtd_info *mtd)
  804. {
  805. DEBUG(MTD_DEBUG_LEVEL3, "onenand_sync: called\n");
  806. /* Grab the lock and see if the device is available */
  807. onenand_get_device(mtd, FL_SYNCING);
  808. /* Release it and go back */
  809. onenand_release_device(mtd);
  810. }
  811. /**
  812. * onenand_block_isbad - [MTD Interface] Check whether the block at the given offset is bad
  813. * @param mtd MTD device structure
  814. * @param ofs offset relative to mtd start
  815. */
  816. int onenand_block_isbad(struct mtd_info *mtd, loff_t ofs)
  817. {
  818. /*
  819. * TODO
  820. * 1. Bad block table (BBT)
  821. * -> using NAND BBT to support JFFS2
  822. * 2. Bad block management (BBM)
  823. * -> bad block replace scheme
  824. *
  825. * Currently we do nothing
  826. */
  827. return 0;
  828. }
  829. /**
  830. * onenand_block_markbad - [MTD Interface] Mark the block at the given offset as bad
  831. * @param mtd MTD device structure
  832. * @param ofs offset relative to mtd start
  833. */
  834. int onenand_block_markbad(struct mtd_info *mtd, loff_t ofs)
  835. {
  836. /* see above */
  837. return 0;
  838. }
  839. /**
  840. * onenand_unlock - [MTD Interface] Unlock block(s)
  841. * @param mtd MTD device structure
  842. * @param ofs offset relative to mtd start
  843. * @param len number of bytes to unlock
  844. *
  845. * Unlock one or more blocks
  846. */
  847. int onenand_unlock(struct mtd_info *mtd, loff_t ofs, size_t len)
  848. {
  849. struct onenand_chip *this = mtd->priv;
  850. int start, end, block, value, status;
  851. start = ofs >> this->erase_shift;
  852. end = len >> this->erase_shift;
  853. /* Continuous lock scheme */
  854. if (this->options & ONENAND_CONT_LOCK) {
  855. /* Set start block address */
  856. this->write_word(start,
  857. this->base + ONENAND_REG_START_BLOCK_ADDRESS);
  858. /* Set end block address */
  859. this->write_word(end - 1,
  860. this->base + ONENAND_REG_END_BLOCK_ADDRESS);
  861. /* Write unlock command */
  862. this->command(mtd, ONENAND_CMD_UNLOCK, 0, 0);
  863. /* There's no return value */
  864. this->wait(mtd, FL_UNLOCKING);
  865. /* Sanity check */
  866. while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
  867. & ONENAND_CTRL_ONGO)
  868. continue;
  869. /* Check lock status */
  870. status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
  871. if (!(status & ONENAND_WP_US))
  872. printk(KERN_ERR "wp status = 0x%x\n", status);
  873. return 0;
  874. }
  875. /* Block lock scheme */
  876. for (block = start; block < end; block++) {
  877. /* Set start block address */
  878. this->write_word(block,
  879. this->base + ONENAND_REG_START_BLOCK_ADDRESS);
  880. /* Write unlock command */
  881. this->command(mtd, ONENAND_CMD_UNLOCK, 0, 0);
  882. /* There's no return value */
  883. this->wait(mtd, FL_UNLOCKING);
  884. /* Sanity check */
  885. while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
  886. & ONENAND_CTRL_ONGO)
  887. continue;
  888. /* Set block address for read block status */
  889. value = onenand_block_address(this->device_id, block);
  890. this->write_word(value,
  891. this->base + ONENAND_REG_START_ADDRESS1);
  892. /* Check lock status */
  893. status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
  894. if (!(status & ONENAND_WP_US))
  895. printk(KERN_ERR "block = %d, wp status = 0x%x\n",
  896. block, status);
  897. }
  898. return 0;
  899. }
  900. /**
  901. * onenand_print_device_info - Print device ID
  902. * @param device device ID
  903. *
  904. * Print device ID
  905. */
  906. void onenand_print_device_info(int device, int verbose)
  907. {
  908. int vcc, demuxed, ddp, density;
  909. if (!verbose)
  910. return;
  911. vcc = device & ONENAND_DEVICE_VCC_MASK;
  912. demuxed = device & ONENAND_DEVICE_IS_DEMUX;
  913. ddp = device & ONENAND_DEVICE_IS_DDP;
  914. density = device >> ONENAND_DEVICE_DENSITY_SHIFT;
  915. printk(KERN_INFO "%sOneNAND%s %dMB %sV 16-bit (0x%02x)\n",
  916. demuxed ? "" : "Muxed ",
  917. ddp ? "(DDP)" : "",
  918. (16 << density), vcc ? "2.65/3.3" : "1.8", device);
  919. }
  920. static const struct onenand_manufacturers onenand_manuf_ids[] = {
  921. {ONENAND_MFR_SAMSUNG, "Samsung"},
  922. {ONENAND_MFR_UNKNOWN, "Unknown"}
  923. };
  924. /**
  925. * onenand_check_maf - Check manufacturer ID
  926. * @param manuf manufacturer ID
  927. *
  928. * Check manufacturer ID
  929. */
  930. static int onenand_check_maf(int manuf)
  931. {
  932. int i;
  933. for (i = 0; onenand_manuf_ids[i].id; i++) {
  934. if (manuf == onenand_manuf_ids[i].id)
  935. break;
  936. }
  937. #ifdef ONENAND_DEBUG
  938. printk(KERN_DEBUG "OneNAND Manufacturer: %s (0x%0x)\n",
  939. onenand_manuf_ids[i].name, manuf);
  940. #endif
  941. return (i != ONENAND_MFR_UNKNOWN);
  942. }
  943. /**
  944. * onenand_probe - [OneNAND Interface] Probe the OneNAND device
  945. * @param mtd MTD device structure
  946. *
  947. * OneNAND detection method:
  948. * Compare the the values from command with ones from register
  949. */
  950. static int onenand_probe(struct mtd_info *mtd)
  951. {
  952. struct onenand_chip *this = mtd->priv;
  953. int bram_maf_id, bram_dev_id, maf_id, dev_id;
  954. int version_id;
  955. int density;
  956. /* Send the command for reading device ID from BootRAM */
  957. this->write_word(ONENAND_CMD_READID, this->base + ONENAND_BOOTRAM);
  958. /* Read manufacturer and device IDs from BootRAM */
  959. bram_maf_id = this->read_word(this->base + ONENAND_BOOTRAM + 0x0);
  960. bram_dev_id = this->read_word(this->base + ONENAND_BOOTRAM + 0x2);
  961. /* Check manufacturer ID */
  962. if (onenand_check_maf(bram_maf_id))
  963. return -ENXIO;
  964. /* Reset OneNAND to read default register values */
  965. this->write_word(ONENAND_CMD_RESET, this->base + ONENAND_BOOTRAM);
  966. {
  967. int i;
  968. for (i = 0; i < 10000; i++) ;
  969. }
  970. /* Read manufacturer and device IDs from Register */
  971. maf_id = this->read_word(this->base + ONENAND_REG_MANUFACTURER_ID);
  972. dev_id = this->read_word(this->base + ONENAND_REG_DEVICE_ID);
  973. /* Check OneNAND device */
  974. if (maf_id != bram_maf_id || dev_id != bram_dev_id)
  975. return -ENXIO;
  976. /* Flash device information */
  977. onenand_print_device_info(dev_id, 0);
  978. this->device_id = dev_id;
  979. density = dev_id >> ONENAND_DEVICE_DENSITY_SHIFT;
  980. this->chipsize = (16 << density) << 20;
  981. /* OneNAND page size & block size */
  982. /* The data buffer size is equal to page size */
  983. mtd->oobblock =
  984. this->read_word(this->base + ONENAND_REG_DATA_BUFFER_SIZE);
  985. mtd->oobsize = mtd->oobblock >> 5;
  986. /* Pagers per block is always 64 in OneNAND */
  987. mtd->erasesize = mtd->oobblock << 6;
  988. this->erase_shift = ffs(mtd->erasesize) - 1;
  989. this->page_shift = ffs(mtd->oobblock) - 1;
  990. this->ppb_shift = (this->erase_shift - this->page_shift);
  991. this->page_mask = (mtd->erasesize / mtd->oobblock) - 1;
  992. /* REVIST: Multichip handling */
  993. mtd->size = this->chipsize;
  994. /* Version ID */
  995. version_id = this->read_word(this->base + ONENAND_REG_VERSION_ID);
  996. #ifdef ONENAND_DEBUG
  997. printk(KERN_DEBUG "OneNAND version = 0x%04x\n", version_id);
  998. #endif
  999. /* Lock scheme */
  1000. if (density <= ONENAND_DEVICE_DENSITY_512Mb &&
  1001. !(version_id >> ONENAND_VERSION_PROCESS_SHIFT)) {
  1002. printk(KERN_INFO "Lock scheme is Continues Lock\n");
  1003. this->options |= ONENAND_CONT_LOCK;
  1004. }
  1005. return 0;
  1006. }
  1007. /**
  1008. * onenand_scan - [OneNAND Interface] Scan for the OneNAND device
  1009. * @param mtd MTD device structure
  1010. * @param maxchips Number of chips to scan for
  1011. *
  1012. * This fills out all the not initialized function pointers
  1013. * with the defaults.
  1014. * The flash ID is read and the mtd/chip structures are
  1015. * filled with the appropriate values.
  1016. */
  1017. int onenand_scan(struct mtd_info *mtd, int maxchips)
  1018. {
  1019. struct onenand_chip *this = mtd->priv;
  1020. if (!this->read_word)
  1021. this->read_word = onenand_readw;
  1022. if (!this->write_word)
  1023. this->write_word = onenand_writew;
  1024. if (!this->command)
  1025. this->command = onenand_command;
  1026. if (!this->wait)
  1027. this->wait = onenand_wait;
  1028. if (!this->read_bufferram)
  1029. this->read_bufferram = onenand_read_bufferram;
  1030. if (!this->write_bufferram)
  1031. this->write_bufferram = onenand_write_bufferram;
  1032. if (onenand_probe(mtd))
  1033. return -ENXIO;
  1034. /* Set Sync. Burst Read after probing */
  1035. if (this->mmcontrol) {
  1036. printk(KERN_INFO "OneNAND Sync. Burst Read support\n");
  1037. this->read_bufferram = onenand_sync_read_bufferram;
  1038. }
  1039. onenand_unlock(mtd, 0, mtd->size);
  1040. return onenand_default_bbt(mtd);
  1041. }
  1042. /**
  1043. * onenand_release - [OneNAND Interface] Free resources held by the OneNAND device
  1044. * @param mtd MTD device structure
  1045. */
  1046. void onenand_release(struct mtd_info *mtd)
  1047. {
  1048. }
  1049. /*
  1050. * OneNAND initialization at U-Boot
  1051. */
  1052. struct mtd_info onenand_mtd;
  1053. struct onenand_chip onenand_chip;
  1054. void onenand_init(void)
  1055. {
  1056. memset(&onenand_mtd, 0, sizeof(struct mtd_info));
  1057. memset(&onenand_chip, 0, sizeof(struct onenand_chip));
  1058. onenand_chip.base = (void *)CFG_ONENAND_BASE;
  1059. onenand_mtd.priv = &onenand_chip;
  1060. onenand_scan(&onenand_mtd, 1);
  1061. puts("OneNAND: ");
  1062. print_size(onenand_mtd.size, "\n");
  1063. }
  1064. #endif /* CONFIG_CMD_ONENAND */