onenand_base.c 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857
  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. /* Send the command for reading device ID from BootRAM */
  1348. this->write_word(ONENAND_CMD_READID, this->base + ONENAND_BOOTRAM);
  1349. /* Read manufacturer and device IDs from BootRAM */
  1350. bram_maf_id = this->read_word(this->base + ONENAND_BOOTRAM + 0x0);
  1351. bram_dev_id = this->read_word(this->base + ONENAND_BOOTRAM + 0x2);
  1352. /* Check manufacturer ID */
  1353. if (onenand_check_maf(bram_maf_id))
  1354. return -ENXIO;
  1355. /* Reset OneNAND to read default register values */
  1356. this->write_word(ONENAND_CMD_RESET, this->base + ONENAND_BOOTRAM);
  1357. /* Read manufacturer and device IDs from Register */
  1358. maf_id = this->read_word(this->base + ONENAND_REG_MANUFACTURER_ID);
  1359. dev_id = this->read_word(this->base + ONENAND_REG_DEVICE_ID);
  1360. /* Check OneNAND device */
  1361. if (maf_id != bram_maf_id || dev_id != bram_dev_id)
  1362. return -ENXIO;
  1363. /* Flash device information */
  1364. onenand_print_device_info(dev_id);
  1365. this->device_id = dev_id;
  1366. density = dev_id >> ONENAND_DEVICE_DENSITY_SHIFT;
  1367. this->chipsize = (16 << density) << 20;
  1368. /* Set density mask. it is used for DDP */
  1369. this->density_mask = (1 << (density + 6));
  1370. /* OneNAND page size & block size */
  1371. /* The data buffer size is equal to page size */
  1372. mtd->writesize = this->read_word(this->base + ONENAND_REG_DATA_BUFFER_SIZE);
  1373. mtd->oobsize = mtd->writesize >> 5;
  1374. /* Pagers per block is always 64 in OneNAND */
  1375. mtd->erasesize = mtd->writesize << 6;
  1376. this->erase_shift = ffs(mtd->erasesize) - 1;
  1377. this->page_shift = ffs(mtd->writesize) - 1;
  1378. this->ppb_shift = (this->erase_shift - this->page_shift);
  1379. this->page_mask = (mtd->erasesize / mtd->writesize) - 1;
  1380. /* REVIST: Multichip handling */
  1381. mtd->size = this->chipsize;
  1382. /* Version ID */
  1383. version_id = this->read_word(this->base + ONENAND_REG_VERSION_ID);
  1384. printk(KERN_DEBUG "OneNAND version = 0x%04x\n", version_id);
  1385. /* Lock scheme */
  1386. if (density <= ONENAND_DEVICE_DENSITY_512Mb &&
  1387. !(version_id >> ONENAND_VERSION_PROCESS_SHIFT)) {
  1388. printk(KERN_INFO "Lock scheme is Continues Lock\n");
  1389. this->options |= ONENAND_CONT_LOCK;
  1390. }
  1391. return 0;
  1392. }
  1393. /**
  1394. * onenand_suspend - [MTD Interface] Suspend the OneNAND flash
  1395. * @param mtd MTD device structure
  1396. */
  1397. static int onenand_suspend(struct mtd_info *mtd)
  1398. {
  1399. return onenand_get_device(mtd, FL_PM_SUSPENDED);
  1400. }
  1401. /**
  1402. * onenand_resume - [MTD Interface] Resume the OneNAND flash
  1403. * @param mtd MTD device structure
  1404. */
  1405. static void onenand_resume(struct mtd_info *mtd)
  1406. {
  1407. struct onenand_chip *this = mtd->priv;
  1408. if (this->state == FL_PM_SUSPENDED)
  1409. onenand_release_device(mtd);
  1410. else
  1411. printk(KERN_ERR "resume() called for the chip which is not"
  1412. "in suspended state\n");
  1413. }
  1414. /**
  1415. * onenand_scan - [OneNAND Interface] Scan for the OneNAND device
  1416. * @param mtd MTD device structure
  1417. * @param maxchips Number of chips to scan for
  1418. *
  1419. * This fills out all the not initialized function pointers
  1420. * with the defaults.
  1421. * The flash ID is read and the mtd/chip structures are
  1422. * filled with the appropriate values.
  1423. */
  1424. int onenand_scan(struct mtd_info *mtd, int maxchips)
  1425. {
  1426. struct onenand_chip *this = mtd->priv;
  1427. if (!this->read_word)
  1428. this->read_word = onenand_readw;
  1429. if (!this->write_word)
  1430. this->write_word = onenand_writew;
  1431. if (!this->command)
  1432. this->command = onenand_command;
  1433. if (!this->wait)
  1434. this->wait = onenand_wait;
  1435. if (!this->read_bufferram)
  1436. this->read_bufferram = onenand_read_bufferram;
  1437. if (!this->write_bufferram)
  1438. this->write_bufferram = onenand_write_bufferram;
  1439. if (!this->block_markbad)
  1440. this->block_markbad = onenand_default_block_markbad;
  1441. if (!this->scan_bbt)
  1442. this->scan_bbt = onenand_default_bbt;
  1443. if (onenand_probe(mtd))
  1444. return -ENXIO;
  1445. /* Set Sync. Burst Read after probing */
  1446. if (this->mmcontrol) {
  1447. printk(KERN_INFO "OneNAND Sync. Burst Read support\n");
  1448. this->read_bufferram = onenand_sync_read_bufferram;
  1449. }
  1450. /* Allocate buffers, if necessary */
  1451. if (!this->page_buf) {
  1452. size_t len;
  1453. len = mtd->writesize + mtd->oobsize;
  1454. this->page_buf = kmalloc(len, GFP_KERNEL);
  1455. if (!this->page_buf) {
  1456. printk(KERN_ERR "onenand_scan(): Can't allocate page_buf\n");
  1457. return -ENOMEM;
  1458. }
  1459. this->options |= ONENAND_PAGEBUF_ALLOC;
  1460. }
  1461. this->state = FL_READY;
  1462. init_waitqueue_head(&this->wq);
  1463. spin_lock_init(&this->chip_lock);
  1464. switch (mtd->oobsize) {
  1465. case 64:
  1466. this->ecclayout = &onenand_oob_64;
  1467. break;
  1468. case 32:
  1469. this->ecclayout = &onenand_oob_32;
  1470. break;
  1471. default:
  1472. printk(KERN_WARNING "No OOB scheme defined for oobsize %d\n",
  1473. mtd->oobsize);
  1474. /* To prevent kernel oops */
  1475. this->ecclayout = &onenand_oob_32;
  1476. break;
  1477. }
  1478. mtd->ecclayout = this->ecclayout;
  1479. /* Fill in remaining MTD driver data */
  1480. mtd->type = MTD_NANDFLASH;
  1481. mtd->flags = MTD_CAP_NANDFLASH;
  1482. mtd->ecctype = MTD_ECC_SW;
  1483. mtd->erase = onenand_erase;
  1484. mtd->point = NULL;
  1485. mtd->unpoint = NULL;
  1486. mtd->read = onenand_read;
  1487. mtd->write = onenand_write;
  1488. mtd->read_oob = onenand_read_oob;
  1489. mtd->write_oob = onenand_write_oob;
  1490. #ifdef CONFIG_MTD_ONENAND_OTP
  1491. mtd->get_fact_prot_info = onenand_get_fact_prot_info;
  1492. mtd->read_fact_prot_reg = onenand_read_fact_prot_reg;
  1493. mtd->get_user_prot_info = onenand_get_user_prot_info;
  1494. mtd->read_user_prot_reg = onenand_read_user_prot_reg;
  1495. mtd->write_user_prot_reg = onenand_write_user_prot_reg;
  1496. mtd->lock_user_prot_reg = onenand_lock_user_prot_reg;
  1497. #endif
  1498. mtd->sync = onenand_sync;
  1499. mtd->lock = NULL;
  1500. mtd->unlock = onenand_unlock;
  1501. mtd->suspend = onenand_suspend;
  1502. mtd->resume = onenand_resume;
  1503. mtd->block_isbad = onenand_block_isbad;
  1504. mtd->block_markbad = onenand_block_markbad;
  1505. mtd->owner = THIS_MODULE;
  1506. /* Unlock whole block */
  1507. mtd->unlock(mtd, 0x0, this->chipsize);
  1508. return this->scan_bbt(mtd);
  1509. }
  1510. /**
  1511. * onenand_release - [OneNAND Interface] Free resources held by the OneNAND device
  1512. * @param mtd MTD device structure
  1513. */
  1514. void onenand_release(struct mtd_info *mtd)
  1515. {
  1516. struct onenand_chip *this = mtd->priv;
  1517. #ifdef CONFIG_MTD_PARTITIONS
  1518. /* Deregister partitions */
  1519. del_mtd_partitions (mtd);
  1520. #endif
  1521. /* Deregister the device */
  1522. del_mtd_device (mtd);
  1523. /* Free bad block table memory, if allocated */
  1524. if (this->bbm)
  1525. kfree(this->bbm);
  1526. /* Buffer allocated by onenand_scan */
  1527. if (this->options & ONENAND_PAGEBUF_ALLOC)
  1528. kfree(this->page_buf);
  1529. }
  1530. EXPORT_SYMBOL_GPL(onenand_scan);
  1531. EXPORT_SYMBOL_GPL(onenand_release);
  1532. MODULE_LICENSE("GPL");
  1533. MODULE_AUTHOR("Kyungmin Park <kyungmin.park@samsung.com>");
  1534. MODULE_DESCRIPTION("Generic OneNAND flash driver code");