2 * This file is part of the coreboot project.
4 * Copyright (C) 2011 Advanced Micro Devices, Inc.
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.
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.
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
24 AGESA_STATUS AmdMemoryReadSPD (UINT32 unused1, UINT32 unused2, AGESA_READ_SPD_PARAMS *info);
25 #define DIMENSION(array)(sizeof (array)/ sizeof (array [0]))
27 /*#pragma optimize ("", off) // for source level debug
28 *---------------------------------------------------------------------------
30 * SPD address table - porting required
33 static const UINT8 spdAddressLookup [2] [2] [4] = // socket, channel, dimm
37 {0xA0, 0xA2}, // channel 0 dimms
38 {0x00, 0x00}, // channel 1 dimms
42 {0x00, 0x00}, // channel 0 dimms
43 {0x00, 0x00}, // channel 1 dimms
47 /*-----------------------------------------------------------------------------
49 * readSmbusByteData - read a single SPD byte from any offset
52 static int readSmbusByteData (int iobase, int address, char *buffer, int offset)
57 address |= 1; // set read bit
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
65 // time limit to avoid hanging for unexpected error status (should never happen)
66 limit = __rdtsc () + 2000000000 / 10;
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
76 buffer [0] = __inbyte (iobase + 5);
77 if (status == 2) status = 0; // check for done with no errors
81 /*-----------------------------------------------------------------------------
83 * readSmbusByte - read a single SPD byte from the default offset
84 * this function is faster function readSmbusByteData
87 static int readSmbusByte (int iobase, int address, char *buffer)
92 __outbyte (iobase + 0, 0xFF); // clear error status
93 __outbyte (iobase + 2, 0x44); // read command
95 // time limit to avoid hanging for unexpected error status
96 limit = __rdtsc () + 2000000000 / 10;
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
106 buffer [0] = __inbyte (iobase + 5);
107 if (status == 2) status = 0; // check for done with no errors
111 /*---------------------------------------------------------------------------
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.
120 static int readspd (int iobase, int SmbusSlaveAddress, char *buffer, int count)
124 /* read the first byte using offset zero */
125 error = readSmbusByteData (iobase, SmbusSlaveAddress, buffer, 0);
126 if (error) return error;
128 /* read the remaining bytes using auto-increment for speed */
129 for (index = 1; index < count; index++)
131 error = readSmbusByte (iobase, SmbusSlaveAddress, &buffer [index]);
132 if (error) return error;
138 static void writePmReg (int reg, int data)
140 __outbyte (0xCD6, reg);
141 __outbyte (0xCD7, data);
144 static void setupFch (int ioBase)
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
153 AGESA_STATUS AmdMemoryReadSPD (UINT32 unused1, UINT32 unused2, AGESA_READ_SPD_PARAMS *info)
155 int spdAddress, ioBase;
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;
161 spdAddress = spdAddressLookup [info->SocketId] [info->MemChannelId] [info->DimmId];
162 if (spdAddress == 0) return AGESA_ERROR;
165 return readspd (ioBase, spdAddress, (void *) info->Buffer, 128);