onenand_base.c 42 KB

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