blob: a09aea6061a659aa63a726aab4ff54e25c47d96a [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
15#include <stdint.h>
16#include <stdlib.h>
17#include <string.h>
18
19#include "flash.h"
20#include "hwaccess_physmap.h"
21#include "programmer.h"
22#include "spi.h"
23
24#define SPI100_FIFO_SIZE 71
25
26struct spi100 {
27 uint8_t *spibar;
Nico Hubere3c305d2023-01-29 21:45:56 +000028 uint8_t *memory;
29 size_t mapped_len;
30
Nico Huber735b1862023-01-29 18:28:45 +000031 unsigned int altspeed;
32};
33
34static void spi100_write8(const struct spi100 *spi100, unsigned int reg, uint8_t val)
35{
36 mmio_writeb(val, spi100->spibar + reg);
37}
38
39static void spi100_write16(const struct spi100 *spi100, unsigned int reg, uint16_t val)
40{
41 mmio_writew(val, spi100->spibar + reg);
42}
43
44static void spi100_writen(const struct spi100 *spi100, unsigned int reg, const uint8_t *data, size_t len)
45{
46 for (; len > 0; --len, ++reg, ++data)
47 mmio_writeb(*data, spi100->spibar + reg);
48}
49
50static uint8_t spi100_read8(const struct spi100 *spi100, unsigned int reg)
51{
52 return mmio_readb(spi100->spibar + reg);
53}
54
55static uint16_t spi100_read16(const struct spi100 *spi100, unsigned int reg)
56{
57 return mmio_readw(spi100->spibar + reg);
58}
59
60static uint32_t spi100_read32(const struct spi100 *spi100, unsigned int reg)
61{
62 return mmio_readl(spi100->spibar + reg);
63}
64
65static void spi100_readn(const struct spi100 *spi100, unsigned int reg, uint8_t *data, size_t len)
66{
Nico Huber07058782023-01-29 19:56:39 +000067 mmio_readn_aligned(spi100->spibar + reg, data, len, 4);
Nico Huber735b1862023-01-29 18:28:45 +000068}
69
70static int spi100_check_readwritecnt(const unsigned int writecnt, const unsigned int readcnt)
71{
72 if (writecnt < 1) {
73 msg_perr("ERROR: SPI controller needs to send at least 1 byte.\n");
74 return SPI_INVALID_LENGTH;
75 }
76
77 if (writecnt - 1 > SPI100_FIFO_SIZE) {
78 msg_perr("ERROR: SPI controller can not send %u bytes, it is limited to %u bytes.\n",
79 writecnt, SPI100_FIFO_SIZE + 1);
80 return SPI_INVALID_LENGTH;
81 }
82
83 const unsigned int maxreadcnt = SPI100_FIFO_SIZE - (writecnt - 1);
84 if (readcnt > maxreadcnt) {
85 msg_perr("ERROR: SPI controller can not receive %u bytes for this command,\n"
86 "it is limited to %u bytes write+read count.\n",
87 readcnt, SPI100_FIFO_SIZE + 1);
88 return SPI_INVALID_LENGTH;
89 }
90 return 0;
91}
92
93static int spi100_send_command(const struct flashctx *const flash,
94 const unsigned int writecnt, const unsigned int readcnt,
95 const unsigned char *const writearr, unsigned char *const readarr)
96{
97 const struct spi100 *const spi100 = flash->mst->spi.data;
98
99 int ret = spi100_check_readwritecnt(writecnt, readcnt);
100 if (ret)
101 return ret;
102
103 spi100_write8(spi100, 0x45, writearr[0]); /* First "command" byte is sent separately. */
104 spi100_write8(spi100, 0x48, writecnt - 1);
105 spi100_write8(spi100, 0x4b, readcnt);
106 if (writecnt > 1)
107 spi100_writen(spi100, 0x80, &writearr[1], writecnt - 1);
108
109 /* Trigger command */
110 spi100_write8(spi100, 0x47, BIT(7));
111
112 /* Wait for completion */
113 int timeout_us = 10*1000*1000;
114 uint32_t spistatus;
115 while (((spistatus = spi100_read32(spi100, 0x4c)) & BIT(31)) && timeout_us--)
116 programmer_delay(1);
117 if (spistatus & BIT(31)) {
118 msg_perr("ERROR: SPI transfer timed out (0x%08x)!\n", spistatus);
119 return SPI_PROGRAMMER_ERROR;
120 }
121 msg_pspew("%s: spistatus: 0x%08x\n", __func__, spistatus);
122
123 if (readcnt)
124 spi100_readn(spi100, 0x80 + writecnt - 1, readarr, readcnt);
125
126 return 0;
127}
128
Nico Hubere3c305d2023-01-29 21:45:56 +0000129static int spi100_read(struct flashctx *const flash, uint8_t *buf, unsigned int start, unsigned int len)
130{
131 const struct spi100 *const spi100 = flash->mst->spi.data;
132
Nico Huber2d614d62023-03-11 01:06:15 +0100133 /* Where in the flash does the memory mapped part start?
134 Can be negative if the mapping is bigger than the chip. */
135 const long long mapped_start = flashrom_flash_getsize(flash) - spi100->mapped_len;
136
137 /* Use SPI100 engine for data outside the memory-mapped range. */
138 if ((long long)start < mapped_start) {
139 const chipsize_t unmapped_len = MIN(len, mapped_start - start);
Nico Hubere3c305d2023-01-29 21:45:56 +0000140 const int ret = default_spi_read(flash, buf, start, unmapped_len);
141 if (ret)
142 return ret;
143 start += unmapped_len;
144 buf += unmapped_len;
145 len -= unmapped_len;
146 }
147
Nico Huber2d614d62023-03-11 01:06:15 +0100148 /* Translate `start` to memory-mapped offset. */
149 start -= mapped_start;
150
Nico Hubere3c305d2023-01-29 21:45:56 +0000151 mmio_readn_aligned(spi100->memory + start, buf, len, 8);
152
153 return 0;
154}
155
Nico Huber735b1862023-01-29 18:28:45 +0000156static int spi100_shutdown(void *data)
157{
158 struct spi100 *const spi100 = data;
159
160 const uint16_t speed_cfg = spi100_read16(spi100, 0x22);
161 spi100_write16(spi100, 0x22, (speed_cfg & ~0xf0) | spi100->altspeed << 4);
162
163 free(spi100);
164 return 0;
165}
166
167static struct spi_master spi100_master = {
168 .max_data_read = SPI100_FIFO_SIZE - 4, /* Account for up to 4 address bytes. */
169 .max_data_write = SPI100_FIFO_SIZE - 4,
170 .command = spi100_send_command,
171 .multicommand = default_spi_send_multicommand,
Nico Hubere3c305d2023-01-29 21:45:56 +0000172 .read = spi100_read,
Nico Huber735b1862023-01-29 18:28:45 +0000173 .write_256 = default_spi_write_256,
174 .probe_opcode = default_spi_probe_opcode,
175 .shutdown = spi100_shutdown,
176};
177
178const char *const spimodes[] = {
179 "Normal read (up to 33MHz)",
180 "Reserved",
181 "Dual IO (1-1-2)",
182 "Quad IO (1-1-4)",
183 "Dual IO (1-2-2)",
184 "Quad IO (1-1-4)",
185 "Normal read (up to 66MHz)",
186 "Fast Read",
187};
188
189const struct {
190 unsigned int khz;
191 const char *speed;
192} spispeeds[] = {
193 { 66666, "66.66 MHz" },
194 { 33333, "33.33 MHz" },
195 { 22222, "22.22 MHz" },
196 { 16666, "16.66 MHz" },
197 { 100000, "100 MHz" },
198 { 800, "800 kHz" },
199 { 0, "Reserved" },
200 { 0, "Reserved" },
201};
202
203static void spi100_print(const struct spi100 *const spi100)
204{
205 const uint32_t spi_cntrl0 = spi100_read32(spi100, 0x00);
206 msg_pdbg("(0x%08" PRIx32 ") ", spi_cntrl0);
207 msg_pdbg("SpiArbEnable=%u, ", spi_cntrl0 >> 19 & 1);
208 msg_pdbg("IllegalAccess=%u, ", spi_cntrl0 >> 21 & 1);
209 msg_pdbg("SpiAccessMacRomEn=%u, ", spi_cntrl0 >> 22 & 1);
210 msg_pdbg("SpiHostAccessRomEn=%u,\n", spi_cntrl0 >> 23 & 1);
211 msg_pdbg(" ");
212 msg_pdbg("ArbWaitCount=%u, ", spi_cntrl0 >> 24 & 7);
213 msg_pdbg("SpiBridgeDisable=%u, ", spi_cntrl0 >> 27 & 1);
214 msg_pdbg("SpiClkGate=%u,\n", spi_cntrl0 >> 28 & 1);
215 msg_pdbg(" ");
216 msg_pdbg("SpiReadMode=%s, ", spimodes[(spi_cntrl0 >> 28 & 6) | (spi_cntrl0 >> 18 & 1)]);
217 msg_pdbg("SpiBusy=%u\n", spi_cntrl0 >> 31 & 1);
218
219 const uint8_t alt_spi_cs = spi100_read8(spi100, 0x1d);
220 msg_pdbg("Using SPI_CS%u\n", alt_spi_cs & 0x3);
221
222 const uint16_t speed_cfg = spi100_read16(spi100, 0x22);
223 msg_pdbg("NormSpeed: %s\n", spispeeds[speed_cfg >> 12 & 0xf].speed);
224 msg_pdbg("FastSpeed: %s\n", spispeeds[speed_cfg >> 8 & 0xf].speed);
225 msg_pdbg("AltSpeed: %s\n", spispeeds[speed_cfg >> 4 & 0xf].speed);
226 msg_pdbg("TpmSpeed: %s\n", spispeeds[speed_cfg >> 0 & 0xf].speed);
227}
228
229static void spi100_set_altspeed(struct spi100 *const spi100)
230{
231 const uint16_t speed_cfg = spi100_read16(spi100, 0x22);
232 const unsigned int normspeed = speed_cfg >> 12 & 0xf;
233 spi100->altspeed = speed_cfg >> 4 & 0xf;
234
235 /* Set SPI speed to 33MHz but not higher than `normal read` speed */
236 unsigned int altspeed;
237 if (spispeeds[normspeed].khz != 0 && spispeeds[normspeed].khz < 33333)
238 altspeed = normspeed;
239 else
240 altspeed = 1;
241
242 if (altspeed != spi100->altspeed) {
243 msg_pinfo("Setting SPI speed to %s.\n", spispeeds[altspeed].speed);
244 spi100_write16(spi100, 0x22, (speed_cfg & ~0xf0) | altspeed << 4);
245 }
246}
247
Nico Hubere3c305d2023-01-29 21:45:56 +0000248int amd_spi100_probe(void *const spibar, void *const memory_mapping, const size_t mapped_len)
Nico Huber735b1862023-01-29 18:28:45 +0000249{
250 struct spi100 *const spi100 = malloc(sizeof(*spi100));
251 if (!spi100) {
252 msg_perr("Out of memory!\n");
253 return ERROR_FATAL;
254 }
255 spi100->spibar = spibar;
Nico Hubere3c305d2023-01-29 21:45:56 +0000256 spi100->memory = memory_mapping;
257 spi100->mapped_len = mapped_len;
Nico Huber735b1862023-01-29 18:28:45 +0000258
259 spi100_print(spi100);
260
261 spi100_set_altspeed(spi100);
262
263 return register_spi_master(&spi100_master, spi100);
264}