onenand_base.c 69 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692
  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_transfer_auto_oob - [Internal] oob auto-placement transfer
  653. * @param mtd MTD device structure
  654. * @param buf destination address
  655. * @param column oob offset to read from
  656. * @param thislen oob length to read
  657. */
  658. static int onenand_transfer_auto_oob(struct mtd_info *mtd, uint8_t *buf, int column,
  659. int thislen)
  660. {
  661. struct onenand_chip *this = mtd->priv;
  662. struct nand_oobfree *free;
  663. int readcol = column;
  664. int readend = column + thislen;
  665. int lastgap = 0;
  666. unsigned int i;
  667. uint8_t *oob_buf = this->oob_buf;
  668. free = this->ecclayout->oobfree;
  669. for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES && free->length; i++, free++) {
  670. if (readcol >= lastgap)
  671. readcol += free->offset - lastgap;
  672. if (readend >= lastgap)
  673. readend += free->offset - lastgap;
  674. lastgap = free->offset + free->length;
  675. }
  676. this->read_bufferram(mtd, ONENAND_SPARERAM, oob_buf, 0, mtd->oobsize);
  677. free = this->ecclayout->oobfree;
  678. for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES && free->length; i++, free++) {
  679. int free_end = free->offset + free->length;
  680. if (free->offset < readend && free_end > readcol) {
  681. int st = max_t(int,free->offset,readcol);
  682. int ed = min_t(int,free_end,readend);
  683. int n = ed - st;
  684. memcpy(buf, oob_buf + st, n);
  685. buf += n;
  686. } else if (column == 0)
  687. break;
  688. }
  689. return 0;
  690. }
  691. /**
  692. * onenand_read_ops - [OneNAND Interface] OneNAND read main and/or out-of-band
  693. * @param mtd MTD device structure
  694. * @param from offset to read from
  695. * @param ops: oob operation description structure
  696. *
  697. * OneNAND read main and/or out-of-band data
  698. */
  699. static int onenand_read_ops(struct mtd_info *mtd, loff_t from,
  700. struct mtd_oob_ops *ops)
  701. {
  702. struct onenand_chip *this = mtd->priv;
  703. struct mtd_ecc_stats stats;
  704. size_t len = ops->len;
  705. size_t ooblen = ops->ooblen;
  706. u_char *buf = ops->datbuf;
  707. u_char *oobbuf = ops->oobbuf;
  708. int read = 0, column, thislen;
  709. int oobread = 0, oobcolumn, thisooblen, oobsize;
  710. int ret = 0, boundary = 0;
  711. int writesize = this->writesize;
  712. DEBUG(MTD_DEBUG_LEVEL3, "onenand_read_ops: from = 0x%08x, len = %i\n", (unsigned int) from, (int) len);
  713. if (ops->mode == MTD_OOB_AUTO)
  714. oobsize = this->ecclayout->oobavail;
  715. else
  716. oobsize = mtd->oobsize;
  717. oobcolumn = from & (mtd->oobsize - 1);
  718. /* Do not allow reads past end of device */
  719. if ((from + len) > mtd->size) {
  720. printk(KERN_ERR "onenand_read_ops: Attempt read beyond end of device\n");
  721. ops->retlen = 0;
  722. ops->oobretlen = 0;
  723. return -EINVAL;
  724. }
  725. /* Grab the lock and see if the device is available */
  726. onenand_get_device(mtd, FL_READING);
  727. stats = mtd->ecc_stats;
  728. /* Read-while-load method */
  729. /* Do first load to bufferRAM */
  730. if (read < len) {
  731. if (!onenand_check_bufferram(mtd, from)) {
  732. this->command(mtd, ONENAND_CMD_READ, from, writesize);
  733. ret = this->wait(mtd, FL_READING);
  734. onenand_update_bufferram(mtd, from, !ret);
  735. }
  736. }
  737. thislen = min_t(int, writesize, len - read);
  738. column = from & (writesize - 1);
  739. if (column + thislen > writesize)
  740. thislen = writesize - column;
  741. while (!ret) {
  742. /* If there is more to load then start next load */
  743. from += thislen;
  744. if (read + thislen < len) {
  745. this->command(mtd, ONENAND_CMD_READ, from, writesize);
  746. /*
  747. * Chip boundary handling in DDP
  748. * Now we issued chip 1 read and pointed chip 1
  749. * bufferam so we have to point chip 0 bufferam.
  750. */
  751. if (ONENAND_IS_DDP(this) &&
  752. unlikely(from == (this->chipsize >> 1))) {
  753. this->write_word(ONENAND_DDP_CHIP0, this->base + ONENAND_REG_START_ADDRESS2);
  754. boundary = 1;
  755. } else
  756. boundary = 0;
  757. ONENAND_SET_PREV_BUFFERRAM(this);
  758. }
  759. /* While load is going, read from last bufferRAM */
  760. this->read_bufferram(mtd, ONENAND_DATARAM, buf, column, thislen);
  761. /* Read oob area if needed */
  762. if (oobbuf) {
  763. thisooblen = oobsize - oobcolumn;
  764. thisooblen = min_t(int, thisooblen, ooblen - oobread);
  765. if (ops->mode == MTD_OOB_AUTO)
  766. onenand_transfer_auto_oob(mtd, oobbuf, oobcolumn, thisooblen);
  767. else
  768. this->read_bufferram(mtd, ONENAND_SPARERAM, oobbuf, oobcolumn, thisooblen);
  769. oobread += thisooblen;
  770. oobbuf += thisooblen;
  771. oobcolumn = 0;
  772. }
  773. /* See if we are done */
  774. read += thislen;
  775. if (read == len)
  776. break;
  777. /* Set up for next read from bufferRAM */
  778. if (unlikely(boundary))
  779. this->write_word(ONENAND_DDP_CHIP1, this->base + ONENAND_REG_START_ADDRESS2);
  780. ONENAND_SET_NEXT_BUFFERRAM(this);
  781. buf += thislen;
  782. thislen = min_t(int, writesize, len - read);
  783. column = 0;
  784. cond_resched();
  785. /* Now wait for load */
  786. ret = this->wait(mtd, FL_READING);
  787. onenand_update_bufferram(mtd, from, !ret);
  788. }
  789. /* Deselect and wake up anyone waiting on the device */
  790. onenand_release_device(mtd);
  791. /*
  792. * Return success, if no ECC failures, else -EBADMSG
  793. * fs driver will take care of that, because
  794. * retlen == desired len and result == -EBADMSG
  795. */
  796. ops->retlen = read;
  797. ops->oobretlen = oobread;
  798. if (mtd->ecc_stats.failed - stats.failed)
  799. return -EBADMSG;
  800. if (ret)
  801. return ret;
  802. return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
  803. }
  804. /**
  805. * onenand_do_read_oob - [MTD Interface] OneNAND read out-of-band
  806. * @param mtd MTD device structure
  807. * @param from offset to read from
  808. * @param ops: oob operation description structure
  809. *
  810. * OneNAND read out-of-band data from the spare area
  811. */
  812. static int onenand_do_read_oob(struct mtd_info *mtd, loff_t from,
  813. struct mtd_oob_ops *ops)
  814. {
  815. struct onenand_chip *this = mtd->priv;
  816. int read = 0, thislen, column, oobsize;
  817. size_t len = ops->ooblen;
  818. mtd_oob_mode_t mode = ops->mode;
  819. u_char *buf = ops->oobbuf;
  820. int ret = 0;
  821. from += ops->ooboffs;
  822. DEBUG(MTD_DEBUG_LEVEL3, "onenand_read_oob: from = 0x%08x, len = %i\n", (unsigned int) from, (int) len);
  823. /* Initialize return length value */
  824. ops->oobretlen = 0;
  825. if (mode == MTD_OOB_AUTO)
  826. oobsize = this->ecclayout->oobavail;
  827. else
  828. oobsize = mtd->oobsize;
  829. column = from & (mtd->oobsize - 1);
  830. if (unlikely(column >= oobsize)) {
  831. printk(KERN_ERR "onenand_read_oob: Attempted to start read outside oob\n");
  832. return -EINVAL;
  833. }
  834. /* Do not allow reads past end of device */
  835. if (unlikely(from >= mtd->size ||
  836. column + len > ((mtd->size >> this->page_shift) -
  837. (from >> this->page_shift)) * oobsize)) {
  838. printk(KERN_ERR "onenand_read_oob: Attempted to read beyond end of device\n");
  839. return -EINVAL;
  840. }
  841. /* Grab the lock and see if the device is available */
  842. onenand_get_device(mtd, FL_READING);
  843. while (read < len) {
  844. cond_resched();
  845. thislen = oobsize - column;
  846. thislen = min_t(int, thislen, len);
  847. this->command(mtd, ONENAND_CMD_READOOB, from, mtd->oobsize);
  848. onenand_update_bufferram(mtd, from, 0);
  849. ret = this->wait(mtd, FL_READING);
  850. /* First copy data and check return value for ECC handling */
  851. if (mode == MTD_OOB_AUTO)
  852. onenand_transfer_auto_oob(mtd, buf, column, thislen);
  853. else
  854. this->read_bufferram(mtd, ONENAND_SPARERAM, buf, column, thislen);
  855. if (ret) {
  856. printk(KERN_ERR "onenand_read_oob: read failed = 0x%x\n", ret);
  857. break;
  858. }
  859. read += thislen;
  860. if (read == len)
  861. break;
  862. buf += thislen;
  863. /* Read more? */
  864. if (read < len) {
  865. /* Page size */
  866. from += mtd->writesize;
  867. column = 0;
  868. }
  869. }
  870. /* Deselect and wake up anyone waiting on the device */
  871. onenand_release_device(mtd);
  872. ops->oobretlen = read;
  873. return ret;
  874. }
  875. /**
  876. * onenand_read - [MTD Interface] Read data from flash
  877. * @param mtd MTD device structure
  878. * @param from offset to read from
  879. * @param len number of bytes to read
  880. * @param retlen pointer to variable to store the number of read bytes
  881. * @param buf the databuffer to put data
  882. *
  883. * Read with ecc
  884. */
  885. static int onenand_read(struct mtd_info *mtd, loff_t from, size_t len,
  886. size_t *retlen, u_char *buf)
  887. {
  888. struct mtd_oob_ops ops = {
  889. .len = len,
  890. .ooblen = 0,
  891. .datbuf = buf,
  892. .oobbuf = NULL,
  893. };
  894. int ret;
  895. ret = onenand_read_ops(mtd, from, &ops);
  896. *retlen = ops.retlen;
  897. return ret;
  898. }
  899. /**
  900. * onenand_read_oob - [MTD Interface] Read main and/or out-of-band
  901. * @param mtd: MTD device structure
  902. * @param from: offset to read from
  903. * @param ops: oob operation description structure
  904. * Read main and/or out-of-band
  905. */
  906. static int onenand_read_oob(struct mtd_info *mtd, loff_t from,
  907. struct mtd_oob_ops *ops)
  908. {
  909. switch (ops->mode) {
  910. case MTD_OOB_PLACE:
  911. case MTD_OOB_AUTO:
  912. break;
  913. case MTD_OOB_RAW:
  914. /* Not implemented yet */
  915. default:
  916. return -EINVAL;
  917. }
  918. if (ops->datbuf)
  919. return onenand_read_ops(mtd, from, ops);
  920. return onenand_do_read_oob(mtd, from, ops);
  921. }
  922. /**
  923. * onenand_bbt_wait - [DEFAULT] wait until the command is done
  924. * @param mtd MTD device structure
  925. * @param state state to select the max. timeout value
  926. *
  927. * Wait for command done.
  928. */
  929. static int onenand_bbt_wait(struct mtd_info *mtd, int state)
  930. {
  931. struct onenand_chip *this = mtd->priv;
  932. unsigned long timeout;
  933. unsigned int interrupt;
  934. unsigned int ctrl;
  935. /* The 20 msec is enough */
  936. timeout = jiffies + msecs_to_jiffies(20);
  937. while (time_before(jiffies, timeout)) {
  938. interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
  939. if (interrupt & ONENAND_INT_MASTER)
  940. break;
  941. }
  942. /* To get correct interrupt status in timeout case */
  943. interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
  944. ctrl = this->read_word(this->base + ONENAND_REG_CTRL_STATUS);
  945. if (ctrl & ONENAND_CTRL_ERROR) {
  946. printk(KERN_DEBUG "onenand_bbt_wait: controller error = 0x%04x\n", ctrl);
  947. /* Initial bad block case */
  948. if (ctrl & ONENAND_CTRL_LOAD)
  949. return ONENAND_BBT_READ_ERROR;
  950. return ONENAND_BBT_READ_FATAL_ERROR;
  951. }
  952. if (interrupt & ONENAND_INT_READ) {
  953. int ecc = this->read_word(this->base + ONENAND_REG_ECC_STATUS);
  954. if (ecc & ONENAND_ECC_2BIT_ALL)
  955. return ONENAND_BBT_READ_ERROR;
  956. } else {
  957. printk(KERN_ERR "onenand_bbt_wait: read timeout!"
  958. "ctrl=0x%04x intr=0x%04x\n", ctrl, interrupt);
  959. return ONENAND_BBT_READ_FATAL_ERROR;
  960. }
  961. return 0;
  962. }
  963. /**
  964. * onenand_bbt_read_oob - [MTD Interface] OneNAND read out-of-band for bbt scan
  965. * @param mtd MTD device structure
  966. * @param from offset to read from
  967. * @param ops oob operation description structure
  968. *
  969. * OneNAND read out-of-band data from the spare area for bbt scan
  970. */
  971. int onenand_bbt_read_oob(struct mtd_info *mtd, loff_t from,
  972. struct mtd_oob_ops *ops)
  973. {
  974. struct onenand_chip *this = mtd->priv;
  975. int read = 0, thislen, column;
  976. int ret = 0;
  977. size_t len = ops->ooblen;
  978. u_char *buf = ops->oobbuf;
  979. DEBUG(MTD_DEBUG_LEVEL3, "onenand_bbt_read_oob: from = 0x%08x, len = %zi\n", (unsigned int) from, len);
  980. /* Initialize return value */
  981. ops->oobretlen = 0;
  982. /* Do not allow reads past end of device */
  983. if (unlikely((from + len) > mtd->size)) {
  984. printk(KERN_ERR "onenand_bbt_read_oob: Attempt read beyond end of device\n");
  985. return ONENAND_BBT_READ_FATAL_ERROR;
  986. }
  987. /* Grab the lock and see if the device is available */
  988. onenand_get_device(mtd, FL_READING);
  989. column = from & (mtd->oobsize - 1);
  990. while (read < len) {
  991. cond_resched();
  992. thislen = mtd->oobsize - column;
  993. thislen = min_t(int, thislen, len);
  994. this->command(mtd, ONENAND_CMD_READOOB, from, mtd->oobsize);
  995. onenand_update_bufferram(mtd, from, 0);
  996. ret = onenand_bbt_wait(mtd, FL_READING);
  997. if (ret)
  998. break;
  999. this->read_bufferram(mtd, ONENAND_SPARERAM, buf, column, thislen);
  1000. read += thislen;
  1001. if (read == len)
  1002. break;
  1003. buf += thislen;
  1004. /* Read more? */
  1005. if (read < len) {
  1006. /* Update Page size */
  1007. from += this->writesize;
  1008. column = 0;
  1009. }
  1010. }
  1011. /* Deselect and wake up anyone waiting on the device */
  1012. onenand_release_device(mtd);
  1013. ops->oobretlen = read;
  1014. return ret;
  1015. }
  1016. #ifdef CONFIG_MTD_ONENAND_VERIFY_WRITE
  1017. /**
  1018. * onenand_verify_oob - [GENERIC] verify the oob contents after a write
  1019. * @param mtd MTD device structure
  1020. * @param buf the databuffer to verify
  1021. * @param to offset to read from
  1022. */
  1023. static int onenand_verify_oob(struct mtd_info *mtd, const u_char *buf, loff_t to)
  1024. {
  1025. struct onenand_chip *this = mtd->priv;
  1026. char oobbuf[64];
  1027. int status, i;
  1028. this->command(mtd, ONENAND_CMD_READOOB, to, mtd->oobsize);
  1029. onenand_update_bufferram(mtd, to, 0);
  1030. status = this->wait(mtd, FL_READING);
  1031. if (status)
  1032. return status;
  1033. this->read_bufferram(mtd, ONENAND_SPARERAM, oobbuf, 0, mtd->oobsize);
  1034. for (i = 0; i < mtd->oobsize; i++)
  1035. if (buf[i] != 0xFF && buf[i] != oobbuf[i])
  1036. return -EBADMSG;
  1037. return 0;
  1038. }
  1039. /**
  1040. * onenand_verify - [GENERIC] verify the chip contents after a write
  1041. * @param mtd MTD device structure
  1042. * @param buf the databuffer to verify
  1043. * @param addr offset to read from
  1044. * @param len number of bytes to read and compare
  1045. */
  1046. static int onenand_verify(struct mtd_info *mtd, const u_char *buf, loff_t addr, size_t len)
  1047. {
  1048. struct onenand_chip *this = mtd->priv;
  1049. void __iomem *dataram;
  1050. int ret = 0;
  1051. int thislen, column;
  1052. while (len != 0) {
  1053. thislen = min_t(int, this->writesize, len);
  1054. column = addr & (this->writesize - 1);
  1055. if (column + thislen > this->writesize)
  1056. thislen = this->writesize - column;
  1057. this->command(mtd, ONENAND_CMD_READ, addr, this->writesize);
  1058. onenand_update_bufferram(mtd, addr, 0);
  1059. ret = this->wait(mtd, FL_READING);
  1060. if (ret)
  1061. return ret;
  1062. onenand_update_bufferram(mtd, addr, 1);
  1063. dataram = this->base + ONENAND_DATARAM;
  1064. dataram += onenand_bufferram_offset(mtd, ONENAND_DATARAM);
  1065. if (memcmp(buf, dataram + column, thislen))
  1066. return -EBADMSG;
  1067. len -= thislen;
  1068. buf += thislen;
  1069. addr += thislen;
  1070. }
  1071. return 0;
  1072. }
  1073. #else
  1074. #define onenand_verify(...) (0)
  1075. #define onenand_verify_oob(...) (0)
  1076. #endif
  1077. #define NOTALIGNED(x) ((x & (this->subpagesize - 1)) != 0)
  1078. /**
  1079. * onenand_fill_auto_oob - [Internal] oob auto-placement transfer
  1080. * @param mtd MTD device structure
  1081. * @param oob_buf oob buffer
  1082. * @param buf source address
  1083. * @param column oob offset to write to
  1084. * @param thislen oob length to write
  1085. */
  1086. static int onenand_fill_auto_oob(struct mtd_info *mtd, u_char *oob_buf,
  1087. const u_char *buf, int column, int thislen)
  1088. {
  1089. struct onenand_chip *this = mtd->priv;
  1090. struct nand_oobfree *free;
  1091. int writecol = column;
  1092. int writeend = column + thislen;
  1093. int lastgap = 0;
  1094. unsigned int i;
  1095. free = this->ecclayout->oobfree;
  1096. for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES && free->length; i++, free++) {
  1097. if (writecol >= lastgap)
  1098. writecol += free->offset - lastgap;
  1099. if (writeend >= lastgap)
  1100. writeend += free->offset - lastgap;
  1101. lastgap = free->offset + free->length;
  1102. }
  1103. free = this->ecclayout->oobfree;
  1104. for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES && free->length; i++, free++) {
  1105. int free_end = free->offset + free->length;
  1106. if (free->offset < writeend && free_end > writecol) {
  1107. int st = max_t(int,free->offset,writecol);
  1108. int ed = min_t(int,free_end,writeend);
  1109. int n = ed - st;
  1110. memcpy(oob_buf + st, buf, n);
  1111. buf += n;
  1112. } else if (column == 0)
  1113. break;
  1114. }
  1115. return 0;
  1116. }
  1117. /**
  1118. * onenand_write_ops - [OneNAND Interface] write main and/or out-of-band
  1119. * @param mtd MTD device structure
  1120. * @param to offset to write to
  1121. * @param ops oob operation description structure
  1122. *
  1123. * Write main and/or oob with ECC
  1124. */
  1125. static int onenand_write_ops(struct mtd_info *mtd, loff_t to,
  1126. struct mtd_oob_ops *ops)
  1127. {
  1128. struct onenand_chip *this = mtd->priv;
  1129. int written = 0, column, thislen, subpage;
  1130. int oobwritten = 0, oobcolumn, thisooblen, oobsize;
  1131. size_t len = ops->len;
  1132. size_t ooblen = ops->ooblen;
  1133. const u_char *buf = ops->datbuf;
  1134. const u_char *oob = ops->oobbuf;
  1135. u_char *oobbuf;
  1136. int ret = 0;
  1137. DEBUG(MTD_DEBUG_LEVEL3, "onenand_write: to = 0x%08x, len = %i\n", (unsigned int) to, (int) len);
  1138. /* Initialize retlen, in case of early exit */
  1139. ops->retlen = 0;
  1140. ops->oobretlen = 0;
  1141. /* Do not allow writes past end of device */
  1142. if (unlikely((to + len) > mtd->size)) {
  1143. printk(KERN_ERR "onenand_write: Attempt write to past end of device\n");
  1144. return -EINVAL;
  1145. }
  1146. /* Reject writes, which are not page aligned */
  1147. if (unlikely(NOTALIGNED(to)) || unlikely(NOTALIGNED(len))) {
  1148. printk(KERN_ERR "onenand_write: Attempt to write not page aligned data\n");
  1149. return -EINVAL;
  1150. }
  1151. if (ops->mode == MTD_OOB_AUTO)
  1152. oobsize = this->ecclayout->oobavail;
  1153. else
  1154. oobsize = mtd->oobsize;
  1155. oobcolumn = to & (mtd->oobsize - 1);
  1156. column = to & (mtd->writesize - 1);
  1157. /* Grab the lock and see if the device is available */
  1158. onenand_get_device(mtd, FL_WRITING);
  1159. /* Loop until all data write */
  1160. while (written < len) {
  1161. u_char *wbuf = (u_char *) buf;
  1162. thislen = min_t(int, mtd->writesize - column, len - written);
  1163. thisooblen = min_t(int, oobsize - oobcolumn, ooblen - oobwritten);
  1164. cond_resched();
  1165. this->command(mtd, ONENAND_CMD_BUFFERRAM, to, thislen);
  1166. /* Partial page write */
  1167. subpage = thislen < mtd->writesize;
  1168. if (subpage) {
  1169. memset(this->page_buf, 0xff, mtd->writesize);
  1170. memcpy(this->page_buf + column, buf, thislen);
  1171. wbuf = this->page_buf;
  1172. }
  1173. this->write_bufferram(mtd, ONENAND_DATARAM, wbuf, 0, mtd->writesize);
  1174. if (oob) {
  1175. oobbuf = this->oob_buf;
  1176. /* We send data to spare ram with oobsize
  1177. * to prevent byte access */
  1178. memset(oobbuf, 0xff, mtd->oobsize);
  1179. if (ops->mode == MTD_OOB_AUTO)
  1180. onenand_fill_auto_oob(mtd, oobbuf, oob, oobcolumn, thisooblen);
  1181. else
  1182. memcpy(oobbuf + oobcolumn, oob, thisooblen);
  1183. oobwritten += thisooblen;
  1184. oob += thisooblen;
  1185. oobcolumn = 0;
  1186. } else
  1187. oobbuf = (u_char *) ffchars;
  1188. this->write_bufferram(mtd, ONENAND_SPARERAM, oobbuf, 0, mtd->oobsize);
  1189. this->command(mtd, ONENAND_CMD_PROG, to, mtd->writesize);
  1190. ret = this->wait(mtd, FL_WRITING);
  1191. /* In partial page write we don't update bufferram */
  1192. onenand_update_bufferram(mtd, to, !ret && !subpage);
  1193. if (ONENAND_IS_2PLANE(this)) {
  1194. ONENAND_SET_BUFFERRAM1(this);
  1195. onenand_update_bufferram(mtd, to + this->writesize, !ret && !subpage);
  1196. }
  1197. if (ret) {
  1198. printk(KERN_ERR "onenand_write: write filaed %d\n", ret);
  1199. break;
  1200. }
  1201. /* Only check verify write turn on */
  1202. ret = onenand_verify(mtd, (u_char *) wbuf, to, thislen);
  1203. if (ret) {
  1204. printk(KERN_ERR "onenand_write: verify failed %d\n", ret);
  1205. break;
  1206. }
  1207. written += thislen;
  1208. if (written == len)
  1209. break;
  1210. column = 0;
  1211. to += thislen;
  1212. buf += thislen;
  1213. }
  1214. /* Deselect and wake up anyone waiting on the device */
  1215. onenand_release_device(mtd);
  1216. ops->retlen = written;
  1217. return ret;
  1218. }
  1219. /**
  1220. * onenand_do_write_oob - [Internal] OneNAND write out-of-band
  1221. * @param mtd MTD device structure
  1222. * @param to offset to write to
  1223. * @param len number of bytes to write
  1224. * @param retlen pointer to variable to store the number of written bytes
  1225. * @param buf the data to write
  1226. * @param mode operation mode
  1227. *
  1228. * OneNAND write out-of-band
  1229. */
  1230. static int onenand_do_write_oob(struct mtd_info *mtd, loff_t to,
  1231. struct mtd_oob_ops *ops)
  1232. {
  1233. struct onenand_chip *this = mtd->priv;
  1234. int column, ret = 0, oobsize;
  1235. int written = 0;
  1236. u_char *oobbuf;
  1237. size_t len = ops->ooblen;
  1238. const u_char *buf = ops->oobbuf;
  1239. mtd_oob_mode_t mode = ops->mode;
  1240. to += ops->ooboffs;
  1241. DEBUG(MTD_DEBUG_LEVEL3, "onenand_write_oob: to = 0x%08x, len = %i\n", (unsigned int) to, (int) len);
  1242. /* Initialize retlen, in case of early exit */
  1243. ops->oobretlen = 0;
  1244. if (mode == MTD_OOB_AUTO)
  1245. oobsize = this->ecclayout->oobavail;
  1246. else
  1247. oobsize = mtd->oobsize;
  1248. column = to & (mtd->oobsize - 1);
  1249. if (unlikely(column >= oobsize)) {
  1250. printk(KERN_ERR "onenand_write_oob: Attempted to start write outside oob\n");
  1251. return -EINVAL;
  1252. }
  1253. /* For compatibility with NAND: Do not allow write past end of page */
  1254. if (unlikely(column + len > oobsize)) {
  1255. printk(KERN_ERR "onenand_write_oob: "
  1256. "Attempt to write past end of page\n");
  1257. return -EINVAL;
  1258. }
  1259. /* Do not allow reads past end of device */
  1260. if (unlikely(to >= mtd->size ||
  1261. column + len > ((mtd->size >> this->page_shift) -
  1262. (to >> this->page_shift)) * oobsize)) {
  1263. printk(KERN_ERR "onenand_write_oob: Attempted to write past end of device\n");
  1264. return -EINVAL;
  1265. }
  1266. /* Grab the lock and see if the device is available */
  1267. onenand_get_device(mtd, FL_WRITING);
  1268. oobbuf = this->oob_buf;
  1269. /* Loop until all data write */
  1270. while (written < len) {
  1271. int thislen = min_t(int, oobsize, len - written);
  1272. cond_resched();
  1273. this->command(mtd, ONENAND_CMD_BUFFERRAM, to, mtd->oobsize);
  1274. /* We send data to spare ram with oobsize
  1275. * to prevent byte access */
  1276. memset(oobbuf, 0xff, mtd->oobsize);
  1277. if (mode == MTD_OOB_AUTO)
  1278. onenand_fill_auto_oob(mtd, oobbuf, buf, column, thislen);
  1279. else
  1280. memcpy(oobbuf + column, buf, thislen);
  1281. this->write_bufferram(mtd, ONENAND_SPARERAM, oobbuf, 0, mtd->oobsize);
  1282. this->command(mtd, ONENAND_CMD_PROGOOB, to, mtd->oobsize);
  1283. onenand_update_bufferram(mtd, to, 0);
  1284. if (ONENAND_IS_2PLANE(this)) {
  1285. ONENAND_SET_BUFFERRAM1(this);
  1286. onenand_update_bufferram(mtd, to + this->writesize, 0);
  1287. }
  1288. ret = this->wait(mtd, FL_WRITING);
  1289. if (ret) {
  1290. printk(KERN_ERR "onenand_write_oob: write failed %d\n", ret);
  1291. break;
  1292. }
  1293. ret = onenand_verify_oob(mtd, oobbuf, to);
  1294. if (ret) {
  1295. printk(KERN_ERR "onenand_write_oob: verify failed %d\n", ret);
  1296. break;
  1297. }
  1298. written += thislen;
  1299. if (written == len)
  1300. break;
  1301. to += mtd->writesize;
  1302. buf += thislen;
  1303. column = 0;
  1304. }
  1305. /* Deselect and wake up anyone waiting on the device */
  1306. onenand_release_device(mtd);
  1307. ops->oobretlen = written;
  1308. return ret;
  1309. }
  1310. /**
  1311. * onenand_write - [MTD Interface] write buffer to FLASH
  1312. * @param mtd MTD device structure
  1313. * @param to offset to write to
  1314. * @param len number of bytes to write
  1315. * @param retlen pointer to variable to store the number of written bytes
  1316. * @param buf the data to write
  1317. *
  1318. * Write with ECC
  1319. */
  1320. static int onenand_write(struct mtd_info *mtd, loff_t to, size_t len,
  1321. size_t *retlen, const u_char *buf)
  1322. {
  1323. struct mtd_oob_ops ops = {
  1324. .len = len,
  1325. .ooblen = 0,
  1326. .datbuf = (u_char *) buf,
  1327. .oobbuf = NULL,
  1328. };
  1329. int ret;
  1330. ret = onenand_write_ops(mtd, to, &ops);
  1331. *retlen = ops.retlen;
  1332. return ret;
  1333. }
  1334. /**
  1335. * onenand_write_oob - [MTD Interface] NAND write data and/or out-of-band
  1336. * @param mtd: MTD device structure
  1337. * @param to: offset to write
  1338. * @param ops: oob operation description structure
  1339. */
  1340. static int onenand_write_oob(struct mtd_info *mtd, loff_t to,
  1341. struct mtd_oob_ops *ops)
  1342. {
  1343. switch (ops->mode) {
  1344. case MTD_OOB_PLACE:
  1345. case MTD_OOB_AUTO:
  1346. break;
  1347. case MTD_OOB_RAW:
  1348. /* Not implemented yet */
  1349. default:
  1350. return -EINVAL;
  1351. }
  1352. if (ops->datbuf)
  1353. return onenand_write_ops(mtd, to, ops);
  1354. return onenand_do_write_oob(mtd, to, ops);
  1355. }
  1356. /**
  1357. * onenand_block_checkbad - [GENERIC] Check if a block is marked bad
  1358. * @param mtd MTD device structure
  1359. * @param ofs offset from device start
  1360. * @param getchip 0, if the chip is already selected
  1361. * @param allowbbt 1, if its allowed to access the bbt area
  1362. *
  1363. * Check, if the block is bad. Either by reading the bad block table or
  1364. * calling of the scan function.
  1365. */
  1366. static int onenand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int getchip, int allowbbt)
  1367. {
  1368. struct onenand_chip *this = mtd->priv;
  1369. struct bbm_info *bbm = this->bbm;
  1370. /* Return info from the table */
  1371. return bbm->isbad_bbt(mtd, ofs, allowbbt);
  1372. }
  1373. /**
  1374. * onenand_erase - [MTD Interface] erase block(s)
  1375. * @param mtd MTD device structure
  1376. * @param instr erase instruction
  1377. *
  1378. * Erase one ore more blocks
  1379. */
  1380. static int onenand_erase(struct mtd_info *mtd, struct erase_info *instr)
  1381. {
  1382. struct onenand_chip *this = mtd->priv;
  1383. unsigned int block_size;
  1384. loff_t addr;
  1385. int len;
  1386. int ret = 0;
  1387. DEBUG(MTD_DEBUG_LEVEL3, "onenand_erase: start = 0x%08x, len = %i\n", (unsigned int) instr->addr, (unsigned int) instr->len);
  1388. block_size = (1 << this->erase_shift);
  1389. /* Start address must align on block boundary */
  1390. if (unlikely(instr->addr & (block_size - 1))) {
  1391. printk(KERN_ERR "onenand_erase: Unaligned address\n");
  1392. return -EINVAL;
  1393. }
  1394. /* Length must align on block boundary */
  1395. if (unlikely(instr->len & (block_size - 1))) {
  1396. printk(KERN_ERR "onenand_erase: Length not block aligned\n");
  1397. return -EINVAL;
  1398. }
  1399. /* Do not allow erase past end of device */
  1400. if (unlikely((instr->len + instr->addr) > mtd->size)) {
  1401. printk(KERN_ERR "onenand_erase: Erase past end of device\n");
  1402. return -EINVAL;
  1403. }
  1404. instr->fail_addr = 0xffffffff;
  1405. /* Grab the lock and see if the device is available */
  1406. onenand_get_device(mtd, FL_ERASING);
  1407. /* Loop throught the pages */
  1408. len = instr->len;
  1409. addr = instr->addr;
  1410. instr->state = MTD_ERASING;
  1411. while (len) {
  1412. cond_resched();
  1413. /* Check if we have a bad block, we do not erase bad blocks */
  1414. if (onenand_block_checkbad(mtd, addr, 0, 0)) {
  1415. printk (KERN_WARNING "onenand_erase: attempt to erase a bad block at addr 0x%08x\n", (unsigned int) addr);
  1416. instr->state = MTD_ERASE_FAILED;
  1417. goto erase_exit;
  1418. }
  1419. this->command(mtd, ONENAND_CMD_ERASE, addr, block_size);
  1420. onenand_invalidate_bufferram(mtd, addr, block_size);
  1421. ret = this->wait(mtd, FL_ERASING);
  1422. /* Check, if it is write protected */
  1423. if (ret) {
  1424. printk(KERN_ERR "onenand_erase: Failed erase, block %d\n", (unsigned) (addr >> this->erase_shift));
  1425. instr->state = MTD_ERASE_FAILED;
  1426. instr->fail_addr = addr;
  1427. goto erase_exit;
  1428. }
  1429. len -= block_size;
  1430. addr += block_size;
  1431. }
  1432. instr->state = MTD_ERASE_DONE;
  1433. erase_exit:
  1434. ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
  1435. /* Do call back function */
  1436. if (!ret)
  1437. mtd_erase_callback(instr);
  1438. /* Deselect and wake up anyone waiting on the device */
  1439. onenand_release_device(mtd);
  1440. return ret;
  1441. }
  1442. /**
  1443. * onenand_sync - [MTD Interface] sync
  1444. * @param mtd MTD device structure
  1445. *
  1446. * Sync is actually a wait for chip ready function
  1447. */
  1448. static void onenand_sync(struct mtd_info *mtd)
  1449. {
  1450. DEBUG(MTD_DEBUG_LEVEL3, "onenand_sync: called\n");
  1451. /* Grab the lock and see if the device is available */
  1452. onenand_get_device(mtd, FL_SYNCING);
  1453. /* Release it and go back */
  1454. onenand_release_device(mtd);
  1455. }
  1456. /**
  1457. * onenand_block_isbad - [MTD Interface] Check whether the block at the given offset is bad
  1458. * @param mtd MTD device structure
  1459. * @param ofs offset relative to mtd start
  1460. *
  1461. * Check whether the block is bad
  1462. */
  1463. static int onenand_block_isbad(struct mtd_info *mtd, loff_t ofs)
  1464. {
  1465. /* Check for invalid offset */
  1466. if (ofs > mtd->size)
  1467. return -EINVAL;
  1468. return onenand_block_checkbad(mtd, ofs, 1, 0);
  1469. }
  1470. /**
  1471. * onenand_default_block_markbad - [DEFAULT] mark a block bad
  1472. * @param mtd MTD device structure
  1473. * @param ofs offset from device start
  1474. *
  1475. * This is the default implementation, which can be overridden by
  1476. * a hardware specific driver.
  1477. */
  1478. static int onenand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
  1479. {
  1480. struct onenand_chip *this = mtd->priv;
  1481. struct bbm_info *bbm = this->bbm;
  1482. u_char buf[2] = {0, 0};
  1483. struct mtd_oob_ops ops = {
  1484. .mode = MTD_OOB_PLACE,
  1485. .ooblen = 2,
  1486. .oobbuf = buf,
  1487. .ooboffs = 0,
  1488. };
  1489. int block;
  1490. /* Get block number */
  1491. block = ((int) ofs) >> bbm->bbt_erase_shift;
  1492. if (bbm->bbt)
  1493. bbm->bbt[block >> 2] |= 0x01 << ((block & 0x03) << 1);
  1494. /* We write two bytes, so we dont have to mess with 16 bit access */
  1495. ofs += mtd->oobsize + (bbm->badblockpos & ~0x01);
  1496. return onenand_do_write_oob(mtd, ofs, &ops);
  1497. }
  1498. /**
  1499. * onenand_block_markbad - [MTD Interface] Mark the block at the given offset as bad
  1500. * @param mtd MTD device structure
  1501. * @param ofs offset relative to mtd start
  1502. *
  1503. * Mark the block as bad
  1504. */
  1505. static int onenand_block_markbad(struct mtd_info *mtd, loff_t ofs)
  1506. {
  1507. struct onenand_chip *this = mtd->priv;
  1508. int ret;
  1509. ret = onenand_block_isbad(mtd, ofs);
  1510. if (ret) {
  1511. /* If it was bad already, return success and do nothing */
  1512. if (ret > 0)
  1513. return 0;
  1514. return ret;
  1515. }
  1516. return this->block_markbad(mtd, ofs);
  1517. }
  1518. /**
  1519. * onenand_do_lock_cmd - [OneNAND Interface] Lock or unlock block(s)
  1520. * @param mtd MTD device structure
  1521. * @param ofs offset relative to mtd start
  1522. * @param len number of bytes to lock or unlock
  1523. * @param cmd lock or unlock command
  1524. *
  1525. * Lock or unlock one or more blocks
  1526. */
  1527. static int onenand_do_lock_cmd(struct mtd_info *mtd, loff_t ofs, size_t len, int cmd)
  1528. {
  1529. struct onenand_chip *this = mtd->priv;
  1530. int start, end, block, value, status;
  1531. int wp_status_mask;
  1532. start = ofs >> this->erase_shift;
  1533. end = len >> this->erase_shift;
  1534. if (cmd == ONENAND_CMD_LOCK)
  1535. wp_status_mask = ONENAND_WP_LS;
  1536. else
  1537. wp_status_mask = ONENAND_WP_US;
  1538. /* Continuous lock scheme */
  1539. if (this->options & ONENAND_HAS_CONT_LOCK) {
  1540. /* Set start block address */
  1541. this->write_word(start, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
  1542. /* Set end block address */
  1543. this->write_word(start + end - 1, this->base + ONENAND_REG_END_BLOCK_ADDRESS);
  1544. /* Write lock command */
  1545. this->command(mtd, cmd, 0, 0);
  1546. /* There's no return value */
  1547. this->wait(mtd, FL_LOCKING);
  1548. /* Sanity check */
  1549. while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
  1550. & ONENAND_CTRL_ONGO)
  1551. continue;
  1552. /* Check lock status */
  1553. status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
  1554. if (!(status & wp_status_mask))
  1555. printk(KERN_ERR "wp status = 0x%x\n", status);
  1556. return 0;
  1557. }
  1558. /* Block lock scheme */
  1559. for (block = start; block < start + end; block++) {
  1560. /* Set block address */
  1561. value = onenand_block_address(this, block);
  1562. this->write_word(value, this->base + ONENAND_REG_START_ADDRESS1);
  1563. /* Select DataRAM for DDP */
  1564. value = onenand_bufferram_address(this, block);
  1565. this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
  1566. /* Set start block address */
  1567. this->write_word(block, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
  1568. /* Write lock command */
  1569. this->command(mtd, cmd, 0, 0);
  1570. /* There's no return value */
  1571. this->wait(mtd, FL_LOCKING);
  1572. /* Sanity check */
  1573. while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
  1574. & ONENAND_CTRL_ONGO)
  1575. continue;
  1576. /* Check lock status */
  1577. status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
  1578. if (!(status & wp_status_mask))
  1579. printk(KERN_ERR "block = %d, wp status = 0x%x\n", block, status);
  1580. }
  1581. return 0;
  1582. }
  1583. /**
  1584. * onenand_lock - [MTD Interface] Lock block(s)
  1585. * @param mtd MTD device structure
  1586. * @param ofs offset relative to mtd start
  1587. * @param len number of bytes to unlock
  1588. *
  1589. * Lock one or more blocks
  1590. */
  1591. static int onenand_lock(struct mtd_info *mtd, loff_t ofs, size_t len)
  1592. {
  1593. return onenand_do_lock_cmd(mtd, ofs, len, ONENAND_CMD_LOCK);
  1594. }
  1595. /**
  1596. * onenand_unlock - [MTD Interface] Unlock block(s)
  1597. * @param mtd MTD device structure
  1598. * @param ofs offset relative to mtd start
  1599. * @param len number of bytes to unlock
  1600. *
  1601. * Unlock one or more blocks
  1602. */
  1603. static int onenand_unlock(struct mtd_info *mtd, loff_t ofs, size_t len)
  1604. {
  1605. return onenand_do_lock_cmd(mtd, ofs, len, ONENAND_CMD_UNLOCK);
  1606. }
  1607. /**
  1608. * onenand_check_lock_status - [OneNAND Interface] Check lock status
  1609. * @param this onenand chip data structure
  1610. *
  1611. * Check lock status
  1612. */
  1613. static void onenand_check_lock_status(struct onenand_chip *this)
  1614. {
  1615. unsigned int value, block, status;
  1616. unsigned int end;
  1617. end = this->chipsize >> this->erase_shift;
  1618. for (block = 0; block < end; block++) {
  1619. /* Set block address */
  1620. value = onenand_block_address(this, block);
  1621. this->write_word(value, this->base + ONENAND_REG_START_ADDRESS1);
  1622. /* Select DataRAM for DDP */
  1623. value = onenand_bufferram_address(this, block);
  1624. this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
  1625. /* Set start block address */
  1626. this->write_word(block, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
  1627. /* Check lock status */
  1628. status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
  1629. if (!(status & ONENAND_WP_US))
  1630. printk(KERN_ERR "block = %d, wp status = 0x%x\n", block, status);
  1631. }
  1632. }
  1633. /**
  1634. * onenand_unlock_all - [OneNAND Interface] unlock all blocks
  1635. * @param mtd MTD device structure
  1636. *
  1637. * Unlock all blocks
  1638. */
  1639. static int onenand_unlock_all(struct mtd_info *mtd)
  1640. {
  1641. struct onenand_chip *this = mtd->priv;
  1642. if (this->options & ONENAND_HAS_UNLOCK_ALL) {
  1643. /* Set start block address */
  1644. this->write_word(0, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
  1645. /* Write unlock command */
  1646. this->command(mtd, ONENAND_CMD_UNLOCK_ALL, 0, 0);
  1647. /* There's no return value */
  1648. this->wait(mtd, FL_LOCKING);
  1649. /* Sanity check */
  1650. while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
  1651. & ONENAND_CTRL_ONGO)
  1652. continue;
  1653. /* Workaround for all block unlock in DDP */
  1654. if (ONENAND_IS_DDP(this)) {
  1655. /* 1st block on another chip */
  1656. loff_t ofs = this->chipsize >> 1;
  1657. size_t len = mtd->erasesize;
  1658. onenand_unlock(mtd, ofs, len);
  1659. }
  1660. onenand_check_lock_status(this);
  1661. return 0;
  1662. }
  1663. onenand_unlock(mtd, 0x0, this->chipsize);
  1664. return 0;
  1665. }
  1666. #ifdef CONFIG_MTD_ONENAND_OTP
  1667. /* Interal OTP operation */
  1668. typedef int (*otp_op_t)(struct mtd_info *mtd, loff_t form, size_t len,
  1669. size_t *retlen, u_char *buf);
  1670. /**
  1671. * do_otp_read - [DEFAULT] Read OTP block area
  1672. * @param mtd MTD device structure
  1673. * @param from The offset to read
  1674. * @param len number of bytes to read
  1675. * @param retlen pointer to variable to store the number of readbytes
  1676. * @param buf the databuffer to put/get data
  1677. *
  1678. * Read OTP block area.
  1679. */
  1680. static int do_otp_read(struct mtd_info *mtd, loff_t from, size_t len,
  1681. size_t *retlen, u_char *buf)
  1682. {
  1683. struct onenand_chip *this = mtd->priv;
  1684. int ret;
  1685. /* Enter OTP access mode */
  1686. this->command(mtd, ONENAND_CMD_OTP_ACCESS, 0, 0);
  1687. this->wait(mtd, FL_OTPING);
  1688. ret = mtd->read(mtd, from, len, retlen, buf);
  1689. /* Exit OTP access mode */
  1690. this->command(mtd, ONENAND_CMD_RESET, 0, 0);
  1691. this->wait(mtd, FL_RESETING);
  1692. return ret;
  1693. }
  1694. /**
  1695. * do_otp_write - [DEFAULT] Write OTP block area
  1696. * @param mtd MTD device structure
  1697. * @param from The offset to write
  1698. * @param len number of bytes to write
  1699. * @param retlen pointer to variable to store the number of write bytes
  1700. * @param buf the databuffer to put/get data
  1701. *
  1702. * Write OTP block area.
  1703. */
  1704. static int do_otp_write(struct mtd_info *mtd, loff_t from, size_t len,
  1705. size_t *retlen, u_char *buf)
  1706. {
  1707. struct onenand_chip *this = mtd->priv;
  1708. unsigned char *pbuf = buf;
  1709. int ret;
  1710. /* Force buffer page aligned */
  1711. if (len < mtd->writesize) {
  1712. memcpy(this->page_buf, buf, len);
  1713. memset(this->page_buf + len, 0xff, mtd->writesize - len);
  1714. pbuf = this->page_buf;
  1715. len = mtd->writesize;
  1716. }
  1717. /* Enter OTP access mode */
  1718. this->command(mtd, ONENAND_CMD_OTP_ACCESS, 0, 0);
  1719. this->wait(mtd, FL_OTPING);
  1720. ret = mtd->write(mtd, from, len, retlen, pbuf);
  1721. /* Exit OTP access mode */
  1722. this->command(mtd, ONENAND_CMD_RESET, 0, 0);
  1723. this->wait(mtd, FL_RESETING);
  1724. return ret;
  1725. }
  1726. /**
  1727. * do_otp_lock - [DEFAULT] Lock OTP block area
  1728. * @param mtd MTD device structure
  1729. * @param from The offset to lock
  1730. * @param len number of bytes to lock
  1731. * @param retlen pointer to variable to store the number of lock bytes
  1732. * @param buf the databuffer to put/get data
  1733. *
  1734. * Lock OTP block area.
  1735. */
  1736. static int do_otp_lock(struct mtd_info *mtd, loff_t from, size_t len,
  1737. size_t *retlen, u_char *buf)
  1738. {
  1739. struct onenand_chip *this = mtd->priv;
  1740. struct mtd_oob_ops ops = {
  1741. .mode = MTD_OOB_PLACE,
  1742. .ooblen = len,
  1743. .oobbuf = buf,
  1744. .ooboffs = 0,
  1745. };
  1746. int ret;
  1747. /* Enter OTP access mode */
  1748. this->command(mtd, ONENAND_CMD_OTP_ACCESS, 0, 0);
  1749. this->wait(mtd, FL_OTPING);
  1750. ret = onenand_do_write_oob(mtd, from, &ops);
  1751. *retlen = ops.oobretlen;
  1752. /* Exit OTP access mode */
  1753. this->command(mtd, ONENAND_CMD_RESET, 0, 0);
  1754. this->wait(mtd, FL_RESETING);
  1755. return ret;
  1756. }
  1757. /**
  1758. * onenand_otp_walk - [DEFAULT] Handle OTP operation
  1759. * @param mtd MTD device structure
  1760. * @param from The offset to read/write
  1761. * @param len number of bytes to read/write
  1762. * @param retlen pointer to variable to store the number of read bytes
  1763. * @param buf the databuffer to put/get data
  1764. * @param action do given action
  1765. * @param mode specify user and factory
  1766. *
  1767. * Handle OTP operation.
  1768. */
  1769. static int onenand_otp_walk(struct mtd_info *mtd, loff_t from, size_t len,
  1770. size_t *retlen, u_char *buf,
  1771. otp_op_t action, int mode)
  1772. {
  1773. struct onenand_chip *this = mtd->priv;
  1774. int otp_pages;
  1775. int density;
  1776. int ret = 0;
  1777. *retlen = 0;
  1778. density = this->device_id >> ONENAND_DEVICE_DENSITY_SHIFT;
  1779. if (density < ONENAND_DEVICE_DENSITY_512Mb)
  1780. otp_pages = 20;
  1781. else
  1782. otp_pages = 10;
  1783. if (mode == MTD_OTP_FACTORY) {
  1784. from += mtd->writesize * otp_pages;
  1785. otp_pages = 64 - otp_pages;
  1786. }
  1787. /* Check User/Factory boundary */
  1788. if (((mtd->writesize * otp_pages) - (from + len)) < 0)
  1789. return 0;
  1790. while (len > 0 && otp_pages > 0) {
  1791. if (!action) { /* OTP Info functions */
  1792. struct otp_info *otpinfo;
  1793. len -= sizeof(struct otp_info);
  1794. if (len <= 0)
  1795. return -ENOSPC;
  1796. otpinfo = (struct otp_info *) buf;
  1797. otpinfo->start = from;
  1798. otpinfo->length = mtd->writesize;
  1799. otpinfo->locked = 0;
  1800. from += mtd->writesize;
  1801. buf += sizeof(struct otp_info);
  1802. *retlen += sizeof(struct otp_info);
  1803. } else {
  1804. size_t tmp_retlen;
  1805. int size = len;
  1806. ret = action(mtd, from, len, &tmp_retlen, buf);
  1807. buf += size;
  1808. len -= size;
  1809. *retlen += size;
  1810. if (ret < 0)
  1811. return ret;
  1812. }
  1813. otp_pages--;
  1814. }
  1815. return 0;
  1816. }
  1817. /**
  1818. * onenand_get_fact_prot_info - [MTD Interface] Read factory OTP info
  1819. * @param mtd MTD device structure
  1820. * @param buf the databuffer to put/get data
  1821. * @param len number of bytes to read
  1822. *
  1823. * Read factory OTP info.
  1824. */
  1825. static int onenand_get_fact_prot_info(struct mtd_info *mtd,
  1826. struct otp_info *buf, size_t len)
  1827. {
  1828. size_t retlen;
  1829. int ret;
  1830. ret = onenand_otp_walk(mtd, 0, len, &retlen, (u_char *) buf, NULL, MTD_OTP_FACTORY);
  1831. return ret ? : retlen;
  1832. }
  1833. /**
  1834. * onenand_read_fact_prot_reg - [MTD Interface] Read factory OTP area
  1835. * @param mtd MTD device structure
  1836. * @param from The offset to read
  1837. * @param len number of bytes to read
  1838. * @param retlen pointer to variable to store the number of read bytes
  1839. * @param buf the databuffer to put/get data
  1840. *
  1841. * Read factory OTP area.
  1842. */
  1843. static int onenand_read_fact_prot_reg(struct mtd_info *mtd, loff_t from,
  1844. size_t len, size_t *retlen, u_char *buf)
  1845. {
  1846. return onenand_otp_walk(mtd, from, len, retlen, buf, do_otp_read, MTD_OTP_FACTORY);
  1847. }
  1848. /**
  1849. * onenand_get_user_prot_info - [MTD Interface] Read user OTP info
  1850. * @param mtd MTD device structure
  1851. * @param buf the databuffer to put/get data
  1852. * @param len number of bytes to read
  1853. *
  1854. * Read user OTP info.
  1855. */
  1856. static int onenand_get_user_prot_info(struct mtd_info *mtd,
  1857. struct otp_info *buf, size_t len)
  1858. {
  1859. size_t retlen;
  1860. int ret;
  1861. ret = onenand_otp_walk(mtd, 0, len, &retlen, (u_char *) buf, NULL, MTD_OTP_USER);
  1862. return ret ? : retlen;
  1863. }
  1864. /**
  1865. * onenand_read_user_prot_reg - [MTD Interface] Read user OTP area
  1866. * @param mtd MTD device structure
  1867. * @param from The offset to read
  1868. * @param len number of bytes to read
  1869. * @param retlen pointer to variable to store the number of read bytes
  1870. * @param buf the databuffer to put/get data
  1871. *
  1872. * Read user OTP area.
  1873. */
  1874. static int onenand_read_user_prot_reg(struct mtd_info *mtd, loff_t from,
  1875. size_t len, size_t *retlen, u_char *buf)
  1876. {
  1877. return onenand_otp_walk(mtd, from, len, retlen, buf, do_otp_read, MTD_OTP_USER);
  1878. }
  1879. /**
  1880. * onenand_write_user_prot_reg - [MTD Interface] Write user OTP area
  1881. * @param mtd MTD device structure
  1882. * @param from The offset to write
  1883. * @param len number of bytes to write
  1884. * @param retlen pointer to variable to store the number of write bytes
  1885. * @param buf the databuffer to put/get data
  1886. *
  1887. * Write user OTP area.
  1888. */
  1889. static int onenand_write_user_prot_reg(struct mtd_info *mtd, loff_t from,
  1890. size_t len, size_t *retlen, u_char *buf)
  1891. {
  1892. return onenand_otp_walk(mtd, from, len, retlen, buf, do_otp_write, MTD_OTP_USER);
  1893. }
  1894. /**
  1895. * onenand_lock_user_prot_reg - [MTD Interface] Lock user OTP area
  1896. * @param mtd MTD device structure
  1897. * @param from The offset to lock
  1898. * @param len number of bytes to unlock
  1899. *
  1900. * Write lock mark on spare area in page 0 in OTP block
  1901. */
  1902. static int onenand_lock_user_prot_reg(struct mtd_info *mtd, loff_t from,
  1903. size_t len)
  1904. {
  1905. unsigned char oob_buf[64];
  1906. size_t retlen;
  1907. int ret;
  1908. memset(oob_buf, 0xff, mtd->oobsize);
  1909. /*
  1910. * Note: OTP lock operation
  1911. * OTP block : 0xXXFC
  1912. * 1st block : 0xXXF3 (If chip support)
  1913. * Both : 0xXXF0 (If chip support)
  1914. */
  1915. oob_buf[ONENAND_OTP_LOCK_OFFSET] = 0xFC;
  1916. /*
  1917. * Write lock mark to 8th word of sector0 of page0 of the spare0.
  1918. * We write 16 bytes spare area instead of 2 bytes.
  1919. */
  1920. from = 0;
  1921. len = 16;
  1922. ret = onenand_otp_walk(mtd, from, len, &retlen, oob_buf, do_otp_lock, MTD_OTP_USER);
  1923. return ret ? : retlen;
  1924. }
  1925. #endif /* CONFIG_MTD_ONENAND_OTP */
  1926. /**
  1927. * onenand_check_features - Check and set OneNAND features
  1928. * @param mtd MTD data structure
  1929. *
  1930. * Check and set OneNAND features
  1931. * - lock scheme
  1932. * - two plane
  1933. */
  1934. static void onenand_check_features(struct mtd_info *mtd)
  1935. {
  1936. struct onenand_chip *this = mtd->priv;
  1937. unsigned int density, process;
  1938. /* Lock scheme depends on density and process */
  1939. density = this->device_id >> ONENAND_DEVICE_DENSITY_SHIFT;
  1940. process = this->version_id >> ONENAND_VERSION_PROCESS_SHIFT;
  1941. /* Lock scheme */
  1942. switch (density) {
  1943. case ONENAND_DEVICE_DENSITY_4Gb:
  1944. this->options |= ONENAND_HAS_2PLANE;
  1945. case ONENAND_DEVICE_DENSITY_2Gb:
  1946. /* 2Gb DDP don't have 2 plane */
  1947. if (!ONENAND_IS_DDP(this))
  1948. this->options |= ONENAND_HAS_2PLANE;
  1949. this->options |= ONENAND_HAS_UNLOCK_ALL;
  1950. case ONENAND_DEVICE_DENSITY_1Gb:
  1951. /* A-Die has all block unlock */
  1952. if (process)
  1953. this->options |= ONENAND_HAS_UNLOCK_ALL;
  1954. break;
  1955. default:
  1956. /* Some OneNAND has continuous lock scheme */
  1957. if (!process)
  1958. this->options |= ONENAND_HAS_CONT_LOCK;
  1959. break;
  1960. }
  1961. if (this->options & ONENAND_HAS_CONT_LOCK)
  1962. printk(KERN_DEBUG "Lock scheme is Continuous Lock\n");
  1963. if (this->options & ONENAND_HAS_UNLOCK_ALL)
  1964. printk(KERN_DEBUG "Chip support all block unlock\n");
  1965. if (this->options & ONENAND_HAS_2PLANE)
  1966. printk(KERN_DEBUG "Chip has 2 plane\n");
  1967. }
  1968. /**
  1969. * onenand_print_device_info - Print device & version ID
  1970. * @param device device ID
  1971. * @param version version ID
  1972. *
  1973. * Print device & version ID
  1974. */
  1975. static void onenand_print_device_info(int device, int version)
  1976. {
  1977. int vcc, demuxed, ddp, density;
  1978. vcc = device & ONENAND_DEVICE_VCC_MASK;
  1979. demuxed = device & ONENAND_DEVICE_IS_DEMUX;
  1980. ddp = device & ONENAND_DEVICE_IS_DDP;
  1981. density = device >> ONENAND_DEVICE_DENSITY_SHIFT;
  1982. printk(KERN_INFO "%sOneNAND%s %dMB %sV 16-bit (0x%02x)\n",
  1983. demuxed ? "" : "Muxed ",
  1984. ddp ? "(DDP)" : "",
  1985. (16 << density),
  1986. vcc ? "2.65/3.3" : "1.8",
  1987. device);
  1988. printk(KERN_DEBUG "OneNAND version = 0x%04x\n", version);
  1989. }
  1990. static const struct onenand_manufacturers onenand_manuf_ids[] = {
  1991. {ONENAND_MFR_SAMSUNG, "Samsung"},
  1992. };
  1993. /**
  1994. * onenand_check_maf - Check manufacturer ID
  1995. * @param manuf manufacturer ID
  1996. *
  1997. * Check manufacturer ID
  1998. */
  1999. static int onenand_check_maf(int manuf)
  2000. {
  2001. int size = ARRAY_SIZE(onenand_manuf_ids);
  2002. char *name;
  2003. int i;
  2004. for (i = 0; i < size; i++)
  2005. if (manuf == onenand_manuf_ids[i].id)
  2006. break;
  2007. if (i < size)
  2008. name = onenand_manuf_ids[i].name;
  2009. else
  2010. name = "Unknown";
  2011. printk(KERN_DEBUG "OneNAND Manufacturer: %s (0x%0x)\n", name, manuf);
  2012. return (i == size);
  2013. }
  2014. /**
  2015. * onenand_probe - [OneNAND Interface] Probe the OneNAND device
  2016. * @param mtd MTD device structure
  2017. *
  2018. * OneNAND detection method:
  2019. * Compare the values from command with ones from register
  2020. */
  2021. static int onenand_probe(struct mtd_info *mtd)
  2022. {
  2023. struct onenand_chip *this = mtd->priv;
  2024. int bram_maf_id, bram_dev_id, maf_id, dev_id, ver_id;
  2025. int density;
  2026. int syscfg;
  2027. /* Save system configuration 1 */
  2028. syscfg = this->read_word(this->base + ONENAND_REG_SYS_CFG1);
  2029. /* Clear Sync. Burst Read mode to read BootRAM */
  2030. this->write_word((syscfg & ~ONENAND_SYS_CFG1_SYNC_READ), this->base + ONENAND_REG_SYS_CFG1);
  2031. /* Send the command for reading device ID from BootRAM */
  2032. this->write_word(ONENAND_CMD_READID, this->base + ONENAND_BOOTRAM);
  2033. /* Read manufacturer and device IDs from BootRAM */
  2034. bram_maf_id = this->read_word(this->base + ONENAND_BOOTRAM + 0x0);
  2035. bram_dev_id = this->read_word(this->base + ONENAND_BOOTRAM + 0x2);
  2036. /* Reset OneNAND to read default register values */
  2037. this->write_word(ONENAND_CMD_RESET, this->base + ONENAND_BOOTRAM);
  2038. /* Wait reset */
  2039. this->wait(mtd, FL_RESETING);
  2040. /* Restore system configuration 1 */
  2041. this->write_word(syscfg, this->base + ONENAND_REG_SYS_CFG1);
  2042. /* Check manufacturer ID */
  2043. if (onenand_check_maf(bram_maf_id))
  2044. return -ENXIO;
  2045. /* Read manufacturer and device IDs from Register */
  2046. maf_id = this->read_word(this->base + ONENAND_REG_MANUFACTURER_ID);
  2047. dev_id = this->read_word(this->base + ONENAND_REG_DEVICE_ID);
  2048. ver_id = this->read_word(this->base + ONENAND_REG_VERSION_ID);
  2049. /* Check OneNAND device */
  2050. if (maf_id != bram_maf_id || dev_id != bram_dev_id)
  2051. return -ENXIO;
  2052. /* Flash device information */
  2053. onenand_print_device_info(dev_id, ver_id);
  2054. this->device_id = dev_id;
  2055. this->version_id = ver_id;
  2056. density = dev_id >> ONENAND_DEVICE_DENSITY_SHIFT;
  2057. this->chipsize = (16 << density) << 20;
  2058. /* Set density mask. it is used for DDP */
  2059. if (ONENAND_IS_DDP(this))
  2060. this->density_mask = (1 << (density + 6));
  2061. else
  2062. this->density_mask = 0;
  2063. /* OneNAND page size & block size */
  2064. /* The data buffer size is equal to page size */
  2065. mtd->writesize = this->read_word(this->base + ONENAND_REG_DATA_BUFFER_SIZE);
  2066. mtd->oobsize = mtd->writesize >> 5;
  2067. /* Pages per a block are always 64 in OneNAND */
  2068. mtd->erasesize = mtd->writesize << 6;
  2069. this->erase_shift = ffs(mtd->erasesize) - 1;
  2070. this->page_shift = ffs(mtd->writesize) - 1;
  2071. this->page_mask = (1 << (this->erase_shift - this->page_shift)) - 1;
  2072. /* It's real page size */
  2073. this->writesize = mtd->writesize;
  2074. /* REVIST: Multichip handling */
  2075. mtd->size = this->chipsize;
  2076. /* Check OneNAND features */
  2077. onenand_check_features(mtd);
  2078. /*
  2079. * We emulate the 4KiB page and 256KiB erase block size
  2080. * But oobsize is still 64 bytes.
  2081. * It is only valid if you turn on 2X program support,
  2082. * Otherwise it will be ignored by compiler.
  2083. */
  2084. if (ONENAND_IS_2PLANE(this)) {
  2085. mtd->writesize <<= 1;
  2086. mtd->erasesize <<= 1;
  2087. }
  2088. return 0;
  2089. }
  2090. /**
  2091. * onenand_suspend - [MTD Interface] Suspend the OneNAND flash
  2092. * @param mtd MTD device structure
  2093. */
  2094. static int onenand_suspend(struct mtd_info *mtd)
  2095. {
  2096. return onenand_get_device(mtd, FL_PM_SUSPENDED);
  2097. }
  2098. /**
  2099. * onenand_resume - [MTD Interface] Resume the OneNAND flash
  2100. * @param mtd MTD device structure
  2101. */
  2102. static void onenand_resume(struct mtd_info *mtd)
  2103. {
  2104. struct onenand_chip *this = mtd->priv;
  2105. if (this->state == FL_PM_SUSPENDED)
  2106. onenand_release_device(mtd);
  2107. else
  2108. printk(KERN_ERR "resume() called for the chip which is not"
  2109. "in suspended state\n");
  2110. }
  2111. /**
  2112. * onenand_scan - [OneNAND Interface] Scan for the OneNAND device
  2113. * @param mtd MTD device structure
  2114. * @param maxchips Number of chips to scan for
  2115. *
  2116. * This fills out all the not initialized function pointers
  2117. * with the defaults.
  2118. * The flash ID is read and the mtd/chip structures are
  2119. * filled with the appropriate values.
  2120. */
  2121. int onenand_scan(struct mtd_info *mtd, int maxchips)
  2122. {
  2123. int i;
  2124. struct onenand_chip *this = mtd->priv;
  2125. if (!this->read_word)
  2126. this->read_word = onenand_readw;
  2127. if (!this->write_word)
  2128. this->write_word = onenand_writew;
  2129. if (!this->command)
  2130. this->command = onenand_command;
  2131. if (!this->wait)
  2132. onenand_setup_wait(mtd);
  2133. if (!this->read_bufferram)
  2134. this->read_bufferram = onenand_read_bufferram;
  2135. if (!this->write_bufferram)
  2136. this->write_bufferram = onenand_write_bufferram;
  2137. if (!this->block_markbad)
  2138. this->block_markbad = onenand_default_block_markbad;
  2139. if (!this->scan_bbt)
  2140. this->scan_bbt = onenand_default_bbt;
  2141. if (onenand_probe(mtd))
  2142. return -ENXIO;
  2143. /* Set Sync. Burst Read after probing */
  2144. if (this->mmcontrol) {
  2145. printk(KERN_INFO "OneNAND Sync. Burst Read support\n");
  2146. this->read_bufferram = onenand_sync_read_bufferram;
  2147. }
  2148. /* Allocate buffers, if necessary */
  2149. if (!this->page_buf) {
  2150. this->page_buf = kzalloc(mtd->writesize, GFP_KERNEL);
  2151. if (!this->page_buf) {
  2152. printk(KERN_ERR "onenand_scan(): Can't allocate page_buf\n");
  2153. return -ENOMEM;
  2154. }
  2155. this->options |= ONENAND_PAGEBUF_ALLOC;
  2156. }
  2157. if (!this->oob_buf) {
  2158. this->oob_buf = kzalloc(mtd->oobsize, GFP_KERNEL);
  2159. if (!this->oob_buf) {
  2160. printk(KERN_ERR "onenand_scan(): Can't allocate oob_buf\n");
  2161. if (this->options & ONENAND_PAGEBUF_ALLOC) {
  2162. this->options &= ~ONENAND_PAGEBUF_ALLOC;
  2163. kfree(this->page_buf);
  2164. }
  2165. return -ENOMEM;
  2166. }
  2167. this->options |= ONENAND_OOBBUF_ALLOC;
  2168. }
  2169. this->state = FL_READY;
  2170. init_waitqueue_head(&this->wq);
  2171. spin_lock_init(&this->chip_lock);
  2172. /*
  2173. * Allow subpage writes up to oobsize.
  2174. */
  2175. switch (mtd->oobsize) {
  2176. case 64:
  2177. this->ecclayout = &onenand_oob_64;
  2178. mtd->subpage_sft = 2;
  2179. break;
  2180. case 32:
  2181. this->ecclayout = &onenand_oob_32;
  2182. mtd->subpage_sft = 1;
  2183. break;
  2184. default:
  2185. printk(KERN_WARNING "No OOB scheme defined for oobsize %d\n",
  2186. mtd->oobsize);
  2187. mtd->subpage_sft = 0;
  2188. /* To prevent kernel oops */
  2189. this->ecclayout = &onenand_oob_32;
  2190. break;
  2191. }
  2192. this->subpagesize = mtd->writesize >> mtd->subpage_sft;
  2193. /*
  2194. * The number of bytes available for a client to place data into
  2195. * the out of band area
  2196. */
  2197. this->ecclayout->oobavail = 0;
  2198. for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES &&
  2199. this->ecclayout->oobfree[i].length; i++)
  2200. this->ecclayout->oobavail +=
  2201. this->ecclayout->oobfree[i].length;
  2202. mtd->oobavail = this->ecclayout->oobavail;
  2203. mtd->ecclayout = this->ecclayout;
  2204. /* Fill in remaining MTD driver data */
  2205. mtd->type = MTD_NANDFLASH;
  2206. mtd->flags = MTD_CAP_NANDFLASH;
  2207. mtd->erase = onenand_erase;
  2208. mtd->point = NULL;
  2209. mtd->unpoint = NULL;
  2210. mtd->read = onenand_read;
  2211. mtd->write = onenand_write;
  2212. mtd->read_oob = onenand_read_oob;
  2213. mtd->write_oob = onenand_write_oob;
  2214. #ifdef CONFIG_MTD_ONENAND_OTP
  2215. mtd->get_fact_prot_info = onenand_get_fact_prot_info;
  2216. mtd->read_fact_prot_reg = onenand_read_fact_prot_reg;
  2217. mtd->get_user_prot_info = onenand_get_user_prot_info;
  2218. mtd->read_user_prot_reg = onenand_read_user_prot_reg;
  2219. mtd->write_user_prot_reg = onenand_write_user_prot_reg;
  2220. mtd->lock_user_prot_reg = onenand_lock_user_prot_reg;
  2221. #endif
  2222. mtd->sync = onenand_sync;
  2223. mtd->lock = onenand_lock;
  2224. mtd->unlock = onenand_unlock;
  2225. mtd->suspend = onenand_suspend;
  2226. mtd->resume = onenand_resume;
  2227. mtd->block_isbad = onenand_block_isbad;
  2228. mtd->block_markbad = onenand_block_markbad;
  2229. mtd->owner = THIS_MODULE;
  2230. /* Unlock whole block */
  2231. onenand_unlock_all(mtd);
  2232. return this->scan_bbt(mtd);
  2233. }
  2234. /**
  2235. * onenand_release - [OneNAND Interface] Free resources held by the OneNAND device
  2236. * @param mtd MTD device structure
  2237. */
  2238. void onenand_release(struct mtd_info *mtd)
  2239. {
  2240. struct onenand_chip *this = mtd->priv;
  2241. #ifdef CONFIG_MTD_PARTITIONS
  2242. /* Deregister partitions */
  2243. del_mtd_partitions (mtd);
  2244. #endif
  2245. /* Deregister the device */
  2246. del_mtd_device (mtd);
  2247. /* Free bad block table memory, if allocated */
  2248. if (this->bbm) {
  2249. struct bbm_info *bbm = this->bbm;
  2250. kfree(bbm->bbt);
  2251. kfree(this->bbm);
  2252. }
  2253. /* Buffers allocated by onenand_scan */
  2254. if (this->options & ONENAND_PAGEBUF_ALLOC)
  2255. kfree(this->page_buf);
  2256. if (this->options & ONENAND_OOBBUF_ALLOC)
  2257. kfree(this->oob_buf);
  2258. }
  2259. EXPORT_SYMBOL_GPL(onenand_scan);
  2260. EXPORT_SYMBOL_GPL(onenand_release);
  2261. MODULE_LICENSE("GPL");
  2262. MODULE_AUTHOR("Kyungmin Park <kyungmin.park@samsung.com>");
  2263. MODULE_DESCRIPTION("Generic OneNAND flash driver code");