Angel Pons | 0612b27 | 2020-04-05 15:46:56 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Aamir Bohra | 52f2974 | 2017-04-19 18:19:14 +0530 | [diff] [blame] | 2 | |
Kyösti Mälkki | 7566467 | 2020-01-01 17:42:45 +0200 | [diff] [blame] | 3 | #include <console/console.h> |
Kyösti Mälkki | 7566467 | 2020-01-01 17:42:45 +0200 | [diff] [blame] | 4 | #include <spd_bin.h> |
Wim Vervoorn | 30e9149 | 2020-05-01 13:50:08 +0200 | [diff] [blame] | 5 | #include <device/smbus_def.h> |
Kyösti Mälkki | 1cae454 | 2020-01-06 12:31:34 +0200 | [diff] [blame] | 6 | #include <device/smbus_host.h> |
Aamir Bohra | 52f2974 | 2017-04-19 18:19:14 +0530 | [diff] [blame] | 7 | #include "smbuslib.h" |
| 8 | |
Kyösti Mälkki | 7566467 | 2020-01-01 17:42:45 +0200 | [diff] [blame] | 9 | static void update_spd_len(struct spd_block *blk) |
| 10 | { |
| 11 | u8 i, j = 0; |
| 12 | for (i = 0 ; i < CONFIG_DIMM_MAX; i++) |
| 13 | if (blk->spd_array[i] != NULL) |
| 14 | j |= blk->spd_array[i][SPD_DRAM_TYPE]; |
| 15 | |
| 16 | /* If spd used is DDR4, then its length is 512 byte. */ |
| 17 | if (j == SPD_DRAM_DDR4) |
| 18 | blk->len = SPD_PAGE_LEN_DDR4; |
| 19 | else |
| 20 | blk->len = SPD_PAGE_LEN; |
| 21 | } |
| 22 | |
| 23 | static void smbus_read_spd(u8 *spd, u8 addr) |
| 24 | { |
| 25 | u16 i; |
| 26 | u8 step = 1; |
| 27 | |
| 28 | if (CONFIG(SPD_READ_BY_WORD)) |
| 29 | step = sizeof(uint16_t); |
| 30 | |
| 31 | for (i = 0; i < SPD_PAGE_LEN; i += step) { |
| 32 | if (CONFIG(SPD_READ_BY_WORD)) |
| 33 | ((u16*)spd)[i / sizeof(uint16_t)] = |
Kyösti Mälkki | 1a1b04e | 2020-01-07 22:34:33 +0200 | [diff] [blame] | 34 | smbus_read_word(addr, i); |
Kyösti Mälkki | 7566467 | 2020-01-01 17:42:45 +0200 | [diff] [blame] | 35 | else |
Kyösti Mälkki | 1a1b04e | 2020-01-07 22:34:33 +0200 | [diff] [blame] | 36 | spd[i] = smbus_read_byte(addr, i); |
Kyösti Mälkki | 7566467 | 2020-01-01 17:42:45 +0200 | [diff] [blame] | 37 | } |
| 38 | } |
| 39 | |
Eric Lai | 0ee9b14c | 2020-04-21 15:32:20 +0800 | [diff] [blame] | 40 | /* return -1 if SMBus errors otherwise return 0 */ |
| 41 | static int get_spd(u8 *spd, u8 addr) |
Kyösti Mälkki | 7566467 | 2020-01-01 17:42:45 +0200 | [diff] [blame] | 42 | { |
Eric Lai | 0ee9b14c | 2020-04-21 15:32:20 +0800 | [diff] [blame] | 43 | /* If address is not 0, it will return CB_ERR(-1) if no dimm */ |
Kyösti Mälkki | 1a1b04e | 2020-01-07 22:34:33 +0200 | [diff] [blame] | 44 | if (smbus_read_byte(addr, 0) < 0) { |
Kyösti Mälkki | 7566467 | 2020-01-01 17:42:45 +0200 | [diff] [blame] | 45 | printk(BIOS_INFO, "No memory dimm at address %02X\n", |
| 46 | addr << 1); |
Eric Lai | 0ee9b14c | 2020-04-21 15:32:20 +0800 | [diff] [blame] | 47 | return -1; |
Kyösti Mälkki | 7566467 | 2020-01-01 17:42:45 +0200 | [diff] [blame] | 48 | } |
Wim Vervoorn | 30e9149 | 2020-05-01 13:50:08 +0200 | [diff] [blame] | 49 | |
Kyösti Mälkki | 1a1b04e | 2020-01-07 22:34:33 +0200 | [diff] [blame] | 50 | if (i2c_eeprom_read(addr, 0, SPD_PAGE_LEN, spd) < 0) { |
Wim Vervoorn | 30e9149 | 2020-05-01 13:50:08 +0200 | [diff] [blame] | 51 | printk(BIOS_INFO, "do_i2c_eeprom_read failed, using fallback\n"); |
| 52 | smbus_read_spd(spd, addr); |
| 53 | } |
Kyösti Mälkki | 7566467 | 2020-01-01 17:42:45 +0200 | [diff] [blame] | 54 | |
| 55 | /* Check if module is DDR4, DDR4 spd is 512 byte. */ |
Kyösti Mälkki | 1a1b04e | 2020-01-07 22:34:33 +0200 | [diff] [blame] | 56 | if (spd[SPD_DRAM_TYPE] == SPD_DRAM_DDR4 && CONFIG_DIMM_SPD_SIZE > SPD_PAGE_LEN) { |
Kyösti Mälkki | 7566467 | 2020-01-01 17:42:45 +0200 | [diff] [blame] | 57 | /* Switch to page 1 */ |
Kyösti Mälkki | 1a1b04e | 2020-01-07 22:34:33 +0200 | [diff] [blame] | 58 | smbus_write_byte(SPD_PAGE_1, 0, 0); |
Wim Vervoorn | 30e9149 | 2020-05-01 13:50:08 +0200 | [diff] [blame] | 59 | |
Kyösti Mälkki | 1a1b04e | 2020-01-07 22:34:33 +0200 | [diff] [blame] | 60 | if (i2c_eeprom_read(addr, 0, SPD_PAGE_LEN, spd + SPD_PAGE_LEN) < 0) { |
Wim Vervoorn | 30e9149 | 2020-05-01 13:50:08 +0200 | [diff] [blame] | 61 | printk(BIOS_INFO, "do_i2c_eeprom_read failed, using fallback\n"); |
| 62 | smbus_read_spd(spd + SPD_PAGE_LEN, addr); |
| 63 | } |
Kyösti Mälkki | 7566467 | 2020-01-01 17:42:45 +0200 | [diff] [blame] | 64 | /* Restore to page 0 */ |
Kyösti Mälkki | 1a1b04e | 2020-01-07 22:34:33 +0200 | [diff] [blame] | 65 | smbus_write_byte(SPD_PAGE_0, 0, 0); |
Kyösti Mälkki | 7566467 | 2020-01-01 17:42:45 +0200 | [diff] [blame] | 66 | } |
Eric Lai | 0ee9b14c | 2020-04-21 15:32:20 +0800 | [diff] [blame] | 67 | return 0; |
Kyösti Mälkki | 7566467 | 2020-01-01 17:42:45 +0200 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | static u8 spd_data[CONFIG_DIMM_MAX * CONFIG_DIMM_SPD_SIZE]; |
| 71 | |
| 72 | void get_spd_smbus(struct spd_block *blk) |
| 73 | { |
| 74 | u8 i; |
| 75 | for (i = 0 ; i < CONFIG_DIMM_MAX; i++) { |
Eric Lai | 0ee9b14c | 2020-04-21 15:32:20 +0800 | [diff] [blame] | 76 | if (blk->addr_map[i] == 0) { |
| 77 | blk->spd_array[i] = NULL; |
| 78 | continue; |
| 79 | } |
| 80 | |
| 81 | if (get_spd(&spd_data[i * CONFIG_DIMM_SPD_SIZE], blk->addr_map[i]) == 0) |
| 82 | blk->spd_array[i] = &spd_data[i * CONFIG_DIMM_SPD_SIZE]; |
| 83 | else |
| 84 | blk->spd_array[i] = NULL; |
Kyösti Mälkki | 7566467 | 2020-01-01 17:42:45 +0200 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | update_spd_len(blk); |
| 88 | } |
Jamie Chen | 7adcfde | 2020-04-16 01:20:29 +0800 | [diff] [blame] | 89 | |
| 90 | /* |
| 91 | * get_spd_sn returns the SODIMM serial number. It only supports DDR3 and DDR4. |
| 92 | * return CB_SUCCESS, sn is the serial number and sn=0xffffffff if the dimm is not present. |
| 93 | * return CB_ERR, if dram_type is not supported or addr is a zero. |
| 94 | */ |
| 95 | enum cb_err get_spd_sn(u8 addr, u32 *sn) |
| 96 | { |
| 97 | u8 i; |
| 98 | u8 dram_type; |
| 99 | int smbus_ret; |
| 100 | |
| 101 | /* addr is not a zero. */ |
| 102 | if (addr == 0x0) |
| 103 | return CB_ERR; |
| 104 | |
| 105 | /* If dimm is not present, set sn to 0xff. */ |
Kyösti Mälkki | 1a1b04e | 2020-01-07 22:34:33 +0200 | [diff] [blame] | 106 | smbus_ret = smbus_read_byte(addr, SPD_DRAM_TYPE); |
Jamie Chen | 7adcfde | 2020-04-16 01:20:29 +0800 | [diff] [blame] | 107 | if (smbus_ret < 0) { |
Kane Chen | be9cef9 | 2022-05-12 16:58:38 +0800 | [diff] [blame] | 108 | printk(BIOS_INFO, "No memory dimm at address %02X\n", addr << 1); |
Jamie Chen | 7adcfde | 2020-04-16 01:20:29 +0800 | [diff] [blame] | 109 | *sn = 0xffffffff; |
| 110 | return CB_SUCCESS; |
| 111 | } |
| 112 | |
| 113 | dram_type = smbus_ret & 0xff; |
| 114 | |
| 115 | /* Check if module is DDR4, DDR4 spd is 512 byte. */ |
| 116 | if (dram_type == SPD_DRAM_DDR4 && CONFIG_DIMM_SPD_SIZE > SPD_PAGE_LEN) { |
| 117 | /* Switch to page 1 */ |
Kyösti Mälkki | 1a1b04e | 2020-01-07 22:34:33 +0200 | [diff] [blame] | 118 | smbus_write_byte(SPD_PAGE_1, 0, 0); |
Jamie Chen | 7adcfde | 2020-04-16 01:20:29 +0800 | [diff] [blame] | 119 | |
| 120 | for (i = 0; i < SPD_SN_LEN; i++) |
Kyösti Mälkki | 1a1b04e | 2020-01-07 22:34:33 +0200 | [diff] [blame] | 121 | *((u8 *)sn + i) = smbus_read_byte(addr, |
Jamie Chen | 7adcfde | 2020-04-16 01:20:29 +0800 | [diff] [blame] | 122 | i + DDR4_SPD_SN_OFF); |
| 123 | |
| 124 | /* Restore to page 0 */ |
Kyösti Mälkki | 1a1b04e | 2020-01-07 22:34:33 +0200 | [diff] [blame] | 125 | smbus_write_byte(SPD_PAGE_0, 0, 0); |
Jamie Chen | 7adcfde | 2020-04-16 01:20:29 +0800 | [diff] [blame] | 126 | } else if (dram_type == SPD_DRAM_DDR3) { |
| 127 | for (i = 0; i < SPD_SN_LEN; i++) |
Kyösti Mälkki | 1a1b04e | 2020-01-07 22:34:33 +0200 | [diff] [blame] | 128 | *((u8 *)sn + i) = smbus_read_byte(addr, |
Jamie Chen | 7adcfde | 2020-04-16 01:20:29 +0800 | [diff] [blame] | 129 | i + DDR3_SPD_SN_OFF); |
| 130 | } else { |
| 131 | printk(BIOS_ERR, "Unsupported dram_type\n"); |
| 132 | return CB_ERR; |
| 133 | } |
| 134 | |
| 135 | return CB_SUCCESS; |
| 136 | } |