onenand_base.c 64 KB

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