onenand_base.c 73 KB

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