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