onenand_base.c 54 KB

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