onenand_base.c 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448
  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. uint8_t *oob_buf = this->page_buf + mtd->writesize;
  716. for (free = this->ecclayout->oobfree; free->length; ++free) {
  717. if (readcol >= lastgap)
  718. readcol += free->offset - lastgap;
  719. if (readend >= lastgap)
  720. readend += free->offset - lastgap;
  721. lastgap = free->offset + free->length;
  722. }
  723. this->read_bufferram(mtd, ONENAND_SPARERAM, oob_buf, 0, mtd->oobsize);
  724. for (free = this->ecclayout->oobfree; free->length; ++free) {
  725. int free_end = free->offset + free->length;
  726. if (free->offset < readend && free_end > readcol) {
  727. int st = max_t(int,free->offset,readcol);
  728. int ed = min_t(int,free_end,readend);
  729. int n = ed - st;
  730. memcpy(buf, oob_buf + st, n);
  731. buf += n;
  732. }
  733. }
  734. return 0;
  735. }
  736. /**
  737. * onenand_do_read_oob - [MTD Interface] OneNAND read out-of-band
  738. * @param mtd MTD device structure
  739. * @param from offset to read from
  740. * @param len number of bytes to read
  741. * @param retlen pointer to variable to store the number of read bytes
  742. * @param buf the databuffer to put data
  743. * @param mode operation mode
  744. *
  745. * OneNAND read out-of-band data from the spare area
  746. */
  747. static int onenand_do_read_oob(struct mtd_info *mtd, loff_t from, size_t len,
  748. size_t *retlen, u_char *buf, mtd_oob_mode_t mode)
  749. {
  750. struct onenand_chip *this = mtd->priv;
  751. int read = 0, thislen, column, oobsize;
  752. int ret = 0;
  753. DEBUG(MTD_DEBUG_LEVEL3, "onenand_read_oob: from = 0x%08x, len = %i\n", (unsigned int) from, (int) len);
  754. /* Initialize return length value */
  755. *retlen = 0;
  756. if (mode == MTD_OOB_AUTO)
  757. oobsize = this->ecclayout->oobavail;
  758. else
  759. oobsize = mtd->oobsize;
  760. column = from & (mtd->oobsize - 1);
  761. if (unlikely(column >= oobsize)) {
  762. printk(KERN_ERR "onenand_read_oob: Attempted to start read outside oob\n");
  763. return -EINVAL;
  764. }
  765. /* Do not allow reads past end of device */
  766. if (unlikely(from >= mtd->size ||
  767. column + len > ((mtd->size >> this->page_shift) -
  768. (from >> this->page_shift)) * oobsize)) {
  769. printk(KERN_ERR "onenand_read_oob: Attempted to read beyond end of device\n");
  770. return -EINVAL;
  771. }
  772. /* Grab the lock and see if the device is available */
  773. onenand_get_device(mtd, FL_READING);
  774. while (read < len) {
  775. cond_resched();
  776. thislen = oobsize - column;
  777. thislen = min_t(int, thislen, len);
  778. this->command(mtd, ONENAND_CMD_READOOB, from, mtd->oobsize);
  779. onenand_update_bufferram(mtd, from, 0);
  780. ret = this->wait(mtd, FL_READING);
  781. /* First copy data and check return value for ECC handling */
  782. if (mode == MTD_OOB_AUTO)
  783. onenand_transfer_auto_oob(mtd, buf, column, thislen);
  784. else
  785. this->read_bufferram(mtd, ONENAND_SPARERAM, buf, column, thislen);
  786. if (ret) {
  787. printk(KERN_ERR "onenand_read_oob: read failed = 0x%x\n", ret);
  788. break;
  789. }
  790. read += thislen;
  791. if (read == len)
  792. break;
  793. buf += thislen;
  794. /* Read more? */
  795. if (read < len) {
  796. /* Page size */
  797. from += mtd->writesize;
  798. column = 0;
  799. }
  800. }
  801. /* Deselect and wake up anyone waiting on the device */
  802. onenand_release_device(mtd);
  803. *retlen = read;
  804. return ret;
  805. }
  806. /**
  807. * onenand_read_oob - [MTD Interface] NAND write data and/or out-of-band
  808. * @param mtd: MTD device structure
  809. * @param from: offset to read from
  810. * @param ops: oob operation description structure
  811. */
  812. static int onenand_read_oob(struct mtd_info *mtd, loff_t from,
  813. struct mtd_oob_ops *ops)
  814. {
  815. switch (ops->mode) {
  816. case MTD_OOB_PLACE:
  817. case MTD_OOB_AUTO:
  818. break;
  819. case MTD_OOB_RAW:
  820. /* Not implemented yet */
  821. default:
  822. return -EINVAL;
  823. }
  824. return onenand_do_read_oob(mtd, from + ops->ooboffs, ops->ooblen,
  825. &ops->oobretlen, ops->oobbuf, ops->mode);
  826. }
  827. /**
  828. * onenand_bbt_wait - [DEFAULT] wait until the command is done
  829. * @param mtd MTD device structure
  830. * @param state state to select the max. timeout value
  831. *
  832. * Wait for command done.
  833. */
  834. static int onenand_bbt_wait(struct mtd_info *mtd, int state)
  835. {
  836. struct onenand_chip *this = mtd->priv;
  837. unsigned long timeout;
  838. unsigned int interrupt;
  839. unsigned int ctrl;
  840. /* The 20 msec is enough */
  841. timeout = jiffies + msecs_to_jiffies(20);
  842. while (time_before(jiffies, timeout)) {
  843. interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
  844. if (interrupt & ONENAND_INT_MASTER)
  845. break;
  846. }
  847. /* To get correct interrupt status in timeout case */
  848. interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
  849. ctrl = this->read_word(this->base + ONENAND_REG_CTRL_STATUS);
  850. if (ctrl & ONENAND_CTRL_ERROR) {
  851. printk(KERN_DEBUG "onenand_bbt_wait: controller error = 0x%04x\n", ctrl);
  852. /* Initial bad block case */
  853. if (ctrl & ONENAND_CTRL_LOAD)
  854. return ONENAND_BBT_READ_ERROR;
  855. return ONENAND_BBT_READ_FATAL_ERROR;
  856. }
  857. if (interrupt & ONENAND_INT_READ) {
  858. int ecc = this->read_word(this->base + ONENAND_REG_ECC_STATUS);
  859. if (ecc & ONENAND_ECC_2BIT_ALL)
  860. return ONENAND_BBT_READ_ERROR;
  861. } else {
  862. printk(KERN_ERR "onenand_bbt_wait: read timeout!"
  863. "ctrl=0x%04x intr=0x%04x\n", ctrl, interrupt);
  864. return ONENAND_BBT_READ_FATAL_ERROR;
  865. }
  866. return 0;
  867. }
  868. /**
  869. * onenand_bbt_read_oob - [MTD Interface] OneNAND read out-of-band for bbt scan
  870. * @param mtd MTD device structure
  871. * @param from offset to read from
  872. * @param ops oob operation description structure
  873. *
  874. * OneNAND read out-of-band data from the spare area for bbt scan
  875. */
  876. int onenand_bbt_read_oob(struct mtd_info *mtd, loff_t from,
  877. struct mtd_oob_ops *ops)
  878. {
  879. struct onenand_chip *this = mtd->priv;
  880. int read = 0, thislen, column;
  881. int ret = 0;
  882. size_t len = ops->ooblen;
  883. u_char *buf = ops->oobbuf;
  884. DEBUG(MTD_DEBUG_LEVEL3, "onenand_bbt_read_oob: from = 0x%08x, len = %zi\n", (unsigned int) from, len);
  885. /* Initialize return value */
  886. ops->oobretlen = 0;
  887. /* Do not allow reads past end of device */
  888. if (unlikely((from + len) > mtd->size)) {
  889. printk(KERN_ERR "onenand_bbt_read_oob: Attempt read beyond end of device\n");
  890. return ONENAND_BBT_READ_FATAL_ERROR;
  891. }
  892. /* Grab the lock and see if the device is available */
  893. onenand_get_device(mtd, FL_READING);
  894. column = from & (mtd->oobsize - 1);
  895. while (read < len) {
  896. cond_resched();
  897. thislen = mtd->oobsize - column;
  898. thislen = min_t(int, thislen, len);
  899. this->command(mtd, ONENAND_CMD_READOOB, from, mtd->oobsize);
  900. onenand_update_bufferram(mtd, from, 0);
  901. ret = onenand_bbt_wait(mtd, FL_READING);
  902. if (ret)
  903. break;
  904. this->read_bufferram(mtd, ONENAND_SPARERAM, buf, column, thislen);
  905. read += thislen;
  906. if (read == len)
  907. break;
  908. buf += thislen;
  909. /* Read more? */
  910. if (read < len) {
  911. /* Update Page size */
  912. from += mtd->writesize;
  913. column = 0;
  914. }
  915. }
  916. /* Deselect and wake up anyone waiting on the device */
  917. onenand_release_device(mtd);
  918. ops->oobretlen = read;
  919. return ret;
  920. }
  921. #ifdef CONFIG_MTD_ONENAND_VERIFY_WRITE
  922. /**
  923. * onenand_verify_oob - [GENERIC] verify the oob contents after a write
  924. * @param mtd MTD device structure
  925. * @param buf the databuffer to verify
  926. * @param to offset to read from
  927. *
  928. */
  929. static int onenand_verify_oob(struct mtd_info *mtd, const u_char *buf, loff_t to)
  930. {
  931. struct onenand_chip *this = mtd->priv;
  932. char oobbuf[64];
  933. int status, i;
  934. this->command(mtd, ONENAND_CMD_READOOB, to, mtd->oobsize);
  935. onenand_update_bufferram(mtd, to, 0);
  936. status = this->wait(mtd, FL_READING);
  937. if (status)
  938. return status;
  939. this->read_bufferram(mtd, ONENAND_SPARERAM, oobbuf, 0, mtd->oobsize);
  940. for (i = 0; i < mtd->oobsize; i++)
  941. if (buf[i] != 0xFF && buf[i] != oobbuf[i])
  942. return -EBADMSG;
  943. return 0;
  944. }
  945. /**
  946. * onenand_verify - [GENERIC] verify the chip contents after a write
  947. * @param mtd MTD device structure
  948. * @param buf the databuffer to verify
  949. * @param addr offset to read from
  950. * @param len number of bytes to read and compare
  951. *
  952. */
  953. static int onenand_verify(struct mtd_info *mtd, const u_char *buf, loff_t addr, size_t len)
  954. {
  955. struct onenand_chip *this = mtd->priv;
  956. void __iomem *dataram;
  957. int ret = 0;
  958. int thislen, column;
  959. while (len != 0) {
  960. thislen = min_t(int, mtd->writesize, len);
  961. column = addr & (mtd->writesize - 1);
  962. if (column + thislen > mtd->writesize)
  963. thislen = mtd->writesize - column;
  964. this->command(mtd, ONENAND_CMD_READ, addr, mtd->writesize);
  965. onenand_update_bufferram(mtd, addr, 0);
  966. ret = this->wait(mtd, FL_READING);
  967. if (ret)
  968. return ret;
  969. onenand_update_bufferram(mtd, addr, 1);
  970. dataram = this->base + ONENAND_DATARAM;
  971. dataram += onenand_bufferram_offset(mtd, ONENAND_DATARAM);
  972. if (memcmp(buf, dataram + column, thislen))
  973. return -EBADMSG;
  974. len -= thislen;
  975. buf += thislen;
  976. addr += thislen;
  977. }
  978. return 0;
  979. }
  980. #else
  981. #define onenand_verify(...) (0)
  982. #define onenand_verify_oob(...) (0)
  983. #endif
  984. #define NOTALIGNED(x) ((x & (this->subpagesize - 1)) != 0)
  985. /**
  986. * onenand_write - [MTD Interface] write buffer to FLASH
  987. * @param mtd MTD device structure
  988. * @param to offset to write to
  989. * @param len number of bytes to write
  990. * @param retlen pointer to variable to store the number of written bytes
  991. * @param buf the data to write
  992. *
  993. * Write with ECC
  994. */
  995. static int onenand_write(struct mtd_info *mtd, loff_t to, size_t len,
  996. size_t *retlen, const u_char *buf)
  997. {
  998. struct onenand_chip *this = mtd->priv;
  999. int written = 0;
  1000. int ret = 0;
  1001. int column, subpage;
  1002. DEBUG(MTD_DEBUG_LEVEL3, "onenand_write: to = 0x%08x, len = %i\n", (unsigned int) to, (int) len);
  1003. /* Initialize retlen, in case of early exit */
  1004. *retlen = 0;
  1005. /* Do not allow writes past end of device */
  1006. if (unlikely((to + len) > mtd->size)) {
  1007. printk(KERN_ERR "onenand_write: Attempt write to past end of device\n");
  1008. return -EINVAL;
  1009. }
  1010. /* Reject writes, which are not page aligned */
  1011. if (unlikely(NOTALIGNED(to)) || unlikely(NOTALIGNED(len))) {
  1012. printk(KERN_ERR "onenand_write: Attempt to write not page aligned data\n");
  1013. return -EINVAL;
  1014. }
  1015. column = to & (mtd->writesize - 1);
  1016. /* Grab the lock and see if the device is available */
  1017. onenand_get_device(mtd, FL_WRITING);
  1018. /* Loop until all data write */
  1019. while (written < len) {
  1020. int thislen = min_t(int, mtd->writesize - column, len - written);
  1021. u_char *wbuf = (u_char *) buf;
  1022. cond_resched();
  1023. this->command(mtd, ONENAND_CMD_BUFFERRAM, to, thislen);
  1024. /* Partial page write */
  1025. subpage = thislen < mtd->writesize;
  1026. if (subpage) {
  1027. memset(this->page_buf, 0xff, mtd->writesize);
  1028. memcpy(this->page_buf + column, buf, thislen);
  1029. wbuf = this->page_buf;
  1030. }
  1031. this->write_bufferram(mtd, ONENAND_DATARAM, wbuf, 0, mtd->writesize);
  1032. this->write_bufferram(mtd, ONENAND_SPARERAM, ffchars, 0, mtd->oobsize);
  1033. this->command(mtd, ONENAND_CMD_PROG, to, mtd->writesize);
  1034. ret = this->wait(mtd, FL_WRITING);
  1035. /* In partial page write we don't update bufferram */
  1036. onenand_update_bufferram(mtd, to, !ret && !subpage);
  1037. if (ret) {
  1038. printk(KERN_ERR "onenand_write: write filaed %d\n", ret);
  1039. break;
  1040. }
  1041. /* Only check verify write turn on */
  1042. ret = onenand_verify(mtd, (u_char *) wbuf, to, thislen);
  1043. if (ret) {
  1044. printk(KERN_ERR "onenand_write: verify failed %d\n", ret);
  1045. break;
  1046. }
  1047. written += thislen;
  1048. if (written == len)
  1049. break;
  1050. column = 0;
  1051. to += thislen;
  1052. buf += thislen;
  1053. }
  1054. /* Deselect and wake up anyone waiting on the device */
  1055. onenand_release_device(mtd);
  1056. *retlen = written;
  1057. return ret;
  1058. }
  1059. /**
  1060. * onenand_fill_auto_oob - [Internal] oob auto-placement transfer
  1061. * @param mtd MTD device structure
  1062. * @param oob_buf oob buffer
  1063. * @param buf source address
  1064. * @param column oob offset to write to
  1065. * @param thislen oob length to write
  1066. */
  1067. static int onenand_fill_auto_oob(struct mtd_info *mtd, u_char *oob_buf,
  1068. const u_char *buf, int column, int thislen)
  1069. {
  1070. struct onenand_chip *this = mtd->priv;
  1071. struct nand_oobfree *free;
  1072. int writecol = column;
  1073. int writeend = column + thislen;
  1074. int lastgap = 0;
  1075. for (free = this->ecclayout->oobfree; free->length; ++free) {
  1076. if (writecol >= lastgap)
  1077. writecol += free->offset - lastgap;
  1078. if (writeend >= lastgap)
  1079. writeend += free->offset - lastgap;
  1080. lastgap = free->offset + free->length;
  1081. }
  1082. for (free = this->ecclayout->oobfree; free->length; ++free) {
  1083. int free_end = free->offset + free->length;
  1084. if (free->offset < writeend && free_end > writecol) {
  1085. int st = max_t(int,free->offset,writecol);
  1086. int ed = min_t(int,free_end,writeend);
  1087. int n = ed - st;
  1088. memcpy(oob_buf + st, buf, n);
  1089. buf += n;
  1090. }
  1091. }
  1092. return 0;
  1093. }
  1094. /**
  1095. * onenand_do_write_oob - [Internal] OneNAND write out-of-band
  1096. * @param mtd MTD device structure
  1097. * @param to offset to write to
  1098. * @param len number of bytes to write
  1099. * @param retlen pointer to variable to store the number of written bytes
  1100. * @param buf the data to write
  1101. * @param mode operation mode
  1102. *
  1103. * OneNAND write out-of-band
  1104. */
  1105. static int onenand_do_write_oob(struct mtd_info *mtd, loff_t to, size_t len,
  1106. size_t *retlen, const u_char *buf, mtd_oob_mode_t mode)
  1107. {
  1108. struct onenand_chip *this = mtd->priv;
  1109. int column, ret = 0, oobsize;
  1110. int written = 0;
  1111. u_char *oobbuf;
  1112. DEBUG(MTD_DEBUG_LEVEL3, "onenand_write_oob: to = 0x%08x, len = %i\n", (unsigned int) to, (int) len);
  1113. /* Initialize retlen, in case of early exit */
  1114. *retlen = 0;
  1115. if (mode == MTD_OOB_AUTO)
  1116. oobsize = this->ecclayout->oobavail;
  1117. else
  1118. oobsize = mtd->oobsize;
  1119. column = to & (mtd->oobsize - 1);
  1120. if (unlikely(column >= oobsize)) {
  1121. printk(KERN_ERR "onenand_write_oob: Attempted to start write outside oob\n");
  1122. return -EINVAL;
  1123. }
  1124. /* For compatibility with NAND: Do not allow write past end of page */
  1125. if (unlikely(column + len > oobsize)) {
  1126. printk(KERN_ERR "onenand_write_oob: "
  1127. "Attempt to write past end of page\n");
  1128. return -EINVAL;
  1129. }
  1130. /* Do not allow reads past end of device */
  1131. if (unlikely(to >= mtd->size ||
  1132. column + len > ((mtd->size >> this->page_shift) -
  1133. (to >> this->page_shift)) * oobsize)) {
  1134. printk(KERN_ERR "onenand_write_oob: Attempted to write past end of device\n");
  1135. return -EINVAL;
  1136. }
  1137. /* Grab the lock and see if the device is available */
  1138. onenand_get_device(mtd, FL_WRITING);
  1139. oobbuf = this->page_buf + mtd->writesize;
  1140. /* Loop until all data write */
  1141. while (written < len) {
  1142. int thislen = min_t(int, oobsize, len - written);
  1143. cond_resched();
  1144. this->command(mtd, ONENAND_CMD_BUFFERRAM, to, mtd->oobsize);
  1145. /* We send data to spare ram with oobsize
  1146. * to prevent byte access */
  1147. memset(oobbuf, 0xff, mtd->oobsize);
  1148. if (mode == MTD_OOB_AUTO)
  1149. onenand_fill_auto_oob(mtd, oobbuf, buf, column, thislen);
  1150. else
  1151. memcpy(oobbuf + column, buf, thislen);
  1152. this->write_bufferram(mtd, ONENAND_SPARERAM, oobbuf, 0, mtd->oobsize);
  1153. this->command(mtd, ONENAND_CMD_PROGOOB, to, mtd->oobsize);
  1154. onenand_update_bufferram(mtd, to, 0);
  1155. ret = this->wait(mtd, FL_WRITING);
  1156. if (ret) {
  1157. printk(KERN_ERR "onenand_write_oob: write failed %d\n", ret);
  1158. break;
  1159. }
  1160. ret = onenand_verify_oob(mtd, oobbuf, to);
  1161. if (ret) {
  1162. printk(KERN_ERR "onenand_write_oob: verify failed %d\n", ret);
  1163. break;
  1164. }
  1165. written += thislen;
  1166. if (written == len)
  1167. break;
  1168. to += mtd->writesize;
  1169. buf += thislen;
  1170. column = 0;
  1171. }
  1172. /* Deselect and wake up anyone waiting on the device */
  1173. onenand_release_device(mtd);
  1174. *retlen = written;
  1175. return ret;
  1176. }
  1177. /**
  1178. * onenand_write_oob - [MTD Interface] NAND write data and/or out-of-band
  1179. * @param mtd: MTD device structure
  1180. * @param to: offset to write
  1181. * @param ops: oob operation description structure
  1182. */
  1183. static int onenand_write_oob(struct mtd_info *mtd, loff_t to,
  1184. struct mtd_oob_ops *ops)
  1185. {
  1186. switch (ops->mode) {
  1187. case MTD_OOB_PLACE:
  1188. case MTD_OOB_AUTO:
  1189. break;
  1190. case MTD_OOB_RAW:
  1191. /* Not implemented yet */
  1192. default:
  1193. return -EINVAL;
  1194. }
  1195. return onenand_do_write_oob(mtd, to + ops->ooboffs, ops->ooblen,
  1196. &ops->oobretlen, ops->oobbuf, ops->mode);
  1197. }
  1198. /**
  1199. * onenand_block_checkbad - [GENERIC] Check if a block is marked bad
  1200. * @param mtd MTD device structure
  1201. * @param ofs offset from device start
  1202. * @param getchip 0, if the chip is already selected
  1203. * @param allowbbt 1, if its allowed to access the bbt area
  1204. *
  1205. * Check, if the block is bad. Either by reading the bad block table or
  1206. * calling of the scan function.
  1207. */
  1208. static int onenand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int getchip, int allowbbt)
  1209. {
  1210. struct onenand_chip *this = mtd->priv;
  1211. struct bbm_info *bbm = this->bbm;
  1212. /* Return info from the table */
  1213. return bbm->isbad_bbt(mtd, ofs, allowbbt);
  1214. }
  1215. /**
  1216. * onenand_erase - [MTD Interface] erase block(s)
  1217. * @param mtd MTD device structure
  1218. * @param instr erase instruction
  1219. *
  1220. * Erase one ore more blocks
  1221. */
  1222. static int onenand_erase(struct mtd_info *mtd, struct erase_info *instr)
  1223. {
  1224. struct onenand_chip *this = mtd->priv;
  1225. unsigned int block_size;
  1226. loff_t addr;
  1227. int len;
  1228. int ret = 0;
  1229. DEBUG(MTD_DEBUG_LEVEL3, "onenand_erase: start = 0x%08x, len = %i\n", (unsigned int) instr->addr, (unsigned int) instr->len);
  1230. block_size = (1 << this->erase_shift);
  1231. /* Start address must align on block boundary */
  1232. if (unlikely(instr->addr & (block_size - 1))) {
  1233. printk(KERN_ERR "onenand_erase: Unaligned address\n");
  1234. return -EINVAL;
  1235. }
  1236. /* Length must align on block boundary */
  1237. if (unlikely(instr->len & (block_size - 1))) {
  1238. printk(KERN_ERR "onenand_erase: Length not block aligned\n");
  1239. return -EINVAL;
  1240. }
  1241. /* Do not allow erase past end of device */
  1242. if (unlikely((instr->len + instr->addr) > mtd->size)) {
  1243. printk(KERN_ERR "onenand_erase: Erase past end of device\n");
  1244. return -EINVAL;
  1245. }
  1246. instr->fail_addr = 0xffffffff;
  1247. /* Grab the lock and see if the device is available */
  1248. onenand_get_device(mtd, FL_ERASING);
  1249. /* Loop throught the pages */
  1250. len = instr->len;
  1251. addr = instr->addr;
  1252. instr->state = MTD_ERASING;
  1253. while (len) {
  1254. cond_resched();
  1255. /* Check if we have a bad block, we do not erase bad blocks */
  1256. if (onenand_block_checkbad(mtd, addr, 0, 0)) {
  1257. printk (KERN_WARNING "onenand_erase: attempt to erase a bad block at addr 0x%08x\n", (unsigned int) addr);
  1258. instr->state = MTD_ERASE_FAILED;
  1259. goto erase_exit;
  1260. }
  1261. this->command(mtd, ONENAND_CMD_ERASE, addr, block_size);
  1262. onenand_invalidate_bufferram(mtd, addr, block_size);
  1263. ret = this->wait(mtd, FL_ERASING);
  1264. /* Check, if it is write protected */
  1265. if (ret) {
  1266. printk(KERN_ERR "onenand_erase: Failed erase, block %d\n", (unsigned) (addr >> this->erase_shift));
  1267. instr->state = MTD_ERASE_FAILED;
  1268. instr->fail_addr = addr;
  1269. goto erase_exit;
  1270. }
  1271. len -= block_size;
  1272. addr += block_size;
  1273. }
  1274. instr->state = MTD_ERASE_DONE;
  1275. erase_exit:
  1276. ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
  1277. /* Do call back function */
  1278. if (!ret)
  1279. mtd_erase_callback(instr);
  1280. /* Deselect and wake up anyone waiting on the device */
  1281. onenand_release_device(mtd);
  1282. return ret;
  1283. }
  1284. /**
  1285. * onenand_sync - [MTD Interface] sync
  1286. * @param mtd MTD device structure
  1287. *
  1288. * Sync is actually a wait for chip ready function
  1289. */
  1290. static void onenand_sync(struct mtd_info *mtd)
  1291. {
  1292. DEBUG(MTD_DEBUG_LEVEL3, "onenand_sync: called\n");
  1293. /* Grab the lock and see if the device is available */
  1294. onenand_get_device(mtd, FL_SYNCING);
  1295. /* Release it and go back */
  1296. onenand_release_device(mtd);
  1297. }
  1298. /**
  1299. * onenand_block_isbad - [MTD Interface] Check whether the block at the given offset is bad
  1300. * @param mtd MTD device structure
  1301. * @param ofs offset relative to mtd start
  1302. *
  1303. * Check whether the block is bad
  1304. */
  1305. static int onenand_block_isbad(struct mtd_info *mtd, loff_t ofs)
  1306. {
  1307. /* Check for invalid offset */
  1308. if (ofs > mtd->size)
  1309. return -EINVAL;
  1310. return onenand_block_checkbad(mtd, ofs, 1, 0);
  1311. }
  1312. /**
  1313. * onenand_default_block_markbad - [DEFAULT] mark a block bad
  1314. * @param mtd MTD device structure
  1315. * @param ofs offset from device start
  1316. *
  1317. * This is the default implementation, which can be overridden by
  1318. * a hardware specific driver.
  1319. */
  1320. static int onenand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
  1321. {
  1322. struct onenand_chip *this = mtd->priv;
  1323. struct bbm_info *bbm = this->bbm;
  1324. u_char buf[2] = {0, 0};
  1325. size_t retlen;
  1326. int block;
  1327. /* Get block number */
  1328. block = ((int) ofs) >> bbm->bbt_erase_shift;
  1329. if (bbm->bbt)
  1330. bbm->bbt[block >> 2] |= 0x01 << ((block & 0x03) << 1);
  1331. /* We write two bytes, so we dont have to mess with 16 bit access */
  1332. ofs += mtd->oobsize + (bbm->badblockpos & ~0x01);
  1333. return onenand_do_write_oob(mtd, ofs , 2, &retlen, buf, MTD_OOB_PLACE);
  1334. }
  1335. /**
  1336. * onenand_block_markbad - [MTD Interface] Mark the block at the given offset as bad
  1337. * @param mtd MTD device structure
  1338. * @param ofs offset relative to mtd start
  1339. *
  1340. * Mark the block as bad
  1341. */
  1342. static int onenand_block_markbad(struct mtd_info *mtd, loff_t ofs)
  1343. {
  1344. struct onenand_chip *this = mtd->priv;
  1345. int ret;
  1346. ret = onenand_block_isbad(mtd, ofs);
  1347. if (ret) {
  1348. /* If it was bad already, return success and do nothing */
  1349. if (ret > 0)
  1350. return 0;
  1351. return ret;
  1352. }
  1353. return this->block_markbad(mtd, ofs);
  1354. }
  1355. /**
  1356. * onenand_do_lock_cmd - [OneNAND Interface] Lock or unlock block(s)
  1357. * @param mtd MTD device structure
  1358. * @param ofs offset relative to mtd start
  1359. * @param len number of bytes to lock or unlock
  1360. * @param cmd lock or unlock command
  1361. *
  1362. * Lock or unlock one or more blocks
  1363. */
  1364. static int onenand_do_lock_cmd(struct mtd_info *mtd, loff_t ofs, size_t len, int cmd)
  1365. {
  1366. struct onenand_chip *this = mtd->priv;
  1367. int start, end, block, value, status;
  1368. int wp_status_mask;
  1369. start = ofs >> this->erase_shift;
  1370. end = len >> this->erase_shift;
  1371. if (cmd == ONENAND_CMD_LOCK)
  1372. wp_status_mask = ONENAND_WP_LS;
  1373. else
  1374. wp_status_mask = ONENAND_WP_US;
  1375. /* Continuous lock scheme */
  1376. if (this->options & ONENAND_HAS_CONT_LOCK) {
  1377. /* Set start block address */
  1378. this->write_word(start, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
  1379. /* Set end block address */
  1380. this->write_word(start + end - 1, this->base + ONENAND_REG_END_BLOCK_ADDRESS);
  1381. /* Write lock command */
  1382. this->command(mtd, cmd, 0, 0);
  1383. /* There's no return value */
  1384. this->wait(mtd, FL_LOCKING);
  1385. /* Sanity check */
  1386. while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
  1387. & ONENAND_CTRL_ONGO)
  1388. continue;
  1389. /* Check lock status */
  1390. status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
  1391. if (!(status & wp_status_mask))
  1392. printk(KERN_ERR "wp status = 0x%x\n", status);
  1393. return 0;
  1394. }
  1395. /* Block lock scheme */
  1396. for (block = start; block < start + end; block++) {
  1397. /* Set block address */
  1398. value = onenand_block_address(this, block);
  1399. this->write_word(value, this->base + ONENAND_REG_START_ADDRESS1);
  1400. /* Select DataRAM for DDP */
  1401. value = onenand_bufferram_address(this, block);
  1402. this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
  1403. /* Set start block address */
  1404. this->write_word(block, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
  1405. /* Write lock command */
  1406. this->command(mtd, cmd, 0, 0);
  1407. /* There's no return value */
  1408. this->wait(mtd, FL_LOCKING);
  1409. /* Sanity check */
  1410. while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
  1411. & ONENAND_CTRL_ONGO)
  1412. continue;
  1413. /* Check lock status */
  1414. status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
  1415. if (!(status & wp_status_mask))
  1416. printk(KERN_ERR "block = %d, wp status = 0x%x\n", block, status);
  1417. }
  1418. return 0;
  1419. }
  1420. /**
  1421. * onenand_lock - [MTD Interface] Lock block(s)
  1422. * @param mtd MTD device structure
  1423. * @param ofs offset relative to mtd start
  1424. * @param len number of bytes to unlock
  1425. *
  1426. * Lock one or more blocks
  1427. */
  1428. static int onenand_lock(struct mtd_info *mtd, loff_t ofs, size_t len)
  1429. {
  1430. return onenand_do_lock_cmd(mtd, ofs, len, ONENAND_CMD_LOCK);
  1431. }
  1432. /**
  1433. * onenand_unlock - [MTD Interface] Unlock block(s)
  1434. * @param mtd MTD device structure
  1435. * @param ofs offset relative to mtd start
  1436. * @param len number of bytes to unlock
  1437. *
  1438. * Unlock one or more blocks
  1439. */
  1440. static int onenand_unlock(struct mtd_info *mtd, loff_t ofs, size_t len)
  1441. {
  1442. return onenand_do_lock_cmd(mtd, ofs, len, ONENAND_CMD_UNLOCK);
  1443. }
  1444. /**
  1445. * onenand_check_lock_status - [OneNAND Interface] Check lock status
  1446. * @param this onenand chip data structure
  1447. *
  1448. * Check lock status
  1449. */
  1450. static void onenand_check_lock_status(struct onenand_chip *this)
  1451. {
  1452. unsigned int value, block, status;
  1453. unsigned int end;
  1454. end = this->chipsize >> this->erase_shift;
  1455. for (block = 0; block < end; block++) {
  1456. /* Set block address */
  1457. value = onenand_block_address(this, block);
  1458. this->write_word(value, this->base + ONENAND_REG_START_ADDRESS1);
  1459. /* Select DataRAM for DDP */
  1460. value = onenand_bufferram_address(this, block);
  1461. this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
  1462. /* Set start block address */
  1463. this->write_word(block, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
  1464. /* Check lock status */
  1465. status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
  1466. if (!(status & ONENAND_WP_US))
  1467. printk(KERN_ERR "block = %d, wp status = 0x%x\n", block, status);
  1468. }
  1469. }
  1470. /**
  1471. * onenand_unlock_all - [OneNAND Interface] unlock all blocks
  1472. * @param mtd MTD device structure
  1473. *
  1474. * Unlock all blocks
  1475. */
  1476. static int onenand_unlock_all(struct mtd_info *mtd)
  1477. {
  1478. struct onenand_chip *this = mtd->priv;
  1479. if (this->options & ONENAND_HAS_UNLOCK_ALL) {
  1480. /* Set start block address */
  1481. this->write_word(0, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
  1482. /* Write unlock command */
  1483. this->command(mtd, ONENAND_CMD_UNLOCK_ALL, 0, 0);
  1484. /* There's no return value */
  1485. this->wait(mtd, FL_LOCKING);
  1486. /* Sanity check */
  1487. while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
  1488. & ONENAND_CTRL_ONGO)
  1489. continue;
  1490. /* Workaround for all block unlock in DDP */
  1491. if (ONENAND_IS_DDP(this)) {
  1492. /* 1st block on another chip */
  1493. loff_t ofs = this->chipsize >> 1;
  1494. size_t len = mtd->erasesize;
  1495. onenand_unlock(mtd, ofs, len);
  1496. }
  1497. onenand_check_lock_status(this);
  1498. return 0;
  1499. }
  1500. onenand_unlock(mtd, 0x0, this->chipsize);
  1501. return 0;
  1502. }
  1503. #ifdef CONFIG_MTD_ONENAND_OTP
  1504. /* Interal OTP operation */
  1505. typedef int (*otp_op_t)(struct mtd_info *mtd, loff_t form, size_t len,
  1506. size_t *retlen, u_char *buf);
  1507. /**
  1508. * do_otp_read - [DEFAULT] Read OTP block area
  1509. * @param mtd MTD device structure
  1510. * @param from The offset to read
  1511. * @param len number of bytes to read
  1512. * @param retlen pointer to variable to store the number of readbytes
  1513. * @param buf the databuffer to put/get data
  1514. *
  1515. * Read OTP block area.
  1516. */
  1517. static int do_otp_read(struct mtd_info *mtd, loff_t from, size_t len,
  1518. size_t *retlen, u_char *buf)
  1519. {
  1520. struct onenand_chip *this = mtd->priv;
  1521. int ret;
  1522. /* Enter OTP access mode */
  1523. this->command(mtd, ONENAND_CMD_OTP_ACCESS, 0, 0);
  1524. this->wait(mtd, FL_OTPING);
  1525. ret = mtd->read(mtd, from, len, retlen, buf);
  1526. /* Exit OTP access mode */
  1527. this->command(mtd, ONENAND_CMD_RESET, 0, 0);
  1528. this->wait(mtd, FL_RESETING);
  1529. return ret;
  1530. }
  1531. /**
  1532. * do_otp_write - [DEFAULT] Write OTP block area
  1533. * @param mtd MTD device structure
  1534. * @param from The offset to write
  1535. * @param len number of bytes to write
  1536. * @param retlen pointer to variable to store the number of write bytes
  1537. * @param buf the databuffer to put/get data
  1538. *
  1539. * Write OTP block area.
  1540. */
  1541. static int do_otp_write(struct mtd_info *mtd, loff_t from, size_t len,
  1542. size_t *retlen, u_char *buf)
  1543. {
  1544. struct onenand_chip *this = mtd->priv;
  1545. unsigned char *pbuf = buf;
  1546. int ret;
  1547. /* Force buffer page aligned */
  1548. if (len < mtd->writesize) {
  1549. memcpy(this->page_buf, buf, len);
  1550. memset(this->page_buf + len, 0xff, mtd->writesize - len);
  1551. pbuf = this->page_buf;
  1552. len = mtd->writesize;
  1553. }
  1554. /* Enter OTP access mode */
  1555. this->command(mtd, ONENAND_CMD_OTP_ACCESS, 0, 0);
  1556. this->wait(mtd, FL_OTPING);
  1557. ret = mtd->write(mtd, from, len, retlen, pbuf);
  1558. /* Exit OTP access mode */
  1559. this->command(mtd, ONENAND_CMD_RESET, 0, 0);
  1560. this->wait(mtd, FL_RESETING);
  1561. return ret;
  1562. }
  1563. /**
  1564. * do_otp_lock - [DEFAULT] Lock OTP block area
  1565. * @param mtd MTD device structure
  1566. * @param from The offset to lock
  1567. * @param len number of bytes to lock
  1568. * @param retlen pointer to variable to store the number of lock bytes
  1569. * @param buf the databuffer to put/get data
  1570. *
  1571. * Lock OTP block area.
  1572. */
  1573. static int do_otp_lock(struct mtd_info *mtd, loff_t from, size_t len,
  1574. size_t *retlen, u_char *buf)
  1575. {
  1576. struct onenand_chip *this = mtd->priv;
  1577. int ret;
  1578. /* Enter OTP access mode */
  1579. this->command(mtd, ONENAND_CMD_OTP_ACCESS, 0, 0);
  1580. this->wait(mtd, FL_OTPING);
  1581. ret = onenand_do_write_oob(mtd, from, len, retlen, buf, MTD_OOB_PLACE);
  1582. /* Exit OTP access mode */
  1583. this->command(mtd, ONENAND_CMD_RESET, 0, 0);
  1584. this->wait(mtd, FL_RESETING);
  1585. return ret;
  1586. }
  1587. /**
  1588. * onenand_otp_walk - [DEFAULT] Handle OTP operation
  1589. * @param mtd MTD device structure
  1590. * @param from The offset to read/write
  1591. * @param len number of bytes to read/write
  1592. * @param retlen pointer to variable to store the number of read bytes
  1593. * @param buf the databuffer to put/get data
  1594. * @param action do given action
  1595. * @param mode specify user and factory
  1596. *
  1597. * Handle OTP operation.
  1598. */
  1599. static int onenand_otp_walk(struct mtd_info *mtd, loff_t from, size_t len,
  1600. size_t *retlen, u_char *buf,
  1601. otp_op_t action, int mode)
  1602. {
  1603. struct onenand_chip *this = mtd->priv;
  1604. int otp_pages;
  1605. int density;
  1606. int ret = 0;
  1607. *retlen = 0;
  1608. density = this->device_id >> ONENAND_DEVICE_DENSITY_SHIFT;
  1609. if (density < ONENAND_DEVICE_DENSITY_512Mb)
  1610. otp_pages = 20;
  1611. else
  1612. otp_pages = 10;
  1613. if (mode == MTD_OTP_FACTORY) {
  1614. from += mtd->writesize * otp_pages;
  1615. otp_pages = 64 - otp_pages;
  1616. }
  1617. /* Check User/Factory boundary */
  1618. if (((mtd->writesize * otp_pages) - (from + len)) < 0)
  1619. return 0;
  1620. while (len > 0 && otp_pages > 0) {
  1621. if (!action) { /* OTP Info functions */
  1622. struct otp_info *otpinfo;
  1623. len -= sizeof(struct otp_info);
  1624. if (len <= 0)
  1625. return -ENOSPC;
  1626. otpinfo = (struct otp_info *) buf;
  1627. otpinfo->start = from;
  1628. otpinfo->length = mtd->writesize;
  1629. otpinfo->locked = 0;
  1630. from += mtd->writesize;
  1631. buf += sizeof(struct otp_info);
  1632. *retlen += sizeof(struct otp_info);
  1633. } else {
  1634. size_t tmp_retlen;
  1635. int size = len;
  1636. ret = action(mtd, from, len, &tmp_retlen, buf);
  1637. buf += size;
  1638. len -= size;
  1639. *retlen += size;
  1640. if (ret < 0)
  1641. return ret;
  1642. }
  1643. otp_pages--;
  1644. }
  1645. return 0;
  1646. }
  1647. /**
  1648. * onenand_get_fact_prot_info - [MTD Interface] Read factory OTP info
  1649. * @param mtd MTD device structure
  1650. * @param buf the databuffer to put/get data
  1651. * @param len number of bytes to read
  1652. *
  1653. * Read factory OTP info.
  1654. */
  1655. static int onenand_get_fact_prot_info(struct mtd_info *mtd,
  1656. struct otp_info *buf, size_t len)
  1657. {
  1658. size_t retlen;
  1659. int ret;
  1660. ret = onenand_otp_walk(mtd, 0, len, &retlen, (u_char *) buf, NULL, MTD_OTP_FACTORY);
  1661. return ret ? : retlen;
  1662. }
  1663. /**
  1664. * onenand_read_fact_prot_reg - [MTD Interface] Read factory OTP area
  1665. * @param mtd MTD device structure
  1666. * @param from The offset to read
  1667. * @param len number of bytes to read
  1668. * @param retlen pointer to variable to store the number of read bytes
  1669. * @param buf the databuffer to put/get data
  1670. *
  1671. * Read factory OTP area.
  1672. */
  1673. static int onenand_read_fact_prot_reg(struct mtd_info *mtd, loff_t from,
  1674. size_t len, size_t *retlen, u_char *buf)
  1675. {
  1676. return onenand_otp_walk(mtd, from, len, retlen, buf, do_otp_read, MTD_OTP_FACTORY);
  1677. }
  1678. /**
  1679. * onenand_get_user_prot_info - [MTD Interface] Read user OTP info
  1680. * @param mtd MTD device structure
  1681. * @param buf the databuffer to put/get data
  1682. * @param len number of bytes to read
  1683. *
  1684. * Read user OTP info.
  1685. */
  1686. static int onenand_get_user_prot_info(struct mtd_info *mtd,
  1687. struct otp_info *buf, size_t len)
  1688. {
  1689. size_t retlen;
  1690. int ret;
  1691. ret = onenand_otp_walk(mtd, 0, len, &retlen, (u_char *) buf, NULL, MTD_OTP_USER);
  1692. return ret ? : retlen;
  1693. }
  1694. /**
  1695. * onenand_read_user_prot_reg - [MTD Interface] Read user OTP 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 read bytes
  1700. * @param buf the databuffer to put/get data
  1701. *
  1702. * Read user OTP area.
  1703. */
  1704. static int onenand_read_user_prot_reg(struct mtd_info *mtd, loff_t from,
  1705. size_t len, size_t *retlen, u_char *buf)
  1706. {
  1707. return onenand_otp_walk(mtd, from, len, retlen, buf, do_otp_read, MTD_OTP_USER);
  1708. }
  1709. /**
  1710. * onenand_write_user_prot_reg - [MTD Interface] Write user OTP area
  1711. * @param mtd MTD device structure
  1712. * @param from The offset to write
  1713. * @param len number of bytes to write
  1714. * @param retlen pointer to variable to store the number of write bytes
  1715. * @param buf the databuffer to put/get data
  1716. *
  1717. * Write user OTP area.
  1718. */
  1719. static int onenand_write_user_prot_reg(struct mtd_info *mtd, loff_t from,
  1720. size_t len, size_t *retlen, u_char *buf)
  1721. {
  1722. return onenand_otp_walk(mtd, from, len, retlen, buf, do_otp_write, MTD_OTP_USER);
  1723. }
  1724. /**
  1725. * onenand_lock_user_prot_reg - [MTD Interface] Lock user OTP area
  1726. * @param mtd MTD device structure
  1727. * @param from The offset to lock
  1728. * @param len number of bytes to unlock
  1729. *
  1730. * Write lock mark on spare area in page 0 in OTP block
  1731. */
  1732. static int onenand_lock_user_prot_reg(struct mtd_info *mtd, loff_t from,
  1733. size_t len)
  1734. {
  1735. unsigned char oob_buf[64];
  1736. size_t retlen;
  1737. int ret;
  1738. memset(oob_buf, 0xff, mtd->oobsize);
  1739. /*
  1740. * Note: OTP lock operation
  1741. * OTP block : 0xXXFC
  1742. * 1st block : 0xXXF3 (If chip support)
  1743. * Both : 0xXXF0 (If chip support)
  1744. */
  1745. oob_buf[ONENAND_OTP_LOCK_OFFSET] = 0xFC;
  1746. /*
  1747. * Write lock mark to 8th word of sector0 of page0 of the spare0.
  1748. * We write 16 bytes spare area instead of 2 bytes.
  1749. */
  1750. from = 0;
  1751. len = 16;
  1752. ret = onenand_otp_walk(mtd, from, len, &retlen, oob_buf, do_otp_lock, MTD_OTP_USER);
  1753. return ret ? : retlen;
  1754. }
  1755. #endif /* CONFIG_MTD_ONENAND_OTP */
  1756. /**
  1757. * onenand_check_features - Check and set OneNAND features
  1758. * @param mtd MTD data structure
  1759. *
  1760. * Check and set OneNAND features
  1761. * - lock scheme
  1762. */
  1763. static void onenand_check_features(struct mtd_info *mtd)
  1764. {
  1765. struct onenand_chip *this = mtd->priv;
  1766. unsigned int density, process;
  1767. /* Lock scheme depends on density and process */
  1768. density = this->device_id >> ONENAND_DEVICE_DENSITY_SHIFT;
  1769. process = this->version_id >> ONENAND_VERSION_PROCESS_SHIFT;
  1770. /* Lock scheme */
  1771. if (density >= ONENAND_DEVICE_DENSITY_1Gb) {
  1772. /* A-Die has all block unlock */
  1773. if (process) {
  1774. printk(KERN_DEBUG "Chip support all block unlock\n");
  1775. this->options |= ONENAND_HAS_UNLOCK_ALL;
  1776. }
  1777. } else {
  1778. /* Some OneNAND has continues lock scheme */
  1779. if (!process) {
  1780. printk(KERN_DEBUG "Lock scheme is Continues Lock\n");
  1781. this->options |= ONENAND_HAS_CONT_LOCK;
  1782. }
  1783. }
  1784. }
  1785. /**
  1786. * onenand_print_device_info - Print device & version ID
  1787. * @param device device ID
  1788. * @param version version ID
  1789. *
  1790. * Print device & version ID
  1791. */
  1792. static void onenand_print_device_info(int device, int version)
  1793. {
  1794. int vcc, demuxed, ddp, density;
  1795. vcc = device & ONENAND_DEVICE_VCC_MASK;
  1796. demuxed = device & ONENAND_DEVICE_IS_DEMUX;
  1797. ddp = device & ONENAND_DEVICE_IS_DDP;
  1798. density = device >> ONENAND_DEVICE_DENSITY_SHIFT;
  1799. printk(KERN_INFO "%sOneNAND%s %dMB %sV 16-bit (0x%02x)\n",
  1800. demuxed ? "" : "Muxed ",
  1801. ddp ? "(DDP)" : "",
  1802. (16 << density),
  1803. vcc ? "2.65/3.3" : "1.8",
  1804. device);
  1805. printk(KERN_DEBUG "OneNAND version = 0x%04x\n", version);
  1806. }
  1807. static const struct onenand_manufacturers onenand_manuf_ids[] = {
  1808. {ONENAND_MFR_SAMSUNG, "Samsung"},
  1809. };
  1810. /**
  1811. * onenand_check_maf - Check manufacturer ID
  1812. * @param manuf manufacturer ID
  1813. *
  1814. * Check manufacturer ID
  1815. */
  1816. static int onenand_check_maf(int manuf)
  1817. {
  1818. int size = ARRAY_SIZE(onenand_manuf_ids);
  1819. char *name;
  1820. int i;
  1821. for (i = 0; i < size; i++)
  1822. if (manuf == onenand_manuf_ids[i].id)
  1823. break;
  1824. if (i < size)
  1825. name = onenand_manuf_ids[i].name;
  1826. else
  1827. name = "Unknown";
  1828. printk(KERN_DEBUG "OneNAND Manufacturer: %s (0x%0x)\n", name, manuf);
  1829. return (i == size);
  1830. }
  1831. /**
  1832. * onenand_probe - [OneNAND Interface] Probe the OneNAND device
  1833. * @param mtd MTD device structure
  1834. *
  1835. * OneNAND detection method:
  1836. * Compare the the values from command with ones from register
  1837. */
  1838. static int onenand_probe(struct mtd_info *mtd)
  1839. {
  1840. struct onenand_chip *this = mtd->priv;
  1841. int bram_maf_id, bram_dev_id, maf_id, dev_id, ver_id;
  1842. int density;
  1843. int syscfg;
  1844. /* Save system configuration 1 */
  1845. syscfg = this->read_word(this->base + ONENAND_REG_SYS_CFG1);
  1846. /* Clear Sync. Burst Read mode to read BootRAM */
  1847. this->write_word((syscfg & ~ONENAND_SYS_CFG1_SYNC_READ), this->base + ONENAND_REG_SYS_CFG1);
  1848. /* Send the command for reading device ID from BootRAM */
  1849. this->write_word(ONENAND_CMD_READID, this->base + ONENAND_BOOTRAM);
  1850. /* Read manufacturer and device IDs from BootRAM */
  1851. bram_maf_id = this->read_word(this->base + ONENAND_BOOTRAM + 0x0);
  1852. bram_dev_id = this->read_word(this->base + ONENAND_BOOTRAM + 0x2);
  1853. /* Reset OneNAND to read default register values */
  1854. this->write_word(ONENAND_CMD_RESET, this->base + ONENAND_BOOTRAM);
  1855. /* Wait reset */
  1856. this->wait(mtd, FL_RESETING);
  1857. /* Restore system configuration 1 */
  1858. this->write_word(syscfg, this->base + ONENAND_REG_SYS_CFG1);
  1859. /* Check manufacturer ID */
  1860. if (onenand_check_maf(bram_maf_id))
  1861. return -ENXIO;
  1862. /* Read manufacturer and device IDs from Register */
  1863. maf_id = this->read_word(this->base + ONENAND_REG_MANUFACTURER_ID);
  1864. dev_id = this->read_word(this->base + ONENAND_REG_DEVICE_ID);
  1865. ver_id = this->read_word(this->base + ONENAND_REG_VERSION_ID);
  1866. /* Check OneNAND device */
  1867. if (maf_id != bram_maf_id || dev_id != bram_dev_id)
  1868. return -ENXIO;
  1869. /* Flash device information */
  1870. onenand_print_device_info(dev_id, ver_id);
  1871. this->device_id = dev_id;
  1872. this->version_id = ver_id;
  1873. density = dev_id >> ONENAND_DEVICE_DENSITY_SHIFT;
  1874. this->chipsize = (16 << density) << 20;
  1875. /* Set density mask. it is used for DDP */
  1876. if (ONENAND_IS_DDP(this))
  1877. this->density_mask = (1 << (density + 6));
  1878. else
  1879. this->density_mask = 0;
  1880. /* OneNAND page size & block size */
  1881. /* The data buffer size is equal to page size */
  1882. mtd->writesize = this->read_word(this->base + ONENAND_REG_DATA_BUFFER_SIZE);
  1883. mtd->oobsize = mtd->writesize >> 5;
  1884. /* Pages per a block are always 64 in OneNAND */
  1885. mtd->erasesize = mtd->writesize << 6;
  1886. this->erase_shift = ffs(mtd->erasesize) - 1;
  1887. this->page_shift = ffs(mtd->writesize) - 1;
  1888. this->page_mask = (1 << (this->erase_shift - this->page_shift)) - 1;
  1889. /* REVIST: Multichip handling */
  1890. mtd->size = this->chipsize;
  1891. /* Check OneNAND features */
  1892. onenand_check_features(mtd);
  1893. return 0;
  1894. }
  1895. /**
  1896. * onenand_suspend - [MTD Interface] Suspend the OneNAND flash
  1897. * @param mtd MTD device structure
  1898. */
  1899. static int onenand_suspend(struct mtd_info *mtd)
  1900. {
  1901. return onenand_get_device(mtd, FL_PM_SUSPENDED);
  1902. }
  1903. /**
  1904. * onenand_resume - [MTD Interface] Resume the OneNAND flash
  1905. * @param mtd MTD device structure
  1906. */
  1907. static void onenand_resume(struct mtd_info *mtd)
  1908. {
  1909. struct onenand_chip *this = mtd->priv;
  1910. if (this->state == FL_PM_SUSPENDED)
  1911. onenand_release_device(mtd);
  1912. else
  1913. printk(KERN_ERR "resume() called for the chip which is not"
  1914. "in suspended state\n");
  1915. }
  1916. /**
  1917. * onenand_scan - [OneNAND Interface] Scan for the OneNAND device
  1918. * @param mtd MTD device structure
  1919. * @param maxchips Number of chips to scan for
  1920. *
  1921. * This fills out all the not initialized function pointers
  1922. * with the defaults.
  1923. * The flash ID is read and the mtd/chip structures are
  1924. * filled with the appropriate values.
  1925. */
  1926. int onenand_scan(struct mtd_info *mtd, int maxchips)
  1927. {
  1928. int i;
  1929. struct onenand_chip *this = mtd->priv;
  1930. if (!this->read_word)
  1931. this->read_word = onenand_readw;
  1932. if (!this->write_word)
  1933. this->write_word = onenand_writew;
  1934. if (!this->command)
  1935. this->command = onenand_command;
  1936. if (!this->wait)
  1937. onenand_setup_wait(mtd);
  1938. if (!this->read_bufferram)
  1939. this->read_bufferram = onenand_read_bufferram;
  1940. if (!this->write_bufferram)
  1941. this->write_bufferram = onenand_write_bufferram;
  1942. if (!this->block_markbad)
  1943. this->block_markbad = onenand_default_block_markbad;
  1944. if (!this->scan_bbt)
  1945. this->scan_bbt = onenand_default_bbt;
  1946. if (onenand_probe(mtd))
  1947. return -ENXIO;
  1948. /* Set Sync. Burst Read after probing */
  1949. if (this->mmcontrol) {
  1950. printk(KERN_INFO "OneNAND Sync. Burst Read support\n");
  1951. this->read_bufferram = onenand_sync_read_bufferram;
  1952. }
  1953. /* Allocate buffers, if necessary */
  1954. if (!this->page_buf) {
  1955. size_t len;
  1956. len = mtd->writesize + mtd->oobsize;
  1957. this->page_buf = kmalloc(len, GFP_KERNEL);
  1958. if (!this->page_buf) {
  1959. printk(KERN_ERR "onenand_scan(): Can't allocate page_buf\n");
  1960. return -ENOMEM;
  1961. }
  1962. this->options |= ONENAND_PAGEBUF_ALLOC;
  1963. }
  1964. this->state = FL_READY;
  1965. init_waitqueue_head(&this->wq);
  1966. spin_lock_init(&this->chip_lock);
  1967. /*
  1968. * Allow subpage writes up to oobsize.
  1969. */
  1970. switch (mtd->oobsize) {
  1971. case 64:
  1972. this->ecclayout = &onenand_oob_64;
  1973. mtd->subpage_sft = 2;
  1974. break;
  1975. case 32:
  1976. this->ecclayout = &onenand_oob_32;
  1977. mtd->subpage_sft = 1;
  1978. break;
  1979. default:
  1980. printk(KERN_WARNING "No OOB scheme defined for oobsize %d\n",
  1981. mtd->oobsize);
  1982. mtd->subpage_sft = 0;
  1983. /* To prevent kernel oops */
  1984. this->ecclayout = &onenand_oob_32;
  1985. break;
  1986. }
  1987. this->subpagesize = mtd->writesize >> mtd->subpage_sft;
  1988. /*
  1989. * The number of bytes available for a client to place data into
  1990. * the out of band area
  1991. */
  1992. this->ecclayout->oobavail = 0;
  1993. for (i = 0; this->ecclayout->oobfree[i].length; i++)
  1994. this->ecclayout->oobavail +=
  1995. this->ecclayout->oobfree[i].length;
  1996. mtd->oobavail = this->ecclayout->oobavail;
  1997. mtd->ecclayout = this->ecclayout;
  1998. /* Fill in remaining MTD driver data */
  1999. mtd->type = MTD_NANDFLASH;
  2000. mtd->flags = MTD_CAP_NANDFLASH;
  2001. mtd->erase = onenand_erase;
  2002. mtd->point = NULL;
  2003. mtd->unpoint = NULL;
  2004. mtd->read = onenand_read;
  2005. mtd->write = onenand_write;
  2006. mtd->read_oob = onenand_read_oob;
  2007. mtd->write_oob = onenand_write_oob;
  2008. #ifdef CONFIG_MTD_ONENAND_OTP
  2009. mtd->get_fact_prot_info = onenand_get_fact_prot_info;
  2010. mtd->read_fact_prot_reg = onenand_read_fact_prot_reg;
  2011. mtd->get_user_prot_info = onenand_get_user_prot_info;
  2012. mtd->read_user_prot_reg = onenand_read_user_prot_reg;
  2013. mtd->write_user_prot_reg = onenand_write_user_prot_reg;
  2014. mtd->lock_user_prot_reg = onenand_lock_user_prot_reg;
  2015. #endif
  2016. mtd->sync = onenand_sync;
  2017. mtd->lock = onenand_lock;
  2018. mtd->unlock = onenand_unlock;
  2019. mtd->suspend = onenand_suspend;
  2020. mtd->resume = onenand_resume;
  2021. mtd->block_isbad = onenand_block_isbad;
  2022. mtd->block_markbad = onenand_block_markbad;
  2023. mtd->owner = THIS_MODULE;
  2024. /* Unlock whole block */
  2025. onenand_unlock_all(mtd);
  2026. return this->scan_bbt(mtd);
  2027. }
  2028. /**
  2029. * onenand_release - [OneNAND Interface] Free resources held by the OneNAND device
  2030. * @param mtd MTD device structure
  2031. */
  2032. void onenand_release(struct mtd_info *mtd)
  2033. {
  2034. struct onenand_chip *this = mtd->priv;
  2035. #ifdef CONFIG_MTD_PARTITIONS
  2036. /* Deregister partitions */
  2037. del_mtd_partitions (mtd);
  2038. #endif
  2039. /* Deregister the device */
  2040. del_mtd_device (mtd);
  2041. /* Free bad block table memory, if allocated */
  2042. if (this->bbm) {
  2043. struct bbm_info *bbm = this->bbm;
  2044. kfree(bbm->bbt);
  2045. kfree(this->bbm);
  2046. }
  2047. /* Buffer allocated by onenand_scan */
  2048. if (this->options & ONENAND_PAGEBUF_ALLOC)
  2049. kfree(this->page_buf);
  2050. }
  2051. EXPORT_SYMBOL_GPL(onenand_scan);
  2052. EXPORT_SYMBOL_GPL(onenand_release);
  2053. MODULE_LICENSE("GPL");
  2054. MODULE_AUTHOR("Kyungmin Park <kyungmin.park@samsung.com>");
  2055. MODULE_DESCRIPTION("Generic OneNAND flash driver code");