onenand_base.c 72 KB

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