onenand_base.c 72 KB

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