onenand_base.c 38 KB

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