onenand_base.c 55 KB

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