onenand_base.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561
  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. *
  602. * Check DataRAM area directly
  603. */
  604. static int onenand_verify_page(struct mtd_info *mtd, u_char *buf, loff_t addr)
  605. {
  606. struct onenand_chip *this = mtd->priv;
  607. void __iomem *dataram0, *dataram1;
  608. int ret = 0;
  609. this->command(mtd, ONENAND_CMD_READ, addr, mtd->oobblock);
  610. ret = this->wait(mtd, FL_READING);
  611. if (ret)
  612. return ret;
  613. onenand_update_bufferram(mtd, addr, 1);
  614. /* Check, if the two dataram areas are same */
  615. dataram0 = this->base + ONENAND_DATARAM;
  616. dataram1 = dataram0 + mtd->oobblock;
  617. if (memcmp(dataram0, dataram1, mtd->oobblock))
  618. return -EBADMSG;
  619. return 0;
  620. }
  621. #else
  622. #define onenand_verify_page(...) (0)
  623. #endif
  624. #define NOTALIGNED(x) ((x & (mtd->oobblock - 1)) != 0)
  625. /**
  626. * onenand_write_ecc - [MTD Interface] OneNAND write with ECC
  627. * @param mtd MTD device structure
  628. * @param to offset to write to
  629. * @param len number of bytes to write
  630. * @param retlen pointer to variable to store the number of written bytes
  631. * @param buf the data to write
  632. * @param eccbuf filesystem supplied oob data buffer
  633. * @param oobsel oob selection structure
  634. *
  635. * OneNAND write with ECC
  636. */
  637. static int onenand_write_ecc(struct mtd_info *mtd, loff_t to, size_t len,
  638. size_t *retlen, const u_char *buf,
  639. u_char *eccbuf, struct nand_oobinfo *oobsel)
  640. {
  641. struct onenand_chip *this = mtd->priv;
  642. int written = 0;
  643. int ret = 0;
  644. DEBUG(MTD_DEBUG_LEVEL3, "onenand_write_ecc: to = 0x%08x, len = %i\n", (unsigned int) to, (int) len);
  645. /* Initialize retlen, in case of early exit */
  646. *retlen = 0;
  647. /* Do not allow writes past end of device */
  648. if (unlikely((to + len) > mtd->size)) {
  649. DEBUG(MTD_DEBUG_LEVEL0, "onenand_write_ecc: Attempt write to past end of device\n");
  650. return -EINVAL;
  651. }
  652. /* Reject writes, which are not page aligned */
  653. if (unlikely(NOTALIGNED(to)) || unlikely(NOTALIGNED(len))) {
  654. DEBUG(MTD_DEBUG_LEVEL0, "onenand_write_ecc: Attempt to write not page aligned data\n");
  655. return -EINVAL;
  656. }
  657. /* Grab the lock and see if the device is available */
  658. onenand_get_device(mtd, FL_WRITING);
  659. /* Loop until all data write */
  660. while (written < len) {
  661. int thislen = min_t(int, mtd->oobblock, len - written);
  662. this->command(mtd, ONENAND_CMD_BUFFERRAM, to, mtd->oobblock);
  663. this->write_bufferram(mtd, ONENAND_DATARAM, buf, 0, thislen);
  664. this->write_bufferram(mtd, ONENAND_SPARERAM, ffchars, 0, mtd->oobsize);
  665. this->command(mtd, ONENAND_CMD_PROG, to, mtd->oobblock);
  666. onenand_update_bufferram(mtd, to, 1);
  667. ret = this->wait(mtd, FL_WRITING);
  668. if (ret) {
  669. DEBUG(MTD_DEBUG_LEVEL0, "onenand_write_ecc: write filaed %d\n", ret);
  670. goto out;
  671. }
  672. written += thislen;
  673. /* Only check verify write turn on */
  674. ret = onenand_verify_page(mtd, (u_char *) buf, to);
  675. if (ret) {
  676. DEBUG(MTD_DEBUG_LEVEL0, "onenand_write_ecc: verify failed %d\n", ret);
  677. goto out;
  678. }
  679. if (written == len)
  680. break;
  681. to += thislen;
  682. buf += thislen;
  683. }
  684. out:
  685. /* Deselect and wake up anyone waiting on the device */
  686. onenand_release_device(mtd);
  687. *retlen = written;
  688. return ret;
  689. }
  690. /**
  691. * onenand_write - [MTD Interface] compability function for onenand_write_ecc
  692. * @param mtd MTD device structure
  693. * @param to offset to write to
  694. * @param len number of bytes to write
  695. * @param retlen pointer to variable to store the number of written bytes
  696. * @param buf the data to write
  697. *
  698. * This function simply calls onenand_write_ecc
  699. * with oob buffer and oobsel = NULL
  700. */
  701. static int onenand_write(struct mtd_info *mtd, loff_t to, size_t len,
  702. size_t *retlen, const u_char *buf)
  703. {
  704. return onenand_write_ecc(mtd, to, len, retlen, buf, NULL, NULL);
  705. }
  706. /**
  707. * onenand_write_oob - [MTD Interface] OneNAND write out-of-band
  708. * @param mtd MTD device structure
  709. * @param to offset to write to
  710. * @param len number of bytes to write
  711. * @param retlen pointer to variable to store the number of written bytes
  712. * @param buf the data to write
  713. *
  714. * OneNAND write out-of-band
  715. */
  716. static int onenand_write_oob(struct mtd_info *mtd, loff_t to, size_t len,
  717. size_t *retlen, const u_char *buf)
  718. {
  719. struct onenand_chip *this = mtd->priv;
  720. int column, status;
  721. int written = 0;
  722. DEBUG(MTD_DEBUG_LEVEL3, "onenand_write_oob: to = 0x%08x, len = %i\n", (unsigned int) to, (int) len);
  723. /* Initialize retlen, in case of early exit */
  724. *retlen = 0;
  725. /* Do not allow writes past end of device */
  726. if (unlikely((to + len) > mtd->size)) {
  727. DEBUG(MTD_DEBUG_LEVEL0, "onenand_write_oob: Attempt write to past end of device\n");
  728. return -EINVAL;
  729. }
  730. /* Grab the lock and see if the device is available */
  731. onenand_get_device(mtd, FL_WRITING);
  732. /* Loop until all data write */
  733. while (written < len) {
  734. int thislen = min_t(int, mtd->oobsize, len - written);
  735. column = to & (mtd->oobsize - 1);
  736. this->command(mtd, ONENAND_CMD_BUFFERRAM, to, mtd->oobsize);
  737. this->write_bufferram(mtd, ONENAND_SPARERAM, ffchars, 0, mtd->oobsize);
  738. this->write_bufferram(mtd, ONENAND_SPARERAM, buf, column, thislen);
  739. this->command(mtd, ONENAND_CMD_PROGOOB, to, mtd->oobsize);
  740. onenand_update_bufferram(mtd, to, 0);
  741. status = this->wait(mtd, FL_WRITING);
  742. if (status)
  743. goto out;
  744. written += thislen;
  745. if (written == len)
  746. break;
  747. to += thislen;
  748. buf += thislen;
  749. }
  750. out:
  751. /* Deselect and wake up anyone waiting on the device */
  752. onenand_release_device(mtd);
  753. *retlen = written;
  754. return 0;
  755. }
  756. /**
  757. * onenand_writev_ecc - [MTD Interface] write with iovec with ecc
  758. * @param mtd MTD device structure
  759. * @param vecs the iovectors to write
  760. * @param count number of vectors
  761. * @param to offset to write to
  762. * @param retlen pointer to variable to store the number of written bytes
  763. * @param eccbuf filesystem supplied oob data buffer
  764. * @param oobsel oob selection structure
  765. *
  766. * OneNAND write with iovec with ecc
  767. */
  768. static int onenand_writev_ecc(struct mtd_info *mtd, const struct kvec *vecs,
  769. unsigned long count, loff_t to, size_t *retlen,
  770. u_char *eccbuf, struct nand_oobinfo *oobsel)
  771. {
  772. struct onenand_chip *this = mtd->priv;
  773. unsigned char buffer[MAX_ONENAND_PAGESIZE], *pbuf;
  774. size_t total_len, len;
  775. int i, written = 0;
  776. int ret = 0;
  777. /* Preset written len for early exit */
  778. *retlen = 0;
  779. /* Calculate total length of data */
  780. total_len = 0;
  781. for (i = 0; i < count; i++)
  782. total_len += vecs[i].iov_len;
  783. DEBUG(MTD_DEBUG_LEVEL3, "onenand_writev_ecc: to = 0x%08x, len = %i, count = %ld\n", (unsigned int) to, (unsigned int) total_len, count);
  784. /* Do not allow write past end of the device */
  785. if (unlikely((to + total_len) > mtd->size)) {
  786. DEBUG(MTD_DEBUG_LEVEL0, "onenand_writev_ecc: Attempted write past end of device\n");
  787. return -EINVAL;
  788. }
  789. /* Reject writes, which are not page aligned */
  790. if (unlikely(NOTALIGNED(to)) || unlikely(NOTALIGNED(total_len))) {
  791. DEBUG(MTD_DEBUG_LEVEL0, "onenand_writev_ecc: Attempt to write not page aligned data\n");
  792. return -EINVAL;
  793. }
  794. /* Grab the lock and see if the device is available */
  795. onenand_get_device(mtd, FL_WRITING);
  796. /* TODO handling oob */
  797. /* Loop until all keve's data has been written */
  798. len = 0;
  799. while (count) {
  800. pbuf = buffer;
  801. /*
  802. * If the given tuple is >= pagesize then
  803. * write it out from the iov
  804. */
  805. if ((vecs->iov_len - len) >= mtd->oobblock) {
  806. pbuf = vecs->iov_base + len;
  807. len += mtd->oobblock;
  808. /* Check, if we have to switch to the next tuple */
  809. if (len >= (int) vecs->iov_len) {
  810. vecs++;
  811. len = 0;
  812. count--;
  813. }
  814. } else {
  815. int cnt = 0, thislen;
  816. while (cnt < mtd->oobblock) {
  817. thislen = min_t(int, mtd->oobblock - cnt, vecs->iov_len - len);
  818. memcpy(buffer + cnt, vecs->iov_base + len, thislen);
  819. cnt += thislen;
  820. len += thislen;
  821. /* Check, if we have to switch to the next tuple */
  822. if (len >= (int) vecs->iov_len) {
  823. vecs++;
  824. len = 0;
  825. count--;
  826. }
  827. }
  828. }
  829. this->command(mtd, ONENAND_CMD_BUFFERRAM, to, mtd->oobblock);
  830. this->write_bufferram(mtd, ONENAND_DATARAM, pbuf, 0, mtd->oobblock);
  831. this->write_bufferram(mtd, ONENAND_SPARERAM, ffchars, 0, mtd->oobsize);
  832. this->command(mtd, ONENAND_CMD_PROG, to, mtd->oobblock);
  833. onenand_update_bufferram(mtd, to, 1);
  834. ret = this->wait(mtd, FL_WRITING);
  835. if (ret) {
  836. DEBUG(MTD_DEBUG_LEVEL0, "onenand_writev_ecc: write failed %d\n", ret);
  837. goto out;
  838. }
  839. /* Only check verify write turn on */
  840. ret = onenand_verify_page(mtd, (u_char *) pbuf, to);
  841. if (ret) {
  842. DEBUG(MTD_DEBUG_LEVEL0, "onenand_writev_ecc: verify failed %d\n", ret);
  843. goto out;
  844. }
  845. written += mtd->oobblock;
  846. to += mtd->oobblock;
  847. }
  848. out:
  849. /* Deselect and wakt up anyone waiting on the device */
  850. onenand_release_device(mtd);
  851. *retlen = written;
  852. return 0;
  853. }
  854. /**
  855. * onenand_writev - [MTD Interface] compabilty function for onenand_writev_ecc
  856. * @param mtd MTD device structure
  857. * @param vecs the iovectors to write
  858. * @param count number of vectors
  859. * @param to offset to write to
  860. * @param retlen pointer to variable to store the number of written bytes
  861. *
  862. * OneNAND write with kvec. This just calls the ecc function
  863. */
  864. static int onenand_writev(struct mtd_info *mtd, const struct kvec *vecs,
  865. unsigned long count, loff_t to, size_t *retlen)
  866. {
  867. return onenand_writev_ecc(mtd, vecs, count, to, retlen, NULL, NULL);
  868. }
  869. /**
  870. * onenand_block_checkbad - [GENERIC] Check if a block is marked bad
  871. * @param mtd MTD device structure
  872. * @param ofs offset from device start
  873. * @param getchip 0, if the chip is already selected
  874. * @param allowbbt 1, if its allowed to access the bbt area
  875. *
  876. * Check, if the block is bad. Either by reading the bad block table or
  877. * calling of the scan function.
  878. */
  879. static int onenand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int getchip, int allowbbt)
  880. {
  881. struct onenand_chip *this = mtd->priv;
  882. struct bbm_info *bbm = this->bbm;
  883. /* Return info from the table */
  884. return bbm->isbad_bbt(mtd, ofs, allowbbt);
  885. }
  886. /**
  887. * onenand_erase - [MTD Interface] erase block(s)
  888. * @param mtd MTD device structure
  889. * @param instr erase instruction
  890. *
  891. * Erase one ore more blocks
  892. */
  893. static int onenand_erase(struct mtd_info *mtd, struct erase_info *instr)
  894. {
  895. struct onenand_chip *this = mtd->priv;
  896. unsigned int block_size;
  897. loff_t addr;
  898. int len;
  899. int ret = 0;
  900. DEBUG(MTD_DEBUG_LEVEL3, "onenand_erase: start = 0x%08x, len = %i\n", (unsigned int) instr->addr, (unsigned int) instr->len);
  901. block_size = (1 << this->erase_shift);
  902. /* Start address must align on block boundary */
  903. if (unlikely(instr->addr & (block_size - 1))) {
  904. DEBUG(MTD_DEBUG_LEVEL0, "onenand_erase: Unaligned address\n");
  905. return -EINVAL;
  906. }
  907. /* Length must align on block boundary */
  908. if (unlikely(instr->len & (block_size - 1))) {
  909. DEBUG(MTD_DEBUG_LEVEL0, "onenand_erase: Length not block aligned\n");
  910. return -EINVAL;
  911. }
  912. /* Do not allow erase past end of device */
  913. if (unlikely((instr->len + instr->addr) > mtd->size)) {
  914. DEBUG(MTD_DEBUG_LEVEL0, "onenand_erase: Erase past end of device\n");
  915. return -EINVAL;
  916. }
  917. instr->fail_addr = 0xffffffff;
  918. /* Grab the lock and see if the device is available */
  919. onenand_get_device(mtd, FL_ERASING);
  920. /* Loop throught the pages */
  921. len = instr->len;
  922. addr = instr->addr;
  923. instr->state = MTD_ERASING;
  924. while (len) {
  925. /* Check if we have a bad block, we do not erase bad blocks */
  926. if (onenand_block_checkbad(mtd, addr, 0, 0)) {
  927. printk (KERN_WARNING "onenand_erase: attempt to erase a bad block at addr 0x%08x\n", (unsigned int) addr);
  928. instr->state = MTD_ERASE_FAILED;
  929. goto erase_exit;
  930. }
  931. this->command(mtd, ONENAND_CMD_ERASE, addr, block_size);
  932. ret = this->wait(mtd, FL_ERASING);
  933. /* Check, if it is write protected */
  934. if (ret) {
  935. if (ret == -EPERM)
  936. DEBUG(MTD_DEBUG_LEVEL0, "onenand_erase: Device is write protected!!!\n");
  937. else
  938. DEBUG(MTD_DEBUG_LEVEL0, "onenand_erase: Failed erase, block %d\n", (unsigned) (addr >> this->erase_shift));
  939. instr->state = MTD_ERASE_FAILED;
  940. instr->fail_addr = addr;
  941. goto erase_exit;
  942. }
  943. len -= block_size;
  944. addr += block_size;
  945. }
  946. instr->state = MTD_ERASE_DONE;
  947. erase_exit:
  948. ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
  949. /* Do call back function */
  950. if (!ret)
  951. mtd_erase_callback(instr);
  952. /* Deselect and wake up anyone waiting on the device */
  953. onenand_release_device(mtd);
  954. return ret;
  955. }
  956. /**
  957. * onenand_sync - [MTD Interface] sync
  958. * @param mtd MTD device structure
  959. *
  960. * Sync is actually a wait for chip ready function
  961. */
  962. static void onenand_sync(struct mtd_info *mtd)
  963. {
  964. DEBUG(MTD_DEBUG_LEVEL3, "onenand_sync: called\n");
  965. /* Grab the lock and see if the device is available */
  966. onenand_get_device(mtd, FL_SYNCING);
  967. /* Release it and go back */
  968. onenand_release_device(mtd);
  969. }
  970. /**
  971. * onenand_block_isbad - [MTD Interface] Check whether the block at the given offset is bad
  972. * @param mtd MTD device structure
  973. * @param ofs offset relative to mtd start
  974. *
  975. * Check whether the block is bad
  976. */
  977. static int onenand_block_isbad(struct mtd_info *mtd, loff_t ofs)
  978. {
  979. /* Check for invalid offset */
  980. if (ofs > mtd->size)
  981. return -EINVAL;
  982. return onenand_block_checkbad(mtd, ofs, 1, 0);
  983. }
  984. /**
  985. * onenand_default_block_markbad - [DEFAULT] mark a block bad
  986. * @param mtd MTD device structure
  987. * @param ofs offset from device start
  988. *
  989. * This is the default implementation, which can be overridden by
  990. * a hardware specific driver.
  991. */
  992. static int onenand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
  993. {
  994. struct onenand_chip *this = mtd->priv;
  995. struct bbm_info *bbm = this->bbm;
  996. u_char buf[2] = {0, 0};
  997. size_t retlen;
  998. int block;
  999. /* Get block number */
  1000. block = ((int) ofs) >> bbm->bbt_erase_shift;
  1001. if (bbm->bbt)
  1002. bbm->bbt[block >> 2] |= 0x01 << ((block & 0x03) << 1);
  1003. /* We write two bytes, so we dont have to mess with 16 bit access */
  1004. ofs += mtd->oobsize + (bbm->badblockpos & ~0x01);
  1005. return mtd->write_oob(mtd, ofs , 2, &retlen, buf);
  1006. }
  1007. /**
  1008. * onenand_block_markbad - [MTD Interface] Mark the block at the given offset as bad
  1009. * @param mtd MTD device structure
  1010. * @param ofs offset relative to mtd start
  1011. *
  1012. * Mark the block as bad
  1013. */
  1014. static int onenand_block_markbad(struct mtd_info *mtd, loff_t ofs)
  1015. {
  1016. struct onenand_chip *this = mtd->priv;
  1017. int ret;
  1018. ret = onenand_block_isbad(mtd, ofs);
  1019. if (ret) {
  1020. /* If it was bad already, return success and do nothing */
  1021. if (ret > 0)
  1022. return 0;
  1023. return ret;
  1024. }
  1025. return this->block_markbad(mtd, ofs);
  1026. }
  1027. /**
  1028. * onenand_unlock - [MTD Interface] Unlock block(s)
  1029. * @param mtd MTD device structure
  1030. * @param ofs offset relative to mtd start
  1031. * @param len number of bytes to unlock
  1032. *
  1033. * Unlock one or more blocks
  1034. */
  1035. static int onenand_unlock(struct mtd_info *mtd, loff_t ofs, size_t len)
  1036. {
  1037. struct onenand_chip *this = mtd->priv;
  1038. int start, end, block, value, status;
  1039. start = ofs >> this->erase_shift;
  1040. end = len >> this->erase_shift;
  1041. /* Continuous lock scheme */
  1042. if (this->options & ONENAND_CONT_LOCK) {
  1043. /* Set start block address */
  1044. this->write_word(start, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
  1045. /* Set end block address */
  1046. this->write_word(end - 1, this->base + ONENAND_REG_END_BLOCK_ADDRESS);
  1047. /* Write unlock command */
  1048. this->command(mtd, ONENAND_CMD_UNLOCK, 0, 0);
  1049. /* There's no return value */
  1050. this->wait(mtd, FL_UNLOCKING);
  1051. /* Sanity check */
  1052. while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
  1053. & ONENAND_CTRL_ONGO)
  1054. continue;
  1055. /* Check lock status */
  1056. status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
  1057. if (!(status & ONENAND_WP_US))
  1058. printk(KERN_ERR "wp status = 0x%x\n", status);
  1059. return 0;
  1060. }
  1061. /* Block lock scheme */
  1062. for (block = start; block < end; block++) {
  1063. /* Set start block address */
  1064. this->write_word(block, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
  1065. /* Write unlock command */
  1066. this->command(mtd, ONENAND_CMD_UNLOCK, 0, 0);
  1067. /* There's no return value */
  1068. this->wait(mtd, FL_UNLOCKING);
  1069. /* Sanity check */
  1070. while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
  1071. & ONENAND_CTRL_ONGO)
  1072. continue;
  1073. /* Set block address for read block status */
  1074. value = onenand_block_address(this->device_id, block);
  1075. this->write_word(value, this->base + ONENAND_REG_START_ADDRESS1);
  1076. /* Check lock status */
  1077. status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
  1078. if (!(status & ONENAND_WP_US))
  1079. printk(KERN_ERR "block = %d, wp status = 0x%x\n", block, status);
  1080. }
  1081. return 0;
  1082. }
  1083. /**
  1084. * onenand_print_device_info - Print device ID
  1085. * @param device device ID
  1086. *
  1087. * Print device ID
  1088. */
  1089. static void onenand_print_device_info(int device)
  1090. {
  1091. int vcc, demuxed, ddp, density;
  1092. vcc = device & ONENAND_DEVICE_VCC_MASK;
  1093. demuxed = device & ONENAND_DEVICE_IS_DEMUX;
  1094. ddp = device & ONENAND_DEVICE_IS_DDP;
  1095. density = device >> ONENAND_DEVICE_DENSITY_SHIFT;
  1096. printk(KERN_INFO "%sOneNAND%s %dMB %sV 16-bit (0x%02x)\n",
  1097. demuxed ? "" : "Muxed ",
  1098. ddp ? "(DDP)" : "",
  1099. (16 << density),
  1100. vcc ? "2.65/3.3" : "1.8",
  1101. device);
  1102. }
  1103. static const struct onenand_manufacturers onenand_manuf_ids[] = {
  1104. {ONENAND_MFR_SAMSUNG, "Samsung"},
  1105. {ONENAND_MFR_UNKNOWN, "Unknown"}
  1106. };
  1107. /**
  1108. * onenand_check_maf - Check manufacturer ID
  1109. * @param manuf manufacturer ID
  1110. *
  1111. * Check manufacturer ID
  1112. */
  1113. static int onenand_check_maf(int manuf)
  1114. {
  1115. int i;
  1116. for (i = 0; onenand_manuf_ids[i].id; i++) {
  1117. if (manuf == onenand_manuf_ids[i].id)
  1118. break;
  1119. }
  1120. printk(KERN_DEBUG "OneNAND Manufacturer: %s (0x%0x)\n",
  1121. onenand_manuf_ids[i].name, manuf);
  1122. return (i != ONENAND_MFR_UNKNOWN);
  1123. }
  1124. /**
  1125. * onenand_probe - [OneNAND Interface] Probe the OneNAND device
  1126. * @param mtd MTD device structure
  1127. *
  1128. * OneNAND detection method:
  1129. * Compare the the values from command with ones from register
  1130. */
  1131. static int onenand_probe(struct mtd_info *mtd)
  1132. {
  1133. struct onenand_chip *this = mtd->priv;
  1134. int bram_maf_id, bram_dev_id, maf_id, dev_id;
  1135. int version_id;
  1136. int density;
  1137. /* Send the command for reading device ID from BootRAM */
  1138. this->write_word(ONENAND_CMD_READID, this->base + ONENAND_BOOTRAM);
  1139. /* Read manufacturer and device IDs from BootRAM */
  1140. bram_maf_id = this->read_word(this->base + ONENAND_BOOTRAM + 0x0);
  1141. bram_dev_id = this->read_word(this->base + ONENAND_BOOTRAM + 0x2);
  1142. /* Check manufacturer ID */
  1143. if (onenand_check_maf(bram_maf_id))
  1144. return -ENXIO;
  1145. /* Reset OneNAND to read default register values */
  1146. this->write_word(ONENAND_CMD_RESET, this->base + ONENAND_BOOTRAM);
  1147. /* Read manufacturer and device IDs from Register */
  1148. maf_id = this->read_word(this->base + ONENAND_REG_MANUFACTURER_ID);
  1149. dev_id = this->read_word(this->base + ONENAND_REG_DEVICE_ID);
  1150. /* Check OneNAND device */
  1151. if (maf_id != bram_maf_id || dev_id != bram_dev_id)
  1152. return -ENXIO;
  1153. /* Flash device information */
  1154. onenand_print_device_info(dev_id);
  1155. this->device_id = dev_id;
  1156. density = dev_id >> ONENAND_DEVICE_DENSITY_SHIFT;
  1157. this->chipsize = (16 << density) << 20;
  1158. /* OneNAND page size & block size */
  1159. /* The data buffer size is equal to page size */
  1160. mtd->oobblock = this->read_word(this->base + ONENAND_REG_DATA_BUFFER_SIZE);
  1161. mtd->oobsize = mtd->oobblock >> 5;
  1162. /* Pagers per block is always 64 in OneNAND */
  1163. mtd->erasesize = mtd->oobblock << 6;
  1164. this->erase_shift = ffs(mtd->erasesize) - 1;
  1165. this->page_shift = ffs(mtd->oobblock) - 1;
  1166. this->ppb_shift = (this->erase_shift - this->page_shift);
  1167. this->page_mask = (mtd->erasesize / mtd->oobblock) - 1;
  1168. /* REVIST: Multichip handling */
  1169. mtd->size = this->chipsize;
  1170. /* Version ID */
  1171. version_id = this->read_word(this->base + ONENAND_REG_VERSION_ID);
  1172. printk(KERN_DEBUG "OneNAND version = 0x%04x\n", version_id);
  1173. /* Lock scheme */
  1174. if (density <= ONENAND_DEVICE_DENSITY_512Mb &&
  1175. !(version_id >> ONENAND_VERSION_PROCESS_SHIFT)) {
  1176. printk(KERN_INFO "Lock scheme is Continues Lock\n");
  1177. this->options |= ONENAND_CONT_LOCK;
  1178. }
  1179. return 0;
  1180. }
  1181. /**
  1182. * onenand_scan - [OneNAND Interface] Scan for the OneNAND device
  1183. * @param mtd MTD device structure
  1184. * @param maxchips Number of chips to scan for
  1185. *
  1186. * This fills out all the not initialized function pointers
  1187. * with the defaults.
  1188. * The flash ID is read and the mtd/chip structures are
  1189. * filled with the appropriate values.
  1190. */
  1191. int onenand_scan(struct mtd_info *mtd, int maxchips)
  1192. {
  1193. struct onenand_chip *this = mtd->priv;
  1194. if (!this->read_word)
  1195. this->read_word = onenand_readw;
  1196. if (!this->write_word)
  1197. this->write_word = onenand_writew;
  1198. if (!this->command)
  1199. this->command = onenand_command;
  1200. if (!this->wait)
  1201. this->wait = onenand_wait;
  1202. if (!this->read_bufferram)
  1203. this->read_bufferram = onenand_read_bufferram;
  1204. if (!this->write_bufferram)
  1205. this->write_bufferram = onenand_write_bufferram;
  1206. if (!this->block_markbad)
  1207. this->block_markbad = onenand_default_block_markbad;
  1208. if (!this->scan_bbt)
  1209. this->scan_bbt = onenand_default_bbt;
  1210. if (onenand_probe(mtd))
  1211. return -ENXIO;
  1212. /* Set Sync. Burst Read after probing */
  1213. if (this->mmcontrol) {
  1214. printk(KERN_INFO "OneNAND Sync. Burst Read support\n");
  1215. this->read_bufferram = onenand_sync_read_bufferram;
  1216. }
  1217. this->state = FL_READY;
  1218. init_waitqueue_head(&this->wq);
  1219. spin_lock_init(&this->chip_lock);
  1220. switch (mtd->oobsize) {
  1221. case 64:
  1222. this->autooob = &onenand_oob_64;
  1223. break;
  1224. case 32:
  1225. this->autooob = &onenand_oob_32;
  1226. break;
  1227. default:
  1228. printk(KERN_WARNING "No OOB scheme defined for oobsize %d\n",
  1229. mtd->oobsize);
  1230. /* To prevent kernel oops */
  1231. this->autooob = &onenand_oob_32;
  1232. break;
  1233. }
  1234. memcpy(&mtd->oobinfo, this->autooob, sizeof(mtd->oobinfo));
  1235. /* Fill in remaining MTD driver data */
  1236. mtd->type = MTD_NANDFLASH;
  1237. mtd->flags = MTD_CAP_NANDFLASH | MTD_ECC;
  1238. mtd->ecctype = MTD_ECC_SW;
  1239. mtd->erase = onenand_erase;
  1240. mtd->point = NULL;
  1241. mtd->unpoint = NULL;
  1242. mtd->read = onenand_read;
  1243. mtd->write = onenand_write;
  1244. mtd->read_ecc = onenand_read_ecc;
  1245. mtd->write_ecc = onenand_write_ecc;
  1246. mtd->read_oob = onenand_read_oob;
  1247. mtd->write_oob = onenand_write_oob;
  1248. mtd->readv = NULL;
  1249. mtd->readv_ecc = NULL;
  1250. mtd->writev = onenand_writev;
  1251. mtd->writev_ecc = onenand_writev_ecc;
  1252. mtd->sync = onenand_sync;
  1253. mtd->lock = NULL;
  1254. mtd->unlock = onenand_unlock;
  1255. mtd->suspend = NULL;
  1256. mtd->resume = NULL;
  1257. mtd->block_isbad = onenand_block_isbad;
  1258. mtd->block_markbad = onenand_block_markbad;
  1259. mtd->owner = THIS_MODULE;
  1260. /* Unlock whole block */
  1261. mtd->unlock(mtd, 0x0, this->chipsize);
  1262. return this->scan_bbt(mtd);
  1263. }
  1264. /**
  1265. * onenand_release - [OneNAND Interface] Free resources held by the OneNAND device
  1266. * @param mtd MTD device structure
  1267. */
  1268. void onenand_release(struct mtd_info *mtd)
  1269. {
  1270. #ifdef CONFIG_MTD_PARTITIONS
  1271. /* Deregister partitions */
  1272. del_mtd_partitions (mtd);
  1273. #endif
  1274. /* Deregister the device */
  1275. del_mtd_device (mtd);
  1276. }
  1277. EXPORT_SYMBOL_GPL(onenand_scan);
  1278. EXPORT_SYMBOL_GPL(onenand_release);
  1279. MODULE_LICENSE("GPL");
  1280. MODULE_AUTHOR("Kyungmin Park <kyungmin.park@samsung.com>");
  1281. MODULE_DESCRIPTION("Generic OneNAND flash driver code");