1 /* $NetBSD: nouveau_nvkm_subdev_clk_pllgt215.c,v 1.3 2021/12/18 23:45:39 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 #include <sys/cdefs.h> 27 __KERNEL_RCSID(0, "$NetBSD: nouveau_nvkm_subdev_clk_pllgt215.c,v 1.3 2021/12/18 23:45:39 riastradh Exp $"); 28 29 #include "pll.h" 30 31 #include <subdev/bios.h> 32 #include <subdev/bios/pll.h> 33 34 int 35 gt215_pll_calc(struct nvkm_subdev *subdev, struct nvbios_pll *info, 36 u32 freq, int *pN, int *pfN, int *pM, int *P) 37 { 38 u32 best_err = ~0, err; 39 int M, lM, hM, N, fN; 40 41 *P = info->vco1.max_freq / freq; 42 if (*P > info->max_p) 43 *P = info->max_p; 44 if (*P < info->min_p) 45 *P = info->min_p; 46 47 lM = (info->refclk + info->vco1.max_inputfreq) / info->vco1.max_inputfreq; 48 lM = max(lM, (int)info->vco1.min_m); 49 hM = (info->refclk + info->vco1.min_inputfreq) / info->vco1.min_inputfreq; 50 hM = min(hM, (int)info->vco1.max_m); 51 lM = min(lM, hM); 52 53 for (M = lM; M <= hM; M++) { 54 u32 tmp = freq * *P * M; 55 N = tmp / info->refclk; 56 fN = tmp % info->refclk; 57 58 if (!pfN) { 59 if (fN >= info->refclk / 2) 60 N++; 61 } else { 62 if (fN < info->refclk / 2) 63 N--; 64 fN = tmp - (N * info->refclk); 65 } 66 67 if (N < info->vco1.min_n) 68 continue; 69 if (N > info->vco1.max_n) 70 break; 71 72 err = abs(freq - (info->refclk * N / M / *P)); 73 if (err < best_err) { 74 best_err = err; 75 *pN = N; 76 *pM = M; 77 } 78 79 if (pfN) { 80 *pfN = ((fN << 13) + info->refclk / 2) / info->refclk; 81 *pfN = (*pfN - 4096) & 0xffff; 82 return freq; 83 } 84 } 85 86 if (unlikely(best_err == ~0)) { 87 nvkm_error(subdev, "unable to find matching pll values\n"); 88 return -EINVAL; 89 } 90 91 return info->refclk * *pN / *pM / *P; 92 } 93