blob: da69cf5e7d59d5072a84b16e95014ed3819c4162 [file] [log] [blame]
Eric Biederman5ade04a2003-10-22 04:03:46 +00001static const char *addr_of_char(unsigned char ch)
2{
3 static const char byte[] = {
Stefan Reinauer14e22772010-04-27 06:56:47 +00004 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
Eric Biederman5ade04a2003-10-22 04:03:46 +00005 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
Stefan Reinauer14e22772010-04-27 06:56:47 +00006 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
Eric Biederman5ade04a2003-10-22 04:03:46 +00007 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
Stefan Reinauer14e22772010-04-27 06:56:47 +00008 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
Eric Biederman5ade04a2003-10-22 04:03:46 +00009 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
Stefan Reinauer14e22772010-04-27 06:56:47 +000010 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
Eric Biederman5ade04a2003-10-22 04:03:46 +000011 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
Stefan Reinauer14e22772010-04-27 06:56:47 +000012 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
Eric Biederman5ade04a2003-10-22 04:03:46 +000013 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
Stefan Reinauer14e22772010-04-27 06:56:47 +000014 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
Eric Biederman5ade04a2003-10-22 04:03:46 +000015 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f,
Stefan Reinauer14e22772010-04-27 06:56:47 +000016 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
Eric Biederman5ade04a2003-10-22 04:03:46 +000017 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
Stefan Reinauer14e22772010-04-27 06:56:47 +000018 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,
Eric Biederman5ade04a2003-10-22 04:03:46 +000019 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f,
Stefan Reinauer14e22772010-04-27 06:56:47 +000020 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
Eric Biederman5ade04a2003-10-22 04:03:46 +000021 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
Stefan Reinauer14e22772010-04-27 06:56:47 +000022 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
Eric Biederman5ade04a2003-10-22 04:03:46 +000023 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f,
Stefan Reinauer14e22772010-04-27 06:56:47 +000024 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
Eric Biederman5ade04a2003-10-22 04:03:46 +000025 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf,
Stefan Reinauer14e22772010-04-27 06:56:47 +000026 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7,
Eric Biederman5ade04a2003-10-22 04:03:46 +000027 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf,
Stefan Reinauer14e22772010-04-27 06:56:47 +000028 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
Eric Biederman5ade04a2003-10-22 04:03:46 +000029 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf,
Stefan Reinauer14e22772010-04-27 06:56:47 +000030 0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7,
Eric Biederman5ade04a2003-10-22 04:03:46 +000031 0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf,
Stefan Reinauer14e22772010-04-27 06:56:47 +000032 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7,
Eric Biederman5ade04a2003-10-22 04:03:46 +000033 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef,
Stefan Reinauer14e22772010-04-27 06:56:47 +000034 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
Eric Biederman5ade04a2003-10-22 04:03:46 +000035 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff,
36 };
37 return byte + ch;
38}
39
40static void console_tx_byte(unsigned char ch)
41{
42 asm volatile(
43 "int $0x80"
44 :
45 : "a" (4), "b" (1), "c" (addr_of_char(ch)), "d" (1));
46}
47
48static void console_tx_string(const char *str)
49{
50 unsigned char ch;
51 while((ch = *str++) != '\0') {
52 console_tx_byte(ch);
53 }
54}
55
56
57
58static int smbus_read_byte(unsigned device, unsigned address)
59{
60 static const unsigned char dimm[] = {
610x80, 0x08, 0x07, 0x0d, 0x0a, 0x02, 0x48, 0x00, 0x04, 0x60, 0x70, 0x02, 0x82, 0x08, 0x08, 0x01,
620x0e, 0x04, 0x0c, 0x01, 0x02, 0x20, 0x00, 0x75, 0x70, 0x00, 0x00, 0x48, 0x30, 0x48, 0x2a, 0x40,
630x80, 0x80, 0x45, 0x45, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
640x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x33,
650xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
660xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
670xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
680xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
690xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
700xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
710xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
720xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
730xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
740xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
750xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
760xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
77
780x80, 0x08, 0x07, 0x0d, 0x0a, 0x02, 0x48, 0x00, 0x04, 0x60, 0x70, 0x02, 0x82, 0x08, 0x08, 0x01,
790x0e, 0x04, 0x0c, 0x01, 0x02, 0x20, 0x00, 0x75, 0x70, 0x00, 0x00, 0x48, 0x30, 0x48, 0x2a, 0x40,
800x80, 0x80, 0x45, 0x45, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
810x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x33,
820xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
830xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
840xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
850xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
860xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
870xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
880xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
890xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
900xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
910xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
920xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
930xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
94 };
95 return dimm[(device << 8) + address];
96}
97
98#define SMBUS_MEM_DEVICE_START 0x00
99#define SMBUS_MEM_DEVICE_END 0x00
100#define SMBUS_MEM_DEVICE_INC 1
101
102
103static void spd_set_memclk(void)
104{
105 /* Compute the minimum cycle time for these dimms */
106 unsigned min_cycle_time, min_latency;
107 unsigned device;
108 unsigned value;
109
110 value = 0x50;
111 min_cycle_time = 0x75;
112 min_latency = 2;
113 device = 0;
114
115 /* Compute the least latency with the fastest clock supported
116 * by both the memory controller and the dimms.
117 */
118 for(device = SMBUS_MEM_DEVICE_START;
119 device <= SMBUS_MEM_DEVICE_END;
120 device += SMBUS_MEM_DEVICE_INC)
121 {
122 int new_cycle_time, new_latency;
123 int index;
124 int latencies;
125 int latency;
126
127 /* First find the supported CAS latencies
128 * Byte 18 for DDR SDRAM is interpreted:
129 * bit 0 == CAS Latency = 1.0
130 * bit 1 == CAS Latency = 1.5
131 * bit 2 == CAS Latency = 2.0
132 * bit 3 == CAS Latency = 2.5
133 * bit 4 == CAS Latency = 3.0
134 * bit 5 == CAS Latency = 3.5
135 * bit 6 == TBD
136 * bit 7 == TBD
137 */
138 new_cycle_time = 0xa0;
139 new_latency = 5;
140
141 latencies = smbus_read_byte(device, 18);
142
143 /* Compute the lowest cas latency supported */
144 latency = __builtin_bsr(latencies) -2;
145
146 /* Loop through and find a fast clock with a low latency */
147 for(index = 0; index < 3; index++, latency++) {
148 int value;
149 if ((latency < 2) || (latency > 4) ||
150 (!(latencies & (1 << latency)))) {
151 continue;
152 }
153 value = smbus_read_byte(device, index);
154 if (value < 0) continue;
155
156 /* Only increase the latency if we decreas the clock */
157 if ((value >= min_cycle_time) && (value < new_cycle_time)) {
158 new_cycle_time = value;
159 new_latency = latency;
160 console_tx_byte(device);
161
162 }
163 }
164 console_tx_string("Transmitting this string causes problems\n");
165 /* Does min_latency need to be increased? */
166 if (new_cycle_time > min_cycle_time) {
167 min_cycle_time = new_cycle_time;
168 }
169 /* Does min_cycle_time need to be increased? */
170 if (new_latency > min_latency) {
171 min_latency = new_latency;
172 }
173 }
174}