onenand_base.c 48 KB

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