onenand_base.c 41 KB

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