onenand_base.c 73 KB

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