onenand_base.c 52 KB

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