blob: fa5200b8c895e0ca722d2a707fd3af8f2e15329c [file] [log] [blame]
Huang Shijie45dfc1a2011-09-08 10:47:10 +08001/*
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>
Huang Shijie45dfc1a2011-09-08 10:47:10 +080024
25#include "gpmi-nand.h"
26#include "gpmi-regs.h"
27#include "bch-regs.h"
28
29struct 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,
37};
38
Huang Shijie4aa6ae32012-03-31 22:36:57 -040039#define MXS_SET_ADDR 0x4
40#define MXS_CLR_ADDR 0x8
Huang Shijie45dfc1a2011-09-08 10:47:10 +080041/*
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 */
Huang Shijie4aa6ae32012-03-31 22:36:57 -040051 writel(mask, addr + MXS_CLR_ADDR);
Huang Shijie45dfc1a2011-09-08 10:47:10 +080052
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 *
Huang Shijie9398d1c2012-01-04 11:18:46 +080073 * In most of the cases, it's ok.
74 * But in MX23, there is a hardware bug in the BCH block (see erratum #2847).
Huang Shijie45dfc1a2011-09-08 10:47:10 +080075 * 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.
Huang Shijie9398d1c2012-01-04 11:18:46 +080079 * You will see a DMA timeout in this case. The bug has been fixed
80 * in the following chips, such as MX28.
Huang Shijie45dfc1a2011-09-08 10:47:10 +080081 *
82 * To avoid this bug, just add a new parameter `just_enable` for
83 * the mxs_reset_block(), and rewrite it here.
84 */
Huang Shijie9398d1c2012-01-04 11:18:46 +080085static int gpmi_reset_block(void __iomem *reset_addr, bool just_enable)
Huang Shijie45dfc1a2011-09-08 10:47:10 +080086{
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 */
Huang Shijie4aa6ae32012-03-31 22:36:57 -040096 writel(MODULE_CLKGATE, reset_addr + MXS_CLR_ADDR);
Huang Shijie45dfc1a2011-09-08 10:47:10 +080097
98 if (!just_enable) {
99 /* set SFTRST to reset the block */
Huang Shijie4aa6ae32012-03-31 22:36:57 -0400100 writel(MODULE_SFTRST, reset_addr + MXS_SET_ADDR);
Huang Shijie45dfc1a2011-09-08 10:47:10 +0800101 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
127int gpmi_init(struct gpmi_nand_data *this)
128{
129 struct resources *r = &this->resources;
130 int ret;
131
Shawn Guof1f802c2011-12-20 14:02:05 +0800132 ret = clk_prepare_enable(r->clock);
Huang Shijie45dfc1a2011-09-08 10:47:10 +0800133 if (ret)
134 goto err_out;
135 ret = gpmi_reset_block(r->gpmi_regs, false);
136 if (ret)
137 goto err_out;
138
139 /* Choose NAND mode. */
140 writel(BM_GPMI_CTRL1_GPMI_MODE, r->gpmi_regs + HW_GPMI_CTRL1_CLR);
141
142 /* Set the IRQ polarity. */
143 writel(BM_GPMI_CTRL1_ATA_IRQRDY_POLARITY,
144 r->gpmi_regs + HW_GPMI_CTRL1_SET);
145
146 /* Disable Write-Protection. */
147 writel(BM_GPMI_CTRL1_DEV_RESET, r->gpmi_regs + HW_GPMI_CTRL1_SET);
148
149 /* Select BCH ECC. */
150 writel(BM_GPMI_CTRL1_BCH_MODE, r->gpmi_regs + HW_GPMI_CTRL1_SET);
151
Shawn Guof1f802c2011-12-20 14:02:05 +0800152 clk_disable_unprepare(r->clock);
Huang Shijie45dfc1a2011-09-08 10:47:10 +0800153 return 0;
154err_out:
155 return ret;
156}
157
158/* This function is very useful. It is called only when the bug occur. */
159void gpmi_dump_info(struct gpmi_nand_data *this)
160{
161 struct resources *r = &this->resources;
162 struct bch_geometry *geo = &this->bch_geometry;
163 u32 reg;
164 int i;
165
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);
170 }
171
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);
185}
186
187/* Configures the geometry for BCH. */
188int bch_set_geometry(struct gpmi_nand_data *this)
189{
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;
198
199 if (common_nfc_set_geometry(this))
200 return !0;
201
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;
207
Shawn Guof1f802c2011-12-20 14:02:05 +0800208 ret = clk_prepare_enable(r->clock);
Huang Shijie45dfc1a2011-09-08 10:47:10 +0800209 if (ret)
210 goto err_out;
211
Huang Shijie9398d1c2012-01-04 11:18:46 +0800212 /*
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.
219 */
220 ret = gpmi_reset_block(r->bch_regs, GPMI_IS_MX23(this));
Huang Shijie45dfc1a2011-09-08 10:47:10 +0800221 if (ret)
222 goto err_out;
223
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);
230
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);
235
236 /* Set *all* chip selects to use layout 0. */
237 writel(0, r->bch_regs + HW_BCH_LAYOUTSELECT);
238
239 /* Enable interrupts. */
240 writel(BM_BCH_CTRL_COMPLETE_IRQ_EN,
241 r->bch_regs + HW_BCH_CTRL_SET);
242
Shawn Guof1f802c2011-12-20 14:02:05 +0800243 clk_disable_unprepare(r->clock);
Huang Shijie45dfc1a2011-09-08 10:47:10 +0800244 return 0;
245err_out:
246 return ret;
247}
248
249/* Converts time in nanoseconds to cycles. */
250static unsigned int ns_to_cycles(unsigned int time,
251 unsigned int period, unsigned int min)
252{
253 unsigned int k;
254
255 k = (time + period - 1) / period;
256 return max(k, min);
257}
258
259/* Apply timing to current hardware conditions. */
260static int gpmi_nfc_compute_hardware_timing(struct gpmi_nand_data *this,
261 struct gpmi_nfc_hardware_timing *hw)
262{
263 struct gpmi_nand_platform_data *pdata = this->pdata;
264 struct timing_threshod *nfc = &timing_default_threshold;
265 struct nand_chip *nand = &this->nand;
266 struct nand_timing target = this->timing;
267 bool improved_timing_is_available;
268 unsigned long clock_frequency_in_hz;
269 unsigned int clock_period_in_ns;
270 bool dll_use_half_periods;
271 unsigned int dll_delay_shift;
272 unsigned int max_sample_delay_in_ns;
273 unsigned int address_setup_in_cycles;
274 unsigned int data_setup_in_ns;
275 unsigned int data_setup_in_cycles;
276 unsigned int data_hold_in_cycles;
277 int ideal_sample_delay_in_ns;
278 unsigned int sample_delay_factor;
279 int tEYE;
280 unsigned int min_prop_delay_in_ns = pdata->min_prop_delay_in_ns;
281 unsigned int max_prop_delay_in_ns = pdata->max_prop_delay_in_ns;
282
283 /*
284 * If there are multiple chips, we need to relax the timings to allow
285 * for signal distortion due to higher capacitance.
286 */
287 if (nand->numchips > 2) {
288 target.data_setup_in_ns += 10;
289 target.data_hold_in_ns += 10;
290 target.address_setup_in_ns += 10;
291 } else if (nand->numchips > 1) {
292 target.data_setup_in_ns += 5;
293 target.data_hold_in_ns += 5;
294 target.address_setup_in_ns += 5;
295 }
296
297 /* Check if improved timing information is available. */
298 improved_timing_is_available =
299 (target.tREA_in_ns >= 0) &&
300 (target.tRLOH_in_ns >= 0) &&
301 (target.tRHOH_in_ns >= 0) ;
302
303 /* Inspect the clock. */
304 clock_frequency_in_hz = nfc->clock_frequency_in_hz;
305 clock_period_in_ns = 1000000000 / clock_frequency_in_hz;
306
307 /*
308 * The NFC quantizes setup and hold parameters in terms of clock cycles.
309 * Here, we quantize the setup and hold timing parameters to the
310 * next-highest clock period to make sure we apply at least the
311 * specified times.
312 *
313 * For data setup and data hold, the hardware interprets a value of zero
314 * as the largest possible delay. This is not what's intended by a zero
315 * in the input parameter, so we impose a minimum of one cycle.
316 */
317 data_setup_in_cycles = ns_to_cycles(target.data_setup_in_ns,
318 clock_period_in_ns, 1);
319 data_hold_in_cycles = ns_to_cycles(target.data_hold_in_ns,
320 clock_period_in_ns, 1);
321 address_setup_in_cycles = ns_to_cycles(target.address_setup_in_ns,
322 clock_period_in_ns, 0);
323
324 /*
325 * The clock's period affects the sample delay in a number of ways:
326 *
327 * (1) The NFC HAL tells us the maximum clock period the sample delay
328 * DLL can tolerate. If the clock period is greater than half that
329 * maximum, we must configure the DLL to be driven by half periods.
330 *
331 * (2) We need to convert from an ideal sample delay, in ns, to a
332 * "sample delay factor," which the NFC uses. This factor depends on
333 * whether we're driving the DLL with full or half periods.
334 * Paraphrasing the reference manual:
335 *
336 * AD = SDF x 0.125 x RP
337 *
338 * where:
339 *
340 * AD is the applied delay, in ns.
341 * SDF is the sample delay factor, which is dimensionless.
342 * RP is the reference period, in ns, which is a full clock period
343 * if the DLL is being driven by full periods, or half that if
344 * the DLL is being driven by half periods.
345 *
346 * Let's re-arrange this in a way that's more useful to us:
347 *
348 * 8
349 * SDF = AD x ----
350 * RP
351 *
352 * The reference period is either the clock period or half that, so this
353 * is:
354 *
355 * 8 AD x DDF
356 * SDF = AD x ----- = --------
357 * f x P P
358 *
359 * where:
360 *
361 * f is 1 or 1/2, depending on how we're driving the DLL.
362 * P is the clock period.
363 * DDF is the DLL Delay Factor, a dimensionless value that
364 * incorporates all the constants in the conversion.
365 *
366 * DDF will be either 8 or 16, both of which are powers of two. We can
367 * reduce the cost of this conversion by using bit shifts instead of
368 * multiplication or division. Thus:
369 *
370 * AD << DDS
371 * SDF = ---------
372 * P
373 *
374 * or
375 *
376 * AD = (SDF >> DDS) x P
377 *
378 * where:
379 *
380 * DDS is the DLL Delay Shift, the logarithm to base 2 of the DDF.
381 */
382 if (clock_period_in_ns > (nfc->max_dll_clock_period_in_ns >> 1)) {
383 dll_use_half_periods = true;
384 dll_delay_shift = 3 + 1;
385 } else {
386 dll_use_half_periods = false;
387 dll_delay_shift = 3;
388 }
389
390 /*
391 * Compute the maximum sample delay the NFC allows, under current
392 * conditions. If the clock is running too slowly, no sample delay is
393 * possible.
394 */
395 if (clock_period_in_ns > nfc->max_dll_clock_period_in_ns)
396 max_sample_delay_in_ns = 0;
397 else {
398 /*
399 * Compute the delay implied by the largest sample delay factor
400 * the NFC allows.
401 */
402 max_sample_delay_in_ns =
403 (nfc->max_sample_delay_factor * clock_period_in_ns) >>
404 dll_delay_shift;
405
406 /*
407 * Check if the implied sample delay larger than the NFC
408 * actually allows.
409 */
410 if (max_sample_delay_in_ns > nfc->max_dll_delay_in_ns)
411 max_sample_delay_in_ns = nfc->max_dll_delay_in_ns;
412 }
413
414 /*
415 * Check if improved timing information is available. If not, we have to
416 * use a less-sophisticated algorithm.
417 */
418 if (!improved_timing_is_available) {
419 /*
420 * Fold the read setup time required by the NFC into the ideal
421 * sample delay.
422 */
423 ideal_sample_delay_in_ns = target.gpmi_sample_delay_in_ns +
424 nfc->internal_data_setup_in_ns;
425
426 /*
427 * The ideal sample delay may be greater than the maximum
428 * allowed by the NFC. If so, we can trade off sample delay time
429 * for more data setup time.
430 *
431 * In each iteration of the following loop, we add a cycle to
432 * the data setup time and subtract a corresponding amount from
433 * the sample delay until we've satisified the constraints or
434 * can't do any better.
435 */
436 while ((ideal_sample_delay_in_ns > max_sample_delay_in_ns) &&
437 (data_setup_in_cycles < nfc->max_data_setup_cycles)) {
438
439 data_setup_in_cycles++;
440 ideal_sample_delay_in_ns -= clock_period_in_ns;
441
442 if (ideal_sample_delay_in_ns < 0)
443 ideal_sample_delay_in_ns = 0;
444
445 }
446
447 /*
448 * Compute the sample delay factor that corresponds most closely
449 * to the ideal sample delay. If the result is too large for the
450 * NFC, use the maximum value.
451 *
452 * Notice that we use the ns_to_cycles function to compute the
453 * sample delay factor. We do this because the form of the
454 * computation is the same as that for calculating cycles.
455 */
456 sample_delay_factor =
457 ns_to_cycles(
458 ideal_sample_delay_in_ns << dll_delay_shift,
459 clock_period_in_ns, 0);
460
461 if (sample_delay_factor > nfc->max_sample_delay_factor)
462 sample_delay_factor = nfc->max_sample_delay_factor;
463
464 /* Skip to the part where we return our results. */
465 goto return_results;
466 }
467
468 /*
469 * If control arrives here, we have more detailed timing information,
470 * so we can use a better algorithm.
471 */
472
473 /*
474 * Fold the read setup time required by the NFC into the maximum
475 * propagation delay.
476 */
477 max_prop_delay_in_ns += nfc->internal_data_setup_in_ns;
478
479 /*
480 * Earlier, we computed the number of clock cycles required to satisfy
481 * the data setup time. Now, we need to know the actual nanoseconds.
482 */
483 data_setup_in_ns = clock_period_in_ns * data_setup_in_cycles;
484
485 /*
486 * Compute tEYE, the width of the data eye when reading from the NAND
487 * Flash. The eye width is fundamentally determined by the data setup
488 * time, perturbed by propagation delays and some characteristics of the
489 * NAND Flash device.
490 *
491 * start of the eye = max_prop_delay + tREA
492 * end of the eye = min_prop_delay + tRHOH + data_setup
493 */
494 tEYE = (int)min_prop_delay_in_ns + (int)target.tRHOH_in_ns +
495 (int)data_setup_in_ns;
496
497 tEYE -= (int)max_prop_delay_in_ns + (int)target.tREA_in_ns;
498
499 /*
500 * The eye must be open. If it's not, we can try to open it by
501 * increasing its main forcer, the data setup time.
502 *
503 * In each iteration of the following loop, we increase the data setup
504 * time by a single clock cycle. We do this until either the eye is
505 * open or we run into NFC limits.
506 */
507 while ((tEYE <= 0) &&
508 (data_setup_in_cycles < nfc->max_data_setup_cycles)) {
509 /* Give a cycle to data setup. */
510 data_setup_in_cycles++;
511 /* Synchronize the data setup time with the cycles. */
512 data_setup_in_ns += clock_period_in_ns;
513 /* Adjust tEYE accordingly. */
514 tEYE += clock_period_in_ns;
515 }
516
517 /*
518 * When control arrives here, the eye is open. The ideal time to sample
519 * the data is in the center of the eye:
520 *
521 * end of the eye + start of the eye
522 * --------------------------------- - data_setup
523 * 2
524 *
525 * After some algebra, this simplifies to the code immediately below.
526 */
527 ideal_sample_delay_in_ns =
528 ((int)max_prop_delay_in_ns +
529 (int)target.tREA_in_ns +
530 (int)min_prop_delay_in_ns +
531 (int)target.tRHOH_in_ns -
532 (int)data_setup_in_ns) >> 1;
533
534 /*
535 * The following figure illustrates some aspects of a NAND Flash read:
536 *
537 *
538 * __ _____________________________________
539 * RDN \_________________/
540 *
541 * <---- tEYE ----->
542 * /-----------------\
543 * Read Data ----------------------------< >---------
544 * \-----------------/
545 * ^ ^ ^ ^
546 * | | | |
547 * |<--Data Setup -->|<--Delay Time -->| |
548 * | | | |
549 * | | |
550 * | |<-- Quantized Delay Time -->|
551 * | | |
552 *
553 *
554 * We have some issues we must now address:
555 *
556 * (1) The *ideal* sample delay time must not be negative. If it is, we
557 * jam it to zero.
558 *
559 * (2) The *ideal* sample delay time must not be greater than that
560 * allowed by the NFC. If it is, we can increase the data setup
561 * time, which will reduce the delay between the end of the data
562 * setup and the center of the eye. It will also make the eye
563 * larger, which might help with the next issue...
564 *
565 * (3) The *quantized* sample delay time must not fall either before the
566 * eye opens or after it closes (the latter is the problem
567 * illustrated in the above figure).
568 */
569
570 /* Jam a negative ideal sample delay to zero. */
571 if (ideal_sample_delay_in_ns < 0)
572 ideal_sample_delay_in_ns = 0;
573
574 /*
575 * Extend the data setup as needed to reduce the ideal sample delay
576 * below the maximum permitted by the NFC.
577 */
578 while ((ideal_sample_delay_in_ns > max_sample_delay_in_ns) &&
579 (data_setup_in_cycles < nfc->max_data_setup_cycles)) {
580
581 /* Give a cycle to data setup. */
582 data_setup_in_cycles++;
583 /* Synchronize the data setup time with the cycles. */
584 data_setup_in_ns += clock_period_in_ns;
585 /* Adjust tEYE accordingly. */
586 tEYE += clock_period_in_ns;
587
588 /*
589 * Decrease the ideal sample delay by one half cycle, to keep it
590 * in the middle of the eye.
591 */
592 ideal_sample_delay_in_ns -= (clock_period_in_ns >> 1);
593
594 /* Jam a negative ideal sample delay to zero. */
595 if (ideal_sample_delay_in_ns < 0)
596 ideal_sample_delay_in_ns = 0;
597 }
598
599 /*
600 * Compute the sample delay factor that corresponds to the ideal sample
601 * delay. If the result is too large, then use the maximum allowed
602 * value.
603 *
604 * Notice that we use the ns_to_cycles function to compute the sample
605 * delay factor. We do this because the form of the computation is the
606 * same as that for calculating cycles.
607 */
608 sample_delay_factor =
609 ns_to_cycles(ideal_sample_delay_in_ns << dll_delay_shift,
610 clock_period_in_ns, 0);
611
612 if (sample_delay_factor > nfc->max_sample_delay_factor)
613 sample_delay_factor = nfc->max_sample_delay_factor;
614
615 /*
616 * These macros conveniently encapsulate a computation we'll use to
617 * continuously evaluate whether or not the data sample delay is inside
618 * the eye.
619 */
620 #define IDEAL_DELAY ((int) ideal_sample_delay_in_ns)
621
622 #define QUANTIZED_DELAY \
623 ((int) ((sample_delay_factor * clock_period_in_ns) >> \
624 dll_delay_shift))
625
626 #define DELAY_ERROR (abs(QUANTIZED_DELAY - IDEAL_DELAY))
627
628 #define SAMPLE_IS_NOT_WITHIN_THE_EYE (DELAY_ERROR > (tEYE >> 1))
629
630 /*
631 * While the quantized sample time falls outside the eye, reduce the
632 * sample delay or extend the data setup to move the sampling point back
633 * toward the eye. Do not allow the number of data setup cycles to
634 * exceed the maximum allowed by the NFC.
635 */
636 while (SAMPLE_IS_NOT_WITHIN_THE_EYE &&
637 (data_setup_in_cycles < nfc->max_data_setup_cycles)) {
638 /*
639 * If control arrives here, the quantized sample delay falls
640 * outside the eye. Check if it's before the eye opens, or after
641 * the eye closes.
642 */
643 if (QUANTIZED_DELAY > IDEAL_DELAY) {
644 /*
645 * If control arrives here, the quantized sample delay
646 * falls after the eye closes. Decrease the quantized
647 * delay time and then go back to re-evaluate.
648 */
649 if (sample_delay_factor != 0)
650 sample_delay_factor--;
651 continue;
652 }
653
654 /*
655 * If control arrives here, the quantized sample delay falls
656 * before the eye opens. Shift the sample point by increasing
657 * data setup time. This will also make the eye larger.
658 */
659
660 /* Give a cycle to data setup. */
661 data_setup_in_cycles++;
662 /* Synchronize the data setup time with the cycles. */
663 data_setup_in_ns += clock_period_in_ns;
664 /* Adjust tEYE accordingly. */
665 tEYE += clock_period_in_ns;
666
667 /*
668 * Decrease the ideal sample delay by one half cycle, to keep it
669 * in the middle of the eye.
670 */
671 ideal_sample_delay_in_ns -= (clock_period_in_ns >> 1);
672
673 /* ...and one less period for the delay time. */
674 ideal_sample_delay_in_ns -= clock_period_in_ns;
675
676 /* Jam a negative ideal sample delay to zero. */
677 if (ideal_sample_delay_in_ns < 0)
678 ideal_sample_delay_in_ns = 0;
679
680 /*
681 * We have a new ideal sample delay, so re-compute the quantized
682 * delay.
683 */
684 sample_delay_factor =
685 ns_to_cycles(
686 ideal_sample_delay_in_ns << dll_delay_shift,
687 clock_period_in_ns, 0);
688
689 if (sample_delay_factor > nfc->max_sample_delay_factor)
690 sample_delay_factor = nfc->max_sample_delay_factor;
691 }
692
693 /* Control arrives here when we're ready to return our results. */
694return_results:
695 hw->data_setup_in_cycles = data_setup_in_cycles;
696 hw->data_hold_in_cycles = data_hold_in_cycles;
697 hw->address_setup_in_cycles = address_setup_in_cycles;
698 hw->use_half_periods = dll_use_half_periods;
699 hw->sample_delay_factor = sample_delay_factor;
700
701 /* Return success. */
702 return 0;
703}
704
705/* Begin the I/O */
706void gpmi_begin(struct gpmi_nand_data *this)
707{
708 struct resources *r = &this->resources;
709 struct timing_threshod *nfc = &timing_default_threshold;
710 unsigned char *gpmi_regs = r->gpmi_regs;
711 unsigned int clock_period_in_ns;
712 uint32_t reg;
713 unsigned int dll_wait_time_in_us;
714 struct gpmi_nfc_hardware_timing hw;
715 int ret;
716
717 /* Enable the clock. */
Shawn Guof1f802c2011-12-20 14:02:05 +0800718 ret = clk_prepare_enable(r->clock);
Huang Shijie45dfc1a2011-09-08 10:47:10 +0800719 if (ret) {
720 pr_err("We failed in enable the clk\n");
721 goto err_out;
722 }
723
724 /* set ready/busy timeout */
725 writel(0x500 << BP_GPMI_TIMING1_BUSY_TIMEOUT,
726 gpmi_regs + HW_GPMI_TIMING1);
727
728 /* Get the timing information we need. */
729 nfc->clock_frequency_in_hz = clk_get_rate(r->clock);
730 clock_period_in_ns = 1000000000 / nfc->clock_frequency_in_hz;
731
732 gpmi_nfc_compute_hardware_timing(this, &hw);
733
734 /* Set up all the simple timing parameters. */
735 reg = BF_GPMI_TIMING0_ADDRESS_SETUP(hw.address_setup_in_cycles) |
736 BF_GPMI_TIMING0_DATA_HOLD(hw.data_hold_in_cycles) |
737 BF_GPMI_TIMING0_DATA_SETUP(hw.data_setup_in_cycles) ;
738
739 writel(reg, gpmi_regs + HW_GPMI_TIMING0);
740
741 /*
742 * DLL_ENABLE must be set to 0 when setting RDN_DELAY or HALF_PERIOD.
743 */
744 writel(BM_GPMI_CTRL1_DLL_ENABLE, gpmi_regs + HW_GPMI_CTRL1_CLR);
745
746 /* Clear out the DLL control fields. */
747 writel(BM_GPMI_CTRL1_RDN_DELAY, gpmi_regs + HW_GPMI_CTRL1_CLR);
748 writel(BM_GPMI_CTRL1_HALF_PERIOD, gpmi_regs + HW_GPMI_CTRL1_CLR);
749
750 /* If no sample delay is called for, return immediately. */
751 if (!hw.sample_delay_factor)
752 return;
753
754 /* Configure the HALF_PERIOD flag. */
755 if (hw.use_half_periods)
756 writel(BM_GPMI_CTRL1_HALF_PERIOD,
757 gpmi_regs + HW_GPMI_CTRL1_SET);
758
759 /* Set the delay factor. */
760 writel(BF_GPMI_CTRL1_RDN_DELAY(hw.sample_delay_factor),
761 gpmi_regs + HW_GPMI_CTRL1_SET);
762
763 /* Enable the DLL. */
764 writel(BM_GPMI_CTRL1_DLL_ENABLE, gpmi_regs + HW_GPMI_CTRL1_SET);
765
766 /*
767 * After we enable the GPMI DLL, we have to wait 64 clock cycles before
768 * we can use the GPMI.
769 *
770 * Calculate the amount of time we need to wait, in microseconds.
771 */
772 dll_wait_time_in_us = (clock_period_in_ns * 64) / 1000;
773
774 if (!dll_wait_time_in_us)
775 dll_wait_time_in_us = 1;
776
777 /* Wait for the DLL to settle. */
778 udelay(dll_wait_time_in_us);
779
780err_out:
781 return;
782}
783
784void gpmi_end(struct gpmi_nand_data *this)
785{
786 struct resources *r = &this->resources;
Shawn Guof1f802c2011-12-20 14:02:05 +0800787 clk_disable_unprepare(r->clock);
Huang Shijie45dfc1a2011-09-08 10:47:10 +0800788}
789
790/* Clears a BCH interrupt. */
791void gpmi_clear_bch(struct gpmi_nand_data *this)
792{
793 struct resources *r = &this->resources;
794 writel(BM_BCH_CTRL_COMPLETE_IRQ, r->bch_regs + HW_BCH_CTRL_CLR);
795}
796
797/* Returns the Ready/Busy status of the given chip. */
798int gpmi_is_ready(struct gpmi_nand_data *this, unsigned chip)
799{
800 struct resources *r = &this->resources;
801 uint32_t mask = 0;
802 uint32_t reg = 0;
803
804 if (GPMI_IS_MX23(this)) {
805 mask = MX23_BM_GPMI_DEBUG_READY0 << chip;
806 reg = readl(r->gpmi_regs + HW_GPMI_DEBUG);
807 } else if (GPMI_IS_MX28(this)) {
808 mask = MX28_BF_GPMI_STAT_READY_BUSY(1 << chip);
809 reg = readl(r->gpmi_regs + HW_GPMI_STAT);
810 } else
811 pr_err("unknow arch.\n");
812 return reg & mask;
813}
814
815static inline void set_dma_type(struct gpmi_nand_data *this,
816 enum dma_ops_type type)
817{
818 this->last_dma_type = this->dma_type;
819 this->dma_type = type;
820}
821
822int gpmi_send_command(struct gpmi_nand_data *this)
823{
824 struct dma_chan *channel = get_dma_chan(this);
825 struct dma_async_tx_descriptor *desc;
826 struct scatterlist *sgl;
827 int chip = this->current_chip;
828 u32 pio[3];
829
830 /* [1] send out the PIO words */
831 pio[0] = BF_GPMI_CTRL0_COMMAND_MODE(BV_GPMI_CTRL0_COMMAND_MODE__WRITE)
832 | BM_GPMI_CTRL0_WORD_LENGTH
833 | BF_GPMI_CTRL0_CS(chip, this)
834 | BF_GPMI_CTRL0_LOCK_CS(LOCK_CS_ENABLE, this)
835 | BF_GPMI_CTRL0_ADDRESS(BV_GPMI_CTRL0_ADDRESS__NAND_CLE)
836 | BM_GPMI_CTRL0_ADDRESS_INCREMENT
837 | BF_GPMI_CTRL0_XFER_COUNT(this->command_length);
838 pio[1] = pio[2] = 0;
Alexandre Bounine16052822012-03-08 16:11:18 -0500839 desc = dmaengine_prep_slave_sg(channel,
Huang Shijie45dfc1a2011-09-08 10:47:10 +0800840 (struct scatterlist *)pio,
Shawn Guo0ef7e202011-12-13 23:48:06 +0800841 ARRAY_SIZE(pio), DMA_TRANS_NONE, 0);
Huang Shijie45dfc1a2011-09-08 10:47:10 +0800842 if (!desc) {
843 pr_err("step 1 error\n");
844 return -1;
845 }
846
847 /* [2] send out the COMMAND + ADDRESS string stored in @buffer */
848 sgl = &this->cmd_sgl;
849
850 sg_init_one(sgl, this->cmd_buffer, this->command_length);
851 dma_map_sg(this->dev, sgl, 1, DMA_TO_DEVICE);
Linus Torvalds623ff772012-03-30 17:31:56 -0700852 desc = dmaengine_prep_slave_sg(channel,
Huang Shijie921de862012-02-16 14:17:33 +0800853 sgl, 1, DMA_MEM_TO_DEV,
854 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
855
Huang Shijie45dfc1a2011-09-08 10:47:10 +0800856 if (!desc) {
857 pr_err("step 2 error\n");
858 return -1;
859 }
860
861 /* [3] submit the DMA */
862 set_dma_type(this, DMA_FOR_COMMAND);
863 return start_dma_without_bch_irq(this, desc);
864}
865
866int gpmi_send_data(struct gpmi_nand_data *this)
867{
868 struct dma_async_tx_descriptor *desc;
869 struct dma_chan *channel = get_dma_chan(this);
870 int chip = this->current_chip;
871 uint32_t command_mode;
872 uint32_t address;
873 u32 pio[2];
874
875 /* [1] PIO */
876 command_mode = BV_GPMI_CTRL0_COMMAND_MODE__WRITE;
877 address = BV_GPMI_CTRL0_ADDRESS__NAND_DATA;
878
879 pio[0] = BF_GPMI_CTRL0_COMMAND_MODE(command_mode)
880 | BM_GPMI_CTRL0_WORD_LENGTH
881 | BF_GPMI_CTRL0_CS(chip, this)
882 | BF_GPMI_CTRL0_LOCK_CS(LOCK_CS_ENABLE, this)
883 | BF_GPMI_CTRL0_ADDRESS(address)
884 | BF_GPMI_CTRL0_XFER_COUNT(this->upper_len);
885 pio[1] = 0;
Alexandre Bounine16052822012-03-08 16:11:18 -0500886 desc = dmaengine_prep_slave_sg(channel, (struct scatterlist *)pio,
Shawn Guo0ef7e202011-12-13 23:48:06 +0800887 ARRAY_SIZE(pio), DMA_TRANS_NONE, 0);
Huang Shijie45dfc1a2011-09-08 10:47:10 +0800888 if (!desc) {
889 pr_err("step 1 error\n");
890 return -1;
891 }
892
893 /* [2] send DMA request */
894 prepare_data_dma(this, DMA_TO_DEVICE);
Alexandre Bounine16052822012-03-08 16:11:18 -0500895 desc = dmaengine_prep_slave_sg(channel, &this->data_sgl,
Huang Shijie921de862012-02-16 14:17:33 +0800896 1, DMA_MEM_TO_DEV,
897 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
Huang Shijie45dfc1a2011-09-08 10:47:10 +0800898 if (!desc) {
899 pr_err("step 2 error\n");
900 return -1;
901 }
902 /* [3] submit the DMA */
903 set_dma_type(this, DMA_FOR_WRITE_DATA);
904 return start_dma_without_bch_irq(this, desc);
905}
906
907int gpmi_read_data(struct gpmi_nand_data *this)
908{
909 struct dma_async_tx_descriptor *desc;
910 struct dma_chan *channel = get_dma_chan(this);
911 int chip = this->current_chip;
912 u32 pio[2];
913
914 /* [1] : send PIO */
915 pio[0] = BF_GPMI_CTRL0_COMMAND_MODE(BV_GPMI_CTRL0_COMMAND_MODE__READ)
916 | BM_GPMI_CTRL0_WORD_LENGTH
917 | BF_GPMI_CTRL0_CS(chip, this)
918 | BF_GPMI_CTRL0_LOCK_CS(LOCK_CS_ENABLE, this)
919 | BF_GPMI_CTRL0_ADDRESS(BV_GPMI_CTRL0_ADDRESS__NAND_DATA)
920 | BF_GPMI_CTRL0_XFER_COUNT(this->upper_len);
921 pio[1] = 0;
Alexandre Bounine16052822012-03-08 16:11:18 -0500922 desc = dmaengine_prep_slave_sg(channel,
Huang Shijie45dfc1a2011-09-08 10:47:10 +0800923 (struct scatterlist *)pio,
Shawn Guo0ef7e202011-12-13 23:48:06 +0800924 ARRAY_SIZE(pio), DMA_TRANS_NONE, 0);
Huang Shijie45dfc1a2011-09-08 10:47:10 +0800925 if (!desc) {
926 pr_err("step 1 error\n");
927 return -1;
928 }
929
930 /* [2] : send DMA request */
931 prepare_data_dma(this, DMA_FROM_DEVICE);
Alexandre Bounine16052822012-03-08 16:11:18 -0500932 desc = dmaengine_prep_slave_sg(channel, &this->data_sgl,
Huang Shijie921de862012-02-16 14:17:33 +0800933 1, DMA_DEV_TO_MEM,
934 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
Huang Shijie45dfc1a2011-09-08 10:47:10 +0800935 if (!desc) {
936 pr_err("step 2 error\n");
937 return -1;
938 }
939
940 /* [3] : submit the DMA */
941 set_dma_type(this, DMA_FOR_READ_DATA);
942 return start_dma_without_bch_irq(this, desc);
943}
944
945int gpmi_send_page(struct gpmi_nand_data *this,
946 dma_addr_t payload, dma_addr_t auxiliary)
947{
948 struct bch_geometry *geo = &this->bch_geometry;
949 uint32_t command_mode;
950 uint32_t address;
951 uint32_t ecc_command;
952 uint32_t buffer_mask;
953 struct dma_async_tx_descriptor *desc;
954 struct dma_chan *channel = get_dma_chan(this);
955 int chip = this->current_chip;
956 u32 pio[6];
957
958 /* A DMA descriptor that does an ECC page read. */
959 command_mode = BV_GPMI_CTRL0_COMMAND_MODE__WRITE;
960 address = BV_GPMI_CTRL0_ADDRESS__NAND_DATA;
961 ecc_command = BV_GPMI_ECCCTRL_ECC_CMD__BCH_ENCODE;
962 buffer_mask = BV_GPMI_ECCCTRL_BUFFER_MASK__BCH_PAGE |
963 BV_GPMI_ECCCTRL_BUFFER_MASK__BCH_AUXONLY;
964
965 pio[0] = BF_GPMI_CTRL0_COMMAND_MODE(command_mode)
966 | BM_GPMI_CTRL0_WORD_LENGTH
967 | BF_GPMI_CTRL0_CS(chip, this)
968 | BF_GPMI_CTRL0_LOCK_CS(LOCK_CS_ENABLE, this)
969 | BF_GPMI_CTRL0_ADDRESS(address)
970 | BF_GPMI_CTRL0_XFER_COUNT(0);
971 pio[1] = 0;
972 pio[2] = BM_GPMI_ECCCTRL_ENABLE_ECC
973 | BF_GPMI_ECCCTRL_ECC_CMD(ecc_command)
974 | BF_GPMI_ECCCTRL_BUFFER_MASK(buffer_mask);
975 pio[3] = geo->page_size;
976 pio[4] = payload;
977 pio[5] = auxiliary;
978
Linus Torvalds623ff772012-03-30 17:31:56 -0700979 desc = dmaengine_prep_slave_sg(channel,
Huang Shijie45dfc1a2011-09-08 10:47:10 +0800980 (struct scatterlist *)pio,
Huang Shijie921de862012-02-16 14:17:33 +0800981 ARRAY_SIZE(pio), DMA_TRANS_NONE,
982 DMA_CTRL_ACK);
Huang Shijie45dfc1a2011-09-08 10:47:10 +0800983 if (!desc) {
984 pr_err("step 2 error\n");
985 return -1;
986 }
987 set_dma_type(this, DMA_FOR_WRITE_ECC_PAGE);
988 return start_dma_with_bch_irq(this, desc);
989}
990
991int gpmi_read_page(struct gpmi_nand_data *this,
992 dma_addr_t payload, dma_addr_t auxiliary)
993{
994 struct bch_geometry *geo = &this->bch_geometry;
995 uint32_t command_mode;
996 uint32_t address;
997 uint32_t ecc_command;
998 uint32_t buffer_mask;
999 struct dma_async_tx_descriptor *desc;
1000 struct dma_chan *channel = get_dma_chan(this);
1001 int chip = this->current_chip;
1002 u32 pio[6];
1003
1004 /* [1] Wait for the chip to report ready. */
1005 command_mode = BV_GPMI_CTRL0_COMMAND_MODE__WAIT_FOR_READY;
1006 address = BV_GPMI_CTRL0_ADDRESS__NAND_DATA;
1007
1008 pio[0] = BF_GPMI_CTRL0_COMMAND_MODE(command_mode)
1009 | BM_GPMI_CTRL0_WORD_LENGTH
1010 | BF_GPMI_CTRL0_CS(chip, this)
1011 | BF_GPMI_CTRL0_LOCK_CS(LOCK_CS_ENABLE, this)
1012 | BF_GPMI_CTRL0_ADDRESS(address)
1013 | BF_GPMI_CTRL0_XFER_COUNT(0);
1014 pio[1] = 0;
Alexandre Bounine16052822012-03-08 16:11:18 -05001015 desc = dmaengine_prep_slave_sg(channel,
Shawn Guo0ef7e202011-12-13 23:48:06 +08001016 (struct scatterlist *)pio, 2,
1017 DMA_TRANS_NONE, 0);
Huang Shijie45dfc1a2011-09-08 10:47:10 +08001018 if (!desc) {
1019 pr_err("step 1 error\n");
1020 return -1;
1021 }
1022
1023 /* [2] Enable the BCH block and read. */
1024 command_mode = BV_GPMI_CTRL0_COMMAND_MODE__READ;
1025 address = BV_GPMI_CTRL0_ADDRESS__NAND_DATA;
1026 ecc_command = BV_GPMI_ECCCTRL_ECC_CMD__BCH_DECODE;
1027 buffer_mask = BV_GPMI_ECCCTRL_BUFFER_MASK__BCH_PAGE
1028 | BV_GPMI_ECCCTRL_BUFFER_MASK__BCH_AUXONLY;
1029
1030 pio[0] = BF_GPMI_CTRL0_COMMAND_MODE(command_mode)
1031 | BM_GPMI_CTRL0_WORD_LENGTH
1032 | BF_GPMI_CTRL0_CS(chip, this)
1033 | BF_GPMI_CTRL0_LOCK_CS(LOCK_CS_ENABLE, this)
1034 | BF_GPMI_CTRL0_ADDRESS(address)
1035 | BF_GPMI_CTRL0_XFER_COUNT(geo->page_size);
1036
1037 pio[1] = 0;
1038 pio[2] = BM_GPMI_ECCCTRL_ENABLE_ECC
1039 | BF_GPMI_ECCCTRL_ECC_CMD(ecc_command)
1040 | BF_GPMI_ECCCTRL_BUFFER_MASK(buffer_mask);
1041 pio[3] = geo->page_size;
1042 pio[4] = payload;
1043 pio[5] = auxiliary;
Alexandre Bounine16052822012-03-08 16:11:18 -05001044 desc = dmaengine_prep_slave_sg(channel,
Huang Shijie45dfc1a2011-09-08 10:47:10 +08001045 (struct scatterlist *)pio,
Huang Shijie921de862012-02-16 14:17:33 +08001046 ARRAY_SIZE(pio), DMA_TRANS_NONE,
1047 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
Huang Shijie45dfc1a2011-09-08 10:47:10 +08001048 if (!desc) {
1049 pr_err("step 2 error\n");
1050 return -1;
1051 }
1052
1053 /* [3] Disable the BCH block */
1054 command_mode = BV_GPMI_CTRL0_COMMAND_MODE__WAIT_FOR_READY;
1055 address = BV_GPMI_CTRL0_ADDRESS__NAND_DATA;
1056
1057 pio[0] = BF_GPMI_CTRL0_COMMAND_MODE(command_mode)
1058 | BM_GPMI_CTRL0_WORD_LENGTH
1059 | BF_GPMI_CTRL0_CS(chip, this)
1060 | BF_GPMI_CTRL0_LOCK_CS(LOCK_CS_ENABLE, this)
1061 | BF_GPMI_CTRL0_ADDRESS(address)
1062 | BF_GPMI_CTRL0_XFER_COUNT(geo->page_size);
1063 pio[1] = 0;
Huang Shijie09ef90d2012-03-12 10:22:18 +08001064 pio[2] = 0; /* clear GPMI_HW_GPMI_ECCCTRL, disable the BCH. */
Alexandre Bounine16052822012-03-08 16:11:18 -05001065 desc = dmaengine_prep_slave_sg(channel,
Huang Shijie09ef90d2012-03-12 10:22:18 +08001066 (struct scatterlist *)pio, 3,
Huang Shijie921de862012-02-16 14:17:33 +08001067 DMA_TRANS_NONE,
1068 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
Huang Shijie45dfc1a2011-09-08 10:47:10 +08001069 if (!desc) {
1070 pr_err("step 3 error\n");
1071 return -1;
1072 }
1073
1074 /* [4] submit the DMA */
1075 set_dma_type(this, DMA_FOR_READ_ECC_PAGE);
1076 return start_dma_with_bch_irq(this, desc);
1077}