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