onenand_base.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298
  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. * @param block block address
  574. * @param page page address
  575. *
  576. * Check DataRAM area directly
  577. */
  578. static int onenand_verify_page(struct mtd_info *mtd, u_char * buf,
  579. loff_t addr, int block, int page)
  580. {
  581. struct onenand_chip *this = mtd->priv;
  582. void __iomem *dataram0, *dataram1;
  583. int ret = 0;
  584. this->command(mtd, ONENAND_CMD_READ, addr, mtd->oobblock);
  585. ret = this->wait(mtd, FL_READING);
  586. if (ret)
  587. return ret;
  588. onenand_update_bufferram(mtd, addr, 1);
  589. /* Check, if the two dataram areas are same */
  590. dataram0 = this->base + ONENAND_DATARAM;
  591. dataram1 = dataram0 + mtd->oobblock;
  592. if (memcmp(dataram0, dataram1, mtd->oobblock))
  593. return -EBADMSG;
  594. return 0;
  595. }
  596. #else
  597. #define onenand_verify_page(...) (0)
  598. #endif
  599. #define NOTALIGNED(x) ((x & (mtd->oobblock - 1)) != 0)
  600. /**
  601. * onenand_write_ecc - [MTD Interface] OneNAND write with ECC
  602. * @param mtd MTD device structure
  603. * @param to offset to write to
  604. * @param len number of bytes to write
  605. * @param retlen pointer to variable to store the number of written bytes
  606. * @param buf the data to write
  607. * @param eccbuf filesystem supplied oob data buffer
  608. * @param oobsel oob selection structure
  609. *
  610. * OneNAND write with ECC
  611. */
  612. static int onenand_write_ecc(struct mtd_info *mtd, loff_t to, size_t len,
  613. size_t * retlen, const u_char * buf,
  614. u_char * eccbuf, struct nand_oobinfo *oobsel)
  615. {
  616. struct onenand_chip *this = mtd->priv;
  617. int written = 0;
  618. int ret = 0;
  619. MTDDEBUG (MTD_DEBUG_LEVEL3, "onenand_write_ecc: "
  620. "to = 0x%08x, len = %i\n",
  621. (unsigned int)to, (int)len);
  622. /* Initialize retlen, in case of early exit */
  623. *retlen = 0;
  624. /* Do not allow writes past end of device */
  625. if (unlikely((to + len) > mtd->size)) {
  626. MTDDEBUG (MTD_DEBUG_LEVEL0, "onenand_write_ecc: "
  627. "Attempt write to past end of device\n");
  628. return -EINVAL;
  629. }
  630. /* Reject writes, which are not page aligned */
  631. if (unlikely(NOTALIGNED(to)) || unlikely(NOTALIGNED(len))) {
  632. MTDDEBUG (MTD_DEBUG_LEVEL0, "onenand_write_ecc: "
  633. "Attempt to write not page aligned data\n");
  634. return -EINVAL;
  635. }
  636. /* Grab the lock and see if the device is available */
  637. onenand_get_device(mtd, FL_WRITING);
  638. /* Loop until all data write */
  639. while (written < len) {
  640. int thislen = min_t(int, mtd->oobblock, len - written);
  641. this->command(mtd, ONENAND_CMD_BUFFERRAM, to, mtd->oobblock);
  642. this->write_bufferram(mtd, ONENAND_DATARAM, buf, 0, thislen);
  643. this->write_bufferram(mtd, ONENAND_SPARERAM, ffchars, 0,
  644. mtd->oobsize);
  645. this->command(mtd, ONENAND_CMD_PROG, to, mtd->oobblock);
  646. onenand_update_bufferram(mtd, to, 1);
  647. ret = this->wait(mtd, FL_WRITING);
  648. if (ret) {
  649. MTDDEBUG (MTD_DEBUG_LEVEL0,
  650. "onenand_write_ecc: write filaed %d\n", ret);
  651. break;
  652. }
  653. written += thislen;
  654. /* Only check verify write turn on */
  655. ret = onenand_verify_page(mtd, (u_char *) buf, to, block, page);
  656. if (ret) {
  657. MTDDEBUG (MTD_DEBUG_LEVEL0,
  658. "onenand_write_ecc: verify failed %d\n", ret);
  659. break;
  660. }
  661. if (written == len)
  662. break;
  663. to += thislen;
  664. buf += thislen;
  665. }
  666. /* Deselect and wake up anyone waiting on the device */
  667. onenand_release_device(mtd);
  668. *retlen = written;
  669. return ret;
  670. }
  671. /**
  672. * onenand_write - [MTD Interface] compability function for onenand_write_ecc
  673. * @param mtd MTD device structure
  674. * @param to offset to write to
  675. * @param len number of bytes to write
  676. * @param retlen pointer to variable to store the number of written bytes
  677. * @param buf the data to write
  678. *
  679. * This function simply calls onenand_write_ecc
  680. * with oob buffer and oobsel = NULL
  681. */
  682. int onenand_write(struct mtd_info *mtd, loff_t to, size_t len,
  683. size_t * retlen, const u_char * buf)
  684. {
  685. return onenand_write_ecc(mtd, to, len, retlen, buf, NULL, NULL);
  686. }
  687. /**
  688. * onenand_write_oob - [MTD Interface] OneNAND write out-of-band
  689. * @param mtd MTD device structure
  690. * @param to offset to write to
  691. * @param len number of bytes to write
  692. * @param retlen pointer to variable to store the number of written bytes
  693. * @param buf the data to write
  694. *
  695. * OneNAND write out-of-band
  696. */
  697. int onenand_write_oob(struct mtd_info *mtd, loff_t to, size_t len,
  698. size_t * retlen, const u_char * buf)
  699. {
  700. struct onenand_chip *this = mtd->priv;
  701. int column, status;
  702. int written = 0;
  703. MTDDEBUG (MTD_DEBUG_LEVEL3, "onenand_write_oob: "
  704. "to = 0x%08x, len = %i\n",
  705. (unsigned int)to, (int)len);
  706. /* Initialize retlen, in case of early exit */
  707. *retlen = 0;
  708. /* Do not allow writes past end of device */
  709. if (unlikely((to + len) > mtd->size)) {
  710. MTDDEBUG (MTD_DEBUG_LEVEL0, "onenand_write_oob: "
  711. "Attempt write to past end of device\n");
  712. return -EINVAL;
  713. }
  714. /* Grab the lock and see if the device is available */
  715. onenand_get_device(mtd, FL_WRITING);
  716. /* Loop until all data write */
  717. while (written < len) {
  718. int thislen = min_t(int, mtd->oobsize, len - written);
  719. column = to & (mtd->oobsize - 1);
  720. this->command(mtd, ONENAND_CMD_BUFFERRAM, to, mtd->oobsize);
  721. this->write_bufferram(mtd, ONENAND_SPARERAM, ffchars, 0,
  722. mtd->oobsize);
  723. this->write_bufferram(mtd, ONENAND_SPARERAM, buf, column,
  724. thislen);
  725. this->command(mtd, ONENAND_CMD_PROGOOB, to, mtd->oobsize);
  726. onenand_update_bufferram(mtd, to, 0);
  727. status = this->wait(mtd, FL_WRITING);
  728. if (status)
  729. break;
  730. written += thislen;
  731. if (written == len)
  732. break;
  733. to += thislen;
  734. buf += thislen;
  735. }
  736. /* Deselect and wake up anyone waiting on the device */
  737. onenand_release_device(mtd);
  738. *retlen = written;
  739. return 0;
  740. }
  741. /**
  742. * onenand_erase - [MTD Interface] erase block(s)
  743. * @param mtd MTD device structure
  744. * @param instr erase instruction
  745. *
  746. * Erase one ore more blocks
  747. */
  748. int onenand_erase(struct mtd_info *mtd, struct erase_info *instr)
  749. {
  750. struct onenand_chip *this = mtd->priv;
  751. unsigned int block_size;
  752. loff_t addr;
  753. int len;
  754. int ret = 0;
  755. MTDDEBUG (MTD_DEBUG_LEVEL3, "onenand_erase: start = 0x%08x, len = %i\n",
  756. (unsigned int)instr->addr, (unsigned int)instr->len);
  757. block_size = (1 << this->erase_shift);
  758. /* Start address must align on block boundary */
  759. if (unlikely(instr->addr & (block_size - 1))) {
  760. MTDDEBUG (MTD_DEBUG_LEVEL0,
  761. "onenand_erase: Unaligned address\n");
  762. return -EINVAL;
  763. }
  764. /* Length must align on block boundary */
  765. if (unlikely(instr->len & (block_size - 1))) {
  766. MTDDEBUG (MTD_DEBUG_LEVEL0,
  767. "onenand_erase: Length not block aligned\n");
  768. return -EINVAL;
  769. }
  770. /* Do not allow erase past end of device */
  771. if (unlikely((instr->len + instr->addr) > mtd->size)) {
  772. MTDDEBUG (MTD_DEBUG_LEVEL0,
  773. "onenand_erase: Erase past end of device\n");
  774. return -EINVAL;
  775. }
  776. instr->fail_addr = 0xffffffff;
  777. /* Grab the lock and see if the device is available */
  778. onenand_get_device(mtd, FL_ERASING);
  779. /* Loop throught the pages */
  780. len = instr->len;
  781. addr = instr->addr;
  782. instr->state = MTD_ERASING;
  783. while (len) {
  784. /* TODO Check badblock */
  785. this->command(mtd, ONENAND_CMD_ERASE, addr, block_size);
  786. ret = this->wait(mtd, FL_ERASING);
  787. /* Check, if it is write protected */
  788. if (ret) {
  789. if (ret == -EPERM)
  790. MTDDEBUG (MTD_DEBUG_LEVEL0, "onenand_erase: "
  791. "Device is write protected!!!\n");
  792. else
  793. MTDDEBUG (MTD_DEBUG_LEVEL0, "onenand_erase: "
  794. "Failed erase, block %d\n",
  795. (unsigned)(addr >> this->erase_shift));
  796. instr->state = MTD_ERASE_FAILED;
  797. instr->fail_addr = addr;
  798. goto erase_exit;
  799. }
  800. len -= block_size;
  801. addr += block_size;
  802. }
  803. instr->state = MTD_ERASE_DONE;
  804. erase_exit:
  805. ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
  806. /* Do call back function */
  807. if (!ret)
  808. mtd_erase_callback(instr);
  809. /* Deselect and wake up anyone waiting on the device */
  810. onenand_release_device(mtd);
  811. return ret;
  812. }
  813. /**
  814. * onenand_sync - [MTD Interface] sync
  815. * @param mtd MTD device structure
  816. *
  817. * Sync is actually a wait for chip ready function
  818. */
  819. void onenand_sync(struct mtd_info *mtd)
  820. {
  821. MTDDEBUG (MTD_DEBUG_LEVEL3, "onenand_sync: called\n");
  822. /* Grab the lock and see if the device is available */
  823. onenand_get_device(mtd, FL_SYNCING);
  824. /* Release it and go back */
  825. onenand_release_device(mtd);
  826. }
  827. /**
  828. * onenand_block_isbad - [MTD Interface] Check whether the block at the given offset is bad
  829. * @param mtd MTD device structure
  830. * @param ofs offset relative to mtd start
  831. */
  832. int onenand_block_isbad(struct mtd_info *mtd, loff_t ofs)
  833. {
  834. /*
  835. * TODO
  836. * 1. Bad block table (BBT)
  837. * -> using NAND BBT to support JFFS2
  838. * 2. Bad block management (BBM)
  839. * -> bad block replace scheme
  840. *
  841. * Currently we do nothing
  842. */
  843. return 0;
  844. }
  845. /**
  846. * onenand_block_markbad - [MTD Interface] Mark the block at the given offset as bad
  847. * @param mtd MTD device structure
  848. * @param ofs offset relative to mtd start
  849. */
  850. int onenand_block_markbad(struct mtd_info *mtd, loff_t ofs)
  851. {
  852. /* see above */
  853. return 0;
  854. }
  855. /**
  856. * onenand_unlock - [MTD Interface] Unlock block(s)
  857. * @param mtd MTD device structure
  858. * @param ofs offset relative to mtd start
  859. * @param len number of bytes to unlock
  860. *
  861. * Unlock one or more blocks
  862. */
  863. int onenand_unlock(struct mtd_info *mtd, loff_t ofs, size_t len)
  864. {
  865. struct onenand_chip *this = mtd->priv;
  866. int start, end, block, value, status;
  867. start = ofs >> this->erase_shift;
  868. end = len >> this->erase_shift;
  869. /* Continuous lock scheme */
  870. if (this->options & ONENAND_CONT_LOCK) {
  871. /* Set start block address */
  872. this->write_word(start,
  873. this->base + ONENAND_REG_START_BLOCK_ADDRESS);
  874. /* Set end block address */
  875. this->write_word(end - 1,
  876. this->base + ONENAND_REG_END_BLOCK_ADDRESS);
  877. /* Write unlock command */
  878. this->command(mtd, ONENAND_CMD_UNLOCK, 0, 0);
  879. /* There's no return value */
  880. this->wait(mtd, FL_UNLOCKING);
  881. /* Sanity check */
  882. while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
  883. & ONENAND_CTRL_ONGO)
  884. continue;
  885. /* Check lock status */
  886. status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
  887. if (!(status & ONENAND_WP_US))
  888. printk(KERN_ERR "wp status = 0x%x\n", status);
  889. return 0;
  890. }
  891. /* Block lock scheme */
  892. for (block = start; block < end; block++) {
  893. /* Set start block address */
  894. this->write_word(block,
  895. this->base + ONENAND_REG_START_BLOCK_ADDRESS);
  896. /* Write unlock command */
  897. this->command(mtd, ONENAND_CMD_UNLOCK, 0, 0);
  898. /* There's no return value */
  899. this->wait(mtd, FL_UNLOCKING);
  900. /* Sanity check */
  901. while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
  902. & ONENAND_CTRL_ONGO)
  903. continue;
  904. /* Set block address for read block status */
  905. value = onenand_block_address(this->device_id, block);
  906. this->write_word(value,
  907. this->base + ONENAND_REG_START_ADDRESS1);
  908. /* Check lock status */
  909. status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
  910. if (!(status & ONENAND_WP_US))
  911. printk(KERN_ERR "block = %d, wp status = 0x%x\n",
  912. block, status);
  913. }
  914. return 0;
  915. }
  916. /**
  917. * onenand_print_device_info - Print device ID
  918. * @param device device ID
  919. *
  920. * Print device ID
  921. */
  922. void onenand_print_device_info(int device, int verbose)
  923. {
  924. int vcc, demuxed, ddp, density;
  925. if (!verbose)
  926. return;
  927. vcc = device & ONENAND_DEVICE_VCC_MASK;
  928. demuxed = device & ONENAND_DEVICE_IS_DEMUX;
  929. ddp = device & ONENAND_DEVICE_IS_DDP;
  930. density = device >> ONENAND_DEVICE_DENSITY_SHIFT;
  931. printk(KERN_INFO "%sOneNAND%s %dMB %sV 16-bit (0x%02x)\n",
  932. demuxed ? "" : "Muxed ",
  933. ddp ? "(DDP)" : "",
  934. (16 << density), vcc ? "2.65/3.3" : "1.8", device);
  935. }
  936. static const struct onenand_manufacturers onenand_manuf_ids[] = {
  937. {ONENAND_MFR_SAMSUNG, "Samsung"},
  938. {ONENAND_MFR_UNKNOWN, "Unknown"}
  939. };
  940. /**
  941. * onenand_check_maf - Check manufacturer ID
  942. * @param manuf manufacturer ID
  943. *
  944. * Check manufacturer ID
  945. */
  946. static int onenand_check_maf(int manuf)
  947. {
  948. int i;
  949. for (i = 0; onenand_manuf_ids[i].id; i++) {
  950. if (manuf == onenand_manuf_ids[i].id)
  951. break;
  952. }
  953. #ifdef ONENAND_DEBUG
  954. printk(KERN_DEBUG "OneNAND Manufacturer: %s (0x%0x)\n",
  955. onenand_manuf_ids[i].name, manuf);
  956. #endif
  957. return (i != ONENAND_MFR_UNKNOWN);
  958. }
  959. /**
  960. * onenand_probe - [OneNAND Interface] Probe the OneNAND device
  961. * @param mtd MTD device structure
  962. *
  963. * OneNAND detection method:
  964. * Compare the the values from command with ones from register
  965. */
  966. static int onenand_probe(struct mtd_info *mtd)
  967. {
  968. struct onenand_chip *this = mtd->priv;
  969. int bram_maf_id, bram_dev_id, maf_id, dev_id;
  970. int version_id;
  971. int density;
  972. /* Send the command for reading device ID from BootRAM */
  973. this->write_word(ONENAND_CMD_READID, this->base + ONENAND_BOOTRAM);
  974. /* Read manufacturer and device IDs from BootRAM */
  975. bram_maf_id = this->read_word(this->base + ONENAND_BOOTRAM + 0x0);
  976. bram_dev_id = this->read_word(this->base + ONENAND_BOOTRAM + 0x2);
  977. /* Check manufacturer ID */
  978. if (onenand_check_maf(bram_maf_id))
  979. return -ENXIO;
  980. /* Reset OneNAND to read default register values */
  981. this->write_word(ONENAND_CMD_RESET, this->base + ONENAND_BOOTRAM);
  982. {
  983. int i;
  984. for (i = 0; i < 10000; i++) ;
  985. }
  986. /* Read manufacturer and device IDs from Register */
  987. maf_id = this->read_word(this->base + ONENAND_REG_MANUFACTURER_ID);
  988. dev_id = this->read_word(this->base + ONENAND_REG_DEVICE_ID);
  989. /* Check OneNAND device */
  990. if (maf_id != bram_maf_id || dev_id != bram_dev_id)
  991. return -ENXIO;
  992. /* FIXME : Current OneNAND MTD doesn't support Flex-OneNAND */
  993. if (dev_id & (1 << 9)) {
  994. printk("Not yet support Flex-OneNAND\n");
  995. return -ENXIO;
  996. }
  997. /* Flash device information */
  998. onenand_print_device_info(dev_id, 0);
  999. this->device_id = dev_id;
  1000. density = dev_id >> ONENAND_DEVICE_DENSITY_SHIFT;
  1001. this->chipsize = (16 << density) << 20;
  1002. /* OneNAND page size & block size */
  1003. /* The data buffer size is equal to page size */
  1004. mtd->oobblock =
  1005. this->read_word(this->base + ONENAND_REG_DATA_BUFFER_SIZE);
  1006. mtd->oobsize = mtd->oobblock >> 5;
  1007. /* Pagers per block is always 64 in OneNAND */
  1008. mtd->erasesize = mtd->oobblock << 6;
  1009. this->erase_shift = ffs(mtd->erasesize) - 1;
  1010. this->page_shift = ffs(mtd->oobblock) - 1;
  1011. this->ppb_shift = (this->erase_shift - this->page_shift);
  1012. this->page_mask = (mtd->erasesize / mtd->oobblock) - 1;
  1013. /* REVIST: Multichip handling */
  1014. mtd->size = this->chipsize;
  1015. /* Version ID */
  1016. version_id = this->read_word(this->base + ONENAND_REG_VERSION_ID);
  1017. #ifdef ONENAND_DEBUG
  1018. printk(KERN_DEBUG "OneNAND version = 0x%04x\n", version_id);
  1019. #endif
  1020. /* Lock scheme */
  1021. if (density <= ONENAND_DEVICE_DENSITY_512Mb &&
  1022. !(version_id >> ONENAND_VERSION_PROCESS_SHIFT)) {
  1023. printk(KERN_INFO "Lock scheme is Continues Lock\n");
  1024. this->options |= ONENAND_CONT_LOCK;
  1025. }
  1026. return 0;
  1027. }
  1028. /**
  1029. * onenand_scan - [OneNAND Interface] Scan for the OneNAND device
  1030. * @param mtd MTD device structure
  1031. * @param maxchips Number of chips to scan for
  1032. *
  1033. * This fills out all the not initialized function pointers
  1034. * with the defaults.
  1035. * The flash ID is read and the mtd/chip structures are
  1036. * filled with the appropriate values.
  1037. */
  1038. int onenand_scan(struct mtd_info *mtd, int maxchips)
  1039. {
  1040. struct onenand_chip *this = mtd->priv;
  1041. if (!this->read_word)
  1042. this->read_word = onenand_readw;
  1043. if (!this->write_word)
  1044. this->write_word = onenand_writew;
  1045. if (!this->command)
  1046. this->command = onenand_command;
  1047. if (!this->wait)
  1048. this->wait = onenand_wait;
  1049. if (!this->read_bufferram)
  1050. this->read_bufferram = onenand_read_bufferram;
  1051. if (!this->write_bufferram)
  1052. this->write_bufferram = onenand_write_bufferram;
  1053. if (onenand_probe(mtd))
  1054. return -ENXIO;
  1055. /* Set Sync. Burst Read after probing */
  1056. if (this->mmcontrol) {
  1057. printk(KERN_INFO "OneNAND Sync. Burst Read support\n");
  1058. this->read_bufferram = onenand_sync_read_bufferram;
  1059. }
  1060. onenand_unlock(mtd, 0, mtd->size);
  1061. return onenand_default_bbt(mtd);
  1062. }
  1063. /**
  1064. * onenand_release - [OneNAND Interface] Free resources held by the OneNAND device
  1065. * @param mtd MTD device structure
  1066. */
  1067. void onenand_release(struct mtd_info *mtd)
  1068. {
  1069. }
  1070. #endif /* CONFIG_CMD_ONENAND */