onenand_base.c 56 KB

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