paul@173 | 1 | /* |
paul@173 | 2 | * Common clock functionality. |
paul@173 | 3 | * |
paul@276 | 4 | * Copyright (C) 2023, 2024 Paul Boddie <paul@boddie.org.uk> |
paul@173 | 5 | * |
paul@173 | 6 | * This program is free software; you can redistribute it and/or |
paul@173 | 7 | * modify it under the terms of the GNU General Public License as |
paul@173 | 8 | * published by the Free Software Foundation; either version 2 of |
paul@173 | 9 | * the License, or (at your option) any later version. |
paul@173 | 10 | * |
paul@173 | 11 | * This program is distributed in the hope that it will be useful, |
paul@173 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@173 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@173 | 14 | * GNU General Public License for more details. |
paul@173 | 15 | * |
paul@173 | 16 | * You should have received a copy of the GNU General Public License |
paul@173 | 17 | * along with this program; if not, write to the Free Software |
paul@173 | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
paul@173 | 19 | * Boston, MA 02110-1301, USA |
paul@173 | 20 | */ |
paul@173 | 21 | |
paul@173 | 22 | #include <l4/devices/hw_mmio_register_block.h> |
paul@173 | 23 | |
paul@173 | 24 | #include "cpm-common.h" |
paul@173 | 25 | #include <math.h> |
paul@185 | 26 | #include <stdio.h> |
paul@173 | 27 | |
paul@173 | 28 | |
paul@173 | 29 | |
paul@173 | 30 | // Register access. |
paul@173 | 31 | |
paul@211 | 32 | Cpm_regs::Cpm_regs(l4_addr_t addr, Clock_base *clocks[]) |
paul@211 | 33 | : _clocks(clocks) |
paul@173 | 34 | { |
paul@173 | 35 | _regs = new Hw::Mmio_register_block<32>(addr); |
paul@173 | 36 | } |
paul@173 | 37 | |
paul@173 | 38 | // Utility methods. |
paul@173 | 39 | |
paul@173 | 40 | uint32_t |
paul@173 | 41 | Cpm_regs::get_field(uint32_t reg, uint32_t mask, uint8_t shift) |
paul@173 | 42 | { |
paul@173 | 43 | return (_regs[reg] & (mask << shift)) >> shift; |
paul@173 | 44 | } |
paul@173 | 45 | |
paul@173 | 46 | void |
paul@173 | 47 | Cpm_regs::set_field(uint32_t reg, uint32_t mask, uint8_t shift, uint32_t value) |
paul@173 | 48 | { |
paul@173 | 49 | _regs[reg] = (_regs[reg] & (~(mask << shift))) | ((mask & value) << shift); |
paul@173 | 50 | } |
paul@173 | 51 | |
paul@173 | 52 | Clock_base * |
paul@173 | 53 | Cpm_regs::get_clock(int num) |
paul@173 | 54 | { |
paul@173 | 55 | return _clocks[num]; |
paul@173 | 56 | } |
paul@173 | 57 | |
paul@173 | 58 | |
paul@173 | 59 | |
paul@173 | 60 | // Field methods. |
paul@173 | 61 | |
paul@173 | 62 | uint32_t |
paul@173 | 63 | Field::get_field(Cpm_regs ®s) |
paul@173 | 64 | { |
paul@173 | 65 | if (defined) |
paul@284 | 66 | { |
paul@284 | 67 | // With an encoding table, use the encoded value as index to obtain the |
paul@284 | 68 | // genuine value. |
paul@284 | 69 | |
paul@284 | 70 | if (table != NULL) |
paul@284 | 71 | return table[regs.get_field(reg, mask, bit)]; |
paul@284 | 72 | else |
paul@284 | 73 | return regs.get_field(reg, mask, bit) + adjustment; |
paul@284 | 74 | } |
paul@173 | 75 | else |
paul@173 | 76 | return 0; |
paul@173 | 77 | } |
paul@173 | 78 | |
paul@173 | 79 | void |
paul@173 | 80 | Field::set_field(Cpm_regs ®s, uint32_t value) |
paul@173 | 81 | { |
paul@173 | 82 | if (defined) |
paul@284 | 83 | { |
paul@284 | 84 | // With an encoding table, find the index of the presented value. |
paul@284 | 85 | |
paul@284 | 86 | if (table != NULL) |
paul@284 | 87 | { |
paul@284 | 88 | for (uint32_t i = 0; i < mask; i++) |
paul@284 | 89 | if (table[i] == value) |
paul@284 | 90 | regs.set_field(reg, mask, bit, i); |
paul@284 | 91 | } |
paul@284 | 92 | else |
paul@284 | 93 | regs.set_field(reg, mask, bit, value >= adjustment ? value - adjustment : 0); |
paul@284 | 94 | } |
paul@173 | 95 | } |
paul@173 | 96 | |
paul@173 | 97 | // Undefined field. |
paul@173 | 98 | |
paul@173 | 99 | Field Field::undefined; |
paul@173 | 100 | |
paul@173 | 101 | |
paul@173 | 102 | |
paul@173 | 103 | // Clock sources. |
paul@173 | 104 | |
paul@173 | 105 | enum Clock_identifiers |
paul@173 | 106 | Mux::get_input(int num) |
paul@173 | 107 | { |
paul@173 | 108 | if (num < _num_inputs) |
paul@173 | 109 | return _inputs[num]; |
paul@173 | 110 | else |
paul@243 | 111 | return Clock_none; |
paul@173 | 112 | } |
paul@173 | 113 | |
paul@243 | 114 | |
paul@243 | 115 | |
paul@173 | 116 | // Clock sources. |
paul@173 | 117 | |
paul@173 | 118 | uint8_t |
paul@173 | 119 | Source::get_source(Cpm_regs ®s) |
paul@173 | 120 | { |
paul@173 | 121 | if (_source.is_defined()) |
paul@173 | 122 | return _source.get_field(regs); |
paul@173 | 123 | else |
paul@173 | 124 | return 0; |
paul@173 | 125 | } |
paul@173 | 126 | |
paul@173 | 127 | void |
paul@173 | 128 | Source::set_source(Cpm_regs ®s, uint8_t source) |
paul@173 | 129 | { |
paul@173 | 130 | if (!_source.is_defined()) |
paul@173 | 131 | return; |
paul@173 | 132 | |
paul@173 | 133 | _source.set_field(regs, source); |
paul@173 | 134 | } |
paul@173 | 135 | |
paul@185 | 136 | enum Clock_identifiers |
paul@185 | 137 | Source::get_source_clock(Cpm_regs ®s) |
paul@185 | 138 | { |
paul@185 | 139 | return get_input(get_number() == 1 ? 0 : get_source(regs)); |
paul@185 | 140 | } |
paul@185 | 141 | |
paul@185 | 142 | void |
paul@185 | 143 | Source::set_source_clock(Cpm_regs ®s, enum Clock_identifiers clock) |
paul@185 | 144 | { |
paul@185 | 145 | for (int source = 0; source < _inputs.get_number(); source++) |
paul@185 | 146 | if (get_input(source) == clock) |
paul@185 | 147 | _source.set_field(regs, source); |
paul@185 | 148 | } |
paul@185 | 149 | |
paul@173 | 150 | // Clock source frequencies. |
paul@173 | 151 | |
paul@213 | 152 | uint64_t |
paul@173 | 153 | Source::get_frequency(Cpm_regs ®s) |
paul@173 | 154 | { |
paul@185 | 155 | enum Clock_identifiers input = get_source_clock(regs); |
paul@173 | 156 | |
paul@243 | 157 | if (input != Clock_none) |
paul@173 | 158 | return regs.get_clock(input)->get_frequency(regs); |
paul@173 | 159 | else |
paul@173 | 160 | return 0; |
paul@173 | 161 | } |
paul@173 | 162 | |
paul@173 | 163 | |
paul@173 | 164 | |
paul@175 | 165 | // Clock control. |
paul@175 | 166 | |
paul@175 | 167 | Control_base::~Control_base() |
paul@175 | 168 | { |
paul@175 | 169 | } |
paul@175 | 170 | |
paul@175 | 171 | void |
paul@175 | 172 | Control_base::change_disable(Cpm_regs ®s) |
paul@175 | 173 | { |
paul@175 | 174 | (void) regs; |
paul@175 | 175 | } |
paul@175 | 176 | |
paul@175 | 177 | void |
paul@175 | 178 | Control_base::change_enable(Cpm_regs ®s) |
paul@175 | 179 | { |
paul@175 | 180 | (void) regs; |
paul@175 | 181 | } |
paul@175 | 182 | |
paul@175 | 183 | int |
paul@175 | 184 | Control::have_clock(Cpm_regs ®s) |
paul@175 | 185 | { |
paul@276 | 186 | bool enabled_via_gate, enabled_via_stop; |
paul@276 | 187 | |
paul@175 | 188 | if (_gate.is_defined()) |
paul@276 | 189 | enabled_via_gate = _gate.get_field(regs) == _gate.get_asserted(); |
paul@175 | 190 | else |
paul@276 | 191 | enabled_via_gate = true; |
paul@276 | 192 | |
paul@276 | 193 | if (_stop.is_defined()) |
paul@276 | 194 | enabled_via_stop = _stop.get_field(regs) == _stop.get_asserted(); |
paul@276 | 195 | else |
paul@276 | 196 | enabled_via_stop = true; |
paul@276 | 197 | |
paul@276 | 198 | return enabled_via_gate && enabled_via_stop; |
paul@175 | 199 | } |
paul@175 | 200 | |
paul@175 | 201 | void |
paul@175 | 202 | Control::start_clock(Cpm_regs ®s) |
paul@175 | 203 | { |
paul@175 | 204 | if (_gate.is_defined()) |
paul@201 | 205 | _gate.set_field(regs, _gate.get_asserted()); |
paul@276 | 206 | |
paul@276 | 207 | if (_stop.is_defined()) |
paul@304 | 208 | { |
paul@304 | 209 | change_enable(regs); |
paul@276 | 210 | _stop.set_field(regs, _stop.get_asserted()); |
paul@304 | 211 | change_disable(regs); |
paul@304 | 212 | } |
paul@175 | 213 | } |
paul@175 | 214 | |
paul@175 | 215 | void |
paul@175 | 216 | Control::stop_clock(Cpm_regs ®s) |
paul@175 | 217 | { |
paul@175 | 218 | if (_gate.is_defined()) |
paul@201 | 219 | _gate.set_field(regs, _gate.get_deasserted()); |
paul@276 | 220 | |
paul@276 | 221 | if (_stop.is_defined()) |
paul@304 | 222 | { |
paul@304 | 223 | change_enable(regs); |
paul@276 | 224 | _stop.set_field(regs, _stop.get_deasserted()); |
paul@304 | 225 | change_disable(regs); |
paul@304 | 226 | } |
paul@175 | 227 | } |
paul@175 | 228 | |
paul@175 | 229 | void |
paul@175 | 230 | Control::wait_busy(Cpm_regs ®s) |
paul@175 | 231 | { |
paul@175 | 232 | if (_busy.is_defined()) |
paul@175 | 233 | while (_busy.get_field(regs)); |
paul@175 | 234 | } |
paul@175 | 235 | |
paul@175 | 236 | void |
paul@175 | 237 | Control::change_disable(Cpm_regs ®s) |
paul@175 | 238 | { |
paul@175 | 239 | if (_change_enable.is_defined()) |
paul@175 | 240 | _change_enable.set_field(regs, 0); |
paul@175 | 241 | } |
paul@175 | 242 | |
paul@175 | 243 | void |
paul@175 | 244 | Control::change_enable(Cpm_regs ®s) |
paul@175 | 245 | { |
paul@175 | 246 | if (_change_enable.is_defined()) |
paul@175 | 247 | _change_enable.set_field(regs, 1); |
paul@175 | 248 | } |
paul@175 | 249 | |
paul@211 | 250 | // Undefined control. |
paul@211 | 251 | |
paul@211 | 252 | Control Control::undefined; |
paul@211 | 253 | |
paul@175 | 254 | |
paul@175 | 255 | |
paul@175 | 256 | // PLL-specific control. |
paul@175 | 257 | |
paul@175 | 258 | int |
paul@175 | 259 | Control_pll::have_pll(Cpm_regs ®s) |
paul@175 | 260 | { |
paul@175 | 261 | return _stable.get_field(regs); |
paul@175 | 262 | } |
paul@175 | 263 | |
paul@175 | 264 | int |
paul@175 | 265 | Control_pll::pll_enabled(Cpm_regs ®s) |
paul@175 | 266 | { |
paul@175 | 267 | return _enable.get_field(regs); |
paul@175 | 268 | } |
paul@175 | 269 | |
paul@175 | 270 | int |
paul@175 | 271 | Control_pll::pll_bypassed(Cpm_regs ®s) |
paul@175 | 272 | { |
paul@175 | 273 | return _bypass.get_field(regs); |
paul@175 | 274 | } |
paul@175 | 275 | |
paul@187 | 276 | void |
paul@187 | 277 | Control_pll::pll_bypass(Cpm_regs ®s) |
paul@187 | 278 | { |
paul@187 | 279 | _bypass.set_field(regs, 1); |
paul@187 | 280 | } |
paul@187 | 281 | |
paul@187 | 282 | void |
paul@187 | 283 | Control_pll::pll_engage(Cpm_regs ®s) |
paul@187 | 284 | { |
paul@187 | 285 | _bypass.set_field(regs, 0); |
paul@187 | 286 | } |
paul@187 | 287 | |
paul@175 | 288 | // Clock control. |
paul@175 | 289 | |
paul@175 | 290 | int |
paul@175 | 291 | Control_pll::have_clock(Cpm_regs ®s) |
paul@175 | 292 | { |
paul@175 | 293 | return have_pll(regs) && pll_enabled(regs); |
paul@175 | 294 | } |
paul@175 | 295 | |
paul@175 | 296 | void |
paul@175 | 297 | Control_pll::start_clock(Cpm_regs ®s) |
paul@175 | 298 | { |
paul@175 | 299 | _enable.set_field(regs, 1); |
paul@175 | 300 | while (!have_pll(regs)); |
paul@175 | 301 | } |
paul@175 | 302 | |
paul@175 | 303 | void |
paul@175 | 304 | Control_pll::stop_clock(Cpm_regs ®s) |
paul@175 | 305 | { |
paul@175 | 306 | _enable.set_field(regs, 0); |
paul@175 | 307 | while (have_pll(regs)); |
paul@175 | 308 | } |
paul@175 | 309 | |
paul@175 | 310 | void |
paul@175 | 311 | Control_pll::wait_busy(Cpm_regs ®s) |
paul@175 | 312 | { |
paul@213 | 313 | // NOTE: Could wait for some kind of stable or "lock" signal, but the chips |
paul@213 | 314 | // provide all sorts of differing signals. |
paul@213 | 315 | |
paul@213 | 316 | (void) regs; |
paul@213 | 317 | } |
paul@213 | 318 | |
paul@213 | 319 | void |
paul@213 | 320 | Control_pll::change_disable(Cpm_regs ®s) |
paul@213 | 321 | { |
paul@213 | 322 | if (_enabled) |
paul@213 | 323 | start_clock(regs); |
paul@213 | 324 | } |
paul@213 | 325 | |
paul@213 | 326 | void |
paul@213 | 327 | Control_pll::change_enable(Cpm_regs ®s) |
paul@213 | 328 | { |
paul@213 | 329 | // NOTE: Since the X1600 manual warns of changing the frequency while enabled, |
paul@213 | 330 | // it is easier to just stop and then start the PLL again. |
paul@213 | 331 | |
paul@213 | 332 | _enabled = have_clock(regs); |
paul@213 | 333 | |
paul@213 | 334 | if (_enabled) |
paul@213 | 335 | stop_clock(regs); |
paul@175 | 336 | } |
paul@175 | 337 | |
paul@175 | 338 | |
paul@175 | 339 | |
paul@174 | 340 | // Clock dividers. |
paul@174 | 341 | |
paul@175 | 342 | Divider_base::~Divider_base() |
paul@175 | 343 | { |
paul@175 | 344 | } |
paul@175 | 345 | |
paul@175 | 346 | |
paul@175 | 347 | |
paul@239 | 348 | // Fixed divider. |
paul@239 | 349 | |
paul@239 | 350 | uint64_t |
paul@239 | 351 | Divider_fixed::get_frequency(Cpm_regs ®s, uint64_t source_frequency) |
paul@239 | 352 | { |
paul@239 | 353 | (void) regs; |
paul@239 | 354 | return source_frequency / _value; |
paul@239 | 355 | } |
paul@239 | 356 | |
paul@239 | 357 | int |
paul@239 | 358 | Divider_fixed::set_frequency(Cpm_regs ®s, uint64_t source_frequency, uint64_t frequency) |
paul@239 | 359 | { |
paul@239 | 360 | (void) regs; (void) source_frequency; (void) frequency; |
paul@239 | 361 | return 0; |
paul@239 | 362 | } |
paul@239 | 363 | |
paul@239 | 364 | int |
paul@239 | 365 | Divider_fixed::get_parameters(Cpm_regs ®s, uint32_t parameters[]) |
paul@239 | 366 | { |
paul@239 | 367 | (void) regs; |
paul@239 | 368 | parameters[0] = _value; |
paul@239 | 369 | return 1; |
paul@239 | 370 | } |
paul@239 | 371 | |
paul@239 | 372 | int |
paul@239 | 373 | Divider_fixed::set_parameters(Cpm_regs ®s, int num_parameters, uint32_t parameters[]) |
paul@239 | 374 | { |
paul@239 | 375 | (void) regs; (void) num_parameters; (void) parameters; |
paul@239 | 376 | return 0; |
paul@239 | 377 | } |
paul@239 | 378 | |
paul@239 | 379 | |
paul@239 | 380 | |
paul@239 | 381 | // Simple divider for regular clocks. |
paul@239 | 382 | |
paul@174 | 383 | uint32_t |
paul@174 | 384 | Divider::get_divider(Cpm_regs ®s) |
paul@174 | 385 | { |
paul@174 | 386 | if (_divider.is_defined()) |
paul@244 | 387 | return _scale * (_divider.get_field(regs) + 1); |
paul@174 | 388 | else |
paul@174 | 389 | return 1; |
paul@174 | 390 | } |
paul@174 | 391 | |
paul@174 | 392 | void |
paul@175 | 393 | Divider::set_divider(Cpm_regs ®s, uint32_t divider) |
paul@174 | 394 | { |
paul@174 | 395 | if (_divider.is_defined()) |
paul@244 | 396 | _divider.set_field(regs, divider / _scale - 1); |
paul@174 | 397 | } |
paul@174 | 398 | |
paul@174 | 399 | // Output clock frequencies. |
paul@174 | 400 | |
paul@213 | 401 | uint64_t |
paul@213 | 402 | Divider::get_frequency(Cpm_regs ®s, uint64_t source_frequency) |
paul@174 | 403 | { |
paul@174 | 404 | return source_frequency / get_divider(regs); |
paul@174 | 405 | } |
paul@174 | 406 | |
paul@178 | 407 | int |
paul@213 | 408 | Divider::set_frequency(Cpm_regs ®s, uint64_t source_frequency, uint64_t frequency) |
paul@187 | 409 | { |
paul@187 | 410 | set_divider(regs, (uint32_t) round((double) source_frequency / (double) frequency)); |
paul@187 | 411 | return 1; |
paul@187 | 412 | } |
paul@187 | 413 | |
paul@187 | 414 | int |
paul@178 | 415 | Divider::get_parameters(Cpm_regs ®s, uint32_t parameters[]) |
paul@178 | 416 | { |
paul@178 | 417 | parameters[0] = get_divider(regs); |
paul@178 | 418 | return 1; |
paul@178 | 419 | } |
paul@178 | 420 | |
paul@185 | 421 | int |
paul@185 | 422 | Divider::set_parameters(Cpm_regs ®s, int num_parameters, uint32_t parameters[]) |
paul@178 | 423 | { |
paul@185 | 424 | if (num_parameters) |
paul@185 | 425 | { |
paul@185 | 426 | set_divider(regs, parameters[0]); |
paul@185 | 427 | return 1; |
paul@185 | 428 | } |
paul@185 | 429 | |
paul@185 | 430 | return 0; |
paul@178 | 431 | } |
paul@178 | 432 | |
paul@174 | 433 | |
paul@174 | 434 | |
paul@187 | 435 | // Common divider functionality. |
paul@187 | 436 | |
paul@187 | 437 | static int is_integer(double x) |
paul@187 | 438 | { |
paul@187 | 439 | double target = round(x) * 1000; |
paul@187 | 440 | double rounded = floor(x * 1000); |
paul@187 | 441 | |
paul@187 | 442 | return (target - 100 < rounded) && (rounded < target + 100); |
paul@187 | 443 | } |
paul@187 | 444 | |
paul@187 | 445 | static double getscale_part(double x) |
paul@187 | 446 | { |
paul@187 | 447 | double part = x - floor(x); |
paul@187 | 448 | |
paul@187 | 449 | if (part > 0.5) |
paul@187 | 450 | return 1 / (1 - part); |
paul@187 | 451 | else if (part > 0) |
paul@187 | 452 | return 1 / part; |
paul@187 | 453 | else |
paul@187 | 454 | return 1; |
paul@187 | 455 | } |
paul@187 | 456 | |
paul@187 | 457 | static double getscale(double x) |
paul@187 | 458 | { |
paul@187 | 459 | double scale = getscale_part(x); |
paul@187 | 460 | |
paul@187 | 461 | if (is_integer(scale)) |
paul@187 | 462 | return scale; |
paul@187 | 463 | else |
paul@187 | 464 | return scale * getscale(scale); |
paul@187 | 465 | } |
paul@187 | 466 | |
paul@187 | 467 | static void get_divider_operands(double frequency, double source_frequency, |
paul@187 | 468 | double *multiplier, double *divider) |
paul@187 | 469 | { |
paul@187 | 470 | double ratio = frequency / source_frequency; |
paul@187 | 471 | double scale = getscale(ratio); |
paul@187 | 472 | |
paul@187 | 473 | *multiplier = scale * ratio; |
paul@187 | 474 | *divider = scale; |
paul@187 | 475 | } |
paul@187 | 476 | |
paul@187 | 477 | static void reduce_divider_operands(uint32_t *m, uint32_t *n, uint32_t m_limit, |
paul@187 | 478 | uint32_t n_limit) |
paul@187 | 479 | { |
paul@187 | 480 | while ((*m > m_limit) && (*n > n_limit) && (*m > 1) && (*n > 1)) |
paul@187 | 481 | { |
paul@187 | 482 | *m >>= 1; |
paul@187 | 483 | *n >>= 1; |
paul@187 | 484 | } |
paul@187 | 485 | } |
paul@187 | 486 | |
paul@187 | 487 | |
paul@187 | 488 | |
paul@219 | 489 | #define zero_as_one(X) ((X) ? (X) : 1) |
paul@187 | 490 | |
paul@187 | 491 | // Feedback multiplier. |
paul@174 | 492 | |
paul@175 | 493 | uint32_t |
paul@174 | 494 | Divider_pll::get_multiplier(Cpm_regs ®s) |
paul@174 | 495 | { |
paul@247 | 496 | return zero_as_one(_multiplier.get_field(regs)); |
paul@174 | 497 | } |
paul@174 | 498 | |
paul@174 | 499 | void |
paul@175 | 500 | Divider_pll::set_multiplier(Cpm_regs ®s, uint32_t multiplier) |
paul@174 | 501 | { |
paul@247 | 502 | _multiplier.set_field(regs, multiplier); |
paul@174 | 503 | } |
paul@174 | 504 | |
paul@187 | 505 | // Input divider. |
paul@174 | 506 | |
paul@175 | 507 | uint32_t |
paul@175 | 508 | Divider_pll::get_input_divider(Cpm_regs ®s) |
paul@174 | 509 | { |
paul@247 | 510 | return zero_as_one(_input_divider.get_field(regs)); |
paul@174 | 511 | } |
paul@174 | 512 | |
paul@174 | 513 | void |
paul@175 | 514 | Divider_pll::set_input_divider(Cpm_regs ®s, uint32_t divider) |
paul@174 | 515 | { |
paul@247 | 516 | _input_divider.set_field(regs, divider); |
paul@174 | 517 | } |
paul@174 | 518 | |
paul@187 | 519 | // Output dividers. |
paul@174 | 520 | |
paul@175 | 521 | uint32_t |
paul@175 | 522 | Divider_pll::get_output_divider(Cpm_regs ®s) |
paul@174 | 523 | { |
paul@219 | 524 | uint32_t d0, d1; |
paul@219 | 525 | |
paul@247 | 526 | d0 = zero_as_one(_output_divider0.get_field(regs)); |
paul@247 | 527 | d1 = _output_divider1.is_defined() ? |
paul@247 | 528 | zero_as_one(_output_divider1.get_field(regs)) : 1; |
paul@174 | 529 | |
paul@174 | 530 | return d0 * d1; |
paul@174 | 531 | } |
paul@174 | 532 | |
paul@174 | 533 | void |
paul@175 | 534 | Divider_pll::set_output_divider(Cpm_regs ®s, uint32_t divider) |
paul@174 | 535 | { |
paul@211 | 536 | uint32_t d0, d1; |
paul@211 | 537 | |
paul@174 | 538 | // Assert 1 as a minimum. |
paul@187 | 539 | |
paul@187 | 540 | if (!divider) |
paul@187 | 541 | divider = 1; |
paul@187 | 542 | |
paul@211 | 543 | // Attempt to set any single divider. |
paul@211 | 544 | |
paul@211 | 545 | if (!_output_divider1.is_defined()) |
paul@211 | 546 | { |
paul@247 | 547 | _output_divider0.set_field(regs, divider); |
paul@211 | 548 | return; |
paul@211 | 549 | } |
paul@211 | 550 | |
paul@211 | 551 | // For two-divider implementations such as the X1600, divider 0 must be less |
paul@211 | 552 | // than or equal to divider 1. |
paul@211 | 553 | |
paul@187 | 554 | if (divider < _output_divider1.get_limit()) |
paul@187 | 555 | { |
paul@187 | 556 | d0 = 1; |
paul@187 | 557 | d1 = divider; |
paul@187 | 558 | } |
paul@187 | 559 | else |
paul@187 | 560 | { |
paul@187 | 561 | d0 = (uint32_t) floor(sqrt(divider)); |
paul@187 | 562 | d1 = divider / d0; |
paul@187 | 563 | } |
paul@174 | 564 | |
paul@247 | 565 | _output_divider0.set_field(regs, d0); |
paul@247 | 566 | _output_divider1.set_field(regs, d1); |
paul@174 | 567 | } |
paul@174 | 568 | |
paul@213 | 569 | uint64_t |
paul@213 | 570 | Divider_pll::get_frequency(Cpm_regs ®s, uint64_t source_frequency) |
paul@174 | 571 | { |
paul@174 | 572 | return (source_frequency * get_multiplier(regs)) / |
paul@175 | 573 | (get_input_divider(regs) * get_output_divider(regs)); |
paul@174 | 574 | } |
paul@174 | 575 | |
paul@178 | 576 | int |
paul@213 | 577 | Divider_pll::set_frequency(Cpm_regs ®s, uint64_t source_frequency, uint64_t frequency) |
paul@187 | 578 | { |
paul@187 | 579 | double intermediate_multiplier, intermediate_input_divider; |
paul@187 | 580 | uint32_t output_min, output_max, output0, output1; |
paul@187 | 581 | uint32_t multiplier, input_divider, output_divider; |
paul@187 | 582 | |
paul@211 | 583 | // Define the range for the output dividers using the intermediate frequency |
paul@211 | 584 | // range applying to each chip, this being the result of the multiplier and |
paul@211 | 585 | // input divider. |
paul@187 | 586 | |
paul@211 | 587 | output_min = (uint32_t) ceil(_intermediate_min / frequency); |
paul@211 | 588 | output_max = (uint32_t) floor(_intermediate_max / frequency); |
paul@211 | 589 | |
paul@211 | 590 | // Distribute the divider across the input and output dividers. |
paul@187 | 591 | |
paul@187 | 592 | output_divider = output_min; |
paul@187 | 593 | |
paul@187 | 594 | while (output_divider <= output_max) |
paul@187 | 595 | { |
paul@211 | 596 | bool usable_divider; |
paul@211 | 597 | |
paul@187 | 598 | // Test divider constraints. |
paul@187 | 599 | |
paul@211 | 600 | if (_output_divider1.is_defined()) |
paul@211 | 601 | { |
paul@211 | 602 | output0 = (uint32_t) floor(sqrt(output_divider)); |
paul@211 | 603 | output1 = (uint32_t) floor(output_divider / output0); |
paul@187 | 604 | |
paul@211 | 605 | usable_divider = ((output0 * output1 == output_divider) && |
paul@187 | 606 | (output0 <= _output_divider0.get_limit()) && |
paul@211 | 607 | (output1 <= _output_divider1.get_limit())); |
paul@211 | 608 | } |
paul@211 | 609 | else |
paul@211 | 610 | usable_divider = output_divider <= _output_divider0.get_limit(); |
paul@211 | 611 | |
paul@211 | 612 | // Apply any usable divider. |
paul@211 | 613 | |
paul@211 | 614 | if (usable_divider) |
paul@187 | 615 | { |
paul@211 | 616 | // Calculate the other parameters. Start by working back from the desired |
paul@211 | 617 | // output frequency to obtain an intermediate frequency using the proposed |
paul@211 | 618 | // divider. |
paul@187 | 619 | |
paul@213 | 620 | double intermediate_frequency = frequency * output_divider; |
paul@187 | 621 | |
paul@211 | 622 | // Calculate the required multiplier and divider. |
paul@211 | 623 | |
paul@187 | 624 | get_divider_operands(intermediate_frequency, source_frequency, |
paul@187 | 625 | &intermediate_multiplier, &intermediate_input_divider); |
paul@187 | 626 | |
paul@187 | 627 | multiplier = (uint32_t) round(intermediate_multiplier); |
paul@187 | 628 | input_divider = (uint32_t) round(intermediate_input_divider); |
paul@187 | 629 | |
paul@211 | 630 | // Attempt to reduce the multiplier and divider to usable values. |
paul@211 | 631 | |
paul@187 | 632 | uint32_t multiplier_limit = _multiplier.get_limit(); |
paul@187 | 633 | uint32_t input_divider_limit = _input_divider.get_limit(); |
paul@187 | 634 | |
paul@187 | 635 | reduce_divider_operands(&multiplier, &input_divider, |
paul@187 | 636 | multiplier_limit, input_divider_limit); |
paul@187 | 637 | |
paul@187 | 638 | if ((multiplier <= multiplier_limit) && (input_divider <= input_divider_limit)) |
paul@187 | 639 | { |
paul@187 | 640 | set_multiplier(regs, multiplier); |
paul@187 | 641 | set_input_divider(regs, input_divider); |
paul@187 | 642 | set_output_divider(regs, output_divider); |
paul@187 | 643 | |
paul@187 | 644 | return 1; |
paul@187 | 645 | } |
paul@187 | 646 | } |
paul@187 | 647 | |
paul@187 | 648 | output_divider++; |
paul@187 | 649 | } |
paul@187 | 650 | |
paul@187 | 651 | return 0; |
paul@187 | 652 | } |
paul@187 | 653 | |
paul@187 | 654 | int |
paul@178 | 655 | Divider_pll::get_parameters(Cpm_regs ®s, uint32_t parameters[]) |
paul@174 | 656 | { |
paul@178 | 657 | parameters[0] = get_multiplier(regs); |
paul@178 | 658 | parameters[1] = get_input_divider(regs); |
paul@178 | 659 | parameters[2] = get_output_divider(regs); |
paul@178 | 660 | return 3; |
paul@178 | 661 | } |
paul@178 | 662 | |
paul@185 | 663 | int |
paul@185 | 664 | Divider_pll::set_parameters(Cpm_regs ®s, int num_parameters, uint32_t parameters[]) |
paul@178 | 665 | { |
paul@185 | 666 | if (num_parameters > 2) |
paul@185 | 667 | { |
paul@185 | 668 | set_multiplier(regs, parameters[0]); |
paul@185 | 669 | set_input_divider(regs, parameters[1]); |
paul@185 | 670 | set_output_divider(regs, parameters[2]); |
paul@185 | 671 | |
paul@185 | 672 | return 3; |
paul@185 | 673 | } |
paul@185 | 674 | |
paul@185 | 675 | return 0; |
paul@174 | 676 | } |
paul@174 | 677 | |
paul@174 | 678 | |
paul@174 | 679 | |
paul@175 | 680 | // I2S clock divider. |
paul@175 | 681 | |
paul@175 | 682 | uint32_t |
paul@175 | 683 | Divider_i2s::get_multiplier(Cpm_regs ®s) |
paul@175 | 684 | { |
paul@175 | 685 | return _multiplier.get_field(regs); |
paul@175 | 686 | } |
paul@175 | 687 | |
paul@175 | 688 | uint32_t |
paul@178 | 689 | Divider_i2s::get_divider_N(Cpm_regs ®s) |
paul@178 | 690 | { |
paul@178 | 691 | return _divider_N.get_field(regs); |
paul@178 | 692 | } |
paul@178 | 693 | |
paul@178 | 694 | uint32_t |
paul@175 | 695 | Divider_i2s::get_divider_D(Cpm_regs ®s) |
paul@175 | 696 | { |
paul@175 | 697 | return _divider_D.get_field(regs); |
paul@175 | 698 | } |
paul@175 | 699 | |
paul@213 | 700 | uint64_t |
paul@213 | 701 | Divider_i2s::get_frequency(Cpm_regs ®s, uint64_t source_frequency) |
paul@175 | 702 | { |
paul@187 | 703 | /* NOTE: Assuming that this is the formula, given that the manual does not |
paul@187 | 704 | really describe how D is used. */ |
paul@187 | 705 | |
paul@175 | 706 | return (source_frequency * get_multiplier(regs)) / |
paul@175 | 707 | (get_divider_N(regs) * get_divider_D(regs)); |
paul@175 | 708 | } |
paul@175 | 709 | |
paul@178 | 710 | int |
paul@213 | 711 | Divider_i2s::set_frequency(Cpm_regs ®s, uint64_t source_frequency, uint64_t frequency) |
paul@187 | 712 | { |
paul@187 | 713 | double m, n; |
paul@187 | 714 | |
paul@187 | 715 | get_divider_operands(frequency, source_frequency, &m, &n); |
paul@187 | 716 | |
paul@187 | 717 | uint32_t multiplier = (uint32_t) round(m); |
paul@187 | 718 | uint32_t divider = (uint32_t) round(n); |
paul@187 | 719 | |
paul@187 | 720 | reduce_divider_operands(&multiplier, ÷r, |
paul@187 | 721 | _multiplier.get_limit(), |
paul@187 | 722 | _divider_N.get_limit()); |
paul@187 | 723 | |
paul@187 | 724 | // Test for operand within limits and the N >= 2M constraint. |
paul@187 | 725 | |
paul@187 | 726 | if ((multiplier <= _multiplier.get_limit()) && (divider <= _divider_N.get_limit()) && |
paul@187 | 727 | (divider >= 2 * multiplier)) |
paul@187 | 728 | { |
paul@187 | 729 | /* NOTE: Setting D to 1. Even though it seems that D might also be used, |
paul@187 | 730 | it does not seem necessary in practice, and the documentation is |
paul@187 | 731 | unclear about its use. */ |
paul@187 | 732 | |
paul@187 | 733 | uint32_t parameters[] = {multiplier, divider, 1}; |
paul@187 | 734 | |
paul@187 | 735 | set_parameters(regs, 3, parameters); |
paul@187 | 736 | return 1; |
paul@187 | 737 | } |
paul@187 | 738 | |
paul@187 | 739 | return 0; |
paul@187 | 740 | } |
paul@187 | 741 | |
paul@187 | 742 | int |
paul@178 | 743 | Divider_i2s::get_parameters(Cpm_regs ®s, uint32_t parameters[]) |
paul@178 | 744 | { |
paul@178 | 745 | parameters[0] = get_multiplier(regs); |
paul@178 | 746 | parameters[1] = get_divider_N(regs); |
paul@178 | 747 | parameters[2] = get_divider_D(regs); |
paul@178 | 748 | return 3; |
paul@178 | 749 | } |
paul@178 | 750 | |
paul@185 | 751 | int |
paul@185 | 752 | Divider_i2s::set_parameters(Cpm_regs ®s, int num_parameters, uint32_t parameters[]) |
paul@175 | 753 | { |
paul@185 | 754 | if (num_parameters == 1) |
paul@185 | 755 | { |
paul@185 | 756 | // Set automatic N and D value calculation if only one parameter is given. |
paul@185 | 757 | |
paul@185 | 758 | _auto_N.set_field(regs, 0); |
paul@185 | 759 | _auto_D.set_field(regs, 0); |
paul@185 | 760 | _multiplier.set_field(regs, parameters[0]); |
paul@185 | 761 | |
paul@185 | 762 | return 1; |
paul@185 | 763 | } |
paul@185 | 764 | else if (num_parameters > 1) |
paul@185 | 765 | { |
paul@187 | 766 | // Require N >= 2M, returning otherwise. |
paul@178 | 767 | |
paul@185 | 768 | if (parameters[1] < 2 * parameters[0]) |
paul@185 | 769 | return 0; |
paul@185 | 770 | |
paul@185 | 771 | // Set automatic D value calculation if only two parameters are given. |
paul@185 | 772 | |
paul@185 | 773 | _auto_N.set_field(regs, 1); |
paul@185 | 774 | _auto_D.set_field(regs, (num_parameters == 2) ? 0 : 1); |
paul@175 | 775 | |
paul@185 | 776 | _multiplier.set_field(regs, parameters[0]); |
paul@185 | 777 | _divider_N.set_field(regs, parameters[1]); |
paul@185 | 778 | |
paul@185 | 779 | // Set D explicitly if given. |
paul@185 | 780 | |
paul@185 | 781 | if (num_parameters > 2) |
paul@185 | 782 | _divider_D.set_field(regs, parameters[2]); |
paul@185 | 783 | |
paul@185 | 784 | return num_parameters; |
paul@185 | 785 | } |
paul@185 | 786 | |
paul@185 | 787 | return 0; |
paul@175 | 788 | } |
paul@175 | 789 | |
paul@175 | 790 | |
paul@175 | 791 | |
paul@175 | 792 | // Clock interface. |
paul@175 | 793 | |
paul@175 | 794 | Clock_base::~Clock_base() |
paul@175 | 795 | { |
paul@175 | 796 | } |
paul@175 | 797 | |
paul@175 | 798 | |
paul@175 | 799 | |
paul@175 | 800 | // Null clock. |
paul@173 | 801 | |
paul@173 | 802 | int |
paul@175 | 803 | Clock_null::have_clock(Cpm_regs ®s) |
paul@175 | 804 | { |
paul@175 | 805 | (void) regs; |
paul@175 | 806 | return false; |
paul@175 | 807 | } |
paul@175 | 808 | |
paul@175 | 809 | void |
paul@175 | 810 | Clock_null::start_clock(Cpm_regs ®s) |
paul@175 | 811 | { |
paul@175 | 812 | (void) regs; |
paul@175 | 813 | } |
paul@175 | 814 | |
paul@175 | 815 | void |
paul@175 | 816 | Clock_null::stop_clock(Cpm_regs ®s) |
paul@175 | 817 | { |
paul@175 | 818 | (void) regs; |
paul@175 | 819 | } |
paul@175 | 820 | |
paul@175 | 821 | // Output clock frequencies. |
paul@175 | 822 | |
paul@213 | 823 | uint64_t |
paul@175 | 824 | Clock_null::get_frequency(Cpm_regs ®s) |
paul@175 | 825 | { |
paul@175 | 826 | (void) regs; |
paul@175 | 827 | return 0; |
paul@175 | 828 | } |
paul@175 | 829 | |
paul@175 | 830 | |
paul@175 | 831 | |
paul@175 | 832 | // Passive clock. |
paul@175 | 833 | |
paul@175 | 834 | int |
paul@175 | 835 | Clock_passive::have_clock(Cpm_regs ®s) |
paul@173 | 836 | { |
paul@173 | 837 | (void) regs; |
paul@173 | 838 | return true; |
paul@173 | 839 | } |
paul@173 | 840 | |
paul@173 | 841 | void |
paul@175 | 842 | Clock_passive::start_clock(Cpm_regs ®s) |
paul@173 | 843 | { |
paul@173 | 844 | (void) regs; |
paul@173 | 845 | } |
paul@173 | 846 | |
paul@173 | 847 | void |
paul@175 | 848 | Clock_passive::stop_clock(Cpm_regs ®s) |
paul@173 | 849 | { |
paul@173 | 850 | (void) regs; |
paul@173 | 851 | } |
paul@173 | 852 | |
paul@175 | 853 | // Output clock frequencies. |
paul@173 | 854 | |
paul@213 | 855 | uint64_t |
paul@175 | 856 | Clock_passive::get_frequency(Cpm_regs ®s) |
paul@173 | 857 | { |
paul@211 | 858 | (void) regs; |
paul@211 | 859 | return _frequency; |
paul@175 | 860 | } |
paul@175 | 861 | |
paul@175 | 862 | |
paul@175 | 863 | |
paul@175 | 864 | // Clock control. |
paul@175 | 865 | |
paul@175 | 866 | int |
paul@179 | 867 | Clock_controlled::have_clock(Cpm_regs ®s) |
paul@175 | 868 | { |
paul@175 | 869 | return _get_control().have_clock(regs); |
paul@173 | 870 | } |
paul@173 | 871 | |
paul@173 | 872 | void |
paul@179 | 873 | Clock_controlled::start_clock(Cpm_regs ®s) |
paul@173 | 874 | { |
paul@175 | 875 | _get_control().start_clock(regs); |
paul@175 | 876 | } |
paul@175 | 877 | |
paul@175 | 878 | void |
paul@179 | 879 | Clock_controlled::stop_clock(Cpm_regs ®s) |
paul@175 | 880 | { |
paul@175 | 881 | _get_control().stop_clock(regs); |
paul@173 | 882 | } |
paul@173 | 883 | |
paul@179 | 884 | |
paul@179 | 885 | |
paul@179 | 886 | // Active clock interface. |
paul@179 | 887 | |
paul@179 | 888 | Clock_active::~Clock_active() |
paul@179 | 889 | { |
paul@179 | 890 | } |
paul@179 | 891 | |
paul@173 | 892 | // Clock sources. |
paul@173 | 893 | |
paul@173 | 894 | uint8_t |
paul@175 | 895 | Clock_active::get_source(Cpm_regs ®s) |
paul@173 | 896 | { |
paul@173 | 897 | return _source.get_source(regs); |
paul@173 | 898 | } |
paul@173 | 899 | |
paul@173 | 900 | void |
paul@175 | 901 | Clock_active::set_source(Cpm_regs ®s, uint8_t source) |
paul@173 | 902 | { |
paul@175 | 903 | _get_control().change_enable(regs); |
paul@173 | 904 | _source.set_source(regs, source); |
paul@175 | 905 | _get_control().wait_busy(regs); |
paul@175 | 906 | _get_control().change_disable(regs); |
paul@173 | 907 | } |
paul@173 | 908 | |
paul@185 | 909 | enum Clock_identifiers |
paul@185 | 910 | Clock_active::get_source_clock(Cpm_regs ®s) |
paul@185 | 911 | { |
paul@185 | 912 | return _source.get_source_clock(regs); |
paul@185 | 913 | } |
paul@185 | 914 | |
paul@185 | 915 | void |
paul@185 | 916 | Clock_active::set_source_clock(Cpm_regs ®s, enum Clock_identifiers clock) |
paul@185 | 917 | { |
paul@185 | 918 | _source.set_source_clock(regs, clock); |
paul@185 | 919 | } |
paul@185 | 920 | |
paul@173 | 921 | // Clock source frequencies. |
paul@173 | 922 | |
paul@213 | 923 | uint64_t |
paul@175 | 924 | Clock_active::get_source_frequency(Cpm_regs ®s) |
paul@173 | 925 | { |
paul@173 | 926 | return _source.get_frequency(regs); |
paul@173 | 927 | } |
paul@173 | 928 | |
paul@173 | 929 | // Output clock frequencies. |
paul@173 | 930 | |
paul@213 | 931 | uint64_t |
paul@175 | 932 | Clock_active::get_frequency(Cpm_regs ®s) |
paul@173 | 933 | { |
paul@174 | 934 | return get_source_frequency(regs); |
paul@173 | 935 | } |
paul@173 | 936 | |
paul@173 | 937 | |
paul@173 | 938 | |
paul@175 | 939 | // Divided clock interface. |
paul@173 | 940 | |
paul@183 | 941 | Clock_divided_base::~Clock_divided_base() |
paul@173 | 942 | { |
paul@173 | 943 | } |
paul@173 | 944 | |
paul@175 | 945 | // Output clock frequencies. |
paul@173 | 946 | |
paul@213 | 947 | uint64_t |
paul@183 | 948 | Clock_divided_base::get_frequency(Cpm_regs ®s) |
paul@173 | 949 | { |
paul@175 | 950 | return _get_divider().get_frequency(regs, get_source_frequency(regs)); |
paul@173 | 951 | } |
paul@173 | 952 | |
paul@178 | 953 | int |
paul@213 | 954 | Clock_divided_base::set_frequency(Cpm_regs ®s, uint64_t frequency) |
paul@187 | 955 | { |
paul@187 | 956 | _get_control().change_enable(regs); |
paul@187 | 957 | int result = _get_divider().set_frequency(regs, get_source_frequency(regs), frequency); |
paul@187 | 958 | _get_control().wait_busy(regs); |
paul@187 | 959 | _get_control().change_disable(regs); |
paul@187 | 960 | |
paul@187 | 961 | return result; |
paul@187 | 962 | } |
paul@187 | 963 | |
paul@187 | 964 | int |
paul@183 | 965 | Clock_divided_base::get_parameters(Cpm_regs ®s, uint32_t parameters[]) |
paul@178 | 966 | { |
paul@178 | 967 | return _get_divider().get_parameters(regs, parameters); |
paul@178 | 968 | } |
paul@178 | 969 | |
paul@185 | 970 | int |
paul@185 | 971 | Clock_divided_base::set_parameters(Cpm_regs ®s, int num_parameters, uint32_t parameters[]) |
paul@178 | 972 | { |
paul@181 | 973 | _get_control().change_enable(regs); |
paul@185 | 974 | int n = _get_divider().set_parameters(regs, num_parameters, parameters); |
paul@178 | 975 | _get_control().wait_busy(regs); |
paul@181 | 976 | _get_control().change_disable(regs); |
paul@185 | 977 | |
paul@185 | 978 | return n; |
paul@178 | 979 | } |
paul@178 | 980 | |
paul@175 | 981 | |
paul@175 | 982 | |
paul@180 | 983 | // PLL functionality. |
paul@175 | 984 | |
paul@175 | 985 | Pll::~Pll() |
paul@173 | 986 | { |
paul@173 | 987 | } |
paul@173 | 988 | |
paul@213 | 989 | uint64_t |
paul@173 | 990 | Pll::get_frequency(Cpm_regs ®s) |
paul@173 | 991 | { |
paul@185 | 992 | if (!_control.pll_bypassed(regs)) |
paul@185 | 993 | return _divider.get_frequency(regs, get_source_frequency(regs)); |
paul@173 | 994 | else |
paul@185 | 995 | return get_source_frequency(regs); |
paul@173 | 996 | } |
paul@187 | 997 | |
paul@187 | 998 | int |
paul@213 | 999 | Pll::set_frequency(Cpm_regs ®s, uint64_t frequency) |
paul@187 | 1000 | { |
paul@187 | 1001 | int result = Clock_divided_base::set_frequency(regs, frequency); |
paul@187 | 1002 | _control.pll_engage(regs); |
paul@187 | 1003 | |
paul@187 | 1004 | return result; |
paul@187 | 1005 | } |