onenand_base.c 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829
  1. /*
  2. * linux/drivers/mtd/onenand/onenand_base.c
  3. *
  4. * Copyright (C) 2005 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/jiffies.h>
  16. #include <linux/mtd/mtd.h>
  17. #include <linux/mtd/onenand.h>
  18. #include <linux/mtd/partitions.h>
  19. #include <asm/io.h>
  20. /**
  21. * onenand_oob_64 - oob info for large (2KB) page
  22. */
  23. static struct nand_oobinfo onenand_oob_64 = {
  24. .useecc = MTD_NANDECC_AUTOPLACE,
  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_oobinfo onenand_oob_32 = {
  41. .useecc = MTD_NANDECC_AUTOPLACE,
  42. .eccbytes = 10,
  43. .eccpos = {
  44. 8, 9, 10, 11, 12,
  45. 24, 25, 26, 27, 28,
  46. },
  47. .oobfree = { {2, 3}, {14, 2}, {18, 3}, {30, 2} }
  48. };
  49. static const unsigned char ffchars[] = {
  50. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  51. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 16 */
  52. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  53. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 32 */
  54. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  55. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 48 */
  56. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  57. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 64 */
  58. };
  59. /**
  60. * onenand_readw - [OneNAND Interface] Read OneNAND register
  61. * @param addr address to read
  62. *
  63. * Read OneNAND register
  64. */
  65. static unsigned short onenand_readw(void __iomem *addr)
  66. {
  67. return readw(addr);
  68. }
  69. /**
  70. * onenand_writew - [OneNAND Interface] Write OneNAND register with value
  71. * @param value value to write
  72. * @param addr address to write
  73. *
  74. * Write OneNAND register with value
  75. */
  76. static void onenand_writew(unsigned short value, void __iomem *addr)
  77. {
  78. writew(value, addr);
  79. }
  80. /**
  81. * onenand_block_address - [DEFAULT] Get block address
  82. * @param this onenand chip data structure
  83. * @param block the block
  84. * @return translated block address if DDP, otherwise same
  85. *
  86. * Setup Start Address 1 Register (F100h)
  87. */
  88. static int onenand_block_address(struct onenand_chip *this, int block)
  89. {
  90. if (this->device_id & ONENAND_DEVICE_IS_DDP) {
  91. /* Device Flash Core select, NAND Flash Block Address */
  92. int dfs = 0;
  93. if (block & this->density_mask)
  94. dfs = 1;
  95. return (dfs << ONENAND_DDP_SHIFT) |
  96. (block & (this->density_mask - 1));
  97. }
  98. return block;
  99. }
  100. /**
  101. * onenand_bufferram_address - [DEFAULT] Get bufferram address
  102. * @param this onenand chip data structure
  103. * @param block the block
  104. * @return set DBS value if DDP, otherwise 0
  105. *
  106. * Setup Start Address 2 Register (F101h) for DDP
  107. */
  108. static int onenand_bufferram_address(struct onenand_chip *this, int block)
  109. {
  110. if (this->device_id & ONENAND_DEVICE_IS_DDP) {
  111. /* Device BufferRAM Select */
  112. int dbs = 0;
  113. if (block & this->density_mask)
  114. dbs = 1;
  115. return (dbs << ONENAND_DDP_SHIFT);
  116. }
  117. return 0;
  118. }
  119. /**
  120. * onenand_page_address - [DEFAULT] Get page address
  121. * @param page the page address
  122. * @param sector the sector address
  123. * @return combined page and sector address
  124. *
  125. * Setup Start Address 8 Register (F107h)
  126. */
  127. static int onenand_page_address(int page, int sector)
  128. {
  129. /* Flash Page Address, Flash Sector Address */
  130. int fpa, fsa;
  131. fpa = page & ONENAND_FPA_MASK;
  132. fsa = sector & ONENAND_FSA_MASK;
  133. return ((fpa << ONENAND_FPA_SHIFT) | fsa);
  134. }
  135. /**
  136. * onenand_buffer_address - [DEFAULT] Get buffer address
  137. * @param dataram1 DataRAM index
  138. * @param sectors the sector address
  139. * @param count the number of sectors
  140. * @return the start buffer value
  141. *
  142. * Setup Start Buffer Register (F200h)
  143. */
  144. static int onenand_buffer_address(int dataram1, int sectors, int count)
  145. {
  146. int bsa, bsc;
  147. /* BufferRAM Sector Address */
  148. bsa = sectors & ONENAND_BSA_MASK;
  149. if (dataram1)
  150. bsa |= ONENAND_BSA_DATARAM1; /* DataRAM1 */
  151. else
  152. bsa |= ONENAND_BSA_DATARAM0; /* DataRAM0 */
  153. /* BufferRAM Sector Count */
  154. bsc = count & ONENAND_BSC_MASK;
  155. return ((bsa << ONENAND_BSA_SHIFT) | bsc);
  156. }
  157. /**
  158. * onenand_command - [DEFAULT] Send command to OneNAND device
  159. * @param mtd MTD device structure
  160. * @param cmd the command to be sent
  161. * @param addr offset to read from or write to
  162. * @param len number of bytes to read or write
  163. *
  164. * Send command to OneNAND device. This function is used for middle/large page
  165. * devices (1KB/2KB Bytes per page)
  166. */
  167. static int onenand_command(struct mtd_info *mtd, int cmd, loff_t addr, size_t len)
  168. {
  169. struct onenand_chip *this = mtd->priv;
  170. int value, readcmd = 0, block_cmd = 0;
  171. int block, page;
  172. /* Now we use page size operation */
  173. int sectors = 4, count = 4;
  174. /* Address translation */
  175. switch (cmd) {
  176. case ONENAND_CMD_UNLOCK:
  177. case ONENAND_CMD_LOCK:
  178. case ONENAND_CMD_LOCK_TIGHT:
  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_bufferram_offset - [DEFAULT] BufferRAM offset
  294. * @param mtd MTD data structure
  295. * @param area BufferRAM area
  296. * @return offset given area
  297. *
  298. * Return BufferRAM offset given area
  299. */
  300. static inline int onenand_bufferram_offset(struct mtd_info *mtd, int area)
  301. {
  302. struct onenand_chip *this = mtd->priv;
  303. if (ONENAND_CURRENT_BUFFERRAM(this)) {
  304. if (area == ONENAND_DATARAM)
  305. return mtd->writesize;
  306. if (area == ONENAND_SPARERAM)
  307. return mtd->oobsize;
  308. }
  309. return 0;
  310. }
  311. /**
  312. * onenand_read_bufferram - [OneNAND Interface] Read the bufferram area
  313. * @param mtd MTD data structure
  314. * @param area BufferRAM area
  315. * @param buffer the databuffer to put/get data
  316. * @param offset offset to read from or write to
  317. * @param count number of bytes to read/write
  318. *
  319. * Read the BufferRAM area
  320. */
  321. static int onenand_read_bufferram(struct mtd_info *mtd, int area,
  322. unsigned char *buffer, int offset, size_t count)
  323. {
  324. struct onenand_chip *this = mtd->priv;
  325. void __iomem *bufferram;
  326. bufferram = this->base + area;
  327. bufferram += onenand_bufferram_offset(mtd, area);
  328. if (ONENAND_CHECK_BYTE_ACCESS(count)) {
  329. unsigned short word;
  330. /* Align with word(16-bit) size */
  331. count--;
  332. /* Read word and save byte */
  333. word = this->read_word(bufferram + offset + count);
  334. buffer[count] = (word & 0xff);
  335. }
  336. memcpy(buffer, bufferram + offset, count);
  337. return 0;
  338. }
  339. /**
  340. * onenand_sync_read_bufferram - [OneNAND Interface] Read the bufferram area with Sync. Burst mode
  341. * @param mtd MTD data structure
  342. * @param area BufferRAM area
  343. * @param buffer the databuffer to put/get data
  344. * @param offset offset to read from or write to
  345. * @param count number of bytes to read/write
  346. *
  347. * Read the BufferRAM area with Sync. Burst Mode
  348. */
  349. static int onenand_sync_read_bufferram(struct mtd_info *mtd, int area,
  350. unsigned char *buffer, int offset, size_t count)
  351. {
  352. struct onenand_chip *this = mtd->priv;
  353. void __iomem *bufferram;
  354. bufferram = this->base + area;
  355. bufferram += onenand_bufferram_offset(mtd, area);
  356. this->mmcontrol(mtd, ONENAND_SYS_CFG1_SYNC_READ);
  357. if (ONENAND_CHECK_BYTE_ACCESS(count)) {
  358. unsigned short word;
  359. /* Align with word(16-bit) size */
  360. count--;
  361. /* Read word and save byte */
  362. word = this->read_word(bufferram + offset + count);
  363. buffer[count] = (word & 0xff);
  364. }
  365. memcpy(buffer, bufferram + offset, count);
  366. this->mmcontrol(mtd, 0);
  367. return 0;
  368. }
  369. /**
  370. * onenand_write_bufferram - [OneNAND Interface] Write the bufferram area
  371. * @param mtd MTD data structure
  372. * @param area BufferRAM area
  373. * @param buffer the databuffer to put/get data
  374. * @param offset offset to read from or write to
  375. * @param count number of bytes to read/write
  376. *
  377. * Write the BufferRAM area
  378. */
  379. static int onenand_write_bufferram(struct mtd_info *mtd, int area,
  380. const unsigned char *buffer, int offset, size_t count)
  381. {
  382. struct onenand_chip *this = mtd->priv;
  383. void __iomem *bufferram;
  384. bufferram = this->base + area;
  385. bufferram += onenand_bufferram_offset(mtd, area);
  386. if (ONENAND_CHECK_BYTE_ACCESS(count)) {
  387. unsigned short word;
  388. int byte_offset;
  389. /* Align with word(16-bit) size */
  390. count--;
  391. /* Calculate byte access offset */
  392. byte_offset = offset + count;
  393. /* Read word and save byte */
  394. word = this->read_word(bufferram + byte_offset);
  395. word = (word & ~0xff) | buffer[count];
  396. this->write_word(word, bufferram + byte_offset);
  397. }
  398. memcpy(bufferram + offset, buffer, count);
  399. return 0;
  400. }
  401. /**
  402. * onenand_check_bufferram - [GENERIC] Check BufferRAM information
  403. * @param mtd MTD data structure
  404. * @param addr address to check
  405. * @return 1 if there are valid data, otherwise 0
  406. *
  407. * Check bufferram if there is data we required
  408. */
  409. static int onenand_check_bufferram(struct mtd_info *mtd, loff_t addr)
  410. {
  411. struct onenand_chip *this = mtd->priv;
  412. int block, page;
  413. int i;
  414. block = (int) (addr >> this->erase_shift);
  415. page = (int) (addr >> this->page_shift);
  416. page &= this->page_mask;
  417. i = ONENAND_CURRENT_BUFFERRAM(this);
  418. /* Is there valid data? */
  419. if (this->bufferram[i].block == block &&
  420. this->bufferram[i].page == page &&
  421. this->bufferram[i].valid)
  422. return 1;
  423. return 0;
  424. }
  425. /**
  426. * onenand_update_bufferram - [GENERIC] Update BufferRAM information
  427. * @param mtd MTD data structure
  428. * @param addr address to update
  429. * @param valid valid flag
  430. *
  431. * Update BufferRAM information
  432. */
  433. static int onenand_update_bufferram(struct mtd_info *mtd, loff_t addr,
  434. int valid)
  435. {
  436. struct onenand_chip *this = mtd->priv;
  437. int block, page;
  438. int i;
  439. block = (int) (addr >> this->erase_shift);
  440. page = (int) (addr >> this->page_shift);
  441. page &= this->page_mask;
  442. /* Invalidate BufferRAM */
  443. for (i = 0; i < MAX_BUFFERRAM; i++) {
  444. if (this->bufferram[i].block == block &&
  445. this->bufferram[i].page == page)
  446. this->bufferram[i].valid = 0;
  447. }
  448. /* Update BufferRAM */
  449. i = ONENAND_CURRENT_BUFFERRAM(this);
  450. this->bufferram[i].block = block;
  451. this->bufferram[i].page = page;
  452. this->bufferram[i].valid = valid;
  453. return 0;
  454. }
  455. /**
  456. * onenand_get_device - [GENERIC] Get chip for selected access
  457. * @param mtd MTD device structure
  458. * @param new_state the state which is requested
  459. *
  460. * Get the device and lock it for exclusive access
  461. */
  462. static int onenand_get_device(struct mtd_info *mtd, int new_state)
  463. {
  464. struct onenand_chip *this = mtd->priv;
  465. DECLARE_WAITQUEUE(wait, current);
  466. /*
  467. * Grab the lock and see if the device is available
  468. */
  469. while (1) {
  470. spin_lock(&this->chip_lock);
  471. if (this->state == FL_READY) {
  472. this->state = new_state;
  473. spin_unlock(&this->chip_lock);
  474. break;
  475. }
  476. if (new_state == FL_PM_SUSPENDED) {
  477. spin_unlock(&this->chip_lock);
  478. return (this->state == FL_PM_SUSPENDED) ? 0 : -EAGAIN;
  479. }
  480. set_current_state(TASK_UNINTERRUPTIBLE);
  481. add_wait_queue(&this->wq, &wait);
  482. spin_unlock(&this->chip_lock);
  483. schedule();
  484. remove_wait_queue(&this->wq, &wait);
  485. }
  486. return 0;
  487. }
  488. /**
  489. * onenand_release_device - [GENERIC] release chip
  490. * @param mtd MTD device structure
  491. *
  492. * Deselect, release chip lock and wake up anyone waiting on the device
  493. */
  494. static void onenand_release_device(struct mtd_info *mtd)
  495. {
  496. struct onenand_chip *this = mtd->priv;
  497. /* Release the chip */
  498. spin_lock(&this->chip_lock);
  499. this->state = FL_READY;
  500. wake_up(&this->wq);
  501. spin_unlock(&this->chip_lock);
  502. }
  503. /**
  504. * onenand_read - [MTD Interface] Read data from flash
  505. * @param mtd MTD device structure
  506. * @param from offset to read from
  507. * @param len number of bytes to read
  508. * @param retlen pointer to variable to store the number of read bytes
  509. * @param buf the databuffer to put data
  510. *
  511. * Read with ecc
  512. */
  513. static int onenand_read(struct mtd_info *mtd, loff_t from, size_t len,
  514. size_t *retlen, u_char *buf)
  515. {
  516. struct onenand_chip *this = mtd->priv;
  517. int read = 0, column;
  518. int thislen;
  519. int ret = 0;
  520. DEBUG(MTD_DEBUG_LEVEL3, "onenand_read: from = 0x%08x, len = %i\n", (unsigned int) from, (int) len);
  521. /* Do not allow reads past end of device */
  522. if ((from + len) > mtd->size) {
  523. DEBUG(MTD_DEBUG_LEVEL0, "onenand_read: Attempt read beyond end of device\n");
  524. *retlen = 0;
  525. return -EINVAL;
  526. }
  527. /* Grab the lock and see if the device is available */
  528. onenand_get_device(mtd, FL_READING);
  529. /* TODO handling oob */
  530. while (read < len) {
  531. thislen = min_t(int, mtd->writesize, len - read);
  532. column = from & (mtd->writesize - 1);
  533. if (column + thislen > mtd->writesize)
  534. thislen = mtd->writesize - column;
  535. if (!onenand_check_bufferram(mtd, from)) {
  536. this->command(mtd, ONENAND_CMD_READ, from, mtd->writesize);
  537. ret = this->wait(mtd, FL_READING);
  538. /* First copy data and check return value for ECC handling */
  539. onenand_update_bufferram(mtd, from, 1);
  540. }
  541. this->read_bufferram(mtd, ONENAND_DATARAM, buf, column, thislen);
  542. read += thislen;
  543. if (read == len)
  544. break;
  545. if (ret) {
  546. DEBUG(MTD_DEBUG_LEVEL0, "onenand_read: read failed = %d\n", ret);
  547. goto out;
  548. }
  549. from += thislen;
  550. buf += thislen;
  551. }
  552. out:
  553. /* Deselect and wake up anyone waiting on the device */
  554. onenand_release_device(mtd);
  555. /*
  556. * Return success, if no ECC failures, else -EBADMSG
  557. * fs driver will take care of that, because
  558. * retlen == desired len and result == -EBADMSG
  559. */
  560. *retlen = read;
  561. return ret;
  562. }
  563. /**
  564. * onenand_read_oob - [MTD Interface] OneNAND read out-of-band
  565. * @param mtd MTD device structure
  566. * @param from offset to read from
  567. * @param len number of bytes to read
  568. * @param retlen pointer to variable to store the number of read bytes
  569. * @param buf the databuffer to put data
  570. *
  571. * OneNAND read out-of-band data from the spare area
  572. */
  573. static int onenand_read_oob(struct mtd_info *mtd, loff_t from, size_t len,
  574. size_t *retlen, u_char *buf)
  575. {
  576. struct onenand_chip *this = mtd->priv;
  577. int read = 0, thislen, column;
  578. int ret = 0;
  579. DEBUG(MTD_DEBUG_LEVEL3, "onenand_read_oob: from = 0x%08x, len = %i\n", (unsigned int) from, (int) len);
  580. /* Initialize return length value */
  581. *retlen = 0;
  582. /* Do not allow reads past end of device */
  583. if (unlikely((from + len) > mtd->size)) {
  584. DEBUG(MTD_DEBUG_LEVEL0, "onenand_read_oob: Attempt read beyond end of device\n");
  585. return -EINVAL;
  586. }
  587. /* Grab the lock and see if the device is available */
  588. onenand_get_device(mtd, FL_READING);
  589. column = from & (mtd->oobsize - 1);
  590. while (read < len) {
  591. thislen = mtd->oobsize - column;
  592. thislen = min_t(int, thislen, len);
  593. this->command(mtd, ONENAND_CMD_READOOB, from, mtd->oobsize);
  594. onenand_update_bufferram(mtd, from, 0);
  595. ret = this->wait(mtd, FL_READING);
  596. /* First copy data and check return value for ECC handling */
  597. this->read_bufferram(mtd, ONENAND_SPARERAM, buf, column, thislen);
  598. read += thislen;
  599. if (read == len)
  600. break;
  601. if (ret) {
  602. DEBUG(MTD_DEBUG_LEVEL0, "onenand_read_oob: read failed = %d\n", ret);
  603. goto out;
  604. }
  605. buf += thislen;
  606. /* Read more? */
  607. if (read < len) {
  608. /* Page size */
  609. from += mtd->writesize;
  610. column = 0;
  611. }
  612. }
  613. out:
  614. /* Deselect and wake up anyone waiting on the device */
  615. onenand_release_device(mtd);
  616. *retlen = read;
  617. return ret;
  618. }
  619. #ifdef CONFIG_MTD_ONENAND_VERIFY_WRITE
  620. /**
  621. * onenand_verify_oob - [GENERIC] verify the oob contents after a write
  622. * @param mtd MTD device structure
  623. * @param buf the databuffer to verify
  624. * @param to offset to read from
  625. * @param len number of bytes to read and compare
  626. *
  627. */
  628. static int onenand_verify_oob(struct mtd_info *mtd, const u_char *buf, loff_t to, int len)
  629. {
  630. struct onenand_chip *this = mtd->priv;
  631. char *readp = this->page_buf;
  632. int column = to & (mtd->oobsize - 1);
  633. int status, i;
  634. this->command(mtd, ONENAND_CMD_READOOB, to, mtd->oobsize);
  635. onenand_update_bufferram(mtd, to, 0);
  636. status = this->wait(mtd, FL_READING);
  637. if (status)
  638. return status;
  639. this->read_bufferram(mtd, ONENAND_SPARERAM, readp, column, len);
  640. for(i = 0; i < len; i++)
  641. if (buf[i] != 0xFF && buf[i] != readp[i])
  642. return -EBADMSG;
  643. return 0;
  644. }
  645. /**
  646. * onenand_verify_page - [GENERIC] verify the chip contents after a write
  647. * @param mtd MTD device structure
  648. * @param buf the databuffer to verify
  649. *
  650. * Check DataRAM area directly
  651. */
  652. static int onenand_verify_page(struct mtd_info *mtd, u_char *buf, loff_t addr)
  653. {
  654. struct onenand_chip *this = mtd->priv;
  655. void __iomem *dataram0, *dataram1;
  656. int ret = 0;
  657. this->command(mtd, ONENAND_CMD_READ, addr, mtd->writesize);
  658. ret = this->wait(mtd, FL_READING);
  659. if (ret)
  660. return ret;
  661. onenand_update_bufferram(mtd, addr, 1);
  662. /* Check, if the two dataram areas are same */
  663. dataram0 = this->base + ONENAND_DATARAM;
  664. dataram1 = dataram0 + mtd->writesize;
  665. if (memcmp(dataram0, dataram1, mtd->writesize))
  666. return -EBADMSG;
  667. return 0;
  668. }
  669. #else
  670. #define onenand_verify_page(...) (0)
  671. #define onenand_verify_oob(...) (0)
  672. #endif
  673. #define NOTALIGNED(x) ((x & (mtd->writesize - 1)) != 0)
  674. /**
  675. * onenand_write - [MTD Interface] write buffer to FLASH
  676. * @param mtd MTD device structure
  677. * @param to offset to write to
  678. * @param len number of bytes to write
  679. * @param retlen pointer to variable to store the number of written bytes
  680. * @param buf the data to write
  681. *
  682. * Write with ECC
  683. */
  684. static int onenand_write(struct mtd_info *mtd, loff_t to, size_t len,
  685. size_t *retlen, const u_char *buf)
  686. {
  687. struct onenand_chip *this = mtd->priv;
  688. int written = 0;
  689. int ret = 0;
  690. DEBUG(MTD_DEBUG_LEVEL3, "onenand_write: to = 0x%08x, len = %i\n", (unsigned int) to, (int) len);
  691. /* Initialize retlen, in case of early exit */
  692. *retlen = 0;
  693. /* Do not allow writes past end of device */
  694. if (unlikely((to + len) > mtd->size)) {
  695. DEBUG(MTD_DEBUG_LEVEL0, "onenand_write: Attempt write to past end of device\n");
  696. return -EINVAL;
  697. }
  698. /* Reject writes, which are not page aligned */
  699. if (unlikely(NOTALIGNED(to)) || unlikely(NOTALIGNED(len))) {
  700. DEBUG(MTD_DEBUG_LEVEL0, "onenand_write: Attempt to write not page aligned data\n");
  701. return -EINVAL;
  702. }
  703. /* Grab the lock and see if the device is available */
  704. onenand_get_device(mtd, FL_WRITING);
  705. /* Loop until all data write */
  706. while (written < len) {
  707. int thislen = min_t(int, mtd->writesize, len - written);
  708. this->command(mtd, ONENAND_CMD_BUFFERRAM, to, mtd->writesize);
  709. this->write_bufferram(mtd, ONENAND_DATARAM, buf, 0, thislen);
  710. this->write_bufferram(mtd, ONENAND_SPARERAM, ffchars, 0, mtd->oobsize);
  711. this->command(mtd, ONENAND_CMD_PROG, to, mtd->writesize);
  712. onenand_update_bufferram(mtd, to, 1);
  713. ret = this->wait(mtd, FL_WRITING);
  714. if (ret) {
  715. DEBUG(MTD_DEBUG_LEVEL0, "onenand_write: write filaed %d\n", ret);
  716. goto out;
  717. }
  718. written += thislen;
  719. /* Only check verify write turn on */
  720. ret = onenand_verify_page(mtd, (u_char *) buf, to);
  721. if (ret) {
  722. DEBUG(MTD_DEBUG_LEVEL0, "onenand_write: verify failed %d\n", ret);
  723. goto out;
  724. }
  725. if (written == len)
  726. break;
  727. to += thislen;
  728. buf += thislen;
  729. }
  730. out:
  731. /* Deselect and wake up anyone waiting on the device */
  732. onenand_release_device(mtd);
  733. *retlen = written;
  734. return ret;
  735. }
  736. /**
  737. * onenand_write_oob - [MTD Interface] OneNAND write out-of-band
  738. * @param mtd MTD device structure
  739. * @param to offset to write to
  740. * @param len number of bytes to write
  741. * @param retlen pointer to variable to store the number of written bytes
  742. * @param buf the data to write
  743. *
  744. * OneNAND write out-of-band
  745. */
  746. static int onenand_write_oob(struct mtd_info *mtd, loff_t to, size_t len,
  747. size_t *retlen, const u_char *buf)
  748. {
  749. struct onenand_chip *this = mtd->priv;
  750. int column, ret = 0;
  751. int written = 0;
  752. DEBUG(MTD_DEBUG_LEVEL3, "onenand_write_oob: to = 0x%08x, len = %i\n", (unsigned int) to, (int) len);
  753. /* Initialize retlen, in case of early exit */
  754. *retlen = 0;
  755. /* Do not allow writes past end of device */
  756. if (unlikely((to + len) > mtd->size)) {
  757. DEBUG(MTD_DEBUG_LEVEL0, "onenand_write_oob: Attempt write to past end of device\n");
  758. return -EINVAL;
  759. }
  760. /* Grab the lock and see if the device is available */
  761. onenand_get_device(mtd, FL_WRITING);
  762. /* Loop until all data write */
  763. while (written < len) {
  764. int thislen = min_t(int, mtd->oobsize, len - written);
  765. column = to & (mtd->oobsize - 1);
  766. this->command(mtd, ONENAND_CMD_BUFFERRAM, to, mtd->oobsize);
  767. /* We send data to spare ram with oobsize
  768. * to prevent byte access */
  769. memset(this->page_buf, 0xff, mtd->oobsize);
  770. memcpy(this->page_buf + column, buf, thislen);
  771. this->write_bufferram(mtd, ONENAND_SPARERAM, this->page_buf, 0, mtd->oobsize);
  772. this->command(mtd, ONENAND_CMD_PROGOOB, to, mtd->oobsize);
  773. onenand_update_bufferram(mtd, to, 0);
  774. ret = this->wait(mtd, FL_WRITING);
  775. if (ret) {
  776. DEBUG(MTD_DEBUG_LEVEL0, "onenand_write_oob: write filaed %d\n", ret);
  777. goto out;
  778. }
  779. ret = onenand_verify_oob(mtd, buf, to, thislen);
  780. if (ret) {
  781. DEBUG(MTD_DEBUG_LEVEL0, "onenand_write_oob: verify failed %d\n", ret);
  782. goto out;
  783. }
  784. written += thislen;
  785. if (written == len)
  786. break;
  787. to += thislen;
  788. buf += thislen;
  789. }
  790. out:
  791. /* Deselect and wake up anyone waiting on the device */
  792. onenand_release_device(mtd);
  793. *retlen = written;
  794. return ret;
  795. }
  796. /**
  797. * onenand_block_checkbad - [GENERIC] Check if a block is marked bad
  798. * @param mtd MTD device structure
  799. * @param ofs offset from device start
  800. * @param getchip 0, if the chip is already selected
  801. * @param allowbbt 1, if its allowed to access the bbt area
  802. *
  803. * Check, if the block is bad. Either by reading the bad block table or
  804. * calling of the scan function.
  805. */
  806. static int onenand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int getchip, int allowbbt)
  807. {
  808. struct onenand_chip *this = mtd->priv;
  809. struct bbm_info *bbm = this->bbm;
  810. /* Return info from the table */
  811. return bbm->isbad_bbt(mtd, ofs, allowbbt);
  812. }
  813. /**
  814. * onenand_erase - [MTD Interface] erase block(s)
  815. * @param mtd MTD device structure
  816. * @param instr erase instruction
  817. *
  818. * Erase one ore more blocks
  819. */
  820. static int onenand_erase(struct mtd_info *mtd, struct erase_info *instr)
  821. {
  822. struct onenand_chip *this = mtd->priv;
  823. unsigned int block_size;
  824. loff_t addr;
  825. int len;
  826. int ret = 0;
  827. DEBUG(MTD_DEBUG_LEVEL3, "onenand_erase: start = 0x%08x, len = %i\n", (unsigned int) instr->addr, (unsigned int) instr->len);
  828. block_size = (1 << this->erase_shift);
  829. /* Start address must align on block boundary */
  830. if (unlikely(instr->addr & (block_size - 1))) {
  831. DEBUG(MTD_DEBUG_LEVEL0, "onenand_erase: Unaligned address\n");
  832. return -EINVAL;
  833. }
  834. /* Length must align on block boundary */
  835. if (unlikely(instr->len & (block_size - 1))) {
  836. DEBUG(MTD_DEBUG_LEVEL0, "onenand_erase: Length not block aligned\n");
  837. return -EINVAL;
  838. }
  839. /* Do not allow erase past end of device */
  840. if (unlikely((instr->len + instr->addr) > mtd->size)) {
  841. DEBUG(MTD_DEBUG_LEVEL0, "onenand_erase: Erase past end of device\n");
  842. return -EINVAL;
  843. }
  844. instr->fail_addr = 0xffffffff;
  845. /* Grab the lock and see if the device is available */
  846. onenand_get_device(mtd, FL_ERASING);
  847. /* Loop throught the pages */
  848. len = instr->len;
  849. addr = instr->addr;
  850. instr->state = MTD_ERASING;
  851. while (len) {
  852. /* Check if we have a bad block, we do not erase bad blocks */
  853. if (onenand_block_checkbad(mtd, addr, 0, 0)) {
  854. printk (KERN_WARNING "onenand_erase: attempt to erase a bad block at addr 0x%08x\n", (unsigned int) addr);
  855. instr->state = MTD_ERASE_FAILED;
  856. goto erase_exit;
  857. }
  858. this->command(mtd, ONENAND_CMD_ERASE, addr, block_size);
  859. ret = this->wait(mtd, FL_ERASING);
  860. /* Check, if it is write protected */
  861. if (ret) {
  862. if (ret == -EPERM)
  863. DEBUG(MTD_DEBUG_LEVEL0, "onenand_erase: Device is write protected!!!\n");
  864. else
  865. DEBUG(MTD_DEBUG_LEVEL0, "onenand_erase: Failed erase, block %d\n", (unsigned) (addr >> this->erase_shift));
  866. instr->state = MTD_ERASE_FAILED;
  867. instr->fail_addr = addr;
  868. goto erase_exit;
  869. }
  870. len -= block_size;
  871. addr += block_size;
  872. }
  873. instr->state = MTD_ERASE_DONE;
  874. erase_exit:
  875. ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
  876. /* Do call back function */
  877. if (!ret)
  878. mtd_erase_callback(instr);
  879. /* Deselect and wake up anyone waiting on the device */
  880. onenand_release_device(mtd);
  881. return ret;
  882. }
  883. /**
  884. * onenand_sync - [MTD Interface] sync
  885. * @param mtd MTD device structure
  886. *
  887. * Sync is actually a wait for chip ready function
  888. */
  889. static void onenand_sync(struct mtd_info *mtd)
  890. {
  891. DEBUG(MTD_DEBUG_LEVEL3, "onenand_sync: called\n");
  892. /* Grab the lock and see if the device is available */
  893. onenand_get_device(mtd, FL_SYNCING);
  894. /* Release it and go back */
  895. onenand_release_device(mtd);
  896. }
  897. /**
  898. * onenand_block_isbad - [MTD Interface] Check whether the block at the given offset is bad
  899. * @param mtd MTD device structure
  900. * @param ofs offset relative to mtd start
  901. *
  902. * Check whether the block is bad
  903. */
  904. static int onenand_block_isbad(struct mtd_info *mtd, loff_t ofs)
  905. {
  906. /* Check for invalid offset */
  907. if (ofs > mtd->size)
  908. return -EINVAL;
  909. return onenand_block_checkbad(mtd, ofs, 1, 0);
  910. }
  911. /**
  912. * onenand_default_block_markbad - [DEFAULT] mark a block bad
  913. * @param mtd MTD device structure
  914. * @param ofs offset from device start
  915. *
  916. * This is the default implementation, which can be overridden by
  917. * a hardware specific driver.
  918. */
  919. static int onenand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
  920. {
  921. struct onenand_chip *this = mtd->priv;
  922. struct bbm_info *bbm = this->bbm;
  923. u_char buf[2] = {0, 0};
  924. size_t retlen;
  925. int block;
  926. /* Get block number */
  927. block = ((int) ofs) >> bbm->bbt_erase_shift;
  928. if (bbm->bbt)
  929. bbm->bbt[block >> 2] |= 0x01 << ((block & 0x03) << 1);
  930. /* We write two bytes, so we dont have to mess with 16 bit access */
  931. ofs += mtd->oobsize + (bbm->badblockpos & ~0x01);
  932. return mtd->write_oob(mtd, ofs , 2, &retlen, buf);
  933. }
  934. /**
  935. * onenand_block_markbad - [MTD Interface] Mark the block at the given offset as bad
  936. * @param mtd MTD device structure
  937. * @param ofs offset relative to mtd start
  938. *
  939. * Mark the block as bad
  940. */
  941. static int onenand_block_markbad(struct mtd_info *mtd, loff_t ofs)
  942. {
  943. struct onenand_chip *this = mtd->priv;
  944. int ret;
  945. ret = onenand_block_isbad(mtd, ofs);
  946. if (ret) {
  947. /* If it was bad already, return success and do nothing */
  948. if (ret > 0)
  949. return 0;
  950. return ret;
  951. }
  952. return this->block_markbad(mtd, ofs);
  953. }
  954. /**
  955. * onenand_unlock - [MTD Interface] Unlock block(s)
  956. * @param mtd MTD device structure
  957. * @param ofs offset relative to mtd start
  958. * @param len number of bytes to unlock
  959. *
  960. * Unlock one or more blocks
  961. */
  962. static int onenand_unlock(struct mtd_info *mtd, loff_t ofs, size_t len)
  963. {
  964. struct onenand_chip *this = mtd->priv;
  965. int start, end, block, value, status;
  966. start = ofs >> this->erase_shift;
  967. end = len >> this->erase_shift;
  968. /* Continuous lock scheme */
  969. if (this->options & ONENAND_CONT_LOCK) {
  970. /* Set start block address */
  971. this->write_word(start, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
  972. /* Set end block address */
  973. this->write_word(end - 1, this->base + ONENAND_REG_END_BLOCK_ADDRESS);
  974. /* Write unlock command */
  975. this->command(mtd, ONENAND_CMD_UNLOCK, 0, 0);
  976. /* There's no return value */
  977. this->wait(mtd, FL_UNLOCKING);
  978. /* Sanity check */
  979. while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
  980. & ONENAND_CTRL_ONGO)
  981. continue;
  982. /* Check lock status */
  983. status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
  984. if (!(status & ONENAND_WP_US))
  985. printk(KERN_ERR "wp status = 0x%x\n", status);
  986. return 0;
  987. }
  988. /* Block lock scheme */
  989. for (block = start; block < end; block++) {
  990. /* Set block address */
  991. value = onenand_block_address(this, block);
  992. this->write_word(value, this->base + ONENAND_REG_START_ADDRESS1);
  993. /* Select DataRAM for DDP */
  994. value = onenand_bufferram_address(this, block);
  995. this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
  996. /* Set start block address */
  997. this->write_word(block, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
  998. /* Write unlock command */
  999. this->command(mtd, ONENAND_CMD_UNLOCK, 0, 0);
  1000. /* There's no return value */
  1001. this->wait(mtd, FL_UNLOCKING);
  1002. /* Sanity check */
  1003. while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
  1004. & ONENAND_CTRL_ONGO)
  1005. continue;
  1006. /* Check lock status */
  1007. status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
  1008. if (!(status & ONENAND_WP_US))
  1009. printk(KERN_ERR "block = %d, wp status = 0x%x\n", block, status);
  1010. }
  1011. return 0;
  1012. }
  1013. #ifdef CONFIG_MTD_ONENAND_OTP
  1014. /* Interal OTP operation */
  1015. typedef int (*otp_op_t)(struct mtd_info *mtd, loff_t form, size_t len,
  1016. size_t *retlen, u_char *buf);
  1017. /**
  1018. * do_otp_read - [DEFAULT] Read OTP block area
  1019. * @param mtd MTD device structure
  1020. * @param from The offset to read
  1021. * @param len number of bytes to read
  1022. * @param retlen pointer to variable to store the number of readbytes
  1023. * @param buf the databuffer to put/get data
  1024. *
  1025. * Read OTP block area.
  1026. */
  1027. static int do_otp_read(struct mtd_info *mtd, loff_t from, size_t len,
  1028. size_t *retlen, u_char *buf)
  1029. {
  1030. struct onenand_chip *this = mtd->priv;
  1031. int ret;
  1032. /* Enter OTP access mode */
  1033. this->command(mtd, ONENAND_CMD_OTP_ACCESS, 0, 0);
  1034. this->wait(mtd, FL_OTPING);
  1035. ret = mtd->read(mtd, from, len, retlen, buf);
  1036. /* Exit OTP access mode */
  1037. this->command(mtd, ONENAND_CMD_RESET, 0, 0);
  1038. this->wait(mtd, FL_RESETING);
  1039. return ret;
  1040. }
  1041. /**
  1042. * do_otp_write - [DEFAULT] Write OTP block area
  1043. * @param mtd MTD device structure
  1044. * @param from The offset to write
  1045. * @param len number of bytes to write
  1046. * @param retlen pointer to variable to store the number of write bytes
  1047. * @param buf the databuffer to put/get data
  1048. *
  1049. * Write OTP block area.
  1050. */
  1051. static int do_otp_write(struct mtd_info *mtd, loff_t from, size_t len,
  1052. size_t *retlen, u_char *buf)
  1053. {
  1054. struct onenand_chip *this = mtd->priv;
  1055. unsigned char *pbuf = buf;
  1056. int ret;
  1057. /* Force buffer page aligned */
  1058. if (len < mtd->writesize) {
  1059. memcpy(this->page_buf, buf, len);
  1060. memset(this->page_buf + len, 0xff, mtd->writesize - len);
  1061. pbuf = this->page_buf;
  1062. len = mtd->writesize;
  1063. }
  1064. /* Enter OTP access mode */
  1065. this->command(mtd, ONENAND_CMD_OTP_ACCESS, 0, 0);
  1066. this->wait(mtd, FL_OTPING);
  1067. ret = mtd->write(mtd, from, len, retlen, pbuf);
  1068. /* Exit OTP access mode */
  1069. this->command(mtd, ONENAND_CMD_RESET, 0, 0);
  1070. this->wait(mtd, FL_RESETING);
  1071. return ret;
  1072. }
  1073. /**
  1074. * do_otp_lock - [DEFAULT] Lock OTP block area
  1075. * @param mtd MTD device structure
  1076. * @param from The offset to lock
  1077. * @param len number of bytes to lock
  1078. * @param retlen pointer to variable to store the number of lock bytes
  1079. * @param buf the databuffer to put/get data
  1080. *
  1081. * Lock OTP block area.
  1082. */
  1083. static int do_otp_lock(struct mtd_info *mtd, loff_t from, size_t len,
  1084. size_t *retlen, u_char *buf)
  1085. {
  1086. struct onenand_chip *this = mtd->priv;
  1087. int ret;
  1088. /* Enter OTP access mode */
  1089. this->command(mtd, ONENAND_CMD_OTP_ACCESS, 0, 0);
  1090. this->wait(mtd, FL_OTPING);
  1091. ret = mtd->write_oob(mtd, from, len, retlen, buf);
  1092. /* Exit OTP access mode */
  1093. this->command(mtd, ONENAND_CMD_RESET, 0, 0);
  1094. this->wait(mtd, FL_RESETING);
  1095. return ret;
  1096. }
  1097. /**
  1098. * onenand_otp_walk - [DEFAULT] Handle OTP operation
  1099. * @param mtd MTD device structure
  1100. * @param from The offset to read/write
  1101. * @param len number of bytes to read/write
  1102. * @param retlen pointer to variable to store the number of read bytes
  1103. * @param buf the databuffer to put/get data
  1104. * @param action do given action
  1105. * @param mode specify user and factory
  1106. *
  1107. * Handle OTP operation.
  1108. */
  1109. static int onenand_otp_walk(struct mtd_info *mtd, loff_t from, size_t len,
  1110. size_t *retlen, u_char *buf,
  1111. otp_op_t action, int mode)
  1112. {
  1113. struct onenand_chip *this = mtd->priv;
  1114. int otp_pages;
  1115. int density;
  1116. int ret = 0;
  1117. *retlen = 0;
  1118. density = this->device_id >> ONENAND_DEVICE_DENSITY_SHIFT;
  1119. if (density < ONENAND_DEVICE_DENSITY_512Mb)
  1120. otp_pages = 20;
  1121. else
  1122. otp_pages = 10;
  1123. if (mode == MTD_OTP_FACTORY) {
  1124. from += mtd->writesize * otp_pages;
  1125. otp_pages = 64 - otp_pages;
  1126. }
  1127. /* Check User/Factory boundary */
  1128. if (((mtd->writesize * otp_pages) - (from + len)) < 0)
  1129. return 0;
  1130. while (len > 0 && otp_pages > 0) {
  1131. if (!action) { /* OTP Info functions */
  1132. struct otp_info *otpinfo;
  1133. len -= sizeof(struct otp_info);
  1134. if (len <= 0)
  1135. return -ENOSPC;
  1136. otpinfo = (struct otp_info *) buf;
  1137. otpinfo->start = from;
  1138. otpinfo->length = mtd->writesize;
  1139. otpinfo->locked = 0;
  1140. from += mtd->writesize;
  1141. buf += sizeof(struct otp_info);
  1142. *retlen += sizeof(struct otp_info);
  1143. } else {
  1144. size_t tmp_retlen;
  1145. int size = len;
  1146. ret = action(mtd, from, len, &tmp_retlen, buf);
  1147. buf += size;
  1148. len -= size;
  1149. *retlen += size;
  1150. if (ret < 0)
  1151. return ret;
  1152. }
  1153. otp_pages--;
  1154. }
  1155. return 0;
  1156. }
  1157. /**
  1158. * onenand_get_fact_prot_info - [MTD Interface] Read factory OTP info
  1159. * @param mtd MTD device structure
  1160. * @param buf the databuffer to put/get data
  1161. * @param len number of bytes to read
  1162. *
  1163. * Read factory OTP info.
  1164. */
  1165. static int onenand_get_fact_prot_info(struct mtd_info *mtd,
  1166. struct otp_info *buf, size_t len)
  1167. {
  1168. size_t retlen;
  1169. int ret;
  1170. ret = onenand_otp_walk(mtd, 0, len, &retlen, (u_char *) buf, NULL, MTD_OTP_FACTORY);
  1171. return ret ? : retlen;
  1172. }
  1173. /**
  1174. * onenand_read_fact_prot_reg - [MTD Interface] Read factory OTP area
  1175. * @param mtd MTD device structure
  1176. * @param from The offset to read
  1177. * @param len number of bytes to read
  1178. * @param retlen pointer to variable to store the number of read bytes
  1179. * @param buf the databuffer to put/get data
  1180. *
  1181. * Read factory OTP area.
  1182. */
  1183. static int onenand_read_fact_prot_reg(struct mtd_info *mtd, loff_t from,
  1184. size_t len, size_t *retlen, u_char *buf)
  1185. {
  1186. return onenand_otp_walk(mtd, from, len, retlen, buf, do_otp_read, MTD_OTP_FACTORY);
  1187. }
  1188. /**
  1189. * onenand_get_user_prot_info - [MTD Interface] Read user OTP info
  1190. * @param mtd MTD device structure
  1191. * @param buf the databuffer to put/get data
  1192. * @param len number of bytes to read
  1193. *
  1194. * Read user OTP info.
  1195. */
  1196. static int onenand_get_user_prot_info(struct mtd_info *mtd,
  1197. struct otp_info *buf, size_t len)
  1198. {
  1199. size_t retlen;
  1200. int ret;
  1201. ret = onenand_otp_walk(mtd, 0, len, &retlen, (u_char *) buf, NULL, MTD_OTP_USER);
  1202. return ret ? : retlen;
  1203. }
  1204. /**
  1205. * onenand_read_user_prot_reg - [MTD Interface] Read user OTP area
  1206. * @param mtd MTD device structure
  1207. * @param from The offset to read
  1208. * @param len number of bytes to read
  1209. * @param retlen pointer to variable to store the number of read bytes
  1210. * @param buf the databuffer to put/get data
  1211. *
  1212. * Read user OTP area.
  1213. */
  1214. static int onenand_read_user_prot_reg(struct mtd_info *mtd, loff_t from,
  1215. size_t len, size_t *retlen, u_char *buf)
  1216. {
  1217. return onenand_otp_walk(mtd, from, len, retlen, buf, do_otp_read, MTD_OTP_USER);
  1218. }
  1219. /**
  1220. * onenand_write_user_prot_reg - [MTD Interface] Write user OTP area
  1221. * @param mtd MTD device structure
  1222. * @param from The offset to write
  1223. * @param len number of bytes to write
  1224. * @param retlen pointer to variable to store the number of write bytes
  1225. * @param buf the databuffer to put/get data
  1226. *
  1227. * Write user OTP area.
  1228. */
  1229. static int onenand_write_user_prot_reg(struct mtd_info *mtd, loff_t from,
  1230. size_t len, size_t *retlen, u_char *buf)
  1231. {
  1232. return onenand_otp_walk(mtd, from, len, retlen, buf, do_otp_write, MTD_OTP_USER);
  1233. }
  1234. /**
  1235. * onenand_lock_user_prot_reg - [MTD Interface] Lock user OTP area
  1236. * @param mtd MTD device structure
  1237. * @param from The offset to lock
  1238. * @param len number of bytes to unlock
  1239. *
  1240. * Write lock mark on spare area in page 0 in OTP block
  1241. */
  1242. static int onenand_lock_user_prot_reg(struct mtd_info *mtd, loff_t from,
  1243. size_t len)
  1244. {
  1245. unsigned char oob_buf[64];
  1246. size_t retlen;
  1247. int ret;
  1248. memset(oob_buf, 0xff, mtd->oobsize);
  1249. /*
  1250. * Note: OTP lock operation
  1251. * OTP block : 0xXXFC
  1252. * 1st block : 0xXXF3 (If chip support)
  1253. * Both : 0xXXF0 (If chip support)
  1254. */
  1255. oob_buf[ONENAND_OTP_LOCK_OFFSET] = 0xFC;
  1256. /*
  1257. * Write lock mark to 8th word of sector0 of page0 of the spare0.
  1258. * We write 16 bytes spare area instead of 2 bytes.
  1259. */
  1260. from = 0;
  1261. len = 16;
  1262. ret = onenand_otp_walk(mtd, from, len, &retlen, oob_buf, do_otp_lock, MTD_OTP_USER);
  1263. return ret ? : retlen;
  1264. }
  1265. #endif /* CONFIG_MTD_ONENAND_OTP */
  1266. /**
  1267. * onenand_print_device_info - Print device ID
  1268. * @param device device ID
  1269. *
  1270. * Print device ID
  1271. */
  1272. static void onenand_print_device_info(int device)
  1273. {
  1274. int vcc, demuxed, ddp, density;
  1275. vcc = device & ONENAND_DEVICE_VCC_MASK;
  1276. demuxed = device & ONENAND_DEVICE_IS_DEMUX;
  1277. ddp = device & ONENAND_DEVICE_IS_DDP;
  1278. density = device >> ONENAND_DEVICE_DENSITY_SHIFT;
  1279. printk(KERN_INFO "%sOneNAND%s %dMB %sV 16-bit (0x%02x)\n",
  1280. demuxed ? "" : "Muxed ",
  1281. ddp ? "(DDP)" : "",
  1282. (16 << density),
  1283. vcc ? "2.65/3.3" : "1.8",
  1284. device);
  1285. }
  1286. static const struct onenand_manufacturers onenand_manuf_ids[] = {
  1287. {ONENAND_MFR_SAMSUNG, "Samsung"},
  1288. };
  1289. /**
  1290. * onenand_check_maf - Check manufacturer ID
  1291. * @param manuf manufacturer ID
  1292. *
  1293. * Check manufacturer ID
  1294. */
  1295. static int onenand_check_maf(int manuf)
  1296. {
  1297. int size = ARRAY_SIZE(onenand_manuf_ids);
  1298. char *name;
  1299. int i;
  1300. for (i = 0; i < size; i++)
  1301. if (manuf == onenand_manuf_ids[i].id)
  1302. break;
  1303. if (i < size)
  1304. name = onenand_manuf_ids[i].name;
  1305. else
  1306. name = "Unknown";
  1307. printk(KERN_DEBUG "OneNAND Manufacturer: %s (0x%0x)\n", name, manuf);
  1308. return (i == size);
  1309. }
  1310. /**
  1311. * onenand_probe - [OneNAND Interface] Probe the OneNAND device
  1312. * @param mtd MTD device structure
  1313. *
  1314. * OneNAND detection method:
  1315. * Compare the the values from command with ones from register
  1316. */
  1317. static int onenand_probe(struct mtd_info *mtd)
  1318. {
  1319. struct onenand_chip *this = mtd->priv;
  1320. int bram_maf_id, bram_dev_id, maf_id, dev_id;
  1321. int version_id;
  1322. int density;
  1323. /* Send the command for reading device ID from BootRAM */
  1324. this->write_word(ONENAND_CMD_READID, this->base + ONENAND_BOOTRAM);
  1325. /* Read manufacturer and device IDs from BootRAM */
  1326. bram_maf_id = this->read_word(this->base + ONENAND_BOOTRAM + 0x0);
  1327. bram_dev_id = this->read_word(this->base + ONENAND_BOOTRAM + 0x2);
  1328. /* Check manufacturer ID */
  1329. if (onenand_check_maf(bram_maf_id))
  1330. return -ENXIO;
  1331. /* Reset OneNAND to read default register values */
  1332. this->write_word(ONENAND_CMD_RESET, this->base + ONENAND_BOOTRAM);
  1333. /* Read manufacturer and device IDs from Register */
  1334. maf_id = this->read_word(this->base + ONENAND_REG_MANUFACTURER_ID);
  1335. dev_id = this->read_word(this->base + ONENAND_REG_DEVICE_ID);
  1336. /* Check OneNAND device */
  1337. if (maf_id != bram_maf_id || dev_id != bram_dev_id)
  1338. return -ENXIO;
  1339. /* Flash device information */
  1340. onenand_print_device_info(dev_id);
  1341. this->device_id = dev_id;
  1342. density = dev_id >> ONENAND_DEVICE_DENSITY_SHIFT;
  1343. this->chipsize = (16 << density) << 20;
  1344. /* Set density mask. it is used for DDP */
  1345. this->density_mask = (1 << (density + 6));
  1346. /* OneNAND page size & block size */
  1347. /* The data buffer size is equal to page size */
  1348. mtd->writesize = this->read_word(this->base + ONENAND_REG_DATA_BUFFER_SIZE);
  1349. mtd->oobsize = mtd->writesize >> 5;
  1350. /* Pagers per block is always 64 in OneNAND */
  1351. mtd->erasesize = mtd->writesize << 6;
  1352. this->erase_shift = ffs(mtd->erasesize) - 1;
  1353. this->page_shift = ffs(mtd->writesize) - 1;
  1354. this->ppb_shift = (this->erase_shift - this->page_shift);
  1355. this->page_mask = (mtd->erasesize / mtd->writesize) - 1;
  1356. /* REVIST: Multichip handling */
  1357. mtd->size = this->chipsize;
  1358. /* Version ID */
  1359. version_id = this->read_word(this->base + ONENAND_REG_VERSION_ID);
  1360. printk(KERN_DEBUG "OneNAND version = 0x%04x\n", version_id);
  1361. /* Lock scheme */
  1362. if (density <= ONENAND_DEVICE_DENSITY_512Mb &&
  1363. !(version_id >> ONENAND_VERSION_PROCESS_SHIFT)) {
  1364. printk(KERN_INFO "Lock scheme is Continues Lock\n");
  1365. this->options |= ONENAND_CONT_LOCK;
  1366. }
  1367. return 0;
  1368. }
  1369. /**
  1370. * onenand_suspend - [MTD Interface] Suspend the OneNAND flash
  1371. * @param mtd MTD device structure
  1372. */
  1373. static int onenand_suspend(struct mtd_info *mtd)
  1374. {
  1375. return onenand_get_device(mtd, FL_PM_SUSPENDED);
  1376. }
  1377. /**
  1378. * onenand_resume - [MTD Interface] Resume the OneNAND flash
  1379. * @param mtd MTD device structure
  1380. */
  1381. static void onenand_resume(struct mtd_info *mtd)
  1382. {
  1383. struct onenand_chip *this = mtd->priv;
  1384. if (this->state == FL_PM_SUSPENDED)
  1385. onenand_release_device(mtd);
  1386. else
  1387. printk(KERN_ERR "resume() called for the chip which is not"
  1388. "in suspended state\n");
  1389. }
  1390. /**
  1391. * onenand_scan - [OneNAND Interface] Scan for the OneNAND device
  1392. * @param mtd MTD device structure
  1393. * @param maxchips Number of chips to scan for
  1394. *
  1395. * This fills out all the not initialized function pointers
  1396. * with the defaults.
  1397. * The flash ID is read and the mtd/chip structures are
  1398. * filled with the appropriate values.
  1399. */
  1400. int onenand_scan(struct mtd_info *mtd, int maxchips)
  1401. {
  1402. struct onenand_chip *this = mtd->priv;
  1403. if (!this->read_word)
  1404. this->read_word = onenand_readw;
  1405. if (!this->write_word)
  1406. this->write_word = onenand_writew;
  1407. if (!this->command)
  1408. this->command = onenand_command;
  1409. if (!this->wait)
  1410. this->wait = onenand_wait;
  1411. if (!this->read_bufferram)
  1412. this->read_bufferram = onenand_read_bufferram;
  1413. if (!this->write_bufferram)
  1414. this->write_bufferram = onenand_write_bufferram;
  1415. if (!this->block_markbad)
  1416. this->block_markbad = onenand_default_block_markbad;
  1417. if (!this->scan_bbt)
  1418. this->scan_bbt = onenand_default_bbt;
  1419. if (onenand_probe(mtd))
  1420. return -ENXIO;
  1421. /* Set Sync. Burst Read after probing */
  1422. if (this->mmcontrol) {
  1423. printk(KERN_INFO "OneNAND Sync. Burst Read support\n");
  1424. this->read_bufferram = onenand_sync_read_bufferram;
  1425. }
  1426. /* Allocate buffers, if necessary */
  1427. if (!this->page_buf) {
  1428. size_t len;
  1429. len = mtd->writesize + mtd->oobsize;
  1430. this->page_buf = kmalloc(len, GFP_KERNEL);
  1431. if (!this->page_buf) {
  1432. printk(KERN_ERR "onenand_scan(): Can't allocate page_buf\n");
  1433. return -ENOMEM;
  1434. }
  1435. this->options |= ONENAND_PAGEBUF_ALLOC;
  1436. }
  1437. this->state = FL_READY;
  1438. init_waitqueue_head(&this->wq);
  1439. spin_lock_init(&this->chip_lock);
  1440. switch (mtd->oobsize) {
  1441. case 64:
  1442. this->autooob = &onenand_oob_64;
  1443. break;
  1444. case 32:
  1445. this->autooob = &onenand_oob_32;
  1446. break;
  1447. default:
  1448. printk(KERN_WARNING "No OOB scheme defined for oobsize %d\n",
  1449. mtd->oobsize);
  1450. /* To prevent kernel oops */
  1451. this->autooob = &onenand_oob_32;
  1452. break;
  1453. }
  1454. memcpy(&mtd->oobinfo, this->autooob, sizeof(mtd->oobinfo));
  1455. /* Fill in remaining MTD driver data */
  1456. mtd->type = MTD_NANDFLASH;
  1457. mtd->flags = MTD_CAP_NANDFLASH;
  1458. mtd->ecctype = MTD_ECC_SW;
  1459. mtd->erase = onenand_erase;
  1460. mtd->point = NULL;
  1461. mtd->unpoint = NULL;
  1462. mtd->read = onenand_read;
  1463. mtd->write = onenand_write;
  1464. mtd->read_oob = onenand_read_oob;
  1465. mtd->write_oob = onenand_write_oob;
  1466. #ifdef CONFIG_MTD_ONENAND_OTP
  1467. mtd->get_fact_prot_info = onenand_get_fact_prot_info;
  1468. mtd->read_fact_prot_reg = onenand_read_fact_prot_reg;
  1469. mtd->get_user_prot_info = onenand_get_user_prot_info;
  1470. mtd->read_user_prot_reg = onenand_read_user_prot_reg;
  1471. mtd->write_user_prot_reg = onenand_write_user_prot_reg;
  1472. mtd->lock_user_prot_reg = onenand_lock_user_prot_reg;
  1473. #endif
  1474. mtd->sync = onenand_sync;
  1475. mtd->lock = NULL;
  1476. mtd->unlock = onenand_unlock;
  1477. mtd->suspend = onenand_suspend;
  1478. mtd->resume = onenand_resume;
  1479. mtd->block_isbad = onenand_block_isbad;
  1480. mtd->block_markbad = onenand_block_markbad;
  1481. mtd->owner = THIS_MODULE;
  1482. /* Unlock whole block */
  1483. mtd->unlock(mtd, 0x0, this->chipsize);
  1484. return this->scan_bbt(mtd);
  1485. }
  1486. /**
  1487. * onenand_release - [OneNAND Interface] Free resources held by the OneNAND device
  1488. * @param mtd MTD device structure
  1489. */
  1490. void onenand_release(struct mtd_info *mtd)
  1491. {
  1492. struct onenand_chip *this = mtd->priv;
  1493. #ifdef CONFIG_MTD_PARTITIONS
  1494. /* Deregister partitions */
  1495. del_mtd_partitions (mtd);
  1496. #endif
  1497. /* Deregister the device */
  1498. del_mtd_device (mtd);
  1499. /* Free bad block table memory, if allocated */
  1500. if (this->bbm)
  1501. kfree(this->bbm);
  1502. /* Buffer allocated by onenand_scan */
  1503. if (this->options & ONENAND_PAGEBUF_ALLOC)
  1504. kfree(this->page_buf);
  1505. }
  1506. EXPORT_SYMBOL_GPL(onenand_scan);
  1507. EXPORT_SYMBOL_GPL(onenand_release);
  1508. MODULE_LICENSE("GPL");
  1509. MODULE_AUTHOR("Kyungmin Park <kyungmin.park@samsung.com>");
  1510. MODULE_DESCRIPTION("Generic OneNAND flash driver code");