onenand_base.c 54 KB

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