onenand_base.c 72 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788
  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/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_4KB_PAGE(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_4KB_PAGE(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. if (ONENAND_IS_2PLANE(this))
  547. blockpage = onenand_get_2x_blockpage(mtd, addr);
  548. else
  549. blockpage = (int) (addr >> this->page_shift);
  550. /* Is there valid data? */
  551. i = ONENAND_CURRENT_BUFFERRAM(this);
  552. if (this->bufferram[i].blockpage == blockpage)
  553. found = 1;
  554. else {
  555. /* Check another BufferRAM */
  556. i = ONENAND_NEXT_BUFFERRAM(this);
  557. if (this->bufferram[i].blockpage == blockpage) {
  558. ONENAND_SET_NEXT_BUFFERRAM(this);
  559. found = 1;
  560. }
  561. }
  562. if (found && ONENAND_IS_DDP(this)) {
  563. /* Select DataRAM for DDP */
  564. int block = onenand_block(this, addr);
  565. int value = onenand_bufferram_address(this, block);
  566. this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
  567. }
  568. return found;
  569. }
  570. /**
  571. * onenand_update_bufferram - [GENERIC] Update BufferRAM information
  572. * @param mtd MTD data structure
  573. * @param addr address to update
  574. * @param valid valid flag
  575. *
  576. * Update BufferRAM information
  577. */
  578. static int onenand_update_bufferram(struct mtd_info *mtd, loff_t addr,
  579. int valid)
  580. {
  581. struct onenand_chip *this = mtd->priv;
  582. int blockpage;
  583. unsigned int i;
  584. if (ONENAND_IS_2PLANE(this))
  585. blockpage = onenand_get_2x_blockpage(mtd, addr);
  586. else
  587. blockpage = (int)(addr >> this->page_shift);
  588. /* Invalidate another BufferRAM */
  589. i = ONENAND_NEXT_BUFFERRAM(this);
  590. if (this->bufferram[i].blockpage == blockpage)
  591. this->bufferram[i].blockpage = -1;
  592. /* Update BufferRAM */
  593. i = ONENAND_CURRENT_BUFFERRAM(this);
  594. if (valid)
  595. this->bufferram[i].blockpage = blockpage;
  596. else
  597. this->bufferram[i].blockpage = -1;
  598. return 0;
  599. }
  600. /**
  601. * onenand_invalidate_bufferram - [GENERIC] Invalidate BufferRAM information
  602. * @param mtd MTD data structure
  603. * @param addr start address to invalidate
  604. * @param len length to invalidate
  605. *
  606. * Invalidate BufferRAM information
  607. */
  608. static void onenand_invalidate_bufferram(struct mtd_info *mtd, loff_t addr,
  609. unsigned int len)
  610. {
  611. struct onenand_chip *this = mtd->priv;
  612. int i;
  613. loff_t end_addr = addr + len;
  614. /* Invalidate BufferRAM */
  615. for (i = 0; i < MAX_BUFFERRAM; i++) {
  616. loff_t buf_addr = this->bufferram[i].blockpage << this->page_shift;
  617. if (buf_addr >= addr && buf_addr < end_addr)
  618. this->bufferram[i].blockpage = -1;
  619. }
  620. }
  621. /**
  622. * onenand_get_device - [GENERIC] Get chip for selected access
  623. * @param mtd MTD device structure
  624. * @param new_state the state which is requested
  625. *
  626. * Get the device and lock it for exclusive access
  627. */
  628. static void onenand_get_device(struct mtd_info *mtd, int new_state)
  629. {
  630. /* Do nothing */
  631. }
  632. /**
  633. * onenand_release_device - [GENERIC] release chip
  634. * @param mtd MTD device structure
  635. *
  636. * Deselect, release chip lock and wake up anyone waiting on the device
  637. */
  638. static void onenand_release_device(struct mtd_info *mtd)
  639. {
  640. /* Do nothing */
  641. }
  642. /**
  643. * onenand_transfer_auto_oob - [Internal] oob auto-placement transfer
  644. * @param mtd MTD device structure
  645. * @param buf destination address
  646. * @param column oob offset to read from
  647. * @param thislen oob length to read
  648. */
  649. static int onenand_transfer_auto_oob(struct mtd_info *mtd, uint8_t *buf,
  650. int column, int thislen)
  651. {
  652. struct onenand_chip *this = mtd->priv;
  653. struct nand_oobfree *free;
  654. int readcol = column;
  655. int readend = column + thislen;
  656. int lastgap = 0;
  657. unsigned int i;
  658. uint8_t *oob_buf = this->oob_buf;
  659. free = this->ecclayout->oobfree;
  660. for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES && free->length; i++, free++) {
  661. if (readcol >= lastgap)
  662. readcol += free->offset - lastgap;
  663. if (readend >= lastgap)
  664. readend += free->offset - lastgap;
  665. lastgap = free->offset + free->length;
  666. }
  667. this->read_bufferram(mtd, 0, ONENAND_SPARERAM, oob_buf, 0, mtd->oobsize);
  668. free = this->ecclayout->oobfree;
  669. for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES && free->length; i++, free++) {
  670. int free_end = free->offset + free->length;
  671. if (free->offset < readend && free_end > readcol) {
  672. int st = max_t(int,free->offset,readcol);
  673. int ed = min_t(int,free_end,readend);
  674. int n = ed - st;
  675. memcpy(buf, oob_buf + st, n);
  676. buf += n;
  677. } else if (column == 0)
  678. break;
  679. }
  680. return 0;
  681. }
  682. /**
  683. * onenand_recover_lsb - [Flex-OneNAND] Recover LSB page data
  684. * @param mtd MTD device structure
  685. * @param addr address to recover
  686. * @param status return value from onenand_wait
  687. *
  688. * MLC NAND Flash cell has paired pages - LSB page and MSB page. LSB page has
  689. * lower page address and MSB page has higher page address in paired pages.
  690. * If power off occurs during MSB page program, the paired LSB page data can
  691. * become corrupt. LSB page recovery read is a way to read LSB page though page
  692. * data are corrupted. When uncorrectable error occurs as a result of LSB page
  693. * read after power up, issue LSB page recovery read.
  694. */
  695. static int onenand_recover_lsb(struct mtd_info *mtd, loff_t addr, int status)
  696. {
  697. struct onenand_chip *this = mtd->priv;
  698. int i;
  699. /* Recovery is only for Flex-OneNAND */
  700. if (!FLEXONENAND(this))
  701. return status;
  702. /* check if we failed due to uncorrectable error */
  703. if (status != -EBADMSG && status != ONENAND_BBT_READ_ECC_ERROR)
  704. return status;
  705. /* check if address lies in MLC region */
  706. i = flexonenand_region(mtd, addr);
  707. if (mtd->eraseregions[i].erasesize < (1 << this->erase_shift))
  708. return status;
  709. printk("onenand_recover_lsb:"
  710. "Attempting to recover from uncorrectable read\n");
  711. /* Issue the LSB page recovery command */
  712. this->command(mtd, FLEXONENAND_CMD_RECOVER_LSB, addr, this->writesize);
  713. return this->wait(mtd, FL_READING);
  714. }
  715. /**
  716. * onenand_read_ops_nolock - [OneNAND Interface] OneNAND read main and/or out-of-band
  717. * @param mtd MTD device structure
  718. * @param from offset to read from
  719. * @param ops oob operation description structure
  720. *
  721. * OneNAND read main and/or out-of-band data
  722. */
  723. static int onenand_read_ops_nolock(struct mtd_info *mtd, loff_t from,
  724. struct mtd_oob_ops *ops)
  725. {
  726. struct onenand_chip *this = mtd->priv;
  727. struct mtd_ecc_stats stats;
  728. size_t len = ops->len;
  729. size_t ooblen = ops->ooblen;
  730. u_char *buf = ops->datbuf;
  731. u_char *oobbuf = ops->oobbuf;
  732. int read = 0, column, thislen;
  733. int oobread = 0, oobcolumn, thisooblen, oobsize;
  734. int ret = 0, boundary = 0;
  735. int writesize = this->writesize;
  736. MTDDEBUG(MTD_DEBUG_LEVEL3, "onenand_read_ops_nolock: from = 0x%08x, len = %i\n", (unsigned int) from, (int) len);
  737. if (ops->mode == MTD_OOB_AUTO)
  738. oobsize = this->ecclayout->oobavail;
  739. else
  740. oobsize = mtd->oobsize;
  741. oobcolumn = from & (mtd->oobsize - 1);
  742. /* Do not allow reads past end of device */
  743. if ((from + len) > mtd->size) {
  744. printk(KERN_ERR "onenand_read_ops_nolock: Attempt read beyond end of device\n");
  745. ops->retlen = 0;
  746. ops->oobretlen = 0;
  747. return -EINVAL;
  748. }
  749. stats = mtd->ecc_stats;
  750. /* Read-while-load method */
  751. /* Note: We can't use this feature in MLC */
  752. /* Do first load to bufferRAM */
  753. if (read < len) {
  754. if (!onenand_check_bufferram(mtd, from)) {
  755. this->main_buf = buf;
  756. this->command(mtd, ONENAND_CMD_READ, from, writesize);
  757. ret = this->wait(mtd, FL_READING);
  758. if (unlikely(ret))
  759. ret = onenand_recover_lsb(mtd, from, ret);
  760. onenand_update_bufferram(mtd, from, !ret);
  761. if (ret == -EBADMSG)
  762. ret = 0;
  763. }
  764. }
  765. thislen = min_t(int, writesize, len - read);
  766. column = from & (writesize - 1);
  767. if (column + thislen > writesize)
  768. thislen = writesize - column;
  769. while (!ret) {
  770. /* If there is more to load then start next load */
  771. from += thislen;
  772. if (!ONENAND_IS_4KB_PAGE(this) && read + thislen < len) {
  773. this->main_buf = buf + thislen;
  774. this->command(mtd, ONENAND_CMD_READ, from, writesize);
  775. /*
  776. * Chip boundary handling in DDP
  777. * Now we issued chip 1 read and pointed chip 1
  778. * bufferam so we have to point chip 0 bufferam.
  779. */
  780. if (ONENAND_IS_DDP(this) &&
  781. unlikely(from == (this->chipsize >> 1))) {
  782. this->write_word(ONENAND_DDP_CHIP0, this->base + ONENAND_REG_START_ADDRESS2);
  783. boundary = 1;
  784. } else
  785. boundary = 0;
  786. ONENAND_SET_PREV_BUFFERRAM(this);
  787. }
  788. /* While load is going, read from last bufferRAM */
  789. this->read_bufferram(mtd, from - thislen, ONENAND_DATARAM, buf, column, thislen);
  790. /* Read oob area if needed */
  791. if (oobbuf) {
  792. thisooblen = oobsize - oobcolumn;
  793. thisooblen = min_t(int, thisooblen, ooblen - oobread);
  794. if (ops->mode == MTD_OOB_AUTO)
  795. onenand_transfer_auto_oob(mtd, oobbuf, oobcolumn, thisooblen);
  796. else
  797. this->read_bufferram(mtd, 0, ONENAND_SPARERAM, oobbuf, oobcolumn, thisooblen);
  798. oobread += thisooblen;
  799. oobbuf += thisooblen;
  800. oobcolumn = 0;
  801. }
  802. if (ONENAND_IS_4KB_PAGE(this) && (read + thislen < len)) {
  803. this->command(mtd, ONENAND_CMD_READ, from, writesize);
  804. ret = this->wait(mtd, FL_READING);
  805. if (unlikely(ret))
  806. ret = onenand_recover_lsb(mtd, from, ret);
  807. onenand_update_bufferram(mtd, from, !ret);
  808. if (ret == -EBADMSG)
  809. ret = 0;
  810. }
  811. /* See if we are done */
  812. read += thislen;
  813. if (read == len)
  814. break;
  815. /* Set up for next read from bufferRAM */
  816. if (unlikely(boundary))
  817. this->write_word(ONENAND_DDP_CHIP1, this->base + ONENAND_REG_START_ADDRESS2);
  818. if (!ONENAND_IS_4KB_PAGE(this))
  819. ONENAND_SET_NEXT_BUFFERRAM(this);
  820. buf += thislen;
  821. thislen = min_t(int, writesize, len - read);
  822. column = 0;
  823. if (!ONENAND_IS_4KB_PAGE(this)) {
  824. /* Now wait for load */
  825. ret = this->wait(mtd, FL_READING);
  826. onenand_update_bufferram(mtd, from, !ret);
  827. if (ret == -EBADMSG)
  828. ret = 0;
  829. }
  830. }
  831. /*
  832. * Return success, if no ECC failures, else -EBADMSG
  833. * fs driver will take care of that, because
  834. * retlen == desired len and result == -EBADMSG
  835. */
  836. ops->retlen = read;
  837. ops->oobretlen = oobread;
  838. if (ret)
  839. return ret;
  840. if (mtd->ecc_stats.failed - stats.failed)
  841. return -EBADMSG;
  842. return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
  843. }
  844. /**
  845. * onenand_read_oob_nolock - [MTD Interface] OneNAND read out-of-band
  846. * @param mtd MTD device structure
  847. * @param from offset to read from
  848. * @param ops oob operation description structure
  849. *
  850. * OneNAND read out-of-band data from the spare area
  851. */
  852. static int onenand_read_oob_nolock(struct mtd_info *mtd, loff_t from,
  853. struct mtd_oob_ops *ops)
  854. {
  855. struct onenand_chip *this = mtd->priv;
  856. struct mtd_ecc_stats stats;
  857. int read = 0, thislen, column, oobsize;
  858. size_t len = ops->ooblen;
  859. mtd_oob_mode_t mode = ops->mode;
  860. u_char *buf = ops->oobbuf;
  861. int ret = 0, readcmd;
  862. from += ops->ooboffs;
  863. MTDDEBUG(MTD_DEBUG_LEVEL3, "onenand_read_oob_nolock: from = 0x%08x, len = %i\n", (unsigned int) from, (int) len);
  864. /* Initialize return length value */
  865. ops->oobretlen = 0;
  866. if (mode == MTD_OOB_AUTO)
  867. oobsize = this->ecclayout->oobavail;
  868. else
  869. oobsize = mtd->oobsize;
  870. column = from & (mtd->oobsize - 1);
  871. if (unlikely(column >= oobsize)) {
  872. printk(KERN_ERR "onenand_read_oob_nolock: Attempted to start read outside oob\n");
  873. return -EINVAL;
  874. }
  875. /* Do not allow reads past end of device */
  876. if (unlikely(from >= mtd->size ||
  877. column + len > ((mtd->size >> this->page_shift) -
  878. (from >> this->page_shift)) * oobsize)) {
  879. printk(KERN_ERR "onenand_read_oob_nolock: Attempted to read beyond end of device\n");
  880. return -EINVAL;
  881. }
  882. stats = mtd->ecc_stats;
  883. readcmd = ONENAND_IS_4KB_PAGE(this) ?
  884. ONENAND_CMD_READ : ONENAND_CMD_READOOB;
  885. while (read < len) {
  886. thislen = oobsize - column;
  887. thislen = min_t(int, thislen, len);
  888. this->spare_buf = buf;
  889. this->command(mtd, readcmd, from, mtd->oobsize);
  890. onenand_update_bufferram(mtd, from, 0);
  891. ret = this->wait(mtd, FL_READING);
  892. if (unlikely(ret))
  893. ret = onenand_recover_lsb(mtd, from, ret);
  894. if (ret && ret != -EBADMSG) {
  895. printk(KERN_ERR "onenand_read_oob_nolock: read failed = 0x%x\n", ret);
  896. break;
  897. }
  898. if (mode == MTD_OOB_AUTO)
  899. onenand_transfer_auto_oob(mtd, buf, column, thislen);
  900. else
  901. this->read_bufferram(mtd, 0, ONENAND_SPARERAM, buf, column, thislen);
  902. read += thislen;
  903. if (read == len)
  904. break;
  905. buf += thislen;
  906. /* Read more? */
  907. if (read < len) {
  908. /* Page size */
  909. from += mtd->writesize;
  910. column = 0;
  911. }
  912. }
  913. ops->oobretlen = read;
  914. if (ret)
  915. return ret;
  916. if (mtd->ecc_stats.failed - stats.failed)
  917. return -EBADMSG;
  918. return 0;
  919. }
  920. /**
  921. * onenand_read - [MTD Interface] MTD compability function for onenand_read_ecc
  922. * @param mtd MTD device structure
  923. * @param from offset to read from
  924. * @param len number of bytes to read
  925. * @param retlen pointer to variable to store the number of read bytes
  926. * @param buf the databuffer to put data
  927. *
  928. * This function simply calls onenand_read_ecc with oob buffer and oobsel = NULL
  929. */
  930. int onenand_read(struct mtd_info *mtd, loff_t from, size_t len,
  931. size_t * retlen, u_char * buf)
  932. {
  933. struct mtd_oob_ops ops = {
  934. .len = len,
  935. .ooblen = 0,
  936. .datbuf = buf,
  937. .oobbuf = NULL,
  938. };
  939. int ret;
  940. onenand_get_device(mtd, FL_READING);
  941. ret = onenand_read_ops_nolock(mtd, from, &ops);
  942. onenand_release_device(mtd);
  943. *retlen = ops.retlen;
  944. return ret;
  945. }
  946. /**
  947. * onenand_read_oob - [MTD Interface] OneNAND read out-of-band
  948. * @param mtd MTD device structure
  949. * @param from offset to read from
  950. * @param ops oob operations description structure
  951. *
  952. * OneNAND main and/or out-of-band
  953. */
  954. int onenand_read_oob(struct mtd_info *mtd, loff_t from,
  955. struct mtd_oob_ops *ops)
  956. {
  957. int ret;
  958. switch (ops->mode) {
  959. case MTD_OOB_PLACE:
  960. case MTD_OOB_AUTO:
  961. break;
  962. case MTD_OOB_RAW:
  963. /* Not implemented yet */
  964. default:
  965. return -EINVAL;
  966. }
  967. onenand_get_device(mtd, FL_READING);
  968. if (ops->datbuf)
  969. ret = onenand_read_ops_nolock(mtd, from, ops);
  970. else
  971. ret = onenand_read_oob_nolock(mtd, from, ops);
  972. onenand_release_device(mtd);
  973. return ret;
  974. }
  975. /**
  976. * onenand_bbt_wait - [DEFAULT] wait until the command is done
  977. * @param mtd MTD device structure
  978. * @param state state to select the max. timeout value
  979. *
  980. * Wait for command done.
  981. */
  982. static int onenand_bbt_wait(struct mtd_info *mtd, int state)
  983. {
  984. struct onenand_chip *this = mtd->priv;
  985. unsigned int flags = ONENAND_INT_MASTER;
  986. unsigned int interrupt;
  987. unsigned int ctrl;
  988. while (1) {
  989. interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
  990. if (interrupt & flags)
  991. break;
  992. }
  993. /* To get correct interrupt status in timeout case */
  994. interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
  995. ctrl = this->read_word(this->base + ONENAND_REG_CTRL_STATUS);
  996. if (interrupt & ONENAND_INT_READ) {
  997. int ecc = onenand_read_ecc(this);
  998. if (ecc & ONENAND_ECC_2BIT_ALL) {
  999. printk(KERN_INFO "onenand_bbt_wait: ecc error = 0x%04x"
  1000. ", controller = 0x%04x\n", ecc, ctrl);
  1001. return ONENAND_BBT_READ_ERROR;
  1002. }
  1003. } else {
  1004. printk(KERN_ERR "onenand_bbt_wait: read timeout!"
  1005. "ctrl=0x%04x intr=0x%04x\n", ctrl, interrupt);
  1006. return ONENAND_BBT_READ_FATAL_ERROR;
  1007. }
  1008. /* Initial bad block case: 0x2400 or 0x0400 */
  1009. if (ctrl & ONENAND_CTRL_ERROR) {
  1010. printk(KERN_DEBUG "onenand_bbt_wait: controller error = 0x%04x\n", ctrl);
  1011. return ONENAND_BBT_READ_ERROR;
  1012. }
  1013. return 0;
  1014. }
  1015. /**
  1016. * onenand_bbt_read_oob - [MTD Interface] OneNAND read out-of-band for bbt scan
  1017. * @param mtd MTD device structure
  1018. * @param from offset to read from
  1019. * @param ops oob operation description structure
  1020. *
  1021. * OneNAND read out-of-band data from the spare area for bbt scan
  1022. */
  1023. int onenand_bbt_read_oob(struct mtd_info *mtd, loff_t from,
  1024. struct mtd_oob_ops *ops)
  1025. {
  1026. struct onenand_chip *this = mtd->priv;
  1027. int read = 0, thislen, column;
  1028. int ret = 0, readcmd;
  1029. size_t len = ops->ooblen;
  1030. u_char *buf = ops->oobbuf;
  1031. MTDDEBUG(MTD_DEBUG_LEVEL3, "onenand_bbt_read_oob: from = 0x%08x, len = %zi\n", (unsigned int) from, len);
  1032. readcmd = ONENAND_IS_4KB_PAGE(this) ?
  1033. ONENAND_CMD_READ : ONENAND_CMD_READOOB;
  1034. /* Initialize return value */
  1035. ops->oobretlen = 0;
  1036. /* Do not allow reads past end of device */
  1037. if (unlikely((from + len) > mtd->size)) {
  1038. printk(KERN_ERR "onenand_bbt_read_oob: Attempt read beyond end of device\n");
  1039. return ONENAND_BBT_READ_FATAL_ERROR;
  1040. }
  1041. /* Grab the lock and see if the device is available */
  1042. onenand_get_device(mtd, FL_READING);
  1043. column = from & (mtd->oobsize - 1);
  1044. while (read < len) {
  1045. thislen = mtd->oobsize - column;
  1046. thislen = min_t(int, thislen, len);
  1047. this->spare_buf = buf;
  1048. this->command(mtd, readcmd, from, mtd->oobsize);
  1049. onenand_update_bufferram(mtd, from, 0);
  1050. ret = this->bbt_wait(mtd, FL_READING);
  1051. if (unlikely(ret))
  1052. ret = onenand_recover_lsb(mtd, from, ret);
  1053. if (ret)
  1054. break;
  1055. this->read_bufferram(mtd, 0, ONENAND_SPARERAM, buf, column, thislen);
  1056. read += thislen;
  1057. if (read == len)
  1058. break;
  1059. buf += thislen;
  1060. /* Read more? */
  1061. if (read < len) {
  1062. /* Update Page size */
  1063. from += this->writesize;
  1064. column = 0;
  1065. }
  1066. }
  1067. /* Deselect and wake up anyone waiting on the device */
  1068. onenand_release_device(mtd);
  1069. ops->oobretlen = read;
  1070. return ret;
  1071. }
  1072. #ifdef CONFIG_MTD_ONENAND_VERIFY_WRITE
  1073. /**
  1074. * onenand_verify_oob - [GENERIC] verify the oob contents after a write
  1075. * @param mtd MTD device structure
  1076. * @param buf the databuffer to verify
  1077. * @param to offset to read from
  1078. */
  1079. static int onenand_verify_oob(struct mtd_info *mtd, const u_char *buf, loff_t to)
  1080. {
  1081. struct onenand_chip *this = mtd->priv;
  1082. u_char *oob_buf = this->oob_buf;
  1083. int status, i, readcmd;
  1084. readcmd = ONENAND_IS_4KB_PAGE(this) ?
  1085. 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_4KB_PAGE(this) ?
  1321. ONENAND_CMD_PROG : ONENAND_CMD_PROGOOB;
  1322. /* Loop until all data write */
  1323. while (written < len) {
  1324. int thislen = min_t(int, oobsize, len - written);
  1325. this->command(mtd, ONENAND_CMD_BUFFERRAM, to, mtd->oobsize);
  1326. /* We send data to spare ram with oobsize
  1327. * to prevent byte access */
  1328. memset(oobbuf, 0xff, mtd->oobsize);
  1329. if (mode == MTD_OOB_AUTO)
  1330. onenand_fill_auto_oob(mtd, oobbuf, buf, column, thislen);
  1331. else
  1332. memcpy(oobbuf + column, buf, thislen);
  1333. this->write_bufferram(mtd, 0, ONENAND_SPARERAM, oobbuf, 0, mtd->oobsize);
  1334. if (ONENAND_IS_4KB_PAGE(this)) {
  1335. /* Set main area of DataRAM to 0xff*/
  1336. memset(this->page_buf, 0xff, mtd->writesize);
  1337. this->write_bufferram(mtd, 0, ONENAND_DATARAM,
  1338. this->page_buf, 0, mtd->writesize);
  1339. }
  1340. this->command(mtd, oobcmd, to, mtd->oobsize);
  1341. onenand_update_bufferram(mtd, to, 0);
  1342. if (ONENAND_IS_2PLANE(this)) {
  1343. ONENAND_SET_BUFFERRAM1(this);
  1344. onenand_update_bufferram(mtd, to + this->writesize, 0);
  1345. }
  1346. ret = this->wait(mtd, FL_WRITING);
  1347. if (ret) {
  1348. printk(KERN_ERR "onenand_write_oob_nolock: write failed %d\n", ret);
  1349. break;
  1350. }
  1351. ret = onenand_verify_oob(mtd, oobbuf, to);
  1352. if (ret) {
  1353. printk(KERN_ERR "onenand_write_oob_nolock: verify failed %d\n", ret);
  1354. break;
  1355. }
  1356. written += thislen;
  1357. if (written == len)
  1358. break;
  1359. to += mtd->writesize;
  1360. buf += thislen;
  1361. column = 0;
  1362. }
  1363. ops->oobretlen = written;
  1364. return ret;
  1365. }
  1366. /**
  1367. * onenand_write - [MTD Interface] compability function for onenand_write_ecc
  1368. * @param mtd MTD device structure
  1369. * @param to offset to write to
  1370. * @param len number of bytes to write
  1371. * @param retlen pointer to variable to store the number of written bytes
  1372. * @param buf the data to write
  1373. *
  1374. * Write with ECC
  1375. */
  1376. int onenand_write(struct mtd_info *mtd, loff_t to, size_t len,
  1377. size_t * retlen, const u_char * buf)
  1378. {
  1379. struct mtd_oob_ops ops = {
  1380. .len = len,
  1381. .ooblen = 0,
  1382. .datbuf = (u_char *) buf,
  1383. .oobbuf = NULL,
  1384. };
  1385. int ret;
  1386. onenand_get_device(mtd, FL_WRITING);
  1387. ret = onenand_write_ops_nolock(mtd, to, &ops);
  1388. onenand_release_device(mtd);
  1389. *retlen = ops.retlen;
  1390. return ret;
  1391. }
  1392. /**
  1393. * onenand_write_oob - [MTD Interface] OneNAND write out-of-band
  1394. * @param mtd MTD device structure
  1395. * @param to offset to write to
  1396. * @param ops oob operation description structure
  1397. *
  1398. * OneNAND write main and/or out-of-band
  1399. */
  1400. int onenand_write_oob(struct mtd_info *mtd, loff_t to,
  1401. struct mtd_oob_ops *ops)
  1402. {
  1403. int ret;
  1404. switch (ops->mode) {
  1405. case MTD_OOB_PLACE:
  1406. case MTD_OOB_AUTO:
  1407. break;
  1408. case MTD_OOB_RAW:
  1409. /* Not implemented yet */
  1410. default:
  1411. return -EINVAL;
  1412. }
  1413. onenand_get_device(mtd, FL_WRITING);
  1414. if (ops->datbuf)
  1415. ret = onenand_write_ops_nolock(mtd, to, ops);
  1416. else
  1417. ret = onenand_write_oob_nolock(mtd, to, ops);
  1418. onenand_release_device(mtd);
  1419. return ret;
  1420. }
  1421. /**
  1422. * onenand_block_isbad_nolock - [GENERIC] Check if a block is marked bad
  1423. * @param mtd MTD device structure
  1424. * @param ofs offset from device start
  1425. * @param allowbbt 1, if its allowed to access the bbt area
  1426. *
  1427. * Check, if the block is bad, Either by reading the bad block table or
  1428. * calling of the scan function.
  1429. */
  1430. static int onenand_block_isbad_nolock(struct mtd_info *mtd, loff_t ofs, int allowbbt)
  1431. {
  1432. struct onenand_chip *this = mtd->priv;
  1433. struct bbm_info *bbm = this->bbm;
  1434. /* Return info from the table */
  1435. return bbm->isbad_bbt(mtd, ofs, allowbbt);
  1436. }
  1437. /**
  1438. * onenand_erase - [MTD Interface] erase block(s)
  1439. * @param mtd MTD device structure
  1440. * @param instr erase instruction
  1441. *
  1442. * Erase one ore more blocks
  1443. */
  1444. int onenand_erase(struct mtd_info *mtd, struct erase_info *instr)
  1445. {
  1446. struct onenand_chip *this = mtd->priv;
  1447. unsigned int block_size;
  1448. loff_t addr = instr->addr;
  1449. unsigned int len = instr->len;
  1450. int ret = 0, i;
  1451. struct mtd_erase_region_info *region = NULL;
  1452. unsigned int region_end = 0;
  1453. MTDDEBUG(MTD_DEBUG_LEVEL3, "onenand_erase: start = 0x%08x, len = %i\n",
  1454. (unsigned int) addr, len);
  1455. /* Do not allow erase past end of device */
  1456. if (unlikely((len + addr) > mtd->size)) {
  1457. MTDDEBUG(MTD_DEBUG_LEVEL0, "onenand_erase:"
  1458. "Erase past end of device\n");
  1459. return -EINVAL;
  1460. }
  1461. if (FLEXONENAND(this)) {
  1462. /* Find the eraseregion of this address */
  1463. i = flexonenand_region(mtd, addr);
  1464. region = &mtd->eraseregions[i];
  1465. block_size = region->erasesize;
  1466. region_end = region->offset
  1467. + region->erasesize * region->numblocks;
  1468. /* Start address within region must align on block boundary.
  1469. * Erase region's start offset is always block start address.
  1470. */
  1471. if (unlikely((addr - region->offset) & (block_size - 1))) {
  1472. MTDDEBUG(MTD_DEBUG_LEVEL0, "onenand_erase:"
  1473. " Unaligned address\n");
  1474. return -EINVAL;
  1475. }
  1476. } else {
  1477. block_size = 1 << this->erase_shift;
  1478. /* Start address must align on block boundary */
  1479. if (unlikely(addr & (block_size - 1))) {
  1480. MTDDEBUG(MTD_DEBUG_LEVEL0, "onenand_erase:"
  1481. "Unaligned address\n");
  1482. return -EINVAL;
  1483. }
  1484. }
  1485. /* Length must align on block boundary */
  1486. if (unlikely(len & (block_size - 1))) {
  1487. MTDDEBUG (MTD_DEBUG_LEVEL0,
  1488. "onenand_erase: Length not block aligned\n");
  1489. return -EINVAL;
  1490. }
  1491. instr->fail_addr = 0xffffffff;
  1492. /* Grab the lock and see if the device is available */
  1493. onenand_get_device(mtd, FL_ERASING);
  1494. /* Loop throught the pages */
  1495. instr->state = MTD_ERASING;
  1496. while (len) {
  1497. /* Check if we have a bad block, we do not erase bad blocks */
  1498. if (instr->priv == 0 && onenand_block_isbad_nolock(mtd, addr, 0)) {
  1499. printk(KERN_WARNING "onenand_erase: attempt to erase"
  1500. " a bad block at addr 0x%08x\n",
  1501. (unsigned int) addr);
  1502. instr->state = MTD_ERASE_FAILED;
  1503. goto erase_exit;
  1504. }
  1505. this->command(mtd, ONENAND_CMD_ERASE, addr, block_size);
  1506. onenand_invalidate_bufferram(mtd, addr, block_size);
  1507. ret = this->wait(mtd, FL_ERASING);
  1508. /* Check, if it is write protected */
  1509. if (ret) {
  1510. if (ret == -EPERM)
  1511. MTDDEBUG (MTD_DEBUG_LEVEL0, "onenand_erase: "
  1512. "Device is write protected!!!\n");
  1513. else
  1514. MTDDEBUG (MTD_DEBUG_LEVEL0, "onenand_erase: "
  1515. "Failed erase, block %d\n",
  1516. onenand_block(this, addr));
  1517. instr->state = MTD_ERASE_FAILED;
  1518. instr->fail_addr = addr;
  1519. goto erase_exit;
  1520. }
  1521. len -= block_size;
  1522. addr += block_size;
  1523. if (addr == region_end) {
  1524. if (!len)
  1525. break;
  1526. region++;
  1527. block_size = region->erasesize;
  1528. region_end = region->offset
  1529. + region->erasesize * region->numblocks;
  1530. if (len & (block_size - 1)) {
  1531. /* This has been checked at MTD
  1532. * partitioning level. */
  1533. printk("onenand_erase: Unaligned address\n");
  1534. goto erase_exit;
  1535. }
  1536. }
  1537. }
  1538. instr->state = MTD_ERASE_DONE;
  1539. erase_exit:
  1540. ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
  1541. /* Do call back function */
  1542. if (!ret)
  1543. mtd_erase_callback(instr);
  1544. /* Deselect and wake up anyone waiting on the device */
  1545. onenand_release_device(mtd);
  1546. return ret;
  1547. }
  1548. /**
  1549. * onenand_sync - [MTD Interface] sync
  1550. * @param mtd MTD device structure
  1551. *
  1552. * Sync is actually a wait for chip ready function
  1553. */
  1554. void onenand_sync(struct mtd_info *mtd)
  1555. {
  1556. MTDDEBUG (MTD_DEBUG_LEVEL3, "onenand_sync: called\n");
  1557. /* Grab the lock and see if the device is available */
  1558. onenand_get_device(mtd, FL_SYNCING);
  1559. /* Release it and go back */
  1560. onenand_release_device(mtd);
  1561. }
  1562. /**
  1563. * onenand_block_isbad - [MTD Interface] Check whether the block at the given offset is bad
  1564. * @param mtd MTD device structure
  1565. * @param ofs offset relative to mtd start
  1566. *
  1567. * Check whether the block is bad
  1568. */
  1569. int onenand_block_isbad(struct mtd_info *mtd, loff_t ofs)
  1570. {
  1571. int ret;
  1572. /* Check for invalid offset */
  1573. if (ofs > mtd->size)
  1574. return -EINVAL;
  1575. onenand_get_device(mtd, FL_READING);
  1576. ret = onenand_block_isbad_nolock(mtd,ofs, 0);
  1577. onenand_release_device(mtd);
  1578. return ret;
  1579. }
  1580. /**
  1581. * onenand_default_block_markbad - [DEFAULT] mark a block bad
  1582. * @param mtd MTD device structure
  1583. * @param ofs offset from device start
  1584. *
  1585. * This is the default implementation, which can be overridden by
  1586. * a hardware specific driver.
  1587. */
  1588. static int onenand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
  1589. {
  1590. struct onenand_chip *this = mtd->priv;
  1591. struct bbm_info *bbm = this->bbm;
  1592. u_char buf[2] = {0, 0};
  1593. struct mtd_oob_ops ops = {
  1594. .mode = MTD_OOB_PLACE,
  1595. .ooblen = 2,
  1596. .oobbuf = buf,
  1597. .ooboffs = 0,
  1598. };
  1599. int block;
  1600. /* Get block number */
  1601. block = onenand_block(this, ofs);
  1602. if (bbm->bbt)
  1603. bbm->bbt[block >> 2] |= 0x01 << ((block & 0x03) << 1);
  1604. /* We write two bytes, so we dont have to mess with 16 bit access */
  1605. ofs += mtd->oobsize + (bbm->badblockpos & ~0x01);
  1606. return onenand_write_oob_nolock(mtd, ofs, &ops);
  1607. }
  1608. /**
  1609. * onenand_block_markbad - [MTD Interface] Mark the block at the given offset as bad
  1610. * @param mtd MTD device structure
  1611. * @param ofs offset relative to mtd start
  1612. *
  1613. * Mark the block as bad
  1614. */
  1615. int onenand_block_markbad(struct mtd_info *mtd, loff_t ofs)
  1616. {
  1617. struct onenand_chip *this = mtd->priv;
  1618. int ret;
  1619. ret = onenand_block_isbad(mtd, ofs);
  1620. if (ret) {
  1621. /* If it was bad already, return success and do nothing */
  1622. if (ret > 0)
  1623. return 0;
  1624. return ret;
  1625. }
  1626. ret = this->block_markbad(mtd, ofs);
  1627. return ret;
  1628. }
  1629. /**
  1630. * onenand_do_lock_cmd - [OneNAND Interface] Lock or unlock block(s)
  1631. * @param mtd MTD device structure
  1632. * @param ofs offset relative to mtd start
  1633. * @param len number of bytes to lock or unlock
  1634. * @param cmd lock or unlock command
  1635. *
  1636. * Lock or unlock one or more blocks
  1637. */
  1638. static int onenand_do_lock_cmd(struct mtd_info *mtd, loff_t ofs, size_t len, int cmd)
  1639. {
  1640. struct onenand_chip *this = mtd->priv;
  1641. int start, end, block, value, status;
  1642. start = onenand_block(this, ofs);
  1643. end = onenand_block(this, ofs + len);
  1644. /* Continuous lock scheme */
  1645. if (this->options & ONENAND_HAS_CONT_LOCK) {
  1646. /* Set start block address */
  1647. this->write_word(start,
  1648. this->base + ONENAND_REG_START_BLOCK_ADDRESS);
  1649. /* Set end block address */
  1650. this->write_word(end - 1,
  1651. this->base + ONENAND_REG_END_BLOCK_ADDRESS);
  1652. /* Write unlock command */
  1653. this->command(mtd, cmd, 0, 0);
  1654. /* There's no return value */
  1655. this->wait(mtd, FL_UNLOCKING);
  1656. /* Sanity check */
  1657. while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
  1658. & ONENAND_CTRL_ONGO)
  1659. continue;
  1660. /* Check lock status */
  1661. status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
  1662. if (!(status & ONENAND_WP_US))
  1663. printk(KERN_ERR "wp status = 0x%x\n", status);
  1664. return 0;
  1665. }
  1666. /* Block lock scheme */
  1667. for (block = start; block < end; block++) {
  1668. /* Set block address */
  1669. value = onenand_block_address(this, block);
  1670. this->write_word(value, this->base + ONENAND_REG_START_ADDRESS1);
  1671. /* Select DataRAM for DDP */
  1672. value = onenand_bufferram_address(this, block);
  1673. this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
  1674. /* Set start block address */
  1675. this->write_word(block,
  1676. this->base + ONENAND_REG_START_BLOCK_ADDRESS);
  1677. /* Write unlock command */
  1678. this->command(mtd, ONENAND_CMD_UNLOCK, 0, 0);
  1679. /* There's no return value */
  1680. this->wait(mtd, FL_UNLOCKING);
  1681. /* Sanity check */
  1682. while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
  1683. & ONENAND_CTRL_ONGO)
  1684. continue;
  1685. /* Check lock status */
  1686. status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
  1687. if (!(status & ONENAND_WP_US))
  1688. printk(KERN_ERR "block = %d, wp status = 0x%x\n",
  1689. block, status);
  1690. }
  1691. return 0;
  1692. }
  1693. #ifdef ONENAND_LINUX
  1694. /**
  1695. * onenand_lock - [MTD Interface] Lock block(s)
  1696. * @param mtd MTD device structure
  1697. * @param ofs offset relative to mtd start
  1698. * @param len number of bytes to unlock
  1699. *
  1700. * Lock one or more blocks
  1701. */
  1702. static int onenand_lock(struct mtd_info *mtd, loff_t ofs, size_t len)
  1703. {
  1704. int ret;
  1705. onenand_get_device(mtd, FL_LOCKING);
  1706. ret = onenand_do_lock_cmd(mtd, ofs, len, ONENAND_CMD_LOCK);
  1707. onenand_release_device(mtd);
  1708. return ret;
  1709. }
  1710. /**
  1711. * onenand_unlock - [MTD Interface] Unlock block(s)
  1712. * @param mtd MTD device structure
  1713. * @param ofs offset relative to mtd start
  1714. * @param len number of bytes to unlock
  1715. *
  1716. * Unlock one or more blocks
  1717. */
  1718. static int onenand_unlock(struct mtd_info *mtd, loff_t ofs, size_t len)
  1719. {
  1720. int ret;
  1721. onenand_get_device(mtd, FL_LOCKING);
  1722. ret = onenand_do_lock_cmd(mtd, ofs, len, ONENAND_CMD_UNLOCK);
  1723. onenand_release_device(mtd);
  1724. return ret;
  1725. }
  1726. #endif
  1727. /**
  1728. * onenand_check_lock_status - [OneNAND Interface] Check lock status
  1729. * @param this onenand chip data structure
  1730. *
  1731. * Check lock status
  1732. */
  1733. static int onenand_check_lock_status(struct onenand_chip *this)
  1734. {
  1735. unsigned int value, block, status;
  1736. unsigned int end;
  1737. end = this->chipsize >> this->erase_shift;
  1738. for (block = 0; block < end; block++) {
  1739. /* Set block address */
  1740. value = onenand_block_address(this, block);
  1741. this->write_word(value, this->base + ONENAND_REG_START_ADDRESS1);
  1742. /* Select DataRAM for DDP */
  1743. value = onenand_bufferram_address(this, block);
  1744. this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
  1745. /* Set start block address */
  1746. this->write_word(block, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
  1747. /* Check lock status */
  1748. status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
  1749. if (!(status & ONENAND_WP_US)) {
  1750. printk(KERN_ERR "block = %d, wp status = 0x%x\n", block, status);
  1751. return 0;
  1752. }
  1753. }
  1754. return 1;
  1755. }
  1756. /**
  1757. * onenand_unlock_all - [OneNAND Interface] unlock all blocks
  1758. * @param mtd MTD device structure
  1759. *
  1760. * Unlock all blocks
  1761. */
  1762. static void onenand_unlock_all(struct mtd_info *mtd)
  1763. {
  1764. struct onenand_chip *this = mtd->priv;
  1765. loff_t ofs = 0;
  1766. size_t len = mtd->size;
  1767. if (this->options & ONENAND_HAS_UNLOCK_ALL) {
  1768. /* Set start block address */
  1769. this->write_word(0, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
  1770. /* Write unlock command */
  1771. this->command(mtd, ONENAND_CMD_UNLOCK_ALL, 0, 0);
  1772. /* There's no return value */
  1773. this->wait(mtd, FL_LOCKING);
  1774. /* Sanity check */
  1775. while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
  1776. & ONENAND_CTRL_ONGO)
  1777. continue;
  1778. /* Check lock status */
  1779. if (onenand_check_lock_status(this))
  1780. return;
  1781. /* Workaround for all block unlock in DDP */
  1782. if (ONENAND_IS_DDP(this) && !FLEXONENAND(this)) {
  1783. /* All blocks on another chip */
  1784. ofs = this->chipsize >> 1;
  1785. len = this->chipsize >> 1;
  1786. }
  1787. }
  1788. onenand_do_lock_cmd(mtd, ofs, len, ONENAND_CMD_UNLOCK);
  1789. }
  1790. /**
  1791. * onenand_check_features - Check and set OneNAND features
  1792. * @param mtd MTD data structure
  1793. *
  1794. * Check and set OneNAND features
  1795. * - lock scheme
  1796. * - two plane
  1797. */
  1798. static void onenand_check_features(struct mtd_info *mtd)
  1799. {
  1800. struct onenand_chip *this = mtd->priv;
  1801. unsigned int density, process;
  1802. /* Lock scheme depends on density and process */
  1803. density = onenand_get_density(this->device_id);
  1804. process = this->version_id >> ONENAND_VERSION_PROCESS_SHIFT;
  1805. /* Lock scheme */
  1806. switch (density) {
  1807. case ONENAND_DEVICE_DENSITY_4Gb:
  1808. if (ONENAND_IS_DDP(this))
  1809. this->options |= ONENAND_HAS_2PLANE;
  1810. else
  1811. this->options |= ONENAND_HAS_4KB_PAGE;
  1812. case ONENAND_DEVICE_DENSITY_2Gb:
  1813. /* 2Gb DDP don't have 2 plane */
  1814. if (!ONENAND_IS_DDP(this))
  1815. this->options |= ONENAND_HAS_2PLANE;
  1816. this->options |= ONENAND_HAS_UNLOCK_ALL;
  1817. case ONENAND_DEVICE_DENSITY_1Gb:
  1818. /* A-Die has all block unlock */
  1819. if (process)
  1820. this->options |= ONENAND_HAS_UNLOCK_ALL;
  1821. break;
  1822. default:
  1823. /* Some OneNAND has continuous lock scheme */
  1824. if (!process)
  1825. this->options |= ONENAND_HAS_CONT_LOCK;
  1826. break;
  1827. }
  1828. if (ONENAND_IS_MLC(this))
  1829. this->options |= ONENAND_HAS_4KB_PAGE;
  1830. if (ONENAND_IS_4KB_PAGE(this))
  1831. this->options &= ~ONENAND_HAS_2PLANE;
  1832. if (FLEXONENAND(this)) {
  1833. this->options &= ~ONENAND_HAS_CONT_LOCK;
  1834. this->options |= ONENAND_HAS_UNLOCK_ALL;
  1835. }
  1836. if (this->options & ONENAND_HAS_CONT_LOCK)
  1837. printk(KERN_DEBUG "Lock scheme is Continuous Lock\n");
  1838. if (this->options & ONENAND_HAS_UNLOCK_ALL)
  1839. printk(KERN_DEBUG "Chip support all block unlock\n");
  1840. if (this->options & ONENAND_HAS_2PLANE)
  1841. printk(KERN_DEBUG "Chip has 2 plane\n");
  1842. if (this->options & ONENAND_HAS_4KB_PAGE)
  1843. printk(KERN_DEBUG "Chip has 4KiB pagesize\n");
  1844. }
  1845. /**
  1846. * onenand_print_device_info - Print device ID
  1847. * @param device device ID
  1848. *
  1849. * Print device ID
  1850. */
  1851. char *onenand_print_device_info(int device, int version)
  1852. {
  1853. int vcc, demuxed, ddp, density, flexonenand;
  1854. char *dev_info = malloc(80);
  1855. char *p = dev_info;
  1856. vcc = device & ONENAND_DEVICE_VCC_MASK;
  1857. demuxed = device & ONENAND_DEVICE_IS_DEMUX;
  1858. ddp = device & ONENAND_DEVICE_IS_DDP;
  1859. density = onenand_get_density(device);
  1860. flexonenand = device & DEVICE_IS_FLEXONENAND;
  1861. p += sprintf(dev_info, "%s%sOneNAND%s %dMB %sV 16-bit (0x%02x)",
  1862. demuxed ? "" : "Muxed ",
  1863. flexonenand ? "Flex-" : "",
  1864. ddp ? "(DDP)" : "",
  1865. (16 << density), vcc ? "2.65/3.3" : "1.8", device);
  1866. sprintf(p, "\nOneNAND version = 0x%04x", version);
  1867. printk("%s\n", dev_info);
  1868. return dev_info;
  1869. }
  1870. static const struct onenand_manufacturers onenand_manuf_ids[] = {
  1871. {ONENAND_MFR_NUMONYX, "Numonyx"},
  1872. {ONENAND_MFR_SAMSUNG, "Samsung"},
  1873. };
  1874. /**
  1875. * onenand_check_maf - Check manufacturer ID
  1876. * @param manuf manufacturer ID
  1877. *
  1878. * Check manufacturer ID
  1879. */
  1880. static int onenand_check_maf(int manuf)
  1881. {
  1882. int size = ARRAY_SIZE(onenand_manuf_ids);
  1883. int i;
  1884. #ifdef ONENAND_DEBUG
  1885. char *name;
  1886. #endif
  1887. for (i = 0; i < size; i++)
  1888. if (manuf == onenand_manuf_ids[i].id)
  1889. break;
  1890. #ifdef ONENAND_DEBUG
  1891. if (i < size)
  1892. name = onenand_manuf_ids[i].name;
  1893. else
  1894. name = "Unknown";
  1895. printk(KERN_DEBUG "OneNAND Manufacturer: %s (0x%0x)\n", name, manuf);
  1896. #endif
  1897. return i == size;
  1898. }
  1899. /**
  1900. * flexonenand_get_boundary - Reads the SLC boundary
  1901. * @param onenand_info - onenand info structure
  1902. *
  1903. * Fill up boundary[] field in onenand_chip
  1904. **/
  1905. static int flexonenand_get_boundary(struct mtd_info *mtd)
  1906. {
  1907. struct onenand_chip *this = mtd->priv;
  1908. unsigned int die, bdry;
  1909. int syscfg, locked;
  1910. /* Disable ECC */
  1911. syscfg = this->read_word(this->base + ONENAND_REG_SYS_CFG1);
  1912. this->write_word((syscfg | 0x0100), this->base + ONENAND_REG_SYS_CFG1);
  1913. for (die = 0; die < this->dies; die++) {
  1914. this->command(mtd, FLEXONENAND_CMD_PI_ACCESS, die, 0);
  1915. this->wait(mtd, FL_SYNCING);
  1916. this->command(mtd, FLEXONENAND_CMD_READ_PI, die, 0);
  1917. this->wait(mtd, FL_READING);
  1918. bdry = this->read_word(this->base + ONENAND_DATARAM);
  1919. if ((bdry >> FLEXONENAND_PI_UNLOCK_SHIFT) == 3)
  1920. locked = 0;
  1921. else
  1922. locked = 1;
  1923. this->boundary[die] = bdry & FLEXONENAND_PI_MASK;
  1924. this->command(mtd, ONENAND_CMD_RESET, 0, 0);
  1925. this->wait(mtd, FL_RESETING);
  1926. printk(KERN_INFO "Die %d boundary: %d%s\n", die,
  1927. this->boundary[die], locked ? "(Locked)" : "(Unlocked)");
  1928. }
  1929. /* Enable ECC */
  1930. this->write_word(syscfg, this->base + ONENAND_REG_SYS_CFG1);
  1931. return 0;
  1932. }
  1933. /**
  1934. * flexonenand_get_size - Fill up fields in onenand_chip and mtd_info
  1935. * boundary[], diesize[], mtd->size, mtd->erasesize,
  1936. * mtd->eraseregions
  1937. * @param mtd - MTD device structure
  1938. */
  1939. static void flexonenand_get_size(struct mtd_info *mtd)
  1940. {
  1941. struct onenand_chip *this = mtd->priv;
  1942. int die, i, eraseshift, density;
  1943. int blksperdie, maxbdry;
  1944. loff_t ofs;
  1945. density = onenand_get_density(this->device_id);
  1946. blksperdie = ((loff_t)(16 << density) << 20) >> (this->erase_shift);
  1947. blksperdie >>= ONENAND_IS_DDP(this) ? 1 : 0;
  1948. maxbdry = blksperdie - 1;
  1949. eraseshift = this->erase_shift - 1;
  1950. mtd->numeraseregions = this->dies << 1;
  1951. /* This fills up the device boundary */
  1952. flexonenand_get_boundary(mtd);
  1953. die = 0;
  1954. ofs = 0;
  1955. i = -1;
  1956. for (; die < this->dies; die++) {
  1957. if (!die || this->boundary[die-1] != maxbdry) {
  1958. i++;
  1959. mtd->eraseregions[i].offset = ofs;
  1960. mtd->eraseregions[i].erasesize = 1 << eraseshift;
  1961. mtd->eraseregions[i].numblocks =
  1962. this->boundary[die] + 1;
  1963. ofs += mtd->eraseregions[i].numblocks << eraseshift;
  1964. eraseshift++;
  1965. } else {
  1966. mtd->numeraseregions -= 1;
  1967. mtd->eraseregions[i].numblocks +=
  1968. this->boundary[die] + 1;
  1969. ofs += (this->boundary[die] + 1) << (eraseshift - 1);
  1970. }
  1971. if (this->boundary[die] != maxbdry) {
  1972. i++;
  1973. mtd->eraseregions[i].offset = ofs;
  1974. mtd->eraseregions[i].erasesize = 1 << eraseshift;
  1975. mtd->eraseregions[i].numblocks = maxbdry ^
  1976. this->boundary[die];
  1977. ofs += mtd->eraseregions[i].numblocks << eraseshift;
  1978. eraseshift--;
  1979. } else
  1980. mtd->numeraseregions -= 1;
  1981. }
  1982. /* Expose MLC erase size except when all blocks are SLC */
  1983. mtd->erasesize = 1 << this->erase_shift;
  1984. if (mtd->numeraseregions == 1)
  1985. mtd->erasesize >>= 1;
  1986. printk(KERN_INFO "Device has %d eraseregions\n", mtd->numeraseregions);
  1987. for (i = 0; i < mtd->numeraseregions; i++)
  1988. printk(KERN_INFO "[offset: 0x%08llx, erasesize: 0x%05x,"
  1989. " numblocks: %04u]\n", mtd->eraseregions[i].offset,
  1990. mtd->eraseregions[i].erasesize,
  1991. mtd->eraseregions[i].numblocks);
  1992. for (die = 0, mtd->size = 0; die < this->dies; die++) {
  1993. this->diesize[die] = (loff_t) (blksperdie << this->erase_shift);
  1994. this->diesize[die] -= (loff_t) (this->boundary[die] + 1)
  1995. << (this->erase_shift - 1);
  1996. mtd->size += this->diesize[die];
  1997. }
  1998. }
  1999. /**
  2000. * flexonenand_check_blocks_erased - Check if blocks are erased
  2001. * @param mtd_info - mtd info structure
  2002. * @param start - first erase block to check
  2003. * @param end - last erase block to check
  2004. *
  2005. * Converting an unerased block from MLC to SLC
  2006. * causes byte values to change. Since both data and its ECC
  2007. * have changed, reads on the block give uncorrectable error.
  2008. * This might lead to the block being detected as bad.
  2009. *
  2010. * Avoid this by ensuring that the block to be converted is
  2011. * erased.
  2012. */
  2013. static int flexonenand_check_blocks_erased(struct mtd_info *mtd,
  2014. int start, int end)
  2015. {
  2016. struct onenand_chip *this = mtd->priv;
  2017. int i, ret;
  2018. int block;
  2019. struct mtd_oob_ops ops = {
  2020. .mode = MTD_OOB_PLACE,
  2021. .ooboffs = 0,
  2022. .ooblen = mtd->oobsize,
  2023. .datbuf = NULL,
  2024. .oobbuf = this->oob_buf,
  2025. };
  2026. loff_t addr;
  2027. printk(KERN_DEBUG "Check blocks from %d to %d\n", start, end);
  2028. for (block = start; block <= end; block++) {
  2029. addr = flexonenand_addr(this, block);
  2030. if (onenand_block_isbad_nolock(mtd, addr, 0))
  2031. continue;
  2032. /*
  2033. * Since main area write results in ECC write to spare,
  2034. * it is sufficient to check only ECC bytes for change.
  2035. */
  2036. ret = onenand_read_oob_nolock(mtd, addr, &ops);
  2037. if (ret)
  2038. return ret;
  2039. for (i = 0; i < mtd->oobsize; i++)
  2040. if (this->oob_buf[i] != 0xff)
  2041. break;
  2042. if (i != mtd->oobsize) {
  2043. printk(KERN_WARNING "Block %d not erased.\n", block);
  2044. return 1;
  2045. }
  2046. }
  2047. return 0;
  2048. }
  2049. /**
  2050. * flexonenand_set_boundary - Writes the SLC boundary
  2051. * @param mtd - mtd info structure
  2052. */
  2053. int flexonenand_set_boundary(struct mtd_info *mtd, int die,
  2054. int boundary, int lock)
  2055. {
  2056. struct onenand_chip *this = mtd->priv;
  2057. int ret, density, blksperdie, old, new, thisboundary;
  2058. loff_t addr;
  2059. if (die >= this->dies)
  2060. return -EINVAL;
  2061. if (boundary == this->boundary[die])
  2062. return 0;
  2063. density = onenand_get_density(this->device_id);
  2064. blksperdie = ((16 << density) << 20) >> this->erase_shift;
  2065. blksperdie >>= ONENAND_IS_DDP(this) ? 1 : 0;
  2066. if (boundary >= blksperdie) {
  2067. printk("flexonenand_set_boundary:"
  2068. "Invalid boundary value. "
  2069. "Boundary not changed.\n");
  2070. return -EINVAL;
  2071. }
  2072. /* Check if converting blocks are erased */
  2073. old = this->boundary[die] + (die * this->density_mask);
  2074. new = boundary + (die * this->density_mask);
  2075. ret = flexonenand_check_blocks_erased(mtd, min(old, new)
  2076. + 1, max(old, new));
  2077. if (ret) {
  2078. printk(KERN_ERR "flexonenand_set_boundary: Please erase blocks before boundary change\n");
  2079. return ret;
  2080. }
  2081. this->command(mtd, FLEXONENAND_CMD_PI_ACCESS, die, 0);
  2082. this->wait(mtd, FL_SYNCING);
  2083. /* Check is boundary is locked */
  2084. this->command(mtd, FLEXONENAND_CMD_READ_PI, die, 0);
  2085. ret = this->wait(mtd, FL_READING);
  2086. thisboundary = this->read_word(this->base + ONENAND_DATARAM);
  2087. if ((thisboundary >> FLEXONENAND_PI_UNLOCK_SHIFT) != 3) {
  2088. printk(KERN_ERR "flexonenand_set_boundary: boundary locked\n");
  2089. goto out;
  2090. }
  2091. printk(KERN_INFO "flexonenand_set_boundary: Changing die %d boundary: %d%s\n",
  2092. die, boundary, lock ? "(Locked)" : "(Unlocked)");
  2093. boundary &= FLEXONENAND_PI_MASK;
  2094. boundary |= lock ? 0 : (3 << FLEXONENAND_PI_UNLOCK_SHIFT);
  2095. addr = die ? this->diesize[0] : 0;
  2096. this->command(mtd, ONENAND_CMD_ERASE, addr, 0);
  2097. ret = this->wait(mtd, FL_ERASING);
  2098. if (ret) {
  2099. printk("flexonenand_set_boundary:"
  2100. "Failed PI erase for Die %d\n", die);
  2101. goto out;
  2102. }
  2103. this->write_word(boundary, this->base + ONENAND_DATARAM);
  2104. this->command(mtd, ONENAND_CMD_PROG, addr, 0);
  2105. ret = this->wait(mtd, FL_WRITING);
  2106. if (ret) {
  2107. printk("flexonenand_set_boundary:"
  2108. "Failed PI write for Die %d\n", die);
  2109. goto out;
  2110. }
  2111. this->command(mtd, FLEXONENAND_CMD_PI_UPDATE, die, 0);
  2112. ret = this->wait(mtd, FL_WRITING);
  2113. out:
  2114. this->write_word(ONENAND_CMD_RESET, this->base + ONENAND_REG_COMMAND);
  2115. this->wait(mtd, FL_RESETING);
  2116. if (!ret)
  2117. /* Recalculate device size on boundary change*/
  2118. flexonenand_get_size(mtd);
  2119. return ret;
  2120. }
  2121. /**
  2122. * onenand_chip_probe - [OneNAND Interface] Probe the OneNAND chip
  2123. * @param mtd MTD device structure
  2124. *
  2125. * OneNAND detection method:
  2126. * Compare the the values from command with ones from register
  2127. */
  2128. static int onenand_chip_probe(struct mtd_info *mtd)
  2129. {
  2130. struct onenand_chip *this = mtd->priv;
  2131. int bram_maf_id, bram_dev_id, maf_id, dev_id;
  2132. int syscfg;
  2133. /* Save system configuration 1 */
  2134. syscfg = this->read_word(this->base + ONENAND_REG_SYS_CFG1);
  2135. /* Clear Sync. Burst Read mode to read BootRAM */
  2136. this->write_word((syscfg & ~ONENAND_SYS_CFG1_SYNC_READ),
  2137. this->base + ONENAND_REG_SYS_CFG1);
  2138. /* Send the command for reading device ID from BootRAM */
  2139. this->write_word(ONENAND_CMD_READID, this->base + ONENAND_BOOTRAM);
  2140. /* Read manufacturer and device IDs from BootRAM */
  2141. bram_maf_id = this->read_word(this->base + ONENAND_BOOTRAM + 0x0);
  2142. bram_dev_id = this->read_word(this->base + ONENAND_BOOTRAM + 0x2);
  2143. /* Reset OneNAND to read default register values */
  2144. this->write_word(ONENAND_CMD_RESET, this->base + ONENAND_BOOTRAM);
  2145. /* Wait reset */
  2146. this->wait(mtd, FL_RESETING);
  2147. /* Restore system configuration 1 */
  2148. this->write_word(syscfg, this->base + ONENAND_REG_SYS_CFG1);
  2149. /* Check manufacturer ID */
  2150. if (onenand_check_maf(bram_maf_id))
  2151. return -ENXIO;
  2152. /* Read manufacturer and device IDs from Register */
  2153. maf_id = this->read_word(this->base + ONENAND_REG_MANUFACTURER_ID);
  2154. dev_id = this->read_word(this->base + ONENAND_REG_DEVICE_ID);
  2155. /* Check OneNAND device */
  2156. if (maf_id != bram_maf_id || dev_id != bram_dev_id)
  2157. return -ENXIO;
  2158. return 0;
  2159. }
  2160. /**
  2161. * onenand_probe - [OneNAND Interface] Probe the OneNAND device
  2162. * @param mtd MTD device structure
  2163. *
  2164. * OneNAND detection method:
  2165. * Compare the the values from command with ones from register
  2166. */
  2167. int onenand_probe(struct mtd_info *mtd)
  2168. {
  2169. struct onenand_chip *this = mtd->priv;
  2170. int dev_id, ver_id;
  2171. int density;
  2172. int ret;
  2173. ret = this->chip_probe(mtd);
  2174. if (ret)
  2175. return ret;
  2176. /* Read device IDs from Register */
  2177. dev_id = this->read_word(this->base + ONENAND_REG_DEVICE_ID);
  2178. ver_id = this->read_word(this->base + ONENAND_REG_VERSION_ID);
  2179. this->technology = this->read_word(this->base + ONENAND_REG_TECHNOLOGY);
  2180. /* Flash device information */
  2181. mtd->name = onenand_print_device_info(dev_id, ver_id);
  2182. this->device_id = dev_id;
  2183. this->version_id = ver_id;
  2184. /* Check OneNAND features */
  2185. onenand_check_features(mtd);
  2186. density = onenand_get_density(dev_id);
  2187. if (FLEXONENAND(this)) {
  2188. this->dies = ONENAND_IS_DDP(this) ? 2 : 1;
  2189. /* Maximum possible erase regions */
  2190. mtd->numeraseregions = this->dies << 1;
  2191. mtd->eraseregions = malloc(sizeof(struct mtd_erase_region_info)
  2192. * (this->dies << 1));
  2193. if (!mtd->eraseregions)
  2194. return -ENOMEM;
  2195. }
  2196. /*
  2197. * For Flex-OneNAND, chipsize represents maximum possible device size.
  2198. * mtd->size represents the actual device size.
  2199. */
  2200. this->chipsize = (16 << density) << 20;
  2201. /* OneNAND page size & block size */
  2202. /* The data buffer size is equal to page size */
  2203. mtd->writesize =
  2204. this->read_word(this->base + ONENAND_REG_DATA_BUFFER_SIZE);
  2205. /* We use the full BufferRAM */
  2206. if (ONENAND_IS_4KB_PAGE(this))
  2207. mtd->writesize <<= 1;
  2208. mtd->oobsize = mtd->writesize >> 5;
  2209. /* Pagers per block is always 64 in OneNAND */
  2210. mtd->erasesize = mtd->writesize << 6;
  2211. /*
  2212. * Flex-OneNAND SLC area has 64 pages per block.
  2213. * Flex-OneNAND MLC area has 128 pages per block.
  2214. * Expose MLC erase size to find erase_shift and page_mask.
  2215. */
  2216. if (FLEXONENAND(this))
  2217. mtd->erasesize <<= 1;
  2218. this->erase_shift = ffs(mtd->erasesize) - 1;
  2219. this->page_shift = ffs(mtd->writesize) - 1;
  2220. this->ppb_shift = (this->erase_shift - this->page_shift);
  2221. this->page_mask = (mtd->erasesize / mtd->writesize) - 1;
  2222. /* Set density mask. it is used for DDP */
  2223. if (ONENAND_IS_DDP(this))
  2224. this->density_mask = this->chipsize >> (this->erase_shift + 1);
  2225. /* It's real page size */
  2226. this->writesize = mtd->writesize;
  2227. /* REVIST: Multichip handling */
  2228. if (FLEXONENAND(this))
  2229. flexonenand_get_size(mtd);
  2230. else
  2231. mtd->size = this->chipsize;
  2232. mtd->flags = MTD_CAP_NANDFLASH;
  2233. mtd->erase = onenand_erase;
  2234. mtd->read = onenand_read;
  2235. mtd->write = onenand_write;
  2236. mtd->read_oob = onenand_read_oob;
  2237. mtd->write_oob = onenand_write_oob;
  2238. mtd->sync = onenand_sync;
  2239. mtd->block_isbad = onenand_block_isbad;
  2240. mtd->block_markbad = onenand_block_markbad;
  2241. return 0;
  2242. }
  2243. /**
  2244. * onenand_scan - [OneNAND Interface] Scan for the OneNAND device
  2245. * @param mtd MTD device structure
  2246. * @param maxchips Number of chips to scan for
  2247. *
  2248. * This fills out all the not initialized function pointers
  2249. * with the defaults.
  2250. * The flash ID is read and the mtd/chip structures are
  2251. * filled with the appropriate values.
  2252. */
  2253. int onenand_scan(struct mtd_info *mtd, int maxchips)
  2254. {
  2255. int i;
  2256. struct onenand_chip *this = mtd->priv;
  2257. if (!this->read_word)
  2258. this->read_word = onenand_readw;
  2259. if (!this->write_word)
  2260. this->write_word = onenand_writew;
  2261. if (!this->command)
  2262. this->command = onenand_command;
  2263. if (!this->wait)
  2264. this->wait = onenand_wait;
  2265. if (!this->bbt_wait)
  2266. this->bbt_wait = onenand_bbt_wait;
  2267. if (!this->read_bufferram)
  2268. this->read_bufferram = onenand_read_bufferram;
  2269. if (!this->write_bufferram)
  2270. this->write_bufferram = onenand_write_bufferram;
  2271. if (!this->chip_probe)
  2272. this->chip_probe = onenand_chip_probe;
  2273. if (!this->block_markbad)
  2274. this->block_markbad = onenand_default_block_markbad;
  2275. if (!this->scan_bbt)
  2276. this->scan_bbt = onenand_default_bbt;
  2277. if (onenand_probe(mtd))
  2278. return -ENXIO;
  2279. /* Set Sync. Burst Read after probing */
  2280. if (this->mmcontrol) {
  2281. printk(KERN_INFO "OneNAND Sync. Burst Read support\n");
  2282. this->read_bufferram = onenand_sync_read_bufferram;
  2283. }
  2284. /* Allocate buffers, if necessary */
  2285. if (!this->page_buf) {
  2286. this->page_buf = kzalloc(mtd->writesize, GFP_KERNEL);
  2287. if (!this->page_buf) {
  2288. printk(KERN_ERR "onenand_scan(): Can't allocate page_buf\n");
  2289. return -ENOMEM;
  2290. }
  2291. this->options |= ONENAND_PAGEBUF_ALLOC;
  2292. }
  2293. if (!this->oob_buf) {
  2294. this->oob_buf = kzalloc(mtd->oobsize, GFP_KERNEL);
  2295. if (!this->oob_buf) {
  2296. printk(KERN_ERR "onenand_scan: Can't allocate oob_buf\n");
  2297. if (this->options & ONENAND_PAGEBUF_ALLOC) {
  2298. this->options &= ~ONENAND_PAGEBUF_ALLOC;
  2299. kfree(this->page_buf);
  2300. }
  2301. return -ENOMEM;
  2302. }
  2303. this->options |= ONENAND_OOBBUF_ALLOC;
  2304. }
  2305. this->state = FL_READY;
  2306. /*
  2307. * Allow subpage writes up to oobsize.
  2308. */
  2309. switch (mtd->oobsize) {
  2310. case 128:
  2311. this->ecclayout = &onenand_oob_128;
  2312. mtd->subpage_sft = 0;
  2313. break;
  2314. case 64:
  2315. this->ecclayout = &onenand_oob_64;
  2316. mtd->subpage_sft = 2;
  2317. break;
  2318. case 32:
  2319. this->ecclayout = &onenand_oob_32;
  2320. mtd->subpage_sft = 1;
  2321. break;
  2322. default:
  2323. printk(KERN_WARNING "No OOB scheme defined for oobsize %d\n",
  2324. mtd->oobsize);
  2325. mtd->subpage_sft = 0;
  2326. /* To prevent kernel oops */
  2327. this->ecclayout = &onenand_oob_32;
  2328. break;
  2329. }
  2330. this->subpagesize = mtd->writesize >> mtd->subpage_sft;
  2331. /*
  2332. * The number of bytes available for a client to place data into
  2333. * the out of band area
  2334. */
  2335. this->ecclayout->oobavail = 0;
  2336. for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES &&
  2337. this->ecclayout->oobfree[i].length; i++)
  2338. this->ecclayout->oobavail +=
  2339. this->ecclayout->oobfree[i].length;
  2340. mtd->oobavail = this->ecclayout->oobavail;
  2341. mtd->ecclayout = this->ecclayout;
  2342. /* Unlock whole block */
  2343. onenand_unlock_all(mtd);
  2344. return this->scan_bbt(mtd);
  2345. }
  2346. /**
  2347. * onenand_release - [OneNAND Interface] Free resources held by the OneNAND device
  2348. * @param mtd MTD device structure
  2349. */
  2350. void onenand_release(struct mtd_info *mtd)
  2351. {
  2352. }