gpmi-nand.c 45 KB

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