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