mtd: gpmi: add device tree support to gpmi-nand
[linux-2.6.git] / drivers / mtd / nand / gpmi-nand / gpmi-lib.c
blob5c55c717fe14cab04f79a136af9ad193710ba04c
1 /*
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/mtd/gpmi-nand.h>
22 #include <linux/delay.h>
23 #include <linux/clk.h>
25 #include "gpmi-nand.h"
26 #include "gpmi-regs.h"
27 #include "bch-regs.h"
29 struct timing_threshod timing_default_threshold = {
30 .max_data_setup_cycles = (BM_GPMI_TIMING0_DATA_SETUP >>
31 BP_GPMI_TIMING0_DATA_SETUP),
32 .internal_data_setup_in_ns = 0,
33 .max_sample_delay_factor = (BM_GPMI_CTRL1_RDN_DELAY >>
34 BP_GPMI_CTRL1_RDN_DELAY),
35 .max_dll_clock_period_in_ns = 32,
36 .max_dll_delay_in_ns = 16,
39 #define MXS_SET_ADDR 0x4
40 #define MXS_CLR_ADDR 0x8
42 * Clear the bit and poll it cleared. This is usually called with
43 * a reset address and mask being either SFTRST(bit 31) or CLKGATE
44 * (bit 30).
46 static int clear_poll_bit(void __iomem *addr, u32 mask)
48 int timeout = 0x400;
50 /* clear the bit */
51 writel(mask, addr + MXS_CLR_ADDR);
54 * SFTRST needs 3 GPMI clocks to settle, the reference manual
55 * recommends to wait 1us.
57 udelay(1);
59 /* poll the bit becoming clear */
60 while ((readl(addr) & mask) && --timeout)
61 /* nothing */;
63 return !timeout;
66 #define MODULE_CLKGATE (1 << 30)
67 #define MODULE_SFTRST (1 << 31)
69 * The current mxs_reset_block() will do two things:
70 * [1] enable the module.
71 * [2] reset the module.
73 * In most of the cases, it's ok.
74 * But in MX23, there is a hardware bug in the BCH block (see erratum #2847).
75 * If you try to soft reset the BCH block, it becomes unusable until
76 * the next hard reset. This case occurs in the NAND boot mode. When the board
77 * boots by NAND, the ROM of the chip will initialize the BCH blocks itself.
78 * So If the driver tries to reset the BCH again, the BCH will not work anymore.
79 * You will see a DMA timeout in this case. The bug has been fixed
80 * in the following chips, such as MX28.
82 * To avoid this bug, just add a new parameter `just_enable` for
83 * the mxs_reset_block(), and rewrite it here.
85 static int gpmi_reset_block(void __iomem *reset_addr, bool just_enable)
87 int ret;
88 int timeout = 0x400;
90 /* clear and poll SFTRST */
91 ret = clear_poll_bit(reset_addr, MODULE_SFTRST);
92 if (unlikely(ret))
93 goto error;
95 /* clear CLKGATE */
96 writel(MODULE_CLKGATE, reset_addr + MXS_CLR_ADDR);
98 if (!just_enable) {
99 /* set SFTRST to reset the block */
100 writel(MODULE_SFTRST, reset_addr + MXS_SET_ADDR);
101 udelay(1);
103 /* poll CLKGATE becoming set */
104 while ((!(readl(reset_addr) & MODULE_CLKGATE)) && --timeout)
105 /* nothing */;
106 if (unlikely(!timeout))
107 goto error;
110 /* clear and poll SFTRST */
111 ret = clear_poll_bit(reset_addr, MODULE_SFTRST);
112 if (unlikely(ret))
113 goto error;
115 /* clear and poll CLKGATE */
116 ret = clear_poll_bit(reset_addr, MODULE_CLKGATE);
117 if (unlikely(ret))
118 goto error;
120 return 0;
122 error:
123 pr_err("%s(%p): module reset timeout\n", __func__, reset_addr);
124 return -ETIMEDOUT;
127 int gpmi_init(struct gpmi_nand_data *this)
129 struct resources *r = &this->resources;
130 int ret;
132 ret = clk_prepare_enable(r->clock);
133 if (ret)
134 goto err_out;
135 ret = gpmi_reset_block(r->gpmi_regs, false);
136 if (ret)
137 goto err_out;
139 /* Choose NAND mode. */
140 writel(BM_GPMI_CTRL1_GPMI_MODE, r->gpmi_regs + HW_GPMI_CTRL1_CLR);
142 /* Set the IRQ polarity. */
143 writel(BM_GPMI_CTRL1_ATA_IRQRDY_POLARITY,
144 r->gpmi_regs + HW_GPMI_CTRL1_SET);
146 /* Disable Write-Protection. */
147 writel(BM_GPMI_CTRL1_DEV_RESET, r->gpmi_regs + HW_GPMI_CTRL1_SET);
149 /* Select BCH ECC. */
150 writel(BM_GPMI_CTRL1_BCH_MODE, r->gpmi_regs + HW_GPMI_CTRL1_SET);
152 clk_disable_unprepare(r->clock);
153 return 0;
154 err_out:
155 return ret;
158 /* This function is very useful. It is called only when the bug occur. */
159 void gpmi_dump_info(struct gpmi_nand_data *this)
161 struct resources *r = &this->resources;
162 struct bch_geometry *geo = &this->bch_geometry;
163 u32 reg;
164 int i;
166 pr_err("Show GPMI registers :\n");
167 for (i = 0; i <= HW_GPMI_DEBUG / 0x10 + 1; i++) {
168 reg = readl(r->gpmi_regs + i * 0x10);
169 pr_err("offset 0x%.3x : 0x%.8x\n", i * 0x10, reg);
172 /* start to print out the BCH info */
173 pr_err("BCH Geometry :\n");
174 pr_err("GF length : %u\n", geo->gf_len);
175 pr_err("ECC Strength : %u\n", geo->ecc_strength);
176 pr_err("Page Size in Bytes : %u\n", geo->page_size);
177 pr_err("Metadata Size in Bytes : %u\n", geo->metadata_size);
178 pr_err("ECC Chunk Size in Bytes: %u\n", geo->ecc_chunk_size);
179 pr_err("ECC Chunk Count : %u\n", geo->ecc_chunk_count);
180 pr_err("Payload Size in Bytes : %u\n", geo->payload_size);
181 pr_err("Auxiliary Size in Bytes: %u\n", geo->auxiliary_size);
182 pr_err("Auxiliary Status Offset: %u\n", geo->auxiliary_status_offset);
183 pr_err("Block Mark Byte Offset : %u\n", geo->block_mark_byte_offset);
184 pr_err("Block Mark Bit Offset : %u\n", geo->block_mark_bit_offset);
187 /* Configures the geometry for BCH. */
188 int bch_set_geometry(struct gpmi_nand_data *this)
190 struct resources *r = &this->resources;
191 struct bch_geometry *bch_geo = &this->bch_geometry;
192 unsigned int block_count;
193 unsigned int block_size;
194 unsigned int metadata_size;
195 unsigned int ecc_strength;
196 unsigned int page_size;
197 int ret;
199 if (common_nfc_set_geometry(this))
200 return !0;
202 block_count = bch_geo->ecc_chunk_count - 1;
203 block_size = bch_geo->ecc_chunk_size;
204 metadata_size = bch_geo->metadata_size;
205 ecc_strength = bch_geo->ecc_strength >> 1;
206 page_size = bch_geo->page_size;
208 ret = clk_prepare_enable(r->clock);
209 if (ret)
210 goto err_out;
213 * Due to erratum #2847 of the MX23, the BCH cannot be soft reset on this
214 * chip, otherwise it will lock up. So we skip resetting BCH on the MX23.
215 * On the other hand, the MX28 needs the reset, because one case has been
216 * seen where the BCH produced ECC errors constantly after 10000
217 * consecutive reboots. The latter case has not been seen on the MX23 yet,
218 * still we don't know if it could happen there as well.
220 ret = gpmi_reset_block(r->bch_regs, GPMI_IS_MX23(this));
221 if (ret)
222 goto err_out;
224 /* Configure layout 0. */
225 writel(BF_BCH_FLASH0LAYOUT0_NBLOCKS(block_count)
226 | BF_BCH_FLASH0LAYOUT0_META_SIZE(metadata_size)
227 | BF_BCH_FLASH0LAYOUT0_ECC0(ecc_strength)
228 | BF_BCH_FLASH0LAYOUT0_DATA0_SIZE(block_size),
229 r->bch_regs + HW_BCH_FLASH0LAYOUT0);
231 writel(BF_BCH_FLASH0LAYOUT1_PAGE_SIZE(page_size)
232 | BF_BCH_FLASH0LAYOUT1_ECCN(ecc_strength)
233 | BF_BCH_FLASH0LAYOUT1_DATAN_SIZE(block_size),
234 r->bch_regs + HW_BCH_FLASH0LAYOUT1);
236 /* Set *all* chip selects to use layout 0. */
237 writel(0, r->bch_regs + HW_BCH_LAYOUTSELECT);
239 /* Enable interrupts. */
240 writel(BM_BCH_CTRL_COMPLETE_IRQ_EN,
241 r->bch_regs + HW_BCH_CTRL_SET);
243 clk_disable_unprepare(r->clock);
244 return 0;
245 err_out:
246 return ret;
249 /* Converts time in nanoseconds to cycles. */
250 static unsigned int ns_to_cycles(unsigned int time,
251 unsigned int period, unsigned int min)
253 unsigned int k;
255 k = (time + period - 1) / period;
256 return max(k, min);
259 #define DEF_MIN_PROP_DELAY 5
260 #define DEF_MAX_PROP_DELAY 9
261 /* Apply timing to current hardware conditions. */
262 static int gpmi_nfc_compute_hardware_timing(struct gpmi_nand_data *this,
263 struct gpmi_nfc_hardware_timing *hw)
265 struct timing_threshod *nfc = &timing_default_threshold;
266 struct nand_chip *nand = &this->nand;
267 struct nand_timing target = this->timing;
268 bool improved_timing_is_available;
269 unsigned long clock_frequency_in_hz;
270 unsigned int clock_period_in_ns;
271 bool dll_use_half_periods;
272 unsigned int dll_delay_shift;
273 unsigned int max_sample_delay_in_ns;
274 unsigned int address_setup_in_cycles;
275 unsigned int data_setup_in_ns;
276 unsigned int data_setup_in_cycles;
277 unsigned int data_hold_in_cycles;
278 int ideal_sample_delay_in_ns;
279 unsigned int sample_delay_factor;
280 int tEYE;
281 unsigned int min_prop_delay_in_ns = DEF_MIN_PROP_DELAY;
282 unsigned int max_prop_delay_in_ns = DEF_MAX_PROP_DELAY;
285 * If there are multiple chips, we need to relax the timings to allow
286 * for signal distortion due to higher capacitance.
288 if (nand->numchips > 2) {
289 target.data_setup_in_ns += 10;
290 target.data_hold_in_ns += 10;
291 target.address_setup_in_ns += 10;
292 } else if (nand->numchips > 1) {
293 target.data_setup_in_ns += 5;
294 target.data_hold_in_ns += 5;
295 target.address_setup_in_ns += 5;
298 /* Check if improved timing information is available. */
299 improved_timing_is_available =
300 (target.tREA_in_ns >= 0) &&
301 (target.tRLOH_in_ns >= 0) &&
302 (target.tRHOH_in_ns >= 0) ;
304 /* Inspect the clock. */
305 clock_frequency_in_hz = nfc->clock_frequency_in_hz;
306 clock_period_in_ns = 1000000000 / clock_frequency_in_hz;
309 * The NFC quantizes setup and hold parameters in terms of clock cycles.
310 * Here, we quantize the setup and hold timing parameters to the
311 * next-highest clock period to make sure we apply at least the
312 * specified times.
314 * For data setup and data hold, the hardware interprets a value of zero
315 * as the largest possible delay. This is not what's intended by a zero
316 * in the input parameter, so we impose a minimum of one cycle.
318 data_setup_in_cycles = ns_to_cycles(target.data_setup_in_ns,
319 clock_period_in_ns, 1);
320 data_hold_in_cycles = ns_to_cycles(target.data_hold_in_ns,
321 clock_period_in_ns, 1);
322 address_setup_in_cycles = ns_to_cycles(target.address_setup_in_ns,
323 clock_period_in_ns, 0);
326 * The clock's period affects the sample delay in a number of ways:
328 * (1) The NFC HAL tells us the maximum clock period the sample delay
329 * DLL can tolerate. If the clock period is greater than half that
330 * maximum, we must configure the DLL to be driven by half periods.
332 * (2) We need to convert from an ideal sample delay, in ns, to a
333 * "sample delay factor," which the NFC uses. This factor depends on
334 * whether we're driving the DLL with full or half periods.
335 * Paraphrasing the reference manual:
337 * AD = SDF x 0.125 x RP
339 * where:
341 * AD is the applied delay, in ns.
342 * SDF is the sample delay factor, which is dimensionless.
343 * RP is the reference period, in ns, which is a full clock period
344 * if the DLL is being driven by full periods, or half that if
345 * the DLL is being driven by half periods.
347 * Let's re-arrange this in a way that's more useful to us:
350 * SDF = AD x ----
351 * RP
353 * The reference period is either the clock period or half that, so this
354 * is:
356 * 8 AD x DDF
357 * SDF = AD x ----- = --------
358 * f x P P
360 * where:
362 * f is 1 or 1/2, depending on how we're driving the DLL.
363 * P is the clock period.
364 * DDF is the DLL Delay Factor, a dimensionless value that
365 * incorporates all the constants in the conversion.
367 * DDF will be either 8 or 16, both of which are powers of two. We can
368 * reduce the cost of this conversion by using bit shifts instead of
369 * multiplication or division. Thus:
371 * AD << DDS
372 * SDF = ---------
375 * or
377 * AD = (SDF >> DDS) x P
379 * where:
381 * DDS is the DLL Delay Shift, the logarithm to base 2 of the DDF.
383 if (clock_period_in_ns > (nfc->max_dll_clock_period_in_ns >> 1)) {
384 dll_use_half_periods = true;
385 dll_delay_shift = 3 + 1;
386 } else {
387 dll_use_half_periods = false;
388 dll_delay_shift = 3;
392 * Compute the maximum sample delay the NFC allows, under current
393 * conditions. If the clock is running too slowly, no sample delay is
394 * possible.
396 if (clock_period_in_ns > nfc->max_dll_clock_period_in_ns)
397 max_sample_delay_in_ns = 0;
398 else {
400 * Compute the delay implied by the largest sample delay factor
401 * the NFC allows.
403 max_sample_delay_in_ns =
404 (nfc->max_sample_delay_factor * clock_period_in_ns) >>
405 dll_delay_shift;
408 * Check if the implied sample delay larger than the NFC
409 * actually allows.
411 if (max_sample_delay_in_ns > nfc->max_dll_delay_in_ns)
412 max_sample_delay_in_ns = nfc->max_dll_delay_in_ns;
416 * Check if improved timing information is available. If not, we have to
417 * use a less-sophisticated algorithm.
419 if (!improved_timing_is_available) {
421 * Fold the read setup time required by the NFC into the ideal
422 * sample delay.
424 ideal_sample_delay_in_ns = target.gpmi_sample_delay_in_ns +
425 nfc->internal_data_setup_in_ns;
428 * The ideal sample delay may be greater than the maximum
429 * allowed by the NFC. If so, we can trade off sample delay time
430 * for more data setup time.
432 * In each iteration of the following loop, we add a cycle to
433 * the data setup time and subtract a corresponding amount from
434 * the sample delay until we've satisified the constraints or
435 * can't do any better.
437 while ((ideal_sample_delay_in_ns > max_sample_delay_in_ns) &&
438 (data_setup_in_cycles < nfc->max_data_setup_cycles)) {
440 data_setup_in_cycles++;
441 ideal_sample_delay_in_ns -= clock_period_in_ns;
443 if (ideal_sample_delay_in_ns < 0)
444 ideal_sample_delay_in_ns = 0;
449 * Compute the sample delay factor that corresponds most closely
450 * to the ideal sample delay. If the result is too large for the
451 * NFC, use the maximum value.
453 * Notice that we use the ns_to_cycles function to compute the
454 * sample delay factor. We do this because the form of the
455 * computation is the same as that for calculating cycles.
457 sample_delay_factor =
458 ns_to_cycles(
459 ideal_sample_delay_in_ns << dll_delay_shift,
460 clock_period_in_ns, 0);
462 if (sample_delay_factor > nfc->max_sample_delay_factor)
463 sample_delay_factor = nfc->max_sample_delay_factor;
465 /* Skip to the part where we return our results. */
466 goto return_results;
470 * If control arrives here, we have more detailed timing information,
471 * so we can use a better algorithm.
475 * Fold the read setup time required by the NFC into the maximum
476 * propagation delay.
478 max_prop_delay_in_ns += nfc->internal_data_setup_in_ns;
481 * Earlier, we computed the number of clock cycles required to satisfy
482 * the data setup time. Now, we need to know the actual nanoseconds.
484 data_setup_in_ns = clock_period_in_ns * data_setup_in_cycles;
487 * Compute tEYE, the width of the data eye when reading from the NAND
488 * Flash. The eye width is fundamentally determined by the data setup
489 * time, perturbed by propagation delays and some characteristics of the
490 * NAND Flash device.
492 * start of the eye = max_prop_delay + tREA
493 * end of the eye = min_prop_delay + tRHOH + data_setup
495 tEYE = (int)min_prop_delay_in_ns + (int)target.tRHOH_in_ns +
496 (int)data_setup_in_ns;
498 tEYE -= (int)max_prop_delay_in_ns + (int)target.tREA_in_ns;
501 * The eye must be open. If it's not, we can try to open it by
502 * increasing its main forcer, the data setup time.
504 * In each iteration of the following loop, we increase the data setup
505 * time by a single clock cycle. We do this until either the eye is
506 * open or we run into NFC limits.
508 while ((tEYE <= 0) &&
509 (data_setup_in_cycles < nfc->max_data_setup_cycles)) {
510 /* Give a cycle to data setup. */
511 data_setup_in_cycles++;
512 /* Synchronize the data setup time with the cycles. */
513 data_setup_in_ns += clock_period_in_ns;
514 /* Adjust tEYE accordingly. */
515 tEYE += clock_period_in_ns;
519 * When control arrives here, the eye is open. The ideal time to sample
520 * the data is in the center of the eye:
522 * end of the eye + start of the eye
523 * --------------------------------- - data_setup
526 * After some algebra, this simplifies to the code immediately below.
528 ideal_sample_delay_in_ns =
529 ((int)max_prop_delay_in_ns +
530 (int)target.tREA_in_ns +
531 (int)min_prop_delay_in_ns +
532 (int)target.tRHOH_in_ns -
533 (int)data_setup_in_ns) >> 1;
536 * The following figure illustrates some aspects of a NAND Flash read:
539 * __ _____________________________________
540 * RDN \_________________/
542 * <---- tEYE ----->
543 * /-----------------\
544 * Read Data ----------------------------< >---------
545 * \-----------------/
546 * ^ ^ ^ ^
547 * | | | |
548 * |<--Data Setup -->|<--Delay Time -->| |
549 * | | | |
550 * | | |
551 * | |<-- Quantized Delay Time -->|
552 * | | |
555 * We have some issues we must now address:
557 * (1) The *ideal* sample delay time must not be negative. If it is, we
558 * jam it to zero.
560 * (2) The *ideal* sample delay time must not be greater than that
561 * allowed by the NFC. If it is, we can increase the data setup
562 * time, which will reduce the delay between the end of the data
563 * setup and the center of the eye. It will also make the eye
564 * larger, which might help with the next issue...
566 * (3) The *quantized* sample delay time must not fall either before the
567 * eye opens or after it closes (the latter is the problem
568 * illustrated in the above figure).
571 /* Jam a negative ideal sample delay to zero. */
572 if (ideal_sample_delay_in_ns < 0)
573 ideal_sample_delay_in_ns = 0;
576 * Extend the data setup as needed to reduce the ideal sample delay
577 * below the maximum permitted by the NFC.
579 while ((ideal_sample_delay_in_ns > max_sample_delay_in_ns) &&
580 (data_setup_in_cycles < nfc->max_data_setup_cycles)) {
582 /* Give a cycle to data setup. */
583 data_setup_in_cycles++;
584 /* Synchronize the data setup time with the cycles. */
585 data_setup_in_ns += clock_period_in_ns;
586 /* Adjust tEYE accordingly. */
587 tEYE += clock_period_in_ns;
590 * Decrease the ideal sample delay by one half cycle, to keep it
591 * in the middle of the eye.
593 ideal_sample_delay_in_ns -= (clock_period_in_ns >> 1);
595 /* Jam a negative ideal sample delay to zero. */
596 if (ideal_sample_delay_in_ns < 0)
597 ideal_sample_delay_in_ns = 0;
601 * Compute the sample delay factor that corresponds to the ideal sample
602 * delay. If the result is too large, then use the maximum allowed
603 * value.
605 * Notice that we use the ns_to_cycles function to compute the sample
606 * delay factor. We do this because the form of the computation is the
607 * same as that for calculating cycles.
609 sample_delay_factor =
610 ns_to_cycles(ideal_sample_delay_in_ns << dll_delay_shift,
611 clock_period_in_ns, 0);
613 if (sample_delay_factor > nfc->max_sample_delay_factor)
614 sample_delay_factor = nfc->max_sample_delay_factor;
617 * These macros conveniently encapsulate a computation we'll use to
618 * continuously evaluate whether or not the data sample delay is inside
619 * the eye.
621 #define IDEAL_DELAY ((int) ideal_sample_delay_in_ns)
623 #define QUANTIZED_DELAY \
624 ((int) ((sample_delay_factor * clock_period_in_ns) >> \
625 dll_delay_shift))
627 #define DELAY_ERROR (abs(QUANTIZED_DELAY - IDEAL_DELAY))
629 #define SAMPLE_IS_NOT_WITHIN_THE_EYE (DELAY_ERROR > (tEYE >> 1))
632 * While the quantized sample time falls outside the eye, reduce the
633 * sample delay or extend the data setup to move the sampling point back
634 * toward the eye. Do not allow the number of data setup cycles to
635 * exceed the maximum allowed by the NFC.
637 while (SAMPLE_IS_NOT_WITHIN_THE_EYE &&
638 (data_setup_in_cycles < nfc->max_data_setup_cycles)) {
640 * If control arrives here, the quantized sample delay falls
641 * outside the eye. Check if it's before the eye opens, or after
642 * the eye closes.
644 if (QUANTIZED_DELAY > IDEAL_DELAY) {
646 * If control arrives here, the quantized sample delay
647 * falls after the eye closes. Decrease the quantized
648 * delay time and then go back to re-evaluate.
650 if (sample_delay_factor != 0)
651 sample_delay_factor--;
652 continue;
656 * If control arrives here, the quantized sample delay falls
657 * before the eye opens. Shift the sample point by increasing
658 * data setup time. This will also make the eye larger.
661 /* Give a cycle to data setup. */
662 data_setup_in_cycles++;
663 /* Synchronize the data setup time with the cycles. */
664 data_setup_in_ns += clock_period_in_ns;
665 /* Adjust tEYE accordingly. */
666 tEYE += clock_period_in_ns;
669 * Decrease the ideal sample delay by one half cycle, to keep it
670 * in the middle of the eye.
672 ideal_sample_delay_in_ns -= (clock_period_in_ns >> 1);
674 /* ...and one less period for the delay time. */
675 ideal_sample_delay_in_ns -= clock_period_in_ns;
677 /* Jam a negative ideal sample delay to zero. */
678 if (ideal_sample_delay_in_ns < 0)
679 ideal_sample_delay_in_ns = 0;
682 * We have a new ideal sample delay, so re-compute the quantized
683 * delay.
685 sample_delay_factor =
686 ns_to_cycles(
687 ideal_sample_delay_in_ns << dll_delay_shift,
688 clock_period_in_ns, 0);
690 if (sample_delay_factor > nfc->max_sample_delay_factor)
691 sample_delay_factor = nfc->max_sample_delay_factor;
694 /* Control arrives here when we're ready to return our results. */
695 return_results:
696 hw->data_setup_in_cycles = data_setup_in_cycles;
697 hw->data_hold_in_cycles = data_hold_in_cycles;
698 hw->address_setup_in_cycles = address_setup_in_cycles;
699 hw->use_half_periods = dll_use_half_periods;
700 hw->sample_delay_factor = sample_delay_factor;
702 /* Return success. */
703 return 0;
706 /* Begin the I/O */
707 void gpmi_begin(struct gpmi_nand_data *this)
709 struct resources *r = &this->resources;
710 struct timing_threshod *nfc = &timing_default_threshold;
711 unsigned char *gpmi_regs = r->gpmi_regs;
712 unsigned int clock_period_in_ns;
713 uint32_t reg;
714 unsigned int dll_wait_time_in_us;
715 struct gpmi_nfc_hardware_timing hw;
716 int ret;
718 /* Enable the clock. */
719 ret = clk_prepare_enable(r->clock);
720 if (ret) {
721 pr_err("We failed in enable the clk\n");
722 goto err_out;
725 /* set ready/busy timeout */
726 writel(0x500 << BP_GPMI_TIMING1_BUSY_TIMEOUT,
727 gpmi_regs + HW_GPMI_TIMING1);
729 /* Get the timing information we need. */
730 nfc->clock_frequency_in_hz = clk_get_rate(r->clock);
731 clock_period_in_ns = 1000000000 / nfc->clock_frequency_in_hz;
733 gpmi_nfc_compute_hardware_timing(this, &hw);
735 /* Set up all the simple timing parameters. */
736 reg = BF_GPMI_TIMING0_ADDRESS_SETUP(hw.address_setup_in_cycles) |
737 BF_GPMI_TIMING0_DATA_HOLD(hw.data_hold_in_cycles) |
738 BF_GPMI_TIMING0_DATA_SETUP(hw.data_setup_in_cycles) ;
740 writel(reg, gpmi_regs + HW_GPMI_TIMING0);
743 * DLL_ENABLE must be set to 0 when setting RDN_DELAY or HALF_PERIOD.
745 writel(BM_GPMI_CTRL1_DLL_ENABLE, gpmi_regs + HW_GPMI_CTRL1_CLR);
747 /* Clear out the DLL control fields. */
748 writel(BM_GPMI_CTRL1_RDN_DELAY, gpmi_regs + HW_GPMI_CTRL1_CLR);
749 writel(BM_GPMI_CTRL1_HALF_PERIOD, gpmi_regs + HW_GPMI_CTRL1_CLR);
751 /* If no sample delay is called for, return immediately. */
752 if (!hw.sample_delay_factor)
753 return;
755 /* Configure the HALF_PERIOD flag. */
756 if (hw.use_half_periods)
757 writel(BM_GPMI_CTRL1_HALF_PERIOD,
758 gpmi_regs + HW_GPMI_CTRL1_SET);
760 /* Set the delay factor. */
761 writel(BF_GPMI_CTRL1_RDN_DELAY(hw.sample_delay_factor),
762 gpmi_regs + HW_GPMI_CTRL1_SET);
764 /* Enable the DLL. */
765 writel(BM_GPMI_CTRL1_DLL_ENABLE, gpmi_regs + HW_GPMI_CTRL1_SET);
768 * After we enable the GPMI DLL, we have to wait 64 clock cycles before
769 * we can use the GPMI.
771 * Calculate the amount of time we need to wait, in microseconds.
773 dll_wait_time_in_us = (clock_period_in_ns * 64) / 1000;
775 if (!dll_wait_time_in_us)
776 dll_wait_time_in_us = 1;
778 /* Wait for the DLL to settle. */
779 udelay(dll_wait_time_in_us);
781 err_out:
782 return;
785 void gpmi_end(struct gpmi_nand_data *this)
787 struct resources *r = &this->resources;
788 clk_disable_unprepare(r->clock);
791 /* Clears a BCH interrupt. */
792 void gpmi_clear_bch(struct gpmi_nand_data *this)
794 struct resources *r = &this->resources;
795 writel(BM_BCH_CTRL_COMPLETE_IRQ, r->bch_regs + HW_BCH_CTRL_CLR);
798 /* Returns the Ready/Busy status of the given chip. */
799 int gpmi_is_ready(struct gpmi_nand_data *this, unsigned chip)
801 struct resources *r = &this->resources;
802 uint32_t mask = 0;
803 uint32_t reg = 0;
805 if (GPMI_IS_MX23(this)) {
806 mask = MX23_BM_GPMI_DEBUG_READY0 << chip;
807 reg = readl(r->gpmi_regs + HW_GPMI_DEBUG);
808 } else if (GPMI_IS_MX28(this)) {
809 mask = MX28_BF_GPMI_STAT_READY_BUSY(1 << chip);
810 reg = readl(r->gpmi_regs + HW_GPMI_STAT);
811 } else
812 pr_err("unknow arch.\n");
813 return reg & mask;
816 static inline void set_dma_type(struct gpmi_nand_data *this,
817 enum dma_ops_type type)
819 this->last_dma_type = this->dma_type;
820 this->dma_type = type;
823 int gpmi_send_command(struct gpmi_nand_data *this)
825 struct dma_chan *channel = get_dma_chan(this);
826 struct dma_async_tx_descriptor *desc;
827 struct scatterlist *sgl;
828 int chip = this->current_chip;
829 u32 pio[3];
831 /* [1] send out the PIO words */
832 pio[0] = BF_GPMI_CTRL0_COMMAND_MODE(BV_GPMI_CTRL0_COMMAND_MODE__WRITE)
833 | BM_GPMI_CTRL0_WORD_LENGTH
834 | BF_GPMI_CTRL0_CS(chip, this)
835 | BF_GPMI_CTRL0_LOCK_CS(LOCK_CS_ENABLE, this)
836 | BF_GPMI_CTRL0_ADDRESS(BV_GPMI_CTRL0_ADDRESS__NAND_CLE)
837 | BM_GPMI_CTRL0_ADDRESS_INCREMENT
838 | BF_GPMI_CTRL0_XFER_COUNT(this->command_length);
839 pio[1] = pio[2] = 0;
840 desc = dmaengine_prep_slave_sg(channel,
841 (struct scatterlist *)pio,
842 ARRAY_SIZE(pio), DMA_TRANS_NONE, 0);
843 if (!desc) {
844 pr_err("step 1 error\n");
845 return -1;
848 /* [2] send out the COMMAND + ADDRESS string stored in @buffer */
849 sgl = &this->cmd_sgl;
851 sg_init_one(sgl, this->cmd_buffer, this->command_length);
852 dma_map_sg(this->dev, sgl, 1, DMA_TO_DEVICE);
853 desc = dmaengine_prep_slave_sg(channel,
854 sgl, 1, DMA_MEM_TO_DEV,
855 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
857 if (!desc) {
858 pr_err("step 2 error\n");
859 return -1;
862 /* [3] submit the DMA */
863 set_dma_type(this, DMA_FOR_COMMAND);
864 return start_dma_without_bch_irq(this, desc);
867 int gpmi_send_data(struct gpmi_nand_data *this)
869 struct dma_async_tx_descriptor *desc;
870 struct dma_chan *channel = get_dma_chan(this);
871 int chip = this->current_chip;
872 uint32_t command_mode;
873 uint32_t address;
874 u32 pio[2];
876 /* [1] PIO */
877 command_mode = BV_GPMI_CTRL0_COMMAND_MODE__WRITE;
878 address = BV_GPMI_CTRL0_ADDRESS__NAND_DATA;
880 pio[0] = BF_GPMI_CTRL0_COMMAND_MODE(command_mode)
881 | BM_GPMI_CTRL0_WORD_LENGTH
882 | BF_GPMI_CTRL0_CS(chip, this)
883 | BF_GPMI_CTRL0_LOCK_CS(LOCK_CS_ENABLE, this)
884 | BF_GPMI_CTRL0_ADDRESS(address)
885 | BF_GPMI_CTRL0_XFER_COUNT(this->upper_len);
886 pio[1] = 0;
887 desc = dmaengine_prep_slave_sg(channel, (struct scatterlist *)pio,
888 ARRAY_SIZE(pio), DMA_TRANS_NONE, 0);
889 if (!desc) {
890 pr_err("step 1 error\n");
891 return -1;
894 /* [2] send DMA request */
895 prepare_data_dma(this, DMA_TO_DEVICE);
896 desc = dmaengine_prep_slave_sg(channel, &this->data_sgl,
897 1, DMA_MEM_TO_DEV,
898 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
899 if (!desc) {
900 pr_err("step 2 error\n");
901 return -1;
903 /* [3] submit the DMA */
904 set_dma_type(this, DMA_FOR_WRITE_DATA);
905 return start_dma_without_bch_irq(this, desc);
908 int gpmi_read_data(struct gpmi_nand_data *this)
910 struct dma_async_tx_descriptor *desc;
911 struct dma_chan *channel = get_dma_chan(this);
912 int chip = this->current_chip;
913 u32 pio[2];
915 /* [1] : send PIO */
916 pio[0] = BF_GPMI_CTRL0_COMMAND_MODE(BV_GPMI_CTRL0_COMMAND_MODE__READ)
917 | BM_GPMI_CTRL0_WORD_LENGTH
918 | BF_GPMI_CTRL0_CS(chip, this)
919 | BF_GPMI_CTRL0_LOCK_CS(LOCK_CS_ENABLE, this)
920 | BF_GPMI_CTRL0_ADDRESS(BV_GPMI_CTRL0_ADDRESS__NAND_DATA)
921 | BF_GPMI_CTRL0_XFER_COUNT(this->upper_len);
922 pio[1] = 0;
923 desc = dmaengine_prep_slave_sg(channel,
924 (struct scatterlist *)pio,
925 ARRAY_SIZE(pio), DMA_TRANS_NONE, 0);
926 if (!desc) {
927 pr_err("step 1 error\n");
928 return -1;
931 /* [2] : send DMA request */
932 prepare_data_dma(this, DMA_FROM_DEVICE);
933 desc = dmaengine_prep_slave_sg(channel, &this->data_sgl,
934 1, DMA_DEV_TO_MEM,
935 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
936 if (!desc) {
937 pr_err("step 2 error\n");
938 return -1;
941 /* [3] : submit the DMA */
942 set_dma_type(this, DMA_FOR_READ_DATA);
943 return start_dma_without_bch_irq(this, desc);
946 int gpmi_send_page(struct gpmi_nand_data *this,
947 dma_addr_t payload, dma_addr_t auxiliary)
949 struct bch_geometry *geo = &this->bch_geometry;
950 uint32_t command_mode;
951 uint32_t address;
952 uint32_t ecc_command;
953 uint32_t buffer_mask;
954 struct dma_async_tx_descriptor *desc;
955 struct dma_chan *channel = get_dma_chan(this);
956 int chip = this->current_chip;
957 u32 pio[6];
959 /* A DMA descriptor that does an ECC page read. */
960 command_mode = BV_GPMI_CTRL0_COMMAND_MODE__WRITE;
961 address = BV_GPMI_CTRL0_ADDRESS__NAND_DATA;
962 ecc_command = BV_GPMI_ECCCTRL_ECC_CMD__BCH_ENCODE;
963 buffer_mask = BV_GPMI_ECCCTRL_BUFFER_MASK__BCH_PAGE |
964 BV_GPMI_ECCCTRL_BUFFER_MASK__BCH_AUXONLY;
966 pio[0] = BF_GPMI_CTRL0_COMMAND_MODE(command_mode)
967 | BM_GPMI_CTRL0_WORD_LENGTH
968 | BF_GPMI_CTRL0_CS(chip, this)
969 | BF_GPMI_CTRL0_LOCK_CS(LOCK_CS_ENABLE, this)
970 | BF_GPMI_CTRL0_ADDRESS(address)
971 | BF_GPMI_CTRL0_XFER_COUNT(0);
972 pio[1] = 0;
973 pio[2] = BM_GPMI_ECCCTRL_ENABLE_ECC
974 | BF_GPMI_ECCCTRL_ECC_CMD(ecc_command)
975 | BF_GPMI_ECCCTRL_BUFFER_MASK(buffer_mask);
976 pio[3] = geo->page_size;
977 pio[4] = payload;
978 pio[5] = auxiliary;
980 desc = dmaengine_prep_slave_sg(channel,
981 (struct scatterlist *)pio,
982 ARRAY_SIZE(pio), DMA_TRANS_NONE,
983 DMA_CTRL_ACK);
984 if (!desc) {
985 pr_err("step 2 error\n");
986 return -1;
988 set_dma_type(this, DMA_FOR_WRITE_ECC_PAGE);
989 return start_dma_with_bch_irq(this, desc);
992 int gpmi_read_page(struct gpmi_nand_data *this,
993 dma_addr_t payload, dma_addr_t auxiliary)
995 struct bch_geometry *geo = &this->bch_geometry;
996 uint32_t command_mode;
997 uint32_t address;
998 uint32_t ecc_command;
999 uint32_t buffer_mask;
1000 struct dma_async_tx_descriptor *desc;
1001 struct dma_chan *channel = get_dma_chan(this);
1002 int chip = this->current_chip;
1003 u32 pio[6];
1005 /* [1] Wait for the chip to report ready. */
1006 command_mode = BV_GPMI_CTRL0_COMMAND_MODE__WAIT_FOR_READY;
1007 address = BV_GPMI_CTRL0_ADDRESS__NAND_DATA;
1009 pio[0] = BF_GPMI_CTRL0_COMMAND_MODE(command_mode)
1010 | BM_GPMI_CTRL0_WORD_LENGTH
1011 | BF_GPMI_CTRL0_CS(chip, this)
1012 | BF_GPMI_CTRL0_LOCK_CS(LOCK_CS_ENABLE, this)
1013 | BF_GPMI_CTRL0_ADDRESS(address)
1014 | BF_GPMI_CTRL0_XFER_COUNT(0);
1015 pio[1] = 0;
1016 desc = dmaengine_prep_slave_sg(channel,
1017 (struct scatterlist *)pio, 2,
1018 DMA_TRANS_NONE, 0);
1019 if (!desc) {
1020 pr_err("step 1 error\n");
1021 return -1;
1024 /* [2] Enable the BCH block and read. */
1025 command_mode = BV_GPMI_CTRL0_COMMAND_MODE__READ;
1026 address = BV_GPMI_CTRL0_ADDRESS__NAND_DATA;
1027 ecc_command = BV_GPMI_ECCCTRL_ECC_CMD__BCH_DECODE;
1028 buffer_mask = BV_GPMI_ECCCTRL_BUFFER_MASK__BCH_PAGE
1029 | BV_GPMI_ECCCTRL_BUFFER_MASK__BCH_AUXONLY;
1031 pio[0] = BF_GPMI_CTRL0_COMMAND_MODE(command_mode)
1032 | BM_GPMI_CTRL0_WORD_LENGTH
1033 | BF_GPMI_CTRL0_CS(chip, this)
1034 | BF_GPMI_CTRL0_LOCK_CS(LOCK_CS_ENABLE, this)
1035 | BF_GPMI_CTRL0_ADDRESS(address)
1036 | BF_GPMI_CTRL0_XFER_COUNT(geo->page_size);
1038 pio[1] = 0;
1039 pio[2] = BM_GPMI_ECCCTRL_ENABLE_ECC
1040 | BF_GPMI_ECCCTRL_ECC_CMD(ecc_command)
1041 | BF_GPMI_ECCCTRL_BUFFER_MASK(buffer_mask);
1042 pio[3] = geo->page_size;
1043 pio[4] = payload;
1044 pio[5] = auxiliary;
1045 desc = dmaengine_prep_slave_sg(channel,
1046 (struct scatterlist *)pio,
1047 ARRAY_SIZE(pio), DMA_TRANS_NONE,
1048 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
1049 if (!desc) {
1050 pr_err("step 2 error\n");
1051 return -1;
1054 /* [3] Disable the BCH block */
1055 command_mode = BV_GPMI_CTRL0_COMMAND_MODE__WAIT_FOR_READY;
1056 address = BV_GPMI_CTRL0_ADDRESS__NAND_DATA;
1058 pio[0] = BF_GPMI_CTRL0_COMMAND_MODE(command_mode)
1059 | BM_GPMI_CTRL0_WORD_LENGTH
1060 | BF_GPMI_CTRL0_CS(chip, this)
1061 | BF_GPMI_CTRL0_LOCK_CS(LOCK_CS_ENABLE, this)
1062 | BF_GPMI_CTRL0_ADDRESS(address)
1063 | BF_GPMI_CTRL0_XFER_COUNT(geo->page_size);
1064 pio[1] = 0;
1065 pio[2] = 0; /* clear GPMI_HW_GPMI_ECCCTRL, disable the BCH. */
1066 desc = dmaengine_prep_slave_sg(channel,
1067 (struct scatterlist *)pio, 3,
1068 DMA_TRANS_NONE,
1069 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
1070 if (!desc) {
1071 pr_err("step 3 error\n");
1072 return -1;
1075 /* [4] submit the DMA */
1076 set_dma_type(this, DMA_FOR_READ_ECC_PAGE);
1077 return start_dma_with_bch_irq(this, desc);