8ef1b4c6500c9346e8b59120d15a21d09817f779
[coreboot.git] / src / mainboard / asrock / e350m1 / dimmSpd.c
1 /*
2  * This file is part of the coreboot project.
3  *
4  * Copyright (C) 2011 Advanced Micro Devices, Inc.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; version 2 of the License.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
18  */
19  
20 #include "Porting.h"
21 #include "AGESA.h"
22 #include "amdlib.h"
23
24 AGESA_STATUS AmdMemoryReadSPD (UINT32 unused1, UINT32 unused2, AGESA_READ_SPD_PARAMS *info);
25 #define DIMENSION(array)(sizeof (array)/ sizeof (array [0]))
26
27 /*#pragma optimize ("", off) // for source level debug
28 *---------------------------------------------------------------------------
29 *
30 * SPD address table - porting required
31 */
32
33 static const UINT8 spdAddressLookup [2] [2] [4] =  // socket, channel, dimm
34    {
35    // socket 0
36       {
37          {0xA0, 0xA4},  // channel 0 dimms
38          {0x00, 0x00},  // channel 1 dimms
39       },
40    // socket 1
41       {
42          {0x00, 0x00},  // channel 0 dimms
43          {0x00, 0x00},  // channel 1 dimms
44       },
45    };
46
47 /*-----------------------------------------------------------------------------
48  *
49  * readSmbusByteData - read a single SPD byte from any offset
50  */
51
52 static int readSmbusByteData (int iobase, int address, char *buffer, int offset)
53    {
54    unsigned int status;
55    UINT64 limit;
56
57    address |= 1; // set read bit
58    
59    __outbyte (iobase + 0, 0xFF);                // clear error status
60    __outbyte (iobase + 1, 0x1F);                // clear error status
61    __outbyte (iobase + 3, offset);              // offset in eeprom
62    __outbyte (iobase + 4, address);             // slave address and read bit
63    __outbyte (iobase + 2, 0x48);                // read byte command
64
65    // time limit to avoid hanging for unexpected error status (should never happen)
66    limit = __rdtsc () + 2000000000 / 10;
67    for (;;)
68       {
69       status = __inbyte (iobase);
70       if (__rdtsc () > limit) break;
71       if ((status & 2) == 0) continue;               // SMBusInterrupt not set, keep waiting
72       if ((status & 1) == 1) continue;               // HostBusy set, keep waiting
73       break;
74       }
75
76    buffer [0] = __inbyte (iobase + 5);
77    if (status == 2) status = 0;                      // check for done with no errors
78    return status;
79    }
80
81 /*-----------------------------------------------------------------------------
82  *
83  * readSmbusByte - read a single SPD byte from the default offset
84  *                 this function is faster function readSmbusByteData
85  */
86
87 static int readSmbusByte (int iobase, int address, char *buffer)
88    {
89    unsigned int status;
90    UINT64 limit;
91
92    __outbyte (iobase + 0, 0xFF);                // clear error status
93    __outbyte (iobase + 2, 0x44);                // read command
94
95    // time limit to avoid hanging for unexpected error status
96    limit = __rdtsc () + 2000000000 / 10;
97    for (;;)
98       {
99       status = __inbyte (iobase);
100       if (__rdtsc () > limit) break;
101       if ((status & 2) == 0) continue;               // SMBusInterrupt not set, keep waiting
102       if ((status & 1) == 1) continue;               // HostBusy set, keep waiting
103       break;
104       }
105
106    buffer [0] = __inbyte (iobase + 5);
107    if (status == 2) status = 0;                      // check for done with no errors
108    return status;
109    }
110
111 /*---------------------------------------------------------------------------
112  *
113  * readspd - Read one or more SPD bytes from a DIMM.
114  *           Start with offset zero and read sequentially.
115  *           Optimization relies on autoincrement to avoid 
116  *           sending offset for every byte.
117  *          Reads 128 bytes in 7-8 ms at 400 KHz.
118  */
119
120 static int readspd (int iobase, int SmbusSlaveAddress, char *buffer, int count)
121    {
122    int index, error;
123
124    /* read the first byte using offset zero */
125    error = readSmbusByteData (iobase, SmbusSlaveAddress, buffer, 0);
126    if (error) return error;
127
128    /* read the remaining bytes using auto-increment for speed */
129    for (index = 1; index < count; index++)
130       {
131       error = readSmbusByte (iobase, SmbusSlaveAddress, &buffer [index]);
132       if (error) return error;
133       }
134    
135    return 0;
136    }
137
138 static void writePmReg (int reg, int data)
139    {
140    __outbyte (0xCD6, reg);
141    __outbyte (0xCD7, data);
142    }
143
144 static void setupFch (int ioBase)
145    {
146    writePmReg (0x2D, ioBase >> 8);
147    writePmReg (0x2C, ioBase | 1);
148    writePmReg (0x29, 0x80);
149    writePmReg (0x28, 0x61);
150    __outbyte (ioBase + 0x0E, 66000000 / 400000 / 4); // set SMBus clock to 400 KHz
151    }
152
153 AGESA_STATUS AmdMemoryReadSPD (UINT32 unused1, UINT32 unused2, AGESA_READ_SPD_PARAMS *info)
154    {
155    int spdAddress, ioBase;
156
157    if (info->SocketId     >= DIMENSION (spdAddressLookup      )) return AGESA_ERROR; 
158    if (info->MemChannelId >= DIMENSION (spdAddressLookup[0]   )) return AGESA_ERROR; 
159    if (info->DimmId       >= DIMENSION (spdAddressLookup[0][0])) return AGESA_ERROR;
160    
161    spdAddress = spdAddressLookup [info->SocketId] [info->MemChannelId] [info->DimmId]; 
162    if (spdAddress == 0) return AGESA_ERROR;
163    ioBase = 0xB00;
164    setupFch (ioBase);
165    return readspd (ioBase, spdAddress, (void *) info->Buffer, 128);
166    }