onenand_base.c 71 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755
  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. * Rohit Hagargundgi <h.rohit at samsung.com>,
  13. * Amul Kumar Saha <amul.saha@samsung.com>:
  14. * Flex-OneNAND support
  15. * Copyright (C) Samsung Electronics, 2009
  16. *
  17. * This program is free software; you can redistribute it and/or modify
  18. * it under the terms of the GNU General Public License version 2 as
  19. * published by the Free Software Foundation.
  20. */
  21. #include <common.h>
  22. #include <linux/mtd/compat.h>
  23. #include <linux/mtd/mtd.h>
  24. #include <linux/mtd/onenand.h>
  25. #include <asm/io.h>
  26. #include <asm/errno.h>
  27. #include <malloc.h>
  28. /* It should access 16-bit instead of 8-bit */
  29. static void *memcpy_16(void *dst, const void *src, unsigned int len)
  30. {
  31. void *ret = dst;
  32. short *d = dst;
  33. const short *s = src;
  34. len >>= 1;
  35. while (len-- > 0)
  36. *d++ = *s++;
  37. return ret;
  38. }
  39. /**
  40. * onenand_oob_128 - oob info for Flex-Onenand with 4KB page
  41. * For now, we expose only 64 out of 80 ecc bytes
  42. */
  43. static struct nand_ecclayout onenand_oob_128 = {
  44. .eccbytes = 64,
  45. .eccpos = {
  46. 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
  47. 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
  48. 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
  49. 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
  50. 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
  51. 86, 87, 88, 89, 90, 91, 92, 93, 94, 95,
  52. 102, 103, 104, 105
  53. },
  54. .oobfree = {
  55. {2, 4}, {18, 4}, {34, 4}, {50, 4},
  56. {66, 4}, {82, 4}, {98, 4}, {114, 4}
  57. }
  58. };
  59. /**
  60. * onenand_oob_64 - oob info for large (2KB) page
  61. */
  62. static struct nand_ecclayout onenand_oob_64 = {
  63. .eccbytes = 20,
  64. .eccpos = {
  65. 8, 9, 10, 11, 12,
  66. 24, 25, 26, 27, 28,
  67. 40, 41, 42, 43, 44,
  68. 56, 57, 58, 59, 60,
  69. },
  70. .oobfree = {
  71. {2, 3}, {14, 2}, {18, 3}, {30, 2},
  72. {34, 3}, {46, 2}, {50, 3}, {62, 2}
  73. }
  74. };
  75. /**
  76. * onenand_oob_32 - oob info for middle (1KB) page
  77. */
  78. static struct nand_ecclayout onenand_oob_32 = {
  79. .eccbytes = 10,
  80. .eccpos = {
  81. 8, 9, 10, 11, 12,
  82. 24, 25, 26, 27, 28,
  83. },
  84. .oobfree = { {2, 3}, {14, 2}, {18, 3}, {30, 2} }
  85. };
  86. static const unsigned char ffchars[] = {
  87. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  88. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 16 */
  89. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  90. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 32 */
  91. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  92. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 48 */
  93. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  94. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 64 */
  95. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  96. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 80 */
  97. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  98. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 96 */
  99. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  100. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 112 */
  101. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  102. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 128 */
  103. };
  104. /**
  105. * onenand_readw - [OneNAND Interface] Read OneNAND register
  106. * @param addr address to read
  107. *
  108. * Read OneNAND register
  109. */
  110. static unsigned short onenand_readw(void __iomem * addr)
  111. {
  112. return readw(addr);
  113. }
  114. /**
  115. * onenand_writew - [OneNAND Interface] Write OneNAND register with value
  116. * @param value value to write
  117. * @param addr address to write
  118. *
  119. * Write OneNAND register with value
  120. */
  121. static void onenand_writew(unsigned short value, void __iomem * addr)
  122. {
  123. writew(value, addr);
  124. }
  125. /**
  126. * onenand_block_address - [DEFAULT] Get block address
  127. * @param device the device id
  128. * @param block the block
  129. * @return translated block address if DDP, otherwise same
  130. *
  131. * Setup Start Address 1 Register (F100h)
  132. */
  133. static int onenand_block_address(struct onenand_chip *this, int block)
  134. {
  135. /* Device Flash Core select, NAND Flash Block Address */
  136. if (block & this->density_mask)
  137. return ONENAND_DDP_CHIP1 | (block ^ this->density_mask);
  138. return block;
  139. }
  140. /**
  141. * onenand_bufferram_address - [DEFAULT] Get bufferram address
  142. * @param device the device id
  143. * @param block the block
  144. * @return set DBS value if DDP, otherwise 0
  145. *
  146. * Setup Start Address 2 Register (F101h) for DDP
  147. */
  148. static int onenand_bufferram_address(struct onenand_chip *this, int block)
  149. {
  150. /* Device BufferRAM Select */
  151. if (block & this->density_mask)
  152. return ONENAND_DDP_CHIP1;
  153. return ONENAND_DDP_CHIP0;
  154. }
  155. /**
  156. * onenand_page_address - [DEFAULT] Get page address
  157. * @param page the page address
  158. * @param sector the sector address
  159. * @return combined page and sector address
  160. *
  161. * Setup Start Address 8 Register (F107h)
  162. */
  163. static int onenand_page_address(int page, int sector)
  164. {
  165. /* Flash Page Address, Flash Sector Address */
  166. int fpa, fsa;
  167. fpa = page & ONENAND_FPA_MASK;
  168. fsa = sector & ONENAND_FSA_MASK;
  169. return ((fpa << ONENAND_FPA_SHIFT) | fsa);
  170. }
  171. /**
  172. * onenand_buffer_address - [DEFAULT] Get buffer address
  173. * @param dataram1 DataRAM index
  174. * @param sectors the sector address
  175. * @param count the number of sectors
  176. * @return the start buffer value
  177. *
  178. * Setup Start Buffer Register (F200h)
  179. */
  180. static int onenand_buffer_address(int dataram1, int sectors, int count)
  181. {
  182. int bsa, bsc;
  183. /* BufferRAM Sector Address */
  184. bsa = sectors & ONENAND_BSA_MASK;
  185. if (dataram1)
  186. bsa |= ONENAND_BSA_DATARAM1; /* DataRAM1 */
  187. else
  188. bsa |= ONENAND_BSA_DATARAM0; /* DataRAM0 */
  189. /* BufferRAM Sector Count */
  190. bsc = count & ONENAND_BSC_MASK;
  191. return ((bsa << ONENAND_BSA_SHIFT) | bsc);
  192. }
  193. /**
  194. * flexonenand_block - Return block number for flash address
  195. * @param this - OneNAND device structure
  196. * @param addr - Address for which block number is needed
  197. */
  198. static unsigned int flexonenand_block(struct onenand_chip *this, loff_t addr)
  199. {
  200. unsigned int boundary, blk, die = 0;
  201. if (ONENAND_IS_DDP(this) && addr >= this->diesize[0]) {
  202. die = 1;
  203. addr -= this->diesize[0];
  204. }
  205. boundary = this->boundary[die];
  206. blk = addr >> (this->erase_shift - 1);
  207. if (blk > boundary)
  208. blk = (blk + boundary + 1) >> 1;
  209. blk += die ? this->density_mask : 0;
  210. return blk;
  211. }
  212. unsigned int onenand_block(struct onenand_chip *this, loff_t addr)
  213. {
  214. if (!FLEXONENAND(this))
  215. return addr >> this->erase_shift;
  216. return flexonenand_block(this, addr);
  217. }
  218. /**
  219. * flexonenand_addr - Return address of the block
  220. * @this: OneNAND device structure
  221. * @block: Block number on Flex-OneNAND
  222. *
  223. * Return address of the block
  224. */
  225. static loff_t flexonenand_addr(struct onenand_chip *this, int block)
  226. {
  227. loff_t ofs = 0;
  228. int die = 0, boundary;
  229. if (ONENAND_IS_DDP(this) && block >= this->density_mask) {
  230. block -= this->density_mask;
  231. die = 1;
  232. ofs = this->diesize[0];
  233. }
  234. boundary = this->boundary[die];
  235. ofs += (loff_t) block << (this->erase_shift - 1);
  236. if (block > (boundary + 1))
  237. ofs += (loff_t) (block - boundary - 1)
  238. << (this->erase_shift - 1);
  239. return ofs;
  240. }
  241. loff_t onenand_addr(struct onenand_chip *this, int block)
  242. {
  243. if (!FLEXONENAND(this))
  244. return (loff_t) block << this->erase_shift;
  245. return flexonenand_addr(this, block);
  246. }
  247. /**
  248. * flexonenand_region - [Flex-OneNAND] Return erase region of addr
  249. * @param mtd MTD device structure
  250. * @param addr address whose erase region needs to be identified
  251. */
  252. int flexonenand_region(struct mtd_info *mtd, loff_t addr)
  253. {
  254. int i;
  255. for (i = 0; i < mtd->numeraseregions; i++)
  256. if (addr < mtd->eraseregions[i].offset)
  257. break;
  258. return i - 1;
  259. }
  260. /**
  261. * onenand_get_density - [DEFAULT] Get OneNAND density
  262. * @param dev_id OneNAND device ID
  263. *
  264. * Get OneNAND density from device ID
  265. */
  266. static inline int onenand_get_density(int dev_id)
  267. {
  268. int density = dev_id >> ONENAND_DEVICE_DENSITY_SHIFT;
  269. return (density & ONENAND_DEVICE_DENSITY_MASK);
  270. }
  271. /**
  272. * onenand_command - [DEFAULT] Send command to OneNAND device
  273. * @param mtd MTD device structure
  274. * @param cmd the command to be sent
  275. * @param addr offset to read from or write to
  276. * @param len number of bytes to read or write
  277. *
  278. * Send command to OneNAND device. This function is used for middle/large page
  279. * devices (1KB/2KB Bytes per page)
  280. */
  281. static int onenand_command(struct mtd_info *mtd, int cmd, loff_t addr,
  282. size_t len)
  283. {
  284. struct onenand_chip *this = mtd->priv;
  285. int value;
  286. int block, page;
  287. /* Now we use page size operation */
  288. int sectors = 0, count = 0;
  289. /* Address translation */
  290. switch (cmd) {
  291. case ONENAND_CMD_UNLOCK:
  292. case ONENAND_CMD_LOCK:
  293. case ONENAND_CMD_LOCK_TIGHT:
  294. case ONENAND_CMD_UNLOCK_ALL:
  295. block = -1;
  296. page = -1;
  297. break;
  298. case FLEXONENAND_CMD_PI_ACCESS:
  299. /* addr contains die index */
  300. block = addr * this->density_mask;
  301. page = -1;
  302. break;
  303. case ONENAND_CMD_ERASE:
  304. case ONENAND_CMD_BUFFERRAM:
  305. block = onenand_block(this, addr);
  306. page = -1;
  307. break;
  308. case FLEXONENAND_CMD_READ_PI:
  309. cmd = ONENAND_CMD_READ;
  310. block = addr * this->density_mask;
  311. page = 0;
  312. break;
  313. default:
  314. block = onenand_block(this, addr);
  315. page = (int) (addr
  316. - onenand_addr(this, block)) >> this->page_shift;
  317. page &= this->page_mask;
  318. break;
  319. }
  320. /* NOTE: The setting order of the registers is very important! */
  321. if (cmd == ONENAND_CMD_BUFFERRAM) {
  322. /* Select DataRAM for DDP */
  323. value = onenand_bufferram_address(this, block);
  324. this->write_word(value,
  325. this->base + ONENAND_REG_START_ADDRESS2);
  326. if (ONENAND_IS_MLC(this))
  327. ONENAND_SET_BUFFERRAM0(this);
  328. else
  329. /* Switch to the next data buffer */
  330. ONENAND_SET_NEXT_BUFFERRAM(this);
  331. return 0;
  332. }
  333. if (block != -1) {
  334. /* Write 'DFS, FBA' of Flash */
  335. value = onenand_block_address(this, block);
  336. this->write_word(value,
  337. this->base + ONENAND_REG_START_ADDRESS1);
  338. /* Select DataRAM for DDP */
  339. value = onenand_bufferram_address(this, block);
  340. this->write_word(value,
  341. this->base + ONENAND_REG_START_ADDRESS2);
  342. }
  343. if (page != -1) {
  344. int dataram;
  345. switch (cmd) {
  346. case FLEXONENAND_CMD_RECOVER_LSB:
  347. case ONENAND_CMD_READ:
  348. case ONENAND_CMD_READOOB:
  349. if (ONENAND_IS_MLC(this))
  350. dataram = ONENAND_SET_BUFFERRAM0(this);
  351. else
  352. dataram = ONENAND_SET_NEXT_BUFFERRAM(this);
  353. break;
  354. default:
  355. dataram = ONENAND_CURRENT_BUFFERRAM(this);
  356. break;
  357. }
  358. /* Write 'FPA, FSA' of Flash */
  359. value = onenand_page_address(page, sectors);
  360. this->write_word(value,
  361. this->base + ONENAND_REG_START_ADDRESS8);
  362. /* Write 'BSA, BSC' of DataRAM */
  363. value = onenand_buffer_address(dataram, sectors, count);
  364. this->write_word(value, this->base + ONENAND_REG_START_BUFFER);
  365. }
  366. /* Interrupt clear */
  367. this->write_word(ONENAND_INT_CLEAR, this->base + ONENAND_REG_INTERRUPT);
  368. /* Write command */
  369. this->write_word(cmd, this->base + ONENAND_REG_COMMAND);
  370. return 0;
  371. }
  372. /**
  373. * onenand_read_ecc - return ecc status
  374. * @param this onenand chip structure
  375. */
  376. static int onenand_read_ecc(struct onenand_chip *this)
  377. {
  378. int ecc, i;
  379. if (!FLEXONENAND(this))
  380. return this->read_word(this->base + ONENAND_REG_ECC_STATUS);
  381. for (i = 0; i < 4; i++) {
  382. ecc = this->read_word(this->base
  383. + ((ONENAND_REG_ECC_STATUS + i) << 1));
  384. if (likely(!ecc))
  385. continue;
  386. if (ecc & FLEXONENAND_UNCORRECTABLE_ERROR)
  387. return ONENAND_ECC_2BIT_ALL;
  388. }
  389. return 0;
  390. }
  391. /**
  392. * onenand_wait - [DEFAULT] wait until the command is done
  393. * @param mtd MTD device structure
  394. * @param state state to select the max. timeout value
  395. *
  396. * Wait for command done. This applies to all OneNAND command
  397. * Read can take up to 30us, erase up to 2ms and program up to 350us
  398. * according to general OneNAND specs
  399. */
  400. static int onenand_wait(struct mtd_info *mtd, int state)
  401. {
  402. struct onenand_chip *this = mtd->priv;
  403. unsigned int flags = ONENAND_INT_MASTER;
  404. unsigned int interrupt = 0;
  405. unsigned int ctrl;
  406. while (1) {
  407. interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
  408. if (interrupt & flags)
  409. break;
  410. }
  411. ctrl = this->read_word(this->base + ONENAND_REG_CTRL_STATUS);
  412. if (interrupt & ONENAND_INT_READ) {
  413. int ecc = onenand_read_ecc(this);
  414. if (ecc & ONENAND_ECC_2BIT_ALL) {
  415. printk("onenand_wait: ECC error = 0x%04x\n", ecc);
  416. return -EBADMSG;
  417. }
  418. }
  419. if (ctrl & ONENAND_CTRL_ERROR) {
  420. printk("onenand_wait: controller error = 0x%04x\n", ctrl);
  421. if (ctrl & ONENAND_CTRL_LOCK)
  422. printk("onenand_wait: it's locked error = 0x%04x\n",
  423. ctrl);
  424. return -EIO;
  425. }
  426. return 0;
  427. }
  428. /**
  429. * onenand_bufferram_offset - [DEFAULT] BufferRAM offset
  430. * @param mtd MTD data structure
  431. * @param area BufferRAM area
  432. * @return offset given area
  433. *
  434. * Return BufferRAM offset given area
  435. */
  436. static inline int onenand_bufferram_offset(struct mtd_info *mtd, int area)
  437. {
  438. struct onenand_chip *this = mtd->priv;
  439. if (ONENAND_CURRENT_BUFFERRAM(this)) {
  440. if (area == ONENAND_DATARAM)
  441. return mtd->writesize;
  442. if (area == ONENAND_SPARERAM)
  443. return mtd->oobsize;
  444. }
  445. return 0;
  446. }
  447. /**
  448. * onenand_read_bufferram - [OneNAND Interface] Read the bufferram area
  449. * @param mtd MTD data structure
  450. * @param area BufferRAM area
  451. * @param buffer the databuffer to put/get data
  452. * @param offset offset to read from or write to
  453. * @param count number of bytes to read/write
  454. *
  455. * Read the BufferRAM area
  456. */
  457. static int onenand_read_bufferram(struct mtd_info *mtd, loff_t addr, int area,
  458. unsigned char *buffer, int offset,
  459. size_t count)
  460. {
  461. struct onenand_chip *this = mtd->priv;
  462. void __iomem *bufferram;
  463. bufferram = this->base + area;
  464. bufferram += onenand_bufferram_offset(mtd, area);
  465. memcpy_16(buffer, bufferram + offset, count);
  466. return 0;
  467. }
  468. /**
  469. * onenand_sync_read_bufferram - [OneNAND Interface] Read the bufferram area with Sync. Burst mode
  470. * @param mtd MTD data structure
  471. * @param area BufferRAM area
  472. * @param buffer the databuffer to put/get data
  473. * @param offset offset to read from or write to
  474. * @param count number of bytes to read/write
  475. *
  476. * Read the BufferRAM area with Sync. Burst Mode
  477. */
  478. static int onenand_sync_read_bufferram(struct mtd_info *mtd, loff_t addr, int area,
  479. unsigned char *buffer, int offset,
  480. size_t count)
  481. {
  482. struct onenand_chip *this = mtd->priv;
  483. void __iomem *bufferram;
  484. bufferram = this->base + area;
  485. bufferram += onenand_bufferram_offset(mtd, area);
  486. this->mmcontrol(mtd, ONENAND_SYS_CFG1_SYNC_READ);
  487. memcpy_16(buffer, bufferram + offset, count);
  488. this->mmcontrol(mtd, 0);
  489. return 0;
  490. }
  491. /**
  492. * onenand_write_bufferram - [OneNAND Interface] Write the bufferram area
  493. * @param mtd MTD data structure
  494. * @param area BufferRAM area
  495. * @param buffer the databuffer to put/get data
  496. * @param offset offset to read from or write to
  497. * @param count number of bytes to read/write
  498. *
  499. * Write the BufferRAM area
  500. */
  501. static int onenand_write_bufferram(struct mtd_info *mtd, loff_t addr, int area,
  502. const unsigned char *buffer, int offset,
  503. size_t count)
  504. {
  505. struct onenand_chip *this = mtd->priv;
  506. void __iomem *bufferram;
  507. bufferram = this->base + area;
  508. bufferram += onenand_bufferram_offset(mtd, area);
  509. memcpy_16(bufferram + offset, buffer, count);
  510. return 0;
  511. }
  512. /**
  513. * onenand_get_2x_blockpage - [GENERIC] Get blockpage at 2x program mode
  514. * @param mtd MTD data structure
  515. * @param addr address to check
  516. * @return blockpage address
  517. *
  518. * Get blockpage address at 2x program mode
  519. */
  520. static int onenand_get_2x_blockpage(struct mtd_info *mtd, loff_t addr)
  521. {
  522. struct onenand_chip *this = mtd->priv;
  523. int blockpage, block, page;
  524. /* Calculate the even block number */
  525. block = (int) (addr >> this->erase_shift) & ~1;
  526. /* Is it the odd plane? */
  527. if (addr & this->writesize)
  528. block++;
  529. page = (int) (addr >> (this->page_shift + 1)) & this->page_mask;
  530. blockpage = (block << 7) | page;
  531. return blockpage;
  532. }
  533. /**
  534. * onenand_check_bufferram - [GENERIC] Check BufferRAM information
  535. * @param mtd MTD data structure
  536. * @param addr address to check
  537. * @return 1 if there are valid data, otherwise 0
  538. *
  539. * Check bufferram if there is data we required
  540. */
  541. static int onenand_check_bufferram(struct mtd_info *mtd, loff_t addr)
  542. {
  543. struct onenand_chip *this = mtd->priv;
  544. int blockpage, found = 0;
  545. unsigned int i;
  546. #ifdef CONFIG_S3C64XX
  547. return 0;
  548. #endif
  549. if (ONENAND_IS_2PLANE(this))
  550. blockpage = onenand_get_2x_blockpage(mtd, addr);
  551. else
  552. blockpage = (int) (addr >> this->page_shift);
  553. /* Is there valid data? */
  554. i = ONENAND_CURRENT_BUFFERRAM(this);
  555. if (this->bufferram[i].blockpage == blockpage)
  556. found = 1;
  557. else {
  558. /* Check another BufferRAM */
  559. i = ONENAND_NEXT_BUFFERRAM(this);
  560. if (this->bufferram[i].blockpage == blockpage) {
  561. ONENAND_SET_NEXT_BUFFERRAM(this);
  562. found = 1;
  563. }
  564. }
  565. if (found && ONENAND_IS_DDP(this)) {
  566. /* Select DataRAM for DDP */
  567. int block = onenand_block(this, addr);
  568. int value = onenand_bufferram_address(this, block);
  569. this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
  570. }
  571. return found;
  572. }
  573. /**
  574. * onenand_update_bufferram - [GENERIC] Update BufferRAM information
  575. * @param mtd MTD data structure
  576. * @param addr address to update
  577. * @param valid valid flag
  578. *
  579. * Update BufferRAM information
  580. */
  581. static int onenand_update_bufferram(struct mtd_info *mtd, loff_t addr,
  582. int valid)
  583. {
  584. struct onenand_chip *this = mtd->priv;
  585. int blockpage;
  586. unsigned int i;
  587. if (ONENAND_IS_2PLANE(this))
  588. blockpage = onenand_get_2x_blockpage(mtd, addr);
  589. else
  590. blockpage = (int)(addr >> this->page_shift);
  591. /* Invalidate another BufferRAM */
  592. i = ONENAND_NEXT_BUFFERRAM(this);
  593. if (this->bufferram[i].blockpage == blockpage)
  594. this->bufferram[i].blockpage = -1;
  595. /* Update BufferRAM */
  596. i = ONENAND_CURRENT_BUFFERRAM(this);
  597. if (valid)
  598. this->bufferram[i].blockpage = blockpage;
  599. else
  600. this->bufferram[i].blockpage = -1;
  601. return 0;
  602. }
  603. /**
  604. * onenand_invalidate_bufferram - [GENERIC] Invalidate BufferRAM information
  605. * @param mtd MTD data structure
  606. * @param addr start address to invalidate
  607. * @param len length to invalidate
  608. *
  609. * Invalidate BufferRAM information
  610. */
  611. static void onenand_invalidate_bufferram(struct mtd_info *mtd, loff_t addr,
  612. unsigned int len)
  613. {
  614. struct onenand_chip *this = mtd->priv;
  615. int i;
  616. loff_t end_addr = addr + len;
  617. /* Invalidate BufferRAM */
  618. for (i = 0; i < MAX_BUFFERRAM; i++) {
  619. loff_t buf_addr = this->bufferram[i].blockpage << this->page_shift;
  620. if (buf_addr >= addr && buf_addr < end_addr)
  621. this->bufferram[i].blockpage = -1;
  622. }
  623. }
  624. /**
  625. * onenand_get_device - [GENERIC] Get chip for selected access
  626. * @param mtd MTD device structure
  627. * @param new_state the state which is requested
  628. *
  629. * Get the device and lock it for exclusive access
  630. */
  631. static void onenand_get_device(struct mtd_info *mtd, int new_state)
  632. {
  633. /* Do nothing */
  634. }
  635. /**
  636. * onenand_release_device - [GENERIC] release chip
  637. * @param mtd MTD device structure
  638. *
  639. * Deselect, release chip lock and wake up anyone waiting on the device
  640. */
  641. static void onenand_release_device(struct mtd_info *mtd)
  642. {
  643. /* Do nothing */
  644. }
  645. /**
  646. * onenand_transfer_auto_oob - [Internal] oob auto-placement transfer
  647. * @param mtd MTD device structure
  648. * @param buf destination address
  649. * @param column oob offset to read from
  650. * @param thislen oob length to read
  651. */
  652. static int onenand_transfer_auto_oob(struct mtd_info *mtd, uint8_t *buf,
  653. int column, int thislen)
  654. {
  655. struct onenand_chip *this = mtd->priv;
  656. struct nand_oobfree *free;
  657. int readcol = column;
  658. int readend = column + thislen;
  659. int lastgap = 0;
  660. unsigned int i;
  661. uint8_t *oob_buf = this->oob_buf;
  662. free = this->ecclayout->oobfree;
  663. for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES && free->length; i++, free++) {
  664. if (readcol >= lastgap)
  665. readcol += free->offset - lastgap;
  666. if (readend >= lastgap)
  667. readend += free->offset - lastgap;
  668. lastgap = free->offset + free->length;
  669. }
  670. this->read_bufferram(mtd, 0, ONENAND_SPARERAM, oob_buf, 0, mtd->oobsize);
  671. free = this->ecclayout->oobfree;
  672. for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES && free->length; i++, free++) {
  673. int free_end = free->offset + free->length;
  674. if (free->offset < readend && free_end > readcol) {
  675. int st = max_t(int,free->offset,readcol);
  676. int ed = min_t(int,free_end,readend);
  677. int n = ed - st;
  678. memcpy(buf, oob_buf + st, n);
  679. buf += n;
  680. } else if (column == 0)
  681. break;
  682. }
  683. return 0;
  684. }
  685. /**
  686. * onenand_recover_lsb - [Flex-OneNAND] Recover LSB page data
  687. * @param mtd MTD device structure
  688. * @param addr address to recover
  689. * @param status return value from onenand_wait
  690. *
  691. * MLC NAND Flash cell has paired pages - LSB page and MSB page. LSB page has
  692. * lower page address and MSB page has higher page address in paired pages.
  693. * If power off occurs during MSB page program, the paired LSB page data can
  694. * become corrupt. LSB page recovery read is a way to read LSB page though page
  695. * data are corrupted. When uncorrectable error occurs as a result of LSB page
  696. * read after power up, issue LSB page recovery read.
  697. */
  698. static int onenand_recover_lsb(struct mtd_info *mtd, loff_t addr, int status)
  699. {
  700. struct onenand_chip *this = mtd->priv;
  701. int i;
  702. /* Recovery is only for Flex-OneNAND */
  703. if (!FLEXONENAND(this))
  704. return status;
  705. /* check if we failed due to uncorrectable error */
  706. if (status != -EBADMSG && status != ONENAND_BBT_READ_ECC_ERROR)
  707. return status;
  708. /* check if address lies in MLC region */
  709. i = flexonenand_region(mtd, addr);
  710. if (mtd->eraseregions[i].erasesize < (1 << this->erase_shift))
  711. return status;
  712. printk("onenand_recover_lsb:"
  713. "Attempting to recover from uncorrectable read\n");
  714. /* Issue the LSB page recovery command */
  715. this->command(mtd, FLEXONENAND_CMD_RECOVER_LSB, addr, this->writesize);
  716. return this->wait(mtd, FL_READING);
  717. }
  718. /**
  719. * onenand_read_ops_nolock - [OneNAND Interface] OneNAND read main and/or out-of-band
  720. * @param mtd MTD device structure
  721. * @param from offset to read from
  722. * @param ops oob operation description structure
  723. *
  724. * OneNAND read main and/or out-of-band data
  725. */
  726. static int onenand_read_ops_nolock(struct mtd_info *mtd, loff_t from,
  727. struct mtd_oob_ops *ops)
  728. {
  729. struct onenand_chip *this = mtd->priv;
  730. struct mtd_ecc_stats stats;
  731. size_t len = ops->len;
  732. size_t ooblen = ops->ooblen;
  733. u_char *buf = ops->datbuf;
  734. u_char *oobbuf = ops->oobbuf;
  735. int read = 0, column, thislen;
  736. int oobread = 0, oobcolumn, thisooblen, oobsize;
  737. int ret = 0, boundary = 0;
  738. int writesize = this->writesize;
  739. MTDDEBUG(MTD_DEBUG_LEVEL3, "onenand_read_ops_nolock: from = 0x%08x, len = %i\n", (unsigned int) from, (int) len);
  740. if (ops->mode == MTD_OOB_AUTO)
  741. oobsize = this->ecclayout->oobavail;
  742. else
  743. oobsize = mtd->oobsize;
  744. oobcolumn = from & (mtd->oobsize - 1);
  745. /* Do not allow reads past end of device */
  746. if ((from + len) > mtd->size) {
  747. printk(KERN_ERR "onenand_read_ops_nolock: Attempt read beyond end of device\n");
  748. ops->retlen = 0;
  749. ops->oobretlen = 0;
  750. return -EINVAL;
  751. }
  752. stats = mtd->ecc_stats;
  753. /* Read-while-load method */
  754. /* Note: We can't use this feature in MLC */
  755. /* Do first load to bufferRAM */
  756. if (read < len) {
  757. if (!onenand_check_bufferram(mtd, from)) {
  758. this->main_buf = buf;
  759. this->command(mtd, ONENAND_CMD_READ, from, writesize);
  760. ret = this->wait(mtd, FL_READING);
  761. if (unlikely(ret))
  762. ret = onenand_recover_lsb(mtd, from, ret);
  763. onenand_update_bufferram(mtd, from, !ret);
  764. if (ret == -EBADMSG)
  765. ret = 0;
  766. }
  767. }
  768. thislen = min_t(int, writesize, len - read);
  769. column = from & (writesize - 1);
  770. if (column + thislen > writesize)
  771. thislen = writesize - column;
  772. while (!ret) {
  773. /* If there is more to load then start next load */
  774. from += thislen;
  775. if (!ONENAND_IS_MLC(this) && read + thislen < len) {
  776. this->main_buf = buf + thislen;
  777. this->command(mtd, ONENAND_CMD_READ, from, writesize);
  778. /*
  779. * Chip boundary handling in DDP
  780. * Now we issued chip 1 read and pointed chip 1
  781. * bufferam so we have to point chip 0 bufferam.
  782. */
  783. if (ONENAND_IS_DDP(this) &&
  784. unlikely(from == (this->chipsize >> 1))) {
  785. this->write_word(ONENAND_DDP_CHIP0, this->base + ONENAND_REG_START_ADDRESS2);
  786. boundary = 1;
  787. } else
  788. boundary = 0;
  789. ONENAND_SET_PREV_BUFFERRAM(this);
  790. }
  791. /* While load is going, read from last bufferRAM */
  792. this->read_bufferram(mtd, from - thislen, ONENAND_DATARAM, buf, column, thislen);
  793. /* Read oob area if needed */
  794. if (oobbuf) {
  795. thisooblen = oobsize - oobcolumn;
  796. thisooblen = min_t(int, thisooblen, ooblen - oobread);
  797. if (ops->mode == MTD_OOB_AUTO)
  798. onenand_transfer_auto_oob(mtd, oobbuf, oobcolumn, thisooblen);
  799. else
  800. this->read_bufferram(mtd, 0, ONENAND_SPARERAM, oobbuf, oobcolumn, thisooblen);
  801. oobread += thisooblen;
  802. oobbuf += thisooblen;
  803. oobcolumn = 0;
  804. }
  805. if (ONENAND_IS_MLC(this) && (read + thislen < len)) {
  806. this->command(mtd, ONENAND_CMD_READ, from, writesize);
  807. ret = this->wait(mtd, FL_READING);
  808. if (unlikely(ret))
  809. ret = onenand_recover_lsb(mtd, from, ret);
  810. onenand_update_bufferram(mtd, from, !ret);
  811. if (ret == -EBADMSG)
  812. ret = 0;
  813. }
  814. /* See if we are done */
  815. read += thislen;
  816. if (read == len)
  817. break;
  818. /* Set up for next read from bufferRAM */
  819. if (unlikely(boundary))
  820. this->write_word(ONENAND_DDP_CHIP1, this->base + ONENAND_REG_START_ADDRESS2);
  821. if (!ONENAND_IS_MLC(this))
  822. ONENAND_SET_NEXT_BUFFERRAM(this);
  823. buf += thislen;
  824. thislen = min_t(int, writesize, len - read);
  825. column = 0;
  826. if (!ONENAND_IS_MLC(this)) {
  827. /* Now wait for load */
  828. ret = this->wait(mtd, FL_READING);
  829. onenand_update_bufferram(mtd, from, !ret);
  830. if (ret == -EBADMSG)
  831. ret = 0;
  832. }
  833. }
  834. /*
  835. * Return success, if no ECC failures, else -EBADMSG
  836. * fs driver will take care of that, because
  837. * retlen == desired len and result == -EBADMSG
  838. */
  839. ops->retlen = read;
  840. ops->oobretlen = oobread;
  841. if (ret)
  842. return ret;
  843. if (mtd->ecc_stats.failed - stats.failed)
  844. return -EBADMSG;
  845. return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
  846. }
  847. /**
  848. * onenand_read_oob_nolock - [MTD Interface] OneNAND read out-of-band
  849. * @param mtd MTD device structure
  850. * @param from offset to read from
  851. * @param ops oob operation description structure
  852. *
  853. * OneNAND read out-of-band data from the spare area
  854. */
  855. static int onenand_read_oob_nolock(struct mtd_info *mtd, loff_t from,
  856. struct mtd_oob_ops *ops)
  857. {
  858. struct onenand_chip *this = mtd->priv;
  859. struct mtd_ecc_stats stats;
  860. int read = 0, thislen, column, oobsize;
  861. size_t len = ops->ooblen;
  862. mtd_oob_mode_t mode = ops->mode;
  863. u_char *buf = ops->oobbuf;
  864. int ret = 0, readcmd;
  865. from += ops->ooboffs;
  866. MTDDEBUG(MTD_DEBUG_LEVEL3, "onenand_read_oob_nolock: from = 0x%08x, len = %i\n", (unsigned int) from, (int) len);
  867. /* Initialize return length value */
  868. ops->oobretlen = 0;
  869. if (mode == MTD_OOB_AUTO)
  870. oobsize = this->ecclayout->oobavail;
  871. else
  872. oobsize = mtd->oobsize;
  873. column = from & (mtd->oobsize - 1);
  874. if (unlikely(column >= oobsize)) {
  875. printk(KERN_ERR "onenand_read_oob_nolock: Attempted to start read outside oob\n");
  876. return -EINVAL;
  877. }
  878. /* Do not allow reads past end of device */
  879. if (unlikely(from >= mtd->size ||
  880. column + len > ((mtd->size >> this->page_shift) -
  881. (from >> this->page_shift)) * oobsize)) {
  882. printk(KERN_ERR "onenand_read_oob_nolock: Attempted to read beyond end of device\n");
  883. return -EINVAL;
  884. }
  885. stats = mtd->ecc_stats;
  886. readcmd = ONENAND_IS_MLC(this) ? ONENAND_CMD_READ : ONENAND_CMD_READOOB;
  887. while (read < len) {
  888. thislen = oobsize - column;
  889. thislen = min_t(int, thislen, len);
  890. this->spare_buf = buf;
  891. this->command(mtd, readcmd, from, mtd->oobsize);
  892. onenand_update_bufferram(mtd, from, 0);
  893. ret = this->wait(mtd, FL_READING);
  894. if (unlikely(ret))
  895. ret = onenand_recover_lsb(mtd, from, ret);
  896. if (ret && ret != -EBADMSG) {
  897. printk(KERN_ERR "onenand_read_oob_nolock: read failed = 0x%x\n", ret);
  898. break;
  899. }
  900. if (mode == MTD_OOB_AUTO)
  901. onenand_transfer_auto_oob(mtd, buf, column, thislen);
  902. else
  903. this->read_bufferram(mtd, 0, ONENAND_SPARERAM, buf, column, thislen);
  904. read += thislen;
  905. if (read == len)
  906. break;
  907. buf += thislen;
  908. /* Read more? */
  909. if (read < len) {
  910. /* Page size */
  911. from += mtd->writesize;
  912. column = 0;
  913. }
  914. }
  915. ops->oobretlen = read;
  916. if (ret)
  917. return ret;
  918. if (mtd->ecc_stats.failed - stats.failed)
  919. return -EBADMSG;
  920. return 0;
  921. }
  922. /**
  923. * onenand_read - [MTD Interface] MTD compability function for onenand_read_ecc
  924. * @param mtd MTD device structure
  925. * @param from offset to read from
  926. * @param len number of bytes to read
  927. * @param retlen pointer to variable to store the number of read bytes
  928. * @param buf the databuffer to put data
  929. *
  930. * This function simply calls onenand_read_ecc with oob buffer and oobsel = NULL
  931. */
  932. int onenand_read(struct mtd_info *mtd, loff_t from, size_t len,
  933. size_t * retlen, u_char * buf)
  934. {
  935. struct mtd_oob_ops ops = {
  936. .len = len,
  937. .ooblen = 0,
  938. .datbuf = buf,
  939. .oobbuf = NULL,
  940. };
  941. int ret;
  942. onenand_get_device(mtd, FL_READING);
  943. ret = onenand_read_ops_nolock(mtd, from, &ops);
  944. onenand_release_device(mtd);
  945. *retlen = ops.retlen;
  946. return ret;
  947. }
  948. /**
  949. * onenand_read_oob - [MTD Interface] OneNAND read out-of-band
  950. * @param mtd MTD device structure
  951. * @param from offset to read from
  952. * @param ops oob operations description structure
  953. *
  954. * OneNAND main and/or out-of-band
  955. */
  956. int onenand_read_oob(struct mtd_info *mtd, loff_t from,
  957. struct mtd_oob_ops *ops)
  958. {
  959. int ret;
  960. switch (ops->mode) {
  961. case MTD_OOB_PLACE:
  962. case MTD_OOB_AUTO:
  963. break;
  964. case MTD_OOB_RAW:
  965. /* Not implemented yet */
  966. default:
  967. return -EINVAL;
  968. }
  969. onenand_get_device(mtd, FL_READING);
  970. if (ops->datbuf)
  971. ret = onenand_read_ops_nolock(mtd, from, ops);
  972. else
  973. ret = onenand_read_oob_nolock(mtd, from, ops);
  974. onenand_release_device(mtd);
  975. return ret;
  976. }
  977. /**
  978. * onenand_bbt_wait - [DEFAULT] wait until the command is done
  979. * @param mtd MTD device structure
  980. * @param state state to select the max. timeout value
  981. *
  982. * Wait for command done.
  983. */
  984. static int onenand_bbt_wait(struct mtd_info *mtd, int state)
  985. {
  986. struct onenand_chip *this = mtd->priv;
  987. unsigned int flags = ONENAND_INT_MASTER;
  988. unsigned int interrupt;
  989. unsigned int ctrl;
  990. while (1) {
  991. interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
  992. if (interrupt & flags)
  993. break;
  994. }
  995. /* To get correct interrupt status in timeout case */
  996. interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
  997. ctrl = this->read_word(this->base + ONENAND_REG_CTRL_STATUS);
  998. if (interrupt & ONENAND_INT_READ) {
  999. int ecc = onenand_read_ecc(this);
  1000. if (ecc & ONENAND_ECC_2BIT_ALL) {
  1001. printk(KERN_INFO "onenand_bbt_wait: ecc error = 0x%04x"
  1002. ", controller = 0x%04x\n", ecc, ctrl);
  1003. return ONENAND_BBT_READ_ERROR;
  1004. }
  1005. } else {
  1006. printk(KERN_ERR "onenand_bbt_wait: read timeout!"
  1007. "ctrl=0x%04x intr=0x%04x\n", ctrl, interrupt);
  1008. return ONENAND_BBT_READ_FATAL_ERROR;
  1009. }
  1010. /* Initial bad block case: 0x2400 or 0x0400 */
  1011. if (ctrl & ONENAND_CTRL_ERROR) {
  1012. printk(KERN_DEBUG "onenand_bbt_wait: controller error = 0x%04x\n", ctrl);
  1013. return ONENAND_BBT_READ_ERROR;
  1014. }
  1015. return 0;
  1016. }
  1017. /**
  1018. * onenand_bbt_read_oob - [MTD Interface] OneNAND read out-of-band for bbt scan
  1019. * @param mtd MTD device structure
  1020. * @param from offset to read from
  1021. * @param ops oob operation description structure
  1022. *
  1023. * OneNAND read out-of-band data from the spare area for bbt scan
  1024. */
  1025. int onenand_bbt_read_oob(struct mtd_info *mtd, loff_t from,
  1026. struct mtd_oob_ops *ops)
  1027. {
  1028. struct onenand_chip *this = mtd->priv;
  1029. int read = 0, thislen, column;
  1030. int ret = 0, readcmd;
  1031. size_t len = ops->ooblen;
  1032. u_char *buf = ops->oobbuf;
  1033. MTDDEBUG(MTD_DEBUG_LEVEL3, "onenand_bbt_read_oob: from = 0x%08x, len = %zi\n", (unsigned int) from, len);
  1034. readcmd = ONENAND_IS_MLC(this) ? ONENAND_CMD_READ : ONENAND_CMD_READOOB;
  1035. /* Initialize return value */
  1036. ops->oobretlen = 0;
  1037. /* Do not allow reads past end of device */
  1038. if (unlikely((from + len) > mtd->size)) {
  1039. printk(KERN_ERR "onenand_bbt_read_oob: Attempt read beyond end of device\n");
  1040. return ONENAND_BBT_READ_FATAL_ERROR;
  1041. }
  1042. /* Grab the lock and see if the device is available */
  1043. onenand_get_device(mtd, FL_READING);
  1044. column = from & (mtd->oobsize - 1);
  1045. while (read < len) {
  1046. thislen = mtd->oobsize - column;
  1047. thislen = min_t(int, thislen, len);
  1048. this->spare_buf = buf;
  1049. this->command(mtd, readcmd, from, mtd->oobsize);
  1050. onenand_update_bufferram(mtd, from, 0);
  1051. ret = this->bbt_wait(mtd, FL_READING);
  1052. if (unlikely(ret))
  1053. ret = onenand_recover_lsb(mtd, from, ret);
  1054. if (ret)
  1055. break;
  1056. this->read_bufferram(mtd, 0, ONENAND_SPARERAM, buf, column, thislen);
  1057. read += thislen;
  1058. if (read == len)
  1059. break;
  1060. buf += thislen;
  1061. /* Read more? */
  1062. if (read < len) {
  1063. /* Update Page size */
  1064. from += this->writesize;
  1065. column = 0;
  1066. }
  1067. }
  1068. /* Deselect and wake up anyone waiting on the device */
  1069. onenand_release_device(mtd);
  1070. ops->oobretlen = read;
  1071. return ret;
  1072. }
  1073. #ifdef CONFIG_MTD_ONENAND_VERIFY_WRITE
  1074. /**
  1075. * onenand_verify_oob - [GENERIC] verify the oob contents after a write
  1076. * @param mtd MTD device structure
  1077. * @param buf the databuffer to verify
  1078. * @param to offset to read from
  1079. */
  1080. static int onenand_verify_oob(struct mtd_info *mtd, const u_char *buf, loff_t to)
  1081. {
  1082. struct onenand_chip *this = mtd->priv;
  1083. u_char *oob_buf = this->oob_buf;
  1084. int status, i, readcmd;
  1085. readcmd = ONENAND_IS_MLC(this) ? ONENAND_CMD_READ : ONENAND_CMD_READOOB;
  1086. this->command(mtd, readcmd, to, mtd->oobsize);
  1087. onenand_update_bufferram(mtd, to, 0);
  1088. status = this->wait(mtd, FL_READING);
  1089. if (status)
  1090. return status;
  1091. this->read_bufferram(mtd, 0, ONENAND_SPARERAM, oob_buf, 0, mtd->oobsize);
  1092. for (i = 0; i < mtd->oobsize; i++)
  1093. if (buf[i] != 0xFF && buf[i] != oob_buf[i])
  1094. return -EBADMSG;
  1095. return 0;
  1096. }
  1097. /**
  1098. * onenand_verify - [GENERIC] verify the chip contents after a write
  1099. * @param mtd MTD device structure
  1100. * @param buf the databuffer to verify
  1101. * @param addr offset to read from
  1102. * @param len number of bytes to read and compare
  1103. */
  1104. static int onenand_verify(struct mtd_info *mtd, const u_char *buf, loff_t addr, size_t len)
  1105. {
  1106. struct onenand_chip *this = mtd->priv;
  1107. void __iomem *dataram;
  1108. int ret = 0;
  1109. int thislen, column;
  1110. while (len != 0) {
  1111. thislen = min_t(int, this->writesize, len);
  1112. column = addr & (this->writesize - 1);
  1113. if (column + thislen > this->writesize)
  1114. thislen = this->writesize - column;
  1115. this->command(mtd, ONENAND_CMD_READ, addr, this->writesize);
  1116. onenand_update_bufferram(mtd, addr, 0);
  1117. ret = this->wait(mtd, FL_READING);
  1118. if (ret)
  1119. return ret;
  1120. onenand_update_bufferram(mtd, addr, 1);
  1121. dataram = this->base + ONENAND_DATARAM;
  1122. dataram += onenand_bufferram_offset(mtd, ONENAND_DATARAM);
  1123. if (memcmp(buf, dataram + column, thislen))
  1124. return -EBADMSG;
  1125. len -= thislen;
  1126. buf += thislen;
  1127. addr += thislen;
  1128. }
  1129. return 0;
  1130. }
  1131. #else
  1132. #define onenand_verify(...) (0)
  1133. #define onenand_verify_oob(...) (0)
  1134. #endif
  1135. #define NOTALIGNED(x) ((x & (this->subpagesize - 1)) != 0)
  1136. /**
  1137. * onenand_fill_auto_oob - [Internal] oob auto-placement transfer
  1138. * @param mtd MTD device structure
  1139. * @param oob_buf oob buffer
  1140. * @param buf source address
  1141. * @param column oob offset to write to
  1142. * @param thislen oob length to write
  1143. */
  1144. static int onenand_fill_auto_oob(struct mtd_info *mtd, u_char *oob_buf,
  1145. const u_char *buf, int column, int thislen)
  1146. {
  1147. struct onenand_chip *this = mtd->priv;
  1148. struct nand_oobfree *free;
  1149. int writecol = column;
  1150. int writeend = column + thislen;
  1151. int lastgap = 0;
  1152. unsigned int i;
  1153. free = this->ecclayout->oobfree;
  1154. for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES && free->length; i++, free++) {
  1155. if (writecol >= lastgap)
  1156. writecol += free->offset - lastgap;
  1157. if (writeend >= lastgap)
  1158. writeend += free->offset - lastgap;
  1159. lastgap = free->offset + free->length;
  1160. }
  1161. free = this->ecclayout->oobfree;
  1162. for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES && free->length; i++, free++) {
  1163. int free_end = free->offset + free->length;
  1164. if (free->offset < writeend && free_end > writecol) {
  1165. int st = max_t(int,free->offset,writecol);
  1166. int ed = min_t(int,free_end,writeend);
  1167. int n = ed - st;
  1168. memcpy(oob_buf + st, buf, n);
  1169. buf += n;
  1170. } else if (column == 0)
  1171. break;
  1172. }
  1173. return 0;
  1174. }
  1175. /**
  1176. * onenand_write_ops_nolock - [OneNAND Interface] write main and/or out-of-band
  1177. * @param mtd MTD device structure
  1178. * @param to offset to write to
  1179. * @param ops oob operation description structure
  1180. *
  1181. * Write main and/or oob with ECC
  1182. */
  1183. static int onenand_write_ops_nolock(struct mtd_info *mtd, loff_t to,
  1184. struct mtd_oob_ops *ops)
  1185. {
  1186. struct onenand_chip *this = mtd->priv;
  1187. int written = 0, column, thislen, subpage;
  1188. int oobwritten = 0, oobcolumn, thisooblen, oobsize;
  1189. size_t len = ops->len;
  1190. size_t ooblen = ops->ooblen;
  1191. const u_char *buf = ops->datbuf;
  1192. const u_char *oob = ops->oobbuf;
  1193. u_char *oobbuf;
  1194. int ret = 0;
  1195. MTDDEBUG(MTD_DEBUG_LEVEL3, "onenand_write_ops_nolock: to = 0x%08x, len = %i\n", (unsigned int) to, (int) len);
  1196. /* Initialize retlen, in case of early exit */
  1197. ops->retlen = 0;
  1198. ops->oobretlen = 0;
  1199. /* Do not allow writes past end of device */
  1200. if (unlikely((to + len) > mtd->size)) {
  1201. printk(KERN_ERR "onenand_write_ops_nolock: Attempt write to past end of device\n");
  1202. return -EINVAL;
  1203. }
  1204. /* Reject writes, which are not page aligned */
  1205. if (unlikely(NOTALIGNED(to) || NOTALIGNED(len))) {
  1206. printk(KERN_ERR "onenand_write_ops_nolock: Attempt to write not page aligned data\n");
  1207. return -EINVAL;
  1208. }
  1209. if (ops->mode == MTD_OOB_AUTO)
  1210. oobsize = this->ecclayout->oobavail;
  1211. else
  1212. oobsize = mtd->oobsize;
  1213. oobcolumn = to & (mtd->oobsize - 1);
  1214. column = to & (mtd->writesize - 1);
  1215. /* Loop until all data write */
  1216. while (written < len) {
  1217. u_char *wbuf = (u_char *) buf;
  1218. thislen = min_t(int, mtd->writesize - column, len - written);
  1219. thisooblen = min_t(int, oobsize - oobcolumn, ooblen - oobwritten);
  1220. this->command(mtd, ONENAND_CMD_BUFFERRAM, to, thislen);
  1221. /* Partial page write */
  1222. subpage = thislen < mtd->writesize;
  1223. if (subpage) {
  1224. memset(this->page_buf, 0xff, mtd->writesize);
  1225. memcpy(this->page_buf + column, buf, thislen);
  1226. wbuf = this->page_buf;
  1227. }
  1228. this->write_bufferram(mtd, to, ONENAND_DATARAM, wbuf, 0, mtd->writesize);
  1229. if (oob) {
  1230. oobbuf = this->oob_buf;
  1231. /* We send data to spare ram with oobsize
  1232. * * to prevent byte access */
  1233. memset(oobbuf, 0xff, mtd->oobsize);
  1234. if (ops->mode == MTD_OOB_AUTO)
  1235. onenand_fill_auto_oob(mtd, oobbuf, oob, oobcolumn, thisooblen);
  1236. else
  1237. memcpy(oobbuf + oobcolumn, oob, thisooblen);
  1238. oobwritten += thisooblen;
  1239. oob += thisooblen;
  1240. oobcolumn = 0;
  1241. } else
  1242. oobbuf = (u_char *) ffchars;
  1243. this->write_bufferram(mtd, 0, ONENAND_SPARERAM, oobbuf, 0, mtd->oobsize);
  1244. this->command(mtd, ONENAND_CMD_PROG, to, mtd->writesize);
  1245. ret = this->wait(mtd, FL_WRITING);
  1246. /* In partial page write we don't update bufferram */
  1247. onenand_update_bufferram(mtd, to, !ret && !subpage);
  1248. if (ONENAND_IS_2PLANE(this)) {
  1249. ONENAND_SET_BUFFERRAM1(this);
  1250. onenand_update_bufferram(mtd, to + this->writesize, !ret && !subpage);
  1251. }
  1252. if (ret) {
  1253. printk(KERN_ERR "onenand_write_ops_nolock: write filaed %d\n", ret);
  1254. break;
  1255. }
  1256. /* Only check verify write turn on */
  1257. ret = onenand_verify(mtd, buf, to, thislen);
  1258. if (ret) {
  1259. printk(KERN_ERR "onenand_write_ops_nolock: verify failed %d\n", ret);
  1260. break;
  1261. }
  1262. written += thislen;
  1263. if (written == len)
  1264. break;
  1265. column = 0;
  1266. to += thislen;
  1267. buf += thislen;
  1268. }
  1269. ops->retlen = written;
  1270. return ret;
  1271. }
  1272. /**
  1273. * onenand_write_oob_nolock - [Internal] OneNAND write out-of-band
  1274. * @param mtd MTD device structure
  1275. * @param to offset to write to
  1276. * @param len number of bytes to write
  1277. * @param retlen pointer to variable to store the number of written bytes
  1278. * @param buf the data to write
  1279. * @param mode operation mode
  1280. *
  1281. * OneNAND write out-of-band
  1282. */
  1283. static int onenand_write_oob_nolock(struct mtd_info *mtd, loff_t to,
  1284. struct mtd_oob_ops *ops)
  1285. {
  1286. struct onenand_chip *this = mtd->priv;
  1287. int column, ret = 0, oobsize;
  1288. int written = 0, oobcmd;
  1289. u_char *oobbuf;
  1290. size_t len = ops->ooblen;
  1291. const u_char *buf = ops->oobbuf;
  1292. mtd_oob_mode_t mode = ops->mode;
  1293. to += ops->ooboffs;
  1294. MTDDEBUG(MTD_DEBUG_LEVEL3, "onenand_write_oob_nolock: to = 0x%08x, len = %i\n", (unsigned int) to, (int) len);
  1295. /* Initialize retlen, in case of early exit */
  1296. ops->oobretlen = 0;
  1297. if (mode == MTD_OOB_AUTO)
  1298. oobsize = this->ecclayout->oobavail;
  1299. else
  1300. oobsize = mtd->oobsize;
  1301. column = to & (mtd->oobsize - 1);
  1302. if (unlikely(column >= oobsize)) {
  1303. printk(KERN_ERR "onenand_write_oob_nolock: Attempted to start write outside oob\n");
  1304. return -EINVAL;
  1305. }
  1306. /* For compatibility with NAND: Do not allow write past end of page */
  1307. if (unlikely(column + len > oobsize)) {
  1308. printk(KERN_ERR "onenand_write_oob_nolock: "
  1309. "Attempt to write past end of page\n");
  1310. return -EINVAL;
  1311. }
  1312. /* Do not allow reads past end of device */
  1313. if (unlikely(to >= mtd->size ||
  1314. column + len > ((mtd->size >> this->page_shift) -
  1315. (to >> this->page_shift)) * oobsize)) {
  1316. printk(KERN_ERR "onenand_write_oob_nolock: Attempted to write past end of device\n");
  1317. return -EINVAL;
  1318. }
  1319. oobbuf = this->oob_buf;
  1320. oobcmd = ONENAND_IS_MLC(this) ? ONENAND_CMD_PROG : ONENAND_CMD_PROGOOB;
  1321. /* Loop until all data write */
  1322. while (written < len) {
  1323. int thislen = min_t(int, oobsize, len - written);
  1324. this->command(mtd, ONENAND_CMD_BUFFERRAM, to, mtd->oobsize);
  1325. /* We send data to spare ram with oobsize
  1326. * to prevent byte access */
  1327. memset(oobbuf, 0xff, mtd->oobsize);
  1328. if (mode == MTD_OOB_AUTO)
  1329. onenand_fill_auto_oob(mtd, oobbuf, buf, column, thislen);
  1330. else
  1331. memcpy(oobbuf + column, buf, thislen);
  1332. this->write_bufferram(mtd, 0, ONENAND_SPARERAM, oobbuf, 0, mtd->oobsize);
  1333. if (ONENAND_IS_MLC(this)) {
  1334. /* Set main area of DataRAM to 0xff*/
  1335. memset(this->page_buf, 0xff, mtd->writesize);
  1336. this->write_bufferram(mtd, 0, ONENAND_DATARAM,
  1337. this->page_buf, 0, mtd->writesize);
  1338. }
  1339. this->command(mtd, oobcmd, to, mtd->oobsize);
  1340. onenand_update_bufferram(mtd, to, 0);
  1341. if (ONENAND_IS_2PLANE(this)) {
  1342. ONENAND_SET_BUFFERRAM1(this);
  1343. onenand_update_bufferram(mtd, to + this->writesize, 0);
  1344. }
  1345. ret = this->wait(mtd, FL_WRITING);
  1346. if (ret) {
  1347. printk(KERN_ERR "onenand_write_oob_nolock: write failed %d\n", ret);
  1348. break;
  1349. }
  1350. ret = onenand_verify_oob(mtd, oobbuf, to);
  1351. if (ret) {
  1352. printk(KERN_ERR "onenand_write_oob_nolock: verify failed %d\n", ret);
  1353. break;
  1354. }
  1355. written += thislen;
  1356. if (written == len)
  1357. break;
  1358. to += mtd->writesize;
  1359. buf += thislen;
  1360. column = 0;
  1361. }
  1362. ops->oobretlen = written;
  1363. return ret;
  1364. }
  1365. /**
  1366. * onenand_write - [MTD Interface] compability function for onenand_write_ecc
  1367. * @param mtd MTD device structure
  1368. * @param to offset to write to
  1369. * @param len number of bytes to write
  1370. * @param retlen pointer to variable to store the number of written bytes
  1371. * @param buf the data to write
  1372. *
  1373. * Write with ECC
  1374. */
  1375. int onenand_write(struct mtd_info *mtd, loff_t to, size_t len,
  1376. size_t * retlen, const u_char * buf)
  1377. {
  1378. struct mtd_oob_ops ops = {
  1379. .len = len,
  1380. .ooblen = 0,
  1381. .datbuf = (u_char *) buf,
  1382. .oobbuf = NULL,
  1383. };
  1384. int ret;
  1385. onenand_get_device(mtd, FL_WRITING);
  1386. ret = onenand_write_ops_nolock(mtd, to, &ops);
  1387. onenand_release_device(mtd);
  1388. *retlen = ops.retlen;
  1389. return ret;
  1390. }
  1391. /**
  1392. * onenand_write_oob - [MTD Interface] OneNAND write out-of-band
  1393. * @param mtd MTD device structure
  1394. * @param to offset to write to
  1395. * @param ops oob operation description structure
  1396. *
  1397. * OneNAND write main and/or out-of-band
  1398. */
  1399. int onenand_write_oob(struct mtd_info *mtd, loff_t to,
  1400. struct mtd_oob_ops *ops)
  1401. {
  1402. int ret;
  1403. switch (ops->mode) {
  1404. case MTD_OOB_PLACE:
  1405. case MTD_OOB_AUTO:
  1406. break;
  1407. case MTD_OOB_RAW:
  1408. /* Not implemented yet */
  1409. default:
  1410. return -EINVAL;
  1411. }
  1412. onenand_get_device(mtd, FL_WRITING);
  1413. if (ops->datbuf)
  1414. ret = onenand_write_ops_nolock(mtd, to, ops);
  1415. else
  1416. ret = onenand_write_oob_nolock(mtd, to, ops);
  1417. onenand_release_device(mtd);
  1418. return ret;
  1419. }
  1420. /**
  1421. * onenand_block_isbad_nolock - [GENERIC] Check if a block is marked bad
  1422. * @param mtd MTD device structure
  1423. * @param ofs offset from device start
  1424. * @param allowbbt 1, if its allowed to access the bbt area
  1425. *
  1426. * Check, if the block is bad, Either by reading the bad block table or
  1427. * calling of the scan function.
  1428. */
  1429. static int onenand_block_isbad_nolock(struct mtd_info *mtd, loff_t ofs, int allowbbt)
  1430. {
  1431. struct onenand_chip *this = mtd->priv;
  1432. struct bbm_info *bbm = this->bbm;
  1433. /* Return info from the table */
  1434. return bbm->isbad_bbt(mtd, ofs, allowbbt);
  1435. }
  1436. /**
  1437. * onenand_erase - [MTD Interface] erase block(s)
  1438. * @param mtd MTD device structure
  1439. * @param instr erase instruction
  1440. *
  1441. * Erase one ore more blocks
  1442. */
  1443. int onenand_erase(struct mtd_info *mtd, struct erase_info *instr)
  1444. {
  1445. struct onenand_chip *this = mtd->priv;
  1446. unsigned int block_size;
  1447. loff_t addr = instr->addr;
  1448. unsigned int len = instr->len;
  1449. int ret = 0, i;
  1450. struct mtd_erase_region_info *region = NULL;
  1451. unsigned int region_end = 0;
  1452. MTDDEBUG(MTD_DEBUG_LEVEL3, "onenand_erase: start = 0x%08x, len = %i\n",
  1453. (unsigned int) addr, len);
  1454. /* Do not allow erase past end of device */
  1455. if (unlikely((len + addr) > mtd->size)) {
  1456. MTDDEBUG(MTD_DEBUG_LEVEL0, "onenand_erase:"
  1457. "Erase past end of device\n");
  1458. return -EINVAL;
  1459. }
  1460. if (FLEXONENAND(this)) {
  1461. /* Find the eraseregion of this address */
  1462. i = flexonenand_region(mtd, addr);
  1463. region = &mtd->eraseregions[i];
  1464. block_size = region->erasesize;
  1465. region_end = region->offset
  1466. + region->erasesize * region->numblocks;
  1467. /* Start address within region must align on block boundary.
  1468. * Erase region's start offset is always block start address.
  1469. */
  1470. if (unlikely((addr - region->offset) & (block_size - 1))) {
  1471. MTDDEBUG(MTD_DEBUG_LEVEL0, "onenand_erase:"
  1472. " Unaligned address\n");
  1473. return -EINVAL;
  1474. }
  1475. } else {
  1476. block_size = 1 << this->erase_shift;
  1477. /* Start address must align on block boundary */
  1478. if (unlikely(addr & (block_size - 1))) {
  1479. MTDDEBUG(MTD_DEBUG_LEVEL0, "onenand_erase:"
  1480. "Unaligned address\n");
  1481. return -EINVAL;
  1482. }
  1483. }
  1484. /* Length must align on block boundary */
  1485. if (unlikely(len & (block_size - 1))) {
  1486. MTDDEBUG (MTD_DEBUG_LEVEL0,
  1487. "onenand_erase: Length not block aligned\n");
  1488. return -EINVAL;
  1489. }
  1490. instr->fail_addr = 0xffffffff;
  1491. /* Grab the lock and see if the device is available */
  1492. onenand_get_device(mtd, FL_ERASING);
  1493. /* Loop throught the pages */
  1494. instr->state = MTD_ERASING;
  1495. while (len) {
  1496. /* Check if we have a bad block, we do not erase bad blocks */
  1497. if (instr->priv == 0 && onenand_block_isbad_nolock(mtd, addr, 0)) {
  1498. printk(KERN_WARNING "onenand_erase: attempt to erase"
  1499. " a bad block at addr 0x%08x\n",
  1500. (unsigned int) addr);
  1501. instr->state = MTD_ERASE_FAILED;
  1502. goto erase_exit;
  1503. }
  1504. this->command(mtd, ONENAND_CMD_ERASE, addr, block_size);
  1505. onenand_invalidate_bufferram(mtd, addr, block_size);
  1506. ret = this->wait(mtd, FL_ERASING);
  1507. /* Check, if it is write protected */
  1508. if (ret) {
  1509. if (ret == -EPERM)
  1510. MTDDEBUG (MTD_DEBUG_LEVEL0, "onenand_erase: "
  1511. "Device is write protected!!!\n");
  1512. else
  1513. MTDDEBUG (MTD_DEBUG_LEVEL0, "onenand_erase: "
  1514. "Failed erase, block %d\n",
  1515. onenand_block(this, addr));
  1516. instr->state = MTD_ERASE_FAILED;
  1517. instr->fail_addr = addr;
  1518. goto erase_exit;
  1519. }
  1520. len -= block_size;
  1521. addr += block_size;
  1522. if (addr == region_end) {
  1523. if (!len)
  1524. break;
  1525. region++;
  1526. block_size = region->erasesize;
  1527. region_end = region->offset
  1528. + region->erasesize * region->numblocks;
  1529. if (len & (block_size - 1)) {
  1530. /* This has been checked at MTD
  1531. * partitioning level. */
  1532. printk("onenand_erase: Unaligned address\n");
  1533. goto erase_exit;
  1534. }
  1535. }
  1536. }
  1537. instr->state = MTD_ERASE_DONE;
  1538. erase_exit:
  1539. ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
  1540. /* Do call back function */
  1541. if (!ret)
  1542. mtd_erase_callback(instr);
  1543. /* Deselect and wake up anyone waiting on the device */
  1544. onenand_release_device(mtd);
  1545. return ret;
  1546. }
  1547. /**
  1548. * onenand_sync - [MTD Interface] sync
  1549. * @param mtd MTD device structure
  1550. *
  1551. * Sync is actually a wait for chip ready function
  1552. */
  1553. void onenand_sync(struct mtd_info *mtd)
  1554. {
  1555. MTDDEBUG (MTD_DEBUG_LEVEL3, "onenand_sync: called\n");
  1556. /* Grab the lock and see if the device is available */
  1557. onenand_get_device(mtd, FL_SYNCING);
  1558. /* Release it and go back */
  1559. onenand_release_device(mtd);
  1560. }
  1561. /**
  1562. * onenand_block_isbad - [MTD Interface] Check whether the block at the given offset is bad
  1563. * @param mtd MTD device structure
  1564. * @param ofs offset relative to mtd start
  1565. *
  1566. * Check whether the block is bad
  1567. */
  1568. int onenand_block_isbad(struct mtd_info *mtd, loff_t ofs)
  1569. {
  1570. int ret;
  1571. /* Check for invalid offset */
  1572. if (ofs > mtd->size)
  1573. return -EINVAL;
  1574. onenand_get_device(mtd, FL_READING);
  1575. ret = onenand_block_isbad_nolock(mtd,ofs, 0);
  1576. onenand_release_device(mtd);
  1577. return ret;
  1578. }
  1579. /**
  1580. * onenand_default_block_markbad - [DEFAULT] mark a block bad
  1581. * @param mtd MTD device structure
  1582. * @param ofs offset from device start
  1583. *
  1584. * This is the default implementation, which can be overridden by
  1585. * a hardware specific driver.
  1586. */
  1587. static int onenand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
  1588. {
  1589. struct onenand_chip *this = mtd->priv;
  1590. struct bbm_info *bbm = this->bbm;
  1591. u_char buf[2] = {0, 0};
  1592. struct mtd_oob_ops ops = {
  1593. .mode = MTD_OOB_PLACE,
  1594. .ooblen = 2,
  1595. .oobbuf = buf,
  1596. .ooboffs = 0,
  1597. };
  1598. int block;
  1599. /* Get block number */
  1600. block = onenand_block(this, ofs);
  1601. if (bbm->bbt)
  1602. bbm->bbt[block >> 2] |= 0x01 << ((block & 0x03) << 1);
  1603. /* We write two bytes, so we dont have to mess with 16 bit access */
  1604. ofs += mtd->oobsize + (bbm->badblockpos & ~0x01);
  1605. return onenand_write_oob_nolock(mtd, ofs, &ops);
  1606. }
  1607. /**
  1608. * onenand_block_markbad - [MTD Interface] Mark the block at the given offset as bad
  1609. * @param mtd MTD device structure
  1610. * @param ofs offset relative to mtd start
  1611. *
  1612. * Mark the block as bad
  1613. */
  1614. int onenand_block_markbad(struct mtd_info *mtd, loff_t ofs)
  1615. {
  1616. struct onenand_chip *this = mtd->priv;
  1617. int ret;
  1618. ret = onenand_block_isbad(mtd, ofs);
  1619. if (ret) {
  1620. /* If it was bad already, return success and do nothing */
  1621. if (ret > 0)
  1622. return 0;
  1623. return ret;
  1624. }
  1625. ret = this->block_markbad(mtd, ofs);
  1626. return ret;
  1627. }
  1628. /**
  1629. * onenand_do_lock_cmd - [OneNAND Interface] Lock or unlock block(s)
  1630. * @param mtd MTD device structure
  1631. * @param ofs offset relative to mtd start
  1632. * @param len number of bytes to lock or unlock
  1633. * @param cmd lock or unlock command
  1634. *
  1635. * Lock or unlock one or more blocks
  1636. */
  1637. static int onenand_do_lock_cmd(struct mtd_info *mtd, loff_t ofs, size_t len, int cmd)
  1638. {
  1639. struct onenand_chip *this = mtd->priv;
  1640. int start, end, block, value, status;
  1641. int wp_status_mask;
  1642. start = onenand_block(this, ofs);
  1643. end = onenand_block(this, ofs + len);
  1644. if (cmd == ONENAND_CMD_LOCK)
  1645. wp_status_mask = ONENAND_WP_LS;
  1646. else
  1647. wp_status_mask = ONENAND_WP_US;
  1648. /* Continuous lock scheme */
  1649. if (this->options & ONENAND_HAS_CONT_LOCK) {
  1650. /* Set start block address */
  1651. this->write_word(start,
  1652. this->base + ONENAND_REG_START_BLOCK_ADDRESS);
  1653. /* Set end block address */
  1654. this->write_word(end - 1,
  1655. this->base + ONENAND_REG_END_BLOCK_ADDRESS);
  1656. /* Write unlock command */
  1657. this->command(mtd, cmd, 0, 0);
  1658. /* There's no return value */
  1659. this->wait(mtd, FL_UNLOCKING);
  1660. /* Sanity check */
  1661. while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
  1662. & ONENAND_CTRL_ONGO)
  1663. continue;
  1664. /* Check lock status */
  1665. status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
  1666. if (!(status & ONENAND_WP_US))
  1667. printk(KERN_ERR "wp status = 0x%x\n", status);
  1668. return 0;
  1669. }
  1670. /* Block lock scheme */
  1671. for (block = start; block < end; block++) {
  1672. /* Set block address */
  1673. value = onenand_block_address(this, block);
  1674. this->write_word(value, this->base + ONENAND_REG_START_ADDRESS1);
  1675. /* Select DataRAM for DDP */
  1676. value = onenand_bufferram_address(this, block);
  1677. this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
  1678. /* Set start block address */
  1679. this->write_word(block,
  1680. this->base + ONENAND_REG_START_BLOCK_ADDRESS);
  1681. /* Write unlock command */
  1682. this->command(mtd, ONENAND_CMD_UNLOCK, 0, 0);
  1683. /* There's no return value */
  1684. this->wait(mtd, FL_UNLOCKING);
  1685. /* Sanity check */
  1686. while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
  1687. & ONENAND_CTRL_ONGO)
  1688. continue;
  1689. /* Check lock status */
  1690. status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
  1691. if (!(status & ONENAND_WP_US))
  1692. printk(KERN_ERR "block = %d, wp status = 0x%x\n",
  1693. block, status);
  1694. }
  1695. return 0;
  1696. }
  1697. #ifdef ONENAND_LINUX
  1698. /**
  1699. * onenand_lock - [MTD Interface] Lock block(s)
  1700. * @param mtd MTD device structure
  1701. * @param ofs offset relative to mtd start
  1702. * @param len number of bytes to unlock
  1703. *
  1704. * Lock one or more blocks
  1705. */
  1706. static int onenand_lock(struct mtd_info *mtd, loff_t ofs, size_t len)
  1707. {
  1708. int ret;
  1709. onenand_get_device(mtd, FL_LOCKING);
  1710. ret = onenand_do_lock_cmd(mtd, ofs, len, ONENAND_CMD_LOCK);
  1711. onenand_release_device(mtd);
  1712. return ret;
  1713. }
  1714. /**
  1715. * onenand_unlock - [MTD Interface] Unlock block(s)
  1716. * @param mtd MTD device structure
  1717. * @param ofs offset relative to mtd start
  1718. * @param len number of bytes to unlock
  1719. *
  1720. * Unlock one or more blocks
  1721. */
  1722. static int onenand_unlock(struct mtd_info *mtd, loff_t ofs, size_t len)
  1723. {
  1724. int ret;
  1725. onenand_get_device(mtd, FL_LOCKING);
  1726. ret = onenand_do_lock_cmd(mtd, ofs, len, ONENAND_CMD_UNLOCK);
  1727. onenand_release_device(mtd);
  1728. return ret;
  1729. }
  1730. #endif
  1731. /**
  1732. * onenand_check_lock_status - [OneNAND Interface] Check lock status
  1733. * @param this onenand chip data structure
  1734. *
  1735. * Check lock status
  1736. */
  1737. static int onenand_check_lock_status(struct onenand_chip *this)
  1738. {
  1739. unsigned int value, block, status;
  1740. unsigned int end;
  1741. end = this->chipsize >> this->erase_shift;
  1742. for (block = 0; block < end; block++) {
  1743. /* Set block address */
  1744. value = onenand_block_address(this, block);
  1745. this->write_word(value, this->base + ONENAND_REG_START_ADDRESS1);
  1746. /* Select DataRAM for DDP */
  1747. value = onenand_bufferram_address(this, block);
  1748. this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
  1749. /* Set start block address */
  1750. this->write_word(block, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
  1751. /* Check lock status */
  1752. status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
  1753. if (!(status & ONENAND_WP_US)) {
  1754. printk(KERN_ERR "block = %d, wp status = 0x%x\n", block, status);
  1755. return 0;
  1756. }
  1757. }
  1758. return 1;
  1759. }
  1760. /**
  1761. * onenand_unlock_all - [OneNAND Interface] unlock all blocks
  1762. * @param mtd MTD device structure
  1763. *
  1764. * Unlock all blocks
  1765. */
  1766. static void onenand_unlock_all(struct mtd_info *mtd)
  1767. {
  1768. struct onenand_chip *this = mtd->priv;
  1769. loff_t ofs = 0;
  1770. size_t len = mtd->size;
  1771. if (this->options & ONENAND_HAS_UNLOCK_ALL) {
  1772. /* Set start block address */
  1773. this->write_word(0, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
  1774. /* Write unlock command */
  1775. this->command(mtd, ONENAND_CMD_UNLOCK_ALL, 0, 0);
  1776. /* There's no return value */
  1777. this->wait(mtd, FL_LOCKING);
  1778. /* Sanity check */
  1779. while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
  1780. & ONENAND_CTRL_ONGO)
  1781. continue;
  1782. /* Check lock status */
  1783. if (onenand_check_lock_status(this))
  1784. return;
  1785. /* Workaround for all block unlock in DDP */
  1786. if (ONENAND_IS_DDP(this) && !FLEXONENAND(this)) {
  1787. /* All blocks on another chip */
  1788. ofs = this->chipsize >> 1;
  1789. len = this->chipsize >> 1;
  1790. }
  1791. }
  1792. onenand_do_lock_cmd(mtd, ofs, len, ONENAND_CMD_UNLOCK);
  1793. }
  1794. /**
  1795. * onenand_check_features - Check and set OneNAND features
  1796. * @param mtd MTD data structure
  1797. *
  1798. * Check and set OneNAND features
  1799. * - lock scheme
  1800. * - two plane
  1801. */
  1802. static void onenand_check_features(struct mtd_info *mtd)
  1803. {
  1804. struct onenand_chip *this = mtd->priv;
  1805. unsigned int density, process;
  1806. /* Lock scheme depends on density and process */
  1807. density = onenand_get_density(this->device_id);
  1808. process = this->version_id >> ONENAND_VERSION_PROCESS_SHIFT;
  1809. /* Lock scheme */
  1810. switch (density) {
  1811. case ONENAND_DEVICE_DENSITY_4Gb:
  1812. this->options |= ONENAND_HAS_2PLANE;
  1813. case ONENAND_DEVICE_DENSITY_2Gb:
  1814. /* 2Gb DDP don't have 2 plane */
  1815. if (!ONENAND_IS_DDP(this))
  1816. this->options |= ONENAND_HAS_2PLANE;
  1817. this->options |= ONENAND_HAS_UNLOCK_ALL;
  1818. case ONENAND_DEVICE_DENSITY_1Gb:
  1819. /* A-Die has all block unlock */
  1820. if (process)
  1821. this->options |= ONENAND_HAS_UNLOCK_ALL;
  1822. break;
  1823. default:
  1824. /* Some OneNAND has continuous lock scheme */
  1825. if (!process)
  1826. this->options |= ONENAND_HAS_CONT_LOCK;
  1827. break;
  1828. }
  1829. if (ONENAND_IS_MLC(this))
  1830. this->options &= ~ONENAND_HAS_2PLANE;
  1831. if (FLEXONENAND(this)) {
  1832. this->options &= ~ONENAND_HAS_CONT_LOCK;
  1833. this->options |= ONENAND_HAS_UNLOCK_ALL;
  1834. }
  1835. if (this->options & ONENAND_HAS_CONT_LOCK)
  1836. printk(KERN_DEBUG "Lock scheme is Continuous Lock\n");
  1837. if (this->options & ONENAND_HAS_UNLOCK_ALL)
  1838. printk(KERN_DEBUG "Chip support all block unlock\n");
  1839. if (this->options & ONENAND_HAS_2PLANE)
  1840. printk(KERN_DEBUG "Chip has 2 plane\n");
  1841. }
  1842. /**
  1843. * onenand_print_device_info - Print device ID
  1844. * @param device device ID
  1845. *
  1846. * Print device ID
  1847. */
  1848. char *onenand_print_device_info(int device, int version)
  1849. {
  1850. int vcc, demuxed, ddp, density, flexonenand;
  1851. char *dev_info = malloc(80);
  1852. char *p = dev_info;
  1853. vcc = device & ONENAND_DEVICE_VCC_MASK;
  1854. demuxed = device & ONENAND_DEVICE_IS_DEMUX;
  1855. ddp = device & ONENAND_DEVICE_IS_DDP;
  1856. density = onenand_get_density(device);
  1857. flexonenand = device & DEVICE_IS_FLEXONENAND;
  1858. p += sprintf(dev_info, "%s%sOneNAND%s %dMB %sV 16-bit (0x%02x)",
  1859. demuxed ? "" : "Muxed ",
  1860. flexonenand ? "Flex-" : "",
  1861. ddp ? "(DDP)" : "",
  1862. (16 << density), vcc ? "2.65/3.3" : "1.8", device);
  1863. sprintf(p, "\nOneNAND version = 0x%04x", version);
  1864. printk("%s\n", dev_info);
  1865. return dev_info;
  1866. }
  1867. static const struct onenand_manufacturers onenand_manuf_ids[] = {
  1868. {ONENAND_MFR_NUMONYX, "Numonyx"},
  1869. {ONENAND_MFR_SAMSUNG, "Samsung"},
  1870. };
  1871. /**
  1872. * onenand_check_maf - Check manufacturer ID
  1873. * @param manuf manufacturer ID
  1874. *
  1875. * Check manufacturer ID
  1876. */
  1877. static int onenand_check_maf(int manuf)
  1878. {
  1879. int size = ARRAY_SIZE(onenand_manuf_ids);
  1880. char *name;
  1881. int i;
  1882. for (i = 0; i < size; i++)
  1883. if (manuf == onenand_manuf_ids[i].id)
  1884. break;
  1885. if (i < size)
  1886. name = onenand_manuf_ids[i].name;
  1887. else
  1888. name = "Unknown";
  1889. #ifdef ONENAND_DEBUG
  1890. printk(KERN_DEBUG "OneNAND Manufacturer: %s (0x%0x)\n", name, manuf);
  1891. #endif
  1892. return i == size;
  1893. }
  1894. /**
  1895. * flexonenand_get_boundary - Reads the SLC boundary
  1896. * @param onenand_info - onenand info structure
  1897. *
  1898. * Fill up boundary[] field in onenand_chip
  1899. **/
  1900. static int flexonenand_get_boundary(struct mtd_info *mtd)
  1901. {
  1902. struct onenand_chip *this = mtd->priv;
  1903. unsigned int die, bdry;
  1904. int ret, syscfg, locked;
  1905. /* Disable ECC */
  1906. syscfg = this->read_word(this->base + ONENAND_REG_SYS_CFG1);
  1907. this->write_word((syscfg | 0x0100), this->base + ONENAND_REG_SYS_CFG1);
  1908. for (die = 0; die < this->dies; die++) {
  1909. this->command(mtd, FLEXONENAND_CMD_PI_ACCESS, die, 0);
  1910. this->wait(mtd, FL_SYNCING);
  1911. this->command(mtd, FLEXONENAND_CMD_READ_PI, die, 0);
  1912. ret = this->wait(mtd, FL_READING);
  1913. bdry = this->read_word(this->base + ONENAND_DATARAM);
  1914. if ((bdry >> FLEXONENAND_PI_UNLOCK_SHIFT) == 3)
  1915. locked = 0;
  1916. else
  1917. locked = 1;
  1918. this->boundary[die] = bdry & FLEXONENAND_PI_MASK;
  1919. this->command(mtd, ONENAND_CMD_RESET, 0, 0);
  1920. ret = this->wait(mtd, FL_RESETING);
  1921. printk(KERN_INFO "Die %d boundary: %d%s\n", die,
  1922. this->boundary[die], locked ? "(Locked)" : "(Unlocked)");
  1923. }
  1924. /* Enable ECC */
  1925. this->write_word(syscfg, this->base + ONENAND_REG_SYS_CFG1);
  1926. return 0;
  1927. }
  1928. /**
  1929. * flexonenand_get_size - Fill up fields in onenand_chip and mtd_info
  1930. * boundary[], diesize[], mtd->size, mtd->erasesize,
  1931. * mtd->eraseregions
  1932. * @param mtd - MTD device structure
  1933. */
  1934. static void flexonenand_get_size(struct mtd_info *mtd)
  1935. {
  1936. struct onenand_chip *this = mtd->priv;
  1937. int die, i, eraseshift, density;
  1938. int blksperdie, maxbdry;
  1939. loff_t ofs;
  1940. density = onenand_get_density(this->device_id);
  1941. blksperdie = ((loff_t)(16 << density) << 20) >> (this->erase_shift);
  1942. blksperdie >>= ONENAND_IS_DDP(this) ? 1 : 0;
  1943. maxbdry = blksperdie - 1;
  1944. eraseshift = this->erase_shift - 1;
  1945. mtd->numeraseregions = this->dies << 1;
  1946. /* This fills up the device boundary */
  1947. flexonenand_get_boundary(mtd);
  1948. die = 0;
  1949. ofs = 0;
  1950. i = -1;
  1951. for (; die < this->dies; die++) {
  1952. if (!die || this->boundary[die-1] != maxbdry) {
  1953. i++;
  1954. mtd->eraseregions[i].offset = ofs;
  1955. mtd->eraseregions[i].erasesize = 1 << eraseshift;
  1956. mtd->eraseregions[i].numblocks =
  1957. this->boundary[die] + 1;
  1958. ofs += mtd->eraseregions[i].numblocks << eraseshift;
  1959. eraseshift++;
  1960. } else {
  1961. mtd->numeraseregions -= 1;
  1962. mtd->eraseregions[i].numblocks +=
  1963. this->boundary[die] + 1;
  1964. ofs += (this->boundary[die] + 1) << (eraseshift - 1);
  1965. }
  1966. if (this->boundary[die] != maxbdry) {
  1967. i++;
  1968. mtd->eraseregions[i].offset = ofs;
  1969. mtd->eraseregions[i].erasesize = 1 << eraseshift;
  1970. mtd->eraseregions[i].numblocks = maxbdry ^
  1971. this->boundary[die];
  1972. ofs += mtd->eraseregions[i].numblocks << eraseshift;
  1973. eraseshift--;
  1974. } else
  1975. mtd->numeraseregions -= 1;
  1976. }
  1977. /* Expose MLC erase size except when all blocks are SLC */
  1978. mtd->erasesize = 1 << this->erase_shift;
  1979. if (mtd->numeraseregions == 1)
  1980. mtd->erasesize >>= 1;
  1981. printk(KERN_INFO "Device has %d eraseregions\n", mtd->numeraseregions);
  1982. for (i = 0; i < mtd->numeraseregions; i++)
  1983. printk(KERN_INFO "[offset: 0x%08llx, erasesize: 0x%05x,"
  1984. " numblocks: %04u]\n", mtd->eraseregions[i].offset,
  1985. mtd->eraseregions[i].erasesize,
  1986. mtd->eraseregions[i].numblocks);
  1987. for (die = 0, mtd->size = 0; die < this->dies; die++) {
  1988. this->diesize[die] = (loff_t) (blksperdie << this->erase_shift);
  1989. this->diesize[die] -= (loff_t) (this->boundary[die] + 1)
  1990. << (this->erase_shift - 1);
  1991. mtd->size += this->diesize[die];
  1992. }
  1993. }
  1994. /**
  1995. * flexonenand_check_blocks_erased - Check if blocks are erased
  1996. * @param mtd_info - mtd info structure
  1997. * @param start - first erase block to check
  1998. * @param end - last erase block to check
  1999. *
  2000. * Converting an unerased block from MLC to SLC
  2001. * causes byte values to change. Since both data and its ECC
  2002. * have changed, reads on the block give uncorrectable error.
  2003. * This might lead to the block being detected as bad.
  2004. *
  2005. * Avoid this by ensuring that the block to be converted is
  2006. * erased.
  2007. */
  2008. static int flexonenand_check_blocks_erased(struct mtd_info *mtd,
  2009. int start, int end)
  2010. {
  2011. struct onenand_chip *this = mtd->priv;
  2012. int i, ret;
  2013. int block;
  2014. struct mtd_oob_ops ops = {
  2015. .mode = MTD_OOB_PLACE,
  2016. .ooboffs = 0,
  2017. .ooblen = mtd->oobsize,
  2018. .datbuf = NULL,
  2019. .oobbuf = this->oob_buf,
  2020. };
  2021. loff_t addr;
  2022. printk(KERN_DEBUG "Check blocks from %d to %d\n", start, end);
  2023. for (block = start; block <= end; block++) {
  2024. addr = flexonenand_addr(this, block);
  2025. if (onenand_block_isbad_nolock(mtd, addr, 0))
  2026. continue;
  2027. /*
  2028. * Since main area write results in ECC write to spare,
  2029. * it is sufficient to check only ECC bytes for change.
  2030. */
  2031. ret = onenand_read_oob_nolock(mtd, addr, &ops);
  2032. if (ret)
  2033. return ret;
  2034. for (i = 0; i < mtd->oobsize; i++)
  2035. if (this->oob_buf[i] != 0xff)
  2036. break;
  2037. if (i != mtd->oobsize) {
  2038. printk(KERN_WARNING "Block %d not erased.\n", block);
  2039. return 1;
  2040. }
  2041. }
  2042. return 0;
  2043. }
  2044. /**
  2045. * flexonenand_set_boundary - Writes the SLC boundary
  2046. * @param mtd - mtd info structure
  2047. */
  2048. int flexonenand_set_boundary(struct mtd_info *mtd, int die,
  2049. int boundary, int lock)
  2050. {
  2051. struct onenand_chip *this = mtd->priv;
  2052. int ret, density, blksperdie, old, new, thisboundary;
  2053. loff_t addr;
  2054. if (die >= this->dies)
  2055. return -EINVAL;
  2056. if (boundary == this->boundary[die])
  2057. return 0;
  2058. density = onenand_get_density(this->device_id);
  2059. blksperdie = ((16 << density) << 20) >> this->erase_shift;
  2060. blksperdie >>= ONENAND_IS_DDP(this) ? 1 : 0;
  2061. if (boundary >= blksperdie) {
  2062. printk("flexonenand_set_boundary:"
  2063. "Invalid boundary value. "
  2064. "Boundary not changed.\n");
  2065. return -EINVAL;
  2066. }
  2067. /* Check if converting blocks are erased */
  2068. old = this->boundary[die] + (die * this->density_mask);
  2069. new = boundary + (die * this->density_mask);
  2070. ret = flexonenand_check_blocks_erased(mtd, min(old, new)
  2071. + 1, max(old, new));
  2072. if (ret) {
  2073. printk(KERN_ERR "flexonenand_set_boundary: Please erase blocks before boundary change\n");
  2074. return ret;
  2075. }
  2076. this->command(mtd, FLEXONENAND_CMD_PI_ACCESS, die, 0);
  2077. this->wait(mtd, FL_SYNCING);
  2078. /* Check is boundary is locked */
  2079. this->command(mtd, FLEXONENAND_CMD_READ_PI, die, 0);
  2080. ret = this->wait(mtd, FL_READING);
  2081. thisboundary = this->read_word(this->base + ONENAND_DATARAM);
  2082. if ((thisboundary >> FLEXONENAND_PI_UNLOCK_SHIFT) != 3) {
  2083. printk(KERN_ERR "flexonenand_set_boundary: boundary locked\n");
  2084. goto out;
  2085. }
  2086. printk(KERN_INFO "flexonenand_set_boundary: Changing die %d boundary: %d%s\n",
  2087. die, boundary, lock ? "(Locked)" : "(Unlocked)");
  2088. boundary &= FLEXONENAND_PI_MASK;
  2089. boundary |= lock ? 0 : (3 << FLEXONENAND_PI_UNLOCK_SHIFT);
  2090. addr = die ? this->diesize[0] : 0;
  2091. this->command(mtd, ONENAND_CMD_ERASE, addr, 0);
  2092. ret = this->wait(mtd, FL_ERASING);
  2093. if (ret) {
  2094. printk("flexonenand_set_boundary:"
  2095. "Failed PI erase for Die %d\n", die);
  2096. goto out;
  2097. }
  2098. this->write_word(boundary, this->base + ONENAND_DATARAM);
  2099. this->command(mtd, ONENAND_CMD_PROG, addr, 0);
  2100. ret = this->wait(mtd, FL_WRITING);
  2101. if (ret) {
  2102. printk("flexonenand_set_boundary:"
  2103. "Failed PI write for Die %d\n", die);
  2104. goto out;
  2105. }
  2106. this->command(mtd, FLEXONENAND_CMD_PI_UPDATE, die, 0);
  2107. ret = this->wait(mtd, FL_WRITING);
  2108. out:
  2109. this->write_word(ONENAND_CMD_RESET, this->base + ONENAND_REG_COMMAND);
  2110. this->wait(mtd, FL_RESETING);
  2111. if (!ret)
  2112. /* Recalculate device size on boundary change*/
  2113. flexonenand_get_size(mtd);
  2114. return ret;
  2115. }
  2116. /**
  2117. * onenand_probe - [OneNAND Interface] Probe the OneNAND device
  2118. * @param mtd MTD device structure
  2119. *
  2120. * OneNAND detection method:
  2121. * Compare the the values from command with ones from register
  2122. */
  2123. static int onenand_probe(struct mtd_info *mtd)
  2124. {
  2125. struct onenand_chip *this = mtd->priv;
  2126. int bram_maf_id, bram_dev_id, maf_id, dev_id, ver_id;
  2127. int density;
  2128. int syscfg;
  2129. /* Save system configuration 1 */
  2130. syscfg = this->read_word(this->base + ONENAND_REG_SYS_CFG1);
  2131. /* Clear Sync. Burst Read mode to read BootRAM */
  2132. this->write_word((syscfg & ~ONENAND_SYS_CFG1_SYNC_READ), this->base + ONENAND_REG_SYS_CFG1);
  2133. /* Send the command for reading device ID from BootRAM */
  2134. this->write_word(ONENAND_CMD_READID, this->base + ONENAND_BOOTRAM);
  2135. /* Read manufacturer and device IDs from BootRAM */
  2136. bram_maf_id = this->read_word(this->base + ONENAND_BOOTRAM + 0x0);
  2137. bram_dev_id = this->read_word(this->base + ONENAND_BOOTRAM + 0x2);
  2138. /* Reset OneNAND to read default register values */
  2139. this->write_word(ONENAND_CMD_RESET, this->base + ONENAND_BOOTRAM);
  2140. /* Wait reset */
  2141. this->wait(mtd, FL_RESETING);
  2142. /* Restore system configuration 1 */
  2143. this->write_word(syscfg, this->base + ONENAND_REG_SYS_CFG1);
  2144. /* Check manufacturer ID */
  2145. if (onenand_check_maf(bram_maf_id))
  2146. return -ENXIO;
  2147. /* Read manufacturer and device IDs from Register */
  2148. maf_id = this->read_word(this->base + ONENAND_REG_MANUFACTURER_ID);
  2149. dev_id = this->read_word(this->base + ONENAND_REG_DEVICE_ID);
  2150. ver_id = this->read_word(this->base + ONENAND_REG_VERSION_ID);
  2151. this->technology = this->read_word(this->base + ONENAND_REG_TECHNOLOGY);
  2152. /* Check OneNAND device */
  2153. if (maf_id != bram_maf_id || dev_id != bram_dev_id)
  2154. return -ENXIO;
  2155. /* Flash device information */
  2156. mtd->name = onenand_print_device_info(dev_id, ver_id);
  2157. this->device_id = dev_id;
  2158. this->version_id = ver_id;
  2159. density = onenand_get_density(dev_id);
  2160. if (FLEXONENAND(this)) {
  2161. this->dies = ONENAND_IS_DDP(this) ? 2 : 1;
  2162. /* Maximum possible erase regions */
  2163. mtd->numeraseregions = this->dies << 1;
  2164. mtd->eraseregions = malloc(sizeof(struct mtd_erase_region_info)
  2165. * (this->dies << 1));
  2166. if (!mtd->eraseregions)
  2167. return -ENOMEM;
  2168. }
  2169. /*
  2170. * For Flex-OneNAND, chipsize represents maximum possible device size.
  2171. * mtd->size represents the actual device size.
  2172. */
  2173. this->chipsize = (16 << density) << 20;
  2174. /* OneNAND page size & block size */
  2175. /* The data buffer size is equal to page size */
  2176. mtd->writesize =
  2177. this->read_word(this->base + ONENAND_REG_DATA_BUFFER_SIZE);
  2178. /* We use the full BufferRAM */
  2179. if (ONENAND_IS_MLC(this))
  2180. mtd->writesize <<= 1;
  2181. mtd->oobsize = mtd->writesize >> 5;
  2182. /* Pagers per block is always 64 in OneNAND */
  2183. mtd->erasesize = mtd->writesize << 6;
  2184. /*
  2185. * Flex-OneNAND SLC area has 64 pages per block.
  2186. * Flex-OneNAND MLC area has 128 pages per block.
  2187. * Expose MLC erase size to find erase_shift and page_mask.
  2188. */
  2189. if (FLEXONENAND(this))
  2190. mtd->erasesize <<= 1;
  2191. this->erase_shift = ffs(mtd->erasesize) - 1;
  2192. this->page_shift = ffs(mtd->writesize) - 1;
  2193. this->ppb_shift = (this->erase_shift - this->page_shift);
  2194. this->page_mask = (mtd->erasesize / mtd->writesize) - 1;
  2195. /* Set density mask. it is used for DDP */
  2196. if (ONENAND_IS_DDP(this))
  2197. this->density_mask = this->chipsize >> (this->erase_shift + 1);
  2198. /* It's real page size */
  2199. this->writesize = mtd->writesize;
  2200. /* REVIST: Multichip handling */
  2201. if (FLEXONENAND(this))
  2202. flexonenand_get_size(mtd);
  2203. else
  2204. mtd->size = this->chipsize;
  2205. /* Check OneNAND features */
  2206. onenand_check_features(mtd);
  2207. mtd->flags = MTD_CAP_NANDFLASH;
  2208. mtd->erase = onenand_erase;
  2209. mtd->read = onenand_read;
  2210. mtd->write = onenand_write;
  2211. mtd->read_oob = onenand_read_oob;
  2212. mtd->write_oob = onenand_write_oob;
  2213. mtd->sync = onenand_sync;
  2214. mtd->block_isbad = onenand_block_isbad;
  2215. mtd->block_markbad = onenand_block_markbad;
  2216. return 0;
  2217. }
  2218. /**
  2219. * onenand_scan - [OneNAND Interface] Scan for the OneNAND device
  2220. * @param mtd MTD device structure
  2221. * @param maxchips Number of chips to scan for
  2222. *
  2223. * This fills out all the not initialized function pointers
  2224. * with the defaults.
  2225. * The flash ID is read and the mtd/chip structures are
  2226. * filled with the appropriate values.
  2227. */
  2228. int onenand_scan(struct mtd_info *mtd, int maxchips)
  2229. {
  2230. int i;
  2231. struct onenand_chip *this = mtd->priv;
  2232. if (!this->read_word)
  2233. this->read_word = onenand_readw;
  2234. if (!this->write_word)
  2235. this->write_word = onenand_writew;
  2236. if (!this->command)
  2237. this->command = onenand_command;
  2238. if (!this->wait)
  2239. this->wait = onenand_wait;
  2240. if (!this->bbt_wait)
  2241. this->bbt_wait = onenand_bbt_wait;
  2242. if (!this->read_bufferram)
  2243. this->read_bufferram = onenand_read_bufferram;
  2244. if (!this->write_bufferram)
  2245. this->write_bufferram = onenand_write_bufferram;
  2246. if (!this->block_markbad)
  2247. this->block_markbad = onenand_default_block_markbad;
  2248. if (!this->scan_bbt)
  2249. this->scan_bbt = onenand_default_bbt;
  2250. if (onenand_probe(mtd))
  2251. return -ENXIO;
  2252. /* Set Sync. Burst Read after probing */
  2253. if (this->mmcontrol) {
  2254. printk(KERN_INFO "OneNAND Sync. Burst Read support\n");
  2255. this->read_bufferram = onenand_sync_read_bufferram;
  2256. }
  2257. /* Allocate buffers, if necessary */
  2258. if (!this->page_buf) {
  2259. this->page_buf = kzalloc(mtd->writesize, GFP_KERNEL);
  2260. if (!this->page_buf) {
  2261. printk(KERN_ERR "onenand_scan(): Can't allocate page_buf\n");
  2262. return -ENOMEM;
  2263. }
  2264. this->options |= ONENAND_PAGEBUF_ALLOC;
  2265. }
  2266. if (!this->oob_buf) {
  2267. this->oob_buf = kzalloc(mtd->oobsize, GFP_KERNEL);
  2268. if (!this->oob_buf) {
  2269. printk(KERN_ERR "onenand_scan: Can't allocate oob_buf\n");
  2270. if (this->options & ONENAND_PAGEBUF_ALLOC) {
  2271. this->options &= ~ONENAND_PAGEBUF_ALLOC;
  2272. kfree(this->page_buf);
  2273. }
  2274. return -ENOMEM;
  2275. }
  2276. this->options |= ONENAND_OOBBUF_ALLOC;
  2277. }
  2278. this->state = FL_READY;
  2279. /*
  2280. * Allow subpage writes up to oobsize.
  2281. */
  2282. switch (mtd->oobsize) {
  2283. case 128:
  2284. this->ecclayout = &onenand_oob_128;
  2285. mtd->subpage_sft = 0;
  2286. break;
  2287. case 64:
  2288. this->ecclayout = &onenand_oob_64;
  2289. mtd->subpage_sft = 2;
  2290. break;
  2291. case 32:
  2292. this->ecclayout = &onenand_oob_32;
  2293. mtd->subpage_sft = 1;
  2294. break;
  2295. default:
  2296. printk(KERN_WARNING "No OOB scheme defined for oobsize %d\n",
  2297. mtd->oobsize);
  2298. mtd->subpage_sft = 0;
  2299. /* To prevent kernel oops */
  2300. this->ecclayout = &onenand_oob_32;
  2301. break;
  2302. }
  2303. this->subpagesize = mtd->writesize >> mtd->subpage_sft;
  2304. /*
  2305. * The number of bytes available for a client to place data into
  2306. * the out of band area
  2307. */
  2308. this->ecclayout->oobavail = 0;
  2309. for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES &&
  2310. this->ecclayout->oobfree[i].length; i++)
  2311. this->ecclayout->oobavail +=
  2312. this->ecclayout->oobfree[i].length;
  2313. mtd->oobavail = this->ecclayout->oobavail;
  2314. mtd->ecclayout = this->ecclayout;
  2315. /* Unlock whole block */
  2316. onenand_unlock_all(mtd);
  2317. return this->scan_bbt(mtd);
  2318. }
  2319. /**
  2320. * onenand_release - [OneNAND Interface] Free resources held by the OneNAND device
  2321. * @param mtd MTD device structure
  2322. */
  2323. void onenand_release(struct mtd_info *mtd)
  2324. {
  2325. }