gpmi-nand.c 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783
  1. /*
  2. * Freescale GPMI NAND Flash Driver
  3. *
  4. * Copyright (C) 2010-2011 Freescale Semiconductor, Inc.
  5. * Copyright (C) 2008 Embedded Alley Solutions, Inc.
  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 as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along
  18. * with this program; if not, write to the Free Software Foundation, Inc.,
  19. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  20. */
  21. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  22. #include <linux/clk.h>
  23. #include <linux/slab.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/module.h>
  26. #include <linux/mtd/partitions.h>
  27. #include <linux/of.h>
  28. #include <linux/of_device.h>
  29. #include <linux/of_mtd.h>
  30. #include "gpmi-nand.h"
  31. /* Resource names for the GPMI NAND driver. */
  32. #define GPMI_NAND_GPMI_REGS_ADDR_RES_NAME "gpmi-nand"
  33. #define GPMI_NAND_BCH_REGS_ADDR_RES_NAME "bch"
  34. #define GPMI_NAND_BCH_INTERRUPT_RES_NAME "bch"
  35. /* add our owner bbt descriptor */
  36. static uint8_t scan_ff_pattern[] = { 0xff };
  37. static struct nand_bbt_descr gpmi_bbt_descr = {
  38. .options = 0,
  39. .offs = 0,
  40. .len = 1,
  41. .pattern = scan_ff_pattern
  42. };
  43. /* We will use all the (page + OOB). */
  44. static struct nand_ecclayout gpmi_hw_ecclayout = {
  45. .eccbytes = 0,
  46. .eccpos = { 0, },
  47. .oobfree = { {.offset = 0, .length = 0} }
  48. };
  49. static irqreturn_t bch_irq(int irq, void *cookie)
  50. {
  51. struct gpmi_nand_data *this = cookie;
  52. gpmi_clear_bch(this);
  53. complete(&this->bch_done);
  54. return IRQ_HANDLED;
  55. }
  56. /*
  57. * Calculate the ECC strength by hand:
  58. * E : The ECC strength.
  59. * G : the length of Galois Field.
  60. * N : The chunk count of per page.
  61. * O : the oobsize of the NAND chip.
  62. * M : the metasize of per page.
  63. *
  64. * The formula is :
  65. * E * G * N
  66. * ------------ <= (O - M)
  67. * 8
  68. *
  69. * So, we get E by:
  70. * (O - M) * 8
  71. * E <= -------------
  72. * G * N
  73. */
  74. static inline int get_ecc_strength(struct gpmi_nand_data *this)
  75. {
  76. struct bch_geometry *geo = &this->bch_geometry;
  77. struct mtd_info *mtd = &this->mtd;
  78. int ecc_strength;
  79. ecc_strength = ((mtd->oobsize - geo->metadata_size) * 8)
  80. / (geo->gf_len * geo->ecc_chunk_count);
  81. /* We need the minor even number. */
  82. return round_down(ecc_strength, 2);
  83. }
  84. static inline bool gpmi_check_ecc(struct gpmi_nand_data *this)
  85. {
  86. struct bch_geometry *geo = &this->bch_geometry;
  87. /* Do the sanity check. */
  88. if (GPMI_IS_MX23(this) || GPMI_IS_MX28(this)) {
  89. /* The mx23/mx28 only support the GF13. */
  90. if (geo->gf_len == 14)
  91. return false;
  92. if (geo->ecc_strength > MXS_ECC_STRENGTH_MAX)
  93. return false;
  94. } else if (GPMI_IS_MX6Q(this)) {
  95. if (geo->ecc_strength > MX6_ECC_STRENGTH_MAX)
  96. return false;
  97. }
  98. return true;
  99. }
  100. /*
  101. * If we can get the ECC information from the nand chip, we do not
  102. * need to calculate them ourselves.
  103. *
  104. * We may have available oob space in this case.
  105. */
  106. static bool set_geometry_by_ecc_info(struct gpmi_nand_data *this)
  107. {
  108. struct bch_geometry *geo = &this->bch_geometry;
  109. struct mtd_info *mtd = &this->mtd;
  110. struct nand_chip *chip = mtd->priv;
  111. struct nand_oobfree *of = gpmi_hw_ecclayout.oobfree;
  112. unsigned int block_mark_bit_offset;
  113. if (!(chip->ecc_strength_ds > 0 && chip->ecc_step_ds > 0))
  114. return false;
  115. switch (chip->ecc_step_ds) {
  116. case SZ_512:
  117. geo->gf_len = 13;
  118. break;
  119. case SZ_1K:
  120. geo->gf_len = 14;
  121. break;
  122. default:
  123. dev_err(this->dev,
  124. "unsupported nand chip. ecc bits : %d, ecc size : %d\n",
  125. chip->ecc_strength_ds, chip->ecc_step_ds);
  126. return false;
  127. }
  128. geo->ecc_chunk_size = chip->ecc_step_ds;
  129. geo->ecc_strength = round_up(chip->ecc_strength_ds, 2);
  130. if (!gpmi_check_ecc(this))
  131. return false;
  132. /* Keep the C >= O */
  133. if (geo->ecc_chunk_size < mtd->oobsize) {
  134. dev_err(this->dev,
  135. "unsupported nand chip. ecc size: %d, oob size : %d\n",
  136. chip->ecc_step_ds, mtd->oobsize);
  137. return false;
  138. }
  139. /* The default value, see comment in the legacy_set_geometry(). */
  140. geo->metadata_size = 10;
  141. geo->ecc_chunk_count = mtd->writesize / geo->ecc_chunk_size;
  142. /*
  143. * Now, the NAND chip with 2K page(data chunk is 512byte) shows below:
  144. *
  145. * | P |
  146. * |<----------------------------------------------------->|
  147. * | |
  148. * | (Block Mark) |
  149. * | P' | | | |
  150. * |<-------------------------------------------->| D | | O' |
  151. * | |<---->| |<--->|
  152. * V V V V V
  153. * +---+----------+-+----------+-+----------+-+----------+-+-----+
  154. * | M | data |E| data |E| data |E| data |E| |
  155. * +---+----------+-+----------+-+----------+-+----------+-+-----+
  156. * ^ ^
  157. * | O |
  158. * |<------------>|
  159. * | |
  160. *
  161. * P : the page size for BCH module.
  162. * E : The ECC strength.
  163. * G : the length of Galois Field.
  164. * N : The chunk count of per page.
  165. * M : the metasize of per page.
  166. * C : the ecc chunk size, aka the "data" above.
  167. * P': the nand chip's page size.
  168. * O : the nand chip's oob size.
  169. * O': the free oob.
  170. *
  171. * The formula for P is :
  172. *
  173. * E * G * N
  174. * P = ------------ + P' + M
  175. * 8
  176. *
  177. * The position of block mark moves forward in the ECC-based view
  178. * of page, and the delta is:
  179. *
  180. * E * G * (N - 1)
  181. * D = (---------------- + M)
  182. * 8
  183. *
  184. * Please see the comment in legacy_set_geometry().
  185. * With the condition C >= O , we still can get same result.
  186. * So the bit position of the physical block mark within the ECC-based
  187. * view of the page is :
  188. * (P' - D) * 8
  189. */
  190. geo->page_size = mtd->writesize + geo->metadata_size +
  191. (geo->gf_len * geo->ecc_strength * geo->ecc_chunk_count) / 8;
  192. /* The available oob size we have. */
  193. if (geo->page_size < mtd->writesize + mtd->oobsize) {
  194. of->offset = geo->page_size - mtd->writesize;
  195. of->length = mtd->oobsize - of->offset;
  196. }
  197. geo->payload_size = mtd->writesize;
  198. geo->auxiliary_status_offset = ALIGN(geo->metadata_size, 4);
  199. geo->auxiliary_size = ALIGN(geo->metadata_size, 4)
  200. + ALIGN(geo->ecc_chunk_count, 4);
  201. if (!this->swap_block_mark)
  202. return true;
  203. /* For bit swap. */
  204. block_mark_bit_offset = mtd->writesize * 8 -
  205. (geo->ecc_strength * geo->gf_len * (geo->ecc_chunk_count - 1)
  206. + geo->metadata_size * 8);
  207. geo->block_mark_byte_offset = block_mark_bit_offset / 8;
  208. geo->block_mark_bit_offset = block_mark_bit_offset % 8;
  209. return true;
  210. }
  211. static int legacy_set_geometry(struct gpmi_nand_data *this)
  212. {
  213. struct bch_geometry *geo = &this->bch_geometry;
  214. struct mtd_info *mtd = &this->mtd;
  215. unsigned int metadata_size;
  216. unsigned int status_size;
  217. unsigned int block_mark_bit_offset;
  218. /*
  219. * The size of the metadata can be changed, though we set it to 10
  220. * bytes now. But it can't be too large, because we have to save
  221. * enough space for BCH.
  222. */
  223. geo->metadata_size = 10;
  224. /* The default for the length of Galois Field. */
  225. geo->gf_len = 13;
  226. /* The default for chunk size. */
  227. geo->ecc_chunk_size = 512;
  228. while (geo->ecc_chunk_size < mtd->oobsize) {
  229. geo->ecc_chunk_size *= 2; /* keep C >= O */
  230. geo->gf_len = 14;
  231. }
  232. geo->ecc_chunk_count = mtd->writesize / geo->ecc_chunk_size;
  233. /* We use the same ECC strength for all chunks. */
  234. geo->ecc_strength = get_ecc_strength(this);
  235. if (!gpmi_check_ecc(this)) {
  236. dev_err(this->dev,
  237. "We can not support this nand chip."
  238. " Its required ecc strength(%d) is beyond our"
  239. " capability(%d).\n", geo->ecc_strength,
  240. (GPMI_IS_MX6Q(this) ? MX6_ECC_STRENGTH_MAX
  241. : MXS_ECC_STRENGTH_MAX));
  242. return -EINVAL;
  243. }
  244. geo->page_size = mtd->writesize + mtd->oobsize;
  245. geo->payload_size = mtd->writesize;
  246. /*
  247. * The auxiliary buffer contains the metadata and the ECC status. The
  248. * metadata is padded to the nearest 32-bit boundary. The ECC status
  249. * contains one byte for every ECC chunk, and is also padded to the
  250. * nearest 32-bit boundary.
  251. */
  252. metadata_size = ALIGN(geo->metadata_size, 4);
  253. status_size = ALIGN(geo->ecc_chunk_count, 4);
  254. geo->auxiliary_size = metadata_size + status_size;
  255. geo->auxiliary_status_offset = metadata_size;
  256. if (!this->swap_block_mark)
  257. return 0;
  258. /*
  259. * We need to compute the byte and bit offsets of
  260. * the physical block mark within the ECC-based view of the page.
  261. *
  262. * NAND chip with 2K page shows below:
  263. * (Block Mark)
  264. * | |
  265. * | D |
  266. * |<---->|
  267. * V V
  268. * +---+----------+-+----------+-+----------+-+----------+-+
  269. * | M | data |E| data |E| data |E| data |E|
  270. * +---+----------+-+----------+-+----------+-+----------+-+
  271. *
  272. * The position of block mark moves forward in the ECC-based view
  273. * of page, and the delta is:
  274. *
  275. * E * G * (N - 1)
  276. * D = (---------------- + M)
  277. * 8
  278. *
  279. * With the formula to compute the ECC strength, and the condition
  280. * : C >= O (C is the ecc chunk size)
  281. *
  282. * It's easy to deduce to the following result:
  283. *
  284. * E * G (O - M) C - M C - M
  285. * ----------- <= ------- <= -------- < ---------
  286. * 8 N N (N - 1)
  287. *
  288. * So, we get:
  289. *
  290. * E * G * (N - 1)
  291. * D = (---------------- + M) < C
  292. * 8
  293. *
  294. * The above inequality means the position of block mark
  295. * within the ECC-based view of the page is still in the data chunk,
  296. * and it's NOT in the ECC bits of the chunk.
  297. *
  298. * Use the following to compute the bit position of the
  299. * physical block mark within the ECC-based view of the page:
  300. * (page_size - D) * 8
  301. *
  302. * --Huang Shijie
  303. */
  304. block_mark_bit_offset = mtd->writesize * 8 -
  305. (geo->ecc_strength * geo->gf_len * (geo->ecc_chunk_count - 1)
  306. + geo->metadata_size * 8);
  307. geo->block_mark_byte_offset = block_mark_bit_offset / 8;
  308. geo->block_mark_bit_offset = block_mark_bit_offset % 8;
  309. return 0;
  310. }
  311. int common_nfc_set_geometry(struct gpmi_nand_data *this)
  312. {
  313. return set_geometry_by_ecc_info(this) ? 0 : legacy_set_geometry(this);
  314. }
  315. struct dma_chan *get_dma_chan(struct gpmi_nand_data *this)
  316. {
  317. int chipnr = this->current_chip;
  318. return this->dma_chans[chipnr];
  319. }
  320. /* Can we use the upper's buffer directly for DMA? */
  321. void prepare_data_dma(struct gpmi_nand_data *this, enum dma_data_direction dr)
  322. {
  323. struct scatterlist *sgl = &this->data_sgl;
  324. int ret;
  325. this->direct_dma_map_ok = true;
  326. /* first try to map the upper buffer directly */
  327. sg_init_one(sgl, this->upper_buf, this->upper_len);
  328. ret = dma_map_sg(this->dev, sgl, 1, dr);
  329. if (ret == 0) {
  330. /* We have to use our own DMA buffer. */
  331. sg_init_one(sgl, this->data_buffer_dma, PAGE_SIZE);
  332. if (dr == DMA_TO_DEVICE)
  333. memcpy(this->data_buffer_dma, this->upper_buf,
  334. this->upper_len);
  335. ret = dma_map_sg(this->dev, sgl, 1, dr);
  336. if (ret == 0)
  337. pr_err("DMA mapping failed.\n");
  338. this->direct_dma_map_ok = false;
  339. }
  340. }
  341. /* This will be called after the DMA operation is finished. */
  342. static void dma_irq_callback(void *param)
  343. {
  344. struct gpmi_nand_data *this = param;
  345. struct completion *dma_c = &this->dma_done;
  346. complete(dma_c);
  347. switch (this->dma_type) {
  348. case DMA_FOR_COMMAND:
  349. dma_unmap_sg(this->dev, &this->cmd_sgl, 1, DMA_TO_DEVICE);
  350. break;
  351. case DMA_FOR_READ_DATA:
  352. dma_unmap_sg(this->dev, &this->data_sgl, 1, DMA_FROM_DEVICE);
  353. if (this->direct_dma_map_ok == false)
  354. memcpy(this->upper_buf, this->data_buffer_dma,
  355. this->upper_len);
  356. break;
  357. case DMA_FOR_WRITE_DATA:
  358. dma_unmap_sg(this->dev, &this->data_sgl, 1, DMA_TO_DEVICE);
  359. break;
  360. case DMA_FOR_READ_ECC_PAGE:
  361. case DMA_FOR_WRITE_ECC_PAGE:
  362. /* We have to wait the BCH interrupt to finish. */
  363. break;
  364. default:
  365. pr_err("in wrong DMA operation.\n");
  366. }
  367. }
  368. int start_dma_without_bch_irq(struct gpmi_nand_data *this,
  369. struct dma_async_tx_descriptor *desc)
  370. {
  371. struct completion *dma_c = &this->dma_done;
  372. int err;
  373. init_completion(dma_c);
  374. desc->callback = dma_irq_callback;
  375. desc->callback_param = this;
  376. dmaengine_submit(desc);
  377. dma_async_issue_pending(get_dma_chan(this));
  378. /* Wait for the interrupt from the DMA block. */
  379. err = wait_for_completion_timeout(dma_c, msecs_to_jiffies(1000));
  380. if (!err) {
  381. pr_err("DMA timeout, last DMA :%d\n", this->last_dma_type);
  382. gpmi_dump_info(this);
  383. return -ETIMEDOUT;
  384. }
  385. return 0;
  386. }
  387. /*
  388. * This function is used in BCH reading or BCH writing pages.
  389. * It will wait for the BCH interrupt as long as ONE second.
  390. * Actually, we must wait for two interrupts :
  391. * [1] firstly the DMA interrupt and
  392. * [2] secondly the BCH interrupt.
  393. */
  394. int start_dma_with_bch_irq(struct gpmi_nand_data *this,
  395. struct dma_async_tx_descriptor *desc)
  396. {
  397. struct completion *bch_c = &this->bch_done;
  398. int err;
  399. /* Prepare to receive an interrupt from the BCH block. */
  400. init_completion(bch_c);
  401. /* start the DMA */
  402. start_dma_without_bch_irq(this, desc);
  403. /* Wait for the interrupt from the BCH block. */
  404. err = wait_for_completion_timeout(bch_c, msecs_to_jiffies(1000));
  405. if (!err) {
  406. pr_err("BCH timeout, last DMA :%d\n", this->last_dma_type);
  407. gpmi_dump_info(this);
  408. return -ETIMEDOUT;
  409. }
  410. return 0;
  411. }
  412. static int acquire_register_block(struct gpmi_nand_data *this,
  413. const char *res_name)
  414. {
  415. struct platform_device *pdev = this->pdev;
  416. struct resources *res = &this->resources;
  417. struct resource *r;
  418. void __iomem *p;
  419. r = platform_get_resource_byname(pdev, IORESOURCE_MEM, res_name);
  420. if (!r) {
  421. pr_err("Can't get resource for %s\n", res_name);
  422. return -ENODEV;
  423. }
  424. p = ioremap(r->start, resource_size(r));
  425. if (!p) {
  426. pr_err("Can't remap %s\n", res_name);
  427. return -ENOMEM;
  428. }
  429. if (!strcmp(res_name, GPMI_NAND_GPMI_REGS_ADDR_RES_NAME))
  430. res->gpmi_regs = p;
  431. else if (!strcmp(res_name, GPMI_NAND_BCH_REGS_ADDR_RES_NAME))
  432. res->bch_regs = p;
  433. else
  434. pr_err("unknown resource name : %s\n", res_name);
  435. return 0;
  436. }
  437. static void release_register_block(struct gpmi_nand_data *this)
  438. {
  439. struct resources *res = &this->resources;
  440. if (res->gpmi_regs)
  441. iounmap(res->gpmi_regs);
  442. if (res->bch_regs)
  443. iounmap(res->bch_regs);
  444. res->gpmi_regs = NULL;
  445. res->bch_regs = NULL;
  446. }
  447. static int acquire_bch_irq(struct gpmi_nand_data *this, irq_handler_t irq_h)
  448. {
  449. struct platform_device *pdev = this->pdev;
  450. struct resources *res = &this->resources;
  451. const char *res_name = GPMI_NAND_BCH_INTERRUPT_RES_NAME;
  452. struct resource *r;
  453. int err;
  454. r = platform_get_resource_byname(pdev, IORESOURCE_IRQ, res_name);
  455. if (!r) {
  456. pr_err("Can't get resource for %s\n", res_name);
  457. return -ENODEV;
  458. }
  459. err = request_irq(r->start, irq_h, 0, res_name, this);
  460. if (err) {
  461. pr_err("Can't own %s\n", res_name);
  462. return err;
  463. }
  464. res->bch_low_interrupt = r->start;
  465. res->bch_high_interrupt = r->end;
  466. return 0;
  467. }
  468. static void release_bch_irq(struct gpmi_nand_data *this)
  469. {
  470. struct resources *res = &this->resources;
  471. int i = res->bch_low_interrupt;
  472. for (; i <= res->bch_high_interrupt; i++)
  473. free_irq(i, this);
  474. }
  475. static void release_dma_channels(struct gpmi_nand_data *this)
  476. {
  477. unsigned int i;
  478. for (i = 0; i < DMA_CHANS; i++)
  479. if (this->dma_chans[i]) {
  480. dma_release_channel(this->dma_chans[i]);
  481. this->dma_chans[i] = NULL;
  482. }
  483. }
  484. static int acquire_dma_channels(struct gpmi_nand_data *this)
  485. {
  486. struct platform_device *pdev = this->pdev;
  487. struct dma_chan *dma_chan;
  488. /* request dma channel */
  489. dma_chan = dma_request_slave_channel(&pdev->dev, "rx-tx");
  490. if (!dma_chan) {
  491. pr_err("Failed to request DMA channel.\n");
  492. goto acquire_err;
  493. }
  494. this->dma_chans[0] = dma_chan;
  495. return 0;
  496. acquire_err:
  497. release_dma_channels(this);
  498. return -EINVAL;
  499. }
  500. static void gpmi_put_clks(struct gpmi_nand_data *this)
  501. {
  502. struct resources *r = &this->resources;
  503. struct clk *clk;
  504. int i;
  505. for (i = 0; i < GPMI_CLK_MAX; i++) {
  506. clk = r->clock[i];
  507. if (clk) {
  508. clk_put(clk);
  509. r->clock[i] = NULL;
  510. }
  511. }
  512. }
  513. static char *extra_clks_for_mx6q[GPMI_CLK_MAX] = {
  514. "gpmi_apb", "gpmi_bch", "gpmi_bch_apb", "per1_bch",
  515. };
  516. static int gpmi_get_clks(struct gpmi_nand_data *this)
  517. {
  518. struct resources *r = &this->resources;
  519. char **extra_clks = NULL;
  520. struct clk *clk;
  521. int err, i;
  522. /* The main clock is stored in the first. */
  523. r->clock[0] = clk_get(this->dev, "gpmi_io");
  524. if (IS_ERR(r->clock[0])) {
  525. err = PTR_ERR(r->clock[0]);
  526. goto err_clock;
  527. }
  528. /* Get extra clocks */
  529. if (GPMI_IS_MX6Q(this))
  530. extra_clks = extra_clks_for_mx6q;
  531. if (!extra_clks)
  532. return 0;
  533. for (i = 1; i < GPMI_CLK_MAX; i++) {
  534. if (extra_clks[i - 1] == NULL)
  535. break;
  536. clk = clk_get(this->dev, extra_clks[i - 1]);
  537. if (IS_ERR(clk)) {
  538. err = PTR_ERR(clk);
  539. goto err_clock;
  540. }
  541. r->clock[i] = clk;
  542. }
  543. if (GPMI_IS_MX6Q(this))
  544. /*
  545. * Set the default value for the gpmi clock in mx6q:
  546. *
  547. * If you want to use the ONFI nand which is in the
  548. * Synchronous Mode, you should change the clock as you need.
  549. */
  550. clk_set_rate(r->clock[0], 22000000);
  551. return 0;
  552. err_clock:
  553. dev_dbg(this->dev, "failed in finding the clocks.\n");
  554. gpmi_put_clks(this);
  555. return err;
  556. }
  557. static int acquire_resources(struct gpmi_nand_data *this)
  558. {
  559. int ret;
  560. ret = acquire_register_block(this, GPMI_NAND_GPMI_REGS_ADDR_RES_NAME);
  561. if (ret)
  562. goto exit_regs;
  563. ret = acquire_register_block(this, GPMI_NAND_BCH_REGS_ADDR_RES_NAME);
  564. if (ret)
  565. goto exit_regs;
  566. ret = acquire_bch_irq(this, bch_irq);
  567. if (ret)
  568. goto exit_regs;
  569. ret = acquire_dma_channels(this);
  570. if (ret)
  571. goto exit_dma_channels;
  572. ret = gpmi_get_clks(this);
  573. if (ret)
  574. goto exit_clock;
  575. return 0;
  576. exit_clock:
  577. release_dma_channels(this);
  578. exit_dma_channels:
  579. release_bch_irq(this);
  580. exit_regs:
  581. release_register_block(this);
  582. return ret;
  583. }
  584. static void release_resources(struct gpmi_nand_data *this)
  585. {
  586. gpmi_put_clks(this);
  587. release_register_block(this);
  588. release_bch_irq(this);
  589. release_dma_channels(this);
  590. }
  591. static int init_hardware(struct gpmi_nand_data *this)
  592. {
  593. int ret;
  594. /*
  595. * This structure contains the "safe" GPMI timing that should succeed
  596. * with any NAND Flash device
  597. * (although, with less-than-optimal performance).
  598. */
  599. struct nand_timing safe_timing = {
  600. .data_setup_in_ns = 80,
  601. .data_hold_in_ns = 60,
  602. .address_setup_in_ns = 25,
  603. .gpmi_sample_delay_in_ns = 6,
  604. .tREA_in_ns = -1,
  605. .tRLOH_in_ns = -1,
  606. .tRHOH_in_ns = -1,
  607. };
  608. /* Initialize the hardwares. */
  609. ret = gpmi_init(this);
  610. if (ret)
  611. return ret;
  612. this->timing = safe_timing;
  613. return 0;
  614. }
  615. static int read_page_prepare(struct gpmi_nand_data *this,
  616. void *destination, unsigned length,
  617. void *alt_virt, dma_addr_t alt_phys, unsigned alt_size,
  618. void **use_virt, dma_addr_t *use_phys)
  619. {
  620. struct device *dev = this->dev;
  621. if (virt_addr_valid(destination)) {
  622. dma_addr_t dest_phys;
  623. dest_phys = dma_map_single(dev, destination,
  624. length, DMA_FROM_DEVICE);
  625. if (dma_mapping_error(dev, dest_phys)) {
  626. if (alt_size < length) {
  627. pr_err("%s, Alternate buffer is too small\n",
  628. __func__);
  629. return -ENOMEM;
  630. }
  631. goto map_failed;
  632. }
  633. *use_virt = destination;
  634. *use_phys = dest_phys;
  635. this->direct_dma_map_ok = true;
  636. return 0;
  637. }
  638. map_failed:
  639. *use_virt = alt_virt;
  640. *use_phys = alt_phys;
  641. this->direct_dma_map_ok = false;
  642. return 0;
  643. }
  644. static inline void read_page_end(struct gpmi_nand_data *this,
  645. void *destination, unsigned length,
  646. void *alt_virt, dma_addr_t alt_phys, unsigned alt_size,
  647. void *used_virt, dma_addr_t used_phys)
  648. {
  649. if (this->direct_dma_map_ok)
  650. dma_unmap_single(this->dev, used_phys, length, DMA_FROM_DEVICE);
  651. }
  652. static inline void read_page_swap_end(struct gpmi_nand_data *this,
  653. void *destination, unsigned length,
  654. void *alt_virt, dma_addr_t alt_phys, unsigned alt_size,
  655. void *used_virt, dma_addr_t used_phys)
  656. {
  657. if (!this->direct_dma_map_ok)
  658. memcpy(destination, alt_virt, length);
  659. }
  660. static int send_page_prepare(struct gpmi_nand_data *this,
  661. const void *source, unsigned length,
  662. void *alt_virt, dma_addr_t alt_phys, unsigned alt_size,
  663. const void **use_virt, dma_addr_t *use_phys)
  664. {
  665. struct device *dev = this->dev;
  666. if (virt_addr_valid(source)) {
  667. dma_addr_t source_phys;
  668. source_phys = dma_map_single(dev, (void *)source, length,
  669. DMA_TO_DEVICE);
  670. if (dma_mapping_error(dev, source_phys)) {
  671. if (alt_size < length) {
  672. pr_err("%s, Alternate buffer is too small\n",
  673. __func__);
  674. return -ENOMEM;
  675. }
  676. goto map_failed;
  677. }
  678. *use_virt = source;
  679. *use_phys = source_phys;
  680. return 0;
  681. }
  682. map_failed:
  683. /*
  684. * Copy the content of the source buffer into the alternate
  685. * buffer and set up the return values accordingly.
  686. */
  687. memcpy(alt_virt, source, length);
  688. *use_virt = alt_virt;
  689. *use_phys = alt_phys;
  690. return 0;
  691. }
  692. static void send_page_end(struct gpmi_nand_data *this,
  693. const void *source, unsigned length,
  694. void *alt_virt, dma_addr_t alt_phys, unsigned alt_size,
  695. const void *used_virt, dma_addr_t used_phys)
  696. {
  697. struct device *dev = this->dev;
  698. if (used_virt == source)
  699. dma_unmap_single(dev, used_phys, length, DMA_TO_DEVICE);
  700. }
  701. static void gpmi_free_dma_buffer(struct gpmi_nand_data *this)
  702. {
  703. struct device *dev = this->dev;
  704. if (this->page_buffer_virt && virt_addr_valid(this->page_buffer_virt))
  705. dma_free_coherent(dev, this->page_buffer_size,
  706. this->page_buffer_virt,
  707. this->page_buffer_phys);
  708. kfree(this->cmd_buffer);
  709. kfree(this->data_buffer_dma);
  710. this->cmd_buffer = NULL;
  711. this->data_buffer_dma = NULL;
  712. this->page_buffer_virt = NULL;
  713. this->page_buffer_size = 0;
  714. }
  715. /* Allocate the DMA buffers */
  716. static int gpmi_alloc_dma_buffer(struct gpmi_nand_data *this)
  717. {
  718. struct bch_geometry *geo = &this->bch_geometry;
  719. struct device *dev = this->dev;
  720. /* [1] Allocate a command buffer. PAGE_SIZE is enough. */
  721. this->cmd_buffer = kzalloc(PAGE_SIZE, GFP_DMA | GFP_KERNEL);
  722. if (this->cmd_buffer == NULL)
  723. goto error_alloc;
  724. /* [2] Allocate a read/write data buffer. PAGE_SIZE is enough. */
  725. this->data_buffer_dma = kzalloc(PAGE_SIZE, GFP_DMA | GFP_KERNEL);
  726. if (this->data_buffer_dma == NULL)
  727. goto error_alloc;
  728. /*
  729. * [3] Allocate the page buffer.
  730. *
  731. * Both the payload buffer and the auxiliary buffer must appear on
  732. * 32-bit boundaries. We presume the size of the payload buffer is a
  733. * power of two and is much larger than four, which guarantees the
  734. * auxiliary buffer will appear on a 32-bit boundary.
  735. */
  736. this->page_buffer_size = geo->payload_size + geo->auxiliary_size;
  737. this->page_buffer_virt = dma_alloc_coherent(dev, this->page_buffer_size,
  738. &this->page_buffer_phys, GFP_DMA);
  739. if (!this->page_buffer_virt)
  740. goto error_alloc;
  741. /* Slice up the page buffer. */
  742. this->payload_virt = this->page_buffer_virt;
  743. this->payload_phys = this->page_buffer_phys;
  744. this->auxiliary_virt = this->payload_virt + geo->payload_size;
  745. this->auxiliary_phys = this->payload_phys + geo->payload_size;
  746. return 0;
  747. error_alloc:
  748. gpmi_free_dma_buffer(this);
  749. pr_err("Error allocating DMA buffers!\n");
  750. return -ENOMEM;
  751. }
  752. static void gpmi_cmd_ctrl(struct mtd_info *mtd, int data, unsigned int ctrl)
  753. {
  754. struct nand_chip *chip = mtd->priv;
  755. struct gpmi_nand_data *this = chip->priv;
  756. int ret;
  757. /*
  758. * Every operation begins with a command byte and a series of zero or
  759. * more address bytes. These are distinguished by either the Address
  760. * Latch Enable (ALE) or Command Latch Enable (CLE) signals being
  761. * asserted. When MTD is ready to execute the command, it will deassert
  762. * both latch enables.
  763. *
  764. * Rather than run a separate DMA operation for every single byte, we
  765. * queue them up and run a single DMA operation for the entire series
  766. * of command and data bytes. NAND_CMD_NONE means the END of the queue.
  767. */
  768. if ((ctrl & (NAND_ALE | NAND_CLE))) {
  769. if (data != NAND_CMD_NONE)
  770. this->cmd_buffer[this->command_length++] = data;
  771. return;
  772. }
  773. if (!this->command_length)
  774. return;
  775. ret = gpmi_send_command(this);
  776. if (ret)
  777. pr_err("Chip: %u, Error %d\n", this->current_chip, ret);
  778. this->command_length = 0;
  779. }
  780. static int gpmi_dev_ready(struct mtd_info *mtd)
  781. {
  782. struct nand_chip *chip = mtd->priv;
  783. struct gpmi_nand_data *this = chip->priv;
  784. return gpmi_is_ready(this, this->current_chip);
  785. }
  786. static void gpmi_select_chip(struct mtd_info *mtd, int chipnr)
  787. {
  788. struct nand_chip *chip = mtd->priv;
  789. struct gpmi_nand_data *this = chip->priv;
  790. if ((this->current_chip < 0) && (chipnr >= 0))
  791. gpmi_begin(this);
  792. else if ((this->current_chip >= 0) && (chipnr < 0))
  793. gpmi_end(this);
  794. this->current_chip = chipnr;
  795. }
  796. static void gpmi_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
  797. {
  798. struct nand_chip *chip = mtd->priv;
  799. struct gpmi_nand_data *this = chip->priv;
  800. pr_debug("len is %d\n", len);
  801. this->upper_buf = buf;
  802. this->upper_len = len;
  803. gpmi_read_data(this);
  804. }
  805. static void gpmi_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
  806. {
  807. struct nand_chip *chip = mtd->priv;
  808. struct gpmi_nand_data *this = chip->priv;
  809. pr_debug("len is %d\n", len);
  810. this->upper_buf = (uint8_t *)buf;
  811. this->upper_len = len;
  812. gpmi_send_data(this);
  813. }
  814. static uint8_t gpmi_read_byte(struct mtd_info *mtd)
  815. {
  816. struct nand_chip *chip = mtd->priv;
  817. struct gpmi_nand_data *this = chip->priv;
  818. uint8_t *buf = this->data_buffer_dma;
  819. gpmi_read_buf(mtd, buf, 1);
  820. return buf[0];
  821. }
  822. /*
  823. * Handles block mark swapping.
  824. * It can be called in swapping the block mark, or swapping it back,
  825. * because the the operations are the same.
  826. */
  827. static void block_mark_swapping(struct gpmi_nand_data *this,
  828. void *payload, void *auxiliary)
  829. {
  830. struct bch_geometry *nfc_geo = &this->bch_geometry;
  831. unsigned char *p;
  832. unsigned char *a;
  833. unsigned int bit;
  834. unsigned char mask;
  835. unsigned char from_data;
  836. unsigned char from_oob;
  837. if (!this->swap_block_mark)
  838. return;
  839. /*
  840. * If control arrives here, we're swapping. Make some convenience
  841. * variables.
  842. */
  843. bit = nfc_geo->block_mark_bit_offset;
  844. p = payload + nfc_geo->block_mark_byte_offset;
  845. a = auxiliary;
  846. /*
  847. * Get the byte from the data area that overlays the block mark. Since
  848. * the ECC engine applies its own view to the bits in the page, the
  849. * physical block mark won't (in general) appear on a byte boundary in
  850. * the data.
  851. */
  852. from_data = (p[0] >> bit) | (p[1] << (8 - bit));
  853. /* Get the byte from the OOB. */
  854. from_oob = a[0];
  855. /* Swap them. */
  856. a[0] = from_data;
  857. mask = (0x1 << bit) - 1;
  858. p[0] = (p[0] & mask) | (from_oob << bit);
  859. mask = ~0 << bit;
  860. p[1] = (p[1] & mask) | (from_oob >> (8 - bit));
  861. }
  862. static int gpmi_ecc_read_page(struct mtd_info *mtd, struct nand_chip *chip,
  863. uint8_t *buf, int oob_required, int page)
  864. {
  865. struct gpmi_nand_data *this = chip->priv;
  866. struct bch_geometry *nfc_geo = &this->bch_geometry;
  867. void *payload_virt;
  868. dma_addr_t payload_phys;
  869. void *auxiliary_virt;
  870. dma_addr_t auxiliary_phys;
  871. unsigned int i;
  872. unsigned char *status;
  873. unsigned int max_bitflips = 0;
  874. int ret;
  875. pr_debug("page number is : %d\n", page);
  876. ret = read_page_prepare(this, buf, mtd->writesize,
  877. this->payload_virt, this->payload_phys,
  878. nfc_geo->payload_size,
  879. &payload_virt, &payload_phys);
  880. if (ret) {
  881. pr_err("Inadequate DMA buffer\n");
  882. ret = -ENOMEM;
  883. return ret;
  884. }
  885. auxiliary_virt = this->auxiliary_virt;
  886. auxiliary_phys = this->auxiliary_phys;
  887. /* go! */
  888. ret = gpmi_read_page(this, payload_phys, auxiliary_phys);
  889. read_page_end(this, buf, mtd->writesize,
  890. this->payload_virt, this->payload_phys,
  891. nfc_geo->payload_size,
  892. payload_virt, payload_phys);
  893. if (ret) {
  894. pr_err("Error in ECC-based read: %d\n", ret);
  895. return ret;
  896. }
  897. /* handle the block mark swapping */
  898. block_mark_swapping(this, payload_virt, auxiliary_virt);
  899. /* Loop over status bytes, accumulating ECC status. */
  900. status = auxiliary_virt + nfc_geo->auxiliary_status_offset;
  901. for (i = 0; i < nfc_geo->ecc_chunk_count; i++, status++) {
  902. if ((*status == STATUS_GOOD) || (*status == STATUS_ERASED))
  903. continue;
  904. if (*status == STATUS_UNCORRECTABLE) {
  905. mtd->ecc_stats.failed++;
  906. continue;
  907. }
  908. mtd->ecc_stats.corrected += *status;
  909. max_bitflips = max_t(unsigned int, max_bitflips, *status);
  910. }
  911. if (oob_required) {
  912. /*
  913. * It's time to deliver the OOB bytes. See gpmi_ecc_read_oob()
  914. * for details about our policy for delivering the OOB.
  915. *
  916. * We fill the caller's buffer with set bits, and then copy the
  917. * block mark to th caller's buffer. Note that, if block mark
  918. * swapping was necessary, it has already been done, so we can
  919. * rely on the first byte of the auxiliary buffer to contain
  920. * the block mark.
  921. */
  922. memset(chip->oob_poi, ~0, mtd->oobsize);
  923. chip->oob_poi[0] = ((uint8_t *) auxiliary_virt)[0];
  924. }
  925. read_page_swap_end(this, buf, mtd->writesize,
  926. this->payload_virt, this->payload_phys,
  927. nfc_geo->payload_size,
  928. payload_virt, payload_phys);
  929. return max_bitflips;
  930. }
  931. static int gpmi_ecc_write_page(struct mtd_info *mtd, struct nand_chip *chip,
  932. const uint8_t *buf, int oob_required)
  933. {
  934. struct gpmi_nand_data *this = chip->priv;
  935. struct bch_geometry *nfc_geo = &this->bch_geometry;
  936. const void *payload_virt;
  937. dma_addr_t payload_phys;
  938. const void *auxiliary_virt;
  939. dma_addr_t auxiliary_phys;
  940. int ret;
  941. pr_debug("ecc write page.\n");
  942. if (this->swap_block_mark) {
  943. /*
  944. * If control arrives here, we're doing block mark swapping.
  945. * Since we can't modify the caller's buffers, we must copy them
  946. * into our own.
  947. */
  948. memcpy(this->payload_virt, buf, mtd->writesize);
  949. payload_virt = this->payload_virt;
  950. payload_phys = this->payload_phys;
  951. memcpy(this->auxiliary_virt, chip->oob_poi,
  952. nfc_geo->auxiliary_size);
  953. auxiliary_virt = this->auxiliary_virt;
  954. auxiliary_phys = this->auxiliary_phys;
  955. /* Handle block mark swapping. */
  956. block_mark_swapping(this,
  957. (void *) payload_virt, (void *) auxiliary_virt);
  958. } else {
  959. /*
  960. * If control arrives here, we're not doing block mark swapping,
  961. * so we can to try and use the caller's buffers.
  962. */
  963. ret = send_page_prepare(this,
  964. buf, mtd->writesize,
  965. this->payload_virt, this->payload_phys,
  966. nfc_geo->payload_size,
  967. &payload_virt, &payload_phys);
  968. if (ret) {
  969. pr_err("Inadequate payload DMA buffer\n");
  970. return 0;
  971. }
  972. ret = send_page_prepare(this,
  973. chip->oob_poi, mtd->oobsize,
  974. this->auxiliary_virt, this->auxiliary_phys,
  975. nfc_geo->auxiliary_size,
  976. &auxiliary_virt, &auxiliary_phys);
  977. if (ret) {
  978. pr_err("Inadequate auxiliary DMA buffer\n");
  979. goto exit_auxiliary;
  980. }
  981. }
  982. /* Ask the NFC. */
  983. ret = gpmi_send_page(this, payload_phys, auxiliary_phys);
  984. if (ret)
  985. pr_err("Error in ECC-based write: %d\n", ret);
  986. if (!this->swap_block_mark) {
  987. send_page_end(this, chip->oob_poi, mtd->oobsize,
  988. this->auxiliary_virt, this->auxiliary_phys,
  989. nfc_geo->auxiliary_size,
  990. auxiliary_virt, auxiliary_phys);
  991. exit_auxiliary:
  992. send_page_end(this, buf, mtd->writesize,
  993. this->payload_virt, this->payload_phys,
  994. nfc_geo->payload_size,
  995. payload_virt, payload_phys);
  996. }
  997. return 0;
  998. }
  999. /*
  1000. * There are several places in this driver where we have to handle the OOB and
  1001. * block marks. This is the function where things are the most complicated, so
  1002. * this is where we try to explain it all. All the other places refer back to
  1003. * here.
  1004. *
  1005. * These are the rules, in order of decreasing importance:
  1006. *
  1007. * 1) Nothing the caller does can be allowed to imperil the block mark.
  1008. *
  1009. * 2) In read operations, the first byte of the OOB we return must reflect the
  1010. * true state of the block mark, no matter where that block mark appears in
  1011. * the physical page.
  1012. *
  1013. * 3) ECC-based read operations return an OOB full of set bits (since we never
  1014. * allow ECC-based writes to the OOB, it doesn't matter what ECC-based reads
  1015. * return).
  1016. *
  1017. * 4) "Raw" read operations return a direct view of the physical bytes in the
  1018. * page, using the conventional definition of which bytes are data and which
  1019. * are OOB. This gives the caller a way to see the actual, physical bytes
  1020. * in the page, without the distortions applied by our ECC engine.
  1021. *
  1022. *
  1023. * What we do for this specific read operation depends on two questions:
  1024. *
  1025. * 1) Are we doing a "raw" read, or an ECC-based read?
  1026. *
  1027. * 2) Are we using block mark swapping or transcription?
  1028. *
  1029. * There are four cases, illustrated by the following Karnaugh map:
  1030. *
  1031. * | Raw | ECC-based |
  1032. * -------------+-------------------------+-------------------------+
  1033. * | Read the conventional | |
  1034. * | OOB at the end of the | |
  1035. * Swapping | page and return it. It | |
  1036. * | contains exactly what | |
  1037. * | we want. | Read the block mark and |
  1038. * -------------+-------------------------+ return it in a buffer |
  1039. * | Read the conventional | full of set bits. |
  1040. * | OOB at the end of the | |
  1041. * | page and also the block | |
  1042. * Transcribing | mark in the metadata. | |
  1043. * | Copy the block mark | |
  1044. * | into the first byte of | |
  1045. * | the OOB. | |
  1046. * -------------+-------------------------+-------------------------+
  1047. *
  1048. * Note that we break rule #4 in the Transcribing/Raw case because we're not
  1049. * giving an accurate view of the actual, physical bytes in the page (we're
  1050. * overwriting the block mark). That's OK because it's more important to follow
  1051. * rule #2.
  1052. *
  1053. * It turns out that knowing whether we want an "ECC-based" or "raw" read is not
  1054. * easy. When reading a page, for example, the NAND Flash MTD code calls our
  1055. * ecc.read_page or ecc.read_page_raw function. Thus, the fact that MTD wants an
  1056. * ECC-based or raw view of the page is implicit in which function it calls
  1057. * (there is a similar pair of ECC-based/raw functions for writing).
  1058. *
  1059. * FIXME: The following paragraph is incorrect, now that there exist
  1060. * ecc.read_oob_raw and ecc.write_oob_raw functions.
  1061. *
  1062. * Since MTD assumes the OOB is not covered by ECC, there is no pair of
  1063. * ECC-based/raw functions for reading or or writing the OOB. The fact that the
  1064. * caller wants an ECC-based or raw view of the page is not propagated down to
  1065. * this driver.
  1066. */
  1067. static int gpmi_ecc_read_oob(struct mtd_info *mtd, struct nand_chip *chip,
  1068. int page)
  1069. {
  1070. struct gpmi_nand_data *this = chip->priv;
  1071. pr_debug("page number is %d\n", page);
  1072. /* clear the OOB buffer */
  1073. memset(chip->oob_poi, ~0, mtd->oobsize);
  1074. /* Read out the conventional OOB. */
  1075. chip->cmdfunc(mtd, NAND_CMD_READ0, mtd->writesize, page);
  1076. chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
  1077. /*
  1078. * Now, we want to make sure the block mark is correct. In the
  1079. * Swapping/Raw case, we already have it. Otherwise, we need to
  1080. * explicitly read it.
  1081. */
  1082. if (!this->swap_block_mark) {
  1083. /* Read the block mark into the first byte of the OOB buffer. */
  1084. chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
  1085. chip->oob_poi[0] = chip->read_byte(mtd);
  1086. }
  1087. return 0;
  1088. }
  1089. static int
  1090. gpmi_ecc_write_oob(struct mtd_info *mtd, struct nand_chip *chip, int page)
  1091. {
  1092. /*
  1093. * The BCH will use all the (page + oob).
  1094. * Our gpmi_hw_ecclayout can only prohibit the JFFS2 to write the oob.
  1095. * But it can not stop some ioctls such MEMWRITEOOB which uses
  1096. * MTD_OPS_PLACE_OOB. So We have to implement this function to prohibit
  1097. * these ioctls too.
  1098. */
  1099. return -EPERM;
  1100. }
  1101. static int gpmi_block_markbad(struct mtd_info *mtd, loff_t ofs)
  1102. {
  1103. struct nand_chip *chip = mtd->priv;
  1104. struct gpmi_nand_data *this = chip->priv;
  1105. int ret = 0;
  1106. uint8_t *block_mark;
  1107. int column, page, status, chipnr;
  1108. chipnr = (int)(ofs >> chip->chip_shift);
  1109. chip->select_chip(mtd, chipnr);
  1110. column = this->swap_block_mark ? mtd->writesize : 0;
  1111. /* Write the block mark. */
  1112. block_mark = this->data_buffer_dma;
  1113. block_mark[0] = 0; /* bad block marker */
  1114. /* Shift to get page */
  1115. page = (int)(ofs >> chip->page_shift);
  1116. chip->cmdfunc(mtd, NAND_CMD_SEQIN, column, page);
  1117. chip->write_buf(mtd, block_mark, 1);
  1118. chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
  1119. status = chip->waitfunc(mtd, chip);
  1120. if (status & NAND_STATUS_FAIL)
  1121. ret = -EIO;
  1122. chip->select_chip(mtd, -1);
  1123. return ret;
  1124. }
  1125. static int nand_boot_set_geometry(struct gpmi_nand_data *this)
  1126. {
  1127. struct boot_rom_geometry *geometry = &this->rom_geometry;
  1128. /*
  1129. * Set the boot block stride size.
  1130. *
  1131. * In principle, we should be reading this from the OTP bits, since
  1132. * that's where the ROM is going to get it. In fact, we don't have any
  1133. * way to read the OTP bits, so we go with the default and hope for the
  1134. * best.
  1135. */
  1136. geometry->stride_size_in_pages = 64;
  1137. /*
  1138. * Set the search area stride exponent.
  1139. *
  1140. * In principle, we should be reading this from the OTP bits, since
  1141. * that's where the ROM is going to get it. In fact, we don't have any
  1142. * way to read the OTP bits, so we go with the default and hope for the
  1143. * best.
  1144. */
  1145. geometry->search_area_stride_exponent = 2;
  1146. return 0;
  1147. }
  1148. static const char *fingerprint = "STMP";
  1149. static int mx23_check_transcription_stamp(struct gpmi_nand_data *this)
  1150. {
  1151. struct boot_rom_geometry *rom_geo = &this->rom_geometry;
  1152. struct device *dev = this->dev;
  1153. struct mtd_info *mtd = &this->mtd;
  1154. struct nand_chip *chip = &this->nand;
  1155. unsigned int search_area_size_in_strides;
  1156. unsigned int stride;
  1157. unsigned int page;
  1158. uint8_t *buffer = chip->buffers->databuf;
  1159. int saved_chip_number;
  1160. int found_an_ncb_fingerprint = false;
  1161. /* Compute the number of strides in a search area. */
  1162. search_area_size_in_strides = 1 << rom_geo->search_area_stride_exponent;
  1163. saved_chip_number = this->current_chip;
  1164. chip->select_chip(mtd, 0);
  1165. /*
  1166. * Loop through the first search area, looking for the NCB fingerprint.
  1167. */
  1168. dev_dbg(dev, "Scanning for an NCB fingerprint...\n");
  1169. for (stride = 0; stride < search_area_size_in_strides; stride++) {
  1170. /* Compute the page addresses. */
  1171. page = stride * rom_geo->stride_size_in_pages;
  1172. dev_dbg(dev, "Looking for a fingerprint in page 0x%x\n", page);
  1173. /*
  1174. * Read the NCB fingerprint. The fingerprint is four bytes long
  1175. * and starts in the 12th byte of the page.
  1176. */
  1177. chip->cmdfunc(mtd, NAND_CMD_READ0, 12, page);
  1178. chip->read_buf(mtd, buffer, strlen(fingerprint));
  1179. /* Look for the fingerprint. */
  1180. if (!memcmp(buffer, fingerprint, strlen(fingerprint))) {
  1181. found_an_ncb_fingerprint = true;
  1182. break;
  1183. }
  1184. }
  1185. chip->select_chip(mtd, saved_chip_number);
  1186. if (found_an_ncb_fingerprint)
  1187. dev_dbg(dev, "\tFound a fingerprint\n");
  1188. else
  1189. dev_dbg(dev, "\tNo fingerprint found\n");
  1190. return found_an_ncb_fingerprint;
  1191. }
  1192. /* Writes a transcription stamp. */
  1193. static int mx23_write_transcription_stamp(struct gpmi_nand_data *this)
  1194. {
  1195. struct device *dev = this->dev;
  1196. struct boot_rom_geometry *rom_geo = &this->rom_geometry;
  1197. struct mtd_info *mtd = &this->mtd;
  1198. struct nand_chip *chip = &this->nand;
  1199. unsigned int block_size_in_pages;
  1200. unsigned int search_area_size_in_strides;
  1201. unsigned int search_area_size_in_pages;
  1202. unsigned int search_area_size_in_blocks;
  1203. unsigned int block;
  1204. unsigned int stride;
  1205. unsigned int page;
  1206. uint8_t *buffer = chip->buffers->databuf;
  1207. int saved_chip_number;
  1208. int status;
  1209. /* Compute the search area geometry. */
  1210. block_size_in_pages = mtd->erasesize / mtd->writesize;
  1211. search_area_size_in_strides = 1 << rom_geo->search_area_stride_exponent;
  1212. search_area_size_in_pages = search_area_size_in_strides *
  1213. rom_geo->stride_size_in_pages;
  1214. search_area_size_in_blocks =
  1215. (search_area_size_in_pages + (block_size_in_pages - 1)) /
  1216. block_size_in_pages;
  1217. dev_dbg(dev, "Search Area Geometry :\n");
  1218. dev_dbg(dev, "\tin Blocks : %u\n", search_area_size_in_blocks);
  1219. dev_dbg(dev, "\tin Strides: %u\n", search_area_size_in_strides);
  1220. dev_dbg(dev, "\tin Pages : %u\n", search_area_size_in_pages);
  1221. /* Select chip 0. */
  1222. saved_chip_number = this->current_chip;
  1223. chip->select_chip(mtd, 0);
  1224. /* Loop over blocks in the first search area, erasing them. */
  1225. dev_dbg(dev, "Erasing the search area...\n");
  1226. for (block = 0; block < search_area_size_in_blocks; block++) {
  1227. /* Compute the page address. */
  1228. page = block * block_size_in_pages;
  1229. /* Erase this block. */
  1230. dev_dbg(dev, "\tErasing block 0x%x\n", block);
  1231. chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
  1232. chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
  1233. /* Wait for the erase to finish. */
  1234. status = chip->waitfunc(mtd, chip);
  1235. if (status & NAND_STATUS_FAIL)
  1236. dev_err(dev, "[%s] Erase failed.\n", __func__);
  1237. }
  1238. /* Write the NCB fingerprint into the page buffer. */
  1239. memset(buffer, ~0, mtd->writesize);
  1240. memset(chip->oob_poi, ~0, mtd->oobsize);
  1241. memcpy(buffer + 12, fingerprint, strlen(fingerprint));
  1242. /* Loop through the first search area, writing NCB fingerprints. */
  1243. dev_dbg(dev, "Writing NCB fingerprints...\n");
  1244. for (stride = 0; stride < search_area_size_in_strides; stride++) {
  1245. /* Compute the page addresses. */
  1246. page = stride * rom_geo->stride_size_in_pages;
  1247. /* Write the first page of the current stride. */
  1248. dev_dbg(dev, "Writing an NCB fingerprint in page 0x%x\n", page);
  1249. chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
  1250. chip->ecc.write_page_raw(mtd, chip, buffer, 0);
  1251. chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
  1252. /* Wait for the write to finish. */
  1253. status = chip->waitfunc(mtd, chip);
  1254. if (status & NAND_STATUS_FAIL)
  1255. dev_err(dev, "[%s] Write failed.\n", __func__);
  1256. }
  1257. /* Deselect chip 0. */
  1258. chip->select_chip(mtd, saved_chip_number);
  1259. return 0;
  1260. }
  1261. static int mx23_boot_init(struct gpmi_nand_data *this)
  1262. {
  1263. struct device *dev = this->dev;
  1264. struct nand_chip *chip = &this->nand;
  1265. struct mtd_info *mtd = &this->mtd;
  1266. unsigned int block_count;
  1267. unsigned int block;
  1268. int chipnr;
  1269. int page;
  1270. loff_t byte;
  1271. uint8_t block_mark;
  1272. int ret = 0;
  1273. /*
  1274. * If control arrives here, we can't use block mark swapping, which
  1275. * means we're forced to use transcription. First, scan for the
  1276. * transcription stamp. If we find it, then we don't have to do
  1277. * anything -- the block marks are already transcribed.
  1278. */
  1279. if (mx23_check_transcription_stamp(this))
  1280. return 0;
  1281. /*
  1282. * If control arrives here, we couldn't find a transcription stamp, so
  1283. * so we presume the block marks are in the conventional location.
  1284. */
  1285. dev_dbg(dev, "Transcribing bad block marks...\n");
  1286. /* Compute the number of blocks in the entire medium. */
  1287. block_count = chip->chipsize >> chip->phys_erase_shift;
  1288. /*
  1289. * Loop over all the blocks in the medium, transcribing block marks as
  1290. * we go.
  1291. */
  1292. for (block = 0; block < block_count; block++) {
  1293. /*
  1294. * Compute the chip, page and byte addresses for this block's
  1295. * conventional mark.
  1296. */
  1297. chipnr = block >> (chip->chip_shift - chip->phys_erase_shift);
  1298. page = block << (chip->phys_erase_shift - chip->page_shift);
  1299. byte = block << chip->phys_erase_shift;
  1300. /* Send the command to read the conventional block mark. */
  1301. chip->select_chip(mtd, chipnr);
  1302. chip->cmdfunc(mtd, NAND_CMD_READ0, mtd->writesize, page);
  1303. block_mark = chip->read_byte(mtd);
  1304. chip->select_chip(mtd, -1);
  1305. /*
  1306. * Check if the block is marked bad. If so, we need to mark it
  1307. * again, but this time the result will be a mark in the
  1308. * location where we transcribe block marks.
  1309. */
  1310. if (block_mark != 0xff) {
  1311. dev_dbg(dev, "Transcribing mark in block %u\n", block);
  1312. ret = chip->block_markbad(mtd, byte);
  1313. if (ret)
  1314. dev_err(dev, "Failed to mark block bad with "
  1315. "ret %d\n", ret);
  1316. }
  1317. }
  1318. /* Write the stamp that indicates we've transcribed the block marks. */
  1319. mx23_write_transcription_stamp(this);
  1320. return 0;
  1321. }
  1322. static int nand_boot_init(struct gpmi_nand_data *this)
  1323. {
  1324. nand_boot_set_geometry(this);
  1325. /* This is ROM arch-specific initilization before the BBT scanning. */
  1326. if (GPMI_IS_MX23(this))
  1327. return mx23_boot_init(this);
  1328. return 0;
  1329. }
  1330. static int gpmi_set_geometry(struct gpmi_nand_data *this)
  1331. {
  1332. int ret;
  1333. /* Free the temporary DMA memory for reading ID. */
  1334. gpmi_free_dma_buffer(this);
  1335. /* Set up the NFC geometry which is used by BCH. */
  1336. ret = bch_set_geometry(this);
  1337. if (ret) {
  1338. pr_err("Error setting BCH geometry : %d\n", ret);
  1339. return ret;
  1340. }
  1341. /* Alloc the new DMA buffers according to the pagesize and oobsize */
  1342. return gpmi_alloc_dma_buffer(this);
  1343. }
  1344. static int gpmi_pre_bbt_scan(struct gpmi_nand_data *this)
  1345. {
  1346. int ret;
  1347. /* Set up swap_block_mark, must be set before the gpmi_set_geometry() */
  1348. if (GPMI_IS_MX23(this))
  1349. this->swap_block_mark = false;
  1350. else
  1351. this->swap_block_mark = true;
  1352. /* Set up the medium geometry */
  1353. ret = gpmi_set_geometry(this);
  1354. if (ret)
  1355. return ret;
  1356. /* NAND boot init, depends on the gpmi_set_geometry(). */
  1357. return nand_boot_init(this);
  1358. }
  1359. static void gpmi_nfc_exit(struct gpmi_nand_data *this)
  1360. {
  1361. nand_release(&this->mtd);
  1362. gpmi_free_dma_buffer(this);
  1363. }
  1364. static int gpmi_init_last(struct gpmi_nand_data *this)
  1365. {
  1366. struct mtd_info *mtd = &this->mtd;
  1367. struct nand_chip *chip = mtd->priv;
  1368. struct nand_ecc_ctrl *ecc = &chip->ecc;
  1369. struct bch_geometry *bch_geo = &this->bch_geometry;
  1370. int ret;
  1371. /* Prepare for the BBT scan. */
  1372. ret = gpmi_pre_bbt_scan(this);
  1373. if (ret)
  1374. return ret;
  1375. /* Init the nand_ecc_ctrl{} */
  1376. ecc->read_page = gpmi_ecc_read_page;
  1377. ecc->write_page = gpmi_ecc_write_page;
  1378. ecc->read_oob = gpmi_ecc_read_oob;
  1379. ecc->write_oob = gpmi_ecc_write_oob;
  1380. ecc->mode = NAND_ECC_HW;
  1381. ecc->size = bch_geo->ecc_chunk_size;
  1382. ecc->strength = bch_geo->ecc_strength;
  1383. ecc->layout = &gpmi_hw_ecclayout;
  1384. /*
  1385. * Can we enable the extra features? such as EDO or Sync mode.
  1386. *
  1387. * We do not check the return value now. That's means if we fail in
  1388. * enable the extra features, we still can run in the normal way.
  1389. */
  1390. gpmi_extra_init(this);
  1391. return 0;
  1392. }
  1393. static int gpmi_nfc_init(struct gpmi_nand_data *this)
  1394. {
  1395. struct mtd_info *mtd = &this->mtd;
  1396. struct nand_chip *chip = &this->nand;
  1397. struct mtd_part_parser_data ppdata = {};
  1398. int ret;
  1399. /* init current chip */
  1400. this->current_chip = -1;
  1401. /* init the MTD data structures */
  1402. mtd->priv = chip;
  1403. mtd->name = "gpmi-nand";
  1404. mtd->owner = THIS_MODULE;
  1405. /* init the nand_chip{}, we don't support a 16-bit NAND Flash bus. */
  1406. chip->priv = this;
  1407. chip->select_chip = gpmi_select_chip;
  1408. chip->cmd_ctrl = gpmi_cmd_ctrl;
  1409. chip->dev_ready = gpmi_dev_ready;
  1410. chip->read_byte = gpmi_read_byte;
  1411. chip->read_buf = gpmi_read_buf;
  1412. chip->write_buf = gpmi_write_buf;
  1413. chip->badblock_pattern = &gpmi_bbt_descr;
  1414. chip->block_markbad = gpmi_block_markbad;
  1415. chip->options |= NAND_NO_SUBPAGE_WRITE;
  1416. if (of_get_nand_on_flash_bbt(this->dev->of_node))
  1417. chip->bbt_options |= NAND_BBT_USE_FLASH | NAND_BBT_NO_OOB;
  1418. /*
  1419. * Allocate a temporary DMA buffer for reading ID in the
  1420. * nand_scan_ident().
  1421. */
  1422. this->bch_geometry.payload_size = 1024;
  1423. this->bch_geometry.auxiliary_size = 128;
  1424. ret = gpmi_alloc_dma_buffer(this);
  1425. if (ret)
  1426. goto err_out;
  1427. ret = nand_scan_ident(mtd, 1, NULL);
  1428. if (ret)
  1429. goto err_out;
  1430. ret = gpmi_init_last(this);
  1431. if (ret)
  1432. goto err_out;
  1433. ret = nand_scan_tail(mtd);
  1434. if (ret)
  1435. goto err_out;
  1436. ppdata.of_node = this->pdev->dev.of_node;
  1437. ret = mtd_device_parse_register(mtd, NULL, &ppdata, NULL, 0);
  1438. if (ret)
  1439. goto err_out;
  1440. return 0;
  1441. err_out:
  1442. gpmi_nfc_exit(this);
  1443. return ret;
  1444. }
  1445. static const struct platform_device_id gpmi_ids[] = {
  1446. { .name = "imx23-gpmi-nand", .driver_data = IS_MX23, },
  1447. { .name = "imx28-gpmi-nand", .driver_data = IS_MX28, },
  1448. { .name = "imx6q-gpmi-nand", .driver_data = IS_MX6Q, },
  1449. {},
  1450. };
  1451. static const struct of_device_id gpmi_nand_id_table[] = {
  1452. {
  1453. .compatible = "fsl,imx23-gpmi-nand",
  1454. .data = (void *)&gpmi_ids[IS_MX23]
  1455. }, {
  1456. .compatible = "fsl,imx28-gpmi-nand",
  1457. .data = (void *)&gpmi_ids[IS_MX28]
  1458. }, {
  1459. .compatible = "fsl,imx6q-gpmi-nand",
  1460. .data = (void *)&gpmi_ids[IS_MX6Q]
  1461. }, {}
  1462. };
  1463. MODULE_DEVICE_TABLE(of, gpmi_nand_id_table);
  1464. static int gpmi_nand_probe(struct platform_device *pdev)
  1465. {
  1466. struct gpmi_nand_data *this;
  1467. const struct of_device_id *of_id;
  1468. int ret;
  1469. of_id = of_match_device(gpmi_nand_id_table, &pdev->dev);
  1470. if (of_id) {
  1471. pdev->id_entry = of_id->data;
  1472. } else {
  1473. pr_err("Failed to find the right device id.\n");
  1474. return -ENODEV;
  1475. }
  1476. this = kzalloc(sizeof(*this), GFP_KERNEL);
  1477. if (!this) {
  1478. pr_err("Failed to allocate per-device memory\n");
  1479. return -ENOMEM;
  1480. }
  1481. platform_set_drvdata(pdev, this);
  1482. this->pdev = pdev;
  1483. this->dev = &pdev->dev;
  1484. ret = acquire_resources(this);
  1485. if (ret)
  1486. goto exit_acquire_resources;
  1487. ret = init_hardware(this);
  1488. if (ret)
  1489. goto exit_nfc_init;
  1490. ret = gpmi_nfc_init(this);
  1491. if (ret)
  1492. goto exit_nfc_init;
  1493. dev_info(this->dev, "driver registered.\n");
  1494. return 0;
  1495. exit_nfc_init:
  1496. release_resources(this);
  1497. exit_acquire_resources:
  1498. dev_err(this->dev, "driver registration failed: %d\n", ret);
  1499. kfree(this);
  1500. return ret;
  1501. }
  1502. static int gpmi_nand_remove(struct platform_device *pdev)
  1503. {
  1504. struct gpmi_nand_data *this = platform_get_drvdata(pdev);
  1505. gpmi_nfc_exit(this);
  1506. release_resources(this);
  1507. kfree(this);
  1508. return 0;
  1509. }
  1510. static struct platform_driver gpmi_nand_driver = {
  1511. .driver = {
  1512. .name = "gpmi-nand",
  1513. .of_match_table = gpmi_nand_id_table,
  1514. },
  1515. .probe = gpmi_nand_probe,
  1516. .remove = gpmi_nand_remove,
  1517. .id_table = gpmi_ids,
  1518. };
  1519. module_platform_driver(gpmi_nand_driver);
  1520. MODULE_AUTHOR("Freescale Semiconductor, Inc.");
  1521. MODULE_DESCRIPTION("i.MX GPMI NAND Flash Controller Driver");
  1522. MODULE_LICENSE("GPL");