blob: d0d2f28905fbbb193b44bb016deec1648666fff9 [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
133 /* Use SPI100 engine for data outside the memory-mapped range */
134 const chipoff_t from_top = flashrom_flash_getsize(flash) - start;
135 if (from_top > spi100->mapped_len) {
136 const chipsize_t unmapped_len = MIN(len, from_top - spi100->mapped_len);
137 const int ret = default_spi_read(flash, buf, start, unmapped_len);
138 if (ret)
139 return ret;
140 start += unmapped_len;
141 buf += unmapped_len;
142 len -= unmapped_len;
143 }
144
145 mmio_readn_aligned(spi100->memory + start, buf, len, 8);
146
147 return 0;
148}
149
Nico Huber735b1862023-01-29 18:28:45 +0000150static int spi100_shutdown(void *data)
151{
152 struct spi100 *const spi100 = data;
153
154 const uint16_t speed_cfg = spi100_read16(spi100, 0x22);
155 spi100_write16(spi100, 0x22, (speed_cfg & ~0xf0) | spi100->altspeed << 4);
156
157 free(spi100);
158 return 0;
159}
160
161static struct spi_master spi100_master = {
162 .max_data_read = SPI100_FIFO_SIZE - 4, /* Account for up to 4 address bytes. */
163 .max_data_write = SPI100_FIFO_SIZE - 4,
164 .command = spi100_send_command,
165 .multicommand = default_spi_send_multicommand,
Nico Hubere3c305d2023-01-29 21:45:56 +0000166 .read = spi100_read,
Nico Huber735b1862023-01-29 18:28:45 +0000167 .write_256 = default_spi_write_256,
168 .probe_opcode = default_spi_probe_opcode,
169 .shutdown = spi100_shutdown,
170};
171
172const char *const spimodes[] = {
173 "Normal read (up to 33MHz)",
174 "Reserved",
175 "Dual IO (1-1-2)",
176 "Quad IO (1-1-4)",
177 "Dual IO (1-2-2)",
178 "Quad IO (1-1-4)",
179 "Normal read (up to 66MHz)",
180 "Fast Read",
181};
182
183const struct {
184 unsigned int khz;
185 const char *speed;
186} spispeeds[] = {
187 { 66666, "66.66 MHz" },
188 { 33333, "33.33 MHz" },
189 { 22222, "22.22 MHz" },
190 { 16666, "16.66 MHz" },
191 { 100000, "100 MHz" },
192 { 800, "800 kHz" },
193 { 0, "Reserved" },
194 { 0, "Reserved" },
195};
196
197static void spi100_print(const struct spi100 *const spi100)
198{
199 const uint32_t spi_cntrl0 = spi100_read32(spi100, 0x00);
200 msg_pdbg("(0x%08" PRIx32 ") ", spi_cntrl0);
201 msg_pdbg("SpiArbEnable=%u, ", spi_cntrl0 >> 19 & 1);
202 msg_pdbg("IllegalAccess=%u, ", spi_cntrl0 >> 21 & 1);
203 msg_pdbg("SpiAccessMacRomEn=%u, ", spi_cntrl0 >> 22 & 1);
204 msg_pdbg("SpiHostAccessRomEn=%u,\n", spi_cntrl0 >> 23 & 1);
205 msg_pdbg(" ");
206 msg_pdbg("ArbWaitCount=%u, ", spi_cntrl0 >> 24 & 7);
207 msg_pdbg("SpiBridgeDisable=%u, ", spi_cntrl0 >> 27 & 1);
208 msg_pdbg("SpiClkGate=%u,\n", spi_cntrl0 >> 28 & 1);
209 msg_pdbg(" ");
210 msg_pdbg("SpiReadMode=%s, ", spimodes[(spi_cntrl0 >> 28 & 6) | (spi_cntrl0 >> 18 & 1)]);
211 msg_pdbg("SpiBusy=%u\n", spi_cntrl0 >> 31 & 1);
212
213 const uint8_t alt_spi_cs = spi100_read8(spi100, 0x1d);
214 msg_pdbg("Using SPI_CS%u\n", alt_spi_cs & 0x3);
215
216 const uint16_t speed_cfg = spi100_read16(spi100, 0x22);
217 msg_pdbg("NormSpeed: %s\n", spispeeds[speed_cfg >> 12 & 0xf].speed);
218 msg_pdbg("FastSpeed: %s\n", spispeeds[speed_cfg >> 8 & 0xf].speed);
219 msg_pdbg("AltSpeed: %s\n", spispeeds[speed_cfg >> 4 & 0xf].speed);
220 msg_pdbg("TpmSpeed: %s\n", spispeeds[speed_cfg >> 0 & 0xf].speed);
221}
222
223static void spi100_set_altspeed(struct spi100 *const spi100)
224{
225 const uint16_t speed_cfg = spi100_read16(spi100, 0x22);
226 const unsigned int normspeed = speed_cfg >> 12 & 0xf;
227 spi100->altspeed = speed_cfg >> 4 & 0xf;
228
229 /* Set SPI speed to 33MHz but not higher than `normal read` speed */
230 unsigned int altspeed;
231 if (spispeeds[normspeed].khz != 0 && spispeeds[normspeed].khz < 33333)
232 altspeed = normspeed;
233 else
234 altspeed = 1;
235
236 if (altspeed != spi100->altspeed) {
237 msg_pinfo("Setting SPI speed to %s.\n", spispeeds[altspeed].speed);
238 spi100_write16(spi100, 0x22, (speed_cfg & ~0xf0) | altspeed << 4);
239 }
240}
241
Nico Hubere3c305d2023-01-29 21:45:56 +0000242int amd_spi100_probe(void *const spibar, void *const memory_mapping, const size_t mapped_len)
Nico Huber735b1862023-01-29 18:28:45 +0000243{
244 struct spi100 *const spi100 = malloc(sizeof(*spi100));
245 if (!spi100) {
246 msg_perr("Out of memory!\n");
247 return ERROR_FATAL;
248 }
249 spi100->spibar = spibar;
Nico Hubere3c305d2023-01-29 21:45:56 +0000250 spi100->memory = memory_mapping;
251 spi100->mapped_len = mapped_len;
Nico Huber735b1862023-01-29 18:28:45 +0000252
253 spi100_print(spi100);
254
255 spi100_set_altspeed(spi100);
256
257 return register_spi_master(&spi100_master, spi100);
258}