Angel Pons | 182dbde | 2020-04-02 23:49:05 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Kyösti Mälkki | c5cc9f2 | 2014-10-17 22:33:22 +0300 | [diff] [blame] | 2 | |
Michał Żygowski | 287ce5f | 2019-12-01 17:41:23 +0100 | [diff] [blame] | 3 | #include <amdblocks/acpimmio.h> |
Kyösti Mälkki | c5cc9f2 | 2014-10-17 22:33:22 +0300 | [diff] [blame] | 4 | #include <device/pci_def.h> |
| 5 | #include <device/device.h> |
Elyes HAOUAS | 20eaef0 | 2019-03-29 17:45:28 +0100 | [diff] [blame] | 6 | #include <console/console.h> |
Elyes HAOUAS | 24230f6 | 2020-07-27 07:56:14 +0200 | [diff] [blame] | 7 | #include <stddef.h> |
Kyösti Mälkki | c5cc9f2 | 2014-10-17 22:33:22 +0300 | [diff] [blame] | 8 | /* warning: Porting.h includes an open #pragma pack(1) */ |
Elyes HAOUAS | 19f5ba8 | 2018-10-14 14:52:06 +0200 | [diff] [blame] | 9 | #include <Porting.h> |
| 10 | #include <AGESA.h> |
| 11 | #include <amdlib.h> |
Kyösti Mälkki | c5cc9f2 | 2014-10-17 22:33:22 +0300 | [diff] [blame] | 12 | #include <northbridge/amd/agesa/dimmSpd.h> |
| 13 | |
| 14 | /*----------------------------------------------------------------------------- |
| 15 | * |
| 16 | * readSmbusByteData - read a single SPD byte from any offset |
| 17 | */ |
| 18 | |
Elyes Haouas | 558d731 | 2022-07-16 09:45:39 +0200 | [diff] [blame] | 19 | static int readSmbusByteData(int iobase, int address, char *buffer, int offset) |
Kyösti Mälkki | c5cc9f2 | 2014-10-17 22:33:22 +0300 | [diff] [blame] | 20 | { |
| 21 | unsigned int status; |
| 22 | UINT64 limit; |
| 23 | |
| 24 | address |= 1; // set read bit |
| 25 | |
Elyes Haouas | 558d731 | 2022-07-16 09:45:39 +0200 | [diff] [blame] | 26 | __outbyte(iobase + 0, 0xFF); // clear error status |
| 27 | __outbyte(iobase + 1, 0x1F); // clear error status |
| 28 | __outbyte(iobase + 3, offset); // offset in eeprom |
| 29 | __outbyte(iobase + 4, address); // slave address and read bit |
| 30 | __outbyte(iobase + 2, 0x48); // read byte command |
Kyösti Mälkki | c5cc9f2 | 2014-10-17 22:33:22 +0300 | [diff] [blame] | 31 | |
| 32 | // time limit to avoid hanging for unexpected error status (should never happen) |
Elyes Haouas | 558d731 | 2022-07-16 09:45:39 +0200 | [diff] [blame] | 33 | limit = __rdtsc() + 2000000000 / 10; |
Kyösti Mälkki | c5cc9f2 | 2014-10-17 22:33:22 +0300 | [diff] [blame] | 34 | for (;;) |
| 35 | { |
Elyes Haouas | 558d731 | 2022-07-16 09:45:39 +0200 | [diff] [blame] | 36 | status = __inbyte(iobase); |
| 37 | if (__rdtsc() > limit) break; |
Kyösti Mälkki | c5cc9f2 | 2014-10-17 22:33:22 +0300 | [diff] [blame] | 38 | if ((status & 2) == 0) continue; // SMBusInterrupt not set, keep waiting |
| 39 | if ((status & 1) == 1) continue; // HostBusy set, keep waiting |
| 40 | break; |
| 41 | } |
| 42 | |
Elyes Haouas | 558d731 | 2022-07-16 09:45:39 +0200 | [diff] [blame] | 43 | buffer [0] = __inbyte(iobase + 5); |
Kyösti Mälkki | c5cc9f2 | 2014-10-17 22:33:22 +0300 | [diff] [blame] | 44 | if (status == 2) status = 0; // check for done with no errors |
| 45 | return status; |
| 46 | } |
| 47 | |
| 48 | /*----------------------------------------------------------------------------- |
| 49 | * |
| 50 | * readSmbusByte - read a single SPD byte from the default offset |
| 51 | * this function is faster function readSmbusByteData |
| 52 | */ |
| 53 | |
Elyes Haouas | 558d731 | 2022-07-16 09:45:39 +0200 | [diff] [blame] | 54 | static int readSmbusByte(int iobase, int address, char *buffer) |
Kyösti Mälkki | c5cc9f2 | 2014-10-17 22:33:22 +0300 | [diff] [blame] | 55 | { |
| 56 | unsigned int status; |
| 57 | UINT64 limit; |
| 58 | |
Elyes Haouas | 558d731 | 2022-07-16 09:45:39 +0200 | [diff] [blame] | 59 | __outbyte(iobase + 0, 0xFF); // clear error status |
| 60 | __outbyte(iobase + 2, 0x44); // read command |
Kyösti Mälkki | c5cc9f2 | 2014-10-17 22:33:22 +0300 | [diff] [blame] | 61 | |
| 62 | // time limit to avoid hanging for unexpected error status |
Elyes Haouas | 558d731 | 2022-07-16 09:45:39 +0200 | [diff] [blame] | 63 | limit = __rdtsc() + 2000000000 / 10; |
Kyösti Mälkki | c5cc9f2 | 2014-10-17 22:33:22 +0300 | [diff] [blame] | 64 | for (;;) |
| 65 | { |
Elyes Haouas | 558d731 | 2022-07-16 09:45:39 +0200 | [diff] [blame] | 66 | status = __inbyte(iobase); |
| 67 | if (__rdtsc() > limit) break; |
Kyösti Mälkki | c5cc9f2 | 2014-10-17 22:33:22 +0300 | [diff] [blame] | 68 | if ((status & 2) == 0) continue; // SMBusInterrupt not set, keep waiting |
| 69 | if ((status & 1) == 1) continue; // HostBusy set, keep waiting |
| 70 | break; |
| 71 | } |
| 72 | |
Elyes Haouas | 558d731 | 2022-07-16 09:45:39 +0200 | [diff] [blame] | 73 | buffer [0] = __inbyte(iobase + 5); |
Kyösti Mälkki | c5cc9f2 | 2014-10-17 22:33:22 +0300 | [diff] [blame] | 74 | if (status == 2) status = 0; // check for done with no errors |
| 75 | return status; |
| 76 | } |
| 77 | |
| 78 | /*--------------------------------------------------------------------------- |
| 79 | * |
| 80 | * readspd - Read one or more SPD bytes from a DIMM. |
| 81 | * Start with offset zero and read sequentially. |
| 82 | * Optimization relies on autoincrement to avoid |
| 83 | * sending offset for every byte. |
| 84 | * Reads 128 bytes in 7-8 ms at 400 KHz. |
| 85 | */ |
| 86 | |
Elyes Haouas | 558d731 | 2022-07-16 09:45:39 +0200 | [diff] [blame] | 87 | static int readspd(int iobase, int SmbusSlaveAddress, char *buffer, int count) |
Kyösti Mälkki | c5cc9f2 | 2014-10-17 22:33:22 +0300 | [diff] [blame] | 88 | { |
| 89 | int index, error; |
| 90 | |
| 91 | printk(BIOS_SPEW, "-------------READING SPD-----------\n"); |
| 92 | printk(BIOS_SPEW, "iobase: 0x%08X, SmbusSlave: 0x%08X, count: %d\n", |
| 93 | iobase, SmbusSlaveAddress, count); |
| 94 | |
| 95 | /* read the first byte using offset zero */ |
Elyes Haouas | 558d731 | 2022-07-16 09:45:39 +0200 | [diff] [blame] | 96 | error = readSmbusByteData(iobase, SmbusSlaveAddress, buffer, 0); |
Kyösti Mälkki | c5cc9f2 | 2014-10-17 22:33:22 +0300 | [diff] [blame] | 97 | |
| 98 | if (error) { |
| 99 | printk(BIOS_ERR, "-------------SPD READ ERROR-----------\n"); |
| 100 | return error; |
| 101 | } |
| 102 | |
| 103 | /* read the remaining bytes using auto-increment for speed */ |
| 104 | for (index = 1; index < count; index++) |
| 105 | { |
Elyes Haouas | 558d731 | 2022-07-16 09:45:39 +0200 | [diff] [blame] | 106 | error = readSmbusByte(iobase, SmbusSlaveAddress, &buffer [index]); |
Kyösti Mälkki | c5cc9f2 | 2014-10-17 22:33:22 +0300 | [diff] [blame] | 107 | if (error) { |
| 108 | printk(BIOS_ERR, "-------------SPD READ ERROR-----------\n"); |
| 109 | return error; |
| 110 | } |
| 111 | } |
| 112 | printk(BIOS_SPEW, "\n"); |
| 113 | printk(BIOS_SPEW, "-------------FINISHED READING SPD-----------\n"); |
| 114 | |
| 115 | return 0; |
| 116 | } |
| 117 | |
Elyes Haouas | 558d731 | 2022-07-16 09:45:39 +0200 | [diff] [blame] | 118 | static void setupFch(int ioBase) |
Kyösti Mälkki | c5cc9f2 | 2014-10-17 22:33:22 +0300 | [diff] [blame] | 119 | { |
Michał Żygowski | 287ce5f | 2019-12-01 17:41:23 +0100 | [diff] [blame] | 120 | pm_write16(0x2c, ioBase | 1); |
Elyes Haouas | 558d731 | 2022-07-16 09:45:39 +0200 | [diff] [blame] | 121 | __outbyte(ioBase + 0x0E, 66000000 / 400000 / 4); // set SMBus clock to 400 KHz |
Kyösti Mälkki | c5cc9f2 | 2014-10-17 22:33:22 +0300 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | int hudson_readSpd(int spdAddress, char *buf, size_t len) |
| 125 | { |
| 126 | int ioBase = 0xB00; |
Elyes Haouas | 558d731 | 2022-07-16 09:45:39 +0200 | [diff] [blame] | 127 | setupFch(ioBase); |
| 128 | return readspd(ioBase, spdAddress, buf, len); |
Kyösti Mälkki | c5cc9f2 | 2014-10-17 22:33:22 +0300 | [diff] [blame] | 129 | } |