onenand_base.c 54 KB

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