onenand_base.c 72 KB

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