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. /* It maybe occur at initial bad block */
  275. DEBUG(MTD_DEBUG_LEVEL0, "onenand_wait: controller error = 0x%04x\n", ctrl);
  276. /* Clear other interrupt bits for preventing ECC error */
  277. interrupt &= ONENAND_INT_MASTER;
  278. }
  279. if (ctrl & ONENAND_CTRL_LOCK) {
  280. DEBUG(MTD_DEBUG_LEVEL0, "onenand_wait: it's locked error = 0x%04x\n", ctrl);
  281. return -EACCES;
  282. }
  283. if (interrupt & ONENAND_INT_READ) {
  284. ecc = this->read_word(this->base + ONENAND_REG_ECC_STATUS);
  285. if (ecc & ONENAND_ECC_2BIT_ALL) {
  286. DEBUG(MTD_DEBUG_LEVEL0, "onenand_wait: ECC error = 0x%04x\n", ecc);
  287. return -EBADMSG;
  288. }
  289. }
  290. return 0;
  291. }
  292. /*
  293. * onenand_interrupt - [DEFAULT] onenand interrupt handler
  294. * @param irq onenand interrupt number
  295. * @param dev_id interrupt data
  296. *
  297. * complete the work
  298. */
  299. static irqreturn_t onenand_interrupt(int irq, void *data)
  300. {
  301. struct onenand_chip *this = (struct onenand_chip *) data;
  302. /* To handle shared interrupt */
  303. if (!this->complete.done)
  304. complete(&this->complete);
  305. return IRQ_HANDLED;
  306. }
  307. /*
  308. * onenand_interrupt_wait - [DEFAULT] wait until the command is done
  309. * @param mtd MTD device structure
  310. * @param state state to select the max. timeout value
  311. *
  312. * Wait for command done.
  313. */
  314. static int onenand_interrupt_wait(struct mtd_info *mtd, int state)
  315. {
  316. struct onenand_chip *this = mtd->priv;
  317. /* To prevent soft lockup */
  318. touch_softlockup_watchdog();
  319. wait_for_completion(&this->complete);
  320. return onenand_wait(mtd, state);
  321. }
  322. /*
  323. * onenand_try_interrupt_wait - [DEFAULT] try interrupt wait
  324. * @param mtd MTD device structure
  325. * @param state state to select the max. timeout value
  326. *
  327. * Try interrupt based wait (It is used one-time)
  328. */
  329. static int onenand_try_interrupt_wait(struct mtd_info *mtd, int state)
  330. {
  331. struct onenand_chip *this = mtd->priv;
  332. unsigned long remain, timeout;
  333. /* We use interrupt wait first */
  334. this->wait = onenand_interrupt_wait;
  335. /* To prevent soft lockup */
  336. touch_softlockup_watchdog();
  337. timeout = msecs_to_jiffies(100);
  338. remain = wait_for_completion_timeout(&this->complete, timeout);
  339. if (!remain) {
  340. printk(KERN_INFO "OneNAND: There's no interrupt. "
  341. "We use the normal wait\n");
  342. /* Release the irq */
  343. free_irq(this->irq, this);
  344. this->wait = onenand_wait;
  345. }
  346. return onenand_wait(mtd, state);
  347. }
  348. /*
  349. * onenand_setup_wait - [OneNAND Interface] setup onenand wait method
  350. * @param mtd MTD device structure
  351. *
  352. * There's two method to wait onenand work
  353. * 1. polling - read interrupt status register
  354. * 2. interrupt - use the kernel interrupt method
  355. */
  356. static void onenand_setup_wait(struct mtd_info *mtd)
  357. {
  358. struct onenand_chip *this = mtd->priv;
  359. int syscfg;
  360. init_completion(&this->complete);
  361. if (this->irq <= 0) {
  362. this->wait = onenand_wait;
  363. return;
  364. }
  365. if (request_irq(this->irq, &onenand_interrupt,
  366. IRQF_SHARED, "onenand", this)) {
  367. /* If we can't get irq, use the normal wait */
  368. this->wait = onenand_wait;
  369. return;
  370. }
  371. /* Enable interrupt */
  372. syscfg = this->read_word(this->base + ONENAND_REG_SYS_CFG1);
  373. syscfg |= ONENAND_SYS_CFG1_IOBE;
  374. this->write_word(syscfg, this->base + ONENAND_REG_SYS_CFG1);
  375. this->wait = onenand_try_interrupt_wait;
  376. }
  377. /**
  378. * onenand_bufferram_offset - [DEFAULT] BufferRAM offset
  379. * @param mtd MTD data structure
  380. * @param area BufferRAM area
  381. * @return offset given area
  382. *
  383. * Return BufferRAM offset given area
  384. */
  385. static inline int onenand_bufferram_offset(struct mtd_info *mtd, int area)
  386. {
  387. struct onenand_chip *this = mtd->priv;
  388. if (ONENAND_CURRENT_BUFFERRAM(this)) {
  389. if (area == ONENAND_DATARAM)
  390. return mtd->writesize;
  391. if (area == ONENAND_SPARERAM)
  392. return mtd->oobsize;
  393. }
  394. return 0;
  395. }
  396. /**
  397. * onenand_read_bufferram - [OneNAND Interface] Read the bufferram area
  398. * @param mtd MTD data structure
  399. * @param area BufferRAM area
  400. * @param buffer the databuffer to put/get data
  401. * @param offset offset to read from or write to
  402. * @param count number of bytes to read/write
  403. *
  404. * Read the BufferRAM area
  405. */
  406. static int onenand_read_bufferram(struct mtd_info *mtd, int area,
  407. unsigned char *buffer, int offset, size_t count)
  408. {
  409. struct onenand_chip *this = mtd->priv;
  410. void __iomem *bufferram;
  411. bufferram = this->base + area;
  412. bufferram += onenand_bufferram_offset(mtd, area);
  413. if (ONENAND_CHECK_BYTE_ACCESS(count)) {
  414. unsigned short word;
  415. /* Align with word(16-bit) size */
  416. count--;
  417. /* Read word and save byte */
  418. word = this->read_word(bufferram + offset + count);
  419. buffer[count] = (word & 0xff);
  420. }
  421. memcpy(buffer, bufferram + offset, count);
  422. return 0;
  423. }
  424. /**
  425. * onenand_sync_read_bufferram - [OneNAND Interface] Read the bufferram area with Sync. Burst mode
  426. * @param mtd MTD data structure
  427. * @param area BufferRAM area
  428. * @param buffer the databuffer to put/get data
  429. * @param offset offset to read from or write to
  430. * @param count number of bytes to read/write
  431. *
  432. * Read the BufferRAM area with Sync. Burst Mode
  433. */
  434. static int onenand_sync_read_bufferram(struct mtd_info *mtd, int area,
  435. unsigned char *buffer, int offset, size_t count)
  436. {
  437. struct onenand_chip *this = mtd->priv;
  438. void __iomem *bufferram;
  439. bufferram = this->base + area;
  440. bufferram += onenand_bufferram_offset(mtd, area);
  441. this->mmcontrol(mtd, ONENAND_SYS_CFG1_SYNC_READ);
  442. if (ONENAND_CHECK_BYTE_ACCESS(count)) {
  443. unsigned short word;
  444. /* Align with word(16-bit) size */
  445. count--;
  446. /* Read word and save byte */
  447. word = this->read_word(bufferram + offset + count);
  448. buffer[count] = (word & 0xff);
  449. }
  450. memcpy(buffer, bufferram + offset, count);
  451. this->mmcontrol(mtd, 0);
  452. return 0;
  453. }
  454. /**
  455. * onenand_write_bufferram - [OneNAND Interface] Write the bufferram area
  456. * @param mtd MTD data structure
  457. * @param area BufferRAM area
  458. * @param buffer the databuffer to put/get data
  459. * @param offset offset to read from or write to
  460. * @param count number of bytes to read/write
  461. *
  462. * Write the BufferRAM area
  463. */
  464. static int onenand_write_bufferram(struct mtd_info *mtd, int area,
  465. const unsigned char *buffer, int offset, size_t count)
  466. {
  467. struct onenand_chip *this = mtd->priv;
  468. void __iomem *bufferram;
  469. bufferram = this->base + area;
  470. bufferram += onenand_bufferram_offset(mtd, area);
  471. if (ONENAND_CHECK_BYTE_ACCESS(count)) {
  472. unsigned short word;
  473. int byte_offset;
  474. /* Align with word(16-bit) size */
  475. count--;
  476. /* Calculate byte access offset */
  477. byte_offset = offset + count;
  478. /* Read word and save byte */
  479. word = this->read_word(bufferram + byte_offset);
  480. word = (word & ~0xff) | buffer[count];
  481. this->write_word(word, bufferram + byte_offset);
  482. }
  483. memcpy(bufferram + offset, buffer, count);
  484. return 0;
  485. }
  486. /**
  487. * onenand_check_bufferram - [GENERIC] Check BufferRAM information
  488. * @param mtd MTD data structure
  489. * @param addr address to check
  490. * @return 1 if there are valid data, otherwise 0
  491. *
  492. * Check bufferram if there is data we required
  493. */
  494. static int onenand_check_bufferram(struct mtd_info *mtd, loff_t addr)
  495. {
  496. struct onenand_chip *this = mtd->priv;
  497. int block, page;
  498. int i;
  499. block = (int) (addr >> this->erase_shift);
  500. page = (int) (addr >> this->page_shift);
  501. page &= this->page_mask;
  502. i = ONENAND_CURRENT_BUFFERRAM(this);
  503. /* Is there valid data? */
  504. if (this->bufferram[i].block == block &&
  505. this->bufferram[i].page == page &&
  506. this->bufferram[i].valid)
  507. return 1;
  508. return 0;
  509. }
  510. /**
  511. * onenand_update_bufferram - [GENERIC] Update BufferRAM information
  512. * @param mtd MTD data structure
  513. * @param addr address to update
  514. * @param valid valid flag
  515. *
  516. * Update BufferRAM information
  517. */
  518. static int onenand_update_bufferram(struct mtd_info *mtd, loff_t addr,
  519. int valid)
  520. {
  521. struct onenand_chip *this = mtd->priv;
  522. int block, page;
  523. int i;
  524. block = (int) (addr >> this->erase_shift);
  525. page = (int) (addr >> this->page_shift);
  526. page &= this->page_mask;
  527. /* Invalidate BufferRAM */
  528. for (i = 0; i < MAX_BUFFERRAM; i++) {
  529. if (this->bufferram[i].block == block &&
  530. this->bufferram[i].page == page)
  531. this->bufferram[i].valid = 0;
  532. }
  533. /* Update BufferRAM */
  534. i = ONENAND_CURRENT_BUFFERRAM(this);
  535. this->bufferram[i].block = block;
  536. this->bufferram[i].page = page;
  537. this->bufferram[i].valid = valid;
  538. return 0;
  539. }
  540. /**
  541. * onenand_get_device - [GENERIC] Get chip for selected access
  542. * @param mtd MTD device structure
  543. * @param new_state the state which is requested
  544. *
  545. * Get the device and lock it for exclusive access
  546. */
  547. static int onenand_get_device(struct mtd_info *mtd, int new_state)
  548. {
  549. struct onenand_chip *this = mtd->priv;
  550. DECLARE_WAITQUEUE(wait, current);
  551. /*
  552. * Grab the lock and see if the device is available
  553. */
  554. while (1) {
  555. spin_lock(&this->chip_lock);
  556. if (this->state == FL_READY) {
  557. this->state = new_state;
  558. spin_unlock(&this->chip_lock);
  559. break;
  560. }
  561. if (new_state == FL_PM_SUSPENDED) {
  562. spin_unlock(&this->chip_lock);
  563. return (this->state == FL_PM_SUSPENDED) ? 0 : -EAGAIN;
  564. }
  565. set_current_state(TASK_UNINTERRUPTIBLE);
  566. add_wait_queue(&this->wq, &wait);
  567. spin_unlock(&this->chip_lock);
  568. schedule();
  569. remove_wait_queue(&this->wq, &wait);
  570. }
  571. return 0;
  572. }
  573. /**
  574. * onenand_release_device - [GENERIC] release chip
  575. * @param mtd MTD device structure
  576. *
  577. * Deselect, release chip lock and wake up anyone waiting on the device
  578. */
  579. static void onenand_release_device(struct mtd_info *mtd)
  580. {
  581. struct onenand_chip *this = mtd->priv;
  582. /* Release the chip */
  583. spin_lock(&this->chip_lock);
  584. this->state = FL_READY;
  585. wake_up(&this->wq);
  586. spin_unlock(&this->chip_lock);
  587. }
  588. /**
  589. * onenand_read - [MTD Interface] Read data from flash
  590. * @param mtd MTD device structure
  591. * @param from offset to read from
  592. * @param len number of bytes to read
  593. * @param retlen pointer to variable to store the number of read bytes
  594. * @param buf the databuffer to put data
  595. *
  596. * Read with ecc
  597. */
  598. static int onenand_read(struct mtd_info *mtd, loff_t from, size_t len,
  599. size_t *retlen, u_char *buf)
  600. {
  601. struct onenand_chip *this = mtd->priv;
  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. while (read < len) {
  616. thislen = min_t(int, mtd->writesize, len - read);
  617. column = from & (mtd->writesize - 1);
  618. if (column + thislen > mtd->writesize)
  619. thislen = mtd->writesize - column;
  620. if (!onenand_check_bufferram(mtd, from)) {
  621. this->command(mtd, ONENAND_CMD_READ, from, mtd->writesize);
  622. ret = this->wait(mtd, FL_READING);
  623. /* First copy data and check return value for ECC handling */
  624. onenand_update_bufferram(mtd, from, 1);
  625. }
  626. this->read_bufferram(mtd, ONENAND_DATARAM, buf, column, thislen);
  627. read += thislen;
  628. if (read == len)
  629. break;
  630. if (ret) {
  631. DEBUG(MTD_DEBUG_LEVEL0, "onenand_read: read failed = %d\n", ret);
  632. goto out;
  633. }
  634. from += thislen;
  635. buf += thislen;
  636. }
  637. out:
  638. /* Deselect and wake up anyone waiting on the device */
  639. onenand_release_device(mtd);
  640. /*
  641. * Return success, if no ECC failures, else -EBADMSG
  642. * fs driver will take care of that, because
  643. * retlen == desired len and result == -EBADMSG
  644. */
  645. *retlen = read;
  646. return ret;
  647. }
  648. /**
  649. * onenand_do_read_oob - [MTD Interface] OneNAND read out-of-band
  650. * @param mtd MTD device structure
  651. * @param from offset to read from
  652. * @param len number of bytes to read
  653. * @param retlen pointer to variable to store the number of read bytes
  654. * @param buf the databuffer to put data
  655. *
  656. * OneNAND read out-of-band data from the spare area
  657. */
  658. int onenand_do_read_oob(struct mtd_info *mtd, loff_t from, size_t len,
  659. size_t *retlen, u_char *buf)
  660. {
  661. struct onenand_chip *this = mtd->priv;
  662. int read = 0, thislen, column;
  663. int ret = 0;
  664. DEBUG(MTD_DEBUG_LEVEL3, "onenand_read_oob: from = 0x%08x, len = %i\n", (unsigned int) from, (int) len);
  665. /* Initialize return length value */
  666. *retlen = 0;
  667. /* Do not allow reads past end of device */
  668. if (unlikely((from + len) > mtd->size)) {
  669. DEBUG(MTD_DEBUG_LEVEL0, "onenand_read_oob: Attempt read beyond end of device\n");
  670. return -EINVAL;
  671. }
  672. /* Grab the lock and see if the device is available */
  673. onenand_get_device(mtd, FL_READING);
  674. column = from & (mtd->oobsize - 1);
  675. while (read < len) {
  676. thislen = mtd->oobsize - column;
  677. thislen = min_t(int, thislen, len);
  678. this->command(mtd, ONENAND_CMD_READOOB, from, mtd->oobsize);
  679. onenand_update_bufferram(mtd, from, 0);
  680. ret = this->wait(mtd, FL_READING);
  681. /* First copy data and check return value for ECC handling */
  682. this->read_bufferram(mtd, ONENAND_SPARERAM, buf, column, thislen);
  683. read += thislen;
  684. if (read == len)
  685. break;
  686. if (ret) {
  687. DEBUG(MTD_DEBUG_LEVEL0, "onenand_read_oob: read failed = %d\n", ret);
  688. goto out;
  689. }
  690. buf += thislen;
  691. /* Read more? */
  692. if (read < len) {
  693. /* Page size */
  694. from += mtd->writesize;
  695. column = 0;
  696. }
  697. }
  698. out:
  699. /* Deselect and wake up anyone waiting on the device */
  700. onenand_release_device(mtd);
  701. *retlen = read;
  702. return ret;
  703. }
  704. /**
  705. * onenand_read_oob - [MTD Interface] NAND write data and/or out-of-band
  706. * @mtd: MTD device structure
  707. * @from: offset to read from
  708. * @ops: oob operation description structure
  709. */
  710. static int onenand_read_oob(struct mtd_info *mtd, loff_t from,
  711. struct mtd_oob_ops *ops)
  712. {
  713. BUG_ON(ops->mode != MTD_OOB_PLACE);
  714. return onenand_do_read_oob(mtd, from + ops->ooboffs, ops->len,
  715. &ops->retlen, ops->oobbuf);
  716. }
  717. #ifdef CONFIG_MTD_ONENAND_VERIFY_WRITE
  718. /**
  719. * onenand_verify_oob - [GENERIC] verify the oob contents after a write
  720. * @param mtd MTD device structure
  721. * @param buf the databuffer to verify
  722. * @param to offset to read from
  723. * @param len number of bytes to read and compare
  724. *
  725. */
  726. static int onenand_verify_oob(struct mtd_info *mtd, const u_char *buf, loff_t to, int len)
  727. {
  728. struct onenand_chip *this = mtd->priv;
  729. char *readp = this->page_buf;
  730. int column = to & (mtd->oobsize - 1);
  731. int status, i;
  732. this->command(mtd, ONENAND_CMD_READOOB, to, mtd->oobsize);
  733. onenand_update_bufferram(mtd, to, 0);
  734. status = this->wait(mtd, FL_READING);
  735. if (status)
  736. return status;
  737. this->read_bufferram(mtd, ONENAND_SPARERAM, readp, column, len);
  738. for(i = 0; i < len; i++)
  739. if (buf[i] != 0xFF && buf[i] != readp[i])
  740. return -EBADMSG;
  741. return 0;
  742. }
  743. /**
  744. * onenand_verify_page - [GENERIC] verify the chip contents after a write
  745. * @param mtd MTD device structure
  746. * @param buf the databuffer to verify
  747. *
  748. * Check DataRAM area directly
  749. */
  750. static int onenand_verify_page(struct mtd_info *mtd, u_char *buf, loff_t addr)
  751. {
  752. struct onenand_chip *this = mtd->priv;
  753. void __iomem *dataram0, *dataram1;
  754. int ret = 0;
  755. this->command(mtd, ONENAND_CMD_READ, addr, mtd->writesize);
  756. ret = this->wait(mtd, FL_READING);
  757. if (ret)
  758. return ret;
  759. onenand_update_bufferram(mtd, addr, 1);
  760. /* Check, if the two dataram areas are same */
  761. dataram0 = this->base + ONENAND_DATARAM;
  762. dataram1 = dataram0 + mtd->writesize;
  763. if (memcmp(dataram0, dataram1, mtd->writesize))
  764. return -EBADMSG;
  765. return 0;
  766. }
  767. #else
  768. #define onenand_verify_page(...) (0)
  769. #define onenand_verify_oob(...) (0)
  770. #endif
  771. #define NOTALIGNED(x) ((x & (mtd->writesize - 1)) != 0)
  772. /**
  773. * onenand_write - [MTD Interface] write buffer to FLASH
  774. * @param mtd MTD device structure
  775. * @param to offset to write to
  776. * @param len number of bytes to write
  777. * @param retlen pointer to variable to store the number of written bytes
  778. * @param buf the data to write
  779. *
  780. * Write with ECC
  781. */
  782. static int onenand_write(struct mtd_info *mtd, loff_t to, size_t len,
  783. size_t *retlen, const u_char *buf)
  784. {
  785. struct onenand_chip *this = mtd->priv;
  786. int written = 0;
  787. int ret = 0;
  788. DEBUG(MTD_DEBUG_LEVEL3, "onenand_write: to = 0x%08x, len = %i\n", (unsigned int) to, (int) len);
  789. /* Initialize retlen, in case of early exit */
  790. *retlen = 0;
  791. /* Do not allow writes past end of device */
  792. if (unlikely((to + len) > mtd->size)) {
  793. DEBUG(MTD_DEBUG_LEVEL0, "onenand_write: Attempt write to past end of device\n");
  794. return -EINVAL;
  795. }
  796. /* Reject writes, which are not page aligned */
  797. if (unlikely(NOTALIGNED(to)) || unlikely(NOTALIGNED(len))) {
  798. DEBUG(MTD_DEBUG_LEVEL0, "onenand_write: Attempt to write not page aligned data\n");
  799. return -EINVAL;
  800. }
  801. /* Grab the lock and see if the device is available */
  802. onenand_get_device(mtd, FL_WRITING);
  803. /* Loop until all data write */
  804. while (written < len) {
  805. int thislen = min_t(int, mtd->writesize, len - written);
  806. this->command(mtd, ONENAND_CMD_BUFFERRAM, to, mtd->writesize);
  807. this->write_bufferram(mtd, ONENAND_DATARAM, buf, 0, thislen);
  808. this->write_bufferram(mtd, ONENAND_SPARERAM, ffchars, 0, mtd->oobsize);
  809. this->command(mtd, ONENAND_CMD_PROG, to, mtd->writesize);
  810. onenand_update_bufferram(mtd, to, 1);
  811. ret = this->wait(mtd, FL_WRITING);
  812. if (ret) {
  813. DEBUG(MTD_DEBUG_LEVEL0, "onenand_write: write filaed %d\n", ret);
  814. goto out;
  815. }
  816. written += thislen;
  817. /* Only check verify write turn on */
  818. ret = onenand_verify_page(mtd, (u_char *) buf, to);
  819. if (ret) {
  820. DEBUG(MTD_DEBUG_LEVEL0, "onenand_write: verify failed %d\n", ret);
  821. goto out;
  822. }
  823. if (written == len)
  824. break;
  825. to += thislen;
  826. buf += thislen;
  827. }
  828. out:
  829. /* Deselect and wake up anyone waiting on the device */
  830. onenand_release_device(mtd);
  831. *retlen = written;
  832. return ret;
  833. }
  834. /**
  835. * onenand_do_write_oob - [Internal] OneNAND write out-of-band
  836. * @param mtd MTD device structure
  837. * @param to offset to write to
  838. * @param len number of bytes to write
  839. * @param retlen pointer to variable to store the number of written bytes
  840. * @param buf the data to write
  841. *
  842. * OneNAND write out-of-band
  843. */
  844. static int onenand_do_write_oob(struct mtd_info *mtd, loff_t to, size_t len,
  845. size_t *retlen, const u_char *buf)
  846. {
  847. struct onenand_chip *this = mtd->priv;
  848. int column, ret = 0;
  849. int written = 0;
  850. DEBUG(MTD_DEBUG_LEVEL3, "onenand_write_oob: to = 0x%08x, len = %i\n", (unsigned int) to, (int) len);
  851. /* Initialize retlen, in case of early exit */
  852. *retlen = 0;
  853. /* Do not allow writes past end of device */
  854. if (unlikely((to + len) > mtd->size)) {
  855. DEBUG(MTD_DEBUG_LEVEL0, "onenand_write_oob: Attempt write to past end of device\n");
  856. return -EINVAL;
  857. }
  858. /* Grab the lock and see if the device is available */
  859. onenand_get_device(mtd, FL_WRITING);
  860. /* Loop until all data write */
  861. while (written < len) {
  862. int thislen = min_t(int, mtd->oobsize, len - written);
  863. column = to & (mtd->oobsize - 1);
  864. this->command(mtd, ONENAND_CMD_BUFFERRAM, to, mtd->oobsize);
  865. /* We send data to spare ram with oobsize
  866. * to prevent byte access */
  867. memset(this->page_buf, 0xff, mtd->oobsize);
  868. memcpy(this->page_buf + column, buf, thislen);
  869. this->write_bufferram(mtd, ONENAND_SPARERAM, this->page_buf, 0, mtd->oobsize);
  870. this->command(mtd, ONENAND_CMD_PROGOOB, to, mtd->oobsize);
  871. onenand_update_bufferram(mtd, to, 0);
  872. ret = this->wait(mtd, FL_WRITING);
  873. if (ret) {
  874. DEBUG(MTD_DEBUG_LEVEL0, "onenand_write_oob: write filaed %d\n", ret);
  875. goto out;
  876. }
  877. ret = onenand_verify_oob(mtd, buf, to, thislen);
  878. if (ret) {
  879. DEBUG(MTD_DEBUG_LEVEL0, "onenand_write_oob: verify failed %d\n", ret);
  880. goto out;
  881. }
  882. written += thislen;
  883. if (written == len)
  884. break;
  885. to += thislen;
  886. buf += thislen;
  887. }
  888. out:
  889. /* Deselect and wake up anyone waiting on the device */
  890. onenand_release_device(mtd);
  891. *retlen = written;
  892. return ret;
  893. }
  894. /**
  895. * onenand_write_oob - [MTD Interface] NAND write data and/or out-of-band
  896. * @mtd: MTD device structure
  897. * @from: offset to read from
  898. * @ops: oob operation description structure
  899. */
  900. static int onenand_write_oob(struct mtd_info *mtd, loff_t to,
  901. struct mtd_oob_ops *ops)
  902. {
  903. BUG_ON(ops->mode != MTD_OOB_PLACE);
  904. return onenand_do_write_oob(mtd, to + ops->ooboffs, ops->len,
  905. &ops->retlen, ops->oobbuf);
  906. }
  907. /**
  908. * onenand_block_checkbad - [GENERIC] Check if a block is marked bad
  909. * @param mtd MTD device structure
  910. * @param ofs offset from device start
  911. * @param getchip 0, if the chip is already selected
  912. * @param allowbbt 1, if its allowed to access the bbt area
  913. *
  914. * Check, if the block is bad. Either by reading the bad block table or
  915. * calling of the scan function.
  916. */
  917. static int onenand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int getchip, int allowbbt)
  918. {
  919. struct onenand_chip *this = mtd->priv;
  920. struct bbm_info *bbm = this->bbm;
  921. /* Return info from the table */
  922. return bbm->isbad_bbt(mtd, ofs, allowbbt);
  923. }
  924. /**
  925. * onenand_erase - [MTD Interface] erase block(s)
  926. * @param mtd MTD device structure
  927. * @param instr erase instruction
  928. *
  929. * Erase one ore more blocks
  930. */
  931. static int onenand_erase(struct mtd_info *mtd, struct erase_info *instr)
  932. {
  933. struct onenand_chip *this = mtd->priv;
  934. unsigned int block_size;
  935. loff_t addr;
  936. int len;
  937. int ret = 0;
  938. DEBUG(MTD_DEBUG_LEVEL3, "onenand_erase: start = 0x%08x, len = %i\n", (unsigned int) instr->addr, (unsigned int) instr->len);
  939. block_size = (1 << this->erase_shift);
  940. /* Start address must align on block boundary */
  941. if (unlikely(instr->addr & (block_size - 1))) {
  942. DEBUG(MTD_DEBUG_LEVEL0, "onenand_erase: Unaligned address\n");
  943. return -EINVAL;
  944. }
  945. /* Length must align on block boundary */
  946. if (unlikely(instr->len & (block_size - 1))) {
  947. DEBUG(MTD_DEBUG_LEVEL0, "onenand_erase: Length not block aligned\n");
  948. return -EINVAL;
  949. }
  950. /* Do not allow erase past end of device */
  951. if (unlikely((instr->len + instr->addr) > mtd->size)) {
  952. DEBUG(MTD_DEBUG_LEVEL0, "onenand_erase: Erase past end of device\n");
  953. return -EINVAL;
  954. }
  955. instr->fail_addr = 0xffffffff;
  956. /* Grab the lock and see if the device is available */
  957. onenand_get_device(mtd, FL_ERASING);
  958. /* Loop throught the pages */
  959. len = instr->len;
  960. addr = instr->addr;
  961. instr->state = MTD_ERASING;
  962. while (len) {
  963. /* Check if we have a bad block, we do not erase bad blocks */
  964. if (onenand_block_checkbad(mtd, addr, 0, 0)) {
  965. printk (KERN_WARNING "onenand_erase: attempt to erase a bad block at addr 0x%08x\n", (unsigned int) addr);
  966. instr->state = MTD_ERASE_FAILED;
  967. goto erase_exit;
  968. }
  969. this->command(mtd, ONENAND_CMD_ERASE, addr, block_size);
  970. ret = this->wait(mtd, FL_ERASING);
  971. /* Check, if it is write protected */
  972. if (ret) {
  973. if (ret == -EPERM)
  974. DEBUG(MTD_DEBUG_LEVEL0, "onenand_erase: Device is write protected!!!\n");
  975. else
  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");