onenand_base.c 53 KB

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