Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
nv04.c
Go to the documentation of this file.
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 
25 #include <subdev/timer.h>
26 
27 #define NV04_PTIMER_INTR_0 0x009100
28 #define NV04_PTIMER_INTR_EN_0 0x009140
29 #define NV04_PTIMER_NUMERATOR 0x009200
30 #define NV04_PTIMER_DENOMINATOR 0x009210
31 #define NV04_PTIMER_TIME_0 0x009400
32 #define NV04_PTIMER_TIME_1 0x009410
33 #define NV04_PTIMER_ALARM_0 0x009420
34 
37  struct list_head alarms;
39 };
40 
41 static u64
42 nv04_timer_read(struct nouveau_timer *ptimer)
43 {
44  struct nv04_timer_priv *priv = (void *)ptimer;
45  u32 hi, lo;
46 
47  do {
48  hi = nv_rd32(priv, NV04_PTIMER_TIME_1);
49  lo = nv_rd32(priv, NV04_PTIMER_TIME_0);
50  } while (hi != nv_rd32(priv, NV04_PTIMER_TIME_1));
51 
52  return ((u64)hi << 32 | lo);
53 }
54 
55 static void
56 nv04_timer_alarm_trigger(struct nouveau_timer *ptimer)
57 {
58  struct nv04_timer_priv *priv = (void *)ptimer;
59  struct nouveau_alarm *alarm, *atemp;
60  unsigned long flags;
61  LIST_HEAD(exec);
62 
63  /* move any due alarms off the pending list */
64  spin_lock_irqsave(&priv->lock, flags);
65  list_for_each_entry_safe(alarm, atemp, &priv->alarms, head) {
66  if (alarm->timestamp <= ptimer->read(ptimer))
67  list_move_tail(&alarm->head, &exec);
68  }
69 
70  /* reschedule interrupt for next alarm time */
71  if (!list_empty(&priv->alarms)) {
72  alarm = list_first_entry(&priv->alarms, typeof(*alarm), head);
73  nv_wr32(priv, NV04_PTIMER_ALARM_0, alarm->timestamp);
74  nv_wr32(priv, NV04_PTIMER_INTR_EN_0, 0x00000001);
75  } else {
76  nv_wr32(priv, NV04_PTIMER_INTR_EN_0, 0x00000000);
77  }
78  spin_unlock_irqrestore(&priv->lock, flags);
79 
80  /* execute any pending alarm handlers */
81  list_for_each_entry_safe(alarm, atemp, &exec, head) {
82  list_del(&alarm->head);
83  alarm->func(alarm);
84  }
85 }
86 
87 static void
88 nv04_timer_alarm(struct nouveau_timer *ptimer, u64 time,
89  struct nouveau_alarm *alarm)
90 {
91  struct nv04_timer_priv *priv = (void *)ptimer;
92  struct nouveau_alarm *list;
93  unsigned long flags;
94 
95  alarm->timestamp = ptimer->read(ptimer) + time;
96 
97  /* append new alarm to list, in soonest-alarm-first order */
98  spin_lock_irqsave(&priv->lock, flags);
99  list_for_each_entry(list, &priv->alarms, head) {
100  if (list->timestamp > alarm->timestamp)
101  break;
102  }
103  list_add_tail(&alarm->head, &list->head);
104  spin_unlock_irqrestore(&priv->lock, flags);
105 
106  /* process pending alarms */
107  nv04_timer_alarm_trigger(ptimer);
108 }
109 
110 static void
111 nv04_timer_intr(struct nouveau_subdev *subdev)
112 {
113  struct nv04_timer_priv *priv = (void *)subdev;
114  u32 stat = nv_rd32(priv, NV04_PTIMER_INTR_0);
115 
116  if (stat & 0x00000001) {
117  nv04_timer_alarm_trigger(&priv->base);
118  nv_wr32(priv, NV04_PTIMER_INTR_0, 0x00000001);
119  stat &= ~0x00000001;
120  }
121 
122  if (stat) {
123  nv_error(priv, "unknown stat 0x%08x\n", stat);
124  nv_wr32(priv, NV04_PTIMER_INTR_0, stat);
125  }
126 }
127 
128 static int
129 nv04_timer_ctor(struct nouveau_object *parent, struct nouveau_object *engine,
130  struct nouveau_oclass *oclass, void *data, u32 size,
131  struct nouveau_object **pobject)
132 {
133  struct nv04_timer_priv *priv;
134  int ret;
135 
136  ret = nouveau_timer_create(parent, engine, oclass, &priv);
137  *pobject = nv_object(priv);
138  if (ret)
139  return ret;
140 
141  priv->base.base.intr = nv04_timer_intr;
142  priv->base.read = nv04_timer_read;
143  priv->base.alarm = nv04_timer_alarm;
144 
145  INIT_LIST_HEAD(&priv->alarms);
146  spin_lock_init(&priv->lock);
147  return 0;
148 }
149 
150 static void
151 nv04_timer_dtor(struct nouveau_object *object)
152 {
153  struct nv04_timer_priv *priv = (void *)object;
154  return nouveau_timer_destroy(&priv->base);
155 }
156 
157 static int
158 nv04_timer_init(struct nouveau_object *object)
159 {
160  struct nouveau_device *device = nv_device(object);
161  struct nv04_timer_priv *priv = (void *)object;
162  u32 m = 1, f, n, d;
163  int ret;
164 
165  ret = nouveau_timer_init(&priv->base);
166  if (ret)
167  return ret;
168 
169  /* aim for 31.25MHz, which gives us nanosecond timestamps */
170  d = 1000000 / 32;
171 
172  /* determine base clock for timer source */
173 #if 0 /*XXX*/
174  if (device->chipset < 0x40) {
175  n = nouveau_hw_get_clock(device, PLL_CORE);
176  } else
177 #endif
178  if (device->chipset <= 0x40) {
179  /*XXX: figure this out */
180  f = -1;
181  n = 0;
182  } else {
183  f = device->crystal;
184  n = f;
185  while (n < (d * 2)) {
186  n += (n / m);
187  m++;
188  }
189 
190  nv_wr32(priv, 0x009220, m - 1);
191  }
192 
193  if (!n) {
194  nv_warn(priv, "unknown input clock freq\n");
195  if (!nv_rd32(priv, NV04_PTIMER_NUMERATOR) ||
196  !nv_rd32(priv, NV04_PTIMER_DENOMINATOR)) {
197  nv_wr32(priv, NV04_PTIMER_NUMERATOR, 1);
198  nv_wr32(priv, NV04_PTIMER_DENOMINATOR, 1);
199  }
200  return 0;
201  }
202 
203  /* reduce ratio to acceptable values */
204  while (((n % 5) == 0) && ((d % 5) == 0)) {
205  n /= 5;
206  d /= 5;
207  }
208 
209  while (((n % 2) == 0) && ((d % 2) == 0)) {
210  n /= 2;
211  d /= 2;
212  }
213 
214  while (n > 0xffff || d > 0xffff) {
215  n >>= 1;
216  d >>= 1;
217  }
218 
219  nv_debug(priv, "input frequency : %dHz\n", f);
220  nv_debug(priv, "input multiplier: %d\n", m);
221  nv_debug(priv, "numerator : 0x%08x\n", n);
222  nv_debug(priv, "denominator : 0x%08x\n", d);
223  nv_debug(priv, "timer frequency : %dHz\n", (f * m) * d / n);
224 
225  nv_wr32(priv, NV04_PTIMER_NUMERATOR, n);
226  nv_wr32(priv, NV04_PTIMER_DENOMINATOR, d);
227  nv_wr32(priv, NV04_PTIMER_INTR_0, 0xffffffff);
228  nv_wr32(priv, NV04_PTIMER_INTR_EN_0, 0x00000000);
229  return 0;
230 }
231 
232 static int
233 nv04_timer_fini(struct nouveau_object *object, bool suspend)
234 {
235  struct nv04_timer_priv *priv = (void *)object;
236  nv_wr32(priv, NV04_PTIMER_INTR_EN_0, 0x00000000);
237  return nouveau_timer_fini(&priv->base, suspend);
238 }
239 
240 struct nouveau_oclass
242  .handle = NV_SUBDEV(TIMER, 0x04),
243  .ofuncs = &(struct nouveau_ofuncs) {
244  .ctor = nv04_timer_ctor,
245  .dtor = nv04_timer_dtor,
246  .init = nv04_timer_init,
247  .fini = nv04_timer_fini,
248  }
249 };