aticlock.c 53.7 KB
Newer Older
1
/* $XFree86$ */
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
2
/*
3
 * Copyright 1997 through 2004 by Marc Aurele La France (TSI @ UQV), tsi@xfree86.org
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501
 *
 * Permission to use, copy, modify, distribute, and sell this software and its
 * documentation for any purpose is hereby granted without fee, provided that
 * the above copyright notice appear in all copies and that both that copyright
 * notice and this permission notice appear in supporting documentation, and
 * that the name of Marc Aurele La France not be used in advertising or
 * publicity pertaining to distribution of the software without specific,
 * written prior permission.  Marc Aurele La France makes no representations
 * about the suitability of this software for any purpose.  It is provided
 * "as-is" without express or implied warranty.
 *
 * MARC AURELE LA FRANCE DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.  IN NO
 * EVENT SHALL MARC AURELE LA FRANCE BE LIABLE FOR ANY SPECIAL, INDIRECT OR
 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
 * PERFORMANCE OF THIS SOFTWARE.
 */

/*
 * Adapters prior to V5 use 4 crystals.  Adapters V5 and later use a clock
 * generator chip.  V3 and V4 adapters differ when it comes to choosing clock
 * frequencies.
 *
 * VGA Wonder V3/V4 Adapter Clock Frequencies
 * R E G I S T E R S
 * 1CE(*)    3C2     3C2    Frequency
 * B2h/BEh
 * Bit 6/4  Bit 3   Bit 2   (MHz)
 * ------- ------- -------  -------
 *    0       0       0     50.000
 *    0       0       1     56.644
 *    0       1       0     Spare 1
 *    0       1       1     44.900
 *    1       0       0     44.900
 *    1       0       1     50.000
 *    1       1       0     Spare 2
 *    1       1       1     36.000
 *
 * (*):  V3 uses index B2h, bit 6;  V4 uses index BEh, bit 4
 *
 * V5, PLUS, XL and XL24 usually have an ATI 18810 clock generator chip, but
 * some have an ATI 18811-0, and it's quite conceivable that some exist with
 * ATI 18811-1's or ATI 18811-2's.  Mach32 adapters are known to use any one of
 * these clock generators.  Mach32 adapters also use a different dot clock
 * ordering.  ATI says there is no reliable way for the driver to determine
 * which clock generator is on the adapter, but this driver will do its best to
 * do so anyway.
 *
 * VGA Wonder V5/PLUS/XL/XL24 Clock Frequencies
 * R E G I S T E R S
 *   1CE     1CE     3C2     3C2    Frequency
 *   B9h     BEh                     (MHz)   18811-0  18811-1
 *  Bit 1   Bit 4   Bit 3   Bit 2    18810   18812-0  18811-2  (*5)
 * ------- ------- ------- -------  -------  -------  -------  -------
 *    0       0       0       0      30.240   30.240  135.000   75.000
 *    0       0       0       1      32.000   32.000   32.000   77.500
 *    0       0       1       0      37.500  110.000  110.000   80.000
 *    0       0       1       1      39.000   80.000   80.000   90.000
 *    0       1       0       0      42.954   42.954  100.000   25.175
 *    0       1       0       1      48.771   48.771  126.000   28.322
 *    0       1       1       0        (*1)   92.400   92.400   31.500
 *    0       1       1       1      36.000   36.000   36.000   36.000
 *    1       0       0       0      40.000   39.910   39.910  100.000
 *    1       0       0       1        (*4)   44.900   44.900  110.000
 *    1       0       1       0      75.000   75.000   75.000  126.000
 *    1       0       1       1      65.000   65.000   65.000  135.000
 *    1       1       0       0      50.350   50.350   50.350   40.000
 *    1       1       0       1      56.640   56.640   56.640   44.900
 *    1       1       1       0        (*2)     (*3)     (*3)   50.000
 *    1       1       1       1      44.900   44.900   44.900   65.000
 *
 * (*1) External 0 (supposedly 16.657 Mhz)
 * (*2) External 1 (supposedly 28.322 MHz)
 * (*3) This setting doesn't seem to generate anything
 * (*4) This setting is documented to be 56.644 MHz, but something close to 82
 *      MHz has also been encountered.
 * (*5) This setting is for Dell OmniPlex 590 systems, with a 68800AX on the
 *      motherboard, along with an AT&T21C498 DAC (which is reported as an
 *      STG1700) and ICS2494AM clock generator (a.k.a. ATI 18811-?).
 *
 * Mach32 Clock Frequencies
 * R E G I S T E R S
 *   1CE     1CE     3C2     3C2    Frequency
 *   B9h     BEh                     (MHz)   18811-0  18811-1
 *  Bit 1   Bit 4   Bit 3   Bit 2    18810   18812-0  18811-2  (*5)
 * ------- ------- ------- -------  -------  -------  -------  -------
 *    0       0       0       0      42.954   42.954  100.000   25.175
 *    0       0       0       1      48.771   48.771  126.000   28.322
 *    0       0       1       0        (*1)   92.400   92.400   31.500
 *    0       0       1       1      36.000   36.000   36.000   36.000
 *    0       1       0       0      30.240   30.240  135.000   75.000
 *    0       1       0       1      32.000   32.000   32.000   77.500
 *    0       1       1       0      37.500  110.000  110.000   80.000
 *    0       1       1       1      39.000   80.000   80.000   90.000
 *    1       0       0       0      50.350   50.350   50.350   40.000
 *    1       0       0       1      56.640   56.640   56.640   44.900
 *    1       0       1       0        (*2)     (*3)     (*3)   50.000
 *    1       0       1       1      44.900   44.900   44.900   65.000
 *    1       1       0       0      40.000   39.910   39.910  100.000
 *    1       1       0       1        (*4)   44.900   44.900  110.000
 *    1       1       1       0      75.000   75.000   75.000  126.000
 *    1       1       1       1      65.000   65.000   65.000  135.000
 *
 * (*1) External 0 (supposedly 16.657 Mhz)
 * (*2) External 1 (supposedly 28.322 MHz)
 * (*3) This setting doesn't seem to generate anything
 * (*4) This setting is documented to be 56.644 MHz, but something close to 82
 *      MHz has also been encountered.
 * (*5) This setting is for Dell OmniPlex 590 systems, with a 68800AX on the
 *      motherboard, along with an AT&T21C498 DAC (which is reported as an
 *      STG1700) and ICS2494AM clock generator (a.k.a. ATI 18811-?).
 *
 * Note that, to reduce confusion, this driver masks out the different clock
 * ordering.
 *
 * For all adapters, these frequencies can be divided by 1 or 2.  For all
 * adapters, except Mach32's and Mach64's, frequencies can also be divided by 3
 * or 4.
 *
 *      Register 1CE, index B8h
 *       Bit 7    Bit 6
 *      -------  -------
 *         0        0           Divide by 1
 *         0        1           Divide by 2
 *         1        0           Divide by 3
 *         1        1           Divide by 4
 *
 * With respect to clocks, Mach64's are entirely different animals.
 *
 * The oldest Mach64's use one of the non-programmable clock generators
 * described above.  In this case, the driver will handle clocks in much the
 * same way as it would for a Mach32.
 *
 * All other Mach64 adapters use a programmable clock generator.  BIOS
 * initialisation programmes an initial set of frequencies.  Two of these are
 * reserved to allow for the setting of modes that do not use a frequency from
 * this initial set.  One of these reserved slots is used by the BIOS mode set
 * routine, the other by the particular accelerated driver used (MS-Windows,
 * AutoCAD, etc.).  The slots reserved in this way are dependent on the
 * particular clock generator used by the adapter.
 *
 * If the driver does not support the adapter's clock generator, it will try to
 * match the (probed or specified) clocks to one of the following sets.
 *
 * Mach64 Clock Frequencies for unsupported programmable clock generators
 * R E G I S T E R S
 *   1CE     1CE     3C2     3C2    Frequency
 *   B9h     BEh                     (MHz)
 *  Bit 1   Bit 4   Bit 3   Bit 2    Set 1    Set 2    Set 3
 * ------- ------- ------- -------  -------  -------  -------
 *    0       0       0       0      50.350   25.180   25.180
 *    0       0       0       1      56.640   28.320   28.320
 *    0       0       1       0      63.000   31.500    0.000
 *    0       0       1       1      72.000   36.000    0.000
 *    0       1       0       0       0.000    0.000    0.000
 *    0       1       0       1     110.000  110.000    0.000
 *    0       1       1       0     126.000  126.000    0.000
 *    0       1       1       1     135.000  135.000    0.000
 *    1       0       0       0      40.000   40.000    0.000
 *    1       0       0       1      44.900   44.900    0.000
 *    1       0       1       0      49.500   49.500    0.000
 *    1       0       1       1      50.000   50.000    0.000
 *    1       1       0       0       0.000    0.000    0.000
 *    1       1       0       1      80.000   80.000    0.000
 *    1       1       1       0      75.000   75.000    0.000
 *    1       1       1       1      65.000   65.000    0.000
 *
 * The driver will never select a setting of 0.000 MHz.  The above comments on
 * clock ordering and clock divider apply here also.
 *
 * For all supported programmable clock generators, the driver will ignore any
 * XF86Config clock line and programme, as needed, the clock number reserved by
 * the BIOS for accelerated drivers.  The driver's mode initialisation routine
 * finds integers N, M and D such that
 *
 *             N
 *      R * -------  MHz
 *           M * D
 *
 * best approximates the mode's clock frequency, where R is the crystal-
 * generated reference frequency (usually 14.318 MHz).  D is a power of 2
 * except for those integrated controllers that also offer odd dividers.
 * Different clock generators have different restrictions on the value N, M and
 * D can assume.  The driver contains an internal table to record these
 * restrictions (among other things).  The resulting values of N, M and D are
 * then encoded in a generator-specific way and used to programme the clock.
 * The Mach64's clock divider is not used in this case.
 */

#include "ati.h"
#include "atiadapter.h"
#include "atichip.h"
#include "atidac.h"
#include "atidsp.h"
#include "atimach64io.h"
#include "atimode.h"
#include "atiwonderio.h"

/*
 * Definitions related to non-programmable clock generators.
 */
const char *ATIClockNames[] =
{
    "unknown",
    "IBM VGA compatible",
    "crystals",
    "ATI 18810 or similar",
    "ATI 18811-0 or similar",
    "ATI 18811-1 or similar",
    "ICS 2494-AM or similar",
    "Programmable (BIOS setting 1)",
    "Programmable (BIOS setting 2)",
    "Programmable (BIOS setting 3)"
};

/*
 * Definitions related to programmable clock generators.
 */
static CARD16 ATIPostDividers[] = {1, 2, 4, 8, 16, 32, 64, 128},
              ATI264xTPostDividers[] = {1, 2, 4, 8, 3, 0, 6, 12};
ClockRec ATIClockDescriptors[] =
{
    {
          0,   0,   0, 1, 1,
          1,   1,   0,
          0, NULL,
        "Non-programmable"
    },
    {
        257, 512, 257, 1, 1,
         46,  46,   0,
          4, ATIPostDividers,
        "ATI 18818 or ICS 2595 or similar"
    },
    {
          2, 129,   2, 1, 1,
          8,  14,   2,
          8, ATIPostDividers,
        "SGS-Thompson 1703 or similar"
    },
    {
         16, 263,   8, 8, 9,
          4,  12,   2,
          4, ATIPostDividers,
        "Chrontel 8398 or similar"
    },
    {
          2, 255,   0, 1, 1,
         45,  45,   0,
          4, ATI264xTPostDividers,
        "Internal"
    },
    {
          2, 257,   2, 1, 1,
          2,  32,   2,
          4, ATIPostDividers,
        "AT&T 20C408 or similar"
    },
    {
         65, 128,  65, 1, 1,
          2,  14,   0,
          4, ATIPostDividers,
        "IBM RGB 514 or similar"
    }
};

/*
 * XF86Config clocks line that start with the following will either be rejected
 * for ATI adapters, or accepted for non-ATI adapters.
 */
static const int
ATIVGAClocks[] =
{
     25175,  28322,
        -1
};

/*
 * The driver will attempt to match fixed clocks to one of the following
 * specifications.
 */
static const int
ATICrystalFrequencies[] =
{
     50000,  56644,      0,  44900,  44900,  50000,      0,  36000,
        -1
},
ATI18810Frequencies[] =
{
     30240,  32000,  37500,  39000,  42954,  48771,      0,  36000,
     40000,      0,  75000,  65000,  50350,  56640,      0,  44900
},
ATI188110Frequencies[] =
{
     30240,  32000, 110000,  80000,  42954,  48771,  92400,  36000,
     39910,  44900,  75000,  65000,  50350,  56640,      0,  44900
},
ATI188111Frequencies[] =
{
    135000,  32000, 110000,  80000, 100000, 126000,  92400,  36000,
     39910,  44900,  75000,  65000,  50350,  56640,      0,  44900
},
ATI2494AMFrequencies[] =
{
     75000,  77500,  80000,  90000,  25175,  28322,  31500,  36000,
    100000, 110000, 126000, 135000,  40000,  44900,  50000,  65000
},
ATIMach64AFrequencies[] =
{
         0, 110000, 126000, 135000,  50350,  56640,  63000,  72000,
         0,  80000,  75000,  65000,  40000,  44900,  49500,  50000
},
ATIMach64BFrequencies[] =
{
         0, 110000, 126000, 135000,  25180,  28320,  31500,  36000,
         0,  80000,  75000,  65000,  40000,  44900,  49500,  50000
},
ATIMach64CFrequencies[] =
{
         0,      0,      0,      0,  25180,  28320,      0,      0,
         0,      0,      0,      0,      0,      0,      0,      0
},
*SpecificationClockLine[] =
{
    NULL,
    ATIVGAClocks,
    ATICrystalFrequencies,
    ATI18810Frequencies,
    ATI188110Frequencies,
    ATI188111Frequencies,
    ATI2494AMFrequencies,
    ATIMach64AFrequencies,
    ATIMach64BFrequencies,
    ATIMach64CFrequencies,
    NULL
};

/*
 * The driver will reject XF86Config clocks lines that start with, or are an
 * initial subset of, one of the following.
 */
static const int
ATIPre_2_1_1_Clocks_A[] =       /* Based on 18810 */
{
     18000,  22450,  25175,  28320,  36000,  44900,  50350,  56640,
     30240,  32000,  37500,  39000,  40000,      0,  75000,  65000,
        -1
},
ATIPre_2_1_1_Clocks_B[] =       /* Based on 18811-0 */
{
     18000,  22450,  25175,  28320,  36000,  44900,  50350,  56640,
     30240,  32000, 110000,  80000,  39910,  44900,  75000,  65000,
        -1
},
ATIPre_2_1_1_Clocks_C[] =       /* Based on 18811-1 (or -2) */
{
     18000,  22450,  25175,  28320,  36000,  44900,  50350,  56640,
    135000,  32000, 110000,  80000,  39910,  44900,  75000,  65000,
        -1
},
ATIPre_2_1_1_Clocks_D[] =       /* Based on ICS 2494AM */
{
     18000,  32500,  20000,  22450,  36000,  65000,  40000,  44900,
     75000,  77500,  80000,  90000, 100000, 110000, 126000, 135000,
        -1
},
ATIPre_2_1_1_Clocks_E[] =       /* Based on programmable setting 1 */
{
     36000,  25000,  20000,  22450,  72000,  50000,  40000,  44900,
         0, 110000, 126000, 135000,      0,  80000,  75000,  65000,
        -1
},
ATIPre_2_1_1_Clocks_F[] =       /* Based on programmable setting 2 */
{
     18000,  25000,  20000,  22450,  36000,  50000,  40000,  44900,
         0, 110000, 126000, 135000,      0,  80000,  75000,  65000,
        -1
},
*InvalidClockLine[] =
{
    NULL,
    ATIVGAClocks,
    ATIPre_2_1_1_Clocks_A,
    ATIPre_2_1_1_Clocks_B,
    ATIPre_2_1_1_Clocks_C,
    ATIPre_2_1_1_Clocks_D,
    ATIPre_2_1_1_Clocks_E,
    ATIPre_2_1_1_Clocks_F,
    NULL
};

/*
 * Clock maps.
 */
static const CARD8 ClockMaps[][4] =
{
    /* Null map */
    { 0, 1, 2, 3},
    /* VGA Wonder map <-> Mach{8,32,64} */
    { 1, 0, 3, 2},
    /* VGA Wonder map <-> Accelerator */
    { 0, 2, 1, 3},
    /* VGA -> Accelerator map */
    { 2, 0, 3, 1},
    /* Accelerator -> VGA map */
    { 1, 3, 0, 2}
};
#define ATIVGAWonderClockMap         ClockMaps[0]
#define ATIVGAWonderClockUnmap       ATIVGAWonderClockMap
#define ATIMachVGAClockMap           ClockMaps[1]
#define ATIMachVGAClockUnmap         ATIMachVGAClockMap
#define ATIVGAProgrammableClockMap   ClockMaps[2]
#define ATIVGAProgrammableClockUnmap ATIVGAProgrammableClockMap
#define ATIAcceleratorClockMap       ClockMaps[3]
#define ATIAcceleratorClockUnmap     ClockMaps[4]
#define ATIProgrammableClockMap      ClockMaps[0]
#define ATIProgrammableClockUnmap    ATIProgrammableClockMap
#define MapClockIndex(_ClockMap, _Index) \
    (SetBits((_ClockMap)[GetBits(_Index, 0x0CU)], 0x0CU) | \
     ((_Index) & ~0x0CU))

/*
 * ATIMatchClockLine --
 *
 * This function tries to match the XF86Config clocks to one of an array of
 * clock lines.  It returns a clock line number or 0.
 */
static int
ATIMatchClockLine
(
    ScrnInfoPtr              pScreenInfo,
    ATIPtr                   pATI,
    const                int **ClockLine,
    const unsigned short int NumberOfClocks,
    const                int CalibrationClockNumber,
    const                int ClockMap
)
{
    int ClockChip = 0, ClockChipIndex = 0;
    int NumberOfMatchingClocks = 0;
    int MinimumGap = CLOCK_TOLERANCE + 1;

    /* For ATI adapters, reject generic VGA clocks */

#ifndef AVOID_CPIO

    if (pATI->Adapter != ATI_ADAPTER_VGA)

#endif /* AVOID_CPIO */

    {
        if (ClockLine == SpecificationClockLine)
            ClockChipIndex++;
    }

    /* If checking for XF86Config clock order, skip crystals */
    if (ClockMap)
        ClockChipIndex++;

    for (;  ClockLine[++ClockChipIndex];  )
    {
        int MaximumGap = 0, ClockCount = 0, ClockIndex = 0;

#ifndef AVOID_CPIO

        /* Only Mach64's and later can have programmable clocks */
        if ((ClockChipIndex >= ATI_CLOCK_MACH64A) &&
            (pATI->Adapter < ATI_ADAPTER_MACH64))
            break;

#endif /* AVOID_CPIO */

        for (;  ClockIndex < NumberOfClocks;  ClockIndex++)
        {
            int Gap, XF86ConfigClock, SpecificationClock;

            SpecificationClock = ClockLine[ClockChipIndex]
                [MapClockIndex(ClockMaps[ClockMap], ClockIndex)];
            if (SpecificationClock < 0)
                break;
            if (!SpecificationClock)
                continue;

            XF86ConfigClock = pScreenInfo->clock[ClockIndex];
            if (!XF86ConfigClock)
                continue;

            Gap = abs(XF86ConfigClock - SpecificationClock);
            if (Gap >= MinimumGap)
                goto SkipThisClockGenerator;
            if (!Gap)
            {
                if (ClockIndex == CalibrationClockNumber)
                    continue;
            }
            else if (Gap > MaximumGap)
502
            {
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
503
                MaximumGap = Gap;
504
            }
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566
            ClockCount++;
        }

        if (ClockCount <= NumberOfMatchingClocks)
            continue;
        NumberOfMatchingClocks = ClockCount;
        ClockChip = ClockChipIndex;
        if (!(MinimumGap = MaximumGap))
            break;

SkipThisClockGenerator:;

#ifndef AVOID_CPIO

        /* For non-ATI adapters, only normalise standard VGA clocks */
        if (pATI->Adapter == ATI_ADAPTER_VGA)
            break;

#endif /* AVOID_CPIO */

    }

    return ClockChip;
}

/*
 * ATIClockPreInit --
 *
 * This function is called by ATIPreInit() and handles the XF86Config clocks
 * line (or lack thereof).
 */
void
ATIClockPreInit
(
    ScrnInfoPtr   pScreenInfo,
    ATIPtr        pATI,
    GDevPtr       pGDev,
    ClockRangePtr pRange
)
{
    double ScaleFactor;
    unsigned short int NumberOfUndividedClocks;
    unsigned short int NumberOfDividers, NumberOfClocks;
    int CalibrationClockNumber, CalibrationClockValue;
    int ClockIndex, SpecificationClock, ClockMap = 0, Index;
    CARD8 CanDisableInterrupts;

#ifndef AVOID_CPIO

    CARD8 genmo;

#endif /* AVOID_CPIO */

    /*
     * Decide what to do about the XF86Config clocks for programmable clock
     * generators.
     */
    if (pATI->ProgrammableClock != ATI_CLOCK_FIXED)
    {
        /* Check for those that are not (yet) handled */
        if ((pATI->ProgrammableClock == ATI_CLOCK_UNKNOWN) ||
            (pATI->ProgrammableClock > NumberOf(ATIClockDescriptors)))
567
        {
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
568 569 570
            xf86DrvMsgVerb(pScreenInfo->scrnIndex, X_WARNING, 0,
                "Unknown programmable clock generator type (0x%02X)"
                " detected.\n", pATI->ProgrammableClock);
571
        }
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
572
        else if (pATI->ClockDescriptor.MaxN <= 0)
573
        {
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
574 575 576
            xf86DrvMsgVerb(pScreenInfo->scrnIndex, X_WARNING, 0,
                "Unsupported programmable clock generator detected:  %s.\n",
                pATI->ClockDescriptor.ClockName);
577
        }
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632
        else
        {
            /*
             * Recognise supported clock generators.  This involves telling the
             * rest of the server about it and (re-)initializing the XF86Config
             * clocks line.
             */
            pRange->clockIndex = -1;
            pScreenInfo->progClock = TRUE;

            /* Set internal clock ordering */

#ifndef AVOID_CPIO

            if (pATI->NewHW.crtc == ATI_CRTC_VGA)
            {
                pATI->NewHW.ClockMap = ATIVGAProgrammableClockMap;
                pATI->NewHW.ClockUnmap = ATIVGAProgrammableClockUnmap;
            }
            else

#endif /* AVOID_CPIO */

            {
                pATI->NewHW.ClockMap = ATIProgrammableClockMap;
                pATI->NewHW.ClockUnmap = ATIProgrammableClockUnmap;
            }

            xf86DrvMsg(pScreenInfo->scrnIndex, X_PROBED,
                "%s programmable clock generator detected.\n",
                pATI->ClockDescriptor.ClockName);
            if (pATI->ReferenceDenominator == 1)
                xf86DrvMsg(pScreenInfo->scrnIndex, X_PROBED,
                    "Reference clock %.3f MHz.\n",
                    (double)pATI->ReferenceNumerator / 1000.0);
            else
                xf86DrvMsg(pScreenInfo->scrnIndex, X_PROBED,
                    "Reference clock %.6g/%d (%.3f) MHz.\n",
                    (double)pATI->ReferenceNumerator / 1000.0,
                    pATI->ReferenceDenominator,
                    (double)pATI->ReferenceNumerator /
                        ((double)pATI->ReferenceDenominator * 1000.0));

            /* Clobber XF86Config clocks line */
            if (pGDev->numclocks)
                xf86DrvMsg(pScreenInfo->scrnIndex, X_NOTICE,
                    "XF86Config clocks specification ignored.\n");

            if (pATI->ProgrammableClock == ATI_CLOCK_CH8398)
            {   /* First two are fixed */
                pScreenInfo->numClocks = 2;
                pScreenInfo->clock[0] = 25175;
                pScreenInfo->clock[1] = 28322;
            }
            else if (pATI->ProgrammableClock == ATI_CLOCK_INTERNAL)
633 634 635 636 637
            {
                /*
                 * The integrated PLL generates clocks as if the reference
                 * frequency were doubled.
                 */
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
638
                pATI->ReferenceNumerator <<= 1;
639
            }
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769

            return;     /* ... to ATIPreInit() */
        }
    }

#ifndef AVOID_CPIO

    /* Set default clock maps */
    pATI->NewHW.ClockMap = ATIVGAWonderClockMap;
    pATI->NewHW.ClockUnmap = ATIVGAWonderClockUnmap;

#endif /* AVOID_CPIO */

    /*
     * Determine the number of clock values the adapter should be able to
     * generate and the dot clock to use for probe calibration.
     */
ProbeClocks:

#ifndef AVOID_CPIO

    if (pATI->Adapter == ATI_ADAPTER_VGA)
    {
        NumberOfDividers = 1;
        NumberOfUndividedClocks = 4;
        CalibrationClockNumber = 1;
        CalibrationClockValue = 28322;
    }
    else

#endif /* AVOID_CPIO */

    {

#ifndef AVOID_CPIO

        NumberOfDividers = 4;
        if ((pATI->Chip <= ATI_CHIP_18800) ||
            (pATI->Adapter == ATI_ADAPTER_V4))
        {
            NumberOfUndividedClocks = 8;
            /* Actually, any undivided clock will do */
            CalibrationClockNumber = 1;
            CalibrationClockValue = 56644;
        }
        else

#endif /* AVOID_CPIO */

        {
            NumberOfUndividedClocks = 16;

#ifndef AVOID_CPIO

            CalibrationClockNumber = 7;
            CalibrationClockValue = 36000;
            if (pATI->Chip >= ATI_CHIP_68800)

#endif /* AVOID_CPIO */

            {
                NumberOfDividers = 2;
                if (pATI->Chip >= ATI_CHIP_264CT)
                {
                    NumberOfDividers = 1;
                    NumberOfUndividedClocks = 4;
                    CalibrationClockNumber = 1;
                    CalibrationClockValue = 28322;
                }
                else

#ifndef AVOID_CPIO

                if (pATI->Adapter >= ATI_ADAPTER_MACH64)

#endif /* AVOID_CPIO */

                {
                    CalibrationClockNumber = 10 /* or 11 */;
                    CalibrationClockValue = 75000 /* or 65000 */;
                }

                /*
                 * When selecting clocks, all ATI accelerators use a different
                 * clock ordering.
                 */

#ifndef AVOID_CPIO

                if (pATI->NewHW.crtc == ATI_CRTC_VGA)
                {
                    pATI->NewHW.ClockMap = ATIMachVGAClockMap;
                    pATI->NewHW.ClockUnmap = ATIMachVGAClockUnmap;
                }
                else

#endif /* AVOID_CPIO */

                {
                    pATI->NewHW.ClockMap = ATIAcceleratorClockMap;
                    pATI->NewHW.ClockUnmap = ATIAcceleratorClockUnmap;
                }
            }
        }
    }

    pATI->OldHW.ClockMap = pATI->NewHW.ClockMap;
    pATI->OldHW.ClockUnmap = pATI->NewHW.ClockUnmap;

    NumberOfClocks = NumberOfUndividedClocks * NumberOfDividers;

    /*
     * Respect any XF86Config clocks line.  Well, that's the theory, anyway.
     * In practice, however, the regular use of probed values is widespread, at
     * times causing otherwise inexplicable results.  So, attempt to normalise
     * the clocks to known (i.e. specification) values.
     */
    if (!pGDev->numclocks || pATI->OptionProbeClocks ||
        xf86ServerIsOnlyProbing())
    {
        if (pATI->ProgrammableClock != ATI_CLOCK_FIXED)
        {
            /*
             * For unsupported programmable clock generators, pick the highest
             * frequency set by BIOS initialisation for clock calibration.
             */
            CalibrationClockNumber = CalibrationClockValue = 0;
            for (ClockIndex = 0;
                 ClockIndex < NumberOfUndividedClocks;
                 ClockIndex++)
770
            {
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
771 772 773 774 775
                if (CalibrationClockValue < pATI->BIOSClocks[ClockIndex])
                {
                    CalibrationClockNumber = ClockIndex;
                    CalibrationClockValue = pATI->BIOSClocks[ClockIndex];
                }
776
            }
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843
            CalibrationClockNumber =
                MapClockIndex(pATI->NewHW.ClockUnmap, CalibrationClockNumber);
            CalibrationClockValue *= 10;
        }

#ifndef AVOID_CPIO

        if (pATI->VGAAdapter != ATI_ADAPTER_NONE)
        {
            /*
             * The current video state needs to be saved before the clock
             * probe, and restored after.  Video memory corruption and other
             * effects occur because, at this early stage, the clock probe
             * cannot reliably be prevented from enabling frequencies that are
             * greater than what the adapter can handle.
             */
            ATIModeSave(pScreenInfo, pATI, &pATI->OldHW);

            /* Ensure clock select pins are not OR'ed with anything */
            if (pATI->CPIO_VGAWonder && (pATI->OldHW.crtc == ATI_CRTC_VGA))
                ATIModifyExtReg(pATI, 0xB5U, pATI->OldHW.b5, 0x7FU, 0x00U);
        }

#endif /* AVOID_CPIO */

        /*
         * Probe the adapter for clock values.  The following is essentially
         * the common layer's xf86GetClocks() reworked to fit.  One difference
         * is the ability to monitor a VSync bit in MMIO space.
         */
        CanDisableInterrupts = TRUE;    /* An assumption verified below */

        for (ClockIndex = 0;  ClockIndex < NumberOfClocks;  ClockIndex++)
        {
            pScreenInfo->clock[ClockIndex] = 0;

            /* Remap clock number */
            Index = MapClockIndex(pATI->OldHW.ClockMap, ClockIndex);

            /* Select the clock */
            switch (pATI->OldHW.crtc)
            {

#ifndef AVOID_CPIO

                case ATI_CRTC_VGA:
                    /* Get generic two low-order bits */
                    genmo = (inb(R_GENMO) & 0xF3U) | ((Index << 2) & 0x0CU);

                    if (pATI->CPIO_VGAWonder)
                    {
                        /*
                         * On adapters with crystals, switching to one of the
                         * spare assignments doesn't do anything (i.e. the
                         * previous setting remains in effect).  So, disable
                         * their selection.
                         */
                        if (((Index & 0x03U) == 0x02U) &&
                            ((pATI->Chip <= ATI_CHIP_18800) ||
                             (pATI->Adapter == ATI_ADAPTER_V4)))
                            continue;

                        /* Start sequencer reset */
                        PutReg(SEQX, 0x00U, 0x00U);

                        /* Set high-order bits */
                        if (pATI->Chip <= ATI_CHIP_18800)
844
                        {
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
845 846
                            ATIModifyExtReg(pATI, 0xB2U, -1, 0xBFU,
                                Index << 4);
847
                        }
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075
                        else
                        {
                            ATIModifyExtReg(pATI, 0xBEU, -1, 0xEFU,
                                Index << 2);
                            if (pATI->Adapter != ATI_ADAPTER_V4)
                            {
                                Index >>= 1;
                                ATIModifyExtReg(pATI, 0xB9U, -1, 0xFDU,
                                    Index >> 1);
                            }
                        }

                        /* Set clock divider bits */
                        ATIModifyExtReg(pATI, 0xB8U, -1, 0x00U,
                            (Index << 3) & 0xC0U);
                    }
                    else
                    {
                        /*
                         * Reject clocks that cannot be selected.
                         */
                        if (Index & ~0x03U)
                            continue;

                        /* Start sequencer reset */
                        PutReg(SEQX, 0x00U, 0x00U);
                    }

                    /* Must set miscellaneous output register last */
                    outb(GENMO, genmo);

                    /* End sequencer reset */
                    PutReg(SEQX, 0x00U, 0x03U);

                    break;

#endif /* AVOID_CPIO */

                case ATI_CRTC_MACH64:
                    out8(CLOCK_CNTL, CLOCK_STROBE |
                        SetBits(Index, CLOCK_SELECT | CLOCK_DIVIDER));
                    break;

                default:
                    continue;
            }

            usleep(50000);      /* Let clock stabilise */

            xf86SetPriority(TRUE);

            /* Try to disable interrupts */
            if (CanDisableInterrupts && !xf86DisableInterrupts())
            {
                xf86DrvMsg(pScreenInfo->scrnIndex, X_WARNING,
                    "Unable to disable interrupts;  Clock probe will not be as"
                    " accurate.\n");
                CanDisableInterrupts = FALSE;
            }

            /*
             * Generate a count while monitoring the vertical sync or blanking
             * pulse.  This is dependent on the CRTC used by the mode on server
             * entry.
             */
            switch (pATI->OldHW.crtc)
            {

#ifndef AVOID_CPIO

                case ATI_CRTC_VGA:
                    /* Verify vertical sync pulses are in fact occurring */
                    Index = 1 << 19;
                    while (!(inb(GENS1(pATI->CPIO_VGABase)) & 0x08U))
                        if (Index-- <= 0)
                            goto EnableInterrupts;
                    Index = 1 << 19;
                    while (inb(GENS1(pATI->CPIO_VGABase)) & 0x08U)
                        if (Index-- <= 0)
                            goto EnableInterrupts;
                    Index = 1 << 19;
                    while (!(inb(GENS1(pATI->CPIO_VGABase)) & 0x08U))
                        if (Index-- <= 0)
                            goto EnableInterrupts;

                    /* Generate the count */
                    for (Index = 0;  Index < 8;  Index++)
                    {
                        while (inb(GENS1(pATI->CPIO_VGABase)) & 0x08U)
                            pScreenInfo->clock[ClockIndex]++;
                        while (!(inb(GENS1(pATI->CPIO_VGABase)) & 0x08U))
                            pScreenInfo->clock[ClockIndex]++;
                    }
                    break;

#endif /* AVOID_CPIO */

                case ATI_CRTC_MACH64:
                    /* Verify vertical blanking pulses are in fact occurring */
                    Index = 1 << 19;
                    while (!(inr(CRTC_INT_CNTL) & CRTC_VBLANK))
                        if (Index-- <= 0)
                            goto EnableInterrupts;
                    Index = 1 << 19;
                    while (inr(CRTC_INT_CNTL) & CRTC_VBLANK)
                        if (Index-- <= 0)
                            goto EnableInterrupts;
                    Index = 1 << 19;
                    while (!(inr(CRTC_INT_CNTL) & CRTC_VBLANK))
                        if (Index-- <= 0)
                            goto EnableInterrupts;

                    /* Generate the count */
                    for (Index = 0;  Index < 4;  Index++)
                    {
                        while (inr(CRTC_INT_CNTL) & CRTC_VBLANK)
                            pScreenInfo->clock[ClockIndex]++;
                        while (!(inr(CRTC_INT_CNTL) & CRTC_VBLANK))
                            pScreenInfo->clock[ClockIndex]++;
                    }
                    break;

                default:
                    break;
            }

        EnableInterrupts:
            if (CanDisableInterrupts)
                xf86EnableInterrupts();

            xf86SetPriority(FALSE);
        }

        ScaleFactor = (double)CalibrationClockValue *
            (double)pScreenInfo->clock[CalibrationClockNumber];

        /* Scale the clocks from counts to kHz */
        for (ClockIndex = 0;  ClockIndex < NumberOfClocks;  ClockIndex++)
        {
            if (ClockIndex == CalibrationClockNumber)
                pScreenInfo->clock[ClockIndex] = CalibrationClockValue;
            else if (pScreenInfo->clock[ClockIndex])
                /* Round to the nearest 10 kHz */
                pScreenInfo->clock[ClockIndex] =
                    (int)(((ScaleFactor /
                            (double)pScreenInfo->clock[ClockIndex]) +
                           5) / 10) * 10;
        }

        pScreenInfo->numClocks = NumberOfClocks;

#ifndef AVOID_CPIO

        if (pATI->VGAAdapter != ATI_ADAPTER_NONE)
        {
            /* Restore video state */
            ATIModeSet(pScreenInfo, pATI, &pATI->OldHW);
            xfree(pATI->OldHW.frame_buffer);
            pATI->OldHW.frame_buffer = NULL;
        }

#endif /* AVOID_CPIO */

        /* Tell user clocks were probed, instead of supplied */
        pATI->OptionProbeClocks = TRUE;

        /* Attempt to match probed clocks to a known specification */
        pATI->Clock = ATIMatchClockLine(pScreenInfo, pATI,
            SpecificationClockLine, NumberOfUndividedClocks,
            CalibrationClockNumber, 0);

#ifndef AVOID_CPIO

        if ((pATI->Chip <= ATI_CHIP_18800) ||
            (pATI->Adapter == ATI_ADAPTER_V4))
        {
            /* V3 and V4 adapters don't have clock chips */
            if (pATI->Clock > ATI_CLOCK_CRYSTALS)
                pATI->Clock = ATI_CLOCK_NONE;
        }
        else

#endif /* AVOID_CPIO */

        {
            /* All others don't have crystals */
            if (pATI->Clock == ATI_CLOCK_CRYSTALS)
                pATI->Clock = ATI_CLOCK_NONE;
        }
    }
    else
    {
        /*
         * Allow for an initial subset of specification clocks.  Can't allow
         * for any more than that though...
         */
        if (NumberOfClocks > pGDev->numclocks)
        {
            NumberOfClocks = pGDev->numclocks;
            if (NumberOfUndividedClocks > NumberOfClocks)
                NumberOfUndividedClocks = NumberOfClocks;
        }

        /* Move XF86Config clocks into the ScrnInfoRec */
        for (ClockIndex = 0;  ClockIndex < NumberOfClocks;  ClockIndex++)
            pScreenInfo->clock[ClockIndex] = pGDev->clock[ClockIndex];
        pScreenInfo->numClocks = NumberOfClocks;

        /* Attempt to match clocks to a known specification */
        pATI->Clock = ATIMatchClockLine(pScreenInfo, pATI,
            SpecificationClockLine, NumberOfUndividedClocks, -1, 0);

#ifndef AVOID_CPIO

        if (pATI->Adapter != ATI_ADAPTER_VGA)

#endif /* AVOID_CPIO */

        {
            if (pATI->Clock == ATI_CLOCK_NONE)
            {
                /*
                 * Reject certain clock lines that are obviously wrong.  This
                 * includes the standard VGA clocks for ATI adapters, and clock
                 * lines that could have been used with the pre-2.1.1 driver.
                 */
                if (ATIMatchClockLine(pScreenInfo, pATI, InvalidClockLine,
                    NumberOfClocks, -1, 0))
1076
                {
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1077
                    pATI->OptionProbeClocks = TRUE;
1078
                }
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140
                else

#ifndef AVOID_CPIO

                if ((pATI->Chip >= ATI_CHIP_18800) &&
                    (pATI->Adapter != ATI_ADAPTER_V4))

#endif /* AVOID_CPIO */

                {
                    /*
                     * Check for clocks that are specified in the wrong order.
                     * This is meant to catch those who are trying to use the
                     * clock order intended for the old accelerated servers.
                     */
                    while ((++ClockMap, ClockMap %= NumberOf(ClockMaps)))
                    {
                        pATI->Clock = ATIMatchClockLine(pScreenInfo, pATI,
                            SpecificationClockLine, NumberOfUndividedClocks,
                            -1, ClockMap);
                        if (pATI->Clock != ATI_CLOCK_NONE)
                        {
                            xf86DrvMsgVerb(pScreenInfo->scrnIndex,
                                X_WARNING, 0,
                                "XF86Config clock ordering incorrect.  Clocks"
                                " will be reordered.\n");
                            break;
                        }
                    }
                }
            }
            else
            /* Ensure crystals are not matched to clock chips, and vice versa */

#ifndef AVOID_CPIO

            if ((pATI->Chip <= ATI_CHIP_18800) ||
                (pATI->Adapter == ATI_ADAPTER_V4))
            {
                if (pATI->Clock > ATI_CLOCK_CRYSTALS)
                    pATI->OptionProbeClocks = TRUE;
            }
            else

#endif /* AVOID_CPIO */

            {
                if (pATI->Clock == ATI_CLOCK_CRYSTALS)
                    pATI->OptionProbeClocks = TRUE;
            }

            if (pATI->OptionProbeClocks)
            {
                xf86DrvMsgVerb(pScreenInfo->scrnIndex, X_WARNING, 0,
                    "Invalid or obsolete XF86Config clocks line rejected.\n"
                    " Clocks will be probed.\n");
                goto ProbeClocks;
            }
        }
    }

    if (pATI->ProgrammableClock != ATI_CLOCK_FIXED)
1141
    {
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1142
        pATI->ProgrammableClock = ATI_CLOCK_FIXED;
1143
    }
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1144
    else if (pATI->Clock == ATI_CLOCK_NONE)
1145
    {
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1146 1147
        xf86DrvMsgVerb(pScreenInfo->scrnIndex, X_WARNING, 0,
            "Unknown clock generator detected.\n");
1148
    }
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1149 1150 1151 1152 1153
    else

#ifndef AVOID_CPIO

    if (pATI->Clock == ATI_CLOCK_CRYSTALS)
1154
    {
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1155 1156
        xf86DrvMsg(pScreenInfo->scrnIndex, X_PROBED,
            "This adapter uses crystals to generate clock frequencies.\n");
1157
    }
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425
    else if (pATI->Clock != ATI_CLOCK_VGA)

#endif /* AVOID_CPIO */

    {
        xf86DrvMsg(pScreenInfo->scrnIndex, X_PROBED,
            "%s clock chip detected.\n", ATIClockNames[pATI->Clock]);
    }

    if (pATI->Clock != ATI_CLOCK_NONE)
    {
        /* Replace undivided clocks with specification values */
        for (ClockIndex = 0;
             ClockIndex < NumberOfUndividedClocks;
             ClockIndex++)
        {
            /*
             * Don't replace clocks that are probed, documented, or set by the
             * user to zero.  One exception is that we need to override the
             * user's value for the spare settings on a crystal-based adapter.
             * Another exception is when the user specifies the clock ordering
             * intended for the old accelerated servers.
             */
            SpecificationClock =
                SpecificationClockLine[pATI->Clock][ClockIndex];
            if (SpecificationClock < 0)
                break;
            if (!ClockMap)
            {
                if (!pScreenInfo->clock[ClockIndex])
                    continue;
                if (!SpecificationClock)
                {
                    if (pATI->Clock != ATI_CLOCK_CRYSTALS)
                        continue;
                }
                else
                {
                    /*
                     * Due to the way clock lines are matched, the following
                     * can prevent the override if the clock is probed,
                     * documented or set by the user to a value greater than
                     * maxClock.
                     */
                    if (abs(SpecificationClock -
                            pScreenInfo->clock[ClockIndex]) > CLOCK_TOLERANCE)
                        continue;
                }
            }
            pScreenInfo->clock[ClockIndex] = SpecificationClock;
        }

        /* Adjust divided clocks */
        for (ClockIndex = NumberOfUndividedClocks;
             ClockIndex < NumberOfClocks;
             ClockIndex++)
            pScreenInfo->clock[ClockIndex] = ATIDivide(
                pScreenInfo->clock[ClockIndex % NumberOfUndividedClocks],
                (ClockIndex / NumberOfUndividedClocks) + 1, 0, 0);
    }

    /* Tell user about fixed clocks */
    xf86ShowClocks(pScreenInfo, pATI->OptionProbeClocks ? X_PROBED : X_CONFIG);

    /* Prevent selection of high clocks, even by V_CLKDIV2 modes */
    for (ClockIndex = 0;  ClockIndex < NumberOfClocks;  ClockIndex++)
        if (pScreenInfo->clock[ClockIndex] > pRange->maxClock)
            pScreenInfo->clock[ClockIndex] = 0;
}

/*
 * ATIClockSave --
 *
 * This function saves that part of an ATIHWRec that relates to clocks.
 */
void
ATIClockSave
(
    ScrnInfoPtr pScreenInfo,
    ATIPtr      pATI,
    ATIHWPtr    pATIHW
)
{
    if (pScreenInfo->vtSema && (pATI->ProgrammableClock > ATI_CLOCK_FIXED))
    {

#ifndef AVOID_CPIO

        if (pATIHW->crtc == ATI_CRTC_VGA)
        {
            pATIHW->ClockMap = ATIVGAProgrammableClockMap;
            pATIHW->ClockUnmap = ATIVGAProgrammableClockUnmap;
        }
        else

#endif /* AVOID_CPIO */

        {
            pATIHW->ClockMap = ATIProgrammableClockMap;
            pATIHW->ClockUnmap = ATIProgrammableClockUnmap;
        }
    }
    else
    {

#ifndef AVOID_CPIO

        if (pATIHW->crtc != ATI_CRTC_VGA)

#endif /* AVOID_CPIO */

        {
            pATIHW->ClockMap = ATIAcceleratorClockMap;
            pATIHW->ClockUnmap = ATIAcceleratorClockUnmap;
        }

#ifndef AVOID_CPIO

        else if (pATI->Chip < ATI_CHIP_68800)
        {
            pATIHW->ClockMap = ATIVGAWonderClockMap;
            pATIHW->ClockUnmap = ATIVGAWonderClockUnmap;
        }
        else
        {
            pATIHW->ClockMap = ATIMachVGAClockMap;
            pATIHW->ClockUnmap = ATIMachVGAClockUnmap;
        }

#endif /* AVOID_CPIO */

    }
}

/*
 * ATIClockCalculate --
 *
 * This function is called to generate, if necessary, the data needed for clock
 * programming, and set clock select bits in various register values.
 */
Bool
ATIClockCalculate
(
    int            iScreen,
    ATIPtr         pATI,
    ATIHWPtr       pATIHW,
    DisplayModePtr pMode
)
{
    int N, M, D;
    int ClockSelect, N1, MinimumGap;
    int Frequency, Multiple;            /* Used as temporaries */

    /* Set default values */
    pATIHW->FeedbackDivider = pATIHW->ReferenceDivider = pATIHW->PostDivider = 0;

    if ((pATI->ProgrammableClock <= ATI_CLOCK_FIXED) ||
        ((pATI->ProgrammableClock == ATI_CLOCK_CH8398) &&
         (pMode->ClockIndex < 2)))
    {
        /* Use a fixed clock */
        ClockSelect = pMode->ClockIndex;
    }
    else
    {
        /* Generate clock programme word, using units of kHz */
        MinimumGap = ((unsigned int)(-1)) >> 1;

        /* Loop through reference dividers */
        for (M = pATI->ClockDescriptor.MinM;
             M <= pATI->ClockDescriptor.MaxM;
             M++)
        {
            /* Loop through post-dividers */
            for (D = 0;  D < pATI->ClockDescriptor.NumD;  D++)
            {
                if (!pATI->ClockDescriptor.PostDividers[D])
                    continue;

                /* Limit undivided VCO to maxClock */
                if (pATI->maxClock &&
                    ((pATI->maxClock / pATI->ClockDescriptor.PostDividers[D]) <
                     pMode->Clock))
                    continue;

                /*
                 * Calculate closest feedback divider and apply its
                 * restrictions.
                 */
                Multiple = M * pATI->ReferenceDenominator *
                    pATI->ClockDescriptor.PostDividers[D];
                N = ATIDivide(pMode->Clock * Multiple,
                    pATI->ReferenceNumerator, 0, 0);
                if (N < pATI->ClockDescriptor.MinN)
                    N = pATI->ClockDescriptor.MinN;
                else if (N > pATI->ClockDescriptor.MaxN)
                    N = pATI->ClockDescriptor.MaxN;
                N -= pATI->ClockDescriptor.NAdjust;
                N1 = (N / pATI->ClockDescriptor.N1) * pATI->ClockDescriptor.N2;
                if (N > N1)
                    N = ATIDivide(N1 + 1, pATI->ClockDescriptor.N1, 0, 1);
                N += pATI->ClockDescriptor.NAdjust;
                N1 += pATI->ClockDescriptor.NAdjust;

                for (;  ;  N = N1)
                {
                    /* Pick the closest setting */
                    Frequency = abs(ATIDivide(N * pATI->ReferenceNumerator,
                        Multiple, 0, 0) - pMode->Clock);
                    if ((Frequency < MinimumGap) ||
                        ((Frequency == MinimumGap) &&
                         (pATIHW->FeedbackDivider < N)))
                    {
                        /* Save settings */
                        pATIHW->FeedbackDivider = N;
                        pATIHW->ReferenceDivider = M;
                        pATIHW->PostDivider = D;
                        MinimumGap = Frequency;
                    }

                    if (N <= N1)
                        break;
                }
            }
        }

        Multiple = pATIHW->ReferenceDivider * pATI->ReferenceDenominator *
            pATI->ClockDescriptor.PostDividers[pATIHW->PostDivider];
        Frequency = pATIHW->FeedbackDivider * pATI->ReferenceNumerator;
        Frequency = ATIDivide(Frequency, Multiple, 0, 0);
        if (abs(Frequency - pMode->Clock) > CLOCK_TOLERANCE)
        {
            xf86DrvMsg(iScreen, X_ERROR,
                "Unable to programme clock %.3fMHz for mode %s.\n",
                (double)(pMode->Clock) / 1000.0, pMode->name);
            return FALSE;
        }
        pMode->SynthClock = Frequency;
        ClockSelect = pATI->ClockNumberToProgramme;

        xf86ErrorFVerb(4,
            "\n Programming clock %d to %.3fMHz for mode %s."
            "  N=%d, M=%d, D=%d.\n",
            ClockSelect, (double)Frequency / 1000.0, pMode->name,
            pATIHW->FeedbackDivider, pATIHW->ReferenceDivider,
            pATIHW->PostDivider);

        if (pATI->Chip >= ATI_CHIP_264VTB)
            ATIDSPCalculate(pATI, pATIHW, pMode);
    }

    /* Set clock select bits, after remapping them */
    pATIHW->clock = ClockSelect;        /* Save pre-map clock number */
    ClockSelect = MapClockIndex(pATIHW->ClockMap, ClockSelect);

    switch (pATIHW->crtc)
    {

#ifndef AVOID_CPIO

        case ATI_CRTC_VGA:
            pATIHW->genmo = (pATIHW->genmo & 0xF3U) |
                ((ClockSelect << 2) & 0x0CU);

            if (pATI->CPIO_VGAWonder)
            {
                /* Set ATI clock select bits */
                if (pATI->Chip <= ATI_CHIP_18800)
1426
                {
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1427 1428
                    pATIHW->b2 = (pATIHW->b2 & 0xBFU) |
                        ((ClockSelect << 4) & 0x40U);
1429
                }
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540
                else
                {
                    pATIHW->be = (pATIHW->be & 0xEFU) |
                        ((ClockSelect << 2) & 0x10U);
                    if (pATI->Adapter != ATI_ADAPTER_V4)
                    {
                        ClockSelect >>= 1;
                        pATIHW->b9 = (pATIHW->b9 & 0xFDU) |
                            ((ClockSelect >> 1) & 0x02U);
                    }
                }

                /* Set clock divider bits */
                pATIHW->b8 = (pATIHW->b8 & 0x3FU) |
                    ((ClockSelect << 3) & 0xC0U);
            }
            break;

#endif /* AVOID_CPIO */

        case ATI_CRTC_MACH64:
            pATIHW->clock_cntl = CLOCK_STROBE |
                SetBits(ClockSelect, CLOCK_SELECT | CLOCK_DIVIDER);
            break;

        default:
            break;
    }

    return TRUE;
}

/*
 * ATIClockSet --
 *
 * This function is called to programme a clock for the mode being set.
 */
void
ATIClockSet
(
    ATIPtr      pATI,
    ATIHWPtr    pATIHW
)
{
    CARD32 crtc_gen_cntl, tmp;
    CARD8 clock_cntl0;
    CARD8 tmp2;
    unsigned int Programme;
    int N = pATIHW->FeedbackDivider - pATI->ClockDescriptor.NAdjust;
    int M = pATIHW->ReferenceDivider - pATI->ClockDescriptor.MAdjust;
    int D = pATIHW->PostDivider;

    /* Temporarily switch to accelerator mode */
    crtc_gen_cntl = inr(CRTC_GEN_CNTL);
    if (!(crtc_gen_cntl & CRTC_EXT_DISP_EN))
        outr(CRTC_GEN_CNTL, crtc_gen_cntl | CRTC_EXT_DISP_EN);

    switch (pATI->ProgrammableClock)
    {
        case ATI_CLOCK_ICS2595:
            clock_cntl0 = in8(CLOCK_CNTL);

            Programme = (SetBits(pATIHW->clock, ICS2595_CLOCK) |
                SetBits(N, ICS2595_FB_DIV) | SetBits(D, ICS2595_POST_DIV)) ^
                ICS2595_TOGGLE;

            ATIDelay(50000);            /* 50 milliseconds */

            (void)xf86DisableInterrupts();

            /* Send all 20 bits of programme word */
            while (Programme >= CLOCK_BIT)
            {
                tmp = (Programme & CLOCK_BIT) | CLOCK_STROBE;
                out8(CLOCK_CNTL, tmp);
                ATIDelay(26);           /* 26 microseconds */
                out8(CLOCK_CNTL, tmp | CLOCK_PULSE);
                ATIDelay(26);           /* 26 microseconds */
                Programme >>= 1;
            }

            xf86EnableInterrupts();

            /* Restore register */
            out8(CLOCK_CNTL, clock_cntl0 | CLOCK_STROBE);
            break;

        case ATI_CLOCK_STG1703:
            (void)ATIGetDACCmdReg(pATI);
            (void)in8(M64_DAC_MASK);
            out8(M64_DAC_MASK, (pATIHW->clock << 1) + 0x20U);
            out8(M64_DAC_MASK, 0);
            out8(M64_DAC_MASK, SetBits(N, 0xFFU));
            out8(M64_DAC_MASK, SetBits(M, 0x1FU) | SetBits(D, 0xE0U));
            break;

        case ATI_CLOCK_CH8398:
            tmp = inr(DAC_CNTL) | (DAC_EXT_SEL_RS2 | DAC_EXT_SEL_RS3);
            outr(DAC_CNTL, tmp);
            out8(M64_DAC_WRITE, pATIHW->clock);
            out8(M64_DAC_DATA, SetBits(N, 0xFFU));
            out8(M64_DAC_DATA, SetBits(M, 0x3FU) | SetBits(D, 0xC0U));
            out8(M64_DAC_MASK, 0x04U);
            outr(DAC_CNTL, tmp & ~(DAC_EXT_SEL_RS2 | DAC_EXT_SEL_RS3));
            tmp2 = in8(M64_DAC_WRITE);
            out8(M64_DAC_WRITE, (tmp2 & 0x70U) | 0x80U);
            outr(DAC_CNTL, tmp & ~DAC_EXT_SEL_RS2);
            break;

        case ATI_CLOCK_INTERNAL:
            /* Reset VCLK generator */
1541
            ATIMach64PutPLLReg(PLL_VCLK_CNTL, pATIHW->pll_vclk_cntl);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1542 1543 1544

            /* Set post-divider */
            tmp2 = pATIHW->clock << 1;
1545
            tmp = ATIMach64GetPLLReg(PLL_VCLK_POST_DIV);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1546 1547
            tmp &= ~(0x03U << tmp2);
            tmp |= SetBits(D, 0x03U) << tmp2;
1548
            ATIMach64PutPLLReg(PLL_VCLK_POST_DIV, tmp);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1549 1550

            /* Set extended post-divider */
1551
            tmp = ATIMach64GetPLLReg(PLL_XCLK_CNTL);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1552 1553
            tmp &= ~(SetBits(1, PLL_VCLK0_XDIV) << pATIHW->clock);
            tmp |= SetBits(D >> 2, PLL_VCLK0_XDIV) << pATIHW->clock;
1554
            ATIMach64PutPLLReg(PLL_XCLK_CNTL, tmp);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1555 1556 1557

            /* Set feedback divider */
            tmp = PLL_VCLK0_FB_DIV + pATIHW->clock;
1558
            ATIMach64PutPLLReg(tmp, SetBits(N, 0xFFU));
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1559 1560

            /* End VCLK generator reset */
1561
            ATIMach64PutPLLReg(PLL_VCLK_CNTL,
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1562 1563 1564
                pATIHW->pll_vclk_cntl & ~PLL_VCLK_RESET);

            /* Reset write bit */
1565
            ATIMach64AccessPLLReg(pATI, 0, FALSE);
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608
            break;

        case ATI_CLOCK_ATT20C408:
            (void)ATIGetDACCmdReg(pATI);
            tmp = in8(M64_DAC_MASK);
            (void)ATIGetDACCmdReg(pATI);
            out8(M64_DAC_MASK, tmp | 1);
            out8(M64_DAC_WRITE, 1);
            out8(M64_DAC_MASK, tmp | 9);
            ATIDelay(400);              /* 400 microseconds */
            tmp2 = (pATIHW->clock << 2) + 0x40U;
            out8(M64_DAC_WRITE, tmp2);
            out8(M64_DAC_MASK, SetBits(N, 0xFFU));
            out8(M64_DAC_WRITE, ++tmp2);
            out8(M64_DAC_MASK, SetBits(M, 0x3FU) | SetBits(D, 0xC0U));
            out8(M64_DAC_WRITE, ++tmp2);
            out8(M64_DAC_MASK, 0x77U);
            ATIDelay(400);              /* 400 microseconds */
            out8(M64_DAC_WRITE, 1);
            out8(M64_DAC_MASK, tmp);
            break;

        case ATI_CLOCK_IBMRGB514:
            /*
             * Here, only update in-core data.  It will be written out later by
             * ATIRGB514Set().
             */
            tmp = (pATIHW->clock << 1) + 0x20U;
            pATIHW->ibmrgb514[tmp] =
                (SetBits(N, 0x3FU) | SetBits(D, 0xC0U)) ^ 0xC0U;
            pATIHW->ibmrgb514[tmp + 1] = SetBits(M, 0x3FU);
            break;

        default:
            break;
    }

    (void)in8(M64_DAC_WRITE);    /* Clear DAC counter */

    /* Restore register */
    if (!(crtc_gen_cntl & CRTC_EXT_DISP_EN))
        outr(CRTC_GEN_CNTL, crtc_gen_cntl);
}