gpmi-nand.c 45 KB

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