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