onenand_base.c 70 KB

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