Merge branch 'nohz/printk-v8' into irq/core
[firefly-linux-kernel-4.4.55.git] / drivers / mtd / nand / gpmi-nand / gpmi-lib.c
1 /*
2  * Freescale GPMI NAND Flash Driver
3  *
4  * Copyright (C) 2008-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/delay.h>
22 #include <linux/clk.h>
23
24 #include "gpmi-nand.h"
25 #include "gpmi-regs.h"
26 #include "bch-regs.h"
27
28 static struct timing_threshod timing_default_threshold = {
29         .max_data_setup_cycles       = (BM_GPMI_TIMING0_DATA_SETUP >>
30                                                 BP_GPMI_TIMING0_DATA_SETUP),
31         .internal_data_setup_in_ns   = 0,
32         .max_sample_delay_factor     = (BM_GPMI_CTRL1_RDN_DELAY >>
33                                                 BP_GPMI_CTRL1_RDN_DELAY),
34         .max_dll_clock_period_in_ns  = 32,
35         .max_dll_delay_in_ns         = 16,
36 };
37
38 #define MXS_SET_ADDR            0x4
39 #define MXS_CLR_ADDR            0x8
40 /*
41  * Clear the bit and poll it cleared.  This is usually called with
42  * a reset address and mask being either SFTRST(bit 31) or CLKGATE
43  * (bit 30).
44  */
45 static int clear_poll_bit(void __iomem *addr, u32 mask)
46 {
47         int timeout = 0x400;
48
49         /* clear the bit */
50         writel(mask, addr + MXS_CLR_ADDR);
51
52         /*
53          * SFTRST needs 3 GPMI clocks to settle, the reference manual
54          * recommends to wait 1us.
55          */
56         udelay(1);
57
58         /* poll the bit becoming clear */
59         while ((readl(addr) & mask) && --timeout)
60                 /* nothing */;
61
62         return !timeout;
63 }
64
65 #define MODULE_CLKGATE          (1 << 30)
66 #define MODULE_SFTRST           (1 << 31)
67 /*
68  * The current mxs_reset_block() will do two things:
69  *  [1] enable the module.
70  *  [2] reset the module.
71  *
72  * In most of the cases, it's ok.
73  * But in MX23, there is a hardware bug in the BCH block (see erratum #2847).
74  * If you try to soft reset the BCH block, it becomes unusable until
75  * the next hard reset. This case occurs in the NAND boot mode. When the board
76  * boots by NAND, the ROM of the chip will initialize the BCH blocks itself.
77  * So If the driver tries to reset the BCH again, the BCH will not work anymore.
78  * You will see a DMA timeout in this case. The bug has been fixed
79  * in the following chips, such as MX28.
80  *
81  * To avoid this bug, just add a new parameter `just_enable` for
82  * the mxs_reset_block(), and rewrite it here.
83  */
84 static int gpmi_reset_block(void __iomem *reset_addr, bool just_enable)
85 {
86         int ret;
87         int timeout = 0x400;
88
89         /* clear and poll SFTRST */
90         ret = clear_poll_bit(reset_addr, MODULE_SFTRST);
91         if (unlikely(ret))
92                 goto error;
93
94         /* clear CLKGATE */
95         writel(MODULE_CLKGATE, reset_addr + MXS_CLR_ADDR);
96
97         if (!just_enable) {
98                 /* set SFTRST to reset the block */
99                 writel(MODULE_SFTRST, reset_addr + MXS_SET_ADDR);
100                 udelay(1);
101
102                 /* poll CLKGATE becoming set */
103                 while ((!(readl(reset_addr) & MODULE_CLKGATE)) && --timeout)
104                         /* nothing */;
105                 if (unlikely(!timeout))
106                         goto error;
107         }
108
109         /* clear and poll SFTRST */
110         ret = clear_poll_bit(reset_addr, MODULE_SFTRST);
111         if (unlikely(ret))
112                 goto error;
113
114         /* clear and poll CLKGATE */
115         ret = clear_poll_bit(reset_addr, MODULE_CLKGATE);
116         if (unlikely(ret))
117                 goto error;
118
119         return 0;
120
121 error:
122         pr_err("%s(%p): module reset timeout\n", __func__, reset_addr);
123         return -ETIMEDOUT;
124 }
125
126 static int __gpmi_enable_clk(struct gpmi_nand_data *this, bool v)
127 {
128         struct clk *clk;
129         int ret;
130         int i;
131
132         for (i = 0; i < GPMI_CLK_MAX; i++) {
133                 clk = this->resources.clock[i];
134                 if (!clk)
135                         break;
136
137                 if (v) {
138                         ret = clk_prepare_enable(clk);
139                         if (ret)
140                                 goto err_clk;
141                 } else {
142                         clk_disable_unprepare(clk);
143                 }
144         }
145         return 0;
146
147 err_clk:
148         for (; i > 0; i--)
149                 clk_disable_unprepare(this->resources.clock[i - 1]);
150         return ret;
151 }
152
153 #define gpmi_enable_clk(x) __gpmi_enable_clk(x, true)
154 #define gpmi_disable_clk(x) __gpmi_enable_clk(x, false)
155
156 int gpmi_init(struct gpmi_nand_data *this)
157 {
158         struct resources *r = &this->resources;
159         int ret;
160
161         ret = gpmi_enable_clk(this);
162         if (ret)
163                 goto err_out;
164         ret = gpmi_reset_block(r->gpmi_regs, false);
165         if (ret)
166                 goto err_out;
167
168         /*
169          * Reset BCH here, too. We got failures otherwise :(
170          * See later BCH reset for explanation of MX23 handling
171          */
172         ret = gpmi_reset_block(r->bch_regs, GPMI_IS_MX23(this));
173         if (ret)
174                 goto err_out;
175
176
177         /* Choose NAND mode. */
178         writel(BM_GPMI_CTRL1_GPMI_MODE, r->gpmi_regs + HW_GPMI_CTRL1_CLR);
179
180         /* Set the IRQ polarity. */
181         writel(BM_GPMI_CTRL1_ATA_IRQRDY_POLARITY,
182                                 r->gpmi_regs + HW_GPMI_CTRL1_SET);
183
184         /* Disable Write-Protection. */
185         writel(BM_GPMI_CTRL1_DEV_RESET, r->gpmi_regs + HW_GPMI_CTRL1_SET);
186
187         /* Select BCH ECC. */
188         writel(BM_GPMI_CTRL1_BCH_MODE, r->gpmi_regs + HW_GPMI_CTRL1_SET);
189
190         gpmi_disable_clk(this);
191         return 0;
192 err_out:
193         return ret;
194 }
195
196 /* This function is very useful. It is called only when the bug occur. */
197 void gpmi_dump_info(struct gpmi_nand_data *this)
198 {
199         struct resources *r = &this->resources;
200         struct bch_geometry *geo = &this->bch_geometry;
201         u32 reg;
202         int i;
203
204         pr_err("Show GPMI registers :\n");
205         for (i = 0; i <= HW_GPMI_DEBUG / 0x10 + 1; i++) {
206                 reg = readl(r->gpmi_regs + i * 0x10);
207                 pr_err("offset 0x%.3x : 0x%.8x\n", i * 0x10, reg);
208         }
209
210         /* start to print out the BCH info */
211         pr_err("BCH Geometry :\n");
212         pr_err("GF length              : %u\n", geo->gf_len);
213         pr_err("ECC Strength           : %u\n", geo->ecc_strength);
214         pr_err("Page Size in Bytes     : %u\n", geo->page_size);
215         pr_err("Metadata Size in Bytes : %u\n", geo->metadata_size);
216         pr_err("ECC Chunk Size in Bytes: %u\n", geo->ecc_chunk_size);
217         pr_err("ECC Chunk Count        : %u\n", geo->ecc_chunk_count);
218         pr_err("Payload Size in Bytes  : %u\n", geo->payload_size);
219         pr_err("Auxiliary Size in Bytes: %u\n", geo->auxiliary_size);
220         pr_err("Auxiliary Status Offset: %u\n", geo->auxiliary_status_offset);
221         pr_err("Block Mark Byte Offset : %u\n", geo->block_mark_byte_offset);
222         pr_err("Block Mark Bit Offset  : %u\n", geo->block_mark_bit_offset);
223 }
224
225 /* Configures the geometry for BCH.  */
226 int bch_set_geometry(struct gpmi_nand_data *this)
227 {
228         struct resources *r = &this->resources;
229         struct bch_geometry *bch_geo = &this->bch_geometry;
230         unsigned int block_count;
231         unsigned int block_size;
232         unsigned int metadata_size;
233         unsigned int ecc_strength;
234         unsigned int page_size;
235         int ret;
236
237         if (common_nfc_set_geometry(this))
238                 return !0;
239
240         block_count   = bch_geo->ecc_chunk_count - 1;
241         block_size    = bch_geo->ecc_chunk_size;
242         metadata_size = bch_geo->metadata_size;
243         ecc_strength  = bch_geo->ecc_strength >> 1;
244         page_size     = bch_geo->page_size;
245
246         ret = gpmi_enable_clk(this);
247         if (ret)
248                 goto err_out;
249
250         /*
251         * Due to erratum #2847 of the MX23, the BCH cannot be soft reset on this
252         * chip, otherwise it will lock up. So we skip resetting BCH on the MX23.
253         * On the other hand, the MX28 needs the reset, because one case has been
254         * seen where the BCH produced ECC errors constantly after 10000
255         * consecutive reboots. The latter case has not been seen on the MX23 yet,
256         * still we don't know if it could happen there as well.
257         */
258         ret = gpmi_reset_block(r->bch_regs, GPMI_IS_MX23(this));
259         if (ret)
260                 goto err_out;
261
262         /* Configure layout 0. */
263         writel(BF_BCH_FLASH0LAYOUT0_NBLOCKS(block_count)
264                         | BF_BCH_FLASH0LAYOUT0_META_SIZE(metadata_size)
265                         | BF_BCH_FLASH0LAYOUT0_ECC0(ecc_strength, this)
266                         | BF_BCH_FLASH0LAYOUT0_DATA0_SIZE(block_size, this),
267                         r->bch_regs + HW_BCH_FLASH0LAYOUT0);
268
269         writel(BF_BCH_FLASH0LAYOUT1_PAGE_SIZE(page_size)
270                         | BF_BCH_FLASH0LAYOUT1_ECCN(ecc_strength, this)
271                         | BF_BCH_FLASH0LAYOUT1_DATAN_SIZE(block_size, this),
272                         r->bch_regs + HW_BCH_FLASH0LAYOUT1);
273
274         /* Set *all* chip selects to use layout 0. */
275         writel(0, r->bch_regs + HW_BCH_LAYOUTSELECT);
276
277         /* Enable interrupts. */
278         writel(BM_BCH_CTRL_COMPLETE_IRQ_EN,
279                                 r->bch_regs + HW_BCH_CTRL_SET);
280
281         gpmi_disable_clk(this);
282         return 0;
283 err_out:
284         return ret;
285 }
286
287 /* Converts time in nanoseconds to cycles. */
288 static unsigned int ns_to_cycles(unsigned int time,
289                         unsigned int period, unsigned int min)
290 {
291         unsigned int k;
292
293         k = (time + period - 1) / period;
294         return max(k, min);
295 }
296
297 #define DEF_MIN_PROP_DELAY      5
298 #define DEF_MAX_PROP_DELAY      9
299 /* Apply timing to current hardware conditions. */
300 static int gpmi_nfc_compute_hardware_timing(struct gpmi_nand_data *this,
301                                         struct gpmi_nfc_hardware_timing *hw)
302 {
303         struct timing_threshod *nfc = &timing_default_threshold;
304         struct resources *r = &this->resources;
305         struct nand_chip *nand = &this->nand;
306         struct nand_timing target = this->timing;
307         bool improved_timing_is_available;
308         unsigned long clock_frequency_in_hz;
309         unsigned int clock_period_in_ns;
310         bool dll_use_half_periods;
311         unsigned int dll_delay_shift;
312         unsigned int max_sample_delay_in_ns;
313         unsigned int address_setup_in_cycles;
314         unsigned int data_setup_in_ns;
315         unsigned int data_setup_in_cycles;
316         unsigned int data_hold_in_cycles;
317         int ideal_sample_delay_in_ns;
318         unsigned int sample_delay_factor;
319         int tEYE;
320         unsigned int min_prop_delay_in_ns = DEF_MIN_PROP_DELAY;
321         unsigned int max_prop_delay_in_ns = DEF_MAX_PROP_DELAY;
322
323         /*
324          * If there are multiple chips, we need to relax the timings to allow
325          * for signal distortion due to higher capacitance.
326          */
327         if (nand->numchips > 2) {
328                 target.data_setup_in_ns    += 10;
329                 target.data_hold_in_ns     += 10;
330                 target.address_setup_in_ns += 10;
331         } else if (nand->numchips > 1) {
332                 target.data_setup_in_ns    += 5;
333                 target.data_hold_in_ns     += 5;
334                 target.address_setup_in_ns += 5;
335         }
336
337         /* Check if improved timing information is available. */
338         improved_timing_is_available =
339                 (target.tREA_in_ns  >= 0) &&
340                 (target.tRLOH_in_ns >= 0) &&
341                 (target.tRHOH_in_ns >= 0) ;
342
343         /* Inspect the clock. */
344         nfc->clock_frequency_in_hz = clk_get_rate(r->clock[0]);
345         clock_frequency_in_hz = nfc->clock_frequency_in_hz;
346         clock_period_in_ns    = NSEC_PER_SEC / clock_frequency_in_hz;
347
348         /*
349          * The NFC quantizes setup and hold parameters in terms of clock cycles.
350          * Here, we quantize the setup and hold timing parameters to the
351          * next-highest clock period to make sure we apply at least the
352          * specified times.
353          *
354          * For data setup and data hold, the hardware interprets a value of zero
355          * as the largest possible delay. This is not what's intended by a zero
356          * in the input parameter, so we impose a minimum of one cycle.
357          */
358         data_setup_in_cycles    = ns_to_cycles(target.data_setup_in_ns,
359                                                         clock_period_in_ns, 1);
360         data_hold_in_cycles     = ns_to_cycles(target.data_hold_in_ns,
361                                                         clock_period_in_ns, 1);
362         address_setup_in_cycles = ns_to_cycles(target.address_setup_in_ns,
363                                                         clock_period_in_ns, 0);
364
365         /*
366          * The clock's period affects the sample delay in a number of ways:
367          *
368          * (1) The NFC HAL tells us the maximum clock period the sample delay
369          *     DLL can tolerate. If the clock period is greater than half that
370          *     maximum, we must configure the DLL to be driven by half periods.
371          *
372          * (2) We need to convert from an ideal sample delay, in ns, to a
373          *     "sample delay factor," which the NFC uses. This factor depends on
374          *     whether we're driving the DLL with full or half periods.
375          *     Paraphrasing the reference manual:
376          *
377          *         AD = SDF x 0.125 x RP
378          *
379          * where:
380          *
381          *     AD   is the applied delay, in ns.
382          *     SDF  is the sample delay factor, which is dimensionless.
383          *     RP   is the reference period, in ns, which is a full clock period
384          *          if the DLL is being driven by full periods, or half that if
385          *          the DLL is being driven by half periods.
386          *
387          * Let's re-arrange this in a way that's more useful to us:
388          *
389          *                        8
390          *         SDF  =  AD x ----
391          *                       RP
392          *
393          * The reference period is either the clock period or half that, so this
394          * is:
395          *
396          *                        8       AD x DDF
397          *         SDF  =  AD x -----  =  --------
398          *                      f x P        P
399          *
400          * where:
401          *
402          *       f  is 1 or 1/2, depending on how we're driving the DLL.
403          *       P  is the clock period.
404          *     DDF  is the DLL Delay Factor, a dimensionless value that
405          *          incorporates all the constants in the conversion.
406          *
407          * DDF will be either 8 or 16, both of which are powers of two. We can
408          * reduce the cost of this conversion by using bit shifts instead of
409          * multiplication or division. Thus:
410          *
411          *                 AD << DDS
412          *         SDF  =  ---------
413          *                     P
414          *
415          *     or
416          *
417          *         AD  =  (SDF >> DDS) x P
418          *
419          * where:
420          *
421          *     DDS  is the DLL Delay Shift, the logarithm to base 2 of the DDF.
422          */
423         if (clock_period_in_ns > (nfc->max_dll_clock_period_in_ns >> 1)) {
424                 dll_use_half_periods = true;
425                 dll_delay_shift      = 3 + 1;
426         } else {
427                 dll_use_half_periods = false;
428                 dll_delay_shift      = 3;
429         }
430
431         /*
432          * Compute the maximum sample delay the NFC allows, under current
433          * conditions. If the clock is running too slowly, no sample delay is
434          * possible.
435          */
436         if (clock_period_in_ns > nfc->max_dll_clock_period_in_ns)
437                 max_sample_delay_in_ns = 0;
438         else {
439                 /*
440                  * Compute the delay implied by the largest sample delay factor
441                  * the NFC allows.
442                  */
443                 max_sample_delay_in_ns =
444                         (nfc->max_sample_delay_factor * clock_period_in_ns) >>
445                                                                 dll_delay_shift;
446
447                 /*
448                  * Check if the implied sample delay larger than the NFC
449                  * actually allows.
450                  */
451                 if (max_sample_delay_in_ns > nfc->max_dll_delay_in_ns)
452                         max_sample_delay_in_ns = nfc->max_dll_delay_in_ns;
453         }
454
455         /*
456          * Check if improved timing information is available. If not, we have to
457          * use a less-sophisticated algorithm.
458          */
459         if (!improved_timing_is_available) {
460                 /*
461                  * Fold the read setup time required by the NFC into the ideal
462                  * sample delay.
463                  */
464                 ideal_sample_delay_in_ns = target.gpmi_sample_delay_in_ns +
465                                                 nfc->internal_data_setup_in_ns;
466
467                 /*
468                  * The ideal sample delay may be greater than the maximum
469                  * allowed by the NFC. If so, we can trade off sample delay time
470                  * for more data setup time.
471                  *
472                  * In each iteration of the following loop, we add a cycle to
473                  * the data setup time and subtract a corresponding amount from
474                  * the sample delay until we've satisified the constraints or
475                  * can't do any better.
476                  */
477                 while ((ideal_sample_delay_in_ns > max_sample_delay_in_ns) &&
478                         (data_setup_in_cycles < nfc->max_data_setup_cycles)) {
479
480                         data_setup_in_cycles++;
481                         ideal_sample_delay_in_ns -= clock_period_in_ns;
482
483                         if (ideal_sample_delay_in_ns < 0)
484                                 ideal_sample_delay_in_ns = 0;
485
486                 }
487
488                 /*
489                  * Compute the sample delay factor that corresponds most closely
490                  * to the ideal sample delay. If the result is too large for the
491                  * NFC, use the maximum value.
492                  *
493                  * Notice that we use the ns_to_cycles function to compute the
494                  * sample delay factor. We do this because the form of the
495                  * computation is the same as that for calculating cycles.
496                  */
497                 sample_delay_factor =
498                         ns_to_cycles(
499                                 ideal_sample_delay_in_ns << dll_delay_shift,
500                                                         clock_period_in_ns, 0);
501
502                 if (sample_delay_factor > nfc->max_sample_delay_factor)
503                         sample_delay_factor = nfc->max_sample_delay_factor;
504
505                 /* Skip to the part where we return our results. */
506                 goto return_results;
507         }
508
509         /*
510          * If control arrives here, we have more detailed timing information,
511          * so we can use a better algorithm.
512          */
513
514         /*
515          * Fold the read setup time required by the NFC into the maximum
516          * propagation delay.
517          */
518         max_prop_delay_in_ns += nfc->internal_data_setup_in_ns;
519
520         /*
521          * Earlier, we computed the number of clock cycles required to satisfy
522          * the data setup time. Now, we need to know the actual nanoseconds.
523          */
524         data_setup_in_ns = clock_period_in_ns * data_setup_in_cycles;
525
526         /*
527          * Compute tEYE, the width of the data eye when reading from the NAND
528          * Flash. The eye width is fundamentally determined by the data setup
529          * time, perturbed by propagation delays and some characteristics of the
530          * NAND Flash device.
531          *
532          * start of the eye = max_prop_delay + tREA
533          * end of the eye   = min_prop_delay + tRHOH + data_setup
534          */
535         tEYE = (int)min_prop_delay_in_ns + (int)target.tRHOH_in_ns +
536                                                         (int)data_setup_in_ns;
537
538         tEYE -= (int)max_prop_delay_in_ns + (int)target.tREA_in_ns;
539
540         /*
541          * The eye must be open. If it's not, we can try to open it by
542          * increasing its main forcer, the data setup time.
543          *
544          * In each iteration of the following loop, we increase the data setup
545          * time by a single clock cycle. We do this until either the eye is
546          * open or we run into NFC limits.
547          */
548         while ((tEYE <= 0) &&
549                         (data_setup_in_cycles < nfc->max_data_setup_cycles)) {
550                 /* Give a cycle to data setup. */
551                 data_setup_in_cycles++;
552                 /* Synchronize the data setup time with the cycles. */
553                 data_setup_in_ns += clock_period_in_ns;
554                 /* Adjust tEYE accordingly. */
555                 tEYE += clock_period_in_ns;
556         }
557
558         /*
559          * When control arrives here, the eye is open. The ideal time to sample
560          * the data is in the center of the eye:
561          *
562          *     end of the eye + start of the eye
563          *     ---------------------------------  -  data_setup
564          *                    2
565          *
566          * After some algebra, this simplifies to the code immediately below.
567          */
568         ideal_sample_delay_in_ns =
569                 ((int)max_prop_delay_in_ns +
570                         (int)target.tREA_in_ns +
571                                 (int)min_prop_delay_in_ns +
572                                         (int)target.tRHOH_in_ns -
573                                                 (int)data_setup_in_ns) >> 1;
574
575         /*
576          * The following figure illustrates some aspects of a NAND Flash read:
577          *
578          *
579          *           __                   _____________________________________
580          * RDN         \_________________/
581          *
582          *                                         <---- tEYE ----->
583          *                                        /-----------------\
584          * Read Data ----------------------------<                   >---------
585          *                                        \-----------------/
586          *             ^                 ^                 ^              ^
587          *             |                 |                 |              |
588          *             |<--Data Setup -->|<--Delay Time -->|              |
589          *             |                 |                 |              |
590          *             |                 |                                |
591          *             |                 |<--   Quantized Delay Time   -->|
592          *             |                 |                                |
593          *
594          *
595          * We have some issues we must now address:
596          *
597          * (1) The *ideal* sample delay time must not be negative. If it is, we
598          *     jam it to zero.
599          *
600          * (2) The *ideal* sample delay time must not be greater than that
601          *     allowed by the NFC. If it is, we can increase the data setup
602          *     time, which will reduce the delay between the end of the data
603          *     setup and the center of the eye. It will also make the eye
604          *     larger, which might help with the next issue...
605          *
606          * (3) The *quantized* sample delay time must not fall either before the
607          *     eye opens or after it closes (the latter is the problem
608          *     illustrated in the above figure).
609          */
610
611         /* Jam a negative ideal sample delay to zero. */
612         if (ideal_sample_delay_in_ns < 0)
613                 ideal_sample_delay_in_ns = 0;
614
615         /*
616          * Extend the data setup as needed to reduce the ideal sample delay
617          * below the maximum permitted by the NFC.
618          */
619         while ((ideal_sample_delay_in_ns > max_sample_delay_in_ns) &&
620                         (data_setup_in_cycles < nfc->max_data_setup_cycles)) {
621
622                 /* Give a cycle to data setup. */
623                 data_setup_in_cycles++;
624                 /* Synchronize the data setup time with the cycles. */
625                 data_setup_in_ns += clock_period_in_ns;
626                 /* Adjust tEYE accordingly. */
627                 tEYE += clock_period_in_ns;
628
629                 /*
630                  * Decrease the ideal sample delay by one half cycle, to keep it
631                  * in the middle of the eye.
632                  */
633                 ideal_sample_delay_in_ns -= (clock_period_in_ns >> 1);
634
635                 /* Jam a negative ideal sample delay to zero. */
636                 if (ideal_sample_delay_in_ns < 0)
637                         ideal_sample_delay_in_ns = 0;
638         }
639
640         /*
641          * Compute the sample delay factor that corresponds to the ideal sample
642          * delay. If the result is too large, then use the maximum allowed
643          * value.
644          *
645          * Notice that we use the ns_to_cycles function to compute the sample
646          * delay factor. We do this because the form of the computation is the
647          * same as that for calculating cycles.
648          */
649         sample_delay_factor =
650                 ns_to_cycles(ideal_sample_delay_in_ns << dll_delay_shift,
651                                                         clock_period_in_ns, 0);
652
653         if (sample_delay_factor > nfc->max_sample_delay_factor)
654                 sample_delay_factor = nfc->max_sample_delay_factor;
655
656         /*
657          * These macros conveniently encapsulate a computation we'll use to
658          * continuously evaluate whether or not the data sample delay is inside
659          * the eye.
660          */
661         #define IDEAL_DELAY  ((int) ideal_sample_delay_in_ns)
662
663         #define QUANTIZED_DELAY  \
664                 ((int) ((sample_delay_factor * clock_period_in_ns) >> \
665                                                         dll_delay_shift))
666
667         #define DELAY_ERROR  (abs(QUANTIZED_DELAY - IDEAL_DELAY))
668
669         #define SAMPLE_IS_NOT_WITHIN_THE_EYE  (DELAY_ERROR > (tEYE >> 1))
670
671         /*
672          * While the quantized sample time falls outside the eye, reduce the
673          * sample delay or extend the data setup to move the sampling point back
674          * toward the eye. Do not allow the number of data setup cycles to
675          * exceed the maximum allowed by the NFC.
676          */
677         while (SAMPLE_IS_NOT_WITHIN_THE_EYE &&
678                         (data_setup_in_cycles < nfc->max_data_setup_cycles)) {
679                 /*
680                  * If control arrives here, the quantized sample delay falls
681                  * outside the eye. Check if it's before the eye opens, or after
682                  * the eye closes.
683                  */
684                 if (QUANTIZED_DELAY > IDEAL_DELAY) {
685                         /*
686                          * If control arrives here, the quantized sample delay
687                          * falls after the eye closes. Decrease the quantized
688                          * delay time and then go back to re-evaluate.
689                          */
690                         if (sample_delay_factor != 0)
691                                 sample_delay_factor--;
692                         continue;
693                 }
694
695                 /*
696                  * If control arrives here, the quantized sample delay falls
697                  * before the eye opens. Shift the sample point by increasing
698                  * data setup time. This will also make the eye larger.
699                  */
700
701                 /* Give a cycle to data setup. */
702                 data_setup_in_cycles++;
703                 /* Synchronize the data setup time with the cycles. */
704                 data_setup_in_ns += clock_period_in_ns;
705                 /* Adjust tEYE accordingly. */
706                 tEYE += clock_period_in_ns;
707
708                 /*
709                  * Decrease the ideal sample delay by one half cycle, to keep it
710                  * in the middle of the eye.
711                  */
712                 ideal_sample_delay_in_ns -= (clock_period_in_ns >> 1);
713
714                 /* ...and one less period for the delay time. */
715                 ideal_sample_delay_in_ns -= clock_period_in_ns;
716
717                 /* Jam a negative ideal sample delay to zero. */
718                 if (ideal_sample_delay_in_ns < 0)
719                         ideal_sample_delay_in_ns = 0;
720
721                 /*
722                  * We have a new ideal sample delay, so re-compute the quantized
723                  * delay.
724                  */
725                 sample_delay_factor =
726                         ns_to_cycles(
727                                 ideal_sample_delay_in_ns << dll_delay_shift,
728                                                         clock_period_in_ns, 0);
729
730                 if (sample_delay_factor > nfc->max_sample_delay_factor)
731                         sample_delay_factor = nfc->max_sample_delay_factor;
732         }
733
734         /* Control arrives here when we're ready to return our results. */
735 return_results:
736         hw->data_setup_in_cycles    = data_setup_in_cycles;
737         hw->data_hold_in_cycles     = data_hold_in_cycles;
738         hw->address_setup_in_cycles = address_setup_in_cycles;
739         hw->use_half_periods        = dll_use_half_periods;
740         hw->sample_delay_factor     = sample_delay_factor;
741         hw->device_busy_timeout     = GPMI_DEFAULT_BUSY_TIMEOUT;
742         hw->wrn_dly_sel             = BV_GPMI_CTRL1_WRN_DLY_SEL_4_TO_8NS;
743
744         /* Return success. */
745         return 0;
746 }
747
748 /*
749  * <1> Firstly, we should know what's the GPMI-clock means.
750  *     The GPMI-clock is the internal clock in the gpmi nand controller.
751  *     If you set 100MHz to gpmi nand controller, the GPMI-clock's period
752  *     is 10ns. Mark the GPMI-clock's period as GPMI-clock-period.
753  *
754  * <2> Secondly, we should know what's the frequency on the nand chip pins.
755  *     The frequency on the nand chip pins is derived from the GPMI-clock.
756  *     We can get it from the following equation:
757  *
758  *         F = G / (DS + DH)
759  *
760  *         F  : the frequency on the nand chip pins.
761  *         G  : the GPMI clock, such as 100MHz.
762  *         DS : GPMI_HW_GPMI_TIMING0:DATA_SETUP
763  *         DH : GPMI_HW_GPMI_TIMING0:DATA_HOLD
764  *
765  * <3> Thirdly, when the frequency on the nand chip pins is above 33MHz,
766  *     the nand EDO(extended Data Out) timing could be applied.
767  *     The GPMI implements a feedback read strobe to sample the read data.
768  *     The feedback read strobe can be delayed to support the nand EDO timing
769  *     where the read strobe may deasserts before the read data is valid, and
770  *     read data is valid for some time after read strobe.
771  *
772  *     The following figure illustrates some aspects of a NAND Flash read:
773  *
774  *                   |<---tREA---->|
775  *                   |             |
776  *                   |         |   |
777  *                   |<--tRP-->|   |
778  *                   |         |   |
779  *                  __          ___|__________________________________
780  *     RDN            \________/   |
781  *                                 |
782  *                                 /---------\
783  *     Read Data    --------------<           >---------
784  *                                 \---------/
785  *                                |     |
786  *                                |<-D->|
787  *     FeedbackRDN  ________             ____________
788  *                          \___________/
789  *
790  *          D stands for delay, set in the HW_GPMI_CTRL1:RDN_DELAY.
791  *
792  *
793  * <4> Now, we begin to describe how to compute the right RDN_DELAY.
794  *
795  *  4.1) From the aspect of the nand chip pins:
796  *        Delay = (tREA + C - tRP)               {1}
797  *
798  *        tREA : the maximum read access time. From the ONFI nand standards,
799  *               we know that tREA is 16ns in mode 5, tREA is 20ns is mode 4.
800  *               Please check it in : www.onfi.org
801  *        C    : a constant for adjust the delay. default is 4.
802  *        tRP  : the read pulse width.
803  *               Specified by the HW_GPMI_TIMING0:DATA_SETUP:
804  *                    tRP = (GPMI-clock-period) * DATA_SETUP
805  *
806  *  4.2) From the aspect of the GPMI nand controller:
807  *         Delay = RDN_DELAY * 0.125 * RP        {2}
808  *
809  *         RP   : the DLL reference period.
810  *            if (GPMI-clock-period > DLL_THRETHOLD)
811  *                   RP = GPMI-clock-period / 2;
812  *            else
813  *                   RP = GPMI-clock-period;
814  *
815  *            Set the HW_GPMI_CTRL1:HALF_PERIOD if GPMI-clock-period
816  *            is greater DLL_THRETHOLD. In other SOCs, the DLL_THRETHOLD
817  *            is 16ns, but in mx6q, we use 12ns.
818  *
819  *  4.3) since {1} equals {2}, we get:
820  *
821  *                    (tREA + 4 - tRP) * 8
822  *         RDN_DELAY = ---------------------     {3}
823  *                           RP
824  *
825  *  4.4) We only support the fastest asynchronous mode of ONFI nand.
826  *       For some ONFI nand, the mode 4 is the fastest mode;
827  *       while for some ONFI nand, the mode 5 is the fastest mode.
828  *       So we only support the mode 4 and mode 5. It is no need to
829  *       support other modes.
830  */
831 static void gpmi_compute_edo_timing(struct gpmi_nand_data *this,
832                         struct gpmi_nfc_hardware_timing *hw)
833 {
834         struct resources *r = &this->resources;
835         unsigned long rate = clk_get_rate(r->clock[0]);
836         int mode = this->timing_mode;
837         int dll_threshold = 16; /* in ns */
838         unsigned long delay;
839         unsigned long clk_period;
840         int t_rea;
841         int c = 4;
842         int t_rp;
843         int rp;
844
845         /*
846          * [1] for GPMI_HW_GPMI_TIMING0:
847          *     The async mode requires 40MHz for mode 4, 50MHz for mode 5.
848          *     The GPMI can support 100MHz at most. So if we want to
849          *     get the 40MHz or 50MHz, we have to set DS=1, DH=1.
850          *     Set the ADDRESS_SETUP to 0 in mode 4.
851          */
852         hw->data_setup_in_cycles = 1;
853         hw->data_hold_in_cycles = 1;
854         hw->address_setup_in_cycles = ((mode == 5) ? 1 : 0);
855
856         /* [2] for GPMI_HW_GPMI_TIMING1 */
857         hw->device_busy_timeout = 0x9000;
858
859         /* [3] for GPMI_HW_GPMI_CTRL1 */
860         hw->wrn_dly_sel = BV_GPMI_CTRL1_WRN_DLY_SEL_NO_DELAY;
861
862         if (GPMI_IS_MX6Q(this))
863                 dll_threshold = 12;
864
865         /*
866          * Enlarge 10 times for the numerator and denominator in {3}.
867          * This make us to get more accurate result.
868          */
869         clk_period = NSEC_PER_SEC / (rate / 10);
870         dll_threshold *= 10;
871         t_rea = ((mode == 5) ? 16 : 20) * 10;
872         c *= 10;
873
874         t_rp = clk_period * 1; /* DATA_SETUP is 1 */
875
876         if (clk_period > dll_threshold) {
877                 hw->use_half_periods = 1;
878                 rp = clk_period / 2;
879         } else {
880                 hw->use_half_periods = 0;
881                 rp = clk_period;
882         }
883
884         /*
885          * Multiply the numerator with 10, we could do a round off:
886          *      7.8 round up to 8; 7.4 round down to 7.
887          */
888         delay  = (((t_rea + c - t_rp) * 8) * 10) / rp;
889         delay = (delay + 5) / 10;
890
891         hw->sample_delay_factor = delay;
892 }
893
894 static int enable_edo_mode(struct gpmi_nand_data *this, int mode)
895 {
896         struct resources  *r = &this->resources;
897         struct nand_chip *nand = &this->nand;
898         struct mtd_info  *mtd = &this->mtd;
899         uint8_t feature[ONFI_SUBFEATURE_PARAM_LEN] = {};
900         unsigned long rate;
901         int ret;
902
903         nand->select_chip(mtd, 0);
904
905         /* [1] send SET FEATURE commond to NAND */
906         feature[0] = mode;
907         ret = nand->onfi_set_features(mtd, nand,
908                                 ONFI_FEATURE_ADDR_TIMING_MODE, feature);
909         if (ret)
910                 goto err_out;
911
912         /* [2] send GET FEATURE command to double-check the timing mode */
913         memset(feature, 0, ONFI_SUBFEATURE_PARAM_LEN);
914         ret = nand->onfi_get_features(mtd, nand,
915                                 ONFI_FEATURE_ADDR_TIMING_MODE, feature);
916         if (ret || feature[0] != mode)
917                 goto err_out;
918
919         nand->select_chip(mtd, -1);
920
921         /* [3] set the main IO clock, 100MHz for mode 5, 80MHz for mode 4. */
922         rate = (mode == 5) ? 100000000 : 80000000;
923         clk_set_rate(r->clock[0], rate);
924
925         /* Let the gpmi_begin() re-compute the timing again. */
926         this->flags &= ~GPMI_TIMING_INIT_OK;
927
928         this->flags |= GPMI_ASYNC_EDO_ENABLED;
929         this->timing_mode = mode;
930         dev_info(this->dev, "enable the asynchronous EDO mode %d\n", mode);
931         return 0;
932
933 err_out:
934         nand->select_chip(mtd, -1);
935         dev_err(this->dev, "mode:%d ,failed in set feature.\n", mode);
936         return -EINVAL;
937 }
938
939 int gpmi_extra_init(struct gpmi_nand_data *this)
940 {
941         struct nand_chip *chip = &this->nand;
942
943         /* Enable the asynchronous EDO feature. */
944         if (GPMI_IS_MX6Q(this) && chip->onfi_version) {
945                 int mode = onfi_get_async_timing_mode(chip);
946
947                 /* We only support the timing mode 4 and mode 5. */
948                 if (mode & ONFI_TIMING_MODE_5)
949                         mode = 5;
950                 else if (mode & ONFI_TIMING_MODE_4)
951                         mode = 4;
952                 else
953                         return 0;
954
955                 return enable_edo_mode(this, mode);
956         }
957         return 0;
958 }
959
960 /* Begin the I/O */
961 void gpmi_begin(struct gpmi_nand_data *this)
962 {
963         struct resources *r = &this->resources;
964         void __iomem *gpmi_regs = r->gpmi_regs;
965         unsigned int   clock_period_in_ns;
966         uint32_t       reg;
967         unsigned int   dll_wait_time_in_us;
968         struct gpmi_nfc_hardware_timing  hw;
969         int ret;
970
971         /* Enable the clock. */
972         ret = gpmi_enable_clk(this);
973         if (ret) {
974                 pr_err("We failed in enable the clk\n");
975                 goto err_out;
976         }
977
978         /* Only initialize the timing once */
979         if (this->flags & GPMI_TIMING_INIT_OK)
980                 return;
981         this->flags |= GPMI_TIMING_INIT_OK;
982
983         if (this->flags & GPMI_ASYNC_EDO_ENABLED)
984                 gpmi_compute_edo_timing(this, &hw);
985         else
986                 gpmi_nfc_compute_hardware_timing(this, &hw);
987
988         /* [1] Set HW_GPMI_TIMING0 */
989         reg = BF_GPMI_TIMING0_ADDRESS_SETUP(hw.address_setup_in_cycles) |
990                 BF_GPMI_TIMING0_DATA_HOLD(hw.data_hold_in_cycles)         |
991                 BF_GPMI_TIMING0_DATA_SETUP(hw.data_setup_in_cycles)       ;
992
993         writel(reg, gpmi_regs + HW_GPMI_TIMING0);
994
995         /* [2] Set HW_GPMI_TIMING1 */
996         writel(BF_GPMI_TIMING1_BUSY_TIMEOUT(hw.device_busy_timeout),
997                 gpmi_regs + HW_GPMI_TIMING1);
998
999         /* [3] The following code is to set the HW_GPMI_CTRL1. */
1000
1001         /* Set the WRN_DLY_SEL */
1002         writel(BM_GPMI_CTRL1_WRN_DLY_SEL, gpmi_regs + HW_GPMI_CTRL1_CLR);
1003         writel(BF_GPMI_CTRL1_WRN_DLY_SEL(hw.wrn_dly_sel),
1004                                         gpmi_regs + HW_GPMI_CTRL1_SET);
1005
1006         /* DLL_ENABLE must be set to 0 when setting RDN_DELAY or HALF_PERIOD. */
1007         writel(BM_GPMI_CTRL1_DLL_ENABLE, gpmi_regs + HW_GPMI_CTRL1_CLR);
1008
1009         /* Clear out the DLL control fields. */
1010         reg = BM_GPMI_CTRL1_RDN_DELAY | BM_GPMI_CTRL1_HALF_PERIOD;
1011         writel(reg, gpmi_regs + HW_GPMI_CTRL1_CLR);
1012
1013         /* If no sample delay is called for, return immediately. */
1014         if (!hw.sample_delay_factor)
1015                 return;
1016
1017         /* Set RDN_DELAY or HALF_PERIOD. */
1018         reg = ((hw.use_half_periods) ? BM_GPMI_CTRL1_HALF_PERIOD : 0)
1019                 | BF_GPMI_CTRL1_RDN_DELAY(hw.sample_delay_factor);
1020
1021         writel(reg, gpmi_regs + HW_GPMI_CTRL1_SET);
1022
1023         /* At last, we enable the DLL. */
1024         writel(BM_GPMI_CTRL1_DLL_ENABLE, gpmi_regs + HW_GPMI_CTRL1_SET);
1025
1026         /*
1027          * After we enable the GPMI DLL, we have to wait 64 clock cycles before
1028          * we can use the GPMI. Calculate the amount of time we need to wait,
1029          * in microseconds.
1030          */
1031         clock_period_in_ns = NSEC_PER_SEC / clk_get_rate(r->clock[0]);
1032         dll_wait_time_in_us = (clock_period_in_ns * 64) / 1000;
1033
1034         if (!dll_wait_time_in_us)
1035                 dll_wait_time_in_us = 1;
1036
1037         /* Wait for the DLL to settle. */
1038         udelay(dll_wait_time_in_us);
1039
1040 err_out:
1041         return;
1042 }
1043
1044 void gpmi_end(struct gpmi_nand_data *this)
1045 {
1046         gpmi_disable_clk(this);
1047 }
1048
1049 /* Clears a BCH interrupt. */
1050 void gpmi_clear_bch(struct gpmi_nand_data *this)
1051 {
1052         struct resources *r = &this->resources;
1053         writel(BM_BCH_CTRL_COMPLETE_IRQ, r->bch_regs + HW_BCH_CTRL_CLR);
1054 }
1055
1056 /* Returns the Ready/Busy status of the given chip. */
1057 int gpmi_is_ready(struct gpmi_nand_data *this, unsigned chip)
1058 {
1059         struct resources *r = &this->resources;
1060         uint32_t mask = 0;
1061         uint32_t reg = 0;
1062
1063         if (GPMI_IS_MX23(this)) {
1064                 mask = MX23_BM_GPMI_DEBUG_READY0 << chip;
1065                 reg = readl(r->gpmi_regs + HW_GPMI_DEBUG);
1066         } else if (GPMI_IS_MX28(this) || GPMI_IS_MX6Q(this)) {
1067                 /* MX28 shares the same R/B register as MX6Q. */
1068                 mask = MX28_BF_GPMI_STAT_READY_BUSY(1 << chip);
1069                 reg = readl(r->gpmi_regs + HW_GPMI_STAT);
1070         } else
1071                 pr_err("unknow arch.\n");
1072         return reg & mask;
1073 }
1074
1075 static inline void set_dma_type(struct gpmi_nand_data *this,
1076                                         enum dma_ops_type type)
1077 {
1078         this->last_dma_type = this->dma_type;
1079         this->dma_type = type;
1080 }
1081
1082 int gpmi_send_command(struct gpmi_nand_data *this)
1083 {
1084         struct dma_chan *channel = get_dma_chan(this);
1085         struct dma_async_tx_descriptor *desc;
1086         struct scatterlist *sgl;
1087         int chip = this->current_chip;
1088         u32 pio[3];
1089
1090         /* [1] send out the PIO words */
1091         pio[0] = BF_GPMI_CTRL0_COMMAND_MODE(BV_GPMI_CTRL0_COMMAND_MODE__WRITE)
1092                 | BM_GPMI_CTRL0_WORD_LENGTH
1093                 | BF_GPMI_CTRL0_CS(chip, this)
1094                 | BF_GPMI_CTRL0_LOCK_CS(LOCK_CS_ENABLE, this)
1095                 | BF_GPMI_CTRL0_ADDRESS(BV_GPMI_CTRL0_ADDRESS__NAND_CLE)
1096                 | BM_GPMI_CTRL0_ADDRESS_INCREMENT
1097                 | BF_GPMI_CTRL0_XFER_COUNT(this->command_length);
1098         pio[1] = pio[2] = 0;
1099         desc = dmaengine_prep_slave_sg(channel,
1100                                         (struct scatterlist *)pio,
1101                                         ARRAY_SIZE(pio), DMA_TRANS_NONE, 0);
1102         if (!desc) {
1103                 pr_err("step 1 error\n");
1104                 return -1;
1105         }
1106
1107         /* [2] send out the COMMAND + ADDRESS string stored in @buffer */
1108         sgl = &this->cmd_sgl;
1109
1110         sg_init_one(sgl, this->cmd_buffer, this->command_length);
1111         dma_map_sg(this->dev, sgl, 1, DMA_TO_DEVICE);
1112         desc = dmaengine_prep_slave_sg(channel,
1113                                 sgl, 1, DMA_MEM_TO_DEV,
1114                                 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
1115
1116         if (!desc) {
1117                 pr_err("step 2 error\n");
1118                 return -1;
1119         }
1120
1121         /* [3] submit the DMA */
1122         set_dma_type(this, DMA_FOR_COMMAND);
1123         return start_dma_without_bch_irq(this, desc);
1124 }
1125
1126 int gpmi_send_data(struct gpmi_nand_data *this)
1127 {
1128         struct dma_async_tx_descriptor *desc;
1129         struct dma_chan *channel = get_dma_chan(this);
1130         int chip = this->current_chip;
1131         uint32_t command_mode;
1132         uint32_t address;
1133         u32 pio[2];
1134
1135         /* [1] PIO */
1136         command_mode = BV_GPMI_CTRL0_COMMAND_MODE__WRITE;
1137         address      = BV_GPMI_CTRL0_ADDRESS__NAND_DATA;
1138
1139         pio[0] = BF_GPMI_CTRL0_COMMAND_MODE(command_mode)
1140                 | BM_GPMI_CTRL0_WORD_LENGTH
1141                 | BF_GPMI_CTRL0_CS(chip, this)
1142                 | BF_GPMI_CTRL0_LOCK_CS(LOCK_CS_ENABLE, this)
1143                 | BF_GPMI_CTRL0_ADDRESS(address)
1144                 | BF_GPMI_CTRL0_XFER_COUNT(this->upper_len);
1145         pio[1] = 0;
1146         desc = dmaengine_prep_slave_sg(channel, (struct scatterlist *)pio,
1147                                         ARRAY_SIZE(pio), DMA_TRANS_NONE, 0);
1148         if (!desc) {
1149                 pr_err("step 1 error\n");
1150                 return -1;
1151         }
1152
1153         /* [2] send DMA request */
1154         prepare_data_dma(this, DMA_TO_DEVICE);
1155         desc = dmaengine_prep_slave_sg(channel, &this->data_sgl,
1156                                         1, DMA_MEM_TO_DEV,
1157                                         DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
1158         if (!desc) {
1159                 pr_err("step 2 error\n");
1160                 return -1;
1161         }
1162         /* [3] submit the DMA */
1163         set_dma_type(this, DMA_FOR_WRITE_DATA);
1164         return start_dma_without_bch_irq(this, desc);
1165 }
1166
1167 int gpmi_read_data(struct gpmi_nand_data *this)
1168 {
1169         struct dma_async_tx_descriptor *desc;
1170         struct dma_chan *channel = get_dma_chan(this);
1171         int chip = this->current_chip;
1172         u32 pio[2];
1173
1174         /* [1] : send PIO */
1175         pio[0] = BF_GPMI_CTRL0_COMMAND_MODE(BV_GPMI_CTRL0_COMMAND_MODE__READ)
1176                 | BM_GPMI_CTRL0_WORD_LENGTH
1177                 | BF_GPMI_CTRL0_CS(chip, this)
1178                 | BF_GPMI_CTRL0_LOCK_CS(LOCK_CS_ENABLE, this)
1179                 | BF_GPMI_CTRL0_ADDRESS(BV_GPMI_CTRL0_ADDRESS__NAND_DATA)
1180                 | BF_GPMI_CTRL0_XFER_COUNT(this->upper_len);
1181         pio[1] = 0;
1182         desc = dmaengine_prep_slave_sg(channel,
1183                                         (struct scatterlist *)pio,
1184                                         ARRAY_SIZE(pio), DMA_TRANS_NONE, 0);
1185         if (!desc) {
1186                 pr_err("step 1 error\n");
1187                 return -1;
1188         }
1189
1190         /* [2] : send DMA request */
1191         prepare_data_dma(this, DMA_FROM_DEVICE);
1192         desc = dmaengine_prep_slave_sg(channel, &this->data_sgl,
1193                                         1, DMA_DEV_TO_MEM,
1194                                         DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
1195         if (!desc) {
1196                 pr_err("step 2 error\n");
1197                 return -1;
1198         }
1199
1200         /* [3] : submit the DMA */
1201         set_dma_type(this, DMA_FOR_READ_DATA);
1202         return start_dma_without_bch_irq(this, desc);
1203 }
1204
1205 int gpmi_send_page(struct gpmi_nand_data *this,
1206                         dma_addr_t payload, dma_addr_t auxiliary)
1207 {
1208         struct bch_geometry *geo = &this->bch_geometry;
1209         uint32_t command_mode;
1210         uint32_t address;
1211         uint32_t ecc_command;
1212         uint32_t buffer_mask;
1213         struct dma_async_tx_descriptor *desc;
1214         struct dma_chan *channel = get_dma_chan(this);
1215         int chip = this->current_chip;
1216         u32 pio[6];
1217
1218         /* A DMA descriptor that does an ECC page read. */
1219         command_mode = BV_GPMI_CTRL0_COMMAND_MODE__WRITE;
1220         address      = BV_GPMI_CTRL0_ADDRESS__NAND_DATA;
1221         ecc_command  = BV_GPMI_ECCCTRL_ECC_CMD__BCH_ENCODE;
1222         buffer_mask  = BV_GPMI_ECCCTRL_BUFFER_MASK__BCH_PAGE |
1223                                 BV_GPMI_ECCCTRL_BUFFER_MASK__BCH_AUXONLY;
1224
1225         pio[0] = BF_GPMI_CTRL0_COMMAND_MODE(command_mode)
1226                 | BM_GPMI_CTRL0_WORD_LENGTH
1227                 | BF_GPMI_CTRL0_CS(chip, this)
1228                 | BF_GPMI_CTRL0_LOCK_CS(LOCK_CS_ENABLE, this)
1229                 | BF_GPMI_CTRL0_ADDRESS(address)
1230                 | BF_GPMI_CTRL0_XFER_COUNT(0);
1231         pio[1] = 0;
1232         pio[2] = BM_GPMI_ECCCTRL_ENABLE_ECC
1233                 | BF_GPMI_ECCCTRL_ECC_CMD(ecc_command)
1234                 | BF_GPMI_ECCCTRL_BUFFER_MASK(buffer_mask);
1235         pio[3] = geo->page_size;
1236         pio[4] = payload;
1237         pio[5] = auxiliary;
1238
1239         desc = dmaengine_prep_slave_sg(channel,
1240                                         (struct scatterlist *)pio,
1241                                         ARRAY_SIZE(pio), DMA_TRANS_NONE,
1242                                         DMA_CTRL_ACK);
1243         if (!desc) {
1244                 pr_err("step 2 error\n");
1245                 return -1;
1246         }
1247         set_dma_type(this, DMA_FOR_WRITE_ECC_PAGE);
1248         return start_dma_with_bch_irq(this, desc);
1249 }
1250
1251 int gpmi_read_page(struct gpmi_nand_data *this,
1252                                 dma_addr_t payload, dma_addr_t auxiliary)
1253 {
1254         struct bch_geometry *geo = &this->bch_geometry;
1255         uint32_t command_mode;
1256         uint32_t address;
1257         uint32_t ecc_command;
1258         uint32_t buffer_mask;
1259         struct dma_async_tx_descriptor *desc;
1260         struct dma_chan *channel = get_dma_chan(this);
1261         int chip = this->current_chip;
1262         u32 pio[6];
1263
1264         /* [1] Wait for the chip to report ready. */
1265         command_mode = BV_GPMI_CTRL0_COMMAND_MODE__WAIT_FOR_READY;
1266         address      = BV_GPMI_CTRL0_ADDRESS__NAND_DATA;
1267
1268         pio[0] =  BF_GPMI_CTRL0_COMMAND_MODE(command_mode)
1269                 | BM_GPMI_CTRL0_WORD_LENGTH
1270                 | BF_GPMI_CTRL0_CS(chip, this)
1271                 | BF_GPMI_CTRL0_LOCK_CS(LOCK_CS_ENABLE, this)
1272                 | BF_GPMI_CTRL0_ADDRESS(address)
1273                 | BF_GPMI_CTRL0_XFER_COUNT(0);
1274         pio[1] = 0;
1275         desc = dmaengine_prep_slave_sg(channel,
1276                                 (struct scatterlist *)pio, 2,
1277                                 DMA_TRANS_NONE, 0);
1278         if (!desc) {
1279                 pr_err("step 1 error\n");
1280                 return -1;
1281         }
1282
1283         /* [2] Enable the BCH block and read. */
1284         command_mode = BV_GPMI_CTRL0_COMMAND_MODE__READ;
1285         address      = BV_GPMI_CTRL0_ADDRESS__NAND_DATA;
1286         ecc_command  = BV_GPMI_ECCCTRL_ECC_CMD__BCH_DECODE;
1287         buffer_mask  = BV_GPMI_ECCCTRL_BUFFER_MASK__BCH_PAGE
1288                         | BV_GPMI_ECCCTRL_BUFFER_MASK__BCH_AUXONLY;
1289
1290         pio[0] =  BF_GPMI_CTRL0_COMMAND_MODE(command_mode)
1291                 | BM_GPMI_CTRL0_WORD_LENGTH
1292                 | BF_GPMI_CTRL0_CS(chip, this)
1293                 | BF_GPMI_CTRL0_LOCK_CS(LOCK_CS_ENABLE, this)
1294                 | BF_GPMI_CTRL0_ADDRESS(address)
1295                 | BF_GPMI_CTRL0_XFER_COUNT(geo->page_size);
1296
1297         pio[1] = 0;
1298         pio[2] =  BM_GPMI_ECCCTRL_ENABLE_ECC
1299                 | BF_GPMI_ECCCTRL_ECC_CMD(ecc_command)
1300                 | BF_GPMI_ECCCTRL_BUFFER_MASK(buffer_mask);
1301         pio[3] = geo->page_size;
1302         pio[4] = payload;
1303         pio[5] = auxiliary;
1304         desc = dmaengine_prep_slave_sg(channel,
1305                                         (struct scatterlist *)pio,
1306                                         ARRAY_SIZE(pio), DMA_TRANS_NONE,
1307                                         DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
1308         if (!desc) {
1309                 pr_err("step 2 error\n");
1310                 return -1;
1311         }
1312
1313         /* [3] Disable the BCH block */
1314         command_mode = BV_GPMI_CTRL0_COMMAND_MODE__WAIT_FOR_READY;
1315         address      = BV_GPMI_CTRL0_ADDRESS__NAND_DATA;
1316
1317         pio[0] = BF_GPMI_CTRL0_COMMAND_MODE(command_mode)
1318                 | BM_GPMI_CTRL0_WORD_LENGTH
1319                 | BF_GPMI_CTRL0_CS(chip, this)
1320                 | BF_GPMI_CTRL0_LOCK_CS(LOCK_CS_ENABLE, this)
1321                 | BF_GPMI_CTRL0_ADDRESS(address)
1322                 | BF_GPMI_CTRL0_XFER_COUNT(geo->page_size);
1323         pio[1] = 0;
1324         pio[2] = 0; /* clear GPMI_HW_GPMI_ECCCTRL, disable the BCH. */
1325         desc = dmaengine_prep_slave_sg(channel,
1326                                 (struct scatterlist *)pio, 3,
1327                                 DMA_TRANS_NONE,
1328                                 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
1329         if (!desc) {
1330                 pr_err("step 3 error\n");
1331                 return -1;
1332         }
1333
1334         /* [4] submit the DMA */
1335         set_dma_type(this, DMA_FOR_READ_ECC_PAGE);
1336         return start_dma_with_bch_irq(this, desc);
1337 }