2 * Freescale GPMI NAND Flash Driver
4 * Copyright (C) 2008-2011 Freescale Semiconductor, Inc.
5 * Copyright (C) 2008 Embedded Alley Solutions, Inc.
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.
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.
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.
21 #include <linux/delay.h>
22 #include <linux/clk.h>
24 #include "gpmi-nand.h"
25 #include "gpmi-regs.h"
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,
38 #define MXS_SET_ADDR 0x4
39 #define MXS_CLR_ADDR 0x8
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
45 static int clear_poll_bit(void __iomem *addr, u32 mask)
50 writel(mask, addr + MXS_CLR_ADDR);
53 * SFTRST needs 3 GPMI clocks to settle, the reference manual
54 * recommends to wait 1us.
58 /* poll the bit becoming clear */
59 while ((readl(addr) & mask) && --timeout)
65 #define MODULE_CLKGATE (1 << 30)
66 #define MODULE_SFTRST (1 << 31)
68 * The current mxs_reset_block() will do two things:
69 * [1] enable the module.
70 * [2] reset the module.
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.
81 * To avoid this bug, just add a new parameter `just_enable` for
82 * the mxs_reset_block(), and rewrite it here.
84 static int gpmi_reset_block(void __iomem *reset_addr, bool just_enable)
89 /* clear and poll SFTRST */
90 ret = clear_poll_bit(reset_addr, MODULE_SFTRST);
95 writel(MODULE_CLKGATE, reset_addr + MXS_CLR_ADDR);
98 /* set SFTRST to reset the block */
99 writel(MODULE_SFTRST, reset_addr + MXS_SET_ADDR);
102 /* poll CLKGATE becoming set */
103 while ((!(readl(reset_addr) & MODULE_CLKGATE)) && --timeout)
105 if (unlikely(!timeout))
109 /* clear and poll SFTRST */
110 ret = clear_poll_bit(reset_addr, MODULE_SFTRST);
114 /* clear and poll CLKGATE */
115 ret = clear_poll_bit(reset_addr, MODULE_CLKGATE);
122 pr_err("%s(%p): module reset timeout\n", __func__, reset_addr);
126 static int __gpmi_enable_clk(struct gpmi_nand_data *this, bool v)
132 for (i = 0; i < GPMI_CLK_MAX; i++) {
133 clk = this->resources.clock[i];
138 ret = clk_prepare_enable(clk);
142 clk_disable_unprepare(clk);
149 clk_disable_unprepare(this->resources.clock[i - 1]);
153 #define gpmi_enable_clk(x) __gpmi_enable_clk(x, true)
154 #define gpmi_disable_clk(x) __gpmi_enable_clk(x, false)
156 int gpmi_init(struct gpmi_nand_data *this)
158 struct resources *r = &this->resources;
161 ret = gpmi_enable_clk(this);
164 ret = gpmi_reset_block(r->gpmi_regs, false);
169 * Reset BCH here, too. We got failures otherwise :(
170 * See later BCH reset for explanation of MX23 handling
172 ret = gpmi_reset_block(r->bch_regs, GPMI_IS_MX23(this));
177 /* Choose NAND mode. */
178 writel(BM_GPMI_CTRL1_GPMI_MODE, r->gpmi_regs + HW_GPMI_CTRL1_CLR);
180 /* Set the IRQ polarity. */
181 writel(BM_GPMI_CTRL1_ATA_IRQRDY_POLARITY,
182 r->gpmi_regs + HW_GPMI_CTRL1_SET);
184 /* Disable Write-Protection. */
185 writel(BM_GPMI_CTRL1_DEV_RESET, r->gpmi_regs + HW_GPMI_CTRL1_SET);
187 /* Select BCH ECC. */
188 writel(BM_GPMI_CTRL1_BCH_MODE, r->gpmi_regs + HW_GPMI_CTRL1_SET);
190 gpmi_disable_clk(this);
196 /* This function is very useful. It is called only when the bug occur. */
197 void gpmi_dump_info(struct gpmi_nand_data *this)
199 struct resources *r = &this->resources;
200 struct bch_geometry *geo = &this->bch_geometry;
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);
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);
225 /* Configures the geometry for BCH. */
226 int bch_set_geometry(struct gpmi_nand_data *this)
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;
237 if (common_nfc_set_geometry(this))
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;
246 ret = gpmi_enable_clk(this);
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.
258 ret = gpmi_reset_block(r->bch_regs, GPMI_IS_MX23(this));
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);
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);
274 /* Set *all* chip selects to use layout 0. */
275 writel(0, r->bch_regs + HW_BCH_LAYOUTSELECT);
277 /* Enable interrupts. */
278 writel(BM_BCH_CTRL_COMPLETE_IRQ_EN,
279 r->bch_regs + HW_BCH_CTRL_SET);
281 gpmi_disable_clk(this);
287 /* Converts time in nanoseconds to cycles. */
288 static unsigned int ns_to_cycles(unsigned int time,
289 unsigned int period, unsigned int min)
293 k = (time + period - 1) / period;
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)
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;
320 unsigned int min_prop_delay_in_ns = DEF_MIN_PROP_DELAY;
321 unsigned int max_prop_delay_in_ns = DEF_MAX_PROP_DELAY;
324 * If there are multiple chips, we need to relax the timings to allow
325 * for signal distortion due to higher capacitance.
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;
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) ;
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;
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
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.
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);
366 * The clock's period affects the sample delay in a number of ways:
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.
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:
377 * AD = SDF x 0.125 x RP
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.
387 * Let's re-arrange this in a way that's more useful to us:
393 * The reference period is either the clock period or half that, so this
397 * SDF = AD x ----- = --------
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.
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:
417 * AD = (SDF >> DDS) x P
421 * DDS is the DLL Delay Shift, the logarithm to base 2 of the DDF.
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;
427 dll_use_half_periods = false;
432 * Compute the maximum sample delay the NFC allows, under current
433 * conditions. If the clock is running too slowly, no sample delay is
436 if (clock_period_in_ns > nfc->max_dll_clock_period_in_ns)
437 max_sample_delay_in_ns = 0;
440 * Compute the delay implied by the largest sample delay factor
443 max_sample_delay_in_ns =
444 (nfc->max_sample_delay_factor * clock_period_in_ns) >>
448 * Check if the implied sample delay larger than the NFC
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;
456 * Check if improved timing information is available. If not, we have to
457 * use a less-sophisticated algorithm.
459 if (!improved_timing_is_available) {
461 * Fold the read setup time required by the NFC into the ideal
464 ideal_sample_delay_in_ns = target.gpmi_sample_delay_in_ns +
465 nfc->internal_data_setup_in_ns;
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.
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.
477 while ((ideal_sample_delay_in_ns > max_sample_delay_in_ns) &&
478 (data_setup_in_cycles < nfc->max_data_setup_cycles)) {
480 data_setup_in_cycles++;
481 ideal_sample_delay_in_ns -= clock_period_in_ns;
483 if (ideal_sample_delay_in_ns < 0)
484 ideal_sample_delay_in_ns = 0;
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.
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.
497 sample_delay_factor =
499 ideal_sample_delay_in_ns << dll_delay_shift,
500 clock_period_in_ns, 0);
502 if (sample_delay_factor > nfc->max_sample_delay_factor)
503 sample_delay_factor = nfc->max_sample_delay_factor;
505 /* Skip to the part where we return our results. */
510 * If control arrives here, we have more detailed timing information,
511 * so we can use a better algorithm.
515 * Fold the read setup time required by the NFC into the maximum
518 max_prop_delay_in_ns += nfc->internal_data_setup_in_ns;
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.
524 data_setup_in_ns = clock_period_in_ns * data_setup_in_cycles;
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
532 * start of the eye = max_prop_delay + tREA
533 * end of the eye = min_prop_delay + tRHOH + data_setup
535 tEYE = (int)min_prop_delay_in_ns + (int)target.tRHOH_in_ns +
536 (int)data_setup_in_ns;
538 tEYE -= (int)max_prop_delay_in_ns + (int)target.tREA_in_ns;
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.
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.
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;
559 * When control arrives here, the eye is open. The ideal time to sample
560 * the data is in the center of the eye:
562 * end of the eye + start of the eye
563 * --------------------------------- - data_setup
566 * After some algebra, this simplifies to the code immediately below.
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;
576 * The following figure illustrates some aspects of a NAND Flash read:
579 * __ _____________________________________
580 * RDN \_________________/
583 * /-----------------\
584 * Read Data ----------------------------< >---------
585 * \-----------------/
588 * |<--Data Setup -->|<--Delay Time -->| |
591 * | |<-- Quantized Delay Time -->|
595 * We have some issues we must now address:
597 * (1) The *ideal* sample delay time must not be negative. If it is, we
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...
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).
611 /* Jam a negative ideal sample delay to zero. */
612 if (ideal_sample_delay_in_ns < 0)
613 ideal_sample_delay_in_ns = 0;
616 * Extend the data setup as needed to reduce the ideal sample delay
617 * below the maximum permitted by the NFC.
619 while ((ideal_sample_delay_in_ns > max_sample_delay_in_ns) &&
620 (data_setup_in_cycles < nfc->max_data_setup_cycles)) {
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;
630 * Decrease the ideal sample delay by one half cycle, to keep it
631 * in the middle of the eye.
633 ideal_sample_delay_in_ns -= (clock_period_in_ns >> 1);
635 /* Jam a negative ideal sample delay to zero. */
636 if (ideal_sample_delay_in_ns < 0)
637 ideal_sample_delay_in_ns = 0;
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
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.
649 sample_delay_factor =
650 ns_to_cycles(ideal_sample_delay_in_ns << dll_delay_shift,
651 clock_period_in_ns, 0);
653 if (sample_delay_factor > nfc->max_sample_delay_factor)
654 sample_delay_factor = nfc->max_sample_delay_factor;
657 * These macros conveniently encapsulate a computation we'll use to
658 * continuously evaluate whether or not the data sample delay is inside
661 #define IDEAL_DELAY ((int) ideal_sample_delay_in_ns)
663 #define QUANTIZED_DELAY \
664 ((int) ((sample_delay_factor * clock_period_in_ns) >> \
667 #define DELAY_ERROR (abs(QUANTIZED_DELAY - IDEAL_DELAY))
669 #define SAMPLE_IS_NOT_WITHIN_THE_EYE (DELAY_ERROR > (tEYE >> 1))
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.
677 while (SAMPLE_IS_NOT_WITHIN_THE_EYE &&
678 (data_setup_in_cycles < nfc->max_data_setup_cycles)) {
680 * If control arrives here, the quantized sample delay falls
681 * outside the eye. Check if it's before the eye opens, or after
684 if (QUANTIZED_DELAY > IDEAL_DELAY) {
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.
690 if (sample_delay_factor != 0)
691 sample_delay_factor--;
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.
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;
709 * Decrease the ideal sample delay by one half cycle, to keep it
710 * in the middle of the eye.
712 ideal_sample_delay_in_ns -= (clock_period_in_ns >> 1);
714 /* ...and one less period for the delay time. */
715 ideal_sample_delay_in_ns -= clock_period_in_ns;
717 /* Jam a negative ideal sample delay to zero. */
718 if (ideal_sample_delay_in_ns < 0)
719 ideal_sample_delay_in_ns = 0;
722 * We have a new ideal sample delay, so re-compute the quantized
725 sample_delay_factor =
727 ideal_sample_delay_in_ns << dll_delay_shift,
728 clock_period_in_ns, 0);
730 if (sample_delay_factor > nfc->max_sample_delay_factor)
731 sample_delay_factor = nfc->max_sample_delay_factor;
734 /* Control arrives here when we're ready to return our 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;
744 /* Return success. */
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.
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:
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
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.
772 * The following figure illustrates some aspects of a NAND Flash read:
779 * __ ___|__________________________________
783 * Read Data --------------< >---------
787 * FeedbackRDN ________ ____________
790 * D stands for delay, set in the HW_GPMI_CTRL1:RDN_DELAY.
793 * <4> Now, we begin to describe how to compute the right RDN_DELAY.
795 * 4.1) From the aspect of the nand chip pins:
796 * Delay = (tREA + C - tRP) {1}
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
806 * 4.2) From the aspect of the GPMI nand controller:
807 * Delay = RDN_DELAY * 0.125 * RP {2}
809 * RP : the DLL reference period.
810 * if (GPMI-clock-period > DLL_THRETHOLD)
811 * RP = GPMI-clock-period / 2;
813 * RP = GPMI-clock-period;
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.
819 * 4.3) since {1} equals {2}, we get:
821 * (tREA + 4 - tRP) * 8
822 * RDN_DELAY = --------------------- {3}
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.
831 static void gpmi_compute_edo_timing(struct gpmi_nand_data *this,
832 struct gpmi_nfc_hardware_timing *hw)
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 */
839 unsigned long clk_period;
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.
852 hw->data_setup_in_cycles = 1;
853 hw->data_hold_in_cycles = 1;
854 hw->address_setup_in_cycles = ((mode == 5) ? 1 : 0);
856 /* [2] for GPMI_HW_GPMI_TIMING1 */
857 hw->device_busy_timeout = 0x9000;
859 /* [3] for GPMI_HW_GPMI_CTRL1 */
860 hw->wrn_dly_sel = BV_GPMI_CTRL1_WRN_DLY_SEL_NO_DELAY;
862 if (GPMI_IS_MX6Q(this))
866 * Enlarge 10 times for the numerator and denominator in {3}.
867 * This make us to get more accurate result.
869 clk_period = NSEC_PER_SEC / (rate / 10);
871 t_rea = ((mode == 5) ? 16 : 20) * 10;
874 t_rp = clk_period * 1; /* DATA_SETUP is 1 */
876 if (clk_period > dll_threshold) {
877 hw->use_half_periods = 1;
880 hw->use_half_periods = 0;
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.
888 delay = (((t_rea + c - t_rp) * 8) * 10) / rp;
889 delay = (delay + 5) / 10;
891 hw->sample_delay_factor = delay;
894 static int enable_edo_mode(struct gpmi_nand_data *this, int mode)
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] = {};
903 nand->select_chip(mtd, 0);
905 /* [1] send SET FEATURE commond to NAND */
907 ret = nand->onfi_set_features(mtd, nand,
908 ONFI_FEATURE_ADDR_TIMING_MODE, feature);
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)
919 nand->select_chip(mtd, -1);
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);
925 /* Let the gpmi_begin() re-compute the timing again. */
926 this->flags &= ~GPMI_TIMING_INIT_OK;
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);
934 nand->select_chip(mtd, -1);
935 dev_err(this->dev, "mode:%d ,failed in set feature.\n", mode);
939 int gpmi_extra_init(struct gpmi_nand_data *this)
941 struct nand_chip *chip = &this->nand;
943 /* Enable the asynchronous EDO feature. */
944 if (GPMI_IS_MX6Q(this) && chip->onfi_version) {
945 int mode = onfi_get_async_timing_mode(chip);
947 /* We only support the timing mode 4 and mode 5. */
948 if (mode & ONFI_TIMING_MODE_5)
950 else if (mode & ONFI_TIMING_MODE_4)
955 return enable_edo_mode(this, mode);
961 void gpmi_begin(struct gpmi_nand_data *this)
963 struct resources *r = &this->resources;
964 void __iomem *gpmi_regs = r->gpmi_regs;
965 unsigned int clock_period_in_ns;
967 unsigned int dll_wait_time_in_us;
968 struct gpmi_nfc_hardware_timing hw;
971 /* Enable the clock. */
972 ret = gpmi_enable_clk(this);
974 pr_err("We failed in enable the clk\n");
978 /* Only initialize the timing once */
979 if (this->flags & GPMI_TIMING_INIT_OK)
981 this->flags |= GPMI_TIMING_INIT_OK;
983 if (this->flags & GPMI_ASYNC_EDO_ENABLED)
984 gpmi_compute_edo_timing(this, &hw);
986 gpmi_nfc_compute_hardware_timing(this, &hw);
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) ;
993 writel(reg, gpmi_regs + HW_GPMI_TIMING0);
995 /* [2] Set HW_GPMI_TIMING1 */
996 writel(BF_GPMI_TIMING1_BUSY_TIMEOUT(hw.device_busy_timeout),
997 gpmi_regs + HW_GPMI_TIMING1);
999 /* [3] The following code is to set the HW_GPMI_CTRL1. */
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);
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);
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);
1013 /* If no sample delay is called for, return immediately. */
1014 if (!hw.sample_delay_factor)
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);
1021 writel(reg, gpmi_regs + HW_GPMI_CTRL1_SET);
1023 /* At last, we enable the DLL. */
1024 writel(BM_GPMI_CTRL1_DLL_ENABLE, gpmi_regs + HW_GPMI_CTRL1_SET);
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,
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;
1034 if (!dll_wait_time_in_us)
1035 dll_wait_time_in_us = 1;
1037 /* Wait for the DLL to settle. */
1038 udelay(dll_wait_time_in_us);
1044 void gpmi_end(struct gpmi_nand_data *this)
1046 gpmi_disable_clk(this);
1049 /* Clears a BCH interrupt. */
1050 void gpmi_clear_bch(struct gpmi_nand_data *this)
1052 struct resources *r = &this->resources;
1053 writel(BM_BCH_CTRL_COMPLETE_IRQ, r->bch_regs + HW_BCH_CTRL_CLR);
1056 /* Returns the Ready/Busy status of the given chip. */
1057 int gpmi_is_ready(struct gpmi_nand_data *this, unsigned chip)
1059 struct resources *r = &this->resources;
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);
1071 pr_err("unknow arch.\n");
1075 static inline void set_dma_type(struct gpmi_nand_data *this,
1076 enum dma_ops_type type)
1078 this->last_dma_type = this->dma_type;
1079 this->dma_type = type;
1082 int gpmi_send_command(struct gpmi_nand_data *this)
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;
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);
1103 pr_err("step 1 error\n");
1107 /* [2] send out the COMMAND + ADDRESS string stored in @buffer */
1108 sgl = &this->cmd_sgl;
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);
1117 pr_err("step 2 error\n");
1121 /* [3] submit the DMA */
1122 set_dma_type(this, DMA_FOR_COMMAND);
1123 return start_dma_without_bch_irq(this, desc);
1126 int gpmi_send_data(struct gpmi_nand_data *this)
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;
1136 command_mode = BV_GPMI_CTRL0_COMMAND_MODE__WRITE;
1137 address = BV_GPMI_CTRL0_ADDRESS__NAND_DATA;
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);
1146 desc = dmaengine_prep_slave_sg(channel, (struct scatterlist *)pio,
1147 ARRAY_SIZE(pio), DMA_TRANS_NONE, 0);
1149 pr_err("step 1 error\n");
1153 /* [2] send DMA request */
1154 prepare_data_dma(this, DMA_TO_DEVICE);
1155 desc = dmaengine_prep_slave_sg(channel, &this->data_sgl,
1157 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
1159 pr_err("step 2 error\n");
1162 /* [3] submit the DMA */
1163 set_dma_type(this, DMA_FOR_WRITE_DATA);
1164 return start_dma_without_bch_irq(this, desc);
1167 int gpmi_read_data(struct gpmi_nand_data *this)
1169 struct dma_async_tx_descriptor *desc;
1170 struct dma_chan *channel = get_dma_chan(this);
1171 int chip = this->current_chip;
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);
1182 desc = dmaengine_prep_slave_sg(channel,
1183 (struct scatterlist *)pio,
1184 ARRAY_SIZE(pio), DMA_TRANS_NONE, 0);
1186 pr_err("step 1 error\n");
1190 /* [2] : send DMA request */
1191 prepare_data_dma(this, DMA_FROM_DEVICE);
1192 desc = dmaengine_prep_slave_sg(channel, &this->data_sgl,
1194 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
1196 pr_err("step 2 error\n");
1200 /* [3] : submit the DMA */
1201 set_dma_type(this, DMA_FOR_READ_DATA);
1202 return start_dma_without_bch_irq(this, desc);
1205 int gpmi_send_page(struct gpmi_nand_data *this,
1206 dma_addr_t payload, dma_addr_t auxiliary)
1208 struct bch_geometry *geo = &this->bch_geometry;
1209 uint32_t command_mode;
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;
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;
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);
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;
1239 desc = dmaengine_prep_slave_sg(channel,
1240 (struct scatterlist *)pio,
1241 ARRAY_SIZE(pio), DMA_TRANS_NONE,
1244 pr_err("step 2 error\n");
1247 set_dma_type(this, DMA_FOR_WRITE_ECC_PAGE);
1248 return start_dma_with_bch_irq(this, desc);
1251 int gpmi_read_page(struct gpmi_nand_data *this,
1252 dma_addr_t payload, dma_addr_t auxiliary)
1254 struct bch_geometry *geo = &this->bch_geometry;
1255 uint32_t command_mode;
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;
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;
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);
1275 desc = dmaengine_prep_slave_sg(channel,
1276 (struct scatterlist *)pio, 2,
1279 pr_err("step 1 error\n");
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;
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);
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;
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);
1309 pr_err("step 2 error\n");
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;
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);
1324 pio[2] = 0; /* clear GPMI_HW_GPMI_ECCCTRL, disable the BCH. */
1325 desc = dmaengine_prep_slave_sg(channel,
1326 (struct scatterlist *)pio, 3,
1328 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
1330 pr_err("step 3 error\n");
1334 /* [4] submit the DMA */
1335 set_dma_type(this, DMA_FOR_READ_ECC_PAGE);
1336 return start_dma_with_bch_irq(this, desc);