blob: 55c945290e5214af35c92fd9c76af03ea4045fba [file] [log] [blame]
Ben Skeggs6ee73862009-12-11 19:24:15 +10001#include "drmP.h"
2#include "drm.h"
3#include "nouveau_drv.h"
4#include "nouveau_drm.h"
Ben Skeggsff2b6c62011-10-27 10:28:17 +10005#include "nouveau_hw.h"
Ben Skeggs6ee73862009-12-11 19:24:15 +10006
Ben Skeggs591b06d2011-07-03 21:16:12 +10007int
8nv04_timer_init(struct drm_device *dev)
9{
10 struct drm_nouveau_private *dev_priv = dev->dev_private;
11 u32 m, n, d;
12
13 nv_wr32(dev, NV04_PTIMER_INTR_EN_0, 0x00000000);
14 nv_wr32(dev, NV04_PTIMER_INTR_0, 0xFFFFFFFF);
15
16 /* aim for 31.25MHz, which gives us nanosecond timestamps */
Ben Skeggsafb0c792011-07-21 16:12:58 +100017 d = 1000000 / 32;
Ben Skeggs591b06d2011-07-03 21:16:12 +100018
19 /* determine base clock for timer source */
20 if (dev_priv->chipset < 0x40) {
Ben Skeggsff2b6c62011-10-27 10:28:17 +100021 n = nouveau_hw_get_clock(dev, PLL_CORE);
Ben Skeggs591b06d2011-07-03 21:16:12 +100022 } else
23 if (dev_priv->chipset == 0x40) {
24 /*XXX: figure this out */
25 n = 0;
26 } else {
Ben Skeggsafb0c792011-07-21 16:12:58 +100027 n = dev_priv->crystal;
Ben Skeggs591b06d2011-07-03 21:16:12 +100028 m = 1;
29 while (n < (d * 2)) {
30 n += (n / m);
31 m++;
32 }
33
34 nv_wr32(dev, 0x009220, m - 1);
35 }
36
37 if (!n) {
38 NV_WARN(dev, "PTIMER: unknown input clock freq\n");
39 if (!nv_rd32(dev, NV04_PTIMER_NUMERATOR) ||
40 !nv_rd32(dev, NV04_PTIMER_DENOMINATOR)) {
41 nv_wr32(dev, NV04_PTIMER_NUMERATOR, 1);
42 nv_wr32(dev, NV04_PTIMER_DENOMINATOR, 1);
43 }
44 return 0;
45 }
46
47 /* reduce ratio to acceptable values */
48 while (((n % 5) == 0) && ((d % 5) == 0)) {
49 n /= 5;
50 d /= 5;
51 }
52
53 while (((n % 2) == 0) && ((d % 2) == 0)) {
54 n /= 2;
55 d /= 2;
56 }
57
58 while (n > 0xffff || d > 0xffff) {
59 n >>= 1;
60 d >>= 1;
61 }
62
63 nv_wr32(dev, NV04_PTIMER_NUMERATOR, n);
64 nv_wr32(dev, NV04_PTIMER_DENOMINATOR, d);
65 return 0;
66}
67
68u64
Ben Skeggs6ee73862009-12-11 19:24:15 +100069nv04_timer_read(struct drm_device *dev)
70{
Ben Skeggs591b06d2011-07-03 21:16:12 +100071 u32 hi, lo;
72
Ben Skeggs6ee73862009-12-11 19:24:15 +100073 do {
Ben Skeggs591b06d2011-07-03 21:16:12 +100074 hi = nv_rd32(dev, NV04_PTIMER_TIME_1);
75 lo = nv_rd32(dev, NV04_PTIMER_TIME_0);
76 } while (hi != nv_rd32(dev, NV04_PTIMER_TIME_1));
77
78 return ((u64)hi << 32 | lo);
Ben Skeggs6ee73862009-12-11 19:24:15 +100079}
80
81void
82nv04_timer_takedown(struct drm_device *dev)
83{
84}