gpmi-nand.c 46 KB

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