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