atb_setup.c 38.2 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 1072 1073 1074 1075 1076 1077 1078 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 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 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
#include "pi_util.h"
#include "atb.h"
#include "../bcp_util.h"
#define PI_DMA_DRAM_GUARD 0xabababab
#define PI_DMA_BUF_GUARD  0xcdcdcdcd

/* set up atb in different configurations for testing */

/* call this function to set up ATB and use index for test number. call
init_flash function with same index number for corresponding flash setup.
Then call dma. 
*/


void copy_entry(atb_entry *input, atb_entry *output){
  output->iv = input->iv;
  output->dev_id = input->dev_id;
  output->perm = input->perm;
  output->size_of_block = input->size_of_block;
  output->physical_address = input->physical_address;
  output->virtual_address = input->virtual_address;
}

/* debugging */
void print_entry(atb_entry *entry){
  printf("iv= %d\n", entry->iv);
  printf("dev id = %d\n",  entry->dev_id);
  printf("permission = %d\n",  entry->perm);
  printf("size of block = %08x\n",  entry->size_of_block); /* 64 MB, code = 12*/
  printf("physical address = %08x\n",  entry->physical_address);
  printf("virtual address = %08x\n",  entry->virtual_address);
  printf("page number(virtual) = %d\n", entry->start_page_number_virtual);
  printf("page number(physical) = %d\n", entry->start_page_number_physical);
  printf("number of pages = %d\n", entry->num_pages);
}
  /* create dummy IV entry for cartidge domain starting at virtual address*/
void writeDummyIVEntry(atb_table *atb_table, u32 virtual_address, int index){
 
    atb_table->atb_row[index].iv = 1;
    atb_table->atb_row[index].dev_id = DEV_0;
    atb_table->atb_row[index].perm = IV_DUMMY;
    atb_table->atb_row[index].size_of_block = 0x00000000; /* 64 MB, code = 12*/
    atb_table->atb_row[index].physical_address = 0x00000000;
    atb_table->atb_row[index].virtual_address = (virtual_address/0x4000)  -1;
}

/* index ranges from 0 to number of tests -1 */

void AtbSetup(int index, atb_table *atb_table){
  int i, entry;

  u32 atbLower, atbUpper;
  
  
  switch(index){

  case 0:
    /* Test 1: entire data is read using one table entry, repeat entries
     in whole table for binary search */
    writeDummyIVEntry(atb_table, 0x5000000, 0);
    
    /* bits 40 to 32 : pio enable = 1, dev id = 0, permission= 01, size =0010
       64 Kbytes: 0001 0001 0010 = 0x00000112*/
    atb_table->atb_row[1].iv = 0;
    atb_table->atb_row[1].dev_id = DEV_0;
    atb_table->atb_row[1].perm = DMA_READ;
    atb_table->atb_row[1].size_of_block = 0x00000000; /* 64 MB, code = 12*/
    atb_table->atb_row[1].physical_address = 0x00000000;
    //atb_table->atb_row[1].virtual_address = 0x5000000/0x4000;
    atb_table->atb_row[1].virtual_address = 0x5000000/0x4000;

    /* copy to other rows */
    for(i=2; i<PI_ATB_NUM_ENTRIES; i++){
      copy_entry(&atb_table->atb_row[1], &atb_table->atb_row[i]);
    }
    /* debugging */
    
    /* write all rows */
    for(i=0;i<PI_ATB_NUM_ENTRIES;i++){
      atbWriteStruct(&atb_table->atb_row[i],i);
    }
    break;

  case 1:
     /* Test 2: entire data is read using one table entry, repeat entries
     in whole table for binary search, 0x8000000 domain */
    writeDummyIVEntry(atb_table, 0x8000000, 0);
    
    /* bits 40 to 32 : pio enable = 1, dev id = 0, permission= 01, size =0010
       64 Kbytes: 0001 0001 0010 = 0x00000112*/
    atb_table->atb_row[1].iv = 0;
    atb_table->atb_row[1].dev_id = DEV_0;
    atb_table->atb_row[1].perm = DMA_READ;
    atb_table->atb_row[1].size_of_block = 0x00000002; /* 64K bytes*/
    atb_table->atb_row[1].physical_address = 0x00000001; /* 64 K alignment*/
    atb_table->atb_row[1].virtual_address = 0x8000000/0x4000;

    /* copy to other rows */
    for(i=2; i<PI_ATB_NUM_ENTRIES; i++){
      copy_entry(&atb_table->atb_row[1], &atb_table->atb_row[i]);
    }
        

    /* write all rows */
    for(i=0;i<PI_ATB_NUM_ENTRIES;i++){
      atbWriteStruct(&atb_table->atb_row[i],i);
    }
    break;

  case 2:
        
    writeDummyIVEntry(atb_table, 0x6000000, 0);
    
    atb_table->atb_row[1].iv = 0;
    atb_table->atb_row[1].dev_id = DEV_0;
    atb_table->atb_row[1].perm = DMA_READ;
    atb_table->atb_row[1].size_of_block = 0x00000000; 
    atb_table->atb_row[1].physical_address = 0x0000001;
    atb_table->atb_row[1].virtual_address = 0x06000000/0x4000;

   
    atb_table->atb_row[2].iv = 0;
    atb_table->atb_row[2].dev_id = DEV_0;
    atb_table->atb_row[2].perm = DMA_READ;
    atb_table->atb_row[2].size_of_block = 0x00000000; 
    atb_table->atb_row[2].physical_address = 0x00000003;
    atb_table->atb_row[2].virtual_address = (0x06000000+ (16*1024))/(0x4000);

    
    atb_table->atb_row[3].iv = 0;
    atb_table->atb_row[3].dev_id = DEV_0;
    atb_table->atb_row[3].perm = DMA_READ;
    atb_table->atb_row[3].size_of_block = 0x00000000;
    atb_table->atb_row[3].physical_address = 0x00000005;
    atb_table->atb_row[3].virtual_address = (0x06000000+ (32*1024))/(0x4000);

    
    atb_table->atb_row[4].iv = 0;
    atb_table->atb_row[4].dev_id = DEV_0;
    atb_table->atb_row[4].perm = DMA_READ;
    atb_table->atb_row[4].size_of_block = 0x00000000;
    atb_table->atb_row[4].physical_address = 0x00000007;
    atb_table->atb_row[4].virtual_address = (0x06000000+ (48*1024))/(0x4000);


    
    /* copy to other rows */
    for(i=5; i< PI_ATB_NUM_ENTRIES; i++){
      copy_entry(&atb_table->atb_row[4], &atb_table->atb_row[i]);
    }
    
    /* write all rows */
    for(i=0;i<PI_ATB_NUM_ENTRIES;i++){
      atbWriteStruct(&atb_table->atb_row[i],i);
    }
    break;

  case 3:
         
    writeDummyIVEntry(atb_table, 0x5000000, 0);
    atb_table->atb_row[1].iv = 0;
    atb_table->atb_row[1].dev_id = DEV_0;
    atb_table->atb_row[1].perm = DMA_READ;
    atb_table->atb_row[1].size_of_block = 0x00000000; 
    atb_table->atb_row[1].physical_address = 0x0000000;
    atb_table->atb_row[1].virtual_address = 0x05000000/0x4000;

   
    atb_table->atb_row[2].iv = 0;
    atb_table->atb_row[2].dev_id = DEV_0;
    atb_table->atb_row[2].perm = DMA_READ;
    atb_table->atb_row[2].size_of_block = 0x00000000; 
    atb_table->atb_row[2].physical_address = 0x00000004;
    atb_table->atb_row[2].virtual_address = (0x05000000+ (16*1024))/(0x4000);

    
    atb_table->atb_row[3].iv = 0;
    atb_table->atb_row[3].dev_id = DEV_0;
    atb_table->atb_row[3].perm = DMA_READ;
    atb_table->atb_row[3].size_of_block = 0x00000000;
    atb_table->atb_row[3].physical_address = 0x00000006;
    atb_table->atb_row[3].virtual_address = (0x05000000+ (32*1024))/(0x4000);

    
    atb_table->atb_row[4].iv = 0;
    atb_table->atb_row[4].dev_id = DEV_0;
    atb_table->atb_row[4].perm = DMA_READ;
    atb_table->atb_row[4].size_of_block = 0x00000000;
    atb_table->atb_row[4].physical_address = 0x00000008;
    atb_table->atb_row[4].virtual_address = (0x05000000+ (48*1024))/(0x4000);


    
    /* copy to other rows */
    for(i=5; i< PI_ATB_NUM_ENTRIES; i++){
      copy_entry(&atb_table->atb_row[4], &atb_table->atb_row[i]);
    }
    
    /* write all rows */
    for(i=0;i<PI_ATB_NUM_ENTRIES;i++){
      atbWriteStruct(&atb_table->atb_row[i],i);
    }
    break;


  case 4:
    writeDummyIVEntry(atb_table, 0x5000000, 0);
    /*add odd blocks mapped, for random dma tests: mapping */
    for(entry =1; entry < PI_ATB_NUM_ENTRIES; entry++){
            
      atb_table->atb_row[entry].iv = 0;
      atb_table->atb_row[entry].dev_id = DEV_0;
      atb_table->atb_row[entry].perm = DMA_READ;
      atb_table->atb_row[entry].size_of_block = 0x00000000; 
      atb_table->atb_row[entry].physical_address = 0x00000001 + 2*(entry-1);
      atb_table->atb_row[entry].virtual_address = (0x05000000 + ((entry-1)*16*1024))/(0x4000);
    }

    
    /* write all rows */
    for(i=0;i<PI_ATB_NUM_ENTRIES;i++){
      atbWriteStruct(&atb_table->atb_row[i],i);
    }
    break;
  case 5:
    writeDummyIVEntry(atb_table, 0x5000000, 0);
    /*add even blocks mapped, for random dma tests: mapping device rotation */
    for(entry =1; entry < PI_ATB_NUM_ENTRIES; entry++){
      
      atb_table->atb_row[entry].iv = 0;
      atb_table->atb_row[entry].dev_id = entry %4;/* rotate device id*/
      
      atb_table->atb_row[entry].perm = DMA_READ;
      atb_table->atb_row[entry].size_of_block = 0x00000000; 
      atb_table->atb_row[entry].physical_address = 0x00000002 + 2*(entry-1);
      atb_table->atb_row[entry].virtual_address = (0x05000000 + ((entry-1)*16*1024))/(0x4000);
    }

    
    /* write all rows */
    for(i=0;i<PI_ATB_NUM_ENTRIES;i++){
      atbWriteStruct(&atb_table->atb_row[i],i);
    }
    break;

  case 6:
    /* map 4MB each as follows: in device 0,1, and device 2, 3 in latter
       halves */
    /* also test iv entry in first two atb entries: repeated. */
    writeDummyIVEntry(atb_table, 0x5000000, 0);
    writeDummyIVEntry(atb_table, 0x5000000, 1);

    atb_table->atb_row[2].iv = 0;
    atb_table->atb_row[2].dev_id = DEV_0;
      
    atb_table->atb_row[2].perm = DMA_READ;
    atb_table->atb_row[2].size_of_block = 0x00000008; /*4MB*/
    /*pick one of first two  4MB blocks in 16MB*/
    atb_table->atb_row[2].physical_address = ((rand()&0x00000001)*4*1024*1024)/(0x4000);
    /*hardcode*/
    /*
    atb_table->atb_row[2].physical_address = (0*4*1024*1024)/(0x4000);
    */
    atb_table->atb_row[2].virtual_address = (0x05000000 + 0)/(0x4000);


    atb_table->atb_row[3].iv = 0;
    atb_table->atb_row[3].dev_id = DEV_1;
      
    atb_table->atb_row[3].perm = DMA_READ;
    atb_table->atb_row[3].size_of_block = 0x00000008; /*4MB*/
    /*pick one of last two 4 4MB blocks in 16MB*/
    
    atb_table->atb_row[3].physical_address = ((rand()&0x00000001)*4*1024*1024  + (8*1024*1024))/(0x4000);
    /*hardcode*/
    /*
    atb_table->atb_row[3].physical_address = (2*4*1024*1024)/(0x4000);
    */
    /*
    atb_table->atb_row[3].virtual_address = (0x05000000 + 4*1024*1024)/(0x4000);
    */
    atb_table->atb_row[3].virtual_address = (0x05000000 + 4*1024*1024)/(0x4000);
    
    atb_table->atb_row[4].iv = 0;
    atb_table->atb_row[4].dev_id = DEV_2;
      
    atb_table->atb_row[4].perm = DMA_READ;
    atb_table->atb_row[4].size_of_block = 0x00000008; /*4MB*/
    /*pick one of 4 4MB blocks in latter 16MB*/
    
    atb_table->atb_row[4].physical_address = ((rand()&0x00000003)*4*1024*1024 + (16*1024*1024))/(0x4000);
    /*
    atb_table->atb_row[4].virtual_address = (0x05000000 + 8*1024*1024)/(0x4000);
    */
    /*
    atb_table->atb_row[4].physical_address = (28*1024*1024)/(0x4000);
    */
    atb_table->atb_row[4].virtual_address = (0x05000000 + 8*1024*1024)/(0x4000);

    
    atb_table->atb_row[5].iv = 0;
    atb_table->atb_row[5].dev_id = DEV_3;
      
    atb_table->atb_row[5].perm = DMA_READ;
    atb_table->atb_row[5].size_of_block = 0x00000008; /*4MB*/
    /*pick one of 8  4MB blocks in latter 32MB*/
    
    atb_table->atb_row[5].physical_address = ((rand()&0x00000007)*4*1024*1024 + (32*1024*1024))/(0x4000);
    /*
    atb_table->atb_row[5].physical_address = (44*1024*1024 + (32*1024*1024))/(0x4000);
    */
    /*
    atb_table->atb_row[5].virtual_address = (0x05000000 + 12*1024*1024)/(0x4000);
    */
    
    atb_table->atb_row[5].virtual_address = (0x05000000 + 12*1024*1024)/(0x4000);

   


    /* copy to other rows */
    for(i=6; i< PI_ATB_NUM_ENTRIES; i++){
      copy_entry(&atb_table->atb_row[5], &atb_table->atb_row[i]);
    }

    
    /* write all rows */
    for(i=0;i<PI_ATB_NUM_ENTRIES;i++){
      atbWriteStruct(&atb_table->atb_row[i],i);
    }

    break;


  case 7:
    /* set permission to WRITE: otherwise like case 4. */
   writeDummyIVEntry(atb_table, 0x5000000, 0);
    /*add odd blocks mapped, for random dma tests: mapping */
    for(entry =1; entry < PI_ATB_NUM_ENTRIES; entry++){
            
      atb_table->atb_row[entry].iv = 0;
      atb_table->atb_row[entry].dev_id = DEV_0;
      atb_table->atb_row[entry].perm = PIO_READ;
      atb_table->atb_row[entry].size_of_block = 0x00000000; 
      atb_table->atb_row[entry].physical_address = 0x00000001 + 2*(entry-1);
      atb_table->atb_row[entry].virtual_address = (0x05000000 + ((entry-1)*16*1024))/(0x4000);
    }

    
    /* write all rows */
    for(i=0;i<PI_ATB_NUM_ENTRIES;i++){
      atbWriteStruct(&atb_table->atb_row[i],i);
    }
    break;

  case 8:
    /* entire devices mapped out */
    /* use for ECC test */
    writeDummyIVEntry(atb_table, 0x10000000, 0);
    
    /* bits 40 to 32 : pio enable = 1, dev id = 0, permission= 01, size =0010
       64 Kbytes: 0001 0001 0010 = 0x00000112*/
    atb_table->atb_row[1].iv = 0;
    atb_table->atb_row[1].dev_id = DEV_0;
    atb_table->atb_row[1].perm = DMA_READ;
    atb_table->atb_row[1].size_of_block = 0x0000000a; /* 16 MB, code = 10*/
    atb_table->atb_row[1].physical_address = 0x00000000;
    atb_table->atb_row[1].virtual_address = 0x10000000/0x4000;

    /* bits 40 to 32 : pio enable = 1, dev id = 0, permission= 01, size =0010
       64 Kbytes: 0001 0001 0010 = 0x00000112*/
    atb_table->atb_row[2].iv = 0;
    atb_table->atb_row[2].dev_id = DEV_1;
    atb_table->atb_row[2].perm = DMA_READ;
    atb_table->atb_row[2].size_of_block = 0x0000000a; /* 16 MB, code = 10*/
    atb_table->atb_row[2].physical_address = 0x00000000;
    atb_table->atb_row[2].virtual_address = (0x10000000 + (16*1024*1024))/0x4000;

    /* bits 40 to 32 : pio enable = 1, dev id = 0, permission= 01, size =0010
       64 Kbytes: 0001 0001 0010 = 0x00000112*/
    atb_table->atb_row[3].iv = 0;
    atb_table->atb_row[3].dev_id = DEV_2;
    atb_table->atb_row[3].perm = DMA_READ;
    atb_table->atb_row[3].size_of_block = 0x0000000b; /* 32 MB, code = 10*/
    atb_table->atb_row[3].physical_address = 0x00000000;
    atb_table->atb_row[3].virtual_address = (0x10000000 + (32*1024*1024))/0x4000;

    /* bits 40 to 32 : pio enable = 1, dev id = 0, permission= 01, size =0010
       64 Kbytes: 0001 0001 0010 = 0x00000112*/
    atb_table->atb_row[4].iv = 0;
    atb_table->atb_row[4].dev_id = DEV_3;
    atb_table->atb_row[4].perm = DMA_READ;
    atb_table->atb_row[4].size_of_block = 0x0000000c; /* 64 MB, code = 10*/
    atb_table->atb_row[4].physical_address = 0x00000000;
    atb_table->atb_row[4].virtual_address = (0x10000000 + (64*1024*1024))/0x4000;

    /* copy to other rows */
    for(i=5; i<PI_ATB_NUM_ENTRIES; i++){
      copy_entry(&atb_table->atb_row[4], &atb_table->atb_row[i]);
    }
    /* debugging */
    
    /* write all rows */
    for(i=0;i<PI_ATB_NUM_ENTRIES;i++){
      atbWriteStruct(&atb_table->atb_row[i],i);
    }
    break;

  case 9:
    /* Test 1: entire data is read using one table entry, repeat entries
     in whole table for binary search */
  
    /* bits 40 to 32 : pio enable = 1, dev id = 0, permission= 01, size =0010
       64 Kbytes: 0001 0001 0010 = 0x00000112*/
    atb_table->atb_row[0].iv = 0;
    atb_table->atb_row[0].dev_id = DEV_0;
    atb_table->atb_row[0].perm = DMA_READ;
    atb_table->atb_row[0].size_of_block = 0x00000002; /* 64 MB, code = 12*/
    atb_table->atb_row[0].physical_address = 0x00000000;
    atb_table->atb_row[0].virtual_address = 0x8000000/0x4000;
    

    /* copy to other rows */
    for(i=0; i<PI_ATB_NUM_ENTRIES; i++){
      copy_entry(&atb_table->atb_row[0], &atb_table->atb_row[i]);
    }
    /* debugging */
    
    /* write all rows */
    for(i=0;i<PI_ATB_NUM_ENTRIES;i++){
      atbWriteStruct(&atb_table->atb_row[i],i);
    }
    break;
   

  case 10:
    /* entire device mapped out */
    /* use for PIO */
    writeDummyIVEntry(atb_table, 0x10000000, 0);
    
    /* bits 40 to 32 : pio enable = 1, dev id = 0, permission= 01, size =0010
       64 Kbytes: 0001 0001 0010 = 0x00000112*/
    atb_table->atb_row[1].iv = 0;
    atb_table->atb_row[1].dev_id = DEV_0;
    atb_table->atb_row[1].perm = PIO_READ;
    atb_table->atb_row[1].size_of_block = 0x0000000a; /* 16 MB, code = 10*/
    atb_table->atb_row[1].physical_address = 0x00000000;
    atb_table->atb_row[1].virtual_address = 0x10000000/0x4000;

    /* bits 40 to 32 : pio enable = 1, dev id = 0, permission= 01, size =0010
       64 Kbytes: 0001 0001 0010 = 0x00000112*/
    atb_table->atb_row[2].iv = 0;
    atb_table->atb_row[2].dev_id = DEV_1;
    atb_table->atb_row[2].perm = PIO_READ;
    atb_table->atb_row[2].size_of_block = 0x0000000a; /* 16 MB, code = 10*/
    atb_table->atb_row[2].physical_address = 0x00000000;
    atb_table->atb_row[2].virtual_address = (0x10000000 + (16*1024*1024))/0x4000;

    /* bits 40 to 32 : pio enable = 1, dev id = 0, permission= 01, size =0010
       64 Kbytes: 0001 0001 0010 = 0x00000112*/
    atb_table->atb_row[3].iv = 0;
    atb_table->atb_row[3].dev_id = DEV_2;
    atb_table->atb_row[3].perm = PIO_READ;
    atb_table->atb_row[3].size_of_block = 0x0000000b; /* 32 MB, code = 10*/
    atb_table->atb_row[3].physical_address = 0x00000000;
    atb_table->atb_row[3].virtual_address = (0x10000000 + (32*1024*1024))/0x4000;

    /* bits 40 to 32 : pio enable = 1, dev id = 0, permission= 01, size =0010
       64 Kbytes: 0001 0001 0010 = 0x00000112*/
    atb_table->atb_row[4].iv = 0;
    atb_table->atb_row[4].dev_id = DEV_3;
    atb_table->atb_row[4].perm = PIO_READ;
    atb_table->atb_row[4].size_of_block = 0x0000000c; /* 64 MB, code = 10*/
    atb_table->atb_row[4].physical_address = 0x00000000;
    atb_table->atb_row[4].virtual_address = (0x10000000 + (64*1024*1024))/0x4000;

    /* copy to other rows */
    for(i=5; i<PI_ATB_NUM_ENTRIES; i++){
      copy_entry(&atb_table->atb_row[4], &atb_table->atb_row[i]);
    }
    /* debugging */
    
    /* write all rows */
    for(i=0;i<PI_ATB_NUM_ENTRIES;i++){
      atbWriteStruct(&atb_table->atb_row[i],i);
    }
    break;

  case 11:
    /* entire devices mapped out: 128 MB */
    /* use for bit walk test for virt addr: will walk through physical
     addresses too*/
    writeDummyIVEntry(atb_table, 0x10000000, 0);
    
    /* bits 40 to 32 : pio enable = 1, dev id = 0, permission= 01, size =0010
       64 Kbytes: 0001 0001 0010 = 0x00000112*/
    atb_table->atb_row[1].iv = 0;
    atb_table->atb_row[1].dev_id = DEV_0;
    atb_table->atb_row[1].perm = DMA_READ;
    atb_table->atb_row[1].size_of_block = 0x0000000a; /* 16 MB, code = 10*/
    atb_table->atb_row[1].physical_address = 0x00000000;
    atb_table->atb_row[1].virtual_address = 0x10000000/0x4000;

    /* bits 40 to 32 : pio enable = 1, dev id = 0, permission= 01, size =0010
       64 Kbytes: 0001 0001 0010 = 0x00000112*/
    atb_table->atb_row[2].iv = 0;
    atb_table->atb_row[2].dev_id = DEV_1;
    atb_table->atb_row[2].perm = DMA_READ;
    atb_table->atb_row[2].size_of_block = 0x0000000a; /* 16 MB, code = 10*/
    atb_table->atb_row[2].physical_address = 0x00000000;
    atb_table->atb_row[2].virtual_address = (0x10000000 + (16*1024*1024))/0x4000;

    /* bits 40 to 32 : pio enable = 1, dev id = 0, permission= 01, size =0010
       64 Kbytes: 0001 0001 0010 = 0x00000112*/
    atb_table->atb_row[3].iv = 0;
    atb_table->atb_row[3].dev_id = DEV_2;
    atb_table->atb_row[3].perm = DMA_READ;
    atb_table->atb_row[3].size_of_block = 0x0000000b; /* 32 MB, code = 10*/
    atb_table->atb_row[3].physical_address = 0x00000000;
    atb_table->atb_row[3].virtual_address = (0x10000000 + (32*1024*1024))/0x4000;

    /* bits 40 to 32 : pio enable = 1, dev id = 0, permission= 01, size =0010
       64 Kbytes: 0001 0001 0010 = 0x00000112*/
    atb_table->atb_row[4].iv = 0;
    atb_table->atb_row[4].dev_id = DEV_3;
    atb_table->atb_row[4].perm = DMA_READ;
    atb_table->atb_row[4].size_of_block = 0x0000000c; /* 64 MB, code = 10*/
    atb_table->atb_row[4].physical_address = 0x00000000;
    atb_table->atb_row[4].virtual_address = (0x10000000 + (64*1024*1024))/0x4000;

    /* copy to other rows */
    for(i=5; i<PI_ATB_NUM_ENTRIES; i++){
      copy_entry(&atb_table->atb_row[4], &atb_table->atb_row[i]);
    }
    /* debugging */
    
    /* write all rows */
    for(i=0;i<PI_ATB_NUM_ENTRIES;i++){
      atbWriteStruct(&atb_table->atb_row[i],i);
    }
    break;


  case 12:
    /* entire device mapped out */
    /* use for PIO and DMA */
    writeDummyIVEntry(atb_table, 0x10000000, 0);
    
    /* bits 40 to 32 : pio enable = 1, dev id = 0, permission= 01, size =0010
       64 Kbytes: 0001 0001 0010 = 0x00000112*/
    atb_table->atb_row[1].iv = 0;
    atb_table->atb_row[1].dev_id = DEV_0;
    atb_table->atb_row[1].perm = PIO_READ | DMA_READ;
    atb_table->atb_row[1].size_of_block = 0x0000000a; /* 16 MB, code = 10*/
    atb_table->atb_row[1].physical_address = 0x00000000;
    atb_table->atb_row[1].virtual_address = 0x10000000/0x4000;

    /* bits 40 to 32 : pio enable = 1, dev id = 0, permission= 01, size =0010
       64 Kbytes: 0001 0001 0010 = 0x00000112*/
    atb_table->atb_row[2].iv = 0;
    atb_table->atb_row[2].dev_id = DEV_1;
    atb_table->atb_row[2].perm = PIO_READ | DMA_READ;
    atb_table->atb_row[2].size_of_block = 0x0000000a; /* 16 MB, code = 10*/
    atb_table->atb_row[2].physical_address = 0x00000000;
    atb_table->atb_row[2].virtual_address = (0x10000000 + (16*1024*1024))/0x4000;

    /* bits 40 to 32 : pio enable = 1, dev id = 0, permission= 01, size =0010
       64 Kbytes: 0001 0001 0010 = 0x00000112*/
    atb_table->atb_row[3].iv = 0;
    atb_table->atb_row[3].dev_id = DEV_2;
    atb_table->atb_row[3].perm = PIO_READ | DMA_READ;
    atb_table->atb_row[3].size_of_block = 0x0000000b; /* 32 MB, code = 10*/
    atb_table->atb_row[3].physical_address = 0x00000000;
    atb_table->atb_row[3].virtual_address = (0x10000000 + (32*1024*1024))/0x4000;

    /* bits 40 to 32 : pio enable = 1, dev id = 0, permission= 01, size =0010
       64 Kbytes: 0001 0001 0010 = 0x00000112*/
    atb_table->atb_row[4].iv = 0;
    atb_table->atb_row[4].dev_id = DEV_3;
    atb_table->atb_row[4].perm = PIO_READ | DMA_READ;
    atb_table->atb_row[4].size_of_block = 0x0000000c; /* 64 MB, code = 10*/
    atb_table->atb_row[4].physical_address = 0x00000000;
    atb_table->atb_row[4].virtual_address = (0x10000000 + (64*1024*1024))/0x4000;

    /* copy to other rows */
    for(i=5; i<PI_ATB_NUM_ENTRIES; i++){
      copy_entry(&atb_table->atb_row[4], &atb_table->atb_row[i]);
    }
    /* debugging */
    
    /* write all rows */
    for(i=0;i<PI_ATB_NUM_ENTRIES;i++){
      atbWriteStruct(&atb_table->atb_row[i],i);
    }
    break;

    /*case 99:*/
    /* generate random atb
       number of holes is random from 0 to 50, create table of usable space,
       allot sequentially from there...picking up first available space
       of desired size : desired size comes from virtual address alignment
       restriction
    */
    /*
    for(i=0; i < NUM_HOLES; i++){
      holes[i] = 
    */
    

  default:

    break;
  }

}

/* table lookup to get mapping from physical address to virtual address
in atb, this is used for writing data to flash: 
the eqvt will be used in hardware to read data
implement binary search to find flash block address
virtual address and physical_address are in 16 K blocks, so virtual address
may be created by masking off bottom 14 bits to get the index to lookup 
dont use this for looking up intermediate addresses
*/
void lookupVirt(u32 *physical_address, atb_table *table, atb_entry *result ){

  int index;
  for(index =0; index < PI_ATB_NUM_ENTRIES; index ++){
    if (table->atb_row[index].physical_address == *physical_address){
      copy_entry(&(table->atb_row[index]), result);
      printf("printing in lookupVirt\n");
      print_entry(result);
      break;
    }
  }
}

/* reverse: lookup flash address from virtual address : to read or write
   flash */
void lookupFlash(u32 *virt_address, atb_table *table, atb_entry *result ){

  int index;
  for(index =0; index < PI_ATB_NUM_ENTRIES; index ++){
    
    if (table->atb_row[index].virtual_address == *virt_address){
      copy_entry(&(table->atb_row[index]), result);
      printf("printing in lookupFlash\n");
      print_entry(result);
      break;
    }
  }
}

u32 twopower(u32 input){
  u32 result = 1;
  int i;
  for(i =0; i< input; i++){
    result *= 2;
  }
  return result;
}


int piAtbFillPageInfo(atb_table *table){
  int i;
  for(i=0; i< PI_ATB_NUM_ENTRIES; i++){
    table->atb_row[i].start_page_number_physical = table->atb_row[i].physical_address *32;
    table->atb_row[i].start_page_number_virtual = table->atb_row[i].virtual_address *32;
    table->atb_row[i].num_pages = twopower(table->atb_row[i].size_of_block)*16*2;
    /*last 2 comes from 1024/512 */
  }
}

/* round downwards to largest multiple of "division", not greater than input*/
u32 piFloor(u32 input, u32 division){
  u32 mod;
  mod = input % division;
  return (input - mod);
}

u32 piCeiling(u32 input, u32 division){
  u32 mod;
  mod = input % division;
  return ((input - mod) + division);
}
  
/* this converts from address to page info and supplies list of physical pages*/
int fillRequestTranslation(u32 start, u32 bytes,atb_table *table, translation *trans){
  int i,j, first_row;
  u32 consumed =0, done;
  u32 index,offset;
  u32 current_start_page;
  u32 first_phy_page;
  
  trans->start_virtual_page_number = piFloor(start, 512)/512;
  
  /* how many bytes into current page */
  trans->start_offset_from_page_start = start - (trans->start_virtual_page_number *512); 
  
  trans->start_virtual_address = piFloor(start, 16*1024)/16*1024; 
  /* 32 pages in a virtual address block */
  
  /* calculate number of pages to load */
  
  if (trans->start_offset_from_page_start == 0){
    /* no page overhead in the beginning */
    trans->num_pages =0;
    consumed =0;
  }
  else{ 
    /* account for first incomplete page */
    trans->num_pages =1;
    consumed = (u32) (512 - trans->start_offset_from_page_start); /* bytes in partial page*/
    _TRACE(DLOG, fprintf(LogFp,"in first incomplete page consumed=%d\n",consumed));
  }
  if(bytes > consumed){
    if(((bytes - consumed) % 512)!= 0){
      /* add overhead of last partial page*/
      trans->num_pages++;
      consumed += ((bytes - consumed)%512);
      _TRACE(DLOG, fprintf(LogFp,"in last incomplete page consumed=%d\n",consumed));
    }
  
  
    /*else no partial last page*/
    /* add number of full pages */
    trans->num_pages += (bytes - consumed)/512;
  }
  printf("number of pages in request=%d\n",trans->num_pages);
  /* allocate for list of page numbers*/
  trans->page_numbers = (u32 *) malloc(sizeof(u32) * (trans->num_pages+1));
    
  
  /* walk through atb and get list of page numbers */
  /*get first entry that is relevant*/
  current_start_page = trans->start_virtual_page_number;

  for(i=0; i< PI_ATB_NUM_ENTRIES-1; i++){
    if((table->atb_row[i].start_page_number_virtual == current_start_page)||(table->atb_row[i].start_page_number_virtual <= current_start_page)&&(table->atb_row[i+1].start_page_number_virtual > current_start_page)){
      /* the desired first entry */
      break;
    }
  }
  /*i points to correct entry. but if its last but one, we have to 
    disambiguate if its last or last but one*/
  if(i == (PI_ATB_NUM_ENTRIES -2)){
    if(table->atb_row[PI_ATB_NUM_ENTRIES-1].start_page_number_virtual <= current_start_page){
      i= PI_ATB_NUM_ENTRIES -1;
    }
  }
  first_row =i;
  printf(" ATB MATCH: %d\n",first_row);
  index =0;
  done=0;
  offset = trans->start_virtual_page_number - table->atb_row[first_row].start_page_number_virtual;
  first_phy_page = table->atb_row[first_row].start_page_number_physical+offset;
  trans->start_block_address = first_phy_page;
  trans->page_numbers[0]= first_phy_page;
  index++;
  /*fill in page numbers in first row*/
  for(i=1; i< table->atb_row[first_row].num_pages-offset; i++){
    if(index >= trans->num_pages) {
      done=1;
      break;
    }

    trans->page_numbers[index]  = first_phy_page +i;
    printf("page number = %d index =%d\n", trans->page_numbers[index], index);
    index++;
  }
  printf("finished first row\n");
  if(!done){
    for(i =first_row+1; i<PI_ATB_NUM_ENTRIES; i++){
      for(j= 0; j<table->atb_row[i].num_pages; j++){
	if (index >= trans->num_pages){
	  done = 1;
	}
	if(!done){
	  trans->page_numbers[index] = table->atb_row[i].start_page_number_physical + j;
	  printf("page number = %d index=%d\n", trans->page_numbers[index], index);
	  index++;
	}
      }
    }
  }    
  /* at this point all page numbers are in the page_numbers[] array*/

  return 0;
}


int LoadPageFromFlash(FILE *fp,u32 pageNum,u8 *buf)
{
    static const bytesPerPage = 528*3+10;
    int len,i,tmp;
    u32 searchPage;

    /* get close */
    fseek(fp,pageNum*bytesPerPage,SEEK_SET);

    do{
        len = getline(buf,528,fp);
        if(len==-1){
            return -1;
        }
        if(( buf[0] == buf[1])&&(buf[0] == '/')){
            searchPage = atoi(strrchr(buf,' '));
            if(searchPage>= pageNum)
                break;
        }
    } while(1);

    if(pageNum>searchPage)
        return -1;

    /* fill in buffer with bytes */
    for(i=0;i<512;i++){
        fscanf(fp,"%x",&tmp);
        buf[i]=(unsigned char)tmp;
    }

    return searchPage;
}

#if 0

void LoadPageFromFlash(FILE *fdata,u32 page_number,u8 *data)
{
    int i,j,k; 
    char dummy[256];
    fprintf(stderr,"page number = %d\n", page_number);
    fseek(fdata, 0, SEEK_SET);
    /*skip remaining pages*/
    
    for(i=0; i< page_number; i++){
      /* skip the //file xxx */
      fgets(dummy, 256, fdata);
      fprintf(stderr,"%s\n", dummy);
      for(j=0; j<512; ++j){
        fscanf(fdata,"%x",&k);
      }
      /*skip ecc */
      for(j=0;j<16;j++){
	fscanf(fdata,"%x", &k);
      }
    }
    
    /*grab correct page*/
    for(i=0; i<512; ++i){
      /* skip the //file xxx */
      fgets(dummy, 256, fdata);
      fprintf(stderr," %s\n", dummy);
      fscanf(fdata,"%x",&j);
      data[i]=(unsigned char)j;
    }
    
        
}
#endif

void LookupPhysicalPage(atb_table *table, u32 start_virtual_page, u32 *start_physical_page){

  int i, index, first_row, done, offset;
  u32 first_phy_page;
  u32 current_start_page = start_virtual_page;
  
  for(i=0; i< PI_ATB_NUM_ENTRIES-1; i++){
    if((table->atb_row[i].start_page_number_virtual == current_start_page)||(table->atb_row[i].start_page_number_virtual <= current_start_page)&&(table->atb_row[i+1].start_page_number_virtual > current_start_page)){
      /* the desired first entry */
      break;
    }
  }
  /*i points to correct entry. but if its last but one, we have to 
    disambiguate if its last or last but one*/
  if(i == (PI_ATB_NUM_ENTRIES -2)){
    if(table->atb_row[PI_ATB_NUM_ENTRIES-1].start_page_number_virtual <= current_start_page){
      i= PI_ATB_NUM_ENTRIES -1;
    }
  }
  first_row =i;

  index =0;
  done=0;
  offset = start_virtual_page - table->atb_row[first_row].start_page_number_virtual;
  first_phy_page = table->atb_row[first_row].start_page_number_physical+offset;
  *start_physical_page = first_phy_page;
  printf("location of physical page = %d\n", first_phy_page);

  return;
}

/* this is tricky, IV could be in same page or any other page depending
   on atb mapping : test both cases */

int piLoadIV(atb_table *table, translation *trans, u8 *iv, FILE *fdata){
  u8 page_data[512]; /*one page*/
  int i;
  u32 page_number;
  /* if in first page use given IV */
  if((trans->start_virtual_page_number == (0x10000000/512))||(trans->start_virtual_page_number==  (0x8000000/512))||(trans->start_virtual_page_number == (0x6000000/512))||(trans->start_virtual_page_number == (0x5000000/512))){
    /* first block, use input iv*/
    _TRACE(DLOG, fprintf(LogFp,"Using Input IV...\n"));

    return 0;
  }
  else{
    /*lookup physical page number corresponding to prev virtual page 
      number */
    LookupPhysicalPage(table, trans->start_virtual_page_number-1, &page_number);
    
    /*load previous page*/
    LoadPageFromFlash(fdata, page_number, page_data);
    for(i=0; i< 16; i++){
      iv[i] = (unsigned char) page_data[512 - 16+i];
    }
  }
  return 0;
}



void writeAsciiBinaryData(char *filename,u8 *writedata,int bytes)
{
    int i,j;
    FILE *fdata;
    if ((fdata=fopen(filename,"a"))==0) {
        printf("cannot open %s\n",filename);
        exit(0);
    }
    for(i=0; i<bytes/16; ++i) {
      for(j=0; j<16; ++j){
            fprintf(fdata,"%02x ",writedata[i*16+j]);
      }
        fprintf(fdata,"\n");
    }
    fclose(fdata);
}

void piDecryptAndExtract(translation *trans, u8 *iv,u8 *key, u8 *dma_result, u32 start, u32 num_bytes, FILE *fdata){
  int i, j, num_pages;
  int index =0;
  u8 encdata[512];
  u8 decdata[512];
  num_pages = trans->num_pages;
  
  for(i=0 ;i< num_pages; i++){  
    printf("page number ->%d\n", i);
    printf("phy page number = %d\n", trans->page_numbers[i]);
    LoadPageFromFlash(fdata, trans->page_numbers[i], encdata);
    aesDecrypt(key, iv, encdata, 512, decdata);
    writeAsciiBinaryData("debug.txt",decdata,512);
    
    
    if(i==0){
      /* first page: may need to skip some bytes*/
      for(j= trans->start_offset_from_page_start; j<512; j++){
	if(index < num_bytes){
	  dma_result[index] =(unsigned char) decdata[j];
	  index++;
	}
      }
    }
    

    else{
      /*subsequent pages: just copy until enough bytes are extracted */
      for(j=0; j<512; j++){
	if(index < num_bytes){
	  dma_result[index] = decdata[j];
	  index++;
	}
      }
    }


    for(j=0; j< 16; j++){
      iv[j] = encdata[512-16+j];
    }
  }
}
    


void startHardwareDma(
    u32 start_virt_address, 
    u32 dram_address, 
    u32 num_bytes, 
    int doEccCheck, 
    u8 * expkey, 
    u8 * iv,    u32 expected_time)
{
    /* write PI_FLASH_CTRL with 0x170083ff */
    /* with ECC ON, would use:  0x17008bff */
    int finished;
    bd_mm_present(0);
    IO_WRITE(PI_ERROR_REG, 0x0);
    if(doEccCheck ==0){
        IO_WRITE(PI_FLASH_CTRL_REG,0x1f0083ff);
    }
    else{
        IO_WRITE(PI_FLASH_CTRL_REG,0x1f008bff);
    }
    
    /* generate and write ekey to be used during this test */
    
    ioWriteBuffer(expkey,44*4,PI_AES_EKEY_REG);
    
    /* write iv to be used during test */
    ioWriteBuffer(iv,16,PI_BUFFER_0_START+(PI_AES_INIT_INDX16<<4));
    
    /* write PI_AES_CTRL with (PI_AES_IV_INDX16<<1) */
    IO_WRITE(PI_AES_CTRL_REG,(PI_AES_INIT_INDX16<<1));
    
    IO_WRITE(PI_DRAM_ADDR_REG, dram_address);
    IO_WRITE(PI_CART_ADDR_REG, start_virt_address);
    IO_WRITE(PI_WR_LEN_REG,num_bytes-1);
}

int runHardware(u32 start_virt_address, u32 dram_address, u32 num_bytes, int doEccCheck, u8 * expkey, u8 * iv, u32 expected_time){
  /* write PI_FLASH_CTRL with 0x170083ff */
  /* with ECC ON, would use:  0x17008bff */
  int finished;
  V_INT vd={0,0};

  IO_WRITE(PI_STATUS_REG,0);

    /* write guard band at begin and end */
  vd.data_part=PI_DMA_DRAM_GUARD;
  BD_V_IO_SWRITE_WORD(dram_address&(~0xf),BD_REQ_NO_IPC_MSG,&vd);
  BD_V_IO_SWRITE_WORD((dram_address&(~0xf))+4,BD_REQ_NO_IPC_MSG,&vd);
  BD_V_IO_SWRITE_WORD((dram_address&(~0xf))+8,BD_REQ_NO_IPC_MSG,&vd);
  BD_V_IO_SWRITE_WORD((dram_address&(~0xf))+12,BD_REQ_NO_IPC_MSG,&vd);
  BD_V_IO_SWRITE_WORD((dram_address+num_bytes)&(~0xf),BD_REQ_NO_IPC_MSG,&vd);
  BD_V_IO_SWRITE_WORD(((dram_address+num_bytes)&(~0xf))+4,
                        BD_REQ_NO_IPC_MSG,&vd);
  BD_V_IO_SWRITE_WORD(((dram_address+num_bytes)&(~0xf))+8,
                        BD_REQ_NO_IPC_MSG,&vd);
  BD_V_IO_SWRITE_WORD(((dram_address+num_bytes)&(~0xf))+12,
                        BD_REQ_NO_IPC_MSG,&vd);
    
  bd_mm_present(0);
  /* keep the masks, zero everything else */

  IO_WRITE(PI_ERROR_REG, (IO_READ(PI_ERROR_REG) & 0xc0000000));
  if(doEccCheck ==0){
    IO_WRITE(PI_FLASH_CTRL_REG,0x1f0083ff);
  }
  else{
    IO_WRITE(PI_FLASH_CTRL_REG,0x1f008bff);
  }
  
  /* generate and write ekey to be used during this test */
    
  ioWriteBuffer(expkey,44*4,PI_AES_EKEY_REG);

  /* write iv to be used during test */
  ioWriteBuffer(iv,16,PI_BUFFER_0_START+(PI_AES_INIT_INDX16<<4));

    /* write PI_AES_CTRL with (PI_AES_IV_INDX16<<1) */
  IO_WRITE(PI_AES_CTRL_REG,(PI_AES_INIT_INDX16<<1));

  IO_WRITE(PI_DRAM_ADDR_REG, dram_address);
  IO_WRITE(PI_CART_ADDR_REG, start_virt_address);
  IO_WRITE(PI_WR_LEN_REG,num_bytes-1);

  finished = pollDMA(expected_time);
  /* finished =0 and 1 means done, 2 means not done */
  if(finished == 2 ){
    IO_WRITE(PI_STATUS_REG,PI_STATUS_RESET);
  }
  return finished;
}


u32  runPIOHardware(u32 start_virt_address, u32 dram_address, u32 num_bytes, int doEccCheck, u8 * expkey, u8 * iv, u32 expected_time){
  
  u32 val;

  /* write PI_FLASH_CTRL with 0x170083ff */
  /* with ECC ON, would use:  0x17008bff */
  bd_mm_present(0);
  IO_WRITE(PI_ERROR_REG, (IO_READ(PI_ERROR_REG) & 0xc0000000));
  if(doEccCheck ==0){
    IO_WRITE(PI_FLASH_CTRL_REG,0x1f0083ff);
  }
  else{
    IO_WRITE(PI_FLASH_CTRL_REG,0x1f008bff);
  }
  
  /* generate and write ekey to be used during this test */
    
  ioWriteBuffer(expkey,44*4,PI_AES_EKEY_REG);

  /* write iv to be used during test */
  ioWriteBuffer(iv,16,PI_BUFFER_0_START+(PI_AES_INIT_INDX16<<4));

    /* write PI_AES_CTRL with (PI_AES_IV_INDX16<<1) */
  IO_WRITE(PI_AES_CTRL_REG,(PI_AES_INIT_INDX16<<1));
  IO_WRITE(PI_CART_ADDR_REG, start_virt_address +10);
  
  val = IO_READ(start_virt_address);
  printf("value=%x\n", val);
  
  return val;
}

#if 0      
void pollDMA(long long expected_time){   
  long long new_st, old_st;
  u32 error;
  /*POLL_DMA_STATUS;*/
  SIM_TIME(&old_st);
  fprintf(stderr,"dma issued\n");
  do{
    SIM_TIME(&new_st);
    if(new_st %10000 == 0){
      error = IO_READ(PI_ERROR_REG);
      fprintf(stderr,"error = %08x\n");
    }
  }while((((IO_READ(PI_STATUS_REG))&PI_STATUS_DMA_BUSY))&&((new_st - old_st)< expected_time));
  fprintf(stderr,"dma stopped at %lld\n",new_st);
  fprintf(stderr,"time taken = %lld\n", new_st - old_st);
  if((IO_READ(PI_STATUS_REG)&PI_STATUS_DMA_BUSY)==0){
    fprintf(stderr,"dma finished!\n");
  }
  return;
}
    

#endif
int verifyResultinDRAMdup(u8 *data, u32 dram_addr, u32 num_bytes){
  int i;
  int result=0;
  u32 new_num_bytes;
  u8 *testbuf = (u8 *) malloc(sizeof(u8)*num_bytes);
  u8 *newtestbuf;
  /* PIO read from buffer to verify */
  /* if not multiple of 4, pad it for IORead, but verify only num_bytes*/
  if((num_bytes %4) != 0){
    new_num_bytes = num_bytes -(num_bytes %4) +4;
  }
  else{
    new_num_bytes = num_bytes;
  }
  if(dram_addr % 4 == 0){
   /* read back  */
    ioReadBuffer(testbuf, new_num_bytes, dram_addr);
  /*
    for(i=0;i<num_bytes;i+=4){
      printf(" %x ",IO_READ(dram_addr+i));
    }
    printf("reading done for result\n");
*/
  }
  else{
    /* its a multiple of 2, but not 4 */
    newtestbuf = (u8 *) malloc(sizeof(u8)*(new_num_bytes + 2));
    ioReadBuffer(newtestbuf, new_num_bytes+2, dram_addr -2);
    for(i=0;i<num_bytes; i++){
      testbuf[i] = newtestbuf[i+2];
    }
    free(newtestbuf);
  }
  
  for(i=0; i< num_bytes; i++){
    printf("%d %02x %02x\n", i, testbuf[i], data[i]);
  }
  

  for(i=0;i<num_bytes;i++){
    
    if(testbuf[i] != data[i]){
      result = 1;
      break;
    }
  }


  if(result==0){
    result = checkDramStartGuardBand(dram_addr);
  }

  if(result==0){
    result = checkDramEndGuardBand(dram_addr+num_bytes);
  }

  if(result == 1){
    free(testbuf);
    return 1;
  }
  else{
    free(testbuf);
    _TRACE(DLOG, fprintf(LogFp,"Compared Correctly!\n"));

    return 0;
  }
}