onenand_base.c 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161
  1. /*
  2. * linux/drivers/mtd/onenand/onenand_base.c
  3. *
  4. * Copyright (C) 2005-2006 Samsung Electronics
  5. * Kyungmin Park <kyungmin.park@samsung.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/module.h>
  13. #include <linux/init.h>
  14. #include <linux/sched.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/jiffies.h>
  17. #include <linux/mtd/mtd.h>
  18. #include <linux/mtd/onenand.h>
  19. #include <linux/mtd/partitions.h>
  20. #include <asm/io.h>
  21. /**
  22. * onenand_oob_64 - oob info for large (2KB) page
  23. */
  24. static struct nand_ecclayout onenand_oob_64 = {
  25. .eccbytes = 20,
  26. .eccpos = {
  27. 8, 9, 10, 11, 12,
  28. 24, 25, 26, 27, 28,
  29. 40, 41, 42, 43, 44,
  30. 56, 57, 58, 59, 60,
  31. },
  32. .oobfree = {
  33. {2, 3}, {14, 2}, {18, 3}, {30, 2},
  34. {34, 3}, {46, 2}, {50, 3}, {62, 2}
  35. }
  36. };
  37. /**
  38. * onenand_oob_32 - oob info for middle (1KB) page
  39. */
  40. static struct nand_ecclayout onenand_oob_32 = {
  41. .eccbytes = 10,
  42. .eccpos = {
  43. 8, 9, 10, 11, 12,
  44. 24, 25, 26, 27, 28,
  45. },
  46. .oobfree = { {2, 3}, {14, 2}, {18, 3}, {30, 2} }
  47. };
  48. static const unsigned char ffchars[] = {
  49. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  50. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 16 */
  51. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  52. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 32 */
  53. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  54. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 48 */
  55. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  56. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 64 */
  57. };
  58. /**
  59. * onenand_readw - [OneNAND Interface] Read OneNAND register
  60. * @param addr address to read
  61. *
  62. * Read OneNAND register
  63. */
  64. static unsigned short onenand_readw(void __iomem *addr)
  65. {
  66. return readw(addr);
  67. }
  68. /**
  69. * onenand_writew - [OneNAND Interface] Write OneNAND register with value
  70. * @param value value to write
  71. * @param addr address to write
  72. *
  73. * Write OneNAND register with value
  74. */
  75. static void onenand_writew(unsigned short value, void __iomem *addr)
  76. {
  77. writew(value, addr);
  78. }
  79. /**
  80. * onenand_block_address - [DEFAULT] Get block address
  81. * @param this onenand chip data structure
  82. * @param block the block
  83. * @return translated block address if DDP, otherwise same
  84. *
  85. * Setup Start Address 1 Register (F100h)
  86. */
  87. static int onenand_block_address(struct onenand_chip *this, int block)
  88. {
  89. if (this->device_id & ONENAND_DEVICE_IS_DDP) {
  90. /* Device Flash Core select, NAND Flash Block Address */
  91. int dfs = 0;
  92. if (block & this->density_mask)
  93. dfs = 1;
  94. return (dfs << ONENAND_DDP_SHIFT) |
  95. (block & (this->density_mask - 1));
  96. }
  97. return block;
  98. }
  99. /**
  100. * onenand_bufferram_address - [DEFAULT] Get bufferram address
  101. * @param this onenand chip data structure
  102. * @param block the block
  103. * @return set DBS value if DDP, otherwise 0
  104. *
  105. * Setup Start Address 2 Register (F101h) for DDP
  106. */
  107. static int onenand_bufferram_address(struct onenand_chip *this, int block)
  108. {
  109. if (this->device_id & ONENAND_DEVICE_IS_DDP) {
  110. /* Device BufferRAM Select */
  111. int dbs = 0;
  112. if (block & this->density_mask)
  113. dbs = 1;
  114. return (dbs << ONENAND_DDP_SHIFT);
  115. }
  116. return 0;
  117. }
  118. /**
  119. * onenand_page_address - [DEFAULT] Get page address
  120. * @param page the page address
  121. * @param sector the sector address
  122. * @return combined page and sector address
  123. *
  124. * Setup Start Address 8 Register (F107h)
  125. */
  126. static int onenand_page_address(int page, int sector)
  127. {
  128. /* Flash Page Address, Flash Sector Address */
  129. int fpa, fsa;
  130. fpa = page & ONENAND_FPA_MASK;
  131. fsa = sector & ONENAND_FSA_MASK;
  132. return ((fpa << ONENAND_FPA_SHIFT) | fsa);
  133. }
  134. /**
  135. * onenand_buffer_address - [DEFAULT] Get buffer address
  136. * @param dataram1 DataRAM index
  137. * @param sectors the sector address
  138. * @param count the number of sectors
  139. * @return the start buffer value
  140. *
  141. * Setup Start Buffer Register (F200h)
  142. */
  143. static int onenand_buffer_address(int dataram1, int sectors, int count)
  144. {
  145. int bsa, bsc;
  146. /* BufferRAM Sector Address */
  147. bsa = sectors & ONENAND_BSA_MASK;
  148. if (dataram1)
  149. bsa |= ONENAND_BSA_DATARAM1; /* DataRAM1 */
  150. else
  151. bsa |= ONENAND_BSA_DATARAM0; /* DataRAM0 */
  152. /* BufferRAM Sector Count */
  153. bsc = count & ONENAND_BSC_MASK;
  154. return ((bsa << ONENAND_BSA_SHIFT) | bsc);
  155. }
  156. /**
  157. * onenand_command - [DEFAULT] Send command to OneNAND device
  158. * @param mtd MTD device structure
  159. * @param cmd the command to be sent
  160. * @param addr offset to read from or write to
  161. * @param len number of bytes to read or write
  162. *
  163. * Send command to OneNAND device. This function is used for middle/large page
  164. * devices (1KB/2KB Bytes per page)
  165. */
  166. static int onenand_command(struct mtd_info *mtd, int cmd, loff_t addr, size_t len)
  167. {
  168. struct onenand_chip *this = mtd->priv;
  169. int value, readcmd = 0, block_cmd = 0;
  170. int block, page;
  171. /* Address translation */
  172. switch (cmd) {
  173. case ONENAND_CMD_UNLOCK:
  174. case ONENAND_CMD_LOCK:
  175. case ONENAND_CMD_LOCK_TIGHT:
  176. case ONENAND_CMD_UNLOCK_ALL:
  177. block = -1;
  178. page = -1;
  179. break;
  180. case ONENAND_CMD_ERASE:
  181. case ONENAND_CMD_BUFFERRAM:
  182. case ONENAND_CMD_OTP_ACCESS:
  183. block_cmd = 1;
  184. block = (int) (addr >> this->erase_shift);
  185. page = -1;
  186. break;
  187. default:
  188. block = (int) (addr >> this->erase_shift);
  189. page = (int) (addr >> this->page_shift);
  190. page &= this->page_mask;
  191. break;
  192. }
  193. /* NOTE: The setting order of the registers is very important! */
  194. if (cmd == ONENAND_CMD_BUFFERRAM) {
  195. /* Select DataRAM for DDP */
  196. value = onenand_bufferram_address(this, block);
  197. this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
  198. /* Switch to the next data buffer */
  199. ONENAND_SET_NEXT_BUFFERRAM(this);
  200. return 0;
  201. }
  202. if (block != -1) {
  203. /* Write 'DFS, FBA' of Flash */
  204. value = onenand_block_address(this, block);
  205. this->write_word(value, this->base + ONENAND_REG_START_ADDRESS1);
  206. if (block_cmd) {
  207. /* Select DataRAM for DDP */
  208. value = onenand_bufferram_address(this, block);
  209. this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
  210. }
  211. }
  212. if (page != -1) {
  213. /* Now we use page size operation */
  214. int sectors = 4, count = 4;
  215. int dataram;
  216. switch (cmd) {
  217. case ONENAND_CMD_READ:
  218. case ONENAND_CMD_READOOB:
  219. dataram = ONENAND_SET_NEXT_BUFFERRAM(this);
  220. readcmd = 1;
  221. break;
  222. default:
  223. dataram = ONENAND_CURRENT_BUFFERRAM(this);
  224. break;
  225. }
  226. /* Write 'FPA, FSA' of Flash */
  227. value = onenand_page_address(page, sectors);
  228. this->write_word(value, this->base + ONENAND_REG_START_ADDRESS8);
  229. /* Write 'BSA, BSC' of DataRAM */
  230. value = onenand_buffer_address(dataram, sectors, count);
  231. this->write_word(value, this->base + ONENAND_REG_START_BUFFER);
  232. if (readcmd) {
  233. /* Select DataRAM for DDP */
  234. value = onenand_bufferram_address(this, block);
  235. this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
  236. }
  237. }
  238. /* Interrupt clear */
  239. this->write_word(ONENAND_INT_CLEAR, this->base + ONENAND_REG_INTERRUPT);
  240. /* Write command */
  241. this->write_word(cmd, this->base + ONENAND_REG_COMMAND);
  242. return 0;
  243. }
  244. /**
  245. * onenand_wait - [DEFAULT] wait until the command is done
  246. * @param mtd MTD device structure
  247. * @param state state to select the max. timeout value
  248. *
  249. * Wait for command done. This applies to all OneNAND command
  250. * Read can take up to 30us, erase up to 2ms and program up to 350us
  251. * according to general OneNAND specs
  252. */
  253. static int onenand_wait(struct mtd_info *mtd, int state)
  254. {
  255. struct onenand_chip * this = mtd->priv;
  256. unsigned long timeout;
  257. unsigned int flags = ONENAND_INT_MASTER;
  258. unsigned int interrupt = 0;
  259. unsigned int ctrl;
  260. /* The 20 msec is enough */
  261. timeout = jiffies + msecs_to_jiffies(20);
  262. while (time_before(jiffies, timeout)) {
  263. interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
  264. if (interrupt & flags)
  265. break;
  266. if (state != FL_READING)
  267. cond_resched();
  268. }
  269. /* To get correct interrupt status in timeout case */
  270. interrupt = this->read_word(this->base + ONENAND_REG_INTERRUPT);
  271. ctrl = this->read_word(this->base + ONENAND_REG_CTRL_STATUS);
  272. if (ctrl & ONENAND_CTRL_ERROR) {
  273. DEBUG(MTD_DEBUG_LEVEL0, "onenand_wait: controller error = 0x%04x\n", ctrl);
  274. if (ctrl & ONENAND_CTRL_LOCK)
  275. DEBUG(MTD_DEBUG_LEVEL0, "onenand_wait: it's locked error.\n");
  276. return ctrl;
  277. }
  278. if (interrupt & ONENAND_INT_READ) {
  279. int ecc = this->read_word(this->base + ONENAND_REG_ECC_STATUS);
  280. if (ecc) {
  281. DEBUG(MTD_DEBUG_LEVEL0, "onenand_wait: ECC error = 0x%04x\n", ecc);
  282. if (ecc & ONENAND_ECC_2BIT_ALL) {
  283. mtd->ecc_stats.failed++;
  284. return ecc;
  285. } else if (ecc & ONENAND_ECC_1BIT_ALL)
  286. mtd->ecc_stats.corrected++;
  287. }
  288. } else if (state == FL_READING) {
  289. printk(KERN_ERR "onenand_wait: read timeout! ctrl=0x%04x intr=0x%04x\n", ctrl, interrupt);
  290. return -EIO;
  291. }
  292. return 0;
  293. }
  294. /*
  295. * onenand_interrupt - [DEFAULT] onenand interrupt handler
  296. * @param irq onenand interrupt number
  297. * @param dev_id interrupt data
  298. *
  299. * complete the work
  300. */
  301. static irqreturn_t onenand_interrupt(int irq, void *data)
  302. {
  303. struct onenand_chip *this = (struct onenand_chip *) data;
  304. /* To handle shared interrupt */
  305. if (!this->complete.done)
  306. complete(&this->complete);
  307. return IRQ_HANDLED;
  308. }
  309. /*
  310. * onenand_interrupt_wait - [DEFAULT] wait until the command is done
  311. * @param mtd MTD device structure
  312. * @param state state to select the max. timeout value
  313. *
  314. * Wait for command done.
  315. */
  316. static int onenand_interrupt_wait(struct mtd_info *mtd, int state)
  317. {
  318. struct onenand_chip *this = mtd->priv;
  319. wait_for_completion(&this->complete);
  320. return onenand_wait(mtd, state);
  321. }
  322. /*
  323. * onenand_try_interrupt_wait - [DEFAULT] try interrupt wait
  324. * @param mtd MTD device structure
  325. * @param state state to select the max. timeout value
  326. *
  327. * Try interrupt based wait (It is used one-time)
  328. */
  329. static int onenand_try_interrupt_wait(struct mtd_info *mtd, int state)
  330. {
  331. struct onenand_chip *this = mtd->priv;
  332. unsigned long remain, timeout;
  333. /* We use interrupt wait first */
  334. this->wait = onenand_interrupt_wait;
  335. timeout = msecs_to_jiffies(100);
  336. remain = wait_for_completion_timeout(&this->complete, timeout);
  337. if (!remain) {
  338. printk(KERN_INFO "OneNAND: There's no interrupt. "
  339. "We use the normal wait\n");
  340. /* Release the irq */
  341. free_irq(this->irq, this);
  342. this->wait = onenand_wait;
  343. }
  344. return onenand_wait(mtd, state);
  345. }
  346. /*
  347. * onenand_setup_wait - [OneNAND Interface] setup onenand wait method
  348. * @param mtd MTD device structure
  349. *
  350. * There's two method to wait onenand work
  351. * 1. polling - read interrupt status register
  352. * 2. interrupt - use the kernel interrupt method
  353. */
  354. static void onenand_setup_wait(struct mtd_info *mtd)
  355. {
  356. struct onenand_chip *this = mtd->priv;
  357. int syscfg;
  358. init_completion(&this->complete);
  359. if (this->irq <= 0) {
  360. this->wait = onenand_wait;
  361. return;
  362. }
  363. if (request_irq(this->irq, &onenand_interrupt,
  364. IRQF_SHARED, "onenand", this)) {
  365. /* If we can't get irq, use the normal wait */
  366. this->wait = onenand_wait;
  367. return;
  368. }
  369. /* Enable interrupt */
  370. syscfg = this->read_word(this->base + ONENAND_REG_SYS_CFG1);
  371. syscfg |= ONENAND_SYS_CFG1_IOBE;
  372. this->write_word(syscfg, this->base + ONENAND_REG_SYS_CFG1);
  373. this->wait = onenand_try_interrupt_wait;
  374. }
  375. /**
  376. * onenand_bufferram_offset - [DEFAULT] BufferRAM offset
  377. * @param mtd MTD data structure
  378. * @param area BufferRAM area
  379. * @return offset given area
  380. *
  381. * Return BufferRAM offset given area
  382. */
  383. static inline int onenand_bufferram_offset(struct mtd_info *mtd, int area)
  384. {
  385. struct onenand_chip *this = mtd->priv;
  386. if (ONENAND_CURRENT_BUFFERRAM(this)) {
  387. if (area == ONENAND_DATARAM)
  388. return mtd->writesize;
  389. if (area == ONENAND_SPARERAM)
  390. return mtd->oobsize;
  391. }
  392. return 0;
  393. }
  394. /**
  395. * onenand_read_bufferram - [OneNAND Interface] Read the bufferram area
  396. * @param mtd MTD data structure
  397. * @param area BufferRAM area
  398. * @param buffer the databuffer to put/get data
  399. * @param offset offset to read from or write to
  400. * @param count number of bytes to read/write
  401. *
  402. * Read the BufferRAM area
  403. */
  404. static int onenand_read_bufferram(struct mtd_info *mtd, int area,
  405. unsigned char *buffer, int offset, size_t count)
  406. {
  407. struct onenand_chip *this = mtd->priv;
  408. void __iomem *bufferram;
  409. bufferram = this->base + area;
  410. bufferram += onenand_bufferram_offset(mtd, area);
  411. if (ONENAND_CHECK_BYTE_ACCESS(count)) {
  412. unsigned short word;
  413. /* Align with word(16-bit) size */
  414. count--;
  415. /* Read word and save byte */
  416. word = this->read_word(bufferram + offset + count);
  417. buffer[count] = (word & 0xff);
  418. }
  419. memcpy(buffer, bufferram + offset, count);
  420. return 0;
  421. }
  422. /**
  423. * onenand_sync_read_bufferram - [OneNAND Interface] Read the bufferram area with Sync. Burst mode
  424. * @param mtd MTD data structure
  425. * @param area BufferRAM area
  426. * @param buffer the databuffer to put/get data
  427. * @param offset offset to read from or write to
  428. * @param count number of bytes to read/write
  429. *
  430. * Read the BufferRAM area with Sync. Burst Mode
  431. */
  432. static int onenand_sync_read_bufferram(struct mtd_info *mtd, int area,
  433. unsigned char *buffer, int offset, size_t count)
  434. {
  435. struct onenand_chip *this = mtd->priv;
  436. void __iomem *bufferram;
  437. bufferram = this->base + area;
  438. bufferram += onenand_bufferram_offset(mtd, area);
  439. this->mmcontrol(mtd, ONENAND_SYS_CFG1_SYNC_READ);
  440. if (ONENAND_CHECK_BYTE_ACCESS(count)) {
  441. unsigned short word;
  442. /* Align with word(16-bit) size */
  443. count--;
  444. /* Read word and save byte */
  445. word = this->read_word(bufferram + offset + count);
  446. buffer[count] = (word & 0xff);
  447. }
  448. memcpy(buffer, bufferram + offset, count);
  449. this->mmcontrol(mtd, 0);
  450. return 0;
  451. }
  452. /**
  453. * onenand_write_bufferram - [OneNAND Interface] Write the bufferram area
  454. * @param mtd MTD data structure
  455. * @param area BufferRAM area
  456. * @param buffer the databuffer to put/get data
  457. * @param offset offset to read from or write to
  458. * @param count number of bytes to read/write
  459. *
  460. * Write the BufferRAM area
  461. */
  462. static int onenand_write_bufferram(struct mtd_info *mtd, int area,
  463. const unsigned char *buffer, int offset, size_t count)
  464. {
  465. struct onenand_chip *this = mtd->priv;
  466. void __iomem *bufferram;
  467. bufferram = this->base + area;
  468. bufferram += onenand_bufferram_offset(mtd, area);
  469. if (ONENAND_CHECK_BYTE_ACCESS(count)) {
  470. unsigned short word;
  471. int byte_offset;
  472. /* Align with word(16-bit) size */
  473. count--;
  474. /* Calculate byte access offset */
  475. byte_offset = offset + count;
  476. /* Read word and save byte */
  477. word = this->read_word(bufferram + byte_offset);
  478. word = (word & ~0xff) | buffer[count];
  479. this->write_word(word, bufferram + byte_offset);
  480. }
  481. memcpy(bufferram + offset, buffer, count);
  482. return 0;
  483. }
  484. /**
  485. * onenand_check_bufferram - [GENERIC] Check BufferRAM information
  486. * @param mtd MTD data structure
  487. * @param addr address to check
  488. * @return 1 if there are valid data, otherwise 0
  489. *
  490. * Check bufferram if there is data we required
  491. */
  492. static int onenand_check_bufferram(struct mtd_info *mtd, loff_t addr)
  493. {
  494. struct onenand_chip *this = mtd->priv;
  495. int block, page;
  496. int i;
  497. block = (int) (addr >> this->erase_shift);
  498. page = (int) (addr >> this->page_shift);
  499. page &= this->page_mask;
  500. i = ONENAND_CURRENT_BUFFERRAM(this);
  501. /* Is there valid data? */
  502. if (this->bufferram[i].block == block &&
  503. this->bufferram[i].page == page &&
  504. this->bufferram[i].valid)
  505. return 1;
  506. return 0;
  507. }
  508. /**
  509. * onenand_update_bufferram - [GENERIC] Update BufferRAM information
  510. * @param mtd MTD data structure
  511. * @param addr address to update
  512. * @param valid valid flag
  513. *
  514. * Update BufferRAM information
  515. */
  516. static int onenand_update_bufferram(struct mtd_info *mtd, loff_t addr,
  517. int valid)
  518. {
  519. struct onenand_chip *this = mtd->priv;
  520. int block, page;
  521. int i;
  522. block = (int) (addr >> this->erase_shift);
  523. page = (int) (addr >> this->page_shift);
  524. page &= this->page_mask;
  525. /* Invalidate BufferRAM */
  526. for (i = 0; i < MAX_BUFFERRAM; i++) {
  527. if (this->bufferram[i].block == block &&
  528. this->bufferram[i].page == page)
  529. this->bufferram[i].valid = 0;
  530. }
  531. /* Update BufferRAM */
  532. i = ONENAND_CURRENT_BUFFERRAM(this);
  533. this->bufferram[i].block = block;
  534. this->bufferram[i].page = page;
  535. this->bufferram[i].valid = valid;
  536. return 0;
  537. }
  538. /**
  539. * onenand_get_device - [GENERIC] Get chip for selected access
  540. * @param mtd MTD device structure
  541. * @param new_state the state which is requested
  542. *
  543. * Get the device and lock it for exclusive access
  544. */
  545. static int onenand_get_device(struct mtd_info *mtd, int new_state)
  546. {
  547. struct onenand_chip *this = mtd->priv;
  548. DECLARE_WAITQUEUE(wait, current);
  549. /*
  550. * Grab the lock and see if the device is available
  551. */
  552. while (1) {
  553. spin_lock(&this->chip_lock);
  554. if (this->state == FL_READY) {
  555. this->state = new_state;
  556. spin_unlock(&this->chip_lock);
  557. break;
  558. }
  559. if (new_state == FL_PM_SUSPENDED) {
  560. spin_unlock(&this->chip_lock);
  561. return (this->state == FL_PM_SUSPENDED) ? 0 : -EAGAIN;
  562. }
  563. set_current_state(TASK_UNINTERRUPTIBLE);
  564. add_wait_queue(&this->wq, &wait);
  565. spin_unlock(&this->chip_lock);
  566. schedule();
  567. remove_wait_queue(&this->wq, &wait);
  568. }
  569. return 0;
  570. }
  571. /**
  572. * onenand_release_device - [GENERIC] release chip
  573. * @param mtd MTD device structure
  574. *
  575. * Deselect, release chip lock and wake up anyone waiting on the device
  576. */
  577. static void onenand_release_device(struct mtd_info *mtd)
  578. {
  579. struct onenand_chip *this = mtd->priv;
  580. /* Release the chip */
  581. spin_lock(&this->chip_lock);
  582. this->state = FL_READY;
  583. wake_up(&this->wq);
  584. spin_unlock(&this->chip_lock);
  585. }
  586. /**
  587. * onenand_read - [MTD Interface] Read data from flash
  588. * @param mtd MTD device structure
  589. * @param from offset to read from
  590. * @param len number of bytes to read
  591. * @param retlen pointer to variable to store the number of read bytes
  592. * @param buf the databuffer to put data
  593. *
  594. * Read with ecc
  595. */
  596. static int onenand_read(struct mtd_info *mtd, loff_t from, size_t len,
  597. size_t *retlen, u_char *buf)
  598. {
  599. struct onenand_chip *this = mtd->priv;
  600. struct mtd_ecc_stats stats;
  601. int read = 0, column;
  602. int thislen;
  603. int ret = 0, boundary = 0;
  604. DEBUG(MTD_DEBUG_LEVEL3, "onenand_read: from = 0x%08x, len = %i\n", (unsigned int) from, (int) len);
  605. /* Do not allow reads past end of device */
  606. if ((from + len) > mtd->size) {
  607. DEBUG(MTD_DEBUG_LEVEL0, "onenand_read: Attempt read beyond end of device\n");
  608. *retlen = 0;
  609. return -EINVAL;
  610. }
  611. /* Grab the lock and see if the device is available */
  612. onenand_get_device(mtd, FL_READING);
  613. /* TODO handling oob */
  614. stats = mtd->ecc_stats;
  615. /* Read-while-load method */
  616. /* Do first load to bufferRAM */
  617. if (read < len) {
  618. if (!onenand_check_bufferram(mtd, from)) {
  619. this->command(mtd, ONENAND_CMD_READ, from, mtd->writesize);
  620. ret = this->wait(mtd, FL_READING);
  621. onenand_update_bufferram(mtd, from, !ret);
  622. }
  623. }
  624. thislen = min_t(int, mtd->writesize, len - read);
  625. column = from & (mtd->writesize - 1);
  626. if (column + thislen > mtd->writesize)
  627. thislen = mtd->writesize - column;
  628. while (!ret) {
  629. /* If there is more to load then start next load */
  630. from += thislen;
  631. if (read + thislen < len) {
  632. this->command(mtd, ONENAND_CMD_READ, from, mtd->writesize);
  633. /*
  634. * Chip boundary handling in DDP
  635. * Now we issued chip 1 read and pointed chip 1
  636. * bufferam so we have to point chip 0 bufferam.
  637. */
  638. if (this->device_id & ONENAND_DEVICE_IS_DDP &&
  639. unlikely(from == (this->chipsize >> 1))) {
  640. this->write_word(0, this->base + ONENAND_REG_START_ADDRESS2);
  641. boundary = 1;
  642. } else
  643. boundary = 0;
  644. ONENAND_SET_PREV_BUFFERRAM(this);
  645. }
  646. /* While load is going, read from last bufferRAM */
  647. this->read_bufferram(mtd, ONENAND_DATARAM, buf, column, thislen);
  648. /* See if we are done */
  649. read += thislen;
  650. if (read == len)
  651. break;
  652. /* Set up for next read from bufferRAM */
  653. if (unlikely(boundary))
  654. this->write_word(0x8000, this->base + ONENAND_REG_START_ADDRESS2);
  655. ONENAND_SET_NEXT_BUFFERRAM(this);
  656. buf += thislen;
  657. thislen = min_t(int, mtd->writesize, len - read);
  658. column = 0;
  659. cond_resched();
  660. /* Now wait for load */
  661. ret = this->wait(mtd, FL_READING);
  662. onenand_update_bufferram(mtd, from, !ret);
  663. }
  664. /* Deselect and wake up anyone waiting on the device */
  665. onenand_release_device(mtd);
  666. /*
  667. * Return success, if no ECC failures, else -EBADMSG
  668. * fs driver will take care of that, because
  669. * retlen == desired len and result == -EBADMSG
  670. */
  671. *retlen = read;
  672. if (mtd->ecc_stats.failed - stats.failed)
  673. return -EBADMSG;
  674. if (ret)
  675. return ret;
  676. return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
  677. }
  678. /**
  679. * onenand_do_read_oob - [MTD Interface] OneNAND read out-of-band
  680. * @param mtd MTD device structure
  681. * @param from offset to read from
  682. * @param len number of bytes to read
  683. * @param retlen pointer to variable to store the number of read bytes
  684. * @param buf the databuffer to put data
  685. *
  686. * OneNAND read out-of-band data from the spare area
  687. */
  688. int onenand_do_read_oob(struct mtd_info *mtd, loff_t from, size_t len,
  689. size_t *retlen, u_char *buf)
  690. {
  691. struct onenand_chip *this = mtd->priv;
  692. int read = 0, thislen, column;
  693. int ret = 0;
  694. DEBUG(MTD_DEBUG_LEVEL3, "onenand_read_oob: from = 0x%08x, len = %i\n", (unsigned int) from, (int) len);
  695. /* Initialize return length value */
  696. *retlen = 0;
  697. /* Do not allow reads past end of device */
  698. if (unlikely((from + len) > mtd->size)) {
  699. DEBUG(MTD_DEBUG_LEVEL0, "onenand_read_oob: Attempt read beyond end of device\n");
  700. return -EINVAL;
  701. }
  702. /* Grab the lock and see if the device is available */
  703. onenand_get_device(mtd, FL_READING);
  704. column = from & (mtd->oobsize - 1);
  705. while (read < len) {
  706. cond_resched();
  707. thislen = mtd->oobsize - column;
  708. thislen = min_t(int, thislen, len);
  709. this->command(mtd, ONENAND_CMD_READOOB, from, mtd->oobsize);
  710. onenand_update_bufferram(mtd, from, 0);
  711. ret = this->wait(mtd, FL_READING);
  712. /* First copy data and check return value for ECC handling */
  713. this->read_bufferram(mtd, ONENAND_SPARERAM, buf, column, thislen);
  714. if (ret) {
  715. DEBUG(MTD_DEBUG_LEVEL0, "onenand_read_oob: read failed = 0x%x\n", ret);
  716. goto out;
  717. }
  718. read += thislen;
  719. if (read == len)
  720. break;
  721. buf += thislen;
  722. /* Read more? */
  723. if (read < len) {
  724. /* Page size */
  725. from += mtd->writesize;
  726. column = 0;
  727. }
  728. }
  729. out:
  730. /* Deselect and wake up anyone waiting on the device */
  731. onenand_release_device(mtd);
  732. *retlen = read;
  733. return ret;
  734. }
  735. /**
  736. * onenand_read_oob - [MTD Interface] NAND write data and/or out-of-band
  737. * @mtd: MTD device structure
  738. * @from: offset to read from
  739. * @ops: oob operation description structure
  740. */
  741. static int onenand_read_oob(struct mtd_info *mtd, loff_t from,
  742. struct mtd_oob_ops *ops)
  743. {
  744. BUG_ON(ops->mode != MTD_OOB_PLACE);
  745. return onenand_do_read_oob(mtd, from + ops->ooboffs, ops->ooblen,
  746. &ops->oobretlen, ops->oobbuf);
  747. }
  748. #ifdef CONFIG_MTD_ONENAND_VERIFY_WRITE
  749. /**
  750. * onenand_verify_oob - [GENERIC] verify the oob contents after a write
  751. * @param mtd MTD device structure
  752. * @param buf the databuffer to verify
  753. * @param to offset to read from
  754. * @param len number of bytes to read and compare
  755. *
  756. */
  757. static int onenand_verify_oob(struct mtd_info *mtd, const u_char *buf, loff_t to, int len)
  758. {
  759. struct onenand_chip *this = mtd->priv;
  760. char *readp = this->page_buf;
  761. int column = to & (mtd->oobsize - 1);
  762. int status, i;
  763. this->command(mtd, ONENAND_CMD_READOOB, to, mtd->oobsize);
  764. onenand_update_bufferram(mtd, to, 0);
  765. status = this->wait(mtd, FL_READING);
  766. if (status)
  767. return status;
  768. this->read_bufferram(mtd, ONENAND_SPARERAM, readp, column, len);
  769. for(i = 0; i < len; i++)
  770. if (buf[i] != 0xFF && buf[i] != readp[i])
  771. return -EBADMSG;
  772. return 0;
  773. }
  774. /**
  775. * onenand_verify_page - [GENERIC] verify the chip contents after a write
  776. * @param mtd MTD device structure
  777. * @param buf the databuffer to verify
  778. *
  779. * Check DataRAM area directly
  780. */
  781. static int onenand_verify_page(struct mtd_info *mtd, u_char *buf, loff_t addr)
  782. {
  783. struct onenand_chip *this = mtd->priv;
  784. void __iomem *dataram0, *dataram1;
  785. int ret = 0;
  786. /* In partial page write, just skip it */
  787. if ((addr & (mtd->writesize - 1)) != 0)
  788. return 0;
  789. this->command(mtd, ONENAND_CMD_READ, addr, mtd->writesize);
  790. ret = this->wait(mtd, FL_READING);
  791. if (ret)
  792. return ret;
  793. onenand_update_bufferram(mtd, addr, 1);
  794. /* Check, if the two dataram areas are same */
  795. dataram0 = this->base + ONENAND_DATARAM;
  796. dataram1 = dataram0 + mtd->writesize;
  797. if (memcmp(dataram0, dataram1, mtd->writesize))
  798. return -EBADMSG;
  799. return 0;
  800. }
  801. #else
  802. #define onenand_verify_page(...) (0)
  803. #define onenand_verify_oob(...) (0)
  804. #endif
  805. #define NOTALIGNED(x) ((x & (this->subpagesize - 1)) != 0)
  806. /**
  807. * onenand_write - [MTD Interface] write buffer to FLASH
  808. * @param mtd MTD device structure
  809. * @param to offset to write to
  810. * @param len number of bytes to write
  811. * @param retlen pointer to variable to store the number of written bytes
  812. * @param buf the data to write
  813. *
  814. * Write with ECC
  815. */
  816. static int onenand_write(struct mtd_info *mtd, loff_t to, size_t len,
  817. size_t *retlen, const u_char *buf)
  818. {
  819. struct onenand_chip *this = mtd->priv;
  820. int written = 0;
  821. int ret = 0;
  822. int column, subpage;
  823. DEBUG(MTD_DEBUG_LEVEL3, "onenand_write: to = 0x%08x, len = %i\n", (unsigned int) to, (int) len);
  824. /* Initialize retlen, in case of early exit */
  825. *retlen = 0;
  826. /* Do not allow writes past end of device */
  827. if (unlikely((to + len) > mtd->size)) {
  828. DEBUG(MTD_DEBUG_LEVEL0, "onenand_write: Attempt write to past end of device\n");
  829. return -EINVAL;
  830. }
  831. /* Reject writes, which are not page aligned */
  832. if (unlikely(NOTALIGNED(to)) || unlikely(NOTALIGNED(len))) {
  833. DEBUG(MTD_DEBUG_LEVEL0, "onenand_write: Attempt to write not page aligned data\n");
  834. return -EINVAL;
  835. }
  836. column = to & (mtd->writesize - 1);
  837. subpage = column || (len & (mtd->writesize - 1));
  838. /* Grab the lock and see if the device is available */
  839. onenand_get_device(mtd, FL_WRITING);
  840. /* Loop until all data write */
  841. while (written < len) {
  842. int bytes = mtd->writesize;
  843. int thislen = min_t(int, bytes, len - written);
  844. u_char *wbuf = (u_char *) buf;
  845. cond_resched();
  846. this->command(mtd, ONENAND_CMD_BUFFERRAM, to, bytes);
  847. /* Partial page write */
  848. if (subpage) {
  849. bytes = min_t(int, bytes - column, (int) len);
  850. memset(this->page_buf, 0xff, mtd->writesize);
  851. memcpy(this->page_buf + column, buf, bytes);
  852. wbuf = this->page_buf;
  853. /* Even though partial write, we need page size */
  854. thislen = mtd->writesize;
  855. }
  856. this->write_bufferram(mtd, ONENAND_DATARAM, wbuf, 0, thislen);
  857. this->write_bufferram(mtd, ONENAND_SPARERAM, ffchars, 0, mtd->oobsize);
  858. this->command(mtd, ONENAND_CMD_PROG, to, mtd->writesize);
  859. /* In partial page write we don't update bufferram */
  860. onenand_update_bufferram(mtd, to, !subpage);
  861. ret = this->wait(mtd, FL_WRITING);
  862. if (ret) {
  863. DEBUG(MTD_DEBUG_LEVEL0, "onenand_write: write filaed %d\n", ret);
  864. break;
  865. }
  866. /* Only check verify write turn on */
  867. ret = onenand_verify_page(mtd, (u_char *) wbuf, to);
  868. if (ret) {
  869. DEBUG(MTD_DEBUG_LEVEL0, "onenand_write: verify failed %d\n", ret);
  870. break;
  871. }
  872. written += thislen;
  873. if (written == len)
  874. break;
  875. column = 0;
  876. to += thislen;
  877. buf += thislen;
  878. }
  879. /* Deselect and wake up anyone waiting on the device */
  880. onenand_release_device(mtd);
  881. *retlen = written;
  882. return ret;
  883. }
  884. /**
  885. * onenand_do_write_oob - [Internal] OneNAND write out-of-band
  886. * @param mtd MTD device structure
  887. * @param to offset to write to
  888. * @param len number of bytes to write
  889. * @param retlen pointer to variable to store the number of written bytes
  890. * @param buf the data to write
  891. *
  892. * OneNAND write out-of-band
  893. */
  894. static int onenand_do_write_oob(struct mtd_info *mtd, loff_t to, size_t len,
  895. size_t *retlen, const u_char *buf)
  896. {
  897. struct onenand_chip *this = mtd->priv;
  898. int column, ret = 0;
  899. int written = 0;
  900. DEBUG(MTD_DEBUG_LEVEL3, "onenand_write_oob: to = 0x%08x, len = %i\n", (unsigned int) to, (int) len);
  901. /* Initialize retlen, in case of early exit */
  902. *retlen = 0;
  903. /* Do not allow writes past end of device */
  904. if (unlikely((to + len) > mtd->size)) {
  905. DEBUG(MTD_DEBUG_LEVEL0, "onenand_write_oob: Attempt write to past end of device\n");
  906. return -EINVAL;
  907. }
  908. /* Grab the lock and see if the device is available */
  909. onenand_get_device(mtd, FL_WRITING);
  910. /* Loop until all data write */
  911. while (written < len) {
  912. int thislen = min_t(int, mtd->oobsize, len - written);
  913. cond_resched();
  914. column = to & (mtd->oobsize - 1);
  915. this->command(mtd, ONENAND_CMD_BUFFERRAM, to, mtd->oobsize);
  916. /* We send data to spare ram with oobsize
  917. * to prevent byte access */
  918. memset(this->page_buf, 0xff, mtd->oobsize);
  919. memcpy(this->page_buf + column, buf, thislen);
  920. this->write_bufferram(mtd, ONENAND_SPARERAM, this->page_buf, 0, mtd->oobsize);
  921. this->command(mtd, ONENAND_CMD_PROGOOB, to, mtd->oobsize);
  922. onenand_update_bufferram(mtd, to, 0);
  923. ret = this->wait(mtd, FL_WRITING);
  924. if (ret) {
  925. DEBUG(MTD_DEBUG_LEVEL0, "onenand_write_oob: write filaed %d\n", ret);
  926. goto out;
  927. }
  928. ret = onenand_verify_oob(mtd, buf, to, thislen);
  929. if (ret) {
  930. DEBUG(MTD_DEBUG_LEVEL0, "onenand_write_oob: verify failed %d\n", ret);
  931. goto out;
  932. }
  933. written += thislen;
  934. if (written == len)
  935. break;
  936. to += thislen;
  937. buf += thislen;
  938. }
  939. out:
  940. /* Deselect and wake up anyone waiting on the device */
  941. onenand_release_device(mtd);
  942. *retlen = written;
  943. return ret;
  944. }
  945. /**
  946. * onenand_write_oob - [MTD Interface] NAND write data and/or out-of-band
  947. * @mtd: MTD device structure
  948. * @from: offset to read from
  949. * @ops: oob operation description structure
  950. */
  951. static int onenand_write_oob(struct mtd_info *mtd, loff_t to,
  952. struct mtd_oob_ops *ops)
  953. {
  954. BUG_ON(ops->mode != MTD_OOB_PLACE);
  955. return onenand_do_write_oob(mtd, to + ops->ooboffs, ops->ooblen,
  956. &ops->oobretlen, ops->oobbuf);
  957. }
  958. /**
  959. * onenand_block_checkbad - [GENERIC] Check if a block is marked bad
  960. * @param mtd MTD device structure
  961. * @param ofs offset from device start
  962. * @param getchip 0, if the chip is already selected
  963. * @param allowbbt 1, if its allowed to access the bbt area
  964. *
  965. * Check, if the block is bad. Either by reading the bad block table or
  966. * calling of the scan function.
  967. */
  968. static int onenand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int getchip, int allowbbt)
  969. {
  970. struct onenand_chip *this = mtd->priv;
  971. struct bbm_info *bbm = this->bbm;
  972. /* Return info from the table */
  973. return bbm->isbad_bbt(mtd, ofs, allowbbt);
  974. }
  975. /**
  976. * onenand_erase - [MTD Interface] erase block(s)
  977. * @param mtd MTD device structure
  978. * @param instr erase instruction
  979. *
  980. * Erase one ore more blocks
  981. */
  982. static int onenand_erase(struct mtd_info *mtd, struct erase_info *instr)
  983. {
  984. struct onenand_chip *this = mtd->priv;
  985. unsigned int block_size;
  986. loff_t addr;
  987. int len;
  988. int ret = 0;
  989. DEBUG(MTD_DEBUG_LEVEL3, "onenand_erase: start = 0x%08x, len = %i\n", (unsigned int) instr->addr, (unsigned int) instr->len);
  990. block_size = (1 << this->erase_shift);
  991. /* Start address must align on block boundary */
  992. if (unlikely(instr->addr & (block_size - 1))) {
  993. DEBUG(MTD_DEBUG_LEVEL0, "onenand_erase: Unaligned address\n");
  994. return -EINVAL;
  995. }
  996. /* Length must align on block boundary */
  997. if (unlikely(instr->len & (block_size - 1))) {
  998. DEBUG(MTD_DEBUG_LEVEL0, "onenand_erase: Length not block aligned\n");
  999. return -EINVAL;
  1000. }
  1001. /* Do not allow erase past end of device */
  1002. if (unlikely((instr->len + instr->addr) > mtd->size)) {
  1003. DEBUG(MTD_DEBUG_LEVEL0, "onenand_erase: Erase past end of device\n");
  1004. return -EINVAL;
  1005. }
  1006. instr->fail_addr = 0xffffffff;
  1007. /* Grab the lock and see if the device is available */
  1008. onenand_get_device(mtd, FL_ERASING);
  1009. /* Loop throught the pages */
  1010. len = instr->len;
  1011. addr = instr->addr;
  1012. instr->state = MTD_ERASING;
  1013. while (len) {
  1014. cond_resched();
  1015. /* Check if we have a bad block, we do not erase bad blocks */
  1016. if (onenand_block_checkbad(mtd, addr, 0, 0)) {
  1017. printk (KERN_WARNING "onenand_erase: attempt to erase a bad block at addr 0x%08x\n", (unsigned int) addr);
  1018. instr->state = MTD_ERASE_FAILED;
  1019. goto erase_exit;
  1020. }
  1021. this->command(mtd, ONENAND_CMD_ERASE, addr, block_size);
  1022. ret = this->wait(mtd, FL_ERASING);
  1023. /* Check, if it is write protected */
  1024. if (ret) {
  1025. DEBUG(MTD_DEBUG_LEVEL0, "onenand_erase: Failed erase, block %d\n", (unsigned) (addr >> this->erase_shift));
  1026. instr->state = MTD_ERASE_FAILED;
  1027. instr->fail_addr = addr;
  1028. goto erase_exit;
  1029. }
  1030. len -= block_size;
  1031. addr += block_size;
  1032. }
  1033. instr->state = MTD_ERASE_DONE;
  1034. erase_exit:
  1035. ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
  1036. /* Do call back function */
  1037. if (!ret)
  1038. mtd_erase_callback(instr);
  1039. /* Deselect and wake up anyone waiting on the device */
  1040. onenand_release_device(mtd);
  1041. return ret;
  1042. }
  1043. /**
  1044. * onenand_sync - [MTD Interface] sync
  1045. * @param mtd MTD device structure
  1046. *
  1047. * Sync is actually a wait for chip ready function
  1048. */
  1049. static void onenand_sync(struct mtd_info *mtd)
  1050. {
  1051. DEBUG(MTD_DEBUG_LEVEL3, "onenand_sync: called\n");
  1052. /* Grab the lock and see if the device is available */
  1053. onenand_get_device(mtd, FL_SYNCING);
  1054. /* Release it and go back */
  1055. onenand_release_device(mtd);
  1056. }
  1057. /**
  1058. * onenand_block_isbad - [MTD Interface] Check whether the block at the given offset is bad
  1059. * @param mtd MTD device structure
  1060. * @param ofs offset relative to mtd start
  1061. *
  1062. * Check whether the block is bad
  1063. */
  1064. static int onenand_block_isbad(struct mtd_info *mtd, loff_t ofs)
  1065. {
  1066. /* Check for invalid offset */
  1067. if (ofs > mtd->size)
  1068. return -EINVAL;
  1069. return onenand_block_checkbad(mtd, ofs, 1, 0);
  1070. }
  1071. /**
  1072. * onenand_default_block_markbad - [DEFAULT] mark a block bad
  1073. * @param mtd MTD device structure
  1074. * @param ofs offset from device start
  1075. *
  1076. * This is the default implementation, which can be overridden by
  1077. * a hardware specific driver.
  1078. */
  1079. static int onenand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
  1080. {
  1081. struct onenand_chip *this = mtd->priv;
  1082. struct bbm_info *bbm = this->bbm;
  1083. u_char buf[2] = {0, 0};
  1084. size_t retlen;
  1085. int block;
  1086. /* Get block number */
  1087. block = ((int) ofs) >> bbm->bbt_erase_shift;
  1088. if (bbm->bbt)
  1089. bbm->bbt[block >> 2] |= 0x01 << ((block & 0x03) << 1);
  1090. /* We write two bytes, so we dont have to mess with 16 bit access */
  1091. ofs += mtd->oobsize + (bbm->badblockpos & ~0x01);
  1092. return onenand_do_write_oob(mtd, ofs , 2, &retlen, buf);
  1093. }
  1094. /**
  1095. * onenand_block_markbad - [MTD Interface] Mark the block at the given offset as bad
  1096. * @param mtd MTD device structure
  1097. * @param ofs offset relative to mtd start
  1098. *
  1099. * Mark the block as bad
  1100. */
  1101. static int onenand_block_markbad(struct mtd_info *mtd, loff_t ofs)
  1102. {
  1103. struct onenand_chip *this = mtd->priv;
  1104. int ret;
  1105. ret = onenand_block_isbad(mtd, ofs);
  1106. if (ret) {
  1107. /* If it was bad already, return success and do nothing */
  1108. if (ret > 0)
  1109. return 0;
  1110. return ret;
  1111. }
  1112. return this->block_markbad(mtd, ofs);
  1113. }
  1114. /**
  1115. * onenand_do_lock_cmd - [OneNAND Interface] Lock or unlock block(s)
  1116. * @param mtd MTD device structure
  1117. * @param ofs offset relative to mtd start
  1118. * @param len number of bytes to lock or unlock
  1119. *
  1120. * Lock or unlock one or more blocks
  1121. */
  1122. static int onenand_do_lock_cmd(struct mtd_info *mtd, loff_t ofs, size_t len, int cmd)
  1123. {
  1124. struct onenand_chip *this = mtd->priv;
  1125. int start, end, block, value, status;
  1126. int wp_status_mask;
  1127. start = ofs >> this->erase_shift;
  1128. end = len >> this->erase_shift;
  1129. if (cmd == ONENAND_CMD_LOCK)
  1130. wp_status_mask = ONENAND_WP_LS;
  1131. else
  1132. wp_status_mask = ONENAND_WP_US;
  1133. /* Continuous lock scheme */
  1134. if (this->options & ONENAND_HAS_CONT_LOCK) {
  1135. /* Set start block address */
  1136. this->write_word(start, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
  1137. /* Set end block address */
  1138. this->write_word(start + end - 1, this->base + ONENAND_REG_END_BLOCK_ADDRESS);
  1139. /* Write lock command */
  1140. this->command(mtd, cmd, 0, 0);
  1141. /* There's no return value */
  1142. this->wait(mtd, FL_LOCKING);
  1143. /* Sanity check */
  1144. while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
  1145. & ONENAND_CTRL_ONGO)
  1146. continue;
  1147. /* Check lock status */
  1148. status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
  1149. if (!(status & wp_status_mask))
  1150. printk(KERN_ERR "wp status = 0x%x\n", status);
  1151. return 0;
  1152. }
  1153. /* Block lock scheme */
  1154. for (block = start; block < start + end; block++) {
  1155. /* Set block address */
  1156. value = onenand_block_address(this, block);
  1157. this->write_word(value, this->base + ONENAND_REG_START_ADDRESS1);
  1158. /* Select DataRAM for DDP */
  1159. value = onenand_bufferram_address(this, block);
  1160. this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
  1161. /* Set start block address */
  1162. this->write_word(block, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
  1163. /* Write lock command */
  1164. this->command(mtd, cmd, 0, 0);
  1165. /* There's no return value */
  1166. this->wait(mtd, FL_LOCKING);
  1167. /* Sanity check */
  1168. while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
  1169. & ONENAND_CTRL_ONGO)
  1170. continue;
  1171. /* Check lock status */
  1172. status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
  1173. if (!(status & wp_status_mask))
  1174. printk(KERN_ERR "block = %d, wp status = 0x%x\n", block, status);
  1175. }
  1176. return 0;
  1177. }
  1178. /**
  1179. * onenand_lock - [MTD Interface] Lock block(s)
  1180. * @param mtd MTD device structure
  1181. * @param ofs offset relative to mtd start
  1182. * @param len number of bytes to unlock
  1183. *
  1184. * Lock one or more blocks
  1185. */
  1186. static int onenand_lock(struct mtd_info *mtd, loff_t ofs, size_t len)
  1187. {
  1188. return onenand_do_lock_cmd(mtd, ofs, len, ONENAND_CMD_LOCK);
  1189. }
  1190. /**
  1191. * onenand_unlock - [MTD Interface] Unlock block(s)
  1192. * @param mtd MTD device structure
  1193. * @param ofs offset relative to mtd start
  1194. * @param len number of bytes to unlock
  1195. *
  1196. * Unlock one or more blocks
  1197. */
  1198. static int onenand_unlock(struct mtd_info *mtd, loff_t ofs, size_t len)
  1199. {
  1200. return onenand_do_lock_cmd(mtd, ofs, len, ONENAND_CMD_UNLOCK);
  1201. }
  1202. /**
  1203. * onenand_check_lock_status - [OneNAND Interface] Check lock status
  1204. * @param this onenand chip data structure
  1205. *
  1206. * Check lock status
  1207. */
  1208. static void onenand_check_lock_status(struct onenand_chip *this)
  1209. {
  1210. unsigned int value, block, status;
  1211. unsigned int end;
  1212. end = this->chipsize >> this->erase_shift;
  1213. for (block = 0; block < end; block++) {
  1214. /* Set block address */
  1215. value = onenand_block_address(this, block);
  1216. this->write_word(value, this->base + ONENAND_REG_START_ADDRESS1);
  1217. /* Select DataRAM for DDP */
  1218. value = onenand_bufferram_address(this, block);
  1219. this->write_word(value, this->base + ONENAND_REG_START_ADDRESS2);
  1220. /* Set start block address */
  1221. this->write_word(block, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
  1222. /* Check lock status */
  1223. status = this->read_word(this->base + ONENAND_REG_WP_STATUS);
  1224. if (!(status & ONENAND_WP_US))
  1225. printk(KERN_ERR "block = %d, wp status = 0x%x\n", block, status);
  1226. }
  1227. }
  1228. /**
  1229. * onenand_unlock_all - [OneNAND Interface] unlock all blocks
  1230. * @param mtd MTD device structure
  1231. *
  1232. * Unlock all blocks
  1233. */
  1234. static int onenand_unlock_all(struct mtd_info *mtd)
  1235. {
  1236. struct onenand_chip *this = mtd->priv;
  1237. if (this->options & ONENAND_HAS_UNLOCK_ALL) {
  1238. /* Set start block address */
  1239. this->write_word(0, this->base + ONENAND_REG_START_BLOCK_ADDRESS);
  1240. /* Write unlock command */
  1241. this->command(mtd, ONENAND_CMD_UNLOCK_ALL, 0, 0);
  1242. /* There's no return value */
  1243. this->wait(mtd, FL_LOCKING);
  1244. /* Sanity check */
  1245. while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)
  1246. & ONENAND_CTRL_ONGO)
  1247. continue;
  1248. /* Workaround for all block unlock in DDP */
  1249. if (this->device_id & ONENAND_DEVICE_IS_DDP) {
  1250. /* 1st block on another chip */
  1251. loff_t ofs = this->chipsize >> 1;
  1252. size_t len = mtd->erasesize;
  1253. onenand_unlock(mtd, ofs, len);
  1254. }
  1255. onenand_check_lock_status(this);
  1256. return 0;
  1257. }
  1258. onenand_unlock(mtd, 0x0, this->chipsize);
  1259. return 0;
  1260. }
  1261. #ifdef CONFIG_MTD_ONENAND_OTP
  1262. /* Interal OTP operation */
  1263. typedef int (*otp_op_t)(struct mtd_info *mtd, loff_t form, size_t len,
  1264. size_t *retlen, u_char *buf);
  1265. /**
  1266. * do_otp_read - [DEFAULT] Read OTP block area
  1267. * @param mtd MTD device structure
  1268. * @param from The offset to read
  1269. * @param len number of bytes to read
  1270. * @param retlen pointer to variable to store the number of readbytes
  1271. * @param buf the databuffer to put/get data
  1272. *
  1273. * Read OTP block area.
  1274. */
  1275. static int do_otp_read(struct mtd_info *mtd, loff_t from, size_t len,
  1276. size_t *retlen, u_char *buf)
  1277. {
  1278. struct onenand_chip *this = mtd->priv;
  1279. int ret;
  1280. /* Enter OTP access mode */
  1281. this->command(mtd, ONENAND_CMD_OTP_ACCESS, 0, 0);
  1282. this->wait(mtd, FL_OTPING);
  1283. ret = mtd->read(mtd, from, len, retlen, buf);
  1284. /* Exit OTP access mode */
  1285. this->command(mtd, ONENAND_CMD_RESET, 0, 0);
  1286. this->wait(mtd, FL_RESETING);
  1287. return ret;
  1288. }
  1289. /**
  1290. * do_otp_write - [DEFAULT] Write OTP block area
  1291. * @param mtd MTD device structure
  1292. * @param from The offset to write
  1293. * @param len number of bytes to write
  1294. * @param retlen pointer to variable to store the number of write bytes
  1295. * @param buf the databuffer to put/get data
  1296. *
  1297. * Write OTP block area.
  1298. */
  1299. static int do_otp_write(struct mtd_info *mtd, loff_t from, size_t len,
  1300. size_t *retlen, u_char *buf)
  1301. {
  1302. struct onenand_chip *this = mtd->priv;
  1303. unsigned char *pbuf = buf;
  1304. int ret;
  1305. /* Force buffer page aligned */
  1306. if (len < mtd->writesize) {
  1307. memcpy(this->page_buf, buf, len);
  1308. memset(this->page_buf + len, 0xff, mtd->writesize - len);
  1309. pbuf = this->page_buf;
  1310. len = mtd->writesize;
  1311. }
  1312. /* Enter OTP access mode */
  1313. this->command(mtd, ONENAND_CMD_OTP_ACCESS, 0, 0);
  1314. this->wait(mtd, FL_OTPING);
  1315. ret = mtd->write(mtd, from, len, retlen, pbuf);
  1316. /* Exit OTP access mode */
  1317. this->command(mtd, ONENAND_CMD_RESET, 0, 0);
  1318. this->wait(mtd, FL_RESETING);
  1319. return ret;
  1320. }
  1321. /**
  1322. * do_otp_lock - [DEFAULT] Lock OTP block area
  1323. * @param mtd MTD device structure
  1324. * @param from The offset to lock
  1325. * @param len number of bytes to lock
  1326. * @param retlen pointer to variable to store the number of lock bytes
  1327. * @param buf the databuffer to put/get data
  1328. *
  1329. * Lock OTP block area.
  1330. */
  1331. static int do_otp_lock(struct mtd_info *mtd, loff_t from, size_t len,
  1332. size_t *retlen, u_char *buf)
  1333. {
  1334. struct onenand_chip *this = mtd->priv;
  1335. int ret;
  1336. /* Enter OTP access mode */
  1337. this->command(mtd, ONENAND_CMD_OTP_ACCESS, 0, 0);
  1338. this->wait(mtd, FL_OTPING);
  1339. ret = onenand_do_write_oob(mtd, from, len, retlen, buf);
  1340. /* Exit OTP access mode */
  1341. this->command(mtd, ONENAND_CMD_RESET, 0, 0);
  1342. this->wait(mtd, FL_RESETING);
  1343. return ret;
  1344. }
  1345. /**
  1346. * onenand_otp_walk - [DEFAULT] Handle OTP operation
  1347. * @param mtd MTD device structure
  1348. * @param from The offset to read/write
  1349. * @param len number of bytes to read/write
  1350. * @param retlen pointer to variable to store the number of read bytes
  1351. * @param buf the databuffer to put/get data
  1352. * @param action do given action
  1353. * @param mode specify user and factory
  1354. *
  1355. * Handle OTP operation.
  1356. */
  1357. static int onenand_otp_walk(struct mtd_info *mtd, loff_t from, size_t len,
  1358. size_t *retlen, u_char *buf,
  1359. otp_op_t action, int mode)
  1360. {
  1361. struct onenand_chip *this = mtd->priv;
  1362. int otp_pages;
  1363. int density;
  1364. int ret = 0;
  1365. *retlen = 0;
  1366. density = this->device_id >> ONENAND_DEVICE_DENSITY_SHIFT;
  1367. if (density < ONENAND_DEVICE_DENSITY_512Mb)
  1368. otp_pages = 20;
  1369. else
  1370. otp_pages = 10;
  1371. if (mode == MTD_OTP_FACTORY) {
  1372. from += mtd->writesize * otp_pages;
  1373. otp_pages = 64 - otp_pages;
  1374. }
  1375. /* Check User/Factory boundary */
  1376. if (((mtd->writesize * otp_pages) - (from + len)) < 0)
  1377. return 0;
  1378. while (len > 0 && otp_pages > 0) {
  1379. if (!action) { /* OTP Info functions */
  1380. struct otp_info *otpinfo;
  1381. len -= sizeof(struct otp_info);
  1382. if (len <= 0)
  1383. return -ENOSPC;
  1384. otpinfo = (struct otp_info *) buf;
  1385. otpinfo->start = from;
  1386. otpinfo->length = mtd->writesize;
  1387. otpinfo->locked = 0;
  1388. from += mtd->writesize;
  1389. buf += sizeof(struct otp_info);
  1390. *retlen += sizeof(struct otp_info);
  1391. } else {
  1392. size_t tmp_retlen;
  1393. int size = len;
  1394. ret = action(mtd, from, len, &tmp_retlen, buf);
  1395. buf += size;
  1396. len -= size;
  1397. *retlen += size;
  1398. if (ret < 0)
  1399. return ret;
  1400. }
  1401. otp_pages--;
  1402. }
  1403. return 0;
  1404. }
  1405. /**
  1406. * onenand_get_fact_prot_info - [MTD Interface] Read factory OTP info
  1407. * @param mtd MTD device structure
  1408. * @param buf the databuffer to put/get data
  1409. * @param len number of bytes to read
  1410. *
  1411. * Read factory OTP info.
  1412. */
  1413. static int onenand_get_fact_prot_info(struct mtd_info *mtd,
  1414. struct otp_info *buf, size_t len)
  1415. {
  1416. size_t retlen;
  1417. int ret;
  1418. ret = onenand_otp_walk(mtd, 0, len, &retlen, (u_char *) buf, NULL, MTD_OTP_FACTORY);
  1419. return ret ? : retlen;
  1420. }
  1421. /**
  1422. * onenand_read_fact_prot_reg - [MTD Interface] Read factory OTP area
  1423. * @param mtd MTD device structure
  1424. * @param from The offset to read
  1425. * @param len number of bytes to read
  1426. * @param retlen pointer to variable to store the number of read bytes
  1427. * @param buf the databuffer to put/get data
  1428. *
  1429. * Read factory OTP area.
  1430. */
  1431. static int onenand_read_fact_prot_reg(struct mtd_info *mtd, loff_t from,
  1432. size_t len, size_t *retlen, u_char *buf)
  1433. {
  1434. return onenand_otp_walk(mtd, from, len, retlen, buf, do_otp_read, MTD_OTP_FACTORY);
  1435. }
  1436. /**
  1437. * onenand_get_user_prot_info - [MTD Interface] Read user OTP info
  1438. * @param mtd MTD device structure
  1439. * @param buf the databuffer to put/get data
  1440. * @param len number of bytes to read
  1441. *
  1442. * Read user OTP info.
  1443. */
  1444. static int onenand_get_user_prot_info(struct mtd_info *mtd,
  1445. struct otp_info *buf, size_t len)
  1446. {
  1447. size_t retlen;
  1448. int ret;
  1449. ret = onenand_otp_walk(mtd, 0, len, &retlen, (u_char *) buf, NULL, MTD_OTP_USER);
  1450. return ret ? : retlen;
  1451. }
  1452. /**
  1453. * onenand_read_user_prot_reg - [MTD Interface] Read user OTP area
  1454. * @param mtd MTD device structure
  1455. * @param from The offset to read
  1456. * @param len number of bytes to read
  1457. * @param retlen pointer to variable to store the number of read bytes
  1458. * @param buf the databuffer to put/get data
  1459. *
  1460. * Read user OTP area.
  1461. */
  1462. static int onenand_read_user_prot_reg(struct mtd_info *mtd, loff_t from,
  1463. size_t len, size_t *retlen, u_char *buf)
  1464. {
  1465. return onenand_otp_walk(mtd, from, len, retlen, buf, do_otp_read, MTD_OTP_USER);
  1466. }
  1467. /**
  1468. * onenand_write_user_prot_reg - [MTD Interface] Write user OTP area
  1469. * @param mtd MTD device structure
  1470. * @param from The offset to write
  1471. * @param len number of bytes to write
  1472. * @param retlen pointer to variable to store the number of write bytes
  1473. * @param buf the databuffer to put/get data
  1474. *
  1475. * Write user OTP area.
  1476. */
  1477. static int onenand_write_user_prot_reg(struct mtd_info *mtd, loff_t from,
  1478. size_t len, size_t *retlen, u_char *buf)
  1479. {
  1480. return onenand_otp_walk(mtd, from, len, retlen, buf, do_otp_write, MTD_OTP_USER);
  1481. }
  1482. /**
  1483. * onenand_lock_user_prot_reg - [MTD Interface] Lock user OTP area
  1484. * @param mtd MTD device structure
  1485. * @param from The offset to lock
  1486. * @param len number of bytes to unlock
  1487. *
  1488. * Write lock mark on spare area in page 0 in OTP block
  1489. */
  1490. static int onenand_lock_user_prot_reg(struct mtd_info *mtd, loff_t from,
  1491. size_t len)
  1492. {
  1493. unsigned char oob_buf[64];
  1494. size_t retlen;
  1495. int ret;
  1496. memset(oob_buf, 0xff, mtd->oobsize);
  1497. /*
  1498. * Note: OTP lock operation
  1499. * OTP block : 0xXXFC
  1500. * 1st block : 0xXXF3 (If chip support)
  1501. * Both : 0xXXF0 (If chip support)
  1502. */
  1503. oob_buf[ONENAND_OTP_LOCK_OFFSET] = 0xFC;
  1504. /*
  1505. * Write lock mark to 8th word of sector0 of page0 of the spare0.
  1506. * We write 16 bytes spare area instead of 2 bytes.
  1507. */
  1508. from = 0;
  1509. len = 16;
  1510. ret = onenand_otp_walk(mtd, from, len, &retlen, oob_buf, do_otp_lock, MTD_OTP_USER);
  1511. return ret ? : retlen;
  1512. }
  1513. #endif /* CONFIG_MTD_ONENAND_OTP */
  1514. /**
  1515. * onenand_lock_scheme - Check and set OneNAND lock scheme
  1516. * @param mtd MTD data structure
  1517. *
  1518. * Check and set OneNAND lock scheme
  1519. */
  1520. static void onenand_lock_scheme(struct mtd_info *mtd)
  1521. {
  1522. struct onenand_chip *this = mtd->priv;
  1523. unsigned int density, process;
  1524. /* Lock scheme depends on density and process */
  1525. density = this->device_id >> ONENAND_DEVICE_DENSITY_SHIFT;
  1526. process = this->version_id >> ONENAND_VERSION_PROCESS_SHIFT;
  1527. /* Lock scheme */
  1528. if (density >= ONENAND_DEVICE_DENSITY_1Gb) {
  1529. /* A-Die has all block unlock */
  1530. if (process) {
  1531. printk(KERN_DEBUG "Chip support all block unlock\n");
  1532. this->options |= ONENAND_HAS_UNLOCK_ALL;
  1533. }
  1534. } else {
  1535. /* Some OneNAND has continues lock scheme */
  1536. if (!process) {
  1537. printk(KERN_DEBUG "Lock scheme is Continues Lock\n");
  1538. this->options |= ONENAND_HAS_CONT_LOCK;
  1539. }
  1540. }
  1541. }
  1542. /**
  1543. * onenand_print_device_info - Print device ID
  1544. * @param device device ID
  1545. *
  1546. * Print device ID
  1547. */
  1548. static void onenand_print_device_info(int device, int version)
  1549. {
  1550. int vcc, demuxed, ddp, density;
  1551. vcc = device & ONENAND_DEVICE_VCC_MASK;
  1552. demuxed = device & ONENAND_DEVICE_IS_DEMUX;
  1553. ddp = device & ONENAND_DEVICE_IS_DDP;
  1554. density = device >> ONENAND_DEVICE_DENSITY_SHIFT;
  1555. printk(KERN_INFO "%sOneNAND%s %dMB %sV 16-bit (0x%02x)\n",
  1556. demuxed ? "" : "Muxed ",
  1557. ddp ? "(DDP)" : "",
  1558. (16 << density),
  1559. vcc ? "2.65/3.3" : "1.8",
  1560. device);
  1561. printk(KERN_DEBUG "OneNAND version = 0x%04x\n", version);
  1562. }
  1563. static const struct onenand_manufacturers onenand_manuf_ids[] = {
  1564. {ONENAND_MFR_SAMSUNG, "Samsung"},
  1565. };
  1566. /**
  1567. * onenand_check_maf - Check manufacturer ID
  1568. * @param manuf manufacturer ID
  1569. *
  1570. * Check manufacturer ID
  1571. */
  1572. static int onenand_check_maf(int manuf)
  1573. {
  1574. int size = ARRAY_SIZE(onenand_manuf_ids);
  1575. char *name;
  1576. int i;
  1577. for (i = 0; i < size; i++)
  1578. if (manuf == onenand_manuf_ids[i].id)
  1579. break;
  1580. if (i < size)
  1581. name = onenand_manuf_ids[i].name;
  1582. else
  1583. name = "Unknown";
  1584. printk(KERN_DEBUG "OneNAND Manufacturer: %s (0x%0x)\n", name, manuf);
  1585. return (i == size);
  1586. }
  1587. /**
  1588. * onenand_probe - [OneNAND Interface] Probe the OneNAND device
  1589. * @param mtd MTD device structure
  1590. *
  1591. * OneNAND detection method:
  1592. * Compare the the values from command with ones from register
  1593. */
  1594. static int onenand_probe(struct mtd_info *mtd)
  1595. {
  1596. struct onenand_chip *this = mtd->priv;
  1597. int bram_maf_id, bram_dev_id, maf_id, dev_id, ver_id;
  1598. int density;
  1599. int syscfg;
  1600. /* Save system configuration 1 */
  1601. syscfg = this->read_word(this->base + ONENAND_REG_SYS_CFG1);
  1602. /* Clear Sync. Burst Read mode to read BootRAM */
  1603. this->write_word((syscfg & ~ONENAND_SYS_CFG1_SYNC_READ), this->base + ONENAND_REG_SYS_CFG1);
  1604. /* Send the command for reading device ID from BootRAM */
  1605. this->write_word(ONENAND_CMD_READID, this->base + ONENAND_BOOTRAM);
  1606. /* Read manufacturer and device IDs from BootRAM */
  1607. bram_maf_id = this->read_word(this->base + ONENAND_BOOTRAM + 0x0);
  1608. bram_dev_id = this->read_word(this->base + ONENAND_BOOTRAM + 0x2);
  1609. /* Reset OneNAND to read default register values */
  1610. this->write_word(ONENAND_CMD_RESET, this->base + ONENAND_BOOTRAM);
  1611. /* Wait reset */
  1612. this->wait(mtd, FL_RESETING);
  1613. /* Restore system configuration 1 */
  1614. this->write_word(syscfg, this->base + ONENAND_REG_SYS_CFG1);
  1615. /* Check manufacturer ID */
  1616. if (onenand_check_maf(bram_maf_id))
  1617. return -ENXIO;
  1618. /* Read manufacturer and device IDs from Register */
  1619. maf_id = this->read_word(this->base + ONENAND_REG_MANUFACTURER_ID);
  1620. dev_id = this->read_word(this->base + ONENAND_REG_DEVICE_ID);
  1621. ver_id = this->read_word(this->base + ONENAND_REG_VERSION_ID);
  1622. /* Check OneNAND device */
  1623. if (maf_id != bram_maf_id || dev_id != bram_dev_id)
  1624. return -ENXIO;
  1625. /* Flash device information */
  1626. onenand_print_device_info(dev_id, ver_id);
  1627. this->device_id = dev_id;
  1628. this->version_id = ver_id;
  1629. density = dev_id >> ONENAND_DEVICE_DENSITY_SHIFT;
  1630. this->chipsize = (16 << density) << 20;
  1631. /* Set density mask. it is used for DDP */
  1632. this->density_mask = (1 << (density + 6));
  1633. /* OneNAND page size & block size */
  1634. /* The data buffer size is equal to page size */
  1635. mtd->writesize = this->read_word(this->base + ONENAND_REG_DATA_BUFFER_SIZE);
  1636. mtd->oobsize = mtd->writesize >> 5;
  1637. /* Pagers per block is always 64 in OneNAND */
  1638. mtd->erasesize = mtd->writesize << 6;
  1639. this->erase_shift = ffs(mtd->erasesize) - 1;
  1640. this->page_shift = ffs(mtd->writesize) - 1;
  1641. this->ppb_shift = (this->erase_shift - this->page_shift);
  1642. this->page_mask = (mtd->erasesize / mtd->writesize) - 1;
  1643. /* REVIST: Multichip handling */
  1644. mtd->size = this->chipsize;
  1645. /* Check OneNAND lock scheme */
  1646. onenand_lock_scheme(mtd);
  1647. return 0;
  1648. }
  1649. /**
  1650. * onenand_suspend - [MTD Interface] Suspend the OneNAND flash
  1651. * @param mtd MTD device structure
  1652. */
  1653. static int onenand_suspend(struct mtd_info *mtd)
  1654. {
  1655. return onenand_get_device(mtd, FL_PM_SUSPENDED);
  1656. }
  1657. /**
  1658. * onenand_resume - [MTD Interface] Resume the OneNAND flash
  1659. * @param mtd MTD device structure
  1660. */
  1661. static void onenand_resume(struct mtd_info *mtd)
  1662. {
  1663. struct onenand_chip *this = mtd->priv;
  1664. if (this->state == FL_PM_SUSPENDED)
  1665. onenand_release_device(mtd);
  1666. else
  1667. printk(KERN_ERR "resume() called for the chip which is not"
  1668. "in suspended state\n");
  1669. }
  1670. /**
  1671. * onenand_scan - [OneNAND Interface] Scan for the OneNAND device
  1672. * @param mtd MTD device structure
  1673. * @param maxchips Number of chips to scan for
  1674. *
  1675. * This fills out all the not initialized function pointers
  1676. * with the defaults.
  1677. * The flash ID is read and the mtd/chip structures are
  1678. * filled with the appropriate values.
  1679. */
  1680. int onenand_scan(struct mtd_info *mtd, int maxchips)
  1681. {
  1682. struct onenand_chip *this = mtd->priv;
  1683. if (!this->read_word)
  1684. this->read_word = onenand_readw;
  1685. if (!this->write_word)
  1686. this->write_word = onenand_writew;
  1687. if (!this->command)
  1688. this->command = onenand_command;
  1689. if (!this->wait)
  1690. onenand_setup_wait(mtd);
  1691. if (!this->read_bufferram)
  1692. this->read_bufferram = onenand_read_bufferram;
  1693. if (!this->write_bufferram)
  1694. this->write_bufferram = onenand_write_bufferram;
  1695. if (!this->block_markbad)
  1696. this->block_markbad = onenand_default_block_markbad;
  1697. if (!this->scan_bbt)
  1698. this->scan_bbt = onenand_default_bbt;
  1699. if (onenand_probe(mtd))
  1700. return -ENXIO;
  1701. /* Set Sync. Burst Read after probing */
  1702. if (this->mmcontrol) {
  1703. printk(KERN_INFO "OneNAND Sync. Burst Read support\n");
  1704. this->read_bufferram = onenand_sync_read_bufferram;
  1705. }
  1706. /* Allocate buffers, if necessary */
  1707. if (!this->page_buf) {
  1708. size_t len;
  1709. len = mtd->writesize + mtd->oobsize;
  1710. this->page_buf = kmalloc(len, GFP_KERNEL);
  1711. if (!this->page_buf) {
  1712. printk(KERN_ERR "onenand_scan(): Can't allocate page_buf\n");
  1713. return -ENOMEM;
  1714. }
  1715. this->options |= ONENAND_PAGEBUF_ALLOC;
  1716. }
  1717. this->state = FL_READY;
  1718. init_waitqueue_head(&this->wq);
  1719. spin_lock_init(&this->chip_lock);
  1720. /*
  1721. * Allow subpage writes up to oobsize.
  1722. */
  1723. switch (mtd->oobsize) {
  1724. case 64:
  1725. this->ecclayout = &onenand_oob_64;
  1726. mtd->subpage_sft = 2;
  1727. break;
  1728. case 32:
  1729. this->ecclayout = &onenand_oob_32;
  1730. mtd->subpage_sft = 1;
  1731. break;
  1732. default:
  1733. printk(KERN_WARNING "No OOB scheme defined for oobsize %d\n",
  1734. mtd->oobsize);
  1735. mtd->subpage_sft = 0;
  1736. /* To prevent kernel oops */
  1737. this->ecclayout = &onenand_oob_32;
  1738. break;
  1739. }
  1740. this->subpagesize = mtd->writesize >> mtd->subpage_sft;
  1741. mtd->ecclayout = this->ecclayout;
  1742. /* Fill in remaining MTD driver data */
  1743. mtd->type = MTD_NANDFLASH;
  1744. mtd->flags = MTD_CAP_NANDFLASH;
  1745. mtd->ecctype = MTD_ECC_SW;
  1746. mtd->erase = onenand_erase;
  1747. mtd->point = NULL;
  1748. mtd->unpoint = NULL;
  1749. mtd->read = onenand_read;
  1750. mtd->write = onenand_write;
  1751. mtd->read_oob = onenand_read_oob;
  1752. mtd->write_oob = onenand_write_oob;
  1753. #ifdef CONFIG_MTD_ONENAND_OTP
  1754. mtd->get_fact_prot_info = onenand_get_fact_prot_info;
  1755. mtd->read_fact_prot_reg = onenand_read_fact_prot_reg;
  1756. mtd->get_user_prot_info = onenand_get_user_prot_info;
  1757. mtd->read_user_prot_reg = onenand_read_user_prot_reg;
  1758. mtd->write_user_prot_reg = onenand_write_user_prot_reg;
  1759. mtd->lock_user_prot_reg = onenand_lock_user_prot_reg;
  1760. #endif
  1761. mtd->sync = onenand_sync;
  1762. mtd->lock = onenand_lock;
  1763. mtd->unlock = onenand_unlock;
  1764. mtd->suspend = onenand_suspend;
  1765. mtd->resume = onenand_resume;
  1766. mtd->block_isbad = onenand_block_isbad;
  1767. mtd->block_markbad = onenand_block_markbad;
  1768. mtd->owner = THIS_MODULE;
  1769. /* Unlock whole block */
  1770. onenand_unlock_all(mtd);
  1771. return this->scan_bbt(mtd);
  1772. }
  1773. /**
  1774. * onenand_release - [OneNAND Interface] Free resources held by the OneNAND device
  1775. * @param mtd MTD device structure
  1776. */
  1777. void onenand_release(struct mtd_info *mtd)
  1778. {
  1779. struct onenand_chip *this = mtd->priv;
  1780. #ifdef CONFIG_MTD_PARTITIONS
  1781. /* Deregister partitions */
  1782. del_mtd_partitions (mtd);
  1783. #endif
  1784. /* Deregister the device */
  1785. del_mtd_device (mtd);
  1786. /* Free bad block table memory, if allocated */
  1787. if (this->bbm)
  1788. kfree(this->bbm);
  1789. /* Buffer allocated by onenand_scan */
  1790. if (this->options & ONENAND_PAGEBUF_ALLOC)
  1791. kfree(this->page_buf);
  1792. }
  1793. EXPORT_SYMBOL_GPL(onenand_scan);
  1794. EXPORT_SYMBOL_GPL(onenand_release);
  1795. MODULE_LICENSE("GPL");
  1796. MODULE_AUTHOR("Kyungmin Park <kyungmin.park@samsung.com>");
  1797. MODULE_DESCRIPTION("Generic OneNAND flash driver code");