onenand_base.c 67 KB

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