onenand_base.c 71 KB

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