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