ddr: altera: Clean up sdr_find_window_centre() part 2
authorMarek Vasut <marex@denx.de>
Sun, 19 Jul 2015 02:04:33 +0000 (04:04 +0200)
committerMarek Vasut <marex@denx.de>
Sat, 8 Aug 2015 12:14:18 +0000 (14:14 +0200)
This function is a treasure trove of ad-hoc iterative
implementations of mathematical functions. Replace all
of those with their non-iterative counterpart.

Signed-off-by: Marek Vasut <marex@denx.de>
drivers/ddr/altera/sequencer.c

index 4c4b36ce502a2dd7a0cb8cc3bc8b2a97d70cc6c2..eb246cc89df270958290b6dfc6171d7502386a8f 100644 (file)
@@ -1492,28 +1492,27 @@ static int sdr_find_window_centre(const u32 grp, const u32 work_bgn,
        debug_cond(DLEVEL == 2, "work_bgn=%d work_end=%d work_mid=%d\n",
                   work_bgn, work_end, work_mid);
        /* Get the middle delay to be less than a VFIFO delay */
-       for (p = 0; p <= IO_DQS_EN_PHASE_MAX;
-            p++, tmp_delay += IO_DELAY_PER_OPA_TAP)
-               ;
+       tmp_delay = (IO_DQS_EN_PHASE_MAX + 1) * IO_DELAY_PER_OPA_TAP;
 
        debug_cond(DLEVEL == 2, "vfifo ptap delay %d\n", tmp_delay);
-       while (work_mid > tmp_delay)
-               work_mid -= tmp_delay;
+       work_mid %= tmp_delay;
        debug_cond(DLEVEL == 2, "new work_mid %d\n", work_mid);
 
-       tmp_delay = 0;
-       for (p = 0; p <= IO_DQS_EN_PHASE_MAX && tmp_delay < work_mid;
-               p++, tmp_delay += IO_DELAY_PER_OPA_TAP)
-               ;
-       tmp_delay -= IO_DELAY_PER_OPA_TAP;
-       debug_cond(DLEVEL == 2, "new p %d, tmp_delay=%d\n", p - 1, tmp_delay);
+       tmp_delay = rounddown(work_mid, IO_DELAY_PER_OPA_TAP);
+       if (tmp_delay > IO_DQS_EN_PHASE_MAX * IO_DELAY_PER_OPA_TAP)
+               tmp_delay = IO_DQS_EN_PHASE_MAX * IO_DELAY_PER_OPA_TAP;
+       p = tmp_delay / IO_DELAY_PER_OPA_TAP;
+
+       debug_cond(DLEVEL == 2, "new p %d, tmp_delay=%d\n", p, tmp_delay);
+
+       d = DIV_ROUND_UP(work_mid - tmp_delay, IO_DELAY_PER_DQS_EN_DCHAIN_TAP);
+       if (d > IO_DQS_EN_DELAY_MAX)
+               d = IO_DQS_EN_DELAY_MAX;
+       tmp_delay += d * IO_DELAY_PER_DQS_EN_DCHAIN_TAP;
 
-       for (d = 0; d <= IO_DQS_EN_DELAY_MAX && tmp_delay < work_mid;
-               d++, tmp_delay += IO_DELAY_PER_DQS_EN_DCHAIN_TAP)
-               ;
        debug_cond(DLEVEL == 2, "new d %d, tmp_delay=%d\n", d, tmp_delay);
 
-       scc_mgr_set_dqs_en_phase_all_ranks(grp, p - 1);
+       scc_mgr_set_dqs_en_phase_all_ranks(grp, p);
        scc_mgr_set_dqs_en_delay_all_ranks(grp, d);
 
        /*
@@ -1540,7 +1539,7 @@ static int sdr_find_window_centre(const u32 grp, const u32 work_bgn,
        } else {
                debug_cond(DLEVEL == 2,
                           "%s:%d center: found: vfifo=%u ptap=%u dtap=%u\n",
-                          __func__, __LINE__, v, p - 1, d);
+                          __func__, __LINE__, v, p, d);
                return 1;
        }
 }