| 1 | /* $NetBSD: nouveau_subdev_clock_pllnva3.c,v 1.1.1.1 2014/08/06 12:36:29 riastradh Exp $ */ |
| 2 | |
| 3 | /* |
| 4 | * Copyright 2010 Red Hat Inc. |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 7 | * copy of this software and associated documentation files (the "Software"), |
| 8 | * to deal in the Software without restriction, including without limitation |
| 9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 10 | * and/or sell copies of the Software, and to permit persons to whom the |
| 11 | * Software is furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in |
| 14 | * all copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 19 | * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR |
| 20 | * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
| 21 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 22 | * OTHER DEALINGS IN THE SOFTWARE. |
| 23 | * |
| 24 | * Authors: Ben Skeggs |
| 25 | */ |
| 26 | |
| 27 | #include <sys/cdefs.h> |
| 28 | __KERNEL_RCSID(0, "$NetBSD: nouveau_subdev_clock_pllnva3.c,v 1.1.1.1 2014/08/06 12:36:29 riastradh Exp $" ); |
| 29 | |
| 30 | #include <subdev/clock.h> |
| 31 | #include <subdev/bios.h> |
| 32 | #include <subdev/bios/pll.h> |
| 33 | |
| 34 | #include "pll.h" |
| 35 | |
| 36 | int |
| 37 | nva3_pll_calc(struct nouveau_subdev *subdev, struct nvbios_pll *info, |
| 38 | u32 freq, int *pN, int *pfN, int *pM, int *P) |
| 39 | { |
| 40 | u32 best_err = ~0, err; |
| 41 | int M, lM, hM, N, fN; |
| 42 | |
| 43 | *P = info->vco1.max_freq / freq; |
| 44 | if (*P > info->max_p) |
| 45 | *P = info->max_p; |
| 46 | if (*P < info->min_p) |
| 47 | *P = info->min_p; |
| 48 | |
| 49 | lM = (info->refclk + info->vco1.max_inputfreq) / info->vco1.max_inputfreq; |
| 50 | lM = max(lM, (int)info->vco1.min_m); |
| 51 | hM = (info->refclk + info->vco1.min_inputfreq) / info->vco1.min_inputfreq; |
| 52 | hM = min(hM, (int)info->vco1.max_m); |
| 53 | lM = min(lM, hM); |
| 54 | |
| 55 | for (M = lM; M <= hM; M++) { |
| 56 | u32 tmp = freq * *P * M; |
| 57 | N = tmp / info->refclk; |
| 58 | fN = tmp % info->refclk; |
| 59 | |
| 60 | if (!pfN) { |
| 61 | if (fN >= info->refclk / 2) |
| 62 | N++; |
| 63 | } else { |
| 64 | if (fN < info->refclk / 2) |
| 65 | N--; |
| 66 | fN = tmp - (N * info->refclk); |
| 67 | } |
| 68 | |
| 69 | if (N < info->vco1.min_n) |
| 70 | continue; |
| 71 | if (N > info->vco1.max_n) |
| 72 | break; |
| 73 | |
| 74 | err = abs(freq - (info->refclk * N / M / *P)); |
| 75 | if (err < best_err) { |
| 76 | best_err = err; |
| 77 | *pN = N; |
| 78 | *pM = M; |
| 79 | } |
| 80 | |
| 81 | if (pfN) { |
| 82 | *pfN = ((fN << 13) + info->refclk / 2) / info->refclk; |
| 83 | *pfN = (*pfN - 4096) & 0xffff; |
| 84 | return freq; |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | if (unlikely(best_err == ~0)) { |
| 89 | nv_error(subdev, "unable to find matching pll values\n" ); |
| 90 | return -EINVAL; |
| 91 | } |
| 92 | |
| 93 | return info->refclk * *pN / *pM / *P; |
| 94 | } |
| 95 | |