coreboot
coreboot is an Open Source project aimed at replacing the proprietary BIOS found in most computers.
spd.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 
3 #include <cbfs.h>
4 #include <console/console.h>
5 #include <gpio.h>
6 #include <soc/gpio.h>
7 #include <soc/romstage.h>
8 #include <string.h>
9 
10 #include "../gpio.h"
11 #include "spd.h"
12 
14 {
15  const int spd_banks[8] = { 8, 16, 32, 64, -1, -1, -1, -1 };
16  const int spd_capmb[8] = { 1, 2, 4, 8, 16, 32, 64, 0 };
17  const int spd_rows[8] = { 12, 13, 14, 15, 16, -1, -1, -1 };
18  const int spd_cols[8] = { 9, 10, 11, 12, -1, -1, -1, -1 };
19  const int spd_ranks[8] = { 1, 2, 3, 4, -1, -1, -1, -1 };
20  const int spd_devw[8] = { 4, 8, 16, 32, -1, -1, -1, -1 };
21  const int spd_busw[8] = { 8, 16, 32, 64, -1, -1, -1, -1 };
22  char spd_name[SPD_PART_LEN+1] = { 0 };
23 
24  int banks = spd_banks[(spd[SPD_DENSITY_BANKS] >> 4) & 7];
25  int capmb = spd_capmb[spd[SPD_DENSITY_BANKS] & 7] * 256;
26  int rows = spd_rows[(spd[SPD_ADDRESSING] >> 3) & 7];
27  int cols = spd_cols[spd[SPD_ADDRESSING] & 7];
28  int ranks = spd_ranks[(spd[SPD_ORGANIZATION] >> 3) & 7];
29  int devw = spd_devw[spd[SPD_ORGANIZATION] & 7];
30  int busw = spd_busw[spd[SPD_BUS_DEV_WIDTH] & 7];
31 
32  /* Module type */
33  printk(BIOS_INFO, "SPD: module type is ");
34  switch (spd[SPD_DRAM_TYPE]) {
35  case SPD_DRAM_DDR3:
36  printk(BIOS_INFO, "DDR3\n");
37  break;
38  case SPD_DRAM_LPDDR3:
39  printk(BIOS_INFO, "LPDDR3\n");
40  break;
41  default:
42  printk(BIOS_INFO, "Unknown (%02x)\n", spd[SPD_DRAM_TYPE]);
43  break;
44  }
45 
46  /* Module Part Number */
47  memcpy(spd_name, &spd[SPD_PART_OFF], SPD_PART_LEN);
48  spd_name[SPD_PART_LEN] = 0;
49  printk(BIOS_INFO, "SPD: module part is %s\n", spd_name);
50 
52  "SPD: banks %d, ranks %d, rows %d, columns %d, density %d Mb\n",
53  banks, ranks, rows, cols, capmb);
54  printk(BIOS_INFO, "SPD: device width %d bits, bus width %d bits\n",
55  devw, busw);
56 
57  if (capmb > 0 && busw > 0 && devw > 0 && ranks > 0) {
58  /* SIZE = DENSITY / 8 * BUS_WIDTH / SDRAM_WIDTH * RANKS */
59  printk(BIOS_INFO, "SPD: module size is %u MB (per channel)\n",
60  capmb / 8 * busw / devw * ranks);
61  }
62 }
63 
65 {
66  gpio_t spd_gpios[] = {
71  };
72  return gpio_base2_value(spd_gpios, ARRAY_SIZE(spd_gpios));
73 }
74 
76 {
77  char *spd_file;
78  size_t spd_file_len;
79  int spd_index;
80 
82  printk(BIOS_INFO, "SPD index %d\n", spd_index);
83 
84  /* Load SPD data from CBFS */
85  spd_file = cbfs_map("spd.bin", &spd_file_len);
86  if (!spd_file)
87  die("SPD data not found.");
88 
89  /* make sure we have at least one SPD in the file. */
90  if (spd_file_len < SPD_LEN)
91  die("Missing SPD data.");
92 
93  /* Make sure we did not overrun the buffer */
94  if (spd_file_len < ((spd_index + 1) * SPD_LEN)) {
95  printk(BIOS_ERR, "SPD index override to 1 - old hardware?\n");
96  spd_index = 1;
97  }
98 
99  spd_index *= SPD_LEN;
100  mainboard_print_spd_info((uint8_t *)(spd_file + spd_index));
101 
102  return (uintptr_t)(spd_file + spd_index);
103 }
void * memcpy(void *dest, const void *src, size_t n)
Definition: memcpy.c:7
#define ARRAY_SIZE(a)
Definition: helpers.h:12
static void * cbfs_map(const char *name, size_t *size_out)
Definition: cbfs.h:246
#define printk(level,...)
Definition: stdlib.h:16
void __noreturn die(const char *fmt,...)
Definition: die.c:17
uint32_t gpio_base2_value(const gpio_t gpio[], int num_gpio)
Definition: gpio.c:30
#define BIOS_INFO
BIOS_INFO - Expected events.
Definition: loglevel.h:113
#define BIOS_ERR
BIOS_ERR - System in incomplete state.
Definition: loglevel.h:72
#define SPD_LEN
Definition: spd.c:22
#define SPD_DENSITY_BANKS
Definition: spd.c:15
#define SPD_PART_LEN
Definition: spd.c:20
#define SPD_DRAM_LPDDR3
Definition: spd.c:14
#define SPD_ADDRESSING
Definition: spd.c:16
#define SPD_ORGANIZATION
Definition: spd.c:17
#define SPD_BUS_DEV_WIDTH
Definition: spd.c:18
#define SPD_DRAM_TYPE
Definition: spd.c:12
#define SPD_PART_OFF
Definition: spd.c:19
#define SPD_DRAM_DDR3
Definition: spd.c:13
#define GPIO_MEM_CONFIG_3
Definition: gpio.h:27
#define GPIO_MEM_CONFIG_0
Definition: gpio.h:24
#define GPIO_MEM_CONFIG_2
Definition: gpio.h:26
#define GPIO_MEM_CONFIG_1
Definition: gpio.h:25
static const int spd_index[32]
Definition: memory.c:10
uintptr_t mainboard_get_spd_data(void)
Definition: spd.c:75
int mainboard_get_spd_index(void)
Definition: spd.c:64
static void mainboard_print_spd_info(uint8_t spd[])
Definition: spd.c:13
unsigned long uintptr_t
Definition: stdint.h:21
unsigned char uint8_t
Definition: stdint.h:8