onenand_base.c 70 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723
  1. /*
  2. * linux/drivers/mtd/onenand/onenand_base.c
  3. *
  4. * Copyright (C) 2005-2007 Samsung Electronics
  5. * Kyungmin Park <kyungmin.park@samsung.com>
  6. *
  7. * Credits:
  8. * Adrian Hunter <ext-adrian.hunter@nokia.com>:
  9. * auto-placement support, read-while load support, various fixes
  10. * Copyright (C) Nokia Corporation, 2007
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License version 2 as
  14. * published by the Free Software Foundation.
  15. */
  16. #include <linux/kernel.h>
  17. #include <linux/module.h>
  18. #include <linux/init.h>
  19. #include <linux/sched.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/jiffies.h>
  22. #include <linux/mtd/mtd.h>
  23. #include <linux/mtd/onenand.h>
  24. #include <linux/mtd/partitions.h>
  25. #include <asm/io.h>
  26. /**
  27. * onenand_oob_64 - oob info for large (2KB) page
  28. */
  29. static struct nand_ecclayout onenand_oob_64 = {
  30. .eccbytes = 20,
  31. .eccpos = {
  32. 8, 9, 10, 11, 12,
  33. 24, 25, 26, 27, 28,
  34. 40, 41, 42, 43, 44,
  35. 56, 57, 58, 59, 60,
  36. },
  37. .oobfree = {
  38. {2, 3}, {14, 2}, {18, 3}, {30, 2},
  39. {34, 3}, {46, 2}, {50, 3}, {62, 2}
  40. }
  41. };
  42. /**
  43. * onenand_oob_32 - oob info for middle (1KB) page
  44. */
  45. static struct nand_ecclayout onenand_oob_32 = {
  46. .eccbytes = 10,
  47. .eccpos = {
  48. 8, 9, 10, 11, 12,
  49. 24, 25, 26, 27, 28,
  50. },
  51. .oobfree = { {2, 3}, {14, 2}, {18, 3}, {30, 2} }
  52. };
  53. static const unsigned char ffchars[] = {
  54. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  55. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 16 */
  56. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  57. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 32 */
  58. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  59. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 48 */
  60. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  61. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 64 */
  62. };
  63. /**
  64. * onenand_readw - [OneNAND Interface] Read OneNAND register
  65. * @param addr address to read
  66. *
  67. * Read OneNAND register
  68. */
  69. static unsigned short onenand_readw(void __iomem *addr)
  70. {
  71. return readw(addr);
  72. }
  73. /**
  74. * onenand_writew - [OneNAND Interface] Write OneNAND register with value
  75. * @param value value to write
  76. * @param addr address to write
  77. *
  78. * Write OneNAND register with value
  79. */
  80. static void onenand_writew(unsigned short value, void __iomem *addr)
  81. {
  82. writew(value, addr);
  83. }
  84. /**
  85. * onenand_block_address - [DEFAULT] Get block address
  86. * @param this onenand chip data structure
  87. * @param block the block
  88. * @return translated block address if DDP, otherwise same
  89. *
  90. * Setup Start Address 1 Register (F100h)
  91. */
  92. static int onenand_block_address(struct onenand_chip *this, int block)
  93. {
  94. /* Device Flash Core select, NAND Flash Block Address */
  95. if (block & this->density_mask)
  96. return ONENAND_DDP_CHIP1 | (block ^ this->density_mask);
  97. return block;
  98. }
  99. /**
  100. * onenand_bufferram_address - [DEFAULT] Get bufferram address
  101. * @param this onenand chip data structure
  102. * @param block the block
  103. * @return set DBS value if DDP, otherwise 0
  104. *
  105. * Setup Start Address 2 Register (F101h) for DDP
  106. */
  107. static int onenand_bufferram_address(struct onenand_chip *this, int block)
  108. {
  109. /* Device BufferRAM Select */
  110. if (block & this->density_mask)
  111. return ONENAND_DDP_CHIP1;
  112. return ONENAND_DDP_CHIP0;
  113. }
  114. /**
  115. * onenand_page_address - [DEFAULT] Get page address
  116. * @param page the page address
  117. * @param sector the sector address
  118. * @return combined page and sector address
  119. *
  120. * Setup Start Address 8 Register (F107h)
  121. */
  122. static int onenand_page_address(int page, int sector)
  123. {
  124. /* Flash Page Address, Flash Sector Address */
  125. int fpa, fsa;
  126. fpa = page & ONENAND_FPA_MASK;
  127. fsa = sector & ONENAND_FSA_MASK;
  128. return ((fpa << ONENAND_FPA_SHIFT) | fsa);
  129. }
  130. /**
  131. * onenand_buffer_address - [DEFAULT] Get buffer address
  132. * @param dataram1 DataRAM index
  133. * @param sectors the sector address
  134. * @param count the number of sectors
  135. * @return the start buffer value
  136. *
  137. * Setup Start Buffer Register (F200h)
  138. */
  139. static int onenand_buffer_address(int dataram1, int sectors, int count)
  140. {
  141. int bsa, bsc;
  142. /* BufferRAM Sector Address */
  143. bsa = sectors & ONENAND_BSA_MASK;
  144. if (dataram1)
  145. bsa |= ONENAND_BSA_DATARAM1; /* DataRAM1 */
  146. else
  147. bsa |= ONENAND_BSA_DATARAM0; /* DataRAM0 */
  148. /* BufferRAM Sector Count */
  149. bsc = count & ONENAND_BSC_MASK;
  150. return ((bsa << ONENAND_BSA_SHIFT) | bsc);
  151. }
  152. /**
  153. * onenand_command - [DEFAULT] Send command to OneNAND device
  154. * @param mtd MTD device structure
  155. * @param cmd the command to be sent
  156. * @param addr offset to read from or write to
  157. * @param len number of bytes to read or write
  158. *
  159. * Send command to OneNAND device. This function is used for middle/large page
  160. * devices (1KB/2KB Bytes per page)
  161. */
  162. static int onenand_command(struct mtd_info *mtd, int cmd, loff_t addr, size_t len)
  163. {
  164. struct onenand_chip *this = mtd->priv;
  165. int value, readcmd = 0, block_cmd = 0;
  166. int block, page;
  167. /* Address translation */
  168. switch (cmd) {
  169. case ONENAND_CMD_UNLOCK:
  170. case ONENAND_CMD_LOCK:
  171. case ONENAND_CMD_LOCK_TIGHT:
  172. case ONENAND_CMD_UNLOCK_ALL:
  173. block = -1;
  174. page = -1;
  175. break;
  176. case ONENAND_CMD_ERASE:
  177. case ONENAND_CMD_BUFFERRAM:
  178. case ONENAND_CMD_OTP_ACCESS:
  179. block_cmd = 1;
  180. block = (int) (addr >> this->erase_shift);
  181. page = -1;
  182. break;
  183. default:
  184. block = (int) (addr >> this->erase_shift);
  185. page = (int) (addr >> this->page_shift);
  186. if (ONENAND_IS_2PLANE(this)) {
  187. /* Make the even block number */
  188. block &= ~1;
  189. /* Is it the odd plane? */
  190. if (addr & this->writesize)
  191. block++;
  192. page >>= 1;
  193. }
  194. page &= this->page_mask;
  195. break;
  196. }
  197. /* NOTE: The setting order of the registers is very important! */
  198. if (cmd == ONENAND_CMD_BUFFERRAM) {
  199. /* Select DataRAM for DDP */
  200. value = onenand_bufferram_address(this, block);
  201. this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
  202. if (ONENAND_IS_2PLANE(this))
  203. /* It is always BufferRAM0 */
  204. ONENAND_SET_BUFFERRAM0(this);
  205. else
  206. /* Switch to the next data buffer */
  207. ONENAND_SET_NEXT_BUFFERRAM(this);
  208. return 0;
  209. }
  210. if (block != -1) {
  211. /* Write 'DFS, FBA' of Flash */
  212. value = onenand_block_address(this, block);
  213. this->write_word(value, this->base + ONENAND_REG_START_ADDRESS1);
  214. if (block_cmd) {
  215. /* Select DataRAM for DDP */
  216. value = onenand_bufferram_address(this, block);
  217. this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
  218. }
  219. }
  220. if (page != -1) {
  221. /* Now we use page size operation */
  222. int sectors = 4, count = 4;
  223. int dataram;
  224. switch (cmd) {
  225. case ONENAND_CMD_READ:
  226. case ONENAND_CMD_READOOB:
  227. dataram = ONENAND_SET_NEXT_BUFFERRAM(this);
  228. readcmd = 1;
  229. break;
  230. default:
  231. if (ONENAND_IS_2PLANE(this) && cmd == ONENAND_CMD_PROG)
  232. cmd = ONENAND_CMD_2X_PROG;
  233. dataram = ONENAND_CURRENT_BUFFERRAM(this);
  234. break;
  235. }
  236. /* Write 'FPA, FSA' of Flash */
  237. value = onenand_page_address(page, sectors);
  238. this->write_word(value, this->base + ONENAND_REG_START_ADDRESS8);
  239. /* Write 'BSA, BSC' of DataRAM */
  240. value = onenand_buffer_address(dataram, sectors, count);
  241. this->write_word(value, this->base + ONENAND_REG_START_BUFFER);
  242. if (readcmd) {
  243. /* Select DataRAM for DDP */
  244. value = onenand_bufferram_address(this, block);
  245. this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
  246. }
  247. }
  248. /* Interrupt clear */
  249. this->write_word(ONENAND_INT_CLEAR, this->base + ONENAND_REG_INTERRUPT);
  250. /* Write command */
  251. this->write_word(cmd, this->base + ONENAND_REG_COMMAND);
  252. return 0;
  253. }
  254. /**
  255. * onenand_wait - [DEFAULT] wait until the command is done
  256. * @param mtd MTD device structure
  257. * @param state state to select the max. timeout value
  258. *
  259. * Wait for command done. This applies to all OneNAND command
  260. * Read can take up to 30us, erase up to 2ms and program up to 350us
  261. * according to general OneNAND specs
  262. */
  263. static int onenand_wait(struct mtd_info *mtd, int state)
  264. {
  265. struct onenand_chip * this = mtd->priv;
  266. unsigned long timeout;
  267. unsigned int flags = ONENAND_INT_MASTER;
  268. unsigned int interrupt = 0;
  269. unsigned int ctrl;
  270. /* The 20 msec is enough */
  271. timeout = jiffies + msecs_to_jiffies(20);
  272. while (time_before(jiffies, timeout)) {
  273. interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
  274. if (interrupt & flags)
  275. break;
  276. if (state != FL_READING)
  277. cond_resched();
  278. }
  279. /* To get correct interrupt status in timeout case */
  280. interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
  281. ctrl = this->read_word(this->base + ONENAND_REG_CTRL_STATUS);
  282. if (ctrl & ONENAND_CTRL_ERROR) {
  283. printk(KERN_ERR "onenand_wait: controller error = 0x%04x\n", ctrl);
  284. if (ctrl & ONENAND_CTRL_LOCK)
  285. printk(KERN_ERR "onenand_wait: it's locked error.\n");
  286. return -EIO;
  287. }
  288. if (interrupt & ONENAND_INT_READ) {
  289. int ecc = this->read_word(this->base + ONENAND_REG_ECC_STATUS);
  290. if (ecc) {
  291. if (ecc & ONENAND_ECC_2BIT_ALL) {
  292. printk(KERN_ERR "onenand_wait: ECC error = 0x%04x\n", ecc);
  293. mtd->ecc_stats.failed++;
  294. return -EBADMSG;
  295. } else if (ecc & ONENAND_ECC_1BIT_ALL) {
  296. printk(KERN_INFO "onenand_wait: correctable ECC error = 0x%04x\n", ecc);
  297. mtd->ecc_stats.corrected++;
  298. }
  299. }
  300. } else if (state == FL_READING) {
  301. printk(KERN_ERR "onenand_wait: read timeout! ctrl=0x%04x intr=0x%04x\n", ctrl, interrupt);
  302. return -EIO;
  303. }
  304. return 0;
  305. }
  306. /*
  307. * onenand_interrupt - [DEFAULT] onenand interrupt handler
  308. * @param irq onenand interrupt number
  309. * @param dev_id interrupt data
  310. *
  311. * complete the work
  312. */
  313. static irqreturn_t onenand_interrupt(int irq, void *data)
  314. {
  315. struct onenand_chip *this = (struct onenand_chip *) data;
  316. /* To handle shared interrupt */
  317. if (!this->complete.done)
  318. complete(&this->complete);
  319. return IRQ_HANDLED;
  320. }
  321. /*
  322. * onenand_interrupt_wait - [DEFAULT] wait until the command is done
  323. * @param mtd MTD device structure
  324. * @param state state to select the max. timeout value
  325. *
  326. * Wait for command done.
  327. */
  328. static int onenand_interrupt_wait(struct mtd_info *mtd, int state)
  329. {
  330. struct onenand_chip *this = mtd->priv;
  331. wait_for_completion(&this->complete);
  332. return onenand_wait(mtd, state);
  333. }
  334. /*
  335. * onenand_try_interrupt_wait - [DEFAULT] try interrupt wait
  336. * @param mtd MTD device structure
  337. * @param state state to select the max. timeout value
  338. *
  339. * Try interrupt based wait (It is used one-time)
  340. */
  341. static int onenand_try_interrupt_wait(struct mtd_info *mtd, int state)
  342. {
  343. struct onenand_chip *this = mtd->priv;
  344. unsigned long remain, timeout;
  345. /* We use interrupt wait first */
  346. this->wait = onenand_interrupt_wait;
  347. timeout = msecs_to_jiffies(100);
  348. remain = wait_for_completion_timeout(&this->complete, timeout);
  349. if (!remain) {
  350. printk(KERN_INFO "OneNAND: There's no interrupt. "
  351. "We use the normal wait\n");
  352. /* Release the irq */
  353. free_irq(this->irq, this);
  354. this->wait = onenand_wait;
  355. }
  356. return onenand_wait(mtd, state);
  357. }
  358. /*
  359. * onenand_setup_wait - [OneNAND Interface] setup onenand wait method
  360. * @param mtd MTD device structure
  361. *
  362. * There's two method to wait onenand work
  363. * 1. polling - read interrupt status register
  364. * 2. interrupt - use the kernel interrupt method
  365. */
  366. static void onenand_setup_wait(struct mtd_info *mtd)
  367. {
  368. struct onenand_chip *this = mtd->priv;
  369. int syscfg;
  370. init_completion(&this->complete);
  371. if (this->irq <= 0) {
  372. this->wait = onenand_wait;
  373. return;
  374. }
  375. if (request_irq(this->irq, &onenand_interrupt,
  376. IRQF_SHARED, "onenand", this)) {
  377. /* If we can't get irq, use the normal wait */
  378. this->wait = onenand_wait;
  379. return;
  380. }
  381. /* Enable interrupt */
  382. syscfg = this->read_word(this->base + ONENAND_REG_SYS_CFG1);
  383. syscfg |= ONENAND_SYS_CFG1_IOBE;
  384. this->write_word(syscfg, this->base + ONENAND_REG_SYS_CFG1);
  385. this->wait = onenand_try_interrupt_wait;
  386. }
  387. /**
  388. * onenand_bufferram_offset - [DEFAULT] BufferRAM offset
  389. * @param mtd MTD data structure
  390. * @param area BufferRAM area
  391. * @return offset given area
  392. *
  393. * Return BufferRAM offset given area
  394. */
  395. static inline int onenand_bufferram_offset(struct mtd_info *mtd, int area)
  396. {
  397. struct onenand_chip *this = mtd->priv;
  398. if (ONENAND_CURRENT_BUFFERRAM(this)) {
  399. /* Note: the 'this->writesize' is a real page size */
  400. if (area == ONENAND_DATARAM)
  401. return this->writesize;
  402. if (area == ONENAND_SPARERAM)
  403. return mtd->oobsize;
  404. }
  405. return 0;
  406. }
  407. /**
  408. * onenand_read_bufferram - [OneNAND Interface] Read the bufferram area
  409. * @param mtd MTD data structure
  410. * @param area BufferRAM area
  411. * @param buffer the databuffer to put/get data
  412. * @param offset offset to read from or write to
  413. * @param count number of bytes to read/write
  414. *
  415. * Read the BufferRAM area
  416. */
  417. static int onenand_read_bufferram(struct mtd_info *mtd, int area,
  418. unsigned char *buffer, int offset, size_t count)
  419. {
  420. struct onenand_chip *this = mtd->priv;
  421. void __iomem *bufferram;
  422. bufferram = this->base + area;
  423. bufferram += onenand_bufferram_offset(mtd, area);
  424. if (ONENAND_CHECK_BYTE_ACCESS(count)) {
  425. unsigned short word;
  426. /* Align with word(16-bit) size */
  427. count--;
  428. /* Read word and save byte */
  429. word = this->read_word(bufferram + offset + count);
  430. buffer[count] = (word & 0xff);
  431. }
  432. memcpy(buffer, bufferram + offset, count);
  433. return 0;
  434. }
  435. /**
  436. * onenand_sync_read_bufferram - [OneNAND Interface] Read the bufferram area with Sync. Burst mode
  437. * @param mtd MTD data structure
  438. * @param area BufferRAM area
  439. * @param buffer the databuffer to put/get data
  440. * @param offset offset to read from or write to
  441. * @param count number of bytes to read/write
  442. *
  443. * Read the BufferRAM area with Sync. Burst Mode
  444. */
  445. static int onenand_sync_read_bufferram(struct mtd_info *mtd, int area,
  446. unsigned char *buffer, int offset, size_t count)
  447. {
  448. struct onenand_chip *this = mtd->priv;
  449. void __iomem *bufferram;
  450. bufferram = this->base + area;
  451. bufferram += onenand_bufferram_offset(mtd, area);
  452. this->mmcontrol(mtd, ONENAND_SYS_CFG1_SYNC_READ);
  453. if (ONENAND_CHECK_BYTE_ACCESS(count)) {
  454. unsigned short word;
  455. /* Align with word(16-bit) size */
  456. count--;
  457. /* Read word and save byte */
  458. word = this->read_word(bufferram + offset + count);
  459. buffer[count] = (word & 0xff);
  460. }
  461. memcpy(buffer, bufferram + offset, count);
  462. this->mmcontrol(mtd, 0);
  463. return 0;
  464. }
  465. /**
  466. * onenand_write_bufferram - [OneNAND Interface] Write the bufferram area
  467. * @param mtd MTD data structure
  468. * @param area BufferRAM area
  469. * @param buffer the databuffer to put/get data
  470. * @param offset offset to read from or write to
  471. * @param count number of bytes to read/write
  472. *
  473. * Write the BufferRAM area
  474. */
  475. static int onenand_write_bufferram(struct mtd_info *mtd, int area,
  476. const unsigned char *buffer, int offset, size_t count)
  477. {
  478. struct onenand_chip *this = mtd->priv;
  479. void __iomem *bufferram;
  480. bufferram = this->base + area;
  481. bufferram += onenand_bufferram_offset(mtd, area);
  482. if (ONENAND_CHECK_BYTE_ACCESS(count)) {
  483. unsigned short word;
  484. int byte_offset;
  485. /* Align with word(16-bit) size */
  486. count--;
  487. /* Calculate byte access offset */
  488. byte_offset = offset + count;
  489. /* Read word and save byte */
  490. word = this->read_word(bufferram + byte_offset);
  491. word = (word & ~0xff) | buffer[count];
  492. this->write_word(word, bufferram + byte_offset);
  493. }
  494. memcpy(bufferram + offset, buffer, count);
  495. return 0;
  496. }
  497. /**
  498. * onenand_get_2x_blockpage - [GENERIC] Get blockpage at 2x program mode
  499. * @param mtd MTD data structure
  500. * @param addr address to check
  501. * @return blockpage address
  502. *
  503. * Get blockpage address at 2x program mode
  504. */
  505. static int onenand_get_2x_blockpage(struct mtd_info *mtd, loff_t addr)
  506. {
  507. struct onenand_chip *this = mtd->priv;
  508. int blockpage, block, page;
  509. /* Calculate the even block number */
  510. block = (int) (addr >> this->erase_shift) & ~1;
  511. /* Is it the odd plane? */
  512. if (addr & this->writesize)
  513. block++;
  514. page = (int) (addr >> (this->page_shift + 1)) & this->page_mask;
  515. blockpage = (block << 7) | page;
  516. return blockpage;
  517. }
  518. /**
  519. * onenand_check_bufferram - [GENERIC] Check BufferRAM information
  520. * @param mtd MTD data structure
  521. * @param addr address to check
  522. * @return 1 if there are valid data, otherwise 0
  523. *
  524. * Check bufferram if there is data we required
  525. */
  526. static int onenand_check_bufferram(struct mtd_info *mtd, loff_t addr)
  527. {
  528. struct onenand_chip *this = mtd->priv;
  529. int blockpage, found = 0;
  530. unsigned int i;
  531. if (ONENAND_IS_2PLANE(this))
  532. blockpage = onenand_get_2x_blockpage(mtd, addr);
  533. else
  534. blockpage = (int) (addr >> this->page_shift);
  535. /* Is there valid data? */
  536. i = ONENAND_CURRENT_BUFFERRAM(this);
  537. if (this->bufferram[i].blockpage == blockpage)
  538. found = 1;
  539. else {
  540. /* Check another BufferRAM */
  541. i = ONENAND_NEXT_BUFFERRAM(this);
  542. if (this->bufferram[i].blockpage == blockpage) {
  543. ONENAND_SET_NEXT_BUFFERRAM(this);
  544. found = 1;
  545. }
  546. }
  547. if (found && ONENAND_IS_DDP(this)) {
  548. /* Select DataRAM for DDP */
  549. int block = (int) (addr >> this->erase_shift);
  550. int value = onenand_bufferram_address(this, block);
  551. this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
  552. }
  553. return found;
  554. }
  555. /**
  556. * onenand_update_bufferram - [GENERIC] Update BufferRAM information
  557. * @param mtd MTD data structure
  558. * @param addr address to update
  559. * @param valid valid flag
  560. *
  561. * Update BufferRAM information
  562. */
  563. static void onenand_update_bufferram(struct mtd_info *mtd, loff_t addr,
  564. int valid)
  565. {
  566. struct onenand_chip *this = mtd->priv;
  567. int blockpage;
  568. unsigned int i;
  569. if (ONENAND_IS_2PLANE(this))
  570. blockpage = onenand_get_2x_blockpage(mtd, addr);
  571. else
  572. blockpage = (int) (addr >> this->page_shift);
  573. /* Invalidate another BufferRAM */
  574. i = ONENAND_NEXT_BUFFERRAM(this);
  575. if (this->bufferram[i].blockpage == blockpage)
  576. this->bufferram[i].blockpage = -1;
  577. /* Update BufferRAM */
  578. i = ONENAND_CURRENT_BUFFERRAM(this);
  579. if (valid)
  580. this->bufferram[i].blockpage = blockpage;
  581. else
  582. this->bufferram[i].blockpage = -1;
  583. }
  584. /**
  585. * onenand_invalidate_bufferram - [GENERIC] Invalidate BufferRAM information
  586. * @param mtd MTD data structure
  587. * @param addr start address to invalidate
  588. * @param len length to invalidate
  589. *
  590. * Invalidate BufferRAM information
  591. */
  592. static void onenand_invalidate_bufferram(struct mtd_info *mtd, loff_t addr,
  593. unsigned int len)
  594. {
  595. struct onenand_chip *this = mtd->priv;
  596. int i;
  597. loff_t end_addr = addr + len;
  598. /* Invalidate BufferRAM */
  599. for (i = 0; i < MAX_BUFFERRAM; i++) {
  600. loff_t buf_addr = this->bufferram[i].blockpage << this->page_shift;
  601. if (buf_addr >= addr && buf_addr < end_addr)
  602. this->bufferram[i].blockpage = -1;
  603. }
  604. }
  605. /**
  606. * onenand_get_device - [GENERIC] Get chip for selected access
  607. * @param mtd MTD device structure
  608. * @param new_state the state which is requested
  609. *
  610. * Get the device and lock it for exclusive access
  611. */
  612. static int onenand_get_device(struct mtd_info *mtd, int new_state)
  613. {
  614. struct onenand_chip *this = mtd->priv;
  615. DECLARE_WAITQUEUE(wait, current);
  616. /*
  617. * Grab the lock and see if the device is available
  618. */
  619. while (1) {
  620. spin_lock(&this->chip_lock);
  621. if (this->state == FL_READY) {
  622. this->state = new_state;
  623. spin_unlock(&this->chip_lock);
  624. break;
  625. }
  626. if (new_state == FL_PM_SUSPENDED) {
  627. spin_unlock(&this->chip_lock);
  628. return (this->state == FL_PM_SUSPENDED) ? 0 : -EAGAIN;
  629. }
  630. set_current_state(TASK_UNINTERRUPTIBLE);
  631. add_wait_queue(&this->wq, &wait);
  632. spin_unlock(&this->chip_lock);
  633. schedule();
  634. remove_wait_queue(&this->wq, &wait);
  635. }
  636. return 0;
  637. }
  638. /**
  639. * onenand_release_device - [GENERIC] release chip
  640. * @param mtd MTD device structure
  641. *
  642. * Deselect, release chip lock and wake up anyone waiting on the device
  643. */
  644. static void onenand_release_device(struct mtd_info *mtd)
  645. {
  646. struct onenand_chip *this = mtd->priv;
  647. /* Release the chip */
  648. spin_lock(&this->chip_lock);
  649. this->state = FL_READY;
  650. wake_up(&this->wq);
  651. spin_unlock(&this->chip_lock);
  652. }
  653. /**
  654. * onenand_transfer_auto_oob - [Internal] oob auto-placement transfer
  655. * @param mtd MTD device structure
  656. * @param buf destination address
  657. * @param column oob offset to read from
  658. * @param thislen oob length to read
  659. */
  660. static int onenand_transfer_auto_oob(struct mtd_info *mtd, uint8_t *buf, int column,
  661. int thislen)
  662. {
  663. struct onenand_chip *this = mtd->priv;
  664. struct nand_oobfree *free;
  665. int readcol = column;
  666. int readend = column + thislen;
  667. int lastgap = 0;
  668. unsigned int i;
  669. uint8_t *oob_buf = this->oob_buf;
  670. free = this->ecclayout->oobfree;
  671. for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES && free->length; i++, free++) {
  672. if (readcol >= lastgap)
  673. readcol += free->offset - lastgap;
  674. if (readend >= lastgap)
  675. readend += free->offset - lastgap;
  676. lastgap = free->offset + free->length;
  677. }
  678. this->read_bufferram(mtd, ONENAND_SPARERAM, oob_buf, 0, mtd->oobsize);
  679. free = this->ecclayout->oobfree;
  680. for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES && free->length; i++, free++) {
  681. int free_end = free->offset + free->length;
  682. if (free->offset < readend && free_end > readcol) {
  683. int st = max_t(int,free->offset,readcol);
  684. int ed = min_t(int,free_end,readend);
  685. int n = ed - st;
  686. memcpy(buf, oob_buf + st, n);
  687. buf += n;
  688. } else if (column == 0)
  689. break;
  690. }
  691. return 0;
  692. }
  693. /**
  694. * onenand_read_ops_nolock - [OneNAND Interface] OneNAND read main and/or out-of-band
  695. * @param mtd MTD device structure
  696. * @param from offset to read from
  697. * @param ops: oob operation description structure
  698. *
  699. * OneNAND read main and/or out-of-band data
  700. */
  701. static int onenand_read_ops_nolock(struct mtd_info *mtd, loff_t from,
  702. struct mtd_oob_ops *ops)
  703. {
  704. struct onenand_chip *this = mtd->priv;
  705. struct mtd_ecc_stats stats;
  706. size_t len = ops->len;
  707. size_t ooblen = ops->ooblen;
  708. u_char *buf = ops->datbuf;
  709. u_char *oobbuf = ops->oobbuf;
  710. int read = 0, column, thislen;
  711. int oobread = 0, oobcolumn, thisooblen, oobsize;
  712. int ret = 0, boundary = 0;
  713. int writesize = this->writesize;
  714. DEBUG(MTD_DEBUG_LEVEL3, "onenand_read_ops_nolock: from = 0x%08x, len = %i\n", (unsigned int) from, (int) len);
  715. if (ops->mode == MTD_OOB_AUTO)
  716. oobsize = this->ecclayout->oobavail;
  717. else
  718. oobsize = mtd->oobsize;
  719. oobcolumn = from & (mtd->oobsize - 1);
  720. /* Do not allow reads past end of device */
  721. if ((from + len) > mtd->size) {
  722. printk(KERN_ERR "onenand_read_ops_nolock: Attempt read beyond end of device\n");
  723. ops->retlen = 0;
  724. ops->oobretlen = 0;
  725. return -EINVAL;
  726. }
  727. stats = mtd->ecc_stats;
  728. /* Read-while-load method */
  729. /* Do first load to bufferRAM */
  730. if (read < len) {
  731. if (!onenand_check_bufferram(mtd, from)) {
  732. this->command(mtd, ONENAND_CMD_READ, from, writesize);
  733. ret = this->wait(mtd, FL_READING);
  734. onenand_update_bufferram(mtd, from, !ret);
  735. }
  736. }
  737. thislen = min_t(int, writesize, len - read);
  738. column = from & (writesize - 1);
  739. if (column + thislen > writesize)
  740. thislen = writesize - column;
  741. while (!ret) {
  742. /* If there is more to load then start next load */
  743. from += thislen;
  744. if (read + thislen < len) {
  745. this->command(mtd, ONENAND_CMD_READ, from, writesize);
  746. /*
  747. * Chip boundary handling in DDP
  748. * Now we issued chip 1 read and pointed chip 1
  749. * bufferam so we have to point chip 0 bufferam.
  750. */
  751. if (ONENAND_IS_DDP(this) &&
  752. unlikely(from == (this->chipsize >> 1))) {
  753. this->write_word(ONENAND_DDP_CHIP0, this->base + ONENAND_REG_START_ADDRESS2);
  754. boundary = 1;
  755. } else
  756. boundary = 0;
  757. ONENAND_SET_PREV_BUFFERRAM(this);
  758. }
  759. /* While load is going, read from last bufferRAM */
  760. this->read_bufferram(mtd, ONENAND_DATARAM, buf, column, thislen);
  761. /* Read oob area if needed */
  762. if (oobbuf) {
  763. thisooblen = oobsize - oobcolumn;
  764. thisooblen = min_t(int, thisooblen, ooblen - oobread);
  765. if (ops->mode == MTD_OOB_AUTO)
  766. onenand_transfer_auto_oob(mtd, oobbuf, oobcolumn, thisooblen);
  767. else
  768. this->read_bufferram(mtd, ONENAND_SPARERAM, oobbuf, oobcolumn, thisooblen);
  769. oobread += thisooblen;
  770. oobbuf += thisooblen;
  771. oobcolumn = 0;
  772. }
  773. /* See if we are done */
  774. read += thislen;
  775. if (read == len)
  776. break;
  777. /* Set up for next read from bufferRAM */
  778. if (unlikely(boundary))
  779. this->write_word(ONENAND_DDP_CHIP1, this->base + ONENAND_REG_START_ADDRESS2);
  780. ONENAND_SET_NEXT_BUFFERRAM(this);
  781. buf += thislen;
  782. thislen = min_t(int, writesize, len - read);
  783. column = 0;
  784. cond_resched();
  785. /* Now wait for load */
  786. ret = this->wait(mtd, FL_READING);
  787. onenand_update_bufferram(mtd, from, !ret);
  788. }
  789. /*
  790. * Return success, if no ECC failures, else -EBADMSG
  791. * fs driver will take care of that, because
  792. * retlen == desired len and result == -EBADMSG
  793. */
  794. ops->retlen = read;
  795. ops->oobretlen = oobread;
  796. if (mtd->ecc_stats.failed - stats.failed)
  797. return -EBADMSG;
  798. if (ret)
  799. return ret;
  800. return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
  801. }
  802. /**
  803. * onenand_read_oob_nolock - [MTD Interface] OneNAND read out-of-band
  804. * @param mtd MTD device structure
  805. * @param from offset to read from
  806. * @param ops: oob operation description structure
  807. *
  808. * OneNAND read out-of-band data from the spare area
  809. */
  810. static int onenand_read_oob_nolock(struct mtd_info *mtd, loff_t from,
  811. struct mtd_oob_ops *ops)
  812. {
  813. struct onenand_chip *this = mtd->priv;
  814. int read = 0, thislen, column, oobsize;
  815. size_t len = ops->ooblen;
  816. mtd_oob_mode_t mode = ops->mode;
  817. u_char *buf = ops->oobbuf;
  818. int ret = 0;
  819. from += ops->ooboffs;
  820. DEBUG(MTD_DEBUG_LEVEL3, "onenand_read_oob_nolock: from = 0x%08x, len = %i\n", (unsigned int) from, (int) len);
  821. /* Initialize return length value */
  822. ops->oobretlen = 0;
  823. if (mode == MTD_OOB_AUTO)
  824. oobsize = this->ecclayout->oobavail;
  825. else
  826. oobsize = mtd->oobsize;
  827. column = from & (mtd->oobsize - 1);
  828. if (unlikely(column >= oobsize)) {
  829. printk(KERN_ERR "onenand_read_oob_nolock: Attempted to start read outside oob\n");
  830. return -EINVAL;
  831. }
  832. /* Do not allow reads past end of device */
  833. if (unlikely(from >= mtd->size ||
  834. column + len > ((mtd->size >> this->page_shift) -
  835. (from >> this->page_shift)) * oobsize)) {
  836. printk(KERN_ERR "onenand_read_oob_nolock: Attempted to read beyond end of device\n");
  837. return -EINVAL;
  838. }
  839. while (read < len) {
  840. cond_resched();
  841. thislen = oobsize - column;
  842. thislen = min_t(int, thislen, len);
  843. this->command(mtd, ONENAND_CMD_READOOB, from, mtd->oobsize);
  844. onenand_update_bufferram(mtd, from, 0);
  845. ret = this->wait(mtd, FL_READING);
  846. /* First copy data and check return value for ECC handling */
  847. if (mode == MTD_OOB_AUTO)
  848. onenand_transfer_auto_oob(mtd, buf, column, thislen);
  849. else
  850. this->read_bufferram(mtd, ONENAND_SPARERAM, buf, column, thislen);
  851. if (ret) {
  852. printk(KERN_ERR "onenand_read_oob_nolock: read failed = 0x%x\n", ret);
  853. break;
  854. }
  855. read += thislen;
  856. if (read == len)
  857. break;
  858. buf += thislen;
  859. /* Read more? */
  860. if (read < len) {
  861. /* Page size */
  862. from += mtd->writesize;
  863. column = 0;
  864. }
  865. }
  866. ops->oobretlen = read;
  867. return ret;
  868. }
  869. /**
  870. * onenand_read - [MTD Interface] Read data from flash
  871. * @param mtd MTD device structure
  872. * @param from offset to read from
  873. * @param len number of bytes to read
  874. * @param retlen pointer to variable to store the number of read bytes
  875. * @param buf the databuffer to put data
  876. *
  877. * Read with ecc
  878. */
  879. static int onenand_read(struct mtd_info *mtd, loff_t from, size_t len,
  880. size_t *retlen, u_char *buf)
  881. {
  882. struct mtd_oob_ops ops = {
  883. .len = len,
  884. .ooblen = 0,
  885. .datbuf = buf,
  886. .oobbuf = NULL,
  887. };
  888. int ret;
  889. onenand_get_device(mtd, FL_READING);
  890. ret = onenand_read_ops_nolock(mtd, from, &ops);
  891. onenand_release_device(mtd);
  892. *retlen = ops.retlen;
  893. return ret;
  894. }
  895. /**
  896. * onenand_read_oob - [MTD Interface] Read main and/or out-of-band
  897. * @param mtd: MTD device structure
  898. * @param from: offset to read from
  899. * @param ops: oob operation description structure
  900. * Read main and/or out-of-band
  901. */
  902. static int onenand_read_oob(struct mtd_info *mtd, loff_t from,
  903. struct mtd_oob_ops *ops)
  904. {
  905. int ret;
  906. switch (ops->mode) {
  907. case MTD_OOB_PLACE:
  908. case MTD_OOB_AUTO:
  909. break;
  910. case MTD_OOB_RAW:
  911. /* Not implemented yet */
  912. default:
  913. return -EINVAL;
  914. }
  915. onenand_get_device(mtd, FL_READING);
  916. if (ops->datbuf)
  917. ret = onenand_read_ops_nolock(mtd, from, ops);
  918. else
  919. ret = onenand_read_oob_nolock(mtd, from, ops);
  920. onenand_release_device(mtd);
  921. return ret;
  922. }
  923. /**
  924. * onenand_bbt_wait - [DEFAULT] wait until the command is done
  925. * @param mtd MTD device structure
  926. * @param state state to select the max. timeout value
  927. *
  928. * Wait for command done.
  929. */
  930. static int onenand_bbt_wait(struct mtd_info *mtd, int state)
  931. {
  932. struct onenand_chip *this = mtd->priv;
  933. unsigned long timeout;
  934. unsigned int interrupt;
  935. unsigned int ctrl;
  936. /* The 20 msec is enough */
  937. timeout = jiffies + msecs_to_jiffies(20);
  938. while (time_before(jiffies, timeout)) {
  939. interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
  940. if (interrupt & ONENAND_INT_MASTER)
  941. break;
  942. }
  943. /* To get correct interrupt status in timeout case */
  944. interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
  945. ctrl = this->read_word(this->base + ONENAND_REG_CTRL_STATUS);
  946. if (ctrl & ONENAND_CTRL_ERROR) {
  947. printk(KERN_DEBUG "onenand_bbt_wait: controller error = 0x%04x\n", ctrl);
  948. /* Initial bad block case */
  949. if (ctrl & ONENAND_CTRL_LOAD)
  950. return ONENAND_BBT_READ_ERROR;
  951. return ONENAND_BBT_READ_FATAL_ERROR;
  952. }
  953. if (interrupt & ONENAND_INT_READ) {
  954. int ecc = this->read_word(this->base + ONENAND_REG_ECC_STATUS);
  955. if (ecc & ONENAND_ECC_2BIT_ALL)
  956. return ONENAND_BBT_READ_ERROR;
  957. } else {
  958. printk(KERN_ERR "onenand_bbt_wait: read timeout!"
  959. "ctrl=0x%04x intr=0x%04x\n", ctrl, interrupt);
  960. return ONENAND_BBT_READ_FATAL_ERROR;
  961. }
  962. return 0;
  963. }
  964. /**
  965. * onenand_bbt_read_oob - [MTD Interface] OneNAND read out-of-band for bbt scan
  966. * @param mtd MTD device structure
  967. * @param from offset to read from
  968. * @param ops oob operation description structure
  969. *
  970. * OneNAND read out-of-band data from the spare area for bbt scan
  971. */
  972. int onenand_bbt_read_oob(struct mtd_info *mtd, loff_t from,
  973. struct mtd_oob_ops *ops)
  974. {
  975. struct onenand_chip *this = mtd->priv;
  976. int read = 0, thislen, column;
  977. int ret = 0;
  978. size_t len = ops->ooblen;
  979. u_char *buf = ops->oobbuf;
  980. DEBUG(MTD_DEBUG_LEVEL3, "onenand_bbt_read_oob: from = 0x%08x, len = %zi\n", (unsigned int) from, len);
  981. /* Initialize return value */
  982. ops->oobretlen = 0;
  983. /* Do not allow reads past end of device */
  984. if (unlikely((from + len) > mtd->size)) {
  985. printk(KERN_ERR "onenand_bbt_read_oob: Attempt read beyond end of device\n");
  986. return ONENAND_BBT_READ_FATAL_ERROR;
  987. }
  988. /* Grab the lock and see if the device is available */
  989. onenand_get_device(mtd, FL_READING);
  990. column = from & (mtd->oobsize - 1);
  991. while (read < len) {
  992. cond_resched();
  993. thislen = mtd->oobsize - column;
  994. thislen = min_t(int, thislen, len);
  995. this->command(mtd, ONENAND_CMD_READOOB, from, mtd->oobsize);
  996. onenand_update_bufferram(mtd, from, 0);
  997. ret = onenand_bbt_wait(mtd, FL_READING);
  998. if (ret)
  999. break;
  1000. this->read_bufferram(mtd, ONENAND_SPARERAM, buf, column, thislen);
  1001. read += thislen;
  1002. if (read == len)
  1003. break;
  1004. buf += thislen;
  1005. /* Read more? */
  1006. if (read < len) {
  1007. /* Update Page size */
  1008. from += this->writesize;
  1009. column = 0;
  1010. }
  1011. }
  1012. /* Deselect and wake up anyone waiting on the device */
  1013. onenand_release_device(mtd);
  1014. ops->oobretlen = read;
  1015. return ret;
  1016. }
  1017. #ifdef CONFIG_MTD_ONENAND_VERIFY_WRITE
  1018. /**
  1019. * onenand_verify_oob - [GENERIC] verify the oob contents after a write
  1020. * @param mtd MTD device structure
  1021. * @param buf the databuffer to verify
  1022. * @param to offset to read from
  1023. */
  1024. static int onenand_verify_oob(struct mtd_info *mtd, const u_char *buf, loff_t to)
  1025. {
  1026. struct onenand_chip *this = mtd->priv;
  1027. char oobbuf[64];
  1028. int status, i;
  1029. this->command(mtd, ONENAND_CMD_READOOB, to, mtd->oobsize);
  1030. onenand_update_bufferram(mtd, to, 0);
  1031. status = this->wait(mtd, FL_READING);
  1032. if (status)
  1033. return status;
  1034. this->read_bufferram(mtd, ONENAND_SPARERAM, oobbuf, 0, mtd->oobsize);
  1035. for (i = 0; i < mtd->oobsize; i++)
  1036. if (buf[i] != 0xFF && buf[i] != oobbuf[i])
  1037. return -EBADMSG;
  1038. return 0;
  1039. }
  1040. /**
  1041. * onenand_verify - [GENERIC] verify the chip contents after a write
  1042. * @param mtd MTD device structure
  1043. * @param buf the databuffer to verify
  1044. * @param addr offset to read from
  1045. * @param len number of bytes to read and compare
  1046. */
  1047. static int onenand_verify(struct mtd_info *mtd, const u_char *buf, loff_t addr, size_t len)
  1048. {
  1049. struct onenand_chip *this = mtd->priv;
  1050. void __iomem *dataram;
  1051. int ret = 0;
  1052. int thislen, column;
  1053. while (len != 0) {
  1054. thislen = min_t(int, this->writesize, len);
  1055. column = addr & (this->writesize - 1);
  1056. if (column + thislen > this->writesize)
  1057. thislen = this->writesize - column;
  1058. this->command(mtd, ONENAND_CMD_READ, addr, this->writesize);
  1059. onenand_update_bufferram(mtd, addr, 0);
  1060. ret = this->wait(mtd, FL_READING);
  1061. if (ret)
  1062. return ret;
  1063. onenand_update_bufferram(mtd, addr, 1);
  1064. dataram = this->base + ONENAND_DATARAM;
  1065. dataram += onenand_bufferram_offset(mtd, ONENAND_DATARAM);
  1066. if (memcmp(buf, dataram + column, thislen))
  1067. return -EBADMSG;
  1068. len -= thislen;
  1069. buf += thislen;
  1070. addr += thislen;
  1071. }
  1072. return 0;
  1073. }
  1074. #else
  1075. #define onenand_verify(...) (0)
  1076. #define onenand_verify_oob(...) (0)
  1077. #endif
  1078. #define NOTALIGNED(x) ((x & (this->subpagesize - 1)) != 0)
  1079. /**
  1080. * onenand_fill_auto_oob - [Internal] oob auto-placement transfer
  1081. * @param mtd MTD device structure
  1082. * @param oob_buf oob buffer
  1083. * @param buf source address
  1084. * @param column oob offset to write to
  1085. * @param thislen oob length to write
  1086. */
  1087. static int onenand_fill_auto_oob(struct mtd_info *mtd, u_char *oob_buf,
  1088. const u_char *buf, int column, int thislen)
  1089. {
  1090. struct onenand_chip *this = mtd->priv;
  1091. struct nand_oobfree *free;
  1092. int writecol = column;
  1093. int writeend = column + thislen;
  1094. int lastgap = 0;
  1095. unsigned int i;
  1096. free = this->ecclayout->oobfree;
  1097. for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES && free->length; i++, free++) {
  1098. if (writecol >= lastgap)
  1099. writecol += free->offset - lastgap;
  1100. if (writeend >= lastgap)
  1101. writeend += free->offset - lastgap;
  1102. lastgap = free->offset + free->length;
  1103. }
  1104. free = this->ecclayout->oobfree;
  1105. for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES && free->length; i++, free++) {
  1106. int free_end = free->offset + free->length;
  1107. if (free->offset < writeend && free_end > writecol) {
  1108. int st = max_t(int,free->offset,writecol);
  1109. int ed = min_t(int,free_end,writeend);
  1110. int n = ed - st;
  1111. memcpy(oob_buf + st, buf, n);
  1112. buf += n;
  1113. } else if (column == 0)
  1114. break;
  1115. }
  1116. return 0;
  1117. }
  1118. /**
  1119. * onenand_write_ops_nolock - [OneNAND Interface] write main and/or out-of-band
  1120. * @param mtd MTD device structure
  1121. * @param to offset to write to
  1122. * @param ops oob operation description structure
  1123. *
  1124. * Write main and/or oob with ECC
  1125. */
  1126. static int onenand_write_ops_nolock(struct mtd_info *mtd, loff_t to,
  1127. struct mtd_oob_ops *ops)
  1128. {
  1129. struct onenand_chip *this = mtd->priv;
  1130. int written = 0, column, thislen, subpage;
  1131. int oobwritten = 0, oobcolumn, thisooblen, oobsize;
  1132. size_t len = ops->len;
  1133. size_t ooblen = ops->ooblen;
  1134. const u_char *buf = ops->datbuf;
  1135. const u_char *oob = ops->oobbuf;
  1136. u_char *oobbuf;
  1137. int ret = 0;
  1138. DEBUG(MTD_DEBUG_LEVEL3, "onenand_write_ops_nolock: to = 0x%08x, len = %i\n", (unsigned int) to, (int) len);
  1139. /* Initialize retlen, in case of early exit */
  1140. ops->retlen = 0;
  1141. ops->oobretlen = 0;
  1142. /* Do not allow writes past end of device */
  1143. if (unlikely((to + len) > mtd->size)) {
  1144. printk(KERN_ERR "onenand_write_ops_nolock: Attempt write to past end of device\n");
  1145. return -EINVAL;
  1146. }
  1147. /* Reject writes, which are not page aligned */
  1148. if (unlikely(NOTALIGNED(to)) || unlikely(NOTALIGNED(len))) {
  1149. printk(KERN_ERR "onenand_write_ops_nolock: Attempt to write not page aligned data\n");
  1150. return -EINVAL;
  1151. }
  1152. if (ops->mode == MTD_OOB_AUTO)
  1153. oobsize = this->ecclayout->oobavail;
  1154. else
  1155. oobsize = mtd->oobsize;
  1156. oobcolumn = to & (mtd->oobsize - 1);
  1157. column = to & (mtd->writesize - 1);
  1158. /* Loop until all data write */
  1159. while (written < len) {
  1160. u_char *wbuf = (u_char *) buf;
  1161. thislen = min_t(int, mtd->writesize - column, len - written);
  1162. thisooblen = min_t(int, oobsize - oobcolumn, ooblen - oobwritten);
  1163. cond_resched();
  1164. this->command(mtd, ONENAND_CMD_BUFFERRAM, to, thislen);
  1165. /* Partial page write */
  1166. subpage = thislen < mtd->writesize;
  1167. if (subpage) {
  1168. memset(this->page_buf, 0xff, mtd->writesize);
  1169. memcpy(this->page_buf + column, buf, thislen);
  1170. wbuf = this->page_buf;
  1171. }
  1172. this->write_bufferram(mtd, ONENAND_DATARAM, wbuf, 0, mtd->writesize);
  1173. if (oob) {
  1174. oobbuf = this->oob_buf;
  1175. /* We send data to spare ram with oobsize
  1176. * to prevent byte access */
  1177. memset(oobbuf, 0xff, mtd->oobsize);
  1178. if (ops->mode == MTD_OOB_AUTO)
  1179. onenand_fill_auto_oob(mtd, oobbuf, oob, oobcolumn, thisooblen);
  1180. else
  1181. memcpy(oobbuf + oobcolumn, oob, thisooblen);
  1182. oobwritten += thisooblen;
  1183. oob += thisooblen;
  1184. oobcolumn = 0;
  1185. } else
  1186. oobbuf = (u_char *) ffchars;
  1187. this->write_bufferram(mtd, ONENAND_SPARERAM, oobbuf, 0, mtd->oobsize);
  1188. this->command(mtd, ONENAND_CMD_PROG, to, mtd->writesize);
  1189. ret = this->wait(mtd, FL_WRITING);
  1190. /* In partial page write we don't update bufferram */
  1191. onenand_update_bufferram(mtd, to, !ret && !subpage);
  1192. if (ONENAND_IS_2PLANE(this)) {
  1193. ONENAND_SET_BUFFERRAM1(this);
  1194. onenand_update_bufferram(mtd, to + this->writesize, !ret && !subpage);
  1195. }
  1196. if (ret) {
  1197. printk(KERN_ERR "onenand_write_ops_nolock: write filaed %d\n", ret);
  1198. break;
  1199. }
  1200. /* Only check verify write turn on */
  1201. ret = onenand_verify(mtd, (u_char *) wbuf, to, thislen);
  1202. if (ret) {
  1203. printk(KERN_ERR "onenand_write_ops_nolock: verify failed %d\n", ret);
  1204. break;
  1205. }
  1206. written += thislen;
  1207. if (written == len)
  1208. break;
  1209. column = 0;
  1210. to += thislen;
  1211. buf += thislen;
  1212. }
  1213. /* Deselect and wake up anyone waiting on the device */
  1214. onenand_release_device(mtd);
  1215. ops->retlen = written;
  1216. return ret;
  1217. }
  1218. /**
  1219. * onenand_write_oob_nolock - [Internal] OneNAND write out-of-band
  1220. * @param mtd MTD device structure
  1221. * @param to offset to write to
  1222. * @param len number of bytes to write
  1223. * @param retlen pointer to variable to store the number of written bytes
  1224. * @param buf the data to write
  1225. * @param mode operation mode
  1226. *
  1227. * OneNAND write out-of-band
  1228. */
  1229. static int onenand_write_oob_nolock(struct mtd_info *mtd, loff_t to,
  1230. struct mtd_oob_ops *ops)
  1231. {
  1232. struct onenand_chip *this = mtd->priv;
  1233. int column, ret = 0, oobsize;
  1234. int written = 0;
  1235. u_char *oobbuf;
  1236. size_t len = ops->ooblen;
  1237. const u_char *buf = ops->oobbuf;
  1238. mtd_oob_mode_t mode = ops->mode;
  1239. to += ops->ooboffs;
  1240. DEBUG(MTD_DEBUG_LEVEL3, "onenand_write_oob_nolock: to = 0x%08x, len = %i\n", (unsigned int) to, (int) len);
  1241. /* Initialize retlen, in case of early exit */
  1242. ops->oobretlen = 0;
  1243. if (mode == MTD_OOB_AUTO)
  1244. oobsize = this->ecclayout->oobavail;
  1245. else
  1246. oobsize = mtd->oobsize;
  1247. column = to & (mtd->oobsize - 1);
  1248. if (unlikely(column >= oobsize)) {
  1249. printk(KERN_ERR "onenand_write_oob_nolock: Attempted to start write outside oob\n");
  1250. return -EINVAL;
  1251. }
  1252. /* For compatibility with NAND: Do not allow write past end of page */
  1253. if (unlikely(column + len > oobsize)) {
  1254. printk(KERN_ERR "onenand_write_oob_nolock: "
  1255. "Attempt to write past end of page\n");
  1256. return -EINVAL;
  1257. }
  1258. /* Do not allow reads past end of device */
  1259. if (unlikely(to >= mtd->size ||
  1260. column + len > ((mtd->size >> this->page_shift) -
  1261. (to >> this->page_shift)) * oobsize)) {
  1262. printk(KERN_ERR "onenand_write_oob_nolock: Attempted to write past end of device\n");
  1263. return -EINVAL;
  1264. }
  1265. oobbuf = this->oob_buf;
  1266. /* Loop until all data write */
  1267. while (written < len) {
  1268. int thislen = min_t(int, oobsize, len - written);
  1269. cond_resched();
  1270. this->command(mtd, ONENAND_CMD_BUFFERRAM, to, mtd->oobsize);
  1271. /* We send data to spare ram with oobsize
  1272. * to prevent byte access */
  1273. memset(oobbuf, 0xff, mtd->oobsize);
  1274. if (mode == MTD_OOB_AUTO)
  1275. onenand_fill_auto_oob(mtd, oobbuf, buf, column, thislen);
  1276. else
  1277. memcpy(oobbuf + column, buf, thislen);
  1278. this->write_bufferram(mtd, ONENAND_SPARERAM, oobbuf, 0, mtd->oobsize);
  1279. this->command(mtd, ONENAND_CMD_PROGOOB, to, mtd->oobsize);
  1280. onenand_update_bufferram(mtd, to, 0);
  1281. if (ONENAND_IS_2PLANE(this)) {
  1282. ONENAND_SET_BUFFERRAM1(this);
  1283. onenand_update_bufferram(mtd, to + this->writesize, 0);
  1284. }
  1285. ret = this->wait(mtd, FL_WRITING);
  1286. if (ret) {
  1287. printk(KERN_ERR "onenand_write_oob_nolock: write failed %d\n", ret);
  1288. break;
  1289. }
  1290. ret = onenand_verify_oob(mtd, oobbuf, to);
  1291. if (ret) {
  1292. printk(KERN_ERR "onenand_write_oob_nolock: verify failed %d\n", ret);
  1293. break;
  1294. }
  1295. written += thislen;
  1296. if (written == len)
  1297. break;
  1298. to += mtd->writesize;
  1299. buf += thislen;
  1300. column = 0;
  1301. }
  1302. ops->oobretlen = written;
  1303. return ret;
  1304. }
  1305. /**
  1306. * onenand_write - [MTD Interface] write buffer to FLASH
  1307. * @param mtd MTD device structure
  1308. * @param to offset to write to
  1309. * @param len number of bytes to write
  1310. * @param retlen pointer to variable to store the number of written bytes
  1311. * @param buf the data to write
  1312. *
  1313. * Write with ECC
  1314. */
  1315. static int onenand_write(struct mtd_info *mtd, loff_t to, size_t len,
  1316. size_t *retlen, const u_char *buf)
  1317. {
  1318. struct mtd_oob_ops ops = {
  1319. .len = len,
  1320. .ooblen = 0,
  1321. .datbuf = (u_char *) buf,
  1322. .oobbuf = NULL,
  1323. };
  1324. int ret;
  1325. onenand_get_device(mtd, FL_WRITING);
  1326. ret = onenand_write_ops_nolock(mtd, to, &ops);
  1327. onenand_release_device(mtd);
  1328. *retlen = ops.retlen;
  1329. return ret;
  1330. }
  1331. /**
  1332. * onenand_write_oob - [MTD Interface] NAND write data and/or out-of-band
  1333. * @param mtd: MTD device structure
  1334. * @param to: offset to write
  1335. * @param ops: oob operation description structure
  1336. */
  1337. static int onenand_write_oob(struct mtd_info *mtd, loff_t to,
  1338. struct mtd_oob_ops *ops)
  1339. {
  1340. int ret;
  1341. switch (ops->mode) {
  1342. case MTD_OOB_PLACE:
  1343. case MTD_OOB_AUTO:
  1344. break;
  1345. case MTD_OOB_RAW:
  1346. /* Not implemented yet */
  1347. default:
  1348. return -EINVAL;
  1349. }
  1350. onenand_get_device(mtd, FL_WRITING);
  1351. if (ops->datbuf)
  1352. ret = onenand_write_ops_nolock(mtd, to, ops);
  1353. else
  1354. ret = onenand_write_oob_nolock(mtd, to, ops);
  1355. onenand_release_device(mtd);
  1356. return ret;
  1357. }
  1358. /**
  1359. * onenand_block_isbad_nolock - [GENERIC] Check if a block is marked bad
  1360. * @param mtd MTD device structure
  1361. * @param ofs offset from device start
  1362. * @param allowbbt 1, if its allowed to access the bbt area
  1363. *
  1364. * Check, if the block is bad. Either by reading the bad block table or
  1365. * calling of the scan function.
  1366. */
  1367. static int onenand_block_isbad_nolock(struct mtd_info *mtd, loff_t ofs, int allowbbt)
  1368. {
  1369. struct onenand_chip *this = mtd->priv;
  1370. struct bbm_info *bbm = this->bbm;
  1371. /* Return info from the table */
  1372. return bbm->isbad_bbt(mtd, ofs, allowbbt);
  1373. }
  1374. /**
  1375. * onenand_erase - [MTD Interface] erase block(s)
  1376. * @param mtd MTD device structure
  1377. * @param instr erase instruction
  1378. *
  1379. * Erase one ore more blocks
  1380. */
  1381. static int onenand_erase(struct mtd_info *mtd, struct erase_info *instr)
  1382. {
  1383. struct onenand_chip *this = mtd->priv;
  1384. unsigned int block_size;
  1385. loff_t addr;
  1386. int len;
  1387. int ret = 0;
  1388. DEBUG(MTD_DEBUG_LEVEL3, "onenand_erase: start = 0x%08x, len = %i\n", (unsigned int) instr->addr, (unsigned int) instr->len);
  1389. block_size = (1 << this->erase_shift);
  1390. /* Start address must align on block boundary */
  1391. if (unlikely(instr->addr & (block_size - 1))) {
  1392. printk(KERN_ERR "onenand_erase: Unaligned address\n");
  1393. return -EINVAL;
  1394. }
  1395. /* Length must align on block boundary */
  1396. if (unlikely(instr->len & (block_size - 1))) {
  1397. printk(KERN_ERR "onenand_erase: Length not block aligned\n");
  1398. return -EINVAL;
  1399. }
  1400. /* Do not allow erase past end of device */
  1401. if (unlikely((instr->len + instr->addr) > mtd->size)) {
  1402. printk(KERN_ERR "onenand_erase: Erase past end of device\n");
  1403. return -EINVAL;
  1404. }
  1405. instr->fail_addr = 0xffffffff;
  1406. /* Grab the lock and see if the device is available */
  1407. onenand_get_device(mtd, FL_ERASING);
  1408. /* Loop throught the pages */
  1409. len = instr->len;
  1410. addr = instr->addr;
  1411. instr->state = MTD_ERASING;
  1412. while (len) {
  1413. cond_resched();
  1414. /* Check if we have a bad block, we do not erase bad blocks */
  1415. if (onenand_block_isbad_nolock(mtd, addr, 0)) {
  1416. printk (KERN_WARNING "onenand_erase: attempt to erase a bad block at addr 0x%08x\n", (unsigned int) addr);
  1417. instr->state = MTD_ERASE_FAILED;
  1418. goto erase_exit;
  1419. }
  1420. this->command(mtd, ONENAND_CMD_ERASE, addr, block_size);
  1421. onenand_invalidate_bufferram(mtd, addr, block_size);
  1422. ret = this->wait(mtd, FL_ERASING);
  1423. /* Check, if it is write protected */
  1424. if (ret) {
  1425. printk(KERN_ERR "onenand_erase: Failed erase, block %d\n", (unsigned) (addr >> this->erase_shift));
  1426. instr->state = MTD_ERASE_FAILED;
  1427. instr->fail_addr = addr;
  1428. goto erase_exit;
  1429. }
  1430. len -= block_size;
  1431. addr += block_size;
  1432. }
  1433. instr->state = MTD_ERASE_DONE;
  1434. erase_exit:
  1435. ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
  1436. /* Deselect and wake up anyone waiting on the device */
  1437. onenand_release_device(mtd);
  1438. /* Do call back function */
  1439. if (!ret)
  1440. mtd_erase_callback(instr);
  1441. return ret;
  1442. }
  1443. /**
  1444. * onenand_sync - [MTD Interface] sync
  1445. * @param mtd MTD device structure
  1446. *
  1447. * Sync is actually a wait for chip ready function
  1448. */
  1449. static void onenand_sync(struct mtd_info *mtd)
  1450. {
  1451. DEBUG(MTD_DEBUG_LEVEL3, "onenand_sync: called\n");
  1452. /* Grab the lock and see if the device is available */
  1453. onenand_get_device(mtd, FL_SYNCING);
  1454. /* Release it and go back */
  1455. onenand_release_device(mtd);
  1456. }
  1457. /**
  1458. * onenand_block_isbad - [MTD Interface] Check whether the block at the given offset is bad
  1459. * @param mtd MTD device structure
  1460. * @param ofs offset relative to mtd start
  1461. *
  1462. * Check whether the block is bad
  1463. */
  1464. static int onenand_block_isbad(struct mtd_info *mtd, loff_t ofs)
  1465. {
  1466. int ret;
  1467. /* Check for invalid offset */
  1468. if (ofs > mtd->size)
  1469. return -EINVAL;
  1470. onenand_get_device(mtd, FL_READING);
  1471. ret = onenand_block_isbad_nolock(mtd, ofs, 0);
  1472. onenand_release_device(mtd);
  1473. return ret;
  1474. }
  1475. /**
  1476. * onenand_default_block_markbad - [DEFAULT] mark a block bad
  1477. * @param mtd MTD device structure
  1478. * @param ofs offset from device start
  1479. *
  1480. * This is the default implementation, which can be overridden by
  1481. * a hardware specific driver.
  1482. */
  1483. static int onenand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
  1484. {
  1485. struct onenand_chip *this = mtd->priv;
  1486. struct bbm_info *bbm = this->bbm;
  1487. u_char buf[2] = {0, 0};
  1488. struct mtd_oob_ops ops = {
  1489. .mode = MTD_OOB_PLACE,
  1490. .ooblen = 2,
  1491. .oobbuf = buf,
  1492. .ooboffs = 0,
  1493. };
  1494. int block;
  1495. /* Get block number */
  1496. block = ((int) ofs) >> bbm->bbt_erase_shift;
  1497. if (bbm->bbt)
  1498. bbm->bbt[block >> 2] |= 0x01 << ((block & 0x03) << 1);
  1499. /* We write two bytes, so we dont have to mess with 16 bit access */
  1500. ofs += mtd->oobsize + (bbm->badblockpos & ~0x01);
  1501. return onenand_write_oob_nolock(mtd, ofs, &ops);
  1502. }
  1503. /**
  1504. * onenand_block_markbad - [MTD Interface] Mark the block at the given offset as bad
  1505. * @param mtd MTD device structure
  1506. * @param ofs offset relative to mtd start
  1507. *
  1508. * Mark the block as bad
  1509. */
  1510. static int onenand_block_markbad(struct mtd_info *mtd, loff_t ofs)
  1511. {
  1512. struct onenand_chip *this = mtd->priv;
  1513. int ret;
  1514. ret = onenand_block_isbad(mtd, ofs);
  1515. if (ret) {
  1516. /* If it was bad already, return success and do nothing */
  1517. if (ret > 0)
  1518. return 0;
  1519. return ret;
  1520. }
  1521. onenand_get_device(mtd, FL_WRITING);
  1522. ret = this->block_markbad(mtd, ofs);
  1523. onenand_release_device(mtd);
  1524. return ret;
  1525. }
  1526. /**
  1527. * onenand_do_lock_cmd - [OneNAND Interface] Lock or unlock block(s)
  1528. * @param mtd MTD device structure
  1529. * @param ofs offset relative to mtd start
  1530. * @param len number of bytes to lock or unlock
  1531. * @param cmd lock or unlock command
  1532. *
  1533. * Lock or unlock one or more blocks
  1534. */
  1535. static int onenand_do_lock_cmd(struct mtd_info *mtd, loff_t ofs, size_t len, int cmd)
  1536. {
  1537. struct onenand_chip *this = mtd->priv;
  1538. int start, end, block, value, status;
  1539. int wp_status_mask;
  1540. start = ofs >> this->erase_shift;
  1541. end = len >> this->erase_shift;
  1542. if (cmd == ONENAND_CMD_LOCK)
  1543. wp_status_mask = ONENAND_WP_LS;
  1544. else
  1545. wp_status_mask = ONENAND_WP_US;
  1546. /* Continuous lock scheme */
  1547. if (this->options & ONENAND_HAS_CONT_LOCK) {
  1548. /* Set start block address */
  1549. this->write_word(start, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
  1550. /* Set end block address */
  1551. this->write_word(start + end - 1, this->base + ONENAND_REG_END_BLOCK_ADDRESS);
  1552. /* Write lock command */
  1553. this->command(mtd, cmd, 0, 0);
  1554. /* There's no return value */
  1555. this->wait(mtd, FL_LOCKING);
  1556. /* Sanity check */
  1557. while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
  1558. & ONENAND_CTRL_ONGO)
  1559. continue;
  1560. /* Check lock status */
  1561. status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
  1562. if (!(status & wp_status_mask))
  1563. printk(KERN_ERR "wp status = 0x%x\n", status);
  1564. return 0;
  1565. }
  1566. /* Block lock scheme */
  1567. for (block = start; block < start + end; block++) {
  1568. /* Set block address */
  1569. value = onenand_block_address(this, block);
  1570. this->write_word(value, this->base + ONENAND_REG_START_ADDRESS1);
  1571. /* Select DataRAM for DDP */
  1572. value = onenand_bufferram_address(this, block);
  1573. this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
  1574. /* Set start block address */
  1575. this->write_word(block, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
  1576. /* Write lock command */
  1577. this->command(mtd, cmd, 0, 0);
  1578. /* There's no return value */
  1579. this->wait(mtd, FL_LOCKING);
  1580. /* Sanity check */
  1581. while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
  1582. & ONENAND_CTRL_ONGO)
  1583. continue;
  1584. /* Check lock status */
  1585. status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
  1586. if (!(status & wp_status_mask))
  1587. printk(KERN_ERR "block = %d, wp status = 0x%x\n", block, status);
  1588. }
  1589. return 0;
  1590. }
  1591. /**
  1592. * onenand_lock - [MTD Interface] Lock block(s)
  1593. * @param mtd MTD device structure
  1594. * @param ofs offset relative to mtd start
  1595. * @param len number of bytes to unlock
  1596. *
  1597. * Lock one or more blocks
  1598. */
  1599. static int onenand_lock(struct mtd_info *mtd, loff_t ofs, size_t len)
  1600. {
  1601. int ret;
  1602. onenand_get_device(mtd, FL_LOCKING);
  1603. ret = onenand_do_lock_cmd(mtd, ofs, len, ONENAND_CMD_LOCK);
  1604. onenand_release_device(mtd);
  1605. return ret;
  1606. }
  1607. /**
  1608. * onenand_unlock - [MTD Interface] Unlock block(s)
  1609. * @param mtd MTD device structure
  1610. * @param ofs offset relative to mtd start
  1611. * @param len number of bytes to unlock
  1612. *
  1613. * Unlock one or more blocks
  1614. */
  1615. static int onenand_unlock(struct mtd_info *mtd, loff_t ofs, size_t len)
  1616. {
  1617. int ret;
  1618. onenand_get_device(mtd, FL_LOCKING);
  1619. ret = onenand_do_lock_cmd(mtd, ofs, len, ONENAND_CMD_UNLOCK);
  1620. onenand_release_device(mtd);
  1621. return ret;
  1622. }
  1623. /**
  1624. * onenand_check_lock_status - [OneNAND Interface] Check lock status
  1625. * @param this onenand chip data structure
  1626. *
  1627. * Check lock status
  1628. */
  1629. static void onenand_check_lock_status(struct onenand_chip *this)
  1630. {
  1631. unsigned int value, block, status;
  1632. unsigned int end;
  1633. end = this->chipsize >> this->erase_shift;
  1634. for (block = 0; block < end; block++) {
  1635. /* Set block address */
  1636. value = onenand_block_address(this, block);
  1637. this->write_word(value, this->base + ONENAND_REG_START_ADDRESS1);
  1638. /* Select DataRAM for DDP */
  1639. value = onenand_bufferram_address(this, block);
  1640. this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
  1641. /* Set start block address */
  1642. this->write_word(block, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
  1643. /* Check lock status */
  1644. status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
  1645. if (!(status & ONENAND_WP_US))
  1646. printk(KERN_ERR "block = %d, wp status = 0x%x\n", block, status);
  1647. }
  1648. }
  1649. /**
  1650. * onenand_unlock_all - [OneNAND Interface] unlock all blocks
  1651. * @param mtd MTD device structure
  1652. *
  1653. * Unlock all blocks
  1654. */
  1655. static int onenand_unlock_all(struct mtd_info *mtd)
  1656. {
  1657. struct onenand_chip *this = mtd->priv;
  1658. if (this->options & ONENAND_HAS_UNLOCK_ALL) {
  1659. /* Set start block address */
  1660. this->write_word(0, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
  1661. /* Write unlock command */
  1662. this->command(mtd, ONENAND_CMD_UNLOCK_ALL, 0, 0);
  1663. /* There's no return value */
  1664. this->wait(mtd, FL_LOCKING);
  1665. /* Sanity check */
  1666. while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
  1667. & ONENAND_CTRL_ONGO)
  1668. continue;
  1669. /* Workaround for all block unlock in DDP */
  1670. if (ONENAND_IS_DDP(this)) {
  1671. /* 1st block on another chip */
  1672. loff_t ofs = this->chipsize >> 1;
  1673. size_t len = mtd->erasesize;
  1674. onenand_do_lock_cmd(mtd, ofs, len, ONENAND_CMD_UNLOCK);
  1675. }
  1676. onenand_check_lock_status(this);
  1677. return 0;
  1678. }
  1679. onenand_do_lock_cmd(mtd, 0x0, this->chipsize, ONENAND_CMD_UNLOCK);
  1680. return 0;
  1681. }
  1682. #ifdef CONFIG_MTD_ONENAND_OTP
  1683. /* Interal OTP operation */
  1684. typedef int (*otp_op_t)(struct mtd_info *mtd, loff_t form, size_t len,
  1685. size_t *retlen, u_char *buf);
  1686. /**
  1687. * do_otp_read - [DEFAULT] Read OTP block area
  1688. * @param mtd MTD device structure
  1689. * @param from The offset to read
  1690. * @param len number of bytes to read
  1691. * @param retlen pointer to variable to store the number of readbytes
  1692. * @param buf the databuffer to put/get data
  1693. *
  1694. * Read OTP block area.
  1695. */
  1696. static int do_otp_read(struct mtd_info *mtd, loff_t from, size_t len,
  1697. size_t *retlen, u_char *buf)
  1698. {
  1699. struct onenand_chip *this = mtd->priv;
  1700. struct mtd_oob_ops ops = {
  1701. .len = len,
  1702. .ooblen = 0,
  1703. .datbuf = buf,
  1704. .oobbuf = NULL,
  1705. };
  1706. int ret;
  1707. /* Enter OTP access mode */
  1708. this->command(mtd, ONENAND_CMD_OTP_ACCESS, 0, 0);
  1709. this->wait(mtd, FL_OTPING);
  1710. ret = onenand_read_ops_nolock(mtd, from, &ops);
  1711. /* Exit OTP access mode */
  1712. this->command(mtd, ONENAND_CMD_RESET, 0, 0);
  1713. this->wait(mtd, FL_RESETING);
  1714. return ret;
  1715. }
  1716. /**
  1717. * do_otp_write - [DEFAULT] Write OTP block area
  1718. * @param mtd MTD device structure
  1719. * @param to The offset to write
  1720. * @param len number of bytes to write
  1721. * @param retlen pointer to variable to store the number of write bytes
  1722. * @param buf the databuffer to put/get data
  1723. *
  1724. * Write OTP block area.
  1725. */
  1726. static int do_otp_write(struct mtd_info *mtd, loff_t to, size_t len,
  1727. size_t *retlen, u_char *buf)
  1728. {
  1729. struct onenand_chip *this = mtd->priv;
  1730. unsigned char *pbuf = buf;
  1731. int ret;
  1732. struct mtd_oob_ops ops;
  1733. /* Force buffer page aligned */
  1734. if (len < mtd->writesize) {
  1735. memcpy(this->page_buf, buf, len);
  1736. memset(this->page_buf + len, 0xff, mtd->writesize - len);
  1737. pbuf = this->page_buf;
  1738. len = mtd->writesize;
  1739. }
  1740. /* Enter OTP access mode */
  1741. this->command(mtd, ONENAND_CMD_OTP_ACCESS, 0, 0);
  1742. this->wait(mtd, FL_OTPING);
  1743. ops.len = len;
  1744. ops.ooblen = 0;
  1745. ops.datbuf = pbuf;
  1746. ops.oobbuf = NULL;
  1747. ret = onenand_write_ops_nolock(mtd, to, &ops);
  1748. *retlen = ops.retlen;
  1749. /* Exit OTP access mode */
  1750. this->command(mtd, ONENAND_CMD_RESET, 0, 0);
  1751. this->wait(mtd, FL_RESETING);
  1752. return ret;
  1753. }
  1754. /**
  1755. * do_otp_lock - [DEFAULT] Lock OTP block area
  1756. * @param mtd MTD device structure
  1757. * @param from The offset to lock
  1758. * @param len number of bytes to lock
  1759. * @param retlen pointer to variable to store the number of lock bytes
  1760. * @param buf the databuffer to put/get data
  1761. *
  1762. * Lock OTP block area.
  1763. */
  1764. static int do_otp_lock(struct mtd_info *mtd, loff_t from, size_t len,
  1765. size_t *retlen, u_char *buf)
  1766. {
  1767. struct onenand_chip *this = mtd->priv;
  1768. struct mtd_oob_ops ops = {
  1769. .mode = MTD_OOB_PLACE,
  1770. .ooblen = len,
  1771. .oobbuf = buf,
  1772. .ooboffs = 0,
  1773. };
  1774. int ret;
  1775. /* Enter OTP access mode */
  1776. this->command(mtd, ONENAND_CMD_OTP_ACCESS, 0, 0);
  1777. this->wait(mtd, FL_OTPING);
  1778. ret = onenand_write_oob_nolock(mtd, from, &ops);
  1779. *retlen = ops.oobretlen;
  1780. /* Exit OTP access mode */
  1781. this->command(mtd, ONENAND_CMD_RESET, 0, 0);
  1782. this->wait(mtd, FL_RESETING);
  1783. return ret;
  1784. }
  1785. /**
  1786. * onenand_otp_walk - [DEFAULT] Handle OTP operation
  1787. * @param mtd MTD device structure
  1788. * @param from The offset to read/write
  1789. * @param len number of bytes to read/write
  1790. * @param retlen pointer to variable to store the number of read bytes
  1791. * @param buf the databuffer to put/get data
  1792. * @param action do given action
  1793. * @param mode specify user and factory
  1794. *
  1795. * Handle OTP operation.
  1796. */
  1797. static int onenand_otp_walk(struct mtd_info *mtd, loff_t from, size_t len,
  1798. size_t *retlen, u_char *buf,
  1799. otp_op_t action, int mode)
  1800. {
  1801. struct onenand_chip *this = mtd->priv;
  1802. int otp_pages;
  1803. int density;
  1804. int ret = 0;
  1805. *retlen = 0;
  1806. density = this->device_id >> ONENAND_DEVICE_DENSITY_SHIFT;
  1807. if (density < ONENAND_DEVICE_DENSITY_512Mb)
  1808. otp_pages = 20;
  1809. else
  1810. otp_pages = 10;
  1811. if (mode == MTD_OTP_FACTORY) {
  1812. from += mtd->writesize * otp_pages;
  1813. otp_pages = 64 - otp_pages;
  1814. }
  1815. /* Check User/Factory boundary */
  1816. if (((mtd->writesize * otp_pages) - (from + len)) < 0)
  1817. return 0;
  1818. onenand_get_device(mtd, FL_OTPING);
  1819. while (len > 0 && otp_pages > 0) {
  1820. if (!action) { /* OTP Info functions */
  1821. struct otp_info *otpinfo;
  1822. len -= sizeof(struct otp_info);
  1823. if (len <= 0) {
  1824. ret = -ENOSPC;
  1825. break;
  1826. }
  1827. otpinfo = (struct otp_info *) buf;
  1828. otpinfo->start = from;
  1829. otpinfo->length = mtd->writesize;
  1830. otpinfo->locked = 0;
  1831. from += mtd->writesize;
  1832. buf += sizeof(struct otp_info);
  1833. *retlen += sizeof(struct otp_info);
  1834. } else {
  1835. size_t tmp_retlen;
  1836. int size = len;
  1837. ret = action(mtd, from, len, &tmp_retlen, buf);
  1838. buf += size;
  1839. len -= size;
  1840. *retlen += size;
  1841. if (ret)
  1842. break;
  1843. }
  1844. otp_pages--;
  1845. }
  1846. onenand_release_device(mtd);
  1847. return ret;
  1848. }
  1849. /**
  1850. * onenand_get_fact_prot_info - [MTD Interface] Read factory OTP info
  1851. * @param mtd MTD device structure
  1852. * @param buf the databuffer to put/get data
  1853. * @param len number of bytes to read
  1854. *
  1855. * Read factory OTP info.
  1856. */
  1857. static int onenand_get_fact_prot_info(struct mtd_info *mtd,
  1858. struct otp_info *buf, size_t len)
  1859. {
  1860. size_t retlen;
  1861. int ret;
  1862. ret = onenand_otp_walk(mtd, 0, len, &retlen, (u_char *) buf, NULL, MTD_OTP_FACTORY);
  1863. return ret ? : retlen;
  1864. }
  1865. /**
  1866. * onenand_read_fact_prot_reg - [MTD Interface] Read factory OTP area
  1867. * @param mtd MTD device structure
  1868. * @param from The offset to read
  1869. * @param len number of bytes to read
  1870. * @param retlen pointer to variable to store the number of read bytes
  1871. * @param buf the databuffer to put/get data
  1872. *
  1873. * Read factory OTP area.
  1874. */
  1875. static int onenand_read_fact_prot_reg(struct mtd_info *mtd, loff_t from,
  1876. size_t len, size_t *retlen, u_char *buf)
  1877. {
  1878. return onenand_otp_walk(mtd, from, len, retlen, buf, do_otp_read, MTD_OTP_FACTORY);
  1879. }
  1880. /**
  1881. * onenand_get_user_prot_info - [MTD Interface] Read user OTP info
  1882. * @param mtd MTD device structure
  1883. * @param buf the databuffer to put/get data
  1884. * @param len number of bytes to read
  1885. *
  1886. * Read user OTP info.
  1887. */
  1888. static int onenand_get_user_prot_info(struct mtd_info *mtd,
  1889. struct otp_info *buf, size_t len)
  1890. {
  1891. size_t retlen;
  1892. int ret;
  1893. ret = onenand_otp_walk(mtd, 0, len, &retlen, (u_char *) buf, NULL, MTD_OTP_USER);
  1894. return ret ? : retlen;
  1895. }
  1896. /**
  1897. * onenand_read_user_prot_reg - [MTD Interface] Read user OTP area
  1898. * @param mtd MTD device structure
  1899. * @param from The offset to read
  1900. * @param len number of bytes to read
  1901. * @param retlen pointer to variable to store the number of read bytes
  1902. * @param buf the databuffer to put/get data
  1903. *
  1904. * Read user OTP area.
  1905. */
  1906. static int onenand_read_user_prot_reg(struct mtd_info *mtd, loff_t from,
  1907. size_t len, size_t *retlen, u_char *buf)
  1908. {
  1909. return onenand_otp_walk(mtd, from, len, retlen, buf, do_otp_read, MTD_OTP_USER);
  1910. }
  1911. /**
  1912. * onenand_write_user_prot_reg - [MTD Interface] Write user OTP area
  1913. * @param mtd MTD device structure
  1914. * @param from The offset to write
  1915. * @param len number of bytes to write
  1916. * @param retlen pointer to variable to store the number of write bytes
  1917. * @param buf the databuffer to put/get data
  1918. *
  1919. * Write user OTP area.
  1920. */
  1921. static int onenand_write_user_prot_reg(struct mtd_info *mtd, loff_t from,
  1922. size_t len, size_t *retlen, u_char *buf)
  1923. {
  1924. return onenand_otp_walk(mtd, from, len, retlen, buf, do_otp_write, MTD_OTP_USER);
  1925. }
  1926. /**
  1927. * onenand_lock_user_prot_reg - [MTD Interface] Lock user OTP area
  1928. * @param mtd MTD device structure
  1929. * @param from The offset to lock
  1930. * @param len number of bytes to unlock
  1931. *
  1932. * Write lock mark on spare area in page 0 in OTP block
  1933. */
  1934. static int onenand_lock_user_prot_reg(struct mtd_info *mtd, loff_t from,
  1935. size_t len)
  1936. {
  1937. unsigned char oob_buf[64];
  1938. size_t retlen;
  1939. int ret;
  1940. memset(oob_buf, 0xff, mtd->oobsize);
  1941. /*
  1942. * Note: OTP lock operation
  1943. * OTP block : 0xXXFC
  1944. * 1st block : 0xXXF3 (If chip support)
  1945. * Both : 0xXXF0 (If chip support)
  1946. */
  1947. oob_buf[ONENAND_OTP_LOCK_OFFSET] = 0xFC;
  1948. /*
  1949. * Write lock mark to 8th word of sector0 of page0 of the spare0.
  1950. * We write 16 bytes spare area instead of 2 bytes.
  1951. */
  1952. from = 0;
  1953. len = 16;
  1954. ret = onenand_otp_walk(mtd, from, len, &retlen, oob_buf, do_otp_lock, MTD_OTP_USER);
  1955. return ret ? : retlen;
  1956. }
  1957. #endif /* CONFIG_MTD_ONENAND_OTP */
  1958. /**
  1959. * onenand_check_features - Check and set OneNAND features
  1960. * @param mtd MTD data structure
  1961. *
  1962. * Check and set OneNAND features
  1963. * - lock scheme
  1964. * - two plane
  1965. */
  1966. static void onenand_check_features(struct mtd_info *mtd)
  1967. {
  1968. struct onenand_chip *this = mtd->priv;
  1969. unsigned int density, process;
  1970. /* Lock scheme depends on density and process */
  1971. density = this->device_id >> ONENAND_DEVICE_DENSITY_SHIFT;
  1972. process = this->version_id >> ONENAND_VERSION_PROCESS_SHIFT;
  1973. /* Lock scheme */
  1974. switch (density) {
  1975. case ONENAND_DEVICE_DENSITY_4Gb:
  1976. this->options |= ONENAND_HAS_2PLANE;
  1977. case ONENAND_DEVICE_DENSITY_2Gb:
  1978. /* 2Gb DDP don't have 2 plane */
  1979. if (!ONENAND_IS_DDP(this))
  1980. this->options |= ONENAND_HAS_2PLANE;
  1981. this->options |= ONENAND_HAS_UNLOCK_ALL;
  1982. case ONENAND_DEVICE_DENSITY_1Gb:
  1983. /* A-Die has all block unlock */
  1984. if (process)
  1985. this->options |= ONENAND_HAS_UNLOCK_ALL;
  1986. break;
  1987. default:
  1988. /* Some OneNAND has continuous lock scheme */
  1989. if (!process)
  1990. this->options |= ONENAND_HAS_CONT_LOCK;
  1991. break;
  1992. }
  1993. if (this->options & ONENAND_HAS_CONT_LOCK)
  1994. printk(KERN_DEBUG "Lock scheme is Continuous Lock\n");
  1995. if (this->options & ONENAND_HAS_UNLOCK_ALL)
  1996. printk(KERN_DEBUG "Chip support all block unlock\n");
  1997. if (this->options & ONENAND_HAS_2PLANE)
  1998. printk(KERN_DEBUG "Chip has 2 plane\n");
  1999. }
  2000. /**
  2001. * onenand_print_device_info - Print device & version ID
  2002. * @param device device ID
  2003. * @param version version ID
  2004. *
  2005. * Print device & version ID
  2006. */
  2007. static void onenand_print_device_info(int device, int version)
  2008. {
  2009. int vcc, demuxed, ddp, density;
  2010. vcc = device & ONENAND_DEVICE_VCC_MASK;
  2011. demuxed = device & ONENAND_DEVICE_IS_DEMUX;
  2012. ddp = device & ONENAND_DEVICE_IS_DDP;
  2013. density = device >> ONENAND_DEVICE_DENSITY_SHIFT;
  2014. printk(KERN_INFO "%sOneNAND%s %dMB %sV 16-bit (0x%02x)\n",
  2015. demuxed ? "" : "Muxed ",
  2016. ddp ? "(DDP)" : "",
  2017. (16 << density),
  2018. vcc ? "2.65/3.3" : "1.8",
  2019. device);
  2020. printk(KERN_INFO "OneNAND version = 0x%04x\n", version);
  2021. }
  2022. static const struct onenand_manufacturers onenand_manuf_ids[] = {
  2023. {ONENAND_MFR_SAMSUNG, "Samsung"},
  2024. };
  2025. /**
  2026. * onenand_check_maf - Check manufacturer ID
  2027. * @param manuf manufacturer ID
  2028. *
  2029. * Check manufacturer ID
  2030. */
  2031. static int onenand_check_maf(int manuf)
  2032. {
  2033. int size = ARRAY_SIZE(onenand_manuf_ids);
  2034. char *name;
  2035. int i;
  2036. for (i = 0; i < size; i++)
  2037. if (manuf == onenand_manuf_ids[i].id)
  2038. break;
  2039. if (i < size)
  2040. name = onenand_manuf_ids[i].name;
  2041. else
  2042. name = "Unknown";
  2043. printk(KERN_DEBUG "OneNAND Manufacturer: %s (0x%0x)\n", name, manuf);
  2044. return (i == size);
  2045. }
  2046. /**
  2047. * onenand_probe - [OneNAND Interface] Probe the OneNAND device
  2048. * @param mtd MTD device structure
  2049. *
  2050. * OneNAND detection method:
  2051. * Compare the values from command with ones from register
  2052. */
  2053. static int onenand_probe(struct mtd_info *mtd)
  2054. {
  2055. struct onenand_chip *this = mtd->priv;
  2056. int bram_maf_id, bram_dev_id, maf_id, dev_id, ver_id;
  2057. int density;
  2058. int syscfg;
  2059. /* Save system configuration 1 */
  2060. syscfg = this->read_word(this->base + ONENAND_REG_SYS_CFG1);
  2061. /* Clear Sync. Burst Read mode to read BootRAM */
  2062. this->write_word((syscfg & ~ONENAND_SYS_CFG1_SYNC_READ), this->base + ONENAND_REG_SYS_CFG1);
  2063. /* Send the command for reading device ID from BootRAM */
  2064. this->write_word(ONENAND_CMD_READID, this->base + ONENAND_BOOTRAM);
  2065. /* Read manufacturer and device IDs from BootRAM */
  2066. bram_maf_id = this->read_word(this->base + ONENAND_BOOTRAM + 0x0);
  2067. bram_dev_id = this->read_word(this->base + ONENAND_BOOTRAM + 0x2);
  2068. /* Reset OneNAND to read default register values */
  2069. this->write_word(ONENAND_CMD_RESET, this->base + ONENAND_BOOTRAM);
  2070. /* Wait reset */
  2071. this->wait(mtd, FL_RESETING);
  2072. /* Restore system configuration 1 */
  2073. this->write_word(syscfg, this->base + ONENAND_REG_SYS_CFG1);
  2074. /* Check manufacturer ID */
  2075. if (onenand_check_maf(bram_maf_id))
  2076. return -ENXIO;
  2077. /* Read manufacturer and device IDs from Register */
  2078. maf_id = this->read_word(this->base + ONENAND_REG_MANUFACTURER_ID);
  2079. dev_id = this->read_word(this->base + ONENAND_REG_DEVICE_ID);
  2080. ver_id = this->read_word(this->base + ONENAND_REG_VERSION_ID);
  2081. /* Check OneNAND device */
  2082. if (maf_id != bram_maf_id || dev_id != bram_dev_id)
  2083. return -ENXIO;
  2084. /* Flash device information */
  2085. onenand_print_device_info(dev_id, ver_id);
  2086. this->device_id = dev_id;
  2087. this->version_id = ver_id;
  2088. density = dev_id >> ONENAND_DEVICE_DENSITY_SHIFT;
  2089. this->chipsize = (16 << density) << 20;
  2090. /* Set density mask. it is used for DDP */
  2091. if (ONENAND_IS_DDP(this))
  2092. this->density_mask = (1 << (density + 6));
  2093. else
  2094. this->density_mask = 0;
  2095. /* OneNAND page size & block size */
  2096. /* The data buffer size is equal to page size */
  2097. mtd->writesize = this->read_word(this->base + ONENAND_REG_DATA_BUFFER_SIZE);
  2098. mtd->oobsize = mtd->writesize >> 5;
  2099. /* Pages per a block are always 64 in OneNAND */
  2100. mtd->erasesize = mtd->writesize << 6;
  2101. this->erase_shift = ffs(mtd->erasesize) - 1;
  2102. this->page_shift = ffs(mtd->writesize) - 1;
  2103. this->page_mask = (1 << (this->erase_shift - this->page_shift)) - 1;
  2104. /* It's real page size */
  2105. this->writesize = mtd->writesize;
  2106. /* REVIST: Multichip handling */
  2107. mtd->size = this->chipsize;
  2108. /* Check OneNAND features */
  2109. onenand_check_features(mtd);
  2110. /*
  2111. * We emulate the 4KiB page and 256KiB erase block size
  2112. * But oobsize is still 64 bytes.
  2113. * It is only valid if you turn on 2X program support,
  2114. * Otherwise it will be ignored by compiler.
  2115. */
  2116. if (ONENAND_IS_2PLANE(this)) {
  2117. mtd->writesize <<= 1;
  2118. mtd->erasesize <<= 1;
  2119. }
  2120. return 0;
  2121. }
  2122. /**
  2123. * onenand_suspend - [MTD Interface] Suspend the OneNAND flash
  2124. * @param mtd MTD device structure
  2125. */
  2126. static int onenand_suspend(struct mtd_info *mtd)
  2127. {
  2128. return onenand_get_device(mtd, FL_PM_SUSPENDED);
  2129. }
  2130. /**
  2131. * onenand_resume - [MTD Interface] Resume the OneNAND flash
  2132. * @param mtd MTD device structure
  2133. */
  2134. static void onenand_resume(struct mtd_info *mtd)
  2135. {
  2136. struct onenand_chip *this = mtd->priv;
  2137. if (this->state == FL_PM_SUSPENDED)
  2138. onenand_release_device(mtd);
  2139. else
  2140. printk(KERN_ERR "resume() called for the chip which is not"
  2141. "in suspended state\n");
  2142. }
  2143. /**
  2144. * onenand_scan - [OneNAND Interface] Scan for the OneNAND device
  2145. * @param mtd MTD device structure
  2146. * @param maxchips Number of chips to scan for
  2147. *
  2148. * This fills out all the not initialized function pointers
  2149. * with the defaults.
  2150. * The flash ID is read and the mtd/chip structures are
  2151. * filled with the appropriate values.
  2152. */
  2153. int onenand_scan(struct mtd_info *mtd, int maxchips)
  2154. {
  2155. int i;
  2156. struct onenand_chip *this = mtd->priv;
  2157. if (!this->read_word)
  2158. this->read_word = onenand_readw;
  2159. if (!this->write_word)
  2160. this->write_word = onenand_writew;
  2161. if (!this->command)
  2162. this->command = onenand_command;
  2163. if (!this->wait)
  2164. onenand_setup_wait(mtd);
  2165. if (!this->read_bufferram)
  2166. this->read_bufferram = onenand_read_bufferram;
  2167. if (!this->write_bufferram)
  2168. this->write_bufferram = onenand_write_bufferram;
  2169. if (!this->block_markbad)
  2170. this->block_markbad = onenand_default_block_markbad;
  2171. if (!this->scan_bbt)
  2172. this->scan_bbt = onenand_default_bbt;
  2173. if (onenand_probe(mtd))
  2174. return -ENXIO;
  2175. /* Set Sync. Burst Read after probing */
  2176. if (this->mmcontrol) {
  2177. printk(KERN_INFO "OneNAND Sync. Burst Read support\n");
  2178. this->read_bufferram = onenand_sync_read_bufferram;
  2179. }
  2180. /* Allocate buffers, if necessary */
  2181. if (!this->page_buf) {
  2182. this->page_buf = kzalloc(mtd->writesize, GFP_KERNEL);
  2183. if (!this->page_buf) {
  2184. printk(KERN_ERR "onenand_scan(): Can't allocate page_buf\n");
  2185. return -ENOMEM;
  2186. }
  2187. this->options |= ONENAND_PAGEBUF_ALLOC;
  2188. }
  2189. if (!this->oob_buf) {
  2190. this->oob_buf = kzalloc(mtd->oobsize, GFP_KERNEL);
  2191. if (!this->oob_buf) {
  2192. printk(KERN_ERR "onenand_scan(): Can't allocate oob_buf\n");
  2193. if (this->options & ONENAND_PAGEBUF_ALLOC) {
  2194. this->options &= ~ONENAND_PAGEBUF_ALLOC;
  2195. kfree(this->page_buf);
  2196. }
  2197. return -ENOMEM;
  2198. }
  2199. this->options |= ONENAND_OOBBUF_ALLOC;
  2200. }
  2201. this->state = FL_READY;
  2202. init_waitqueue_head(&this->wq);
  2203. spin_lock_init(&this->chip_lock);
  2204. /*
  2205. * Allow subpage writes up to oobsize.
  2206. */
  2207. switch (mtd->oobsize) {
  2208. case 64:
  2209. this->ecclayout = &onenand_oob_64;
  2210. mtd->subpage_sft = 2;
  2211. break;
  2212. case 32:
  2213. this->ecclayout = &onenand_oob_32;
  2214. mtd->subpage_sft = 1;
  2215. break;
  2216. default:
  2217. printk(KERN_WARNING "No OOB scheme defined for oobsize %d\n",
  2218. mtd->oobsize);
  2219. mtd->subpage_sft = 0;
  2220. /* To prevent kernel oops */
  2221. this->ecclayout = &onenand_oob_32;
  2222. break;
  2223. }
  2224. this->subpagesize = mtd->writesize >> mtd->subpage_sft;
  2225. /*
  2226. * The number of bytes available for a client to place data into
  2227. * the out of band area
  2228. */
  2229. this->ecclayout->oobavail = 0;
  2230. for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES &&
  2231. this->ecclayout->oobfree[i].length; i++)
  2232. this->ecclayout->oobavail +=
  2233. this->ecclayout->oobfree[i].length;
  2234. mtd->oobavail = this->ecclayout->oobavail;
  2235. mtd->ecclayout = this->ecclayout;
  2236. /* Fill in remaining MTD driver data */
  2237. mtd->type = MTD_NANDFLASH;
  2238. mtd->flags = MTD_CAP_NANDFLASH;
  2239. mtd->erase = onenand_erase;
  2240. mtd->point = NULL;
  2241. mtd->unpoint = NULL;
  2242. mtd->read = onenand_read;
  2243. mtd->write = onenand_write;
  2244. mtd->read_oob = onenand_read_oob;
  2245. mtd->write_oob = onenand_write_oob;
  2246. #ifdef CONFIG_MTD_ONENAND_OTP
  2247. mtd->get_fact_prot_info = onenand_get_fact_prot_info;
  2248. mtd->read_fact_prot_reg = onenand_read_fact_prot_reg;
  2249. mtd->get_user_prot_info = onenand_get_user_prot_info;
  2250. mtd->read_user_prot_reg = onenand_read_user_prot_reg;
  2251. mtd->write_user_prot_reg = onenand_write_user_prot_reg;
  2252. mtd->lock_user_prot_reg = onenand_lock_user_prot_reg;
  2253. #endif
  2254. mtd->sync = onenand_sync;
  2255. mtd->lock = onenand_lock;
  2256. mtd->unlock = onenand_unlock;
  2257. mtd->suspend = onenand_suspend;
  2258. mtd->resume = onenand_resume;
  2259. mtd->block_isbad = onenand_block_isbad;
  2260. mtd->block_markbad = onenand_block_markbad;
  2261. mtd->owner = THIS_MODULE;
  2262. /* Unlock whole block */
  2263. onenand_unlock_all(mtd);
  2264. return this->scan_bbt(mtd);
  2265. }
  2266. /**
  2267. * onenand_release - [OneNAND Interface] Free resources held by the OneNAND device
  2268. * @param mtd MTD device structure
  2269. */
  2270. void onenand_release(struct mtd_info *mtd)
  2271. {
  2272. struct onenand_chip *this = mtd->priv;
  2273. #ifdef CONFIG_MTD_PARTITIONS
  2274. /* Deregister partitions */
  2275. del_mtd_partitions (mtd);
  2276. #endif
  2277. /* Deregister the device */
  2278. del_mtd_device (mtd);
  2279. /* Free bad block table memory, if allocated */
  2280. if (this->bbm) {
  2281. struct bbm_info *bbm = this->bbm;
  2282. kfree(bbm->bbt);
  2283. kfree(this->bbm);
  2284. }
  2285. /* Buffers allocated by onenand_scan */
  2286. if (this->options & ONENAND_PAGEBUF_ALLOC)
  2287. kfree(this->page_buf);
  2288. if (this->options & ONENAND_OOBBUF_ALLOC)
  2289. kfree(this->oob_buf);
  2290. }
  2291. EXPORT_SYMBOL_GPL(onenand_scan);
  2292. EXPORT_SYMBOL_GPL(onenand_release);
  2293. MODULE_LICENSE("GPL");
  2294. MODULE_AUTHOR("Kyungmin Park <kyungmin.park@samsung.com>");
  2295. MODULE_DESCRIPTION("Generic OneNAND flash driver code");