onenand_base.c 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864
  1. /*
  2. * linux/drivers/mtd/onenand/onenand_base.c
  3. *
  4. * Copyright (C) 2005-2007 Samsung Electronics
  5. * Kyungmin Park <kyungmin.park@samsung.com>
  6. *
  7. * Credits:
  8. * Adrian Hunter <ext-adrian.hunter@nokia.com>:
  9. * auto-placement support, read-while load support, various fixes
  10. * Copyright (C) Nokia Corporation, 2007
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License version 2 as
  14. * published by the Free Software Foundation.
  15. */
  16. #include <common.h>
  17. #include <linux/mtd/compat.h>
  18. #include <linux/mtd/mtd.h>
  19. #include <linux/mtd/onenand.h>
  20. #include <asm/io.h>
  21. #include <asm/errno.h>
  22. #include <malloc.h>
  23. /* It should access 16-bit instead of 8-bit */
  24. static inline void *memcpy_16(void *dst, const void *src, unsigned int len)
  25. {
  26. void *ret = dst;
  27. short *d = dst;
  28. const short *s = src;
  29. len >>= 1;
  30. while (len-- > 0)
  31. *d++ = *s++;
  32. return ret;
  33. }
  34. static const unsigned char ffchars[] = {
  35. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  36. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 16 */
  37. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  38. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 32 */
  39. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  40. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 48 */
  41. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  42. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 64 */
  43. };
  44. /**
  45. * onenand_readw - [OneNAND Interface] Read OneNAND register
  46. * @param addr address to read
  47. *
  48. * Read OneNAND register
  49. */
  50. static unsigned short onenand_readw(void __iomem * addr)
  51. {
  52. return readw(addr);
  53. }
  54. /**
  55. * onenand_writew - [OneNAND Interface] Write OneNAND register with value
  56. * @param value value to write
  57. * @param addr address to write
  58. *
  59. * Write OneNAND register with value
  60. */
  61. static void onenand_writew(unsigned short value, void __iomem * addr)
  62. {
  63. writew(value, addr);
  64. }
  65. /**
  66. * onenand_block_address - [DEFAULT] Get block address
  67. * @param device the device id
  68. * @param block the block
  69. * @return translated block address if DDP, otherwise same
  70. *
  71. * Setup Start Address 1 Register (F100h)
  72. */
  73. static int onenand_block_address(int device, int block)
  74. {
  75. if (device & ONENAND_DEVICE_IS_DDP) {
  76. /* Device Flash Core select, NAND Flash Block Address */
  77. int dfs = 0, density, mask;
  78. density = device >> ONENAND_DEVICE_DENSITY_SHIFT;
  79. mask = (1 << (density + 6));
  80. if (block & mask)
  81. dfs = 1;
  82. return (dfs << ONENAND_DDP_SHIFT) | (block & (mask - 1));
  83. }
  84. return block;
  85. }
  86. /**
  87. * onenand_bufferram_address - [DEFAULT] Get bufferram address
  88. * @param device the device id
  89. * @param block the block
  90. * @return set DBS value if DDP, otherwise 0
  91. *
  92. * Setup Start Address 2 Register (F101h) for DDP
  93. */
  94. static int onenand_bufferram_address(int device, int block)
  95. {
  96. if (device & ONENAND_DEVICE_IS_DDP) {
  97. /* Device BufferRAM Select */
  98. int dbs = 0, density, mask;
  99. density = device >> ONENAND_DEVICE_DENSITY_SHIFT;
  100. mask = (1 << (density + 6));
  101. if (block & mask)
  102. dbs = 1;
  103. return (dbs << ONENAND_DDP_SHIFT);
  104. }
  105. return 0;
  106. }
  107. /**
  108. * onenand_page_address - [DEFAULT] Get page address
  109. * @param page the page address
  110. * @param sector the sector address
  111. * @return combined page and sector address
  112. *
  113. * Setup Start Address 8 Register (F107h)
  114. */
  115. static int onenand_page_address(int page, int sector)
  116. {
  117. /* Flash Page Address, Flash Sector Address */
  118. int fpa, fsa;
  119. fpa = page & ONENAND_FPA_MASK;
  120. fsa = sector & ONENAND_FSA_MASK;
  121. return ((fpa << ONENAND_FPA_SHIFT) | fsa);
  122. }
  123. /**
  124. * onenand_buffer_address - [DEFAULT] Get buffer address
  125. * @param dataram1 DataRAM index
  126. * @param sectors the sector address
  127. * @param count the number of sectors
  128. * @return the start buffer value
  129. *
  130. * Setup Start Buffer Register (F200h)
  131. */
  132. static int onenand_buffer_address(int dataram1, int sectors, int count)
  133. {
  134. int bsa, bsc;
  135. /* BufferRAM Sector Address */
  136. bsa = sectors & ONENAND_BSA_MASK;
  137. if (dataram1)
  138. bsa |= ONENAND_BSA_DATARAM1; /* DataRAM1 */
  139. else
  140. bsa |= ONENAND_BSA_DATARAM0; /* DataRAM0 */
  141. /* BufferRAM Sector Count */
  142. bsc = count & ONENAND_BSC_MASK;
  143. return ((bsa << ONENAND_BSA_SHIFT) | bsc);
  144. }
  145. /**
  146. * onenand_command - [DEFAULT] Send command to OneNAND device
  147. * @param mtd MTD device structure
  148. * @param cmd the command to be sent
  149. * @param addr offset to read from or write to
  150. * @param len number of bytes to read or write
  151. *
  152. * Send command to OneNAND device. This function is used for middle/large page
  153. * devices (1KB/2KB Bytes per page)
  154. */
  155. static int onenand_command(struct mtd_info *mtd, int cmd, loff_t addr,
  156. size_t len)
  157. {
  158. struct onenand_chip *this = mtd->priv;
  159. int value, readcmd = 0;
  160. int block, page;
  161. /* Now we use page size operation */
  162. int sectors = 4, count = 4;
  163. /* Address translation */
  164. switch (cmd) {
  165. case ONENAND_CMD_UNLOCK:
  166. case ONENAND_CMD_LOCK:
  167. case ONENAND_CMD_LOCK_TIGHT:
  168. block = -1;
  169. page = -1;
  170. break;
  171. case ONENAND_CMD_ERASE:
  172. case ONENAND_CMD_BUFFERRAM:
  173. block = (int)(addr >> this->erase_shift);
  174. page = -1;
  175. break;
  176. default:
  177. block = (int)(addr >> this->erase_shift);
  178. page = (int)(addr >> this->page_shift);
  179. page &= this->page_mask;
  180. break;
  181. }
  182. /* NOTE: The setting order of the registers is very important! */
  183. if (cmd == ONENAND_CMD_BUFFERRAM) {
  184. /* Select DataRAM for DDP */
  185. value = onenand_bufferram_address(this->device_id, block);
  186. this->write_word(value,
  187. this->base + ONENAND_REG_START_ADDRESS2);
  188. /* Switch to the next data buffer */
  189. ONENAND_SET_NEXT_BUFFERRAM(this);
  190. return 0;
  191. }
  192. if (block != -1) {
  193. /* Write 'DFS, FBA' of Flash */
  194. value = onenand_block_address(this->device_id, block);
  195. this->write_word(value,
  196. this->base + ONENAND_REG_START_ADDRESS1);
  197. }
  198. if (page != -1) {
  199. int dataram;
  200. switch (cmd) {
  201. case ONENAND_CMD_READ:
  202. case ONENAND_CMD_READOOB:
  203. dataram = ONENAND_SET_NEXT_BUFFERRAM(this);
  204. readcmd = 1;
  205. break;
  206. default:
  207. dataram = ONENAND_CURRENT_BUFFERRAM(this);
  208. break;
  209. }
  210. /* Write 'FPA, FSA' of Flash */
  211. value = onenand_page_address(page, sectors);
  212. this->write_word(value,
  213. this->base + ONENAND_REG_START_ADDRESS8);
  214. /* Write 'BSA, BSC' of DataRAM */
  215. value = onenand_buffer_address(dataram, sectors, count);
  216. this->write_word(value, this->base + ONENAND_REG_START_BUFFER);
  217. if (readcmd) {
  218. /* Select DataRAM for DDP */
  219. value =
  220. onenand_bufferram_address(this->device_id, block);
  221. this->write_word(value,
  222. this->base +
  223. ONENAND_REG_START_ADDRESS2);
  224. }
  225. }
  226. /* Interrupt clear */
  227. this->write_word(ONENAND_INT_CLEAR, this->base + ONENAND_REG_INTERRUPT);
  228. /* Write command */
  229. this->write_word(cmd, this->base + ONENAND_REG_COMMAND);
  230. return 0;
  231. }
  232. /**
  233. * onenand_wait - [DEFAULT] wait until the command is done
  234. * @param mtd MTD device structure
  235. * @param state state to select the max. timeout value
  236. *
  237. * Wait for command done. This applies to all OneNAND command
  238. * Read can take up to 30us, erase up to 2ms and program up to 350us
  239. * according to general OneNAND specs
  240. */
  241. static int onenand_wait(struct mtd_info *mtd, int state)
  242. {
  243. struct onenand_chip *this = mtd->priv;
  244. unsigned int flags = ONENAND_INT_MASTER;
  245. unsigned int interrupt = 0;
  246. unsigned int ctrl, ecc;
  247. while (1) {
  248. interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
  249. if (interrupt & flags)
  250. break;
  251. }
  252. ctrl = this->read_word(this->base + ONENAND_REG_CTRL_STATUS);
  253. if (ctrl & ONENAND_CTRL_ERROR) {
  254. MTDDEBUG (MTD_DEBUG_LEVEL0,
  255. "onenand_wait: controller error = 0x%04x\n", ctrl);
  256. return -EAGAIN;
  257. }
  258. if (ctrl & ONENAND_CTRL_LOCK) {
  259. MTDDEBUG (MTD_DEBUG_LEVEL0,
  260. "onenand_wait: it's locked error = 0x%04x\n", ctrl);
  261. return -EIO;
  262. }
  263. if (interrupt & ONENAND_INT_READ) {
  264. ecc = this->read_word(this->base + ONENAND_REG_ECC_STATUS);
  265. if (ecc & ONENAND_ECC_2BIT_ALL) {
  266. MTDDEBUG (MTD_DEBUG_LEVEL0,
  267. "onenand_wait: ECC error = 0x%04x\n", ecc);
  268. return -EBADMSG;
  269. }
  270. }
  271. return 0;
  272. }
  273. /**
  274. * onenand_bufferram_offset - [DEFAULT] BufferRAM offset
  275. * @param mtd MTD data structure
  276. * @param area BufferRAM area
  277. * @return offset given area
  278. *
  279. * Return BufferRAM offset given area
  280. */
  281. static inline int onenand_bufferram_offset(struct mtd_info *mtd, int area)
  282. {
  283. struct onenand_chip *this = mtd->priv;
  284. if (ONENAND_CURRENT_BUFFERRAM(this)) {
  285. if (area == ONENAND_DATARAM)
  286. return mtd->writesize;
  287. if (area == ONENAND_SPARERAM)
  288. return mtd->oobsize;
  289. }
  290. return 0;
  291. }
  292. /**
  293. * onenand_read_bufferram - [OneNAND Interface] Read the bufferram area
  294. * @param mtd MTD data structure
  295. * @param area BufferRAM area
  296. * @param buffer the databuffer to put/get data
  297. * @param offset offset to read from or write to
  298. * @param count number of bytes to read/write
  299. *
  300. * Read the BufferRAM area
  301. */
  302. static int onenand_read_bufferram(struct mtd_info *mtd, int area,
  303. unsigned char *buffer, int offset,
  304. size_t count)
  305. {
  306. struct onenand_chip *this = mtd->priv;
  307. void __iomem *bufferram;
  308. bufferram = this->base + area;
  309. bufferram += onenand_bufferram_offset(mtd, area);
  310. memcpy_16(buffer, bufferram + offset, count);
  311. return 0;
  312. }
  313. /**
  314. * onenand_sync_read_bufferram - [OneNAND Interface] Read the bufferram area with Sync. Burst mode
  315. * @param mtd MTD data structure
  316. * @param area BufferRAM area
  317. * @param buffer the databuffer to put/get data
  318. * @param offset offset to read from or write to
  319. * @param count number of bytes to read/write
  320. *
  321. * Read the BufferRAM area with Sync. Burst Mode
  322. */
  323. static int onenand_sync_read_bufferram(struct mtd_info *mtd, int area,
  324. unsigned char *buffer, int offset,
  325. size_t count)
  326. {
  327. struct onenand_chip *this = mtd->priv;
  328. void __iomem *bufferram;
  329. bufferram = this->base + area;
  330. bufferram += onenand_bufferram_offset(mtd, area);
  331. this->mmcontrol(mtd, ONENAND_SYS_CFG1_SYNC_READ);
  332. memcpy_16(buffer, bufferram + offset, count);
  333. this->mmcontrol(mtd, 0);
  334. return 0;
  335. }
  336. /**
  337. * onenand_write_bufferram - [OneNAND Interface] Write the bufferram area
  338. * @param mtd MTD data structure
  339. * @param area BufferRAM area
  340. * @param buffer the databuffer to put/get data
  341. * @param offset offset to read from or write to
  342. * @param count number of bytes to read/write
  343. *
  344. * Write the BufferRAM area
  345. */
  346. static int onenand_write_bufferram(struct mtd_info *mtd, int area,
  347. const unsigned char *buffer, int offset,
  348. size_t count)
  349. {
  350. struct onenand_chip *this = mtd->priv;
  351. void __iomem *bufferram;
  352. bufferram = this->base + area;
  353. bufferram += onenand_bufferram_offset(mtd, area);
  354. memcpy_16(bufferram + offset, buffer, count);
  355. return 0;
  356. }
  357. /**
  358. * onenand_check_bufferram - [GENERIC] Check BufferRAM information
  359. * @param mtd MTD data structure
  360. * @param addr address to check
  361. * @return 1 if there are valid data, otherwise 0
  362. *
  363. * Check bufferram if there is data we required
  364. */
  365. static int onenand_check_bufferram(struct mtd_info *mtd, loff_t addr)
  366. {
  367. struct onenand_chip *this = mtd->priv;
  368. int block, page;
  369. int i;
  370. block = (int)(addr >> this->erase_shift);
  371. page = (int)(addr >> this->page_shift);
  372. page &= this->page_mask;
  373. i = ONENAND_CURRENT_BUFFERRAM(this);
  374. /* Is there valid data? */
  375. if (this->bufferram[i].block == block &&
  376. this->bufferram[i].page == page && this->bufferram[i].valid)
  377. return 1;
  378. return 0;
  379. }
  380. /**
  381. * onenand_update_bufferram - [GENERIC] Update BufferRAM information
  382. * @param mtd MTD data structure
  383. * @param addr address to update
  384. * @param valid valid flag
  385. *
  386. * Update BufferRAM information
  387. */
  388. static int onenand_update_bufferram(struct mtd_info *mtd, loff_t addr,
  389. int valid)
  390. {
  391. struct onenand_chip *this = mtd->priv;
  392. int block, page;
  393. int i;
  394. block = (int)(addr >> this->erase_shift);
  395. page = (int)(addr >> this->page_shift);
  396. page &= this->page_mask;
  397. /* Invalidate BufferRAM */
  398. for (i = 0; i < MAX_BUFFERRAM; i++) {
  399. if (this->bufferram[i].block == block &&
  400. this->bufferram[i].page == page)
  401. this->bufferram[i].valid = 0;
  402. }
  403. /* Update BufferRAM */
  404. i = ONENAND_CURRENT_BUFFERRAM(this);
  405. this->bufferram[i].block = block;
  406. this->bufferram[i].page = page;
  407. this->bufferram[i].valid = valid;
  408. return 0;
  409. }
  410. /**
  411. * onenand_invalidate_bufferram - [GENERIC] Invalidate BufferRAM information
  412. * @param mtd MTD data structure
  413. * @param addr start address to invalidate
  414. * @param len length to invalidate
  415. *
  416. * Invalidate BufferRAM information
  417. */
  418. static void onenand_invalidate_bufferram(struct mtd_info *mtd, loff_t addr,
  419. unsigned int len)
  420. {
  421. struct onenand_chip *this = mtd->priv;
  422. int i;
  423. loff_t end_addr = addr + len;
  424. /* Invalidate BufferRAM */
  425. for (i = 0; i < MAX_BUFFERRAM; i++) {
  426. loff_t buf_addr = this->bufferram[i].block << this->erase_shift;
  427. if (buf_addr >= addr && buf_addr < end_addr)
  428. this->bufferram[i].valid = 0;
  429. }
  430. }
  431. /**
  432. * onenand_get_device - [GENERIC] Get chip for selected access
  433. * @param mtd MTD device structure
  434. * @param new_state the state which is requested
  435. *
  436. * Get the device and lock it for exclusive access
  437. */
  438. static void onenand_get_device(struct mtd_info *mtd, int new_state)
  439. {
  440. /* Do nothing */
  441. }
  442. /**
  443. * onenand_release_device - [GENERIC] release chip
  444. * @param mtd MTD device structure
  445. *
  446. * Deselect, release chip lock and wake up anyone waiting on the device
  447. */
  448. static void onenand_release_device(struct mtd_info *mtd)
  449. {
  450. /* Do nothing */
  451. }
  452. /**
  453. * onenand_transfer_auto_oob - [Internal] oob auto-placement transfer
  454. * @param mtd MTD device structure
  455. * @param buf destination address
  456. * @param column oob offset to read from
  457. * @param thislen oob length to read
  458. */
  459. static int onenand_transfer_auto_oob(struct mtd_info *mtd, uint8_t *buf,
  460. int column, int thislen)
  461. {
  462. struct onenand_chip *this = mtd->priv;
  463. struct nand_oobfree *free;
  464. int readcol = column;
  465. int readend = column + thislen;
  466. int lastgap = 0;
  467. unsigned int i;
  468. uint8_t *oob_buf = this->oob_buf;
  469. free = this->ecclayout->oobfree;
  470. for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES && free->length; i++, free++) {
  471. if (readcol >= lastgap)
  472. readcol += free->offset - lastgap;
  473. if (readend >= lastgap)
  474. readend += free->offset - lastgap;
  475. lastgap = free->offset + free->length;
  476. }
  477. this->read_bufferram(mtd, ONENAND_SPARERAM, oob_buf, 0, mtd->oobsize);
  478. free = this->ecclayout->oobfree;
  479. for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES && free->length; i++, free++) {
  480. int free_end = free->offset + free->length;
  481. if (free->offset < readend && free_end > readcol) {
  482. int st = max_t(int,free->offset,readcol);
  483. int ed = min_t(int,free_end,readend);
  484. int n = ed - st;
  485. memcpy(buf, oob_buf + st, n);
  486. buf += n;
  487. } else if (column == 0)
  488. break;
  489. }
  490. return 0;
  491. }
  492. /**
  493. * onenand_read_ops_nolock - [OneNAND Interface] OneNAND read main and/or out-of-band
  494. * @param mtd MTD device structure
  495. * @param from offset to read from
  496. * @param ops oob operation description structure
  497. *
  498. * OneNAND read main and/or out-of-band data
  499. */
  500. static int onenand_read_ops_nolock(struct mtd_info *mtd, loff_t from,
  501. struct mtd_oob_ops *ops)
  502. {
  503. struct onenand_chip *this = mtd->priv;
  504. struct mtd_ecc_stats stats;
  505. size_t len = ops->len;
  506. size_t ooblen = ops->ooblen;
  507. u_char *buf = ops->datbuf;
  508. u_char *oobbuf = ops->oobbuf;
  509. int read = 0, column, thislen;
  510. int oobread = 0, oobcolumn, thisooblen, oobsize;
  511. int ret = 0, boundary = 0;
  512. int writesize = this->writesize;
  513. MTDDEBUG(MTD_DEBUG_LEVEL3,
  514. "onenand_read_ops_nolock: from = 0x%08x, len = %i\n",
  515. (unsigned int) from, (int) len);
  516. if (ops->mode == MTD_OOB_AUTO)
  517. oobsize = this->ecclayout->oobavail;
  518. else
  519. oobsize = mtd->oobsize;
  520. oobcolumn = from & (mtd->oobsize - 1);
  521. /* Do not allow reads past end of device */
  522. if ((from + len) > mtd->size) {
  523. printk(KERN_ERR "onenand_read_ops_nolock: Attempt read beyond end of device\n");
  524. ops->retlen = 0;
  525. ops->oobretlen = 0;
  526. return -EINVAL;
  527. }
  528. stats = mtd->ecc_stats;
  529. /* Read-while-load method */
  530. /* Do first load to bufferRAM */
  531. if (read < len) {
  532. if (!onenand_check_bufferram(mtd, from)) {
  533. this->command(mtd, ONENAND_CMD_READ, from, writesize);
  534. ret = this->wait(mtd, FL_READING);
  535. onenand_update_bufferram(mtd, from, !ret);
  536. if (ret == -EBADMSG)
  537. ret = 0;
  538. }
  539. }
  540. thislen = min_t(int, writesize, len - read);
  541. column = from & (writesize - 1);
  542. if (column + thislen > writesize)
  543. thislen = writesize - column;
  544. while (!ret) {
  545. /* If there is more to load then start next load */
  546. from += thislen;
  547. if (read + thislen < len) {
  548. this->command(mtd, ONENAND_CMD_READ, from, writesize);
  549. /*
  550. * Chip boundary handling in DDP
  551. * Now we issued chip 1 read and pointed chip 1
  552. * bufferam so we have to point chip 0 bufferam.
  553. */
  554. if (ONENAND_IS_DDP(this) &&
  555. unlikely(from == (this->chipsize >> 1))) {
  556. this->write_word(ONENAND_DDP_CHIP0, this->base + ONENAND_REG_START_ADDRESS2);
  557. boundary = 1;
  558. } else
  559. boundary = 0;
  560. ONENAND_SET_PREV_BUFFERRAM(this);
  561. }
  562. /* While load is going, read from last bufferRAM */
  563. this->read_bufferram(mtd, ONENAND_DATARAM, buf, column, thislen);
  564. /* Read oob area if needed */
  565. if (oobbuf) {
  566. thisooblen = oobsize - oobcolumn;
  567. thisooblen = min_t(int, thisooblen, ooblen - oobread);
  568. if (ops->mode == MTD_OOB_AUTO)
  569. onenand_transfer_auto_oob(mtd, oobbuf, oobcolumn, thisooblen);
  570. else
  571. this->read_bufferram(mtd, ONENAND_SPARERAM, oobbuf, oobcolumn, thisooblen);
  572. oobread += thisooblen;
  573. oobbuf += thisooblen;
  574. oobcolumn = 0;
  575. }
  576. /* See if we are done */
  577. read += thislen;
  578. if (read == len)
  579. break;
  580. /* Set up for next read from bufferRAM */
  581. if (unlikely(boundary))
  582. this->write_word(ONENAND_DDP_CHIP1, this->base + ONENAND_REG_START_ADDRESS2);
  583. ONENAND_SET_NEXT_BUFFERRAM(this);
  584. buf += thislen;
  585. thislen = min_t(int, writesize, len - read);
  586. column = 0;
  587. /* Now wait for load */
  588. ret = this->wait(mtd, FL_READING);
  589. onenand_update_bufferram(mtd, from, !ret);
  590. if (ret == -EBADMSG)
  591. ret = 0;
  592. }
  593. /*
  594. * Return success, if no ECC failures, else -EBADMSG
  595. * fs driver will take care of that, because
  596. * retlen == desired len and result == -EBADMSG
  597. */
  598. ops->retlen = read;
  599. ops->oobretlen = oobread;
  600. if (ret)
  601. return ret;
  602. if (mtd->ecc_stats.failed - stats.failed)
  603. return -EBADMSG;
  604. return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
  605. }
  606. /**
  607. * onenand_read_oob_nolock - [MTD Interface] OneNAND read out-of-band
  608. * @param mtd MTD device structure
  609. * @param from offset to read from
  610. * @param ops oob operation description structure
  611. *
  612. * OneNAND read out-of-band data from the spare area
  613. */
  614. static int onenand_read_oob_nolock(struct mtd_info *mtd, loff_t from,
  615. struct mtd_oob_ops *ops)
  616. {
  617. struct onenand_chip *this = mtd->priv;
  618. struct mtd_ecc_stats stats;
  619. int read = 0, thislen, column, oobsize;
  620. size_t len = ops->ooblen;
  621. mtd_oob_mode_t mode = ops->mode;
  622. u_char *buf = ops->oobbuf;
  623. int ret = 0;
  624. from += ops->ooboffs;
  625. MTDDEBUG(MTD_DEBUG_LEVEL3,
  626. "onenand_read_oob_nolock: from = 0x%08x, len = %i\n",
  627. (unsigned int) from, (int) len);
  628. /* Initialize return length value */
  629. ops->oobretlen = 0;
  630. if (mode == MTD_OOB_AUTO)
  631. oobsize = this->ecclayout->oobavail;
  632. else
  633. oobsize = mtd->oobsize;
  634. column = from & (mtd->oobsize - 1);
  635. if (unlikely(column >= oobsize)) {
  636. printk(KERN_ERR "onenand_read_oob_nolock: Attempted to start read outside oob\n");
  637. return -EINVAL;
  638. }
  639. /* Do not allow reads past end of device */
  640. if (unlikely(from >= mtd->size ||
  641. column + len > ((mtd->size >> this->page_shift) -
  642. (from >> this->page_shift)) * oobsize)) {
  643. printk(KERN_ERR "onenand_read_oob_nolock: Attempted to read beyond end of device\n");
  644. return -EINVAL;
  645. }
  646. stats = mtd->ecc_stats;
  647. while (read < len) {
  648. thislen = oobsize - column;
  649. thislen = min_t(int, thislen, len);
  650. this->command(mtd, ONENAND_CMD_READOOB, from, mtd->oobsize);
  651. onenand_update_bufferram(mtd, from, 0);
  652. ret = this->wait(mtd, FL_READING);
  653. if (ret && ret != -EBADMSG) {
  654. printk(KERN_ERR "onenand_read_oob_nolock: read failed = 0x%x\n", ret);
  655. break;
  656. }
  657. if (mode == MTD_OOB_AUTO)
  658. onenand_transfer_auto_oob(mtd, buf, column, thislen);
  659. else
  660. this->read_bufferram(mtd, ONENAND_SPARERAM, buf, column, thislen);
  661. read += thislen;
  662. if (read == len)
  663. break;
  664. buf += thislen;
  665. /* Read more? */
  666. if (read < len) {
  667. /* Page size */
  668. from += mtd->writesize;
  669. column = 0;
  670. }
  671. }
  672. ops->oobretlen = read;
  673. if (ret)
  674. return ret;
  675. if (mtd->ecc_stats.failed - stats.failed)
  676. return -EBADMSG;
  677. return 0;
  678. }
  679. /**
  680. * onenand_read - [MTD Interface] MTD compability function for onenand_read_ecc
  681. * @param mtd MTD device structure
  682. * @param from offset to read from
  683. * @param len number of bytes to read
  684. * @param retlen pointer to variable to store the number of read bytes
  685. * @param buf the databuffer to put data
  686. *
  687. * This function simply calls onenand_read_ecc with oob buffer and oobsel = NULL
  688. */
  689. int onenand_read(struct mtd_info *mtd, loff_t from, size_t len,
  690. size_t * retlen, u_char * buf)
  691. {
  692. struct mtd_oob_ops ops = {
  693. .len = len,
  694. .ooblen = 0,
  695. .datbuf = buf,
  696. .oobbuf = NULL,
  697. };
  698. int ret;
  699. onenand_get_device(mtd, FL_READING);
  700. ret = onenand_read_ops_nolock(mtd, from, &ops);
  701. onenand_release_device(mtd);
  702. *retlen = ops.retlen;
  703. return ret;
  704. }
  705. /**
  706. * onenand_read_oob - [MTD Interface] OneNAND read out-of-band
  707. * @param mtd MTD device structure
  708. * @param from offset to read from
  709. * @param ops oob operations description structure
  710. *
  711. * OneNAND main and/or out-of-band
  712. */
  713. int onenand_read_oob(struct mtd_info *mtd, loff_t from,
  714. struct mtd_oob_ops *ops)
  715. {
  716. int ret;
  717. switch (ops->mode) {
  718. case MTD_OOB_PLACE:
  719. case MTD_OOB_AUTO:
  720. break;
  721. case MTD_OOB_RAW:
  722. /* Not implemented yet */
  723. default:
  724. return -EINVAL;
  725. }
  726. onenand_get_device(mtd, FL_READING);
  727. if (ops->datbuf)
  728. ret = onenand_read_ops_nolock(mtd, from, ops);
  729. else
  730. ret = onenand_read_oob_nolock(mtd, from, ops);
  731. onenand_release_device(mtd);
  732. return ret;
  733. }
  734. /**
  735. * onenand_bbt_wait - [DEFAULT] wait until the command is done
  736. * @param mtd MTD device structure
  737. * @param state state to select the max. timeout value
  738. *
  739. * Wait for command done.
  740. */
  741. static int onenand_bbt_wait(struct mtd_info *mtd, int state)
  742. {
  743. struct onenand_chip *this = mtd->priv;
  744. unsigned int flags = ONENAND_INT_MASTER;
  745. unsigned int interrupt;
  746. unsigned int ctrl;
  747. while (1) {
  748. interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
  749. if (interrupt & flags)
  750. break;
  751. }
  752. /* To get correct interrupt status in timeout case */
  753. interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
  754. ctrl = this->read_word(this->base + ONENAND_REG_CTRL_STATUS);
  755. /* Initial bad block case: 0x2400 or 0x0400 */
  756. if (ctrl & ONENAND_CTRL_ERROR) {
  757. printk(KERN_DEBUG "onenand_bbt_wait: controller error = 0x%04x\n", ctrl);
  758. return ONENAND_BBT_READ_ERROR;
  759. }
  760. if (interrupt & ONENAND_INT_READ) {
  761. int ecc = this->read_word(this->base + ONENAND_REG_ECC_STATUS);
  762. if (ecc & ONENAND_ECC_2BIT_ALL)
  763. return ONENAND_BBT_READ_ERROR;
  764. } else {
  765. printk(KERN_ERR "onenand_bbt_wait: read timeout!"
  766. "ctrl=0x%04x intr=0x%04x\n", ctrl, interrupt);
  767. return ONENAND_BBT_READ_FATAL_ERROR;
  768. }
  769. return 0;
  770. }
  771. /**
  772. * onenand_bbt_read_oob - [MTD Interface] OneNAND read out-of-band for bbt scan
  773. * @param mtd MTD device structure
  774. * @param from offset to read from
  775. * @param ops oob operation description structure
  776. *
  777. * OneNAND read out-of-band data from the spare area for bbt scan
  778. */
  779. int onenand_bbt_read_oob(struct mtd_info *mtd, loff_t from,
  780. struct mtd_oob_ops *ops)
  781. {
  782. struct onenand_chip *this = mtd->priv;
  783. int read = 0, thislen, column;
  784. int ret = 0;
  785. size_t len = ops->ooblen;
  786. u_char *buf = ops->oobbuf;
  787. MTDDEBUG(MTD_DEBUG_LEVEL3,
  788. "onenand_bbt_read_oob: from = 0x%08x, len = %zi\n",
  789. (unsigned int) from, len);
  790. /* Initialize return value */
  791. ops->oobretlen = 0;
  792. /* Do not allow reads past end of device */
  793. if (unlikely((from + len) > mtd->size)) {
  794. printk(KERN_ERR "onenand_bbt_read_oob: Attempt read beyond end of device\n");
  795. return ONENAND_BBT_READ_FATAL_ERROR;
  796. }
  797. /* Grab the lock and see if the device is available */
  798. onenand_get_device(mtd, FL_READING);
  799. column = from & (mtd->oobsize - 1);
  800. while (read < len) {
  801. thislen = mtd->oobsize - column;
  802. thislen = min_t(int, thislen, len);
  803. this->command(mtd, ONENAND_CMD_READOOB, from, mtd->oobsize);
  804. onenand_update_bufferram(mtd, from, 0);
  805. ret = onenand_bbt_wait(mtd, FL_READING);
  806. if (ret)
  807. break;
  808. this->read_bufferram(mtd, ONENAND_SPARERAM, buf, column, thislen);
  809. read += thislen;
  810. if (read == len)
  811. break;
  812. buf += thislen;
  813. /* Read more? */
  814. if (read < len) {
  815. /* Update Page size */
  816. from += this->writesize;
  817. column = 0;
  818. }
  819. }
  820. /* Deselect and wake up anyone waiting on the device */
  821. onenand_release_device(mtd);
  822. ops->oobretlen = read;
  823. return ret;
  824. }
  825. #ifdef CONFIG_MTD_ONENAND_VERIFY_WRITE
  826. /**
  827. * onenand_verify_oob - [GENERIC] verify the oob contents after a write
  828. * @param mtd MTD device structure
  829. * @param buf the databuffer to verify
  830. * @param to offset to read from
  831. */
  832. static int onenand_verify_oob(struct mtd_info *mtd, const u_char *buf, loff_t to)
  833. {
  834. struct onenand_chip *this = mtd->priv;
  835. u_char *oob_buf = this->oob_buf;
  836. int status, i;
  837. this->command(mtd, ONENAND_CMD_READOOB, to, mtd->oobsize);
  838. onenand_update_bufferram(mtd, to, 0);
  839. status = this->wait(mtd, FL_READING);
  840. if (status)
  841. return status;
  842. this->read_bufferram(mtd, ONENAND_SPARERAM, oob_buf, 0, mtd->oobsize);
  843. for (i = 0; i < mtd->oobsize; i++)
  844. if (buf[i] != 0xFF && buf[i] != oob_buf[i])
  845. return -EBADMSG;
  846. return 0;
  847. }
  848. /**
  849. * onenand_verify - [GENERIC] verify the chip contents after a write
  850. * @param mtd MTD device structure
  851. * @param buf the databuffer to verify
  852. * @param addr offset to read from
  853. * @param len number of bytes to read and compare
  854. */
  855. static int onenand_verify(struct mtd_info *mtd, const u_char *buf, loff_t addr, size_t len)
  856. {
  857. struct onenand_chip *this = mtd->priv;
  858. void __iomem *dataram;
  859. int ret = 0;
  860. int thislen, column;
  861. while (len != 0) {
  862. thislen = min_t(int, this->writesize, len);
  863. column = addr & (this->writesize - 1);
  864. if (column + thislen > this->writesize)
  865. thislen = this->writesize - column;
  866. this->command(mtd, ONENAND_CMD_READ, addr, this->writesize);
  867. onenand_update_bufferram(mtd, addr, 0);
  868. ret = this->wait(mtd, FL_READING);
  869. if (ret)
  870. return ret;
  871. onenand_update_bufferram(mtd, addr, 1);
  872. dataram = this->base + ONENAND_DATARAM;
  873. dataram += onenand_bufferram_offset(mtd, ONENAND_DATARAM);
  874. if (memcmp(buf, dataram + column, thislen))
  875. return -EBADMSG;
  876. len -= thislen;
  877. buf += thislen;
  878. addr += thislen;
  879. }
  880. return 0;
  881. }
  882. #else
  883. #define onenand_verify(...) (0)
  884. #define onenand_verify_oob(...) (0)
  885. #endif
  886. #define NOTALIGNED(x) ((x & (mtd->writesize - 1)) != 0)
  887. /**
  888. * onenand_fill_auto_oob - [Internal] oob auto-placement transfer
  889. * @param mtd MTD device structure
  890. * @param oob_buf oob buffer
  891. * @param buf source address
  892. * @param column oob offset to write to
  893. * @param thislen oob length to write
  894. */
  895. static int onenand_fill_auto_oob(struct mtd_info *mtd, u_char *oob_buf,
  896. const u_char *buf, int column, int thislen)
  897. {
  898. struct onenand_chip *this = mtd->priv;
  899. struct nand_oobfree *free;
  900. int writecol = column;
  901. int writeend = column + thislen;
  902. int lastgap = 0;
  903. unsigned int i;
  904. free = this->ecclayout->oobfree;
  905. for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES && free->length; i++, free++) {
  906. if (writecol >= lastgap)
  907. writecol += free->offset - lastgap;
  908. if (writeend >= lastgap)
  909. writeend += free->offset - lastgap;
  910. lastgap = free->offset + free->length;
  911. }
  912. free = this->ecclayout->oobfree;
  913. for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES && free->length; i++, free++) {
  914. int free_end = free->offset + free->length;
  915. if (free->offset < writeend && free_end > writecol) {
  916. int st = max_t(int,free->offset,writecol);
  917. int ed = min_t(int,free_end,writeend);
  918. int n = ed - st;
  919. memcpy(oob_buf + st, buf, n);
  920. buf += n;
  921. } else if (column == 0)
  922. break;
  923. }
  924. return 0;
  925. }
  926. /**
  927. * onenand_write_ops_nolock - [OneNAND Interface] write main and/or out-of-band
  928. * @param mtd MTD device structure
  929. * @param to offset to write to
  930. * @param ops oob operation description structure
  931. *
  932. * Write main and/or oob with ECC
  933. */
  934. static int onenand_write_ops_nolock(struct mtd_info *mtd, loff_t to,
  935. struct mtd_oob_ops *ops)
  936. {
  937. struct onenand_chip *this = mtd->priv;
  938. int written = 0, column, thislen, subpage;
  939. int oobwritten = 0, oobcolumn, thisooblen, oobsize;
  940. size_t len = ops->len;
  941. size_t ooblen = ops->ooblen;
  942. const u_char *buf = ops->datbuf;
  943. const u_char *oob = ops->oobbuf;
  944. u_char *oobbuf;
  945. int ret = 0;
  946. MTDDEBUG(MTD_DEBUG_LEVEL3,
  947. "onenand_write_ops_nolock: to = 0x%08x, len = %i\n",
  948. (unsigned int) to, (int) len);
  949. /* Initialize retlen, in case of early exit */
  950. ops->retlen = 0;
  951. ops->oobretlen = 0;
  952. /* Do not allow writes past end of device */
  953. if (unlikely((to + len) > mtd->size)) {
  954. printk(KERN_ERR "onenand_write_ops_nolock: Attempt write to past end of device\n");
  955. return -EINVAL;
  956. }
  957. /* Reject writes, which are not page aligned */
  958. if (unlikely(NOTALIGNED(to) || NOTALIGNED(len))) {
  959. printk(KERN_ERR "onenand_write_ops_nolock: Attempt to write not page aligned data\n");
  960. return -EINVAL;
  961. }
  962. if (ops->mode == MTD_OOB_AUTO)
  963. oobsize = this->ecclayout->oobavail;
  964. else
  965. oobsize = mtd->oobsize;
  966. oobcolumn = to & (mtd->oobsize - 1);
  967. column = to & (mtd->writesize - 1);
  968. /* Loop until all data write */
  969. while (written < len) {
  970. u_char *wbuf = (u_char *) buf;
  971. thislen = min_t(int, mtd->writesize - column, len - written);
  972. thisooblen = min_t(int, oobsize - oobcolumn, ooblen - oobwritten);
  973. this->command(mtd, ONENAND_CMD_BUFFERRAM, to, thislen);
  974. /* Partial page write */
  975. subpage = thislen < mtd->writesize;
  976. if (subpage) {
  977. memset(this->page_buf, 0xff, mtd->writesize);
  978. memcpy(this->page_buf + column, buf, thislen);
  979. wbuf = this->page_buf;
  980. }
  981. this->write_bufferram(mtd, ONENAND_DATARAM, wbuf, 0, mtd->writesize);
  982. if (oob) {
  983. oobbuf = this->oob_buf;
  984. /* We send data to spare ram with oobsize
  985. * * to prevent byte access */
  986. memset(oobbuf, 0xff, mtd->oobsize);
  987. if (ops->mode == MTD_OOB_AUTO)
  988. onenand_fill_auto_oob(mtd, oobbuf, oob, oobcolumn, thisooblen);
  989. else
  990. memcpy(oobbuf + oobcolumn, oob, thisooblen);
  991. oobwritten += thisooblen;
  992. oob += thisooblen;
  993. oobcolumn = 0;
  994. } else
  995. oobbuf = (u_char *) ffchars;
  996. this->write_bufferram(mtd, ONENAND_SPARERAM, oobbuf, 0, mtd->oobsize);
  997. this->command(mtd, ONENAND_CMD_PROG, to, mtd->writesize);
  998. ret = this->wait(mtd, FL_WRITING);
  999. /* In partial page write we don't update bufferram */
  1000. onenand_update_bufferram(mtd, to, !ret && !subpage);
  1001. if (ONENAND_IS_2PLANE(this)) {
  1002. ONENAND_SET_BUFFERRAM1(this);
  1003. onenand_update_bufferram(mtd, to + this->writesize, !ret && !subpage);
  1004. }
  1005. if (ret) {
  1006. printk(KERN_ERR "onenand_write_ops_nolock: write filaed %d\n", ret);
  1007. break;
  1008. }
  1009. /* Only check verify write turn on */
  1010. ret = onenand_verify(mtd, buf, to, thislen);
  1011. if (ret) {
  1012. printk(KERN_ERR "onenand_write_ops_nolock: verify failed %d\n", ret);
  1013. break;
  1014. }
  1015. written += thislen;
  1016. if (written == len)
  1017. break;
  1018. column = 0;
  1019. to += thislen;
  1020. buf += thislen;
  1021. }
  1022. ops->retlen = written;
  1023. return ret;
  1024. }
  1025. /**
  1026. * onenand_write_oob_nolock - [Internal] OneNAND write out-of-band
  1027. * @param mtd MTD device structure
  1028. * @param to offset to write to
  1029. * @param len number of bytes to write
  1030. * @param retlen pointer to variable to store the number of written bytes
  1031. * @param buf the data to write
  1032. * @param mode operation mode
  1033. *
  1034. * OneNAND write out-of-band
  1035. */
  1036. static int onenand_write_oob_nolock(struct mtd_info *mtd, loff_t to,
  1037. struct mtd_oob_ops *ops)
  1038. {
  1039. struct onenand_chip *this = mtd->priv;
  1040. int column, ret = 0, oobsize;
  1041. int written = 0;
  1042. u_char *oobbuf;
  1043. size_t len = ops->ooblen;
  1044. const u_char *buf = ops->oobbuf;
  1045. mtd_oob_mode_t mode = ops->mode;
  1046. to += ops->ooboffs;
  1047. MTDDEBUG(MTD_DEBUG_LEVEL3,
  1048. "onenand_write_oob_nolock: to = 0x%08x, len = %i\n",
  1049. (unsigned int) to, (int) len);
  1050. /* Initialize retlen, in case of early exit */
  1051. ops->oobretlen = 0;
  1052. if (mode == MTD_OOB_AUTO)
  1053. oobsize = this->ecclayout->oobavail;
  1054. else
  1055. oobsize = mtd->oobsize;
  1056. column = to & (mtd->oobsize - 1);
  1057. if (unlikely(column >= oobsize)) {
  1058. printk(KERN_ERR "onenand_write_oob_nolock: Attempted to start write outside oob\n");
  1059. return -EINVAL;
  1060. }
  1061. /* For compatibility with NAND: Do not allow write past end of page */
  1062. if (unlikely(column + len > oobsize)) {
  1063. printk(KERN_ERR "onenand_write_oob_nolock: "
  1064. "Attempt to write past end of page\n");
  1065. return -EINVAL;
  1066. }
  1067. /* Do not allow reads past end of device */
  1068. if (unlikely(to >= mtd->size ||
  1069. column + len > ((mtd->size >> this->page_shift) -
  1070. (to >> this->page_shift)) * oobsize)) {
  1071. printk(KERN_ERR "onenand_write_oob_nolock: Attempted to write past end of device\n");
  1072. return -EINVAL;
  1073. }
  1074. oobbuf = this->oob_buf;
  1075. /* Loop until all data write */
  1076. while (written < len) {
  1077. int thislen = min_t(int, oobsize, len - written);
  1078. this->command(mtd, ONENAND_CMD_BUFFERRAM, to, mtd->oobsize);
  1079. /* We send data to spare ram with oobsize
  1080. * to prevent byte access */
  1081. memset(oobbuf, 0xff, mtd->oobsize);
  1082. if (mode == MTD_OOB_AUTO)
  1083. onenand_fill_auto_oob(mtd, oobbuf, buf, column, thislen);
  1084. else
  1085. memcpy(oobbuf + column, buf, thislen);
  1086. this->write_bufferram(mtd, ONENAND_SPARERAM, oobbuf, 0, mtd->oobsize);
  1087. this->command(mtd, ONENAND_CMD_PROGOOB, to, mtd->oobsize);
  1088. onenand_update_bufferram(mtd, to, 0);
  1089. if (ONENAND_IS_2PLANE(this)) {
  1090. ONENAND_SET_BUFFERRAM1(this);
  1091. onenand_update_bufferram(mtd, to + this->writesize, 0);
  1092. }
  1093. ret = this->wait(mtd, FL_WRITING);
  1094. if (ret) {
  1095. printk(KERN_ERR "onenand_write_oob_nolock: write failed %d\n", ret);
  1096. break;
  1097. }
  1098. ret = onenand_verify_oob(mtd, oobbuf, to);
  1099. if (ret) {
  1100. printk(KERN_ERR "onenand_write_oob_nolock: verify failed %d\n", ret);
  1101. break;
  1102. }
  1103. written += thislen;
  1104. if (written == len)
  1105. break;
  1106. to += mtd->writesize;
  1107. buf += thislen;
  1108. column = 0;
  1109. }
  1110. ops->oobretlen = written;
  1111. return ret;
  1112. }
  1113. /**
  1114. * onenand_write - [MTD Interface] compability function for onenand_write_ecc
  1115. * @param mtd MTD device structure
  1116. * @param to offset to write to
  1117. * @param len number of bytes to write
  1118. * @param retlen pointer to variable to store the number of written bytes
  1119. * @param buf the data to write
  1120. *
  1121. * Write with ECC
  1122. */
  1123. int onenand_write(struct mtd_info *mtd, loff_t to, size_t len,
  1124. size_t * retlen, const u_char * buf)
  1125. {
  1126. struct mtd_oob_ops ops = {
  1127. .len = len,
  1128. .ooblen = 0,
  1129. .datbuf = (u_char *) buf,
  1130. .oobbuf = NULL,
  1131. };
  1132. int ret;
  1133. onenand_get_device(mtd, FL_WRITING);
  1134. ret = onenand_write_ops_nolock(mtd, to, &ops);
  1135. onenand_release_device(mtd);
  1136. *retlen = ops.retlen;
  1137. return ret;
  1138. }
  1139. /**
  1140. * onenand_write_oob - [MTD Interface] OneNAND write out-of-band
  1141. * @param mtd MTD device structure
  1142. * @param to offset to write to
  1143. * @param ops oob operation description structure
  1144. *
  1145. * OneNAND write main and/or out-of-band
  1146. */
  1147. int onenand_write_oob(struct mtd_info *mtd, loff_t to,
  1148. struct mtd_oob_ops *ops)
  1149. {
  1150. int ret;
  1151. switch (ops->mode) {
  1152. case MTD_OOB_PLACE:
  1153. case MTD_OOB_AUTO:
  1154. break;
  1155. case MTD_OOB_RAW:
  1156. /* Not implemented yet */
  1157. default:
  1158. return -EINVAL;
  1159. }
  1160. onenand_get_device(mtd, FL_WRITING);
  1161. if (ops->datbuf)
  1162. ret = onenand_write_ops_nolock(mtd, to, ops);
  1163. else
  1164. ret = onenand_write_oob_nolock(mtd, to, ops);
  1165. onenand_release_device(mtd);
  1166. return ret;
  1167. }
  1168. /**
  1169. * onenand_block_isbad_nolock - [GENERIC] Check if a block is marked bad
  1170. * @param mtd MTD device structure
  1171. * @param ofs offset from device start
  1172. * @param allowbbt 1, if its allowed to access the bbt area
  1173. *
  1174. * Check, if the block is bad, Either by reading the bad block table or
  1175. * calling of the scan function.
  1176. */
  1177. static int onenand_block_isbad_nolock(struct mtd_info *mtd, loff_t ofs, int allowbbt)
  1178. {
  1179. struct onenand_chip *this = mtd->priv;
  1180. struct bbm_info *bbm = this->bbm;
  1181. /* Return info from the table */
  1182. return bbm->isbad_bbt(mtd, ofs, allowbbt);
  1183. }
  1184. /**
  1185. * onenand_erase - [MTD Interface] erase block(s)
  1186. * @param mtd MTD device structure
  1187. * @param instr erase instruction
  1188. *
  1189. * Erase one ore more blocks
  1190. */
  1191. int onenand_erase(struct mtd_info *mtd, struct erase_info *instr)
  1192. {
  1193. struct onenand_chip *this = mtd->priv;
  1194. unsigned int block_size;
  1195. loff_t addr;
  1196. int len;
  1197. int ret = 0;
  1198. MTDDEBUG (MTD_DEBUG_LEVEL3,
  1199. "onenand_erase: start = 0x%08x, len = %i\n",
  1200. (unsigned int)instr->addr, (unsigned int)instr->len);
  1201. block_size = (1 << this->erase_shift);
  1202. /* Start address must align on block boundary */
  1203. if (unlikely(instr->addr & (block_size - 1))) {
  1204. MTDDEBUG (MTD_DEBUG_LEVEL0,
  1205. "onenand_erase: Unaligned address\n");
  1206. return -EINVAL;
  1207. }
  1208. /* Length must align on block boundary */
  1209. if (unlikely(instr->len & (block_size - 1))) {
  1210. MTDDEBUG (MTD_DEBUG_LEVEL0,
  1211. "onenand_erase: Length not block aligned\n");
  1212. return -EINVAL;
  1213. }
  1214. /* Do not allow erase past end of device */
  1215. if (unlikely((instr->len + instr->addr) > mtd->size)) {
  1216. MTDDEBUG (MTD_DEBUG_LEVEL0,
  1217. "onenand_erase: Erase past end of device\n");
  1218. return -EINVAL;
  1219. }
  1220. instr->fail_addr = 0xffffffff;
  1221. /* Grab the lock and see if the device is available */
  1222. onenand_get_device(mtd, FL_ERASING);
  1223. /* Loop throught the pages */
  1224. len = instr->len;
  1225. addr = instr->addr;
  1226. instr->state = MTD_ERASING;
  1227. while (len) {
  1228. /* TODO Check badblock */
  1229. this->command(mtd, ONENAND_CMD_ERASE, addr, block_size);
  1230. onenand_invalidate_bufferram(mtd, addr, block_size);
  1231. ret = this->wait(mtd, FL_ERASING);
  1232. /* Check, if it is write protected */
  1233. if (ret) {
  1234. if (ret == -EPERM)
  1235. MTDDEBUG (MTD_DEBUG_LEVEL0, "onenand_erase: "
  1236. "Device is write protected!!!\n");
  1237. else
  1238. MTDDEBUG (MTD_DEBUG_LEVEL0, "onenand_erase: "
  1239. "Failed erase, block %d\n",
  1240. (unsigned)(addr >> this->erase_shift));
  1241. instr->state = MTD_ERASE_FAILED;
  1242. instr->fail_addr = addr;
  1243. goto erase_exit;
  1244. }
  1245. len -= block_size;
  1246. addr += block_size;
  1247. }
  1248. instr->state = MTD_ERASE_DONE;
  1249. erase_exit:
  1250. ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
  1251. /* Do call back function */
  1252. if (!ret)
  1253. mtd_erase_callback(instr);
  1254. /* Deselect and wake up anyone waiting on the device */
  1255. onenand_release_device(mtd);
  1256. return ret;
  1257. }
  1258. /**
  1259. * onenand_sync - [MTD Interface] sync
  1260. * @param mtd MTD device structure
  1261. *
  1262. * Sync is actually a wait for chip ready function
  1263. */
  1264. void onenand_sync(struct mtd_info *mtd)
  1265. {
  1266. MTDDEBUG (MTD_DEBUG_LEVEL3, "onenand_sync: called\n");
  1267. /* Grab the lock and see if the device is available */
  1268. onenand_get_device(mtd, FL_SYNCING);
  1269. /* Release it and go back */
  1270. onenand_release_device(mtd);
  1271. }
  1272. /**
  1273. * onenand_block_isbad - [MTD Interface] Check whether the block at the given offset is bad
  1274. * @param mtd MTD device structure
  1275. * @param ofs offset relative to mtd start
  1276. *
  1277. * Check whether the block is bad
  1278. */
  1279. int onenand_block_isbad(struct mtd_info *mtd, loff_t ofs)
  1280. {
  1281. int ret;
  1282. /* Check for invalid offset */
  1283. if (ofs > mtd->size)
  1284. return -EINVAL;
  1285. onenand_get_device(mtd, FL_READING);
  1286. ret = onenand_block_isbad_nolock(mtd,ofs, 0);
  1287. onenand_release_device(mtd);
  1288. return ret;
  1289. }
  1290. /**
  1291. * onenand_block_markbad - [MTD Interface] Mark the block at the given offset as bad
  1292. * @param mtd MTD device structure
  1293. * @param ofs offset relative to mtd start
  1294. *
  1295. * Mark the block as bad
  1296. */
  1297. int onenand_block_markbad(struct mtd_info *mtd, loff_t ofs)
  1298. {
  1299. struct onenand_chip *this = mtd->priv;
  1300. int ret;
  1301. ret = onenand_block_isbad(mtd, ofs);
  1302. if (ret) {
  1303. /* If it was bad already, return success and do nothing */
  1304. if (ret > 0)
  1305. return 0;
  1306. return ret;
  1307. }
  1308. ret = this->block_markbad(mtd, ofs);
  1309. return ret;
  1310. }
  1311. /**
  1312. * onenand_unlock - [MTD Interface] Unlock block(s)
  1313. * @param mtd MTD device structure
  1314. * @param ofs offset relative to mtd start
  1315. * @param len number of bytes to unlock
  1316. *
  1317. * Unlock one or more blocks
  1318. */
  1319. int onenand_unlock(struct mtd_info *mtd, loff_t ofs, size_t len)
  1320. {
  1321. struct onenand_chip *this = mtd->priv;
  1322. int start, end, block, value, status;
  1323. start = ofs >> this->erase_shift;
  1324. end = len >> this->erase_shift;
  1325. /* Continuous lock scheme */
  1326. if (this->options & ONENAND_CONT_LOCK) {
  1327. /* Set start block address */
  1328. this->write_word(start,
  1329. this->base + ONENAND_REG_START_BLOCK_ADDRESS);
  1330. /* Set end block address */
  1331. this->write_word(end - 1,
  1332. this->base + ONENAND_REG_END_BLOCK_ADDRESS);
  1333. /* Write unlock command */
  1334. this->command(mtd, ONENAND_CMD_UNLOCK, 0, 0);
  1335. /* There's no return value */
  1336. this->wait(mtd, FL_UNLOCKING);
  1337. /* Sanity check */
  1338. while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
  1339. & ONENAND_CTRL_ONGO)
  1340. continue;
  1341. /* Check lock status */
  1342. status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
  1343. if (!(status & ONENAND_WP_US))
  1344. printk(KERN_ERR "wp status = 0x%x\n", status);
  1345. return 0;
  1346. }
  1347. /* Block lock scheme */
  1348. for (block = start; block < end; block++) {
  1349. /* Set start block address */
  1350. this->write_word(block,
  1351. this->base + ONENAND_REG_START_BLOCK_ADDRESS);
  1352. /* Write unlock command */
  1353. this->command(mtd, ONENAND_CMD_UNLOCK, 0, 0);
  1354. /* There's no return value */
  1355. this->wait(mtd, FL_UNLOCKING);
  1356. /* Sanity check */
  1357. while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
  1358. & ONENAND_CTRL_ONGO)
  1359. continue;
  1360. /* Set block address for read block status */
  1361. value = onenand_block_address(this->device_id, block);
  1362. this->write_word(value,
  1363. this->base + ONENAND_REG_START_ADDRESS1);
  1364. /* Check lock status */
  1365. status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
  1366. if (!(status & ONENAND_WP_US))
  1367. printk(KERN_ERR "block = %d, wp status = 0x%x\n",
  1368. block, status);
  1369. }
  1370. return 0;
  1371. }
  1372. /**
  1373. * onenand_print_device_info - Print device ID
  1374. * @param device device ID
  1375. *
  1376. * Print device ID
  1377. */
  1378. char * onenand_print_device_info(int device)
  1379. {
  1380. int vcc, demuxed, ddp, density;
  1381. char *dev_info = malloc(80);
  1382. vcc = device & ONENAND_DEVICE_VCC_MASK;
  1383. demuxed = device & ONENAND_DEVICE_IS_DEMUX;
  1384. ddp = device & ONENAND_DEVICE_IS_DDP;
  1385. density = device >> ONENAND_DEVICE_DENSITY_SHIFT;
  1386. sprintf(dev_info, "%sOneNAND%s %dMB %sV 16-bit (0x%02x)",
  1387. demuxed ? "" : "Muxed ",
  1388. ddp ? "(DDP)" : "",
  1389. (16 << density), vcc ? "2.65/3.3" : "1.8", device);
  1390. return dev_info;
  1391. }
  1392. static const struct onenand_manufacturers onenand_manuf_ids[] = {
  1393. {ONENAND_MFR_SAMSUNG, "Samsung"},
  1394. {ONENAND_MFR_UNKNOWN, "Unknown"}
  1395. };
  1396. /**
  1397. * onenand_check_maf - Check manufacturer ID
  1398. * @param manuf manufacturer ID
  1399. *
  1400. * Check manufacturer ID
  1401. */
  1402. static int onenand_check_maf(int manuf)
  1403. {
  1404. int i;
  1405. for (i = 0; onenand_manuf_ids[i].id; i++) {
  1406. if (manuf == onenand_manuf_ids[i].id)
  1407. break;
  1408. }
  1409. #ifdef ONENAND_DEBUG
  1410. printk(KERN_DEBUG "OneNAND Manufacturer: %s (0x%0x)\n",
  1411. onenand_manuf_ids[i].name, manuf);
  1412. #endif
  1413. return (i != ONENAND_MFR_UNKNOWN);
  1414. }
  1415. /**
  1416. * onenand_probe - [OneNAND Interface] Probe the OneNAND device
  1417. * @param mtd MTD device structure
  1418. *
  1419. * OneNAND detection method:
  1420. * Compare the the values from command with ones from register
  1421. */
  1422. static int onenand_probe(struct mtd_info *mtd)
  1423. {
  1424. struct onenand_chip *this = mtd->priv;
  1425. int bram_maf_id, bram_dev_id, maf_id, dev_id;
  1426. int version_id;
  1427. int density;
  1428. /* Send the command for reading device ID from BootRAM */
  1429. this->write_word(ONENAND_CMD_READID, this->base + ONENAND_BOOTRAM);
  1430. /* Read manufacturer and device IDs from BootRAM */
  1431. bram_maf_id = this->read_word(this->base + ONENAND_BOOTRAM + 0x0);
  1432. bram_dev_id = this->read_word(this->base + ONENAND_BOOTRAM + 0x2);
  1433. /* Check manufacturer ID */
  1434. if (onenand_check_maf(bram_maf_id))
  1435. return -ENXIO;
  1436. /* Reset OneNAND to read default register values */
  1437. this->write_word(ONENAND_CMD_RESET, this->base + ONENAND_BOOTRAM);
  1438. /* Wait reset */
  1439. this->wait(mtd, FL_RESETING);
  1440. /* Read manufacturer and device IDs from Register */
  1441. maf_id = this->read_word(this->base + ONENAND_REG_MANUFACTURER_ID);
  1442. dev_id = this->read_word(this->base + ONENAND_REG_DEVICE_ID);
  1443. /* Check OneNAND device */
  1444. if (maf_id != bram_maf_id || dev_id != bram_dev_id)
  1445. return -ENXIO;
  1446. /* FIXME : Current OneNAND MTD doesn't support Flex-OneNAND */
  1447. if (dev_id & (1 << 9)) {
  1448. printk("Not yet support Flex-OneNAND\n");
  1449. return -ENXIO;
  1450. }
  1451. /* Flash device information */
  1452. mtd->name = onenand_print_device_info(dev_id);
  1453. this->device_id = dev_id;
  1454. density = dev_id >> ONENAND_DEVICE_DENSITY_SHIFT;
  1455. this->chipsize = (16 << density) << 20;
  1456. /* OneNAND page size & block size */
  1457. /* The data buffer size is equal to page size */
  1458. mtd->writesize =
  1459. this->read_word(this->base + ONENAND_REG_DATA_BUFFER_SIZE);
  1460. mtd->oobsize = mtd->writesize >> 5;
  1461. /* Pagers per block is always 64 in OneNAND */
  1462. mtd->erasesize = mtd->writesize << 6;
  1463. this->erase_shift = ffs(mtd->erasesize) - 1;
  1464. this->page_shift = ffs(mtd->writesize) - 1;
  1465. this->ppb_shift = (this->erase_shift - this->page_shift);
  1466. this->page_mask = (mtd->erasesize / mtd->writesize) - 1;
  1467. /* It's real page size */
  1468. this->writesize = mtd->writesize;
  1469. /* REVIST: Multichip handling */
  1470. mtd->size = this->chipsize;
  1471. /* Version ID */
  1472. version_id = this->read_word(this->base + ONENAND_REG_VERSION_ID);
  1473. #ifdef ONENAND_DEBUG
  1474. printk(KERN_DEBUG "OneNAND version = 0x%04x\n", version_id);
  1475. #endif
  1476. /* Lock scheme */
  1477. if (density <= ONENAND_DEVICE_DENSITY_512Mb &&
  1478. !(version_id >> ONENAND_VERSION_PROCESS_SHIFT)) {
  1479. printk(KERN_INFO "Lock scheme is Continues Lock\n");
  1480. this->options |= ONENAND_CONT_LOCK;
  1481. }
  1482. mtd->flags = MTD_CAP_NANDFLASH;
  1483. mtd->erase = onenand_erase;
  1484. mtd->read = onenand_read;
  1485. mtd->write = onenand_write;
  1486. mtd->read_oob = onenand_read_oob;
  1487. mtd->write_oob = onenand_write_oob;
  1488. mtd->sync = onenand_sync;
  1489. mtd->block_isbad = onenand_block_isbad;
  1490. mtd->block_markbad = onenand_block_markbad;
  1491. return 0;
  1492. }
  1493. /**
  1494. * onenand_scan - [OneNAND Interface] Scan for the OneNAND device
  1495. * @param mtd MTD device structure
  1496. * @param maxchips Number of chips to scan for
  1497. *
  1498. * This fills out all the not initialized function pointers
  1499. * with the defaults.
  1500. * The flash ID is read and the mtd/chip structures are
  1501. * filled with the appropriate values.
  1502. */
  1503. int onenand_scan(struct mtd_info *mtd, int maxchips)
  1504. {
  1505. struct onenand_chip *this = mtd->priv;
  1506. if (!this->read_word)
  1507. this->read_word = onenand_readw;
  1508. if (!this->write_word)
  1509. this->write_word = onenand_writew;
  1510. if (!this->command)
  1511. this->command = onenand_command;
  1512. if (!this->wait)
  1513. this->wait = onenand_wait;
  1514. if (!this->read_bufferram)
  1515. this->read_bufferram = onenand_read_bufferram;
  1516. if (!this->write_bufferram)
  1517. this->write_bufferram = onenand_write_bufferram;
  1518. if (onenand_probe(mtd))
  1519. return -ENXIO;
  1520. /* Set Sync. Burst Read after probing */
  1521. if (this->mmcontrol) {
  1522. printk(KERN_INFO "OneNAND Sync. Burst Read support\n");
  1523. this->read_bufferram = onenand_sync_read_bufferram;
  1524. }
  1525. /* Allocate buffers, if necessary */
  1526. if (!this->page_buf) {
  1527. this->page_buf = kzalloc(mtd->writesize, GFP_KERNEL);
  1528. if (!this->page_buf) {
  1529. printk(KERN_ERR "onenand_scan(): Can't allocate page_buf\n");
  1530. return -ENOMEM;
  1531. }
  1532. this->options |= ONENAND_PAGEBUF_ALLOC;
  1533. }
  1534. if (!this->oob_buf) {
  1535. this->oob_buf = kzalloc(mtd->oobsize, GFP_KERNEL);
  1536. if (!this->oob_buf) {
  1537. printk(KERN_ERR "onenand_scan: Can't allocate oob_buf\n");
  1538. if (this->options & ONENAND_PAGEBUF_ALLOC) {
  1539. this->options &= ~ONENAND_PAGEBUF_ALLOC;
  1540. kfree(this->page_buf);
  1541. }
  1542. return -ENOMEM;
  1543. }
  1544. this->options |= ONENAND_OOBBUF_ALLOC;
  1545. }
  1546. onenand_unlock(mtd, 0, mtd->size);
  1547. return onenand_default_bbt(mtd);
  1548. }
  1549. /**
  1550. * onenand_release - [OneNAND Interface] Free resources held by the OneNAND device
  1551. * @param mtd MTD device structure
  1552. */
  1553. void onenand_release(struct mtd_info *mtd)
  1554. {
  1555. }