onenand_base.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656
  1. /*
  2. * linux/drivers/mtd/onenand/onenand_base.c
  3. *
  4. * Copyright (C) 2005 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/jiffies.h>
  16. #include <linux/mtd/mtd.h>
  17. #include <linux/mtd/onenand.h>
  18. #include <linux/mtd/partitions.h>
  19. #include <asm/io.h>
  20. /**
  21. * onenand_oob_64 - oob info for large (2KB) page
  22. */
  23. static struct nand_oobinfo onenand_oob_64 = {
  24. .useecc = MTD_NANDECC_AUTOPLACE,
  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. {24, 3}, {46, 2}, {40, 3}, {62, 2} }
  35. };
  36. /**
  37. * onenand_oob_32 - oob info for middle (1KB) page
  38. */
  39. static struct nand_oobinfo onenand_oob_32 = {
  40. .useecc = MTD_NANDECC_AUTOPLACE,
  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;
  170. int block, page;
  171. /* Now we use page size operation */
  172. int sectors = 4, count = 4;
  173. /* Address translation */
  174. switch (cmd) {
  175. case ONENAND_CMD_UNLOCK:
  176. case ONENAND_CMD_LOCK:
  177. case ONENAND_CMD_LOCK_TIGHT:
  178. block = -1;
  179. page = -1;
  180. break;
  181. case ONENAND_CMD_ERASE:
  182. case ONENAND_CMD_BUFFERRAM:
  183. block = (int) (addr >> this->erase_shift);
  184. page = -1;
  185. break;
  186. default:
  187. block = (int) (addr >> this->erase_shift);
  188. page = (int) (addr >> this->page_shift);
  189. page &= this->page_mask;
  190. break;
  191. }
  192. /* NOTE: The setting order of the registers is very important! */
  193. if (cmd == ONENAND_CMD_BUFFERRAM) {
  194. /* Select DataRAM for DDP */
  195. value = onenand_bufferram_address(this, block);
  196. this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
  197. /* Switch to the next data buffer */
  198. ONENAND_SET_NEXT_BUFFERRAM(this);
  199. return 0;
  200. }
  201. if (block != -1) {
  202. /* Write 'DFS, FBA' of Flash */
  203. value = onenand_block_address(this, block);
  204. this->write_word(value, this->base + ONENAND_REG_START_ADDRESS1);
  205. }
  206. if (page != -1) {
  207. int dataram;
  208. switch (cmd) {
  209. case ONENAND_CMD_READ:
  210. case ONENAND_CMD_READOOB:
  211. dataram = ONENAND_SET_NEXT_BUFFERRAM(this);
  212. readcmd = 1;
  213. break;
  214. default:
  215. dataram = ONENAND_CURRENT_BUFFERRAM(this);
  216. break;
  217. }
  218. /* Write 'FPA, FSA' of Flash */
  219. value = onenand_page_address(page, sectors);
  220. this->write_word(value, this->base + ONENAND_REG_START_ADDRESS8);
  221. /* Write 'BSA, BSC' of DataRAM */
  222. value = onenand_buffer_address(dataram, sectors, count);
  223. this->write_word(value, this->base + ONENAND_REG_START_BUFFER);
  224. if (readcmd) {
  225. /* Select DataRAM for DDP */
  226. value = onenand_bufferram_address(this, block);
  227. this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
  228. }
  229. }
  230. /* Interrupt clear */
  231. this->write_word(ONENAND_INT_CLEAR, this->base + ONENAND_REG_INTERRUPT);
  232. /* Write command */
  233. this->write_word(cmd, this->base + ONENAND_REG_COMMAND);
  234. return 0;
  235. }
  236. /**
  237. * onenand_wait - [DEFAULT] wait until the command is done
  238. * @param mtd MTD device structure
  239. * @param state state to select the max. timeout value
  240. *
  241. * Wait for command done. This applies to all OneNAND command
  242. * Read can take up to 30us, erase up to 2ms and program up to 350us
  243. * according to general OneNAND specs
  244. */
  245. static int onenand_wait(struct mtd_info *mtd, int state)
  246. {
  247. struct onenand_chip * this = mtd->priv;
  248. unsigned long timeout;
  249. unsigned int flags = ONENAND_INT_MASTER;
  250. unsigned int interrupt = 0;
  251. unsigned int ctrl, ecc;
  252. /* The 20 msec is enough */
  253. timeout = jiffies + msecs_to_jiffies(20);
  254. while (time_before(jiffies, timeout)) {
  255. interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
  256. if (interrupt & flags)
  257. break;
  258. if (state != FL_READING)
  259. cond_resched();
  260. touch_softlockup_watchdog();
  261. }
  262. /* To get correct interrupt status in timeout case */
  263. interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
  264. ctrl = this->read_word(this->base + ONENAND_REG_CTRL_STATUS);
  265. if (ctrl & ONENAND_CTRL_ERROR) {
  266. /* It maybe occur at initial bad block */
  267. DEBUG(MTD_DEBUG_LEVEL0, "onenand_wait: controller error = 0x%04x\n", ctrl);
  268. /* Clear other interrupt bits for preventing ECC error */
  269. interrupt &= ONENAND_INT_MASTER;
  270. }
  271. if (ctrl & ONENAND_CTRL_LOCK) {
  272. DEBUG(MTD_DEBUG_LEVEL0, "onenand_wait: it's locked error = 0x%04x\n", ctrl);
  273. return -EACCES;
  274. }
  275. if (interrupt & ONENAND_INT_READ) {
  276. ecc = this->read_word(this->base + ONENAND_REG_ECC_STATUS);
  277. if (ecc & ONENAND_ECC_2BIT_ALL) {
  278. DEBUG(MTD_DEBUG_LEVEL0, "onenand_wait: ECC error = 0x%04x\n", ecc);
  279. return -EBADMSG;
  280. }
  281. }
  282. return 0;
  283. }
  284. /**
  285. * onenand_bufferram_offset - [DEFAULT] BufferRAM offset
  286. * @param mtd MTD data structure
  287. * @param area BufferRAM area
  288. * @return offset given area
  289. *
  290. * Return BufferRAM offset given area
  291. */
  292. static inline int onenand_bufferram_offset(struct mtd_info *mtd, int area)
  293. {
  294. struct onenand_chip *this = mtd->priv;
  295. if (ONENAND_CURRENT_BUFFERRAM(this)) {
  296. if (area == ONENAND_DATARAM)
  297. return mtd->oobblock;
  298. if (area == ONENAND_SPARERAM)
  299. return mtd->oobsize;
  300. }
  301. return 0;
  302. }
  303. /**
  304. * onenand_read_bufferram - [OneNAND Interface] Read the bufferram area
  305. * @param mtd MTD data structure
  306. * @param area BufferRAM area
  307. * @param buffer the databuffer to put/get data
  308. * @param offset offset to read from or write to
  309. * @param count number of bytes to read/write
  310. *
  311. * Read the BufferRAM area
  312. */
  313. static int onenand_read_bufferram(struct mtd_info *mtd, int area,
  314. unsigned char *buffer, int offset, size_t count)
  315. {
  316. struct onenand_chip *this = mtd->priv;
  317. void __iomem *bufferram;
  318. bufferram = this->base + area;
  319. bufferram += onenand_bufferram_offset(mtd, area);
  320. if (ONENAND_CHECK_BYTE_ACCESS(count)) {
  321. unsigned short word;
  322. /* Align with word(16-bit) size */
  323. count--;
  324. /* Read word and save byte */
  325. word = this->read_word(bufferram + offset + count);
  326. buffer[count] = (word & 0xff);
  327. }
  328. memcpy(buffer, bufferram + offset, count);
  329. return 0;
  330. }
  331. /**
  332. * onenand_sync_read_bufferram - [OneNAND Interface] Read the bufferram area with Sync. Burst mode
  333. * @param mtd MTD data structure
  334. * @param area BufferRAM area
  335. * @param buffer the databuffer to put/get data
  336. * @param offset offset to read from or write to
  337. * @param count number of bytes to read/write
  338. *
  339. * Read the BufferRAM area with Sync. Burst Mode
  340. */
  341. static int onenand_sync_read_bufferram(struct mtd_info *mtd, int area,
  342. unsigned char *buffer, int offset, size_t count)
  343. {
  344. struct onenand_chip *this = mtd->priv;
  345. void __iomem *bufferram;
  346. bufferram = this->base + area;
  347. bufferram += onenand_bufferram_offset(mtd, area);
  348. this->mmcontrol(mtd, ONENAND_SYS_CFG1_SYNC_READ);
  349. if (ONENAND_CHECK_BYTE_ACCESS(count)) {
  350. unsigned short word;
  351. /* Align with word(16-bit) size */
  352. count--;
  353. /* Read word and save byte */
  354. word = this->read_word(bufferram + offset + count);
  355. buffer[count] = (word & 0xff);
  356. }
  357. memcpy(buffer, bufferram + offset, count);
  358. this->mmcontrol(mtd, 0);
  359. return 0;
  360. }
  361. /**
  362. * onenand_write_bufferram - [OneNAND Interface] Write the bufferram area
  363. * @param mtd MTD data structure
  364. * @param area BufferRAM area
  365. * @param buffer the databuffer to put/get data
  366. * @param offset offset to read from or write to
  367. * @param count number of bytes to read/write
  368. *
  369. * Write the BufferRAM area
  370. */
  371. static int onenand_write_bufferram(struct mtd_info *mtd, int area,
  372. const unsigned char *buffer, int offset, size_t count)
  373. {
  374. struct onenand_chip *this = mtd->priv;
  375. void __iomem *bufferram;
  376. bufferram = this->base + area;
  377. bufferram += onenand_bufferram_offset(mtd, area);
  378. if (ONENAND_CHECK_BYTE_ACCESS(count)) {
  379. unsigned short word;
  380. int byte_offset;
  381. /* Align with word(16-bit) size */
  382. count--;
  383. /* Calculate byte access offset */
  384. byte_offset = offset + count;
  385. /* Read word and save byte */
  386. word = this->read_word(bufferram + byte_offset);
  387. word = (word & ~0xff) | buffer[count];
  388. this->write_word(word, bufferram + byte_offset);
  389. }
  390. memcpy(bufferram + offset, buffer, count);
  391. return 0;
  392. }
  393. /**
  394. * onenand_check_bufferram - [GENERIC] Check BufferRAM information
  395. * @param mtd MTD data structure
  396. * @param addr address to check
  397. * @return 1 if there are valid data, otherwise 0
  398. *
  399. * Check bufferram if there is data we required
  400. */
  401. static int onenand_check_bufferram(struct mtd_info *mtd, loff_t addr)
  402. {
  403. struct onenand_chip *this = mtd->priv;
  404. int block, page;
  405. int i;
  406. block = (int) (addr >> this->erase_shift);
  407. page = (int) (addr >> this->page_shift);
  408. page &= this->page_mask;
  409. i = ONENAND_CURRENT_BUFFERRAM(this);
  410. /* Is there valid data? */
  411. if (this->bufferram[i].block == block &&
  412. this->bufferram[i].page == page &&
  413. this->bufferram[i].valid)
  414. return 1;
  415. return 0;
  416. }
  417. /**
  418. * onenand_update_bufferram - [GENERIC] Update BufferRAM information
  419. * @param mtd MTD data structure
  420. * @param addr address to update
  421. * @param valid valid flag
  422. *
  423. * Update BufferRAM information
  424. */
  425. static int onenand_update_bufferram(struct mtd_info *mtd, loff_t addr,
  426. int valid)
  427. {
  428. struct onenand_chip *this = mtd->priv;
  429. int block, page;
  430. int i;
  431. block = (int) (addr >> this->erase_shift);
  432. page = (int) (addr >> this->page_shift);
  433. page &= this->page_mask;
  434. /* Invalidate BufferRAM */
  435. for (i = 0; i < MAX_BUFFERRAM; i++) {
  436. if (this->bufferram[i].block == block &&
  437. this->bufferram[i].page == page)
  438. this->bufferram[i].valid = 0;
  439. }
  440. /* Update BufferRAM */
  441. i = ONENAND_CURRENT_BUFFERRAM(this);
  442. this->bufferram[i].block = block;
  443. this->bufferram[i].page = page;
  444. this->bufferram[i].valid = valid;
  445. return 0;
  446. }
  447. /**
  448. * onenand_get_device - [GENERIC] Get chip for selected access
  449. * @param mtd MTD device structure
  450. * @param new_state the state which is requested
  451. *
  452. * Get the device and lock it for exclusive access
  453. */
  454. static int onenand_get_device(struct mtd_info *mtd, int new_state)
  455. {
  456. struct onenand_chip *this = mtd->priv;
  457. DECLARE_WAITQUEUE(wait, current);
  458. /*
  459. * Grab the lock and see if the device is available
  460. */
  461. while (1) {
  462. spin_lock(&this->chip_lock);
  463. if (this->state == FL_READY) {
  464. this->state = new_state;
  465. spin_unlock(&this->chip_lock);
  466. break;
  467. }
  468. if (new_state == FL_PM_SUSPENDED) {
  469. spin_unlock(&this->chip_lock);
  470. return (this->state == FL_PM_SUSPENDED) ? 0 : -EAGAIN;
  471. }
  472. set_current_state(TASK_UNINTERRUPTIBLE);
  473. add_wait_queue(&this->wq, &wait);
  474. spin_unlock(&this->chip_lock);
  475. schedule();
  476. remove_wait_queue(&this->wq, &wait);
  477. }
  478. return 0;
  479. }
  480. /**
  481. * onenand_release_device - [GENERIC] release chip
  482. * @param mtd MTD device structure
  483. *
  484. * Deselect, release chip lock and wake up anyone waiting on the device
  485. */
  486. static void onenand_release_device(struct mtd_info *mtd)
  487. {
  488. struct onenand_chip *this = mtd->priv;
  489. /* Release the chip */
  490. spin_lock(&this->chip_lock);
  491. this->state = FL_READY;
  492. wake_up(&this->wq);
  493. spin_unlock(&this->chip_lock);
  494. }
  495. /**
  496. * onenand_read_ecc - [MTD Interface] Read data with ECC
  497. * @param mtd MTD device structure
  498. * @param from offset to read from
  499. * @param len number of bytes to read
  500. * @param retlen pointer to variable to store the number of read bytes
  501. * @param buf the databuffer to put data
  502. * @param oob_buf filesystem supplied oob data buffer
  503. * @param oobsel oob selection structure
  504. *
  505. * OneNAND read with ECC
  506. */
  507. static int onenand_read_ecc(struct mtd_info *mtd, loff_t from, size_t len,
  508. size_t *retlen, u_char *buf,
  509. u_char *oob_buf, struct nand_oobinfo *oobsel)
  510. {
  511. struct onenand_chip *this = mtd->priv;
  512. int read = 0, column;
  513. int thislen;
  514. int ret = 0;
  515. DEBUG(MTD_DEBUG_LEVEL3, "onenand_read_ecc: from = 0x%08x, len = %i\n", (unsigned int) from, (int) len);
  516. /* Do not allow reads past end of device */
  517. if ((from + len) > mtd->size) {
  518. DEBUG(MTD_DEBUG_LEVEL0, "onenand_read_ecc: Attempt read beyond end of device\n");
  519. *retlen = 0;
  520. return -EINVAL;
  521. }
  522. /* Grab the lock and see if the device is available */
  523. onenand_get_device(mtd, FL_READING);
  524. /* TODO handling oob */
  525. while (read < len) {
  526. thislen = min_t(int, mtd->oobblock, len - read);
  527. column = from & (mtd->oobblock - 1);
  528. if (column + thislen > mtd->oobblock)
  529. thislen = mtd->oobblock - column;
  530. if (!onenand_check_bufferram(mtd, from)) {
  531. this->command(mtd, ONENAND_CMD_READ, from, mtd->oobblock);
  532. ret = this->wait(mtd, FL_READING);
  533. /* First copy data and check return value for ECC handling */
  534. onenand_update_bufferram(mtd, from, 1);
  535. }
  536. this->read_bufferram(mtd, ONENAND_DATARAM, buf, column, thislen);
  537. read += thislen;
  538. if (read == len)
  539. break;
  540. if (ret) {
  541. DEBUG(MTD_DEBUG_LEVEL0, "onenand_read_ecc: read failed = %d\n", ret);
  542. goto out;
  543. }
  544. from += thislen;
  545. buf += thislen;
  546. }
  547. out:
  548. /* Deselect and wake up anyone waiting on the device */
  549. onenand_release_device(mtd);
  550. /*
  551. * Return success, if no ECC failures, else -EBADMSG
  552. * fs driver will take care of that, because
  553. * retlen == desired len and result == -EBADMSG
  554. */
  555. *retlen = read;
  556. return ret;
  557. }
  558. /**
  559. * onenand_read - [MTD Interface] MTD compability function for onenand_read_ecc
  560. * @param mtd MTD device structure
  561. * @param from offset to read from
  562. * @param len number of bytes to read
  563. * @param retlen pointer to variable to store the number of read bytes
  564. * @param buf the databuffer to put data
  565. *
  566. * This function simply calls onenand_read_ecc with oob buffer and oobsel = NULL
  567. */
  568. static int onenand_read(struct mtd_info *mtd, loff_t from, size_t len,
  569. size_t *retlen, u_char *buf)
  570. {
  571. return onenand_read_ecc(mtd, from, len, retlen, buf, NULL, NULL);
  572. }
  573. /**
  574. * onenand_read_oob - [MTD Interface] OneNAND read out-of-band
  575. * @param mtd MTD device structure
  576. * @param from offset to read from
  577. * @param len number of bytes to read
  578. * @param retlen pointer to variable to store the number of read bytes
  579. * @param buf the databuffer to put data
  580. *
  581. * OneNAND read out-of-band data from the spare area
  582. */
  583. static int onenand_read_oob(struct mtd_info *mtd, loff_t from, size_t len,
  584. size_t *retlen, u_char *buf)
  585. {
  586. struct onenand_chip *this = mtd->priv;
  587. int read = 0, thislen, column;
  588. int ret = 0;
  589. DEBUG(MTD_DEBUG_LEVEL3, "onenand_read_oob: from = 0x%08x, len = %i\n", (unsigned int) from, (int) len);
  590. /* Initialize return length value */
  591. *retlen = 0;
  592. /* Do not allow reads past end of device */
  593. if (unlikely((from + len) > mtd->size)) {
  594. DEBUG(MTD_DEBUG_LEVEL0, "onenand_read_oob: Attempt read beyond end of device\n");
  595. return -EINVAL;
  596. }
  597. /* Grab the lock and see if the device is available */
  598. onenand_get_device(mtd, FL_READING);
  599. column = from & (mtd->oobsize - 1);
  600. while (read < len) {
  601. thislen = mtd->oobsize - column;
  602. thislen = min_t(int, thislen, len);
  603. this->command(mtd, ONENAND_CMD_READOOB, from, mtd->oobsize);
  604. onenand_update_bufferram(mtd, from, 0);
  605. ret = this->wait(mtd, FL_READING);
  606. /* First copy data and check return value for ECC handling */
  607. this->read_bufferram(mtd, ONENAND_SPARERAM, buf, column, thislen);
  608. read += thislen;
  609. if (read == len)
  610. break;
  611. if (ret) {
  612. DEBUG(MTD_DEBUG_LEVEL0, "onenand_read_oob: read failed = %d\n", ret);
  613. goto out;
  614. }
  615. buf += thislen;
  616. /* Read more? */
  617. if (read < len) {
  618. /* Page size */
  619. from += mtd->oobblock;
  620. column = 0;
  621. }
  622. }
  623. out:
  624. /* Deselect and wake up anyone waiting on the device */
  625. onenand_release_device(mtd);
  626. *retlen = read;
  627. return ret;
  628. }
  629. #ifdef CONFIG_MTD_ONENAND_VERIFY_WRITE
  630. /**
  631. * onenand_verify_page - [GENERIC] verify the chip contents after a write
  632. * @param mtd MTD device structure
  633. * @param buf the databuffer to verify
  634. *
  635. * Check DataRAM area directly
  636. */
  637. static int onenand_verify_page(struct mtd_info *mtd, u_char *buf, loff_t addr)
  638. {
  639. struct onenand_chip *this = mtd->priv;
  640. void __iomem *dataram0, *dataram1;
  641. int ret = 0;
  642. this->command(mtd, ONENAND_CMD_READ, addr, mtd->oobblock);
  643. ret = this->wait(mtd, FL_READING);
  644. if (ret)
  645. return ret;
  646. onenand_update_bufferram(mtd, addr, 1);
  647. /* Check, if the two dataram areas are same */
  648. dataram0 = this->base + ONENAND_DATARAM;
  649. dataram1 = dataram0 + mtd->oobblock;
  650. if (memcmp(dataram0, dataram1, mtd->oobblock))
  651. return -EBADMSG;
  652. return 0;
  653. }
  654. #else
  655. #define onenand_verify_page(...) (0)
  656. #endif
  657. #define NOTALIGNED(x) ((x & (mtd->oobblock - 1)) != 0)
  658. /**
  659. * onenand_write_ecc - [MTD Interface] OneNAND write with ECC
  660. * @param mtd MTD device structure
  661. * @param to offset to write to
  662. * @param len number of bytes to write
  663. * @param retlen pointer to variable to store the number of written bytes
  664. * @param buf the data to write
  665. * @param eccbuf filesystem supplied oob data buffer
  666. * @param oobsel oob selection structure
  667. *
  668. * OneNAND write with ECC
  669. */
  670. static int onenand_write_ecc(struct mtd_info *mtd, loff_t to, size_t len,
  671. size_t *retlen, const u_char *buf,
  672. u_char *eccbuf, struct nand_oobinfo *oobsel)
  673. {
  674. struct onenand_chip *this = mtd->priv;
  675. int written = 0;
  676. int ret = 0;
  677. DEBUG(MTD_DEBUG_LEVEL3, "onenand_write_ecc: to = 0x%08x, len = %i\n", (unsigned int) to, (int) len);
  678. /* Initialize retlen, in case of early exit */
  679. *retlen = 0;
  680. /* Do not allow writes past end of device */
  681. if (unlikely((to + len) > mtd->size)) {
  682. DEBUG(MTD_DEBUG_LEVEL0, "onenand_write_ecc: Attempt write to past end of device\n");
  683. return -EINVAL;
  684. }
  685. /* Reject writes, which are not page aligned */
  686. if (unlikely(NOTALIGNED(to)) || unlikely(NOTALIGNED(len))) {
  687. DEBUG(MTD_DEBUG_LEVEL0, "onenand_write_ecc: Attempt to write not page aligned data\n");
  688. return -EINVAL;
  689. }
  690. /* Grab the lock and see if the device is available */
  691. onenand_get_device(mtd, FL_WRITING);
  692. /* Loop until all data write */
  693. while (written < len) {
  694. int thislen = min_t(int, mtd->oobblock, len - written);
  695. this->command(mtd, ONENAND_CMD_BUFFERRAM, to, mtd->oobblock);
  696. this->write_bufferram(mtd, ONENAND_DATARAM, buf, 0, thislen);
  697. this->write_bufferram(mtd, ONENAND_SPARERAM, ffchars, 0, mtd->oobsize);
  698. this->command(mtd, ONENAND_CMD_PROG, to, mtd->oobblock);
  699. onenand_update_bufferram(mtd, to, 1);
  700. ret = this->wait(mtd, FL_WRITING);
  701. if (ret) {
  702. DEBUG(MTD_DEBUG_LEVEL0, "onenand_write_ecc: write filaed %d\n", ret);
  703. goto out;
  704. }
  705. written += thislen;
  706. /* Only check verify write turn on */
  707. ret = onenand_verify_page(mtd, (u_char *) buf, to);
  708. if (ret) {
  709. DEBUG(MTD_DEBUG_LEVEL0, "onenand_write_ecc: verify failed %d\n", ret);
  710. goto out;
  711. }
  712. if (written == len)
  713. break;
  714. to += thislen;
  715. buf += thislen;
  716. }
  717. out:
  718. /* Deselect and wake up anyone waiting on the device */
  719. onenand_release_device(mtd);
  720. *retlen = written;
  721. return ret;
  722. }
  723. /**
  724. * onenand_write - [MTD Interface] compability function for onenand_write_ecc
  725. * @param mtd MTD device structure
  726. * @param to offset to write to
  727. * @param len number of bytes to write
  728. * @param retlen pointer to variable to store the number of written bytes
  729. * @param buf the data to write
  730. *
  731. * This function simply calls onenand_write_ecc
  732. * with oob buffer and oobsel = NULL
  733. */
  734. static int onenand_write(struct mtd_info *mtd, loff_t to, size_t len,
  735. size_t *retlen, const u_char *buf)
  736. {
  737. return onenand_write_ecc(mtd, to, len, retlen, buf, NULL, NULL);
  738. }
  739. /**
  740. * onenand_write_oob - [MTD Interface] OneNAND write out-of-band
  741. * @param mtd MTD device structure
  742. * @param to offset to write to
  743. * @param len number of bytes to write
  744. * @param retlen pointer to variable to store the number of written bytes
  745. * @param buf the data to write
  746. *
  747. * OneNAND write out-of-band
  748. */
  749. static int onenand_write_oob(struct mtd_info *mtd, loff_t to, size_t len,
  750. size_t *retlen, const u_char *buf)
  751. {
  752. struct onenand_chip *this = mtd->priv;
  753. int column, status;
  754. int written = 0;
  755. DEBUG(MTD_DEBUG_LEVEL3, "onenand_write_oob: to = 0x%08x, len = %i\n", (unsigned int) to, (int) len);
  756. /* Initialize retlen, in case of early exit */
  757. *retlen = 0;
  758. /* Do not allow writes past end of device */
  759. if (unlikely((to + len) > mtd->size)) {
  760. DEBUG(MTD_DEBUG_LEVEL0, "onenand_write_oob: Attempt write to past end of device\n");
  761. return -EINVAL;
  762. }
  763. /* Grab the lock and see if the device is available */
  764. onenand_get_device(mtd, FL_WRITING);
  765. /* Loop until all data write */
  766. while (written < len) {
  767. int thislen = min_t(int, mtd->oobsize, len - written);
  768. column = to & (mtd->oobsize - 1);
  769. this->command(mtd, ONENAND_CMD_BUFFERRAM, to, mtd->oobsize);
  770. this->write_bufferram(mtd, ONENAND_SPARERAM, ffchars, 0, mtd->oobsize);
  771. this->write_bufferram(mtd, ONENAND_SPARERAM, buf, column, thislen);
  772. this->command(mtd, ONENAND_CMD_PROGOOB, to, mtd->oobsize);
  773. onenand_update_bufferram(mtd, to, 0);
  774. status = this->wait(mtd, FL_WRITING);
  775. if (status)
  776. goto out;
  777. written += thislen;
  778. if (written == len)
  779. break;
  780. to += thislen;
  781. buf += thislen;
  782. }
  783. out:
  784. /* Deselect and wake up anyone waiting on the device */
  785. onenand_release_device(mtd);
  786. *retlen = written;
  787. return 0;
  788. }
  789. /**
  790. * onenand_writev_ecc - [MTD Interface] write with iovec with ecc
  791. * @param mtd MTD device structure
  792. * @param vecs the iovectors to write
  793. * @param count number of vectors
  794. * @param to offset to write to
  795. * @param retlen pointer to variable to store the number of written bytes
  796. * @param eccbuf filesystem supplied oob data buffer
  797. * @param oobsel oob selection structure
  798. *
  799. * OneNAND write with iovec with ecc
  800. */
  801. static int onenand_writev_ecc(struct mtd_info *mtd, const struct kvec *vecs,
  802. unsigned long count, loff_t to, size_t *retlen,
  803. u_char *eccbuf, struct nand_oobinfo *oobsel)
  804. {
  805. struct onenand_chip *this = mtd->priv;
  806. unsigned char *pbuf;
  807. size_t total_len, len;
  808. int i, written = 0;
  809. int ret = 0;
  810. /* Preset written len for early exit */
  811. *retlen = 0;
  812. /* Calculate total length of data */
  813. total_len = 0;
  814. for (i = 0; i < count; i++)
  815. total_len += vecs[i].iov_len;
  816. DEBUG(MTD_DEBUG_LEVEL3, "onenand_writev_ecc: to = 0x%08x, len = %i, count = %ld\n", (unsigned int) to, (unsigned int) total_len, count);
  817. /* Do not allow write past end of the device */
  818. if (unlikely((to + total_len) > mtd->size)) {
  819. DEBUG(MTD_DEBUG_LEVEL0, "onenand_writev_ecc: Attempted write past end of device\n");
  820. return -EINVAL;
  821. }
  822. /* Reject writes, which are not page aligned */
  823. if (unlikely(NOTALIGNED(to)) || unlikely(NOTALIGNED(total_len))) {
  824. DEBUG(MTD_DEBUG_LEVEL0, "onenand_writev_ecc: Attempt to write not page aligned data\n");
  825. return -EINVAL;
  826. }
  827. /* Grab the lock and see if the device is available */
  828. onenand_get_device(mtd, FL_WRITING);
  829. /* TODO handling oob */
  830. /* Loop until all keve's data has been written */
  831. len = 0;
  832. while (count) {
  833. pbuf = this->page_buf;
  834. /*
  835. * If the given tuple is >= pagesize then
  836. * write it out from the iov
  837. */
  838. if ((vecs->iov_len - len) >= mtd->oobblock) {
  839. pbuf = vecs->iov_base + len;
  840. len += mtd->oobblock;
  841. /* Check, if we have to switch to the next tuple */
  842. if (len >= (int) vecs->iov_len) {
  843. vecs++;
  844. len = 0;
  845. count--;
  846. }
  847. } else {
  848. int cnt = 0, thislen;
  849. while (cnt < mtd->oobblock) {
  850. thislen = min_t(int, mtd->oobblock - cnt, vecs->iov_len - len);
  851. memcpy(this->page_buf + cnt, vecs->iov_base + len, thislen);
  852. cnt += thislen;
  853. len += thislen;
  854. /* Check, if we have to switch to the next tuple */
  855. if (len >= (int) vecs->iov_len) {
  856. vecs++;
  857. len = 0;
  858. count--;
  859. }
  860. }
  861. }
  862. this->command(mtd, ONENAND_CMD_BUFFERRAM, to, mtd->oobblock);
  863. this->write_bufferram(mtd, ONENAND_DATARAM, pbuf, 0, mtd->oobblock);
  864. this->write_bufferram(mtd, ONENAND_SPARERAM, ffchars, 0, mtd->oobsize);
  865. this->command(mtd, ONENAND_CMD_PROG, to, mtd->oobblock);
  866. onenand_update_bufferram(mtd, to, 1);
  867. ret = this->wait(mtd, FL_WRITING);
  868. if (ret) {
  869. DEBUG(MTD_DEBUG_LEVEL0, "onenand_writev_ecc: write failed %d\n", ret);
  870. goto out;
  871. }
  872. /* Only check verify write turn on */
  873. ret = onenand_verify_page(mtd, (u_char *) pbuf, to);
  874. if (ret) {
  875. DEBUG(MTD_DEBUG_LEVEL0, "onenand_writev_ecc: verify failed %d\n", ret);
  876. goto out;
  877. }
  878. written += mtd->oobblock;
  879. to += mtd->oobblock;
  880. }
  881. out:
  882. /* Deselect and wakt up anyone waiting on the device */
  883. onenand_release_device(mtd);
  884. *retlen = written;
  885. return 0;
  886. }
  887. /**
  888. * onenand_writev - [MTD Interface] compabilty function for onenand_writev_ecc
  889. * @param mtd MTD device structure
  890. * @param vecs the iovectors to write
  891. * @param count number of vectors
  892. * @param to offset to write to
  893. * @param retlen pointer to variable to store the number of written bytes
  894. *
  895. * OneNAND write with kvec. This just calls the ecc function
  896. */
  897. static int onenand_writev(struct mtd_info *mtd, const struct kvec *vecs,
  898. unsigned long count, loff_t to, size_t *retlen)
  899. {
  900. return onenand_writev_ecc(mtd, vecs, count, to, retlen, NULL, NULL);
  901. }
  902. /**
  903. * onenand_block_checkbad - [GENERIC] Check if a block is marked bad
  904. * @param mtd MTD device structure
  905. * @param ofs offset from device start
  906. * @param getchip 0, if the chip is already selected
  907. * @param allowbbt 1, if its allowed to access the bbt area
  908. *
  909. * Check, if the block is bad. Either by reading the bad block table or
  910. * calling of the scan function.
  911. */
  912. static int onenand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int getchip, int allowbbt)
  913. {
  914. struct onenand_chip *this = mtd->priv;
  915. struct bbm_info *bbm = this->bbm;
  916. /* Return info from the table */
  917. return bbm->isbad_bbt(mtd, ofs, allowbbt);
  918. }
  919. /**
  920. * onenand_erase - [MTD Interface] erase block(s)
  921. * @param mtd MTD device structure
  922. * @param instr erase instruction
  923. *
  924. * Erase one ore more blocks
  925. */
  926. static int onenand_erase(struct mtd_info *mtd, struct erase_info *instr)
  927. {
  928. struct onenand_chip *this = mtd->priv;
  929. unsigned int block_size;
  930. loff_t addr;
  931. int len;
  932. int ret = 0;
  933. DEBUG(MTD_DEBUG_LEVEL3, "onenand_erase: start = 0x%08x, len = %i\n", (unsigned int) instr->addr, (unsigned int) instr->len);
  934. block_size = (1 << this->erase_shift);
  935. /* Start address must align on block boundary */
  936. if (unlikely(instr->addr & (block_size - 1))) {
  937. DEBUG(MTD_DEBUG_LEVEL0, "onenand_erase: Unaligned address\n");
  938. return -EINVAL;
  939. }
  940. /* Length must align on block boundary */
  941. if (unlikely(instr->len & (block_size - 1))) {
  942. DEBUG(MTD_DEBUG_LEVEL0, "onenand_erase: Length not block aligned\n");
  943. return -EINVAL;
  944. }
  945. /* Do not allow erase past end of device */
  946. if (unlikely((instr->len + instr->addr) > mtd->size)) {
  947. DEBUG(MTD_DEBUG_LEVEL0, "onenand_erase: Erase past end of device\n");
  948. return -EINVAL;
  949. }
  950. instr->fail_addr = 0xffffffff;
  951. /* Grab the lock and see if the device is available */
  952. onenand_get_device(mtd, FL_ERASING);
  953. /* Loop throught the pages */
  954. len = instr->len;
  955. addr = instr->addr;
  956. instr->state = MTD_ERASING;
  957. while (len) {
  958. /* Check if we have a bad block, we do not erase bad blocks */
  959. if (onenand_block_checkbad(mtd, addr, 0, 0)) {
  960. printk (KERN_WARNING "onenand_erase: attempt to erase a bad block at addr 0x%08x\n", (unsigned int) addr);
  961. instr->state = MTD_ERASE_FAILED;
  962. goto erase_exit;
  963. }
  964. this->command(mtd, ONENAND_CMD_ERASE, addr, block_size);
  965. ret = this->wait(mtd, FL_ERASING);
  966. /* Check, if it is write protected */
  967. if (ret) {
  968. if (ret == -EPERM)
  969. DEBUG(MTD_DEBUG_LEVEL0, "onenand_erase: Device is write protected!!!\n");
  970. else
  971. DEBUG(MTD_DEBUG_LEVEL0, "onenand_erase: Failed erase, block %d\n", (unsigned) (addr >> this->erase_shift));
  972. instr->state = MTD_ERASE_FAILED;
  973. instr->fail_addr = addr;
  974. goto erase_exit;
  975. }
  976. len -= block_size;
  977. addr += block_size;
  978. }
  979. instr->state = MTD_ERASE_DONE;
  980. erase_exit:
  981. ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
  982. /* Do call back function */
  983. if (!ret)
  984. mtd_erase_callback(instr);
  985. /* Deselect and wake up anyone waiting on the device */
  986. onenand_release_device(mtd);
  987. return ret;
  988. }
  989. /**
  990. * onenand_sync - [MTD Interface] sync
  991. * @param mtd MTD device structure
  992. *
  993. * Sync is actually a wait for chip ready function
  994. */
  995. static void onenand_sync(struct mtd_info *mtd)
  996. {
  997. DEBUG(MTD_DEBUG_LEVEL3, "onenand_sync: called\n");
  998. /* Grab the lock and see if the device is available */
  999. onenand_get_device(mtd, FL_SYNCING);
  1000. /* Release it and go back */
  1001. onenand_release_device(mtd);
  1002. }
  1003. /**
  1004. * onenand_block_isbad - [MTD Interface] Check whether the block at the given offset is bad
  1005. * @param mtd MTD device structure
  1006. * @param ofs offset relative to mtd start
  1007. *
  1008. * Check whether the block is bad
  1009. */
  1010. static int onenand_block_isbad(struct mtd_info *mtd, loff_t ofs)
  1011. {
  1012. /* Check for invalid offset */
  1013. if (ofs > mtd->size)
  1014. return -EINVAL;
  1015. return onenand_block_checkbad(mtd, ofs, 1, 0);
  1016. }
  1017. /**
  1018. * onenand_default_block_markbad - [DEFAULT] mark a block bad
  1019. * @param mtd MTD device structure
  1020. * @param ofs offset from device start
  1021. *
  1022. * This is the default implementation, which can be overridden by
  1023. * a hardware specific driver.
  1024. */
  1025. static int onenand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
  1026. {
  1027. struct onenand_chip *this = mtd->priv;
  1028. struct bbm_info *bbm = this->bbm;
  1029. u_char buf[2] = {0, 0};
  1030. size_t retlen;
  1031. int block;
  1032. /* Get block number */
  1033. block = ((int) ofs) >> bbm->bbt_erase_shift;
  1034. if (bbm->bbt)
  1035. bbm->bbt[block >> 2] |= 0x01 << ((block & 0x03) << 1);
  1036. /* We write two bytes, so we dont have to mess with 16 bit access */
  1037. ofs += mtd->oobsize + (bbm->badblockpos & ~0x01);
  1038. return mtd->write_oob(mtd, ofs , 2, &retlen, buf);
  1039. }
  1040. /**
  1041. * onenand_block_markbad - [MTD Interface] Mark the block at the given offset as bad
  1042. * @param mtd MTD device structure
  1043. * @param ofs offset relative to mtd start
  1044. *
  1045. * Mark the block as bad
  1046. */
  1047. static int onenand_block_markbad(struct mtd_info *mtd, loff_t ofs)
  1048. {
  1049. struct onenand_chip *this = mtd->priv;
  1050. int ret;
  1051. ret = onenand_block_isbad(mtd, ofs);
  1052. if (ret) {
  1053. /* If it was bad already, return success and do nothing */
  1054. if (ret > 0)
  1055. return 0;
  1056. return ret;
  1057. }
  1058. return this->block_markbad(mtd, ofs);
  1059. }
  1060. /**
  1061. * onenand_unlock - [MTD Interface] Unlock block(s)
  1062. * @param mtd MTD device structure
  1063. * @param ofs offset relative to mtd start
  1064. * @param len number of bytes to unlock
  1065. *
  1066. * Unlock one or more blocks
  1067. */
  1068. static int onenand_unlock(struct mtd_info *mtd, loff_t ofs, size_t len)
  1069. {
  1070. struct onenand_chip *this = mtd->priv;
  1071. int start, end, block, value, status;
  1072. start = ofs >> this->erase_shift;
  1073. end = len >> this->erase_shift;
  1074. /* Continuous lock scheme */
  1075. if (this->options & ONENAND_CONT_LOCK) {
  1076. /* Set start block address */
  1077. this->write_word(start, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
  1078. /* Set end block address */
  1079. this->write_word(end - 1, this->base + ONENAND_REG_END_BLOCK_ADDRESS);
  1080. /* Write unlock command */
  1081. this->command(mtd, ONENAND_CMD_UNLOCK, 0, 0);
  1082. /* There's no return value */
  1083. this->wait(mtd, FL_UNLOCKING);
  1084. /* Sanity check */
  1085. while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
  1086. & ONENAND_CTRL_ONGO)
  1087. continue;
  1088. /* Check lock status */
  1089. status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
  1090. if (!(status & ONENAND_WP_US))
  1091. printk(KERN_ERR "wp status = 0x%x\n", status);
  1092. return 0;
  1093. }
  1094. /* Block lock scheme */
  1095. for (block = start; block < end; block++) {
  1096. /* Set block address */
  1097. value = onenand_block_address(this, block);
  1098. this->write_word(value, this->base + ONENAND_REG_START_ADDRESS1);
  1099. /* Select DataRAM for DDP */
  1100. value = onenand_bufferram_address(this, block);
  1101. this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
  1102. /* Set start block address */
  1103. this->write_word(block, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
  1104. /* Write unlock command */
  1105. this->command(mtd, ONENAND_CMD_UNLOCK, 0, 0);
  1106. /* There's no return value */
  1107. this->wait(mtd, FL_UNLOCKING);
  1108. /* Sanity check */
  1109. while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
  1110. & ONENAND_CTRL_ONGO)
  1111. continue;
  1112. /* Check lock status */
  1113. status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
  1114. if (!(status & ONENAND_WP_US))
  1115. printk(KERN_ERR "block = %d, wp status = 0x%x\n", block, status);
  1116. }
  1117. return 0;
  1118. }
  1119. /**
  1120. * onenand_print_device_info - Print device ID
  1121. * @param device device ID
  1122. *
  1123. * Print device ID
  1124. */
  1125. static void onenand_print_device_info(int device)
  1126. {
  1127. int vcc, demuxed, ddp, density;
  1128. vcc = device & ONENAND_DEVICE_VCC_MASK;
  1129. demuxed = device & ONENAND_DEVICE_IS_DEMUX;
  1130. ddp = device & ONENAND_DEVICE_IS_DDP;
  1131. density = device >> ONENAND_DEVICE_DENSITY_SHIFT;
  1132. printk(KERN_INFO "%sOneNAND%s %dMB %sV 16-bit (0x%02x)\n",
  1133. demuxed ? "" : "Muxed ",
  1134. ddp ? "(DDP)" : "",
  1135. (16 << density),
  1136. vcc ? "2.65/3.3" : "1.8",
  1137. device);
  1138. }
  1139. static const struct onenand_manufacturers onenand_manuf_ids[] = {
  1140. {ONENAND_MFR_SAMSUNG, "Samsung"},
  1141. };
  1142. /**
  1143. * onenand_check_maf - Check manufacturer ID
  1144. * @param manuf manufacturer ID
  1145. *
  1146. * Check manufacturer ID
  1147. */
  1148. static int onenand_check_maf(int manuf)
  1149. {
  1150. int size = ARRAY_SIZE(onenand_manuf_ids);
  1151. char *name;
  1152. int i;
  1153. for (i = 0; i < size; i++)
  1154. if (manuf == onenand_manuf_ids[i].id)
  1155. break;
  1156. if (i < size)
  1157. name = onenand_manuf_ids[i].name;
  1158. else
  1159. name = "Unknown";
  1160. printk(KERN_DEBUG "OneNAND Manufacturer: %s (0x%0x)\n", name, manuf);
  1161. return (i == size);
  1162. }
  1163. /**
  1164. * onenand_probe - [OneNAND Interface] Probe the OneNAND device
  1165. * @param mtd MTD device structure
  1166. *
  1167. * OneNAND detection method:
  1168. * Compare the the values from command with ones from register
  1169. */
  1170. static int onenand_probe(struct mtd_info *mtd)
  1171. {
  1172. struct onenand_chip *this = mtd->priv;
  1173. int bram_maf_id, bram_dev_id, maf_id, dev_id;
  1174. int version_id;
  1175. int density;
  1176. /* Send the command for reading device ID from BootRAM */
  1177. this->write_word(ONENAND_CMD_READID, this->base + ONENAND_BOOTRAM);
  1178. /* Read manufacturer and device IDs from BootRAM */
  1179. bram_maf_id = this->read_word(this->base + ONENAND_BOOTRAM + 0x0);
  1180. bram_dev_id = this->read_word(this->base + ONENAND_BOOTRAM + 0x2);
  1181. /* Check manufacturer ID */
  1182. if (onenand_check_maf(bram_maf_id))
  1183. return -ENXIO;
  1184. /* Reset OneNAND to read default register values */
  1185. this->write_word(ONENAND_CMD_RESET, this->base + ONENAND_BOOTRAM);
  1186. /* Read manufacturer and device IDs from Register */
  1187. maf_id = this->read_word(this->base + ONENAND_REG_MANUFACTURER_ID);
  1188. dev_id = this->read_word(this->base + ONENAND_REG_DEVICE_ID);
  1189. /* Check OneNAND device */
  1190. if (maf_id != bram_maf_id || dev_id != bram_dev_id)
  1191. return -ENXIO;
  1192. /* Flash device information */
  1193. onenand_print_device_info(dev_id);
  1194. this->device_id = dev_id;
  1195. density = dev_id >> ONENAND_DEVICE_DENSITY_SHIFT;
  1196. this->chipsize = (16 << density) << 20;
  1197. /* Set density mask. it is used for DDP */
  1198. this->density_mask = (1 << (density + 6));
  1199. /* OneNAND page size & block size */
  1200. /* The data buffer size is equal to page size */
  1201. mtd->oobblock = this->read_word(this->base + ONENAND_REG_DATA_BUFFER_SIZE);
  1202. mtd->oobsize = mtd->oobblock >> 5;
  1203. /* Pagers per block is always 64 in OneNAND */
  1204. mtd->erasesize = mtd->oobblock << 6;
  1205. this->erase_shift = ffs(mtd->erasesize) - 1;
  1206. this->page_shift = ffs(mtd->oobblock) - 1;
  1207. this->ppb_shift = (this->erase_shift - this->page_shift);
  1208. this->page_mask = (mtd->erasesize / mtd->oobblock) - 1;
  1209. /* REVIST: Multichip handling */
  1210. mtd->size = this->chipsize;
  1211. /* Version ID */
  1212. version_id = this->read_word(this->base + ONENAND_REG_VERSION_ID);
  1213. printk(KERN_DEBUG "OneNAND version = 0x%04x\n", version_id);
  1214. /* Lock scheme */
  1215. if (density <= ONENAND_DEVICE_DENSITY_512Mb &&
  1216. !(version_id >> ONENAND_VERSION_PROCESS_SHIFT)) {
  1217. printk(KERN_INFO "Lock scheme is Continues Lock\n");
  1218. this->options |= ONENAND_CONT_LOCK;
  1219. }
  1220. return 0;
  1221. }
  1222. /**
  1223. * onenand_suspend - [MTD Interface] Suspend the OneNAND flash
  1224. * @param mtd MTD device structure
  1225. */
  1226. static int onenand_suspend(struct mtd_info *mtd)
  1227. {
  1228. return onenand_get_device(mtd, FL_PM_SUSPENDED);
  1229. }
  1230. /**
  1231. * onenand_resume - [MTD Interface] Resume the OneNAND flash
  1232. * @param mtd MTD device structure
  1233. */
  1234. static void onenand_resume(struct mtd_info *mtd)
  1235. {
  1236. struct onenand_chip *this = mtd->priv;
  1237. if (this->state == FL_PM_SUSPENDED)
  1238. onenand_release_device(mtd);
  1239. else
  1240. printk(KERN_ERR "resume() called for the chip which is not"
  1241. "in suspended state\n");
  1242. }
  1243. /**
  1244. * onenand_scan - [OneNAND Interface] Scan for the OneNAND device
  1245. * @param mtd MTD device structure
  1246. * @param maxchips Number of chips to scan for
  1247. *
  1248. * This fills out all the not initialized function pointers
  1249. * with the defaults.
  1250. * The flash ID is read and the mtd/chip structures are
  1251. * filled with the appropriate values.
  1252. */
  1253. int onenand_scan(struct mtd_info *mtd, int maxchips)
  1254. {
  1255. struct onenand_chip *this = mtd->priv;
  1256. if (!this->read_word)
  1257. this->read_word = onenand_readw;
  1258. if (!this->write_word)
  1259. this->write_word = onenand_writew;
  1260. if (!this->command)
  1261. this->command = onenand_command;
  1262. if (!this->wait)
  1263. this->wait = onenand_wait;
  1264. if (!this->read_bufferram)
  1265. this->read_bufferram = onenand_read_bufferram;
  1266. if (!this->write_bufferram)
  1267. this->write_bufferram = onenand_write_bufferram;
  1268. if (!this->block_markbad)
  1269. this->block_markbad = onenand_default_block_markbad;
  1270. if (!this->scan_bbt)
  1271. this->scan_bbt = onenand_default_bbt;
  1272. if (onenand_probe(mtd))
  1273. return -ENXIO;
  1274. /* Set Sync. Burst Read after probing */
  1275. if (this->mmcontrol) {
  1276. printk(KERN_INFO "OneNAND Sync. Burst Read support\n");
  1277. this->read_bufferram = onenand_sync_read_bufferram;
  1278. }
  1279. /* Allocate buffers, if necessary */
  1280. if (!this->page_buf) {
  1281. size_t len;
  1282. len = mtd->oobblock + mtd->oobsize;
  1283. this->page_buf = kmalloc(len, GFP_KERNEL);
  1284. if (!this->page_buf) {
  1285. printk(KERN_ERR "onenand_scan(): Can't allocate page_buf\n");
  1286. return -ENOMEM;
  1287. }
  1288. this->options |= ONENAND_PAGEBUF_ALLOC;
  1289. }
  1290. this->state = FL_READY;
  1291. init_waitqueue_head(&this->wq);
  1292. spin_lock_init(&this->chip_lock);
  1293. switch (mtd->oobsize) {
  1294. case 64:
  1295. this->autooob = &onenand_oob_64;
  1296. break;
  1297. case 32:
  1298. this->autooob = &onenand_oob_32;
  1299. break;
  1300. default:
  1301. printk(KERN_WARNING "No OOB scheme defined for oobsize %d\n",
  1302. mtd->oobsize);
  1303. /* To prevent kernel oops */
  1304. this->autooob = &onenand_oob_32;
  1305. break;
  1306. }
  1307. memcpy(&mtd->oobinfo, this->autooob, sizeof(mtd->oobinfo));
  1308. /* Fill in remaining MTD driver data */
  1309. mtd->type = MTD_NANDFLASH;
  1310. mtd->flags = MTD_CAP_NANDFLASH | MTD_ECC;
  1311. mtd->ecctype = MTD_ECC_SW;
  1312. mtd->erase = onenand_erase;
  1313. mtd->point = NULL;
  1314. mtd->unpoint = NULL;
  1315. mtd->read = onenand_read;
  1316. mtd->write = onenand_write;
  1317. mtd->read_ecc = onenand_read_ecc;
  1318. mtd->write_ecc = onenand_write_ecc;
  1319. mtd->read_oob = onenand_read_oob;
  1320. mtd->write_oob = onenand_write_oob;
  1321. mtd->readv = NULL;
  1322. mtd->readv_ecc = NULL;
  1323. mtd->writev = onenand_writev;
  1324. mtd->writev_ecc = onenand_writev_ecc;
  1325. mtd->sync = onenand_sync;
  1326. mtd->lock = NULL;
  1327. mtd->unlock = onenand_unlock;
  1328. mtd->suspend = onenand_suspend;
  1329. mtd->resume = onenand_resume;
  1330. mtd->block_isbad = onenand_block_isbad;
  1331. mtd->block_markbad = onenand_block_markbad;
  1332. mtd->owner = THIS_MODULE;
  1333. /* Unlock whole block */
  1334. mtd->unlock(mtd, 0x0, this->chipsize);
  1335. return this->scan_bbt(mtd);
  1336. }
  1337. /**
  1338. * onenand_release - [OneNAND Interface] Free resources held by the OneNAND device
  1339. * @param mtd MTD device structure
  1340. */
  1341. void onenand_release(struct mtd_info *mtd)
  1342. {
  1343. struct onenand_chip *this = mtd->priv;
  1344. #ifdef CONFIG_MTD_PARTITIONS
  1345. /* Deregister partitions */
  1346. del_mtd_partitions (mtd);
  1347. #endif
  1348. /* Deregister the device */
  1349. del_mtd_device (mtd);
  1350. /* Free bad block table memory, if allocated */
  1351. if (this->bbm)
  1352. kfree(this->bbm);
  1353. /* Buffer allocated by onenand_scan */
  1354. if (this->options & ONENAND_PAGEBUF_ALLOC)
  1355. kfree(this->page_buf);
  1356. }
  1357. EXPORT_SYMBOL_GPL(onenand_scan);
  1358. EXPORT_SYMBOL_GPL(onenand_release);
  1359. MODULE_LICENSE("GPL");
  1360. MODULE_AUTHOR("Kyungmin Park <kyungmin.park@samsung.com>");
  1361. MODULE_DESCRIPTION("Generic OneNAND flash driver code");