1/* Header: //Mercury/Projects/archives/XFree86/4.0/smi_dac.c-arc 1.8 27 Nov 2000 15:47:08 Frido $ */ 2 3/* 4Copyright (C) 1994-1998 The XFree86 Project, Inc. All Rights Reserved. 5Copyright (C) 2000 Silicon Motion, Inc. All Rights Reserved. 6 7Permission is hereby granted, free of charge, to any person obtaining a copy of 8this software and associated documentation files (the "Software"), to deal in 9the Software without restriction, including without limitation the rights to 10use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 11of the Software, and to permit persons to whom the Software is furnished to do 12so, subject to the following conditions: 13 14The above copyright notice and this permission notice shall be included in all 15copies or substantial portions of the Software. 16 17THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FIT- 19NESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20XFREE86 PROJECT BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 21AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 22WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 23 24Except as contained in this notice, the names of the XFree86 Project and 25Silicon Motion shall not be used in advertising or otherwise to promote the 26sale, use or other dealings in this Software without prior written 27authorization from the XFree86 Project and Silicon Motion. 28*/ 29 30#ifdef HAVE_CONFIG_H 31#include "config.h" 32#endif 33 34#include "smi.h" 35 36#define BASE_FREQ 14.31818 /* MHz */ 37 38void 39SMI_CommonCalcClock(int scrnIndex, long freq, int min_m, int min_n1, 40 int max_n1, int min_n2, int max_n2, long freq_min, 41 long freq_max, unsigned char *mdiv, unsigned char *ndiv) 42{ 43 ScrnInfoPtr pScrn = xf86Screens[scrnIndex]; 44 SMIPtr pSmi = SMIPTR(pScrn); 45 double div, diff, best_diff; 46 unsigned int m; 47 unsigned char n1, n2; 48 unsigned char best_n1 = 63, best_n2 = 3, best_m = 255; 49 50 double ffreq = freq / 1000.0 / BASE_FREQ; 51 double ffreq_min = freq_min / 1000.0 / BASE_FREQ; 52 double ffreq_max = freq_max / 1000.0 / BASE_FREQ; 53 54 if (ffreq < ffreq_min / (1 << max_n2)) { 55 xf86DrvMsg(scrnIndex,X_WARNING,"invalid frequency %1.3f MHz [freq >= %1.3f MHz]\n", 56 ffreq * BASE_FREQ, ffreq_min * BASE_FREQ / (1 << max_n2)); 57 ffreq = ffreq_min / (1 << max_n2); 58 } 59 if (ffreq > ffreq_max / (1 << min_n2)) { 60 xf86DrvMsg(scrnIndex,X_WARNING,"invalid frequency %1.3f MHz [freq <= %1.3f MHz]\n", 61 ffreq * BASE_FREQ, ffreq_max * BASE_FREQ / (1 << min_n2)); 62 ffreq = ffreq_max / (1 << min_n2); 63 } 64 65 /* work out suitable timings */ 66 best_diff = ffreq; 67 68 for (n2 = min_n2; n2 <= max_n2; n2++) { 69 for (n1 = min_n1; n1 <= max_n1; n1++) { 70 m = (int)(ffreq * n1 * (1 << n2) + 0.5); 71 if ( (m < min_m) || (m > 255) ) { 72 continue; 73 } 74 div = (double)(m) / (double)(n1); 75 if ( (div >= ffreq_min) && (div <= ffreq_max) ) { 76 diff = ffreq - div / (1 << n2); 77 if (diff < 0.0) { 78 diff = -diff; 79 } 80 if (diff < best_diff) { 81 best_diff = diff; 82 best_m = m; 83 best_n1 = n1; 84 best_n2 = n2; 85 } 86 } 87 } 88 } 89 90 DEBUG("Clock parameters for %1.6f MHz: m=%d, n1=%d, n2=%d\n", 91 ((double)(best_m) / (double)(best_n1) / (1 << best_n2)) * BASE_FREQ, 92 best_m, best_n1, best_n2); 93 94 if (SMI_LYNX_SERIES(pSmi->Chipset)) { 95 /* Prefer post scalar enabled for even denominators */ 96 if (freq < 70000 && max_n2 > 0 && 97 best_n2 == 0 && best_n1 % 2 == 0){ 98 best_n1 >>= 1; 99 best_n2 = 1; 100 } 101 102 *ndiv = best_n1 | 103 (best_n2 & 0x1) << 7 | 104 (best_n2 & 0x2) >> 1 <<6; 105 } else { 106 *ndiv = best_n1 | (best_n2 << 7); 107 108 /* Enable second VCO */ 109 if (freq > 120000) 110 *ndiv |= 1 << 6; 111 } 112 113 *mdiv = best_m; 114} 115 116