onenand_base.c 41 KB

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