onenand_base.c 33 KB

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