onenand_base.c 72 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773
  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 - [INTERN] 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 (!mtd_is_eccerr(status) && 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_OPS_AUTO_OOB)
  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_OPS_AUTO_OOB)
  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 (mtd_is_eccerr(ret))
  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 (mtd_is_eccerr(ret))
  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 max bitflips per ecc step; ONENANDs correct 1 bit only */
  843. return mtd->ecc_stats.corrected != stats.corrected ? 1 : 0;
  844. }
  845. /**
  846. * onenand_read_oob_nolock - [MTD Interface] OneNAND read out-of-band
  847. * @param mtd MTD device structure
  848. * @param from offset to read from
  849. * @param ops oob operation description structure
  850. *
  851. * OneNAND read out-of-band data from the spare area
  852. */
  853. static int onenand_read_oob_nolock(struct mtd_info *mtd, loff_t from,
  854. struct mtd_oob_ops *ops)
  855. {
  856. struct onenand_chip *this = mtd->priv;
  857. struct mtd_ecc_stats stats;
  858. int read = 0, thislen, column, oobsize;
  859. size_t len = ops->ooblen;
  860. unsigned int mode = ops->mode;
  861. u_char *buf = ops->oobbuf;
  862. int ret = 0, readcmd;
  863. from += ops->ooboffs;
  864. MTDDEBUG(MTD_DEBUG_LEVEL3, "onenand_read_oob_nolock: from = 0x%08x, len = %i\n", (unsigned int) from, (int) len);
  865. /* Initialize return length value */
  866. ops->oobretlen = 0;
  867. if (mode == MTD_OPS_AUTO_OOB)
  868. oobsize = this->ecclayout->oobavail;
  869. else
  870. oobsize = mtd->oobsize;
  871. column = from & (mtd->oobsize - 1);
  872. if (unlikely(column >= oobsize)) {
  873. printk(KERN_ERR "onenand_read_oob_nolock: Attempted to start read outside oob\n");
  874. return -EINVAL;
  875. }
  876. /* Do not allow reads past end of device */
  877. if (unlikely(from >= mtd->size ||
  878. column + len > ((mtd->size >> this->page_shift) -
  879. (from >> this->page_shift)) * oobsize)) {
  880. printk(KERN_ERR "onenand_read_oob_nolock: Attempted to read beyond end of device\n");
  881. return -EINVAL;
  882. }
  883. stats = mtd->ecc_stats;
  884. readcmd = ONENAND_IS_4KB_PAGE(this) ?
  885. ONENAND_CMD_READ : ONENAND_CMD_READOOB;
  886. while (read < len) {
  887. thislen = oobsize - column;
  888. thislen = min_t(int, thislen, len);
  889. this->spare_buf = buf;
  890. this->command(mtd, readcmd, from, mtd->oobsize);
  891. onenand_update_bufferram(mtd, from, 0);
  892. ret = this->wait(mtd, FL_READING);
  893. if (unlikely(ret))
  894. ret = onenand_recover_lsb(mtd, from, ret);
  895. if (ret && ret != -EBADMSG) {
  896. printk(KERN_ERR "onenand_read_oob_nolock: read failed = 0x%x\n", ret);
  897. break;
  898. }
  899. if (mode == MTD_OPS_AUTO_OOB)
  900. onenand_transfer_auto_oob(mtd, buf, column, thislen);
  901. else
  902. this->read_bufferram(mtd, 0, ONENAND_SPARERAM, buf, column, thislen);
  903. read += thislen;
  904. if (read == len)
  905. break;
  906. buf += thislen;
  907. /* Read more? */
  908. if (read < len) {
  909. /* Page size */
  910. from += mtd->writesize;
  911. column = 0;
  912. }
  913. }
  914. ops->oobretlen = read;
  915. if (ret)
  916. return ret;
  917. if (mtd->ecc_stats.failed - stats.failed)
  918. return -EBADMSG;
  919. return 0;
  920. }
  921. /**
  922. * onenand_read - [MTD Interface] MTD compability function for onenand_read_ecc
  923. * @param mtd MTD device structure
  924. * @param from offset to read from
  925. * @param len number of bytes to read
  926. * @param retlen pointer to variable to store the number of read bytes
  927. * @param buf the databuffer to put data
  928. *
  929. * This function simply calls onenand_read_ecc with oob buffer and oobsel = NULL
  930. */
  931. int onenand_read(struct mtd_info *mtd, loff_t from, size_t len,
  932. size_t * retlen, u_char * buf)
  933. {
  934. struct mtd_oob_ops ops = {
  935. .len = len,
  936. .ooblen = 0,
  937. .datbuf = buf,
  938. .oobbuf = NULL,
  939. };
  940. int ret;
  941. onenand_get_device(mtd, FL_READING);
  942. ret = onenand_read_ops_nolock(mtd, from, &ops);
  943. onenand_release_device(mtd);
  944. *retlen = ops.retlen;
  945. return ret;
  946. }
  947. /**
  948. * onenand_read_oob - [MTD Interface] OneNAND read out-of-band
  949. * @param mtd MTD device structure
  950. * @param from offset to read from
  951. * @param ops oob operations description structure
  952. *
  953. * OneNAND main and/or out-of-band
  954. */
  955. int onenand_read_oob(struct mtd_info *mtd, loff_t from,
  956. struct mtd_oob_ops *ops)
  957. {
  958. int ret;
  959. switch (ops->mode) {
  960. case MTD_OPS_PLACE_OOB:
  961. case MTD_OPS_AUTO_OOB:
  962. break;
  963. case MTD_OPS_RAW:
  964. /* Not implemented yet */
  965. default:
  966. return -EINVAL;
  967. }
  968. onenand_get_device(mtd, FL_READING);
  969. if (ops->datbuf)
  970. ret = onenand_read_ops_nolock(mtd, from, ops);
  971. else
  972. ret = onenand_read_oob_nolock(mtd, from, ops);
  973. onenand_release_device(mtd);
  974. return ret;
  975. }
  976. /**
  977. * onenand_bbt_wait - [DEFAULT] wait until the command is done
  978. * @param mtd MTD device structure
  979. * @param state state to select the max. timeout value
  980. *
  981. * Wait for command done.
  982. */
  983. static int onenand_bbt_wait(struct mtd_info *mtd, int state)
  984. {
  985. struct onenand_chip *this = mtd->priv;
  986. unsigned int flags = ONENAND_INT_MASTER;
  987. unsigned int interrupt;
  988. unsigned int ctrl;
  989. while (1) {
  990. interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
  991. if (interrupt & flags)
  992. break;
  993. }
  994. /* To get correct interrupt status in timeout case */
  995. interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
  996. ctrl = this->read_word(this->base + ONENAND_REG_CTRL_STATUS);
  997. if (interrupt & ONENAND_INT_READ) {
  998. int ecc = onenand_read_ecc(this);
  999. if (ecc & ONENAND_ECC_2BIT_ALL) {
  1000. printk(KERN_INFO "onenand_bbt_wait: ecc error = 0x%04x"
  1001. ", controller = 0x%04x\n", ecc, ctrl);
  1002. return ONENAND_BBT_READ_ERROR;
  1003. }
  1004. } else {
  1005. printk(KERN_ERR "onenand_bbt_wait: read timeout!"
  1006. "ctrl=0x%04x intr=0x%04x\n", ctrl, interrupt);
  1007. return ONENAND_BBT_READ_FATAL_ERROR;
  1008. }
  1009. /* Initial bad block case: 0x2400 or 0x0400 */
  1010. if (ctrl & ONENAND_CTRL_ERROR) {
  1011. printk(KERN_DEBUG "onenand_bbt_wait: controller error = 0x%04x\n", ctrl);
  1012. return ONENAND_BBT_READ_ERROR;
  1013. }
  1014. return 0;
  1015. }
  1016. /**
  1017. * onenand_bbt_read_oob - [MTD Interface] OneNAND read out-of-band for bbt scan
  1018. * @param mtd MTD device structure
  1019. * @param from offset to read from
  1020. * @param ops oob operation description structure
  1021. *
  1022. * OneNAND read out-of-band data from the spare area for bbt scan
  1023. */
  1024. int onenand_bbt_read_oob(struct mtd_info *mtd, loff_t from,
  1025. struct mtd_oob_ops *ops)
  1026. {
  1027. struct onenand_chip *this = mtd->priv;
  1028. int read = 0, thislen, column;
  1029. int ret = 0, readcmd;
  1030. size_t len = ops->ooblen;
  1031. u_char *buf = ops->oobbuf;
  1032. MTDDEBUG(MTD_DEBUG_LEVEL3, "onenand_bbt_read_oob: from = 0x%08x, len = %zi\n", (unsigned int) from, len);
  1033. readcmd = ONENAND_IS_4KB_PAGE(this) ?
  1034. ONENAND_CMD_READ : ONENAND_CMD_READOOB;
  1035. /* Initialize return value */
  1036. ops->oobretlen = 0;
  1037. /* Do not allow reads past end of device */
  1038. if (unlikely((from + len) > mtd->size)) {
  1039. printk(KERN_ERR "onenand_bbt_read_oob: Attempt read beyond end of device\n");
  1040. return ONENAND_BBT_READ_FATAL_ERROR;
  1041. }
  1042. /* Grab the lock and see if the device is available */
  1043. onenand_get_device(mtd, FL_READING);
  1044. column = from & (mtd->oobsize - 1);
  1045. while (read < len) {
  1046. thislen = mtd->oobsize - column;
  1047. thislen = min_t(int, thislen, len);
  1048. this->spare_buf = buf;
  1049. this->command(mtd, readcmd, from, mtd->oobsize);
  1050. onenand_update_bufferram(mtd, from, 0);
  1051. ret = this->bbt_wait(mtd, FL_READING);
  1052. if (unlikely(ret))
  1053. ret = onenand_recover_lsb(mtd, from, ret);
  1054. if (ret)
  1055. break;
  1056. this->read_bufferram(mtd, 0, ONENAND_SPARERAM, buf, column, thislen);
  1057. read += thislen;
  1058. if (read == len)
  1059. break;
  1060. buf += thislen;
  1061. /* Read more? */
  1062. if (read < len) {
  1063. /* Update Page size */
  1064. from += this->writesize;
  1065. column = 0;
  1066. }
  1067. }
  1068. /* Deselect and wake up anyone waiting on the device */
  1069. onenand_release_device(mtd);
  1070. ops->oobretlen = read;
  1071. return ret;
  1072. }
  1073. #ifdef CONFIG_MTD_ONENAND_VERIFY_WRITE
  1074. /**
  1075. * onenand_verify_oob - [GENERIC] verify the oob contents after a write
  1076. * @param mtd MTD device structure
  1077. * @param buf the databuffer to verify
  1078. * @param to offset to read from
  1079. */
  1080. static int onenand_verify_oob(struct mtd_info *mtd, const u_char *buf, loff_t to)
  1081. {
  1082. struct onenand_chip *this = mtd->priv;
  1083. u_char *oob_buf = this->oob_buf;
  1084. int status, i, readcmd;
  1085. readcmd = ONENAND_IS_4KB_PAGE(this) ?
  1086. ONENAND_CMD_READ : ONENAND_CMD_READOOB;
  1087. this->command(mtd, readcmd, to, mtd->oobsize);
  1088. onenand_update_bufferram(mtd, to, 0);
  1089. status = this->wait(mtd, FL_READING);
  1090. if (status)
  1091. return status;
  1092. this->read_bufferram(mtd, 0, ONENAND_SPARERAM, oob_buf, 0, mtd->oobsize);
  1093. for (i = 0; i < mtd->oobsize; i++)
  1094. if (buf[i] != 0xFF && buf[i] != oob_buf[i])
  1095. return -EBADMSG;
  1096. return 0;
  1097. }
  1098. /**
  1099. * onenand_verify - [GENERIC] verify the chip contents after a write
  1100. * @param mtd MTD device structure
  1101. * @param buf the databuffer to verify
  1102. * @param addr offset to read from
  1103. * @param len number of bytes to read and compare
  1104. */
  1105. static int onenand_verify(struct mtd_info *mtd, const u_char *buf, loff_t addr, size_t len)
  1106. {
  1107. struct onenand_chip *this = mtd->priv;
  1108. void __iomem *dataram;
  1109. int ret = 0;
  1110. int thislen, column;
  1111. while (len != 0) {
  1112. thislen = min_t(int, this->writesize, len);
  1113. column = addr & (this->writesize - 1);
  1114. if (column + thislen > this->writesize)
  1115. thislen = this->writesize - column;
  1116. this->command(mtd, ONENAND_CMD_READ, addr, this->writesize);
  1117. onenand_update_bufferram(mtd, addr, 0);
  1118. ret = this->wait(mtd, FL_READING);
  1119. if (ret)
  1120. return ret;
  1121. onenand_update_bufferram(mtd, addr, 1);
  1122. dataram = this->base + ONENAND_DATARAM;
  1123. dataram += onenand_bufferram_offset(mtd, ONENAND_DATARAM);
  1124. if (memcmp(buf, dataram + column, thislen))
  1125. return -EBADMSG;
  1126. len -= thislen;
  1127. buf += thislen;
  1128. addr += thislen;
  1129. }
  1130. return 0;
  1131. }
  1132. #else
  1133. #define onenand_verify(...) (0)
  1134. #define onenand_verify_oob(...) (0)
  1135. #endif
  1136. #define NOTALIGNED(x) ((x & (this->subpagesize - 1)) != 0)
  1137. /**
  1138. * onenand_fill_auto_oob - [INTERN] oob auto-placement transfer
  1139. * @param mtd MTD device structure
  1140. * @param oob_buf oob buffer
  1141. * @param buf source address
  1142. * @param column oob offset to write to
  1143. * @param thislen oob length to write
  1144. */
  1145. static int onenand_fill_auto_oob(struct mtd_info *mtd, u_char *oob_buf,
  1146. const u_char *buf, int column, int thislen)
  1147. {
  1148. struct onenand_chip *this = mtd->priv;
  1149. struct nand_oobfree *free;
  1150. int writecol = column;
  1151. int writeend = column + thislen;
  1152. int lastgap = 0;
  1153. unsigned int i;
  1154. free = this->ecclayout->oobfree;
  1155. for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES && free->length; i++, free++) {
  1156. if (writecol >= lastgap)
  1157. writecol += free->offset - lastgap;
  1158. if (writeend >= lastgap)
  1159. writeend += free->offset - lastgap;
  1160. lastgap = free->offset + free->length;
  1161. }
  1162. free = this->ecclayout->oobfree;
  1163. for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES && free->length; i++, free++) {
  1164. int free_end = free->offset + free->length;
  1165. if (free->offset < writeend && free_end > writecol) {
  1166. int st = max_t(int,free->offset,writecol);
  1167. int ed = min_t(int,free_end,writeend);
  1168. int n = ed - st;
  1169. memcpy(oob_buf + st, buf, n);
  1170. buf += n;
  1171. } else if (column == 0)
  1172. break;
  1173. }
  1174. return 0;
  1175. }
  1176. /**
  1177. * onenand_write_ops_nolock - [OneNAND Interface] write main and/or out-of-band
  1178. * @param mtd MTD device structure
  1179. * @param to offset to write to
  1180. * @param ops oob operation description structure
  1181. *
  1182. * Write main and/or oob with ECC
  1183. */
  1184. static int onenand_write_ops_nolock(struct mtd_info *mtd, loff_t to,
  1185. struct mtd_oob_ops *ops)
  1186. {
  1187. struct onenand_chip *this = mtd->priv;
  1188. int written = 0, column, thislen, subpage;
  1189. int oobwritten = 0, oobcolumn, thisooblen, oobsize;
  1190. size_t len = ops->len;
  1191. size_t ooblen = ops->ooblen;
  1192. const u_char *buf = ops->datbuf;
  1193. const u_char *oob = ops->oobbuf;
  1194. u_char *oobbuf;
  1195. int ret = 0;
  1196. MTDDEBUG(MTD_DEBUG_LEVEL3, "onenand_write_ops_nolock: to = 0x%08x, len = %i\n", (unsigned int) to, (int) len);
  1197. /* Initialize retlen, in case of early exit */
  1198. ops->retlen = 0;
  1199. ops->oobretlen = 0;
  1200. /* Reject writes, which are not page aligned */
  1201. if (unlikely(NOTALIGNED(to) || NOTALIGNED(len))) {
  1202. printk(KERN_ERR "onenand_write_ops_nolock: Attempt to write not page aligned data\n");
  1203. return -EINVAL;
  1204. }
  1205. if (ops->mode == MTD_OPS_AUTO_OOB)
  1206. oobsize = this->ecclayout->oobavail;
  1207. else
  1208. oobsize = mtd->oobsize;
  1209. oobcolumn = to & (mtd->oobsize - 1);
  1210. column = to & (mtd->writesize - 1);
  1211. /* Loop until all data write */
  1212. while (written < len) {
  1213. u_char *wbuf = (u_char *) buf;
  1214. thislen = min_t(int, mtd->writesize - column, len - written);
  1215. thisooblen = min_t(int, oobsize - oobcolumn, ooblen - oobwritten);
  1216. this->command(mtd, ONENAND_CMD_BUFFERRAM, to, thislen);
  1217. /* Partial page write */
  1218. subpage = thislen < mtd->writesize;
  1219. if (subpage) {
  1220. memset(this->page_buf, 0xff, mtd->writesize);
  1221. memcpy(this->page_buf + column, buf, thislen);
  1222. wbuf = this->page_buf;
  1223. }
  1224. this->write_bufferram(mtd, to, ONENAND_DATARAM, wbuf, 0, mtd->writesize);
  1225. if (oob) {
  1226. oobbuf = this->oob_buf;
  1227. /* We send data to spare ram with oobsize
  1228. * * to prevent byte access */
  1229. memset(oobbuf, 0xff, mtd->oobsize);
  1230. if (ops->mode == MTD_OPS_AUTO_OOB)
  1231. onenand_fill_auto_oob(mtd, oobbuf, oob, oobcolumn, thisooblen);
  1232. else
  1233. memcpy(oobbuf + oobcolumn, oob, thisooblen);
  1234. oobwritten += thisooblen;
  1235. oob += thisooblen;
  1236. oobcolumn = 0;
  1237. } else
  1238. oobbuf = (u_char *) ffchars;
  1239. this->write_bufferram(mtd, 0, ONENAND_SPARERAM, oobbuf, 0, mtd->oobsize);
  1240. this->command(mtd, ONENAND_CMD_PROG, to, mtd->writesize);
  1241. ret = this->wait(mtd, FL_WRITING);
  1242. /* In partial page write we don't update bufferram */
  1243. onenand_update_bufferram(mtd, to, !ret && !subpage);
  1244. if (ONENAND_IS_2PLANE(this)) {
  1245. ONENAND_SET_BUFFERRAM1(this);
  1246. onenand_update_bufferram(mtd, to + this->writesize, !ret && !subpage);
  1247. }
  1248. if (ret) {
  1249. printk(KERN_ERR "onenand_write_ops_nolock: write filaed %d\n", ret);
  1250. break;
  1251. }
  1252. /* Only check verify write turn on */
  1253. ret = onenand_verify(mtd, buf, to, thislen);
  1254. if (ret) {
  1255. printk(KERN_ERR "onenand_write_ops_nolock: verify failed %d\n", ret);
  1256. break;
  1257. }
  1258. written += thislen;
  1259. if (written == len)
  1260. break;
  1261. column = 0;
  1262. to += thislen;
  1263. buf += thislen;
  1264. }
  1265. ops->retlen = written;
  1266. return ret;
  1267. }
  1268. /**
  1269. * onenand_write_oob_nolock - [INTERN] OneNAND write out-of-band
  1270. * @param mtd MTD device structure
  1271. * @param to offset to write to
  1272. * @param len number of bytes to write
  1273. * @param retlen pointer to variable to store the number of written bytes
  1274. * @param buf the data to write
  1275. * @param mode operation mode
  1276. *
  1277. * OneNAND write out-of-band
  1278. */
  1279. static int onenand_write_oob_nolock(struct mtd_info *mtd, loff_t to,
  1280. struct mtd_oob_ops *ops)
  1281. {
  1282. struct onenand_chip *this = mtd->priv;
  1283. int column, ret = 0, oobsize;
  1284. int written = 0, oobcmd;
  1285. u_char *oobbuf;
  1286. size_t len = ops->ooblen;
  1287. const u_char *buf = ops->oobbuf;
  1288. unsigned int mode = ops->mode;
  1289. to += ops->ooboffs;
  1290. MTDDEBUG(MTD_DEBUG_LEVEL3, "onenand_write_oob_nolock: to = 0x%08x, len = %i\n", (unsigned int) to, (int) len);
  1291. /* Initialize retlen, in case of early exit */
  1292. ops->oobretlen = 0;
  1293. if (mode == MTD_OPS_AUTO_OOB)
  1294. oobsize = this->ecclayout->oobavail;
  1295. else
  1296. oobsize = mtd->oobsize;
  1297. column = to & (mtd->oobsize - 1);
  1298. if (unlikely(column >= oobsize)) {
  1299. printk(KERN_ERR "onenand_write_oob_nolock: Attempted to start write outside oob\n");
  1300. return -EINVAL;
  1301. }
  1302. /* For compatibility with NAND: Do not allow write past end of page */
  1303. if (unlikely(column + len > oobsize)) {
  1304. printk(KERN_ERR "onenand_write_oob_nolock: "
  1305. "Attempt to write past end of page\n");
  1306. return -EINVAL;
  1307. }
  1308. /* Do not allow reads past end of device */
  1309. if (unlikely(to >= mtd->size ||
  1310. column + len > ((mtd->size >> this->page_shift) -
  1311. (to >> this->page_shift)) * oobsize)) {
  1312. printk(KERN_ERR "onenand_write_oob_nolock: Attempted to write past end of device\n");
  1313. return -EINVAL;
  1314. }
  1315. oobbuf = this->oob_buf;
  1316. oobcmd = ONENAND_IS_4KB_PAGE(this) ?
  1317. ONENAND_CMD_PROG : ONENAND_CMD_PROGOOB;
  1318. /* Loop until all data write */
  1319. while (written < len) {
  1320. int thislen = min_t(int, oobsize, len - written);
  1321. this->command(mtd, ONENAND_CMD_BUFFERRAM, to, mtd->oobsize);
  1322. /* We send data to spare ram with oobsize
  1323. * to prevent byte access */
  1324. memset(oobbuf, 0xff, mtd->oobsize);
  1325. if (mode == MTD_OPS_AUTO_OOB)
  1326. onenand_fill_auto_oob(mtd, oobbuf, buf, column, thislen);
  1327. else
  1328. memcpy(oobbuf + column, buf, thislen);
  1329. this->write_bufferram(mtd, 0, ONENAND_SPARERAM, oobbuf, 0, mtd->oobsize);
  1330. if (ONENAND_IS_4KB_PAGE(this)) {
  1331. /* Set main area of DataRAM to 0xff*/
  1332. memset(this->page_buf, 0xff, mtd->writesize);
  1333. this->write_bufferram(mtd, 0, ONENAND_DATARAM,
  1334. this->page_buf, 0, mtd->writesize);
  1335. }
  1336. this->command(mtd, oobcmd, to, mtd->oobsize);
  1337. onenand_update_bufferram(mtd, to, 0);
  1338. if (ONENAND_IS_2PLANE(this)) {
  1339. ONENAND_SET_BUFFERRAM1(this);
  1340. onenand_update_bufferram(mtd, to + this->writesize, 0);
  1341. }
  1342. ret = this->wait(mtd, FL_WRITING);
  1343. if (ret) {
  1344. printk(KERN_ERR "onenand_write_oob_nolock: write failed %d\n", ret);
  1345. break;
  1346. }
  1347. ret = onenand_verify_oob(mtd, oobbuf, to);
  1348. if (ret) {
  1349. printk(KERN_ERR "onenand_write_oob_nolock: verify failed %d\n", ret);
  1350. break;
  1351. }
  1352. written += thislen;
  1353. if (written == len)
  1354. break;
  1355. to += mtd->writesize;
  1356. buf += thislen;
  1357. column = 0;
  1358. }
  1359. ops->oobretlen = written;
  1360. return ret;
  1361. }
  1362. /**
  1363. * onenand_write - [MTD Interface] compability function for onenand_write_ecc
  1364. * @param mtd MTD device structure
  1365. * @param to offset to write to
  1366. * @param len number of bytes to write
  1367. * @param retlen pointer to variable to store the number of written bytes
  1368. * @param buf the data to write
  1369. *
  1370. * Write with ECC
  1371. */
  1372. int onenand_write(struct mtd_info *mtd, loff_t to, size_t len,
  1373. size_t * retlen, const u_char * buf)
  1374. {
  1375. struct mtd_oob_ops ops = {
  1376. .len = len,
  1377. .ooblen = 0,
  1378. .datbuf = (u_char *) buf,
  1379. .oobbuf = NULL,
  1380. };
  1381. int ret;
  1382. onenand_get_device(mtd, FL_WRITING);
  1383. ret = onenand_write_ops_nolock(mtd, to, &ops);
  1384. onenand_release_device(mtd);
  1385. *retlen = ops.retlen;
  1386. return ret;
  1387. }
  1388. /**
  1389. * onenand_write_oob - [MTD Interface] OneNAND write out-of-band
  1390. * @param mtd MTD device structure
  1391. * @param to offset to write to
  1392. * @param ops oob operation description structure
  1393. *
  1394. * OneNAND write main and/or out-of-band
  1395. */
  1396. int onenand_write_oob(struct mtd_info *mtd, loff_t to,
  1397. struct mtd_oob_ops *ops)
  1398. {
  1399. int ret;
  1400. switch (ops->mode) {
  1401. case MTD_OPS_PLACE_OOB:
  1402. case MTD_OPS_AUTO_OOB:
  1403. break;
  1404. case MTD_OPS_RAW:
  1405. /* Not implemented yet */
  1406. default:
  1407. return -EINVAL;
  1408. }
  1409. onenand_get_device(mtd, FL_WRITING);
  1410. if (ops->datbuf)
  1411. ret = onenand_write_ops_nolock(mtd, to, ops);
  1412. else
  1413. ret = onenand_write_oob_nolock(mtd, to, ops);
  1414. onenand_release_device(mtd);
  1415. return ret;
  1416. }
  1417. /**
  1418. * onenand_block_isbad_nolock - [GENERIC] Check if a block is marked bad
  1419. * @param mtd MTD device structure
  1420. * @param ofs offset from device start
  1421. * @param allowbbt 1, if its allowed to access the bbt area
  1422. *
  1423. * Check, if the block is bad, Either by reading the bad block table or
  1424. * calling of the scan function.
  1425. */
  1426. static int onenand_block_isbad_nolock(struct mtd_info *mtd, loff_t ofs, int allowbbt)
  1427. {
  1428. struct onenand_chip *this = mtd->priv;
  1429. struct bbm_info *bbm = this->bbm;
  1430. /* Return info from the table */
  1431. return bbm->isbad_bbt(mtd, ofs, allowbbt);
  1432. }
  1433. /**
  1434. * onenand_erase - [MTD Interface] erase block(s)
  1435. * @param mtd MTD device structure
  1436. * @param instr erase instruction
  1437. *
  1438. * Erase one ore more blocks
  1439. */
  1440. int onenand_erase(struct mtd_info *mtd, struct erase_info *instr)
  1441. {
  1442. struct onenand_chip *this = mtd->priv;
  1443. unsigned int block_size;
  1444. loff_t addr = instr->addr;
  1445. unsigned int len = instr->len;
  1446. int ret = 0, i;
  1447. struct mtd_erase_region_info *region = NULL;
  1448. unsigned int region_end = 0;
  1449. MTDDEBUG(MTD_DEBUG_LEVEL3, "onenand_erase: start = 0x%08x, len = %i\n",
  1450. (unsigned int) addr, len);
  1451. if (FLEXONENAND(this)) {
  1452. /* Find the eraseregion of this address */
  1453. i = flexonenand_region(mtd, addr);
  1454. region = &mtd->eraseregions[i];
  1455. block_size = region->erasesize;
  1456. region_end = region->offset
  1457. + region->erasesize * region->numblocks;
  1458. /* Start address within region must align on block boundary.
  1459. * Erase region's start offset is always block start address.
  1460. */
  1461. if (unlikely((addr - region->offset) & (block_size - 1))) {
  1462. MTDDEBUG(MTD_DEBUG_LEVEL0, "onenand_erase:"
  1463. " Unaligned address\n");
  1464. return -EINVAL;
  1465. }
  1466. } else {
  1467. block_size = 1 << this->erase_shift;
  1468. /* Start address must align on block boundary */
  1469. if (unlikely(addr & (block_size - 1))) {
  1470. MTDDEBUG(MTD_DEBUG_LEVEL0, "onenand_erase:"
  1471. "Unaligned address\n");
  1472. return -EINVAL;
  1473. }
  1474. }
  1475. /* Length must align on block boundary */
  1476. if (unlikely(len & (block_size - 1))) {
  1477. MTDDEBUG (MTD_DEBUG_LEVEL0,
  1478. "onenand_erase: Length not block aligned\n");
  1479. return -EINVAL;
  1480. }
  1481. /* Grab the lock and see if the device is available */
  1482. onenand_get_device(mtd, FL_ERASING);
  1483. /* Loop throught the pages */
  1484. instr->state = MTD_ERASING;
  1485. while (len) {
  1486. /* Check if we have a bad block, we do not erase bad blocks */
  1487. if (instr->priv == 0 && onenand_block_isbad_nolock(mtd, addr, 0)) {
  1488. printk(KERN_WARNING "onenand_erase: attempt to erase"
  1489. " a bad block at addr 0x%08x\n",
  1490. (unsigned int) addr);
  1491. instr->state = MTD_ERASE_FAILED;
  1492. goto erase_exit;
  1493. }
  1494. this->command(mtd, ONENAND_CMD_ERASE, addr, block_size);
  1495. onenand_invalidate_bufferram(mtd, addr, block_size);
  1496. ret = this->wait(mtd, FL_ERASING);
  1497. /* Check, if it is write protected */
  1498. if (ret) {
  1499. if (ret == -EPERM)
  1500. MTDDEBUG (MTD_DEBUG_LEVEL0, "onenand_erase: "
  1501. "Device is write protected!!!\n");
  1502. else
  1503. MTDDEBUG (MTD_DEBUG_LEVEL0, "onenand_erase: "
  1504. "Failed erase, block %d\n",
  1505. onenand_block(this, addr));
  1506. instr->state = MTD_ERASE_FAILED;
  1507. instr->fail_addr = addr;
  1508. goto erase_exit;
  1509. }
  1510. len -= block_size;
  1511. addr += block_size;
  1512. if (addr == region_end) {
  1513. if (!len)
  1514. break;
  1515. region++;
  1516. block_size = region->erasesize;
  1517. region_end = region->offset
  1518. + region->erasesize * region->numblocks;
  1519. if (len & (block_size - 1)) {
  1520. /* This has been checked at MTD
  1521. * partitioning level. */
  1522. printk("onenand_erase: Unaligned address\n");
  1523. goto erase_exit;
  1524. }
  1525. }
  1526. }
  1527. instr->state = MTD_ERASE_DONE;
  1528. erase_exit:
  1529. ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
  1530. /* Do call back function */
  1531. if (!ret)
  1532. mtd_erase_callback(instr);
  1533. /* Deselect and wake up anyone waiting on the device */
  1534. onenand_release_device(mtd);
  1535. return ret;
  1536. }
  1537. /**
  1538. * onenand_sync - [MTD Interface] sync
  1539. * @param mtd MTD device structure
  1540. *
  1541. * Sync is actually a wait for chip ready function
  1542. */
  1543. void onenand_sync(struct mtd_info *mtd)
  1544. {
  1545. MTDDEBUG (MTD_DEBUG_LEVEL3, "onenand_sync: called\n");
  1546. /* Grab the lock and see if the device is available */
  1547. onenand_get_device(mtd, FL_SYNCING);
  1548. /* Release it and go back */
  1549. onenand_release_device(mtd);
  1550. }
  1551. /**
  1552. * onenand_block_isbad - [MTD Interface] Check whether the block at the given offset is bad
  1553. * @param mtd MTD device structure
  1554. * @param ofs offset relative to mtd start
  1555. *
  1556. * Check whether the block is bad
  1557. */
  1558. int onenand_block_isbad(struct mtd_info *mtd, loff_t ofs)
  1559. {
  1560. int ret;
  1561. /* Check for invalid offset */
  1562. if (ofs > mtd->size)
  1563. return -EINVAL;
  1564. onenand_get_device(mtd, FL_READING);
  1565. ret = onenand_block_isbad_nolock(mtd,ofs, 0);
  1566. onenand_release_device(mtd);
  1567. return ret;
  1568. }
  1569. /**
  1570. * onenand_default_block_markbad - [DEFAULT] mark a block bad
  1571. * @param mtd MTD device structure
  1572. * @param ofs offset from device start
  1573. *
  1574. * This is the default implementation, which can be overridden by
  1575. * a hardware specific driver.
  1576. */
  1577. static int onenand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
  1578. {
  1579. struct onenand_chip *this = mtd->priv;
  1580. struct bbm_info *bbm = this->bbm;
  1581. u_char buf[2] = {0, 0};
  1582. struct mtd_oob_ops ops = {
  1583. .mode = MTD_OPS_PLACE_OOB,
  1584. .ooblen = 2,
  1585. .oobbuf = buf,
  1586. .ooboffs = 0,
  1587. };
  1588. int block;
  1589. /* Get block number */
  1590. block = onenand_block(this, ofs);
  1591. if (bbm->bbt)
  1592. bbm->bbt[block >> 2] |= 0x01 << ((block & 0x03) << 1);
  1593. /* We write two bytes, so we dont have to mess with 16 bit access */
  1594. ofs += mtd->oobsize + (bbm->badblockpos & ~0x01);
  1595. return onenand_write_oob_nolock(mtd, ofs, &ops);
  1596. }
  1597. /**
  1598. * onenand_block_markbad - [MTD Interface] Mark the block at the given offset as bad
  1599. * @param mtd MTD device structure
  1600. * @param ofs offset relative to mtd start
  1601. *
  1602. * Mark the block as bad
  1603. */
  1604. int onenand_block_markbad(struct mtd_info *mtd, loff_t ofs)
  1605. {
  1606. int ret;
  1607. ret = onenand_block_isbad(mtd, ofs);
  1608. if (ret) {
  1609. /* If it was bad already, return success and do nothing */
  1610. if (ret > 0)
  1611. return 0;
  1612. return ret;
  1613. }
  1614. ret = mtd_block_markbad(mtd, ofs);
  1615. return ret;
  1616. }
  1617. /**
  1618. * onenand_do_lock_cmd - [OneNAND Interface] Lock or unlock block(s)
  1619. * @param mtd MTD device structure
  1620. * @param ofs offset relative to mtd start
  1621. * @param len number of bytes to lock or unlock
  1622. * @param cmd lock or unlock command
  1623. *
  1624. * Lock or unlock one or more blocks
  1625. */
  1626. static int onenand_do_lock_cmd(struct mtd_info *mtd, loff_t ofs, size_t len, int cmd)
  1627. {
  1628. struct onenand_chip *this = mtd->priv;
  1629. int start, end, block, value, status;
  1630. start = onenand_block(this, ofs);
  1631. end = onenand_block(this, ofs + len);
  1632. /* Continuous lock scheme */
  1633. if (this->options & ONENAND_HAS_CONT_LOCK) {
  1634. /* Set start block address */
  1635. this->write_word(start,
  1636. this->base + ONENAND_REG_START_BLOCK_ADDRESS);
  1637. /* Set end block address */
  1638. this->write_word(end - 1,
  1639. this->base + ONENAND_REG_END_BLOCK_ADDRESS);
  1640. /* Write unlock command */
  1641. this->command(mtd, cmd, 0, 0);
  1642. /* There's no return value */
  1643. this->wait(mtd, FL_UNLOCKING);
  1644. /* Sanity check */
  1645. while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
  1646. & ONENAND_CTRL_ONGO)
  1647. continue;
  1648. /* Check lock status */
  1649. status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
  1650. if (!(status & ONENAND_WP_US))
  1651. printk(KERN_ERR "wp status = 0x%x\n", status);
  1652. return 0;
  1653. }
  1654. /* Block lock scheme */
  1655. for (block = start; block < end; block++) {
  1656. /* Set block address */
  1657. value = onenand_block_address(this, block);
  1658. this->write_word(value, this->base + ONENAND_REG_START_ADDRESS1);
  1659. /* Select DataRAM for DDP */
  1660. value = onenand_bufferram_address(this, block);
  1661. this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
  1662. /* Set start block address */
  1663. this->write_word(block,
  1664. this->base + ONENAND_REG_START_BLOCK_ADDRESS);
  1665. /* Write unlock command */
  1666. this->command(mtd, ONENAND_CMD_UNLOCK, 0, 0);
  1667. /* There's no return value */
  1668. this->wait(mtd, FL_UNLOCKING);
  1669. /* Sanity check */
  1670. while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
  1671. & ONENAND_CTRL_ONGO)
  1672. continue;
  1673. /* Check lock status */
  1674. status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
  1675. if (!(status & ONENAND_WP_US))
  1676. printk(KERN_ERR "block = %d, wp status = 0x%x\n",
  1677. block, status);
  1678. }
  1679. return 0;
  1680. }
  1681. #ifdef ONENAND_LINUX
  1682. /**
  1683. * onenand_lock - [MTD Interface] Lock block(s)
  1684. * @param mtd MTD device structure
  1685. * @param ofs offset relative to mtd start
  1686. * @param len number of bytes to unlock
  1687. *
  1688. * Lock one or more blocks
  1689. */
  1690. static int onenand_lock(struct mtd_info *mtd, loff_t ofs, size_t len)
  1691. {
  1692. int ret;
  1693. onenand_get_device(mtd, FL_LOCKING);
  1694. ret = onenand_do_lock_cmd(mtd, ofs, len, ONENAND_CMD_LOCK);
  1695. onenand_release_device(mtd);
  1696. return ret;
  1697. }
  1698. /**
  1699. * onenand_unlock - [MTD Interface] Unlock block(s)
  1700. * @param mtd MTD device structure
  1701. * @param ofs offset relative to mtd start
  1702. * @param len number of bytes to unlock
  1703. *
  1704. * Unlock one or more blocks
  1705. */
  1706. static int onenand_unlock(struct mtd_info *mtd, loff_t ofs, size_t len)
  1707. {
  1708. int ret;
  1709. onenand_get_device(mtd, FL_LOCKING);
  1710. ret = onenand_do_lock_cmd(mtd, ofs, len, ONENAND_CMD_UNLOCK);
  1711. onenand_release_device(mtd);
  1712. return ret;
  1713. }
  1714. #endif
  1715. /**
  1716. * onenand_check_lock_status - [OneNAND Interface] Check lock status
  1717. * @param this onenand chip data structure
  1718. *
  1719. * Check lock status
  1720. */
  1721. static int onenand_check_lock_status(struct onenand_chip *this)
  1722. {
  1723. unsigned int value, block, status;
  1724. unsigned int end;
  1725. end = this->chipsize >> this->erase_shift;
  1726. for (block = 0; block < end; block++) {
  1727. /* Set block address */
  1728. value = onenand_block_address(this, block);
  1729. this->write_word(value, this->base + ONENAND_REG_START_ADDRESS1);
  1730. /* Select DataRAM for DDP */
  1731. value = onenand_bufferram_address(this, block);
  1732. this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
  1733. /* Set start block address */
  1734. this->write_word(block, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
  1735. /* Check lock status */
  1736. status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
  1737. if (!(status & ONENAND_WP_US)) {
  1738. printk(KERN_ERR "block = %d, wp status = 0x%x\n", block, status);
  1739. return 0;
  1740. }
  1741. }
  1742. return 1;
  1743. }
  1744. /**
  1745. * onenand_unlock_all - [OneNAND Interface] unlock all blocks
  1746. * @param mtd MTD device structure
  1747. *
  1748. * Unlock all blocks
  1749. */
  1750. static void onenand_unlock_all(struct mtd_info *mtd)
  1751. {
  1752. struct onenand_chip *this = mtd->priv;
  1753. loff_t ofs = 0;
  1754. size_t len = mtd->size;
  1755. if (this->options & ONENAND_HAS_UNLOCK_ALL) {
  1756. /* Set start block address */
  1757. this->write_word(0, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
  1758. /* Write unlock command */
  1759. this->command(mtd, ONENAND_CMD_UNLOCK_ALL, 0, 0);
  1760. /* There's no return value */
  1761. this->wait(mtd, FL_LOCKING);
  1762. /* Sanity check */
  1763. while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
  1764. & ONENAND_CTRL_ONGO)
  1765. continue;
  1766. /* Check lock status */
  1767. if (onenand_check_lock_status(this))
  1768. return;
  1769. /* Workaround for all block unlock in DDP */
  1770. if (ONENAND_IS_DDP(this) && !FLEXONENAND(this)) {
  1771. /* All blocks on another chip */
  1772. ofs = this->chipsize >> 1;
  1773. len = this->chipsize >> 1;
  1774. }
  1775. }
  1776. onenand_do_lock_cmd(mtd, ofs, len, ONENAND_CMD_UNLOCK);
  1777. }
  1778. /**
  1779. * onenand_check_features - Check and set OneNAND features
  1780. * @param mtd MTD data structure
  1781. *
  1782. * Check and set OneNAND features
  1783. * - lock scheme
  1784. * - two plane
  1785. */
  1786. static void onenand_check_features(struct mtd_info *mtd)
  1787. {
  1788. struct onenand_chip *this = mtd->priv;
  1789. unsigned int density, process;
  1790. /* Lock scheme depends on density and process */
  1791. density = onenand_get_density(this->device_id);
  1792. process = this->version_id >> ONENAND_VERSION_PROCESS_SHIFT;
  1793. /* Lock scheme */
  1794. switch (density) {
  1795. case ONENAND_DEVICE_DENSITY_4Gb:
  1796. if (ONENAND_IS_DDP(this))
  1797. this->options |= ONENAND_HAS_2PLANE;
  1798. else
  1799. this->options |= ONENAND_HAS_4KB_PAGE;
  1800. case ONENAND_DEVICE_DENSITY_2Gb:
  1801. /* 2Gb DDP don't have 2 plane */
  1802. if (!ONENAND_IS_DDP(this))
  1803. this->options |= ONENAND_HAS_2PLANE;
  1804. this->options |= ONENAND_HAS_UNLOCK_ALL;
  1805. case ONENAND_DEVICE_DENSITY_1Gb:
  1806. /* A-Die has all block unlock */
  1807. if (process)
  1808. this->options |= ONENAND_HAS_UNLOCK_ALL;
  1809. break;
  1810. default:
  1811. /* Some OneNAND has continuous lock scheme */
  1812. if (!process)
  1813. this->options |= ONENAND_HAS_CONT_LOCK;
  1814. break;
  1815. }
  1816. if (ONENAND_IS_MLC(this))
  1817. this->options |= ONENAND_HAS_4KB_PAGE;
  1818. if (ONENAND_IS_4KB_PAGE(this))
  1819. this->options &= ~ONENAND_HAS_2PLANE;
  1820. if (FLEXONENAND(this)) {
  1821. this->options &= ~ONENAND_HAS_CONT_LOCK;
  1822. this->options |= ONENAND_HAS_UNLOCK_ALL;
  1823. }
  1824. if (this->options & ONENAND_HAS_CONT_LOCK)
  1825. printk(KERN_DEBUG "Lock scheme is Continuous Lock\n");
  1826. if (this->options & ONENAND_HAS_UNLOCK_ALL)
  1827. printk(KERN_DEBUG "Chip support all block unlock\n");
  1828. if (this->options & ONENAND_HAS_2PLANE)
  1829. printk(KERN_DEBUG "Chip has 2 plane\n");
  1830. if (this->options & ONENAND_HAS_4KB_PAGE)
  1831. printk(KERN_DEBUG "Chip has 4KiB pagesize\n");
  1832. }
  1833. /**
  1834. * onenand_print_device_info - Print device ID
  1835. * @param device device ID
  1836. *
  1837. * Print device ID
  1838. */
  1839. char *onenand_print_device_info(int device, int version)
  1840. {
  1841. int vcc, demuxed, ddp, density, flexonenand;
  1842. char *dev_info = malloc(80);
  1843. char *p = dev_info;
  1844. vcc = device & ONENAND_DEVICE_VCC_MASK;
  1845. demuxed = device & ONENAND_DEVICE_IS_DEMUX;
  1846. ddp = device & ONENAND_DEVICE_IS_DDP;
  1847. density = onenand_get_density(device);
  1848. flexonenand = device & DEVICE_IS_FLEXONENAND;
  1849. p += sprintf(dev_info, "%s%sOneNAND%s %dMB %sV 16-bit (0x%02x)",
  1850. demuxed ? "" : "Muxed ",
  1851. flexonenand ? "Flex-" : "",
  1852. ddp ? "(DDP)" : "",
  1853. (16 << density), vcc ? "2.65/3.3" : "1.8", device);
  1854. sprintf(p, "\nOneNAND version = 0x%04x", version);
  1855. printk("%s\n", dev_info);
  1856. return dev_info;
  1857. }
  1858. static const struct onenand_manufacturers onenand_manuf_ids[] = {
  1859. {ONENAND_MFR_NUMONYX, "Numonyx"},
  1860. {ONENAND_MFR_SAMSUNG, "Samsung"},
  1861. };
  1862. /**
  1863. * onenand_check_maf - Check manufacturer ID
  1864. * @param manuf manufacturer ID
  1865. *
  1866. * Check manufacturer ID
  1867. */
  1868. static int onenand_check_maf(int manuf)
  1869. {
  1870. int size = ARRAY_SIZE(onenand_manuf_ids);
  1871. int i;
  1872. #ifdef ONENAND_DEBUG
  1873. char *name;
  1874. #endif
  1875. for (i = 0; i < size; i++)
  1876. if (manuf == onenand_manuf_ids[i].id)
  1877. break;
  1878. #ifdef ONENAND_DEBUG
  1879. if (i < size)
  1880. name = onenand_manuf_ids[i].name;
  1881. else
  1882. name = "Unknown";
  1883. printk(KERN_DEBUG "OneNAND Manufacturer: %s (0x%0x)\n", name, manuf);
  1884. #endif
  1885. return i == size;
  1886. }
  1887. /**
  1888. * flexonenand_get_boundary - Reads the SLC boundary
  1889. * @param onenand_info - onenand info structure
  1890. *
  1891. * Fill up boundary[] field in onenand_chip
  1892. **/
  1893. static int flexonenand_get_boundary(struct mtd_info *mtd)
  1894. {
  1895. struct onenand_chip *this = mtd->priv;
  1896. unsigned int die, bdry;
  1897. int syscfg, locked;
  1898. /* Disable ECC */
  1899. syscfg = this->read_word(this->base + ONENAND_REG_SYS_CFG1);
  1900. this->write_word((syscfg | 0x0100), this->base + ONENAND_REG_SYS_CFG1);
  1901. for (die = 0; die < this->dies; die++) {
  1902. this->command(mtd, FLEXONENAND_CMD_PI_ACCESS, die, 0);
  1903. this->wait(mtd, FL_SYNCING);
  1904. this->command(mtd, FLEXONENAND_CMD_READ_PI, die, 0);
  1905. this->wait(mtd, FL_READING);
  1906. bdry = this->read_word(this->base + ONENAND_DATARAM);
  1907. if ((bdry >> FLEXONENAND_PI_UNLOCK_SHIFT) == 3)
  1908. locked = 0;
  1909. else
  1910. locked = 1;
  1911. this->boundary[die] = bdry & FLEXONENAND_PI_MASK;
  1912. this->command(mtd, ONENAND_CMD_RESET, 0, 0);
  1913. this->wait(mtd, FL_RESETING);
  1914. printk(KERN_INFO "Die %d boundary: %d%s\n", die,
  1915. this->boundary[die], locked ? "(Locked)" : "(Unlocked)");
  1916. }
  1917. /* Enable ECC */
  1918. this->write_word(syscfg, this->base + ONENAND_REG_SYS_CFG1);
  1919. return 0;
  1920. }
  1921. /**
  1922. * flexonenand_get_size - Fill up fields in onenand_chip and mtd_info
  1923. * boundary[], diesize[], mtd->size, mtd->erasesize,
  1924. * mtd->eraseregions
  1925. * @param mtd - MTD device structure
  1926. */
  1927. static void flexonenand_get_size(struct mtd_info *mtd)
  1928. {
  1929. struct onenand_chip *this = mtd->priv;
  1930. int die, i, eraseshift, density;
  1931. int blksperdie, maxbdry;
  1932. loff_t ofs;
  1933. density = onenand_get_density(this->device_id);
  1934. blksperdie = ((loff_t)(16 << density) << 20) >> (this->erase_shift);
  1935. blksperdie >>= ONENAND_IS_DDP(this) ? 1 : 0;
  1936. maxbdry = blksperdie - 1;
  1937. eraseshift = this->erase_shift - 1;
  1938. mtd->numeraseregions = this->dies << 1;
  1939. /* This fills up the device boundary */
  1940. flexonenand_get_boundary(mtd);
  1941. die = 0;
  1942. ofs = 0;
  1943. i = -1;
  1944. for (; die < this->dies; die++) {
  1945. if (!die || this->boundary[die-1] != maxbdry) {
  1946. i++;
  1947. mtd->eraseregions[i].offset = ofs;
  1948. mtd->eraseregions[i].erasesize = 1 << eraseshift;
  1949. mtd->eraseregions[i].numblocks =
  1950. this->boundary[die] + 1;
  1951. ofs += mtd->eraseregions[i].numblocks << eraseshift;
  1952. eraseshift++;
  1953. } else {
  1954. mtd->numeraseregions -= 1;
  1955. mtd->eraseregions[i].numblocks +=
  1956. this->boundary[die] + 1;
  1957. ofs += (this->boundary[die] + 1) << (eraseshift - 1);
  1958. }
  1959. if (this->boundary[die] != maxbdry) {
  1960. i++;
  1961. mtd->eraseregions[i].offset = ofs;
  1962. mtd->eraseregions[i].erasesize = 1 << eraseshift;
  1963. mtd->eraseregions[i].numblocks = maxbdry ^
  1964. this->boundary[die];
  1965. ofs += mtd->eraseregions[i].numblocks << eraseshift;
  1966. eraseshift--;
  1967. } else
  1968. mtd->numeraseregions -= 1;
  1969. }
  1970. /* Expose MLC erase size except when all blocks are SLC */
  1971. mtd->erasesize = 1 << this->erase_shift;
  1972. if (mtd->numeraseregions == 1)
  1973. mtd->erasesize >>= 1;
  1974. printk(KERN_INFO "Device has %d eraseregions\n", mtd->numeraseregions);
  1975. for (i = 0; i < mtd->numeraseregions; i++)
  1976. printk(KERN_INFO "[offset: 0x%08llx, erasesize: 0x%05x,"
  1977. " numblocks: %04u]\n", mtd->eraseregions[i].offset,
  1978. mtd->eraseregions[i].erasesize,
  1979. mtd->eraseregions[i].numblocks);
  1980. for (die = 0, mtd->size = 0; die < this->dies; die++) {
  1981. this->diesize[die] = (loff_t) (blksperdie << this->erase_shift);
  1982. this->diesize[die] -= (loff_t) (this->boundary[die] + 1)
  1983. << (this->erase_shift - 1);
  1984. mtd->size += this->diesize[die];
  1985. }
  1986. }
  1987. /**
  1988. * flexonenand_check_blocks_erased - Check if blocks are erased
  1989. * @param mtd_info - mtd info structure
  1990. * @param start - first erase block to check
  1991. * @param end - last erase block to check
  1992. *
  1993. * Converting an unerased block from MLC to SLC
  1994. * causes byte values to change. Since both data and its ECC
  1995. * have changed, reads on the block give uncorrectable error.
  1996. * This might lead to the block being detected as bad.
  1997. *
  1998. * Avoid this by ensuring that the block to be converted is
  1999. * erased.
  2000. */
  2001. static int flexonenand_check_blocks_erased(struct mtd_info *mtd,
  2002. int start, int end)
  2003. {
  2004. struct onenand_chip *this = mtd->priv;
  2005. int i, ret;
  2006. int block;
  2007. struct mtd_oob_ops ops = {
  2008. .mode = MTD_OPS_PLACE_OOB,
  2009. .ooboffs = 0,
  2010. .ooblen = mtd->oobsize,
  2011. .datbuf = NULL,
  2012. .oobbuf = this->oob_buf,
  2013. };
  2014. loff_t addr;
  2015. printk(KERN_DEBUG "Check blocks from %d to %d\n", start, end);
  2016. for (block = start; block <= end; block++) {
  2017. addr = flexonenand_addr(this, block);
  2018. if (onenand_block_isbad_nolock(mtd, addr, 0))
  2019. continue;
  2020. /*
  2021. * Since main area write results in ECC write to spare,
  2022. * it is sufficient to check only ECC bytes for change.
  2023. */
  2024. ret = onenand_read_oob_nolock(mtd, addr, &ops);
  2025. if (ret)
  2026. return ret;
  2027. for (i = 0; i < mtd->oobsize; i++)
  2028. if (this->oob_buf[i] != 0xff)
  2029. break;
  2030. if (i != mtd->oobsize) {
  2031. printk(KERN_WARNING "Block %d not erased.\n", block);
  2032. return 1;
  2033. }
  2034. }
  2035. return 0;
  2036. }
  2037. /**
  2038. * flexonenand_set_boundary - Writes the SLC boundary
  2039. * @param mtd - mtd info structure
  2040. */
  2041. int flexonenand_set_boundary(struct mtd_info *mtd, int die,
  2042. int boundary, int lock)
  2043. {
  2044. struct onenand_chip *this = mtd->priv;
  2045. int ret, density, blksperdie, old, new, thisboundary;
  2046. loff_t addr;
  2047. if (die >= this->dies)
  2048. return -EINVAL;
  2049. if (boundary == this->boundary[die])
  2050. return 0;
  2051. density = onenand_get_density(this->device_id);
  2052. blksperdie = ((16 << density) << 20) >> this->erase_shift;
  2053. blksperdie >>= ONENAND_IS_DDP(this) ? 1 : 0;
  2054. if (boundary >= blksperdie) {
  2055. printk("flexonenand_set_boundary:"
  2056. "Invalid boundary value. "
  2057. "Boundary not changed.\n");
  2058. return -EINVAL;
  2059. }
  2060. /* Check if converting blocks are erased */
  2061. old = this->boundary[die] + (die * this->density_mask);
  2062. new = boundary + (die * this->density_mask);
  2063. ret = flexonenand_check_blocks_erased(mtd, min(old, new)
  2064. + 1, max(old, new));
  2065. if (ret) {
  2066. printk(KERN_ERR "flexonenand_set_boundary: Please erase blocks before boundary change\n");
  2067. return ret;
  2068. }
  2069. this->command(mtd, FLEXONENAND_CMD_PI_ACCESS, die, 0);
  2070. this->wait(mtd, FL_SYNCING);
  2071. /* Check is boundary is locked */
  2072. this->command(mtd, FLEXONENAND_CMD_READ_PI, die, 0);
  2073. ret = this->wait(mtd, FL_READING);
  2074. thisboundary = this->read_word(this->base + ONENAND_DATARAM);
  2075. if ((thisboundary >> FLEXONENAND_PI_UNLOCK_SHIFT) != 3) {
  2076. printk(KERN_ERR "flexonenand_set_boundary: boundary locked\n");
  2077. goto out;
  2078. }
  2079. printk(KERN_INFO "flexonenand_set_boundary: Changing die %d boundary: %d%s\n",
  2080. die, boundary, lock ? "(Locked)" : "(Unlocked)");
  2081. boundary &= FLEXONENAND_PI_MASK;
  2082. boundary |= lock ? 0 : (3 << FLEXONENAND_PI_UNLOCK_SHIFT);
  2083. addr = die ? this->diesize[0] : 0;
  2084. this->command(mtd, ONENAND_CMD_ERASE, addr, 0);
  2085. ret = this->wait(mtd, FL_ERASING);
  2086. if (ret) {
  2087. printk("flexonenand_set_boundary:"
  2088. "Failed PI erase for Die %d\n", die);
  2089. goto out;
  2090. }
  2091. this->write_word(boundary, this->base + ONENAND_DATARAM);
  2092. this->command(mtd, ONENAND_CMD_PROG, addr, 0);
  2093. ret = this->wait(mtd, FL_WRITING);
  2094. if (ret) {
  2095. printk("flexonenand_set_boundary:"
  2096. "Failed PI write for Die %d\n", die);
  2097. goto out;
  2098. }
  2099. this->command(mtd, FLEXONENAND_CMD_PI_UPDATE, die, 0);
  2100. ret = this->wait(mtd, FL_WRITING);
  2101. out:
  2102. this->write_word(ONENAND_CMD_RESET, this->base + ONENAND_REG_COMMAND);
  2103. this->wait(mtd, FL_RESETING);
  2104. if (!ret)
  2105. /* Recalculate device size on boundary change*/
  2106. flexonenand_get_size(mtd);
  2107. return ret;
  2108. }
  2109. /**
  2110. * onenand_chip_probe - [OneNAND Interface] Probe the OneNAND chip
  2111. * @param mtd MTD device structure
  2112. *
  2113. * OneNAND detection method:
  2114. * Compare the the values from command with ones from register
  2115. */
  2116. static int onenand_chip_probe(struct mtd_info *mtd)
  2117. {
  2118. struct onenand_chip *this = mtd->priv;
  2119. int bram_maf_id, bram_dev_id, maf_id, dev_id;
  2120. int syscfg;
  2121. /* Save system configuration 1 */
  2122. syscfg = this->read_word(this->base + ONENAND_REG_SYS_CFG1);
  2123. /* Clear Sync. Burst Read mode to read BootRAM */
  2124. this->write_word((syscfg & ~ONENAND_SYS_CFG1_SYNC_READ),
  2125. this->base + ONENAND_REG_SYS_CFG1);
  2126. /* Send the command for reading device ID from BootRAM */
  2127. this->write_word(ONENAND_CMD_READID, this->base + ONENAND_BOOTRAM);
  2128. /* Read manufacturer and device IDs from BootRAM */
  2129. bram_maf_id = this->read_word(this->base + ONENAND_BOOTRAM + 0x0);
  2130. bram_dev_id = this->read_word(this->base + ONENAND_BOOTRAM + 0x2);
  2131. /* Reset OneNAND to read default register values */
  2132. this->write_word(ONENAND_CMD_RESET, this->base + ONENAND_BOOTRAM);
  2133. /* Wait reset */
  2134. this->wait(mtd, FL_RESETING);
  2135. /* Restore system configuration 1 */
  2136. this->write_word(syscfg, this->base + ONENAND_REG_SYS_CFG1);
  2137. /* Check manufacturer ID */
  2138. if (onenand_check_maf(bram_maf_id))
  2139. return -ENXIO;
  2140. /* Read manufacturer and device IDs from Register */
  2141. maf_id = this->read_word(this->base + ONENAND_REG_MANUFACTURER_ID);
  2142. dev_id = this->read_word(this->base + ONENAND_REG_DEVICE_ID);
  2143. /* Check OneNAND device */
  2144. if (maf_id != bram_maf_id || dev_id != bram_dev_id)
  2145. return -ENXIO;
  2146. return 0;
  2147. }
  2148. /**
  2149. * onenand_probe - [OneNAND Interface] Probe the OneNAND device
  2150. * @param mtd MTD device structure
  2151. *
  2152. * OneNAND detection method:
  2153. * Compare the the values from command with ones from register
  2154. */
  2155. int onenand_probe(struct mtd_info *mtd)
  2156. {
  2157. struct onenand_chip *this = mtd->priv;
  2158. int dev_id, ver_id;
  2159. int density;
  2160. int ret;
  2161. ret = this->chip_probe(mtd);
  2162. if (ret)
  2163. return ret;
  2164. /* Read device IDs from Register */
  2165. dev_id = this->read_word(this->base + ONENAND_REG_DEVICE_ID);
  2166. ver_id = this->read_word(this->base + ONENAND_REG_VERSION_ID);
  2167. this->technology = this->read_word(this->base + ONENAND_REG_TECHNOLOGY);
  2168. /* Flash device information */
  2169. mtd->name = onenand_print_device_info(dev_id, ver_id);
  2170. this->device_id = dev_id;
  2171. this->version_id = ver_id;
  2172. /* Check OneNAND features */
  2173. onenand_check_features(mtd);
  2174. density = onenand_get_density(dev_id);
  2175. if (FLEXONENAND(this)) {
  2176. this->dies = ONENAND_IS_DDP(this) ? 2 : 1;
  2177. /* Maximum possible erase regions */
  2178. mtd->numeraseregions = this->dies << 1;
  2179. mtd->eraseregions = malloc(sizeof(struct mtd_erase_region_info)
  2180. * (this->dies << 1));
  2181. if (!mtd->eraseregions)
  2182. return -ENOMEM;
  2183. }
  2184. /*
  2185. * For Flex-OneNAND, chipsize represents maximum possible device size.
  2186. * mtd->size represents the actual device size.
  2187. */
  2188. this->chipsize = (16 << density) << 20;
  2189. /* OneNAND page size & block size */
  2190. /* The data buffer size is equal to page size */
  2191. mtd->writesize =
  2192. this->read_word(this->base + ONENAND_REG_DATA_BUFFER_SIZE);
  2193. /* We use the full BufferRAM */
  2194. if (ONENAND_IS_4KB_PAGE(this))
  2195. mtd->writesize <<= 1;
  2196. mtd->oobsize = mtd->writesize >> 5;
  2197. /* Pagers per block is always 64 in OneNAND */
  2198. mtd->erasesize = mtd->writesize << 6;
  2199. /*
  2200. * Flex-OneNAND SLC area has 64 pages per block.
  2201. * Flex-OneNAND MLC area has 128 pages per block.
  2202. * Expose MLC erase size to find erase_shift and page_mask.
  2203. */
  2204. if (FLEXONENAND(this))
  2205. mtd->erasesize <<= 1;
  2206. this->erase_shift = ffs(mtd->erasesize) - 1;
  2207. this->page_shift = ffs(mtd->writesize) - 1;
  2208. this->ppb_shift = (this->erase_shift - this->page_shift);
  2209. this->page_mask = (mtd->erasesize / mtd->writesize) - 1;
  2210. /* Set density mask. it is used for DDP */
  2211. if (ONENAND_IS_DDP(this))
  2212. this->density_mask = this->chipsize >> (this->erase_shift + 1);
  2213. /* It's real page size */
  2214. this->writesize = mtd->writesize;
  2215. /* REVIST: Multichip handling */
  2216. if (FLEXONENAND(this))
  2217. flexonenand_get_size(mtd);
  2218. else
  2219. mtd->size = this->chipsize;
  2220. mtd->flags = MTD_CAP_NANDFLASH;
  2221. mtd->_erase = onenand_erase;
  2222. mtd->_read = onenand_read;
  2223. mtd->_write = onenand_write;
  2224. mtd->_read_oob = onenand_read_oob;
  2225. mtd->_write_oob = onenand_write_oob;
  2226. mtd->_sync = onenand_sync;
  2227. mtd->_block_isbad = onenand_block_isbad;
  2228. mtd->_block_markbad = onenand_block_markbad;
  2229. return 0;
  2230. }
  2231. /**
  2232. * onenand_scan - [OneNAND Interface] Scan for the OneNAND device
  2233. * @param mtd MTD device structure
  2234. * @param maxchips Number of chips to scan for
  2235. *
  2236. * This fills out all the not initialized function pointers
  2237. * with the defaults.
  2238. * The flash ID is read and the mtd/chip structures are
  2239. * filled with the appropriate values.
  2240. */
  2241. int onenand_scan(struct mtd_info *mtd, int maxchips)
  2242. {
  2243. int i;
  2244. struct onenand_chip *this = mtd->priv;
  2245. if (!this->read_word)
  2246. this->read_word = onenand_readw;
  2247. if (!this->write_word)
  2248. this->write_word = onenand_writew;
  2249. if (!this->command)
  2250. this->command = onenand_command;
  2251. if (!this->wait)
  2252. this->wait = onenand_wait;
  2253. if (!this->bbt_wait)
  2254. this->bbt_wait = onenand_bbt_wait;
  2255. if (!this->read_bufferram)
  2256. this->read_bufferram = onenand_read_bufferram;
  2257. if (!this->write_bufferram)
  2258. this->write_bufferram = onenand_write_bufferram;
  2259. if (!this->chip_probe)
  2260. this->chip_probe = onenand_chip_probe;
  2261. if (!this->block_markbad)
  2262. this->block_markbad = onenand_default_block_markbad;
  2263. if (!this->scan_bbt)
  2264. this->scan_bbt = onenand_default_bbt;
  2265. if (onenand_probe(mtd))
  2266. return -ENXIO;
  2267. /* Set Sync. Burst Read after probing */
  2268. if (this->mmcontrol) {
  2269. printk(KERN_INFO "OneNAND Sync. Burst Read support\n");
  2270. this->read_bufferram = onenand_sync_read_bufferram;
  2271. }
  2272. /* Allocate buffers, if necessary */
  2273. if (!this->page_buf) {
  2274. this->page_buf = kzalloc(mtd->writesize, GFP_KERNEL);
  2275. if (!this->page_buf) {
  2276. printk(KERN_ERR "onenand_scan(): Can't allocate page_buf\n");
  2277. return -ENOMEM;
  2278. }
  2279. this->options |= ONENAND_PAGEBUF_ALLOC;
  2280. }
  2281. if (!this->oob_buf) {
  2282. this->oob_buf = kzalloc(mtd->oobsize, GFP_KERNEL);
  2283. if (!this->oob_buf) {
  2284. printk(KERN_ERR "onenand_scan: Can't allocate oob_buf\n");
  2285. if (this->options & ONENAND_PAGEBUF_ALLOC) {
  2286. this->options &= ~ONENAND_PAGEBUF_ALLOC;
  2287. kfree(this->page_buf);
  2288. }
  2289. return -ENOMEM;
  2290. }
  2291. this->options |= ONENAND_OOBBUF_ALLOC;
  2292. }
  2293. this->state = FL_READY;
  2294. /*
  2295. * Allow subpage writes up to oobsize.
  2296. */
  2297. switch (mtd->oobsize) {
  2298. case 128:
  2299. this->ecclayout = &onenand_oob_128;
  2300. mtd->subpage_sft = 0;
  2301. break;
  2302. case 64:
  2303. this->ecclayout = &onenand_oob_64;
  2304. mtd->subpage_sft = 2;
  2305. break;
  2306. case 32:
  2307. this->ecclayout = &onenand_oob_32;
  2308. mtd->subpage_sft = 1;
  2309. break;
  2310. default:
  2311. printk(KERN_WARNING "No OOB scheme defined for oobsize %d\n",
  2312. mtd->oobsize);
  2313. mtd->subpage_sft = 0;
  2314. /* To prevent kernel oops */
  2315. this->ecclayout = &onenand_oob_32;
  2316. break;
  2317. }
  2318. this->subpagesize = mtd->writesize >> mtd->subpage_sft;
  2319. /*
  2320. * The number of bytes available for a client to place data into
  2321. * the out of band area
  2322. */
  2323. this->ecclayout->oobavail = 0;
  2324. for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES &&
  2325. this->ecclayout->oobfree[i].length; i++)
  2326. this->ecclayout->oobavail +=
  2327. this->ecclayout->oobfree[i].length;
  2328. mtd->oobavail = this->ecclayout->oobavail;
  2329. mtd->ecclayout = this->ecclayout;
  2330. /* Unlock whole block */
  2331. onenand_unlock_all(mtd);
  2332. return this->scan_bbt(mtd);
  2333. }
  2334. /**
  2335. * onenand_release - [OneNAND Interface] Free resources held by the OneNAND device
  2336. * @param mtd MTD device structure
  2337. */
  2338. void onenand_release(struct mtd_info *mtd)
  2339. {
  2340. }