onenand_base.c 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151
  1. /*
  2. * linux/drivers/mtd/onenand/onenand_base.c
  3. *
  4. * Copyright (C) 2005-2006 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);
  490. page &= this->page_mask;
  491. i = ONENAND_CURRENT_BUFFERRAM(this);
  492. /* Is there valid data? */
  493. if (this->bufferram[i].block == block &&
  494. this->bufferram[i].page == page &&
  495. this->bufferram[i].valid)
  496. return 1;
  497. return 0;
  498. }
  499. /**
  500. * onenand_update_bufferram - [GENERIC] Update BufferRAM information
  501. * @param mtd MTD data structure
  502. * @param addr address to update
  503. * @param valid valid flag
  504. *
  505. * Update BufferRAM information
  506. */
  507. static int onenand_update_bufferram(struct mtd_info *mtd, loff_t addr,
  508. int valid)
  509. {
  510. struct onenand_chip *this = mtd->priv;
  511. int block, page;
  512. int i;
  513. block = (int) (addr >> this->erase_shift);
  514. page = (int) (addr >> this->page_shift);
  515. page &= this->page_mask;
  516. /* Invalidate BufferRAM */
  517. for (i = 0; i < MAX_BUFFERRAM; i++) {
  518. if (this->bufferram[i].block == block &&
  519. this->bufferram[i].page == page)
  520. this->bufferram[i].valid = 0;
  521. }
  522. /* Update BufferRAM */
  523. i = ONENAND_CURRENT_BUFFERRAM(this);
  524. this->bufferram[i].block = block;
  525. this->bufferram[i].page = page;
  526. this->bufferram[i].valid = valid;
  527. return 0;
  528. }
  529. /**
  530. * onenand_get_device - [GENERIC] Get chip for selected access
  531. * @param mtd MTD device structure
  532. * @param new_state the state which is requested
  533. *
  534. * Get the device and lock it for exclusive access
  535. */
  536. static int onenand_get_device(struct mtd_info *mtd, int new_state)
  537. {
  538. struct onenand_chip *this = mtd->priv;
  539. DECLARE_WAITQUEUE(wait, current);
  540. /*
  541. * Grab the lock and see if the device is available
  542. */
  543. while (1) {
  544. spin_lock(&this->chip_lock);
  545. if (this->state == FL_READY) {
  546. this->state = new_state;
  547. spin_unlock(&this->chip_lock);
  548. break;
  549. }
  550. if (new_state == FL_PM_SUSPENDED) {
  551. spin_unlock(&this->chip_lock);
  552. return (this->state == FL_PM_SUSPENDED) ? 0 : -EAGAIN;
  553. }
  554. set_current_state(TASK_UNINTERRUPTIBLE);
  555. add_wait_queue(&this->wq, &wait);
  556. spin_unlock(&this->chip_lock);
  557. schedule();
  558. remove_wait_queue(&this->wq, &wait);
  559. }
  560. return 0;
  561. }
  562. /**
  563. * onenand_release_device - [GENERIC] release chip
  564. * @param mtd MTD device structure
  565. *
  566. * Deselect, release chip lock and wake up anyone waiting on the device
  567. */
  568. static void onenand_release_device(struct mtd_info *mtd)
  569. {
  570. struct onenand_chip *this = mtd->priv;
  571. /* Release the chip */
  572. spin_lock(&this->chip_lock);
  573. this->state = FL_READY;
  574. wake_up(&this->wq);
  575. spin_unlock(&this->chip_lock);
  576. }
  577. /**
  578. * onenand_read - [MTD Interface] Read data from flash
  579. * @param mtd MTD device structure
  580. * @param from offset to read from
  581. * @param len number of bytes to read
  582. * @param retlen pointer to variable to store the number of read bytes
  583. * @param buf the databuffer to put data
  584. *
  585. * Read with ecc
  586. */
  587. static int onenand_read(struct mtd_info *mtd, loff_t from, size_t len,
  588. size_t *retlen, u_char *buf)
  589. {
  590. struct onenand_chip *this = mtd->priv;
  591. struct mtd_ecc_stats stats;
  592. int read = 0, column;
  593. int thislen;
  594. int ret = 0, boundary = 0;
  595. DEBUG(MTD_DEBUG_LEVEL3, "onenand_read: from = 0x%08x, len = %i\n", (unsigned int) from, (int) len);
  596. /* Do not allow reads past end of device */
  597. if ((from + len) > mtd->size) {
  598. DEBUG(MTD_DEBUG_LEVEL0, "onenand_read: Attempt read beyond end of device\n");
  599. *retlen = 0;
  600. return -EINVAL;
  601. }
  602. /* Grab the lock and see if the device is available */
  603. onenand_get_device(mtd, FL_READING);
  604. /* TODO handling oob */
  605. stats = mtd->ecc_stats;
  606. /* Read-while-load method */
  607. /* Do first load to bufferRAM */
  608. if (read < len) {
  609. if (!onenand_check_bufferram(mtd, from)) {
  610. this->command(mtd, ONENAND_CMD_READ, from, mtd->writesize);
  611. ret = this->wait(mtd, FL_READING);
  612. onenand_update_bufferram(mtd, from, !ret);
  613. }
  614. }
  615. thislen = min_t(int, mtd->writesize, len - read);
  616. column = from & (mtd->writesize - 1);
  617. if (column + thislen > mtd->writesize)
  618. thislen = mtd->writesize - column;
  619. while (!ret) {
  620. /* If there is more to load then start next load */
  621. from += thislen;
  622. if (read + thislen < len) {
  623. this->command(mtd, ONENAND_CMD_READ, from, mtd->writesize);
  624. /*
  625. * Chip boundary handling in DDP
  626. * Now we issued chip 1 read and pointed chip 1
  627. * bufferam so we have to point chip 0 bufferam.
  628. */
  629. if (ONENAND_IS_DDP(this) &&
  630. unlikely(from == (this->chipsize >> 1))) {
  631. this->write_word(ONENAND_DDP_CHIP0, this->base + ONENAND_REG_START_ADDRESS2);
  632. boundary = 1;
  633. } else
  634. boundary = 0;
  635. ONENAND_SET_PREV_BUFFERRAM(this);
  636. }
  637. /* While load is going, read from last bufferRAM */
  638. this->read_bufferram(mtd, ONENAND_DATARAM, buf, column, thislen);
  639. /* See if we are done */
  640. read += thislen;
  641. if (read == len)
  642. break;
  643. /* Set up for next read from bufferRAM */
  644. if (unlikely(boundary))
  645. this->write_word(ONENAND_DDP_CHIP1, this->base + ONENAND_REG_START_ADDRESS2);
  646. ONENAND_SET_NEXT_BUFFERRAM(this);
  647. buf += thislen;
  648. thislen = min_t(int, mtd->writesize, len - read);
  649. column = 0;
  650. cond_resched();
  651. /* Now wait for load */
  652. ret = this->wait(mtd, FL_READING);
  653. onenand_update_bufferram(mtd, from, !ret);
  654. }
  655. /* Deselect and wake up anyone waiting on the device */
  656. onenand_release_device(mtd);
  657. /*
  658. * Return success, if no ECC failures, else -EBADMSG
  659. * fs driver will take care of that, because
  660. * retlen == desired len and result == -EBADMSG
  661. */
  662. *retlen = read;
  663. if (mtd->ecc_stats.failed - stats.failed)
  664. return -EBADMSG;
  665. if (ret)
  666. return ret;
  667. return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
  668. }
  669. /**
  670. * onenand_do_read_oob - [MTD Interface] OneNAND read out-of-band
  671. * @param mtd MTD device structure
  672. * @param from offset to read from
  673. * @param len number of bytes to read
  674. * @param retlen pointer to variable to store the number of read bytes
  675. * @param buf the databuffer to put data
  676. *
  677. * OneNAND read out-of-band data from the spare area
  678. */
  679. int onenand_do_read_oob(struct mtd_info *mtd, loff_t from, size_t len,
  680. size_t *retlen, u_char *buf)
  681. {
  682. struct onenand_chip *this = mtd->priv;
  683. int read = 0, thislen, column;
  684. int ret = 0;
  685. DEBUG(MTD_DEBUG_LEVEL3, "onenand_read_oob: from = 0x%08x, len = %i\n", (unsigned int) from, (int) len);
  686. /* Initialize return length value */
  687. *retlen = 0;
  688. /* Do not allow reads past end of device */
  689. if (unlikely((from + len) > mtd->size)) {
  690. DEBUG(MTD_DEBUG_LEVEL0, "onenand_read_oob: Attempt read beyond end of device\n");
  691. return -EINVAL;
  692. }
  693. /* Grab the lock and see if the device is available */
  694. onenand_get_device(mtd, FL_READING);
  695. column = from & (mtd->oobsize - 1);
  696. while (read < len) {
  697. cond_resched();
  698. thislen = mtd->oobsize - column;
  699. thislen = min_t(int, thislen, len);
  700. this->command(mtd, ONENAND_CMD_READOOB, from, mtd->oobsize);
  701. onenand_update_bufferram(mtd, from, 0);
  702. ret = this->wait(mtd, FL_READING);
  703. /* First copy data and check return value for ECC handling */
  704. this->read_bufferram(mtd, ONENAND_SPARERAM, buf, column, thislen);
  705. if (ret) {
  706. DEBUG(MTD_DEBUG_LEVEL0, "onenand_read_oob: read failed = 0x%x\n", ret);
  707. goto out;
  708. }
  709. read += thislen;
  710. if (read == len)
  711. break;
  712. buf += thislen;
  713. /* Read more? */
  714. if (read < len) {
  715. /* Page size */
  716. from += mtd->writesize;
  717. column = 0;
  718. }
  719. }
  720. out:
  721. /* Deselect and wake up anyone waiting on the device */
  722. onenand_release_device(mtd);
  723. *retlen = read;
  724. return ret;
  725. }
  726. /**
  727. * onenand_read_oob - [MTD Interface] NAND write data and/or out-of-band
  728. * @mtd: MTD device structure
  729. * @from: offset to read from
  730. * @ops: oob operation description structure
  731. */
  732. static int onenand_read_oob(struct mtd_info *mtd, loff_t from,
  733. struct mtd_oob_ops *ops)
  734. {
  735. BUG_ON(ops->mode != MTD_OOB_PLACE);
  736. return onenand_do_read_oob(mtd, from + ops->ooboffs, ops->ooblen,
  737. &ops->oobretlen, ops->oobbuf);
  738. }
  739. #ifdef CONFIG_MTD_ONENAND_VERIFY_WRITE
  740. /**
  741. * onenand_verify_oob - [GENERIC] verify the oob contents after a write
  742. * @param mtd MTD device structure
  743. * @param buf the databuffer to verify
  744. * @param to offset to read from
  745. * @param len number of bytes to read and compare
  746. *
  747. */
  748. static int onenand_verify_oob(struct mtd_info *mtd, const u_char *buf, loff_t to, int len)
  749. {
  750. struct onenand_chip *this = mtd->priv;
  751. char *readp = this->page_buf;
  752. int column = to & (mtd->oobsize - 1);
  753. int status, i;
  754. this->command(mtd, ONENAND_CMD_READOOB, to, mtd->oobsize);
  755. onenand_update_bufferram(mtd, to, 0);
  756. status = this->wait(mtd, FL_READING);
  757. if (status)
  758. return status;
  759. this->read_bufferram(mtd, ONENAND_SPARERAM, readp, column, len);
  760. for(i = 0; i < len; i++)
  761. if (buf[i] != 0xFF && buf[i] != readp[i])
  762. return -EBADMSG;
  763. return 0;
  764. }
  765. /**
  766. * onenand_verify_page - [GENERIC] verify the chip contents after a write
  767. * @param mtd MTD device structure
  768. * @param buf the databuffer to verify
  769. *
  770. * Check DataRAM area directly
  771. */
  772. static int onenand_verify_page(struct mtd_info *mtd, u_char *buf, loff_t addr)
  773. {
  774. struct onenand_chip *this = mtd->priv;
  775. void __iomem *dataram0, *dataram1;
  776. int ret = 0;
  777. /* In partial page write, just skip it */
  778. if ((addr & (mtd->writesize - 1)) != 0)
  779. return 0;
  780. this->command(mtd, ONENAND_CMD_READ, addr, mtd->writesize);
  781. ret = this->wait(mtd, FL_READING);
  782. if (ret)
  783. return ret;
  784. onenand_update_bufferram(mtd, addr, 1);
  785. /* Check, if the two dataram areas are same */
  786. dataram0 = this->base + ONENAND_DATARAM;
  787. dataram1 = dataram0 + mtd->writesize;
  788. if (memcmp(dataram0, dataram1, mtd->writesize))
  789. return -EBADMSG;
  790. return 0;
  791. }
  792. #else
  793. #define onenand_verify_page(...) (0)
  794. #define onenand_verify_oob(...) (0)
  795. #endif
  796. #define NOTALIGNED(x) ((x & (this->subpagesize - 1)) != 0)
  797. /**
  798. * onenand_write - [MTD Interface] write buffer to FLASH
  799. * @param mtd MTD device structure
  800. * @param to offset to write to
  801. * @param len number of bytes to write
  802. * @param retlen pointer to variable to store the number of written bytes
  803. * @param buf the data to write
  804. *
  805. * Write with ECC
  806. */
  807. static int onenand_write(struct mtd_info *mtd, loff_t to, size_t len,
  808. size_t *retlen, const u_char *buf)
  809. {
  810. struct onenand_chip *this = mtd->priv;
  811. int written = 0;
  812. int ret = 0;
  813. int column, subpage;
  814. DEBUG(MTD_DEBUG_LEVEL3, "onenand_write: to = 0x%08x, len = %i\n", (unsigned int) to, (int) len);
  815. /* Initialize retlen, in case of early exit */
  816. *retlen = 0;
  817. /* Do not allow writes past end of device */
  818. if (unlikely((to + len) > mtd->size)) {
  819. DEBUG(MTD_DEBUG_LEVEL0, "onenand_write: Attempt write to past end of device\n");
  820. return -EINVAL;
  821. }
  822. /* Reject writes, which are not page aligned */
  823. if (unlikely(NOTALIGNED(to)) || unlikely(NOTALIGNED(len))) {
  824. DEBUG(MTD_DEBUG_LEVEL0, "onenand_write: Attempt to write not page aligned data\n");
  825. return -EINVAL;
  826. }
  827. column = to & (mtd->writesize - 1);
  828. subpage = column || (len & (mtd->writesize - 1));
  829. /* Grab the lock and see if the device is available */
  830. onenand_get_device(mtd, FL_WRITING);
  831. /* Loop until all data write */
  832. while (written < len) {
  833. int bytes = mtd->writesize;
  834. int thislen = min_t(int, bytes, len - written);
  835. u_char *wbuf = (u_char *) buf;
  836. cond_resched();
  837. this->command(mtd, ONENAND_CMD_BUFFERRAM, to, bytes);
  838. /* Partial page write */
  839. if (subpage) {
  840. bytes = min_t(int, bytes - column, (int) len);
  841. memset(this->page_buf, 0xff, mtd->writesize);
  842. memcpy(this->page_buf + column, buf, bytes);
  843. wbuf = this->page_buf;
  844. /* Even though partial write, we need page size */
  845. thislen = mtd->writesize;
  846. }
  847. this->write_bufferram(mtd, ONENAND_DATARAM, wbuf, 0, thislen);
  848. this->write_bufferram(mtd, ONENAND_SPARERAM, ffchars, 0, mtd->oobsize);
  849. this->command(mtd, ONENAND_CMD_PROG, to, mtd->writesize);
  850. /* In partial page write we don't update bufferram */
  851. onenand_update_bufferram(mtd, to, !subpage);
  852. ret = this->wait(mtd, FL_WRITING);
  853. if (ret) {
  854. DEBUG(MTD_DEBUG_LEVEL0, "onenand_write: write filaed %d\n", ret);
  855. break;
  856. }
  857. /* Only check verify write turn on */
  858. ret = onenand_verify_page(mtd, (u_char *) wbuf, to);
  859. if (ret) {
  860. DEBUG(MTD_DEBUG_LEVEL0, "onenand_write: verify failed %d\n", ret);
  861. break;
  862. }
  863. written += thislen;
  864. if (written == len)
  865. break;
  866. column = 0;
  867. to += thislen;
  868. buf += thislen;
  869. }
  870. /* Deselect and wake up anyone waiting on the device */
  871. onenand_release_device(mtd);
  872. *retlen = written;
  873. return ret;
  874. }
  875. /**
  876. * onenand_do_write_oob - [Internal] OneNAND write out-of-band
  877. * @param mtd MTD device structure
  878. * @param to offset to write to
  879. * @param len number of bytes to write
  880. * @param retlen pointer to variable to store the number of written bytes
  881. * @param buf the data to write
  882. *
  883. * OneNAND write out-of-band
  884. */
  885. static int onenand_do_write_oob(struct mtd_info *mtd, loff_t to, size_t len,
  886. size_t *retlen, const u_char *buf)
  887. {
  888. struct onenand_chip *this = mtd->priv;
  889. int column, ret = 0;
  890. int written = 0;
  891. DEBUG(MTD_DEBUG_LEVEL3, "onenand_write_oob: to = 0x%08x, len = %i\n", (unsigned int) to, (int) len);
  892. /* Initialize retlen, in case of early exit */
  893. *retlen = 0;
  894. /* Do not allow writes past end of device */
  895. if (unlikely((to + len) > mtd->size)) {
  896. DEBUG(MTD_DEBUG_LEVEL0, "onenand_write_oob: Attempt write to past end of device\n");
  897. return -EINVAL;
  898. }
  899. /* Grab the lock and see if the device is available */
  900. onenand_get_device(mtd, FL_WRITING);
  901. /* Loop until all data write */
  902. while (written < len) {
  903. int thislen = min_t(int, mtd->oobsize, len - written);
  904. cond_resched();
  905. column = to & (mtd->oobsize - 1);
  906. this->command(mtd, ONENAND_CMD_BUFFERRAM, to, mtd->oobsize);
  907. /* We send data to spare ram with oobsize
  908. * to prevent byte access */
  909. memset(this->page_buf, 0xff, mtd->oobsize);
  910. memcpy(this->page_buf + column, buf, thislen);
  911. this->write_bufferram(mtd, ONENAND_SPARERAM, this->page_buf, 0, mtd->oobsize);
  912. this->command(mtd, ONENAND_CMD_PROGOOB, to, mtd->oobsize);
  913. onenand_update_bufferram(mtd, to, 0);
  914. ret = this->wait(mtd, FL_WRITING);
  915. if (ret) {
  916. DEBUG(MTD_DEBUG_LEVEL0, "onenand_write_oob: write filaed %d\n", ret);
  917. goto out;
  918. }
  919. ret = onenand_verify_oob(mtd, buf, to, thislen);
  920. if (ret) {
  921. DEBUG(MTD_DEBUG_LEVEL0, "onenand_write_oob: verify failed %d\n", ret);
  922. goto out;
  923. }
  924. written += thislen;
  925. if (written == len)
  926. break;
  927. to += thislen;
  928. buf += thislen;
  929. }
  930. out:
  931. /* Deselect and wake up anyone waiting on the device */
  932. onenand_release_device(mtd);
  933. *retlen = written;
  934. return ret;
  935. }
  936. /**
  937. * onenand_write_oob - [MTD Interface] NAND write data and/or out-of-band
  938. * @mtd: MTD device structure
  939. * @from: offset to read from
  940. * @ops: oob operation description structure
  941. */
  942. static int onenand_write_oob(struct mtd_info *mtd, loff_t to,
  943. struct mtd_oob_ops *ops)
  944. {
  945. BUG_ON(ops->mode != MTD_OOB_PLACE);
  946. return onenand_do_write_oob(mtd, to + ops->ooboffs, ops->ooblen,
  947. &ops->oobretlen, ops->oobbuf);
  948. }
  949. /**
  950. * onenand_block_checkbad - [GENERIC] Check if a block is marked bad
  951. * @param mtd MTD device structure
  952. * @param ofs offset from device start
  953. * @param getchip 0, if the chip is already selected
  954. * @param allowbbt 1, if its allowed to access the bbt area
  955. *
  956. * Check, if the block is bad. Either by reading the bad block table or
  957. * calling of the scan function.
  958. */
  959. static int onenand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int getchip, int allowbbt)
  960. {
  961. struct onenand_chip *this = mtd->priv;
  962. struct bbm_info *bbm = this->bbm;
  963. /* Return info from the table */
  964. return bbm->isbad_bbt(mtd, ofs, allowbbt);
  965. }
  966. /**
  967. * onenand_erase - [MTD Interface] erase block(s)
  968. * @param mtd MTD device structure
  969. * @param instr erase instruction
  970. *
  971. * Erase one ore more blocks
  972. */
  973. static int onenand_erase(struct mtd_info *mtd, struct erase_info *instr)
  974. {
  975. struct onenand_chip *this = mtd->priv;
  976. unsigned int block_size;
  977. loff_t addr;
  978. int len;
  979. int ret = 0;
  980. DEBUG(MTD_DEBUG_LEVEL3, "onenand_erase: start = 0x%08x, len = %i\n", (unsigned int) instr->addr, (unsigned int) instr->len);
  981. block_size = (1 << this->erase_shift);
  982. /* Start address must align on block boundary */
  983. if (unlikely(instr->addr & (block_size - 1))) {
  984. DEBUG(MTD_DEBUG_LEVEL0, "onenand_erase: Unaligned address\n");
  985. return -EINVAL;
  986. }
  987. /* Length must align on block boundary */
  988. if (unlikely(instr->len & (block_size - 1))) {
  989. DEBUG(MTD_DEBUG_LEVEL0, "onenand_erase: Length not block aligned\n");
  990. return -EINVAL;
  991. }
  992. /* Do not allow erase past end of device */
  993. if (unlikely((instr->len + instr->addr) > mtd->size)) {
  994. DEBUG(MTD_DEBUG_LEVEL0, "onenand_erase: Erase past end of device\n");
  995. return -EINVAL;
  996. }
  997. instr->fail_addr = 0xffffffff;
  998. /* Grab the lock and see if the device is available */
  999. onenand_get_device(mtd, FL_ERASING);
  1000. /* Loop throught the pages */
  1001. len = instr->len;
  1002. addr = instr->addr;
  1003. instr->state = MTD_ERASING;
  1004. while (len) {
  1005. cond_resched();
  1006. /* Check if we have a bad block, we do not erase bad blocks */
  1007. if (onenand_block_checkbad(mtd, addr, 0, 0)) {
  1008. printk (KERN_WARNING "onenand_erase: attempt to erase a bad block at addr 0x%08x\n", (unsigned int) addr);
  1009. instr->state = MTD_ERASE_FAILED;
  1010. goto erase_exit;
  1011. }
  1012. this->command(mtd, ONENAND_CMD_ERASE, addr, block_size);
  1013. ret = this->wait(mtd, FL_ERASING);
  1014. /* Check, if it is write protected */
  1015. if (ret) {
  1016. DEBUG(MTD_DEBUG_LEVEL0, "onenand_erase: Failed erase, block %d\n", (unsigned) (addr >> this->erase_shift));
  1017. instr->state = MTD_ERASE_FAILED;
  1018. instr->fail_addr = addr;
  1019. goto erase_exit;
  1020. }
  1021. len -= block_size;
  1022. addr += block_size;
  1023. }
  1024. instr->state = MTD_ERASE_DONE;
  1025. erase_exit:
  1026. ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
  1027. /* Do call back function */
  1028. if (!ret)
  1029. mtd_erase_callback(instr);
  1030. /* Deselect and wake up anyone waiting on the device */
  1031. onenand_release_device(mtd);
  1032. return ret;
  1033. }
  1034. /**
  1035. * onenand_sync - [MTD Interface] sync
  1036. * @param mtd MTD device structure
  1037. *
  1038. * Sync is actually a wait for chip ready function
  1039. */
  1040. static void onenand_sync(struct mtd_info *mtd)
  1041. {
  1042. DEBUG(MTD_DEBUG_LEVEL3, "onenand_sync: called\n");
  1043. /* Grab the lock and see if the device is available */
  1044. onenand_get_device(mtd, FL_SYNCING);
  1045. /* Release it and go back */
  1046. onenand_release_device(mtd);
  1047. }
  1048. /**
  1049. * onenand_block_isbad - [MTD Interface] Check whether the block at the given offset is bad
  1050. * @param mtd MTD device structure
  1051. * @param ofs offset relative to mtd start
  1052. *
  1053. * Check whether the block is bad
  1054. */
  1055. static int onenand_block_isbad(struct mtd_info *mtd, loff_t ofs)
  1056. {
  1057. /* Check for invalid offset */
  1058. if (ofs > mtd->size)
  1059. return -EINVAL;
  1060. return onenand_block_checkbad(mtd, ofs, 1, 0);
  1061. }
  1062. /**
  1063. * onenand_default_block_markbad - [DEFAULT] mark a block bad
  1064. * @param mtd MTD device structure
  1065. * @param ofs offset from device start
  1066. *
  1067. * This is the default implementation, which can be overridden by
  1068. * a hardware specific driver.
  1069. */
  1070. static int onenand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
  1071. {
  1072. struct onenand_chip *this = mtd->priv;
  1073. struct bbm_info *bbm = this->bbm;
  1074. u_char buf[2] = {0, 0};
  1075. size_t retlen;
  1076. int block;
  1077. /* Get block number */
  1078. block = ((int) ofs) >> bbm->bbt_erase_shift;
  1079. if (bbm->bbt)
  1080. bbm->bbt[block >> 2] |= 0x01 << ((block & 0x03) << 1);
  1081. /* We write two bytes, so we dont have to mess with 16 bit access */
  1082. ofs += mtd->oobsize + (bbm->badblockpos & ~0x01);
  1083. return onenand_do_write_oob(mtd, ofs , 2, &retlen, buf);
  1084. }
  1085. /**
  1086. * onenand_block_markbad - [MTD Interface] Mark the block at the given offset as bad
  1087. * @param mtd MTD device structure
  1088. * @param ofs offset relative to mtd start
  1089. *
  1090. * Mark the block as bad
  1091. */
  1092. static int onenand_block_markbad(struct mtd_info *mtd, loff_t ofs)
  1093. {
  1094. struct onenand_chip *this = mtd->priv;
  1095. int ret;
  1096. ret = onenand_block_isbad(mtd, ofs);
  1097. if (ret) {
  1098. /* If it was bad already, return success and do nothing */
  1099. if (ret > 0)
  1100. return 0;
  1101. return ret;
  1102. }
  1103. return this->block_markbad(mtd, ofs);
  1104. }
  1105. /**
  1106. * onenand_do_lock_cmd - [OneNAND Interface] Lock or unlock block(s)
  1107. * @param mtd MTD device structure
  1108. * @param ofs offset relative to mtd start
  1109. * @param len number of bytes to lock or unlock
  1110. *
  1111. * Lock or unlock one or more blocks
  1112. */
  1113. static int onenand_do_lock_cmd(struct mtd_info *mtd, loff_t ofs, size_t len, int cmd)
  1114. {
  1115. struct onenand_chip *this = mtd->priv;
  1116. int start, end, block, value, status;
  1117. int wp_status_mask;
  1118. start = ofs >> this->erase_shift;
  1119. end = len >> this->erase_shift;
  1120. if (cmd == ONENAND_CMD_LOCK)
  1121. wp_status_mask = ONENAND_WP_LS;
  1122. else
  1123. wp_status_mask = ONENAND_WP_US;
  1124. /* Continuous lock scheme */
  1125. if (this->options & ONENAND_HAS_CONT_LOCK) {
  1126. /* Set start block address */
  1127. this->write_word(start, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
  1128. /* Set end block address */
  1129. this->write_word(start + end - 1, this->base + ONENAND_REG_END_BLOCK_ADDRESS);
  1130. /* Write lock command */
  1131. this->command(mtd, cmd, 0, 0);
  1132. /* There's no return value */
  1133. this->wait(mtd, FL_LOCKING);
  1134. /* Sanity check */
  1135. while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
  1136. & ONENAND_CTRL_ONGO)
  1137. continue;
  1138. /* Check lock status */
  1139. status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
  1140. if (!(status & wp_status_mask))
  1141. printk(KERN_ERR "wp status = 0x%x\n", status);
  1142. return 0;
  1143. }
  1144. /* Block lock scheme */
  1145. for (block = start; block < start + end; block++) {
  1146. /* Set block address */
  1147. value = onenand_block_address(this, block);
  1148. this->write_word(value, this->base + ONENAND_REG_START_ADDRESS1);
  1149. /* Select DataRAM for DDP */
  1150. value = onenand_bufferram_address(this, block);
  1151. this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
  1152. /* Set start block address */
  1153. this->write_word(block, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
  1154. /* Write lock command */
  1155. this->command(mtd, cmd, 0, 0);
  1156. /* There's no return value */
  1157. this->wait(mtd, FL_LOCKING);
  1158. /* Sanity check */
  1159. while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
  1160. & ONENAND_CTRL_ONGO)
  1161. continue;
  1162. /* Check lock status */
  1163. status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
  1164. if (!(status & wp_status_mask))
  1165. printk(KERN_ERR "block = %d, wp status = 0x%x\n", block, status);
  1166. }
  1167. return 0;
  1168. }
  1169. /**
  1170. * onenand_lock - [MTD Interface] Lock block(s)
  1171. * @param mtd MTD device structure
  1172. * @param ofs offset relative to mtd start
  1173. * @param len number of bytes to unlock
  1174. *
  1175. * Lock one or more blocks
  1176. */
  1177. static int onenand_lock(struct mtd_info *mtd, loff_t ofs, size_t len)
  1178. {
  1179. return onenand_do_lock_cmd(mtd, ofs, len, ONENAND_CMD_LOCK);
  1180. }
  1181. /**
  1182. * onenand_unlock - [MTD Interface] Unlock block(s)
  1183. * @param mtd MTD device structure
  1184. * @param ofs offset relative to mtd start
  1185. * @param len number of bytes to unlock
  1186. *
  1187. * Unlock one or more blocks
  1188. */
  1189. static int onenand_unlock(struct mtd_info *mtd, loff_t ofs, size_t len)
  1190. {
  1191. return onenand_do_lock_cmd(mtd, ofs, len, ONENAND_CMD_UNLOCK);
  1192. }
  1193. /**
  1194. * onenand_check_lock_status - [OneNAND Interface] Check lock status
  1195. * @param this onenand chip data structure
  1196. *
  1197. * Check lock status
  1198. */
  1199. static void onenand_check_lock_status(struct onenand_chip *this)
  1200. {
  1201. unsigned int value, block, status;
  1202. unsigned int end;
  1203. end = this->chipsize >> this->erase_shift;
  1204. for (block = 0; block < end; block++) {
  1205. /* Set block address */
  1206. value = onenand_block_address(this, block);
  1207. this->write_word(value, this->base + ONENAND_REG_START_ADDRESS1);
  1208. /* Select DataRAM for DDP */
  1209. value = onenand_bufferram_address(this, block);
  1210. this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
  1211. /* Set start block address */
  1212. this->write_word(block, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
  1213. /* Check lock status */
  1214. status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
  1215. if (!(status & ONENAND_WP_US))
  1216. printk(KERN_ERR "block = %d, wp status = 0x%x\n", block, status);
  1217. }
  1218. }
  1219. /**
  1220. * onenand_unlock_all - [OneNAND Interface] unlock all blocks
  1221. * @param mtd MTD device structure
  1222. *
  1223. * Unlock all blocks
  1224. */
  1225. static int onenand_unlock_all(struct mtd_info *mtd)
  1226. {
  1227. struct onenand_chip *this = mtd->priv;
  1228. if (this->options & ONENAND_HAS_UNLOCK_ALL) {
  1229. /* Set start block address */
  1230. this->write_word(0, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
  1231. /* Write unlock command */
  1232. this->command(mtd, ONENAND_CMD_UNLOCK_ALL, 0, 0);
  1233. /* There's no return value */
  1234. this->wait(mtd, FL_LOCKING);
  1235. /* Sanity check */
  1236. while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
  1237. & ONENAND_CTRL_ONGO)
  1238. continue;
  1239. /* Workaround for all block unlock in DDP */
  1240. if (ONENAND_IS_DDP(this)) {
  1241. /* 1st block on another chip */
  1242. loff_t ofs = this->chipsize >> 1;
  1243. size_t len = mtd->erasesize;
  1244. onenand_unlock(mtd, ofs, len);
  1245. }
  1246. onenand_check_lock_status(this);
  1247. return 0;
  1248. }
  1249. onenand_unlock(mtd, 0x0, this->chipsize);
  1250. return 0;
  1251. }
  1252. #ifdef CONFIG_MTD_ONENAND_OTP
  1253. /* Interal OTP operation */
  1254. typedef int (*otp_op_t)(struct mtd_info *mtd, loff_t form, size_t len,
  1255. size_t *retlen, u_char *buf);
  1256. /**
  1257. * do_otp_read - [DEFAULT] Read OTP block area
  1258. * @param mtd MTD device structure
  1259. * @param from The offset to read
  1260. * @param len number of bytes to read
  1261. * @param retlen pointer to variable to store the number of readbytes
  1262. * @param buf the databuffer to put/get data
  1263. *
  1264. * Read OTP block area.
  1265. */
  1266. static int do_otp_read(struct mtd_info *mtd, loff_t from, size_t len,
  1267. size_t *retlen, u_char *buf)
  1268. {
  1269. struct onenand_chip *this = mtd->priv;
  1270. int ret;
  1271. /* Enter OTP access mode */
  1272. this->command(mtd, ONENAND_CMD_OTP_ACCESS, 0, 0);
  1273. this->wait(mtd, FL_OTPING);
  1274. ret = mtd->read(mtd, from, len, retlen, buf);
  1275. /* Exit OTP access mode */
  1276. this->command(mtd, ONENAND_CMD_RESET, 0, 0);
  1277. this->wait(mtd, FL_RESETING);
  1278. return ret;
  1279. }
  1280. /**
  1281. * do_otp_write - [DEFAULT] Write OTP block area
  1282. * @param mtd MTD device structure
  1283. * @param from The offset to write
  1284. * @param len number of bytes to write
  1285. * @param retlen pointer to variable to store the number of write bytes
  1286. * @param buf the databuffer to put/get data
  1287. *
  1288. * Write OTP block area.
  1289. */
  1290. static int do_otp_write(struct mtd_info *mtd, loff_t from, size_t len,
  1291. size_t *retlen, u_char *buf)
  1292. {
  1293. struct onenand_chip *this = mtd->priv;
  1294. unsigned char *pbuf = buf;
  1295. int ret;
  1296. /* Force buffer page aligned */
  1297. if (len < mtd->writesize) {
  1298. memcpy(this->page_buf, buf, len);
  1299. memset(this->page_buf + len, 0xff, mtd->writesize - len);
  1300. pbuf = this->page_buf;
  1301. len = mtd->writesize;
  1302. }
  1303. /* Enter OTP access mode */
  1304. this->command(mtd, ONENAND_CMD_OTP_ACCESS, 0, 0);
  1305. this->wait(mtd, FL_OTPING);
  1306. ret = mtd->write(mtd, from, len, retlen, pbuf);
  1307. /* Exit OTP access mode */
  1308. this->command(mtd, ONENAND_CMD_RESET, 0, 0);
  1309. this->wait(mtd, FL_RESETING);
  1310. return ret;
  1311. }
  1312. /**
  1313. * do_otp_lock - [DEFAULT] Lock OTP block area
  1314. * @param mtd MTD device structure
  1315. * @param from The offset to lock
  1316. * @param len number of bytes to lock
  1317. * @param retlen pointer to variable to store the number of lock bytes
  1318. * @param buf the databuffer to put/get data
  1319. *
  1320. * Lock OTP block area.
  1321. */
  1322. static int do_otp_lock(struct mtd_info *mtd, loff_t from, size_t len,
  1323. size_t *retlen, u_char *buf)
  1324. {
  1325. struct onenand_chip *this = mtd->priv;
  1326. int ret;
  1327. /* Enter OTP access mode */
  1328. this->command(mtd, ONENAND_CMD_OTP_ACCESS, 0, 0);
  1329. this->wait(mtd, FL_OTPING);
  1330. ret = onenand_do_write_oob(mtd, from, len, retlen, buf);
  1331. /* Exit OTP access mode */
  1332. this->command(mtd, ONENAND_CMD_RESET, 0, 0);
  1333. this->wait(mtd, FL_RESETING);
  1334. return ret;
  1335. }
  1336. /**
  1337. * onenand_otp_walk - [DEFAULT] Handle OTP operation
  1338. * @param mtd MTD device structure
  1339. * @param from The offset to read/write
  1340. * @param len number of bytes to read/write
  1341. * @param retlen pointer to variable to store the number of read bytes
  1342. * @param buf the databuffer to put/get data
  1343. * @param action do given action
  1344. * @param mode specify user and factory
  1345. *
  1346. * Handle OTP operation.
  1347. */
  1348. static int onenand_otp_walk(struct mtd_info *mtd, loff_t from, size_t len,
  1349. size_t *retlen, u_char *buf,
  1350. otp_op_t action, int mode)
  1351. {
  1352. struct onenand_chip *this = mtd->priv;
  1353. int otp_pages;
  1354. int density;
  1355. int ret = 0;
  1356. *retlen = 0;
  1357. density = this->device_id >> ONENAND_DEVICE_DENSITY_SHIFT;
  1358. if (density < ONENAND_DEVICE_DENSITY_512Mb)
  1359. otp_pages = 20;
  1360. else
  1361. otp_pages = 10;
  1362. if (mode == MTD_OTP_FACTORY) {
  1363. from += mtd->writesize * otp_pages;
  1364. otp_pages = 64 - otp_pages;
  1365. }
  1366. /* Check User/Factory boundary */
  1367. if (((mtd->writesize * otp_pages) - (from + len)) < 0)
  1368. return 0;
  1369. while (len > 0 && otp_pages > 0) {
  1370. if (!action) { /* OTP Info functions */
  1371. struct otp_info *otpinfo;
  1372. len -= sizeof(struct otp_info);
  1373. if (len <= 0)
  1374. return -ENOSPC;
  1375. otpinfo = (struct otp_info *) buf;
  1376. otpinfo->start = from;
  1377. otpinfo->length = mtd->writesize;
  1378. otpinfo->locked = 0;
  1379. from += mtd->writesize;
  1380. buf += sizeof(struct otp_info);
  1381. *retlen += sizeof(struct otp_info);
  1382. } else {
  1383. size_t tmp_retlen;
  1384. int size = len;
  1385. ret = action(mtd, from, len, &tmp_retlen, buf);
  1386. buf += size;
  1387. len -= size;
  1388. *retlen += size;
  1389. if (ret < 0)
  1390. return ret;
  1391. }
  1392. otp_pages--;
  1393. }
  1394. return 0;
  1395. }
  1396. /**
  1397. * onenand_get_fact_prot_info - [MTD Interface] Read factory OTP info
  1398. * @param mtd MTD device structure
  1399. * @param buf the databuffer to put/get data
  1400. * @param len number of bytes to read
  1401. *
  1402. * Read factory OTP info.
  1403. */
  1404. static int onenand_get_fact_prot_info(struct mtd_info *mtd,
  1405. struct otp_info *buf, size_t len)
  1406. {
  1407. size_t retlen;
  1408. int ret;
  1409. ret = onenand_otp_walk(mtd, 0, len, &retlen, (u_char *) buf, NULL, MTD_OTP_FACTORY);
  1410. return ret ? : retlen;
  1411. }
  1412. /**
  1413. * onenand_read_fact_prot_reg - [MTD Interface] Read factory OTP area
  1414. * @param mtd MTD device structure
  1415. * @param from The offset to read
  1416. * @param len number of bytes to read
  1417. * @param retlen pointer to variable to store the number of read bytes
  1418. * @param buf the databuffer to put/get data
  1419. *
  1420. * Read factory OTP area.
  1421. */
  1422. static int onenand_read_fact_prot_reg(struct mtd_info *mtd, loff_t from,
  1423. size_t len, size_t *retlen, u_char *buf)
  1424. {
  1425. return onenand_otp_walk(mtd, from, len, retlen, buf, do_otp_read, MTD_OTP_FACTORY);
  1426. }
  1427. /**
  1428. * onenand_get_user_prot_info - [MTD Interface] Read user OTP info
  1429. * @param mtd MTD device structure
  1430. * @param buf the databuffer to put/get data
  1431. * @param len number of bytes to read
  1432. *
  1433. * Read user OTP info.
  1434. */
  1435. static int onenand_get_user_prot_info(struct mtd_info *mtd,
  1436. struct otp_info *buf, size_t len)
  1437. {
  1438. size_t retlen;
  1439. int ret;
  1440. ret = onenand_otp_walk(mtd, 0, len, &retlen, (u_char *) buf, NULL, MTD_OTP_USER);
  1441. return ret ? : retlen;
  1442. }
  1443. /**
  1444. * onenand_read_user_prot_reg - [MTD Interface] Read user OTP area
  1445. * @param mtd MTD device structure
  1446. * @param from The offset to read
  1447. * @param len number of bytes to read
  1448. * @param retlen pointer to variable to store the number of read bytes
  1449. * @param buf the databuffer to put/get data
  1450. *
  1451. * Read user OTP area.
  1452. */
  1453. static int onenand_read_user_prot_reg(struct mtd_info *mtd, loff_t from,
  1454. size_t len, size_t *retlen, u_char *buf)
  1455. {
  1456. return onenand_otp_walk(mtd, from, len, retlen, buf, do_otp_read, MTD_OTP_USER);
  1457. }
  1458. /**
  1459. * onenand_write_user_prot_reg - [MTD Interface] Write user OTP area
  1460. * @param mtd MTD device structure
  1461. * @param from The offset to write
  1462. * @param len number of bytes to write
  1463. * @param retlen pointer to variable to store the number of write bytes
  1464. * @param buf the databuffer to put/get data
  1465. *
  1466. * Write user OTP area.
  1467. */
  1468. static int onenand_write_user_prot_reg(struct mtd_info *mtd, loff_t from,
  1469. size_t len, size_t *retlen, u_char *buf)
  1470. {
  1471. return onenand_otp_walk(mtd, from, len, retlen, buf, do_otp_write, MTD_OTP_USER);
  1472. }
  1473. /**
  1474. * onenand_lock_user_prot_reg - [MTD Interface] Lock user OTP area
  1475. * @param mtd MTD device structure
  1476. * @param from The offset to lock
  1477. * @param len number of bytes to unlock
  1478. *
  1479. * Write lock mark on spare area in page 0 in OTP block
  1480. */
  1481. static int onenand_lock_user_prot_reg(struct mtd_info *mtd, loff_t from,
  1482. size_t len)
  1483. {
  1484. unsigned char oob_buf[64];
  1485. size_t retlen;
  1486. int ret;
  1487. memset(oob_buf, 0xff, mtd->oobsize);
  1488. /*
  1489. * Note: OTP lock operation
  1490. * OTP block : 0xXXFC
  1491. * 1st block : 0xXXF3 (If chip support)
  1492. * Both : 0xXXF0 (If chip support)
  1493. */
  1494. oob_buf[ONENAND_OTP_LOCK_OFFSET] = 0xFC;
  1495. /*
  1496. * Write lock mark to 8th word of sector0 of page0 of the spare0.
  1497. * We write 16 bytes spare area instead of 2 bytes.
  1498. */
  1499. from = 0;
  1500. len = 16;
  1501. ret = onenand_otp_walk(mtd, from, len, &retlen, oob_buf, do_otp_lock, MTD_OTP_USER);
  1502. return ret ? : retlen;
  1503. }
  1504. #endif /* CONFIG_MTD_ONENAND_OTP */
  1505. /**
  1506. * onenand_lock_scheme - Check and set OneNAND lock scheme
  1507. * @param mtd MTD data structure
  1508. *
  1509. * Check and set OneNAND lock scheme
  1510. */
  1511. static void onenand_lock_scheme(struct mtd_info *mtd)
  1512. {
  1513. struct onenand_chip *this = mtd->priv;
  1514. unsigned int density, process;
  1515. /* Lock scheme depends on density and process */
  1516. density = this->device_id >> ONENAND_DEVICE_DENSITY_SHIFT;
  1517. process = this->version_id >> ONENAND_VERSION_PROCESS_SHIFT;
  1518. /* Lock scheme */
  1519. if (density >= ONENAND_DEVICE_DENSITY_1Gb) {
  1520. /* A-Die has all block unlock */
  1521. if (process) {
  1522. printk(KERN_DEBUG "Chip support all block unlock\n");
  1523. this->options |= ONENAND_HAS_UNLOCK_ALL;
  1524. }
  1525. } else {
  1526. /* Some OneNAND has continues lock scheme */
  1527. if (!process) {
  1528. printk(KERN_DEBUG "Lock scheme is Continues Lock\n");
  1529. this->options |= ONENAND_HAS_CONT_LOCK;
  1530. }
  1531. }
  1532. }
  1533. /**
  1534. * onenand_print_device_info - Print device ID
  1535. * @param device device ID
  1536. *
  1537. * Print device ID
  1538. */
  1539. static void onenand_print_device_info(int device, int version)
  1540. {
  1541. int vcc, demuxed, ddp, density;
  1542. vcc = device & ONENAND_DEVICE_VCC_MASK;
  1543. demuxed = device & ONENAND_DEVICE_IS_DEMUX;
  1544. ddp = device & ONENAND_DEVICE_IS_DDP;
  1545. density = device >> ONENAND_DEVICE_DENSITY_SHIFT;
  1546. printk(KERN_INFO "%sOneNAND%s %dMB %sV 16-bit (0x%02x)\n",
  1547. demuxed ? "" : "Muxed ",
  1548. ddp ? "(DDP)" : "",
  1549. (16 << density),
  1550. vcc ? "2.65/3.3" : "1.8",
  1551. device);
  1552. printk(KERN_DEBUG "OneNAND version = 0x%04x\n", version);
  1553. }
  1554. static const struct onenand_manufacturers onenand_manuf_ids[] = {
  1555. {ONENAND_MFR_SAMSUNG, "Samsung"},
  1556. };
  1557. /**
  1558. * onenand_check_maf - Check manufacturer ID
  1559. * @param manuf manufacturer ID
  1560. *
  1561. * Check manufacturer ID
  1562. */
  1563. static int onenand_check_maf(int manuf)
  1564. {
  1565. int size = ARRAY_SIZE(onenand_manuf_ids);
  1566. char *name;
  1567. int i;
  1568. for (i = 0; i < size; i++)
  1569. if (manuf == onenand_manuf_ids[i].id)
  1570. break;
  1571. if (i < size)
  1572. name = onenand_manuf_ids[i].name;
  1573. else
  1574. name = "Unknown";
  1575. printk(KERN_DEBUG "OneNAND Manufacturer: %s (0x%0x)\n", name, manuf);
  1576. return (i == size);
  1577. }
  1578. /**
  1579. * onenand_probe - [OneNAND Interface] Probe the OneNAND device
  1580. * @param mtd MTD device structure
  1581. *
  1582. * OneNAND detection method:
  1583. * Compare the the values from command with ones from register
  1584. */
  1585. static int onenand_probe(struct mtd_info *mtd)
  1586. {
  1587. struct onenand_chip *this = mtd->priv;
  1588. int bram_maf_id, bram_dev_id, maf_id, dev_id, ver_id;
  1589. int density;
  1590. int syscfg;
  1591. /* Save system configuration 1 */
  1592. syscfg = this->read_word(this->base + ONENAND_REG_SYS_CFG1);
  1593. /* Clear Sync. Burst Read mode to read BootRAM */
  1594. this->write_word((syscfg & ~ONENAND_SYS_CFG1_SYNC_READ), this->base + ONENAND_REG_SYS_CFG1);
  1595. /* Send the command for reading device ID from BootRAM */
  1596. this->write_word(ONENAND_CMD_READID, this->base + ONENAND_BOOTRAM);
  1597. /* Read manufacturer and device IDs from BootRAM */
  1598. bram_maf_id = this->read_word(this->base + ONENAND_BOOTRAM + 0x0);
  1599. bram_dev_id = this->read_word(this->base + ONENAND_BOOTRAM + 0x2);
  1600. /* Reset OneNAND to read default register values */
  1601. this->write_word(ONENAND_CMD_RESET, this->base + ONENAND_BOOTRAM);
  1602. /* Wait reset */
  1603. this->wait(mtd, FL_RESETING);
  1604. /* Restore system configuration 1 */
  1605. this->write_word(syscfg, this->base + ONENAND_REG_SYS_CFG1);
  1606. /* Check manufacturer ID */
  1607. if (onenand_check_maf(bram_maf_id))
  1608. return -ENXIO;
  1609. /* Read manufacturer and device IDs from Register */
  1610. maf_id = this->read_word(this->base + ONENAND_REG_MANUFACTURER_ID);
  1611. dev_id = this->read_word(this->base + ONENAND_REG_DEVICE_ID);
  1612. ver_id = this->read_word(this->base + ONENAND_REG_VERSION_ID);
  1613. /* Check OneNAND device */
  1614. if (maf_id != bram_maf_id || dev_id != bram_dev_id)
  1615. return -ENXIO;
  1616. /* Flash device information */
  1617. onenand_print_device_info(dev_id, ver_id);
  1618. this->device_id = dev_id;
  1619. this->version_id = ver_id;
  1620. density = dev_id >> ONENAND_DEVICE_DENSITY_SHIFT;
  1621. this->chipsize = (16 << density) << 20;
  1622. /* Set density mask. it is used for DDP */
  1623. if (ONENAND_IS_DDP(this))
  1624. this->density_mask = (1 << (density + 6));
  1625. else
  1626. this->density_mask = 0;
  1627. /* OneNAND page size & block size */
  1628. /* The data buffer size is equal to page size */
  1629. mtd->writesize = this->read_word(this->base + ONENAND_REG_DATA_BUFFER_SIZE);
  1630. mtd->oobsize = mtd->writesize >> 5;
  1631. /* Pagers per block is always 64 in OneNAND */
  1632. mtd->erasesize = mtd->writesize << 6;
  1633. this->erase_shift = ffs(mtd->erasesize) - 1;
  1634. this->page_shift = ffs(mtd->writesize) - 1;
  1635. this->ppb_shift = (this->erase_shift - this->page_shift);
  1636. this->page_mask = (mtd->erasesize / mtd->writesize) - 1;
  1637. /* REVIST: Multichip handling */
  1638. mtd->size = this->chipsize;
  1639. /* Check OneNAND lock scheme */
  1640. onenand_lock_scheme(mtd);
  1641. return 0;
  1642. }
  1643. /**
  1644. * onenand_suspend - [MTD Interface] Suspend the OneNAND flash
  1645. * @param mtd MTD device structure
  1646. */
  1647. static int onenand_suspend(struct mtd_info *mtd)
  1648. {
  1649. return onenand_get_device(mtd, FL_PM_SUSPENDED);
  1650. }
  1651. /**
  1652. * onenand_resume - [MTD Interface] Resume the OneNAND flash
  1653. * @param mtd MTD device structure
  1654. */
  1655. static void onenand_resume(struct mtd_info *mtd)
  1656. {
  1657. struct onenand_chip *this = mtd->priv;
  1658. if (this->state == FL_PM_SUSPENDED)
  1659. onenand_release_device(mtd);
  1660. else
  1661. printk(KERN_ERR "resume() called for the chip which is not"
  1662. "in suspended state\n");
  1663. }
  1664. /**
  1665. * onenand_scan - [OneNAND Interface] Scan for the OneNAND device
  1666. * @param mtd MTD device structure
  1667. * @param maxchips Number of chips to scan for
  1668. *
  1669. * This fills out all the not initialized function pointers
  1670. * with the defaults.
  1671. * The flash ID is read and the mtd/chip structures are
  1672. * filled with the appropriate values.
  1673. */
  1674. int onenand_scan(struct mtd_info *mtd, int maxchips)
  1675. {
  1676. struct onenand_chip *this = mtd->priv;
  1677. if (!this->read_word)
  1678. this->read_word = onenand_readw;
  1679. if (!this->write_word)
  1680. this->write_word = onenand_writew;
  1681. if (!this->command)
  1682. this->command = onenand_command;
  1683. if (!this->wait)
  1684. onenand_setup_wait(mtd);
  1685. if (!this->read_bufferram)
  1686. this->read_bufferram = onenand_read_bufferram;
  1687. if (!this->write_bufferram)
  1688. this->write_bufferram = onenand_write_bufferram;
  1689. if (!this->block_markbad)
  1690. this->block_markbad = onenand_default_block_markbad;
  1691. if (!this->scan_bbt)
  1692. this->scan_bbt = onenand_default_bbt;
  1693. if (onenand_probe(mtd))
  1694. return -ENXIO;
  1695. /* Set Sync. Burst Read after probing */
  1696. if (this->mmcontrol) {
  1697. printk(KERN_INFO "OneNAND Sync. Burst Read support\n");
  1698. this->read_bufferram = onenand_sync_read_bufferram;
  1699. }
  1700. /* Allocate buffers, if necessary */
  1701. if (!this->page_buf) {
  1702. size_t len;
  1703. len = mtd->writesize + mtd->oobsize;
  1704. this->page_buf = kmalloc(len, GFP_KERNEL);
  1705. if (!this->page_buf) {
  1706. printk(KERN_ERR "onenand_scan(): Can't allocate page_buf\n");
  1707. return -ENOMEM;
  1708. }
  1709. this->options |= ONENAND_PAGEBUF_ALLOC;
  1710. }
  1711. this->state = FL_READY;
  1712. init_waitqueue_head(&this->wq);
  1713. spin_lock_init(&this->chip_lock);
  1714. /*
  1715. * Allow subpage writes up to oobsize.
  1716. */
  1717. switch (mtd->oobsize) {
  1718. case 64:
  1719. this->ecclayout = &onenand_oob_64;
  1720. mtd->subpage_sft = 2;
  1721. break;
  1722. case 32:
  1723. this->ecclayout = &onenand_oob_32;
  1724. mtd->subpage_sft = 1;
  1725. break;
  1726. default:
  1727. printk(KERN_WARNING "No OOB scheme defined for oobsize %d\n",
  1728. mtd->oobsize);
  1729. mtd->subpage_sft = 0;
  1730. /* To prevent kernel oops */
  1731. this->ecclayout = &onenand_oob_32;
  1732. break;
  1733. }
  1734. this->subpagesize = mtd->writesize >> mtd->subpage_sft;
  1735. mtd->ecclayout = this->ecclayout;
  1736. /* Fill in remaining MTD driver data */
  1737. mtd->type = MTD_NANDFLASH;
  1738. mtd->flags = MTD_CAP_NANDFLASH;
  1739. mtd->ecctype = MTD_ECC_SW;
  1740. mtd->erase = onenand_erase;
  1741. mtd->point = NULL;
  1742. mtd->unpoint = NULL;
  1743. mtd->read = onenand_read;
  1744. mtd->write = onenand_write;
  1745. mtd->read_oob = onenand_read_oob;
  1746. mtd->write_oob = onenand_write_oob;
  1747. #ifdef CONFIG_MTD_ONENAND_OTP
  1748. mtd->get_fact_prot_info = onenand_get_fact_prot_info;
  1749. mtd->read_fact_prot_reg = onenand_read_fact_prot_reg;
  1750. mtd->get_user_prot_info = onenand_get_user_prot_info;
  1751. mtd->read_user_prot_reg = onenand_read_user_prot_reg;
  1752. mtd->write_user_prot_reg = onenand_write_user_prot_reg;
  1753. mtd->lock_user_prot_reg = onenand_lock_user_prot_reg;
  1754. #endif
  1755. mtd->sync = onenand_sync;
  1756. mtd->lock = onenand_lock;
  1757. mtd->unlock = onenand_unlock;
  1758. mtd->suspend = onenand_suspend;
  1759. mtd->resume = onenand_resume;
  1760. mtd->block_isbad = onenand_block_isbad;
  1761. mtd->block_markbad = onenand_block_markbad;
  1762. mtd->owner = THIS_MODULE;
  1763. /* Unlock whole block */
  1764. onenand_unlock_all(mtd);
  1765. return this->scan_bbt(mtd);
  1766. }
  1767. /**
  1768. * onenand_release - [OneNAND Interface] Free resources held by the OneNAND device
  1769. * @param mtd MTD device structure
  1770. */
  1771. void onenand_release(struct mtd_info *mtd)
  1772. {
  1773. struct onenand_chip *this = mtd->priv;
  1774. #ifdef CONFIG_MTD_PARTITIONS
  1775. /* Deregister partitions */
  1776. del_mtd_partitions (mtd);
  1777. #endif
  1778. /* Deregister the device */
  1779. del_mtd_device (mtd);
  1780. /* Free bad block table memory, if allocated */
  1781. if (this->bbm)
  1782. kfree(this->bbm);
  1783. /* Buffer allocated by onenand_scan */
  1784. if (this->options & ONENAND_PAGEBUF_ALLOC)
  1785. kfree(this->page_buf);
  1786. }
  1787. EXPORT_SYMBOL_GPL(onenand_scan);
  1788. EXPORT_SYMBOL_GPL(onenand_release);
  1789. MODULE_LICENSE("GPL");
  1790. MODULE_AUTHOR("Kyungmin Park <kyungmin.park@samsung.com>");
  1791. MODULE_DESCRIPTION("Generic OneNAND flash driver code");