onenand_base.c 56 KB

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