v850e_utils.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /*
  2. * include/asm-v850/v850e_utils.h -- Utility functions associated with
  3. * V850E CPUs
  4. *
  5. * Copyright (C) 2001,02,03 NEC Electronics Corporation
  6. * Copyright (C) 2001,02,03 Miles Bader <miles@gnu.org>
  7. *
  8. * This file is subject to the terms and conditions of the GNU General
  9. * Public License. See the file COPYING in the main directory of this
  10. * archive for more details.
  11. *
  12. * Written by Miles Bader <miles@gnu.org>
  13. */
  14. #include <asm/v850e_utils.h>
  15. /* Calculate counter clock-divider and count values to attain the
  16. desired frequency RATE from the base frequency BASE_FREQ. The
  17. counter is expected to have a clock-divider, which can divide the
  18. system cpu clock by a power of two value from MIN_DIVLOG2 to
  19. MAX_DIV_LOG2, and a word-size of COUNTER_SIZE bits (the counter
  20. counts up and resets whenever it's equal to the compare register,
  21. generating an interrupt or whatever when it does so). The returned
  22. values are: *DIVLOG2 -- log2 of the desired clock divider and *COUNT
  23. -- the counter compare value to use. Returns true if it was possible
  24. to find a reasonable value, otherwise false (and the other return
  25. values will be set to be as good as possible). */
  26. int calc_counter_params (unsigned long base_freq,
  27. unsigned long rate,
  28. unsigned min_divlog2, unsigned max_divlog2,
  29. unsigned counter_size,
  30. unsigned *divlog2, unsigned *count)
  31. {
  32. unsigned _divlog2;
  33. int ok = 0;
  34. /* Find the lowest clock divider setting that can represent RATE. */
  35. for (_divlog2 = min_divlog2; _divlog2 <= max_divlog2; _divlog2++) {
  36. /* Minimum interrupt rate possible using this divider. */
  37. unsigned min_int_rate
  38. = (base_freq >> _divlog2) >> counter_size;
  39. if (min_int_rate <= rate) {
  40. /* This setting is the highest resolution
  41. setting that's slow enough enough to attain
  42. RATE interrupts per second, so use it. */
  43. ok = 1;
  44. break;
  45. }
  46. }
  47. if (_divlog2 > max_divlog2)
  48. /* Can't find correct setting. */
  49. _divlog2 = max_divlog2;
  50. if (divlog2)
  51. *divlog2 = _divlog2;
  52. if (count)
  53. *count = ((base_freq >> _divlog2) + rate/2) / rate;
  54. return ok;
  55. }