onenand_base.c 73 KB

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