2 * Copyright 2012 Red Hat Inc.
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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.
28 nv04_timer_read(struct nouveau_timer *ptimer)
30 struct nv04_timer_priv *priv = (void *)ptimer;
34 hi = nv_rd32(priv, NV04_PTIMER_TIME_1);
35 lo = nv_rd32(priv, NV04_PTIMER_TIME_0);
36 } while (hi != nv_rd32(priv, NV04_PTIMER_TIME_1));
38 return ((u64)hi << 32 | lo);
42 nv04_timer_alarm_trigger(struct nouveau_timer *ptimer)
44 struct nv04_timer_priv *priv = (void *)ptimer;
45 struct nouveau_alarm *alarm, *atemp;
49 /* move any due alarms off the pending list */
50 spin_lock_irqsave(&priv->lock, flags);
51 list_for_each_entry_safe(alarm, atemp, &priv->alarms, head) {
52 if (alarm->timestamp <= ptimer->read(ptimer))
53 list_move_tail(&alarm->head, &exec);
56 /* reschedule interrupt for next alarm time */
57 if (!list_empty(&priv->alarms)) {
58 alarm = list_first_entry(&priv->alarms, typeof(*alarm), head);
59 nv_wr32(priv, NV04_PTIMER_ALARM_0, alarm->timestamp);
60 nv_wr32(priv, NV04_PTIMER_INTR_EN_0, 0x00000001);
62 nv_wr32(priv, NV04_PTIMER_INTR_EN_0, 0x00000000);
64 spin_unlock_irqrestore(&priv->lock, flags);
66 /* execute any pending alarm handlers */
67 list_for_each_entry_safe(alarm, atemp, &exec, head) {
68 list_del_init(&alarm->head);
74 nv04_timer_alarm(struct nouveau_timer *ptimer, u64 time,
75 struct nouveau_alarm *alarm)
77 struct nv04_timer_priv *priv = (void *)ptimer;
78 struct nouveau_alarm *list;
81 alarm->timestamp = ptimer->read(ptimer) + time;
83 /* append new alarm to list, in soonest-alarm-first order */
84 spin_lock_irqsave(&priv->lock, flags);
86 if (!list_empty(&alarm->head))
87 list_del(&alarm->head);
89 list_for_each_entry(list, &priv->alarms, head) {
90 if (list->timestamp > alarm->timestamp)
93 list_add_tail(&alarm->head, &list->head);
95 spin_unlock_irqrestore(&priv->lock, flags);
97 /* process pending alarms */
98 nv04_timer_alarm_trigger(ptimer);
102 nv04_timer_alarm_cancel(struct nouveau_timer *ptimer,
103 struct nouveau_alarm *alarm)
105 struct nv04_timer_priv *priv = (void *)ptimer;
107 spin_lock_irqsave(&priv->lock, flags);
108 list_del_init(&alarm->head);
109 spin_unlock_irqrestore(&priv->lock, flags);
113 nv04_timer_intr(struct nouveau_subdev *subdev)
115 struct nv04_timer_priv *priv = (void *)subdev;
116 u32 stat = nv_rd32(priv, NV04_PTIMER_INTR_0);
118 if (stat & 0x00000001) {
119 nv04_timer_alarm_trigger(&priv->base);
120 nv_wr32(priv, NV04_PTIMER_INTR_0, 0x00000001);
125 nv_error(priv, "unknown stat 0x%08x\n", stat);
126 nv_wr32(priv, NV04_PTIMER_INTR_0, stat);
131 nv04_timer_fini(struct nouveau_object *object, bool suspend)
133 struct nv04_timer_priv *priv = (void *)object;
135 priv->suspend_time = nv04_timer_read(&priv->base);
136 nv_wr32(priv, NV04_PTIMER_INTR_EN_0, 0x00000000);
137 return nouveau_timer_fini(&priv->base, suspend);
141 nv04_timer_init(struct nouveau_object *object)
143 struct nouveau_device *device = nv_device(object);
144 struct nv04_timer_priv *priv = (void *)object;
145 u32 m = 1, f, n, d, lo, hi;
148 ret = nouveau_timer_init(&priv->base);
152 /* aim for 31.25MHz, which gives us nanosecond timestamps */
155 /* determine base clock for timer source */
157 if (device->chipset < 0x40) {
158 n = nouveau_hw_get_clock(device, PLL_CORE);
161 if (device->chipset <= 0x40) {
162 /*XXX: figure this out */
168 while (n < (d * 2)) {
173 nv_wr32(priv, 0x009220, m - 1);
177 nv_warn(priv, "unknown input clock freq\n");
178 if (!nv_rd32(priv, NV04_PTIMER_NUMERATOR) ||
179 !nv_rd32(priv, NV04_PTIMER_DENOMINATOR)) {
180 nv_wr32(priv, NV04_PTIMER_NUMERATOR, 1);
181 nv_wr32(priv, NV04_PTIMER_DENOMINATOR, 1);
186 /* reduce ratio to acceptable values */
187 while (((n % 5) == 0) && ((d % 5) == 0)) {
192 while (((n % 2) == 0) && ((d % 2) == 0)) {
197 while (n > 0xffff || d > 0xffff) {
202 /* restore the time before suspend */
203 lo = priv->suspend_time;
204 hi = (priv->suspend_time >> 32);
206 nv_debug(priv, "input frequency : %dHz\n", f);
207 nv_debug(priv, "input multiplier: %d\n", m);
208 nv_debug(priv, "numerator : 0x%08x\n", n);
209 nv_debug(priv, "denominator : 0x%08x\n", d);
210 nv_debug(priv, "timer frequency : %dHz\n", (f * m) * d / n);
211 nv_debug(priv, "time low : 0x%08x\n", lo);
212 nv_debug(priv, "time high : 0x%08x\n", hi);
214 nv_wr32(priv, NV04_PTIMER_NUMERATOR, n);
215 nv_wr32(priv, NV04_PTIMER_DENOMINATOR, d);
216 nv_wr32(priv, NV04_PTIMER_INTR_0, 0xffffffff);
217 nv_wr32(priv, NV04_PTIMER_INTR_EN_0, 0x00000000);
218 nv_wr32(priv, NV04_PTIMER_TIME_1, hi);
219 nv_wr32(priv, NV04_PTIMER_TIME_0, lo);
225 nv04_timer_dtor(struct nouveau_object *object)
227 struct nv04_timer_priv *priv = (void *)object;
228 return nouveau_timer_destroy(&priv->base);
232 nv04_timer_ctor(struct nouveau_object *parent, struct nouveau_object *engine,
233 struct nouveau_oclass *oclass, void *data, u32 size,
234 struct nouveau_object **pobject)
236 struct nv04_timer_priv *priv;
239 ret = nouveau_timer_create(parent, engine, oclass, &priv);
240 *pobject = nv_object(priv);
244 priv->base.base.intr = nv04_timer_intr;
245 priv->base.read = nv04_timer_read;
246 priv->base.alarm = nv04_timer_alarm;
247 priv->base.alarm_cancel = nv04_timer_alarm_cancel;
248 priv->suspend_time = 0;
250 INIT_LIST_HEAD(&priv->alarms);
251 spin_lock_init(&priv->lock);
255 struct nouveau_oclass
256 nv04_timer_oclass = {
257 .handle = NV_SUBDEV(TIMER, 0x04),
258 .ofuncs = &(struct nouveau_ofuncs) {
259 .ctor = nv04_timer_ctor,
260 .dtor = nv04_timer_dtor,
261 .init = nv04_timer_init,
262 .fini = nv04_timer_fini,