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#include <mainboard.h>
30#include <console.h>
31#include <string.h>
32#include <mtrr.h>
33#include <macros.h>
34#include <spd_ddr2.h>
35#include <cpu.h>
36#include <msr.h>
37#include <amd/k8/k8.h>
38#include <amd/k8/sysconf.h>
39#include <device/pci.h>
40#include <pci_ops.h>
41#include <mc146818rtc.h>
42#include <lib.h>
43
44#include <spd_ddr2.h>
45
46#warning where to we define supported DIMM types
47#define DIMM_SUPPORT 0x0104
48
49
50inline void print_raminit(const char *strval, u32 val)
51{
52 printk(BIOS_DEBUG, "%s%08x\n", strval, val);
53}
54
55#if 0
56#if defined(CONFIG_LB_MEM_TOPK) & ((CONFIG_LB_MEM_TOPK -1) != 0)
57# error "CONFIG_LB_MEM_TOPK must be a power of 2"
58#endif
59#endif
60
61
62u32 pci_read_config32_index(u32 dev, u32 index_reg, u32 index)
63{
64 u32 dword;
65
66 pci_conf1_write_config32(dev, index_reg, index);
67
68 dword = pci_conf1_read_config32(dev, index_reg+0x4);
69
70 return dword;
71}
72
73void pci_write_config32_index(u32 dev, u32 index_reg, u32 index, u32 data)
74{
75
76 pci_conf1_write_config32(dev, index_reg, index);
77
78 pci_conf1_write_config32(dev, index_reg + 0x4, data);
79
80}
81
82u32 pci_read_config32_index_wait(u32 dev, u32 index_reg, u32 index)
83{
84
85 u32 dword;
86
87 index &= ~(1<<30);
88 pci_conf1_write_config32(dev, index_reg, index);
89
90 do {
91 dword = pci_conf1_read_config32(dev, index_reg);
92 } while (!(dword & (1<<31)));
93
94 dword = pci_conf1_read_config32(dev, index_reg+0x4);
95
96 return dword;
97}
98
99void pci_write_config32_index_wait(u32 dev, u32 index_reg, u32 index, u32 data)
100{
101
102 u32 dword;
103
104 pci_conf1_write_config32(dev, index_reg + 0x4, data);
105
106 index |= (1<<30);
107 pci_conf1_write_config32(dev, index_reg, index);
108 do {
109 dword = pci_conf1_read_config32(dev, index_reg);
110 } while (!(dword & (1<<31)));
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
141int controller_present(const struct mem_controller *ctrl)
142{
143 return pci_conf1_read_config32(ctrl->f0, 0) == 0x11001022;
144}
145
146void sdram_set_registers(const struct mem_controller *ctrl, struct sys_info *sysinfo)
147{
148 static const struct rmap register_values[] = {
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 PCM(0, 0x18, 1, 0x44, 0x0000f8f8, 0x00000000),
178 PCM(0, 0x18, 1, 0x4C, 0x0000f8f8, 0x00000001),
179 PCM(0, 0x18, 1, 0x54, 0x0000f8f8, 0x00000002),
180 PCM(0, 0x18, 1, 0x5C, 0x0000f8f8, 0x00000003),
181 PCM(0, 0x18, 1, 0x64, 0x0000f8f8, 0x00000004),
182 PCM(0, 0x18, 1, 0x6C, 0x0000f8f8, 0x00000005),
183 PCM(0, 0x18, 1, 0x74, 0x0000f8f8, 0x00000006),
184 PCM(0, 0x18, 1, 0x7C, 0x0000f8f8, 0x00000007),
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 PCM(0, 0x18, 1, 0x40, 0x0000f8fc, 0x00000000),
216 PCM(0, 0x18, 1, 0x48, 0x0000f8fc, 0x00000000),
217 PCM(0, 0x18, 1, 0x50, 0x0000f8fc, 0x00000000),
218 PCM(0, 0x18, 1, 0x58, 0x0000f8fc, 0x00000000),
219 PCM(0, 0x18, 1, 0x60, 0x0000f8fc, 0x00000000),
220 PCM(0, 0x18, 1, 0x68, 0x0000f8fc, 0x00000000),
221 PCM(0, 0x18, 1, 0x70, 0x0000f8fc, 0x00000000),
222 PCM(0, 0x18, 1, 0x78, 0x0000f8fc, 0x00000000),
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 PCM(0, 0x18, 2, 0x40, 0xe007c018, 0x00000000),
249 PCM(0, 0x18, 2, 0x44, 0xe007c018, 0x00000000),
250 PCM(0, 0x18, 2, 0x48, 0xe007c018, 0x00000000),
251 PCM(0, 0x18, 2, 0x4C, 0xe007c018, 0x00000000),
252 PCM(0, 0x18, 2, 0x50, 0xe007c018, 0x00000000),
253 PCM(0, 0x18, 2, 0x54, 0xe007c018, 0x00000000),
254 PCM(0, 0x18, 2, 0x58, 0xe007c018, 0x00000000),
255 PCM(0, 0x18, 2, 0x5C, 0xe007c018, 0x00000000),
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271 PCM(0, 0x18, 2, 0x60, 0xe007c01f, 0x00000000),
272 PCM(0, 0x18, 2, 0x64, 0xe007c01f, 0x00000000),
273 PCM(0, 0x18, 2, 0x68, 0xe007c01f, 0x00000000),
274 PCM(0, 0x18, 2, 0x6C, 0xe007c01f, 0x00000000),
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 PCM(0, 0x18, 2, 0x78, 0xfff80000, (6<<4)|(6<<0)),
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 PCM(0, 0x18, 2, 0x80, 0xffff0000, 0x00000000),
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 PCM(0, 0x18, 2, 0x88, 0x000004c8, 0xff000002 ),
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 PCM(0, 0x18, 2, 0x8c, 0x000c008f, (2 << 16)|(1 << 8)),
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 PCM(0, 0x18, 2, 0x90, 0xfff6004c, 0x00000010),
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 PCM(0, 0x18, 2, 0x94, 0x00a82f00,0x00008000),
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 PCM(0, 0x18, 2, 0xa0, 0x00fffc00, 0xff000000),
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 PCM(0, 0x18, 3, 0x58, 0xffe0e0e0, 0x00000000),
637
638
639
640
641
642
643
644
645 PCM(0, 0x18, 3, 0x5C, 0x0000003e, 0x00000000),
646
647
648
649
650
651 PCM(0, 0x18, 3, 0x60, 0xffffff00, 0x00000000),
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 int i;
678 int max;
679
680 if (!controller_present(ctrl)) {
681 sysinfo->ctrl_present[ctrl->node_id] = 0;
682 return;
683 }
684 sysinfo->ctrl_present[ctrl->node_id] = 1;
685
686 printk(BIOS_SPEW, "setting up CPU 0x%02x northbridge registers\n", ctrl->node_id);
687
688
689 max = ARRAY_SIZE(register_values);
690 for(i = 0; i < max; i++) {
691 struct rmap const *rm = ®ister_values[i];
692 u32 dev;
693 unsigned where;
694 unsigned long reg;
695 dev = PCI_BDF(rm->pcm.bus, rm->pcm.dev, rm->pcm.fn);
696 dev -= PCI_BDF(0, 0x18, 0);
697 dev += ctrl->f0;
698
699 where = rm->pcm.reg;
700 reg = pci_conf1_read_config32(dev, where);
701 reg &= rm->pcm.and;
702 reg |= rm->pcm.or;
703 pci_conf1_write_config32(dev, where, reg);
704 }
705
706 printk(BIOS_SPEW, "done.\n");
707}
708
709int is_dual_channel(const struct mem_controller *ctrl)
710{
711 u32 dcl;
712 dcl = pci_conf1_read_config32(ctrl->f2, DRAM_CONFIG_LOW);
713 return dcl & DCL_Width128;
714}
715
716
717int is_opteron(const struct mem_controller *ctrl)
718{
719
720
721
722
723
724#warning "FIXME implement a better test for opterons"
725 u32 nbcap;
726 nbcap = pci_conf1_read_config32(ctrl->f3, NORTHBRIDGE_CAP);
727 return !!(nbcap & NBCAP_128Bit);
728}
729
730int is_registered(const struct mem_controller *ctrl)
731{
732
733
734
735
736 u32 dcl;
737 dcl = pci_conf1_read_config32(ctrl->f2, DRAM_CONFIG_LOW);
738 return !(dcl & DCL_UnBuffDimm);
739}
740
741void spd_get_dimm_size(unsigned device, struct dimm_size *sz)
742{
743
744 int value;
745 sz->per_rank = 0;
746 sz->rows = 0;
747 sz->col = 0;
748 sz->rank = 0;
749
750 value = spd_read_byte(device, SPD_ROW_NUM);
751 if (value < 0) goto hw_err;
752 if ((value & 0xff) == 0) goto val_err;
753 sz->per_rank += value & 0xff;
754 sz->rows = value & 0xff;
755 printk(BIOS_SPEW, "%d rows\n", sz->rows);
756
757 value = spd_read_byte(device, SPD_COL_NUM);
758 if (value < 0) goto hw_err;
759 if ((value & 0xff) == 0) goto val_err;
760 sz->per_rank += value & 0xff;
761 sz->col = value & 0xff;
762 printk(BIOS_SPEW, "%d pre_rank %d col\n", sz->per_rank, sz->col);
763
764 value = spd_read_byte(device, SPD_BANK_NUM);
765 if (value < 0) goto hw_err;
766 if ((value & 0xff) == 0) goto val_err;
767 sz->bank = log2c(value & 0xff);
768 printk(BIOS_SPEW, "%d SPD banks %d bank\n", value, sz->bank);
769 sz->per_rank += sz->bank;
770 printk(BIOS_SPEW, "sz->per_rank is now %d\n", sz->per_rank);
771
772 value = spd_read_byte(device, SPD_DATA_WIDTH);
773 if (value < 0) goto hw_err;
774 value &= 0xff;
775 if ((value != 72) && (value != 64)) goto val_err;
776
777 sz->per_rank += log2f(value) - 3;
778 printk(BIOS_SPEW, "value %d log2f(value) %d sz->per_rank now %d\n", value, log2f(value), sz->per_rank);
779
780
781 value = spd_read_byte(device, SPD_MOD_ATTRIB_RANK);
782 if (value < 0) goto hw_err;
783
784 value &= SPD_MOD_ATTRIB_RANK_NUM_MASK;
785 value += SPD_MOD_ATTRIB_RANK_NUM_BASE;
786 printk(BIOS_SPEW, "# ranks %d\n", value);
787
788
789
790
791
792
793 if ((value != 1) && (value != 2) && (value != 4 )) {
794 goto val_err;
795 }
796 sz->rank = value;
797
798
799
800
801 value = spd_read_byte(device, SPD_RANK_SIZE);
802 if (value < 0) goto hw_err;
803 value &= 0xff;
804 printk(BIOS_SPEW, "spd rank size is %d\n", value);
805 value = log2f(value);
806 if(value <=4 ) value += 8;
807 value += (27-5);
808 printk(BIOS_SPEW, " computed value is %d\n", value);
809 if( value != (sz->per_rank)) {
810 printk(BIOS_ERR, "Bad RANK Size -- value is 0x%x, and it should be 0x%x\n", value, sz->per_rank);
811 printk(BIOS_ERR, "This error has been reduced to a warning for now\n");
812
813 }
814
815 goto out;
816
817 val_err:
818 die("Bad SPD value\n");
819
820hw_err:
821 sz->per_rank = 0;
822 sz->rows = 0;
823 sz->col = 0;
824 sz->bank = 0;
825 sz->rank = 0;
826 out:
827 return;
828}
829
830void set_dimm_size(const struct mem_controller *ctrl, struct dimm_size *sz, unsigned index, int is_Width128)
831{
832 u32 base0, base1;
833
834
835
836
837
838
839
840
841 base0 = base1 = 0;
842
843
844 if (sz->per_rank >= 27) {
845 base0 = (1 << ((sz->per_rank - 27 ) + 19)) | 1;
846 }
847
848
849 if (sz->rank > 1) {
850 base1 = (1 << ((sz->per_rank - 27 ) + 19)) | 1;
851 }
852
853
854 if (is_Width128) {
855 base0 = (base0 << 1) | (base0 & 1);
856 base1 = (base1 << 1) | (base1 & 1);
857 }
858
859
860 base0 &= ~0xe007fffe;
861 base1 &= ~0xe007fffe;
862
863
864 pci_conf1_write_config32(ctrl->f2, DRAM_CSBASE + (((index << 1)+0)<<2), base0);
865 pci_conf1_write_config32(ctrl->f2, DRAM_CSBASE + (((index << 1)+1)<<2), base1);
866#ifdef QRANK_DIMM_SUPPORT
867 if(sz->rank == 4) {
868 pci_conf1_write_config32(ctrl->f2, DRAM_CSBASE + (((index << 1)+4)<<2), base0);
869 pci_conf1_write_config32(ctrl->f2, DRAM_CSBASE + (((index << 1)+5)<<2), base1);
870 }
871#endif
872
873
874 if (base0) {
875 u32 dword;
876 u32 ClkDis0;
877#if CPU_SOCKET_TYPE == SOCKET_L1
878 ClkDis0 = DTL_MemClkDis0;
879#else
880 #if CPU_SOCKET_TYPE == SOCKET_AM2
881 ClkDis0 = DTL_MemClkDis0_AM2;
882 #endif
883#endif
884
885 dword = pci_conf1_read_config32(ctrl->f2, DRAM_TIMING_LOW);
886 dword &= ~(ClkDis0 >> index);
887#ifdef QRANK_DIMM_SUPPORT
888 if(sz->rank == 4) {
889 dword &= ~(ClkDis0 >> (index+2));
890 }
891#endif
892 pci_conf1_write_config32(ctrl->f2, DRAM_TIMING_LOW, dword);
893
894 if (is_Width128) {
895 dword = pci_conf1_read_config32(ctrl->f2, DRAM_CTRL_MISC);
896 dword &= ~(ClkDis0 >> index);
897#ifdef QRANK_DIMM_SUPPORT
898 if(sz->rank == 4) {
899 dword &= ~(ClkDis0 >> (index+2));
900 }
901#endif
902 pci_conf1_write_config32(ctrl->f2, DRAM_CTRL_MISC, dword);
903 }
904
905 }
906}
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923void set_dimm_cs_map(const struct mem_controller *ctrl, struct dimm_size *sz, unsigned index)
924{
925 static const u8 cs_map_aaa[24] = {
926
927
928 0, 1, 3,
929 0, 2, 6,
930 0, 0, 0,
931 0, 0, 0,
932
933 0, 4, 0,
934 0, 5, 8,
935 0, 7, 9,
936 0,10,11,
937 };
938
939 u32 map;
940
941 map = pci_conf1_read_config32(ctrl->f2, DRAM_BANK_ADDR_MAP);
942 map &= ~(0xf << (index * 4));
943#ifdef QRANK_DIMM_SUPPORT
944 if(sz->rank == 4) {
945 map &= ~(0xf << ( (index + 2) * 4));
946 }
947#endif
948
949
950 if (sz->per_rank >= 27) {
951 unsigned temp_map;
952 temp_map = cs_map_aaa[(sz->bank-2)*3*4 + (sz->rows - 13)*3 + (sz->col - 9) ];
953 map |= temp_map << (index*4);
954#ifdef QRANK_DIMM_SUPPORT
955 if(sz->rank == 4) {
956 map |= temp_map << ( (index + 2) * 4);
957 }
958#endif
959 }
960
961 pci_conf1_write_config32(ctrl->f2, DRAM_BANK_ADDR_MAP, map);
962
963}
964
965long spd_set_ram_size(const struct mem_controller *ctrl, long dimm_mask, struct mem_info *meminfo)
966{
967 int i;
968
969 for(i = 0; i < DIMM_SOCKETS; i++) {
970 struct dimm_size *sz = &(meminfo->sz[i]);
971 if (!(dimm_mask & (1 << i))) {
972 continue;
973 }
974 spd_get_dimm_size(ctrl->channel0[i], sz);
975 if (sz->per_rank == 0) {
976 return -1;
977 }
978 set_dimm_size(ctrl, sz, i, meminfo->is_Width128);
979 set_dimm_cs_map (ctrl, sz, i);
980 }
981 return dimm_mask;
982}
983
984void route_dram_accesses(const struct mem_controller *ctrl,
985 unsigned long base_k, unsigned long limit_k)
986{
987
988 unsigned node_id;
989 unsigned limit;
990 unsigned base;
991 unsigned index;
992 unsigned limit_reg, base_reg;
993 u32 device;
994
995 node_id = ctrl->node_id;
996 index = (node_id << 3);
997 limit = (limit_k << 2);
998 limit &= 0xffff0000;
999 limit -= 0x00010000;
1000 limit |= ( 0 << 8) | (node_id << 0);
1001 base = (base_k << 2);
1002 base &= 0xffff0000;
1003 base |= (0 << 8) | (1<<1) | (1<<0);
1004
1005 limit_reg = 0x44 + index;
1006 base_reg = 0x40 + index;
1007 for(device = PCI_BDF(0, 0x18, 1); device <= PCI_BDF(0, 0x1f, 1); device += PCI_BDF(0, 1, 0)) {
1008 pci_conf1_write_config32(device, limit_reg, limit);
1009 pci_conf1_write_config32(device, base_reg, base);
1010 }
1011}
1012
1013void set_top_mem(unsigned tom_k, unsigned hole_startk)
1014{
1015
1016 if (!tom_k) {
1017 die("No memory?");
1018 }
1019
1020
1021 printk(BIOS_DEBUG, "RAM: 0x%x KB\n", tom_k);
1022
1023 struct msr msr;
1024 if(tom_k > (4*1024*1024)) {
1025
1026 msr.lo = (tom_k & 0x003fffff) << 10;
1027 msr.hi = (tom_k & 0xffc00000) >> 22;
1028 wrmsr(TOP_MEM2, msr);
1029 }
1030
1031
1032
1033
1034 if (tom_k >= 0x003f0000) {
1035#if CONFIG_HW_MEM_HOLE_SIZEK != 0
1036 if(hole_startk != 0) {
1037 tom_k = hole_startk;
1038 } else
1039#endif
1040 tom_k = 0x3f0000;
1041 }
1042 msr.lo = (tom_k & 0x003fffff) << 10;
1043 msr.hi = (tom_k & 0xffc00000) >> 22;
1044 wrmsr(TOP_MEM, msr);
1045}
1046
1047unsigned long interleave_chip_selects(const struct mem_controller *ctrl, int is_Width128)
1048{
1049
1050
1051 static const u8 csbase_low_f0_shift[] = {
1052 (14 - (13-5)),
1053 (15 - (13-5)),
1054 (15 - (13-5)),
1055 (16 - (13-5)),
1056 (16 - (13-5)),
1057 (16 - (13-5)),
1058 (16 - (13-5)),
1059 (16 - (13-5)),
1060 (17 - (13-5)),
1061 (17 - (13-5)),
1062 (16 - (13-5)),
1063 (17 - (13-5)),
1064 };
1065
1066
1067
1068 u32 csbase_inc;
1069 int chip_selects, index;
1070 int bits;
1071 unsigned common_size;
1072 unsigned common_cs_mode;
1073 u32 csbase, csmask;
1074
1075
1076
1077
1078 chip_selects = 0;
1079 common_size = 0;
1080 common_cs_mode = 0xff;
1081 for(index = 0; index < 8; index++) {
1082 unsigned size;
1083 unsigned cs_mode;
1084 u32 value;
1085
1086 value = pci_conf1_read_config32(ctrl->f2, DRAM_CSBASE + (index << 2));
1087
1088
1089 if (!(value & 1)) {
1090 continue;
1091 }
1092 chip_selects++;
1093 size = (value >> 19) & 0x3ff;
1094 if (common_size == 0) {
1095 common_size = size;
1096 }
1097
1098 if (common_size != size) {
1099 return 0;
1100 }
1101
1102 value = pci_conf1_read_config32(ctrl->f2, DRAM_BANK_ADDR_MAP);
1103 cs_mode =( value >> ((index>>1)*4)) & 0xf;
1104 if(common_cs_mode == 0xff) {
1105 common_cs_mode = cs_mode;
1106 }
1107
1108 if(common_cs_mode != cs_mode) {
1109 return 0;
1110 }
1111 }
1112
1113
1114
1115
1116 bits = log2c(chip_selects);
1117 if (((1 << bits) != chip_selects) || (bits < 1) || (bits > 3)) {
1118 return 0;
1119 }
1120
1121
1122 csbase_inc = 1 << (csbase_low_f0_shift[common_cs_mode]);
1123 if(is_Width128) {
1124 csbase_inc <<=1;
1125 }
1126
1127
1128
1129
1130
1131
1132 csbase = 0 | 1;
1133 csmask = (((common_size << bits) - 1) << 19);
1134 csmask |= 0x3fe0 & ~((csbase_inc << bits) - csbase_inc);
1135 for(index = 0; index < 8; index++) {
1136 u32 value;
1137
1138 value = pci_conf1_read_config32(ctrl->f2, DRAM_CSBASE + (index << 2));
1139
1140 if (!(value & 1)) {
1141 continue;
1142 }
1143 pci_conf1_write_config32(ctrl->f2, DRAM_CSBASE + (index << 2), csbase);
1144 if((index & 1) == 0) {
1145 pci_conf1_write_config32(ctrl->f2, DRAM_CSMASK + ((index>>1) << 2), csmask);
1146 }
1147 csbase += csbase_inc;
1148 }
1149
1150 printk(BIOS_DEBUG, "Interleaved\n");
1151
1152
1153 return common_size << ((27-10) + bits);
1154}
1155unsigned long order_chip_selects(const struct mem_controller *ctrl)
1156{
1157 unsigned long tom;
1158
1159
1160 tom = 0;
1161 for(;;) {
1162
1163 unsigned index, candidate;
1164 u32 csbase, csmask;
1165 unsigned size;
1166 csbase = 0;
1167 candidate = 0;
1168 for(index = 0; index < 8; index++) {
1169 u32 value;
1170 value = pci_conf1_read_config32(ctrl->f2, DRAM_CSBASE + (index << 2));
1171
1172
1173 if (!(value & 1)) {
1174 continue;
1175 }
1176
1177
1178 if (value <= csbase) {
1179 continue;
1180 }
1181
1182
1183 if (tom & (1 << (index + 24))) {
1184 continue;
1185 }
1186
1187 csbase = value;
1188 candidate = index;
1189 }
1190
1191 if (csbase == 0) {
1192 break;
1193 }
1194
1195
1196 size = csbase >> 19;
1197
1198
1199 tom |= (1 << (candidate + 24));
1200
1201
1202 csbase = (tom << 19) | 1;
1203
1204
1205 tom += size;
1206
1207
1208 csmask = ((size -1) << 19);
1209 csmask |= 0x3fe0;
1210
1211
1212 pci_conf1_write_config32(ctrl->f2, DRAM_CSBASE + (candidate << 2), csbase);
1213
1214 if((candidate & 1) == 0) {
1215 pci_conf1_write_config32(ctrl->f2, DRAM_CSMASK + ((candidate>>1) << 2), csmask);
1216 }
1217
1218 }
1219
1220 return (tom & ~0xff000000) << (27-10);
1221}
1222
1223unsigned long memory_end_k(const struct mem_controller *ctrl, int max_node_id)
1224{
1225 unsigned node_id;
1226 unsigned end_k;
1227
1228 end_k = 0;
1229 for(node_id = 0; node_id < max_node_id; node_id++) {
1230 u32 limit, base;
1231 unsigned index;
1232 index = node_id << 3;
1233 base = pci_conf1_read_config32(ctrl->f1, 0x40 + index);
1234
1235 if ((base & 3) == 3) {
1236 limit = pci_conf1_read_config32(ctrl->f1, 0x44 + index);
1237 end_k = ((limit + 0x00010000) & 0xffff0000) >> 2;
1238 }
1239 }
1240 return end_k;
1241}
1242
1243void order_dimms(const struct mem_controller *ctrl, struct mem_info *meminfo)
1244{
1245 unsigned long tom_k, base_k;
1246 int interleave = 1;
1247
1248 if (get_option(&interleave, "interleave_chip_selects")) {
1249 tom_k = interleave_chip_selects(ctrl, meminfo->is_Width128);
1250 } else {
1251 printk(BIOS_DEBUG, "Interleaving disabled\n");
1252 tom_k = 0;
1253 }
1254 if (!tom_k) {
1255 tom_k = order_chip_selects(ctrl);
1256 }
1257
1258 base_k = memory_end_k(ctrl, ctrl->node_id);
1259 tom_k += base_k;
1260 route_dram_accesses(ctrl, base_k, tom_k);
1261 set_top_mem(tom_k, 0);
1262}
1263
1264long disable_dimm(const struct mem_controller *ctrl, unsigned index, long dimm_mask, struct mem_info *meminfo)
1265{
1266 printk(BIOS_DEBUG, "disabling dimm 0x%x \n", index);
1267 pci_conf1_write_config32(ctrl->f2, DRAM_CSBASE + (((index << 1)+0)<<2), 0);
1268 pci_conf1_write_config32(ctrl->f2, DRAM_CSBASE + (((index << 1)+1)<<2), 0);
1269#ifdef QRANK_DIMM_SUPPORT
1270 if(meminfo->sz[index].rank == 4) {
1271 pci_conf1_write_config32(ctrl->f2, DRAM_CSBASE + (((index << 1)+4)<<2), 0);
1272 pci_conf1_write_config32(ctrl->f2, DRAM_CSBASE + (((index << 1)+5)<<2), 0);
1273 }
1274#endif
1275
1276 dimm_mask &= ~(1 << index);
1277 return dimm_mask;
1278}
1279
1280long spd_handle_unbuffered_dimms(const struct mem_controller *ctrl, long dimm_mask, struct mem_info *meminfo)
1281{
1282 int i;
1283 u32 registered;
1284 u32 dcl;
1285 registered = 0;
1286 for(i = 0; (i < DIMM_SOCKETS); i++) {
1287 int value;
1288 if (!(dimm_mask & (1 << i))) {
1289 continue;
1290 }
1291 value = spd_read_byte(ctrl->channel0[i], SPD_DIMM_TYPE);
1292 if (value < 0) {
1293 return -1;
1294 }
1295
1296 value &= 0x3f;
1297 if ((value == SPD_DIMM_TYPE_RDIMM) || (value == SPD_DIMM_TYPE_mRDIMM)) {
1298
1299 registered |= (1<<i);
1300 }
1301 }
1302
1303 if (is_opteron(ctrl)) {
1304#if 0
1305 if ( registered != (dimm_mask & ((1<<DIMM_SOCKETS)-1)) ) {
1306 dimm_mask &= (registered | (registered << DIMM_SOCKETS) );
1307
1308 }
1309
1310 if (!registered) {
1311 die("Unbuffered Dimms not supported on Opteron");
1312 }
1313#endif
1314 }
1315
1316
1317 dcl = pci_conf1_read_config32(ctrl->f2, DRAM_CONFIG_LOW);
1318 dcl &= ~DCL_UnBuffDimm;
1319 meminfo->is_registered = 1;
1320 if (!registered) {
1321 dcl |= DCL_UnBuffDimm;
1322 meminfo->is_registered = 0;
1323 }
1324 pci_conf1_write_config32(ctrl->f2, DRAM_CONFIG_LOW, dcl);
1325
1326#if 1
1327 if (meminfo->is_registered) {
1328 printk(BIOS_DEBUG, "Registered\n");
1329 } else {
1330 printk(BIOS_DEBUG, "Unbuffered\n");
1331 }
1332#endif
1333 return dimm_mask;
1334}
1335
1336unsigned int spd_detect_dimms(const struct mem_controller *ctrl)
1337{
1338 unsigned dimm_mask;
1339 int i;
1340 dimm_mask = 0;
1341 for(i = 0; i < DIMM_SOCKETS; i++) {
1342 int byte;
1343 unsigned device;
1344 device = ctrl->channel0[i];
1345 if (device) {
1346 byte = spd_read_byte(ctrl->channel0[i], SPD_MEM_TYPE);
1347 if (byte == SPD_MEM_TYPE_SDRAM_DDR2) {
1348 dimm_mask |= (1 << i);
1349 }
1350 }
1351 device = ctrl->channel1[i];
1352 if (device) {
1353 byte = spd_read_byte(ctrl->channel1[i], SPD_MEM_TYPE);
1354 if (byte == SPD_MEM_TYPE_SDRAM_DDR2) {
1355 dimm_mask |= (1 << (i + DIMM_SOCKETS));
1356 }
1357 }
1358 }
1359 return dimm_mask;
1360}
1361
1362long spd_enable_2channels(const struct mem_controller *ctrl, long dimm_mask, struct mem_info *meminfo)
1363{
1364 int i;
1365 u32 nbcap;
1366
1367 static const u8 addresses[] = {
1368 2,
1369 3,
1370 4,
1371 5,
1372 6,
1373 9,
1374 11,
1375 13,
1376 17,
1377 18,
1378 20,
1379 21,
1380 23,
1381 26,
1382 27,
1383 28,
1384 29,
1385 30,
1386 36,
1387 37,
1388 38,
1389 41,
1390 41,
1391 42,
1392 };
1393
1394 if ((dimm_mask & ((1 << DIMM_SOCKETS) - 1)) !=
1395 ((dimm_mask >> DIMM_SOCKETS) & ((1 << DIMM_SOCKETS) - 1))) {
1396 goto single_channel;
1397 }
1398
1399 nbcap = pci_conf1_read_config32(ctrl->f3, NORTHBRIDGE_CAP);
1400 if (!(nbcap & NBCAP_128Bit)) {
1401 goto single_channel;
1402 }
1403 for(i = 0; (i < 4) && (ctrl->channel0[i]); i++) {
1404 unsigned device0, device1;
1405 int value0, value1;
1406 int j;
1407
1408 if (!(dimm_mask & (1 << i))) {
1409 continue;
1410 }
1411 device0 = ctrl->channel0[i];
1412 device1 = ctrl->channel1[i];
1413 for(j = 0; j < ARRAY_SIZE(addresses); j++) {
1414 unsigned addr;
1415 addr = addresses[j];
1416 value0 = spd_read_byte(device0, addr);
1417 if (value0 < 0) {
1418 return -1;
1419 }
1420 value1 = spd_read_byte(device1, addr);
1421 if (value1 < 0) {
1422 return -1;
1423 }
1424 if (value0 != value1) {
1425 goto single_channel;
1426 }
1427 }
1428 }
1429 printk(BIOS_SPEW, "Enabling dual channel memory\n");
1430 u32 dcl;
1431 dcl = pci_conf1_read_config32(ctrl->f2, DRAM_CONFIG_LOW);
1432 dcl &= ~DCL_BurstLength32;
1433
1434
1435
1436
1437 dcl |= DCL_Width128;
1438 pci_conf1_write_config32(ctrl->f2, DRAM_CONFIG_LOW, dcl);
1439 meminfo->is_Width128 = 1;
1440 return dimm_mask;
1441 single_channel:
1442 dimm_mask &= ~((1 << (DIMM_SOCKETS *2)) - (1 << DIMM_SOCKETS));
1443 meminfo->is_Width128 = 0;
1444 return dimm_mask;
1445}
1446
1447struct mem_param {
1448 u16 cycle_time;
1449 u8 divisor;
1450 u8 TrwtTO;
1451 u8 Twrrd;
1452 u8 Twrwr;
1453 u8 Trdrd;
1454 u8 DcqByPassMax;
1455 u32 dch_memclk;
1456 char name[9];
1457};
1458
1459 static const struct mem_param speed[] = {
1460 {
1461 .name = "200Mhz\n",
1462 .cycle_time = 0x500,
1463 .divisor = 200,
1464 .dch_memclk = DCH_MemClkFreq_200MHz,
1465 .TrwtTO = 7,
1466 .Twrrd = 2,
1467 .Twrwr = 2,
1468 .Trdrd = 3,
1469 .DcqByPassMax = 4,
1470
1471 },
1472 {
1473 .name = "266Mhz\n",
1474 .cycle_time = 0x375,
1475 .divisor = 150,
1476 .dch_memclk = DCH_MemClkFreq_266MHz,
1477 .TrwtTO = 7,
1478 .Twrrd = 2,
1479 .Twrwr = 2,
1480 .Trdrd = 3,
1481 .DcqByPassMax = 4,
1482 },
1483 {
1484 .name = "333Mhz\n",
1485 .cycle_time = 0x300,
1486 .divisor = 120,
1487 .dch_memclk = DCH_MemClkFreq_333MHz,
1488 .TrwtTO = 7,
1489 .Twrrd = 2,
1490 .Twrwr = 2,
1491 .Trdrd = 3,
1492 .DcqByPassMax = 4,
1493
1494 },
1495 {
1496 .name = "400Mhz\n",
1497 .cycle_time = 0x250,
1498 .divisor = 100,
1499 .dch_memclk = DCH_MemClkFreq_400MHz,
1500 .TrwtTO = 7,
1501 .Twrrd = 2,
1502 .Twrwr = 2,
1503 .Trdrd = 3,
1504 .DcqByPassMax = 4,
1505 },
1506 {
1507 .cycle_time = 0x000,
1508 },
1509 };
1510
1511const struct mem_param *get_mem_param(unsigned min_cycle_time)
1512{
1513
1514 const struct mem_param *param;
1515 for(param = &speed[0]; param->cycle_time ; param++) {
1516 if (min_cycle_time > (param+1)->cycle_time) {
1517 break;
1518 }
1519 }
1520 if (!param->cycle_time) {
1521 die("min_cycle_time to low");
1522 }
1523 printk(BIOS_SPEW, param->name);
1524#ifdef DRAM_MIN_CYCLE_TIME
1525 printk(BIOS_DEBUG, param->name);
1526#endif
1527 return param;
1528}
1529
1530u8 get_exact_divisor(int i, u8 divisor)
1531{
1532
1533 static const u8 dv_a[] = {
1534
1535 250, 250, 250, 250,
1536 200, 200, 200, 100,
1537 200, 166, 166, 100,
1538 200, 171, 142, 100,
1539
1540 200, 150, 125, 100,
1541 200, 156, 133, 100,
1542 200, 160, 120, 100,
1543 200, 163, 127, 100,
1544
1545 200, 150, 133, 100,
1546 200, 153, 123, 100,
1547 200, 157, 128, 100,
1548 200, 160, 120, 100,
1549 };
1550
1551 unsigned fid_cur;
1552 int index;
1553
1554 struct msr msr;
1555 msr = rdmsr(0xc0010042);
1556 fid_cur = msr.lo & 0x3f;
1557
1558 index = fid_cur>>1;
1559
1560 if(index>12) return divisor;
1561
1562 if(i>3) return divisor;
1563
1564 return dv_a[index * 4+i];
1565
1566}
1567
1568struct spd_set_memclk_result {
1569 const struct mem_param *param;
1570 long dimm_mask;
1571};
1572
1573unsigned convert_to_linear(unsigned value)
1574{
1575 static const unsigned fraction[] = { 0x25, 0x33, 0x66, 0x75 };
1576 unsigned valuex;
1577
1578
1579 if((value & 0xf) < 10) {
1580 value <<= 4;
1581 } else {
1582 valuex = ((value & 0xf0) << 4) | fraction [(value & 0xf)-10];
1583 value = valuex;
1584 }
1585 return value;
1586}
1587
1588struct spd_set_memclk_result spd_set_memclk(const struct mem_controller *ctrl, long dimm_mask, struct mem_info *meminfo)
1589{
1590
1591 struct spd_set_memclk_result result;
1592 unsigned min_cycle_time, min_latency, bios_cycle_time;
1593 int i;
1594 u32 value;
1595 unsigned int max_mem_clock = 0;
1596
1597 static const u8 latency_indicies[] = { 25, 23, 9 };
1598
1599 static const u16 min_cycle_times[] = {
1600 [NBCAP_MEMCLK_NOLIMIT] = 0x250,
1601 [NBCAP_MEMCLK_333MHZ] = 0x300,
1602 [NBCAP_MEMCLK_266MHZ] = 0x375,
1603 [NBCAP_MEMCLK_200MHZ] = 0x500,
1604 };
1605
1606
1607 value = pci_conf1_read_config32(ctrl->f3, NORTHBRIDGE_CAP);
1608 min_cycle_time = min_cycle_times[(value >> NBCAP_MEMCLK_SHIFT) & NBCAP_MEMCLK_MASK];
1609 get_option(&max_mem_clock, "max_mem_clock");
1610 bios_cycle_time = min_cycle_times[max_mem_clock];
1611 if (bios_cycle_time > min_cycle_time) {
1612 min_cycle_time = bios_cycle_time;
1613 }
1614 min_latency = 3;
1615
1616 printk(BIOS_DEBUG, "%s: 0x%x\n", "1 min_cycle_time:", min_cycle_time);
1617
1618
1619
1620
1621 for(i = 0; i < DIMM_SOCKETS; i++) {
1622 int new_cycle_time, new_latency;
1623 int index;
1624 int latencies;
1625 int latency;
1626
1627 if (!(dimm_mask & (1 << i))) {
1628 continue;
1629 }
1630
1631
1632
1633
1634
1635
1636
1637
1638 new_cycle_time = 0x500;
1639 new_latency = 6;
1640
1641 latencies = spd_read_byte(ctrl->channel0[i], SPD_CAS_LAT);
1642 if (latencies <= 0) continue;
1643
1644 printk(BIOS_DEBUG, "%s: 0x%x\n", "i:", i);
1645 printk(BIOS_DEBUG, "%s: 0x%x\n", "\tlatencies:", latencies);
1646
1647 latency = log2f(latencies) - 2;
1648
1649
1650 for(index = 0; index < 3; index++, latency++) {
1651 int value;
1652 if ((latency < 3) || (latency > 6) ||
1653 (!(latencies & (1 << latency)))) {
1654 continue;
1655 }
1656 value = spd_read_byte(ctrl->channel0[i], latency_indicies[index]);
1657 if (value < 0) {
1658 goto hw_error;
1659 }
1660 printk(BIOS_DEBUG, "%s: 0x%x\n", "\tindex:", index);
1661 printk(BIOS_DEBUG, "%s: 0x%x\n", "\t\tlatency:", latency);
1662 printk(BIOS_DEBUG, "%s: 0x%x\n", "\t\tvalue1:", value);
1663
1664 value = convert_to_linear(value);
1665
1666 printk(BIOS_DEBUG, "%s: 0x%x\n", "\t\tvalue2:", value);
1667
1668
1669 if (value >= min_cycle_time ) {
1670 if(value < new_cycle_time) {
1671 new_cycle_time = value;
1672 new_latency = latency;
1673 } else if (value == new_cycle_time) {
1674 if(new_latency > latency) {
1675 new_latency = latency;
1676 }
1677 }
1678 }
1679 printk(BIOS_DEBUG, "%s: 0x%x\n", "\t\tnew_cycle_time:", new_cycle_time);
1680 printk(BIOS_DEBUG, "%s: 0x%x\n", "\t\tnew_latency:", new_latency);
1681
1682 }
1683 if (new_latency > 6){
1684 continue;
1685 }
1686
1687 if (new_cycle_time > min_cycle_time) {
1688 min_cycle_time = new_cycle_time;
1689 }
1690
1691 if (new_latency > min_latency) {
1692 min_latency = new_latency;
1693 }
1694
1695 printk(BIOS_DEBUG, "%s: 0x%x\n", "2 min_cycle_time:", min_cycle_time);
1696 printk(BIOS_DEBUG, "%s: 0x%x\n", "2 min_latency:", min_latency);
1697 }
1698
1699
1700
1701
1702 printk(BIOS_DEBUG, "%s: 0x%x\n", "3 min_cycle_time:", min_cycle_time);
1703 printk(BIOS_DEBUG, "%s: 0x%x\n", "3 min_latency:", min_latency);
1704
1705 for(i = 0; (i < DIMM_SOCKETS) && (ctrl->channel0[i]); i++) {
1706 int latencies;
1707 int latency;
1708 int index;
1709 int value;
1710 if (!(dimm_mask & (1 << i))) {
1711 continue;
1712 }
1713 latencies = spd_read_byte(ctrl->channel0[i], SPD_CAS_LAT);
1714 if (latencies < 0) goto hw_error;
1715 if (latencies == 0) {
1716 continue;
1717 }
1718
1719
1720 latency = log2f(latencies) -2;
1721
1722
1723 for(index = 0; index < 3; index++, latency++) {
1724 if (!(latencies & (1 << latency))) {
1725 continue;
1726 }
1727 if (latency == min_latency)
1728 break;
1729 }
1730
1731 if ((latency != min_latency) || (index >= 3)) {
1732 goto dimm_err;
1733 }
1734
1735
1736 value = spd_read_byte(ctrl->channel0[i], latency_indicies[index]);
1737 if (value < 0) goto hw_error;
1738
1739 value = convert_to_linear(value);
1740
1741
1742
1743 if (value <= min_cycle_time) {
1744 continue;
1745 }
1746
1747 dimm_err:
1748 dimm_mask = disable_dimm(ctrl, i, dimm_mask, meminfo);
1749 }
1750
1751 printk(BIOS_DEBUG, "%s: 0x%x\n", "4 min_cycle_time:", min_cycle_time);
1752
1753
1754 result.param = get_mem_param(min_cycle_time);
1755
1756
1757 value = pci_conf1_read_config32(ctrl->f2, DRAM_CONFIG_HIGH);
1758 value &= ~(DCH_MemClkFreq_MASK << DCH_MemClkFreq_SHIFT);
1759
1760 value |= result.param->dch_memclk << DCH_MemClkFreq_SHIFT;
1761 pci_conf1_write_config32(ctrl->f2, DRAM_CONFIG_HIGH, value);
1762
1763 printk(BIOS_DEBUG, result.param->name);
1764
1765
1766 value = pci_conf1_read_config32(ctrl->f2, DRAM_TIMING_LOW);
1767 value &= ~(DTL_TCL_MASK << DTL_TCL_SHIFT);
1768 value |= (min_latency - DTL_TCL_BASE) << DTL_TCL_SHIFT;
1769 pci_conf1_write_config32(ctrl->f2, DRAM_TIMING_LOW, value);
1770
1771 result.dimm_mask = dimm_mask;
1772 return result;
1773 hw_error:
1774 result.param = (const struct mem_param *)0;
1775 result.dimm_mask = -1;
1776 return result;
1777}
1778
1779unsigned convert_to_1_4(unsigned value)
1780{
1781 static const u8 fraction[] = { 0, 1, 2, 2, 3, 3, 0 };
1782 unsigned valuex;
1783
1784
1785 valuex = fraction [value & 0x7];
1786 return valuex;
1787}
1788int update_dimm_Trc(const struct mem_controller *ctrl, const struct mem_param *param, int i)
1789{
1790 unsigned clocks, old_clocks;
1791 u32 dtl;
1792 int value;
1793 int value2;
1794 value = spd_read_byte(ctrl->channel0[i], SPD_TRC);
1795 if (value < 0) return -1;
1796
1797 value2 = spd_read_byte(ctrl->channel0[i], SPD_TRC -1);
1798 value <<= 2;
1799 value += convert_to_1_4(value2>>4);
1800
1801 value *=10;
1802
1803 clocks = (value + param->divisor - 1)/param->divisor;
1804
1805 if (clocks < DTL_TRC_MIN) {
1806 clocks = DTL_TRC_MIN;
1807 }
1808 if (clocks > DTL_TRC_MAX) {
1809 return 0;
1810 }
1811
1812 dtl = pci_conf1_read_config32(ctrl->f2, DRAM_TIMING_LOW);
1813 old_clocks = ((dtl >> DTL_TRC_SHIFT) & DTL_TRC_MASK) + DTL_TRC_BASE;
1814 if (old_clocks >= clocks) {
1815
1816 return 1;
1817 }
1818 dtl &= ~(DTL_TRC_MASK << DTL_TRC_SHIFT);
1819 dtl |= ((clocks - DTL_TRC_BASE) << DTL_TRC_SHIFT);
1820 pci_conf1_write_config32(ctrl->f2, DRAM_TIMING_LOW, dtl);
1821 return 1;
1822}
1823
1824int update_dimm_Trfc(const struct mem_controller *ctrl, const struct mem_param *param, int i, struct mem_info *meminfo)
1825{
1826 unsigned clocks, old_clocks;
1827 u32 dth;
1828 int value;
1829
1830
1831 value = spd_read_byte(ctrl->channel0[i], SPD_PRI_WIDTH);
1832 if (value < 0) {
1833 return -1;
1834 }
1835
1836 value = 6 - log2f(value);
1837
1838 clocks = meminfo->sz[i].per_rank - 27 + 2 - value;
1839
1840 dth = pci_conf1_read_config32(ctrl->f2, DRAM_TIMING_HIGH);
1841
1842 old_clocks = ((dth >> (DTH_TRFC0_SHIFT+i*3)) & DTH_TRFC_MASK);
1843 if (old_clocks >= clocks) {
1844 return 1;
1845 }
1846 dth &= ~(DTH_TRFC_MASK << (DTH_TRFC0_SHIFT+i*3));
1847 dth |= clocks << (DTH_TRFC0_SHIFT+i*3);
1848 pci_conf1_write_config32(ctrl->f2, DRAM_TIMING_HIGH, dth);
1849 return 1;
1850}
1851
1852int update_dimm_TT_1_4(const struct mem_controller *ctrl, const struct mem_param *param,
1853 int i,
1854 unsigned TT_REG,
1855 unsigned SPD_TT, unsigned TT_SHIFT,
1856 unsigned TT_MASK, unsigned TT_BASE,
1857 unsigned TT_MIN, unsigned TT_MAX )
1858{
1859 unsigned clocks, old_clocks;
1860 u32 dtl;
1861 int value;
1862 value = spd_read_byte(ctrl->channel0[i], SPD_TT);
1863 if (value < 0) return -1;
1864 value *=10;
1865 clocks = (value + param->divisor -1)/param->divisor;
1866 if (clocks < TT_MIN) {
1867 clocks = TT_MIN;
1868 }
1869 if (clocks > TT_MAX) {
1870 return 0;
1871 }
1872 dtl = pci_conf1_read_config32(ctrl->f2, TT_REG);
1873
1874 old_clocks = ((dtl >> TT_SHIFT) & TT_MASK) + TT_BASE;
1875 if (old_clocks >= clocks) {
1876
1877 return 1;
1878 }
1879 dtl &= ~(TT_MASK << TT_SHIFT);
1880 dtl |= ((clocks - TT_BASE) << TT_SHIFT);
1881 pci_conf1_write_config32(ctrl->f2, TT_REG, dtl);
1882 return 1;
1883}
1884
1885int update_dimm_Trcd(const struct mem_controller *ctrl, const struct mem_param *param, int i)
1886{
1887 return update_dimm_TT_1_4(ctrl, param, i, DRAM_TIMING_LOW, SPD_TRCD, DTL_TRCD_SHIFT, DTL_TRCD_MASK, DTL_TRCD_BASE, DTL_TRCD_MIN, DTL_TRCD_MAX);
1888}
1889
1890int update_dimm_Trrd(const struct mem_controller *ctrl, const struct mem_param *param, int i)
1891{
1892 return update_dimm_TT_1_4(ctrl, param, i, DRAM_TIMING_LOW, SPD_TRRD, DTL_TRRD_SHIFT, DTL_TRRD_MASK, DTL_TRRD_BASE, DTL_TRRD_MIN, DTL_TRRD_MAX);
1893}
1894
1895int update_dimm_Tras(const struct mem_controller *ctrl, const struct mem_param *param, int i)
1896{
1897 unsigned clocks, old_clocks;
1898 u32 dtl;
1899 int value;
1900 value = spd_read_byte(ctrl->channel0[i], SPD_TRAS);
1901 if (value < 0) return -1;
1902 printk(BIOS_DEBUG, "%s: 0x%x\n", "update_dimm_Tras: 0 value=", value);
1903
1904 value<<=2;
1905
1906 value *= 10;
1907 printk(BIOS_DEBUG, "%s: 0x%x\n", "update_dimm_Tras: 1 value=", value);
1908
1909 clocks = (value + param->divisor - 1)/param->divisor;
1910 printk(BIOS_DEBUG, "%s: 0x%x\n", "update_dimm_Tras: divisor=", param->divisor);
1911 printk(BIOS_DEBUG, "%s: 0x%x\n", "update_dimm_Tras: clocks=", clocks);
1912 if (clocks < DTL_TRAS_MIN) {
1913 clocks = DTL_TRAS_MIN;
1914 }
1915 if (clocks > DTL_TRAS_MAX) {
1916 return 0;
1917 }
1918 dtl = pci_conf1_read_config32(ctrl->f2, DRAM_TIMING_LOW);
1919 old_clocks = ((dtl >> DTL_TRAS_SHIFT) & DTL_TRAS_MASK) + DTL_TRAS_BASE;
1920 if (old_clocks >= clocks) {
1921 return 1;
1922 }
1923 dtl &= ~(DTL_TRAS_MASK << DTL_TRAS_SHIFT);
1924 dtl |= ((clocks - DTL_TRAS_BASE) << DTL_TRAS_SHIFT);
1925 pci_conf1_write_config32(ctrl->f2, DRAM_TIMING_LOW, dtl);
1926 return 1;
1927}
1928
1929int update_dimm_Trp(const struct mem_controller *ctrl, const struct mem_param *param, int i)
1930{
1931 return update_dimm_TT_1_4(ctrl, param, i, DRAM_TIMING_LOW, SPD_TRP, DTL_TRP_SHIFT, DTL_TRP_MASK, DTL_TRP_BASE, DTL_TRP_MIN, DTL_TRP_MAX);
1932}
1933
1934int update_dimm_Trtp(const struct mem_controller *ctrl, const struct mem_param *param, int i, struct mem_info *meminfo)
1935{
1936
1937 int offset = 2;
1938 if(!meminfo->is_Width128) {
1939 u32 dword;
1940 dword = pci_conf1_read_config32(ctrl->f2, DRAM_CONFIG_LOW);
1941 if((dword & DCL_BurstLength32)) offset = 0;
1942 }
1943 return update_dimm_TT_1_4(ctrl, param, i, DRAM_TIMING_LOW, SPD_TRTP, DTL_TRTP_SHIFT, DTL_TRTP_MASK, DTL_TRTP_BASE+offset, DTL_TRTP_MIN+offset, DTL_TRTP_MAX+offset);
1944}
1945
1946
1947int update_dimm_Twr(const struct mem_controller *ctrl, const struct mem_param *param, int i)
1948{
1949 return update_dimm_TT_1_4(ctrl, param, i, DRAM_TIMING_LOW, SPD_TWR, DTL_TWR_SHIFT, DTL_TWR_MASK, DTL_TWR_BASE, DTL_TWR_MIN, DTL_TWR_MAX);
1950}
1951
1952
1953int update_dimm_Tref(const struct mem_controller *ctrl, const struct mem_param *param, int i)
1954{
1955 u32 dth, dth_old;
1956 int value;
1957 value = spd_read_byte(ctrl->channel0[i], SPD_TREF);
1958 if (value < 0) return -1;
1959
1960 if(value == 1 ) {
1961 value = 3;
1962 } else {
1963 value = 2;
1964 }
1965
1966 dth = pci_conf1_read_config32(ctrl->f2, DRAM_TIMING_HIGH);
1967
1968 dth_old = dth;
1969 dth &= ~(DTH_TREF_MASK << DTH_TREF_SHIFT);
1970 dth |= (value << DTH_TREF_SHIFT);
1971 if(dth_old != dth) {
1972 pci_conf1_write_config32(ctrl->f2, DRAM_TIMING_HIGH, dth);
1973 }
1974 return 1;
1975}
1976
1977void set_4RankRDimm(const struct mem_controller *ctrl, const struct mem_param *param, struct mem_info *meminfo)
1978{
1979#ifdef QRANK_DIMM_SUPPORT
1980 int value;
1981 int i;
1982
1983
1984 if(!(meminfo->is_registered)) return;
1985
1986 value = 0;
1987
1988 for(i = 0; i < DIMM_SOCKETS; i++) {
1989 if (!(dimm_mask & (1 << i))) {
1990 continue;
1991 }
1992
1993 if(meminfo->sz.rank == 4) {
1994 value = 1;
1995 break;
1996 }
1997 }
1998
1999 if(value == 1) {
2000 u32 dch;
2001 dch = pci_conf1_read_config32(ctrl->f2, DRAM_CONFIG_HIGH);
2002 dch |= DCH_FourRankRDimm;
2003 pci_conf1_write_config32(ctrl->f2, DRAM_CONFIG_HIGH, dch);
2004 }
2005#endif
2006}
2007
2008
2009u32 get_extra_dimm_mask(const struct mem_controller *ctrl, struct mem_info *meminfo)
2010{
2011 int i;
2012
2013 u32 mask_x4;
2014 u32 mask_x16;
2015 u32 mask_single_rank;
2016 u32 mask_page_1k;
2017 int value;
2018#ifdef QRANK_DIMM_SUPPORT
2019 int rank;
2020#endif
2021
2022 long dimm_mask = meminfo->dimm_mask;
2023
2024
2025 mask_x4 = 0;
2026 mask_x16 = 0;
2027 mask_single_rank = 0;
2028 mask_page_1k = 0;
2029
2030 for(i = 0; i < DIMM_SOCKETS; i++) {
2031
2032 if (!(dimm_mask & (1 << i))) {
2033 continue;
2034 }
2035
2036 if(meminfo->sz[i].rank == 1) {
2037 mask_single_rank |= 1<<i;
2038 }
2039
2040 if(meminfo->sz[i].col==10) {
2041 mask_page_1k |= 1<<i;
2042 }
2043
2044
2045 value = spd_read_byte(ctrl->channel0[i], SPD_PRI_WIDTH);
2046
2047 #ifdef QRANK_DIMM_SUPPORT
2048 rank = meminfo->sz[i].rank;
2049 #endif
2050
2051 if(value==4) {
2052 mask_x4 |= (1<<i);
2053 #ifdef QRANK_DIMM_SUPPORT
2054 if(rank==4) {
2055 mask_x4 |= 1<<(i+2);
2056 }
2057 #endif
2058 } else if(value==16) {
2059 mask_x16 |= (1<<i);
2060 #ifdef QRANK_DIMM_SUPPORT
2061 if(rank==4) {
2062 mask_x16 |= 1<<(i+2);
2063 }
2064 #endif
2065 }
2066
2067 }
2068
2069 meminfo->x4_mask= mask_x4;
2070 meminfo->x16_mask = mask_x16;
2071
2072 meminfo->single_rank_mask = mask_single_rank;
2073 meminfo->page_1k_mask = mask_page_1k;
2074
2075 return mask_x4;
2076
2077}
2078
2079
2080void set_dimm_x4(const struct mem_controller *ctrl, const struct mem_param *param, struct mem_info *meminfo)
2081{
2082 u32 dcl;
2083 dcl = pci_conf1_read_config32(ctrl->f2, DRAM_CONFIG_LOW);
2084 dcl &= ~(DCL_X4Dimm_MASK<<DCL_X4Dimm_SHIFT);
2085 dcl |= ((meminfo->x4_mask) & 0xf) << (DCL_X4Dimm_SHIFT);
2086 pci_conf1_write_config32(ctrl->f2, DRAM_CONFIG_LOW, dcl);
2087}
2088
2089int count_ones(u32 dimm_mask)
2090{
2091 int dimms;
2092 unsigned index;
2093 dimms = 0;
2094 for(index = 0; index < DIMM_SOCKETS; index++, dimm_mask>>=1) {
2095 if (dimm_mask & 1) {
2096 dimms++;
2097 }
2098 }
2099 return dimms;
2100}
2101
2102
2103void set_DramTerm(const struct mem_controller *ctrl, const struct mem_param *param, struct mem_info *meminfo)
2104{
2105 u32 dcl;
2106 unsigned odt;
2107 odt = 1;
2108
2109 if(param->divisor == 100) {
2110 if(meminfo->is_Width128) {
2111 if(count_ones(meminfo->dimm_mask & 0x0f)==2) {
2112 odt = 3;
2113 }
2114 }
2115
2116 }
2117 dcl = pci_conf1_read_config32(ctrl->f2, DRAM_CONFIG_LOW);
2118 dcl &= ~(DCL_DramTerm_MASK<<DCL_DramTerm_SHIFT);
2119 dcl |= (odt & DCL_DramTerm_MASK) << (DCL_DramTerm_SHIFT);
2120 pci_conf1_write_config32(ctrl->f2, DRAM_CONFIG_LOW, dcl);
2121}
2122
2123
2124void set_ecc(const struct mem_controller *ctrl,const struct mem_param *param, long dimm_mask, struct mem_info *meminfo)
2125{
2126 int i;
2127 int value;
2128 int ECC_memory = 1;
2129
2130 u32 dcl, nbcap;
2131 nbcap = pci_conf1_read_config32(ctrl->f3, NORTHBRIDGE_CAP);
2132 dcl = pci_conf1_read_config32(ctrl->f2, DRAM_CONFIG_LOW);
2133 dcl &= ~DCL_DimmEccEn;
2134 if (nbcap & NBCAP_ECC) {
2135 dcl |= DCL_DimmEccEn;
2136 }
2137 get_option(&ECC_memory, "ECC_memory");
2138 if (ECC_memory) {
2139 dcl &= ~DCL_DimmEccEn;
2140 }
2141 pci_conf1_write_config32(ctrl->f2, DRAM_CONFIG_LOW, dcl);
2142
2143 meminfo->is_ecc = 1;
2144 if(!(dcl & DCL_DimmEccEn)) {
2145 meminfo->is_ecc = 0;
2146 return;
2147 }
2148
2149 for(i = 0; i < DIMM_SOCKETS; i++) {
2150
2151 if (!(dimm_mask & (1 << i))) {
2152 continue;
2153 }
2154
2155 value = spd_read_byte(ctrl->channel0[i], SPD_DIMM_CONF_TYPE);
2156
2157 if(!(value & SPD_DIMM_CONF_TYPE_ECC)) {
2158 dcl &= ~DCL_DimmEccEn;
2159 pci_conf1_write_config32(ctrl->f2, DRAM_CONFIG_LOW, dcl);
2160 meminfo->is_ecc = 0;
2161 return;
2162 }
2163
2164 }
2165}
2166
2167int update_dimm_Twtr(const struct mem_controller *ctrl, const struct mem_param *param, int i)
2168{
2169
2170 return update_dimm_TT_1_4(ctrl, param, i, DRAM_TIMING_HIGH, SPD_TWTR, DTH_TWTR_SHIFT, DTH_TWTR_MASK, DTH_TWTR_BASE, DTH_TWTR_MIN, DTH_TWTR_MAX);
2171
2172}
2173
2174void set_TT(const struct mem_controller *ctrl, const struct mem_param *param, unsigned TT_REG,
2175 unsigned TT_SHIFT, unsigned TT_MASK, unsigned TT_BASE, unsigned TT_MIN, unsigned TT_MAX, unsigned val, const char *str)
2176{
2177 u32 reg;
2178
2179 if ((val < TT_MIN) || (val > TT_MAX)) {
2180 printk(BIOS_ERR, str);
2181 die(" Unknown\n");
2182 }
2183
2184 reg = pci_conf1_read_config32(ctrl->f2, TT_REG);
2185 reg &= ~(TT_MASK << TT_SHIFT);
2186 reg |= ((val - TT_BASE) << TT_SHIFT);
2187 pci_conf1_write_config32(ctrl->f2, TT_REG, reg);
2188 return;
2189}
2190
2191void set_TrwtTO(const struct mem_controller *ctrl, const struct mem_param *param)
2192{
2193 set_TT(ctrl, param, DRAM_TIMING_HIGH, DTH_TRWTTO_SHIFT, DTH_TRWTTO_MASK,DTH_TRWTTO_BASE, DTH_TRWTTO_MIN, DTH_TRWTTO_MAX, param->TrwtTO, "TrwtTO");
2194}
2195
2196void set_Twrrd(const struct mem_controller *ctrl, const struct mem_param *param)
2197{
2198 set_TT(ctrl, param, DRAM_TIMING_HIGH, DTH_TWRRD_SHIFT, DTH_TWRRD_MASK,DTH_TWRRD_BASE, DTH_TWRRD_MIN, DTH_TWRRD_MAX, param->Twrrd, "Twrrd");
2199}
2200
2201void set_Twrwr(const struct mem_controller *ctrl, const struct mem_param *param)
2202{
2203 set_TT(ctrl, param, DRAM_TIMING_HIGH, DTH_TWRWR_SHIFT, DTH_TWRWR_MASK,DTH_TWRWR_BASE, DTH_TWRWR_MIN, DTH_TWRWR_MAX, param->Twrwr, "Twrwr");
2204}
2205
2206void set_Trdrd(const struct mem_controller *ctrl, const struct mem_param *param)
2207{
2208 set_TT(ctrl, param, DRAM_TIMING_HIGH, DTH_TRDRD_SHIFT, DTH_TRDRD_MASK,DTH_TRDRD_BASE, DTH_TRDRD_MIN, DTH_TRDRD_MAX, param->Trdrd, "Trdrd");
2209}
2210
2211void set_DcqBypassMax(const struct mem_controller *ctrl, const struct mem_param *param)
2212{
2213 set_TT(ctrl, param, DRAM_CONFIG_HIGH, DCH_DcqBypassMax_SHIFT, DCH_DcqBypassMax_MASK,DCH_DcqBypassMax_BASE, DCH_DcqBypassMax_MIN, DCH_DcqBypassMax_MAX, param->DcqByPassMax, "DcqBypassMax");
2214}
2215
2216void set_Tfaw(const struct mem_controller *ctrl, const struct mem_param *param, struct mem_info *meminfo)
2217{
2218 static const u8 faw_1k[] = {8, 10, 13, 14};
2219 static const u8 faw_2k[] = {10, 14, 17, 18};
2220 unsigned memclkfreq_index;
2221 unsigned faw;
2222
2223
2224 memclkfreq_index = param->dch_memclk;
2225
2226 if(meminfo->page_1k_mask != 0) {
2227 faw = faw_1k[memclkfreq_index];
2228 }
2229 else {
2230 faw = faw_2k[memclkfreq_index];
2231 }
2232
2233 set_TT(ctrl, param, DRAM_CONFIG_HIGH, DCH_FourActWindow_SHIFT, DCH_FourActWindow_MASK, DCH_FourActWindow_BASE, DCH_FourActWindow_MIN, DCH_FourActWindow_MAX, faw, "FourActWindow");
2234
2235}
2236
2237
2238void set_max_async_latency(const struct mem_controller *ctrl, const struct mem_param *param)
2239{
2240 u32 dch;
2241 unsigned async_lat;
2242
2243
2244 dch = pci_conf1_read_config32(ctrl->f2, DRAM_CONFIG_HIGH);
2245 dch &= ~(DCH_MaxAsyncLat_MASK << DCH_MaxAsyncLat_SHIFT);
2246
2247
2248 async_lat = 6+6;
2249
2250
2251 dch |= ((async_lat - DCH_MaxAsyncLat_BASE) << DCH_MaxAsyncLat_SHIFT);
2252 pci_conf1_write_config32(ctrl->f2, DRAM_CONFIG_HIGH, dch);
2253}
2254
2255void set_SlowAccessMode(const struct mem_controller *ctrl)
2256{
2257 u32 dch;
2258
2259 dch = pci_conf1_read_config32(ctrl->f2, DRAM_CONFIG_HIGH);
2260
2261 dch |= (1<<20);
2262
2263 pci_conf1_write_config32(ctrl->f2, DRAM_CONFIG_HIGH, dch);
2264}
2265
2266
2267
2268
2269
2270
2271void set_misc_timing(const struct mem_controller *ctrl, struct mem_info *meminfo)
2272{
2273 u32 dword;
2274 u32 dwordx;
2275 long dimm_mask = meminfo->dimm_mask & 0x0f;
2276
2277#if DIMM_SUPPORT==0x0104
2278
2279 dword = 0x00111222;
2280 dwordx = 0x002f0000;
2281 switch (meminfo->memclk_set) {
2282 case DCH_MemClkFreq_266MHz:
2283 if( (dimm_mask == 0x03) || (dimm_mask == 0x02) || (dimm_mask == 0x01)) {
2284 dwordx = 0x002f2700;
2285 }
2286 break;
2287 case DCH_MemClkFreq_333MHz:
2288 if( (dimm_mask == 0x03) || (dimm_mask == 0x02) || (dimm_mask == 0x01)) {
2289 if ((meminfo->single_rank_mask & 0x03)!=0x03) {
2290 dwordx = 0x002f2f00;
2291 }
2292 }
2293 break;
2294 case DCH_MemClkFreq_400MHz:
2295 dwordx = 0x002f3300;
2296 break;
2297 }
2298
2299#endif
2300
2301#if DIMM_SUPPORT==0x0004
2302 unsigned SlowAccessMode = 0;
2303
2304 dword = 0x00111222;
2305 dwordx = 0x002f2f00;
2306 switch (meminfo->memclk_set) {
2307 case DCH_MemClkFreq_200MHz:
2308 if(dimm_mask == 0x03) {
2309 SlowAccessMode = 1;
2310 dword = 0x00111322;
2311 }
2312 break;
2313 case DCH_MemClkFreq_266MHz:
2314 if(dimm_mask == 0x03) {
2315 SlowAccessMode = 1;
2316 dword = 0x00111322;
2317 if((meminfo->x4_mask == 0 ) && (meminfo->x16_mask == 0)) {
2318 switch (meminfo->single_rank_mask) {
2319 case 0x03:
2320 dwordx = 0x00002f00;
2321 break;
2322 case 0x00:
2323 dwordx = 0x00342f00;
2324 break;
2325 default:
2326 dwordx = 0x00372f00;
2327 }
2328 } else if((meminfo->x4_mask == 0 ) && (meminfo->x16_mask == 0x01) && (meminfo->single_rank_mask == 0x01)) {
2329 dwordx = 0x00382f00;
2330 } else if((meminfo->x4_mask == 0 ) && (meminfo->x16_mask == 0x02) && (meminfo->single_rank_mask == 0x02)) {
2331 dwordx = 0x00382f00;
2332 }
2333
2334 }
2335 else {
2336 if((meminfo->x4_mask == 0 ) && (meminfo->x16_mask == 0x00) && ((meminfo->single_rank_mask == 0x01)||(meminfo->single_rank_mask == 0x02))) {
2337 dwordx = 0x002f2f00;
2338 } else {
2339 dwordx = 0x002b2f00;
2340 }
2341 }
2342 break;
2343 case DCH_MemClkFreq_333MHz:
2344 dwordx = 0x00202220;
2345 if(dimm_mask == 0x03) {
2346 SlowAccessMode = 1;
2347 dword = 0x00111322;
2348 if((meminfo->x4_mask == 0 ) && (meminfo->x16_mask == 0)) {
2349 switch (meminfo->single_rank_mask) {
2350 case 0x03:
2351 dwordx = 0x00302220;
2352 break;
2353 case 0x00:
2354 dwordx = 0x002b2220;
2355 break;
2356 default:
2357 dwordx = 0x002a2220;
2358 }
2359 } else if((meminfo->x4_mask == 0) && (meminfo->x16_mask == 0x01) && (meminfo->single_rank_mask == 0x01)) {
2360 dwordx = 0x002c2220;
2361 } else if((meminfo->x4_mask == 0) && (meminfo->x16_mask == 0x02) && (meminfo->single_rank_mask == 0x02)) {
2362 dwordx = 0x002c2220;
2363 }
2364 }
2365 break;
2366 case DCH_MemClkFreq_400MHz:
2367 dwordx = 0x00202520;
2368 SlowAccessMode = 1;
2369 if(dimm_mask == 0x03) {
2370 dword = 0x00113322;
2371 } else {
2372 dword = 0x00113222;
2373 }
2374 break;
2375 }
2376
2377 printk(BIOS_DEBUG, "\tdimm_mask = 0x%x\n", meminfo->dimm_mask);
2378 printk(BIOS_DEBUG, "\tx4_mask = 0x%x\n", meminfo->x4_mask);
2379 printk(BIOS_DEBUG, "\tx16_mask = 0x%x\n", meminfo->x16_mask);
2380 printk(BIOS_DEBUG, "\tsingle_rank_mask = 0x%x\n", meminfo->single_rank_mask);
2381 printk(BIOS_DEBUG, "\tODC = 0x%x\n", dword);
2382 printk(BIOS_DEBUG, "\tAddr Timing= 0x%x\n", dwordx);
2383#endif
2384
2385#if (DIMM_SUPPORT & 0x0100)==0x0000
2386 if(SlowAccessMode) {
2387 set_SlowAccessMode(ctrl);
2388 }
2389#endif
2390
2391
2392 pci_write_config32_index_wait(ctrl->f2, 0x98, 0, dword);
2393 if(meminfo->is_Width128) {
2394 pci_write_config32_index_wait(ctrl->f2, 0x98, 0x20, dword);
2395 }
2396
2397
2398 pci_write_config32_index_wait(ctrl->f2, 0x98, 4, dwordx);
2399 if(meminfo->is_Width128) {
2400 pci_write_config32_index_wait(ctrl->f2, 0x98, 0x24, dwordx);
2401 }
2402
2403}
2404
2405
2406void set_RDqsEn(const struct mem_controller *ctrl, const struct mem_param *param, struct mem_info *meminfo)
2407{
2408#if CPU_SOCKET_TYPE==SOCKET_L1
2409
2410 u32 dch;
2411
2412 dch = pci_conf1_read_config32(ctrl->f2, DRAM_CONFIG_HIGH);
2413
2414 dch &= ~DCH_RDqsEn;
2415 if((!meminfo->x4_mask) && (!meminfo->x16_mask)) {
2416 dch |= DCH_RDqsEn;
2417 }
2418
2419 pci_conf1_write_config32(ctrl->f2, DRAM_CONFIG_HIGH, dch);
2420#endif
2421}
2422
2423
2424void set_idle_cycle_limit(const struct mem_controller *ctrl, const struct mem_param *param)
2425{
2426 u32 dcm;
2427
2428 dcm = pci_conf1_read_config32(ctrl->f2, DRAM_CTRL_MISC);
2429 dcm &= ~(DCM_ILD_lmt_MASK << DCM_ILD_lmt_SHIFT);
2430 dcm |= DCM_ILD_lmt_16 << DCM_ILD_lmt_SHIFT;
2431 dcm |= DCM_DCC_EN;
2432 pci_conf1_write_config32(ctrl->f2, DRAM_CTRL_MISC, dcm);
2433}
2434
2435void set_RdWrQByp(const struct mem_controller *ctrl, const struct mem_param *param)
2436{
2437 set_TT(ctrl, param, DRAM_CTRL_MISC, DCM_RdWrQByp_SHIFT, DCM_RdWrQByp_MASK,0, 0, 3, 2, "RdWrQByp");
2438}
2439
2440
2441
2442long spd_set_dram_timing(const struct mem_controller *ctrl, const struct mem_param *param, long dimm_mask, struct mem_info *meminfo)
2443{
2444 int i;
2445
2446 for(i = 0; i < DIMM_SOCKETS; i++) {
2447 int rc;
2448 if (!(dimm_mask & (1 << i))) {
2449 continue;
2450 }
2451 printk(BIOS_DEBUG, "%s: 0x%x\n", "dimm socket: ", i);
2452
2453 printk(BIOS_DEBUG, "\ttrc\n");
2454 if ((rc = update_dimm_Trc (ctrl, param, i)) <= 0) goto dimm_err;
2455
2456 printk(BIOS_DEBUG, "\ttrcd\n");
2457 if ((rc = update_dimm_Trcd(ctrl, param, i)) <= 0) goto dimm_err;
2458
2459 printk(BIOS_DEBUG, "\ttrrd\n");
2460 if ((rc = update_dimm_Trrd(ctrl, param, i)) <= 0) goto dimm_err;
2461
2462 printk(BIOS_DEBUG, "\ttras\n");
2463 if ((rc = update_dimm_Tras(ctrl, param, i)) <= 0) goto dimm_err;
2464
2465 printk(BIOS_DEBUG, "\ttrp\n");
2466 if ((rc = update_dimm_Trp (ctrl, param, i)) <= 0) goto dimm_err;
2467
2468 printk(BIOS_DEBUG, "\ttrtp\n");
2469 if ((rc = update_dimm_Trtp(ctrl, param, i, meminfo)) <= 0) goto dimm_err;
2470
2471 printk(BIOS_DEBUG, "\ttwr\n");
2472 if ((rc = update_dimm_Twr (ctrl, param, i)) <= 0) goto dimm_err;
2473
2474
2475 printk(BIOS_DEBUG, "\ttref\n");
2476 if ((rc = update_dimm_Tref(ctrl, param, i)) <= 0) goto dimm_err;
2477
2478 printk(BIOS_DEBUG, "\ttwtr\n");
2479 if ((rc = update_dimm_Twtr(ctrl, param, i)) <= 0) goto dimm_err;
2480
2481 printk(BIOS_DEBUG, "\ttrfc\n");
2482 if ((rc = update_dimm_Trfc(ctrl, param, i, meminfo)) <= 0) goto dimm_err;
2483
2484
2485
2486 continue;
2487 dimm_err:
2488 if (rc < 0) {
2489 return -1;
2490 }
2491 dimm_mask = disable_dimm(ctrl, i, dimm_mask, meminfo);
2492 }
2493
2494 meminfo->dimm_mask = dimm_mask;
2495
2496 get_extra_dimm_mask(ctrl, meminfo);
2497
2498
2499
2500 set_TrwtTO(ctrl, param);
2501 set_Twrrd (ctrl, param);
2502 set_Twrwr (ctrl, param);
2503 set_Trdrd (ctrl, param);
2504
2505 set_4RankRDimm(ctrl, param, meminfo);
2506
2507
2508 set_Tfaw(ctrl, param, meminfo);
2509 set_DcqBypassMax(ctrl, param);
2510 set_max_async_latency(ctrl, param);
2511 set_RDqsEn(ctrl, param, meminfo);
2512
2513
2514 set_ecc(ctrl, param, dimm_mask, meminfo);
2515 set_dimm_x4(ctrl, param, meminfo);
2516 set_DramTerm(ctrl, param, meminfo);
2517
2518
2519 set_idle_cycle_limit(ctrl, param);
2520 set_RdWrQByp(ctrl, param);
2521
2522 return dimm_mask;
2523}
2524
2525void sdram_set_spd_registers(const struct mem_controller *ctrl, struct sys_info *sysinfo)
2526{
2527 struct spd_set_memclk_result result;
2528 const struct mem_param *param;
2529 struct mem_param paramx;
2530 struct mem_info *meminfo;
2531 long dimm_mask;
2532 void activate_spd_rom(const struct mem_controller *ctrl);
2533 void hard_reset(void);
2534#if 1
2535 if (!sysinfo->ctrl_present[ctrl->node_id]) {
2536 return;
2537 }
2538#endif
2539 meminfo = &sysinfo->meminfo[ctrl->node_id];
2540
2541 printk(BIOS_DEBUG, "FIXME sdram_set_spd_registers: paramx :%p\n", ¶mx);
2542
2543 activate_spd_rom(ctrl);
2544 dimm_mask = spd_detect_dimms(ctrl);
2545 if (!(dimm_mask & ((1 << DIMM_SOCKETS) - 1))) {
2546 printk(BIOS_DEBUG, "No memory for this cpu\n");
2547 return;
2548 }
2549 dimm_mask = spd_enable_2channels(ctrl, dimm_mask, meminfo);
2550 if (dimm_mask < 0)
2551 goto hw_spd_err;
2552 dimm_mask = spd_set_ram_size(ctrl , dimm_mask, meminfo);
2553 if (dimm_mask < 0)
2554 goto hw_spd_err;
2555 dimm_mask = spd_handle_unbuffered_dimms(ctrl, dimm_mask, meminfo);
2556 if (dimm_mask < 0)
2557 goto hw_spd_err;
2558 result = spd_set_memclk(ctrl, dimm_mask, meminfo);
2559 param = result.param;
2560 dimm_mask = result.dimm_mask;
2561 if (dimm_mask < 0)
2562 goto hw_spd_err;
2563
2564
2565 meminfo->memclk_set = param->dch_memclk;
2566
2567 memcpy(¶mx, param, sizeof(paramx));
2568
2569 paramx.divisor = get_exact_divisor(param->dch_memclk, paramx.divisor);
2570
2571 dimm_mask = spd_set_dram_timing(ctrl, ¶mx , dimm_mask, meminfo);
2572 if (dimm_mask < 0)
2573 goto hw_spd_err;
2574
2575 order_dimms(ctrl, meminfo);
2576
2577 return;
2578 hw_spd_err:
2579
2580 printk(BIOS_ERR, "SPD error - reset\n");
2581 hard_reset();
2582 return;
2583}
2584
2585#define TIMEOUT_LOOPS 300000
2586
2587
2588
2589#if CONFIG_HW_MEM_HOLE_SIZEK != 0
2590u32 hoist_memory(int controllers, const struct mem_controller *ctrl,unsigned hole_startk, int i)
2591{
2592 int ii;
2593 u32 carry_over;
2594 u32 dev;
2595 u32 base, limit;
2596 u32 basek;
2597 u32 hoist;
2598 int j;
2599
2600 carry_over = (4*1024*1024) - hole_startk;
2601
2602 for(ii=controllers - 1;ii>i;ii--) {
2603 base = pci_conf1_read_config32(ctrl[0].f1, 0x40 + (ii << 3));
2604 if ((base & ((1<<1)|(1<<0))) != ((1<<1)|(1<<0))) {
2605 continue;
2606 }
2607 limit = pci_conf1_read_config32(ctrl[0].f1, 0x44 + (ii << 3));
2608 limit += (carry_over << 2 );
2609 base += (carry_over << 2 );
2610 for(j = 0; j < controllers; j++) {
2611 pci_conf1_write_config32(ctrl[j].f1, 0x44 + (ii << 3), limit);
2612 pci_conf1_write_config32(ctrl[j].f1, 0x40 + (ii << 3), base );
2613 }
2614 }
2615 limit = pci_conf1_read_config32(ctrl[0].f1, 0x44 + (i << 3));
2616 limit += (carry_over << 2);
2617 for(j = 0; j < controllers; j++) {
2618 pci_conf1_write_config32(ctrl[j].f1, 0x44 + (i << 3), limit);
2619 }
2620 dev = ctrl[i].f1;
2621 base = pci_conf1_read_config32(dev, 0x40 + (i << 3));
2622 basek = (base & 0xffff0000) >> 2;
2623 if(basek == hole_startk) {
2624
2625
2626 base &= 0x0000ffff;
2627 base |= (4*1024*1024)<<2;
2628 for(j = 0; j < controllers; j++) {
2629 pci_conf1_write_config32(ctrl[j].f1, 0x40 + (i<<3), base);
2630 }
2631 }
2632 else
2633 {
2634 hoist =
2635 ((hole_startk << 10) & 0xff000000) +
2636
2637 (((basek + carry_over) >> 6) & 0x0000ff00) +
2638
2639 1;
2640 pci_conf1_write_config32(dev, 0xf0, hoist);
2641 }
2642
2643 return carry_over;
2644}
2645
2646void set_hw_mem_hole(int controllers, const struct mem_controller *ctrl)
2647{
2648
2649 u32 hole_startk;
2650 int i;
2651
2652 hole_startk = 4*1024*1024 - CONFIG_HW_MEM_HOLE_SIZEK;
2653
2654#if HW_MEM_HOLE_SIZE_AUTO_INC == 1
2655
2656 u32 basek_pri = 0;
2657 for(i=0; i<controllers; i++) {
2658 u32 base;
2659 u32 base_k;
2660 base = pci_conf1_read_config32(ctrl[0].f1, 0x40 + (i << 3));
2661 if ((base & ((1<<1)|(1<<0))) != ((1<<1)|(1<<0))) {
2662 continue;
2663 }
2664 base_k = (base & 0xffff0000) >> 2;
2665 if(base_k == hole_startk) {
2666 hole_startk -= (base_k - basek_pri)>>1;
2667 break;
2668 }
2669 basek_pri = base_k;
2670 }
2671#endif
2672
2673 for(i=0; i<controllers; i++) {
2674 u32 base, limit;
2675 unsigned base_k, limit_k;
2676 base = pci_conf1_read_config32(ctrl[0].f1, 0x40 + (i << 3));
2677 if ((base & ((1<<1)|(1<<0))) != ((1<<1)|(1<<0))) {
2678 continue;
2679 }
2680 limit = pci_conf1_read_config32(ctrl[0].f1, 0x44 + (i << 3));
2681 base_k = (base & 0xffff0000) >> 2;
2682 limit_k = ((limit + 0x00010000) & 0xffff0000) >> 2;
2683 if ((base_k <= hole_startk) && (limit_k > hole_startk)) {
2684 unsigned end_k;
2685 hoist_memory(controllers, ctrl, hole_startk, i);
2686 end_k = memory_end_k(ctrl, controllers);
2687 set_top_mem(end_k, hole_startk);
2688 break;
2689 }
2690 }
2691
2692}
2693
2694#endif
2695static void wait_all_core0_mem_trained(struct sys_info *sysinfo)
2696{
2697
2698 int i;
2699 u32 mask = 0;
2700 unsigned int needs_reset = 0;
2701 void hard_reset(void);
2702
2703 if(sysinfo->nodes == 1) return;
2704
2705 for(i=1; i<sysinfo->nodes; i++) {
2706
2707 if(sysinfo->mem_trained[i]==0x00) continue;
2708
2709 mask |= (1<<i);
2710
2711 }
2712
2713 i = 1;
2714 while(1) {
2715 if(mask & (1<<i)) {
2716 if((sysinfo->mem_trained[i])!=0x80) {
2717 mask &= ~(1<<i);
2718 }
2719 }
2720
2721 if(!mask) break;
2722
2723 i++;
2724 i%=sysinfo->nodes;
2725 }
2726
2727 for(i=0; i<sysinfo->nodes; i++) {
2728 printk(BIOS_DEBUG, "mem_trained[%02x]=%02x\n", i, sysinfo->mem_trained[i]);
2729 switch(sysinfo->mem_trained[i]) {
2730 case 0:
2731 case 1:
2732 break;
2733 case 0x81:
2734 case 0x82:
2735 case 0x83:
2736 needs_reset = 1;
2737 break;
2738 }
2739 }
2740 if(needs_reset) {
2741 printk(BIOS_DEBUG, "mem trained failed\n");
2742 hard_reset();
2743 }
2744
2745}
2746
2747void sdram_enable(int controllers, const struct mem_controller *ctrl, struct sys_info *sysinfo)
2748{
2749 int i;
2750 void dqs_timing(int controllers, const struct mem_controller *ctrl, struct sys_info *sysinfo);
2751 void memreset(int controllers, const struct mem_controller *ctrl);
2752
2753
2754 if (memory_end_k(ctrl, controllers) == 0) {
2755 die("No memory\n");
2756 }
2757
2758
2759 for(i = 0; i < controllers; i++) {
2760 u32 dch;
2761 if (!sysinfo->ctrl_present[ i ])
2762 continue;
2763 dch = pci_conf1_read_config32(ctrl[i].f2, DRAM_CONFIG_HIGH);
2764
2765
2766 if(sysinfo->meminfo[i].dimm_mask==0x00){
2767 dch |= DCH_DisDramInterface;
2768 pci_conf1_write_config32(ctrl[i].f2, DRAM_CONFIG_HIGH, dch);
2769
2770 }
2771 else {
2772 dch |= DCH_MemClkFreqVal;
2773 pci_conf1_write_config32(ctrl[i].f2, DRAM_CONFIG_HIGH, dch);
2774
2775 set_misc_timing(ctrl+i, sysinfo->meminfo+i );
2776 }
2777 }
2778
2779
2780 memreset(controllers, ctrl);
2781#if 1
2782 printk(BIOS_DEBUG, "prepare to InitDram:");
2783 for(i=0; i<10; i++) {
2784 printk(BIOS_DEBUG, "%08x\b\b\b\b\b\b\b\b", i);
2785 }
2786 printk(BIOS_DEBUG, "\n");
2787#endif
2788
2789 for(i = 0; i < controllers; i++) {
2790 u32 dcl, dch;
2791 if (!sysinfo->ctrl_present[ i ])
2792 continue;
2793
2794 dch = pci_conf1_read_config32(ctrl[i].f2, DRAM_CONFIG_HIGH);
2795 if (!(dch & DCH_MemClkFreqVal)) {
2796 continue;
2797 }
2798
2799
2800 dcl = pci_conf1_read_config32(ctrl[i].f2, DRAM_CONFIG_LOW);
2801 if (dcl & DCL_DimmEccEn) {
2802 u32 mnc;
2803 printk(BIOS_SPEW, "ECC enabled\n");
2804 mnc = pci_conf1_read_config32(ctrl[i].f3, MCA_NB_CONFIG);
2805 mnc |= MNC_ECC_EN;
2806 if (dcl & DCL_Width128) {
2807 mnc |= MNC_CHIPKILL_EN;
2808 }
2809 pci_conf1_write_config32(ctrl[i].f3, MCA_NB_CONFIG, mnc);
2810 }
2811
2812#if 0
2813
2814 dword = pci_conf1_read_config32(ctrl[i].f2, DRAM_CTRL);
2815 dword |= DC_DqsRcvEnTrain;
2816 pci_conf1_write_config32(ctrl[i].f2, DRAM_CTRL, dword);
2817#endif
2818
2819 pci_conf1_write_config32(ctrl[i].f2, DRAM_CONFIG_LOW, dcl);
2820 dcl |= DCL_InitDram;
2821 pci_conf1_write_config32(ctrl[i].f2, DRAM_CONFIG_LOW, dcl);
2822
2823 }
2824
2825 for(i = 0; i < controllers; i++) {
2826 u32 dcl, dcm;
2827 if (!sysinfo->ctrl_present[ i ])
2828 continue;
2829
2830 if(sysinfo->meminfo[i].dimm_mask==0x00) continue;
2831
2832 printk(BIOS_DEBUG, "Initializing memory: ");
2833 int loops = 0;
2834 do {
2835 dcl = pci_conf1_read_config32(ctrl[i].f2, DRAM_CONFIG_LOW);
2836 loops++;
2837 if ((loops & 1023) == 0) {
2838 printk(BIOS_DEBUG, ".");
2839 }
2840 } while(((dcl & DCL_InitDram) != 0) && (loops < TIMEOUT_LOOPS));
2841 if (loops >= TIMEOUT_LOOPS) {
2842 printk(BIOS_DEBUG, " failed\n");
2843 continue;
2844 }
2845
2846
2847 do {
2848 dcm = pci_conf1_read_config32(ctrl[i].f2, DRAM_CTRL_MISC);
2849 } while(((dcm & DCM_MemClrStatus) == 0) );
2850
2851 printk(BIOS_DEBUG, " done\n");
2852 }
2853
2854#if CONFIG_HW_MEM_HOLE_SIZEK != 0
2855
2856
2857 set_hw_mem_hole(controllers, ctrl);
2858#endif
2859
2860
2861 {
2862 struct msr msr;
2863
2864 msr = rdmsr(TOP_MEM);
2865 sysinfo->tom_k = ((msr.hi<<24) | (msr.lo>>8))>>2;
2866
2867
2868 msr = rdmsr(TOP_MEM2);
2869 sysinfo->tom2_k = ((msr.hi<<24)| (msr.lo>>8))>>2;
2870 }
2871
2872 for(i = 0; i < controllers; i++) {
2873 sysinfo->mem_trained[i] = 0;
2874
2875 if (!sysinfo->ctrl_present[ i ])
2876 continue;
2877
2878
2879 if(sysinfo->meminfo[i].dimm_mask==0x00)
2880 continue;
2881
2882 sysinfo->mem_trained[i] = 0x80;
2883 }
2884
2885
2886#if MEM_TRAIN_SEQ == 0
2887 dqs_timing(controllers, ctrl, sysinfo);
2888#else
2889
2890 #if MEM_TRAIN_SEQ == 2
2891
2892 setup_mtrr_dqs(sysinfo->tom_k, sysinfo->tom2_k);
2893 #endif
2894
2895 for(i = 0; i < controllers; i++) {
2896
2897 if(sysinfo->mem_trained[i]!=0x80)
2898 continue;
2899
2900 dqs_timing(i, &ctrl[i], sysinfo);
2901
2902 #if MEM_TRAIN_SEQ == 1
2903 break;
2904 #endif
2905 }
2906
2907 #if MEM_TRAIN_SEQ == 2
2908 clear_mtrr_dqs(sysinfo->tom2_k);
2909 #endif
2910
2911#endif
2912
2913#if MEM_TRAIN_SEQ != 1
2914 wait_all_core0_mem_trained(sysinfo);
2915#endif
2916
2917}
2918void fill_mem_ctrl(int controllers, struct mem_controller *ctrl_a, const u16 *spd_addr)
2919{
2920 int i;
2921 int j;
2922 struct mem_controller *ctrl;
2923 for(i=0;i<controllers; i++) {
2924 ctrl = &ctrl_a[i];
2925 ctrl->node_id = i;
2926 ctrl->f0 = PCI_BDF(0, 0x18+i, 0);
2927 ctrl->f1 = PCI_BDF(0, 0x18+i, 1);
2928 ctrl->f2 = PCI_BDF(0, 0x18+i, 2);
2929 ctrl->f3 = PCI_BDF(0, 0x18+i, 3);
2930
2931 if(spd_addr == (void *)0) continue;
2932
2933 for(j=0;j<DIMM_SOCKETS;j++) {
2934 ctrl->channel0[j] = spd_addr[(i*2+0)*DIMM_SOCKETS + j];
2935 ctrl->channel1[j] = spd_addr[(i*2+1)*DIMM_SOCKETS + j];
2936 }
2937 }
2938}
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948void sdram_initialize(int controllers, const struct mem_controller *ctrl, struct sys_info *sysinfo)
2949{
2950 int i;
2951
2952 for(i = 0; i < controllers; i++) {
2953 printk(BIOS_DEBUG, "Ram1.%d, ",i);
2954
2955 sdram_set_registers(ctrl + i , sysinfo);
2956 }
2957
2958
2959 for(i = 0; i < controllers; i++) {
2960 printk(BIOS_DEBUG, "Ram2.%d, ",i);
2961 sdram_set_spd_registers(ctrl + i , sysinfo);
2962
2963 }
2964
2965
2966
2967
2968
2969 printk(BIOS_DEBUG, "Ram3\n");
2970
2971 sdram_enable(controllers, ctrl, sysinfo);
2972
2973 printk(BIOS_DEBUG, "Ram4\n");
2974
2975}
2976