onenand_base.c 32 KB

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