onenand_base.c 70 KB

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