onenand_base.c 72 KB

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