onenand_base.c 43 KB

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