onenand_base.c 64 KB

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