}
}
-static int find_working_phase(uint32_t *grp, uint32_t *bit_chk,
- uint32_t dtaps_per_ptap, uint32_t *work_bgn,
- uint32_t *v, uint32_t *d, uint32_t *p,
- uint32_t *i, uint32_t *max_working_cnt)
+/**
+ * sdr_find_phase() - Find DQS enable phase
+ * @working: If 1, look for working phase, if 0, look for non-working phase
+ * @grp: Read/Write group
+ * @v: VFIFO value
+ * @work: Working window position
+ * @i: Iterator
+ * @p: DQS Phase Iterator
+ *
+ * Find working or non-working DQS enable phase setting.
+ */
+static int sdr_find_phase(int working, const u32 grp, u32 *v, u32 *work,
+ u32 *i, u32 *p)
{
- uint32_t found_begin = 0;
- uint32_t tmp_delay = 0;
- uint32_t test_status;
+ u32 ret, bit_chk;
+ const u32 end = VFIFO_SIZE + (working ? 0 : 1);
- for (*d = 0; *d <= dtaps_per_ptap; (*d)++, tmp_delay +=
- IO_DELAY_PER_DQS_EN_DCHAIN_TAP) {
- *work_bgn = tmp_delay;
- scc_mgr_set_dqs_en_delay_all_ranks(*grp, *d);
-
- for (*i = 0; *i < VFIFO_SIZE; (*i)++) {
- for (*p = 0; *p <= IO_DQS_EN_PHASE_MAX; (*p)++, *work_bgn +=
- IO_DELAY_PER_OPA_TAP) {
- scc_mgr_set_dqs_en_phase_all_ranks(*grp, *p);
+ for (; *i < end; (*i)++) {
+ if (working)
+ *p = 0;
- test_status =
- rw_mgr_mem_calibrate_read_test_all_ranks
- (*grp, 1, PASS_ONE_BIT, bit_chk, 0);
+ for (; *p <= IO_DQS_EN_PHASE_MAX; (*p)++) {
+ scc_mgr_set_dqs_en_phase_all_ranks(grp, *p);
- if (test_status) {
- *max_working_cnt = 1;
- found_begin = 1;
- break;
- }
- }
+ ret = rw_mgr_mem_calibrate_read_test_all_ranks(grp, 1,
+ PASS_ONE_BIT, &bit_chk, 0);
+ if (!working)
+ ret = !ret;
- if (found_begin)
- break;
+ if (ret)
+ return 0;
- if (*p > IO_DQS_EN_PHASE_MAX)
- /* fiddle with FIFO */
- rw_mgr_incr_vfifo(*grp, v);
+ *work += IO_DELAY_PER_OPA_TAP;
}
- if (found_begin)
- break;
+ if (*p > IO_DQS_EN_PHASE_MAX) {
+ /* Fiddle with FIFO. */
+ rw_mgr_incr_vfifo(grp, v);
+ if (!working)
+ *p = 0;
+ }
}
- if (*i >= VFIFO_SIZE) {
- /* cannot find working solution */
- debug_cond(DLEVEL == 2, "%s:%d find_dqs_en_phase: no vfifo/\
- ptap/dtap\n", __func__, __LINE__);
- return 0;
- } else {
- return 1;
+ return -EINVAL;
+}
+
+static int sdr_working_phase(uint32_t grp, uint32_t *work_bgn,
+ uint32_t *v, uint32_t *d, uint32_t *p,
+ uint32_t *i)
+{
+ const u32 dtaps_per_ptap = IO_DELAY_PER_OPA_TAP /
+ IO_DELAY_PER_DQS_EN_DCHAIN_TAP;
+ int ret;
+
+ *work_bgn = 0;
+
+ for (*d = 0; *d <= dtaps_per_ptap; (*d)++) {
+ *i = 0;
+ scc_mgr_set_dqs_en_delay_all_ranks(grp, *d);
+ ret = sdr_find_phase(1, grp, v, work_bgn, i, p);
+ if (!ret)
+ return 0;
+ *work_bgn += IO_DELAY_PER_DQS_EN_DCHAIN_TAP;
}
+
+ /* Cannot find working solution */
+ debug_cond(DLEVEL == 2, "%s:%d find_dqs_en_phase: no vfifo/ptap/dtap\n",
+ __func__, __LINE__);
+ return -EINVAL;
}
-static void sdr_backup_phase(uint32_t *grp, uint32_t *bit_chk,
+static void sdr_backup_phase(uint32_t grp,
uint32_t *work_bgn, uint32_t *v, uint32_t *d,
- uint32_t *p, uint32_t *max_working_cnt)
+ uint32_t *p)
{
- uint32_t found_begin = 0;
uint32_t tmp_delay;
+ u32 bit_chk;
/* Special case code for backing up a phase */
if (*p == 0) {
*p = IO_DQS_EN_PHASE_MAX;
- rw_mgr_decr_vfifo(*grp, v);
+ rw_mgr_decr_vfifo(grp, v);
} else {
(*p)--;
}
tmp_delay = *work_bgn - IO_DELAY_PER_OPA_TAP;
- scc_mgr_set_dqs_en_phase_all_ranks(*grp, *p);
+ scc_mgr_set_dqs_en_phase_all_ranks(grp, *p);
for (*d = 0; *d <= IO_DQS_EN_DELAY_MAX && tmp_delay < *work_bgn;
(*d)++, tmp_delay += IO_DELAY_PER_DQS_EN_DCHAIN_TAP) {
- scc_mgr_set_dqs_en_delay_all_ranks(*grp, *d);
+ scc_mgr_set_dqs_en_delay_all_ranks(grp, *d);
- if (rw_mgr_mem_calibrate_read_test_all_ranks(*grp, 1,
+ if (rw_mgr_mem_calibrate_read_test_all_ranks(grp, 1,
PASS_ONE_BIT,
- bit_chk, 0)) {
- found_begin = 1;
+ &bit_chk, 0)) {
*work_bgn = tmp_delay;
break;
}
}
- /* We have found a working dtap before the ptap found above */
- if (found_begin == 1)
- (*max_working_cnt)++;
-
/*
* Restore VFIFO to old state before we decremented it
* (if needed).
(*p)++;
if (*p > IO_DQS_EN_PHASE_MAX) {
*p = 0;
- rw_mgr_incr_vfifo(*grp, v);
+ rw_mgr_incr_vfifo(grp, v);
}
- scc_mgr_set_dqs_en_delay_all_ranks(*grp, 0);
+ scc_mgr_set_dqs_en_delay_all_ranks(grp, 0);
}
-static int sdr_nonworking_phase(uint32_t *grp, uint32_t *bit_chk,
+static int sdr_nonworking_phase(uint32_t grp,
uint32_t *work_bgn, uint32_t *v, uint32_t *d,
- uint32_t *p, uint32_t *i, uint32_t *max_working_cnt,
+ uint32_t *p, uint32_t *i,
uint32_t *work_end)
{
- uint32_t found_end = 0;
+ int ret;
(*p)++;
*work_end += IO_DELAY_PER_OPA_TAP;
if (*p > IO_DQS_EN_PHASE_MAX) {
- /* fiddle with FIFO */
+ /* Fiddle with FIFO. */
*p = 0;
- rw_mgr_incr_vfifo(*grp, v);
+ rw_mgr_incr_vfifo(grp, v);
}
- for (; *i < VFIFO_SIZE + 1; (*i)++) {
- for (; *p <= IO_DQS_EN_PHASE_MAX; (*p)++, *work_end
- += IO_DELAY_PER_OPA_TAP) {
- scc_mgr_set_dqs_en_phase_all_ranks(*grp, *p);
-
- if (!rw_mgr_mem_calibrate_read_test_all_ranks
- (*grp, 1, PASS_ONE_BIT, bit_chk, 0)) {
- found_end = 1;
- break;
- } else {
- (*max_working_cnt)++;
- }
- }
-
- if (found_end)
- break;
-
- if (*p > IO_DQS_EN_PHASE_MAX) {
- /* fiddle with FIFO */
- rw_mgr_incr_vfifo(*grp, v);
- *p = 0;
- }
+ ret = sdr_find_phase(0, grp, v, work_end, i, p);
+ if (ret) {
+ /* Cannot see edge of failing read. */
+ debug_cond(DLEVEL == 2, "%s:%d: end: failed\n",
+ __func__, __LINE__);
}
- if (*i >= VFIFO_SIZE + 1) {
- /* cannot see edge of failing read */
- debug_cond(DLEVEL == 2, "%s:%d sdr_nonworking_phase: end:\
- failed\n", __func__, __LINE__);
- return 0;
- } else {
- return 1;
- }
+ return ret;
}
-static int sdr_find_window_centre(uint32_t *grp, uint32_t *bit_chk,
- uint32_t *work_bgn, uint32_t *v, uint32_t *d,
- uint32_t *p, uint32_t *work_mid,
- uint32_t *work_end)
+/**
+ * sdr_find_window_center() - Find center of the working DQS window.
+ * @grp: Read/Write group
+ * @work_bgn: First working settings
+ * @work_end: Last working settings
+ * @val: VFIFO value
+ *
+ * Find center of the working DQS enable window.
+ */
+static int sdr_find_window_center(const u32 grp, const u32 work_bgn,
+ const u32 work_end, const u32 val)
{
- int i;
+ u32 bit_chk, work_mid, v = val;
int tmp_delay = 0;
+ int i, p, d;
- *work_mid = (*work_bgn + *work_end) / 2;
+ work_mid = (work_bgn + work_end) / 2;
debug_cond(DLEVEL == 2, "work_bgn=%d work_end=%d work_mid=%d\n",
- *work_bgn, *work_end, *work_mid);
+ 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;
- debug_cond(DLEVEL == 2, "new work_mid %d\n", *work_mid);
+ 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);
- 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);
+ 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;
- scc_mgr_set_dqs_en_phase_all_ranks(*grp, (*p) - 1);
- scc_mgr_set_dqs_en_delay_all_ranks(*grp, *d);
+ debug_cond(DLEVEL == 2, "new d %d, tmp_delay=%d\n", d, tmp_delay);
+
+ scc_mgr_set_dqs_en_phase_all_ranks(grp, p);
+ scc_mgr_set_dqs_en_delay_all_ranks(grp, d);
/*
* push vfifo until we can successfully calibrate. We can do this
*/
for (i = 0; i < VFIFO_SIZE; i++) {
debug_cond(DLEVEL == 2, "find_dqs_en_phase: center: vfifo=%u\n",
- *v);
- if (rw_mgr_mem_calibrate_read_test_all_ranks(*grp, 1,
+ v);
+ if (rw_mgr_mem_calibrate_read_test_all_ranks(grp, 1,
PASS_ONE_BIT,
- bit_chk, 0)) {
- break;
+ &bit_chk, 0)) {
+ debug_cond(DLEVEL == 2,
+ "%s:%d center: found: vfifo=%u ptap=%u dtap=%u\n",
+ __func__, __LINE__, v, p, d);
+ return 0;
}
- /* fiddle with FIFO */
- rw_mgr_incr_vfifo(*grp, v);
+ /* Fiddle with FIFO. */
+ rw_mgr_incr_vfifo(grp, &v);
}
- if (i >= VFIFO_SIZE) {
- debug_cond(DLEVEL == 2, "%s:%d find_dqs_en_phase: center: \
- failed\n", __func__, __LINE__);
- return 0;
- } else {
- return 1;
- }
+ debug_cond(DLEVEL == 2, "%s:%d center: failed.\n",
+ __func__, __LINE__);
+ return -EINVAL;
}
/* find a good dqs enable to use */
static uint32_t rw_mgr_mem_calibrate_vfifo_find_dqs_en_phase(uint32_t grp)
{
uint32_t v, d, p, i;
- uint32_t max_working_cnt;
uint32_t bit_chk;
uint32_t dtaps_per_ptap;
- uint32_t work_bgn, work_mid, work_end;
+ uint32_t work_bgn, work_end;
uint32_t found_passing_read, found_failing_read, initial_failing_dtap;
debug("%s:%d %u\n", __func__, __LINE__, grp);
/* * Step 1 : First push vfifo until we get a failing read * */
v = find_vfifo_read(grp, &bit_chk);
- max_working_cnt = 0;
-
/* ******************************************************** */
/* * step 2: find first working phase, increment in ptaps * */
work_bgn = 0;
- if (find_working_phase(&grp, &bit_chk, dtaps_per_ptap, &work_bgn, &v, &d,
- &p, &i, &max_working_cnt) == 0)
+ if (sdr_working_phase(grp, &work_bgn, &v, &d, &p, &i))
return 0;
work_end = work_bgn;
/* * step 3a: if we have room, back off by one and
increment in dtaps * */
- sdr_backup_phase(&grp, &bit_chk, &work_bgn, &v, &d, &p,
- &max_working_cnt);
+ sdr_backup_phase(grp, &work_bgn, &v, &d, &p);
/* ********************************************************* */
/* * step 4a: go forward from working phase to non working
phase, increment in ptaps * */
- if (sdr_nonworking_phase(&grp, &bit_chk, &work_bgn, &v, &d, &p,
- &i, &max_working_cnt, &work_end) == 0)
+ if (sdr_nonworking_phase(grp, &work_bgn, &v, &d, &p,
+ &i, &work_end))
return 0;
/* ********************************************************* */
v, p, d, work_bgn);
work_end = work_bgn;
-
- /* * The actual increment of dtaps is done outside of the
- if/else loop to share code */
-
- /* Only here to counterbalance a subtract later on which is
- not needed if this branch of the algorithm is taken */
- max_working_cnt++;
}
/* The dtap increment to find the failing edge is done here */
/* ******************************************** */
/* * step 6: Find the centre of the window * */
- if (sdr_find_window_centre(&grp, &bit_chk, &work_bgn, &v, &d, &p,
- &work_mid, &work_end) == 0)
- return 0;
+ if (sdr_find_window_centre(grp, work_bgn, work_end, v))
+ return 0; /* FIXME: Old code, return 0 means failure :-( */
- debug_cond(DLEVEL == 2, "%s:%d find_dqs_en_phase: center found: \
- vfifo=%u ptap=%u dtap=%u\n", __func__, __LINE__,
- v, p-1, d);
return 1;
}