| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
#include <xparameters.h> |
| 44 |
#include <xil_io.h> |
| 45 |
#include "test.h" |
| 46 |
#include "mmcm.h" |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
#define MIN(x, y) (x < y ? x : y) |
| 52 |
#define MAX(x, y) (x > y ? x : y) |
| 53 |
#define DIV_ROUND_UP(x,y) (((x) + (y) - 1) / (y)) |
| 54 |
#define DIV_ROUND_CLOSEST(x, y) (uint32_t)(((double)x / y) + 0.5) |
| 55 |
#define CLAMP(val, min, max) (val < min ? min : (val > max ? max :val)) |
| 56 |
#define ABS(x) (x < 0 ? -x : x) |
| 57 |
|
| 58 |
static const uint32_t mmcm_filter_table[] = |
| 59 |
{ |
| 60 |
0x01001990, 0x01001190, 0x01009890, 0x01001890, |
| 61 |
0x01008890, 0x01009090, 0x01009090, 0x01009090, |
| 62 |
0x01009090, 0x01000890, 0x01000890, 0x01000890, |
| 63 |
0x08009090, 0x01001090, 0x01001090, 0x01001090, |
| 64 |
0x01001090, 0x01001090, 0x01001090, 0x01001090, |
| 65 |
0x01001090, 0x01001090, 0x01001090, 0x01008090, |
| 66 |
0x01008090, 0x01008090, 0x01008090, 0x01008090, |
| 67 |
0x01008090, 0x01008090, 0x01008090, 0x01008090, |
| 68 |
0x01008090, 0x01008090, 0x01008090, 0x01008090, |
| 69 |
0x01008090, 0x08001090, 0x08001090, 0x08001090, |
| 70 |
0x08001090, 0x08001090, 0x08001090, 0x08001090, |
| 71 |
0x08001090, 0x08001090, 0x08001090 |
| 72 |
}; |
| 73 |
|
| 74 |
static const uint32_t mmcm_lock_table[] = |
| 75 |
{ |
| 76 |
0x060603e8, 0x060603e8, 0x080803e8, 0x0b0b03e8, |
| 77 |
0x0e0e03e8, 0x111103e8, 0x131303e8, 0x161603e8, |
| 78 |
0x191903e8, 0x1c1c03e8, 0x1f1f0384, 0x1f1f0339, |
| 79 |
0x1f1f02ee, 0x1f1f02bc, 0x1f1f028a, 0x1f1f0271, |
| 80 |
0x1f1f023f, 0x1f1f0226, 0x1f1f020d, 0x1f1f01f4, |
| 81 |
0x1f1f01db, 0x1f1f01c2, 0x1f1f01a9, 0x1f1f0190, |
| 82 |
0x1f1f0190, 0x1f1f0177, 0x1f1f015e, 0x1f1f015e, |
| 83 |
0x1f1f0145, 0x1f1f0145, 0x1f1f012c, 0x1f1f012c, |
| 84 |
0x1f1f012c, 0x1f1f0113, 0x1f1f0113, 0x1f1f0113 |
| 85 |
}; |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
uint32_t mmcm_addr; |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
void mmcm_init(uint32_t sel) |
| 96 |
{ |
| 97 |
mmcm_addr = ((sel == IICSEL_B1HPC_AXI)||(sel == IICSEL_B1HPC_PS7)) ? |
| 98 |
CFAD9122_1_BASEADDR : CFAD9122_0_BASEADDR; |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
void mmcm_write(uint32_t reg, |
| 105 |
uint32_t val) |
| 106 |
{ |
| 107 |
Xil_Out32(mmcm_addr + 0x4000 + reg, val); |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
static void mmcm_read(uint32_t reg, |
| 114 |
uint32_t *val) |
| 115 |
{ |
| 116 |
*val = Xil_In32(mmcm_addr + 0x4000 + reg); |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
static void mmcm_read_reg(uint32_t reg, |
| 123 |
uint32_t *val) |
| 124 |
{ |
| 125 |
uint32_t timeout = 1000000; |
| 126 |
uint32_t reg_val; |
| 127 |
|
| 128 |
do { |
| 129 |
mmcm_read(AXI_MMCM_REG_DRP_STATUS, ®_val); |
| 130 |
} while ((reg_val & AXI_MMCM_DRP_STATUS_BUSY) && --timeout); |
| 131 |
|
| 132 |
if (timeout == 0) { |
| 133 |
return; |
| 134 |
} |
| 135 |
|
| 136 |
reg_val = AXI_MMCM_DRP_CNTRL_SEL | AXI_MMCM_DRP_CNTRL_READ | (reg << 16); |
| 137 |
|
| 138 |
mmcm_write(AXI_MMCM_REG_DRP_CNTRL, reg_val); |
| 139 |
do { |
| 140 |
mmcm_read(AXI_MMCM_REG_DRP_STATUS, val); |
| 141 |
} while ((*val & AXI_MMCM_DRP_STATUS_BUSY) && --timeout); |
| 142 |
|
| 143 |
if (timeout == 0) { |
| 144 |
return; |
| 145 |
} |
| 146 |
|
| 147 |
*val &= 0xffff; |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
void mmcm_write_reg(uint32_t reg, |
| 154 |
uint32_t val, |
| 155 |
uint32_t mask) |
| 156 |
{ |
| 157 |
uint32_t timeout = 1000000; |
| 158 |
uint32_t reg_val; |
| 159 |
|
| 160 |
do { |
| 161 |
mmcm_read(AXI_MMCM_REG_DRP_STATUS, ®_val); |
| 162 |
} while ((reg_val & AXI_MMCM_DRP_STATUS_BUSY) && --timeout); |
| 163 |
|
| 164 |
if (timeout == 0) { |
| 165 |
return; |
| 166 |
} |
| 167 |
|
| 168 |
if (mask != 0xffff) { |
| 169 |
mmcm_read_reg(reg, ®_val); |
| 170 |
reg_val &= ~mask; |
| 171 |
} else { |
| 172 |
reg_val = 0; |
| 173 |
} |
| 174 |
|
| 175 |
reg_val |= AXI_MMCM_DRP_CNTRL_SEL | (reg << 16) | (val & mask); |
| 176 |
|
| 177 |
mmcm_write(AXI_MMCM_REG_DRP_CNTRL, reg_val); |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
static uint32_t mmcm_lookup_filter(uint32_t m) |
| 184 |
{ |
| 185 |
if (m < 47) |
| 186 |
return mmcm_filter_table[m]; |
| 187 |
|
| 188 |
return 0x08008090; |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
static uint32_t mmcm_lookup_lock(uint32_t m) |
| 195 |
{ |
| 196 |
if (m < 36) |
| 197 |
return mmcm_lock_table[m]; |
| 198 |
|
| 199 |
return 0x1f1f00fa; |
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
void mmcm_calc_params(uint32_t fin, |
| 206 |
uint32_t fout, |
| 207 |
uint32_t *best_d, |
| 208 |
uint32_t *best_m, |
| 209 |
uint32_t *best_dout) |
| 210 |
{ |
| 211 |
const uint32_t fpfd_min = 10000; |
| 212 |
const uint32_t fpfd_max = 300000; |
| 213 |
const uint32_t fvco_min = 600000; |
| 214 |
const uint32_t fvco_max = 1200000; |
| 215 |
uint32_t d = 0; |
| 216 |
uint32_t d_min = 0; |
| 217 |
uint32_t d_max = 0; |
| 218 |
uint32_t _d_min = 0; |
| 219 |
uint32_t _d_max = 0; |
| 220 |
uint32_t m = 0; |
| 221 |
uint32_t m_min = 0; |
| 222 |
uint32_t m_max = 0; |
| 223 |
uint32_t dout = 0; |
| 224 |
uint32_t fvco = 0; |
| 225 |
int32_t f = 0; |
| 226 |
int32_t best_f = 0; |
| 227 |
|
| 228 |
fin /= 1000; |
| 229 |
fout /= 1000; |
| 230 |
|
| 231 |
best_f = 0x7fffffff; |
| 232 |
*best_d = 0; |
| 233 |
*best_m = 0; |
| 234 |
*best_dout = 0; |
| 235 |
|
| 236 |
d_min = MAX(DIV_ROUND_UP(fin, fpfd_max), 1); |
| 237 |
d_max = MIN(fin / fpfd_min, 80); |
| 238 |
|
| 239 |
m_min = MAX(DIV_ROUND_UP(fvco_min, fin) * d_min, 1); |
| 240 |
m_max = MIN(fvco_max * d_max / fin, 64); |
| 241 |
|
| 242 |
for(m = m_min; m <= m_max; m++) { |
| 243 |
_d_min = MAX(d_min, DIV_ROUND_UP(fin * m, fvco_max)); |
| 244 |
_d_max = MIN(d_max, fin * m / fvco_min); |
| 245 |
|
| 246 |
for (d = _d_min; d <= _d_max; d++) { |
| 247 |
fvco = fin * m / d; |
| 248 |
dout = DIV_ROUND_CLOSEST(fvco, fout); |
| 249 |
dout = CLAMP(dout, 1, 128); |
| 250 |
f = fvco / dout; |
| 251 |
if (ABS(f - fout) < ABS(best_f - fout)) { |
| 252 |
best_f = f; |
| 253 |
*best_d = d; |
| 254 |
*best_m = m; |
| 255 |
*best_dout = dout; |
| 256 |
if (best_f == fout) |
| 257 |
return; |
| 258 |
} |
| 259 |
} |
| 260 |
} |
| 261 |
} |
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
void mmcm_calc_clk_params(uint32_t divider, |
| 267 |
uint32_t *low, |
| 268 |
uint32_t *high, |
| 269 |
uint32_t *edge, |
| 270 |
uint32_t *nocount) |
| 271 |
{ |
| 272 |
if (divider == 1) |
| 273 |
*nocount = 1; |
| 274 |
else |
| 275 |
*nocount = 0; |
| 276 |
*high = divider / 2; |
| 277 |
*edge = divider % 2; |
| 278 |
*low = divider - *high; |
| 279 |
} |
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
static void mmcm_enable(char enable) |
| 285 |
{ |
| 286 |
uint32_t val = AXI_MMCM_RESET_ENABLE; |
| 287 |
|
| 288 |
if (enable) |
| 289 |
val |= AXI_MMCM_RESET_MMCM_ENABLE; |
| 290 |
|
| 291 |
mmcm_write(AXI_MMCM_REG_RESET, val); |
| 292 |
} |
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
int32_t mmcm_set_rate(uint32_t rate, |
| 298 |
uint32_t parent_rate) |
| 299 |
{ |
| 300 |
uint32_t d = 0; |
| 301 |
uint32_t m = 0; |
| 302 |
uint32_t dout = 0; |
| 303 |
uint32_t nocount = 0; |
| 304 |
uint32_t high = 0; |
| 305 |
uint32_t edge = 0; |
| 306 |
uint32_t low = 0; |
| 307 |
uint32_t filter = 0; |
| 308 |
uint32_t lock = 0; |
| 309 |
|
| 310 |
if (parent_rate == 0 || rate == 0) |
| 311 |
return 0; |
| 312 |
|
| 313 |
mmcm_calc_params(parent_rate, rate, &d, &m, &dout); |
| 314 |
|
| 315 |
if (d == 0 || dout == 0 || m == 0) |
| 316 |
return 0; |
| 317 |
|
| 318 |
filter = mmcm_lookup_filter(m - 1); |
| 319 |
lock = mmcm_lookup_lock(m - 1); |
| 320 |
|
| 321 |
mmcm_enable(0); |
| 322 |
|
| 323 |
mmcm_calc_clk_params(dout, &low, &high, &edge, &nocount); |
| 324 |
mmcm_write_reg(MMCM_REG_CLKOUT0_1, (high << 6) | low, 0xefff); |
| 325 |
mmcm_write_reg(MMCM_REG_CLKOUT0_2, (edge << 7) | (nocount << 6), 0x03ff); |
| 326 |
|
| 327 |
dout *= 4; |
| 328 |
mmcm_calc_clk_params(dout, &low, &high, &edge, &nocount); |
| 329 |
mmcm_write_reg(MMCM_REG_CLKOUT1_1, (high << 6) | low, 0xefff); |
| 330 |
mmcm_write_reg(MMCM_REG_CLKOUT1_2, (edge << 7) | (nocount << 6), 0x03ff); |
| 331 |
|
| 332 |
mmcm_calc_clk_params(d, &low, &high, &edge, &nocount); |
| 333 |
mmcm_write_reg(MMCM_REG_CLK_DIV, (edge << 13) | (nocount << 12) | (high << 6) | low, 0x3fff); |
| 334 |
|
| 335 |
mmcm_calc_clk_params(m, &low, &high, &edge, &nocount); |
| 336 |
mmcm_write_reg(MMCM_REG_CLK_FB1, (high << 6) | low, 0xefff); |
| 337 |
mmcm_write_reg(MMCM_REG_CLK_FB2, (edge << 7) | (nocount << 6), 0x03ff); |
| 338 |
|
| 339 |
mmcm_write_reg(MMCM_REG_LOCK1, lock & 0x3ff, 0x3ff); |
| 340 |
mmcm_write_reg(MMCM_REG_LOCK2, (((lock >> 16) & 0x1f) << 10) | 0x1, 0x7fff); |
| 341 |
mmcm_write_reg(MMCM_REG_LOCK3, (((lock >> 24) & 0x1f) << 10) | 0x3e9, 0x7fff); |
| 342 |
mmcm_write_reg(MMCM_REG_FILTER1, filter >> 16, 0x9900); |
| 343 |
mmcm_write_reg(MMCM_REG_FILTER2, filter, 0x9900); |
| 344 |
|
| 345 |
mmcm_enable(1); |
| 346 |
|
| 347 |
return 0; |
| 348 |
} |
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
uint32_t mmcm_get_rate(uint32_t parent_rate) |
| 354 |
{ |
| 355 |
uint32_t d, m, dout; |
| 356 |
uint32_t reg; |
| 357 |
uint64_t tmp; |
| 358 |
|
| 359 |
mmcm_read_reg(MMCM_REG_CLKOUT0_1, ®); |
| 360 |
dout = (reg & 0x3f) + ((reg >> 6) & 0x3f); |
| 361 |
mmcm_read_reg(MMCM_REG_CLK_DIV, ®); |
| 362 |
d = (reg & 0x3f) + ((reg >> 6) & 0x3f); |
| 363 |
mmcm_read_reg(MMCM_REG_CLK_FB1, ®); |
| 364 |
m = (reg & 0x3f) + ((reg >> 6) & 0x3f); |
| 365 |
|
| 366 |
if (d == 0 || dout == 0) |
| 367 |
return 0; |
| 368 |
|
| 369 |
tmp = (uint64_t)(parent_rate / d) * m; |
| 370 |
tmp = tmp / dout; |
| 371 |
|
| 372 |
if (tmp > 0xffffffff) |
| 373 |
return 0xffffffff; |
| 374 |
|
| 375 |
return (uint32_t)tmp; |
| 376 |
} |