gpmi-nand.c 45 KB

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