Since some people disapprove of white space cleanups mixed in regular commits
[coreboot.git] / util / romcc / tests / simple_test73.c
1 static const char *addr_of_char(unsigned char ch)
2 {
3         static const char byte[] = {
4                 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
5                 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
6                 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
7                 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
8                 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
9                 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
10                 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
11                 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
12                 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
13                 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
14                 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
15                 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f,
16                 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
17                 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
18                 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,
19                 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f,
20                 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
21                 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
22                 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
23                 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f,
24                 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
25                 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf,
26                 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7,
27                 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf,
28                 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
29                 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf,
30                 0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7,
31                 0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf,
32                 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7,
33                 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef,
34                 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
35                 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff,
36         };
37         return byte + ch;
38 }
39
40 static 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
48 static 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
58 static int smbus_read_byte(unsigned device, unsigned address)
59 {
60         static const unsigned char dimm[] = {
61 0x80, 0x08, 0x07, 0x0d, 0x0a, 0x02, 0x48, 0x00, 0x04, 0x60, 0x70, 0x02, 0x82, 0x08, 0x08, 0x01,
62 0x0e, 0x04, 0x0c, 0x01, 0x02, 0x20, 0x00, 0x75, 0x70, 0x00, 0x00, 0x48, 0x30, 0x48, 0x2a, 0x40,
63 0x80, 0x80, 0x45, 0x45, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
64 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x33,
65 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
66 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
67 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
68 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
69 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
70 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
71 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
72 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
73 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
74 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
75 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
76 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
77
78 0x80, 0x08, 0x07, 0x0d, 0x0a, 0x02, 0x48, 0x00, 0x04, 0x60, 0x70, 0x02, 0x82, 0x08, 0x08, 0x01,
79 0x0e, 0x04, 0x0c, 0x01, 0x02, 0x20, 0x00, 0x75, 0x70, 0x00, 0x00, 0x48, 0x30, 0x48, 0x2a, 0x40,
80 0x80, 0x80, 0x45, 0x45, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
81 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x33,
82 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
83 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
84 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
85 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
86 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
87 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
88 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
89 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
90 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
91 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
92 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
93 0xff, 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
103 static 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 }