onenand_base.c 63 KB

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