onenand_base.c 74 KB

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