Debian Codegraph @

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

raw symbols SCIP
1 /* SPDX-License-Identifier: GPL-2.0-only */
2
3 #include <console/console.h>
4 #include <spi_flash.h>
5 #include <soc/pci_devs.h>
6 #include <amdblocks/lpc.h>
7 #include <amdblocks/smi.h>
8 #include <amdblocks/spi.h>
9 #include <device/pci_ops.h>
10 #include <lib.h>
11 #include <timer.h>
12 #include <types.h>
13
14 #define GRANULARITY_TEST_4k 0x0000f000 /* bits 15-12 */
15 #define WORD_TO_DWORD_UPPER(x) ((x << 16) & 0xffff0000)
16
17 /* SPI MMIO registers */
18 #define SPI_RESTRICTED_CMD1 0x04
19 #define SPI_RESTRICTED_CMD2 0x08
20 #define SPI_CNTRL1 0x0c
21 #define SPI_CMD_CODE 0x45
22 #define SPI_CMD_TRIGGER 0x47
23 #define SPI_CMD_TRIGGER_EXECUTE BIT(7)
24 #define SPI_TX_BYTE_COUNT 0x48
25 #define SPI_RX_BYTE_COUNT 0x4b
26
27 enum spi_dump_state_phase {
28 SPI_DUMP_STATE_BEFORE_CMD,
29 SPI_DUMP_STATE_AFTER_CMD,
30 };
31
32 static void dump_state(enum spi_dump_state_phase phase)
33 {
34 u8 dump_size;
35 uintptr_t addr;
36 u32 status;
37
38 if (!CONFIG(SOC_AMD_COMMON_BLOCK_SPI_DEBUG))
39 return;
40
41 switch (phase) {
42 case SPI_DUMP_STATE_BEFORE_CMD:
43 printk(BIOS_DEBUG, "SPI: Before execute\n");
44 break;
45 case SPI_DUMP_STATE_AFTER_CMD:
46 printk(BIOS_DEBUG, "SPI: Transaction finished\n");
47 break;
48 default: /* We shouldn't reach this */
49 return;
50 }
51
52 printk(BIOS_DEBUG, "Cntrl0: %x\n", spi_read32(SPI_CNTRL0));
53
54 status = spi_read32(SPI_STATUS);
55 printk(BIOS_DEBUG, "Status: %x\n", status);
56 printk(BIOS_DEBUG,
57 " Busy: %u, FIFO Read Pointer: %u, FIFO Write Pointer: %u, Done Bytes: %u\n",
58 !!(status & SPI_BUSY),
59 (status >> SPI_FIFO_RD_PTR_SHIFT) & SPI_FIFO_RD_PTR_MASK,
60 (status >> SPI_FIFO_WR_PTR_SHIFT) & SPI_FIFO_WR_PTR_MASK,
61 (status >> SPI_DONE_BYTE_COUNT_SHIFT) & SPI_DONE_BYTE_COUNT_MASK);
62
63 printk(BIOS_DEBUG, "CmdCode: %x\n", spi_read8(SPI_CMD_CODE));
64
65 addr = spi_get_bar() + SPI_FIFO;
66
67 switch (phase) {
68 case SPI_DUMP_STATE_BEFORE_CMD:
69 dump_size = spi_read8(SPI_TX_BYTE_COUNT);
70 printk(BIOS_DEBUG, "TxByteCount: %x\n", dump_size);
71 break;
72 case SPI_DUMP_STATE_AFTER_CMD:
73 dump_size = spi_read8(SPI_RX_BYTE_COUNT);
74 printk(BIOS_DEBUG, "RxByteCount: %x\n", dump_size);
75 addr += spi_read8(SPI_TX_BYTE_COUNT);
76 break;
77 }
78
79 if (dump_size > 0)
80 hexdump((void *)addr, dump_size);
81 }
82
83 static int wait_for_ready(void)
84 {
85 const uint32_t timeout_ms = 500;
86 struct stopwatch sw;
87
88 stopwatch_init_msecs_expire(&sw, timeout_ms);
89
90 do {
91 if (!(spi_read32(SPI_STATUS) & SPI_BUSY))
92 return 0;
93 } while (!stopwatch_expired(&sw));
94
95 return -1;
96 }
97
98 static int execute_command(void)
99 {
100 dump_state(SPI_DUMP_STATE_BEFORE_CMD);
101
102 spi_write8(SPI_CMD_TRIGGER, SPI_CMD_TRIGGER_EXECUTE);
103
104 if (wait_for_ready()) {
105 printk(BIOS_ERR, "FCH SPI Error: Timeout executing command\n");
106 return -1;
107 }
108
109 dump_state(SPI_DUMP_STATE_AFTER_CMD);
110
111 return 0;
112 }
113
114 void spi_init(void)
115 {
116 printk(BIOS_DEBUG, "%s: SPI BAR at 0x%08lx\n", __func__, spi_get_bar());
117 }
118
119 static uint8_t cmd_code;
120 static uint8_t tx_byte_count;
121 static uint8_t rx_byte_count;
122 static uint8_t fifo[SPI_FIFO_DEPTH];
123
124 void fch_spi_backup_registers(void)
125 {
126 /* When bus is locked no need for backup */
127 if (ENV_SMM && (spi_read8(SPI_MISC_CNTRL) & SPI_SEMAPHORE_BIOS_LOCKED))
128 return;
129
130 cmd_code = spi_read8(SPI_CMD_CODE);
131 tx_byte_count = spi_read8(SPI_TX_BYTE_COUNT);
132 rx_byte_count = spi_read8(SPI_RX_BYTE_COUNT);
133
134 for (int count = 0; count < SPI_FIFO_DEPTH; count++)
135 fifo[count] = spi_read8(SPI_FIFO + count);
136 }
137
138 void fch_spi_restore_registers(void)
139 {
140 /* When bus is locked no need for backup */
141 if (ENV_SMM && (spi_read8(SPI_MISC_CNTRL) & SPI_SEMAPHORE_BIOS_LOCKED))
142 return;
143
144 spi_write8(SPI_CMD_CODE, cmd_code);
145 spi_write8(SPI_TX_BYTE_COUNT, tx_byte_count);
146 spi_write8(SPI_RX_BYTE_COUNT, rx_byte_count);
147
148 for (int count = 0; count < SPI_FIFO_DEPTH; count++)
149 spi_write8(SPI_FIFO + count, fifo[count]);
150 }
151
152 static int spi_ctrlr_xfer(const struct spi_slave *slave, const void *dout,
153 size_t bytesout, void *din, size_t bytesin)
154 {
155 size_t count;
156 uint8_t cmd;
157 uint8_t *bufin = din;
158 const uint8_t *bufout = dout;
159
160 if (CONFIG(SOC_AMD_COMMON_BLOCK_SPI_DEBUG))
161 printk(BIOS_DEBUG, "%s(%zx, %zx)\n", __func__, bytesout, bytesin);
162
163 /* First byte is cmd which cannot be sent through FIFO */
164 cmd = bufout[0];
165 bufout++;
166 bytesout--;
167
168 /*
169 * Check if this is a write command attempting to transfer more bytes
170 * than the controller can handle. Iterations for writes are not
171 * supported here because each SPI write command needs to be preceded
172 * and followed by other SPI commands.
173 */
174 if (bytesout + bytesin > SPI_FIFO_DEPTH) {
175 printk(BIOS_WARNING, "FCH SPI: Too much to transfer, code error!\n");
176 return -1;
177 }
178
179 if (wait_for_ready()) {
180 printk(BIOS_ERR, "FCH SPI: Failed to acquire the SPI bus\n");
181 return -1;
182 }
183
184 spi_write8(SPI_CMD_CODE, cmd);
185 spi_write8(SPI_TX_BYTE_COUNT, bytesout);
186 spi_write8(SPI_RX_BYTE_COUNT, bytesin);
187
188 for (count = 0; count < bytesout; count++)
189 spi_write8(SPI_FIFO + count, bufout[count]);
190
191 if (execute_command())
192 return -1;
193
194 for (count = 0; count < bytesin; count++)
195 bufin[count] = spi_read8(SPI_FIFO + count + bytesout);
196
197 return 0;
198 }
199
200 static int xfer_vectors(const struct spi_slave *slave,
201 struct spi_op vectors[], size_t count)
202 {
203 int rc;
204
205 thread_mutex_lock(&spi_hw_mutex);
206 rc = spi_flash_vector_helper(slave, vectors, count, spi_ctrlr_xfer);
207 thread_mutex_unlock(&spi_hw_mutex);
208
209 return rc;
210 }
211
212 static int protect_a_range(u32 value)
213 {
214 u32 reg32;
215 u8 n;
216
217 /* find a free protection register */
218 for (n = 0; n < MAX_ROM_PROTECT_RANGES; n++) {
219 reg32 = pci_read_config32(SOC_LPC_DEV, ROM_PROTECT_RANGE_REG(n));
220 if (!reg32)
221 break;
222 }
223 if (n == MAX_ROM_PROTECT_RANGES)
224 return -1; /* no free range */
225
226 pci_write_config32(SOC_LPC_DEV, ROM_PROTECT_RANGE_REG(n), value);
227 return 0;
228 }
229
230 /*
231 * Protect range of SPI flash defined by region using the SPI flash controller.
232 *
233 * Note: Up to 4 ranges can be protected, though if a particular region requires more than one
234 * range, total number of regions decreases accordingly. Each range can be programmed to 4KiB or
235 * 64KiB granularity.
236 *
237 * Warning: If more than 1 region needs protection, and they need mixed protections (read/write)
238 * than start with the region that requires the most protection. After the restricted commands
239 * have been written, they can't be changed (write once). So if first region is write protection
240 * and second region is read protection, it's best to define first region as read and write
241 * protection.
242 */
243 static int fch_spi_flash_protect(const struct spi_flash *flash, const struct region *region,
244 const enum ctrlr_prot_type type)
245 {
246 int ret;
247 u32 reg32, rom_base, range_base;
248 size_t addr, len, gran_value, total_ranges, range;
249 bool granularity_64k = true; /* assume 64k granularity */
250
251 addr = region_offset(region);
252 len = region_sz(region);
253
254 reg32 = pci_read_config32(SOC_LPC_DEV, ROM_ADDRESS_RANGE2_START);
255 rom_base = WORD_TO_DWORD_UPPER(reg32);
256 if (addr < rom_base)
257 return -1;
258 range_base = addr % rom_base;
259
260 /* Define granularity to be used */
261 if (GRANULARITY_TEST_4k & range_base)
262 granularity_64k = false; /* use 4K granularity */
263 if (GRANULARITY_TEST_4k & len)
264 granularity_64k = false; /* use 4K granularity */
265
266 /* Define the first range and total number of ranges required */
267 if (granularity_64k) {
268 gran_value = 0x00010000; /* 64 KiB */
269 range_base = range_base >> 16;
270 } else {
271 gran_value = 0x00001000; /* 4 KiB */
272 range_base = range_base >> 12;
273 }
274 total_ranges = len / gran_value;
275 range_base &= RANGE_ADDR_MASK;
276
277 /* Create reg32 to be written into a range register and program required ranges */
278 reg32 = rom_base & ROM_BASE_MASK;
279 reg32 |= range_base;
280 if (granularity_64k)
281 reg32 |= RANGE_UNIT;
282 if (type & WRITE_PROTECT)
283 reg32 |= ROM_RANGE_WP;
284 if (type & READ_PROTECT)
285 reg32 |= ROM_RANGE_RP;
286
287 for (range = 0; range < total_ranges; range++) {
288 ret = protect_a_range(reg32);
289 if (ret)
290 return ret;
291 /*
292 * Next range (lower 8 bits). Range points to the start address of a region.
293 * The range value must be multiplied by the granularity (which is also the
294 * size of the region) to get the actual offset from the SPI start address.
295 */
296 reg32++;
297 }
298
299 /* define commands to be blocked if in range */
300 reg32 = 0;
301 if (type & WRITE_PROTECT) {
302 /* FIXME */
303 printk(BIOS_INFO, "%s: Write Enable and Write Cmd not blocked\n", __func__);
304 reg32 |= (flash->erase_cmd << 8);
305 }
306 if (type & READ_PROTECT) {
307 /* FIXME */
308 printk(BIOS_INFO, "%s: READ_PROTECT not supported.\n", __func__);
309 }
310
311 /* Final steps to protect region */
312 spi_write32(SPI_RESTRICTED_CMD1, reg32);
313 reg32 = spi_read32(SPI_CNTRL0);
314 reg32 &= ~SPI_ACCESS_MAC_ROM_EN;
315 spi_write32(SPI_CNTRL0, reg32);
316
317 return 0;
318 }
319
320 /* Block PSP SMI while operating on the SPI flash */
321 static int spi_ctrlr_claim_bus(const struct spi_slave *slave)
322 {
323 uint8_t reg8;
324
325 if (CONFIG(SOC_AMD_COMMON_BLOCK_PSP_SMI)) {
326 if (ENV_RAMSTAGE || ENV_SMM) {
327 reg8 = spi_read8(SPI_MISC_CNTRL);
328
329 if (reg8 & SPI_SEMAPHORE_BIOS_LOCKED)
330 return -1;
331 }
332 if (ENV_RAMSTAGE)
333 spi_write8(SPI_MISC_CNTRL, reg8 | SPI_SEMAPHORE_BIOS_LOCKED);
334 }
335
336 return 0;
337 }
338
339 /* Allow PSP SMI when not operating on the SPI flash */
340 static void spi_ctrlr_release_bus(const struct spi_slave *slave)
341 {
342 uint8_t reg8;
343
344 if (ENV_RAMSTAGE && CONFIG(SOC_AMD_COMMON_BLOCK_PSP_SMI)) {
345 reg8 = spi_read8(SPI_MISC_CNTRL);
346 spi_write8(SPI_MISC_CNTRL, reg8 & ~SPI_SEMAPHORE_BIOS_LOCKED);
347 }
348 }
349
350 static const struct spi_ctrlr fch_spi_flash_ctrlr = {
351 .claim_bus = spi_ctrlr_claim_bus,
352 .release_bus = spi_ctrlr_release_bus,
353 .xfer_vector = xfer_vectors,
354 .max_xfer_size = SPI_FIFO_DEPTH,
355 .flags = SPI_CNTRLR_DEDUCT_CMD_LEN | SPI_CNTRLR_DEDUCT_OPCODE_LEN,
356 .flash_protect = fch_spi_flash_protect,
357 };
358
359 const struct spi_ctrlr_buses spi_ctrlr_bus_map[] = {
360 {
361 .ctrlr = &fch_spi_flash_ctrlr,
362 .bus_start = 0,
363 .bus_end = 0,
364 },
365 };
366
367 const size_t spi_ctrlr_bus_map_count = ARRAY_SIZE(spi_ctrlr_bus_map);