gpmi-nand.c 45 KB

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