nv04.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * Copyright 2012 Red Hat Inc.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  18. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  19. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. * OTHER DEALINGS IN THE SOFTWARE.
  21. *
  22. * Authors: Ben Skeggs
  23. */
  24. #include "priv.h"
  25. #include "regsnv04.h"
  26. void
  27. nv04_timer_time(struct nvkm_timer *tmr, u64 time)
  28. {
  29. struct nvkm_subdev *subdev = &tmr->subdev;
  30. struct nvkm_device *device = subdev->device;
  31. u32 hi = upper_32_bits(time);
  32. u32 lo = lower_32_bits(time);
  33. nvkm_debug(subdev, "time low : %08x\n", lo);
  34. nvkm_debug(subdev, "time high : %08x\n", hi);
  35. nvkm_wr32(device, NV04_PTIMER_TIME_1, hi);
  36. nvkm_wr32(device, NV04_PTIMER_TIME_0, lo);
  37. }
  38. u64
  39. nv04_timer_read(struct nvkm_timer *tmr)
  40. {
  41. struct nvkm_device *device = tmr->subdev.device;
  42. u32 hi, lo;
  43. do {
  44. hi = nvkm_rd32(device, NV04_PTIMER_TIME_1);
  45. lo = nvkm_rd32(device, NV04_PTIMER_TIME_0);
  46. } while (hi != nvkm_rd32(device, NV04_PTIMER_TIME_1));
  47. return ((u64)hi << 32 | lo);
  48. }
  49. void
  50. nv04_timer_alarm_fini(struct nvkm_timer *tmr)
  51. {
  52. struct nvkm_device *device = tmr->subdev.device;
  53. nvkm_wr32(device, NV04_PTIMER_INTR_EN_0, 0x00000000);
  54. }
  55. void
  56. nv04_timer_alarm_init(struct nvkm_timer *tmr, u32 time)
  57. {
  58. struct nvkm_device *device = tmr->subdev.device;
  59. nvkm_wr32(device, NV04_PTIMER_ALARM_0, time);
  60. nvkm_wr32(device, NV04_PTIMER_INTR_EN_0, 0x00000001);
  61. }
  62. void
  63. nv04_timer_intr(struct nvkm_timer *tmr)
  64. {
  65. struct nvkm_subdev *subdev = &tmr->subdev;
  66. struct nvkm_device *device = subdev->device;
  67. u32 stat = nvkm_rd32(device, NV04_PTIMER_INTR_0);
  68. if (stat & 0x00000001) {
  69. nvkm_timer_alarm_trigger(tmr);
  70. nvkm_wr32(device, NV04_PTIMER_INTR_0, 0x00000001);
  71. stat &= ~0x00000001;
  72. }
  73. if (stat) {
  74. nvkm_error(subdev, "intr %08x\n", stat);
  75. nvkm_wr32(device, NV04_PTIMER_INTR_0, stat);
  76. }
  77. }
  78. static void
  79. nv04_timer_init(struct nvkm_timer *tmr)
  80. {
  81. struct nvkm_subdev *subdev = &tmr->subdev;
  82. struct nvkm_device *device = subdev->device;
  83. u32 f = 0; /*XXX: nvclk */
  84. u32 n, d;
  85. /* aim for 31.25MHz, which gives us nanosecond timestamps */
  86. d = 1000000 / 32;
  87. n = f;
  88. if (!f) {
  89. n = nvkm_rd32(device, NV04_PTIMER_NUMERATOR);
  90. d = nvkm_rd32(device, NV04_PTIMER_DENOMINATOR);
  91. if (!n || !d) {
  92. n = 1;
  93. d = 1;
  94. }
  95. nvkm_warn(subdev, "unknown input clock freq\n");
  96. }
  97. /* reduce ratio to acceptable values */
  98. while (((n % 5) == 0) && ((d % 5) == 0)) {
  99. n /= 5;
  100. d /= 5;
  101. }
  102. while (((n % 2) == 0) && ((d % 2) == 0)) {
  103. n /= 2;
  104. d /= 2;
  105. }
  106. while (n > 0xffff || d > 0xffff) {
  107. n >>= 1;
  108. d >>= 1;
  109. }
  110. nvkm_debug(subdev, "input frequency : %dHz\n", f);
  111. nvkm_debug(subdev, "numerator : %08x\n", n);
  112. nvkm_debug(subdev, "denominator : %08x\n", d);
  113. nvkm_debug(subdev, "timer frequency : %dHz\n", f * d / n);
  114. nvkm_wr32(device, NV04_PTIMER_NUMERATOR, n);
  115. nvkm_wr32(device, NV04_PTIMER_DENOMINATOR, d);
  116. }
  117. static const struct nvkm_timer_func
  118. nv04_timer = {
  119. .init = nv04_timer_init,
  120. .intr = nv04_timer_intr,
  121. .read = nv04_timer_read,
  122. .time = nv04_timer_time,
  123. .alarm_init = nv04_timer_alarm_init,
  124. .alarm_fini = nv04_timer_alarm_fini,
  125. };
  126. int
  127. nv04_timer_new(struct nvkm_device *device, int index, struct nvkm_timer **ptmr)
  128. {
  129. return nvkm_timer_new_(&nv04_timer, device, index, ptmr);
  130. }