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