Debian Codegraph @

src/soc/amd/common/block/spi/fch_spi.c

raw symbols SCIP
1 /* SPDX-License-Identifier: GPL-2.0-only */
2
3 #include <amdblocks/chip.h>
4 #include <amdblocks/lpc.h>
5 #include <amdblocks/psp_efs.h>
6 #include <amdblocks/spi.h>
7 #include <console/console.h>
8 #include <device/mmio.h>
9 #include <soc/iomap.h>
10 #include <soc/lpc.h>
11 #include <stdint.h>
12
13 static const char *spi_speed_str[8] = {
14 "66.66 Mhz",
15 "33.33 MHz",
16 "22.22 MHz",
17 "16.66 MHz",
18 "100 MHz",
19 "800 KHz",
20 "Invalid",
21 "Invalid"
22 };
23
24 static const char *read_mode_str[8] = {
25 "Normal Read (up to 33M)",
26 "Reserved",
27 "Dual IO (1-1-2)",
28 "Quad IO (1-1-4)",
29 "Dual IO (1-2-2)",
30 "Quad IO (1-4-4)",
31 "Normal Read (up to 66M)",
32 "Fast Read"
33 };
34
35 static const char *remapping[8] = {
36 "0-1-2-3",
37 "1-0-3-2",
38 "2-3-0-1",
39 "3-2-1-0",
40 };
41
42 void show_spi_speeds_and_modes(void)
43 {
44 uint16_t val16 = spi_read16(SPI100_SPEED_CONFIG);
45 uint32_t val32 = spi_read32(SPI_CNTRL0);
46 uint8_t val8 = fch_spi_rom_remapping();
47
48 printk(BIOS_DEBUG, "SPI normal read speed: %s\n",
49 spi_speed_str[DECODE_SPI_NORMAL_SPEED(val16)]);
50 printk(BIOS_DEBUG, "SPI fast read speed: %s\n",
51 spi_speed_str[DECODE_SPI_FAST_SPEED(val16)]);
52 printk(BIOS_DEBUG, "SPI alt read speed: %s\n",
53 spi_speed_str[DECODE_SPI_ALT_SPEED(val16)]);
54 printk(BIOS_DEBUG, "SPI TPM read speed: %s\n",
55 spi_speed_str[DECODE_SPI_TPM_SPEED(val16)]);
56 printk(BIOS_DEBUG, "SPI100: %s\n",
57 spi_read16(SPI100_ENABLE) & SPI_USE_SPI100 ? "Enabled" : "Disabled");
58 printk(BIOS_DEBUG, "SPI Read Mode: %s\n", read_mode_str[DECODE_SPI_READ_MODE(val32)]);
59 printk(BIOS_DEBUG, "SPI ROM mapping: %s\n", remapping[val8]);
60 }
61
62 void __weak mainboard_spi_cfg_override(uint8_t *fast_speed, uint8_t *read_mode)
63 {
64 /* No overriding SPI speeds. */
65 }
66
67 static uint8_t lower_speed(uint8_t speed1, uint8_t speed2)
68 {
69 uint8_t speeds[] = {SPI_SPEED_800K, SPI_SPEED_16M, SPI_SPEED_22M,
70 SPI_SPEED_33M, SPI_SPEED_66M, SPI_SPEED_100M};
71
72 for (int i = 0; i < ARRAY_SIZE(speeds); i++) {
73 if (speed1 == speeds[i])
74 return speed1;
75 if (speed2 == speeds[i])
76 return speed2;
77 }
78
79 /* Fall back to 16MHz if we got invalid speed values */
80 return SPI_SPEED_16M;
81 }
82
83 static void fch_spi_set_spi100(uint8_t norm, uint8_t fast, uint8_t alt, uint8_t tpm)
84 {
85 spi_write16(SPI100_SPEED_CONFIG, SPI_SPEED_CFG(norm, fast, alt, tpm));
86 spi_write16(SPI100_ENABLE, SPI_USE_SPI100 | spi_read16(SPI100_ENABLE));
87 }
88
89 static void fch_spi_configure_4dw_burst(void)
90 {
91 uint16_t val = spi_read16(SPI100_HOST_PREF_CONFIG);
92
93 if (CONFIG(SOC_AMD_COMMON_BLOCK_SPI_4DW_BURST))
94 val |= SPI_RD4DW_EN_HOST;
95 else
96 val &= ~SPI_RD4DW_EN_HOST;
97
98 spi_write16(SPI100_HOST_PREF_CONFIG, val);
99 }
100
101 static void fch_spi_set_read_mode(u32 mode)
102 {
103 uint32_t val = spi_read32(SPI_CNTRL0) & ~SPI_READ_MODE_MASK;
104
105 spi_write32(SPI_CNTRL0, val | SPI_READ_MODE(mode));
106 }
107
108 uint8_t fch_spi_rom_remapping(void)
109 {
110 return spi_read8(SPI_ROM_PAGE) & SPI_ROM_PAGE_SEL;
111 }
112
113 void fch_spi_config_modes(void)
114 {
115 uint8_t read_mode, fast_speed;
116 uint8_t normal_speed = CONFIG_NORMAL_READ_SPI_SPEED;
117 uint8_t alt_speed = CONFIG_ALT_SPI_SPEED;
118 uint8_t tpm_speed = CONFIG_TPM_SPI_SPEED;
119
120 if (!read_efs_spi_settings(&read_mode, &fast_speed)) {
121 read_mode = CONFIG_EFS_SPI_READ_MODE;
122 fast_speed = CONFIG_EFS_SPI_SPEED;
123 }
124 mainboard_spi_cfg_override(&fast_speed, &read_mode);
125
126 if (fast_speed != CONFIG_EFS_SPI_SPEED) {
127 normal_speed = lower_speed(normal_speed, fast_speed);
128 tpm_speed = lower_speed(tpm_speed, fast_speed);
129 alt_speed = lower_speed(alt_speed, fast_speed);
130 }
131
132 fch_spi_set_read_mode((u32)read_mode);
133 fch_spi_set_spi100(normal_speed, fast_speed, alt_speed, tpm_speed);
134 }
135
136 void fch_spi_early_init(void)
137 {
138 lpc_enable_spi_rom(SPI_ROM_ENABLE);
139 lpc_enable_spi_prefetch();
140 fch_spi_configure_4dw_burst();
141 fch_spi_config_modes();
142 }