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.
25 #include <subdev/timer.h>
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
35 struct nv04_timer_priv {
36 struct nouveau_timer base;
37 struct list_head alarms;
42 nv04_timer_read(struct nouveau_timer *ptimer)
44 struct nv04_timer_priv *priv = (void *)ptimer;
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));
52 return ((u64)hi << 32 | lo);
56 nv04_timer_alarm_trigger(struct nouveau_timer *ptimer)
58 struct nv04_timer_priv *priv = (void *)ptimer;
59 struct nouveau_alarm *alarm, *atemp;
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);
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);
76 nv_wr32(priv, NV04_PTIMER_INTR_EN_0, 0x00000000);
78 spin_unlock_irqrestore(&priv->lock, flags);
80 /* execute any pending alarm handlers */
81 list_for_each_entry_safe(alarm, atemp, &exec, head) {
82 list_del_init(&alarm->head);
88 nv04_timer_alarm(struct nouveau_timer *ptimer, u64 time,
89 struct nouveau_alarm *alarm)
91 struct nv04_timer_priv *priv = (void *)ptimer;
92 struct nouveau_alarm *list;
95 alarm->timestamp = ptimer->read(ptimer) + time;
97 /* append new alarm to list, in soonest-alarm-first order */
98 spin_lock_irqsave(&priv->lock, flags);
100 if (!list_empty(&alarm->head))
101 list_del(&alarm->head);
103 list_for_each_entry(list, &priv->alarms, head) {
104 if (list->timestamp > alarm->timestamp)
107 list_add_tail(&alarm->head, &list->head);
109 spin_unlock_irqrestore(&priv->lock, flags);
111 /* process pending alarms */
112 nv04_timer_alarm_trigger(ptimer);
116 nv04_timer_intr(struct nouveau_subdev *subdev)
118 struct nv04_timer_priv *priv = (void *)subdev;
119 u32 stat = nv_rd32(priv, NV04_PTIMER_INTR_0);
121 if (stat & 0x00000001) {
122 nv04_timer_alarm_trigger(&priv->base);
123 nv_wr32(priv, NV04_PTIMER_INTR_0, 0x00000001);
128 nv_error(priv, "unknown stat 0x%08x\n", stat);
129 nv_wr32(priv, NV04_PTIMER_INTR_0, stat);
134 nv04_timer_ctor(struct nouveau_object *parent, struct nouveau_object *engine,
135 struct nouveau_oclass *oclass, void *data, u32 size,
136 struct nouveau_object **pobject)
138 struct nv04_timer_priv *priv;
141 ret = nouveau_timer_create(parent, engine, oclass, &priv);
142 *pobject = nv_object(priv);
146 priv->base.base.intr = nv04_timer_intr;
147 priv->base.read = nv04_timer_read;
148 priv->base.alarm = nv04_timer_alarm;
150 INIT_LIST_HEAD(&priv->alarms);
151 spin_lock_init(&priv->lock);
156 nv04_timer_dtor(struct nouveau_object *object)
158 struct nv04_timer_priv *priv = (void *)object;
159 return nouveau_timer_destroy(&priv->base);
163 nv04_timer_init(struct nouveau_object *object)
165 struct nouveau_device *device = nv_device(object);
166 struct nv04_timer_priv *priv = (void *)object;
170 ret = nouveau_timer_init(&priv->base);
174 /* aim for 31.25MHz, which gives us nanosecond timestamps */
177 /* determine base clock for timer source */
179 if (device->chipset < 0x40) {
180 n = nouveau_hw_get_clock(device, PLL_CORE);
183 if (device->chipset <= 0x40) {
184 /*XXX: figure this out */
190 while (n < (d * 2)) {
195 nv_wr32(priv, 0x009220, m - 1);
199 nv_warn(priv, "unknown input clock freq\n");
200 if (!nv_rd32(priv, NV04_PTIMER_NUMERATOR) ||
201 !nv_rd32(priv, NV04_PTIMER_DENOMINATOR)) {
202 nv_wr32(priv, NV04_PTIMER_NUMERATOR, 1);
203 nv_wr32(priv, NV04_PTIMER_DENOMINATOR, 1);
208 /* reduce ratio to acceptable values */
209 while (((n % 5) == 0) && ((d % 5) == 0)) {
214 while (((n % 2) == 0) && ((d % 2) == 0)) {
219 while (n > 0xffff || d > 0xffff) {
224 nv_debug(priv, "input frequency : %dHz\n", f);
225 nv_debug(priv, "input multiplier: %d\n", m);
226 nv_debug(priv, "numerator : 0x%08x\n", n);
227 nv_debug(priv, "denominator : 0x%08x\n", d);
228 nv_debug(priv, "timer frequency : %dHz\n", (f * m) * d / n);
230 nv_wr32(priv, NV04_PTIMER_NUMERATOR, n);
231 nv_wr32(priv, NV04_PTIMER_DENOMINATOR, d);
232 nv_wr32(priv, NV04_PTIMER_INTR_0, 0xffffffff);
233 nv_wr32(priv, NV04_PTIMER_INTR_EN_0, 0x00000000);
238 nv04_timer_fini(struct nouveau_object *object, bool suspend)
240 struct nv04_timer_priv *priv = (void *)object;
241 nv_wr32(priv, NV04_PTIMER_INTR_EN_0, 0x00000000);
242 return nouveau_timer_fini(&priv->base, suspend);
245 struct nouveau_oclass
246 nv04_timer_oclass = {
247 .handle = NV_SUBDEV(TIMER, 0x04),
248 .ofuncs = &(struct nouveau_ofuncs) {
249 .ctor = nv04_timer_ctor,
250 .dtor = nv04_timer_dtor,
251 .init = nv04_timer_init,
252 .fini = nv04_timer_fini,