2 * (C) Copyright 2003 Intracom S.A.
3 * Pantelis Antoniou <panto@intracom.gr>
5 * This little program makes an exhaustive search for the
6 * correct terms of pdf, mfi, mfn, mfd, s, dbrmo, in PLPRCR.
7 * The goal is to produce a gclk2 from a xin input, while respecting
8 * all the restrictions on their combination.
10 * Generaly you select the first row of the produced table.
12 * SPDX-License-Identifier: GPL-2.0+
19 #define DPREF_MIN 10000000
20 #define DPREF_MAX 32000000
22 #define DPGDCK_MAX 320000000
23 #define DPGDCK_MIN 160000000
43 #define GCLK2_MAX 150000000
45 static int calculate (int xin, int target_clock,
46 int ppm, int pdf, int mfi, int mfn, int mfd, int s,
47 int *dprefp, int *dpgdckp, int *jdbckp,
48 int *gclk2p, int *dbrmop)
50 unsigned int dpref, dpgdck, jdbck, gclk2, t1, t2, dbrmo;
56 /* valid num, denum? */
57 if (mfn > 0 && mfn >= mfd)
60 dpref = xin / (pdf + 1);
63 if (dpref < DPREF_MIN || dpref > DPREF_MAX)
67 dpgdck = (2 * mfi * xin) / (pdf + 1) ;
70 /* 5 <= mfi + (mfn / mfd + 1) <= 15 */
73 if ( MF_MIN * t1 > t2 || MF_MAX * t1 < t2)
76 dpgdck = (unsigned int)(2 * (mfi * mfd + mfi + mfn) *
78 ((mfd + 1) * (pdf + 1));
80 dbrmo = 10 * mfn < (mfd + 1);
84 if (dpgdck < DPGDCK_MIN || dpgdck > DPGDCK_MAX)
91 if (gclk2 > GCLK2_MAX)
94 t1 = abs(gclk2 - target_clock);
96 /* XXX max 1MHz dev. in clock */
100 /* dev within range (XXX gclk2 scaled to avoid overflow) */
101 if (t1 * 1000 > (unsigned int)ppm * (gclk2 / 1000))
113 int conf_clock(int xin, int target_clock, int ppm)
115 int pdf, s, mfn, mfd, mfi;
116 int dpref, dpgdck, jdbck, gclk2, xout, dbrmo;
119 /* integer multipliers */
120 for (pdf = PDF_MIN; pdf <= PDF_MAX; pdf++) {
121 for (mfi = MFI_MIN; mfi <= MFI_MAX; mfi++) {
122 for (s = 0; s <= S_MAX; s++) {
123 xout = calculate(xin, target_clock,
124 ppm, pdf, mfi, 0, 0, s,
125 &dpref, &dpgdck, &jdbck,
131 printf("pdf mfi mfn mfd s dbrmo dpref dpgdck jdbck gclk2 exact?\n");
132 printf("--- --- --- --- - ----- ----- ------ ----- ----- ------\n");
135 printf("%3d %3d --- --- %1d %5d %9d %9d %9d %9d%s\n",
137 dpref, dpgdck, jdbck, gclk2,
138 gclk2 == target_clock ? " YES" : "");
145 /* fractional multipliers */
146 for (pdf = PDF_MIN; pdf <= PDF_MAX; pdf++) {
147 for (mfi = MFI_MIN; mfi <= MFI_MAX; mfi++) {
148 for (mfn = 1; mfn <= MFN_MAX; mfn++) {
149 for (mfd = 1; mfd <= MFD_MAX; mfd++) {
150 for (s = 0; s <= S_MAX; s++) {
151 xout = calculate(xin, target_clock,
152 ppm, pdf, mfi, mfn, mfd, s,
153 &dpref, &dpgdck, &jdbck,
159 printf("pdf mfi mfn mfd s dbrmo dpref dpgdck jdbck gclk2 exact?\n");
160 printf("--- --- --- --- - ----- ----- ------ ----- ----- ------\n");
163 printf("%3d %3d %3d %3d %1d %5d %9d %9d %9d %9d%s\n",
164 pdf, mfi, mfn, mfd, s,
165 dbrmo, dpref, dpgdck, jdbck, gclk2,
166 gclk2 == target_clock ? " YES" : "");
179 int main(int argc, char *argv[])
181 int xin, want_gclk2, found, ppm = 100;
184 fprintf(stderr, "usage: mpc86x_clk <xin> <want_gclk2> [ppm]\n");
185 fprintf(stderr, " default ppm is 100\n");
190 want_gclk2 = atoi(argv[2]);
194 found = conf_clock(xin, want_gclk2, ppm);
196 fprintf(stderr, "cannot produce gclk2 %d from xin %d\n",