Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * Copyright 2012 Red Hat Inc.
0003  *
0004  * Permission is hereby granted, free of charge, to any person obtaining a
0005  * copy of this software and associated documentation files (the "Software"),
0006  * to deal in the Software without restriction, including without limitation
0007  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
0008  * and/or sell copies of the Software, and to permit persons to whom the
0009  * Software is furnished to do so, subject to the following conditions:
0010  *
0011  * The above copyright notice and this permission notice shall be included in
0012  * all copies or substantial portions of the Software.
0013  *
0014  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
0015  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
0016  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
0017  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
0018  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
0019  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
0020  * OTHER DEALINGS IN THE SOFTWARE.
0021  *
0022  * Authors: Ben Skeggs
0023  */
0024 #include "priv.h"
0025 #include "regsnv04.h"
0026 
0027 static void
0028 nv41_timer_init(struct nvkm_timer *tmr)
0029 {
0030     struct nvkm_subdev *subdev = &tmr->subdev;
0031     struct nvkm_device *device = subdev->device;
0032     u32 f = device->crystal;
0033     u32 m = 1, n, d;
0034 
0035     /* aim for 31.25MHz, which gives us nanosecond timestamps */
0036     d = 1000000 / 32;
0037     n = f;
0038 
0039     while (n < (d * 2)) {
0040         n += (n / m);
0041         m++;
0042     }
0043 
0044     /* reduce ratio to acceptable values */
0045     while (((n % 5) == 0) && ((d % 5) == 0)) {
0046         n /= 5;
0047         d /= 5;
0048     }
0049 
0050     while (((n % 2) == 0) && ((d % 2) == 0)) {
0051         n /= 2;
0052         d /= 2;
0053     }
0054 
0055     while (n > 0xffff || d > 0xffff) {
0056         n >>= 1;
0057         d >>= 1;
0058     }
0059 
0060     nvkm_debug(subdev, "input frequency : %dHz\n", f);
0061     nvkm_debug(subdev, "input multiplier: %d\n", m);
0062     nvkm_debug(subdev, "numerator       : %08x\n", n);
0063     nvkm_debug(subdev, "denominator     : %08x\n", d);
0064     nvkm_debug(subdev, "timer frequency : %dHz\n", (f * m) * d / n);
0065 
0066     nvkm_wr32(device, 0x009220, m - 1);
0067     nvkm_wr32(device, NV04_PTIMER_NUMERATOR, n);
0068     nvkm_wr32(device, NV04_PTIMER_DENOMINATOR, d);
0069 }
0070 
0071 static const struct nvkm_timer_func
0072 nv41_timer = {
0073     .init = nv41_timer_init,
0074     .intr = nv04_timer_intr,
0075     .read = nv04_timer_read,
0076     .time = nv04_timer_time,
0077     .alarm_init = nv04_timer_alarm_init,
0078     .alarm_fini = nv04_timer_alarm_fini,
0079 };
0080 
0081 int
0082 nv41_timer_new(struct nvkm_device *device, enum nvkm_subdev_type type, int inst,
0083            struct nvkm_timer **ptmr)
0084 {
0085     return nvkm_timer_new_(&nv41_timer, device, type, inst, ptmr);
0086 }