pq2.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * PowerQUICC II support functions
  4. *
  5. * Author: Scott Wood <[email protected]>
  6. *
  7. * Copyright (c) 2007 Freescale Semiconductor, Inc.
  8. */
  9. #include "ops.h"
  10. #include "types.h"
  11. #include "fsl-soc.h"
  12. #include "pq2.h"
  13. #include "stdio.h"
  14. #include "io.h"
  15. #define PQ2_SCCR (0x10c80/4) /* System Clock Configuration Register */
  16. #define PQ2_SCMR (0x10c88/4) /* System Clock Mode Register */
  17. static int pq2_corecnf_map[] = {
  18. 3, 2, 2, 2, 4, 4, 5, 9, 6, 11, 8, 10, 3, 12, 7, -1,
  19. 6, 5, 13, 2, 14, 4, 15, 9, 0, 11, 8, 10, 16, 12, 7, -1
  20. };
  21. /* Get various clocks from crystal frequency.
  22. * Returns zero on failure and non-zero on success.
  23. */
  24. int pq2_get_clocks(u32 crystal, u32 *sysfreq, u32 *corefreq,
  25. u32 *timebase, u32 *brgfreq)
  26. {
  27. u32 *immr;
  28. u32 sccr, scmr, mainclk, busclk;
  29. int corecnf, busdf, plldf, pllmf, dfbrg;
  30. immr = fsl_get_immr();
  31. if (!immr) {
  32. printf("pq2_get_clocks: Couldn't get IMMR base.\r\n");
  33. return 0;
  34. }
  35. sccr = in_be32(&immr[PQ2_SCCR]);
  36. scmr = in_be32(&immr[PQ2_SCMR]);
  37. dfbrg = sccr & 3;
  38. corecnf = (scmr >> 24) & 0x1f;
  39. busdf = (scmr >> 20) & 0xf;
  40. plldf = (scmr >> 12) & 1;
  41. pllmf = scmr & 0xfff;
  42. mainclk = crystal * (pllmf + 1) / (plldf + 1);
  43. busclk = mainclk / (busdf + 1);
  44. if (sysfreq)
  45. *sysfreq = mainclk / 2;
  46. if (timebase)
  47. *timebase = busclk / 4;
  48. if (brgfreq)
  49. *brgfreq = mainclk / (1 << ((dfbrg + 1) * 2));
  50. if (corefreq) {
  51. int coremult = pq2_corecnf_map[corecnf];
  52. if (coremult < 0)
  53. *corefreq = mainclk / 2;
  54. else if (coremult == 0)
  55. return 0;
  56. else
  57. *corefreq = busclk * coremult / 2;
  58. }
  59. return 1;
  60. }
  61. /* Set common device tree fields based on the given clock frequencies. */
  62. void pq2_set_clocks(u32 sysfreq, u32 corefreq, u32 timebase, u32 brgfreq)
  63. {
  64. void *node;
  65. dt_fixup_cpu_clocks(corefreq, timebase, sysfreq);
  66. node = finddevice("/soc/cpm");
  67. if (node)
  68. setprop(node, "clock-frequency", &sysfreq, 4);
  69. node = finddevice("/soc/cpm/brg");
  70. if (node)
  71. setprop(node, "clock-frequency", &brgfreq, 4);
  72. }
  73. int pq2_fixup_clocks(u32 crystal)
  74. {
  75. u32 sysfreq, corefreq, timebase, brgfreq;
  76. if (!pq2_get_clocks(crystal, &sysfreq, &corefreq, &timebase, &brgfreq))
  77. return 0;
  78. pq2_set_clocks(sysfreq, corefreq, timebase, brgfreq);
  79. return 1;
  80. }