onenand_base.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696
  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_oob - [GENERIC] verify the oob contents after a write
  633. * @param mtd MTD device structure
  634. * @param buf the databuffer to verify
  635. * @param to offset to read from
  636. * @param len number of bytes to read and compare
  637. *
  638. */
  639. static int onenand_verify_oob(struct mtd_info *mtd, const u_char *buf, loff_t to, int len)
  640. {
  641. struct onenand_chip *this = mtd->priv;
  642. char *readp = this->page_buf;
  643. int column = to & (mtd->oobsize - 1);
  644. int status, i;
  645. this->command(mtd, ONENAND_CMD_READOOB, to, mtd->oobsize);
  646. onenand_update_bufferram(mtd, to, 0);
  647. status = this->wait(mtd, FL_READING);
  648. if (status)
  649. return status;
  650. this->read_bufferram(mtd, ONENAND_SPARERAM, readp, column, len);
  651. for(i = 0; i < len; i++)
  652. if (buf[i] != 0xFF && buf[i] != readp[i])
  653. return -EBADMSG;
  654. return 0;
  655. }
  656. /**
  657. * onenand_verify_page - [GENERIC] verify the chip contents after a write
  658. * @param mtd MTD device structure
  659. * @param buf the databuffer to verify
  660. *
  661. * Check DataRAM area directly
  662. */
  663. static int onenand_verify_page(struct mtd_info *mtd, u_char *buf, loff_t addr)
  664. {
  665. struct onenand_chip *this = mtd->priv;
  666. void __iomem *dataram0, *dataram1;
  667. int ret = 0;
  668. this->command(mtd, ONENAND_CMD_READ, addr, mtd->oobblock);
  669. ret = this->wait(mtd, FL_READING);
  670. if (ret)
  671. return ret;
  672. onenand_update_bufferram(mtd, addr, 1);
  673. /* Check, if the two dataram areas are same */
  674. dataram0 = this->base + ONENAND_DATARAM;
  675. dataram1 = dataram0 + mtd->oobblock;
  676. if (memcmp(dataram0, dataram1, mtd->oobblock))
  677. return -EBADMSG;
  678. return 0;
  679. }
  680. #else
  681. #define onenand_verify_page(...) (0)
  682. #define onenand_verify_oob(...) (0)
  683. #endif
  684. #define NOTALIGNED(x) ((x & (mtd->oobblock - 1)) != 0)
  685. /**
  686. * onenand_write_ecc - [MTD Interface] OneNAND write with ECC
  687. * @param mtd MTD device structure
  688. * @param to offset to write to
  689. * @param len number of bytes to write
  690. * @param retlen pointer to variable to store the number of written bytes
  691. * @param buf the data to write
  692. * @param eccbuf filesystem supplied oob data buffer
  693. * @param oobsel oob selection structure
  694. *
  695. * OneNAND write with ECC
  696. */
  697. static int onenand_write_ecc(struct mtd_info *mtd, loff_t to, size_t len,
  698. size_t *retlen, const u_char *buf,
  699. u_char *eccbuf, struct nand_oobinfo *oobsel)
  700. {
  701. struct onenand_chip *this = mtd->priv;
  702. int written = 0;
  703. int ret = 0;
  704. DEBUG(MTD_DEBUG_LEVEL3, "onenand_write_ecc: to = 0x%08x, len = %i\n", (unsigned int) to, (int) len);
  705. /* Initialize retlen, in case of early exit */
  706. *retlen = 0;
  707. /* Do not allow writes past end of device */
  708. if (unlikely((to + len) > mtd->size)) {
  709. DEBUG(MTD_DEBUG_LEVEL0, "onenand_write_ecc: Attempt write to past end of device\n");
  710. return -EINVAL;
  711. }
  712. /* Reject writes, which are not page aligned */
  713. if (unlikely(NOTALIGNED(to)) || unlikely(NOTALIGNED(len))) {
  714. DEBUG(MTD_DEBUG_LEVEL0, "onenand_write_ecc: Attempt to write not page aligned data\n");
  715. return -EINVAL;
  716. }
  717. /* Grab the lock and see if the device is available */
  718. onenand_get_device(mtd, FL_WRITING);
  719. /* Loop until all data write */
  720. while (written < len) {
  721. int thislen = min_t(int, mtd->oobblock, len - written);
  722. this->command(mtd, ONENAND_CMD_BUFFERRAM, to, mtd->oobblock);
  723. this->write_bufferram(mtd, ONENAND_DATARAM, buf, 0, thislen);
  724. this->write_bufferram(mtd, ONENAND_SPARERAM, ffchars, 0, mtd->oobsize);
  725. this->command(mtd, ONENAND_CMD_PROG, to, mtd->oobblock);
  726. onenand_update_bufferram(mtd, to, 1);
  727. ret = this->wait(mtd, FL_WRITING);
  728. if (ret) {
  729. DEBUG(MTD_DEBUG_LEVEL0, "onenand_write_ecc: write filaed %d\n", ret);
  730. goto out;
  731. }
  732. written += thislen;
  733. /* Only check verify write turn on */
  734. ret = onenand_verify_page(mtd, (u_char *) buf, to);
  735. if (ret) {
  736. DEBUG(MTD_DEBUG_LEVEL0, "onenand_write_ecc: verify failed %d\n", ret);
  737. goto out;
  738. }
  739. if (written == len)
  740. break;
  741. to += thislen;
  742. buf += thislen;
  743. }
  744. out:
  745. /* Deselect and wake up anyone waiting on the device */
  746. onenand_release_device(mtd);
  747. *retlen = written;
  748. return ret;
  749. }
  750. /**
  751. * onenand_write - [MTD Interface] compability function for onenand_write_ecc
  752. * @param mtd MTD device structure
  753. * @param to offset to write to
  754. * @param len number of bytes to write
  755. * @param retlen pointer to variable to store the number of written bytes
  756. * @param buf the data to write
  757. *
  758. * This function simply calls onenand_write_ecc
  759. * with oob buffer and oobsel = NULL
  760. */
  761. static int onenand_write(struct mtd_info *mtd, loff_t to, size_t len,
  762. size_t *retlen, const u_char *buf)
  763. {
  764. return onenand_write_ecc(mtd, to, len, retlen, buf, NULL, NULL);
  765. }
  766. /**
  767. * onenand_write_oob - [MTD Interface] OneNAND write out-of-band
  768. * @param mtd MTD device structure
  769. * @param to offset to write to
  770. * @param len number of bytes to write
  771. * @param retlen pointer to variable to store the number of written bytes
  772. * @param buf the data to write
  773. *
  774. * OneNAND write out-of-band
  775. */
  776. static int onenand_write_oob(struct mtd_info *mtd, loff_t to, size_t len,
  777. size_t *retlen, const u_char *buf)
  778. {
  779. struct onenand_chip *this = mtd->priv;
  780. int column, ret = 0;
  781. int written = 0;
  782. DEBUG(MTD_DEBUG_LEVEL3, "onenand_write_oob: to = 0x%08x, len = %i\n", (unsigned int) to, (int) len);
  783. /* Initialize retlen, in case of early exit */
  784. *retlen = 0;
  785. /* Do not allow writes past end of device */
  786. if (unlikely((to + len) > mtd->size)) {
  787. DEBUG(MTD_DEBUG_LEVEL0, "onenand_write_oob: Attempt write to past end of device\n");
  788. return -EINVAL;
  789. }
  790. /* Grab the lock and see if the device is available */
  791. onenand_get_device(mtd, FL_WRITING);
  792. /* Loop until all data write */
  793. while (written < len) {
  794. int thislen = min_t(int, mtd->oobsize, len - written);
  795. column = to & (mtd->oobsize - 1);
  796. this->command(mtd, ONENAND_CMD_BUFFERRAM, to, mtd->oobsize);
  797. this->write_bufferram(mtd, ONENAND_SPARERAM, ffchars, 0, mtd->oobsize);
  798. this->write_bufferram(mtd, ONENAND_SPARERAM, buf, column, thislen);
  799. this->command(mtd, ONENAND_CMD_PROGOOB, to, mtd->oobsize);
  800. onenand_update_bufferram(mtd, to, 0);
  801. ret = this->wait(mtd, FL_WRITING);
  802. if (ret) {
  803. DEBUG(MTD_DEBUG_LEVEL0, "onenand_write_oob: write filaed %d\n", ret);
  804. goto out;
  805. }
  806. ret = onenand_verify_oob(mtd, buf, to, thislen);
  807. if (ret) {
  808. DEBUG(MTD_DEBUG_LEVEL0, "onenand_write_oob: verify failed %d\n", ret);
  809. goto out;
  810. }
  811. written += thislen;
  812. if (written == len)
  813. break;
  814. to += thislen;
  815. buf += thislen;
  816. }
  817. out:
  818. /* Deselect and wake up anyone waiting on the device */
  819. onenand_release_device(mtd);
  820. *retlen = written;
  821. return ret;
  822. }
  823. /**
  824. * onenand_writev_ecc - [MTD Interface] write with iovec with ecc
  825. * @param mtd MTD device structure
  826. * @param vecs the iovectors to write
  827. * @param count number of vectors
  828. * @param to offset to write to
  829. * @param retlen pointer to variable to store the number of written bytes
  830. * @param eccbuf filesystem supplied oob data buffer
  831. * @param oobsel oob selection structure
  832. *
  833. * OneNAND write with iovec with ecc
  834. */
  835. static int onenand_writev_ecc(struct mtd_info *mtd, const struct kvec *vecs,
  836. unsigned long count, loff_t to, size_t *retlen,
  837. u_char *eccbuf, struct nand_oobinfo *oobsel)
  838. {
  839. struct onenand_chip *this = mtd->priv;
  840. unsigned char *pbuf;
  841. size_t total_len, len;
  842. int i, written = 0;
  843. int ret = 0;
  844. /* Preset written len for early exit */
  845. *retlen = 0;
  846. /* Calculate total length of data */
  847. total_len = 0;
  848. for (i = 0; i < count; i++)
  849. total_len += vecs[i].iov_len;
  850. DEBUG(MTD_DEBUG_LEVEL3, "onenand_writev_ecc: to = 0x%08x, len = %i, count = %ld\n", (unsigned int) to, (unsigned int) total_len, count);
  851. /* Do not allow write past end of the device */
  852. if (unlikely((to + total_len) > mtd->size)) {
  853. DEBUG(MTD_DEBUG_LEVEL0, "onenand_writev_ecc: Attempted write past end of device\n");
  854. return -EINVAL;
  855. }
  856. /* Reject writes, which are not page aligned */
  857. if (unlikely(NOTALIGNED(to)) || unlikely(NOTALIGNED(total_len))) {
  858. DEBUG(MTD_DEBUG_LEVEL0, "onenand_writev_ecc: Attempt to write not page aligned data\n");
  859. return -EINVAL;
  860. }
  861. /* Grab the lock and see if the device is available */
  862. onenand_get_device(mtd, FL_WRITING);
  863. /* TODO handling oob */
  864. /* Loop until all keve's data has been written */
  865. len = 0;
  866. while (count) {
  867. pbuf = this->page_buf;
  868. /*
  869. * If the given tuple is >= pagesize then
  870. * write it out from the iov
  871. */
  872. if ((vecs->iov_len - len) >= mtd->oobblock) {
  873. pbuf = vecs->iov_base + len;
  874. len += mtd->oobblock;
  875. /* Check, if we have to switch to the next tuple */
  876. if (len >= (int) vecs->iov_len) {
  877. vecs++;
  878. len = 0;
  879. count--;
  880. }
  881. } else {
  882. int cnt = 0, thislen;
  883. while (cnt < mtd->oobblock) {
  884. thislen = min_t(int, mtd->oobblock - cnt, vecs->iov_len - len);
  885. memcpy(this->page_buf + cnt, vecs->iov_base + len, thislen);
  886. cnt += thislen;
  887. len += thislen;
  888. /* Check, if we have to switch to the next tuple */
  889. if (len >= (int) vecs->iov_len) {
  890. vecs++;
  891. len = 0;
  892. count--;
  893. }
  894. }
  895. }
  896. this->command(mtd, ONENAND_CMD_BUFFERRAM, to, mtd->oobblock);
  897. this->write_bufferram(mtd, ONENAND_DATARAM, pbuf, 0, mtd->oobblock);
  898. this->write_bufferram(mtd, ONENAND_SPARERAM, ffchars, 0, mtd->oobsize);
  899. this->command(mtd, ONENAND_CMD_PROG, to, mtd->oobblock);
  900. onenand_update_bufferram(mtd, to, 1);
  901. ret = this->wait(mtd, FL_WRITING);
  902. if (ret) {
  903. DEBUG(MTD_DEBUG_LEVEL0, "onenand_writev_ecc: write failed %d\n", ret);
  904. goto out;
  905. }
  906. /* Only check verify write turn on */
  907. ret = onenand_verify_page(mtd, (u_char *) pbuf, to);
  908. if (ret) {
  909. DEBUG(MTD_DEBUG_LEVEL0, "onenand_writev_ecc: verify failed %d\n", ret);
  910. goto out;
  911. }
  912. written += mtd->oobblock;
  913. to += mtd->oobblock;
  914. }
  915. out:
  916. /* Deselect and wakt up anyone waiting on the device */
  917. onenand_release_device(mtd);
  918. *retlen = written;
  919. return 0;
  920. }
  921. /**
  922. * onenand_writev - [MTD Interface] compabilty function for onenand_writev_ecc
  923. * @param mtd MTD device structure
  924. * @param vecs the iovectors to write
  925. * @param count number of vectors
  926. * @param to offset to write to
  927. * @param retlen pointer to variable to store the number of written bytes
  928. *
  929. * OneNAND write with kvec. This just calls the ecc function
  930. */
  931. static int onenand_writev(struct mtd_info *mtd, const struct kvec *vecs,
  932. unsigned long count, loff_t to, size_t *retlen)
  933. {
  934. return onenand_writev_ecc(mtd, vecs, count, to, retlen, NULL, NULL);
  935. }
  936. /**
  937. * onenand_block_checkbad - [GENERIC] Check if a block is marked bad
  938. * @param mtd MTD device structure
  939. * @param ofs offset from device start
  940. * @param getchip 0, if the chip is already selected
  941. * @param allowbbt 1, if its allowed to access the bbt area
  942. *
  943. * Check, if the block is bad. Either by reading the bad block table or
  944. * calling of the scan function.
  945. */
  946. static int onenand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int getchip, int allowbbt)
  947. {
  948. struct onenand_chip *this = mtd->priv;
  949. struct bbm_info *bbm = this->bbm;
  950. /* Return info from the table */
  951. return bbm->isbad_bbt(mtd, ofs, allowbbt);
  952. }
  953. /**
  954. * onenand_erase - [MTD Interface] erase block(s)
  955. * @param mtd MTD device structure
  956. * @param instr erase instruction
  957. *
  958. * Erase one ore more blocks
  959. */
  960. static int onenand_erase(struct mtd_info *mtd, struct erase_info *instr)
  961. {
  962. struct onenand_chip *this = mtd->priv;
  963. unsigned int block_size;
  964. loff_t addr;
  965. int len;
  966. int ret = 0;
  967. DEBUG(MTD_DEBUG_LEVEL3, "onenand_erase: start = 0x%08x, len = %i\n", (unsigned int) instr->addr, (unsigned int) instr->len);
  968. block_size = (1 << this->erase_shift);
  969. /* Start address must align on block boundary */
  970. if (unlikely(instr->addr & (block_size - 1))) {
  971. DEBUG(MTD_DEBUG_LEVEL0, "onenand_erase: Unaligned address\n");
  972. return -EINVAL;
  973. }
  974. /* Length must align on block boundary */
  975. if (unlikely(instr->len & (block_size - 1))) {
  976. DEBUG(MTD_DEBUG_LEVEL0, "onenand_erase: Length not block aligned\n");
  977. return -EINVAL;
  978. }
  979. /* Do not allow erase past end of device */
  980. if (unlikely((instr->len + instr->addr) > mtd->size)) {
  981. DEBUG(MTD_DEBUG_LEVEL0, "onenand_erase: Erase past end of device\n");
  982. return -EINVAL;
  983. }
  984. instr->fail_addr = 0xffffffff;
  985. /* Grab the lock and see if the device is available */
  986. onenand_get_device(mtd, FL_ERASING);
  987. /* Loop throught the pages */
  988. len = instr->len;
  989. addr = instr->addr;
  990. instr->state = MTD_ERASING;
  991. while (len) {
  992. /* Check if we have a bad block, we do not erase bad blocks */
  993. if (onenand_block_checkbad(mtd, addr, 0, 0)) {
  994. printk (KERN_WARNING "onenand_erase: attempt to erase a bad block at addr 0x%08x\n", (unsigned int) addr);
  995. instr->state = MTD_ERASE_FAILED;
  996. goto erase_exit;
  997. }
  998. this->command(mtd, ONENAND_CMD_ERASE, addr, block_size);
  999. ret = this->wait(mtd, FL_ERASING);
  1000. /* Check, if it is write protected */
  1001. if (ret) {
  1002. if (ret == -EPERM)
  1003. DEBUG(MTD_DEBUG_LEVEL0, "onenand_erase: Device is write protected!!!\n");
  1004. else
  1005. DEBUG(MTD_DEBUG_LEVEL0, "onenand_erase: Failed erase, block %d\n", (unsigned) (addr >> this->erase_shift));
  1006. instr->state = MTD_ERASE_FAILED;
  1007. instr->fail_addr = addr;
  1008. goto erase_exit;
  1009. }
  1010. len -= block_size;
  1011. addr += block_size;
  1012. }
  1013. instr->state = MTD_ERASE_DONE;
  1014. erase_exit:
  1015. ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
  1016. /* Do call back function */
  1017. if (!ret)
  1018. mtd_erase_callback(instr);
  1019. /* Deselect and wake up anyone waiting on the device */
  1020. onenand_release_device(mtd);
  1021. return ret;
  1022. }
  1023. /**
  1024. * onenand_sync - [MTD Interface] sync
  1025. * @param mtd MTD device structure
  1026. *
  1027. * Sync is actually a wait for chip ready function
  1028. */
  1029. static void onenand_sync(struct mtd_info *mtd)
  1030. {
  1031. DEBUG(MTD_DEBUG_LEVEL3, "onenand_sync: called\n");
  1032. /* Grab the lock and see if the device is available */
  1033. onenand_get_device(mtd, FL_SYNCING);
  1034. /* Release it and go back */
  1035. onenand_release_device(mtd);
  1036. }
  1037. /**
  1038. * onenand_block_isbad - [MTD Interface] Check whether the block at the given offset is bad
  1039. * @param mtd MTD device structure
  1040. * @param ofs offset relative to mtd start
  1041. *
  1042. * Check whether the block is bad
  1043. */
  1044. static int onenand_block_isbad(struct mtd_info *mtd, loff_t ofs)
  1045. {
  1046. /* Check for invalid offset */
  1047. if (ofs > mtd->size)
  1048. return -EINVAL;
  1049. return onenand_block_checkbad(mtd, ofs, 1, 0);
  1050. }
  1051. /**
  1052. * onenand_default_block_markbad - [DEFAULT] mark a block bad
  1053. * @param mtd MTD device structure
  1054. * @param ofs offset from device start
  1055. *
  1056. * This is the default implementation, which can be overridden by
  1057. * a hardware specific driver.
  1058. */
  1059. static int onenand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
  1060. {
  1061. struct onenand_chip *this = mtd->priv;
  1062. struct bbm_info *bbm = this->bbm;
  1063. u_char buf[2] = {0, 0};
  1064. size_t retlen;
  1065. int block;
  1066. /* Get block number */
  1067. block = ((int) ofs) >> bbm->bbt_erase_shift;
  1068. if (bbm->bbt)
  1069. bbm->bbt[block >> 2] |= 0x01 << ((block & 0x03) << 1);
  1070. /* We write two bytes, so we dont have to mess with 16 bit access */
  1071. ofs += mtd->oobsize + (bbm->badblockpos & ~0x01);
  1072. return mtd->write_oob(mtd, ofs , 2, &retlen, buf);
  1073. }
  1074. /**
  1075. * onenand_block_markbad - [MTD Interface] Mark the block at the given offset as bad
  1076. * @param mtd MTD device structure
  1077. * @param ofs offset relative to mtd start
  1078. *
  1079. * Mark the block as bad
  1080. */
  1081. static int onenand_block_markbad(struct mtd_info *mtd, loff_t ofs)
  1082. {
  1083. struct onenand_chip *this = mtd->priv;
  1084. int ret;
  1085. ret = onenand_block_isbad(mtd, ofs);
  1086. if (ret) {
  1087. /* If it was bad already, return success and do nothing */
  1088. if (ret > 0)
  1089. return 0;
  1090. return ret;
  1091. }
  1092. return this->block_markbad(mtd, ofs);
  1093. }
  1094. /**
  1095. * onenand_unlock - [MTD Interface] Unlock block(s)
  1096. * @param mtd MTD device structure
  1097. * @param ofs offset relative to mtd start
  1098. * @param len number of bytes to unlock
  1099. *
  1100. * Unlock one or more blocks
  1101. */
  1102. static int onenand_unlock(struct mtd_info *mtd, loff_t ofs, size_t len)
  1103. {
  1104. struct onenand_chip *this = mtd->priv;
  1105. int start, end, block, value, status;
  1106. start = ofs >> this->erase_shift;
  1107. end = len >> this->erase_shift;
  1108. /* Continuous lock scheme */
  1109. if (this->options & ONENAND_CONT_LOCK) {
  1110. /* Set start block address */
  1111. this->write_word(start, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
  1112. /* Set end block address */
  1113. this->write_word(end - 1, this->base + ONENAND_REG_END_BLOCK_ADDRESS);
  1114. /* Write unlock command */
  1115. this->command(mtd, ONENAND_CMD_UNLOCK, 0, 0);
  1116. /* There's no return value */
  1117. this->wait(mtd, FL_UNLOCKING);
  1118. /* Sanity check */
  1119. while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
  1120. & ONENAND_CTRL_ONGO)
  1121. continue;
  1122. /* Check lock status */
  1123. status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
  1124. if (!(status & ONENAND_WP_US))
  1125. printk(KERN_ERR "wp status = 0x%x\n", status);
  1126. return 0;
  1127. }
  1128. /* Block lock scheme */
  1129. for (block = start; block < end; block++) {
  1130. /* Set block address */
  1131. value = onenand_block_address(this, block);
  1132. this->write_word(value, this->base + ONENAND_REG_START_ADDRESS1);
  1133. /* Select DataRAM for DDP */
  1134. value = onenand_bufferram_address(this, block);
  1135. this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
  1136. /* Set start block address */
  1137. this->write_word(block, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
  1138. /* Write unlock command */
  1139. this->command(mtd, ONENAND_CMD_UNLOCK, 0, 0);
  1140. /* There's no return value */
  1141. this->wait(mtd, FL_UNLOCKING);
  1142. /* Sanity check */
  1143. while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
  1144. & ONENAND_CTRL_ONGO)
  1145. continue;
  1146. /* Check lock status */
  1147. status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
  1148. if (!(status & ONENAND_WP_US))
  1149. printk(KERN_ERR "block = %d, wp status = 0x%x\n", block, status);
  1150. }
  1151. return 0;
  1152. }
  1153. /**
  1154. * onenand_print_device_info - Print device ID
  1155. * @param device device ID
  1156. *
  1157. * Print device ID
  1158. */
  1159. static void onenand_print_device_info(int device)
  1160. {
  1161. int vcc, demuxed, ddp, density;
  1162. vcc = device & ONENAND_DEVICE_VCC_MASK;
  1163. demuxed = device & ONENAND_DEVICE_IS_DEMUX;
  1164. ddp = device & ONENAND_DEVICE_IS_DDP;
  1165. density = device >> ONENAND_DEVICE_DENSITY_SHIFT;
  1166. printk(KERN_INFO "%sOneNAND%s %dMB %sV 16-bit (0x%02x)\n",
  1167. demuxed ? "" : "Muxed ",
  1168. ddp ? "(DDP)" : "",
  1169. (16 << density),
  1170. vcc ? "2.65/3.3" : "1.8",
  1171. device);
  1172. }
  1173. static const struct onenand_manufacturers onenand_manuf_ids[] = {
  1174. {ONENAND_MFR_SAMSUNG, "Samsung"},
  1175. };
  1176. /**
  1177. * onenand_check_maf - Check manufacturer ID
  1178. * @param manuf manufacturer ID
  1179. *
  1180. * Check manufacturer ID
  1181. */
  1182. static int onenand_check_maf(int manuf)
  1183. {
  1184. int size = ARRAY_SIZE(onenand_manuf_ids);
  1185. char *name;
  1186. int i;
  1187. for (i = 0; i < size; i++)
  1188. if (manuf == onenand_manuf_ids[i].id)
  1189. break;
  1190. if (i < size)
  1191. name = onenand_manuf_ids[i].name;
  1192. else
  1193. name = "Unknown";
  1194. printk(KERN_DEBUG "OneNAND Manufacturer: %s (0x%0x)\n", name, manuf);
  1195. return (i == size);
  1196. }
  1197. /**
  1198. * onenand_probe - [OneNAND Interface] Probe the OneNAND device
  1199. * @param mtd MTD device structure
  1200. *
  1201. * OneNAND detection method:
  1202. * Compare the the values from command with ones from register
  1203. */
  1204. static int onenand_probe(struct mtd_info *mtd)
  1205. {
  1206. struct onenand_chip *this = mtd->priv;
  1207. int bram_maf_id, bram_dev_id, maf_id, dev_id;
  1208. int version_id;
  1209. int density;
  1210. /* Send the command for reading device ID from BootRAM */
  1211. this->write_word(ONENAND_CMD_READID, this->base + ONENAND_BOOTRAM);
  1212. /* Read manufacturer and device IDs from BootRAM */
  1213. bram_maf_id = this->read_word(this->base + ONENAND_BOOTRAM + 0x0);
  1214. bram_dev_id = this->read_word(this->base + ONENAND_BOOTRAM + 0x2);
  1215. /* Check manufacturer ID */
  1216. if (onenand_check_maf(bram_maf_id))
  1217. return -ENXIO;
  1218. /* Reset OneNAND to read default register values */
  1219. this->write_word(ONENAND_CMD_RESET, this->base + ONENAND_BOOTRAM);
  1220. /* Read manufacturer and device IDs from Register */
  1221. maf_id = this->read_word(this->base + ONENAND_REG_MANUFACTURER_ID);
  1222. dev_id = this->read_word(this->base + ONENAND_REG_DEVICE_ID);
  1223. /* Check OneNAND device */
  1224. if (maf_id != bram_maf_id || dev_id != bram_dev_id)
  1225. return -ENXIO;
  1226. /* Flash device information */
  1227. onenand_print_device_info(dev_id);
  1228. this->device_id = dev_id;
  1229. density = dev_id >> ONENAND_DEVICE_DENSITY_SHIFT;
  1230. this->chipsize = (16 << density) << 20;
  1231. /* Set density mask. it is used for DDP */
  1232. this->density_mask = (1 << (density + 6));
  1233. /* OneNAND page size & block size */
  1234. /* The data buffer size is equal to page size */
  1235. mtd->oobblock = this->read_word(this->base + ONENAND_REG_DATA_BUFFER_SIZE);
  1236. mtd->oobsize = mtd->oobblock >> 5;
  1237. /* Pagers per block is always 64 in OneNAND */
  1238. mtd->erasesize = mtd->oobblock << 6;
  1239. this->erase_shift = ffs(mtd->erasesize) - 1;
  1240. this->page_shift = ffs(mtd->oobblock) - 1;
  1241. this->ppb_shift = (this->erase_shift - this->page_shift);
  1242. this->page_mask = (mtd->erasesize / mtd->oobblock) - 1;
  1243. /* REVIST: Multichip handling */
  1244. mtd->size = this->chipsize;
  1245. /* Version ID */
  1246. version_id = this->read_word(this->base + ONENAND_REG_VERSION_ID);
  1247. printk(KERN_DEBUG "OneNAND version = 0x%04x\n", version_id);
  1248. /* Lock scheme */
  1249. if (density <= ONENAND_DEVICE_DENSITY_512Mb &&
  1250. !(version_id >> ONENAND_VERSION_PROCESS_SHIFT)) {
  1251. printk(KERN_INFO "Lock scheme is Continues Lock\n");
  1252. this->options |= ONENAND_CONT_LOCK;
  1253. }
  1254. return 0;
  1255. }
  1256. /**
  1257. * onenand_suspend - [MTD Interface] Suspend the OneNAND flash
  1258. * @param mtd MTD device structure
  1259. */
  1260. static int onenand_suspend(struct mtd_info *mtd)
  1261. {
  1262. return onenand_get_device(mtd, FL_PM_SUSPENDED);
  1263. }
  1264. /**
  1265. * onenand_resume - [MTD Interface] Resume the OneNAND flash
  1266. * @param mtd MTD device structure
  1267. */
  1268. static void onenand_resume(struct mtd_info *mtd)
  1269. {
  1270. struct onenand_chip *this = mtd->priv;
  1271. if (this->state == FL_PM_SUSPENDED)
  1272. onenand_release_device(mtd);
  1273. else
  1274. printk(KERN_ERR "resume() called for the chip which is not"
  1275. "in suspended state\n");
  1276. }
  1277. /**
  1278. * onenand_scan - [OneNAND Interface] Scan for the OneNAND device
  1279. * @param mtd MTD device structure
  1280. * @param maxchips Number of chips to scan for
  1281. *
  1282. * This fills out all the not initialized function pointers
  1283. * with the defaults.
  1284. * The flash ID is read and the mtd/chip structures are
  1285. * filled with the appropriate values.
  1286. */
  1287. int onenand_scan(struct mtd_info *mtd, int maxchips)
  1288. {
  1289. struct onenand_chip *this = mtd->priv;
  1290. if (!this->read_word)
  1291. this->read_word = onenand_readw;
  1292. if (!this->write_word)
  1293. this->write_word = onenand_writew;
  1294. if (!this->command)
  1295. this->command = onenand_command;
  1296. if (!this->wait)
  1297. this->wait = onenand_wait;
  1298. if (!this->read_bufferram)
  1299. this->read_bufferram = onenand_read_bufferram;
  1300. if (!this->write_bufferram)
  1301. this->write_bufferram = onenand_write_bufferram;
  1302. if (!this->block_markbad)
  1303. this->block_markbad = onenand_default_block_markbad;
  1304. if (!this->scan_bbt)
  1305. this->scan_bbt = onenand_default_bbt;
  1306. if (onenand_probe(mtd))
  1307. return -ENXIO;
  1308. /* Set Sync. Burst Read after probing */
  1309. if (this->mmcontrol) {
  1310. printk(KERN_INFO "OneNAND Sync. Burst Read support\n");
  1311. this->read_bufferram = onenand_sync_read_bufferram;
  1312. }
  1313. /* Allocate buffers, if necessary */
  1314. if (!this->page_buf) {
  1315. size_t len;
  1316. len = mtd->oobblock + mtd->oobsize;
  1317. this->page_buf = kmalloc(len, GFP_KERNEL);
  1318. if (!this->page_buf) {
  1319. printk(KERN_ERR "onenand_scan(): Can't allocate page_buf\n");
  1320. return -ENOMEM;
  1321. }
  1322. this->options |= ONENAND_PAGEBUF_ALLOC;
  1323. }
  1324. this->state = FL_READY;
  1325. init_waitqueue_head(&this->wq);
  1326. spin_lock_init(&this->chip_lock);
  1327. switch (mtd->oobsize) {
  1328. case 64:
  1329. this->autooob = &onenand_oob_64;
  1330. break;
  1331. case 32:
  1332. this->autooob = &onenand_oob_32;
  1333. break;
  1334. default:
  1335. printk(KERN_WARNING "No OOB scheme defined for oobsize %d\n",
  1336. mtd->oobsize);
  1337. /* To prevent kernel oops */
  1338. this->autooob = &onenand_oob_32;
  1339. break;
  1340. }
  1341. memcpy(&mtd->oobinfo, this->autooob, sizeof(mtd->oobinfo));
  1342. /* Fill in remaining MTD driver data */
  1343. mtd->type = MTD_NANDFLASH;
  1344. mtd->flags = MTD_CAP_NANDFLASH | MTD_ECC;
  1345. mtd->ecctype = MTD_ECC_SW;
  1346. mtd->erase = onenand_erase;
  1347. mtd->point = NULL;
  1348. mtd->unpoint = NULL;
  1349. mtd->read = onenand_read;
  1350. mtd->write = onenand_write;
  1351. mtd->read_ecc = onenand_read_ecc;
  1352. mtd->write_ecc = onenand_write_ecc;
  1353. mtd->read_oob = onenand_read_oob;
  1354. mtd->write_oob = onenand_write_oob;
  1355. mtd->readv = NULL;
  1356. mtd->readv_ecc = NULL;
  1357. mtd->writev = onenand_writev;
  1358. mtd->writev_ecc = onenand_writev_ecc;
  1359. mtd->sync = onenand_sync;
  1360. mtd->lock = NULL;
  1361. mtd->unlock = onenand_unlock;
  1362. mtd->suspend = onenand_suspend;
  1363. mtd->resume = onenand_resume;
  1364. mtd->block_isbad = onenand_block_isbad;
  1365. mtd->block_markbad = onenand_block_markbad;
  1366. mtd->owner = THIS_MODULE;
  1367. /* Unlock whole block */
  1368. mtd->unlock(mtd, 0x0, this->chipsize);
  1369. return this->scan_bbt(mtd);
  1370. }
  1371. /**
  1372. * onenand_release - [OneNAND Interface] Free resources held by the OneNAND device
  1373. * @param mtd MTD device structure
  1374. */
  1375. void onenand_release(struct mtd_info *mtd)
  1376. {
  1377. struct onenand_chip *this = mtd->priv;
  1378. #ifdef CONFIG_MTD_PARTITIONS
  1379. /* Deregister partitions */
  1380. del_mtd_partitions (mtd);
  1381. #endif
  1382. /* Deregister the device */
  1383. del_mtd_device (mtd);
  1384. /* Free bad block table memory, if allocated */
  1385. if (this->bbm)
  1386. kfree(this->bbm);
  1387. /* Buffer allocated by onenand_scan */
  1388. if (this->options & ONENAND_PAGEBUF_ALLOC)
  1389. kfree(this->page_buf);
  1390. }
  1391. EXPORT_SYMBOL_GPL(onenand_scan);
  1392. EXPORT_SYMBOL_GPL(onenand_release);
  1393. MODULE_LICENSE("GPL");
  1394. MODULE_AUTHOR("Kyungmin Park <kyungmin.park@samsung.com>");
  1395. MODULE_DESCRIPTION("Generic OneNAND flash driver code");