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