onenand_base.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293
  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. DEBUG(MTD_DEBUG_LEVEL0,
  250. "onenand_wait: controller error = 0x%04x\n", ctrl);
  251. return -EAGAIN;
  252. }
  253. if (ctrl & ONENAND_CTRL_LOCK) {
  254. DEBUG(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. DEBUG(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. DEBUG(MTD_DEBUG_LEVEL3, "onenand_read_ecc: from = 0x%08x, len = %i\n",
  447. (unsigned int)from, (int)len);
  448. /* Do not allow reads past end of device */
  449. if ((from + len) > mtd->size) {
  450. DEBUG(MTD_DEBUG_LEVEL0,
  451. "onenand_read_ecc: Attempt read beyond end of device\n");
  452. *retlen = 0;
  453. return -EINVAL;
  454. }
  455. /* Grab the lock and see if the device is available */
  456. onenand_get_device(mtd, FL_READING);
  457. while (read < len) {
  458. thislen = min_t(int, mtd->oobblock, len - read);
  459. column = from & (mtd->oobblock - 1);
  460. if (column + thislen > mtd->oobblock)
  461. thislen = mtd->oobblock - column;
  462. if (!onenand_check_bufferram(mtd, from)) {
  463. this->command(mtd, ONENAND_CMD_READ, from,
  464. mtd->oobblock);
  465. ret = this->wait(mtd, FL_READING);
  466. /* First copy data and check return value for ECC handling */
  467. onenand_update_bufferram(mtd, from, 1);
  468. }
  469. this->read_bufferram(mtd, ONENAND_DATARAM, buf, column,
  470. thislen);
  471. read += thislen;
  472. if (read == len)
  473. break;
  474. if (ret) {
  475. DEBUG(MTD_DEBUG_LEVEL0,
  476. "onenand_read_ecc: read failed = %d\n", ret);
  477. break;
  478. }
  479. from += thislen;
  480. buf += thislen;
  481. }
  482. /* Deselect and wake up anyone waiting on the device */
  483. onenand_release_device(mtd);
  484. /*
  485. * Return success, if no ECC failures, else -EBADMSG
  486. * fs driver will take care of that, because
  487. * retlen == desired len and result == -EBADMSG
  488. */
  489. *retlen = read;
  490. return ret;
  491. }
  492. /**
  493. * onenand_read - [MTD Interface] MTD compability function for onenand_read_ecc
  494. * @param mtd MTD device structure
  495. * @param from offset to read from
  496. * @param len number of bytes to read
  497. * @param retlen pointer to variable to store the number of read bytes
  498. * @param buf the databuffer to put data
  499. *
  500. * This function simply calls onenand_read_ecc with oob buffer and oobsel = NULL
  501. */
  502. int onenand_read(struct mtd_info *mtd, loff_t from, size_t len,
  503. size_t * retlen, u_char * buf)
  504. {
  505. return onenand_read_ecc(mtd, from, len, retlen, buf, NULL, NULL);
  506. }
  507. /**
  508. * onenand_read_oob - [MTD Interface] OneNAND read out-of-band
  509. * @param mtd MTD device structure
  510. * @param from offset to read from
  511. * @param len number of bytes to read
  512. * @param retlen pointer to variable to store the number of read bytes
  513. * @param buf the databuffer to put data
  514. *
  515. * OneNAND read out-of-band data from the spare area
  516. */
  517. int onenand_read_oob(struct mtd_info *mtd, loff_t from, size_t len,
  518. size_t * retlen, u_char * buf)
  519. {
  520. struct onenand_chip *this = mtd->priv;
  521. int read = 0, thislen, column;
  522. int ret = 0;
  523. DEBUG(MTD_DEBUG_LEVEL3, "onenand_read_oob: from = 0x%08x, len = %i\n",
  524. (unsigned int)from, (int)len);
  525. /* Initialize return length value */
  526. *retlen = 0;
  527. /* Do not allow reads past end of device */
  528. if (unlikely((from + len) > mtd->size)) {
  529. DEBUG(MTD_DEBUG_LEVEL0,
  530. "onenand_read_oob: Attempt read beyond end of device\n");
  531. return -EINVAL;
  532. }
  533. /* Grab the lock and see if the device is available */
  534. onenand_get_device(mtd, FL_READING);
  535. column = from & (mtd->oobsize - 1);
  536. while (read < len) {
  537. thislen = mtd->oobsize - column;
  538. thislen = min_t(int, thislen, len);
  539. this->command(mtd, ONENAND_CMD_READOOB, from, mtd->oobsize);
  540. onenand_update_bufferram(mtd, from, 0);
  541. ret = this->wait(mtd, FL_READING);
  542. /* First copy data and check return value for ECC handling */
  543. this->read_bufferram(mtd, ONENAND_SPARERAM, buf, column,
  544. thislen);
  545. read += thislen;
  546. if (read == len)
  547. break;
  548. if (ret) {
  549. DEBUG(MTD_DEBUG_LEVEL0,
  550. "onenand_read_oob: read failed = %d\n", ret);
  551. break;
  552. }
  553. buf += thislen;
  554. /* Read more? */
  555. if (read < len) {
  556. /* Page size */
  557. from += mtd->oobblock;
  558. column = 0;
  559. }
  560. }
  561. /* Deselect and wake up anyone waiting on the device */
  562. onenand_release_device(mtd);
  563. *retlen = read;
  564. return ret;
  565. }
  566. #ifdef CONFIG_MTD_ONENAND_VERIFY_WRITE
  567. /**
  568. * onenand_verify_page - [GENERIC] verify the chip contents after a write
  569. * @param mtd MTD device structure
  570. * @param buf the databuffer to verify
  571. * @param block block address
  572. * @param page page address
  573. *
  574. * Check DataRAM area directly
  575. */
  576. static int onenand_verify_page(struct mtd_info *mtd, u_char * buf,
  577. loff_t addr, int block, int page)
  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. DEBUG(MTD_DEBUG_LEVEL3, "onenand_write_ecc: to = 0x%08x, len = %i\n",
  618. (unsigned int)to, (int)len);
  619. /* Initialize retlen, in case of early exit */
  620. *retlen = 0;
  621. /* Do not allow writes past end of device */
  622. if (unlikely((to + len) > mtd->size)) {
  623. DEBUG(MTD_DEBUG_LEVEL0,
  624. "onenand_write_ecc: Attempt write to past end of device\n");
  625. return -EINVAL;
  626. }
  627. /* Reject writes, which are not page aligned */
  628. if (unlikely(NOTALIGNED(to)) || unlikely(NOTALIGNED(len))) {
  629. DEBUG(MTD_DEBUG_LEVEL0,
  630. "onenand_write_ecc: Attempt to write not page aligned data\n");
  631. return -EINVAL;
  632. }
  633. /* Grab the lock and see if the device is available */
  634. onenand_get_device(mtd, FL_WRITING);
  635. /* Loop until all data write */
  636. while (written < len) {
  637. int thislen = min_t(int, mtd->oobblock, len - written);
  638. this->command(mtd, ONENAND_CMD_BUFFERRAM, to, mtd->oobblock);
  639. this->write_bufferram(mtd, ONENAND_DATARAM, buf, 0, thislen);
  640. this->write_bufferram(mtd, ONENAND_SPARERAM, ffchars, 0,
  641. mtd->oobsize);
  642. this->command(mtd, ONENAND_CMD_PROG, to, mtd->oobblock);
  643. onenand_update_bufferram(mtd, to, 1);
  644. ret = this->wait(mtd, FL_WRITING);
  645. if (ret) {
  646. DEBUG(MTD_DEBUG_LEVEL0,
  647. "onenand_write_ecc: write filaed %d\n", ret);
  648. break;
  649. }
  650. written += thislen;
  651. /* Only check verify write turn on */
  652. ret = onenand_verify_page(mtd, (u_char *) buf, to, block, page);
  653. if (ret) {
  654. DEBUG(MTD_DEBUG_LEVEL0,
  655. "onenand_write_ecc: verify failed %d\n", ret);
  656. break;
  657. }
  658. if (written == len)
  659. break;
  660. to += thislen;
  661. buf += thislen;
  662. }
  663. /* Deselect and wake up anyone waiting on the device */
  664. onenand_release_device(mtd);
  665. *retlen = written;
  666. return ret;
  667. }
  668. /**
  669. * onenand_write - [MTD Interface] compability function for onenand_write_ecc
  670. * @param mtd MTD device structure
  671. * @param to offset to write to
  672. * @param len number of bytes to write
  673. * @param retlen pointer to variable to store the number of written bytes
  674. * @param buf the data to write
  675. *
  676. * This function simply calls onenand_write_ecc
  677. * with oob buffer and oobsel = NULL
  678. */
  679. int onenand_write(struct mtd_info *mtd, loff_t to, size_t len,
  680. size_t * retlen, const u_char * buf)
  681. {
  682. return onenand_write_ecc(mtd, to, len, retlen, buf, NULL, NULL);
  683. }
  684. /**
  685. * onenand_write_oob - [MTD Interface] OneNAND write out-of-band
  686. * @param mtd MTD device structure
  687. * @param to offset to write to
  688. * @param len number of bytes to write
  689. * @param retlen pointer to variable to store the number of written bytes
  690. * @param buf the data to write
  691. *
  692. * OneNAND write out-of-band
  693. */
  694. int onenand_write_oob(struct mtd_info *mtd, loff_t to, size_t len,
  695. size_t * retlen, const u_char * buf)
  696. {
  697. struct onenand_chip *this = mtd->priv;
  698. int column, status;
  699. int written = 0;
  700. DEBUG(MTD_DEBUG_LEVEL3, "onenand_write_oob: to = 0x%08x, len = %i\n",
  701. (unsigned int)to, (int)len);
  702. /* Initialize retlen, in case of early exit */
  703. *retlen = 0;
  704. /* Do not allow writes past end of device */
  705. if (unlikely((to + len) > mtd->size)) {
  706. DEBUG(MTD_DEBUG_LEVEL0,
  707. "onenand_write_oob: Attempt write to past end of device\n");
  708. return -EINVAL;
  709. }
  710. /* Grab the lock and see if the device is available */
  711. onenand_get_device(mtd, FL_WRITING);
  712. /* Loop until all data write */
  713. while (written < len) {
  714. int thislen = min_t(int, mtd->oobsize, len - written);
  715. column = to & (mtd->oobsize - 1);
  716. this->command(mtd, ONENAND_CMD_BUFFERRAM, to, mtd->oobsize);
  717. this->write_bufferram(mtd, ONENAND_SPARERAM, ffchars, 0,
  718. mtd->oobsize);
  719. this->write_bufferram(mtd, ONENAND_SPARERAM, buf, column,
  720. thislen);
  721. this->command(mtd, ONENAND_CMD_PROGOOB, to, mtd->oobsize);
  722. onenand_update_bufferram(mtd, to, 0);
  723. status = this->wait(mtd, FL_WRITING);
  724. if (status)
  725. break;
  726. written += thislen;
  727. if (written == len)
  728. break;
  729. to += thislen;
  730. buf += thislen;
  731. }
  732. /* Deselect and wake up anyone waiting on the device */
  733. onenand_release_device(mtd);
  734. *retlen = written;
  735. return 0;
  736. }
  737. /**
  738. * onenand_erase - [MTD Interface] erase block(s)
  739. * @param mtd MTD device structure
  740. * @param instr erase instruction
  741. *
  742. * Erase one ore more blocks
  743. */
  744. int onenand_erase(struct mtd_info *mtd, struct erase_info *instr)
  745. {
  746. struct onenand_chip *this = mtd->priv;
  747. unsigned int block_size;
  748. loff_t addr;
  749. int len;
  750. int ret = 0;
  751. DEBUG(MTD_DEBUG_LEVEL3, "onenand_erase: start = 0x%08x, len = %i\n",
  752. (unsigned int)instr->addr, (unsigned int)instr->len);
  753. block_size = (1 << this->erase_shift);
  754. /* Start address must align on block boundary */
  755. if (unlikely(instr->addr & (block_size - 1))) {
  756. DEBUG(MTD_DEBUG_LEVEL0, "onenand_erase: Unaligned address\n");
  757. return -EINVAL;
  758. }
  759. /* Length must align on block boundary */
  760. if (unlikely(instr->len & (block_size - 1))) {
  761. DEBUG(MTD_DEBUG_LEVEL0,
  762. "onenand_erase: Length not block aligned\n");
  763. return -EINVAL;
  764. }
  765. /* Do not allow erase past end of device */
  766. if (unlikely((instr->len + instr->addr) > mtd->size)) {
  767. DEBUG(MTD_DEBUG_LEVEL0,
  768. "onenand_erase: Erase past end of device\n");
  769. return -EINVAL;
  770. }
  771. instr->fail_addr = 0xffffffff;
  772. /* Grab the lock and see if the device is available */
  773. onenand_get_device(mtd, FL_ERASING);
  774. /* Loop throught the pages */
  775. len = instr->len;
  776. addr = instr->addr;
  777. instr->state = MTD_ERASING;
  778. while (len) {
  779. /* TODO Check badblock */
  780. this->command(mtd, ONENAND_CMD_ERASE, addr, block_size);
  781. ret = this->wait(mtd, FL_ERASING);
  782. /* Check, if it is write protected */
  783. if (ret) {
  784. if (ret == -EPERM)
  785. DEBUG(MTD_DEBUG_LEVEL0,
  786. "onenand_erase: Device is write protected!!!\n");
  787. else
  788. DEBUG(MTD_DEBUG_LEVEL0,
  789. "onenand_erase: Failed erase, block %d\n",
  790. (unsigned)(addr >> this->erase_shift));
  791. instr->state = MTD_ERASE_FAILED;
  792. instr->fail_addr = addr;
  793. goto erase_exit;
  794. }
  795. len -= block_size;
  796. addr += block_size;
  797. }
  798. instr->state = MTD_ERASE_DONE;
  799. erase_exit:
  800. ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
  801. /* Do call back function */
  802. if (!ret)
  803. mtd_erase_callback(instr);
  804. /* Deselect and wake up anyone waiting on the device */
  805. onenand_release_device(mtd);
  806. return ret;
  807. }
  808. /**
  809. * onenand_sync - [MTD Interface] sync
  810. * @param mtd MTD device structure
  811. *
  812. * Sync is actually a wait for chip ready function
  813. */
  814. void onenand_sync(struct mtd_info *mtd)
  815. {
  816. DEBUG(MTD_DEBUG_LEVEL3, "onenand_sync: called\n");
  817. /* Grab the lock and see if the device is available */
  818. onenand_get_device(mtd, FL_SYNCING);
  819. /* Release it and go back */
  820. onenand_release_device(mtd);
  821. }
  822. /**
  823. * onenand_block_isbad - [MTD Interface] Check whether the block at the given offset is bad
  824. * @param mtd MTD device structure
  825. * @param ofs offset relative to mtd start
  826. */
  827. int onenand_block_isbad(struct mtd_info *mtd, loff_t ofs)
  828. {
  829. /*
  830. * TODO
  831. * 1. Bad block table (BBT)
  832. * -> using NAND BBT to support JFFS2
  833. * 2. Bad block management (BBM)
  834. * -> bad block replace scheme
  835. *
  836. * Currently we do nothing
  837. */
  838. return 0;
  839. }
  840. /**
  841. * onenand_block_markbad - [MTD Interface] Mark the block at the given offset as bad
  842. * @param mtd MTD device structure
  843. * @param ofs offset relative to mtd start
  844. */
  845. int onenand_block_markbad(struct mtd_info *mtd, loff_t ofs)
  846. {
  847. /* see above */
  848. return 0;
  849. }
  850. /**
  851. * onenand_unlock - [MTD Interface] Unlock block(s)
  852. * @param mtd MTD device structure
  853. * @param ofs offset relative to mtd start
  854. * @param len number of bytes to unlock
  855. *
  856. * Unlock one or more blocks
  857. */
  858. int onenand_unlock(struct mtd_info *mtd, loff_t ofs, size_t len)
  859. {
  860. struct onenand_chip *this = mtd->priv;
  861. int start, end, block, value, status;
  862. start = ofs >> this->erase_shift;
  863. end = len >> this->erase_shift;
  864. /* Continuous lock scheme */
  865. if (this->options & ONENAND_CONT_LOCK) {
  866. /* Set start block address */
  867. this->write_word(start,
  868. this->base + ONENAND_REG_START_BLOCK_ADDRESS);
  869. /* Set end block address */
  870. this->write_word(end - 1,
  871. this->base + ONENAND_REG_END_BLOCK_ADDRESS);
  872. /* Write unlock command */
  873. this->command(mtd, ONENAND_CMD_UNLOCK, 0, 0);
  874. /* There's no return value */
  875. this->wait(mtd, FL_UNLOCKING);
  876. /* Sanity check */
  877. while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
  878. & ONENAND_CTRL_ONGO)
  879. continue;
  880. /* Check lock status */
  881. status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
  882. if (!(status & ONENAND_WP_US))
  883. printk(KERN_ERR "wp status = 0x%x\n", status);
  884. return 0;
  885. }
  886. /* Block lock scheme */
  887. for (block = start; block < end; block++) {
  888. /* Set start block address */
  889. this->write_word(block,
  890. this->base + ONENAND_REG_START_BLOCK_ADDRESS);
  891. /* Write unlock command */
  892. this->command(mtd, ONENAND_CMD_UNLOCK, 0, 0);
  893. /* There's no return value */
  894. this->wait(mtd, FL_UNLOCKING);
  895. /* Sanity check */
  896. while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
  897. & ONENAND_CTRL_ONGO)
  898. continue;
  899. /* Set block address for read block status */
  900. value = onenand_block_address(this->device_id, block);
  901. this->write_word(value,
  902. this->base + ONENAND_REG_START_ADDRESS1);
  903. /* Check lock status */
  904. status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
  905. if (!(status & ONENAND_WP_US))
  906. printk(KERN_ERR "block = %d, wp status = 0x%x\n",
  907. block, status);
  908. }
  909. return 0;
  910. }
  911. /**
  912. * onenand_print_device_info - Print device ID
  913. * @param device device ID
  914. *
  915. * Print device ID
  916. */
  917. void onenand_print_device_info(int device, int verbose)
  918. {
  919. int vcc, demuxed, ddp, density;
  920. if (!verbose)
  921. return;
  922. vcc = device & ONENAND_DEVICE_VCC_MASK;
  923. demuxed = device & ONENAND_DEVICE_IS_DEMUX;
  924. ddp = device & ONENAND_DEVICE_IS_DDP;
  925. density = device >> ONENAND_DEVICE_DENSITY_SHIFT;
  926. printk(KERN_INFO "%sOneNAND%s %dMB %sV 16-bit (0x%02x)\n",
  927. demuxed ? "" : "Muxed ",
  928. ddp ? "(DDP)" : "",
  929. (16 << density), vcc ? "2.65/3.3" : "1.8", device);
  930. }
  931. static const struct onenand_manufacturers onenand_manuf_ids[] = {
  932. {ONENAND_MFR_SAMSUNG, "Samsung"},
  933. {ONENAND_MFR_UNKNOWN, "Unknown"}
  934. };
  935. /**
  936. * onenand_check_maf - Check manufacturer ID
  937. * @param manuf manufacturer ID
  938. *
  939. * Check manufacturer ID
  940. */
  941. static int onenand_check_maf(int manuf)
  942. {
  943. int i;
  944. for (i = 0; onenand_manuf_ids[i].id; i++) {
  945. if (manuf == onenand_manuf_ids[i].id)
  946. break;
  947. }
  948. #ifdef ONENAND_DEBUG
  949. printk(KERN_DEBUG "OneNAND Manufacturer: %s (0x%0x)\n",
  950. onenand_manuf_ids[i].name, manuf);
  951. #endif
  952. return (i != ONENAND_MFR_UNKNOWN);
  953. }
  954. /**
  955. * onenand_probe - [OneNAND Interface] Probe the OneNAND device
  956. * @param mtd MTD device structure
  957. *
  958. * OneNAND detection method:
  959. * Compare the the values from command with ones from register
  960. */
  961. static int onenand_probe(struct mtd_info *mtd)
  962. {
  963. struct onenand_chip *this = mtd->priv;
  964. int bram_maf_id, bram_dev_id, maf_id, dev_id;
  965. int version_id;
  966. int density;
  967. /* Send the command for reading device ID from BootRAM */
  968. this->write_word(ONENAND_CMD_READID, this->base + ONENAND_BOOTRAM);
  969. /* Read manufacturer and device IDs from BootRAM */
  970. bram_maf_id = this->read_word(this->base + ONENAND_BOOTRAM + 0x0);
  971. bram_dev_id = this->read_word(this->base + ONENAND_BOOTRAM + 0x2);
  972. /* Check manufacturer ID */
  973. if (onenand_check_maf(bram_maf_id))
  974. return -ENXIO;
  975. /* Reset OneNAND to read default register values */
  976. this->write_word(ONENAND_CMD_RESET, this->base + ONENAND_BOOTRAM);
  977. {
  978. int i;
  979. for (i = 0; i < 10000; i++) ;
  980. }
  981. /* Read manufacturer and device IDs from Register */
  982. maf_id = this->read_word(this->base + ONENAND_REG_MANUFACTURER_ID);
  983. dev_id = this->read_word(this->base + ONENAND_REG_DEVICE_ID);
  984. /* Check OneNAND device */
  985. if (maf_id != bram_maf_id || dev_id != bram_dev_id)
  986. return -ENXIO;
  987. /* FIXME : Current OneNAND MTD doesn't support Flex-OneNAND */
  988. if (dev_id & (1 << 9)) {
  989. printk("Not yet support Flex-OneNAND\n");
  990. return -ENXIO;
  991. }
  992. /* Flash device information */
  993. onenand_print_device_info(dev_id, 0);
  994. this->device_id = dev_id;
  995. density = dev_id >> ONENAND_DEVICE_DENSITY_SHIFT;
  996. this->chipsize = (16 << density) << 20;
  997. /* OneNAND page size & block size */
  998. /* The data buffer size is equal to page size */
  999. mtd->oobblock =
  1000. this->read_word(this->base + ONENAND_REG_DATA_BUFFER_SIZE);
  1001. mtd->oobsize = mtd->oobblock >> 5;
  1002. /* Pagers per block is always 64 in OneNAND */
  1003. mtd->erasesize = mtd->oobblock << 6;
  1004. this->erase_shift = ffs(mtd->erasesize) - 1;
  1005. this->page_shift = ffs(mtd->oobblock) - 1;
  1006. this->ppb_shift = (this->erase_shift - this->page_shift);
  1007. this->page_mask = (mtd->erasesize / mtd->oobblock) - 1;
  1008. /* REVIST: Multichip handling */
  1009. mtd->size = this->chipsize;
  1010. /* Version ID */
  1011. version_id = this->read_word(this->base + ONENAND_REG_VERSION_ID);
  1012. #ifdef ONENAND_DEBUG
  1013. printk(KERN_DEBUG "OneNAND version = 0x%04x\n", version_id);
  1014. #endif
  1015. /* Lock scheme */
  1016. if (density <= ONENAND_DEVICE_DENSITY_512Mb &&
  1017. !(version_id >> ONENAND_VERSION_PROCESS_SHIFT)) {
  1018. printk(KERN_INFO "Lock scheme is Continues Lock\n");
  1019. this->options |= ONENAND_CONT_LOCK;
  1020. }
  1021. return 0;
  1022. }
  1023. /**
  1024. * onenand_scan - [OneNAND Interface] Scan for the OneNAND device
  1025. * @param mtd MTD device structure
  1026. * @param maxchips Number of chips to scan for
  1027. *
  1028. * This fills out all the not initialized function pointers
  1029. * with the defaults.
  1030. * The flash ID is read and the mtd/chip structures are
  1031. * filled with the appropriate values.
  1032. */
  1033. int onenand_scan(struct mtd_info *mtd, int maxchips)
  1034. {
  1035. struct onenand_chip *this = mtd->priv;
  1036. if (!this->read_word)
  1037. this->read_word = onenand_readw;
  1038. if (!this->write_word)
  1039. this->write_word = onenand_writew;
  1040. if (!this->command)
  1041. this->command = onenand_command;
  1042. if (!this->wait)
  1043. this->wait = onenand_wait;
  1044. if (!this->read_bufferram)
  1045. this->read_bufferram = onenand_read_bufferram;
  1046. if (!this->write_bufferram)
  1047. this->write_bufferram = onenand_write_bufferram;
  1048. if (onenand_probe(mtd))
  1049. return -ENXIO;
  1050. /* Set Sync. Burst Read after probing */
  1051. if (this->mmcontrol) {
  1052. printk(KERN_INFO "OneNAND Sync. Burst Read support\n");
  1053. this->read_bufferram = onenand_sync_read_bufferram;
  1054. }
  1055. onenand_unlock(mtd, 0, mtd->size);
  1056. return onenand_default_bbt(mtd);
  1057. }
  1058. /**
  1059. * onenand_release - [OneNAND Interface] Free resources held by the OneNAND device
  1060. * @param mtd MTD device structure
  1061. */
  1062. void onenand_release(struct mtd_info *mtd)
  1063. {
  1064. }
  1065. #endif /* CONFIG_CMD_ONENAND */