onenand_base.c 41 KB

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