onenand_base.c 73 KB

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