Version:  2.0.40 2.2.26 2.4.37 3.0 3.1 3.2 3.3 3.4 3.5 3.6 3.7 3.8 3.9 3.10 3.11 3.12 3.13 3.14 3.15 3.16

Linux/drivers/mtd/nand/gpmi-nand/gpmi-nand.c

  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/partitions.h>
 26 #include <linux/of.h>
 27 #include <linux/of_device.h>
 28 #include <linux/of_mtd.h>
 29 #include "gpmi-nand.h"
 30 #include "bch-regs.h"
 31 
 32 /* Resource names for the GPMI NAND driver. */
 33 #define GPMI_NAND_GPMI_REGS_ADDR_RES_NAME  "gpmi-nand"
 34 #define GPMI_NAND_BCH_REGS_ADDR_RES_NAME   "bch"
 35 #define GPMI_NAND_BCH_INTERRUPT_RES_NAME   "bch"
 36 
 37 /* add our owner bbt descriptor */
 38 static uint8_t scan_ff_pattern[] = { 0xff };
 39 static struct nand_bbt_descr gpmi_bbt_descr = {
 40         .options        = 0,
 41         .offs           = 0,
 42         .len            = 1,
 43         .pattern        = scan_ff_pattern
 44 };
 45 
 46 /*
 47  * We may change the layout if we can get the ECC info from the datasheet,
 48  * else we will use all the (page + OOB).
 49  */
 50 static struct nand_ecclayout gpmi_hw_ecclayout = {
 51         .eccbytes = 0,
 52         .eccpos = { 0, },
 53         .oobfree = { {.offset = 0, .length = 0} }
 54 };
 55 
 56 static const struct gpmi_devdata gpmi_devdata_imx23 = {
 57         .type = IS_MX23,
 58         .bch_max_ecc_strength = 20,
 59         .max_chain_delay = 16,
 60 };
 61 
 62 static const struct gpmi_devdata gpmi_devdata_imx28 = {
 63         .type = IS_MX28,
 64         .bch_max_ecc_strength = 20,
 65         .max_chain_delay = 16,
 66 };
 67 
 68 static const struct gpmi_devdata gpmi_devdata_imx6q = {
 69         .type = IS_MX6Q,
 70         .bch_max_ecc_strength = 40,
 71         .max_chain_delay = 12,
 72 };
 73 
 74 static const struct gpmi_devdata gpmi_devdata_imx6sx = {
 75         .type = IS_MX6SX,
 76         .bch_max_ecc_strength = 62,
 77         .max_chain_delay = 12,
 78 };
 79 
 80 static irqreturn_t bch_irq(int irq, void *cookie)
 81 {
 82         struct gpmi_nand_data *this = cookie;
 83 
 84         gpmi_clear_bch(this);
 85         complete(&this->bch_done);
 86         return IRQ_HANDLED;
 87 }
 88 
 89 /*
 90  *  Calculate the ECC strength by hand:
 91  *      E : The ECC strength.
 92  *      G : the length of Galois Field.
 93  *      N : The chunk count of per page.
 94  *      O : the oobsize of the NAND chip.
 95  *      M : the metasize of per page.
 96  *
 97  *      The formula is :
 98  *              E * G * N
 99  *            ------------ <= (O - M)
100  *                  8
101  *
102  *      So, we get E by:
103  *                    (O - M) * 8
104  *              E <= -------------
105  *                       G * N
106  */
107 static inline int get_ecc_strength(struct gpmi_nand_data *this)
108 {
109         struct bch_geometry *geo = &this->bch_geometry;
110         struct mtd_info *mtd = &this->mtd;
111         int ecc_strength;
112 
113         ecc_strength = ((mtd->oobsize - geo->metadata_size) * 8)
114                         / (geo->gf_len * geo->ecc_chunk_count);
115 
116         /* We need the minor even number. */
117         return round_down(ecc_strength, 2);
118 }
119 
120 static inline bool gpmi_check_ecc(struct gpmi_nand_data *this)
121 {
122         struct bch_geometry *geo = &this->bch_geometry;
123 
124         /* Do the sanity check. */
125         if (GPMI_IS_MX23(this) || GPMI_IS_MX28(this)) {
126                 /* The mx23/mx28 only support the GF13. */
127                 if (geo->gf_len == 14)
128                         return false;
129         }
130         return geo->ecc_strength <= this->devdata->bch_max_ecc_strength;
131 }
132 
133 /*
134  * If we can get the ECC information from the nand chip, we do not
135  * need to calculate them ourselves.
136  *
137  * We may have available oob space in this case.
138  */
139 static bool set_geometry_by_ecc_info(struct gpmi_nand_data *this)
140 {
141         struct bch_geometry *geo = &this->bch_geometry;
142         struct mtd_info *mtd = &this->mtd;
143         struct nand_chip *chip = mtd->priv;
144         struct nand_oobfree *of = gpmi_hw_ecclayout.oobfree;
145         unsigned int block_mark_bit_offset;
146 
147         if (!(chip->ecc_strength_ds > 0 && chip->ecc_step_ds > 0))
148                 return false;
149 
150         switch (chip->ecc_step_ds) {
151         case SZ_512:
152                 geo->gf_len = 13;
153                 break;
154         case SZ_1K:
155                 geo->gf_len = 14;
156                 break;
157         default:
158                 dev_err(this->dev,
159                         "unsupported nand chip. ecc bits : %d, ecc size : %d\n",
160                         chip->ecc_strength_ds, chip->ecc_step_ds);
161                 return false;
162         }
163         geo->ecc_chunk_size = chip->ecc_step_ds;
164         geo->ecc_strength = round_up(chip->ecc_strength_ds, 2);
165         if (!gpmi_check_ecc(this))
166                 return false;
167 
168         /* Keep the C >= O */
169         if (geo->ecc_chunk_size < mtd->oobsize) {
170                 dev_err(this->dev,
171                         "unsupported nand chip. ecc size: %d, oob size : %d\n",
172                         chip->ecc_step_ds, mtd->oobsize);
173                 return false;
174         }
175 
176         /* The default value, see comment in the legacy_set_geometry(). */
177         geo->metadata_size = 10;
178 
179         geo->ecc_chunk_count = mtd->writesize / geo->ecc_chunk_size;
180 
181         /*
182          * Now, the NAND chip with 2K page(data chunk is 512byte) shows below:
183          *
184          *    |                          P                            |
185          *    |<----------------------------------------------------->|
186          *    |                                                       |
187          *    |                                        (Block Mark)   |
188          *    |                      P'                      |      | |     |
189          *    |<-------------------------------------------->|  D   | |  O' |
190          *    |                                              |<---->| |<--->|
191          *    V                                              V      V V     V
192          *    +---+----------+-+----------+-+----------+-+----------+-+-----+
193          *    | M |   data   |E|   data   |E|   data   |E|   data   |E|     |
194          *    +---+----------+-+----------+-+----------+-+----------+-+-----+
195          *                                                   ^              ^
196          *                                                   |      O       |
197          *                                                   |<------------>|
198          *                                                   |              |
199          *
200          *      P : the page size for BCH module.
201          *      E : The ECC strength.
202          *      G : the length of Galois Field.
203          *      N : The chunk count of per page.
204          *      M : the metasize of per page.
205          *      C : the ecc chunk size, aka the "data" above.
206          *      P': the nand chip's page size.
207          *      O : the nand chip's oob size.
208          *      O': the free oob.
209          *
210          *      The formula for P is :
211          *
212          *                  E * G * N
213          *             P = ------------ + P' + M
214          *                      8
215          *
216          * The position of block mark moves forward in the ECC-based view
217          * of page, and the delta is:
218          *
219          *                   E * G * (N - 1)
220          *             D = (---------------- + M)
221          *                          8
222          *
223          * Please see the comment in legacy_set_geometry().
224          * With the condition C >= O , we still can get same result.
225          * So the bit position of the physical block mark within the ECC-based
226          * view of the page is :
227          *             (P' - D) * 8
228          */
229         geo->page_size = mtd->writesize + geo->metadata_size +
230                 (geo->gf_len * geo->ecc_strength * geo->ecc_chunk_count) / 8;
231 
232         /* The available oob size we have. */
233         if (geo->page_size < mtd->writesize + mtd->oobsize) {
234                 of->offset = geo->page_size - mtd->writesize;
235                 of->length = mtd->oobsize - of->offset;
236         }
237 
238         geo->payload_size = mtd->writesize;
239 
240         geo->auxiliary_status_offset = ALIGN(geo->metadata_size, 4);
241         geo->auxiliary_size = ALIGN(geo->metadata_size, 4)
242                                 + ALIGN(geo->ecc_chunk_count, 4);
243 
244         if (!this->swap_block_mark)
245                 return true;
246 
247         /* For bit swap. */
248         block_mark_bit_offset = mtd->writesize * 8 -
249                 (geo->ecc_strength * geo->gf_len * (geo->ecc_chunk_count - 1)
250                                 + geo->metadata_size * 8);
251 
252         geo->block_mark_byte_offset = block_mark_bit_offset / 8;
253         geo->block_mark_bit_offset  = block_mark_bit_offset % 8;
254         return true;
255 }
256 
257 static int legacy_set_geometry(struct gpmi_nand_data *this)
258 {
259         struct bch_geometry *geo = &this->bch_geometry;
260         struct mtd_info *mtd = &this->mtd;
261         unsigned int metadata_size;
262         unsigned int status_size;
263         unsigned int block_mark_bit_offset;
264 
265         /*
266          * The size of the metadata can be changed, though we set it to 10
267          * bytes now. But it can't be too large, because we have to save
268          * enough space for BCH.
269          */
270         geo->metadata_size = 10;
271 
272         /* The default for the length of Galois Field. */
273         geo->gf_len = 13;
274 
275         /* The default for chunk size. */
276         geo->ecc_chunk_size = 512;
277         while (geo->ecc_chunk_size < mtd->oobsize) {
278                 geo->ecc_chunk_size *= 2; /* keep C >= O */
279                 geo->gf_len = 14;
280         }
281 
282         geo->ecc_chunk_count = mtd->writesize / geo->ecc_chunk_size;
283 
284         /* We use the same ECC strength for all chunks. */
285         geo->ecc_strength = get_ecc_strength(this);
286         if (!gpmi_check_ecc(this)) {
287                 dev_err(this->dev,
288                         "We can not support this nand chip."
289                         " Its required ecc strength(%d) is beyond our"
290                         " capability(%d).\n", geo->ecc_strength,
291                         this->devdata->bch_max_ecc_strength);
292                 return -EINVAL;
293         }
294 
295         geo->page_size = mtd->writesize + mtd->oobsize;
296         geo->payload_size = mtd->writesize;
297 
298         /*
299          * The auxiliary buffer contains the metadata and the ECC status. The
300          * metadata is padded to the nearest 32-bit boundary. The ECC status
301          * contains one byte for every ECC chunk, and is also padded to the
302          * nearest 32-bit boundary.
303          */
304         metadata_size = ALIGN(geo->metadata_size, 4);
305         status_size   = ALIGN(geo->ecc_chunk_count, 4);
306 
307         geo->auxiliary_size = metadata_size + status_size;
308         geo->auxiliary_status_offset = metadata_size;
309 
310         if (!this->swap_block_mark)
311                 return 0;
312 
313         /*
314          * We need to compute the byte and bit offsets of
315          * the physical block mark within the ECC-based view of the page.
316          *
317          * NAND chip with 2K page shows below:
318          *                                             (Block Mark)
319          *                                                   |      |
320          *                                                   |  D   |
321          *                                                   |<---->|
322          *                                                   V      V
323          *    +---+----------+-+----------+-+----------+-+----------+-+
324          *    | M |   data   |E|   data   |E|   data   |E|   data   |E|
325          *    +---+----------+-+----------+-+----------+-+----------+-+
326          *
327          * The position of block mark moves forward in the ECC-based view
328          * of page, and the delta is:
329          *
330          *                   E * G * (N - 1)
331          *             D = (---------------- + M)
332          *                          8
333          *
334          * With the formula to compute the ECC strength, and the condition
335          *       : C >= O         (C is the ecc chunk size)
336          *
337          * It's easy to deduce to the following result:
338          *
339          *         E * G       (O - M)      C - M         C - M
340          *      ----------- <= ------- <=  --------  <  ---------
341          *           8            N           N          (N - 1)
342          *
343          *  So, we get:
344          *
345          *                   E * G * (N - 1)
346          *             D = (---------------- + M) < C
347          *                          8
348          *
349          *  The above inequality means the position of block mark
350          *  within the ECC-based view of the page is still in the data chunk,
351          *  and it's NOT in the ECC bits of the chunk.
352          *
353          *  Use the following to compute the bit position of the
354          *  physical block mark within the ECC-based view of the page:
355          *          (page_size - D) * 8
356          *
357          *  --Huang Shijie
358          */
359         block_mark_bit_offset = mtd->writesize * 8 -
360                 (geo->ecc_strength * geo->gf_len * (geo->ecc_chunk_count - 1)
361                                 + geo->metadata_size * 8);
362 
363         geo->block_mark_byte_offset = block_mark_bit_offset / 8;
364         geo->block_mark_bit_offset  = block_mark_bit_offset % 8;
365         return 0;
366 }
367 
368 int common_nfc_set_geometry(struct gpmi_nand_data *this)
369 {
370         if (of_property_read_bool(this->dev->of_node, "fsl,use-minimum-ecc")
371                 && set_geometry_by_ecc_info(this))
372                 return 0;
373         return legacy_set_geometry(this);
374 }
375 
376 struct dma_chan *get_dma_chan(struct gpmi_nand_data *this)
377 {
378         /* We use the DMA channel 0 to access all the nand chips. */
379         return this->dma_chans[0];
380 }
381 
382 /* Can we use the upper's buffer directly for DMA? */
383 void prepare_data_dma(struct gpmi_nand_data *this, enum dma_data_direction dr)
384 {
385         struct scatterlist *sgl = &this->data_sgl;
386         int ret;
387 
388         /* first try to map the upper buffer directly */
389         if (virt_addr_valid(this->upper_buf) &&
390                 !object_is_on_stack(this->upper_buf)) {
391                 sg_init_one(sgl, this->upper_buf, this->upper_len);
392                 ret = dma_map_sg(this->dev, sgl, 1, dr);
393                 if (ret == 0)
394                         goto map_fail;
395 
396                 this->direct_dma_map_ok = true;
397                 return;
398         }
399 
400 map_fail:
401         /* We have to use our own DMA buffer. */
402         sg_init_one(sgl, this->data_buffer_dma, this->upper_len);
403 
404         if (dr == DMA_TO_DEVICE)
405                 memcpy(this->data_buffer_dma, this->upper_buf, this->upper_len);
406 
407         dma_map_sg(this->dev, sgl, 1, dr);
408 
409         this->direct_dma_map_ok = false;
410 }
411 
412 /* This will be called after the DMA operation is finished. */
413 static void dma_irq_callback(void *param)
414 {
415         struct gpmi_nand_data *this = param;
416         struct completion *dma_c = &this->dma_done;
417 
418         switch (this->dma_type) {
419         case DMA_FOR_COMMAND:
420                 dma_unmap_sg(this->dev, &this->cmd_sgl, 1, DMA_TO_DEVICE);
421                 break;
422 
423         case DMA_FOR_READ_DATA:
424                 dma_unmap_sg(this->dev, &this->data_sgl, 1, DMA_FROM_DEVICE);
425                 if (this->direct_dma_map_ok == false)
426                         memcpy(this->upper_buf, this->data_buffer_dma,
427                                 this->upper_len);
428                 break;
429 
430         case DMA_FOR_WRITE_DATA:
431                 dma_unmap_sg(this->dev, &this->data_sgl, 1, DMA_TO_DEVICE);
432                 break;
433 
434         case DMA_FOR_READ_ECC_PAGE:
435         case DMA_FOR_WRITE_ECC_PAGE:
436                 /* We have to wait the BCH interrupt to finish. */
437                 break;
438 
439         default:
440                 dev_err(this->dev, "in wrong DMA operation.\n");
441         }
442 
443         complete(dma_c);
444 }
445 
446 int start_dma_without_bch_irq(struct gpmi_nand_data *this,
447                                 struct dma_async_tx_descriptor *desc)
448 {
449         struct completion *dma_c = &this->dma_done;
450         int err;
451 
452         init_completion(dma_c);
453 
454         desc->callback          = dma_irq_callback;
455         desc->callback_param    = this;
456         dmaengine_submit(desc);
457         dma_async_issue_pending(get_dma_chan(this));
458 
459         /* Wait for the interrupt from the DMA block. */
460         err = wait_for_completion_timeout(dma_c, msecs_to_jiffies(1000));
461         if (!err) {
462                 dev_err(this->dev, "DMA timeout, last DMA :%d\n",
463                         this->last_dma_type);
464                 gpmi_dump_info(this);
465                 return -ETIMEDOUT;
466         }
467         return 0;
468 }
469 
470 /*
471  * This function is used in BCH reading or BCH writing pages.
472  * It will wait for the BCH interrupt as long as ONE second.
473  * Actually, we must wait for two interrupts :
474  *      [1] firstly the DMA interrupt and
475  *      [2] secondly the BCH interrupt.
476  */
477 int start_dma_with_bch_irq(struct gpmi_nand_data *this,
478                         struct dma_async_tx_descriptor *desc)
479 {
480         struct completion *bch_c = &this->bch_done;
481         int err;
482 
483         /* Prepare to receive an interrupt from the BCH block. */
484         init_completion(bch_c);
485 
486         /* start the DMA */
487         start_dma_without_bch_irq(this, desc);
488 
489         /* Wait for the interrupt from the BCH block. */
490         err = wait_for_completion_timeout(bch_c, msecs_to_jiffies(1000));
491         if (!err) {
492                 dev_err(this->dev, "BCH timeout, last DMA :%d\n",
493                         this->last_dma_type);
494                 gpmi_dump_info(this);
495                 return -ETIMEDOUT;
496         }
497         return 0;
498 }
499 
500 static int acquire_register_block(struct gpmi_nand_data *this,
501                                   const char *res_name)
502 {
503         struct platform_device *pdev = this->pdev;
504         struct resources *res = &this->resources;
505         struct resource *r;
506         void __iomem *p;
507 
508         r = platform_get_resource_byname(pdev, IORESOURCE_MEM, res_name);
509         p = devm_ioremap_resource(&pdev->dev, r);
510         if (IS_ERR(p))
511                 return PTR_ERR(p);
512 
513         if (!strcmp(res_name, GPMI_NAND_GPMI_REGS_ADDR_RES_NAME))
514                 res->gpmi_regs = p;
515         else if (!strcmp(res_name, GPMI_NAND_BCH_REGS_ADDR_RES_NAME))
516                 res->bch_regs = p;
517         else
518                 dev_err(this->dev, "unknown resource name : %s\n", res_name);
519 
520         return 0;
521 }
522 
523 static int acquire_bch_irq(struct gpmi_nand_data *this, irq_handler_t irq_h)
524 {
525         struct platform_device *pdev = this->pdev;
526         const char *res_name = GPMI_NAND_BCH_INTERRUPT_RES_NAME;
527         struct resource *r;
528         int err;
529 
530         r = platform_get_resource_byname(pdev, IORESOURCE_IRQ, res_name);
531         if (!r) {
532                 dev_err(this->dev, "Can't get resource for %s\n", res_name);
533                 return -ENODEV;
534         }
535 
536         err = devm_request_irq(this->dev, r->start, irq_h, 0, res_name, this);
537         if (err)
538                 dev_err(this->dev, "error requesting BCH IRQ\n");
539 
540         return err;
541 }
542 
543 static void release_dma_channels(struct gpmi_nand_data *this)
544 {
545         unsigned int i;
546         for (i = 0; i < DMA_CHANS; i++)
547                 if (this->dma_chans[i]) {
548                         dma_release_channel(this->dma_chans[i]);
549                         this->dma_chans[i] = NULL;
550                 }
551 }
552 
553 static int acquire_dma_channels(struct gpmi_nand_data *this)
554 {
555         struct platform_device *pdev = this->pdev;
556         struct dma_chan *dma_chan;
557 
558         /* request dma channel */
559         dma_chan = dma_request_slave_channel(&pdev->dev, "rx-tx");
560         if (!dma_chan) {
561                 dev_err(this->dev, "Failed to request DMA channel.\n");
562                 goto acquire_err;
563         }
564 
565         this->dma_chans[0] = dma_chan;
566         return 0;
567 
568 acquire_err:
569         release_dma_channels(this);
570         return -EINVAL;
571 }
572 
573 static char *extra_clks_for_mx6q[GPMI_CLK_MAX] = {
574         "gpmi_apb", "gpmi_bch", "gpmi_bch_apb", "per1_bch",
575 };
576 
577 static int gpmi_get_clks(struct gpmi_nand_data *this)
578 {
579         struct resources *r = &this->resources;
580         char **extra_clks = NULL;
581         struct clk *clk;
582         int err, i;
583 
584         /* The main clock is stored in the first. */
585         r->clock[0] = devm_clk_get(this->dev, "gpmi_io");
586         if (IS_ERR(r->clock[0])) {
587                 err = PTR_ERR(r->clock[0]);
588                 goto err_clock;
589         }
590 
591         /* Get extra clocks */
592         if (GPMI_IS_MX6(this))
593                 extra_clks = extra_clks_for_mx6q;
594         if (!extra_clks)
595                 return 0;
596 
597         for (i = 1; i < GPMI_CLK_MAX; i++) {
598                 if (extra_clks[i - 1] == NULL)
599                         break;
600 
601                 clk = devm_clk_get(this->dev, extra_clks[i - 1]);
602                 if (IS_ERR(clk)) {
603                         err = PTR_ERR(clk);
604                         goto err_clock;
605                 }
606 
607                 r->clock[i] = clk;
608         }
609 
610         if (GPMI_IS_MX6(this))
611                 /*
612                  * Set the default value for the gpmi clock.
613                  *
614                  * If you want to use the ONFI nand which is in the
615                  * Synchronous Mode, you should change the clock as you need.
616                  */
617                 clk_set_rate(r->clock[0], 22000000);
618 
619         return 0;
620 
621 err_clock:
622         dev_dbg(this->dev, "failed in finding the clocks.\n");
623         return err;
624 }
625 
626 static int acquire_resources(struct gpmi_nand_data *this)
627 {
628         int ret;
629 
630         ret = acquire_register_block(this, GPMI_NAND_GPMI_REGS_ADDR_RES_NAME);
631         if (ret)
632                 goto exit_regs;
633 
634         ret = acquire_register_block(this, GPMI_NAND_BCH_REGS_ADDR_RES_NAME);
635         if (ret)
636                 goto exit_regs;
637 
638         ret = acquire_bch_irq(this, bch_irq);
639         if (ret)
640                 goto exit_regs;
641 
642         ret = acquire_dma_channels(this);
643         if (ret)
644                 goto exit_regs;
645 
646         ret = gpmi_get_clks(this);
647         if (ret)
648                 goto exit_clock;
649         return 0;
650 
651 exit_clock:
652         release_dma_channels(this);
653 exit_regs:
654         return ret;
655 }
656 
657 static void release_resources(struct gpmi_nand_data *this)
658 {
659         release_dma_channels(this);
660 }
661 
662 static int init_hardware(struct gpmi_nand_data *this)
663 {
664         int ret;
665 
666         /*
667          * This structure contains the "safe" GPMI timing that should succeed
668          * with any NAND Flash device
669          * (although, with less-than-optimal performance).
670          */
671         struct nand_timing  safe_timing = {
672                 .data_setup_in_ns        = 80,
673                 .data_hold_in_ns         = 60,
674                 .address_setup_in_ns     = 25,
675                 .gpmi_sample_delay_in_ns =  6,
676                 .tREA_in_ns              = -1,
677                 .tRLOH_in_ns             = -1,
678                 .tRHOH_in_ns             = -1,
679         };
680 
681         /* Initialize the hardwares. */
682         ret = gpmi_init(this);
683         if (ret)
684                 return ret;
685 
686         this->timing = safe_timing;
687         return 0;
688 }
689 
690 static int read_page_prepare(struct gpmi_nand_data *this,
691                         void *destination, unsigned length,
692                         void *alt_virt, dma_addr_t alt_phys, unsigned alt_size,
693                         void **use_virt, dma_addr_t *use_phys)
694 {
695         struct device *dev = this->dev;
696 
697         if (virt_addr_valid(destination)) {
698                 dma_addr_t dest_phys;
699 
700                 dest_phys = dma_map_single(dev, destination,
701                                                 length, DMA_FROM_DEVICE);
702                 if (dma_mapping_error(dev, dest_phys)) {
703                         if (alt_size < length) {
704                                 dev_err(dev, "Alternate buffer is too small\n");
705                                 return -ENOMEM;
706                         }
707                         goto map_failed;
708                 }
709                 *use_virt = destination;
710                 *use_phys = dest_phys;
711                 this->direct_dma_map_ok = true;
712                 return 0;
713         }
714 
715 map_failed:
716         *use_virt = alt_virt;
717         *use_phys = alt_phys;
718         this->direct_dma_map_ok = false;
719         return 0;
720 }
721 
722 static inline void read_page_end(struct gpmi_nand_data *this,
723                         void *destination, unsigned length,
724                         void *alt_virt, dma_addr_t alt_phys, unsigned alt_size,
725                         void *used_virt, dma_addr_t used_phys)
726 {
727         if (this->direct_dma_map_ok)
728                 dma_unmap_single(this->dev, used_phys, length, DMA_FROM_DEVICE);
729 }
730 
731 static inline void read_page_swap_end(struct gpmi_nand_data *this,
732                         void *destination, unsigned length,
733                         void *alt_virt, dma_addr_t alt_phys, unsigned alt_size,
734                         void *used_virt, dma_addr_t used_phys)
735 {
736         if (!this->direct_dma_map_ok)
737                 memcpy(destination, alt_virt, length);
738 }
739 
740 static int send_page_prepare(struct gpmi_nand_data *this,
741                         const void *source, unsigned length,
742                         void *alt_virt, dma_addr_t alt_phys, unsigned alt_size,
743                         const void **use_virt, dma_addr_t *use_phys)
744 {
745         struct device *dev = this->dev;
746 
747         if (virt_addr_valid(source)) {
748                 dma_addr_t source_phys;
749 
750                 source_phys = dma_map_single(dev, (void *)source, length,
751                                                 DMA_TO_DEVICE);
752                 if (dma_mapping_error(dev, source_phys)) {
753                         if (alt_size < length) {
754                                 dev_err(dev, "Alternate buffer is too small\n");
755                                 return -ENOMEM;
756                         }
757                         goto map_failed;
758                 }
759                 *use_virt = source;
760                 *use_phys = source_phys;
761                 return 0;
762         }
763 map_failed:
764         /*
765          * Copy the content of the source buffer into the alternate
766          * buffer and set up the return values accordingly.
767          */
768         memcpy(alt_virt, source, length);
769 
770         *use_virt = alt_virt;
771         *use_phys = alt_phys;
772         return 0;
773 }
774 
775 static void send_page_end(struct gpmi_nand_data *this,
776                         const void *source, unsigned length,
777                         void *alt_virt, dma_addr_t alt_phys, unsigned alt_size,
778                         const void *used_virt, dma_addr_t used_phys)
779 {
780         struct device *dev = this->dev;
781         if (used_virt == source)
782                 dma_unmap_single(dev, used_phys, length, DMA_TO_DEVICE);
783 }
784 
785 static void gpmi_free_dma_buffer(struct gpmi_nand_data *this)
786 {
787         struct device *dev = this->dev;
788 
789         if (this->page_buffer_virt && virt_addr_valid(this->page_buffer_virt))
790                 dma_free_coherent(dev, this->page_buffer_size,
791                                         this->page_buffer_virt,
792                                         this->page_buffer_phys);
793         kfree(this->cmd_buffer);
794         kfree(this->data_buffer_dma);
795 
796         this->cmd_buffer        = NULL;
797         this->data_buffer_dma   = NULL;
798         this->page_buffer_virt  = NULL;
799         this->page_buffer_size  =  0;
800 }
801 
802 /* Allocate the DMA buffers */
803 static int gpmi_alloc_dma_buffer(struct gpmi_nand_data *this)
804 {
805         struct bch_geometry *geo = &this->bch_geometry;
806         struct device *dev = this->dev;
807         struct mtd_info *mtd = &this->mtd;
808 
809         /* [1] Allocate a command buffer. PAGE_SIZE is enough. */
810         this->cmd_buffer = kzalloc(PAGE_SIZE, GFP_DMA | GFP_KERNEL);
811         if (this->cmd_buffer == NULL)
812                 goto error_alloc;
813 
814         /*
815          * [2] Allocate a read/write data buffer.
816          *     The gpmi_alloc_dma_buffer can be called twice.
817          *     We allocate a PAGE_SIZE length buffer if gpmi_alloc_dma_buffer
818          *     is called before the nand_scan_ident; and we allocate a buffer
819          *     of the real NAND page size when the gpmi_alloc_dma_buffer is
820          *     called after the nand_scan_ident.
821          */
822         this->data_buffer_dma = kzalloc(mtd->writesize ?: PAGE_SIZE,
823                                         GFP_DMA | GFP_KERNEL);
824         if (this->data_buffer_dma == NULL)
825                 goto error_alloc;
826 
827         /*
828          * [3] Allocate the page buffer.
829          *
830          * Both the payload buffer and the auxiliary buffer must appear on
831          * 32-bit boundaries. We presume the size of the payload buffer is a
832          * power of two and is much larger than four, which guarantees the
833          * auxiliary buffer will appear on a 32-bit boundary.
834          */
835         this->page_buffer_size = geo->payload_size + geo->auxiliary_size;
836         this->page_buffer_virt = dma_alloc_coherent(dev, this->page_buffer_size,
837                                         &this->page_buffer_phys, GFP_DMA);
838         if (!this->page_buffer_virt)
839                 goto error_alloc;
840 
841 
842         /* Slice up the page buffer. */
843         this->payload_virt = this->page_buffer_virt;
844         this->payload_phys = this->page_buffer_phys;
845         this->auxiliary_virt = this->payload_virt + geo->payload_size;
846         this->auxiliary_phys = this->payload_phys + geo->payload_size;
847         return 0;
848 
849 error_alloc:
850         gpmi_free_dma_buffer(this);
851         return -ENOMEM;
852 }
853 
854 static void gpmi_cmd_ctrl(struct mtd_info *mtd, int data, unsigned int ctrl)
855 {
856         struct nand_chip *chip = mtd->priv;
857         struct gpmi_nand_data *this = chip->priv;
858         int ret;
859 
860         /*
861          * Every operation begins with a command byte and a series of zero or
862          * more address bytes. These are distinguished by either the Address
863          * Latch Enable (ALE) or Command Latch Enable (CLE) signals being
864          * asserted. When MTD is ready to execute the command, it will deassert
865          * both latch enables.
866          *
867          * Rather than run a separate DMA operation for every single byte, we
868          * queue them up and run a single DMA operation for the entire series
869          * of command and data bytes. NAND_CMD_NONE means the END of the queue.
870          */
871         if ((ctrl & (NAND_ALE | NAND_CLE))) {
872                 if (data != NAND_CMD_NONE)
873                         this->cmd_buffer[this->command_length++] = data;
874                 return;
875         }
876 
877         if (!this->command_length)
878                 return;
879 
880         ret = gpmi_send_command(this);
881         if (ret)
882                 dev_err(this->dev, "Chip: %u, Error %d\n",
883                         this->current_chip, ret);
884 
885         this->command_length = 0;
886 }
887 
888 static int gpmi_dev_ready(struct mtd_info *mtd)
889 {
890         struct nand_chip *chip = mtd->priv;
891         struct gpmi_nand_data *this = chip->priv;
892 
893         return gpmi_is_ready(this, this->current_chip);
894 }
895 
896 static void gpmi_select_chip(struct mtd_info *mtd, int chipnr)
897 {
898         struct nand_chip *chip = mtd->priv;
899         struct gpmi_nand_data *this = chip->priv;
900 
901         if ((this->current_chip < 0) && (chipnr >= 0))
902                 gpmi_begin(this);
903         else if ((this->current_chip >= 0) && (chipnr < 0))
904                 gpmi_end(this);
905 
906         this->current_chip = chipnr;
907 }
908 
909 static void gpmi_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
910 {
911         struct nand_chip *chip = mtd->priv;
912         struct gpmi_nand_data *this = chip->priv;
913 
914         dev_dbg(this->dev, "len is %d\n", len);
915         this->upper_buf = buf;
916         this->upper_len = len;
917 
918         gpmi_read_data(this);
919 }
920 
921 static void gpmi_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
922 {
923         struct nand_chip *chip = mtd->priv;
924         struct gpmi_nand_data *this = chip->priv;
925 
926         dev_dbg(this->dev, "len is %d\n", len);
927         this->upper_buf = (uint8_t *)buf;
928         this->upper_len = len;
929 
930         gpmi_send_data(this);
931 }
932 
933 static uint8_t gpmi_read_byte(struct mtd_info *mtd)
934 {
935         struct nand_chip *chip = mtd->priv;
936         struct gpmi_nand_data *this = chip->priv;
937         uint8_t *buf = this->data_buffer_dma;
938 
939         gpmi_read_buf(mtd, buf, 1);
940         return buf[0];
941 }
942 
943 /*
944  * Handles block mark swapping.
945  * It can be called in swapping the block mark, or swapping it back,
946  * because the the operations are the same.
947  */
948 static void block_mark_swapping(struct gpmi_nand_data *this,
949                                 void *payload, void *auxiliary)
950 {
951         struct bch_geometry *nfc_geo = &this->bch_geometry;
952         unsigned char *p;
953         unsigned char *a;
954         unsigned int  bit;
955         unsigned char mask;
956         unsigned char from_data;
957         unsigned char from_oob;
958 
959         if (!this->swap_block_mark)
960                 return;
961 
962         /*
963          * If control arrives here, we're swapping. Make some convenience
964          * variables.
965          */
966         bit = nfc_geo->block_mark_bit_offset;
967         p   = payload + nfc_geo->block_mark_byte_offset;
968         a   = auxiliary;
969 
970         /*
971          * Get the byte from the data area that overlays the block mark. Since
972          * the ECC engine applies its own view to the bits in the page, the
973          * physical block mark won't (in general) appear on a byte boundary in
974          * the data.
975          */
976         from_data = (p[0] >> bit) | (p[1] << (8 - bit));
977 
978         /* Get the byte from the OOB. */
979         from_oob = a[0];
980 
981         /* Swap them. */
982         a[0] = from_data;
983 
984         mask = (0x1 << bit) - 1;
985         p[0] = (p[0] & mask) | (from_oob << bit);
986 
987         mask = ~0 << bit;
988         p[1] = (p[1] & mask) | (from_oob >> (8 - bit));
989 }
990 
991 static int gpmi_ecc_read_page(struct mtd_info *mtd, struct nand_chip *chip,
992                                 uint8_t *buf, int oob_required, int page)
993 {
994         struct gpmi_nand_data *this = chip->priv;
995         struct bch_geometry *nfc_geo = &this->bch_geometry;
996         void          *payload_virt;
997         dma_addr_t    payload_phys;
998         void          *auxiliary_virt;
999         dma_addr_t    auxiliary_phys;
1000         unsigned int  i;
1001         unsigned char *status;
1002         unsigned int  max_bitflips = 0;
1003         int           ret;
1004 
1005         dev_dbg(this->dev, "page number is : %d\n", page);
1006         ret = read_page_prepare(this, buf, nfc_geo->payload_size,
1007                                         this->payload_virt, this->payload_phys,
1008                                         nfc_geo->payload_size,
1009                                         &payload_virt, &payload_phys);
1010         if (ret) {
1011                 dev_err(this->dev, "Inadequate DMA buffer\n");
1012                 ret = -ENOMEM;
1013                 return ret;
1014         }
1015         auxiliary_virt = this->auxiliary_virt;
1016         auxiliary_phys = this->auxiliary_phys;
1017 
1018         /* go! */
1019         ret = gpmi_read_page(this, payload_phys, auxiliary_phys);
1020         read_page_end(this, buf, nfc_geo->payload_size,
1021                         this->payload_virt, this->payload_phys,
1022                         nfc_geo->payload_size,
1023                         payload_virt, payload_phys);
1024         if (ret) {
1025                 dev_err(this->dev, "Error in ECC-based read: %d\n", ret);
1026                 return ret;
1027         }
1028 
1029         /* handle the block mark swapping */
1030         block_mark_swapping(this, payload_virt, auxiliary_virt);
1031 
1032         /* Loop over status bytes, accumulating ECC status. */
1033         status = auxiliary_virt + nfc_geo->auxiliary_status_offset;
1034 
1035         for (i = 0; i < nfc_geo->ecc_chunk_count; i++, status++) {
1036                 if ((*status == STATUS_GOOD) || (*status == STATUS_ERASED))
1037                         continue;
1038 
1039                 if (*status == STATUS_UNCORRECTABLE) {
1040                         mtd->ecc_stats.failed++;
1041                         continue;
1042                 }
1043                 mtd->ecc_stats.corrected += *status;
1044                 max_bitflips = max_t(unsigned int, max_bitflips, *status);
1045         }
1046 
1047         if (oob_required) {
1048                 /*
1049                  * It's time to deliver the OOB bytes. See gpmi_ecc_read_oob()
1050                  * for details about our policy for delivering the OOB.
1051                  *
1052                  * We fill the caller's buffer with set bits, and then copy the
1053                  * block mark to th caller's buffer. Note that, if block mark
1054                  * swapping was necessary, it has already been done, so we can
1055                  * rely on the first byte of the auxiliary buffer to contain
1056                  * the block mark.
1057                  */
1058                 memset(chip->oob_poi, ~0, mtd->oobsize);
1059                 chip->oob_poi[0] = ((uint8_t *) auxiliary_virt)[0];
1060         }
1061 
1062         read_page_swap_end(this, buf, nfc_geo->payload_size,
1063                         this->payload_virt, this->payload_phys,
1064                         nfc_geo->payload_size,
1065                         payload_virt, payload_phys);
1066 
1067         return max_bitflips;
1068 }
1069 
1070 /* Fake a virtual small page for the subpage read */
1071 static int gpmi_ecc_read_subpage(struct mtd_info *mtd, struct nand_chip *chip,
1072                         uint32_t offs, uint32_t len, uint8_t *buf, int page)
1073 {
1074         struct gpmi_nand_data *this = chip->priv;
1075         void __iomem *bch_regs = this->resources.bch_regs;
1076         struct bch_geometry old_geo = this->bch_geometry;
1077         struct bch_geometry *geo = &this->bch_geometry;
1078         int size = chip->ecc.size; /* ECC chunk size */
1079         int meta, n, page_size;
1080         u32 r1_old, r2_old, r1_new, r2_new;
1081         unsigned int max_bitflips;
1082         int first, last, marker_pos;
1083         int ecc_parity_size;
1084         int col = 0;
1085 
1086         /* The size of ECC parity */
1087         ecc_parity_size = geo->gf_len * geo->ecc_strength / 8;
1088 
1089         /* Align it with the chunk size */
1090         first = offs / size;
1091         last = (offs + len - 1) / size;
1092 
1093         /*
1094          * Find the chunk which contains the Block Marker. If this chunk is
1095          * in the range of [first, last], we have to read out the whole page.
1096          * Why? since we had swapped the data at the position of Block Marker
1097          * to the metadata which is bound with the chunk 0.
1098          */
1099         marker_pos = geo->block_mark_byte_offset / size;
1100         if (last >= marker_pos && first <= marker_pos) {
1101                 dev_dbg(this->dev, "page:%d, first:%d, last:%d, marker at:%d\n",
1102                                 page, first, last, marker_pos);
1103                 return gpmi_ecc_read_page(mtd, chip, buf, 0, page);
1104         }
1105 
1106         meta = geo->metadata_size;
1107         if (first) {
1108                 col = meta + (size + ecc_parity_size) * first;
1109                 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, col, -1);
1110 
1111                 meta = 0;
1112                 buf = buf + first * size;
1113         }
1114 
1115         /* Save the old environment */
1116         r1_old = r1_new = readl(bch_regs + HW_BCH_FLASH0LAYOUT0);
1117         r2_old = r2_new = readl(bch_regs + HW_BCH_FLASH0LAYOUT1);
1118 
1119         /* change the BCH registers and bch_geometry{} */
1120         n = last - first + 1;
1121         page_size = meta + (size + ecc_parity_size) * n;
1122 
1123         r1_new &= ~(BM_BCH_FLASH0LAYOUT0_NBLOCKS |
1124                         BM_BCH_FLASH0LAYOUT0_META_SIZE);
1125         r1_new |= BF_BCH_FLASH0LAYOUT0_NBLOCKS(n - 1)
1126                         | BF_BCH_FLASH0LAYOUT0_META_SIZE(meta);
1127         writel(r1_new, bch_regs + HW_BCH_FLASH0LAYOUT0);
1128 
1129         r2_new &= ~BM_BCH_FLASH0LAYOUT1_PAGE_SIZE;
1130         r2_new |= BF_BCH_FLASH0LAYOUT1_PAGE_SIZE(page_size);
1131         writel(r2_new, bch_regs + HW_BCH_FLASH0LAYOUT1);
1132 
1133         geo->ecc_chunk_count = n;
1134         geo->payload_size = n * size;
1135         geo->page_size = page_size;
1136         geo->auxiliary_status_offset = ALIGN(meta, 4);
1137 
1138         dev_dbg(this->dev, "page:%d(%d:%d)%d, chunk:(%d:%d), BCH PG size:%d\n",
1139                 page, offs, len, col, first, n, page_size);
1140 
1141         /* Read the subpage now */
1142         this->swap_block_mark = false;
1143         max_bitflips = gpmi_ecc_read_page(mtd, chip, buf, 0, page);
1144 
1145         /* Restore */
1146         writel(r1_old, bch_regs + HW_BCH_FLASH0LAYOUT0);
1147         writel(r2_old, bch_regs + HW_BCH_FLASH0LAYOUT1);
1148         this->bch_geometry = old_geo;
1149         this->swap_block_mark = true;
1150 
1151         return max_bitflips;
1152 }
1153 
1154 static int gpmi_ecc_write_page(struct mtd_info *mtd, struct nand_chip *chip,
1155                                 const uint8_t *buf, int oob_required)
1156 {
1157         struct gpmi_nand_data *this = chip->priv;
1158         struct bch_geometry *nfc_geo = &this->bch_geometry;
1159         const void *payload_virt;
1160         dma_addr_t payload_phys;
1161         const void *auxiliary_virt;
1162         dma_addr_t auxiliary_phys;
1163         int        ret;
1164 
1165         dev_dbg(this->dev, "ecc write page.\n");
1166         if (this->swap_block_mark) {
1167                 /*
1168                  * If control arrives here, we're doing block mark swapping.
1169                  * Since we can't modify the caller's buffers, we must copy them
1170                  * into our own.
1171                  */
1172                 memcpy(this->payload_virt, buf, mtd->writesize);
1173                 payload_virt = this->payload_virt;
1174                 payload_phys = this->payload_phys;
1175 
1176                 memcpy(this->auxiliary_virt, chip->oob_poi,
1177                                 nfc_geo->auxiliary_size);
1178                 auxiliary_virt = this->auxiliary_virt;
1179                 auxiliary_phys = this->auxiliary_phys;
1180 
1181                 /* Handle block mark swapping. */
1182                 block_mark_swapping(this,
1183                                 (void *) payload_virt, (void *) auxiliary_virt);
1184         } else {
1185                 /*
1186                  * If control arrives here, we're not doing block mark swapping,
1187                  * so we can to try and use the caller's buffers.
1188                  */
1189                 ret = send_page_prepare(this,
1190                                 buf, mtd->writesize,
1191                                 this->payload_virt, this->payload_phys,
1192                                 nfc_geo->payload_size,
1193                                 &payload_virt, &payload_phys);
1194                 if (ret) {
1195                         dev_err(this->dev, "Inadequate payload DMA buffer\n");
1196                         return 0;
1197                 }
1198 
1199                 ret = send_page_prepare(this,
1200                                 chip->oob_poi, mtd->oobsize,
1201                                 this->auxiliary_virt, this->auxiliary_phys,
1202                                 nfc_geo->auxiliary_size,
1203                                 &auxiliary_virt, &auxiliary_phys);
1204                 if (ret) {
1205                         dev_err(this->dev, "Inadequate auxiliary DMA buffer\n");
1206                         goto exit_auxiliary;
1207                 }
1208         }
1209 
1210         /* Ask the NFC. */
1211         ret = gpmi_send_page(this, payload_phys, auxiliary_phys);
1212         if (ret)
1213                 dev_err(this->dev, "Error in ECC-based write: %d\n", ret);
1214 
1215         if (!this->swap_block_mark) {
1216                 send_page_end(this, chip->oob_poi, mtd->oobsize,
1217                                 this->auxiliary_virt, this->auxiliary_phys,
1218                                 nfc_geo->auxiliary_size,
1219                                 auxiliary_virt, auxiliary_phys);
1220 exit_auxiliary:
1221                 send_page_end(this, buf, mtd->writesize,
1222                                 this->payload_virt, this->payload_phys,
1223                                 nfc_geo->payload_size,
1224                                 payload_virt, payload_phys);
1225         }
1226 
1227         return 0;
1228 }
1229 
1230 /*
1231  * There are several places in this driver where we have to handle the OOB and
1232  * block marks. This is the function where things are the most complicated, so
1233  * this is where we try to explain it all. All the other places refer back to
1234  * here.
1235  *
1236  * These are the rules, in order of decreasing importance:
1237  *
1238  * 1) Nothing the caller does can be allowed to imperil the block mark.
1239  *
1240  * 2) In read operations, the first byte of the OOB we return must reflect the
1241  *    true state of the block mark, no matter where that block mark appears in
1242  *    the physical page.
1243  *
1244  * 3) ECC-based read operations return an OOB full of set bits (since we never
1245  *    allow ECC-based writes to the OOB, it doesn't matter what ECC-based reads
1246  *    return).
1247  *
1248  * 4) "Raw" read operations return a direct view of the physical bytes in the
1249  *    page, using the conventional definition of which bytes are data and which
1250  *    are OOB. This gives the caller a way to see the actual, physical bytes
1251  *    in the page, without the distortions applied by our ECC engine.
1252  *
1253  *
1254  * What we do for this specific read operation depends on two questions:
1255  *
1256  * 1) Are we doing a "raw" read, or an ECC-based read?
1257  *
1258  * 2) Are we using block mark swapping or transcription?
1259  *
1260  * There are four cases, illustrated by the following Karnaugh map:
1261  *
1262  *                    |           Raw           |         ECC-based       |
1263  *       -------------+-------------------------+-------------------------+
1264  *                    | Read the conventional   |                         |
1265  *                    | OOB at the end of the   |                         |
1266  *       Swapping     | page and return it. It  |                         |
1267  *                    | contains exactly what   |                         |
1268  *                    | we want.                | Read the block mark and |
1269  *       -------------+-------------------------+ return it in a buffer   |
1270  *                    | Read the conventional   | full of set bits.       |
1271  *                    | OOB at the end of the   |                         |
1272  *                    | page and also the block |                         |
1273  *       Transcribing | mark in the metadata.   |                         |
1274  *                    | Copy the block mark     |                         |
1275  *                    | into the first byte of  |                         |
1276  *                    | the OOB.                |                         |
1277  *       -------------+-------------------------+-------------------------+
1278  *
1279  * Note that we break rule #4 in the Transcribing/Raw case because we're not
1280  * giving an accurate view of the actual, physical bytes in the page (we're
1281  * overwriting the block mark). That's OK because it's more important to follow
1282  * rule #2.
1283  *
1284  * It turns out that knowing whether we want an "ECC-based" or "raw" read is not
1285  * easy. When reading a page, for example, the NAND Flash MTD code calls our
1286  * ecc.read_page or ecc.read_page_raw function. Thus, the fact that MTD wants an
1287  * ECC-based or raw view of the page is implicit in which function it calls
1288  * (there is a similar pair of ECC-based/raw functions for writing).
1289  *
1290  * FIXME: The following paragraph is incorrect, now that there exist
1291  * ecc.read_oob_raw and ecc.write_oob_raw functions.
1292  *
1293  * Since MTD assumes the OOB is not covered by ECC, there is no pair of
1294  * ECC-based/raw functions for reading or or writing the OOB. The fact that the
1295  * caller wants an ECC-based or raw view of the page is not propagated down to
1296  * this driver.
1297  */
1298 static int gpmi_ecc_read_oob(struct mtd_info *mtd, struct nand_chip *chip,
1299                                 int page)
1300 {
1301         struct gpmi_nand_data *this = chip->priv;
1302 
1303         dev_dbg(this->dev, "page number is %d\n", page);
1304         /* clear the OOB buffer */
1305         memset(chip->oob_poi, ~0, mtd->oobsize);
1306 
1307         /* Read out the conventional OOB. */
1308         chip->cmdfunc(mtd, NAND_CMD_READ0, mtd->writesize, page);
1309         chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1310 
1311         /*
1312          * Now, we want to make sure the block mark is correct. In the
1313          * Swapping/Raw case, we already have it. Otherwise, we need to
1314          * explicitly read it.
1315          */
1316         if (!this->swap_block_mark) {
1317                 /* Read the block mark into the first byte of the OOB buffer. */
1318                 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
1319                 chip->oob_poi[0] = chip->read_byte(mtd);
1320         }
1321 
1322         return 0;
1323 }
1324 
1325 static int
1326 gpmi_ecc_write_oob(struct mtd_info *mtd, struct nand_chip *chip, int page)
1327 {
1328         struct nand_oobfree *of = mtd->ecclayout->oobfree;
1329         int status = 0;
1330 
1331         /* Do we have available oob area? */
1332         if (!of->length)
1333                 return -EPERM;
1334 
1335         if (!nand_is_slc(chip))
1336                 return -EPERM;
1337 
1338         chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize + of->offset, page);
1339         chip->write_buf(mtd, chip->oob_poi + of->offset, of->length);
1340         chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1341 
1342         status = chip->waitfunc(mtd, chip);
1343         return status & NAND_STATUS_FAIL ? -EIO : 0;
1344 }
1345 
1346 static int gpmi_block_markbad(struct mtd_info *mtd, loff_t ofs)
1347 {
1348         struct nand_chip *chip = mtd->priv;
1349         struct gpmi_nand_data *this = chip->priv;
1350         int ret = 0;
1351         uint8_t *block_mark;
1352         int column, page, status, chipnr;
1353 
1354         chipnr = (int)(ofs >> chip->chip_shift);
1355         chip->select_chip(mtd, chipnr);
1356 
1357         column = this->swap_block_mark ? mtd->writesize : 0;
1358 
1359         /* Write the block mark. */
1360         block_mark = this->data_buffer_dma;
1361         block_mark[0] = 0; /* bad block marker */
1362 
1363         /* Shift to get page */
1364         page = (int)(ofs >> chip->page_shift);
1365 
1366         chip->cmdfunc(mtd, NAND_CMD_SEQIN, column, page);
1367         chip->write_buf(mtd, block_mark, 1);
1368         chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1369 
1370         status = chip->waitfunc(mtd, chip);
1371         if (status & NAND_STATUS_FAIL)
1372                 ret = -EIO;
1373 
1374         chip->select_chip(mtd, -1);
1375 
1376         return ret;
1377 }
1378 
1379 static int nand_boot_set_geometry(struct gpmi_nand_data *this)
1380 {
1381         struct boot_rom_geometry *geometry = &this->rom_geometry;
1382 
1383         /*
1384          * Set the boot block stride size.
1385          *
1386          * In principle, we should be reading this from the OTP bits, since
1387          * that's where the ROM is going to get it. In fact, we don't have any
1388          * way to read the OTP bits, so we go with the default and hope for the
1389          * best.
1390          */
1391         geometry->stride_size_in_pages = 64;
1392 
1393         /*
1394          * Set the search area stride exponent.
1395          *
1396          * In principle, we should be reading this from the OTP bits, since
1397          * that's where the ROM is going to get it. In fact, we don't have any
1398          * way to read the OTP bits, so we go with the default and hope for the
1399          * best.
1400          */
1401         geometry->search_area_stride_exponent = 2;
1402         return 0;
1403 }
1404 
1405 static const char  *fingerprint = "STMP";
1406 static int mx23_check_transcription_stamp(struct gpmi_nand_data *this)
1407 {
1408         struct boot_rom_geometry *rom_geo = &this->rom_geometry;
1409         struct device *dev = this->dev;
1410         struct mtd_info *mtd = &this->mtd;
1411         struct nand_chip *chip = &this->nand;
1412         unsigned int search_area_size_in_strides;
1413         unsigned int stride;
1414         unsigned int page;
1415         uint8_t *buffer = chip->buffers->databuf;
1416         int saved_chip_number;
1417         int found_an_ncb_fingerprint = false;
1418 
1419         /* Compute the number of strides in a search area. */
1420         search_area_size_in_strides = 1 << rom_geo->search_area_stride_exponent;
1421 
1422         saved_chip_number = this->current_chip;
1423         chip->select_chip(mtd, 0);
1424 
1425         /*
1426          * Loop through the first search area, looking for the NCB fingerprint.
1427          */
1428         dev_dbg(dev, "Scanning for an NCB fingerprint...\n");
1429 
1430         for (stride = 0; stride < search_area_size_in_strides; stride++) {
1431                 /* Compute the page addresses. */
1432                 page = stride * rom_geo->stride_size_in_pages;
1433 
1434                 dev_dbg(dev, "Looking for a fingerprint in page 0x%x\n", page);
1435 
1436                 /*
1437                  * Read the NCB fingerprint. The fingerprint is four bytes long
1438                  * and starts in the 12th byte of the page.
1439                  */
1440                 chip->cmdfunc(mtd, NAND_CMD_READ0, 12, page);
1441                 chip->read_buf(mtd, buffer, strlen(fingerprint));
1442 
1443                 /* Look for the fingerprint. */
1444                 if (!memcmp(buffer, fingerprint, strlen(fingerprint))) {
1445                         found_an_ncb_fingerprint = true;
1446                         break;
1447                 }
1448 
1449         }
1450 
1451         chip->select_chip(mtd, saved_chip_number);
1452 
1453         if (found_an_ncb_fingerprint)
1454                 dev_dbg(dev, "\tFound a fingerprint\n");
1455         else
1456                 dev_dbg(dev, "\tNo fingerprint found\n");
1457         return found_an_ncb_fingerprint;
1458 }
1459 
1460 /* Writes a transcription stamp. */
1461 static int mx23_write_transcription_stamp(struct gpmi_nand_data *this)
1462 {
1463         struct device *dev = this->dev;
1464         struct boot_rom_geometry *rom_geo = &this->rom_geometry;
1465         struct mtd_info *mtd = &this->mtd;
1466         struct nand_chip *chip = &this->nand;
1467         unsigned int block_size_in_pages;
1468         unsigned int search_area_size_in_strides;
1469         unsigned int search_area_size_in_pages;
1470         unsigned int search_area_size_in_blocks;
1471         unsigned int block;
1472         unsigned int stride;
1473         unsigned int page;
1474         uint8_t      *buffer = chip->buffers->databuf;
1475         int saved_chip_number;
1476         int status;
1477 
1478         /* Compute the search area geometry. */
1479         block_size_in_pages = mtd->erasesize / mtd->writesize;
1480         search_area_size_in_strides = 1 << rom_geo->search_area_stride_exponent;
1481         search_area_size_in_pages = search_area_size_in_strides *
1482                                         rom_geo->stride_size_in_pages;
1483         search_area_size_in_blocks =
1484                   (search_area_size_in_pages + (block_size_in_pages - 1)) /
1485                                     block_size_in_pages;
1486 
1487         dev_dbg(dev, "Search Area Geometry :\n");
1488         dev_dbg(dev, "\tin Blocks : %u\n", search_area_size_in_blocks);
1489         dev_dbg(dev, "\tin Strides: %u\n", search_area_size_in_strides);
1490         dev_dbg(dev, "\tin Pages  : %u\n", search_area_size_in_pages);
1491 
1492         /* Select chip 0. */
1493         saved_chip_number = this->current_chip;
1494         chip->select_chip(mtd, 0);
1495 
1496         /* Loop over blocks in the first search area, erasing them. */
1497         dev_dbg(dev, "Erasing the search area...\n");
1498 
1499         for (block = 0; block < search_area_size_in_blocks; block++) {
1500                 /* Compute the page address. */
1501                 page = block * block_size_in_pages;
1502 
1503                 /* Erase this block. */
1504                 dev_dbg(dev, "\tErasing block 0x%x\n", block);
1505                 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
1506                 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
1507 
1508                 /* Wait for the erase to finish. */
1509                 status = chip->waitfunc(mtd, chip);
1510                 if (status & NAND_STATUS_FAIL)
1511                         dev_err(dev, "[%s] Erase failed.\n", __func__);
1512         }
1513 
1514         /* Write the NCB fingerprint into the page buffer. */
1515         memset(buffer, ~0, mtd->writesize);
1516         memcpy(buffer + 12, fingerprint, strlen(fingerprint));
1517 
1518         /* Loop through the first search area, writing NCB fingerprints. */
1519         dev_dbg(dev, "Writing NCB fingerprints...\n");
1520         for (stride = 0; stride < search_area_size_in_strides; stride++) {
1521                 /* Compute the page addresses. */
1522                 page = stride * rom_geo->stride_size_in_pages;
1523 
1524                 /* Write the first page of the current stride. */
1525                 dev_dbg(dev, "Writing an NCB fingerprint in page 0x%x\n", page);
1526                 chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
1527                 chip->ecc.write_page_raw(mtd, chip, buffer, 0);
1528                 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1529 
1530                 /* Wait for the write to finish. */
1531                 status = chip->waitfunc(mtd, chip);
1532                 if (status & NAND_STATUS_FAIL)
1533                         dev_err(dev, "[%s] Write failed.\n", __func__);
1534         }
1535 
1536         /* Deselect chip 0. */
1537         chip->select_chip(mtd, saved_chip_number);
1538         return 0;
1539 }
1540 
1541 static int mx23_boot_init(struct gpmi_nand_data  *this)
1542 {
1543         struct device *dev = this->dev;
1544         struct nand_chip *chip = &this->nand;
1545         struct mtd_info *mtd = &this->mtd;
1546         unsigned int block_count;
1547         unsigned int block;
1548         int     chipnr;
1549         int     page;
1550         loff_t  byte;
1551         uint8_t block_mark;
1552         int     ret = 0;
1553 
1554         /*
1555          * If control arrives here, we can't use block mark swapping, which
1556          * means we're forced to use transcription. First, scan for the
1557          * transcription stamp. If we find it, then we don't have to do
1558          * anything -- the block marks are already transcribed.
1559          */
1560         if (mx23_check_transcription_stamp(this))
1561                 return 0;
1562 
1563         /*
1564          * If control arrives here, we couldn't find a transcription stamp, so
1565          * so we presume the block marks are in the conventional location.
1566          */
1567         dev_dbg(dev, "Transcribing bad block marks...\n");
1568 
1569         /* Compute the number of blocks in the entire medium. */
1570         block_count = chip->chipsize >> chip->phys_erase_shift;
1571 
1572         /*
1573          * Loop over all the blocks in the medium, transcribing block marks as
1574          * we go.
1575          */
1576         for (block = 0; block < block_count; block++) {
1577                 /*
1578                  * Compute the chip, page and byte addresses for this block's
1579                  * conventional mark.
1580                  */
1581                 chipnr = block >> (chip->chip_shift - chip->phys_erase_shift);
1582                 page = block << (chip->phys_erase_shift - chip->page_shift);
1583                 byte = block <<  chip->phys_erase_shift;
1584 
1585                 /* Send the command to read the conventional block mark. */
1586                 chip->select_chip(mtd, chipnr);
1587                 chip->cmdfunc(mtd, NAND_CMD_READ0, mtd->writesize, page);
1588                 block_mark = chip->read_byte(mtd);
1589                 chip->select_chip(mtd, -1);
1590 
1591                 /*
1592                  * Check if the block is marked bad. If so, we need to mark it
1593                  * again, but this time the result will be a mark in the
1594                  * location where we transcribe block marks.
1595                  */
1596                 if (block_mark != 0xff) {
1597                         dev_dbg(dev, "Transcribing mark in block %u\n", block);
1598                         ret = chip->block_markbad(mtd, byte);
1599                         if (ret)
1600                                 dev_err(dev, "Failed to mark block bad with "
1601                                                         "ret %d\n", ret);
1602                 }
1603         }
1604 
1605         /* Write the stamp that indicates we've transcribed the block marks. */
1606         mx23_write_transcription_stamp(this);
1607         return 0;
1608 }
1609 
1610 static int nand_boot_init(struct gpmi_nand_data  *this)
1611 {
1612         nand_boot_set_geometry(this);
1613 
1614         /* This is ROM arch-specific initilization before the BBT scanning. */
1615         if (GPMI_IS_MX23(this))
1616                 return mx23_boot_init(this);
1617         return 0;
1618 }
1619 
1620 static int gpmi_set_geometry(struct gpmi_nand_data *this)
1621 {
1622         int ret;
1623 
1624         /* Free the temporary DMA memory for reading ID. */
1625         gpmi_free_dma_buffer(this);
1626 
1627         /* Set up the NFC geometry which is used by BCH. */
1628         ret = bch_set_geometry(this);
1629         if (ret) {
1630                 dev_err(this->dev, "Error setting BCH geometry : %d\n", ret);
1631                 return ret;
1632         }
1633 
1634         /* Alloc the new DMA buffers according to the pagesize and oobsize */
1635         return gpmi_alloc_dma_buffer(this);
1636 }
1637 
1638 static void gpmi_nand_exit(struct gpmi_nand_data *this)
1639 {
1640         nand_release(&this->mtd);
1641         gpmi_free_dma_buffer(this);
1642 }
1643 
1644 static int gpmi_init_last(struct gpmi_nand_data *this)
1645 {
1646         struct mtd_info *mtd = &this->mtd;
1647         struct nand_chip *chip = mtd->priv;
1648         struct nand_ecc_ctrl *ecc = &chip->ecc;
1649         struct bch_geometry *bch_geo = &this->bch_geometry;
1650         int ret;
1651 
1652         /* Set up swap_block_mark, must be set before the gpmi_set_geometry() */
1653         this->swap_block_mark = !GPMI_IS_MX23(this);
1654 
1655         /* Set up the medium geometry */
1656         ret = gpmi_set_geometry(this);
1657         if (ret)
1658                 return ret;
1659 
1660         /* Init the nand_ecc_ctrl{} */
1661         ecc->read_page  = gpmi_ecc_read_page;
1662         ecc->write_page = gpmi_ecc_write_page;
1663         ecc->read_oob   = gpmi_ecc_read_oob;
1664         ecc->write_oob  = gpmi_ecc_write_oob;
1665         ecc->mode       = NAND_ECC_HW;
1666         ecc->size       = bch_geo->ecc_chunk_size;
1667         ecc->strength   = bch_geo->ecc_strength;
1668         ecc->layout     = &gpmi_hw_ecclayout;
1669 
1670         /*
1671          * We only enable the subpage read when:
1672          *  (1) the chip is imx6, and
1673          *  (2) the size of the ECC parity is byte aligned.
1674          */
1675         if (GPMI_IS_MX6(this) &&
1676                 ((bch_geo->gf_len * bch_geo->ecc_strength) % 8) == 0) {
1677                 ecc->read_subpage = gpmi_ecc_read_subpage;
1678                 chip->options |= NAND_SUBPAGE_READ;
1679         }
1680 
1681         /*
1682          * Can we enable the extra features? such as EDO or Sync mode.
1683          *
1684          * We do not check the return value now. That's means if we fail in
1685          * enable the extra features, we still can run in the normal way.
1686          */
1687         gpmi_extra_init(this);
1688 
1689         return 0;
1690 }
1691 
1692 static int gpmi_nand_init(struct gpmi_nand_data *this)
1693 {
1694         struct mtd_info  *mtd = &this->mtd;
1695         struct nand_chip *chip = &this->nand;
1696         struct mtd_part_parser_data ppdata = {};
1697         int ret;
1698 
1699         /* init current chip */
1700         this->current_chip      = -1;
1701 
1702         /* init the MTD data structures */
1703         mtd->priv               = chip;
1704         mtd->name               = "gpmi-nand";
1705         mtd->owner              = THIS_MODULE;
1706 
1707         /* init the nand_chip{}, we don't support a 16-bit NAND Flash bus. */
1708         chip->priv              = this;
1709         chip->select_chip       = gpmi_select_chip;
1710         chip->cmd_ctrl          = gpmi_cmd_ctrl;
1711         chip->dev_ready         = gpmi_dev_ready;
1712         chip->read_byte         = gpmi_read_byte;
1713         chip->read_buf          = gpmi_read_buf;
1714         chip->write_buf         = gpmi_write_buf;
1715         chip->badblock_pattern  = &gpmi_bbt_descr;
1716         chip->block_markbad     = gpmi_block_markbad;
1717         chip->options           |= NAND_NO_SUBPAGE_WRITE;
1718         if (of_get_nand_on_flash_bbt(this->dev->of_node))
1719                 chip->bbt_options |= NAND_BBT_USE_FLASH | NAND_BBT_NO_OOB;
1720 
1721         /*
1722          * Allocate a temporary DMA buffer for reading ID in the
1723          * nand_scan_ident().
1724          */
1725         this->bch_geometry.payload_size = 1024;
1726         this->bch_geometry.auxiliary_size = 128;
1727         ret = gpmi_alloc_dma_buffer(this);
1728         if (ret)
1729                 goto err_out;
1730 
1731         ret = nand_scan_ident(mtd, GPMI_IS_MX6(this) ? 2 : 1, NULL);
1732         if (ret)
1733                 goto err_out;
1734 
1735         ret = gpmi_init_last(this);
1736         if (ret)
1737                 goto err_out;
1738 
1739         chip->options |= NAND_SKIP_BBTSCAN;
1740         ret = nand_scan_tail(mtd);
1741         if (ret)
1742                 goto err_out;
1743 
1744         ret = nand_boot_init(this);
1745         if (ret)
1746                 goto err_out;
1747         chip->scan_bbt(mtd);
1748 
1749         ppdata.of_node = this->pdev->dev.of_node;
1750         ret = mtd_device_parse_register(mtd, NULL, &ppdata, NULL, 0);
1751         if (ret)
1752                 goto err_out;
1753         return 0;
1754 
1755 err_out:
1756         gpmi_nand_exit(this);
1757         return ret;
1758 }
1759 
1760 static const struct of_device_id gpmi_nand_id_table[] = {
1761         {
1762                 .compatible = "fsl,imx23-gpmi-nand",
1763                 .data = (void *)&gpmi_devdata_imx23,
1764         }, {
1765                 .compatible = "fsl,imx28-gpmi-nand",
1766                 .data = (void *)&gpmi_devdata_imx28,
1767         }, {
1768                 .compatible = "fsl,imx6q-gpmi-nand",
1769                 .data = (void *)&gpmi_devdata_imx6q,
1770         }, {
1771                 .compatible = "fsl,imx6sx-gpmi-nand",
1772                 .data = (void *)&gpmi_devdata_imx6sx,
1773         }, {}
1774 };
1775 MODULE_DEVICE_TABLE(of, gpmi_nand_id_table);
1776 
1777 static int gpmi_nand_probe(struct platform_device *pdev)
1778 {
1779         struct gpmi_nand_data *this;
1780         const struct of_device_id *of_id;
1781         int ret;
1782 
1783         this = devm_kzalloc(&pdev->dev, sizeof(*this), GFP_KERNEL);
1784         if (!this)
1785                 return -ENOMEM;
1786 
1787         of_id = of_match_device(gpmi_nand_id_table, &pdev->dev);
1788         if (of_id) {
1789                 this->devdata = of_id->data;
1790         } else {
1791                 dev_err(&pdev->dev, "Failed to find the right device id.\n");
1792                 return -ENODEV;
1793         }
1794 
1795         platform_set_drvdata(pdev, this);
1796         this->pdev  = pdev;
1797         this->dev   = &pdev->dev;
1798 
1799         ret = acquire_resources(this);
1800         if (ret)
1801                 goto exit_acquire_resources;
1802 
1803         ret = init_hardware(this);
1804         if (ret)
1805                 goto exit_nfc_init;
1806 
1807         ret = gpmi_nand_init(this);
1808         if (ret)
1809                 goto exit_nfc_init;
1810 
1811         dev_info(this->dev, "driver registered.\n");
1812 
1813         return 0;
1814 
1815 exit_nfc_init:
1816         release_resources(this);
1817 exit_acquire_resources:
1818         dev_err(this->dev, "driver registration failed: %d\n", ret);
1819 
1820         return ret;
1821 }
1822 
1823 static int gpmi_nand_remove(struct platform_device *pdev)
1824 {
1825         struct gpmi_nand_data *this = platform_get_drvdata(pdev);
1826 
1827         gpmi_nand_exit(this);
1828         release_resources(this);
1829         return 0;
1830 }
1831 
1832 static struct platform_driver gpmi_nand_driver = {
1833         .driver = {
1834                 .name = "gpmi-nand",
1835                 .of_match_table = gpmi_nand_id_table,
1836         },
1837         .probe   = gpmi_nand_probe,
1838         .remove  = gpmi_nand_remove,
1839 };
1840 module_platform_driver(gpmi_nand_driver);
1841 
1842 MODULE_AUTHOR("Freescale Semiconductor, Inc.");
1843 MODULE_DESCRIPTION("i.MX GPMI NAND Flash Controller Driver");
1844 MODULE_LICENSE("GPL");
1845 

This page was automatically generated by LXR 0.3.1 (source).  •  Linux is a registered trademark of Linus Torvalds  •  Contact us