remove trailing whitespace
[coreboot.git] / src / mainboard / amd / inagua / 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 #define SMBUS_BASE_ADDR         0xB00
34 static const UINT8 spdAddressLookup [1] [2] [1] =  // socket, channel, dimm
35    {
36    // socket 0
37       {
38          {0xA0},  // channel 0 dimms
39          {0xA2},  // channel 1 dimms
40       },
41    };
42
43 /*-----------------------------------------------------------------------------
44  *
45  * readSmbusByteData - read a single SPD byte from any offset
46  */
47
48 static int readSmbusByteData (int iobase, int address, char *buffer, int offset)
49    {
50    unsigned int status;
51    UINT64 limit;
52
53    address |= 1; // set read bit
54
55    __outbyte (iobase + 0, 0xFF);                // clear error status
56    __outbyte (iobase + 1, 0x1F);                // clear error status
57    __outbyte (iobase + 3, offset);              // offset in eeprom
58    __outbyte (iobase + 4, address);             // slave address and read bit
59    __outbyte (iobase + 2, 0x48);                // read byte command
60
61    // time limit to avoid hanging for unexpected error status (should never happen)
62    limit = __rdtsc () + 2000000000 / 10;
63    for (;;)
64       {
65       status = __inbyte (iobase);
66       if (__rdtsc () > limit) break;
67       if ((status & 2) == 0) continue;               // SMBusInterrupt not set, keep waiting
68       if ((status & 1) == 1) continue;               // HostBusy set, keep waiting
69       break;
70       }
71
72    buffer [0] = __inbyte (iobase + 5);
73    if (status == 2) status = 0;                      // check for done with no errors
74    return status;
75    }
76
77 /*-----------------------------------------------------------------------------
78  *
79  * readSmbusByte - read a single SPD byte from the default offset
80  *                 this function is faster function readSmbusByteData
81  */
82
83 static int readSmbusByte (int iobase, int address, char *buffer)
84    {
85    unsigned int status;
86    UINT64 limit;
87
88    __outbyte (iobase + 0, 0xFF);                // clear error status
89    __outbyte (iobase + 2, 0x44);                // read command
90
91    // time limit to avoid hanging for unexpected error status
92    limit = __rdtsc () + 2000000000 / 10;
93    for (;;)
94       {
95       status = __inbyte (iobase);
96       if (__rdtsc () > limit) break;
97       if ((status & 2) == 0) continue;               // SMBusInterrupt not set, keep waiting
98       if ((status & 1) == 1) continue;               // HostBusy set, keep waiting
99       break;
100       }
101
102    buffer [0] = __inbyte (iobase + 5);
103    if (status == 2) status = 0;                      // check for done with no errors
104    return status;
105    }
106
107 /*---------------------------------------------------------------------------
108  *
109  * readspd - Read one or more SPD bytes from a DIMM.
110  *           Start with offset zero and read sequentially.
111  *           Optimization relies on autoincrement to avoid
112  *           sending offset for every byte.
113  *          Reads 128 bytes in 7-8 ms at 400 KHz.
114  */
115
116 static int readspd (int iobase, int SmbusSlaveAddress, char *buffer, int count)
117    {
118    int index, error;
119
120    /* read the first byte using offset zero */
121    error = readSmbusByteData (iobase, SmbusSlaveAddress, buffer, 0);
122    if (error) return error;
123
124    /* read the remaining bytes using auto-increment for speed */
125    for (index = 1; index < count; index++)
126       {
127       error = readSmbusByte (iobase, SmbusSlaveAddress, &buffer [index]);
128       if (error) return error;
129       }
130
131    return 0;
132    }
133
134 static void writePmReg (int reg, int data)
135    {
136    __outbyte (0xCD6, reg);
137    __outbyte (0xCD7, data);
138    }
139
140 static void setupFch (int ioBase)
141    {
142    writePmReg (0x2D, ioBase >> 8);
143    writePmReg (0x2C, ioBase | 1);
144    writePmReg (0x29, 0x80);
145    writePmReg (0x28, 0x61);
146    __outbyte (ioBase + 0x0E, 66000000 / 400000 / 4); // set SMBus clock to 400 KHz
147    }
148
149 AGESA_STATUS AmdMemoryReadSPD (UINT32 unused1, UINT32 unused2, AGESA_READ_SPD_PARAMS *info)
150    {
151    int spdAddress, ioBase;
152
153    if (info->SocketId     >= DIMENSION (spdAddressLookup      )) return AGESA_ERROR;
154    if (info->MemChannelId >= DIMENSION (spdAddressLookup[0]   )) return AGESA_ERROR;
155    if (info->DimmId       >= DIMENSION (spdAddressLookup[0][0])) return AGESA_ERROR;
156
157    spdAddress = spdAddressLookup [info->SocketId] [info->MemChannelId] [info->DimmId];
158    if (spdAddress == 0) return AGESA_ERROR;
159    ioBase = SMBUS_BASE_ADDR;
160    setupFch (ioBase);
161    return readspd (ioBase, spdAddress, (void *) info->Buffer, 128);
162    }