onenand_base.c 70 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732
  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 = 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. if (ret == -EBADMSG)
  736. ret = 0;
  737. }
  738. }
  739. thislen = min_t(int, writesize, len - read);
  740. column = from & (writesize - 1);
  741. if (column + thislen > writesize)
  742. thislen = writesize - column;
  743. while (!ret) {
  744. /* If there is more to load then start next load */
  745. from += thislen;
  746. if (read + thislen < len) {
  747. this->command(mtd, ONENAND_CMD_READ, from, writesize);
  748. /*
  749. * Chip boundary handling in DDP
  750. * Now we issued chip 1 read and pointed chip 1
  751. * bufferam so we have to point chip 0 bufferam.
  752. */
  753. if (ONENAND_IS_DDP(this) &&
  754. unlikely(from == (this->chipsize >> 1))) {
  755. this->write_word(ONENAND_DDP_CHIP0, this->base + ONENAND_REG_START_ADDRESS2);
  756. boundary = 1;
  757. } else
  758. boundary = 0;
  759. ONENAND_SET_PREV_BUFFERRAM(this);
  760. }
  761. /* While load is going, read from last bufferRAM */
  762. this->read_bufferram(mtd, ONENAND_DATARAM, buf, column, thislen);
  763. /* Read oob area if needed */
  764. if (oobbuf) {
  765. thisooblen = oobsize - oobcolumn;
  766. thisooblen = min_t(int, thisooblen, ooblen - oobread);
  767. if (ops->mode == MTD_OOB_AUTO)
  768. onenand_transfer_auto_oob(mtd, oobbuf, oobcolumn, thisooblen);
  769. else
  770. this->read_bufferram(mtd, ONENAND_SPARERAM, oobbuf, oobcolumn, thisooblen);
  771. oobread += thisooblen;
  772. oobbuf += thisooblen;
  773. oobcolumn = 0;
  774. }
  775. /* See if we are done */
  776. read += thislen;
  777. if (read == len)
  778. break;
  779. /* Set up for next read from bufferRAM */
  780. if (unlikely(boundary))
  781. this->write_word(ONENAND_DDP_CHIP1, this->base + ONENAND_REG_START_ADDRESS2);
  782. ONENAND_SET_NEXT_BUFFERRAM(this);
  783. buf += thislen;
  784. thislen = min_t(int, writesize, len - read);
  785. column = 0;
  786. cond_resched();
  787. /* Now wait for load */
  788. ret = this->wait(mtd, FL_READING);
  789. onenand_update_bufferram(mtd, from, !ret);
  790. if (ret == -EBADMSG)
  791. ret = 0;
  792. }
  793. /*
  794. * Return success, if no ECC failures, else -EBADMSG
  795. * fs driver will take care of that, because
  796. * retlen == desired len and result == -EBADMSG
  797. */
  798. ops->retlen = read;
  799. ops->oobretlen = oobread;
  800. if (ret)
  801. return ret;
  802. if (mtd->ecc_stats.failed - stats.failed)
  803. return -EBADMSG;
  804. return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
  805. }
  806. /**
  807. * onenand_read_oob_nolock - [MTD Interface] OneNAND read out-of-band
  808. * @param mtd MTD device structure
  809. * @param from offset to read from
  810. * @param ops: oob operation description structure
  811. *
  812. * OneNAND read out-of-band data from the spare area
  813. */
  814. static int onenand_read_oob_nolock(struct mtd_info *mtd, loff_t from,
  815. struct mtd_oob_ops *ops)
  816. {
  817. struct onenand_chip *this = mtd->priv;
  818. struct mtd_ecc_stats stats;
  819. int read = 0, thislen, column, oobsize;
  820. size_t len = ops->ooblen;
  821. mtd_oob_mode_t mode = ops->mode;
  822. u_char *buf = ops->oobbuf;
  823. int ret = 0;
  824. from += ops->ooboffs;
  825. DEBUG(MTD_DEBUG_LEVEL3, "onenand_read_oob_nolock: from = 0x%08x, len = %i\n", (unsigned int) from, (int) len);
  826. /* Initialize return length value */
  827. ops->oobretlen = 0;
  828. if (mode == MTD_OOB_AUTO)
  829. oobsize = this->ecclayout->oobavail;
  830. else
  831. oobsize = mtd->oobsize;
  832. column = from & (mtd->oobsize - 1);
  833. if (unlikely(column >= oobsize)) {
  834. printk(KERN_ERR "onenand_read_oob_nolock: Attempted to start read outside oob\n");
  835. return -EINVAL;
  836. }
  837. /* Do not allow reads past end of device */
  838. if (unlikely(from >= mtd->size ||
  839. column + len > ((mtd->size >> this->page_shift) -
  840. (from >> this->page_shift)) * oobsize)) {
  841. printk(KERN_ERR "onenand_read_oob_nolock: Attempted to read beyond end of device\n");
  842. return -EINVAL;
  843. }
  844. stats = mtd->ecc_stats;
  845. while (read < len) {
  846. cond_resched();
  847. thislen = oobsize - column;
  848. thislen = min_t(int, thislen, len);
  849. this->command(mtd, ONENAND_CMD_READOOB, from, mtd->oobsize);
  850. onenand_update_bufferram(mtd, from, 0);
  851. ret = this->wait(mtd, FL_READING);
  852. if (ret && ret != -EBADMSG) {
  853. printk(KERN_ERR "onenand_read_oob_nolock: read failed = 0x%x\n", ret);
  854. break;
  855. }
  856. if (mode == MTD_OOB_AUTO)
  857. onenand_transfer_auto_oob(mtd, buf, column, thislen);
  858. else
  859. this->read_bufferram(mtd, ONENAND_SPARERAM, buf, column, thislen);
  860. read += thislen;
  861. if (read == len)
  862. break;
  863. buf += thislen;
  864. /* Read more? */
  865. if (read < len) {
  866. /* Page size */
  867. from += mtd->writesize;
  868. column = 0;
  869. }
  870. }
  871. ops->oobretlen = read;
  872. if (ret)
  873. return ret;
  874. if (mtd->ecc_stats.failed - stats.failed)
  875. return -EBADMSG;
  876. return 0;
  877. }
  878. /**
  879. * onenand_read - [MTD Interface] Read data from flash
  880. * @param mtd MTD device structure
  881. * @param from offset to read from
  882. * @param len number of bytes to read
  883. * @param retlen pointer to variable to store the number of read bytes
  884. * @param buf the databuffer to put data
  885. *
  886. * Read with ecc
  887. */
  888. static int onenand_read(struct mtd_info *mtd, loff_t from, size_t len,
  889. size_t *retlen, u_char *buf)
  890. {
  891. struct mtd_oob_ops ops = {
  892. .len = len,
  893. .ooblen = 0,
  894. .datbuf = buf,
  895. .oobbuf = NULL,
  896. };
  897. int ret;
  898. onenand_get_device(mtd, FL_READING);
  899. ret = onenand_read_ops_nolock(mtd, from, &ops);
  900. onenand_release_device(mtd);
  901. *retlen = ops.retlen;
  902. return ret;
  903. }
  904. /**
  905. * onenand_read_oob - [MTD Interface] Read main and/or out-of-band
  906. * @param mtd: MTD device structure
  907. * @param from: offset to read from
  908. * @param ops: oob operation description structure
  909. * Read main and/or out-of-band
  910. */
  911. static int onenand_read_oob(struct mtd_info *mtd, loff_t from,
  912. struct mtd_oob_ops *ops)
  913. {
  914. int ret;
  915. switch (ops->mode) {
  916. case MTD_OOB_PLACE:
  917. case MTD_OOB_AUTO:
  918. break;
  919. case MTD_OOB_RAW:
  920. /* Not implemented yet */
  921. default:
  922. return -EINVAL;
  923. }
  924. onenand_get_device(mtd, FL_READING);
  925. if (ops->datbuf)
  926. ret = onenand_read_ops_nolock(mtd, from, ops);
  927. else
  928. ret = onenand_read_oob_nolock(mtd, from, ops);
  929. onenand_release_device(mtd);
  930. return ret;
  931. }
  932. /**
  933. * onenand_bbt_wait - [DEFAULT] wait until the command is done
  934. * @param mtd MTD device structure
  935. * @param state state to select the max. timeout value
  936. *
  937. * Wait for command done.
  938. */
  939. static int onenand_bbt_wait(struct mtd_info *mtd, int state)
  940. {
  941. struct onenand_chip *this = mtd->priv;
  942. unsigned long timeout;
  943. unsigned int interrupt;
  944. unsigned int ctrl;
  945. /* The 20 msec is enough */
  946. timeout = jiffies + msecs_to_jiffies(20);
  947. while (time_before(jiffies, timeout)) {
  948. interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
  949. if (interrupt & ONENAND_INT_MASTER)
  950. break;
  951. }
  952. /* To get correct interrupt status in timeout case */
  953. interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
  954. ctrl = this->read_word(this->base + ONENAND_REG_CTRL_STATUS);
  955. if (ctrl & ONENAND_CTRL_ERROR) {
  956. printk(KERN_DEBUG "onenand_bbt_wait: controller error = 0x%04x\n", ctrl);
  957. /* Initial bad block case */
  958. if (ctrl & ONENAND_CTRL_LOAD)
  959. return ONENAND_BBT_READ_ERROR;
  960. return ONENAND_BBT_READ_FATAL_ERROR;
  961. }
  962. if (interrupt & ONENAND_INT_READ) {
  963. int ecc = this->read_word(this->base + ONENAND_REG_ECC_STATUS);
  964. if (ecc & ONENAND_ECC_2BIT_ALL)
  965. return ONENAND_BBT_READ_ERROR;
  966. } else {
  967. printk(KERN_ERR "onenand_bbt_wait: read timeout!"
  968. "ctrl=0x%04x intr=0x%04x\n", ctrl, interrupt);
  969. return ONENAND_BBT_READ_FATAL_ERROR;
  970. }
  971. return 0;
  972. }
  973. /**
  974. * onenand_bbt_read_oob - [MTD Interface] OneNAND read out-of-band for bbt scan
  975. * @param mtd MTD device structure
  976. * @param from offset to read from
  977. * @param ops oob operation description structure
  978. *
  979. * OneNAND read out-of-band data from the spare area for bbt scan
  980. */
  981. int onenand_bbt_read_oob(struct mtd_info *mtd, loff_t from,
  982. struct mtd_oob_ops *ops)
  983. {
  984. struct onenand_chip *this = mtd->priv;
  985. int read = 0, thislen, column;
  986. int ret = 0;
  987. size_t len = ops->ooblen;
  988. u_char *buf = ops->oobbuf;
  989. DEBUG(MTD_DEBUG_LEVEL3, "onenand_bbt_read_oob: from = 0x%08x, len = %zi\n", (unsigned int) from, len);
  990. /* Initialize return value */
  991. ops->oobretlen = 0;
  992. /* Do not allow reads past end of device */
  993. if (unlikely((from + len) > mtd->size)) {
  994. printk(KERN_ERR "onenand_bbt_read_oob: Attempt read beyond end of device\n");
  995. return ONENAND_BBT_READ_FATAL_ERROR;
  996. }
  997. /* Grab the lock and see if the device is available */
  998. onenand_get_device(mtd, FL_READING);
  999. column = from & (mtd->oobsize - 1);
  1000. while (read < len) {
  1001. cond_resched();
  1002. thislen = mtd->oobsize - column;
  1003. thislen = min_t(int, thislen, len);
  1004. this->command(mtd, ONENAND_CMD_READOOB, from, mtd->oobsize);
  1005. onenand_update_bufferram(mtd, from, 0);
  1006. ret = onenand_bbt_wait(mtd, FL_READING);
  1007. if (ret)
  1008. break;
  1009. this->read_bufferram(mtd, ONENAND_SPARERAM, buf, column, thislen);
  1010. read += thislen;
  1011. if (read == len)
  1012. break;
  1013. buf += thislen;
  1014. /* Read more? */
  1015. if (read < len) {
  1016. /* Update Page size */
  1017. from += this->writesize;
  1018. column = 0;
  1019. }
  1020. }
  1021. /* Deselect and wake up anyone waiting on the device */
  1022. onenand_release_device(mtd);
  1023. ops->oobretlen = read;
  1024. return ret;
  1025. }
  1026. #ifdef CONFIG_MTD_ONENAND_VERIFY_WRITE
  1027. /**
  1028. * onenand_verify_oob - [GENERIC] verify the oob contents after a write
  1029. * @param mtd MTD device structure
  1030. * @param buf the databuffer to verify
  1031. * @param to offset to read from
  1032. */
  1033. static int onenand_verify_oob(struct mtd_info *mtd, const u_char *buf, loff_t to)
  1034. {
  1035. struct onenand_chip *this = mtd->priv;
  1036. char oobbuf[64];
  1037. int status, i;
  1038. this->command(mtd, ONENAND_CMD_READOOB, to, mtd->oobsize);
  1039. onenand_update_bufferram(mtd, to, 0);
  1040. status = this->wait(mtd, FL_READING);
  1041. if (status)
  1042. return status;
  1043. this->read_bufferram(mtd, ONENAND_SPARERAM, oobbuf, 0, mtd->oobsize);
  1044. for (i = 0; i < mtd->oobsize; i++)
  1045. if (buf[i] != 0xFF && buf[i] != oobbuf[i])
  1046. return -EBADMSG;
  1047. return 0;
  1048. }
  1049. /**
  1050. * onenand_verify - [GENERIC] verify the chip contents after a write
  1051. * @param mtd MTD device structure
  1052. * @param buf the databuffer to verify
  1053. * @param addr offset to read from
  1054. * @param len number of bytes to read and compare
  1055. */
  1056. static int onenand_verify(struct mtd_info *mtd, const u_char *buf, loff_t addr, size_t len)
  1057. {
  1058. struct onenand_chip *this = mtd->priv;
  1059. void __iomem *dataram;
  1060. int ret = 0;
  1061. int thislen, column;
  1062. while (len != 0) {
  1063. thislen = min_t(int, this->writesize, len);
  1064. column = addr & (this->writesize - 1);
  1065. if (column + thislen > this->writesize)
  1066. thislen = this->writesize - column;
  1067. this->command(mtd, ONENAND_CMD_READ, addr, this->writesize);
  1068. onenand_update_bufferram(mtd, addr, 0);
  1069. ret = this->wait(mtd, FL_READING);
  1070. if (ret)
  1071. return ret;
  1072. onenand_update_bufferram(mtd, addr, 1);
  1073. dataram = this->base + ONENAND_DATARAM;
  1074. dataram += onenand_bufferram_offset(mtd, ONENAND_DATARAM);
  1075. if (memcmp(buf, dataram + column, thislen))
  1076. return -EBADMSG;
  1077. len -= thislen;
  1078. buf += thislen;
  1079. addr += thislen;
  1080. }
  1081. return 0;
  1082. }
  1083. #else
  1084. #define onenand_verify(...) (0)
  1085. #define onenand_verify_oob(...) (0)
  1086. #endif
  1087. #define NOTALIGNED(x) ((x & (this->subpagesize - 1)) != 0)
  1088. /**
  1089. * onenand_fill_auto_oob - [Internal] oob auto-placement transfer
  1090. * @param mtd MTD device structure
  1091. * @param oob_buf oob buffer
  1092. * @param buf source address
  1093. * @param column oob offset to write to
  1094. * @param thislen oob length to write
  1095. */
  1096. static int onenand_fill_auto_oob(struct mtd_info *mtd, u_char *oob_buf,
  1097. const u_char *buf, int column, int thislen)
  1098. {
  1099. struct onenand_chip *this = mtd->priv;
  1100. struct nand_oobfree *free;
  1101. int writecol = column;
  1102. int writeend = column + thislen;
  1103. int lastgap = 0;
  1104. unsigned int i;
  1105. free = this->ecclayout->oobfree;
  1106. for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES && free->length; i++, free++) {
  1107. if (writecol >= lastgap)
  1108. writecol += free->offset - lastgap;
  1109. if (writeend >= lastgap)
  1110. writeend += free->offset - lastgap;
  1111. lastgap = free->offset + free->length;
  1112. }
  1113. free = this->ecclayout->oobfree;
  1114. for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES && free->length; i++, free++) {
  1115. int free_end = free->offset + free->length;
  1116. if (free->offset < writeend && free_end > writecol) {
  1117. int st = max_t(int,free->offset,writecol);
  1118. int ed = min_t(int,free_end,writeend);
  1119. int n = ed - st;
  1120. memcpy(oob_buf + st, buf, n);
  1121. buf += n;
  1122. } else if (column == 0)
  1123. break;
  1124. }
  1125. return 0;
  1126. }
  1127. /**
  1128. * onenand_write_ops_nolock - [OneNAND Interface] write main and/or out-of-band
  1129. * @param mtd MTD device structure
  1130. * @param to offset to write to
  1131. * @param ops oob operation description structure
  1132. *
  1133. * Write main and/or oob with ECC
  1134. */
  1135. static int onenand_write_ops_nolock(struct mtd_info *mtd, loff_t to,
  1136. struct mtd_oob_ops *ops)
  1137. {
  1138. struct onenand_chip *this = mtd->priv;
  1139. int written = 0, column, thislen, subpage;
  1140. int oobwritten = 0, oobcolumn, thisooblen, oobsize;
  1141. size_t len = ops->len;
  1142. size_t ooblen = ops->ooblen;
  1143. const u_char *buf = ops->datbuf;
  1144. const u_char *oob = ops->oobbuf;
  1145. u_char *oobbuf;
  1146. int ret = 0;
  1147. DEBUG(MTD_DEBUG_LEVEL3, "onenand_write_ops_nolock: to = 0x%08x, len = %i\n", (unsigned int) to, (int) len);
  1148. /* Initialize retlen, in case of early exit */
  1149. ops->retlen = 0;
  1150. ops->oobretlen = 0;
  1151. /* Do not allow writes past end of device */
  1152. if (unlikely((to + len) > mtd->size)) {
  1153. printk(KERN_ERR "onenand_write_ops_nolock: Attempt write to past end of device\n");
  1154. return -EINVAL;
  1155. }
  1156. /* Reject writes, which are not page aligned */
  1157. if (unlikely(NOTALIGNED(to)) || unlikely(NOTALIGNED(len))) {
  1158. printk(KERN_ERR "onenand_write_ops_nolock: Attempt to write not page aligned data\n");
  1159. return -EINVAL;
  1160. }
  1161. if (ops->mode == MTD_OOB_AUTO)
  1162. oobsize = this->ecclayout->oobavail;
  1163. else
  1164. oobsize = mtd->oobsize;
  1165. oobcolumn = to & (mtd->oobsize - 1);
  1166. column = to & (mtd->writesize - 1);
  1167. /* Loop until all data write */
  1168. while (written < len) {
  1169. u_char *wbuf = (u_char *) buf;
  1170. thislen = min_t(int, mtd->writesize - column, len - written);
  1171. thisooblen = min_t(int, oobsize - oobcolumn, ooblen - oobwritten);
  1172. cond_resched();
  1173. this->command(mtd, ONENAND_CMD_BUFFERRAM, to, thislen);
  1174. /* Partial page write */
  1175. subpage = thislen < mtd->writesize;
  1176. if (subpage) {
  1177. memset(this->page_buf, 0xff, mtd->writesize);
  1178. memcpy(this->page_buf + column, buf, thislen);
  1179. wbuf = this->page_buf;
  1180. }
  1181. this->write_bufferram(mtd, ONENAND_DATARAM, wbuf, 0, mtd->writesize);
  1182. if (oob) {
  1183. oobbuf = this->oob_buf;
  1184. /* We send data to spare ram with oobsize
  1185. * to prevent byte access */
  1186. memset(oobbuf, 0xff, mtd->oobsize);
  1187. if (ops->mode == MTD_OOB_AUTO)
  1188. onenand_fill_auto_oob(mtd, oobbuf, oob, oobcolumn, thisooblen);
  1189. else
  1190. memcpy(oobbuf + oobcolumn, oob, thisooblen);
  1191. oobwritten += thisooblen;
  1192. oob += thisooblen;
  1193. oobcolumn = 0;
  1194. } else
  1195. oobbuf = (u_char *) ffchars;
  1196. this->write_bufferram(mtd, ONENAND_SPARERAM, oobbuf, 0, mtd->oobsize);
  1197. this->command(mtd, ONENAND_CMD_PROG, to, mtd->writesize);
  1198. ret = this->wait(mtd, FL_WRITING);
  1199. /* In partial page write we don't update bufferram */
  1200. onenand_update_bufferram(mtd, to, !ret && !subpage);
  1201. if (ONENAND_IS_2PLANE(this)) {
  1202. ONENAND_SET_BUFFERRAM1(this);
  1203. onenand_update_bufferram(mtd, to + this->writesize, !ret && !subpage);
  1204. }
  1205. if (ret) {
  1206. printk(KERN_ERR "onenand_write_ops_nolock: write filaed %d\n", ret);
  1207. break;
  1208. }
  1209. /* Only check verify write turn on */
  1210. ret = onenand_verify(mtd, (u_char *) wbuf, to, thislen);
  1211. if (ret) {
  1212. printk(KERN_ERR "onenand_write_ops_nolock: verify failed %d\n", ret);
  1213. break;
  1214. }
  1215. written += thislen;
  1216. if (written == len)
  1217. break;
  1218. column = 0;
  1219. to += thislen;
  1220. buf += thislen;
  1221. }
  1222. ops->retlen = written;
  1223. return ret;
  1224. }
  1225. /**
  1226. * onenand_write_oob_nolock - [Internal] OneNAND write out-of-band
  1227. * @param mtd MTD device structure
  1228. * @param to offset to write to
  1229. * @param len number of bytes to write
  1230. * @param retlen pointer to variable to store the number of written bytes
  1231. * @param buf the data to write
  1232. * @param mode operation mode
  1233. *
  1234. * OneNAND write out-of-band
  1235. */
  1236. static int onenand_write_oob_nolock(struct mtd_info *mtd, loff_t to,
  1237. struct mtd_oob_ops *ops)
  1238. {
  1239. struct onenand_chip *this = mtd->priv;
  1240. int column, ret = 0, oobsize;
  1241. int written = 0;
  1242. u_char *oobbuf;
  1243. size_t len = ops->ooblen;
  1244. const u_char *buf = ops->oobbuf;
  1245. mtd_oob_mode_t mode = ops->mode;
  1246. to += ops->ooboffs;
  1247. DEBUG(MTD_DEBUG_LEVEL3, "onenand_write_oob_nolock: to = 0x%08x, len = %i\n", (unsigned int) to, (int) len);
  1248. /* Initialize retlen, in case of early exit */
  1249. ops->oobretlen = 0;
  1250. if (mode == MTD_OOB_AUTO)
  1251. oobsize = this->ecclayout->oobavail;
  1252. else
  1253. oobsize = mtd->oobsize;
  1254. column = to & (mtd->oobsize - 1);
  1255. if (unlikely(column >= oobsize)) {
  1256. printk(KERN_ERR "onenand_write_oob_nolock: Attempted to start write outside oob\n");
  1257. return -EINVAL;
  1258. }
  1259. /* For compatibility with NAND: Do not allow write past end of page */
  1260. if (unlikely(column + len > oobsize)) {
  1261. printk(KERN_ERR "onenand_write_oob_nolock: "
  1262. "Attempt to write past end of page\n");
  1263. return -EINVAL;
  1264. }
  1265. /* Do not allow reads past end of device */
  1266. if (unlikely(to >= mtd->size ||
  1267. column + len > ((mtd->size >> this->page_shift) -
  1268. (to >> this->page_shift)) * oobsize)) {
  1269. printk(KERN_ERR "onenand_write_oob_nolock: Attempted to write past end of device\n");
  1270. return -EINVAL;
  1271. }
  1272. oobbuf = this->oob_buf;
  1273. /* Loop until all data write */
  1274. while (written < len) {
  1275. int thislen = min_t(int, oobsize, len - written);
  1276. cond_resched();
  1277. this->command(mtd, ONENAND_CMD_BUFFERRAM, to, mtd->oobsize);
  1278. /* We send data to spare ram with oobsize
  1279. * to prevent byte access */
  1280. memset(oobbuf, 0xff, mtd->oobsize);
  1281. if (mode == MTD_OOB_AUTO)
  1282. onenand_fill_auto_oob(mtd, oobbuf, buf, column, thislen);
  1283. else
  1284. memcpy(oobbuf + column, buf, thislen);
  1285. this->write_bufferram(mtd, ONENAND_SPARERAM, oobbuf, 0, mtd->oobsize);
  1286. this->command(mtd, ONENAND_CMD_PROGOOB, to, mtd->oobsize);
  1287. onenand_update_bufferram(mtd, to, 0);
  1288. if (ONENAND_IS_2PLANE(this)) {
  1289. ONENAND_SET_BUFFERRAM1(this);
  1290. onenand_update_bufferram(mtd, to + this->writesize, 0);
  1291. }
  1292. ret = this->wait(mtd, FL_WRITING);
  1293. if (ret) {
  1294. printk(KERN_ERR "onenand_write_oob_nolock: write failed %d\n", ret);
  1295. break;
  1296. }
  1297. ret = onenand_verify_oob(mtd, oobbuf, to);
  1298. if (ret) {
  1299. printk(KERN_ERR "onenand_write_oob_nolock: verify failed %d\n", ret);
  1300. break;
  1301. }
  1302. written += thislen;
  1303. if (written == len)
  1304. break;
  1305. to += mtd->writesize;
  1306. buf += thislen;
  1307. column = 0;
  1308. }
  1309. ops->oobretlen = written;
  1310. return ret;
  1311. }
  1312. /**
  1313. * onenand_write - [MTD Interface] write buffer to FLASH
  1314. * @param mtd MTD device structure
  1315. * @param to offset to write to
  1316. * @param len number of bytes to write
  1317. * @param retlen pointer to variable to store the number of written bytes
  1318. * @param buf the data to write
  1319. *
  1320. * Write with ECC
  1321. */
  1322. static int onenand_write(struct mtd_info *mtd, loff_t to, size_t len,
  1323. size_t *retlen, const u_char *buf)
  1324. {
  1325. struct mtd_oob_ops ops = {
  1326. .len = len,
  1327. .ooblen = 0,
  1328. .datbuf = (u_char *) buf,
  1329. .oobbuf = NULL,
  1330. };
  1331. int ret;
  1332. onenand_get_device(mtd, FL_WRITING);
  1333. ret = onenand_write_ops_nolock(mtd, to, &ops);
  1334. onenand_release_device(mtd);
  1335. *retlen = ops.retlen;
  1336. return ret;
  1337. }
  1338. /**
  1339. * onenand_write_oob - [MTD Interface] NAND write data and/or out-of-band
  1340. * @param mtd: MTD device structure
  1341. * @param to: offset to write
  1342. * @param ops: oob operation description structure
  1343. */
  1344. static int onenand_write_oob(struct mtd_info *mtd, loff_t to,
  1345. struct mtd_oob_ops *ops)
  1346. {
  1347. int ret;
  1348. switch (ops->mode) {
  1349. case MTD_OOB_PLACE:
  1350. case MTD_OOB_AUTO:
  1351. break;
  1352. case MTD_OOB_RAW:
  1353. /* Not implemented yet */
  1354. default:
  1355. return -EINVAL;
  1356. }
  1357. onenand_get_device(mtd, FL_WRITING);
  1358. if (ops->datbuf)
  1359. ret = onenand_write_ops_nolock(mtd, to, ops);
  1360. else
  1361. ret = onenand_write_oob_nolock(mtd, to, ops);
  1362. onenand_release_device(mtd);
  1363. return ret;
  1364. }
  1365. /**
  1366. * onenand_block_isbad_nolock - [GENERIC] Check if a block is marked bad
  1367. * @param mtd MTD device structure
  1368. * @param ofs offset from device start
  1369. * @param allowbbt 1, if its allowed to access the bbt area
  1370. *
  1371. * Check, if the block is bad. Either by reading the bad block table or
  1372. * calling of the scan function.
  1373. */
  1374. static int onenand_block_isbad_nolock(struct mtd_info *mtd, loff_t ofs, int allowbbt)
  1375. {
  1376. struct onenand_chip *this = mtd->priv;
  1377. struct bbm_info *bbm = this->bbm;
  1378. /* Return info from the table */
  1379. return bbm->isbad_bbt(mtd, ofs, allowbbt);
  1380. }
  1381. /**
  1382. * onenand_erase - [MTD Interface] erase block(s)
  1383. * @param mtd MTD device structure
  1384. * @param instr erase instruction
  1385. *
  1386. * Erase one ore more blocks
  1387. */
  1388. static int onenand_erase(struct mtd_info *mtd, struct erase_info *instr)
  1389. {
  1390. struct onenand_chip *this = mtd->priv;
  1391. unsigned int block_size;
  1392. loff_t addr;
  1393. int len;
  1394. int ret = 0;
  1395. DEBUG(MTD_DEBUG_LEVEL3, "onenand_erase: start = 0x%08x, len = %i\n", (unsigned int) instr->addr, (unsigned int) instr->len);
  1396. block_size = (1 << this->erase_shift);
  1397. /* Start address must align on block boundary */
  1398. if (unlikely(instr->addr & (block_size - 1))) {
  1399. printk(KERN_ERR "onenand_erase: Unaligned address\n");
  1400. return -EINVAL;
  1401. }
  1402. /* Length must align on block boundary */
  1403. if (unlikely(instr->len & (block_size - 1))) {
  1404. printk(KERN_ERR "onenand_erase: Length not block aligned\n");
  1405. return -EINVAL;
  1406. }
  1407. /* Do not allow erase past end of device */
  1408. if (unlikely((instr->len + instr->addr) > mtd->size)) {
  1409. printk(KERN_ERR "onenand_erase: Erase past end of device\n");
  1410. return -EINVAL;
  1411. }
  1412. instr->fail_addr = 0xffffffff;
  1413. /* Grab the lock and see if the device is available */
  1414. onenand_get_device(mtd, FL_ERASING);
  1415. /* Loop throught the pages */
  1416. len = instr->len;
  1417. addr = instr->addr;
  1418. instr->state = MTD_ERASING;
  1419. while (len) {
  1420. cond_resched();
  1421. /* Check if we have a bad block, we do not erase bad blocks */
  1422. if (onenand_block_isbad_nolock(mtd, addr, 0)) {
  1423. printk (KERN_WARNING "onenand_erase: attempt to erase a bad block at addr 0x%08x\n", (unsigned int) addr);
  1424. instr->state = MTD_ERASE_FAILED;
  1425. goto erase_exit;
  1426. }
  1427. this->command(mtd, ONENAND_CMD_ERASE, addr, block_size);
  1428. onenand_invalidate_bufferram(mtd, addr, block_size);
  1429. ret = this->wait(mtd, FL_ERASING);
  1430. /* Check, if it is write protected */
  1431. if (ret) {
  1432. printk(KERN_ERR "onenand_erase: Failed erase, block %d\n", (unsigned) (addr >> this->erase_shift));
  1433. instr->state = MTD_ERASE_FAILED;
  1434. instr->fail_addr = addr;
  1435. goto erase_exit;
  1436. }
  1437. len -= block_size;
  1438. addr += block_size;
  1439. }
  1440. instr->state = MTD_ERASE_DONE;
  1441. erase_exit:
  1442. ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
  1443. /* Deselect and wake up anyone waiting on the device */
  1444. onenand_release_device(mtd);
  1445. /* Do call back function */
  1446. if (!ret)
  1447. mtd_erase_callback(instr);
  1448. return ret;
  1449. }
  1450. /**
  1451. * onenand_sync - [MTD Interface] sync
  1452. * @param mtd MTD device structure
  1453. *
  1454. * Sync is actually a wait for chip ready function
  1455. */
  1456. static void onenand_sync(struct mtd_info *mtd)
  1457. {
  1458. DEBUG(MTD_DEBUG_LEVEL3, "onenand_sync: called\n");
  1459. /* Grab the lock and see if the device is available */
  1460. onenand_get_device(mtd, FL_SYNCING);
  1461. /* Release it and go back */
  1462. onenand_release_device(mtd);
  1463. }
  1464. /**
  1465. * onenand_block_isbad - [MTD Interface] Check whether the block at the given offset is bad
  1466. * @param mtd MTD device structure
  1467. * @param ofs offset relative to mtd start
  1468. *
  1469. * Check whether the block is bad
  1470. */
  1471. static int onenand_block_isbad(struct mtd_info *mtd, loff_t ofs)
  1472. {
  1473. int ret;
  1474. /* Check for invalid offset */
  1475. if (ofs > mtd->size)
  1476. return -EINVAL;
  1477. onenand_get_device(mtd, FL_READING);
  1478. ret = onenand_block_isbad_nolock(mtd, ofs, 0);
  1479. onenand_release_device(mtd);
  1480. return ret;
  1481. }
  1482. /**
  1483. * onenand_default_block_markbad - [DEFAULT] mark a block bad
  1484. * @param mtd MTD device structure
  1485. * @param ofs offset from device start
  1486. *
  1487. * This is the default implementation, which can be overridden by
  1488. * a hardware specific driver.
  1489. */
  1490. static int onenand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
  1491. {
  1492. struct onenand_chip *this = mtd->priv;
  1493. struct bbm_info *bbm = this->bbm;
  1494. u_char buf[2] = {0, 0};
  1495. struct mtd_oob_ops ops = {
  1496. .mode = MTD_OOB_PLACE,
  1497. .ooblen = 2,
  1498. .oobbuf = buf,
  1499. .ooboffs = 0,
  1500. };
  1501. int block;
  1502. /* Get block number */
  1503. block = ((int) ofs) >> bbm->bbt_erase_shift;
  1504. if (bbm->bbt)
  1505. bbm->bbt[block >> 2] |= 0x01 << ((block & 0x03) << 1);
  1506. /* We write two bytes, so we dont have to mess with 16 bit access */
  1507. ofs += mtd->oobsize + (bbm->badblockpos & ~0x01);
  1508. return onenand_write_oob_nolock(mtd, ofs, &ops);
  1509. }
  1510. /**
  1511. * onenand_block_markbad - [MTD Interface] Mark the block at the given offset as bad
  1512. * @param mtd MTD device structure
  1513. * @param ofs offset relative to mtd start
  1514. *
  1515. * Mark the block as bad
  1516. */
  1517. static int onenand_block_markbad(struct mtd_info *mtd, loff_t ofs)
  1518. {
  1519. struct onenand_chip *this = mtd->priv;
  1520. int ret;
  1521. ret = onenand_block_isbad(mtd, ofs);
  1522. if (ret) {
  1523. /* If it was bad already, return success and do nothing */
  1524. if (ret > 0)
  1525. return 0;
  1526. return ret;
  1527. }
  1528. onenand_get_device(mtd, FL_WRITING);
  1529. ret = this->block_markbad(mtd, ofs);
  1530. onenand_release_device(mtd);
  1531. return ret;
  1532. }
  1533. /**
  1534. * onenand_do_lock_cmd - [OneNAND Interface] Lock or unlock block(s)
  1535. * @param mtd MTD device structure
  1536. * @param ofs offset relative to mtd start
  1537. * @param len number of bytes to lock or unlock
  1538. * @param cmd lock or unlock command
  1539. *
  1540. * Lock or unlock one or more blocks
  1541. */
  1542. static int onenand_do_lock_cmd(struct mtd_info *mtd, loff_t ofs, size_t len, int cmd)
  1543. {
  1544. struct onenand_chip *this = mtd->priv;
  1545. int start, end, block, value, status;
  1546. int wp_status_mask;
  1547. start = ofs >> this->erase_shift;
  1548. end = len >> this->erase_shift;
  1549. if (cmd == ONENAND_CMD_LOCK)
  1550. wp_status_mask = ONENAND_WP_LS;
  1551. else
  1552. wp_status_mask = ONENAND_WP_US;
  1553. /* Continuous lock scheme */
  1554. if (this->options & ONENAND_HAS_CONT_LOCK) {
  1555. /* Set start block address */
  1556. this->write_word(start, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
  1557. /* Set end block address */
  1558. this->write_word(start + end - 1, this->base + ONENAND_REG_END_BLOCK_ADDRESS);
  1559. /* Write lock command */
  1560. this->command(mtd, cmd, 0, 0);
  1561. /* There's no return value */
  1562. this->wait(mtd, FL_LOCKING);
  1563. /* Sanity check */
  1564. while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
  1565. & ONENAND_CTRL_ONGO)
  1566. continue;
  1567. /* Check lock status */
  1568. status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
  1569. if (!(status & wp_status_mask))
  1570. printk(KERN_ERR "wp status = 0x%x\n", status);
  1571. return 0;
  1572. }
  1573. /* Block lock scheme */
  1574. for (block = start; block < start + end; block++) {
  1575. /* Set block address */
  1576. value = onenand_block_address(this, block);
  1577. this->write_word(value, this->base + ONENAND_REG_START_ADDRESS1);
  1578. /* Select DataRAM for DDP */
  1579. value = onenand_bufferram_address(this, block);
  1580. this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
  1581. /* Set start block address */
  1582. this->write_word(block, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
  1583. /* Write lock command */
  1584. this->command(mtd, cmd, 0, 0);
  1585. /* There's no return value */
  1586. this->wait(mtd, FL_LOCKING);
  1587. /* Sanity check */
  1588. while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
  1589. & ONENAND_CTRL_ONGO)
  1590. continue;
  1591. /* Check lock status */
  1592. status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
  1593. if (!(status & wp_status_mask))
  1594. printk(KERN_ERR "block = %d, wp status = 0x%x\n", block, status);
  1595. }
  1596. return 0;
  1597. }
  1598. /**
  1599. * onenand_lock - [MTD Interface] Lock block(s)
  1600. * @param mtd MTD device structure
  1601. * @param ofs offset relative to mtd start
  1602. * @param len number of bytes to unlock
  1603. *
  1604. * Lock one or more blocks
  1605. */
  1606. static int onenand_lock(struct mtd_info *mtd, loff_t ofs, size_t len)
  1607. {
  1608. int ret;
  1609. onenand_get_device(mtd, FL_LOCKING);
  1610. ret = onenand_do_lock_cmd(mtd, ofs, len, ONENAND_CMD_LOCK);
  1611. onenand_release_device(mtd);
  1612. return ret;
  1613. }
  1614. /**
  1615. * onenand_unlock - [MTD Interface] Unlock block(s)
  1616. * @param mtd MTD device structure
  1617. * @param ofs offset relative to mtd start
  1618. * @param len number of bytes to unlock
  1619. *
  1620. * Unlock one or more blocks
  1621. */
  1622. static int onenand_unlock(struct mtd_info *mtd, loff_t ofs, size_t len)
  1623. {
  1624. int ret;
  1625. onenand_get_device(mtd, FL_LOCKING);
  1626. ret = onenand_do_lock_cmd(mtd, ofs, len, ONENAND_CMD_UNLOCK);
  1627. onenand_release_device(mtd);
  1628. return ret;
  1629. }
  1630. /**
  1631. * onenand_check_lock_status - [OneNAND Interface] Check lock status
  1632. * @param this onenand chip data structure
  1633. *
  1634. * Check lock status
  1635. */
  1636. static void onenand_check_lock_status(struct onenand_chip *this)
  1637. {
  1638. unsigned int value, block, status;
  1639. unsigned int end;
  1640. end = this->chipsize >> this->erase_shift;
  1641. for (block = 0; block < end; block++) {
  1642. /* Set block address */
  1643. value = onenand_block_address(this, block);
  1644. this->write_word(value, this->base + ONENAND_REG_START_ADDRESS1);
  1645. /* Select DataRAM for DDP */
  1646. value = onenand_bufferram_address(this, block);
  1647. this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
  1648. /* Set start block address */
  1649. this->write_word(block, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
  1650. /* Check lock status */
  1651. status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
  1652. if (!(status & ONENAND_WP_US))
  1653. printk(KERN_ERR "block = %d, wp status = 0x%x\n", block, status);
  1654. }
  1655. }
  1656. /**
  1657. * onenand_unlock_all - [OneNAND Interface] unlock all blocks
  1658. * @param mtd MTD device structure
  1659. *
  1660. * Unlock all blocks
  1661. */
  1662. static int onenand_unlock_all(struct mtd_info *mtd)
  1663. {
  1664. struct onenand_chip *this = mtd->priv;
  1665. if (this->options & ONENAND_HAS_UNLOCK_ALL) {
  1666. /* Set start block address */
  1667. this->write_word(0, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
  1668. /* Write unlock command */
  1669. this->command(mtd, ONENAND_CMD_UNLOCK_ALL, 0, 0);
  1670. /* There's no return value */
  1671. this->wait(mtd, FL_LOCKING);
  1672. /* Sanity check */
  1673. while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
  1674. & ONENAND_CTRL_ONGO)
  1675. continue;
  1676. /* Workaround for all block unlock in DDP */
  1677. if (ONENAND_IS_DDP(this)) {
  1678. /* 1st block on another chip */
  1679. loff_t ofs = this->chipsize >> 1;
  1680. size_t len = mtd->erasesize;
  1681. onenand_do_lock_cmd(mtd, ofs, len, ONENAND_CMD_UNLOCK);
  1682. }
  1683. onenand_check_lock_status(this);
  1684. return 0;
  1685. }
  1686. onenand_do_lock_cmd(mtd, 0x0, this->chipsize, ONENAND_CMD_UNLOCK);
  1687. return 0;
  1688. }
  1689. #ifdef CONFIG_MTD_ONENAND_OTP
  1690. /* Interal OTP operation */
  1691. typedef int (*otp_op_t)(struct mtd_info *mtd, loff_t form, size_t len,
  1692. size_t *retlen, u_char *buf);
  1693. /**
  1694. * do_otp_read - [DEFAULT] Read OTP block area
  1695. * @param mtd MTD device structure
  1696. * @param from The offset to read
  1697. * @param len number of bytes to read
  1698. * @param retlen pointer to variable to store the number of readbytes
  1699. * @param buf the databuffer to put/get data
  1700. *
  1701. * Read OTP block area.
  1702. */
  1703. static int do_otp_read(struct mtd_info *mtd, loff_t from, size_t len,
  1704. size_t *retlen, u_char *buf)
  1705. {
  1706. struct onenand_chip *this = mtd->priv;
  1707. struct mtd_oob_ops ops = {
  1708. .len = len,
  1709. .ooblen = 0,
  1710. .datbuf = buf,
  1711. .oobbuf = NULL,
  1712. };
  1713. int ret;
  1714. /* Enter OTP access mode */
  1715. this->command(mtd, ONENAND_CMD_OTP_ACCESS, 0, 0);
  1716. this->wait(mtd, FL_OTPING);
  1717. ret = onenand_read_ops_nolock(mtd, from, &ops);
  1718. /* Exit OTP access mode */
  1719. this->command(mtd, ONENAND_CMD_RESET, 0, 0);
  1720. this->wait(mtd, FL_RESETING);
  1721. return ret;
  1722. }
  1723. /**
  1724. * do_otp_write - [DEFAULT] Write OTP block area
  1725. * @param mtd MTD device structure
  1726. * @param to The offset to write
  1727. * @param len number of bytes to write
  1728. * @param retlen pointer to variable to store the number of write bytes
  1729. * @param buf the databuffer to put/get data
  1730. *
  1731. * Write OTP block area.
  1732. */
  1733. static int do_otp_write(struct mtd_info *mtd, loff_t to, size_t len,
  1734. size_t *retlen, u_char *buf)
  1735. {
  1736. struct onenand_chip *this = mtd->priv;
  1737. unsigned char *pbuf = buf;
  1738. int ret;
  1739. struct mtd_oob_ops ops;
  1740. /* Force buffer page aligned */
  1741. if (len < mtd->writesize) {
  1742. memcpy(this->page_buf, buf, len);
  1743. memset(this->page_buf + len, 0xff, mtd->writesize - len);
  1744. pbuf = this->page_buf;
  1745. len = mtd->writesize;
  1746. }
  1747. /* Enter OTP access mode */
  1748. this->command(mtd, ONENAND_CMD_OTP_ACCESS, 0, 0);
  1749. this->wait(mtd, FL_OTPING);
  1750. ops.len = len;
  1751. ops.ooblen = 0;
  1752. ops.datbuf = pbuf;
  1753. ops.oobbuf = NULL;
  1754. ret = onenand_write_ops_nolock(mtd, to, &ops);
  1755. *retlen = ops.retlen;
  1756. /* Exit OTP access mode */
  1757. this->command(mtd, ONENAND_CMD_RESET, 0, 0);
  1758. this->wait(mtd, FL_RESETING);
  1759. return ret;
  1760. }
  1761. /**
  1762. * do_otp_lock - [DEFAULT] Lock OTP block area
  1763. * @param mtd MTD device structure
  1764. * @param from The offset to lock
  1765. * @param len number of bytes to lock
  1766. * @param retlen pointer to variable to store the number of lock bytes
  1767. * @param buf the databuffer to put/get data
  1768. *
  1769. * Lock OTP block area.
  1770. */
  1771. static int do_otp_lock(struct mtd_info *mtd, loff_t from, size_t len,
  1772. size_t *retlen, u_char *buf)
  1773. {
  1774. struct onenand_chip *this = mtd->priv;
  1775. struct mtd_oob_ops ops = {
  1776. .mode = MTD_OOB_PLACE,
  1777. .ooblen = len,
  1778. .oobbuf = buf,
  1779. .ooboffs = 0,
  1780. };
  1781. int ret;
  1782. /* Enter OTP access mode */
  1783. this->command(mtd, ONENAND_CMD_OTP_ACCESS, 0, 0);
  1784. this->wait(mtd, FL_OTPING);
  1785. ret = onenand_write_oob_nolock(mtd, from, &ops);
  1786. *retlen = ops.oobretlen;
  1787. /* Exit OTP access mode */
  1788. this->command(mtd, ONENAND_CMD_RESET, 0, 0);
  1789. this->wait(mtd, FL_RESETING);
  1790. return ret;
  1791. }
  1792. /**
  1793. * onenand_otp_walk - [DEFAULT] Handle OTP operation
  1794. * @param mtd MTD device structure
  1795. * @param from The offset to read/write
  1796. * @param len number of bytes to read/write
  1797. * @param retlen pointer to variable to store the number of read bytes
  1798. * @param buf the databuffer to put/get data
  1799. * @param action do given action
  1800. * @param mode specify user and factory
  1801. *
  1802. * Handle OTP operation.
  1803. */
  1804. static int onenand_otp_walk(struct mtd_info *mtd, loff_t from, size_t len,
  1805. size_t *retlen, u_char *buf,
  1806. otp_op_t action, int mode)
  1807. {
  1808. struct onenand_chip *this = mtd->priv;
  1809. int otp_pages;
  1810. int density;
  1811. int ret = 0;
  1812. *retlen = 0;
  1813. density = this->device_id >> ONENAND_DEVICE_DENSITY_SHIFT;
  1814. if (density < ONENAND_DEVICE_DENSITY_512Mb)
  1815. otp_pages = 20;
  1816. else
  1817. otp_pages = 10;
  1818. if (mode == MTD_OTP_FACTORY) {
  1819. from += mtd->writesize * otp_pages;
  1820. otp_pages = 64 - otp_pages;
  1821. }
  1822. /* Check User/Factory boundary */
  1823. if (((mtd->writesize * otp_pages) - (from + len)) < 0)
  1824. return 0;
  1825. onenand_get_device(mtd, FL_OTPING);
  1826. while (len > 0 && otp_pages > 0) {
  1827. if (!action) { /* OTP Info functions */
  1828. struct otp_info *otpinfo;
  1829. len -= sizeof(struct otp_info);
  1830. if (len <= 0) {
  1831. ret = -ENOSPC;
  1832. break;
  1833. }
  1834. otpinfo = (struct otp_info *) buf;
  1835. otpinfo->start = from;
  1836. otpinfo->length = mtd->writesize;
  1837. otpinfo->locked = 0;
  1838. from += mtd->writesize;
  1839. buf += sizeof(struct otp_info);
  1840. *retlen += sizeof(struct otp_info);
  1841. } else {
  1842. size_t tmp_retlen;
  1843. int size = len;
  1844. ret = action(mtd, from, len, &tmp_retlen, buf);
  1845. buf += size;
  1846. len -= size;
  1847. *retlen += size;
  1848. if (ret)
  1849. break;
  1850. }
  1851. otp_pages--;
  1852. }
  1853. onenand_release_device(mtd);
  1854. return ret;
  1855. }
  1856. /**
  1857. * onenand_get_fact_prot_info - [MTD Interface] Read factory OTP info
  1858. * @param mtd MTD device structure
  1859. * @param buf the databuffer to put/get data
  1860. * @param len number of bytes to read
  1861. *
  1862. * Read factory OTP info.
  1863. */
  1864. static int onenand_get_fact_prot_info(struct mtd_info *mtd,
  1865. struct otp_info *buf, size_t len)
  1866. {
  1867. size_t retlen;
  1868. int ret;
  1869. ret = onenand_otp_walk(mtd, 0, len, &retlen, (u_char *) buf, NULL, MTD_OTP_FACTORY);
  1870. return ret ? : retlen;
  1871. }
  1872. /**
  1873. * onenand_read_fact_prot_reg - [MTD Interface] Read factory OTP area
  1874. * @param mtd MTD device structure
  1875. * @param from The offset to read
  1876. * @param len number of bytes to read
  1877. * @param retlen pointer to variable to store the number of read bytes
  1878. * @param buf the databuffer to put/get data
  1879. *
  1880. * Read factory OTP area.
  1881. */
  1882. static int onenand_read_fact_prot_reg(struct mtd_info *mtd, loff_t from,
  1883. size_t len, size_t *retlen, u_char *buf)
  1884. {
  1885. return onenand_otp_walk(mtd, from, len, retlen, buf, do_otp_read, MTD_OTP_FACTORY);
  1886. }
  1887. /**
  1888. * onenand_get_user_prot_info - [MTD Interface] Read user OTP info
  1889. * @param mtd MTD device structure
  1890. * @param buf the databuffer to put/get data
  1891. * @param len number of bytes to read
  1892. *
  1893. * Read user OTP info.
  1894. */
  1895. static int onenand_get_user_prot_info(struct mtd_info *mtd,
  1896. struct otp_info *buf, size_t len)
  1897. {
  1898. size_t retlen;
  1899. int ret;
  1900. ret = onenand_otp_walk(mtd, 0, len, &retlen, (u_char *) buf, NULL, MTD_OTP_USER);
  1901. return ret ? : retlen;
  1902. }
  1903. /**
  1904. * onenand_read_user_prot_reg - [MTD Interface] Read user OTP area
  1905. * @param mtd MTD device structure
  1906. * @param from The offset to read
  1907. * @param len number of bytes to read
  1908. * @param retlen pointer to variable to store the number of read bytes
  1909. * @param buf the databuffer to put/get data
  1910. *
  1911. * Read user OTP area.
  1912. */
  1913. static int onenand_read_user_prot_reg(struct mtd_info *mtd, loff_t from,
  1914. size_t len, size_t *retlen, u_char *buf)
  1915. {
  1916. return onenand_otp_walk(mtd, from, len, retlen, buf, do_otp_read, MTD_OTP_USER);
  1917. }
  1918. /**
  1919. * onenand_write_user_prot_reg - [MTD Interface] Write user OTP area
  1920. * @param mtd MTD device structure
  1921. * @param from The offset to write
  1922. * @param len number of bytes to write
  1923. * @param retlen pointer to variable to store the number of write bytes
  1924. * @param buf the databuffer to put/get data
  1925. *
  1926. * Write user OTP area.
  1927. */
  1928. static int onenand_write_user_prot_reg(struct mtd_info *mtd, loff_t from,
  1929. size_t len, size_t *retlen, u_char *buf)
  1930. {
  1931. return onenand_otp_walk(mtd, from, len, retlen, buf, do_otp_write, MTD_OTP_USER);
  1932. }
  1933. /**
  1934. * onenand_lock_user_prot_reg - [MTD Interface] Lock user OTP area
  1935. * @param mtd MTD device structure
  1936. * @param from The offset to lock
  1937. * @param len number of bytes to unlock
  1938. *
  1939. * Write lock mark on spare area in page 0 in OTP block
  1940. */
  1941. static int onenand_lock_user_prot_reg(struct mtd_info *mtd, loff_t from,
  1942. size_t len)
  1943. {
  1944. unsigned char oob_buf[64];
  1945. size_t retlen;
  1946. int ret;
  1947. memset(oob_buf, 0xff, mtd->oobsize);
  1948. /*
  1949. * Note: OTP lock operation
  1950. * OTP block : 0xXXFC
  1951. * 1st block : 0xXXF3 (If chip support)
  1952. * Both : 0xXXF0 (If chip support)
  1953. */
  1954. oob_buf[ONENAND_OTP_LOCK_OFFSET] = 0xFC;
  1955. /*
  1956. * Write lock mark to 8th word of sector0 of page0 of the spare0.
  1957. * We write 16 bytes spare area instead of 2 bytes.
  1958. */
  1959. from = 0;
  1960. len = 16;
  1961. ret = onenand_otp_walk(mtd, from, len, &retlen, oob_buf, do_otp_lock, MTD_OTP_USER);
  1962. return ret ? : retlen;
  1963. }
  1964. #endif /* CONFIG_MTD_ONENAND_OTP */
  1965. /**
  1966. * onenand_check_features - Check and set OneNAND features
  1967. * @param mtd MTD data structure
  1968. *
  1969. * Check and set OneNAND features
  1970. * - lock scheme
  1971. * - two plane
  1972. */
  1973. static void onenand_check_features(struct mtd_info *mtd)
  1974. {
  1975. struct onenand_chip *this = mtd->priv;
  1976. unsigned int density, process;
  1977. /* Lock scheme depends on density and process */
  1978. density = this->device_id >> ONENAND_DEVICE_DENSITY_SHIFT;
  1979. process = this->version_id >> ONENAND_VERSION_PROCESS_SHIFT;
  1980. /* Lock scheme */
  1981. switch (density) {
  1982. case ONENAND_DEVICE_DENSITY_4Gb:
  1983. this->options |= ONENAND_HAS_2PLANE;
  1984. case ONENAND_DEVICE_DENSITY_2Gb:
  1985. /* 2Gb DDP don't have 2 plane */
  1986. if (!ONENAND_IS_DDP(this))
  1987. this->options |= ONENAND_HAS_2PLANE;
  1988. this->options |= ONENAND_HAS_UNLOCK_ALL;
  1989. case ONENAND_DEVICE_DENSITY_1Gb:
  1990. /* A-Die has all block unlock */
  1991. if (process)
  1992. this->options |= ONENAND_HAS_UNLOCK_ALL;
  1993. break;
  1994. default:
  1995. /* Some OneNAND has continuous lock scheme */
  1996. if (!process)
  1997. this->options |= ONENAND_HAS_CONT_LOCK;
  1998. break;
  1999. }
  2000. if (this->options & ONENAND_HAS_CONT_LOCK)
  2001. printk(KERN_DEBUG "Lock scheme is Continuous Lock\n");
  2002. if (this->options & ONENAND_HAS_UNLOCK_ALL)
  2003. printk(KERN_DEBUG "Chip support all block unlock\n");
  2004. if (this->options & ONENAND_HAS_2PLANE)
  2005. printk(KERN_DEBUG "Chip has 2 plane\n");
  2006. }
  2007. /**
  2008. * onenand_print_device_info - Print device & version ID
  2009. * @param device device ID
  2010. * @param version version ID
  2011. *
  2012. * Print device & version ID
  2013. */
  2014. static void onenand_print_device_info(int device, int version)
  2015. {
  2016. int vcc, demuxed, ddp, density;
  2017. vcc = device & ONENAND_DEVICE_VCC_MASK;
  2018. demuxed = device & ONENAND_DEVICE_IS_DEMUX;
  2019. ddp = device & ONENAND_DEVICE_IS_DDP;
  2020. density = device >> ONENAND_DEVICE_DENSITY_SHIFT;
  2021. printk(KERN_INFO "%sOneNAND%s %dMB %sV 16-bit (0x%02x)\n",
  2022. demuxed ? "" : "Muxed ",
  2023. ddp ? "(DDP)" : "",
  2024. (16 << density),
  2025. vcc ? "2.65/3.3" : "1.8",
  2026. device);
  2027. printk(KERN_INFO "OneNAND version = 0x%04x\n", version);
  2028. }
  2029. static const struct onenand_manufacturers onenand_manuf_ids[] = {
  2030. {ONENAND_MFR_SAMSUNG, "Samsung"},
  2031. };
  2032. /**
  2033. * onenand_check_maf - Check manufacturer ID
  2034. * @param manuf manufacturer ID
  2035. *
  2036. * Check manufacturer ID
  2037. */
  2038. static int onenand_check_maf(int manuf)
  2039. {
  2040. int size = ARRAY_SIZE(onenand_manuf_ids);
  2041. char *name;
  2042. int i;
  2043. for (i = 0; i < size; i++)
  2044. if (manuf == onenand_manuf_ids[i].id)
  2045. break;
  2046. if (i < size)
  2047. name = onenand_manuf_ids[i].name;
  2048. else
  2049. name = "Unknown";
  2050. printk(KERN_DEBUG "OneNAND Manufacturer: %s (0x%0x)\n", name, manuf);
  2051. return (i == size);
  2052. }
  2053. /**
  2054. * onenand_probe - [OneNAND Interface] Probe the OneNAND device
  2055. * @param mtd MTD device structure
  2056. *
  2057. * OneNAND detection method:
  2058. * Compare the values from command with ones from register
  2059. */
  2060. static int onenand_probe(struct mtd_info *mtd)
  2061. {
  2062. struct onenand_chip *this = mtd->priv;
  2063. int bram_maf_id, bram_dev_id, maf_id, dev_id, ver_id;
  2064. int density;
  2065. int syscfg;
  2066. /* Save system configuration 1 */
  2067. syscfg = this->read_word(this->base + ONENAND_REG_SYS_CFG1);
  2068. /* Clear Sync. Burst Read mode to read BootRAM */
  2069. this->write_word((syscfg & ~ONENAND_SYS_CFG1_SYNC_READ), this->base + ONENAND_REG_SYS_CFG1);
  2070. /* Send the command for reading device ID from BootRAM */
  2071. this->write_word(ONENAND_CMD_READID, this->base + ONENAND_BOOTRAM);
  2072. /* Read manufacturer and device IDs from BootRAM */
  2073. bram_maf_id = this->read_word(this->base + ONENAND_BOOTRAM + 0x0);
  2074. bram_dev_id = this->read_word(this->base + ONENAND_BOOTRAM + 0x2);
  2075. /* Reset OneNAND to read default register values */
  2076. this->write_word(ONENAND_CMD_RESET, this->base + ONENAND_BOOTRAM);
  2077. /* Wait reset */
  2078. this->wait(mtd, FL_RESETING);
  2079. /* Restore system configuration 1 */
  2080. this->write_word(syscfg, this->base + ONENAND_REG_SYS_CFG1);
  2081. /* Check manufacturer ID */
  2082. if (onenand_check_maf(bram_maf_id))
  2083. return -ENXIO;
  2084. /* Read manufacturer and device IDs from Register */
  2085. maf_id = this->read_word(this->base + ONENAND_REG_MANUFACTURER_ID);
  2086. dev_id = this->read_word(this->base + ONENAND_REG_DEVICE_ID);
  2087. ver_id = this->read_word(this->base + ONENAND_REG_VERSION_ID);
  2088. /* Check OneNAND device */
  2089. if (maf_id != bram_maf_id || dev_id != bram_dev_id)
  2090. return -ENXIO;
  2091. /* Flash device information */
  2092. onenand_print_device_info(dev_id, ver_id);
  2093. this->device_id = dev_id;
  2094. this->version_id = ver_id;
  2095. density = dev_id >> ONENAND_DEVICE_DENSITY_SHIFT;
  2096. this->chipsize = (16 << density) << 20;
  2097. /* Set density mask. it is used for DDP */
  2098. if (ONENAND_IS_DDP(this))
  2099. this->density_mask = (1 << (density + 6));
  2100. else
  2101. this->density_mask = 0;
  2102. /* OneNAND page size & block size */
  2103. /* The data buffer size is equal to page size */
  2104. mtd->writesize = this->read_word(this->base + ONENAND_REG_DATA_BUFFER_SIZE);
  2105. mtd->oobsize = mtd->writesize >> 5;
  2106. /* Pages per a block are always 64 in OneNAND */
  2107. mtd->erasesize = mtd->writesize << 6;
  2108. this->erase_shift = ffs(mtd->erasesize) - 1;
  2109. this->page_shift = ffs(mtd->writesize) - 1;
  2110. this->page_mask = (1 << (this->erase_shift - this->page_shift)) - 1;
  2111. /* It's real page size */
  2112. this->writesize = mtd->writesize;
  2113. /* REVIST: Multichip handling */
  2114. mtd->size = this->chipsize;
  2115. /* Check OneNAND features */
  2116. onenand_check_features(mtd);
  2117. /*
  2118. * We emulate the 4KiB page and 256KiB erase block size
  2119. * But oobsize is still 64 bytes.
  2120. * It is only valid if you turn on 2X program support,
  2121. * Otherwise it will be ignored by compiler.
  2122. */
  2123. if (ONENAND_IS_2PLANE(this)) {
  2124. mtd->writesize <<= 1;
  2125. mtd->erasesize <<= 1;
  2126. }
  2127. return 0;
  2128. }
  2129. /**
  2130. * onenand_suspend - [MTD Interface] Suspend the OneNAND flash
  2131. * @param mtd MTD device structure
  2132. */
  2133. static int onenand_suspend(struct mtd_info *mtd)
  2134. {
  2135. return onenand_get_device(mtd, FL_PM_SUSPENDED);
  2136. }
  2137. /**
  2138. * onenand_resume - [MTD Interface] Resume the OneNAND flash
  2139. * @param mtd MTD device structure
  2140. */
  2141. static void onenand_resume(struct mtd_info *mtd)
  2142. {
  2143. struct onenand_chip *this = mtd->priv;
  2144. if (this->state == FL_PM_SUSPENDED)
  2145. onenand_release_device(mtd);
  2146. else
  2147. printk(KERN_ERR "resume() called for the chip which is not"
  2148. "in suspended state\n");
  2149. }
  2150. /**
  2151. * onenand_scan - [OneNAND Interface] Scan for the OneNAND device
  2152. * @param mtd MTD device structure
  2153. * @param maxchips Number of chips to scan for
  2154. *
  2155. * This fills out all the not initialized function pointers
  2156. * with the defaults.
  2157. * The flash ID is read and the mtd/chip structures are
  2158. * filled with the appropriate values.
  2159. */
  2160. int onenand_scan(struct mtd_info *mtd, int maxchips)
  2161. {
  2162. int i;
  2163. struct onenand_chip *this = mtd->priv;
  2164. if (!this->read_word)
  2165. this->read_word = onenand_readw;
  2166. if (!this->write_word)
  2167. this->write_word = onenand_writew;
  2168. if (!this->command)
  2169. this->command = onenand_command;
  2170. if (!this->wait)
  2171. onenand_setup_wait(mtd);
  2172. if (!this->read_bufferram)
  2173. this->read_bufferram = onenand_read_bufferram;
  2174. if (!this->write_bufferram)
  2175. this->write_bufferram = onenand_write_bufferram;
  2176. if (!this->block_markbad)
  2177. this->block_markbad = onenand_default_block_markbad;
  2178. if (!this->scan_bbt)
  2179. this->scan_bbt = onenand_default_bbt;
  2180. if (onenand_probe(mtd))
  2181. return -ENXIO;
  2182. /* Set Sync. Burst Read after probing */
  2183. if (this->mmcontrol) {
  2184. printk(KERN_INFO "OneNAND Sync. Burst Read support\n");
  2185. this->read_bufferram = onenand_sync_read_bufferram;
  2186. }
  2187. /* Allocate buffers, if necessary */
  2188. if (!this->page_buf) {
  2189. this->page_buf = kzalloc(mtd->writesize, GFP_KERNEL);
  2190. if (!this->page_buf) {
  2191. printk(KERN_ERR "onenand_scan(): Can't allocate page_buf\n");
  2192. return -ENOMEM;
  2193. }
  2194. this->options |= ONENAND_PAGEBUF_ALLOC;
  2195. }
  2196. if (!this->oob_buf) {
  2197. this->oob_buf = kzalloc(mtd->oobsize, GFP_KERNEL);
  2198. if (!this->oob_buf) {
  2199. printk(KERN_ERR "onenand_scan(): Can't allocate oob_buf\n");
  2200. if (this->options & ONENAND_PAGEBUF_ALLOC) {
  2201. this->options &= ~ONENAND_PAGEBUF_ALLOC;
  2202. kfree(this->page_buf);
  2203. }
  2204. return -ENOMEM;
  2205. }
  2206. this->options |= ONENAND_OOBBUF_ALLOC;
  2207. }
  2208. this->state = FL_READY;
  2209. init_waitqueue_head(&this->wq);
  2210. spin_lock_init(&this->chip_lock);
  2211. /*
  2212. * Allow subpage writes up to oobsize.
  2213. */
  2214. switch (mtd->oobsize) {
  2215. case 64:
  2216. this->ecclayout = &onenand_oob_64;
  2217. mtd->subpage_sft = 2;
  2218. break;
  2219. case 32:
  2220. this->ecclayout = &onenand_oob_32;
  2221. mtd->subpage_sft = 1;
  2222. break;
  2223. default:
  2224. printk(KERN_WARNING "No OOB scheme defined for oobsize %d\n",
  2225. mtd->oobsize);
  2226. mtd->subpage_sft = 0;
  2227. /* To prevent kernel oops */
  2228. this->ecclayout = &onenand_oob_32;
  2229. break;
  2230. }
  2231. this->subpagesize = mtd->writesize >> mtd->subpage_sft;
  2232. /*
  2233. * The number of bytes available for a client to place data into
  2234. * the out of band area
  2235. */
  2236. this->ecclayout->oobavail = 0;
  2237. for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES &&
  2238. this->ecclayout->oobfree[i].length; i++)
  2239. this->ecclayout->oobavail +=
  2240. this->ecclayout->oobfree[i].length;
  2241. mtd->oobavail = this->ecclayout->oobavail;
  2242. mtd->ecclayout = this->ecclayout;
  2243. /* Fill in remaining MTD driver data */
  2244. mtd->type = MTD_NANDFLASH;
  2245. mtd->flags = MTD_CAP_NANDFLASH;
  2246. mtd->erase = onenand_erase;
  2247. mtd->point = NULL;
  2248. mtd->unpoint = NULL;
  2249. mtd->read = onenand_read;
  2250. mtd->write = onenand_write;
  2251. mtd->read_oob = onenand_read_oob;
  2252. mtd->write_oob = onenand_write_oob;
  2253. #ifdef CONFIG_MTD_ONENAND_OTP
  2254. mtd->get_fact_prot_info = onenand_get_fact_prot_info;
  2255. mtd->read_fact_prot_reg = onenand_read_fact_prot_reg;
  2256. mtd->get_user_prot_info = onenand_get_user_prot_info;
  2257. mtd->read_user_prot_reg = onenand_read_user_prot_reg;
  2258. mtd->write_user_prot_reg = onenand_write_user_prot_reg;
  2259. mtd->lock_user_prot_reg = onenand_lock_user_prot_reg;
  2260. #endif
  2261. mtd->sync = onenand_sync;
  2262. mtd->lock = onenand_lock;
  2263. mtd->unlock = onenand_unlock;
  2264. mtd->suspend = onenand_suspend;
  2265. mtd->resume = onenand_resume;
  2266. mtd->block_isbad = onenand_block_isbad;
  2267. mtd->block_markbad = onenand_block_markbad;
  2268. mtd->owner = THIS_MODULE;
  2269. /* Unlock whole block */
  2270. onenand_unlock_all(mtd);
  2271. return this->scan_bbt(mtd);
  2272. }
  2273. /**
  2274. * onenand_release - [OneNAND Interface] Free resources held by the OneNAND device
  2275. * @param mtd MTD device structure
  2276. */
  2277. void onenand_release(struct mtd_info *mtd)
  2278. {
  2279. struct onenand_chip *this = mtd->priv;
  2280. #ifdef CONFIG_MTD_PARTITIONS
  2281. /* Deregister partitions */
  2282. del_mtd_partitions (mtd);
  2283. #endif
  2284. /* Deregister the device */
  2285. del_mtd_device (mtd);
  2286. /* Free bad block table memory, if allocated */
  2287. if (this->bbm) {
  2288. struct bbm_info *bbm = this->bbm;
  2289. kfree(bbm->bbt);
  2290. kfree(this->bbm);
  2291. }
  2292. /* Buffers allocated by onenand_scan */
  2293. if (this->options & ONENAND_PAGEBUF_ALLOC)
  2294. kfree(this->page_buf);
  2295. if (this->options & ONENAND_OOBBUF_ALLOC)
  2296. kfree(this->oob_buf);
  2297. }
  2298. EXPORT_SYMBOL_GPL(onenand_scan);
  2299. EXPORT_SYMBOL_GPL(onenand_release);
  2300. MODULE_LICENSE("GPL");
  2301. MODULE_AUTHOR("Kyungmin Park <kyungmin.park@samsung.com>");
  2302. MODULE_DESCRIPTION("Generic OneNAND flash driver code");