dg_utils.c 26.8 KB
1 2 3 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 502 503 504 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 567 568 569 570 571 572 573 574 575 576 577 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 633 634 635 636 637 638 639 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 770 771 772 773 774 775 776 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 844 845 846 847 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
/*
 * Copyright (c) 1995, Silicon Graphics, Inc.  All Rights Reserved.
 *
 * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
 * the contents of this file may not be disclosed to third parties, copied
 * or duplicated in any form, in whole or in part, without the prior written
 * permission of Silicon Graphics, Inc.
 *
 * RESTRICTED RIGHTS LEGEND:
 * Use, duplication or disclosure by the Government is subject to restrictions
 * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
 * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
 * successor clauses in the FAR, DOD or NASA FAR Supplement.  Unpublished
 * rights reserved under the Copyright Laws of the United States.
 *
 * Module: dg_utils.c:	Functions of convenience for diag writers.
 */

#include <stdio.h>
#include <stdarg.h>
#include <unistd.h>
#include <sys/types.h>
#ifdef __sgi__
#include <bstring.h>
#endif
#include <stdlib.h>
#include <string.h>
#include <netinet/in.h>

#include "diag.h"
#include "dbg_comm.h"

#define AUTOCC        1

static void WriteCC(unsigned char, unsigned char);

unsigned int pdw_mem;
unsigned int ccreg_address; /* DWORD address of the CCTL reg of the RDRAM
			     being initialized in the application space */
static int InitCCValue(void);
#define	PHYS_TO_K1(x)	((x)|0xA0000000)	/* physical to kseg1 */

int dgListSubTests(TEST_REF *test_refs) 
{
    int testIndex;

    errlog(INFO, "Available Subtests for %s:", ideTestName);
    for (testIndex = 0; test_refs[testIndex].num != TEST_NULL; testIndex++) {
	errlog(INFO, "(%d)\t%s", test_refs[testIndex].num, 
	  test_refs[testIndex].name);
    }
}

#define MANUFACTURE_NEC 0x00000500
#define MANUFACTURE_TOSHIBA 0x00000200
#define NEC_RELEASE_C2

#ifdef NEC_RELEASE_C2
/* NEC Rev C2 mode enable/disable */
#define RDRAM_ENABLE  0xc2000000
#define RDRAM_DISABLE 0xc0000000
#else
/* Non NEC Rev C2 mode enable/disable */
#define RDRAM_ENABLE  0xc6000000
#define RDRAM_DISABLE 0xc4000000
#endif


int
dgInitRdram(void) {
    unsigned int read_data;
    unsigned int read_data1;
    unsigned int next_device;
    unsigned int next_id;
    unsigned int next_reg;
    unsigned int next_1_id;
    unsigned int next_1_mem;
    unsigned int next_1_reg;
    unsigned int next_2_id;
    unsigned int next_2_reg;
    unsigned int next_2_mem;
    unsigned int device_type[8];
    unsigned int cc_value[8];
    unsigned int reg_step;
    unsigned int reg_max;
    unsigned int multibank;
    unsigned int cc;
    int i;


    /* Check if RI was previously initialized by reading a register that
     *  must be non-zero if the RDRAM was configured.
     */
    errlog(DEBUG, "Initializing RDRAM");
    dgReadWord(0x0470000c, &read_data);
    if (read_data) {
	/* the RI has already been initialized */
	if (dgIORevision == 1) {
	    /* reset of initialized RI not supported */
	    return 0;
	} else {
	    /* reset RI to its power on state */
	    dgWriteWord(0x04700000, 0x0000000e);
	    dgWriteWord(0x04700004, 0x00000000);
	    dgWriteWord(0x0470000c, 0x00000000);
	    dgWriteWord(0x04700010, 0x00000000);
	    dgWriteWord(0x04700014, 0x0000000f);
	    dgWriteWord(0x04700018, 0x00000000);
	    dgWriteWord(0x0470001c, 0x00000000);
	}
    }

    /* write current control */
    if (dgIORevision == 1) 
	    dgWriteWord(0x04700004, 0x00000010);
    else
	    dgWriteWord(0x04700004, 0x00000040);

    /* delay 32 cycles for current control */
    dgWriteWord(0x04300000, 0x00000000);
    dgWriteWord(0x04300000, 0x00000000);
    dgWriteWord(0x04300000, 0x00000000);

    /* load current control */
    dgWriteWord(0x04700008, 0x00000000);

    /* load select register */
    dgWriteWord(0x0470000c, 0x00000014);

    /* enable RDRAM reset mode */
    dgWriteWord(0x04700000, 0x00000000);

    /* delay 80 cycles for RDRAM reset */
    dgWriteWord(0x04300000, 0x00000000);
    dgWriteWord(0x04300000, 0x00000000);
    dgWriteWord(0x04300000, 0x00000000);
    dgWriteWord(0x04300000, 0x00000000);
    dgWriteWord(0x04300000, 0x00000000);
    dgWriteWord(0x04300000, 0x00000000);
    dgWriteWord(0x04300000, 0x00000000);

    /* enable standby mode */
    dgWriteWord(0x04700000, 0x0000000e);

    /* enable MI init mode */
    dgWriteWord(0x04300000, 0x0000010f);

    /* write RDRAM Delay register */
    dgWriteWord(0x03f80008, 0x18082838);

    /* write RDRAM RefRow register */
    dgWriteWord(0x03f80014, 0x00000000);

    /* Move all RDRAMs to the top of the address space */
    dgWriteWord(0x03f80004, 0x80000000);

    next_id = 0;
    next_reg = 0x03f00000;
    next_1_id = 0;
    next_1_reg = 0x03f00000;
    next_1_mem = 0;
    next_2_id = 0;
    next_2_reg = 0x03f00000;
    next_2_mem = 0;
    multibank = 0;
    pdw_mem = 0;

    if (dgIORevision == 1) {
	reg_step = 0x00000200;
	reg_max = 0x03f04000;
    } else {
	reg_step = 0x00000400;
	reg_max = 0x03f08000;
    }

    for (next_device = 0; next_device < 8; next_device++) {
	/* move the next RDRAM to the next 2 MByte slot */
	dgWriteWord(reg_max + 0x4, next_id);

	/* enable the RDRAM */
	ccreg_address = next_reg + 0xc;
	cc = InitCCValue();
	if (cc == 0)
		break;
	cc_value[next_device] = cc;
  
	/* determine the RDRAM size */
	dgWriteWord(0x04300000, 0x00002000);
	dgReadWord(next_reg + 0x0, &read_data);
	device_type[next_device] = read_data & 0xf0ff0000;
	dgWriteWord(0x04300000, 0x00001000);
  

	switch (device_type[next_device]) {
	case 0xb0090000 :
	    pdw_mem = pdw_mem + 0x00100000;
            errlog(DEBUG, "Found 1 Mbyte RDRAM");
	    break;

	case 0xb0190000 :
	    next_1_id = next_1_id + 0x08000000;
	    next_1_reg = next_1_reg + (reg_step * 2);
	    next_1_mem = next_1_mem + 0x00200000;
	    pdw_mem = pdw_mem + 0x00200000;
            multibank = (multibank << 1) + 1;
            errlog(DEBUG, "Found 2 Mbyte RDRAM");
	    break;

	default :
	    errlog(ERR_SEVERE, "Unsupported RDRAM size = 0x%x",
		   device_type[next_device]);
	}

	dgWriteWord(0x04300000, 0x00002000);
	dgReadWord(next_reg + 0x24, &read_data);
	dgReadWord(next_reg + 0x0, &read_data1);
	dgWriteWord(0x04300000, 0x00001000);
  
	if (((read_data & 0x0000ffff) == MANUFACTURE_NEC)
	    && ((read_data1 & 0x01000000) == 0)) {
	    errlog(DEBUG, "NEC RDRAM", read_data);
	    dgWriteWord(next_reg + 0x18, 0x101c0a04);
	} else if ((read_data & 0x0000ffff) == MANUFACTURE_TOSHIBA) {
	    errlog(DEBUG, "Toshiba RDRAM", read_data);
	    dgWriteWord(next_reg + 0x18, 0x080c1204);
	} else {
	    errlog(DEBUG, "Unknown RDRAM - 0x%x", read_data);
	    errlog(DEBUG, "Using Toshiba RAS interval", read_data);
	    dgWriteWord(next_reg + 0x18, 0x080c1204);
	}

	next_id = next_id + 0x08000000;
	next_reg = next_reg + (reg_step * 2);
    }

    /* Reinitialize all the RDRAMs */
    dgWriteWord(0x03f8000c, RDRAM_DISABLE);
    dgWriteWord(0x03f80004, 0x80000000);
  
    /* Group the RDRAMs in 1 Mbyte and 2 Mbyte blocks */
    for (i = 0; i < next_device; i++) {
	switch (device_type[i]) {
	case 0xb0090000 :
	    /* Move the RDRAM to the next 1 Mbyte slot */
	    dgWriteWord(reg_max + 0x4, next_1_id);
	    ccreg_address = next_1_reg + 0xc;
            WriteCC(cc_value[i], AUTOCC);  /* Write the final auto CC value */
    
	    /* Tickle the RDRAM */
	    dgReadWord(next_1_mem + 0x00000, &read_data);
	    dgReadWord(next_1_mem + 0x80000, &read_data);
	    dgReadWord(next_1_mem + 0x00000, &read_data);
	    dgReadWord(next_1_mem + 0x80000, &read_data);
    
	    next_1_id = next_1_id + 0x04000000;
	    next_1_reg = next_1_reg + reg_step;
	    next_1_mem = next_1_mem + 0x00100000;
	    break;
    
	case 0xb0190000 :
	    /* Move the RDRAM to the next 2 Mbyte slot */
	    dgWriteWord(reg_max + 0x4, next_2_id);
	    ccreg_address = next_2_reg + 0xc;
            WriteCC(cc_value[i], AUTOCC);  /* Write the final auto CC value */
    
	    /* Tickle the RDRAM */
	    dgReadWord(next_2_mem + 0x000000, &read_data);
	    dgReadWord(next_2_mem + 0x080000, &read_data);
	    dgReadWord(next_2_mem + 0x100000, &read_data);
	    dgReadWord(next_2_mem + 0x180000, &read_data);
	    dgReadWord(next_2_mem + 0x000000, &read_data);
	    dgReadWord(next_2_mem + 0x080000, &read_data);
	    dgReadWord(next_2_mem + 0x100000, &read_data);
	    dgReadWord(next_2_mem + 0x180000, &read_data);
    
	    next_2_id = next_2_id + 0x08000000;
	    next_2_reg = next_2_reg + (reg_step * 2);
	    next_2_mem = next_2_mem + 0x00200000;
	    break;
    
	default :
	    errlog(ERR_SEVERE, "Device_type table corrupt - %x",
		   device_type[i]);
	    break;
	}
    }

    /* enable refresh */
    dgWriteWord(0x04700010, 0x63634 | (multibank << 19));

    /* display spiffy message */
    errlog(INFO, "%d Mbytes RDRAM ready", next_1_mem / 0x100000);
    return 0;
}


int
dgTestReg(unsigned int address,
	  unsigned int write_data,
	  unsigned int expect_data) {

  unsigned int read_data;

  if (dgWriteWord(address, write_data)) return(1);
  if (dgReadWord(address, &read_data)) return(1);
  if (read_data != expect_data) {
    errlog(ERR_SEVERE,
	   "data miscompare - ad = %08x, wr = %08x, rd = %08x, ex = %08x",
	   address, write_data, read_data, expect_data);
    return(1);
  } else {
    errlog(DEBUG,
	   "compare - ad = %08x, wr = %08x, rd = %08x, ex = %08x",
	   address, write_data, read_data, expect_data);
    return(0);
  }
}

int
dgTestRegMsk(unsigned int address,
          unsigned int write_data,
          unsigned int expect_data,
          unsigned int mask) {

  unsigned int read_data;

  if (dgWriteWord(address, write_data)) return(1);
  if (dgReadWord(address, &read_data)) return(1);
  read_data &= mask;
  expect_data &= mask;
  if (read_data != expect_data) {
    errlog(ERR_SEVERE,
           "data miscompare - ad = %08x, wr = %08x, rd = %08x, ex = %08x",
           address, write_data, read_data, expect_data);
    return(1);
  } else {
    errlog(DEBUG,
           "compare - ad = %08x, wr = %08x, rd = %08x, ex = %08x",
           address, write_data, read_data, expect_data);
    return(0);
  }
}

int
dgRdTestWordMsk(unsigned int address,
                unsigned int expect_data,
                unsigned int mask) {

  unsigned int read_data;

  if (dgReadWord(address, &read_data)) return(1);
  read_data &= mask;
  expect_data &= mask;
  if (read_data != expect_data) {
    errlog(ERR_SEVERE,
           "data miscompare - ad = %08x, rd = %08x, ex = %08x",
           address, read_data, expect_data);
    return(1);
  } else {
    errlog(DEBUG,
           "compare - ad = %08x, rd = %08x, ex = %08x",
           address, read_data, expect_data);
    return(0);
  }
}

void
dgWait(char *fmt, ...)
{
  char buf[1024];
  va_list args;

  /* print the message */
  va_start(args, fmt);
  printf("WAIT:   ");
  vprintf(fmt, args);
  va_end(args);

  if (isatty(0)) {
    /* only wait if reading from a tty */
    printf(" (press Enter to continue) ");
#ifdef __sgi__
    gets(buf);
#else
    fgets(buf, sizeof buf, stdin);
#endif
  }
  else {
    printf("\n");
  }
}


/******************************************************************************/
/* Toggle the interrupt line for scope trace                                  */
/******************************************************************************/
 
void
dgTrigger(void)
{
  dgWriteWord(0x0430000c, 0x00000002); /* enable sp interrupt */
  dgWriteWord(0x04040010, 0x00000010); /* set sp interrupt */
  dgWriteWord(0x04040010, 0x00000008); /* clear sp interrupt */
  }


int
dgCompareWord(unsigned int address,
	      unsigned int expect_data)
{
  unsigned int read_data;

  dgReadWord(address, &read_data);
  if (expect_data != read_data) {
    errlog(ERR_SEVERE,
	   "data miscompare - ad = %08x, wr = %08x, rd = %08x",
	   address, expect_data, read_data);
    return 1;
  }
  return 0;
}



/* video formats */
#define VI_NTSC_LPN1 0
#define VI_NTSC_LPF1 1
#define VI_NTSC_LAN1 2
#define VI_NTSC_LAF1 3
#define VI_NTSC_LPN2 4
#define VI_NTSC_LPF2 5
#define VI_NTSC_LAN2 6
#define VI_NTSC_LAF2 7
#define VI_NTSC_HPN1 8
#define VI_NTSC_HPF1 9
#define VI_NTSC_HAN1 10
#define VI_NTSC_HAF1 11
#define VI_NTSC_HPN2 12
#define VI_NTSC_HPF2 13

#define VI_PAL_LPN1  14
#define VI_PAL_LPF1  15
#define VI_PAL_LAN1  16
#define VI_PAL_LAF1  17
#define VI_PAL_LPN2  18
#define VI_PAL_LPF2  19
#define VI_PAL_LAN2  20
#define VI_PAL_LAF2  21
#define VI_PAL_HPN1  22
#define VI_PAL_HPF1  23
#define VI_PAL_HAN1  24
#define VI_PAL_HAF1  25
#define VI_PAL_HPN2  26
#define VI_PAL_HPF2  27


int
dgEnableVideo(unsigned int start_address,
	      int mode)
{
  int error_count = 0;

  /* program the VI and enable */
  dgWriteWord(0x04400004, start_address);
  dgWriteWord(0x0440000c, 0x000003ff);
  dgWriteWord(0x04400010, 0x00000000);

  /* set ntsc/pal parameters */
  switch (mode) {
  case VI_NTSC_LPN1:
  case VI_NTSC_LPF1:
  case VI_NTSC_LAN1:
  case VI_NTSC_LAF1:
  case VI_NTSC_LPN2:
  case VI_NTSC_LPF2:
  case VI_NTSC_LAN2:
  case VI_NTSC_LAF2:
  case VI_NTSC_HPN1:
  case VI_NTSC_HPF1:
  case VI_NTSC_HAN1:
  case VI_NTSC_HAF1:
  case VI_NTSC_HPN2:
  case VI_NTSC_HPF2:
    dgWriteWord(0x04400014, 0x03e52239);
    dgWriteWord(0x0440001c, 0x00000c15);
    dgWriteWord(0x04400020, 0x0c150c15);
    dgWriteWord(0x04400024, 0x007402f4);
    dgWriteWord(0x04400028, 0x00290209);
    dgWriteWord(0x0440002c, 0x00110209);
    break;

  case VI_PAL_LPN1:
  case VI_PAL_LPF1:
  case VI_PAL_LAN1:
  case VI_PAL_LAF1:
  case VI_PAL_LPN2:
  case VI_PAL_LPF2:
  case VI_PAL_LAN2:
  case VI_PAL_LAF2:
  case VI_PAL_HPN1:
  case VI_PAL_HPF1:
  case VI_PAL_HAN1:
  case VI_PAL_HAF1:
  case VI_PAL_HPN2:
  case VI_PAL_HPF2:
    dgWriteWord(0x04400014, 0x0404233a);
    dgWriteWord(0x0440001c, 0x00150c69);
    dgWriteWord(0x04400020, 0x0c6f0c6e);
    dgWriteWord(0x04400024, 0x00800300);
    dgWriteWord(0x04400028, 0x005b023b);
    dgWriteWord(0x0440002c, 0x000f026d);
    break;
  }

  /* set VI_WIDTH */
  switch (mode) {
  case VI_NTSC_LPN1:
  case VI_NTSC_LPF1:
  case VI_NTSC_LAN1:
  case VI_NTSC_LAF1:
  case VI_NTSC_LPN2:
  case VI_NTSC_LPF2:
  case VI_NTSC_LAN2:
  case VI_NTSC_LAF2:
  case VI_PAL_LPN1:
  case VI_PAL_LPF1:
  case VI_PAL_LAN1:
  case VI_PAL_LAF1:
  case VI_PAL_LPN2:
  case VI_PAL_LPF2:
  case VI_PAL_LAN2:
  case VI_PAL_LAF2:
    dgWriteWord(0x04400008, 0x00000140);
    break;

  case VI_NTSC_HPN1:
  case VI_NTSC_HAN1:
  case VI_NTSC_HPN2:
  case VI_PAL_HPN1:
  case VI_PAL_HAN1:
  case VI_PAL_HPN2:
    dgWriteWord(0x04400008, 0x00000500);
    break;

  case VI_NTSC_HPF1:
  case VI_NTSC_HAF1:
  case VI_NTSC_HPF2:
  case VI_PAL_HPF1:
  case VI_PAL_HAF1:
  case VI_PAL_HPF2:
    dgWriteWord(0x04400008, 0x00000280);
    break;
  }

  /* set VI_V_SYNC */
  switch (mode) {
  case VI_NTSC_LPN1:
  case VI_NTSC_LAN1:
  case VI_NTSC_LPN2:
  case VI_NTSC_LAN2:
    dgWriteWord(0x04400018, 0x0000020b);
    break;

  case VI_NTSC_LPF1:
  case VI_NTSC_LAF1:
  case VI_NTSC_LPF2:
  case VI_NTSC_LAF2:
  case VI_NTSC_HPN1:
  case VI_NTSC_HAN1:
  case VI_NTSC_HPN2:
  case VI_NTSC_HPF1:
  case VI_NTSC_HAF1:
  case VI_NTSC_HPF2:
    dgWriteWord(0x04400018, 0x0000020c);
    break;

  case VI_PAL_LPN1:
  case VI_PAL_LAN1:
  case VI_PAL_LPN2:
  case VI_PAL_LAN2:
    dgWriteWord(0x04400018, 0x0000026f);
    break;

  case VI_PAL_LPF1:
  case VI_PAL_LAF1:
  case VI_PAL_LPF2:
  case VI_PAL_LAF2:
  case VI_PAL_HPN1:
  case VI_PAL_HAN1:
  case VI_PAL_HPN2:
  case VI_PAL_HPF1:
  case VI_PAL_HAF1:
  case VI_PAL_HPF2:
    dgWriteWord(0x04400018, 0x00000270);
    break;
  }

  /* set VI_X_SCALE & VI_Y_SCALE */
  switch (mode) {
  case VI_NTSC_LPN1:
  case VI_NTSC_LAN1:
  case VI_NTSC_LPN2:
  case VI_NTSC_LAN2:
  case VI_PAL_LPN1:
  case VI_PAL_LAN1:
  case VI_PAL_LPN2:
  case VI_PAL_LAN2:
    dgWriteWord(0x04400030, 0x00000200);
    dgWriteWord(0x04400034, 0x00000400);
    break;

  case VI_NTSC_LPF1:
  case VI_NTSC_LAF1:
  case VI_NTSC_LPF2:
  case VI_NTSC_LAF2:
  case VI_PAL_LPF1:
  case VI_PAL_LAF1:
  case VI_PAL_LPF2:
  case VI_PAL_LAF2:
    dgWriteWord(0x04400030, 0x00000200);
    dgWriteWord(0x04400034, 0x01000400);
    break;

  case VI_NTSC_HPN1:
  case VI_NTSC_HAN1:
  case VI_NTSC_HPN2:
  case VI_PAL_HPN1:
  case VI_PAL_HAN1:
  case VI_PAL_HPN2:
    dgWriteWord(0x04400030, 0x00000400);
    dgWriteWord(0x04400034, 0x00000400);
    break;

  case VI_NTSC_HPF1:
  case VI_NTSC_HAF1:
  case VI_NTSC_HPF2:
  case VI_PAL_HPF1:
  case VI_PAL_HAF1:
  case VI_PAL_HPF2:
    dgWriteWord(0x04400030, 0x00000400);
    dgWriteWord(0x04400034, 0x02000800);
    break;
  }

  /* set VI_CTRL */
  switch (mode) {
  case VI_NTSC_LPN1:
    /* ntsc, 320 x 240, 16 bit, point-sampled, non-interlaced */
    dgWriteWord(0x04400000, 0x00003202);
    break;

  case VI_NTSC_LPF1:
    /* ntsc, 320 x 240, 16 bit, point-sampled, interlaced */
    dgWriteWord(0x04400000, 0x00003242);
    break;

  case VI_NTSC_LAN1:
    /* ntsc, 320 x 240, 16 bit, anti-aliased, non-interlaced */
    dgWriteWord(0x04400000, 0x00003112);
    break;

  case VI_NTSC_LAF1:
    /* ntsc, 320 x 240, 16 bit, anti-aliased, interlaced */
    dgWriteWord(0x04400000, 0x00003152);
    break;

  case VI_NTSC_LPN2:
    /* ntsc, 320 x 240, 32 bit, point-sampled, non-interlaced */
    dgWriteWord(0x04400000, 0x00003303);
    break;

  case VI_NTSC_LPF2:
    /* ntsc, 320 x 240, 32 bit, point-sampled, interlaced */
    dgWriteWord(0x04400000, 0x00003243);
    break;

  case VI_NTSC_LAN2:
    /* ntsc, 320 x 240, 32 bit, anti-aliased, non-interlaced */
    dgWriteWord(0x04400000, 0x00003013);
    break;

  case VI_NTSC_LAF2:
    /* ntsc, 320 x 240, 32 bit, anti-aliased, interlaced */
    dgWriteWord(0x04400000, 0x00003053);
    break;

  case VI_NTSC_HPN1:
    /* ntsc, 640 x 480, 16 bit, point-sampled, non-interlaced */
    dgWriteWord(0x04400000, 0x00003342);
    break;

  case VI_NTSC_HPF1:
    /* ntsc, 640 x 480, 16 bit, point-sampled, interlaced */
    dgWriteWord(0x04400000, 0x00003242);
    break;

  case VI_NTSC_HAN1:
    /* ntsc, 640 x 480, 16 bit, anti-aliased, non-interlaced */
    dgWriteWord(0x04400000, 0x00003052);
    break;

  case VI_NTSC_HAF1:
    /* ntsc, 640 x 480, 16 bit, anti-aliased, interlaced */
    dgWriteWord(0x04400000, 0x00003052);
    break;

  case VI_NTSC_HPN2:
    /* ntsc, 640 x 480, 32 bit, point-sampled, non-interlaced */
    dgWriteWord(0x04400000, 0x00003343);
    break;

  case VI_NTSC_HPF2:
    /* ntsc, 640 x 480, 32 bit, point-sampled, interlaced */
    dgWriteWord(0x04400000, 0x00003243);
    break;

  case VI_PAL_LPN1:
    /* pal, 320 x 240, 16 bit, point-sampled, non-interlaced */
    dgWriteWord(0x04400000, 0x00003302);
    break;

  case VI_PAL_LPF1:
    /* pal, 320 x 240, 16 bit, point-sampled, interlaced */
    dgWriteWord(0x04400000, 0x00003242);
    break;

  case VI_PAL_LAN1:
    /* pal, 320 x 240, 16 bit, anti-aliased, non-interlaced */
    dgWriteWord(0x04400000, 0x00003112);
    break;

  case VI_PAL_LAF1:
    /* pal, 320 x 240, 16 bit, anti-aliased, interlaced */
    dgWriteWord(0x04400000, 0x00003152);
    break;

  case VI_PAL_LPN2:
    /* pal, 320 x 240, 32 bit, point-sampled, non-interlaced */
    dgWriteWord(0x04400000, 0x00003303);
    break;

  case VI_PAL_LPF2:
    /* pal, 320 x 240, 32 bit, point-sampled, interlaced */
    dgWriteWord(0x04400000, 0x00003243);
    break;

  case VI_PAL_LAN2:
    /* pal, 320 x 240, 32 bit, anti-aliased, non-interlaced */
    dgWriteWord(0x04400000, 0x00003013);
    break;

  case VI_PAL_LAF2:
    /* pal, 320 x 240, 32 bit, anti-aliased, interlaced */
    dgWriteWord(0x04400000, 0x00003053);
    break;

  case VI_PAL_HPN1:
    /* pal, 640 x 480, 16 bit, point-sampled, non-interlaced */
    dgWriteWord(0x04400000, 0x00003342);
    break;

  case VI_PAL_HPF1:
    /* pal, 640 x 480, 16 bit, point-sampled, interlaced */
    dgWriteWord(0x04400000, 0x00003242);
    break;

  case VI_PAL_HAN1:
    /* pal, 640 x 480, 16 bit, anti-aliased, non-interlaced */
    dgWriteWord(0x04400000, 0x00003052);
    break;

  case VI_PAL_HAF1:
    /* pal, 640 x 480, 16 bit, anti-aliased, interlaced */
    dgWriteWord(0x04400000, 0x00003052);
    break;

  case VI_PAL_HPN2:
    /* pal, 640 x 480, 32 bit, point-sampled, non-interlaced */
    dgWriteWord(0x04400000, 0x00003343);
    break;

  case VI_PAL_HPF2:
    /* pal, 640 x 480, 32 bit, point-sampled, interlaced */
    dgWriteWord(0x04400000, 0x00003243);
    break;

  default:
    errlog(ERR_SEVERE, "unsupported video mode - %d", mode);
    return 1;
  }
  return 0;
}


/*
    ***************************************************************************
    * Copyright 1995 Rambus Inc., All Rights Reserved                         *
    * CONFIDENTIAL INFORMATION - RAMBUS INC. PROPRIETARY                      *
    *                                                                         *
    * Data contained herein is proprietary information of Rambus Inc.         *
    * which shall be treated confidentially and shall not be furnished to     *
    * third parties or made PUBLIC without prior written permission by        *
    * Rambus Inc. Rambus Inc. does not convey any license under its           *
    * patent, copyright or maskwork rights or any rights of others.           *
    *                                                                         *
    * Data contained herein is preliminary. Rambus Inc. makes no warranties,  *
    * expressed or implied, of functionality or suitability for any purpose.  *
    * Rambus Inc. assumes no obligation to correct any errors contained       *
    * herein or to advise any user of this text of any correction if such be  *
    * made.                                                                   *
    ***************************************************************************
*     July 17, 1995
*     Changes for assembly optimization: September 21, 1995
*/


/*
 * Notation:
 *  DWORD - a 32-bit integer
 *  BYTE  - a 8-bit integer
 */


/*
 * The following parameter and function definitions are application
 * dependent. They should be mapped to the appropriate initialization
 * routines for the application.
 */
 

/* 
 * The following code is an implementation of the auto current control
 * algorithm.
 */

#define DERWMASK              0x02000000
#define ASRWMASK              0x04000000
#define X2RWMASK              0x40000000
#define CERWMASK              0x80000000
#define C0RMASK               0x00000040
#define C1RMASK               0x00004000
#define C2RMASK               0x00400000
#define C3RMASK               0x00000080
#define C4RMASK               0x00008000
#define C5RMASK               0x00800000
#define C0WMASK               0x00000001
#define C1WMASK               0x00000002
#define C2WMASK               0x00000004
#define C3WMASK               0x00000008
#define C4WMASK               0x00000010
#define C5WMASK               0x00000020
#define C0SHIFT               6
#define C1SHIFT               13
#define C2SHIFT               20
#define C3SHIFT               4
#define C4SHIFT               11
#define C5SHIFT               18

#define MAXTRYS       10
#define MANUALCC      2
#define CHECKSHIFT    16
#define CCMASK        0x3f 


static int mgi_readoffset = 4;	/* dword offset for read */

void WriteCC(unsigned char b_value, unsigned char b_auto)
{
#ifdef NEC_RELEASE_C2
 unsigned int dw_value = (DERWMASK|X2RWMASK);
#else
 unsigned int dw_value = (DERWMASK|X2RWMASK|ASRWMASK);
#endif

 b_value ^= (CCMASK) ; 

 if(b_auto == AUTOCC)
  {
   dw_value |= CERWMASK ;
  }

 dw_value |= (b_value&C0WMASK)<<C0SHIFT;
 dw_value |= (b_value&C1WMASK)<<C1SHIFT;
 dw_value |= (b_value&C2WMASK)<<C2SHIFT;
 dw_value |= (b_value&C3WMASK)<<C3SHIFT;
 dw_value |= (b_value&C4WMASK)<<C4SHIFT;
 dw_value |= (b_value&C5WMASK)<<C5SHIFT;

 dgWriteWord(ccreg_address, dw_value);

 /*
  * In auto-mode, introduce a delay of 512 (Rambus bus) clocks to ensure
  * that the auto current control circuitry calibrates to the new value
  * Code this as a simple delay loop that delays for about 2048ns.
  */

 if (b_auto == AUTOCC) {
   dgWriteWord(0x04300000, 0x00000000);
  }
 
}


static void ReadCC(unsigned char * b_value)
{
 unsigned int dw_value = 0;
 unsigned int dw_tmp;

	dgWriteWord(0x04300000, 0x00002000);
	dgReadWord(ccreg_address, &dw_value);
	dgWriteWord(0x04300000, 0x00001000);


   dw_tmp = 0;
   dw_tmp |= (dw_value&C0RMASK)>>C0SHIFT;
   dw_tmp |= (dw_value&C1RMASK)>>C1SHIFT;
   dw_tmp |= (dw_value&C2RMASK)>>C2SHIFT;
   dw_tmp |= (dw_value&C3RMASK)>>C3SHIFT;
   dw_tmp |= (dw_value&C4RMASK)>>C4SHIFT;
   dw_tmp |= (dw_value&C5RMASK)>>C5SHIFT;

   *b_value = (unsigned char) dw_tmp ;
}


#define MULT  22
#define MFAC  10
#define NTRY  10
#define BITS   8
#define TRYBITS (NTRY*BITS)

static int ConvertManualToAuto(int i_ccvalue)
{
 int i_readccval = 0;
 int i_minccval = 0;
 int i_delccval = 64*MFAC*TRYBITS;
 unsigned char b_readccval = 0;
 int i_accval;
 int i_dccv;

 for(i_accval = 0; i_accval < 65 ; i_accval++)
  {
   if(i_accval > 63)
    {
	 return(0) ;
	}
   WriteCC(i_accval, AUTOCC);

    /*
     * Now read back the calibrated value. The read of this register is
     * done 2 times and the last value is the one that is used.
     * A "dummy" transaction is required (for NEC Rev D parts) for the
     * calibrated current control value to be loaded for a read.
     */

/* The first ReadCC below is a dummy transaction read: the results of
   the read are not used. In the assembly code, optimize for this case
 */
   ReadCC((unsigned char *)&b_readccval);	/* Dummy transaction */
   
   ReadCC((unsigned char *)&b_readccval);
   i_readccval = (MFAC*TRYBITS)*b_readccval;
   i_dccv = abs(i_readccval-i_ccvalue);
   if(i_dccv < i_delccval)
    {
	 i_delccval = i_dccv;
	 i_minccval = i_accval;
	}
   if(i_readccval>=i_ccvalue)
    break;
  }


 return(((i_minccval+i_accval)/2)) ;

}


static int TestCCValue(int b_value)
{
 int i_passcount = 0;
 int j, k;
 unsigned int dw_value;
 WriteCC(b_value, MANUALCC);

 for(j=0; j < NTRY ; j++)
  {
   dgWriteWord(pdw_mem, 0xffffffff);
   dgWriteWord(pdw_mem, 0xffffffff);
   dgWriteWord(pdw_mem+mgi_readoffset, 0xffffffff);

   dgReadWord(pdw_mem+mgi_readoffset, &dw_value);
   dw_value = (dw_value >>CHECKSHIFT);
     for(k = 0; k < BITS ; k++)
     {
	if((dw_value&0x00000001))
	    i_passcount++;
	dw_value = dw_value>>1;
      }
    }
 return(i_passcount);
}


static int FindCC()
{
 int i_ccvalue = 0;
 int i_prevpass = 0;
 int i_pass = 0;
 int i_sumv = 0;
 int j;
 int i_accvalue;

 for(j = 0; i_prevpass < TRYBITS ; j++)
  {
   if(j > 63)
    {
	 return(0);			/* no memory */
	}
   i_pass = TestCCValue(j);
   if(i_pass > 0)
    {
     i_sumv += j*(i_pass-i_prevpass);
     i_prevpass = i_pass ;
    }
  }
 

 i_ccvalue = MULT*(i_sumv - (TRYBITS/2)) ;
 printf("i_ccvalue %d\n", i_ccvalue);

 /*phase 2 convert manual to auto*/
 i_accvalue = ConvertManualToAuto(i_ccvalue);
 printf("i_accvalue %d\n", i_accvalue);
 return (i_accvalue);
}


/*
 * The "main" routine for initializing auto current control for a single
 * RDRAM. 
 */ 

#define	PASSES	4	/* Number of iterations of algorithm */

static int InitCCValue()

{
  int i, accv, sumccv;
  unsigned char cc = 0;

  sumccv = 0;
  for (i=0; i<PASSES; i++) {
    accv = FindCC();
    sumccv += accv;
  }
  accv = sumccv/PASSES;
  printf("accv %x\n", accv); 
  WriteCC(accv, AUTOCC);  /* Write the final auto CC value */
  ReadCC((unsigned char *)&cc);
  printf("readback cc %d\n", cc); 
  return(accv);

}
#ifdef jean
new_WriteCC()
{
	ccreg_address = 0x03f0000c;
	WriteCC(15, 2);
}
#endif