blob: c37ed07ba042338b7f15c454b433ea1b091b15fe [file] [log] [blame]
Nico Huber735b1862023-01-29 18:28:45 +00001/*
2 * This file is part of the flashrom project.
3 *
Nico Huber9512c9c2025-01-30 22:38:18 +01004 * Copyright (C) 2023 Nico Huber <nico.h@gmx.de>
5 *
Nico Huber735b1862023-01-29 18:28:45 +00006 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 */
16
Nico Huberf5fcd742023-03-11 17:11:12 +000017#include <stdbool.h>
Nico Huber735b1862023-01-29 18:28:45 +000018#include <stdint.h>
19#include <stdlib.h>
20#include <string.h>
21
22#include "flash.h"
23#include "hwaccess_physmap.h"
24#include "programmer.h"
25#include "spi.h"
26
27#define SPI100_FIFO_SIZE 71
28
29struct spi100 {
30 uint8_t *spibar;
Nico Hubere3c305d2023-01-29 21:45:56 +000031 uint8_t *memory;
32 size_t mapped_len;
Nico Huberf5fcd742023-03-11 17:11:12 +000033 bool no_4ba_mmap;
Nico Hubere3c305d2023-01-29 21:45:56 +000034
Nico Huber735b1862023-01-29 18:28:45 +000035 unsigned int altspeed;
36};
37
38static void spi100_write8(const struct spi100 *spi100, unsigned int reg, uint8_t val)
39{
40 mmio_writeb(val, spi100->spibar + reg);
41}
42
43static void spi100_write16(const struct spi100 *spi100, unsigned int reg, uint16_t val)
44{
45 mmio_writew(val, spi100->spibar + reg);
46}
47
48static void spi100_writen(const struct spi100 *spi100, unsigned int reg, const uint8_t *data, size_t len)
49{
50 for (; len > 0; --len, ++reg, ++data)
51 mmio_writeb(*data, spi100->spibar + reg);
52}
53
54static uint8_t spi100_read8(const struct spi100 *spi100, unsigned int reg)
55{
56 return mmio_readb(spi100->spibar + reg);
57}
58
59static uint16_t spi100_read16(const struct spi100 *spi100, unsigned int reg)
60{
61 return mmio_readw(spi100->spibar + reg);
62}
63
64static uint32_t spi100_read32(const struct spi100 *spi100, unsigned int reg)
65{
66 return mmio_readl(spi100->spibar + reg);
67}
68
69static void spi100_readn(const struct spi100 *spi100, unsigned int reg, uint8_t *data, size_t len)
70{
Nico Huber07058782023-01-29 19:56:39 +000071 mmio_readn_aligned(spi100->spibar + reg, data, len, 4);
Nico Huber735b1862023-01-29 18:28:45 +000072}
73
Nico Huber4661e7c2023-04-28 21:57:45 +000074static int spi100_mmap_read(struct flashctx *flash, uint8_t *dst, unsigned int start, unsigned int len)
75{
76 const struct spi100 *const spi100 = flash->mst.spi->data;
77 mmio_readn_aligned(spi100->memory + start, dst, len, 8);
78 return 0;
79}
80
Nico Huber735b1862023-01-29 18:28:45 +000081static int spi100_check_readwritecnt(const unsigned int writecnt, const unsigned int readcnt)
82{
83 if (writecnt < 1) {
84 msg_perr("ERROR: SPI controller needs to send at least 1 byte.\n");
85 return SPI_INVALID_LENGTH;
86 }
87
88 if (writecnt - 1 > SPI100_FIFO_SIZE) {
89 msg_perr("ERROR: SPI controller can not send %u bytes, it is limited to %u bytes.\n",
90 writecnt, SPI100_FIFO_SIZE + 1);
91 return SPI_INVALID_LENGTH;
92 }
93
94 const unsigned int maxreadcnt = SPI100_FIFO_SIZE - (writecnt - 1);
95 if (readcnt > maxreadcnt) {
96 msg_perr("ERROR: SPI controller can not receive %u bytes for this command,\n"
97 "it is limited to %u bytes write+read count.\n",
98 readcnt, SPI100_FIFO_SIZE + 1);
99 return SPI_INVALID_LENGTH;
100 }
101 return 0;
102}
103
104static int spi100_send_command(const struct flashctx *const flash,
105 const unsigned int writecnt, const unsigned int readcnt,
106 const unsigned char *const writearr, unsigned char *const readarr)
107{
Nico Huber9a11cbf2023-01-13 01:19:07 +0100108 const struct spi100 *const spi100 = flash->mst.spi->data;
Nico Huber735b1862023-01-29 18:28:45 +0000109
110 int ret = spi100_check_readwritecnt(writecnt, readcnt);
111 if (ret)
112 return ret;
113
114 spi100_write8(spi100, 0x45, writearr[0]); /* First "command" byte is sent separately. */
115 spi100_write8(spi100, 0x48, writecnt - 1);
116 spi100_write8(spi100, 0x4b, readcnt);
117 if (writecnt > 1)
118 spi100_writen(spi100, 0x80, &writearr[1], writecnt - 1);
119
Nico Huberd2472e52023-03-12 00:44:15 +0000120 /* Check if the command/address is allowed */
121 const uint32_t spi_cntrl0 = spi100_read32(spi100, 0x00);
122 if (spi_cntrl0 & (1 << 21)) {
123 msg_perr("ERROR: Illegal access for opcode 0x%02x!", writearr[0]);
124 return SPI_INVALID_OPCODE;
125 } else {
126 msg_pspew("%s: executing opcode 0x%02x.\n", __func__, writearr[0]);
127 }
128
Nico Huber735b1862023-01-29 18:28:45 +0000129 /* Trigger command */
130 spi100_write8(spi100, 0x47, BIT(7));
131
132 /* Wait for completion */
133 int timeout_us = 10*1000*1000;
134 uint32_t spistatus;
135 while (((spistatus = spi100_read32(spi100, 0x4c)) & BIT(31)) && timeout_us--)
136 programmer_delay(1);
137 if (spistatus & BIT(31)) {
138 msg_perr("ERROR: SPI transfer timed out (0x%08x)!\n", spistatus);
139 return SPI_PROGRAMMER_ERROR;
140 }
141 msg_pspew("%s: spistatus: 0x%08x\n", __func__, spistatus);
142
143 if (readcnt)
144 spi100_readn(spi100, 0x80 + writecnt - 1, readarr, readcnt);
145
146 return 0;
147}
148
Nico Hubere3c305d2023-01-29 21:45:56 +0000149static int spi100_read(struct flashctx *const flash, uint8_t *buf, unsigned int start, unsigned int len)
150{
Nico Huber9a11cbf2023-01-13 01:19:07 +0100151 const struct spi100 *const spi100 = flash->mst.spi->data;
Nico Huberc3b02dc2023-08-12 01:13:45 +0200152 const chipsize_t chip_size = flashprog_flash_getsize(flash);
Nico Huberf5fcd742023-03-11 17:11:12 +0000153
154 /* Don't consider memory mapping at all
155 if 4BA chips are not mapped as expected. */
156 if (chip_size > 16*MiB && spi100->no_4ba_mmap)
157 return default_spi_read(flash, buf, start, len);
Nico Hubere3c305d2023-01-29 21:45:56 +0000158
Nico Huber2d614d62023-03-11 01:06:15 +0100159 /* Where in the flash does the memory mapped part start?
160 Can be negative if the mapping is bigger than the chip. */
Nico Huberf5fcd742023-03-11 17:11:12 +0000161 const long long mapped_start = chip_size - spi100->mapped_len;
Nico Huber2d614d62023-03-11 01:06:15 +0100162
163 /* Use SPI100 engine for data outside the memory-mapped range. */
164 if ((long long)start < mapped_start) {
165 const chipsize_t unmapped_len = MIN(len, mapped_start - start);
Nico Hubere3c305d2023-01-29 21:45:56 +0000166 const int ret = default_spi_read(flash, buf, start, unmapped_len);
167 if (ret)
168 return ret;
169 start += unmapped_len;
170 buf += unmapped_len;
171 len -= unmapped_len;
172 }
173
Nico Huber2d614d62023-03-11 01:06:15 +0100174 /* Translate `start` to memory-mapped offset. */
175 start -= mapped_start;
176
Nico Huber4661e7c2023-04-28 21:57:45 +0000177 flashprog_read_chunked(flash, buf, start, len, MAX_DATA_READ_UNLIMITED, spi100_mmap_read);
Nico Hubere3c305d2023-01-29 21:45:56 +0000178
179 return 0;
180}
181
Nico Huber735b1862023-01-29 18:28:45 +0000182static int spi100_shutdown(void *data)
183{
184 struct spi100 *const spi100 = data;
185
186 const uint16_t speed_cfg = spi100_read16(spi100, 0x22);
187 spi100_write16(spi100, 0x22, (speed_cfg & ~0xf0) | spi100->altspeed << 4);
188
189 free(spi100);
190 return 0;
191}
192
193static struct spi_master spi100_master = {
Nico Huberf5fcd742023-03-11 17:11:12 +0000194 .features = SPI_MASTER_4BA | SPI_MASTER_NO_4BA_MODES,
Nico Huber735b1862023-01-29 18:28:45 +0000195 .max_data_read = SPI100_FIFO_SIZE - 4, /* Account for up to 4 address bytes. */
196 .max_data_write = SPI100_FIFO_SIZE - 4,
197 .command = spi100_send_command,
198 .multicommand = default_spi_send_multicommand,
Nico Hubere3c305d2023-01-29 21:45:56 +0000199 .read = spi100_read,
Nico Huber735b1862023-01-29 18:28:45 +0000200 .write_256 = default_spi_write_256,
201 .probe_opcode = default_spi_probe_opcode,
202 .shutdown = spi100_shutdown,
203};
204
205const char *const spimodes[] = {
206 "Normal read (up to 33MHz)",
207 "Reserved",
208 "Dual IO (1-1-2)",
209 "Quad IO (1-1-4)",
210 "Dual IO (1-2-2)",
211 "Quad IO (1-1-4)",
212 "Normal read (up to 66MHz)",
213 "Fast Read",
214};
215
216const struct {
217 unsigned int khz;
218 const char *speed;
219} spispeeds[] = {
220 { 66666, "66.66 MHz" },
221 { 33333, "33.33 MHz" },
222 { 22222, "22.22 MHz" },
223 { 16666, "16.66 MHz" },
224 { 100000, "100 MHz" },
225 { 800, "800 kHz" },
226 { 0, "Reserved" },
227 { 0, "Reserved" },
228};
229
230static void spi100_print(const struct spi100 *const spi100)
231{
232 const uint32_t spi_cntrl0 = spi100_read32(spi100, 0x00);
233 msg_pdbg("(0x%08" PRIx32 ") ", spi_cntrl0);
234 msg_pdbg("SpiArbEnable=%u, ", spi_cntrl0 >> 19 & 1);
235 msg_pdbg("IllegalAccess=%u, ", spi_cntrl0 >> 21 & 1);
236 msg_pdbg("SpiAccessMacRomEn=%u, ", spi_cntrl0 >> 22 & 1);
237 msg_pdbg("SpiHostAccessRomEn=%u,\n", spi_cntrl0 >> 23 & 1);
238 msg_pdbg(" ");
239 msg_pdbg("ArbWaitCount=%u, ", spi_cntrl0 >> 24 & 7);
240 msg_pdbg("SpiBridgeDisable=%u, ", spi_cntrl0 >> 27 & 1);
241 msg_pdbg("SpiClkGate=%u,\n", spi_cntrl0 >> 28 & 1);
242 msg_pdbg(" ");
243 msg_pdbg("SpiReadMode=%s, ", spimodes[(spi_cntrl0 >> 28 & 6) | (spi_cntrl0 >> 18 & 1)]);
244 msg_pdbg("SpiBusy=%u\n", spi_cntrl0 >> 31 & 1);
245
246 const uint8_t alt_spi_cs = spi100_read8(spi100, 0x1d);
247 msg_pdbg("Using SPI_CS%u\n", alt_spi_cs & 0x3);
248
249 const uint16_t speed_cfg = spi100_read16(spi100, 0x22);
250 msg_pdbg("NormSpeed: %s\n", spispeeds[speed_cfg >> 12 & 0xf].speed);
251 msg_pdbg("FastSpeed: %s\n", spispeeds[speed_cfg >> 8 & 0xf].speed);
252 msg_pdbg("AltSpeed: %s\n", spispeeds[speed_cfg >> 4 & 0xf].speed);
253 msg_pdbg("TpmSpeed: %s\n", spispeeds[speed_cfg >> 0 & 0xf].speed);
254}
255
Nico Huberf5fcd742023-03-11 17:11:12 +0000256static void spi100_check_4ba(struct spi100 *const spi100)
257{
258 const uint16_t rom2_addr_override = spi100_read16(spi100, 0x30);
259 const uint32_t addr32_ctrl0 = spi100_read32(spi100, 0x50);
260 const uint32_t addr32_ctrl3 = spi100_read32(spi100, 0x5c);
261
262 spi100->no_4ba_mmap = false;
263
264 /* Most bits are undocumented ("reserved"), so we play safe. */
265 if (rom2_addr_override != 0x14c0) {
266 msg_pdbg("ROM2 address override *not* in default configuration.\n");
267 spi100->no_4ba_mmap = true;
268 }
269
270 /* Check if the controller would use 4-byte addresses by itself. */
271 if (addr32_ctrl0 & 1) {
272 msg_pdbg("Memory-mapped access uses 32-bit addresses.\n");
273 } else {
274 msg_pdbg("Memory-mapped access uses 24-bit addresses.\n");
275 spi100->no_4ba_mmap = true;
276 }
277
278 /* Another override (xor'ed) for the most-significant address bits. */
279 if (addr32_ctrl3 & 0xff) {
280 msg_pdbg("SPI ROM page bits set: 0x%02x\n", addr32_ctrl3 & 0xff);
281 spi100->no_4ba_mmap = true;
282 }
283}
284
Nico Huber735b1862023-01-29 18:28:45 +0000285static void spi100_set_altspeed(struct spi100 *const spi100)
286{
287 const uint16_t speed_cfg = spi100_read16(spi100, 0x22);
288 const unsigned int normspeed = speed_cfg >> 12 & 0xf;
289 spi100->altspeed = speed_cfg >> 4 & 0xf;
290
291 /* Set SPI speed to 33MHz but not higher than `normal read` speed */
292 unsigned int altspeed;
293 if (spispeeds[normspeed].khz != 0 && spispeeds[normspeed].khz < 33333)
294 altspeed = normspeed;
295 else
296 altspeed = 1;
297
298 if (altspeed != spi100->altspeed) {
299 msg_pinfo("Setting SPI speed to %s.\n", spispeeds[altspeed].speed);
300 spi100_write16(spi100, 0x22, (speed_cfg & ~0xf0) | altspeed << 4);
301 }
302}
303
Nico Hubere3c305d2023-01-29 21:45:56 +0000304int amd_spi100_probe(void *const spibar, void *const memory_mapping, const size_t mapped_len)
Nico Huber735b1862023-01-29 18:28:45 +0000305{
306 struct spi100 *const spi100 = malloc(sizeof(*spi100));
307 if (!spi100) {
308 msg_perr("Out of memory!\n");
309 return ERROR_FATAL;
310 }
311 spi100->spibar = spibar;
Nico Hubere3c305d2023-01-29 21:45:56 +0000312 spi100->memory = memory_mapping;
313 spi100->mapped_len = mapped_len;
Nico Huber735b1862023-01-29 18:28:45 +0000314
315 spi100_print(spi100);
316
317 spi100_set_altspeed(spi100);
318
Nico Huberf5fcd742023-03-11 17:11:12 +0000319 spi100_check_4ba(spi100);
320
Nico Huber89569d62023-01-12 23:31:40 +0100321 return register_spi_master(&spi100_master, 0, spi100);
Nico Huber735b1862023-01-29 18:28:45 +0000322}