blob: e3f5acc95bbdc870f837879f8fa842af2a44bc12 [file] [log] [blame]
Kyösti Mälkkia1ebbc42014-10-17 22:33:22 +03001/*
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 <device/pci_def.h>
21#include <device/device.h>
22#include <stdlib.h>
23#include "OEM.h" /* SMBUS0_BASE_ADDRESS */
24
25/* warning: Porting.h includes an open #pragma pack(1) */
26#include "Porting.h"
27#include "AGESA.h"
28#include "chip.h"
29#include "smbus_spd.h"
30
31#include <northbridge/amd/agesa/dimmSpd.h>
32
33/* uncomment for source level debug - GDB gets really confused otherwise. */
34//#pragma optimize ("", off)
35
36/**
37 * Read a single SPD byte. If the first byte is being read, set up the
38 * address and offset. Following bytes auto increment.
39 */
40static UINT8 readSmbusByte(UINT16 iobase, UINT8 address, char *buffer,
41 int offset, int initial_offset)
42{
43 unsigned int status = -1;
44 UINT64 time_limit;
45
46 /* clear status register */
47 __outbyte(iobase + SMBUS_STATUS_REG, 0x1E);
48
49 if (offset == initial_offset) {
50 /* Clear slave status, set offset, set slave address and start reading */
51 __outbyte(iobase + SMBUS_SLAVE_STATUS_REG, 0x3E);
52 __outbyte(iobase + SMBUS_CONTROL_REG, offset);
53 __outbyte(iobase + SMBUS_HOST_CMD_REG, address | READ_BIT);
54 __outbyte(iobase + SMBUS_COMMAND_REG, SMBUS_READ_BYTE_COMMAND);
55 } else {
56 /* Issue read command - auto increments to next byte */
57 __outbyte(iobase + SMBUS_COMMAND_REG, SMBUS_READ_COMMAND);
58 }
59 /* time limit to avoid hanging for unexpected error status */
60 time_limit = __rdtsc() + MAX_READ_TSC_COUNT;
61 while (__rdtsc() <= time_limit) {
62 status = __inbyte(iobase + SMBUS_STATUS_REG);
63 if ((status & SMBUS_INTERRUPT_MASK) == 0)
64 continue; /* SMBusInterrupt not set, keep waiting */
65 if ((status & HOSTBUSY_MASK) != 0)
66 continue; /* HostBusy set, keep waiting */
67 break;
68 }
69
70 if (status != STATUS__COMPLETED_SUCCESSFULLY)
71 return AGESA_ERROR;
72
73 buffer[0] = __inbyte(iobase + SMBUS_DATA0_REG);
74 return AGESA_SUCCESS;
75}
76
77static void writePmReg(UINT8 reg, UINT8 data)
78{
79 __outbyte(PMIO_INDEX_REG, reg);
80 __outbyte(PMIO_DATA_REG, data);
81}
82
83static void setupFch(UINT16 ioBase)
84{
85 /* set up SMBUS - Set to SMBUS 0 & set base address */
86 /* For SB800 & Hudson1 to SB900 & Hudson 2/3 */
87 writePmReg(SMBUS_BAR_HIGH_BYTE, ioBase >> 8);
88 writePmReg(SMBUS_BAR_LOW_BYTE, (ioBase & 0xe0) | 1);
89
90 /* set SMBus clock to 400 KHz */
91 __outbyte(ioBase + SMBUS_CLOCK_REG, SMBUS_FREQUENCY_CONST / 400000);
92}
93
94/**
95 * Read one or more SPD bytes from a DIMM.
96 * Start with offset zero and read sequentially.
97 * Reads 128 bytes in 7-8 ms at 400 KHz.
98 */
99static UINT8 readspd(UINT16 iobase, UINT8 SmbusSlaveAddress, char *buffer,
100 UINT16 count)
101{
102 UINT16 index;
103 UINT8 status;
104 UINT8 initial_offset = 0;
105
106 setupFch(iobase);
107
108 for (index = initial_offset; index < count; index++) {
109 status = readSmbusByte(iobase, SmbusSlaveAddress, &buffer[index], index,
110 initial_offset);
111 if (status != AGESA_SUCCESS)
112 return status;
113 }
114
115 return status;
116}
117
118int smbus_readSpd(int spdAddress, char *buf, size_t len)
119{
120 int ioBase = SMBUS0_BASE_ADDRESS;
121 setupFch (ioBase);
122 return readspd (ioBase, spdAddress, buf, len);
123}