onenand_base.c 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285
  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. DEBUG(MTD_DEBUG_LEVEL0, "onenand_wait: controller error = 0x%04x\n", ctrl);
  265. if (ctrl & ONENAND_CTRL_LOCK)
  266. DEBUG(MTD_DEBUG_LEVEL0, "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. DEBUG(MTD_DEBUG_LEVEL0, "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;
  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. return 1;
  493. /* Check another BufferRAM */
  494. i = ONENAND_NEXT_BUFFERRAM(this);
  495. if (this->bufferram[i].blockpage == blockpage) {
  496. ONENAND_SET_NEXT_BUFFERRAM(this);
  497. return 1;
  498. }
  499. return 0;
  500. }
  501. /**
  502. * onenand_update_bufferram - [GENERIC] Update BufferRAM information
  503. * @param mtd MTD data structure
  504. * @param addr address to update
  505. * @param valid valid flag
  506. *
  507. * Update BufferRAM information
  508. */
  509. static void onenand_update_bufferram(struct mtd_info *mtd, loff_t addr,
  510. int valid)
  511. {
  512. struct onenand_chip *this = mtd->priv;
  513. int blockpage;
  514. unsigned int i;
  515. blockpage = (int) (addr >> this->page_shift);
  516. /* Invalidate another BufferRAM */
  517. i = ONENAND_NEXT_BUFFERRAM(this);
  518. if (this->bufferram[i].blockpage == blockpage)
  519. this->bufferram[i].blockpage = -1;
  520. /* Update BufferRAM */
  521. i = ONENAND_CURRENT_BUFFERRAM(this);
  522. if (valid)
  523. this->bufferram[i].blockpage = blockpage;
  524. else
  525. this->bufferram[i].blockpage = -1;
  526. }
  527. /**
  528. * onenand_get_device - [GENERIC] Get chip for selected access
  529. * @param mtd MTD device structure
  530. * @param new_state the state which is requested
  531. *
  532. * Get the device and lock it for exclusive access
  533. */
  534. static int onenand_get_device(struct mtd_info *mtd, int new_state)
  535. {
  536. struct onenand_chip *this = mtd->priv;
  537. DECLARE_WAITQUEUE(wait, current);
  538. /*
  539. * Grab the lock and see if the device is available
  540. */
  541. while (1) {
  542. spin_lock(&this->chip_lock);
  543. if (this->state == FL_READY) {
  544. this->state = new_state;
  545. spin_unlock(&this->chip_lock);
  546. break;
  547. }
  548. if (new_state == FL_PM_SUSPENDED) {
  549. spin_unlock(&this->chip_lock);
  550. return (this->state == FL_PM_SUSPENDED) ? 0 : -EAGAIN;
  551. }
  552. set_current_state(TASK_UNINTERRUPTIBLE);
  553. add_wait_queue(&this->wq, &wait);
  554. spin_unlock(&this->chip_lock);
  555. schedule();
  556. remove_wait_queue(&this->wq, &wait);
  557. }
  558. return 0;
  559. }
  560. /**
  561. * onenand_release_device - [GENERIC] release chip
  562. * @param mtd MTD device structure
  563. *
  564. * Deselect, release chip lock and wake up anyone waiting on the device
  565. */
  566. static void onenand_release_device(struct mtd_info *mtd)
  567. {
  568. struct onenand_chip *this = mtd->priv;
  569. /* Release the chip */
  570. spin_lock(&this->chip_lock);
  571. this->state = FL_READY;
  572. wake_up(&this->wq);
  573. spin_unlock(&this->chip_lock);
  574. }
  575. /**
  576. * onenand_read - [MTD Interface] Read data from flash
  577. * @param mtd MTD device structure
  578. * @param from offset to read from
  579. * @param len number of bytes to read
  580. * @param retlen pointer to variable to store the number of read bytes
  581. * @param buf the databuffer to put data
  582. *
  583. * Read with ecc
  584. */
  585. static int onenand_read(struct mtd_info *mtd, loff_t from, size_t len,
  586. size_t *retlen, u_char *buf)
  587. {
  588. struct onenand_chip *this = mtd->priv;
  589. struct mtd_ecc_stats stats;
  590. int read = 0, column;
  591. int thislen;
  592. int ret = 0, boundary = 0;
  593. DEBUG(MTD_DEBUG_LEVEL3, "onenand_read: from = 0x%08x, len = %i\n", (unsigned int) from, (int) len);
  594. /* Do not allow reads past end of device */
  595. if ((from + len) > mtd->size) {
  596. DEBUG(MTD_DEBUG_LEVEL0, "onenand_read: Attempt read beyond end of device\n");
  597. *retlen = 0;
  598. return -EINVAL;
  599. }
  600. /* Grab the lock and see if the device is available */
  601. onenand_get_device(mtd, FL_READING);
  602. stats = mtd->ecc_stats;
  603. /* Read-while-load method */
  604. /* Do first load to bufferRAM */
  605. if (read < len) {
  606. if (!onenand_check_bufferram(mtd, from)) {
  607. this->command(mtd, ONENAND_CMD_READ, from, mtd->writesize);
  608. ret = this->wait(mtd, FL_READING);
  609. onenand_update_bufferram(mtd, from, !ret);
  610. }
  611. }
  612. thislen = min_t(int, mtd->writesize, len - read);
  613. column = from & (mtd->writesize - 1);
  614. if (column + thislen > mtd->writesize)
  615. thislen = mtd->writesize - column;
  616. while (!ret) {
  617. /* If there is more to load then start next load */
  618. from += thislen;
  619. if (read + thislen < len) {
  620. this->command(mtd, ONENAND_CMD_READ, from, mtd->writesize);
  621. /*
  622. * Chip boundary handling in DDP
  623. * Now we issued chip 1 read and pointed chip 1
  624. * bufferam so we have to point chip 0 bufferam.
  625. */
  626. if (ONENAND_IS_DDP(this) &&
  627. unlikely(from == (this->chipsize >> 1))) {
  628. this->write_word(ONENAND_DDP_CHIP0, this->base + ONENAND_REG_START_ADDRESS2);
  629. boundary = 1;
  630. } else
  631. boundary = 0;
  632. ONENAND_SET_PREV_BUFFERRAM(this);
  633. }
  634. /* While load is going, read from last bufferRAM */
  635. this->read_bufferram(mtd, ONENAND_DATARAM, buf, column, thislen);
  636. /* See if we are done */
  637. read += thislen;
  638. if (read == len)
  639. break;
  640. /* Set up for next read from bufferRAM */
  641. if (unlikely(boundary))
  642. this->write_word(ONENAND_DDP_CHIP1, this->base + ONENAND_REG_START_ADDRESS2);
  643. ONENAND_SET_NEXT_BUFFERRAM(this);
  644. buf += thislen;
  645. thislen = min_t(int, mtd->writesize, len - read);
  646. column = 0;
  647. cond_resched();
  648. /* Now wait for load */
  649. ret = this->wait(mtd, FL_READING);
  650. onenand_update_bufferram(mtd, from, !ret);
  651. }
  652. /* Deselect and wake up anyone waiting on the device */
  653. onenand_release_device(mtd);
  654. /*
  655. * Return success, if no ECC failures, else -EBADMSG
  656. * fs driver will take care of that, because
  657. * retlen == desired len and result == -EBADMSG
  658. */
  659. *retlen = read;
  660. if (mtd->ecc_stats.failed - stats.failed)
  661. return -EBADMSG;
  662. if (ret)
  663. return ret;
  664. return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
  665. }
  666. /**
  667. * onenand_transfer_auto_oob - [Internal] oob auto-placement transfer
  668. * @param mtd MTD device structure
  669. * @param buf destination address
  670. * @param column oob offset to read from
  671. * @param thislen oob length to read
  672. */
  673. static int onenand_transfer_auto_oob(struct mtd_info *mtd, uint8_t *buf, int column,
  674. int thislen)
  675. {
  676. struct onenand_chip *this = mtd->priv;
  677. struct nand_oobfree *free;
  678. int readcol = column;
  679. int readend = column + thislen;
  680. int lastgap = 0;
  681. uint8_t *oob_buf = this->page_buf + mtd->writesize;
  682. for (free = this->ecclayout->oobfree; free->length; ++free) {
  683. if (readcol >= lastgap)
  684. readcol += free->offset - lastgap;
  685. if (readend >= lastgap)
  686. readend += free->offset - lastgap;
  687. lastgap = free->offset + free->length;
  688. }
  689. this->read_bufferram(mtd, ONENAND_SPARERAM, oob_buf, 0, mtd->oobsize);
  690. for (free = this->ecclayout->oobfree; free->length; ++free) {
  691. int free_end = free->offset + free->length;
  692. if (free->offset < readend && free_end > readcol) {
  693. int st = max_t(int,free->offset,readcol);
  694. int ed = min_t(int,free_end,readend);
  695. int n = ed - st;
  696. memcpy(buf, oob_buf + st, n);
  697. buf += n;
  698. }
  699. }
  700. return 0;
  701. }
  702. /**
  703. * onenand_do_read_oob - [MTD Interface] OneNAND read out-of-band
  704. * @param mtd MTD device structure
  705. * @param from offset to read from
  706. * @param len number of bytes to read
  707. * @param retlen pointer to variable to store the number of read bytes
  708. * @param buf the databuffer to put data
  709. * @param mode operation mode
  710. *
  711. * OneNAND read out-of-band data from the spare area
  712. */
  713. int onenand_do_read_oob(struct mtd_info *mtd, loff_t from, size_t len,
  714. size_t *retlen, u_char *buf, mtd_oob_mode_t mode)
  715. {
  716. struct onenand_chip *this = mtd->priv;
  717. int read = 0, thislen, column, oobsize;
  718. int ret = 0;
  719. DEBUG(MTD_DEBUG_LEVEL3, "onenand_read_oob: from = 0x%08x, len = %i\n", (unsigned int) from, (int) len);
  720. /* Initialize return length value */
  721. *retlen = 0;
  722. if (mode == MTD_OOB_AUTO)
  723. oobsize = this->ecclayout->oobavail;
  724. else
  725. oobsize = mtd->oobsize;
  726. column = from & (mtd->oobsize - 1);
  727. if (unlikely(column >= oobsize)) {
  728. DEBUG(MTD_DEBUG_LEVEL0, "onenand_read_oob: Attempted to start read outside oob\n");
  729. return -EINVAL;
  730. }
  731. /* Do not allow reads past end of device */
  732. if (unlikely(from >= mtd->size ||
  733. column + len > ((mtd->size >> this->page_shift) -
  734. (from >> this->page_shift)) * oobsize)) {
  735. DEBUG(MTD_DEBUG_LEVEL0, "onenand_read_oob: Attempted to read beyond end of device\n");
  736. return -EINVAL;
  737. }
  738. /* Grab the lock and see if the device is available */
  739. onenand_get_device(mtd, FL_READING);
  740. while (read < len) {
  741. cond_resched();
  742. thislen = oobsize - column;
  743. thislen = min_t(int, thislen, len);
  744. this->command(mtd, ONENAND_CMD_READOOB, from, mtd->oobsize);
  745. onenand_update_bufferram(mtd, from, 0);
  746. ret = this->wait(mtd, FL_READING);
  747. /* First copy data and check return value for ECC handling */
  748. if (mode == MTD_OOB_AUTO)
  749. onenand_transfer_auto_oob(mtd, buf, column, thislen);
  750. else
  751. this->read_bufferram(mtd, ONENAND_SPARERAM, buf, column, thislen);
  752. if (ret) {
  753. DEBUG(MTD_DEBUG_LEVEL0, "onenand_read_oob: read failed = 0x%x\n", ret);
  754. break;
  755. }
  756. read += thislen;
  757. if (read == len)
  758. break;
  759. buf += thislen;
  760. /* Read more? */
  761. if (read < len) {
  762. /* Page size */
  763. from += mtd->writesize;
  764. column = 0;
  765. }
  766. }
  767. /* Deselect and wake up anyone waiting on the device */
  768. onenand_release_device(mtd);
  769. *retlen = read;
  770. return ret;
  771. }
  772. /**
  773. * onenand_read_oob - [MTD Interface] NAND write data and/or out-of-band
  774. * @mtd: MTD device structure
  775. * @from: offset to read from
  776. * @ops: oob operation description structure
  777. */
  778. static int onenand_read_oob(struct mtd_info *mtd, loff_t from,
  779. struct mtd_oob_ops *ops)
  780. {
  781. switch (ops->mode) {
  782. case MTD_OOB_PLACE:
  783. case MTD_OOB_AUTO:
  784. break;
  785. case MTD_OOB_RAW:
  786. /* Not implemented yet */
  787. default:
  788. return -EINVAL;
  789. }
  790. return onenand_do_read_oob(mtd, from + ops->ooboffs, ops->ooblen,
  791. &ops->oobretlen, ops->oobbuf, ops->mode);
  792. }
  793. #ifdef CONFIG_MTD_ONENAND_VERIFY_WRITE
  794. /**
  795. * onenand_verify_oob - [GENERIC] verify the oob contents after a write
  796. * @param mtd MTD device structure
  797. * @param buf the databuffer to verify
  798. * @param to offset to read from
  799. *
  800. */
  801. static int onenand_verify_oob(struct mtd_info *mtd, const u_char *buf, loff_t to)
  802. {
  803. struct onenand_chip *this = mtd->priv;
  804. char *readp = this->page_buf + mtd->writesize;
  805. int status, i;
  806. this->command(mtd, ONENAND_CMD_READOOB, to, mtd->oobsize);
  807. onenand_update_bufferram(mtd, to, 0);
  808. status = this->wait(mtd, FL_READING);
  809. if (status)
  810. return status;
  811. this->read_bufferram(mtd, ONENAND_SPARERAM, readp, 0, mtd->oobsize);
  812. for(i = 0; i < mtd->oobsize; i++)
  813. if (buf[i] != 0xFF && buf[i] != readp[i])
  814. return -EBADMSG;
  815. return 0;
  816. }
  817. /**
  818. * onenand_verify - [GENERIC] verify the chip contents after a write
  819. * @param mtd MTD device structure
  820. * @param buf the databuffer to verify
  821. * @param addr offset to read from
  822. * @param len number of bytes to read and compare
  823. *
  824. */
  825. static int onenand_verify(struct mtd_info *mtd, const u_char *buf, loff_t addr, size_t len)
  826. {
  827. struct onenand_chip *this = mtd->priv;
  828. void __iomem *dataram;
  829. int ret = 0;
  830. int thislen, column;
  831. while (len != 0) {
  832. thislen = min_t(int, mtd->writesize, len);
  833. column = addr & (mtd->writesize - 1);
  834. if (column + thislen > mtd->writesize)
  835. thislen = mtd->writesize - column;
  836. this->command(mtd, ONENAND_CMD_READ, addr, mtd->writesize);
  837. onenand_update_bufferram(mtd, addr, 0);
  838. ret = this->wait(mtd, FL_READING);
  839. if (ret)
  840. return ret;
  841. onenand_update_bufferram(mtd, addr, 1);
  842. dataram = this->base + ONENAND_DATARAM;
  843. dataram += onenand_bufferram_offset(mtd, ONENAND_DATARAM);
  844. if (memcmp(buf, dataram + column, thislen))
  845. return -EBADMSG;
  846. len -= thislen;
  847. buf += thislen;
  848. addr += thislen;
  849. }
  850. return 0;
  851. }
  852. #else
  853. #define onenand_verify(...) (0)
  854. #define onenand_verify_oob(...) (0)
  855. #endif
  856. #define NOTALIGNED(x) ((x & (this->subpagesize - 1)) != 0)
  857. /**
  858. * onenand_write - [MTD Interface] write buffer to FLASH
  859. * @param mtd MTD device structure
  860. * @param to offset to write to
  861. * @param len number of bytes to write
  862. * @param retlen pointer to variable to store the number of written bytes
  863. * @param buf the data to write
  864. *
  865. * Write with ECC
  866. */
  867. static int onenand_write(struct mtd_info *mtd, loff_t to, size_t len,
  868. size_t *retlen, const u_char *buf)
  869. {
  870. struct onenand_chip *this = mtd->priv;
  871. int written = 0;
  872. int ret = 0;
  873. int column, subpage;
  874. DEBUG(MTD_DEBUG_LEVEL3, "onenand_write: to = 0x%08x, len = %i\n", (unsigned int) to, (int) len);
  875. /* Initialize retlen, in case of early exit */
  876. *retlen = 0;
  877. /* Do not allow writes past end of device */
  878. if (unlikely((to + len) > mtd->size)) {
  879. DEBUG(MTD_DEBUG_LEVEL0, "onenand_write: Attempt write to past end of device\n");
  880. return -EINVAL;
  881. }
  882. /* Reject writes, which are not page aligned */
  883. if (unlikely(NOTALIGNED(to)) || unlikely(NOTALIGNED(len))) {
  884. DEBUG(MTD_DEBUG_LEVEL0, "onenand_write: Attempt to write not page aligned data\n");
  885. return -EINVAL;
  886. }
  887. column = to & (mtd->writesize - 1);
  888. subpage = column || (len & (mtd->writesize - 1));
  889. /* Grab the lock and see if the device is available */
  890. onenand_get_device(mtd, FL_WRITING);
  891. /* Loop until all data write */
  892. while (written < len) {
  893. int bytes = mtd->writesize;
  894. int thislen = min_t(int, bytes, len - written);
  895. u_char *wbuf = (u_char *) buf;
  896. cond_resched();
  897. this->command(mtd, ONENAND_CMD_BUFFERRAM, to, bytes);
  898. /* Partial page write */
  899. if (subpage) {
  900. bytes = min_t(int, bytes - column, (int) len);
  901. memset(this->page_buf, 0xff, mtd->writesize);
  902. memcpy(this->page_buf + column, buf, bytes);
  903. wbuf = this->page_buf;
  904. /* Even though partial write, we need page size */
  905. thislen = mtd->writesize;
  906. }
  907. this->write_bufferram(mtd, ONENAND_DATARAM, wbuf, 0, thislen);
  908. this->write_bufferram(mtd, ONENAND_SPARERAM, ffchars, 0, mtd->oobsize);
  909. this->command(mtd, ONENAND_CMD_PROG, to, mtd->writesize);
  910. /* In partial page write we don't update bufferram */
  911. onenand_update_bufferram(mtd, to, !subpage);
  912. ret = this->wait(mtd, FL_WRITING);
  913. if (ret) {
  914. DEBUG(MTD_DEBUG_LEVEL0, "onenand_write: write filaed %d\n", ret);
  915. break;
  916. }
  917. /* Only check verify write turn on */
  918. ret = onenand_verify(mtd, (u_char *) wbuf, to, thislen);
  919. if (ret) {
  920. DEBUG(MTD_DEBUG_LEVEL0, "onenand_write: verify failed %d\n", ret);
  921. break;
  922. }
  923. written += thislen;
  924. if (written == len)
  925. break;
  926. column = 0;
  927. to += thislen;
  928. buf += thislen;
  929. }
  930. /* Deselect and wake up anyone waiting on the device */
  931. onenand_release_device(mtd);
  932. *retlen = written;
  933. return ret;
  934. }
  935. /**
  936. * onenand_fill_auto_oob - [Internal] oob auto-placement transfer
  937. * @param mtd MTD device structure
  938. * @param oob_buf oob buffer
  939. * @param buf source address
  940. * @param column oob offset to write to
  941. * @param thislen oob length to write
  942. */
  943. static int onenand_fill_auto_oob(struct mtd_info *mtd, u_char *oob_buf,
  944. const u_char *buf, int column, int thislen)
  945. {
  946. struct onenand_chip *this = mtd->priv;
  947. struct nand_oobfree *free;
  948. int writecol = column;
  949. int writeend = column + thislen;
  950. int lastgap = 0;
  951. for (free = this->ecclayout->oobfree; free->length; ++free) {
  952. if (writecol >= lastgap)
  953. writecol += free->offset - lastgap;
  954. if (writeend >= lastgap)
  955. writeend += free->offset - lastgap;
  956. lastgap = free->offset + free->length;
  957. }
  958. for (free = this->ecclayout->oobfree; free->length; ++free) {
  959. int free_end = free->offset + free->length;
  960. if (free->offset < writeend && free_end > writecol) {
  961. int st = max_t(int,free->offset,writecol);
  962. int ed = min_t(int,free_end,writeend);
  963. int n = ed - st;
  964. memcpy(oob_buf + st, buf, n);
  965. buf += n;
  966. }
  967. }
  968. return 0;
  969. }
  970. /**
  971. * onenand_do_write_oob - [Internal] OneNAND write out-of-band
  972. * @param mtd MTD device structure
  973. * @param to offset to write to
  974. * @param len number of bytes to write
  975. * @param retlen pointer to variable to store the number of written bytes
  976. * @param buf the data to write
  977. * @param mode operation mode
  978. *
  979. * OneNAND write out-of-band
  980. */
  981. static int onenand_do_write_oob(struct mtd_info *mtd, loff_t to, size_t len,
  982. size_t *retlen, const u_char *buf, mtd_oob_mode_t mode)
  983. {
  984. struct onenand_chip *this = mtd->priv;
  985. int column, ret = 0, oobsize;
  986. int written = 0;
  987. DEBUG(MTD_DEBUG_LEVEL3, "onenand_write_oob: to = 0x%08x, len = %i\n", (unsigned int) to, (int) len);
  988. /* Initialize retlen, in case of early exit */
  989. *retlen = 0;
  990. if (mode == MTD_OOB_AUTO)
  991. oobsize = this->ecclayout->oobavail;
  992. else
  993. oobsize = mtd->oobsize;
  994. column = to & (mtd->oobsize - 1);
  995. if (unlikely(column >= oobsize)) {
  996. DEBUG(MTD_DEBUG_LEVEL0, "onenand_write_oob: Attempted to start write outside oob\n");
  997. return -EINVAL;
  998. }
  999. /* Do not allow reads past end of device */
  1000. if (unlikely(to >= mtd->size ||
  1001. column + len > ((mtd->size >> this->page_shift) -
  1002. (to >> this->page_shift)) * oobsize)) {
  1003. DEBUG(MTD_DEBUG_LEVEL0, "onenand_write_oob: Attempted to write past end of device\n");
  1004. return -EINVAL;
  1005. }
  1006. /* Grab the lock and see if the device is available */
  1007. onenand_get_device(mtd, FL_WRITING);
  1008. /* Loop until all data write */
  1009. while (written < len) {
  1010. int thislen = min_t(int, oobsize, len - written);
  1011. cond_resched();
  1012. this->command(mtd, ONENAND_CMD_BUFFERRAM, to, mtd->oobsize);
  1013. /* We send data to spare ram with oobsize
  1014. * to prevent byte access */
  1015. memset(this->page_buf, 0xff, mtd->oobsize);
  1016. if (mode == MTD_OOB_AUTO)
  1017. onenand_fill_auto_oob(mtd, this->page_buf, buf, column, thislen);
  1018. else
  1019. memcpy(this->page_buf + column, buf, thislen);
  1020. this->write_bufferram(mtd, ONENAND_SPARERAM, this->page_buf, 0, mtd->oobsize);
  1021. this->command(mtd, ONENAND_CMD_PROGOOB, to, mtd->oobsize);
  1022. onenand_update_bufferram(mtd, to, 0);
  1023. ret = this->wait(mtd, FL_WRITING);
  1024. if (ret) {
  1025. DEBUG(MTD_DEBUG_LEVEL0, "onenand_write_oob: write failed %d\n", ret);
  1026. break;
  1027. }
  1028. ret = onenand_verify_oob(mtd, this->page_buf, to);
  1029. if (ret) {
  1030. DEBUG(MTD_DEBUG_LEVEL0, "onenand_write_oob: verify failed %d\n", ret);
  1031. break;
  1032. }
  1033. written += thislen;
  1034. if (written == len)
  1035. break;
  1036. to += mtd->writesize;
  1037. buf += thislen;
  1038. column = 0;
  1039. }
  1040. /* Deselect and wake up anyone waiting on the device */
  1041. onenand_release_device(mtd);
  1042. *retlen = written;
  1043. return ret;
  1044. }
  1045. /**
  1046. * onenand_write_oob - [MTD Interface] NAND write data and/or out-of-band
  1047. * @mtd: MTD device structure
  1048. * @from: offset to read from
  1049. * @ops: oob operation description structure
  1050. */
  1051. static int onenand_write_oob(struct mtd_info *mtd, loff_t to,
  1052. struct mtd_oob_ops *ops)
  1053. {
  1054. switch (ops->mode) {
  1055. case MTD_OOB_PLACE:
  1056. case MTD_OOB_AUTO:
  1057. break;
  1058. case MTD_OOB_RAW:
  1059. /* Not implemented yet */
  1060. default:
  1061. return -EINVAL;
  1062. }
  1063. return onenand_do_write_oob(mtd, to + ops->ooboffs, ops->ooblen,
  1064. &ops->oobretlen, ops->oobbuf, ops->mode);
  1065. }
  1066. /**
  1067. * onenand_block_checkbad - [GENERIC] Check if a block is marked bad
  1068. * @param mtd MTD device structure
  1069. * @param ofs offset from device start
  1070. * @param getchip 0, if the chip is already selected
  1071. * @param allowbbt 1, if its allowed to access the bbt area
  1072. *
  1073. * Check, if the block is bad. Either by reading the bad block table or
  1074. * calling of the scan function.
  1075. */
  1076. static int onenand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int getchip, int allowbbt)
  1077. {
  1078. struct onenand_chip *this = mtd->priv;
  1079. struct bbm_info *bbm = this->bbm;
  1080. /* Return info from the table */
  1081. return bbm->isbad_bbt(mtd, ofs, allowbbt);
  1082. }
  1083. /**
  1084. * onenand_erase - [MTD Interface] erase block(s)
  1085. * @param mtd MTD device structure
  1086. * @param instr erase instruction
  1087. *
  1088. * Erase one ore more blocks
  1089. */
  1090. static int onenand_erase(struct mtd_info *mtd, struct erase_info *instr)
  1091. {
  1092. struct onenand_chip *this = mtd->priv;
  1093. unsigned int block_size;
  1094. loff_t addr;
  1095. int len;
  1096. int ret = 0;
  1097. DEBUG(MTD_DEBUG_LEVEL3, "onenand_erase: start = 0x%08x, len = %i\n", (unsigned int) instr->addr, (unsigned int) instr->len);
  1098. block_size = (1 << this->erase_shift);
  1099. /* Start address must align on block boundary */
  1100. if (unlikely(instr->addr & (block_size - 1))) {
  1101. DEBUG(MTD_DEBUG_LEVEL0, "onenand_erase: Unaligned address\n");
  1102. return -EINVAL;
  1103. }
  1104. /* Length must align on block boundary */
  1105. if (unlikely(instr->len & (block_size - 1))) {
  1106. DEBUG(MTD_DEBUG_LEVEL0, "onenand_erase: Length not block aligned\n");
  1107. return -EINVAL;
  1108. }
  1109. /* Do not allow erase past end of device */
  1110. if (unlikely((instr->len + instr->addr) > mtd->size)) {
  1111. DEBUG(MTD_DEBUG_LEVEL0, "onenand_erase: Erase past end of device\n");
  1112. return -EINVAL;
  1113. }
  1114. instr->fail_addr = 0xffffffff;
  1115. /* Grab the lock and see if the device is available */
  1116. onenand_get_device(mtd, FL_ERASING);
  1117. /* Loop throught the pages */
  1118. len = instr->len;
  1119. addr = instr->addr;
  1120. instr->state = MTD_ERASING;
  1121. while (len) {
  1122. cond_resched();
  1123. /* Check if we have a bad block, we do not erase bad blocks */
  1124. if (onenand_block_checkbad(mtd, addr, 0, 0)) {
  1125. printk (KERN_WARNING "onenand_erase: attempt to erase a bad block at addr 0x%08x\n", (unsigned int) addr);
  1126. instr->state = MTD_ERASE_FAILED;
  1127. goto erase_exit;
  1128. }
  1129. this->command(mtd, ONENAND_CMD_ERASE, addr, block_size);
  1130. ret = this->wait(mtd, FL_ERASING);
  1131. /* Check, if it is write protected */
  1132. if (ret) {
  1133. DEBUG(MTD_DEBUG_LEVEL0, "onenand_erase: Failed erase, block %d\n", (unsigned) (addr >> this->erase_shift));
  1134. instr->state = MTD_ERASE_FAILED;
  1135. instr->fail_addr = addr;
  1136. goto erase_exit;
  1137. }
  1138. len -= block_size;
  1139. addr += block_size;
  1140. }
  1141. instr->state = MTD_ERASE_DONE;
  1142. erase_exit:
  1143. ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
  1144. /* Do call back function */
  1145. if (!ret)
  1146. mtd_erase_callback(instr);
  1147. /* Deselect and wake up anyone waiting on the device */
  1148. onenand_release_device(mtd);
  1149. return ret;
  1150. }
  1151. /**
  1152. * onenand_sync - [MTD Interface] sync
  1153. * @param mtd MTD device structure
  1154. *
  1155. * Sync is actually a wait for chip ready function
  1156. */
  1157. static void onenand_sync(struct mtd_info *mtd)
  1158. {
  1159. DEBUG(MTD_DEBUG_LEVEL3, "onenand_sync: called\n");
  1160. /* Grab the lock and see if the device is available */
  1161. onenand_get_device(mtd, FL_SYNCING);
  1162. /* Release it and go back */
  1163. onenand_release_device(mtd);
  1164. }
  1165. /**
  1166. * onenand_block_isbad - [MTD Interface] Check whether the block at the given offset is bad
  1167. * @param mtd MTD device structure
  1168. * @param ofs offset relative to mtd start
  1169. *
  1170. * Check whether the block is bad
  1171. */
  1172. static int onenand_block_isbad(struct mtd_info *mtd, loff_t ofs)
  1173. {
  1174. /* Check for invalid offset */
  1175. if (ofs > mtd->size)
  1176. return -EINVAL;
  1177. return onenand_block_checkbad(mtd, ofs, 1, 0);
  1178. }
  1179. /**
  1180. * onenand_default_block_markbad - [DEFAULT] mark a block bad
  1181. * @param mtd MTD device structure
  1182. * @param ofs offset from device start
  1183. *
  1184. * This is the default implementation, which can be overridden by
  1185. * a hardware specific driver.
  1186. */
  1187. static int onenand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
  1188. {
  1189. struct onenand_chip *this = mtd->priv;
  1190. struct bbm_info *bbm = this->bbm;
  1191. u_char buf[2] = {0, 0};
  1192. size_t retlen;
  1193. int block;
  1194. /* Get block number */
  1195. block = ((int) ofs) >> bbm->bbt_erase_shift;
  1196. if (bbm->bbt)
  1197. bbm->bbt[block >> 2] |= 0x01 << ((block & 0x03) << 1);
  1198. /* We write two bytes, so we dont have to mess with 16 bit access */
  1199. ofs += mtd->oobsize + (bbm->badblockpos & ~0x01);
  1200. return onenand_do_write_oob(mtd, ofs , 2, &retlen, buf, MTD_OOB_PLACE);
  1201. }
  1202. /**
  1203. * onenand_block_markbad - [MTD Interface] Mark the block at the given offset as bad
  1204. * @param mtd MTD device structure
  1205. * @param ofs offset relative to mtd start
  1206. *
  1207. * Mark the block as bad
  1208. */
  1209. static int onenand_block_markbad(struct mtd_info *mtd, loff_t ofs)
  1210. {
  1211. struct onenand_chip *this = mtd->priv;
  1212. int ret;
  1213. ret = onenand_block_isbad(mtd, ofs);
  1214. if (ret) {
  1215. /* If it was bad already, return success and do nothing */
  1216. if (ret > 0)
  1217. return 0;
  1218. return ret;
  1219. }
  1220. return this->block_markbad(mtd, ofs);
  1221. }
  1222. /**
  1223. * onenand_do_lock_cmd - [OneNAND Interface] Lock or unlock block(s)
  1224. * @param mtd MTD device structure
  1225. * @param ofs offset relative to mtd start
  1226. * @param len number of bytes to lock or unlock
  1227. *
  1228. * Lock or unlock one or more blocks
  1229. */
  1230. static int onenand_do_lock_cmd(struct mtd_info *mtd, loff_t ofs, size_t len, int cmd)
  1231. {
  1232. struct onenand_chip *this = mtd->priv;
  1233. int start, end, block, value, status;
  1234. int wp_status_mask;
  1235. start = ofs >> this->erase_shift;
  1236. end = len >> this->erase_shift;
  1237. if (cmd == ONENAND_CMD_LOCK)
  1238. wp_status_mask = ONENAND_WP_LS;
  1239. else
  1240. wp_status_mask = ONENAND_WP_US;
  1241. /* Continuous lock scheme */
  1242. if (this->options & ONENAND_HAS_CONT_LOCK) {
  1243. /* Set start block address */
  1244. this->write_word(start, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
  1245. /* Set end block address */
  1246. this->write_word(start + end - 1, this->base + ONENAND_REG_END_BLOCK_ADDRESS);
  1247. /* Write lock command */
  1248. this->command(mtd, cmd, 0, 0);
  1249. /* There's no return value */
  1250. this->wait(mtd, FL_LOCKING);
  1251. /* Sanity check */
  1252. while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
  1253. & ONENAND_CTRL_ONGO)
  1254. continue;
  1255. /* Check lock status */
  1256. status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
  1257. if (!(status & wp_status_mask))
  1258. printk(KERN_ERR "wp status = 0x%x\n", status);
  1259. return 0;
  1260. }
  1261. /* Block lock scheme */
  1262. for (block = start; block < start + end; block++) {
  1263. /* Set block address */
  1264. value = onenand_block_address(this, block);
  1265. this->write_word(value, this->base + ONENAND_REG_START_ADDRESS1);
  1266. /* Select DataRAM for DDP */
  1267. value = onenand_bufferram_address(this, block);
  1268. this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
  1269. /* Set start block address */
  1270. this->write_word(block, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
  1271. /* Write lock command */
  1272. this->command(mtd, cmd, 0, 0);
  1273. /* There's no return value */
  1274. this->wait(mtd, FL_LOCKING);
  1275. /* Sanity check */
  1276. while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
  1277. & ONENAND_CTRL_ONGO)
  1278. continue;
  1279. /* Check lock status */
  1280. status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
  1281. if (!(status & wp_status_mask))
  1282. printk(KERN_ERR "block = %d, wp status = 0x%x\n", block, status);
  1283. }
  1284. return 0;
  1285. }
  1286. /**
  1287. * onenand_lock - [MTD Interface] Lock block(s)
  1288. * @param mtd MTD device structure
  1289. * @param ofs offset relative to mtd start
  1290. * @param len number of bytes to unlock
  1291. *
  1292. * Lock one or more blocks
  1293. */
  1294. static int onenand_lock(struct mtd_info *mtd, loff_t ofs, size_t len)
  1295. {
  1296. return onenand_do_lock_cmd(mtd, ofs, len, ONENAND_CMD_LOCK);
  1297. }
  1298. /**
  1299. * onenand_unlock - [MTD Interface] Unlock block(s)
  1300. * @param mtd MTD device structure
  1301. * @param ofs offset relative to mtd start
  1302. * @param len number of bytes to unlock
  1303. *
  1304. * Unlock one or more blocks
  1305. */
  1306. static int onenand_unlock(struct mtd_info *mtd, loff_t ofs, size_t len)
  1307. {
  1308. return onenand_do_lock_cmd(mtd, ofs, len, ONENAND_CMD_UNLOCK);
  1309. }
  1310. /**
  1311. * onenand_check_lock_status - [OneNAND Interface] Check lock status
  1312. * @param this onenand chip data structure
  1313. *
  1314. * Check lock status
  1315. */
  1316. static void onenand_check_lock_status(struct onenand_chip *this)
  1317. {
  1318. unsigned int value, block, status;
  1319. unsigned int end;
  1320. end = this->chipsize >> this->erase_shift;
  1321. for (block = 0; block < end; block++) {
  1322. /* Set block address */
  1323. value = onenand_block_address(this, block);
  1324. this->write_word(value, this->base + ONENAND_REG_START_ADDRESS1);
  1325. /* Select DataRAM for DDP */
  1326. value = onenand_bufferram_address(this, block);
  1327. this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
  1328. /* Set start block address */
  1329. this->write_word(block, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
  1330. /* Check lock status */
  1331. status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
  1332. if (!(status & ONENAND_WP_US))
  1333. printk(KERN_ERR "block = %d, wp status = 0x%x\n", block, status);
  1334. }
  1335. }
  1336. /**
  1337. * onenand_unlock_all - [OneNAND Interface] unlock all blocks
  1338. * @param mtd MTD device structure
  1339. *
  1340. * Unlock all blocks
  1341. */
  1342. static int onenand_unlock_all(struct mtd_info *mtd)
  1343. {
  1344. struct onenand_chip *this = mtd->priv;
  1345. if (this->options & ONENAND_HAS_UNLOCK_ALL) {
  1346. /* Set start block address */
  1347. this->write_word(0, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
  1348. /* Write unlock command */
  1349. this->command(mtd, ONENAND_CMD_UNLOCK_ALL, 0, 0);
  1350. /* There's no return value */
  1351. this->wait(mtd, FL_LOCKING);
  1352. /* Sanity check */
  1353. while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
  1354. & ONENAND_CTRL_ONGO)
  1355. continue;
  1356. /* Workaround for all block unlock in DDP */
  1357. if (ONENAND_IS_DDP(this)) {
  1358. /* 1st block on another chip */
  1359. loff_t ofs = this->chipsize >> 1;
  1360. size_t len = mtd->erasesize;
  1361. onenand_unlock(mtd, ofs, len);
  1362. }
  1363. onenand_check_lock_status(this);
  1364. return 0;
  1365. }
  1366. onenand_unlock(mtd, 0x0, this->chipsize);
  1367. return 0;
  1368. }
  1369. #ifdef CONFIG_MTD_ONENAND_OTP
  1370. /* Interal OTP operation */
  1371. typedef int (*otp_op_t)(struct mtd_info *mtd, loff_t form, size_t len,
  1372. size_t *retlen, u_char *buf);
  1373. /**
  1374. * do_otp_read - [DEFAULT] Read OTP block area
  1375. * @param mtd MTD device structure
  1376. * @param from The offset to read
  1377. * @param len number of bytes to read
  1378. * @param retlen pointer to variable to store the number of readbytes
  1379. * @param buf the databuffer to put/get data
  1380. *
  1381. * Read OTP block area.
  1382. */
  1383. static int do_otp_read(struct mtd_info *mtd, loff_t from, size_t len,
  1384. size_t *retlen, u_char *buf)
  1385. {
  1386. struct onenand_chip *this = mtd->priv;
  1387. int ret;
  1388. /* Enter OTP access mode */
  1389. this->command(mtd, ONENAND_CMD_OTP_ACCESS, 0, 0);
  1390. this->wait(mtd, FL_OTPING);
  1391. ret = mtd->read(mtd, from, len, retlen, buf);
  1392. /* Exit OTP access mode */
  1393. this->command(mtd, ONENAND_CMD_RESET, 0, 0);
  1394. this->wait(mtd, FL_RESETING);
  1395. return ret;
  1396. }
  1397. /**
  1398. * do_otp_write - [DEFAULT] Write OTP block area
  1399. * @param mtd MTD device structure
  1400. * @param from The offset to write
  1401. * @param len number of bytes to write
  1402. * @param retlen pointer to variable to store the number of write bytes
  1403. * @param buf the databuffer to put/get data
  1404. *
  1405. * Write OTP block area.
  1406. */
  1407. static int do_otp_write(struct mtd_info *mtd, loff_t from, size_t len,
  1408. size_t *retlen, u_char *buf)
  1409. {
  1410. struct onenand_chip *this = mtd->priv;
  1411. unsigned char *pbuf = buf;
  1412. int ret;
  1413. /* Force buffer page aligned */
  1414. if (len < mtd->writesize) {
  1415. memcpy(this->page_buf, buf, len);
  1416. memset(this->page_buf + len, 0xff, mtd->writesize - len);
  1417. pbuf = this->page_buf;
  1418. len = mtd->writesize;
  1419. }
  1420. /* Enter OTP access mode */
  1421. this->command(mtd, ONENAND_CMD_OTP_ACCESS, 0, 0);
  1422. this->wait(mtd, FL_OTPING);
  1423. ret = mtd->write(mtd, from, len, retlen, pbuf);
  1424. /* Exit OTP access mode */
  1425. this->command(mtd, ONENAND_CMD_RESET, 0, 0);
  1426. this->wait(mtd, FL_RESETING);
  1427. return ret;
  1428. }
  1429. /**
  1430. * do_otp_lock - [DEFAULT] Lock OTP block area
  1431. * @param mtd MTD device structure
  1432. * @param from The offset to lock
  1433. * @param len number of bytes to lock
  1434. * @param retlen pointer to variable to store the number of lock bytes
  1435. * @param buf the databuffer to put/get data
  1436. *
  1437. * Lock OTP block area.
  1438. */
  1439. static int do_otp_lock(struct mtd_info *mtd, loff_t from, size_t len,
  1440. size_t *retlen, u_char *buf)
  1441. {
  1442. struct onenand_chip *this = mtd->priv;
  1443. int ret;
  1444. /* Enter OTP access mode */
  1445. this->command(mtd, ONENAND_CMD_OTP_ACCESS, 0, 0);
  1446. this->wait(mtd, FL_OTPING);
  1447. ret = onenand_do_write_oob(mtd, from, len, retlen, buf, MTD_OOB_PLACE);
  1448. /* Exit OTP access mode */
  1449. this->command(mtd, ONENAND_CMD_RESET, 0, 0);
  1450. this->wait(mtd, FL_RESETING);
  1451. return ret;
  1452. }
  1453. /**
  1454. * onenand_otp_walk - [DEFAULT] Handle OTP operation
  1455. * @param mtd MTD device structure
  1456. * @param from The offset to read/write
  1457. * @param len number of bytes to read/write
  1458. * @param retlen pointer to variable to store the number of read bytes
  1459. * @param buf the databuffer to put/get data
  1460. * @param action do given action
  1461. * @param mode specify user and factory
  1462. *
  1463. * Handle OTP operation.
  1464. */
  1465. static int onenand_otp_walk(struct mtd_info *mtd, loff_t from, size_t len,
  1466. size_t *retlen, u_char *buf,
  1467. otp_op_t action, int mode)
  1468. {
  1469. struct onenand_chip *this = mtd->priv;
  1470. int otp_pages;
  1471. int density;
  1472. int ret = 0;
  1473. *retlen = 0;
  1474. density = this->device_id >> ONENAND_DEVICE_DENSITY_SHIFT;
  1475. if (density < ONENAND_DEVICE_DENSITY_512Mb)
  1476. otp_pages = 20;
  1477. else
  1478. otp_pages = 10;
  1479. if (mode == MTD_OTP_FACTORY) {
  1480. from += mtd->writesize * otp_pages;
  1481. otp_pages = 64 - otp_pages;
  1482. }
  1483. /* Check User/Factory boundary */
  1484. if (((mtd->writesize * otp_pages) - (from + len)) < 0)
  1485. return 0;
  1486. while (len > 0 && otp_pages > 0) {
  1487. if (!action) { /* OTP Info functions */
  1488. struct otp_info *otpinfo;
  1489. len -= sizeof(struct otp_info);
  1490. if (len <= 0)
  1491. return -ENOSPC;
  1492. otpinfo = (struct otp_info *) buf;
  1493. otpinfo->start = from;
  1494. otpinfo->length = mtd->writesize;
  1495. otpinfo->locked = 0;
  1496. from += mtd->writesize;
  1497. buf += sizeof(struct otp_info);
  1498. *retlen += sizeof(struct otp_info);
  1499. } else {
  1500. size_t tmp_retlen;
  1501. int size = len;
  1502. ret = action(mtd, from, len, &tmp_retlen, buf);
  1503. buf += size;
  1504. len -= size;
  1505. *retlen += size;
  1506. if (ret < 0)
  1507. return ret;
  1508. }
  1509. otp_pages--;
  1510. }
  1511. return 0;
  1512. }
  1513. /**
  1514. * onenand_get_fact_prot_info - [MTD Interface] Read factory OTP info
  1515. * @param mtd MTD device structure
  1516. * @param buf the databuffer to put/get data
  1517. * @param len number of bytes to read
  1518. *
  1519. * Read factory OTP info.
  1520. */
  1521. static int onenand_get_fact_prot_info(struct mtd_info *mtd,
  1522. struct otp_info *buf, size_t len)
  1523. {
  1524. size_t retlen;
  1525. int ret;
  1526. ret = onenand_otp_walk(mtd, 0, len, &retlen, (u_char *) buf, NULL, MTD_OTP_FACTORY);
  1527. return ret ? : retlen;
  1528. }
  1529. /**
  1530. * onenand_read_fact_prot_reg - [MTD Interface] Read factory OTP area
  1531. * @param mtd MTD device structure
  1532. * @param from The offset to read
  1533. * @param len number of bytes to read
  1534. * @param retlen pointer to variable to store the number of read bytes
  1535. * @param buf the databuffer to put/get data
  1536. *
  1537. * Read factory OTP area.
  1538. */
  1539. static int onenand_read_fact_prot_reg(struct mtd_info *mtd, loff_t from,
  1540. size_t len, size_t *retlen, u_char *buf)
  1541. {
  1542. return onenand_otp_walk(mtd, from, len, retlen, buf, do_otp_read, MTD_OTP_FACTORY);
  1543. }
  1544. /**
  1545. * onenand_get_user_prot_info - [MTD Interface] Read user OTP info
  1546. * @param mtd MTD device structure
  1547. * @param buf the databuffer to put/get data
  1548. * @param len number of bytes to read
  1549. *
  1550. * Read user OTP info.
  1551. */
  1552. static int onenand_get_user_prot_info(struct mtd_info *mtd,
  1553. struct otp_info *buf, size_t len)
  1554. {
  1555. size_t retlen;
  1556. int ret;
  1557. ret = onenand_otp_walk(mtd, 0, len, &retlen, (u_char *) buf, NULL, MTD_OTP_USER);
  1558. return ret ? : retlen;
  1559. }
  1560. /**
  1561. * onenand_read_user_prot_reg - [MTD Interface] Read user OTP area
  1562. * @param mtd MTD device structure
  1563. * @param from The offset to read
  1564. * @param len number of bytes to read
  1565. * @param retlen pointer to variable to store the number of read bytes
  1566. * @param buf the databuffer to put/get data
  1567. *
  1568. * Read user OTP area.
  1569. */
  1570. static int onenand_read_user_prot_reg(struct mtd_info *mtd, loff_t from,
  1571. size_t len, size_t *retlen, u_char *buf)
  1572. {
  1573. return onenand_otp_walk(mtd, from, len, retlen, buf, do_otp_read, MTD_OTP_USER);
  1574. }
  1575. /**
  1576. * onenand_write_user_prot_reg - [MTD Interface] Write user OTP area
  1577. * @param mtd MTD device structure
  1578. * @param from The offset to write
  1579. * @param len number of bytes to write
  1580. * @param retlen pointer to variable to store the number of write bytes
  1581. * @param buf the databuffer to put/get data
  1582. *
  1583. * Write user OTP area.
  1584. */
  1585. static int onenand_write_user_prot_reg(struct mtd_info *mtd, loff_t from,
  1586. size_t len, size_t *retlen, u_char *buf)
  1587. {
  1588. return onenand_otp_walk(mtd, from, len, retlen, buf, do_otp_write, MTD_OTP_USER);
  1589. }
  1590. /**
  1591. * onenand_lock_user_prot_reg - [MTD Interface] Lock user OTP area
  1592. * @param mtd MTD device structure
  1593. * @param from The offset to lock
  1594. * @param len number of bytes to unlock
  1595. *
  1596. * Write lock mark on spare area in page 0 in OTP block
  1597. */
  1598. static int onenand_lock_user_prot_reg(struct mtd_info *mtd, loff_t from,
  1599. size_t len)
  1600. {
  1601. unsigned char oob_buf[64];
  1602. size_t retlen;
  1603. int ret;
  1604. memset(oob_buf, 0xff, mtd->oobsize);
  1605. /*
  1606. * Note: OTP lock operation
  1607. * OTP block : 0xXXFC
  1608. * 1st block : 0xXXF3 (If chip support)
  1609. * Both : 0xXXF0 (If chip support)
  1610. */
  1611. oob_buf[ONENAND_OTP_LOCK_OFFSET] = 0xFC;
  1612. /*
  1613. * Write lock mark to 8th word of sector0 of page0 of the spare0.
  1614. * We write 16 bytes spare area instead of 2 bytes.
  1615. */
  1616. from = 0;
  1617. len = 16;
  1618. ret = onenand_otp_walk(mtd, from, len, &retlen, oob_buf, do_otp_lock, MTD_OTP_USER);
  1619. return ret ? : retlen;
  1620. }
  1621. #endif /* CONFIG_MTD_ONENAND_OTP */
  1622. /**
  1623. * onenand_check_features - Check and set OneNAND features
  1624. * @param mtd MTD data structure
  1625. *
  1626. * Check and set OneNAND features
  1627. * - lock scheme
  1628. */
  1629. static void onenand_check_features(struct mtd_info *mtd)
  1630. {
  1631. struct onenand_chip *this = mtd->priv;
  1632. unsigned int density, process;
  1633. /* Lock scheme depends on density and process */
  1634. density = this->device_id >> ONENAND_DEVICE_DENSITY_SHIFT;
  1635. process = this->version_id >> ONENAND_VERSION_PROCESS_SHIFT;
  1636. /* Lock scheme */
  1637. if (density >= ONENAND_DEVICE_DENSITY_1Gb) {
  1638. /* A-Die has all block unlock */
  1639. if (process) {
  1640. printk(KERN_DEBUG "Chip support all block unlock\n");
  1641. this->options |= ONENAND_HAS_UNLOCK_ALL;
  1642. }
  1643. } else {
  1644. /* Some OneNAND has continues lock scheme */
  1645. if (!process) {
  1646. printk(KERN_DEBUG "Lock scheme is Continues Lock\n");
  1647. this->options |= ONENAND_HAS_CONT_LOCK;
  1648. }
  1649. }
  1650. }
  1651. /**
  1652. * onenand_print_device_info - Print device ID
  1653. * @param device device ID
  1654. *
  1655. * Print device ID
  1656. */
  1657. static void onenand_print_device_info(int device, int version)
  1658. {
  1659. int vcc, demuxed, ddp, density;
  1660. vcc = device & ONENAND_DEVICE_VCC_MASK;
  1661. demuxed = device & ONENAND_DEVICE_IS_DEMUX;
  1662. ddp = device & ONENAND_DEVICE_IS_DDP;
  1663. density = device >> ONENAND_DEVICE_DENSITY_SHIFT;
  1664. printk(KERN_INFO "%sOneNAND%s %dMB %sV 16-bit (0x%02x)\n",
  1665. demuxed ? "" : "Muxed ",
  1666. ddp ? "(DDP)" : "",
  1667. (16 << density),
  1668. vcc ? "2.65/3.3" : "1.8",
  1669. device);
  1670. printk(KERN_DEBUG "OneNAND version = 0x%04x\n", version);
  1671. }
  1672. static const struct onenand_manufacturers onenand_manuf_ids[] = {
  1673. {ONENAND_MFR_SAMSUNG, "Samsung"},
  1674. };
  1675. /**
  1676. * onenand_check_maf - Check manufacturer ID
  1677. * @param manuf manufacturer ID
  1678. *
  1679. * Check manufacturer ID
  1680. */
  1681. static int onenand_check_maf(int manuf)
  1682. {
  1683. int size = ARRAY_SIZE(onenand_manuf_ids);
  1684. char *name;
  1685. int i;
  1686. for (i = 0; i < size; i++)
  1687. if (manuf == onenand_manuf_ids[i].id)
  1688. break;
  1689. if (i < size)
  1690. name = onenand_manuf_ids[i].name;
  1691. else
  1692. name = "Unknown";
  1693. printk(KERN_DEBUG "OneNAND Manufacturer: %s (0x%0x)\n", name, manuf);
  1694. return (i == size);
  1695. }
  1696. /**
  1697. * onenand_probe - [OneNAND Interface] Probe the OneNAND device
  1698. * @param mtd MTD device structure
  1699. *
  1700. * OneNAND detection method:
  1701. * Compare the the values from command with ones from register
  1702. */
  1703. static int onenand_probe(struct mtd_info *mtd)
  1704. {
  1705. struct onenand_chip *this = mtd->priv;
  1706. int bram_maf_id, bram_dev_id, maf_id, dev_id, ver_id;
  1707. int density;
  1708. int syscfg;
  1709. /* Save system configuration 1 */
  1710. syscfg = this->read_word(this->base + ONENAND_REG_SYS_CFG1);
  1711. /* Clear Sync. Burst Read mode to read BootRAM */
  1712. this->write_word((syscfg & ~ONENAND_SYS_CFG1_SYNC_READ), this->base + ONENAND_REG_SYS_CFG1);
  1713. /* Send the command for reading device ID from BootRAM */
  1714. this->write_word(ONENAND_CMD_READID, this->base + ONENAND_BOOTRAM);
  1715. /* Read manufacturer and device IDs from BootRAM */
  1716. bram_maf_id = this->read_word(this->base + ONENAND_BOOTRAM + 0x0);
  1717. bram_dev_id = this->read_word(this->base + ONENAND_BOOTRAM + 0x2);
  1718. /* Reset OneNAND to read default register values */
  1719. this->write_word(ONENAND_CMD_RESET, this->base + ONENAND_BOOTRAM);
  1720. /* Wait reset */
  1721. this->wait(mtd, FL_RESETING);
  1722. /* Restore system configuration 1 */
  1723. this->write_word(syscfg, this->base + ONENAND_REG_SYS_CFG1);
  1724. /* Check manufacturer ID */
  1725. if (onenand_check_maf(bram_maf_id))
  1726. return -ENXIO;
  1727. /* Read manufacturer and device IDs from Register */
  1728. maf_id = this->read_word(this->base + ONENAND_REG_MANUFACTURER_ID);
  1729. dev_id = this->read_word(this->base + ONENAND_REG_DEVICE_ID);
  1730. ver_id = this->read_word(this->base + ONENAND_REG_VERSION_ID);
  1731. /* Check OneNAND device */
  1732. if (maf_id != bram_maf_id || dev_id != bram_dev_id)
  1733. return -ENXIO;
  1734. /* Flash device information */
  1735. onenand_print_device_info(dev_id, ver_id);
  1736. this->device_id = dev_id;
  1737. this->version_id = ver_id;
  1738. density = dev_id >> ONENAND_DEVICE_DENSITY_SHIFT;
  1739. this->chipsize = (16 << density) << 20;
  1740. /* Set density mask. it is used for DDP */
  1741. if (ONENAND_IS_DDP(this))
  1742. this->density_mask = (1 << (density + 6));
  1743. else
  1744. this->density_mask = 0;
  1745. /* OneNAND page size & block size */
  1746. /* The data buffer size is equal to page size */
  1747. mtd->writesize = this->read_word(this->base + ONENAND_REG_DATA_BUFFER_SIZE);
  1748. mtd->oobsize = mtd->writesize >> 5;
  1749. /* Pages per a block are always 64 in OneNAND */
  1750. mtd->erasesize = mtd->writesize << 6;
  1751. this->erase_shift = ffs(mtd->erasesize) - 1;
  1752. this->page_shift = ffs(mtd->writesize) - 1;
  1753. this->page_mask = (1 << (this->erase_shift - this->page_shift)) - 1;
  1754. /* REVIST: Multichip handling */
  1755. mtd->size = this->chipsize;
  1756. /* Check OneNAND features */
  1757. onenand_check_features(mtd);
  1758. return 0;
  1759. }
  1760. /**
  1761. * onenand_suspend - [MTD Interface] Suspend the OneNAND flash
  1762. * @param mtd MTD device structure
  1763. */
  1764. static int onenand_suspend(struct mtd_info *mtd)
  1765. {
  1766. return onenand_get_device(mtd, FL_PM_SUSPENDED);
  1767. }
  1768. /**
  1769. * onenand_resume - [MTD Interface] Resume the OneNAND flash
  1770. * @param mtd MTD device structure
  1771. */
  1772. static void onenand_resume(struct mtd_info *mtd)
  1773. {
  1774. struct onenand_chip *this = mtd->priv;
  1775. if (this->state == FL_PM_SUSPENDED)
  1776. onenand_release_device(mtd);
  1777. else
  1778. printk(KERN_ERR "resume() called for the chip which is not"
  1779. "in suspended state\n");
  1780. }
  1781. /**
  1782. * onenand_scan - [OneNAND Interface] Scan for the OneNAND device
  1783. * @param mtd MTD device structure
  1784. * @param maxchips Number of chips to scan for
  1785. *
  1786. * This fills out all the not initialized function pointers
  1787. * with the defaults.
  1788. * The flash ID is read and the mtd/chip structures are
  1789. * filled with the appropriate values.
  1790. */
  1791. int onenand_scan(struct mtd_info *mtd, int maxchips)
  1792. {
  1793. int i;
  1794. struct onenand_chip *this = mtd->priv;
  1795. if (!this->read_word)
  1796. this->read_word = onenand_readw;
  1797. if (!this->write_word)
  1798. this->write_word = onenand_writew;
  1799. if (!this->command)
  1800. this->command = onenand_command;
  1801. if (!this->wait)
  1802. onenand_setup_wait(mtd);
  1803. if (!this->read_bufferram)
  1804. this->read_bufferram = onenand_read_bufferram;
  1805. if (!this->write_bufferram)
  1806. this->write_bufferram = onenand_write_bufferram;
  1807. if (!this->block_markbad)
  1808. this->block_markbad = onenand_default_block_markbad;
  1809. if (!this->scan_bbt)
  1810. this->scan_bbt = onenand_default_bbt;
  1811. if (onenand_probe(mtd))
  1812. return -ENXIO;
  1813. /* Set Sync. Burst Read after probing */
  1814. if (this->mmcontrol) {
  1815. printk(KERN_INFO "OneNAND Sync. Burst Read support\n");
  1816. this->read_bufferram = onenand_sync_read_bufferram;
  1817. }
  1818. /* Allocate buffers, if necessary */
  1819. if (!this->page_buf) {
  1820. size_t len;
  1821. len = mtd->writesize + mtd->oobsize;
  1822. this->page_buf = kmalloc(len, GFP_KERNEL);
  1823. if (!this->page_buf) {
  1824. printk(KERN_ERR "onenand_scan(): Can't allocate page_buf\n");
  1825. return -ENOMEM;
  1826. }
  1827. this->options |= ONENAND_PAGEBUF_ALLOC;
  1828. }
  1829. this->state = FL_READY;
  1830. init_waitqueue_head(&this->wq);
  1831. spin_lock_init(&this->chip_lock);
  1832. /*
  1833. * Allow subpage writes up to oobsize.
  1834. */
  1835. switch (mtd->oobsize) {
  1836. case 64:
  1837. this->ecclayout = &onenand_oob_64;
  1838. mtd->subpage_sft = 2;
  1839. break;
  1840. case 32:
  1841. this->ecclayout = &onenand_oob_32;
  1842. mtd->subpage_sft = 1;
  1843. break;
  1844. default:
  1845. printk(KERN_WARNING "No OOB scheme defined for oobsize %d\n",
  1846. mtd->oobsize);
  1847. mtd->subpage_sft = 0;
  1848. /* To prevent kernel oops */
  1849. this->ecclayout = &onenand_oob_32;
  1850. break;
  1851. }
  1852. this->subpagesize = mtd->writesize >> mtd->subpage_sft;
  1853. /*
  1854. * The number of bytes available for a client to place data into
  1855. * the out of band area
  1856. */
  1857. this->ecclayout->oobavail = 0;
  1858. for (i = 0; this->ecclayout->oobfree[i].length; i++)
  1859. this->ecclayout->oobavail +=
  1860. this->ecclayout->oobfree[i].length;
  1861. mtd->ecclayout = this->ecclayout;
  1862. /* Fill in remaining MTD driver data */
  1863. mtd->type = MTD_NANDFLASH;
  1864. mtd->flags = MTD_CAP_NANDFLASH;
  1865. mtd->ecctype = MTD_ECC_SW;
  1866. mtd->erase = onenand_erase;
  1867. mtd->point = NULL;
  1868. mtd->unpoint = NULL;
  1869. mtd->read = onenand_read;
  1870. mtd->write = onenand_write;
  1871. mtd->read_oob = onenand_read_oob;
  1872. mtd->write_oob = onenand_write_oob;
  1873. #ifdef CONFIG_MTD_ONENAND_OTP
  1874. mtd->get_fact_prot_info = onenand_get_fact_prot_info;
  1875. mtd->read_fact_prot_reg = onenand_read_fact_prot_reg;
  1876. mtd->get_user_prot_info = onenand_get_user_prot_info;
  1877. mtd->read_user_prot_reg = onenand_read_user_prot_reg;
  1878. mtd->write_user_prot_reg = onenand_write_user_prot_reg;
  1879. mtd->lock_user_prot_reg = onenand_lock_user_prot_reg;
  1880. #endif
  1881. mtd->sync = onenand_sync;
  1882. mtd->lock = onenand_lock;
  1883. mtd->unlock = onenand_unlock;
  1884. mtd->suspend = onenand_suspend;
  1885. mtd->resume = onenand_resume;
  1886. mtd->block_isbad = onenand_block_isbad;
  1887. mtd->block_markbad = onenand_block_markbad;
  1888. mtd->owner = THIS_MODULE;
  1889. /* Unlock whole block */
  1890. onenand_unlock_all(mtd);
  1891. return this->scan_bbt(mtd);
  1892. }
  1893. /**
  1894. * onenand_release - [OneNAND Interface] Free resources held by the OneNAND device
  1895. * @param mtd MTD device structure
  1896. */
  1897. void onenand_release(struct mtd_info *mtd)
  1898. {
  1899. struct onenand_chip *this = mtd->priv;
  1900. #ifdef CONFIG_MTD_PARTITIONS
  1901. /* Deregister partitions */
  1902. del_mtd_partitions (mtd);
  1903. #endif
  1904. /* Deregister the device */
  1905. del_mtd_device (mtd);
  1906. /* Free bad block table memory, if allocated */
  1907. if (this->bbm) {
  1908. struct bbm_info *bbm = this->bbm;
  1909. kfree(bbm->bbt);
  1910. kfree(this->bbm);
  1911. }
  1912. /* Buffer allocated by onenand_scan */
  1913. if (this->options & ONENAND_PAGEBUF_ALLOC)
  1914. kfree(this->page_buf);
  1915. }
  1916. EXPORT_SYMBOL_GPL(onenand_scan);
  1917. EXPORT_SYMBOL_GPL(onenand_release);
  1918. MODULE_LICENSE("GPL");
  1919. MODULE_AUTHOR("Kyungmin Park <kyungmin.park@samsung.com>");
  1920. MODULE_DESCRIPTION("Generic OneNAND flash driver code");