onenand_base.c 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967
  1. /*
  2. * linux/drivers/mtd/onenand/onenand_base.c
  3. *
  4. * Copyright (C) 2005-2006 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 <linux/kernel.h>
  12. #include <linux/module.h>
  13. #include <linux/init.h>
  14. #include <linux/sched.h>
  15. #include <linux/jiffies.h>
  16. #include <linux/mtd/mtd.h>
  17. #include <linux/mtd/onenand.h>
  18. #include <linux/mtd/partitions.h>
  19. #include <asm/io.h>
  20. /**
  21. * onenand_oob_64 - oob info for large (2KB) page
  22. */
  23. static struct nand_ecclayout onenand_oob_64 = {
  24. .eccbytes = 20,
  25. .eccpos = {
  26. 8, 9, 10, 11, 12,
  27. 24, 25, 26, 27, 28,
  28. 40, 41, 42, 43, 44,
  29. 56, 57, 58, 59, 60,
  30. },
  31. .oobfree = {
  32. {2, 3}, {14, 2}, {18, 3}, {30, 2},
  33. {34, 3}, {46, 2}, {50, 3}, {62, 2}
  34. }
  35. };
  36. /**
  37. * onenand_oob_32 - oob info for middle (1KB) page
  38. */
  39. static struct nand_ecclayout onenand_oob_32 = {
  40. .eccbytes = 10,
  41. .eccpos = {
  42. 8, 9, 10, 11, 12,
  43. 24, 25, 26, 27, 28,
  44. },
  45. .oobfree = { {2, 3}, {14, 2}, {18, 3}, {30, 2} }
  46. };
  47. static const unsigned char ffchars[] = {
  48. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  49. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 16 */
  50. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  51. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 32 */
  52. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  53. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 48 */
  54. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  55. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 64 */
  56. };
  57. /**
  58. * onenand_readw - [OneNAND Interface] Read OneNAND register
  59. * @param addr address to read
  60. *
  61. * Read OneNAND register
  62. */
  63. static unsigned short onenand_readw(void __iomem *addr)
  64. {
  65. return readw(addr);
  66. }
  67. /**
  68. * onenand_writew - [OneNAND Interface] Write OneNAND register with value
  69. * @param value value to write
  70. * @param addr address to write
  71. *
  72. * Write OneNAND register with value
  73. */
  74. static void onenand_writew(unsigned short value, void __iomem *addr)
  75. {
  76. writew(value, addr);
  77. }
  78. /**
  79. * onenand_block_address - [DEFAULT] Get block address
  80. * @param this onenand chip data structure
  81. * @param block the block
  82. * @return translated block address if DDP, otherwise same
  83. *
  84. * Setup Start Address 1 Register (F100h)
  85. */
  86. static int onenand_block_address(struct onenand_chip *this, int block)
  87. {
  88. if (this->device_id & ONENAND_DEVICE_IS_DDP) {
  89. /* Device Flash Core select, NAND Flash Block Address */
  90. int dfs = 0;
  91. if (block & this->density_mask)
  92. dfs = 1;
  93. return (dfs << ONENAND_DDP_SHIFT) |
  94. (block & (this->density_mask - 1));
  95. }
  96. return block;
  97. }
  98. /**
  99. * onenand_bufferram_address - [DEFAULT] Get bufferram address
  100. * @param this onenand chip data structure
  101. * @param block the block
  102. * @return set DBS value if DDP, otherwise 0
  103. *
  104. * Setup Start Address 2 Register (F101h) for DDP
  105. */
  106. static int onenand_bufferram_address(struct onenand_chip *this, int block)
  107. {
  108. if (this->device_id & ONENAND_DEVICE_IS_DDP) {
  109. /* Device BufferRAM Select */
  110. int dbs = 0;
  111. if (block & this->density_mask)
  112. dbs = 1;
  113. return (dbs << ONENAND_DDP_SHIFT);
  114. }
  115. return 0;
  116. }
  117. /**
  118. * onenand_page_address - [DEFAULT] Get page address
  119. * @param page the page address
  120. * @param sector the sector address
  121. * @return combined page and sector address
  122. *
  123. * Setup Start Address 8 Register (F107h)
  124. */
  125. static int onenand_page_address(int page, int sector)
  126. {
  127. /* Flash Page Address, Flash Sector Address */
  128. int fpa, fsa;
  129. fpa = page & ONENAND_FPA_MASK;
  130. fsa = sector & ONENAND_FSA_MASK;
  131. return ((fpa << ONENAND_FPA_SHIFT) | fsa);
  132. }
  133. /**
  134. * onenand_buffer_address - [DEFAULT] Get buffer address
  135. * @param dataram1 DataRAM index
  136. * @param sectors the sector address
  137. * @param count the number of sectors
  138. * @return the start buffer value
  139. *
  140. * Setup Start Buffer Register (F200h)
  141. */
  142. static int onenand_buffer_address(int dataram1, int sectors, int count)
  143. {
  144. int bsa, bsc;
  145. /* BufferRAM Sector Address */
  146. bsa = sectors & ONENAND_BSA_MASK;
  147. if (dataram1)
  148. bsa |= ONENAND_BSA_DATARAM1; /* DataRAM1 */
  149. else
  150. bsa |= ONENAND_BSA_DATARAM0; /* DataRAM0 */
  151. /* BufferRAM Sector Count */
  152. bsc = count & ONENAND_BSC_MASK;
  153. return ((bsa << ONENAND_BSA_SHIFT) | bsc);
  154. }
  155. /**
  156. * onenand_command - [DEFAULT] Send command to OneNAND device
  157. * @param mtd MTD device structure
  158. * @param cmd the command to be sent
  159. * @param addr offset to read from or write to
  160. * @param len number of bytes to read or write
  161. *
  162. * Send command to OneNAND device. This function is used for middle/large page
  163. * devices (1KB/2KB Bytes per page)
  164. */
  165. static int onenand_command(struct mtd_info *mtd, int cmd, loff_t addr, size_t len)
  166. {
  167. struct onenand_chip *this = mtd->priv;
  168. int value, readcmd = 0, block_cmd = 0;
  169. int block, page;
  170. /* Now we use page size operation */
  171. int sectors = 4, count = 4;
  172. /* Address translation */
  173. switch (cmd) {
  174. case ONENAND_CMD_UNLOCK:
  175. case ONENAND_CMD_LOCK:
  176. case ONENAND_CMD_LOCK_TIGHT:
  177. case ONENAND_CMD_UNLOCK_ALL:
  178. block = -1;
  179. page = -1;
  180. break;
  181. case ONENAND_CMD_ERASE:
  182. case ONENAND_CMD_BUFFERRAM:
  183. case ONENAND_CMD_OTP_ACCESS:
  184. block_cmd = 1;
  185. block = (int) (addr >> this->erase_shift);
  186. page = -1;
  187. break;
  188. default:
  189. block = (int) (addr >> this->erase_shift);
  190. page = (int) (addr >> this->page_shift);
  191. page &= this->page_mask;
  192. break;
  193. }
  194. /* NOTE: The setting order of the registers is very important! */
  195. if (cmd == ONENAND_CMD_BUFFERRAM) {
  196. /* Select DataRAM for DDP */
  197. value = onenand_bufferram_address(this, block);
  198. this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
  199. /* Switch to the next data buffer */
  200. ONENAND_SET_NEXT_BUFFERRAM(this);
  201. return 0;
  202. }
  203. if (block != -1) {
  204. /* Write 'DFS, FBA' of Flash */
  205. value = onenand_block_address(this, block);
  206. this->write_word(value, this->base + ONENAND_REG_START_ADDRESS1);
  207. if (block_cmd) {
  208. /* Select DataRAM for DDP */
  209. value = onenand_bufferram_address(this, block);
  210. this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
  211. }
  212. }
  213. if (page != -1) {
  214. int dataram;
  215. switch (cmd) {
  216. case ONENAND_CMD_READ:
  217. case ONENAND_CMD_READOOB:
  218. dataram = ONENAND_SET_NEXT_BUFFERRAM(this);
  219. readcmd = 1;
  220. break;
  221. default:
  222. dataram = ONENAND_CURRENT_BUFFERRAM(this);
  223. break;
  224. }
  225. /* Write 'FPA, FSA' of Flash */
  226. value = onenand_page_address(page, sectors);
  227. this->write_word(value, this->base + ONENAND_REG_START_ADDRESS8);
  228. /* Write 'BSA, BSC' of DataRAM */
  229. value = onenand_buffer_address(dataram, sectors, count);
  230. this->write_word(value, this->base + ONENAND_REG_START_BUFFER);
  231. if (readcmd) {
  232. /* Select DataRAM for DDP */
  233. value = onenand_bufferram_address(this, block);
  234. this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
  235. }
  236. }
  237. /* Interrupt clear */
  238. this->write_word(ONENAND_INT_CLEAR, this->base + ONENAND_REG_INTERRUPT);
  239. /* Write command */
  240. this->write_word(cmd, this->base + ONENAND_REG_COMMAND);
  241. return 0;
  242. }
  243. /**
  244. * onenand_wait - [DEFAULT] wait until the command is done
  245. * @param mtd MTD device structure
  246. * @param state state to select the max. timeout value
  247. *
  248. * Wait for command done. This applies to all OneNAND command
  249. * Read can take up to 30us, erase up to 2ms and program up to 350us
  250. * according to general OneNAND specs
  251. */
  252. static int onenand_wait(struct mtd_info *mtd, int state)
  253. {
  254. struct onenand_chip * this = mtd->priv;
  255. unsigned long timeout;
  256. unsigned int flags = ONENAND_INT_MASTER;
  257. unsigned int interrupt = 0;
  258. unsigned int ctrl, ecc;
  259. /* The 20 msec is enough */
  260. timeout = jiffies + msecs_to_jiffies(20);
  261. while (time_before(jiffies, timeout)) {
  262. interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
  263. if (interrupt & flags)
  264. break;
  265. if (state != FL_READING)
  266. cond_resched();
  267. touch_softlockup_watchdog();
  268. }
  269. /* To get correct interrupt status in timeout case */
  270. interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
  271. ctrl = this->read_word(this->base + ONENAND_REG_CTRL_STATUS);
  272. if (ctrl & ONENAND_CTRL_ERROR) {
  273. /* It maybe occur at initial bad block */
  274. DEBUG(MTD_DEBUG_LEVEL0, "onenand_wait: controller error = 0x%04x\n", ctrl);
  275. /* Clear other interrupt bits for preventing ECC error */
  276. interrupt &= ONENAND_INT_MASTER;
  277. }
  278. if (ctrl & ONENAND_CTRL_LOCK) {
  279. DEBUG(MTD_DEBUG_LEVEL0, "onenand_wait: it's locked error = 0x%04x\n", ctrl);
  280. return -EACCES;
  281. }
  282. if (interrupt & ONENAND_INT_READ) {
  283. ecc = this->read_word(this->base + ONENAND_REG_ECC_STATUS);
  284. if (ecc & ONENAND_ECC_2BIT_ALL) {
  285. DEBUG(MTD_DEBUG_LEVEL0, "onenand_wait: ECC error = 0x%04x\n", ecc);
  286. return -EBADMSG;
  287. }
  288. }
  289. return 0;
  290. }
  291. /**
  292. * onenand_bufferram_offset - [DEFAULT] BufferRAM offset
  293. * @param mtd MTD data structure
  294. * @param area BufferRAM area
  295. * @return offset given area
  296. *
  297. * Return BufferRAM offset given area
  298. */
  299. static inline int onenand_bufferram_offset(struct mtd_info *mtd, int area)
  300. {
  301. struct onenand_chip *this = mtd->priv;
  302. if (ONENAND_CURRENT_BUFFERRAM(this)) {
  303. if (area == ONENAND_DATARAM)
  304. return mtd->writesize;
  305. if (area == ONENAND_SPARERAM)
  306. return mtd->oobsize;
  307. }
  308. return 0;
  309. }
  310. /**
  311. * onenand_read_bufferram - [OneNAND Interface] Read the bufferram area
  312. * @param mtd MTD data structure
  313. * @param area BufferRAM area
  314. * @param buffer the databuffer to put/get data
  315. * @param offset offset to read from or write to
  316. * @param count number of bytes to read/write
  317. *
  318. * Read the BufferRAM area
  319. */
  320. static int onenand_read_bufferram(struct mtd_info *mtd, int area,
  321. unsigned char *buffer, int offset, size_t count)
  322. {
  323. struct onenand_chip *this = mtd->priv;
  324. void __iomem *bufferram;
  325. bufferram = this->base + area;
  326. bufferram += onenand_bufferram_offset(mtd, area);
  327. if (ONENAND_CHECK_BYTE_ACCESS(count)) {
  328. unsigned short word;
  329. /* Align with word(16-bit) size */
  330. count--;
  331. /* Read word and save byte */
  332. word = this->read_word(bufferram + offset + count);
  333. buffer[count] = (word & 0xff);
  334. }
  335. memcpy(buffer, bufferram + offset, count);
  336. return 0;
  337. }
  338. /**
  339. * onenand_sync_read_bufferram - [OneNAND Interface] Read the bufferram area with Sync. Burst mode
  340. * @param mtd MTD data structure
  341. * @param area BufferRAM area
  342. * @param buffer the databuffer to put/get data
  343. * @param offset offset to read from or write to
  344. * @param count number of bytes to read/write
  345. *
  346. * Read the BufferRAM area with Sync. Burst Mode
  347. */
  348. static int onenand_sync_read_bufferram(struct mtd_info *mtd, int area,
  349. unsigned char *buffer, int offset, size_t count)
  350. {
  351. struct onenand_chip *this = mtd->priv;
  352. void __iomem *bufferram;
  353. bufferram = this->base + area;
  354. bufferram += onenand_bufferram_offset(mtd, area);
  355. this->mmcontrol(mtd, ONENAND_SYS_CFG1_SYNC_READ);
  356. if (ONENAND_CHECK_BYTE_ACCESS(count)) {
  357. unsigned short word;
  358. /* Align with word(16-bit) size */
  359. count--;
  360. /* Read word and save byte */
  361. word = this->read_word(bufferram + offset + count);
  362. buffer[count] = (word & 0xff);
  363. }
  364. memcpy(buffer, bufferram + offset, count);
  365. this->mmcontrol(mtd, 0);
  366. return 0;
  367. }
  368. /**
  369. * onenand_write_bufferram - [OneNAND Interface] Write the bufferram area
  370. * @param mtd MTD data structure
  371. * @param area BufferRAM area
  372. * @param buffer the databuffer to put/get data
  373. * @param offset offset to read from or write to
  374. * @param count number of bytes to read/write
  375. *
  376. * Write the BufferRAM area
  377. */
  378. static int onenand_write_bufferram(struct mtd_info *mtd, int area,
  379. const unsigned char *buffer, int offset, size_t count)
  380. {
  381. struct onenand_chip *this = mtd->priv;
  382. void __iomem *bufferram;
  383. bufferram = this->base + area;
  384. bufferram += onenand_bufferram_offset(mtd, area);
  385. if (ONENAND_CHECK_BYTE_ACCESS(count)) {
  386. unsigned short word;
  387. int byte_offset;
  388. /* Align with word(16-bit) size */
  389. count--;
  390. /* Calculate byte access offset */
  391. byte_offset = offset + count;
  392. /* Read word and save byte */
  393. word = this->read_word(bufferram + byte_offset);
  394. word = (word & ~0xff) | buffer[count];
  395. this->write_word(word, bufferram + byte_offset);
  396. }
  397. memcpy(bufferram + offset, buffer, count);
  398. return 0;
  399. }
  400. /**
  401. * onenand_check_bufferram - [GENERIC] Check BufferRAM information
  402. * @param mtd MTD data structure
  403. * @param addr address to check
  404. * @return 1 if there are valid data, otherwise 0
  405. *
  406. * Check bufferram if there is data we required
  407. */
  408. static int onenand_check_bufferram(struct mtd_info *mtd, loff_t addr)
  409. {
  410. struct onenand_chip *this = mtd->priv;
  411. int block, page;
  412. int i;
  413. block = (int) (addr >> this->erase_shift);
  414. page = (int) (addr >> this->page_shift);
  415. page &= this->page_mask;
  416. i = ONENAND_CURRENT_BUFFERRAM(this);
  417. /* Is there valid data? */
  418. if (this->bufferram[i].block == block &&
  419. this->bufferram[i].page == page &&
  420. this->bufferram[i].valid)
  421. return 1;
  422. return 0;
  423. }
  424. /**
  425. * onenand_update_bufferram - [GENERIC] Update BufferRAM information
  426. * @param mtd MTD data structure
  427. * @param addr address to update
  428. * @param valid valid flag
  429. *
  430. * Update BufferRAM information
  431. */
  432. static int onenand_update_bufferram(struct mtd_info *mtd, loff_t addr,
  433. int valid)
  434. {
  435. struct onenand_chip *this = mtd->priv;
  436. int block, page;
  437. int i;
  438. block = (int) (addr >> this->erase_shift);
  439. page = (int) (addr >> this->page_shift);
  440. page &= this->page_mask;
  441. /* Invalidate BufferRAM */
  442. for (i = 0; i < MAX_BUFFERRAM; i++) {
  443. if (this->bufferram[i].block == block &&
  444. this->bufferram[i].page == page)
  445. this->bufferram[i].valid = 0;
  446. }
  447. /* Update BufferRAM */
  448. i = ONENAND_CURRENT_BUFFERRAM(this);
  449. this->bufferram[i].block = block;
  450. this->bufferram[i].page = page;
  451. this->bufferram[i].valid = valid;
  452. return 0;
  453. }
  454. /**
  455. * onenand_get_device - [GENERIC] Get chip for selected access
  456. * @param mtd MTD device structure
  457. * @param new_state the state which is requested
  458. *
  459. * Get the device and lock it for exclusive access
  460. */
  461. static int onenand_get_device(struct mtd_info *mtd, int new_state)
  462. {
  463. struct onenand_chip *this = mtd->priv;
  464. DECLARE_WAITQUEUE(wait, current);
  465. /*
  466. * Grab the lock and see if the device is available
  467. */
  468. while (1) {
  469. spin_lock(&this->chip_lock);
  470. if (this->state == FL_READY) {
  471. this->state = new_state;
  472. spin_unlock(&this->chip_lock);
  473. break;
  474. }
  475. if (new_state == FL_PM_SUSPENDED) {
  476. spin_unlock(&this->chip_lock);
  477. return (this->state == FL_PM_SUSPENDED) ? 0 : -EAGAIN;
  478. }
  479. set_current_state(TASK_UNINTERRUPTIBLE);
  480. add_wait_queue(&this->wq, &wait);
  481. spin_unlock(&this->chip_lock);
  482. schedule();
  483. remove_wait_queue(&this->wq, &wait);
  484. }
  485. return 0;
  486. }
  487. /**
  488. * onenand_release_device - [GENERIC] release chip
  489. * @param mtd MTD device structure
  490. *
  491. * Deselect, release chip lock and wake up anyone waiting on the device
  492. */
  493. static void onenand_release_device(struct mtd_info *mtd)
  494. {
  495. struct onenand_chip *this = mtd->priv;
  496. /* Release the chip */
  497. spin_lock(&this->chip_lock);
  498. this->state = FL_READY;
  499. wake_up(&this->wq);
  500. spin_unlock(&this->chip_lock);
  501. }
  502. /**
  503. * onenand_read - [MTD Interface] Read data from flash
  504. * @param mtd MTD device structure
  505. * @param from offset to read from
  506. * @param len number of bytes to read
  507. * @param retlen pointer to variable to store the number of read bytes
  508. * @param buf the databuffer to put data
  509. *
  510. * Read with ecc
  511. */
  512. static int onenand_read(struct mtd_info *mtd, loff_t from, size_t len,
  513. size_t *retlen, u_char *buf)
  514. {
  515. struct onenand_chip *this = mtd->priv;
  516. int read = 0, column;
  517. int thislen;
  518. int ret = 0;
  519. DEBUG(MTD_DEBUG_LEVEL3, "onenand_read: from = 0x%08x, len = %i\n", (unsigned int) from, (int) len);
  520. /* Do not allow reads past end of device */
  521. if ((from + len) > mtd->size) {
  522. DEBUG(MTD_DEBUG_LEVEL0, "onenand_read: Attempt read beyond end of device\n");
  523. *retlen = 0;
  524. return -EINVAL;
  525. }
  526. /* Grab the lock and see if the device is available */
  527. onenand_get_device(mtd, FL_READING);
  528. /* TODO handling oob */
  529. while (read < len) {
  530. thislen = min_t(int, mtd->writesize, len - read);
  531. column = from & (mtd->writesize - 1);
  532. if (column + thislen > mtd->writesize)
  533. thislen = mtd->writesize - column;
  534. if (!onenand_check_bufferram(mtd, from)) {
  535. this->command(mtd, ONENAND_CMD_READ, from, mtd->writesize);
  536. ret = this->wait(mtd, FL_READING);
  537. /* First copy data and check return value for ECC handling */
  538. onenand_update_bufferram(mtd, from, 1);
  539. }
  540. this->read_bufferram(mtd, ONENAND_DATARAM, buf, column, thislen);
  541. read += thislen;
  542. if (read == len)
  543. break;
  544. if (ret) {
  545. DEBUG(MTD_DEBUG_LEVEL0, "onenand_read: read failed = %d\n", ret);
  546. goto out;
  547. }
  548. from += thislen;
  549. buf += thislen;
  550. }
  551. out:
  552. /* Deselect and wake up anyone waiting on the device */
  553. onenand_release_device(mtd);
  554. /*
  555. * Return success, if no ECC failures, else -EBADMSG
  556. * fs driver will take care of that, because
  557. * retlen == desired len and result == -EBADMSG
  558. */
  559. *retlen = read;
  560. return ret;
  561. }
  562. /**
  563. * onenand_do_read_oob - [MTD Interface] OneNAND read out-of-band
  564. * @param mtd MTD device structure
  565. * @param from offset to read from
  566. * @param len number of bytes to read
  567. * @param retlen pointer to variable to store the number of read bytes
  568. * @param buf the databuffer to put data
  569. *
  570. * OneNAND read out-of-band data from the spare area
  571. */
  572. int onenand_do_read_oob(struct mtd_info *mtd, loff_t from, size_t len,
  573. size_t *retlen, u_char *buf)
  574. {
  575. struct onenand_chip *this = mtd->priv;
  576. int read = 0, thislen, column;
  577. int ret = 0;
  578. DEBUG(MTD_DEBUG_LEVEL3, "onenand_read_oob: from = 0x%08x, len = %i\n", (unsigned int) from, (int) len);
  579. /* Initialize return length value */
  580. *retlen = 0;
  581. /* Do not allow reads past end of device */
  582. if (unlikely((from + len) > mtd->size)) {
  583. DEBUG(MTD_DEBUG_LEVEL0, "onenand_read_oob: Attempt read beyond end of device\n");
  584. return -EINVAL;
  585. }
  586. /* Grab the lock and see if the device is available */
  587. onenand_get_device(mtd, FL_READING);
  588. column = from & (mtd->oobsize - 1);
  589. while (read < len) {
  590. thislen = mtd->oobsize - column;
  591. thislen = min_t(int, thislen, len);
  592. this->command(mtd, ONENAND_CMD_READOOB, from, mtd->oobsize);
  593. onenand_update_bufferram(mtd, from, 0);
  594. ret = this->wait(mtd, FL_READING);
  595. /* First copy data and check return value for ECC handling */
  596. this->read_bufferram(mtd, ONENAND_SPARERAM, buf, column, thislen);
  597. read += thislen;
  598. if (read == len)
  599. break;
  600. if (ret) {
  601. DEBUG(MTD_DEBUG_LEVEL0, "onenand_read_oob: read failed = %d\n", ret);
  602. goto out;
  603. }
  604. buf += thislen;
  605. /* Read more? */
  606. if (read < len) {
  607. /* Page size */
  608. from += mtd->writesize;
  609. column = 0;
  610. }
  611. }
  612. out:
  613. /* Deselect and wake up anyone waiting on the device */
  614. onenand_release_device(mtd);
  615. *retlen = read;
  616. return ret;
  617. }
  618. /**
  619. * onenand_read_oob - [MTD Interface] NAND write data and/or out-of-band
  620. * @mtd: MTD device structure
  621. * @from: offset to read from
  622. * @ops: oob operation description structure
  623. */
  624. static int onenand_read_oob(struct mtd_info *mtd, loff_t from,
  625. struct mtd_oob_ops *ops)
  626. {
  627. BUG_ON(ops->mode != MTD_OOB_PLACE);
  628. return onenand_do_read_oob(mtd, from + ops->ooboffs, ops->len,
  629. &ops->retlen, ops->oobbuf);
  630. }
  631. #ifdef CONFIG_MTD_ONENAND_VERIFY_WRITE
  632. /**
  633. * onenand_verify_oob - [GENERIC] verify the oob contents after a write
  634. * @param mtd MTD device structure
  635. * @param buf the databuffer to verify
  636. * @param to offset to read from
  637. * @param len number of bytes to read and compare
  638. *
  639. */
  640. static int onenand_verify_oob(struct mtd_info *mtd, const u_char *buf, loff_t to, int len)
  641. {
  642. struct onenand_chip *this = mtd->priv;
  643. char *readp = this->page_buf;
  644. int column = to & (mtd->oobsize - 1);
  645. int status, i;
  646. this->command(mtd, ONENAND_CMD_READOOB, to, mtd->oobsize);
  647. onenand_update_bufferram(mtd, to, 0);
  648. status = this->wait(mtd, FL_READING);
  649. if (status)
  650. return status;
  651. this->read_bufferram(mtd, ONENAND_SPARERAM, readp, column, len);
  652. for(i = 0; i < len; i++)
  653. if (buf[i] != 0xFF && buf[i] != readp[i])
  654. return -EBADMSG;
  655. return 0;
  656. }
  657. /**
  658. * onenand_verify_page - [GENERIC] verify the chip contents after a write
  659. * @param mtd MTD device structure
  660. * @param buf the databuffer to verify
  661. *
  662. * Check DataRAM area directly
  663. */
  664. static int onenand_verify_page(struct mtd_info *mtd, u_char *buf, loff_t addr)
  665. {
  666. struct onenand_chip *this = mtd->priv;
  667. void __iomem *dataram0, *dataram1;
  668. int ret = 0;
  669. this->command(mtd, ONENAND_CMD_READ, addr, mtd->writesize);
  670. ret = this->wait(mtd, FL_READING);
  671. if (ret)
  672. return ret;
  673. onenand_update_bufferram(mtd, addr, 1);
  674. /* Check, if the two dataram areas are same */
  675. dataram0 = this->base + ONENAND_DATARAM;
  676. dataram1 = dataram0 + mtd->writesize;
  677. if (memcmp(dataram0, dataram1, mtd->writesize))
  678. return -EBADMSG;
  679. return 0;
  680. }
  681. #else
  682. #define onenand_verify_page(...) (0)
  683. #define onenand_verify_oob(...) (0)
  684. #endif
  685. #define NOTALIGNED(x) ((x & (mtd->writesize - 1)) != 0)
  686. /**
  687. * onenand_write - [MTD Interface] write buffer to FLASH
  688. * @param mtd MTD device structure
  689. * @param to offset to write to
  690. * @param len number of bytes to write
  691. * @param retlen pointer to variable to store the number of written bytes
  692. * @param buf the data to write
  693. *
  694. * Write with ECC
  695. */
  696. static int onenand_write(struct mtd_info *mtd, loff_t to, size_t len,
  697. size_t *retlen, const u_char *buf)
  698. {
  699. struct onenand_chip *this = mtd->priv;
  700. int written = 0;
  701. int ret = 0;
  702. DEBUG(MTD_DEBUG_LEVEL3, "onenand_write: to = 0x%08x, len = %i\n", (unsigned int) to, (int) len);
  703. /* Initialize retlen, in case of early exit */
  704. *retlen = 0;
  705. /* Do not allow writes past end of device */
  706. if (unlikely((to + len) > mtd->size)) {
  707. DEBUG(MTD_DEBUG_LEVEL0, "onenand_write: Attempt write to past end of device\n");
  708. return -EINVAL;
  709. }
  710. /* Reject writes, which are not page aligned */
  711. if (unlikely(NOTALIGNED(to)) || unlikely(NOTALIGNED(len))) {
  712. DEBUG(MTD_DEBUG_LEVEL0, "onenand_write: Attempt to write not page aligned data\n");
  713. return -EINVAL;
  714. }
  715. /* Grab the lock and see if the device is available */
  716. onenand_get_device(mtd, FL_WRITING);
  717. /* Loop until all data write */
  718. while (written < len) {
  719. int thislen = min_t(int, mtd->writesize, len - written);
  720. this->command(mtd, ONENAND_CMD_BUFFERRAM, to, mtd->writesize);
  721. this->write_bufferram(mtd, ONENAND_DATARAM, buf, 0, thislen);
  722. this->write_bufferram(mtd, ONENAND_SPARERAM, ffchars, 0, mtd->oobsize);
  723. this->command(mtd, ONENAND_CMD_PROG, to, mtd->writesize);
  724. onenand_update_bufferram(mtd, to, 1);
  725. ret = this->wait(mtd, FL_WRITING);
  726. if (ret) {
  727. DEBUG(MTD_DEBUG_LEVEL0, "onenand_write: write filaed %d\n", ret);
  728. goto out;
  729. }
  730. written += thislen;
  731. /* Only check verify write turn on */
  732. ret = onenand_verify_page(mtd, (u_char *) buf, to);
  733. if (ret) {
  734. DEBUG(MTD_DEBUG_LEVEL0, "onenand_write: verify failed %d\n", ret);
  735. goto out;
  736. }
  737. if (written == len)
  738. break;
  739. to += thislen;
  740. buf += thislen;
  741. }
  742. out:
  743. /* Deselect and wake up anyone waiting on the device */
  744. onenand_release_device(mtd);
  745. *retlen = written;
  746. return ret;
  747. }
  748. /**
  749. * onenand_do_write_oob - [Internal] OneNAND write out-of-band
  750. * @param mtd MTD device structure
  751. * @param to offset to write to
  752. * @param len number of bytes to write
  753. * @param retlen pointer to variable to store the number of written bytes
  754. * @param buf the data to write
  755. *
  756. * OneNAND write out-of-band
  757. */
  758. static int onenand_do_write_oob(struct mtd_info *mtd, loff_t to, size_t len,
  759. size_t *retlen, const u_char *buf)
  760. {
  761. struct onenand_chip *this = mtd->priv;
  762. int column, ret = 0;
  763. int written = 0;
  764. DEBUG(MTD_DEBUG_LEVEL3, "onenand_write_oob: to = 0x%08x, len = %i\n", (unsigned int) to, (int) len);
  765. /* Initialize retlen, in case of early exit */
  766. *retlen = 0;
  767. /* Do not allow writes past end of device */
  768. if (unlikely((to + len) > mtd->size)) {
  769. DEBUG(MTD_DEBUG_LEVEL0, "onenand_write_oob: Attempt write to past end of device\n");
  770. return -EINVAL;
  771. }
  772. /* Grab the lock and see if the device is available */
  773. onenand_get_device(mtd, FL_WRITING);
  774. /* Loop until all data write */
  775. while (written < len) {
  776. int thislen = min_t(int, mtd->oobsize, len - written);
  777. column = to & (mtd->oobsize - 1);
  778. this->command(mtd, ONENAND_CMD_BUFFERRAM, to, mtd->oobsize);
  779. /* We send data to spare ram with oobsize
  780. * to prevent byte access */
  781. memset(this->page_buf, 0xff, mtd->oobsize);
  782. memcpy(this->page_buf + column, buf, thislen);
  783. this->write_bufferram(mtd, ONENAND_SPARERAM, this->page_buf, 0, mtd->oobsize);
  784. this->command(mtd, ONENAND_CMD_PROGOOB, to, mtd->oobsize);
  785. onenand_update_bufferram(mtd, to, 0);
  786. ret = this->wait(mtd, FL_WRITING);
  787. if (ret) {
  788. DEBUG(MTD_DEBUG_LEVEL0, "onenand_write_oob: write filaed %d\n", ret);
  789. goto out;
  790. }
  791. ret = onenand_verify_oob(mtd, buf, to, thislen);
  792. if (ret) {
  793. DEBUG(MTD_DEBUG_LEVEL0, "onenand_write_oob: verify failed %d\n", ret);
  794. goto out;
  795. }
  796. written += thislen;
  797. if (written == len)
  798. break;
  799. to += thislen;
  800. buf += thislen;
  801. }
  802. out:
  803. /* Deselect and wake up anyone waiting on the device */
  804. onenand_release_device(mtd);
  805. *retlen = written;
  806. return ret;
  807. }
  808. /**
  809. * onenand_write_oob - [MTD Interface] NAND write data and/or out-of-band
  810. * @mtd: MTD device structure
  811. * @from: offset to read from
  812. * @ops: oob operation description structure
  813. */
  814. static int onenand_write_oob(struct mtd_info *mtd, loff_t to,
  815. struct mtd_oob_ops *ops)
  816. {
  817. BUG_ON(ops->mode != MTD_OOB_PLACE);
  818. return onenand_do_write_oob(mtd, to + ops->ooboffs, ops->len,
  819. &ops->retlen, ops->oobbuf);
  820. }
  821. /**
  822. * onenand_block_checkbad - [GENERIC] Check if a block is marked bad
  823. * @param mtd MTD device structure
  824. * @param ofs offset from device start
  825. * @param getchip 0, if the chip is already selected
  826. * @param allowbbt 1, if its allowed to access the bbt area
  827. *
  828. * Check, if the block is bad. Either by reading the bad block table or
  829. * calling of the scan function.
  830. */
  831. static int onenand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int getchip, int allowbbt)
  832. {
  833. struct onenand_chip *this = mtd->priv;
  834. struct bbm_info *bbm = this->bbm;
  835. /* Return info from the table */
  836. return bbm->isbad_bbt(mtd, ofs, allowbbt);
  837. }
  838. /**
  839. * onenand_erase - [MTD Interface] erase block(s)
  840. * @param mtd MTD device structure
  841. * @param instr erase instruction
  842. *
  843. * Erase one ore more blocks
  844. */
  845. static int onenand_erase(struct mtd_info *mtd, struct erase_info *instr)
  846. {
  847. struct onenand_chip *this = mtd->priv;
  848. unsigned int block_size;
  849. loff_t addr;
  850. int len;
  851. int ret = 0;
  852. DEBUG(MTD_DEBUG_LEVEL3, "onenand_erase: start = 0x%08x, len = %i\n", (unsigned int) instr->addr, (unsigned int) instr->len);
  853. block_size = (1 << this->erase_shift);
  854. /* Start address must align on block boundary */
  855. if (unlikely(instr->addr & (block_size - 1))) {
  856. DEBUG(MTD_DEBUG_LEVEL0, "onenand_erase: Unaligned address\n");
  857. return -EINVAL;
  858. }
  859. /* Length must align on block boundary */
  860. if (unlikely(instr->len & (block_size - 1))) {
  861. DEBUG(MTD_DEBUG_LEVEL0, "onenand_erase: Length not block aligned\n");
  862. return -EINVAL;
  863. }
  864. /* Do not allow erase past end of device */
  865. if (unlikely((instr->len + instr->addr) > mtd->size)) {
  866. DEBUG(MTD_DEBUG_LEVEL0, "onenand_erase: Erase past end of device\n");
  867. return -EINVAL;
  868. }
  869. instr->fail_addr = 0xffffffff;
  870. /* Grab the lock and see if the device is available */
  871. onenand_get_device(mtd, FL_ERASING);
  872. /* Loop throught the pages */
  873. len = instr->len;
  874. addr = instr->addr;
  875. instr->state = MTD_ERASING;
  876. while (len) {
  877. /* Check if we have a bad block, we do not erase bad blocks */
  878. if (onenand_block_checkbad(mtd, addr, 0, 0)) {
  879. printk (KERN_WARNING "onenand_erase: attempt to erase a bad block at addr 0x%08x\n", (unsigned int) addr);
  880. instr->state = MTD_ERASE_FAILED;
  881. goto erase_exit;
  882. }
  883. this->command(mtd, ONENAND_CMD_ERASE, addr, block_size);
  884. ret = this->wait(mtd, FL_ERASING);
  885. /* Check, if it is write protected */
  886. if (ret) {
  887. if (ret == -EPERM)
  888. DEBUG(MTD_DEBUG_LEVEL0, "onenand_erase: Device is write protected!!!\n");
  889. else
  890. DEBUG(MTD_DEBUG_LEVEL0, "onenand_erase: Failed erase, block %d\n", (unsigned) (addr >> this->erase_shift));
  891. instr->state = MTD_ERASE_FAILED;
  892. instr->fail_addr = addr;
  893. goto erase_exit;
  894. }
  895. len -= block_size;
  896. addr += block_size;
  897. }
  898. instr->state = MTD_ERASE_DONE;
  899. erase_exit:
  900. ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
  901. /* Do call back function */
  902. if (!ret)
  903. mtd_erase_callback(instr);
  904. /* Deselect and wake up anyone waiting on the device */
  905. onenand_release_device(mtd);
  906. return ret;
  907. }
  908. /**
  909. * onenand_sync - [MTD Interface] sync
  910. * @param mtd MTD device structure
  911. *
  912. * Sync is actually a wait for chip ready function
  913. */
  914. static void onenand_sync(struct mtd_info *mtd)
  915. {
  916. DEBUG(MTD_DEBUG_LEVEL3, "onenand_sync: called\n");
  917. /* Grab the lock and see if the device is available */
  918. onenand_get_device(mtd, FL_SYNCING);
  919. /* Release it and go back */
  920. onenand_release_device(mtd);
  921. }
  922. /**
  923. * onenand_block_isbad - [MTD Interface] Check whether the block at the given offset is bad
  924. * @param mtd MTD device structure
  925. * @param ofs offset relative to mtd start
  926. *
  927. * Check whether the block is bad
  928. */
  929. static int onenand_block_isbad(struct mtd_info *mtd, loff_t ofs)
  930. {
  931. /* Check for invalid offset */
  932. if (ofs > mtd->size)
  933. return -EINVAL;
  934. return onenand_block_checkbad(mtd, ofs, 1, 0);
  935. }
  936. /**
  937. * onenand_default_block_markbad - [DEFAULT] mark a block bad
  938. * @param mtd MTD device structure
  939. * @param ofs offset from device start
  940. *
  941. * This is the default implementation, which can be overridden by
  942. * a hardware specific driver.
  943. */
  944. static int onenand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
  945. {
  946. struct onenand_chip *this = mtd->priv;
  947. struct bbm_info *bbm = this->bbm;
  948. u_char buf[2] = {0, 0};
  949. size_t retlen;
  950. int block;
  951. /* Get block number */
  952. block = ((int) ofs) >> bbm->bbt_erase_shift;
  953. if (bbm->bbt)
  954. bbm->bbt[block >> 2] |= 0x01 << ((block & 0x03) << 1);
  955. /* We write two bytes, so we dont have to mess with 16 bit access */
  956. ofs += mtd->oobsize + (bbm->badblockpos & ~0x01);
  957. return onenand_do_write_oob(mtd, ofs , 2, &retlen, buf);
  958. }
  959. /**
  960. * onenand_block_markbad - [MTD Interface] Mark the block at the given offset as bad
  961. * @param mtd MTD device structure
  962. * @param ofs offset relative to mtd start
  963. *
  964. * Mark the block as bad
  965. */
  966. static int onenand_block_markbad(struct mtd_info *mtd, loff_t ofs)
  967. {
  968. struct onenand_chip *this = mtd->priv;
  969. int ret;
  970. ret = onenand_block_isbad(mtd, ofs);
  971. if (ret) {
  972. /* If it was bad already, return success and do nothing */
  973. if (ret > 0)
  974. return 0;
  975. return ret;
  976. }
  977. return this->block_markbad(mtd, ofs);
  978. }
  979. /**
  980. * onenand_unlock - [MTD Interface] Unlock block(s)
  981. * @param mtd MTD device structure
  982. * @param ofs offset relative to mtd start
  983. * @param len number of bytes to unlock
  984. *
  985. * Unlock one or more blocks
  986. */
  987. static int onenand_unlock(struct mtd_info *mtd, loff_t ofs, size_t len)
  988. {
  989. struct onenand_chip *this = mtd->priv;
  990. int start, end, block, value, status;
  991. start = ofs >> this->erase_shift;
  992. end = len >> this->erase_shift;
  993. /* Continuous lock scheme */
  994. if (this->options & ONENAND_HAS_CONT_LOCK) {
  995. /* Set start block address */
  996. this->write_word(start, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
  997. /* Set end block address */
  998. this->write_word(start + end - 1, this->base + ONENAND_REG_END_BLOCK_ADDRESS);
  999. /* Write unlock command */
  1000. this->command(mtd, ONENAND_CMD_UNLOCK, 0, 0);
  1001. /* There's no return value */
  1002. this->wait(mtd, FL_UNLOCKING);
  1003. /* Sanity check */
  1004. while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
  1005. & ONENAND_CTRL_ONGO)
  1006. continue;
  1007. /* Check lock status */
  1008. status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
  1009. if (!(status & ONENAND_WP_US))
  1010. printk(KERN_ERR "wp status = 0x%x\n", status);
  1011. return 0;
  1012. }
  1013. /* Block lock scheme */
  1014. for (block = start; block < start + end; block++) {
  1015. /* Set block address */
  1016. value = onenand_block_address(this, block);
  1017. this->write_word(value, this->base + ONENAND_REG_START_ADDRESS1);
  1018. /* Select DataRAM for DDP */
  1019. value = onenand_bufferram_address(this, block);
  1020. this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
  1021. /* Set start block address */
  1022. this->write_word(block, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
  1023. /* Write unlock command */
  1024. this->command(mtd, ONENAND_CMD_UNLOCK, 0, 0);
  1025. /* There's no return value */
  1026. this->wait(mtd, FL_UNLOCKING);
  1027. /* Sanity check */
  1028. while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
  1029. & ONENAND_CTRL_ONGO)
  1030. continue;
  1031. /* Check lock status */
  1032. status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
  1033. if (!(status & ONENAND_WP_US))
  1034. printk(KERN_ERR "block = %d, wp status = 0x%x\n", block, status);
  1035. }
  1036. return 0;
  1037. }
  1038. /**
  1039. * onenand_check_lock_status - [OneNAND Interface] Check lock status
  1040. * @param this onenand chip data structure
  1041. *
  1042. * Check lock status
  1043. */
  1044. static void onenand_check_lock_status(struct onenand_chip *this)
  1045. {
  1046. unsigned int value, block, status;
  1047. unsigned int end;
  1048. end = this->chipsize >> this->erase_shift;
  1049. for (block = 0; block < end; block++) {
  1050. /* Set block address */
  1051. value = onenand_block_address(this, block);
  1052. this->write_word(value, this->base + ONENAND_REG_START_ADDRESS1);
  1053. /* Select DataRAM for DDP */
  1054. value = onenand_bufferram_address(this, block);
  1055. this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
  1056. /* Set start block address */
  1057. this->write_word(block, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
  1058. /* Check lock status */
  1059. status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
  1060. if (!(status & ONENAND_WP_US))
  1061. printk(KERN_ERR "block = %d, wp status = 0x%x\n", block, status);
  1062. }
  1063. }
  1064. /**
  1065. * onenand_unlock_all - [OneNAND Interface] unlock all blocks
  1066. * @param mtd MTD device structure
  1067. *
  1068. * Unlock all blocks
  1069. */
  1070. static int onenand_unlock_all(struct mtd_info *mtd)
  1071. {
  1072. struct onenand_chip *this = mtd->priv;
  1073. if (this->options & ONENAND_HAS_UNLOCK_ALL) {
  1074. /* Write unlock command */
  1075. this->command(mtd, ONENAND_CMD_UNLOCK_ALL, 0, 0);
  1076. /* There's no return value */
  1077. this->wait(mtd, FL_UNLOCKING);
  1078. /* Sanity check */
  1079. while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
  1080. & ONENAND_CTRL_ONGO)
  1081. continue;
  1082. /* Workaround for all block unlock in DDP */
  1083. if (this->device_id & ONENAND_DEVICE_IS_DDP) {
  1084. loff_t ofs;
  1085. size_t len;
  1086. /* 1st block on another chip */
  1087. ofs = this->chipsize >> 1;
  1088. len = 1 << this->erase_shift;
  1089. onenand_unlock(mtd, ofs, len);
  1090. }
  1091. onenand_check_lock_status(this);
  1092. return 0;
  1093. }
  1094. mtd->unlock(mtd, 0x0, this->chipsize);
  1095. return 0;
  1096. }
  1097. #ifdef CONFIG_MTD_ONENAND_OTP
  1098. /* Interal OTP operation */
  1099. typedef int (*otp_op_t)(struct mtd_info *mtd, loff_t form, size_t len,
  1100. size_t *retlen, u_char *buf);
  1101. /**
  1102. * do_otp_read - [DEFAULT] Read OTP block area
  1103. * @param mtd MTD device structure
  1104. * @param from The offset to read
  1105. * @param len number of bytes to read
  1106. * @param retlen pointer to variable to store the number of readbytes
  1107. * @param buf the databuffer to put/get data
  1108. *
  1109. * Read OTP block area.
  1110. */
  1111. static int do_otp_read(struct mtd_info *mtd, loff_t from, size_t len,
  1112. size_t *retlen, u_char *buf)
  1113. {
  1114. struct onenand_chip *this = mtd->priv;
  1115. int ret;
  1116. /* Enter OTP access mode */
  1117. this->command(mtd, ONENAND_CMD_OTP_ACCESS, 0, 0);
  1118. this->wait(mtd, FL_OTPING);
  1119. ret = mtd->read(mtd, from, len, retlen, buf);
  1120. /* Exit OTP access mode */
  1121. this->command(mtd, ONENAND_CMD_RESET, 0, 0);
  1122. this->wait(mtd, FL_RESETING);
  1123. return ret;
  1124. }
  1125. /**
  1126. * do_otp_write - [DEFAULT] Write OTP block area
  1127. * @param mtd MTD device structure
  1128. * @param from The offset to write
  1129. * @param len number of bytes to write
  1130. * @param retlen pointer to variable to store the number of write bytes
  1131. * @param buf the databuffer to put/get data
  1132. *
  1133. * Write OTP block area.
  1134. */
  1135. static int do_otp_write(struct mtd_info *mtd, loff_t from, size_t len,
  1136. size_t *retlen, u_char *buf)
  1137. {
  1138. struct onenand_chip *this = mtd->priv;
  1139. unsigned char *pbuf = buf;
  1140. int ret;
  1141. /* Force buffer page aligned */
  1142. if (len < mtd->writesize) {
  1143. memcpy(this->page_buf, buf, len);
  1144. memset(this->page_buf + len, 0xff, mtd->writesize - len);
  1145. pbuf = this->page_buf;
  1146. len = mtd->writesize;
  1147. }
  1148. /* Enter OTP access mode */
  1149. this->command(mtd, ONENAND_CMD_OTP_ACCESS, 0, 0);
  1150. this->wait(mtd, FL_OTPING);
  1151. ret = mtd->write(mtd, from, len, retlen, pbuf);
  1152. /* Exit OTP access mode */
  1153. this->command(mtd, ONENAND_CMD_RESET, 0, 0);
  1154. this->wait(mtd, FL_RESETING);
  1155. return ret;
  1156. }
  1157. /**
  1158. * do_otp_lock - [DEFAULT] Lock OTP block area
  1159. * @param mtd MTD device structure
  1160. * @param from The offset to lock
  1161. * @param len number of bytes to lock
  1162. * @param retlen pointer to variable to store the number of lock bytes
  1163. * @param buf the databuffer to put/get data
  1164. *
  1165. * Lock OTP block area.
  1166. */
  1167. static int do_otp_lock(struct mtd_info *mtd, loff_t from, size_t len,
  1168. size_t *retlen, u_char *buf)
  1169. {
  1170. struct onenand_chip *this = mtd->priv;
  1171. int ret;
  1172. /* Enter OTP access mode */
  1173. this->command(mtd, ONENAND_CMD_OTP_ACCESS, 0, 0);
  1174. this->wait(mtd, FL_OTPING);
  1175. ret = onenand_do_write_oob(mtd, from, len, retlen, buf);
  1176. /* Exit OTP access mode */
  1177. this->command(mtd, ONENAND_CMD_RESET, 0, 0);
  1178. this->wait(mtd, FL_RESETING);
  1179. return ret;
  1180. }
  1181. /**
  1182. * onenand_otp_walk - [DEFAULT] Handle OTP operation
  1183. * @param mtd MTD device structure
  1184. * @param from The offset to read/write
  1185. * @param len number of bytes to read/write
  1186. * @param retlen pointer to variable to store the number of read bytes
  1187. * @param buf the databuffer to put/get data
  1188. * @param action do given action
  1189. * @param mode specify user and factory
  1190. *
  1191. * Handle OTP operation.
  1192. */
  1193. static int onenand_otp_walk(struct mtd_info *mtd, loff_t from, size_t len,
  1194. size_t *retlen, u_char *buf,
  1195. otp_op_t action, int mode)
  1196. {
  1197. struct onenand_chip *this = mtd->priv;
  1198. int otp_pages;
  1199. int density;
  1200. int ret = 0;
  1201. *retlen = 0;
  1202. density = this->device_id >> ONENAND_DEVICE_DENSITY_SHIFT;
  1203. if (density < ONENAND_DEVICE_DENSITY_512Mb)
  1204. otp_pages = 20;
  1205. else
  1206. otp_pages = 10;
  1207. if (mode == MTD_OTP_FACTORY) {
  1208. from += mtd->writesize * otp_pages;
  1209. otp_pages = 64 - otp_pages;
  1210. }
  1211. /* Check User/Factory boundary */
  1212. if (((mtd->writesize * otp_pages) - (from + len)) < 0)
  1213. return 0;
  1214. while (len > 0 && otp_pages > 0) {
  1215. if (!action) { /* OTP Info functions */
  1216. struct otp_info *otpinfo;
  1217. len -= sizeof(struct otp_info);
  1218. if (len <= 0)
  1219. return -ENOSPC;
  1220. otpinfo = (struct otp_info *) buf;
  1221. otpinfo->start = from;
  1222. otpinfo->length = mtd->writesize;
  1223. otpinfo->locked = 0;
  1224. from += mtd->writesize;
  1225. buf += sizeof(struct otp_info);
  1226. *retlen += sizeof(struct otp_info);
  1227. } else {
  1228. size_t tmp_retlen;
  1229. int size = len;
  1230. ret = action(mtd, from, len, &tmp_retlen, buf);
  1231. buf += size;
  1232. len -= size;
  1233. *retlen += size;
  1234. if (ret < 0)
  1235. return ret;
  1236. }
  1237. otp_pages--;
  1238. }
  1239. return 0;
  1240. }
  1241. /**
  1242. * onenand_get_fact_prot_info - [MTD Interface] Read factory OTP info
  1243. * @param mtd MTD device structure
  1244. * @param buf the databuffer to put/get data
  1245. * @param len number of bytes to read
  1246. *
  1247. * Read factory OTP info.
  1248. */
  1249. static int onenand_get_fact_prot_info(struct mtd_info *mtd,
  1250. struct otp_info *buf, size_t len)
  1251. {
  1252. size_t retlen;
  1253. int ret;
  1254. ret = onenand_otp_walk(mtd, 0, len, &retlen, (u_char *) buf, NULL, MTD_OTP_FACTORY);
  1255. return ret ? : retlen;
  1256. }
  1257. /**
  1258. * onenand_read_fact_prot_reg - [MTD Interface] Read factory OTP area
  1259. * @param mtd MTD device structure
  1260. * @param from The offset to read
  1261. * @param len number of bytes to read
  1262. * @param retlen pointer to variable to store the number of read bytes
  1263. * @param buf the databuffer to put/get data
  1264. *
  1265. * Read factory OTP area.
  1266. */
  1267. static int onenand_read_fact_prot_reg(struct mtd_info *mtd, loff_t from,
  1268. size_t len, size_t *retlen, u_char *buf)
  1269. {
  1270. return onenand_otp_walk(mtd, from, len, retlen, buf, do_otp_read, MTD_OTP_FACTORY);
  1271. }
  1272. /**
  1273. * onenand_get_user_prot_info - [MTD Interface] Read user OTP info
  1274. * @param mtd MTD device structure
  1275. * @param buf the databuffer to put/get data
  1276. * @param len number of bytes to read
  1277. *
  1278. * Read user OTP info.
  1279. */
  1280. static int onenand_get_user_prot_info(struct mtd_info *mtd,
  1281. struct otp_info *buf, size_t len)
  1282. {
  1283. size_t retlen;
  1284. int ret;
  1285. ret = onenand_otp_walk(mtd, 0, len, &retlen, (u_char *) buf, NULL, MTD_OTP_USER);
  1286. return ret ? : retlen;
  1287. }
  1288. /**
  1289. * onenand_read_user_prot_reg - [MTD Interface] Read user OTP area
  1290. * @param mtd MTD device structure
  1291. * @param from The offset to read
  1292. * @param len number of bytes to read
  1293. * @param retlen pointer to variable to store the number of read bytes
  1294. * @param buf the databuffer to put/get data
  1295. *
  1296. * Read user OTP area.
  1297. */
  1298. static int onenand_read_user_prot_reg(struct mtd_info *mtd, loff_t from,
  1299. size_t len, size_t *retlen, u_char *buf)
  1300. {
  1301. return onenand_otp_walk(mtd, from, len, retlen, buf, do_otp_read, MTD_OTP_USER);
  1302. }
  1303. /**
  1304. * onenand_write_user_prot_reg - [MTD Interface] Write user OTP area
  1305. * @param mtd MTD device structure
  1306. * @param from The offset to write
  1307. * @param len number of bytes to write
  1308. * @param retlen pointer to variable to store the number of write bytes
  1309. * @param buf the databuffer to put/get data
  1310. *
  1311. * Write user OTP area.
  1312. */
  1313. static int onenand_write_user_prot_reg(struct mtd_info *mtd, loff_t from,
  1314. size_t len, size_t *retlen, u_char *buf)
  1315. {
  1316. return onenand_otp_walk(mtd, from, len, retlen, buf, do_otp_write, MTD_OTP_USER);
  1317. }
  1318. /**
  1319. * onenand_lock_user_prot_reg - [MTD Interface] Lock user OTP area
  1320. * @param mtd MTD device structure
  1321. * @param from The offset to lock
  1322. * @param len number of bytes to unlock
  1323. *
  1324. * Write lock mark on spare area in page 0 in OTP block
  1325. */
  1326. static int onenand_lock_user_prot_reg(struct mtd_info *mtd, loff_t from,
  1327. size_t len)
  1328. {
  1329. unsigned char oob_buf[64];
  1330. size_t retlen;
  1331. int ret;
  1332. memset(oob_buf, 0xff, mtd->oobsize);
  1333. /*
  1334. * Note: OTP lock operation
  1335. * OTP block : 0xXXFC
  1336. * 1st block : 0xXXF3 (If chip support)
  1337. * Both : 0xXXF0 (If chip support)
  1338. */
  1339. oob_buf[ONENAND_OTP_LOCK_OFFSET] = 0xFC;
  1340. /*
  1341. * Write lock mark to 8th word of sector0 of page0 of the spare0.
  1342. * We write 16 bytes spare area instead of 2 bytes.
  1343. */
  1344. from = 0;
  1345. len = 16;
  1346. ret = onenand_otp_walk(mtd, from, len, &retlen, oob_buf, do_otp_lock, MTD_OTP_USER);
  1347. return ret ? : retlen;
  1348. }
  1349. #endif /* CONFIG_MTD_ONENAND_OTP */
  1350. /**
  1351. * onenand_lock_scheme - Check and set OneNAND lock scheme
  1352. * @param mtd MTD data structure
  1353. *
  1354. * Check and set OneNAND lock scheme
  1355. */
  1356. static void onenand_lock_scheme(struct mtd_info *mtd)
  1357. {
  1358. struct onenand_chip *this = mtd->priv;
  1359. unsigned int density, process;
  1360. /* Lock scheme depends on density and process */
  1361. density = this->device_id >> ONENAND_DEVICE_DENSITY_SHIFT;
  1362. process = this->version_id >> ONENAND_VERSION_PROCESS_SHIFT;
  1363. /* Lock scheme */
  1364. if (density >= ONENAND_DEVICE_DENSITY_1Gb) {
  1365. /* A-Die has all block unlock */
  1366. if (process) {
  1367. printk(KERN_DEBUG "Chip support all block unlock\n");
  1368. this->options |= ONENAND_HAS_UNLOCK_ALL;
  1369. }
  1370. } else {
  1371. /* Some OneNAND has continues lock scheme */
  1372. if (!process) {
  1373. printk(KERN_DEBUG "Lock scheme is Continues Lock\n");
  1374. this->options |= ONENAND_HAS_CONT_LOCK;
  1375. }
  1376. }
  1377. }
  1378. /**
  1379. * onenand_print_device_info - Print device ID
  1380. * @param device device ID
  1381. *
  1382. * Print device ID
  1383. */
  1384. static void onenand_print_device_info(int device, int version)
  1385. {
  1386. int vcc, demuxed, ddp, density;
  1387. vcc = device & ONENAND_DEVICE_VCC_MASK;
  1388. demuxed = device & ONENAND_DEVICE_IS_DEMUX;
  1389. ddp = device & ONENAND_DEVICE_IS_DDP;
  1390. density = device >> ONENAND_DEVICE_DENSITY_SHIFT;
  1391. printk(KERN_INFO "%sOneNAND%s %dMB %sV 16-bit (0x%02x)\n",
  1392. demuxed ? "" : "Muxed ",
  1393. ddp ? "(DDP)" : "",
  1394. (16 << density),
  1395. vcc ? "2.65/3.3" : "1.8",
  1396. device);
  1397. printk(KERN_DEBUG "OneNAND version = 0x%04x\n", version);
  1398. }
  1399. static const struct onenand_manufacturers onenand_manuf_ids[] = {
  1400. {ONENAND_MFR_SAMSUNG, "Samsung"},
  1401. };
  1402. /**
  1403. * onenand_check_maf - Check manufacturer ID
  1404. * @param manuf manufacturer ID
  1405. *
  1406. * Check manufacturer ID
  1407. */
  1408. static int onenand_check_maf(int manuf)
  1409. {
  1410. int size = ARRAY_SIZE(onenand_manuf_ids);
  1411. char *name;
  1412. int i;
  1413. for (i = 0; i < size; i++)
  1414. if (manuf == onenand_manuf_ids[i].id)
  1415. break;
  1416. if (i < size)
  1417. name = onenand_manuf_ids[i].name;
  1418. else
  1419. name = "Unknown";
  1420. printk(KERN_DEBUG "OneNAND Manufacturer: %s (0x%0x)\n", name, manuf);
  1421. return (i == size);
  1422. }
  1423. /**
  1424. * onenand_probe - [OneNAND Interface] Probe the OneNAND device
  1425. * @param mtd MTD device structure
  1426. *
  1427. * OneNAND detection method:
  1428. * Compare the the values from command with ones from register
  1429. */
  1430. static int onenand_probe(struct mtd_info *mtd)
  1431. {
  1432. struct onenand_chip *this = mtd->priv;
  1433. int bram_maf_id, bram_dev_id, maf_id, dev_id, ver_id;
  1434. int density;
  1435. int syscfg;
  1436. /* Save system configuration 1 */
  1437. syscfg = this->read_word(this->base + ONENAND_REG_SYS_CFG1);
  1438. /* Clear Sync. Burst Read mode to read BootRAM */
  1439. this->write_word((syscfg & ~ONENAND_SYS_CFG1_SYNC_READ), this->base + ONENAND_REG_SYS_CFG1);
  1440. /* Send the command for reading device ID from BootRAM */
  1441. this->write_word(ONENAND_CMD_READID, this->base + ONENAND_BOOTRAM);
  1442. /* Read manufacturer and device IDs from BootRAM */
  1443. bram_maf_id = this->read_word(this->base + ONENAND_BOOTRAM + 0x0);
  1444. bram_dev_id = this->read_word(this->base + ONENAND_BOOTRAM + 0x2);
  1445. /* Reset OneNAND to read default register values */
  1446. this->write_word(ONENAND_CMD_RESET, this->base + ONENAND_BOOTRAM);
  1447. /* Wait reset */
  1448. this->wait(mtd, FL_RESETING);
  1449. /* Restore system configuration 1 */
  1450. this->write_word(syscfg, this->base + ONENAND_REG_SYS_CFG1);
  1451. /* Check manufacturer ID */
  1452. if (onenand_check_maf(bram_maf_id))
  1453. return -ENXIO;
  1454. /* Read manufacturer and device IDs from Register */
  1455. maf_id = this->read_word(this->base + ONENAND_REG_MANUFACTURER_ID);
  1456. dev_id = this->read_word(this->base + ONENAND_REG_DEVICE_ID);
  1457. ver_id= this->read_word(this->base + ONENAND_REG_VERSION_ID);
  1458. /* Check OneNAND device */
  1459. if (maf_id != bram_maf_id || dev_id != bram_dev_id)
  1460. return -ENXIO;
  1461. /* Flash device information */
  1462. onenand_print_device_info(dev_id, ver_id);
  1463. this->device_id = dev_id;
  1464. this->version_id = ver_id;
  1465. density = dev_id >> ONENAND_DEVICE_DENSITY_SHIFT;
  1466. this->chipsize = (16 << density) << 20;
  1467. /* Set density mask. it is used for DDP */
  1468. this->density_mask = (1 << (density + 6));
  1469. /* OneNAND page size & block size */
  1470. /* The data buffer size is equal to page size */
  1471. mtd->writesize = this->read_word(this->base + ONENAND_REG_DATA_BUFFER_SIZE);
  1472. mtd->oobsize = mtd->writesize >> 5;
  1473. /* Pagers per block is always 64 in OneNAND */
  1474. mtd->erasesize = mtd->writesize << 6;
  1475. this->erase_shift = ffs(mtd->erasesize) - 1;
  1476. this->page_shift = ffs(mtd->writesize) - 1;
  1477. this->ppb_shift = (this->erase_shift - this->page_shift);
  1478. this->page_mask = (mtd->erasesize / mtd->writesize) - 1;
  1479. /* REVIST: Multichip handling */
  1480. mtd->size = this->chipsize;
  1481. /* Check OneNAND lock scheme */
  1482. onenand_lock_scheme(mtd);
  1483. return 0;
  1484. }
  1485. /**
  1486. * onenand_suspend - [MTD Interface] Suspend the OneNAND flash
  1487. * @param mtd MTD device structure
  1488. */
  1489. static int onenand_suspend(struct mtd_info *mtd)
  1490. {
  1491. return onenand_get_device(mtd, FL_PM_SUSPENDED);
  1492. }
  1493. /**
  1494. * onenand_resume - [MTD Interface] Resume the OneNAND flash
  1495. * @param mtd MTD device structure
  1496. */
  1497. static void onenand_resume(struct mtd_info *mtd)
  1498. {
  1499. struct onenand_chip *this = mtd->priv;
  1500. if (this->state == FL_PM_SUSPENDED)
  1501. onenand_release_device(mtd);
  1502. else
  1503. printk(KERN_ERR "resume() called for the chip which is not"
  1504. "in suspended state\n");
  1505. }
  1506. /**
  1507. * onenand_scan - [OneNAND Interface] Scan for the OneNAND device
  1508. * @param mtd MTD device structure
  1509. * @param maxchips Number of chips to scan for
  1510. *
  1511. * This fills out all the not initialized function pointers
  1512. * with the defaults.
  1513. * The flash ID is read and the mtd/chip structures are
  1514. * filled with the appropriate values.
  1515. */
  1516. int onenand_scan(struct mtd_info *mtd, int maxchips)
  1517. {
  1518. struct onenand_chip *this = mtd->priv;
  1519. if (!this->read_word)
  1520. this->read_word = onenand_readw;
  1521. if (!this->write_word)
  1522. this->write_word = onenand_writew;
  1523. if (!this->command)
  1524. this->command = onenand_command;
  1525. if (!this->wait)
  1526. this->wait = onenand_wait;
  1527. if (!this->read_bufferram)
  1528. this->read_bufferram = onenand_read_bufferram;
  1529. if (!this->write_bufferram)
  1530. this->write_bufferram = onenand_write_bufferram;
  1531. if (!this->block_markbad)
  1532. this->block_markbad = onenand_default_block_markbad;
  1533. if (!this->scan_bbt)
  1534. this->scan_bbt = onenand_default_bbt;
  1535. if (onenand_probe(mtd))
  1536. return -ENXIO;
  1537. /* Set Sync. Burst Read after probing */
  1538. if (this->mmcontrol) {
  1539. printk(KERN_INFO "OneNAND Sync. Burst Read support\n");
  1540. this->read_bufferram = onenand_sync_read_bufferram;
  1541. }
  1542. /* Allocate buffers, if necessary */
  1543. if (!this->page_buf) {
  1544. size_t len;
  1545. len = mtd->writesize + mtd->oobsize;
  1546. this->page_buf = kmalloc(len, GFP_KERNEL);
  1547. if (!this->page_buf) {
  1548. printk(KERN_ERR "onenand_scan(): Can't allocate page_buf\n");
  1549. return -ENOMEM;
  1550. }
  1551. this->options |= ONENAND_PAGEBUF_ALLOC;
  1552. }
  1553. this->state = FL_READY;
  1554. init_waitqueue_head(&this->wq);
  1555. spin_lock_init(&this->chip_lock);
  1556. switch (mtd->oobsize) {
  1557. case 64:
  1558. this->ecclayout = &onenand_oob_64;
  1559. break;
  1560. case 32:
  1561. this->ecclayout = &onenand_oob_32;
  1562. break;
  1563. default:
  1564. printk(KERN_WARNING "No OOB scheme defined for oobsize %d\n",
  1565. mtd->oobsize);
  1566. /* To prevent kernel oops */
  1567. this->ecclayout = &onenand_oob_32;
  1568. break;
  1569. }
  1570. mtd->ecclayout = this->ecclayout;
  1571. /* Fill in remaining MTD driver data */
  1572. mtd->type = MTD_NANDFLASH;
  1573. mtd->flags = MTD_CAP_NANDFLASH;
  1574. mtd->ecctype = MTD_ECC_SW;
  1575. mtd->erase = onenand_erase;
  1576. mtd->point = NULL;
  1577. mtd->unpoint = NULL;
  1578. mtd->read = onenand_read;
  1579. mtd->write = onenand_write;
  1580. mtd->read_oob = onenand_read_oob;
  1581. mtd->write_oob = onenand_write_oob;
  1582. #ifdef CONFIG_MTD_ONENAND_OTP
  1583. mtd->get_fact_prot_info = onenand_get_fact_prot_info;
  1584. mtd->read_fact_prot_reg = onenand_read_fact_prot_reg;
  1585. mtd->get_user_prot_info = onenand_get_user_prot_info;
  1586. mtd->read_user_prot_reg = onenand_read_user_prot_reg;
  1587. mtd->write_user_prot_reg = onenand_write_user_prot_reg;
  1588. mtd->lock_user_prot_reg = onenand_lock_user_prot_reg;
  1589. #endif
  1590. mtd->sync = onenand_sync;
  1591. mtd->lock = NULL;
  1592. mtd->unlock = onenand_unlock;
  1593. mtd->suspend = onenand_suspend;
  1594. mtd->resume = onenand_resume;
  1595. mtd->block_isbad = onenand_block_isbad;
  1596. mtd->block_markbad = onenand_block_markbad;
  1597. mtd->owner = THIS_MODULE;
  1598. /* Unlock whole block */
  1599. onenand_unlock_all(mtd);
  1600. return this->scan_bbt(mtd);
  1601. }
  1602. /**
  1603. * onenand_release - [OneNAND Interface] Free resources held by the OneNAND device
  1604. * @param mtd MTD device structure
  1605. */
  1606. void onenand_release(struct mtd_info *mtd)
  1607. {
  1608. struct onenand_chip *this = mtd->priv;
  1609. #ifdef CONFIG_MTD_PARTITIONS
  1610. /* Deregister partitions */
  1611. del_mtd_partitions (mtd);
  1612. #endif
  1613. /* Deregister the device */
  1614. del_mtd_device (mtd);
  1615. /* Free bad block table memory, if allocated */
  1616. if (this->bbm)
  1617. kfree(this->bbm);
  1618. /* Buffer allocated by onenand_scan */
  1619. if (this->options & ONENAND_PAGEBUF_ALLOC)
  1620. kfree(this->page_buf);
  1621. }
  1622. EXPORT_SYMBOL_GPL(onenand_scan);
  1623. EXPORT_SYMBOL_GPL(onenand_release);
  1624. MODULE_LICENSE("GPL");
  1625. MODULE_AUTHOR("Kyungmin Park <kyungmin.park@samsung.com>");
  1626. MODULE_DESCRIPTION("Generic OneNAND flash driver code");